This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Prefer -std=c89 over -ansi.
[perl5.git] / op.h
1 /*    op.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 /*
12  * The fields of BASEOP are:
13  *      op_next         Pointer to next ppcode to execute after this one.
14  *                      (Top level pre-grafted op points to first op,
15  *                      but this is replaced when op is grafted in, when
16  *                      this op will point to the real next op, and the new
17  *                      parent takes over role of remembering starting op.)
18  *      op_ppaddr       Pointer to current ppcode's function.
19  *      op_type         The type of the operation.
20  *      op_opt          Whether or not the op has been optimised by the
21  *                      peephole optimiser.
22  *      op_slabbed      allocated via opslab
23  *      op_static       tell op_free() to skip PerlMemShared_free(), when
24  *                      !op_slabbed.
25  *      op_savefree     on savestack via SAVEFREEOP
26  *      op_folded       Result/remainder of a constant fold operation.
27  *      op_lastsib      this op is is the last sibling
28  *      op_spare        One spare bit
29  *      op_flags        Flags common to all operations.  See OPf_* below.
30  *      op_private      Flags peculiar to a particular operation (BUT,
31  *                      by default, set to the number of children until
32  *                      the operation is privatized by a check routine,
33  *                      which may or may not check number of children).
34  */
35 #include "op_reg_common.h"
36
37 #define OPCODE U16
38
39 typedef PERL_BITFIELD16 Optype;
40
41 #ifdef BASEOP_DEFINITION
42 #define BASEOP BASEOP_DEFINITION
43 #else
44 #define BASEOP                          \
45     OP*         op_next;                \
46     OP*         op_sibling;             \
47     OP*         (*op_ppaddr)(pTHX);     \
48     PADOFFSET   op_targ;                \
49     PERL_BITFIELD16 op_type:9;          \
50     PERL_BITFIELD16 op_opt:1;           \
51     PERL_BITFIELD16 op_slabbed:1;       \
52     PERL_BITFIELD16 op_savefree:1;      \
53     PERL_BITFIELD16 op_static:1;        \
54     PERL_BITFIELD16 op_folded:1;        \
55     PERL_BITFIELD16 op_lastsib:1;       \
56     PERL_BITFIELD16 op_spare:1;         \
57     U8          op_flags;               \
58     U8          op_private;
59 #endif
60
61 /* If op_type:9 is changed to :10, also change PUSHEVAL in cop.h.
62    Also, if the type of op_type is ever changed (e.g. to PERL_BITFIELD32)
63    then all the other bit-fields before/after it should change their
64    types too to let VC pack them into the same 4 byte integer.*/
65
66 /* for efficiency, requires OPf_WANT_VOID == G_VOID etc */
67 #define OP_GIMME(op,dfl) \
68         (((op)->op_flags & OPf_WANT) ? ((op)->op_flags & OPf_WANT) : dfl)
69
70 #define OP_GIMME_REVERSE(flags) ((flags) & G_WANT)
71
72 /*
73 =head1 "Gimme" Values
74
75 =for apidoc Amn|U32|GIMME_V
76 The XSUB-writer's equivalent to Perl's C<wantarray>.  Returns C<G_VOID>,
77 C<G_SCALAR> or C<G_ARRAY> for void, scalar or list context,
78 respectively.  See L<perlcall> for a usage example.
79
80 =for apidoc Amn|U32|GIMME
81 A backward-compatible version of C<GIMME_V> which can only return
82 C<G_SCALAR> or C<G_ARRAY>; in a void context, it returns C<G_SCALAR>.
83 Deprecated.  Use C<GIMME_V> instead.
84
85 =cut
86 */
87
88 #define GIMME_V         OP_GIMME(PL_op, block_gimme())
89
90 /* Public flags */
91
92 #define OPf_WANT        3       /* Mask for "want" bits: */
93 #define  OPf_WANT_VOID   1      /*   Want nothing */
94 #define  OPf_WANT_SCALAR 2      /*   Want single value */
95 #define  OPf_WANT_LIST   3      /*   Want list of any length */
96 #define OPf_KIDS        4       /* There is a firstborn child. */
97 #define OPf_PARENS      8       /* This operator was parenthesized. */
98                                 /*  (Or block needs explicit scope entry.) */
99 #define OPf_REF         16      /* Certified reference. */
100                                 /*  (Return container, not containee). */
101 #define OPf_MOD         32      /* Will modify (lvalue). */
102 #define OPf_STACKED     64      /* Some arg is arriving on the stack. */
103 #define OPf_SPECIAL     128     /* Do something weird for this op: */
104                                 /*  On local LVAL, don't init local value. */
105                                 /*  On OP_SORT, subroutine is inlined. */
106                                 /*  On OP_NOT, inversion was implicit. */
107                                 /*  On OP_LEAVE, don't restore curpm. */
108                                 /*  On truncate, we truncate filehandle */
109                                 /*  On control verbs, we saw no label */
110                                 /*  On flipflop, we saw ... instead of .. */
111                                 /*  On UNOPs, saw bare parens, e.g. eof(). */
112                                 /*  On OP_NULL, saw a "do". */
113                                 /*  On OP_EXISTS, treat av as av, not avhv.  */
114                                 /*  On OP_(ENTER|LEAVE)EVAL, don't clear $@ */
115                                 /*  On pushre, rx is used as part of split, e.g. split " " */
116                                 /*  On regcomp, "use re 'eval'" was in scope */
117                                 /*  On RV2[ACGHS]V, don't create GV--in
118                                     defined()*/
119                                 /*  On OP_DBSTATE, indicates breakpoint
120                                  *    (runtime property) */
121                                 /*  On OP_REQUIRE, was seen as CORE::require */
122                                 /*  On OP_(ENTER|LEAVE)WHEN, there's
123                                     no condition */
124                                 /*  On OP_SMARTMATCH, an implicit smartmatch */
125                                 /*  On OP_ANONHASH and OP_ANONLIST, create a
126                                     reference to the new anon hash or array */
127                                 /*  On OP_HELEM and OP_HSLICE, localization will be followed
128                                     by assignment, so do not wipe the target if it is special
129                                     (e.g. a glob or a magic SV) */
130                                 /*  On OP_MATCH, OP_SUBST & OP_TRANS, the
131                                     operand of a logical or conditional
132                                     that was optimised away, so it should
133                                     not be bound via =~ */
134                                 /*  On OP_CONST, from a constant CV */
135                                 /*  On OP_GLOB, two meanings:
136                                     - Before ck_glob, called as CORE::glob
137                                     - After ck_glob, use Perl glob function
138                                  */
139                                 /*  On OP_PADRANGE, push @_ */
140                                 /*  On OP_DUMP, has no label */
141                                 /*  On OP_UNSTACK, in a foreach loop */
142
143 /* old names; don't use in new code, but don't break them, either */
144 #define OPf_LIST        OPf_WANT_LIST
145 #define OPf_KNOW        OPf_WANT
146
147 #define GIMME \
148           (PL_op->op_flags & OPf_WANT                                   \
149            ? ((PL_op->op_flags & OPf_WANT) == OPf_WANT_LIST             \
150               ? G_ARRAY                                                 \
151               : G_SCALAR)                                               \
152            : dowantarray())
153
154
155 /* NOTE: OPp* flags are now auto-generated and defined in opcode.h,
156  *       from data in regen/op_private */
157
158
159 #define OPpTRANS_ALL    (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF|OPpTRANS_IDENTICAL|OPpTRANS_SQUASH|OPpTRANS_COMPLEMENT|OPpTRANS_GROWS|OPpTRANS_DELETE)
160
161
162
163 /* Mask for OP_ENTERSUB flags, the absence of which must be propagated
164  in dynamic context */
165 #define OPpENTERSUB_LVAL_MASK (OPpLVAL_INTRO|OPpENTERSUB_INARGS)
166
167
168
169 struct op {
170     BASEOP
171 };
172
173 struct unop {
174     BASEOP
175     OP *        op_first;
176 };
177
178 struct binop {
179     BASEOP
180     OP *        op_first;
181     OP *        op_last;
182 };
183
184 struct logop {
185     BASEOP
186     OP *        op_first;
187     OP *        op_other;
188 };
189
190 struct listop {
191     BASEOP
192     OP *        op_first;
193     OP *        op_last;
194 };
195
196 struct methop {
197     BASEOP
198     union {
199         /* op_u.op_first *must* be aligned the same as the op_first
200          * field of the other op types, and op_u.op_meth_sv *must*
201          * be aligned with op_sv */
202         OP* op_first;   /* optree for method name */
203         SV* op_meth_sv; /* static method name */
204     } op_u;
205 #ifdef USE_ITHREADS
206     PADOFFSET op_class_targ; /* pad index for class name if threaded */
207 #else
208     SV*       op_class_sv;   /* static class name */
209 #endif
210 };
211
212 struct pmop {
213     BASEOP
214     OP *        op_first;
215     OP *        op_last;
216 #ifdef USE_ITHREADS
217     PADOFFSET   op_pmoffset;
218 #else
219     REGEXP *    op_pmregexp;            /* compiled expression */
220 #endif
221     U32         op_pmflags;
222     union {
223         OP *    op_pmreplroot;          /* For OP_SUBST */
224 #ifdef USE_ITHREADS
225         PADOFFSET  op_pmtargetoff;      /* For OP_PUSHRE */
226 #else
227         GV *    op_pmtargetgv;
228 #endif
229     }   op_pmreplrootu;
230     union {
231         OP *    op_pmreplstart; /* Only used in OP_SUBST */
232 #ifdef USE_ITHREADS
233         PADOFFSET op_pmstashoff; /* Only used in OP_MATCH, with PMf_ONCE set */
234 #else
235         HV *    op_pmstash;
236 #endif
237     }           op_pmstashstartu;
238     OP *        op_code_list;   /* list of (?{}) code blocks */
239 };
240
241 #ifdef USE_ITHREADS
242 #define PM_GETRE(o)     (SvTYPE(PL_regex_pad[(o)->op_pmoffset]) == SVt_REGEXP \
243                          ? (REGEXP*)(PL_regex_pad[(o)->op_pmoffset]) : NULL)
244 /* The assignment is just to enforce type safety (or at least get a warning).
245  */
246 /* With first class regexps not via a reference one needs to assign
247    &PL_sv_undef under ithreads. (This would probably work unthreaded, but NULL
248    is cheaper. I guess we could allow NULL, but the check above would get
249    more complex, and we'd have an AV with (SV*)NULL in it, which feels bad */
250 /* BEWARE - something that calls this macro passes (r) which has a side
251    effect.  */
252 #define PM_SETRE(o,r)   STMT_START {                                    \
253                             REGEXP *const _pm_setre = (r);              \
254                             assert(_pm_setre);                          \
255                             PL_regex_pad[(o)->op_pmoffset] = MUTABLE_SV(_pm_setre); \
256                         } STMT_END
257 #else
258 #define PM_GETRE(o)     ((o)->op_pmregexp)
259 #define PM_SETRE(o,r)   ((o)->op_pmregexp = (r))
260 #endif
261
262 /* Currently these PMf flags occupy a single 32-bit word.  Not all bits are
263  * currently used.  The lower bits are shared with their corresponding RXf flag
264  * bits, up to but not including _RXf_PMf_SHIFT_NEXT.  The unused bits
265  * immediately follow; finally the used Pmf-only (unshared) bits, so that the
266  * highest bit in the word is used.  This gathers all the unused bits as a pool
267  * in the middle, like so: 11111111111111110000001111111111
268  * where the '1's represent used bits, and the '0's unused.  This design allows
269  * us to allocate off one end of the pool if we need to add a shared bit, and
270  * off the other end if we need a non-shared bit, without disturbing the other
271  * bits.  This maximizes the likelihood of being able to change things without
272  * breaking binary compatibility.
273  *
274  * To add shared bits, do so in op_reg_common.h.  This should change
275  * _RXf_PMf_SHIFT_NEXT so that things won't compile.  Then come to regexp.h and
276  * op.h and adjust the constant adders in the definitions of PMf_BASE_SHIFT and
277  * Pmf_BASE_SHIFT down by the number of shared bits you added.  That's it.
278  * Things should be binary compatible.  But if either of these gets to having
279  * to subtract rather than add, leave at 0 and adjust all the entries below
280  * that are in terms of this according.  But if the first one of those is
281  * already PMf_BASE_SHIFT+0, there are no bits left, and a redesign is in
282  * order.
283  *
284  * To remove unshared bits, just delete its entry.  If you're where breaking
285  * binary compatibility is ok to do, you might want to adjust things to move
286  * the newly opened space so that it gets absorbed into the common pool.
287  *
288  * To add unshared bits, first use up any gaps in the middle.  Otherwise,
289  * allocate off the low end until you get to PMf_BASE_SHIFT+0.  If that isn't
290  * enough, move PMf_BASE_SHIFT down (if possible) and add the new bit at the
291  * other end instead; this preserves binary compatibility. */
292 #define PMf_BASE_SHIFT (_RXf_PMf_SHIFT_NEXT+4)
293
294 /* 'use re "taint"' in scope: taint $1 etc. if target tainted */
295 #define PMf_RETAINT     (1U<<(PMf_BASE_SHIFT+5))
296
297 /* match successfully only once per reset, with related flag RXf_USED in
298  * re->extflags holding state.  This is used only for ?? matches, and only on
299  * OP_MATCH and OP_QR */
300 #define PMf_ONCE        (1U<<(PMf_BASE_SHIFT+6))
301
302 /* PMf_ONCE, i.e. ?pat?, has matched successfully.  Not used under threading. */
303 #define PMf_USED        (1U<<(PMf_BASE_SHIFT+7))
304
305 /* subst replacement is constant */
306 #define PMf_CONST       (1U<<(PMf_BASE_SHIFT+8))
307
308 /* keep 1st runtime pattern forever */
309 #define PMf_KEEP        (1U<<(PMf_BASE_SHIFT+9))
310
311 #define PMf_GLOBAL      (1U<<(PMf_BASE_SHIFT+10)) /* pattern had a g modifier */
312
313 /* don't reset pos() if //g fails */
314 #define PMf_CONTINUE    (1U<<(PMf_BASE_SHIFT+11))
315
316 /* evaluating replacement as expr */
317 #define PMf_EVAL        (1U<<(PMf_BASE_SHIFT+12))
318
319 /* Return substituted string instead of modifying it. */
320 #define PMf_NONDESTRUCT (1U<<(PMf_BASE_SHIFT+13))
321
322 /* the pattern has a CV attached (currently only under qr/...(?{}).../) */
323 #define PMf_HAS_CV      (1U<<(PMf_BASE_SHIFT+14))
324
325 /* op_code_list is private; don't free it etc. It may well point to
326  * code within another sub, with different pad etc */
327 #define PMf_CODELIST_PRIVATE    (1U<<(PMf_BASE_SHIFT+15))
328
329 /* the PMOP is a QR (we should be able to detect that from the op type,
330  * but the regex compilation API passes just the pm flags, not the op
331  * itself */
332 #define PMf_IS_QR       (1U<<(PMf_BASE_SHIFT+16))
333 #define PMf_USE_RE_EVAL (1U<<(PMf_BASE_SHIFT+17)) /* use re'eval' in scope */
334
335 /* See comments at the beginning of these defines about adding bits.  The
336  * highest bit position should be used, so that if PMf_BASE_SHIFT gets
337  * increased, the #error below will be triggered so that you will be reminded
338  * to adjust things at the other end to keep the bit positions unchanged */
339 #if PMf_BASE_SHIFT+17 > 31
340 #   error Too many PMf_ bits used.  See above and regnodes.h for any spare in middle
341 #endif
342
343 #ifdef USE_ITHREADS
344
345 #  define PmopSTASH(o)         ((o)->op_pmflags & PMf_ONCE                         \
346                                 ? PL_stashpad[(o)->op_pmstashstartu.op_pmstashoff]   \
347                                 : NULL)
348 #  define PmopSTASH_set(o,hv)   \
349         (assert_((o)->op_pmflags & PMf_ONCE)                            \
350          (o)->op_pmstashstartu.op_pmstashoff =                          \
351             (hv) ? alloccopstash(hv) : 0)
352 #else
353 #  define PmopSTASH(o)                                                  \
354     (((o)->op_pmflags & PMf_ONCE) ? (o)->op_pmstashstartu.op_pmstash : NULL)
355 #  if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
356 #    define PmopSTASH_set(o,hv)         ({                              \
357         assert((o)->op_pmflags & PMf_ONCE);                             \
358         ((o)->op_pmstashstartu.op_pmstash = (hv));                      \
359     })
360 #  else
361 #    define PmopSTASH_set(o,hv) ((o)->op_pmstashstartu.op_pmstash = (hv))
362 #  endif
363 #endif
364 #define PmopSTASHPV(o)  (PmopSTASH(o) ? HvNAME_get(PmopSTASH(o)) : NULL)
365    /* op_pmstashstartu.op_pmstash is not refcounted */
366 #define PmopSTASHPV_set(o,pv)   PmopSTASH_set((o), gv_stashpv(pv,GV_ADD))
367
368 struct svop {
369     BASEOP
370     SV *        op_sv;
371 };
372
373 struct padop {
374     BASEOP
375     PADOFFSET   op_padix;
376 };
377
378 struct pvop {
379     BASEOP
380     char *      op_pv;
381 };
382
383 struct loop {
384     BASEOP
385     OP *        op_first;
386     OP *        op_last;
387     OP *        op_redoop;
388     OP *        op_nextop;
389     OP *        op_lastop;
390 };
391
392 #define cUNOPx(o)       ((UNOP*)o)
393 #define cBINOPx(o)      ((BINOP*)o)
394 #define cLISTOPx(o)     ((LISTOP*)o)
395 #define cLOGOPx(o)      ((LOGOP*)o)
396 #define cPMOPx(o)       ((PMOP*)o)
397 #define cSVOPx(o)       ((SVOP*)o)
398 #define cPADOPx(o)      ((PADOP*)o)
399 #define cPVOPx(o)       ((PVOP*)o)
400 #define cCOPx(o)        ((COP*)o)
401 #define cLOOPx(o)       ((LOOP*)o)
402 #define cMETHOPx(o)     ((METHOP*)o)
403
404 #define cUNOP           cUNOPx(PL_op)
405 #define cBINOP          cBINOPx(PL_op)
406 #define cLISTOP         cLISTOPx(PL_op)
407 #define cLOGOP          cLOGOPx(PL_op)
408 #define cPMOP           cPMOPx(PL_op)
409 #define cSVOP           cSVOPx(PL_op)
410 #define cPADOP          cPADOPx(PL_op)
411 #define cPVOP           cPVOPx(PL_op)
412 #define cCOP            cCOPx(PL_op)
413 #define cLOOP           cLOOPx(PL_op)
414
415 #define cUNOPo          cUNOPx(o)
416 #define cBINOPo         cBINOPx(o)
417 #define cLISTOPo        cLISTOPx(o)
418 #define cLOGOPo         cLOGOPx(o)
419 #define cPMOPo          cPMOPx(o)
420 #define cSVOPo          cSVOPx(o)
421 #define cPADOPo         cPADOPx(o)
422 #define cPVOPo          cPVOPx(o)
423 #define cCOPo           cCOPx(o)
424 #define cLOOPo          cLOOPx(o)
425
426 #define kUNOP           cUNOPx(kid)
427 #define kBINOP          cBINOPx(kid)
428 #define kLISTOP         cLISTOPx(kid)
429 #define kLOGOP          cLOGOPx(kid)
430 #define kPMOP           cPMOPx(kid)
431 #define kSVOP           cSVOPx(kid)
432 #define kPADOP          cPADOPx(kid)
433 #define kPVOP           cPVOPx(kid)
434 #define kCOP            cCOPx(kid)
435 #define kLOOP           cLOOPx(kid)
436
437
438 #ifdef USE_ITHREADS
439 #  define       cGVOPx_gv(o)    ((GV*)PAD_SVl(cPADOPx(o)->op_padix))
440 #  ifndef PERL_CORE
441 #    define     IS_PADGV(v)     (v && isGV(v))
442 #    define     IS_PADCONST(v) \
443         (v && (SvREADONLY(v) || (SvIsCOW(v) && !SvLEN(v))))
444 #  endif
445 #  define       cSVOPx_sv(v)    (cSVOPx(v)->op_sv \
446                                  ? cSVOPx(v)->op_sv : PAD_SVl((v)->op_targ))
447 #  define       cSVOPx_svp(v)   (cSVOPx(v)->op_sv \
448                                  ? &cSVOPx(v)->op_sv : &PAD_SVl((v)->op_targ))
449 #  define       cMETHOPx_class(v) (cMETHOPx(v)->op_class_targ ? \
450         PAD_SVl(cMETHOPx(v)->op_class_targ) : NULL)
451 #else
452 #  define       cGVOPx_gv(o)    ((GV*)cSVOPx(o)->op_sv)
453 #  ifndef PERL_CORE
454 #    define     IS_PADGV(v)     FALSE
455 #    define     IS_PADCONST(v)  FALSE
456 #  endif
457 #  define       cSVOPx_sv(v)    (cSVOPx(v)->op_sv)
458 #  define       cSVOPx_svp(v)   (&cSVOPx(v)->op_sv)
459 #  define       cMETHOPx_class(v) (cMETHOPx(v)->op_class_sv)
460 #endif
461
462 #  define       cMETHOPx_meth(v)        cSVOPx_sv(v)
463
464 #define cGVOP_gv                cGVOPx_gv(PL_op)
465 #define cGVOPo_gv               cGVOPx_gv(o)
466 #define kGVOP_gv                cGVOPx_gv(kid)
467 #define cSVOP_sv                cSVOPx_sv(PL_op)
468 #define cSVOPo_sv               cSVOPx_sv(o)
469 #define kSVOP_sv                cSVOPx_sv(kid)
470
471 #ifndef PERL_CORE
472 #  define Nullop ((OP*)NULL)
473 #endif
474
475 /* Lowest byte of PL_opargs */
476 #define OA_MARK 1
477 #define OA_FOLDCONST 2
478 #define OA_RETSCALAR 4
479 #define OA_TARGET 8
480 #define OA_TARGLEX 16
481 #define OA_OTHERINT 32
482 #define OA_DANGEROUS 64
483 #define OA_DEFGV 128
484
485 /* The next 4 bits (8..11) encode op class information */
486 #define OCSHIFT 8
487
488 #define OA_CLASS_MASK (15 << OCSHIFT)
489
490 #define OA_BASEOP (0 << OCSHIFT)
491 #define OA_UNOP (1 << OCSHIFT)
492 #define OA_BINOP (2 << OCSHIFT)
493 #define OA_LOGOP (3 << OCSHIFT)
494 #define OA_LISTOP (4 << OCSHIFT)
495 #define OA_PMOP (5 << OCSHIFT)
496 #define OA_SVOP (6 << OCSHIFT)
497 #define OA_PADOP (7 << OCSHIFT)
498 #define OA_PVOP_OR_SVOP (8 << OCSHIFT)
499 #define OA_LOOP (9 << OCSHIFT)
500 #define OA_COP (10 << OCSHIFT)
501 #define OA_BASEOP_OR_UNOP (11 << OCSHIFT)
502 #define OA_FILESTATOP (12 << OCSHIFT)
503 #define OA_LOOPEXOP (13 << OCSHIFT)
504 #define OA_METHOP (14 << OCSHIFT)
505
506 /* Each remaining nybble of PL_opargs (i.e. bits 12..15, 16..19 etc)
507  * encode the type for each arg */
508 #define OASHIFT 12
509
510 #define OA_SCALAR 1
511 #define OA_LIST 2
512 #define OA_AVREF 3
513 #define OA_HVREF 4
514 #define OA_CVREF 5
515 #define OA_FILEREF 6
516 #define OA_SCALARREF 7
517 #define OA_OPTIONAL 8
518
519 /* Op_REFCNT is a reference count at the head of each op tree: needed
520  * since the tree is shared between threads, and between cloned closure
521  * copies in the same thread. OP_REFCNT_LOCK/UNLOCK is used when modifying
522  * this count.
523  * The same mutex is used to protect the refcounts of the reg_trie_data
524  * and reg_ac_data structures, which are shared between duplicated
525  * regexes.
526  */
527
528 #ifdef USE_ITHREADS
529 #  define OP_REFCNT_INIT                MUTEX_INIT(&PL_op_mutex)
530 #  ifdef PERL_CORE
531 #    define OP_REFCNT_LOCK              MUTEX_LOCK(&PL_op_mutex)
532 #    define OP_REFCNT_UNLOCK            MUTEX_UNLOCK(&PL_op_mutex)
533 #  else
534 #    define OP_REFCNT_LOCK              op_refcnt_lock()
535 #    define OP_REFCNT_UNLOCK            op_refcnt_unlock()
536 #  endif
537 #  define OP_REFCNT_TERM                MUTEX_DESTROY(&PL_op_mutex)
538 #else
539 #  define OP_REFCNT_INIT                NOOP
540 #  define OP_REFCNT_LOCK                NOOP
541 #  define OP_REFCNT_UNLOCK              NOOP
542 #  define OP_REFCNT_TERM                NOOP
543 #endif
544
545 #define OpREFCNT_set(o,n)               ((o)->op_targ = (n))
546 #ifdef PERL_DEBUG_READONLY_OPS
547 #  define OpREFCNT_inc(o)               Perl_op_refcnt_inc(aTHX_ o)
548 #  define OpREFCNT_dec(o)               Perl_op_refcnt_dec(aTHX_ o)
549 #else
550 #  define OpREFCNT_inc(o)               ((o) ? (++(o)->op_targ, (o)) : NULL)
551 #  define OpREFCNT_dec(o)               (--(o)->op_targ)
552 #endif
553
554 /* flags used by Perl_load_module() */
555 #define PERL_LOADMOD_DENY               0x1     /* no Module */
556 #define PERL_LOADMOD_NOIMPORT           0x2     /* use Module () */
557 #define PERL_LOADMOD_IMPORT_OPS         0x4     /* import arguments
558                                                    are passed as a sin-
559                                                    gle op tree, not a
560                                                    list of SVs */
561
562 #if defined(PERL_IN_PERLY_C) || defined(PERL_IN_OP_C) || defined(PERL_IN_TOKE_C)
563 #define ref(o, type) doref(o, type, TRUE)
564 #endif
565
566 /*
567 =head1 Optree Manipulation Functions
568
569 =for apidoc Am|OP*|LINKLIST|OP *o
570 Given the root of an optree, link the tree in execution order using the
571 C<op_next> pointers and return the first op executed.  If this has
572 already been done, it will not be redone, and C<< o->op_next >> will be
573 returned.  If C<< o->op_next >> is not already set, I<o> should be at
574 least an C<UNOP>.
575
576 =cut
577 */
578
579 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : op_linklist((OP*)o))
580
581 /* no longer used anywhere in core */
582 #ifndef PERL_CORE
583 #define cv_ckproto(cv, gv, p) \
584    cv_ckproto_len_flags((cv), (gv), (p), (p) ? strlen(p) : 0, 0)
585 #endif
586
587 #ifdef PERL_CORE
588 #  define my(o) my_attrs((o), NULL)
589 #endif
590
591 #ifdef USE_REENTRANT_API
592 #include "reentr.h"
593 #endif
594
595 #define NewOp(m,var,c,type)     \
596         (var = (type *) Perl_Slab_Alloc(aTHX_ c*sizeof(type)))
597 #define NewOpSz(m,var,size)     \
598         (var = (OP *) Perl_Slab_Alloc(aTHX_ size))
599 #define FreeOp(p) Perl_Slab_Free(aTHX_ p)
600
601 /*
602  * The per-CV op slabs consist of a header (the opslab struct) and a bunch
603  * of space for allocating op slots, each of which consists of two pointers
604  * followed by an op.  The first pointer points to the next op slot.  The
605  * second points to the slab.  At the end of the slab is a null pointer,
606  * so that slot->opslot_next - slot can be used to determine the size
607  * of the op.
608  *
609  * Each CV can have multiple slabs; opslab_next points to the next slab, to
610  * form a chain.  All bookkeeping is done on the first slab, which is where
611  * all the op slots point.
612  *
613  * Freed ops are marked as freed and attached to the freed chain
614  * via op_next pointers.
615  *
616  * When there is more than one slab, the second slab in the slab chain is
617  * assumed to be the one with free space available.  It is used when allo-
618  * cating an op if there are no freed ops available or big enough.
619  */
620
621 #ifdef PERL_CORE
622 struct opslot {
623     /* keep opslot_next first */
624     OPSLOT *    opslot_next;            /* next slot */
625     OPSLAB *    opslot_slab;            /* owner */
626     OP          opslot_op;              /* the op itself */
627 };
628
629 struct opslab {
630     OPSLOT *    opslab_first;           /* first op in this slab */
631     OPSLAB *    opslab_next;            /* next slab */
632     OP *        opslab_freed;           /* chain of freed ops */
633     size_t      opslab_refcnt;          /* number of ops */
634 # ifdef PERL_DEBUG_READONLY_OPS
635     U16         opslab_size;            /* size of slab in pointers */
636     bool        opslab_readonly;
637 # endif
638     OPSLOT      opslab_slots;           /* slots begin here */
639 };
640
641 # define OPSLOT_HEADER          STRUCT_OFFSET(OPSLOT, opslot_op)
642 # define OPSLOT_HEADER_P        (OPSLOT_HEADER/sizeof(I32 *))
643 # define OpSLOT(o)              (assert_(o->op_slabbed) \
644                                  (OPSLOT *)(((char *)o)-OPSLOT_HEADER))
645 # define OpSLAB(o)              OpSLOT(o)->opslot_slab
646 # define OpslabREFCNT_dec(slab)      \
647         (((slab)->opslab_refcnt == 1) \
648          ? opslab_free_nopad(slab)     \
649          : (void)--(slab)->opslab_refcnt)
650   /* Variant that does not null out the pads */
651 # define OpslabREFCNT_dec_padok(slab) \
652         (((slab)->opslab_refcnt == 1)  \
653          ? opslab_free(slab)            \
654          : (void)--(slab)->opslab_refcnt)
655 #endif
656
657 struct block_hooks {
658     U32     bhk_flags;
659     void    (*bhk_start)        (pTHX_ int full);
660     void    (*bhk_pre_end)      (pTHX_ OP **seq);
661     void    (*bhk_post_end)     (pTHX_ OP **seq);
662     void    (*bhk_eval)         (pTHX_ OP *const saveop);
663 };
664
665 /*
666 =head1 Compile-time scope hooks
667
668 =for apidoc mx|U32|BhkFLAGS|BHK *hk
669 Return the BHK's flags.
670
671 =for apidoc mx|void *|BhkENTRY|BHK *hk|which
672 Return an entry from the BHK structure.  I<which> is a preprocessor token
673 indicating which entry to return.  If the appropriate flag is not set
674 this will return NULL.  The type of the return value depends on which
675 entry you ask for.
676
677 =for apidoc Amx|void|BhkENTRY_set|BHK *hk|which|void *ptr
678 Set an entry in the BHK structure, and set the flags to indicate it is
679 valid.  I<which> is a preprocessing token indicating which entry to set.
680 The type of I<ptr> depends on the entry.
681
682 =for apidoc Amx|void|BhkDISABLE|BHK *hk|which
683 Temporarily disable an entry in this BHK structure, by clearing the
684 appropriate flag.  I<which> is a preprocessor token indicating which
685 entry to disable.
686
687 =for apidoc Amx|void|BhkENABLE|BHK *hk|which
688 Re-enable an entry in this BHK structure, by setting the appropriate
689 flag.  I<which> is a preprocessor token indicating which entry to enable.
690 This will assert (under -DDEBUGGING) if the entry doesn't contain a valid
691 pointer.
692
693 =for apidoc mx|void|CALL_BLOCK_HOOKS|which|arg
694 Call all the registered block hooks for type I<which>.  I<which> is a
695 preprocessing token; the type of I<arg> depends on I<which>.
696
697 =cut
698 */
699
700 #define BhkFLAGS(hk)            ((hk)->bhk_flags)
701
702 #define BHKf_bhk_start      0x01
703 #define BHKf_bhk_pre_end    0x02
704 #define BHKf_bhk_post_end   0x04
705 #define BHKf_bhk_eval       0x08
706
707 #define BhkENTRY(hk, which) \
708     ((BhkFLAGS(hk) & BHKf_ ## which) ? ((hk)->which) : NULL)
709
710 #define BhkENABLE(hk, which) \
711     STMT_START { \
712         BhkFLAGS(hk) |= BHKf_ ## which; \
713         assert(BhkENTRY(hk, which)); \
714     } STMT_END
715
716 #define BhkDISABLE(hk, which) \
717     STMT_START { \
718         BhkFLAGS(hk) &= ~(BHKf_ ## which); \
719     } STMT_END
720
721 #define BhkENTRY_set(hk, which, ptr) \
722     STMT_START { \
723         (hk)->which = ptr; \
724         BhkENABLE(hk, which); \
725     } STMT_END
726
727 #define CALL_BLOCK_HOOKS(which, arg) \
728     STMT_START { \
729         if (PL_blockhooks) { \
730             SSize_t i; \
731             for (i = av_tindex(PL_blockhooks); i >= 0; i--) { \
732                 SV *sv = AvARRAY(PL_blockhooks)[i]; \
733                 BHK *hk; \
734                 \
735                 assert(SvIOK(sv)); \
736                 if (SvUOK(sv)) \
737                     hk = INT2PTR(BHK *, SvUVX(sv)); \
738                 else \
739                     hk = INT2PTR(BHK *, SvIVX(sv)); \
740                 \
741                 if (BhkENTRY(hk, which)) \
742                     BhkENTRY(hk, which)(aTHX_ arg); \
743             } \
744         } \
745     } STMT_END
746
747 /* flags for rv2cv_op_cv */
748
749 #define RV2CVOPCV_MARK_EARLY     0x00000001
750 #define RV2CVOPCV_RETURN_NAME_GV 0x00000002
751 #define RV2CVOPCV_RETURN_STUB    0x00000004
752 #ifdef PERL_CORE /* behaviour of this flag is subject to change: */
753 # define RV2CVOPCV_MAYBE_NAME_GV  0x00000008
754 #endif
755 #define RV2CVOPCV_FLAG_MASK      0x0000000f /* all of the above */
756
757 #define op_lvalue(op,t) Perl_op_lvalue_flags(aTHX_ op,t,0)
758
759 /* flags for op_lvalue_flags */
760
761 #define OP_LVALUE_NO_CROAK 1
762
763 /*
764 =head1 Custom Operators
765
766 =for apidoc Am|U32|XopFLAGS|XOP *xop
767 Return the XOP's flags.
768
769 =for apidoc Am||XopENTRY|XOP *xop|which
770 Return a member of the XOP structure.  I<which> is a cpp token
771 indicating which entry to return.  If the member is not set
772 this will return a default value.  The return type depends
773 on I<which>.  This macro evaluates its arguments more than
774 once.  If you are using C<Perl_custom_op_xop> to retreive a
775 C<XOP *> from a C<OP *>, use the more efficient L</XopENTRYCUSTOM> instead.
776
777 =for apidoc Am||XopENTRYCUSTOM|const OP *o|which
778 Exactly like C<XopENTRY(XopENTRY(Perl_custom_op_xop(aTHX_ o), which)> but more
779 efficient.  The I<which> parameter is identical to L</XopENTRY>.
780
781 =for apidoc Am|void|XopENTRY_set|XOP *xop|which|value
782 Set a member of the XOP structure.  I<which> is a cpp token
783 indicating which entry to set.  See L<perlguts/"Custom Operators">
784 for details about the available members and how
785 they are used.  This macro evaluates its argument
786 more than once.
787
788 =for apidoc Am|void|XopDISABLE|XOP *xop|which
789 Temporarily disable a member of the XOP, by clearing the appropriate flag.
790
791 =for apidoc Am|void|XopENABLE|XOP *xop|which
792 Reenable a member of the XOP which has been disabled.
793
794 =cut
795 */
796
797 struct custom_op {
798     U32             xop_flags;    
799     const char     *xop_name;
800     const char     *xop_desc;
801     U32             xop_class;
802     void          (*xop_peep)(pTHX_ OP *o, OP *oldop);
803 };
804
805 /* return value of Perl_custom_op_get_field, similar to void * then casting but
806    the U32 doesn't need truncation on 64 bit platforms in the caller, also
807    for easier macro writing */
808 typedef union {
809     const char     *xop_name;
810     const char     *xop_desc;
811     U32             xop_class;
812     void          (*xop_peep)(pTHX_ OP *o, OP *oldop);
813     XOP            *xop_ptr;
814 } XOPRETANY;
815
816 #define XopFLAGS(xop) ((xop)->xop_flags)
817
818 #define XOPf_xop_name   0x01
819 #define XOPf_xop_desc   0x02
820 #define XOPf_xop_class  0x04
821 #define XOPf_xop_peep   0x08
822
823 /* used by Perl_custom_op_get_field for option checking */
824 typedef enum {
825     XOPe_xop_ptr = 0, /* just get the XOP *, don't look inside it */
826     XOPe_xop_name = XOPf_xop_name,
827     XOPe_xop_desc = XOPf_xop_desc,
828     XOPe_xop_class = XOPf_xop_class,
829     XOPe_xop_peep = XOPf_xop_peep
830 } xop_flags_enum;
831
832 #define XOPd_xop_name   PL_op_name[OP_CUSTOM]
833 #define XOPd_xop_desc   PL_op_desc[OP_CUSTOM]
834 #define XOPd_xop_class  OA_BASEOP
835 #define XOPd_xop_peep   ((Perl_cpeep_t)0)
836
837 #define XopENTRY_set(xop, which, to) \
838     STMT_START { \
839         (xop)->which = (to); \
840         (xop)->xop_flags |= XOPf_ ## which; \
841     } STMT_END
842
843 #define XopENTRY(xop, which) \
844     ((XopFLAGS(xop) & XOPf_ ## which) ? (xop)->which : XOPd_ ## which)
845
846 #define XopENTRYCUSTOM(o, which) \
847     (Perl_custom_op_get_field(aTHX_ o, XOPe_ ## which).which)
848
849 #define XopDISABLE(xop, which) ((xop)->xop_flags &= ~XOPf_ ## which)
850 #define XopENABLE(xop, which) \
851     STMT_START { \
852         (xop)->xop_flags |= XOPf_ ## which; \
853         assert(XopENTRY(xop, which)); \
854     } STMT_END
855
856 #define Perl_custom_op_xop(x) \
857     (Perl_custom_op_get_field(x, XOPe_xop_ptr).xop_ptr)
858
859 /*
860 =head1 Optree Manipulation Functions
861
862 =for apidoc Am|const char *|OP_NAME|OP *o
863 Return the name of the provided OP.  For core ops this looks up the name
864 from the op_type; for custom ops from the op_ppaddr.
865
866 =for apidoc Am|const char *|OP_DESC|OP *o
867 Return a short description of the provided OP.
868
869 =for apidoc Am|U32|OP_CLASS|OP *o
870 Return the class of the provided OP: that is, which of the *OP
871 structures it uses.  For core ops this currently gets the information out
872 of PL_opargs, which does not always accurately reflect the type used.
873 For custom ops the type is returned from the registration, and it is up
874 to the registree to ensure it is accurate.  The value returned will be
875 one of the OA_* constants from op.h.
876
877 =for apidoc Am|bool|OP_TYPE_IS|OP *o, Optype type
878 Returns true if the given OP is not a NULL pointer
879 and if it is of the given type.
880
881 The negation of this macro, C<OP_TYPE_ISNT> is also available
882 as well as C<OP_TYPE_IS_NN> and C<OP_TYPE_ISNT_NN> which elide
883 the NULL pointer check.
884
885 =for apidoc Am|bool|OP_TYPE_IS_OR_WAS|OP *o, Optype type
886 Returns true if the given OP is not a NULL pointer and
887 if it is of the given type or used to be before being
888 replaced by an OP of type OP_NULL.
889
890 The negation of this macro, C<OP_TYPE_ISNT_AND_WASNT>
891 is also available as well as C<OP_TYPE_IS_OR_WAS_NN>
892 and C<OP_TYPE_ISNT_AND_WASNT_NN> which elide
893 the NULL pointer check.
894
895 =for apidoc Am|bool|OP_HAS_SIBLING|OP *o
896 Returns true if o has a sibling
897
898 =for apidoc Am|bool|OP_SIBLING|OP *o
899 Returns the sibling of o, or NULL if there is no sibling
900
901 =for apidoc Am|bool|OP_SIBLING_set|OP *o|OP *sib
902 Sets the sibling of o to sib
903
904 =cut
905 */
906
907 #define OP_NAME(o) ((o)->op_type == OP_CUSTOM \
908                     ? XopENTRYCUSTOM(o, xop_name) \
909                     : PL_op_name[(o)->op_type])
910 #define OP_DESC(o) ((o)->op_type == OP_CUSTOM \
911                     ? XopENTRYCUSTOM(o, xop_desc) \
912                     : PL_op_desc[(o)->op_type])
913 #define OP_CLASS(o) ((o)->op_type == OP_CUSTOM \
914                      ? XopENTRYCUSTOM(o, xop_class) \
915                      : (PL_opargs[(o)->op_type] & OA_CLASS_MASK))
916
917 #define OP_TYPE_IS(o, type) ((o) && (o)->op_type == (type))
918 #define OP_TYPE_IS_NN(o, type) ((o)->op_type == (type))
919 #define OP_TYPE_ISNT(o, type) ((o) && (o)->op_type != (type))
920 #define OP_TYPE_ISNT_NN(o, type) ((o)->op_type != (type))
921
922 #define OP_TYPE_IS_OR_WAS_NN(o, type) \
923     ( ((o)->op_type == OP_NULL \
924        ? (o)->op_targ \
925        : (o)->op_type) \
926       == (type) )
927
928 #define OP_TYPE_IS_OR_WAS(o, type) \
929     ( (o) && OP_TYPE_IS_OR_WAS_NN(o, type) )
930
931 #define OP_TYPE_ISNT_AND_WASNT_NN(o, type) \
932     ( ((o)->op_type == OP_NULL \
933        ? (o)->op_targ \
934        : (o)->op_type) \
935       != (type) )
936
937 #define OP_TYPE_ISNT_AND_WASNT(o, type) \
938     ( (o) && OP_TYPE_ISNT_AND_WASNT_NN(o, type) )
939
940 #ifdef PERL_OP_PARENT
941 #  define OP_HAS_SIBLING(o)      (!cBOOL((o)->op_lastsib))
942 #  define OP_SIBLING(o)          (0 + (o)->op_lastsib ? NULL : (o)->op_sibling)
943 #  define OP_SIBLING_set(o, sib) ((o)->op_sibling = (sib))
944 #else
945 #  define OP_HAS_SIBLING(o)      (cBOOL((o)->op_sibling))
946 #  define OP_SIBLING(o)          (0 + (o)->op_sibling)
947 #  define OP_SIBLING_set(o, sib) ((o)->op_sibling = (sib))
948 #endif
949
950 #define newATTRSUB(f, o, p, a, b) Perl_newATTRSUB_x(aTHX_  f, o, p, a, b, FALSE)
951 #define newSUB(f, o, p, b)      newATTRSUB((f), (o), (p), NULL, (b))
952
953 /*
954 =head1 Hook manipulation
955 */
956
957 #ifdef USE_ITHREADS
958 #  define OP_CHECK_MUTEX_INIT           MUTEX_INIT(&PL_check_mutex)
959 #  define OP_CHECK_MUTEX_LOCK           MUTEX_LOCK(&PL_check_mutex)
960 #  define OP_CHECK_MUTEX_UNLOCK         MUTEX_UNLOCK(&PL_check_mutex)
961 #  define OP_CHECK_MUTEX_TERM           MUTEX_DESTROY(&PL_check_mutex)
962 #else
963 #  define OP_CHECK_MUTEX_INIT           NOOP
964 #  define OP_CHECK_MUTEX_LOCK           NOOP
965 #  define OP_CHECK_MUTEX_UNLOCK         NOOP
966 #  define OP_CHECK_MUTEX_TERM           NOOP
967 #endif
968
969 /*
970  * Local variables:
971  * c-indentation-style: bsd
972  * c-basic-offset: 4
973  * indent-tabs-mode: nil
974  * End:
975  *
976  * ex: set ts=8 sts=4 sw=4 et:
977  */