Integrate changes #8659,8702,8808,8809,8810 from maintperl.
[p5sagit/p5-mst-13.2.git] / pp.h
CommitLineData
a0d0e21e 1/* pp.h
79072805 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
79072805 4 *
a0d0e21e 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.
79072805 7 *
a0d0e21e 8 */
79072805 9
11343788 10#ifdef USE_THREADS
11#define ARGS thr
52e1cb5e 12#define dARGS struct perl_thread *thr;
11343788 13#else
79072805 14#define ARGS
79072805 15#define dARGS
11343788 16#endif /* USE_THREADS */
22c35a8c 17
cea2e8a9 18#define PP(s) OP * Perl_##s(pTHX)
79072805 19
954c1994 20/*
21=for apidoc AmU||SP
22Stack pointer. This is usually handled by C<xsubpp>. See C<dSP> and
23C<SPAGAIN>.
24
25=for apidoc AmU||MARK
26Stack marker variable for the XSUB. See C<dMARK>.
27
28=for apidoc Ams||PUSHMARK
29Opening bracket for arguments on a callback. See C<PUTBACK> and
30L<perlcall>.
31
32=for apidoc Ams||dSP
33Declares a local copy of perl's stack pointer for the XSUB, available via
34the C<SP> macro. See C<SP>.
35
fac3506f 36=for apidoc ms||djSP
37
38Declare Just C<SP>. This is actually identical to C<dSP>, and declares
39a local copy of perl's stack pointer, available via the C<SP> macro.
40See C<SP>. (Available for backward source code compatibility with the
41old (Perl 5.005) thread model.)
42
954c1994 43=for apidoc Ams||dMARK
44Declare a stack marker variable, C<mark>, for the XSUB. See C<MARK> and
45C<dORIGMARK>.
46
47=for apidoc Ams||dORIGMARK
48Saves the original stack mark for the XSUB. See C<ORIGMARK>.
49
50=for apidoc AmU||ORIGMARK
51The original stack mark for the XSUB. See C<dORIGMARK>.
52
53=for apidoc Ams||SPAGAIN
54Refetch the stack pointer. Used after a callback. See L<perlcall>.
55
fac3506f 56=cut */
954c1994 57
ac634a9a 58#undef SP /* Solaris 2.7 i386 has this in /usr/include/sys/reg.h */
79072805 59#define SP sp
60#define MARK mark
61#define TARG targ
62
3280af22 63#define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
a0d0e21e 64 markstack_grow(); \
3280af22 65 *PL_markstack_ptr = (p) - PL_stack_base
a0d0e21e 66
3280af22 67#define TOPMARK (*PL_markstack_ptr)
68#define POPMARK (*PL_markstack_ptr--)
a0d0e21e 69
3280af22 70#define djSP register SV **sp = PL_stack_sp
411caa50 71#define dSP djSP
3280af22 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)
79072805 76
3280af22 77#define SPAGAIN sp = PL_stack_sp
78#define MSPAGAIN sp = PL_stack_sp; mark = ORIGMARK
79072805 79
533c011a 80#define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
79072805 81#define dTARGETSTACKED SV * GETTARGETSTACKED
82
533c011a 83#define GETTARGET targ = PAD_SV(PL_op->op_targ)
79072805 84#define dTARGET SV * GETTARGET
85
533c011a 86#define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
79072805 87#define dATARGET SV * GETATARGET
88
89#define dTARG SV *targ
90
533c011a 91#define NORMAL PL_op->op_next
cea2e8a9 92#define DIE return Perl_die
79072805 93
954c1994 94/*
95=for apidoc Ams||PUTBACK
96Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
97See C<PUSHMARK> and L<perlcall> for other uses.
98
99=for apidoc Amn|SV*|POPs
100Pops an SV off the stack.
101
102=for apidoc Amn|char*|POPp
103Pops a string off the stack.
104
105=for apidoc Amn|NV|POPn
106Pops a double off the stack.
107
108=for apidoc Amn|IV|POPi
109Pops an integer off the stack.
110
111=for apidoc Amn|long|POPl
112Pops a long off the stack.
113
114=cut
115*/
116
3280af22 117#define PUTBACK PL_stack_sp = sp
79072805 118#define RETURN return PUTBACK, NORMAL
119#define RETURNOP(o) return PUTBACK, o
120#define RETURNX(x) return x, PUTBACK, NORMAL
121
122#define POPs (*sp--)
2d8e6c8d 123#define POPp (SvPVx(POPs, PL_na)) /* deprecated */
124#define POPpx (SvPVx(POPs, n_a))
463ee0b2 125#define POPn (SvNVx(POPs))
a0d0e21e 126#define POPi ((IV)SvIVx(POPs))
ff68c719 127#define POPu ((UV)SvUVx(POPs))
463ee0b2 128#define POPl ((long)SvIVx(POPs))
d9b3e12d 129#define POPul ((unsigned long)SvIVx(POPs))
1b8cd678 130#ifdef HAS_QUAD
d9b3e12d 131#define POPq ((Quad_t)SvIVx(POPs))
c5a0f51a 132#define POPuq ((Uquad_t)SvUVx(POPs))
d9b3e12d 133#endif
79072805 134
135#define TOPs (*sp)
56370e9f 136#define TOPm1s (*(sp-1))
7dca457a 137#define TOPp1s (*(sp+1))
2d8e6c8d 138#define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
139#define TOPpx (SvPV(TOPs, n_a))
463ee0b2 140#define TOPn (SvNV(TOPs))
a0d0e21e 141#define TOPi ((IV)SvIV(TOPs))
ff68c719 142#define TOPu ((UV)SvUV(TOPs))
463ee0b2 143#define TOPl ((long)SvIV(TOPs))
c5a0f51a 144#define TOPul ((unsigned long)SvUV(TOPs))
1b8cd678 145#ifdef HAS_QUAD
d9b3e12d 146#define TOPq ((Quad_t)SvIV(TOPs))
c5a0f51a 147#define TOPuq ((Uquad_t)SvUV(TOPs))
d9b3e12d 148#endif
79072805 149
150/* Go to some pains in the rare event that we must extend the stack. */
954c1994 151
152/*
153=for apidoc Am|void|EXTEND|SP|int nitems
154Used to extend the argument stack for an XSUB's return values. Once
4375e838 155used, guarantees that there is room for at least C<nitems> to be pushed
954c1994 156onto the stack.
157
158=for apidoc Am|void|PUSHs|SV* sv
aacdac46 159Push an SV onto the stack. The stack must have room for this element.
954c1994 160Does not handle 'set' magic. See C<XPUSHs>.
161
162=for apidoc Am|void|PUSHp|char* str|STRLEN len
163Push a string onto the stack. The stack must have room for this element.
164The C<len> indicates the length of the string. Handles 'set' magic. See
165C<XPUSHp>.
166
167=for apidoc Am|void|PUSHn|NV nv
168Push a double onto the stack. The stack must have room for this element.
169Handles 'set' magic. See C<XPUSHn>.
170
171=for apidoc Am|void|PUSHi|IV iv
172Push an integer onto the stack. The stack must have room for this element.
173Handles 'set' magic. See C<XPUSHi>.
174
175=for apidoc Am|void|PUSHu|UV uv
176Push an unsigned integer onto the stack. The stack must have room for this
177element. See C<XPUSHu>.
178
179=for apidoc Am|void|XPUSHs|SV* sv
180Push an SV onto the stack, extending the stack if necessary. Does not
181handle 'set' magic. See C<PUSHs>.
182
183=for apidoc Am|void|XPUSHp|char* str|STRLEN len
184Push a string onto the stack, extending the stack if necessary. The C<len>
185indicates the length of the string. Handles 'set' magic. See
186C<PUSHp>.
187
188=for apidoc Am|void|XPUSHn|NV nv
189Push a double onto the stack, extending the stack if necessary. Handles
190'set' magic. See C<PUSHn>.
191
192=for apidoc Am|void|XPUSHi|IV iv
193Push an integer onto the stack, extending the stack if necessary. Handles
194'set' magic. See C<PUSHi>.
195
196=for apidoc Am|void|XPUSHu|UV uv
aacdac46 197Push an unsigned integer onto the stack, extending the stack if necessary.
954c1994 198See C<PUSHu>.
199
200=cut
201*/
202
3280af22 203#define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (n)) { \
bbce6d69 204 sp = stack_grow(sp,p, (int) (n)); \
86547924 205 } } STMT_END
a0d0e21e 206
79072805 207/* Same thing, but update mark register too. */
3280af22 208#define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (n)) { \
209 int markoff = mark - PL_stack_base; \
bbce6d69 210 sp = stack_grow(sp,p,(int) (n)); \
3280af22 211 mark = PL_stack_base + markoff; \
86547924 212 } } STMT_END
79072805 213
214#define PUSHs(s) (*++sp = (s))
86547924 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
65202027 217#define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
86547924 218#define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
55497cff 219#define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
79072805 220
86547924 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
65202027 224#define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
86547924 225#define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
55497cff 226#define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
b162f9ea 227#define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
79072805 228
229#define SETs(s) (*sp = s)
86547924 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
65202027 232#define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
86547924 233#define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
55497cff 234#define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
a0d0e21e 235
79072805 236#define dTOPss SV *sv = TOPs
237#define dPOPss SV *sv = POPs
65202027 238#define dTOPnv NV value = TOPn
239#define dPOPnv NV value = POPn
a0d0e21e 240#define dTOPiv IV value = TOPi
241#define dPOPiv IV value = POPi
55497cff 242#define dTOPuv UV value = TOPu
243#define dPOPuv UV value = POPu
1b8cd678 244#ifdef HAS_QUAD
2d4389e4 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
249#endif
79072805 250
7a4c00b4 251#define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
65202027 252#define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
7a4c00b4 253#define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
254
255#define USE_LEFT(sv) \
533c011a 256 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
7a4c00b4 257#define dPOPXnnrl_ul(X) \
65202027 258 NV right = POPn; \
7a4c00b4 259 SV *leftsv = CAT2(X,s); \
65202027 260 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
7a4c00b4 261#define dPOPXiirl_ul(X) \
262 IV right = POPi; \
263 SV *leftsv = CAT2(X,s); \
264 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
265
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)
271
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)
79072805 277
3280af22 278#define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
279#define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
280#define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
79072805 281
3280af22 282#define RETSETYES RETURNX(SETs(&PL_sv_yes))
283#define RETSETNO RETURNX(SETs(&PL_sv_no))
284#define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
79072805 285
533c011a 286#define ARGTARG PL_op->op_targ
b162f9ea 287
288 /* See OPpTARGET_MY: */
289#define MAXARG (PL_op->op_private & 15)
79072805 290
e336de0d 291#define SWITCHSTACK(f,t) \
292 STMT_START { \
677b06e3 293 AvFILLp(f) = sp - PL_stack_base; \
3280af22 294 PL_stack_base = AvARRAY(t); \
677b06e3 295 PL_stack_max = PL_stack_base + AvMAX(t); \
3280af22 296 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
677b06e3 297 PL_curstack = t; \
e336de0d 298 } STMT_END
79072805 299
bbce6d69 300#define EXTEND_MORTAL(n) \
677b06e3 301 STMT_START { \
302 if (PL_tmps_ix + (n) >= PL_tmps_max) \
303 tmps_grow(n); \
304 } STMT_END
bbce6d69 305
a0d0e21e 306#define AMGf_noright 1
307#define AMGf_noleft 2
308#define AMGf_assign 4
309#define AMGf_unary 8
310
86547924 311#define tryAMAGICbinW(meth,assign,set) STMT_START { \
3280af22 312 if (PL_amagic_generation) { \
a0d0e21e 313 SV* tmpsv; \
314 SV* right= *(sp); SV* left= *(sp-1);\
315 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
316 (tmpsv=amagic_call(left, \
317 right, \
318 CAT2(meth,_amg), \
319 (assign)? AMGf_assign: 0))) {\
320 SPAGAIN; \
321 (void)POPs; set(tmpsv); RETURN; } \
322 } \
86547924 323 } STMT_END
a0d0e21e 324
325#define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
326#define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
327
3280af22 328#define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
a0d0e21e 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)
332
e8c20960 333#define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
3280af22 334 if (PL_amagic_generation) { \
a0d0e21e 335 SV* tmpsv; \
f5284f61 336 SV* arg= sp[shift]; \
337 am_again: \
a0d0e21e 338 if ((SvAMAGIC(arg))&&\
339 (tmpsv=AMG_CALLun(arg,meth))) {\
f5284f61 340 SPAGAIN; if (shift) sp += shift; \
e8c20960 341 set(tmpsv); ret; } \
a0d0e21e 342 } \
86547924 343 } STMT_END
a0d0e21e 344
f5284f61 345#define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
346
b162f9ea 347#define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
348#define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
f5284f61 349#define tryAMAGICunTARGET(meth, shift) \
350 { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
351 { dTARGETSTACKED; \
e8c20960 352 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
353
aacdac46 354#define setAGAIN(ref) sv = ref; \
355 if (!SvROK(ref)) \
cea2e8a9 356 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
aacdac46 357 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
358 arg = ref; \
359 goto am_again; \
360 }
e8c20960 361
0f4592ef 362#define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
a0d0e21e 363
533c011a 364#define opASSIGN (PL_op->op_flags & OPf_STACKED)
e9905555 365#define SETsv(sv) STMT_START { \
b162f9ea 366 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
367 { sv_setsv(TARG, (sv)); SETTARG; } \
368 else SETs(sv); } STMT_END
369
370#define SETsvUN(sv) STMT_START { \
371 if (SvFLAGS(TARG) & SVs_PADMY) \
372 { sv_setsv(TARG, (sv)); SETTARG; } \
e9905555 373 else SETs(sv); } STMT_END
a0d0e21e 374
375/* newSVsv does not behave as advertised, so we copy missing
376 * information by hand */
377
76e3520e 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); \
748a9306 383 SvRV(rv)=AMG_CALLun(rv,copy); \
86547924 384 } } STMT_END
78f9721b 385
386/*
387=for apidoc mU||LVRET
388True if this op will be the return value of an lvalue subroutine
389
390=cut */
9d96b2e7 391#define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())