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 I32 origmark = mark - PL_stack_base
72 #define SETORIGMARK origmark = 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
91 #define DIE_NULL return DieNull
94 =for apidoc Ams||PUTBACK
95 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
96 See C<PUSHMARK> and L<perlcall> for other uses.
98 =for apidoc Amn|SV*|POPs
99 Pops an SV off the stack.
101 =for apidoc Amn|char*|POPp
102 Pops a string off the stack. Deprecated. New code should provide
103 a STRLEN n_a and use POPpx.
105 =for apidoc Amn|char*|POPpx
106 Pops a string off the stack.
107 Requires a variable STRLEN n_a in scope.
109 =for apidoc Amn|char*|POPpbytex
110 Pops a string off the stack which must consist of bytes i.e. characters < 256.
111 Requires a variable STRLEN n_a in scope.
113 =for apidoc Amn|NV|POPn
114 Pops a double off the stack.
116 =for apidoc Amn|IV|POPi
117 Pops an integer off the stack.
119 =for apidoc Amn|long|POPl
120 Pops a long off the stack.
125 #define PUTBACK PL_stack_sp = sp
126 #define RETURN return PUTBACK, NORMAL
127 #define RETURNOP(o) return PUTBACK, o
128 #define RETURNX(x) return x, PUTBACK, NORMAL
131 #define POPp (SvPVx(POPs, PL_na)) /* deprecated */
132 #define POPpx (SvPVx(POPs, n_a))
133 #define POPpbytex (SvPVbytex(POPs, n_a))
134 #define POPn (SvNVx(POPs))
135 #define POPi ((IV)SvIVx(POPs))
136 #define POPu ((UV)SvUVx(POPs))
137 #define POPl ((long)SvIVx(POPs))
138 #define POPul ((unsigned long)SvIVx(POPs))
140 #define POPq ((Quad_t)SvIVx(POPs))
141 #define POPuq ((Uquad_t)SvUVx(POPs))
145 #define TOPm1s (*(sp-1))
146 #define TOPp1s (*(sp+1))
147 #define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
148 #define TOPpx (SvPV(TOPs, n_a))
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 #define TOPq ((Quad_t)SvIV(TOPs))
156 #define TOPuq ((Uquad_t)SvUV(TOPs))
159 /* Go to some pains in the rare event that we must extend the stack. */
162 =for apidoc Am|void|EXTEND|SP|int nitems
163 Used to extend the argument stack for an XSUB's return values. Once
164 used, guarantees that there is room for at least C<nitems> to be pushed
167 =for apidoc Am|void|PUSHs|SV* sv
168 Push an SV onto the stack. The stack must have room for this element.
169 Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
170 C<XPUSHs> and C<XPUSHmortal>.
172 =for apidoc Am|void|PUSHp|char* str|STRLEN len
173 Push a string onto the stack. The stack must have room for this element.
174 The C<len> indicates the length of the string. Handles 'set' magic. Uses
175 C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
176 call multiple C<TARG>-oriented macros to return lists from XSUB's - see
177 C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
179 =for apidoc Am|void|PUSHn|NV nv
180 Push a double onto the stack. The stack must have room for this element.
181 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
182 called to declare it. Do not call multiple C<TARG>-oriented macros to
183 return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
186 =for apidoc Am|void|PUSHi|IV iv
187 Push an integer onto the stack. The stack must have room for this element.
188 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
189 called to declare it. Do not call multiple C<TARG>-oriented macros to
190 return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
193 =for apidoc Am|void|PUSHu|UV uv
194 Push an unsigned integer onto the stack. The stack must have room for this
195 element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
196 should be called to declare it. Do not call multiple C<TARG>-oriented
197 macros to return lists from XSUB's - see C<mPUSHu> instead. See also
198 C<XPUSHu> and C<mXPUSHu>.
200 =for apidoc Am|void|XPUSHs|SV* sv
201 Push an SV onto the stack, extending the stack if necessary. Does not
202 handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
203 C<PUSHs> and C<PUSHmortal>.
205 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
206 Push a string onto the stack, extending the stack if necessary. The C<len>
207 indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
208 C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
209 multiple C<TARG>-oriented macros to return lists from XSUB's - see
210 C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
212 =for apidoc Am|void|XPUSHn|NV nv
213 Push a double onto the stack, extending the stack if necessary. Handles
214 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
215 declare it. Do not call multiple C<TARG>-oriented macros to return lists
216 from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
218 =for apidoc Am|void|XPUSHi|IV iv
219 Push an integer onto the stack, extending the stack if necessary. Handles
220 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
221 declare it. Do not call multiple C<TARG>-oriented macros to return lists
222 from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
224 =for apidoc Am|void|XPUSHu|UV uv
225 Push an unsigned integer onto the stack, extending the stack if necessary.
226 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
227 called to declare it. Do not call multiple C<TARG>-oriented macros to
228 return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
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 handle 'set' magic. Does not use C<TARG>. See also
234 C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
236 =for apidoc Am|void|mPUSHp|char* str|STRLEN len
237 Push a string onto the stack. The stack must have room for this element.
238 The C<len> indicates the length of the string. Handles 'set' magic. Does
239 not use C<TARG>. See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
241 =for apidoc Am|void|mPUSHn|NV nv
242 Push a double onto the stack. The stack must have room for this element.
243 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn>
246 =for apidoc Am|void|mPUSHi|IV iv
247 Push an integer onto the stack. The stack must have room for this element.
248 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi>
251 =for apidoc Am|void|mPUSHu|UV uv
252 Push an unsigned integer onto the stack. The stack must have room for this
253 element. Handles 'set' magic. Does not use C<TARG>. See also C<PUSHu>,
254 C<mXPUSHu> and C<XPUSHu>.
256 =for apidoc Am|void|XPUSHmortal
257 Push a new mortal SV onto the stack, extending the stack if necessary. Does
258 not handle 'set' magic. Does not use C<TARG>. See also C<XPUSHs>,
259 C<PUSHmortal> and C<PUSHs>.
261 =for apidoc Am|void|mXPUSHp|char* str|STRLEN len
262 Push a string onto the stack, extending the stack if necessary. The C<len>
263 indicates the length of the string. Handles 'set' magic. Does not use
264 C<TARG>. See also C<XPUSHp>, C<mPUSHp> and C<PUSHp>.
266 =for apidoc Am|void|mXPUSHn|NV nv
267 Push a double onto the stack, extending the stack if necessary. Handles
268 'set' magic. Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and
271 =for apidoc Am|void|mXPUSHi|IV iv
272 Push an integer onto the stack, extending the stack if necessary. Handles
273 'set' magic. Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and
276 =for apidoc Am|void|mXPUSHu|UV uv
277 Push an unsigned integer onto the stack, extending the stack if necessary.
278 Handles 'set' magic. Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu>
284 #define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
285 sp = stack_grow(sp,p, (int) (n)); \
288 /* Same thing, but update mark register too. */
289 #define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
290 int markoff = mark - PL_stack_base; \
291 sp = stack_grow(sp,p,(int) (n)); \
292 mark = PL_stack_base + markoff; \
295 #define PUSHs(s) (*++sp = (s))
296 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
297 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
298 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
299 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
300 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
302 #define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
303 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
304 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
305 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
306 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
307 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
308 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
310 #define PUSHmortal PUSHs(sv_newmortal())
311 #define mPUSHp(p,l) sv_setpvn_mg(PUSHmortal, (p), (l))
312 #define mPUSHn(n) sv_setnv_mg(PUSHmortal, (NV)(n))
313 #define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
314 #define mPUSHu(u) sv_setuv_mg(PUSHmortal, (UV)(u))
316 #define XPUSHmortal XPUSHs(sv_newmortal())
317 #define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); sv_setpvn_mg(PUSHmortal, (p), (l)); } STMT_END
318 #define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv_mg(PUSHmortal, (NV)(n)); } STMT_END
319 #define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv_mg(PUSHmortal, (IV)(i)); } STMT_END
320 #define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv_mg(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 dTOPiv IV value = TOPi
334 #define dPOPiv IV value = POPi
335 #define dTOPuv UV value = TOPu
336 #define dPOPuv UV value = POPu
338 #define dTOPqv Quad_t value = TOPu
339 #define dPOPqv Quad_t value = POPu
340 #define dTOPuqv Uquad_t value = TOPuq
341 #define dPOPuqv Uquad_t value = POPuq
344 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
345 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
346 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
348 #define USE_LEFT(sv) \
349 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
350 #define dPOPXnnrl_ul(X) \
352 SV *leftsv = CAT2(X,s); \
353 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
354 #define dPOPXiirl_ul(X) \
356 SV *leftsv = CAT2(X,s); \
357 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
359 #define dPOPPOPssrl dPOPXssrl(POP)
360 #define dPOPPOPnnrl dPOPXnnrl(POP)
361 #define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
362 #define dPOPPOPiirl dPOPXiirl(POP)
363 #define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
365 #define dPOPTOPssrl dPOPXssrl(TOP)
366 #define dPOPTOPnnrl dPOPXnnrl(TOP)
367 #define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
368 #define dPOPTOPiirl dPOPXiirl(TOP)
369 #define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
371 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
372 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
373 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
375 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
376 #define RETSETNO RETURNX(SETs(&PL_sv_no))
377 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
379 #define ARGTARG PL_op->op_targ
381 /* See OPpTARGET_MY: */
382 #define MAXARG (PL_op->op_private & 15)
384 #define SWITCHSTACK(f,t) \
386 AvFILLp(f) = sp - PL_stack_base; \
387 PL_stack_base = AvARRAY(t); \
388 PL_stack_max = PL_stack_base + AvMAX(t); \
389 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
393 #define EXTEND_MORTAL(n) \
395 if (PL_tmps_ix + (n) >= PL_tmps_max) \
399 #define AMGf_noright 1
400 #define AMGf_noleft 2
401 #define AMGf_assign 4
404 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
405 if (PL_amagic_generation) { \
407 SV* right= *(sp); SV* left= *(sp-1);\
408 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
409 (tmpsv=amagic_call(left, \
412 (assign)? AMGf_assign: 0))) {\
414 (void)POPs; set(tmpsv); RETURN; } \
418 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
419 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
421 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
422 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
423 #define AMG_CALLbinL(left,right,meth) \
424 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
426 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
427 if (PL_amagic_generation) { \
429 SV* arg= sp[shift]; \
430 if(0) goto am_again; /* shut up unused warning */ \
432 if ((SvAMAGIC(arg))&&\
433 (tmpsv=AMG_CALLun(arg,meth))) {\
434 SPAGAIN; if (shift) sp += shift; \
439 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
441 #define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
442 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
443 #define tryAMAGICunTARGET(meth, shift) \
444 STMT_START { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
446 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}} STMT_END
448 #define setAGAIN(ref) \
452 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
453 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
459 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
461 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
462 #define SETsv(sv) STMT_START { \
463 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
464 { sv_setsv(TARG, (sv)); SETTARG; } \
465 else SETs(sv); } STMT_END
467 #define SETsvUN(sv) STMT_START { \
468 if (SvFLAGS(TARG) & SVs_PADMY) \
469 { sv_setsv(TARG, (sv)); SETTARG; } \
470 else SETs(sv); } STMT_END
472 /* newSVsv does not behave as advertised, so we copy missing
473 * information by hand */
475 /* SV* ref causes confusion with the member variable
476 changed SV* ref to SV* tmpRef */
477 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
478 if (SvREFCNT(tmpRef)>1) { \
479 SvRV(rv)=AMG_CALLun(rv,copy); \
480 SvREFCNT_dec(tmpRef); \
484 =for apidoc mU||LVRET
485 True if this op will be the return value of an lvalue subroutine
488 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())