This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
442fdc60cae41c26f512966020d9ede946d67c47
[perl5.git] / pp.h
1 /*    pp.h
2  *
3  *    Copyright (c) 1991-1997, Larry Wall
4  *
5  *    You may distribute under the terms of either the GNU General Public
6  *    License or the Artistic License, as specified in the README file.
7  *
8  */
9
10 #ifdef USE_THREADS
11 #define ARGS thr
12 #define dARGS struct perl_thread *thr;
13 #else
14 #define ARGS
15 #define dARGS
16 #endif /* USE_THREADS */
17
18 #define PP(s) OP * s(ARGSproto)
19
20 #define SP sp
21 #define MARK mark
22 #define TARG targ
23
24 #define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
25                         markstack_grow();                       \
26                     *PL_markstack_ptr = (p) - PL_stack_base
27
28 #define TOPMARK         (*PL_markstack_ptr)
29 #define POPMARK         (*PL_markstack_ptr--)
30
31 #define djSP            register SV **sp = PL_stack_sp
32 #define dSP             dTHR; djSP
33 #define dMARK           register SV **mark = PL_stack_base + POPMARK
34 #define dORIGMARK       I32 origmark = mark - PL_stack_base
35 #define SETORIGMARK     origmark = mark - PL_stack_base
36 #define ORIGMARK        (PL_stack_base + origmark)
37
38 #define SPAGAIN         sp = PL_stack_sp
39 #define MSPAGAIN        sp = PL_stack_sp; mark = ORIGMARK
40
41 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
42 #define dTARGETSTACKED SV * GETTARGETSTACKED
43
44 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
45 #define dTARGET SV * GETTARGET
46
47 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
48 #define dATARGET SV * GETATARGET
49
50 #define dTARG SV *targ
51
52 #define NORMAL PL_op->op_next
53 #define DIE return die
54
55 #define PUTBACK         PL_stack_sp = sp
56 #define RETURN          return PUTBACK, NORMAL
57 #define RETURNOP(o)     return PUTBACK, o
58 #define RETURNX(x)      return x, PUTBACK, NORMAL
59
60 #define POPs            (*sp--)
61 #define POPp            (SvPVx(POPs, PL_na))
62 #define POPn            (SvNVx(POPs))
63 #define POPi            ((IV)SvIVx(POPs))
64 #define POPu            ((UV)SvUVx(POPs))
65 #define POPl            ((long)SvIVx(POPs))
66
67 #define TOPs            (*sp)
68 #define TOPp            (SvPV(TOPs, PL_na))
69 #define TOPn            (SvNV(TOPs))
70 #define TOPi            ((IV)SvIV(TOPs))
71 #define TOPu            ((UV)SvUV(TOPs))
72 #define TOPl            ((long)SvIV(TOPs))
73
74 /* Go to some pains in the rare event that we must extend the stack. */
75 #define EXTEND(p,n)     STMT_START { if (PL_stack_max - p < (n)) {              \
76                             sp = stack_grow(sp,p, (int) (n));           \
77                         } } STMT_END
78
79 /* Same thing, but update mark register too. */
80 #define MEXTEND(p,n)    STMT_START {if (PL_stack_max - p < (n)) {               \
81                             int markoff = mark - PL_stack_base;         \
82                             sp = stack_grow(sp,p,(int) (n));            \
83                             mark = PL_stack_base + markoff;             \
84                         } } STMT_END
85
86 #define PUSHs(s)        (*++sp = (s))
87 #define PUSHTARG        STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
88 #define PUSHp(p,l)      STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
89 #define PUSHn(n)        STMT_START { sv_setnv(TARG, (double)(n)); PUSHTARG; } STMT_END
90 #define PUSHi(i)        STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
91 #define PUSHu(u)        STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
92
93 #define XPUSHs(s)       STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
94 #define XPUSHTARG       STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
95 #define XPUSHp(p,l)     STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
96 #define XPUSHn(n)       STMT_START { sv_setnv(TARG, (double)(n)); XPUSHTARG; } STMT_END
97 #define XPUSHi(i)       STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
98 #define XPUSHu(u)       STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
99
100 #define SETs(s)         (*sp = s)
101 #define SETTARG         STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
102 #define SETp(p,l)       STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
103 #define SETn(n)         STMT_START { sv_setnv(TARG, (double)(n)); SETTARG; } STMT_END
104 #define SETi(i)         STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
105 #define SETu(u)         STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
106
107 #define dTOPss          SV *sv = TOPs
108 #define dPOPss          SV *sv = POPs
109 #define dTOPnv          double value = TOPn
110 #define dPOPnv          double value = POPn
111 #define dTOPiv          IV value = TOPi
112 #define dPOPiv          IV value = POPi
113 #define dTOPuv          UV value = TOPu
114 #define dPOPuv          UV value = POPu
115
116 #define dPOPXssrl(X)    SV *right = POPs; SV *left = CAT2(X,s)
117 #define dPOPXnnrl(X)    double right = POPn; double left = CAT2(X,n)
118 #define dPOPXiirl(X)    IV right = POPi; IV left = CAT2(X,i)
119
120 #define USE_LEFT(sv) \
121         (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
122 #define dPOPXnnrl_ul(X) \
123     double right = POPn;                                \
124     SV *leftsv = CAT2(X,s);                             \
125     double left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
126 #define dPOPXiirl_ul(X) \
127     IV right = POPi;                                    \
128     SV *leftsv = CAT2(X,s);                             \
129     IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
130
131 #define dPOPPOPssrl     dPOPXssrl(POP)
132 #define dPOPPOPnnrl     dPOPXnnrl(POP)
133 #define dPOPPOPnnrl_ul  dPOPXnnrl_ul(POP)
134 #define dPOPPOPiirl     dPOPXiirl(POP)
135 #define dPOPPOPiirl_ul  dPOPXiirl_ul(POP)
136
137 #define dPOPTOPssrl     dPOPXssrl(TOP)
138 #define dPOPTOPnnrl     dPOPXnnrl(TOP)
139 #define dPOPTOPnnrl_ul  dPOPXnnrl_ul(TOP)
140 #define dPOPTOPiirl     dPOPXiirl(TOP)
141 #define dPOPTOPiirl_ul  dPOPXiirl_ul(TOP)
142
143 #define RETPUSHYES      RETURNX(PUSHs(&PL_sv_yes))
144 #define RETPUSHNO       RETURNX(PUSHs(&PL_sv_no))
145 #define RETPUSHUNDEF    RETURNX(PUSHs(&PL_sv_undef))
146
147 #define RETSETYES       RETURNX(SETs(&PL_sv_yes))
148 #define RETSETNO        RETURNX(SETs(&PL_sv_no))
149 #define RETSETUNDEF     RETURNX(SETs(&PL_sv_undef))
150
151 #define ARGTARG         PL_op->op_targ
152 #define MAXARG          PL_op->op_private
153
154 #define SWITCHSTACK(f,t) \
155     STMT_START {                                                        \
156         AvFILLp(f) = sp - PL_stack_base;                                        \
157         PL_stack_base = AvARRAY(t);                                     \
158         PL_stack_max = PL_stack_base + AvMAX(t);                                \
159         sp = PL_stack_sp = PL_stack_base + AvFILLp(t);                  \
160         PL_curstack = t;                                                        \
161     } STMT_END
162
163 #define EXTEND_MORTAL(n) \
164         STMT_START { \
165             if (PL_tmps_ix + (n) >= PL_tmps_max) \
166                 Renew(PL_tmps_stack, PL_tmps_max = PL_tmps_ix + (n) + 1, SV*); \
167         } STMT_END
168
169 #ifdef OVERLOAD
170
171 #define AMGf_noright    1
172 #define AMGf_noleft     2
173 #define AMGf_assign     4
174 #define AMGf_unary      8
175
176 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
177           if (PL_amagic_generation) { \
178             SV* tmpsv; \
179             SV* right= *(sp); SV* left= *(sp-1);\
180             if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
181                 (tmpsv=amagic_call(left, \
182                                    right, \
183                                    CAT2(meth,_amg), \
184                                    (assign)? AMGf_assign: 0))) {\
185                SPAGAIN; \
186                (void)POPs; set(tmpsv); RETURN; } \
187           } \
188         } STMT_END
189
190 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
191 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
192
193 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef,  \
194                                         CAT2(meth,_amg),AMGf_noright | AMGf_unary)
195 #define AMG_CALLbinL(left,right,meth) \
196             amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
197
198 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
199           if (PL_amagic_generation) { \
200             SV* tmpsv; \
201             SV* arg= sp[shift]; \
202           am_again: \
203             if ((SvAMAGIC(arg))&&\
204                 (tmpsv=AMG_CALLun(arg,meth))) {\
205                SPAGAIN; if (shift) sp += shift; \
206                set(tmpsv); ret; } \
207           } \
208         } STMT_END
209
210 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
211
212 #define tryAMAGICun     tryAMAGICunSET
213 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
214 #define tryAMAGICunTARGET(meth, shift)                                  \
215         { dSP; sp--;    /* get TARGET from below PL_stack_sp */         \
216             { dTARGETSTACKED;                                           \
217                 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
218
219 #define setAGAIN(ref) sv = arg = ref;                                   \
220   if (!SvROK(ref))                                                      \
221       croak("Overloaded dereference did not return a reference");       \
222   goto am_again;
223
224 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,)
225
226 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
227 #define SETsv(sv)       STMT_START {                                    \
228                 if (opASSIGN) { sv_setsv(TARG, (sv)); SETTARG; }        \
229                 else SETs(sv); } STMT_END
230
231 /* newSVsv does not behave as advertised, so we copy missing
232  * information by hand */
233
234 /* SV* ref causes confusion with the member variable
235    changed SV* ref to SV* tmpRef */
236 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv);      \
237   if (SvREFCNT(tmpRef)>1) {                 \
238     SvREFCNT_dec(tmpRef);                   \
239     SvRV(rv)=AMG_CALLun(rv,copy);        \
240   } } STMT_END
241 #else
242
243 #define tryAMAGICbin(a,b)
244 #define tryAMAGICbinSET(a,b)
245 #define tryAMAGICun(a)
246 #define tryAMAGICunSET(a)
247
248 #endif /* OVERLOAD */