This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't do the ... expansion on archlib, sitelib etc, given that
[perl5.git] / pp.h
... / ...
CommitLineData
1/* pp.h
2 *
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4 * 2000, 2001, by Larry Wall and others
5 *
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.
8 *
9 */
10
11#define PP(s) OP * Perl_##s(pTHX)
12
13/*
14=head1 Stack Manipulation Macros
15
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 Am|void|PUSHMARK|SP
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
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
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
51=cut */
52
53#undef SP /* Solaris 2.7 i386 has this in /usr/include/sys/reg.h */
54#define SP sp
55#define MARK mark
56#define TARG targ
57
58#define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
59 markstack_grow(); \
60 *PL_markstack_ptr = (p) - PL_stack_base
61
62#define TOPMARK (*PL_markstack_ptr)
63#define POPMARK (*PL_markstack_ptr--)
64
65#define dSP register SV **sp = PL_stack_sp
66#define djSP dSP
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)
71
72#define SPAGAIN sp = PL_stack_sp
73#define MSPAGAIN sp = PL_stack_sp; mark = ORIGMARK
74
75#define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
76#define dTARGETSTACKED SV * GETTARGETSTACKED
77
78#define GETTARGET targ = PAD_SV(PL_op->op_targ)
79#define dTARGET SV * GETTARGET
80
81#define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
82#define dATARGET SV * GETATARGET
83
84#define dTARG SV *targ
85
86#define NORMAL PL_op->op_next
87#define DIE return Perl_die
88
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
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
102Pops a string off the stack.
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.
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
121#define PUTBACK PL_stack_sp = sp
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--)
127#define POPp (SvPVx(POPs, PL_na)) /* deprecated */
128#define POPpx (SvPVx(POPs, n_a))
129#define POPpbytex (SvPVbytex(POPs, n_a))
130#define POPn (SvNVx(POPs))
131#define POPi ((IV)SvIVx(POPs))
132#define POPu ((UV)SvUVx(POPs))
133#define POPl ((long)SvIVx(POPs))
134#define POPul ((unsigned long)SvIVx(POPs))
135#ifdef HAS_QUAD
136#define POPq ((Quad_t)SvIVx(POPs))
137#define POPuq ((Uquad_t)SvUVx(POPs))
138#endif
139
140#define TOPs (*sp)
141#define TOPm1s (*(sp-1))
142#define TOPp1s (*(sp+1))
143#define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
144#define TOPpx (SvPV(TOPs, n_a))
145#define TOPn (SvNV(TOPs))
146#define TOPi ((IV)SvIV(TOPs))
147#define TOPu ((UV)SvUV(TOPs))
148#define TOPl ((long)SvIV(TOPs))
149#define TOPul ((unsigned long)SvUV(TOPs))
150#ifdef HAS_QUAD
151#define TOPq ((Quad_t)SvIV(TOPs))
152#define TOPuq ((Uquad_t)SvUV(TOPs))
153#endif
154
155/* Go to some pains in the rare event that we must extend the stack. */
156
157/*
158=for apidoc Am|void|EXTEND|SP|int nitems
159Used to extend the argument stack for an XSUB's return values. Once
160used, guarantees that there is room for at least C<nitems> to be pushed
161onto the stack.
162
163=for apidoc Am|void|PUSHs|SV* sv
164Push an SV onto the stack. The stack must have room for this element.
165Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
166C<XPUSHs> and C<XPUSHmortal>.
167
168=for apidoc Am|void|PUSHp|char* str|STRLEN len
169Push a string onto the stack. The stack must have room for this element.
170The C<len> indicates the length of the string. Handles 'set' magic. Uses
171C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
172call multiple C<TARG>-oriented macros to return lists from XSUB's - see
173C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
174
175=for apidoc Am|void|PUSHn|NV nv
176Push a double onto the stack. The stack must have room for this element.
177Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
178called to declare it. Do not call multiple C<TARG>-oriented macros to
179return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
180C<mXPUSHn>.
181
182=for apidoc Am|void|PUSHi|IV iv
183Push an integer onto the stack. The stack must have room for this element.
184Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
185called to declare it. Do not call multiple C<TARG>-oriented macros to
186return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
187C<mXPUSHi>.
188
189=for apidoc Am|void|PUSHu|UV uv
190Push an unsigned integer onto the stack. The stack must have room for this
191element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
192should be called to declare it. Do not call multiple C<TARG>-oriented
193macros to return lists from XSUB's - see C<mPUSHu> instead. See also
194C<XPUSHu> and C<mXPUSHu>.
195
196=for apidoc Am|void|XPUSHs|SV* sv
197Push an SV onto the stack, extending the stack if necessary. Does not
198handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
199C<PUSHs> and C<PUSHmortal>.
200
201=for apidoc Am|void|XPUSHp|char* str|STRLEN len
202Push a string onto the stack, extending the stack if necessary. The C<len>
203indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
204C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
205multiple C<TARG>-oriented macros to return lists from XSUB's - see
206C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
207
208=for apidoc Am|void|XPUSHn|NV nv
209Push a double onto the stack, extending the stack if necessary. Handles
210'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
211declare it. Do not call multiple C<TARG>-oriented macros to return lists
212from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
213
214=for apidoc Am|void|XPUSHi|IV iv
215Push an integer onto the stack, extending the stack if necessary. Handles
216'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
217declare it. Do not call multiple C<TARG>-oriented macros to return lists
218from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
219
220=for apidoc Am|void|XPUSHu|UV uv
221Push an unsigned integer onto the stack, extending the stack if necessary.
222Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
223called to declare it. Do not call multiple C<TARG>-oriented macros to
224return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
225C<mPUSHu>.
226
227=for apidoc Am|void|PUSHmortal
228Push a new mortal SV onto the stack. The stack must have room for this
229element. Does not handle 'set' magic. Does not use C<TARG>. See also
230C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
231
232=for apidoc Am|void|mPUSHp|char* str|STRLEN len
233Push a string onto the stack. The stack must have room for this element.
234The C<len> indicates the length of the string. Handles 'set' magic. Does
235not use C<TARG>. See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
236
237=for apidoc Am|void|mPUSHn|NV nv
238Push a double onto the stack. The stack must have room for this element.
239Handles 'set' magic. Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn>
240and C<XPUSHn>.
241
242=for apidoc Am|void|mPUSHi|IV iv
243Push an integer onto the stack. The stack must have room for this element.
244Handles 'set' magic. Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi>
245and C<XPUSHi>.
246
247=for apidoc Am|void|mPUSHu|UV uv
248Push an unsigned integer onto the stack. The stack must have room for this
249element. Handles 'set' magic. Does not use C<TARG>. See also C<PUSHu>,
250C<mXPUSHu> and C<XPUSHu>.
251
252=for apidoc Am|void|XPUSHmortal
253Push a new mortal SV onto the stack, extending the stack if necessary. Does
254not handle 'set' magic. Does not use C<TARG>. See also C<XPUSHs>,
255C<PUSHmortal> and C<PUSHs>.
256
257=for apidoc Am|void|mXPUSHp|char* str|STRLEN len
258Push a string onto the stack, extending the stack if necessary. The C<len>
259indicates the length of the string. Handles 'set' magic. Does not use
260C<TARG>. See also C<XPUSHp>, C<mPUSHp> and C<PUSHp>.
261
262=for apidoc Am|void|mXPUSHn|NV nv
263Push a double onto the stack, extending the stack if necessary. Handles
264'set' magic. Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and
265C<PUSHn>.
266
267=for apidoc Am|void|mXPUSHi|IV iv
268Push an integer onto the stack, extending the stack if necessary. Handles
269'set' magic. Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and
270C<PUSHi>.
271
272=for apidoc Am|void|mXPUSHu|UV uv
273Push an unsigned integer onto the stack, extending the stack if necessary.
274Handles 'set' magic. Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu>
275and C<PUSHu>.
276
277=cut
278*/
279
280#define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
281 sp = stack_grow(sp,p, (int) (n)); \
282 } } STMT_END
283
284/* Same thing, but update mark register too. */
285#define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
286 int markoff = mark - PL_stack_base; \
287 sp = stack_grow(sp,p,(int) (n)); \
288 mark = PL_stack_base + markoff; \
289 } } STMT_END
290
291#define PUSHs(s) (*++sp = (s))
292#define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
293#define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
294#define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
295#define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
296#define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
297
298#define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
299#define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
300#define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
301#define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
302#define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
303#define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
304#define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
305
306#define PUSHmortal PUSHs(sv_newmortal())
307#define mPUSHp(p,l) sv_setpvn_mg(PUSHmortal, (p), (l))
308#define mPUSHn(n) sv_setnv_mg(PUSHmortal, (NV)(n))
309#define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
310#define mPUSHu(u) sv_setuv_mg(PUSHmortal, (UV)(u))
311
312#define XPUSHmortal XPUSHs(sv_newmortal())
313#define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); sv_setpvn_mg(PUSHmortal, (p), (l)); } STMT_END
314#define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv_mg(PUSHmortal, (NV)(n)); } STMT_END
315#define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv_mg(PUSHmortal, (IV)(i)); } STMT_END
316#define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv_mg(PUSHmortal, (UV)(u)); } STMT_END
317
318#define SETs(s) (*sp = s)
319#define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
320#define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
321#define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
322#define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
323#define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
324
325#define dTOPss SV *sv = TOPs
326#define dPOPss SV *sv = POPs
327#define dTOPnv NV value = TOPn
328#define dPOPnv NV value = POPn
329#define dTOPiv IV value = TOPi
330#define dPOPiv IV value = POPi
331#define dTOPuv UV value = TOPu
332#define dPOPuv UV value = POPu
333#ifdef HAS_QUAD
334#define dTOPqv Quad_t value = TOPu
335#define dPOPqv Quad_t value = POPu
336#define dTOPuqv Uquad_t value = TOPuq
337#define dPOPuqv Uquad_t value = POPuq
338#endif
339
340#define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
341#define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
342#define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
343
344#define USE_LEFT(sv) \
345 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
346#define dPOPXnnrl_ul(X) \
347 NV right = POPn; \
348 SV *leftsv = CAT2(X,s); \
349 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
350#define dPOPXiirl_ul(X) \
351 IV right = POPi; \
352 SV *leftsv = CAT2(X,s); \
353 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
354
355#define dPOPPOPssrl dPOPXssrl(POP)
356#define dPOPPOPnnrl dPOPXnnrl(POP)
357#define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
358#define dPOPPOPiirl dPOPXiirl(POP)
359#define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
360
361#define dPOPTOPssrl dPOPXssrl(TOP)
362#define dPOPTOPnnrl dPOPXnnrl(TOP)
363#define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
364#define dPOPTOPiirl dPOPXiirl(TOP)
365#define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
366
367#define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
368#define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
369#define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
370
371#define RETSETYES RETURNX(SETs(&PL_sv_yes))
372#define RETSETNO RETURNX(SETs(&PL_sv_no))
373#define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
374
375#define ARGTARG PL_op->op_targ
376
377 /* See OPpTARGET_MY: */
378#define MAXARG (PL_op->op_private & 15)
379
380#define SWITCHSTACK(f,t) \
381 STMT_START { \
382 AvFILLp(f) = sp - PL_stack_base; \
383 PL_stack_base = AvARRAY(t); \
384 PL_stack_max = PL_stack_base + AvMAX(t); \
385 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
386 PL_curstack = t; \
387 } STMT_END
388
389#define EXTEND_MORTAL(n) \
390 STMT_START { \
391 if (PL_tmps_ix + (n) >= PL_tmps_max) \
392 tmps_grow(n); \
393 } STMT_END
394
395#define AMGf_noright 1
396#define AMGf_noleft 2
397#define AMGf_assign 4
398#define AMGf_unary 8
399
400#define tryAMAGICbinW(meth,assign,set) STMT_START { \
401 if (PL_amagic_generation) { \
402 SV* tmpsv; \
403 SV* right= *(sp); SV* left= *(sp-1);\
404 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
405 (tmpsv=amagic_call(left, \
406 right, \
407 CAT2(meth,_amg), \
408 (assign)? AMGf_assign: 0))) {\
409 SPAGAIN; \
410 (void)POPs; set(tmpsv); RETURN; } \
411 } \
412 } STMT_END
413
414#define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
415#define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
416
417#define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
418 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
419#define AMG_CALLbinL(left,right,meth) \
420 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
421
422#define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
423 if (PL_amagic_generation) { \
424 SV* tmpsv; \
425 SV* arg= sp[shift]; \
426 if(0) goto am_again; /* shut up unused warning */ \
427 am_again: \
428 if ((SvAMAGIC(arg))&&\
429 (tmpsv=AMG_CALLun(arg,meth))) {\
430 SPAGAIN; if (shift) sp += shift; \
431 set(tmpsv); ret; } \
432 } \
433 } STMT_END
434
435#define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
436
437#define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
438#define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
439#define tryAMAGICunTARGET(meth, shift) \
440 { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
441 { dTARGETSTACKED; \
442 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
443
444#define setAGAIN(ref) sv = ref; \
445 if (!SvROK(ref)) \
446 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
447 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
448 arg = ref; \
449 goto am_again; \
450 }
451
452#define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
453
454#define opASSIGN (PL_op->op_flags & OPf_STACKED)
455#define SETsv(sv) STMT_START { \
456 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
457 { sv_setsv(TARG, (sv)); SETTARG; } \
458 else SETs(sv); } STMT_END
459
460#define SETsvUN(sv) STMT_START { \
461 if (SvFLAGS(TARG) & SVs_PADMY) \
462 { sv_setsv(TARG, (sv)); SETTARG; } \
463 else SETs(sv); } STMT_END
464
465/* newSVsv does not behave as advertised, so we copy missing
466 * information by hand */
467
468/* SV* ref causes confusion with the member variable
469 changed SV* ref to SV* tmpRef */
470#define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
471 if (SvREFCNT(tmpRef)>1) { \
472 SvRV(rv)=AMG_CALLun(rv,copy); \
473 SvREFCNT_dec(tmpRef); \
474 } } STMT_END
475
476/*
477=for apidoc mU||LVRET
478True if this op will be the return value of an lvalue subroutine
479
480=cut */
481#define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())