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