This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix type mismatches in x2p's safe{alloc,realloc,free}.
[perl5.git] / pp.h
CommitLineData
a0d0e21e 1/* pp.h
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, 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
LW
9
10#define ARGS
463ee0b2 11#define ARGSproto void
79072805
LW
12#define dARGS
13#define PP(s) OP* s(ARGS) dARGS
14
15#define SP sp
16#define MARK mark
17#define TARG targ
18
a0d0e21e
LW
19#define PUSHMARK(p) if (++markstack_ptr == markstack_max) \
20 markstack_grow(); \
21 *markstack_ptr = (p) - stack_base
22
23#define TOPMARK (*markstack_ptr)
79072805 24#define POPMARK (*markstack_ptr--)
a0d0e21e 25
79072805
LW
26#define dSP register SV **sp = stack_sp
27#define dMARK register SV **mark = stack_base + POPMARK
28#define dORIGMARK I32 origmark = mark - stack_base
29#define SETORIGMARK origmark = mark - stack_base
a0d0e21e 30#define ORIGMARK (stack_base + origmark)
79072805
LW
31
32#define SPAGAIN sp = stack_sp
33#define MSPAGAIN sp = stack_sp; mark = ORIGMARK
34
35#define GETTARGETSTACKED targ = (op->op_flags & OPf_STACKED ? POPs : PAD_SV(op->op_targ))
36#define dTARGETSTACKED SV * GETTARGETSTACKED
37
38#define GETTARGET targ = PAD_SV(op->op_targ)
39#define dTARGET SV * GETTARGET
40
41#define GETATARGET targ = (op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(op->op_targ))
42#define dATARGET SV * GETATARGET
43
44#define dTARG SV *targ
45
79072805
LW
46#define NORMAL op->op_next
47#define DIE return die
79072805
LW
48
49#define PUTBACK stack_sp = sp
50#define RETURN return PUTBACK, NORMAL
51#define RETURNOP(o) return PUTBACK, o
52#define RETURNX(x) return x, PUTBACK, NORMAL
53
54#define POPs (*sp--)
463ee0b2
LW
55#define POPp (SvPVx(POPs, na))
56#define POPn (SvNVx(POPs))
a0d0e21e 57#define POPi ((IV)SvIVx(POPs))
55497cff 58#define POPu ((UV)SvIVx(POPs))
463ee0b2 59#define POPl ((long)SvIVx(POPs))
79072805
LW
60
61#define TOPs (*sp)
463ee0b2
LW
62#define TOPp (SvPV(TOPs, na))
63#define TOPn (SvNV(TOPs))
a0d0e21e 64#define TOPi ((IV)SvIV(TOPs))
55497cff 65#define TOPu ((UV)SvIV(TOPs))
463ee0b2 66#define TOPl ((long)SvIV(TOPs))
79072805
LW
67
68/* Go to some pains in the rare event that we must extend the stack. */
86547924 69#define EXTEND(p,n) STMT_START { if (stack_max - p < (n)) { \
a0d0e21e 70 sp = stack_grow(sp,p, (int) (n)); \
86547924 71 } } STMT_END
a0d0e21e 72
79072805 73/* Same thing, but update mark register too. */
86547924 74#define MEXTEND(p,n) STMT_START {if (stack_max - p < (n)) { \
a0d0e21e
LW
75 int markoff = mark - stack_base; \
76 sp = stack_grow(sp,p,(int) (n)); \
77 mark = stack_base + markoff; \
86547924 78 } } STMT_END
79072805
LW
79
80#define PUSHs(s) (*++sp = (s))
86547924
PP
81#define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
82#define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
83#define PUSHn(n) STMT_START { sv_setnv(TARG, (double)(n)); PUSHTARG; } STMT_END
84#define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
55497cff 85#define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
79072805 86
86547924
PP
87#define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
88#define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
89#define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
90#define XPUSHn(n) STMT_START { sv_setnv(TARG, (double)(n)); XPUSHTARG; } STMT_END
91#define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
55497cff 92#define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
79072805
LW
93
94#define SETs(s) (*sp = s)
86547924
PP
95#define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
96#define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
97#define SETn(n) STMT_START { sv_setnv(TARG, (double)(n)); SETTARG; } STMT_END
98#define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
55497cff 99#define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
a0d0e21e 100
79072805
LW
101#define dTOPss SV *sv = TOPs
102#define dPOPss SV *sv = POPs
103#define dTOPnv double value = TOPn
104#define dPOPnv double value = POPn
a0d0e21e
LW
105#define dTOPiv IV value = TOPi
106#define dPOPiv IV value = POPi
55497cff
PP
107#define dTOPuv UV value = TOPu
108#define dPOPuv UV value = POPu
79072805 109
a0d0e21e 110#define dPOPPOPssrl SV *right = POPs; SV *left = POPs
79072805 111#define dPOPPOPnnrl double right = POPn; double left = POPn
a0d0e21e 112#define dPOPPOPiirl IV right = POPi; IV left = POPi
55497cff 113#define dPOPPOPuurl UV right = POPu; UV left = POPu
79072805 114
a0d0e21e 115#define dPOPTOPssrl SV *right = POPs; SV *left = TOPs
79072805 116#define dPOPTOPnnrl double right = POPn; double left = TOPn
a0d0e21e 117#define dPOPTOPiirl IV right = POPi; IV left = TOPi
55497cff 118#define dPOPTOPuurl UV right = POPu; UV left = TOPu
79072805
LW
119
120#define RETPUSHYES RETURNX(PUSHs(&sv_yes))
121#define RETPUSHNO RETURNX(PUSHs(&sv_no))
122#define RETPUSHUNDEF RETURNX(PUSHs(&sv_undef))
123
124#define RETSETYES RETURNX(SETs(&sv_yes))
125#define RETSETNO RETURNX(SETs(&sv_no))
126#define RETSETUNDEF RETURNX(SETs(&sv_undef))
127
128#define ARGTARG op->op_targ
129#define MAXARG op->op_private
130
131#define SWITCHSTACK(f,t) AvFILL(f) = sp - stack_base; \
132 stack_base = AvARRAY(t); \
133 stack_max = stack_base + AvMAX(t); \
a0d0e21e 134 sp = stack_sp = stack_base + AvFILL(t); \
e9905555 135 curstack = t;
79072805 136
a0d0e21e
LW
137#ifdef OVERLOAD
138
139#define AMGf_noright 1
140#define AMGf_noleft 2
141#define AMGf_assign 4
142#define AMGf_unary 8
143
86547924 144#define tryAMAGICbinW(meth,assign,set) STMT_START { \
a0d0e21e
LW
145 if (amagic_generation) { \
146 SV* tmpsv; \
147 SV* right= *(sp); SV* left= *(sp-1);\
148 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
149 (tmpsv=amagic_call(left, \
150 right, \
151 CAT2(meth,_amg), \
152 (assign)? AMGf_assign: 0))) {\
153 SPAGAIN; \
154 (void)POPs; set(tmpsv); RETURN; } \
155 } \
86547924 156 } STMT_END
a0d0e21e
LW
157
158#define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
159#define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
160
161#define AMG_CALLun(sv,meth) amagic_call(sv,&sv_undef, \
162 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
163#define AMG_CALLbinL(left,right,meth) \
164 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
165
86547924 166#define tryAMAGICunW(meth,set) STMT_START { \
a0d0e21e
LW
167 if (amagic_generation) { \
168 SV* tmpsv; \
169 SV* arg= *(sp); \
170 if ((SvAMAGIC(arg))&&\
171 (tmpsv=AMG_CALLun(arg,meth))) {\
172 SPAGAIN; \
173 set(tmpsv); RETURN; } \
174 } \
86547924 175 } STMT_END
a0d0e21e 176
e9905555 177#define tryAMAGICun tryAMAGICunSET
a0d0e21e
LW
178#define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs)
179
180#define opASSIGN (op->op_flags & OPf_STACKED)
e9905555
PP
181#define SETsv(sv) STMT_START { \
182 if (opASSIGN) { sv_setsv(TARG, (sv)); SETTARG; } \
183 else SETs(sv); } STMT_END
a0d0e21e
LW
184
185/* newSVsv does not behave as advertised, so we copy missing
186 * information by hand */
187
188
86547924 189#define RvDEEPCP(rv) STMT_START { SV* ref=SvRV(rv); \
a0d0e21e
LW
190 if (SvREFCNT(ref)>1) { \
191 SvREFCNT_dec(ref); \
748a9306 192 SvRV(rv)=AMG_CALLun(rv,copy); \
86547924 193 } } STMT_END
a0d0e21e
LW
194#else
195
196#define tryAMAGICbin(a,b)
197#define tryAMAGICbinSET(a,b)
198#define tryAMAGICun(a)
199#define tryAMAGICunSET(a)
200
201#endif /* OVERLOAD */