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 #ifdef USE_5005THREADS
13 #define dARGS struct perl_thread *thr;
17 #endif /* USE_5005THREADS */
19 #define PP(s) OP * Perl_##s(pTHX)
22 =head1 Stack Manipulation Macros
25 Stack pointer. This is usually handled by C<xsubpp>. See C<dSP> and
29 Stack marker variable for the XSUB. See C<dMARK>.
31 =for apidoc Am|void|PUSHMARK|SP
32 Opening bracket for arguments on a callback. See C<PUTBACK> and
36 Declares a local copy of perl's stack pointer for the XSUB, available via
37 the C<SP> macro. See C<SP>.
41 Declare Just C<SP>. This is actually identical to C<dSP>, and declares
42 a local copy of perl's stack pointer, available via the C<SP> macro.
43 See C<SP>. (Available for backward source code compatibility with the
44 old (Perl 5.005) thread model.)
46 =for apidoc Ams||dMARK
47 Declare a stack marker variable, C<mark>, for the XSUB. See C<MARK> and
50 =for apidoc Ams||dORIGMARK
51 Saves the original stack mark for the XSUB. See C<ORIGMARK>.
53 =for apidoc AmU||ORIGMARK
54 The original stack mark for the XSUB. See C<dORIGMARK>.
56 =for apidoc Ams||SPAGAIN
57 Refetch the stack pointer. Used after a callback. See L<perlcall>.
61 #undef SP /* Solaris 2.7 i386 has this in /usr/include/sys/reg.h */
68 if (++PL_markstack_ptr == PL_markstack_max) \
70 *PL_markstack_ptr = (p) - PL_stack_base; \
73 #define TOPMARK (*PL_markstack_ptr)
74 #define POPMARK (*PL_markstack_ptr--)
76 #define dSP register SV **sp = PL_stack_sp
78 #define dMARK register SV **mark = PL_stack_base + POPMARK
79 #define dORIGMARK I32 origmark = mark - PL_stack_base
80 #define SETORIGMARK origmark = mark - PL_stack_base
81 #define ORIGMARK (PL_stack_base + origmark)
83 #define SPAGAIN sp = PL_stack_sp
84 #define MSPAGAIN STMT_START { sp = PL_stack_sp; mark = ORIGMARK; } STMT_END
86 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
87 #define dTARGETSTACKED SV * GETTARGETSTACKED
89 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
90 #define dTARGET SV * GETTARGET
92 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
93 #define dATARGET SV * GETATARGET
95 #define dTARG SV *targ
97 #define NORMAL PL_op->op_next
98 #define DIE return Perl_die
99 #define DIE_NULL return DieNull
102 =for apidoc Ams||PUTBACK
103 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
104 See C<PUSHMARK> and L<perlcall> for other uses.
106 =for apidoc Amn|SV*|POPs
107 Pops an SV off the stack.
109 =for apidoc Amn|char*|POPp
110 Pops a string off the stack. Deprecated. New code should provide
111 a STRLEN n_a and use POPpx.
113 =for apidoc Amn|char*|POPpx
114 Pops a string off the stack.
115 Requires a variable STRLEN n_a in scope.
117 =for apidoc Amn|char*|POPpbytex
118 Pops a string off the stack which must consist of bytes i.e. characters < 256.
119 Requires a variable STRLEN n_a in scope.
121 =for apidoc Amn|NV|POPn
122 Pops a double off the stack.
124 =for apidoc Amn|IV|POPi
125 Pops an integer off the stack.
127 =for apidoc Amn|long|POPl
128 Pops a long off the stack.
133 #define PUTBACK PL_stack_sp = sp
134 #define RETURN return PUTBACK, NORMAL
135 #define RETURNOP(o) return PUTBACK, o
136 #define RETURNX(x) return x, PUTBACK, NORMAL
139 #define POPp (SvPVx(POPs, PL_na)) /* deprecated */
140 #define POPpx (SvPVx(POPs, n_a))
141 #define POPpbytex (SvPVbytex(POPs, n_a))
142 #define POPn (SvNVx(POPs))
143 #define POPi ((IV)SvIVx(POPs))
144 #define POPu ((UV)SvUVx(POPs))
145 #define POPl ((long)SvIVx(POPs))
146 #define POPul ((unsigned long)SvIVx(POPs))
148 #define POPq ((Quad_t)SvIVx(POPs))
149 #define POPuq ((Uquad_t)SvUVx(POPs))
153 #define TOPm1s (*(sp-1))
154 #define TOPp1s (*(sp+1))
155 #define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
156 #define TOPpx (SvPV(TOPs, n_a))
157 #define TOPn (SvNV(TOPs))
158 #define TOPi ((IV)SvIV(TOPs))
159 #define TOPu ((UV)SvUV(TOPs))
160 #define TOPl ((long)SvIV(TOPs))
161 #define TOPul ((unsigned long)SvUV(TOPs))
163 #define TOPq ((Quad_t)SvIV(TOPs))
164 #define TOPuq ((Uquad_t)SvUV(TOPs))
167 /* Go to some pains in the rare event that we must extend the stack. */
170 =for apidoc Am|void|EXTEND|SP|int nitems
171 Used to extend the argument stack for an XSUB's return values. Once
172 used, guarantees that there is room for at least C<nitems> to be pushed
175 =for apidoc Am|void|PUSHs|SV* sv
176 Push an SV onto the stack. The stack must have room for this element.
177 Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
178 C<XPUSHs> and C<XPUSHmortal>.
180 =for apidoc Am|void|PUSHp|char* str|STRLEN len
181 Push a string onto the stack. The stack must have room for this element.
182 The C<len> indicates the length of the string. Handles 'set' magic. Uses
183 C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
184 call multiple C<TARG>-oriented macros to return lists from XSUB's - see
185 C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
187 =for apidoc Am|void|PUSHn|NV nv
188 Push a double onto the stack. The stack must have room for this element.
189 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
190 called to declare it. Do not call multiple C<TARG>-oriented macros to
191 return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
194 =for apidoc Am|void|PUSHi|IV iv
195 Push an integer onto the stack. The stack must have room for this element.
196 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
197 called to declare it. Do not call multiple C<TARG>-oriented macros to
198 return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
201 =for apidoc Am|void|PUSHu|UV uv
202 Push an unsigned integer onto the stack. The stack must have room for this
203 element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
204 should be called to declare it. Do not call multiple C<TARG>-oriented
205 macros to return lists from XSUB's - see C<mPUSHu> instead. See also
206 C<XPUSHu> and C<mXPUSHu>.
208 =for apidoc Am|void|XPUSHs|SV* sv
209 Push an SV onto the stack, extending the stack if necessary. Does not
210 handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
211 C<PUSHs> and C<PUSHmortal>.
213 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
214 Push a string onto the stack, extending the stack if necessary. The C<len>
215 indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
216 C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
217 multiple C<TARG>-oriented macros to return lists from XSUB's - see
218 C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
220 =for apidoc Am|void|XPUSHn|NV nv
221 Push a double onto the stack, extending the stack if necessary. Handles
222 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
223 declare it. Do not call multiple C<TARG>-oriented macros to return lists
224 from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
226 =for apidoc Am|void|XPUSHi|IV iv
227 Push an integer onto the stack, extending the stack if necessary. Handles
228 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
229 declare it. Do not call multiple C<TARG>-oriented macros to return lists
230 from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
232 =for apidoc Am|void|XPUSHu|UV uv
233 Push an unsigned integer onto the stack, extending the stack if necessary.
234 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
235 called to declare it. Do not call multiple C<TARG>-oriented macros to
236 return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
239 =for apidoc Am|void|PUSHmortal
240 Push a new mortal SV onto the stack. The stack must have room for this
241 element. Does not handle 'set' magic. Does not use C<TARG>. See also
242 C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
244 =for apidoc Am|void|mPUSHp|char* str|STRLEN len
245 Push a string onto the stack. The stack must have room for this element.
246 The C<len> indicates the length of the string. Handles 'set' magic. Does
247 not use C<TARG>. See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
249 =for apidoc Am|void|mPUSHn|NV nv
250 Push a double onto the stack. The stack must have room for this element.
251 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn>
254 =for apidoc Am|void|mPUSHi|IV iv
255 Push an integer onto the stack. The stack must have room for this element.
256 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi>
259 =for apidoc Am|void|mPUSHu|UV uv
260 Push an unsigned integer onto the stack. The stack must have room for this
261 element. Handles 'set' magic. Does not use C<TARG>. See also C<PUSHu>,
262 C<mXPUSHu> and C<XPUSHu>.
264 =for apidoc Am|void|XPUSHmortal
265 Push a new mortal SV onto the stack, extending the stack if necessary. Does
266 not handle 'set' magic. Does not use C<TARG>. See also C<XPUSHs>,
267 C<PUSHmortal> and C<PUSHs>.
269 =for apidoc Am|void|mXPUSHp|char* str|STRLEN len
270 Push a string onto the stack, extending the stack if necessary. The C<len>
271 indicates the length of the string. Handles 'set' magic. Does not use
272 C<TARG>. See also C<XPUSHp>, C<mPUSHp> and C<PUSHp>.
274 =for apidoc Am|void|mXPUSHn|NV nv
275 Push a double onto the stack, extending the stack if necessary. Handles
276 'set' magic. Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and
279 =for apidoc Am|void|mXPUSHi|IV iv
280 Push an integer onto the stack, extending the stack if necessary. Handles
281 'set' magic. Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and
284 =for apidoc Am|void|mXPUSHu|UV uv
285 Push an unsigned integer onto the stack, extending the stack if necessary.
286 Handles 'set' magic. Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu>
292 #define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
293 sp = stack_grow(sp,p, (int) (n)); \
296 /* Same thing, but update mark register too. */
297 #define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
298 int markoff = mark - PL_stack_base; \
299 sp = stack_grow(sp,p,(int) (n)); \
300 mark = PL_stack_base + markoff; \
303 #define PUSHs(s) (*++sp = (s))
304 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
305 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
306 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
307 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
308 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
310 #define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
311 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
312 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
313 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
314 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
315 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
316 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
318 #define PUSHmortal PUSHs(sv_newmortal())
319 #define mPUSHp(p,l) sv_setpvn_mg(PUSHmortal, (p), (l))
320 #define mPUSHn(n) sv_setnv_mg(PUSHmortal, (NV)(n))
321 #define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
322 #define mPUSHu(u) sv_setuv_mg(PUSHmortal, (UV)(u))
324 #define XPUSHmortal XPUSHs(sv_newmortal())
325 #define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); sv_setpvn_mg(PUSHmortal, (p), (l)); } STMT_END
326 #define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv_mg(PUSHmortal, (NV)(n)); } STMT_END
327 #define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv_mg(PUSHmortal, (IV)(i)); } STMT_END
328 #define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv_mg(PUSHmortal, (UV)(u)); } STMT_END
330 #define SETs(s) (*sp = s)
331 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
332 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
333 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
334 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
335 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
337 #define dTOPss SV *sv = TOPs
338 #define dPOPss SV *sv = POPs
339 #define dTOPnv NV value = TOPn
340 #define dPOPnv NV value = POPn
341 #define dTOPiv IV value = TOPi
342 #define dPOPiv IV value = POPi
343 #define dTOPuv UV value = TOPu
344 #define dPOPuv UV value = POPu
346 #define dTOPqv Quad_t value = TOPu
347 #define dPOPqv Quad_t value = POPu
348 #define dTOPuqv Uquad_t value = TOPuq
349 #define dPOPuqv Uquad_t value = POPuq
352 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
353 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
354 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
356 #define USE_LEFT(sv) \
357 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
358 #define dPOPXnnrl_ul(X) \
360 SV *leftsv = CAT2(X,s); \
361 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
362 #define dPOPXiirl_ul(X) \
364 SV *leftsv = CAT2(X,s); \
365 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
367 #define dPOPPOPssrl dPOPXssrl(POP)
368 #define dPOPPOPnnrl dPOPXnnrl(POP)
369 #define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
370 #define dPOPPOPiirl dPOPXiirl(POP)
371 #define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
373 #define dPOPTOPssrl dPOPXssrl(TOP)
374 #define dPOPTOPnnrl dPOPXnnrl(TOP)
375 #define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
376 #define dPOPTOPiirl dPOPXiirl(TOP)
377 #define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
379 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
380 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
381 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
383 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
384 #define RETSETNO RETURNX(SETs(&PL_sv_no))
385 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
387 #define ARGTARG PL_op->op_targ
389 /* See OPpTARGET_MY: */
390 #define MAXARG (PL_op->op_private & 15)
392 #define SWITCHSTACK(f,t) \
394 AvFILLp(f) = sp - PL_stack_base; \
395 PL_stack_base = AvARRAY(t); \
396 PL_stack_max = PL_stack_base + AvMAX(t); \
397 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
401 #define EXTEND_MORTAL(n) \
403 if (PL_tmps_ix + (n) >= PL_tmps_max) \
407 #define AMGf_noright 1
408 #define AMGf_noleft 2
409 #define AMGf_assign 4
412 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
413 if (PL_amagic_generation) { \
415 SV* right= *(sp); SV* left= *(sp-1);\
416 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
417 (tmpsv=amagic_call(left, \
420 (assign)? AMGf_assign: 0))) {\
422 (void)POPs; set(tmpsv); RETURN; } \
426 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
427 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
429 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
430 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
431 #define AMG_CALLbinL(left,right,meth) \
432 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
434 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
435 if (PL_amagic_generation) { \
437 SV* arg= sp[shift]; \
438 if(0) goto am_again; /* shut up unused warning */ \
440 if ((SvAMAGIC(arg))&&\
441 (tmpsv=AMG_CALLun(arg,meth))) {\
442 SPAGAIN; if (shift) sp += shift; \
447 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
449 #define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
450 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
451 #define tryAMAGICunTARGET(meth, shift) \
452 STMT_START { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
454 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}} STMT_END
456 #define setAGAIN(ref) \
460 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
461 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
467 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
469 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
470 #define SETsv(sv) STMT_START { \
471 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
472 { sv_setsv(TARG, (sv)); SETTARG; } \
473 else SETs(sv); } STMT_END
475 #define SETsvUN(sv) STMT_START { \
476 if (SvFLAGS(TARG) & SVs_PADMY) \
477 { sv_setsv(TARG, (sv)); SETTARG; } \
478 else SETs(sv); } STMT_END
480 /* newSVsv does not behave as advertised, so we copy missing
481 * information by hand */
483 /* SV* ref causes confusion with the member variable
484 changed SV* ref to SV* tmpRef */
485 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
486 if (SvREFCNT(tmpRef)>1) { \
487 SvRV_set(rv, AMG_CALLun(rv,copy)); \
488 SvREFCNT_dec(tmpRef); \
492 =for apidoc mU||LVRET
493 True if this op will be the return value of an lvalue subroutine
496 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())