3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
12 #define dARGS struct perl_thread *thr;
16 #endif /* USE_THREADS */
18 #define PP(s) OP * Perl_##s(pTHX)
22 Stack pointer. This is usually handled by C<xsubpp>. See C<dSP> and
26 Stack marker variable for the XSUB. See C<dMARK>.
28 =for apidoc Ams||PUSHMARK
29 Opening bracket for arguments on a callback. See C<PUTBACK> and
33 Declares a local copy of perl's stack pointer for the XSUB, available via
34 the C<SP> macro. See C<SP>.
38 Declare Just C<SP>. This is actually identical to C<dSP>, and declares
39 a local copy of perl's stack pointer, available via the C<SP> macro.
40 See C<SP>. (Available for backward source code compatibility with the
41 old (Perl 5.005) thread model.)
43 =for apidoc Ams||dMARK
44 Declare a stack marker variable, C<mark>, for the XSUB. See C<MARK> and
47 =for apidoc Ams||dORIGMARK
48 Saves the original stack mark for the XSUB. See C<ORIGMARK>.
50 =for apidoc AmU||ORIGMARK
51 The original stack mark for the XSUB. See C<dORIGMARK>.
53 =for apidoc Ams||SPAGAIN
54 Refetch the stack pointer. Used after a callback. See L<perlcall>.
58 #undef SP /* Solaris 2.7 i386 has this in /usr/include/sys/reg.h */
63 #define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
65 *PL_markstack_ptr = (p) - PL_stack_base
67 #define TOPMARK (*PL_markstack_ptr)
68 #define POPMARK (*PL_markstack_ptr--)
70 #define djSP register SV **sp = PL_stack_sp
72 #define dMARK register SV **mark = PL_stack_base + POPMARK
73 #define dORIGMARK I32 origmark = mark - PL_stack_base
74 #define SETORIGMARK origmark = mark - PL_stack_base
75 #define ORIGMARK (PL_stack_base + origmark)
77 #define SPAGAIN sp = PL_stack_sp
78 #define MSPAGAIN sp = PL_stack_sp; mark = ORIGMARK
80 #define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
81 #define dTARGETSTACKED SV * GETTARGETSTACKED
83 #define GETTARGET targ = PAD_SV(PL_op->op_targ)
84 #define dTARGET SV * GETTARGET
86 #define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
87 #define dATARGET SV * GETATARGET
89 #define dTARG SV *targ
91 #define NORMAL PL_op->op_next
92 #define DIE return Perl_die
95 =for apidoc Ams||PUTBACK
96 Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
97 See C<PUSHMARK> and L<perlcall> for other uses.
99 =for apidoc Amn|SV*|POPs
100 Pops an SV off the stack.
102 =for apidoc Amn|char*|POPp
103 Pops a string off the stack.
105 =for apidoc Amn|NV|POPn
106 Pops a double off the stack.
108 =for apidoc Amn|IV|POPi
109 Pops an integer off the stack.
111 =for apidoc Amn|long|POPl
112 Pops a long off the stack.
117 #define PUTBACK PL_stack_sp = sp
118 #define RETURN return PUTBACK, NORMAL
119 #define RETURNOP(o) return PUTBACK, o
120 #define RETURNX(x) return x, PUTBACK, NORMAL
123 #define POPp (SvPVx(POPs, PL_na)) /* deprecated */
124 #define POPpx (SvPVx(POPs, n_a))
125 #define POPn (SvNVx(POPs))
126 #define POPi ((IV)SvIVx(POPs))
127 #define POPu ((UV)SvUVx(POPs))
128 #define POPl ((long)SvIVx(POPs))
129 #define POPul ((unsigned long)SvIVx(POPs))
131 #define POPq ((Quad_t)SvIVx(POPs))
132 #define POPuq ((Uquad_t)SvUVx(POPs))
136 #define TOPm1s (*(sp-1))
137 #define TOPp1s (*(sp+1))
138 #define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
139 #define TOPpx (SvPV(TOPs, n_a))
140 #define TOPn (SvNV(TOPs))
141 #define TOPi ((IV)SvIV(TOPs))
142 #define TOPu ((UV)SvUV(TOPs))
143 #define TOPl ((long)SvIV(TOPs))
144 #define TOPul ((unsigned long)SvUV(TOPs))
146 #define TOPq ((Quad_t)SvIV(TOPs))
147 #define TOPuq ((Uquad_t)SvUV(TOPs))
150 /* Go to some pains in the rare event that we must extend the stack. */
153 =for apidoc Am|void|EXTEND|SP|int nitems
154 Used to extend the argument stack for an XSUB's return values. Once
155 used, guarantees that there is room for at least C<nitems> to be pushed
158 =for apidoc Am|void|PUSHs|SV* sv
159 Push an SV onto the stack. The stack must have room for this element.
160 Does not handle 'set' magic. See C<XPUSHs>.
162 =for apidoc Am|void|PUSHp|char* str|STRLEN len
163 Push a string onto the stack. The stack must have room for this element.
164 The C<len> indicates the length of the string. Handles 'set' magic. See
167 =for apidoc Am|void|PUSHn|NV nv
168 Push a double onto the stack. The stack must have room for this element.
169 Handles 'set' magic. See C<XPUSHn>.
171 =for apidoc Am|void|PUSHi|IV iv
172 Push an integer onto the stack. The stack must have room for this element.
173 Handles 'set' magic. See C<XPUSHi>.
175 =for apidoc Am|void|PUSHu|UV uv
176 Push an unsigned integer onto the stack. The stack must have room for this
177 element. See C<XPUSHu>.
179 =for apidoc Am|void|XPUSHs|SV* sv
180 Push an SV onto the stack, extending the stack if necessary. Does not
181 handle 'set' magic. See C<PUSHs>.
183 =for apidoc Am|void|XPUSHp|char* str|STRLEN len
184 Push a string onto the stack, extending the stack if necessary. The C<len>
185 indicates the length of the string. Handles 'set' magic. See
188 =for apidoc Am|void|XPUSHn|NV nv
189 Push a double onto the stack, extending the stack if necessary. Handles
190 'set' magic. See C<PUSHn>.
192 =for apidoc Am|void|XPUSHi|IV iv
193 Push an integer onto the stack, extending the stack if necessary. Handles
194 'set' magic. See C<PUSHi>.
196 =for apidoc Am|void|XPUSHu|UV uv
197 Push an unsigned integer onto the stack, extending the stack if necessary.
203 #define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (n)) { \
204 sp = stack_grow(sp,p, (int) (n)); \
207 /* Same thing, but update mark register too. */
208 #define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (n)) { \
209 int markoff = mark - PL_stack_base; \
210 sp = stack_grow(sp,p,(int) (n)); \
211 mark = PL_stack_base + markoff; \
214 #define PUSHs(s) (*++sp = (s))
215 #define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
216 #define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
217 #define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
218 #define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
219 #define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
221 #define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
222 #define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
223 #define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
224 #define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
225 #define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
226 #define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
227 #define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
229 #define SETs(s) (*sp = s)
230 #define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
231 #define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
232 #define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
233 #define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
234 #define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
236 #define dTOPss SV *sv = TOPs
237 #define dPOPss SV *sv = POPs
238 #define dTOPnv NV value = TOPn
239 #define dPOPnv NV value = POPn
240 #define dTOPiv IV value = TOPi
241 #define dPOPiv IV value = POPi
242 #define dTOPuv UV value = TOPu
243 #define dPOPuv UV value = POPu
245 #define dTOPqv Quad_t value = TOPu
246 #define dPOPqv Quad_t value = POPu
247 #define dTOPuqv Uquad_t value = TOPuq
248 #define dPOPuqv Uquad_t value = POPuq
251 #define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
252 #define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
253 #define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
255 #define USE_LEFT(sv) \
256 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
257 #define dPOPXnnrl_ul(X) \
259 SV *leftsv = CAT2(X,s); \
260 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
261 #define dPOPXiirl_ul(X) \
263 SV *leftsv = CAT2(X,s); \
264 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
266 #define dPOPPOPssrl dPOPXssrl(POP)
267 #define dPOPPOPnnrl dPOPXnnrl(POP)
268 #define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
269 #define dPOPPOPiirl dPOPXiirl(POP)
270 #define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
272 #define dPOPTOPssrl dPOPXssrl(TOP)
273 #define dPOPTOPnnrl dPOPXnnrl(TOP)
274 #define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
275 #define dPOPTOPiirl dPOPXiirl(TOP)
276 #define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
278 #define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
279 #define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
280 #define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
282 #define RETSETYES RETURNX(SETs(&PL_sv_yes))
283 #define RETSETNO RETURNX(SETs(&PL_sv_no))
284 #define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
286 #define ARGTARG PL_op->op_targ
288 /* See OPpTARGET_MY: */
289 #define MAXARG (PL_op->op_private & 15)
291 #define SWITCHSTACK(f,t) \
293 AvFILLp(f) = sp - PL_stack_base; \
294 PL_stack_base = AvARRAY(t); \
295 PL_stack_max = PL_stack_base + AvMAX(t); \
296 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
300 #define EXTEND_MORTAL(n) \
302 if (PL_tmps_ix + (n) >= PL_tmps_max) \
306 #define AMGf_noright 1
307 #define AMGf_noleft 2
308 #define AMGf_assign 4
311 #define tryAMAGICbinW(meth,assign,set) STMT_START { \
312 if (PL_amagic_generation) { \
314 SV* right= *(sp); SV* left= *(sp-1);\
315 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
316 (tmpsv=amagic_call(left, \
319 (assign)? AMGf_assign: 0))) {\
321 (void)POPs; set(tmpsv); RETURN; } \
325 #define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
326 #define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
328 #define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
329 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
330 #define AMG_CALLbinL(left,right,meth) \
331 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
333 #define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
334 if (PL_amagic_generation) { \
336 SV* arg= sp[shift]; \
338 if ((SvAMAGIC(arg))&&\
339 (tmpsv=AMG_CALLun(arg,meth))) {\
340 SPAGAIN; if (shift) sp += shift; \
345 #define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
347 #define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
348 #define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
349 #define tryAMAGICunTARGET(meth, shift) \
350 { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
352 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
354 #define setAGAIN(ref) sv = ref; \
356 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
357 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
362 #define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
364 #define opASSIGN (PL_op->op_flags & OPf_STACKED)
365 #define SETsv(sv) STMT_START { \
366 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
367 { sv_setsv(TARG, (sv)); SETTARG; } \
368 else SETs(sv); } STMT_END
370 #define SETsvUN(sv) STMT_START { \
371 if (SvFLAGS(TARG) & SVs_PADMY) \
372 { sv_setsv(TARG, (sv)); SETTARG; } \
373 else SETs(sv); } STMT_END
375 /* newSVsv does not behave as advertised, so we copy missing
376 * information by hand */
378 /* SV* ref causes confusion with the member variable
379 changed SV* ref to SV* tmpRef */
380 #define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
381 if (SvREFCNT(tmpRef)>1) { \
382 SvREFCNT_dec(tmpRef); \
383 SvRV(rv)=AMG_CALLun(rv,copy); \
387 =for apidoc mU||LVRET
388 True if this op will be the return value of an lvalue subroutine
391 #define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())