Re: sync sync sync: have I missed any patches?
[p5sagit/p5-mst-13.2.git] / pp.h
1 /*    pp.h
2  *
3  *    Copyright (c) 1991-2001, 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 /*
21 =for apidoc AmU||SP
22 Stack pointer.  This is usually handled by C<xsubpp>.  See C<dSP> and
23 C<SPAGAIN>.
24
25 =for apidoc AmU||MARK
26 Stack marker variable for the XSUB.  See C<dMARK>.
27
28 =for apidoc Ams||PUSHMARK
29 Opening bracket for arguments on a callback.  See C<PUTBACK> and
30 L<perlcall>.
31
32 =for apidoc Ams||dSP
33 Declares a local copy of perl's stack pointer for the XSUB, available via
34 the C<SP> macro.  See C<SP>.
35
36 =for apidoc ms||djSP
37
38 Declare Just C<SP>. This is actually identical to C<dSP>, and declares
39 a local copy of perl's stack pointer, available via the C<SP> macro.
40 See C<SP>.  (Available for backward source code compatibility with the
41 old (Perl 5.005) thread model.)
42
43 =for apidoc Ams||dMARK
44 Declare a stack marker variable, C<mark>, for the XSUB.  See C<MARK> and
45 C<dORIGMARK>.
46
47 =for apidoc Ams||dORIGMARK
48 Saves the original stack mark for the XSUB.  See C<ORIGMARK>.
49
50 =for apidoc AmU||ORIGMARK
51 The original stack mark for the XSUB.  See C<dORIGMARK>.
52
53 =for apidoc Ams||SPAGAIN
54 Refetch the stack pointer.  Used after a callback.  See L<perlcall>.
55
56 =cut */
57
58 #undef SP /* Solaris 2.7 i386 has this in /usr/include/sys/reg.h */
59 #define SP sp
60 #define MARK mark
61 #define TARG targ
62
63 #define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
64                         markstack_grow();                       \
65                     *PL_markstack_ptr = (p) - PL_stack_base
66
67 #define TOPMARK         (*PL_markstack_ptr)
68 #define POPMARK         (*PL_markstack_ptr--)
69
70 #define dSP             register SV **sp = PL_stack_sp
71 #define djSP            dSP
72 #define dMARK           register SV **mark = PL_stack_base + POPMARK
73 #define dORIGMARK       I32 origmark = mark - PL_stack_base
74 #define SETORIGMARK     origmark = mark - PL_stack_base
75 #define ORIGMARK        (PL_stack_base + origmark)
76
77 #define SPAGAIN         sp = PL_stack_sp
78 #define MSPAGAIN        sp = PL_stack_sp; mark = ORIGMARK
79
80 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
81 #define dTARGETSTACKED SV * GETTARGETSTACKED
82
83 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
84 #define dTARGET SV * GETTARGET
85
86 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
87 #define dATARGET SV * GETATARGET
88
89 #define dTARG SV *targ
90
91 #define NORMAL PL_op->op_next
92 #define DIE return Perl_die
93
94 /*
95 =for apidoc Ams||PUTBACK
96 Closing bracket for XSUB arguments.  This is usually handled by C<xsubpp>.
97 See C<PUSHMARK> and L<perlcall> for other uses.
98
99 =for apidoc Amn|SV*|POPs
100 Pops an SV off the stack.
101
102 =for apidoc Amn|char*|POPp
103 Pops a string off the stack.
104
105 =for apidoc Amn|NV|POPn
106 Pops a double off the stack.
107
108 =for apidoc Amn|IV|POPi
109 Pops an integer off the stack.
110
111 =for apidoc Amn|long|POPl
112 Pops a long off the stack.
113
114 =cut
115 */
116
117 #define PUTBACK         PL_stack_sp = sp
118 #define RETURN          return PUTBACK, NORMAL
119 #define RETURNOP(o)     return PUTBACK, o
120 #define RETURNX(x)      return x, PUTBACK, NORMAL
121
122 #define POPs            (*sp--)
123 #define POPp            (SvPVx(POPs, PL_na))            /* deprecated */
124 #define POPpx           (SvPVx(POPs, n_a))
125 #define POPn            (SvNVx(POPs))
126 #define POPi            ((IV)SvIVx(POPs))
127 #define POPu            ((UV)SvUVx(POPs))
128 #define POPl            ((long)SvIVx(POPs))
129 #define POPul           ((unsigned long)SvIVx(POPs))
130 #ifdef HAS_QUAD
131 #define POPq            ((Quad_t)SvIVx(POPs))
132 #define POPuq           ((Uquad_t)SvUVx(POPs))
133 #endif
134
135 #define TOPs            (*sp)
136 #define TOPm1s          (*(sp-1))
137 #define TOPp1s          (*(sp+1))
138 #define TOPp            (SvPV(TOPs, PL_na))             /* deprecated */
139 #define TOPpx           (SvPV(TOPs, n_a))
140 #define TOPn            (SvNV(TOPs))
141 #define TOPi            ((IV)SvIV(TOPs))
142 #define TOPu            ((UV)SvUV(TOPs))
143 #define TOPl            ((long)SvIV(TOPs))
144 #define TOPul           ((unsigned long)SvUV(TOPs))
145 #ifdef HAS_QUAD
146 #define TOPq            ((Quad_t)SvIV(TOPs))
147 #define TOPuq           ((Uquad_t)SvUV(TOPs))
148 #endif
149
150 /* Go to some pains in the rare event that we must extend the stack. */
151
152 /*
153 =for apidoc Am|void|EXTEND|SP|int nitems
154 Used to extend the argument stack for an XSUB's return values. Once
155 used, guarantees that there is room for at least C<nitems> to be pushed
156 onto the stack.
157
158 =for apidoc Am|void|PUSHs|SV* sv
159 Push an SV onto the stack.  The stack must have room for this element.
160 Does not handle 'set' magic.  See C<XPUSHs>.
161
162 =for apidoc Am|void|PUSHp|char* str|STRLEN len
163 Push a string onto the stack.  The stack must have room for this element.
164 The C<len> indicates the length of the string.  Handles 'set' magic.  See
165 C<XPUSHp>.
166
167 =for apidoc Am|void|PUSHn|NV nv
168 Push a double onto the stack.  The stack must have room for this element.
169 Handles 'set' magic.  See C<XPUSHn>.
170
171 =for apidoc Am|void|PUSHi|IV iv
172 Push an integer onto the stack.  The stack must have room for this element.
173 Handles 'set' magic.  See C<XPUSHi>.
174
175 =for apidoc Am|void|PUSHu|UV uv
176 Push an unsigned integer onto the stack.  The stack must have room for this
177 element.  See C<XPUSHu>.
178
179 =for apidoc Am|void|XPUSHs|SV* sv
180 Push an SV onto the stack, extending the stack if necessary.  Does not
181 handle 'set' magic.  See C<PUSHs>.
182
183 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
184 Push a string onto the stack, extending the stack if necessary.  The C<len>
185 indicates the length of the string.  Handles 'set' magic.  See
186 C<PUSHp>.
187
188 =for apidoc Am|void|XPUSHn|NV nv
189 Push a double onto the stack, extending the stack if necessary.  Handles
190 'set' magic.  See C<PUSHn>.
191
192 =for apidoc Am|void|XPUSHi|IV iv
193 Push an integer onto the stack, extending the stack if necessary.  Handles
194 'set' magic. See C<PUSHi>.
195
196 =for apidoc Am|void|XPUSHu|UV uv
197 Push an unsigned integer onto the stack, extending the stack if necessary.
198 See C<PUSHu>.
199
200 =cut
201 */
202
203 #define EXTEND(p,n)     STMT_START { if (PL_stack_max - p < (n)) {              \
204                             sp = stack_grow(sp,p, (int) (n));           \
205                         } } STMT_END
206
207 /* Same thing, but update mark register too. */
208 #define MEXTEND(p,n)    STMT_START {if (PL_stack_max - p < (n)) {               \
209                             int markoff = mark - PL_stack_base;         \
210                             sp = stack_grow(sp,p,(int) (n));            \
211                             mark = PL_stack_base + markoff;             \
212                         } } STMT_END
213
214 #define PUSHs(s)        (*++sp = (s))
215 #define PUSHTARG        STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
216 #define PUSHp(p,l)      STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
217 #define PUSHn(n)        STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
218 #define PUSHi(i)        STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
219 #define PUSHu(u)        STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
220
221 #define XPUSHs(s)       STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
222 #define XPUSHTARG       STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
223 #define XPUSHp(p,l)     STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
224 #define XPUSHn(n)       STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
225 #define XPUSHi(i)       STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
226 #define XPUSHu(u)       STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
227 #define XPUSHundef      STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
228
229 #define SETs(s)         (*sp = s)
230 #define SETTARG         STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
231 #define SETp(p,l)       STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
232 #define SETn(n)         STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
233 #define SETi(i)         STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
234 #define SETu(u)         STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
235
236 #define dTOPss          SV *sv = TOPs
237 #define dPOPss          SV *sv = POPs
238 #define dTOPnv          NV value = TOPn
239 #define dPOPnv          NV value = POPn
240 #define dTOPiv          IV value = TOPi
241 #define dPOPiv          IV value = POPi
242 #define dTOPuv          UV value = TOPu
243 #define dPOPuv          UV value = POPu
244 #ifdef HAS_QUAD
245 #define dTOPqv          Quad_t value = TOPu
246 #define dPOPqv          Quad_t value = POPu
247 #define dTOPuqv         Uquad_t value = TOPuq
248 #define dPOPuqv         Uquad_t value = POPuq
249 #endif
250
251 #define dPOPXssrl(X)    SV *right = POPs; SV *left = CAT2(X,s)
252 #define dPOPXnnrl(X)    NV right = POPn; NV left = CAT2(X,n)
253 #define dPOPXiirl(X)    IV right = POPi; IV left = CAT2(X,i)
254
255 #define USE_LEFT(sv) \
256         (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
257 #define dPOPXnnrl_ul(X) \
258     NV right = POPn;                            \
259     SV *leftsv = CAT2(X,s);                             \
260     NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
261 #define dPOPXiirl_ul(X) \
262     IV right = POPi;                                    \
263     SV *leftsv = CAT2(X,s);                             \
264     IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
265
266 #define dPOPPOPssrl     dPOPXssrl(POP)
267 #define dPOPPOPnnrl     dPOPXnnrl(POP)
268 #define dPOPPOPnnrl_ul  dPOPXnnrl_ul(POP)
269 #define dPOPPOPiirl     dPOPXiirl(POP)
270 #define dPOPPOPiirl_ul  dPOPXiirl_ul(POP)
271
272 #define dPOPTOPssrl     dPOPXssrl(TOP)
273 #define dPOPTOPnnrl     dPOPXnnrl(TOP)
274 #define dPOPTOPnnrl_ul  dPOPXnnrl_ul(TOP)
275 #define dPOPTOPiirl     dPOPXiirl(TOP)
276 #define dPOPTOPiirl_ul  dPOPXiirl_ul(TOP)
277
278 #define RETPUSHYES      RETURNX(PUSHs(&PL_sv_yes))
279 #define RETPUSHNO       RETURNX(PUSHs(&PL_sv_no))
280 #define RETPUSHUNDEF    RETURNX(PUSHs(&PL_sv_undef))
281
282 #define RETSETYES       RETURNX(SETs(&PL_sv_yes))
283 #define RETSETNO        RETURNX(SETs(&PL_sv_no))
284 #define RETSETUNDEF     RETURNX(SETs(&PL_sv_undef))
285
286 #define ARGTARG         PL_op->op_targ
287
288     /* See OPpTARGET_MY: */
289 #define MAXARG          (PL_op->op_private & 15)
290
291 #define SWITCHSTACK(f,t) \
292     STMT_START {                                                        \
293         AvFILLp(f) = sp - PL_stack_base;                                \
294         PL_stack_base = AvARRAY(t);                                     \
295         PL_stack_max = PL_stack_base + AvMAX(t);                        \
296         sp = PL_stack_sp = PL_stack_base + AvFILLp(t);                  \
297         PL_curstack = t;                                                \
298     } STMT_END
299
300 #define EXTEND_MORTAL(n) \
301     STMT_START {                                                        \
302         if (PL_tmps_ix + (n) >= PL_tmps_max)                            \
303             tmps_grow(n);                                               \
304     } STMT_END
305
306 #define AMGf_noright    1
307 #define AMGf_noleft     2
308 #define AMGf_assign     4
309 #define AMGf_unary      8
310
311 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
312           if (PL_amagic_generation) { \
313             SV* tmpsv; \
314             SV* right= *(sp); SV* left= *(sp-1);\
315             if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
316                 (tmpsv=amagic_call(left, \
317                                    right, \
318                                    CAT2(meth,_amg), \
319                                    (assign)? AMGf_assign: 0))) {\
320                SPAGAIN; \
321                (void)POPs; set(tmpsv); RETURN; } \
322           } \
323         } STMT_END
324
325 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
326 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
327
328 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef,  \
329                                         CAT2(meth,_amg),AMGf_noright | AMGf_unary)
330 #define AMG_CALLbinL(left,right,meth) \
331             amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
332
333 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
334           if (PL_amagic_generation) { \
335             SV* tmpsv; \
336             SV* arg= sp[shift]; \
337           am_again: \
338             if ((SvAMAGIC(arg))&&\
339                 (tmpsv=AMG_CALLun(arg,meth))) {\
340                SPAGAIN; if (shift) sp += shift; \
341                set(tmpsv); ret; } \
342           } \
343         } STMT_END
344
345 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
346
347 #define tryAMAGICun(meth)       tryAMAGICunW(meth,SETsvUN,0,RETURN)
348 #define tryAMAGICunSET(meth)    tryAMAGICunW(meth,SETs,0,RETURN)
349 #define tryAMAGICunTARGET(meth, shift)                                  \
350         { dSP; sp--;    /* get TARGET from below PL_stack_sp */         \
351             { dTARGETSTACKED;                                           \
352                 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
353
354 #define setAGAIN(ref) sv = ref;                                                 \
355   if (!SvROK(ref))                                                              \
356       Perl_croak(aTHX_ "Overloaded dereference did not return a reference");    \
357   if (ref != arg && SvRV(ref) != SvRV(arg)) {                                   \
358       arg = ref;                                                                \
359       goto am_again;                                                            \
360   }
361
362 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
363
364 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
365 #define SETsv(sv)       STMT_START {                                    \
366                 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY))            \
367                    { sv_setsv(TARG, (sv)); SETTARG; }                   \
368                 else SETs(sv); } STMT_END
369
370 #define SETsvUN(sv)     STMT_START {                                    \
371                 if (SvFLAGS(TARG) & SVs_PADMY)          \
372                    { sv_setsv(TARG, (sv)); SETTARG; }                   \
373                 else SETs(sv); } STMT_END
374
375 /* newSVsv does not behave as advertised, so we copy missing
376  * information by hand */
377
378 /* SV* ref causes confusion with the member variable
379    changed SV* ref to SV* tmpRef */
380 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv);      \
381   if (SvREFCNT(tmpRef)>1) {                 \
382     SvREFCNT_dec(tmpRef);                   \
383     SvRV(rv)=AMG_CALLun(rv,copy);        \
384   } } STMT_END
385
386 /*
387 =for apidoc mU||LVRET
388 True if this op will be the return value of an lvalue subroutine
389
390 =cut */
391 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())