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