This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Encoding neutral unpack
[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
c578083c 23=for apidoc Am|void|PUSHMARK|SP
954c1994
GS
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
16e7e110
TH
58#define PUSHMARK(p) \
59 STMT_START { \
60 if (++PL_markstack_ptr == PL_markstack_max) \
61 markstack_grow(); \
62 *PL_markstack_ptr = (p) - PL_stack_base; \
63 } STMT_END
a0d0e21e 64
3280af22
NIS
65#define TOPMARK (*PL_markstack_ptr)
66#define POPMARK (*PL_markstack_ptr--)
a0d0e21e 67
39644a26
TJ
68#define dSP register SV **sp = PL_stack_sp
69#define djSP dSP
3280af22
NIS
70#define dMARK register SV **mark = PL_stack_base + POPMARK
71#define dORIGMARK I32 origmark = mark - PL_stack_base
72#define SETORIGMARK origmark = mark - PL_stack_base
73#define ORIGMARK (PL_stack_base + origmark)
79072805 74
3280af22 75#define SPAGAIN sp = PL_stack_sp
16e7e110 76#define MSPAGAIN STMT_START { sp = PL_stack_sp; mark = ORIGMARK; } STMT_END
79072805 77
533c011a 78#define GETTARGETSTACKED targ = (PL_op->op_flags & OPf_STACKED ? POPs : PAD_SV(PL_op->op_targ))
79072805
LW
79#define dTARGETSTACKED SV * GETTARGETSTACKED
80
533c011a 81#define GETTARGET targ = PAD_SV(PL_op->op_targ)
79072805
LW
82#define dTARGET SV * GETTARGET
83
533c011a 84#define GETATARGET targ = (PL_op->op_flags & OPf_STACKED ? sp[-1] : PAD_SV(PL_op->op_targ))
79072805
LW
85#define dATARGET SV * GETATARGET
86
87#define dTARG SV *targ
88
533c011a 89#define NORMAL PL_op->op_next
cea2e8a9 90#define DIE return Perl_die
5df617be 91#define DIE_NULL return DieNull
79072805 92
954c1994
GS
93/*
94=for apidoc Ams||PUTBACK
95Closing bracket for XSUB arguments. This is usually handled by C<xsubpp>.
96See C<PUSHMARK> and L<perlcall> for other uses.
97
98=for apidoc Amn|SV*|POPs
99Pops an SV off the stack.
100
101=for apidoc Amn|char*|POPp
595ae481
NIS
102Pops a string off the stack. Deprecated. New code should provide
103a STRLEN n_a and use POPpx.
104
105=for apidoc Amn|char*|POPpx
954c1994 106Pops a string off the stack.
595ae481
NIS
107Requires a variable STRLEN n_a in scope.
108
109=for apidoc Amn|char*|POPpbytex
110Pops a string off the stack which must consist of bytes i.e. characters < 256.
111Requires a variable STRLEN n_a in scope.
954c1994
GS
112
113=for apidoc Amn|NV|POPn
114Pops a double off the stack.
115
116=for apidoc Amn|IV|POPi
117Pops an integer off the stack.
118
119=for apidoc Amn|long|POPl
120Pops a long off the stack.
121
122=cut
123*/
124
3280af22 125#define PUTBACK PL_stack_sp = sp
79072805
LW
126#define RETURN return PUTBACK, NORMAL
127#define RETURNOP(o) return PUTBACK, o
128#define RETURNX(x) return x, PUTBACK, NORMAL
129
130#define POPs (*sp--)
2d8e6c8d
GS
131#define POPp (SvPVx(POPs, PL_na)) /* deprecated */
132#define POPpx (SvPVx(POPs, n_a))
595ae481 133#define POPpbytex (SvPVbytex(POPs, n_a))
463ee0b2 134#define POPn (SvNVx(POPs))
a0d0e21e 135#define POPi ((IV)SvIVx(POPs))
ff68c719 136#define POPu ((UV)SvUVx(POPs))
463ee0b2 137#define POPl ((long)SvIVx(POPs))
d9b3e12d 138#define POPul ((unsigned long)SvIVx(POPs))
1b8cd678 139#ifdef HAS_QUAD
d9b3e12d 140#define POPq ((Quad_t)SvIVx(POPs))
c5a0f51a 141#define POPuq ((Uquad_t)SvUVx(POPs))
d9b3e12d 142#endif
79072805
LW
143
144#define TOPs (*sp)
56370e9f 145#define TOPm1s (*(sp-1))
7dca457a 146#define TOPp1s (*(sp+1))
2d8e6c8d
GS
147#define TOPp (SvPV(TOPs, PL_na)) /* deprecated */
148#define TOPpx (SvPV(TOPs, n_a))
463ee0b2 149#define TOPn (SvNV(TOPs))
a0d0e21e 150#define TOPi ((IV)SvIV(TOPs))
ff68c719 151#define TOPu ((UV)SvUV(TOPs))
463ee0b2 152#define TOPl ((long)SvIV(TOPs))
c5a0f51a 153#define TOPul ((unsigned long)SvUV(TOPs))
1b8cd678 154#ifdef HAS_QUAD
d9b3e12d 155#define TOPq ((Quad_t)SvIV(TOPs))
c5a0f51a 156#define TOPuq ((Uquad_t)SvUV(TOPs))
d9b3e12d 157#endif
79072805
LW
158
159/* Go to some pains in the rare event that we must extend the stack. */
954c1994
GS
160
161/*
162=for apidoc Am|void|EXTEND|SP|int nitems
163Used to extend the argument stack for an XSUB's return values. Once
4375e838 164used, guarantees that there is room for at least C<nitems> to be pushed
954c1994
GS
165onto the stack.
166
167=for apidoc Am|void|PUSHs|SV* sv
aacdac46 168Push an SV onto the stack. The stack must have room for this element.
d82b684c
SH
169Does not handle 'set' magic. Does not use C<TARG>. See also C<PUSHmortal>,
170C<XPUSHs> and C<XPUSHmortal>.
954c1994
GS
171
172=for apidoc Am|void|PUSHp|char* str|STRLEN len
173Push a string onto the stack. The stack must have room for this element.
d82b684c
SH
174The C<len> indicates the length of the string. Handles 'set' magic. Uses
175C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to declare it. Do not
176call multiple C<TARG>-oriented macros to return lists from XSUB's - see
177C<mPUSHp> instead. See also C<XPUSHp> and C<mXPUSHp>.
954c1994
GS
178
179=for apidoc Am|void|PUSHn|NV nv
180Push a double onto the stack. The stack must have room for this element.
d82b684c
SH
181Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
182called to declare it. Do not call multiple C<TARG>-oriented macros to
183return lists from XSUB's - see C<mPUSHn> instead. See also C<XPUSHn> and
184C<mXPUSHn>.
954c1994
GS
185
186=for apidoc Am|void|PUSHi|IV iv
187Push an integer onto the stack. The stack must have room for this element.
d82b684c
SH
188Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
189called to declare it. Do not call multiple C<TARG>-oriented macros to
190return lists from XSUB's - see C<mPUSHi> instead. See also C<XPUSHi> and
191C<mXPUSHi>.
954c1994
GS
192
193=for apidoc Am|void|PUSHu|UV uv
194Push an unsigned integer onto the stack. The stack must have room for this
d82b684c
SH
195element. Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG>
196should be called to declare it. Do not call multiple C<TARG>-oriented
197macros to return lists from XSUB's - see C<mPUSHu> instead. See also
198C<XPUSHu> and C<mXPUSHu>.
954c1994
GS
199
200=for apidoc Am|void|XPUSHs|SV* sv
201Push an SV onto the stack, extending the stack if necessary. Does not
d82b684c
SH
202handle 'set' magic. Does not use C<TARG>. See also C<XPUSHmortal>,
203C<PUSHs> and C<PUSHmortal>.
954c1994
GS
204
205=for apidoc Am|void|XPUSHp|char* str|STRLEN len
206Push a string onto the stack, extending the stack if necessary. The C<len>
d82b684c
SH
207indicates the length of the string. Handles 'set' magic. Uses C<TARG>, so
208C<dTARGET> or C<dXSTARG> should be called to declare it. Do not call
209multiple C<TARG>-oriented macros to return lists from XSUB's - see
210C<mXPUSHp> instead. See also C<PUSHp> and C<mPUSHp>.
954c1994
GS
211
212=for apidoc Am|void|XPUSHn|NV nv
213Push a double onto the stack, extending the stack if necessary. Handles
d82b684c
SH
214'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
215declare it. Do not call multiple C<TARG>-oriented macros to return lists
216from XSUB's - see C<mXPUSHn> instead. See also C<PUSHn> and C<mPUSHn>.
954c1994
GS
217
218=for apidoc Am|void|XPUSHi|IV iv
219Push an integer onto the stack, extending the stack if necessary. Handles
d82b684c
SH
220'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be called to
221declare it. Do not call multiple C<TARG>-oriented macros to return lists
222from XSUB's - see C<mXPUSHi> instead. See also C<PUSHi> and C<mPUSHi>.
954c1994
GS
223
224=for apidoc Am|void|XPUSHu|UV uv
aacdac46 225Push an unsigned integer onto the stack, extending the stack if necessary.
d82b684c
SH
226Handles 'set' magic. Uses C<TARG>, so C<dTARGET> or C<dXSTARG> should be
227called to declare it. Do not call multiple C<TARG>-oriented macros to
228return lists from XSUB's - see C<mXPUSHu> instead. See also C<PUSHu> and
229C<mPUSHu>.
230
231=for apidoc Am|void|PUSHmortal
232Push a new mortal SV onto the stack. The stack must have room for this
233element. Does not handle 'set' magic. Does not use C<TARG>. See also
234C<PUSHs>, C<XPUSHmortal> and C<XPUSHs>.
235
236=for apidoc Am|void|mPUSHp|char* str|STRLEN len
237Push a string onto the stack. The stack must have room for this element.
de4f2208
RGS
238The C<len> indicates the length of the string. Handles 'set' magic. Does
239not use C<TARG>. See also C<PUSHp>, C<mXPUSHp> and C<XPUSHp>.
d82b684c
SH
240
241=for apidoc Am|void|mPUSHn|NV nv
242Push a double onto the stack. The stack must have room for this element.
de4f2208
RGS
243Handles 'set' magic. Does not use C<TARG>. See also C<PUSHn>, C<mXPUSHn>
244and C<XPUSHn>.
d82b684c
SH
245
246=for apidoc Am|void|mPUSHi|IV iv
247Push an integer onto the stack. The stack must have room for this element.
de4f2208
RGS
248Handles 'set' magic. Does not use C<TARG>. See also C<PUSHi>, C<mXPUSHi>
249and C<XPUSHi>.
d82b684c
SH
250
251=for apidoc Am|void|mPUSHu|UV uv
252Push an unsigned integer onto the stack. The stack must have room for this
de4f2208
RGS
253element. Handles 'set' magic. Does not use C<TARG>. See also C<PUSHu>,
254C<mXPUSHu> and C<XPUSHu>.
d82b684c
SH
255
256=for apidoc Am|void|XPUSHmortal
257Push a new mortal SV onto the stack, extending the stack if necessary. Does
258not handle 'set' magic. Does not use C<TARG>. See also C<XPUSHs>,
259C<PUSHmortal> and C<PUSHs>.
260
261=for apidoc Am|void|mXPUSHp|char* str|STRLEN len
262Push a string onto the stack, extending the stack if necessary. The C<len>
de4f2208
RGS
263indicates the length of the string. Handles 'set' magic. Does not use
264C<TARG>. See also C<XPUSHp>, C<mPUSHp> and C<PUSHp>.
d82b684c
SH
265
266=for apidoc Am|void|mXPUSHn|NV nv
de4f2208
RGS
267Push a double onto the stack, extending the stack if necessary. Handles
268'set' magic. Does not use C<TARG>. See also C<XPUSHn>, C<mPUSHn> and
269C<PUSHn>.
d82b684c
SH
270
271=for apidoc Am|void|mXPUSHi|IV iv
de4f2208
RGS
272Push an integer onto the stack, extending the stack if necessary. Handles
273'set' magic. Does not use C<TARG>. See also C<XPUSHi>, C<mPUSHi> and
274C<PUSHi>.
d82b684c
SH
275
276=for apidoc Am|void|mXPUSHu|UV uv
277Push an unsigned integer onto the stack, extending the stack if necessary.
de4f2208
RGS
278Handles 'set' magic. Does not use C<TARG>. See also C<XPUSHu>, C<mPUSHu>
279and C<PUSHu>.
954c1994
GS
280
281=cut
282*/
283
c5661c80 284#define EXTEND(p,n) STMT_START { if (PL_stack_max - p < (int)(n)) { \
bbce6d69 285 sp = stack_grow(sp,p, (int) (n)); \
86547924 286 } } STMT_END
a0d0e21e 287
79072805 288/* Same thing, but update mark register too. */
c5661c80 289#define MEXTEND(p,n) STMT_START {if (PL_stack_max - p < (int)(n)) { \
3280af22 290 int markoff = mark - PL_stack_base; \
bbce6d69 291 sp = stack_grow(sp,p,(int) (n)); \
3280af22 292 mark = PL_stack_base + markoff; \
86547924 293 } } STMT_END
79072805
LW
294
295#define PUSHs(s) (*++sp = (s))
86547924 296#define PUSHTARG STMT_START { SvSETMAGIC(TARG); PUSHs(TARG); } STMT_END
297#define PUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); PUSHTARG; } STMT_END
65202027 298#define PUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); PUSHTARG; } STMT_END
86547924 299#define PUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); PUSHTARG; } STMT_END
55497cff 300#define PUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); PUSHTARG; } STMT_END
79072805 301
86547924 302#define XPUSHs(s) STMT_START { EXTEND(sp,1); (*++sp = (s)); } STMT_END
303#define XPUSHTARG STMT_START { SvSETMAGIC(TARG); XPUSHs(TARG); } STMT_END
304#define XPUSHp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); XPUSHTARG; } STMT_END
65202027 305#define XPUSHn(n) STMT_START { sv_setnv(TARG, (NV)(n)); XPUSHTARG; } STMT_END
86547924 306#define XPUSHi(i) STMT_START { sv_setiv(TARG, (IV)(i)); XPUSHTARG; } STMT_END
55497cff 307#define XPUSHu(u) STMT_START { sv_setuv(TARG, (UV)(u)); XPUSHTARG; } STMT_END
b162f9ea 308#define XPUSHundef STMT_START { SvOK_off(TARG); XPUSHs(TARG); } STMT_END
79072805 309
2cc7004b
MHM
310#define PUSHmortal PUSHs(sv_newmortal())
311#define mPUSHp(p,l) sv_setpvn_mg(PUSHmortal, (p), (l))
312#define mPUSHn(n) sv_setnv_mg(PUSHmortal, (NV)(n))
313#define mPUSHi(i) sv_setiv_mg(PUSHmortal, (IV)(i))
314#define mPUSHu(u) sv_setuv_mg(PUSHmortal, (UV)(u))
315
316#define XPUSHmortal XPUSHs(sv_newmortal())
317#define mXPUSHp(p,l) STMT_START { EXTEND(sp,1); sv_setpvn_mg(PUSHmortal, (p), (l)); } STMT_END
318#define mXPUSHn(n) STMT_START { EXTEND(sp,1); sv_setnv_mg(PUSHmortal, (NV)(n)); } STMT_END
319#define mXPUSHi(i) STMT_START { EXTEND(sp,1); sv_setiv_mg(PUSHmortal, (IV)(i)); } STMT_END
320#define mXPUSHu(u) STMT_START { EXTEND(sp,1); sv_setuv_mg(PUSHmortal, (UV)(u)); } STMT_END
d82b684c 321
79072805 322#define SETs(s) (*sp = s)
86547924 323#define SETTARG STMT_START { SvSETMAGIC(TARG); SETs(TARG); } STMT_END
324#define SETp(p,l) STMT_START { sv_setpvn(TARG, (p), (l)); SETTARG; } STMT_END
65202027 325#define SETn(n) STMT_START { sv_setnv(TARG, (NV)(n)); SETTARG; } STMT_END
86547924 326#define SETi(i) STMT_START { sv_setiv(TARG, (IV)(i)); SETTARG; } STMT_END
55497cff 327#define SETu(u) STMT_START { sv_setuv(TARG, (UV)(u)); SETTARG; } STMT_END
a0d0e21e 328
79072805
LW
329#define dTOPss SV *sv = TOPs
330#define dPOPss SV *sv = POPs
65202027
DS
331#define dTOPnv NV value = TOPn
332#define dPOPnv NV value = POPn
a0d0e21e
LW
333#define dTOPiv IV value = TOPi
334#define dPOPiv IV value = POPi
55497cff 335#define dTOPuv UV value = TOPu
336#define dPOPuv UV value = POPu
1b8cd678 337#ifdef HAS_QUAD
2d4389e4
JH
338#define dTOPqv Quad_t value = TOPu
339#define dPOPqv Quad_t value = POPu
340#define dTOPuqv Uquad_t value = TOPuq
341#define dPOPuqv Uquad_t value = POPuq
342#endif
79072805 343
7a4c00b4 344#define dPOPXssrl(X) SV *right = POPs; SV *left = CAT2(X,s)
65202027 345#define dPOPXnnrl(X) NV right = POPn; NV left = CAT2(X,n)
7a4c00b4 346#define dPOPXiirl(X) IV right = POPi; IV left = CAT2(X,i)
347
348#define USE_LEFT(sv) \
533c011a 349 (SvOK(sv) || SvGMAGICAL(sv) || !(PL_op->op_flags & OPf_STACKED))
7a4c00b4 350#define dPOPXnnrl_ul(X) \
65202027 351 NV right = POPn; \
7a4c00b4 352 SV *leftsv = CAT2(X,s); \
65202027 353 NV left = USE_LEFT(leftsv) ? SvNV(leftsv) : 0.0
7a4c00b4 354#define dPOPXiirl_ul(X) \
355 IV right = POPi; \
356 SV *leftsv = CAT2(X,s); \
357 IV left = USE_LEFT(leftsv) ? SvIV(leftsv) : 0
358
359#define dPOPPOPssrl dPOPXssrl(POP)
360#define dPOPPOPnnrl dPOPXnnrl(POP)
361#define dPOPPOPnnrl_ul dPOPXnnrl_ul(POP)
362#define dPOPPOPiirl dPOPXiirl(POP)
363#define dPOPPOPiirl_ul dPOPXiirl_ul(POP)
364
365#define dPOPTOPssrl dPOPXssrl(TOP)
366#define dPOPTOPnnrl dPOPXnnrl(TOP)
367#define dPOPTOPnnrl_ul dPOPXnnrl_ul(TOP)
368#define dPOPTOPiirl dPOPXiirl(TOP)
369#define dPOPTOPiirl_ul dPOPXiirl_ul(TOP)
79072805 370
3280af22
NIS
371#define RETPUSHYES RETURNX(PUSHs(&PL_sv_yes))
372#define RETPUSHNO RETURNX(PUSHs(&PL_sv_no))
373#define RETPUSHUNDEF RETURNX(PUSHs(&PL_sv_undef))
79072805 374
3280af22
NIS
375#define RETSETYES RETURNX(SETs(&PL_sv_yes))
376#define RETSETNO RETURNX(SETs(&PL_sv_no))
377#define RETSETUNDEF RETURNX(SETs(&PL_sv_undef))
79072805 378
533c011a 379#define ARGTARG PL_op->op_targ
b162f9ea
IZ
380
381 /* See OPpTARGET_MY: */
382#define MAXARG (PL_op->op_private & 15)
79072805 383
e336de0d
GS
384#define SWITCHSTACK(f,t) \
385 STMT_START { \
677b06e3 386 AvFILLp(f) = sp - PL_stack_base; \
3280af22 387 PL_stack_base = AvARRAY(t); \
677b06e3 388 PL_stack_max = PL_stack_base + AvMAX(t); \
3280af22 389 sp = PL_stack_sp = PL_stack_base + AvFILLp(t); \
677b06e3 390 PL_curstack = t; \
e336de0d 391 } STMT_END
79072805 392
bbce6d69 393#define EXTEND_MORTAL(n) \
677b06e3
GS
394 STMT_START { \
395 if (PL_tmps_ix + (n) >= PL_tmps_max) \
396 tmps_grow(n); \
397 } STMT_END
bbce6d69 398
a0d0e21e
LW
399#define AMGf_noright 1
400#define AMGf_noleft 2
401#define AMGf_assign 4
402#define AMGf_unary 8
403
86547924 404#define tryAMAGICbinW(meth,assign,set) STMT_START { \
3280af22 405 if (PL_amagic_generation) { \
a0d0e21e
LW
406 SV* tmpsv; \
407 SV* right= *(sp); SV* left= *(sp-1);\
408 if ((SvAMAGIC(left)||SvAMAGIC(right))&&\
409 (tmpsv=amagic_call(left, \
410 right, \
411 CAT2(meth,_amg), \
412 (assign)? AMGf_assign: 0))) {\
413 SPAGAIN; \
414 (void)POPs; set(tmpsv); RETURN; } \
415 } \
86547924 416 } STMT_END
a0d0e21e
LW
417
418#define tryAMAGICbin(meth,assign) tryAMAGICbinW(meth,assign,SETsv)
419#define tryAMAGICbinSET(meth,assign) tryAMAGICbinW(meth,assign,SETs)
420
3280af22 421#define AMG_CALLun(sv,meth) amagic_call(sv,&PL_sv_undef, \
a0d0e21e
LW
422 CAT2(meth,_amg),AMGf_noright | AMGf_unary)
423#define AMG_CALLbinL(left,right,meth) \
424 amagic_call(left,right,CAT2(meth,_amg),AMGf_noright)
425
e8c20960 426#define tryAMAGICunW(meth,set,shift,ret) STMT_START { \
3280af22 427 if (PL_amagic_generation) { \
a0d0e21e 428 SV* tmpsv; \
f5284f61 429 SV* arg= sp[shift]; \
c4459e09 430 if(0) goto am_again; /* shut up unused warning */ \
f5284f61 431 am_again: \
a0d0e21e
LW
432 if ((SvAMAGIC(arg))&&\
433 (tmpsv=AMG_CALLun(arg,meth))) {\
f5284f61 434 SPAGAIN; if (shift) sp += shift; \
e8c20960 435 set(tmpsv); ret; } \
a0d0e21e 436 } \
86547924 437 } STMT_END
a0d0e21e 438
f5284f61
IZ
439#define FORCE_SETs(sv) STMT_START { sv_setsv(TARG, (sv)); SETTARG; } STMT_END
440
b162f9ea
IZ
441#define tryAMAGICun(meth) tryAMAGICunW(meth,SETsvUN,0,RETURN)
442#define tryAMAGICunSET(meth) tryAMAGICunW(meth,SETs,0,RETURN)
f5284f61 443#define tryAMAGICunTARGET(meth, shift) \
16e7e110 444 STMT_START { dSP; sp--; /* get TARGET from below PL_stack_sp */ \
f5284f61 445 { dTARGETSTACKED; \
16e7e110
TH
446 { dSP; tryAMAGICunW(meth,FORCE_SETs,shift,RETURN);}}} STMT_END
447
448#define setAGAIN(ref) \
449 STMT_START { \
450 sv = ref; \
451 if (!SvROK(ref)) \
452 Perl_croak(aTHX_ "Overloaded dereference did not return a reference"); \
453 if (ref != arg && SvRV(ref) != SvRV(arg)) { \
454 arg = ref; \
455 goto am_again; \
456 } \
457 } STMT_END
e8c20960 458
0f4592ef 459#define tryAMAGICunDEREF(meth) tryAMAGICunW(meth,setAGAIN,0,(void)0)
a0d0e21e 460
533c011a 461#define opASSIGN (PL_op->op_flags & OPf_STACKED)
e9905555 462#define SETsv(sv) STMT_START { \
b162f9ea
IZ
463 if (opASSIGN || (SvFLAGS(TARG) & SVs_PADMY)) \
464 { sv_setsv(TARG, (sv)); SETTARG; } \
465 else SETs(sv); } STMT_END
466
467#define SETsvUN(sv) STMT_START { \
468 if (SvFLAGS(TARG) & SVs_PADMY) \
469 { sv_setsv(TARG, (sv)); SETTARG; } \
e9905555 470 else SETs(sv); } STMT_END
a0d0e21e
LW
471
472/* newSVsv does not behave as advertised, so we copy missing
473 * information by hand */
474
76e3520e
GS
475/* SV* ref causes confusion with the member variable
476 changed SV* ref to SV* tmpRef */
477#define RvDEEPCP(rv) STMT_START { SV* tmpRef=SvRV(rv); \
478 if (SvREFCNT(tmpRef)>1) { \
29ddfe35 479 SvRV(rv)=AMG_CALLun(rv,copy); \
76e3520e 480 SvREFCNT_dec(tmpRef); \
86547924 481 } } STMT_END
78f9721b
SM
482
483/*
484=for apidoc mU||LVRET
485True if this op will be the return value of an lvalue subroutine
486
487=cut */
9d96b2e7 488#define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())