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