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 /* Used to avoid recursion through the op tree in scalarvoid() and
116 #define DEFERRED_OP_STEP 100
117 #define DEFER_OP(o) \
119 if (UNLIKELY(defer_ix == (defer_stack_alloc-1))) { \
120 defer_stack_alloc += DEFERRED_OP_STEP; \
121 assert(defer_stack_alloc > 0); \
122 Renew(defer_stack, defer_stack_alloc, OP *); \
124 defer_stack[++defer_ix] = o; \
127 #define POP_DEFERRED_OP() (defer_ix >= 0 ? defer_stack[defer_ix--] : (OP *)NULL)
129 /* remove any leading "empty" ops from the op_next chain whose first
130 * node's address is stored in op_p. Store the updated address of the
131 * first node in op_p.
135 S_prune_chain_head(OP** op_p)
138 && ( (*op_p)->op_type == OP_NULL
139 || (*op_p)->op_type == OP_SCOPE
140 || (*op_p)->op_type == OP_SCALAR
141 || (*op_p)->op_type == OP_LINESEQ)
143 *op_p = (*op_p)->op_next;
147 /* See the explanatory comments above struct opslab in op.h. */
149 #ifdef PERL_DEBUG_READONLY_OPS
150 # define PERL_SLAB_SIZE 128
151 # define PERL_MAX_SLAB_SIZE 4096
152 # include <sys/mman.h>
155 #ifndef PERL_SLAB_SIZE
156 # define PERL_SLAB_SIZE 64
158 #ifndef PERL_MAX_SLAB_SIZE
159 # define PERL_MAX_SLAB_SIZE 2048
162 /* rounds up to nearest pointer */
163 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
164 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
167 S_new_slab(pTHX_ size_t sz)
169 #ifdef PERL_DEBUG_READONLY_OPS
170 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
171 PROT_READ|PROT_WRITE,
172 MAP_ANON|MAP_PRIVATE, -1, 0);
173 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
174 (unsigned long) sz, slab));
175 if (slab == MAP_FAILED) {
176 perror("mmap failed");
179 slab->opslab_size = (U16)sz;
181 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
184 /* The context is unused in non-Windows */
187 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
191 /* requires double parens and aTHX_ */
192 #define DEBUG_S_warn(args) \
194 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
198 Perl_Slab_Alloc(pTHX_ size_t sz)
206 /* We only allocate ops from the slab during subroutine compilation.
207 We find the slab via PL_compcv, hence that must be non-NULL. It could
208 also be pointing to a subroutine which is now fully set up (CvROOT()
209 pointing to the top of the optree for that sub), or a subroutine
210 which isn't using the slab allocator. If our sanity checks aren't met,
211 don't use a slab, but allocate the OP directly from the heap. */
212 if (!PL_compcv || CvROOT(PL_compcv)
213 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
215 o = (OP*)PerlMemShared_calloc(1, sz);
219 /* While the subroutine is under construction, the slabs are accessed via
220 CvSTART(), to avoid needing to expand PVCV by one pointer for something
221 unneeded at runtime. Once a subroutine is constructed, the slabs are
222 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
223 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
225 if (!CvSTART(PL_compcv)) {
227 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
228 CvSLABBED_on(PL_compcv);
229 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
231 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
233 opsz = SIZE_TO_PSIZE(sz);
234 sz = opsz + OPSLOT_HEADER_P;
236 /* The slabs maintain a free list of OPs. In particular, constant folding
237 will free up OPs, so it makes sense to re-use them where possible. A
238 freed up slot is used in preference to a new allocation. */
239 if (slab->opslab_freed) {
240 OP **too = &slab->opslab_freed;
242 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
243 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
244 DEBUG_S_warn((aTHX_ "Alas! too small"));
245 o = *(too = &o->op_next);
246 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
250 Zero(o, opsz, I32 *);
256 #define INIT_OPSLOT \
257 slot->opslot_slab = slab; \
258 slot->opslot_next = slab2->opslab_first; \
259 slab2->opslab_first = slot; \
260 o = &slot->opslot_op; \
263 /* The partially-filled slab is next in the chain. */
264 slab2 = slab->opslab_next ? slab->opslab_next : slab;
265 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
266 /* Remaining space is too small. */
268 /* If we can fit a BASEOP, add it to the free chain, so as not
270 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
271 slot = &slab2->opslab_slots;
273 o->op_type = OP_FREED;
274 o->op_next = slab->opslab_freed;
275 slab->opslab_freed = o;
278 /* Create a new slab. Make this one twice as big. */
279 slot = slab2->opslab_first;
280 while (slot->opslot_next) slot = slot->opslot_next;
281 slab2 = S_new_slab(aTHX_
282 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
284 : (DIFF(slab2, slot)+1)*2);
285 slab2->opslab_next = slab->opslab_next;
286 slab->opslab_next = slab2;
288 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
290 /* Create a new op slot */
291 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
292 assert(slot >= &slab2->opslab_slots);
293 if (DIFF(&slab2->opslab_slots, slot)
294 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
295 slot = &slab2->opslab_slots;
297 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
300 /* lastsib == 1, op_sibling == 0 implies a solitary unattached op */
302 assert(!o->op_sibling);
309 #ifdef PERL_DEBUG_READONLY_OPS
311 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
313 PERL_ARGS_ASSERT_SLAB_TO_RO;
315 if (slab->opslab_readonly) return;
316 slab->opslab_readonly = 1;
317 for (; slab; slab = slab->opslab_next) {
318 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
319 (unsigned long) slab->opslab_size, slab));*/
320 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
321 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
322 (unsigned long)slab->opslab_size, errno);
327 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
331 PERL_ARGS_ASSERT_SLAB_TO_RW;
333 if (!slab->opslab_readonly) return;
335 for (; slab2; slab2 = slab2->opslab_next) {
336 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
337 (unsigned long) size, slab2));*/
338 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
339 PROT_READ|PROT_WRITE)) {
340 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
341 (unsigned long)slab2->opslab_size, errno);
344 slab->opslab_readonly = 0;
348 # define Slab_to_rw(op) NOOP
351 /* This cannot possibly be right, but it was copied from the old slab
352 allocator, to which it was originally added, without explanation, in
355 # define PerlMemShared PerlMem
359 Perl_Slab_Free(pTHX_ void *op)
361 OP * const o = (OP *)op;
364 PERL_ARGS_ASSERT_SLAB_FREE;
366 if (!o->op_slabbed) {
368 PerlMemShared_free(op);
373 /* If this op is already freed, our refcount will get screwy. */
374 assert(o->op_type != OP_FREED);
375 o->op_type = OP_FREED;
376 o->op_next = slab->opslab_freed;
377 slab->opslab_freed = o;
378 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
379 OpslabREFCNT_dec_padok(slab);
383 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
385 const bool havepad = !!PL_comppad;
386 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
389 PAD_SAVE_SETNULLPAD();
396 Perl_opslab_free(pTHX_ OPSLAB *slab)
399 PERL_ARGS_ASSERT_OPSLAB_FREE;
401 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
402 assert(slab->opslab_refcnt == 1);
403 for (; slab; slab = slab2) {
404 slab2 = slab->opslab_next;
406 slab->opslab_refcnt = ~(size_t)0;
408 #ifdef PERL_DEBUG_READONLY_OPS
409 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
411 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
412 perror("munmap failed");
416 PerlMemShared_free(slab);
422 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
427 size_t savestack_count = 0;
429 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
432 for (slot = slab2->opslab_first;
434 slot = slot->opslot_next) {
435 if (slot->opslot_op.op_type != OP_FREED
436 && !(slot->opslot_op.op_savefree
442 assert(slot->opslot_op.op_slabbed);
443 op_free(&slot->opslot_op);
444 if (slab->opslab_refcnt == 1) goto free;
447 } while ((slab2 = slab2->opslab_next));
448 /* > 1 because the CV still holds a reference count. */
449 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
451 assert(savestack_count == slab->opslab_refcnt-1);
453 /* Remove the CV’s reference count. */
454 slab->opslab_refcnt--;
461 #ifdef PERL_DEBUG_READONLY_OPS
463 Perl_op_refcnt_inc(pTHX_ OP *o)
466 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
467 if (slab && slab->opslab_readonly) {
480 Perl_op_refcnt_dec(pTHX_ OP *o)
483 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
485 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
487 if (slab && slab->opslab_readonly) {
489 result = --o->op_targ;
492 result = --o->op_targ;
498 * In the following definition, the ", (OP*)0" is just to make the compiler
499 * think the expression is of the right type: croak actually does a Siglongjmp.
501 #define CHECKOP(type,o) \
502 ((PL_op_mask && PL_op_mask[type]) \
503 ? ( op_free((OP*)o), \
504 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
506 : PL_check[type](aTHX_ (OP*)o))
508 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
510 #define CHANGE_TYPE(o,type) \
512 o->op_type = (OPCODE)type; \
513 o->op_ppaddr = PL_ppaddr[type]; \
517 S_no_fh_allowed(pTHX_ OP *o)
519 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
521 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
527 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
529 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
530 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
535 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
537 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
539 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
544 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
546 PERL_ARGS_ASSERT_BAD_TYPE_PV;
548 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
549 (int)n, name, t, OP_DESC(kid)), flags);
553 S_bad_type_gv(pTHX_ I32 n, const char *t, GV *gv, U32 flags, const OP *kid)
555 SV * const namesv = cv_name((CV *)gv, NULL, 0);
556 PERL_ARGS_ASSERT_BAD_TYPE_GV;
558 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
559 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
563 S_no_bareword_allowed(pTHX_ OP *o)
565 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
567 qerror(Perl_mess(aTHX_
568 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
570 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
573 /* "register" allocation */
576 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
579 const bool is_our = (PL_parser->in_my == KEY_our);
581 PERL_ARGS_ASSERT_ALLOCMY;
583 if (flags & ~SVf_UTF8)
584 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
587 /* complain about "my $<special_var>" etc etc */
591 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
592 (name[1] == '_' && (*name == '$' || len > 2))))
594 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
595 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
596 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
597 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
598 PL_parser->in_my == KEY_state ? "state" : "my"));
600 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
601 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
604 else if (len == 2 && name[1] == '_' && !is_our)
605 /* diag_listed_as: Use of my $_ is experimental */
606 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
607 "Use of %s $_ is experimental",
608 PL_parser->in_my == KEY_state
612 /* allocate a spare slot and store the name in that slot */
614 off = pad_add_name_pvn(name, len,
615 (is_our ? padadd_OUR :
616 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
617 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
618 PL_parser->in_my_stash,
620 /* $_ is always in main::, even with our */
621 ? (PL_curstash && !memEQs(name,len,"$_")
627 /* anon sub prototypes contains state vars should always be cloned,
628 * otherwise the state var would be shared between anon subs */
630 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
631 CvCLONE_on(PL_compcv);
637 =head1 Optree Manipulation Functions
639 =for apidoc alloccopstash
641 Available only under threaded builds, this function allocates an entry in
642 C<PL_stashpad> for the stash passed to it.
649 Perl_alloccopstash(pTHX_ HV *hv)
651 PADOFFSET off = 0, o = 1;
652 bool found_slot = FALSE;
654 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
656 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
658 for (; o < PL_stashpadmax; ++o) {
659 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
660 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
661 found_slot = TRUE, off = o;
664 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
665 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
666 off = PL_stashpadmax;
667 PL_stashpadmax += 10;
670 PL_stashpad[PL_stashpadix = off] = hv;
675 /* free the body of an op without examining its contents.
676 * Always use this rather than FreeOp directly */
679 S_op_destroy(pTHX_ OP *o)
687 =for apidoc Am|void|op_free|OP *o
689 Free an op. Only use this when an op is no longer linked to from any
696 Perl_op_free(pTHX_ OP *o)
700 SSize_t defer_ix = -1;
701 SSize_t defer_stack_alloc = 0;
702 OP **defer_stack = NULL;
706 /* Though ops may be freed twice, freeing the op after its slab is a
708 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
709 /* During the forced freeing of ops after compilation failure, kidops
710 may be freed before their parents. */
711 if (!o || o->op_type == OP_FREED)
716 /* an op should only ever acquire op_private flags that we know about.
717 * If this fails, you may need to fix something in regen/op_private */
718 if (o->op_ppaddr == PL_ppaddr[o->op_type]) {
719 assert(!(o->op_private & ~PL_op_private_valid[type]));
722 if (o->op_private & OPpREFCOUNTED) {
733 refcnt = OpREFCNT_dec(o);
736 /* Need to find and remove any pattern match ops from the list
737 we maintain for reset(). */
738 find_and_forget_pmops(o);
748 /* Call the op_free hook if it has been set. Do it now so that it's called
749 * at the right time for refcounted ops, but still before all of the kids
753 if (o->op_flags & OPf_KIDS) {
755 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
756 nextkid = OP_SIBLING(kid); /* Get before next freeing kid */
757 if (!kid || kid->op_type == OP_FREED)
758 /* During the forced freeing of ops after
759 compilation failure, kidops may be freed before
762 if (!(kid->op_flags & OPf_KIDS))
763 /* If it has no kids, just free it now */
770 type = (OPCODE)o->op_targ;
773 Slab_to_rw(OpSLAB(o));
775 /* COP* is not cleared by op_clear() so that we may track line
776 * numbers etc even after null() */
777 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
783 #ifdef DEBUG_LEAKING_SCALARS
787 } while ( (o = POP_DEFERRED_OP()) );
789 Safefree(defer_stack);
793 Perl_op_clear(pTHX_ OP *o)
798 PERL_ARGS_ASSERT_OP_CLEAR;
800 switch (o->op_type) {
801 case OP_NULL: /* Was holding old type, if any. */
804 case OP_ENTEREVAL: /* Was holding hints. */
808 if (!(o->op_flags & OPf_REF)
809 || (PL_check[o->op_type] != Perl_ck_ftst))
816 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
821 /* It's possible during global destruction that the GV is freed
822 before the optree. Whilst the SvREFCNT_inc is happy to bump from
823 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
824 will trigger an assertion failure, because the entry to sv_clear
825 checks that the scalar is not already freed. A check of for
826 !SvIS_FREED(gv) turns out to be invalid, because during global
827 destruction the reference count can be forced down to zero
828 (with SVf_BREAK set). In which case raising to 1 and then
829 dropping to 0 triggers cleanup before it should happen. I
830 *think* that this might actually be a general, systematic,
831 weakness of the whole idea of SVf_BREAK, in that code *is*
832 allowed to raise and lower references during global destruction,
833 so any *valid* code that happens to do this during global
834 destruction might well trigger premature cleanup. */
835 bool still_valid = gv && SvREFCNT(gv);
838 SvREFCNT_inc_simple_void(gv);
840 if (cPADOPo->op_padix > 0) {
841 pad_swipe(cPADOPo->op_padix, TRUE);
842 cPADOPo->op_padix = 0;
845 SvREFCNT_dec(cSVOPo->op_sv);
846 cSVOPo->op_sv = NULL;
849 int try_downgrade = SvREFCNT(gv) == 2;
852 gv_try_downgrade(gv);
856 case OP_METHOD_NAMED:
857 SvREFCNT_dec(cMETHOPx(o)->op_u.op_meth_sv);
858 cMETHOPx(o)->op_u.op_meth_sv = NULL;
861 pad_swipe(o->op_targ, 1);
867 if (cMETHOPx(o)->op_class_targ) {
868 pad_swipe(cMETHOPx(o)->op_class_targ, 1);
869 cMETHOPx(o)->op_class_targ = 0;
872 SvREFCNT_dec(cMETHOPx(o)->op_class_sv);
873 cMETHOPx(o)->op_class_sv = NULL;
878 SvREFCNT_dec(cSVOPo->op_sv);
879 cSVOPo->op_sv = NULL;
882 Even if op_clear does a pad_free for the target of the op,
883 pad_free doesn't actually remove the sv that exists in the pad;
884 instead it lives on. This results in that it could be reused as
885 a target later on when the pad was reallocated.
888 pad_swipe(o->op_targ,1);
898 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
903 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
904 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
906 if (cPADOPo->op_padix > 0) {
907 pad_swipe(cPADOPo->op_padix, TRUE);
908 cPADOPo->op_padix = 0;
911 SvREFCNT_dec(cSVOPo->op_sv);
912 cSVOPo->op_sv = NULL;
916 PerlMemShared_free(cPVOPo->op_pv);
917 cPVOPo->op_pv = NULL;
921 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
925 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
926 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
929 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
935 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
936 op_free(cPMOPo->op_code_list);
937 cPMOPo->op_code_list = NULL;
939 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
940 /* we use the same protection as the "SAFE" version of the PM_ macros
941 * here since sv_clean_all might release some PMOPs
942 * after PL_regex_padav has been cleared
943 * and the clearing of PL_regex_padav needs to
944 * happen before sv_clean_all
947 if(PL_regex_pad) { /* We could be in destruction */
948 const IV offset = (cPMOPo)->op_pmoffset;
949 ReREFCNT_dec(PM_GETRE(cPMOPo));
950 PL_regex_pad[offset] = &PL_sv_undef;
951 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
955 ReREFCNT_dec(PM_GETRE(cPMOPo));
956 PM_SETRE(cPMOPo, NULL);
962 if (o->op_targ > 0) {
963 pad_free(o->op_targ);
969 S_cop_free(pTHX_ COP* cop)
971 PERL_ARGS_ASSERT_COP_FREE;
974 if (! specialWARN(cop->cop_warnings))
975 PerlMemShared_free(cop->cop_warnings);
976 cophh_free(CopHINTHASH_get(cop));
977 if (PL_curcop == cop)
982 S_forget_pmop(pTHX_ PMOP *const o
985 HV * const pmstash = PmopSTASH(o);
987 PERL_ARGS_ASSERT_FORGET_PMOP;
989 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
990 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
992 PMOP **const array = (PMOP**) mg->mg_ptr;
993 U32 count = mg->mg_len / sizeof(PMOP**);
998 /* Found it. Move the entry at the end to overwrite it. */
999 array[i] = array[--count];
1000 mg->mg_len = count * sizeof(PMOP**);
1001 /* Could realloc smaller at this point always, but probably
1002 not worth it. Probably worth free()ing if we're the
1005 Safefree(mg->mg_ptr);
1018 S_find_and_forget_pmops(pTHX_ OP *o)
1020 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
1022 if (o->op_flags & OPf_KIDS) {
1023 OP *kid = cUNOPo->op_first;
1025 switch (kid->op_type) {
1030 forget_pmop((PMOP*)kid);
1032 find_and_forget_pmops(kid);
1033 kid = OP_SIBLING(kid);
1039 =for apidoc Am|void|op_null|OP *o
1041 Neutralizes an op when it is no longer needed, but is still linked to from
1048 Perl_op_null(pTHX_ OP *o)
1052 PERL_ARGS_ASSERT_OP_NULL;
1054 if (o->op_type == OP_NULL)
1057 o->op_targ = o->op_type;
1058 CHANGE_TYPE(o, OP_NULL);
1062 Perl_op_refcnt_lock(pTHX)
1067 PERL_UNUSED_CONTEXT;
1072 Perl_op_refcnt_unlock(pTHX)
1077 PERL_UNUSED_CONTEXT;
1083 =for apidoc op_sibling_splice
1085 A general function for editing the structure of an existing chain of
1086 op_sibling nodes. By analogy with the perl-level splice() function, allows
1087 you to delete zero or more sequential nodes, replacing them with zero or
1088 more different nodes. Performs the necessary op_first/op_last
1089 housekeeping on the parent node and op_sibling manipulation on the
1090 children. The last deleted node will be marked as as the last node by
1091 updating the op_sibling or op_lastsib field as appropriate.
1093 Note that op_next is not manipulated, and nodes are not freed; that is the
1094 responsibility of the caller. It also won't create a new list op for an
1095 empty list etc; use higher-level functions like op_append_elem() for that.
1097 parent is the parent node of the sibling chain.
1099 start is the node preceding the first node to be spliced. Node(s)
1100 following it will be deleted, and ops will be inserted after it. If it is
1101 NULL, the first node onwards is deleted, and nodes are inserted at the
1104 del_count is the number of nodes to delete. If zero, no nodes are deleted.
1105 If -1 or greater than or equal to the number of remaining kids, all
1106 remaining kids are deleted.
1108 insert is the first of a chain of nodes to be inserted in place of the nodes.
1109 If NULL, no nodes are inserted.
1111 The head of the chain of deleted ops is returned, or NULL if no ops were
1116 action before after returns
1117 ------ ----- ----- -------
1120 splice(P, A, 2, X-Y-Z) | | B-C
1124 splice(P, NULL, 1, X-Y) | | A
1128 splice(P, NULL, 3, NULL) | | A-B-C
1132 splice(P, B, 0, X-Y) | | NULL
1139 Perl_op_sibling_splice(OP *parent, OP *start, int del_count, OP* insert)
1141 OP *first = start ? OP_SIBLING(start) : cLISTOPx(parent)->op_first;
1143 OP *last_del = NULL;
1144 OP *last_ins = NULL;
1146 PERL_ARGS_ASSERT_OP_SIBLING_SPLICE;
1148 assert(del_count >= -1);
1150 if (del_count && first) {
1152 while (--del_count && OP_HAS_SIBLING(last_del))
1153 last_del = OP_SIBLING(last_del);
1154 rest = OP_SIBLING(last_del);
1155 OP_SIBLING_set(last_del, NULL);
1156 last_del->op_lastsib = 1;
1163 while (OP_HAS_SIBLING(last_ins))
1164 last_ins = OP_SIBLING(last_ins);
1165 OP_SIBLING_set(last_ins, rest);
1166 last_ins->op_lastsib = rest ? 0 : 1;
1172 OP_SIBLING_set(start, insert);
1173 start->op_lastsib = insert ? 0 : 1;
1176 cLISTOPx(parent)->op_first = insert;
1179 /* update op_last etc */
1180 U32 type = parent->op_type;
1183 if (type == OP_NULL)
1184 type = parent->op_targ;
1185 type = PL_opargs[type] & OA_CLASS_MASK;
1187 lastop = last_ins ? last_ins : start ? start : NULL;
1188 if ( type == OA_BINOP
1189 || type == OA_LISTOP
1193 cLISTOPx(parent)->op_last = lastop;
1196 lastop->op_lastsib = 1;
1197 #ifdef PERL_OP_PARENT
1198 lastop->op_sibling = parent;
1202 return last_del ? first : NULL;
1206 =for apidoc op_parent
1208 returns the parent OP of o, if it has a parent. Returns NULL otherwise.
1209 (Currently perl must be built with C<-DPERL_OP_PARENT> for this feature to
1216 Perl_op_parent(OP *o)
1218 PERL_ARGS_ASSERT_OP_PARENT;
1219 #ifdef PERL_OP_PARENT
1220 while (OP_HAS_SIBLING(o))
1222 return o->op_sibling;
1230 /* replace the sibling following start with a new UNOP, which becomes
1231 * the parent of the original sibling; e.g.
1233 * op_sibling_newUNOP(P, A, unop-args...)
1241 * where U is the new UNOP.
1243 * parent and start args are the same as for op_sibling_splice();
1244 * type and flags args are as newUNOP().
1246 * Returns the new UNOP.
1250 S_op_sibling_newUNOP(pTHX_ OP *parent, OP *start, I32 type, I32 flags)
1254 kid = op_sibling_splice(parent, start, 1, NULL);
1255 newop = newUNOP(type, flags, kid);
1256 op_sibling_splice(parent, start, 0, newop);
1261 /* lowest-level newLOGOP-style function - just allocates and populates
1262 * the struct. Higher-level stuff should be done by S_new_logop() /
1263 * newLOGOP(). This function exists mainly to avoid op_first assignment
1264 * being spread throughout this file.
1268 S_alloc_LOGOP(pTHX_ I32 type, OP *first, OP* other)
1273 NewOp(1101, logop, 1, LOGOP);
1274 CHANGE_TYPE(logop, type);
1275 logop->op_first = first;
1276 logop->op_other = other;
1277 logop->op_flags = OPf_KIDS;
1278 while (kid && OP_HAS_SIBLING(kid))
1279 kid = OP_SIBLING(kid);
1281 kid->op_lastsib = 1;
1282 #ifdef PERL_OP_PARENT
1283 kid->op_sibling = (OP*)logop;
1290 /* Contextualizers */
1293 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1295 Applies a syntactic context to an op tree representing an expression.
1296 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1297 or C<G_VOID> to specify the context to apply. The modified op tree
1304 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1306 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1308 case G_SCALAR: return scalar(o);
1309 case G_ARRAY: return list(o);
1310 case G_VOID: return scalarvoid(o);
1312 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1319 =for apidoc Am|OP*|op_linklist|OP *o
1320 This function is the implementation of the L</LINKLIST> macro. It should
1321 not be called directly.
1327 Perl_op_linklist(pTHX_ OP *o)
1331 PERL_ARGS_ASSERT_OP_LINKLIST;
1336 /* establish postfix order */
1337 first = cUNOPo->op_first;
1340 o->op_next = LINKLIST(first);
1343 OP *sibl = OP_SIBLING(kid);
1345 kid->op_next = LINKLIST(sibl);
1360 S_scalarkids(pTHX_ OP *o)
1362 if (o && o->op_flags & OPf_KIDS) {
1364 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1371 S_scalarboolean(pTHX_ OP *o)
1373 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1375 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1376 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1377 if (ckWARN(WARN_SYNTAX)) {
1378 const line_t oldline = CopLINE(PL_curcop);
1380 if (PL_parser && PL_parser->copline != NOLINE) {
1381 /* This ensures that warnings are reported at the first line
1382 of the conditional, not the last. */
1383 CopLINE_set(PL_curcop, PL_parser->copline);
1385 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1386 CopLINE_set(PL_curcop, oldline);
1393 S_op_varname(pTHX_ const OP *o)
1396 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1397 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1399 const char funny = o->op_type == OP_PADAV
1400 || o->op_type == OP_RV2AV ? '@' : '%';
1401 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1403 if (cUNOPo->op_first->op_type != OP_GV
1404 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1406 return varname(gv, funny, 0, NULL, 0, 1);
1409 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1414 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1415 { /* or not so pretty :-) */
1416 if (o->op_type == OP_CONST) {
1418 if (SvPOK(*retsv)) {
1420 *retsv = sv_newmortal();
1421 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1422 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1424 else if (!SvOK(*retsv))
1427 else *retpv = "...";
1431 S_scalar_slice_warning(pTHX_ const OP *o)
1435 o->op_type == OP_HSLICE ? '{' : '[';
1437 o->op_type == OP_HSLICE ? '}' : ']';
1439 SV *keysv = NULL; /* just to silence compiler warnings */
1440 const char *key = NULL;
1442 if (!(o->op_private & OPpSLICEWARNING))
1444 if (PL_parser && PL_parser->error_count)
1445 /* This warning can be nonsensical when there is a syntax error. */
1448 kid = cLISTOPo->op_first;
1449 kid = OP_SIBLING(kid); /* get past pushmark */
1450 /* weed out false positives: any ops that can return lists */
1451 switch (kid->op_type) {
1480 /* Don't warn if we have a nulled list either. */
1481 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1484 assert(OP_SIBLING(kid));
1485 name = S_op_varname(aTHX_ OP_SIBLING(kid));
1486 if (!name) /* XS module fiddling with the op tree */
1488 S_op_pretty(aTHX_ kid, &keysv, &key);
1489 assert(SvPOK(name));
1490 sv_chop(name,SvPVX(name)+1);
1492 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1493 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1494 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1496 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1497 lbrack, key, rbrack);
1499 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1500 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1501 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1503 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1504 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1508 Perl_scalar(pTHX_ OP *o)
1512 /* assumes no premature commitment */
1513 if (!o || (PL_parser && PL_parser->error_count)
1514 || (o->op_flags & OPf_WANT)
1515 || o->op_type == OP_RETURN)
1520 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1522 switch (o->op_type) {
1524 scalar(cBINOPo->op_first);
1525 if (o->op_private & OPpREPEAT_DOLIST) {
1526 kid = cLISTOPx(cUNOPo->op_first)->op_first;
1527 assert(kid->op_type == OP_PUSHMARK);
1528 if (OP_HAS_SIBLING(kid) && !OP_HAS_SIBLING(OP_SIBLING(kid))) {
1529 op_null(cLISTOPx(cUNOPo->op_first)->op_first);
1530 o->op_private &=~ OPpREPEAT_DOLIST;
1537 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1547 if (o->op_flags & OPf_KIDS) {
1548 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid))
1554 kid = cLISTOPo->op_first;
1556 kid = OP_SIBLING(kid);
1559 OP *sib = OP_SIBLING(kid);
1560 if (sib && kid->op_type != OP_LEAVEWHEN
1561 && ( OP_HAS_SIBLING(sib) || sib->op_type != OP_NULL
1562 || ( sib->op_targ != OP_NEXTSTATE
1563 && sib->op_targ != OP_DBSTATE )))
1569 PL_curcop = &PL_compiling;
1574 kid = cLISTOPo->op_first;
1577 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1582 /* Warn about scalar context */
1583 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1584 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1587 const char *key = NULL;
1589 /* This warning can be nonsensical when there is a syntax error. */
1590 if (PL_parser && PL_parser->error_count)
1593 if (!ckWARN(WARN_SYNTAX)) break;
1595 kid = cLISTOPo->op_first;
1596 kid = OP_SIBLING(kid); /* get past pushmark */
1597 assert(OP_SIBLING(kid));
1598 name = S_op_varname(aTHX_ OP_SIBLING(kid));
1599 if (!name) /* XS module fiddling with the op tree */
1601 S_op_pretty(aTHX_ kid, &keysv, &key);
1602 assert(SvPOK(name));
1603 sv_chop(name,SvPVX(name)+1);
1605 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1606 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1607 "%%%"SVf"%c%s%c in scalar context better written "
1609 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1610 lbrack, key, rbrack);
1612 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1613 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1614 "%%%"SVf"%c%"SVf"%c in scalar context better "
1615 "written as $%"SVf"%c%"SVf"%c",
1616 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1617 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1624 Perl_scalarvoid(pTHX_ OP *arg)
1630 SSize_t defer_stack_alloc = 0;
1631 SSize_t defer_ix = -1;
1632 OP **defer_stack = NULL;
1635 PERL_ARGS_ASSERT_SCALARVOID;
1638 SV *useless_sv = NULL;
1639 const char* useless = NULL;
1641 if (o->op_type == OP_NEXTSTATE
1642 || o->op_type == OP_DBSTATE
1643 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1644 || o->op_targ == OP_DBSTATE)))
1645 PL_curcop = (COP*)o; /* for warning below */
1647 /* assumes no premature commitment */
1648 want = o->op_flags & OPf_WANT;
1649 if ((want && want != OPf_WANT_SCALAR)
1650 || (PL_parser && PL_parser->error_count)
1651 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1656 if ((o->op_private & OPpTARGET_MY)
1657 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1659 /* newASSIGNOP has already applied scalar context, which we
1660 leave, as if this op is inside SASSIGN. */
1664 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1666 switch (o->op_type) {
1668 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1672 if (o->op_flags & OPf_STACKED)
1676 if (o->op_private == 4)
1711 case OP_GETSOCKNAME:
1712 case OP_GETPEERNAME:
1717 case OP_GETPRIORITY:
1742 useless = OP_DESC(o);
1752 case OP_AELEMFAST_LEX:
1756 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1757 /* Otherwise it's "Useless use of grep iterator" */
1758 useless = OP_DESC(o);
1762 kid = cLISTOPo->op_first;
1763 if (kid && kid->op_type == OP_PUSHRE
1765 && !(o->op_flags & OPf_STACKED)
1767 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
1769 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
1772 useless = OP_DESC(o);
1776 kid = cUNOPo->op_first;
1777 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1778 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1781 useless = "negative pattern binding (!~)";
1785 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1786 useless = "non-destructive substitution (s///r)";
1790 useless = "non-destructive transliteration (tr///r)";
1797 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1798 (!OP_HAS_SIBLING(o) || OP_SIBLING(o)->op_type != OP_READLINE))
1799 useless = "a variable";
1804 if (cSVOPo->op_private & OPpCONST_STRICT)
1805 no_bareword_allowed(o);
1807 if (ckWARN(WARN_VOID)) {
1809 /* don't warn on optimised away booleans, eg
1810 * use constant Foo, 5; Foo || print; */
1811 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1813 /* the constants 0 and 1 are permitted as they are
1814 conventionally used as dummies in constructs like
1815 1 while some_condition_with_side_effects; */
1816 else if (SvNIOK(sv) && ((nv = SvNV(sv)) == 0.0 || nv == 1.0))
1818 else if (SvPOK(sv)) {
1819 SV * const dsv = newSVpvs("");
1821 = Perl_newSVpvf(aTHX_
1823 pv_pretty(dsv, SvPVX_const(sv),
1824 SvCUR(sv), 32, NULL, NULL,
1826 | PERL_PV_ESCAPE_NOCLEAR
1827 | PERL_PV_ESCAPE_UNI_DETECT));
1828 SvREFCNT_dec_NN(dsv);
1830 else if (SvOK(sv)) {
1831 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1834 useless = "a constant (undef)";
1837 op_null(o); /* don't execute or even remember it */
1841 CHANGE_TYPE(o, OP_PREINC); /* pre-increment is faster */
1845 CHANGE_TYPE(o, OP_PREDEC); /* pre-decrement is faster */
1849 CHANGE_TYPE(o, OP_I_PREINC); /* pre-increment is faster */
1853 CHANGE_TYPE(o, OP_I_PREDEC); /* pre-decrement is faster */
1858 UNOP *refgen, *rv2cv;
1861 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1864 rv2gv = ((BINOP *)o)->op_last;
1865 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1868 refgen = (UNOP *)((BINOP *)o)->op_first;
1870 if (!refgen || (refgen->op_type != OP_REFGEN
1871 && refgen->op_type != OP_SREFGEN))
1874 exlist = (LISTOP *)refgen->op_first;
1875 if (!exlist || exlist->op_type != OP_NULL
1876 || exlist->op_targ != OP_LIST)
1879 if (exlist->op_first->op_type != OP_PUSHMARK
1880 && exlist->op_first != exlist->op_last)
1883 rv2cv = (UNOP*)exlist->op_last;
1885 if (rv2cv->op_type != OP_RV2CV)
1888 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1889 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1890 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1892 o->op_private |= OPpASSIGN_CV_TO_GV;
1893 rv2gv->op_private |= OPpDONT_INIT_GV;
1894 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1906 kid = cLOGOPo->op_first;
1907 if (kid->op_type == OP_NOT
1908 && (kid->op_flags & OPf_KIDS)) {
1909 if (o->op_type == OP_AND) {
1910 CHANGE_TYPE(o, OP_OR);
1912 CHANGE_TYPE(o, OP_AND);
1922 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
1923 if (!(kid->op_flags & OPf_KIDS))
1930 if (o->op_flags & OPf_STACKED)
1937 if (!(o->op_flags & OPf_KIDS))
1948 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
1949 if (!(kid->op_flags & OPf_KIDS))
1955 /* If the first kid after pushmark is something that the padrange
1956 optimisation would reject, then null the list and the pushmark.
1958 if ((kid = cLISTOPo->op_first)->op_type == OP_PUSHMARK
1959 && ( !(kid = OP_SIBLING(kid))
1960 || ( kid->op_type != OP_PADSV
1961 && kid->op_type != OP_PADAV
1962 && kid->op_type != OP_PADHV)
1963 || kid->op_private & ~OPpLVAL_INTRO
1964 || !(kid = OP_SIBLING(kid))
1965 || ( kid->op_type != OP_PADSV
1966 && kid->op_type != OP_PADAV
1967 && kid->op_type != OP_PADHV)
1968 || kid->op_private & ~OPpLVAL_INTRO)
1970 op_null(cUNOPo->op_first); /* NULL the pushmark */
1971 op_null(o); /* NULL the list */
1983 /* mortalise it, in case warnings are fatal. */
1984 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1985 "Useless use of %"SVf" in void context",
1986 SVfARG(sv_2mortal(useless_sv)));
1989 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1990 "Useless use of %s in void context",
1993 } while ( (o = POP_DEFERRED_OP()) );
1995 Safefree(defer_stack);
2001 S_listkids(pTHX_ OP *o)
2003 if (o && o->op_flags & OPf_KIDS) {
2005 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2012 Perl_list(pTHX_ OP *o)
2016 /* assumes no premature commitment */
2017 if (!o || (o->op_flags & OPf_WANT)
2018 || (PL_parser && PL_parser->error_count)
2019 || o->op_type == OP_RETURN)
2024 if ((o->op_private & OPpTARGET_MY)
2025 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2027 return o; /* As if inside SASSIGN */
2030 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
2032 switch (o->op_type) {
2034 list(cBINOPo->op_first);
2037 if (o->op_private & OPpREPEAT_DOLIST
2038 && !(o->op_flags & OPf_STACKED))
2040 list(cBINOPo->op_first);
2041 kid = cBINOPo->op_last;
2042 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)
2043 && SvIVX(kSVOP_sv) == 1)
2045 op_null(o); /* repeat */
2046 op_null(cUNOPx(cBINOPo->op_first)->op_first);/* pushmark */
2048 op_free(op_sibling_splice(o, cBINOPo->op_first, 1, NULL));
2055 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
2063 if (!(o->op_flags & OPf_KIDS))
2065 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
2066 list(cBINOPo->op_first);
2067 return gen_constant_list(o);
2073 if (cLISTOPo->op_first->op_type == OP_PUSHMARK) {
2074 op_null(cUNOPo->op_first); /* NULL the pushmark */
2075 op_null(o); /* NULL the list */
2080 kid = cLISTOPo->op_first;
2082 kid = OP_SIBLING(kid);
2085 OP *sib = OP_SIBLING(kid);
2086 if (sib && kid->op_type != OP_LEAVEWHEN)
2092 PL_curcop = &PL_compiling;
2096 kid = cLISTOPo->op_first;
2103 S_scalarseq(pTHX_ OP *o)
2106 const OPCODE type = o->op_type;
2108 if (type == OP_LINESEQ || type == OP_SCOPE ||
2109 type == OP_LEAVE || type == OP_LEAVETRY)
2112 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2113 if (OP_HAS_SIBLING(kid)) {
2117 PL_curcop = &PL_compiling;
2119 o->op_flags &= ~OPf_PARENS;
2120 if (PL_hints & HINT_BLOCK_SCOPE)
2121 o->op_flags |= OPf_PARENS;
2124 o = newOP(OP_STUB, 0);
2129 S_modkids(pTHX_ OP *o, I32 type)
2131 if (o && o->op_flags & OPf_KIDS) {
2133 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2134 op_lvalue(kid, type);
2140 =for apidoc finalize_optree
2142 This function finalizes the optree. Should be called directly after
2143 the complete optree is built. It does some additional
2144 checking which can't be done in the normal ck_xxx functions and makes
2145 the tree thread-safe.
2150 Perl_finalize_optree(pTHX_ OP* o)
2152 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
2155 SAVEVPTR(PL_curcop);
2163 /* Relocate sv to the pad for thread safety.
2164 * Despite being a "constant", the SV is written to,
2165 * for reference counts, sv_upgrade() etc. */
2166 PERL_STATIC_INLINE void
2167 S_op_relocate_sv(pTHX_ SV** svp, PADOFFSET* targp)
2170 PERL_ARGS_ASSERT_OP_RELOCATE_SV;
2172 ix = pad_alloc(OP_CONST, SVf_READONLY);
2173 SvREFCNT_dec(PAD_SVl(ix));
2174 PAD_SETSV(ix, *svp);
2175 /* XXX I don't know how this isn't readonly already. */
2176 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
2184 S_finalize_op(pTHX_ OP* o)
2186 PERL_ARGS_ASSERT_FINALIZE_OP;
2189 switch (o->op_type) {
2192 PL_curcop = ((COP*)o); /* for warnings */
2195 if (OP_HAS_SIBLING(o)) {
2196 OP *sib = OP_SIBLING(o);
2197 if (( sib->op_type == OP_NEXTSTATE || sib->op_type == OP_DBSTATE)
2198 && ckWARN(WARN_EXEC)
2199 && OP_HAS_SIBLING(sib))
2201 const OPCODE type = OP_SIBLING(sib)->op_type;
2202 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
2203 const line_t oldline = CopLINE(PL_curcop);
2204 CopLINE_set(PL_curcop, CopLINE((COP*)sib));
2205 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2206 "Statement unlikely to be reached");
2207 Perl_warner(aTHX_ packWARN(WARN_EXEC),
2208 "\t(Maybe you meant system() when you said exec()?)\n");
2209 CopLINE_set(PL_curcop, oldline);
2216 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
2217 GV * const gv = cGVOPo_gv;
2218 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
2219 /* XXX could check prototype here instead of just carping */
2220 SV * const sv = sv_newmortal();
2221 gv_efullname3(sv, gv, NULL);
2222 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2223 "%"SVf"() called too early to check prototype",
2230 if (cSVOPo->op_private & OPpCONST_STRICT)
2231 no_bareword_allowed(o);
2235 op_relocate_sv(&cSVOPo->op_sv, &o->op_targ);
2240 /* Relocate all the METHOP's SVs to the pad for thread safety. */
2241 case OP_METHOD_NAMED:
2242 op_relocate_sv(&cMETHOPx(o)->op_u.op_meth_sv, &o->op_targ);
2254 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
2257 rop = (UNOP*)((BINOP*)o)->op_first;
2262 S_scalar_slice_warning(aTHX_ o);
2266 kid = OP_SIBLING(cLISTOPo->op_first);
2267 if (/* I bet there's always a pushmark... */
2268 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
2269 && OP_TYPE_ISNT_NN(kid, OP_CONST))
2274 key_op = (SVOP*)(kid->op_type == OP_CONST
2276 : OP_SIBLING(kLISTOP->op_first));
2278 rop = (UNOP*)((LISTOP*)o)->op_last;
2281 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
2283 else if (rop->op_first->op_type == OP_PADSV)
2284 /* @$hash{qw(keys here)} */
2285 rop = (UNOP*)rop->op_first;
2287 /* @{$hash}{qw(keys here)} */
2288 if (rop->op_first->op_type == OP_SCOPE
2289 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
2291 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
2297 lexname = NULL; /* just to silence compiler warnings */
2298 fields = NULL; /* just to silence compiler warnings */
2302 && (lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE),
2303 SvPAD_TYPED(lexname))
2304 && (fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE))
2305 && isGV(*fields) && GvHV(*fields);
2307 key_op = (SVOP*)OP_SIBLING(key_op)) {
2309 if (key_op->op_type != OP_CONST)
2311 svp = cSVOPx_svp(key_op);
2313 /* Make the CONST have a shared SV */
2314 if ((!SvIsCOW_shared_hash(sv = *svp))
2315 && SvTYPE(sv) < SVt_PVMG && SvOK(sv) && !SvROK(sv)) {
2317 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
2318 SV *nsv = newSVpvn_share(key,
2319 SvUTF8(sv) ? -keylen : keylen, 0);
2320 SvREFCNT_dec_NN(sv);
2325 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) {
2326 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2327 "in variable %"SVf" of type %"HEKf,
2328 SVfARG(*svp), SVfARG(lexname),
2329 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
2335 S_scalar_slice_warning(aTHX_ o);
2339 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2340 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2347 if (o->op_flags & OPf_KIDS) {
2351 /* check that op_last points to the last sibling, and that
2352 * the last op_sibling field points back to the parent, and
2353 * that the only ops with KIDS are those which are entitled to
2355 U32 type = o->op_type;
2359 if (type == OP_NULL) {
2361 /* ck_glob creates a null UNOP with ex-type GLOB
2362 * (which is a list op. So pretend it wasn't a listop */
2363 if (type == OP_GLOB)
2366 family = PL_opargs[type] & OA_CLASS_MASK;
2368 has_last = ( family == OA_BINOP
2369 || family == OA_LISTOP
2370 || family == OA_PMOP
2371 || family == OA_LOOP
2373 assert( has_last /* has op_first and op_last, or ...
2374 ... has (or may have) op_first: */
2375 || family == OA_UNOP
2376 || family == OA_LOGOP
2377 || family == OA_BASEOP_OR_UNOP
2378 || family == OA_FILESTATOP
2379 || family == OA_LOOPEXOP
2380 || family == OA_METHOP
2381 /* I don't know why SASSIGN is tagged as OA_BASEOP - DAPM */
2382 || type == OP_SASSIGN
2383 || type == OP_CUSTOM
2384 || type == OP_NULL /* new_logop does this */
2386 /* XXX list form of 'x' is has a null op_last. This is wrong,
2387 * but requires too much hacking (e.g. in Deparse) to fix for
2389 if (type == OP_REPEAT && (o->op_private & OPpREPEAT_DOLIST)) {
2394 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2395 # ifdef PERL_OP_PARENT
2396 if (!OP_HAS_SIBLING(kid)) {
2398 assert(kid == cLISTOPo->op_last);
2399 assert(kid->op_sibling == o);
2402 if (OP_HAS_SIBLING(kid)) {
2403 assert(!kid->op_lastsib);
2406 assert(kid->op_lastsib);
2408 assert(kid == cLISTOPo->op_last);
2414 for (kid = cUNOPo->op_first; kid; kid = OP_SIBLING(kid))
2420 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2422 Propagate lvalue ("modifiable") context to an op and its children.
2423 I<type> represents the context type, roughly based on the type of op that
2424 would do the modifying, although C<local()> is represented by OP_NULL,
2425 because it has no op type of its own (it is signalled by a flag on
2428 This function detects things that can't be modified, such as C<$x+1>, and
2429 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2430 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2432 It also flags things that need to behave specially in an lvalue context,
2433 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2439 S_mark_padname_lvalue(pTHX_ PADNAME *pn)
2442 PadnameLVALUE_on(pn);
2443 while (PadnameOUTER(pn) && PARENT_PAD_INDEX(pn)) {
2446 assert(CvPADLIST(cv));
2448 PadlistNAMESARRAY(CvPADLIST(cv))[PARENT_PAD_INDEX(pn)];
2449 assert(PadnameLEN(pn));
2450 PadnameLVALUE_on(pn);
2455 S_vivifies(const OPCODE type)
2458 case OP_RV2AV: case OP_ASLICE:
2459 case OP_RV2HV: case OP_KVASLICE:
2460 case OP_RV2SV: case OP_HSLICE:
2461 case OP_AELEMFAST: case OP_KVHSLICE:
2470 S_lvref(pTHX_ OP *o, I32 type)
2474 switch (o->op_type) {
2476 for (kid = OP_SIBLING(cUNOPo->op_first); kid;
2477 kid = OP_SIBLING(kid))
2478 S_lvref(aTHX_ kid, type);
2483 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2484 o->op_flags |= OPf_STACKED;
2485 if (o->op_flags & OPf_PARENS) {
2486 if (o->op_private & OPpLVAL_INTRO) {
2487 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2488 "localized parenthesized array in list assignment"));
2492 CHANGE_TYPE(o, OP_LVAVREF);
2493 o->op_private &= OPpLVAL_INTRO|OPpPAD_STATE;
2494 o->op_flags |= OPf_MOD|OPf_REF;
2497 o->op_private |= OPpLVREF_AV;
2500 kid = cUNOPo->op_first;
2501 if (kid->op_type == OP_NULL)
2502 kid = cUNOPx(kUNOP->op_first->op_sibling)
2504 o->op_private = OPpLVREF_CV;
2505 if (kid->op_type == OP_GV)
2506 o->op_flags |= OPf_STACKED;
2507 else if (kid->op_type == OP_PADCV) {
2508 o->op_targ = kid->op_targ;
2510 op_free(cUNOPo->op_first);
2511 cUNOPo->op_first = NULL;
2512 o->op_flags &=~ OPf_KIDS;
2517 if (o->op_flags & OPf_PARENS) {
2519 yyerror(Perl_form(aTHX_ "Can't modify reference to "
2520 "parenthesized hash in list assignment"));
2523 o->op_private |= OPpLVREF_HV;
2527 if (cUNOPo->op_first->op_type != OP_GV) goto badref;
2528 o->op_flags |= OPf_STACKED;
2531 if (o->op_flags & OPf_PARENS) goto parenhash;
2532 o->op_private |= OPpLVREF_HV;
2535 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2538 PAD_COMPNAME_GEN_set(o->op_targ, PERL_INT_MAX);
2539 if (o->op_flags & OPf_PARENS) goto slurpy;
2540 o->op_private |= OPpLVREF_AV;
2544 o->op_private |= OPpLVREF_ELEM;
2545 o->op_flags |= OPf_STACKED;
2549 CHANGE_TYPE(o, OP_LVREFSLICE);
2550 o->op_private &= OPpLVAL_INTRO|OPpLVREF_ELEM;
2553 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2555 else if (!(o->op_flags & OPf_KIDS))
2557 if (o->op_targ != OP_LIST) {
2558 S_lvref(aTHX_ cBINOPo->op_first, type);
2563 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid)) {
2564 assert((kid->op_flags & OPf_WANT) != OPf_WANT_VOID);
2565 S_lvref(aTHX_ kid, type);
2569 if (o->op_flags & OPf_PARENS)
2574 /* diag_listed_as: Can't modify reference to %s in %s assignment */
2575 yyerror(Perl_form(aTHX_ "Can't modify reference to %s in %s",
2576 o->op_type == OP_NULL && o->op_flags & OPf_SPECIAL
2582 CHANGE_TYPE(o, OP_LVREF);
2584 OPpLVAL_INTRO|OPpLVREF_ELEM|OPpLVREF_TYPE|OPpPAD_STATE;
2585 if (type == OP_ENTERLOOP)
2586 o->op_private |= OPpLVREF_ITER;
2590 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2594 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2597 if (!o || (PL_parser && PL_parser->error_count))
2600 if ((o->op_private & OPpTARGET_MY)
2601 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2606 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2608 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2610 switch (o->op_type) {
2615 if ((o->op_flags & OPf_PARENS))
2619 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2620 !(o->op_flags & OPf_STACKED)) {
2621 CHANGE_TYPE(o, OP_RV2CV); /* entersub => rv2cv */
2622 assert(cUNOPo->op_first->op_type == OP_NULL);
2623 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2626 else { /* lvalue subroutine call */
2627 o->op_private |= OPpLVAL_INTRO;
2628 PL_modcount = RETURN_UNLIMITED_NUMBER;
2629 if (type == OP_GREPSTART || type == OP_ENTERSUB
2630 || type == OP_REFGEN || type == OP_LEAVESUBLV) {
2631 /* Potential lvalue context: */
2632 o->op_private |= OPpENTERSUB_INARGS;
2635 else { /* Compile-time error message: */
2636 OP *kid = cUNOPo->op_first;
2640 if (kid->op_type != OP_PUSHMARK) {
2641 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2643 "panic: unexpected lvalue entersub "
2644 "args: type/targ %ld:%"UVuf,
2645 (long)kid->op_type, (UV)kid->op_targ);
2646 kid = kLISTOP->op_first;
2648 while (OP_HAS_SIBLING(kid))
2649 kid = OP_SIBLING(kid);
2650 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2651 break; /* Postpone until runtime */
2654 kid = kUNOP->op_first;
2655 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2656 kid = kUNOP->op_first;
2657 if (kid->op_type == OP_NULL)
2659 "Unexpected constant lvalue entersub "
2660 "entry via type/targ %ld:%"UVuf,
2661 (long)kid->op_type, (UV)kid->op_targ);
2662 if (kid->op_type != OP_GV) {
2669 : SvROK(gv) && SvTYPE(SvRV(gv)) == SVt_PVCV
2670 ? MUTABLE_CV(SvRV(gv))
2681 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2682 /* grep, foreach, subcalls, refgen */
2683 if (type == OP_GREPSTART || type == OP_ENTERSUB
2684 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2686 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2687 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2689 : (o->op_type == OP_ENTERSUB
2690 ? "non-lvalue subroutine call"
2692 type ? PL_op_desc[type] : "local"));
2705 case OP_RIGHT_SHIFT:
2714 if (!(o->op_flags & OPf_STACKED))
2720 if (o->op_flags & OPf_STACKED) {
2724 if (!(o->op_private & OPpREPEAT_DOLIST))
2727 const I32 mods = PL_modcount;
2728 modkids(cBINOPo->op_first, type);
2729 if (type != OP_AASSIGN)
2731 kid = cBINOPo->op_last;
2732 if (kid->op_type == OP_CONST && SvIOK(kSVOP_sv)) {
2733 const IV iv = SvIV(kSVOP_sv);
2734 if (PL_modcount != RETURN_UNLIMITED_NUMBER)
2736 mods + (PL_modcount - mods) * (iv < 0 ? 0 : iv);
2739 PL_modcount = RETURN_UNLIMITED_NUMBER;
2745 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
2746 op_lvalue(kid, type);
2751 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2752 PL_modcount = RETURN_UNLIMITED_NUMBER;
2753 return o; /* Treat \(@foo) like ordinary list. */
2757 if (scalar_mod_type(o, type))
2759 ref(cUNOPo->op_first, o->op_type);
2766 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2767 if (type == OP_LEAVESUBLV && (
2768 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2769 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2771 o->op_private |= OPpMAYBE_LVSUB;
2775 PL_modcount = RETURN_UNLIMITED_NUMBER;
2779 if (type == OP_LEAVESUBLV)
2780 o->op_private |= OPpMAYBE_LVSUB;
2783 PL_hints |= HINT_BLOCK_SCOPE;
2784 if (type == OP_LEAVESUBLV)
2785 o->op_private |= OPpMAYBE_LVSUB;
2789 ref(cUNOPo->op_first, o->op_type);
2793 PL_hints |= HINT_BLOCK_SCOPE;
2803 case OP_AELEMFAST_LEX:
2810 PL_modcount = RETURN_UNLIMITED_NUMBER;
2811 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2812 return o; /* Treat \(@foo) like ordinary list. */
2813 if (scalar_mod_type(o, type))
2815 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2816 && type == OP_LEAVESUBLV)
2817 o->op_private |= OPpMAYBE_LVSUB;
2821 if (!type) /* local() */
2822 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2823 PAD_COMPNAME_SV(o->op_targ));
2824 if (!(o->op_private & OPpLVAL_INTRO)
2825 || ( type != OP_SASSIGN && type != OP_AASSIGN
2826 && PadnameIsSTATE(PAD_COMPNAME_SV(o->op_targ)) ))
2827 S_mark_padname_lvalue(aTHX_ PAD_COMPNAME_SV(o->op_targ));
2836 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2840 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2846 if (type == OP_LEAVESUBLV)
2847 o->op_private |= OPpMAYBE_LVSUB;
2848 if (o->op_flags & OPf_KIDS)
2849 op_lvalue(OP_SIBLING(cBINOPo->op_first), type);
2854 ref(cBINOPo->op_first, o->op_type);
2855 if (type == OP_ENTERSUB &&
2856 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2857 o->op_private |= OPpLVAL_DEFER;
2858 if (type == OP_LEAVESUBLV)
2859 o->op_private |= OPpMAYBE_LVSUB;
2866 o->op_private |= OPpLVALUE;
2872 if (o->op_flags & OPf_KIDS)
2873 op_lvalue(cLISTOPo->op_last, type);
2878 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2880 else if (!(o->op_flags & OPf_KIDS))
2882 if (o->op_targ != OP_LIST) {
2883 op_lvalue(cBINOPo->op_first, type);
2889 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
2890 /* elements might be in void context because the list is
2891 in scalar context or because they are attribute sub calls */
2892 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2893 op_lvalue(kid, type);
2901 if (type == OP_LEAVESUBLV
2902 || !S_vivifies(cLOGOPo->op_first->op_type))
2903 op_lvalue(cLOGOPo->op_first, type);
2904 if (type == OP_LEAVESUBLV
2905 || !S_vivifies(OP_SIBLING(cLOGOPo->op_first)->op_type))
2906 op_lvalue(OP_SIBLING(cLOGOPo->op_first), type);
2910 if (type != OP_AASSIGN && type != OP_SASSIGN
2911 && type != OP_ENTERLOOP)
2913 /* Don’t bother applying lvalue context to the ex-list. */
2914 kid = cUNOPx(cUNOPo->op_first)->op_first;
2915 assert (!OP_HAS_SIBLING(kid));
2918 if (type != OP_AASSIGN) goto nomod;
2919 kid = cUNOPo->op_first;
2922 const U8 ec = PL_parser ? PL_parser->error_count : 0;
2923 S_lvref(aTHX_ kid, type);
2924 if (!PL_parser || PL_parser->error_count == ec) {
2925 if (!FEATURE_REFALIASING_IS_ENABLED)
2927 "Experimental aliasing via reference not enabled");
2928 Perl_ck_warner_d(aTHX_
2929 packWARN(WARN_EXPERIMENTAL__REFALIASING),
2930 "Aliasing via reference is experimental");
2933 if (o->op_type == OP_REFGEN)
2934 op_null(cUNOPx(cUNOPo->op_first)->op_first); /* pushmark */
2939 kid = cLISTOPo->op_first;
2940 if (kid && kid->op_type == OP_PUSHRE &&
2942 || o->op_flags & OPf_STACKED
2944 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff
2946 || ((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv
2949 /* This is actually @array = split. */
2950 PL_modcount = RETURN_UNLIMITED_NUMBER;
2956 /* [20011101.069] File test operators interpret OPf_REF to mean that
2957 their argument is a filehandle; thus \stat(".") should not set
2959 if (type == OP_REFGEN &&
2960 PL_check[o->op_type] == Perl_ck_ftst)
2963 if (type != OP_LEAVESUBLV)
2964 o->op_flags |= OPf_MOD;
2966 if (type == OP_AASSIGN || type == OP_SASSIGN)
2967 o->op_flags |= OPf_SPECIAL|OPf_REF;
2968 else if (!type) { /* local() */
2971 o->op_private |= OPpLVAL_INTRO;
2972 o->op_flags &= ~OPf_SPECIAL;
2973 PL_hints |= HINT_BLOCK_SCOPE;
2978 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2979 "Useless localization of %s", OP_DESC(o));
2982 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2983 && type != OP_LEAVESUBLV)
2984 o->op_flags |= OPf_REF;
2989 S_scalar_mod_type(const OP *o, I32 type)
2994 if (o && o->op_type == OP_RV2GV)
3018 case OP_RIGHT_SHIFT:
3039 S_is_handle_constructor(const OP *o, I32 numargs)
3041 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
3043 switch (o->op_type) {
3051 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
3064 S_refkids(pTHX_ OP *o, I32 type)
3066 if (o && o->op_flags & OPf_KIDS) {
3068 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
3075 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
3080 PERL_ARGS_ASSERT_DOREF;
3082 if (!o || (PL_parser && PL_parser->error_count))
3085 switch (o->op_type) {
3087 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3088 !(o->op_flags & OPf_STACKED)) {
3089 CHANGE_TYPE(o, OP_RV2CV); /* entersub => rv2cv */
3090 assert(cUNOPo->op_first->op_type == OP_NULL);
3091 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
3092 o->op_flags |= OPf_SPECIAL;
3094 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
3095 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3096 : type == OP_RV2HV ? OPpDEREF_HV
3098 o->op_flags |= OPf_MOD;
3104 for (kid = OP_SIBLING(cUNOPo->op_first); kid; kid = OP_SIBLING(kid))
3105 doref(kid, type, set_op_ref);
3108 if (type == OP_DEFINED)
3109 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3110 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3113 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3114 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3115 : type == OP_RV2HV ? OPpDEREF_HV
3117 o->op_flags |= OPf_MOD;
3124 o->op_flags |= OPf_REF;
3127 if (type == OP_DEFINED)
3128 o->op_flags |= OPf_SPECIAL; /* don't create GV */
3129 doref(cUNOPo->op_first, o->op_type, set_op_ref);
3135 o->op_flags |= OPf_REF;
3140 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
3142 doref(cBINOPo->op_first, type, set_op_ref);
3146 doref(cBINOPo->op_first, o->op_type, set_op_ref);
3147 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
3148 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
3149 : type == OP_RV2HV ? OPpDEREF_HV
3151 o->op_flags |= OPf_MOD;
3161 if (!(o->op_flags & OPf_KIDS))
3163 doref(cLISTOPo->op_last, type, set_op_ref);
3173 S_dup_attrlist(pTHX_ OP *o)
3177 PERL_ARGS_ASSERT_DUP_ATTRLIST;
3179 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
3180 * where the first kid is OP_PUSHMARK and the remaining ones
3181 * are OP_CONST. We need to push the OP_CONST values.
3183 if (o->op_type == OP_CONST)
3184 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
3186 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
3188 for (o = cLISTOPo->op_first; o; o = OP_SIBLING(o)) {
3189 if (o->op_type == OP_CONST)
3190 rop = op_append_elem(OP_LIST, rop,
3191 newSVOP(OP_CONST, o->op_flags,
3192 SvREFCNT_inc_NN(cSVOPo->op_sv)));
3199 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
3201 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
3203 PERL_ARGS_ASSERT_APPLY_ATTRS;
3205 /* fake up C<use attributes $pkg,$rv,@attrs> */
3207 #define ATTRSMODULE "attributes"
3208 #define ATTRSMODULE_PM "attributes.pm"
3210 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3211 newSVpvs(ATTRSMODULE),
3213 op_prepend_elem(OP_LIST,
3214 newSVOP(OP_CONST, 0, stashsv),
3215 op_prepend_elem(OP_LIST,
3216 newSVOP(OP_CONST, 0,
3218 dup_attrlist(attrs))));
3222 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
3224 OP *pack, *imop, *arg;
3225 SV *meth, *stashsv, **svp;
3227 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
3232 assert(target->op_type == OP_PADSV ||
3233 target->op_type == OP_PADHV ||
3234 target->op_type == OP_PADAV);
3236 /* Ensure that attributes.pm is loaded. */
3237 /* Don't force the C<use> if we don't need it. */
3238 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
3239 if (svp && *svp != &PL_sv_undef)
3240 NOOP; /* already in %INC */
3242 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
3243 newSVpvs(ATTRSMODULE), NULL);
3245 /* Need package name for method call. */
3246 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
3248 /* Build up the real arg-list. */
3249 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
3251 arg = newOP(OP_PADSV, 0);
3252 arg->op_targ = target->op_targ;
3253 arg = op_prepend_elem(OP_LIST,
3254 newSVOP(OP_CONST, 0, stashsv),
3255 op_prepend_elem(OP_LIST,
3256 newUNOP(OP_REFGEN, 0,
3257 op_lvalue(arg, OP_REFGEN)),
3258 dup_attrlist(attrs)));
3260 /* Fake up a method call to import */
3261 meth = newSVpvs_share("import");
3262 imop = op_convert_list(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
3263 op_append_elem(OP_LIST,
3264 op_prepend_elem(OP_LIST, pack, arg),
3265 newMETHOP_named(OP_METHOD_NAMED, 0, meth)));
3267 /* Combine the ops. */
3268 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
3272 =notfor apidoc apply_attrs_string
3274 Attempts to apply a list of attributes specified by the C<attrstr> and
3275 C<len> arguments to the subroutine identified by the C<cv> argument which
3276 is expected to be associated with the package identified by the C<stashpv>
3277 argument (see L<attributes>). It gets this wrong, though, in that it
3278 does not correctly identify the boundaries of the individual attribute
3279 specifications within C<attrstr>. This is not really intended for the
3280 public API, but has to be listed here for systems such as AIX which
3281 need an explicit export list for symbols. (It's called from XS code
3282 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
3283 to respect attribute syntax properly would be welcome.
3289 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
3290 const char *attrstr, STRLEN len)
3294 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
3297 len = strlen(attrstr);
3301 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
3303 const char * const sstr = attrstr;
3304 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
3305 attrs = op_append_elem(OP_LIST, attrs,
3306 newSVOP(OP_CONST, 0,
3307 newSVpvn(sstr, attrstr-sstr)));
3311 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
3312 newSVpvs(ATTRSMODULE),
3313 NULL, op_prepend_elem(OP_LIST,
3314 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
3315 op_prepend_elem(OP_LIST,
3316 newSVOP(OP_CONST, 0,
3317 newRV(MUTABLE_SV(cv))),
3322 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
3324 OP *new_proto = NULL;
3329 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
3335 if (o->op_type == OP_CONST) {
3336 pv = SvPV(cSVOPo_sv, pvlen);
3337 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3338 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3339 SV ** const tmpo = cSVOPx_svp(o);
3340 SvREFCNT_dec(cSVOPo_sv);
3345 } else if (o->op_type == OP_LIST) {
3347 assert(o->op_flags & OPf_KIDS);
3348 lasto = cLISTOPo->op_first;
3349 assert(lasto->op_type == OP_PUSHMARK);
3350 for (o = OP_SIBLING(lasto); o; o = OP_SIBLING(o)) {
3351 if (o->op_type == OP_CONST) {
3352 pv = SvPV(cSVOPo_sv, pvlen);
3353 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
3354 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
3355 SV ** const tmpo = cSVOPx_svp(o);
3356 SvREFCNT_dec(cSVOPo_sv);
3358 if (new_proto && ckWARN(WARN_MISC)) {
3360 const char * newp = SvPV(cSVOPo_sv, new_len);
3361 Perl_warner(aTHX_ packWARN(WARN_MISC),
3362 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
3363 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
3369 /* excise new_proto from the list */
3370 op_sibling_splice(*attrs, lasto, 1, NULL);
3377 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
3378 would get pulled in with no real need */
3379 if (!OP_HAS_SIBLING(cLISTOPx(*attrs)->op_first)) {
3388 svname = sv_newmortal();
3389 gv_efullname3(svname, name, NULL);
3391 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
3392 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
3394 svname = (SV *)name;
3395 if (ckWARN(WARN_ILLEGALPROTO))
3396 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
3397 if (*proto && ckWARN(WARN_PROTOTYPE)) {
3398 STRLEN old_len, new_len;
3399 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
3400 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
3402 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
3403 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
3405 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
3406 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
3416 S_cant_declare(pTHX_ OP *o)
3418 if (o->op_type == OP_NULL
3419 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
3420 o = cUNOPo->op_first;
3421 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
3422 o->op_type == OP_NULL
3423 && o->op_flags & OPf_SPECIAL
3426 PL_parser->in_my == KEY_our ? "our" :
3427 PL_parser->in_my == KEY_state ? "state" :
3432 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
3435 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
3437 PERL_ARGS_ASSERT_MY_KID;
3439 if (!o || (PL_parser && PL_parser->error_count))
3444 if (type == OP_LIST) {
3446 for (kid = cLISTOPo->op_first; kid; kid = OP_SIBLING(kid))
3447 my_kid(kid, attrs, imopsp);
3449 } else if (type == OP_UNDEF || type == OP_STUB) {
3451 } else if (type == OP_RV2SV || /* "our" declaration */
3453 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
3454 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
3455 S_cant_declare(aTHX_ o);
3457 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
3459 PL_parser->in_my = FALSE;
3460 PL_parser->in_my_stash = NULL;
3461 apply_attrs(GvSTASH(gv),
3462 (type == OP_RV2SV ? GvSV(gv) :
3463 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
3464 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
3467 o->op_private |= OPpOUR_INTRO;
3470 else if (type != OP_PADSV &&
3473 type != OP_PUSHMARK)
3475 S_cant_declare(aTHX_ o);
3478 else if (attrs && type != OP_PUSHMARK) {
3482 PL_parser->in_my = FALSE;
3483 PL_parser->in_my_stash = NULL;
3485 /* check for C<my Dog $spot> when deciding package */
3486 stash = PAD_COMPNAME_TYPE(o->op_targ);
3488 stash = PL_curstash;
3489 apply_attrs_my(stash, o, attrs, imopsp);
3491 o->op_flags |= OPf_MOD;
3492 o->op_private |= OPpLVAL_INTRO;
3494 o->op_private |= OPpPAD_STATE;
3499 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
3502 int maybe_scalar = 0;
3504 PERL_ARGS_ASSERT_MY_ATTRS;
3506 /* [perl #17376]: this appears to be premature, and results in code such as
3507 C< our(%x); > executing in list mode rather than void mode */
3509 if (o->op_flags & OPf_PARENS)
3519 o = my_kid(o, attrs, &rops);
3521 if (maybe_scalar && o->op_type == OP_PADSV) {
3522 o = scalar(op_append_list(OP_LIST, rops, o));
3523 o->op_private |= OPpLVAL_INTRO;
3526 /* The listop in rops might have a pushmark at the beginning,
3527 which will mess up list assignment. */
3528 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
3529 if (rops->op_type == OP_LIST &&
3530 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
3532 OP * const pushmark = lrops->op_first;
3533 /* excise pushmark */
3534 op_sibling_splice(rops, NULL, 1, NULL);
3537 o = op_append_list(OP_LIST, o, rops);
3540 PL_parser->in_my = FALSE;
3541 PL_parser->in_my_stash = NULL;
3546 Perl_sawparens(pTHX_ OP *o)
3548 PERL_UNUSED_CONTEXT;
3550 o->op_flags |= OPf_PARENS;
3555 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
3559 const OPCODE ltype = left->op_type;
3560 const OPCODE rtype = right->op_type;
3562 PERL_ARGS_ASSERT_BIND_MATCH;
3564 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
3565 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
3567 const char * const desc
3569 rtype == OP_SUBST || rtype == OP_TRANS
3570 || rtype == OP_TRANSR
3572 ? (int)rtype : OP_MATCH];
3573 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
3575 S_op_varname(aTHX_ left);
3577 Perl_warner(aTHX_ packWARN(WARN_MISC),
3578 "Applying %s to %"SVf" will act on scalar(%"SVf")",
3579 desc, SVfARG(name), SVfARG(name));
3581 const char * const sample = (isary
3582 ? "@array" : "%hash");
3583 Perl_warner(aTHX_ packWARN(WARN_MISC),
3584 "Applying %s to %s will act on scalar(%s)",
3585 desc, sample, sample);
3589 if (rtype == OP_CONST &&
3590 cSVOPx(right)->op_private & OPpCONST_BARE &&
3591 cSVOPx(right)->op_private & OPpCONST_STRICT)
3593 no_bareword_allowed(right);
3596 /* !~ doesn't make sense with /r, so error on it for now */
3597 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3599 /* diag_listed_as: Using !~ with %s doesn't make sense */
3600 yyerror("Using !~ with s///r doesn't make sense");
3601 if (rtype == OP_TRANSR && type == OP_NOT)
3602 /* diag_listed_as: Using !~ with %s doesn't make sense */
3603 yyerror("Using !~ with tr///r doesn't make sense");
3605 ismatchop = (rtype == OP_MATCH ||
3606 rtype == OP_SUBST ||
3607 rtype == OP_TRANS || rtype == OP_TRANSR)
3608 && !(right->op_flags & OPf_SPECIAL);
3609 if (ismatchop && right->op_private & OPpTARGET_MY) {
3611 right->op_private &= ~OPpTARGET_MY;
3613 if (!(right->op_flags & OPf_STACKED) && !right->op_targ && ismatchop) {
3614 if (left->op_type == OP_PADSV
3615 && !(left->op_private & OPpLVAL_INTRO))
3617 right->op_targ = left->op_targ;
3622 right->op_flags |= OPf_STACKED;
3623 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3624 ! (rtype == OP_TRANS &&
3625 right->op_private & OPpTRANS_IDENTICAL) &&
3626 ! (rtype == OP_SUBST &&
3627 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3628 left = op_lvalue(left, rtype);
3629 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3630 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
3632 o = op_prepend_elem(rtype, scalar(left), right);
3635 return newUNOP(OP_NOT, 0, scalar(o));
3639 return bind_match(type, left,
3640 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
3644 Perl_invert(pTHX_ OP *o)
3648 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3652 =for apidoc Amx|OP *|op_scope|OP *o
3654 Wraps up an op tree with some additional ops so that at runtime a dynamic
3655 scope will be created. The original ops run in the new dynamic scope,
3656 and then, provided that they exit normally, the scope will be unwound.
3657 The additional ops used to create and unwind the dynamic scope will
3658 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3659 instead if the ops are simple enough to not need the full dynamic scope
3666 Perl_op_scope(pTHX_ OP *o)
3670 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3671 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3672 CHANGE_TYPE(o, OP_LEAVE);
3674 else if (o->op_type == OP_LINESEQ) {
3676 CHANGE_TYPE(o, OP_SCOPE);
3677 kid = ((LISTOP*)o)->op_first;
3678 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3681 /* The following deals with things like 'do {1 for 1}' */
3682 kid = OP_SIBLING(kid);
3684 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3689 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3695 Perl_op_unscope(pTHX_ OP *o)
3697 if (o && o->op_type == OP_LINESEQ) {
3698 OP *kid = cLISTOPo->op_first;
3699 for(; kid; kid = OP_SIBLING(kid))
3700 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3707 =for apidoc Am|int|block_start|int full
3709 Handles compile-time scope entry.
3710 Arranges for hints to be restored on block
3711 exit and also handles pad sequence numbers to make lexical variables scope
3712 right. Returns a savestack index for use with C<block_end>.
3718 Perl_block_start(pTHX_ int full)
3720 const int retval = PL_savestack_ix;
3722 PL_compiling.cop_seq = PL_cop_seqmax;
3724 pad_block_start(full);
3726 PL_hints &= ~HINT_BLOCK_SCOPE;
3727 SAVECOMPILEWARNINGS();
3728 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3729 SAVEI32(PL_compiling.cop_seq);
3730 PL_compiling.cop_seq = 0;
3732 CALL_BLOCK_HOOKS(bhk_start, full);
3738 =for apidoc Am|OP *|block_end|I32 floor|OP *seq
3740 Handles compile-time scope exit. I<floor>
3741 is the savestack index returned by
3742 C<block_start>, and I<seq> is the body of the block. Returns the block,
3749 Perl_block_end(pTHX_ I32 floor, OP *seq)
3751 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3752 OP* retval = scalarseq(seq);
3755 /* XXX Is the null PL_parser check necessary here? */
3756 assert(PL_parser); /* Let’s find out under debugging builds. */
3757 if (PL_parser && PL_parser->parsed_sub) {
3758 o = newSTATEOP(0, NULL, NULL);
3760 retval = op_append_elem(OP_LINESEQ, retval, o);
3763 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3767 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3771 /* pad_leavemy has created a sequence of introcv ops for all my
3772 subs declared in the block. We have to replicate that list with
3773 clonecv ops, to deal with this situation:
3778 sub s1 { state sub foo { \&s2 } }
3781 Originally, I was going to have introcv clone the CV and turn
3782 off the stale flag. Since &s1 is declared before &s2, the
3783 introcv op for &s1 is executed (on sub entry) before the one for
3784 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3785 cloned, since it is a state sub) closes over &s2 and expects
3786 to see it in its outer CV’s pad. If the introcv op clones &s1,
3787 then &s2 is still marked stale. Since &s1 is not active, and
3788 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3789 ble will not stay shared’ warning. Because it is the same stub
3790 that will be used when the introcv op for &s2 is executed, clos-
3791 ing over it is safe. Hence, we have to turn off the stale flag
3792 on all lexical subs in the block before we clone any of them.
3793 Hence, having introcv clone the sub cannot work. So we create a
3794 list of ops like this:
3818 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3819 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3820 for (;; kid = OP_SIBLING(kid)) {
3821 OP *newkid = newOP(OP_CLONECV, 0);
3822 newkid->op_targ = kid->op_targ;
3823 o = op_append_elem(OP_LINESEQ, o, newkid);
3824 if (kid == last) break;
3826 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3829 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3835 =head1 Compile-time scope hooks
3837 =for apidoc Aox||blockhook_register
3839 Register a set of hooks to be called when the Perl lexical scope changes
3840 at compile time. See L<perlguts/"Compile-time scope hooks">.
3846 Perl_blockhook_register(pTHX_ BHK *hk)
3848 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3850 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3854 Perl_newPROG(pTHX_ OP *o)
3856 PERL_ARGS_ASSERT_NEWPROG;
3863 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3864 ((PL_in_eval & EVAL_KEEPERR)
3865 ? OPf_SPECIAL : 0), o);
3867 cx = &cxstack[cxstack_ix];
3868 assert(CxTYPE(cx) == CXt_EVAL);
3870 if ((cx->blk_gimme & G_WANT) == G_VOID)
3871 scalarvoid(PL_eval_root);
3872 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3875 scalar(PL_eval_root);
3877 PL_eval_start = op_linklist(PL_eval_root);
3878 PL_eval_root->op_private |= OPpREFCOUNTED;
3879 OpREFCNT_set(PL_eval_root, 1);
3880 PL_eval_root->op_next = 0;
3881 i = PL_savestack_ix;
3884 CALL_PEEP(PL_eval_start);
3885 finalize_optree(PL_eval_root);
3886 S_prune_chain_head(&PL_eval_start);
3888 PL_savestack_ix = i;
3891 if (o->op_type == OP_STUB) {
3892 /* This block is entered if nothing is compiled for the main
3893 program. This will be the case for an genuinely empty main
3894 program, or one which only has BEGIN blocks etc, so already
3897 Historically (5.000) the guard above was !o. However, commit
3898 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3899 c71fccf11fde0068, changed perly.y so that newPROG() is now
3900 called with the output of block_end(), which returns a new
3901 OP_STUB for the case of an empty optree. ByteLoader (and
3902 maybe other things) also take this path, because they set up
3903 PL_main_start and PL_main_root directly, without generating an
3906 If the parsing the main program aborts (due to parse errors,
3907 or due to BEGIN or similar calling exit), then newPROG()
3908 isn't even called, and hence this code path and its cleanups
3909 are skipped. This shouldn't make a make a difference:
3910 * a non-zero return from perl_parse is a failure, and
3911 perl_destruct() should be called immediately.
3912 * however, if exit(0) is called during the parse, then
3913 perl_parse() returns 0, and perl_run() is called. As
3914 PL_main_start will be NULL, perl_run() will return
3915 promptly, and the exit code will remain 0.
3918 PL_comppad_name = 0;
3920 S_op_destroy(aTHX_ o);
3923 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3924 PL_curcop = &PL_compiling;
3925 PL_main_start = LINKLIST(PL_main_root);
3926 PL_main_root->op_private |= OPpREFCOUNTED;
3927 OpREFCNT_set(PL_main_root, 1);
3928 PL_main_root->op_next = 0;
3929 CALL_PEEP(PL_main_start);
3930 finalize_optree(PL_main_root);
3931 S_prune_chain_head(&PL_main_start);
3932 cv_forget_slab(PL_compcv);
3935 /* Register with debugger */
3937 CV * const cv = get_cvs("DB::postponed", 0);
3941 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3943 call_sv(MUTABLE_SV(cv), G_DISCARD);
3950 Perl_localize(pTHX_ OP *o, I32 lex)
3952 PERL_ARGS_ASSERT_LOCALIZE;
3954 if (o->op_flags & OPf_PARENS)
3955 /* [perl #17376]: this appears to be premature, and results in code such as
3956 C< our(%x); > executing in list mode rather than void mode */
3963 if ( PL_parser->bufptr > PL_parser->oldbufptr
3964 && PL_parser->bufptr[-1] == ','
3965 && ckWARN(WARN_PARENTHESIS))
3967 char *s = PL_parser->bufptr;
3970 /* some heuristics to detect a potential error */
3971 while (*s && (strchr(", \t\n", *s)))
3975 if (*s && strchr("@$%*", *s) && *++s
3976 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
3979 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
3981 while (*s && (strchr(", \t\n", *s)))
3987 if (sigil && (*s == ';' || *s == '=')) {
3988 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3989 "Parentheses missing around \"%s\" list",
3991 ? (PL_parser->in_my == KEY_our
3993 : PL_parser->in_my == KEY_state
4003 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
4004 PL_parser->in_my = FALSE;
4005 PL_parser->in_my_stash = NULL;
4010 Perl_jmaybe(pTHX_ OP *o)
4012 PERL_ARGS_ASSERT_JMAYBE;
4014 if (o->op_type == OP_LIST) {
4016 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
4017 o = op_convert_list(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
4022 PERL_STATIC_INLINE OP *
4023 S_op_std_init(pTHX_ OP *o)
4025 I32 type = o->op_type;
4027 PERL_ARGS_ASSERT_OP_STD_INIT;
4029 if (PL_opargs[type] & OA_RETSCALAR)
4031 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
4032 o->op_targ = pad_alloc(type, SVs_PADTMP);
4037 PERL_STATIC_INLINE OP *
4038 S_op_integerize(pTHX_ OP *o)
4040 I32 type = o->op_type;
4042 PERL_ARGS_ASSERT_OP_INTEGERIZE;
4044 /* integerize op. */
4045 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
4048 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
4051 if (type == OP_NEGATE)
4052 /* XXX might want a ck_negate() for this */
4053 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
4059 S_fold_constants(pTHX_ OP *o)
4064 VOL I32 type = o->op_type;
4070 SV * const oldwarnhook = PL_warnhook;
4071 SV * const olddiehook = PL_diehook;
4073 U8 oldwarn = PL_dowarn;
4076 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
4078 if (!(PL_opargs[type] & OA_FOLDCONST))
4087 #ifdef USE_LOCALE_CTYPE
4088 if (IN_LC_COMPILETIME(LC_CTYPE))
4097 #ifdef USE_LOCALE_COLLATE
4098 if (IN_LC_COMPILETIME(LC_COLLATE))
4103 /* XXX what about the numeric ops? */
4104 #ifdef USE_LOCALE_NUMERIC
4105 if (IN_LC_COMPILETIME(LC_NUMERIC))
4110 if (!OP_HAS_SIBLING(cLISTOPo->op_first)
4111 || OP_SIBLING(cLISTOPo->op_first)->op_type != OP_CONST)
4114 SV * const sv = cSVOPx_sv(OP_SIBLING(cLISTOPo->op_first));
4115 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
4117 const char *s = SvPVX_const(sv);
4118 while (s < SvEND(sv)) {
4119 if (isALPHA_FOLD_EQ(*s, 'p')) goto nope;
4126 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
4129 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
4130 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
4134 if (PL_parser && PL_parser->error_count)
4135 goto nope; /* Don't try to run w/ errors */
4137 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
4138 const OPCODE type = curop->op_type;
4139 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
4141 type != OP_SCALAR &&
4143 type != OP_PUSHMARK)
4149 curop = LINKLIST(o);
4150 old_next = o->op_next;
4154 oldscope = PL_scopestack_ix;
4155 create_eval_scope(G_FAKINGEVAL);
4157 /* Verify that we don't need to save it: */
4158 assert(PL_curcop == &PL_compiling);
4159 StructCopy(&PL_compiling, ¬_compiling, COP);
4160 PL_curcop = ¬_compiling;
4161 /* The above ensures that we run with all the correct hints of the
4162 currently compiling COP, but that IN_PERL_RUNTIME is true. */
4163 assert(IN_PERL_RUNTIME);
4164 PL_warnhook = PERL_WARNHOOK_FATAL;
4168 /* Effective $^W=1. */
4169 if ( ! (PL_dowarn & G_WARN_ALL_MASK))
4170 PL_dowarn |= G_WARN_ON;
4175 sv = *(PL_stack_sp--);
4176 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
4177 pad_swipe(o->op_targ, FALSE);
4179 else if (SvTEMP(sv)) { /* grab mortal temp? */
4180 SvREFCNT_inc_simple_void(sv);
4183 else { assert(SvIMMORTAL(sv)); }
4186 /* Something tried to die. Abandon constant folding. */
4187 /* Pretend the error never happened. */
4189 o->op_next = old_next;
4193 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
4194 PL_warnhook = oldwarnhook;
4195 PL_diehook = olddiehook;
4196 /* XXX note that this croak may fail as we've already blown away
4197 * the stack - eg any nested evals */
4198 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
4201 PL_dowarn = oldwarn;
4202 PL_warnhook = oldwarnhook;
4203 PL_diehook = olddiehook;
4204 PL_curcop = &PL_compiling;
4206 if (PL_scopestack_ix > oldscope)
4207 delete_eval_scope();
4212 folded = cBOOL(o->op_folded);
4215 if (type == OP_STRINGIFY) SvPADTMP_off(sv);
4216 else if (!SvIMMORTAL(sv)) {
4220 if (type == OP_RV2GV)
4221 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
4224 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
4225 /* OP_STRINGIFY and constant folding are used to implement qq.
4226 Here the constant folding is an implementation detail that we
4227 want to hide. If the stringify op is itself already marked
4228 folded, however, then it is actually a folded join. */
4229 if (type != OP_STRINGIFY || folded) newop->op_folded = 1;
4238 S_gen_constant_list(pTHX_ OP *o)
4242 const SSize_t oldtmps_floor = PL_tmps_floor;
4247 if (PL_parser && PL_parser->error_count)
4248 return o; /* Don't attempt to run with errors */
4250 curop = LINKLIST(o);
4253 S_prune_chain_head(&curop);
4255 Perl_pp_pushmark(aTHX);
4258 assert (!(curop->op_flags & OPf_SPECIAL));
4259 assert(curop->op_type == OP_RANGE);
4260 Perl_pp_anonlist(aTHX);
4261 PL_tmps_floor = oldtmps_floor;
4263 CHANGE_TYPE(o, OP_RV2AV);
4264 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
4265 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
4266 o->op_opt = 0; /* needs to be revisited in rpeep() */
4267 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
4269 /* replace subtree with an OP_CONST */
4270 curop = ((UNOP*)o)->op_first;
4271 op_sibling_splice(o, NULL, -1, newSVOP(OP_CONST, 0, (SV *)av));
4274 if (AvFILLp(av) != -1)
4275 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
4278 SvREADONLY_on(*svp);
4285 =head1 Optree Manipulation Functions
4288 /* List constructors */
4291 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
4293 Append an item to the list of ops contained directly within a list-type
4294 op, returning the lengthened list. I<first> is the list-type op,
4295 and I<last> is the op to append to the list. I<optype> specifies the
4296 intended opcode for the list. If I<first> is not already a list of the
4297 right type, it will be upgraded into one. If either I<first> or I<last>
4298 is null, the other is returned unchanged.
4304 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
4312 if (first->op_type != (unsigned)type
4313 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
4315 return newLISTOP(type, 0, first, last);
4318 op_sibling_splice(first, ((LISTOP*)first)->op_last, 0, last);
4319 first->op_flags |= OPf_KIDS;
4324 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
4326 Concatenate the lists of ops contained directly within two list-type ops,
4327 returning the combined list. I<first> and I<last> are the list-type ops
4328 to concatenate. I<optype> specifies the intended opcode for the list.
4329 If either I<first> or I<last> is not already a list of the right type,
4330 it will be upgraded into one. If either I<first> or I<last> is null,
4331 the other is returned unchanged.
4337 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
4345 if (first->op_type != (unsigned)type)
4346 return op_prepend_elem(type, first, last);
4348 if (last->op_type != (unsigned)type)
4349 return op_append_elem(type, first, last);
4351 ((LISTOP*)first)->op_last->op_lastsib = 0;
4352 OP_SIBLING_set(((LISTOP*)first)->op_last, ((LISTOP*)last)->op_first);
4353 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
4354 ((LISTOP*)first)->op_last->op_lastsib = 1;
4355 #ifdef PERL_OP_PARENT
4356 ((LISTOP*)first)->op_last->op_sibling = first;
4358 first->op_flags |= (last->op_flags & OPf_KIDS);
4361 S_op_destroy(aTHX_ last);
4367 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
4369 Prepend an item to the list of ops contained directly within a list-type
4370 op, returning the lengthened list. I<first> is the op to prepend to the
4371 list, and I<last> is the list-type op. I<optype> specifies the intended
4372 opcode for the list. If I<last> is not already a list of the right type,
4373 it will be upgraded into one. If either I<first> or I<last> is null,
4374 the other is returned unchanged.
4380 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
4388 if (last->op_type == (unsigned)type) {
4389 if (type == OP_LIST) { /* already a PUSHMARK there */
4390 /* insert 'first' after pushmark */
4391 op_sibling_splice(last, cLISTOPx(last)->op_first, 0, first);
4392 if (!(first->op_flags & OPf_PARENS))
4393 last->op_flags &= ~OPf_PARENS;
4396 op_sibling_splice(last, NULL, 0, first);
4397 last->op_flags |= OPf_KIDS;
4401 return newLISTOP(type, 0, first, last);
4405 =for apidoc Am|OP *|op_convert_list|I32 type|I32 flags|OP *o
4407 Converts I<o> into a list op if it is not one already, and then converts it
4408 into the specified I<type>, calling its check function, allocating a target if
4409 it needs one, and folding constants.
4411 A list-type op is usually constructed one kid at a time via C<newLISTOP>,
4412 C<op_prepend_elem> and C<op_append_elem>. Then finally it is passed to
4413 C<op_convert> to make it the right type.
4419 Perl_op_convert_list(pTHX_ I32 type, I32 flags, OP *o)
4422 if (type < 0) type = -type, flags |= OPf_SPECIAL;
4423 if (!o || o->op_type != OP_LIST)
4424 o = force_list(o, 0);
4426 o->op_flags &= ~OPf_WANT;
4428 if (!(PL_opargs[type] & OA_MARK))
4429 op_null(cLISTOPo->op_first);
4431 OP * const kid2 = OP_SIBLING(cLISTOPo->op_first);
4432 if (kid2 && kid2->op_type == OP_COREARGS) {
4433 op_null(cLISTOPo->op_first);
4434 kid2->op_private |= OPpCOREARGS_PUSHMARK;
4438 CHANGE_TYPE(o, type);
4439 o->op_flags |= flags;
4441 o = CHECKOP(type, o);
4442 if (o->op_type != (unsigned)type)
4445 return fold_constants(op_integerize(op_std_init(o)));
4452 =head1 Optree construction
4454 =for apidoc Am|OP *|newNULLLIST
4456 Constructs, checks, and returns a new C<stub> op, which represents an
4457 empty list expression.
4463 Perl_newNULLLIST(pTHX)
4465 return newOP(OP_STUB, 0);
4468 /* promote o and any siblings to be a list if its not already; i.e.
4476 * pushmark - o - A - B
4478 * If nullit it true, the list op is nulled.
4482 S_force_list(pTHX_ OP *o, bool nullit)
4484 if (!o || o->op_type != OP_LIST) {
4487 /* manually detach any siblings then add them back later */
4488 rest = OP_SIBLING(o);
4489 OP_SIBLING_set(o, NULL);
4492 o = newLISTOP(OP_LIST, 0, o, NULL);
4494 op_sibling_splice(o, cLISTOPo->op_last, 0, rest);
4502 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
4504 Constructs, checks, and returns an op of any list type. I<type> is
4505 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4506 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
4507 supply up to two ops to be direct children of the list op; they are
4508 consumed by this function and become part of the constructed op tree.
4514 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4519 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
4521 NewOp(1101, listop, 1, LISTOP);
4523 CHANGE_TYPE(listop, type);
4526 listop->op_flags = (U8)flags;
4530 else if (!first && last)
4533 OP_SIBLING_set(first, last);
4534 listop->op_first = first;
4535 listop->op_last = last;
4536 if (type == OP_LIST) {
4537 OP* const pushop = newOP(OP_PUSHMARK, 0);
4538 pushop->op_lastsib = 0;
4539 OP_SIBLING_set(pushop, first);
4540 listop->op_first = pushop;
4541 listop->op_flags |= OPf_KIDS;
4543 listop->op_last = pushop;
4546 first->op_lastsib = 0;
4547 if (listop->op_last) {
4548 listop->op_last->op_lastsib = 1;
4549 #ifdef PERL_OP_PARENT
4550 listop->op_last->op_sibling = (OP*)listop;
4554 return CHECKOP(type, listop);
4558 =for apidoc Am|OP *|newOP|I32 type|I32 flags
4560 Constructs, checks, and returns an op of any base type (any type that
4561 has no extra fields). I<type> is the opcode. I<flags> gives the
4562 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
4569 Perl_newOP(pTHX_ I32 type, I32 flags)
4574 if (type == -OP_ENTEREVAL) {
4575 type = OP_ENTEREVAL;
4576 flags |= OPpEVAL_BYTES<<8;
4579 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
4580 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4581 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4582 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4584 NewOp(1101, o, 1, OP);
4585 CHANGE_TYPE(o, type);
4586 o->op_flags = (U8)flags;
4589 o->op_private = (U8)(0 | (flags >> 8));
4590 if (PL_opargs[type] & OA_RETSCALAR)
4592 if (PL_opargs[type] & OA_TARGET)
4593 o->op_targ = pad_alloc(type, SVs_PADTMP);
4594 return CHECKOP(type, o);
4598 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
4600 Constructs, checks, and returns an op of any unary type. I<type> is
4601 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
4602 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
4603 bits, the eight bits of C<op_private>, except that the bit with value 1
4604 is automatically set. I<first> supplies an optional op to be the direct
4605 child of the unary op; it is consumed by this function and become part
4606 of the constructed op tree.
4612 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
4617 if (type == -OP_ENTEREVAL) {
4618 type = OP_ENTEREVAL;
4619 flags |= OPpEVAL_BYTES<<8;
4622 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
4623 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
4624 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
4625 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
4626 || type == OP_SASSIGN
4627 || type == OP_ENTERTRY
4628 || type == OP_NULL );
4631 first = newOP(OP_STUB, 0);
4632 if (PL_opargs[type] & OA_MARK)
4633 first = force_list(first, 1);
4635 NewOp(1101, unop, 1, UNOP);
4636 CHANGE_TYPE(unop, type);
4637 unop->op_first = first;
4638 unop->op_flags = (U8)(flags | OPf_KIDS);
4639 unop->op_private = (U8)(1 | (flags >> 8));
4641 #ifdef PERL_OP_PARENT
4642 if (!OP_HAS_SIBLING(first)) /* true unless weird syntax error */
4643 first->op_sibling = (OP*)unop;
4646 unop = (UNOP*) CHECKOP(type, unop);
4650 return fold_constants(op_integerize(op_std_init((OP *) unop)));
4654 =for apidoc Am|OP *|newMETHOP|I32 type|I32 flags|OP *first
4656 Constructs, checks, and returns an op of method type with a method name
4657 evaluated at runtime. I<type> is the opcode. I<flags> gives the eight
4658 bits of C<op_flags>, except that C<OPf_KIDS> will be set automatically,
4659 and, shifted up eight bits, the eight bits of C<op_private>, except that
4660 the bit with value 1 is automatically set. I<dynamic_meth> supplies an
4661 op which evaluates method name; it is consumed by this function and
4662 become part of the constructed op tree.
4663 Supported optypes: OP_METHOD.
4669 S_newMETHOP_internal(pTHX_ I32 type, I32 flags, OP* dynamic_meth, SV* const_meth) {
4673 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_METHOP);
4675 NewOp(1101, methop, 1, METHOP);
4677 if (PL_opargs[type] & OA_MARK) dynamic_meth = force_list(dynamic_meth, 1);
4678 methop->op_flags = (U8)(flags | OPf_KIDS);
4679 methop->op_u.op_first = dynamic_meth;
4680 methop->op_private = (U8)(1 | (flags >> 8));
4682 #ifdef PERL_OP_PARENT
4683 if (!OP_HAS_SIBLING(dynamic_meth))
4684 dynamic_meth->op_sibling = (OP*)methop;
4689 methop->op_flags = (U8)(flags & ~OPf_KIDS);
4690 methop->op_u.op_meth_sv = const_meth;
4691 methop->op_private = (U8)(0 | (flags >> 8));
4692 methop->op_next = (OP*)methop;
4696 methop->op_class_targ = 0;
4698 methop->op_class_sv = NULL;
4700 CHANGE_TYPE(methop, type);
4701 methop = (METHOP*) CHECKOP(type, methop);
4703 if (methop->op_next) return (OP*)methop;
4705 return fold_constants(op_integerize(op_std_init((OP *) methop)));
4709 Perl_newMETHOP (pTHX_ I32 type, I32 flags, OP* dynamic_meth) {
4710 PERL_ARGS_ASSERT_NEWMETHOP;
4711 return newMETHOP_internal(type, flags, dynamic_meth, NULL);
4715 =for apidoc Am|OP *|newMETHOP_named|I32 type|I32 flags|SV *const_meth
4717 Constructs, checks, and returns an op of method type with a constant
4718 method name. I<type> is the opcode. I<flags> gives the eight bits of
4719 C<op_flags>, and, shifted up eight bits, the eight bits of
4720 C<op_private>. I<const_meth> supplies a constant method name;
4721 it must be a shared COW string.
4722 Supported optypes: OP_METHOD_NAMED.
4728 Perl_newMETHOP_named (pTHX_ I32 type, I32 flags, SV* const_meth) {
4729 PERL_ARGS_ASSERT_NEWMETHOP_NAMED;
4730 return newMETHOP_internal(type, flags, NULL, const_meth);
4734 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
4736 Constructs, checks, and returns an op of any binary type. I<type>
4737 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
4738 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
4739 the eight bits of C<op_private>, except that the bit with value 1 or
4740 2 is automatically set as required. I<first> and I<last> supply up to
4741 two ops to be the direct children of the binary op; they are consumed
4742 by this function and become part of the constructed op tree.
4748 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4753 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4754 || type == OP_SASSIGN || type == OP_NULL );
4756 NewOp(1101, binop, 1, BINOP);
4759 first = newOP(OP_NULL, 0);
4761 CHANGE_TYPE(binop, type);
4762 binop->op_first = first;
4763 binop->op_flags = (U8)(flags | OPf_KIDS);
4766 binop->op_private = (U8)(1 | (flags >> 8));
4769 binop->op_private = (U8)(2 | (flags >> 8));
4770 OP_SIBLING_set(first, last);
4771 first->op_lastsib = 0;
4774 #ifdef PERL_OP_PARENT
4775 if (!OP_HAS_SIBLING(last)) /* true unless weird syntax error */
4776 last->op_sibling = (OP*)binop;
4779 binop->op_last = OP_SIBLING(binop->op_first);
4780 #ifdef PERL_OP_PARENT
4782 binop->op_last->op_sibling = (OP*)binop;
4785 binop = (BINOP*)CHECKOP(type, binop);
4786 if (binop->op_next || binop->op_type != (OPCODE)type)
4789 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4792 static int uvcompare(const void *a, const void *b)
4793 __attribute__nonnull__(1)
4794 __attribute__nonnull__(2)
4795 __attribute__pure__;
4796 static int uvcompare(const void *a, const void *b)
4798 if (*((const UV *)a) < (*(const UV *)b))
4800 if (*((const UV *)a) > (*(const UV *)b))
4802 if (*((const UV *)a+1) < (*(const UV *)b+1))
4804 if (*((const UV *)a+1) > (*(const UV *)b+1))
4810 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4812 SV * const tstr = ((SVOP*)expr)->op_sv;
4814 ((SVOP*)repl)->op_sv;
4817 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4818 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4824 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4825 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4826 I32 del = o->op_private & OPpTRANS_DELETE;
4829 PERL_ARGS_ASSERT_PMTRANS;
4831 PL_hints |= HINT_BLOCK_SCOPE;
4834 o->op_private |= OPpTRANS_FROM_UTF;
4837 o->op_private |= OPpTRANS_TO_UTF;
4839 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4840 SV* const listsv = newSVpvs("# comment\n");
4842 const U8* tend = t + tlen;
4843 const U8* rend = r + rlen;
4859 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;