4 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
5 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
14 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
15 * youngest of the Old Took's daughters); and Mr. Drogo was his second
16 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
17 * either way, as the saying is, if you follow me.' --the Gaffer
19 * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22 /* This file contains the functions that create, manipulate and optimize
23 * the OP structures that hold a compiled perl program.
25 * A Perl program is compiled into a tree of OPs. Each op contains
26 * structural pointers (eg to its siblings and the next op in the
27 * execution sequence), a pointer to the function that would execute the
28 * op, plus any data specific to that op. For example, an OP_CONST op
29 * points to the pp_const() function and to an SV containing the constant
30 * value. When pp_const() is executed, its job is to push that SV onto the
33 * OPs are mainly created by the newFOO() functions, which are mainly
34 * called from the parser (in perly.y) as the code is parsed. For example
35 * the Perl code $a + $b * $c would cause the equivalent of the following
36 * to be called (oversimplifying a bit):
38 * newBINOP(OP_ADD, flags,
40 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
43 * Note that during the build of miniperl, a temporary copy of this file
44 * is made, called opmini.c.
48 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
52 An execution-order pass
54 The bottom-up pass is represented by all the "newOP" routines and
55 the ck_ routines. The bottom-upness is actually driven by yacc.
56 So at the point that a ck_ routine fires, we have no idea what the
57 context is, either upward in the syntax tree, or either forward or
58 backward in the execution order. (The bottom-up parser builds that
59 part of the execution order it knows about, but if you follow the "next"
60 links around, you'll find it's actually a closed loop through the
63 Whenever the bottom-up parser gets to a node that supplies context to
64 its components, it invokes that portion of the top-down pass that applies
65 to that part of the subtree (and marks the top node as processed, so
66 if a node further up supplies context, it doesn't have to take the
67 plunge again). As a particular subcase of this, as the new node is
68 built, it takes all the closed execution loops of its subcomponents
69 and links them into a new closed loop for the higher level node. But
70 it's still not the real execution order.
72 The actual execution order is not known till we get a grammar reduction
73 to a top-level unit like a subroutine or file that will be called by
74 "name" rather than via a "next" pointer. At that point, we can call
75 into peep() to do that code's portion of the 3rd pass. It has to be
76 recursive, but it's recursive on basic blocks, not on tree nodes.
79 /* To implement user lexical pragmas, there needs to be a way at run time to
80 get the compile time state of %^H for that block. Storing %^H in every
81 block (or even COP) would be very expensive, so a different approach is
82 taken. The (running) state of %^H is serialised into a tree of HE-like
83 structs. Stores into %^H are chained onto the current leaf as a struct
84 refcounted_he * with the key and the value. Deletes from %^H are saved
85 with a value of PL_sv_placeholder. The state of %^H at any point can be
86 turned back into a regular HV by walking back up the tree from that point's
87 leaf, ignoring any key you've already seen (placeholder or not), storing
88 the rest into the HV structure, then removing the placeholders. Hence
89 memory is only used to store the %^H deltas from the enclosing COP, rather
90 than the entire %^H on each COP.
92 To cause actions on %^H to write out the serialisation records, it has
93 magic type 'H'. This magic (itself) does nothing, but its presence causes
94 the values to gain magic type 'h', which has entries for set and clear.
95 C<Perl_magic_sethint> updates C<PL_compiling.cop_hints_hash> with a store
96 record, with deletes written by C<Perl_magic_clearhint>. C<SAVEHINTS>
97 saves the current C<PL_compiling.cop_hints_hash> on the save stack, so that
98 it will be correctly restored when any inner compiling scope is exited.
104 #include "keywords.h"
107 #define CALL_PEEP(o) PL_peepp(aTHX_ o)
108 #define CALL_RPEEP(o) PL_rpeepp(aTHX_ o)
109 #define CALL_OPFREEHOOK(o) if (PL_opfreehook) PL_opfreehook(aTHX_ o)
111 #if defined(PL_OP_SLAB_ALLOC)
113 #ifdef PERL_DEBUG_READONLY_OPS
114 # define PERL_SLAB_SIZE 4096
115 # include <sys/mman.h>
118 #ifndef PERL_SLAB_SIZE
119 #define PERL_SLAB_SIZE 2048
123 Perl_Slab_Alloc(pTHX_ size_t sz)
127 * To make incrementing use count easy PL_OpSlab is an I32 *
128 * To make inserting the link to slab PL_OpPtr is I32 **
129 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
130 * Add an overhead for pointer to slab and round up as a number of pointers
132 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
133 if ((PL_OpSpace -= sz) < 0) {
134 #ifdef PERL_DEBUG_READONLY_OPS
135 /* We need to allocate chunk by chunk so that we can control the VM
137 PL_OpPtr = (I32**) mmap(0, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE,
138 MAP_ANON|MAP_PRIVATE, -1, 0);
140 DEBUG_m(PerlIO_printf(Perl_debug_log, "mapped %lu at %p\n",
141 (unsigned long) PERL_SLAB_SIZE*sizeof(I32*),
143 if(PL_OpPtr == MAP_FAILED) {
144 perror("mmap failed");
149 PL_OpPtr = (I32 **) PerlMemShared_calloc(PERL_SLAB_SIZE,sizeof(I32*));
154 /* We reserve the 0'th I32 sized chunk as a use count */
155 PL_OpSlab = (I32 *) PL_OpPtr;
156 /* Reduce size by the use count word, and by the size we need.
157 * Latter is to mimic the '-=' in the if() above
159 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
160 /* Allocation pointer starts at the top.
161 Theory: because we build leaves before trunk allocating at end
162 means that at run time access is cache friendly upward
164 PL_OpPtr += PERL_SLAB_SIZE;
166 #ifdef PERL_DEBUG_READONLY_OPS
167 /* We remember this slab. */
168 /* This implementation isn't efficient, but it is simple. */
169 PL_slabs = (I32**) realloc(PL_slabs, sizeof(I32**) * (PL_slab_count + 1));
170 PL_slabs[PL_slab_count++] = PL_OpSlab;
171 DEBUG_m(PerlIO_printf(Perl_debug_log, "Allocate %p\n", PL_OpSlab));
174 assert( PL_OpSpace >= 0 );
175 /* Move the allocation pointer down */
177 assert( PL_OpPtr > (I32 **) PL_OpSlab );
178 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
179 (*PL_OpSlab)++; /* Increment use count of slab */
180 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
181 assert( *PL_OpSlab > 0 );
182 return (void *)(PL_OpPtr + 1);
185 #ifdef PERL_DEBUG_READONLY_OPS
187 Perl_pending_Slabs_to_ro(pTHX) {
188 /* Turn all the allocated op slabs read only. */
189 U32 count = PL_slab_count;
190 I32 **const slabs = PL_slabs;
192 /* Reset the array of pending OP slabs, as we're about to turn this lot
193 read only. Also, do it ahead of the loop in case the warn triggers,
194 and a warn handler has an eval */
199 /* Force a new slab for any further allocation. */
203 void *const start = slabs[count];
204 const size_t size = PERL_SLAB_SIZE* sizeof(I32*);
205 if(mprotect(start, size, PROT_READ)) {
206 Perl_warn(aTHX_ "mprotect for %p %lu failed with %d",
207 start, (unsigned long) size, errno);
215 S_Slab_to_rw(pTHX_ void *op)
217 I32 * const * const ptr = (I32 **) op;
218 I32 * const slab = ptr[-1];
220 PERL_ARGS_ASSERT_SLAB_TO_RW;
222 assert( ptr-1 > (I32 **) slab );
223 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
225 if(mprotect(slab, PERL_SLAB_SIZE*sizeof(I32*), PROT_READ|PROT_WRITE)) {
226 Perl_warn(aTHX_ "mprotect RW for %p %lu failed with %d",
227 slab, (unsigned long) PERL_SLAB_SIZE*sizeof(I32*), errno);
232 Perl_op_refcnt_inc(pTHX_ OP *o)
243 Perl_op_refcnt_dec(pTHX_ OP *o)
245 PERL_ARGS_ASSERT_OP_REFCNT_DEC;
250 # define Slab_to_rw(op)
254 Perl_Slab_Free(pTHX_ void *op)
256 I32 * const * const ptr = (I32 **) op;
257 I32 * const slab = ptr[-1];
258 PERL_ARGS_ASSERT_SLAB_FREE;
259 assert( ptr-1 > (I32 **) slab );
260 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
263 if (--(*slab) == 0) {
265 # define PerlMemShared PerlMem
268 #ifdef PERL_DEBUG_READONLY_OPS
269 U32 count = PL_slab_count;
270 /* Need to remove this slab from our list of slabs */
273 if (PL_slabs[count] == slab) {
275 /* Found it. Move the entry at the end to overwrite it. */
276 DEBUG_m(PerlIO_printf(Perl_debug_log,
277 "Deallocate %p by moving %p from %lu to %lu\n",
279 PL_slabs[PL_slab_count - 1],
280 PL_slab_count, count));
281 PL_slabs[count] = PL_slabs[--PL_slab_count];
282 /* Could realloc smaller at this point, but probably not
284 if(munmap(slab, PERL_SLAB_SIZE*sizeof(I32*))) {
285 perror("munmap failed");
293 PerlMemShared_free(slab);
295 if (slab == PL_OpSlab) {
302 * In the following definition, the ", (OP*)0" is just to make the compiler
303 * think the expression is of the right type: croak actually does a Siglongjmp.
305 #define CHECKOP(type,o) \
306 ((PL_op_mask && PL_op_mask[type]) \
307 ? ( op_free((OP*)o), \
308 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
310 : PL_check[type](aTHX_ (OP*)o))
312 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
314 #define CHANGE_TYPE(o,type) \
316 o->op_type = (OPCODE)type; \
317 o->op_ppaddr = PL_ppaddr[type]; \
321 S_gv_ename(pTHX_ GV *gv)
323 SV* const tmpsv = sv_newmortal();
325 PERL_ARGS_ASSERT_GV_ENAME;
327 gv_efullname3(tmpsv, gv, NULL);
328 return SvPV_nolen_const(tmpsv);
332 S_no_fh_allowed(pTHX_ OP *o)
334 PERL_ARGS_ASSERT_NO_FH_ALLOWED;
336 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
342 S_too_few_arguments(pTHX_ OP *o, const char *name)
344 PERL_ARGS_ASSERT_TOO_FEW_ARGUMENTS;
346 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
351 S_too_many_arguments(pTHX_ OP *o, const char *name)
353 PERL_ARGS_ASSERT_TOO_MANY_ARGUMENTS;
355 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
360 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
362 PERL_ARGS_ASSERT_BAD_TYPE;
364 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
365 (int)n, name, t, OP_DESC(kid)));
369 S_no_bareword_allowed(pTHX_ OP *o)
371 PERL_ARGS_ASSERT_NO_BAREWORD_ALLOWED;
374 return; /* various ok barewords are hidden in extra OP_NULL */
375 qerror(Perl_mess(aTHX_
376 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
378 o->op_private &= ~OPpCONST_STRICT; /* prevent warning twice about the same OP */
381 /* "register" allocation */
384 Perl_allocmy(pTHX_ const char *const name, const STRLEN len, const U32 flags)
388 const bool is_our = (PL_parser->in_my == KEY_our);
390 PERL_ARGS_ASSERT_ALLOCMY;
392 if (flags & ~SVf_UTF8)
393 Perl_croak(aTHX_ "panic: allocmy illegal flag bits 0x%" UVxf,
396 /* Until we're using the length for real, cross check that we're being
398 assert(strlen(name) == len);
400 /* complain about "my $<special_var>" etc etc */
404 ((flags & SVf_UTF8) && UTF8_IS_START(name[1])) ||
405 (name[1] == '_' && (*name == '$' || len > 2))))
407 /* name[2] is true if strlen(name) > 2 */
408 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
409 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%.*s in \"%s\"",
410 name[0], toCTRL(name[1]), (int)(len - 2), name + 2,
411 PL_parser->in_my == KEY_state ? "state" : "my"));
413 yyerror(Perl_form(aTHX_ "Can't use global %.*s in \"%s\"", (int) len, name,
414 PL_parser->in_my == KEY_state ? "state" : "my"));
418 /* allocate a spare slot and store the name in that slot */
420 off = pad_add_name_pvn(name, len,
421 (is_our ? padadd_OUR :
422 PL_parser->in_my == KEY_state ? padadd_STATE : 0)
423 | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ),
424 PL_parser->in_my_stash,
426 /* $_ is always in main::, even with our */
427 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
431 /* anon sub prototypes contains state vars should always be cloned,
432 * otherwise the state var would be shared between anon subs */
434 if (PL_parser->in_my == KEY_state && CvANON(PL_compcv))
435 CvCLONE_on(PL_compcv);
440 /* free the body of an op without examining its contents.
441 * Always use this rather than FreeOp directly */
444 S_op_destroy(pTHX_ OP *o)
446 if (o->op_latefree) {
454 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a,b)
456 # define forget_pmop(a,b) S_forget_pmop(aTHX_ a)
462 Perl_op_free(pTHX_ OP *o)
469 if (o->op_latefreed) {
476 if (o->op_private & OPpREFCOUNTED) {
487 refcnt = OpREFCNT_dec(o);
490 /* Need to find and remove any pattern match ops from the list
491 we maintain for reset(). */
492 find_and_forget_pmops(o);
502 /* Call the op_free hook if it has been set. Do it now so that it's called
503 * at the right time for refcounted ops, but still before all of the kids
507 if (o->op_flags & OPf_KIDS) {
508 register OP *kid, *nextkid;
509 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
510 nextkid = kid->op_sibling; /* Get before next freeing kid */
515 #ifdef PERL_DEBUG_READONLY_OPS
519 /* COP* is not cleared by op_clear() so that we may track line
520 * numbers etc even after null() */
521 if (type == OP_NEXTSTATE || type == OP_DBSTATE
522 || (type == OP_NULL /* the COP might have been null'ed */
523 && ((OPCODE)o->op_targ == OP_NEXTSTATE
524 || (OPCODE)o->op_targ == OP_DBSTATE))) {
529 type = (OPCODE)o->op_targ;
532 if (o->op_latefree) {
538 #ifdef DEBUG_LEAKING_SCALARS
545 Perl_op_clear(pTHX_ OP *o)
550 PERL_ARGS_ASSERT_OP_CLEAR;
553 mad_free(o->op_madprop);
558 switch (o->op_type) {
559 case OP_NULL: /* Was holding old type, if any. */
560 if (PL_madskills && o->op_targ != OP_NULL) {
561 o->op_type = (Optype)o->op_targ;
566 case OP_ENTEREVAL: /* Was holding hints. */
570 if (!(o->op_flags & OPf_REF)
571 || (PL_check[o->op_type] != Perl_ck_ftst))
578 GV *gv = (o->op_type == OP_GV || o->op_type == OP_GVSV)
583 /* It's possible during global destruction that the GV is freed
584 before the optree. Whilst the SvREFCNT_inc is happy to bump from
585 0 to 1 on a freed SV, the corresponding SvREFCNT_dec from 1 to 0
586 will trigger an assertion failure, because the entry to sv_clear
587 checks that the scalar is not already freed. A check of for
588 !SvIS_FREED(gv) turns out to be invalid, because during global
589 destruction the reference count can be forced down to zero
590 (with SVf_BREAK set). In which case raising to 1 and then
591 dropping to 0 triggers cleanup before it should happen. I
592 *think* that this might actually be a general, systematic,
593 weakness of the whole idea of SVf_BREAK, in that code *is*
594 allowed to raise and lower references during global destruction,
595 so any *valid* code that happens to do this during global
596 destruction might well trigger premature cleanup. */
597 bool still_valid = gv && SvREFCNT(gv);
600 SvREFCNT_inc_simple_void(gv);
602 if (cPADOPo->op_padix > 0) {
603 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
604 * may still exist on the pad */
605 pad_swipe(cPADOPo->op_padix, TRUE);
606 cPADOPo->op_padix = 0;
609 SvREFCNT_dec(cSVOPo->op_sv);
610 cSVOPo->op_sv = NULL;
613 int try_downgrade = SvREFCNT(gv) == 2;
616 gv_try_downgrade(gv);
620 case OP_METHOD_NAMED:
623 SvREFCNT_dec(cSVOPo->op_sv);
624 cSVOPo->op_sv = NULL;
627 Even if op_clear does a pad_free for the target of the op,
628 pad_free doesn't actually remove the sv that exists in the pad;
629 instead it lives on. This results in that it could be reused as
630 a target later on when the pad was reallocated.
633 pad_swipe(o->op_targ,1);
642 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
647 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
649 if (cPADOPo->op_padix > 0) {
650 pad_swipe(cPADOPo->op_padix, TRUE);
651 cPADOPo->op_padix = 0;
654 SvREFCNT_dec(cSVOPo->op_sv);
655 cSVOPo->op_sv = NULL;
659 PerlMemShared_free(cPVOPo->op_pv);
660 cPVOPo->op_pv = NULL;
664 op_free(cPMOPo->op_pmreplrootu.op_pmreplroot);
668 if (cPMOPo->op_pmreplrootu.op_pmtargetoff) {
669 /* No GvIN_PAD_off here, because other references may still
670 * exist on the pad */
671 pad_swipe(cPMOPo->op_pmreplrootu.op_pmtargetoff, TRUE);
674 SvREFCNT_dec(MUTABLE_SV(cPMOPo->op_pmreplrootu.op_pmtargetgv));
680 forget_pmop(cPMOPo, 1);
681 cPMOPo->op_pmreplrootu.op_pmreplroot = NULL;
682 /* we use the same protection as the "SAFE" version of the PM_ macros
683 * here since sv_clean_all might release some PMOPs
684 * after PL_regex_padav has been cleared
685 * and the clearing of PL_regex_padav needs to
686 * happen before sv_clean_all
689 if(PL_regex_pad) { /* We could be in destruction */
690 const IV offset = (cPMOPo)->op_pmoffset;
691 ReREFCNT_dec(PM_GETRE(cPMOPo));
692 PL_regex_pad[offset] = &PL_sv_undef;
693 sv_catpvn_nomg(PL_regex_pad[0], (const char *)&offset,
697 ReREFCNT_dec(PM_GETRE(cPMOPo));
698 PM_SETRE(cPMOPo, NULL);
704 if (o->op_targ > 0) {
705 pad_free(o->op_targ);
711 S_cop_free(pTHX_ COP* cop)
713 PERL_ARGS_ASSERT_COP_FREE;
717 if (! specialWARN(cop->cop_warnings))
718 PerlMemShared_free(cop->cop_warnings);
719 cophh_free(CopHINTHASH_get(cop));
723 S_forget_pmop(pTHX_ PMOP *const o
729 HV * const pmstash = PmopSTASH(o);
731 PERL_ARGS_ASSERT_FORGET_PMOP;
733 if (pmstash && !SvIS_FREED(pmstash)) {
734 MAGIC * const mg = mg_find((const SV *)pmstash, PERL_MAGIC_symtab);
736 PMOP **const array = (PMOP**) mg->mg_ptr;
737 U32 count = mg->mg_len / sizeof(PMOP**);
742 /* Found it. Move the entry at the end to overwrite it. */
743 array[i] = array[--count];
744 mg->mg_len = count * sizeof(PMOP**);
745 /* Could realloc smaller at this point always, but probably
746 not worth it. Probably worth free()ing if we're the
749 Safefree(mg->mg_ptr);
766 S_find_and_forget_pmops(pTHX_ OP *o)
768 PERL_ARGS_ASSERT_FIND_AND_FORGET_PMOPS;
770 if (o->op_flags & OPf_KIDS) {
771 OP *kid = cUNOPo->op_first;
773 switch (kid->op_type) {
778 forget_pmop((PMOP*)kid, 0);
780 find_and_forget_pmops(kid);
781 kid = kid->op_sibling;
787 Perl_op_null(pTHX_ OP *o)
791 PERL_ARGS_ASSERT_OP_NULL;
793 if (o->op_type == OP_NULL)
797 o->op_targ = o->op_type;
798 o->op_type = OP_NULL;
799 o->op_ppaddr = PL_ppaddr[OP_NULL];
803 Perl_op_refcnt_lock(pTHX)
811 Perl_op_refcnt_unlock(pTHX)
818 /* Contextualizers */
821 =for apidoc Am|OP *|op_contextualize|OP *o|I32 context
823 Applies a syntactic context to an op tree representing an expression.
824 I<o> is the op tree, and I<context> must be C<G_SCALAR>, C<G_ARRAY>,
825 or C<G_VOID> to specify the context to apply. The modified op tree
832 Perl_op_contextualize(pTHX_ OP *o, I32 context)
834 PERL_ARGS_ASSERT_OP_CONTEXTUALIZE;
836 case G_SCALAR: return scalar(o);
837 case G_ARRAY: return list(o);
838 case G_VOID: return scalarvoid(o);
840 Perl_croak(aTHX_ "panic: op_contextualize bad context %ld",
847 =head1 Optree Manipulation Functions
849 =for apidoc Am|OP*|op_linklist|OP *o
850 This function is the implementation of the L</LINKLIST> macro. It should
851 not be called directly.
857 Perl_op_linklist(pTHX_ OP *o)
861 PERL_ARGS_ASSERT_OP_LINKLIST;
866 /* establish postfix order */
867 first = cUNOPo->op_first;
870 o->op_next = LINKLIST(first);
873 if (kid->op_sibling) {
874 kid->op_next = LINKLIST(kid->op_sibling);
875 kid = kid->op_sibling;
889 S_scalarkids(pTHX_ OP *o)
891 if (o && o->op_flags & OPf_KIDS) {
893 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
900 S_scalarboolean(pTHX_ OP *o)
904 PERL_ARGS_ASSERT_SCALARBOOLEAN;
906 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST
907 && !(cBINOPo->op_first->op_flags & OPf_SPECIAL)) {
908 if (ckWARN(WARN_SYNTAX)) {
909 const line_t oldline = CopLINE(PL_curcop);
911 if (PL_parser && PL_parser->copline != NOLINE)
912 CopLINE_set(PL_curcop, PL_parser->copline);
913 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
914 CopLINE_set(PL_curcop, oldline);
921 Perl_scalar(pTHX_ OP *o)
926 /* assumes no premature commitment */
927 if (!o || (PL_parser && PL_parser->error_count)
928 || (o->op_flags & OPf_WANT)
929 || o->op_type == OP_RETURN)
934 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
936 switch (o->op_type) {
938 scalar(cBINOPo->op_first);
943 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
953 if (o->op_flags & OPf_KIDS) {
954 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
960 kid = cLISTOPo->op_first;
962 kid = kid->op_sibling;
965 OP *sib = kid->op_sibling;
966 if (sib && kid->op_type != OP_LEAVEWHEN)
972 PL_curcop = &PL_compiling;
977 kid = cLISTOPo->op_first;
980 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
987 Perl_scalarvoid(pTHX_ OP *o)
991 const char* useless = NULL;
992 U32 useless_is_utf8 = 0;
996 PERL_ARGS_ASSERT_SCALARVOID;
998 /* trailing mad null ops don't count as "there" for void processing */
1000 o->op_type != OP_NULL &&
1002 o->op_sibling->op_type == OP_NULL)
1005 for (sib = o->op_sibling;
1006 sib && sib->op_type == OP_NULL;
1007 sib = sib->op_sibling) ;
1013 if (o->op_type == OP_NEXTSTATE
1014 || o->op_type == OP_DBSTATE
1015 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
1016 || o->op_targ == OP_DBSTATE)))
1017 PL_curcop = (COP*)o; /* for warning below */
1019 /* assumes no premature commitment */
1020 want = o->op_flags & OPf_WANT;
1021 if ((want && want != OPf_WANT_SCALAR)
1022 || (PL_parser && PL_parser->error_count)
1023 || o->op_type == OP_RETURN || o->op_type == OP_REQUIRE || o->op_type == OP_LEAVEWHEN)
1028 if ((o->op_private & OPpTARGET_MY)
1029 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1031 return scalar(o); /* As if inside SASSIGN */
1034 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
1036 switch (o->op_type) {
1038 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
1042 if (o->op_flags & OPf_STACKED)
1046 if (o->op_private == 4)
1071 case OP_AELEMFAST_LEX:
1090 case OP_GETSOCKNAME:
1091 case OP_GETPEERNAME:
1096 case OP_GETPRIORITY:
1121 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
1122 /* Otherwise it's "Useless use of grep iterator" */
1123 useless = OP_DESC(o);
1127 kid = cLISTOPo->op_first;
1128 if (kid && kid->op_type == OP_PUSHRE
1130 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetoff)
1132 && !((PMOP*)kid)->op_pmreplrootu.op_pmtargetgv)
1134 useless = OP_DESC(o);
1138 kid = cUNOPo->op_first;
1139 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
1140 kid->op_type != OP_TRANS && kid->op_type != OP_TRANSR) {
1143 useless = "negative pattern binding (!~)";
1147 if (cPMOPo->op_pmflags & PMf_NONDESTRUCT)
1148 useless = "non-destructive substitution (s///r)";
1152 useless = "non-destructive transliteration (tr///r)";
1159 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
1160 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
1161 useless = "a variable";
1166 if (cSVOPo->op_private & OPpCONST_STRICT)
1167 no_bareword_allowed(o);
1169 if (ckWARN(WARN_VOID)) {
1170 /* don't warn on optimised away booleans, eg
1171 * use constant Foo, 5; Foo || print; */
1172 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
1174 /* the constants 0 and 1 are permitted as they are
1175 conventionally used as dummies in constructs like
1176 1 while some_condition_with_side_effects; */
1177 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1179 else if (SvPOK(sv)) {
1180 /* perl4's way of mixing documentation and code
1181 (before the invention of POD) was based on a
1182 trick to mix nroff and perl code. The trick was
1183 built upon these three nroff macros being used in
1184 void context. The pink camel has the details in
1185 the script wrapman near page 319. */
1186 const char * const maybe_macro = SvPVX_const(sv);
1187 if (strnEQ(maybe_macro, "di", 2) ||
1188 strnEQ(maybe_macro, "ds", 2) ||
1189 strnEQ(maybe_macro, "ig", 2))
1192 SV * const dsv = newSVpvs("");
1193 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1195 pv_pretty(dsv, maybe_macro, SvCUR(sv), 32, NULL, NULL,
1196 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_NOCLEAR | PERL_PV_ESCAPE_UNI_DETECT )));
1198 useless = SvPV_nolen(msv);
1199 useless_is_utf8 = SvUTF8(msv);
1202 else if (SvOK(sv)) {
1203 SV* msv = sv_2mortal(Perl_newSVpvf(aTHX_
1204 "a constant (%"SVf")", sv));
1205 useless = SvPV_nolen(msv);
1208 useless = "a constant (undef)";
1211 op_null(o); /* don't execute or even remember it */
1215 o->op_type = OP_PREINC; /* pre-increment is faster */
1216 o->op_ppaddr = PL_ppaddr[OP_PREINC];
1220 o->op_type = OP_PREDEC; /* pre-decrement is faster */
1221 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
1225 o->op_type = OP_I_PREINC; /* pre-increment is faster */
1226 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
1230 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
1231 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
1236 UNOP *refgen, *rv2cv;
1239 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
1242 rv2gv = ((BINOP *)o)->op_last;
1243 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
1246 refgen = (UNOP *)((BINOP *)o)->op_first;
1248 if (!refgen || refgen->op_type != OP_REFGEN)
1251 exlist = (LISTOP *)refgen->op_first;
1252 if (!exlist || exlist->op_type != OP_NULL
1253 || exlist->op_targ != OP_LIST)
1256 if (exlist->op_first->op_type != OP_PUSHMARK)
1259 rv2cv = (UNOP*)exlist->op_last;
1261 if (rv2cv->op_type != OP_RV2CV)
1264 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
1265 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
1266 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
1268 o->op_private |= OPpASSIGN_CV_TO_GV;
1269 rv2gv->op_private |= OPpDONT_INIT_GV;
1270 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
1282 kid = cLOGOPo->op_first;
1283 if (kid->op_type == OP_NOT
1284 && (kid->op_flags & OPf_KIDS)
1286 if (o->op_type == OP_AND) {
1288 o->op_ppaddr = PL_ppaddr[OP_OR];
1290 o->op_type = OP_AND;
1291 o->op_ppaddr = PL_ppaddr[OP_AND];
1300 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1305 if (o->op_flags & OPf_STACKED)
1312 if (!(o->op_flags & OPf_KIDS))
1323 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1333 Perl_ck_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %"SVf" in void context",
1334 newSVpvn_flags(useless, strlen(useless),
1335 SVs_TEMP | ( useless_is_utf8 ? SVf_UTF8 : 0 )));
1340 S_listkids(pTHX_ OP *o)
1342 if (o && o->op_flags & OPf_KIDS) {
1344 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1351 Perl_list(pTHX_ OP *o)
1356 /* assumes no premature commitment */
1357 if (!o || (o->op_flags & OPf_WANT)
1358 || (PL_parser && PL_parser->error_count)
1359 || o->op_type == OP_RETURN)
1364 if ((o->op_private & OPpTARGET_MY)
1365 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1367 return o; /* As if inside SASSIGN */
1370 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
1372 switch (o->op_type) {
1375 list(cBINOPo->op_first);
1380 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1388 if (!(o->op_flags & OPf_KIDS))
1390 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1391 list(cBINOPo->op_first);
1392 return gen_constant_list(o);
1399 kid = cLISTOPo->op_first;
1401 kid = kid->op_sibling;
1404 OP *sib = kid->op_sibling;
1405 if (sib && kid->op_type != OP_LEAVEWHEN)
1411 PL_curcop = &PL_compiling;
1415 kid = cLISTOPo->op_first;
1422 S_scalarseq(pTHX_ OP *o)
1426 const OPCODE type = o->op_type;
1428 if (type == OP_LINESEQ || type == OP_SCOPE ||
1429 type == OP_LEAVE || type == OP_LEAVETRY)
1432 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1433 if (kid->op_sibling) {
1437 PL_curcop = &PL_compiling;
1439 o->op_flags &= ~OPf_PARENS;
1440 if (PL_hints & HINT_BLOCK_SCOPE)
1441 o->op_flags |= OPf_PARENS;
1444 o = newOP(OP_STUB, 0);
1449 S_modkids(pTHX_ OP *o, I32 type)
1451 if (o && o->op_flags & OPf_KIDS) {
1453 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1454 op_lvalue(kid, type);
1460 =for apidoc finalize_optree
1462 This function finalizes the optree. Should be called directly after
1463 the complete optree is built. It does some additional
1464 checking which can't be done in the normal ck_xxx functions and makes
1465 the tree thread-safe.
1470 Perl_finalize_optree(pTHX_ OP* o)
1472 PERL_ARGS_ASSERT_FINALIZE_OPTREE;
1475 SAVEVPTR(PL_curcop);
1483 S_finalize_op(pTHX_ OP* o)
1485 PERL_ARGS_ASSERT_FINALIZE_OP;
1487 #if defined(PERL_MAD) && defined(USE_ITHREADS)
1489 /* Make sure mad ops are also thread-safe */
1490 MADPROP *mp = o->op_madprop;
1492 if (mp->mad_type == MAD_OP && mp->mad_vlen) {
1493 OP *prop_op = (OP *) mp->mad_val;
1494 /* We only need "Relocate sv to the pad for thread safety.", but this
1495 easiest way to make sure it traverses everything */
1496 if (prop_op->op_type == OP_CONST)
1497 cSVOPx(prop_op)->op_private &= ~OPpCONST_STRICT;
1498 finalize_op(prop_op);
1505 switch (o->op_type) {
1508 PL_curcop = ((COP*)o); /* for warnings */
1512 && (o->op_sibling->op_type == OP_NEXTSTATE || o->op_sibling->op_type == OP_DBSTATE)
1513 && ckWARN(WARN_SYNTAX))
1515 if (o->op_sibling->op_sibling) {
1516 const OPCODE type = o->op_sibling->op_sibling->op_type;
1517 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
1518 const line_t oldline = CopLINE(PL_curcop);
1519 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_sibling));
1520 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1521 "Statement unlikely to be reached");
1522 Perl_warner(aTHX_ packWARN(WARN_EXEC),
1523 "\t(Maybe you meant system() when you said exec()?)\n");
1524 CopLINE_set(PL_curcop, oldline);
1531 if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
1532 GV * const gv = cGVOPo_gv;
1533 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
1534 /* XXX could check prototype here instead of just carping */
1535 SV * const sv = sv_newmortal();
1536 gv_efullname3(sv, gv, NULL);
1537 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
1538 "%"SVf"() called too early to check prototype",
1545 if (cSVOPo->op_private & OPpCONST_STRICT)
1546 no_bareword_allowed(o);
1550 case OP_METHOD_NAMED:
1551 /* Relocate sv to the pad for thread safety.
1552 * Despite being a "constant", the SV is written to,
1553 * for reference counts, sv_upgrade() etc. */
1554 if (cSVOPo->op_sv) {
1555 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
1556 if (o->op_type != OP_METHOD_NAMED &&
1557 (SvPADTMP(cSVOPo->op_sv) || SvPADMY(cSVOPo->op_sv)))
1559 /* If op_sv is already a PADTMP/MY then it is being used by
1560 * some pad, so make a copy. */
1561 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
1562 SvREADONLY_on(PAD_SVl(ix));
1563 SvREFCNT_dec(cSVOPo->op_sv);
1565 else if (o->op_type != OP_METHOD_NAMED
1566 && cSVOPo->op_sv == &PL_sv_undef) {
1567 /* PL_sv_undef is hack - it's unsafe to store it in the
1568 AV that is the pad, because av_fetch treats values of
1569 PL_sv_undef as a "free" AV entry and will merrily
1570 replace them with a new SV, causing pad_alloc to think
1571 that this pad slot is free. (When, clearly, it is not)
1573 SvOK_off(PAD_SVl(ix));
1574 SvPADTMP_on(PAD_SVl(ix));
1575 SvREADONLY_on(PAD_SVl(ix));
1578 SvREFCNT_dec(PAD_SVl(ix));
1579 SvPADTMP_on(cSVOPo->op_sv);
1580 PAD_SETSV(ix, cSVOPo->op_sv);
1581 /* XXX I don't know how this isn't readonly already. */
1582 SvREADONLY_on(PAD_SVl(ix));
1584 cSVOPo->op_sv = NULL;
1595 const char *key = NULL;
1598 if (((BINOP*)o)->op_last->op_type != OP_CONST)
1601 /* Make the CONST have a shared SV */
1602 svp = cSVOPx_svp(((BINOP*)o)->op_last);
1603 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv))
1604 && SvTYPE(sv) < SVt_PVMG && !SvROK(sv)) {
1605 key = SvPV_const(sv, keylen);
1606 lexname = newSVpvn_share(key,
1607 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
1613 if ((o->op_private & (OPpLVAL_INTRO)))
1616 rop = (UNOP*)((BINOP*)o)->op_first;
1617 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
1619 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
1620 if (!SvPAD_TYPED(lexname))
1622 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1623 if (!fields || !GvHV(*fields))
1625 key = SvPV_const(*svp, keylen);
1626 if (!hv_fetch(GvHV(*fields), key,
1627 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1628 Perl_croak(aTHX_ "No such class field \"%s\" "
1629 "in variable %s of type %s",
1630 key, SvPV_nolen_const(lexname), HvNAME_get(SvSTASH(lexname)));
1642 SVOP *first_key_op, *key_op;
1644 if ((o->op_private & (OPpLVAL_INTRO))
1645 /* I bet there's always a pushmark... */
1646 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
1647 /* hmmm, no optimization if list contains only one key. */
1649 rop = (UNOP*)((LISTOP*)o)->op_last;
1650 if (rop->op_type != OP_RV2HV)
1652 if (rop->op_first->op_type == OP_PADSV)
1653 /* @$hash{qw(keys here)} */
1654 rop = (UNOP*)rop->op_first;
1656 /* @{$hash}{qw(keys here)} */
1657 if (rop->op_first->op_type == OP_SCOPE
1658 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
1660 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
1666 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
1667 if (!SvPAD_TYPED(lexname))
1669 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
1670 if (!fields || !GvHV(*fields))
1672 /* Again guessing that the pushmark can be jumped over.... */
1673 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
1674 ->op_first->op_sibling;
1675 for (key_op = first_key_op; key_op;
1676 key_op = (SVOP*)key_op->op_sibling) {
1677 if (key_op->op_type != OP_CONST)
1679 svp = cSVOPx_svp(key_op);
1680 key = SvPV_const(*svp, keylen);
1681 if (!hv_fetch(GvHV(*fields), key,
1682 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE)) {
1683 Perl_croak(aTHX_ "No such class field \"%s\" "
1684 "in variable %s of type %s",
1685 key, SvPV_nolen(lexname), HvNAME_get(SvSTASH(lexname)));
1691 if (cPMOPo->op_pmreplrootu.op_pmreplroot)
1692 finalize_op(cPMOPo->op_pmreplrootu.op_pmreplroot);
1699 if (o->op_flags & OPf_KIDS) {
1701 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
1707 =for apidoc Amx|OP *|op_lvalue|OP *o|I32 type
1709 Propagate lvalue ("modifiable") context to an op and its children.
1710 I<type> represents the context type, roughly based on the type of op that
1711 would do the modifying, although C<local()> is represented by OP_NULL,
1712 because it has no op type of its own (it is signalled by a flag on
1715 This function detects things that can't be modified, such as C<$x+1>, and
1716 generates errors for them. For example, C<$x+1 = 2> would cause it to be
1717 called with an op of type OP_ADD and a C<type> argument of OP_SASSIGN.
1719 It also flags things that need to behave specially in an lvalue context,
1720 such as C<$$x = 5> which might have to vivify a reference in C<$x>.
1726 Perl_op_lvalue_flags(pTHX_ OP *o, I32 type, U32 flags)
1730 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1733 if (!o || (PL_parser && PL_parser->error_count))
1736 if ((o->op_private & OPpTARGET_MY)
1737 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1742 assert( (o->op_flags & OPf_WANT) != OPf_WANT_VOID );
1744 if (type == OP_PRTF || type == OP_SPRINTF) type = OP_ENTERSUB;
1746 switch (o->op_type) {
1752 if ((o->op_flags & OPf_PARENS) || PL_madskills)
1756 if ((type == OP_UNDEF || type == OP_REFGEN || type == OP_LOCK) &&
1757 !(o->op_flags & OPf_STACKED)) {
1758 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1759 /* Both ENTERSUB and RV2CV use this bit, but for different pur-
1760 poses, so we need it clear. */
1761 o->op_private &= ~1;
1762 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1763 assert(cUNOPo->op_first->op_type == OP_NULL);
1764 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1767 else { /* lvalue subroutine call */
1768 o->op_private |= OPpLVAL_INTRO
1769 |(OPpENTERSUB_INARGS * (type == OP_LEAVESUBLV));
1770 PL_modcount = RETURN_UNLIMITED_NUMBER;
1771 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1772 /* Potential lvalue context: */
1773 o->op_private |= OPpENTERSUB_INARGS;
1776 else { /* Compile-time error message: */
1777 OP *kid = cUNOPo->op_first;
1781 if (kid->op_type != OP_PUSHMARK) {
1782 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1784 "panic: unexpected lvalue entersub "
1785 "args: type/targ %ld:%"UVuf,
1786 (long)kid->op_type, (UV)kid->op_targ);
1787 kid = kLISTOP->op_first;
1789 while (kid->op_sibling)
1790 kid = kid->op_sibling;
1791 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1792 break; /* Postpone until runtime */
1796 kid = kUNOP->op_first;
1797 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1798 kid = kUNOP->op_first;
1799 if (kid->op_type == OP_NULL)
1801 "Unexpected constant lvalue entersub "
1802 "entry via type/targ %ld:%"UVuf,
1803 (long)kid->op_type, (UV)kid->op_targ);
1804 if (kid->op_type != OP_GV) {
1808 cv = GvCV(kGVOP_gv);
1818 if (flags & OP_LVALUE_NO_CROAK) return NULL;
1819 /* grep, foreach, subcalls, refgen */
1820 if (type == OP_GREPSTART || type == OP_ENTERSUB
1821 || type == OP_REFGEN || type == OP_LEAVESUBLV)
1823 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1824 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1826 : (o->op_type == OP_ENTERSUB
1827 ? "non-lvalue subroutine call"
1829 type ? PL_op_desc[type] : "local"));
1843 case OP_RIGHT_SHIFT:
1852 if (!(o->op_flags & OPf_STACKED))
1859 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1860 op_lvalue(kid, type);
1865 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1866 PL_modcount = RETURN_UNLIMITED_NUMBER;
1867 return o; /* Treat \(@foo) like ordinary list. */
1871 if (scalar_mod_type(o, type))
1873 ref(cUNOPo->op_first, o->op_type);
1877 if (type == OP_LEAVESUBLV)
1878 o->op_private |= OPpMAYBE_LVSUB;
1884 PL_modcount = RETURN_UNLIMITED_NUMBER;
1887 PL_hints |= HINT_BLOCK_SCOPE;
1888 if (type == OP_LEAVESUBLV)
1889 o->op_private |= OPpMAYBE_LVSUB;
1893 ref(cUNOPo->op_first, o->op_type);
1897 PL_hints |= HINT_BLOCK_SCOPE;
1906 case OP_AELEMFAST_LEX:
1913 PL_modcount = RETURN_UNLIMITED_NUMBER;
1914 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1915 return o; /* Treat \(@foo) like ordinary list. */
1916 if (scalar_mod_type(o, type))
1918 if (type == OP_LEAVESUBLV)
1919 o->op_private |= OPpMAYBE_LVSUB;
1923 if (!type) /* local() */
1924 Perl_croak(aTHX_ "Can't localize lexical variable %"SVf,
1925 PAD_COMPNAME_SV(o->op_targ));
1934 if (type != OP_SASSIGN && type != OP_LEAVESUBLV)
1938 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1944 if (type == OP_LEAVESUBLV)
1945 o->op_private |= OPpMAYBE_LVSUB;
1946 pad_free(o->op_targ);
1947 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1948 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1949 if (o->op_flags & OPf_KIDS)
1950 op_lvalue(cBINOPo->op_first->op_sibling, type);
1955 ref(cBINOPo->op_first, o->op_type);
1956 if (type == OP_ENTERSUB &&
1957 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1958 o->op_private |= OPpLVAL_DEFER;
1959 if (type == OP_LEAVESUBLV)
1960 o->op_private |= OPpMAYBE_LVSUB;
1970 if (o->op_flags & OPf_KIDS)
1971 op_lvalue(cLISTOPo->op_last, type);
1976 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1978 else if (!(o->op_flags & OPf_KIDS))
1980 if (o->op_targ != OP_LIST) {
1981 op_lvalue(cBINOPo->op_first, type);
1987 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1988 /* elements might be in void context because the list is
1989 in scalar context or because they are attribute sub calls */
1990 if ( (kid->op_flags & OPf_WANT) != OPf_WANT_VOID )
1991 op_lvalue(kid, type);
1995 if (type != OP_LEAVESUBLV)
1997 break; /* op_lvalue()ing was handled by ck_return() */
2000 /* [20011101.069] File test operators interpret OPf_REF to mean that
2001 their argument is a filehandle; thus \stat(".") should not set
2003 if (type == OP_REFGEN &&
2004 PL_check[o->op_type] == Perl_ck_ftst)
2007 if (type != OP_LEAVESUBLV)
2008 o->op_flags |= OPf_MOD;
2010 if (type == OP_AASSIGN || type == OP_SASSIGN)
2011 o->op_flags |= OPf_SPECIAL|OPf_REF;
2012 else if (!type) { /* local() */
2015 o->op_private |= OPpLVAL_INTRO;
2016 o->op_flags &= ~OPf_SPECIAL;
2017 PL_hints |= HINT_BLOCK_SCOPE;
2022 Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
2023 "Useless localization of %s", OP_DESC(o));
2026 else if (type != OP_GREPSTART && type != OP_ENTERSUB
2027 && type != OP_LEAVESUBLV)
2028 o->op_flags |= OPf_REF;
2033 S_scalar_mod_type(const OP *o, I32 type)
2035 assert(o || type != OP_SASSIGN);
2039 if (o->op_type == OP_RV2GV)
2063 case OP_RIGHT_SHIFT:
2084 S_is_handle_constructor(const OP *o, I32 numargs)
2086 PERL_ARGS_ASSERT_IS_HANDLE_CONSTRUCTOR;
2088 switch (o->op_type) {
2096 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
2109 S_refkids(pTHX_ OP *o, I32 type)
2111 if (o && o->op_flags & OPf_KIDS) {
2113 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2120 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
2125 PERL_ARGS_ASSERT_DOREF;
2127 if (!o || (PL_parser && PL_parser->error_count))
2130 switch (o->op_type) {
2132 if ((type == OP_EXISTS || type == OP_DEFINED) &&
2133 !(o->op_flags & OPf_STACKED)) {
2134 o->op_type = OP_RV2CV; /* entersub => rv2cv */
2135 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
2136 assert(cUNOPo->op_first->op_type == OP_NULL);
2137 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
2138 o->op_flags |= OPf_SPECIAL;
2139 o->op_private &= ~1;
2141 else if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV){
2142 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2143 : type == OP_RV2HV ? OPpDEREF_HV
2145 o->op_flags |= OPf_MOD;
2151 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
2152 doref(kid, type, set_op_ref);
2155 if (type == OP_DEFINED)
2156 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2157 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2160 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2161 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2162 : type == OP_RV2HV ? OPpDEREF_HV
2164 o->op_flags |= OPf_MOD;
2171 o->op_flags |= OPf_REF;
2174 if (type == OP_DEFINED)
2175 o->op_flags |= OPf_SPECIAL; /* don't create GV */
2176 doref(cUNOPo->op_first, o->op_type, set_op_ref);
2182 o->op_flags |= OPf_REF;
2187 if (!(o->op_flags & OPf_KIDS))
2189 doref(cBINOPo->op_first, type, set_op_ref);
2193 doref(cBINOPo->op_first, o->op_type, set_op_ref);
2194 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
2195 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
2196 : type == OP_RV2HV ? OPpDEREF_HV
2198 o->op_flags |= OPf_MOD;
2208 if (!(o->op_flags & OPf_KIDS))
2210 doref(cLISTOPo->op_last, type, set_op_ref);
2220 S_dup_attrlist(pTHX_ OP *o)
2225 PERL_ARGS_ASSERT_DUP_ATTRLIST;
2227 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
2228 * where the first kid is OP_PUSHMARK and the remaining ones
2229 * are OP_CONST. We need to push the OP_CONST values.
2231 if (o->op_type == OP_CONST)
2232 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
2234 else if (o->op_type == OP_NULL)
2238 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
2240 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
2241 if (o->op_type == OP_CONST)
2242 rop = op_append_elem(OP_LIST, rop,
2243 newSVOP(OP_CONST, o->op_flags,
2244 SvREFCNT_inc_NN(cSVOPo->op_sv)));
2251 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
2256 PERL_ARGS_ASSERT_APPLY_ATTRS;
2258 /* fake up C<use attributes $pkg,$rv,@attrs> */
2259 ENTER; /* need to protect against side-effects of 'use' */
2260 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2262 #define ATTRSMODULE "attributes"
2263 #define ATTRSMODULE_PM "attributes.pm"
2266 /* Don't force the C<use> if we don't need it. */
2267 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
2268 if (svp && *svp != &PL_sv_undef)
2269 NOOP; /* already in %INC */
2271 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
2272 newSVpvs(ATTRSMODULE), NULL);
2275 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2276 newSVpvs(ATTRSMODULE),
2278 op_prepend_elem(OP_LIST,
2279 newSVOP(OP_CONST, 0, stashsv),
2280 op_prepend_elem(OP_LIST,
2281 newSVOP(OP_CONST, 0,
2283 dup_attrlist(attrs))));
2289 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
2292 OP *pack, *imop, *arg;
2295 PERL_ARGS_ASSERT_APPLY_ATTRS_MY;
2300 assert(target->op_type == OP_PADSV ||
2301 target->op_type == OP_PADHV ||
2302 target->op_type == OP_PADAV);
2304 /* Ensure that attributes.pm is loaded. */
2305 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
2307 /* Need package name for method call. */
2308 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
2310 /* Build up the real arg-list. */
2311 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
2313 arg = newOP(OP_PADSV, 0);
2314 arg->op_targ = target->op_targ;
2315 arg = op_prepend_elem(OP_LIST,
2316 newSVOP(OP_CONST, 0, stashsv),
2317 op_prepend_elem(OP_LIST,
2318 newUNOP(OP_REFGEN, 0,
2319 op_lvalue(arg, OP_REFGEN)),
2320 dup_attrlist(attrs)));
2322 /* Fake up a method call to import */
2323 meth = newSVpvs_share("import");
2324 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
2325 op_append_elem(OP_LIST,
2326 op_prepend_elem(OP_LIST, pack, list(arg)),
2327 newSVOP(OP_METHOD_NAMED, 0, meth)));
2329 /* Combine the ops. */
2330 *imopsp = op_append_elem(OP_LIST, *imopsp, imop);
2334 =notfor apidoc apply_attrs_string
2336 Attempts to apply a list of attributes specified by the C<attrstr> and
2337 C<len> arguments to the subroutine identified by the C<cv> argument which
2338 is expected to be associated with the package identified by the C<stashpv>
2339 argument (see L<attributes>). It gets this wrong, though, in that it
2340 does not correctly identify the boundaries of the individual attribute
2341 specifications within C<attrstr>. This is not really intended for the
2342 public API, but has to be listed here for systems such as AIX which
2343 need an explicit export list for symbols. (It's called from XS code
2344 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
2345 to respect attribute syntax properly would be welcome.
2351 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
2352 const char *attrstr, STRLEN len)
2356 PERL_ARGS_ASSERT_APPLY_ATTRS_STRING;
2359 len = strlen(attrstr);
2363 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
2365 const char * const sstr = attrstr;
2366 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
2367 attrs = op_append_elem(OP_LIST, attrs,
2368 newSVOP(OP_CONST, 0,
2369 newSVpvn(sstr, attrstr-sstr)));
2373 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
2374 newSVpvs(ATTRSMODULE),
2375 NULL, op_prepend_elem(OP_LIST,
2376 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
2377 op_prepend_elem(OP_LIST,
2378 newSVOP(OP_CONST, 0,
2379 newRV(MUTABLE_SV(cv))),
2384 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
2388 const bool stately = PL_parser && PL_parser->in_my == KEY_state;
2390 PERL_ARGS_ASSERT_MY_KID;
2392 if (!o || (PL_parser && PL_parser->error_count))
2396 if (PL_madskills && type == OP_NULL && o->op_flags & OPf_KIDS) {
2397 (void)my_kid(cUNOPo->op_first, attrs, imopsp);
2401 if (type == OP_LIST) {
2403 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
2404 my_kid(kid, attrs, imopsp);
2406 } else if (type == OP_UNDEF
2412 } else if (type == OP_RV2SV || /* "our" declaration */
2414 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
2415 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
2416 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2418 PL_parser->in_my == KEY_our
2420 : PL_parser->in_my == KEY_state ? "state" : "my"));
2422 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
2423 PL_parser->in_my = FALSE;
2424 PL_parser->in_my_stash = NULL;
2425 apply_attrs(GvSTASH(gv),
2426 (type == OP_RV2SV ? GvSV(gv) :
2427 type == OP_RV2AV ? MUTABLE_SV(GvAV(gv)) :
2428 type == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(gv)),
2431 o->op_private |= OPpOUR_INTRO;
2434 else if (type != OP_PADSV &&
2437 type != OP_PUSHMARK)
2439 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
2441 PL_parser->in_my == KEY_our
2443 : PL_parser->in_my == KEY_state ? "state" : "my"));
2446 else if (attrs && type != OP_PUSHMARK) {
2449 PL_parser->in_my = FALSE;
2450 PL_parser->in_my_stash = NULL;
2452 /* check for C<my Dog $spot> when deciding package */
2453 stash = PAD_COMPNAME_TYPE(o->op_targ);
2455 stash = PL_curstash;
2456 apply_attrs_my(stash, o, attrs, imopsp);
2458 o->op_flags |= OPf_MOD;
2459 o->op_private |= OPpLVAL_INTRO;
2461 o->op_private |= OPpPAD_STATE;
2466 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
2470 int maybe_scalar = 0;
2472 PERL_ARGS_ASSERT_MY_ATTRS;
2474 /* [perl #17376]: this appears to be premature, and results in code such as
2475 C< our(%x); > executing in list mode rather than void mode */
2477 if (o->op_flags & OPf_PARENS)
2487 o = my_kid(o, attrs, &rops);
2489 if (maybe_scalar && o->op_type == OP_PADSV) {
2490 o = scalar(op_append_list(OP_LIST, rops, o));
2491 o->op_private |= OPpLVAL_INTRO;
2494 /* The listop in rops might have a pushmark at the beginning,
2495 which will mess up list assignment. */
2496 LISTOP * const lrops = (LISTOP *)rops; /* for brevity */
2497 if (rops->op_type == OP_LIST &&
2498 lrops->op_first && lrops->op_first->op_type == OP_PUSHMARK)
2500 OP * const pushmark = lrops->op_first;
2501 lrops->op_first = pushmark->op_sibling;
2504 o = op_append_list(OP_LIST, o, rops);
2507 PL_parser->in_my = FALSE;
2508 PL_parser->in_my_stash = NULL;
2513 Perl_sawparens(pTHX_ OP *o)
2515 PERL_UNUSED_CONTEXT;
2517 o->op_flags |= OPf_PARENS;
2522 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
2526 const OPCODE ltype = left->op_type;
2527 const OPCODE rtype = right->op_type;
2529 PERL_ARGS_ASSERT_BIND_MATCH;
2531 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
2532 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
2534 const char * const desc
2536 rtype == OP_SUBST || rtype == OP_TRANS
2537 || rtype == OP_TRANSR
2539 ? (int)rtype : OP_MATCH];
2540 const bool isary = ltype == OP_RV2AV || ltype == OP_PADAV;
2543 (ltype == OP_RV2AV || ltype == OP_RV2HV)
2544 ? cUNOPx(left)->op_first->op_type == OP_GV
2545 && (gv = cGVOPx_gv(cUNOPx(left)->op_first))
2546 ? varname(gv, isary ? '@' : '%', 0, NULL, 0, 1)
2549 (GV *)PL_compcv, isary ? '@' : '%', left->op_targ, NULL, 0, 1
2552 Perl_warner(aTHX_ packWARN(WARN_MISC),
2553 "Applying %s to %"SVf" will act on scalar(%"SVf")",
2556 const char * const sample = (isary
2557 ? "@array" : "%hash");
2558 Perl_warner(aTHX_ packWARN(WARN_MISC),
2559 "Applying %s to %s will act on scalar(%s)",
2560 desc, sample, sample);
2564 if (rtype == OP_CONST &&
2565 cSVOPx(right)->op_private & OPpCONST_BARE &&
2566 cSVOPx(right)->op_private & OPpCONST_STRICT)
2568 no_bareword_allowed(right);
2571 /* !~ doesn't make sense with /r, so error on it for now */
2572 if (rtype == OP_SUBST && (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT) &&
2574 yyerror("Using !~ with s///r doesn't make sense");
2575 if (rtype == OP_TRANSR && type == OP_NOT)
2576 yyerror("Using !~ with tr///r doesn't make sense");
2578 ismatchop = (rtype == OP_MATCH ||
2579 rtype == OP_SUBST ||
2580 rtype == OP_TRANS || rtype == OP_TRANSR)
2581 && !(right->op_flags & OPf_SPECIAL);
2582 if (ismatchop && right->op_private & OPpTARGET_MY) {
2584 right->op_private &= ~OPpTARGET_MY;
2586 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
2589 right->op_flags |= OPf_STACKED;
2590 if (rtype != OP_MATCH && rtype != OP_TRANSR &&
2591 ! (rtype == OP_TRANS &&
2592 right->op_private & OPpTRANS_IDENTICAL) &&
2593 ! (rtype == OP_SUBST &&
2594 (cPMOPx(right)->op_pmflags & PMf_NONDESTRUCT)))
2595 newleft = op_lvalue(left, rtype);
2598 if (right->op_type == OP_TRANS || right->op_type == OP_TRANSR)
2599 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
2601 o = op_prepend_elem(rtype, scalar(newleft), right);
2603 return newUNOP(OP_NOT, 0, scalar(o));
2607 return bind_match(type, left,
2608 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
2612 Perl_invert(pTHX_ OP *o)
2616 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
2620 =for apidoc Amx|OP *|op_scope|OP *o
2622 Wraps up an op tree with some additional ops so that at runtime a dynamic
2623 scope will be created. The original ops run in the new dynamic scope,
2624 and then, provided that they exit normally, the scope will be unwound.
2625 The additional ops used to create and unwind the dynamic scope will
2626 normally be an C<enter>/C<leave> pair, but a C<scope> op may be used
2627 instead if the ops are simple enough to not need the full dynamic scope
2634 Perl_op_scope(pTHX_ OP *o)
2638 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
2639 o = op_prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2640 o->op_type = OP_LEAVE;
2641 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
2643 else if (o->op_type == OP_LINESEQ) {
2645 o->op_type = OP_SCOPE;
2646 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
2647 kid = ((LISTOP*)o)->op_first;
2648 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
2651 /* The following deals with things like 'do {1 for 1}' */
2652 kid = kid->op_sibling;
2654 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
2659 o = newLISTOP(OP_SCOPE, 0, o, NULL);
2665 Perl_block_start(pTHX_ int full)
2668 const int retval = PL_savestack_ix;
2670 pad_block_start(full);
2672 PL_hints &= ~HINT_BLOCK_SCOPE;
2673 SAVECOMPILEWARNINGS();
2674 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
2676 CALL_BLOCK_HOOKS(bhk_start, full);
2682 Perl_block_end(pTHX_ I32 floor, OP *seq)
2685 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
2686 OP* retval = scalarseq(seq);
2688 CALL_BLOCK_HOOKS(bhk_pre_end, &retval);
2691 CopHINTS_set(&PL_compiling, PL_hints);
2693 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2696 CALL_BLOCK_HOOKS(bhk_post_end, &retval);
2702 =head1 Compile-time scope hooks
2704 =for apidoc Aox||blockhook_register
2706 Register a set of hooks to be called when the Perl lexical scope changes
2707 at compile time. See L<perlguts/"Compile-time scope hooks">.
2713 Perl_blockhook_register(pTHX_ BHK *hk)
2715 PERL_ARGS_ASSERT_BLOCKHOOK_REGISTER;
2717 Perl_av_create_and_push(aTHX_ &PL_blockhooks, newSViv(PTR2IV(hk)));
2724 const PADOFFSET offset = pad_findmy_pvs("$_", 0);
2725 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS_isOUR(offset)) {
2726 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
2729 OP * const o = newOP(OP_PADSV, 0);
2730 o->op_targ = offset;
2736 Perl_newPROG(pTHX_ OP *o)
2740 PERL_ARGS_ASSERT_NEWPROG;
2746 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2747 ((PL_in_eval & EVAL_KEEPERR)
2748 ? OPf_SPECIAL : 0), o);
2750 cx = &cxstack[cxstack_ix];
2751 assert(CxTYPE(cx) == CXt_EVAL);
2753 if ((cx->blk_gimme & G_WANT) == G_VOID)
2754 scalarvoid(PL_eval_root);
2755 else if ((cx->blk_gimme & G_WANT) == G_ARRAY)
2758 scalar(PL_eval_root);
2760 /* don't use LINKLIST, since PL_eval_root might indirect through
2761 * a rather expensive function call and LINKLIST evaluates its
2762 * argument more than once */
2763 PL_eval_start = op_linklist(PL_eval_root);
2764 PL_eval_root->op_private |= OPpREFCOUNTED;
2765 OpREFCNT_set(PL_eval_root, 1);
2766 PL_eval_root->op_next = 0;
2767 CALL_PEEP(PL_eval_start);
2768 finalize_optree(PL_eval_root);
2772 if (o->op_type == OP_STUB) {
2773 PL_comppad_name = 0;
2775 S_op_destroy(aTHX_ o);
2778 PL_main_root = op_scope(sawparens(scalarvoid(o)));
2779 PL_curcop = &PL_compiling;
2780 PL_main_start = LINKLIST(PL_main_root);
2781 PL_main_root->op_private |= OPpREFCOUNTED;
2782 OpREFCNT_set(PL_main_root, 1);
2783 PL_main_root->op_next = 0;
2784 CALL_PEEP(PL_main_start);
2785 finalize_optree(PL_main_root);
2788 /* Register with debugger */
2790 CV * const cv = get_cvs("DB::postponed", 0);
2794 XPUSHs(MUTABLE_SV(CopFILEGV(&PL_compiling)));
2796 call_sv(MUTABLE_SV(cv), G_DISCARD);
2803 Perl_localize(pTHX_ OP *o, I32 lex)
2807 PERL_ARGS_ASSERT_LOCALIZE;
2809 if (o->op_flags & OPf_PARENS)
2810 /* [perl #17376]: this appears to be premature, and results in code such as
2811 C< our(%x); > executing in list mode rather than void mode */
2818 if ( PL_parser->bufptr > PL_parser->oldbufptr
2819 && PL_parser->bufptr[-1] == ','
2820 && ckWARN(WARN_PARENTHESIS))
2822 char *s = PL_parser->bufptr;
2825 /* some heuristics to detect a potential error */
2826 while (*s && (strchr(", \t\n", *s)))
2830 if (*s && strchr("@$%*", *s) && *++s
2831 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2834 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2836 while (*s && (strchr(", \t\n", *s)))
2842 if (sigil && (*s == ';' || *s == '=')) {
2843 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2844 "Parentheses missing around \"%s\" list",
2846 ? (PL_parser->in_my == KEY_our
2848 : PL_parser->in_my == KEY_state
2858 o = op_lvalue(o, OP_NULL); /* a bit kludgey */
2859 PL_parser->in_my = FALSE;
2860 PL_parser->in_my_stash = NULL;
2865 Perl_jmaybe(pTHX_ OP *o)
2867 PERL_ARGS_ASSERT_JMAYBE;
2869 if (o->op_type == OP_LIST) {
2871 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2872 o = convert(OP_JOIN, 0, op_prepend_elem(OP_LIST, o2, o));
2877 PERL_STATIC_INLINE OP *
2878 S_op_std_init(pTHX_ OP *o)
2880 I32 type = o->op_type;
2882 PERL_ARGS_ASSERT_OP_STD_INIT;
2884 if (PL_opargs[type] & OA_RETSCALAR)
2886 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2887 o->op_targ = pad_alloc(type, SVs_PADTMP);
2892 PERL_STATIC_INLINE OP *
2893 S_op_integerize(pTHX_ OP *o)
2895 I32 type = o->op_type;
2897 PERL_ARGS_ASSERT_OP_INTEGERIZE;
2899 /* integerize op, unless it happens to be C<-foo>.
2900 * XXX should pp_i_negate() do magic string negation instead? */
2901 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2902 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2903 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2906 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2909 if (type == OP_NEGATE)
2910 /* XXX might want a ck_negate() for this */
2911 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2917 S_fold_constants(pTHX_ register OP *o)
2920 register OP * VOL curop;
2922 VOL I32 type = o->op_type;
2927 SV * const oldwarnhook = PL_warnhook;
2928 SV * const olddiehook = PL_diehook;
2932 PERL_ARGS_ASSERT_FOLD_CONSTANTS;
2934 if (!(PL_opargs[type] & OA_FOLDCONST))
2948 /* XXX what about the numeric ops? */
2949 if (IN_LOCALE_COMPILETIME)
2954 if (PL_parser && PL_parser->error_count)
2955 goto nope; /* Don't try to run w/ errors */
2957 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2958 const OPCODE type = curop->op_type;
2959 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2961 type != OP_SCALAR &&
2963 type != OP_PUSHMARK)
2969 curop = LINKLIST(o);
2970 old_next = o->op_next;
2974 oldscope = PL_scopestack_ix;
2975 create_eval_scope(G_FAKINGEVAL);
2977 /* Verify that we don't need to save it: */
2978 assert(PL_curcop == &PL_compiling);
2979 StructCopy(&PL_compiling, ¬_compiling, COP);
2980 PL_curcop = ¬_compiling;
2981 /* The above ensures that we run with all the correct hints of the
2982 currently compiling COP, but that IN_PERL_RUNTIME is not true. */
2983 assert(IN_PERL_RUNTIME);
2984 PL_warnhook = PERL_WARNHOOK_FATAL;
2991 sv = *(PL_stack_sp--);
2992 if (o->op_targ && sv == PAD_SV(o->op_targ)) { /* grab pad temp? */
2994 /* Can't simply swipe the SV from the pad, because that relies on
2995 the op being freed "real soon now". Under MAD, this doesn't
2996 happen (see the #ifdef below). */
2999 pad_swipe(o->op_targ, FALSE);
3002 else if (SvTEMP(sv)) { /* grab mortal temp? */
3003 SvREFCNT_inc_simple_void(sv);
3008 /* Something tried to die. Abandon constant folding. */
3009 /* Pretend the error never happened. */
3011 o->op_next = old_next;
3015 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
3016 PL_warnhook = oldwarnhook;
3017 PL_diehook = olddiehook;
3018 /* XXX note that this croak may fail as we've already blown away
3019 * the stack - eg any nested evals */
3020 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
3023 PL_warnhook = oldwarnhook;
3024 PL_diehook = olddiehook;
3025 PL_curcop = &PL_compiling;
3027 if (PL_scopestack_ix > oldscope)
3028 delete_eval_scope();
3037 if (type == OP_RV2GV)
3038 newop = newGVOP(OP_GV, 0, MUTABLE_GV(sv));
3040 newop = newSVOP(OP_CONST, 0, MUTABLE_SV(sv));
3041 op_getmad(o,newop,'f');
3049 S_gen_constant_list(pTHX_ register OP *o)
3053 const I32 oldtmps_floor = PL_tmps_floor;
3056 if (PL_parser && PL_parser->error_count)
3057 return o; /* Don't attempt to run with errors */
3059 PL_op = curop = LINKLIST(o);
3062 Perl_pp_pushmark(aTHX);
3065 assert (!(curop->op_flags & OPf_SPECIAL));
3066 assert(curop->op_type == OP_RANGE);
3067 Perl_pp_anonlist(aTHX);
3068 PL_tmps_floor = oldtmps_floor;
3070 o->op_type = OP_RV2AV;
3071 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
3072 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
3073 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
3074 o->op_opt = 0; /* needs to be revisited in rpeep() */
3075 curop = ((UNOP*)o)->op_first;
3076 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
3078 op_getmad(curop,o,'O');
3087 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
3090 if (type < 0) type = -type, flags |= OPf_SPECIAL;
3091 if (!o || o->op_type != OP_LIST)
3092 o = newLISTOP(OP_LIST, 0, o, NULL);
3094 o->op_flags &= ~OPf_WANT;
3096 if (!(PL_opargs[type] & OA_MARK))
3097 op_null(cLISTOPo->op_first);
3099 OP * const kid2 = cLISTOPo->op_first->op_sibling;
3100 if (kid2 && kid2->op_type == OP_COREARGS) {
3101 op_null(cLISTOPo->op_first);
3102 kid2->op_private |= OPpCOREARGS_PUSHMARK;
3106 o->op_type = (OPCODE)type;
3107 o->op_ppaddr = PL_ppaddr[type];
3108 o->op_flags |= flags;
3110 o = CHECKOP(type, o);
3111 if (o->op_type != (unsigned)type)
3114 return fold_constants(op_integerize(op_std_init(o)));
3118 =head1 Optree Manipulation Functions
3121 /* List constructors */
3124 =for apidoc Am|OP *|op_append_elem|I32 optype|OP *first|OP *last
3126 Append an item to the list of ops contained directly within a list-type
3127 op, returning the lengthened list. I<first> is the list-type op,
3128 and I<last> is the op to append to the list. I<optype> specifies the
3129 intended opcode for the list. If I<first> is not already a list of the
3130 right type, it will be upgraded into one. If either I<first> or I<last>
3131 is null, the other is returned unchanged.
3137 Perl_op_append_elem(pTHX_ I32 type, OP *first, OP *last)
3145 if (first->op_type != (unsigned)type
3146 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
3148 return newLISTOP(type, 0, first, last);
3151 if (first->op_flags & OPf_KIDS)
3152 ((LISTOP*)first)->op_last->op_sibling = last;
3154 first->op_flags |= OPf_KIDS;
3155 ((LISTOP*)first)->op_first = last;
3157 ((LISTOP*)first)->op_last = last;
3162 =for apidoc Am|OP *|op_append_list|I32 optype|OP *first|OP *last
3164 Concatenate the lists of ops contained directly within two list-type ops,
3165 returning the combined list. I<first> and I<last> are the list-type ops
3166 to concatenate. I<optype> specifies the intended opcode for the list.
3167 If either I<first> or I<last> is not already a list of the right type,
3168 it will be upgraded into one. If either I<first> or I<last> is null,
3169 the other is returned unchanged.
3175 Perl_op_append_list(pTHX_ I32 type, OP *first, OP *last)
3183 if (first->op_type != (unsigned)type)
3184 return op_prepend_elem(type, first, last);
3186 if (last->op_type != (unsigned)type)
3187 return op_append_elem(type, first, last);
3189 ((LISTOP*)first)->op_last->op_sibling = ((LISTOP*)last)->op_first;
3190 ((LISTOP*)first)->op_last = ((LISTOP*)last)->op_last;
3191 first->op_flags |= (last->op_flags & OPf_KIDS);
3194 if (((LISTOP*)last)->op_first && first->op_madprop) {
3195 MADPROP *mp = ((LISTOP*)last)->op_first->op_madprop;
3197 while (mp->mad_next)
3199 mp->mad_next = first->op_madprop;
3202 ((LISTOP*)last)->op_first->op_madprop = first->op_madprop;
3205 first->op_madprop = last->op_madprop;
3206 last->op_madprop = 0;
3209 S_op_destroy(aTHX_ last);
3215 =for apidoc Am|OP *|op_prepend_elem|I32 optype|OP *first|OP *last
3217 Prepend an item to the list of ops contained directly within a list-type
3218 op, returning the lengthened list. I<first> is the op to prepend to the
3219 list, and I<last> is the list-type op. I<optype> specifies the intended
3220 opcode for the list. If I<last> is not already a list of the right type,
3221 it will be upgraded into one. If either I<first> or I<last> is null,
3222 the other is returned unchanged.
3228 Perl_op_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
3236 if (last->op_type == (unsigned)type) {
3237 if (type == OP_LIST) { /* already a PUSHMARK there */
3238 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
3239 ((LISTOP*)last)->op_first->op_sibling = first;
3240 if (!(first->op_flags & OPf_PARENS))
3241 last->op_flags &= ~OPf_PARENS;
3244 if (!(last->op_flags & OPf_KIDS)) {
3245 ((LISTOP*)last)->op_last = first;
3246 last->op_flags |= OPf_KIDS;
3248 first->op_sibling = ((LISTOP*)last)->op_first;
3249 ((LISTOP*)last)->op_first = first;
3251 last->op_flags |= OPf_KIDS;
3255 return newLISTOP(type, 0, first, last);
3263 Perl_newTOKEN(pTHX_ I32 optype, YYSTYPE lval, MADPROP* madprop)
3266 Newxz(tk, 1, TOKEN);
3267 tk->tk_type = (OPCODE)optype;
3268 tk->tk_type = 12345;
3270 tk->tk_mad = madprop;
3275 Perl_token_free(pTHX_ TOKEN* tk)
3277 PERL_ARGS_ASSERT_TOKEN_FREE;
3279 if (tk->tk_type != 12345)
3281 mad_free(tk->tk_mad);
3286 Perl_token_getmad(pTHX_ TOKEN* tk, OP* o, char slot)
3291 PERL_ARGS_ASSERT_TOKEN_GETMAD;
3293 if (tk->tk_type != 12345) {
3294 Perl_warner(aTHX_ packWARN(WARN_MISC),
3295 "Invalid TOKEN object ignored");
3302 /* faked up qw list? */
3304 tm->mad_type == MAD_SV &&
3305 SvPVX((SV *)tm->mad_val)[0] == 'q')
3312 /* pretend constant fold didn't happen? */
3313 if (mp->mad_key == 'f' &&
3314 (o->op_type == OP_CONST ||
3315 o->op_type == OP_GV) )
3317 token_getmad(tk,(OP*)mp->mad_val,slot);
3331 if (mp->mad_key == 'X')
3332 mp->mad_key = slot; /* just change the first one */
3342 Perl_op_getmad_weak(pTHX_ OP* from, OP* o, char slot)
3351 /* pretend constant fold didn't happen? */
3352 if (mp->mad_key == 'f' &&
3353 (o->op_type == OP_CONST ||
3354 o->op_type == OP_GV) )
3356 op_getmad(from,(OP*)mp->mad_val,slot);
3363 mp->mad_next = newMADPROP(slot,MAD_OP,from,0);
3366 o->op_madprop = newMADPROP(slot,MAD_OP,from,0);
3372 Perl_op_getmad(pTHX_ OP* from, OP* o, char slot)
3381 /* pretend constant fold didn't happen? */
3382 if (mp->mad_key == 'f' &&
3383 (o->op_type == OP_CONST ||
3384 o->op_type == OP_GV) )
3386 op_getmad(from,(OP*)mp->mad_val,slot);
3393 mp->mad_next = newMADPROP(slot,MAD_OP,from,1);
3396 o->op_madprop = newMADPROP(slot,MAD_OP,from,1);
3400 PerlIO_printf(PerlIO_stderr(),
3401 "DESTROYING op = %0"UVxf"\n", PTR2UV(from));
3407 Perl_prepend_madprops(pTHX_ MADPROP* mp, OP* o, char slot)
3425 Perl_append_madprops(pTHX_ MADPROP* tm, OP* o, char slot)
3429 addmad(tm, &(o->op_madprop), slot);
3433 Perl_addmad(pTHX_ MADPROP* tm, MADPROP** root, char slot)
3454 Perl_newMADsv(pTHX_ char key, SV* sv)
3456 PERL_ARGS_ASSERT_NEWMADSV;
3458 return newMADPROP(key, MAD_SV, sv, 0);
3462 Perl_newMADPROP(pTHX_ char key, char type, void* val, I32 vlen)
3464 MADPROP *const mp = (MADPROP *) PerlMemShared_malloc(sizeof(MADPROP));
3467 mp->mad_vlen = vlen;
3468 mp->mad_type = type;
3470 /* PerlIO_printf(PerlIO_stderr(), "NEW mp = %0x\n", mp); */
3475 Perl_mad_free(pTHX_ MADPROP* mp)
3477 /* PerlIO_printf(PerlIO_stderr(), "FREE mp = %0x\n", mp); */
3481 mad_free(mp->mad_next);
3482 /* if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING && mp->mad_vlen)
3483 PerlIO_printf(PerlIO_stderr(), "DESTROYING '%c'=<%s>\n", mp->mad_key & 255, mp->mad_val); */
3484 switch (mp->mad_type) {
3488 Safefree((char*)mp->mad_val);
3491 if (mp->mad_vlen) /* vlen holds "strong/weak" boolean */
3492 op_free((OP*)mp->mad_val);
3495 sv_free(MUTABLE_SV(mp->mad_val));
3498 PerlIO_printf(PerlIO_stderr(), "Unrecognized mad\n");
3501 PerlMemShared_free(mp);
3507 =head1 Optree construction
3509 =for apidoc Am|OP *|newNULLLIST
3511 Constructs, checks, and returns a new C<stub> op, which represents an
3512 empty list expression.
3518 Perl_newNULLLIST(pTHX)
3520 return newOP(OP_STUB, 0);
3524 S_force_list(pTHX_ OP *o)
3526 if (!o || o->op_type != OP_LIST)
3527 o = newLISTOP(OP_LIST, 0, o, NULL);
3533 =for apidoc Am|OP *|newLISTOP|I32 type|I32 flags|OP *first|OP *last
3535 Constructs, checks, and returns an op of any list type. I<type> is
3536 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3537 C<OPf_KIDS> will be set automatically if required. I<first> and I<last>
3538 supply up to two ops to be direct children of the list op; they are
3539 consumed by this function and become part of the constructed op tree.
3545 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3550 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_LISTOP);
3552 NewOp(1101, listop, 1, LISTOP);
3554 listop->op_type = (OPCODE)type;
3555 listop->op_ppaddr = PL_ppaddr[type];
3558 listop->op_flags = (U8)flags;
3562 else if (!first && last)
3565 first->op_sibling = last;
3566 listop->op_first = first;
3567 listop->op_last = last;
3568 if (type == OP_LIST) {
3569 OP* const pushop = newOP(OP_PUSHMARK, 0);
3570 pushop->op_sibling = first;
3571 listop->op_first = pushop;
3572 listop->op_flags |= OPf_KIDS;
3574 listop->op_last = pushop;
3577 return CHECKOP(type, listop);
3581 =for apidoc Am|OP *|newOP|I32 type|I32 flags
3583 Constructs, checks, and returns an op of any base type (any type that
3584 has no extra fields). I<type> is the opcode. I<flags> gives the
3585 eight bits of C<op_flags>, and, shifted up eight bits, the eight bits
3592 Perl_newOP(pTHX_ I32 type, I32 flags)
3597 if (type == -OP_ENTEREVAL) {
3598 type = OP_ENTEREVAL;
3599 flags |= OPpEVAL_BYTES<<8;
3602 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP
3603 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3604 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3605 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
3607 NewOp(1101, o, 1, OP);
3608 o->op_type = (OPCODE)type;
3609 o->op_ppaddr = PL_ppaddr[type];
3610 o->op_flags = (U8)flags;
3612 o->op_latefreed = 0;
3616 o->op_private = (U8)(0 | (flags >> 8));
3617 if (PL_opargs[type] & OA_RETSCALAR)
3619 if (PL_opargs[type] & OA_TARGET)
3620 o->op_targ = pad_alloc(type, SVs_PADTMP);
3621 return CHECKOP(type, o);
3625 =for apidoc Am|OP *|newUNOP|I32 type|I32 flags|OP *first
3627 Constructs, checks, and returns an op of any unary type. I<type> is
3628 the opcode. I<flags> gives the eight bits of C<op_flags>, except that
3629 C<OPf_KIDS> will be set automatically if required, and, shifted up eight
3630 bits, the eight bits of C<op_private>, except that the bit with value 1
3631 is automatically set. I<first> supplies an optional op to be the direct
3632 child of the unary op; it is consumed by this function and become part
3633 of the constructed op tree.
3639 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
3644 if (type == -OP_ENTEREVAL) {
3645 type = OP_ENTEREVAL;
3646 flags |= OPpEVAL_BYTES<<8;
3649 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_UNOP
3650 || (PL_opargs[type] & OA_CLASS_MASK) == OA_BASEOP_OR_UNOP
3651 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP
3652 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP
3653 || type == OP_SASSIGN
3654 || type == OP_ENTERTRY
3655 || type == OP_NULL );
3658 first = newOP(OP_STUB, 0);
3659 if (PL_opargs[type] & OA_MARK)
3660 first = force_list(first);
3662 NewOp(1101, unop, 1, UNOP);
3663 unop->op_type = (OPCODE)type;
3664 unop->op_ppaddr = PL_ppaddr[type];
3665 unop->op_first = first;
3666 unop->op_flags = (U8)(flags | OPf_KIDS);
3667 unop->op_private = (U8)(1 | (flags >> 8));
3668 unop = (UNOP*) CHECKOP(type, unop);
3672 return fold_constants(op_integerize(op_std_init((OP *) unop)));
3676 =for apidoc Am|OP *|newBINOP|I32 type|I32 flags|OP *first|OP *last
3678 Constructs, checks, and returns an op of any binary type. I<type>
3679 is the opcode. I<flags> gives the eight bits of C<op_flags>, except
3680 that C<OPf_KIDS> will be set automatically, and, shifted up eight bits,
3681 the eight bits of C<op_private>, except that the bit with value 1 or
3682 2 is automatically set as required. I<first> and I<last> supply up to
3683 two ops to be the direct children of the binary op; they are consumed
3684 by this function and become part of the constructed op tree.
3690 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
3695 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_BINOP
3696 || type == OP_SASSIGN || type == OP_NULL );
3698 NewOp(1101, binop, 1, BINOP);
3701 first = newOP(OP_NULL, 0);
3703 binop->op_type = (OPCODE)type;
3704 binop->op_ppaddr = PL_ppaddr[type];
3705 binop->op_first = first;
3706 binop->op_flags = (U8)(flags | OPf_KIDS);
3709 binop->op_private = (U8)(1 | (flags >> 8));
3712 binop->op_private = (U8)(2 | (flags >> 8));
3713 first->op_sibling = last;
3716 binop = (BINOP*)CHECKOP(type, binop);
3717 if (binop->op_next || binop->op_type != (OPCODE)type)
3720 binop->op_last = binop->op_first->op_sibling;
3722 return fold_constants(op_integerize(op_std_init((OP *)binop)));
3725 static int uvcompare(const void *a, const void *b)
3726 __attribute__nonnull__(1)
3727 __attribute__nonnull__(2)
3728 __attribute__pure__;
3729 static int uvcompare(const void *a, const void *b)
3731 if (*((const UV *)a) < (*(const UV *)b))
3733 if (*((const UV *)a) > (*(const UV *)b))
3735 if (*((const UV *)a+1) < (*(const UV *)b+1))
3737 if (*((const UV *)a+1) > (*(const UV *)b+1))
3743 S_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
3746 SV * const tstr = ((SVOP*)expr)->op_sv;
3749 (repl->op_type == OP_NULL)
3750 ? ((SVOP*)((LISTOP*)repl)->op_first)->op_sv :
3752 ((SVOP*)repl)->op_sv;
3755 const U8 *t = (U8*)SvPV_const(tstr, tlen);
3756 const U8 *r = (U8*)SvPV_const(rstr, rlen);
3760 register short *tbl;
3762 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
3763 const I32 squash = o->op_private & OPpTRANS_SQUASH;
3764 I32 del = o->op_private & OPpTRANS_DELETE;
3767 PERL_ARGS_ASSERT_PMTRANS;
3769 PL_hints |= HINT_BLOCK_SCOPE;
3772 o->op_private |= OPpTRANS_FROM_UTF;
3775 o->op_private |= OPpTRANS_TO_UTF;
3777 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
3778 SV* const listsv = newSVpvs("# comment\n");
3780 const U8* tend = t + tlen;
3781 const U8* rend = r + rlen;
3795 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
3796 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
3799 const U32 flags = UTF8_ALLOW_DEFAULT;
3803 t = tsave = bytes_to_utf8(t, &len);
3806 if (!to_utf && rlen) {
3808 r = rsave = bytes_to_utf8(r, &len);
3812 /* There are several snags with this code on EBCDIC:
3813 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
3814 2. scan_const() in toke.c has encoded chars in native encoding which makes
3815 ranges at least in EBCDIC 0..255 range the bottom odd.
3819 U8 tmpbuf[UTF8_MAXBYTES+1];
3822 Newx(cp, 2*tlen, UV);
3824 transv = newSVpvs("");
3826 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3828 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
3830 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, flags);
3834 cp[2*i+1] = cp[2*i];
3838 qsort(cp, i, 2*sizeof(UV), uvcompare);
3839 for (j = 0; j < i; j++) {
3841 diff = val - nextmin;
3843 t = uvuni_to_utf8(tmpbuf,nextmin);
3844 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3846 U8 range_mark = UTF_TO_NATIVE(0xff);
3847 t = uvuni_to_utf8(tmpbuf, val - 1);
3848 sv_catpvn(transv, (char *)&range_mark, 1);
3849 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3856 t = uvuni_to_utf8(tmpbuf,nextmin);
3857 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3859 U8 range_mark = UTF_TO_NATIVE(0xff);
3860 sv_catpvn(transv, (char *)&range_mark, 1);
3862 t = uvuni_to_utf8(tmpbuf, 0x7fffffff);
3863 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
3864 t = (const U8*)SvPVX_const(transv);
3865 tlen = SvCUR(transv);
3869 else if (!rlen && !del) {
3870 r = t; rlen = tlen; rend = tend;
3873 if ((!rlen && !del) || t == r ||
3874 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
3876 o->op_private |= OPpTRANS_IDENTICAL;
3880 while (t < tend || tfirst <= tlast) {
3881 /* see if we need more "t" chars */
3882 if (tfirst > tlast) {
3883 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3885 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
3887 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, flags);
3894 /* now see if we need more "r" chars */
3895 if (rfirst > rlast) {
3897 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3899 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
3901 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, flags);
3910 rfirst = rlast = 0xffffffff;
3914 /* now see which range will peter our first, if either. */
3915 tdiff = tlast - tfirst;
3916 rdiff = rlast - rfirst;
3923 if (rfirst == 0xffffffff) {
3924 diff = tdiff; /* oops, pretend rdiff is infinite */
3926 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
3927 (long)tfirst, (long)tlast);
3929 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
3933 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
3934 (long)tfirst, (long)(tfirst + diff),
3937 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
3938 (long)tfirst, (long)rfirst);
3940 if (rfirst + diff > max)
3941 max = rfirst + diff;
3943 grows = (tfirst < rfirst &&
3944 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
3956 else if (max > 0xff)
3961 PerlMemShared_free(cPVOPo->op_pv);
3962 cPVOPo->op_pv = NULL;
3964 swash = MUTABLE_SV(swash_init("utf8", "", listsv, bits, none));
3966 cPADOPo->op_padix = pad_alloc(OP_TRANS, SVs_PADTMP);
3967 SvREFCNT_dec(PAD_SVl(cPADOPo->op_padix));
3968 PAD_SETSV(cPADOPo->op_padix, swash);
3970 SvREADONLY_on(swash);
3972 cSVOPo->op_sv = swash;
3974 SvREFCNT_dec(listsv);
3975 SvREFCNT_dec(transv);
3977 if (!del && havefinal && rlen)
3978 (void)hv_store(MUTABLE_HV(SvRV(swash)), "FINAL", 5,
3979 newSVuv((UV)final), 0);
3982 o->op_private |= OPpTRANS_GROWS;
3988 op_getmad(expr,o,'e');
3989 op_getmad(repl,o,'r');
3997 tbl = (short*)cPVOPo->op_pv;
3999 Zero(tbl, 256, short);
4000 for (i = 0; i < (I32)tlen; i++)
4002 for (i = 0, j = 0; i < 256; i++) {
4004 if (j >= (I32)rlen) {
4013 if (i < 128 && r[j] >= 128)
4023 o->op_private |= OPpTRANS_IDENTICAL;
4025 else if (j >= (I32)rlen)
4030 PerlMemShared_realloc(tbl,
4031 (0x101+rlen-j) * sizeof(short));
4032 cPVOPo->op_pv = (char*)tbl;
4034 tbl[0x100] = (short)(rlen - j);
4035 for (i=0; i < (I32)rlen - j; i++)
4036 tbl[0x101+i] = r[j+i];
4040 if (!rlen && !del) {
4043 o->op_private |= OPpTRANS_IDENTICAL;
4045 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
4046 o->op_private |= OPpTRANS_IDENTICAL;
4048 for (i = 0; i < 256; i++)
4050 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
4051 if (j >= (I32)rlen) {
4053 if (tbl[t[i]] == -1)
4059 if (tbl[t[i]] == -1) {
4060 if (t[i] < 128 && r[j] >= 128)
4067 if(del && rlen == tlen) {
4068 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Useless use of /d modifier in transliteration operator");
4069 } else if(rlen > tlen) {
4070 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Replacement list is longer than search list");
4074 o->op_private |= OPpTRANS_GROWS;
4076 op_getmad(expr,o,'e');
4077 op_getmad(repl,o,'r');
4087 =for apidoc Am|OP *|newPMOP|I32 type|I32 flags
4089 Constructs, checks, and returns an op of any pattern matching type.
4090 I<type> is the opcode. I<flags> gives the eight bits of C<op_flags>
4091 and, shifted up eight bits, the eight bits of C<op_private>.
4097 Perl_newPMOP(pTHX_ I32 type, I32 flags)
4102 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PMOP);
4104 NewOp(1101, pmop, 1, PMOP);
4105 pmop->op_type = (OPCODE)type;
4106 pmop->op_ppaddr = PL_ppaddr[type];
4107 pmop->op_flags = (U8)flags;
4108 pmop->op_private = (U8)(0 | (flags >> 8));
4110 if (PL_hints & HINT_RE_TAINT)
4111 pmop->op_pmflags |= PMf_RETAINT;
4112 if (IN_LOCALE_COMPILETIME) {
4113 set_regex_charset(&(pmop->op_pmflags), REGEX_LOCALE_CHARSET);
4115 else if ((! (PL_hints & HINT_BYTES))
4116 /* Both UNI_8_BIT and locale :not_characters imply Unicode */
4117 && (PL_hints & (HINT_UNI_8_BIT|HINT_LOCALE_NOT_CHARS)))
4119 set_regex_charset(&(pmop->op_pmflags), REGEX_UNICODE_CHARSET);
4121 if (PL_hints & HINT_RE_FLAGS) {
4122 SV *reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4123 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags"), 0, 0
4125 if (reflags && SvOK(reflags)) pmop->op_pmflags |= SvIV(reflags);
4126 reflags = Perl_refcounted_he_fetch_pvn(aTHX_
4127 PL_compiling.cop_hints_hash, STR_WITH_LEN("reflags_charset"), 0, 0
4129 if (reflags && SvOK(reflags)) {
4130 set_regex_charset(&(pmop->op_pmflags), (regex_charset)SvIV(reflags));
4136 assert(SvPOK(PL_regex_pad[0]));
4137 if (SvCUR(PL_regex_pad[0])) {
4138 /* Pop off the "packed" IV from the end. */
4139 SV *const repointer_list = PL_regex_pad[0];
4140 const char *p = SvEND(repointer_list) - sizeof(IV);
4141 const IV offset = *((IV*)p);
4143 assert(SvCUR(repointer_list) % sizeof(IV) == 0);
4145 SvEND_set(repointer_list, p);
4147 pmop->op_pmoffset = offset;
4148 /* This slot should be free, so assert this: */
4149 assert(PL_regex_pad[offset] == &PL_sv_undef);
4151 SV * const repointer = &PL_sv_undef;
4152 av_push(PL_regex_padav, repointer);
4153 pmop->op_pmoffset = av_len(PL_regex_padav);
4154 PL_regex_pad = AvARRAY(PL_regex_padav);
4158 return CHECKOP(type, pmop);
4161 /* Given some sort of match op o, and an expression expr containing a
4162 * pattern, either compile expr into a regex and attach it to o (if it's
4163 * constant), or convert expr into a runtime regcomp op sequence (if it's
4166 * isreg indicates that the pattern is part of a regex construct, eg
4167 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
4168 * split "pattern", which aren't. In the former case, expr will be a list
4169 * if the pattern contains more than one term (eg /a$b/) or if it contains
4170 * a replacement, ie s/// or tr///.
4174 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
4179 I32 repl_has_vars = 0;
4183 PERL_ARGS_ASSERT_PMRUNTIME;
4186 o->op_type == OP_SUBST
4187 || o->op_type == OP_TRANS || o->op_type == OP_TRANSR
4189 /* last element in list is the replacement; pop it */
4191 repl = cLISTOPx(expr)->op_last;
4192 kid = cLISTOPx(expr)->op_first;
4193 while (kid->op_sibling != repl)
4194 kid = kid->op_sibling;
4195 kid->op_sibling = NULL;
4196 cLISTOPx(expr)->op_last = kid;
4199 if (isreg && expr->op_type == OP_LIST &&
4200 cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last)
4202 /* convert single element list to element */
4203 OP* const oe = expr;
4204 expr = cLISTOPx(oe)->op_first->op_sibling;
4205 cLISTOPx(oe)->op_first->op_sibling = NULL;
4206 cLISTOPx(oe)->op_last = NULL;
4210 if (o->op_type == OP_TRANS || o->op_type == OP_TRANSR) {
4211 return pmtrans(o, expr, repl);
4214 reglist = isreg && expr->op_type == OP_LIST;
4218 PL_hints |= HINT_BLOCK_SCOPE;
4221 if (expr->op_type == OP_CONST) {
4222 SV *pat = ((SVOP*)expr)->op_sv;
4223 U32 pm_flags = pm->op_pmflags & RXf_PMf_COMPILETIME;
4225 if (o->op_flags & OPf_SPECIAL)
4226 pm_flags |= RXf_SPLIT;
4229 assert (SvUTF8(pat));
4230 } else if (SvUTF8(pat)) {
4231 /* Not doing UTF-8, despite what the SV says. Is this only if we're
4232 trapped in use 'bytes'? */
4233 /* Make a copy of the octet sequence, but without the flag on, as
4234 the compiler now honours the SvUTF8 flag on pat. */
4236 const char *const p = SvPV(pat, len);
4237 pat = newSVpvn_flags(p, len, SVs_TEMP);
4240 PM_SETRE(pm, CALLREGCOMP(pat, pm_flags));
4243 op_getmad(expr,(OP*)pm,'e');
4249 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
4250 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
4252 : OP_REGCMAYBE),0,expr);
4254 NewOp(1101, rcop, 1, LOGOP);
4255 rcop->op_type = OP_REGCOMP;
4256 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
4257 rcop->op_first = scalar(expr);
4258 rcop->op_flags |= OPf_KIDS
4259 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
4260 | (reglist ? OPf_STACKED : 0);
4261 rcop->op_private = 1;
4264 rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
4266 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
4267 if (PL_hints & HINT_RE_EVAL) PL_cv_has_eval = 1;
4269 /* establish postfix order */
4270 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
4272 rcop->op_next = expr;
4273 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
4276 rcop->op_next = LINKLIST(expr);
4277 expr->op_next = (OP*)rcop;
4280 op_prepend_elem(o->op_type, scalar((OP*)rcop), o);
4285 if (pm->op_pmflags & PMf_EVAL) {
4287 if (CopLINE(PL_curcop) < (line_t)PL_parser->multi_end)
4288 CopLINE_set(PL_curcop, (line_t)PL_parser->multi_end);
4290 else if (repl->op_type == OP_CONST)
4294 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
4295 if (curop->op_type == OP_SCOPE
4296 || curop->op_type == OP_LEAVE
4297 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
4298 if (curop->op_type == OP_GV) {
4299 GV * const gv = cGVOPx_gv(curop);
4301 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
4304 else if (curop->op_type == OP_RV2CV)
4306 else if (curop->op_type == OP_RV2SV ||
4307 curop->op_type == OP_RV2AV ||
4308 curop->op_type == OP_RV2HV ||
4309 curop->op_type == OP_RV2GV) {
4310 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
4313 else if (curop->op_type == OP_PADSV ||
4314 curop->op_type == OP_PADAV ||
4315 curop->op_type == OP_PADHV ||
4316 curop->op_type == OP_PADANY)
4320 else if (curop->op_type == OP_PUSHRE)
4321 NOOP; /* Okay here, dangerous in newASSIGNOP */
4331 || RX_EXTFLAGS(PM_GETRE(pm)) & RXf_EVAL_SEEN)))
4333 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4334 op_prepend_elem(o->op_type, scalar(repl), o);
4337 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
4338 pm->op_pmflags |= PMf_MAYBE_CONST;
4340 NewOp(1101, rcop, 1, LOGOP);
4341 rcop->op_type = OP_SUBSTCONT;
4342 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
4343 rcop->op_first = scalar(repl);
4344 rcop->op_flags |= OPf_KIDS;
4345 rcop->op_private = 1;
4348 /* establish postfix order */
4349 rcop->op_next = LINKLIST(repl);
4350 repl->op_next = (OP*)rcop;
4352 pm->op_pmreplrootu.op_pmreplroot = scalar((OP*)rcop);
4353 assert(!(pm->op_pmflags & PMf_ONCE));
4354 pm->op_pmstashstartu.op_pmreplstart = LINKLIST(rcop);
4363 =for apidoc Am|OP *|newSVOP|I32 type|I32 flags|SV *sv
4365 Constructs, checks, and returns an op of any type that involves an
4366 embedded SV. I<type> is the opcode. I<flags> gives the eight bits
4367 of C<op_flags>. I<sv> gives the SV to embed in the op; this function
4368 takes ownership of one reference to it.
4374 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
4379 PERL_ARGS_ASSERT_NEWSVOP;
4381 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4382 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4383 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4385 NewOp(1101, svop, 1, SVOP);
4386 svop->op_type = (OPCODE)type;
4387 svop->op_ppaddr = PL_ppaddr[type];
4389 svop->op_next = (OP*)svop;
4390 svop->op_flags = (U8)flags;
4391 if (PL_opargs[type] & OA_RETSCALAR)
4393 if (PL_opargs[type] & OA_TARGET)
4394 svop->op_targ = pad_alloc(type, SVs_PADTMP);
4395 return CHECKOP(type, svop);
4401 =for apidoc Am|OP *|newPADOP|I32 type|I32 flags|SV *sv
4403 Constructs, checks, and returns an op of any type that involves a
4404 reference to a pad element. I<type> is the opcode. I<flags> gives the
4405 eight bits of C<op_flags>. A pad slot is automatically allocated, and
4406 is populated with I<sv>; this function takes ownership of one reference
4409 This function only exists if Perl has been compiled to use ithreads.
4415 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
4420 PERL_ARGS_ASSERT_NEWPADOP;
4422 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP
4423 || (PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4424 || (PL_opargs[type] & OA_CLASS_MASK) == OA_FILESTATOP);
4426 NewOp(1101, padop, 1, PADOP);
4427 padop->op_type = (OPCODE)type;
4428 padop->op_ppaddr = PL_ppaddr[type];
4429 padop->op_padix = pad_alloc(type, SVs_PADTMP);
4430 SvREFCNT_dec(PAD_SVl(padop->op_padix));
4431 PAD_SETSV(padop->op_padix, sv);
4434 padop->op_next = (OP*)padop;
4435 padop->op_flags = (U8)flags;
4436 if (PL_opargs[type] & OA_RETSCALAR)
4438 if (PL_opargs[type] & OA_TARGET)
4439 padop->op_targ = pad_alloc(type, SVs_PADTMP);
4440 return CHECKOP(type, padop);
4443 #endif /* !USE_ITHREADS */
4446 =for apidoc Am|OP *|newGVOP|I32 type|I32 flags|GV *gv
4448 Constructs, checks, and returns an op of any type that involves an
4449 embedded reference to a GV. I<type> is the opcode. I<flags> gives the
4450 eight bits of C<op_flags>. I<gv> identifies the GV that the op should
4451 reference; calling this function does not transfer ownership of any
4458 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
4462 PERL_ARGS_ASSERT_NEWGVOP;
4466 return newPADOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4468 return newSVOP(type, flags, SvREFCNT_inc_simple_NN(gv));
4473 =for apidoc Am|OP *|newPVOP|I32 type|I32 flags|char *pv
4475 Constructs, checks, and returns an op of any type that involves an
4476 embedded C-level pointer (PV). I<type> is the opcode. I<flags> gives
4477 the eight bits of C<op_flags>. I<pv> supplies the C-level pointer, which
4478 must have been allocated using L</PerlMemShared_malloc>; the memory will
4479 be freed when the op is destroyed.
4485 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
4490 assert((PL_opargs[type] & OA_CLASS_MASK) == OA_PVOP_OR_SVOP
4492 || (PL_opargs[type] & OA_CLASS_MASK) == OA_LOOPEXOP);
4494 NewOp(1101, pvop, 1, PVOP);
4495 pvop->op_type = (OPCODE)type;
4496 pvop->op_ppaddr = PL_ppaddr[type];
4498 pvop->op_next = (OP*)pvop;
4499 pvop->op_flags = (U8)flags;
4500 if (PL_opargs[type] & OA_RETSCALAR)
4502 if (PL_opargs[type] & OA_TARGET)
4503 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
4504 return CHECKOP(type, pvop);
4512 Perl_package(pTHX_ OP *o)
4515 SV *const sv = cSVOPo->op_sv;
4520 PERL_ARGS_ASSERT_PACKAGE;
4522 SAVEGENERICSV(PL_curstash);
4523 save_item(PL_curstname);
4525 PL_curstash = (HV *)SvREFCNT_inc(gv_stashsv(sv, GV_ADD));
4527 sv_setsv(PL_curstname, sv);
4529 PL_hints |= HINT_BLOCK_SCOPE;
4530 PL_parser->copline = NOLINE;
4531 PL_parser->expect = XSTATE;
4536 if (!PL_madskills) {
4541 pegop = newOP(OP_NULL,0);
4542 op_getmad(o,pegop,'P');
4548 Perl_package_version( pTHX_ OP *v )
4551 U32 savehints = PL_hints;
4552 PERL_ARGS_ASSERT_PACKAGE_VERSION;
4553 PL_hints &= ~HINT_STRICT_VARS;
4554 sv_setsv( GvSV(gv_fetchpvs("VERSION", GV_ADDMULTI, SVt_PV)), cSVOPx(v)->op_sv );
4555 PL_hints = savehints;
4564 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
4571 OP *pegop = newOP(OP_NULL,0);
4573 SV *use_version = NULL;
4575 PERL_ARGS_ASSERT_UTILIZE;
4577 if (idop->op_type != OP_CONST)
4578 Perl_croak(aTHX_ "Module name must be constant");
4581 op_getmad(idop,pegop,'U');
4586 SV * const vesv = ((SVOP*)version)->op_sv;
4589 op_getmad(version,pegop,'V');
4590 if (!arg && !SvNIOKp(vesv)) {
4597 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
4598 Perl_croak(aTHX_ "Version number must be a constant number");
4600 /* Make copy of idop so we don't free it twice */
4601 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4603 /* Fake up a method call to VERSION */
4604 meth = newSVpvs_share("VERSION");
4605 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4606 op_append_elem(OP_LIST,
4607 op_prepend_elem(OP_LIST, pack, list(version)),
4608 newSVOP(OP_METHOD_NAMED, 0, meth)));
4612 /* Fake up an import/unimport */
4613 if (arg && arg->op_type == OP_STUB) {
4615 op_getmad(arg,pegop,'S');
4616 imop = arg; /* no import on explicit () */
4618 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
4619 imop = NULL; /* use 5.0; */
4621 use_version = ((SVOP*)idop)->op_sv;
4623 idop->op_private |= OPpCONST_NOVER;
4629 op_getmad(arg,pegop,'A');
4631 /* Make copy of idop so we don't free it twice */
4632 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
4634 /* Fake up a method call to import/unimport */
4636 ? newSVpvs_share("import") : newSVpvs_share("unimport");
4637 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
4638 op_append_elem(OP_LIST,
4639 op_prepend_elem(OP_LIST, pack, list(arg)),
4640 newSVOP(OP_METHOD_NAMED, 0, meth)));
4643 /* Fake up the BEGIN {}, which does its thing immediately. */
4645 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
4648 op_append_elem(OP_LINESEQ,
4649 op_append_elem(OP_LINESEQ,
4650 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
4651 newSTATEOP(0, NULL, veop)),
4652 newSTATEOP(0, NULL, imop) ));
4655 HV * const hinthv = GvHV(PL_hintgv);
4656 const bool hhoff = !hinthv || !(PL_hints & HINT_LOCALIZE_HH);
4659 * feature bundle that corresponds to the required version. */
4660 use_version = sv_2mortal(new_version(use_version));
4661 S_enable_feature_bundle(aTHX_ use_version);
4663 /* If a version >= 5.11.0 is requested, strictures are on by default! */
4664 if (vcmp(use_version,
4665 sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
4666 if (hhoff || !hv_exists(hinthv, "strict/refs", 11))
4667 PL_hints |= HINT_STRICT_REFS;
4668 if (hhoff || !hv_exists(hinthv, "strict/subs", 11))
4669 PL_hints |= HINT_STRICT_SUBS;
4670 if (hhoff || !hv_exists(hinthv, "strict/vars", 11))
4671 PL_hints |= HINT_STRICT_VARS;
4673 /* otherwise they are off */
4675 if (hhoff || !hv_exists(hinthv, "strict/refs", 11))
4676 PL_hints &= ~HINT_STRICT_REFS;
4677 if (hhoff || !hv_exists(hinthv, "strict/subs", 11))
4678 PL_hints &= ~HINT_STRICT_SUBS;
4679 if (hhoff || !hv_exists(hinthv, "strict/vars", 11))
4680 PL_hints &= ~HINT_STRICT_VARS;
4684 /* The "did you use incorrect case?" warning used to be here.
4685 * The problem is that on case-insensitive filesystems one
4686 * might get false positives for "use" (and "require"):
4687 * "use Strict" or "require CARP" will work. This causes
4688 * portability problems for the script: in case-strict
4689 * filesystems the script will stop working.
4691 * The "incorrect case" warning checked whether "use Foo"
4692 * imported "Foo" to your namespace, but that is wrong, too:
4693 * there is no requirement nor promise in the language that
4694 * a Foo.pm should or would contain anything in package "Foo".
4696 * There is very little Configure-wise that can be done, either:
4697 * the case-sensitivity of the build filesystem of Perl does not
4698 * help in guessing the case-sensitivity of the runtime environment.
4701 PL_hints |= HINT_BLOCK_SCOPE;
4702 PL_parser->copline = NOLINE;
4703 PL_parser->expect = XSTATE;
4704 PL_cop_seqmax++; /* Purely for B::*'s benefit */
4705 if (PL_cop_seqmax == PERL_PADSEQ_INTRO) /* not a legal value */
4709 if (!PL_madskills) {
4710 /* FIXME - don't allocate pegop if !PL_madskills */
4719 =head1 Embedding Functions
4721 =for apidoc load_module
4723 Loads the module whose name is pointed to by the string part of name.
4724 Note that the actual module name, not its filename, should be given.
4725 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
4726 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
4727 (or 0 for no flags). ver, if specified and not NULL, provides version semantics
4728 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
4729 arguments can be used to specify arguments to the module's import()
4730 method, similar to C<use Foo::Bar VERSION LIST>. They must be
4731 terminated with a final NULL pointer. Note that this list can only
4732 be omitted when the PERL_LOADMOD_NOIMPORT flag has been used.
4733 Otherwise at least a single NULL pointer to designate the default
4734 import list is required.
4736 The reference count for each specified C<SV*> parameter is decremented.
4741 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
4745 PERL_ARGS_ASSERT_LOAD_MODULE;
4747 va_start(args, ver);
4748 vload_module(flags, name, ver, &args);
4752 #ifdef PERL_IMPLICIT_CONTEXT
4754 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
4758 PERL_ARGS_ASSERT_LOAD_MODULE_NOCONTEXT;
4759 va_start(args, ver);
4760 vload_module(flags, name, ver, &args);
4766 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
4770 OP * const modname = newSVOP(OP_CONST, 0, name);
4772 PERL_ARGS_ASSERT_VLOAD_MODULE;
4774 modname->op_private |= OPpCONST_BARE;
4776 veop = newSVOP(OP_CONST, 0, ver);
4780 if (flags & PERL_LOADMOD_NOIMPORT) {
4781 imop = sawparens(newNULLLIST());
4783 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
4784 imop = va_arg(*args, OP*);
4789 sv = va_arg(*args, SV*);
4791 imop = op_append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
4792 sv = va_arg(*args, SV*);
4796 /* utilize() fakes up a BEGIN { require ..; import ... }, so make sure
4797 * that it has a PL_parser to play with while doing that, and also
4798 * that it doesn't mess with any existing parser, by creating a tmp
4799 * new parser with lex_start(). This won't actually be used for much,
4800 * since pp_require() will create another parser for the real work. */
4803 SAVEVPTR(PL_curcop);
4804 lex_start(NULL, NULL, LEX_START_SAME_FILTER);
4805 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
4806 veop, modname, imop);
4811 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
4817 PERL_ARGS_ASSERT_DOFILE;
4819 if (!force_builtin) {
4820 gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
4821 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
4822 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
4823 gv = gvp ? *gvp : NULL;
4827 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
4828 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
4829 op_append_elem(OP_LIST, term,
4830 scalar(newUNOP(OP_RV2CV, 0,
4831 newGVOP(OP_GV, 0, gv))))));
4834 doop = newUNOP(OP_DOFILE, 0, scalar(term));
4840 =head1 Optree construction
4842 =for apidoc Am|OP *|newSLICEOP|I32 flags|OP *subscript|OP *listval
4844 Constructs, checks, and returns an C<lslice> (list slice) op. I<flags>
4845 gives the eight bits of C<op_flags>, except that C<OPf_KIDS> will
4846 be set automatically, and, shifted up eight bits, the eight bits of
4847 C<op_private>, except that the bit with value 1 or 2 is automatically
4848 set as required. I<listval> and I<subscript> supply the parameters of
4849 the slice; they are consumed by this function and become part of the
4850 constructed op tree.
4856 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
4858 return newBINOP(OP_LSLICE, flags,
4859 list(force_list(subscript)),
4860 list(force_list(listval)) );
4864 S_is_list_assignment(pTHX_ register const OP *o)
4872 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
4873 o = cUNOPo->op_first;
4875 flags = o->op_flags;
4877 if (type == OP_COND_EXPR) {
4878 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
4879 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
4884 yyerror("Assignment to both a list and a scalar");
4888 if (type == OP_LIST &&
4889 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
4890 o->op_private & OPpLVAL_INTRO)
4893 if (type == OP_LIST || flags & OPf_PARENS ||
4894 type == OP_RV2AV || type == OP_RV2HV ||
4895 type == OP_ASLICE || type == OP_HSLICE)
4898 if (type == OP_PADAV || type == OP_PADHV)
4901 if (type == OP_RV2SV)
4908 Helper function for newASSIGNOP to detection commonality between the
4909 lhs and the rhs. Marks all variables with PL_generation. If it