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 if (UNLIKELY(++PL_markstack_ptr == PL_markstack_max)) \
62 *PL_markstack_ptr = (I32)((p) - PL_stack_base);\
65 #define TOPMARK (*PL_markstack_ptr)
66 #define POPMARK (*PL_markstack_ptr--)
68 #define dSP SV **sp = PL_stack_sp
70 #define dMARK SV **mark = PL_stack_base + POPMARK
71 #define dORIGMARK const I32 origmark = (I32)(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
92 =for apidoc Ams||PUTBACK
93 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
94 See C<PUSHMARK> and L<perlcall> for other uses.
96 =for apidoc Amn|SV*|POPs
97 Pops an SV off the stack.
99 =for apidoc Amn|char*|POPp
100 Pops a string off the stack.
102 =for apidoc Amn|char*|POPpx
103 Pops a string off the stack. Identical to POPp. There are two names for
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)
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))
138 #define TOPm1s (*(sp-1))
139 #define TOPp1s (*(sp+1))
141 #define TOPpx (SvPV_nolen(TOPs))
142 #define TOPn (SvNV(TOPs))
143 #define TOPi ((IV)SvIV(TOPs))
144 #define TOPu ((UV)SvUV(TOPs))
145 #define TOPl ((long)SvIV(TOPs))
146 #define TOPul ((unsigned long)SvUV(TOPs))
148 /* Go to some pains in the rare event that we must extend the stack. */
151 =for apidoc Am|void|EXTEND|SP|SSize_t nitems
152 Used to extend the argument stack for an XSUB's return values. Once
153 used, guarantees that there is room for at least C<nitems> to be pushed
156 =for apidoc Am|void|PUSHs|SV* sv
157 Push an SV onto the stack. The stack must have room for this element.
158 Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
159 C<XPUSHs> and C<XPUSHmortal>.
161 =for apidoc Am|void|PUSHp|char* str|STRLEN len
162 Push a string onto the stack. The stack must have room for this element.
163 The C<len> indicates the length of the string. Handles 'set' magic. Uses
164 C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
165 call multiple C<TARG>-oriented macros to return lists from XSUB's - see
166 C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
168 =for apidoc Am|void|PUSHn|NV nv
169 Push a double onto the stack. The stack must have room for this element.
170 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
171 called to declare it. Do not call multiple C<TARG>-oriented macros to
172 return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
175 =for apidoc Am|void|PUSHi|IV iv
176 Push an integer 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<mPUSHi> instead. See also C<XPUSHi> and
182 =for apidoc Am|void|PUSHu|UV uv
183 Push an unsigned integer onto the stack. The stack must have room for this
184 element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
185 should be called to declare it. Do not call multiple C<TARG>-oriented
186 macros to return lists from XSUB's - see C<mPUSHu> instead. See also
187 C<XPUSHu> and C<mXPUSHu>.
189 =for apidoc Am|void|XPUSHs|SV* sv
190 Push an SV onto the stack, extending the stack if necessary. Does not
191 handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
192 C<PUSHs> and C<PUSHmortal>.
194 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
195 Push a string onto the stack, extending the stack if necessary. The C<len>
196 indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
197 C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
198 multiple C<TARG>-oriented macros to return lists from XSUB's - see
199 C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
201 =for apidoc Am|void|XPUSHn|NV nv
202 Push a double onto the stack, extending the stack if necessary. Handles
203 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
204 declare it. Do not call multiple C<TARG>-oriented macros to return lists
205 from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
207 =for apidoc Am|void|XPUSHi|IV iv
208 Push an integer onto the stack, extending the stack if necessary. Handles
209 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
210 declare it. Do not call multiple C<TARG>-oriented macros to return lists
211 from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
213 =for apidoc Am|void|XPUSHu|UV uv
214 Push an unsigned integer onto the stack, extending the stack if necessary.
215 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
216 called to declare it. Do not call multiple C<TARG>-oriented macros to
217 return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
220 =for apidoc Am|void|mPUSHs|SV* sv
221 Push an SV onto the stack and mortalizes the SV. The stack must have room
222 for this element. Does not use C<TARG>. See also C<PUSHs> and C<mXPUSHs>.
224 =for apidoc Am|void|PUSHmortal
225 Push a new mortal SV onto the stack. The stack must have room for this
226 element. Does not use C<TARG>. See also C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
228 =for apidoc Am|void|mPUSHp|char* str|STRLEN len
229 Push a string onto the stack. The stack must have room for this element.
230 The C<len> indicates the length of the string. Does not use C<TARG>.
231 See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
233 =for apidoc Am|void|mPUSHn|NV nv
234 Push a double onto the stack. The stack must have room for this element.
235 Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn> and C<XPUSHn>.
237 =for apidoc Am|void|mPUSHi|IV iv
238 Push an integer onto the stack. The stack must have room for this element.
239 Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi> and C<XPUSHi>.
241 =for apidoc Am|void|mPUSHu|UV uv
242 Push an unsigned integer onto the stack. The stack must have room for this
243 element. Does not use C<TARG>. See also C<PUSHu>, C<mXPUSHu> and C<XPUSHu>.
245 =for apidoc Am|void|mXPUSHs|SV* sv
246 Push an SV onto the stack, extending the stack if necessary and mortalizes
247 the SV. Does not use C<TARG>. See also C<XPUSHs> and C<mPUSHs>.
249 =for apidoc Am|void|XPUSHmortal
250 Push a new mortal SV onto the stack, extending the stack if necessary.
251 Does not use C<TARG>. See also C<XPUSHs>, C<PUSHmortal> and C<PUSHs>.
253 =for apidoc Am|void|mXPUSHp|char* str|STRLEN len
254 Push a string onto the stack, extending the stack if necessary. The C<len>
255 indicates the length of the string. Does not use C<TARG>. See also C<XPUSHp>,
256 C<mPUSHp> and C<PUSHp>.
258 =for apidoc Am|void|mXPUSHn|NV nv
259 Push a double onto the stack, extending the stack if necessary.
260 Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and C<PUSHn>.
262 =for apidoc Am|void|mXPUSHi|IV iv
263 Push an integer onto the stack, extending the stack if necessary.
264 Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and C<PUSHi>.
266 =for apidoc Am|void|mXPUSHu|UV uv
267 Push an unsigned integer onto the stack, extending the stack if necessary.
268 Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu> and C<PUSHu>.
273 #ifdef STRESS_REALLOC
274 # define EXTEND(p,n) (void)(sp = stack_grow(sp,p, (SSize_t)(n)))
275 /* Same thing, but update mark register too. */
276 # define MEXTEND(p,n) STMT_START { \
277 const int markoff = mark - PL_stack_base; \
278 sp = stack_grow(sp,p,(SSize_t) (n)); \
279 mark = PL_stack_base + markoff; \
282 # define EXTEND(p,n) (void)(UNLIKELY(PL_stack_max - p < (SSize_t)(n)) && \
283 (sp = stack_grow(sp,p, (SSize_t) (n))))
285 /* Same thing, but update mark register too. */
286 # define MEXTEND(p,n) STMT_START {if (UNLIKELY(PL_stack_max - p < (int)(n))) {\
287 const int markoff = mark - PL_stack_base; \
288 sp = stack_grow(sp,p,(SSize_t) (n)); \
289 mark = PL_stack_base + markoff; \
293 #define PUSHs(s) (*++sp = (s))
294 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
295 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
296 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
297 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
298 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
300 #define XPUSHs(s) (EXTEND(sp,1), *++sp = (s))
301 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
302 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
303 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
304 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
305 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
306 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
308 #define mPUSHs(s) PUSHs(sv_2mortal(s))
309 #define PUSHmortal PUSHs(sv_newmortal())
310 #define mPUSHp(p,l) PUSHs(newSVpvn_flags((p), (l), SVs_TEMP))
311 #define mPUSHn(n) sv_setnv(PUSHmortal, (NV)(n))
312 #define mPUSHi(i) sv_setiv(PUSHmortal, (IV)(i))
313 #define mPUSHu(u) sv_setuv(PUSHmortal, (UV)(u))
315 #define mXPUSHs(s) XPUSHs(sv_2mortal(s))
316 #define XPUSHmortal XPUSHs(sv_newmortal())
317 #define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); mPUSHp((p), (l)); } STMT_END
318 #define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv(PUSHmortal, (NV)(n)); } STMT_END
319 #define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv(PUSHmortal, (IV)(i)); } STMT_END
320 #define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv(PUSHmortal, (UV)(u)); } STMT_END
322 #define SETs(s) (*sp = s)
323 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
324 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
325 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
326 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
327 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
329 #define dTOPss SV *sv = TOPs
330 #define dPOPss SV *sv = POPs
331 #define dTOPnv NV value = TOPn
332 #define dPOPnv NV value = POPn
333 #define dPOPnv_nomg NV value = (sp--, SvNV_nomg(TOPp1s))
334 #define dTOPiv IV value = TOPi
335 #define dPOPiv IV value = POPi
336 #define dTOPuv UV value = TOPu
337 #define dPOPuv UV value = POPu
339 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
340 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
341 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
343 #define USE_LEFT(sv) \
344 (SvOK(sv) || !(PL_op->op_flags & OPf_STACKED))
345 #define dPOPXiirl_ul_nomg(X) \
346 IV right = (sp--, SvIV_nomg(TOPp1s)); \
347 SV *leftsv = CAT2(X,s); \
348 IV left = USE_LEFT(leftsv) ? SvIV_nomg(leftsv) : 0
350 #define dPOPPOPssrl dPOPXssrl(POP)
351 #define dPOPPOPnnrl dPOPXnnrl(POP)
352 #define dPOPPOPiirl dPOPXiirl(POP)
354 #define dPOPTOPssrl dPOPXssrl(TOP)
355 #define dPOPTOPnnrl dPOPXnnrl(TOP)
356 #define dPOPTOPnnrl_nomg \
357 NV right = SvNV_nomg(TOPs); NV left = (sp--, SvNV_nomg(TOPs))
358 #define dPOPTOPiirl dPOPXiirl(TOP)
359 #define dPOPTOPiirl_ul_nomg dPOPXiirl_ul_nomg(TOP)
360 #define dPOPTOPiirl_nomg \
361 IV right = SvIV_nomg(TOPs); IV left = (sp--, SvIV_nomg(TOPs))
363 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
364 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
365 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
367 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
368 #define RETSETNO RETURNX(SETs(&PL_sv_no))
369 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
371 #define ARGTARG PL_op->op_targ
373 /* See OPpTARGET_MY: */
374 #define MAXARG (PL_op->op_private & 15)
376 #define SWITCHSTACK(f,t) \
378 AvFILLp(f) = sp - PL_stack_base; \
379 PL_stack_base = AvARRAY(t); \
380 PL_stack_max = PL_stack_base + AvMAX(t); \
381 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
385 #define EXTEND_MORTAL(n) \
387 if (UNLIKELY(PL_tmps_ix + (n) >= PL_tmps_max)) \
391 #define AMGf_noright 1
392 #define AMGf_noleft 2
393 #define AMGf_assign 4
395 #define AMGf_numeric 0x10 /* for Perl_try_amagic_bin */
396 #define AMGf_set 0x20 /* for Perl_try_amagic_bin */
397 #define AMGf_want_list 0x40
400 /* do SvGETMAGIC on the stack args before checking for overload */
402 #define tryAMAGICun_MG(method, flags) STMT_START { \
403 if ( UNLIKELY((SvFLAGS(TOPs) & (SVf_ROK|SVs_GMG))) \
404 && Perl_try_amagic_un(aTHX_ method, flags)) \
407 #define tryAMAGICbin_MG(method, flags) STMT_START { \
408 if ( UNLIKELY(((SvFLAGS(TOPm1s)|SvFLAGS(TOPs)) & (SVf_ROK|SVs_GMG))) \
409 && Perl_try_amagic_bin(aTHX_ method, flags)) \
413 #define AMG_CALLunary(sv,meth) \
414 amagic_call(sv,&PL_sv_undef, meth, AMGf_noright | AMGf_unary)
416 /* No longer used in core. Use AMG_CALLunary instead */
417 #define AMG_CALLun(sv,meth) AMG_CALLunary(sv, CAT2(meth,_amg))
419 #define tryAMAGICunTARGETlist(meth, jump) \
424 int gimme = GIMME_V; \
425 if (UNLIKELY(SvAMAGIC(arg) && \
426 (tmpsv = amagic_call(arg, &PL_sv_undef, meth, \
427 AMGf_want_list | AMGf_noright \
431 if (gimme == G_VOID) { \
432 (void)POPs; /* XXX ??? */ \
434 else if (gimme == G_ARRAY) { \
437 assert(SvTYPE(tmpsv) == SVt_PVAV); \
438 len = av_tindex((AV *)tmpsv) + 1; \
439 (void)POPs; /* get rid of the arg */ \
441 for (i = 0; i < len; ++i) \
442 PUSHs(av_shift((AV *)tmpsv)); \
444 else { /* AMGf_want_scalar */ \
445 dATARGET; /* just use the arg's location */ \
446 sv_setsv(TARG, tmpsv); \
453 OP *jump_o = NORMAL->op_next; \
454 while (jump_o->op_type == OP_NULL) \
455 jump_o = jump_o->op_next; \
456 assert(jump_o->op_type == OP_ENTERSUB); \
457 PL_markstack_ptr--; \
458 return jump_o->op_next; \
464 /* This is no longer used anywhere in the core. You might wish to consider
465 calling amagic_deref_call() directly, as it has a cleaner interface. */
466 #define tryAMAGICunDEREF(meth) \
468 sv = amagic_deref_call(*sp, CAT2(meth,_amg)); \
473 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
474 #define SETsv(sv) STMT_START { \
475 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
476 { sv_setsv(TARG, (sv)); SETTARG; } \
477 else SETs(sv); } STMT_END
479 #define SETsvUN(sv) STMT_START { \
480 if (SvFLAGS(TARG) & SVs_PADMY) \
481 { sv_setsv(TARG, (sv)); SETTARG; } \
482 else SETs(sv); } STMT_END
485 =for apidoc mU||LVRET
486 True if this op will be the return value of an lvalue subroutine
489 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())
491 #define SvCANEXISTDELETE(sv) \
493 || !(mg = mg_find((const SV *) sv, PERL_MAGIC_tied)) \
494 || ( (stash = SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(sv), mg)))) \
495 && gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
496 && gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
502 /* These are just for Perl_tied_method(), which is not part of the public API.
503 Use 0x04 rather than the next available bit, to help the compiler if the
504 architecture can generate more efficient instructions. */
505 # define TIED_METHOD_MORTALIZE_NOT_NEEDED 0x04
506 # define TIED_METHOD_ARGUMENTS_ON_STACK 0x08
507 # define TIED_METHOD_SAY 0x10
509 /* Used in various places that need to dereference a glob or globref */
510 # define MAYBE_DEREF_GV_flags(sv,phlags) \
512 (void)(phlags & SV_GMAGIC && (SvGETMAGIC(sv),0)), \
515 : SvROK(sv) && SvTYPE(SvRV(sv)) <= SVt_PVLV && \
516 (SvGETMAGIC(SvRV(sv)), isGV_with_GP(SvRV(sv))) \
520 # define MAYBE_DEREF_GV(sv) MAYBE_DEREF_GV_flags(sv,SV_GMAGIC)
521 # define MAYBE_DEREF_GV_nomg(sv) MAYBE_DEREF_GV_flags(sv,0)
523 # define FIND_RUNCV_padid_eq 1
524 # define FIND_RUNCV_level_eq 2
530 * c-indentation-style: bsd
532 * indent-tabs-mode: nil
535 * ex: set ts=8 sts=4 sw=4 et: