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