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_no_fh_allowed(pTHX_ OP *o)
502 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
504 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
510 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
512 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
513 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
518 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
520 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
522 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
527 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
529 PERL_ARGS_ASSERT_BAD_TYPE_PV;
531 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
532 (int)n, name, t, OP_DESC(kid)), flags);
536 S_bad_type_gv(pTHX_ I32 n, const char *t, GV *gv, U32 flags, const OP *kid)
538 SV * const namesv = cv_name((CV *)gv, NULL, 0);
539 PERL_ARGS_ASSERT_BAD_TYPE_GV;
541 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
542 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
546 S_no_bareword_allowed(pTHX_ OP *o)
548 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
550 qerror(Perl_mess(aTHX_
551 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
553 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
556 /* "register" allocation */
559 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
562 const bool is_our = (PL_parser->in_my == KEY_our);
564 PERL_ARGS_ASSERT_ALLOCMY;
566 if (flags & ~SVf_UTF8)
567 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
570 /* complain about "my $<special_var>" etc etc */
574 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
575 (name[1] == '_' && (*name == '$' || len > 2))))
577 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
578 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
579 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
580 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
581 PL_parser->in_my == KEY_state ? "state" : "my"));
583 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
584 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
587 else if (len == 2 && name[1] == '_' && !is_our)
588 /* diag_listed_as: Use of my $_ is experimental */
589 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
590 "Use of %s $_ is experimental",
591 PL_parser->in_my == KEY_state
595 /* allocate a spare slot and store the name in that slot */
597 off = pad_add_name_pvn(name, len,
598 (is_our ? padadd_OUR :
599 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
600 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
601 PL_parser->in_my_stash,
603 /* $_ is always in main::, even with our */
604 ? (PL_curstash && !memEQs(name,len,"$_")
610 /* anon sub prototypes contains state vars should always be cloned,
611 * otherwise the state var would be shared between anon subs */
613 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
614 CvCLONE_on(PL_compcv);
620 =head1 Optree Manipulation Functions
622 =for apidoc alloccopstash
624 Available only under threaded builds, this function allocates an entry in
625 C<PL_stashpad> for the stash passed to it.
632 Perl_alloccopstash(pTHX_ HV *hv)
634 PADOFFSET off = 0, o = 1;
635 bool found_slot = FALSE;
637 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
639 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
641 for (; o < PL_stashpadmax; ++o) {
642 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
643 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
644 found_slot = TRUE, off = o;
647 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
648 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
649 off = PL_stashpadmax;
650 PL_stashpadmax += 10;
653 PL_stashpad[PL_stashpadix = off] = hv;
658 /* free the body of an op without examining its contents.
659 * Always use this rather than FreeOp directly */
662 S_op_destroy(pTHX_ OP *o)
670 =for apidoc Am|void|op_free|OP *o
672 Free an op. Only use this when an op is no longer linked to from any
679 Perl_op_free(pTHX_ OP *o)
684 /* Though ops may be freed twice, freeing the op after its slab is a
686 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
687 /* During the forced freeing of ops after compilation failure, kidops
688 may be freed before their parents. */
689 if (!o || o->op_type == OP_FREED)
694 /* an op should only ever acquire op_private flags that we know about.
695 * If this fails, you may need to fix something in regen/op_private */
696 if (o->op_ppaddr == PL_ppaddr[o->op_type]) {
697 assert(!(o->op_private & ~PL_op_private_valid[type]));
700 if (o->op_private & OPpREFCOUNTED) {
711 refcnt = OpREFCNT_dec(o);
714 /* Need to find and remove any pattern match ops from the list
715 we maintain for reset(). */
716 find_and_forget_pmops(o);
726 /* Call the op_free hook if it has been set. Do it now so that it's called
727 * at the right time for refcounted ops, but still before all of the kids
731 if (o->op_flags & OPf_KIDS) {
733 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
734 nextkid = OP_SIBLING(kid); /* Get before next freeing kid */
739 type = (OPCODE)o->op_targ;
742 Slab_to_rw(OpSLAB(o));
744 /* COP* is not cleared by op_clear() so that we may track line
745 * numbers etc even after null() */
746 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
752 #ifdef DEBUG_LEAKING_SCALARS
759 Perl_op_clear(pTHX_ OP *o)
764 PERL_ARGS_ASSERT_OP_CLEAR;
766 switch (o->op_type) {
767 case OP_NULL: /* Was holding old type, if any. */
770 case OP_ENTEREVAL: /* Was holding hints. */
774 if (!(o->op_flags & OPf_REF)
775 || (PL_check[o->op_type] != Perl_ck_ftst))
782 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
787 /* It's possible during global destruction that the GV is freed
788 before the optree. Whilst the SvREFCNT_inc is happy to bump from
789 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
790 will trigger an assertion failure, because the entry to sv_clear
791 checks that the scalar is not already freed. A check of for
792 !SvIS_FREED(gv) turns out to be invalid, because during global
793 destruction the reference count can be forced down to zero
794 (with SVf_BREAK set). In which case raising to 1 and then
795 dropping to 0 triggers cleanup before it should happen. I
796 *think* that this might actually be a general, systematic,
797 weakness of the whole idea of SVf_BREAK, in that code *is*
798 allowed to raise and lower references during global destruction,
799 so any *valid* code that happens to do this during global
800 destruction might well trigger premature cleanup. */
801 bool still_valid = gv && SvREFCNT(gv);
804 SvREFCNT_inc_simple_void(gv);
806 if (cPADOPo->op_padix > 0) {
807 pad_swipe(cPADOPo->op_padix, TRUE);
808 cPADOPo->op_padix = 0;
811 SvREFCNT_dec(cSVOPo->op_sv);
812 cSVOPo->op_sv = NULL;
815 int try_downgrade = SvREFCNT(gv) == 2;
818 gv_try_downgrade(gv);
822 case OP_METHOD_NAMED:
823 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
824 cMETHOPx(o)->op_u.op_meth_sv = NULL;
827 pad_swipe(o->op_targ, 1);
834 SvREFCNT_dec(cSVOPo->op_sv);
835 cSVOPo->op_sv = NULL;
838 Even if op_clear does a pad_free for the target of the op,
839 pad_free doesn't actually remove the sv that exists in the pad;
840 instead it lives on. This results in that it could be reused as
841 a target later on when the pad was reallocated.
844 pad_swipe(o->op_targ,1);
854 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
859 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
860 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
862 if (cPADOPo->op_padix > 0) {
863 pad_swipe(cPADOPo->op_padix, TRUE);
864 cPADOPo->op_padix = 0;
867 SvREFCNT_dec(cSVOPo->op_sv);
868 cSVOPo->op_sv = NULL;
872 PerlMemShared_free(cPVOPo->op_pv);
873 cPVOPo->op_pv = NULL;
877 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
881 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
882 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
885 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
891 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
892 op_free(cPMOPo->op_code_list);
893 cPMOPo->op_code_list = NULL;
895 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
896 /* we use the same protection as the "SAFE" version of the PM_ macros
897 * here since sv_clean_all might release some PMOPs
898 * after PL_regex_padav has been cleared
899 * and the clearing of PL_regex_padav needs to
900 * happen before sv_clean_all
903 if(PL_regex_pad) { /* We could be in destruction */
904 const IV offset = (cPMOPo)->op_pmoffset;
905 ReREFCNT_dec(PM_GETRE(cPMOPo));
906 PL_regex_pad[offset] = &PL_sv_undef;
907 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
911 ReREFCNT_dec(PM_GETRE(cPMOPo));
912 PM_SETRE(cPMOPo, NULL);
918 if (o->op_targ > 0) {
919 pad_free(o->op_targ);
925 S_cop_free(pTHX_ COP* cop)
927 PERL_ARGS_ASSERT_COP_FREE;
930 if (! specialWARN(cop->cop_warnings))
931 PerlMemShared_free(cop->cop_warnings);
932 cophh_free(CopHINTHASH_get(cop));
933 if (PL_curcop == cop)
938 S_forget_pmop(pTHX_ PMOP *const o
941 HV * const pmstash = PmopSTASH(o);
943 PERL_ARGS_ASSERT_FORGET_PMOP;
945 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
946 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
948 PMOP **const array = (PMOP**) mg->mg_ptr;
949 U32 count = mg->mg_len / sizeof(PMOP**);
954 /* Found it. Move the entry at the end to overwrite it. */
955 array[i] = array[--count];
956 mg->mg_len = count * sizeof(PMOP**);
957 /* Could realloc smaller at this point always, but probably
958 not worth it. Probably worth free()ing if we're the
961 Safefree(mg->mg_ptr);
974 S_find_and_forget_pmops(pTHX_ OP *o)
976 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
978 if (o->op_flags & OPf_KIDS) {
979 OP *kid = cUNOPo->op_first;
981 switch (kid->op_type) {
986 forget_pmop((PMOP*)kid);
988 find_and_forget_pmops(kid);
989 kid = OP_SIBLING(kid);
995 =for apidoc Am|void|op_null|OP *o
997 Neutralizes an op when it is no longer needed, but is still linked to from
1004 Perl_op_null(pTHX_ OP *o)
1008 PERL_ARGS_ASSERT_OP_NULL;
1010 if (o->op_type == OP_NULL)
1013 o->op_targ = o->op_type;
1014 o->op_type = OP_NULL;
1015 o->op_ppaddr = PL_ppaddr[OP_NULL];
1019 Perl_op_refcnt_lock(pTHX)
1024 PERL_UNUSED_CONTEXT;
1029 Perl_op_refcnt_unlock(pTHX)
1034 PERL_UNUSED_CONTEXT;
1040 =for apidoc op_sibling_splice
1042 A general function for editing the structure of an existing chain of
1043 op_sibling nodes. By analogy with the perl-level splice() function, allows
1044 you to delete zero or more sequential nodes, replacing them with zero or
1045 more different nodes. Performs the necessary op_first/op_last
1046 housekeeping on the parent node and op_sibling manipulation on the
1047 children. The last deleted node will be marked as as the last node by
1048 updating the op_sibling or op_lastsib field as appropriate.
1050 Note that op_next is not manipulated, and nodes are not freed; that is the
1051 responsibility of the caller. It also won't create a new list op for an
1052 empty list etc; use higher-level functions like op_append_elem() for that.
1054 parent is the parent node of the sibling chain.
1056 start is the node preceding the first node to be spliced. Node(s)
1057 following it will be deleted, and ops will be inserted after it. If it is
1058 NULL, the first node onwards is deleted, and nodes are inserted at the
1061 del_count is the number of nodes to delete. If zero, no nodes are deleted.
1062 If -1 or greater than or equal to the number of remaining kids, all
1063 remaining kids are deleted.
1065 insert is the first of a chain of nodes to be inserted in place of the nodes.
1066 If NULL, no nodes are inserted.
1068 The head of the chain of deleted ops is returned, or NULL if no ops were
1073 action before after returns
1074 ------ ----- ----- -------
1077 splice(P, A, 2, X-Y-Z) | | B-C
1081 splice(P, NULL, 1, X-Y) | | A
1085 splice(P, NULL, 3, NULL) | | A-B-C
1089 splice(P, B, 0, X-Y) | | NULL
1096 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1098 OP *first = start ? OP_SIBLING(start) : cLISTOPx(parent)->op_first;
1100 OP *last_del = NULL;
1101 OP *last_ins = NULL;
1103 PERL_ARGS_ASSERT_OP_SIBLING_SPLICE;
1105 assert(del_count >= -1);
1107 if (del_count && first) {
1109 while (--del_count && OP_HAS_SIBLING(last_del))
1110 last_del = OP_SIBLING(last_del);
1111 rest = OP_SIBLING(last_del);
1112 OP_SIBLING_set(last_del, NULL);
1113 last_del->op_lastsib = 1;
1120 while (OP_HAS_SIBLING(last_ins))
1121 last_ins = OP_SIBLING(last_ins);
1122 OP_SIBLING_set(last_ins, rest);
1123 last_ins->op_lastsib = rest ? 0 : 1;
1129 OP_SIBLING_set(start, insert);
1130 start->op_lastsib = insert ? 0 : 1;
1133 cLISTOPx(parent)->op_first = insert;
1136 /* update op_last etc */
1137 U32 type = parent->op_type;
1140 if (type == OP_NULL)
1141 type = parent->op_targ;
1142 type = PL_opargs[type] & OA_CLASS_MASK;
1144 lastop = last_ins ? last_ins : start ? start : NULL;
1145 if ( type == OA_BINOP
1146 || type == OA_LISTOP
1150 cLISTOPx(parent)->op_last = lastop;
1153 lastop->op_lastsib = 1;
1154 #ifdef PERL_OP_PARENT
1155 lastop->op_sibling = parent;
1159 return last_del ? first : NULL;
1163 =for apidoc op_parent
1165 returns the parent OP of o, if it has a parent. Returns NULL otherwise.
1166 (Currently perl must be built with C<-DPERL_OP_PARENT> for this feature to
1173 Perl_op_parent(OP *o)
1175 PERL_ARGS_ASSERT_OP_PARENT;
1176 #ifdef PERL_OP_PARENT
1177 while (OP_HAS_SIBLING(o))
1179 return o->op_sibling;
1187 /* replace the sibling following start with a new UNOP, which becomes
1188 * the parent of the original sibling; e.g.
1190 * op_sibling_newUNOP(P, A, unop-args...)
1198 * where U is the new UNOP.
1200 * parent and start args are the same as for op_sibling_splice();
1201 * type and flags args are as newUNOP().
1203 * Returns the new UNOP.
1207 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1211 kid = op_sibling_splice(parent, start, 1, NULL);
1212 newop = newUNOP(type, flags, kid);
1213 op_sibling_splice(parent, start, 0, newop);
1218 /* lowest-level newLOGOP-style function - just allocates and populates
1219 * the struct. Higher-level stuff should be done by S_new_logop() /
1220 * newLOGOP(). This function exists mainly to avoid op_first assignment
1221 * being spread throughout this file.
1225 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1229 NewOp(1101, logop, 1, LOGOP);
1230 logop->op_type = (OPCODE)type;
1231 logop->op_first = first;
1232 logop->op_other = other;
1233 logop->op_flags = OPf_KIDS;
1234 while (kid && OP_HAS_SIBLING(kid))
1235 kid = OP_SIBLING(kid);
1237 kid->op_lastsib = 1;
1238 #ifdef PERL_OP_PARENT
1239 kid->op_sibling = (OP*)logop;
1246 /* Contextualizers */
1249 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1251 Applies a syntactic context to an op tree representing an expression.
1252 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1253 or C<G_VOID> to specify the context to apply. The modified op tree
1260 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1262 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1264 case G_SCALAR: return scalar(o);
1265 case G_ARRAY: return list(o);
1266 case G_VOID: return scalarvoid(o);
1268 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1275 =for apidoc Am|OP*|op_linklist|OP *o
1276 This function is the implementation of the L</LINKLIST> macro. It should
1277 not be called directly.
1283 Perl_op_linklist(pTHX_ OP *o)
1287 PERL_ARGS_ASSERT_OP_LINKLIST;
1292 /* establish postfix order */
1293 first = cUNOPo->op_first;
1296 o->op_next = LINKLIST(first);
1299 OP *sibl = OP_SIBLING(kid);
1301 kid->op_next = LINKLIST(sibl);
1316 S_scalarkids(pTHX_ OP *o)
1318 if (o && o->op_flags & OPf_KIDS) {
1320 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1327 S_scalarboolean(pTHX_ OP *o)
1329 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1331 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1332 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1333 if (ckWARN(WARN_SYNTAX)) {
1334 const line_t oldline = CopLINE(PL_curcop);
1336 if (PL_parser && PL_parser->copline != NOLINE) {
1337 /* This ensures that warnings are reported at the first line
1338 of the conditional, not the last. */
1339 CopLINE_set(PL_curcop, PL_parser->copline);
1341 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1342 CopLINE_set(PL_curcop, oldline);
1349 S_op_varname(pTHX_ const OP *o)
1352 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1353 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1355 const char funny = o->op_type == OP_PADAV
1356 || o->op_type == OP_RV2AV ? '@' : '%';
1357 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1359 if (cUNOPo->op_first->op_type != OP_GV
1360 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1362 return varname(gv, funny, 0, NULL, 0, 1);
1365 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1370 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1371 { /* or not so pretty :-) */
1372 if (o->op_type == OP_CONST) {
1374 if (SvPOK(*retsv)) {
1376 *retsv = sv_newmortal();
1377 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1378 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1380 else if (!SvOK(*retsv))
1383 else *retpv = "...";
1387 S_scalar_slice_warning(pTHX_ const OP *o)
1391 o->op_type == OP_HSLICE ? '{' : '[';
1393 o->op_type == OP_HSLICE ? '}' : ']';
1395 SV *keysv = NULL; /* just to silence compiler warnings */
1396 const char *key = NULL;
1398 if (!(o->op_private & OPpSLICEWARNING))
1400 if (PL_parser && PL_parser->error_count)
1401 /* This warning can be nonsensical when there is a syntax error. */
1404 kid = cLISTOPo->op_first;
1405 kid = OP_SIBLING(kid); /* get past pushmark */
1406 /* weed out false positives: any ops that can return lists */
1407 switch (kid->op_type) {
1436 /* Don't warn if we have a nulled list either. */
1437 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1440 assert(OP_SIBLING(kid));
1441 name = S_op_varname(aTHX_ OP_SIBLING(kid));
1442 if (!name) /* XS module fiddling with the op tree */
1444 S_op_pretty(aTHX_ kid, &keysv, &key);
1445 assert(SvPOK(name));
1446 sv_chop(name,SvPVX(name)+1);
1448 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1449 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1450 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1452 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1453 lbrack, key, rbrack);
1455 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1456 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1457 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1459 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1460 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1464 Perl_scalar(pTHX_ OP *o)
1468 /* assumes no premature commitment */
1469 if (!o || (PL_parser && PL_parser->error_count)
1470 || (o->op_flags & OPf_WANT)
1471 || o->op_type == OP_RETURN)
1476 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1478 switch (o->op_type) {
1480 scalar(cBINOPo->op_first);
1485 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1495 if (o->op_flags & OPf_KIDS) {
1496 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid))
1502 kid = cLISTOPo->op_first;
1504 kid = OP_SIBLING(kid);
1507 OP *sib = OP_SIBLING(kid);
1508 if (sib && kid->op_type != OP_LEAVEWHEN)
1514 PL_curcop = &PL_compiling;
1519 kid = cLISTOPo->op_first;
1522 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1527 /* Warn about scalar context */
1528 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1529 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1532 const char *key = NULL;
1534 /* This warning can be nonsensical when there is a syntax error. */
1535 if (PL_parser && PL_parser->error_count)
1538 if (!ckWARN(WARN_SYNTAX)) break;
1540 kid = cLISTOPo->op_first;
1541 kid = OP_SIBLING(kid); /* get past pushmark */
1542 assert(OP_SIBLING(kid));
1543 name = S_op_varname(aTHX_ OP_SIBLING(kid));
1544 if (!name) /* XS module fiddling with the op tree */
1546 S_op_pretty(aTHX_ kid, &keysv, &key);
1547 assert(SvPOK(name));
1548 sv_chop(name,SvPVX(name)+1);
1550 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1551 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1552 "%%%"SVf"%c%s%c in scalar context better written "
1554 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1555 lbrack, key, rbrack);
1557 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1558 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1559 "%%%"SVf"%c%"SVf"%c in scalar context better "
1560 "written as $%"SVf"%c%"SVf"%c",
1561 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1562 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1569 Perl_scalarvoid(pTHX_ OP *o)
1573 SV *useless_sv = NULL;
1574 const char* useless = NULL;
1578 PERL_ARGS_ASSERT_SCALARVOID;
1580 if (o->op_type == OP_NEXTSTATE
1581 || o->op_type == OP_DBSTATE
1582 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1583 || o->op_targ == OP_DBSTATE)))
1584 PL_curcop = (COP*)o; /* for warning below */
1586 /* assumes no premature commitment */
1587 want = o->op_flags & OPf_WANT;
1588 if ((want && want != OPf_WANT_SCALAR)
1589 || (PL_parser && PL_parser->error_count)
1590 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1595 if ((o->op_private & OPpTARGET_MY)
1596 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1598 return scalar(o); /* As if inside SASSIGN */
1601 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1603 switch (o->op_type) {
1605 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1609 if (o->op_flags & OPf_STACKED)
1613 if (o->op_private == 4)
1638 case OP_AELEMFAST_LEX:
1659 case OP_GETSOCKNAME:
1660 case OP_GETPEERNAME:
1665 case OP_GETPRIORITY:
1690 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1691 /* Otherwise it's "Useless use of grep iterator" */
1692 useless = OP_DESC(o);
1696 kid = cLISTOPo->op_first;
1697 if (kid && kid->op_type == OP_PUSHRE
1699 && !(o->op_flags & OPf_STACKED)
1701 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1703 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1705 useless = OP_DESC(o);
1709 kid = cUNOPo->op_first;
1710 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1711 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1714 useless = "negative pattern binding (!~)";
1718 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1719 useless = "non-destructive substitution (s///r)";
1723 useless = "non-destructive transliteration (tr///r)";
1730 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1731 (!OP_HAS_SIBLING(o) || OP_SIBLING(o)->op_type != OP_READLINE))
1732 useless = "a variable";
1737 if (cSVOPo->op_private & OPpCONST_STRICT)
1738 no_bareword_allowed(o);
1740 if (ckWARN(WARN_VOID)) {
1742 /* don't warn on optimised away booleans, eg
1743 * use constant Foo, 5; Foo || print; */
1744 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1746 /* the constants 0 and 1 are permitted as they are
1747 conventionally used as dummies in constructs like
1748 1 while some_condition_with_side_effects; */
1749 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
1751 else if (SvPOK(sv)) {
1752 SV * const dsv = newSVpvs("");
1754 = Perl_newSVpvf(aTHX_
1756 pv_pretty(dsv, SvPVX_const(sv),
1757 SvCUR(sv), 32, NULL, NULL,
1759 | PERL_PV_ESCAPE_NOCLEAR
1760 | PERL_PV_ESCAPE_UNI_DETECT));
1761 SvREFCNT_dec_NN(dsv);
1763 else if (SvOK(sv)) {
1764 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1767 useless = "a constant (undef)";
1770 op_null(o); /* don't execute or even remember it */
1774 o->op_type = OP_PREINC; /* pre-increment is faster */
1775 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1779 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1780 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1784 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1785 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1789 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1790 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1795 UNOP *refgen, *rv2cv;
1798 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1801 rv2gv = ((BINOP *)o)->op_last;
1802 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1805 refgen = (UNOP *)((BINOP *)o)->op_first;
1807 if (!refgen || (refgen->op_type != OP_REFGEN
1808 && refgen->op_type != OP_SREFGEN))
1811 exlist = (LISTOP *)refgen->op_first;
1812 if (!exlist || exlist->op_type != OP_NULL
1813 || exlist->op_targ != OP_LIST)
1816 if (exlist->op_first->op_type != OP_PUSHMARK
1817 && exlist->op_first != exlist->op_last)
1820 rv2cv = (UNOP*)exlist->op_last;
1822 if (rv2cv->op_type != OP_RV2CV)
1825 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1826 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1827 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1829 o->op_private |= OPpASSIGN_CV_TO_GV;
1830 rv2gv->op_private |= OPpDONT_INIT_GV;
1831 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1843 kid = cLOGOPo->op_first;
1844 if (kid->op_type == OP_NOT
1845 && (kid->op_flags & OPf_KIDS)) {
1846 if (o->op_type == OP_AND) {
1848 o->op_ppaddr = PL_ppaddr[OP_OR];
1850 o->op_type = OP_AND;
1851 o->op_ppaddr = PL_ppaddr[OP_AND];
1861 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1866 if (o->op_flags & OPf_STACKED)
1873 if (!(o->op_flags & OPf_KIDS))
1884 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1888 /* If the first kid after pushmark is something that the padrange
1889 optimisation would reject, then null the list and the pushmark.
1891 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
1892 && ( !(kid = OP_SIBLING(kid))
1893 || ( kid->op_type != OP_PADSV
1894 && kid->op_type != OP_PADAV
1895 && kid->op_type != OP_PADHV)
1896 || kid->op_private & ~OPpLVAL_INTRO
1897 || !(kid = OP_SIBLING(kid))
1898 || ( kid->op_type != OP_PADSV
1899 && kid->op_type != OP_PADAV
1900 && kid->op_type != OP_PADHV)
1901 || kid->op_private & ~OPpLVAL_INTRO)
1903 op_null(cUNOPo->op_first); /* NULL the pushmark */
1904 op_null(o); /* NULL the list */
1915 /* mortalise it, in case warnings are fatal. */
1916 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1917 "Useless use of %"SVf" in void context",
1918 SVfARG(sv_2mortal(useless_sv)));
1921 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1922 "Useless use of %s in void context",
1929 S_listkids(pTHX_ OP *o)
1931 if (o && o->op_flags & OPf_KIDS) {
1933 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1940 Perl_list(pTHX_ OP *o)
1944 /* assumes no premature commitment */
1945 if (!o || (o->op_flags & OPf_WANT)
1946 || (PL_parser && PL_parser->error_count)
1947 || o->op_type == OP_RETURN)
1952 if ((o->op_private & OPpTARGET_MY)
1953 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1955 return o; /* As if inside SASSIGN */
1958 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1960 switch (o->op_type) {
1962 list(cBINOPo->op_first);
1965 if (o->op_private & OPpREPEAT_DOLIST
1966 && !(o->op_flags & OPf_STACKED))
1968 list(cBINOPo->op_first);
1969 kid = cBINOPo->op_last;
1970 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
1971 && SvIVX(kSVOP_sv) == 1)
1973 op_null(o); /* repeat */
1974 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
1976 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
1983 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1991 if (!(o->op_flags & OPf_KIDS))
1993 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1994 list(cBINOPo->op_first);
1995 return gen_constant_list(o);
2001 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2002 op_null(cUNOPo->op_first); /* NULL the pushmark */
2003 op_null(o); /* NULL the list */
2008 kid = cLISTOPo->op_first;
2010 kid = OP_SIBLING(kid);
2013 OP *sib = OP_SIBLING(kid);
2014 if (sib && kid->op_type != OP_LEAVEWHEN)
2020 PL_curcop = &PL_compiling;
2024 kid = cLISTOPo->op_first;
2031 S_scalarseq(pTHX_ OP *o)
2034 const OPCODE type = o->op_type;
2036 if (type == OP_LINESEQ || type == OP_SCOPE ||
2037 type == OP_LEAVE || type == OP_LEAVETRY)
2040 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2041 if (OP_HAS_SIBLING(kid)) {
2045 PL_curcop = &PL_compiling;
2047 o->op_flags &= ~OPf_PARENS;
2048 if (PL_hints & HINT_BLOCK_SCOPE)
2049 o->op_flags |= OPf_PARENS;
2052 o = newOP(OP_STUB, 0);
2057 S_modkids(pTHX_ OP *o, I32 type)
2059 if (o && o->op_flags & OPf_KIDS) {
2061 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2062 op_lvalue(kid, type);
2068 =for apidoc finalize_optree
2070 This function finalizes the optree. Should be called directly after
2071 the complete optree is built. It does some additional
2072 checking which can't be done in the normal ck_xxx functions and makes
2073 the tree thread-safe.
2078 Perl_finalize_optree(pTHX_ OP* o)
2080 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2083 SAVEVPTR(PL_curcop);
2091 /* Relocate sv to the pad for thread safety.
2092 * Despite being a "constant", the SV is written to,
2093 * for reference counts, sv_upgrade() etc. */
2094 PERL_STATIC_INLINE void
2095 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2098 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2100 ix = pad_alloc(OP_CONST, SVf_READONLY);
2101 SvREFCNT_dec(PAD_SVl(ix));
2102 PAD_SETSV(ix, *svp);
2103 /* XXX I don't know how this isn't readonly already. */
2104 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2112 S_finalize_op(pTHX_ OP* o)
2114 PERL_ARGS_ASSERT_FINALIZE_OP;
2117 switch (o->op_type) {
2120 PL_curcop = ((COP*)o); /* for warnings */
2123 if (OP_HAS_SIBLING(o)) {
2124 OP *sib = OP_SIBLING(o);
2125 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2126 && ckWARN(WARN_EXEC)
2127 && OP_HAS_SIBLING(sib))
2129 const OPCODE type = OP_SIBLING(sib)->op_type;
2130 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2131 const line_t oldline = CopLINE(PL_curcop);
2132 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2133 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2134 "Statement unlikely to be reached");
2135 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2136 "\t(Maybe you meant system() when you said exec()?)\n");
2137 CopLINE_set(PL_curcop, oldline);
2144 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2145 GV * const gv = cGVOPo_gv;
2146 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2147 /* XXX could check prototype here instead of just carping */
2148 SV * const sv = sv_newmortal();
2149 gv_efullname3(sv, gv, NULL);
2150 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2151 "%"SVf"() called too early to check prototype",
2158 if (cSVOPo->op_private & OPpCONST_STRICT)
2159 no_bareword_allowed(o);
2163 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2168 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2169 case OP_METHOD_NAMED:
2170 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2182 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2185 rop = (UNOP*)((BINOP*)o)->op_first;
2190 S_scalar_slice_warning(aTHX_ o);
2194 kid = OP_SIBLING(cLISTOPo->op_first);
2195 if (/* I bet there's always a pushmark... */
2196 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2197 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2202 key_op = (SVOP*)(kid->op_type == OP_CONST
2204 : OP_SIBLING(kLISTOP->op_first));
2206 rop = (UNOP*)((LISTOP*)o)->op_last;
2209 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2211 else if (rop->op_first->op_type == OP_PADSV)
2212 /* @$hash{qw(keys here)} */
2213 rop = (UNOP*)rop->op_first;
2215 /* @{$hash}{qw(keys here)} */
2216 if (rop->op_first->op_type == OP_SCOPE
2217 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2219 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2225 lexname = NULL; /* just to silence compiler warnings */
2226 fields = NULL; /* just to silence compiler warnings */
2230 && (lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE),
2231 SvPAD_TYPED(lexname))
2232 && (fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE))
2233 && isGV(*fields) && GvHV(*fields);
2235 key_op = (SVOP*)OP_SIBLING(key_op)) {
2237 if (key_op->op_type != OP_CONST)
2239 svp = cSVOPx_svp(key_op);
2241 /* Make the CONST have a shared SV */
2242 if ((!SvIsCOW_shared_hash(sv = *svp))
2243 && SvTYPE(sv) < SVt_PVMG && SvOK(sv) && !SvROK(sv)) {
2245 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2246 SV *nsv = newSVpvn_share(key,
2247 SvUTF8(sv) ? -keylen : keylen, 0);
2248 SvREFCNT_dec_NN(sv);
2253 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) {
2254 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2255 "in variable %"SVf" of type %"HEKf,
2256 SVfARG(*svp), SVfARG(lexname),
2257 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
2263 S_scalar_slice_warning(aTHX_ o);
2267 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2268 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2275 if (o->op_flags & OPf_KIDS) {
2279 /* check that op_last points to the last sibling, and that
2280 * the last op_sibling field points back to the parent, and
2281 * that the only ops with KIDS are those which are entitled to
2283 U32 type = o->op_type;
2287 if (type == OP_NULL) {
2289 /* ck_glob creates a null UNOP with ex-type GLOB
2290 * (which is a list op. So pretend it wasn't a listop */
2291 if (type == OP_GLOB)
2294 family = PL_opargs[type] & OA_CLASS_MASK;
2296 has_last = ( family == OA_BINOP
2297 || family == OA_LISTOP
2298 || family == OA_PMOP
2299 || family == OA_LOOP
2301 assert( has_last /* has op_first and op_last, or ...
2302 ... has (or may have) op_first: */
2303 || family == OA_UNOP
2304 || family == OA_LOGOP
2305 || family == OA_BASEOP_OR_UNOP
2306 || family == OA_FILESTATOP
2307 || family == OA_LOOPEXOP
2308 || family == OA_METHOP
2309 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2310 || type == OP_SASSIGN
2311 || type == OP_CUSTOM
2312 || type == OP_NULL /* new_logop does this */
2314 /* XXX list form of 'x' is has a null op_last. This is wrong,
2315 * but requires too much hacking (e.g. in Deparse) to fix for
2317 if (type == OP_REPEAT && (o->op_private & OPpREPEAT_DOLIST)) {
2322 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2323 # ifdef PERL_OP_PARENT
2324 if (!OP_HAS_SIBLING(kid)) {
2326 assert(kid == cLISTOPo->op_last);
2327 assert(kid->op_sibling == o);
2330 if (OP_HAS_SIBLING(kid)) {
2331 assert(!kid->op_lastsib);
2334 assert(kid->op_lastsib);
2336 assert(kid == cLISTOPo->op_last);
2342 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid))
2348 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2350 Propagate lvalue ("modifiable") context to an op and its children.
2351 I<type> represents the context type, roughly based on the type of op that
2352 would do the modifying, although C<local()> is represented by OP_NULL,
2353 because it has no op type of its own (it is signalled by a flag on
2356 This function detects things that can't be modified, such as C<$x+1>, and
2357 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2358 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2360 It also flags things that need to behave specially in an lvalue context,
2361 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2367 S_vivifies(const OPCODE type)
2370 case OP_RV2AV: case OP_ASLICE:
2371 case OP_RV2HV: case OP_KVASLICE:
2372 case OP_RV2SV: case OP_HSLICE:
2373 case OP_AELEMFAST: case OP_KVHSLICE:
2382 S_lvref(pTHX_ OP *o, I32 type)
2386 switch (o->op_type) {
2388 for (kid = OP_SIBLING(cUNOPo->op_first); kid;
2389 kid = OP_SIBLING(kid))
2390 S_lvref(aTHX_ kid, type);
2395 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2396 o->op_flags |= OPf_STACKED;
2397 if (o->op_flags & OPf_PARENS) {
2398 if (o->op_private & OPpLVAL_INTRO) {
2399 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2400 "localized parenthesized array in list assignment"));
2404 o->op_type = OP_LVAVREF;
2405 o->op_ppaddr = PL_ppaddr[OP_LVAVREF];
2406 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2407 o->op_flags |= OPf_MOD|OPf_REF;
2410 o->op_private |= OPpLVREF_AV;
2413 kid = cUNOPo->op_first;
2414 if (kid->op_type == OP_NULL)
2415 kid = cUNOPx(kUNOP->op_first->op_sibling)
2417 o->op_private = OPpLVREF_CV;
2418 if (kid->op_type == OP_GV)
2419 o->op_flags |= OPf_STACKED;
2420 else if (kid->op_type == OP_PADCV) {
2421 o->op_targ = kid->op_targ;
2423 op_free(cUNOPo->op_first);
2424 cUNOPo->op_first = NULL;
2425 o->op_flags &=~ OPf_KIDS;
2430 if (o->op_flags & OPf_PARENS) {
2432 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2433 "parenthesized hash in list assignment"));
2436 o->op_private |= OPpLVREF_HV;
2440 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2441 o->op_flags |= OPf_STACKED;
2444 if (o->op_flags & OPf_PARENS) goto parenhash;
2445 o->op_private |= OPpLVREF_HV;
2448 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2451 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2452 if (o->op_flags & OPf_PARENS) goto slurpy;
2453 o->op_private |= OPpLVREF_AV;
2457 o->op_private |= OPpLVREF_ELEM;
2458 o->op_flags |= OPf_STACKED;
2462 o->op_type = OP_LVREFSLICE;
2463 o->op_ppaddr = PL_ppaddr[OP_LVREFSLICE];
2464 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM;
2467 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2469 else if (!(o->op_flags & OPf_KIDS))
2471 if (o->op_targ != OP_LIST) {
2472 S_lvref(aTHX_ cBINOPo->op_first, type);
2477 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2478 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2479 S_lvref(aTHX_ kid, type);
2483 if (o->op_flags & OPf_PARENS)
2488 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2489 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2490 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2496 o->op_type = OP_LVREF;
2497 o->op_ppaddr = PL_ppaddr[OP_LVREF];
2499 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2500 if (type == OP_ENTERLOOP)
2501 o->op_private |= OPpLVREF_ITER;
2505 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2509 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2512 if (!o || (PL_parser && PL_parser->error_count))
2515 if ((o->op_private & OPpTARGET_MY)
2516 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2521 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2523 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2525 switch (o->op_type) {
2530 if ((o->op_flags & OPf_PARENS))
2534 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2535 !(o->op_flags & OPf_STACKED)) {
2536 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2537 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2538 assert(cUNOPo->op_first->op_type == OP_NULL);
2539 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2542 else { /* lvalue subroutine call */
2543 o->op_private |= OPpLVAL_INTRO;
2544 PL_modcount = RETURN_UNLIMITED_NUMBER;
2545 if (type == OP_GREPSTART || type == OP_ENTERSUB
2546 || type == OP_REFGEN || type == OP_LEAVESUBLV) {
2547 /* Potential lvalue context: */
2548 o->op_private |= OPpENTERSUB_INARGS;
2551 else { /* Compile-time error message: */
2552 OP *kid = cUNOPo->op_first;
2556 if (kid->op_type != OP_PUSHMARK) {
2557 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2559 "panic: unexpected lvalue entersub "
2560 "args: type/targ %ld:%"UVuf,
2561 (long)kid->op_type, (UV)kid->op_targ);
2562 kid = kLISTOP->op_first;
2564 while (OP_HAS_SIBLING(kid))
2565 kid = OP_SIBLING(kid);
2566 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2567 break; /* Postpone until runtime */
2570 kid = kUNOP->op_first;
2571 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2572 kid = kUNOP->op_first;
2573 if (kid->op_type == OP_NULL)
2575 "Unexpected constant lvalue entersub "
2576 "entry via type/targ %ld:%"UVuf,
2577 (long)kid->op_type, (UV)kid->op_targ);
2578 if (kid->op_type != OP_GV) {
2585 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2586 ? MUTABLE_CV(SvRV(gv))
2597 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2598 /* grep, foreach, subcalls, refgen */
2599 if (type == OP_GREPSTART || type == OP_ENTERSUB
2600 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2602 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2603 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2605 : (o->op_type == OP_ENTERSUB
2606 ? "non-lvalue subroutine call"
2608 type ? PL_op_desc[type] : "local"));
2621 case OP_RIGHT_SHIFT:
2630 if (!(o->op_flags & OPf_STACKED))
2636 if (o->op_flags & OPf_STACKED) {
2640 if (type != OP_AASSIGN || !(o->op_private & OPpREPEAT_DOLIST))
2643 const I32 mods = PL_modcount;
2644 modkids(cBINOPo->op_first, OP_AASSIGN);
2645 kid = cBINOPo->op_last;
2646 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
2647 const IV iv = SvIV(kSVOP_sv);
2648 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
2650 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
2653 PL_modcount = RETURN_UNLIMITED_NUMBER;
2659 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
2660 op_lvalue(kid, type);
2665 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2666 PL_modcount = RETURN_UNLIMITED_NUMBER;
2667 return o; /* Treat \(@foo) like ordinary list. */
2671 if (scalar_mod_type(o, type))
2673 ref(cUNOPo->op_first, o->op_type);
2680 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2681 if (type == OP_LEAVESUBLV && (
2682 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2683 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2685 o->op_private |= OPpMAYBE_LVSUB;
2689 PL_modcount = RETURN_UNLIMITED_NUMBER;
2693 if (type == OP_LEAVESUBLV)
2694 o->op_private |= OPpMAYBE_LVSUB;
2697 PL_hints |= HINT_BLOCK_SCOPE;
2698 if (type == OP_LEAVESUBLV)
2699 o->op_private |= OPpMAYBE_LVSUB;
2703 ref(cUNOPo->op_first, o->op_type);
2707 PL_hints |= HINT_BLOCK_SCOPE;
2717 case OP_AELEMFAST_LEX:
2724 PL_modcount = RETURN_UNLIMITED_NUMBER;
2725 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2726 return o; /* Treat \(@foo) like ordinary list. */
2727 if (scalar_mod_type(o, type))
2729 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2730 && type == OP_LEAVESUBLV)
2731 o->op_private |= OPpMAYBE_LVSUB;
2735 if (!type) /* local() */
2736 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2737 PAD_COMPNAME_SV(o->op_targ));
2746 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2750 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2756 if (type == OP_LEAVESUBLV)
2757 o->op_private |= OPpMAYBE_LVSUB;
2758 if (o->op_flags & OPf_KIDS)
2759 op_lvalue(OP_SIBLING(cBINOPo->op_first), type);
2764 ref(cBINOPo->op_first, o->op_type);
2765 if (type == OP_ENTERSUB &&
2766 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2767 o->op_private |= OPpLVAL_DEFER;
2768 if (type == OP_LEAVESUBLV)
2769 o->op_private |= OPpMAYBE_LVSUB;
2776 o->op_private |= OPpLVALUE;
2782 if (o->op_flags & OPf_KIDS)
2783 op_lvalue(cLISTOPo->op_last, type);
2788 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2790 else if (!(o->op_flags & OPf_KIDS))
2792 if (o->op_targ != OP_LIST) {
2793 op_lvalue(cBINOPo->op_first, type);
2799 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2800 /* elements might be in void context because the list is
2801 in scalar context or because they are attribute sub calls */
2802 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2803 op_lvalue(kid, type);
2811 if (type == OP_LEAVESUBLV
2812 || !S_vivifies(cLOGOPo->op_first->op_type))
2813 op_lvalue(cLOGOPo->op_first, type);
2814 if (type == OP_LEAVESUBLV
2815 || !S_vivifies(OP_SIBLING(cLOGOPo->op_first)->op_type))
2816 op_lvalue(OP_SIBLING(cLOGOPo->op_first), type);
2820 if (type != OP_AASSIGN && type != OP_SASSIGN
2821 && type != OP_ENTERLOOP)
2823 /* Don’t bother applying lvalue context to the ex-list. */
2824 kid = cUNOPx(cUNOPo->op_first)->op_first;
2825 assert (!OP_HAS_SIBLING(kid));
2828 if (type != OP_AASSIGN) goto nomod;
2829 kid = cUNOPo->op_first;
2832 const U8 ec = PL_parser ? PL_parser->error_count : 0;
2833 S_lvref(aTHX_ kid, type);
2834 if (!PL_parser || PL_parser->error_count == ec) {
2835 if (!FEATURE_REFALIASING_IS_ENABLED)
2837 "Experimental aliasing via reference not enabled");
2838 Perl_ck_warner_d(aTHX_
2839 packWARN(WARN_EXPERIMENTAL__REFALIASING),
2840 "Aliasing via reference is experimental");
2843 if (o->op_type == OP_REFGEN)
2844 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
2849 /* [20011101.069] File test operators interpret OPf_REF to mean that
2850 their argument is a filehandle; thus \stat(".") should not set
2852 if (type == OP_REFGEN &&
2853 PL_check[o->op_type] == Perl_ck_ftst)
2856 if (type != OP_LEAVESUBLV)
2857 o->op_flags |= OPf_MOD;
2859 if (type == OP_AASSIGN || type == OP_SASSIGN)
2860 o->op_flags |= OPf_SPECIAL|OPf_REF;
2861 else if (!type) { /* local() */
2864 o->op_private |= OPpLVAL_INTRO;
2865 o->op_flags &= ~OPf_SPECIAL;
2866 PL_hints |= HINT_BLOCK_SCOPE;
2871 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2872 "Useless localization of %s", OP_DESC(o));
2875 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2876 && type != OP_LEAVESUBLV)
2877 o->op_flags |= OPf_REF;
2882 S_scalar_mod_type(const OP *o, I32 type)
2887 if (o && o->op_type == OP_RV2GV)
2911 case OP_RIGHT_SHIFT:
2932 S_is_handle_constructor(const OP *o, I32 numargs)
2934 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2936 switch (o->op_type) {
2944 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2957 S_refkids(pTHX_ OP *o, I32 type)
2959 if (o && o->op_flags & OPf_KIDS) {
2961 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2968 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2973 PERL_ARGS_ASSERT_DOREF;
2975 if (!o || (PL_parser && PL_parser->error_count))
2978 switch (o->op_type) {
2980 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2981 !(o->op_flags & OPf_STACKED)) {
2982 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2983 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2984 assert(cUNOPo->op_first->op_type == OP_NULL);
2985 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2986 o->op_flags |= OPf_SPECIAL;
2988 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2989 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2990 : type == OP_RV2HV ? OPpDEREF_HV
2992 o->op_flags |= OPf_MOD;
2998 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
2999 doref(kid, type, set_op_ref);
3002 if (type == OP_DEFINED)
3003 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3004 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3007 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3008 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3009 : type == OP_RV2HV ? OPpDEREF_HV
3011 o->op_flags |= OPf_MOD;
3018 o->op_flags |= OPf_REF;
3021 if (type == OP_DEFINED)
3022 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3023 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3029 o->op_flags |= OPf_REF;
3034 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3036 doref(cBINOPo->op_first, type, set_op_ref);
3040 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3041 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3042 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3043 : type == OP_RV2HV ? OPpDEREF_HV
3045 o->op_flags |= OPf_MOD;
3055 if (!(o->op_flags & OPf_KIDS))
3057 doref(cLISTOPo->op_last, type, set_op_ref);
3067 S_dup_attrlist(pTHX_ OP *o)
3071 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3073 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3074 * where the first kid is OP_PUSHMARK and the remaining ones
3075 * are OP_CONST. We need to push the OP_CONST values.
3077 if (o->op_type == OP_CONST)
3078 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3080 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3082 for (o = cLISTOPo->op_first; o; o = OP_SIBLING(o)) {
3083 if (o->op_type == OP_CONST)
3084 rop = op_append_elem(OP_LIST, rop,
3085 newSVOP(OP_CONST, o->op_flags,
3086 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3093 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3095 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
3097 PERL_ARGS_ASSERT_APPLY_ATTRS;
3099 /* fake up C<use attributes $pkg,$rv,@attrs> */
3101 #define ATTRSMODULE "attributes"
3102 #define ATTRSMODULE_PM "attributes.pm"
3104 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3105 newSVpvs(ATTRSMODULE),
3107 op_prepend_elem(OP_LIST,
3108 newSVOP(OP_CONST, 0, stashsv),
3109 op_prepend_elem(OP_LIST,
3110 newSVOP(OP_CONST, 0,
3112 dup_attrlist(attrs))));
3116 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3118 OP *pack, *imop, *arg;
3119 SV *meth, *stashsv, **svp;
3121 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3126 assert(target->op_type == OP_PADSV ||
3127 target->op_type == OP_PADHV ||
3128 target->op_type == OP_PADAV);
3130 /* Ensure that attributes.pm is loaded. */
3131 /* Don't force the C<use> if we don't need it. */
3132 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3133 if (svp && *svp != &PL_sv_undef)
3134 NOOP; /* already in %INC */
3136 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3137 newSVpvs(ATTRSMODULE), NULL);
3139 /* Need package name for method call. */
3140 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3142 /* Build up the real arg-list. */
3143 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
3145 arg = newOP(OP_PADSV, 0);
3146 arg->op_targ = target->op_targ;
3147 arg = op_prepend_elem(OP_LIST,
3148 newSVOP(OP_CONST, 0, stashsv),
3149 op_prepend_elem(OP_LIST,
3150 newUNOP(OP_REFGEN, 0,
3151 op_lvalue(arg, OP_REFGEN)),
3152 dup_attrlist(attrs)));
3154 /* Fake up a method call to import */
3155 meth = newSVpvs_share("import");
3156 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3157 op_append_elem(OP_LIST,
3158 op_prepend_elem(OP_LIST, pack, arg),
3159 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3161 /* Combine the ops. */
3162 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3166 =notfor apidoc apply_attrs_string
3168 Attempts to apply a list of attributes specified by the C<attrstr> and
3169 C<len> arguments to the subroutine identified by the C<cv> argument which
3170 is expected to be associated with the package identified by the C<stashpv>
3171 argument (see L<attributes>). It gets this wrong, though, in that it
3172 does not correctly identify the boundaries of the individual attribute
3173 specifications within C<attrstr>. This is not really intended for the
3174 public API, but has to be listed here for systems such as AIX which
3175 need an explicit export list for symbols. (It's called from XS code
3176 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3177 to respect attribute syntax properly would be welcome.
3183 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3184 const char *attrstr, STRLEN len)
3188 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3191 len = strlen(attrstr);
3195 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3197 const char * const sstr = attrstr;
3198 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3199 attrs = op_append_elem(OP_LIST, attrs,
3200 newSVOP(OP_CONST, 0,
3201 newSVpvn(sstr, attrstr-sstr)));
3205 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3206 newSVpvs(ATTRSMODULE),
3207 NULL, op_prepend_elem(OP_LIST,
3208 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3209 op_prepend_elem(OP_LIST,
3210 newSVOP(OP_CONST, 0,
3211 newRV(MUTABLE_SV(cv))),
3216 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3218 OP *new_proto = NULL;
3223 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3229 if (o->op_type == OP_CONST) {
3230 pv = SvPV(cSVOPo_sv, pvlen);
3231 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3232 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3233 SV ** const tmpo = cSVOPx_svp(o);
3234 SvREFCNT_dec(cSVOPo_sv);
3239 } else if (o->op_type == OP_LIST) {
3241 assert(o->op_flags & OPf_KIDS);
3242 lasto = cLISTOPo->op_first;
3243 assert(lasto->op_type == OP_PUSHMARK);
3244 for (o = OP_SIBLING(lasto); o; o = OP_SIBLING(o)) {
3245 if (o->op_type == OP_CONST) {
3246 pv = SvPV(cSVOPo_sv, pvlen);
3247 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3248 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3249 SV ** const tmpo = cSVOPx_svp(o);
3250 SvREFCNT_dec(cSVOPo_sv);
3252 if (new_proto && ckWARN(WARN_MISC)) {
3254 const char * newp = SvPV(cSVOPo_sv, new_len);
3255 Perl_warner(aTHX_ packWARN(WARN_MISC),
3256 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3257 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3263 /* excise new_proto from the list */
3264 op_sibling_splice(*attrs, lasto, 1, NULL);
3271 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3272 would get pulled in with no real need */
3273 if (!OP_HAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3282 svname = sv_newmortal();
3283 gv_efullname3(svname, name, NULL);
3285 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3286 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3288 svname = (SV *)name;
3289 if (ckWARN(WARN_ILLEGALPROTO))
3290 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3291 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3292 STRLEN old_len, new_len;
3293 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3294 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3296 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3297 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3299 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3300 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3310 S_cant_declare(pTHX_ OP *o)
3312 if (o->op_type == OP_NULL
3313 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3314 o = cUNOPo->op_first;
3315 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3316 o->op_type == OP_NULL
3317 && o->op_flags & OPf_SPECIAL
3320 PL_parser->in_my == KEY_our ? "our" :
3321 PL_parser->in_my == KEY_state ? "state" :
3326 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3329 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3331 PERL_ARGS_ASSERT_MY_KID;
3333 if (!o || (PL_parser && PL_parser->error_count))
3338 if (type == OP_LIST) {
3340 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
3341 my_kid(kid, attrs, imopsp);
3343 } else if (type == OP_UNDEF || type == OP_STUB) {
3345 } else if (type == OP_RV2SV || /* "our" declaration */
3347 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
3348 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3349 S_cant_declare(aTHX_ o);
3351 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3353 PL_parser->in_my = FALSE;
3354 PL_parser->in_my_stash = NULL;
3355 apply_attrs(GvSTASH(gv),
3356 (type == OP_RV2SV ? GvSV(gv) :
3357 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3358 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3361 o->op_private |= OPpOUR_INTRO;
3364 else if (type != OP_PADSV &&
3367 type != OP_PUSHMARK)
3369 S_cant_declare(aTHX_ o);
3372 else if (attrs && type != OP_PUSHMARK) {
3376 PL_parser->in_my = FALSE;
3377 PL_parser->in_my_stash = NULL;
3379 /* check for C<my Dog $spot> when deciding package */
3380 stash = PAD_COMPNAME_TYPE(o->op_targ);
3382 stash = PL_curstash;
3383 apply_attrs_my(stash, o, attrs, imopsp);
3385 o->op_flags |= OPf_MOD;
3386 o->op_private |= OPpLVAL_INTRO;
3388 o->op_private |= OPpPAD_STATE;
3393 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3396 int maybe_scalar = 0;
3398 PERL_ARGS_ASSERT_MY_ATTRS;
3400 /* [perl #17376]: this appears to be premature, and results in code such as
3401 C< our(%x); > executing in list mode rather than void mode */
3403 if (o->op_flags & OPf_PARENS)
3413 o = my_kid(o, attrs, &rops);
3415 if (maybe_scalar && o->op_type == OP_PADSV) {
3416 o = scalar(op_append_list(OP_LIST, rops, o));
3417 o->op_private |= OPpLVAL_INTRO;
3420 /* The listop in rops might have a pushmark at the beginning,
3421 which will mess up list assignment. */
3422 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3423 if (rops->op_type == OP_LIST &&
3424 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3426 OP * const pushmark = lrops->op_first;
3427 /* excise pushmark */
3428 op_sibling_splice(rops, NULL, 1, NULL);
3431 o = op_append_list(OP_LIST, o, rops);
3434 PL_parser->in_my = FALSE;
3435 PL_parser->in_my_stash = NULL;
3440 Perl_sawparens(pTHX_ OP *o)
3442 PERL_UNUSED_CONTEXT;
3444 o->op_flags |= OPf_PARENS;
3449 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3453 const OPCODE ltype = left->op_type;
3454 const OPCODE rtype = right->op_type;
3456 PERL_ARGS_ASSERT_BIND_MATCH;
3458 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3459 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3461 const char * const desc
3463 rtype == OP_SUBST || rtype == OP_TRANS
3464 || rtype == OP_TRANSR
3466 ? (int)rtype : OP_MATCH];
3467 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3469 S_op_varname(aTHX_ left);
3471 Perl_warner(aTHX_ packWARN(WARN_MISC),
3472 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3473 desc, SVfARG(name), SVfARG(name));
3475 const char * const sample = (isary
3476 ? "@array" : "%hash");
3477 Perl_warner(aTHX_ packWARN(WARN_MISC),
3478 "Applying %s to %s will act on scalar(%s)",
3479 desc, sample, sample);
3483 if (rtype == OP_CONST &&
3484 cSVOPx(right)->op_private & OPpCONST_BARE &&
3485 cSVOPx(right)->op_private & OPpCONST_STRICT)
3487 no_bareword_allowed(right);
3490 /* !~ doesn't make sense with /r, so error on it for now */
3491 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3493 /* diag_listed_as: Using !~ with %s doesn't make sense */
3494 yyerror("Using !~ with s///r doesn't make sense");
3495 if (rtype == OP_TRANSR && type == OP_NOT)
3496 /* diag_listed_as: Using !~ with %s doesn't make sense */
3497 yyerror("Using !~ with tr///r doesn't make sense");
3499 ismatchop = (rtype == OP_MATCH ||
3500 rtype == OP_SUBST ||
3501 rtype == OP_TRANS || rtype == OP_TRANSR)
3502 && !(right->op_flags & OPf_SPECIAL);
3503 if (ismatchop && right->op_private & OPpTARGET_MY) {
3505 right->op_private &= ~OPpTARGET_MY;
3507 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
3510 right->op_flags |= OPf_STACKED;
3511 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3512 ! (rtype == OP_TRANS &&
3513 right->op_private & OPpTRANS_IDENTICAL) &&
3514 ! (rtype == OP_SUBST &&
3515 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3516 newleft = op_lvalue(left, rtype);
3519 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3520 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
3522 o = op_prepend_elem(rtype, scalar(newleft), right);
3524 return newUNOP(OP_NOT, 0, scalar(o));
3528 return bind_match(type, left,
3529 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
3533 Perl_invert(pTHX_ OP *o)
3537 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3541 =for apidoc Amx|OP *|op_scope|OP *o
3543 Wraps up an op tree with some additional ops so that at runtime a dynamic
3544 scope will be created. The original ops run in the new dynamic scope,
3545 and then, provided that they exit normally, the scope will be unwound.
3546 The additional ops used to create and unwind the dynamic scope will
3547 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3548 instead if the ops are simple enough to not need the full dynamic scope
3555 Perl_op_scope(pTHX_ OP *o)
3559 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3560 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3561 o->op_type = OP_LEAVE;
3562 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
3564 else if (o->op_type == OP_LINESEQ) {
3566 o->op_type = OP_SCOPE;
3567 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
3568 kid = ((LISTOP*)o)->op_first;
3569 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3572 /* The following deals with things like 'do {1 for 1}' */
3573 kid = OP_SIBLING(kid);
3575 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3580 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3586 Perl_op_unscope(pTHX_ OP *o)
3588 if (o && o->op_type == OP_LINESEQ) {
3589 OP *kid = cLISTOPo->op_first;
3590 for(; kid; kid = OP_SIBLING(kid))
3591 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3598 =for apidoc Am|int|block_start|int full
3600 Handles compile-time scope entry. Arranges for hints to be restored on block
3601 exit and also handles pad sequence numbers to make lexical variables scope
3602 right. Returns a savestack index for use with C<block_end>.
3608 Perl_block_start(pTHX_ int full)
3610 const int retval = PL_savestack_ix;
3612 pad_block_start(full);
3614 PL_hints &= ~HINT_BLOCK_SCOPE;
3615 SAVECOMPILEWARNINGS();
3616 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3618 CALL_BLOCK_HOOKS(bhk_start, full);
3624 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
3626 Handles compile-time scope exit. I<floor> is the savestack index returned by
3627 C<block_start>, and I<seq> is the body of the block. Returns the block,
3634 Perl_block_end(pTHX_ I32 floor, OP *seq)
3636 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3637 OP* retval = scalarseq(seq);
3640 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3644 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3648 /* pad_leavemy has created a sequence of introcv ops for all my
3649 subs declared in the block. We have to replicate that list with
3650 clonecv ops, to deal with this situation:
3655 sub s1 { state sub foo { \&s2 } }
3658 Originally, I was going to have introcv clone the CV and turn
3659 off the stale flag. Since &s1 is declared before &s2, the
3660 introcv op for &s1 is executed (on sub entry) before the one for
3661 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3662 cloned, since it is a state sub) closes over &s2 and expects
3663 to see it in its outer CV’s pad. If the introcv op clones &s1,
3664 then &s2 is still marked stale. Since &s1 is not active, and
3665 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3666 ble will not stay shared’ warning. Because it is the same stub
3667 that will be used when the introcv op for &s2 is executed, clos-
3668 ing over it is safe. Hence, we have to turn off the stale flag
3669 on all lexical subs in the block before we clone any of them.
3670 Hence, having introcv clone the sub cannot work. So we create a
3671 list of ops like this:
3695 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3696 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3697 for (;; kid = OP_SIBLING(kid)) {
3698 OP *newkid = newOP(OP_CLONECV, 0);
3699 newkid->op_targ = kid->op_targ;
3700 o = op_append_elem(OP_LINESEQ, o, newkid);
3701 if (kid == last) break;
3703 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3706 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3712 =head1 Compile-time scope hooks
3714 =for apidoc Aox||blockhook_register
3716 Register a set of hooks to be called when the Perl lexical scope changes
3717 at compile time. See L<perlguts/"Compile-time scope hooks">.
3723 Perl_blockhook_register(pTHX_ BHK *hk)
3725 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3727 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3731 Perl_newPROG(pTHX_ OP *o)
3733 PERL_ARGS_ASSERT_NEWPROG;
3740 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3741 ((PL_in_eval & EVAL_KEEPERR)
3742 ? OPf_SPECIAL : 0), o);
3744 cx = &cxstack[cxstack_ix];
3745 assert(CxTYPE(cx) == CXt_EVAL);
3747 if ((cx->blk_gimme & G_WANT) == G_VOID)
3748 scalarvoid(PL_eval_root);
3749 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3752 scalar(PL_eval_root);
3754 PL_eval_start = op_linklist(PL_eval_root);
3755 PL_eval_root->op_private |= OPpREFCOUNTED;
3756 OpREFCNT_set(PL_eval_root, 1);
3757 PL_eval_root->op_next = 0;
3758 i = PL_savestack_ix;
3761 CALL_PEEP(PL_eval_start);
3762 finalize_optree(PL_eval_root);
3763 S_prune_chain_head(&PL_eval_start);
3765 PL_savestack_ix = i;
3768 if (o->op_type == OP_STUB) {
3769 /* This block is entered if nothing is compiled for the main
3770 program. This will be the case for an genuinely empty main
3771 program, or one which only has BEGIN blocks etc, so already
3774 Historically (5.000) the guard above was !o. However, commit
3775 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3776 c71fccf11fde0068, changed perly.y so that newPROG() is now
3777 called with the output of block_end(), which returns a new
3778 OP_STUB for the case of an empty optree. ByteLoader (and
3779 maybe other things) also take this path, because they set up
3780 PL_main_start and PL_main_root directly, without generating an
3783 If the parsing the main program aborts (due to parse errors,
3784 or due to BEGIN or similar calling exit), then newPROG()
3785 isn't even called, and hence this code path and its cleanups
3786 are skipped. This shouldn't make a make a difference:
3787 * a non-zero return from perl_parse is a failure, and
3788 perl_destruct() should be called immediately.
3789 * however, if exit(0) is called during the parse, then
3790 perl_parse() returns 0, and perl_run() is called. As
3791 PL_main_start will be NULL, perl_run() will return
3792 promptly, and the exit code will remain 0.
3795 PL_comppad_name = 0;
3797 S_op_destroy(aTHX_ o);
3800 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3801 PL_curcop = &PL_compiling;
3802 PL_main_start = LINKLIST(PL_main_root);
3803 PL_main_root->op_private |= OPpREFCOUNTED;
3804 OpREFCNT_set(PL_main_root, 1);
3805 PL_main_root->op_next = 0;
3806 CALL_PEEP(PL_main_start);
3807 finalize_optree(PL_main_root);
3808 S_prune_chain_head(&PL_main_start);
3809 cv_forget_slab(PL_compcv);
3812 /* Register with debugger */
3814 CV * const cv = get_cvs("DB::postponed", 0);
3818 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3820 call_sv(MUTABLE_SV(cv), G_DISCARD);
3827 Perl_localize(pTHX_ OP *o, I32 lex)
3829 PERL_ARGS_ASSERT_LOCALIZE;
3831 if (o->op_flags & OPf_PARENS)
3832 /* [perl #17376]: this appears to be premature, and results in code such as
3833 C< our(%x); > executing in list mode rather than void mode */
3840 if ( PL_parser->bufptr > PL_parser->oldbufptr
3841 && PL_parser->bufptr[-1] == ','
3842 && ckWARN(WARN_PARENTHESIS))
3844 char *s = PL_parser->bufptr;
3847 /* some heuristics to detect a potential error */
3848 while (*s && (strchr(", \t\n", *s)))
3852 if (*s && strchr("@$%*", *s) && *++s
3853 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
3856 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
3858 while (*s && (strchr(", \t\n", *s)))
3864 if (sigil && (*s == ';' || *s == '=')) {
3865 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3866 "Parentheses missing around \"%s\" list",
3868 ? (PL_parser->in_my == KEY_our
3870 : PL_parser->in_my == KEY_state
3880 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3881 PL_parser->in_my = FALSE;
3882 PL_parser->in_my_stash = NULL;
3887 Perl_jmaybe(pTHX_ OP *o)
3889 PERL_ARGS_ASSERT_JMAYBE;
3891 if (o->op_type == OP_LIST) {
3893 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3894 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3899 PERL_STATIC_INLINE OP *
3900 S_op_std_init(pTHX_ OP *o)
3902 I32 type = o->op_type;
3904 PERL_ARGS_ASSERT_OP_STD_INIT;
3906 if (PL_opargs[type] & OA_RETSCALAR)
3908 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3909 o->op_targ = pad_alloc(type, SVs_PADTMP);
3914 PERL_STATIC_INLINE OP *
3915 S_op_integerize(pTHX_ OP *o)
3917 I32 type = o->op_type;
3919 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3921 /* integerize op. */
3922 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3925 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
3928 if (type == OP_NEGATE)
3929 /* XXX might want a ck_negate() for this */
3930 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3936 S_fold_constants(pTHX_ OP *o)
3941 VOL I32 type = o->op_type;
3947 SV * const oldwarnhook = PL_warnhook;
3948 SV * const olddiehook = PL_diehook;
3950 U8 oldwarn = PL_dowarn;
3953 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3955 if (!(PL_opargs[type] & OA_FOLDCONST))
3964 #ifdef USE_LOCALE_CTYPE
3965 if (IN_LC_COMPILETIME(LC_CTYPE))
3974 #ifdef USE_LOCALE_COLLATE
3975 if (IN_LC_COMPILETIME(LC_COLLATE))
3980 /* XXX what about the numeric ops? */
3981 #ifdef USE_LOCALE_NUMERIC
3982 if (IN_LC_COMPILETIME(LC_NUMERIC))
3987 if (!OP_HAS_SIBLING(cLISTOPo->op_first)
3988 || OP_SIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
3991 SV * const sv = cSVOPx_sv(OP_SIBLING(cLISTOPo->op_first));
3992 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3994 const char *s = SvPVX_const(sv);
3995 while (s < SvEND(sv)) {
3996 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4003 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4006 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4007 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4011 if (PL_parser && PL_parser->error_count)
4012 goto nope; /* Don't try to run w/ errors */
4014 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4015 const OPCODE type = curop->op_type;
4016 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
4018 type != OP_SCALAR &&
4020 type != OP_PUSHMARK)
4026 curop = LINKLIST(o);
4027 old_next = o->op_next;
4031 oldscope = PL_scopestack_ix;
4032 create_eval_scope(G_FAKINGEVAL);
4034 /* Verify that we don't need to save it: */
4035 assert(PL_curcop == &PL_compiling);
4036 StructCopy(&PL_compiling, ¬_compiling, COP);
4037 PL_curcop = ¬_compiling;
4038 /* The above ensures that we run with all the correct hints of the
4039 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
4040 assert(IN_PERL_RUNTIME);
4041 PL_warnhook = PERL_WARNHOOK_FATAL;
4045 /* Effective $^W=1. */
4046 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4047 PL_dowarn |= G_WARN_ON;
4052 sv = *(PL_stack_sp--);
4053 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4054 pad_swipe(o->op_targ, FALSE);
4056 else if (SvTEMP(sv)) { /* grab mortal temp? */
4057 SvREFCNT_inc_simple_void(sv);
4060 else { assert(SvIMMORTAL(sv)); }
4063 /* Something tried to die. Abandon constant folding. */
4064 /* Pretend the error never happened. */
4066 o->op_next = old_next;
4070 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4071 PL_warnhook = oldwarnhook;
4072 PL_diehook = olddiehook;
4073 /* XXX note that this croak may fail as we've already blown away
4074 * the stack - eg any nested evals */
4075 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4078 PL_dowarn = oldwarn;
4079 PL_warnhook = oldwarnhook;
4080 PL_diehook = olddiehook;
4081 PL_curcop = &PL_compiling;
4083 if (PL_scopestack_ix > oldscope)
4084 delete_eval_scope();
4089 folded = cBOOL(o->op_folded);
4092 if (type == OP_STRINGIFY) SvPADTMP_off(sv);
4093 else if (!SvIMMORTAL(sv)) {
4097 if (type == OP_RV2GV)
4098 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
4101 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4102 /* OP_STRINGIFY and constant folding are used to implement qq.
4103 Here the constant folding is an implementation detail that we
4104 want to hide. If the stringify op is itself already marked
4105 folded, however, then it is actually a folded join. */
4106 if (type != OP_STRINGIFY || folded) newop->op_folded = 1;
4115 S_gen_constant_list(pTHX_ OP *o)
4119 const SSize_t oldtmps_floor = PL_tmps_floor;
4124 if (PL_parser && PL_parser->error_count)
4125 return o; /* Don't attempt to run with errors */
4127 curop = LINKLIST(o);
4130 S_prune_chain_head(&curop);
4132 Perl_pp_pushmark(aTHX);
4135 assert (!(curop->op_flags & OPf_SPECIAL));
4136 assert(curop->op_type == OP_RANGE);
4137 Perl_pp_anonlist(aTHX);
4138 PL_tmps_floor = oldtmps_floor;
4140 o->op_type = OP_RV2AV;
4141 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4142 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4143 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4144 o->op_opt = 0; /* needs to be revisited in rpeep() */
4145 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4147 /* replace subtree with an OP_CONST */
4148 curop = ((UNOP*)o)->op_first;
4149 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4152 if (AvFILLp(av) != -1)
4153 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4156 SvREADONLY_on(*svp);
4163 =head1 Optree Manipulation Functions
4166 /* List constructors */
4169 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4171 Append an item to the list of ops contained directly within a list-type
4172 op, returning the lengthened list. I<first> is the list-type op,
4173 and I<last> is the op to append to the list. I<optype> specifies the
4174 intended opcode for the list. If I<first> is not already a list of the
4175 right type, it will be upgraded into one. If either I<first> or I<last>
4176 is null, the other is returned unchanged.
4182 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4190 if (first->op_type != (unsigned)type
4191 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4193 return newLISTOP(type, 0, first, last);
4196 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4197 first->op_flags |= OPf_KIDS;
4202 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4204 Concatenate the lists of ops contained directly within two list-type ops,
4205 returning the combined list. I<first> and I<last> are the list-type ops
4206 to concatenate. I<optype> specifies the intended opcode for the list.
4207 If either I<first> or I<last> is not already a list of the right type,
4208 it will be upgraded into one. If either I<first> or I<last> is null,
4209 the other is returned unchanged.
4215 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4223 if (first->op_type != (unsigned)type)
4224 return op_prepend_elem(type, first, last);
4226 if (last->op_type != (unsigned)type)
4227 return op_append_elem(type, first, last);
4229 ((LISTOP*)first)->op_last->op_lastsib = 0;
4230 OP_SIBLING_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4231 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4232 ((LISTOP*)first)->op_last->op_lastsib = 1;
4233 #ifdef PERL_OP_PARENT
4234 ((LISTOP*)first)->op_last->op_sibling = first;
4236 first->op_flags |= (last->op_flags & OPf_KIDS);
4239 S_op_destroy(aTHX_ last);
4245 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4247 Prepend an item to the list of ops contained directly within a list-type
4248 op, returning the lengthened list. I<first> is the op to prepend to the
4249 list, and I<last> is the list-type op. I<optype> specifies the intended
4250 opcode for the list. If I<last> is not already a list of the right type,
4251 it will be upgraded into one. If either I<first> or I<last> is null,
4252 the other is returned unchanged.
4258 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4266 if (last->op_type == (unsigned)type) {
4267 if (type == OP_LIST) { /* already a PUSHMARK there */
4268 /* insert 'first' after pushmark */
4269 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4270 if (!(first->op_flags & OPf_PARENS))
4271 last->op_flags &= ~OPf_PARENS;
4274 op_sibling_splice(last, NULL, 0, first);
4275 last->op_flags |= OPf_KIDS;
4279 return newLISTOP(type, 0, first, last);
4283 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4285 Converts I<o> into a list op if it is not one already, and then converts it
4286 into the specified I<type>, calling its check function, allocating a target if
4287 it needs one, and folding constants.
4289 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4290 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4291 C<op_convert> to make it the right type.
4297 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4300 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4301 if (!o || o->op_type != OP_LIST)
4302 o = force_list(o, 0);
4304 o->op_flags &= ~OPf_WANT;
4306 if (!(PL_opargs[type] & OA_MARK))
4307 op_null(cLISTOPo->op_first);
4309 OP * const kid2 = OP_SIBLING(cLISTOPo->op_first);
4310 if (kid2 && kid2->op_type == OP_COREARGS) {
4311 op_null(cLISTOPo->op_first);
4312 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4316 o->op_type = (OPCODE)type;
4317 o->op_ppaddr = PL_ppaddr[type];
4318 o->op_flags |= flags;
4320 o = CHECKOP(type, o);
4321 if (o->op_type != (unsigned)type)
4324 return fold_constants(op_integerize(op_std_init(o)));
4331 =head1 Optree construction
4333 =for apidoc Am|OP *|newNULLLIST
4335 Constructs, checks, and returns a new C<stub> op, which represents an
4336 empty list expression.
4342 Perl_newNULLLIST(pTHX)
4344 return newOP(OP_STUB, 0);
4347 /* promote o and any siblings to be a list if its not already; i.e.
4355 * pushmark - o - A - B
4357 * If nullit it true, the list op is nulled.
4361 S_force_list(pTHX_ OP *o, bool nullit)
4363 if (!o || o->op_type != OP_LIST) {
4366 /* manually detach any siblings then add them back later */
4367 rest = OP_SIBLING(o);
4368 OP_SIBLING_set(o, NULL);
4371 o = newLISTOP(OP_LIST, 0, o, NULL);
4373 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4381 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4383 Constructs, checks, and returns an op of any list type. I<type> is
4384 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4385 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
4386 supply up to two ops to be direct children of the list op; they are
4387 consumed by this function and become part of the constructed op tree.
4393 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4398 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
4400 NewOp(1101, listop, 1, LISTOP);
4402 listop->op_type = (OPCODE)type;
4403 listop->op_ppaddr = PL_ppaddr[type];
4406 listop->op_flags = (U8)flags;
4410 else if (!first && last)
4413 OP_SIBLING_set(first, last);
4414 listop->op_first = first;
4415 listop->op_last = last;
4416 if (type == OP_LIST) {
4417 OP* const pushop = newOP(OP_PUSHMARK, 0);
4418 pushop->op_lastsib = 0;
4419 OP_SIBLING_set(pushop, first);
4420 listop->op_first = pushop;
4421 listop->op_flags |= OPf_KIDS;
4423 listop->op_last = pushop;
4426 first->op_lastsib = 0;
4427 if (listop->op_last) {
4428 listop->op_last->op_lastsib = 1;
4429 #ifdef PERL_OP_PARENT
4430 listop->op_last->op_sibling = (OP*)listop;
4434 return CHECKOP(type, listop);
4438 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4440 Constructs, checks, and returns an op of any base type (any type that
4441 has no extra fields). I<type> is the opcode. I<flags> gives the
4442 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4449 Perl_newOP(pTHX_ I32 type, I32 flags)
4454 if (type == -OP_ENTEREVAL) {
4455 type = OP_ENTEREVAL;
4456 flags |= OPpEVAL_BYTES<<8;
4459 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4460 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4461 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4462 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4464 NewOp(1101, o, 1, OP);
4465 o->op_type = (OPCODE)type;
4466 o->op_ppaddr = PL_ppaddr[type];
4467 o->op_flags = (U8)flags;
4470 o->op_private = (U8)(0 | (flags >> 8));
4471 if (PL_opargs[type] & OA_RETSCALAR)
4473 if (PL_opargs[type] & OA_TARGET)
4474 o->op_targ = pad_alloc(type, SVs_PADTMP);
4475 return CHECKOP(type, o);
4479 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4481 Constructs, checks, and returns an op of any unary type. I<type> is
4482 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4483 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4484 bits, the eight bits of C<op_private>, except that the bit with value 1
4485 is automatically set. I<first> supplies an optional op to be the direct
4486 child of the unary op; it is consumed by this function and become part
4487 of the constructed op tree.
4493 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4498 if (type == -OP_ENTEREVAL) {
4499 type = OP_ENTEREVAL;
4500 flags |= OPpEVAL_BYTES<<8;
4503 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4504 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4505 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4506 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4507 || type == OP_SASSIGN
4508 || type == OP_ENTERTRY
4509 || type == OP_NULL );
4512 first = newOP(OP_STUB, 0);
4513 if (PL_opargs[type] & OA_MARK)
4514 first = force_list(first, 1);
4516 NewOp(1101, unop, 1, UNOP);
4517 unop->op_type = (OPCODE)type;
4518 unop->op_ppaddr = PL_ppaddr[type];
4519 unop->op_first = first;
4520 unop->op_flags = (U8)(flags | OPf_KIDS);
4521 unop->op_private = (U8)(1 | (flags >> 8));
4523 #ifdef PERL_OP_PARENT
4524 if (!OP_HAS_SIBLING(first)) /* true unless weird syntax error */
4525 first->op_sibling = (OP*)unop;
4528 unop = (UNOP*) CHECKOP(type, unop);
4532 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4536 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first
4538 Constructs, checks, and returns an op of method type with a method name
4539 evaluated at runtime. I<type> is the opcode. I<flags> gives the eight
4540 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
4541 and, shifted up eight bits, the eight bits of C<op_private>, except that
4542 the bit with value 1 is automatically set. I<dynamic_meth> supplies an
4543 op which evaluates method name; it is consumed by this function and
4544 become part of the constructed op tree.
4545 Supported optypes: OP_METHOD.
4551 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
4555 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP);
4557 NewOp(1101, methop, 1, METHOP);
4559 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1);
4560 methop->op_flags = (U8)(flags | OPf_KIDS);
4561 methop->op_u.op_first = dynamic_meth;
4562 methop->op_private = (U8)(1 | (flags >> 8));
4566 methop->op_flags = (U8)(flags & ~OPf_KIDS);
4567 methop->op_u.op_meth_sv = const_meth;
4568 methop->op_private = (U8)(0 | (flags >> 8));
4569 methop->op_next = (OP*)methop;
4572 methop->op_type = (OPCODE)type;
4573 methop->op_ppaddr = PL_ppaddr[type];
4574 methop = (METHOP*) CHECKOP(type, methop);
4576 if (methop->op_next) return (OP*)methop;
4578 return fold_constants(op_integerize(op_std_init((OP *) methop)));
4582 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
4583 PERL_ARGS_ASSERT_NEWMETHOP;
4584 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
4588 =for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth
4590 Constructs, checks, and returns an op of method type with a constant
4591 method name. I<type> is the opcode. I<flags> gives the eight bits of
4592 C<op_flags>, and, shifted up eight bits, the eight bits of
4593 C<op_private>. I<const_meth> supplies a constant method name;
4594 it must be a shared COW string.
4595 Supported optypes: OP_METHOD_NAMED.
4601 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
4602 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
4603 return newMETHOP_internal(type, flags, NULL, const_meth);
4607 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
4609 Constructs, checks, and returns an op of any binary type. I<type>
4610 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
4611 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
4612 the eight bits of C<op_private>, except that the bit with value 1 or
4613 2 is automatically set as required. I<first> and I<last> supply up to
4614 two ops to be the direct children of the binary op; they are consumed
4615 by this function and become part of the constructed op tree.
4621 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4626 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4627 || type == OP_SASSIGN || type == OP_NULL );
4629 NewOp(1101, binop, 1, BINOP);
4632 first = newOP(OP_NULL, 0);
4634 binop->op_type = (OPCODE)type;
4635 binop->op_ppaddr = PL_ppaddr[type];
4636 binop->op_first = first;
4637 binop->op_flags = (U8)(flags | OPf_KIDS);
4640 binop->op_private = (U8)(1 | (flags >> 8));
4643 binop->op_private = (U8)(2 | (flags >> 8));
4644 OP_SIBLING_set(first, last);
4645 first->op_lastsib = 0;
4648 #ifdef PERL_OP_PARENT
4649 if (!OP_HAS_SIBLING(last)) /* true unless weird syntax error */
4650 last->op_sibling = (OP*)binop;
4653 binop->op_last = OP_SIBLING(binop->op_first);
4654 #ifdef PERL_OP_PARENT
4656 binop->op_last->op_sibling = (OP*)binop;
4659 binop = (BINOP*)CHECKOP(type, binop);
4660 if (binop->op_next || binop->op_type != (OPCODE)type)
4663 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4666 static int uvcompare(const void *a, const void *b)
4667 __attribute__nonnull__(1)
4668 __attribute__nonnull__(2)
4669 __attribute__pure__;
4670 static int uvcompare(const void *a, const void *b)
4672 if (*((const UV *)a) < (*(const UV *)b))
4674 if (*((const UV *)a) > (*(const UV *)b))
4676 if (*((const UV *)a+1) < (*(const UV *)b+1))
4678 if (*((const UV *)a+1) > (*(const UV *)b+1))
4684 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4686 SV * const tstr = ((SVOP*)expr)->op_sv;
4688 ((SVOP*)repl)->op_sv;
4691 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4692 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4698 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4699 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4700 I32 del = o->op_private & OPpTRANS_DELETE;
4703 PERL_ARGS_ASSERT_PMTRANS;
4705 PL_hints |= HINT_BLOCK_SCOPE;
4708 o->op_private |= OPpTRANS_FROM_UTF;
4711 o->op_private |= OPpTRANS_TO_UTF;
4713 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4714 SV* const listsv = newSVpvs("# comment\n");
4716 const U8* tend = t + tlen;
4717 const U8* rend = r + rlen;
4731 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4732 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4735 const U32 flags = UTF8_ALLOW_DEFAULT;
4739 t = tsave = bytes_to_utf8(t, &len);
4742 if (!to_utf && rlen) {
4744 r = rsave = bytes_to_utf8(r, &len);
4748 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has
4749 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
4753 U8 tmpbuf[UTF8_MAXBYTES+1];
4756 Newx(cp, 2*tlen, UV);
4758 transv = newSVpvs("");
4760 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4762 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
4764 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4768 cp[2*i+1] = cp[2*i];
4772 qsort(cp, i, 2*sizeof(UV), uvcompare);
4773 for (j = 0; j < i; j++) {
4775 diff = val - nextmin;
4777 t = uvchr_to_utf8(tmpbuf,nextmin);
4778 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4780 U8 range_mark = ILLEGAL_UTF8_BYTE;
4781 t = uvchr_to_utf8(tmpbuf, val - 1);
4782 sv_catpvn(transv, (char *)&range_mark, 1);
4783 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4790 t = uvchr_to_utf8(tmpbuf,nextmin);
4791 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4793 U8 range_mark = ILLEGAL_UTF8_BYTE;
4794 sv_catpvn(transv, (char *)&range_mark, 1);
4796 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
4797 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4798 t = (const U8*)SvPVX_const(transv);
4799 tlen = SvCUR(transv);
4803 else if (!rlen && !del) {
4804 r = t; rlen = tlen; rend = tend;
4807 if ((!rlen && !del) || t == r ||
4808 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4810 o->op_private |= OPpTRANS_IDENTICAL;
4814 while (t < tend || tfirst <= tlast) {
4815 /* see if we need more "t" chars */
4816 if (tfirst > tlast) {
4817 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4819 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4821 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4828 /* now see if we need more "r" chars */
4829 if (rfirst > rlast) {
4831 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4833 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4835 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4844 rfirst = rlast = 0xffffffff;
4848 /* now see which range will peter our first, if either. */
4849 tdiff = tlast - tfirst;
4850 rdiff = rlast - rfirst;
4857 if (rfirst == 0xffffffff) {
4858 diff = tdiff; /* oops, pretend rdiff is infinite */
4860 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4861 (long)tfirst, (long)tlast);
4863 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4867 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4868 (long)tfirst, (long)(tfirst + diff),