3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 I32 * mark_stack_entry; \
61 if (UNLIKELY((mark_stack_entry = ++PL_markstack_ptr) == PL_markstack_max)) \
62 mark_stack_entry = markstack_grow(); \
63 *mark_stack_entry = (I32)((p) - PL_stack_base); \
66 #define TOPMARK (*PL_markstack_ptr)
67 #define POPMARK (*PL_markstack_ptr--)
69 #define dSP SV **sp = PL_stack_sp
71 #define dMARK SV **mark = PL_stack_base + POPMARK
72 #define dORIGMARK const I32 origmark = (I32)(mark - PL_stack_base)
73 #define ORIGMARK (PL_stack_base + origmark)
75 #define SPAGAIN sp = PL_stack_sp
76 #define MSPAGAIN STMT_START { sp = PL_stack_sp; mark = ORIGMARK; } STMT_END
78 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
79 #define dTARGETSTACKED SV * GETTARGETSTACKED
81 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
82 #define dTARGET SV * GETTARGET
84 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
85 #define dATARGET SV * GETATARGET
87 #define dTARG SV *targ
89 #define NORMAL PL_op->op_next
90 #define DIE return Perl_die
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.
103 =for apidoc Amn|char*|POPpx
104 Pops a string off the stack. Identical to POPp. There are two names for
107 =for apidoc Amn|char*|POPpbytex
108 Pops a string off the stack which must consist of bytes i.e. characters < 256.
110 =for apidoc Amn|NV|POPn
111 Pops a double off the stack.
113 =for apidoc Amn|IV|POPi
114 Pops an integer off the stack.
116 =for apidoc Amn|UV|POPu
117 Pops an unsigned integer off the stack.
119 =for apidoc Amn|long|POPl
120 Pops a long off the stack.
122 =for apidoc Amn|long|POPul
123 Pops an unsigned long off the stack.
128 #define PUTBACK PL_stack_sp = sp
129 #define RETURN return (PUTBACK, NORMAL)
130 #define RETURNOP(o) return (PUTBACK, o)
131 #define RETURNX(x) return (x, PUTBACK, NORMAL)
135 #define POPpx (SvPVx_nolen(POPs))
136 #define POPpconstx (SvPVx_nolen_const(POPs))
137 #define POPpbytex (SvPVbytex_nolen(POPs))
138 #define POPn (SvNVx(POPs))
139 #define POPi ((IV)SvIVx(POPs))
140 #define POPu ((UV)SvUVx(POPs))
141 #define POPl ((long)SvIVx(POPs))
142 #define POPul ((unsigned long)SvIVx(POPs))
145 #define TOPm1s (*(sp-1))
146 #define TOPp1s (*(sp+1))
148 #define TOPpx (SvPV_nolen(TOPs))
149 #define TOPn (SvNV(TOPs))
150 #define TOPi ((IV)SvIV(TOPs))
151 #define TOPu ((UV)SvUV(TOPs))
152 #define TOPl ((long)SvIV(TOPs))
153 #define TOPul ((unsigned long)SvUV(TOPs))
155 /* Go to some pains in the rare event that we must extend the stack. */
158 =for apidoc Am|void|EXTEND|SP|SSize_t nitems
159 Used to extend the argument stack for an XSUB's return values. Once
160 used, guarantees that there is room for at least C<nitems> to be pushed
163 =for apidoc Am|void|PUSHs|SV* sv
164 Push an SV onto the stack. The stack must have room for this element.
165 Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
166 C<XPUSHs> and C<XPUSHmortal>.
168 =for apidoc Am|void|PUSHp|char* str|STRLEN len
169 Push a string onto the stack. The stack must have room for this element.
170 The C<len> indicates the length of the string. Handles 'set' magic. Uses
171 C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
172 call multiple C<TARG>-oriented macros to return lists from XSUB's - see
173 C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
175 =for apidoc Am|void|PUSHn|NV nv
176 Push a double onto the stack. The stack must have room for this element.
177 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
178 called to declare it. Do not call multiple C<TARG>-oriented macros to
179 return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
182 =for apidoc Am|void|PUSHi|IV iv
183 Push an integer onto the stack. The stack must have room for this element.
184 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
185 called to declare it. Do not call multiple C<TARG>-oriented macros to
186 return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
189 =for apidoc Am|void|PUSHu|UV uv
190 Push an unsigned integer onto the stack. The stack must have room for this
191 element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
192 should be called to declare it. Do not call multiple C<TARG>-oriented
193 macros to return lists from XSUB's - see C<mPUSHu> instead. See also
194 C<XPUSHu> and C<mXPUSHu>.
196 =for apidoc Am|void|XPUSHs|SV* sv
197 Push an SV onto the stack, extending the stack if necessary. Does not
198 handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
199 C<PUSHs> and C<PUSHmortal>.
201 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
202 Push a string onto the stack, extending the stack if necessary. The C<len>
203 indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
204 C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
205 multiple C<TARG>-oriented macros to return lists from XSUB's - see
206 C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
208 =for apidoc Am|void|XPUSHn|NV nv
209 Push a double onto the stack, extending the stack if necessary. Handles
210 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
211 declare it. Do not call multiple C<TARG>-oriented macros to return lists
212 from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
214 =for apidoc Am|void|XPUSHi|IV iv
215 Push an integer onto the stack, extending the stack if necessary. Handles
216 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
217 declare it. Do not call multiple C<TARG>-oriented macros to return lists
218 from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
220 =for apidoc Am|void|XPUSHu|UV uv
221 Push an unsigned integer onto the stack, extending the stack if necessary.
222 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
223 called to declare it. Do not call multiple C<TARG>-oriented macros to
224 return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
227 =for apidoc Am|void|mPUSHs|SV* sv
228 Push an SV onto the stack and mortalizes the SV. The stack must have room
229 for this element. Does not use C<TARG>. See also C<PUSHs> and C<mXPUSHs>.
231 =for apidoc Am|void|PUSHmortal
232 Push a new mortal SV onto the stack. The stack must have room for this
233 element. Does not use C<TARG>. See also C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
235 =for apidoc Am|void|mPUSHp|char* str|STRLEN len
236 Push a string onto the stack. The stack must have room for this element.
237 The C<len> indicates the length of the string. Does not use C<TARG>.
238 See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
240 =for apidoc Am|void|mPUSHn|NV nv
241 Push a double onto the stack. The stack must have room for this element.
242 Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn> and C<XPUSHn>.
244 =for apidoc Am|void|mPUSHi|IV iv
245 Push an integer onto the stack. The stack must have room for this element.
246 Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi> and C<XPUSHi>.
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. Does not use C<TARG>. See also C<PUSHu>, C<mXPUSHu> and C<XPUSHu>.
252 =for apidoc Am|void|mXPUSHs|SV* sv
253 Push an SV onto the stack, extending the stack if necessary and mortalizes
254 the SV. Does not use C<TARG>. See also C<XPUSHs> and C<mPUSHs>.
256 =for apidoc Am|void|XPUSHmortal
257 Push a new mortal SV onto the stack, extending the stack if necessary.
258 Does not use C<TARG>. See also C<XPUSHs>, C<PUSHmortal> and C<PUSHs>.
260 =for apidoc Am|void|mXPUSHp|char* str|STRLEN len
261 Push a string onto the stack, extending the stack if necessary. The C<len>
262 indicates the length of the string. Does not use C<TARG>. See also C<XPUSHp>,
263 C<mPUSHp> and C<PUSHp>.
265 =for apidoc Am|void|mXPUSHn|NV nv
266 Push a double onto the stack, extending the stack if necessary.
267 Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and C<PUSHn>.
269 =for apidoc Am|void|mXPUSHi|IV iv
270 Push an integer onto the stack, extending the stack if necessary.
271 Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and C<PUSHi>.
273 =for apidoc Am|void|mXPUSHu|UV uv
274 Push an unsigned integer onto the stack, extending the stack if necessary.
275 Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu> and C<PUSHu>.
280 #ifdef STRESS_REALLOC
281 # define EXTEND(p,n) STMT_START { \
282 sp = stack_grow(sp,p,(SSize_t) (n)); \
283 PERL_UNUSED_VAR(sp); \
285 /* Same thing, but update mark register too. */
286 # define MEXTEND(p,n) STMT_START { \
287 const SSize_t markoff = mark - PL_stack_base; \
288 sp = stack_grow(sp,p,(SSize_t) (n)); \
289 mark = PL_stack_base + markoff; \
290 PERL_UNUSED_VAR(sp); \
293 # define EXTEND(p,n) STMT_START { \
294 if (UNLIKELY(PL_stack_max - p < (SSize_t)(n))) { \
295 sp = stack_grow(sp,p,(SSize_t) (n)); \
296 PERL_UNUSED_VAR(sp); \
298 /* Same thing, but update mark register too. */
299 # define MEXTEND(p,n) STMT_START { \
300 if (UNLIKELY(PL_stack_max - p < (SSize_t)(n))) { \
301 const SSize_t markoff = mark - PL_stack_base; \
302 sp = stack_grow(sp,p,(SSize_t) (n)); \
303 mark = PL_stack_base + markoff; \
304 PERL_UNUSED_VAR(sp); \
308 #define PUSHs(s) (*++sp = (s))
309 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
310 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
311 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
312 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
313 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
315 #define XPUSHs(s) STMT_START { EXTEND(sp,1); *++sp = (s); } STMT_END
316 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
317 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
318 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
319 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
320 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
321 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
323 #define mPUSHs(s) PUSHs(sv_2mortal(s))
324 #define PUSHmortal PUSHs(sv_newmortal())
325 #define mPUSHp(p,l) PUSHs(newSVpvn_flags((p), (l), SVs_TEMP))
326 #define mPUSHn(n) sv_setnv(PUSHmortal, (NV)(n))
327 #define mPUSHi(i) sv_setiv(PUSHmortal, (IV)(i))
328 #define mPUSHu(u) sv_setuv(PUSHmortal, (UV)(u))
330 #define mXPUSHs(s) XPUSHs(sv_2mortal(s))
331 #define XPUSHmortal XPUSHs(sv_newmortal())
332 #define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); mPUSHp((p), (l)); } STMT_END
333 #define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv(PUSHmortal, (NV)(n)); } STMT_END
334 #define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv(PUSHmortal, (IV)(i)); } STMT_END
335 #define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv(PUSHmortal, (UV)(u)); } STMT_END
337 #define SETs(s) (*sp = s)
338 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
339 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
340 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
341 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
342 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
344 #define dTOPss SV *sv = TOPs
345 #define dPOPss SV *sv = POPs
346 #define dTOPnv NV value = TOPn
347 #define dPOPnv NV value = POPn
348 #define dPOPnv_nomg NV value = (sp--, SvNV_nomg(TOPp1s))
349 #define dTOPiv IV value = TOPi
350 #define dPOPiv IV value = POPi
351 #define dTOPuv UV value = TOPu
352 #define dPOPuv UV value = POPu
354 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
355 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
356 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
358 #define USE_LEFT(sv) \
359 (SvOK(sv) || !(PL_op->op_flags & OPf_STACKED))
360 #define dPOPXiirl_ul_nomg(X) \
361 IV right = (sp--, SvIV_nomg(TOPp1s)); \
362 SV *leftsv = CAT2(X,s); \
363 IV left = USE_LEFT(leftsv) ? SvIV_nomg(leftsv) : 0
365 #define dPOPPOPssrl dPOPXssrl(POP)
366 #define dPOPPOPnnrl dPOPXnnrl(POP)
367 #define dPOPPOPiirl dPOPXiirl(POP)
369 #define dPOPTOPssrl dPOPXssrl(TOP)
370 #define dPOPTOPnnrl dPOPXnnrl(TOP)
371 #define dPOPTOPnnrl_nomg \
372 NV right = SvNV_nomg(TOPs); NV left = (sp--, SvNV_nomg(TOPs))
373 #define dPOPTOPiirl dPOPXiirl(TOP)
374 #define dPOPTOPiirl_ul_nomg dPOPXiirl_ul_nomg(TOP)
375 #define dPOPTOPiirl_nomg \
376 IV right = SvIV_nomg(TOPs); IV left = (sp--, SvIV_nomg(TOPs))
378 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
379 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
380 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
382 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
383 #define RETSETNO RETURNX(SETs(&PL_sv_no))
384 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
385 #define RETSETTARG STMT_START { SETTARG; RETURN; } STMT_END
387 #define ARGTARG PL_op->op_targ
389 #define MAXARG (PL_op->op_private & OPpARG4_MASK)
391 #define SWITCHSTACK(f,t) \
393 AvFILLp(f) = sp - PL_stack_base; \
394 PL_stack_base = AvARRAY(t); \
395 PL_stack_max = PL_stack_base + AvMAX(t); \
396 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
400 #define EXTEND_MORTAL(n) \
402 SSize_t eMiX = PL_tmps_ix + (n); \
403 if (UNLIKELY(eMiX >= PL_tmps_max)) \
404 (void)Perl_tmps_grow_p(aTHX_ eMiX); \
407 #define AMGf_noright 1
408 #define AMGf_noleft 2
409 #define AMGf_assign 4
411 #define AMGf_numeric 0x10 /* for Perl_try_amagic_bin */
412 #define AMGf_set 0x20 /* for Perl_try_amagic_bin */
413 #define AMGf_want_list 0x40
414 #define AMGf_numarg 0x80
417 /* do SvGETMAGIC on the stack args before checking for overload */
419 #define tryAMAGICun_MG(method, flags) STMT_START { \
420 if ( UNLIKELY((SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG))) \
421 && Perl_try_amagic_un(aTHX_ method, flags)) \
424 #define tryAMAGICbin_MG(method, flags) STMT_START { \
425 if ( UNLIKELY(((SvFLAGS(TOPm1s)|SvFLAGS(TOPs)) & (SVf_ROK|SVs_GMG))) \
426 && Perl_try_amagic_bin(aTHX_ method, flags)) \
430 #define AMG_CALLunary(sv,meth) \
431 amagic_call(sv,&PL_sv_undef, meth, AMGf_noright | AMGf_unary)
433 /* No longer used in core. Use AMG_CALLunary instead */
434 #define AMG_CALLun(sv,meth) AMG_CALLunary(sv, CAT2(meth,_amg))
436 #define tryAMAGICunTARGETlist(meth, jump) \
441 int gimme = GIMME_V; \
442 if (UNLIKELY(SvAMAGIC(arg) && \
443 (tmpsv = amagic_call(arg, &PL_sv_undef, meth, \
444 AMGf_want_list | AMGf_noright \
448 if (gimme == G_VOID) { \
451 else if (gimme == G_ARRAY) { \
454 assert(SvTYPE(tmpsv) == SVt_PVAV); \
455 len = av_tindex((AV *)tmpsv) + 1; \
456 (void)POPs; /* get rid of the arg */ \
458 for (i = 0; i < len; ++i) \
459 PUSHs(av_shift((AV *)tmpsv)); \
461 else { /* AMGf_want_scalar */ \
462 dATARGET; /* just use the arg's location */ \
463 sv_setsv(TARG, tmpsv); \
470 OP *jump_o = NORMAL->op_next; \
471 while (jump_o->op_type == OP_NULL) \
472 jump_o = jump_o->op_next; \
473 assert(jump_o->op_type == OP_ENTERSUB); \
474 PL_markstack_ptr--; \
475 return jump_o->op_next; \
481 /* This is no longer used anywhere in the core. You might wish to consider
482 calling amagic_deref_call() directly, as it has a cleaner interface. */
483 #define tryAMAGICunDEREF(meth) \
485 sv = amagic_deref_call(*sp, CAT2(meth,_amg)); \
490 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
493 =for apidoc mU||LVRET
494 True if this op will be the return value of an lvalue subroutine
497 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())
499 #define SvCANEXISTDELETE(sv) \
501 || !(mg = mg_find((const SV *) sv, PERL_MAGIC_tied)) \
502 || ( (stash = SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(sv), mg)))) \
503 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
504 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
510 /* These are just for Perl_tied_method(), which is not part of the public API.
511 Use 0x04 rather than the next available bit, to help the compiler if the
512 architecture can generate more efficient instructions. */
513 # define TIED_METHOD_MORTALIZE_NOT_NEEDED 0x04
514 # define TIED_METHOD_ARGUMENTS_ON_STACK 0x08
515 # define TIED_METHOD_SAY 0x10
517 /* Used in various places that need to dereference a glob or globref */
518 # define MAYBE_DEREF_GV_flags(sv,phlags) \
520 (void)(phlags & SV_GMAGIC && (SvGETMAGIC(sv),0)), \
523 : SvROK(sv) && SvTYPE(SvRV(sv)) <= SVt_PVLV && \
524 (SvGETMAGIC(SvRV(sv)), isGV_with_GP(SvRV(sv))) \
528 # define MAYBE_DEREF_GV(sv) MAYBE_DEREF_GV_flags(sv,SV_GMAGIC)
529 # define MAYBE_DEREF_GV_nomg(sv) MAYBE_DEREF_GV_flags(sv,0)
531 # define FIND_RUNCV_padid_eq 1
532 # define FIND_RUNCV_level_eq 2
537 * ex: set ts=8 sts=4 sw=4 et: