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 SV **sp = PL_stack_sp
78 #define dMARK register SV **mark = PL_stack_base + POPMARK
79 #define dORIGMARK const I32 origmark = mark - PL_stack_base
80 #define ORIGMARK (PL_stack_base + origmark)
82 #define SPAGAIN sp = PL_stack_sp
83 #define MSPAGAIN STMT_START { sp = PL_stack_sp; mark = ORIGMARK; } STMT_END
85 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
86 #define dTARGETSTACKED SV * GETTARGETSTACKED
88 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
89 #define dTARGET SV * GETTARGET
91 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
92 #define dATARGET SV * GETATARGET
94 #define dTARG SV *targ
96 #define NORMAL PL_op->op_next
97 #define DIE return Perl_die
99 # define DIE_NULL return DieNull
103 =for apidoc Ams||PUTBACK
104 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
105 See C<PUSHMARK> and L<perlcall> for other uses.
107 =for apidoc Amn|SV*|POPs
108 Pops an SV off the stack.
110 =for apidoc Amn|char*|POPp
111 Pops a string off the stack. Deprecated. New code should use POPpx.
113 =for apidoc Amn|char*|POPpx
114 Pops a string off the stack.
116 =for apidoc Amn|char*|POPpbytex
117 Pops a string off the stack which must consist of bytes i.e. characters < 256.
119 =for apidoc Amn|NV|POPn
120 Pops a double off the stack.
122 =for apidoc Amn|IV|POPi
123 Pops an integer off the stack.
125 =for apidoc Amn|long|POPl
126 Pops a long off the stack.
131 #define PUTBACK PL_stack_sp = sp
132 #define RETURN return (PUTBACK, NORMAL)
133 #define RETURNOP(o) return (PUTBACK, o)
134 #define RETURNX(x) return (x, PUTBACK, NORMAL)
137 #define POPp (SvPVx(POPs, PL_na)) /* deprecated */
138 #define POPpx (SvPVx_nolen(POPs))
139 #define POPpconstx (SvPVx_nolen_const(POPs))
140 #define POPpbytex (SvPVbytex_nolen(POPs))
141 #define POPn (SvNVx(POPs))
142 #define POPi ((IV)SvIVx(POPs))
143 #define POPu ((UV)SvUVx(POPs))
144 #define POPl ((long)SvIVx(POPs))
145 #define POPul ((unsigned long)SvIVx(POPs))
147 #define POPq ((Quad_t)SvIVx(POPs))
148 #define POPuq ((Uquad_t)SvUVx(POPs))
152 #define TOPm1s (*(sp-1))
153 #define TOPp1s (*(sp+1))
154 #define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
155 #define TOPpx (SvPV_nolen(TOPs))
156 #define TOPn (SvNV(TOPs))
157 #define TOPi ((IV)SvIV(TOPs))
158 #define TOPu ((UV)SvUV(TOPs))
159 #define TOPl ((long)SvIV(TOPs))
160 #define TOPul ((unsigned long)SvUV(TOPs))
162 #define TOPq ((Quad_t)SvIV(TOPs))
163 #define TOPuq ((Uquad_t)SvUV(TOPs))
166 /* Go to some pains in the rare event that we must extend the stack. */
169 =for apidoc Am|void|EXTEND|SP|int nitems
170 Used to extend the argument stack for an XSUB's return values. Once
171 used, guarantees that there is room for at least C<nitems> to be pushed
174 =for apidoc Am|void|PUSHs|SV* sv
175 Push an SV onto the stack. The stack must have room for this element.
176 Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
177 C<XPUSHs> and C<XPUSHmortal>.
179 =for apidoc Am|void|PUSHp|char* str|STRLEN len
180 Push a string onto the stack. The stack must have room for this element.
181 The C<len> indicates the length of the string. Handles 'set' magic. Uses
182 C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
183 call multiple C<TARG>-oriented macros to return lists from XSUB's - see
184 C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
186 =for apidoc Am|void|PUSHn|NV nv
187 Push a double 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<mPUSHn> instead. See also C<XPUSHn> and
193 =for apidoc Am|void|PUSHi|IV iv
194 Push an integer onto the stack. The stack must have room for this element.
195 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
196 called to declare it. Do not call multiple C<TARG>-oriented macros to
197 return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
200 =for apidoc Am|void|PUSHu|UV uv
201 Push an unsigned integer onto the stack. The stack must have room for this
202 element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
203 should be called to declare it. Do not call multiple C<TARG>-oriented
204 macros to return lists from XSUB's - see C<mPUSHu> instead. See also
205 C<XPUSHu> and C<mXPUSHu>.
207 =for apidoc Am|void|XPUSHs|SV* sv
208 Push an SV onto the stack, extending the stack if necessary. Does not
209 handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
210 C<PUSHs> and C<PUSHmortal>.
212 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
213 Push a string onto the stack, extending the stack if necessary. The C<len>
214 indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
215 C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
216 multiple C<TARG>-oriented macros to return lists from XSUB's - see
217 C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
219 =for apidoc Am|void|XPUSHn|NV nv
220 Push a double onto the stack, extending the stack if necessary. Handles
221 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
222 declare it. Do not call multiple C<TARG>-oriented macros to return lists
223 from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
225 =for apidoc Am|void|XPUSHi|IV iv
226 Push an integer onto the stack, extending the stack if necessary. Handles
227 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
228 declare it. Do not call multiple C<TARG>-oriented macros to return lists
229 from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
231 =for apidoc Am|void|XPUSHu|UV uv
232 Push an unsigned integer onto the stack, extending the stack if necessary.
233 Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
234 called to declare it. Do not call multiple C<TARG>-oriented macros to
235 return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
238 =for apidoc Am|void|PUSHmortal
239 Push a new mortal SV onto the stack. The stack must have room for this
240 element. Does not handle 'set' magic. Does not use C<TARG>. See also
241 C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
243 =for apidoc Am|void|mPUSHp|char* str|STRLEN len
244 Push a string onto the stack. The stack must have room for this element.
245 The C<len> indicates the length of the string. Handles 'set' magic. Does
246 not use C<TARG>. See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
248 =for apidoc Am|void|mPUSHn|NV nv
249 Push a double onto the stack. The stack must have room for this element.
250 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn>
253 =for apidoc Am|void|mPUSHi|IV iv
254 Push an integer onto the stack. The stack must have room for this element.
255 Handles 'set' magic. Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi>
258 =for apidoc Am|void|mPUSHu|UV uv
259 Push an unsigned integer onto the stack. The stack must have room for this
260 element. Handles 'set' magic. Does not use C<TARG>. See also C<PUSHu>,
261 C<mXPUSHu> and C<XPUSHu>.
263 =for apidoc Am|void|XPUSHmortal
264 Push a new mortal SV onto the stack, extending the stack if necessary. Does
265 not handle 'set' magic. Does not use C<TARG>. See also C<XPUSHs>,
266 C<PUSHmortal> and C<PUSHs>.
268 =for apidoc Am|void|mXPUSHp|char* str|STRLEN len
269 Push a string onto the stack, extending the stack if necessary. The C<len>
270 indicates the length of the string. Handles 'set' magic. Does not use
271 C<TARG>. See also C<XPUSHp>, C<mPUSHp> and C<PUSHp>.
273 =for apidoc Am|void|mXPUSHn|NV nv
274 Push a double onto the stack, extending the stack if necessary. Handles
275 'set' magic. Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and
278 =for apidoc Am|void|mXPUSHi|IV iv
279 Push an integer onto the stack, extending the stack if necessary. Handles
280 'set' magic. Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and
283 =for apidoc Am|void|mXPUSHu|UV uv
284 Push an unsigned integer onto the stack, extending the stack if necessary.
285 Handles 'set' magic. Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu>
291 #define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
292 sp = stack_grow(sp,p, (int) (n)); \
295 /* Same thing, but update mark register too. */
296 #define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
297 const int markoff = mark - PL_stack_base; \
298 sp = stack_grow(sp,p,(int) (n)); \
299 mark = PL_stack_base + markoff; \
302 #define PUSHs(s) (*++sp = (s))
303 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
304 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
305 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
306 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
307 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
309 #define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
310 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
311 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
312 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
313 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
314 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
315 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
317 #define PUSHmortal PUSHs(sv_newmortal())
318 #define mPUSHp(p,l) sv_setpvn_mg(PUSHmortal, (p), (l))
319 #define mPUSHn(n) sv_setnv_mg(PUSHmortal, (NV)(n))
320 #define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
321 #define mPUSHu(u) sv_setuv_mg(PUSHmortal, (UV)(u))
323 #define XPUSHmortal XPUSHs(sv_newmortal())
324 #define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); sv_setpvn_mg(PUSHmortal, (p), (l)); } STMT_END
325 #define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv_mg(PUSHmortal, (NV)(n)); } STMT_END
326 #define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv_mg(PUSHmortal, (IV)(i)); } STMT_END
327 #define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv_mg(PUSHmortal, (UV)(u)); } STMT_END
329 #define SETs(s) (*sp = s)
330 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
331 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
332 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
333 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
334 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
336 #define dTOPss SV *sv = TOPs
337 #define dPOPss SV *sv = POPs
338 #define dTOPnv NV value = TOPn
339 #define dPOPnv NV value = POPn
340 #define dTOPiv IV value = TOPi
341 #define dPOPiv IV value = POPi
342 #define dTOPuv UV value = TOPu
343 #define dPOPuv UV value = POPu
345 #define dTOPqv Quad_t value = TOPu
346 #define dPOPqv Quad_t value = POPu
347 #define dTOPuqv Uquad_t value = TOPuq
348 #define dPOPuqv Uquad_t value = POPuq
351 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
352 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
353 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
355 #define USE_LEFT(sv) \
356 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
357 #define dPOPXnnrl_ul(X) \
359 SV *leftsv = CAT2(X,s); \
360 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
361 #define dPOPXiirl_ul(X) \
363 SV *leftsv = CAT2(X,s); \
364 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
366 #define dPOPPOPssrl dPOPXssrl(POP)
367 #define dPOPPOPnnrl dPOPXnnrl(POP)
368 #define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
369 #define dPOPPOPiirl dPOPXiirl(POP)
370 #define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
372 #define dPOPTOPssrl dPOPXssrl(TOP)
373 #define dPOPTOPnnrl dPOPXnnrl(TOP)
374 #define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
375 #define dPOPTOPiirl dPOPXiirl(TOP)
376 #define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
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))
386 #define ARGTARG PL_op->op_targ
388 /* See OPpTARGET_MY: */
389 #define MAXARG (PL_op->op_private & 15)
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 if (PL_tmps_ix + (n) >= PL_tmps_max) \
406 #define AMGf_noright 1
407 #define AMGf_noleft 2
408 #define AMGf_assign 4
411 #define tryAMAGICbinW_var(meth_enum,assign,set) STMT_START { \
412 if (PL_amagic_generation) { \
413 SV* const left = *(sp-1); \
414 SV* const right = *(sp); \
415 if ((SvAMAGIC(left)||SvAMAGIC(right))) {\
416 SV * const tmpsv = amagic_call(left, \
419 (assign)? AMGf_assign: 0); \
422 (void)POPs; set(tmpsv); RETURN; } \
427 #define tryAMAGICbinW(meth,assign,set) \
428 tryAMAGICbinW_var(CAT2(meth,_amg),assign,set)
430 #define tryAMAGICbin_var(meth_enum,assign) \
431 tryAMAGICbinW_var(meth_enum,assign,SETsv)
432 #define tryAMAGICbin(meth,assign) \
433 tryAMAGICbin_var(CAT2(meth,_amg),assign)
435 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
437 #define tryAMAGICbinSET_var(meth_enum,assign) \
438 tryAMAGICbinW_var(meth_enum,assign,SETs)
440 #define AMG_CALLun_var(sv,meth_enum) amagic_call(sv,&PL_sv_undef, \
441 meth_enum,AMGf_noright | AMGf_unary)
442 #define AMG_CALLun(sv,meth) AMG_CALLun_var(sv,CAT2(meth,_amg))
444 #define AMG_CALLbinL(left,right,meth) \
445 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
447 #define tryAMAGICunW_var(meth_enum,set,shift,ret) STMT_START { \
448 if (PL_amagic_generation) { \
450 SV* arg= sp[shift]; \
451 if(0) goto am_again; /* shut up unused warning */ \
453 if ((SvAMAGIC(arg))&&\
454 (tmpsv=AMG_CALLun_var(arg,(meth_enum)))) {\
455 SPAGAIN; if (shift) sp += shift; \
459 #define tryAMAGICunW(meth,set,shift,ret) \
460 tryAMAGICunW_var(CAT2(meth,_amg),set,shift,ret)
462 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
464 #define tryAMAGICun_var(meth_enum) tryAMAGICunW_var(meth_enum,SETsvUN,0,RETURN)
465 #define tryAMAGICun(meth) tryAMAGICun_var(CAT2(meth,_amg))
466 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
467 #define tryAMAGICunTARGET(meth, shift) \
468 STMT_START { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
470 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}} STMT_END
472 #define setAGAIN(ref) \
476 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
477 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
483 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
484 #define tryAMAGICunDEREF_var(meth_enum) \
485 tryAMAGICunW_var(meth_enum,setAGAIN,0,(void)0)
487 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
488 #define SETsv(sv) STMT_START { \
489 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
490 { sv_setsv(TARG, (sv)); SETTARG; } \
491 else SETs(sv); } STMT_END
493 #define SETsvUN(sv) STMT_START { \
494 if (SvFLAGS(TARG) & SVs_PADMY) \
495 { sv_setsv(TARG, (sv)); SETTARG; } \
496 else SETs(sv); } STMT_END
498 /* newSVsv does not behave as advertised, so we copy missing
499 * information by hand */
501 /* SV* ref causes confusion with the member variable
502 changed SV* ref to SV* tmpRef */
503 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
504 if (SvREFCNT(tmpRef)>1) { \
505 SvRV_set(rv, AMG_CALLun(rv,copy)); \
506 SvREFCNT_dec(tmpRef); \
510 =for apidoc mU||LVRET
511 True if this op will be the return value of an lvalue subroutine
514 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())
518 * c-indentation-style: bsd
520 * indent-tabs-mode: t
523 * ex: set ts=8 sts=4 sw=4 noet: