This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
portability fix for IO::File and FileHandle
[perl5.git] / pp.h
CommitLineData
a0d0e21e 1/* pp.h
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, 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
11343788
MB
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
LW
19
20#define SP sp
21#define MARK mark
22#define TARG targ
23
3280af22 24#define PUSHMARK(p) if (++PL_markstack_ptr == PL_markstack_max) \
a0d0e21e 25 markstack_grow(); \
3280af22 26 *PL_markstack_ptr = (p) - PL_stack_base
a0d0e21e 27
3280af22
NIS
28#define TOPMARK (*PL_markstack_ptr)
29#define POPMARK (*PL_markstack_ptr--)
a0d0e21e 30
3280af22 31#define djSP register SV **sp = PL_stack_sp
4e35701f 32#define dSP dTHR; djSP
3280af22
NIS
33#define dMARK register SV **mark = PL_stack_base + POPMARK
34#define dORIGMARK I32 origmark = mark - PL_stack_base
35#define SETORIGMARK origmark = mark - PL_stack_base
36#define ORIGMARK (PL_stack_base + origmark)
79072805 37
3280af22
NIS
38#define SPAGAIN sp = PL_stack_sp
39#define MSPAGAIN sp = PL_stack_sp; mark = ORIGMARK
79072805 40
533c011a 41#define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
79072805
LW
42#define dTARGETSTACKED SV * GETTARGETSTACKED
43
533c011a 44#define GETTARGET targ = PAD_SV(PL_op->op_targ)
79072805
LW
45#define dTARGET SV * GETTARGET
46
533c011a 47#define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
79072805
LW
48#define dATARGET SV * GETATARGET
49
50#define dTARG SV *targ
51
533c011a 52#define NORMAL PL_op->op_next
cea2e8a9 53#define DIE return Perl_die
79072805 54
3280af22 55#define PUTBACK PL_stack_sp = sp
79072805
LW
56#define RETURN return PUTBACK, NORMAL
57#define RETURNOP(o) return PUTBACK, o
58#define RETURNX(x) return x, PUTBACK, NORMAL
59
60#define POPs (*sp--)
2d8e6c8d
GS
61#define POPp (SvPVx(POPs, PL_na)) /* deprecated */
62#define POPpx (SvPVx(POPs, n_a))
463ee0b2 63#define POPn (SvNVx(POPs))
a0d0e21e 64#define POPi ((IV)SvIVx(POPs))
ff68c719 65#define POPu ((UV)SvUVx(POPs))
463ee0b2 66#define POPl ((long)SvIVx(POPs))
79072805
LW
67
68#define TOPs (*sp)
2d8e6c8d
GS
69#define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
70#define TOPpx (SvPV(TOPs, n_a))
463ee0b2 71#define TOPn (SvNV(TOPs))
a0d0e21e 72#define TOPi ((IV)SvIV(TOPs))
ff68c719 73#define TOPu ((UV)SvUV(TOPs))
463ee0b2 74#define TOPl ((long)SvIV(TOPs))
79072805
LW
75
76/* Go to some pains in the rare event that we must extend the stack. */
3280af22 77#define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (n)) { \
bbce6d69 78 sp = stack_grow(sp,p, (int) (n)); \
86547924 79 } } STMT_END
a0d0e21e 80
79072805 81/* Same thing, but update mark register too. */
3280af22
NIS
82#define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (n)) { \
83 int markoff = mark - PL_stack_base; \
bbce6d69 84 sp = stack_grow(sp,p,(int) (n)); \
3280af22 85 mark = PL_stack_base + markoff; \
86547924 86 } } STMT_END
79072805
LW
87
88#define PUSHs(s) (*++sp = (s))
86547924
PP
89#define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
90#define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
65202027 91#define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
86547924 92#define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
55497cff 93#define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
79072805 94
86547924
PP
95#define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
96#define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
97#define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
65202027 98#define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
86547924 99#define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
55497cff 100#define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
79072805
LW
101
102#define SETs(s) (*sp = s)
86547924
PP
103#define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
104#define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
65202027 105#define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
86547924 106#define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
55497cff 107#define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
a0d0e21e 108
79072805
LW
109#define dTOPss SV *sv = TOPs
110#define dPOPss SV *sv = POPs
65202027
DS
111#define dTOPnv NV value = TOPn
112#define dPOPnv NV value = POPn
a0d0e21e
LW
113#define dTOPiv IV value = TOPi
114#define dPOPiv IV value = POPi
55497cff
PP
115#define dTOPuv UV value = TOPu
116#define dPOPuv UV value = POPu
79072805 117
7a4c00b4 118#define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
65202027 119#define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
7a4c00b4
PP
120#define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
121
122#define USE_LEFT(sv) \
533c011a 123 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
7a4c00b4 124#define dPOPXnnrl_ul(X) \
65202027 125 NV right = POPn; \
7a4c00b4 126 SV *leftsv = CAT2(X,s); \
65202027 127 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
7a4c00b4
PP
128#define dPOPXiirl_ul(X) \
129 IV right = POPi; \
130 SV *leftsv = CAT2(X,s); \
131 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
132
133#define dPOPPOPssrl dPOPXssrl(POP)
134#define dPOPPOPnnrl dPOPXnnrl(POP)
135#define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
136#define dPOPPOPiirl dPOPXiirl(POP)
137#define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
138
139#define dPOPTOPssrl dPOPXssrl(TOP)
140#define dPOPTOPnnrl dPOPXnnrl(TOP)
141#define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
142#define dPOPTOPiirl dPOPXiirl(TOP)
143#define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
79072805 144
3280af22
NIS
145#define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
146#define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
147#define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
79072805 148
3280af22
NIS
149#define RETSETYES RETURNX(SETs(&PL_sv_yes))
150#define RETSETNO RETURNX(SETs(&PL_sv_no))
151#define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
79072805 152
533c011a
NIS
153#define ARGTARG PL_op->op_targ
154#define MAXARG PL_op->op_private
79072805 155
e336de0d
GS
156#define SWITCHSTACK(f,t) \
157 STMT_START { \
677b06e3 158 AvFILLp(f) = sp - PL_stack_base; \
3280af22 159 PL_stack_base = AvARRAY(t); \
677b06e3 160 PL_stack_max = PL_stack_base + AvMAX(t); \
3280af22 161 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
677b06e3 162 PL_curstack = t; \
e336de0d 163 } STMT_END
79072805 164
bbce6d69 165#define EXTEND_MORTAL(n) \
677b06e3
GS
166 STMT_START { \
167 if (PL_tmps_ix + (n) >= PL_tmps_max) \
168 tmps_grow(n); \
169 } STMT_END
bbce6d69 170
a0d0e21e
LW
171#define AMGf_noright 1
172#define AMGf_noleft 2
173#define AMGf_assign 4
174#define AMGf_unary 8
175
86547924 176#define tryAMAGICbinW(meth,assign,set) STMT_START { \
3280af22 177 if (PL_amagic_generation) { \
a0d0e21e
LW
178 SV* tmpsv; \
179 SV* right= *(sp); SV* left= *(sp-1);\
180 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
181 (tmpsv=amagic_call(left, \
182 right, \
183 CAT2(meth,_amg), \
184 (assign)? AMGf_assign: 0))) {\
185 SPAGAIN; \
186 (void)POPs; set(tmpsv); RETURN; } \
187 } \
86547924 188 } STMT_END
a0d0e21e
LW
189
190#define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
191#define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
192
3280af22 193#define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
a0d0e21e
LW
194 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
195#define AMG_CALLbinL(left,right,meth) \
196 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
197
e8c20960 198#define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
3280af22 199 if (PL_amagic_generation) { \
a0d0e21e 200 SV* tmpsv; \
f5284f61
IZ
201 SV* arg= sp[shift]; \
202 am_again: \
a0d0e21e
LW
203 if ((SvAMAGIC(arg))&&\
204 (tmpsv=AMG_CALLun(arg,meth))) {\
f5284f61 205 SPAGAIN; if (shift) sp += shift; \
e8c20960 206 set(tmpsv); ret; } \
a0d0e21e 207 } \
86547924 208 } STMT_END
a0d0e21e 209
f5284f61
IZ
210#define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
211
e9905555 212#define tryAMAGICun tryAMAGICunSET
e8c20960 213#define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
f5284f61
IZ
214#define tryAMAGICunTARGET(meth, shift) \
215 { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
216 { dTARGETSTACKED; \
e8c20960
IZ
217 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}}
218
219#define setAGAIN(ref) sv = arg = ref; \
220 if (!SvROK(ref)) \
cea2e8a9 221 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
e8c20960
IZ
222 goto am_again;
223
0f4592ef 224#define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
a0d0e21e 225
533c011a 226#define opASSIGN (PL_op->op_flags & OPf_STACKED)
e9905555
PP
227#define SETsv(sv) STMT_START { \
228 if (opASSIGN) { sv_setsv(TARG, (sv)); SETTARG; } \
229 else SETs(sv); } STMT_END
a0d0e21e
LW
230
231/* newSVsv does not behave as advertised, so we copy missing
232 * information by hand */
233
76e3520e
GS
234/* SV* ref causes confusion with the member variable
235 changed SV* ref to SV* tmpRef */
236#define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
237 if (SvREFCNT(tmpRef)>1) { \
238 SvREFCNT_dec(tmpRef); \
748a9306 239 SvRV(rv)=AMG_CALLun(rv,copy); \
86547924 240 } } STMT_END