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 #if defined(PL_OP_SLAB_ALLOC)
114 #ifdef PERL_DEBUG_READONLY_OPS
115 # define PERL_SLAB_SIZE 4096
116 # include <sys/mman.h>
119 #ifndef PERL_SLAB_SIZE
120 #define PERL_SLAB_SIZE 2048
124 Perl_Slab_Alloc(pTHX_ size_t sz)
128 * To make incrementing use count easy PL_OpSlab is an I32 *
129 * To make inserting the link to slab PL_OpPtr is I32 **
130 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
131 * Add an overhead for pointer to slab and round up as a number of pointers
133 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
134 if ((PL_OpSpace -= sz) < 0) {
135 #ifdef PERL_DEBUG_READONLY_OPS
136 /* We need to allocate chunk by chunk so that we can control the VM
138 PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE,
139 MAP_ANON|MAP_PRIVATE, -1, 0);
141 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
142 (unsigned long) PERL_SLAB_SIZE*sizeof(I32*),
144 if(PL_OpPtr == MAP_FAILED) {
145 perror("mmap failed");
150 PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*));
155 /* We reserve the 0'th I32 sized chunk as a use count */
156 PL_OpSlab = (I32 *) PL_OpPtr;
157 /* Reduce size by the use count word, and by the size we need.
158 * Latter is to mimic the '-=' in the if() above
160 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
161 /* Allocation pointer starts at the top.
162 Theory: because we build leaves before trunk allocating at end
163 means that at run time access is cache friendly upward
165 PL_OpPtr += PERL_SLAB_SIZE;
167 #ifdef PERL_DEBUG_READONLY_OPS
168 /* We remember this slab. */
169 /* This implementation isn't efficient, but it is simple. */
170 PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1));
171 PL_slabs[PL_slab_count++] = PL_OpSlab;
172 DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab));
175 assert( PL_OpSpace >= 0 );
176 /* Move the allocation pointer down */
178 assert( PL_OpPtr > (I32 **) PL_OpSlab );
179 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
180 (*PL_OpSlab)++; /* Increment use count of slab */
181 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
182 assert( *PL_OpSlab > 0 );
183 return (void *)(PL_OpPtr + 1);
186 #ifdef PERL_DEBUG_READONLY_OPS
188 Perl_pending_Slabs_to_ro(pTHX) {
189 /* Turn all the allocated op slabs read only. */
190 U32 count = PL_slab_count;
191 I32 **const slabs = PL_slabs;
193 /* Reset the array of pending OP slabs, as we're about to turn this lot
194 read only. Also, do it ahead of the loop in case the warn triggers,
195 and a warn handler has an eval */
200 /* Force a new slab for any further allocation. */
204 void *const start = slabs[count];
205 const size_t size = PERL_SLAB_SIZE* sizeof(I32*);
206 if(mprotect(start, size, PROT_READ)) {
207 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d",
208 start, (unsigned long) size, errno);
216 S_Slab_to_rw(pTHX_ void *op)
218 I32 * const * const ptr = (I32 **) op;
219 I32 * const slab = ptr[-1];
221 PERL_ARGS_ASSERT_SLAB_TO_RW;
223 assert( ptr-1 > (I32 **) slab );
224 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
226 if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) {
227 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d",
228 slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno);
233 Perl_op_refcnt_inc(pTHX_ OP *o)
244 Perl_op_refcnt_dec(pTHX_ OP *o)
246 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
251 # define Slab_to_rw(op)
255 Perl_Slab_Free(pTHX_ void *op)
257 I32 * const * const ptr = (I32 **) op;
258 I32 * const slab = ptr[-1];
259 PERL_ARGS_ASSERT_SLAB_FREE;
260 assert( ptr-1 > (I32 **) slab );
261 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
264 if (--(*slab) == 0) {
266 # define PerlMemShared PerlMem
269 #ifdef PERL_DEBUG_READONLY_OPS
270 U32 count = PL_slab_count;
271 /* Need to remove this slab from our list of slabs */
274 if (PL_slabs[count] == slab) {
276 /* Found it. Move the entry at the end to overwrite it. */
277 DEBUG_m(PerlIO_printf(Perl_debug_log,
278 "Deallocate %p by moving %p from %lu to %lu\n",
280 PL_slabs[PL_slab_count - 1],
281 PL_slab_count, count));
282 PL_slabs[count] = PL_slabs[--PL_slab_count];
283 /* Could realloc smaller at this point, but probably not
285 if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) {
286 perror("munmap failed");
294 PerlMemShared_free(slab);
296 if (slab == PL_OpSlab) {
303 * In the following definition, the ", (OP*)0" is just to make the compiler
304 * think the expression is of the right type: croak actually does a Siglongjmp.
306 #define CHECKOP(type,o) \
307 ((PL_op_mask && PL_op_mask[type]) \
308 ? ( op_free((OP*)o), \
309 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
311 : PL_check[type](aTHX_ (OP*)o))
313 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
315 #define CHANGE_TYPE(o,type) \
317 o->op_type = (OPCODE)type; \
318 o->op_ppaddr = PL_ppaddr[type]; \
322 S_gv_ename(pTHX_ GV *gv)
324 SV* const tmpsv = sv_newmortal();
326 PERL_ARGS_ASSERT_GV_ENAME;
328 gv_efullname3(tmpsv, gv, NULL);
333 S_no_fh_allowed(pTHX_ OP *o)
335 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
337 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
343 S_too_few_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
345 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_SV;
346 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %"SVf, namesv),
347 SvUTF8(namesv) | flags);
352 S_too_few_arguments_pv(pTHX_ OP *o, const char* name, U32 flags)
354 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS_PV;
355 yyerror_pv(Perl_form(aTHX_ "Not enough arguments for %s", name), flags);
360 S_too_many_arguments_pv(pTHX_ OP *o, const char *name, U32 flags)
362 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_PV;
364 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %s", name), flags);
369 S_too_many_arguments_sv(pTHX_ OP *o, SV *namesv, U32 flags)
371 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS_SV;
373 yyerror_pv(Perl_form(aTHX_ "Too many arguments for %"SVf, SVfARG(namesv)),
374 SvUTF8(namesv) | flags);
379 S_bad_type_pv(pTHX_ I32 n, const char *t, const char *name, U32 flags, const OP *kid)
381 PERL_ARGS_ASSERT_BAD_TYPE_PV;
383 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
384 (int)n, name, t, OP_DESC(kid)), flags);
388 S_bad_type_sv(pTHX_ I32 n, const char *t, SV *namesv, U32 flags, const OP *kid)
390 PERL_ARGS_ASSERT_BAD_TYPE_SV;
392 yyerror_pv(Perl_form(aTHX_ "Type of arg %d to %"SVf" must be %s (not %s)",
393 (int)n, SVfARG(namesv), t, OP_DESC(kid)), SvUTF8(namesv) | flags);
397 S_no_bareword_allowed(pTHX_ OP *o)
399 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
402 return; /* various ok barewords are hidden in extra OP_NULL */
403 qerror(Perl_mess(aTHX_
404 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
406 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
409 /* "register" allocation */
412 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
416 const bool is_our = (PL_parser->in_my == KEY_our);
418 PERL_ARGS_ASSERT_ALLOCMY;
420 if (flags & ~SVf_UTF8)
421 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
424 /* Until we're using the length for real, cross check that we're being
426 assert(strlen(name) == len);
428 /* complain about "my $<special_var>" etc etc */
432 ((flags & SVf_UTF8) && isIDFIRST_utf8((U8 *)name+1)) ||
433 (name[1] == '_' && (*name == '$' || len > 2))))
435 /* name[2] is true if strlen(name) > 2 */
436 if (!(flags & SVf_UTF8 && UTF8_IS_START(name[1]))
437 && (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1]))) {
438 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
439 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
440 PL_parser->in_my == KEY_state ? "state" : "my"));
442 yyerror_pv(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
443 PL_parser->in_my == KEY_state ? "state" : "my"), flags & SVf_UTF8);
447 /* allocate a spare slot and store the name in that slot */
449 off = pad_add_name_pvn(name, len,
450 (is_our ? padadd_OUR :
451 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
452 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
453 PL_parser->in_my_stash,
455 /* $_ is always in main::, even with our */
456 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
460 /* anon sub prototypes contains state vars should always be cloned,
461 * otherwise the state var would be shared between anon subs */
463 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
464 CvCLONE_on(PL_compcv);
470 =for apidoc alloccopstash
472 Available only under threaded builds, this function allocates an entry in
473 C<PL_stashpad> for the stash passed to it.
480 Perl_alloccopstash(pTHX_ HV *hv)
482 PADOFFSET off = 0, o = 1;
483 bool found_slot = FALSE;
485 PERL_ARGS_ASSERT_ALLOCCOPSTASH;
487 if (PL_stashpad[PL_stashpadix] == hv) return PL_stashpadix;
489 for (; o < PL_stashpadmax; ++o) {
490 if (PL_stashpad[o] == hv) return PL_stashpadix = o;
491 if (!PL_stashpad[o] || SvTYPE(PL_stashpad[o]) != SVt_PVHV)
492 found_slot = TRUE, off = o;
495 Renew(PL_stashpad, PL_stashpadmax + 10, HV *);
496 Zero(PL_stashpad + PL_stashpadmax, 10, HV *);
497 off = PL_stashpadmax;
498 PL_stashpadmax += 10;
501 PL_stashpad[PL_stashpadix = off] = hv;
506 /* free the body of an op without examining its contents.
507 * Always use this rather than FreeOp directly */
510 S_op_destroy(pTHX_ OP *o)
512 if (o->op_latefree) {
520 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
522 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
528 Perl_op_free(pTHX_ OP *o)
535 if (o->op_latefreed) {
542 if (o->op_private & OPpREFCOUNTED) {
553 refcnt = OpREFCNT_dec(o);
556 /* Need to find and remove any pattern match ops from the list
557 we maintain for reset(). */
558 find_and_forget_pmops(o);
568 /* Call the op_free hook if it has been set. Do it now so that it's called
569 * at the right time for refcounted ops, but still before all of the kids
573 if (o->op_flags & OPf_KIDS) {
574 register OP *kid, *nextkid;
575 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
576 nextkid = kid->op_sibling; /* Get before next freeing kid */
581 #ifdef PERL_DEBUG_READONLY_OPS
585 /* COP* is not cleared by op_clear() so that we may track line
586 * numbers etc even after null() */
587 if (type == OP_NEXTSTATE || type == OP_DBSTATE
588 || (type == OP_NULL /* the COP might have been null'ed */
589 && ((OPCODE)o->op_targ == OP_NEXTSTATE
590 || (OPCODE)o->op_targ == OP_DBSTATE))) {
595 type = (OPCODE)o->op_targ;
598 if (o->op_latefree) {
604 #ifdef DEBUG_LEAKING_SCALARS
611 Perl_op_clear(pTHX_ OP *o)
616 PERL_ARGS_ASSERT_OP_CLEAR;
619 mad_free(o->op_madprop);
624 switch (o->op_type) {
625 case OP_NULL: /* Was holding old type, if any. */
626 if (PL_madskills && o->op_targ != OP_NULL) {
627 o->op_type = (Optype)o->op_targ;
632 case OP_ENTEREVAL: /* Was holding hints. */
636 if (!(o->op_flags & OPf_REF)
637 || (PL_check[o->op_type] != Perl_ck_ftst))
644 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
649 /* It's possible during global destruction that the GV is freed
650 before the optree. Whilst the SvREFCNT_inc is happy to bump from
651 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
652 will trigger an assertion failure, because the entry to sv_clear
653 checks that the scalar is not already freed. A check of for
654 !SvIS_FREED(gv) turns out to be invalid, because during global
655 destruction the reference count can be forced down to zero
656 (with SVf_BREAK set). In which case raising to 1 and then
657 dropping to 0 triggers cleanup before it should happen. I
658 *think* that this might actually be a general, systematic,
659 weakness of the whole idea of SVf_BREAK, in that code *is*
660 allowed to raise and lower references during global destruction,
661 so any *valid* code that happens to do this during global
662 destruction might well trigger premature cleanup. */
663 bool still_valid = gv && SvREFCNT(gv);
666 SvREFCNT_inc_simple_void(gv);
668 if (cPADOPo->op_padix > 0) {
669 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
670 * may still exist on the pad */
671 pad_swipe(cPADOPo->op_padix, TRUE);
672 cPADOPo->op_padix = 0;
675 SvREFCNT_dec(cSVOPo->op_sv);
676 cSVOPo->op_sv = NULL;
679 int try_downgrade = SvREFCNT(gv) == 2;
682 gv_try_downgrade(gv);
686 case OP_METHOD_NAMED:
689 SvREFCNT_dec(cSVOPo->op_sv);
690 cSVOPo->op_sv = NULL;
693 Even if op_clear does a pad_free for the target of the op,
694 pad_free doesn't actually remove the sv that exists in the pad;
695 instead it lives on. This results in that it could be reused as
696 a target later on when the pad was reallocated.
699 pad_swipe(o->op_targ,1);
708 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
713 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
715 if (cPADOPo->op_padix > 0) {
716 pad_swipe(cPADOPo->op_padix, TRUE);
717 cPADOPo->op_padix = 0;
720 SvREFCNT_dec(cSVOPo->op_sv);
721 cSVOPo->op_sv = NULL;
725 PerlMemShared_free(cPVOPo->op_pv);
726 cPVOPo->op_pv = NULL;
730 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
734 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
735 /* No GvIN_PAD_off here, because other references may still
736 * exist on the pad */
737 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
740 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
746 forget_pmop(cPMOPo, 1);
747 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
748 /* we use the same protection as the "SAFE" version of the PM_ macros
749 * here since sv_clean_all might release some PMOPs
750 * after PL_regex_padav has been cleared
751 * and the clearing of PL_regex_padav needs to
752 * happen before sv_clean_all
755 if(PL_regex_pad) { /* We could be in destruction */
756 const IV offset = (cPMOPo)->op_pmoffset;
757 ReREFCNT_dec(PM_GETRE(cPMOPo));
758 PL_regex_pad[offset] = &PL_sv_undef;
759 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
763 ReREFCNT_dec(PM_GETRE(cPMOPo));
764 PM_SETRE(cPMOPo, NULL);
770 if (o->op_targ > 0) {
771 pad_free(o->op_targ);
777 S_cop_free(pTHX_ COP* cop)
779 PERL_ARGS_ASSERT_COP_FREE;
782 if (! specialWARN(cop->cop_warnings))
783 PerlMemShared_free(cop->cop_warnings);
784 cophh_free(CopHINTHASH_get(cop));
788 S_forget_pmop(pTHX_ PMOP *const o
794 HV * const pmstash = PmopSTASH(o);
796 PERL_ARGS_ASSERT_FORGET_PMOP;
798 if (pmstash && !SvIS_FREED(pmstash) && SvMAGICAL(pmstash)) {
799 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
801 PMOP **const array = (PMOP**) mg->mg_ptr;
802 U32 count = mg->mg_len / sizeof(PMOP**);
807 /* Found it. Move the entry at the end to overwrite it. */
808 array[i] = array[--count];
809 mg->mg_len = count * sizeof(PMOP**);
810 /* Could realloc smaller at this point always, but probably
811 not worth it. Probably worth free()ing if we're the
814 Safefree(mg->mg_ptr);
831 S_find_and_forget_pmops(pTHX_ OP *o)
833 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
835 if (o->op_flags & OPf_KIDS) {
836 OP *kid = cUNOPo->op_first;
838 switch (kid->op_type) {
843 forget_pmop((PMOP*)kid, 0);
845 find_and_forget_pmops(kid);
846 kid = kid->op_sibling;
852 Perl_op_null(pTHX_ OP *o)
856 PERL_ARGS_ASSERT_OP_NULL;
858 if (o->op_type == OP_NULL)
862 o->op_targ = o->op_type;
863 o->op_type = OP_NULL;
864 o->op_ppaddr = PL_ppaddr[OP_NULL];
868 Perl_op_refcnt_lock(pTHX)
876 Perl_op_refcnt_unlock(pTHX)
883 /* Contextualizers */
886 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
888 Applies a syntactic context to an op tree representing an expression.
889 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
890 or C<G_VOID> to specify the context to apply. The modified op tree
897 Perl_op_contextualize(pTHX_ OP *o, I32 context)
899 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
901 case G_SCALAR: return scalar(o);
902 case G_ARRAY: return list(o);
903 case G_VOID: return scalarvoid(o);
905 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
912 =head1 Optree Manipulation Functions
914 =for apidoc Am|OP*|op_linklist|OP *o
915 This function is the implementation of the L</LINKLIST> macro. It should
916 not be called directly.
922 Perl_op_linklist(pTHX_ OP *o)
926 PERL_ARGS_ASSERT_OP_LINKLIST;
931 /* establish postfix order */
932 first = cUNOPo->op_first;
935 o->op_next = LINKLIST(first);
938 if (kid->op_sibling) {
939 kid->op_next = LINKLIST(kid->op_sibling);
940 kid = kid->op_sibling;
954 S_scalarkids(pTHX_ OP *o)
956 if (o && o->op_flags & OPf_KIDS) {
958 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
965 S_scalarboolean(pTHX_ OP *o)
969 PERL_ARGS_ASSERT_SCALARBOOLEAN;
971 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
972 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
973 if (ckWARN(WARN_SYNTAX)) {
974 const line_t oldline = CopLINE(PL_curcop);
976 if (PL_parser && PL_parser->copline != NOLINE)
977 CopLINE_set(PL_curcop, PL_parser->copline);
978 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
979 CopLINE_set(PL_curcop, oldline);
986 Perl_scalar(pTHX_ OP *o)
991 /* assumes no premature commitment */
992 if (!o || (PL_parser && PL_parser->error_count)
993 || (o->op_flags & OPf_WANT)
994 || o->op_type == OP_RETURN)
999 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
1001 switch (o->op_type) {
1003 scalar(cBINOPo->op_first);
1008 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1018 if (o->op_flags & OPf_KIDS) {
1019 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1025 kid = cLISTOPo->op_first;
1027 kid = kid->op_sibling;
1030 OP *sib = kid->op_sibling;
1031 if (sib && kid->op_type != OP_LEAVEWHEN)
1037 PL_curcop = &PL_compiling;
1042 kid = cLISTOPo->op_first;
1045 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
1052 Perl_scalarvoid(pTHX_ OP *o)
1056 const char* useless = NULL;
1057 U32 useless_is_utf8 = 0;
1061 PERL_ARGS_ASSERT_SCALARVOID;
1063 /* trailing mad null ops don't count as "there" for void processing */
1065 o->op_type != OP_NULL &&
1067 o->op_sibling->op_type == OP_NULL)
1070 for (sib = o->op_sibling;
1071 sib && sib->op_type == OP_NULL;
1072 sib = sib->op_sibling) ;
1078 if (o->op_type == OP_NEXTSTATE
1079 || o->op_type == OP_DBSTATE
1080 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1081 || o->op_targ == OP_DBSTATE)))
1082 PL_curcop = (COP*)o; /* for warning below */
1084 /* assumes no premature commitment */
1085 want = o->op_flags & OPf_WANT;
1086 if ((want && want != OPf_WANT_SCALAR)
1087 || (PL_parser && PL_parser->error_count)
1088 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1093 if ((o->op_private & OPpTARGET_MY)
1094 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1096 return scalar(o); /* As if inside SASSIGN */
1099 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1101 switch (o->op_type) {
1103 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1107 if (o->op_flags & OPf_STACKED)
1111 if (o->op_private == 4)
1136 case OP_AELEMFAST_LEX:
1155 case OP_GETSOCKNAME:
1156 case OP_GETPEERNAME:
1161 case OP_GETPRIORITY:
1186 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1187 /* Otherwise it's "Useless use of grep iterator" */
1188 useless = OP_DESC(o);
1192 kid = cLISTOPo->op_first;
1193 if (kid && kid->op_type == OP_PUSHRE
1195 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1197 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1199 useless = OP_DESC(o);
1203 kid = cUNOPo->op_first;
1204 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1205 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1208 useless = "negative pattern binding (!~)";
1212 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1213 useless = "non-destructive substitution (s///r)";
1217 useless = "non-destructive transliteration (tr///r)";
1224 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1225 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1226 useless = "a variable";
1231 if (cSVOPo->op_private & OPpCONST_STRICT)
1232 no_bareword_allowed(o);
1234 if (ckWARN(WARN_VOID)) {
1235 /* don't warn on optimised away booleans, eg
1236 * use constant Foo, 5; Foo || print; */
1237 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1239 /* the constants 0 and 1 are permitted as they are
1240 conventionally used as dummies in constructs like
1241 1 while some_condition_with_side_effects; */
1242 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1244 else if (SvPOK(sv)) {
1245 /* perl4's way of mixing documentation and code
1246 (before the invention of POD) was based on a
1247 trick to mix nroff and perl code. The trick was
1248 built upon these three nroff macros being used in
1249 void context. The pink camel has the details in
1250 the script wrapman near page 319. */
1251 const char * const maybe_macro = SvPVX_const(sv);
1252 if (strnEQ(maybe_macro, "di", 2) ||
1253 strnEQ(maybe_macro, "ds", 2) ||
1254 strnEQ(maybe_macro, "ig", 2))
1257 SV * const dsv = newSVpvs("");
1258 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1260 pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
1261 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
1263 useless = SvPV_nolen(msv);
1264 useless_is_utf8 = SvUTF8(msv);
1267 else if (SvOK(sv)) {
1268 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1269 "a constant (%"SVf")", sv));
1270 useless = SvPV_nolen(msv);
1273 useless = "a constant (undef)";
1276 op_null(o); /* don't execute or even remember it */
1280 o->op_type = OP_PREINC; /* pre-increment is faster */
1281 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1285 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1286 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1290 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1291 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1295 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1296 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1301 UNOP *refgen, *rv2cv;
1304 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1307 rv2gv = ((BINOP *)o)->op_last;
1308 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1311 refgen = (UNOP *)((BINOP *)o)->op_first;
1313 if (!refgen || refgen->op_type != OP_REFGEN)
1316 exlist = (LISTOP *)refgen->op_first;
1317 if (!exlist || exlist->op_type != OP_NULL
1318 || exlist->op_targ != OP_LIST)
1321 if (exlist->op_first->op_type != OP_PUSHMARK)
1324 rv2cv = (UNOP*)exlist->op_last;
1326 if (rv2cv->op_type != OP_RV2CV)
1329 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1330 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1331 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1333 o->op_private |= OPpASSIGN_CV_TO_GV;
1334 rv2gv->op_private |= OPpDONT_INIT_GV;
1335 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1347 kid = cLOGOPo->op_first;
1348 if (kid->op_type == OP_NOT
1349 && (kid->op_flags & OPf_KIDS)
1351 if (o->op_type == OP_AND) {
1353 o->op_ppaddr = PL_ppaddr[OP_OR];
1355 o->op_type = OP_AND;
1356 o->op_ppaddr = PL_ppaddr[OP_AND];
1365 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1370 if (o->op_flags & OPf_STACKED)
1377 if (!(o->op_flags & OPf_KIDS))
1388 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1398 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %"SVf" in void context",
1399 newSVpvn_flags(useless, strlen(useless),
1400 SVs_TEMP | ( useless_is_utf8 ? SVf_UTF8 : 0 )));
1405 S_listkids(pTHX_ OP *o)
1407 if (o && o->op_flags & OPf_KIDS) {
1409 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1416 Perl_list(pTHX_ OP *o)
1421 /* assumes no premature commitment */
1422 if (!o || (o->op_flags & OPf_WANT)
1423 || (PL_parser && PL_parser->error_count)
1424 || o->op_type == OP_RETURN)
1429 if ((o->op_private & OPpTARGET_MY)
1430 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1432 return o; /* As if inside SASSIGN */
1435 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1437 switch (o->op_type) {
1440 list(cBINOPo->op_first);
1445 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1453 if (!(o->op_flags & OPf_KIDS))
1455 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1456 list(cBINOPo->op_first);
1457 return gen_constant_list(o);
1464 kid = cLISTOPo->op_first;
1466 kid = kid->op_sibling;
1469 OP *sib = kid->op_sibling;
1470 if (sib && kid->op_type != OP_LEAVEWHEN)
1476 PL_curcop = &PL_compiling;
1480 kid = cLISTOPo->op_first;
1487 S_scalarseq(pTHX_ OP *o)
1491 const OPCODE type = o->op_type;
1493 if (type == OP_LINESEQ || type == OP_SCOPE ||
1494 type == OP_LEAVE || type == OP_LEAVETRY)
1497 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1498 if (kid->op_sibling) {
1502 PL_curcop = &PL_compiling;
1504 o->op_flags &= ~OPf_PARENS;
1505 if (PL_hints & HINT_BLOCK_SCOPE)
1506 o->op_flags |= OPf_PARENS;
1509 o = newOP(OP_STUB, 0);
1514 S_modkids(pTHX_ OP *o, I32 type)
1516 if (o && o->op_flags & OPf_KIDS) {
1518 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1519 op_lvalue(kid, type);
1525 =for apidoc finalize_optree
1527 This function finalizes the optree. Should be called directly after
1528 the complete optree is built. It does some additional
1529 checking which can't be done in the normal ck_xxx functions and makes
1530 the tree thread-safe.
1535 Perl_finalize_optree(pTHX_ OP* o)
1537 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1540 SAVEVPTR(PL_curcop);
1548 S_finalize_op(pTHX_ OP* o)
1550 PERL_ARGS_ASSERT_FINALIZE_OP;
1552 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1554 /* Make sure mad ops are also thread-safe */
1555 MADPROP *mp = o->op_madprop;
1557 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1558 OP *prop_op = (OP *) mp->mad_val;
1559 /* We only need "Relocate sv to the pad for thread safety.", but this
1560 easiest way to make sure it traverses everything */
1561 if (prop_op->op_type == OP_CONST)
1562 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1563 finalize_op(prop_op);
1570 switch (o->op_type) {
1573 PL_curcop = ((COP*)o); /* for warnings */
1577 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1578 && ckWARN(WARN_SYNTAX))
1580 if (o->op_sibling->op_sibling) {
1581 const OPCODE type = o->op_sibling->op_sibling->op_type;
1582 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1583 const line_t oldline = CopLINE(PL_curcop);
1584 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1585 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1586 "Statement unlikely to be reached");
1587 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1588 "\t(Maybe you meant system() when you said exec()?)\n");
1589 CopLINE_set(PL_curcop, oldline);
1596 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1597 GV * const gv = cGVOPo_gv;
1598 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1599 /* XXX could check prototype here instead of just carping */
1600 SV * const sv = sv_newmortal();
1601 gv_efullname3(sv, gv, NULL);
1602 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1603 "%"SVf"() called too early to check prototype",
1610 if (cSVOPo->op_private & OPpCONST_STRICT)
1611 no_bareword_allowed(o);
1615 case OP_METHOD_NAMED:
1616 /* Relocate sv to the pad for thread safety.
1617 * Despite being a "constant", the SV is written to,
1618 * for reference counts, sv_upgrade() etc. */
1619 if (cSVOPo->op_sv) {
1620 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
1621 if (o->op_type != OP_METHOD_NAMED &&
1622 (SvPADTMP(cSVOPo->op_sv) || SvPADMY(cSVOPo->op_sv)))
1624 /* If op_sv is already a PADTMP/MY then it is being used by
1625 * some pad, so make a copy. */
1626 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
1627 SvREADONLY_on(PAD_SVl(ix));
1628 SvREFCNT_dec(cSVOPo->op_sv);
1630 else if (o->op_type != OP_METHOD_NAMED
1631 && cSVOPo->op_sv == &PL_sv_undef) {
1632 /* PL_sv_undef is hack - it's unsafe to store it in the
1633 AV that is the pad, because av_fetch treats values of
1634 PL_sv_undef as a "free" AV entry and will merrily
1635 replace them with a new SV, causing pad_alloc to think
1636 that this pad slot is free. (When, clearly, it is not)
1638 SvOK_off(PAD_SVl(ix));
1639 SvPADTMP_on(PAD_SVl(ix));
1640 SvREADONLY_on(PAD_SVl(ix));
1643 SvREFCNT_dec(PAD_SVl(ix));
1644 SvPADTMP_on(cSVOPo->op_sv);
1645 PAD_SETSV(ix, cSVOPo->op_sv);
1646 /* XXX I don't know how this isn't readonly already. */
1647 SvREADONLY_on(PAD_SVl(ix));
1649 cSVOPo->op_sv = NULL;
1660 const char *key = NULL;
1663 if (((BINOP*)o)->op_last->op_type != OP_CONST)
1666 /* Make the CONST have a shared SV */
1667 svp = cSVOPx_svp(((BINOP*)o)->op_last);
1668 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv))
1669 && SvTYPE(sv) < SVt_PVMG && !SvROK(sv)) {
1670 key = SvPV_const(sv, keylen);
1671 lexname = newSVpvn_share(key,
1672 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
1678 if ((o->op_private & (OPpLVAL_INTRO)))
1681 rop = (UNOP*)((BINOP*)o)->op_first;
1682 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
1684 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
1685 if (!SvPAD_TYPED(lexname))
1687 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1688 if (!fields || !GvHV(*fields))
1690 key = SvPV_const(*svp, keylen);
1691 if (!hv_fetch(GvHV(*fields), key,
1692 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1693 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1694 "in variable %"SVf" of type %"HEKf,
1695 SVfARG(*svp), SVfARG(lexname),
1696 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1708 SVOP *first_key_op, *key_op;
1710 if ((o->op_private & (OPpLVAL_INTRO))
1711 /* I bet there's always a pushmark... */
1712 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
1713 /* hmmm, no optimization if list contains only one key. */
1715 rop = (UNOP*)((LISTOP*)o)->op_last;
1716 if (rop->op_type != OP_RV2HV)
1718 if (rop->op_first->op_type == OP_PADSV)
1719 /* @$hash{qw(keys here)} */
1720 rop = (UNOP*)rop->op_first;
1722 /* @{$hash}{qw(keys here)} */
1723 if (rop->op_first->op_type == OP_SCOPE
1724 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1726 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1732 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
1733 if (!SvPAD_TYPED(lexname))
1735 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1736 if (!fields || !GvHV(*fields))
1738 /* Again guessing that the pushmark can be jumped over.... */
1739 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
1740 ->op_first->op_sibling;
1741 for (key_op = first_key_op; key_op;
1742 key_op = (SVOP*)key_op->op_sibling) {
1743 if (key_op->op_type != OP_CONST)
1745 svp = cSVOPx_svp(key_op);
1746 key = SvPV_const(*svp, keylen);
1747 if (!hv_fetch(GvHV(*fields), key,
1748 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1749 Perl_croak(aTHX_ "No such class field \"%"SVf"\" "
1750 "in variable %"SVf" of type %"HEKf,
1751 SVfARG(*svp), SVfARG(lexname),
1752 HEKfARG(HvNAME_HEK(SvSTASH(lexname))));
1758 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1759 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1766 if (o->op_flags & OPf_KIDS) {
1768 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1774 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
1776 Propagate lvalue ("modifiable") context to an op and its children.
1777 I<type> represents the context type, roughly based on the type of op that
1778 would do the modifying, although C<local()> is represented by OP_NULL,
1779 because it has no op type of its own (it is signalled by a flag on
1782 This function detects things that can't be modified, such as C<$x+1>, and
1783 generates errors for them. For example, C<$x+1 = 2> would cause it to be
1784 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
1786 It also flags things that need to behave specially in an lvalue context,
1787 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
1793 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
1797 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1800 if (!o || (PL_parser && PL_parser->error_count))
1803 if ((o->op_private & OPpTARGET_MY)
1804 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1809 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
1811 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
1813 switch (o->op_type) {
1818 if ((o->op_flags & OPf_PARENS) || PL_madskills)
1822 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
1823 !(o->op_flags & OPf_STACKED)) {
1824 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1825 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
1826 poses, so we need it clear. */
1827 o->op_private &= ~1;
1828 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1829 assert(cUNOPo->op_first->op_type == OP_NULL);
1830 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1833 else { /* lvalue subroutine call */
1834 o->op_private |= OPpLVAL_INTRO
1835 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
1836 PL_modcount = RETURN_UNLIMITED_NUMBER;
1837 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1838 /* Potential lvalue context: */
1839 o->op_private |= OPpENTERSUB_INARGS;
1842 else { /* Compile-time error message: */
1843 OP *kid = cUNOPo->op_first;
1846 if (kid->op_type != OP_PUSHMARK) {
1847 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1849 "panic: unexpected lvalue entersub "
1850 "args: type/targ %ld:%"UVuf,
1851 (long)kid->op_type, (UV)kid->op_targ);
1852 kid = kLISTOP->op_first;
1854 while (kid->op_sibling)
1855 kid = kid->op_sibling;
1856 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1857 break; /* Postpone until runtime */
1860 kid = kUNOP->op_first;
1861 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1862 kid = kUNOP->op_first;
1863 if (kid->op_type == OP_NULL)
1865 "Unexpected constant lvalue entersub "
1866 "entry via type/targ %ld:%"UVuf,
1867 (long)kid->op_type, (UV)kid->op_targ);
1868 if (kid->op_type != OP_GV) {
1872 cv = GvCV(kGVOP_gv);
1882 if (flags & OP_LVALUE_NO_CROAK) return NULL;
1883 /* grep, foreach, subcalls, refgen */
1884 if (type == OP_GREPSTART || type == OP_ENTERSUB
1885 || type == OP_REFGEN || type == OP_LEAVESUBLV)
1887 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1888 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1890 : (o->op_type == OP_ENTERSUB
1891 ? "non-lvalue subroutine call"
1893 type ? PL_op_desc[type] : "local"));
1907 case OP_RIGHT_SHIFT:
1916 if (!(o->op_flags & OPf_STACKED))
1923 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1924 op_lvalue(kid, type);
1929 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1930 PL_modcount = RETURN_UNLIMITED_NUMBER;
1931 return o; /* Treat \(@foo) like ordinary list. */
1935 if (scalar_mod_type(o, type))
1937 ref(cUNOPo->op_first, o->op_type);
1941 if (type == OP_LEAVESUBLV)
1942 o->op_private |= OPpMAYBE_LVSUB;
1948 PL_modcount = RETURN_UNLIMITED_NUMBER;
1951 PL_hints |= HINT_BLOCK_SCOPE;
1952 if (type == OP_LEAVESUBLV)
1953 o->op_private |= OPpMAYBE_LVSUB;
1957 ref(cUNOPo->op_first, o->op_type);
1961 PL_hints |= HINT_BLOCK_SCOPE;
1970 case OP_AELEMFAST_LEX:
1977 PL_modcount = RETURN_UNLIMITED_NUMBER;
1978 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1979 return o; /* Treat \(@foo) like ordinary list. */
1980 if (scalar_mod_type(o, type))
1982 if (type == OP_LEAVESUBLV)
1983 o->op_private |= OPpMAYBE_LVSUB;
1987 if (!type) /* local() */
1988 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
1989 PAD_COMPNAME_SV(o->op_targ));
1998 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
2002 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
2008 if (type == OP_LEAVESUBLV)
2009 o->op_private |= OPpMAYBE_LVSUB;
2010 pad_free(o->op_targ);
2011 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
2012 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
2013 if (o->op_flags & OPf_KIDS)
2014 op_lvalue(cBINOPo->op_first->op_sibling, type);
2019 ref(cBINOPo->op_first, o->op_type);
2020 if (type == OP_ENTERSUB &&
2021 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
2022 o->op_private |= OPpLVAL_DEFER;
2023 if (type == OP_LEAVESUBLV)
2024 o->op_private |= OPpMAYBE_LVSUB;
2034 if (o->op_flags & OPf_KIDS)
2035 op_lvalue(cLISTOPo->op_last, type);
2040 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
2042 else if (!(o->op_flags & OPf_KIDS))
2044 if (o->op_targ != OP_LIST) {
2045 op_lvalue(cBINOPo->op_first, type);
2051 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2052 /* elements might be in void context because the list is
2053 in scalar context or because they are attribute sub calls */
2054 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
2055 op_lvalue(kid, type);
2059 if (type != OP_LEAVESUBLV)
2061 break; /* op_lvalue()ing was handled by ck_return() */
2067 /* [20011101.069] File test operators interpret OPf_REF to mean that
2068 their argument is a filehandle; thus \stat(".") should not set
2070 if (type == OP_REFGEN &&
2071 PL_check[o->op_type] == Perl_ck_ftst)
2074 if (type != OP_LEAVESUBLV)
2075 o->op_flags |= OPf_MOD;
2077 if (type == OP_AASSIGN || type == OP_SASSIGN)
2078 o->op_flags |= OPf_SPECIAL|OPf_REF;
2079 else if (!type) { /* local() */
2082 o->op_private |= OPpLVAL_INTRO;
2083 o->op_flags &= ~OPf_SPECIAL;
2084 PL_hints |= HINT_BLOCK_SCOPE;
2089 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2090 "Useless localization of %s", OP_DESC(o));
2093 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2094 && type != OP_LEAVESUBLV)
2095 o->op_flags |= OPf_REF;
2100 S_scalar_mod_type(const OP *o, I32 type)
2105 if (o && o->op_type == OP_RV2GV)
2129 case OP_RIGHT_SHIFT:
2150 S_is_handle_constructor(const OP *o, I32 numargs)
2152 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2154 switch (o->op_type) {
2162 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2175 S_refkids(pTHX_ OP *o, I32 type)
2177 if (o && o->op_flags & OPf_KIDS) {
2179 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2186 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2191 PERL_ARGS_ASSERT_DOREF;
2193 if (!o || (PL_parser && PL_parser->error_count))
2196 switch (o->op_type) {
2198 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2199 !(o->op_flags & OPf_STACKED)) {
2200 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2201 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2202 assert(cUNOPo->op_first->op_type == OP_NULL);
2203 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2204 o->op_flags |= OPf_SPECIAL;
2205 o->op_private &= ~1;
2207 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2208 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2209 : type == OP_RV2HV ? OPpDEREF_HV
2211 o->op_flags |= OPf_MOD;
2217 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2218 doref(kid, type, set_op_ref);
2221 if (type == OP_DEFINED)
2222 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2223 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2226 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2227 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2228 : type == OP_RV2HV ? OPpDEREF_HV
2230 o->op_flags |= OPf_MOD;
2237 o->op_flags |= OPf_REF;
2240 if (type == OP_DEFINED)
2241 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2242 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2248 o->op_flags |= OPf_REF;
2253 if (!(o->op_flags & OPf_KIDS))
2255 doref(cBINOPo->op_first, type, set_op_ref);
2259 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2260 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2261 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2262 : type == OP_RV2HV ? OPpDEREF_HV
2264 o->op_flags |= OPf_MOD;
2274 if (!(o->op_flags & OPf_KIDS))
2276 doref(cLISTOPo->op_last, type, set_op_ref);
2286 S_dup_attrlist(pTHX_ OP *o)
2291 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2293 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2294 * where the first kid is OP_PUSHMARK and the remaining ones
2295 * are OP_CONST. We need to push the OP_CONST values.
2297 if (o->op_type == OP_CONST)
2298 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2300 else if (o->op_type == OP_NULL)
2304 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2306 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2307 if (o->op_type == OP_CONST)
2308 rop = op_append_elem(OP_LIST, rop,
2309 newSVOP(OP_CONST, o->op_flags,
2310 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2317 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
2322 PERL_ARGS_ASSERT_APPLY_ATTRS;
2324 /* fake up C<use attributes $pkg,$rv,@attrs> */
2325 ENTER; /* need to protect against side-effects of 'use' */
2326 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2328 #define ATTRSMODULE "attributes"
2329 #define ATTRSMODULE_PM "attributes.pm"
2332 /* Don't force the C<use> if we don't need it. */
2333 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2334 if (svp && *svp != &PL_sv_undef)
2335 NOOP; /* already in %INC */
2337 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2338 newSVpvs(ATTRSMODULE), NULL);
2341 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2342 newSVpvs(ATTRSMODULE),
2344 op_prepend_elem(OP_LIST,
2345 newSVOP(OP_CONST, 0, stashsv),
2346 op_prepend_elem(OP_LIST,
2347 newSVOP(OP_CONST, 0,
2349 dup_attrlist(attrs))));
2355 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2358 OP *pack, *imop, *arg;
2361 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2366 assert(target->op_type == OP_PADSV ||
2367 target->op_type == OP_PADHV ||
2368 target->op_type == OP_PADAV);
2370 /* Ensure that attributes.pm is loaded. */
2371 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
2373 /* Need package name for method call. */
2374 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2376 /* Build up the real arg-list. */
2377 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2379 arg = newOP(OP_PADSV, 0);
2380 arg->op_targ = target->op_targ;
2381 arg = op_prepend_elem(OP_LIST,
2382 newSVOP(OP_CONST, 0, stashsv),
2383 op_prepend_elem(OP_LIST,
2384 newUNOP(OP_REFGEN, 0,
2385 op_lvalue(arg, OP_REFGEN)),
2386 dup_attrlist(attrs)));
2388 /* Fake up a method call to import */
2389 meth = newSVpvs_share("import");
2390 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2391 op_append_elem(OP_LIST,
2392 op_prepend_elem(OP_LIST, pack, list(arg)),
2393 newSVOP(OP_METHOD_NAMED, 0, meth)));
2395 /* Combine the ops. */
2396 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2400 =notfor apidoc apply_attrs_string
2402 Attempts to apply a list of attributes specified by the C<attrstr> and
2403 C<len> arguments to the subroutine identified by the C<cv> argument which
2404 is expected to be associated with the package identified by the C<stashpv>
2405 argument (see L<attributes>). It gets this wrong, though, in that it
2406 does not correctly identify the boundaries of the individual attribute
2407 specifications within C<attrstr>. This is not really intended for the
2408 public API, but has to be listed here for systems such as AIX which
2409 need an explicit export list for symbols. (It's called from XS code
2410 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2411 to respect attribute syntax properly would be welcome.
2417 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2418 const char *attrstr, STRLEN len)
2422 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2425 len = strlen(attrstr);
2429 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2431 const char * const sstr = attrstr;
2432 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2433 attrs = op_append_elem(OP_LIST, attrs,
2434 newSVOP(OP_CONST, 0,
2435 newSVpvn(sstr, attrstr-sstr)));
2439 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2440 newSVpvs(ATTRSMODULE),
2441 NULL, op_prepend_elem(OP_LIST,
2442 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2443 op_prepend_elem(OP_LIST,
2444 newSVOP(OP_CONST, 0,
2445 newRV(MUTABLE_SV(cv))),
2450 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2454 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2456 PERL_ARGS_ASSERT_MY_KID;
2458 if (!o || (PL_parser && PL_parser->error_count))
2462 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2463 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2467 if (type == OP_LIST) {
2469 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2470 my_kid(kid, attrs, imopsp);
2472 } else if (type == OP_UNDEF
2478 } else if (type == OP_RV2SV || /* "our" declaration */
2480 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2481 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2482 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2484 PL_parser->in_my == KEY_our
2486 : PL_parser->in_my == KEY_state ? "state" : "my"));
2488 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2489 PL_parser->in_my = FALSE;
2490 PL_parser->in_my_stash = NULL;
2491 apply_attrs(GvSTASH(gv),
2492 (type == OP_RV2SV ? GvSV(gv) :
2493 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2494 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2497 o->op_private |= OPpOUR_INTRO;
2500 else if (type != OP_PADSV &&
2503 type != OP_PUSHMARK)
2505 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2507 PL_parser->in_my == KEY_our
2509 : PL_parser->in_my == KEY_state ? "state" : "my"));
2512 else if (attrs && type != OP_PUSHMARK) {
2515 PL_parser->in_my = FALSE;
2516 PL_parser->in_my_stash = NULL;
2518 /* check for C<my Dog $spot> when deciding package */
2519 stash = PAD_COMPNAME_TYPE(o->op_targ);
2521 stash = PL_curstash;
2522 apply_attrs_my(stash, o, attrs, imopsp);
2524 o->op_flags |= OPf_MOD;
2525 o->op_private |= OPpLVAL_INTRO;
2527 o->op_private |= OPpPAD_STATE;
2532 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2536 int maybe_scalar = 0;
2538 PERL_ARGS_ASSERT_MY_ATTRS;
2540 /* [perl #17376]: this appears to be premature, and results in code such as
2541 C< our(%x); > executing in list mode rather than void mode */
2543 if (o->op_flags & OPf_PARENS)
2553 o = my_kid(o, attrs, &rops);
2555 if (maybe_scalar && o->op_type == OP_PADSV) {
2556 o = scalar(op_append_list(OP_LIST, rops, o));
2557 o->op_private |= OPpLVAL_INTRO;
2560 /* The listop in rops might have a pushmark at the beginning,
2561 which will mess up list assignment. */
2562 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2563 if (rops->op_type == OP_LIST &&
2564 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2566 OP * const pushmark = lrops->op_first;
2567 lrops->op_first = pushmark->op_sibling;
2570 o = op_append_list(OP_LIST, o, rops);
2573 PL_parser->in_my = FALSE;
2574 PL_parser->in_my_stash = NULL;
2579 Perl_sawparens(pTHX_ OP *o)
2581 PERL_UNUSED_CONTEXT;
2583 o->op_flags |= OPf_PARENS;
2588 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2592 const OPCODE ltype = left->op_type;
2593 const OPCODE rtype = right->op_type;
2595 PERL_ARGS_ASSERT_BIND_MATCH;
2597 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2598 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2600 const char * const desc
2602 rtype == OP_SUBST || rtype == OP_TRANS
2603 || rtype == OP_TRANSR
2605 ? (int)rtype : OP_MATCH];
2606 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2609 (ltype == OP_RV2AV || ltype == OP_RV2HV)
2610 ? cUNOPx(left)->op_first->op_type == OP_GV
2611 && (gv = cGVOPx_gv(cUNOPx(left)->op_first))
2612 ? varname(gv, isary ? '@' : '%', 0, NULL, 0, 1)
2615 (GV *)PL_compcv, isary ? '@' : '%', left->op_targ, NULL, 0, 1
2618 Perl_warner(aTHX_ packWARN(WARN_MISC),
2619 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2622 const char * const sample = (isary
2623 ? "@array" : "%hash");
2624 Perl_warner(aTHX_ packWARN(WARN_MISC),
2625 "Applying %s to %s will act on scalar(%s)",
2626 desc, sample, sample);
2630 if (rtype == OP_CONST &&
2631 cSVOPx(right)->op_private & OPpCONST_BARE &&
2632 cSVOPx(right)->op_private & OPpCONST_STRICT)
2634 no_bareword_allowed(right);
2637 /* !~ doesn't make sense with /r, so error on it for now */
2638 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2640 yyerror("Using !~ with s///r doesn't make sense");
2641 if (rtype == OP_TRANSR && type == OP_NOT)
2642 yyerror("Using !~ with tr///r doesn't make sense");
2644 ismatchop = (rtype == OP_MATCH ||
2645 rtype == OP_SUBST ||
2646 rtype == OP_TRANS || rtype == OP_TRANSR)
2647 && !(right->op_flags & OPf_SPECIAL);
2648 if (ismatchop && right->op_private & OPpTARGET_MY) {
2650 right->op_private &= ~OPpTARGET_MY;
2652 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2655 right->op_flags |= OPf_STACKED;
2656 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
2657 ! (rtype == OP_TRANS &&
2658 right->op_private & OPpTRANS_IDENTICAL) &&
2659 ! (rtype == OP_SUBST &&
2660 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2661 newleft = op_lvalue(left, rtype);
2664 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
2665 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2667 o = op_prepend_elem(rtype, scalar(newleft), right);
2669 return newUNOP(OP_NOT, 0, scalar(o));
2673 return bind_match(type, left,
2674 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
2678 Perl_invert(pTHX_ OP *o)
2682 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2686 =for apidoc Amx|OP *|op_scope|OP *o
2688 Wraps up an op tree with some additional ops so that at runtime a dynamic
2689 scope will be created. The original ops run in the new dynamic scope,
2690 and then, provided that they exit normally, the scope will be unwound.
2691 The additional ops used to create and unwind the dynamic scope will
2692 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
2693 instead if the ops are simple enough to not need the full dynamic scope
2700 Perl_op_scope(pTHX_ OP *o)
2704 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2705 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2706 o->op_type = OP_LEAVE;
2707 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2709 else if (o->op_type == OP_LINESEQ) {
2711 o->op_type = OP_SCOPE;
2712 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2713 kid = ((LISTOP*)o)->op_first;
2714 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2717 /* The following deals with things like 'do {1 for 1}' */
2718 kid = kid->op_sibling;
2720 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2725 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2731 Perl_block_start(pTHX_ int full)
2734 const int retval = PL_savestack_ix;
2736 pad_block_start(full);
2738 PL_hints &= ~HINT_BLOCK_SCOPE;
2739 SAVECOMPILEWARNINGS();
2740 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2742 CALL_BLOCK_HOOKS(bhk_start, full);
2748 Perl_block_end(pTHX_ I32 floor, OP *seq)
2751 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2752 OP* retval = scalarseq(seq);
2754 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
2757 CopHINTS_set(&PL_compiling, PL_hints);
2759 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2762 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
2768 =head1 Compile-time scope hooks
2770 =for apidoc Aox||blockhook_register
2772 Register a set of hooks to be called when the Perl lexical scope changes
2773 at compile time. See L<perlguts/"Compile-time scope hooks">.
2779 Perl_blockhook_register(pTHX_ BHK *hk)
2781 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
2783 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
2790 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
2791 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2792 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2795 OP * const o = newOP(OP_PADSV, 0);
2796 o->op_targ = offset;
2802 Perl_newPROG(pTHX_ OP *o)
2806 PERL_ARGS_ASSERT_NEWPROG;
2813 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2814 ((PL_in_eval & EVAL_KEEPERR)
2815 ? OPf_SPECIAL : 0), o);
2817 cx = &cxstack[cxstack_ix];
2818 assert(CxTYPE(cx) == CXt_EVAL);
2820 if ((cx->blk_gimme & G_WANT) == G_VOID)
2821 scalarvoid(PL_eval_root);
2822 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
2825 scalar(PL_eval_root);
2827 /* don't use LINKLIST, since PL_eval_root might indirect through
2828 * a rather expensive function call and LINKLIST evaluates its
2829 * argument more than once */
2830 PL_eval_start = op_linklist(PL_eval_root);
2831 PL_eval_root->op_private |= OPpREFCOUNTED;
2832 OpREFCNT_set(PL_eval_root, 1);
2833 PL_eval_root->op_next = 0;
2834 i = PL_savestack_ix;
2837 CALL_PEEP(PL_eval_start);
2838 finalize_optree(PL_eval_root);
2840 PL_savestack_ix = i;
2843 if (o->op_type == OP_STUB) {
2844 PL_comppad_name = 0;
2846 S_op_destroy(aTHX_ o);
2849 PL_main_root = op_scope(sawparens(scalarvoid(o)));
2850 PL_curcop = &PL_compiling;
2851 PL_main_start = LINKLIST(PL_main_root);
2852 PL_main_root->op_private |= OPpREFCOUNTED;
2853 OpREFCNT_set(PL_main_root, 1);
2854 PL_main_root->op_next = 0;
2855 CALL_PEEP(PL_main_start);
2856 finalize_optree(PL_main_root);
2859 /* Register with debugger */
2861 CV * const cv = get_cvs("DB::postponed", 0);
2865 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2867 call_sv(MUTABLE_SV(cv), G_DISCARD);
2874 Perl_localize(pTHX_ OP *o, I32 lex)
2878 PERL_ARGS_ASSERT_LOCALIZE;
2880 if (o->op_flags & OPf_PARENS)
2881 /* [perl #17376]: this appears to be premature, and results in code such as
2882 C< our(%x); > executing in list mode rather than void mode */
2889 if ( PL_parser->bufptr > PL_parser->oldbufptr
2890 && PL_parser->bufptr[-1] == ','
2891 && ckWARN(WARN_PARENTHESIS))
2893 char *s = PL_parser->bufptr;
2896 /* some heuristics to detect a potential error */
2897 while (*s && (strchr(", \t\n", *s)))
2901 if (*s && strchr("@$%*", *s) && *++s
2902 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2905 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2907 while (*s && (strchr(", \t\n", *s)))
2913 if (sigil && (*s == ';' || *s == '=')) {
2914 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2915 "Parentheses missing around \"%s\" list",
2917 ? (PL_parser->in_my == KEY_our
2919 : PL_parser->in_my == KEY_state
2929 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
2930 PL_parser->in_my = FALSE;
2931 PL_parser->in_my_stash = NULL;
2936 Perl_jmaybe(pTHX_ OP *o)
2938 PERL_ARGS_ASSERT_JMAYBE;
2940 if (o->op_type == OP_LIST) {
2942 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2943 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
2948 PERL_STATIC_INLINE OP *
2949 S_op_std_init(pTHX_ OP *o)
2951 I32 type = o->op_type;
2953 PERL_ARGS_ASSERT_OP_STD_INIT;
2955 if (PL_opargs[type] & OA_RETSCALAR)
2957 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2958 o->op_targ = pad_alloc(type, SVs_PADTMP);
2963 PERL_STATIC_INLINE OP *
2964 S_op_integerize(pTHX_ OP *o)
2966 I32 type = o->op_type;
2968 PERL_ARGS_ASSERT_OP_INTEGERIZE;
2970 /* integerize op, unless it happens to be C<-foo>.
2971 * XXX should pp_i_negate() do magic string negation instead? */
2972 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2973 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2974 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2977 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2980 if (type == OP_NEGATE)
2981 /* XXX might want a ck_negate() for this */
2982 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2988 S_fold_constants(pTHX_ register OP *o)
2991 register OP * VOL curop;
2993 VOL I32 type = o->op_type;
2998 SV * const oldwarnhook = PL_warnhook;
2999 SV * const olddiehook = PL_diehook;
3003 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
3005 if (!(PL_opargs[type] & OA_FOLDCONST))
3019 /* XXX what about the numeric ops? */
3020 if (IN_LOCALE_COMPILETIME)
3024 if (o->op_private & OPpREPEAT_DOLIST) goto nope;
3027 if (PL_parser && PL_parser->error_count)
3028 goto nope; /* Don't try to run w/ errors */
3030 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3031 const OPCODE type = curop->op_type;
3032 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
3034 type != OP_SCALAR &&
3036 type != OP_PUSHMARK)
3042 curop = LINKLIST(o);
3043 old_next = o->op_next;
3047 oldscope = PL_scopestack_ix;
3048 create_eval_scope(G_FAKINGEVAL);
3050 /* Verify that we don't need to save it: */
3051 assert(PL_curcop == &PL_compiling);
3052 StructCopy(&PL_compiling, ¬_compiling, COP);
3053 PL_curcop = ¬_compiling;
3054 /* The above ensures that we run with all the correct hints of the
3055 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
3056 assert(IN_PERL_RUNTIME);
3057 PL_warnhook = PERL_WARNHOOK_FATAL;
3064 sv = *(PL_stack_sp--);
3065 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
3067 /* Can't simply swipe the SV from the pad, because that relies on
3068 the op being freed "real soon now". Under MAD, this doesn't
3069 happen (see the #ifdef below). */
3072 pad_swipe(o->op_targ, FALSE);
3075 else if (SvTEMP(sv)) { /* grab mortal temp? */
3076 SvREFCNT_inc_simple_void(sv);
3081 /* Something tried to die. Abandon constant folding. */
3082 /* Pretend the error never happened. */
3084 o->op_next = old_next;
3088 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3089 PL_warnhook = oldwarnhook;
3090 PL_diehook = olddiehook;
3091 /* XXX note that this croak may fail as we've already blown away
3092 * the stack - eg any nested evals */
3093 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3096 PL_warnhook = oldwarnhook;
3097 PL_diehook = olddiehook;
3098 PL_curcop = &PL_compiling;
3100 if (PL_scopestack_ix > oldscope)
3101 delete_eval_scope();
3110 if (type == OP_RV2GV)
3111 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3113 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3114 op_getmad(o,newop,'f');
3122 S_gen_constant_list(pTHX_ register OP *o)
3126 const I32 oldtmps_floor = PL_tmps_floor;
3129 if (PL_parser && PL_parser->error_count)
3130 return o; /* Don't attempt to run with errors */
3132 PL_op = curop = LINKLIST(o);
3135 Perl_pp_pushmark(aTHX);
3138 assert (!(curop->op_flags & OPf_SPECIAL));
3139 assert(curop->op_type == OP_RANGE);
3140 Perl_pp_anonlist(aTHX);
3141 PL_tmps_floor = oldtmps_floor;
3143 o->op_type = OP_RV2AV;
3144 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3145 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3146 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3147 o->op_opt = 0; /* needs to be revisited in rpeep() */
3148 curop = ((UNOP*)o)->op_first;
3149 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
3151 op_getmad(curop,o,'O');
3160 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3163 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3164 if (!o || o->op_type != OP_LIST)
3165 o = newLISTOP(OP_LIST, 0, o, NULL);
3167 o->op_flags &= ~OPf_WANT;
3169 if (!(PL_opargs[type] & OA_MARK))
3170 op_null(cLISTOPo->op_first);
3172 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3173 if (kid2 && kid2->op_type == OP_COREARGS) {
3174 op_null(cLISTOPo->op_first);
3175 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3179 o->op_type = (OPCODE)type;
3180 o->op_ppaddr = PL_ppaddr[type];
3181 o->op_flags |= flags;
3183 o = CHECKOP(type, o);
3184 if (o->op_type != (unsigned)type)
3187 return fold_constants(op_integerize(op_std_init(o)));
3191 =head1 Optree Manipulation Functions
3194 /* List constructors */
3197 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3199 Append an item to the list of ops contained directly within a list-type
3200 op, returning the lengthened list. I<first> is the list-type op,
3201 and I<last> is the op to append to the list. I<optype> specifies the
3202 intended opcode for the list. If I<first> is not already a list of the
3203 right type, it will be upgraded into one. If either I<first> or I<last>
3204 is null, the other is returned unchanged.
3210 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3218 if (first->op_type != (unsigned)type
3219 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3221 return newLISTOP(type, 0, first, last);
3224 if (first->op_flags & OPf_KIDS)
3225 ((LISTOP*)first)->op_last->op_sibling = last;
3227 first->op_flags |= OPf_KIDS;
3228 ((LISTOP*)first)->op_first = last;
3230 ((LISTOP*)first)->op_last = last;
3235 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3237 Concatenate the lists of ops contained directly within two list-type ops,
3238 returning the combined list. I<first> and I<last> are the list-type ops
3239 to concatenate. I<optype> specifies the intended opcode for the list.
3240 If either I<first> or I<last> is not already a list of the right type,
3241 it will be upgraded into one. If either I<first> or I<last> is null,
3242 the other is returned unchanged.
3248 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3256 if (first->op_type != (unsigned)type)
3257 return op_prepend_elem(type, first, last);
3259 if (last->op_type != (unsigned)type)
3260 return op_append_elem(type, first, last);
3262 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3263 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3264 first->op_flags |= (last->op_flags & OPf_KIDS);
3267 if (((LISTOP*)last)->op_first && first->op_madprop) {
3268 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3270 while (mp->mad_next)
3272 mp->mad_next = first->op_madprop;
3275 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3278 first->op_madprop = last->op_madprop;
3279 last->op_madprop = 0;
3282 S_op_destroy(aTHX_ last);
3288 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3290 Prepend an item to the list of ops contained directly within a list-type
3291 op, returning the lengthened list. I<first> is the op to prepend to the
3292 list, and I<last> is the list-type op. I<optype> specifies the intended
3293 opcode for the list. If I<last> is not already a list of the right type,
3294 it will be upgraded into one. If either I<first> or I<last> is null,
3295 the other is returned unchanged.
3301 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3309 if (last->op_type == (unsigned)type) {
3310 if (type == OP_LIST) { /* already a PUSHMARK there */
3311 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3312 ((LISTOP*)last)->op_first->op_sibling = first;
3313 if (!(first->op_flags & OPf_PARENS))
3314 last->op_flags &= ~OPf_PARENS;
3317 if (!(last->op_flags & OPf_KIDS)) {
3318 ((LISTOP*)last)->op_last = first;
3319 last->op_flags |= OPf_KIDS;
3321 first->op_sibling = ((LISTOP*)last)->op_first;
3322 ((LISTOP*)last)->op_first = first;
3324 last->op_flags |= OPf_KIDS;
3328 return newLISTOP(type, 0, first, last);
3336 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3339 Newxz(tk, 1, TOKEN);
3340 tk->tk_type = (OPCODE)optype;
3341 tk->tk_type = 12345;
3343 tk->tk_mad = madprop;
3348 Perl_token_free(pTHX_ TOKEN* tk)
3350 PERL_ARGS_ASSERT_TOKEN_FREE;
3352 if (tk->tk_type != 12345)
3354 mad_free(tk->tk_mad);
3359 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3364 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3366 if (tk->tk_type != 12345) {
3367 Perl_warner(aTHX_ packWARN(WARN_MISC),
3368 "Invalid TOKEN object ignored");
3375 /* faked up qw list? */
3377 tm->mad_type == MAD_SV &&
3378 SvPVX((SV *)tm->mad_val)[0] == 'q')
3385 /* pretend constant fold didn't happen? */
3386 if (mp->mad_key == 'f' &&
3387 (o->op_type == OP_CONST ||
3388 o->op_type == OP_GV) )
3390 token_getmad(tk,(OP*)mp->mad_val,slot);
3404 if (mp->mad_key == 'X')
3405 mp->mad_key = slot; /* just change the first one */
3415 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3424 /* pretend constant fold didn't happen? */
3425 if (mp->mad_key == 'f' &&
3426 (o->op_type == OP_CONST ||
3427 o->op_type == OP_GV) )
3429 op_getmad(from,(OP*)mp->mad_val,slot);
3436 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3439 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3445 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3454 /* pretend constant fold didn't happen? */
3455 if (mp->mad_key == 'f' &&
3456 (o->op_type == OP_CONST ||
3457 o->op_type == OP_GV) )
3459 op_getmad(from,(OP*)mp->mad_val,slot);
3466 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
3469 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
3473 PerlIO_printf(PerlIO_stderr(),
3474 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
3480 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
3498 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
3502 addmad(tm, &(o->op_madprop), slot);
3506 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
3527 Perl_newMADsv(pTHX_ char key, SV* sv)
3529 PERL_ARGS_ASSERT_NEWMADSV;
3531 return newMADPROP(key, MAD_SV, sv, 0);
3535 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3537 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
3540 mp->mad_vlen = vlen;
3541 mp->mad_type = type;
3543 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
3548 Perl_mad_free(pTHX_ MADPROP* mp)
3550 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3554 mad_free(mp->mad_next);
3555 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3556 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3557 switch (mp->mad_type) {
3561 Safefree((char*)mp->mad_val);
3564 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
3565 op_free((OP*)mp->mad_val);
3568 sv_free(MUTABLE_SV(mp->mad_val));
3571 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3574 PerlMemShared_free(mp);
3580 =head1 Optree construction
3582 =for apidoc Am|OP *|newNULLLIST
3584 Constructs, checks, and returns a new C<stub> op, which represents an
3585 empty list expression.
3591 Perl_newNULLLIST(pTHX)
3593 return newOP(OP_STUB, 0);
3597 S_force_list(pTHX_ OP *o)
3599 if (!o || o->op_type != OP_LIST)
3600 o = newLISTOP(OP_LIST, 0, o, NULL);
3606 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3608 Constructs, checks, and returns an op of any list type. I<type> is
3609 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3610 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3611 supply up to two ops to be direct children of the list op; they are
3612 consumed by this function and become part of the constructed op tree.
3618 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3623 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3625 NewOp(1101, listop, 1, LISTOP);
3627 listop->op_type = (OPCODE)type;
3628 listop->op_ppaddr = PL_ppaddr[type];
3631 listop->op_flags = (U8)flags;
3635 else if (!first && last)
3638 first->op_sibling = last;
3639 listop->op_first = first;
3640 listop->op_last = last;
3641 if (type == OP_LIST) {
3642 OP* const pushop = newOP(OP_PUSHMARK, 0);
3643 pushop->op_sibling = first;
3644 listop->op_first = pushop;
3645 listop->op_flags |= OPf_KIDS;
3647 listop->op_last = pushop;
3650 return CHECKOP(type, listop);
3654 =for apidoc Am|OP *|newOP|I32 type|I32 flags
3656 Constructs, checks, and returns an op of any base type (any type that
3657 has no extra fields). I<type> is the opcode. I<flags> gives the
3658 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3665 Perl_newOP(pTHX_ I32 type, I32 flags)
3670 if (type == -OP_ENTEREVAL) {
3671 type = OP_ENTEREVAL;
3672 flags |= OPpEVAL_BYTES<<8;
3675 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3676 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3677 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3678 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3680 NewOp(1101, o, 1, OP);
3681 o->op_type = (OPCODE)type;
3682 o->op_ppaddr = PL_ppaddr[type];
3683 o->op_flags = (U8)flags;
3685 o->op_latefreed = 0;
3689 o->op_private = (U8)(0 | (flags >> 8));
3690 if (PL_opargs[type] & OA_RETSCALAR)
3692 if (PL_opargs[type] & OA_TARGET)
3693 o->op_targ = pad_alloc(type, SVs_PADTMP);
3694 return CHECKOP(type, o);
3698 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3700 Constructs, checks, and returns an op of any unary type. I<type> is
3701 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3702 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3703 bits, the eight bits of C<op_private>, except that the bit with value 1
3704 is automatically set. I<first> supplies an optional op to be the direct
3705 child of the unary op; it is consumed by this function and become part
3706 of the constructed op tree.
3712 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3717 if (type == -OP_ENTEREVAL) {
3718 type = OP_ENTEREVAL;
3719 flags |= OPpEVAL_BYTES<<8;
3722 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3723 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3724 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3725 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3726 || type == OP_SASSIGN
3727 || type == OP_ENTERTRY
3728 || type == OP_NULL );
3731 first = newOP(OP_STUB, 0);
3732 if (PL_opargs[type] & OA_MARK)
3733 first = force_list(first);
3735 NewOp(1101, unop, 1, UNOP);
3736 unop->op_type = (OPCODE)type;
3737 unop->op_ppaddr = PL_ppaddr[type];
3738 unop->op_first = first;
3739 unop->op_flags = (U8)(flags | OPf_KIDS);
3740 unop->op_private = (U8)(1 | (flags >> 8));
3741 unop = (UNOP*) CHECKOP(type, unop);
3745 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3749 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3751 Constructs, checks, and returns an op of any binary type. I<type>
3752 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3753 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3754 the eight bits of C<op_private>, except that the bit with value 1 or
3755 2 is automatically set as required. I<first> and I<last> supply up to
3756 two ops to be the direct children of the binary op; they are consumed
3757 by this function and become part of the constructed op tree.
3763 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3768 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3769 || type == OP_SASSIGN || type == OP_NULL );
3771 NewOp(1101, binop, 1, BINOP);
3774 first = newOP(OP_NULL, 0);
3776 binop->op_type = (OPCODE)type;
3777 binop->op_ppaddr = PL_ppaddr[type];
3778 binop->op_first = first;
3779 binop->op_flags = (U8)(flags | OPf_KIDS);
3782 binop->op_private = (U8)(1 | (flags >> 8));
3785 binop->op_private = (U8)(2 | (flags >> 8));
3786 first->op_sibling = last;
3789 binop = (BINOP*)CHECKOP(type, binop);
3790 if (binop->op_next || binop->op_type != (OPCODE)type)
3793 binop->op_last = binop->op_first->op_sibling;
3795 return fold_constants(op_integerize(op_std_init((OP *)binop)));
3798 static int uvcompare(const void *a, const void *b)
3799 __attribute__nonnull__(1)
3800 __attribute__nonnull__(2)
3801 __attribute__pure__;
3802 static int uvcompare(const void *a, const void *b)
3804 if (*((const UV *)a) < (*(const UV *)b))
3806 if (*((const UV *)a) > (*(const UV *)b))
3808 if (*((const UV *)a+1) < (*(const UV *)b+1))
3810 if (*((const UV *)a+1) > (*(const UV *)b+1))
3816 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3819 SV * const tstr = ((SVOP*)expr)->op_sv;
3822 (repl->op_type == OP_NULL)
3823 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3825 ((SVOP*)repl)->op_sv;
3828 const U8 *t = (U8*)SvPV_const(tstr, tlen);
3829 const U8 *r = (U8*)SvPV_const(rstr, rlen);
3833 register short *tbl;
3835 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3836 const I32 squash = o->op_private & OPpTRANS_SQUASH;
3837 I32 del = o->op_private & OPpTRANS_DELETE;
3840 PERL_ARGS_ASSERT_PMTRANS;
3842 PL_hints |= HINT_BLOCK_SCOPE;
3845 o->op_private |= OPpTRANS_FROM_UTF;
3848 o->op_private |= OPpTRANS_TO_UTF;
3850 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3851 SV* const listsv = newSVpvs("# comment\n");
3853 const U8* tend = t + tlen;
3854 const U8* rend = r + rlen;
3868 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
3869 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
3872 const U32 flags = UTF8_ALLOW_DEFAULT;
3876 t = tsave = bytes_to_utf8(t, &len);
3879 if (!to_utf && rlen) {
3881 r = rsave = bytes_to_utf8(r, &len);
3885 /* There are several snags with this code on EBCDIC:
3886 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
3887 2. scan_const() in toke.c has encoded chars in native encoding which makes
3888 ranges at least in EBCDIC 0..255 range the bottom odd.
3892 U8 tmpbuf[UTF8_MAXBYTES+1];
3895 Newx(cp, 2*tlen, UV);
3897 transv = newSVpvs("");
3899 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3901 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
3903 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3907 cp[2*i+1] = cp[2*i];
3911 qsort(cp, i, 2*sizeof(UV), uvcompare);
3912 for (j = 0; j < i; j++) {
3914 diff = val - nextmin;
3916 t = uvuni_to_utf8(tmpbuf,nextmin);
3917 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3919 U8 range_mark = UTF_TO_NATIVE(0xff);
3920 t = uvuni_to_utf8(tmpbuf, val - 1);
3921 sv_catpvn(transv, (char *)&range_mark, 1);
3922 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3929 t = uvuni_to_utf8(tmpbuf,nextmin);
3930 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3932 U8 range_mark = UTF_TO_NATIVE(0xff);
3933 sv_catpvn(transv, (char *)&range_mark, 1);
3935 t = uvuni_to_utf8(tmpbuf, 0x7fffffff);
3936 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3937 t = (const U8*)SvPVX_const(transv);
3938 tlen = SvCUR(transv);
3942 else if (!rlen && !del) {
3943 r = t; rlen = tlen; rend = tend;
3946 if ((!rlen && !del) || t == r ||
3947 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
3949 o->op_private |= OPpTRANS_IDENTICAL;
3953 while (t < tend || tfirst <= tlast) {
3954 /* see if we need more "t" chars */
3955 if (tfirst > tlast) {
3956 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3958 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
3960 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3967 /* now see if we need more "r" chars */
3968 if (rfirst > rlast) {
3970 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3972 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
3974 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3983 rfirst = rlast = 0xffffffff;
3987 /* now see which range will peter our first, if either. */
3988 tdiff = tlast - tfirst;
3989 rdiff = rlast - rfirst;
3996 if (rfirst == 0xffffffff) {
3997 diff = tdiff; /* oops, pretend rdiff is infinite */
3999 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
4000 (long)tfirst, (long)tlast);
4002 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
4006 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
4007 (long)tfirst, (long)(tfirst + diff),
4010 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
4011 (long)tfirst, (long)rfirst);
4013 if (rfirst + diff > max)
4014 max = rfirst + diff;
4016 grows = (tfirst < rfirst &&
4017 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
4029 else if (max > 0xff)
4034 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
4036 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
4037 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
4038 PAD_SETSV(cPADOPo->op_padix, swash);
4040 SvREADONLY_on(swash);
4042 cSVOPo->op_sv = swash;
4044 SvREFCNT_dec(listsv);
4045 SvREFCNT_dec(transv);
4047 if (!del && havefinal && rlen)
4048 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
4049 newSVuv((UV)final), 0);
4052 o->op_private |= OPpTRANS_GROWS;
4058 op_getmad(expr,o,'e');
4059 op_getmad(repl,o,'r');
4067 tbl = (short*)PerlMemShared_calloc(
4068 (o->op_private & OPpTRANS_COMPLEMENT) &&
4069 !(o->op_private & OPpTRANS_DELETE) ? 258 : 256,
4071 cPVOPo->op_pv = (char*)tbl;
4073 for (i = 0; i < (I32)tlen; i++)
4075 for (i = 0, j = 0; i < 256; i++) {
4077 if (j >= (I32)rlen) {
4086 if (i < 128 && r[j] >= 128)
4096 o->op_private |= OPpTRANS_IDENTICAL;
4098 else if (j >= (I32)rlen)
4103 PerlMemShared_realloc(tbl,
4104 (0x101+rlen-j) * sizeof(short));
4105 cPVOPo->op_pv = (char*)tbl;
4107 tbl[0x100] = (short)(rlen - j);
4108 for (i=0; i < (I32)rlen - j; i++)
4109 tbl[0x101+i] = r[j+i];
4113 if (!rlen && !del) {
4116 o->op_private |= OPpTRANS_IDENTICAL;
4118 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4119 o->op_private |= OPpTRANS_IDENTICAL;
4121 for (i = 0; i < 256; i++)
4123 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4124 if (j >= (I32)rlen) {
4126 if (tbl[t[i]] == -1)
4132 if (tbl[t[i]] == -1) {
4133 if (t[i] < 128 && r[j] >= 128)
4140 if(del && rlen == tlen) {
4141 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4142 } else if(rlen > tlen) {
4143 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4147 o->op_private |= OPpTRANS_GROWS;
4149 op_getmad(expr,o,'e');
4150 op_getmad(repl,o,'r');
4160 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4162 Constructs, checks, and returns an op of any pattern matching type.
4163 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4164 and, shifted up eight bits, the eight bits of C<op_private>.
4170 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4175 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4177 NewOp(1101, pmop, 1, PMOP);
4178 pmop->op_type = (OPCODE)type;
4179 pmop->op_ppaddr = PL_ppaddr[type];
4180 pmop->op_flags = (U8)flags;
4181 pmop->op_private = (U8)(0 | (flags >> 8));
4183 if (PL_hints & HINT_RE_TAINT)
4184 pmop->op_pmflags |= PMf_RETAINT;
4185 if (IN_LOCALE_COMPILETIME) {
4186 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4188 else if ((! (PL_hints & HINT_BYTES))
4189 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4190 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4192 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4194 if (PL_hints & HINT_RE_FLAGS) {
4195 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4196 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4198 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4199 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4200 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4202 if (reflags && SvOK(reflags)) {
4203 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4209 assert(SvPOK(PL_regex_pad[0]));
4210 if (SvCUR(PL_regex_pad[0])) {
4211 /* Pop off the "packed" IV from the end. */
4212 SV *const repointer_list = PL_regex_pad[0];
4213 const char *p = SvEND(repointer_list) - sizeof(IV);
4214 const IV offset = *((IV*)p);
4216 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4218 SvEND_set(repointer_list, p);
4220 pmop->op_pmoffset = offset;
4221 /* This slot should be free, so assert this: */
4222 assert(PL_regex_pad[offset] == &PL_sv_undef);
4224 SV * const repointer = &PL_sv_undef;
4225 av_push(PL_regex_padav, repointer);
4226 pmop->op_pmoffset = av_len(PL_regex_padav);
4227 PL_regex_pad = AvARRAY(PL_regex_padav);
4231 return CHECKOP(type, pmop);
4234 /* Given some sort of match op o, and an expression expr containing a
4235 * pattern, either compile expr into a regex and attach it to o (if it's
4236 * constant), or convert expr into a runtime regcomp op sequence (if it's
4239 * isreg indicates that the pattern is part of a regex construct, eg
4240 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4241 * split "pattern", which aren't. In the former case, expr will be a list
4242 * if the pattern contains more than one term (eg /a$b/) or if it contains
4243 * a replacement, ie s/// or tr///.
4247 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
4252 I32 repl_has_vars = 0;
4254 bool is_trans = (o->op_type == OP_TRANS || o->op_type == OP_TRANSR);
4255 bool is_compiletime;
4260 PERL_ARGS_ASSERT_PMRUNTIME;
4262 /* for s/// and tr///, last element in list is the replacement; pop it */
4264 if (is_trans || o->op_type == OP_SUBST) {
4266 repl = cLISTOPx(expr)->op_last;
4267 kid = cLISTOPx(expr)->op_first;
4268 while (kid->op_sibling != repl)
4269 kid = kid->op_sibling;
4270 kid->op_sibling = NULL;
4271 cLISTOPx(expr)->op_last = kid;
4274 /* for TRANS, convert LIST/PUSH/CONST into CONST, and pass to pmtrans() */
4277 OP* const oe = expr;
4278 assert(expr->op_type == OP_LIST);
4279 assert(cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK);
4280 assert(cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last);
4281 expr = cLISTOPx(oe)->op_last;
4282 cLISTOPx(oe)->op_first->op_sibling = NULL;
4283 cLISTOPx(oe)->op_last = NULL;
4286 return pmtrans(o, expr, repl);
4289 /* find whether we have any runtime or code elements */
4293 if (expr->op_type == OP_LIST) {
4295 for (o = cLISTOPx(expr)->op_first; o; o = o->op_sibling) {
4296 if (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL))
4298 else if (o->op_type != OP_CONST && o->op_type != OP_PUSHMARK)
4302 else { assert(expr->op_type != OP_PUSHMARK); if (expr->op_type != OP_CONST && expr->op_type != OP_PUSHMARK)
4306 /* are we using an external (non-perl) re engine? */
4308 eng = current_re_engine();
4309 ext_eng = (eng && eng != &PL_core_reg_engine);
4311 /* concatenate adjacent CONSTs, and for non-perl engines, strip out
4314 if (expr->op_type == OP_LIST
4315 && (!is_compiletime || /* XXX TMP until we handle runtime (?{}) */
4316 !has_code || ext_eng))
4319 o = cLISTOPx(expr)->op_first;
4320 while (o->op_sibling) {
4321 kid = o->op_sibling;
4322 if (kid->op_type == OP_NULL && (kid->op_flags & OPf_SPECIAL)) {
4324 o->op_sibling = kid->op_sibling;
4325 kid->op_sibling = NULL;
4328 else if (o->op_type == OP_CONST && kid->op_type == OP_CONST){
4329 SV* sv = cSVOPo->op_sv;
4331 sv_catsv(sv, cSVOPx(kid)->op_sv);
4333 o->op_sibling = kid->op_sibling;
4334 kid->op_sibling = NULL;
4340 cLISTOPx(expr)->op_last = o;
4343 PL_hints |= HINT_BLOCK_SCOPE;
4346 if (is_compiletime) {
4347 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4349 if (o->op_flags & OPf_SPECIAL)
4350 pm_flags |= RXf_SPLIT;
4352 if (!has_code || ext_eng) {
4354 assert( expr->op_type == OP_CONST
4355 || ( expr->op_type == OP_LIST
4356 && cLISTOPx(expr)->op_first->op_type == OP_PUSHMARK
4357 && cLISTOPx(expr)->op_first->op_sibling
4358 && cLISTOPx(expr)->op_first->op_sibling->op_type == OP_CONST
4359 && !cLISTOPx(expr)->op_first->op_sibling->op_sibling
4362 pat = ((SVOP*)(expr->op_type == OP_LIST
4363 ? cLISTOPx(expr)->op_first->op_sibling : expr))->op_sv;
4366 assert (SvUTF8(pat));
4367 } else if (SvUTF8(pat)) {
4368 /* Not doing UTF-8, despite what the SV says. Is this only if we're
4369 trapped in use 'bytes'? */
4370 /* Make a copy of the octet sequence, but without the flag on, as
4371 the compiler now honours the SvUTF8 flag on pat. */
4373 const char *const p = SvPV(pat, len);
4374 pat = newSVpvn_flags(p, len, SVs_TEMP);
4377 PM_SETRE(pm, CALLREGCOMP(pat, pm_flags));
4380 PM_SETRE(pm, re_op_compile(NULL, expr, pm_flags));
4383 op_getmad(expr,(OP*)pm,'e');
4391 reglist = isreg && expr->op_type == OP_LIST;
4395 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
4396 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
4398 : OP_REGCMAYBE),0,expr);
4400 NewOp(1101, rcop, 1, LOGOP);
4401 rcop->op_type = OP_REGCOMP;
4402 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4403 rcop->op_first = scalar(expr);
4404 rcop->op_flags |= OPf_KIDS
4405 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4406 | (reglist ? OPf_STACKED : 0);
4407 rcop->op_private = 1;
4410 rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
4412 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4413 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4415 /* establish postfix order */
4416 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
4418 rcop->op_next = expr;
4419 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4422 rcop->op_next = LINKLIST(expr);
4423 expr->op_next = (OP*)rcop;
4426 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4431 if (pm->op_pmflags & PMf_EVAL) {
4433 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
4434 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
4436 else if (repl->op_type == OP_CONST)
4440 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
4441 if (curop->op_type == OP_SCOPE
4442 || curop->op_type == OP_LEAVE
4443 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
4444 if (curop->op_type == OP_GV) {
4445 GV * const gv = cGVOPx_gv(curop);
4447 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
4450 else if (curop->op_type == OP_RV2CV)
4452 else if (curop->op_type == OP_RV2SV ||
4453 curop->op_type == OP_RV2AV ||
4454 curop->op_type == OP_RV2HV ||
4455 curop->op_type == OP_RV2GV) {
4456 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
4459 else if (curop->op_type == OP_PADSV ||
4460 curop->op_type == OP_PADAV ||
4461 curop->op_type == OP_PADHV ||
4462 curop->op_type == OP_PADANY)
4466 else if (curop->op_type == OP_PUSHRE)
4467 NOOP; /* Okay here, dangerous in newASSIGNOP */
4477 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4479 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4480 op_prepend_elem(o->op_type, scalar(repl), o);
4483 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
4484 pm->op_pmflags |= PMf_MAYBE_CONST;
4486 NewOp(1101, rcop, 1, LOGOP);
4487 rcop->op_type = OP_SUBSTCONT;
4488 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4489 rcop->op_first = scalar(repl);
4490 rcop->op_flags |= OPf_KIDS;
4491 rcop->op_private = 1;
4494 /* establish postfix order */
4495 rcop->op_next = LINKLIST(repl);
4496 repl->op_next = (OP*)rcop;
4498 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
4499 assert(!(pm->op_pmflags & PMf_ONCE));
4500 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
4509 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
4511 Constructs, checks, and returns an op of any type that involves an
4512 embedded SV. I<type> is the opcode. I<flags> gives the eight bits
4513 of C<op_flags>. I<sv> gives the SV to embed in the op; this function
4514 takes ownership of one reference to it.
4520 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
4525 PERL_ARGS_ASSERT_NEWSVOP;
4527 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4528 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4529 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4531 NewOp(1101, svop, 1, SVOP);
4532 svop->op_type = (OPCODE)type;
4533 svop->op_ppaddr = PL_ppaddr[type];
4535 svop->op_next = (OP*)svop;
4536 svop->op_flags = (U8)flags;
4537 if (PL_opargs[type] & OA_RETSCALAR)
4539 if (PL_opargs[type] & OA_TARGET)
4540 svop->op_targ = pad_alloc(type, SVs_PADTMP);
4541 return CHECKOP(type, svop);
4547 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
4549 Constructs, checks, and returns an op of any type that involves a
4550 reference to a pad element. I<type> is the opcode. I<flags> gives the
4551 eight bits of C<op_flags>. A pad slot is automatically allocated, and
4552 is populated with I<sv>; this function takes ownership of one reference
4555 This function only exists if Perl has been compiled to use ithreads.
4561 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
4566 PERL_ARGS_ASSERT_NEWPADOP;
4568 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4569 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4570 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4572 NewOp(1101, padop, 1, PADOP);
4573 padop->op_type = (OPCODE)type;
4574 padop->op_ppaddr = PL_ppaddr[type];
4575 padop->op_padix = pad_alloc(type, SVs_PADTMP);
4576 SvREFCNT_dec(PAD_SVl(padop->op_padix));
4577 PAD_SETSV(padop->op_padix, sv);
4580 padop->op_next = (OP*)padop;
4581 padop->op_flags = (U8)flags;
4582 if (PL_opargs[type] & OA_RETSCALAR)
4584 if (PL_opargs[type] & OA_TARGET)
4585 padop->op_targ = pad_alloc(type, SVs_PADTMP);
4586 return CHECKOP(type, padop);
4589 #endif /* !USE_ITHREADS */
4592 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
4594 Constructs, checks, and returns an op of any type that involves an
4595 embedded reference to a GV. I<type> is the opcode. I<flags> gives the
4596 eight bits of C<op_flags>. I<gv> identifies the GV that the op should
4597 reference; calling this function does not transfer ownership of any
4604 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
4608 PERL_ARGS_ASSERT_NEWGVOP;
4612 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4614 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4619 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
4621 Constructs, checks, and returns an op of any type that involves an
4622 embedded C-level pointer (PV). I<type> is the opcode. I<flags> gives
4623 the eight bits of C<op_flags>. I<pv> supplies the C-level pointer, which
4624 must have been allocated using L</PerlMemShared_malloc>; the memory will
4625 be freed when the op is destroyed.
4631 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
4634 const bool utf8 = cBOOL(flags & SVf_UTF8);
4639 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4641 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4643 NewOp(1101, pvop, 1, PVOP);
4644 pvop->op_type = (OPCODE)type;
4645 pvop->op_ppaddr = PL_ppaddr[type];
4647 pvop->op_next = (OP*)pvop;
4648 pvop->op_flags = (U8)flags;
4649 pvop->op_private = utf8 ? OPpPV_IS_UTF8 : 0;
4650 if (PL_opargs[type] & OA_RETSCALAR)
4652 if (PL_opargs[type] & OA_TARGET)
4653 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
4654 return CHECKOP(type, pvop);
4662 Perl_package(pTHX_ OP *o)
4665 SV *const sv = cSVOPo->op_sv;
4670 PERL_ARGS_ASSERT_PACKAGE;
4672 SAVEGENERICSV(PL_curstash);
4673 save_item(PL_curstname);
4675 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
4677 sv_setsv(PL_curstname, sv);
4679 PL_hints |= HINT_BLOCK_SCOPE;
4680 PL_parser->copline = NOLINE;
4681 PL_parser->expect = XSTATE;
4686 if (!PL_madskills) {
4691 pegop = newOP(OP_NULL,0);
4692 op_getmad(o,pegop,'P');
4698 Perl_package_version( pTHX_ OP *v )
4701 U32 savehints = PL_hints;
4702 PERL_ARGS_ASSERT_PACKAGE_VERSION;
4703 PL_hints &= ~HINT_STRICT_VARS;
4704 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
4705 PL_hints = savehints;
4714 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
4721 OP *pegop = newOP(OP_NULL,0);
4723 SV *use_version = NULL;
4725 PERL_ARGS_ASSERT_UTILIZE;
4727 if (idop->op_type != OP_CONST)
4728 Perl_croak(aTHX_ "Module name must be constant");
4731 op_getmad(idop,pegop,'U');
4736 SV * const vesv = ((SVOP*)version)->op_sv;
4739 op_getmad(version,pegop,'V');
4740 if (!arg && !SvNIOKp(vesv)) {
4747 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
4748 Perl_croak(aTHX_ "Version number must be a constant number");
4750 /* Make copy of idop so we don't free it twice */
4751 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4753 /* Fake up a method call to VERSION */
4754 meth = newSVpvs_share("VERSION");
4755 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4756 op_append_elem(OP_LIST,
4757 op_prepend_elem(OP_LIST, pack, list(version)),
4758 newSVOP(OP_METHOD_NAMED, 0, meth)));
4762 /* Fake up an import/unimport */
4763 if (arg && arg->op_type == OP_STUB) {
4765 op_getmad(arg,pegop,'S');
4766 imop = arg; /* no import on explicit () */
4768 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
4769 imop = NULL; /* use 5.0; */
4771 use_version = ((SVOP*)idop)->op_sv;
4773 idop->op_private |= OPpCONST_NOVER;
4779 op_getmad(arg,pegop,'A');
4781 /* Make copy of idop so we don't free it twice */
4782 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4784 /* Fake up a method call to import/unimport */
4786 ? newSVpvs_share("import") : newSVpvs_share("unimport");
4787 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4788 op_append_elem(OP_LIST,
4789 op_prepend_elem(OP_LIST, pack, list(arg)),
4790 newSVOP(OP_METHOD_NAMED, 0, meth)));
4793 /* Fake up the BEGIN {}, which does its thing immediately. */
4795 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
4798 op_append_elem(OP_LINESEQ,
4799 op_append_elem(OP_LINESEQ,
4800 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
4801 newSTATEOP(0, NULL, veop)),
4802 newSTATEOP(0, NULL, imop) ));
4806 * feature bundle that corresponds to the required version. */
4807 use_version = sv_2mortal(new_version(use_version));
4808 S_enable_feature_bundle(aTHX_ use_version);
4810 /* If a version >= 5.11.0 is requested, strictures are on by default! */
4811 if (vcmp(use_version,
4812 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
4813 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
4814 PL_hints |= HINT_STRICT_REFS;
4815 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
4816 PL_hints |= HINT_STRICT_SUBS;
4817 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
4818 PL_hints |= HINT_STRICT_VARS;
4820 /* otherwise they are off */
4822 if (!(PL_hints & HINT_EXPLICIT_STRICT_REFS))
4823 PL_hints &= ~HINT_STRICT_REFS;
4824 if (!(PL_hints & HINT_EXPLICIT_STRICT_SUBS))
4825 PL_hints &= ~HINT_STRICT_SUBS;
4826 if (!(PL_hints & HINT_EXPLICIT_STRICT_VARS))
4827 PL_hints &= ~HINT_STRICT_VARS;
4831 /* The "did you use incorrect case?" warning used to be here.
4832 * The problem is that on case-insensitive filesystems one
4833 * might get false positives for "use" (and "require"):
4834 * "use Strict" or "require CARP" will work. This causes
4835 * portability problems for the script: in case-strict
4836 * filesystems the script will stop working.
4838 * The "incorrect case" warning checked whether "use Foo"
4839 * imported "Foo" to your namespace, but that is wrong, too:
4840 * there is no requirement nor promise in the language that
4841 * a Foo.pm should or would contain anything in package "Foo".
4843 * There is very little Configure-wise that can be done, either:
4844 * the case-sensitivity of the build filesystem of Perl does not
4845 * help in guessing the case-sensitivity of the runtime environment.
4848 PL_hints |= HINT_BLOCK_SCOPE;
4849 PL_parser->copline = NOLINE;
4850 PL_parser->expect = XSTATE;
4851 PL_cop_seqmax++; /* Purely for B::*'s benefit */
4852 if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */
4856 if (!PL_madskills) {
4857 /* FIXME - don't allocate pegop if !PL_madskills */
4866 =head1 Embedding Functions
4868 =for apidoc load_module
4870 Loads the module whose name is pointed to by the string part of name.
4871 Note that the actual module name, not its filename, should be given.
4872 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
4873 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
4874 (or 0 for no flags). ver, if specified and not NULL, provides version semantics
4875 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
4876 arguments can be used to specify arguments to the module's import()
4877 method, similar to C<use Foo::Bar VERSION LIST>. They must be
4878 terminated with a final NULL pointer. Note that this list can only
4879 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
4880 Otherwise at least a single NULL pointer to designate the default
4881 import list is required.
4883 The reference count for each specified C<SV*> parameter is decremented.
4888 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
4892 PERL_ARGS_ASSERT_LOAD_MODULE;