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