4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
112 /* remove any leading "empty" ops from the op_next chain whose first
113 * node's address is stored in op_p. Store the updated address of the
114 * first node in op_p.
118 S_prune_chain_head(OP** op_p)
121 && ( (*op_p)->op_type == OP_NULL
122 || (*op_p)->op_type == OP_SCOPE
123 || (*op_p)->op_type == OP_SCALAR
124 || (*op_p)->op_type == OP_LINESEQ)
126 *op_p = (*op_p)->op_next;
130 /* See the explanatory comments above struct opslab in op.h. */
132 #ifdef PERL_DEBUG_READONLY_OPS
133 # define PERL_SLAB_SIZE 128
134 # define PERL_MAX_SLAB_SIZE 4096
135 # include <sys/mman.h>
138 #ifndef PERL_SLAB_SIZE
139 # define PERL_SLAB_SIZE 64
141 #ifndef PERL_MAX_SLAB_SIZE
142 # define PERL_MAX_SLAB_SIZE 2048
145 /* rounds up to nearest pointer */
146 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
147 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
150 S_new_slab(pTHX_ size_t sz)
152 #ifdef PERL_DEBUG_READONLY_OPS
153 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
154 PROT_READ|PROT_WRITE,
155 MAP_ANON|MAP_PRIVATE, -1, 0);
156 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
157 (unsigned long) sz, slab));
158 if (slab == MAP_FAILED) {
159 perror("mmap failed");
162 slab->opslab_size = (U16)sz;
164 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
167 /* The context is unused in non-Windows */
170 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
174 /* requires double parens and aTHX_ */
175 #define DEBUG_S_warn(args) \
177 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
181 Perl_Slab_Alloc(pTHX_ size_t sz)
190 /* We only allocate ops from the slab during subroutine compilation.
191 We find the slab via PL_compcv, hence that must be non-NULL. It could
192 also be pointing to a subroutine which is now fully set up (CvROOT()
193 pointing to the top of the optree for that sub), or a subroutine
194 which isn't using the slab allocator. If our sanity checks aren't met,
195 don't use a slab, but allocate the OP directly from the heap. */
196 if (!PL_compcv || CvROOT(PL_compcv)
197 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
198 return PerlMemShared_calloc(1, sz);
200 /* While the subroutine is under construction, the slabs are accessed via
201 CvSTART(), to avoid needing to expand PVCV by one pointer for something
202 unneeded at runtime. Once a subroutine is constructed, the slabs are
203 accessed via CvROOT(). So if CvSTART() is NULL, no slab has been
204 allocated yet. See the commit message for 8be227ab5eaa23f2 for more
206 if (!CvSTART(PL_compcv)) {
208 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
209 CvSLABBED_on(PL_compcv);
210 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
212 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
214 opsz = SIZE_TO_PSIZE(sz);
215 sz = opsz + OPSLOT_HEADER_P;
217 /* The slabs maintain a free list of OPs. In particular, constant folding
218 will free up OPs, so it makes sense to re-use them where possible. A
219 freed up slot is used in preference to a new allocation. */
220 if (slab->opslab_freed) {
221 OP **too = &slab->opslab_freed;
223 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", (void*)o, (void*)slab));
224 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
225 DEBUG_S_warn((aTHX_ "Alas! too small"));
226 o = *(too = &o->op_next);
227 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", (void*)o)); }
231 Zero(o, opsz, I32 *);
237 #define INIT_OPSLOT \
238 slot->opslot_slab = slab; \
239 slot->opslot_next = slab2->opslab_first; \
240 slab2->opslab_first = slot; \
241 o = &slot->opslot_op; \
244 /* The partially-filled slab is next in the chain. */
245 slab2 = slab->opslab_next ? slab->opslab_next : slab;
246 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
247 /* Remaining space is too small. */
249 /* If we can fit a BASEOP, add it to the free chain, so as not
251 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
252 slot = &slab2->opslab_slots;
254 o->op_type = OP_FREED;
255 o->op_next = slab->opslab_freed;
256 slab->opslab_freed = o;
259 /* Create a new slab. Make this one twice as big. */
260 slot = slab2->opslab_first;
261 while (slot->opslot_next) slot = slot->opslot_next;
262 slab2 = S_new_slab(aTHX_
263 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
265 : (DIFF(slab2, slot)+1)*2);
266 slab2->opslab_next = slab->opslab_next;
267 slab->opslab_next = slab2;
269 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
271 /* Create a new op slot */
272 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
273 assert(slot >= &slab2->opslab_slots);
274 if (DIFF(&slab2->opslab_slots, slot)
275 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
276 slot = &slab2->opslab_slots;
278 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", (void*)o, (void*)slab));
284 #ifdef PERL_DEBUG_READONLY_OPS
286 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
288 PERL_ARGS_ASSERT_SLAB_TO_RO;
290 if (slab->opslab_readonly) return;
291 slab->opslab_readonly = 1;
292 for (; slab; slab = slab->opslab_next) {
293 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
294 (unsigned long) slab->opslab_size, slab));*/
295 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
296 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
297 (unsigned long)slab->opslab_size, errno);
302 Perl_Slab_to_rw(pTHX_ OPSLAB *const slab)
306 PERL_ARGS_ASSERT_SLAB_TO_RW;
308 if (!slab->opslab_readonly) return;
310 for (; slab2; slab2 = slab2->opslab_next) {
311 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
312 (unsigned long) size, slab2));*/
313 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
314 PROT_READ|PROT_WRITE)) {
315 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
316 (unsigned long)slab2->opslab_size, errno);
319 slab->opslab_readonly = 0;
323 # define Slab_to_rw(op) NOOP
326 /* This cannot possibly be right, but it was copied from the old slab
327 allocator, to which it was originally added, without explanation, in
330 # define PerlMemShared PerlMem
334 Perl_Slab_Free(pTHX_ void *op)
337 OP * const o = (OP *)op;
340 PERL_ARGS_ASSERT_SLAB_FREE;
342 if (!o->op_slabbed) {
344 PerlMemShared_free(op);
349 /* If this op is already freed, our refcount will get screwy. */
350 assert(o->op_type != OP_FREED);
351 o->op_type = OP_FREED;
352 o->op_next = slab->opslab_freed;
353 slab->opslab_freed = o;
354 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", (void*)o, (void*)slab));
355 OpslabREFCNT_dec_padok(slab);
359 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
362 const bool havepad = !!PL_comppad;
363 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
366 PAD_SAVE_SETNULLPAD();
373 Perl_opslab_free(pTHX_ OPSLAB *slab)
377 PERL_ARGS_ASSERT_OPSLAB_FREE;
378 DEBUG_S_warn((aTHX_ "freeing slab %p", (void*)slab));
379 assert(slab->opslab_refcnt == 1);
380 for (; slab; slab = slab2) {
381 slab2 = slab->opslab_next;
383 slab->opslab_refcnt = ~(size_t)0;
385 #ifdef PERL_DEBUG_READONLY_OPS
386 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
388 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
389 perror("munmap failed");
393 PerlMemShared_free(slab);
399 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
404 size_t savestack_count = 0;
406 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
409 for (slot = slab2->opslab_first;
411 slot = slot->opslot_next) {
412 if (slot->opslot_op.op_type != OP_FREED
413 && !(slot->opslot_op.op_savefree
419 assert(slot->opslot_op.op_slabbed);
420 op_free(&slot->opslot_op);
421 if (slab->opslab_refcnt == 1) goto free;
424 } while ((slab2 = slab2->opslab_next));
425 /* > 1 because the CV still holds a reference count. */
426 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
428 assert(savestack_count == slab->opslab_refcnt-1);
430 /* Remove the CV’s reference count. */
431 slab->opslab_refcnt--;
438 #ifdef PERL_DEBUG_READONLY_OPS
440 Perl_op_refcnt_inc(pTHX_ OP *o)
443 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
444 if (slab && slab->opslab_readonly) {
457 Perl_op_refcnt_dec(pTHX_ OP *o)
460 OPSLAB *const slab = o->op_slabbed ? OpSLAB(o) : NULL;
462 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
464 if (slab && slab->opslab_readonly) {
466 result = --o->op_targ;
469 result = --o->op_targ;
475 * In the following definition, the ", (OP*)0" is just to make the compiler
476 * think the expression is of the right type: croak actually does a Siglongjmp.
478 #define CHECKOP(type,o) \
479 ((PL_op_mask && PL_op_mask[type]) \
480 ? ( op_free((OP*)o), \
481 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
483 : PL_check[type](aTHX_ (OP*)o))
485 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
487 #define CHANGE_TYPE(o,type) \
489 o->op_type = (OPCODE)type; \
490 o->op_ppaddr = PL_ppaddr[type]; \
494 S_gv_ename(pTHX_ GV *gv)
496 SV* const tmpsv = sv_newmortal();
498 PERL_ARGS_ASSERT_GV_ENAME;
500 gv_efullname3(tmpsv, gv, NULL);
505 S_no_fh_allowed(pTHX_ OP *o)
507 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
509 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
515 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
517 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
518 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, SVfARG(namesv)),
519 SvUTF8(namesv) | flags);
524 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
526 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
527 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
532 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
534 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
536 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
541 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
543 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
545 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
546 SvUTF8(namesv) | flags);
551 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
553 PERL_ARGS_ASSERT_BAD_TYPE_PV;
555 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
556 (int)n, name, t, OP_DESC(kid)), flags);
560 S_bad_type_gv(pTHX_ I32 n, const char *t, GV *gv, U32 flags, const OP *kid)
562 SV * const namesv = gv_ename(gv);
563 PERL_ARGS_ASSERT_BAD_TYPE_GV;
565 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
566 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
570 S_no_bareword_allowed(pTHX_ OP *o)
572 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
574 qerror(Perl_mess(aTHX_
575 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
577 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
580 /* "register" allocation */
583 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
587 const bool is_our = (PL_parser->in_my == KEY_our);
589 PERL_ARGS_ASSERT_ALLOCMY;
591 if (flags & ~SVf_UTF8)
592 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
595 /* Until we're using the length for real, cross check that we're being
597 assert(strlen(name) == len);
599 /* complain about "my $<special_var>" etc etc */
603 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
604 (name[1] == '_' && (*name == '$' || len > 2))))
606 /* name[2] is true if strlen(name) > 2 */
607 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
608 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
609 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
610 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
611 PL_parser->in_my == KEY_state ? "state" : "my"));
613 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
614 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
617 else if (len == 2 && name[1] == '_' && !is_our)
618 /* diag_listed_as: Use of my $_ is experimental */
619 Perl_ck_warner_d(aTHX_ packWARN(WARN_EXPERIMENTAL__LEXICAL_TOPIC),
620 "Use of %s $_ is experimental",
621 PL_parser->in_my == KEY_state
625 /* allocate a spare slot and store the name in that slot */
627 off = pad_add_name_pvn(name, len,
628 (is_our ? padadd_OUR :
629 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
630 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
631 PL_parser->in_my_stash,
633 /* $_ is always in main::, even with our */
634 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
638 /* anon sub prototypes contains state vars should always be cloned,
639 * otherwise the state var would be shared between anon subs */
641 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
642 CvCLONE_on(PL_compcv);
648 =head1 Optree Manipulation Functions
650 =for apidoc alloccopstash
652 Available only under threaded builds, this function allocates an entry in
653 C<PL_stashpad> for the stash passed to it.
660 Perl_alloccopstash(pTHX_ HV *hv)
662 PADOFFSET off = 0, o = 1;
663 bool found_slot = FALSE;
665 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
667 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
669 for (; o < PL_stashpadmax; ++o) {
670 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
671 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
672 found_slot = TRUE, off = o;
675 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
676 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
677 off = PL_stashpadmax;
678 PL_stashpadmax += 10;
681 PL_stashpad[PL_stashpadix = off] = hv;
686 /* free the body of an op without examining its contents.
687 * Always use this rather than FreeOp directly */
690 S_op_destroy(pTHX_ OP *o)
698 =for apidoc Am|void|op_free|OP *o
700 Free an op. Only use this when an op is no longer linked to from any
707 Perl_op_free(pTHX_ OP *o)
712 /* Though ops may be freed twice, freeing the op after its slab is a
714 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
715 /* During the forced freeing of ops after compilation failure, kidops
716 may be freed before their parents. */
717 if (!o || o->op_type == OP_FREED)
721 if (o->op_private & OPpREFCOUNTED) {
732 refcnt = OpREFCNT_dec(o);
735 /* Need to find and remove any pattern match ops from the list
736 we maintain for reset(). */
737 find_and_forget_pmops(o);
747 /* Call the op_free hook if it has been set. Do it now so that it's called
748 * at the right time for refcounted ops, but still before all of the kids
752 if (o->op_flags & OPf_KIDS) {
754 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
755 nextkid = kid->op_sibling; /* Get before next freeing kid */
760 type = (OPCODE)o->op_targ;
763 Slab_to_rw(OpSLAB(o));
765 /* COP* is not cleared by op_clear() so that we may track line
766 * numbers etc even after null() */
767 if (type == OP_NEXTSTATE || type == OP_DBSTATE) {
773 #ifdef DEBUG_LEAKING_SCALARS
780 Perl_op_clear(pTHX_ OP *o)
785 PERL_ARGS_ASSERT_OP_CLEAR;
787 switch (o->op_type) {
788 case OP_NULL: /* Was holding old type, if any. */
791 case OP_ENTEREVAL: /* Was holding hints. */
795 if (!(o->op_flags & OPf_REF)
796 || (PL_check[o->op_type] != Perl_ck_ftst))
803 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
808 /* It's possible during global destruction that the GV is freed
809 before the optree. Whilst the SvREFCNT_inc is happy to bump from
810 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
811 will trigger an assertion failure, because the entry to sv_clear
812 checks that the scalar is not already freed. A check of for
813 !SvIS_FREED(gv) turns out to be invalid, because during global
814 destruction the reference count can be forced down to zero
815 (with SVf_BREAK set). In which case raising to 1 and then
816 dropping to 0 triggers cleanup before it should happen. I
817 *think* that this might actually be a general, systematic,
818 weakness of the whole idea of SVf_BREAK, in that code *is*
819 allowed to raise and lower references during global destruction,
820 so any *valid* code that happens to do this during global
821 destruction might well trigger premature cleanup. */
822 bool still_valid = gv && SvREFCNT(gv);
825 SvREFCNT_inc_simple_void(gv);
827 if (cPADOPo->op_padix > 0) {
828 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
829 * may still exist on the pad */
830 pad_swipe(cPADOPo->op_padix, TRUE);
831 cPADOPo->op_padix = 0;
834 SvREFCNT_dec(cSVOPo->op_sv);
835 cSVOPo->op_sv = NULL;
838 int try_downgrade = SvREFCNT(gv) == 2;
841 gv_try_downgrade(gv);
845 case OP_METHOD_NAMED:
848 SvREFCNT_dec(cSVOPo->op_sv);
849 cSVOPo->op_sv = NULL;
852 Even if op_clear does a pad_free for the target of the op,
853 pad_free doesn't actually remove the sv that exists in the pad;
854 instead it lives on. This results in that it could be reused as
855 a target later on when the pad was reallocated.
858 pad_swipe(o->op_targ,1);
868 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
873 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
874 assert(o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
876 if (cPADOPo->op_padix > 0) {
877 pad_swipe(cPADOPo->op_padix, TRUE);
878 cPADOPo->op_padix = 0;
881 SvREFCNT_dec(cSVOPo->op_sv);
882 cSVOPo->op_sv = NULL;
886 PerlMemShared_free(cPVOPo->op_pv);
887 cPVOPo->op_pv = NULL;
891 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
895 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
896 /* No GvIN_PAD_off here, because other references may still
897 * exist on the pad */
898 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
901 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
907 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
908 op_free(cPMOPo->op_code_list);
909 cPMOPo->op_code_list = NULL;
911 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
912 /* we use the same protection as the "SAFE" version of the PM_ macros
913 * here since sv_clean_all might release some PMOPs
914 * after PL_regex_padav has been cleared
915 * and the clearing of PL_regex_padav needs to
916 * happen before sv_clean_all
919 if(PL_regex_pad) { /* We could be in destruction */
920 const IV offset = (cPMOPo)->op_pmoffset;
921 ReREFCNT_dec(PM_GETRE(cPMOPo));
922 PL_regex_pad[offset] = &PL_sv_undef;
923 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
927 ReREFCNT_dec(PM_GETRE(cPMOPo));
928 PM_SETRE(cPMOPo, NULL);
934 if (o->op_targ > 0) {
935 pad_free(o->op_targ);
941 S_cop_free(pTHX_ COP* cop)
943 PERL_ARGS_ASSERT_COP_FREE;
946 if (! specialWARN(cop->cop_warnings))
947 PerlMemShared_free(cop->cop_warnings);
948 cophh_free(CopHINTHASH_get(cop));
949 if (PL_curcop == cop)
954 S_forget_pmop(pTHX_ PMOP *const o
957 HV * const pmstash = PmopSTASH(o);
959 PERL_ARGS_ASSERT_FORGET_PMOP;
961 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
962 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
964 PMOP **const array = (PMOP**) mg->mg_ptr;
965 U32 count = mg->mg_len / sizeof(PMOP**);
970 /* Found it. Move the entry at the end to overwrite it. */
971 array[i] = array[--count];
972 mg->mg_len = count * sizeof(PMOP**);
973 /* Could realloc smaller at this point always, but probably
974 not worth it. Probably worth free()ing if we're the
977 Safefree(mg->mg_ptr);
990 S_find_and_forget_pmops(pTHX_ OP *o)
992 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
994 if (o->op_flags & OPf_KIDS) {
995 OP *kid = cUNOPo->op_first;
997 switch (kid->op_type) {
1002 forget_pmop((PMOP*)kid);
1004 find_and_forget_pmops(kid);
1005 kid = kid->op_sibling;
1011 =for apidoc Am|void|op_null|OP *o
1013 Neutralizes an op when it is no longer needed, but is still linked to from
1020 Perl_op_null(pTHX_ OP *o)
1024 PERL_ARGS_ASSERT_OP_NULL;
1026 if (o->op_type == OP_NULL)
1029 o->op_targ = o->op_type;
1030 o->op_type = OP_NULL;
1031 o->op_ppaddr = PL_ppaddr[OP_NULL];
1035 Perl_op_refcnt_lock(pTHX)
1038 PERL_UNUSED_CONTEXT;
1043 Perl_op_refcnt_unlock(pTHX)
1046 PERL_UNUSED_CONTEXT;
1050 /* Contextualizers */
1053 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1055 Applies a syntactic context to an op tree representing an expression.
1056 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1057 or C<G_VOID> to specify the context to apply. The modified op tree
1064 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1066 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1068 case G_SCALAR: return scalar(o);
1069 case G_ARRAY: return list(o);
1070 case G_VOID: return scalarvoid(o);
1072 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1079 =for apidoc Am|OP*|op_linklist|OP *o
1080 This function is the implementation of the L</LINKLIST> macro. It should
1081 not be called directly.
1087 Perl_op_linklist(pTHX_ OP *o)
1091 PERL_ARGS_ASSERT_OP_LINKLIST;
1096 /* establish postfix order */
1097 first = cUNOPo->op_first;
1100 o->op_next = LINKLIST(first);
1103 if (kid->op_sibling) {
1104 kid->op_next = LINKLIST(kid->op_sibling);
1105 kid = kid->op_sibling;
1119 S_scalarkids(pTHX_ OP *o)
1121 if (o && o->op_flags & OPf_KIDS) {
1123 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1130 S_scalarboolean(pTHX_ OP *o)
1134 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1136 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1137 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1138 if (ckWARN(WARN_SYNTAX)) {
1139 const line_t oldline = CopLINE(PL_curcop);
1141 if (PL_parser && PL_parser->copline != NOLINE) {
1142 /* This ensures that warnings are reported at the first line
1143 of the conditional, not the last. */
1144 CopLINE_set(PL_curcop, PL_parser->copline);
1146 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1147 CopLINE_set(PL_curcop, oldline);
1154 S_op_varname(pTHX_ const OP *o)
1157 assert(o->op_type == OP_PADAV || o->op_type == OP_RV2AV ||
1158 o->op_type == OP_PADHV || o->op_type == OP_RV2HV);
1160 const char funny = o->op_type == OP_PADAV
1161 || o->op_type == OP_RV2AV ? '@' : '%';
1162 if (o->op_type == OP_RV2AV || o->op_type == OP_RV2HV) {
1164 if (cUNOPo->op_first->op_type != OP_GV
1165 || !(gv = cGVOPx_gv(cUNOPo->op_first)))
1167 return varname(gv, funny, 0, NULL, 0, 1);
1170 varname(MUTABLE_GV(PL_compcv), funny, o->op_targ, NULL, 0, 1);
1175 S_op_pretty(pTHX_ const OP *o, SV **retsv, const char **retpv)
1176 { /* or not so pretty :-) */
1177 if (o->op_type == OP_CONST) {
1179 if (SvPOK(*retsv)) {
1181 *retsv = sv_newmortal();
1182 pv_pretty(*retsv, SvPVX_const(sv), SvCUR(sv), 32, NULL, NULL,
1183 PERL_PV_PRETTY_DUMP |PERL_PV_ESCAPE_UNI_DETECT);
1185 else if (!SvOK(*retsv))
1188 else *retpv = "...";
1192 S_scalar_slice_warning(pTHX_ const OP *o)
1196 o->op_type == OP_HSLICE ? '{' : '[';
1198 o->op_type == OP_HSLICE ? '}' : ']';
1200 SV *keysv = NULL; /* just to silence compiler warnings */
1201 const char *key = NULL;
1203 if (!(o->op_private & OPpSLICEWARNING))
1205 if (PL_parser && PL_parser->error_count)
1206 /* This warning can be nonsensical when there is a syntax error. */
1209 kid = cLISTOPo->op_first;
1210 kid = kid->op_sibling; /* get past pushmark */
1211 /* weed out false positives: any ops that can return lists */
1212 switch (kid->op_type) {
1241 /* Don't warn if we have a nulled list either. */
1242 if (kid->op_type == OP_NULL && kid->op_targ == OP_LIST)
1245 assert(kid->op_sibling);
1246 name = S_op_varname(aTHX_ kid->op_sibling);
1247 if (!name) /* XS module fiddling with the op tree */
1249 S_op_pretty(aTHX_ kid, &keysv, &key);
1250 assert(SvPOK(name));
1251 sv_chop(name,SvPVX(name)+1);
1253 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1254 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1255 "Scalar value @%"SVf"%c%s%c better written as $%"SVf
1257 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1258 lbrack, key, rbrack);
1260 /* diag_listed_as: Scalar value @%s[%s] better written as $%s[%s] */
1261 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1262 "Scalar value @%"SVf"%c%"SVf"%c better written as $%"
1264 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1265 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1269 Perl_scalar(pTHX_ OP *o)
1274 /* assumes no premature commitment */
1275 if (!o || (PL_parser && PL_parser->error_count)
1276 || (o->op_flags & OPf_WANT)
1277 || o->op_type == OP_RETURN)
1282 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1284 switch (o->op_type) {
1286 scalar(cBINOPo->op_first);
1291 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1301 if (o->op_flags & OPf_KIDS) {
1302 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1308 kid = cLISTOPo->op_first;
1310 kid = kid->op_sibling;
1313 OP *sib = kid->op_sibling;
1314 if (sib && kid->op_type != OP_LEAVEWHEN)
1320 PL_curcop = &PL_compiling;
1325 kid = cLISTOPo->op_first;
1328 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1333 /* Warn about scalar context */
1334 const char lbrack = o->op_type == OP_KVHSLICE ? '{' : '[';
1335 const char rbrack = o->op_type == OP_KVHSLICE ? '}' : ']';
1338 const char *key = NULL;
1340 /* This warning can be nonsensical when there is a syntax error. */
1341 if (PL_parser && PL_parser->error_count)
1344 if (!ckWARN(WARN_SYNTAX)) break;
1346 kid = cLISTOPo->op_first;
1347 kid = kid->op_sibling; /* get past pushmark */
1348 assert(kid->op_sibling);
1349 name = S_op_varname(aTHX_ kid->op_sibling);
1350 if (!name) /* XS module fiddling with the op tree */
1352 S_op_pretty(aTHX_ kid, &keysv, &key);
1353 assert(SvPOK(name));
1354 sv_chop(name,SvPVX(name)+1);
1356 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1357 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1358 "%%%"SVf"%c%s%c in scalar context better written "
1360 SVfARG(name), lbrack, key, rbrack, SVfARG(name),
1361 lbrack, key, rbrack);
1363 /* diag_listed_as: %%s[%s] in scalar context better written as $%s[%s] */
1364 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1365 "%%%"SVf"%c%"SVf"%c in scalar context better "
1366 "written as $%"SVf"%c%"SVf"%c",
1367 SVfARG(name), lbrack, SVfARG(keysv), rbrack,
1368 SVfARG(name), lbrack, SVfARG(keysv), rbrack);
1375 Perl_scalarvoid(pTHX_ OP *o)
1379 SV *useless_sv = NULL;
1380 const char* useless = NULL;
1384 PERL_ARGS_ASSERT_SCALARVOID;
1386 if (o->op_type == OP_NEXTSTATE
1387 || o->op_type == OP_DBSTATE
1388 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1389 || o->op_targ == OP_DBSTATE)))
1390 PL_curcop = (COP*)o; /* for warning below */
1392 /* assumes no premature commitment */
1393 want = o->op_flags & OPf_WANT;
1394 if ((want && want != OPf_WANT_SCALAR)
1395 || (PL_parser && PL_parser->error_count)
1396 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1401 if ((o->op_private & OPpTARGET_MY)
1402 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1404 return scalar(o); /* As if inside SASSIGN */
1407 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1409 switch (o->op_type) {
1411 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1415 if (o->op_flags & OPf_STACKED)
1419 if (o->op_private == 4)
1444 case OP_AELEMFAST_LEX:
1465 case OP_GETSOCKNAME:
1466 case OP_GETPEERNAME:
1471 case OP_GETPRIORITY:
1496 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1497 /* Otherwise it's "Useless use of grep iterator" */
1498 useless = OP_DESC(o);
1502 kid = cLISTOPo->op_first;
1503 if (kid && kid->op_type == OP_PUSHRE
1505 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1507 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1509 useless = OP_DESC(o);
1513 kid = cUNOPo->op_first;
1514 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1515 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1518 useless = "negative pattern binding (!~)";
1522 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1523 useless = "non-destructive substitution (s///r)";
1527 useless = "non-destructive transliteration (tr///r)";
1534 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1535 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1536 useless = "a variable";
1541 if (cSVOPo->op_private & OPpCONST_STRICT)
1542 no_bareword_allowed(o);
1544 if (ckWARN(WARN_VOID)) {
1545 /* don't warn on optimised away booleans, eg
1546 * use constant Foo, 5; Foo || print; */
1547 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1549 /* the constants 0 and 1 are permitted as they are
1550 conventionally used as dummies in constructs like
1551 1 while some_condition_with_side_effects; */
1552 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1554 else if (SvPOK(sv)) {
1555 SV * const dsv = newSVpvs("");
1557 = Perl_newSVpvf(aTHX_
1559 pv_pretty(dsv, SvPVX_const(sv),
1560 SvCUR(sv), 32, NULL, NULL,
1562 | PERL_PV_ESCAPE_NOCLEAR
1563 | PERL_PV_ESCAPE_UNI_DETECT));
1564 SvREFCNT_dec_NN(dsv);
1566 else if (SvOK(sv)) {
1567 useless_sv = Perl_newSVpvf(aTHX_ "a constant (%"SVf")", SVfARG(sv));
1570 useless = "a constant (undef)";
1573 op_null(o); /* don't execute or even remember it */
1577 o->op_type = OP_PREINC; /* pre-increment is faster */
1578 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1582 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1583 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1587 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1588 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1592 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1593 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1598 UNOP *refgen, *rv2cv;
1601 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1604 rv2gv = ((BINOP *)o)->op_last;
1605 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1608 refgen = (UNOP *)((BINOP *)o)->op_first;
1610 if (!refgen || refgen->op_type != OP_REFGEN)
1613 exlist = (LISTOP *)refgen->op_first;
1614 if (!exlist || exlist->op_type != OP_NULL
1615 || exlist->op_targ != OP_LIST)
1618 if (exlist->op_first->op_type != OP_PUSHMARK)
1621 rv2cv = (UNOP*)exlist->op_last;
1623 if (rv2cv->op_type != OP_RV2CV)
1626 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1627 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1628 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1630 o->op_private |= OPpASSIGN_CV_TO_GV;
1631 rv2gv->op_private |= OPpDONT_INIT_GV;
1632 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1644 kid = cLOGOPo->op_first;
1645 if (kid->op_type == OP_NOT
1646 && (kid->op_flags & OPf_KIDS)) {
1647 if (o->op_type == OP_AND) {
1649 o->op_ppaddr = PL_ppaddr[OP_OR];
1651 o->op_type = OP_AND;
1652 o->op_ppaddr = PL_ppaddr[OP_AND];
1662 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1667 if (o->op_flags & OPf_STACKED)
1674 if (!(o->op_flags & OPf_KIDS))
1685 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1696 /* mortalise it, in case warnings are fatal. */
1697 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1698 "Useless use of %"SVf" in void context",
1699 SVfARG(sv_2mortal(useless_sv)));
1702 Perl_ck_warner(aTHX_ packWARN(WARN_VOID),
1703 "Useless use of %s in void context",
1710 S_listkids(pTHX_ OP *o)
1712 if (o && o->op_flags & OPf_KIDS) {
1714 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1721 Perl_list(pTHX_ OP *o)
1726 /* assumes no premature commitment */
1727 if (!o || (o->op_flags & OPf_WANT)
1728 || (PL_parser && PL_parser->error_count)
1729 || o->op_type == OP_RETURN)
1734 if ((o->op_private & OPpTARGET_MY)
1735 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1737 return o; /* As if inside SASSIGN */
1740 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1742 switch (o->op_type) {
1745 list(cBINOPo->op_first);
1750 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1758 if (!(o->op_flags & OPf_KIDS))
1760 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1761 list(cBINOPo->op_first);
1762 return gen_constant_list(o);
1769 kid = cLISTOPo->op_first;
1771 kid = kid->op_sibling;
1774 OP *sib = kid->op_sibling;
1775 if (sib && kid->op_type != OP_LEAVEWHEN)
1781 PL_curcop = &PL_compiling;
1785 kid = cLISTOPo->op_first;
1792 S_scalarseq(pTHX_ OP *o)
1796 const OPCODE type = o->op_type;
1798 if (type == OP_LINESEQ || type == OP_SCOPE ||
1799 type == OP_LEAVE || type == OP_LEAVETRY)
1802 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1803 if (kid->op_sibling) {
1807 PL_curcop = &PL_compiling;
1809 o->op_flags &= ~OPf_PARENS;
1810 if (PL_hints & HINT_BLOCK_SCOPE)
1811 o->op_flags |= OPf_PARENS;
1814 o = newOP(OP_STUB, 0);
1819 S_modkids(pTHX_ OP *o, I32 type)
1821 if (o && o->op_flags & OPf_KIDS) {
1823 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1824 op_lvalue(kid, type);
1830 =for apidoc finalize_optree
1832 This function finalizes the optree. Should be called directly after
1833 the complete optree is built. It does some additional
1834 checking which can't be done in the normal ck_xxx functions and makes
1835 the tree thread-safe.
1840 Perl_finalize_optree(pTHX_ OP* o)
1842 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1845 SAVEVPTR(PL_curcop);
1853 S_finalize_op(pTHX_ OP* o)
1855 PERL_ARGS_ASSERT_FINALIZE_OP;
1858 switch (o->op_type) {
1861 PL_curcop = ((COP*)o); /* for warnings */
1865 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1866 && ckWARN(WARN_EXEC))
1868 if (o->op_sibling->op_sibling) {
1869 const OPCODE type = o->op_sibling->op_sibling->op_type;
1870 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1871 const line_t oldline = CopLINE(PL_curcop);
1872 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1873 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1874 "Statement unlikely to be reached");
1875 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1876 "\t(Maybe you meant system() when you said exec()?)\n");
1877 CopLINE_set(PL_curcop, oldline);
1884 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1885 GV * const gv = cGVOPo_gv;
1886 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1887 /* XXX could check prototype here instead of just carping */
1888 SV * const sv = sv_newmortal();
1889 gv_efullname3(sv, gv, NULL);
1890 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1891 "%"SVf"() called too early to check prototype",
1898 if (cSVOPo->op_private & OPpCONST_STRICT)
1899 no_bareword_allowed(o);
1903 case OP_METHOD_NAMED:
1904 /* Relocate sv to the pad for thread safety.
1905 * Despite being a "constant", the SV is written to,
1906 * for reference counts, sv_upgrade() etc. */
1907 if (cSVOPo->op_sv) {
1908 const PADOFFSET ix = pad_alloc(OP_CONST, SVf_READONLY);
1909 SvREFCNT_dec(PAD_SVl(ix));
1910 PAD_SETSV(ix, cSVOPo->op_sv);
1911 /* XXX I don't know how this isn't readonly already. */
1912 if (!SvIsCOW(PAD_SVl(ix))) SvREADONLY_on(PAD_SVl(ix));
1913 cSVOPo->op_sv = NULL;
1927 if ((key_op = cSVOPx(((BINOP*)o)->op_last))->op_type != OP_CONST)
1930 rop = (UNOP*)((BINOP*)o)->op_first;
1935 S_scalar_slice_warning(aTHX_ o);
1939 kid = cLISTOPo->op_first->op_sibling;
1940 if (/* I bet there's always a pushmark... */
1941 OP_TYPE_ISNT_AND_WASNT_NN(kid, OP_LIST)
1942 && OP_TYPE_ISNT_NN(kid, OP_CONST))
1947 key_op = (SVOP*)(kid->op_type == OP_CONST
1949 : kLISTOP->op_first->op_sibling);
1951 rop = (UNOP*)((LISTOP*)o)->op_last;
1954 if (o->op_private & OPpLVAL_INTRO || rop->op_type != OP_RV2HV)
1956 else if (rop->op_first->op_type == OP_PADSV)
1957 /* @$hash{qw(keys here)} */
1958 rop = (UNOP*)rop->op_first;
1960 /* @{$hash}{qw(keys here)} */
1961 if (rop->op_first->op_type == OP_SCOPE
1962 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1964 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1970 lexname = NULL; /* just to silence compiler warnings */
1971 fields = NULL; /* just to silence compiler warnings */
1975 && (lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE),
1976 SvPAD_TYPED(lexname))
1977 && (fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE))
1978 && isGV(*fields) && GvHV(*fields);
1980 key_op = (SVOP*)key_op->op_sibling) {
1982 if (key_op->op_type != OP_CONST)
1984 svp = cSVOPx_svp(key_op);
1986 /* Make the CONST have a shared SV */
1987 if ((!SvIsCOW_shared_hash(sv = *svp))
1988 && SvTYPE(sv) < SVt_PVMG && SvOK(sv) && !SvROK(sv)) {
1990 const char * const key = SvPV_const(sv, *(STRLEN*)&keylen);
1991 SV *nsv = newSVpvn_share(key,
1992 SvUTF8(sv) ? -keylen : keylen, 0);
1993 SvREFCNT_dec_NN(sv);
1998 && !hv_fetch_ent(GvHV(*fields), *svp, FALSE, 0)) {
1999 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
2000 "in variable %"SVf" of type %"HEKf,
2001 SVfARG(*svp), SVfARG(lexname),
2002 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
2008 S_scalar_slice_warning(aTHX_ o);
2012 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
2013 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
2020 if (o->op_flags & OPf_KIDS) {
2022 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
2028 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
2030 Propagate lvalue ("modifiable") context to an op and its children.
2031 I<type> represents the context type, roughly based on the type of op that
2032 would do the modifying, although C<local()> is represented by OP_NULL,
2033 because it has no op type of its own (it is signalled by a flag on
2036 This function detects things that can't be modified, such as C<$x+1>, and
2037 generates errors for them. For example, C<$x+1 = 2> would cause it to be
2038 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
2040 It also flags things that need to behave specially in an lvalue context,
2041 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
2047 S_vivifies(const OPCODE type)
2050 case OP_RV2AV: case OP_ASLICE:
2051 case OP_RV2HV: case OP_KVASLICE:
2052 case OP_RV2SV: case OP_HSLICE:
2053 case OP_AELEMFAST: case OP_KVHSLICE:
2062 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
2066 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
2069 if (!o || (PL_parser && PL_parser->error_count))
2072 if ((o->op_private & OPpTARGET_MY)
2073 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
2078 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
2080 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
2082 switch (o->op_type) {
2087 if ((o->op_flags & OPf_PARENS))
2091 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
2092 !(o->op_flags & OPf_STACKED)) {
2093 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2094 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
2095 poses, so we need it clear. */
2096 o->op_private &= ~1;
2097 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2098 assert(cUNOPo->op_first->op_type == OP_NULL);
2099 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
2102 else { /* lvalue subroutine call */
2103 o->op_private |= OPpLVAL_INTRO
2104 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
2105 PL_modcount = RETURN_UNLIMITED_NUMBER;
2106 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
2107 /* Potential lvalue context: */
2108 o->op_private |= OPpENTERSUB_INARGS;
2111 else { /* Compile-time error message: */
2112 OP *kid = cUNOPo->op_first;
2115 if (kid->op_type != OP_PUSHMARK) {
2116 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
2118 "panic: unexpected lvalue entersub "
2119 "args: type/targ %ld:%"UVuf,
2120 (long)kid->op_type, (UV)kid->op_targ);
2121 kid = kLISTOP->op_first;
2123 while (kid->op_sibling)
2124 kid = kid->op_sibling;
2125 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
2126 break; /* Postpone until runtime */
2129 kid = kUNOP->op_first;
2130 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
2131 kid = kUNOP->op_first;
2132 if (kid->op_type == OP_NULL)
2134 "Unexpected constant lvalue entersub "
2135 "entry via type/targ %ld:%"UVuf,
2136 (long)kid->op_type, (UV)kid->op_targ);
2137 if (kid->op_type != OP_GV) {
2141 cv = GvCV(kGVOP_gv);
2151 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2152 /* grep, foreach, subcalls, refgen */
2153 if (type == OP_GREPSTART || type == OP_ENTERSUB
2154 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2156 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2157 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2159 : (o->op_type == OP_ENTERSUB
2160 ? "non-lvalue subroutine call"
2162 type ? PL_op_desc[type] : "local"));
2176 case OP_RIGHT_SHIFT:
2185 if (!(o->op_flags & OPf_STACKED))
2192 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2193 op_lvalue(kid, type);
2198 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2199 PL_modcount = RETURN_UNLIMITED_NUMBER;
2200 return o; /* Treat \(@foo) like ordinary list. */
2204 if (scalar_mod_type(o, type))
2206 ref(cUNOPo->op_first, o->op_type);
2213 /* Do not apply the lvsub flag for rv2[ah]v in scalar context. */
2214 if (type == OP_LEAVESUBLV && (
2215 (o->op_type != OP_RV2AV && o->op_type != OP_RV2HV)
2216 || (o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2218 o->op_private |= OPpMAYBE_LVSUB;
2222 PL_modcount = RETURN_UNLIMITED_NUMBER;
2226 if (type == OP_LEAVESUBLV)
2227 o->op_private |= OPpMAYBE_LVSUB;
2230 PL_hints |= HINT_BLOCK_SCOPE;
2231 if (type == OP_LEAVESUBLV)
2232 o->op_private |= OPpMAYBE_LVSUB;
2236 ref(cUNOPo->op_first, o->op_type);
2240 PL_hints |= HINT_BLOCK_SCOPE;
2250 case OP_AELEMFAST_LEX:
2257 PL_modcount = RETURN_UNLIMITED_NUMBER;
2258 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2259 return o; /* Treat \(@foo) like ordinary list. */
2260 if (scalar_mod_type(o, type))
2262 if ((o->op_flags & OPf_WANT) != OPf_WANT_SCALAR
2263 && type == OP_LEAVESUBLV)
2264 o->op_private |= OPpMAYBE_LVSUB;
2268 if (!type) /* local() */
2269 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2270 PAD_COMPNAME_SV(o->op_targ));
2279 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2283 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2289 if (type == OP_LEAVESUBLV)
2290 o->op_private |= OPpMAYBE_LVSUB;
2291 if (o->op_flags & OPf_KIDS)
2292 op_lvalue(cBINOPo->op_first->op_sibling, type);
2297 ref(cBINOPo->op_first, o->op_type);
2298 if (type == OP_ENTERSUB &&
2299 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2300 o->op_private |= OPpLVAL_DEFER;
2301 if (type == OP_LEAVESUBLV)
2302 o->op_private |= OPpMAYBE_LVSUB;
2309 o->op_private |= OPpLVALUE;
2315 if (o->op_flags & OPf_KIDS)
2316 op_lvalue(cLISTOPo->op_last, type);
2321 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2323 else if (!(o->op_flags & OPf_KIDS))
2325 if (o->op_targ != OP_LIST) {
2326 op_lvalue(cBINOPo->op_first, type);
2332 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2333 /* elements might be in void context because the list is
2334 in scalar context or because they are attribute sub calls */
2335 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2336 op_lvalue(kid, type);
2340 if (type != OP_LEAVESUBLV)
2342 break; /* op_lvalue()ing was handled by ck_return() */
2349 if (type == OP_LEAVESUBLV
2350 || !S_vivifies(cLOGOPo->op_first->op_type))
2351 op_lvalue(cLOGOPo->op_first, type);
2352 if (type == OP_LEAVESUBLV
2353 || !S_vivifies(cLOGOPo->op_first->op_sibling->op_type))
2354 op_lvalue(cLOGOPo->op_first->op_sibling, type);
2358 /* [20011101.069] File test operators interpret OPf_REF to mean that
2359 their argument is a filehandle; thus \stat(".") should not set
2361 if (type == OP_REFGEN &&
2362 PL_check[o->op_type] == Perl_ck_ftst)
2365 if (type != OP_LEAVESUBLV)
2366 o->op_flags |= OPf_MOD;
2368 if (type == OP_AASSIGN || type == OP_SASSIGN)
2369 o->op_flags |= OPf_SPECIAL|OPf_REF;
2370 else if (!type) { /* local() */
2373 o->op_private |= OPpLVAL_INTRO;
2374 o->op_flags &= ~OPf_SPECIAL;
2375 PL_hints |= HINT_BLOCK_SCOPE;
2380 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2381 "Useless localization of %s", OP_DESC(o));
2384 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2385 && type != OP_LEAVESUBLV)
2386 o->op_flags |= OPf_REF;
2391 S_scalar_mod_type(const OP *o, I32 type)
2396 if (o && o->op_type == OP_RV2GV)
2420 case OP_RIGHT_SHIFT:
2441 S_is_handle_constructor(const OP *o, I32 numargs)
2443 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2445 switch (o->op_type) {
2453 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2466 S_refkids(pTHX_ OP *o, I32 type)
2468 if (o && o->op_flags & OPf_KIDS) {
2470 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2477 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2482 PERL_ARGS_ASSERT_DOREF;
2484 if (!o || (PL_parser && PL_parser->error_count))
2487 switch (o->op_type) {
2489 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2490 !(o->op_flags & OPf_STACKED)) {
2491 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2492 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2493 assert(cUNOPo->op_first->op_type == OP_NULL);
2494 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2495 o->op_flags |= OPf_SPECIAL;
2496 o->op_private &= ~1;
2498 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2499 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2500 : type == OP_RV2HV ? OPpDEREF_HV
2502 o->op_flags |= OPf_MOD;
2508 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2509 doref(kid, type, set_op_ref);
2512 if (type == OP_DEFINED)
2513 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2514 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2517 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2518 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2519 : type == OP_RV2HV ? OPpDEREF_HV
2521 o->op_flags |= OPf_MOD;
2528 o->op_flags |= OPf_REF;
2531 if (type == OP_DEFINED)
2532 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2533 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2539 o->op_flags |= OPf_REF;
2544 if (!(o->op_flags & OPf_KIDS) || type == OP_DEFINED)
2546 doref(cBINOPo->op_first, type, set_op_ref);
2550 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2551 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2552 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2553 : type == OP_RV2HV ? OPpDEREF_HV
2555 o->op_flags |= OPf_MOD;
2565 if (!(o->op_flags & OPf_KIDS))
2567 doref(cLISTOPo->op_last, type, set_op_ref);
2577 S_dup_attrlist(pTHX_ OP *o)
2582 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2584 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2585 * where the first kid is OP_PUSHMARK and the remaining ones
2586 * are OP_CONST. We need to push the OP_CONST values.
2588 if (o->op_type == OP_CONST)
2589 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2591 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2593 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2594 if (o->op_type == OP_CONST)
2595 rop = op_append_elem(OP_LIST, rop,
2596 newSVOP(OP_CONST, o->op_flags,
2597 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2604 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
2607 SV * const stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2609 PERL_ARGS_ASSERT_APPLY_ATTRS;
2611 /* fake up C<use attributes $pkg,$rv,@attrs> */
2613 #define ATTRSMODULE "attributes"
2614 #define ATTRSMODULE_PM "attributes.pm"
2616 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2617 newSVpvs(ATTRSMODULE),
2619 op_prepend_elem(OP_LIST,
2620 newSVOP(OP_CONST, 0, stashsv),
2621 op_prepend_elem(OP_LIST,
2622 newSVOP(OP_CONST, 0,
2624 dup_attrlist(attrs))));
2628 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2631 OP *pack, *imop, *arg;
2632 SV *meth, *stashsv, **svp;
2634 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2639 assert(target->op_type == OP_PADSV ||
2640 target->op_type == OP_PADHV ||
2641 target->op_type == OP_PADAV);
2643 /* Ensure that attributes.pm is loaded. */
2644 /* Don't force the C<use> if we don't need it. */
2645 svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2646 if (svp && *svp != &PL_sv_undef)
2647 NOOP; /* already in %INC */
2649 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2650 newSVpvs(ATTRSMODULE), NULL);
2652 /* Need package name for method call. */
2653 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2655 /* Build up the real arg-list. */
2656 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2658 arg = newOP(OP_PADSV, 0);
2659 arg->op_targ = target->op_targ;
2660 arg = op_prepend_elem(OP_LIST,
2661 newSVOP(OP_CONST, 0, stashsv),
2662 op_prepend_elem(OP_LIST,
2663 newUNOP(OP_REFGEN, 0,
2664 op_lvalue(arg, OP_REFGEN)),
2665 dup_attrlist(attrs)));
2667 /* Fake up a method call to import */
2668 meth = newSVpvs_share("import");
2669 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2670 op_append_elem(OP_LIST,
2671 op_prepend_elem(OP_LIST, pack, list(arg)),
2672 newSVOP(OP_METHOD_NAMED, 0, meth)));
2674 /* Combine the ops. */
2675 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2679 =notfor apidoc apply_attrs_string
2681 Attempts to apply a list of attributes specified by the C<attrstr> and
2682 C<len> arguments to the subroutine identified by the C<cv> argument which
2683 is expected to be associated with the package identified by the C<stashpv>
2684 argument (see L<attributes>). It gets this wrong, though, in that it
2685 does not correctly identify the boundaries of the individual attribute
2686 specifications within C<attrstr>. This is not really intended for the
2687 public API, but has to be listed here for systems such as AIX which
2688 need an explicit export list for symbols. (It's called from XS code
2689 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2690 to respect attribute syntax properly would be welcome.
2696 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2697 const char *attrstr, STRLEN len)
2701 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2704 len = strlen(attrstr);
2708 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2710 const char * const sstr = attrstr;
2711 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2712 attrs = op_append_elem(OP_LIST, attrs,
2713 newSVOP(OP_CONST, 0,
2714 newSVpvn(sstr, attrstr-sstr)));
2718 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2719 newSVpvs(ATTRSMODULE),
2720 NULL, op_prepend_elem(OP_LIST,
2721 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2722 op_prepend_elem(OP_LIST,
2723 newSVOP(OP_CONST, 0,
2724 newRV(MUTABLE_SV(cv))),
2729 S_move_proto_attr(pTHX_ OP **proto, OP **attrs, const GV * name)
2731 OP *new_proto = NULL;
2736 PERL_ARGS_ASSERT_MOVE_PROTO_ATTR;
2742 if (o->op_type == OP_CONST) {
2743 pv = SvPV(cSVOPo_sv, pvlen);
2744 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
2745 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
2746 SV ** const tmpo = cSVOPx_svp(o);
2747 SvREFCNT_dec(cSVOPo_sv);
2752 } else if (o->op_type == OP_LIST) {
2754 assert(o->op_flags & OPf_KIDS);
2755 assert(cLISTOPo->op_first->op_type == OP_PUSHMARK);
2756 /* Counting on the first op to hit the lasto = o line */
2757 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2758 if (o->op_type == OP_CONST) {
2759 pv = SvPV(cSVOPo_sv, pvlen);
2760 if (pvlen >= 10 && memEQ(pv, "prototype(", 10)) {
2761 SV * const tmpsv = newSVpvn_flags(pv + 10, pvlen - 11, SvUTF8(cSVOPo_sv));
2762 SV ** const tmpo = cSVOPx_svp(o);
2763 SvREFCNT_dec(cSVOPo_sv);
2765 if (new_proto && ckWARN(WARN_MISC)) {
2767 const char * newp = SvPV(cSVOPo_sv, new_len);
2768 Perl_warner(aTHX_ packWARN(WARN_MISC),
2769 "Attribute prototype(%"UTF8f") discards earlier prototype attribute in same sub",
2770 UTF8fARG(SvUTF8(cSVOPo_sv), new_len, newp));
2776 lasto->op_sibling = o->op_sibling;
2782 /* If the list is now just the PUSHMARK, scrap the whole thing; otherwise attributes.xs
2783 would get pulled in with no real need */
2784 if (!cLISTOPx(*attrs)->op_first->op_sibling) {
2793 svname = sv_newmortal();
2794 gv_efullname3(svname, name, NULL);
2796 else if (SvPOK(name) && *SvPVX((SV *)name) == '&')
2797 svname = newSVpvn_flags(SvPVX((SV *)name)+1, SvCUR(name)-1, SvUTF8(name)|SVs_TEMP);
2799 svname = (SV *)name;
2800 if (ckWARN(WARN_ILLEGALPROTO))
2801 (void)validate_proto(svname, cSVOPx_sv(new_proto), TRUE);
2802 if (*proto && ckWARN(WARN_PROTOTYPE)) {
2803 STRLEN old_len, new_len;
2804 const char * oldp = SvPV(cSVOPx_sv(*proto), old_len);
2805 const char * newp = SvPV(cSVOPx_sv(new_proto), new_len);
2807 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
2808 "Prototype '%"UTF8f"' overridden by attribute 'prototype(%"UTF8f")'"
2810 UTF8fARG(SvUTF8(cSVOPx_sv(*proto)), old_len, oldp),
2811 UTF8fARG(SvUTF8(cSVOPx_sv(new_proto)), new_len, newp),
2821 S_cant_declare(pTHX_ OP *o)
2823 if (o->op_type == OP_NULL
2824 && (o->op_flags & (OPf_SPECIAL|OPf_KIDS)) == OPf_KIDS)
2825 o = cUNOPo->op_first;
2826 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2827 o->op_type == OP_NULL
2828 && o->op_flags & OPf_SPECIAL
2831 PL_parser->in_my == KEY_our ? "our" :
2832 PL_parser->in_my == KEY_state ? "state" :
2837 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2841 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2843 PERL_ARGS_ASSERT_MY_KID;
2845 if (!o || (PL_parser && PL_parser->error_count))
2850 if (type == OP_LIST) {
2852 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2853 my_kid(kid, attrs, imopsp);
2855 } else if (type == OP_UNDEF || type == OP_STUB) {
2857 } else if (type == OP_RV2SV || /* "our" declaration */
2859 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2860 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2861 S_cant_declare(aTHX_ o);
2863 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2865 PL_parser->in_my = FALSE;
2866 PL_parser->in_my_stash = NULL;
2867 apply_attrs(GvSTASH(gv),
2868 (type == OP_RV2SV ? GvSV(gv) :
2869 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2870 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2873 o->op_private |= OPpOUR_INTRO;
2876 else if (type != OP_PADSV &&
2879 type != OP_PUSHMARK)
2881 S_cant_declare(aTHX_ o);
2884 else if (attrs && type != OP_PUSHMARK) {
2888 PL_parser->in_my = FALSE;
2889 PL_parser->in_my_stash = NULL;
2891 /* check for C<my Dog $spot> when deciding package */
2892 stash = PAD_COMPNAME_TYPE(o->op_targ);
2894 stash = PL_curstash;
2895 apply_attrs_my(stash, o, attrs, imopsp);
2897 o->op_flags |= OPf_MOD;
2898 o->op_private |= OPpLVAL_INTRO;
2900 o->op_private |= OPpPAD_STATE;
2905 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2909 int maybe_scalar = 0;
2911 PERL_ARGS_ASSERT_MY_ATTRS;
2913 /* [perl #17376]: this appears to be premature, and results in code such as
2914 C< our(%x); > executing in list mode rather than void mode */
2916 if (o->op_flags & OPf_PARENS)
2926 o = my_kid(o, attrs, &rops);
2928 if (maybe_scalar && o->op_type == OP_PADSV) {
2929 o = scalar(op_append_list(OP_LIST, rops, o));
2930 o->op_private |= OPpLVAL_INTRO;
2933 /* The listop in rops might have a pushmark at the beginning,
2934 which will mess up list assignment. */
2935 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2936 if (rops->op_type == OP_LIST &&
2937 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2939 OP * const pushmark = lrops->op_first;
2940 lrops->op_first = pushmark->op_sibling;
2943 o = op_append_list(OP_LIST, o, rops);
2946 PL_parser->in_my = FALSE;
2947 PL_parser->in_my_stash = NULL;
2952 Perl_sawparens(pTHX_ OP *o)
2954 PERL_UNUSED_CONTEXT;
2956 o->op_flags |= OPf_PARENS;
2961 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2965 const OPCODE ltype = left->op_type;
2966 const OPCODE rtype = right->op_type;
2968 PERL_ARGS_ASSERT_BIND_MATCH;
2970 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2971 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2973 const char * const desc
2975 rtype == OP_SUBST || rtype == OP_TRANS
2976 || rtype == OP_TRANSR
2978 ? (int)rtype : OP_MATCH];
2979 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2981 S_op_varname(aTHX_ left);
2983 Perl_warner(aTHX_ packWARN(WARN_MISC),
2984 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2985 desc, SVfARG(name), SVfARG(name));
2987 const char * const sample = (isary
2988 ? "@array" : "%hash");
2989 Perl_warner(aTHX_ packWARN(WARN_MISC),
2990 "Applying %s to %s will act on scalar(%s)",
2991 desc, sample, sample);
2995 if (rtype == OP_CONST &&
2996 cSVOPx(right)->op_private & OPpCONST_BARE &&
2997 cSVOPx(right)->op_private & OPpCONST_STRICT)
2999 no_bareword_allowed(right);
3002 /* !~ doesn't make sense with /r, so error on it for now */
3003 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
3005 /* diag_listed_as: Using !~ with %s doesn't make sense */
3006 yyerror("Using !~ with s///r doesn't make sense");
3007 if (rtype == OP_TRANSR && type == OP_NOT)
3008 /* diag_listed_as: Using !~ with %s doesn't make sense */
3009 yyerror("Using !~ with tr///r doesn't make sense");
3011 ismatchop = (rtype == OP_MATCH ||
3012 rtype == OP_SUBST ||
3013 rtype == OP_TRANS || rtype == OP_TRANSR)
3014 && !(right->op_flags & OPf_SPECIAL);
3015 if (ismatchop && right->op_private & OPpTARGET_MY) {
3017 right->op_private &= ~OPpTARGET_MY;
3019 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
3022 right->op_flags |= OPf_STACKED;
3023 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
3024 ! (rtype == OP_TRANS &&
3025 right->op_private & OPpTRANS_IDENTICAL) &&
3026 ! (rtype == OP_SUBST &&
3027 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
3028 newleft = op_lvalue(left, rtype);
3031 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
3032 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
3034 o = op_prepend_elem(rtype, scalar(newleft), right);
3036 return newUNOP(OP_NOT, 0, scalar(o));
3040 return bind_match(type, left,
3041 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
3045 Perl_invert(pTHX_ OP *o)
3049 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
3053 =for apidoc Amx|OP *|op_scope|OP *o
3055 Wraps up an op tree with some additional ops so that at runtime a dynamic
3056 scope will be created. The original ops run in the new dynamic scope,
3057 and then, provided that they exit normally, the scope will be unwound.
3058 The additional ops used to create and unwind the dynamic scope will
3059 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
3060 instead if the ops are simple enough to not need the full dynamic scope
3067 Perl_op_scope(pTHX_ OP *o)
3071 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || TAINTING_get) {
3072 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
3073 o->op_type = OP_LEAVE;
3074 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
3076 else if (o->op_type == OP_LINESEQ) {
3078 o->op_type = OP_SCOPE;
3079 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
3080 kid = ((LISTOP*)o)->op_first;
3081 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
3084 /* The following deals with things like 'do {1 for 1}' */
3085 kid = kid->op_sibling;
3087 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
3092 o = newLISTOP(OP_SCOPE, 0, o, NULL);
3098 Perl_op_unscope(pTHX_ OP *o)
3100 if (o && o->op_type == OP_LINESEQ) {
3101 OP *kid = cLISTOPo->op_first;
3102 for(; kid; kid = kid->op_sibling)
3103 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
3110 Perl_block_start(pTHX_ int full)
3113 const int retval = PL_savestack_ix;
3115 pad_block_start(full);
3117 PL_hints &= ~HINT_BLOCK_SCOPE;
3118 SAVECOMPILEWARNINGS();
3119 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
3121 CALL_BLOCK_HOOKS(bhk_start, full);
3127 Perl_block_end(pTHX_ I32 floor, OP *seq)
3130 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
3131 OP* retval = scalarseq(seq);
3134 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
3138 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
3142 /* pad_leavemy has created a sequence of introcv ops for all my
3143 subs declared in the block. We have to replicate that list with
3144 clonecv ops, to deal with this situation:
3149 sub s1 { state sub foo { \&s2 } }
3152 Originally, I was going to have introcv clone the CV and turn
3153 off the stale flag. Since &s1 is declared before &s2, the
3154 introcv op for &s1 is executed (on sub entry) before the one for
3155 &s2. But the &foo sub inside &s1 (which is cloned when &s1 is
3156 cloned, since it is a state sub) closes over &s2 and expects
3157 to see it in its outer CV’s pad. If the introcv op clones &s1,
3158 then &s2 is still marked stale. Since &s1 is not active, and
3159 &foo closes over &s1’s implicit entry for &s2, we get a ‘Varia-
3160 ble will not stay shared’ warning. Because it is the same stub
3161 that will be used when the introcv op for &s2 is executed, clos-
3162 ing over it is safe. Hence, we have to turn off the stale flag
3163 on all lexical subs in the block before we clone any of them.
3164 Hence, having introcv clone the sub cannot work. So we create a
3165 list of ops like this:
3189 OP *kid = o->op_flags & OPf_KIDS ? cLISTOPo->op_first : o;
3190 OP * const last = o->op_flags & OPf_KIDS ? cLISTOPo->op_last : o;
3191 for (;; kid = kid->op_sibling) {
3192 OP *newkid = newOP(OP_CLONECV, 0);
3193 newkid->op_targ = kid->op_targ;
3194 o = op_append_elem(OP_LINESEQ, o, newkid);
3195 if (kid == last) break;
3197 retval = op_prepend_elem(OP_LINESEQ, o, retval);
3200 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
3206 =head1 Compile-time scope hooks
3208 =for apidoc Aox||blockhook_register
3210 Register a set of hooks to be called when the Perl lexical scope changes
3211 at compile time. See L<perlguts/"Compile-time scope hooks">.
3217 Perl_blockhook_register(pTHX_ BHK *hk)
3219 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
3221 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
3228 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
3229 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
3230 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
3233 OP * const o = newOP(OP_PADSV, 0);
3234 o->op_targ = offset;
3240 Perl_newPROG(pTHX_ OP *o)
3244 PERL_ARGS_ASSERT_NEWPROG;
3251 PL_eval_root = newUNOP(OP_LEAVEEVAL,
3252 ((PL_in_eval & EVAL_KEEPERR)
3253 ? OPf_SPECIAL : 0), o);
3255 cx = &cxstack[cxstack_ix];
3256 assert(CxTYPE(cx) == CXt_EVAL);
3258 if ((cx->blk_gimme & G_WANT) == G_VOID)
3259 scalarvoid(PL_eval_root);
3260 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
3263 scalar(PL_eval_root);
3265 PL_eval_start = op_linklist(PL_eval_root);
3266 PL_eval_root->op_private |= OPpREFCOUNTED;
3267 OpREFCNT_set(PL_eval_root, 1);
3268 PL_eval_root->op_next = 0;
3269 i = PL_savestack_ix;
3272 CALL_PEEP(PL_eval_start);
3273 finalize_optree(PL_eval_root);
3274 S_prune_chain_head(&PL_eval_start);
3276 PL_savestack_ix = i;
3279 if (o->op_type == OP_STUB) {
3280 /* This block is entered if nothing is compiled for the main
3281 program. This will be the case for an genuinely empty main
3282 program, or one which only has BEGIN blocks etc, so already
3285 Historically (5.000) the guard above was !o. However, commit
3286 f8a08f7b8bd67b28 (Jun 2001), integrated to blead as
3287 c71fccf11fde0068, changed perly.y so that newPROG() is now
3288 called with the output of block_end(), which returns a new
3289 OP_STUB for the case of an empty optree. ByteLoader (and
3290 maybe other things) also take this path, because they set up
3291 PL_main_start and PL_main_root directly, without generating an
3294 If the parsing the main program aborts (due to parse errors,
3295 or due to BEGIN or similar calling exit), then newPROG()
3296 isn't even called, and hence this code path and its cleanups
3297 are skipped. This shouldn't make a make a difference:
3298 * a non-zero return from perl_parse is a failure, and
3299 perl_destruct() should be called immediately.
3300 * however, if exit(0) is called during the parse, then
3301 perl_parse() returns 0, and perl_run() is called. As
3302 PL_main_start will be NULL, perl_run() will return
3303 promptly, and the exit code will remain 0.
3306 PL_comppad_name = 0;
3308 S_op_destroy(aTHX_ o);
3311 PL_main_root = op_scope(sawparens(scalarvoid(o)));
3312 PL_curcop = &PL_compiling;
3313 PL_main_start = LINKLIST(PL_main_root);
3314 PL_main_root->op_private |= OPpREFCOUNTED;
3315 OpREFCNT_set(PL_main_root, 1);
3316 PL_main_root->op_next = 0;
3317 CALL_PEEP(PL_main_start);
3318 finalize_optree(PL_main_root);
3319 S_prune_chain_head(&PL_main_start);
3320 cv_forget_slab(PL_compcv);
3323 /* Register with debugger */
3325 CV * const cv = get_cvs("DB::postponed", 0);
3329 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
3331 call_sv(MUTABLE_SV(cv), G_DISCARD);
3338 Perl_localize(pTHX_ OP *o, I32 lex)
3342 PERL_ARGS_ASSERT_LOCALIZE;
3344 if (o->op_flags & OPf_PARENS)
3345 /* [perl #17376]: this appears to be premature, and results in code such as
3346 C< our(%x); > executing in list mode rather than void mode */
3353 if ( PL_parser->bufptr > PL_parser->oldbufptr
3354 && PL_parser->bufptr[-1] == ','
3355 && ckWARN(WARN_PARENTHESIS))
3357 char *s = PL_parser->bufptr;
3360 /* some heuristics to detect a potential error */
3361 while (*s && (strchr(", \t\n", *s)))
3365 if (*s && strchr("@$%*", *s) && *++s
3366 && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s))) {
3369 while (*s && (isWORDCHAR(*s) || UTF8_IS_CONTINUED(*s)))
3371 while (*s && (strchr(", \t\n", *s)))
3377 if (sigil && (*s == ';' || *s == '=')) {
3378 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3379 "Parentheses missing around \"%s\" list",
3381 ? (PL_parser->in_my == KEY_our
3383 : PL_parser->in_my == KEY_state
3393 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3394 PL_parser->in_my = FALSE;
3395 PL_parser->in_my_stash = NULL;
3400 Perl_jmaybe(pTHX_ OP *o)
3402 PERL_ARGS_ASSERT_JMAYBE;
3404 if (o->op_type == OP_LIST) {
3406 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3407 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3412 PERL_STATIC_INLINE OP *
3413 S_op_std_init(pTHX_ OP *o)
3415 I32 type = o->op_type;
3417 PERL_ARGS_ASSERT_OP_STD_INIT;
3419 if (PL_opargs[type] & OA_RETSCALAR)
3421 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3422 o->op_targ = pad_alloc(type, SVs_PADTMP);
3427 PERL_STATIC_INLINE OP *
3428 S_op_integerize(pTHX_ OP *o)
3430 I32 type = o->op_type;
3432 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3434 /* integerize op. */
3435 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3438 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
3441 if (type == OP_NEGATE)
3442 /* XXX might want a ck_negate() for this */
3443 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3449 S_fold_constants(pTHX_ OP *o)
3454 VOL I32 type = o->op_type;
3459 SV * const oldwarnhook = PL_warnhook;
3460 SV * const olddiehook = PL_diehook;
3464 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3466 if (!(PL_opargs[type] & OA_FOLDCONST))
3475 #ifdef USE_LOCALE_CTYPE
3476 if (IN_LC_COMPILETIME(LC_CTYPE))
3485 #ifdef USE_LOCALE_COLLATE
3486 if (IN_LC_COMPILETIME(LC_COLLATE))
3491 /* XXX what about the numeric ops? */
3492 #ifdef USE_LOCALE_NUMERIC
3493 if (IN_LC_COMPILETIME(LC_NUMERIC))
3498 if (!cLISTOPo->op_first->op_sibling
3499 || cLISTOPo->op_first->op_sibling->op_type != OP_CONST)
3502 SV * const sv = cSVOPx_sv(cLISTOPo->op_first->op_sibling);
3503 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3505 const char *s = SvPVX_const(sv);
3506 while (s < SvEND(sv)) {
3507 if (*s == 'p' || *s == 'P') goto nope;
3514 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3517 if (cUNOPx(cUNOPo->op_first)->op_first->op_type != OP_CONST
3518 || SvPADTMP(cSVOPx_sv(cUNOPx(cUNOPo->op_first)->op_first)))
3522 if (PL_parser && PL_parser->error_count)
3523 goto nope; /* Don't try to run w/ errors */
3525 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3526 const OPCODE type = curop->op_type;
3527 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3529 type != OP_SCALAR &&
3531 type != OP_PUSHMARK)
3537 curop = LINKLIST(o);
3538 old_next = o->op_next;
3542 oldscope = PL_scopestack_ix;
3543 create_eval_scope(G_FAKINGEVAL);
3545 /* Verify that we don't need to save it: */
3546 assert(PL_curcop == &PL_compiling);
3547 StructCopy(&PL_compiling, ¬_compiling, COP);
3548 PL_curcop = ¬_compiling;
3549 /* The above ensures that we run with all the correct hints of the
3550 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3551 assert(IN_PERL_RUNTIME);
3552 PL_warnhook = PERL_WARNHOOK_FATAL;
3559 sv = *(PL_stack_sp--);
3560 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3561 pad_swipe(o->op_targ, FALSE);
3563 else if (SvTEMP(sv)) { /* grab mortal temp? */
3564 SvREFCNT_inc_simple_void(sv);
3567 else { assert(SvIMMORTAL(sv)); }
3570 /* Something tried to die. Abandon constant folding. */
3571 /* Pretend the error never happened. */
3573 o->op_next = old_next;
3577 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3578 PL_warnhook = oldwarnhook;
3579 PL_diehook = olddiehook;
3580 /* XXX note that this croak may fail as we've already blown away
3581 * the stack - eg any nested evals */
3582 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3585 PL_warnhook = oldwarnhook;
3586 PL_diehook = olddiehook;
3587 PL_curcop = &PL_compiling;
3589 if (PL_scopestack_ix > oldscope)
3590 delete_eval_scope();
3597 if (type == OP_STRINGIFY) SvPADTMP_off(sv);
3598 else if (!SvIMMORTAL(sv)) {
3602 if (type == OP_RV2GV)
3603 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3606 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3607 if (type != OP_STRINGIFY) newop->op_folded = 1;
3616 S_gen_constant_list(pTHX_ OP *o)
3620 const SSize_t oldtmps_floor = PL_tmps_floor;
3625 if (PL_parser && PL_parser->error_count)
3626 return o; /* Don't attempt to run with errors */
3628 curop = LINKLIST(o);
3631 S_prune_chain_head(&curop);
3633 Perl_pp_pushmark(aTHX);
3636 assert (!(curop->op_flags & OPf_SPECIAL));
3637 assert(curop->op_type == OP_RANGE);
3638 Perl_pp_anonlist(aTHX);
3639 PL_tmps_floor = oldtmps_floor;
3641 o->op_type = OP_RV2AV;
3642 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3643 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3644 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3645 o->op_opt = 0; /* needs to be revisited in rpeep() */
3646 curop = ((UNOP*)o)->op_first;
3647 av = (AV *)SvREFCNT_inc_NN(*PL_stack_sp--);
3648 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, (SV *)av);
3649 if (AvFILLp(av) != -1)
3650 for (svp = AvARRAY(av) + AvFILLp(av); svp >= AvARRAY(av); --svp)
3653 SvREADONLY_on(*svp);
3661 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3664 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3665 if (!o || o->op_type != OP_LIST)
3666 o = newLISTOP(OP_LIST, 0, o, NULL);
3668 o->op_flags &= ~OPf_WANT;
3670 if (!(PL_opargs[type] & OA_MARK))
3671 op_null(cLISTOPo->op_first);
3673 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3674 if (kid2 && kid2->op_type == OP_COREARGS) {
3675 op_null(cLISTOPo->op_first);
3676 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3680 o->op_type = (OPCODE)type;
3681 o->op_ppaddr = PL_ppaddr[type];
3682 o->op_flags |= flags;
3684 o = CHECKOP(type, o);
3685 if (o->op_type != (unsigned)type)
3688 return fold_constants(op_integerize(op_std_init(o)));
3692 =head1 Optree Manipulation Functions
3695 /* List constructors */
3698 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3700 Append an item to the list of ops contained directly within a list-type
3701 op, returning the lengthened list. I<first> is the list-type op,
3702 and I<last> is the op to append to the list. I<optype> specifies the
3703 intended opcode for the list. If I<first> is not already a list of the
3704 right type, it will be upgraded into one. If either I<first> or I<last>
3705 is null, the other is returned unchanged.
3711 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3719 if (first->op_type != (unsigned)type
3720 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3722 return newLISTOP(type, 0, first, last);
3725 if (first->op_flags & OPf_KIDS)
3726 ((LISTOP*)first)->op_last->op_sibling = last;
3728 first->op_flags |= OPf_KIDS;
3729 ((LISTOP*)first)->op_first = last;
3731 ((LISTOP*)first)->op_last = last;
3736 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3738 Concatenate the lists of ops contained directly within two list-type ops,
3739 returning the combined list. I<first> and I<last> are the list-type ops
3740 to concatenate. I<optype> specifies the intended opcode for the list.
3741 If either I<first> or I<last> is not already a list of the right type,
3742 it will be upgraded into one. If either I<first> or I<last> is null,
3743 the other is returned unchanged.
3749 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3757 if (first->op_type != (unsigned)type)
3758 return op_prepend_elem(type, first, last);
3760 if (last->op_type != (unsigned)type)
3761 return op_append_elem(type, first, last);
3763 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3764 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3765 first->op_flags |= (last->op_flags & OPf_KIDS);
3768 S_op_destroy(aTHX_ last);
3774 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3776 Prepend an item to the list of ops contained directly within a list-type
3777 op, returning the lengthened list. I<first> is the op to prepend to the
3778 list, and I<last> is the list-type op. I<optype> specifies the intended
3779 opcode for the list. If I<last> is not already a list of the right type,
3780 it will be upgraded into one. If either I<first> or I<last> is null,
3781 the other is returned unchanged.
3787 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3795 if (last->op_type == (unsigned)type) {
3796 if (type == OP_LIST) { /* already a PUSHMARK there */
3797 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3798 ((LISTOP*)last)->op_first->op_sibling = first;
3799 if (!(first->op_flags & OPf_PARENS))
3800 last->op_flags &= ~OPf_PARENS;
3803 if (!(last->op_flags & OPf_KIDS)) {
3804 ((LISTOP*)last)->op_last = first;
3805 last->op_flags |= OPf_KIDS;
3807 first->op_sibling = ((LISTOP*)last)->op_first;
3808 ((LISTOP*)last)->op_first = first;
3810 last->op_flags |= OPf_KIDS;
3814 return newLISTOP(type, 0, first, last);
3821 =head1 Optree construction
3823 =for apidoc Am|OP *|newNULLLIST
3825 Constructs, checks, and returns a new C<stub> op, which represents an
3826 empty list expression.
3832 Perl_newNULLLIST(pTHX)
3834 return newOP(OP_STUB, 0);
3838 S_force_list(pTHX_ OP *o)
3840 if (!o || o->op_type != OP_LIST)
3841 o = newLISTOP(OP_LIST, 0, o, NULL);
3847 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3849 Constructs, checks, and returns an op of any list type. I<type> is
3850 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3851 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3852 supply up to two ops to be direct children of the list op; they are
3853 consumed by this function and become part of the constructed op tree.
3859 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3864 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3866 NewOp(1101, listop, 1, LISTOP);
3868 listop->op_type = (OPCODE)type;
3869 listop->op_ppaddr = PL_ppaddr[type];
3872 listop->op_flags = (U8)flags;
3876 else if (!first && last)
3879 first->op_sibling = last;
3880 listop->op_first = first;
3881 listop->op_last = last;
3882 if (type == OP_LIST) {
3883 OP* const pushop = newOP(OP_PUSHMARK, 0);
3884 pushop->op_sibling = first;
3885 listop->op_first = pushop;
3886 listop->op_flags |= OPf_KIDS;
3888 listop->op_last = pushop;
3891 return CHECKOP(type, listop);
3895 =for apidoc Am|OP *|newOP|I32 type|I32 flags
3897 Constructs, checks, and returns an op of any base type (any type that
3898 has no extra fields). I<type> is the opcode. I<flags> gives the
3899 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3906 Perl_newOP(pTHX_ I32 type, I32 flags)
3911 if (type == -OP_ENTEREVAL) {
3912 type = OP_ENTEREVAL;
3913 flags |= OPpEVAL_BYTES<<8;
3916 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3917 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3918 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3919 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3921 NewOp(1101, o, 1, OP);
3922 o->op_type = (OPCODE)type;
3923 o->op_ppaddr = PL_ppaddr[type];
3924 o->op_flags = (U8)flags;
3927 o->op_private = (U8)(0 | (flags >> 8));
3928 if (PL_opargs[type] & OA_RETSCALAR)
3930 if (PL_opargs[type] & OA_TARGET)
3931 o->op_targ = pad_alloc(type, SVs_PADTMP);
3932 return CHECKOP(type, o);
3936 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3938 Constructs, checks, and returns an op of any unary type. I<type> is
3939 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3940 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3941 bits, the eight bits of C<op_private>, except that the bit with value 1
3942 is automatically set. I<first> supplies an optional op to be the direct
3943 child of the unary op; it is consumed by this function and become part
3944 of the constructed op tree.
3950 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3955 if (type == -OP_ENTEREVAL) {
3956 type = OP_ENTEREVAL;
3957 flags |= OPpEVAL_BYTES<<8;
3960 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3961 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3962 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3963 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3964 || type == OP_SASSIGN
3965 || type == OP_ENTERTRY
3966 || type == OP_NULL );
3969 first = newOP(OP_STUB, 0);
3970 if (PL_opargs[type] & OA_MARK)
3971 first = force_list(first);
3973 NewOp(1101, unop, 1, UNOP);
3974 unop->op_type = (OPCODE)type;
3975 unop->op_ppaddr = PL_ppaddr[type];
3976 unop->op_first = first;
3977 unop->op_flags = (U8)(flags | OPf_KIDS);
3978 unop->op_private = (U8)(1 | (flags >> 8));
3979 unop = (UNOP*) CHECKOP(type, unop);
3983 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3987 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3989 Constructs, checks, and returns an op of any binary type. I<type>
3990 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3991 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3992 the eight bits of C<op_private>, except that the bit with value 1 or
3993 2 is automatically set as required. I<first> and I<last> supply up to
3994 two ops to be the direct children of the binary op; they are consumed
3995 by this function and become part of the constructed op tree.
4001 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
4006 ASSUME((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
4007 || type == OP_SASSIGN || type == OP_NULL );
4009 NewOp(1101, binop, 1, BINOP);
4012 first = newOP(OP_NULL, 0);
4014 binop->op_type = (OPCODE)type;
4015 binop->op_ppaddr = PL_ppaddr[type];
4016 binop->op_first = first;
4017 binop->op_flags = (U8)(flags | OPf_KIDS);
4020 binop->op_private = (U8)(1 | (flags >> 8));
4023 binop->op_private = (U8)(2 | (flags >> 8));
4024 first->op_sibling = last;
4027 binop = (BINOP*)CHECKOP(type, binop);
4028 if (binop->op_next || binop->op_type != (OPCODE)type)
4031 binop->op_last = binop->op_first->op_sibling;
4033 return fold_constants(op_integerize(op_std_init((OP *)binop)));
4036 static int uvcompare(const void *a, const void *b)
4037 __attribute__nonnull__(1)
4038 __attribute__nonnull__(2)
4039 __attribute__pure__;
4040 static int uvcompare(const void *a, const void *b)
4042 if (*((const UV *)a) < (*(const UV *)b))
4044 if (*((const UV *)a) > (*(const UV *)b))
4046 if (*((const UV *)a+1) < (*(const UV *)b+1))
4048 if (*((const UV *)a+1) > (*(const UV *)b+1))
4054 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
4057 SV * const tstr = ((SVOP*)expr)->op_sv;
4059 ((SVOP*)repl)->op_sv;
4062 const U8 *t = (U8*)SvPV_const(tstr, tlen);
4063 const U8 *r = (U8*)SvPV_const(rstr, rlen);
4069 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
4070 const I32 squash = o->op_private & OPpTRANS_SQUASH;
4071 I32 del = o->op_private & OPpTRANS_DELETE;
4074 PERL_ARGS_ASSERT_PMTRANS;
4076 PL_hints |= HINT_BLOCK_SCOPE;
4079 o->op_private |= OPpTRANS_FROM_UTF;
4082 o->op_private |= OPpTRANS_TO_UTF;
4084 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
4085 SV* const listsv = newSVpvs("# comment\n");
4087 const U8* tend = t + tlen;
4088 const U8* rend = r + rlen;
4102 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4103 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4106 const U32 flags = UTF8_ALLOW_DEFAULT;
4110 t = tsave = bytes_to_utf8(t, &len);
4113 if (!to_utf && rlen) {
4115 r = rsave = bytes_to_utf8(r, &len);
4119 /* There is a snag with this code on EBCDIC: scan_const() in toke.c has
4120 * encoded chars in native encoding which makes ranges in the EBCDIC 0..255
4124 U8 tmpbuf[UTF8_MAXBYTES+1];
4127 Newx(cp, 2*tlen, UV);
4129 transv = newSVpvs("");
4131 cp[2*i] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4133 if (t < tend && *t == ILLEGAL_UTF8_BYTE) {
4135 cp[2*i+1] = utf8n_to_uvchr(t, tend-t, &ulen, flags);
4139 cp[2*i+1] = cp[2*i];
4143 qsort(cp, i, 2*sizeof(UV), uvcompare);
4144 for (j = 0; j < i; j++) {
4146 diff = val - nextmin;
4148 t = uvchr_to_utf8(tmpbuf,nextmin);
4149 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4151 U8 range_mark = ILLEGAL_UTF8_BYTE;
4152 t = uvchr_to_utf8(tmpbuf, val - 1);
4153 sv_catpvn(transv, (char *)&range_mark, 1);
4154 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4161 t = uvchr_to_utf8(tmpbuf,nextmin);
4162 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4164 U8 range_mark = ILLEGAL_UTF8_BYTE;
4165 sv_catpvn(transv, (char *)&range_mark, 1);
4167 t = uvchr_to_utf8(tmpbuf, 0x7fffffff);
4168 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4169 t = (const U8*)SvPVX_const(transv);
4170 tlen = SvCUR(transv);
4174 else if (!rlen && !del) {
4175 r = t; rlen = tlen; rend = tend;
4178 if ((!rlen && !del) || t == r ||
4179 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4181 o->op_private |= OPpTRANS_IDENTICAL;
4185 while (t < tend || tfirst <= tlast) {
4186 /* see if we need more "t" chars */
4187 if (tfirst > tlast) {
4188 tfirst = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4190 if (t < tend && *t == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4192 tlast = (I32)utf8n_to_uvchr(t, tend - t, &ulen, flags);
4199 /* now see if we need more "r" chars */
4200 if (rfirst > rlast) {
4202 rfirst = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4204 if (r < rend && *r == ILLEGAL_UTF8_BYTE) { /* illegal utf8 val indicates range */
4206 rlast = (I32)utf8n_to_uvchr(r, rend - r, &ulen, flags);
4215 rfirst = rlast = 0xffffffff;
4219 /* now see which range will peter our first, if either. */
4220 tdiff = tlast - tfirst;
4221 rdiff = rlast - rfirst;
4228 if (rfirst == 0xffffffff) {
4229 diff = tdiff; /* oops, pretend rdiff is infinite */
4231 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4232 (long)tfirst, (long)tlast);
4234 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4238 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4239 (long)tfirst, (long)(tfirst + diff),
4242 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4243 (long)tfirst, (long)rfirst);
4245 if (rfirst + diff > max)
4246 max = rfirst + diff;
4248 grows = (tfirst < rfirst &&
4249 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4261 else if (max > 0xff)
4266 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4268 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVf_READONLY);
4269 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4270 PAD_SETSV(cPADOPo->op_padix, swash);
4272 SvREADONLY_on(swash);
4274 cSVOPo->op_sv = swash;
4276 SvREFCNT_dec(listsv);
4277 SvREFCNT_dec(transv);
4279 if (!del && havefinal && rlen)
4280 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4281 newSVuv((UV)final), 0);
4284 o->op_private |= OPpTRANS_GROWS;
4294 tbl = (short*)PerlMemShared_calloc(
4295 (o->op_private & OPpTRANS_COMPLEMENT) &&
4296 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4298 cPVOPo->op_pv = (char*)tbl;
4300 for (i = 0; i < (I32)tlen; i++)
4302 for (i = 0, j = 0; i < 256; i++) {
4304 if (j >= (I32)rlen) {
4313 if (i < 128 && r[j] >= 128)
4323 o->op_private |= OPpTRANS_IDENTICAL;
4325 else if (j >= (I32)rlen)
4330 PerlMemShared_realloc(tbl,
4331 (0x101+rlen-j) * sizeof(short));
4332 cPVOPo->op_pv = (char*)tbl;
4334 tbl[0x100] = (short)(rlen - j);
4335 for (i=0; i < (I32)rlen - j; i++)
4336 tbl[0x101+i] = r[j+i];
4340 if (!rlen && !del) {
4343 o->op_private |= OPpTRANS_IDENTICAL;
4345 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4346 o->op_private |= OPpTRANS_IDENTICAL;
4348 for (i = 0; i < 256; i++)
4350 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4351 if (j >= (I32)rlen) {
4353 if (tbl[t[i]] == -1)
4359 if (tbl[t[i]] == -1) {
4360 if (t[i] < 128 && r[j] >= 128)
4367 if(del && rlen == tlen) {
4368 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4369 } else if(rlen > tlen && !complement) {
4370 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4374 o->op_private |= OPpTRANS_GROWS;
4382 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4384 Constructs, checks, and returns an op of any pattern matching type.
4385 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4386 and, shifted up eight bits, the eight bits of C<op_private>.
4392 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4397 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4399 NewOp(1101, pmop, 1, PMOP);
4400 pmop->op_type = (OPCODE)type;
4401 pmop->op_ppaddr = PL_ppaddr[type];
4402 pmop->op_flags = (U8)flags;
4403 pmop->op_private = (U8)(0 | (flags >> 8));
4405 if (PL_hints & HINT_RE_TAINT)
4406 pmop->op_pmflags |= PMf_RETAINT;
4407 #ifdef USE_LOCALE_CTYPE
4408 if (IN_LC_COMPILETIME(LC_CTYPE)) {
4409 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4414 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4416 if (PL_hints & HINT_RE_FLAGS) {
4417 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4418 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4420 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4421 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4422 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4424 if (reflags && SvOK(reflags)) {
4425 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4431 assert(SvPOK(PL_regex_pad[0]));
4432 if (SvCUR(PL_regex_pad[0])) {
4433 /* Pop off the "packed" IV from the end. */
4434 SV *const repointer_list = PL_regex_pad[0];
4435 const char *p = SvEND(repointer_list) - sizeof(IV);
4436 const IV offset = *((IV*)p);
4438 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4440 SvEND_set(repointer_list, p);
4442 pmop->op_pmoffset = offset;
4443 /* This slot should be free, so assert this: */
4444 assert(PL_regex_pad[offset] == &PL_sv_undef);
4446 SV * const repointer = &PL_sv_undef;
4447 av_push(PL_regex_padav, repointer);
4448 pmop->op_pmoffset = av_tindex(PL_regex_padav);
4449 PL_regex_pad = AvARRAY(PL_regex_padav);
4453 return CHECKOP(type, pmop);
4456 /* Given some sort of match op o, and an expression expr containing a
4457 * pattern, either compile expr into a regex and attach it to o (if it's
4458 * constant), or convert expr into a runtime regcomp op sequence (if it's
4461 * isreg indicates that the pattern is part of a regex construct, eg
4462 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4463 * split "pattern", which aren't. In the former case, expr will be a list
4464 * if the pattern contains more than one term (eg /a$b/) or if it contains
4465 * a replacement, ie s/// or tr///.
4467 * When the pattern has been compiled within a new anon CV (for
4468 * qr/(?{...})/ ), then floor indicates the savestack level just before
4469 * the new sub was created
4473 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4478 I32 repl_has_vars = 0;
4480 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4481 bool is_compiletime;
4484 PERL_ARGS_ASSERT_PMRUNTIME;
4486 /* for s/// and tr///, last element in list is the replacement; pop it */
4488 if (is_trans || o->op_type == OP_SUBST) {
4490 repl = cLISTOPx(expr)->op_last;
4491 kid = cLISTOPx(expr)->op_first;
4492 while (kid->op_sibling != repl)
4493 kid = kid->op_sibling;
4494 kid->op_sibling = NULL;
4495 cLISTOPx(expr)->op_last = kid;
4498 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4501 OP* const oe = expr;
4502 assert(expr->op_type == OP_LIST);
4503 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4504 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4505 expr = cLISTOPx(oe)->op_last;
4506 cLISTOPx(oe)->op_first->op_sibling = NULL;
4507 cLISTOPx(oe)->op_last = NULL;
4510 return pmtrans(o, expr, repl);
4513 /* find whether we have any runtime or code elements;
4514 * at the same time, temporarily set the op_next of each DO block;
4515 * then when we LINKLIST, this will cause the DO blocks to be excluded
4516 * from the op_next chain (and from having LINKLIST recursively
4517 * applied to them). We fix up the DOs specially later */
4521 if (expr->op_type == OP_LIST) {
4523 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4524 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4526 assert(!o->op_next && o->op_sibling);
4527 o->op_next = o->op_sibling;
4529 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4533 else if (expr->op_type != OP_CONST)
4538 /* fix up DO blocks; treat each one as a separate little sub;
4539 * also, mark any arrays as LIST/REF */
4541 if (expr->op_type == OP_LIST) {
4543 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4545 if (o->op_type == OP_PADAV || o->op_type == OP_RV2AV) {
4546 assert( !(o->op_flags & OPf_WANT));
4547 /* push the array rather than its contents. The regex
4548 * engine will retrieve and join the elements later */
4549 o->op_flags |= (OPf_WANT_LIST | OPf_REF);
4553 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4555 o->op_next = NULL; /* undo temporary hack from above */
4558 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4559 LISTOP *leaveop = cLISTOPx(cLISTOPo->op_first);
4561 assert(leaveop->op_first->op_type == OP_ENTER);
4562 assert(leaveop->op_first->op_sibling);
4563 o->op_next = leaveop->op_first->op_sibling;
4565 assert(leaveop->op_flags & OPf_KIDS);
4566 assert(leaveop->op_last->op_next == (OP*)leaveop);
4567 leaveop->op_next = NULL; /* stop on last op */
4568 op_null((OP*)leaveop);
4572 OP *scope = cLISTOPo->op_first;
4573 assert(scope->op_type == OP_SCOPE);
4574 assert(scope->op_flags & OPf_KIDS);
4575 scope->op_next = NULL; /* stop on last op */
4578 /* have to peep the DOs individually as we've removed it from
4579 * the op_next chain */
4581 S_prune_chain_head(&(o->op_next));
4583 /* runtime finalizes as part of finalizing whole tree */
4587 else if (expr->op_type == OP_PADAV || expr->op_type == OP_RV2AV) {
4588 assert( !(expr->op_flags & OPf_WANT));
4589 /* push the array rather than its contents. The regex
4590 * engine will retrieve and join the elements later */
4591 expr->op_flags |= (OPf_WANT_LIST | OPf_REF);
4594 PL_hints |= HINT_BLOCK_SCOPE;
4596 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4598 if (is_compiletime) {
4599 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4600 regexp_engine const *eng = current_re_engine();
4602 if (o->op_flags & OPf_SPECIAL)
4603 rx_flags |= RXf_SPLIT;
4605 if (!has_code || !eng->op_comp) {
4606 /* compile-time simple constant pattern */
4608 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4609 /* whoops! we guessed that a qr// had a code block, but we
4610 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4611 * that isn't required now. Note that we have to be pretty
4612 * confident that nothing used that CV's pad while the
4613 * regex was parsed */
4614 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4615 /* But we know that one op is using this CV's slab. */
4616 cv_forget_slab(PL_compcv);
4618 pm->op_pmflags &= ~PMf_HAS_CV;
4623 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4624 rx_flags, pm->op_pmflags)
4625 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4626 rx_flags, pm->op_pmflags)
4631 /* compile-time pattern that includes literal code blocks */
4632 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4635 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4638 if (pm->op_pmflags & PMf_HAS_CV) {
4640 /* this QR op (and the anon sub we embed it in) is never
4641 * actually executed. It's just a placeholder where we can
4642 * squirrel away expr in op_code_list without the peephole
4643 * optimiser etc processing it for a second time */
4644 OP *qr = newPMOP(OP_QR, 0);
4645 ((PMOP*)qr)->op_code_list = expr;
4647 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4648 SvREFCNT_inc_simple_void(PL_compcv);
4649 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4650 ReANY(re)->qr_anoncv = cv;
4652 /* attach the anon CV to the pad so that
4653 * pad_fixup_inner_anons() can find it */
4654 (void)pad_add_anon(cv, o->op_type);
4655 SvREFCNT_inc_simple_void(cv);
4658 pm->op_code_list = expr;
4663 /* runtime pattern: build chain of regcomp etc ops */
4665 PADOFFSET cv_targ = 0;
4667 reglist = isreg && expr->op_type == OP_LIST;
4672 pm->op_code_list = expr;
4673 /* don't free op_code_list; its ops are embedded elsewhere too */
4674 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
4677 if (o->op_flags & OPf_SPECIAL)
4678 pm->op_pmflags |= PMf_SPLIT;
4680 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
4681 * to allow its op_next to be pointed past the regcomp and
4682 * preceding stacking ops;
4683 * OP_REGCRESET is there to reset taint before executing the
4685 if (pm->op_pmflags & PMf_KEEP || TAINTING_get)
4686 expr = newUNOP((TAINTING_get ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
4688 if (pm->op_pmflags & PMf_HAS_CV) {
4689 /* we have a runtime qr with literal code. This means
4690 * that the qr// has been wrapped in a new CV, which
4691 * means that runtime consts, vars etc will have been compiled
4692 * against a new pad. So... we need to execute those ops
4693 * within the environment of the new CV. So wrap them in a call
4694 * to a new anon sub. i.e. for
4698 * we build an anon sub that looks like
4700 * sub { "a", $b, '(?{...})' }
4702 * and call it, passing the returned list to regcomp.
4703 * Or to put it another way, the list of ops that get executed
4707 * ------ -------------------
4708 * pushmark (for regcomp)
4709 * pushmark (for entersub)
4710 * pushmark (for refgen)
4714 * regcreset regcreset
4716 * const("a") const("a")
4718 * const("(?{...})") const("(?{...})")
4723 SvREFCNT_inc_simple_void(PL_compcv);
4724 /* these lines are just an unrolled newANONATTRSUB */
4725 expr = newSVOP(OP_ANONCODE, 0,
4726 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
4727 cv_targ = expr->op_targ;
4728 expr = newUNOP(OP_REFGEN, 0, expr);
4730 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
4733 NewOp(1101, rcop, 1, LOGOP);
4734 rcop->op_type = OP_REGCOMP;
4735 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4736 rcop->op_first = scalar(expr);
4737 rcop->op_flags |= OPf_KIDS
4738 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4739 | (reglist ? OPf_STACKED : 0);
4740 rcop->op_private = 0;
4742 rcop->op_targ = cv_targ;
4744 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4745 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4747 /* establish postfix order */
4748 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
4750 rcop->op_next = expr;
4751 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4754 rcop->op_next = LINKLIST(expr);
4755 expr->op_next = (OP*)rcop;
4758 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4764 /* If we are looking at s//.../e with a single statement, get past
4765 the implicit do{}. */
4766 if (curop->op_type == OP_NULL && curop->op_flags & OPf_KIDS
4767 && cUNOPx(curop)->op_first->op_type == OP_SCOPE
4768 && cUNOPx(curop)->op_first->op_flags & OPf_KIDS) {
4769 OP *kid = cUNOPx(cUNOPx(curop)->op_first)->op_first;
4770 if (kid->op_type == OP_NULL && kid->op_sibling
4771 && !kid->op_sibling->op_sibling)
4772 curop = kid->op_sibling;
4774 if (curop->op_type == OP_CONST)
4776 else if (( (curop->op_type == OP_RV2SV ||
4777 curop->op_type == OP_RV2AV ||
4778 curop->op_type == OP_RV2HV ||
4779 curop->op_type == OP_RV2GV)
4780 && cUNOPx(curop)->op_first
4781 && cUNOPx(curop)->op_first->op_type == OP_GV )
4782 || curop->op_type == OP_PADSV
4783 || curop->op_type == OP_PADAV
4784 || curop->op_type == OP_PADHV
4785 || curop->op_type == OP_PADANY) {
4793 || !RX_PRELEN(PM_GETRE(pm))
4794 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4796 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4797 op_prepend_elem(o->op_type, scalar(repl), o);
4800 NewOp(1101, rcop, 1, LOGOP);
4801 rcop->op_type = OP_SUBSTCONT;
4802 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4803 rcop->op_first = scalar(repl);
4804 rcop->op_flags |= OPf_KIDS;
4805 rcop->op_private = 1;
4808 /* establish postfix order */
4809 rcop->op_next = LINKLIST(repl);
4810 repl->op_next = (OP*)rcop;