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 /* See the explanatory comments above struct opslab in op.h. */
114 #ifdef PERL_DEBUG_READONLY_OPS
115 # define PERL_SLAB_SIZE 128
116 # define PERL_MAX_SLAB_SIZE 4096
117 # include <sys/mman.h>
120 #ifndef PERL_SLAB_SIZE
121 # define PERL_SLAB_SIZE 64
123 #ifndef PERL_MAX_SLAB_SIZE
124 # define PERL_MAX_SLAB_SIZE 2048
127 /* rounds up to nearest pointer */
128 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
129 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
132 S_new_slab(pTHX_ size_t sz)
134 #ifdef PERL_DEBUG_READONLY_OPS
135 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
136 PROT_READ|PROT_WRITE,
137 MAP_ANON|MAP_PRIVATE, -1, 0);
138 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
139 (unsigned long) sz, slab));
140 if (slab == MAP_FAILED) {
141 perror("mmap failed");
144 slab->opslab_size = (U16)sz;
146 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
148 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
152 /* requires double parens and aTHX_ */
153 #define DEBUG_S_warn(args) \
155 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
159 Perl_Slab_Alloc(pTHX_ size_t sz)
168 if (!PL_compcv || CvROOT(PL_compcv)
169 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
170 return PerlMemShared_calloc(1, sz);
172 if (!CvSTART(PL_compcv)) { /* sneak it in here */
174 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
175 CvSLABBED_on(PL_compcv);
176 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
178 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
180 opsz = SIZE_TO_PSIZE(sz);
181 sz = opsz + OPSLOT_HEADER_P;
183 if (slab->opslab_freed) {
184 OP **too = &slab->opslab_freed;
186 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", o, slab));
187 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
188 DEBUG_S_warn((aTHX_ "Alas! too small"));
189 o = *(too = &o->op_next);
190 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", o)); }
194 Zero(o, opsz, I32 *);
200 #define INIT_OPSLOT \
201 slot->opslot_slab = slab; \
202 slot->opslot_next = slab2->opslab_first; \
203 slab2->opslab_first = slot; \
204 o = &slot->opslot_op; \
207 /* The partially-filled slab is next in the chain. */
208 slab2 = slab->opslab_next ? slab->opslab_next : slab;
209 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
210 /* Remaining space is too small. */
212 /* If we can fit a BASEOP, add it to the free chain, so as not
214 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
215 slot = &slab2->opslab_slots;
217 o->op_type = OP_FREED;
218 o->op_next = slab->opslab_freed;
219 slab->opslab_freed = o;
222 /* Create a new slab. Make this one twice as big. */
223 slot = slab2->opslab_first;
224 while (slot->opslot_next) slot = slot->opslot_next;
225 slab2 = S_new_slab(aTHX_
226 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
228 : (DIFF(slab2, slot)+1)*2);
229 slab2->opslab_next = slab->opslab_next;
230 slab->opslab_next = slab2;
232 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
234 /* Create a new op slot */
235 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
236 assert(slot >= &slab2->opslab_slots);
237 if (DIFF(&slab2->opslab_slots, slot)
238 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
239 slot = &slab2->opslab_slots;
241 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", o, slab));
247 #ifdef PERL_DEBUG_READONLY_OPS
249 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
251 PERL_ARGS_ASSERT_SLAB_TO_RO;
253 if (slab->opslab_readonly) return;
254 slab->opslab_readonly = 1;
255 for (; slab; slab = slab->opslab_next) {
256 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
257 (unsigned long) slab->opslab_size, slab));*/
258 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
259 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
260 (unsigned long)slab->opslab_size, errno);
265 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
269 PERL_ARGS_ASSERT_SLAB_TO_RW;
271 if (!slab->opslab_readonly) return;
273 for (; slab2; slab2 = slab2->opslab_next) {
274 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
275 (unsigned long) size, slab2));*/
276 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
277 PROT_READ|PROT_WRITE)) {
278 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
279 (unsigned long)slab2->opslab_size, errno);
282 slab->opslab_readonly = 0;
286 # define Slab_to_rw(op)
289 /* This cannot possibly be right, but it was copied from the old slab
290 allocator, to which it was originally added, without explanation, in
293 # define PerlMemShared PerlMem
297 Perl_Slab_Free(pTHX_ void *op)
300 OP * const o = (OP *)op;
303 PERL_ARGS_ASSERT_SLAB_FREE;
305 if (!o->op_slabbed) {
306 PerlMemShared_free(op);
311 /* If this op is already freed, our refcount will get screwy. */
312 assert(o->op_type != OP_FREED);
313 o->op_type = OP_FREED;
314 o->op_next = slab->opslab_freed;
315 slab->opslab_freed = o;
316 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", o, slab));
317 OpslabREFCNT_dec_padok(slab);
321 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
324 const bool havepad = !!PL_comppad;
325 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
328 PAD_SAVE_SETNULLPAD();
335 Perl_opslab_free(pTHX_ OPSLAB *slab)
339 PERL_ARGS_ASSERT_OPSLAB_FREE;
340 DEBUG_S_warn((aTHX_ "freeing slab %p", slab));
341 assert(slab->opslab_refcnt == 1);
342 for (; slab; slab = slab2) {
343 slab2 = slab->opslab_next;
345 slab->opslab_refcnt = ~(size_t)0;
347 #ifdef PERL_DEBUG_READONLY_OPS
348 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
350 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
351 perror("munmap failed");
355 PerlMemShared_free(slab);
361 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
366 size_t savestack_count = 0;
368 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
371 for (slot = slab2->opslab_first;
373 slot = slot->opslot_next) {
374 if (slot->opslot_op.op_type != OP_FREED
375 && !(slot->opslot_op.op_savefree
381 assert(slot->opslot_op.op_slabbed);
382 slab->opslab_refcnt++; /* op_free may free slab */
383 op_free(&slot->opslot_op);
384 if (!--slab->opslab_refcnt) goto free;
387 } while ((slab2 = slab2->opslab_next));
388 /* > 1 because the CV still holds a reference count. */
389 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
391 assert(savestack_count == slab->opslab_refcnt-1);
399 #ifdef PERL_DEBUG_READONLY_OPS
401 Perl_op_refcnt_inc(pTHX_ OP *o)
404 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
405 if (slab && slab->opslab_readonly) {
418 Perl_op_refcnt_dec(pTHX_ OP *o)
421 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
423 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
425 if (slab && slab->opslab_readonly) {
427 result = --o->op_targ;
430 result = --o->op_targ;
436 * In the following definition, the ", (OP*)0" is just to make the compiler
437 * think the expression is of the right type: croak actually does a Siglongjmp.
439 #define CHECKOP(type,o) \
440 ((PL_op_mask && PL_op_mask[type]) \
441 ? ( op_free((OP*)o), \
442 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
444 : PL_check[type](aTHX_ (OP*)o))
446 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
448 #define CHANGE_TYPE(o,type) \
450 o->op_type = (OPCODE)type; \
451 o->op_ppaddr = PL_ppaddr[type]; \
455 S_gv_ename(pTHX_ GV *gv)
457 SV* const tmpsv = sv_newmortal();
459 PERL_ARGS_ASSERT_GV_ENAME;
461 gv_efullname3(tmpsv, gv, NULL);
466 S_no_fh_allowed(pTHX_ OP *o)
468 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
470 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
476 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
478 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
479 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
480 SvUTF8(namesv) | flags);
485 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
487 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
488 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
493 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
495 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
497 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
502 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
504 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
506 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
507 SvUTF8(namesv) | flags);
512 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
514 PERL_ARGS_ASSERT_BAD_TYPE_PV;
516 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
517 (int)n, name, t, OP_DESC(kid)), flags);
521 S_bad_type_sv(pTHX_ I32 n, const char *t, SV *namesv, U32 flags, const OP *kid)
523 PERL_ARGS_ASSERT_BAD_TYPE_SV;
525 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
526 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
530 S_no_bareword_allowed(pTHX_ OP *o)
532 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
535 return; /* various ok barewords are hidden in extra OP_NULL */
536 qerror(Perl_mess(aTHX_
537 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
539 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
542 /* "register" allocation */
545 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
549 const bool is_our = (PL_parser->in_my == KEY_our);
551 PERL_ARGS_ASSERT_ALLOCMY;
553 if (flags & ~SVf_UTF8)
554 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
557 /* Until we're using the length for real, cross check that we're being
559 assert(strlen(name) == len);
561 /* complain about "my $<special_var>" etc etc */
565 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
566 (name[1] == '_' && (*name == '$' || len > 2))))
568 /* name[2] is true if strlen(name) > 2 */
569 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
570 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
571 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
572 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
573 PL_parser->in_my == KEY_state ? "state" : "my"));
575 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
576 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
580 /* allocate a spare slot and store the name in that slot */
582 off = pad_add_name_pvn(name, len,
583 (is_our ? padadd_OUR :
584 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
585 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
586 PL_parser->in_my_stash,
588 /* $_ is always in main::, even with our */
589 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
593 /* anon sub prototypes contains state vars should always be cloned,
594 * otherwise the state var would be shared between anon subs */
596 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
597 CvCLONE_on(PL_compcv);
603 =for apidoc alloccopstash
605 Available only under threaded builds, this function allocates an entry in
606 C<PL_stashpad> for the stash passed to it.
613 Perl_alloccopstash(pTHX_ HV *hv)
615 PADOFFSET off = 0, o = 1;
616 bool found_slot = FALSE;
618 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
620 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
622 for (; o < PL_stashpadmax; ++o) {
623 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
624 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
625 found_slot = TRUE, off = o;
628 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
629 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
630 off = PL_stashpadmax;
631 PL_stashpadmax += 10;
634 PL_stashpad[PL_stashpadix = off] = hv;
639 /* free the body of an op without examining its contents.
640 * Always use this rather than FreeOp directly */
643 S_op_destroy(pTHX_ OP *o)
649 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
651 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
657 Perl_op_free(pTHX_ OP *o)
662 /* Though ops may be freed twice, freeing the op after its slab is a
664 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
665 /* During the forced freeing of ops after compilation failure, kidops
666 may be freed before their parents. */
667 if (!o || o->op_type == OP_FREED)
671 if (o->op_private & OPpREFCOUNTED) {
682 refcnt = OpREFCNT_dec(o);
685 /* Need to find and remove any pattern match ops from the list
686 we maintain for reset(). */
687 find_and_forget_pmops(o);
697 /* Call the op_free hook if it has been set. Do it now so that it's called
698 * at the right time for refcounted ops, but still before all of the kids
702 if (o->op_flags & OPf_KIDS) {
704 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
705 nextkid = kid->op_sibling; /* Get before next freeing kid */
710 type = (OPCODE)o->op_targ;
713 Slab_to_rw(OpSLAB(o));
716 /* COP* is not cleared by op_clear() so that we may track line
717 * numbers etc even after null() */
718 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
724 #ifdef DEBUG_LEAKING_SCALARS
731 Perl_op_clear(pTHX_ OP *o)
736 PERL_ARGS_ASSERT_OP_CLEAR;
739 mad_free(o->op_madprop);
744 switch (o->op_type) {
745 case OP_NULL: /* Was holding old type, if any. */
746 if (PL_madskills && o->op_targ != OP_NULL) {
747 o->op_type = (Optype)o->op_targ;
752 case OP_ENTEREVAL: /* Was holding hints. */
756 if (!(o->op_flags & OPf_REF)
757 || (PL_check[o->op_type] != Perl_ck_ftst))
764 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
769 /* It's possible during global destruction that the GV is freed
770 before the optree. Whilst the SvREFCNT_inc is happy to bump from
771 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
772 will trigger an assertion failure, because the entry to sv_clear
773 checks that the scalar is not already freed. A check of for
774 !SvIS_FREED(gv) turns out to be invalid, because during global
775 destruction the reference count can be forced down to zero
776 (with SVf_BREAK set). In which case raising to 1 and then
777 dropping to 0 triggers cleanup before it should happen. I
778 *think* that this might actually be a general, systematic,
779 weakness of the whole idea of SVf_BREAK, in that code *is*
780 allowed to raise and lower references during global destruction,
781 so any *valid* code that happens to do this during global
782 destruction might well trigger premature cleanup. */
783 bool still_valid = gv && SvREFCNT(gv);
786 SvREFCNT_inc_simple_void(gv);
788 if (cPADOPo->op_padix > 0) {
789 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
790 * may still exist on the pad */
791 pad_swipe(cPADOPo->op_padix, TRUE);
792 cPADOPo->op_padix = 0;
795 SvREFCNT_dec(cSVOPo->op_sv);
796 cSVOPo->op_sv = NULL;
799 int try_downgrade = SvREFCNT(gv) == 2;
802 gv_try_downgrade(gv);
806 case OP_METHOD_NAMED:
809 SvREFCNT_dec(cSVOPo->op_sv);
810 cSVOPo->op_sv = NULL;
813 Even if op_clear does a pad_free for the target of the op,
814 pad_free doesn't actually remove the sv that exists in the pad;
815 instead it lives on. This results in that it could be reused as
816 a target later on when the pad was reallocated.
819 pad_swipe(o->op_targ,1);
829 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
834 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
835 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
837 if (cPADOPo->op_padix > 0) {
838 pad_swipe(cPADOPo->op_padix, TRUE);
839 cPADOPo->op_padix = 0;
842 SvREFCNT_dec(cSVOPo->op_sv);
843 cSVOPo->op_sv = NULL;
847 PerlMemShared_free(cPVOPo->op_pv);
848 cPVOPo->op_pv = NULL;
852 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
856 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
857 /* No GvIN_PAD_off here, because other references may still
858 * exist on the pad */
859 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
862 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
868 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
869 op_free(cPMOPo->op_code_list);
870 cPMOPo->op_code_list = NULL;
871 forget_pmop(cPMOPo, 1);
872 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
873 /* we use the same protection as the "SAFE" version of the PM_ macros
874 * here since sv_clean_all might release some PMOPs
875 * after PL_regex_padav has been cleared
876 * and the clearing of PL_regex_padav needs to
877 * happen before sv_clean_all
880 if(PL_regex_pad) { /* We could be in destruction */
881 const IV offset = (cPMOPo)->op_pmoffset;
882 ReREFCNT_dec(PM_GETRE(cPMOPo));
883 PL_regex_pad[offset] = &PL_sv_undef;
884 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
888 ReREFCNT_dec(PM_GETRE(cPMOPo));
889 PM_SETRE(cPMOPo, NULL);
895 if (o->op_targ > 0) {
896 pad_free(o->op_targ);
902 S_cop_free(pTHX_ COP* cop)
904 PERL_ARGS_ASSERT_COP_FREE;
907 if (! specialWARN(cop->cop_warnings))
908 PerlMemShared_free(cop->cop_warnings);
909 cophh_free(CopHINTHASH_get(cop));
913 S_forget_pmop(pTHX_ PMOP *const o
919 HV * const pmstash = PmopSTASH(o);
921 PERL_ARGS_ASSERT_FORGET_PMOP;
923 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
924 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
926 PMOP **const array = (PMOP**) mg->mg_ptr;
927 U32 count = mg->mg_len / sizeof(PMOP**);
932 /* Found it. Move the entry at the end to overwrite it. */
933 array[i] = array[--count];
934 mg->mg_len = count * sizeof(PMOP**);
935 /* Could realloc smaller at this point always, but probably
936 not worth it. Probably worth free()ing if we're the
939 Safefree(mg->mg_ptr);
956 S_find_and_forget_pmops(pTHX_ OP *o)
958 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
960 if (o->op_flags & OPf_KIDS) {
961 OP *kid = cUNOPo->op_first;
963 switch (kid->op_type) {
968 forget_pmop((PMOP*)kid, 0);
970 find_and_forget_pmops(kid);
971 kid = kid->op_sibling;
977 Perl_op_null(pTHX_ OP *o)
981 PERL_ARGS_ASSERT_OP_NULL;
983 if (o->op_type == OP_NULL)
987 o->op_targ = o->op_type;
988 o->op_type = OP_NULL;
989 o->op_ppaddr = PL_ppaddr[OP_NULL];
993 Perl_op_refcnt_lock(pTHX)
1001 Perl_op_refcnt_unlock(pTHX)
1004 PERL_UNUSED_CONTEXT;
1008 /* Contextualizers */
1011 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1013 Applies a syntactic context to an op tree representing an expression.
1014 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1015 or C<G_VOID> to specify the context to apply. The modified op tree
1022 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1024 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1026 case G_SCALAR: return scalar(o);
1027 case G_ARRAY: return list(o);
1028 case G_VOID: return scalarvoid(o);
1030 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1037 =head1 Optree Manipulation Functions
1039 =for apidoc Am|OP*|op_linklist|OP *o
1040 This function is the implementation of the L</LINKLIST> macro. It should
1041 not be called directly.
1047 Perl_op_linklist(pTHX_ OP *o)
1051 PERL_ARGS_ASSERT_OP_LINKLIST;
1056 /* establish postfix order */
1057 first = cUNOPo->op_first;
1060 o->op_next = LINKLIST(first);
1063 if (kid->op_sibling) {
1064 kid->op_next = LINKLIST(kid->op_sibling);
1065 kid = kid->op_sibling;
1079 S_scalarkids(pTHX_ OP *o)
1081 if (o && o->op_flags & OPf_KIDS) {
1083 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1090 S_scalarboolean(pTHX_ OP *o)
1094 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1096 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1097 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1098 if (ckWARN(WARN_SYNTAX)) {
1099 const line_t oldline = CopLINE(PL_curcop);
1101 if (PL_parser && PL_parser->copline != NOLINE) {
1102 /* This ensures that warnings are reported at the first line
1103 of the conditional, not the last. */
1104 CopLINE_set(PL_curcop, PL_parser->copline);
1106 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1107 CopLINE_set(PL_curcop, oldline);
1114 Perl_scalar(pTHX_ OP *o)
1119 /* assumes no premature commitment */
1120 if (!o || (PL_parser && PL_parser->error_count)
1121 || (o->op_flags & OPf_WANT)
1122 || o->op_type == OP_RETURN)
1127 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1129 switch (o->op_type) {
1131 scalar(cBINOPo->op_first);
1136 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1146 if (o->op_flags & OPf_KIDS) {
1147 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1153 kid = cLISTOPo->op_first;
1155 kid = kid->op_sibling;
1158 OP *sib = kid->op_sibling;
1159 if (sib && kid->op_type != OP_LEAVEWHEN)
1165 PL_curcop = &PL_compiling;
1170 kid = cLISTOPo->op_first;
1173 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1180 Perl_scalarvoid(pTHX_ OP *o)
1184 SV *useless_sv = NULL;
1185 const char* useless = NULL;
1189 PERL_ARGS_ASSERT_SCALARVOID;
1191 /* trailing mad null ops don't count as "there" for void processing */
1193 o->op_type != OP_NULL &&
1195 o->op_sibling->op_type == OP_NULL)
1198 for (sib = o->op_sibling;
1199 sib && sib->op_type == OP_NULL;
1200 sib = sib->op_sibling) ;
1206 if (o->op_type == OP_NEXTSTATE
1207 || o->op_type == OP_DBSTATE
1208 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1209 || o->op_targ == OP_DBSTATE)))
1210 PL_curcop = (COP*)o; /* for warning below */
1212 /* assumes no premature commitment */
1213 want = o->op_flags & OPf_WANT;
1214 if ((want && want != OPf_WANT_SCALAR)
1215 || (PL_parser && PL_parser->error_count)
1216 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1221 if ((o->op_private & OPpTARGET_MY)
1222 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1224 return scalar(o); /* As if inside SASSIGN */
1227 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1229 switch (o->op_type) {
1231 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1235 if (o->op_flags & OPf_STACKED)
1239 if (o->op_private == 4)
1264 case OP_AELEMFAST_LEX:
1283 case OP_GETSOCKNAME:
1284 case OP_GETPEERNAME:
1289 case OP_GETPRIORITY:
1314 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1315 /* Otherwise it's "Useless use of grep iterator" */
1316 useless = OP_DESC(o);
1320 kid = cLISTOPo->op_first;
1321 if (kid && kid->op_type == OP_PUSHRE
1323 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1325 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1327 useless = OP_DESC(o);
1331 kid = cUNOPo->op_first;
1332 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1333 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1336 useless = "negative pattern binding (!~)";
1340 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1341 useless = "non-destructive substitution (s///r)";
1345 useless = "non-destructive transliteration (tr///r)";
1352 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1353 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1354 useless = "a variable";
1359 if (cSVOPo->op_private & OPpCONST_STRICT)
1360 no_bareword_allowed(o);
1362 if (ckWARN(WARN_VOID)) {
1363 /* don't warn on optimised away booleans, eg
1364 * use constant Foo, 5; Foo || print; */
1365 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1367 /* the constants 0 and 1 are permitted as they are
1368 conventionally used as dummies in constructs like
1369 1 while some_condition_with_side_effects; */
1370 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1372 else if (SvPOK(sv)) {
1373 /* perl4's way of mixing documentation and code
1374 (before the invention of POD) was based on a
1375 trick to mix nroff and perl code. The trick was
1376 built upon these three nroff macros being used in
1377 void context. The pink camel has the details in
1378 the script wrapman near page 319. */
1379 const char * const maybe_macro = SvPVX_const(sv);
1380 if (strnEQ(maybe_macro, "di", 2) ||
1381 strnEQ(maybe_macro, "ds", 2) ||
1382 strnEQ(maybe_macro, "ig", 2))
1385 SV * const dsv = newSVpvs("");
1387 = Perl_newSVpvf(aTHX_
1389 pv_pretty(dsv, maybe_macro,
1390 SvCUR(sv), 32, NULL, NULL,
1392 | PERL_PV_ESCAPE_NOCLEAR
1393 | PERL_PV_ESCAPE_UNI_DETECT));
1397 else if (SvOK(sv)) {
1398 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", sv);
1401 useless = "a constant (undef)";
1404 op_null(o); /* don't execute or even remember it */
1408 o->op_type = OP_PREINC; /* pre-increment is faster */
1409 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1413 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1414 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1418 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1419 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1423 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1424 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1429 UNOP *refgen, *rv2cv;
1432 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1435 rv2gv = ((BINOP *)o)->op_last;
1436 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1439 refgen = (UNOP *)((BINOP *)o)->op_first;
1441 if (!refgen || refgen->op_type != OP_REFGEN)
1444 exlist = (LISTOP *)refgen->op_first;
1445 if (!exlist || exlist->op_type != OP_NULL
1446 || exlist->op_targ != OP_LIST)
1449 if (exlist->op_first->op_type != OP_PUSHMARK)
1452 rv2cv = (UNOP*)exlist->op_last;
1454 if (rv2cv->op_type != OP_RV2CV)
1457 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1458 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1459 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1461 o->op_private |= OPpASSIGN_CV_TO_GV;
1462 rv2gv->op_private |= OPpDONT_INIT_GV;
1463 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1475 kid = cLOGOPo->op_first;
1476 if (kid->op_type == OP_NOT
1477 && (kid->op_flags & OPf_KIDS)
1479 if (o->op_type == OP_AND) {
1481 o->op_ppaddr = PL_ppaddr[OP_OR];
1483 o->op_type = OP_AND;
1484 o->op_ppaddr = PL_ppaddr[OP_AND];
1493 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1498 if (o->op_flags & OPf_STACKED)
1505 if (!(o->op_flags & OPf_KIDS))
1516 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1527 /* mortalise it, in case warnings are fatal. */
1528 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1529 "Useless use of %"SVf" in void context",
1530 sv_2mortal(useless_sv));
1533 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1534 "Useless use of %s in void context",
1541 S_listkids(pTHX_ OP *o)
1543 if (o && o->op_flags & OPf_KIDS) {
1545 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1552 Perl_list(pTHX_ OP *o)
1557 /* assumes no premature commitment */
1558 if (!o || (o->op_flags & OPf_WANT)
1559 || (PL_parser && PL_parser->error_count)
1560 || o->op_type == OP_RETURN)
1565 if ((o->op_private & OPpTARGET_MY)
1566 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1568 return o; /* As if inside SASSIGN */
1571 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1573 switch (o->op_type) {
1576 list(cBINOPo->op_first);
1581 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1589 if (!(o->op_flags & OPf_KIDS))
1591 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1592 list(cBINOPo->op_first);
1593 return gen_constant_list(o);
1600 kid = cLISTOPo->op_first;
1602 kid = kid->op_sibling;
1605 OP *sib = kid->op_sibling;
1606 if (sib && kid->op_type != OP_LEAVEWHEN)
1612 PL_curcop = &PL_compiling;
1616 kid = cLISTOPo->op_first;
1623 S_scalarseq(pTHX_ OP *o)
1627 const OPCODE type = o->op_type;
1629 if (type == OP_LINESEQ || type == OP_SCOPE ||
1630 type == OP_LEAVE || type == OP_LEAVETRY)
1633 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1634 if (kid->op_sibling) {
1638 PL_curcop = &PL_compiling;
1640 o->op_flags &= ~OPf_PARENS;
1641 if (PL_hints & HINT_BLOCK_SCOPE)
1642 o->op_flags |= OPf_PARENS;
1645 o = newOP(OP_STUB, 0);
1650 S_modkids(pTHX_ OP *o, I32 type)
1652 if (o && o->op_flags & OPf_KIDS) {
1654 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1655 op_lvalue(kid, type);
1661 =for apidoc finalize_optree
1663 This function finalizes the optree. Should be called directly after
1664 the complete optree is built. It does some additional
1665 checking which can't be done in the normal ck_xxx functions and makes
1666 the tree thread-safe.
1671 Perl_finalize_optree(pTHX_ OP* o)
1673 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1676 SAVEVPTR(PL_curcop);
1684 S_finalize_op(pTHX_ OP* o)
1686 PERL_ARGS_ASSERT_FINALIZE_OP;
1688 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1690 /* Make sure mad ops are also thread-safe */
1691 MADPROP *mp = o->op_madprop;
1693 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1694 OP *prop_op = (OP *) mp->mad_val;
1695 /* We only need "Relocate sv to the pad for thread safety.", but this
1696 easiest way to make sure it traverses everything */
1697 if (prop_op->op_type == OP_CONST)
1698 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1699 finalize_op(prop_op);
1706 switch (o->op_type) {
1709 PL_curcop = ((COP*)o); /* for warnings */
1713 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1714 && ckWARN(WARN_SYNTAX))
1716 if (o->op_sibling->op_sibling) {
1717 const OPCODE type = o->op_sibling->op_sibling->op_type;
1718 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1719 const line_t oldline = CopLINE(PL_curcop);
1720 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1721 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1722 "Statement unlikely to be reached");
1723 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1724 "\t(Maybe you meant system() when you said exec()?)\n");
1725 CopLINE_set(PL_curcop, oldline);
1732 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1733 GV * const gv = cGVOPo_gv;
1734 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1735 /* XXX could check prototype here instead of just carping */
1736 SV * const sv = sv_newmortal();
1737 gv_efullname3(sv, gv, NULL);
1738 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1739 "%"SVf"() called too early to check prototype",
1746 if (cSVOPo->op_private & OPpCONST_STRICT)
1747 no_bareword_allowed(o);
1751 case OP_METHOD_NAMED:
1752 /* Relocate sv to the pad for thread safety.
1753 * Despite being a "constant", the SV is written to,
1754 * for reference counts, sv_upgrade() etc. */
1755 if (cSVOPo->op_sv) {
1756 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
1757 if (o->op_type != OP_METHOD_NAMED &&
1758 (SvPADTMP(cSVOPo->op_sv) || SvPADMY(cSVOPo->op_sv)))
1760 /* If op_sv is already a PADTMP/MY then it is being used by
1761 * some pad, so make a copy. */
1762 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
1763 SvREADONLY_on(PAD_SVl(ix));
1764 SvREFCNT_dec(cSVOPo->op_sv);
1766 else if (o->op_type != OP_METHOD_NAMED
1767 && cSVOPo->op_sv == &PL_sv_undef) {
1768 /* PL_sv_undef is hack - it's unsafe to store it in the
1769 AV that is the pad, because av_fetch treats values of
1770 PL_sv_undef as a "free" AV entry and will merrily
1771 replace them with a new SV, causing pad_alloc to think
1772 that this pad slot is free. (When, clearly, it is not)
1774 SvOK_off(PAD_SVl(ix));
1775 SvPADTMP_on(PAD_SVl(ix));
1776 SvREADONLY_on(PAD_SVl(ix));
1779 SvREFCNT_dec(PAD_SVl(ix));
1780 SvPADTMP_on(cSVOPo->op_sv);
1781 PAD_SETSV(ix, cSVOPo->op_sv);
1782 /* XXX I don't know how this isn't readonly already. */
1783 SvREADONLY_on(PAD_SVl(ix));
1785 cSVOPo->op_sv = NULL;
1796 const char *key = NULL;
1799 if (((BINOP*)o)->op_last->op_type != OP_CONST)
1802 /* Make the CONST have a shared SV */
1803 svp = cSVOPx_svp(((BINOP*)o)->op_last);
1804 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv))
1805 && SvTYPE(sv) < SVt_PVMG && !SvROK(sv)) {
1806 key = SvPV_const(sv, keylen);
1807 lexname = newSVpvn_share(key,
1808 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
1814 if ((o->op_private & (OPpLVAL_INTRO)))
1817 rop = (UNOP*)((BINOP*)o)->op_first;
1818 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
1820 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
1821 if (!SvPAD_TYPED(lexname))
1823 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1824 if (!fields || !GvHV(*fields))
1826 key = SvPV_const(*svp, keylen);
1827 if (!hv_fetch(GvHV(*fields), key,
1828 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1829 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1830 "in variable %"SVf" of type %"HEKf,
1831 SVfARG(*svp), SVfARG(lexname),
1832 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1844 SVOP *first_key_op, *key_op;
1846 if ((o->op_private & (OPpLVAL_INTRO))
1847 /* I bet there's always a pushmark... */
1848 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
1849 /* hmmm, no optimization if list contains only one key. */
1851 rop = (UNOP*)((LISTOP*)o)->op_last;
1852 if (rop->op_type != OP_RV2HV)
1854 if (rop->op_first->op_type == OP_PADSV)
1855 /* @$hash{qw(keys here)} */
1856 rop = (UNOP*)rop->op_first;
1858 /* @{$hash}{qw(keys here)} */
1859 if (rop->op_first->op_type == OP_SCOPE
1860 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1862 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1868 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
1869 if (!SvPAD_TYPED(lexname))
1871 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1872 if (!fields || !GvHV(*fields))
1874 /* Again guessing that the pushmark can be jumped over.... */
1875 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
1876 ->op_first->op_sibling;
1877 for (key_op = first_key_op; key_op;
1878 key_op = (SVOP*)key_op->op_sibling) {
1879 if (key_op->op_type != OP_CONST)
1881 svp = cSVOPx_svp(key_op);
1882 key = SvPV_const(*svp, keylen);
1883 if (!hv_fetch(GvHV(*fields), key,
1884 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1885 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1886 "in variable %"SVf" of type %"HEKf,
1887 SVfARG(*svp), SVfARG(lexname),
1888 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1894 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1895 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1902 if (o->op_flags & OPf_KIDS) {
1904 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1910 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
1912 Propagate lvalue ("modifiable") context to an op and its children.
1913 I<type> represents the context type, roughly based on the type of op that
1914 would do the modifying, although C<local()> is represented by OP_NULL,
1915 because it has no op type of its own (it is signalled by a flag on
1918 This function detects things that can't be modified, such as C<$x+1>, and
1919 generates errors for them. For example, C<$x+1 = 2> would cause it to be
1920 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
1922 It also flags things that need to behave specially in an lvalue context,
1923 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
1929 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
1933 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1936 if (!o || (PL_parser && PL_parser->error_count))
1939 if ((o->op_private & OPpTARGET_MY)
1940 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1945 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
1947 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
1949 switch (o->op_type) {
1954 if ((o->op_flags & OPf_PARENS) || PL_madskills)
1958 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
1959 !(o->op_flags & OPf_STACKED)) {
1960 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1961 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
1962 poses, so we need it clear. */
1963 o->op_private &= ~1;
1964 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1965 assert(cUNOPo->op_first->op_type == OP_NULL);
1966 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1969 else { /* lvalue subroutine call */
1970 o->op_private |= OPpLVAL_INTRO
1971 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
1972 PL_modcount = RETURN_UNLIMITED_NUMBER;
1973 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1974 /* Potential lvalue context: */
1975 o->op_private |= OPpENTERSUB_INARGS;
1978 else { /* Compile-time error message: */
1979 OP *kid = cUNOPo->op_first;
1982 if (kid->op_type != OP_PUSHMARK) {
1983 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1985 "panic: unexpected lvalue entersub "
1986 "args: type/targ %ld:%"UVuf,
1987 (long)kid->op_type, (UV)kid->op_targ);
1988 kid = kLISTOP->op_first;
1990 while (kid->op_sibling)
1991 kid = kid->op_sibling;
1992 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1993 break; /* Postpone until runtime */
1996 kid = kUNOP->op_first;
1997 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1998 kid = kUNOP->op_first;
1999 if (kid->op_type == OP_NULL)
2001 "Unexpected constant lvalue entersub "
2002 "entry via type/targ %ld:%"UVuf,
2003 (long)kid->op_type, (UV)kid->op_targ);
2004 if (kid->op_type != OP_GV) {
2008 cv = GvCV(kGVOP_gv);
2018 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2019 /* grep, foreach, subcalls, refgen */
2020 if (type == OP_GREPSTART || type == OP_ENTERSUB
2021 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2023 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2024 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2026 : (o->op_type == OP_ENTERSUB
2027 ? "non-lvalue subroutine call"
2029 type ? PL_op_desc[type] : "local"));
2043 case OP_RIGHT_SHIFT:
2052 if (!(o->op_flags & OPf_STACKED))
2059 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2060 op_lvalue(kid, type);
2065 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2066 PL_modcount = RETURN_UNLIMITED_NUMBER;
2067 return o; /* Treat \(@foo) like ordinary list. */
2071 if (scalar_mod_type(o, type))
2073 ref(cUNOPo->op_first, o->op_type);
2077 if (type == OP_LEAVESUBLV)
2078 o->op_private |= OPpMAYBE_LVSUB;
2084 PL_modcount = RETURN_UNLIMITED_NUMBER;
2087 PL_hints |= HINT_BLOCK_SCOPE;
2088 if (type == OP_LEAVESUBLV)
2089 o->op_private |= OPpMAYBE_LVSUB;
2093 ref(cUNOPo->op_first, o->op_type);
2097 PL_hints |= HINT_BLOCK_SCOPE;
2106 case OP_AELEMFAST_LEX:
2113 PL_modcount = RETURN_UNLIMITED_NUMBER;
2114 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2115 return o; /* Treat \(@foo) like ordinary list. */
2116 if (scalar_mod_type(o, type))
2118 if (type == OP_LEAVESUBLV)
2119 o->op_private |= OPpMAYBE_LVSUB;
2123 if (!type) /* local() */
2124 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2125 PAD_COMPNAME_SV(o->op_targ));
2134 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2138 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2144 if (type == OP_LEAVESUBLV)
2145 o->op_private |= OPpMAYBE_LVSUB;
2146 pad_free(o->op_targ);
2147 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
2148 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
2149 if (o->op_flags & OPf_KIDS)
2150 op_lvalue(cBINOPo->op_first->op_sibling, type);
2155 ref(cBINOPo->op_first, o->op_type);
2156 if (type == OP_ENTERSUB &&
2157 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2158 o->op_private |= OPpLVAL_DEFER;
2159 if (type == OP_LEAVESUBLV)
2160 o->op_private |= OPpMAYBE_LVSUB;
2170 if (o->op_flags & OPf_KIDS)
2171 op_lvalue(cLISTOPo->op_last, type);
2176 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2178 else if (!(o->op_flags & OPf_KIDS))
2180 if (o->op_targ != OP_LIST) {
2181 op_lvalue(cBINOPo->op_first, type);
2187 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2188 /* elements might be in void context because the list is
2189 in scalar context or because they are attribute sub calls */
2190 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2191 op_lvalue(kid, type);
2195 if (type != OP_LEAVESUBLV)
2197 break; /* op_lvalue()ing was handled by ck_return() */
2203 /* [20011101.069] File test operators interpret OPf_REF to mean that
2204 their argument is a filehandle; thus \stat(".") should not set
2206 if (type == OP_REFGEN &&
2207 PL_check[o->op_type] == Perl_ck_ftst)
2210 if (type != OP_LEAVESUBLV)
2211 o->op_flags |= OPf_MOD;
2213 if (type == OP_AASSIGN || type == OP_SASSIGN)
2214 o->op_flags |= OPf_SPECIAL|OPf_REF;
2215 else if (!type) { /* local() */
2218 o->op_private |= OPpLVAL_INTRO;
2219 o->op_flags &= ~OPf_SPECIAL;
2220 PL_hints |= HINT_BLOCK_SCOPE;
2225 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2226 "Useless localization of %s", OP_DESC(o));
2229 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2230 && type != OP_LEAVESUBLV)
2231 o->op_flags |= OPf_REF;
2236 S_scalar_mod_type(const OP *o, I32 type)
2241 if (o && o->op_type == OP_RV2GV)
2265 case OP_RIGHT_SHIFT:
2286 S_is_handle_constructor(const OP *o, I32 numargs)
2288 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2290 switch (o->op_type) {
2298 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2311 S_refkids(pTHX_ OP *o, I32 type)
2313 if (o && o->op_flags & OPf_KIDS) {
2315 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2322 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2327 PERL_ARGS_ASSERT_DOREF;
2329 if (!o || (PL_parser && PL_parser->error_count))
2332 switch (o->op_type) {
2334 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2335 !(o->op_flags & OPf_STACKED)) {
2336 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2337 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2338 assert(cUNOPo->op_first->op_type == OP_NULL);
2339 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2340 o->op_flags |= OPf_SPECIAL;
2341 o->op_private &= ~1;
2343 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2344 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2345 : type == OP_RV2HV ? OPpDEREF_HV
2347 o->op_flags |= OPf_MOD;
2353 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2354 doref(kid, type, set_op_ref);
2357 if (type == OP_DEFINED)
2358 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2359 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2362 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2363 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2364 : type == OP_RV2HV ? OPpDEREF_HV
2366 o->op_flags |= OPf_MOD;
2373 o->op_flags |= OPf_REF;
2376 if (type == OP_DEFINED)
2377 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2378 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2384 o->op_flags |= OPf_REF;
2389 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
2391 doref(cBINOPo->op_first, type, set_op_ref);
2395 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2396 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2397 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2398 : type == OP_RV2HV ? OPpDEREF_HV
2400 o->op_flags |= OPf_MOD;
2410 if (!(o->op_flags & OPf_KIDS))
2412 doref(cLISTOPo->op_last, type, set_op_ref);
2422 S_dup_attrlist(pTHX_ OP *o)
2427 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2429 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2430 * where the first kid is OP_PUSHMARK and the remaining ones
2431 * are OP_CONST. We need to push the OP_CONST values.
2433 if (o->op_type == OP_CONST)
2434 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2436 else if (o->op_type == OP_NULL)
2440 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2442 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2443 if (o->op_type == OP_CONST)
2444 rop = op_append_elem(OP_LIST, rop,
2445 newSVOP(OP_CONST, o->op_flags,
2446 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2453 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
2456 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2458 PERL_ARGS_ASSERT_APPLY_ATTRS;
2460 /* fake up C<use attributes $pkg,$rv,@attrs> */
2461 ENTER; /* need to protect against side-effects of 'use' */
2463 #define ATTRSMODULE "attributes"
2464 #define ATTRSMODULE_PM "attributes.pm"
2466 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2467 newSVpvs(ATTRSMODULE),
2469 op_prepend_elem(OP_LIST,
2470 newSVOP(OP_CONST, 0, stashsv),
2471 op_prepend_elem(OP_LIST,
2472 newSVOP(OP_CONST, 0,
2474 dup_attrlist(attrs))));
2479 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2482 OP *pack, *imop, *arg;
2483 SV *meth, *stashsv, **svp;
2485 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2490 assert(target->op_type == OP_PADSV ||
2491 target->op_type == OP_PADHV ||
2492 target->op_type == OP_PADAV);
2494 /* Ensure that attributes.pm is loaded. */
2495 ENTER; /* need to protect against side-effects of 'use' */
2496 /* Don't force the C<use> if we don't need it. */
2497 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2498 if (svp && *svp != &PL_sv_undef)
2499 NOOP; /* already in %INC */
2501 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2502 newSVpvs(ATTRSMODULE), NULL);
2505 /* Need package name for method call. */
2506 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2508 /* Build up the real arg-list. */
2509 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2511 arg = newOP(OP_PADSV, 0);
2512 arg->op_targ = target->op_targ;
2513 arg = op_prepend_elem(OP_LIST,
2514 newSVOP(OP_CONST, 0, stashsv),
2515 op_prepend_elem(OP_LIST,
2516 newUNOP(OP_REFGEN, 0,
2517 op_lvalue(arg, OP_REFGEN)),
2518 dup_attrlist(attrs)));
2520 /* Fake up a method call to import */
2521 meth = newSVpvs_share("import");
2522 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2523 op_append_elem(OP_LIST,
2524 op_prepend_elem(OP_LIST, pack, list(arg)),
2525 newSVOP(OP_METHOD_NAMED, 0, meth)));
2527 /* Combine the ops. */
2528 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2532 =notfor apidoc apply_attrs_string
2534 Attempts to apply a list of attributes specified by the C<attrstr> and
2535 C<len> arguments to the subroutine identified by the C<cv> argument which
2536 is expected to be associated with the package identified by the C<stashpv>
2537 argument (see L<attributes>). It gets this wrong, though, in that it
2538 does not correctly identify the boundaries of the individual attribute
2539 specifications within C<attrstr>. This is not really intended for the
2540 public API, but has to be listed here for systems such as AIX which
2541 need an explicit export list for symbols. (It's called from XS code
2542 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2543 to respect attribute syntax properly would be welcome.
2549 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2550 const char *attrstr, STRLEN len)
2554 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2557 len = strlen(attrstr);
2561 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2563 const char * const sstr = attrstr;
2564 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2565 attrs = op_append_elem(OP_LIST, attrs,
2566 newSVOP(OP_CONST, 0,
2567 newSVpvn(sstr, attrstr-sstr)));
2571 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2572 newSVpvs(ATTRSMODULE),
2573 NULL, op_prepend_elem(OP_LIST,
2574 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2575 op_prepend_elem(OP_LIST,
2576 newSVOP(OP_CONST, 0,
2577 newRV(MUTABLE_SV(cv))),
2582 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2586 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2588 PERL_ARGS_ASSERT_MY_KID;
2590 if (!o || (PL_parser && PL_parser->error_count))
2594 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2595 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2599 if (type == OP_LIST) {
2601 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2602 my_kid(kid, attrs, imopsp);
2604 } else if (type == OP_UNDEF || type == OP_STUB) {
2606 } else if (type == OP_RV2SV || /* "our" declaration */
2608 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2609 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2610 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2612 PL_parser->in_my == KEY_our
2614 : PL_parser->in_my == KEY_state ? "state" : "my"));
2616 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2617 PL_parser->in_my = FALSE;
2618 PL_parser->in_my_stash = NULL;
2619 apply_attrs(GvSTASH(gv),
2620 (type == OP_RV2SV ? GvSV(gv) :
2621 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2622 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2625 o->op_private |= OPpOUR_INTRO;
2628 else if (type != OP_PADSV &&
2631 type != OP_PUSHMARK)
2633 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2635 PL_parser->in_my == KEY_our
2637 : PL_parser->in_my == KEY_state ? "state" : "my"));
2640 else if (attrs && type != OP_PUSHMARK) {
2643 PL_parser->in_my = FALSE;
2644 PL_parser->in_my_stash = NULL;
2646 /* check for C<my Dog $spot> when deciding package */
2647 stash = PAD_COMPNAME_TYPE(o->op_targ);
2649 stash = PL_curstash;
2650 apply_attrs_my(stash, o, attrs, imopsp);
2652 o->op_flags |= OPf_MOD;
2653 o->op_private |= OPpLVAL_INTRO;
2655 o->op_private |= OPpPAD_STATE;
2660 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2664 int maybe_scalar = 0;
2666 PERL_ARGS_ASSERT_MY_ATTRS;
2668 /* [perl #17376]: this appears to be premature, and results in code such as
2669 C< our(%x); > executing in list mode rather than void mode */
2671 if (o->op_flags & OPf_PARENS)
2681 o = my_kid(o, attrs, &rops);
2683 if (maybe_scalar && o->op_type == OP_PADSV) {
2684 o = scalar(op_append_list(OP_LIST, rops, o));
2685 o->op_private |= OPpLVAL_INTRO;
2688 /* The listop in rops might have a pushmark at the beginning,
2689 which will mess up list assignment. */
2690 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2691 if (rops->op_type == OP_LIST &&
2692 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2694 OP * const pushmark = lrops->op_first;
2695 lrops->op_first = pushmark->op_sibling;
2698 o = op_append_list(OP_LIST, o, rops);
2701 PL_parser->in_my = FALSE;
2702 PL_parser->in_my_stash = NULL;
2707 Perl_sawparens(pTHX_ OP *o)
2709 PERL_UNUSED_CONTEXT;
2711 o->op_flags |= OPf_PARENS;
2716 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2720 const OPCODE ltype = left->op_type;
2721 const OPCODE rtype = right->op_type;
2723 PERL_ARGS_ASSERT_BIND_MATCH;
2725 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2726 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2728 const char * const desc
2730 rtype == OP_SUBST || rtype == OP_TRANS
2731 || rtype == OP_TRANSR
2733 ? (int)rtype : OP_MATCH];
2734 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2737 (ltype == OP_RV2AV || ltype == OP_RV2HV)
2738 ? cUNOPx(left)->op_first->op_type == OP_GV
2739 && (gv = cGVOPx_gv(cUNOPx(left)->op_first))
2740 ? varname(gv, isary ? '@' : '%', 0, NULL, 0, 1)
2743 (GV *)PL_compcv, isary ? '@' : '%', left->op_targ, NULL, 0, 1
2746 Perl_warner(aTHX_ packWARN(WARN_MISC),
2747 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2750 const char * const sample = (isary
2751 ? "@array" : "%hash");
2752 Perl_warner(aTHX_ packWARN(WARN_MISC),
2753 "Applying %s to %s will act on scalar(%s)",
2754 desc, sample, sample);
2758 if (rtype == OP_CONST &&
2759 cSVOPx(right)->op_private & OPpCONST_BARE &&
2760 cSVOPx(right)->op_private & OPpCONST_STRICT)
2762 no_bareword_allowed(right);
2765 /* !~ doesn't make sense with /r, so error on it for now */
2766 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2768 yyerror("Using !~ with s///r doesn't make sense");
2769 if (rtype == OP_TRANSR && type == OP_NOT)
2770 yyerror("Using !~ with tr///r doesn't make sense");
2772 ismatchop = (rtype == OP_MATCH ||
2773 rtype == OP_SUBST ||
2774 rtype == OP_TRANS || rtype == OP_TRANSR)
2775 && !(right->op_flags & OPf_SPECIAL);
2776 if (ismatchop && right->op_private & OPpTARGET_MY) {
2778 right->op_private &= ~OPpTARGET_MY;
2780 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2783 right->op_flags |= OPf_STACKED;
2784 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
2785 ! (rtype == OP_TRANS &&
2786 right->op_private & OPpTRANS_IDENTICAL) &&
2787 ! (rtype == OP_SUBST &&
2788 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2789 newleft = op_lvalue(left, rtype);
2792 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
2793 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2795 o = op_prepend_elem(rtype, scalar(newleft), right);
2797 return newUNOP(OP_NOT, 0, scalar(o));
2801 return bind_match(type, left,
2802 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
2806 Perl_invert(pTHX_ OP *o)
2810 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2814 =for apidoc Amx|OP *|op_scope|OP *o
2816 Wraps up an op tree with some additional ops so that at runtime a dynamic
2817 scope will be created. The original ops run in the new dynamic scope,
2818 and then, provided that they exit normally, the scope will be unwound.
2819 The additional ops used to create and unwind the dynamic scope will
2820 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
2821 instead if the ops are simple enough to not need the full dynamic scope
2828 Perl_op_scope(pTHX_ OP *o)
2832 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2833 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2834 o->op_type = OP_LEAVE;
2835 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2837 else if (o->op_type == OP_LINESEQ) {
2839 o->op_type = OP_SCOPE;
2840 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2841 kid = ((LISTOP*)o)->op_first;
2842 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2845 /* The following deals with things like 'do {1 for 1}' */
2846 kid = kid->op_sibling;
2848 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2853 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2859 Perl_op_unscope(pTHX_ OP *o)
2861 if (o && o->op_type == OP_LINESEQ) {
2862 OP *kid = cLISTOPo->op_first;
2863 for(; kid; kid = kid->op_sibling)
2864 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2871 Perl_block_start(pTHX_ int full)
2874 const int retval = PL_savestack_ix;
2876 pad_block_start(full);
2878 PL_hints &= ~HINT_BLOCK_SCOPE;
2879 SAVECOMPILEWARNINGS();
2880 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2882 CALL_BLOCK_HOOKS(bhk_start, full);
2888 Perl_block_end(pTHX_ I32 floor, OP *seq)
2891 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2892 OP* retval = scalarseq(seq);
2895 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
2898 CopHINTS_set(&PL_compiling, PL_hints);
2900 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2904 /* pad_leavemy has created a sequence of introcv ops for all my
2905 subs declared in the block. We have to replicate that list with
2906 clonecv ops, to deal with this situation:
2911 sub s1 { state sub foo { \&s2 } }
2914 Originally, I was going to have introcv clone the CV and turn
2915 off the stale flag. Since &s1 is declared before &s2, the
2916 introcv op for &s1 is executed (on sub entry) before the one for
2917 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
2918 cloned, since it is a state sub) closes over &s2 and expects
2919 to see it in its outer CV’s pad. If the introcv op clones &s1,
2920 then &s2 is still marked stale. Since &s1 is not active, and
2921 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
2922 ble will not stay shared’ warning. Because it is the same stub
2923 that will be used when the introcv op for &s2 is executed, clos-
2924 ing over it is safe. Hence, we have to turn off the stale flag
2925 on all lexical subs in the block before we clone any of them.
2926 Hence, having introcv clone the sub cannot work. So we create a
2927 list of ops like this:
2951 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
2952 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
2953 for (;; kid = kid->op_sibling) {
2954 OP *newkid = newOP(OP_CLONECV, 0);
2955 newkid->op_targ = kid->op_targ;
2956 o = op_append_elem(OP_LINESEQ, o, newkid);
2957 if (kid == last) break;
2959 retval = op_prepend_elem(OP_LINESEQ, o, retval);
2962 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
2968 =head1 Compile-time scope hooks
2970 =for apidoc Aox||blockhook_register
2972 Register a set of hooks to be called when the Perl lexical scope changes
2973 at compile time. See L<perlguts/"Compile-time scope hooks">.
2979 Perl_blockhook_register(pTHX_ BHK *hk)
2981 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
2983 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
2990 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
2991 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2992 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2995 OP * const o = newOP(OP_PADSV, 0);
2996 o->op_targ = offset;
3002 Perl_newPROG(pTHX_ OP *o)
3006 PERL_ARGS_ASSERT_NEWPROG;
3013 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3014 ((PL_in_eval & EVAL_KEEPERR)
3015 ? OPf_SPECIAL : 0), o);
3017 cx = &cxstack[cxstack_ix];
3018 assert(CxTYPE(cx) == CXt_EVAL);
3020 if ((cx->blk_gimme & G_WANT) == G_VOID)
3021 scalarvoid(PL_eval_root);
3022 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3025 scalar(PL_eval_root);
3027 PL_eval_start = op_linklist(PL_eval_root);
3028 PL_eval_root->op_private |= OPpREFCOUNTED;
3029 OpREFCNT_set(PL_eval_root, 1);
3030 PL_eval_root->op_next = 0;
3031 i = PL_savestack_ix;
3034 CALL_PEEP(PL_eval_start);
3035 finalize_optree(PL_eval_root);
3037 PL_savestack_ix = i;
3040 if (o->op_type == OP_STUB) {
3041 /* This block is entered if nothing is compiled for the main
3042 program. This will be the case for an genuinely empty main
3043 program, or one which only has BEGIN blocks etc, so already
3046 Historically (5.000) the guard above was !o. However, commit
3047 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3048 c71fccf11fde0068, changed perly.y so that newPROG() is now
3049 called with the output of block_end(), which returns a new
3050 OP_STUB for the case of an empty optree. ByteLoader (and
3051 maybe other things) also take this path, because they set up
3052 PL_main_start and PL_main_root directly, without generating an
3055 If the parsing the main program aborts (due to parse errors,
3056 or due to BEGIN or similar calling exit), then newPROG()
3057 isn't even called, and hence this code path and its cleanups
3058 are skipped. This shouldn't make a make a difference:
3059 * a non-zero return from perl_parse is a failure, and
3060 perl_destruct() should be called immediately.
3061 * however, if exit(0) is called during the parse, then
3062 perl_parse() returns 0, and perl_run() is called. As
3063 PL_main_start will be NULL, perl_run() will return
3064 promptly, and the exit code will remain 0.
3067 PL_comppad_name = 0;
3069 S_op_destroy(aTHX_ o);
3072 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3073 PL_curcop = &PL_compiling;
3074 PL_main_start = LINKLIST(PL_main_root);
3075 PL_main_root->op_private |= OPpREFCOUNTED;
3076 OpREFCNT_set(PL_main_root, 1);
3077 PL_main_root->op_next = 0;
3078 CALL_PEEP(PL_main_start);
3079 finalize_optree(PL_main_root);
3080 cv_forget_slab(PL_compcv);
3083 /* Register with debugger */
3085 CV * const cv = get_cvs("DB::postponed", 0);
3089 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3091 call_sv(MUTABLE_SV(cv), G_DISCARD);
3098 Perl_localize(pTHX_ OP *o, I32 lex)
3102 PERL_ARGS_ASSERT_LOCALIZE;
3104 if (o->op_flags & OPf_PARENS)
3105 /* [perl #17376]: this appears to be premature, and results in code such as
3106 C< our(%x); > executing in list mode rather than void mode */
3113 if ( PL_parser->bufptr > PL_parser->oldbufptr
3114 && PL_parser->bufptr[-1] == ','
3115 && ckWARN(WARN_PARENTHESIS))
3117 char *s = PL_parser->bufptr;
3120 /* some heuristics to detect a potential error */
3121 while (*s && (strchr(", \t\n", *s)))
3125 if (*s && strchr("@$%*", *s) && *++s
3126 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
3129 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
3131 while (*s && (strchr(", \t\n", *s)))
3137 if (sigil && (*s == ';' || *s == '=')) {
3138 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3139 "Parentheses missing around \"%s\" list",
3141 ? (PL_parser->in_my == KEY_our
3143 : PL_parser->in_my == KEY_state
3153 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3154 PL_parser->in_my = FALSE;
3155 PL_parser->in_my_stash = NULL;
3160 Perl_jmaybe(pTHX_ OP *o)
3162 PERL_ARGS_ASSERT_JMAYBE;
3164 if (o->op_type == OP_LIST) {
3166 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3167 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3172 PERL_STATIC_INLINE OP *
3173 S_op_std_init(pTHX_ OP *o)
3175 I32 type = o->op_type;
3177 PERL_ARGS_ASSERT_OP_STD_INIT;
3179 if (PL_opargs[type] & OA_RETSCALAR)
3181 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3182 o->op_targ = pad_alloc(type, SVs_PADTMP);
3187 PERL_STATIC_INLINE OP *
3188 S_op_integerize(pTHX_ OP *o)
3190 I32 type = o->op_type;
3192 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3194 /* integerize op. */
3195 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3198 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
3201 if (type == OP_NEGATE)
3202 /* XXX might want a ck_negate() for this */
3203 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3209 S_fold_constants(pTHX_ register OP *o)
3214 VOL I32 type = o->op_type;
3219 SV * const oldwarnhook = PL_warnhook;
3220 SV * const olddiehook = PL_diehook;
3224 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3226 if (!(PL_opargs[type] & OA_FOLDCONST))
3240 /* XXX what about the numeric ops? */
3241 if (IN_LOCALE_COMPILETIME)
3245 if (!cLISTOPo->op_first->op_sibling
3246 || cLISTOPo->op_first->op_sibling->op_type != OP_CONST)
3249 SV * const sv = cSVOPx_sv(cLISTOPo->op_first->op_sibling);
3250 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3252 const char *s = SvPVX_const(sv);
3253 while (s < SvEND(sv)) {
3254 if (*s == 'p' || *s == 'P') goto nope;
3261 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3264 if (PL_parser && PL_parser->error_count)
3265 goto nope; /* Don't try to run w/ errors */
3267 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3268 const OPCODE type = curop->op_type;
3269 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3271 type != OP_SCALAR &&
3273 type != OP_PUSHMARK)
3279 curop = LINKLIST(o);
3280 old_next = o->op_next;
3284 oldscope = PL_scopestack_ix;
3285 create_eval_scope(G_FAKINGEVAL);
3287 /* Verify that we don't need to save it: */
3288 assert(PL_curcop == &PL_compiling);
3289 StructCopy(&PL_compiling, ¬_compiling, COP);
3290 PL_curcop = ¬_compiling;
3291 /* The above ensures that we run with all the correct hints of the
3292 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3293 assert(IN_PERL_RUNTIME);
3294 PL_warnhook = PERL_WARNHOOK_FATAL;
3301 sv = *(PL_stack_sp--);
3302 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3304 /* Can't simply swipe the SV from the pad, because that relies on
3305 the op being freed "real soon now". Under MAD, this doesn't
3306 happen (see the #ifdef below). */
3309 pad_swipe(o->op_targ, FALSE);
3312 else if (SvTEMP(sv)) { /* grab mortal temp? */
3313 SvREFCNT_inc_simple_void(sv);
3318 /* Something tried to die. Abandon constant folding. */
3319 /* Pretend the error never happened. */
3321 o->op_next = old_next;
3325 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3326 PL_warnhook = oldwarnhook;
3327 PL_diehook = olddiehook;
3328 /* XXX note that this croak may fail as we've already blown away
3329 * the stack - eg any nested evals */
3330 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3333 PL_warnhook = oldwarnhook;
3334 PL_diehook = olddiehook;
3335 PL_curcop = &PL_compiling;
3337 if (PL_scopestack_ix > oldscope)
3338 delete_eval_scope();
3347 if (type == OP_RV2GV)
3348 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3350 newop = newSVOP(OP_CONST, OPpCONST_FOLDED<<8, MUTABLE_SV(sv));
3351 op_getmad(o,newop,'f');
3359 S_gen_constant_list(pTHX_ register OP *o)
3363 const I32 oldtmps_floor = PL_tmps_floor;
3366 if (PL_parser && PL_parser->error_count)
3367 return o; /* Don't attempt to run with errors */
3369 PL_op = curop = LINKLIST(o);
3372 Perl_pp_pushmark(aTHX);
3375 assert (!(curop->op_flags & OPf_SPECIAL));
3376 assert(curop->op_type == OP_RANGE);
3377 Perl_pp_anonlist(aTHX);
3378 PL_tmps_floor = oldtmps_floor;
3380 o->op_type = OP_RV2AV;
3381 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3382 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3383 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3384 o->op_opt = 0; /* needs to be revisited in rpeep() */
3385 curop = ((UNOP*)o)->op_first;
3386 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
3388 op_getmad(curop,o,'O');
3397 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3400 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3401 if (!o || o->op_type != OP_LIST)
3402 o = newLISTOP(OP_LIST, 0, o, NULL);
3404 o->op_flags &= ~OPf_WANT;
3406 if (!(PL_opargs[type] & OA_MARK))
3407 op_null(cLISTOPo->op_first);
3409 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3410 if (kid2 && kid2->op_type == OP_COREARGS) {
3411 op_null(cLISTOPo->op_first);
3412 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3416 o->op_type = (OPCODE)type;
3417 o->op_ppaddr = PL_ppaddr[type];
3418 o->op_flags |= flags;
3420 o = CHECKOP(type, o);
3421 if (o->op_type != (unsigned)type)
3424 return fold_constants(op_integerize(op_std_init(o)));
3428 =head1 Optree Manipulation Functions
3431 /* List constructors */
3434 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3436 Append an item to the list of ops contained directly within a list-type
3437 op, returning the lengthened list. I<first> is the list-type op,
3438 and I<last> is the op to append to the list. I<optype> specifies the
3439 intended opcode for the list. If I<first> is not already a list of the
3440 right type, it will be upgraded into one. If either I<first> or I<last>
3441 is null, the other is returned unchanged.
3447 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3455 if (first->op_type != (unsigned)type
3456 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3458 return newLISTOP(type, 0, first, last);
3461 if (first->op_flags & OPf_KIDS)
3462 ((LISTOP*)first)->op_last->op_sibling = last;
3464 first->op_flags |= OPf_KIDS;
3465 ((LISTOP*)first)->op_first = last;
3467 ((LISTOP*)first)->op_last = last;
3472 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3474 Concatenate the lists of ops contained directly within two list-type ops,
3475 returning the combined list. I<first> and I<last> are the list-type ops
3476 to concatenate. I<optype> specifies the intended opcode for the list.
3477 If either I<first> or I<last> is not already a list of the right type,
3478 it will be upgraded into one. If either I<first> or I<last> is null,
3479 the other is returned unchanged.
3485 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3493 if (first->op_type != (unsigned)type)
3494 return op_prepend_elem(type, first, last);
3496 if (last->op_type != (unsigned)type)
3497 return op_append_elem(type, first, last);
3499 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3500 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3501 first->op_flags |= (last->op_flags & OPf_KIDS);
3504 if (((LISTOP*)last)->op_first && first->op_madprop) {
3505 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3507 while (mp->mad_next)
3509 mp->mad_next = first->op_madprop;
3512 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3515 first->op_madprop = last->op_madprop;
3516 last->op_madprop = 0;
3519 S_op_destroy(aTHX_ last);
3525 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3527 Prepend an item to the list of ops contained directly within a list-type
3528 op, returning the lengthened list. I<first> is the op to prepend to the
3529 list, and I<last> is the list-type op. I<optype> specifies the intended
3530 opcode for the list. If I<last> is not already a list of the right type,
3531 it will be upgraded into one. If either I<first> or I<last> is null,
3532 the other is returned unchanged.
3538 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3546 if (last->op_type == (unsigned)type) {
3547 if (type == OP_LIST) { /* already a PUSHMARK there */
3548 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3549 ((LISTOP*)last)->op_first->op_sibling = first;
3550 if (!(first->op_flags & OPf_PARENS))
3551 last->op_flags &= ~OPf_PARENS;
3554 if (!(last->op_flags & OPf_KIDS)) {
3555 ((LISTOP*)last)->op_last = first;
3556 last->op_flags |= OPf_KIDS;
3558 first->op_sibling = ((LISTOP*)last)->op_first;
3559 ((LISTOP*)last)->op_first = first;
3561 last->op_flags |= OPf_KIDS;
3565 return newLISTOP(type, 0, first, last);
3573 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3576 Newxz(tk, 1, TOKEN);
3577 tk->tk_type = (OPCODE)optype;
3578 tk->tk_type = 12345;
3580 tk->tk_mad = madprop;
3585 Perl_token_free(pTHX_ TOKEN* tk)
3587 PERL_ARGS_ASSERT_TOKEN_FREE;
3589 if (tk->tk_type != 12345)
3591 mad_free(tk->tk_mad);
3596 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3601 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3603 if (tk->tk_type != 12345) {
3604 Perl_warner(aTHX_ packWARN(WARN_MISC),
3605 "Invalid TOKEN object ignored");
3612 /* faked up qw list? */
3614 tm->mad_type == MAD_SV &&
3615 SvPVX((SV *)tm->mad_val)[0] == 'q')
3622 /* pretend constant fold didn't happen? */
3623 if (mp->mad_key == 'f' &&
3624 (o->op_type == OP_CONST ||
3625 o->op_type == OP_GV) )
3627 token_getmad(tk,(OP*)mp->mad_val,slot);
3641 if (mp->mad_key == 'X')
3642 mp->mad_key = slot; /* just change the first one */
3652 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3661 /* pretend constant fold didn't happen? */
3662 if (mp->mad_key == 'f' &&
3663 (o->op_type == OP_CONST ||
3664 o->op_type == OP_GV) )
3666 op_getmad(from,(OP*)mp->mad_val,slot);
3673 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3676 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3682 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3691 /* pretend constant fold didn't happen? */
3692 if (mp->mad_key == 'f' &&
3693 (o->op_type == OP_CONST ||
3694 o->op_type == OP_GV) )
3696 op_getmad(from,(OP*)mp->mad_val,slot);
3703 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
3706 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
3710 PerlIO_printf(PerlIO_stderr(),
3711 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
3717 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
3735 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
3739 addmad(tm, &(o->op_madprop), slot);
3743 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
3764 Perl_newMADsv(pTHX_ char key, SV* sv)
3766 PERL_ARGS_ASSERT_NEWMADSV;
3768 return newMADPROP(key, MAD_SV, sv, 0);
3772 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3774 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
3777 mp->mad_vlen = vlen;
3778 mp->mad_type = type;
3780 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
3785 Perl_mad_free(pTHX_ MADPROP* mp)
3787 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3791 mad_free(mp->mad_next);
3792 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3793 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3794 switch (mp->mad_type) {
3798 Safefree((char*)mp->mad_val);
3801 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
3802 op_free((OP*)mp->mad_val);
3805 sv_free(MUTABLE_SV(mp->mad_val));
3808 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3811 PerlMemShared_free(mp);
3817 =head1 Optree construction
3819 =for apidoc Am|OP *|newNULLLIST
3821 Constructs, checks, and returns a new C<stub> op, which represents an
3822 empty list expression.
3828 Perl_newNULLLIST(pTHX)
3830 return newOP(OP_STUB, 0);
3834 S_force_list(pTHX_ OP *o)
3836 if (!o || o->op_type != OP_LIST)
3837 o = newLISTOP(OP_LIST, 0, o, NULL);
3843 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3845 Constructs, checks, and returns an op of any list type. I<type> is
3846 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3847 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3848 supply up to two ops to be direct children of the list op; they are
3849 consumed by this function and become part of the constructed op tree.
3855 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3860 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3862 NewOp(1101, listop, 1, LISTOP);
3864 listop->op_type = (OPCODE)type;
3865 listop->op_ppaddr = PL_ppaddr[type];
3868 listop->op_flags = (U8)flags;
3872 else if (!first && last)
3875 first->op_sibling = last;
3876 listop->op_first = first;
3877 listop->op_last = last;
3878 if (type == OP_LIST) {
3879 OP* const pushop = newOP(OP_PUSHMARK, 0);
3880 pushop->op_sibling = first;
3881 listop->op_first = pushop;
3882 listop->op_flags |= OPf_KIDS;
3884 listop->op_last = pushop;
3887 return CHECKOP(type, listop);
3891 =for apidoc Am|OP *|newOP|I32 type|I32 flags
3893 Constructs, checks, and returns an op of any base type (any type that
3894 has no extra fields). I<type> is the opcode. I<flags> gives the
3895 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3902 Perl_newOP(pTHX_ I32 type, I32 flags)
3907 if (type == -OP_ENTEREVAL) {
3908 type = OP_ENTEREVAL;
3909 flags |= OPpEVAL_BYTES<<8;
3912 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3913 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3914 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3915 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3917 NewOp(1101, o, 1, OP);
3918 o->op_type = (OPCODE)type;
3919 o->op_ppaddr = PL_ppaddr[type];
3920 o->op_flags = (U8)flags;
3923 o->op_private = (U8)(0 | (flags >> 8));
3924 if (PL_opargs[type] & OA_RETSCALAR)
3926 if (PL_opargs[type] & OA_TARGET)
3927 o->op_targ = pad_alloc(type, SVs_PADTMP);
3928 return CHECKOP(type, o);
3932 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3934 Constructs, checks, and returns an op of any unary type. I<type> is
3935 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3936 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3937 bits, the eight bits of C<op_private>, except that the bit with value 1
3938 is automatically set. I<first> supplies an optional op to be the direct
3939 child of the unary op; it is consumed by this function and become part
3940 of the constructed op tree.
3946 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3951 if (type == -OP_ENTEREVAL) {
3952 type = OP_ENTEREVAL;
3953 flags |= OPpEVAL_BYTES<<8;
3956 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3957 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3958 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3959 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3960 || type == OP_SASSIGN
3961 || type == OP_ENTERTRY
3962 || type == OP_NULL );
3965 first = newOP(OP_STUB, 0);
3966 if (PL_opargs[type] & OA_MARK)
3967 first = force_list(first);
3969 NewOp(1101, unop, 1, UNOP);
3970 unop->op_type = (OPCODE)type;
3971 unop->op_ppaddr = PL_ppaddr[type];
3972 unop->op_first = first;
3973 unop->op_flags = (U8)(flags | OPf_KIDS);
3974 unop->op_private = (U8)(1 | (flags >> 8));
3975 unop = (UNOP*) CHECKOP(type, unop);
3979 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3983 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3985 Constructs, checks, and returns an op of any binary type. I<type>
3986 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3987 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3988 the eight bits of C<op_private>, except that the bit with value 1 or
3989 2 is automatically set as required. I<first> and I<last> supply up to
3990 two ops to be the direct children of the binary op; they are consumed
3991 by this function and become part of the constructed op tree.
3997 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4002 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4003 || type == OP_SASSIGN || type == OP_NULL );
4005 NewOp(1101, binop, 1, BINOP);
4008 first = newOP(OP_NULL, 0);
4010 binop->op_type = (OPCODE)type;
4011 binop->op_ppaddr = PL_ppaddr[type];
4012 binop->op_first = first;
4013 binop->op_flags = (U8)(flags | OPf_KIDS);
4016 binop->op_private = (U8)(1 | (flags >> 8));
4019 binop->op_private = (U8)(2 | (flags >> 8));
4020 first->op_sibling = last;
4023 binop = (BINOP*)CHECKOP(type, binop);
4024 if (binop->op_next || binop->op_type != (OPCODE)type)
4027 binop->op_last = binop->op_first->op_sibling;
4029 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4032 static int uvcompare(const void *a, const void *b)
4033 __attribute__nonnull__(1)
4034 __attribute__nonnull__(2)
4035 __attribute__pure__;
4036 static int uvcompare(const void *a, const void *b)
4038 if (*((const UV *)a) < (*(const UV *)b))
4040 if (*((const UV *)a) > (*(const UV *)b))
4042 if (*((const UV *)a+1) < (*(const UV *)b+1))
4044 if (*((const UV *)a+1) > (*(const UV *)b+1))
4050 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4053 SV * const tstr = ((SVOP*)expr)->op_sv;
4056 (repl->op_type == OP_NULL)
4057 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
4059 ((SVOP*)repl)->op_sv;
4062 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4063 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4069 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4070 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4071 I32 del = o->op_private & OPpTRANS_DELETE;
4074 PERL_ARGS_ASSERT_PMTRANS;
4076 PL_hints |= HINT_BLOCK_SCOPE;
4079 o->op_private |= OPpTRANS_FROM_UTF;
4082 o->op_private |= OPpTRANS_TO_UTF;
4084 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4085 SV* const listsv = newSVpvs("# comment\n");
4087 const U8* tend = t + tlen;
4088 const U8* rend = r + rlen;
4102 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4103 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4106 const U32 flags = UTF8_ALLOW_DEFAULT;
4110 t = tsave = bytes_to_utf8(t, &len);
4113 if (!to_utf && rlen) {
4115 r = rsave = bytes_to_utf8(r, &len);
4119 /* There are several snags with this code on EBCDIC:
4120 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
4121 2. scan_const() in toke.c has encoded chars in native encoding which makes
4122 ranges at least in EBCDIC 0..255 range the bottom odd.
4126 U8 tmpbuf[UTF8_MAXBYTES+1];
4129 Newx(cp, 2*tlen, UV);
4131 transv = newSVpvs("");
4133 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4135 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
4137 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4141 cp[2*i+1] = cp[2*i];
4145 qsort(cp, i, 2*sizeof(UV), uvcompare);
4146 for (j = 0; j < i; j++) {
4148 diff = val - nextmin;
4150 t = uvuni_to_utf8(tmpbuf,nextmin);
4151 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4153 U8 range_mark = UTF_TO_NATIVE(0xff);
4154 t = uvuni_to_utf8(tmpbuf, val - 1);
4155 sv_catpvn(transv, (char *)&range_mark, 1);
4156 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4163 t = uvuni_to_utf8(tmpbuf,nextmin);
4164 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4166 U8 range_mark = UTF_TO_NATIVE(0xff);
4167 sv_catpvn(transv, (char *)&range_mark, 1);
4169 t = uvuni_to_utf8(tmpbuf, 0x7fffffff);
4170 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4171 t = (const U8*)SvPVX_const(transv);
4172 tlen = SvCUR(transv);
4176 else if (!rlen && !del) {
4177 r = t; rlen = tlen; rend = tend;
4180 if ((!rlen && !del) || t == r ||
4181 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4183 o->op_private |= OPpTRANS_IDENTICAL;
4187 while (t < tend || tfirst <= tlast) {
4188 /* see if we need more "t" chars */
4189 if (tfirst > tlast) {
4190 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4192 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
4194 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4201 /* now see if we need more "r" chars */
4202 if (rfirst > rlast) {
4204 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4206 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
4208 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4217 rfirst = rlast = 0xffffffff;
4221 /* now see which range will peter our first, if either. */
4222 tdiff = tlast - tfirst;
4223 rdiff = rlast - rfirst;
4230 if (rfirst == 0xffffffff) {
4231 diff = tdiff; /* oops, pretend rdiff is infinite */
4233 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4234 (long)tfirst, (long)tlast);
4236 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4240 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4241 (long)tfirst, (long)(tfirst + diff),
4244 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4245 (long)tfirst, (long)rfirst);
4247 if (rfirst + diff > max)
4248 max = rfirst + diff;
4250 grows = (tfirst < rfirst &&
4251 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4263 else if (max > 0xff)
4268 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4270 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
4271 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4272 PAD_SETSV(cPADOPo->op_padix, swash);
4274 SvREADONLY_on(swash);
4276 cSVOPo->op_sv = swash;
4278 SvREFCNT_dec(listsv);
4279 SvREFCNT_dec(transv);
4281 if (!del && havefinal && rlen)
4282 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4283 newSVuv((UV)final), 0);
4286 o->op_private |= OPpTRANS_GROWS;
4292 op_getmad(expr,o,'e');
4293 op_getmad(repl,o,'r');
4301 tbl = (short*)PerlMemShared_calloc(
4302 (o->op_private & OPpTRANS_COMPLEMENT) &&
4303 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4305 cPVOPo->op_pv = (char*)tbl;
4307 for (i = 0; i < (I32)tlen; i++)
4309 for (i = 0, j = 0; i < 256; i++) {
4311 if (j >= (I32)rlen) {
4320 if (i < 128 && r[j] >= 128)
4330 o->op_private |= OPpTRANS_IDENTICAL;
4332 else if (j >= (I32)rlen)
4337 PerlMemShared_realloc(tbl,
4338 (0x101+rlen-j) * sizeof(short));
4339 cPVOPo->op_pv = (char*)tbl;
4341 tbl[0x100] = (short)(rlen - j);
4342 for (i=0; i < (I32)rlen - j; i++)
4343 tbl[0x101+i] = r[j+i];
4347 if (!rlen && !del) {
4350 o->op_private |= OPpTRANS_IDENTICAL;
4352 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4353 o->op_private |= OPpTRANS_IDENTICAL;
4355 for (i = 0; i < 256; i++)
4357 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4358 if (j >= (I32)rlen) {
4360 if (tbl[t[i]] == -1)
4366 if (tbl[t[i]] == -1) {
4367 if (t[i] < 128 && r[j] >= 128)
4374 if(del && rlen == tlen) {
4375 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4376 } else if(rlen > tlen) {
4377 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4381 o->op_private |= OPpTRANS_GROWS;
4383 op_getmad(expr,o,'e');
4384 op_getmad(repl,o,'r');
4394 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4396 Constructs, checks, and returns an op of any pattern matching type.
4397 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4398 and, shifted up eight bits, the eight bits of C<op_private>.
4404 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4409 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4411 NewOp(1101, pmop, 1, PMOP);
4412 pmop->op_type = (OPCODE)type;
4413 pmop->op_ppaddr = PL_ppaddr[type];
4414 pmop->op_flags = (U8)flags;
4415 pmop->op_private = (U8)(0 | (flags >> 8));
4417 if (PL_hints & HINT_RE_TAINT)
4418 pmop->op_pmflags |= PMf_RETAINT;
4419 if (IN_LOCALE_COMPILETIME) {
4420 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4422 else if ((! (PL_hints & HINT_BYTES))
4423 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4424 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4426 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4428 if (PL_hints & HINT_RE_FLAGS) {
4429 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4430 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4432 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4433 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4434 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4436 if (reflags && SvOK(reflags)) {
4437 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4443 assert(SvPOK(PL_regex_pad[0]));
4444 if (SvCUR(PL_regex_pad[0])) {
4445 /* Pop off the "packed" IV from the end. */
4446 SV *const repointer_list = PL_regex_pad[0];
4447 const char *p = SvEND(repointer_list) - sizeof(IV);
4448 const IV offset = *((IV*)p);
4450 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4452 SvEND_set(repointer_list, p);
4454 pmop->op_pmoffset = offset;
4455 /* This slot should be free, so assert this: */
4456 assert(PL_regex_pad[offset] == &PL_sv_undef);
4458 SV * const repointer = &PL_sv_undef;
4459 av_push(PL_regex_padav, repointer);
4460 pmop->op_pmoffset = av_len(PL_regex_padav);
4461 PL_regex_pad = AvARRAY(PL_regex_padav);
4465 return CHECKOP(type, pmop);
4468 /* Given some sort of match op o, and an expression expr containing a
4469 * pattern, either compile expr into a regex and attach it to o (if it's
4470 * constant), or convert expr into a runtime regcomp op sequence (if it's
4473 * isreg indicates that the pattern is part of a regex construct, eg
4474 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4475 * split "pattern", which aren't. In the former case, expr will be a list
4476 * if the pattern contains more than one term (eg /a$b/) or if it contains
4477 * a replacement, ie s/// or tr///.
4479 * When the pattern has been compiled within a new anon CV (for
4480 * qr/(?{...})/ ), then floor indicates the savestack level just before
4481 * the new sub was created
4485 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4490 I32 repl_has_vars = 0;
4492 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4493 bool is_compiletime;
4496 PERL_ARGS_ASSERT_PMRUNTIME;
4498 /* for s/// and tr///, last element in list is the replacement; pop it */
4500 if (is_trans || o->op_type == OP_SUBST) {
4502 repl = cLISTOPx(expr)->op_last;
4503 kid = cLISTOPx(expr)->op_first;
4504 while (kid->op_sibling != repl)
4505 kid = kid->op_sibling;
4506 kid->op_sibling = NULL;
4507 cLISTOPx(expr)->op_last = kid;
4510 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4513 OP* const oe = expr;
4514 assert(expr->op_type == OP_LIST);
4515 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4516 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4517 expr = cLISTOPx(oe)->op_last;
4518 cLISTOPx(oe)->op_first->op_sibling = NULL;
4519 cLISTOPx(oe)->op_last = NULL;
4522 return pmtrans(o, expr, repl);
4525 /* find whether we have any runtime or code elements;
4526 * at the same time, temporarily set the op_next of each DO block;
4527 * then when we LINKLIST, this will cause the DO blocks to be excluded
4528 * from the op_next chain (and from having LINKLIST recursively
4529 * applied to them). We fix up the DOs specially later */
4533 if (expr->op_type == OP_LIST) {
4535 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4536 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4538 assert(!o->op_next && o->op_sibling);
4539 o->op_next = o->op_sibling;
4541 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4545 else if (expr->op_type != OP_CONST)
4550 /* fix up DO blocks; treat each one as a separate little sub */
4552 if (expr->op_type == OP_LIST) {
4554 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4555 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4557 o->op_next = NULL; /* undo temporary hack from above */
4560 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4561 LISTOP *leave = cLISTOPx(cLISTOPo->op_first);
4563 assert(leave->op_first->op_type == OP_ENTER);
4564 assert(leave->op_first->op_sibling);
4565 o->op_next = leave->op_first->op_sibling;
4567 assert(leave->op_flags & OPf_KIDS);
4568 assert(leave->op_last->op_next = (OP*)leave);
4569 leave->op_next = NULL; /* stop on last op */
4570 op_null((OP*)leave);
4574 OP *scope = cLISTOPo->op_first;
4575 assert(scope->op_type == OP_SCOPE);
4576 assert(scope->op_flags & OPf_KIDS);
4577 scope->op_next = NULL; /* stop on last op */
4580 /* have to peep the DOs individually as we've removed it from
4581 * the op_next chain */
4584 /* runtime finalizes as part of finalizing whole tree */
4589 PL_hints |= HINT_BLOCK_SCOPE;
4591 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4593 if (is_compiletime) {
4594 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4595 regexp_engine const *eng = current_re_engine();
4597 if (!has_code || !eng->op_comp) {
4598 /* compile-time simple constant pattern */
4600 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4601 /* whoops! we guessed that a qr// had a code block, but we
4602 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4603 * that isn't required now. Note that we have to be pretty
4604 * confident that nothing used that CV's pad while the
4605 * regex was parsed */
4606 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4607 /* But we know that one op is using this CV's slab. */
4608 cv_forget_slab(PL_compcv);
4610 pm->op_pmflags &= ~PMf_HAS_CV;
4615 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4616 rx_flags, pm->op_pmflags)
4617 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4618 rx_flags, pm->op_pmflags)
4621 op_getmad(expr,(OP*)pm,'e');
4627 /* compile-time pattern that includes literal code blocks */
4628 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4631 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4634 if (pm->op_pmflags & PMf_HAS_CV) {
4636 /* this QR op (and the anon sub we embed it in) is never
4637 * actually executed. It's just a placeholder where we can
4638 * squirrel away expr in op_code_list without the peephole
4639 * optimiser etc processing it for a second time */
4640 OP *qr = newPMOP(OP_QR, 0);
4641 ((PMOP*)qr)->op_code_list = expr;
4643 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4644 SvREFCNT_inc_simple_void(PL_compcv);
4645 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4646 ((struct regexp *)SvANY(re))->qr_anoncv = cv;
4648 /* attach the anon CV to the pad so that
4649 * pad_fixup_inner_anons() can find it */
4650 (void)pad_add_anon(cv, o->op_type);
4651 SvREFCNT_inc_simple_void(cv);
4654 pm->op_code_list = expr;
4659 /* runtime pattern: build chain of regcomp etc ops */
4661 PADOFFSET cv_targ = 0;
4663 reglist = isreg && expr->op_type == OP_LIST;
4668 pm->op_code_list = expr;
4669 /* don't free op_code_list; its ops are embedded elsewhere too */
4670 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
4673 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
4674 * to allow its op_next to be pointed past the regcomp and
4675 * preceding stacking ops;
4676 * OP_REGCRESET is there to reset taint before executing the
4678 if (pm->op_pmflags & PMf_KEEP || PL_tainting)
4679 expr = newUNOP((PL_tainting ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
4681 if (pm->op_pmflags & PMf_HAS_CV) {
4682 /* we have a runtime qr with literal code. This means
4683 * that the qr// has been wrapped in a new CV, which
4684 * means that runtime consts, vars etc will have been compiled
4685 * against a new pad. So... we need to execute those ops
4686 * within the environment of the new CV. So wrap them in a call
4687 * to a new anon sub. i.e. for
4691 * we build an anon sub that looks like
4693 * sub { "a", $b, '(?{...})' }
4695 * and call it, passing the returned list to regcomp.
4696 * Or to put it another way, the list of ops that get executed
4700 * ------ -------------------
4701 * pushmark (for regcomp)
4702 * pushmark (for entersub)
4703 * pushmark (for refgen)
4707 * regcreset regcreset
4709 * const("a") const("a")
4711 * const("(?{...})") const("(?{...})")
4716 SvREFCNT_inc_simple_void(PL_compcv);
4717 /* these lines are just an unrolled newANONATTRSUB */
4718 expr = newSVOP(OP_ANONCODE, 0,
4719 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
4720 cv_targ = expr->op_targ;
4721 expr = newUNOP(OP_REFGEN, 0, expr);
4723 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
4726 NewOp(1101, rcop, 1, LOGOP);
4727 rcop->op_type = OP_REGCOMP;
4728 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4729 rcop->op_first = scalar(expr);
4730 rcop->op_flags |= OPf_KIDS
4731 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4732 | (reglist ? OPf_STACKED : 0);
4733 rcop->op_private = 0;
4735 rcop->op_targ = cv_targ;
4737 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4738 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4740 /* establish postfix order */
4741 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
4743 rcop->op_next = expr;
4744 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4747 rcop->op_next = LINKLIST(expr);
4748 expr->op_next = (OP*)rcop;
4751 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4756 if (pm->op_pmflags & PMf_EVAL) {
4758 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
4759 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
4761 else if (repl->op_type == OP_CONST)
4765 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
4766 if (curop->op_type == OP_SCOPE
4767 || curop->op_type == OP_LEAVE
4768 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
4769 if (curop->op_type == OP_GV) {
4770 GV * const gv = cGVOPx_gv(curop);
4772 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
4775 else if (curop->op_type == OP_RV2CV)
4777 else if (curop->op_type == OP_RV2SV ||
4778 curop->op_type == OP_RV2AV ||
4779 curop->op_type == OP_RV2HV ||
4780 curop->op_type == OP_RV2GV) {
4781 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
4784 else if (curop->op_type == OP_PADSV ||
4785 curop->op_type == OP_PADAV ||
4786 curop->op_type == OP_PADHV ||
4787 curop->op_type == OP_PADANY)
4791 else if (curop->op_type == OP_PUSHRE)
4792 NOOP; /* Okay here, dangerous in newASSIGNOP */
4802 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4804 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4805 op_prepend_elem(o->op_type, scalar(repl), o);
4808 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
4809 pm->op_pmflags |= PMf_MAYBE_CONST;
4811 NewOp(1101, rcop, 1, LOGOP);
4812 rcop->op_type = OP_SUBSTCONT;
4813 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4814 rcop->op_first = scalar(repl);
4815 rcop->op_flags |= OPf_KIDS;
4816 rcop->op_private = 1;