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