3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 * 2000, 2001, 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 Ams||PUSHMARK
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 */
58 #define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
60 *PL_markstack_ptr = (p) - PL_stack_base
62 #define TOPMARK (*PL_markstack_ptr)
63 #define POPMARK (*PL_markstack_ptr--)
65 #define dSP register SV **sp = PL_stack_sp
67 #define dMARK register SV **mark = PL_stack_base + POPMARK
68 #define dORIGMARK I32 origmark = mark - PL_stack_base
69 #define SETORIGMARK origmark = mark - PL_stack_base
70 #define ORIGMARK (PL_stack_base + origmark)
72 #define SPAGAIN sp = PL_stack_sp
73 #define MSPAGAIN sp = PL_stack_sp; mark = ORIGMARK
75 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
76 #define dTARGETSTACKED SV * GETTARGETSTACKED
78 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
79 #define dTARGET SV * GETTARGET
81 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
82 #define dATARGET SV * GETATARGET
84 #define dTARG SV *targ
86 #define NORMAL PL_op->op_next
87 #define DIE return Perl_die
90 =for apidoc Ams||PUTBACK
91 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
92 See C<PUSHMARK> and L<perlcall> for other uses.
94 =for apidoc Amn|SV*|POPs
95 Pops an SV off the stack.
97 =for apidoc Amn|char*|POPp
98 Pops a string off the stack. Deprecated. New code should provide
99 a STRLEN n_a and use POPpx.
101 =for apidoc Amn|char*|POPpx
102 Pops a string off the stack.
103 Requires a variable STRLEN n_a in scope.
105 =for apidoc Amn|char*|POPpbytex
106 Pops a string off the stack which must consist of bytes i.e. characters < 256.
107 Requires a variable STRLEN n_a in scope.
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(POPs, n_a))
129 #define POPpbytex (SvPVbytex(POPs, n_a))
130 #define POPn (SvNVx(POPs))
131 #define POPi ((IV)SvIVx(POPs))
132 #define POPu ((UV)SvUVx(POPs))
133 #define POPl ((long)SvIVx(POPs))
134 #define POPul ((unsigned long)SvIVx(POPs))
136 #define POPq ((Quad_t)SvIVx(POPs))
137 #define POPuq ((Uquad_t)SvUVx(POPs))
141 #define TOPm1s (*(sp-1))
142 #define TOPp1s (*(sp+1))
143 #define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
144 #define TOPpx (SvPV(TOPs, n_a))
145 #define TOPn (SvNV(TOPs))
146 #define TOPi ((IV)SvIV(TOPs))
147 #define TOPu ((UV)SvUV(TOPs))
148 #define TOPl ((long)SvIV(TOPs))
149 #define TOPul ((unsigned long)SvUV(TOPs))
151 #define TOPq ((Quad_t)SvIV(TOPs))
152 #define TOPuq ((Uquad_t)SvUV(TOPs))
155 /* Go to some pains in the rare event that we must extend the stack. */
158 =for apidoc Am|void|EXTEND|SP|int 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. See C<XPUSHs>.
167 =for apidoc Am|void|PUSHp|char* str|STRLEN len
168 Push a string onto the stack. The stack must have room for this element.
169 The C<len> indicates the length of the string. Handles 'set' magic. See
172 =for apidoc Am|void|PUSHn|NV nv
173 Push a double onto the stack. The stack must have room for this element.
174 Handles 'set' magic. See C<XPUSHn>.
176 =for apidoc Am|void|PUSHi|IV iv
177 Push an integer onto the stack. The stack must have room for this element.
178 Handles 'set' magic. See C<XPUSHi>.
180 =for apidoc Am|void|PUSHu|UV uv
181 Push an unsigned integer onto the stack. The stack must have room for this
182 element. See C<XPUSHu>.
184 =for apidoc Am|void|XPUSHs|SV* sv
185 Push an SV onto the stack, extending the stack if necessary. Does not
186 handle 'set' magic. See C<PUSHs>.
188 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
189 Push a string onto the stack, extending the stack if necessary. The C<len>
190 indicates the length of the string. Handles 'set' magic. See
193 =for apidoc Am|void|XPUSHn|NV nv
194 Push a double onto the stack, extending the stack if necessary. Handles
195 'set' magic. See C<PUSHn>.
197 =for apidoc Am|void|XPUSHi|IV iv
198 Push an integer onto the stack, extending the stack if necessary. Handles
199 'set' magic. See C<PUSHi>.
201 =for apidoc Am|void|XPUSHu|UV uv
202 Push an unsigned integer onto the stack, extending the stack if necessary.
208 #define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
209 sp = stack_grow(sp,p, (int) (n)); \
212 /* Same thing, but update mark register too. */
213 #define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
214 int markoff = mark - PL_stack_base; \
215 sp = stack_grow(sp,p,(int) (n)); \
216 mark = PL_stack_base + markoff; \
219 #define PUSHs(s) (*++sp = (s))
220 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
221 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
222 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
223 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
224 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
226 #define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
227 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
228 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
229 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
230 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
231 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
232 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
234 #define SETs(s) (*sp = s)
235 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
236 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
237 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
238 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
239 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
241 #define dTOPss SV *sv = TOPs
242 #define dPOPss SV *sv = POPs
243 #define dTOPnv NV value = TOPn
244 #define dPOPnv NV value = POPn
245 #define dTOPiv IV value = TOPi
246 #define dPOPiv IV value = POPi
247 #define dTOPuv UV value = TOPu
248 #define dPOPuv UV value = POPu
250 #define dTOPqv Quad_t value = TOPu
251 #define dPOPqv Quad_t value = POPu
252 #define dTOPuqv Uquad_t value = TOPuq
253 #define dPOPuqv Uquad_t value = POPuq
256 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
257 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
258 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
260 #define USE_LEFT(sv) \
261 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
262 #define dPOPXnnrl_ul(X) \
264 SV *leftsv = CAT2(X,s); \
265 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
266 #define dPOPXiirl_ul(X) \
268 SV *leftsv = CAT2(X,s); \
269 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
271 #define dPOPPOPssrl dPOPXssrl(POP)
272 #define dPOPPOPnnrl dPOPXnnrl(POP)
273 #define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
274 #define dPOPPOPiirl dPOPXiirl(POP)
275 #define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
277 #define dPOPTOPssrl dPOPXssrl(TOP)
278 #define dPOPTOPnnrl dPOPXnnrl(TOP)
279 #define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
280 #define dPOPTOPiirl dPOPXiirl(TOP)
281 #define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
283 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
284 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
285 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
287 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
288 #define RETSETNO RETURNX(SETs(&PL_sv_no))
289 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
291 #define ARGTARG PL_op->op_targ
293 /* See OPpTARGET_MY: */
294 #define MAXARG (PL_op->op_private & 15)
296 #define SWITCHSTACK(f,t) \
298 AvFILLp(f) = sp - PL_stack_base; \
299 PL_stack_base = AvARRAY(t); \
300 PL_stack_max = PL_stack_base + AvMAX(t); \
301 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
305 #define EXTEND_MORTAL(n) \
307 if (PL_tmps_ix + (n) >= PL_tmps_max) \
311 #define AMGf_noright 1
312 #define AMGf_noleft 2
313 #define AMGf_assign 4
316 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
317 if (PL_amagic_generation) { \
319 SV* right= *(sp); SV* left= *(sp-1);\
320 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
321 (tmpsv=amagic_call(left, \
324 (assign)? AMGf_assign: 0))) {\
326 (void)POPs; set(tmpsv); RETURN; } \
330 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
331 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
333 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
334 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
335 #define AMG_CALLbinL(left,right,meth) \
336 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
338 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
339 if (PL_amagic_generation) { \
341 SV* arg= sp[shift]; \
342 if(0) goto am_again; /* shut up unused warning */ \
344 if ((SvAMAGIC(arg))&&\
345 (tmpsv=AMG_CALLun(arg,meth))) {\
346 SPAGAIN; if (shift) sp += shift; \
351 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
353 #define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
354 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
355 #define tryAMAGICunTARGET(meth, shift) \
356 { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
358 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
360 #define setAGAIN(ref) sv = ref; \
362 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
363 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
368 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
370 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
371 #define SETsv(sv) STMT_START { \
372 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
373 { sv_setsv(TARG, (sv)); SETTARG; } \
374 else SETs(sv); } STMT_END
376 #define SETsvUN(sv) STMT_START { \
377 if (SvFLAGS(TARG) & SVs_PADMY) \
378 { sv_setsv(TARG, (sv)); SETTARG; } \
379 else SETs(sv); } STMT_END
381 /* newSVsv does not behave as advertised, so we copy missing
382 * information by hand */
384 /* SV* ref causes confusion with the member variable
385 changed SV* ref to SV* tmpRef */
386 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
387 if (SvREFCNT(tmpRef)>1) { \
388 SvRV(rv)=AMG_CALLun(rv,copy); \
389 SvREFCNT_dec(tmpRef); \
393 =for apidoc mU||LVRET
394 True if this op will be the return value of an lvalue subroutine
397 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())