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