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 /* remove any leading "empty" ops from the op_next chain whose first
113 * node's address is stored in op_p. Store the updated address of the
114 * first node in op_p.
118 S_prune_chain_head(OP** op_p)
121 && ( (*op_p)->op_type == OP_NULL
122 || (*op_p)->op_type == OP_SCOPE
123 || (*op_p)->op_type == OP_SCALAR
124 || (*op_p)->op_type == OP_LINESEQ)
126 *op_p = (*op_p)->op_next;
130 /* See the explanatory comments above struct opslab in op.h. */
132 #ifdef PERL_DEBUG_READONLY_OPS
133 # define PERL_SLAB_SIZE 128
134 # define PERL_MAX_SLAB_SIZE 4096
135 # include <sys/mman.h>
138 #ifndef PERL_SLAB_SIZE
139 # define PERL_SLAB_SIZE 64
141 #ifndef PERL_MAX_SLAB_SIZE
142 # define PERL_MAX_SLAB_SIZE 2048
145 /* rounds up to nearest pointer */
146 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
147 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
150 S_new_slab(pTHX_ size_t sz)
152 #ifdef PERL_DEBUG_READONLY_OPS
153 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
154 PROT_READ|PROT_WRITE,
155 MAP_ANON|MAP_PRIVATE, -1, 0);
156 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
157 (unsigned long) sz, slab));
158 if (slab == MAP_FAILED) {
159 perror("mmap failed");
162 slab->opslab_size = (U16)sz;
164 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
167 /* The context is unused in non-Windows */
170 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
174 /* requires double parens and aTHX_ */
175 #define DEBUG_S_warn(args) \
177 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
181 Perl_Slab_Alloc(pTHX_ size_t sz)
189 /* We only allocate ops from the slab during subroutine compilation.
190 We find the slab via PL_compcv, hence that must be non-NULL. It could
191 also be pointing to a subroutine which is now fully set up (CvROOT()
192 pointing to the top of the optree for that sub), or a subroutine
193 which isn't using the slab allocator. If our sanity checks aren't met,
194 don't use a slab, but allocate the OP directly from the heap. */
195 if (!PL_compcv || CvROOT(PL_compcv)
196 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
198 o = (OP*)PerlMemShared_calloc(1, sz);
202 /* While the subroutine is under construction, the slabs are accessed via
203 CvSTART(), to avoid needing to expand PVCV by one pointer for something
204 unneeded at runtime. Once a subroutine is constructed, the slabs are
205 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
206 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
208 if (!CvSTART(PL_compcv)) {
210 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
211 CvSLABBED_on(PL_compcv);
212 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
214 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
216 opsz = SIZE_TO_PSIZE(sz);
217 sz = opsz + OPSLOT_HEADER_P;
219 /* The slabs maintain a free list of OPs. In particular, constant folding
220 will free up OPs, so it makes sense to re-use them where possible. A
221 freed up slot is used in preference to a new allocation. */
222 if (slab->opslab_freed) {
223 OP **too = &slab->opslab_freed;
225 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
226 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
227 DEBUG_S_warn((aTHX_ "Alas! too small"));
228 o = *(too = &o->op_next);
229 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
233 Zero(o, opsz, I32 *);
239 #define INIT_OPSLOT \
240 slot->opslot_slab = slab; \
241 slot->opslot_next = slab2->opslab_first; \
242 slab2->opslab_first = slot; \
243 o = &slot->opslot_op; \
246 /* The partially-filled slab is next in the chain. */
247 slab2 = slab->opslab_next ? slab->opslab_next : slab;
248 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
249 /* Remaining space is too small. */
251 /* If we can fit a BASEOP, add it to the free chain, so as not
253 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
254 slot = &slab2->opslab_slots;
256 o->op_type = OP_FREED;
257 o->op_next = slab->opslab_freed;
258 slab->opslab_freed = o;
261 /* Create a new slab. Make this one twice as big. */
262 slot = slab2->opslab_first;
263 while (slot->opslot_next) slot = slot->opslot_next;
264 slab2 = S_new_slab(aTHX_
265 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
267 : (DIFF(slab2, slot)+1)*2);
268 slab2->opslab_next = slab->opslab_next;
269 slab->opslab_next = slab2;
271 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
273 /* Create a new op slot */
274 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
275 assert(slot >= &slab2->opslab_slots);
276 if (DIFF(&slab2->opslab_slots, slot)
277 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
278 slot = &slab2->opslab_slots;
280 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
283 /* lastsib == 1, op_sibling == 0 implies a solitary unattached op */
285 assert(!o->op_sibling);
292 #ifdef PERL_DEBUG_READONLY_OPS
294 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
296 PERL_ARGS_ASSERT_SLAB_TO_RO;
298 if (slab->opslab_readonly) return;
299 slab->opslab_readonly = 1;
300 for (; slab; slab = slab->opslab_next) {
301 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
302 (unsigned long) slab->opslab_size, slab));*/
303 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
304 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
305 (unsigned long)slab->opslab_size, errno);
310 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
314 PERL_ARGS_ASSERT_SLAB_TO_RW;
316 if (!slab->opslab_readonly) return;
318 for (; slab2; slab2 = slab2->opslab_next) {
319 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
320 (unsigned long) size, slab2));*/
321 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
322 PROT_READ|PROT_WRITE)) {
323 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
324 (unsigned long)slab2->opslab_size, errno);
327 slab->opslab_readonly = 0;
331 # define Slab_to_rw(op) NOOP
334 /* This cannot possibly be right, but it was copied from the old slab
335 allocator, to which it was originally added, without explanation, in
338 # define PerlMemShared PerlMem
342 Perl_Slab_Free(pTHX_ void *op)
344 OP * const o = (OP *)op;
347 PERL_ARGS_ASSERT_SLAB_FREE;
349 if (!o->op_slabbed) {
351 PerlMemShared_free(op);
356 /* If this op is already freed, our refcount will get screwy. */
357 assert(o->op_type != OP_FREED);
358 o->op_type = OP_FREED;
359 o->op_next = slab->opslab_freed;
360 slab->opslab_freed = o;
361 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
362 OpslabREFCNT_dec_padok(slab);
366 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
368 const bool havepad = !!PL_comppad;
369 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
372 PAD_SAVE_SETNULLPAD();
379 Perl_opslab_free(pTHX_ OPSLAB *slab)
382 PERL_ARGS_ASSERT_OPSLAB_FREE;
384 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
385 assert(slab->opslab_refcnt == 1);
386 for (; slab; slab = slab2) {
387 slab2 = slab->opslab_next;
389 slab->opslab_refcnt = ~(size_t)0;
391 #ifdef PERL_DEBUG_READONLY_OPS
392 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
394 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
395 perror("munmap failed");
399 PerlMemShared_free(slab);
405 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
410 size_t savestack_count = 0;
412 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
415 for (slot = slab2->opslab_first;
417 slot = slot->opslot_next) {
418 if (slot->opslot_op.op_type != OP_FREED
419 && !(slot->opslot_op.op_savefree
425 assert(slot->opslot_op.op_slabbed);
426 op_free(&slot->opslot_op);
427 if (slab->opslab_refcnt == 1) goto free;
430 } while ((slab2 = slab2->opslab_next));
431 /* > 1 because the CV still holds a reference count. */
432 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
434 assert(savestack_count == slab->opslab_refcnt-1);
436 /* Remove the CV’s reference count. */
437 slab->opslab_refcnt--;
444 #ifdef PERL_DEBUG_READONLY_OPS
446 Perl_op_refcnt_inc(pTHX_ OP *o)
449 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
450 if (slab && slab->opslab_readonly) {
463 Perl_op_refcnt_dec(pTHX_ OP *o)
466 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
468 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
470 if (slab && slab->opslab_readonly) {
472 result = --o->op_targ;
475 result = --o->op_targ;
481 * In the following definition, the ", (OP*)0" is just to make the compiler
482 * think the expression is of the right type: croak actually does a Siglongjmp.
484 #define CHECKOP(type,o) \
485 ((PL_op_mask && PL_op_mask[type]) \
486 ? ( op_free((OP*)o), \
487 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
489 : PL_check[type](aTHX_ (OP*)o))
491 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
493 #define CHANGE_TYPE(o,type) \
495 o->op_type = (OPCODE)type; \
496 o->op_ppaddr = PL_ppaddr[type]; \
500 S_gv_ename(pTHX_ GV *gv)
502 SV* const tmpsv = sv_newmortal();
504 PERL_ARGS_ASSERT_GV_ENAME;
506 gv_efullname3(tmpsv, gv, NULL);
511 S_no_fh_allowed(pTHX_ OP *o)
513 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
515 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
521 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
523 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
524 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, SVfARG(namesv)),
525 SvUTF8(namesv) | flags);
530 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
532 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
533 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
538 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
540 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
542 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
547 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
549 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
551 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
552 SvUTF8(namesv) | flags);
557 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
559 PERL_ARGS_ASSERT_BAD_TYPE_PV;
561 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
562 (int)n, name, t, OP_DESC(kid)), flags);
566 S_bad_type_gv(pTHX_ I32 n, const char *t, GV *gv, U32 flags, const OP *kid)
568 SV * const namesv = gv_ename(gv);
569 PERL_ARGS_ASSERT_BAD_TYPE_GV;
571 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
572 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
576 S_no_bareword_allowed(pTHX_ OP *o)
578 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
580 qerror(Perl_mess(aTHX_
581 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
583 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
586 /* "register" allocation */
589 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
592 const bool is_our = (PL_parser->in_my == KEY_our);
594 PERL_ARGS_ASSERT_ALLOCMY;
596 if (flags & ~SVf_UTF8)
597 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
600 /* Until we're using the length for real, cross check that we're being
602 assert(strlen(name) == len);
604 /* complain about "my $<special_var>" etc etc */
608 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
609 (name[1] == '_' && (*name == '$' || len > 2))))
611 /* name[2] is true if strlen(name) > 2 */
612 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
613 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
614 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
615 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
616 PL_parser->in_my == KEY_state ? "state" : "my"));
618 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
619 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
622 else if (len == 2 && name[1] == '_' && !is_our)
623 /* diag_listed_as: Use of my $_ is experimental */
624 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
625 "Use of %s $_ is experimental",
626 PL_parser->in_my == KEY_state
630 /* allocate a spare slot and store the name in that slot */
632 off = pad_add_name_pvn(name, len,
633 (is_our ? padadd_OUR :
634 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
635 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
636 PL_parser->in_my_stash,
638 /* $_ is always in main::, even with our */
639 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
643 /* anon sub prototypes contains state vars should always be cloned,
644 * otherwise the state var would be shared between anon subs */
646 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
647 CvCLONE_on(PL_compcv);
653 =head1 Optree Manipulation Functions
655 =for apidoc alloccopstash
657 Available only under threaded builds, this function allocates an entry in
658 C<PL_stashpad> for the stash passed to it.
665 Perl_alloccopstash(pTHX_ HV *hv)
667 PADOFFSET off = 0, o = 1;
668 bool found_slot = FALSE;
670 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
672 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
674 for (; o < PL_stashpadmax; ++o) {
675 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
676 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
677 found_slot = TRUE, off = o;
680 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
681 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
682 off = PL_stashpadmax;
683 PL_stashpadmax += 10;
686 PL_stashpad[PL_stashpadix = off] = hv;
691 /* free the body of an op without examining its contents.
692 * Always use this rather than FreeOp directly */
695 S_op_destroy(pTHX_ OP *o)
703 =for apidoc Am|void|op_free|OP *o
705 Free an op. Only use this when an op is no longer linked to from any
712 Perl_op_free(pTHX_ OP *o)
719 /* Though ops may be freed twice, freeing the op after its slab is a
721 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
722 /* During the forced freeing of ops after compilation failure, kidops
723 may be freed before their parents. */
724 if (!o || o->op_type == OP_FREED)
728 if (o->op_private & OPpREFCOUNTED) {
739 refcnt = OpREFCNT_dec(o);
742 /* Need to find and remove any pattern match ops from the list
743 we maintain for reset(). */
744 find_and_forget_pmops(o);
754 /* Call the op_free hook if it has been set. Do it now so that it's called
755 * at the right time for refcounted ops, but still before all of the kids
759 if (o->op_flags & OPf_KIDS) {
761 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
762 nextkid = OP_SIBLING(kid); /* Get before next freeing kid */
767 type = (OPCODE)o->op_targ;
770 Slab_to_rw(OpSLAB(o));
772 /* COP* is not cleared by op_clear() so that we may track line
773 * numbers etc even after null() */
774 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
780 #ifdef DEBUG_LEAKING_SCALARS
787 Perl_op_clear(pTHX_ OP *o)
792 PERL_ARGS_ASSERT_OP_CLEAR;
794 switch (o->op_type) {
795 case OP_NULL: /* Was holding old type, if any. */
798 case OP_ENTEREVAL: /* Was holding hints. */
802 if (!(o->op_flags & OPf_REF)
803 || (PL_check[o->op_type] != Perl_ck_ftst))
810 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
815 /* It's possible during global destruction that the GV is freed
816 before the optree. Whilst the SvREFCNT_inc is happy to bump from
817 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
818 will trigger an assertion failure, because the entry to sv_clear
819 checks that the scalar is not already freed. A check of for
820 !SvIS_FREED(gv) turns out to be invalid, because during global
821 destruction the reference count can be forced down to zero
822 (with SVf_BREAK set). In which case raising to 1 and then
823 dropping to 0 triggers cleanup before it should happen. I
824 *think* that this might actually be a general, systematic,
825 weakness of the whole idea of SVf_BREAK, in that code *is*
826 allowed to raise and lower references during global destruction,
827 so any *valid* code that happens to do this during global
828 destruction might well trigger premature cleanup. */
829 bool still_valid = gv && SvREFCNT(gv);
832 SvREFCNT_inc_simple_void(gv);
834 if (cPADOPo->op_padix > 0) {
835 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
836 * may still exist on the pad */
837 pad_swipe(cPADOPo->op_padix, TRUE);
838 cPADOPo->op_padix = 0;
841 SvREFCNT_dec(cSVOPo->op_sv);
842 cSVOPo->op_sv = NULL;
845 int try_downgrade = SvREFCNT(gv) == 2;
848 gv_try_downgrade(gv);
852 case OP_METHOD_NAMED:
855 SvREFCNT_dec(cSVOPo->op_sv);
856 cSVOPo->op_sv = NULL;
859 Even if op_clear does a pad_free for the target of the op,
860 pad_free doesn't actually remove the sv that exists in the pad;
861 instead it lives on. This results in that it could be reused as
862 a target later on when the pad was reallocated.
865 pad_swipe(o->op_targ,1);
875 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
880 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
881 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
883 if (cPADOPo->op_padix > 0) {
884 pad_swipe(cPADOPo->op_padix, TRUE);
885 cPADOPo->op_padix = 0;
888 SvREFCNT_dec(cSVOPo->op_sv);
889 cSVOPo->op_sv = NULL;
893 PerlMemShared_free(cPVOPo->op_pv);
894 cPVOPo->op_pv = NULL;
898 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
902 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
903 /* No GvIN_PAD_off here, because other references may still
904 * exist on the pad */
905 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
908 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
914 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
915 op_free(cPMOPo->op_code_list);
916 cPMOPo->op_code_list = NULL;
918 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
919 /* we use the same protection as the "SAFE" version of the PM_ macros
920 * here since sv_clean_all might release some PMOPs
921 * after PL_regex_padav has been cleared
922 * and the clearing of PL_regex_padav needs to
923 * happen before sv_clean_all
926 if(PL_regex_pad) { /* We could be in destruction */
927 const IV offset = (cPMOPo)->op_pmoffset;
928 ReREFCNT_dec(PM_GETRE(cPMOPo));
929 PL_regex_pad[offset] = &PL_sv_undef;
930 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
934 ReREFCNT_dec(PM_GETRE(cPMOPo));
935 PM_SETRE(cPMOPo, NULL);
941 if (o->op_targ > 0) {
942 pad_free(o->op_targ);
948 S_cop_free(pTHX_ COP* cop)
950 PERL_ARGS_ASSERT_COP_FREE;
953 if (! specialWARN(cop->cop_warnings))
954 PerlMemShared_free(cop->cop_warnings);
955 cophh_free(CopHINTHASH_get(cop));
956 if (PL_curcop == cop)
961 S_forget_pmop(pTHX_ PMOP *const o
964 HV * const pmstash = PmopSTASH(o);
966 PERL_ARGS_ASSERT_FORGET_PMOP;
968 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
969 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
971 PMOP **const array = (PMOP**) mg->mg_ptr;
972 U32 count = mg->mg_len / sizeof(PMOP**);
977 /* Found it. Move the entry at the end to overwrite it. */
978 array[i] = array[--count];
979 mg->mg_len = count * sizeof(PMOP**);
980 /* Could realloc smaller at this point always, but probably
981 not worth it. Probably worth free()ing if we're the
984 Safefree(mg->mg_ptr);
997 S_find_and_forget_pmops(pTHX_ OP *o)
999 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1001 if (o->op_flags & OPf_KIDS) {
1002 OP *kid = cUNOPo->op_first;
1004 switch (kid->op_type) {
1009 forget_pmop((PMOP*)kid);
1011 find_and_forget_pmops(kid);
1012 kid = OP_SIBLING(kid);
1018 =for apidoc Am|void|op_null|OP *o
1020 Neutralizes an op when it is no longer needed, but is still linked to from
1027 Perl_op_null(pTHX_ OP *o)
1031 PERL_ARGS_ASSERT_OP_NULL;
1033 if (o->op_type == OP_NULL)
1036 o->op_targ = o->op_type;
1037 o->op_type = OP_NULL;
1038 o->op_ppaddr = PL_ppaddr[OP_NULL];
1042 Perl_op_refcnt_lock(pTHX)
1047 PERL_UNUSED_CONTEXT;
1052 Perl_op_refcnt_unlock(pTHX)
1057 PERL_UNUSED_CONTEXT;
1063 =for apidoc op_sibling_splice
1065 A general function for editing the structure of an existing chain of
1066 op_sibling nodes. By analogy with the perl-level splice() function, allows
1067 you to delete zero or more sequential nodes, replacing them with zero or
1068 more different nodes. Performs the necessary op_first/op_last
1069 housekeeping on the parent node and op_sibling manipulation on the
1070 children. The last deleted node will be marked as as the last node by
1071 updating the op_sibling or op_lastsib field as appropriate.
1073 Note that op_next is not manipulated, and nodes are not freed; that is the
1074 responsibility of the caller. It also won't create a new list op for an
1075 empty list etc; use higher-level functions like op_append_elem() for that.
1077 parent is the parent node of the sibling chain.
1079 start is the node preceding the first node to be spliced. Node(s)
1080 following it will be deleted, and ops will be inserted after it. If it is
1081 NULL, the first node onwards is deleted, and nodes are inserted at the
1084 del_count is the number of nodes to delete. If zero, no nodes are deleted.
1085 If -1 or greater than or equal to the number of remaining kids, all
1086 remaining kids are deleted.
1088 insert is the first of a chain of nodes to be inserted in place of the nodes.
1089 If NULL, no nodes are inserted.
1091 The head of the chain of deleted ops is returned, or NULL if no ops were
1096 action before after returns
1097 ------ ----- ----- -------
1100 splice(P, A, 2, X-Y-Z) | | B-C
1104 splice(P, NULL, 1, X-Y) | | A
1108 splice(P, NULL, 3, NULL) | | A-B-C
1112 splice(P, B, 0, X-Y) | | NULL
1119 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1121 OP *first = start ? OP_SIBLING(start) : cLISTOPx(parent)->op_first;
1123 OP *last_del = NULL;
1124 OP *last_ins = NULL;
1126 PERL_ARGS_ASSERT_OP_SIBLING_SPLICE;
1128 assert(del_count >= -1);
1130 if (del_count && first) {
1132 while (--del_count && OP_HAS_SIBLING(last_del))
1133 last_del = OP_SIBLING(last_del);
1134 rest = OP_SIBLING(last_del);
1135 OP_SIBLING_set(last_del, NULL);
1136 last_del->op_lastsib = 1;
1143 while (OP_HAS_SIBLING(last_ins))
1144 last_ins = OP_SIBLING(last_ins);
1145 OP_SIBLING_set(last_ins, rest);
1146 last_ins->op_lastsib = rest ? 0 : 1;
1152 OP_SIBLING_set(start, insert);
1153 start->op_lastsib = insert ? 0 : 1;
1156 cLISTOPx(parent)->op_first = insert;
1159 /* update op_last etc */
1160 U32 type = parent->op_type;
1163 if (type == OP_NULL)
1164 type = parent->op_targ;
1165 type = PL_opargs[type] & OA_CLASS_MASK;
1167 lastop = last_ins ? last_ins : start ? start : NULL;
1168 if ( type == OA_BINOP
1169 || type == OA_LISTOP
1173 cLISTOPx(parent)->op_last = lastop;
1176 lastop->op_lastsib = 1;
1177 #ifdef PERL_OP_PARENT
1178 lastop->op_sibling = parent;
1182 return last_del ? first : NULL;
1186 =for apidoc op_parent
1188 returns the parent OP of o, if it has a parent. Returns NULL otherwise.
1189 (Currently perl must be built with C<-DPERL_OP_PARENT> for this feature to
1196 Perl_op_parent(OP *o)
1198 PERL_ARGS_ASSERT_OP_PARENT;
1199 #ifdef PERL_OP_PARENT
1200 while (OP_HAS_SIBLING(o))
1202 return o->op_sibling;
1210 /* replace the sibling following start with a new UNOP, which becomes
1211 * the parent of the original sibling; e.g.
1213 * op_sibling_newUNOP(P, A, unop-args...)
1221 * where U is the new UNOP.
1223 * parent and start args are the same as for op_sibling_splice();
1224 * type and flags args are as newUNOP().
1226 * Returns the new UNOP.
1230 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1234 kid = op_sibling_splice(parent, start, 1, NULL);
1235 newop = newUNOP(type, flags, kid);
1236 op_sibling_splice(parent, start, 0, newop);
1241 /* lowest-level newLOGOP-style function - just allocates and populates
1242 * the struct. Higher-level stuff should be done by S_new_logop() /
1243 * newLOGOP(). This function exists mainly to avoid op_first assignment
1244 * being spread throughout this file.
1248 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1252 NewOp(1101, logop, 1, LOGOP);
1253 logop->op_type = (OPCODE)type;
1254 logop->op_first = first;
1255 logop->op_other = other;
1256 logop->op_flags = OPf_KIDS;
1257 while (kid && OP_HAS_SIBLING(kid))
1258 kid = OP_SIBLING(kid);
1260 kid->op_lastsib = 1;
1261 #ifdef PERL_OP_PARENT
1262 kid->op_sibling = (OP*)logop;
1269 /* Contextualizers */
1272 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1274 Applies a syntactic context to an op tree representing an expression.
1275 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1276 or C<G_VOID> to specify the context to apply. The modified op tree
1283 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1285 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1287 case G_SCALAR: return scalar(o);
1288 case G_ARRAY: return list(o);
1289 case G_VOID: return scalarvoid(o);
1291 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1298 =for apidoc Am|OP*|op_linklist|OP *o
1299 This function is the implementation of the L</LINKLIST> macro. It should
1300 not be called directly.
1306 Perl_op_linklist(pTHX_ OP *o)
1310 PERL_ARGS_ASSERT_OP_LINKLIST;
1315 /* establish postfix order */
1316 first = cUNOPo->op_first;
1319 o->op_next = LINKLIST(first);
1322 OP *sibl = OP_SIBLING(kid);
1324 kid->op_next = LINKLIST(sibl);
1339 S_scalarkids(pTHX_ OP *o)
1341 if (o && o->op_flags & OPf_KIDS) {
1343 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1350 S_scalarboolean(pTHX_ OP *o)
1352 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1354 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1355 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1356 if (ckWARN(WARN_SYNTAX)) {
1357 const line_t oldline = CopLINE(PL_curcop);
1359 if (PL_parser && PL_parser->copline != NOLINE) {
1360 /* This ensures that warnings are reported at the first line
1361 of the conditional, not the last. */
1362 CopLINE_set(PL_curcop, PL_parser->copline);
1364 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1365 CopLINE_set(PL_curcop, oldline);
1372 S_op_varname(pTHX_ const OP *o)
1375 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1376 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1378 const char funny = o->op_type == OP_PADAV
1379 || o->op_type == OP_RV2AV ? '@' : '%';
1380 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1382 if (cUNOPo->op_first->op_type != OP_GV
1383 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1385 return varname(gv, funny, 0, NULL, 0, 1);
1388 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1393 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1394 { /* or not so pretty :-) */
1395 if (o->op_type == OP_CONST) {
1397 if (SvPOK(*retsv)) {
1399 *retsv = sv_newmortal();
1400 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1401 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1403 else if (!SvOK(*retsv))
1406 else *retpv = "...";
1410 S_scalar_slice_warning(pTHX_ const OP *o)
1414 o->op_type == OP_HSLICE ? '{' : '[';
1416 o->op_type == OP_HSLICE ? '}' : ']';
1418 SV *keysv = NULL; /* just to silence compiler warnings */
1419 const char *key = NULL;
1421 if (!(o->op_private & OPpSLICEWARNING))
1423 if (PL_parser && PL_parser->error_count)
1424 /* This warning can be nonsensical when there is a syntax error. */
1427 kid = cLISTOPo->op_first;
1428 kid = OP_SIBLING(kid); /* get past pushmark */
1429 /* weed out false positives: any ops that can return lists */
1430 switch (kid->op_type) {
1459 /* Don't warn if we have a nulled list either. */
1460 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1463 assert(OP_SIBLING(kid));
1464 name = S_op_varname(aTHX_ OP_SIBLING(kid));
1465 if (!name) /* XS module fiddling with the op tree */
1467 S_op_pretty(aTHX_ kid, &keysv, &key);
1468 assert(SvPOK(name));
1469 sv_chop(name,SvPVX(name)+1);
1471 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1472 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1473 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1475 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1476 lbrack, key, rbrack);
1478 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1479 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1480 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1482 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1483 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1487 Perl_scalar(pTHX_ OP *o)
1491 /* assumes no premature commitment */
1492 if (!o || (PL_parser && PL_parser->error_count)
1493 || (o->op_flags & OPf_WANT)
1494 || o->op_type == OP_RETURN)
1499 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1501 switch (o->op_type) {
1503 scalar(cBINOPo->op_first);
1508 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1518 if (o->op_flags & OPf_KIDS) {
1519 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid))
1525 kid = cLISTOPo->op_first;
1527 kid = OP_SIBLING(kid);
1530 OP *sib = OP_SIBLING(kid);
1531 if (sib && kid->op_type != OP_LEAVEWHEN)
1537 PL_curcop = &PL_compiling;
1542 kid = cLISTOPo->op_first;
1545 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1550 /* Warn about scalar context */
1551 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1552 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1555 const char *key = NULL;
1557 /* This warning can be nonsensical when there is a syntax error. */
1558 if (PL_parser && PL_parser->error_count)
1561 if (!ckWARN(WARN_SYNTAX)) break;
1563 kid = cLISTOPo->op_first;
1564 kid = OP_SIBLING(kid); /* get past pushmark */
1565 assert(OP_SIBLING(kid));
1566 name = S_op_varname(aTHX_ OP_SIBLING(kid));
1567 if (!name) /* XS module fiddling with the op tree */
1569 S_op_pretty(aTHX_ kid, &keysv, &key);
1570 assert(SvPOK(name));
1571 sv_chop(name,SvPVX(name)+1);
1573 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1574 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1575 "%%%"SVf"%c%s%c in scalar context better written "
1577 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1578 lbrack, key, rbrack);
1580 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1581 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1582 "%%%"SVf"%c%"SVf"%c in scalar context better "
1583 "written as $%"SVf"%c%"SVf"%c",
1584 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1585 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1592 Perl_scalarvoid(pTHX_ OP *o)
1596 SV *useless_sv = NULL;
1597 const char* useless = NULL;
1601 PERL_ARGS_ASSERT_SCALARVOID;
1603 if (o->op_type == OP_NEXTSTATE
1604 || o->op_type == OP_DBSTATE
1605 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1606 || o->op_targ == OP_DBSTATE)))
1607 PL_curcop = (COP*)o; /* for warning below */
1609 /* assumes no premature commitment */
1610 want = o->op_flags & OPf_WANT;
1611 if ((want && want != OPf_WANT_SCALAR)
1612 || (PL_parser && PL_parser->error_count)
1613 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1618 if ((o->op_private & OPpTARGET_MY)
1619 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1621 return scalar(o); /* As if inside SASSIGN */
1624 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1626 switch (o->op_type) {
1628 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1632 if (o->op_flags & OPf_STACKED)
1636 if (o->op_private == 4)
1661 case OP_AELEMFAST_LEX:
1682 case OP_GETSOCKNAME:
1683 case OP_GETPEERNAME:
1688 case OP_GETPRIORITY:
1713 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1714 /* Otherwise it's "Useless use of grep iterator" */
1715 useless = OP_DESC(o);
1719 kid = cLISTOPo->op_first;
1720 if (kid && kid->op_type == OP_PUSHRE
1722 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1724 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1726 useless = OP_DESC(o);
1730 kid = cUNOPo->op_first;
1731 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1732 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1735 useless = "negative pattern binding (!~)";
1739 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1740 useless = "non-destructive substitution (s///r)";
1744 useless = "non-destructive transliteration (tr///r)";
1751 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1752 (!OP_HAS_SIBLING(o) || OP_SIBLING(o)->op_type != OP_READLINE))
1753 useless = "a variable";
1758 if (cSVOPo->op_private & OPpCONST_STRICT)
1759 no_bareword_allowed(o);
1761 if (ckWARN(WARN_VOID)) {
1762 /* don't warn on optimised away booleans, eg
1763 * use constant Foo, 5; Foo || print; */
1764 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1766 /* the constants 0 and 1 are permitted as they are
1767 conventionally used as dummies in constructs like
1768 1 while some_condition_with_side_effects; */
1769 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1771 else if (SvPOK(sv)) {
1772 SV * const dsv = newSVpvs("");
1774 = Perl_newSVpvf(aTHX_
1776 pv_pretty(dsv, SvPVX_const(sv),
1777 SvCUR(sv), 32, NULL, NULL,
1779 | PERL_PV_ESCAPE_NOCLEAR
1780 | PERL_PV_ESCAPE_UNI_DETECT));
1781 SvREFCNT_dec_NN(dsv);
1783 else if (SvOK(sv)) {
1784 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1787 useless = "a constant (undef)";
1790 op_null(o); /* don't execute or even remember it */
1794 o->op_type = OP_PREINC; /* pre-increment is faster */
1795 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1799 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1800 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1804 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1805 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1809 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1810 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1815 UNOP *refgen, *rv2cv;
1818 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1821 rv2gv = ((BINOP *)o)->op_last;
1822 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1825 refgen = (UNOP *)((BINOP *)o)->op_first;
1827 if (!refgen || refgen->op_type != OP_REFGEN)
1830 exlist = (LISTOP *)refgen->op_first;
1831 if (!exlist || exlist->op_type != OP_NULL
1832 || exlist->op_targ != OP_LIST)
1835 if (exlist->op_first->op_type != OP_PUSHMARK)
1838 rv2cv = (UNOP*)exlist->op_last;
1840 if (rv2cv->op_type != OP_RV2CV)
1843 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1844 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1845 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1847 o->op_private |= OPpASSIGN_CV_TO_GV;
1848 rv2gv->op_private |= OPpDONT_INIT_GV;
1849 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1861 kid = cLOGOPo->op_first;
1862 if (kid->op_type == OP_NOT
1863 && (kid->op_flags & OPf_KIDS)) {
1864 if (o->op_type == OP_AND) {
1866 o->op_ppaddr = PL_ppaddr[OP_OR];
1868 o->op_type = OP_AND;
1869 o->op_ppaddr = PL_ppaddr[OP_AND];
1879 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1884 if (o->op_flags & OPf_STACKED)
1891 if (!(o->op_flags & OPf_KIDS))
1902 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1913 /* mortalise it, in case warnings are fatal. */
1914 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1915 "Useless use of %"SVf" in void context",
1916 SVfARG(sv_2mortal(useless_sv)));
1919 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1920 "Useless use of %s in void context",
1927 S_listkids(pTHX_ OP *o)
1929 if (o && o->op_flags & OPf_KIDS) {
1931 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1938 Perl_list(pTHX_ OP *o)
1942 /* assumes no premature commitment */
1943 if (!o || (o->op_flags & OPf_WANT)
1944 || (PL_parser && PL_parser->error_count)
1945 || o->op_type == OP_RETURN)
1950 if ((o->op_private & OPpTARGET_MY)
1951 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1953 return o; /* As if inside SASSIGN */
1956 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1958 switch (o->op_type) {
1961 list(cBINOPo->op_first);
1966 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1974 if (!(o->op_flags & OPf_KIDS))
1976 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1977 list(cBINOPo->op_first);
1978 return gen_constant_list(o);
1985 kid = cLISTOPo->op_first;
1987 kid = OP_SIBLING(kid);
1990 OP *sib = OP_SIBLING(kid);
1991 if (sib && kid->op_type != OP_LEAVEWHEN)
1997 PL_curcop = &PL_compiling;
2001 kid = cLISTOPo->op_first;
2008 S_scalarseq(pTHX_ OP *o)
2011 const OPCODE type = o->op_type;
2013 if (type == OP_LINESEQ || type == OP_SCOPE ||
2014 type == OP_LEAVE || type == OP_LEAVETRY)
2017 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2018 if (OP_HAS_SIBLING(kid)) {
2022 PL_curcop = &PL_compiling;
2024 o->op_flags &= ~OPf_PARENS;
2025 if (PL_hints & HINT_BLOCK_SCOPE)
2026 o->op_flags |= OPf_PARENS;
2029 o = newOP(OP_STUB, 0);
2034 S_modkids(pTHX_ OP *o, I32 type)
2036 if (o && o->op_flags & OPf_KIDS) {
2038 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2039 op_lvalue(kid, type);
2045 =for apidoc finalize_optree
2047 This function finalizes the optree. Should be called directly after
2048 the complete optree is built. It does some additional
2049 checking which can't be done in the normal ck_xxx functions and makes
2050 the tree thread-safe.
2055 Perl_finalize_optree(pTHX_ OP* o)
2057 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2060 SAVEVPTR(PL_curcop);
2068 S_finalize_op(pTHX_ OP* o)
2070 PERL_ARGS_ASSERT_FINALIZE_OP;
2073 switch (o->op_type) {
2076 PL_curcop = ((COP*)o); /* for warnings */
2079 if (OP_HAS_SIBLING(o)) {
2080 OP *sib = OP_SIBLING(o);
2081 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2082 && ckWARN(WARN_EXEC)
2083 && OP_HAS_SIBLING(sib))
2085 const OPCODE type = OP_SIBLING(sib)->op_type;
2086 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2087 const line_t oldline = CopLINE(PL_curcop);
2088 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2089 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2090 "Statement unlikely to be reached");
2091 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2092 "\t(Maybe you meant system() when you said exec()?)\n");
2093 CopLINE_set(PL_curcop, oldline);
2100 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2101 GV * const gv = cGVOPo_gv;
2102 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2103 /* XXX could check prototype here instead of just carping */
2104 SV * const sv = sv_newmortal();
2105 gv_efullname3(sv, gv, NULL);
2106 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2107 "%"SVf"() called too early to check prototype",
2114 if (cSVOPo->op_private & OPpCONST_STRICT)
2115 no_bareword_allowed(o);
2119 case OP_METHOD_NAMED:
2120 /* Relocate sv to the pad for thread safety.
2121 * Despite being a "constant", the SV is written to,
2122 * for reference counts, sv_upgrade() etc. */
2123 if (cSVOPo->op_sv) {
2124 const PADOFFSET ix = pad_alloc(OP_CONST, SVf_READONLY);
2125 SvREFCNT_dec(PAD_SVl(ix));
2126 PAD_SETSV(ix, cSVOPo->op_sv);
2127 /* XXX I don't know how this isn't readonly already. */
2128 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2129 cSVOPo->op_sv = NULL;
2143 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2146 rop = (UNOP*)((BINOP*)o)->op_first;
2151 S_scalar_slice_warning(aTHX_ o);
2155 kid = OP_SIBLING(cLISTOPo->op_first);
2156 if (/* I bet there's always a pushmark... */
2157 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2158 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2163 key_op = (SVOP*)(kid->op_type == OP_CONST
2165 : OP_SIBLING(kLISTOP->op_first));
2167 rop = (UNOP*)((LISTOP*)o)->op_last;
2170 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2172 else if (rop->op_first->op_type == OP_PADSV)
2173 /* @$hash{qw(keys here)} */
2174 rop = (UNOP*)rop->op_first;
2176 /* @{$hash}{qw(keys here)} */
2177 if (rop->op_first->op_type == OP_SCOPE
2178 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2180 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2186 lexname = NULL; /* just to silence compiler warnings */
2187 fields = NULL; /* just to silence compiler warnings */
2191 && (lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE),
2192 SvPAD_TYPED(lexname))
2193 && (fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE))
2194 && isGV(*fields) && GvHV(*fields);
2196 key_op = (SVOP*)OP_SIBLING(key_op)) {
2198 if (key_op->op_type != OP_CONST)
2200 svp = cSVOPx_svp(key_op);
2202 /* Make the CONST have a shared SV */
2203 if ((!SvIsCOW_shared_hash(sv = *svp))
2204 && SvTYPE(sv) < SVt_PVMG && SvOK(sv) && !SvROK(sv)) {
2206 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2207 SV *nsv = newSVpvn_share(key,
2208 SvUTF8(sv) ? -keylen : keylen, 0);
2209 SvREFCNT_dec_NN(sv);
2214 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) {
2215 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2216 "in variable %"SVf" of type %"HEKf,
2217 SVfARG(*svp), SVfARG(lexname),
2218 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
2224 S_scalar_slice_warning(aTHX_ o);
2228 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2229 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2236 if (o->op_flags & OPf_KIDS) {
2240 /* check that op_last points to the last sibling, and that
2241 * the last op_sibling field points back to the parent, and
2242 * that the only ops with KIDS are those which are entitled to
2244 U32 type = o->op_type;
2248 if (type == OP_NULL) {
2250 /* ck_glob creates a null UNOP with ex-type GLOB
2251 * (which is a list op. So pretend it wasn't a listop */
2252 if (type == OP_GLOB)
2255 family = PL_opargs[type] & OA_CLASS_MASK;
2257 has_last = ( family == OA_BINOP
2258 || family == OA_LISTOP
2259 || family == OA_PMOP
2260 || family == OA_LOOP
2262 assert( has_last /* has op_first and op_last, or ...
2263 ... has (or may have) op_first: */
2264 || family == OA_UNOP
2265 || family == OA_LOGOP
2266 || family == OA_BASEOP_OR_UNOP
2267 || family == OA_FILESTATOP
2268 || family == OA_LOOPEXOP
2269 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2270 || type == OP_SASSIGN
2271 || type == OP_CUSTOM
2272 || type == OP_NULL /* new_logop does this */
2274 /* XXX list form of 'x' is has a null op_last. This is wrong,
2275 * but requires too much hacking (e.g. in Deparse) to fix for
2277 if (type == OP_REPEAT && (o->op_private & OPpREPEAT_DOLIST)) {
2282 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2283 # ifdef PERL_OP_PARENT
2284 if (!OP_HAS_SIBLING(kid)) {
2286 assert(kid == cLISTOPo->op_last);
2287 assert(kid->op_sibling == o);
2290 if (OP_HAS_SIBLING(kid)) {
2291 assert(!kid->op_lastsib);
2294 assert(kid->op_lastsib);
2296 assert(kid == cLISTOPo->op_last);
2302 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid))
2308 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2310 Propagate lvalue ("modifiable") context to an op and its children.
2311 I<type> represents the context type, roughly based on the type of op that
2312 would do the modifying, although C<local()> is represented by OP_NULL,
2313 because it has no op type of its own (it is signalled by a flag on
2316 This function detects things that can't be modified, such as C<$x+1>, and
2317 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2318 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2320 It also flags things that need to behave specially in an lvalue context,
2321 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2327 S_vivifies(const OPCODE type)
2330 case OP_RV2AV: case OP_ASLICE:
2331 case OP_RV2HV: case OP_KVASLICE:
2332 case OP_RV2SV: case OP_HSLICE:
2333 case OP_AELEMFAST: case OP_KVHSLICE:
2342 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2346 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2349 if (!o || (PL_parser && PL_parser->error_count))
2352 if ((o->op_private & OPpTARGET_MY)
2353 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2358 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2360 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2362 switch (o->op_type) {
2367 if ((o->op_flags & OPf_PARENS))
2371 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2372 !(o->op_flags & OPf_STACKED)) {
2373 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2374 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
2375 poses, so we need it clear. */
2376 o->op_private &= ~1;
2377 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2378 assert(cUNOPo->op_first->op_type == OP_NULL);
2379 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2382 else { /* lvalue subroutine call */
2383 o->op_private |= OPpLVAL_INTRO
2384 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
2385 PL_modcount = RETURN_UNLIMITED_NUMBER;
2386 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
2387 /* Potential lvalue context: */
2388 o->op_private |= OPpENTERSUB_INARGS;
2391 else { /* Compile-time error message: */
2392 OP *kid = cUNOPo->op_first;
2395 if (kid->op_type != OP_PUSHMARK) {
2396 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2398 "panic: unexpected lvalue entersub "
2399 "args: type/targ %ld:%"UVuf,
2400 (long)kid->op_type, (UV)kid->op_targ);
2401 kid = kLISTOP->op_first;
2403 while (OP_HAS_SIBLING(kid))
2404 kid = OP_SIBLING(kid);
2405 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2406 break; /* Postpone until runtime */
2409 kid = kUNOP->op_first;
2410 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2411 kid = kUNOP->op_first;
2412 if (kid->op_type == OP_NULL)
2414 "Unexpected constant lvalue entersub "
2415 "entry via type/targ %ld:%"UVuf,
2416 (long)kid->op_type, (UV)kid->op_targ);
2417 if (kid->op_type != OP_GV) {
2421 cv = GvCV(kGVOP_gv);
2431 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2432 /* grep, foreach, subcalls, refgen */
2433 if (type == OP_GREPSTART || type == OP_ENTERSUB
2434 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2436 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2437 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2439 : (o->op_type == OP_ENTERSUB
2440 ? "non-lvalue subroutine call"
2442 type ? PL_op_desc[type] : "local"));
2456 case OP_RIGHT_SHIFT:
2465 if (!(o->op_flags & OPf_STACKED))
2472 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
2473 op_lvalue(kid, type);
2478 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2479 PL_modcount = RETURN_UNLIMITED_NUMBER;
2480 return o; /* Treat \(@foo) like ordinary list. */
2484 if (scalar_mod_type(o, type))
2486 ref(cUNOPo->op_first, o->op_type);
2493 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2494 if (type == OP_LEAVESUBLV && (
2495 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2496 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2498 o->op_private |= OPpMAYBE_LVSUB;
2502 PL_modcount = RETURN_UNLIMITED_NUMBER;
2506 if (type == OP_LEAVESUBLV)
2507 o->op_private |= OPpMAYBE_LVSUB;
2510 PL_hints |= HINT_BLOCK_SCOPE;
2511 if (type == OP_LEAVESUBLV)
2512 o->op_private |= OPpMAYBE_LVSUB;
2516 ref(cUNOPo->op_first, o->op_type);
2520 PL_hints |= HINT_BLOCK_SCOPE;
2530 case OP_AELEMFAST_LEX:
2537 PL_modcount = RETURN_UNLIMITED_NUMBER;
2538 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2539 return o; /* Treat \(@foo) like ordinary list. */
2540 if (scalar_mod_type(o, type))
2542 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2543 && type == OP_LEAVESUBLV)
2544 o->op_private |= OPpMAYBE_LVSUB;
2548 if (!type) /* local() */
2549 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2550 PAD_COMPNAME_SV(o->op_targ));
2559 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2563 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2569 if (type == OP_LEAVESUBLV)
2570 o->op_private |= OPpMAYBE_LVSUB;
2571 if (o->op_flags & OPf_KIDS)
2572 op_lvalue(OP_SIBLING(cBINOPo->op_first), type);
2577 ref(cBINOPo->op_first, o->op_type);
2578 if (type == OP_ENTERSUB &&
2579 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2580 o->op_private |= OPpLVAL_DEFER;
2581 if (type == OP_LEAVESUBLV)
2582 o->op_private |= OPpMAYBE_LVSUB;
2589 o->op_private |= OPpLVALUE;
2595 if (o->op_flags & OPf_KIDS)
2596 op_lvalue(cLISTOPo->op_last, type);
2601 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2603 else if (!(o->op_flags & OPf_KIDS))
2605 if (o->op_targ != OP_LIST) {
2606 op_lvalue(cBINOPo->op_first, type);
2612 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2613 /* elements might be in void context because the list is
2614 in scalar context or because they are attribute sub calls */
2615 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2616 op_lvalue(kid, type);
2620 if (type != OP_LEAVESUBLV)
2622 break; /* op_lvalue()ing was handled by ck_return() */
2629 if (type == OP_LEAVESUBLV
2630 || !S_vivifies(cLOGOPo->op_first->op_type))
2631 op_lvalue(cLOGOPo->op_first, type);
2632 if (type == OP_LEAVESUBLV
2633 || !S_vivifies(OP_SIBLING(cLOGOPo->op_first)->op_type))
2634 op_lvalue(OP_SIBLING(cLOGOPo->op_first), type);
2638 /* [20011101.069] File test operators interpret OPf_REF to mean that
2639 their argument is a filehandle; thus \stat(".") should not set
2641 if (type == OP_REFGEN &&
2642 PL_check[o->op_type] == Perl_ck_ftst)
2645 if (type != OP_LEAVESUBLV)
2646 o->op_flags |= OPf_MOD;
2648 if (type == OP_AASSIGN || type == OP_SASSIGN)
2649 o->op_flags |= OPf_SPECIAL|OPf_REF;
2650 else if (!type) { /* local() */
2653 o->op_private |= OPpLVAL_INTRO;
2654 o->op_flags &= ~OPf_SPECIAL;
2655 PL_hints |= HINT_BLOCK_SCOPE;
2660 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2661 "Useless localization of %s", OP_DESC(o));
2664 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2665 && type != OP_LEAVESUBLV)
2666 o->op_flags |= OPf_REF;
2671 S_scalar_mod_type(const OP *o, I32 type)
2676 if (o && o->op_type == OP_RV2GV)
2700 case OP_RIGHT_SHIFT:
2721 S_is_handle_constructor(const OP *o, I32 numargs)
2723 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2725 switch (o->op_type) {
2733 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2746 S_refkids(pTHX_ OP *o, I32 type)
2748 if (o && o->op_flags & OPf_KIDS) {
2750 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2757 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2762 PERL_ARGS_ASSERT_DOREF;
2764 if (!o || (PL_parser && PL_parser->error_count))
2767 switch (o->op_type) {
2769 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2770 !(o->op_flags & OPf_STACKED)) {
2771 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2772 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2773 assert(cUNOPo->op_first->op_type == OP_NULL);
2774 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2775 o->op_flags |= OPf_SPECIAL;
2776 o->op_private &= ~1;
2778 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2779 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2780 : type == OP_RV2HV ? OPpDEREF_HV
2782 o->op_flags |= OPf_MOD;
2788 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
2789 doref(kid, type, set_op_ref);
2792 if (type == OP_DEFINED)
2793 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2794 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2797 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2798 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2799 : type == OP_RV2HV ? OPpDEREF_HV
2801 o->op_flags |= OPf_MOD;
2808 o->op_flags |= OPf_REF;
2811 if (type == OP_DEFINED)
2812 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2813 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2819 o->op_flags |= OPf_REF;
2824 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
2826 doref(cBINOPo->op_first, type, set_op_ref);
2830 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2831 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2832 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2833 : type == OP_RV2HV ? OPpDEREF_HV
2835 o->op_flags |= OPf_MOD;
2845 if (!(o->op_flags & OPf_KIDS))
2847 doref(cLISTOPo->op_last, type, set_op_ref);
2857 S_dup_attrlist(pTHX_ OP *o)
2861 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2863 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2864 * where the first kid is OP_PUSHMARK and the remaining ones
2865 * are OP_CONST. We need to push the OP_CONST values.
2867 if (o->op_type == OP_CONST)
2868 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2870 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2872 for (o = cLISTOPo->op_first; o; o = OP_SIBLING(o)) {
2873 if (o->op_type == OP_CONST)
2874 rop = op_append_elem(OP_LIST, rop,
2875 newSVOP(OP_CONST, o->op_flags,
2876 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2883 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
2885 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2887 PERL_ARGS_ASSERT_APPLY_ATTRS;
2889 /* fake up C<use attributes $pkg,$rv,@attrs> */
2891 #define ATTRSMODULE "attributes"
2892 #define ATTRSMODULE_PM "attributes.pm"
2894 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2895 newSVpvs(ATTRSMODULE),
2897 op_prepend_elem(OP_LIST,
2898 newSVOP(OP_CONST, 0, stashsv),
2899 op_prepend_elem(OP_LIST,
2900 newSVOP(OP_CONST, 0,
2902 dup_attrlist(attrs))));
2906 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2908 OP *pack, *imop, *arg;
2909 SV *meth, *stashsv, **svp;
2911 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2916 assert(target->op_type == OP_PADSV ||
2917 target->op_type == OP_PADHV ||
2918 target->op_type == OP_PADAV);
2920 /* Ensure that attributes.pm is loaded. */
2921 /* Don't force the C<use> if we don't need it. */
2922 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2923 if (svp && *svp != &PL_sv_undef)
2924 NOOP; /* already in %INC */
2926 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2927 newSVpvs(ATTRSMODULE), NULL);
2929 /* Need package name for method call. */
2930 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2932 /* Build up the real arg-list. */
2933 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2935 arg = newOP(OP_PADSV, 0);
2936 arg->op_targ = target->op_targ;
2937 arg = op_prepend_elem(OP_LIST,
2938 newSVOP(OP_CONST, 0, stashsv),
2939 op_prepend_elem(OP_LIST,
2940 newUNOP(OP_REFGEN, 0,
2941 op_lvalue(arg, OP_REFGEN)),
2942 dup_attrlist(attrs)));
2944 /* Fake up a method call to import */
2945 meth = newSVpvs_share("import");
2946 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2947 op_append_elem(OP_LIST,
2948 op_prepend_elem(OP_LIST, pack, list(arg)),
2949 newSVOP(OP_METHOD_NAMED, 0, meth)));
2951 /* Combine the ops. */
2952 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2956 =notfor apidoc apply_attrs_string
2958 Attempts to apply a list of attributes specified by the C<attrstr> and
2959 C<len> arguments to the subroutine identified by the C<cv> argument which
2960 is expected to be associated with the package identified by the C<stashpv>
2961 argument (see L<attributes>). It gets this wrong, though, in that it
2962 does not correctly identify the boundaries of the individual attribute
2963 specifications within C<attrstr>. This is not really intended for the
2964 public API, but has to be listed here for systems such as AIX which
2965 need an explicit export list for symbols. (It's called from XS code
2966 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2967 to respect attribute syntax properly would be welcome.
2973 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2974 const char *attrstr, STRLEN len)
2978 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2981 len = strlen(attrstr);
2985 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2987 const char * const sstr = attrstr;
2988 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2989 attrs = op_append_elem(OP_LIST, attrs,
2990 newSVOP(OP_CONST, 0,
2991 newSVpvn(sstr, attrstr-sstr)));
2995 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2996 newSVpvs(ATTRSMODULE),
2997 NULL, op_prepend_elem(OP_LIST,
2998 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2999 op_prepend_elem(OP_LIST,
3000 newSVOP(OP_CONST, 0,
3001 newRV(MUTABLE_SV(cv))),
3006 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3008 OP *new_proto = NULL;
3013 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3019 if (o->op_type == OP_CONST) {
3020 pv = SvPV(cSVOPo_sv, pvlen);
3021 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3022 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3023 SV ** const tmpo = cSVOPx_svp(o);
3024 SvREFCNT_dec(cSVOPo_sv);
3029 } else if (o->op_type == OP_LIST) {
3031 assert(o->op_flags & OPf_KIDS);
3032 lasto = cLISTOPo->op_first;
3033 assert(lasto->op_type == OP_PUSHMARK);
3034 for (o = OP_SIBLING(lasto); o; o = OP_SIBLING(o)) {
3035 if (o->op_type == OP_CONST) {
3036 pv = SvPV(cSVOPo_sv, pvlen);
3037 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3038 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3039 SV ** const tmpo = cSVOPx_svp(o);
3040 SvREFCNT_dec(cSVOPo_sv);
3042 if (new_proto && ckWARN(WARN_MISC)) {
3044 const char * newp = SvPV(cSVOPo_sv, new_len);
3045 Perl_warner(aTHX_ packWARN(WARN_MISC),
3046 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3047 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3053 /* excise new_proto from the list */
3054 op_sibling_splice(*attrs, lasto, 1, NULL);
3061 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3062 would get pulled in with no real need */
3063 if (!OP_HAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3072 svname = sv_newmortal();
3073 gv_efullname3(svname, name, NULL);
3075 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3076 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3078 svname = (SV *)name;
3079 if (ckWARN(WARN_ILLEGALPROTO))
3080 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3081 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3082 STRLEN old_len, new_len;
3083 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3084 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3086 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3087 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3089 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3090 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3100 S_cant_declare(pTHX_ OP *o)
3102 if (o->op_type == OP_NULL
3103 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3104 o = cUNOPo->op_first;
3105 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3106 o->op_type == OP_NULL
3107 && o->op_flags & OPf_SPECIAL
3110 PL_parser->in_my == KEY_our ? "our" :
3111 PL_parser->in_my == KEY_state ? "state" :
3116 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3119 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3121 PERL_ARGS_ASSERT_MY_KID;
3123 if (!o || (PL_parser && PL_parser->error_count))
3128 if (type == OP_LIST) {
3130 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
3131 my_kid(kid, attrs, imopsp);
3133 } else if (type == OP_UNDEF || type == OP_STUB) {
3135 } else if (type == OP_RV2SV || /* "our" declaration */
3137 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
3138 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3139 S_cant_declare(aTHX_ o);
3141 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3143 PL_parser->in_my = FALSE;
3144 PL_parser->in_my_stash = NULL;
3145 apply_attrs(GvSTASH(gv),
3146 (type == OP_RV2SV ? GvSV(gv) :
3147 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3148 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3151 o->op_private |= OPpOUR_INTRO;
3154 else if (type != OP_PADSV &&
3157 type != OP_PUSHMARK)
3159 S_cant_declare(aTHX_ o);
3162 else if (attrs && type != OP_PUSHMARK) {
3166 PL_parser->in_my = FALSE;
3167 PL_parser->in_my_stash = NULL;
3169 /* check for C<my Dog $spot> when deciding package */
3170 stash = PAD_COMPNAME_TYPE(o->op_targ);
3172 stash = PL_curstash;
3173 apply_attrs_my(stash, o, attrs, imopsp);
3175 o->op_flags |= OPf_MOD;
3176 o->op_private |= OPpLVAL_INTRO;
3178 o->op_private |= OPpPAD_STATE;
3183 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3186 int maybe_scalar = 0;
3188 PERL_ARGS_ASSERT_MY_ATTRS;
3190 /* [perl #17376]: this appears to be premature, and results in code such as
3191 C< our(%x); > executing in list mode rather than void mode */
3193 if (o->op_flags & OPf_PARENS)
3203 o = my_kid(o, attrs, &rops);
3205 if (maybe_scalar && o->op_type == OP_PADSV) {
3206 o = scalar(op_append_list(OP_LIST, rops, o));
3207 o->op_private |= OPpLVAL_INTRO;
3210 /* The listop in rops might have a pushmark at the beginning,
3211 which will mess up list assignment. */
3212 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3213 if (rops->op_type == OP_LIST &&
3214 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3216 OP * const pushmark = lrops->op_first;
3217 /* excise pushmark */
3218 op_sibling_splice(rops, NULL, 1, NULL);
3221 o = op_append_list(OP_LIST, o, rops);
3224 PL_parser->in_my = FALSE;
3225 PL_parser->in_my_stash = NULL;
3230 Perl_sawparens(pTHX_ OP *o)
3232 PERL_UNUSED_CONTEXT;
3234 o->op_flags |= OPf_PARENS;
3239 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3243 const OPCODE ltype = left->op_type;
3244 const OPCODE rtype = right->op_type;
3246 PERL_ARGS_ASSERT_BIND_MATCH;
3248 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3249 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3251 const char * const desc
3253 rtype == OP_SUBST || rtype == OP_TRANS
3254 || rtype == OP_TRANSR
3256 ? (int)rtype : OP_MATCH];
3257 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3259 S_op_varname(aTHX_ left);
3261 Perl_warner(aTHX_ packWARN(WARN_MISC),
3262 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3263 desc, SVfARG(name), SVfARG(name));
3265 const char * const sample = (isary
3266 ? "@array" : "%hash");
3267 Perl_warner(aTHX_ packWARN(WARN_MISC),
3268 "Applying %s to %s will act on scalar(%s)",
3269 desc, sample, sample);
3273 if (rtype == OP_CONST &&
3274 cSVOPx(right)->op_private & OPpCONST_BARE &&
3275 cSVOPx(right)->op_private & OPpCONST_STRICT)
3277 no_bareword_allowed(right);
3280 /* !~ doesn't make sense with /r, so error on it for now */
3281 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3283 /* diag_listed_as: Using !~ with %s doesn't make sense */
3284 yyerror("Using !~ with s///r doesn't make sense");
3285 if (rtype == OP_TRANSR && type == OP_NOT)
3286 /* diag_listed_as: Using !~ with %s doesn't make sense */
3287 yyerror("Using !~ with tr///r doesn't make sense");
3289 ismatchop = (rtype == OP_MATCH ||
3290 rtype == OP_SUBST ||
3291 rtype == OP_TRANS || rtype == OP_TRANSR)
3292 && !(right->op_flags & OPf_SPECIAL);
3293 if (ismatchop && right->op_private & OPpTARGET_MY) {
3295 right->op_private &= ~OPpTARGET_MY;
3297 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
3300 right->op_flags |= OPf_STACKED;
3301 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3302 ! (rtype == OP_TRANS &&
3303 right->op_private & OPpTRANS_IDENTICAL) &&
3304 ! (rtype == OP_SUBST &&
3305 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3306 newleft = op_lvalue(left, rtype);
3309 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3310 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
3312 o = op_prepend_elem(rtype, scalar(newleft), right);
3314 return newUNOP(OP_NOT, 0, scalar(o));
3318 return bind_match(type, left,
3319 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
3323 Perl_invert(pTHX_ OP *o)
3327 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3331 =for apidoc Amx|OP *|op_scope|OP *o
3333 Wraps up an op tree with some additional ops so that at runtime a dynamic
3334 scope will be created. The original ops run in the new dynamic scope,
3335 and then, provided that they exit normally, the scope will be unwound.
3336 The additional ops used to create and unwind the dynamic scope will
3337 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3338 instead if the ops are simple enough to not need the full dynamic scope
3345 Perl_op_scope(pTHX_ OP *o)
3349 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3350 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3351 o->op_type = OP_LEAVE;
3352 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
3354 else if (o->op_type == OP_LINESEQ) {
3356 o->op_type = OP_SCOPE;
3357 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
3358 kid = ((LISTOP*)o)->op_first;
3359 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3362 /* The following deals with things like 'do {1 for 1}' */
3363 kid = OP_SIBLING(kid);
3365 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3370 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3376 Perl_op_unscope(pTHX_ OP *o)
3378 if (o && o->op_type == OP_LINESEQ) {
3379 OP *kid = cLISTOPo->op_first;
3380 for(; kid; kid = OP_SIBLING(kid))
3381 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3388 Perl_block_start(pTHX_ int full)
3390 const int retval = PL_savestack_ix;
3392 pad_block_start(full);
3394 PL_hints &= ~HINT_BLOCK_SCOPE;
3395 SAVECOMPILEWARNINGS();
3396 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3398 CALL_BLOCK_HOOKS(bhk_start, full);
3404 Perl_block_end(pTHX_ I32 floor, OP *seq)
3406 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3407 OP* retval = scalarseq(seq);
3410 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3414 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3418 /* pad_leavemy has created a sequence of introcv ops for all my
3419 subs declared in the block. We have to replicate that list with
3420 clonecv ops, to deal with this situation:
3425 sub s1 { state sub foo { \&s2 } }
3428 Originally, I was going to have introcv clone the CV and turn
3429 off the stale flag. Since &s1 is declared before &s2, the
3430 introcv op for &s1 is executed (on sub entry) before the one for
3431 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3432 cloned, since it is a state sub) closes over &s2 and expects
3433 to see it in its outer CV’s pad. If the introcv op clones &s1,
3434 then &s2 is still marked stale. Since &s1 is not active, and
3435 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3436 ble will not stay shared’ warning. Because it is the same stub
3437 that will be used when the introcv op for &s2 is executed, clos-
3438 ing over it is safe. Hence, we have to turn off the stale flag
3439 on all lexical subs in the block before we clone any of them.
3440 Hence, having introcv clone the sub cannot work. So we create a
3441 list of ops like this:
3465 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3466 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3467 for (;; kid = OP_SIBLING(kid)) {
3468 OP *newkid = newOP(OP_CLONECV, 0);
3469 newkid->op_targ = kid->op_targ;
3470 o = op_append_elem(OP_LINESEQ, o, newkid);
3471 if (kid == last) break;
3473 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3476 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3482 =head1 Compile-time scope hooks
3484 =for apidoc Aox||blockhook_register
3486 Register a set of hooks to be called when the Perl lexical scope changes
3487 at compile time. See L<perlguts/"Compile-time scope hooks">.
3493 Perl_blockhook_register(pTHX_ BHK *hk)
3495 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3497 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3503 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
3504 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
3505 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
3508 OP * const o = newOP(OP_PADSV, 0);
3509 o->op_targ = offset;
3515 Perl_newPROG(pTHX_ OP *o)
3517 PERL_ARGS_ASSERT_NEWPROG;
3524 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3525 ((PL_in_eval & EVAL_KEEPERR)
3526 ? OPf_SPECIAL : 0), o);
3528 cx = &cxstack[cxstack_ix];
3529 assert(CxTYPE(cx) == CXt_EVAL);
3531 if ((cx->blk_gimme & G_WANT) == G_VOID)
3532 scalarvoid(PL_eval_root);
3533 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3536 scalar(PL_eval_root);
3538 PL_eval_start = op_linklist(PL_eval_root);
3539 PL_eval_root->op_private |= OPpREFCOUNTED;
3540 OpREFCNT_set(PL_eval_root, 1);
3541 PL_eval_root->op_next = 0;
3542 i = PL_savestack_ix;
3545 CALL_PEEP(PL_eval_start);
3546 finalize_optree(PL_eval_root);
3547 S_prune_chain_head(&PL_eval_start);
3549 PL_savestack_ix = i;
3552 if (o->op_type == OP_STUB) {
3553 /* This block is entered if nothing is compiled for the main
3554 program. This will be the case for an genuinely empty main
3555 program, or one which only has BEGIN blocks etc, so already
3558 Historically (5.000) the guard above was !o. However, commit
3559 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3560 c71fccf11fde0068, changed perly.y so that newPROG() is now
3561 called with the output of block_end(), which returns a new
3562 OP_STUB for the case of an empty optree. ByteLoader (and
3563 maybe other things) also take this path, because they set up
3564 PL_main_start and PL_main_root directly, without generating an
3567 If the parsing the main program aborts (due to parse errors,
3568 or due to BEGIN or similar calling exit), then newPROG()
3569 isn't even called, and hence this code path and its cleanups
3570 are skipped. This shouldn't make a make a difference:
3571 * a non-zero return from perl_parse is a failure, and
3572 perl_destruct() should be called immediately.
3573 * however, if exit(0) is called during the parse, then
3574 perl_parse() returns 0, and perl_run() is called. As
3575 PL_main_start will be NULL, perl_run() will return
3576 promptly, and the exit code will remain 0.
3579 PL_comppad_name = 0;
3581 S_op_destroy(aTHX_ o);
3584 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3585 PL_curcop = &PL_compiling;
3586 PL_main_start = LINKLIST(PL_main_root);
3587 PL_main_root->op_private |= OPpREFCOUNTED;
3588 OpREFCNT_set(PL_main_root, 1);
3589 PL_main_root->op_next = 0;
3590 CALL_PEEP(PL_main_start);
3591 finalize_optree(PL_main_root);
3592 S_prune_chain_head(&PL_main_start);
3593 cv_forget_slab(PL_compcv);
3596 /* Register with debugger */
3598 CV * const cv = get_cvs("DB::postponed", 0);
3602 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3604 call_sv(MUTABLE_SV(cv), G_DISCARD);
3611 Perl_localize(pTHX_ OP *o, I32 lex)
3613 PERL_ARGS_ASSERT_LOCALIZE;
3615 if (o->op_flags & OPf_PARENS)
3616 /* [perl #17376]: this appears to be premature, and results in code such as
3617 C< our(%x); > executing in list mode rather than void mode */
3624 if ( PL_parser->bufptr > PL_parser->oldbufptr
3625 && PL_parser->bufptr[-1] == ','
3626 && ckWARN(WARN_PARENTHESIS))
3628 char *s = PL_parser->bufptr;
3631 /* some heuristics to detect a potential error */
3632 while (*s && (strchr(", \t\n", *s)))
3636 if (*s && strchr("@$%*", *s) && *++s
3637 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
3640 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
3642 while (*s && (strchr(", \t\n", *s)))
3648 if (sigil && (*s == ';' || *s == '=')) {
3649 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3650 "Parentheses missing around \"%s\" list",
3652 ? (PL_parser->in_my == KEY_our
3654 : PL_parser->in_my == KEY_state
3664 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3665 PL_parser->in_my = FALSE;
3666 PL_parser->in_my_stash = NULL;
3671 Perl_jmaybe(pTHX_ OP *o)
3673 PERL_ARGS_ASSERT_JMAYBE;
3675 if (o->op_type == OP_LIST) {
3677 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3678 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3683 PERL_STATIC_INLINE OP *
3684 S_op_std_init(pTHX_ OP *o)
3686 I32 type = o->op_type;
3688 PERL_ARGS_ASSERT_OP_STD_INIT;
3690 if (PL_opargs[type] & OA_RETSCALAR)
3692 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3693 o->op_targ = pad_alloc(type, SVs_PADTMP);
3698 PERL_STATIC_INLINE OP *
3699 S_op_integerize(pTHX_ OP *o)
3701 I32 type = o->op_type;
3703 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3705 /* integerize op. */
3706 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3709 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
3712 if (type == OP_NEGATE)
3713 /* XXX might want a ck_negate() for this */
3714 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3720 S_fold_constants(pTHX_ OP *o)
3725 VOL I32 type = o->op_type;
3730 SV * const oldwarnhook = PL_warnhook;
3731 SV * const olddiehook = PL_diehook;
3735 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3737 if (!(PL_opargs[type] & OA_FOLDCONST))
3746 #ifdef USE_LOCALE_CTYPE
3747 if (IN_LC_COMPILETIME(LC_CTYPE))
3756 #ifdef USE_LOCALE_COLLATE
3757 if (IN_LC_COMPILETIME(LC_COLLATE))
3762 /* XXX what about the numeric ops? */
3763 #ifdef USE_LOCALE_NUMERIC
3764 if (IN_LC_COMPILETIME(LC_NUMERIC))
3769 if (!OP_HAS_SIBLING(cLISTOPo->op_first)
3770 || OP_SIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
3773 SV * const sv = cSVOPx_sv(OP_SIBLING(cLISTOPo->op_first));
3774 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3776 const char *s = SvPVX_const(sv);
3777 while (s < SvEND(sv)) {
3778 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
3785 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3788 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
3789 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
3793 if (PL_parser && PL_parser->error_count)
3794 goto nope; /* Don't try to run w/ errors */
3796 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3797 const OPCODE type = curop->op_type;
3798 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3800 type != OP_SCALAR &&
3802 type != OP_PUSHMARK)
3808 curop = LINKLIST(o);
3809 old_next = o->op_next;
3813 oldscope = PL_scopestack_ix;
3814 create_eval_scope(G_FAKINGEVAL);
3816 /* Verify that we don't need to save it: */
3817 assert(PL_curcop == &PL_compiling);
3818 StructCopy(&PL_compiling, ¬_compiling, COP);
3819 PL_curcop = ¬_compiling;
3820 /* The above ensures that we run with all the correct hints of the
3821 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3822 assert(IN_PERL_RUNTIME);
3823 PL_warnhook = PERL_WARNHOOK_FATAL;
3830 sv = *(PL_stack_sp--);
3831 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3832 pad_swipe(o->op_targ, FALSE);
3834 else if (SvTEMP(sv)) { /* grab mortal temp? */
3835 SvREFCNT_inc_simple_void(sv);
3838 else { assert(SvIMMORTAL(sv)); }
3841 /* Something tried to die. Abandon constant folding. */
3842 /* Pretend the error never happened. */
3844 o->op_next = old_next;
3848 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3849 PL_warnhook = oldwarnhook;
3850 PL_diehook = olddiehook;
3851 /* XXX note that this croak may fail as we've already blown away
3852 * the stack - eg any nested evals */
3853 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3856 PL_warnhook = oldwarnhook;
3857 PL_diehook = olddiehook;
3858 PL_curcop = &PL_compiling;
3860 if (PL_scopestack_ix > oldscope)
3861 delete_eval_scope();
3868 if (type == OP_STRINGIFY) SvPADTMP_off(sv);
3869 else if (!SvIMMORTAL(sv)) {
3873 if (type == OP_RV2GV)
3874 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3877 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3878 if (type != OP_STRINGIFY) newop->op_folded = 1;
3887 S_gen_constant_list(pTHX_ OP *o)
3891 const SSize_t oldtmps_floor = PL_tmps_floor;
3896 if (PL_parser && PL_parser->error_count)
3897 return o; /* Don't attempt to run with errors */
3899 curop = LINKLIST(o);
3902 S_prune_chain_head(&curop);
3904 Perl_pp_pushmark(aTHX);
3907 assert (!(curop->op_flags & OPf_SPECIAL));
3908 assert(curop->op_type == OP_RANGE);
3909 Perl_pp_anonlist(aTHX);
3910 PL_tmps_floor = oldtmps_floor;
3912 o->op_type = OP_RV2AV;
3913 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3914 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3915 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3916 o->op_opt = 0; /* needs to be revisited in rpeep() */
3917 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
3919 /* replace subtree with an OP_CONST */
3920 curop = ((UNOP*)o)->op_first;
3921 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
3924 if (AvFILLp(av) != -1)
3925 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
3928 SvREADONLY_on(*svp);
3934 /* convert o (and any siblings) into a list if not already, then
3935 * convert the parent OP_LIST to type 'type', and CHECKOP() and fold it
3939 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3942 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3943 if (!o || o->op_type != OP_LIST)
3944 o = force_list(o, 0);
3946 o->op_flags &= ~OPf_WANT;
3948 if (!(PL_opargs[type] & OA_MARK))
3949 op_null(cLISTOPo->op_first);
3951 OP * const kid2 = OP_SIBLING(cLISTOPo->op_first);
3952 if (kid2 && kid2->op_type == OP_COREARGS) {
3953 op_null(cLISTOPo->op_first);
3954 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3958 o->op_type = (OPCODE)type;
3959 o->op_ppaddr = PL_ppaddr[type];
3960 o->op_flags |= flags;
3962 o = CHECKOP(type, o);
3963 if (o->op_type != (unsigned)type)
3966 return fold_constants(op_integerize(op_std_init(o)));
3970 =head1 Optree Manipulation Functions
3973 /* List constructors */
3976 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3978 Append an item to the list of ops contained directly within a list-type
3979 op, returning the lengthened list. I<first> is the list-type op,
3980 and I<last> is the op to append to the list. I<optype> specifies the
3981 intended opcode for the list. If I<first> is not already a list of the
3982 right type, it will be upgraded into one. If either I<first> or I<last>
3983 is null, the other is returned unchanged.
3989 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3997 if (first->op_type != (unsigned)type
3998 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4000 return newLISTOP(type, 0, first, last);
4003 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4004 first->op_flags |= OPf_KIDS;
4009 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4011 Concatenate the lists of ops contained directly within two list-type ops,
4012 returning the combined list. I<first> and I<last> are the list-type ops
4013 to concatenate. I<optype> specifies the intended opcode for the list.
4014 If either I<first> or I<last> is not already a list of the right type,
4015 it will be upgraded into one. If either I<first> or I<last> is null,
4016 the other is returned unchanged.
4022 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4030 if (first->op_type != (unsigned)type)
4031 return op_prepend_elem(type, first, last);
4033 if (last->op_type != (unsigned)type)
4034 return op_append_elem(type, first, last);
4036 ((LISTOP*)first)->op_last->op_lastsib = 0;
4037 OP_SIBLING_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4038 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4039 ((LISTOP*)first)->op_last->op_lastsib = 1;
4040 #ifdef PERL_OP_PARENT
4041 ((LISTOP*)first)->op_last->op_sibling = first;
4043 first->op_flags |= (last->op_flags & OPf_KIDS);
4046 S_op_destroy(aTHX_ last);
4052 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4054 Prepend an item to the list of ops contained directly within a list-type
4055 op, returning the lengthened list. I<first> is the op to prepend to the
4056 list, and I<last> is the list-type op. I<optype> specifies the intended
4057 opcode for the list. If I<last> is not already a list of the right type,
4058 it will be upgraded into one. If either I<first> or I<last> is null,
4059 the other is returned unchanged.
4065 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4073 if (last->op_type == (unsigned)type) {
4074 if (type == OP_LIST) { /* already a PUSHMARK there */
4075 /* insert 'first' after pushmark */
4076 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4077 if (!(first->op_flags & OPf_PARENS))
4078 last->op_flags &= ~OPf_PARENS;
4081 op_sibling_splice(last, NULL, 0, first);
4082 last->op_flags |= OPf_KIDS;
4086 return newLISTOP(type, 0, first, last);
4093 =head1 Optree construction
4095 =for apidoc Am|OP *|newNULLLIST
4097 Constructs, checks, and returns a new C<stub> op, which represents an
4098 empty list expression.
4104 Perl_newNULLLIST(pTHX)
4106 return newOP(OP_STUB, 0);
4109 /* promote o and any siblings to be a list if its not already; i.e.
4117 * pushmark - o - A - B
4119 * If nullit it true, the list op is nulled.
4123 S_force_list(pTHX_ OP *o, bool nullit)
4125 if (!o || o->op_type != OP_LIST) {
4128 /* manually detach any siblings then add them back later */
4129 rest = OP_SIBLING(o);
4130 OP_SIBLING_set(o, NULL);
4133 o = newLISTOP(OP_LIST, 0, o, NULL);
4135 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4143 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4145 Constructs, checks, and returns an op of any list type. I<type> is
4146 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4147 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
4148 supply up to two ops to be direct children of the list op; they are
4149 consumed by this function and become part of the constructed op tree.
4155 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4160 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
4162 NewOp(1101, listop, 1, LISTOP);
4164 listop->op_type = (OPCODE)type;
4165 listop->op_ppaddr = PL_ppaddr[type];
4168 listop->op_flags = (U8)flags;
4172 else if (!first && last)
4175 OP_SIBLING_set(first, last);
4176 listop->op_first = first;
4177 listop->op_last = last;
4178 if (type == OP_LIST) {
4179 OP* const pushop = newOP(OP_PUSHMARK, 0);
4180 pushop->op_lastsib = 0;
4181 OP_SIBLING_set(pushop, first);
4182 listop->op_first = pushop;
4183 listop->op_flags |= OPf_KIDS;
4185 listop->op_last = pushop;
4188 first->op_lastsib = 0;
4189 if (listop->op_last) {
4190 listop->op_last->op_lastsib = 1;
4191 #ifdef PERL_OP_PARENT
4192 listop->op_last->op_sibling = (OP*)listop;
4196 return CHECKOP(type, listop);
4200 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4202 Constructs, checks, and returns an op of any base type (any type that
4203 has no extra fields). I<type> is the opcode. I<flags> gives the
4204 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4211 Perl_newOP(pTHX_ I32 type, I32 flags)
4216 if (type == -OP_ENTEREVAL) {
4217 type = OP_ENTEREVAL;
4218 flags |= OPpEVAL_BYTES<<8;
4221 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4222 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4223 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4224 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4226 NewOp(1101, o, 1, OP);
4227 o->op_type = (OPCODE)type;
4228 o->op_ppaddr = PL_ppaddr[type];
4229 o->op_flags = (U8)flags;
4232 o->op_private = (U8)(0 | (flags >> 8));
4233 if (PL_opargs[type] & OA_RETSCALAR)
4235 if (PL_opargs[type] & OA_TARGET)
4236 o->op_targ = pad_alloc(type, SVs_PADTMP);
4237 return CHECKOP(type, o);
4241 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4243 Constructs, checks, and returns an op of any unary type. I<type> is
4244 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4245 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4246 bits, the eight bits of C<op_private>, except that the bit with value 1
4247 is automatically set. I<first> supplies an optional op to be the direct
4248 child of the unary op; it is consumed by this function and become part
4249 of the constructed op tree.
4255 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4260 if (type == -OP_ENTEREVAL) {
4261 type = OP_ENTEREVAL;
4262 flags |= OPpEVAL_BYTES<<8;
4265 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4266 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4267 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4268 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4269 || type == OP_SASSIGN
4270 || type == OP_ENTERTRY
4271 || type == OP_NULL );
4274 first = newOP(OP_STUB, 0);
4275 if (PL_opargs[type] & OA_MARK)
4276 first = force_list(first, 1);
4278 NewOp(1101, unop, 1, UNOP);
4279 unop->op_type = (OPCODE)type;
4280 unop->op_ppaddr = PL_ppaddr[type];
4281 unop->op_first = first;
4282 unop->op_flags = (U8)(flags | OPf_KIDS);
4283 unop->op_private = (U8)(1 | (flags >> 8));
4285 #ifdef PERL_OP_PARENT
4286 if (!OP_HAS_SIBLING(first)) /* true unless weird syntax error */
4287 first->op_sibling = (OP*)unop;
4290 unop = (UNOP*) CHECKOP(type, unop);
4294 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4298 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
4300 Constructs, checks, and returns an op of any binary type. I<type>
4301 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
4302 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
4303 the eight bits of C<op_private>, except that the bit with value 1 or
4304 2 is automatically set as required. I<first> and I<last> supply up to
4305 two ops to be the direct children of the binary op; they are consumed
4306 by this function and become part of the constructed op tree.
4312 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4317 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4318 || type == OP_SASSIGN || type == OP_NULL );
4320 NewOp(1101, binop, 1, BINOP);
4323 first = newOP(OP_NULL, 0);
4325 binop->op_type = (OPCODE)type;
4326 binop->op_ppaddr = PL_ppaddr[type];
4327 binop->op_first = first;
4328 binop->op_flags = (U8)(flags | OPf_KIDS);
4331 binop->op_private = (U8)(1 | (flags >> 8));
4334 binop->op_private = (U8)(2 | (flags >> 8));
4335 OP_SIBLING_set(first, last);
4336 first->op_lastsib = 0;
4339 #ifdef PERL_OP_PARENT
4340 if (!OP_HAS_SIBLING(last)) /* true unless weird syntax error */
4341 last->op_sibling = (OP*)binop;
4344 binop = (BINOP*)CHECKOP(type, binop);
4345 if (binop->op_next || binop->op_type != (OPCODE)type)
4348 binop->op_last = OP_SIBLING(binop->op_first);
4349 #ifdef PERL_OP_PARENT
4351 binop->op_last->op_sibling = (OP*)binop;
4354 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4357 static int uvcompare(const void *a, const void *b)
4358 __attribute__nonnull__(1)
4359 __attribute__nonnull__(2)
4360 __attribute__pure__;
4361 static int uvcompare(const void *a, const void *b)
4363 if (*((const UV *)a) < (*(const UV *)b))
4365 if (*((const UV *)a) > (*(const UV *)b))
4367 if (*((const UV *)a+1) < (*(const UV *)b+1))
4369 if (*((const UV *)a+1) > (*(const UV *)b+1))
4375 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4377 SV * const tstr = ((SVOP*)expr)->op_sv;
4379 ((SVOP*)repl)->op_sv;
4382 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4383 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4389 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4390 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4391 I32 del = o->op_private & OPpTRANS_DELETE;
4394 PERL_ARGS_ASSERT_PMTRANS;
4396 PL_hints |= HINT_BLOCK_SCOPE;
4399 o->op_private |= OPpTRANS_FROM_UTF;
4402 o->op_private |= OPpTRANS_TO_UTF;
4404 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4405 SV* const listsv = newSVpvs("# comment\n");
4407 const U8* tend = t + tlen;
4408 const U8* rend = r + rlen;
4422 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4423 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4426 const U32 flags = UTF8_ALLOW_DEFAULT;
4430 t = tsave = bytes_to_utf8(t, &len);
4433 if (!to_utf && rlen) {
4435 r = rsave = bytes_to_utf8(r, &len);
4439 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has
4440 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
4444 U8 tmpbuf[UTF8_MAXBYTES+1];
4447 Newx(cp, 2*tlen, UV);
4449 transv = newSVpvs("");
4451 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4453 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
4455 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4459 cp[2*i+1] = cp[2*i];
4463 qsort(cp, i, 2*sizeof(UV), uvcompare);
4464 for (j = 0; j < i; j++) {
4466 diff = val - nextmin;
4468 t = uvchr_to_utf8(tmpbuf,nextmin);
4469 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4471 U8 range_mark = ILLEGAL_UTF8_BYTE;
4472 t = uvchr_to_utf8(tmpbuf, val - 1);
4473 sv_catpvn(transv, (char *)&range_mark, 1);
4474 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4481 t = uvchr_to_utf8(tmpbuf,nextmin);
4482 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4484 U8 range_mark = ILLEGAL_UTF8_BYTE;
4485 sv_catpvn(transv, (char *)&range_mark, 1);
4487 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
4488 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4489 t = (const U8*)SvPVX_const(transv);
4490 tlen = SvCUR(transv);
4494 else if (!rlen && !del) {
4495 r = t; rlen = tlen; rend = tend;
4498 if ((!rlen && !del) || t == r ||
4499 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4501 o->op_private |= OPpTRANS_IDENTICAL;
4505 while (t < tend || tfirst <= tlast) {
4506 /* see if we need more "t" chars */
4507 if (tfirst > tlast) {
4508 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4510 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4512 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4519 /* now see if we need more "r" chars */
4520 if (rfirst > rlast) {
4522 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4524 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4526 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4535 rfirst = rlast = 0xffffffff;
4539 /* now see which range will peter our first, if either. */
4540 tdiff = tlast - tfirst;
4541 rdiff = rlast - rfirst;
4548 if (rfirst == 0xffffffff) {
4549 diff = tdiff; /* oops, pretend rdiff is infinite */
4551 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4552 (long)tfirst, (long)tlast);
4554 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4558 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4559 (long)tfirst, (long)(tfirst + diff),
4562 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4563 (long)tfirst, (long)rfirst);
4565 if (rfirst + diff > max)
4566 max = rfirst + diff;
4568 grows = (tfirst < rfirst &&
4569 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4581 else if (max > 0xff)
4586 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4588 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
4589 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4590 PAD_SETSV(cPADOPo->op_padix, swash);
4592 SvREADONLY_on(swash);
4594 cSVOPo->op_sv = swash;
4596 SvREFCNT_dec(listsv);
4597 SvREFCNT_dec(transv);
4599 if (!del && havefinal && rlen)
4600 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4601 newSVuv((UV)final), 0);
4604 o->op_private |= OPpTRANS_GROWS;
4614 tbl = (short*)PerlMemShared_calloc(
4615 (o->op_private & OPpTRANS_COMPLEMENT) &&
4616 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4618 cPVOPo->op_pv = (char*)tbl;
4620 for (i = 0; i < (I32)tlen; i++)
4622 for (i = 0, j = 0; i < 256; i++) {
4624 if (j >= (I32)rlen) {
4633 if (i < 128 && r[j] >= 128)
4643 o->op_private |= OPpTRANS_IDENTICAL;
4645 else if (j >= (I32)rlen)
4650 PerlMemShared_realloc(tbl,
4651 (0x101+rlen-j) * sizeof(short));
4652 cPVOPo->op_pv = (char*)tbl;
4654 tbl[0x100] = (short)(rlen - j);
4655 for (i=0; i < (I32)rlen - j; i++)
4656 tbl[0x101+i] = r[j+i];
4660 if (!rlen && !del) {
4663 o->op_private |= OPpTRANS_IDENTICAL;
4665 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4666 o->op_private |= OPpTRANS_IDENTICAL;
4668 for (i = 0; i < 256; i++)
4670 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4671 if (j >= (I32)rlen) {
4673 if (tbl[t[i]] == -1)
4679 if (tbl[t[i]] == -1) {
4680 if (t[i] < 128 && r[j] >= 128)
4687 if(del && rlen == tlen) {
4688 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4689 } else if(rlen > tlen && !complement) {
4690 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4694 o->op_private |= OPpTRANS_GROWS;
4702 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4704 Constructs, checks, and returns an op of any pattern matching type.
4705 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4706 and, shifted up eight bits, the eight bits of C<op_private>.
4712 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4717 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4719 NewOp(1101, pmop, 1, PMOP);
4720 pmop->op_type = (OPCODE)type;
4721 pmop->op_ppaddr = PL_ppaddr[type];
4722 pmop->op_flags = (U8)flags;
4723 pmop->op_private = (U8)(0 | (flags >> 8));
4725 if (PL_hints & HINT_RE_TAINT)
4726 pmop->op_pmflags |= PMf_RETAINT;
4727 #ifdef USE_LOCALE_CTYPE
4728 if (IN_LC_COMPILETIME(LC_CTYPE)) {
4729 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4734 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4736 if (PL_hints & HINT_RE_FLAGS) {
4737 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4738 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4740 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4741 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4742 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4744 if (reflags && SvOK(reflags)) {
4745 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4751 assert(SvPOK(PL_regex_pad[0]));
4752 if (SvCUR(PL_regex_pad[0])) {
4753 /* Pop off the "packed" IV from the end. */
4754 SV *const repointer_list = PL_regex_pad[0];
4755 const char *p = SvEND(repointer_list) - sizeof(IV);
4756 const IV offset = *((IV*)p);
4758 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4760 SvEND_set(repointer_list, p);
4762 pmop->op_pmoffset = offset;
4763 /* This slot should be free, so assert this: */
4764 assert(PL_regex_pad[offset] == &PL_sv_undef);
4766 SV * const repointer = &PL_sv_undef;
4767 av_push(PL_regex_padav, repointer);
4768 pmop->op_pmoffset = av_tindex(PL_regex_padav);
4769 PL_regex_pad = AvARRAY(PL_regex_padav);
4773 return CHECKOP(type, pmop);
4776 /* Given some sort of match op o, and an expression expr containing a
4777 * pattern, either compile expr into a regex and attach it to o (if it's
4778 * constant), or convert expr into a runtime regcomp op sequence (if it's
4781 * isreg indicates that the pattern is part of a regex construct, eg
4782 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4783 * split "pattern", which aren't. In the former case, expr will be a list
4784 * if the pattern contains more than one term (eg /a$b/) or if it contains
4785 * a replacement, ie s/// or tr///.
4787 * When the pattern has been compiled within a new anon CV (for
4788 * qr/(?{...})/ ), then floor indicates the savestack level just before
4789 * the new sub was created
4793 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4798 I32 repl_has_vars = 0;
4800 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4801 bool is_compiletime;
4804 PERL_ARGS_ASSERT_PMRUNTIME;
4806 /* for s/// and tr///, last element in list is the replacement; pop it */
4808 if (is_trans || o->op_type == OP_SUBST) {
4810 repl = cLISTOPx(expr)->op_last;
4811 kid = cLISTOPx(expr)->op_first;
4812 while (OP_SIBLING(kid) != repl)
4813 kid = OP_SIBLING(kid);
4814 op_sibling_splice(expr, kid, 1, NULL);
4817 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4822 assert(expr->op_type == OP_LIST);
4823 first = cLISTOPx(expr)->op_first;
4824 last = cLISTOPx(expr)->op_last;
4825 assert(first->op_type == OP_PUSHMARK);
4826 assert(OP_SIBLING(first) == last);
4828 /* cut 'last' from sibling chain, then free everything else */
4829 op_sibling_splice(expr, first, 1, NULL);
4832 return pmtrans(o, last, repl);
4835 /* find whether we have any runtime or code elements;
4836 * at the same time, temporarily set the op_next of each DO block;
4837 * then when we LINKLIST, this will cause the DO blocks to be excluded
4838 * from the op_next chain (and from having LINKLIST recursively
4839 * applied to them). We fix up the DOs specially later */
4843 if (expr->op_type == OP_LIST) {
4845 for (o = cLISTOPx(expr)->op_first; o; o = OP_SIBLING(o)) {
4846 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4848 assert(!o->op_next && OP_HAS_SIBLING(o));
4849 o->op_next = OP_SIBLING(o);
4851 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4855 else if (expr->op_type != OP_CONST)
4860 /* fix up DO blocks; treat each one as a separate little sub;
4861 * also, mark any arrays as LIST/REF */
4863 if (expr->op_type == OP_LIST) {
4865 for (o = cLISTOPx(expr)->op_first; o; o = OP_SIBLING(o)) {
4867 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
4868 assert( !(o->op_flags & OPf_WANT));
4869 /* push the array rather than its contents. The regex
4870 * engine will retrieve and join the elements later */
4871 o->op_flags |= (OPf_WANT_LIST | OPf_REF);