3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
11 #define PP(s) OP * Perl_##s(pTHX)
14 =head1 Stack Manipulation Macros
17 Stack pointer. This is usually handled by C<xsubpp>. See C<dSP> and
21 Stack marker variable for the XSUB. See C<dMARK>.
23 =for apidoc Am|void|PUSHMARK|SP
24 Opening bracket for arguments on a callback. See C<PUTBACK> and
28 Declares a local copy of perl's stack pointer for the XSUB, available via
29 the C<SP> macro. See C<SP>.
33 Declare Just C<SP>. This is actually identical to C<dSP>, and declares
34 a local copy of perl's stack pointer, available via the C<SP> macro.
35 See C<SP>. (Available for backward source code compatibility with the
36 old (Perl 5.005) thread model.)
38 =for apidoc Ams||dMARK
39 Declare a stack marker variable, C<mark>, for the XSUB. See C<MARK> and
42 =for apidoc Ams||dORIGMARK
43 Saves the original stack mark for the XSUB. See C<ORIGMARK>.
45 =for apidoc AmU||ORIGMARK
46 The original stack mark for the XSUB. See C<dORIGMARK>.
48 =for apidoc Ams||SPAGAIN
49 Refetch the stack pointer. Used after a callback. See L<perlcall>.
53 #undef SP /* Solaris 2.7 i386 has this in /usr/include/sys/reg.h */
60 if (++PL_markstack_ptr == PL_markstack_max) \
62 *PL_markstack_ptr = (p) - PL_stack_base; \
65 #define TOPMARK (*PL_markstack_ptr)
66 #define POPMARK (*PL_markstack_ptr--)
68 #define dSP register SV **sp = PL_stack_sp
70 #define dMARK register SV **mark = PL_stack_base + POPMARK
71 #define dORIGMARK const I32 origmark = mark - PL_stack_base
72 #define ORIGMARK (PL_stack_base + origmark)
74 #define SPAGAIN sp = PL_stack_sp
75 #define MSPAGAIN STMT_START { sp = PL_stack_sp; mark = ORIGMARK; } STMT_END
77 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
78 #define dTARGETSTACKED SV * GETTARGETSTACKED
80 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
81 #define dTARGET SV * GETTARGET
83 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
84 #define dATARGET SV * GETATARGET
86 #define dTARG SV *targ
88 #define NORMAL PL_op->op_next
89 #define DIE return Perl_die
90 #define DIE_NULL return DieNull
93 =for apidoc Ams||PUTBACK
94 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
95 See C<PUSHMARK> and L<perlcall> for other uses.
97 =for apidoc Amn|SV*|POPs
98 Pops an SV off the stack.
100 =for apidoc Amn|char*|POPp
101 Pops a string off the stack. Deprecated. New code should use POPpx.
103 =for apidoc Amn|char*|POPpx
104 Pops a string off the stack.
106 =for apidoc Amn|char*|POPpbytex
107 Pops a string off the stack which must consist of bytes i.e. characters < 256.
109 =for apidoc Amn|NV|POPn
110 Pops a double off the stack.
112 =for apidoc Amn|IV|POPi
113 Pops an integer off the stack.
115 =for apidoc Amn|long|POPl
116 Pops a long off the stack.
121 #define PUTBACK PL_stack_sp = sp
122 #define RETURN return PUTBACK, NORMAL
123 #define RETURNOP(o) return PUTBACK, o
124 #define RETURNX(x) return x, PUTBACK, NORMAL
127 #define POPp (SvPVx(POPs, PL_na)) /* deprecated */
128 #define POPpx (SvPVx_nolen(POPs))
129 #define POPpconstx (SvPVx_nolen_const(POPs))
130 #define POPpbytex (SvPVbytex_nolen(POPs))
131 #define POPn (SvNVx(POPs))
132 #define POPi ((IV)SvIVx(POPs))
133 #define POPu ((UV)SvUVx(POPs))
134 #define POPl ((long)SvIVx(POPs))
135 #define POPul ((unsigned long)SvIVx(POPs))
137 #define POPq ((Quad_t)SvIVx(POPs))
138 #define POPuq ((Uquad_t)SvUVx(POPs))
142 #define TOPm1s (*(sp-1))
143 #define TOPp1s (*(sp+1))
144 #define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
145 #define TOPpx (SvPV(TOPs, n_a))
146 #define TOPn (SvNV(TOPs))
147 #define TOPi ((IV)SvIV(TOPs))
148 #define TOPu ((UV)SvUV(TOPs))
149 #define TOPl ((long)SvIV(TOPs))
150 #define TOPul ((unsigned long)SvUV(TOPs))
152 #define TOPq ((Quad_t)SvIV(TOPs))
153 #define TOPuq ((Uquad_t)SvUV(TOPs))
156 /* Go to some pains in the rare event that we must extend the stack. */
159 =for apidoc Am|void|EXTEND|SP|int nitems
160 Used to extend the argument stack for an XSUB's return values. Once
161 used, guarantees that there is room for at least C<nitems> to be pushed
164 =for apidoc Am|void|PUSHs|SV* sv
165 Push an SV onto the stack. The stack must have room for this element.
166 Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
167 C<XPUSHs> and C<XPUSHmortal>.
169 =for apidoc Am|void|PUSHp|char* str|STRLEN len
170 Push a string onto the stack. The stack must have room for this element.
171 The C<len> indicates the length of the string. Handles 'set' magic. Uses
172 C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
173 call multiple C<TARG>-oriented macros to return lists from XSUB's - see
174 C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
176 =for apidoc Am|void|PUSHn|NV nv
177 Push a double onto the stack. The stack must have room for this element.
178 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
179 called to declare it. Do not call multiple C<TARG>-oriented macros to
180 return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
183 =for apidoc Am|void|PUSHi|IV iv
184 Push an integer onto the stack. The stack must have room for this element.
185 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
186 called to declare it. Do not call multiple C<TARG>-oriented macros to
187 return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
190 =for apidoc Am|void|PUSHu|UV uv
191 Push an unsigned integer onto the stack. The stack must have room for this
192 element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
193 should be called to declare it. Do not call multiple C<TARG>-oriented
194 macros to return lists from XSUB's - see C<mPUSHu> instead. See also
195 C<XPUSHu> and C<mXPUSHu>.
197 =for apidoc Am|void|XPUSHs|SV* sv
198 Push an SV onto the stack, extending the stack if necessary. Does not
199 handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
200 C<PUSHs> and C<PUSHmortal>.
202 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
203 Push a string onto the stack, extending the stack if necessary. The C<len>
204 indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
205 C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
206 multiple C<TARG>-oriented macros to return lists from XSUB's - see
207 C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
209 =for apidoc Am|void|XPUSHn|NV nv
210 Push a double onto the stack, extending the stack if necessary. Handles
211 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
212 declare it. Do not call multiple C<TARG>-oriented macros to return lists
213 from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
215 =for apidoc Am|void|XPUSHi|IV iv
216 Push an integer onto the stack, extending the stack if necessary. Handles
217 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
218 declare it. Do not call multiple C<TARG>-oriented macros to return lists
219 from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
221 =for apidoc Am|void|XPUSHu|UV uv
222 Push an unsigned integer onto the stack, extending the stack if necessary.
223 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
224 called to declare it. Do not call multiple C<TARG>-oriented macros to
225 return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
228 =for apidoc Am|void|PUSHmortal
229 Push a new mortal SV onto the stack. The stack must have room for this
230 element. Does not handle 'set' magic. Does not use C<TARG>. See also
231 C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
233 =for apidoc Am|void|mPUSHp|char* str|STRLEN len
234 Push a string onto the stack. The stack must have room for this element.
235 The C<len> indicates the length of the string. Handles 'set' magic. Does
236 not use C<TARG>. See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
238 =for apidoc Am|void|mPUSHn|NV nv
239 Push a double onto the stack. The stack must have room for this element.
240 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn>
243 =for apidoc Am|void|mPUSHi|IV iv
244 Push an integer onto the stack. The stack must have room for this element.
245 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi>
248 =for apidoc Am|void|mPUSHu|UV uv
249 Push an unsigned integer onto the stack. The stack must have room for this
250 element. Handles 'set' magic. Does not use C<TARG>. See also C<PUSHu>,
251 C<mXPUSHu> and C<XPUSHu>.
253 =for apidoc Am|void|XPUSHmortal
254 Push a new mortal SV onto the stack, extending the stack if necessary. Does
255 not handle 'set' magic. Does not use C<TARG>. See also C<XPUSHs>,
256 C<PUSHmortal> and C<PUSHs>.
258 =for apidoc Am|void|mXPUSHp|char* str|STRLEN len
259 Push a string onto the stack, extending the stack if necessary. The C<len>
260 indicates the length of the string. Handles 'set' magic. Does not use
261 C<TARG>. See also C<XPUSHp>, C<mPUSHp> and C<PUSHp>.
263 =for apidoc Am|void|mXPUSHn|NV nv
264 Push a double onto the stack, extending the stack if necessary. Handles
265 'set' magic. Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and
268 =for apidoc Am|void|mXPUSHi|IV iv
269 Push an integer onto the stack, extending the stack if necessary. Handles
270 'set' magic. Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and
273 =for apidoc Am|void|mXPUSHu|UV uv
274 Push an unsigned integer onto the stack, extending the stack if necessary.
275 Handles 'set' magic. Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu>
281 #define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
282 sp = stack_grow(sp,p, (int) (n)); \
285 /* Same thing, but update mark register too. */
286 #define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
287 int markoff = mark - PL_stack_base; \
288 sp = stack_grow(sp,p,(int) (n)); \
289 mark = PL_stack_base + markoff; \
292 #define PUSHs(s) (*++sp = (s))
293 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
294 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
295 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
296 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
297 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
299 #define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
300 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
301 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
302 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
303 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
304 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
305 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
307 #define PUSHmortal PUSHs(sv_newmortal())
308 #define mPUSHp(p,l) sv_setpvn_mg(PUSHmortal, (p), (l))
309 #define mPUSHn(n) sv_setnv_mg(PUSHmortal, (NV)(n))
310 #define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
311 #define mPUSHu(u) sv_setuv_mg(PUSHmortal, (UV)(u))
313 #define XPUSHmortal XPUSHs(sv_newmortal())
314 #define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); sv_setpvn_mg(PUSHmortal, (p), (l)); } STMT_END
315 #define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv_mg(PUSHmortal, (NV)(n)); } STMT_END
316 #define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv_mg(PUSHmortal, (IV)(i)); } STMT_END
317 #define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv_mg(PUSHmortal, (UV)(u)); } STMT_END
319 #define SETs(s) (*sp = s)
320 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
321 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
322 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
323 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
324 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
326 #define dTOPss SV *sv = TOPs
327 #define dPOPss SV *sv = POPs
328 #define dTOPnv NV value = TOPn
329 #define dPOPnv NV value = POPn
330 #define dTOPiv IV value = TOPi
331 #define dPOPiv IV value = POPi
332 #define dTOPuv UV value = TOPu
333 #define dPOPuv UV value = POPu
335 #define dTOPqv Quad_t value = TOPu
336 #define dPOPqv Quad_t value = POPu
337 #define dTOPuqv Uquad_t value = TOPuq
338 #define dPOPuqv Uquad_t value = POPuq
341 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
342 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
343 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
345 #define USE_LEFT(sv) \
346 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
347 #define dPOPXnnrl_ul(X) \
349 SV *leftsv = CAT2(X,s); \
350 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
351 #define dPOPXiirl_ul(X) \
353 SV *leftsv = CAT2(X,s); \
354 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
356 #define dPOPPOPssrl dPOPXssrl(POP)
357 #define dPOPPOPnnrl dPOPXnnrl(POP)
358 #define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
359 #define dPOPPOPiirl dPOPXiirl(POP)
360 #define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
362 #define dPOPTOPssrl dPOPXssrl(TOP)
363 #define dPOPTOPnnrl dPOPXnnrl(TOP)
364 #define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
365 #define dPOPTOPiirl dPOPXiirl(TOP)
366 #define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
368 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
369 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
370 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
372 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
373 #define RETSETNO RETURNX(SETs(&PL_sv_no))
374 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
376 #define ARGTARG PL_op->op_targ
378 /* See OPpTARGET_MY: */
379 #define MAXARG (PL_op->op_private & 15)
381 #define SWITCHSTACK(f,t) \
383 AvFILLp(f) = sp - PL_stack_base; \
384 PL_stack_base = AvARRAY(t); \
385 PL_stack_max = PL_stack_base + AvMAX(t); \
386 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
390 #define EXTEND_MORTAL(n) \
392 if (PL_tmps_ix + (n) >= PL_tmps_max) \
396 #define AMGf_noright 1
397 #define AMGf_noleft 2
398 #define AMGf_assign 4
401 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
402 if (PL_amagic_generation) { \
404 SV* right= *(sp); SV* left= *(sp-1);\
405 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
406 (tmpsv=amagic_call(left, \
409 (assign)? AMGf_assign: 0))) {\
411 (void)POPs; set(tmpsv); RETURN; } \
415 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
416 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
418 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
419 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
420 #define AMG_CALLbinL(left,right,meth) \
421 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
423 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
424 if (PL_amagic_generation) { \
426 SV* arg= sp[shift]; \
427 if(0) goto am_again; /* shut up unused warning */ \
429 if ((SvAMAGIC(arg))&&\
430 (tmpsv=AMG_CALLun(arg,meth))) {\
431 SPAGAIN; if (shift) sp += shift; \
436 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
438 #define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
439 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
440 #define tryAMAGICunTARGET(meth, shift) \
441 STMT_START { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
443 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}} STMT_END
445 #define setAGAIN(ref) \
449 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
450 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
456 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
458 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
459 #define SETsv(sv) STMT_START { \
460 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
461 { sv_setsv(TARG, (sv)); SETTARG; } \
462 else SETs(sv); } STMT_END
464 #define SETsvUN(sv) STMT_START { \
465 if (SvFLAGS(TARG) & SVs_PADMY) \
466 { sv_setsv(TARG, (sv)); SETTARG; } \
467 else SETs(sv); } STMT_END
469 /* newSVsv does not behave as advertised, so we copy missing
470 * information by hand */
472 /* SV* ref causes confusion with the member variable
473 changed SV* ref to SV* tmpRef */
474 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
475 if (SvREFCNT(tmpRef)>1) { \
476 SvRV_set(rv, AMG_CALLun(rv,copy)); \
477 SvREFCNT_dec(tmpRef); \
481 =for apidoc mU||LVRET
482 True if this op will be the return value of an lvalue subroutine
485 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())