4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
108 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
109 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
110 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
112 /* See the explanatory comments above struct opslab in op.h. */
114 #ifdef PERL_DEBUG_READONLY_OPS
115 # define PERL_SLAB_SIZE 128
116 # define PERL_MAX_SLAB_SIZE 4096
117 # include <sys/mman.h>
120 #ifndef PERL_SLAB_SIZE
121 # define PERL_SLAB_SIZE 64
123 #ifndef PERL_MAX_SLAB_SIZE
124 # define PERL_MAX_SLAB_SIZE 2048
127 /* rounds up to nearest pointer */
128 #define SIZE_TO_PSIZE(x) (((x) + sizeof(I32 *) - 1)/sizeof(I32 *))
129 #define DIFF(o,p) ((size_t)((I32 **)(p) - (I32**)(o)))
132 S_new_slab(pTHX_ size_t sz)
134 #ifdef PERL_DEBUG_READONLY_OPS
135 OPSLAB *slab = (OPSLAB *) mmap(0, sz * sizeof(I32 *),
136 PROT_READ|PROT_WRITE,
137 MAP_ANON|MAP_PRIVATE, -1, 0);
138 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
139 (unsigned long) sz, slab));
140 if (slab == MAP_FAILED) {
141 perror("mmap failed");
144 slab->opslab_size = (U16)sz;
146 OPSLAB *slab = (OPSLAB *)PerlMemShared_calloc(sz, sizeof(I32 *));
148 slab->opslab_first = (OPSLOT *)((I32 **)slab + sz - 1);
152 /* requires double parens and aTHX_ */
153 #define DEBUG_S_warn(args) \
155 PerlIO_printf(Perl_debug_log, "%s", SvPVx_nolen(Perl_mess args)) \
159 Perl_Slab_Alloc(pTHX_ size_t sz)
168 if (!PL_compcv || CvROOT(PL_compcv)
169 || (CvSTART(PL_compcv) && !CvSLABBED(PL_compcv)))
170 return PerlMemShared_calloc(1, sz);
172 if (!CvSTART(PL_compcv)) { /* sneak it in here */
174 (OP *)(slab = S_new_slab(aTHX_ PERL_SLAB_SIZE));
175 CvSLABBED_on(PL_compcv);
176 slab->opslab_refcnt = 2; /* one for the CV; one for the new OP */
178 else ++(slab = (OPSLAB *)CvSTART(PL_compcv))->opslab_refcnt;
180 opsz = SIZE_TO_PSIZE(sz);
181 sz = opsz + OPSLOT_HEADER_P;
183 if (slab->opslab_freed) {
184 OP **too = &slab->opslab_freed;
186 DEBUG_S_warn((aTHX_ "found free op at %p, slab %p", o, slab));
187 while (o && DIFF(OpSLOT(o), OpSLOT(o)->opslot_next) < sz) {
188 DEBUG_S_warn((aTHX_ "Alas! too small"));
189 o = *(too = &o->op_next);
190 if (o) { DEBUG_S_warn((aTHX_ "found another free op at %p", o)); }
194 Zero(o, opsz, I32 *);
200 #define INIT_OPSLOT \
201 slot->opslot_slab = slab; \
202 slot->opslot_next = slab2->opslab_first; \
203 slab2->opslab_first = slot; \
204 o = &slot->opslot_op; \
207 /* The partially-filled slab is next in the chain. */
208 slab2 = slab->opslab_next ? slab->opslab_next : slab;
209 if ((space = DIFF(&slab2->opslab_slots, slab2->opslab_first)) < sz) {
210 /* Remaining space is too small. */
212 /* If we can fit a BASEOP, add it to the free chain, so as not
214 if (space >= SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P) {
215 slot = &slab2->opslab_slots;
217 o->op_type = OP_FREED;
218 o->op_next = slab->opslab_freed;
219 slab->opslab_freed = o;
222 /* Create a new slab. Make this one twice as big. */
223 slot = slab2->opslab_first;
224 while (slot->opslot_next) slot = slot->opslot_next;
225 slab2 = S_new_slab(aTHX_
226 (DIFF(slab2, slot)+1)*2 > PERL_MAX_SLAB_SIZE
228 : (DIFF(slab2, slot)+1)*2);
229 slab2->opslab_next = slab->opslab_next;
230 slab->opslab_next = slab2;
232 assert(DIFF(&slab2->opslab_slots, slab2->opslab_first) >= sz);
234 /* Create a new op slot */
235 slot = (OPSLOT *)((I32 **)slab2->opslab_first - sz);
236 assert(slot >= &slab2->opslab_slots);
237 if (DIFF(&slab2->opslab_slots, slot)
238 < SIZE_TO_PSIZE(sizeof(OP)) + OPSLOT_HEADER_P)
239 slot = &slab2->opslab_slots;
241 DEBUG_S_warn((aTHX_ "allocating op at %p, slab %p", o, slab));
247 #ifdef PERL_DEBUG_READONLY_OPS
249 Perl_Slab_to_ro(pTHX_ OPSLAB *slab)
251 PERL_ARGS_ASSERT_SLAB_TO_RO;
253 if (slab->opslab_readonly) return;
254 slab->opslab_readonly = 1;
255 for (; slab; slab = slab->opslab_next) {
256 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->ro %lu at %p\n",
257 (unsigned long) slab->opslab_size, slab));*/
258 if (mprotect(slab, slab->opslab_size * sizeof(I32 *), PROT_READ))
259 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", slab,
260 (unsigned long)slab->opslab_size, errno);
265 S_Slab_to_rw(pTHX_ void *op)
267 OP * const o = (OP *)op;
271 PERL_ARGS_ASSERT_SLAB_TO_RW;
273 if (!o->op_slabbed) return;
276 if (!slab->opslab_readonly) return;
278 for (; slab2; slab2 = slab2->opslab_next) {
279 /*DEBUG_U(PerlIO_printf(Perl_debug_log,"mprotect ->rw %lu at %p\n",
280 (unsigned long) size, slab2));*/
281 if (mprotect((void *)slab2, slab2->opslab_size * sizeof(I32 *),
282 PROT_READ|PROT_WRITE)) {
283 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab,
284 (unsigned long)slab2->opslab_size, errno);
287 slab->opslab_readonly = 0;
291 # define Slab_to_rw(op)
294 /* This cannot possibly be right, but it was copied from the old slab
295 allocator, to which it was originally added, without explanation, in
298 # define PerlMemShared PerlMem
302 Perl_Slab_Free(pTHX_ void *op)
305 OP * const o = (OP *)op;
308 PERL_ARGS_ASSERT_SLAB_FREE;
310 if (!o->op_slabbed) {
311 PerlMemShared_free(op);
316 /* If this op is already freed, our refcount will get screwy. */
317 assert(o->op_type != OP_FREED);
318 o->op_type = OP_FREED;
319 o->op_next = slab->opslab_freed;
320 slab->opslab_freed = o;
321 DEBUG_S_warn((aTHX_ "free op at %p, recorded in slab %p", o, slab));
322 OpslabREFCNT_dec_padok(slab);
326 Perl_opslab_free_nopad(pTHX_ OPSLAB *slab)
329 const bool havepad = !!PL_comppad;
330 PERL_ARGS_ASSERT_OPSLAB_FREE_NOPAD;
333 PAD_SAVE_SETNULLPAD();
340 Perl_opslab_free(pTHX_ OPSLAB *slab)
344 PERL_ARGS_ASSERT_OPSLAB_FREE;
345 DEBUG_S_warn((aTHX_ "freeing slab %p", slab));
346 assert(slab->opslab_refcnt == 1);
347 for (; slab; slab = slab2) {
348 slab2 = slab->opslab_next;
350 slab->opslab_refcnt = ~(size_t)0;
352 #ifdef PERL_DEBUG_READONLY_OPS
353 DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate slab at %p\n",
355 if (munmap(slab, slab->opslab_size * sizeof(I32 *))) {
356 perror("munmap failed");
360 PerlMemShared_free(slab);
366 Perl_opslab_force_free(pTHX_ OPSLAB *slab)
371 size_t savestack_count = 0;
373 PERL_ARGS_ASSERT_OPSLAB_FORCE_FREE;
376 for (slot = slab2->opslab_first;
378 slot = slot->opslot_next) {
379 if (slot->opslot_op.op_type != OP_FREED
380 && !(slot->opslot_op.op_savefree
386 assert(slot->opslot_op.op_slabbed);
387 slab->opslab_refcnt++; /* op_free may free slab */
388 op_free(&slot->opslot_op);
389 if (!--slab->opslab_refcnt) goto free;
392 } while ((slab2 = slab2->opslab_next));
393 /* > 1 because the CV still holds a reference count. */
394 if (slab->opslab_refcnt > 1) { /* still referenced by the savestack */
396 assert(savestack_count == slab->opslab_refcnt-1);
404 #ifdef PERL_DEBUG_READONLY_OPS
406 Perl_op_refcnt_inc(pTHX_ OP *o)
417 Perl_op_refcnt_dec(pTHX_ OP *o)
419 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
425 * In the following definition, the ", (OP*)0" is just to make the compiler
426 * think the expression is of the right type: croak actually does a Siglongjmp.
428 #define CHECKOP(type,o) \
429 ((PL_op_mask && PL_op_mask[type]) \
430 ? ( op_free((OP*)o), \
431 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
433 : PL_check[type](aTHX_ (OP*)o))
435 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
437 #define CHANGE_TYPE(o,type) \
439 o->op_type = (OPCODE)type; \
440 o->op_ppaddr = PL_ppaddr[type]; \
444 S_gv_ename(pTHX_ GV *gv)
446 SV* const tmpsv = sv_newmortal();
448 PERL_ARGS_ASSERT_GV_ENAME;
450 gv_efullname3(tmpsv, gv, NULL);
455 S_no_fh_allowed(pTHX_ OP *o)
457 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
459 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
465 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
467 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
468 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
469 SvUTF8(namesv) | flags);
474 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
476 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
477 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
482 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
484 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
486 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
491 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
493 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
495 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
496 SvUTF8(namesv) | flags);
501 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
503 PERL_ARGS_ASSERT_BAD_TYPE_PV;
505 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
506 (int)n, name, t, OP_DESC(kid)), flags);
510 S_bad_type_sv(pTHX_ I32 n, const char *t, SV *namesv, U32 flags, const OP *kid)
512 PERL_ARGS_ASSERT_BAD_TYPE_SV;
514 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
515 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
519 S_no_bareword_allowed(pTHX_ OP *o)
521 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
524 return; /* various ok barewords are hidden in extra OP_NULL */
525 qerror(Perl_mess(aTHX_
526 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
528 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
531 /* "register" allocation */
534 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
538 const bool is_our = (PL_parser->in_my == KEY_our);
540 PERL_ARGS_ASSERT_ALLOCMY;
542 if (flags & ~SVf_UTF8)
543 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
546 /* Until we're using the length for real, cross check that we're being
548 assert(strlen(name) == len);
550 /* complain about "my $<special_var>" etc etc */
554 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
555 (name[1] == '_' && (*name == '$' || len > 2))))
557 /* name[2] is true if strlen(name) > 2 */
558 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
559 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
560 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
561 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
562 PL_parser->in_my == KEY_state ? "state" : "my"));
564 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
565 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
569 /* allocate a spare slot and store the name in that slot */
571 off = pad_add_name_pvn(name, len,
572 (is_our ? padadd_OUR :
573 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
574 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
575 PL_parser->in_my_stash,
577 /* $_ is always in main::, even with our */
578 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
582 /* anon sub prototypes contains state vars should always be cloned,
583 * otherwise the state var would be shared between anon subs */
585 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
586 CvCLONE_on(PL_compcv);
592 =for apidoc alloccopstash
594 Available only under threaded builds, this function allocates an entry in
595 C<PL_stashpad> for the stash passed to it.
602 Perl_alloccopstash(pTHX_ HV *hv)
604 PADOFFSET off = 0, o = 1;
605 bool found_slot = FALSE;
607 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
609 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
611 for (; o < PL_stashpadmax; ++o) {
612 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
613 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
614 found_slot = TRUE, off = o;
617 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
618 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
619 off = PL_stashpadmax;
620 PL_stashpadmax += 10;
623 PL_stashpad[PL_stashpadix = off] = hv;
628 /* free the body of an op without examining its contents.
629 * Always use this rather than FreeOp directly */
632 S_op_destroy(pTHX_ OP *o)
634 if (o->op_latefree) {
642 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
644 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
650 Perl_op_free(pTHX_ OP *o)
655 /* Though ops may be freed twice, freeing the op after its slab is a
657 assert(!o || !o->op_slabbed || OpSLAB(o)->opslab_refcnt != ~(size_t)0);
658 /* During the forced freeing of ops after compilation failure, kidops
659 may be freed before their parents. */
660 if (!o || o->op_type == OP_FREED)
662 if (o->op_latefreed) {
669 if (o->op_private & OPpREFCOUNTED) {
680 refcnt = OpREFCNT_dec(o);
683 /* Need to find and remove any pattern match ops from the list
684 we maintain for reset(). */
685 find_and_forget_pmops(o);
695 /* Call the op_free hook if it has been set. Do it now so that it's called
696 * at the right time for refcounted ops, but still before all of the kids
700 if (o->op_flags & OPf_KIDS) {
701 register OP *kid, *nextkid;
702 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
703 nextkid = kid->op_sibling; /* Get before next freeing kid */
710 /* COP* is not cleared by op_clear() so that we may track line
711 * numbers etc even after null() */
712 if (type == OP_NEXTSTATE || type == OP_DBSTATE
713 || (type == OP_NULL /* the COP might have been null'ed */
714 && ((OPCODE)o->op_targ == OP_NEXTSTATE
715 || (OPCODE)o->op_targ == OP_DBSTATE))) {
720 type = (OPCODE)o->op_targ;
723 if (o->op_latefree) {
729 #ifdef DEBUG_LEAKING_SCALARS
736 Perl_op_clear(pTHX_ OP *o)
741 PERL_ARGS_ASSERT_OP_CLEAR;
744 mad_free(o->op_madprop);
749 switch (o->op_type) {
750 case OP_NULL: /* Was holding old type, if any. */
751 if (PL_madskills && o->op_targ != OP_NULL) {
752 o->op_type = (Optype)o->op_targ;
757 case OP_ENTEREVAL: /* Was holding hints. */
761 if (!(o->op_flags & OPf_REF)
762 || (PL_check[o->op_type] != Perl_ck_ftst))
769 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
774 /* It's possible during global destruction that the GV is freed
775 before the optree. Whilst the SvREFCNT_inc is happy to bump from
776 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
777 will trigger an assertion failure, because the entry to sv_clear
778 checks that the scalar is not already freed. A check of for
779 !SvIS_FREED(gv) turns out to be invalid, because during global
780 destruction the reference count can be forced down to zero
781 (with SVf_BREAK set). In which case raising to 1 and then
782 dropping to 0 triggers cleanup before it should happen. I
783 *think* that this might actually be a general, systematic,
784 weakness of the whole idea of SVf_BREAK, in that code *is*
785 allowed to raise and lower references during global destruction,
786 so any *valid* code that happens to do this during global
787 destruction might well trigger premature cleanup. */
788 bool still_valid = gv && SvREFCNT(gv);
791 SvREFCNT_inc_simple_void(gv);
793 if (cPADOPo->op_padix > 0) {
794 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
795 * may still exist on the pad */
796 pad_swipe(cPADOPo->op_padix, TRUE);
797 cPADOPo->op_padix = 0;
800 SvREFCNT_dec(cSVOPo->op_sv);
801 cSVOPo->op_sv = NULL;
804 int try_downgrade = SvREFCNT(gv) == 2;
807 gv_try_downgrade(gv);
811 case OP_METHOD_NAMED:
814 SvREFCNT_dec(cSVOPo->op_sv);
815 cSVOPo->op_sv = NULL;
818 Even if op_clear does a pad_free for the target of the op,
819 pad_free doesn't actually remove the sv that exists in the pad;
820 instead it lives on. This results in that it could be reused as
821 a target later on when the pad was reallocated.
824 pad_swipe(o->op_targ,1);
833 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
838 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
840 if (cPADOPo->op_padix > 0) {
841 pad_swipe(cPADOPo->op_padix, TRUE);
842 cPADOPo->op_padix = 0;
845 SvREFCNT_dec(cSVOPo->op_sv);
846 cSVOPo->op_sv = NULL;
850 PerlMemShared_free(cPVOPo->op_pv);
851 cPVOPo->op_pv = NULL;
855 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
859 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
860 /* No GvIN_PAD_off here, because other references may still
861 * exist on the pad */
862 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
865 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
871 if (!(cPMOPo->op_pmflags & PMf_CODELIST_PRIVATE))
872 op_free(cPMOPo->op_code_list);
873 cPMOPo->op_code_list = NULL;
874 forget_pmop(cPMOPo, 1);
875 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
876 /* we use the same protection as the "SAFE" version of the PM_ macros
877 * here since sv_clean_all might release some PMOPs
878 * after PL_regex_padav has been cleared
879 * and the clearing of PL_regex_padav needs to
880 * happen before sv_clean_all
883 if(PL_regex_pad) { /* We could be in destruction */
884 const IV offset = (cPMOPo)->op_pmoffset;
885 ReREFCNT_dec(PM_GETRE(cPMOPo));
886 PL_regex_pad[offset] = &PL_sv_undef;
887 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
891 ReREFCNT_dec(PM_GETRE(cPMOPo));
892 PM_SETRE(cPMOPo, NULL);
898 if (o->op_targ > 0) {
899 pad_free(o->op_targ);
905 S_cop_free(pTHX_ COP* cop)
907 PERL_ARGS_ASSERT_COP_FREE;
910 if (! specialWARN(cop->cop_warnings))
911 PerlMemShared_free(cop->cop_warnings);
912 cophh_free(CopHINTHASH_get(cop));
916 S_forget_pmop(pTHX_ PMOP *const o
922 HV * const pmstash = PmopSTASH(o);
924 PERL_ARGS_ASSERT_FORGET_PMOP;
926 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
927 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
929 PMOP **const array = (PMOP**) mg->mg_ptr;
930 U32 count = mg->mg_len / sizeof(PMOP**);
935 /* Found it. Move the entry at the end to overwrite it. */
936 array[i] = array[--count];
937 mg->mg_len = count * sizeof(PMOP**);
938 /* Could realloc smaller at this point always, but probably
939 not worth it. Probably worth free()ing if we're the
942 Safefree(mg->mg_ptr);
959 S_find_and_forget_pmops(pTHX_ OP *o)
961 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
963 if (o->op_flags & OPf_KIDS) {
964 OP *kid = cUNOPo->op_first;
966 switch (kid->op_type) {
971 forget_pmop((PMOP*)kid, 0);
973 find_and_forget_pmops(kid);
974 kid = kid->op_sibling;
980 Perl_op_null(pTHX_ OP *o)
984 PERL_ARGS_ASSERT_OP_NULL;
986 if (o->op_type == OP_NULL)
990 o->op_targ = o->op_type;
991 o->op_type = OP_NULL;
992 o->op_ppaddr = PL_ppaddr[OP_NULL];
996 Perl_op_refcnt_lock(pTHX)
1004 Perl_op_refcnt_unlock(pTHX)
1007 PERL_UNUSED_CONTEXT;
1011 /* Contextualizers */
1014 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
1016 Applies a syntactic context to an op tree representing an expression.
1017 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
1018 or C<G_VOID> to specify the context to apply. The modified op tree
1025 Perl_op_contextualize(pTHX_ OP *o, I32 context)
1027 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
1029 case G_SCALAR: return scalar(o);
1030 case G_ARRAY: return list(o);
1031 case G_VOID: return scalarvoid(o);
1033 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
1040 =head1 Optree Manipulation Functions
1042 =for apidoc Am|OP*|op_linklist|OP *o
1043 This function is the implementation of the L</LINKLIST> macro. It should
1044 not be called directly.
1050 Perl_op_linklist(pTHX_ OP *o)
1054 PERL_ARGS_ASSERT_OP_LINKLIST;
1059 /* establish postfix order */
1060 first = cUNOPo->op_first;
1063 o->op_next = LINKLIST(first);
1066 if (kid->op_sibling) {
1067 kid->op_next = LINKLIST(kid->op_sibling);
1068 kid = kid->op_sibling;
1082 S_scalarkids(pTHX_ OP *o)
1084 if (o && o->op_flags & OPf_KIDS) {
1086 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1093 S_scalarboolean(pTHX_ OP *o)
1097 PERL_ARGS_ASSERT_SCALARBOOLEAN;
1099 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
1100 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
1101 if (ckWARN(WARN_SYNTAX)) {
1102 const line_t oldline = CopLINE(PL_curcop);
1104 if (PL_parser && PL_parser->copline != NOLINE)
1105 CopLINE_set(PL_curcop, PL_parser->copline);
1106 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
1107 CopLINE_set(PL_curcop, oldline);
1114 Perl_scalar(pTHX_ OP *o)
1119 /* assumes no premature commitment */
1120 if (!o || (PL_parser && PL_parser->error_count)
1121 || (o->op_flags & OPf_WANT)
1122 || o->op_type == OP_RETURN)
1127 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1129 switch (o->op_type) {
1131 scalar(cBINOPo->op_first);
1136 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1146 if (o->op_flags & OPf_KIDS) {
1147 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1153 kid = cLISTOPo->op_first;
1155 kid = kid->op_sibling;
1158 OP *sib = kid->op_sibling;
1159 if (sib && kid->op_type != OP_LEAVEWHEN)
1165 PL_curcop = &PL_compiling;
1170 kid = cLISTOPo->op_first;
1173 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1180 Perl_scalarvoid(pTHX_ OP *o)
1184 const char* useless = NULL;
1185 U32 useless_is_utf8 = 0;
1189 PERL_ARGS_ASSERT_SCALARVOID;
1191 /* trailing mad null ops don't count as "there" for void processing */
1193 o->op_type != OP_NULL &&
1195 o->op_sibling->op_type == OP_NULL)
1198 for (sib = o->op_sibling;
1199 sib && sib->op_type == OP_NULL;
1200 sib = sib->op_sibling) ;
1206 if (o->op_type == OP_NEXTSTATE
1207 || o->op_type == OP_DBSTATE
1208 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1209 || o->op_targ == OP_DBSTATE)))
1210 PL_curcop = (COP*)o; /* for warning below */
1212 /* assumes no premature commitment */
1213 want = o->op_flags & OPf_WANT;
1214 if ((want && want != OPf_WANT_SCALAR)
1215 || (PL_parser && PL_parser->error_count)
1216 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1221 if ((o->op_private & OPpTARGET_MY)
1222 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1224 return scalar(o); /* As if inside SASSIGN */
1227 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1229 switch (o->op_type) {
1231 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1235 if (o->op_flags & OPf_STACKED)
1239 if (o->op_private == 4)
1264 case OP_AELEMFAST_LEX:
1283 case OP_GETSOCKNAME:
1284 case OP_GETPEERNAME:
1289 case OP_GETPRIORITY:
1314 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1315 /* Otherwise it's "Useless use of grep iterator" */
1316 useless = OP_DESC(o);
1320 kid = cLISTOPo->op_first;
1321 if (kid && kid->op_type == OP_PUSHRE
1323 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1325 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1327 useless = OP_DESC(o);
1331 kid = cUNOPo->op_first;
1332 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1333 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1336 useless = "negative pattern binding (!~)";
1340 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1341 useless = "non-destructive substitution (s///r)";
1345 useless = "non-destructive transliteration (tr///r)";
1352 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1353 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1354 useless = "a variable";
1359 if (cSVOPo->op_private & OPpCONST_STRICT)
1360 no_bareword_allowed(o);
1362 if (ckWARN(WARN_VOID)) {
1363 /* don't warn on optimised away booleans, eg
1364 * use constant Foo, 5; Foo || print; */
1365 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1367 /* the constants 0 and 1 are permitted as they are
1368 conventionally used as dummies in constructs like
1369 1 while some_condition_with_side_effects; */
1370 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1372 else if (SvPOK(sv)) {
1373 /* perl4's way of mixing documentation and code
1374 (before the invention of POD) was based on a
1375 trick to mix nroff and perl code. The trick was
1376 built upon these three nroff macros being used in
1377 void context. The pink camel has the details in
1378 the script wrapman near page 319. */
1379 const char * const maybe_macro = SvPVX_const(sv);
1380 if (strnEQ(maybe_macro, "di", 2) ||
1381 strnEQ(maybe_macro, "ds", 2) ||
1382 strnEQ(maybe_macro, "ig", 2))
1385 SV * const dsv = newSVpvs("");
1386 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1388 pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
1389 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
1391 useless = SvPV_nolen(msv);
1392 useless_is_utf8 = SvUTF8(msv);
1395 else if (SvOK(sv)) {
1396 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1397 "a constant (%"SVf")", sv));
1398 useless = SvPV_nolen(msv);
1401 useless = "a constant (undef)";
1404 op_null(o); /* don't execute or even remember it */
1408 o->op_type = OP_PREINC; /* pre-increment is faster */
1409 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1413 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1414 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1418 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1419 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1423 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1424 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1429 UNOP *refgen, *rv2cv;
1432 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1435 rv2gv = ((BINOP *)o)->op_last;
1436 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1439 refgen = (UNOP *)((BINOP *)o)->op_first;
1441 if (!refgen || refgen->op_type != OP_REFGEN)
1444 exlist = (LISTOP *)refgen->op_first;
1445 if (!exlist || exlist->op_type != OP_NULL
1446 || exlist->op_targ != OP_LIST)
1449 if (exlist->op_first->op_type != OP_PUSHMARK)
1452 rv2cv = (UNOP*)exlist->op_last;
1454 if (rv2cv->op_type != OP_RV2CV)
1457 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1458 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1459 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1461 o->op_private |= OPpASSIGN_CV_TO_GV;
1462 rv2gv->op_private |= OPpDONT_INIT_GV;
1463 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1475 kid = cLOGOPo->op_first;
1476 if (kid->op_type == OP_NOT
1477 && (kid->op_flags & OPf_KIDS)
1479 if (o->op_type == OP_AND) {
1481 o->op_ppaddr = PL_ppaddr[OP_OR];
1483 o->op_type = OP_AND;
1484 o->op_ppaddr = PL_ppaddr[OP_AND];
1493 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1498 if (o->op_flags & OPf_STACKED)
1505 if (!(o->op_flags & OPf_KIDS))
1516 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1526 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %"SVf" in void context",
1527 newSVpvn_flags(useless, strlen(useless),
1528 SVs_TEMP | ( useless_is_utf8 ? SVf_UTF8 : 0 )));
1533 S_listkids(pTHX_ OP *o)
1535 if (o && o->op_flags & OPf_KIDS) {
1537 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1544 Perl_list(pTHX_ OP *o)
1549 /* assumes no premature commitment */
1550 if (!o || (o->op_flags & OPf_WANT)
1551 || (PL_parser && PL_parser->error_count)
1552 || o->op_type == OP_RETURN)
1557 if ((o->op_private & OPpTARGET_MY)
1558 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1560 return o; /* As if inside SASSIGN */
1563 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1565 switch (o->op_type) {
1568 list(cBINOPo->op_first);
1573 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1581 if (!(o->op_flags & OPf_KIDS))
1583 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1584 list(cBINOPo->op_first);
1585 return gen_constant_list(o);
1592 kid = cLISTOPo->op_first;
1594 kid = kid->op_sibling;
1597 OP *sib = kid->op_sibling;
1598 if (sib && kid->op_type != OP_LEAVEWHEN)
1604 PL_curcop = &PL_compiling;
1608 kid = cLISTOPo->op_first;
1615 S_scalarseq(pTHX_ OP *o)
1619 const OPCODE type = o->op_type;
1621 if (type == OP_LINESEQ || type == OP_SCOPE ||
1622 type == OP_LEAVE || type == OP_LEAVETRY)
1625 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1626 if (kid->op_sibling) {
1630 PL_curcop = &PL_compiling;
1632 o->op_flags &= ~OPf_PARENS;
1633 if (PL_hints & HINT_BLOCK_SCOPE)
1634 o->op_flags |= OPf_PARENS;
1637 o = newOP(OP_STUB, 0);
1642 S_modkids(pTHX_ OP *o, I32 type)
1644 if (o && o->op_flags & OPf_KIDS) {
1646 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1647 op_lvalue(kid, type);
1653 =for apidoc finalize_optree
1655 This function finalizes the optree. Should be called directly after
1656 the complete optree is built. It does some additional
1657 checking which can't be done in the normal ck_xxx functions and makes
1658 the tree thread-safe.
1663 Perl_finalize_optree(pTHX_ OP* o)
1665 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1668 SAVEVPTR(PL_curcop);
1676 S_finalize_op(pTHX_ OP* o)
1678 PERL_ARGS_ASSERT_FINALIZE_OP;
1680 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1682 /* Make sure mad ops are also thread-safe */
1683 MADPROP *mp = o->op_madprop;
1685 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1686 OP *prop_op = (OP *) mp->mad_val;
1687 /* We only need "Relocate sv to the pad for thread safety.", but this
1688 easiest way to make sure it traverses everything */
1689 if (prop_op->op_type == OP_CONST)
1690 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1691 finalize_op(prop_op);
1698 switch (o->op_type) {
1701 PL_curcop = ((COP*)o); /* for warnings */
1705 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1706 && ckWARN(WARN_SYNTAX))
1708 if (o->op_sibling->op_sibling) {
1709 const OPCODE type = o->op_sibling->op_sibling->op_type;
1710 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1711 const line_t oldline = CopLINE(PL_curcop);
1712 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1713 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1714 "Statement unlikely to be reached");
1715 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1716 "\t(Maybe you meant system() when you said exec()?)\n");
1717 CopLINE_set(PL_curcop, oldline);
1724 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1725 GV * const gv = cGVOPo_gv;
1726 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1727 /* XXX could check prototype here instead of just carping */
1728 SV * const sv = sv_newmortal();
1729 gv_efullname3(sv, gv, NULL);
1730 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1731 "%"SVf"() called too early to check prototype",
1738 if (cSVOPo->op_private & OPpCONST_STRICT)
1739 no_bareword_allowed(o);
1743 case OP_METHOD_NAMED:
1744 /* Relocate sv to the pad for thread safety.
1745 * Despite being a "constant", the SV is written to,
1746 * for reference counts, sv_upgrade() etc. */
1747 if (cSVOPo->op_sv) {
1748 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
1749 if (o->op_type != OP_METHOD_NAMED &&
1750 (SvPADTMP(cSVOPo->op_sv) || SvPADMY(cSVOPo->op_sv)))
1752 /* If op_sv is already a PADTMP/MY then it is being used by
1753 * some pad, so make a copy. */
1754 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
1755 SvREADONLY_on(PAD_SVl(ix));
1756 SvREFCNT_dec(cSVOPo->op_sv);
1758 else if (o->op_type != OP_METHOD_NAMED
1759 && cSVOPo->op_sv == &PL_sv_undef) {
1760 /* PL_sv_undef is hack - it's unsafe to store it in the
1761 AV that is the pad, because av_fetch treats values of
1762 PL_sv_undef as a "free" AV entry and will merrily
1763 replace them with a new SV, causing pad_alloc to think
1764 that this pad slot is free. (When, clearly, it is not)
1766 SvOK_off(PAD_SVl(ix));
1767 SvPADTMP_on(PAD_SVl(ix));
1768 SvREADONLY_on(PAD_SVl(ix));
1771 SvREFCNT_dec(PAD_SVl(ix));
1772 SvPADTMP_on(cSVOPo->op_sv);
1773 PAD_SETSV(ix, cSVOPo->op_sv);
1774 /* XXX I don't know how this isn't readonly already. */
1775 SvREADONLY_on(PAD_SVl(ix));
1777 cSVOPo->op_sv = NULL;
1788 const char *key = NULL;
1791 if (((BINOP*)o)->op_last->op_type != OP_CONST)
1794 /* Make the CONST have a shared SV */
1795 svp = cSVOPx_svp(((BINOP*)o)->op_last);
1796 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv))
1797 && SvTYPE(sv) < SVt_PVMG && !SvROK(sv)) {
1798 key = SvPV_const(sv, keylen);
1799 lexname = newSVpvn_share(key,
1800 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
1806 if ((o->op_private & (OPpLVAL_INTRO)))
1809 rop = (UNOP*)((BINOP*)o)->op_first;
1810 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
1812 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
1813 if (!SvPAD_TYPED(lexname))
1815 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1816 if (!fields || !GvHV(*fields))
1818 key = SvPV_const(*svp, keylen);
1819 if (!hv_fetch(GvHV(*fields), key,
1820 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1821 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1822 "in variable %"SVf" of type %"HEKf,
1823 SVfARG(*svp), SVfARG(lexname),
1824 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1836 SVOP *first_key_op, *key_op;
1838 if ((o->op_private & (OPpLVAL_INTRO))
1839 /* I bet there's always a pushmark... */
1840 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
1841 /* hmmm, no optimization if list contains only one key. */
1843 rop = (UNOP*)((LISTOP*)o)->op_last;
1844 if (rop->op_type != OP_RV2HV)
1846 if (rop->op_first->op_type == OP_PADSV)
1847 /* @$hash{qw(keys here)} */
1848 rop = (UNOP*)rop->op_first;
1850 /* @{$hash}{qw(keys here)} */
1851 if (rop->op_first->op_type == OP_SCOPE
1852 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1854 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1860 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
1861 if (!SvPAD_TYPED(lexname))
1863 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1864 if (!fields || !GvHV(*fields))
1866 /* Again guessing that the pushmark can be jumped over.... */
1867 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
1868 ->op_first->op_sibling;
1869 for (key_op = first_key_op; key_op;
1870 key_op = (SVOP*)key_op->op_sibling) {
1871 if (key_op->op_type != OP_CONST)
1873 svp = cSVOPx_svp(key_op);
1874 key = SvPV_const(*svp, keylen);
1875 if (!hv_fetch(GvHV(*fields), key,
1876 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1877 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1878 "in variable %"SVf" of type %"HEKf,
1879 SVfARG(*svp), SVfARG(lexname),
1880 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1886 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1887 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1894 if (o->op_flags & OPf_KIDS) {
1896 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1902 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
1904 Propagate lvalue ("modifiable") context to an op and its children.
1905 I<type> represents the context type, roughly based on the type of op that
1906 would do the modifying, although C<local()> is represented by OP_NULL,
1907 because it has no op type of its own (it is signalled by a flag on
1910 This function detects things that can't be modified, such as C<$x+1>, and
1911 generates errors for them. For example, C<$x+1 = 2> would cause it to be
1912 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
1914 It also flags things that need to behave specially in an lvalue context,
1915 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
1921 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
1925 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1928 if (!o || (PL_parser && PL_parser->error_count))
1931 if ((o->op_private & OPpTARGET_MY)
1932 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1937 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
1939 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
1941 switch (o->op_type) {
1946 if ((o->op_flags & OPf_PARENS) || PL_madskills)
1950 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
1951 !(o->op_flags & OPf_STACKED)) {
1952 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1953 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
1954 poses, so we need it clear. */
1955 o->op_private &= ~1;
1956 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1957 assert(cUNOPo->op_first->op_type == OP_NULL);
1958 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1961 else { /* lvalue subroutine call */
1962 o->op_private |= OPpLVAL_INTRO
1963 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
1964 PL_modcount = RETURN_UNLIMITED_NUMBER;
1965 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1966 /* Potential lvalue context: */
1967 o->op_private |= OPpENTERSUB_INARGS;
1970 else { /* Compile-time error message: */
1971 OP *kid = cUNOPo->op_first;
1974 if (kid->op_type != OP_PUSHMARK) {
1975 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1977 "panic: unexpected lvalue entersub "
1978 "args: type/targ %ld:%"UVuf,
1979 (long)kid->op_type, (UV)kid->op_targ);
1980 kid = kLISTOP->op_first;
1982 while (kid->op_sibling)
1983 kid = kid->op_sibling;
1984 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1985 break; /* Postpone until runtime */
1988 kid = kUNOP->op_first;
1989 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1990 kid = kUNOP->op_first;
1991 if (kid->op_type == OP_NULL)
1993 "Unexpected constant lvalue entersub "
1994 "entry via type/targ %ld:%"UVuf,
1995 (long)kid->op_type, (UV)kid->op_targ);
1996 if (kid->op_type != OP_GV) {
2000 cv = GvCV(kGVOP_gv);
2010 if (flags & OP_LVALUE_NO_CROAK) return NULL;
2011 /* grep, foreach, subcalls, refgen */
2012 if (type == OP_GREPSTART || type == OP_ENTERSUB
2013 || type == OP_REFGEN || type == OP_LEAVESUBLV)
2015 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
2016 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
2018 : (o->op_type == OP_ENTERSUB
2019 ? "non-lvalue subroutine call"
2021 type ? PL_op_desc[type] : "local"));
2035 case OP_RIGHT_SHIFT:
2044 if (!(o->op_flags & OPf_STACKED))
2051 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2052 op_lvalue(kid, type);
2057 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
2058 PL_modcount = RETURN_UNLIMITED_NUMBER;
2059 return o; /* Treat \(@foo) like ordinary list. */
2063 if (scalar_mod_type(o, type))
2065 ref(cUNOPo->op_first, o->op_type);
2069 if (type == OP_LEAVESUBLV)
2070 o->op_private |= OPpMAYBE_LVSUB;
2076 PL_modcount = RETURN_UNLIMITED_NUMBER;
2079 PL_hints |= HINT_BLOCK_SCOPE;
2080 if (type == OP_LEAVESUBLV)
2081 o->op_private |= OPpMAYBE_LVSUB;
2085 ref(cUNOPo->op_first, o->op_type);
2089 PL_hints |= HINT_BLOCK_SCOPE;
2098 case OP_AELEMFAST_LEX:
2105 PL_modcount = RETURN_UNLIMITED_NUMBER;
2106 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
2107 return o; /* Treat \(@foo) like ordinary list. */
2108 if (scalar_mod_type(o, type))
2110 if (type == OP_LEAVESUBLV)
2111 o->op_private |= OPpMAYBE_LVSUB;
2115 if (!type) /* local() */
2116 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
2117 PAD_COMPNAME_SV(o->op_targ));
2126 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2130 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2136 if (type == OP_LEAVESUBLV)
2137 o->op_private |= OPpMAYBE_LVSUB;
2138 pad_free(o->op_targ);
2139 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
2140 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
2141 if (o->op_flags & OPf_KIDS)
2142 op_lvalue(cBINOPo->op_first->op_sibling, type);
2147 ref(cBINOPo->op_first, o->op_type);
2148 if (type == OP_ENTERSUB &&
2149 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2150 o->op_private |= OPpLVAL_DEFER;
2151 if (type == OP_LEAVESUBLV)
2152 o->op_private |= OPpMAYBE_LVSUB;
2162 if (o->op_flags & OPf_KIDS)
2163 op_lvalue(cLISTOPo->op_last, type);
2168 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2170 else if (!(o->op_flags & OPf_KIDS))
2172 if (o->op_targ != OP_LIST) {
2173 op_lvalue(cBINOPo->op_first, type);
2179 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2180 /* elements might be in void context because the list is
2181 in scalar context or because they are attribute sub calls */
2182 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2183 op_lvalue(kid, type);
2187 if (type != OP_LEAVESUBLV)
2189 break; /* op_lvalue()ing was handled by ck_return() */
2195 /* [20011101.069] File test operators interpret OPf_REF to mean that
2196 their argument is a filehandle; thus \stat(".") should not set
2198 if (type == OP_REFGEN &&
2199 PL_check[o->op_type] == Perl_ck_ftst)
2202 if (type != OP_LEAVESUBLV)
2203 o->op_flags |= OPf_MOD;
2205 if (type == OP_AASSIGN || type == OP_SASSIGN)
2206 o->op_flags |= OPf_SPECIAL|OPf_REF;
2207 else if (!type) { /* local() */
2210 o->op_private |= OPpLVAL_INTRO;
2211 o->op_flags &= ~OPf_SPECIAL;
2212 PL_hints |= HINT_BLOCK_SCOPE;
2217 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2218 "Useless localization of %s", OP_DESC(o));
2221 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2222 && type != OP_LEAVESUBLV)
2223 o->op_flags |= OPf_REF;
2228 S_scalar_mod_type(const OP *o, I32 type)
2233 if (o && o->op_type == OP_RV2GV)
2257 case OP_RIGHT_SHIFT:
2278 S_is_handle_constructor(const OP *o, I32 numargs)
2280 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2282 switch (o->op_type) {
2290 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2303 S_refkids(pTHX_ OP *o, I32 type)
2305 if (o && o->op_flags & OPf_KIDS) {
2307 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2314 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2319 PERL_ARGS_ASSERT_DOREF;
2321 if (!o || (PL_parser && PL_parser->error_count))
2324 switch (o->op_type) {
2326 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2327 !(o->op_flags & OPf_STACKED)) {
2328 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2329 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2330 assert(cUNOPo->op_first->op_type == OP_NULL);
2331 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2332 o->op_flags |= OPf_SPECIAL;
2333 o->op_private &= ~1;
2335 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2336 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2337 : type == OP_RV2HV ? OPpDEREF_HV
2339 o->op_flags |= OPf_MOD;
2345 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2346 doref(kid, type, set_op_ref);
2349 if (type == OP_DEFINED)
2350 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2351 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2354 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2355 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2356 : type == OP_RV2HV ? OPpDEREF_HV
2358 o->op_flags |= OPf_MOD;
2365 o->op_flags |= OPf_REF;
2368 if (type == OP_DEFINED)
2369 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2370 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2376 o->op_flags |= OPf_REF;
2381 if (!(o->op_flags & OPf_KIDS))
2383 doref(cBINOPo->op_first, type, set_op_ref);
2387 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2388 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2389 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2390 : type == OP_RV2HV ? OPpDEREF_HV
2392 o->op_flags |= OPf_MOD;
2402 if (!(o->op_flags & OPf_KIDS))
2404 doref(cLISTOPo->op_last, type, set_op_ref);
2414 S_dup_attrlist(pTHX_ OP *o)
2419 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2421 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2422 * where the first kid is OP_PUSHMARK and the remaining ones
2423 * are OP_CONST. We need to push the OP_CONST values.
2425 if (o->op_type == OP_CONST)
2426 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2428 else if (o->op_type == OP_NULL)
2432 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2434 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2435 if (o->op_type == OP_CONST)
2436 rop = op_append_elem(OP_LIST, rop,
2437 newSVOP(OP_CONST, o->op_flags,
2438 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2445 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
2450 PERL_ARGS_ASSERT_APPLY_ATTRS;
2452 /* fake up C<use attributes $pkg,$rv,@attrs> */
2453 ENTER; /* need to protect against side-effects of 'use' */
2454 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2456 #define ATTRSMODULE "attributes"
2457 #define ATTRSMODULE_PM "attributes.pm"
2460 /* Don't force the C<use> if we don't need it. */
2461 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2462 if (svp && *svp != &PL_sv_undef)
2463 NOOP; /* already in %INC */
2465 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2466 newSVpvs(ATTRSMODULE), NULL);
2469 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2470 newSVpvs(ATTRSMODULE),
2472 op_prepend_elem(OP_LIST,
2473 newSVOP(OP_CONST, 0, stashsv),
2474 op_prepend_elem(OP_LIST,
2475 newSVOP(OP_CONST, 0,
2477 dup_attrlist(attrs))));
2483 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2486 OP *pack, *imop, *arg;
2489 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2494 assert(target->op_type == OP_PADSV ||
2495 target->op_type == OP_PADHV ||
2496 target->op_type == OP_PADAV);
2498 /* Ensure that attributes.pm is loaded. */
2499 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
2501 /* Need package name for method call. */
2502 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2504 /* Build up the real arg-list. */
2505 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2507 arg = newOP(OP_PADSV, 0);
2508 arg->op_targ = target->op_targ;
2509 arg = op_prepend_elem(OP_LIST,
2510 newSVOP(OP_CONST, 0, stashsv),
2511 op_prepend_elem(OP_LIST,
2512 newUNOP(OP_REFGEN, 0,
2513 op_lvalue(arg, OP_REFGEN)),
2514 dup_attrlist(attrs)));
2516 /* Fake up a method call to import */
2517 meth = newSVpvs_share("import");
2518 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2519 op_append_elem(OP_LIST,
2520 op_prepend_elem(OP_LIST, pack, list(arg)),
2521 newSVOP(OP_METHOD_NAMED, 0, meth)));
2523 /* Combine the ops. */
2524 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2528 =notfor apidoc apply_attrs_string
2530 Attempts to apply a list of attributes specified by the C<attrstr> and
2531 C<len> arguments to the subroutine identified by the C<cv> argument which
2532 is expected to be associated with the package identified by the C<stashpv>
2533 argument (see L<attributes>). It gets this wrong, though, in that it
2534 does not correctly identify the boundaries of the individual attribute
2535 specifications within C<attrstr>. This is not really intended for the
2536 public API, but has to be listed here for systems such as AIX which
2537 need an explicit export list for symbols. (It's called from XS code
2538 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2539 to respect attribute syntax properly would be welcome.
2545 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2546 const char *attrstr, STRLEN len)
2550 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2553 len = strlen(attrstr);
2557 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2559 const char * const sstr = attrstr;
2560 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2561 attrs = op_append_elem(OP_LIST, attrs,
2562 newSVOP(OP_CONST, 0,
2563 newSVpvn(sstr, attrstr-sstr)));
2567 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2568 newSVpvs(ATTRSMODULE),
2569 NULL, op_prepend_elem(OP_LIST,
2570 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2571 op_prepend_elem(OP_LIST,
2572 newSVOP(OP_CONST, 0,
2573 newRV(MUTABLE_SV(cv))),
2578 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2582 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2584 PERL_ARGS_ASSERT_MY_KID;
2586 if (!o || (PL_parser && PL_parser->error_count))
2590 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2591 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2595 if (type == OP_LIST) {
2597 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2598 my_kid(kid, attrs, imopsp);
2600 } else if (type == OP_UNDEF || type == OP_STUB) {
2602 } else if (type == OP_RV2SV || /* "our" declaration */
2604 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2605 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2606 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2608 PL_parser->in_my == KEY_our
2610 : PL_parser->in_my == KEY_state ? "state" : "my"));
2612 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2613 PL_parser->in_my = FALSE;
2614 PL_parser->in_my_stash = NULL;
2615 apply_attrs(GvSTASH(gv),
2616 (type == OP_RV2SV ? GvSV(gv) :
2617 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2618 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2621 o->op_private |= OPpOUR_INTRO;
2624 else if (type != OP_PADSV &&
2627 type != OP_PUSHMARK)
2629 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2631 PL_parser->in_my == KEY_our
2633 : PL_parser->in_my == KEY_state ? "state" : "my"));
2636 else if (attrs && type != OP_PUSHMARK) {
2639 PL_parser->in_my = FALSE;
2640 PL_parser->in_my_stash = NULL;
2642 /* check for C<my Dog $spot> when deciding package */
2643 stash = PAD_COMPNAME_TYPE(o->op_targ);
2645 stash = PL_curstash;
2646 apply_attrs_my(stash, o, attrs, imopsp);
2648 o->op_flags |= OPf_MOD;
2649 o->op_private |= OPpLVAL_INTRO;
2651 o->op_private |= OPpPAD_STATE;
2656 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2660 int maybe_scalar = 0;
2662 PERL_ARGS_ASSERT_MY_ATTRS;
2664 /* [perl #17376]: this appears to be premature, and results in code such as
2665 C< our(%x); > executing in list mode rather than void mode */
2667 if (o->op_flags & OPf_PARENS)
2677 o = my_kid(o, attrs, &rops);
2679 if (maybe_scalar && o->op_type == OP_PADSV) {
2680 o = scalar(op_append_list(OP_LIST, rops, o));
2681 o->op_private |= OPpLVAL_INTRO;
2684 /* The listop in rops might have a pushmark at the beginning,
2685 which will mess up list assignment. */
2686 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2687 if (rops->op_type == OP_LIST &&
2688 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2690 OP * const pushmark = lrops->op_first;
2691 lrops->op_first = pushmark->op_sibling;
2694 o = op_append_list(OP_LIST, o, rops);
2697 PL_parser->in_my = FALSE;
2698 PL_parser->in_my_stash = NULL;
2703 Perl_sawparens(pTHX_ OP *o)
2705 PERL_UNUSED_CONTEXT;
2707 o->op_flags |= OPf_PARENS;
2712 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2716 const OPCODE ltype = left->op_type;
2717 const OPCODE rtype = right->op_type;
2719 PERL_ARGS_ASSERT_BIND_MATCH;
2721 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2722 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2724 const char * const desc
2726 rtype == OP_SUBST || rtype == OP_TRANS
2727 || rtype == OP_TRANSR
2729 ? (int)rtype : OP_MATCH];
2730 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2733 (ltype == OP_RV2AV || ltype == OP_RV2HV)
2734 ? cUNOPx(left)->op_first->op_type == OP_GV
2735 && (gv = cGVOPx_gv(cUNOPx(left)->op_first))
2736 ? varname(gv, isary ? '@' : '%', 0, NULL, 0, 1)
2739 (GV *)PL_compcv, isary ? '@' : '%', left->op_targ, NULL, 0, 1
2742 Perl_warner(aTHX_ packWARN(WARN_MISC),
2743 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2746 const char * const sample = (isary
2747 ? "@array" : "%hash");
2748 Perl_warner(aTHX_ packWARN(WARN_MISC),
2749 "Applying %s to %s will act on scalar(%s)",
2750 desc, sample, sample);
2754 if (rtype == OP_CONST &&
2755 cSVOPx(right)->op_private & OPpCONST_BARE &&
2756 cSVOPx(right)->op_private & OPpCONST_STRICT)
2758 no_bareword_allowed(right);
2761 /* !~ doesn't make sense with /r, so error on it for now */
2762 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2764 yyerror("Using !~ with s///r doesn't make sense");
2765 if (rtype == OP_TRANSR && type == OP_NOT)
2766 yyerror("Using !~ with tr///r doesn't make sense");
2768 ismatchop = (rtype == OP_MATCH ||
2769 rtype == OP_SUBST ||
2770 rtype == OP_TRANS || rtype == OP_TRANSR)
2771 && !(right->op_flags & OPf_SPECIAL);
2772 if (ismatchop && right->op_private & OPpTARGET_MY) {
2774 right->op_private &= ~OPpTARGET_MY;
2776 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2779 right->op_flags |= OPf_STACKED;
2780 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
2781 ! (rtype == OP_TRANS &&
2782 right->op_private & OPpTRANS_IDENTICAL) &&
2783 ! (rtype == OP_SUBST &&
2784 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2785 newleft = op_lvalue(left, rtype);
2788 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
2789 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2791 o = op_prepend_elem(rtype, scalar(newleft), right);
2793 return newUNOP(OP_NOT, 0, scalar(o));
2797 return bind_match(type, left,
2798 pmruntime(newPMOP(OP_MATCH, 0), right, 0, 0));
2802 Perl_invert(pTHX_ OP *o)
2806 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2810 =for apidoc Amx|OP *|op_scope|OP *o
2812 Wraps up an op tree with some additional ops so that at runtime a dynamic
2813 scope will be created. The original ops run in the new dynamic scope,
2814 and then, provided that they exit normally, the scope will be unwound.
2815 The additional ops used to create and unwind the dynamic scope will
2816 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
2817 instead if the ops are simple enough to not need the full dynamic scope
2824 Perl_op_scope(pTHX_ OP *o)
2828 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2829 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2830 o->op_type = OP_LEAVE;
2831 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2833 else if (o->op_type == OP_LINESEQ) {
2835 o->op_type = OP_SCOPE;
2836 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2837 kid = ((LISTOP*)o)->op_first;
2838 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2841 /* The following deals with things like 'do {1 for 1}' */
2842 kid = kid->op_sibling;
2844 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2849 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2855 Perl_block_start(pTHX_ int full)
2858 const int retval = PL_savestack_ix;
2860 pad_block_start(full);
2862 PL_hints &= ~HINT_BLOCK_SCOPE;
2863 SAVECOMPILEWARNINGS();
2864 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2866 CALL_BLOCK_HOOKS(bhk_start, full);
2872 Perl_block_end(pTHX_ I32 floor, OP *seq)
2875 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2876 OP* retval = scalarseq(seq);
2878 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
2881 CopHINTS_set(&PL_compiling, PL_hints);
2883 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2886 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
2892 =head1 Compile-time scope hooks
2894 =for apidoc Aox||blockhook_register
2896 Register a set of hooks to be called when the Perl lexical scope changes
2897 at compile time. See L<perlguts/"Compile-time scope hooks">.
2903 Perl_blockhook_register(pTHX_ BHK *hk)
2905 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
2907 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
2914 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
2915 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2916 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2919 OP * const o = newOP(OP_PADSV, 0);
2920 o->op_targ = offset;
2926 Perl_newPROG(pTHX_ OP *o)
2930 PERL_ARGS_ASSERT_NEWPROG;
2937 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2938 ((PL_in_eval & EVAL_KEEPERR)
2939 ? OPf_SPECIAL : 0), o);
2941 cx = &cxstack[cxstack_ix];
2942 assert(CxTYPE(cx) == CXt_EVAL);
2944 if ((cx->blk_gimme & G_WANT) == G_VOID)
2945 scalarvoid(PL_eval_root);
2946 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
2949 scalar(PL_eval_root);
2951 PL_eval_start = op_linklist(PL_eval_root);
2952 PL_eval_root->op_private |= OPpREFCOUNTED;
2953 OpREFCNT_set(PL_eval_root, 1);
2954 PL_eval_root->op_next = 0;
2955 i = PL_savestack_ix;
2958 CALL_PEEP(PL_eval_start);
2959 finalize_optree(PL_eval_root);
2961 PL_savestack_ix = i;
2964 if (o->op_type == OP_STUB) {
2965 PL_comppad_name = 0;
2967 S_op_destroy(aTHX_ o);
2970 PL_main_root = op_scope(sawparens(scalarvoid(o)));
2971 PL_curcop = &PL_compiling;
2972 PL_main_start = LINKLIST(PL_main_root);
2973 PL_main_root->op_private |= OPpREFCOUNTED;
2974 OpREFCNT_set(PL_main_root, 1);
2975 PL_main_root->op_next = 0;
2976 CALL_PEEP(PL_main_start);
2977 finalize_optree(PL_main_root);
2978 cv_forget_slab(PL_compcv);
2981 /* Register with debugger */
2983 CV * const cv = get_cvs("DB::postponed", 0);
2987 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2989 call_sv(MUTABLE_SV(cv), G_DISCARD);
2996 Perl_localize(pTHX_ OP *o, I32 lex)
3000 PERL_ARGS_ASSERT_LOCALIZE;
3002 if (o->op_flags & OPf_PARENS)
3003 /* [perl #17376]: this appears to be premature, and results in code such as
3004 C< our(%x); > executing in list mode rather than void mode */
3011 if ( PL_parser->bufptr > PL_parser->oldbufptr
3012 && PL_parser->bufptr[-1] == ','
3013 && ckWARN(WARN_PARENTHESIS))
3015 char *s = PL_parser->bufptr;
3018 /* some heuristics to detect a potential error */
3019 while (*s && (strchr(", \t\n", *s)))
3023 if (*s && strchr("@$%*", *s) && *++s
3024 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
3027 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
3029 while (*s && (strchr(", \t\n", *s)))
3035 if (sigil && (*s == ';' || *s == '=')) {
3036 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
3037 "Parentheses missing around \"%s\" list",
3039 ? (PL_parser->in_my == KEY_our
3041 : PL_parser->in_my == KEY_state
3051 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
3052 PL_parser->in_my = FALSE;
3053 PL_parser->in_my_stash = NULL;
3058 Perl_jmaybe(pTHX_ OP *o)
3060 PERL_ARGS_ASSERT_JMAYBE;
3062 if (o->op_type == OP_LIST) {
3064 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
3065 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
3070 PERL_STATIC_INLINE OP *
3071 S_op_std_init(pTHX_ OP *o)
3073 I32 type = o->op_type;
3075 PERL_ARGS_ASSERT_OP_STD_INIT;
3077 if (PL_opargs[type] & OA_RETSCALAR)
3079 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
3080 o->op_targ = pad_alloc(type, SVs_PADTMP);
3085 PERL_STATIC_INLINE OP *
3086 S_op_integerize(pTHX_ OP *o)
3088 I32 type = o->op_type;
3090 PERL_ARGS_ASSERT_OP_INTEGERIZE;
3092 /* integerize op. */
3093 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER))
3096 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
3099 if (type == OP_NEGATE)
3100 /* XXX might want a ck_negate() for this */
3101 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
3107 S_fold_constants(pTHX_ register OP *o)
3110 register OP * VOL curop;
3112 VOL I32 type = o->op_type;
3117 SV * const oldwarnhook = PL_warnhook;
3118 SV * const olddiehook = PL_diehook;
3122 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3124 if (!(PL_opargs[type] & OA_FOLDCONST))
3138 /* XXX what about the numeric ops? */
3139 if (IN_LOCALE_COMPILETIME)
3143 if (!cLISTOPo->op_first->op_sibling
3144 || cLISTOPo->op_first->op_sibling->op_type != OP_CONST)
3147 SV * const sv = cSVOPx_sv(cLISTOPo->op_first->op_sibling);
3148 if (!SvPOK(sv) || SvGMAGICAL(sv)) goto nope;
3150 const char *s = SvPVX_const(sv);
3151 while (s < SvEND(sv)) {
3152 if (*s == 'p' || *s == 'P') goto nope;
3159 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3162 if (PL_parser && PL_parser->error_count)
3163 goto nope; /* Don't try to run w/ errors */
3165 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3166 const OPCODE type = curop->op_type;
3167 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3169 type != OP_SCALAR &&
3171 type != OP_PUSHMARK)
3177 curop = LINKLIST(o);
3178 old_next = o->op_next;
3182 oldscope = PL_scopestack_ix;
3183 create_eval_scope(G_FAKINGEVAL);
3185 /* Verify that we don't need to save it: */
3186 assert(PL_curcop == &PL_compiling);
3187 StructCopy(&PL_compiling, ¬_compiling, COP);
3188 PL_curcop = ¬_compiling;
3189 /* The above ensures that we run with all the correct hints of the
3190 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3191 assert(IN_PERL_RUNTIME);
3192 PL_warnhook = PERL_WARNHOOK_FATAL;
3199 sv = *(PL_stack_sp--);
3200 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3202 /* Can't simply swipe the SV from the pad, because that relies on
3203 the op being freed "real soon now". Under MAD, this doesn't
3204 happen (see the #ifdef below). */
3207 pad_swipe(o->op_targ, FALSE);
3210 else if (SvTEMP(sv)) { /* grab mortal temp? */
3211 SvREFCNT_inc_simple_void(sv);
3216 /* Something tried to die. Abandon constant folding. */
3217 /* Pretend the error never happened. */
3219 o->op_next = old_next;
3223 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3224 PL_warnhook = oldwarnhook;
3225 PL_diehook = olddiehook;
3226 /* XXX note that this croak may fail as we've already blown away
3227 * the stack - eg any nested evals */
3228 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3231 PL_warnhook = oldwarnhook;
3232 PL_diehook = olddiehook;
3233 PL_curcop = &PL_compiling;
3235 if (PL_scopestack_ix > oldscope)
3236 delete_eval_scope();
3245 if (type == OP_RV2GV)
3246 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3248 newop = newSVOP(OP_CONST, OPpCONST_FOLDED<<8, MUTABLE_SV(sv));
3249 op_getmad(o,newop,'f');
3257 S_gen_constant_list(pTHX_ register OP *o)
3261 const I32 oldtmps_floor = PL_tmps_floor;
3264 if (PL_parser && PL_parser->error_count)
3265 return o; /* Don't attempt to run with errors */
3267 PL_op = curop = LINKLIST(o);
3270 Perl_pp_pushmark(aTHX);
3273 assert (!(curop->op_flags & OPf_SPECIAL));
3274 assert(curop->op_type == OP_RANGE);
3275 Perl_pp_anonlist(aTHX);
3276 PL_tmps_floor = oldtmps_floor;
3278 o->op_type = OP_RV2AV;
3279 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3280 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3281 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3282 o->op_opt = 0; /* needs to be revisited in rpeep() */
3283 curop = ((UNOP*)o)->op_first;
3284 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
3286 op_getmad(curop,o,'O');
3295 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3298 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3299 if (!o || o->op_type != OP_LIST)
3300 o = newLISTOP(OP_LIST, 0, o, NULL);
3302 o->op_flags &= ~OPf_WANT;
3304 if (!(PL_opargs[type] & OA_MARK))
3305 op_null(cLISTOPo->op_first);
3307 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3308 if (kid2 && kid2->op_type == OP_COREARGS) {
3309 op_null(cLISTOPo->op_first);
3310 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3314 o->op_type = (OPCODE)type;
3315 o->op_ppaddr = PL_ppaddr[type];
3316 o->op_flags |= flags;
3318 o = CHECKOP(type, o);
3319 if (o->op_type != (unsigned)type)
3322 return fold_constants(op_integerize(op_std_init(o)));
3326 =head1 Optree Manipulation Functions
3329 /* List constructors */
3332 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3334 Append an item to the list of ops contained directly within a list-type
3335 op, returning the lengthened list. I<first> is the list-type op,
3336 and I<last> is the op to append to the list. I<optype> specifies the
3337 intended opcode for the list. If I<first> is not already a list of the
3338 right type, it will be upgraded into one. If either I<first> or I<last>
3339 is null, the other is returned unchanged.
3345 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3353 if (first->op_type != (unsigned)type
3354 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3356 return newLISTOP(type, 0, first, last);
3359 if (first->op_flags & OPf_KIDS)
3360 ((LISTOP*)first)->op_last->op_sibling = last;
3362 first->op_flags |= OPf_KIDS;
3363 ((LISTOP*)first)->op_first = last;
3365 ((LISTOP*)first)->op_last = last;
3370 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3372 Concatenate the lists of ops contained directly within two list-type ops,
3373 returning the combined list. I<first> and I<last> are the list-type ops
3374 to concatenate. I<optype> specifies the intended opcode for the list.
3375 If either I<first> or I<last> is not already a list of the right type,
3376 it will be upgraded into one. If either I<first> or I<last> is null,
3377 the other is returned unchanged.
3383 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3391 if (first->op_type != (unsigned)type)
3392 return op_prepend_elem(type, first, last);
3394 if (last->op_type != (unsigned)type)
3395 return op_append_elem(type, first, last);
3397 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3398 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3399 first->op_flags |= (last->op_flags & OPf_KIDS);
3402 if (((LISTOP*)last)->op_first && first->op_madprop) {
3403 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3405 while (mp->mad_next)
3407 mp->mad_next = first->op_madprop;
3410 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3413 first->op_madprop = last->op_madprop;
3414 last->op_madprop = 0;
3417 S_op_destroy(aTHX_ last);
3423 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3425 Prepend an item to the list of ops contained directly within a list-type
3426 op, returning the lengthened list. I<first> is the op to prepend to the
3427 list, and I<last> is the list-type op. I<optype> specifies the intended
3428 opcode for the list. If I<last> is not already a list of the right type,
3429 it will be upgraded into one. If either I<first> or I<last> is null,
3430 the other is returned unchanged.
3436 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3444 if (last->op_type == (unsigned)type) {
3445 if (type == OP_LIST) { /* already a PUSHMARK there */
3446 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3447 ((LISTOP*)last)->op_first->op_sibling = first;
3448 if (!(first->op_flags & OPf_PARENS))
3449 last->op_flags &= ~OPf_PARENS;
3452 if (!(last->op_flags & OPf_KIDS)) {
3453 ((LISTOP*)last)->op_last = first;
3454 last->op_flags |= OPf_KIDS;
3456 first->op_sibling = ((LISTOP*)last)->op_first;
3457 ((LISTOP*)last)->op_first = first;
3459 last->op_flags |= OPf_KIDS;
3463 return newLISTOP(type, 0, first, last);
3471 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3474 Newxz(tk, 1, TOKEN);
3475 tk->tk_type = (OPCODE)optype;
3476 tk->tk_type = 12345;
3478 tk->tk_mad = madprop;
3483 Perl_token_free(pTHX_ TOKEN* tk)
3485 PERL_ARGS_ASSERT_TOKEN_FREE;
3487 if (tk->tk_type != 12345)
3489 mad_free(tk->tk_mad);
3494 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3499 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3501 if (tk->tk_type != 12345) {
3502 Perl_warner(aTHX_ packWARN(WARN_MISC),
3503 "Invalid TOKEN object ignored");
3510 /* faked up qw list? */
3512 tm->mad_type == MAD_SV &&
3513 SvPVX((SV *)tm->mad_val)[0] == 'q')
3520 /* pretend constant fold didn't happen? */
3521 if (mp->mad_key == 'f' &&
3522 (o->op_type == OP_CONST ||
3523 o->op_type == OP_GV) )
3525 token_getmad(tk,(OP*)mp->mad_val,slot);
3539 if (mp->mad_key == 'X')
3540 mp->mad_key = slot; /* just change the first one */
3550 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3559 /* pretend constant fold didn't happen? */
3560 if (mp->mad_key == 'f' &&
3561 (o->op_type == OP_CONST ||
3562 o->op_type == OP_GV) )
3564 op_getmad(from,(OP*)mp->mad_val,slot);
3571 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3574 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3580 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3589 /* pretend constant fold didn't happen? */
3590 if (mp->mad_key == 'f' &&
3591 (o->op_type == OP_CONST ||
3592 o->op_type == OP_GV) )
3594 op_getmad(from,(OP*)mp->mad_val,slot);
3601 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
3604 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
3608 PerlIO_printf(PerlIO_stderr(),
3609 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
3615 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
3633 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
3637 addmad(tm, &(o->op_madprop), slot);
3641 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
3662 Perl_newMADsv(pTHX_ char key, SV* sv)
3664 PERL_ARGS_ASSERT_NEWMADSV;
3666 return newMADPROP(key, MAD_SV, sv, 0);
3670 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3672 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
3675 mp->mad_vlen = vlen;
3676 mp->mad_type = type;
3678 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
3683 Perl_mad_free(pTHX_ MADPROP* mp)
3685 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3689 mad_free(mp->mad_next);
3690 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3691 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3692 switch (mp->mad_type) {
3696 Safefree((char*)mp->mad_val);
3699 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
3700 op_free((OP*)mp->mad_val);
3703 sv_free(MUTABLE_SV(mp->mad_val));
3706 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3709 PerlMemShared_free(mp);
3715 =head1 Optree construction
3717 =for apidoc Am|OP *|newNULLLIST
3719 Constructs, checks, and returns a new C<stub> op, which represents an
3720 empty list expression.
3726 Perl_newNULLLIST(pTHX)
3728 return newOP(OP_STUB, 0);
3732 S_force_list(pTHX_ OP *o)
3734 if (!o || o->op_type != OP_LIST)
3735 o = newLISTOP(OP_LIST, 0, o, NULL);
3741 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3743 Constructs, checks, and returns an op of any list type. I<type> is
3744 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3745 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3746 supply up to two ops to be direct children of the list op; they are
3747 consumed by this function and become part of the constructed op tree.
3753 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3758 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3760 NewOp(1101, listop, 1, LISTOP);
3762 listop->op_type = (OPCODE)type;
3763 listop->op_ppaddr = PL_ppaddr[type];
3766 listop->op_flags = (U8)flags;
3770 else if (!first && last)
3773 first->op_sibling = last;
3774 listop->op_first = first;
3775 listop->op_last = last;
3776 if (type == OP_LIST) {
3777 OP* const pushop = newOP(OP_PUSHMARK, 0);
3778 pushop->op_sibling = first;
3779 listop->op_first = pushop;
3780 listop->op_flags |= OPf_KIDS;
3782 listop->op_last = pushop;
3785 return CHECKOP(type, listop);
3789 =for apidoc Am|OP *|newOP|I32 type|I32 flags
3791 Constructs, checks, and returns an op of any base type (any type that
3792 has no extra fields). I<type> is the opcode. I<flags> gives the
3793 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3800 Perl_newOP(pTHX_ I32 type, I32 flags)
3805 if (type == -OP_ENTEREVAL) {
3806 type = OP_ENTEREVAL;
3807 flags |= OPpEVAL_BYTES<<8;
3810 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3811 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3812 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3813 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3815 NewOp(1101, o, 1, OP);
3816 o->op_type = (OPCODE)type;
3817 o->op_ppaddr = PL_ppaddr[type];
3818 o->op_flags = (U8)flags;
3820 o->op_latefreed = 0;
3824 o->op_private = (U8)(0 | (flags >> 8));
3825 if (PL_opargs[type] & OA_RETSCALAR)
3827 if (PL_opargs[type] & OA_TARGET)
3828 o->op_targ = pad_alloc(type, SVs_PADTMP);
3829 return CHECKOP(type, o);
3833 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3835 Constructs, checks, and returns an op of any unary type. I<type> is
3836 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3837 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3838 bits, the eight bits of C<op_private>, except that the bit with value 1
3839 is automatically set. I<first> supplies an optional op to be the direct
3840 child of the unary op; it is consumed by this function and become part
3841 of the constructed op tree.
3847 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3852 if (type == -OP_ENTEREVAL) {
3853 type = OP_ENTEREVAL;
3854 flags |= OPpEVAL_BYTES<<8;
3857 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3858 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3859 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3860 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3861 || type == OP_SASSIGN
3862 || type == OP_ENTERTRY
3863 || type == OP_NULL );
3866 first = newOP(OP_STUB, 0);
3867 if (PL_opargs[type] & OA_MARK)
3868 first = force_list(first);
3870 NewOp(1101, unop, 1, UNOP);
3871 unop->op_type = (OPCODE)type;
3872 unop->op_ppaddr = PL_ppaddr[type];
3873 unop->op_first = first;
3874 unop->op_flags = (U8)(flags | OPf_KIDS);
3875 unop->op_private = (U8)(1 | (flags >> 8));
3876 unop = (UNOP*) CHECKOP(type, unop);
3880 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3884 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3886 Constructs, checks, and returns an op of any binary type. I<type>
3887 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3888 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3889 the eight bits of C<op_private>, except that the bit with value 1 or
3890 2 is automatically set as required. I<first> and I<last> supply up to
3891 two ops to be the direct children of the binary op; they are consumed
3892 by this function and become part of the constructed op tree.
3898 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3903 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3904 || type == OP_SASSIGN || type == OP_NULL );
3906 NewOp(1101, binop, 1, BINOP);
3909 first = newOP(OP_NULL, 0);
3911 binop->op_type = (OPCODE)type;
3912 binop->op_ppaddr = PL_ppaddr[type];
3913 binop->op_first = first;
3914 binop->op_flags = (U8)(flags | OPf_KIDS);
3917 binop->op_private = (U8)(1 | (flags >> 8));
3920 binop->op_private = (U8)(2 | (flags >> 8));
3921 first->op_sibling = last;
3924 binop = (BINOP*)CHECKOP(type, binop);
3925 if (binop->op_next || binop->op_type != (OPCODE)type)
3928 binop->op_last = binop->op_first->op_sibling;
3930 return fold_constants(op_integerize(op_std_init((OP *)binop)));
3933 static int uvcompare(const void *a, const void *b)
3934 __attribute__nonnull__(1)
3935 __attribute__nonnull__(2)
3936 __attribute__pure__;
3937 static int uvcompare(const void *a, const void *b)
3939 if (*((const UV *)a) < (*(const UV *)b))
3941 if (*((const UV *)a) > (*(const UV *)b))
3943 if (*((const UV *)a+1) < (*(const UV *)b+1))
3945 if (*((const UV *)a+1) > (*(const UV *)b+1))
3951 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3954 SV * const tstr = ((SVOP*)expr)->op_sv;
3957 (repl->op_type == OP_NULL)
3958 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3960 ((SVOP*)repl)->op_sv;
3963 const U8 *t = (U8*)SvPV_const(tstr, tlen);
3964 const U8 *r = (U8*)SvPV_const(rstr, rlen);
3968 register short *tbl;
3970 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3971 const I32 squash = o->op_private & OPpTRANS_SQUASH;
3972 I32 del = o->op_private & OPpTRANS_DELETE;
3975 PERL_ARGS_ASSERT_PMTRANS;
3977 PL_hints |= HINT_BLOCK_SCOPE;
3980 o->op_private |= OPpTRANS_FROM_UTF;
3983 o->op_private |= OPpTRANS_TO_UTF;
3985 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3986 SV* const listsv = newSVpvs("# comment\n");
3988 const U8* tend = t + tlen;
3989 const U8* rend = r + rlen;
4003 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
4004 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
4007 const U32 flags = UTF8_ALLOW_DEFAULT;
4011 t = tsave = bytes_to_utf8(t, &len);
4014 if (!to_utf && rlen) {
4016 r = rsave = bytes_to_utf8(r, &len);
4020 /* There are several snags with this code on EBCDIC:
4021 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
4022 2. scan_const() in toke.c has encoded chars in native encoding which makes
4023 ranges at least in EBCDIC 0..255 range the bottom odd.
4027 U8 tmpbuf[UTF8_MAXBYTES+1];
4030 Newx(cp, 2*tlen, UV);
4032 transv = newSVpvs("");
4034 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4036 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
4038 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
4042 cp[2*i+1] = cp[2*i];
4046 qsort(cp, i, 2*sizeof(UV), uvcompare);
4047 for (j = 0; j < i; j++) {
4049 diff = val - nextmin;
4051 t = uvuni_to_utf8(tmpbuf,nextmin);
4052 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4054 U8 range_mark = UTF_TO_NATIVE(0xff);
4055 t = uvuni_to_utf8(tmpbuf, val - 1);
4056 sv_catpvn(transv, (char *)&range_mark, 1);
4057 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4064 t = uvuni_to_utf8(tmpbuf,nextmin);
4065 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4067 U8 range_mark = UTF_TO_NATIVE(0xff);
4068 sv_catpvn(transv, (char *)&range_mark, 1);
4070 t = uvuni_to_utf8(tmpbuf, 0x7fffffff);
4071 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
4072 t = (const U8*)SvPVX_const(transv);
4073 tlen = SvCUR(transv);
4077 else if (!rlen && !del) {
4078 r = t; rlen = tlen; rend = tend;
4081 if ((!rlen && !del) || t == r ||
4082 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
4084 o->op_private |= OPpTRANS_IDENTICAL;
4088 while (t < tend || tfirst <= tlast) {
4089 /* see if we need more "t" chars */
4090 if (tfirst > tlast) {
4091 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4093 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
4095 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
4102 /* now see if we need more "r" chars */
4103 if (rfirst > rlast) {
4105 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4107 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
4109 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
4118 rfirst = rlast = 0xffffffff;
4122 /* now see which range will peter our first, if either. */
4123 tdiff = tlast - tfirst;
4124 rdiff = rlast - rfirst;
4131 if (rfirst == 0xffffffff) {
4132 diff = tdiff; /* oops, pretend rdiff is infinite */
4134 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4135 (long)tfirst, (long)tlast);
4137 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4141 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4142 (long)tfirst, (long)(tfirst + diff),
4145 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4146 (long)tfirst, (long)rfirst);
4148 if (rfirst + diff > max)
4149 max = rfirst + diff;
4151 grows = (tfirst < rfirst &&
4152 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4164 else if (max > 0xff)
4169 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4171 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
4172 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4173 PAD_SETSV(cPADOPo->op_padix, swash);
4175 SvREADONLY_on(swash);
4177 cSVOPo->op_sv = swash;
4179 SvREFCNT_dec(listsv);
4180 SvREFCNT_dec(transv);
4182 if (!del && havefinal && rlen)
4183 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4184 newSVuv((UV)final), 0);
4187 o->op_private |= OPpTRANS_GROWS;
4193 op_getmad(expr,o,'e');
4194 op_getmad(repl,o,'r');
4202 tbl = (short*)PerlMemShared_calloc(
4203 (o->op_private & OPpTRANS_COMPLEMENT) &&
4204 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4206 cPVOPo->op_pv = (char*)tbl;
4208 for (i = 0; i < (I32)tlen; i++)
4210 for (i = 0, j = 0; i < 256; i++) {
4212 if (j >= (I32)rlen) {
4221 if (i < 128 && r[j] >= 128)
4231 o->op_private |= OPpTRANS_IDENTICAL;
4233 else if (j >= (I32)rlen)
4238 PerlMemShared_realloc(tbl,
4239 (0x101+rlen-j) * sizeof(short));
4240 cPVOPo->op_pv = (char*)tbl;
4242 tbl[0x100] = (short)(rlen - j);
4243 for (i=0; i < (I32)rlen - j; i++)
4244 tbl[0x101+i] = r[j+i];
4248 if (!rlen && !del) {
4251 o->op_private |= OPpTRANS_IDENTICAL;
4253 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4254 o->op_private |= OPpTRANS_IDENTICAL;
4256 for (i = 0; i < 256; i++)
4258 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4259 if (j >= (I32)rlen) {
4261 if (tbl[t[i]] == -1)
4267 if (tbl[t[i]] == -1) {
4268 if (t[i] < 128 && r[j] >= 128)
4275 if(del && rlen == tlen) {
4276 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4277 } else if(rlen > tlen) {
4278 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4282 o->op_private |= OPpTRANS_GROWS;
4284 op_getmad(expr,o,'e');
4285 op_getmad(repl,o,'r');
4295 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4297 Constructs, checks, and returns an op of any pattern matching type.
4298 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4299 and, shifted up eight bits, the eight bits of C<op_private>.
4305 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4310 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4312 NewOp(1101, pmop, 1, PMOP);
4313 pmop->op_type = (OPCODE)type;
4314 pmop->op_ppaddr = PL_ppaddr[type];
4315 pmop->op_flags = (U8)flags;
4316 pmop->op_private = (U8)(0 | (flags >> 8));
4318 if (PL_hints & HINT_RE_TAINT)
4319 pmop->op_pmflags |= PMf_RETAINT;
4320 if (IN_LOCALE_COMPILETIME) {
4321 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4323 else if ((! (PL_hints & HINT_BYTES))
4324 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4325 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4327 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4329 if (PL_hints & HINT_RE_FLAGS) {
4330 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4331 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4333 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4334 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4335 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4337 if (reflags && SvOK(reflags)) {
4338 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4344 assert(SvPOK(PL_regex_pad[0]));
4345 if (SvCUR(PL_regex_pad[0])) {
4346 /* Pop off the "packed" IV from the end. */
4347 SV *const repointer_list = PL_regex_pad[0];
4348 const char *p = SvEND(repointer_list) - sizeof(IV);
4349 const IV offset = *((IV*)p);
4351 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4353 SvEND_set(repointer_list, p);
4355 pmop->op_pmoffset = offset;
4356 /* This slot should be free, so assert this: */
4357 assert(PL_regex_pad[offset] == &PL_sv_undef);
4359 SV * const repointer = &PL_sv_undef;
4360 av_push(PL_regex_padav, repointer);
4361 pmop->op_pmoffset = av_len(PL_regex_padav);
4362 PL_regex_pad = AvARRAY(PL_regex_padav);
4366 return CHECKOP(type, pmop);
4369 /* Given some sort of match op o, and an expression expr containing a
4370 * pattern, either compile expr into a regex and attach it to o (if it's
4371 * constant), or convert expr into a runtime regcomp op sequence (if it's
4374 * isreg indicates that the pattern is part of a regex construct, eg
4375 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4376 * split "pattern", which aren't. In the former case, expr will be a list
4377 * if the pattern contains more than one term (eg /a$b/) or if it contains
4378 * a replacement, ie s/// or tr///.
4380 * When the pattern has been compiled within a new anon CV (for
4381 * qr/(?{...})/ ), then floor indicates the savestack level just before
4382 * the new sub was created
4386 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg, I32 floor)
4391 I32 repl_has_vars = 0;
4393 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4394 bool is_compiletime;
4397 PERL_ARGS_ASSERT_PMRUNTIME;
4399 /* for s/// and tr///, last element in list is the replacement; pop it */
4401 if (is_trans || o->op_type == OP_SUBST) {
4403 repl = cLISTOPx(expr)->op_last;
4404 kid = cLISTOPx(expr)->op_first;
4405 while (kid->op_sibling != repl)
4406 kid = kid->op_sibling;
4407 kid->op_sibling = NULL;
4408 cLISTOPx(expr)->op_last = kid;
4411 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4414 OP* const oe = expr;
4415 assert(expr->op_type == OP_LIST);
4416 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4417 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4418 expr = cLISTOPx(oe)->op_last;
4419 cLISTOPx(oe)->op_first->op_sibling = NULL;
4420 cLISTOPx(oe)->op_last = NULL;
4423 return pmtrans(o, expr, repl);
4426 /* find whether we have any runtime or code elements;
4427 * at the same time, temporarily set the op_next of each DO block;
4428 * then when we LINKLIST, this will cause the DO blocks to be excluded
4429 * from the op_next chain (and from having LINKLIST recursively
4430 * applied to them). We fix up the DOs specially later */
4434 if (expr->op_type == OP_LIST) {
4436 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4437 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)) {
4439 assert(!o->op_next && o->op_sibling);
4440 o->op_next = o->op_sibling;
4442 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4446 else if (expr->op_type != OP_CONST)
4451 /* fix up DO blocks; treat each one as a separate little sub */
4453 if (expr->op_type == OP_LIST) {
4455 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4456 if (!(o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)))
4458 o->op_next = NULL; /* undo temporary hack from above */
4461 if (cLISTOPo->op_first->op_type == OP_LEAVE) {
4462 LISTOP *leave = cLISTOPx(cLISTOPo->op_first);
4464 assert(leave->op_first->op_type == OP_ENTER);
4465 assert(leave->op_first->op_sibling);
4466 o->op_next = leave->op_first->op_sibling;
4468 assert(leave->op_flags & OPf_KIDS);
4469 assert(leave->op_last->op_next = (OP*)leave);
4470 leave->op_next = NULL; /* stop on last op */
4471 op_null((OP*)leave);
4475 OP *scope = cLISTOPo->op_first;
4476 assert(scope->op_type == OP_SCOPE);
4477 assert(scope->op_flags & OPf_KIDS);
4478 scope->op_next = NULL; /* stop on last op */
4481 /* have to peep the DOs individually as we've removed it from
4482 * the op_next chain */
4485 /* runtime finalizes as part of finalizing whole tree */
4490 PL_hints |= HINT_BLOCK_SCOPE;
4492 assert(floor==0 || (pm->op_pmflags & PMf_HAS_CV));
4494 if (is_compiletime) {
4495 U32 rx_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4496 regexp_engine const *eng = current_re_engine();
4498 if (o->op_flags & OPf_SPECIAL)
4499 rx_flags |= RXf_SPLIT;
4501 if (!has_code || !eng->op_comp) {
4502 /* compile-time simple constant pattern */
4504 if ((pm->op_pmflags & PMf_HAS_CV) && !has_code) {
4505 /* whoops! we guessed that a qr// had a code block, but we
4506 * were wrong (e.g. /[(?{}]/ ). Throw away the PL_compcv
4507 * that isn't required now. Note that we have to be pretty
4508 * confident that nothing used that CV's pad while the
4509 * regex was parsed */
4510 assert(AvFILLp(PL_comppad) == 0); /* just @_ */
4511 /* But we know that one op is using this CV's slab. */
4512 cv_forget_slab(PL_compcv);
4514 pm->op_pmflags &= ~PMf_HAS_CV;
4519 ? eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4520 rx_flags, pm->op_pmflags)
4521 : Perl_re_op_compile(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4522 rx_flags, pm->op_pmflags)
4525 op_getmad(expr,(OP*)pm,'e');
4531 /* compile-time pattern that includes literal code blocks */
4532 REGEXP* re = eng->op_comp(aTHX_ NULL, 0, expr, eng, NULL, NULL,
4535 ((PL_hints & HINT_RE_EVAL) ? PMf_USE_RE_EVAL : 0))
4538 if (pm->op_pmflags & PMf_HAS_CV) {
4540 /* this QR op (and the anon sub we embed it in) is never
4541 * actually executed. It's just a placeholder where we can
4542 * squirrel away expr in op_code_list without the peephole
4543 * optimiser etc processing it for a second time */
4544 OP *qr = newPMOP(OP_QR, 0);
4545 ((PMOP*)qr)->op_code_list = expr;
4547 /* handle the implicit sub{} wrapped round the qr/(?{..})/ */
4548 SvREFCNT_inc_simple_void(PL_compcv);
4549 cv = newATTRSUB(floor, 0, NULL, NULL, qr);
4550 ((struct regexp *)SvANY(re))->qr_anoncv = cv;
4552 /* attach the anon CV to the pad so that
4553 * pad_fixup_inner_anons() can find it */
4554 (void)pad_add_anon(cv, o->op_type);
4555 SvREFCNT_inc_simple_void(cv);
4558 pm->op_code_list = expr;
4563 /* runtime pattern: build chain of regcomp etc ops */
4565 PADOFFSET cv_targ = 0;
4567 reglist = isreg && expr->op_type == OP_LIST;
4572 pm->op_code_list = expr;
4573 /* don't free op_code_list; its ops are embedded elsewhere too */
4574 pm->op_pmflags |= PMf_CODELIST_PRIVATE;
4577 /* the OP_REGCMAYBE is a placeholder in the non-threaded case
4578 * to allow its op_next to be pointed past the regcomp and
4579 * preceding stacking ops;
4580 * OP_REGCRESET is there to reset taint before executing the
4582 if (pm->op_pmflags & PMf_KEEP || PL_tainting)
4583 expr = newUNOP((PL_tainting ? OP_REGCRESET : OP_REGCMAYBE),0,expr);
4585 if (pm->op_pmflags & PMf_HAS_CV) {
4586 /* we have a runtime qr with literal code. This means
4587 * that the qr// has been wrapped in a new CV, which
4588 * means that runtime consts, vars etc will have been compiled
4589 * against a new pad. So... we need to execute those ops
4590 * within the environment of the new CV. So wrap them in a call
4591 * to a new anon sub. i.e. for
4595 * we build an anon sub that looks like
4597 * sub { "a", $b, '(?{...})' }
4599 * and call it, passing the returned list to regcomp.
4600 * Or to put it another way, the list of ops that get executed
4604 * ------ -------------------
4605 * pushmark (for regcomp)
4606 * pushmark (for entersub)
4607 * pushmark (for refgen)
4611 * regcreset regcreset
4613 * const("a") const("a")
4615 * const("(?{...})") const("(?{...})")
4620 SvREFCNT_inc_simple_void(PL_compcv);
4621 /* these lines are just an unrolled newANONATTRSUB */
4622 expr = newSVOP(OP_ANONCODE, 0,
4623 MUTABLE_SV(newATTRSUB(floor, 0, NULL, NULL, expr)));
4624 cv_targ = expr->op_targ;
4625 expr = newUNOP(OP_REFGEN, 0, expr);
4627 expr = list(force_list(newUNOP(OP_ENTERSUB, 0, scalar(expr))));
4630 NewOp(1101, rcop, 1, LOGOP);
4631 rcop->op_type = OP_REGCOMP;
4632 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4633 rcop->op_first = scalar(expr);
4634 rcop->op_flags |= OPf_KIDS
4635 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4636 | (reglist ? OPf_STACKED : 0);
4637 rcop->op_private = 0;
4639 rcop->op_targ = cv_targ;
4641 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4642 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4644 /* establish postfix order */
4645 if (expr->op_type == OP_REGCRESET || expr->op_type == OP_REGCMAYBE) {
4647 rcop->op_next = expr;
4648 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4651 rcop->op_next = LINKLIST(expr);
4652 expr->op_next = (OP*)rcop;
4655 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4660 if (pm->op_pmflags & PMf_EVAL) {
4662 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
4663 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
4665 else if (repl->op_type == OP_CONST)
4669 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
4670 if (curop->op_type == OP_SCOPE
4671 || curop->op_type == OP_LEAVE
4672 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
4673 if (curop->op_type == OP_GV) {
4674 GV * const gv = cGVOPx_gv(curop);
4676 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
4679 else if (curop->op_type == OP_RV2CV)
4681 else if (curop->op_type == OP_RV2SV ||
4682 curop->op_type == OP_RV2AV ||
4683 curop->op_type == OP_RV2HV ||
4684 curop->op_type == OP_RV2GV) {
4685 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
4688 else if (curop->op_type == OP_PADSV ||
4689 curop->op_type == OP_PADAV ||
4690 curop->op_type == OP_PADHV ||
4691 curop->op_type == OP_PADANY)
4695 else if (curop->op_type == OP_PUSHRE)
4696 NOOP; /* Okay here, dangerous in newASSIGNOP */
4706 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4708 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4709 op_prepend_elem(o->op_type, scalar(repl), o);
4712 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
4713 pm->op_pmflags |= PMf_MAYBE_CONST;
4715 NewOp(1101, rcop, 1, LOGOP);
4716 rcop->op_type = OP_SUBSTCONT;
4717 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4718 rcop->op_first = scalar(repl);
4719 rcop->op_flags |= OPf_KIDS;
4720 rcop->op_private = 1;
4723 /* establish postfix order */
4724 rcop->op_next = LINKLIST(repl);
4725 repl->op_next = (OP*)rcop;
4727 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
4728 assert(!(pm->op_pmflags & PMf_ONCE));
4729 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
4738 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
4740 Constructs, checks, and returns an op of any type that involves an
4741 embedded SV. I<type> is the opcode. I<flags> gives the eight bits
4742 of C<op_flags>. I<sv> gives the SV to embed in the op; this function
4743 takes ownership of one reference to it.
4749 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
4754 PERL_ARGS_ASSERT_NEWSVOP;
4756 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4757 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4758 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4760 NewOp(1101, svop, 1, SVOP);
4761 svop->op_type = (OPCODE)type;
4762 svop->op_ppaddr = PL_ppaddr[type];
4764 svop->op_next = (OP*)svop;
4765 svop->op_flags = (U8)flags;
4766 svop->op_private = (U8)(0 | (flags >> 8));
4767 if (PL_opargs[type] & OA_RETSCALAR)
4769 if (PL_opargs[type] & OA_TARGET)
4770 svop->op_targ = pad_alloc(type, SVs_PADTMP);
4771 return CHECKOP(type, svop);
4777 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
4779 Constructs, checks, and returns an op of any type that involves a
4780 reference to a pad element. I<type> is the opcode. I<flags> gives the
4781 eight bits of C<op_flags>. A pad slot is automatically allocated, and
4782 is populated with I<sv>; this function takes ownership of one reference
4785 This function only exists if Perl has been compiled to use ithreads.
4791 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
4796 PERL_ARGS_ASSERT_NEWPADOP;
4798 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4799 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4800 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4802 NewOp(1101, padop, 1, PADOP);
4803 padop->op_type = (OPCODE)type;
4804 padop->op_ppaddr = PL_ppaddr[type];
4805 padop->op_padix = pad_alloc(type, SVs_PADTMP);
4806 SvREFCNT_dec(PAD_SVl(padop->op_padix));
4807 PAD_SETSV(padop->op_padix, sv);
4810 padop->op_next = (OP*)padop;
4811 padop->op_flags = (U8)flags;
4812 if (PL_opargs[type] & OA_RETSCALAR)
4814 if (PL_opargs[type] & OA_TARGET)
4815 padop->op_targ = pad_alloc(type, SVs_PADTMP);
4816 return CHECKOP(type, padop);
4819 #endif /* !USE_ITHREADS */
4822 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
4824 Constructs, checks, and returns an op of any type that involves an
4825 embedded reference to a GV. I<type> is the opcode. I<flags> gives the
4826 eight bits of C<op_flags>. I<gv> identifies the GV that the op should
4827 reference; calling this function does not transfer ownership of any
4834 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
4838 PERL_ARGS_ASSERT_NEWGVOP;
4842 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4844 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4849 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
4851 Constructs, checks, and returns an op of any type that involves an
4852 embedded C-level pointer (PV). I<type> is the opcode. I<flags> gives
4853 the eight bits of C<op_flags>. I<pv> supplies the C-level pointer, which
4854 must have been allocated using L</PerlMemShared_malloc>; the memory will
4855 be freed when the op is destroyed.