#line 2 "op.c" /* op.c * * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others * * You may distribute under the terms of either the GNU General Public * License or the Artistic License, as specified in the README file. * */ /* * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was * our Mr. Bilbo's first cousin on the mother's side (her mother being the * youngest of the Old Took's daughters); and Mr. Drogo was his second * cousin. So Mr. Frodo is his first *and* second cousin, once removed * either way, as the saying is, if you follow me.' --the Gaffer * * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] */ /* This file contains the functions that create, manipulate and optimize * the OP structures that hold a compiled perl program. * * A Perl program is compiled into a tree of OPs. Each op contains * structural pointers (eg to its siblings and the next op in the * execution sequence), a pointer to the function that would execute the * op, plus any data specific to that op. For example, an OP_CONST op * points to the pp_const() function and to an SV containing the constant * value. When pp_const() is executed, its job is to push that SV onto the * stack. * * OPs are mainly created by the newFOO() functions, which are mainly * called from the parser (in perly.y) as the code is parsed. For example * the Perl code $a + $b * $c would cause the equivalent of the following * to be called (oversimplifying a bit): * * newBINOP(OP_ADD, flags, * newSVREF($a), * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c)) * ) * * Note that during the build of miniperl, a temporary copy of this file * is made, called opmini.c. */ /* Perl's compiler is essentially a 3-pass compiler with interleaved phases: A bottom-up pass A top-down pass An execution-order pass The bottom-up pass is represented by all the "newOP" routines and the ck_ routines. The bottom-upness is actually driven by yacc. So at the point that a ck_ routine fires, we have no idea what the context is, either upward in the syntax tree, or either forward or backward in the execution order. (The bottom-up parser builds that part of the execution order it knows about, but if you follow the "next" links around, you'll find it's actually a closed loop through the top level node.) Whenever the bottom-up parser gets to a node that supplies context to its components, it invokes that portion of the top-down pass that applies to that part of the subtree (and marks the top node as processed, so if a node further up supplies context, it doesn't have to take the plunge again). As a particular subcase of this, as the new node is built, it takes all the closed execution loops of its subcomponents and links them into a new closed loop for the higher level node. But it's still not the real execution order. The actual execution order is not known till we get a grammar reduction to a top-level unit like a subroutine or file that will be called by "name" rather than via a "next" pointer. At that point, we can call into peep() to do that code's portion of the 3rd pass. It has to be recursive, but it's recursive on basic blocks, not on tree nodes. */ /* To implement user lexical pragmas, there needs to be a way at run time to get the compile time state of %^H for that block. Storing %^H in every block (or even COP) would be very expensive, so a different approach is taken. The (running) state of %^H is serialised into a tree of HE-like structs. Stores into %^H are chained onto the current leaf as a struct refcounted_he * with the key and the value. Deletes from %^H are saved with a value of PL_sv_placeholder. The state of %^H at any point can be turned back into a regular HV by walking back up the tree from that point's leaf, ignoring any key you've already seen (placeholder or not), storing the rest into the HV structure, then removing the placeholders. Hence memory is only used to store the %^H deltas from the enclosing COP, rather than the entire %^H on each COP. To cause actions on %^H to write out the serialisation records, it has magic type 'H'. This magic (itself) does nothing, but its presence causes the values to gain magic type 'h', which has entries for set and clear. C updates C with a store record, with deletes written by C. C saves the current C on the save stack, so that it will be correctly restored when any inner compiling scope is exited. */ #include "EXTERN.h" #define PERL_IN_OP_C #include "perl.h" #include "keywords.h" #define CALL_PEEP(o) PL_peepp(aTHX_ o) #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o) #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o) #if defined(PL_OP_SLAB_ALLOC) #ifdef PERL_DEBUG_READONLY_OPS # define PERL_SLAB_SIZE 4096 # include #endif #ifndef PERL_SLAB_SIZE #define PERL_SLAB_SIZE 2048 #endif void * Perl_Slab_Alloc(pTHX_ size_t sz) { dVAR; /* * To make incrementing use count easy PL_OpSlab is an I32 * * To make inserting the link to slab PL_OpPtr is I32 ** * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments * Add an overhead for pointer to slab and round up as a number of pointers */ sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *); if ((PL_OpSpace -= sz) < 0) { #ifdef PERL_DEBUG_READONLY_OPS /* We need to allocate chunk by chunk so that we can control the VM mapping */ PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n", (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), PL_OpPtr)); if(PL_OpPtr == MAP_FAILED) { perror("mmap failed"); abort(); } #else PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*)); #endif if (!PL_OpPtr) { return NULL; } /* We reserve the 0'th I32 sized chunk as a use count */ PL_OpSlab = (I32 *) PL_OpPtr; /* Reduce size by the use count word, and by the size we need. * Latter is to mimic the '-=' in the if() above */ PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz; /* Allocation pointer starts at the top. Theory: because we build leaves before trunk allocating at end means that at run time access is cache friendly upward */ PL_OpPtr += PERL_SLAB_SIZE; #ifdef PERL_DEBUG_READONLY_OPS /* We remember this slab. */ /* This implementation isn't efficient, but it is simple. */ PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1)); PL_slabs[PL_slab_count++] = PL_OpSlab; DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab)); #endif } assert( PL_OpSpace >= 0 ); /* Move the allocation pointer down */ PL_OpPtr -= sz; assert( PL_OpPtr > (I32 **) PL_OpSlab ); *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */ (*PL_OpSlab)++; /* Increment use count of slab */ assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) ); assert( *PL_OpSlab > 0 ); return (void *)(PL_OpPtr + 1); } #ifdef PERL_DEBUG_READONLY_OPS void Perl_pending_Slabs_to_ro(pTHX) { /* Turn all the allocated op slabs read only. */ U32 count = PL_slab_count; I32 **const slabs = PL_slabs; /* Reset the array of pending OP slabs, as we're about to turn this lot read only. Also, do it ahead of the loop in case the warn triggers, and a warn handler has an eval */ PL_slabs = NULL; PL_slab_count = 0; /* Force a new slab for any further allocation. */ PL_OpSpace = 0; while (count--) { void *const start = slabs[count]; const size_t size = PERL_SLAB_SIZE* sizeof(I32*); if(mprotect(start, size, PROT_READ)) { Perl_warn(aTHX_ "mprotect for %p %lu failed with %d", start, (unsigned long) size, errno); } } free(slabs); } STATIC void S_Slab_to_rw(pTHX_ void *op) { I32 * const * const ptr = (I32 **) op; I32 * const slab = ptr[-1]; PERL_ARGS_ASSERT_SLAB_TO_RW; assert( ptr-1 > (I32 **) slab ); assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) ); assert( *slab > 0 ); if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) { Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d", slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno); } } OP * Perl_op_refcnt_inc(pTHX_ OP *o) { if(o) { Slab_to_rw(o); ++o->op_targ; } return o; } PADOFFSET Perl_op_refcnt_dec(pTHX_ OP *o) { PERL_ARGS_ASSERT_OP_REFCNT_DEC; Slab_to_rw(o); return --o->op_targ; } #else # define Slab_to_rw(op) #endif void Perl_Slab_Free(pTHX_ void *op) { I32 * const * const ptr = (I32 **) op; I32 * const slab = ptr[-1]; PERL_ARGS_ASSERT_SLAB_FREE; assert( ptr-1 > (I32 **) slab ); assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) ); assert( *slab > 0 ); Slab_to_rw(op); if (--(*slab) == 0) { # ifdef NETWARE # define PerlMemShared PerlMem # endif #ifdef PERL_DEBUG_READONLY_OPS U32 count = PL_slab_count; /* Need to remove this slab from our list of slabs */ if (count) { while (count--) { if (PL_slabs[count] == slab) { dVAR; /* Found it. Move the entry at the end to overwrite it. */ DEBUG_m(PerlIO_printf(Perl_debug_log, "Deallocate %p by moving %p from %lu to %lu\n", PL_OpSlab, PL_slabs[PL_slab_count - 1], PL_slab_count, count)); PL_slabs[count] = PL_slabs[--PL_slab_count]; /* Could realloc smaller at this point, but probably not worth it. */ if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) { perror("munmap failed"); abort(); } break; } } } #else PerlMemShared_free(slab); #endif if (slab == PL_OpSlab) { PL_OpSpace = 0; } } } #endif /* * In the following definition, the ", (OP*)0" is just to make the compiler * think the expression is of the right type: croak actually does a Siglongjmp. */ #define CHECKOP(type,o) \ ((PL_op_mask && PL_op_mask[type]) \ ? ( op_free((OP*)o), \ Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \ (OP*)0 ) \ : PL_check[type](aTHX_ (OP*)o)) #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2) #define CHANGE_TYPE(o,type) \ STMT_START { \ o->op_type = (OPCODE)type; \ o->op_ppaddr = PL_ppaddr[type]; \ } STMT_END STATIC const char* S_gv_ename(pTHX_ GV *gv) { SV* const tmpsv = sv_newmortal(); PERL_ARGS_ASSERT_GV_ENAME; gv_efullname3(tmpsv, gv, NULL); return SvPV_nolen_const(tmpsv); } STATIC OP * S_no_fh_allowed(pTHX_ OP *o) { PERL_ARGS_ASSERT_NO_FH_ALLOWED; yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function", OP_DESC(o))); return o; } STATIC OP * S_too_few_arguments(pTHX_ OP *o, const char *name) { PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS; yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name)); return o; } STATIC OP * S_too_many_arguments(pTHX_ OP *o, const char *name) { PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS; yyerror(Perl_form(aTHX_ "Too many arguments for %s", name)); return o; } STATIC void S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid) { PERL_ARGS_ASSERT_BAD_TYPE; yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)", (int)n, name, t, OP_DESC(kid))); } STATIC void S_no_bareword_allowed(pTHX_ const OP *o) { PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED; if (PL_madskills) return; /* various ok barewords are hidden in extra OP_NULL */ qerror(Perl_mess(aTHX_ "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use", SVfARG(cSVOPo_sv))); } /* "register" allocation */ PADOFFSET Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags) { dVAR; PADOFFSET off; const bool is_our = (PL_parser->in_my == KEY_our); PERL_ARGS_ASSERT_ALLOCMY; if (flags) Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf, (UV)flags); /* Until we're using the length for real, cross check that we're being told the truth. */ assert(strlen(name) == len); /* complain about "my $" etc etc */ if (len && !(is_our || isALPHA(name[1]) || (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) || (name[1] == '_' && (*name == '$' || len > 2)))) { /* name[2] is true if strlen(name) > 2 */ if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) { yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"", name[0], toCTRL(name[1]), (int)(len - 2), name + 2, PL_parser->in_my == KEY_state ? "state" : "my")); } else { yyerror(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name, PL_parser->in_my == KEY_state ? "state" : "my")); } } /* allocate a spare slot and store the name in that slot */ off = pad_add_name(name, len, is_our ? padadd_OUR : PL_parser->in_my == KEY_state ? padadd_STATE : 0, PL_parser->in_my_stash, (is_our /* $_ is always in main::, even with our */ ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash) : NULL ) ); /* anon sub prototypes contains state vars should always be cloned, * otherwise the state var would be shared between anon subs */ if (PL_parser->in_my == KEY_state && CvANON(PL_compcv)) CvCLONE_on(PL_compcv); return off; } /* free the body of an op without examining its contents. * Always use this rather than FreeOp directly */ static void S_op_destroy(pTHX_ OP *o) { if (o->op_latefree) { o->op_latefreed = 1; return; } FreeOp(o); } #ifdef USE_ITHREADS # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b) #else # define forget_pmop(a,b) S_forget_pmop(aTHX_ a) #endif /* Destructor */ void Perl_op_free(pTHX_ OP *o) { dVAR; OPCODE type; if (!o) return; if (o->op_latefreed) { if (o->op_latefree) return; goto do_free; } type = o->op_type; if (o->op_private & OPpREFCOUNTED) { switch (type) { case OP_LEAVESUB: case OP_LEAVESUBLV: case OP_LEAVEEVAL: case OP_LEAVE: case OP_SCOPE: case OP_LEAVEWRITE: { PADOFFSET refcnt; OP_REFCNT_LOCK; refcnt = OpREFCNT_dec(o); OP_REFCNT_UNLOCK; if (refcnt) { /* Need to find and remove any pattern match ops from the list we maintain for reset(). */ find_and_forget_pmops(o); return; } } break; default: break; } } /* Call the op_free hook if it has been set. Do it now so that it's called * at the right time for refcounted ops, but still before all of the kids * are freed. */ CALL_OPFREEHOOK(o); if (o->op_flags & OPf_KIDS) { register OP *kid, *nextkid; for (kid = cUNOPo->op_first; kid; kid = nextkid) { nextkid = kid->op_sibling; /* Get before next freeing kid */ op_free(kid); } } #ifdef PERL_DEBUG_READONLY_OPS Slab_to_rw(o); #endif /* COP* is not cleared by op_clear() so that we may track line * numbers etc even after null() */ if (type == OP_NEXTSTATE || type == OP_DBSTATE || (type == OP_NULL /* the COP might have been null'ed */ && ((OPCODE)o->op_targ == OP_NEXTSTATE || (OPCODE)o->op_targ == OP_DBSTATE))) { cop_free((COP*)o); } if (type == OP_NULL) type = (OPCODE)o->op_targ; op_clear(o); if (o->op_latefree) { o->op_latefreed = 1; return; } do_free: FreeOp(o); #ifdef DEBUG_LEAKING_SCALARS if (PL_op == o) PL_op = NULL; #endif } void Perl_op_clear(pTHX_ OP *o) { dVAR; PERL_ARGS_ASSERT_OP_CLEAR; #ifdef PERL_MAD /* if (o->op_madprop && o->op_madprop->mad_next) abort(); */ /* FIXME for MAD - if I uncomment these two lines t/op/pack.t fails with "modification of a read only value" for a reason I can't fathom why. It's the "" stringification of $_, where $_ was set to '' in a foreach loop, but it defies simplification into a small test case. However, commenting them out has caused ext/List/Util/t/weak.t to fail the last test. */ /* mad_free(o->op_madprop); o->op_madprop = 0; */ #endif retry: switch (o->op_type) { case OP_NULL: /* Was holding old type, if any. */ if (PL_madskills && o->op_targ != OP_NULL) { o->op_type = (Optype)o->op_targ; o->op_targ = 0; goto retry; } case OP_ENTERTRY: case OP_ENTEREVAL: /* Was holding hints. */ o->op_targ = 0; break; default: if (!(o->op_flags & OPf_REF) || (PL_check[o->op_type] != Perl_ck_ftst)) break; /* FALL THROUGH */ case OP_GVSV: case OP_GV: case OP_AELEMFAST: if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) { /* not an OP_PADAV replacement */ GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV) #ifdef USE_ITHREADS && PL_curpad #endif ? cGVOPo_gv : NULL; /* It's possible during global destruction that the GV is freed before the optree. Whilst the SvREFCNT_inc is happy to bump from 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0 will trigger an assertion failure, because the entry to sv_clear checks that the scalar is not already freed. A check of for !SvIS_FREED(gv) turns out to be invalid, because during global destruction the reference count can be forced down to zero (with SVf_BREAK set). In which case raising to 1 and then dropping to 0 triggers cleanup before it should happen. I *think* that this might actually be a general, systematic, weakness of the whole idea of SVf_BREAK, in that code *is* allowed to raise and lower references during global destruction, so any *valid* code that happens to do this during global destruction might well trigger premature cleanup. */ bool still_valid = gv && SvREFCNT(gv); if (still_valid) SvREFCNT_inc_simple_void(gv); #ifdef USE_ITHREADS if (cPADOPo->op_padix > 0) { /* No GvIN_PAD_off(cGVOPo_gv) here, because other references * may still exist on the pad */ pad_swipe(cPADOPo->op_padix, TRUE); cPADOPo->op_padix = 0; } #else SvREFCNT_dec(cSVOPo->op_sv); cSVOPo->op_sv = NULL; #endif if (still_valid) { int try_downgrade = SvREFCNT(gv) == 2; SvREFCNT_dec(gv); if (try_downgrade) gv_try_downgrade(gv); } } break; case OP_METHOD_NAMED: case OP_CONST: case OP_HINTSEVAL: SvREFCNT_dec(cSVOPo->op_sv); cSVOPo->op_sv = NULL; #ifdef USE_ITHREADS /** Bug #15654 Even if op_clear does a pad_free for the target of the op, pad_free doesn't actually remove the sv that exists in the pad; instead it lives on. This results in that it could be reused as a target later on when the pad was reallocated. **/ if(o->op_targ) { pad_swipe(o->op_targ,1); o->op_targ = 0; } #endif break; case OP_GOTO: case OP_NEXT: case OP_LAST: case OP_REDO: if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) break; /* FALL THROUGH */ case OP_TRANS: case OP_TRANSR: if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { #ifdef USE_ITHREADS if (cPADOPo->op_padix > 0) { pad_swipe(cPADOPo->op_padix, TRUE); cPADOPo->op_padix = 0; } #else SvREFCNT_dec(cSVOPo->op_sv); cSVOPo->op_sv = NULL; #endif } else { PerlMemShared_free(cPVOPo->op_pv); cPVOPo->op_pv = NULL; } break; case OP_SUBST: op_free(cPMOPo->op_pmreplrootu.op_pmreplroot); goto clear_pmop; case OP_PUSHRE: #ifdef USE_ITHREADS if (cPMOPo->op_pmreplrootu.op_pmtargetoff) { /* No GvIN_PAD_off here, because other references may still * exist on the pad */ pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE); } #else SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv)); #endif /* FALL THROUGH */ case OP_MATCH: case OP_QR: clear_pmop: forget_pmop(cPMOPo, 1); cPMOPo->op_pmreplrootu.op_pmreplroot = NULL; /* we use the same protection as the "SAFE" version of the PM_ macros * here since sv_clean_all might release some PMOPs * after PL_regex_padav has been cleared * and the clearing of PL_regex_padav needs to * happen before sv_clean_all */ #ifdef USE_ITHREADS if(PL_regex_pad) { /* We could be in destruction */ const IV offset = (cPMOPo)->op_pmoffset; ReREFCNT_dec(PM_GETRE(cPMOPo)); PL_regex_pad[offset] = &PL_sv_undef; sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset, sizeof(offset)); } #else ReREFCNT_dec(PM_GETRE(cPMOPo)); PM_SETRE(cPMOPo, NULL); #endif break; } if (o->op_targ > 0) { pad_free(o->op_targ); o->op_targ = 0; } } STATIC void S_cop_free(pTHX_ COP* cop) { PERL_ARGS_ASSERT_COP_FREE; CopFILE_free(cop); CopSTASH_free(cop); if (! specialWARN(cop->cop_warnings)) PerlMemShared_free(cop->cop_warnings); cophh_free(CopHINTHASH_get(cop)); } STATIC void S_forget_pmop(pTHX_ PMOP *const o #ifdef USE_ITHREADS , U32 flags #endif ) { HV * const pmstash = PmopSTASH(o); PERL_ARGS_ASSERT_FORGET_PMOP; if (pmstash && !SvIS_FREED(pmstash)) { MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab); if (mg) { PMOP **const array = (PMOP**) mg->mg_ptr; U32 count = mg->mg_len / sizeof(PMOP**); U32 i = count; while (i--) { if (array[i] == o) { /* Found it. Move the entry at the end to overwrite it. */ array[i] = array[--count]; mg->mg_len = count * sizeof(PMOP**); /* Could realloc smaller at this point always, but probably not worth it. Probably worth free()ing if we're the last. */ if(!count) { Safefree(mg->mg_ptr); mg->mg_ptr = NULL; } break; } } } } if (PL_curpm == o) PL_curpm = NULL; #ifdef USE_ITHREADS if (flags) PmopSTASH_free(o); #endif } STATIC void S_find_and_forget_pmops(pTHX_ OP *o) { PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS; if (o->op_flags & OPf_KIDS) { OP *kid = cUNOPo->op_first; while (kid) { switch (kid->op_type) { case OP_SUBST: case OP_PUSHRE: case OP_MATCH: case OP_QR: forget_pmop((PMOP*)kid, 0); } find_and_forget_pmops(kid); kid = kid->op_sibling; } } } void Perl_op_null(pTHX_ OP *o) { dVAR; PERL_ARGS_ASSERT_OP_NULL; if (o->op_type == OP_NULL) return; if (!PL_madskills) op_clear(o); o->op_targ = o->op_type; o->op_type = OP_NULL; o->op_ppaddr = PL_ppaddr[OP_NULL]; } void Perl_op_refcnt_lock(pTHX) { dVAR; PERL_UNUSED_CONTEXT; OP_REFCNT_LOCK; } void Perl_op_refcnt_unlock(pTHX) { dVAR; PERL_UNUSED_CONTEXT; OP_REFCNT_UNLOCK; } /* Contextualizers */ /* =for apidoc Am|OP *|op_contextualize|OP *o|I32 context Applies a syntactic context to an op tree representing an expression. I is the op tree, and I must be C, C, or C to specify the context to apply. The modified op tree is returned. =cut */ OP * Perl_op_contextualize(pTHX_ OP *o, I32 context) { PERL_ARGS_ASSERT_OP_CONTEXTUALIZE; switch (context) { case G_SCALAR: return scalar(o); case G_ARRAY: return list(o); case G_VOID: return scalarvoid(o); default: Perl_croak(aTHX_ "panic: op_contextualize bad context"); return o; } } /* =head1 Optree Manipulation Functions =for apidoc Am|OP*|op_linklist|OP *o This function is the implementation of the L macro. It should not be called directly. =cut */ OP * Perl_op_linklist(pTHX_ OP *o) { OP *first; PERL_ARGS_ASSERT_OP_LINKLIST; if (o->op_next) return o->op_next; /* establish postfix order */ first = cUNOPo->op_first; if (first) { register OP *kid; o->op_next = LINKLIST(first); kid = first; for (;;) { if (kid->op_sibling) { kid->op_next = LINKLIST(kid->op_sibling); kid = kid->op_sibling; } else { kid->op_next = o; break; } } } else o->op_next = o; return o->op_next; } static OP * S_scalarkids(pTHX_ OP *o) { if (o && o->op_flags & OPf_KIDS) { OP *kid; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) scalar(kid); } return o; } STATIC OP * S_scalarboolean(pTHX_ OP *o) { dVAR; PERL_ARGS_ASSERT_SCALARBOOLEAN; if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) { if (ckWARN(WARN_SYNTAX)) { const line_t oldline = CopLINE(PL_curcop); if (PL_parser && PL_parser->copline != NOLINE) CopLINE_set(PL_curcop, PL_parser->copline); Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be =="); CopLINE_set(PL_curcop, oldline); } } return scalar(o); } OP * Perl_scalar(pTHX_ OP *o) { dVAR; OP *kid; /* assumes no premature commitment */ if (!o || (PL_parser && PL_parser->error_count) || (o->op_flags & OPf_WANT) || o->op_type == OP_RETURN) { return o; } o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; switch (o->op_type) { case OP_REPEAT: scalar(cBINOPo->op_first); break; case OP_OR: case OP_AND: case OP_COND_EXPR: for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) scalar(kid); break; /* FALL THROUGH */ case OP_SPLIT: case OP_MATCH: case OP_QR: case OP_SUBST: case OP_NULL: default: if (o->op_flags & OPf_KIDS) { for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) scalar(kid); } break; case OP_LEAVE: case OP_LEAVETRY: kid = cLISTOPo->op_first; scalar(kid); kid = kid->op_sibling; do_kids: while (kid) { OP *sib = kid->op_sibling; if (sib && kid->op_type != OP_LEAVEWHEN) { if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) { scalar(kid); scalarvoid(sib); break; } else scalarvoid(kid); } else scalar(kid); kid = sib; } PL_curcop = &PL_compiling; break; case OP_SCOPE: case OP_LINESEQ: case OP_LIST: kid = cLISTOPo->op_first; goto do_kids; case OP_SORT: Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context"); break; } return o; } OP * Perl_scalarvoid(pTHX_ OP *o) { dVAR; OP *kid; const char* useless = NULL; SV* sv; U8 want; PERL_ARGS_ASSERT_SCALARVOID; /* trailing mad null ops don't count as "there" for void processing */ if (PL_madskills && o->op_type != OP_NULL && o->op_sibling && o->op_sibling->op_type == OP_NULL) { OP *sib; for (sib = o->op_sibling; sib && sib->op_type == OP_NULL; sib = sib->op_sibling) ; if (!sib) return o; } if (o->op_type == OP_NEXTSTATE || o->op_type == OP_DBSTATE || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE))) PL_curcop = (COP*)o; /* for warning below */ /* assumes no premature commitment */ want = o->op_flags & OPf_WANT; if ((want && want != OPf_WANT_SCALAR) || (PL_parser && PL_parser->error_count) || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN) { return o; } if ((o->op_private & OPpTARGET_MY) && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ { return scalar(o); /* As if inside SASSIGN */ } o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; switch (o->op_type) { default: if (!(PL_opargs[o->op_type] & OA_FOLDCONST)) break; /* FALL THROUGH */ case OP_REPEAT: if (o->op_flags & OPf_STACKED) break; goto func_ops; case OP_SUBSTR: if (o->op_private == 4) break; /* FALL THROUGH */ case OP_GVSV: case OP_WANTARRAY: case OP_GV: case OP_SMARTMATCH: case OP_PADSV: case OP_PADAV: case OP_PADHV: case OP_PADANY: case OP_AV2ARYLEN: case OP_REF: case OP_REFGEN: case OP_SREFGEN: case OP_DEFINED: case OP_HEX: case OP_OCT: case OP_LENGTH: case OP_VEC: case OP_INDEX: case OP_RINDEX: case OP_SPRINTF: case OP_AELEM: case OP_AELEMFAST: case OP_ASLICE: case OP_HELEM: case OP_HSLICE: case OP_UNPACK: case OP_PACK: case OP_JOIN: case OP_LSLICE: case OP_ANONLIST: case OP_ANONHASH: case OP_SORT: case OP_REVERSE: case OP_RANGE: case OP_FLIP: case OP_FLOP: case OP_CALLER: case OP_FILENO: case OP_EOF: case OP_TELL: case OP_GETSOCKNAME: case OP_GETPEERNAME: case OP_READLINK: case OP_TELLDIR: case OP_GETPPID: case OP_GETPGRP: case OP_GETPRIORITY: case OP_TIME: case OP_TMS: case OP_LOCALTIME: case OP_GMTIME: case OP_GHBYNAME: case OP_GHBYADDR: case OP_GHOSTENT: case OP_GNBYNAME: case OP_GNBYADDR: case OP_GNETENT: case OP_GPBYNAME: case OP_GPBYNUMBER: case OP_GPROTOENT: case OP_GSBYNAME: case OP_GSBYPORT: case OP_GSERVENT: case OP_GPWNAM: case OP_GPWUID: case OP_GGRNAM: case OP_GGRGID: case OP_GETLOGIN: case OP_PROTOTYPE: func_ops: if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO))) /* Otherwise it's "Useless use of grep iterator" */ useless = OP_DESC(o); break; case OP_SPLIT: kid = cLISTOPo->op_first; if (kid && kid->op_type == OP_PUSHRE #ifdef USE_ITHREADS && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff) #else && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv) #endif useless = OP_DESC(o); break; case OP_NOT: kid = cUNOPo->op_first; if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST && kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) { goto func_ops; } useless = "negative pattern binding (!~)"; break; case OP_SUBST: if (cPMOPo->op_pmflags & PMf_NONDESTRUCT) useless = "non-destructive substitution (s///r)"; break; case OP_TRANSR: useless = "non-destructive transliteration (tr///r)"; break; case OP_RV2GV: case OP_RV2SV: case OP_RV2AV: case OP_RV2HV: if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) && (!o->op_sibling || o->op_sibling->op_type != OP_READLINE)) useless = "a variable"; break; case OP_CONST: sv = cSVOPo_sv; if (cSVOPo->op_private & OPpCONST_STRICT) no_bareword_allowed(o); else { if (ckWARN(WARN_VOID)) { if (SvOK(sv)) { SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_ "a constant (%"SVf")", sv)); useless = SvPV_nolen(msv); } else useless = "a constant (undef)"; if (o->op_private & OPpCONST_ARYBASE) useless = NULL; /* don't warn on optimised away booleans, eg * use constant Foo, 5; Foo || print; */ if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT) useless = NULL; /* the constants 0 and 1 are permitted as they are conventionally used as dummies in constructs like 1 while some_condition_with_side_effects; */ else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) useless = NULL; else if (SvPOK(sv)) { /* perl4's way of mixing documentation and code (before the invention of POD) was based on a trick to mix nroff and perl code. The trick was built upon these three nroff macros being used in void context. The pink camel has the details in the script wrapman near page 319. */ const char * const maybe_macro = SvPVX_const(sv); if (strnEQ(maybe_macro, "di", 2) || strnEQ(maybe_macro, "ds", 2) || strnEQ(maybe_macro, "ig", 2)) useless = NULL; } } } op_null(o); /* don't execute or even remember it */ break; case OP_POSTINC: o->op_type = OP_PREINC; /* pre-increment is faster */ o->op_ppaddr = PL_ppaddr[OP_PREINC]; break; case OP_POSTDEC: o->op_type = OP_PREDEC; /* pre-decrement is faster */ o->op_ppaddr = PL_ppaddr[OP_PREDEC]; break; case OP_I_POSTINC: o->op_type = OP_I_PREINC; /* pre-increment is faster */ o->op_ppaddr = PL_ppaddr[OP_I_PREINC]; break; case OP_I_POSTDEC: o->op_type = OP_I_PREDEC; /* pre-decrement is faster */ o->op_ppaddr = PL_ppaddr[OP_I_PREDEC]; break; case OP_OR: case OP_AND: kid = cLOGOPo->op_first; if (kid->op_type == OP_NOT && (kid->op_flags & OPf_KIDS) && !PL_madskills) { if (o->op_type == OP_AND) { o->op_type = OP_OR; o->op_ppaddr = PL_ppaddr[OP_OR]; } else { o->op_type = OP_AND; o->op_ppaddr = PL_ppaddr[OP_AND]; } op_null(kid); } case OP_DOR: case OP_COND_EXPR: case OP_ENTERGIVEN: case OP_ENTERWHEN: for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) scalarvoid(kid); break; case OP_NULL: if (o->op_flags & OPf_STACKED) break; /* FALL THROUGH */ case OP_NEXTSTATE: case OP_DBSTATE: case OP_ENTERTRY: case OP_ENTER: if (!(o->op_flags & OPf_KIDS)) break; /* FALL THROUGH */ case OP_SCOPE: case OP_LEAVE: case OP_LEAVETRY: case OP_LEAVELOOP: case OP_LINESEQ: case OP_LIST: case OP_LEAVEGIVEN: case OP_LEAVEWHEN: for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) scalarvoid(kid); break; case OP_ENTEREVAL: scalarkids(o); break; case OP_SCALAR: return scalar(o); } if (useless) Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless); return o; } static OP * S_listkids(pTHX_ OP *o) { if (o && o->op_flags & OPf_KIDS) { OP *kid; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) list(kid); } return o; } OP * Perl_list(pTHX_ OP *o) { dVAR; OP *kid; /* assumes no premature commitment */ if (!o || (o->op_flags & OPf_WANT) || (PL_parser && PL_parser->error_count) || o->op_type == OP_RETURN) { return o; } if ((o->op_private & OPpTARGET_MY) && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ { return o; /* As if inside SASSIGN */ } o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; switch (o->op_type) { case OP_FLOP: case OP_REPEAT: list(cBINOPo->op_first); break; case OP_OR: case OP_AND: case OP_COND_EXPR: for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) list(kid); break; default: case OP_MATCH: case OP_QR: case OP_SUBST: case OP_NULL: if (!(o->op_flags & OPf_KIDS)) break; if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { list(cBINOPo->op_first); return gen_constant_list(o); } case OP_LIST: listkids(o); break; case OP_LEAVE: case OP_LEAVETRY: kid = cLISTOPo->op_first; list(kid); kid = kid->op_sibling; do_kids: while (kid) { OP *sib = kid->op_sibling; if (sib && kid->op_type != OP_LEAVEWHEN) { if (sib->op_type == OP_BREAK && sib->op_flags & OPf_SPECIAL) { list(kid); scalarvoid(sib); break; } else scalarvoid(kid); } else list(kid); kid = sib; } PL_curcop = &PL_compiling; break; case OP_SCOPE: case OP_LINESEQ: kid = cLISTOPo->op_first; goto do_kids; } return o; } static OP * S_scalarseq(pTHX_ OP *o) { dVAR; if (o) { const OPCODE type = o->op_type; if (type == OP_LINESEQ || type == OP_SCOPE || type == OP_LEAVE || type == OP_LEAVETRY) { OP *kid; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { if (kid->op_sibling) { scalarvoid(kid); } } PL_curcop = &PL_compiling; } o->op_flags &= ~OPf_PARENS; if (PL_hints & HINT_BLOCK_SCOPE) o->op_flags |= OPf_PARENS; } else o = newOP(OP_STUB, 0); return o; } STATIC OP * S_modkids(pTHX_ OP *o, I32 type) { if (o && o->op_flags & OPf_KIDS) { OP *kid; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) op_lvalue(kid, type); } return o; } /* =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type Propagate lvalue ("modifiable") context to an op and its children. I represents the context type, roughly based on the type of op that would do the modifying, although C is represented by OP_NULL, because it has no op type of its own (it is signalled by a flag on the lvalue op). This function detects things that can't be modified, such as C<$x+1>, and generates errors for them. It also flags things that need to behave specially in an lvalue context, such as C<$$x> which might have to vivify a reference in C<$x>. =cut */ OP * Perl_op_lvalue(pTHX_ OP *o, I32 type) { dVAR; OP *kid; /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */ int localize = -1; if (!o || (PL_parser && PL_parser->error_count)) return o; if ((o->op_private & OPpTARGET_MY) && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */ { return o; } switch (o->op_type) { case OP_UNDEF: localize = 0; PL_modcount++; return o; case OP_CONST: if (!(o->op_private & OPpCONST_ARYBASE)) goto nomod; localize = 0; if (PL_eval_start && PL_eval_start->op_type == OP_CONST) { CopARYBASE_set(&PL_compiling, (I32)SvIV(cSVOPx(PL_eval_start)->op_sv)); PL_eval_start = 0; } else if (!type) { SAVECOPARYBASE(&PL_compiling); CopARYBASE_set(&PL_compiling, 0); } else if (type == OP_REFGEN) goto nomod; else Perl_croak(aTHX_ "That use of $[ is unsupported"); break; case OP_STUB: if ((o->op_flags & OPf_PARENS) || PL_madskills) break; goto nomod; case OP_ENTERSUB: if ((type == OP_UNDEF || type == OP_REFGEN) && !(o->op_flags & OPf_STACKED)) { o->op_type = OP_RV2CV; /* entersub => rv2cv */ /* The default is to set op_private to the number of children, which for a UNOP such as RV2CV is always 1. And w're using the bit for a flag in RV2CV, so we need it clear. */ o->op_private &= ~1; o->op_ppaddr = PL_ppaddr[OP_RV2CV]; assert(cUNOPo->op_first->op_type == OP_NULL); op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ break; } else if (o->op_private & OPpENTERSUB_NOMOD) return o; else { /* lvalue subroutine call */ o->op_private |= OPpLVAL_INTRO; PL_modcount = RETURN_UNLIMITED_NUMBER; if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) { /* Backward compatibility mode: */ o->op_private |= OPpENTERSUB_INARGS; break; } else { /* Compile-time error message: */ OP *kid = cUNOPo->op_first; CV *cv; OP *okid; if (kid->op_type != OP_PUSHMARK) { if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST) Perl_croak(aTHX_ "panic: unexpected lvalue entersub " "args: type/targ %ld:%"UVuf, (long)kid->op_type, (UV)kid->op_targ); kid = kLISTOP->op_first; } while (kid->op_sibling) kid = kid->op_sibling; if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) { /* Indirect call */ if (kid->op_type == OP_METHOD_NAMED || kid->op_type == OP_METHOD) { UNOP *newop; NewOp(1101, newop, 1, UNOP); newop->op_type = OP_RV2CV; newop->op_ppaddr = PL_ppaddr[OP_RV2CV]; newop->op_first = NULL; newop->op_next = (OP*)newop; kid->op_sibling = (OP*)newop; newop->op_private |= OPpLVAL_INTRO; newop->op_private &= ~1; break; } if (kid->op_type != OP_RV2CV) Perl_croak(aTHX_ "panic: unexpected lvalue entersub " "entry via type/targ %ld:%"UVuf, (long)kid->op_type, (UV)kid->op_targ); kid->op_private |= OPpLVAL_INTRO; break; /* Postpone until runtime */ } okid = kid; kid = kUNOP->op_first; if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV) kid = kUNOP->op_first; if (kid->op_type == OP_NULL) Perl_croak(aTHX_ "Unexpected constant lvalue entersub " "entry via type/targ %ld:%"UVuf, (long)kid->op_type, (UV)kid->op_targ); if (kid->op_type != OP_GV) { /* Restore RV2CV to check lvalueness */ restore_2cv: if (kid->op_next && kid->op_next != kid) { /* Happens? */ okid->op_next = kid->op_next; kid->op_next = okid; } else okid->op_next = NULL; okid->op_type = OP_RV2CV; okid->op_targ = 0; okid->op_ppaddr = PL_ppaddr[OP_RV2CV]; okid->op_private |= OPpLVAL_INTRO; okid->op_private &= ~1; break; } cv = GvCV(kGVOP_gv); if (!cv) goto restore_2cv; if (CvLVALUE(cv)) break; } } /* FALL THROUGH */ default: nomod: /* grep, foreach, subcalls, refgen */ if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) break; yyerror(Perl_form(aTHX_ "Can't modify %s in %s", (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL) ? "do block" : (o->op_type == OP_ENTERSUB ? "non-lvalue subroutine call" : OP_DESC(o))), type ? PL_op_desc[type] : "local")); return o; case OP_PREINC: case OP_PREDEC: case OP_POW: case OP_MULTIPLY: case OP_DIVIDE: case OP_MODULO: case OP_REPEAT: case OP_ADD: case OP_SUBTRACT: case OP_CONCAT: case OP_LEFT_SHIFT: case OP_RIGHT_SHIFT: case OP_BIT_AND: case OP_BIT_XOR: case OP_BIT_OR: case OP_I_MULTIPLY: case OP_I_DIVIDE: case OP_I_MODULO: case OP_I_ADD: case OP_I_SUBTRACT: if (!(o->op_flags & OPf_STACKED)) goto nomod; PL_modcount++; break; case OP_COND_EXPR: localize = 1; for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) op_lvalue(kid, type); break; case OP_RV2AV: case OP_RV2HV: if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { PL_modcount = RETURN_UNLIMITED_NUMBER; return o; /* Treat \(@foo) like ordinary list. */ } /* FALL THROUGH */ case OP_RV2GV: if (scalar_mod_type(o, type)) goto nomod; ref(cUNOPo->op_first, o->op_type); /* FALL THROUGH */ case OP_ASLICE: case OP_HSLICE: if (type == OP_LEAVESUBLV) o->op_private |= OPpMAYBE_LVSUB; localize = 1; /* FALL THROUGH */ case OP_AASSIGN: case OP_NEXTSTATE: case OP_DBSTATE: PL_modcount = RETURN_UNLIMITED_NUMBER; break; case OP_AV2ARYLEN: PL_hints |= HINT_BLOCK_SCOPE; if (type == OP_LEAVESUBLV) o->op_private |= OPpMAYBE_LVSUB; PL_modcount++; break; case OP_RV2SV: ref(cUNOPo->op_first, o->op_type); localize = 1; /* FALL THROUGH */ case OP_GV: PL_hints |= HINT_BLOCK_SCOPE; case OP_SASSIGN: case OP_ANDASSIGN: case OP_ORASSIGN: case OP_DORASSIGN: PL_modcount++; break; case OP_AELEMFAST: localize = -1; PL_modcount++; break; case OP_PADAV: case OP_PADHV: PL_modcount = RETURN_UNLIMITED_NUMBER; if (type == OP_REFGEN && o->op_flags & OPf_PARENS) return o; /* Treat \(@foo) like ordinary list. */ if (scalar_mod_type(o, type)) goto nomod; if (type == OP_LEAVESUBLV) o->op_private |= OPpMAYBE_LVSUB; /* FALL THROUGH */ case OP_PADSV: PL_modcount++; if (!type) /* local() */ Perl_croak(aTHX_ "Can't localize lexical variable %s", PAD_COMPNAME_PV(o->op_targ)); break; case OP_PUSHMARK: localize = 0; break; case OP_KEYS: if (type != OP_SASSIGN) goto nomod; goto lvalue_func; case OP_SUBSTR: if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */ goto nomod; /* FALL THROUGH */ case OP_POS: case OP_VEC: if (type == OP_LEAVESUBLV) o->op_private |= OPpMAYBE_LVSUB; lvalue_func: pad_free(o->op_targ); o->op_targ = pad_alloc(o->op_type, SVs_PADMY); assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL); if (o->op_flags & OPf_KIDS) op_lvalue(cBINOPo->op_first->op_sibling, type); break; case OP_AELEM: case OP_HELEM: ref(cBINOPo->op_first, o->op_type); if (type == OP_ENTERSUB && !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) o->op_private |= OPpLVAL_DEFER; if (type == OP_LEAVESUBLV) o->op_private |= OPpMAYBE_LVSUB; localize = 1; PL_modcount++; break; case OP_SCOPE: case OP_LEAVE: case OP_ENTER: case OP_LINESEQ: localize = 0; if (o->op_flags & OPf_KIDS) op_lvalue(cLISTOPo->op_last, type); break; case OP_NULL: localize = 0; if (o->op_flags & OPf_SPECIAL) /* do BLOCK */ goto nomod; else if (!(o->op_flags & OPf_KIDS)) break; if (o->op_targ != OP_LIST) { op_lvalue(cBINOPo->op_first, type); break; } /* FALL THROUGH */ case OP_LIST: localize = 0; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) op_lvalue(kid, type); break; case OP_RETURN: if (type != OP_LEAVESUBLV) goto nomod; break; /* op_lvalue()ing was handled by ck_return() */ } /* [20011101.069] File test operators interpret OPf_REF to mean that their argument is a filehandle; thus \stat(".") should not set it. AMS 20011102 */ if (type == OP_REFGEN && PL_check[o->op_type] == Perl_ck_ftst) return o; if (type != OP_LEAVESUBLV) o->op_flags |= OPf_MOD; if (type == OP_AASSIGN || type == OP_SASSIGN) o->op_flags |= OPf_SPECIAL|OPf_REF; else if (!type) { /* local() */ switch (localize) { case 1: o->op_private |= OPpLVAL_INTRO; o->op_flags &= ~OPf_SPECIAL; PL_hints |= HINT_BLOCK_SCOPE; break; case 0: break; case -1: Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), "Useless localization of %s", OP_DESC(o)); } } else if (type != OP_GREPSTART && type != OP_ENTERSUB && type != OP_LEAVESUBLV) o->op_flags |= OPf_REF; return o; } STATIC bool S_scalar_mod_type(const OP *o, I32 type) { PERL_ARGS_ASSERT_SCALAR_MOD_TYPE; switch (type) { case OP_SASSIGN: if (o->op_type == OP_RV2GV) return FALSE; /* FALL THROUGH */ case OP_PREINC: case OP_PREDEC: case OP_POSTINC: case OP_POSTDEC: case OP_I_PREINC: case OP_I_PREDEC: case OP_I_POSTINC: case OP_I_POSTDEC: case OP_POW: case OP_MULTIPLY: case OP_DIVIDE: case OP_MODULO: case OP_REPEAT: case OP_ADD: case OP_SUBTRACT: case OP_I_MULTIPLY: case OP_I_DIVIDE: case OP_I_MODULO: case OP_I_ADD: case OP_I_SUBTRACT: case OP_LEFT_SHIFT: case OP_RIGHT_SHIFT: case OP_BIT_AND: case OP_BIT_XOR: case OP_BIT_OR: case OP_CONCAT: case OP_SUBST: case OP_TRANS: case OP_TRANSR: case OP_READ: case OP_SYSREAD: case OP_RECV: case OP_ANDASSIGN: case OP_ORASSIGN: case OP_DORASSIGN: return TRUE; default: return FALSE; } } STATIC bool S_is_handle_constructor(const OP *o, I32 numargs) { PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR; switch (o->op_type) { case OP_PIPE_OP: case OP_SOCKPAIR: if (numargs == 2) return TRUE; /* FALL THROUGH */ case OP_SYSOPEN: case OP_OPEN: case OP_SELECT: /* XXX c.f. SelectSaver.pm */ case OP_SOCKET: case OP_OPEN_DIR: case OP_ACCEPT: if (numargs == 1) return TRUE; /* FALLTHROUGH */ default: return FALSE; } } static OP * S_refkids(pTHX_ OP *o, I32 type) { if (o && o->op_flags & OPf_KIDS) { OP *kid; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) ref(kid, type); } return o; } OP * Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref) { dVAR; OP *kid; PERL_ARGS_ASSERT_DOREF; if (!o || (PL_parser && PL_parser->error_count)) return o; switch (o->op_type) { case OP_ENTERSUB: if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) && !(o->op_flags & OPf_STACKED)) { o->op_type = OP_RV2CV; /* entersub => rv2cv */ o->op_ppaddr = PL_ppaddr[OP_RV2CV]; assert(cUNOPo->op_first->op_type == OP_NULL); op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */ o->op_flags |= OPf_SPECIAL; o->op_private &= ~1; } break; case OP_COND_EXPR: for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) doref(kid, type, set_op_ref); break; case OP_RV2SV: if (type == OP_DEFINED) o->op_flags |= OPf_SPECIAL; /* don't create GV */ doref(cUNOPo->op_first, o->op_type, set_op_ref); /* FALL THROUGH */ case OP_PADSV: if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : type == OP_RV2HV ? OPpDEREF_HV : OPpDEREF_SV); o->op_flags |= OPf_MOD; } break; case OP_RV2AV: case OP_RV2HV: if (set_op_ref) o->op_flags |= OPf_REF; /* FALL THROUGH */ case OP_RV2GV: if (type == OP_DEFINED) o->op_flags |= OPf_SPECIAL; /* don't create GV */ doref(cUNOPo->op_first, o->op_type, set_op_ref); break; case OP_PADAV: case OP_PADHV: if (set_op_ref) o->op_flags |= OPf_REF; break; case OP_SCALAR: case OP_NULL: if (!(o->op_flags & OPf_KIDS)) break; doref(cBINOPo->op_first, type, set_op_ref); break; case OP_AELEM: case OP_HELEM: doref(cBINOPo->op_first, o->op_type, set_op_ref); if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : type == OP_RV2HV ? OPpDEREF_HV : OPpDEREF_SV); o->op_flags |= OPf_MOD; } break; case OP_SCOPE: case OP_LEAVE: set_op_ref = FALSE; /* FALL THROUGH */ case OP_ENTER: case OP_LIST: if (!(o->op_flags & OPf_KIDS)) break; doref(cLISTOPo->op_last, type, set_op_ref); break; default: break; } return scalar(o); } STATIC OP * S_dup_attrlist(pTHX_ OP *o) { dVAR; OP *rop; PERL_ARGS_ASSERT_DUP_ATTRLIST; /* An attrlist is either a simple OP_CONST or an OP_LIST with kids, * where the first kid is OP_PUSHMARK and the remaining ones * are OP_CONST. We need to push the OP_CONST values. */ if (o->op_type == OP_CONST) rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv)); #ifdef PERL_MAD else if (o->op_type == OP_NULL) rop = NULL; #endif else { assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS)); rop = NULL; for (o = cLISTOPo->op_first; o; o=o->op_sibling) { if (o->op_type == OP_CONST) rop = op_append_elem(OP_LIST, rop, newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv))); } } return rop; } STATIC void S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my) { dVAR; SV *stashsv; PERL_ARGS_ASSERT_APPLY_ATTRS; /* fake up C */ ENTER; /* need to protect against side-effects of 'use' */ stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no; #define ATTRSMODULE "attributes" #define ATTRSMODULE_PM "attributes.pm" if (for_my) { /* Don't force the C if we don't need it. */ SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE); if (svp && *svp != &PL_sv_undef) NOOP; /* already in %INC */ else Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT, newSVpvs(ATTRSMODULE), NULL); } else { Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, newSVpvs(ATTRSMODULE), NULL, op_prepend_elem(OP_LIST, newSVOP(OP_CONST, 0, stashsv), op_prepend_elem(OP_LIST, newSVOP(OP_CONST, 0, newRV(target)), dup_attrlist(attrs)))); } LEAVE; } STATIC void S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp) { dVAR; OP *pack, *imop, *arg; SV *meth, *stashsv; PERL_ARGS_ASSERT_APPLY_ATTRS_MY; if (!attrs) return; assert(target->op_type == OP_PADSV || target->op_type == OP_PADHV || target->op_type == OP_PADAV); /* Ensure that attributes.pm is loaded. */ apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE); /* Need package name for method call. */ pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE)); /* Build up the real arg-list. */ stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no; arg = newOP(OP_PADSV, 0); arg->op_targ = target->op_targ; arg = op_prepend_elem(OP_LIST, newSVOP(OP_CONST, 0, stashsv), op_prepend_elem(OP_LIST, newUNOP(OP_REFGEN, 0, op_lvalue(arg, OP_REFGEN)), dup_attrlist(attrs))); /* Fake up a method call to import */ meth = newSVpvs_share("import"); imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID, op_append_elem(OP_LIST, op_prepend_elem(OP_LIST, pack, list(arg)), newSVOP(OP_METHOD_NAMED, 0, meth))); imop->op_private |= OPpENTERSUB_NOMOD; /* Combine the ops. */ *imopsp = op_append_elem(OP_LIST, *imopsp, imop); } /* =notfor apidoc apply_attrs_string Attempts to apply a list of attributes specified by the C and C arguments to the subroutine identified by the C argument which is expected to be associated with the package identified by the C argument (see L). It gets this wrong, though, in that it does not correctly identify the boundaries of the individual attribute specifications within C. This is not really intended for the public API, but has to be listed here for systems such as AIX which need an explicit export list for symbols. (It's called from XS code in support of the C keyword from F.) Patches to fix it to respect attribute syntax properly would be welcome. =cut */ void Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv, const char *attrstr, STRLEN len) { OP *attrs = NULL; PERL_ARGS_ASSERT_APPLY_ATTRS_STRING; if (!len) { len = strlen(attrstr); } while (len) { for (; isSPACE(*attrstr) && len; --len, ++attrstr) ; if (len) { const char * const sstr = attrstr; for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ; attrs = op_append_elem(OP_LIST, attrs, newSVOP(OP_CONST, 0, newSVpvn(sstr, attrstr-sstr))); } } Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS, newSVpvs(ATTRSMODULE), NULL, op_prepend_elem(OP_LIST, newSVOP(OP_CONST, 0, newSVpv(stashpv,0)), op_prepend_elem(OP_LIST, newSVOP(OP_CONST, 0, newRV(MUTABLE_SV(cv))), attrs))); } STATIC OP * S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp) { dVAR; I32 type; PERL_ARGS_ASSERT_MY_KID; if (!o || (PL_parser && PL_parser->error_count)) return o; type = o->op_type; if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) { (void)my_kid(cUNOPo->op_first, attrs, imopsp); return o; } if (type == OP_LIST) { OP *kid; for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) my_kid(kid, attrs, imopsp); } else if (type == OP_UNDEF #ifdef PERL_MAD || type == OP_STUB #endif ) { return o; } else if (type == OP_RV2SV || /* "our" declaration */ type == OP_RV2AV || type == OP_RV2HV) { /* XXX does this let anything illegal in? */ if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */ yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"", OP_DESC(o), PL_parser->in_my == KEY_our ? "our" : PL_parser->in_my == KEY_state ? "state" : "my")); } else if (attrs) { GV * const gv = cGVOPx_gv(cUNOPo->op_first); PL_parser->in_my = FALSE; PL_parser->in_my_stash = NULL; apply_attrs(GvSTASH(gv), (type == OP_RV2SV ? GvSV(gv) : type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) : type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)), attrs, FALSE); } o->op_private |= OPpOUR_INTRO; return o; } else if (type != OP_PADSV && type != OP_PADAV && type != OP_PADHV && type != OP_PUSHMARK) { yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"", OP_DESC(o), PL_parser->in_my == KEY_our ? "our" : PL_parser->in_my == KEY_state ? "state" : "my")); return o; } else if (attrs && type != OP_PUSHMARK) { HV *stash; PL_parser->in_my = FALSE; PL_parser->in_my_stash = NULL; /* check for C when deciding package */ stash = PAD_COMPNAME_TYPE(o->op_targ); if (!stash) stash = PL_curstash; apply_attrs_my(stash, o, attrs, imopsp); } o->op_flags |= OPf_MOD; o->op_private |= OPpLVAL_INTRO; if (PL_parser->in_my == KEY_state) o->op_private |= OPpPAD_STATE; return o; } OP * Perl_my_attrs(pTHX_ OP *o, OP *attrs) { dVAR; OP *rops; int maybe_scalar = 0; PERL_ARGS_ASSERT_MY_ATTRS; /* [perl #17376]: this appears to be premature, and results in code such as C< our(%x); > executing in list mode rather than void mode */ #if 0 if (o->op_flags & OPf_PARENS) list(o); else maybe_scalar = 1; #else maybe_scalar = 1; #endif if (attrs) SAVEFREEOP(attrs); rops = NULL; o = my_kid(o, attrs, &rops); if (rops) { if (maybe_scalar && o->op_type == OP_PADSV) { o = scalar(op_append_list(OP_LIST, rops, o)); o->op_private |= OPpLVAL_INTRO; } else o = op_append_list(OP_LIST, o, rops); } PL_parser->in_my = FALSE; PL_parser->in_my_stash = NULL; return o; } OP * Perl_sawparens(pTHX_ OP *o) { PERL_UNUSED_CONTEXT; if (o) o->op_flags |= OPf_PARENS; return o; } OP * Perl_bind_match(pTHX_ I32 type, OP *left, OP *right) { OP *o; bool ismatchop = 0; const OPCODE ltype = left->op_type; const OPCODE rtype = right->op_type; PERL_ARGS_ASSERT_BIND_MATCH; if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV || ltype == OP_PADHV) && ckWARN(WARN_MISC)) { const char * const desc = PL_op_desc[( rtype == OP_SUBST || rtype == OP_TRANS || rtype == OP_TRANSR ) ? (int)rtype : OP_MATCH]; const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV) ? "@array" : "%hash"); Perl_warner(aTHX_ packWARN(WARN_MISC), "Applying %s to %s will act on scalar(%s)", desc, sample, sample); } if (rtype == OP_CONST && cSVOPx(right)->op_private & OPpCONST_BARE && cSVOPx(right)->op_private & OPpCONST_STRICT) { no_bareword_allowed(right); } /* !~ doesn't make sense with /r, so error on it for now */ if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) && type == OP_NOT) yyerror("Using !~ with s///r doesn't make sense"); if (rtype == OP_TRANSR && type == OP_NOT) yyerror("Using !~ with tr///r doesn't make sense"); ismatchop = (rtype == OP_MATCH || rtype == OP_SUBST || rtype == OP_TRANS || rtype == OP_TRANSR) && !(right->op_flags & OPf_SPECIAL); if (ismatchop && right->op_private & OPpTARGET_MY) { right->op_targ = 0; right->op_private &= ~OPpTARGET_MY; } if (!(right->op_flags & OPf_STACKED) && ismatchop) { OP *newleft; right->op_flags |= OPf_STACKED; if (rtype != OP_MATCH && rtype != OP_TRANSR && ! (rtype == OP_TRANS && right->op_private & OPpTRANS_IDENTICAL) && ! (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT))) newleft = op_lvalue(left, rtype); else newleft = left; if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR) o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right); else o = op_prepend_elem(rtype, scalar(newleft), right); if (type == OP_NOT) return newUNOP(OP_NOT, 0, scalar(o)); return o; } else return bind_match(type, left, pmruntime(newPMOP(OP_MATCH, 0), right, 0)); } OP * Perl_invert(pTHX_ OP *o) { if (!o) return NULL; return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); } /* =for apidoc Amx|OP *|op_scope|OP *o Wraps up an op tree with some additional ops so that at runtime a dynamic scope will be created. The original ops run in the new dynamic scope, and then, provided that they exit normally, the scope will be unwound. The additional ops used to create and unwind the dynamic scope will normally be an C/C pair, but a C op may be used instead if the ops are simple enough to not need the full dynamic scope structure. =cut */ OP * Perl_op_scope(pTHX_ OP *o) { dVAR; if (o) { if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) { o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); o->op_type = OP_LEAVE; o->op_ppaddr = PL_ppaddr[OP_LEAVE]; } else if (o->op_type == OP_LINESEQ) { OP *kid; o->op_type = OP_SCOPE; o->op_ppaddr = PL_ppaddr[OP_SCOPE]; kid = ((LISTOP*)o)->op_first; if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) { op_null(kid); /* The following deals with things like 'do {1 for 1}' */ kid = kid->op_sibling; if (kid && (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)) op_null(kid); } } else o = newLISTOP(OP_SCOPE, 0, o, NULL); } return o; } int Perl_block_start(pTHX_ int full) { dVAR; const int retval = PL_savestack_ix; pad_block_start(full); SAVEHINTS(); PL_hints &= ~HINT_BLOCK_SCOPE; SAVECOMPILEWARNINGS(); PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings); CALL_BLOCK_HOOKS(bhk_start, full); return retval; } OP* Perl_block_end(pTHX_ I32 floor, OP *seq) { dVAR; const int needblockscope = PL_hints & HINT_BLOCK_SCOPE; OP* retval = scalarseq(seq); CALL_BLOCK_HOOKS(bhk_pre_end, &retval); LEAVE_SCOPE(floor); CopHINTS_set(&PL_compiling, PL_hints); if (needblockscope) PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */ pad_leavemy(); CALL_BLOCK_HOOKS(bhk_post_end, &retval); return retval; } /* =head1 Compile-time scope hooks =for apidoc Ao||blockhook_register Register a set of hooks to be called when the Perl lexical scope changes at compile time. See L. =cut */ void Perl_blockhook_register(pTHX_ BHK *hk) { PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER; Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk))); } STATIC OP * S_newDEFSVOP(pTHX) { dVAR; const PADOFFSET offset = Perl_pad_findmy(aTHX_ STR_WITH_LEN("$_"), 0); if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) { return newSVREF(newGVOP(OP_GV, 0, PL_defgv)); } else { OP * const o = newOP(OP_PADSV, 0); o->op_targ = offset; return o; } } void Perl_newPROG(pTHX_ OP *o) { dVAR; PERL_ARGS_ASSERT_NEWPROG; if (PL_in_eval) { if (PL_eval_root) return; PL_eval_root = newUNOP(OP_LEAVEEVAL, ((PL_in_eval & EVAL_KEEPERR) ? OPf_SPECIAL : 0), o); /* don't use LINKLIST, since PL_eval_root might indirect through * a rather expensive function call and LINKLIST evaluates its * argument more than once */ PL_eval_start = op_linklist(PL_eval_root); PL_eval_root->op_private |= OPpREFCOUNTED; OpREFCNT_set(PL_eval_root, 1); PL_eval_root->op_next = 0; CALL_PEEP(PL_eval_start); } else { if (o->op_type == OP_STUB) { PL_comppad_name = 0; PL_compcv = 0; S_op_destroy(aTHX_ o); return; } PL_main_root = op_scope(sawparens(scalarvoid(o))); PL_curcop = &PL_compiling; PL_main_start = LINKLIST(PL_main_root); PL_main_root->op_private |= OPpREFCOUNTED; OpREFCNT_set(PL_main_root, 1); PL_main_root->op_next = 0; CALL_PEEP(PL_main_start); PL_compcv = 0; /* Register with debugger */ if (PERLDB_INTER) { CV * const cv = get_cvs("DB::postponed", 0); if (cv) { dSP; PUSHMARK(SP); XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling))); PUTBACK; call_sv(MUTABLE_SV(cv), G_DISCARD); } } } } OP * Perl_localize(pTHX_ OP *o, I32 lex) { dVAR; PERL_ARGS_ASSERT_LOCALIZE; if (o->op_flags & OPf_PARENS) /* [perl #17376]: this appears to be premature, and results in code such as C< our(%x); > executing in list mode rather than void mode */ #if 0 list(o); #else NOOP; #endif else { if ( PL_parser->bufptr > PL_parser->oldbufptr && PL_parser->bufptr[-1] == ',' && ckWARN(WARN_PARENTHESIS)) { char *s = PL_parser->bufptr; bool sigil = FALSE; /* some heuristics to detect a potential error */ while (*s && (strchr(", \t\n", *s))) s++; while (1) { if (*s && strchr("@$%*", *s) && *++s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) { s++; sigil = TRUE; while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) s++; while (*s && (strchr(", \t\n", *s))) s++; } else break; } if (sigil && (*s == ';' || *s == '=')) { Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS), "Parentheses missing around \"%s\" list", lex ? (PL_parser->in_my == KEY_our ? "our" : PL_parser->in_my == KEY_state ? "state" : "my") : "local"); } } } if (lex) o = my(o); else o = op_lvalue(o, OP_NULL); /* a bit kludgey */ PL_parser->in_my = FALSE; PL_parser->in_my_stash = NULL; return o; } OP * Perl_jmaybe(pTHX_ OP *o) { PERL_ARGS_ASSERT_JMAYBE; if (o->op_type == OP_LIST) { OP * const o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV))); o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o)); } return o; } static OP * S_fold_constants(pTHX_ register OP *o) { dVAR; register OP * VOL curop; OP *newop; VOL I32 type = o->op_type; SV * VOL sv = NULL; int ret = 0; I32 oldscope; OP *old_next; SV * const oldwarnhook = PL_warnhook; SV * const olddiehook = PL_diehook; COP not_compiling; dJMPENV; PERL_ARGS_ASSERT_FOLD_CONSTANTS; if (PL_opargs[type] & OA_RETSCALAR) scalar(o); if (PL_opargs[type] & OA_TARGET && !o->op_targ) o->op_targ = pad_alloc(type, SVs_PADTMP); /* integerize op, unless it happens to be C<-foo>. * XXX should pp_i_negate() do magic string negation instead? */ if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER) && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST && (cUNOPo->op_first->op_private & OPpCONST_BARE))) { o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)]; } if (!(PL_opargs[type] & OA_FOLDCONST)) goto nope; switch (type) { case OP_NEGATE: /* XXX might want a ck_negate() for this */ cUNOPo->op_first->op_private &= ~OPpCONST_STRICT; break; case OP_UCFIRST: case OP_LCFIRST: case OP_UC: case OP_LC: case OP_SLT: case OP_SGT: case OP_SLE: case OP_SGE: case OP_SCMP: case OP_SPRINTF: /* XXX what about the numeric ops? */ if (PL_hints & HINT_LOCALE) goto nope; break; } if (PL_parser && PL_parser->error_count) goto nope; /* Don't try to run w/ errors */ for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { const OPCODE type = curop->op_type; if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) && type != OP_LIST && type != OP_SCALAR && type != OP_NULL && type != OP_PUSHMARK) { goto nope; } } curop = LINKLIST(o); old_next = o->op_next; o->op_next = 0; PL_op = curop; oldscope = PL_scopestack_ix; create_eval_scope(G_FAKINGEVAL); /* Verify that we don't need to save it: */ assert(PL_curcop == &PL_compiling); StructCopy(&PL_compiling, ¬_compiling, COP); PL_curcop = ¬_compiling; /* The above ensures that we run with all the correct hints of the currently compiling COP, but that IN_PERL_RUNTIME is not true. */ assert(IN_PERL_RUNTIME); PL_warnhook = PERL_WARNHOOK_FATAL; PL_diehook = NULL; JMPENV_PUSH(ret); switch (ret) { case 0: CALLRUNOPS(aTHX); sv = *(PL_stack_sp--); if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */ pad_swipe(o->op_targ, FALSE); else if (SvTEMP(sv)) { /* grab mortal temp? */ SvREFCNT_inc_simple_void(sv); SvTEMP_off(sv); } break; case 3: /* Something tried to die. Abandon constant folding. */ /* Pretend the error never happened. */ CLEAR_ERRSV(); o->op_next = old_next; break; default: JMPENV_POP; /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */ PL_warnhook = oldwarnhook; PL_diehook = olddiehook; /* XXX note that this croak may fail as we've already blown away * the stack - eg any nested evals */ Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret); } JMPENV_POP; PL_warnhook = oldwarnhook; PL_diehook = olddiehook; PL_curcop = &PL_compiling; if (PL_scopestack_ix > oldscope) delete_eval_scope(); if (ret) goto nope; #ifndef PERL_MAD op_free(o); #endif assert(sv); if (type == OP_RV2GV) newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv)); else newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv)); op_getmad(o,newop,'f'); return newop; nope: return o; } static OP * S_gen_constant_list(pTHX_ register OP *o) { dVAR; register OP *curop; const I32 oldtmps_floor = PL_tmps_floor; list(o); if (PL_parser && PL_parser->error_count) return o; /* Don't attempt to run with errors */ PL_op = curop = LINKLIST(o); o->op_next = 0; CALL_PEEP(curop); pp_pushmark(); CALLRUNOPS(aTHX); PL_op = curop; assert (!(curop->op_flags & OPf_SPECIAL)); assert(curop->op_type == OP_RANGE); pp_anonlist(); PL_tmps_floor = oldtmps_floor; o->op_type = OP_RV2AV; o->op_ppaddr = PL_ppaddr[OP_RV2AV]; o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */ o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */ o->op_opt = 0; /* needs to be revisited in rpeep() */ curop = ((UNOP*)o)->op_first; ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--)); #ifdef PERL_MAD op_getmad(curop,o,'O'); #else op_free(curop); #endif LINKLIST(o); return list(o); } OP * Perl_convert(pTHX_ I32 type, I32 flags, OP *o) { dVAR; if (!o || o->op_type != OP_LIST) o = newLISTOP(OP_LIST, 0, o, NULL); else o->op_flags &= ~OPf_WANT; if (!(PL_opargs[type] & OA_MARK)) op_null(cLISTOPo->op_first); o->op_type = (OPCODE)type; o->op_ppaddr = PL_ppaddr[type]; o->op_flags |= flags; o = CHECKOP(type, o); if (o->op_type != (unsigned)type) return o; return fold_constants(o); } /* =head1 Optree Manipulation Functions */ /* List constructors */ /* =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last Append an item to the list of ops contained directly within a list-type op, returning the lengthened list. I is the list-type op, and I is the op to append to the list. I specifies the intended opcode for the list. If I is not already a list of the right type, it will be upgraded into one. If either I or I is null, the other is returned unchanged. =cut */ OP * Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last) { if (!first) return last; if (!last) return first; if (first->op_type != (unsigned)type || (type == OP_LIST && (first->op_flags & OPf_PARENS))) { return newLISTOP(type, 0, first, last); } if (first->op_flags & OPf_KIDS) ((LISTOP*)first)->op_last->op_sibling = last; else { first->op_flags |= OPf_KIDS; ((LISTOP*)first)->op_first = last; } ((LISTOP*)first)->op_last = last; return first; } /* =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last Concatenate the lists of ops contained directly within two list-type ops, returning the combined list. I and I are the list-type ops to concatenate. I specifies the intended opcode for the list. If either I or I is not already a list of the right type, it will be upgraded into one. If either I or I is null, the other is returned unchanged. =cut */ OP * Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last) { if (!first) return last; if (!last) return first; if (first->op_type != (unsigned)type) return op_prepend_elem(type, first, last); if (last->op_type != (unsigned)type) return op_append_elem(type, first, last); ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first; ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last; first->op_flags |= (last->op_flags & OPf_KIDS); #ifdef PERL_MAD if (((LISTOP*)last)->op_first && first->op_madprop) { MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop; if (mp) { while (mp->mad_next) mp = mp->mad_next; mp->mad_next = first->op_madprop; } else { ((LISTOP*)last)->op_first->op_madprop = first->op_madprop; } } first->op_madprop = last->op_madprop; last->op_madprop = 0; #endif S_op_destroy(aTHX_ last); return first; } /* =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last Prepend an item to the list of ops contained directly within a list-type op, returning the lengthened list. I is the op to prepend to the list, and I is the list-type op. I specifies the intended opcode for the list. If I is not already a list of the right type, it will be upgraded into one. If either I or I is null, the other is returned unchanged. =cut */ OP * Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last) { if (!first) return last; if (!last) return first; if (last->op_type == (unsigned)type) { if (type == OP_LIST) { /* already a PUSHMARK there */ first->op_sibling = ((LISTOP*)last)->op_first->op_sibling; ((LISTOP*)last)->op_first->op_sibling = first; if (!(first->op_flags & OPf_PARENS)) last->op_flags &= ~OPf_PARENS; } else { if (!(last->op_flags & OPf_KIDS)) { ((LISTOP*)last)->op_last = first; last->op_flags |= OPf_KIDS; } first->op_sibling = ((LISTOP*)last)->op_first; ((LISTOP*)last)->op_first = first; } last->op_flags |= OPf_KIDS; return last; } return newLISTOP(type, 0, first, last); } /* Constructors */ #ifdef PERL_MAD TOKEN * Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop) { TOKEN *tk; Newxz(tk, 1, TOKEN); tk->tk_type = (OPCODE)optype; tk->tk_type = 12345; tk->tk_lval = lval; tk->tk_mad = madprop; return tk; } void Perl_token_free(pTHX_ TOKEN* tk) { PERL_ARGS_ASSERT_TOKEN_FREE; if (tk->tk_type != 12345) return; mad_free(tk->tk_mad); Safefree(tk); } void Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot) { MADPROP* mp; MADPROP* tm; PERL_ARGS_ASSERT_TOKEN_GETMAD; if (tk->tk_type != 12345) { Perl_warner(aTHX_ packWARN(WARN_MISC), "Invalid TOKEN object ignored"); return; } tm = tk->tk_mad; if (!tm) return; /* faked up qw list? */ if (slot == '(' && tm->mad_type == MAD_SV && SvPVX((SV *)tm->mad_val)[0] == 'q') slot = 'x'; if (o) { mp = o->op_madprop; if (mp) { for (;;) { /* pretend constant fold didn't happen? */ if (mp->mad_key == 'f' && (o->op_type == OP_CONST || o->op_type == OP_GV) ) { token_getmad(tk,(OP*)mp->mad_val,slot); return; } if (!mp->mad_next) break; mp = mp->mad_next; } mp->mad_next = tm; mp = mp->mad_next; } else { o->op_madprop = tm; mp = o->op_madprop; } if (mp->mad_key == 'X') mp->mad_key = slot; /* just change the first one */ tk->tk_mad = 0; } else mad_free(tm); Safefree(tk); } void Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot) { MADPROP* mp; if (!from) return; if (o) { mp = o->op_madprop; if (mp) { for (;;) { /* pretend constant fold didn't happen? */ if (mp->mad_key == 'f' && (o->op_type == OP_CONST || o->op_type == OP_GV) ) { op_getmad(from,(OP*)mp->mad_val,slot); return; } if (!mp->mad_next) break; mp = mp->mad_next; } mp->mad_next = newMADPROP(slot,MAD_OP,from,0); } else { o->op_madprop = newMADPROP(slot,MAD_OP,from,0); } } } void Perl_op_getmad(pTHX_ OP* from, OP* o, char slot) { MADPROP* mp; if (!from) return; if (o) { mp = o->op_madprop; if (mp) { for (;;) { /* pretend constant fold didn't happen? */ if (mp->mad_key == 'f' && (o->op_type == OP_CONST || o->op_type == OP_GV) ) { op_getmad(from,(OP*)mp->mad_val,slot); return; } if (!mp->mad_next) break; mp = mp->mad_next; } mp->mad_next = newMADPROP(slot,MAD_OP,from,1); } else { o->op_madprop = newMADPROP(slot,MAD_OP,from,1); } } else { PerlIO_printf(PerlIO_stderr(), "DESTROYING op = %0"UVxf"\n", PTR2UV(from)); op_free(from); } } void Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot) { MADPROP* tm; if (!mp || !o) return; if (slot) mp->mad_key = slot; tm = o->op_madprop; o->op_madprop = mp; for (;;) { if (!mp->mad_next) break; mp = mp->mad_next; } mp->mad_next = tm; } void Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot) { if (!o) return; addmad(tm, &(o->op_madprop), slot); } void Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot) { MADPROP* mp; if (!tm || !root) return; if (slot) tm->mad_key = slot; mp = *root; if (!mp) { *root = tm; return; } for (;;) { if (!mp->mad_next) break; mp = mp->mad_next; } mp->mad_next = tm; } MADPROP * Perl_newMADsv(pTHX_ char key, SV* sv) { PERL_ARGS_ASSERT_NEWMADSV; return newMADPROP(key, MAD_SV, sv, 0); } MADPROP * Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen) { MADPROP *mp; Newxz(mp, 1, MADPROP); mp->mad_next = 0; mp->mad_key = key; mp->mad_vlen = vlen; mp->mad_type = type; mp->mad_val = val; /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */ return mp; } void Perl_mad_free(pTHX_ MADPROP* mp) { /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */ if (!mp) return; if (mp->mad_next) mad_free(mp->mad_next); /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen) PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */ switch (mp->mad_type) { case MAD_NULL: break; case MAD_PV: Safefree((char*)mp->mad_val); break; case MAD_OP: if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */ op_free((OP*)mp->mad_val); break; case MAD_SV: sv_free(MUTABLE_SV(mp->mad_val)); break; default: PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n"); break; } Safefree(mp); } #endif /* =head1 Optree construction =for apidoc Am|OP *|newNULLLIST Constructs, checks, and returns a new C op, which represents an empty list expression. =cut */ OP * Perl_newNULLLIST(pTHX) { return newOP(OP_STUB, 0); } static OP * S_force_list(pTHX_ OP *o) { if (!o || o->op_type != OP_LIST) o = newLISTOP(OP_LIST, 0, o, NULL); op_null(o); return o; } /* =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last Constructs, checks, and returns an op of any list type. I is the opcode. I gives the eight bits of C, except that C will be set automatically if required. I and I supply up to two ops to be direct children of the list op; they are consumed by this function and become part of the constructed op tree. =cut */ OP * Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) { dVAR; LISTOP *listop; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP); NewOp(1101, listop, 1, LISTOP); listop->op_type = (OPCODE)type; listop->op_ppaddr = PL_ppaddr[type]; if (first || last) flags |= OPf_KIDS; listop->op_flags = (U8)flags; if (!last && first) last = first; else if (!first && last) first = last; else if (first) first->op_sibling = last; listop->op_first = first; listop->op_last = last; if (type == OP_LIST) { OP* const pushop = newOP(OP_PUSHMARK, 0); pushop->op_sibling = first; listop->op_first = pushop; listop->op_flags |= OPf_KIDS; if (!last) listop->op_last = pushop; } return CHECKOP(type, listop); } /* =for apidoc Am|OP *|newOP|I32 type|I32 flags Constructs, checks, and returns an op of any base type (any type that has no extra fields). I is the opcode. I gives the eight bits of C, and, shifted up eight bits, the eight bits of C. =cut */ OP * Perl_newOP(pTHX_ I32 type, I32 flags) { dVAR; OP *o; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); NewOp(1101, o, 1, OP); o->op_type = (OPCODE)type; o->op_ppaddr = PL_ppaddr[type]; o->op_flags = (U8)flags; o->op_latefree = 0; o->op_latefreed = 0; o->op_attached = 0; o->op_next = o; o->op_private = (U8)(0 | (flags >> 8)); if (PL_opargs[type] & OA_RETSCALAR) scalar(o); if (PL_opargs[type] & OA_TARGET) o->op_targ = pad_alloc(type, SVs_PADTMP); return CHECKOP(type, o); } /* =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first Constructs, checks, and returns an op of any unary type. I is the opcode. I gives the eight bits of C, except that C will be set automatically if required, and, shifted up eight bits, the eight bits of C, except that the bit with value 1 is automatically set. I supplies an optional op to be the direct child of the unary op; it is consumed by this function and become part of the constructed op tree. =cut */ OP * Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first) { dVAR; UNOP *unop; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP || type == OP_SASSIGN || type == OP_ENTERTRY || type == OP_NULL ); if (!first) first = newOP(OP_STUB, 0); if (PL_opargs[type] & OA_MARK) first = force_list(first); NewOp(1101, unop, 1, UNOP); unop->op_type = (OPCODE)type; unop->op_ppaddr = PL_ppaddr[type]; unop->op_first = first; unop->op_flags = (U8)(flags | OPf_KIDS); unop->op_private = (U8)(1 | (flags >> 8)); unop = (UNOP*) CHECKOP(type, unop); if (unop->op_next) return (OP*)unop; return fold_constants((OP *) unop); } /* =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last Constructs, checks, and returns an op of any binary type. I is the opcode. I gives the eight bits of C, except that C will be set automatically, and, shifted up eight bits, the eight bits of C, except that the bit with value 1 or 2 is automatically set as required. I and I supply up to two ops to be the direct children of the binary op; they are consumed by this function and become part of the constructed op tree. =cut */ OP * Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last) { dVAR; BINOP *binop; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP || type == OP_SASSIGN || type == OP_NULL ); NewOp(1101, binop, 1, BINOP); if (!first) first = newOP(OP_NULL, 0); binop->op_type = (OPCODE)type; binop->op_ppaddr = PL_ppaddr[type]; binop->op_first = first; binop->op_flags = (U8)(flags | OPf_KIDS); if (!last) { last = first; binop->op_private = (U8)(1 | (flags >> 8)); } else { binop->op_private = (U8)(2 | (flags >> 8)); first->op_sibling = last; } binop = (BINOP*)CHECKOP(type, binop); if (binop->op_next || binop->op_type != (OPCODE)type) return (OP*)binop; binop->op_last = binop->op_first->op_sibling; return fold_constants((OP *)binop); } static int uvcompare(const void *a, const void *b) __attribute__nonnull__(1) __attribute__nonnull__(2) __attribute__pure__; static int uvcompare(const void *a, const void *b) { if (*((const UV *)a) < (*(const UV *)b)) return -1; if (*((const UV *)a) > (*(const UV *)b)) return 1; if (*((const UV *)a+1) < (*(const UV *)b+1)) return -1; if (*((const UV *)a+1) > (*(const UV *)b+1)) return 1; return 0; } static OP * S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl) { dVAR; SV * const tstr = ((SVOP*)expr)->op_sv; SV * const rstr = #ifdef PERL_MAD (repl->op_type == OP_NULL) ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv : #endif ((SVOP*)repl)->op_sv; STRLEN tlen; STRLEN rlen; const U8 *t = (U8*)SvPV_const(tstr, tlen); const U8 *r = (U8*)SvPV_const(rstr, rlen); register I32 i; register I32 j; I32 grows = 0; register short *tbl; const I32 complement = o->op_private & OPpTRANS_COMPLEMENT; const I32 squash = o->op_private & OPpTRANS_SQUASH; I32 del = o->op_private & OPpTRANS_DELETE; SV* swash; PERL_ARGS_ASSERT_PMTRANS; PL_hints |= HINT_BLOCK_SCOPE; if (SvUTF8(tstr)) o->op_private |= OPpTRANS_FROM_UTF; if (SvUTF8(rstr)) o->op_private |= OPpTRANS_TO_UTF; if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) { SV* const listsv = newSVpvs("# comment\n"); SV* transv = NULL; const U8* tend = t + tlen; const U8* rend = r + rlen; STRLEN ulen; UV tfirst = 1; UV tlast = 0; IV tdiff; UV rfirst = 1; UV rlast = 0; IV rdiff; IV diff; I32 none = 0; U32 max = 0; I32 bits; I32 havefinal = 0; U32 final = 0; const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF; const I32 to_utf = o->op_private & OPpTRANS_TO_UTF; U8* tsave = NULL; U8* rsave = NULL; const U32 flags = UTF8_ALLOW_DEFAULT; if (!from_utf) { STRLEN len = tlen; t = tsave = bytes_to_utf8(t, &len); tend = t + len; } if (!to_utf && rlen) { STRLEN len = rlen; r = rsave = bytes_to_utf8(r, &len); rend = r + len; } /* There are several snags with this code on EBCDIC: 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes). 2. scan_const() in toke.c has encoded chars in native encoding which makes ranges at least in EBCDIC 0..255 range the bottom odd. */ if (complement) { U8 tmpbuf[UTF8_MAXBYTES+1]; UV *cp; UV nextmin = 0; Newx(cp, 2*tlen, UV); i = 0; transv = newSVpvs(""); while (t < tend) { cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags); t += ulen; if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { t++; cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags); t += ulen; } else { cp[2*i+1] = cp[2*i]; } i++; } qsort(cp, i, 2*sizeof(UV), uvcompare); for (j = 0; j < i; j++) { UV val = cp[2*j]; diff = val - nextmin; if (diff > 0) { t = uvuni_to_utf8(tmpbuf,nextmin); sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); if (diff > 1) { U8 range_mark = UTF_TO_NATIVE(0xff); t = uvuni_to_utf8(tmpbuf, val - 1); sv_catpvn(transv, (char *)&range_mark, 1); sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); } } val = cp[2*j+1]; if (val >= nextmin) nextmin = val + 1; } t = uvuni_to_utf8(tmpbuf,nextmin); sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); { U8 range_mark = UTF_TO_NATIVE(0xff); sv_catpvn(transv, (char *)&range_mark, 1); } t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff, UNICODE_ALLOW_SUPER); sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf); t = (const U8*)SvPVX_const(transv); tlen = SvCUR(transv); tend = t + tlen; Safefree(cp); } else if (!rlen && !del) { r = t; rlen = tlen; rend = tend; } if (!squash) { if ((!rlen && !del) || t == r || (tlen == rlen && memEQ((char *)t, (char *)r, tlen))) { o->op_private |= OPpTRANS_IDENTICAL; } } while (t < tend || tfirst <= tlast) { /* see if we need more "t" chars */ if (tfirst > tlast) { tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags); t += ulen; if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */ t++; tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags); t += ulen; } else tlast = tfirst; } /* now see if we need more "r" chars */ if (rfirst > rlast) { if (r < rend) { rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags); r += ulen; if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */ r++; rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags); r += ulen; } else rlast = rfirst; } else { if (!havefinal++) final = rlast; rfirst = rlast = 0xffffffff; } } /* now see which range will peter our first, if either. */ tdiff = tlast - tfirst; rdiff = rlast - rfirst; if (tdiff <= rdiff) diff = tdiff; else diff = rdiff; if (rfirst == 0xffffffff) { diff = tdiff; /* oops, pretend rdiff is infinite */ if (diff > 0) Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n", (long)tfirst, (long)tlast); else Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst); } else { if (diff > 0) Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n", (long)tfirst, (long)(tfirst + diff), (long)rfirst); else Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n", (long)tfirst, (long)rfirst); if (rfirst + diff > max) max = rfirst + diff; if (!grows) grows = (tfirst < rfirst && UNISKIP(tfirst) < UNISKIP(rfirst + diff)); rfirst += diff + 1; } tfirst += diff + 1; } none = ++max; if (del) del = ++max; if (max > 0xffff) bits = 32; else if (max > 0xff) bits = 16; else bits = 8; PerlMemShared_free(cPVOPo->op_pv); cPVOPo->op_pv = NULL; swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none)); #ifdef USE_ITHREADS cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP); SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix)); PAD_SETSV(cPADOPo->op_padix, swash); SvPADTMP_on(swash); SvREADONLY_on(swash); #else cSVOPo->op_sv = swash; #endif SvREFCNT_dec(listsv); SvREFCNT_dec(transv); if (!del && havefinal && rlen) (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5, newSVuv((UV)final), 0); if (grows) o->op_private |= OPpTRANS_GROWS; Safefree(tsave); Safefree(rsave); #ifdef PERL_MAD op_getmad(expr,o,'e'); op_getmad(repl,o,'r'); #else op_free(expr); op_free(repl); #endif return o; } tbl = (short*)cPVOPo->op_pv; if (complement) { Zero(tbl, 256, short); for (i = 0; i < (I32)tlen; i++) tbl[t[i]] = -1; for (i = 0, j = 0; i < 256; i++) { if (!tbl[i]) { if (j >= (I32)rlen) { if (del) tbl[i] = -2; else if (rlen) tbl[i] = r[j-1]; else tbl[i] = (short)i; } else { if (i < 128 && r[j] >= 128) grows = 1; tbl[i] = r[j++]; } } } if (!del) { if (!rlen) { j = rlen; if (!squash) o->op_private |= OPpTRANS_IDENTICAL; } else if (j >= (I32)rlen) j = rlen - 1; else { tbl = (short *) PerlMemShared_realloc(tbl, (0x101+rlen-j) * sizeof(short)); cPVOPo->op_pv = (char*)tbl; } tbl[0x100] = (short)(rlen - j); for (i=0; i < (I32)rlen - j; i++) tbl[0x101+i] = r[j+i]; } } else { if (!rlen && !del) { r = t; rlen = tlen; if (!squash) o->op_private |= OPpTRANS_IDENTICAL; } else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) { o->op_private |= OPpTRANS_IDENTICAL; } for (i = 0; i < 256; i++) tbl[i] = -1; for (i = 0, j = 0; i < (I32)tlen; i++,j++) { if (j >= (I32)rlen) { if (del) { if (tbl[t[i]] == -1) tbl[t[i]] = -2; continue; } --j; } if (tbl[t[i]] == -1) { if (t[i] < 128 && r[j] >= 128) grows = 1; tbl[t[i]] = r[j]; } } } if(del && rlen == tlen) { Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator"); } else if(rlen > tlen) { Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list"); } if (grows) o->op_private |= OPpTRANS_GROWS; #ifdef PERL_MAD op_getmad(expr,o,'e'); op_getmad(repl,o,'r'); #else op_free(expr); op_free(repl); #endif return o; } /* =for apidoc Am|OP *|newPMOP|I32 type|I32 flags Constructs, checks, and returns an op of any pattern matching type. I is the opcode. I gives the eight bits of C and, shifted up eight bits, the eight bits of C. =cut */ OP * Perl_newPMOP(pTHX_ I32 type, I32 flags) { dVAR; PMOP *pmop; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP); NewOp(1101, pmop, 1, PMOP); pmop->op_type = (OPCODE)type; pmop->op_ppaddr = PL_ppaddr[type]; pmop->op_flags = (U8)flags; pmop->op_private = (U8)(0 | (flags >> 8)); if (PL_hints & HINT_RE_TAINT) pmop->op_pmflags |= PMf_RETAINT; if (PL_hints & HINT_LOCALE) { pmop->op_pmflags |= PMf_LOCALE; } else if ((! (PL_hints & HINT_BYTES)) && (PL_hints & HINT_UNI_8_BIT)) { pmop->op_pmflags |= RXf_PMf_UNICODE; } if (PL_hints & HINT_RE_FLAGS) { SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_ PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0 ); if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags); reflags = Perl_refcounted_he_fetch_pvn(aTHX_ PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_dul"), 0, 0 ); if (reflags && SvOK(reflags)) { pmop->op_pmflags &= ~(RXf_PMf_LOCALE|RXf_PMf_UNICODE); pmop->op_pmflags |= SvIV(reflags); } } #ifdef USE_ITHREADS assert(SvPOK(PL_regex_pad[0])); if (SvCUR(PL_regex_pad[0])) { /* Pop off the "packed" IV from the end. */ SV *const repointer_list = PL_regex_pad[0]; const char *p = SvEND(repointer_list) - sizeof(IV); const IV offset = *((IV*)p); assert(SvCUR(repointer_list) % sizeof(IV) == 0); SvEND_set(repointer_list, p); pmop->op_pmoffset = offset; /* This slot should be free, so assert this: */ assert(PL_regex_pad[offset] == &PL_sv_undef); } else { SV * const repointer = &PL_sv_undef; av_push(PL_regex_padav, repointer); pmop->op_pmoffset = av_len(PL_regex_padav); PL_regex_pad = AvARRAY(PL_regex_padav); } #endif return CHECKOP(type, pmop); } /* Given some sort of match op o, and an expression expr containing a * pattern, either compile expr into a regex and attach it to o (if it's * constant), or convert expr into a runtime regcomp op sequence (if it's * not) * * isreg indicates that the pattern is part of a regex construct, eg * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or * split "pattern", which aren't. In the former case, expr will be a list * if the pattern contains more than one term (eg /a$b/) or if it contains * a replacement, ie s/// or tr///. */ OP * Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg) { dVAR; PMOP *pm; LOGOP *rcop; I32 repl_has_vars = 0; OP* repl = NULL; bool reglist; PERL_ARGS_ASSERT_PMRUNTIME; if ( o->op_type == OP_SUBST || o->op_type == OP_TRANS || o->op_type == OP_TRANSR ) { /* last element in list is the replacement; pop it */ OP* kid; repl = cLISTOPx(expr)->op_last; kid = cLISTOPx(expr)->op_first; while (kid->op_sibling != repl) kid = kid->op_sibling; kid->op_sibling = NULL; cLISTOPx(expr)->op_last = kid; } if (isreg && expr->op_type == OP_LIST && cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last) { /* convert single element list to element */ OP* const oe = expr; expr = cLISTOPx(oe)->op_first->op_sibling; cLISTOPx(oe)->op_first->op_sibling = NULL; cLISTOPx(oe)->op_last = NULL; op_free(oe); } if (o->op_type == OP_TRANS || o->op_type == OP_TRANSR) { return pmtrans(o, expr, repl); } reglist = isreg && expr->op_type == OP_LIST; if (reglist) op_null(expr); PL_hints |= HINT_BLOCK_SCOPE; pm = (PMOP*)o; if (expr->op_type == OP_CONST) { SV *pat = ((SVOP*)expr)->op_sv; U32 pm_flags = pm->op_pmflags & PMf_COMPILETIME; if (o->op_flags & OPf_SPECIAL) pm_flags |= RXf_SPLIT; if (DO_UTF8(pat)) { assert (SvUTF8(pat)); } else if (SvUTF8(pat)) { /* Not doing UTF-8, despite what the SV says. Is this only if we're trapped in use 'bytes'? */ /* Make a copy of the octet sequence, but without the flag on, as the compiler now honours the SvUTF8 flag on pat. */ STRLEN len; const char *const p = SvPV(pat, len); pat = newSVpvn_flags(p, len, SVs_TEMP); } PM_SETRE(pm, CALLREGCOMP(pat, pm_flags)); #ifdef PERL_MAD op_getmad(expr,(OP*)pm,'e'); #else op_free(expr); #endif } else { if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) expr = newUNOP((!(PL_hints & HINT_RE_EVAL) ? OP_REGCRESET : OP_REGCMAYBE),0,expr); NewOp(1101, rcop, 1, LOGOP); rcop->op_type = OP_REGCOMP; rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP]; rcop->op_first = scalar(expr); rcop->op_flags |= OPf_KIDS | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0) | (reglist ? OPf_STACKED : 0); rcop->op_private = 1; rcop->op_other = o; if (reglist) rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP); /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */ PL_cv_has_eval = 1; /* establish postfix order */ if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) { LINKLIST(expr); rcop->op_next = expr; ((UNOP*)expr)->op_first->op_next = (OP*)rcop; } else { rcop->op_next = LINKLIST(expr); expr->op_next = (OP*)rcop; } op_prepend_elem(o->op_type, scalar((OP*)rcop), o); } if (repl) { OP *curop; if (pm->op_pmflags & PMf_EVAL) { curop = NULL; if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end) CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end); } else if (repl->op_type == OP_CONST) curop = repl; else { OP *lastop = NULL; for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) { if (curop->op_type == OP_SCOPE || curop->op_type == OP_LEAVE || (PL_opargs[curop->op_type] & OA_DANGEROUS)) { if (curop->op_type == OP_GV) { GV * const gv = cGVOPx_gv(curop); repl_has_vars = 1; if (strchr("&`'123456789+-\016\022", *GvENAME(gv))) break; } else if (curop->op_type == OP_RV2CV) break; else if (curop->op_type == OP_RV2SV || curop->op_type == OP_RV2AV || curop->op_type == OP_RV2HV || curop->op_type == OP_RV2GV) { if (lastop && lastop->op_type != OP_GV) /*funny deref?*/ break; } else if (curop->op_type == OP_PADSV || curop->op_type == OP_PADAV || curop->op_type == OP_PADHV || curop->op_type == OP_PADANY) { repl_has_vars = 1; } else if (curop->op_type == OP_PUSHRE) NOOP; /* Okay here, dangerous in newASSIGNOP */ else break; } lastop = curop; } } if (curop == repl && !(repl_has_vars && (!PM_GETRE(pm) || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN))) { pm->op_pmflags |= PMf_CONST; /* const for long enough */ op_prepend_elem(o->op_type, scalar(repl), o); } else { if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */ pm->op_pmflags |= PMf_MAYBE_CONST; } NewOp(1101, rcop, 1, LOGOP); rcop->op_type = OP_SUBSTCONT; rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT]; rcop->op_first = scalar(repl); rcop->op_flags |= OPf_KIDS; rcop->op_private = 1; rcop->op_other = o; /* establish postfix order */ rcop->op_next = LINKLIST(repl); repl->op_next = (OP*)rcop; pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop); assert(!(pm->op_pmflags & PMf_ONCE)); pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop); rcop->op_next = 0; } } return (OP*)pm; } /* =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv Constructs, checks, and returns an op of any type that involves an embedded SV. I is the opcode. I gives the eight bits of C. I gives the SV to embed in the op; this function takes ownership of one reference to it. =cut */ OP * Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv) { dVAR; SVOP *svop; PERL_ARGS_ASSERT_NEWSVOP; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP); NewOp(1101, svop, 1, SVOP); svop->op_type = (OPCODE)type; svop->op_ppaddr = PL_ppaddr[type]; svop->op_sv = sv; svop->op_next = (OP*)svop; svop->op_flags = (U8)flags; if (PL_opargs[type] & OA_RETSCALAR) scalar((OP*)svop); if (PL_opargs[type] & OA_TARGET) svop->op_targ = pad_alloc(type, SVs_PADTMP); return CHECKOP(type, svop); } #ifdef USE_ITHREADS /* =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv Constructs, checks, and returns an op of any type that involves a reference to a pad element. I is the opcode. I gives the eight bits of C. A pad slot is automatically allocated, and is populated with I; this function takes ownership of one reference to it. This function only exists if Perl has been compiled to use ithreads. =cut */ OP * Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv) { dVAR; PADOP *padop; PERL_ARGS_ASSERT_NEWPADOP; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP); NewOp(1101, padop, 1, PADOP); padop->op_type = (OPCODE)type; padop->op_ppaddr = PL_ppaddr[type]; padop->op_padix = pad_alloc(type, SVs_PADTMP); SvREFCNT_dec(PAD_SVl(padop->op_padix)); PAD_SETSV(padop->op_padix, sv); assert(sv); SvPADTMP_on(sv); padop->op_next = (OP*)padop; padop->op_flags = (U8)flags; if (PL_opargs[type] & OA_RETSCALAR) scalar((OP*)padop); if (PL_opargs[type] & OA_TARGET) padop->op_targ = pad_alloc(type, SVs_PADTMP); return CHECKOP(type, padop); } #endif /* !USE_ITHREADS */ /* =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv Constructs, checks, and returns an op of any type that involves an embedded reference to a GV. I is the opcode. I gives the eight bits of C. I identifies the GV that the op should reference; calling this function does not transfer ownership of any reference to it. =cut */ OP * Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv) { dVAR; PERL_ARGS_ASSERT_NEWGVOP; #ifdef USE_ITHREADS GvIN_PAD_on(gv); return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv)); #else return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv)); #endif } /* =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv Constructs, checks, and returns an op of any type that involves an embedded C-level pointer (PV). I is the opcode. I gives the eight bits of C. I supplies the C-level pointer, which must have been allocated using L; the memory will be freed when the op is destroyed. =cut */ OP * Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv) { dVAR; PVOP *pvop; assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP); NewOp(1101, pvop, 1, PVOP); pvop->op_type = (OPCODE)type; pvop->op_ppaddr = PL_ppaddr[type]; pvop->op_pv = pv; pvop->op_next = (OP*)pvop; pvop->op_flags = (U8)flags; if (PL_opargs[type] & OA_RETSCALAR) scalar((OP*)pvop); if (PL_opargs[type] & OA_TARGET) pvop->op_targ = pad_alloc(type, SVs_PADTMP); return CHECKOP(type, pvop); } #ifdef PERL_MAD OP* #else void #endif Perl_package(pTHX_ OP *o) { dVAR; SV *const sv = cSVOPo->op_sv; #ifdef PERL_MAD OP *pegop; #endif PERL_ARGS_ASSERT_PACKAGE; save_hptr(&PL_curstash); save_item(PL_curstname); PL_curstash = gv_stashsv(sv, GV_ADD); sv_setsv(PL_curstname, sv); PL_hints |= HINT_BLOCK_SCOPE; PL_parser->copline = NOLINE; PL_parser->expect = XSTATE; #ifndef PERL_MAD op_free(o); #else if (!PL_madskills) { op_free(o); return NULL; } pegop = newOP(OP_NULL,0); op_getmad(o,pegop,'P'); return pegop; #endif } void Perl_package_version( pTHX_ OP *v ) { dVAR; U32 savehints = PL_hints; PERL_ARGS_ASSERT_PACKAGE_VERSION; PL_hints &= ~HINT_STRICT_VARS; sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv ); PL_hints = savehints; op_free(v); } #ifdef PERL_MAD OP* #else void #endif Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg) { dVAR; OP *pack; OP *imop; OP *veop; #ifdef PERL_MAD OP *pegop = newOP(OP_NULL,0); #endif SV *use_version = NULL; PERL_ARGS_ASSERT_UTILIZE; if (idop->op_type != OP_CONST) Perl_croak(aTHX_ "Module name must be constant"); if (PL_madskills) op_getmad(idop,pegop,'U'); veop = NULL; if (version) { SV * const vesv = ((SVOP*)version)->op_sv; if (PL_madskills) op_getmad(version,pegop,'V'); if (!arg && !SvNIOKp(vesv)) { arg = version; } else { OP *pack; SV *meth; if (version->op_type != OP_CONST || !SvNIOKp(vesv)) Perl_croak(aTHX_ "Version number must be a constant number"); /* Make copy of idop so we don't free it twice */ pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); /* Fake up a method call to VERSION */ meth = newSVpvs_share("VERSION"); veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, op_append_elem(OP_LIST, op_prepend_elem(OP_LIST, pack, list(version)), newSVOP(OP_METHOD_NAMED, 0, meth))); } } /* Fake up an import/unimport */ if (arg && arg->op_type == OP_STUB) { if (PL_madskills) op_getmad(arg,pegop,'S'); imop = arg; /* no import on explicit () */ } else if (SvNIOKp(((SVOP*)idop)->op_sv)) { imop = NULL; /* use 5.0; */ if (aver) use_version = ((SVOP*)idop)->op_sv; else idop->op_private |= OPpCONST_NOVER; } else { SV *meth; if (PL_madskills) op_getmad(arg,pegop,'A'); /* Make copy of idop so we don't free it twice */ pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv)); /* Fake up a method call to import/unimport */ meth = aver ? newSVpvs_share("import") : newSVpvs_share("unimport"); imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, op_append_elem(OP_LIST, op_prepend_elem(OP_LIST, pack, list(arg)), newSVOP(OP_METHOD_NAMED, 0, meth))); } /* Fake up the BEGIN {}, which does its thing immediately. */ newATTRSUB(floor, newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")), NULL, NULL, op_append_elem(OP_LINESEQ, op_append_elem(OP_LINESEQ, newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)), newSTATEOP(0, NULL, veop)), newSTATEOP(0, NULL, imop) )); if (use_version) { /* If we request a version >= 5.9.5, load feature.pm with the * feature bundle that corresponds to the required version. */ use_version = sv_2mortal(new_version(use_version)); if (vcmp(use_version, sv_2mortal(upg_version(newSVnv(5.009005), FALSE))) >= 0) { SV *const importsv = vnormal(use_version); *SvPVX_mutable(importsv) = ':'; ENTER_with_name("load_feature"); Perl_load_module(aTHX_ 0, newSVpvs("feature"), NULL, importsv, NULL); LEAVE_with_name("load_feature"); } /* If a version >= 5.11.0 is requested, strictures are on by default! */ if (vcmp(use_version, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) { PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS); } } /* The "did you use incorrect case?" warning used to be here. * The problem is that on case-insensitive filesystems one * might get false positives for "use" (and "require"): * "use Strict" or "require CARP" will work. This causes * portability problems for the script: in case-strict * filesystems the script will stop working. * * The "incorrect case" warning checked whether "use Foo" * imported "Foo" to your namespace, but that is wrong, too: * there is no requirement nor promise in the language that * a Foo.pm should or would contain anything in package "Foo". * * There is very little Configure-wise that can be done, either: * the case-sensitivity of the build filesystem of Perl does not * help in guessing the case-sensitivity of the runtime environment. */ PL_hints |= HINT_BLOCK_SCOPE; PL_parser->copline = NOLINE; PL_parser->expect = XSTATE; PL_cop_seqmax++; /* Purely for B::*'s benefit */ #ifdef PERL_MAD if (!PL_madskills) { /* FIXME - don't allocate pegop if !PL_madskills */ op_free(pegop); return NULL; } return pegop; #endif } /* =head1 Embedding Functions =for apidoc load_module Loads the module whose name is pointed to by the string part of name. Note that the actual module name, not its filename, should be given. Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS (or 0 for no flags). ver, if specified, provides version semantics similar to C. The optional trailing SV* arguments can be used to specify arguments to the module's import() method, similar to C. They must be terminated with a final NULL pointer. Note that this list can only be omitted when the PERL_LOADMOD_NOIMPORT flag has been used. Otherwise at least a single NULL pointer to designate the default import list is required. =cut */ void Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...) { va_list args; PERL_ARGS_ASSERT_LOAD_MODULE; va_start(args, ver); vload_module(flags, name, ver, &args); va_end(args); } #ifdef PERL_IMPLICIT_CONTEXT void Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...) { dTHX; va_list args; PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT; va_start(args, ver); vload_module(flags, name, ver, &args); va_end(args); } #endif void Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args) { dVAR; OP *veop, *imop; OP * const modname = newSVOP(OP_CONST, 0, name); PERL_ARGS_ASSERT_VLOAD_MODULE; modname->op_private |= OPpCONST_BARE; if (ver) { veop = newSVOP(OP_CONST, 0, ver); } else veop = NULL; if (flags & PERL_LOADMOD_NOIMPORT) { imop = sawparens(newNULLLIST()); } else if (flags & PERL_LOADMOD_IMPORT_OPS) { imop = va_arg(*args, OP*); } else { SV *sv; imop = NULL; sv = va_arg(*args, SV*); while (sv) { imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv)); sv = va_arg(*args, SV*); } } /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure * that it has a PL_parser to play with while doing that, and also * that it doesn't mess with any existing parser, by creating a tmp * new parser with lex_start(). This won't actually be used for much, * since pp_require() will create another parser for the real work. */ ENTER; SAVEVPTR(PL_curcop); lex_start(NULL, NULL, 0); utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0), veop, modname, imop); LEAVE; } OP * Perl_dofile(pTHX_ OP *term, I32 force_builtin) { dVAR; OP *doop; GV *gv = NULL; PERL_ARGS_ASSERT_DOFILE; if (!force_builtin) { gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV); if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) { GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE); gv = gvp ? *gvp : NULL; } } if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) { doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, term, scalar(newUNOP(OP_RV2CV, 0, newGVOP(OP_GV, 0, gv)))))); } else { doop = newUNOP(OP_DOFILE, 0, scalar(term)); } return doop; } /* =head1 Optree construction =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval Constructs, checks, and returns an C (list slice) op. I gives the eight bits of C, except that C will be set automatically, and, shifted up eight bits, the eight bits of C, except that the bit with value 1 or 2 is automatically set as required. I and I supply the parameters of the slice; they are consumed by this function and become part of the constructed op tree. =cut */ OP * Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval) { return newBINOP(OP_LSLICE, flags, list(force_list(subscript)), list(force_list(listval)) ); } STATIC I32 S_is_list_assignment(pTHX_ register const OP *o) { unsigned type; U8 flags; if (!o) return TRUE; if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS)) o = cUNOPo->op_first; flags = o->op_flags; type = o->op_type; if (type == OP_COND_EXPR) { const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling); const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling); if (t && f) return TRUE; if (t || f) yyerror("Assignment to both a list and a scalar"); return FALSE; } if (type == OP_LIST && (flags & OPf_WANT) == OPf_WANT_SCALAR && o->op_private & OPpLVAL_INTRO) return FALSE; if (type == OP_LIST || flags & OPf_PARENS || type == OP_RV2AV || type == OP_RV2HV || type == OP_ASLICE || type == OP_HSLICE) return TRUE; if (type == OP_PADAV || type == OP_PADHV) return TRUE; if (type == OP_RV2SV) return FALSE; return FALSE; } /* =for apidoc Am|OP *|newASSIGNOP|I32 flags|OP *left|I32 optype|OP *right Constructs, checks, and returns an assignment op. I and I supply the parameters of the assignment; they are consumed by this function and become part of the constructed op tree. If I is C, C, or C, then a suitable conditional optree is constructed. If I is the opcode of a binary operator, such as C, then an op is constructed that performs the binary operation and assigns the result to the left argument. Either way, if I is non-zero then I has no effect. If I is zero, then a plain scalar or list assignment is constructed. Which type of assignment it is is automatically determined. I gives the eight bits of C, except that C will be set automatically, and, shifted up eight bits, the eight bits of C, except that the bit with value 1 or 2 is automatically set as required. =cut */ OP * Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right) { dVAR; OP *o; if (optype) { if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) { return newLOGOP(optype, 0, op_lvalue(scalar(left), optype), newUNOP(OP_SASSIGN, 0, scalar(right))); } else { return newBINOP(optype, OPf_STACKED, op_lvalue(scalar(left), optype), scalar(right)); } } if (is_list_assignment(left)) { static const char no_list_state[] = "Initialization of state variables" " in list context currently forbidden"; OP *curop; bool maybe_common_vars = TRUE; PL_modcount = 0; /* Grandfathering $[ assignment here. Bletch.*/ /* Only simple assignments like C<< ($[) = 1 >> are allowed */ PL_eval_start = (left->op_type == OP_CONST) ? right : NULL; left = op_lvalue(left, OP_AASSIGN); if (PL_eval_start) PL_eval_start = 0; else if (left->op_type == OP_CONST) { deprecate("assignment to $["); /* FIXME for MAD */ /* Result of assignment is always 1 (or we'd be dead already) */ return newSVOP(OP_CONST, 0, newSViv(1)); } curop = list(force_list(left)); o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop); o->op_private = (U8)(0 | (flags >> 8)); if ((left->op_type == OP_LIST || (left->op_type == OP_NULL && left->op_targ == OP_LIST))) { OP* lop = ((LISTOP*)left)->op_first; maybe_common_vars = FALSE; while (lop) { if (lop->op_type == OP_PADSV || lop->op_type == OP_PADAV || lop->op_type == OP_PADHV || lop->op_type == OP_PADANY) { if (!(lop->op_private & OPpLVAL_INTRO)) maybe_common_vars = TRUE; if (lop->op_private & OPpPAD_STATE) { if (left->op_private & OPpLVAL_INTRO) { /* Each variable in state($a, $b, $c) = ... */ } else { /* Each state variable in (state $a, my $b, our $c, $d, undef) = ... */ } yyerror(no_list_state); } else { /* Each my variable in (state $a, my $b, our $c, $d, undef) = ... */ } } else if (lop->op_type == OP_UNDEF || lop->op_type == OP_PUSHMARK) { /* undef may be interesting in (state $a, undef, state $c) */ } else { /* Other ops in the list. */ maybe_common_vars = TRUE; } lop = lop->op_sibling; } } else if ((left->op_private & OPpLVAL_INTRO) && ( left->op_type == OP_PADSV || left->op_type == OP_PADAV || left->op_type == OP_PADHV || left->op_type == OP_PADANY)) { if (left->op_type == OP_PADSV) maybe_common_vars = FALSE; if (left->op_private & OPpPAD_STATE) { /* All single variable list context state assignments, hence state ($a) = ... (state $a) = ... state @a = ... state (@a) = ... (state @a) = ... state %a = ... state (%a) = ... (state %a) = ... */ yyerror(no_list_state); } } /* PL_generation sorcery: * an assignment like ($a,$b) = ($c,$d) is easier than * ($a,$b) = ($c,$a), since there is no need for temporary vars. * To detect whether there are common vars, the global var * PL_generation is incremented for each assign op we compile. * Then, while compiling the assign op, we run through all the * variables on both sides of the assignment, setting a spare slot * in each of them to PL_generation. If any of them already have * that value, we know we've got commonality. We could use a * single bit marker, but then we'd have to make 2 passes, first * to clear the flag, then to test and set it. To find somewhere * to store these values, evil chicanery is done with SvUVX(). */ if (maybe_common_vars) { OP *lastop = o; PL_generation++; for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { if (PL_opargs[curop->op_type] & OA_DANGEROUS) { if (curop->op_type == OP_GV) { GV *gv = cGVOPx_gv(curop); if (gv == PL_defgv || (int)GvASSIGN_GENERATION(gv) == PL_generation) break; GvASSIGN_GENERATION_set(gv, PL_generation); } else if (curop->op_type == OP_PADSV || curop->op_type == OP_PADAV || curop->op_type == OP_PADHV || curop->op_type == OP_PADANY) { if (PAD_COMPNAME_GEN(curop->op_targ) == (STRLEN)PL_generation) break; PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation); } else if (curop->op_type == OP_RV2CV) break; else if (curop->op_type == OP_RV2SV || curop->op_type == OP_RV2AV || curop->op_type == OP_RV2HV || curop->op_type == OP_RV2GV) { if (lastop->op_type != OP_GV) /* funny deref? */ break; } else if (curop->op_type == OP_PUSHRE) { #ifdef USE_ITHREADS if (((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff) { GV *const gv = MUTABLE_GV(PAD_SVl(((PMOP*)curop)->op_pmreplrootu.op_pmtargetoff)); if (gv == PL_defgv || (int)GvASSIGN_GENERATION(gv) == PL_generation) break; GvASSIGN_GENERATION_set(gv, PL_generation); } #else GV *const gv = ((PMOP*)curop)->op_pmreplrootu.op_pmtargetgv; if (gv) { if (gv == PL_defgv || (int)GvASSIGN_GENERATION(gv) == PL_generation) break; GvASSIGN_GENERATION_set(gv, PL_generation); } #endif } else break; } lastop = curop; } if (curop != o) o->op_private |= OPpASSIGN_COMMON; } if (right && right->op_type == OP_SPLIT && !PL_madskills) { OP* tmpop = ((LISTOP*)right)->op_first; if (tmpop && (tmpop->op_type == OP_PUSHRE)) { PMOP * const pm = (PMOP*)tmpop; if (left->op_type == OP_RV2AV && !(left->op_private & OPpLVAL_INTRO) && !(o->op_private & OPpASSIGN_COMMON) ) { tmpop = ((UNOP*)left)->op_first; if (tmpop->op_type == OP_GV #ifdef USE_ITHREADS && !pm->op_pmreplrootu.op_pmtargetoff #else && !pm->op_pmreplrootu.op_pmtargetgv #endif ) { #ifdef USE_ITHREADS pm->op_pmreplrootu.op_pmtargetoff = cPADOPx(tmpop)->op_padix; cPADOPx(tmpop)->op_padix = 0; /* steal it */ #else pm->op_pmreplrootu.op_pmtargetgv = MUTABLE_GV(cSVOPx(tmpop)->op_sv); cSVOPx(tmpop)->op_sv = NULL; /* steal it */ #endif pm->op_pmflags |= PMf_ONCE; tmpop = cUNOPo->op_first; /* to list (nulled) */ tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ tmpop->op_sibling = NULL; /* don't free split */ right->op_next = tmpop->op_next; /* fix starting loc */ op_free(o); /* blow off assign */ right->op_flags &= ~OPf_WANT; /* "I don't know and I don't care." */ return right; } } else { if (PL_modcount < RETURN_UNLIMITED_NUMBER && ((LISTOP*)right)->op_last->op_type == OP_CONST) { SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv; if (SvIOK(sv) && SvIVX(sv) == 0) sv_setiv(sv, PL_modcount+1); } } } } return o; } if (!right) right = newOP(OP_UNDEF, 0); if (right->op_type == OP_READLINE) { right->op_flags |= OPf_STACKED; return newBINOP(OP_NULL, flags, op_lvalue(scalar(left), OP_SASSIGN), scalar(right)); } else { PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ o = newBINOP(OP_SASSIGN, flags, scalar(right), op_lvalue(scalar(left), OP_SASSIGN) ); if (PL_eval_start) PL_eval_start = 0; else { if (!PL_madskills) { /* assignment to $[ is ignored when making a mad dump */ deprecate("assignment to $["); op_free(o); o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling))); o->op_private |= OPpCONST_ARYBASE; } } } return o; } /* =for apidoc Am|OP *|newSTATEOP|I32 flags|char *label|OP *o Constructs a state op (COP). The state op is normally a C op, but will be a C op if debugging is enabled for currently-compiled code. The state op is populated from L (or L). If I