4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
112 #if defined(PL_OP_SLAB_ALLOC)
114 #ifdef PERL_DEBUG_READONLY_OPS
115 # define PERL_SLAB_SIZE 4096
116 # include <sys/mman.h>
119 #ifndef PERL_SLAB_SIZE
120 #define PERL_SLAB_SIZE 2048
124 Perl_Slab_Alloc(pTHX_ size_t sz)
128 * To make incrementing use count easy PL_OpSlab is an I32 *
129 * To make inserting the link to slab PL_OpPtr is I32 **
130 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
131 * Add an overhead for pointer to slab and round up as a number of pointers
133 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
134 if ((PL_OpSpace -= sz) < 0) {
135 #ifdef PERL_DEBUG_READONLY_OPS
136 /* We need to allocate chunk by chunk so that we can control the VM
138 PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE,
139 MAP_ANON|MAP_PRIVATE, -1, 0);
141 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
142 (unsigned long) PERL_SLAB_SIZE*sizeof(I32*),
144 if(PL_OpPtr == MAP_FAILED) {
145 perror("mmap failed");
150 PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*));
155 /* We reserve the 0'th I32 sized chunk as a use count */
156 PL_OpSlab = (I32 *) PL_OpPtr;
157 /* Reduce size by the use count word, and by the size we need.
158 * Latter is to mimic the '-=' in the if() above
160 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
161 /* Allocation pointer starts at the top.
162 Theory: because we build leaves before trunk allocating at end
163 means that at run time access is cache friendly upward
165 PL_OpPtr += PERL_SLAB_SIZE;
167 #ifdef PERL_DEBUG_READONLY_OPS
168 /* We remember this slab. */
169 /* This implementation isn't efficient, but it is simple. */
170 PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1));
171 PL_slabs[PL_slab_count++] = PL_OpSlab;
172 DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab));
175 assert( PL_OpSpace >= 0 );
176 /* Move the allocation pointer down */
178 assert( PL_OpPtr > (I32 **) PL_OpSlab );
179 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
180 (*PL_OpSlab)++; /* Increment use count of slab */
181 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
182 assert( *PL_OpSlab > 0 );
183 return (void *)(PL_OpPtr + 1);
186 #ifdef PERL_DEBUG_READONLY_OPS
188 Perl_pending_Slabs_to_ro(pTHX) {
189 /* Turn all the allocated op slabs read only. */
190 U32 count = PL_slab_count;
191 I32 **const slabs = PL_slabs;
193 /* Reset the array of pending OP slabs, as we're about to turn this lot
194 read only. Also, do it ahead of the loop in case the warn triggers,
195 and a warn handler has an eval */
200 /* Force a new slab for any further allocation. */
204 void *const start = slabs[count];
205 const size_t size = PERL_SLAB_SIZE* sizeof(I32*);
206 if(mprotect(start, size, PROT_READ)) {
207 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d",
208 start, (unsigned long) size, errno);
216 S_Slab_to_rw(pTHX_ void *op)
218 I32 * const * const ptr = (I32 **) op;
219 I32 * const slab = ptr[-1];
221 PERL_ARGS_ASSERT_SLAB_TO_RW;
223 assert( ptr-1 > (I32 **) slab );
224 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
226 if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) {
227 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d",
228 slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno);
233 Perl_op_refcnt_inc(pTHX_ OP *o)
244 Perl_op_refcnt_dec(pTHX_ OP *o)
246 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
251 # define Slab_to_rw(op)
255 Perl_Slab_Free(pTHX_ void *op)
257 I32 * const * const ptr = (I32 **) op;
258 I32 * const slab = ptr[-1];
259 PERL_ARGS_ASSERT_SLAB_FREE;
260 assert( ptr-1 > (I32 **) slab );
261 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
264 if (--(*slab) == 0) {
266 # define PerlMemShared PerlMem
269 #ifdef PERL_DEBUG_READONLY_OPS
270 U32 count = PL_slab_count;
271 /* Need to remove this slab from our list of slabs */
274 if (PL_slabs[count] == slab) {
276 /* Found it. Move the entry at the end to overwrite it. */
277 DEBUG_m(PerlIO_printf(Perl_debug_log,
278 "Deallocate %p by moving %p from %lu to %lu\n",
280 PL_slabs[PL_slab_count - 1],
281 PL_slab_count, count));
282 PL_slabs[count] = PL_slabs[--PL_slab_count];
283 /* Could realloc smaller at this point, but probably not
285 if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) {
286 perror("munmap failed");
294 PerlMemShared_free(slab);
296 if (slab == PL_OpSlab) {
301 #else /* !defined(PL_OP_SLAB_ALLOC) */
303 /* See the explanatory comments above struct opslab in op.h. */
305 # ifndef PERL_SLAB_SIZE
306 # define PERL_SLAB_SIZE 64
309 /* rounds up to nearest pointer */
310 # define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
311 # define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
314 S_new_slab(pTHX_ size_t sz)
316 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
317 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
321 /* requires double parens and aTHX_ */
322 #define DEBUG_S_warn(args) \
324 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
328 Perl_Slab_Alloc(pTHX_ size_t sz)
337 if (!PL_compcv || CvROOT(PL_compcv)
338 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
339 return PerlMemShared_calloc(1, sz);
341 if (!CvSTART(PL_compcv)) { /* sneak it in here */
343 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
344 CvSLABBED_on(PL_compcv);
345 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
347 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
349 sz = SIZE_TO_PSIZE(sz) + OPSLOT_HEADER_P;
351 if (slab->opslab_freed) {
352 OP **too = &slab->opslab_freed;
354 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", o, slab));
355 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
356 DEBUG_S_warn((aTHX_ "Alas! too small"));
357 o = *(too = &o->op_next);
358 if (o) DEBUG_S_warn((aTHX_ "found another free op at %p", o));
362 Zero(o, DIFF(o, OpSLOT(o)->opslot_next), I32 *);
368 # define INIT_OPSLOT \
369 slot->opslot_slab = slab; \
370 slot->opslot_next = slab2->opslab_first; \
371 slab2->opslab_first = slot; \
372 o = &slot->opslot_op; \
375 /* The partially-filled slab is next in the chain. */
376 slab2 = slab->opslab_next ? slab->opslab_next : slab;
377 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
378 /* Remaining space is too small. */
382 /* If we can fit a BASEOP, add it to the free chain, so as not
384 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
385 slot = &slab2->opslab_slots;
387 o->op_type = OP_FREED;
388 o->op_next = slab->opslab_freed;
389 slab->opslab_freed = o;
392 /* Create a new slab. Make this one twice as big. */
393 slot = slab2->opslab_first;
394 while (slot->opslot_next) slot = slot->opslot_next;
395 newslab = S_new_slab(aTHX_ DIFF(slab2, slot)*2);
396 newslab->opslab_next = slab->opslab_next;
397 slab->opslab_next = slab2 = newslab;
399 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
401 /* Create a new op slot */
402 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
403 assert(slot >= &slab2->opslab_slots);
404 if (DIFF(&slab2->opslab_slots, slot)
405 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
406 slot = &slab2->opslab_slots;
408 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", o, slab));
414 /* This cannot possibly be right, but it was copied from the old slab
415 allocator, to which it was originally added, without explanation, in
418 # define PerlMemShared PerlMem
422 Perl_Slab_Free(pTHX_ void *op)
425 OP * const o = (OP *)op;
428 PERL_ARGS_ASSERT_SLAB_FREE;
430 if (!o->op_slabbed) {
431 PerlMemShared_free(op);
436 /* If this op is already freed, our refcount will get screwy. */
437 assert(o->op_type != OP_FREED);
438 o->op_type = OP_FREED;
439 o->op_next = slab->opslab_freed;
440 slab->opslab_freed = o;
441 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", o, slab));
442 OpslabREFCNT_dec_padok(slab);
446 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
449 const bool havepad = !!PL_comppad;
450 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
453 PAD_SAVE_SETNULLPAD();
460 Perl_opslab_free(pTHX_ OPSLAB *slab)
464 PERL_ARGS_ASSERT_OPSLAB_FREE;
465 DEBUG_S_warn((aTHX_ "freeing slab %p", slab));
466 assert(slab->opslab_refcnt == 1);
467 for (; slab; slab = slab2) {
468 slab2 = slab->opslab_next;
470 slab->opslab_refcnt = ~(size_t)0;
472 PerlMemShared_free(slab);
477 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
482 size_t savestack_count = 0;
484 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
487 for (slot = slab2->opslab_first;
489 slot = slot->opslot_next) {
490 if (slot->opslot_op.op_type != OP_FREED
491 && !(slot->opslot_op.op_savefree
497 assert(slot->opslot_op.op_slabbed);
498 slab->opslab_refcnt++; /* op_free may free slab */
499 op_free(&slot->opslot_op);
500 if (!--slab->opslab_refcnt) goto free;
503 } while ((slab2 = slab2->opslab_next));
504 /* > 1 because the CV still holds a reference count. */
505 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
507 assert(savestack_count == slab->opslab_refcnt-1);
517 * In the following definition, the ", (OP*)0" is just to make the compiler
518 * think the expression is of the right type: croak actually does a Siglongjmp.
520 #define CHECKOP(type,o) \
521 ((PL_op_mask && PL_op_mask[type]) \
522 ? ( op_free((OP*)o), \
523 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
525 : PL_check[type](aTHX_ (OP*)o))
527 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
529 #define CHANGE_TYPE(o,type) \
531 o->op_type = (OPCODE)type; \
532 o->op_ppaddr = PL_ppaddr[type]; \
536 S_gv_ename(pTHX_ GV *gv)
538 SV* const tmpsv = sv_newmortal();
540 PERL_ARGS_ASSERT_GV_ENAME;
542 gv_efullname3(tmpsv, gv, NULL);
547 S_no_fh_allowed(pTHX_ OP *o)
549 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
551 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
557 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
559 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
560 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
561 SvUTF8(namesv) | flags);
566 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
568 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
569 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
574 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
576 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
578 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
583 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
585 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
587 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
588 SvUTF8(namesv) | flags);
593 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
595 PERL_ARGS_ASSERT_BAD_TYPE_PV;
597 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
598 (int)n, name, t, OP_DESC(kid)), flags);
602 S_bad_type_sv(pTHX_ I32 n, const char *t, SV *namesv, U32 flags, const OP *kid)
604 PERL_ARGS_ASSERT_BAD_TYPE_SV;
606 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
607 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
611 S_no_bareword_allowed(pTHX_ OP *o)
613 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
616 return; /* various ok barewords are hidden in extra OP_NULL */
617 qerror(Perl_mess(aTHX_
618 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
620 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
623 /* "register" allocation */
626 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
630 const bool is_our = (PL_parser->in_my == KEY_our);
632 PERL_ARGS_ASSERT_ALLOCMY;
634 if (flags & ~SVf_UTF8)
635 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
638 /* Until we're using the length for real, cross check that we're being
640 assert(strlen(name) == len);
642 /* complain about "my $<special_var>" etc etc */
646 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
647 (name[1] == '_' && (*name == '$' || len > 2))))
649 /* name[2] is true if strlen(name) > 2 */
650 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
651 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
652 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
653 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
654 PL_parser->in_my == KEY_state ? "state" : "my"));
656 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
657 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
661 /* allocate a spare slot and store the name in that slot */
663 off = pad_add_name_pvn(name, len,
664 (is_our ? padadd_OUR :
665 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
666 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
667 PL_parser->in_my_stash,
669 /* $_ is always in main::, even with our */
670 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
674 /* anon sub prototypes contains state vars should always be cloned,
675 * otherwise the state var would be shared between anon subs */
677 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
678 CvCLONE_on(PL_compcv);
684 =for apidoc alloccopstash
686 Available only under threaded builds, this function allocates an entry in
687 C<PL_stashpad> for the stash passed to it.
694 Perl_alloccopstash(pTHX_ HV *hv)
696 PADOFFSET off = 0, o = 1;
697 bool found_slot = FALSE;
699 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
701 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
703 for (; o < PL_stashpadmax; ++o) {
704 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
705 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
706 found_slot = TRUE, off = o;
709 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
710 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
711 off = PL_stashpadmax;
712 PL_stashpadmax += 10;
715 PL_stashpad[PL_stashpadix = off] = hv;
720 /* free the body of an op without examining its contents.
721 * Always use this rather than FreeOp directly */
724 S_op_destroy(pTHX_ OP *o)
726 if (o->op_latefree) {
734 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
736 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
742 Perl_op_free(pTHX_ OP *o)
747 #ifndef PL_OP_SLAB_ALLOC
748 /* Though ops may be freed twice, freeing the op after its slab is a
750 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
752 /* During the forced freeing of ops after compilation failure, kidops
753 may be freed before their parents. */
754 if (!o || o->op_type == OP_FREED)
756 if (o->op_latefreed) {
763 if (o->op_private & OPpREFCOUNTED) {
774 refcnt = OpREFCNT_dec(o);
777 /* Need to find and remove any pattern match ops from the list
778 we maintain for reset(). */
779 find_and_forget_pmops(o);
789 /* Call the op_free hook if it has been set. Do it now so that it's called
790 * at the right time for refcounted ops, but still before all of the kids
794 if (o->op_flags & OPf_KIDS) {
795 register OP *kid, *nextkid;
796 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
797 nextkid = kid->op_sibling; /* Get before next freeing kid */
802 #ifdef PERL_DEBUG_READONLY_OPS
806 /* COP* is not cleared by op_clear() so that we may track line
807 * numbers etc even after null() */
808 if (type == OP_NEXTSTATE || type == OP_DBSTATE
809 || (type == OP_NULL /* the COP might have been null'ed */
810 && ((OPCODE)o->op_targ == OP_NEXTSTATE
811 || (OPCODE)o->op_targ == OP_DBSTATE))) {
816 type = (OPCODE)o->op_targ;
819 if (o->op_latefree) {
825 #ifdef DEBUG_LEAKING_SCALARS
832 Perl_op_clear(pTHX_ OP *o)
837 PERL_ARGS_ASSERT_OP_CLEAR;
840 mad_free(o->op_madprop);
845 switch (o->op_type) {
846 case OP_NULL: /* Was holding old type, if any. */
847 if (PL_madskills && o->op_targ != OP_NULL) {
848 o->op_type = (Optype)o->op_targ;
853 case OP_ENTEREVAL: /* Was holding hints. */
857 if (!(o->op_flags & OPf_REF)
858 || (PL_check[o->op_type] != Perl_ck_ftst))
865 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
870 /* It's possible during global destruction that the GV is freed
871 before the optree. Whilst the SvREFCNT_inc is happy to bump from
872 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
873 will trigger an assertion failure, because the entry to sv_clear
874 checks that the scalar is not already freed. A check of for
875 !SvIS_FREED(gv) turns out to be invalid, because during global
876 destruction the reference count can be forced down to zero
877 (with SVf_BREAK set). In which case raising to 1 and then
878 dropping to 0 triggers cleanup before it should happen. I
879 *think* that this might actually be a general, systematic,
880 weakness of the whole idea of SVf_BREAK, in that code *is*
881 allowed to raise and lower references during global destruction,
882 so any *valid* code that happens to do this during global
883 destruction might well trigger premature cleanup. */
884 bool still_valid = gv && SvREFCNT(gv);
887 SvREFCNT_inc_simple_void(gv);
889 if (cPADOPo->op_padix > 0) {
890 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
891 * may still exist on the pad */
892 pad_swipe(cPADOPo->op_padix, TRUE);
893 cPADOPo->op_padix = 0;
896 SvREFCNT_dec(cSVOPo->op_sv);
897 cSVOPo->op_sv = NULL;
900 int try_downgrade = SvREFCNT(gv) == 2;
903 gv_try_downgrade(gv);
907 case OP_METHOD_NAMED:
910 SvREFCNT_dec(cSVOPo->op_sv);
911 cSVOPo->op_sv = NULL;
914 Even if op_clear does a pad_free for the target of the op,
915 pad_free doesn't actually remove the sv that exists in the pad;
916 instead it lives on. This results in that it could be reused as
917 a target later on when the pad was reallocated.
920 pad_swipe(o->op_targ,1);
929 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
934 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
936 if (cPADOPo->op_padix > 0) {
937 pad_swipe(cPADOPo->op_padix, TRUE);
938 cPADOPo->op_padix = 0;
941 SvREFCNT_dec(cSVOPo->op_sv);
942 cSVOPo->op_sv = NULL;
946 PerlMemShared_free(cPVOPo->op_pv);
947 cPVOPo->op_pv = NULL;
951 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
955 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
956 /* No GvIN_PAD_off here, because other references may still
957 * exist on the pad */
958 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
961 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
967 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
968 op_free(cPMOPo->op_code_list);
969 cPMOPo->op_code_list = NULL;
970 forget_pmop(cPMOPo, 1);
971 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
972 /* we use the same protection as the "SAFE" version of the PM_ macros
973 * here since sv_clean_all might release some PMOPs
974 * after PL_regex_padav has been cleared
975 * and the clearing of PL_regex_padav needs to
976 * happen before sv_clean_all
979 if(PL_regex_pad) { /* We could be in destruction */
980 const IV offset = (cPMOPo)->op_pmoffset;
981 ReREFCNT_dec(PM_GETRE(cPMOPo));
982 PL_regex_pad[offset] = &PL_sv_undef;
983 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
987 ReREFCNT_dec(PM_GETRE(cPMOPo));
988 PM_SETRE(cPMOPo, NULL);
994 if (o->op_targ > 0) {
995 pad_free(o->op_targ);
1001 S_cop_free(pTHX_ COP* cop)
1003 PERL_ARGS_ASSERT_COP_FREE;
1006 if (! specialWARN(cop->cop_warnings))
1007 PerlMemShared_free(cop->cop_warnings);
1008 cophh_free(CopHINTHASH_get(cop));
1012 S_forget_pmop(pTHX_ PMOP *const o
1018 HV * const pmstash = PmopSTASH(o);
1020 PERL_ARGS_ASSERT_FORGET_PMOP;
1022 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
1023 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
1025 PMOP **const array = (PMOP**) mg->mg_ptr;
1026 U32 count = mg->mg_len / sizeof(PMOP**);
1030 if (array[i] == o) {
1031 /* Found it. Move the entry at the end to overwrite it. */
1032 array[i] = array[--count];
1033 mg->mg_len = count * sizeof(PMOP**);
1034 /* Could realloc smaller at this point always, but probably
1035 not worth it. Probably worth free()ing if we're the
1038 Safefree(mg->mg_ptr);
1055 S_find_and_forget_pmops(pTHX_ OP *o)
1057 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1059 if (o->op_flags & OPf_KIDS) {
1060 OP *kid = cUNOPo->op_first;
1062 switch (kid->op_type) {
1067 forget_pmop((PMOP*)kid, 0);
1069 find_and_forget_pmops(kid);
1070 kid = kid->op_sibling;
1076 Perl_op_null(pTHX_ OP *o)
1080 PERL_ARGS_ASSERT_OP_NULL;
1082 if (o->op_type == OP_NULL)
1086 o->op_targ = o->op_type;
1087 o->op_type = OP_NULL;
1088 o->op_ppaddr = PL_ppaddr[OP_NULL];
1092 Perl_op_refcnt_lock(pTHX)
1095 PERL_UNUSED_CONTEXT;
1100 Perl_op_refcnt_unlock(pTHX)
1103 PERL_UNUSED_CONTEXT;
1107 /* Contextualizers */
1110 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1112 Applies a syntactic context to an op tree representing an expression.
1113 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1114 or C<G_VOID> to specify the context to apply. The modified op tree
1121 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1123 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1125 case G_SCALAR: return scalar(o);
1126 case G_ARRAY: return list(o);
1127 case G_VOID: return scalarvoid(o);
1129 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1136 =head1 Optree Manipulation Functions
1138 =for apidoc Am|OP*|op_linklist|OP *o
1139 This function is the implementation of the L</LINKLIST> macro. It should
1140 not be called directly.
1146 Perl_op_linklist(pTHX_ OP *o)
1150 PERL_ARGS_ASSERT_OP_LINKLIST;
1155 /* establish postfix order */
1156 first = cUNOPo->op_first;
1159 o->op_next = LINKLIST(first);
1162 if (kid->op_sibling) {
1163 kid->op_next = LINKLIST(kid->op_sibling);
1164 kid = kid->op_sibling;
1178 S_scalarkids(pTHX_ OP *o)
1180 if (o && o->op_flags & OPf_KIDS) {
1182 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1189 S_scalarboolean(pTHX_ OP *o)
1193 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1195 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1196 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1197 if (ckWARN(WARN_SYNTAX)) {
1198 const line_t oldline = CopLINE(PL_curcop);
1200 if (PL_parser && PL_parser->copline != NOLINE)
1201 CopLINE_set(PL_curcop, PL_parser->copline);
1202 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1203 CopLINE_set(PL_curcop, oldline);
1210 Perl_scalar(pTHX_ OP *o)
1215 /* assumes no premature commitment */
1216 if (!o || (PL_parser && PL_parser->error_count)
1217 || (o->op_flags & OPf_WANT)
1218 || o->op_type == OP_RETURN)
1223 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1225 switch (o->op_type) {
1227 scalar(cBINOPo->op_first);
1232 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1242 if (o->op_flags & OPf_KIDS) {
1243 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1249 kid = cLISTOPo->op_first;
1251 kid = kid->op_sibling;
1254 OP *sib = kid->op_sibling;
1255 if (sib && kid->op_type != OP_LEAVEWHEN)
1261 PL_curcop = &PL_compiling;
1266 kid = cLISTOPo->op_first;
1269 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1276 Perl_scalarvoid(pTHX_ OP *o)
1280 const char* useless = NULL;
1281 U32 useless_is_utf8 = 0;
1285 PERL_ARGS_ASSERT_SCALARVOID;
1287 /* trailing mad null ops don't count as "there" for void processing */
1289 o->op_type != OP_NULL &&
1291 o->op_sibling->op_type == OP_NULL)
1294 for (sib = o->op_sibling;
1295 sib && sib->op_type == OP_NULL;
1296 sib = sib->op_sibling) ;
1302 if (o->op_type == OP_NEXTSTATE
1303 || o->op_type == OP_DBSTATE
1304 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1305 || o->op_targ == OP_DBSTATE)))
1306 PL_curcop = (COP*)o; /* for warning below */
1308 /* assumes no premature commitment */
1309 want = o->op_flags & OPf_WANT;
1310 if ((want && want != OPf_WANT_SCALAR)
1311 || (PL_parser && PL_parser->error_count)
1312 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1317 if ((o->op_private & OPpTARGET_MY)
1318 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1320 return scalar(o); /* As if inside SASSIGN */
1323 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1325 switch (o->op_type) {
1327 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1331 if (o->op_flags & OPf_STACKED)
1335 if (o->op_private == 4)
1360 case OP_AELEMFAST_LEX:
1379 case OP_GETSOCKNAME:
1380 case OP_GETPEERNAME:
1385 case OP_GETPRIORITY:
1410 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1411 /* Otherwise it's "Useless use of grep iterator" */
1412 useless = OP_DESC(o);
1416 kid = cLISTOPo->op_first;
1417 if (kid && kid->op_type == OP_PUSHRE
1419 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1421 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1423 useless = OP_DESC(o);
1427 kid = cUNOPo->op_first;
1428 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1429 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1432 useless = "negative pattern binding (!~)";
1436 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1437 useless = "non-destructive substitution (s///r)";
1441 useless = "non-destructive transliteration (tr///r)";
1448 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1449 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1450 useless = "a variable";
1455 if (cSVOPo->op_private & OPpCONST_STRICT)
1456 no_bareword_allowed(o);
1458 if (ckWARN(WARN_VOID)) {
1459 /* don't warn on optimised away booleans, eg
1460 * use constant Foo, 5; Foo || print; */
1461 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1463 /* the constants 0 and 1 are permitted as they are
1464 conventionally used as dummies in constructs like
1465 1 while some_condition_with_side_effects; */
1466 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1468 else if (SvPOK(sv)) {
1469 /* perl4's way of mixing documentation and code
1470 (before the invention of POD) was based on a
1471 trick to mix nroff and perl code. The trick was
1472 built upon these three nroff macros being used in
1473 void context. The pink camel has the details in
1474 the script wrapman near page 319. */
1475 const char * const maybe_macro = SvPVX_const(sv);
1476 if (strnEQ(maybe_macro, "di", 2) ||
1477 strnEQ(maybe_macro, "ds", 2) ||
1478 strnEQ(maybe_macro, "ig", 2))
1481 SV * const dsv = newSVpvs("");
1482 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1484 pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
1485 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
1487 useless = SvPV_nolen(msv);
1488 useless_is_utf8 = SvUTF8(msv);
1491 else if (SvOK(sv)) {
1492 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1493 "a constant (%"SVf")", sv));
1494 useless = SvPV_nolen(msv);
1497 useless = "a constant (undef)";
1500 op_null(o); /* don't execute or even remember it */
1504 o->op_type = OP_PREINC; /* pre-increment is faster */
1505 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1509 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1510 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1514 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1515 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1519 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1520 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1525 UNOP *refgen, *rv2cv;
1528 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1531 rv2gv = ((BINOP *)o)->op_last;
1532 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1535 refgen = (UNOP *)((BINOP *)o)->op_first;
1537 if (!refgen || refgen->op_type != OP_REFGEN)
1540 exlist = (LISTOP *)refgen->op_first;
1541 if (!exlist || exlist->op_type != OP_NULL
1542 || exlist->op_targ != OP_LIST)
1545 if (exlist->op_first->op_type != OP_PUSHMARK)
1548 rv2cv = (UNOP*)exlist->op_last;
1550 if (rv2cv->op_type != OP_RV2CV)
1553 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1554 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1555 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1557 o->op_private |= OPpASSIGN_CV_TO_GV;
1558 rv2gv->op_private |= OPpDONT_INIT_GV;
1559 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1571 kid = cLOGOPo->op_first;
1572 if (kid->op_type == OP_NOT
1573 && (kid->op_flags & OPf_KIDS)
1575 if (o->op_type == OP_AND) {
1577 o->op_ppaddr = PL_ppaddr[OP_OR];
1579 o->op_type = OP_AND;
1580 o->op_ppaddr = PL_ppaddr[OP_AND];
1589 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1594 if (o->op_flags & OPf_STACKED)
1601 if (!(o->op_flags & OPf_KIDS))
1612 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1622 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %"SVf" in void context",
1623 newSVpvn_flags(useless, strlen(useless),
1624 SVs_TEMP | ( useless_is_utf8 ? SVf_UTF8 : 0 )));
1629 S_listkids(pTHX_ OP *o)
1631 if (o && o->op_flags & OPf_KIDS) {
1633 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1640 Perl_list(pTHX_ OP *o)
1645 /* assumes no premature commitment */
1646 if (!o || (o->op_flags & OPf_WANT)
1647 || (PL_parser && PL_parser->error_count)
1648 || o->op_type == OP_RETURN)
1653 if ((o->op_private & OPpTARGET_MY)
1654 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1656 return o; /* As if inside SASSIGN */
1659 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1661 switch (o->op_type) {
1664 list(cBINOPo->op_first);
1669 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1677 if (!(o->op_flags & OPf_KIDS))
1679 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1680 list(cBINOPo->op_first);
1681 return gen_constant_list(o);
1688 kid = cLISTOPo->op_first;
1690 kid = kid->op_sibling;
1693 OP *sib = kid->op_sibling;
1694 if (sib && kid->op_type != OP_LEAVEWHEN)
1700 PL_curcop = &PL_compiling;
1704 kid = cLISTOPo->op_first;
1711 S_scalarseq(pTHX_ OP *o)
1715 const OPCODE type = o->op_type;
1717 if (type == OP_LINESEQ || type == OP_SCOPE ||
1718 type == OP_LEAVE || type == OP_LEAVETRY)
1721 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1722 if (kid->op_sibling) {
1726 PL_curcop = &PL_compiling;
1728 o->op_flags &= ~OPf_PARENS;
1729 if (PL_hints & HINT_BLOCK_SCOPE)
1730 o->op_flags |= OPf_PARENS;
1733 o = newOP(OP_STUB, 0);
1738 S_modkids(pTHX_ OP *o, I32 type)
1740 if (o && o->op_flags & OPf_KIDS) {
1742 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1743 op_lvalue(kid, type);
1749 =for apidoc finalize_optree
1751 This function finalizes the optree. Should be called directly after
1752 the complete optree is built. It does some additional
1753 checking which can't be done in the normal ck_xxx functions and makes
1754 the tree thread-safe.
1759 Perl_finalize_optree(pTHX_ OP* o)
1761 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1764 SAVEVPTR(PL_curcop);
1772 S_finalize_op(pTHX_ OP* o)
1774 PERL_ARGS_ASSERT_FINALIZE_OP;
1776 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1778 /* Make sure mad ops are also thread-safe */
1779 MADPROP *mp = o->op_madprop;
1781 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1782 OP *prop_op = (OP *) mp->mad_val;
1783 /* We only need "Relocate sv to the pad for thread safety.", but this
1784 easiest way to make sure it traverses everything */
1785 if (prop_op->op_type == OP_CONST)
1786 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1787 finalize_op(prop_op);
1794 switch (o->op_type) {
1797 PL_curcop = ((COP*)o); /* for warnings */
1801 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1802 && ckWARN(WARN_SYNTAX))
1804 if (o->op_sibling->op_sibling) {
1805 const OPCODE type = o->op_sibling->op_sibling->op_type;
1806 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1807 const line_t oldline = CopLINE(PL_curcop);
1808 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1809 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1810 "Statement unlikely to be reached");
1811 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1812 "\t(Maybe you meant system() when you said exec()?)\n");
1813 CopLINE_set(PL_curcop, oldline);
1820 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1821 GV * const gv = cGVOPo_gv;
1822 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1823 /* XXX could check prototype here instead of just carping */
1824 SV * const sv = sv_newmortal();
1825 gv_efullname3(sv, gv, NULL);
1826 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1827 "%"SVf"() called too early to check prototype",
1834 if (cSVOPo->op_private & OPpCONST_STRICT)
1835 no_bareword_allowed(o);
1839 case OP_METHOD_NAMED:
1840 /* Relocate sv to the pad for thread safety.
1841 * Despite being a "constant", the SV is written to,
1842 * for reference counts, sv_upgrade() etc. */
1843 if (cSVOPo->op_sv) {
1844 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
1845 if (o->op_type != OP_METHOD_NAMED &&
1846 (SvPADTMP(cSVOPo->op_sv) || SvPADMY(cSVOPo->op_sv)))
1848 /* If op_sv is already a PADTMP/MY then it is being used by
1849 * some pad, so make a copy. */
1850 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
1851 SvREADONLY_on(PAD_SVl(ix));
1852 SvREFCNT_dec(cSVOPo->op_sv);
1854 else if (o->op_type != OP_METHOD_NAMED
1855 && cSVOPo->op_sv == &PL_sv_undef) {
1856 /* PL_sv_undef is hack - it's unsafe to store it in the
1857 AV that is the pad, because av_fetch treats values of
1858 PL_sv_undef as a "free" AV entry and will merrily
1859 replace them with a new SV, causing pad_alloc to think
1860 that this pad slot is free. (When, clearly, it is not)
1862 SvOK_off(PAD_SVl(ix));
1863 SvPADTMP_on(PAD_SVl(ix));
1864 SvREADONLY_on(PAD_SVl(ix));
1867 SvREFCNT_dec(PAD_SVl(ix));
1868 SvPADTMP_on(cSVOPo->op_sv);
1869 PAD_SETSV(ix, cSVOPo->op_sv);
1870 /* XXX I don't know how this isn't readonly already. */
1871 SvREADONLY_on(PAD_SVl(ix));
1873 cSVOPo->op_sv = NULL;
1884 const char *key = NULL;
1887 if (((BINOP*)o)->op_last->op_type != OP_CONST)
1890 /* Make the CONST have a shared SV */
1891 svp = cSVOPx_svp(((BINOP*)o)->op_last);
1892 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv))
1893 && SvTYPE(sv) < SVt_PVMG && !SvROK(sv)) {
1894 key = SvPV_const(sv, keylen);
1895 lexname = newSVpvn_share(key,
1896 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
1902 if ((o->op_private & (OPpLVAL_INTRO)))
1905 rop = (UNOP*)((BINOP*)o)->op_first;
1906 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
1908 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
1909 if (!SvPAD_TYPED(lexname))
1911 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1912 if (!fields || !GvHV(*fields))
1914 key = SvPV_const(*svp, keylen);
1915 if (!hv_fetch(GvHV(*fields), key,
1916 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1917 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1918 "in variable %"SVf" of type %"HEKf,
1919 SVfARG(*svp), SVfARG(lexname),
1920 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1932 SVOP *first_key_op, *key_op;
1934 if ((o->op_private & (OPpLVAL_INTRO))
1935 /* I bet there's always a pushmark... */
1936 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
1937 /* hmmm, no optimization if list contains only one key. */
1939 rop = (UNOP*)((LISTOP*)o)->op_last;
1940 if (rop->op_type != OP_RV2HV)
1942 if (rop->op_first->op_type == OP_PADSV)
1943 /* @$hash{qw(keys here)} */
1944 rop = (UNOP*)rop->op_first;
1946 /* @{$hash}{qw(keys here)} */
1947 if (rop->op_first->op_type == OP_SCOPE
1948 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1950 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1956 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
1957 if (!SvPAD_TYPED(lexname))
1959 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1960 if (!fields || !GvHV(*fields))
1962 /* Again guessing that the pushmark can be jumped over.... */
1963 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
1964 ->op_first->op_sibling;
1965 for (key_op = first_key_op; key_op;
1966 key_op = (SVOP*)key_op->op_sibling) {
1967 if (key_op->op_type != OP_CONST)
1969 svp = cSVOPx_svp(key_op);
1970 key = SvPV_const(*svp, keylen);
1971 if (!hv_fetch(GvHV(*fields), key,
1972 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1973 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1974 "in variable %"SVf" of type %"HEKf,
1975 SVfARG(*svp), SVfARG(lexname),
1976 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1982 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1983 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1990 if (o->op_flags & OPf_KIDS) {
1992 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1998 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2000 Propagate lvalue ("modifiable") context to an op and its children.
2001 I<type> represents the context type, roughly based on the type of op that
2002 would do the modifying, although C<local()> is represented by OP_NULL,
2003 because it has no op type of its own (it is signalled by a flag on
2006 This function detects things that can't be modified, such as C<$x+1>, and
2007 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2008 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2010 It also flags things that need to behave specially in an lvalue context,
2011 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2017 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2021 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2024 if (!o || (PL_parser && PL_parser->error_count))
2027 if ((o->op_private & OPpTARGET_MY)
2028 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2033 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2035 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2037 switch (o->op_type) {
2042 if ((o->op_flags & OPf_PARENS) || PL_madskills)
2046 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2047 !(o->op_flags & OPf_STACKED)) {
2048 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2049 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
2050 poses, so we need it clear. */
2051 o->op_private &= ~1;
2052 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2053 assert(cUNOPo->op_first->op_type == OP_NULL);
2054 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2057 else { /* lvalue subroutine call */
2058 o->op_private |= OPpLVAL_INTRO
2059 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
2060 PL_modcount = RETURN_UNLIMITED_NUMBER;
2061 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
2062 /* Potential lvalue context: */
2063 o->op_private |= OPpENTERSUB_INARGS;
2066 else { /* Compile-time error message: */
2067 OP *kid = cUNOPo->op_first;
2070 if (kid->op_type != OP_PUSHMARK) {
2071 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2073 "panic: unexpected lvalue entersub "
2074 "args: type/targ %ld:%"UVuf,
2075 (long)kid->op_type, (UV)kid->op_targ);
2076 kid = kLISTOP->op_first;
2078 while (kid->op_sibling)
2079 kid = kid->op_sibling;
2080 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2081 break; /* Postpone until runtime */
2084 kid = kUNOP->op_first;
2085 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2086 kid = kUNOP->op_first;
2087 if (kid->op_type == OP_NULL)
2089 "Unexpected constant lvalue entersub "
2090 "entry via type/targ %ld:%"UVuf,
2091 (long)kid->op_type, (UV)kid->op_targ);
2092 if (kid->op_type != OP_GV) {
2096 cv = GvCV(kGVOP_gv);
2106 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2107 /* grep, foreach, subcalls, refgen */
2108 if (type == OP_GREPSTART || type == OP_ENTERSUB
2109 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2111 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2112 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2114 : (o->op_type == OP_ENTERSUB
2115 ? "non-lvalue subroutine call"
2117 type ? PL_op_desc[type] : "local"));
2131 case OP_RIGHT_SHIFT:
2140 if (!(o->op_flags & OPf_STACKED))
2147 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2148 op_lvalue(kid, type);
2153 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2154 PL_modcount = RETURN_UNLIMITED_NUMBER;
2155 return o; /* Treat \(@foo) like ordinary list. */
2159 if (scalar_mod_type(o, type))
2161 ref(cUNOPo->op_first, o->op_type);
2165 if (type == OP_LEAVESUBLV)
2166 o->op_private |= OPpMAYBE_LVSUB;
2172 PL_modcount = RETURN_UNLIMITED_NUMBER;
2175 PL_hints |= HINT_BLOCK_SCOPE;
2176 if (type == OP_LEAVESUBLV)
2177 o->op_private |= OPpMAYBE_LVSUB;
2181 ref(cUNOPo->op_first, o->op_type);
2185 PL_hints |= HINT_BLOCK_SCOPE;
2194 case OP_AELEMFAST_LEX:
2201 PL_modcount = RETURN_UNLIMITED_NUMBER;
2202 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2203 return o; /* Treat \(@foo) like ordinary list. */
2204 if (scalar_mod_type(o, type))
2206 if (type == OP_LEAVESUBLV)
2207 o->op_private |= OPpMAYBE_LVSUB;
2211 if (!type) /* local() */
2212 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2213 PAD_COMPNAME_SV(o->op_targ));
2222 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2226 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2232 if (type == OP_LEAVESUBLV)
2233 o->op_private |= OPpMAYBE_LVSUB;
2234 pad_free(o->op_targ);
2235 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
2236 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
2237 if (o->op_flags & OPf_KIDS)
2238 op_lvalue(cBINOPo->op_first->op_sibling, type);
2243 ref(cBINOPo->op_first, o->op_type);
2244 if (type == OP_ENTERSUB &&
2245 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2246 o->op_private |= OPpLVAL_DEFER;
2247 if (type == OP_LEAVESUBLV)
2248 o->op_private |= OPpMAYBE_LVSUB;
2258 if (o->op_flags & OPf_KIDS)
2259 op_lvalue(cLISTOPo->op_last, type);
2264 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2266 else if (!(o->op_flags & OPf_KIDS))
2268 if (o->op_targ != OP_LIST) {
2269 op_lvalue(cBINOPo->op_first, type);
2275 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2276 /* elements might be in void context because the list is
2277 in scalar context or because they are attribute sub calls */
2278 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2279 op_lvalue(kid, type);
2283 if (type != OP_LEAVESUBLV)
2285 break; /* op_lvalue()ing was handled by ck_return() */
2291 /* [20011101.069] File test operators interpret OPf_REF to mean that
2292 their argument is a filehandle; thus \stat(".") should not set
2294 if (type == OP_REFGEN &&
2295 PL_check[o->op_type] == Perl_ck_ftst)
2298 if (type != OP_LEAVESUBLV)
2299 o->op_flags |= OPf_MOD;
2301 if (type == OP_AASSIGN || type == OP_SASSIGN)
2302 o->op_flags |= OPf_SPECIAL|OPf_REF;
2303 else if (!type) { /* local() */
2306 o->op_private |= OPpLVAL_INTRO;
2307 o->op_flags &= ~OPf_SPECIAL;
2308 PL_hints |= HINT_BLOCK_SCOPE;
2313 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2314 "Useless localization of %s", OP_DESC(o));
2317 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2318 && type != OP_LEAVESUBLV)
2319 o->op_flags |= OPf_REF;
2324 S_scalar_mod_type(const OP *o, I32 type)
2329 if (o && o->op_type == OP_RV2GV)
2353 case OP_RIGHT_SHIFT:
2374 S_is_handle_constructor(const OP *o, I32 numargs)
2376 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2378 switch (o->op_type) {
2386 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2399 S_refkids(pTHX_ OP *o, I32 type)
2401 if (o && o->op_flags & OPf_KIDS) {
2403 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2410 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2415 PERL_ARGS_ASSERT_DOREF;
2417 if (!o || (PL_parser && PL_parser->error_count))
2420 switch (o->op_type) {
2422 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2423 !(o->op_flags & OPf_STACKED)) {
2424 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2425 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2426 assert(cUNOPo->op_first->op_type == OP_NULL);
2427 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2428 o->op_flags |= OPf_SPECIAL;
2429 o->op_private &= ~1;
2431 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2432 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2433 : type == OP_RV2HV ? OPpDEREF_HV
2435 o->op_flags |= OPf_MOD;
2441 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2442 doref(kid, type, set_op_ref);
2445 if (type == OP_DEFINED)
2446 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2447 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2450 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2451 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2452 : type == OP_RV2HV ? OPpDEREF_HV
2454 o->op_flags |= OPf_MOD;
2461 o->op_flags |= OPf_REF;
2464 if (type == OP_DEFINED)
2465 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2466 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2472 o->op_flags |= OPf_REF;
2477 if (!(o->op_flags & OPf_KIDS))
2479 doref(cBINOPo->op_first, type, set_op_ref);
2483 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2484 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2485 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2486 : type == OP_RV2HV ? OPpDEREF_HV
2488 o->op_flags |= OPf_MOD;
2498 if (!(o->op_flags & OPf_KIDS))
2500 doref(cLISTOPo->op_last, type, set_op_ref);
2510 S_dup_attrlist(pTHX_ OP *o)
2515 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2517 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2518 * where the first kid is OP_PUSHMARK and the remaining ones
2519 * are OP_CONST. We need to push the OP_CONST values.
2521 if (o->op_type == OP_CONST)
2522 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2524 else if (o->op_type == OP_NULL)
2528 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2530 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2531 if (o->op_type == OP_CONST)
2532 rop = op_append_elem(OP_LIST, rop,
2533 newSVOP(OP_CONST, o->op_flags,
2534 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2541 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
2546 PERL_ARGS_ASSERT_APPLY_ATTRS;
2548 /* fake up C<use attributes $pkg,$rv,@attrs> */
2549 ENTER; /* need to protect against side-effects of 'use' */
2550 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2552 #define ATTRSMODULE "attributes"
2553 #define ATTRSMODULE_PM "attributes.pm"
2556 /* Don't force the C<use> if we don't need it. */
2557 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2558 if (svp && *svp != &PL_sv_undef)
2559 NOOP; /* already in %INC */
2561 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2562 newSVpvs(ATTRSMODULE), NULL);
2565 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2566 newSVpvs(ATTRSMODULE),
2568 op_prepend_elem(OP_LIST,
2569 newSVOP(OP_CONST, 0, stashsv),
2570 op_prepend_elem(OP_LIST,
2571 newSVOP(OP_CONST, 0,
2573 dup_attrlist(attrs))));
2579 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2582 OP *pack, *imop, *arg;
2585 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2590 assert(target->op_type == OP_PADSV ||
2591 target->op_type == OP_PADHV ||
2592 target->op_type == OP_PADAV);
2594 /* Ensure that attributes.pm is loaded. */
2595 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
2597 /* Need package name for method call. */
2598 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2600 /* Build up the real arg-list. */
2601 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2603 arg = newOP(OP_PADSV, 0);
2604 arg->op_targ = target->op_targ;
2605 arg = op_prepend_elem(OP_LIST,
2606 newSVOP(OP_CONST, 0, stashsv),
2607 op_prepend_elem(OP_LIST,
2608 newUNOP(OP_REFGEN, 0,
2609 op_lvalue(arg, OP_REFGEN)),
2610 dup_attrlist(attrs)));
2612 /* Fake up a method call to import */
2613 meth = newSVpvs_share("import");
2614 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2615 op_append_elem(OP_LIST,
2616 op_prepend_elem(OP_LIST, pack, list(arg)),
2617 newSVOP(OP_METHOD_NAMED, 0, meth)));
2619 /* Combine the ops. */
2620 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2624 =notfor apidoc apply_attrs_string
2626 Attempts to apply a list of attributes specified by the C<attrstr> and
2627 C<len> arguments to the subroutine identified by the C<cv> argument which
2628 is expected to be associated with the package identified by the C<stashpv>
2629 argument (see L<attributes>). It gets this wrong, though, in that it
2630 does not correctly identify the boundaries of the individual attribute
2631 specifications within C<attrstr>. This is not really intended for the
2632 public API, but has to be listed here for systems such as AIX which
2633 need an explicit export list for symbols. (It's called from XS code
2634 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2635 to respect attribute syntax properly would be welcome.
2641 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2642 const char *attrstr, STRLEN len)
2646 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2649 len = strlen(attrstr);
2653 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2655 const char * const sstr = attrstr;
2656 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2657 attrs = op_append_elem(OP_LIST, attrs,
2658 newSVOP(OP_CONST, 0,
2659 newSVpvn(sstr, attrstr-sstr)));
2663 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2664 newSVpvs(ATTRSMODULE),
2665 NULL, op_prepend_elem(OP_LIST,
2666 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2667 op_prepend_elem(OP_LIST,
2668 newSVOP(OP_CONST, 0,
2669 newRV(MUTABLE_SV(cv))),
2674 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2678 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2680 PERL_ARGS_ASSERT_MY_KID;
2682 if (!o || (PL_parser && PL_parser->error_count))
2686 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2687 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2691 if (type == OP_LIST) {
2693 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2694 my_kid(kid, attrs, imopsp);
2696 } else if (type == OP_UNDEF || type == OP_STUB) {
2698 } else if (type == OP_RV2SV || /* "our" declaration */
2700 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2701 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2702 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2704 PL_parser->in_my == KEY_our
2706 : PL_parser->in_my == KEY_state ? "state" : "my"));
2708 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2709 PL_parser->in_my = FALSE;
2710 PL_parser->in_my_stash = NULL;
2711 apply_attrs(GvSTASH(gv),
2712 (type == OP_RV2SV ? GvSV(gv) :
2713 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2714 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2717 o->op_private |= OPpOUR_INTRO;
2720 else if (type != OP_PADSV &&
2723 type != OP_PUSHMARK)
2725 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2727 PL_parser->in_my == KEY_our
2729 : PL_parser->in_my == KEY_state ? "state" : "my"));
2732 else if (attrs && type != OP_PUSHMARK) {
2735 PL_parser->in_my = FALSE;
2736 PL_parser->in_my_stash = NULL;
2738 /* check for C<my Dog $spot> when deciding package */
2739 stash = PAD_COMPNAME_TYPE(o->op_targ);
2741 stash = PL_curstash;
2742 apply_attrs_my(stash, o, attrs, imopsp);
2744 o->op_flags |= OPf_MOD;
2745 o->op_private |= OPpLVAL_INTRO;
2747 o->op_private |= OPpPAD_STATE;
2752 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2756 int maybe_scalar = 0;
2758 PERL_ARGS_ASSERT_MY_ATTRS;
2760 /* [perl #17376]: this appears to be premature, and results in code such as
2761 C< our(%x); > executing in list mode rather than void mode */
2763 if (o->op_flags & OPf_PARENS)
2773 o = my_kid(o, attrs, &rops);
2775 if (maybe_scalar && o->op_type == OP_PADSV) {
2776 o = scalar(op_append_list(OP_LIST, rops, o));
2777 o->op_private |= OPpLVAL_INTRO;
2780 /* The listop in rops might have a pushmark at the beginning,
2781 which will mess up list assignment. */
2782 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2783 if (rops->op_type == OP_LIST &&
2784 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2786 OP * const pushmark = lrops->op_first;
2787 lrops->op_first = pushmark->op_sibling;
2790 o = op_append_list(OP_LIST, o, rops);
2793 PL_parser->in_my = FALSE;
2794 PL_parser->in_my_stash = NULL;
2799 Perl_sawparens(pTHX_ OP *o)
2801 PERL_UNUSED_CONTEXT;
2803 o->op_flags |= OPf_PARENS;
2808 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2812 const OPCODE ltype = left->op_type;
2813 const OPCODE rtype = right->op_type;
2815 PERL_ARGS_ASSERT_BIND_MATCH;
2817 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2818 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2820 const char * const desc
2822 rtype == OP_SUBST || rtype == OP_TRANS
2823 || rtype == OP_TRANSR
2825 ? (int)rtype : OP_MATCH];
2826 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2829 (ltype == OP_RV2AV || ltype == OP_RV2HV)
2830 ? cUNOPx(left)->op_first->op_type == OP_GV
2831 && (gv = cGVOPx_gv(cUNOPx(left)->op_first))
2832 ? varname(gv, isary ? '@' : '%', 0, NULL, 0, 1)
2835 (GV *)PL_compcv, isary ? '@' : '%', left->op_targ, NULL, 0, 1
2838 Perl_warner(aTHX_ packWARN(WARN_MISC),
2839 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2842 const char * const sample = (isary
2843 ? "@array" : "%hash");
2844 Perl_warner(aTHX_ packWARN(WARN_MISC),
2845 "Applying %s to %s will act on scalar(%s)",
2846 desc, sample, sample);
2850 if (rtype == OP_CONST &&
2851 cSVOPx(right)->op_private & OPpCONST_BARE &&
2852 cSVOPx(right)->op_private & OPpCONST_STRICT)
2854 no_bareword_allowed(right);
2857 /* !~ doesn't make sense with /r, so error on it for now */
2858 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2860 yyerror("Using !~ with s///r doesn't make sense");
2861 if (rtype == OP_TRANSR && type == OP_NOT)
2862 yyerror("Using !~ with tr///r doesn't make sense");
2864 ismatchop = (rtype == OP_MATCH ||
2865 rtype == OP_SUBST ||
2866 rtype == OP_TRANS || rtype == OP_TRANSR)
2867 && !(right->op_flags & OPf_SPECIAL);
2868 if (ismatchop && right->op_private & OPpTARGET_MY) {
2870 right->op_private &= ~OPpTARGET_MY;
2872 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2875 right->op_flags |= OPf_STACKED;
2876 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
2877 ! (rtype == OP_TRANS &&
2878 right->op_private & OPpTRANS_IDENTICAL) &&
2879 ! (rtype == OP_SUBST &&
2880 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2881 newleft = op_lvalue(left, rtype);
2884 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
2885 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2887 o = op_prepend_elem(rtype, scalar(newleft), right);
2889 return newUNOP(OP_NOT, 0, scalar(o));
2893 return bind_match(type, left,
2894 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
2898 Perl_invert(pTHX_ OP *o)
2902 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2906 =for apidoc Amx|OP *|op_scope|OP *o
2908 Wraps up an op tree with some additional ops so that at runtime a dynamic
2909 scope will be created. The original ops run in the new dynamic scope,
2910 and then, provided that they exit normally, the scope will be unwound.
2911 The additional ops used to create and unwind the dynamic scope will
2912 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
2913 instead if the ops are simple enough to not need the full dynamic scope
2920 Perl_op_scope(pTHX_ OP *o)
2924 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2925 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2926 o->op_type = OP_LEAVE;
2927 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2929 else if (o->op_type == OP_LINESEQ) {
2931 o->op_type = OP_SCOPE;
2932 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2933 kid = ((LISTOP*)o)->op_first;
2934 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2937 /* The following deals with things like 'do {1 for 1}' */
2938 kid = kid->op_sibling;
2940 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2945 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2951 Perl_block_start(pTHX_ int full)
2954 const int retval = PL_savestack_ix;
2956 pad_block_start(full);
2958 PL_hints &= ~HINT_BLOCK_SCOPE;
2959 SAVECOMPILEWARNINGS();
2960 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2962 CALL_BLOCK_HOOKS(bhk_start, full);
2968 Perl_block_end(pTHX_ I32 floor, OP *seq)
2971 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2972 OP* retval = scalarseq(seq);
2974 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
2977 CopHINTS_set(&PL_compiling, PL_hints);
2979 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2982 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
2988 =head1 Compile-time scope hooks
2990 =for apidoc Aox||blockhook_register
2992 Register a set of hooks to be called when the Perl lexical scope changes
2993 at compile time. See L<perlguts/"Compile-time scope hooks">.
2999 Perl_blockhook_register(pTHX_ BHK *hk)
3001 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3003 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3010 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
3011 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
3012 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
3015 OP * const o = newOP(OP_PADSV, 0);
3016 o->op_targ = offset;
3022 Perl_newPROG(pTHX_ OP *o)
3026 PERL_ARGS_ASSERT_NEWPROG;
3033 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3034 ((PL_in_eval & EVAL_KEEPERR)
3035 ? OPf_SPECIAL : 0), o);
3037 cx = &cxstack[cxstack_ix];
3038 assert(CxTYPE(cx) == CXt_EVAL);
3040 if ((cx->blk_gimme & G_WANT) == G_VOID)
3041 scalarvoid(PL_eval_root);
3042 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3045 scalar(PL_eval_root);
3047 PL_eval_start = op_linklist(PL_eval_root);
3048 PL_eval_root->op_private |= OPpREFCOUNTED;
3049 OpREFCNT_set(PL_eval_root, 1);
3050 PL_eval_root->op_next = 0;
3051 i = PL_savestack_ix;
3054 CALL_PEEP(PL_eval_start);
3055 finalize_optree(PL_eval_root);
3057 PL_savestack_ix = i;
3060 if (o->op_type == OP_STUB) {
3061 PL_comppad_name = 0;
3063 S_op_destroy(aTHX_ o);
3066 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3067 PL_curcop = &PL_compiling;
3068 PL_main_start = LINKLIST(PL_main_root);
3069 PL_main_root->op_private |= OPpREFCOUNTED;
3070 OpREFCNT_set(PL_main_root, 1);
3071 PL_main_root->op_next = 0;
3072 CALL_PEEP(PL_main_start);
3073 finalize_optree(PL_main_root);
3074 cv_forget_slab(PL_compcv);
3077 /* Register with debugger */
3079 CV * const cv = get_cvs("DB::postponed", 0);
3083 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3085 call_sv(MUTABLE_SV(cv), G_DISCARD);
3092 Perl_localize(pTHX_ OP *o, I32 lex)
3096 PERL_ARGS_ASSERT_LOCALIZE;
3098 if (o->op_flags & OPf_PARENS)
3099 /* [perl #17376]: this appears to be premature, and results in code such as
3100 C< our(%x); > executing in list mode rather than void mode */
3107 if ( PL_parser->bufptr > PL_parser->oldbufptr
3108 && PL_parser->bufptr[-1] == ','
3109 && ckWARN(WARN_PARENTHESIS))
3111 char *s = PL_parser->bufptr;
3114 /* some heuristics to detect a potential error */
3115 while (*s && (strchr(", \t\n", *s)))
3119 if (*s && strchr("@$%*", *s) && *++s
3120 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
3123 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
3125 while (*s && (strchr(", \t\n", *s)))
3131 if (sigil && (*s == ';' || *s == '=')) {
3132 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3133 "Parentheses missing around \"%s\" list",
3135 ? (PL_parser->in_my == KEY_our
3137 : PL_parser->in_my == KEY_state
3147 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3148 PL_parser->in_my = FALSE;
3149 PL_parser->in_my_stash = NULL;
3154 Perl_jmaybe(pTHX_ OP *o)
3156 PERL_ARGS_ASSERT_JMAYBE;
3158 if (o->op_type == OP_LIST) {
3160 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3161 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3166 PERL_STATIC_INLINE OP *
3167 S_op_std_init(pTHX_ OP *o)
3169 I32 type = o->op_type;
3171 PERL_ARGS_ASSERT_OP_STD_INIT;
3173 if (PL_opargs[type] & OA_RETSCALAR)
3175 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3176 o->op_targ = pad_alloc(type, SVs_PADTMP);
3181 PERL_STATIC_INLINE OP *
3182 S_op_integerize(pTHX_ OP *o)
3184 I32 type = o->op_type;
3186 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3188 /* integerize op, unless it happens to be C<-foo>.
3189 * XXX should pp_i_negate() do magic string negation instead? */
3190 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
3191 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
3192 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
3195 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
3198 if (type == OP_NEGATE)
3199 /* XXX might want a ck_negate() for this */
3200 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3206 S_fold_constants(pTHX_ register OP *o)
3209 register OP * VOL curop;
3211 VOL I32 type = o->op_type;
3216 SV * const oldwarnhook = PL_warnhook;
3217 SV * const olddiehook = PL_diehook;
3221 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3223 if (!(PL_opargs[type] & OA_FOLDCONST))
3237 /* XXX what about the numeric ops? */
3238 if (IN_LOCALE_COMPILETIME)
3242 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3245 if (PL_parser && PL_parser->error_count)
3246 goto nope; /* Don't try to run w/ errors */
3248 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3249 const OPCODE type = curop->op_type;
3250 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3252 type != OP_SCALAR &&
3254 type != OP_PUSHMARK)
3260 curop = LINKLIST(o);
3261 old_next = o->op_next;
3265 oldscope = PL_scopestack_ix;
3266 create_eval_scope(G_FAKINGEVAL);
3268 /* Verify that we don't need to save it: */
3269 assert(PL_curcop == &PL_compiling);
3270 StructCopy(&PL_compiling, ¬_compiling, COP);
3271 PL_curcop = ¬_compiling;
3272 /* The above ensures that we run with all the correct hints of the
3273 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3274 assert(IN_PERL_RUNTIME);
3275 PL_warnhook = PERL_WARNHOOK_FATAL;
3282 sv = *(PL_stack_sp--);
3283 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3285 /* Can't simply swipe the SV from the pad, because that relies on
3286 the op being freed "real soon now". Under MAD, this doesn't
3287 happen (see the #ifdef below). */
3290 pad_swipe(o->op_targ, FALSE);
3293 else if (SvTEMP(sv)) { /* grab mortal temp? */
3294 SvREFCNT_inc_simple_void(sv);
3299 /* Something tried to die. Abandon constant folding. */
3300 /* Pretend the error never happened. */
3302 o->op_next = old_next;
3306 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3307 PL_warnhook = oldwarnhook;
3308 PL_diehook = olddiehook;
3309 /* XXX note that this croak may fail as we've already blown away
3310 * the stack - eg any nested evals */
3311 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3314 PL_warnhook = oldwarnhook;
3315 PL_diehook = olddiehook;
3316 PL_curcop = &PL_compiling;
3318 if (PL_scopestack_ix > oldscope)
3319 delete_eval_scope();
3328 if (type == OP_RV2GV)
3329 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3331 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3332 op_getmad(o,newop,'f');
3340 S_gen_constant_list(pTHX_ register OP *o)
3344 const I32 oldtmps_floor = PL_tmps_floor;
3347 if (PL_parser && PL_parser->error_count)
3348 return o; /* Don't attempt to run with errors */
3350 PL_op = curop = LINKLIST(o);
3353 Perl_pp_pushmark(aTHX);
3356 assert (!(curop->op_flags & OPf_SPECIAL));
3357 assert(curop->op_type == OP_RANGE);
3358 Perl_pp_anonlist(aTHX);
3359 PL_tmps_floor = oldtmps_floor;
3361 o->op_type = OP_RV2AV;
3362 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3363 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3364 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3365 o->op_opt = 0; /* needs to be revisited in rpeep() */
3366 curop = ((UNOP*)o)->op_first;
3367 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
3369 op_getmad(curop,o,'O');
3378 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3381 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3382 if (!o || o->op_type != OP_LIST)
3383 o = newLISTOP(OP_LIST, 0, o, NULL);
3385 o->op_flags &= ~OPf_WANT;
3387 if (!(PL_opargs[type] & OA_MARK))
3388 op_null(cLISTOPo->op_first);
3390 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3391 if (kid2 && kid2->op_type == OP_COREARGS) {
3392 op_null(cLISTOPo->op_first);
3393 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3397 o->op_type = (OPCODE)type;
3398 o->op_ppaddr = PL_ppaddr[type];
3399 o->op_flags |= flags;
3401 o = CHECKOP(type, o);
3402 if (o->op_type != (unsigned)type)
3405 return fold_constants(op_integerize(op_std_init(o)));
3409 =head1 Optree Manipulation Functions
3412 /* List constructors */
3415 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3417 Append an item to the list of ops contained directly within a list-type
3418 op, returning the lengthened list. I<first> is the list-type op,
3419 and I<last> is the op to append to the list. I<optype> specifies the
3420 intended opcode for the list. If I<first> is not already a list of the
3421 right type, it will be upgraded into one. If either I<first> or I<last>
3422 is null, the other is returned unchanged.
3428 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3436 if (first->op_type != (unsigned)type
3437 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3439 return newLISTOP(type, 0, first, last);
3442 if (first->op_flags & OPf_KIDS)
3443 ((LISTOP*)first)->op_last->op_sibling = last;
3445 first->op_flags |= OPf_KIDS;
3446 ((LISTOP*)first)->op_first = last;
3448 ((LISTOP*)first)->op_last = last;
3453 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3455 Concatenate the lists of ops contained directly within two list-type ops,
3456 returning the combined list. I<first> and I<last> are the list-type ops
3457 to concatenate. I<optype> specifies the intended opcode for the list.
3458 If either I<first> or I<last> is not already a list of the right type,
3459 it will be upgraded into one. If either I<first> or I<last> is null,
3460 the other is returned unchanged.
3466 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3474 if (first->op_type != (unsigned)type)
3475 return op_prepend_elem(type, first, last);
3477 if (last->op_type != (unsigned)type)
3478 return op_append_elem(type, first, last);
3480 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3481 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3482 first->op_flags |= (last->op_flags & OPf_KIDS);
3485 if (((LISTOP*)last)->op_first && first->op_madprop) {
3486 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3488 while (mp->mad_next)
3490 mp->mad_next = first->op_madprop;
3493 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3496 first->op_madprop = last->op_madprop;
3497 last->op_madprop = 0;
3500 S_op_destroy(aTHX_ last);
3506 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3508 Prepend an item to the list of ops contained directly within a list-type
3509 op, returning the lengthened list. I<first> is the op to prepend to the
3510 list, and I<last> is the list-type op. I<optype> specifies the intended
3511 opcode for the list. If I<last> is not already a list of the right type,
3512 it will be upgraded into one. If either I<first> or I<last> is null,
3513 the other is returned unchanged.
3519 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3527 if (last->op_type == (unsigned)type) {
3528 if (type == OP_LIST) { /* already a PUSHMARK there */
3529 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3530 ((LISTOP*)last)->op_first->op_sibling = first;
3531 if (!(first->op_flags & OPf_PARENS))
3532 last->op_flags &= ~OPf_PARENS;
3535 if (!(last->op_flags & OPf_KIDS)) {
3536 ((LISTOP*)last)->op_last = first;
3537 last->op_flags |= OPf_KIDS;
3539 first->op_sibling = ((LISTOP*)last)->op_first;
3540 ((LISTOP*)last)->op_first = first;
3542 last->op_flags |= OPf_KIDS;
3546 return newLISTOP(type, 0, first, last);
3554 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3557 Newxz(tk, 1, TOKEN);
3558 tk->tk_type = (OPCODE)optype;
3559 tk->tk_type = 12345;
3561 tk->tk_mad = madprop;
3566 Perl_token_free(pTHX_ TOKEN* tk)
3568 PERL_ARGS_ASSERT_TOKEN_FREE;
3570 if (tk->tk_type != 12345)
3572 mad_free(tk->tk_mad);
3577 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3582 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3584 if (tk->tk_type != 12345) {
3585 Perl_warner(aTHX_ packWARN(WARN_MISC),
3586 "Invalid TOKEN object ignored");
3593 /* faked up qw list? */
3595 tm->mad_type == MAD_SV &&
3596 SvPVX((SV *)tm->mad_val)[0] == 'q')
3603 /* pretend constant fold didn't happen? */
3604 if (mp->mad_key == 'f' &&
3605 (o->op_type == OP_CONST ||
3606 o->op_type == OP_GV) )
3608 token_getmad(tk,(OP*)mp->mad_val,slot);
3622 if (mp->mad_key == 'X')
3623 mp->mad_key = slot; /* just change the first one */
3633 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3642 /* pretend constant fold didn't happen? */
3643 if (mp->mad_key == 'f' &&
3644 (o->op_type == OP_CONST ||
3645 o->op_type == OP_GV) )
3647 op_getmad(from,(OP*)mp->mad_val,slot);
3654 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3657 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3663 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3672 /* pretend constant fold didn't happen? */
3673 if (mp->mad_key == 'f' &&
3674 (o->op_type == OP_CONST ||
3675 o->op_type == OP_GV) )
3677 op_getmad(from,(OP*)mp->mad_val,slot);
3684 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
3687 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
3691 PerlIO_printf(PerlIO_stderr(),
3692 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
3698 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
3716 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
3720 addmad(tm, &(o->op_madprop), slot);
3724 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
3745 Perl_newMADsv(pTHX_ char key, SV* sv)
3747 PERL_ARGS_ASSERT_NEWMADSV;
3749 return newMADPROP(key, MAD_SV, sv, 0);
3753 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3755 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
3758 mp->mad_vlen = vlen;
3759 mp->mad_type = type;
3761 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
3766 Perl_mad_free(pTHX_ MADPROP* mp)
3768 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3772 mad_free(mp->mad_next);
3773 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3774 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3775 switch (mp->mad_type) {
3779 Safefree((char*)mp->mad_val);
3782 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
3783 op_free((OP*)mp->mad_val);
3786 sv_free(MUTABLE_SV(mp->mad_val));
3789 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3792 PerlMemShared_free(mp);
3798 =head1 Optree construction
3800 =for apidoc Am|OP *|newNULLLIST
3802 Constructs, checks, and returns a new C<stub> op, which represents an
3803 empty list expression.
3809 Perl_newNULLLIST(pTHX)
3811 return newOP(OP_STUB, 0);
3815 S_force_list(pTHX_ OP *o)
3817 if (!o || o->op_type != OP_LIST)
3818 o = newLISTOP(OP_LIST, 0, o, NULL);
3824 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3826 Constructs, checks, and returns an op of any list type. I<type> is
3827 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3828 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3829 supply up to two ops to be direct children of the list op; they are
3830 consumed by this function and become part of the constructed op tree.
3836 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3841 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3843 NewOp(1101, listop, 1, LISTOP);
3845 listop->op_type = (OPCODE)type;
3846 listop->op_ppaddr = PL_ppaddr[type];
3849 listop->op_flags = (U8)flags;
3853 else if (!first && last)
3856 first->op_sibling = last;
3857 listop->op_first = first;
3858 listop->op_last = last;
3859 if (type == OP_LIST) {
3860 OP* const pushop = newOP(OP_PUSHMARK, 0);
3861 pushop->op_sibling = first;
3862 listop->op_first = pushop;
3863 listop->op_flags |= OPf_KIDS;
3865 listop->op_last = pushop;
3868 return CHECKOP(type, listop);
3872 =for apidoc Am|OP *|newOP|I32 type|I32 flags
3874 Constructs, checks, and returns an op of any base type (any type that
3875 has no extra fields). I<type> is the opcode. I<flags> gives the
3876 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3883 Perl_newOP(pTHX_ I32 type, I32 flags)
3888 if (type == -OP_ENTEREVAL) {
3889 type = OP_ENTEREVAL;
3890 flags |= OPpEVAL_BYTES<<8;
3893 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3894 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3895 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3896 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3898 NewOp(1101, o, 1, OP);
3899 o->op_type = (OPCODE)type;
3900 o->op_ppaddr = PL_ppaddr[type];
3901 o->op_flags = (U8)flags;
3903 o->op_latefreed = 0;
3907 o->op_private = (U8)(0 | (flags >> 8));
3908 if (PL_opargs[type] & OA_RETSCALAR)
3910 if (PL_opargs[type] & OA_TARGET)
3911 o->op_targ = pad_alloc(type, SVs_PADTMP);
3912 return CHECKOP(type, o);
3916 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3918 Constructs, checks, and returns an op of any unary type. I<type> is
3919 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3920 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3921 bits, the eight bits of C<op_private>, except that the bit with value 1
3922 is automatically set. I<first> supplies an optional op to be the direct
3923 child of the unary op; it is consumed by this function and become part
3924 of the constructed op tree.
3930 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3935 if (type == -OP_ENTEREVAL) {
3936 type = OP_ENTEREVAL;
3937 flags |= OPpEVAL_BYTES<<8;
3940 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3941 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3942 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3943 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3944 || type == OP_SASSIGN
3945 || type == OP_ENTERTRY
3946 || type == OP_NULL );
3949 first = newOP(OP_STUB, 0);
3950 if (PL_opargs[type] & OA_MARK)
3951 first = force_list(first);
3953 NewOp(1101, unop, 1, UNOP);
3954 unop->op_type = (OPCODE)type;
3955 unop->op_ppaddr = PL_ppaddr[type];
3956 unop->op_first = first;
3957 unop->op_flags = (U8)(flags | OPf_KIDS);
3958 unop->op_private = (U8)(1 | (flags >> 8));
3959 unop = (UNOP*) CHECKOP(type, unop);
3963 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3967 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3969 Constructs, checks, and returns an op of any binary type. I<type>
3970 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3971 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3972 the eight bits of C<op_private>, except that the bit with value 1 or
3973 2 is automatically set as required. I<first> and I<last> supply up to
3974 two ops to be the direct children of the binary op; they are consumed
3975 by this function and become part of the constructed op tree.
3981 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3986 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3987 || type == OP_SASSIGN || type == OP_NULL );
3989 NewOp(1101, binop, 1, BINOP);
3992 first = newOP(OP_NULL, 0);
3994 binop->op_type = (OPCODE)type;
3995 binop->op_ppaddr = PL_ppaddr[type];
3996 binop->op_first = first;
3997 binop->op_flags = (U8)(flags | OPf_KIDS);
4000 binop->op_private = (U8)(1 | (flags >> 8));
4003 binop->op_private = (U8)(2 | (flags >> 8));
4004 first->op_sibling = last;
4007 binop = (BINOP*)CHECKOP(type, binop);
4008 if (binop->op_next || binop->op_type != (OPCODE)type)
4011 binop->op_last = binop->op_first->op_sibling;
4013 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4016 static int uvcompare(const void *a, const void *b)
4017 __attribute__nonnull__(1)
4018 __attribute__nonnull__(2)
4019 __attribute__pure__;
4020 static int uvcompare(const void *a, const void *b)
4022 if (*((const UV *)a) < (*(const UV *)b))
4024 if (*((const UV *)a) > (*(const UV *)b))
4026 if (*((const UV *)a+1) < (*(const UV *)b+1))
4028 if (*((const UV *)a+1) > (*(const UV *)b+1))
4034 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4037 SV * const tstr = ((SVOP*)expr)->op_sv;
4040 (repl->op_type == OP_NULL)
4041 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
4043 ((SVOP*)repl)->op_sv;
4046 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4047 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4051 register short *tbl;
4053 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4054 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4055 I32 del = o->op_private & OPpTRANS_DELETE;
4058 PERL_ARGS_ASSERT_PMTRANS;
4060 PL_hints |= HINT_BLOCK_SCOPE;
4063 o->op_private |= OPpTRANS_FROM_UTF;
4066 o->op_private |= OPpTRANS_TO_UTF;
4068 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4069 SV* const listsv = newSVpvs("# comment\n");
4071 const U8* tend = t + tlen;
4072 const U8* rend = r + rlen;
4086 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4087 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4090 const U32 flags = UTF8_ALLOW_DEFAULT;
4094 t = tsave = bytes_to_utf8(t, &len);
4097 if (!to_utf && rlen) {
4099 r = rsave = bytes_to_utf8(r, &len);
4103 /* There are several snags with this code on EBCDIC:
4104 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
4105 2. scan_const() in toke.c has encoded chars in native encoding which makes
4106 ranges at least in EBCDIC 0..255 range the bottom odd.
4110 U8 tmpbuf[UTF8_MAXBYTES+1];
4113 Newx(cp, 2*tlen, UV);
4115 transv = newSVpvs("");
4117 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4119 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
4121 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4125 cp[2*i+1] = cp[2*i];
4129 qsort(cp, i, 2*sizeof(UV), uvcompare);
4130 for (j = 0; j < i; j++) {
4132 diff = val - nextmin;
4134 t = uvuni_to_utf8(tmpbuf,nextmin);
4135 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4137 U8 range_mark = UTF_TO_NATIVE(0xff);
4138 t = uvuni_to_utf8(tmpbuf, val - 1);
4139 sv_catpvn(transv, (char *)&range_mark, 1);
4140 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4147 t = uvuni_to_utf8(tmpbuf,nextmin);
4148 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4150 U8 range_mark = UTF_TO_NATIVE(0xff);
4151 sv_catpvn(transv, (char *)&range_mark, 1);
4153 t = uvuni_to_utf8(tmpbuf, 0x7fffffff);
4154 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4155 t = (const U8*)SvPVX_const(transv);
4156 tlen = SvCUR(transv);
4160 else if (!rlen && !del) {
4161 r = t; rlen = tlen; rend = tend;
4164 if ((!rlen && !del) || t == r ||
4165 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4167 o->op_private |= OPpTRANS_IDENTICAL;
4171 while (t < tend || tfirst <= tlast) {
4172 /* see if we need more "t" chars */
4173 if (tfirst > tlast) {
4174 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4176 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
4178 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4185 /* now see if we need more "r" chars */
4186 if (rfirst > rlast) {
4188 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4190 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
4192 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4201 rfirst = rlast = 0xffffffff;
4205 /* now see which range will peter our first, if either. */
4206 tdiff = tlast - tfirst;
4207 rdiff = rlast - rfirst;
4214 if (rfirst == 0xffffffff) {
4215 diff = tdiff; /* oops, pretend rdiff is infinite */
4217 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4218 (long)tfirst, (long)tlast);
4220 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4224 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4225 (long)tfirst, (long)(tfirst + diff),
4228 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4229 (long)tfirst, (long)rfirst);
4231 if (rfirst + diff > max)
4232 max = rfirst + diff;
4234 grows = (tfirst < rfirst &&
4235 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4247 else if (max > 0xff)
4252 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4254 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
4255 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4256 PAD_SETSV(cPADOPo->op_padix, swash);
4258 SvREADONLY_on(swash);
4260 cSVOPo->op_sv = swash;
4262 SvREFCNT_dec(listsv);
4263 SvREFCNT_dec(transv);
4265 if (!del && havefinal && rlen)
4266 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4267 newSVuv((UV)final), 0);
4270 o->op_private |= OPpTRANS_GROWS;
4276 op_getmad(expr,o,'e');
4277 op_getmad(repl,o,'r');
4285 tbl = (short*)PerlMemShared_calloc(
4286 (o->op_private & OPpTRANS_COMPLEMENT) &&
4287 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4289 cPVOPo->op_pv = (char*)tbl;
4291 for (i = 0; i < (I32)tlen; i++)
4293 for (i = 0, j = 0; i < 256; i++) {
4295 if (j >= (I32)rlen) {
4304 if (i < 128 && r[j] >= 128)
4314 o->op_private |= OPpTRANS_IDENTICAL;
4316 else if (j >= (I32)rlen)
4321 PerlMemShared_realloc(tbl,
4322 (0x101+rlen-j) * sizeof(short));
4323 cPVOPo->op_pv = (char*)tbl;
4325 tbl[0x100] = (short)(rlen - j);
4326 for (i=0; i < (I32)rlen - j; i++)
4327 tbl[0x101+i] = r[j+i];
4331 if (!rlen && !del) {
4334 o->op_private |= OPpTRANS_IDENTICAL;
4336 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4337 o->op_private |= OPpTRANS_IDENTICAL;
4339 for (i = 0; i < 256; i++)
4341 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4342 if (j >= (I32)rlen) {
4344 if (tbl[t[i]] == -1)
4350 if (tbl[t[i]] == -1) {
4351 if (t[i] < 128 && r[j] >= 128)
4358 if(del && rlen == tlen) {
4359 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4360 } else if(rlen > tlen) {
4361 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4365 o->op_private |= OPpTRANS_GROWS;
4367 op_getmad(expr,o,'e');
4368 op_getmad(repl,o,'r');
4378 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4380 Constructs, checks, and returns an op of any pattern matching type.
4381 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4382 and, shifted up eight bits, the eight bits of C<op_private>.
4388 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4393 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4395 NewOp(1101, pmop, 1, PMOP);
4396 pmop->op_type = (OPCODE)type;
4397 pmop->op_ppaddr = PL_ppaddr[type];
4398 pmop->op_flags = (U8)flags;
4399 pmop->op_private = (U8)(0 | (flags >> 8));
4401 if (PL_hints & HINT_RE_TAINT)
4402 pmop->op_pmflags |= PMf_RETAINT;
4403 if (IN_LOCALE_COMPILETIME) {
4404 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4406 else if ((! (PL_hints & HINT_BYTES))
4407 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4408 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4410 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4412 if (PL_hints & HINT_RE_FLAGS) {
4413 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4414 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4416 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4417 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4418 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4420 if (reflags && SvOK(reflags)) {
4421 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4427 assert(SvPOK(PL_regex_pad[0]));
4428 if (SvCUR(PL_regex_pad[0])) {
4429 /* Pop off the "packed" IV from the end. */
4430 SV *const repointer_list = PL_regex_pad[0];
4431 const char *p = SvEND(repointer_list) - sizeof(IV);
4432 const IV offset = *((IV*)p);
4434 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4436 SvEND_set(repointer_list, p);
4438 pmop->op_pmoffset = offset;
4439 /* This slot should be free, so assert this: */
4440 assert(PL_regex_pad[offset] == &PL_sv_undef);
4442 SV * const repointer = &PL_sv_undef;
4443 av_push(PL_regex_padav, repointer);
4444 pmop->op_pmoffset = av_len(PL_regex_padav);
4445 PL_regex_pad = AvARRAY(PL_regex_padav);
4449 return CHECKOP(type, pmop);
4452 /* Given some sort of match op o, and an expression expr containing a
4453 * pattern, either compile expr into a regex and attach it to o (if it's
4454 * constant), or convert expr into a runtime regcomp op sequence (if it's
4457 * isreg indicates that the pattern is part of a regex construct, eg
4458 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4459 * split "pattern", which aren't. In the former case, expr will be a list
4460 * if the pattern contains more than one term (eg /a$b/) or if it contains
4461 * a replacement, ie s/// or tr///.
4463 * When the pattern has been compiled within a new anon CV (for
4464 * qr/(?{...})/ ), then floor indicates the savestack level just before
4465 * the new sub was created
4469 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4474 I32 repl_has_vars = 0;
4476 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4477 bool is_compiletime;
4480 PERL_ARGS_ASSERT_PMRUNTIME;
4482 /* for s/// and tr///, last element in list is the replacement; pop it */
4484 if (is_trans || o->op_type == OP_SUBST) {
4486 repl = cLISTOPx(expr)->op_last;
4487 kid = cLISTOPx(expr)->op_first;
4488 while (kid->op_sibling != repl)
4489 kid = kid->op_sibling;
4490 kid->op_sibling = NULL;
4491 cLISTOPx(expr)->op_last = kid;
4494 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4497 OP* const oe = expr;
4498 assert(expr->op_type == OP_LIST);
4499 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4500 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4501 expr = cLISTOPx(oe)->op_last;
4502 cLISTOPx(oe)->op_first->op_sibling = NULL;
4503 cLISTOPx(oe)->op_last = NULL;
4506 return pmtrans(o, expr, repl);
4509 /* find whether we have any runtime or code elements;
4510 * at the same time, temporarily set the op_next of each DO block;
4511 * then when we LINKLIST, this will cause the DO blocks to be excluded
4512 * from the op_next chain (and from having LINKLIST recursively
4513 * applied to them). We fix up the DOs specially later */
4517 if (expr->op_type == OP_LIST) {
4519 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4520 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4522 assert(!o->op_next && o->op_sibling);
4523 o->op_next = o->op_sibling;
4525 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4529 else if (expr->op_type != OP_CONST)
4534 /* fix up DO blocks; treat each one as a separate little sub */
4536 if (expr->op_type == OP_LIST) {
4538 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4539 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4541 o->op_next = NULL; /* undo temporary hack from above */
4544 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4545 LISTOP *leave = cLISTOPx(cLISTOPo->op_first);
4547 assert(leave->op_first->op_type == OP_ENTER);
4548 assert(leave->op_first->op_sibling);
4549 o->op_next = leave->op_first->op_sibling;
4551 assert(leave->op_flags & OPf_KIDS);
4552 assert(leave->op_last->op_next = (OP*)leave);
4553 leave->op_next = NULL; /* stop on last op */
4554 op_null((OP*)leave);
4558 OP *scope = cLISTOPo->op_first;
4559 assert(scope->op_type == OP_SCOPE);
4560 assert(scope->op_flags & OPf_KIDS);
4561 scope->op_next = NULL; /* stop on last op */
4564 /* have to peep the DOs individually as we've removed it from
4565 * the op_next chain */
4568 /* runtime finalizes as part of finalizing whole tree */
4573 PL_hints |= HINT_BLOCK_SCOPE;
4575 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4577 if (is_compiletime) {
4578 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4579 regexp_engine const *eng = current_re_engine();
4581 if (o->op_flags & OPf_SPECIAL)
4582 rx_flags |= RXf_SPLIT;
4584 if (!has_code || !eng->op_comp) {
4585 /* compile-time simple constant pattern */
4587 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4588 /* whoops! we guessed that a qr// had a code block, but we
4589 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4590 * that isn't required now. Note that we have to be pretty
4591 * confident that nothing used that CV's pad while the
4592 * regex was parsed */
4593 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4594 /* But we know that one op is using this CV's slab. */
4595 cv_forget_slab(PL_compcv);
4597 pm->op_pmflags &= ~PMf_HAS_CV;
4602 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4603 rx_flags, pm->op_pmflags)
4604 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4605 rx_flags, pm->op_pmflags)
4608 op_getmad(expr,(OP*)pm,'e');
4614 /* compile-time pattern that includes literal code blocks */
4615 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4618 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4621 if (pm->op_pmflags & PMf_HAS_CV) {
4623 /* this QR op (and the anon sub we embed it in) is never
4624 * actually executed. It's just a placeholder where we can
4625 * squirrel away expr in op_code_list without the peephole
4626 * optimiser etc processing it for a second time */
4627 OP *qr = newPMOP(OP_QR, 0);
4628 ((PMOP*)qr)->op_code_list = expr;
4630 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4631 SvREFCNT_inc_simple_void(PL_compcv);
4632 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4633 ((struct regexp *)SvANY(re))->qr_anoncv = cv;
4635 /* attach the anon CV to the pad so that
4636 * pad_fixup_inner_anons() can find it */
4637 (void)pad_add_anon(cv, o->op_type);
4638 SvREFCNT_inc_simple_void(cv);
4643 pm->op_code_list = expr;
4648 /* runtime pattern: build chain of regcomp etc ops */
4650 PADOFFSET cv_targ = 0;
4652 reglist = isreg && expr->op_type == OP_LIST;
4657 pm->op_code_list = expr;
4658 /* don't free op_code_list; its ops are embedded elsewhere too */
4659 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
4662 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
4663 * to allow its op_next to be pointed past the regcomp and
4664 * preceding stacking ops;
4665 * OP_REGCRESET is there to reset taint before executing the
4667 if (pm->op_pmflags & PMf_KEEP || PL_tainting)
4668 expr = newUNOP((PL_tainting ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
4670 if (pm->op_pmflags & PMf_HAS_CV) {
4671 /* we have a runtime qr with literal code. This means
4672 * that the qr// has been wrapped in a new CV, which
4673 * means that runtime consts, vars etc will have been compiled
4674 * against a new pad. So... we need to execute those ops
4675 * within the environment of the new CV. So wrap them in a call
4676 * to a new anon sub. i.e. for
4680 * we build an anon sub that looks like
4682 * sub { "a", $b, '(?{...})' }
4684 * and call it, passing the returned list to regcomp.
4685 * Or to put it another way, the list of ops that get executed
4689 * ------ -------------------
4690 * pushmark (for regcomp)
4691 * pushmark (for entersub)
4692 * pushmark (for refgen)
4696 * regcreset regcreset
4698 * const("a") const("a")
4700 * const("(?{...})") const("(?{...})")
4705 SvREFCNT_inc_simple_void(PL_compcv);
4706 /* these lines are just an unrolled newANONATTRSUB */
4707 expr = newSVOP(OP_ANONCODE, 0,
4708 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
4709 cv_targ = expr->op_targ;
4710 expr = newUNOP(OP_REFGEN, 0, expr);
4712 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
4715 NewOp(1101, rcop, 1, LOGOP);
4716 rcop->op_type = OP_REGCOMP;
4717 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4718 rcop->op_first = scalar(expr);
4719 rcop->op_flags |= OPf_KIDS
4720 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4721 | (reglist ? OPf_STACKED : 0);
4722 rcop->op_private = 0;
4724 rcop->op_targ = cv_targ;
4726 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4727 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4729 /* establish postfix order */
4730 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
4732 rcop->op_next = expr;
4733 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4736 rcop->op_next = LINKLIST(expr);
4737 expr->op_next = (OP*)rcop;
4740 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4745 if (pm->op_pmflags & PMf_EVAL) {
4747 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
4748 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
4750 else if (repl->op_type == OP_CONST)
4754 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
4755 if (curop->op_type == OP_SCOPE
4756 || curop->op_type == OP_LEAVE
4757 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
4758 if (curop->op_type == OP_GV) {
4759 GV * const gv = cGVOPx_gv(curop);
4761 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
4764 else if (curop->op_type == OP_RV2CV)
4766 else if (curop->op_type == OP_RV2SV ||
4767 curop->op_type == OP_RV2AV ||
4768 curop->op_type == OP_RV2HV ||
4769 curop->op_type == OP_RV2GV) {
4770 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
4773 else if (curop->op_type == OP_PADSV ||
4774 curop->op_type == OP_PADAV ||
4775 curop->op_type == OP_PADHV ||
4776 curop->op_type == OP_PADANY)
4780 else if (curop->op_type == OP_PUSHRE)
4781 NOOP; /* Okay here, dangerous in newASSIGNOP */
4791 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4793 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4794 op_prepend_elem(o->op_type, scalar(repl), o);
4797 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
4798 pm->op_pmflags |= PMf_MAYBE_CONST;
4800 NewOp(1101, rcop, 1, LOGOP);
4801 rcop->op_type = OP_SUBSTCONT;
4802 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4803 rcop->op_first = scalar(repl);
4804 rcop->op_flags |= OPf_KIDS;
4805 rcop->op_private = 1;
4808 /* establish postfix order */
4809 rcop->op_next = LINKLIST(repl);
4810 repl->op_next = (OP*)rcop;
4812 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
4813 assert(!(pm->op_pmflags & PMf_ONCE));
4814 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
4823 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
4825 Constructs, checks, and returns an op of any type that involves an
4826 embedded SV. I<type> is the opcode. I<flags> gives the eight bits
4827 of C<op_flags>. I<sv> gives the SV to embed in the op; this function
4828 takes ownership of one reference to it.
4834 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
4839 PERL_ARGS_ASSERT_NEWSVOP;
4841 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4842 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4843 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4845 NewOp(1101, svop, 1, SVOP);