0e6383c58a07d8767c1369ad734e5ec722cb5a53
[p5sagit/p5-mst-13.2.git] / pp.h
1 /*    pp.h
2  *
3  *    Copyright (c) 1991-1999, 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 * Perl_##s(pTHX)
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 Perl_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))            /* deprecated */
62 #define POPpx           (SvPVx(POPs, n_a))
63 #define POPn            (SvNVx(POPs))
64 #define POPi            ((IV)SvIVx(POPs))
65 #define POPu            ((UV)SvUVx(POPs))
66 #define POPl            ((long)SvIVx(POPs))
67 #define POPul           ((unsigned long)SvIVx(POPs))
68 #ifdef HAS_QUAD
69 #define POPq            ((Quad_t)SvIVx(POPs))
70 #endif
71
72 #define TOPs            (*sp)
73 #define TOPp            (SvPV(TOPs, PL_na))             /* deprecated */
74 #define TOPpx           (SvPV(TOPs, n_a))
75 #define TOPn            (SvNV(TOPs))
76 #define TOPi            ((IV)SvIV(TOPs))
77 #define TOPu            ((UV)SvUV(TOPs))
78 #define TOPl            ((long)SvIV(TOPs))
79 #define TOPul           ((unsigned long)SvIV(TOPs))
80 #ifdef HAS_QUAD
81 #define TOPq            ((Quad_t)SvIV(TOPs))
82 #endif
83
84 /* Go to some pains in the rare event that we must extend the stack. */
85 #define EXTEND(p,n)     STMT_START { if (PL_stack_max - p < (n)) {              \
86                             sp = stack_grow(sp,p, (int) (n));           \
87                         } } STMT_END
88
89 /* Same thing, but update mark register too. */
90 #define MEXTEND(p,n)    STMT_START {if (PL_stack_max - p < (n)) {               \
91                             int markoff = mark - PL_stack_base;         \
92                             sp = stack_grow(sp,p,(int) (n));            \
93                             mark = PL_stack_base + markoff;             \
94                         } } STMT_END
95
96 #define PUSHs(s)        (*++sp = (s))
97 #define PUSHTARG        STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
98 #define PUSHp(p,l)      STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
99 #define PUSHn(n)        STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
100 #define PUSHi(i)        STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
101 #define PUSHu(u)        STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
102
103 #define XPUSHs(s)       STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
104 #define XPUSHTARG       STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
105 #define XPUSHp(p,l)     STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
106 #define XPUSHn(n)       STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
107 #define XPUSHi(i)       STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
108 #define XPUSHu(u)       STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
109 #define XPUSHundef      STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
110
111 #define SETs(s)         (*sp = s)
112 #define SETTARG         STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
113 #define SETp(p,l)       STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
114 #define SETn(n)         STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
115 #define SETi(i)         STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
116 #define SETu(u)         STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
117
118 #define dTOPss          SV *sv = TOPs
119 #define dPOPss          SV *sv = POPs
120 #define dTOPnv          NV value = TOPn
121 #define dPOPnv          NV value = POPn
122 #define dTOPiv          IV value = TOPi
123 #define dPOPiv          IV value = POPi
124 #define dTOPuv          UV value = TOPu
125 #define dPOPuv          UV value = POPu
126
127 #define dPOPXssrl(X)    SV *right = POPs; SV *left = CAT2(X,s)
128 #define dPOPXnnrl(X)    NV right = POPn; NV left = CAT2(X,n)
129 #define dPOPXiirl(X)    IV right = POPi; IV left = CAT2(X,i)
130
131 #define USE_LEFT(sv) \
132         (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
133 #define dPOPXnnrl_ul(X) \
134     NV right = POPn;                            \
135     SV *leftsv = CAT2(X,s);                             \
136     NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
137 #define dPOPXiirl_ul(X) \
138     IV right = POPi;                                    \
139     SV *leftsv = CAT2(X,s);                             \
140     IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
141
142 #define dPOPPOPssrl     dPOPXssrl(POP)
143 #define dPOPPOPnnrl     dPOPXnnrl(POP)
144 #define dPOPPOPnnrl_ul  dPOPXnnrl_ul(POP)
145 #define dPOPPOPiirl     dPOPXiirl(POP)
146 #define dPOPPOPiirl_ul  dPOPXiirl_ul(POP)
147
148 #define dPOPTOPssrl     dPOPXssrl(TOP)
149 #define dPOPTOPnnrl     dPOPXnnrl(TOP)
150 #define dPOPTOPnnrl_ul  dPOPXnnrl_ul(TOP)
151 #define dPOPTOPiirl     dPOPXiirl(TOP)
152 #define dPOPTOPiirl_ul  dPOPXiirl_ul(TOP)
153
154 #define RETPUSHYES      RETURNX(PUSHs(&PL_sv_yes))
155 #define RETPUSHNO       RETURNX(PUSHs(&PL_sv_no))
156 #define RETPUSHUNDEF    RETURNX(PUSHs(&PL_sv_undef))
157
158 #define RETSETYES       RETURNX(SETs(&PL_sv_yes))
159 #define RETSETNO        RETURNX(SETs(&PL_sv_no))
160 #define RETSETUNDEF     RETURNX(SETs(&PL_sv_undef))
161
162 #define ARGTARG         PL_op->op_targ
163
164     /* See OPpTARGET_MY: */
165 #define MAXARG          (PL_op->op_private & 15)
166
167 #define SWITCHSTACK(f,t) \
168     STMT_START {                                                        \
169         AvFILLp(f) = sp - PL_stack_base;                                \
170         PL_stack_base = AvARRAY(t);                                     \
171         PL_stack_max = PL_stack_base + AvMAX(t);                        \
172         sp = PL_stack_sp = PL_stack_base + AvFILLp(t);                  \
173         PL_curstack = t;                                                \
174     } STMT_END
175
176 #define EXTEND_MORTAL(n) \
177     STMT_START {                                                        \
178         if (PL_tmps_ix + (n) >= PL_tmps_max)                            \
179             tmps_grow(n);                                               \
180     } STMT_END
181
182 #define AMGf_noright    1
183 #define AMGf_noleft     2
184 #define AMGf_assign     4
185 #define AMGf_unary      8
186
187 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
188           if (PL_amagic_generation) { \
189             SV* tmpsv; \
190             SV* right= *(sp); SV* left= *(sp-1);\
191             if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
192                 (tmpsv=amagic_call(left, \
193                                    right, \
194                                    CAT2(meth,_amg), \
195                                    (assign)? AMGf_assign: 0))) {\
196                SPAGAIN; \
197                (void)POPs; set(tmpsv); RETURN; } \
198           } \
199         } STMT_END
200
201 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
202 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
203
204 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef,  \
205                                         CAT2(meth,_amg),AMGf_noright | AMGf_unary)
206 #define AMG_CALLbinL(left,right,meth) \
207             amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
208
209 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
210           if (PL_amagic_generation) { \
211             SV* tmpsv; \
212             SV* arg= sp[shift]; \
213           am_again: \
214             if ((SvAMAGIC(arg))&&\
215                 (tmpsv=AMG_CALLun(arg,meth))) {\
216                SPAGAIN; if (shift) sp += shift; \
217                set(tmpsv); ret; } \
218           } \
219         } STMT_END
220
221 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
222
223 #define tryAMAGICun(meth)       tryAMAGICunW(meth,SETsvUN,0,RETURN)
224 #define tryAMAGICunSET(meth)    tryAMAGICunW(meth,SETs,0,RETURN)
225 #define tryAMAGICunTARGET(meth, shift)                                  \
226         { dSP; sp--;    /* get TARGET from below PL_stack_sp */         \
227             { dTARGETSTACKED;                                           \
228                 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
229
230 #define setAGAIN(ref) sv = arg = ref;                                   \
231   if (!SvROK(ref))                                                      \
232       Perl_croak(aTHX_ "Overloaded dereference did not return a reference");    \
233   goto am_again;
234
235 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
236
237 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
238 #define SETsv(sv)       STMT_START {                                    \
239                 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY))            \
240                    { sv_setsv(TARG, (sv)); SETTARG; }                   \
241                 else SETs(sv); } STMT_END
242
243 #define SETsvUN(sv)     STMT_START {                                    \
244                 if (SvFLAGS(TARG) & SVs_PADMY)          \
245                    { sv_setsv(TARG, (sv)); SETTARG; }                   \
246                 else SETs(sv); } STMT_END
247
248 /* newSVsv does not behave as advertised, so we copy missing
249  * information by hand */
250
251 /* SV* ref causes confusion with the member variable
252    changed SV* ref to SV* tmpRef */
253 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv);      \
254   if (SvREFCNT(tmpRef)>1) {                 \
255     SvREFCNT_dec(tmpRef);                   \
256     SvRV(rv)=AMG_CALLun(rv,copy);        \
257   } } STMT_END