3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
13 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
14 * youngest of the Old Took's daughters); and Mr. Drogo was his second
15 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
16 * either way, as the saying is, if you follow me." --the Gaffer
19 /* This file contains the functions that create, manipulate and optimize
20 * the OP structures that hold a compiled perl program.
22 * A Perl program is compiled into a tree of OPs. Each op contains
23 * structural pointers (eg to its siblings and the next op in the
24 * execution sequence), a pointer to the function that would execute the
25 * op, plus any data specific to that op. For example, an OP_CONST op
26 * points to the pp_const() function and to an SV containing the constant
27 * value. When pp_const() is executed, its job is to push that SV onto the
30 * OPs are mainly created by the newFOO() functions, which are mainly
31 * called from the parser (in perly.y) as the code is parsed. For example
32 * the Perl code $a + $b * $c would cause the equivalent of the following
33 * to be called (oversimplifying a bit):
35 * newBINOP(OP_ADD, flags,
37 * newBINOP(OP_MULTIPLY, flags, newSVREF($b), newSVREF($c))
40 * Note that during the build of miniperl, a temporary copy of this file
41 * is made, called opmini.c.
45 Perl's compiler is essentially a 3-pass compiler with interleaved phases:
49 An execution-order pass
51 The bottom-up pass is represented by all the "newOP" routines and
52 the ck_ routines. The bottom-upness is actually driven by yacc.
53 So at the point that a ck_ routine fires, we have no idea what the
54 context is, either upward in the syntax tree, or either forward or
55 backward in the execution order. (The bottom-up parser builds that
56 part of the execution order it knows about, but if you follow the "next"
57 links around, you'll find it's actually a closed loop through the
60 Whenever the bottom-up parser gets to a node that supplies context to
61 its components, it invokes that portion of the top-down pass that applies
62 to that part of the subtree (and marks the top node as processed, so
63 if a node further up supplies context, it doesn't have to take the
64 plunge again). As a particular subcase of this, as the new node is
65 built, it takes all the closed execution loops of its subcomponents
66 and links them into a new closed loop for the higher level node. But
67 it's still not the real execution order.
69 The actual execution order is not known till we get a grammar reduction
70 to a top-level unit like a subroutine or file that will be called by
71 "name" rather than via a "next" pointer. At that point, we can call
72 into peep() to do that code's portion of the 3rd pass. It has to be
73 recursive, but it's recursive on basic blocks, not on tree nodes.
81 #define CALL_PEEP(o) CALL_FPTR(PL_peepp)(aTHX_ o)
83 #if defined(PL_OP_SLAB_ALLOC)
85 #ifndef PERL_SLAB_SIZE
86 #define PERL_SLAB_SIZE 2048
90 Perl_Slab_Alloc(pTHX_ int m, size_t sz)
93 * To make incrementing use count easy PL_OpSlab is an I32 *
94 * To make inserting the link to slab PL_OpPtr is I32 **
95 * So compute size in units of sizeof(I32 *) as that is how Pl_OpPtr increments
96 * Add an overhead for pointer to slab and round up as a number of pointers
98 sz = (sz + 2*sizeof(I32 *) -1)/sizeof(I32 *);
99 if ((PL_OpSpace -= sz) < 0) {
100 PL_OpPtr = (I32 **) PerlMemShared_malloc(PERL_SLAB_SIZE*sizeof(I32*));
104 Zero(PL_OpPtr,PERL_SLAB_SIZE,I32 **);
105 /* We reserve the 0'th I32 sized chunk as a use count */
106 PL_OpSlab = (I32 *) PL_OpPtr;
107 /* Reduce size by the use count word, and by the size we need.
108 * Latter is to mimic the '-=' in the if() above
110 PL_OpSpace = PERL_SLAB_SIZE - (sizeof(I32)+sizeof(I32 **)-1)/sizeof(I32 **) - sz;
111 /* Allocation pointer starts at the top.
112 Theory: because we build leaves before trunk allocating at end
113 means that at run time access is cache friendly upward
115 PL_OpPtr += PERL_SLAB_SIZE;
117 assert( PL_OpSpace >= 0 );
118 /* Move the allocation pointer down */
120 assert( PL_OpPtr > (I32 **) PL_OpSlab );
121 *PL_OpPtr = PL_OpSlab; /* Note which slab it belongs to */
122 (*PL_OpSlab)++; /* Increment use count of slab */
123 assert( PL_OpPtr+sz <= ((I32 **) PL_OpSlab + PERL_SLAB_SIZE) );
124 assert( *PL_OpSlab > 0 );
125 return (void *)(PL_OpPtr + 1);
129 Perl_Slab_Free(pTHX_ void *op)
131 I32 * const * const ptr = (I32 **) op;
132 I32 * const slab = ptr[-1];
133 assert( ptr-1 > (I32 **) slab );
134 assert( ptr < ( (I32 **) slab + PERL_SLAB_SIZE) );
136 if (--(*slab) == 0) {
138 # define PerlMemShared PerlMem
141 PerlMemShared_free(slab);
142 if (slab == PL_OpSlab) {
149 * In the following definition, the ", Nullop" is just to make the compiler
150 * think the expression is of the right type: croak actually does a Siglongjmp.
152 #define CHECKOP(type,o) \
153 ((PL_op_mask && PL_op_mask[type]) \
154 ? ( op_free((OP*)o), \
155 Perl_croak(aTHX_ "'%s' trapped by operation mask", PL_op_desc[type]), \
157 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
159 #define RETURN_UNLIMITED_NUMBER (PERL_INT_MAX / 2)
162 S_gv_ename(pTHX_ GV *gv)
164 SV* const tmpsv = sv_newmortal();
165 gv_efullname3(tmpsv, gv, Nullch);
166 return SvPV_nolen_const(tmpsv);
170 S_no_fh_allowed(pTHX_ OP *o)
172 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
178 S_too_few_arguments(pTHX_ OP *o, const char *name)
180 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
185 S_too_many_arguments(pTHX_ OP *o, const char *name)
187 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
192 S_bad_type(pTHX_ I32 n, const char *t, const char *name, const OP *kid)
194 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
195 (int)n, name, t, OP_DESC(kid)));
199 S_no_bareword_allowed(pTHX_ const OP *o)
201 qerror(Perl_mess(aTHX_
202 "Bareword \"%"SVf"\" not allowed while \"strict subs\" in use",
206 /* "register" allocation */
209 Perl_allocmy(pTHX_ char *name)
212 const bool is_our = (PL_in_my == KEY_our);
214 /* complain about "my $<special_var>" etc etc */
218 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
219 (name[1] == '_' && (*name == '$' || name[2]))))
221 /* name[2] is true if strlen(name) > 2 */
222 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
223 /* 1999-02-27 mjd@plover.com */
225 p = strchr(name, '\0');
226 /* The next block assumes the buffer is at least 205 chars
227 long. At present, it's always at least 256 chars. */
229 strcpy(name+200, "...");
235 /* Move everything else down one character */
236 for (; p-name > 2; p--)
238 name[2] = toCTRL(name[1]);
241 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
244 /* check for duplicate declaration */
245 pad_check_dup(name, is_our, (PL_curstash ? PL_curstash : PL_defstash));
247 if (PL_in_my_stash && *name != '$') {
248 yyerror(Perl_form(aTHX_
249 "Can't declare class for non-scalar %s in \"%s\"",
250 name, is_our ? "our" : "my"));
253 /* allocate a spare slot and store the name in that slot */
255 off = pad_add_name(name,
258 /* $_ is always in main::, even with our */
259 ? (PL_curstash && !strEQ(name,"$_") ? PL_curstash : PL_defstash)
270 Perl_op_free(pTHX_ OP *o)
276 if (!o || o->op_static)
279 if (o->op_private & OPpREFCOUNTED) {
280 switch (o->op_type) {
288 refcnt = OpREFCNT_dec(o);
298 if (o->op_flags & OPf_KIDS) {
299 register OP *kid, *nextkid;
300 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
301 nextkid = kid->op_sibling; /* Get before next freeing kid */
307 type = (OPCODE)o->op_targ;
309 /* COP* is not cleared by op_clear() so that we may track line
310 * numbers etc even after null() */
311 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
316 #ifdef DEBUG_LEAKING_SCALARS
323 Perl_op_clear(pTHX_ OP *o)
327 switch (o->op_type) {
328 case OP_NULL: /* Was holding old type, if any. */
329 case OP_ENTEREVAL: /* Was holding hints. */
333 if (!(o->op_flags & OPf_REF)
334 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
340 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
341 /* not an OP_PADAV replacement */
343 if (cPADOPo->op_padix > 0) {
344 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
345 * may still exist on the pad */
346 pad_swipe(cPADOPo->op_padix, TRUE);
347 cPADOPo->op_padix = 0;
350 SvREFCNT_dec(cSVOPo->op_sv);
351 cSVOPo->op_sv = Nullsv;
355 case OP_METHOD_NAMED:
357 SvREFCNT_dec(cSVOPo->op_sv);
358 cSVOPo->op_sv = Nullsv;
361 Even if op_clear does a pad_free for the target of the op,
362 pad_free doesn't actually remove the sv that exists in the pad;
363 instead it lives on. This results in that it could be reused as
364 a target later on when the pad was reallocated.
367 pad_swipe(o->op_targ,1);
376 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
380 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
381 SvREFCNT_dec(cSVOPo->op_sv);
382 cSVOPo->op_sv = Nullsv;
385 Safefree(cPVOPo->op_pv);
386 cPVOPo->op_pv = Nullch;
390 op_free(cPMOPo->op_pmreplroot);
394 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
395 /* No GvIN_PAD_off here, because other references may still
396 * exist on the pad */
397 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
400 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
407 HV * const pmstash = PmopSTASH(cPMOPo);
408 if (pmstash && !SvIS_FREED(pmstash)) {
409 MAGIC * const mg = mg_find((SV*)pmstash, PERL_MAGIC_symtab);
411 PMOP *pmop = (PMOP*) mg->mg_obj;
412 PMOP *lastpmop = NULL;
414 if (cPMOPo == pmop) {
416 lastpmop->op_pmnext = pmop->op_pmnext;
418 mg->mg_obj = (SV*) pmop->op_pmnext;
422 pmop = pmop->op_pmnext;
426 PmopSTASH_free(cPMOPo);
428 cPMOPo->op_pmreplroot = Nullop;
429 /* we use the "SAFE" version of the PM_ macros here
430 * since sv_clean_all might release some PMOPs
431 * after PL_regex_padav has been cleared
432 * and the clearing of PL_regex_padav needs to
433 * happen before sv_clean_all
435 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
436 PM_SETRE_SAFE(cPMOPo, (REGEXP*)NULL);
438 if(PL_regex_pad) { /* We could be in destruction */
439 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
440 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
441 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
448 if (o->op_targ > 0) {
449 pad_free(o->op_targ);
455 S_cop_free(pTHX_ COP* cop)
457 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
460 if (! specialWARN(cop->cop_warnings))
461 SvREFCNT_dec(cop->cop_warnings);
462 if (! specialCopIO(cop->cop_io)) {
466 char *s = SvPV(cop->cop_io,len);
467 Perl_warn(aTHX_ "io='%.*s'",(int) len,s); /* ??? --jhi */
470 SvREFCNT_dec(cop->cop_io);
476 Perl_op_null(pTHX_ OP *o)
479 if (o->op_type == OP_NULL)
482 o->op_targ = o->op_type;
483 o->op_type = OP_NULL;
484 o->op_ppaddr = PL_ppaddr[OP_NULL];
488 Perl_op_refcnt_lock(pTHX)
495 Perl_op_refcnt_unlock(pTHX)
501 /* Contextualizers */
503 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
506 Perl_linklist(pTHX_ OP *o)
513 /* establish postfix order */
514 first = cUNOPo->op_first;
517 o->op_next = LINKLIST(first);
520 if (kid->op_sibling) {
521 kid->op_next = LINKLIST(kid->op_sibling);
522 kid = kid->op_sibling;
536 Perl_scalarkids(pTHX_ OP *o)
538 if (o && o->op_flags & OPf_KIDS) {
540 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
547 S_scalarboolean(pTHX_ OP *o)
549 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
550 if (ckWARN(WARN_SYNTAX)) {
551 const line_t oldline = CopLINE(PL_curcop);
553 if (PL_copline != NOLINE)
554 CopLINE_set(PL_curcop, PL_copline);
555 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
556 CopLINE_set(PL_curcop, oldline);
563 Perl_scalar(pTHX_ OP *o)
568 /* assumes no premature commitment */
569 if (!o || PL_error_count || (o->op_flags & OPf_WANT)
570 || o->op_type == OP_RETURN)
575 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
577 switch (o->op_type) {
579 scalar(cBINOPo->op_first);
584 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
588 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
589 if (!kPMOP->op_pmreplroot)
590 deprecate_old("implicit split to @_");
598 if (o->op_flags & OPf_KIDS) {
599 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
605 kid = cLISTOPo->op_first;
607 while ((kid = kid->op_sibling)) {
613 WITH_THR(PL_curcop = &PL_compiling);
618 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
624 WITH_THR(PL_curcop = &PL_compiling);
627 if (ckWARN(WARN_VOID))
628 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
634 Perl_scalarvoid(pTHX_ OP *o)
638 const char* useless = NULL;
642 if (o->op_type == OP_NEXTSTATE
643 || o->op_type == OP_SETSTATE
644 || o->op_type == OP_DBSTATE
645 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
646 || o->op_targ == OP_SETSTATE
647 || o->op_targ == OP_DBSTATE)))
648 PL_curcop = (COP*)o; /* for warning below */
650 /* assumes no premature commitment */
651 want = o->op_flags & OPf_WANT;
652 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
653 || o->op_type == OP_RETURN)
658 if ((o->op_private & OPpTARGET_MY)
659 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
661 return scalar(o); /* As if inside SASSIGN */
664 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
666 switch (o->op_type) {
668 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
672 if (o->op_flags & OPf_STACKED)
676 if (o->op_private == 4)
748 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
749 useless = OP_DESC(o);
753 kid = cUNOPo->op_first;
754 if (kid->op_type != OP_MATCH && kid->op_type != OP_SUBST &&
755 kid->op_type != OP_TRANS) {
758 useless = "negative pattern binding (!~)";
765 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
766 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
767 useless = "a variable";
772 if (cSVOPo->op_private & OPpCONST_STRICT)
773 no_bareword_allowed(o);
775 if (ckWARN(WARN_VOID)) {
776 useless = "a constant";
777 /* don't warn on optimised away booleans, eg
778 * use constant Foo, 5; Foo || print; */
779 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
781 /* the constants 0 and 1 are permitted as they are
782 conventionally used as dummies in constructs like
783 1 while some_condition_with_side_effects; */
784 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
786 else if (SvPOK(sv)) {
787 /* perl4's way of mixing documentation and code
788 (before the invention of POD) was based on a
789 trick to mix nroff and perl code. The trick was
790 built upon these three nroff macros being used in
791 void context. The pink camel has the details in
792 the script wrapman near page 319. */
793 if (strnEQ(SvPVX_const(sv), "di", 2) ||
794 strnEQ(SvPVX_const(sv), "ds", 2) ||
795 strnEQ(SvPVX_const(sv), "ig", 2))
800 op_null(o); /* don't execute or even remember it */
804 o->op_type = OP_PREINC; /* pre-increment is faster */
805 o->op_ppaddr = PL_ppaddr[OP_PREINC];
809 o->op_type = OP_PREDEC; /* pre-decrement is faster */
810 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
814 o->op_type = OP_I_PREINC; /* pre-increment is faster */
815 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
819 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
820 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
829 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
834 if (o->op_flags & OPf_STACKED)
841 if (!(o->op_flags & OPf_KIDS))
852 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
859 /* all requires must return a boolean value */
860 o->op_flags &= ~OPf_WANT;
865 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
866 if (!kPMOP->op_pmreplroot)
867 deprecate_old("implicit split to @_");
871 if (useless && ckWARN(WARN_VOID))
872 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
877 Perl_listkids(pTHX_ OP *o)
879 if (o && o->op_flags & OPf_KIDS) {
881 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
888 Perl_list(pTHX_ OP *o)
893 /* assumes no premature commitment */
894 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
895 || o->op_type == OP_RETURN)
900 if ((o->op_private & OPpTARGET_MY)
901 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
903 return o; /* As if inside SASSIGN */
906 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
908 switch (o->op_type) {
911 list(cBINOPo->op_first);
916 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
924 if (!(o->op_flags & OPf_KIDS))
926 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
927 list(cBINOPo->op_first);
928 return gen_constant_list(o);
935 kid = cLISTOPo->op_first;
937 while ((kid = kid->op_sibling)) {
943 WITH_THR(PL_curcop = &PL_compiling);
947 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
953 WITH_THR(PL_curcop = &PL_compiling);
956 /* all requires must return a boolean value */
957 o->op_flags &= ~OPf_WANT;
964 Perl_scalarseq(pTHX_ OP *o)
967 if (o->op_type == OP_LINESEQ ||
968 o->op_type == OP_SCOPE ||
969 o->op_type == OP_LEAVE ||
970 o->op_type == OP_LEAVETRY)
973 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
974 if (kid->op_sibling) {
978 PL_curcop = &PL_compiling;
980 o->op_flags &= ~OPf_PARENS;
981 if (PL_hints & HINT_BLOCK_SCOPE)
982 o->op_flags |= OPf_PARENS;
985 o = newOP(OP_STUB, 0);
990 S_modkids(pTHX_ OP *o, I32 type)
992 if (o && o->op_flags & OPf_KIDS) {
994 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1000 /* Propagate lvalue ("modifiable") context to an op and its children.
1001 * 'type' represents the context type, roughly based on the type of op that
1002 * would do the modifying, although local() is represented by OP_NULL.
1003 * It's responsible for detecting things that can't be modified, flag
1004 * things that need to behave specially in an lvalue context (e.g., "$$x = 5"
1005 * might have to vivify a reference in $x), and so on.
1007 * For example, "$a+1 = 2" would cause mod() to be called with o being
1008 * OP_ADD and type being OP_SASSIGN, and would output an error.
1012 Perl_mod(pTHX_ OP *o, I32 type)
1016 /* -1 = error on localize, 0 = ignore localize, 1 = ok to localize */
1019 if (!o || PL_error_count)
1022 if ((o->op_private & OPpTARGET_MY)
1023 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1028 switch (o->op_type) {
1034 if (!(o->op_private & (OPpCONST_ARYBASE)))
1036 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1037 PL_compiling.cop_arybase = (I32)SvIV(cSVOPx(PL_eval_start)->op_sv);
1041 SAVEI32(PL_compiling.cop_arybase);
1042 PL_compiling.cop_arybase = 0;
1044 else if (type == OP_REFGEN)
1047 Perl_croak(aTHX_ "That use of $[ is unsupported");
1050 if (o->op_flags & OPf_PARENS)
1054 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1055 !(o->op_flags & OPf_STACKED)) {
1056 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1057 /* The default is to set op_private to the number of children,
1058 which for a UNOP such as RV2CV is always 1. And w're using
1059 the bit for a flag in RV2CV, so we need it clear. */
1060 o->op_private &= ~1;
1061 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1062 assert(cUNOPo->op_first->op_type == OP_NULL);
1063 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1066 else if (o->op_private & OPpENTERSUB_NOMOD)
1068 else { /* lvalue subroutine call */
1069 o->op_private |= OPpLVAL_INTRO;
1070 PL_modcount = RETURN_UNLIMITED_NUMBER;
1071 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1072 /* Backward compatibility mode: */
1073 o->op_private |= OPpENTERSUB_INARGS;
1076 else { /* Compile-time error message: */
1077 OP *kid = cUNOPo->op_first;
1081 if (kid->op_type == OP_PUSHMARK)
1083 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1085 "panic: unexpected lvalue entersub "
1086 "args: type/targ %ld:%"UVuf,
1087 (long)kid->op_type, (UV)kid->op_targ);
1088 kid = kLISTOP->op_first;
1090 while (kid->op_sibling)
1091 kid = kid->op_sibling;
1092 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1094 if (kid->op_type == OP_METHOD_NAMED
1095 || kid->op_type == OP_METHOD)
1099 NewOp(1101, newop, 1, UNOP);
1100 newop->op_type = OP_RV2CV;
1101 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1102 newop->op_first = Nullop;
1103 newop->op_next = (OP*)newop;
1104 kid->op_sibling = (OP*)newop;
1105 newop->op_private |= OPpLVAL_INTRO;
1106 newop->op_private &= ~1;
1110 if (kid->op_type != OP_RV2CV)
1112 "panic: unexpected lvalue entersub "
1113 "entry via type/targ %ld:%"UVuf,
1114 (long)kid->op_type, (UV)kid->op_targ);
1115 kid->op_private |= OPpLVAL_INTRO;
1116 break; /* Postpone until runtime */
1120 kid = kUNOP->op_first;
1121 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1122 kid = kUNOP->op_first;
1123 if (kid->op_type == OP_NULL)
1125 "Unexpected constant lvalue entersub "
1126 "entry via type/targ %ld:%"UVuf,
1127 (long)kid->op_type, (UV)kid->op_targ);
1128 if (kid->op_type != OP_GV) {
1129 /* Restore RV2CV to check lvalueness */
1131 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1132 okid->op_next = kid->op_next;
1133 kid->op_next = okid;
1136 okid->op_next = Nullop;
1137 okid->op_type = OP_RV2CV;
1139 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1140 okid->op_private |= OPpLVAL_INTRO;
1141 okid->op_private &= ~1;
1145 cv = GvCV(kGVOP_gv);
1155 /* grep, foreach, subcalls, refgen, m//g */
1156 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN
1157 || type == OP_MATCH)
1159 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1160 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1162 : (o->op_type == OP_ENTERSUB
1163 ? "non-lvalue subroutine call"
1165 type ? PL_op_desc[type] : "local"));
1179 case OP_RIGHT_SHIFT:
1188 if (!(o->op_flags & OPf_STACKED))
1195 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1201 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1202 PL_modcount = RETURN_UNLIMITED_NUMBER;
1203 return o; /* Treat \(@foo) like ordinary list. */
1207 if (scalar_mod_type(o, type))
1209 ref(cUNOPo->op_first, o->op_type);
1213 if (type == OP_LEAVESUBLV)
1214 o->op_private |= OPpMAYBE_LVSUB;
1220 PL_modcount = RETURN_UNLIMITED_NUMBER;
1223 ref(cUNOPo->op_first, o->op_type);
1228 PL_hints |= HINT_BLOCK_SCOPE;
1243 PL_modcount = RETURN_UNLIMITED_NUMBER;
1244 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1245 return o; /* Treat \(@foo) like ordinary list. */
1246 if (scalar_mod_type(o, type))
1248 if (type == OP_LEAVESUBLV)
1249 o->op_private |= OPpMAYBE_LVSUB;
1253 if (!type) /* local() */
1254 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1255 PAD_COMPNAME_PV(o->op_targ));
1263 if (type != OP_SASSIGN)
1267 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1272 if (type == OP_LEAVESUBLV)
1273 o->op_private |= OPpMAYBE_LVSUB;
1275 pad_free(o->op_targ);
1276 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1277 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1278 if (o->op_flags & OPf_KIDS)
1279 mod(cBINOPo->op_first->op_sibling, type);
1284 ref(cBINOPo->op_first, o->op_type);
1285 if (type == OP_ENTERSUB &&
1286 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1287 o->op_private |= OPpLVAL_DEFER;
1288 if (type == OP_LEAVESUBLV)
1289 o->op_private |= OPpMAYBE_LVSUB;
1299 if (o->op_flags & OPf_KIDS)
1300 mod(cLISTOPo->op_last, type);
1305 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1307 else if (!(o->op_flags & OPf_KIDS))
1309 if (o->op_targ != OP_LIST) {
1310 mod(cBINOPo->op_first, type);
1316 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1321 if (type != OP_LEAVESUBLV)
1323 break; /* mod()ing was handled by ck_return() */
1326 /* [20011101.069] File test operators interpret OPf_REF to mean that
1327 their argument is a filehandle; thus \stat(".") should not set
1329 if (type == OP_REFGEN &&
1330 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1333 if (type != OP_LEAVESUBLV)
1334 o->op_flags |= OPf_MOD;
1336 if (type == OP_AASSIGN || type == OP_SASSIGN)
1337 o->op_flags |= OPf_SPECIAL|OPf_REF;
1338 else if (!type) { /* local() */
1341 o->op_private |= OPpLVAL_INTRO;
1342 o->op_flags &= ~OPf_SPECIAL;
1343 PL_hints |= HINT_BLOCK_SCOPE;
1348 if (ckWARN(WARN_SYNTAX)) {
1349 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
1350 "Useless localization of %s", OP_DESC(o));
1354 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1355 && type != OP_LEAVESUBLV)
1356 o->op_flags |= OPf_REF;
1361 S_scalar_mod_type(pTHX_ const OP *o, I32 type)
1365 if (o->op_type == OP_RV2GV)
1389 case OP_RIGHT_SHIFT:
1408 S_is_handle_constructor(pTHX_ const OP *o, I32 numargs)
1410 switch (o->op_type) {
1418 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1431 Perl_refkids(pTHX_ OP *o, I32 type)
1433 if (o && o->op_flags & OPf_KIDS) {
1435 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1442 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
1447 if (!o || PL_error_count)
1450 switch (o->op_type) {
1452 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1453 !(o->op_flags & OPf_STACKED)) {
1454 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1455 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1456 assert(cUNOPo->op_first->op_type == OP_NULL);
1457 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1458 o->op_flags |= OPf_SPECIAL;
1459 o->op_private &= ~1;
1464 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1465 doref(kid, type, set_op_ref);
1468 if (type == OP_DEFINED)
1469 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1470 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1473 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1474 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1475 : type == OP_RV2HV ? OPpDEREF_HV
1477 o->op_flags |= OPf_MOD;
1482 o->op_flags |= OPf_MOD; /* XXX ??? */
1488 o->op_flags |= OPf_REF;
1491 if (type == OP_DEFINED)
1492 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1493 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1499 o->op_flags |= OPf_REF;
1504 if (!(o->op_flags & OPf_KIDS))
1506 doref(cBINOPo->op_first, type, set_op_ref);
1510 doref(cBINOPo->op_first, o->op_type, set_op_ref);
1511 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1512 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1513 : type == OP_RV2HV ? OPpDEREF_HV
1515 o->op_flags |= OPf_MOD;
1525 if (!(o->op_flags & OPf_KIDS))
1527 doref(cLISTOPo->op_last, type, set_op_ref);
1537 S_dup_attrlist(pTHX_ OP *o)
1541 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1542 * where the first kid is OP_PUSHMARK and the remaining ones
1543 * are OP_CONST. We need to push the OP_CONST values.
1545 if (o->op_type == OP_CONST)
1546 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1548 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1550 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1551 if (o->op_type == OP_CONST)
1552 rop = append_elem(OP_LIST, rop,
1553 newSVOP(OP_CONST, o->op_flags,
1554 SvREFCNT_inc(cSVOPo->op_sv)));
1561 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1566 /* fake up C<use attributes $pkg,$rv,@attrs> */
1567 ENTER; /* need to protect against side-effects of 'use' */
1569 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1571 #define ATTRSMODULE "attributes"
1572 #define ATTRSMODULE_PM "attributes.pm"
1575 /* Don't force the C<use> if we don't need it. */
1576 SV * const * const svp = hv_fetch(GvHVn(PL_incgv), ATTRSMODULE_PM,
1577 sizeof(ATTRSMODULE_PM)-1, 0);
1578 if (svp && *svp != &PL_sv_undef)
1579 ; /* already in %INC */
1581 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1582 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1586 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1587 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1589 prepend_elem(OP_LIST,
1590 newSVOP(OP_CONST, 0, stashsv),
1591 prepend_elem(OP_LIST,
1592 newSVOP(OP_CONST, 0,
1594 dup_attrlist(attrs))));
1600 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1602 OP *pack, *imop, *arg;
1608 assert(target->op_type == OP_PADSV ||
1609 target->op_type == OP_PADHV ||
1610 target->op_type == OP_PADAV);
1612 /* Ensure that attributes.pm is loaded. */
1613 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1615 /* Need package name for method call. */
1616 pack = newSVOP(OP_CONST, 0, newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1618 /* Build up the real arg-list. */
1619 stashsv = stash ? newSVhek(HvNAME_HEK(stash)) : &PL_sv_no;
1621 arg = newOP(OP_PADSV, 0);
1622 arg->op_targ = target->op_targ;
1623 arg = prepend_elem(OP_LIST,
1624 newSVOP(OP_CONST, 0, stashsv),
1625 prepend_elem(OP_LIST,
1626 newUNOP(OP_REFGEN, 0,
1627 mod(arg, OP_REFGEN)),
1628 dup_attrlist(attrs)));
1630 /* Fake up a method call to import */
1631 meth = newSVpvs_share("import");
1632 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1633 append_elem(OP_LIST,
1634 prepend_elem(OP_LIST, pack, list(arg)),
1635 newSVOP(OP_METHOD_NAMED, 0, meth)));
1636 imop->op_private |= OPpENTERSUB_NOMOD;
1638 /* Combine the ops. */
1639 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1643 =notfor apidoc apply_attrs_string
1645 Attempts to apply a list of attributes specified by the C<attrstr> and
1646 C<len> arguments to the subroutine identified by the C<cv> argument which
1647 is expected to be associated with the package identified by the C<stashpv>
1648 argument (see L<attributes>). It gets this wrong, though, in that it
1649 does not correctly identify the boundaries of the individual attribute
1650 specifications within C<attrstr>. This is not really intended for the
1651 public API, but has to be listed here for systems such as AIX which
1652 need an explicit export list for symbols. (It's called from XS code
1653 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1654 to respect attribute syntax properly would be welcome.
1660 Perl_apply_attrs_string(pTHX_ const char *stashpv, CV *cv,
1661 const char *attrstr, STRLEN len)
1666 len = strlen(attrstr);
1670 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1672 const char * const sstr = attrstr;
1673 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1674 attrs = append_elem(OP_LIST, attrs,
1675 newSVOP(OP_CONST, 0,
1676 newSVpvn(sstr, attrstr-sstr)));
1680 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1681 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1),
1682 Nullsv, prepend_elem(OP_LIST,
1683 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1684 prepend_elem(OP_LIST,
1685 newSVOP(OP_CONST, 0,
1691 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1695 if (!o || PL_error_count)
1699 if (type == OP_LIST) {
1701 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1702 my_kid(kid, attrs, imopsp);
1703 } else if (type == OP_UNDEF) {
1705 } else if (type == OP_RV2SV || /* "our" declaration */
1707 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1708 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1709 yyerror(Perl_form(aTHX_ "Can't declare %s in %s",
1710 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1712 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
1714 PL_in_my_stash = NULL;
1715 apply_attrs(GvSTASH(gv),
1716 (type == OP_RV2SV ? GvSV(gv) :
1717 type == OP_RV2AV ? (SV*)GvAV(gv) :
1718 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1721 o->op_private |= OPpOUR_INTRO;
1724 else if (type != OP_PADSV &&
1727 type != OP_PUSHMARK)
1729 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1731 PL_in_my == KEY_our ? "our" : "my"));
1734 else if (attrs && type != OP_PUSHMARK) {
1738 PL_in_my_stash = NULL;
1740 /* check for C<my Dog $spot> when deciding package */
1741 stash = PAD_COMPNAME_TYPE(o->op_targ);
1743 stash = PL_curstash;
1744 apply_attrs_my(stash, o, attrs, imopsp);
1746 o->op_flags |= OPf_MOD;
1747 o->op_private |= OPpLVAL_INTRO;
1752 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1755 int maybe_scalar = 0;
1757 /* [perl #17376]: this appears to be premature, and results in code such as
1758 C< our(%x); > executing in list mode rather than void mode */
1760 if (o->op_flags & OPf_PARENS)
1770 o = my_kid(o, attrs, &rops);
1772 if (maybe_scalar && o->op_type == OP_PADSV) {
1773 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1774 o->op_private |= OPpLVAL_INTRO;
1777 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1780 PL_in_my_stash = NULL;
1785 Perl_my(pTHX_ OP *o)
1787 return my_attrs(o, Nullop);
1791 Perl_sawparens(pTHX_ OP *o)
1794 o->op_flags |= OPf_PARENS;
1799 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1804 if ( (left->op_type == OP_RV2AV ||
1805 left->op_type == OP_RV2HV ||
1806 left->op_type == OP_PADAV ||
1807 left->op_type == OP_PADHV)
1808 && ckWARN(WARN_MISC))
1810 const char * const desc = PL_op_desc[(right->op_type == OP_SUBST ||
1811 right->op_type == OP_TRANS)
1812 ? right->op_type : OP_MATCH];
1813 const char * const sample = ((left->op_type == OP_RV2AV ||
1814 left->op_type == OP_PADAV)
1815 ? "@array" : "%hash");
1816 Perl_warner(aTHX_ packWARN(WARN_MISC),
1817 "Applying %s to %s will act on scalar(%s)",
1818 desc, sample, sample);
1821 if (right->op_type == OP_CONST &&
1822 cSVOPx(right)->op_private & OPpCONST_BARE &&
1823 cSVOPx(right)->op_private & OPpCONST_STRICT)
1825 no_bareword_allowed(right);
1828 ismatchop = right->op_type == OP_MATCH ||
1829 right->op_type == OP_SUBST ||
1830 right->op_type == OP_TRANS;
1831 if (ismatchop && right->op_private & OPpTARGET_MY) {
1833 right->op_private &= ~OPpTARGET_MY;
1835 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
1836 right->op_flags |= OPf_STACKED;
1837 /* s/// and tr/// modify their arg.
1838 * m//g also indirectly modifies the arg by setting pos magic on it */
1839 if ( (right->op_type == OP_MATCH &&
1840 (cPMOPx(right)->op_pmflags & PMf_GLOBAL))
1841 || (right->op_type == OP_SUBST)
1842 || (right->op_type == OP_TRANS &&
1843 ! (right->op_private & OPpTRANS_IDENTICAL))
1845 left = mod(left, right->op_type);
1846 if (right->op_type == OP_TRANS)
1847 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1849 o = prepend_elem(right->op_type, scalar(left), right);
1851 return newUNOP(OP_NOT, 0, scalar(o));
1855 return bind_match(type, left,
1856 pmruntime(newPMOP(OP_MATCH, 0), right, 0));
1860 Perl_invert(pTHX_ OP *o)
1864 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1865 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1869 Perl_scope(pTHX_ OP *o)
1873 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1874 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1875 o->op_type = OP_LEAVE;
1876 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1878 else if (o->op_type == OP_LINESEQ) {
1880 o->op_type = OP_SCOPE;
1881 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1882 kid = ((LISTOP*)o)->op_first;
1883 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1886 /* The following deals with things like 'do {1 for 1}' */
1887 kid = kid->op_sibling;
1889 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
1894 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1900 Perl_block_start(pTHX_ int full)
1902 const int retval = PL_savestack_ix;
1903 pad_block_start(full);
1905 PL_hints &= ~HINT_BLOCK_SCOPE;
1906 SAVESPTR(PL_compiling.cop_warnings);
1907 if (! specialWARN(PL_compiling.cop_warnings)) {
1908 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1909 SAVEFREESV(PL_compiling.cop_warnings) ;
1911 SAVESPTR(PL_compiling.cop_io);
1912 if (! specialCopIO(PL_compiling.cop_io)) {
1913 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1914 SAVEFREESV(PL_compiling.cop_io) ;
1920 Perl_block_end(pTHX_ I32 floor, OP *seq)
1922 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1923 OP* const retval = scalarseq(seq);
1925 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
1927 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1935 const I32 offset = pad_findmy("$_");
1936 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS(offset) & SVpad_OUR) {
1937 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1940 OP * const o = newOP(OP_PADSV, 0);
1941 o->op_targ = offset;
1947 Perl_newPROG(pTHX_ OP *o)
1952 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1953 ((PL_in_eval & EVAL_KEEPERR)
1954 ? OPf_SPECIAL : 0), o);
1955 PL_eval_start = linklist(PL_eval_root);
1956 PL_eval_root->op_private |= OPpREFCOUNTED;
1957 OpREFCNT_set(PL_eval_root, 1);
1958 PL_eval_root->op_next = 0;
1959 CALL_PEEP(PL_eval_start);
1962 if (o->op_type == OP_STUB) {
1963 PL_comppad_name = 0;
1968 PL_main_root = scope(sawparens(scalarvoid(o)));
1969 PL_curcop = &PL_compiling;
1970 PL_main_start = LINKLIST(PL_main_root);
1971 PL_main_root->op_private |= OPpREFCOUNTED;
1972 OpREFCNT_set(PL_main_root, 1);
1973 PL_main_root->op_next = 0;
1974 CALL_PEEP(PL_main_start);
1977 /* Register with debugger */
1979 CV * const cv = get_cv("DB::postponed", FALSE);
1983 XPUSHs((SV*)CopFILEGV(&PL_compiling));
1985 call_sv((SV*)cv, G_DISCARD);
1992 Perl_localize(pTHX_ OP *o, I32 lex)
1994 if (o->op_flags & OPf_PARENS)
1995 /* [perl #17376]: this appears to be premature, and results in code such as
1996 C< our(%x); > executing in list mode rather than void mode */
2003 if ( PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ','
2004 && ckWARN(WARN_PARENTHESIS))
2006 char *s = PL_bufptr;
2009 /* some heuristics to detect a potential error */
2010 while (*s && (strchr(", \t\n", *s)))
2014 if (*s && strchr("@$%*", *s) && *++s
2015 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2018 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2020 while (*s && (strchr(", \t\n", *s)))
2026 if (sigil && (*s == ';' || *s == '=')) {
2027 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2028 "Parentheses missing around \"%s\" list",
2029 lex ? (PL_in_my == KEY_our ? "our" : "my")
2037 o = mod(o, OP_NULL); /* a bit kludgey */
2039 PL_in_my_stash = NULL;
2044 Perl_jmaybe(pTHX_ OP *o)
2046 if (o->op_type == OP_LIST) {
2048 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", GV_ADD, SVt_PV))),
2049 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2055 Perl_fold_constants(pTHX_ register OP *o)
2059 I32 type = o->op_type;
2062 if (PL_opargs[type] & OA_RETSCALAR)
2064 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2065 o->op_targ = pad_alloc(type, SVs_PADTMP);
2067 /* integerize op, unless it happens to be C<-foo>.
2068 * XXX should pp_i_negate() do magic string negation instead? */
2069 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2070 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2071 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2073 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2076 if (!(PL_opargs[type] & OA_FOLDCONST))
2081 /* XXX might want a ck_negate() for this */
2082 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2093 /* XXX what about the numeric ops? */
2094 if (PL_hints & HINT_LOCALE)
2099 goto nope; /* Don't try to run w/ errors */
2101 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2102 if ((curop->op_type != OP_CONST ||
2103 (curop->op_private & OPpCONST_BARE)) &&
2104 curop->op_type != OP_LIST &&
2105 curop->op_type != OP_SCALAR &&
2106 curop->op_type != OP_NULL &&
2107 curop->op_type != OP_PUSHMARK)
2113 curop = LINKLIST(o);
2117 sv = *(PL_stack_sp--);
2118 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2119 pad_swipe(o->op_targ, FALSE);
2120 else if (SvTEMP(sv)) { /* grab mortal temp? */
2121 (void)SvREFCNT_inc(sv);
2125 if (type == OP_RV2GV)
2126 return newGVOP(OP_GV, 0, (GV*)sv);
2127 return newSVOP(OP_CONST, 0, sv);
2134 Perl_gen_constant_list(pTHX_ register OP *o)
2138 const I32 oldtmps_floor = PL_tmps_floor;
2142 return o; /* Don't attempt to run with errors */
2144 PL_op = curop = LINKLIST(o);
2151 PL_tmps_floor = oldtmps_floor;
2153 o->op_type = OP_RV2AV;
2154 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2155 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2156 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2157 o->op_opt = 0; /* needs to be revisited in peep() */
2158 curop = ((UNOP*)o)->op_first;
2159 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
2166 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2169 if (!o || o->op_type != OP_LIST)
2170 o = newLISTOP(OP_LIST, 0, o, Nullop);
2172 o->op_flags &= ~OPf_WANT;
2174 if (!(PL_opargs[type] & OA_MARK))
2175 op_null(cLISTOPo->op_first);
2177 o->op_type = (OPCODE)type;
2178 o->op_ppaddr = PL_ppaddr[type];
2179 o->op_flags |= flags;
2181 o = CHECKOP(type, o);
2182 if (o->op_type != (unsigned)type)
2185 return fold_constants(o);
2188 /* List constructors */
2191 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2199 if (first->op_type != (unsigned)type
2200 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2202 return newLISTOP(type, 0, first, last);
2205 if (first->op_flags & OPf_KIDS)
2206 ((LISTOP*)first)->op_last->op_sibling = last;
2208 first->op_flags |= OPf_KIDS;
2209 ((LISTOP*)first)->op_first = last;
2211 ((LISTOP*)first)->op_last = last;
2216 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2224 if (first->op_type != (unsigned)type)
2225 return prepend_elem(type, (OP*)first, (OP*)last);
2227 if (last->op_type != (unsigned)type)
2228 return append_elem(type, (OP*)first, (OP*)last);
2230 first->op_last->op_sibling = last->op_first;
2231 first->op_last = last->op_last;
2232 first->op_flags |= (last->op_flags & OPf_KIDS);
2240 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2248 if (last->op_type == (unsigned)type) {
2249 if (type == OP_LIST) { /* already a PUSHMARK there */
2250 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2251 ((LISTOP*)last)->op_first->op_sibling = first;
2252 if (!(first->op_flags & OPf_PARENS))
2253 last->op_flags &= ~OPf_PARENS;
2256 if (!(last->op_flags & OPf_KIDS)) {
2257 ((LISTOP*)last)->op_last = first;
2258 last->op_flags |= OPf_KIDS;
2260 first->op_sibling = ((LISTOP*)last)->op_first;
2261 ((LISTOP*)last)->op_first = first;
2263 last->op_flags |= OPf_KIDS;
2267 return newLISTOP(type, 0, first, last);
2273 Perl_newNULLLIST(pTHX)
2275 return newOP(OP_STUB, 0);
2279 Perl_force_list(pTHX_ OP *o)
2281 if (!o || o->op_type != OP_LIST)
2282 o = newLISTOP(OP_LIST, 0, o, Nullop);
2288 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2293 NewOp(1101, listop, 1, LISTOP);
2295 listop->op_type = (OPCODE)type;
2296 listop->op_ppaddr = PL_ppaddr[type];
2299 listop->op_flags = (U8)flags;
2303 else if (!first && last)
2306 first->op_sibling = last;
2307 listop->op_first = first;
2308 listop->op_last = last;
2309 if (type == OP_LIST) {
2310 OP* const pushop = newOP(OP_PUSHMARK, 0);
2311 pushop->op_sibling = first;
2312 listop->op_first = pushop;
2313 listop->op_flags |= OPf_KIDS;
2315 listop->op_last = pushop;
2318 return CHECKOP(type, listop);
2322 Perl_newOP(pTHX_ I32 type, I32 flags)
2326 NewOp(1101, o, 1, OP);
2327 o->op_type = (OPCODE)type;
2328 o->op_ppaddr = PL_ppaddr[type];
2329 o->op_flags = (U8)flags;
2332 o->op_private = (U8)(0 | (flags >> 8));
2333 if (PL_opargs[type] & OA_RETSCALAR)
2335 if (PL_opargs[type] & OA_TARGET)
2336 o->op_targ = pad_alloc(type, SVs_PADTMP);
2337 return CHECKOP(type, o);
2341 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2347 first = newOP(OP_STUB, 0);
2348 if (PL_opargs[type] & OA_MARK)
2349 first = force_list(first);
2351 NewOp(1101, unop, 1, UNOP);
2352 unop->op_type = (OPCODE)type;
2353 unop->op_ppaddr = PL_ppaddr[type];
2354 unop->op_first = first;
2355 unop->op_flags = (U8)(flags | OPf_KIDS);
2356 unop->op_private = (U8)(1 | (flags >> 8));
2357 unop = (UNOP*) CHECKOP(type, unop);
2361 return fold_constants((OP *) unop);
2365 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2369 NewOp(1101, binop, 1, BINOP);
2372 first = newOP(OP_NULL, 0);
2374 binop->op_type = (OPCODE)type;
2375 binop->op_ppaddr = PL_ppaddr[type];
2376 binop->op_first = first;
2377 binop->op_flags = (U8)(flags | OPf_KIDS);
2380 binop->op_private = (U8)(1 | (flags >> 8));
2383 binop->op_private = (U8)(2 | (flags >> 8));
2384 first->op_sibling = last;
2387 binop = (BINOP*)CHECKOP(type, binop);
2388 if (binop->op_next || binop->op_type != (OPCODE)type)
2391 binop->op_last = binop->op_first->op_sibling;
2393 return fold_constants((OP *)binop);
2396 static int uvcompare(const void *a, const void *b) __attribute__nonnull__(1) __attribute__nonnull__(2) __attribute__pure__;
2397 static int uvcompare(const void *a, const void *b)
2399 if (*((const UV *)a) < (*(const UV *)b))
2401 if (*((const UV *)a) > (*(const UV *)b))
2403 if (*((const UV *)a+1) < (*(const UV *)b+1))
2405 if (*((const UV *)a+1) > (*(const UV *)b+1))
2411 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2413 SV * const tstr = ((SVOP*)expr)->op_sv;
2414 SV * const rstr = ((SVOP*)repl)->op_sv;
2417 const U8 *t = (U8*)SvPV_const(tstr, tlen);
2418 const U8 *r = (U8*)SvPV_const(rstr, rlen);
2422 register short *tbl;
2424 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
2425 const I32 squash = o->op_private & OPpTRANS_SQUASH;
2426 I32 del = o->op_private & OPpTRANS_DELETE;
2427 PL_hints |= HINT_BLOCK_SCOPE;
2430 o->op_private |= OPpTRANS_FROM_UTF;
2433 o->op_private |= OPpTRANS_TO_UTF;
2435 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2436 SV* const listsv = newSVpvs("# comment\n");
2438 const U8* tend = t + tlen;
2439 const U8* rend = r + rlen;
2453 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2454 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2460 t = tsave = bytes_to_utf8(t, &len);
2463 if (!to_utf && rlen) {
2465 r = rsave = bytes_to_utf8(r, &len);
2469 /* There are several snags with this code on EBCDIC:
2470 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2471 2. scan_const() in toke.c has encoded chars in native encoding which makes
2472 ranges at least in EBCDIC 0..255 range the bottom odd.
2476 U8 tmpbuf[UTF8_MAXBYTES+1];
2479 Newx(cp, 2*tlen, UV);
2481 transv = newSVpvs("");
2483 cp[2*i] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2485 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2487 cp[2*i+1] = utf8n_to_uvuni(t, tend-t, &ulen, 0);
2491 cp[2*i+1] = cp[2*i];
2495 qsort(cp, i, 2*sizeof(UV), uvcompare);
2496 for (j = 0; j < i; j++) {
2498 diff = val - nextmin;
2500 t = uvuni_to_utf8(tmpbuf,nextmin);
2501 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2503 U8 range_mark = UTF_TO_NATIVE(0xff);
2504 t = uvuni_to_utf8(tmpbuf, val - 1);
2505 sv_catpvn(transv, (char *)&range_mark, 1);
2506 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2513 t = uvuni_to_utf8(tmpbuf,nextmin);
2514 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2516 U8 range_mark = UTF_TO_NATIVE(0xff);
2517 sv_catpvn(transv, (char *)&range_mark, 1);
2519 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2520 UNICODE_ALLOW_SUPER);
2521 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2522 t = (const U8*)SvPVX_const(transv);
2523 tlen = SvCUR(transv);
2527 else if (!rlen && !del) {
2528 r = t; rlen = tlen; rend = tend;
2531 if ((!rlen && !del) || t == r ||
2532 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2534 o->op_private |= OPpTRANS_IDENTICAL;
2538 while (t < tend || tfirst <= tlast) {
2539 /* see if we need more "t" chars */
2540 if (tfirst > tlast) {
2541 tfirst = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2543 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2545 tlast = (I32)utf8n_to_uvuni(t, tend - t, &ulen, 0);
2552 /* now see if we need more "r" chars */
2553 if (rfirst > rlast) {
2555 rfirst = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2557 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2559 rlast = (I32)utf8n_to_uvuni(r, rend - r, &ulen, 0);
2568 rfirst = rlast = 0xffffffff;
2572 /* now see which range will peter our first, if either. */
2573 tdiff = tlast - tfirst;
2574 rdiff = rlast - rfirst;
2581 if (rfirst == 0xffffffff) {
2582 diff = tdiff; /* oops, pretend rdiff is infinite */
2584 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2585 (long)tfirst, (long)tlast);
2587 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2591 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2592 (long)tfirst, (long)(tfirst + diff),
2595 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2596 (long)tfirst, (long)rfirst);
2598 if (rfirst + diff > max)
2599 max = rfirst + diff;
2601 grows = (tfirst < rfirst &&
2602 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2614 else if (max > 0xff)
2619 Safefree(cPVOPo->op_pv);
2620 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2621 SvREFCNT_dec(listsv);
2623 SvREFCNT_dec(transv);
2625 if (!del && havefinal && rlen)
2626 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2627 newSVuv((UV)final), 0);
2630 o->op_private |= OPpTRANS_GROWS;
2642 tbl = (short*)cPVOPo->op_pv;
2644 Zero(tbl, 256, short);
2645 for (i = 0; i < (I32)tlen; i++)
2647 for (i = 0, j = 0; i < 256; i++) {
2649 if (j >= (I32)rlen) {
2658 if (i < 128 && r[j] >= 128)
2668 o->op_private |= OPpTRANS_IDENTICAL;
2670 else if (j >= (I32)rlen)
2673 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2674 tbl[0x100] = (short)(rlen - j);
2675 for (i=0; i < (I32)rlen - j; i++)
2676 tbl[0x101+i] = r[j+i];
2680 if (!rlen && !del) {
2683 o->op_private |= OPpTRANS_IDENTICAL;
2685 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2686 o->op_private |= OPpTRANS_IDENTICAL;
2688 for (i = 0; i < 256; i++)
2690 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2691 if (j >= (I32)rlen) {
2693 if (tbl[t[i]] == -1)
2699 if (tbl[t[i]] == -1) {
2700 if (t[i] < 128 && r[j] >= 128)
2707 o->op_private |= OPpTRANS_GROWS;
2715 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2720 NewOp(1101, pmop, 1, PMOP);
2721 pmop->op_type = (OPCODE)type;
2722 pmop->op_ppaddr = PL_ppaddr[type];
2723 pmop->op_flags = (U8)flags;
2724 pmop->op_private = (U8)(0 | (flags >> 8));
2726 if (PL_hints & HINT_RE_TAINT)
2727 pmop->op_pmpermflags |= PMf_RETAINT;
2728 if (PL_hints & HINT_LOCALE)
2729 pmop->op_pmpermflags |= PMf_LOCALE;
2730 pmop->op_pmflags = pmop->op_pmpermflags;
2733 if (av_len((AV*) PL_regex_pad[0]) > -1) {
2734 SV * const repointer = av_pop((AV*)PL_regex_pad[0]);
2735 pmop->op_pmoffset = SvIV(repointer);
2736 SvREPADTMP_off(repointer);
2737 sv_setiv(repointer,0);
2739 SV * const repointer = newSViv(0);
2740 av_push(PL_regex_padav,SvREFCNT_inc(repointer));
2741 pmop->op_pmoffset = av_len(PL_regex_padav);
2742 PL_regex_pad = AvARRAY(PL_regex_padav);
2746 /* link into pm list */
2747 if (type != OP_TRANS && PL_curstash) {
2748 MAGIC *mg = mg_find((SV*)PL_curstash, PERL_MAGIC_symtab);
2751 mg = sv_magicext((SV*)PL_curstash, 0, PERL_MAGIC_symtab, 0, 0, 0);
2753 pmop->op_pmnext = (PMOP*)mg->mg_obj;
2754 mg->mg_obj = (SV*)pmop;
2755 PmopSTASH_set(pmop,PL_curstash);
2758 return CHECKOP(type, pmop);
2761 /* Given some sort of match op o, and an expression expr containing a
2762 * pattern, either compile expr into a regex and attach it to o (if it's
2763 * constant), or convert expr into a runtime regcomp op sequence (if it's
2766 * isreg indicates that the pattern is part of a regex construct, eg
2767 * $x =~ /pattern/ or split /pattern/, as opposed to $x =~ $pattern or
2768 * split "pattern", which aren't. In the former case, expr will be a list
2769 * if the pattern contains more than one term (eg /a$b/) or if it contains
2770 * a replacement, ie s/// or tr///.
2774 Perl_pmruntime(pTHX_ OP *o, OP *expr, bool isreg)
2779 I32 repl_has_vars = 0;
2783 if (o->op_type == OP_SUBST || o->op_type == OP_TRANS) {
2784 /* last element in list is the replacement; pop it */
2786 repl = cLISTOPx(expr)->op_last;
2787 kid = cLISTOPx(expr)->op_first;
2788 while (kid->op_sibling != repl)
2789 kid = kid->op_sibling;
2790 kid->op_sibling = Nullop;
2791 cLISTOPx(expr)->op_last = kid;
2794 if (isreg && expr->op_type == OP_LIST &&
2795 cLISTOPx(expr)->op_first->op_sibling == cLISTOPx(expr)->op_last)
2797 /* convert single element list to element */
2798 OP* const oe = expr;
2799 expr = cLISTOPx(oe)->op_first->op_sibling;
2800 cLISTOPx(oe)->op_first->op_sibling = Nullop;
2801 cLISTOPx(oe)->op_last = Nullop;
2805 if (o->op_type == OP_TRANS) {
2806 return pmtrans(o, expr, repl);
2809 reglist = isreg && expr->op_type == OP_LIST;
2813 PL_hints |= HINT_BLOCK_SCOPE;
2816 if (expr->op_type == OP_CONST) {
2818 SV *pat = ((SVOP*)expr)->op_sv;
2819 const char *p = SvPV_const(pat, plen);
2820 if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
2821 U32 was_readonly = SvREADONLY(pat);
2825 sv_force_normal_flags(pat, 0);
2826 assert(!SvREADONLY(pat));
2829 SvREADONLY_off(pat);
2833 sv_setpvn(pat, "\\s+", 3);
2835 SvFLAGS(pat) |= was_readonly;
2837 p = SvPV_const(pat, plen);
2838 pm->op_pmflags |= PMf_SKIPWHITE;
2841 pm->op_pmdynflags |= PMdf_UTF8;
2842 /* FIXME - can we make this function take const char * args? */
2843 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char*)p, (char*)p + plen, pm));
2844 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2845 pm->op_pmflags |= PMf_WHITE;
2849 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2850 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2852 : OP_REGCMAYBE),0,expr);
2854 NewOp(1101, rcop, 1, LOGOP);
2855 rcop->op_type = OP_REGCOMP;
2856 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2857 rcop->op_first = scalar(expr);
2858 rcop->op_flags |= OPf_KIDS
2859 | ((PL_hints & HINT_RE_EVAL) ? OPf_SPECIAL : 0)
2860 | (reglist ? OPf_STACKED : 0);
2861 rcop->op_private = 1;
2864 rcop->op_targ = pad_alloc(rcop->op_type, SVs_PADTMP);
2866 /* /$x/ may cause an eval, since $x might be qr/(?{..})/ */
2869 /* establish postfix order */
2870 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2872 rcop->op_next = expr;
2873 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2876 rcop->op_next = LINKLIST(expr);
2877 expr->op_next = (OP*)rcop;
2880 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2885 if (pm->op_pmflags & PMf_EVAL) {
2887 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2888 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2890 else if (repl->op_type == OP_CONST)
2894 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2895 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
2896 if (curop->op_type == OP_GV) {
2897 GV *gv = cGVOPx_gv(curop);
2899 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2902 else if (curop->op_type == OP_RV2CV)
2904 else if (curop->op_type == OP_RV2SV ||
2905 curop->op_type == OP_RV2AV ||
2906 curop->op_type == OP_RV2HV ||
2907 curop->op_type == OP_RV2GV) {
2908 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2911 else if (curop->op_type == OP_PADSV ||
2912 curop->op_type == OP_PADAV ||
2913 curop->op_type == OP_PADHV ||
2914 curop->op_type == OP_PADANY) {
2917 else if (curop->op_type == OP_PUSHRE)
2918 ; /* Okay here, dangerous in newASSIGNOP */
2928 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2929 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2930 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2931 prepend_elem(o->op_type, scalar(repl), o);
2934 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2935 pm->op_pmflags |= PMf_MAYBE_CONST;
2936 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2938 NewOp(1101, rcop, 1, LOGOP);
2939 rcop->op_type = OP_SUBSTCONT;
2940 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2941 rcop->op_first = scalar(repl);
2942 rcop->op_flags |= OPf_KIDS;
2943 rcop->op_private = 1;
2946 /* establish postfix order */
2947 rcop->op_next = LINKLIST(repl);
2948 repl->op_next = (OP*)rcop;
2950 pm->op_pmreplroot = scalar((OP*)rcop);
2951 pm->op_pmreplstart = LINKLIST(rcop);
2960 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
2964 NewOp(1101, svop, 1, SVOP);
2965 svop->op_type = (OPCODE)type;
2966 svop->op_ppaddr = PL_ppaddr[type];
2968 svop->op_next = (OP*)svop;
2969 svop->op_flags = (U8)flags;
2970 if (PL_opargs[type] & OA_RETSCALAR)
2972 if (PL_opargs[type] & OA_TARGET)
2973 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2974 return CHECKOP(type, svop);
2978 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2982 NewOp(1101, padop, 1, PADOP);
2983 padop->op_type = (OPCODE)type;
2984 padop->op_ppaddr = PL_ppaddr[type];
2985 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2986 SvREFCNT_dec(PAD_SVl(padop->op_padix));
2987 PAD_SETSV(padop->op_padix, sv);
2990 padop->op_next = (OP*)padop;
2991 padop->op_flags = (U8)flags;
2992 if (PL_opargs[type] & OA_RETSCALAR)
2994 if (PL_opargs[type] & OA_TARGET)
2995 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2996 return CHECKOP(type, padop);
3000 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3006 return newPADOP(type, flags, SvREFCNT_inc(gv));
3008 return newSVOP(type, flags, SvREFCNT_inc(gv));
3013 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3017 NewOp(1101, pvop, 1, PVOP);
3018 pvop->op_type = (OPCODE)type;
3019 pvop->op_ppaddr = PL_ppaddr[type];
3021 pvop->op_next = (OP*)pvop;
3022 pvop->op_flags = (U8)flags;
3023 if (PL_opargs[type] & OA_RETSCALAR)
3025 if (PL_opargs[type] & OA_TARGET)
3026 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3027 return CHECKOP(type, pvop);
3031 Perl_package(pTHX_ OP *o)
3036 save_hptr(&PL_curstash);
3037 save_item(PL_curstname);
3039 name = SvPV_const(cSVOPo->op_sv, len);
3040 PL_curstash = gv_stashpvn(name, len, TRUE);
3041 sv_setpvn(PL_curstname, name, len);
3044 PL_hints |= HINT_BLOCK_SCOPE;
3045 PL_copline = NOLINE;
3050 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3056 if (idop->op_type != OP_CONST)
3057 Perl_croak(aTHX_ "Module name must be constant");
3062 SV * const vesv = ((SVOP*)version)->op_sv;
3064 if (!arg && !SvNIOKp(vesv)) {
3071 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3072 Perl_croak(aTHX_ "Version number must be constant number");
3074 /* Make copy of idop so we don't free it twice */
3075 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3077 /* Fake up a method call to VERSION */
3078 meth = newSVpvs_share("VERSION");
3079 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3080 append_elem(OP_LIST,
3081 prepend_elem(OP_LIST, pack, list(version)),
3082 newSVOP(OP_METHOD_NAMED, 0, meth)));
3086 /* Fake up an import/unimport */
3087 if (arg && arg->op_type == OP_STUB)
3088 imop = arg; /* no import on explicit () */
3089 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3090 imop = Nullop; /* use 5.0; */
3092 idop->op_private |= OPpCONST_NOVER;
3097 /* Make copy of idop so we don't free it twice */
3098 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3100 /* Fake up a method call to import/unimport */
3102 ? newSVpvs_share("import") : newSVpvs_share("unimport");
3103 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3104 append_elem(OP_LIST,
3105 prepend_elem(OP_LIST, pack, list(arg)),
3106 newSVOP(OP_METHOD_NAMED, 0, meth)));
3109 /* Fake up the BEGIN {}, which does its thing immediately. */
3111 newSVOP(OP_CONST, 0, newSVpvs_share("BEGIN")),
3114 append_elem(OP_LINESEQ,
3115 append_elem(OP_LINESEQ,
3116 newSTATEOP(0, Nullch, newUNOP(OP_REQUIRE, 0, idop)),
3117 newSTATEOP(0, Nullch, veop)),
3118 newSTATEOP(0, Nullch, imop) ));
3120 /* The "did you use incorrect case?" warning used to be here.
3121 * The problem is that on case-insensitive filesystems one
3122 * might get false positives for "use" (and "require"):
3123 * "use Strict" or "require CARP" will work. This causes
3124 * portability problems for the script: in case-strict
3125 * filesystems the script will stop working.
3127 * The "incorrect case" warning checked whether "use Foo"
3128 * imported "Foo" to your namespace, but that is wrong, too:
3129 * there is no requirement nor promise in the language that
3130 * a Foo.pm should or would contain anything in package "Foo".
3132 * There is very little Configure-wise that can be done, either:
3133 * the case-sensitivity of the build filesystem of Perl does not
3134 * help in guessing the case-sensitivity of the runtime environment.
3137 PL_hints |= HINT_BLOCK_SCOPE;
3138 PL_copline = NOLINE;
3140 PL_cop_seqmax++; /* Purely for B::*'s benefit */
3144 =head1 Embedding Functions
3146 =for apidoc load_module
3148 Loads the module whose name is pointed to by the string part of name.
3149 Note that the actual module name, not its filename, should be given.
3150 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3151 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3152 (or 0 for no flags). ver, if specified, provides version semantics
3153 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3154 arguments can be used to specify arguments to the module's import()
3155 method, similar to C<use Foo::Bar VERSION LIST>.
3160 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3163 va_start(args, ver);
3164 vload_module(flags, name, ver, &args);
3168 #ifdef PERL_IMPLICIT_CONTEXT
3170 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3174 va_start(args, ver);
3175 vload_module(flags, name, ver, &args);
3181 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3185 OP * const modname = newSVOP(OP_CONST, 0, name);
3186 modname->op_private |= OPpCONST_BARE;
3188 veop = newSVOP(OP_CONST, 0, ver);
3192 if (flags & PERL_LOADMOD_NOIMPORT) {
3193 imop = sawparens(newNULLLIST());
3195 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3196 imop = va_arg(*args, OP*);
3201 sv = va_arg(*args, SV*);
3203 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3204 sv = va_arg(*args, SV*);
3208 const line_t ocopline = PL_copline;
3209 COP * const ocurcop = PL_curcop;
3210 const int oexpect = PL_expect;
3212 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3213 veop, modname, imop);
3214 PL_expect = oexpect;
3215 PL_copline = ocopline;
3216 PL_curcop = ocurcop;
3221 Perl_dofile(pTHX_ OP *term, I32 force_builtin)
3226 if (!force_builtin) {
3227 gv = gv_fetchpv("do", 0, SVt_PVCV);
3228 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
3229 GV * const * const gvp = (GV**)hv_fetch(PL_globalstash, "do", 2, FALSE);
3230 gv = gvp ? *gvp : Nullgv;
3234 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3235 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3236 append_elem(OP_LIST, term,
3237 scalar(newUNOP(OP_RV2CV, 0,
3242 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3248 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3250 return newBINOP(OP_LSLICE, flags,
3251 list(force_list(subscript)),
3252 list(force_list(listval)) );
3256 S_is_list_assignment(pTHX_ register const OP *o)
3261 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3262 o = cUNOPo->op_first;
3264 if (o->op_type == OP_COND_EXPR) {
3265 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
3266 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3271 yyerror("Assignment to both a list and a scalar");
3275 if (o->op_type == OP_LIST &&
3276 (o->op_flags & OPf_WANT) == OPf_WANT_SCALAR &&
3277 o->op_private & OPpLVAL_INTRO)
3280 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3281 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3282 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
3285 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
3288 if (o->op_type == OP_RV2SV)
3295 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3300 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN || optype == OP_DORASSIGN) {
3301 return newLOGOP(optype, 0,
3302 mod(scalar(left), optype),
3303 newUNOP(OP_SASSIGN, 0, scalar(right)));
3306 return newBINOP(optype, OPf_STACKED,
3307 mod(scalar(left), optype), scalar(right));
3311 if (is_list_assignment(left)) {
3315 /* Grandfathering $[ assignment here. Bletch.*/
3316 /* Only simple assignments like C<< ($[) = 1 >> are allowed */
3317 PL_eval_start = (left->op_type == OP_CONST) ? right : 0;
3318 left = mod(left, OP_AASSIGN);
3321 else if (left->op_type == OP_CONST) {
3322 /* Result of assignment is always 1 (or we'd be dead already) */
3323 return newSVOP(OP_CONST, 0, newSViv(1));
3325 curop = list(force_list(left));
3326 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3327 o->op_private = (U8)(0 | (flags >> 8));
3329 /* PL_generation sorcery:
3330 * an assignment like ($a,$b) = ($c,$d) is easier than
3331 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3332 * To detect whether there are common vars, the global var
3333 * PL_generation is incremented for each assign op we compile.
3334 * Then, while compiling the assign op, we run through all the
3335 * variables on both sides of the assignment, setting a spare slot
3336 * in each of them to PL_generation. If any of them already have
3337 * that value, we know we've got commonality. We could use a
3338 * single bit marker, but then we'd have to make 2 passes, first
3339 * to clear the flag, then to test and set it. To find somewhere
3340 * to store these values, evil chicanery is done with SvCUR().
3343 if (!(left->op_private & OPpLVAL_INTRO)) {
3346 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3347 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3348 if (curop->op_type == OP_GV) {
3349 GV *gv = cGVOPx_gv(curop);
3350 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3352 SvCUR_set(gv, PL_generation);
3354 else if (curop->op_type == OP_PADSV ||
3355 curop->op_type == OP_PADAV ||
3356 curop->op_type == OP_PADHV ||
3357 curop->op_type == OP_PADANY)
3359 if (PAD_COMPNAME_GEN(curop->op_targ)
3360 == (STRLEN)PL_generation)
3362 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
3365 else if (curop->op_type == OP_RV2CV)
3367 else if (curop->op_type == OP_RV2SV ||
3368 curop->op_type == OP_RV2AV ||
3369 curop->op_type == OP_RV2HV ||
3370 curop->op_type == OP_RV2GV) {
3371 if (lastop->op_type != OP_GV) /* funny deref? */
3374 else if (curop->op_type == OP_PUSHRE) {
3375 if (((PMOP*)curop)->op_pmreplroot) {
3377 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3378 ((PMOP*)curop)->op_pmreplroot));
3380 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3382 if (gv == PL_defgv || (int)SvCUR(gv) == PL_generation)
3384 SvCUR_set(gv, PL_generation);
3393 o->op_private |= OPpASSIGN_COMMON;
3395 if (right && right->op_type == OP_SPLIT) {
3397 if ((tmpop = ((LISTOP*)right)->op_first) &&
3398 tmpop->op_type == OP_PUSHRE)
3400 PMOP * const pm = (PMOP*)tmpop;
3401 if (left->op_type == OP_RV2AV &&
3402 !(left->op_private & OPpLVAL_INTRO) &&
3403 !(o->op_private & OPpASSIGN_COMMON) )
3405 tmpop = ((UNOP*)left)->op_first;
3406 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3408 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3409 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3411 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3412 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3414 pm->op_pmflags |= PMf_ONCE;
3415 tmpop = cUNOPo->op_first; /* to list (nulled) */
3416 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3417 tmpop->op_sibling = Nullop; /* don't free split */
3418 right->op_next = tmpop->op_next; /* fix starting loc */
3419 op_free(o); /* blow off assign */
3420 right->op_flags &= ~OPf_WANT;
3421 /* "I don't know and I don't care." */
3426 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3427 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3429 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3431 sv_setiv(sv, PL_modcount+1);
3439 right = newOP(OP_UNDEF, 0);
3440 if (right->op_type == OP_READLINE) {
3441 right->op_flags |= OPf_STACKED;
3442 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3445 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3446 o = newBINOP(OP_SASSIGN, flags,
3447 scalar(right), mod(scalar(left), OP_SASSIGN) );
3451 o = newSVOP(OP_CONST, 0, newSViv(PL_compiling.cop_arybase));
3458 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3461 const U32 seq = intro_my();
3464 NewOp(1101, cop, 1, COP);
3465 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3466 cop->op_type = OP_DBSTATE;
3467 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3470 cop->op_type = OP_NEXTSTATE;
3471 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3473 cop->op_flags = (U8)flags;
3474 cop->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
3476 cop->op_private |= NATIVE_HINTS;
3478 PL_compiling.op_private = cop->op_private;
3479 cop->op_next = (OP*)cop;
3482 cop->cop_label = label;
3483 PL_hints |= HINT_BLOCK_SCOPE;
3486 cop->cop_arybase = PL_curcop->cop_arybase;
3487 if (specialWARN(PL_curcop->cop_warnings))
3488 cop->cop_warnings = PL_curcop->cop_warnings ;
3490 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3491 if (specialCopIO(PL_curcop->cop_io))
3492 cop->cop_io = PL_curcop->cop_io;
3494 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3497 if (PL_copline == NOLINE)
3498 CopLINE_set(cop, CopLINE(PL_curcop));
3500 CopLINE_set(cop, PL_copline);
3501 PL_copline = NOLINE;
3504 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3506 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3508 CopSTASH_set(cop, PL_curstash);
3510 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3511 SV * const * const svp = av_fetch(CopFILEAVx(PL_curcop), (I32)CopLINE(cop), FALSE);
3512 if (svp && *svp != &PL_sv_undef ) {
3513 (void)SvIOK_on(*svp);
3514 SvIV_set(*svp, PTR2IV(cop));
3518 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3523 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3526 return new_logop(type, flags, &first, &other);
3530 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3535 OP *first = *firstp;
3536 OP * const other = *otherp;
3538 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3539 return newBINOP(type, flags, scalar(first), scalar(other));
3541 scalarboolean(first);
3542 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3543 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3544 if (type == OP_AND || type == OP_OR) {
3550 first = *firstp = cUNOPo->op_first;
3552 first->op_next = o->op_next;
3553 cUNOPo->op_first = Nullop;
3557 if (first->op_type == OP_CONST) {
3558 if (first->op_private & OPpCONST_STRICT)
3559 no_bareword_allowed(first);
3560 else if ((first->op_private & OPpCONST_BARE) && ckWARN(WARN_BAREWORD))
3561 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3562 if ((type == OP_AND && SvTRUE(((SVOP*)first)->op_sv)) ||
3563 (type == OP_OR && !SvTRUE(((SVOP*)first)->op_sv)) ||
3564 (type == OP_DOR && !SvOK(((SVOP*)first)->op_sv))) {
3567 if (other->op_type == OP_CONST)
3568 other->op_private |= OPpCONST_SHORTCIRCUIT;
3572 /* check for C<my $x if 0>, or C<my($x,$y) if 0> */
3573 const OP *o2 = other;
3574 if ( ! (o2->op_type == OP_LIST
3575 && (( o2 = cUNOPx(o2)->op_first))
3576 && o2->op_type == OP_PUSHMARK
3577 && (( o2 = o2->op_sibling)) )
3580 if ((o2->op_type == OP_PADSV || o2->op_type == OP_PADAV
3581 || o2->op_type == OP_PADHV)
3582 && o2->op_private & OPpLVAL_INTRO
3583 && ckWARN(WARN_DEPRECATED))
3585 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
3586 "Deprecated use of my() in false conditional");
3591 if (first->op_type == OP_CONST)
3592 first->op_private |= OPpCONST_SHORTCIRCUIT;
3596 else if ((first->op_flags & OPf_KIDS) && type != OP_DOR
3597 && ckWARN(WARN_MISC)) /* [#24076] Don't warn for <FH> err FOO. */
3599 const OP * const k1 = ((UNOP*)first)->op_first;
3600 const OP * const k2 = k1->op_sibling;
3602 switch (first->op_type)
3605 if (k2 && k2->op_type == OP_READLINE
3606 && (k2->op_flags & OPf_STACKED)
3607 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3609 warnop = k2->op_type;
3614 if (k1->op_type == OP_READDIR
3615 || k1->op_type == OP_GLOB
3616 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3617 || k1->op_type == OP_EACH)
3619 warnop = ((k1->op_type == OP_NULL)
3620 ? (OPCODE)k1->op_targ : k1->op_type);
3625 const line_t oldline = CopLINE(PL_curcop);
3626 CopLINE_set(PL_curcop, PL_copline);
3627 Perl_warner(aTHX_ packWARN(WARN_MISC),
3628 "Value of %s%s can be \"0\"; test with defined()",
3630 ((warnop == OP_READLINE || warnop == OP_GLOB)
3631 ? " construct" : "() operator"));
3632 CopLINE_set(PL_curcop, oldline);
3639 if (type == OP_ANDASSIGN || type == OP_ORASSIGN || type == OP_DORASSIGN)
3640 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3642 NewOp(1101, logop, 1, LOGOP);
3644 logop->op_type = (OPCODE)type;
3645 logop->op_ppaddr = PL_ppaddr[type];
3646 logop->op_first = first;
3647 logop->op_flags = (U8)(flags | OPf_KIDS);
3648 logop->op_other = LINKLIST(other);
3649 logop->op_private = (U8)(1 | (flags >> 8));
3651 /* establish postfix order */
3652 logop->op_next = LINKLIST(first);
3653 first->op_next = (OP*)logop;
3654 first->op_sibling = other;
3656 CHECKOP(type,logop);
3658 o = newUNOP(OP_NULL, 0, (OP*)logop);
3665 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3673 return newLOGOP(OP_AND, 0, first, trueop);
3675 return newLOGOP(OP_OR, 0, first, falseop);
3677 scalarboolean(first);
3678 if (first->op_type == OP_CONST) {
3679 if (first->op_private & OPpCONST_BARE &&
3680 first->op_private & OPpCONST_STRICT) {
3681 no_bareword_allowed(first);
3683 if (SvTRUE(((SVOP*)first)->op_sv)) {
3694 NewOp(1101, logop, 1, LOGOP);
3695 logop->op_type = OP_COND_EXPR;
3696 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3697 logop->op_first = first;
3698 logop->op_flags = (U8)(flags | OPf_KIDS);
3699 logop->op_private = (U8)(1 | (flags >> 8));
3700 logop->op_other = LINKLIST(trueop);
3701 logop->op_next = LINKLIST(falseop);
3703 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3706 /* establish postfix order */
3707 start = LINKLIST(first);
3708 first->op_next = (OP*)logop;
3710 first->op_sibling = trueop;
3711 trueop->op_sibling = falseop;
3712 o = newUNOP(OP_NULL, 0, (OP*)logop);
3714 trueop->op_next = falseop->op_next = o;
3721 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3730 NewOp(1101, range, 1, LOGOP);
3732 range->op_type = OP_RANGE;
3733 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3734 range->op_first = left;
3735 range->op_flags = OPf_KIDS;
3736 leftstart = LINKLIST(left);
3737 range->op_other = LINKLIST(right);
3738 range->op_private = (U8)(1 | (flags >> 8));
3740 left->op_sibling = right;
3742 range->op_next = (OP*)range;
3743 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3744 flop = newUNOP(OP_FLOP, 0, flip);
3745 o = newUNOP(OP_NULL, 0, flop);
3747 range->op_next = leftstart;
3749 left->op_next = flip;
3750 right->op_next = flop;
3752 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3753 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3754 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3755 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3757 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3758 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3761 if (!flip->op_private || !flop->op_private)
3762 linklist(o); /* blow off optimizer unless constant */
3768 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3772 const bool once = block && block->op_flags & OPf_SPECIAL &&
3773 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3775 PERL_UNUSED_ARG(debuggable);
3778 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3779 return block; /* do {} while 0 does once */
3780 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3781 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3782 expr = newUNOP(OP_DEFINED, 0,
3783 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3784 } else if (expr->op_flags & OPf_KIDS) {
3785 const OP * const k1 = ((UNOP*)expr)->op_first;
3786 const OP * const k2 = k1 ? k1->op_sibling : NULL;
3787 switch (expr->op_type) {
3789 if (k2 && k2->op_type == OP_READLINE
3790 && (k2->op_flags & OPf_STACKED)
3791 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3792 expr = newUNOP(OP_DEFINED, 0, expr);
3796 if (k1->op_type == OP_READDIR
3797 || k1->op_type == OP_GLOB
3798 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3799 || k1->op_type == OP_EACH)
3800 expr = newUNOP(OP_DEFINED, 0, expr);
3806 /* if block is null, the next append_elem() would put UNSTACK, a scalar
3807 * op, in listop. This is wrong. [perl #27024] */
3809 block = newOP(OP_NULL, 0);
3810 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3811 o = new_logop(OP_AND, 0, &expr, &listop);
3814 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3816 if (once && o != listop)
3817 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3820 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3822 o->op_flags |= flags;
3824 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3829 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32
3830 whileline, OP *expr, OP *block, OP *cont, I32 has_my)
3839 PERL_UNUSED_ARG(debuggable);
3842 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3843 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3844 expr = newUNOP(OP_DEFINED, 0,
3845 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3846 } else if (expr->op_flags & OPf_KIDS) {
3847 const OP * const k1 = ((UNOP*)expr)->op_first;
3848 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
3849 switch (expr->op_type) {
3851 if (k2 && k2->op_type == OP_READLINE
3852 && (k2->op_flags & OPf_STACKED)
3853 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3854 expr = newUNOP(OP_DEFINED, 0, expr);
3858 if (k1->op_type == OP_READDIR
3859 || k1->op_type == OP_GLOB
3860 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3861 || k1->op_type == OP_EACH)
3862 expr = newUNOP(OP_DEFINED, 0, expr);
3869 block = newOP(OP_NULL, 0);
3870 else if (cont || has_my) {
3871 block = scope(block);
3875 next = LINKLIST(cont);
3878 OP * const unstack = newOP(OP_UNSTACK, 0);
3881 cont = append_elem(OP_LINESEQ, cont, unstack);
3884 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3885 redo = LINKLIST(listop);
3888 PL_copline = (line_t)whileline;
3890 o = new_logop(OP_AND, 0, &expr, &listop);
3891 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3892 op_free(expr); /* oops, it's a while (0) */
3894 return Nullop; /* listop already freed by new_logop */
3897 ((LISTOP*)listop)->op_last->op_next =
3898 (o == listop ? redo : LINKLIST(o));
3904 NewOp(1101,loop,1,LOOP);
3905 loop->op_type = OP_ENTERLOOP;
3906 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3907 loop->op_private = 0;
3908 loop->op_next = (OP*)loop;
3911 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3913 loop->op_redoop = redo;
3914 loop->op_lastop = o;
3915 o->op_private |= loopflags;
3918 loop->op_nextop = next;
3920 loop->op_nextop = o;
3922 o->op_flags |= flags;
3923 o->op_private |= (flags >> 8);
3928 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
3933 PADOFFSET padoff = 0;
3938 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3939 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
3940 sv->op_type = OP_RV2GV;
3941 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
3942 if (cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
3943 iterpflags |= OPpITER_DEF;
3945 else if (sv->op_type == OP_PADSV) { /* private variable */
3946 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
3947 padoff = sv->op_targ;
3952 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3953 padoff = sv->op_targ;
3955 iterflags |= OPf_SPECIAL;
3960 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
3961 if (padoff && strEQ(PAD_COMPNAME_PV(padoff), "$_"))
3962 iterpflags |= OPpITER_DEF;
3965 const I32 offset = pad_findmy("$_");
3966 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS(offset) & SVpad_OUR) {
3967 sv = newGVOP(OP_GV, 0, PL_defgv);
3972 iterpflags |= OPpITER_DEF;
3974 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3975 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3976 iterflags |= OPf_STACKED;
3978 else if (expr->op_type == OP_NULL &&
3979 (expr->op_flags & OPf_KIDS) &&
3980 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3982 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3983 * set the STACKED flag to indicate that these values are to be
3984 * treated as min/max values by 'pp_iterinit'.
3986 UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3987 LOGOP* const range = (LOGOP*) flip->op_first;
3988 OP* const left = range->op_first;
3989 OP* const right = left->op_sibling;
3992 range->op_flags &= ~OPf_KIDS;
3993 range->op_first = Nullop;
3995 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3996 listop->op_first->op_next = range->op_next;
3997 left->op_next = range->op_other;
3998 right->op_next = (OP*)listop;
3999 listop->op_next = listop->op_first;
4002 expr = (OP*)(listop);
4004 iterflags |= OPf_STACKED;
4007 expr = mod(force_list(expr), OP_GREPSTART);
4010 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4011 append_elem(OP_LIST, expr, scalar(sv))));
4012 assert(!loop->op_next);
4013 /* for my $x () sets OPpLVAL_INTRO;
4014 * for our $x () sets OPpOUR_INTRO */
4015 loop->op_private = (U8)iterpflags;
4016 #ifdef PL_OP_SLAB_ALLOC
4019 NewOp(1234,tmp,1,LOOP);
4020 Copy(loop,tmp,1,LISTOP);
4025 Renew(loop, 1, LOOP);
4027 loop->op_targ = padoff;
4028 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont, 0);
4029 PL_copline = forline;
4030 return newSTATEOP(0, label, wop);
4034 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4038 if (type != OP_GOTO || label->op_type == OP_CONST) {
4039 /* "last()" means "last" */
4040 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4041 o = newOP(type, OPf_SPECIAL);
4043 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4044 ? SvPVx_nolen_const(((SVOP*)label)->op_sv)
4050 /* Check whether it's going to be a goto &function */
4051 if (label->op_type == OP_ENTERSUB
4052 && !(label->op_flags & OPf_STACKED))
4053 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4054 o = newUNOP(type, OPf_STACKED, label);
4056 PL_hints |= HINT_BLOCK_SCOPE;
4060 /* if the condition is a literal array or hash
4061 (or @{ ... } etc), make a reference to it.
4064 S_ref_array_or_hash(pTHX_ OP *cond)
4067 && (cond->op_type == OP_RV2AV
4068 || cond->op_type == OP_PADAV
4069 || cond->op_type == OP_RV2HV
4070 || cond->op_type == OP_PADHV))
4072 return newUNOP(OP_REFGEN,
4073 0, mod(cond, OP_REFGEN));
4079 /* These construct the optree fragments representing given()
4082 entergiven and enterwhen are LOGOPs; the op_other pointer
4083 points up to the associated leave op. We need this so we
4084 can put it in the context and make break/continue work.
4085 (Also, of course, pp_enterwhen will jump straight to
4086 op_other if the match fails.)
4091 S_newGIVWHENOP(pTHX_ OP *cond, OP *block,
4092 I32 enter_opcode, I32 leave_opcode,
4093 PADOFFSET entertarg)
4098 NewOp(1101, enterop, 1, LOGOP);
4099 enterop->op_type = enter_opcode;
4100 enterop->op_ppaddr = PL_ppaddr[enter_opcode];
4101 enterop->op_flags = (U8) OPf_KIDS;
4102 enterop->op_targ = ((entertarg == NOT_IN_PAD) ? 0 : entertarg);
4103 enterop->op_private = 0;
4105 o = newUNOP(leave_opcode, 0, (OP *) enterop);
4108 enterop->op_first = scalar(cond);
4109 cond->op_sibling = block;
4111 o->op_next = LINKLIST(cond);
4112 cond->op_next = (OP *) enterop;
4115 /* This is a default {} block */
4116 enterop->op_first = block;
4117 enterop->op_flags |= OPf_SPECIAL;
4119 o->op_next = (OP *) enterop;
4122 CHECKOP(enter_opcode, enterop); /* Currently does nothing, since
4123 entergiven and enterwhen both
4126 enterop->op_next = LINKLIST(block);
4127 block->op_next = enterop->op_other = o;
4132 /* Does this look like a boolean operation? For these purposes
4133 a boolean operation is:
4134 - a subroutine call [*]
4135 - a logical connective
4136 - a comparison operator
4137 - a filetest operator, with the exception of -s -M -A -C
4138 - defined(), exists() or eof()
4139 - /$re/ or $foo =~ /$re/
4141 [*] possibly surprising
4145 S_looks_like_bool(pTHX_ OP *o)
4147 switch(o->op_type) {
4149 return looks_like_bool(cLOGOPo->op_first);
4153 looks_like_bool(cLOGOPo->op_first)
4154 && looks_like_bool(cLOGOPo->op_first->op_sibling));
4158 case OP_NOT: case OP_XOR:
4159 /* Note that OP_DOR is not here */
4161 case OP_EQ: case OP_NE: case OP_LT:
4162 case OP_GT: case OP_LE: case OP_GE:
4164 case OP_I_EQ: case OP_I_NE: case OP_I_LT:
4165 case OP_I_GT: case OP_I_LE: case OP_I_GE:
4167 case OP_SEQ: case OP_SNE: case OP_SLT:
4168 case OP_SGT: case OP_SLE: case OP_SGE:
4172 case OP_FTRREAD: case OP_FTRWRITE: case OP_FTREXEC:
4173 case OP_FTEREAD: case OP_FTEWRITE: case OP_FTEEXEC:
4174 case OP_FTIS: case OP_FTEOWNED: case OP_FTROWNED:
4175 case OP_FTZERO: case OP_FTSOCK: case OP_FTCHR:
4176 case OP_FTBLK: case OP_FTFILE: case OP_FTDIR:
4177 case OP_FTPIPE: case OP_FTLINK: case OP_FTSUID:
4178 case OP_FTSGID: case OP_FTSVTX: case OP_FTTTY:
4179 case OP_FTTEXT: case OP_FTBINARY:
4181 case OP_DEFINED: case OP_EXISTS:
4182 case OP_MATCH: case OP_EOF:
4187 /* Detect comparisons that have been optimized away */
4188 if (cSVOPo->op_sv == &PL_sv_yes
4189 || cSVOPo->op_sv == &PL_sv_no)
4200 Perl_newGIVENOP(pTHX_ OP *cond, OP *block, PADOFFSET defsv_off)
4203 return newGIVWHENOP(
4204 ref_array_or_hash(cond),
4206 OP_ENTERGIVEN, OP_LEAVEGIVEN,
4210 /* If cond is null, this is a default {} block */
4212 Perl_newWHENOP(pTHX_ OP *cond, OP *block)
4214 bool cond_llb = (!cond || looks_like_bool(cond));
4220 cond_op = newBINOP(OP_SMARTMATCH, OPf_SPECIAL,
4222 scalar(ref_array_or_hash(cond)));
4225 return newGIVWHENOP(
4227 append_elem(block->op_type, block, newOP(OP_BREAK, OPf_SPECIAL)),
4228 OP_ENTERWHEN, OP_LEAVEWHEN, 0);
4232 =for apidoc cv_undef
4234 Clear out all the active components of a CV. This can happen either
4235 by an explicit C<undef &foo>, or by the reference count going to zero.
4236 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
4237 children can still follow the full lexical scope chain.
4243 Perl_cv_undef(pTHX_ CV *cv)
4247 if (CvFILE(cv) && !CvXSUB(cv)) {
4248 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4249 Safefree(CvFILE(cv));
4254 if (!CvXSUB(cv) && CvROOT(cv)) {
4256 Perl_croak(aTHX_ "Can't undef active subroutine");
4259 PAD_SAVE_SETNULLPAD();
4261 op_free(CvROOT(cv));
4262 CvROOT(cv) = Nullop;
4263 CvSTART(cv) = Nullop;
4266 SvPOK_off((SV*)cv); /* forget prototype */
4271 /* remove CvOUTSIDE unless this is an undef rather than a free */
4272 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4273 if (!CvWEAKOUTSIDE(cv))
4274 SvREFCNT_dec(CvOUTSIDE(cv));
4275 CvOUTSIDE(cv) = Nullcv;
4278 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4284 /* delete all flags except WEAKOUTSIDE */
4285 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4289 Perl_cv_ckproto(pTHX_ const CV *cv, const GV *gv, const char *p)
4291 if (((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX_const(cv)))) && ckWARN_d(WARN_PROTOTYPE)) {
4292 SV* const msg = sv_newmortal();
4296 gv_efullname3(name = sv_newmortal(), gv, Nullch);
4297 sv_setpv(msg, "Prototype mismatch:");
4299 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, name);
4301 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (const SV *)cv);
4303 sv_catpvs(msg, ": none");
4304 sv_catpvs(msg, " vs ");
4306 Perl_sv_catpvf(aTHX_ msg, "(%s)", p);
4308 sv_catpvs(msg, "none");
4309 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, msg);
4313 static void const_sv_xsub(pTHX_ CV* cv);
4317 =head1 Optree Manipulation Functions
4319 =for apidoc cv_const_sv
4321 If C<cv> is a constant sub eligible for inlining. returns the constant
4322 value returned by the sub. Otherwise, returns NULL.
4324 Constant subs can be created with C<newCONSTSUB> or as described in
4325 L<perlsub/"Constant Functions">.
4330 Perl_cv_const_sv(pTHX_ CV *cv)
4334 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
4336 return CvCONST(cv) ? (SV*)CvXSUBANY(cv).any_ptr : NULL;
4339 /* op_const_sv: examine an optree to determine whether it's in-lineable.
4340 * Can be called in 3 ways:
4343 * look for a single OP_CONST with attached value: return the value
4345 * cv && CvCLONE(cv) && !CvCONST(cv)
4347 * examine the clone prototype, and if contains only a single
4348 * OP_CONST referencing a pad const, or a single PADSV referencing
4349 * an outer lexical, return a non-zero value to indicate the CV is
4350 * a candidate for "constizing" at clone time
4354 * We have just cloned an anon prototype that was marked as a const
4355 * candidiate. Try to grab the current value, and in the case of
4356 * PADSV, ignore it if it has multiple references. Return the value.
4360 Perl_op_const_sv(pTHX_ const OP *o, CV *cv)
4367 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4368 o = cLISTOPo->op_first->op_sibling;
4370 for (; o; o = o->op_next) {
4371 const OPCODE type = o->op_type;
4373 if (sv && o->op_next == o)
4375 if (o->op_next != o) {
4376 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4378 if (type == OP_DBSTATE)
4381 if (type == OP_LEAVESUB || type == OP_RETURN)
4385 if (type == OP_CONST && cSVOPo->op_sv)
4387 else if (cv && type == OP_CONST) {
4388 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4392 else if (cv && type == OP_PADSV) {
4393 if (CvCONST(cv)) { /* newly cloned anon */
4394 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4395 /* the candidate should have 1 ref from this pad and 1 ref
4396 * from the parent */
4397 if (!sv || SvREFCNT(sv) != 2)
4404 if (PAD_COMPNAME_FLAGS(o->op_targ) & SVf_FAKE)
4405 sv = &PL_sv_undef; /* an arbitrary non-null value */
4416 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4418 PERL_UNUSED_ARG(floor);
4428 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4432 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4434 return Perl_newATTRSUB(aTHX_ floor, o, proto, Nullop, block);
4438 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4445 register CV *cv = NULL;
4447 /* If the subroutine has no body, no attributes, and no builtin attributes
4448 then it's just a sub declaration, and we may be able to get away with
4449 storing with a placeholder scalar in the symbol table, rather than a
4450 full GV and CV. If anything is present then it will take a full CV to
4452 const I32 gv_fetch_flags
4453 = (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4454 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
4455 const char * const name = o ? SvPVx_nolen_const(cSVOPo->op_sv) : Nullch;
4458 assert(proto->op_type == OP_CONST);
4459 ps = SvPVx_const(((SVOP*)proto)->op_sv, ps_len);
4464 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4465 SV * const sv = sv_newmortal();
4466 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4467 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4468 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4469 aname = SvPVX_const(sv);
4474 gv = name ? gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV)
4475 : gv_fetchpv(aname ? aname
4476 : (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4477 gv_fetch_flags, SVt_PVCV);
4486 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4487 maximum a prototype before. */
4488 if (SvTYPE(gv) > SVt_NULL) {
4489 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4490 && ckWARN_d(WARN_PROTOTYPE))
4492 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4494 cv_ckproto((CV*)gv, NULL, ps);
4497 sv_setpvn((SV*)gv, ps, ps_len);
4499 sv_setiv((SV*)gv, -1);
4500 SvREFCNT_dec(PL_compcv);
4501 cv = PL_compcv = NULL;
4502 PL_sub_generation++;
4506 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4508 #ifdef GV_UNIQUE_CHECK
4509 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4510 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4514 if (!block || !ps || *ps || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4517 const_sv = op_const_sv(block, Nullcv);
4520 const bool exists = CvROOT(cv) || CvXSUB(cv);
4522 #ifdef GV_UNIQUE_CHECK
4523 if (exists && GvUNIQUE(gv)) {
4524 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4528 /* if the subroutine doesn't exist and wasn't pre-declared
4529 * with a prototype, assume it will be AUTOLOADed,
4530 * skipping the prototype check
4532 if (exists || SvPOK(cv))
4533 cv_ckproto(cv, gv, ps);
4534 /* already defined (or promised)? */
4535 if (exists || GvASSUMECV(gv)) {
4536 if (!block && !attrs) {
4537 if (CvFLAGS(PL_compcv)) {
4538 /* might have had built-in attrs applied */
4539 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4541 /* just a "sub foo;" when &foo is already defined */
4542 SAVEFREESV(PL_compcv);
4546 if (ckWARN(WARN_REDEFINE)
4548 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4550 const line_t oldline = CopLINE(PL_curcop);
4551 if (PL_copline != NOLINE)
4552 CopLINE_set(PL_curcop, PL_copline);
4553 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4554 CvCONST(cv) ? "Constant subroutine %s redefined"
4555 : "Subroutine %s redefined", name);
4556 CopLINE_set(PL_curcop, oldline);
4564 (void)SvREFCNT_inc(const_sv);
4566 assert(!CvROOT(cv) && !CvCONST(cv));
4567 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4568 CvXSUBANY(cv).any_ptr = const_sv;
4569 CvXSUB(cv) = const_sv_xsub;
4574 cv = newCONSTSUB(NULL, name, const_sv);
4577 SvREFCNT_dec(PL_compcv);
4579 PL_sub_generation++;
4586 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4587 * before we clobber PL_compcv.
4591 /* Might have had built-in attributes applied -- propagate them. */
4592 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4593 if (CvGV(cv) && GvSTASH(CvGV(cv)))
4594 stash = GvSTASH(CvGV(cv));
4595 else if (CvSTASH(cv))
4596 stash = CvSTASH(cv);
4598 stash = PL_curstash;
4601 /* possibly about to re-define existing subr -- ignore old cv */
4602 rcv = (SV*)PL_compcv;
4603 if (name && GvSTASH(gv))
4604 stash = GvSTASH(gv);
4606 stash = PL_curstash;
4608 apply_attrs(stash, rcv, attrs, FALSE);
4610 if (cv) { /* must reuse cv if autoloaded */
4612 /* got here with just attrs -- work done, so bug out */
4613 SAVEFREESV(PL_compcv);
4616 /* transfer PL_compcv to cv */
4618 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4619 if (!CvWEAKOUTSIDE(cv))
4620 SvREFCNT_dec(CvOUTSIDE(cv));
4621 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4622 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4623 CvOUTSIDE(PL_compcv) = 0;
4624 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4625 CvPADLIST(PL_compcv) = 0;
4626 /* inner references to PL_compcv must be fixed up ... */
4627 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4628 /* ... before we throw it away */
4629 SvREFCNT_dec(PL_compcv);
4631 if (PERLDB_INTER)/* Advice debugger on the new sub. */
4632 ++PL_sub_generation;
4639 PL_sub_generation++;
4643 CvFILE_set_from_cop(cv, PL_curcop);
4644 CvSTASH(cv) = PL_curstash;
4647 sv_setpvn((SV*)cv, ps, ps_len);
4649 if (PL_error_count) {
4653 const char *s = strrchr(name, ':');
4655 if (strEQ(s, "BEGIN")) {
4656 const char not_safe[] =
4657 "BEGIN not safe after errors--compilation aborted";
4658 if (PL_in_eval & EVAL_KEEPERR)
4659 Perl_croak(aTHX_ not_safe);
4661 /* force display of errors found but not reported */
4662 sv_catpv(ERRSV, not_safe);
4663 Perl_croak(aTHX_ "%"SVf, ERRSV);
4672 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4673 mod(scalarseq(block), OP_LEAVESUBLV));
4676 /* This makes sub {}; work as expected. */
4677 if (block->op_type == OP_STUB) {
4679 block = newSTATEOP(0, Nullch, 0);
4681 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4683 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4684 OpREFCNT_set(CvROOT(cv), 1);
4685 CvSTART(cv) = LINKLIST(CvROOT(cv));
4686 CvROOT(cv)->op_next = 0;
4687 CALL_PEEP(CvSTART(cv));
4689 /* now that optimizer has done its work, adjust pad values */
4691 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4694 assert(!CvCONST(cv));
4695 if (ps && !*ps && op_const_sv(block, cv))
4699 if (name || aname) {
4701 const char * const tname = (name ? name : aname);
4703 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4704 SV * const sv = NEWSV(0,0);
4705 SV * const tmpstr = sv_newmortal();
4706 GV * const db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
4709 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4711 (long)PL_subline, (long)CopLINE(PL_curcop));
4712 gv_efullname3(tmpstr, gv, Nullch);
4713 hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), SvCUR(tmpstr), sv, 0);
4714 hv = GvHVn(db_postponed);
4715 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))) {
4716 CV * const pcv = GvCV(db_postponed);
4722 call_sv((SV*)pcv, G_DISCARD);
4727 if ((s = strrchr(tname,':')))
4732 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4735 if (strEQ(s, "BEGIN") && !PL_error_count) {
4736 const I32 oldscope = PL_scopestack_ix;
4738 SAVECOPFILE(&PL_compiling);
4739 SAVECOPLINE(&PL_compiling);
4742 PL_beginav = newAV();
4743 DEBUG_x( dump_sub(gv) );
4744 av_push(PL_beginav, (SV*)cv);
4745 GvCV(gv) = 0; /* cv has been hijacked */
4746 call_list(oldscope, PL_beginav);
4748 PL_curcop = &PL_compiling;
4749 PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
4752 else if (strEQ(s, "END") && !PL_error_count) {
4755 DEBUG_x( dump_sub(gv) );
4756 av_unshift(PL_endav, 1);
4757 av_store(PL_endav, 0, (SV*)cv);
4758 GvCV(gv) = 0; /* cv has been hijacked */
4760 else if (strEQ(s, "CHECK") && !PL_error_count) {
4762 PL_checkav = newAV();
4763 DEBUG_x( dump_sub(gv) );
4764 if (PL_main_start && ckWARN(WARN_VOID))
4765 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4766 av_unshift(PL_checkav, 1);
4767 av_store(PL_checkav, 0, (SV*)cv);
4768 GvCV(gv) = 0; /* cv has been hijacked */
4770 else if (strEQ(s, "INIT") && !PL_error_count) {
4772 PL_initav = newAV();
4773 DEBUG_x( dump_sub(gv) );
4774 if (PL_main_start && ckWARN(WARN_VOID))
4775 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4776 av_push(PL_initav, (SV*)cv);
4777 GvCV(gv) = 0; /* cv has been hijacked */
4782 PL_copline = NOLINE;
4787 /* XXX unsafe for threads if eval_owner isn't held */
4789 =for apidoc newCONSTSUB
4791 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4792 eligible for inlining at compile-time.
4798 Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
4805 SAVECOPLINE(PL_curcop);
4806 CopLINE_set(PL_curcop, PL_copline);
4809 PL_hints &= ~HINT_BLOCK_SCOPE;
4812 SAVESPTR(PL_curstash);
4813 SAVECOPSTASH(PL_curcop);
4814 PL_curstash = stash;
4815 CopSTASH_set(PL_curcop,stash);
4818 cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));
4819 CvXSUBANY(cv).any_ptr = sv;
4821 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4825 CopSTASH_free(PL_curcop);
4833 =for apidoc U||newXS
4835 Used by C<xsubpp> to hook up XSUBs as Perl subs.
4841 Perl_newXS(pTHX_ const char *name, XSUBADDR_t subaddr, const char *filename)
4843 GV * const gv = gv_fetchpv(name ? name :
4844 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4845 GV_ADDMULTI, SVt_PVCV);
4849 Perl_croak(aTHX_ "panic: no address for '%s' in '%s'", name, filename);
4851 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4853 /* just a cached method */
4857 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4858 /* already defined (or promised) */
4859 /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
4860 if (ckWARN(WARN_REDEFINE)) {
4861 GV * const gvcv = CvGV(cv);
4863 HV * const stash = GvSTASH(gvcv);
4865 const char *name = HvNAME_get(stash);
4866 if ( strEQ(name,"autouse") ) {
4867 const line_t oldline = CopLINE(PL_curcop);
4868 if (PL_copline != NOLINE)
4869 CopLINE_set(PL_curcop, PL_copline);
4870 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4871 CvCONST(cv) ? "Constant subroutine %s redefined"
4872 : "Subroutine %s redefined"
4874 CopLINE_set(PL_curcop, oldline);
4884 if (cv) /* must reuse cv if autoloaded */
4887 cv = (CV*)NEWSV(1105,0);
4888 sv_upgrade((SV *)cv, SVt_PVCV);
4892 PL_sub_generation++;
4896 (void)gv_fetchfile(filename);
4897 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
4898 an external constant string */
4899 CvXSUB(cv) = subaddr;
4902 const char *s = strrchr(name,':');
4908 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4911 if (strEQ(s, "BEGIN")) {
4913 PL_beginav = newAV();
4914 av_push(PL_beginav, (SV*)cv);
4915 GvCV(gv) = 0; /* cv has been hijacked */
4917 else if (strEQ(s, "END")) {
4920 av_unshift(PL_endav, 1);
4921 av_store(PL_endav, 0, (SV*)cv);
4922 GvCV(gv) = 0; /* cv has been hijacked */
4924 else if (strEQ(s, "CHECK")) {
4926 PL_checkav = newAV();
4927 if (PL_main_start && ckWARN(WARN_VOID))
4928 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4929 av_unshift(PL_checkav, 1);
4930 av_store(PL_checkav, 0, (SV*)cv);
4931 GvCV(gv) = 0; /* cv has been hijacked */
4933 else if (strEQ(s, "INIT")) {
4935 PL_initav = newAV();
4936 if (PL_main_start && ckWARN(WARN_VOID))
4937 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4938 av_push(PL_initav, (SV*)cv);
4939 GvCV(gv) = 0; /* cv has been hijacked */
4950 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4955 ? gv_fetchsv(cSVOPo->op_sv, GV_ADD, SVt_PVFM)
4956 : gv_fetchpv("STDOUT", GV_ADD, SVt_PVFM);
4958 #ifdef GV_UNIQUE_CHECK
4960 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4964 if ((cv = GvFORM(gv))) {
4965 if (ckWARN(WARN_REDEFINE)) {
4966 const line_t oldline = CopLINE(PL_curcop);
4967 if (PL_copline != NOLINE)
4968 CopLINE_set(PL_curcop, PL_copline);
4969 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4970 o ? "Format %"SVf" redefined"
4971 : "Format STDOUT redefined" ,cSVOPo->op_sv);
4972 CopLINE_set(PL_curcop, oldline);
4979 CvFILE_set_from_cop(cv, PL_curcop);
4982 pad_tidy(padtidy_FORMAT);
4983 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4984 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4985 OpREFCNT_set(CvROOT(cv), 1);
4986 CvSTART(cv) = LINKLIST(CvROOT(cv));
4987 CvROOT(cv)->op_next = 0;
4988 CALL_PEEP(CvSTART(cv));
4990 PL_copline = NOLINE;
4995 Perl_newANONLIST(pTHX_ OP *o)
4997 return newUNOP(OP_REFGEN, 0,
4998 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
5002 Perl_newANONHASH(pTHX_ OP *o)
5004 return newUNOP(OP_REFGEN, 0,
5005 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
5009 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
5011 return newANONATTRSUB(floor, proto, Nullop, block);
5015 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
5017 return newUNOP(OP_REFGEN, 0,
5018 newSVOP(OP_ANONCODE, 0,
5019 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
5023 Perl_oopsAV(pTHX_ OP *o)
5026 switch (o->op_type) {
5028 o->op_type = OP_PADAV;
5029 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5030 return ref(o, OP_RV2AV);
5033 o->op_type = OP_RV2AV;
5034 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
5039 if (ckWARN_d(WARN_INTERNAL))
5040 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
5047 Perl_oopsHV(pTHX_ OP *o)
5050 switch (o->op_type) {
5053 o->op_type = OP_PADHV;
5054 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5055 return ref(o, OP_RV2HV);
5059 o->op_type = OP_RV2HV;
5060 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
5065 if (ckWARN_d(WARN_INTERNAL))
5066 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
5073 Perl_newAVREF(pTHX_ OP *o)
5076 if (o->op_type == OP_PADANY) {
5077 o->op_type = OP_PADAV;
5078 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5081 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
5082 && ckWARN(WARN_DEPRECATED)) {
5083 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5084 "Using an array as a reference is deprecated");
5086 return newUNOP(OP_RV2AV, 0, scalar(o));
5090 Perl_newGVREF(pTHX_ I32 type, OP *o)
5092 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
5093 return newUNOP(OP_NULL, 0, o);
5094 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5098 Perl_newHVREF(pTHX_ OP *o)
5101 if (o->op_type == OP_PADANY) {
5102 o->op_type = OP_PADHV;
5103 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5106 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
5107 && ckWARN(WARN_DEPRECATED)) {
5108 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5109 "Using a hash as a reference is deprecated");
5111 return newUNOP(OP_RV2HV, 0, scalar(o));
5115 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5117 return newUNOP(OP_RV2CV, flags, scalar(o));
5121 Perl_newSVREF(pTHX_ OP *o)
5124 if (o->op_type == OP_PADANY) {
5125 o->op_type = OP_PADSV;
5126 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5129 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5130 o->op_flags |= OPpDONE_SVREF;
5133 return newUNOP(OP_RV2SV, 0, scalar(o));
5136 /* Check routines. See the comments at the top of this file for details
5137 * on when these are called */
5140 Perl_ck_anoncode(pTHX_ OP *o)
5142 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
5143 cSVOPo->op_sv = Nullsv;
5148 Perl_ck_bitop(pTHX_ OP *o)
5150 #define OP_IS_NUMCOMPARE(op) \
5151 ((op) == OP_LT || (op) == OP_I_LT || \
5152 (op) == OP_GT || (op) == OP_I_GT || \
5153 (op) == OP_LE || (op) == OP_I_LE || \
5154 (op) == OP_GE || (op) == OP_I_GE || \
5155 (op) == OP_EQ || (op) == OP_I_EQ || \
5156 (op) == OP_NE || (op) == OP_I_NE || \
5157 (op) == OP_NCMP || (op) == OP_I_NCMP)
5158 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
5159 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
5160 && (o->op_type == OP_BIT_OR
5161 || o->op_type == OP_BIT_AND
5162 || o->op_type == OP_BIT_XOR))
5164 const OP * const left = cBINOPo->op_first;
5165 const OP * const right = left->op_sibling;
5166 if ((OP_IS_NUMCOMPARE(left->op_type) &&
5167 (left->op_flags & OPf_PARENS) == 0) ||
5168 (OP_IS_NUMCOMPARE(right->op_type) &&
5169 (right->op_flags & OPf_PARENS) == 0))
5170 if (ckWARN(WARN_PRECEDENCE))
5171 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
5172 "Possible precedence problem on bitwise %c operator",
5173 o->op_type == OP_BIT_OR ? '|'
5174 : o->op_type == OP_BIT_AND ? '&' : '^'
5181 Perl_ck_concat(pTHX_ OP *o)
5183 const OP * const kid = cUNOPo->op_first;
5184 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
5185 !(kUNOP->op_first->op_flags & OPf_MOD))
5186 o->op_flags |= OPf_STACKED;
5191 Perl_ck_spair(pTHX_ OP *o)
5194 if (o->op_flags & OPf_KIDS) {
5197 const OPCODE type = o->op_type;
5198 o = modkids(ck_fun(o), type);
5199 kid = cUNOPo->op_first;
5200 newop = kUNOP->op_first->op_sibling;
5202 (newop->op_sibling ||
5203 !(PL_opargs[newop->op_type] & OA_RETSCALAR) ||
5204 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
5205 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
5209 op_free(kUNOP->op_first);
5210 kUNOP->op_first = newop;
5212 o->op_ppaddr = PL_ppaddr[++o->op_type];
5217 Perl_ck_delete(pTHX_ OP *o)
5221 if (o->op_flags & OPf_KIDS) {
5222 OP * const kid = cUNOPo->op_first;
5223 switch (kid->op_type) {
5225 o->op_flags |= OPf_SPECIAL;
5228 o->op_private |= OPpSLICE;
5231 o->op_flags |= OPf_SPECIAL;
5236 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5245 Perl_ck_die(pTHX_ OP *o)
5248 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5254 Perl_ck_eof(pTHX_ OP *o)
5256 const I32 type = o->op_type;
5258 if (o->op_flags & OPf_KIDS) {
5259 if (cLISTOPo->op_first->op_type == OP_STUB) {
5261 o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
5269 Perl_ck_eval(pTHX_ OP *o)
5272 PL_hints |= HINT_BLOCK_SCOPE;
5273 if (o->op_flags & OPf_KIDS) {
5274 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5277 o->op_flags &= ~OPf_KIDS;
5280 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5283 cUNOPo->op_first = 0;
5286 NewOp(1101, enter, 1, LOGOP);
5287 enter->op_type = OP_ENTERTRY;
5288 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5289 enter->op_private = 0;
5291 /* establish postfix order */
5292 enter->op_next = (OP*)enter;
5294 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5295 o->op_type = OP_LEAVETRY;
5296 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5297 enter->op_other = o;
5307 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5309 o->op_targ = (PADOFFSET)PL_hints;
5310 if ((PL_hints & HINT_LOCALIZE_HH) != 0 && GvHV(PL_hintgv)) {
5311 /* Store a copy of %^H that pp_entereval can pick up */
5312 OP *hhop = newSVOP(OP_CONST, 0, (SV*)newHVhv(GvHV(PL_hintgv)));
5313 cUNOPo->op_first->op_sibling = hhop;
5314 o->op_private |= OPpEVAL_HAS_HH;
5320 Perl_ck_exit(pTHX_ OP *o)
5323 HV * const table = GvHV(PL_hintgv);
5325 SV * const * const svp = hv_fetch(table, "vmsish_exit", 11, FALSE);
5326 if (svp && *svp && SvTRUE(*svp))
5327 o->op_private |= OPpEXIT_VMSISH;
5329 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5335 Perl_ck_exec(pTHX_ OP *o)
5337 if (o->op_flags & OPf_STACKED) {
5340 kid = cUNOPo->op_first->op_sibling;
5341 if (kid->op_type == OP_RV2GV)
5350 Perl_ck_exists(pTHX_ OP *o)
5353 if (o->op_flags & OPf_KIDS) {
5354 OP * const kid = cUNOPo->op_first;
5355 if (kid->op_type == OP_ENTERSUB) {
5356 (void) ref(kid, o->op_type);
5357 if (kid->op_type != OP_RV2CV && !PL_error_count)
5358 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5360 o->op_private |= OPpEXISTS_SUB;
5362 else if (kid->op_type == OP_AELEM)
5363 o->op_flags |= OPf_SPECIAL;
5364 else if (kid->op_type != OP_HELEM)
5365 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5373 Perl_ck_rvconst(pTHX_ register OP *o)
5376 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5378 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5379 if (o->op_type == OP_RV2CV)
5380 o->op_private &= ~1;
5382 if (kid->op_type == OP_CONST) {
5385 SV * const kidsv = kid->op_sv;
5387 /* Is it a constant from cv_const_sv()? */
5388 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5389 SV * const rsv = SvRV(kidsv);
5390 const int svtype = SvTYPE(rsv);
5391 const char *badtype = Nullch;
5393 switch (o->op_type) {
5395 if (svtype > SVt_PVMG)
5396 badtype = "a SCALAR";
5399 if (svtype != SVt_PVAV)
5400 badtype = "an ARRAY";
5403 if (svtype != SVt_PVHV)
5407 if (svtype != SVt_PVCV)
5412 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5415 else if ((o->op_type == OP_RV2HV || o->op_type == OP_RV2SV) &&
5416 (PL_hints & HINT_STRICT_REFS) && SvPOK(kidsv)) {
5417 /* If this is an access to a stash, disable "strict refs", because
5418 * stashes aren't auto-vivified at compile-time (unless we store
5419 * symbols in them), and we don't want to produce a run-time
5420 * stricture error when auto-vivifying the stash. */
5421 const char *s = SvPV_nolen(kidsv);
5422 const STRLEN l = SvCUR(kidsv);
5423 if (l > 1 && s[l-1] == ':' && s[l-2] == ':')
5424 o->op_private &= ~HINT_STRICT_REFS;
5426 if ((o->op_private & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5427 const char *badthing = Nullch;
5428 switch (o->op_type) {
5430 badthing = "a SCALAR";
5433 badthing = "an ARRAY";
5436 badthing = "a HASH";
5441 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
5445 * This is a little tricky. We only want to add the symbol if we
5446 * didn't add it in the lexer. Otherwise we get duplicate strict
5447 * warnings. But if we didn't add it in the lexer, we must at
5448 * least pretend like we wanted to add it even if it existed before,
5449 * or we get possible typo warnings. OPpCONST_ENTERED says
5450 * whether the lexer already added THIS instance of this symbol.
5452 iscv = (o->op_type == OP_RV2CV) * 2;
5454 gv = gv_fetchsv(kidsv,
5455 iscv | !(kid->op_private & OPpCONST_ENTERED),
5458 : o->op_type == OP_RV2SV
5460 : o->op_type == OP_RV2AV
5462 : o->op_type == OP_RV2HV
5465 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5467 kid->op_type = OP_GV;
5468 SvREFCNT_dec(kid->op_sv);
5470 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5471 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5472 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
5474 PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc(gv));
5476 kid->op_sv = SvREFCNT_inc(gv);
5478 kid->op_private = 0;
5479 kid->op_ppaddr = PL_ppaddr[OP_GV];
5486 Perl_ck_ftst(pTHX_ OP *o)
5489 const I32 type = o->op_type;
5491 if (o->op_flags & OPf_REF) {
5494 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5495 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5497 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5498 OP * const newop = newGVOP(type, OPf_REF,
5499 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
5505 if ((PL_hints & HINT_FILETEST_ACCESS) &&
5506 OP_IS_FILETEST_ACCESS(o))
5507 o->op_private |= OPpFT_ACCESS;
5509 if (PL_check[kid->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst)
5510 && kid->op_type != OP_STAT && kid->op_type != OP_LSTAT)
5511 o->op_private |= OPpFT_STACKED;
5515 if (type == OP_FTTTY)
5516 o = newGVOP(type, OPf_REF, PL_stdingv);
5518 o = newUNOP(type, 0, newDEFSVOP());
5524 Perl_ck_fun(pTHX_ OP *o)
5526 const int type = o->op_type;
5527 register I32 oa = PL_opargs[type] >> OASHIFT;
5529 if (o->op_flags & OPf_STACKED) {
5530 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5533 return no_fh_allowed(o);
5536 if (o->op_flags & OPf_KIDS) {
5537 OP **tokid = &cLISTOPo->op_first;
5538 register OP *kid = cLISTOPo->op_first;
5542 if (kid->op_type == OP_PUSHMARK ||
5543 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5545 tokid = &kid->op_sibling;
5546 kid = kid->op_sibling;
5548 if (!kid && PL_opargs[type] & OA_DEFGV)
5549 *tokid = kid = newDEFSVOP();
5553 sibl = kid->op_sibling;
5556 /* list seen where single (scalar) arg expected? */
5557 if (numargs == 1 && !(oa >> 4)
5558 && kid->op_type == OP_LIST && type != OP_SCALAR)
5560 return too_many_arguments(o,PL_op_desc[type]);
5573 if ((type == OP_PUSH || type == OP_UNSHIFT)
5574 && !kid->op_sibling && ckWARN(WARN_SYNTAX))
5575 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5576 "Useless use of %s with no values",
5579 if (kid->op_type == OP_CONST &&
5580 (kid->op_private & OPpCONST_BARE))
5582 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
5583 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
5584 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5585 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5586 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
5587 ((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
5590 kid->op_sibling = sibl;
5593 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5594 bad_type(numargs, "array", PL_op_desc[type], kid);
5598 if (kid->op_type == OP_CONST &&
5599 (kid->op_private & OPpCONST_BARE))
5601 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
5602 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
5603 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5604 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5605 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
5606 ((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
5609 kid->op_sibling = sibl;
5612 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5613 bad_type(numargs, "hash", PL_op_desc[type], kid);
5618 OP * const newop = newUNOP(OP_NULL, 0, kid);
5619 kid->op_sibling = 0;
5621 newop->op_next = newop;
5623 kid->op_sibling = sibl;
5628 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5629 if (kid->op_type == OP_CONST &&
5630 (kid->op_private & OPpCONST_BARE))
5632 OP * const newop = newGVOP(OP_GV, 0,
5633 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
5634 if (!(o->op_private & 1) && /* if not unop */
5635 kid == cLISTOPo->op_last)
5636 cLISTOPo->op_last = newop;
5640 else if (kid->op_type == OP_READLINE) {
5641 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5642 bad_type(numargs, "HANDLE", OP_DESC(o), kid);
5645 I32 flags = OPf_SPECIAL;
5649 /* is this op a FH constructor? */
5650 if (is_handle_constructor(o,numargs)) {
5651 const char *name = Nullch;
5655 /* Set a flag to tell rv2gv to vivify
5656 * need to "prove" flag does not mean something
5657 * else already - NI-S 1999/05/07
5660 if (kid->op_type == OP_PADSV) {
5661 name = PAD_COMPNAME_PV(kid->op_targ);
5662 /* SvCUR of a pad namesv can't be trusted
5663 * (see PL_generation), so calc its length
5669 else if (kid->op_type == OP_RV2SV
5670 && kUNOP->op_first->op_type == OP_GV)
5672 GV * const gv = cGVOPx_gv(kUNOP->op_first);
5674 len = GvNAMELEN(gv);
5676 else if (kid->op_type == OP_AELEM
5677 || kid->op_type == OP_HELEM)
5679 OP *op = ((BINOP*)kid)->op_first;
5682 SV *tmpstr = Nullsv;
5683 const char * const a =
5684 kid->op_type == OP_AELEM ?
5686 if (((op->op_type == OP_RV2AV) ||
5687 (op->op_type == OP_RV2HV)) &&
5688 (op = ((UNOP*)op)->op_first) &&
5689 (op->op_type == OP_GV)) {
5690 /* packagevar $a[] or $h{} */
5691 GV * const gv = cGVOPx_gv(op);
5699 else if (op->op_type == OP_PADAV
5700 || op->op_type == OP_PADHV) {
5701 /* lexicalvar $a[] or $h{} */
5702 const char * const padname =
5703 PAD_COMPNAME_PV(op->op_targ);
5712 name = SvPV_const(tmpstr, len);
5717 name = "__ANONIO__";
5724 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5725 namesv = PAD_SVl(targ);
5726 SvUPGRADE(namesv, SVt_PV);
5728 sv_setpvn(namesv, "$", 1);
5729 sv_catpvn(namesv, name, len);
5732 kid->op_sibling = 0;
5733 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5734 kid->op_targ = targ;
5735 kid->op_private |= priv;
5737 kid->op_sibling = sibl;
5743 mod(scalar(kid), type);
5747 tokid = &kid->op_sibling;
5748 kid = kid->op_sibling;
5750 o->op_private |= numargs;
5752 return too_many_arguments(o,OP_DESC(o));
5755 else if (PL_opargs[type] & OA_DEFGV) {
5757 return newUNOP(type, 0, newDEFSVOP());
5761 while (oa & OA_OPTIONAL)
5763 if (oa && oa != OA_LIST)
5764 return too_few_arguments(o,OP_DESC(o));
5770 Perl_ck_glob(pTHX_ OP *o)
5776 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5777 append_elem(OP_GLOB, o, newDEFSVOP());
5779 if (!((gv = gv_fetchpv("glob", 0, SVt_PVCV))
5780 && GvCVu(gv) && GvIMPORTED_CV(gv)))
5782 gv = gv_fetchpv("CORE::GLOBAL::glob", 0, SVt_PVCV);
5785 #if !defined(PERL_EXTERNAL_GLOB)
5786 /* XXX this can be tightened up and made more failsafe. */
5787 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5790 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5791 newSVpvs("File::Glob"), Nullsv, Nullsv, Nullsv);
5792 gv = gv_fetchpv("CORE::GLOBAL::glob", 0, SVt_PVCV);
5793 glob_gv = gv_fetchpv("File::Glob::csh_glob", 0, SVt_PVCV);
5794 GvCV(gv) = GvCV(glob_gv);
5795 (void)SvREFCNT_inc((SV*)GvCV(gv));
5796 GvIMPORTED_CV_on(gv);
5799 #endif /* PERL_EXTERNAL_GLOB */
5801 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5802 append_elem(OP_GLOB, o,
5803 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5804 o->op_type = OP_LIST;
5805 o->op_ppaddr = PL_ppaddr[OP_LIST];
5806 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5807 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5808 cLISTOPo->op_first->op_targ = 0;
5809 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5810 append_elem(OP_LIST, o,
5811 scalar(newUNOP(OP_RV2CV, 0,
5812 newGVOP(OP_GV, 0, gv)))));
5813 o = newUNOP(OP_NULL, 0, ck_subr(o));
5814 o->op_targ = OP_GLOB; /* hint at what it used to be */
5817 gv = newGVgen("main");
5819 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5825 Perl_ck_grep(pTHX_ OP *o)
5830 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5833 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5834 NewOp(1101, gwop, 1, LOGOP);
5836 if (o->op_flags & OPf_STACKED) {
5839 kid = cLISTOPo->op_first->op_sibling;
5840 if (!cUNOPx(kid)->op_next)
5841 Perl_croak(aTHX_ "panic: ck_grep");
5842 for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
5845 kid->op_next = (OP*)gwop;
5846 o->op_flags &= ~OPf_STACKED;
5848 kid = cLISTOPo->op_first->op_sibling;
5849 if (type == OP_MAPWHILE)
5856 kid = cLISTOPo->op_first->op_sibling;
5857 if (kid->op_type != OP_NULL)
5858 Perl_croak(aTHX_ "panic: ck_grep");
5859 kid = kUNOP->op_first;
5861 gwop->op_type = type;
5862 gwop->op_ppaddr = PL_ppaddr[type];
5863 gwop->op_first = listkids(o);
5864 gwop->op_flags |= OPf_KIDS;
5865 gwop->op_other = LINKLIST(kid);
5866 kid->op_next = (OP*)gwop;
5867 offset = pad_findmy("$_");
5868 if (offset == NOT_IN_PAD || PAD_COMPNAME_FLAGS(offset) & SVpad_OUR) {
5869 o->op_private = gwop->op_private = 0;
5870 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5873 o->op_private = gwop->op_private = OPpGREP_LEX;
5874 gwop->op_targ = o->op_targ = offset;
5877 kid = cLISTOPo->op_first->op_sibling;
5878 if (!kid || !kid->op_sibling)
5879 return too_few_arguments(o,OP_DESC(o));
5880 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5881 mod(kid, OP_GREPSTART);
5887 Perl_ck_index(pTHX_ OP *o)
5889 if (o->op_flags & OPf_KIDS) {
5890 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5892 kid = kid->op_sibling; /* get past "big" */
5893 if (kid && kid->op_type == OP_CONST)
5894 fbm_compile(((SVOP*)kid)->op_sv, 0);
5900 Perl_ck_lengthconst(pTHX_ OP *o)
5902 /* XXX length optimization goes here */
5907 Perl_ck_lfun(pTHX_ OP *o)
5909 const OPCODE type = o->op_type;
5910 return modkids(ck_fun(o), type);
5914 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5916 if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
5917 switch (cUNOPo->op_first->op_type) {
5919 /* This is needed for
5920 if (defined %stash::)
5921 to work. Do not break Tk.
5923 break; /* Globals via GV can be undef */
5925 case OP_AASSIGN: /* Is this a good idea? */
5926 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5927 "defined(@array) is deprecated");
5928 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5929 "\t(Maybe you should just omit the defined()?)\n");
5932 /* This is needed for
5933 if (defined %stash::)
5934 to work. Do not break Tk.
5936 break; /* Globals via GV can be undef */
5938 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5939 "defined(%%hash) is deprecated");
5940 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5941 "\t(Maybe you should just omit the defined()?)\n");
5952 Perl_ck_rfun(pTHX_ OP *o)
5954 const OPCODE type = o->op_type;
5955 return refkids(ck_fun(o), type);
5959 Perl_ck_listiob(pTHX_ OP *o)
5963 kid = cLISTOPo->op_first;
5966 kid = cLISTOPo->op_first;
5968 if (kid->op_type == OP_PUSHMARK)
5969 kid = kid->op_sibling;
5970 if (kid && o->op_flags & OPf_STACKED)
5971 kid = kid->op_sibling;
5972 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5973 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5974 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5975 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5976 cLISTOPo->op_first->op_sibling = kid;
5977 cLISTOPo->op_last = kid;
5978 kid = kid->op_sibling;
5983 append_elem(o->op_type, o, newDEFSVOP());
5989 Perl_ck_say(pTHX_ OP *o)
5992 o->op_type = OP_PRINT;
5993 cLISTOPo->op_last = cLISTOPo->op_last->op_sibling
5994 = newSVOP(OP_CONST, 0, newSVpvs("\n"));
5999 Perl_ck_smartmatch(pTHX_ OP *o)
6001 if (0 == (o->op_flags & OPf_SPECIAL)) {
6002 OP *first = cBINOPo->op_first;
6003 OP *second = first->op_sibling;
6005 /* Implicitly take a reference to an array or hash */
6006 first->op_sibling = Nullop;
6007 first = cBINOPo->op_first = ref_array_or_hash(first);
6008 second = first->op_sibling = ref_array_or_hash(second);
6010 /* Implicitly take a reference to a regular expression */
6011 if (first->op_type == OP_MATCH) {
6012 first->op_type = OP_QR;
6013 first->op_ppaddr = PL_ppaddr[OP_QR];
6015 if (second->op_type == OP_MATCH) {
6016 second->op_type = OP_QR;
6017 second->op_ppaddr = PL_ppaddr[OP_QR];
6026 Perl_ck_sassign(pTHX_ OP *o)
6028 OP *kid = cLISTOPo->op_first;
6029 /* has a disposable target? */
6030 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
6031 && !(kid->op_flags & OPf_STACKED)
6032 /* Cannot steal the second time! */
6033 && !(kid->op_private & OPpTARGET_MY))
6035 OP * const kkid = kid->op_sibling;
6037 /* Can just relocate the target. */
6038 if (kkid && kkid->op_type == OP_PADSV
6039 && !(kkid->op_private & OPpLVAL_INTRO))
6041 kid->op_targ = kkid->op_targ;
6043 /* Now we do not need PADSV and SASSIGN. */
6044 kid->op_sibling = o->op_sibling; /* NULL */
6045 cLISTOPo->op_first = NULL;
6048 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
6056 Perl_ck_match(pTHX_ OP *o)
6058 if (o->op_type != OP_QR && PL_compcv) {
6059 const I32 offset = pad_findmy("$_");
6060 if (offset != NOT_IN_PAD && !(PAD_COMPNAME_FLAGS(offset) & SVpad_OUR)) {
6061 o->op_targ = offset;
6062 o->op_private |= OPpTARGET_MY;
6065 if (o->op_type == OP_MATCH || o->op_type == OP_QR)
6066 o->op_private |= OPpRUNTIME;
6071 Perl_ck_method(pTHX_ OP *o)
6073 OP * const kid = cUNOPo->op_first;
6074 if (kid->op_type == OP_CONST) {
6075 SV* sv = kSVOP->op_sv;
6076 if (!(strchr(SvPVX_const(sv), ':') || strchr(SvPVX_const(sv), '\''))) {
6078 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
6079 sv = newSVpvn_share(SvPVX_const(sv), SvCUR(sv), 0);
6082 kSVOP->op_sv = Nullsv;
6084 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
6093 Perl_ck_null(pTHX_ OP *o)
6099 Perl_ck_open(pTHX_ OP *o)
6101 HV * const table = GvHV(PL_hintgv);
6103 SV **svp = hv_fetch(table, "open_IN", 7, FALSE);
6105 const I32 mode = mode_from_discipline(*svp);
6106 if (mode & O_BINARY)
6107 o->op_private |= OPpOPEN_IN_RAW;
6108 else if (mode & O_TEXT)
6109 o->op_private |= OPpOPEN_IN_CRLF;
6112 svp = hv_fetch(table, "open_OUT", 8, FALSE);
6114 const I32 mode = mode_from_discipline(*svp);
6115 if (mode & O_BINARY)
6116 o->op_private |= OPpOPEN_OUT_RAW;
6117 else if (mode & O_TEXT)
6118 o->op_private |= OPpOPEN_OUT_CRLF;
6121 if (o->op_type == OP_BACKTICK)
6124 /* In case of three-arg dup open remove strictness
6125 * from the last arg if it is a bareword. */
6126 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
6127 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
6131 if ((last->op_type == OP_CONST) && /* The bareword. */
6132 (last->op_private & OPpCONST_BARE) &&
6133 (last->op_private & OPpCONST_STRICT) &&
6134 (oa = first->op_sibling) && /* The fh. */
6135 (oa = oa->op_sibling) && /* The mode. */
6136 (oa->op_type == OP_CONST) &&
6137 SvPOK(((SVOP*)oa)->op_sv) &&
6138 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
6139 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
6140 (last == oa->op_sibling)) /* The bareword. */
6141 last->op_private &= ~OPpCONST_STRICT;
6147 Perl_ck_repeat(pTHX_ OP *o)
6149 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
6150 o->op_private |= OPpREPEAT_DOLIST;
6151 cBINOPo->op_first = force_list(cBINOPo->op_first);
6159 Perl_ck_require(pTHX_ OP *o)
6163 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
6164 SVOP * const kid = (SVOP*)cUNOPo->op_first;
6166 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6167 SV * const sv = kid->op_sv;
6168 U32 was_readonly = SvREADONLY(sv);
6173 sv_force_normal_flags(sv, 0);
6174 assert(!SvREADONLY(sv));
6181 for (s = SvPVX(sv); *s; s++) {
6182 if (*s == ':' && s[1] == ':') {
6183 const STRLEN len = strlen(s+2)+1;
6185 Move(s+2, s+1, len, char);
6186 SvCUR_set(sv, SvCUR(sv) - 1);
6189 sv_catpvs(sv, ".pm");
6190 SvFLAGS(sv) |= was_readonly;
6194 if (!(o->op_flags & OPf_SPECIAL)) { /* Wasn't written as CORE::require */
6195 /* handle override, if any */
6196 gv = gv_fetchpv("require", 0, SVt_PVCV);
6197 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
6198 GV * const * const gvp = (GV**)hv_fetch(PL_globalstash, "require", 7, FALSE);
6199 gv = gvp ? *gvp : Nullgv;
6203 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
6204 OP * const kid = cUNOPo->op_first;
6205 cUNOPo->op_first = 0;
6207 return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
6208 append_elem(OP_LIST, kid,
6209 scalar(newUNOP(OP_RV2CV, 0,
6218 Perl_ck_return(pTHX_ OP *o)
6220 if (CvLVALUE(PL_compcv)) {
6222 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
6223 mod(kid, OP_LEAVESUBLV);
6229 Perl_ck_select(pTHX_ OP *o)
6233 if (o->op_flags & OPf_KIDS) {
6234 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6235 if (kid && kid->op_sibling) {
6236 o->op_type = OP_SSELECT;
6237 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
6239 return fold_constants(o);
6243 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6244 if (kid && kid->op_type == OP_RV2GV)
6245 kid->op_private &= ~HINT_STRICT_REFS;
6250 Perl_ck_shift(pTHX_ OP *o)
6252 const I32 type = o->op_type;
6254 if (!(o->op_flags & OPf_KIDS)) {
6258 argop = newUNOP(OP_RV2AV, 0,
6259 scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
6260 return newUNOP(type, 0, scalar(argop));
6262 return scalar(modkids(ck_fun(o), type));
6266 Perl_ck_sort(pTHX_ OP *o)
6270 if (o->op_type == OP_SORT && (PL_hints & HINT_LOCALIZE_HH) != 0)
6272 HV *hinthv = GvHV(PL_hintgv);
6274 SV **svp = hv_fetch(hinthv, "sort", 4, 0);
6276 I32 sorthints = (I32)SvIV(*svp);
6277 if ((sorthints & HINT_SORT_QUICKSORT) != 0)
6278 o->op_private |= OPpSORT_QSORT;
6279 if ((sorthints & HINT_SORT_STABLE) != 0)
6280 o->op_private |= OPpSORT_STABLE;
6285 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6287 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6288 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6290 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6292 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6294 if (kid->op_type == OP_SCOPE) {
6298 else if (kid->op_type == OP_LEAVE) {
6299 if (o->op_type == OP_SORT) {
6300 op_null(kid); /* wipe out leave */
6303 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6304 if (k->op_next == kid)
6306 /* don't descend into loops */
6307 else if (k->op_type == OP_ENTERLOOP
6308 || k->op_type == OP_ENTERITER)
6310 k = cLOOPx(k)->op_lastop;
6315 kid->op_next = 0; /* just disconnect the leave */
6316 k = kLISTOP->op_first;
6321 if (o->op_type == OP_SORT) {
6322 /* provide scalar context for comparison function/block */
6328 o->op_flags |= OPf_SPECIAL;
6330 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6333 firstkid = firstkid->op_sibling;
6336 /* provide list context for arguments */
6337 if (o->op_type == OP_SORT)
6344 S_simplify_sort(pTHX_ OP *o)
6346 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6351 if (!(o->op_flags & OPf_STACKED))
6353 GvMULTI_on(gv_fetchpv("a", GV_ADD, SVt_PV));
6354 GvMULTI_on(gv_fetchpv("b", GV_ADD, SVt_PV));
6355 kid = kUNOP->op_first; /* get past null */
6356 if (kid->op_type != OP_SCOPE)
6358 kid = kLISTOP->op_last; /* get past scope */
6359 switch(kid->op_type) {
6367 k = kid; /* remember this node*/
6368 if (kBINOP->op_first->op_type != OP_RV2SV)
6370 kid = kBINOP->op_first; /* get past cmp */
6371 if (kUNOP->op_first->op_type != OP_GV)
6373 kid = kUNOP->op_first; /* get past rv2sv */
6375 if (GvSTASH(gv) != PL_curstash)
6377 gvname = GvNAME(gv);
6378 if (*gvname == 'a' && gvname[1] == '\0')
6380 else if (*gvname == 'b' && gvname[1] == '\0')
6385 kid = k; /* back to cmp */
6386 if (kBINOP->op_last->op_type != OP_RV2SV)
6388 kid = kBINOP->op_last; /* down to 2nd arg */
6389 if (kUNOP->op_first->op_type != OP_GV)
6391 kid = kUNOP->op_first; /* get past rv2sv */
6393 if (GvSTASH(gv) != PL_curstash)
6395 gvname = GvNAME(gv);
6397 ? !(*gvname == 'a' && gvname[1] == '\0')
6398 : !(*gvname == 'b' && gvname[1] == '\0'))
6400 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6402 o->op_private |= OPpSORT_DESCEND;
6403 if (k->op_type == OP_NCMP)
6404 o->op_private |= OPpSORT_NUMERIC;
6405 if (k->op_type == OP_I_NCMP)
6406 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6407 kid = cLISTOPo->op_first->op_sibling;
6408 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6409 op_free(kid); /* then delete it */
6413 Perl_ck_split(pTHX_ OP *o)
6418 if (o->op_flags & OPf_STACKED)
6419 return no_fh_allowed(o);
6421 kid = cLISTOPo->op_first;
6422 if (kid->op_type != OP_NULL)
6423 Perl_croak(aTHX_ "panic: ck_split");
6424 kid = kid->op_sibling;
6425 op_free(cLISTOPo->op_first);
6426 cLISTOPo->op_first = kid;
6428 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
6429 cLISTOPo->op_last = kid; /* There was only one element previously */
6432 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6433 OP * const sibl = kid->op_sibling;
6434 kid->op_sibling = 0;
6435 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, 0);
6436 if (cLISTOPo->op_first == cLISTOPo->op_last)
6437 cLISTOPo->op_last = kid;
6438 cLISTOPo->op_first = kid;
6439 kid->op_sibling = sibl;
6442 kid->op_type = OP_PUSHRE;
6443 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6445 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL && ckWARN(WARN_REGEXP)) {
6446 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
6447 "Use of /g modifier is meaningless in split");
6450 if (!kid->op_sibling)
6451 append_elem(OP_SPLIT, o, newDEFSVOP());
6453 kid = kid->op_sibling;
6456 if (!kid->op_sibling)
6457 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6459 kid = kid->op_sibling;
6462 if (kid->op_sibling)
6463 return too_many_arguments(o,OP_DESC(o));
6469 Perl_ck_join(pTHX_ OP *o)
6471 const OP * const kid = cLISTOPo->op_first->op_sibling;
6472 if (kid && kid->op_type == OP_MATCH) {
6473 if (ckWARN(WARN_SYNTAX)) {
6474 const REGEXP *re = PM_GETRE(kPMOP);
6475 const char *pmstr = re ? re->precomp : "STRING";
6476 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6477 "/%s/ should probably be written as \"%s\"",
6485 Perl_ck_subr(pTHX_ OP *o)
6487 OP *prev = ((cUNOPo->op_first->op_sibling)
6488 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6489 OP *o2 = prev->op_sibling;
6496 I32 contextclass = 0;
6500 o->op_private |= OPpENTERSUB_HASTARG;
6501 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6502 if (cvop->op_type == OP_RV2CV) {
6504 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6505 op_null(cvop); /* disable rv2cv */
6506 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6507 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6508 GV *gv = cGVOPx_gv(tmpop);
6511 tmpop->op_private |= OPpEARLY_CV;
6514 namegv = CvANON(cv) ? gv : CvGV(cv);
6515 proto = SvPV_nolen((SV*)cv);
6517 if (CvASSERTION(cv)) {
6518 if (PL_hints & HINT_ASSERTING) {
6519 if (PERLDB_ASSERTION && PL_curstash != PL_debstash)
6520 o->op_private |= OPpENTERSUB_DB;
6524 if (!(PL_hints & HINT_ASSERTIONSSEEN) && ckWARN(WARN_ASSERTIONS)) {
6525 Perl_warner(aTHX_ packWARN(WARN_ASSERTIONS),
6526 "Impossible to activate assertion call");
6533 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6534 if (o2->op_type == OP_CONST)
6535 o2->op_private &= ~OPpCONST_STRICT;
6536 else if (o2->op_type == OP_LIST) {
6537 OP * const o = ((UNOP*)o2)->op_first->op_sibling;
6538 if (o && o->op_type == OP_CONST)
6539 o->op_private &= ~OPpCONST_STRICT;
6542 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6543 if (PERLDB_SUB && PL_curstash != PL_debstash)
6544 o->op_private |= OPpENTERSUB_DB;
6545 while (o2 != cvop) {
6549 return too_many_arguments(o, gv_ename(namegv));
6567 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6569 arg == 1 ? "block or sub {}" : "sub {}",
6570 gv_ename(namegv), o2);
6573 /* '*' allows any scalar type, including bareword */
6576 if (o2->op_type == OP_RV2GV)
6577 goto wrapref; /* autoconvert GLOB -> GLOBref */
6578 else if (o2->op_type == OP_CONST)
6579 o2->op_private &= ~OPpCONST_STRICT;
6580 else if (o2->op_type == OP_ENTERSUB) {
6581 /* accidental subroutine, revert to bareword */
6582 OP *gvop = ((UNOP*)o2)->op_first;
6583 if (gvop && gvop->op_type == OP_NULL) {
6584 gvop = ((UNOP*)gvop)->op_first;
6586 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6589 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6590 (gvop = ((UNOP*)gvop)->op_first) &&
6591 gvop->op_type == OP_GV)
6593 GV * const gv = cGVOPx_gv(gvop);
6594 OP * const sibling = o2->op_sibling;
6595 SV * const n = newSVpvs("");
6597 gv_fullname4(n, gv, "", FALSE);
6598 o2 = newSVOP(OP_CONST, 0, n);
6599 prev->op_sibling = o2;
6600 o2->op_sibling = sibling;
6616 if (contextclass++ == 0) {
6617 e = strchr(proto, ']');
6618 if (!e || e == proto)
6627 /* XXX We shouldn't be modifying proto, so we can const proto */
6632 while (*--p != '[');
6633 bad_type(arg, Perl_form(aTHX_ "one of %s", p),
6634 gv_ename(namegv), o2);
6640 if (o2->op_type == OP_RV2GV)
6643 bad_type(arg, "symbol", gv_ename(namegv), o2);
6646 if (o2->op_type == OP_ENTERSUB)
6649 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6652 if (o2->op_type == OP_RV2SV ||
6653 o2->op_type == OP_PADSV ||
6654 o2->op_type == OP_HELEM ||
6655 o2->op_type == OP_AELEM ||
6656 o2->op_type == OP_THREADSV)
6659 bad_type(arg, "scalar", gv_ename(namegv), o2);
6662 if (o2->op_type == OP_RV2AV ||
6663 o2->op_type == OP_PADAV)
6666 bad_type(arg, "array", gv_ename(namegv), o2);
6669 if (o2->op_type == OP_RV2HV ||
6670 o2->op_type == OP_PADHV)
6673 bad_type(arg, "hash", gv_ename(namegv), o2);
6678 OP* const sib = kid->op_sibling;
6679 kid->op_sibling = 0;
6680 o2 = newUNOP(OP_REFGEN, 0, kid);
6681 o2->op_sibling = sib;
6682 prev->op_sibling = o2;
6684 if (contextclass && e) {
6699 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
6700 gv_ename(namegv), cv);
6705 mod(o2, OP_ENTERSUB);
6707 o2 = o2->op_sibling;
6709 if (proto && !optional &&
6710 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
6711 return too_few_arguments(o, gv_ename(namegv));
6714 o=newSVOP(OP_CONST, 0, newSViv(0));
6720 Perl_ck_svconst(pTHX_ OP *o)
6722 SvREADONLY_on(cSVOPo->op_sv);
6727 Perl_ck_trunc(pTHX_ OP *o)
6729 if (o->op_flags & OPf_KIDS) {
6730 SVOP *kid = (SVOP*)cUNOPo->op_first;
6732 if (kid->op_type == OP_NULL)
6733 kid = (SVOP*)kid->op_sibling;
6734 if (kid && kid->op_type == OP_CONST &&
6735 (kid->op_private & OPpCONST_BARE))
6737 o->op_flags |= OPf_SPECIAL;
6738 kid->op_private &= ~OPpCONST_STRICT;
6745 Perl_ck_unpack(pTHX_ OP *o)
6747 OP *kid = cLISTOPo->op_first;
6748 if (kid->op_sibling) {
6749 kid = kid->op_sibling;
6750 if (!kid->op_sibling)
6751 kid->op_sibling = newDEFSVOP();
6757 Perl_ck_substr(pTHX_ OP *o)
6760 if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6761 OP *kid = cLISTOPo->op_first;
6763 if (kid->op_type == OP_NULL)
6764 kid = kid->op_sibling;
6766 kid->op_flags |= OPf_MOD;
6772 /* A peephole optimizer. We visit the ops in the order they're to execute.
6773 * See the comments at the top of this file for more details about when
6774 * peep() is called */
6777 Perl_peep(pTHX_ register OP *o)
6780 register OP* oldop = NULL;
6782 if (!o || o->op_opt)
6786 SAVEVPTR(PL_curcop);
6787 for (; o; o = o->op_next) {
6791 switch (o->op_type) {
6795 PL_curcop = ((COP*)o); /* for warnings */
6800 if (cSVOPo->op_private & OPpCONST_STRICT)
6801 no_bareword_allowed(o);
6803 case OP_METHOD_NAMED:
6804 /* Relocate sv to the pad for thread safety.
6805 * Despite being a "constant", the SV is written to,
6806 * for reference counts, sv_upgrade() etc. */
6808 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6809 if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
6810 /* If op_sv is already a PADTMP then it is being used by
6811 * some pad, so make a copy. */
6812 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
6813 SvREADONLY_on(PAD_SVl(ix));
6814 SvREFCNT_dec(cSVOPo->op_sv);
6816 else if (o->op_type == OP_CONST
6817 && cSVOPo->op_sv == &PL_sv_undef) {
6818 /* PL_sv_undef is hack - it's unsafe to store it in the
6819 AV that is the pad, because av_fetch treats values of
6820 PL_sv_undef as a "free" AV entry and will merrily
6821 replace them with a new SV, causing pad_alloc to think
6822 that this pad slot is free. (When, clearly, it is not)
6824 SvOK_off(PAD_SVl(ix));
6825 SvPADTMP_on(PAD_SVl(ix));
6826 SvREADONLY_on(PAD_SVl(ix));
6829 SvREFCNT_dec(PAD_SVl(ix));
6830 SvPADTMP_on(cSVOPo->op_sv);
6831 PAD_SETSV(ix, cSVOPo->op_sv);
6832 /* XXX I don't know how this isn't readonly already. */
6833 SvREADONLY_on(PAD_SVl(ix));
6835 cSVOPo->op_sv = Nullsv;
6843 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6844 if (o->op_next->op_private & OPpTARGET_MY) {
6845 if (o->op_flags & OPf_STACKED) /* chained concats */
6846 goto ignore_optimization;
6848 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6849 o->op_targ = o->op_next->op_targ;
6850 o->op_next->op_targ = 0;
6851 o->op_private |= OPpTARGET_MY;
6854 op_null(o->op_next);
6856 ignore_optimization:
6860 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6862 break; /* Scalar stub must produce undef. List stub is noop */
6866 if (o->op_targ == OP_NEXTSTATE
6867 || o->op_targ == OP_DBSTATE
6868 || o->op_targ == OP_SETSTATE)
6870 PL_curcop = ((COP*)o);
6872 /* XXX: We avoid setting op_seq here to prevent later calls
6873 to peep() from mistakenly concluding that optimisation
6874 has already occurred. This doesn't fix the real problem,
6875 though (See 20010220.007). AMS 20010719 */
6876 /* op_seq functionality is now replaced by op_opt */
6877 if (oldop && o->op_next) {
6878 oldop->op_next = o->op_next;
6886 if (oldop && o->op_next) {
6887 oldop->op_next = o->op_next;
6895 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
6896 OP* const pop = (o->op_type == OP_PADAV) ?
6897 o->op_next : o->op_next->op_next;
6899 if (pop && pop->op_type == OP_CONST &&
6900 ((PL_op = pop->op_next)) &&
6901 pop->op_next->op_type == OP_AELEM &&
6902 !(pop->op_next->op_private &
6903 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6904 (i = SvIV(((SVOP*)pop)->op_sv) - PL_curcop->cop_arybase)
6909 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
6910 no_bareword_allowed(pop);
6911 if (o->op_type == OP_GV)
6912 op_null(o->op_next);
6913 op_null(pop->op_next);
6915 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6916 o->op_next = pop->op_next->op_next;
6917 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6918 o->op_private = (U8)i;
6919 if (o->op_type == OP_GV) {
6924 o->op_flags |= OPf_SPECIAL;
6925 o->op_type = OP_AELEMFAST;
6931 if (o->op_next->op_type == OP_RV2SV) {
6932 if (!(o->op_next->op_private & OPpDEREF)) {
6933 op_null(o->op_next);
6934 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6936 o->op_next = o->op_next->op_next;
6937 o->op_type = OP_GVSV;
6938 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6941 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6942 GV * const gv = cGVOPo_gv;
6943 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
6944 /* XXX could check prototype here instead of just carping */
6945 SV * const sv = sv_newmortal();
6946 gv_efullname3(sv, gv, Nullch);
6947 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
6948 "%"SVf"() called too early to check prototype",
6952 else if (o->op_next->op_type == OP_READLINE
6953 && o->op_next->op_next->op_type == OP_CONCAT
6954 && (o->op_next->op_next->op_flags & OPf_STACKED))
6956 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
6957 o->op_type = OP_RCATLINE;
6958 o->op_flags |= OPf_STACKED;
6959 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
6960 op_null(o->op_next->op_next);
6961 op_null(o->op_next);
6978 while (cLOGOP->op_other->op_type == OP_NULL)
6979 cLOGOP->op_other = cLOGOP->op_other->op_next;
6980 peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
6986 while (cLOOP->op_redoop->op_type == OP_NULL)
6987 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6988 peep(cLOOP->op_redoop);
6989 while (cLOOP->op_nextop->op_type == OP_NULL)
6990 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6991 peep(cLOOP->op_nextop);
6992 while (cLOOP->op_lastop->op_type == OP_NULL)
6993 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6994 peep(cLOOP->op_lastop);
7001 while (cPMOP->op_pmreplstart &&
7002 cPMOP->op_pmreplstart->op_type == OP_NULL)
7003 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
7004 peep(cPMOP->op_pmreplstart);
7009 if (o->op_next && o->op_next->op_type == OP_NEXTSTATE
7010 && ckWARN(WARN_SYNTAX))
7012 if (o->op_next->op_sibling &&
7013 o->op_next->op_sibling->op_type != OP_EXIT &&
7014 o->op_next->op_sibling->op_type != OP_WARN &&
7015 o->op_next->op_sibling->op_type != OP_DIE) {
7016 const line_t oldline = CopLINE(PL_curcop);
7018 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
7019 Perl_warner(aTHX_ packWARN(WARN_EXEC),
7020 "Statement unlikely to be reached");
7021 Perl_warner(aTHX_ packWARN(WARN_EXEC),
7022 "\t(Maybe you meant system() when you said exec()?)\n");
7023 CopLINE_set(PL_curcop, oldline);
7033 const char *key = NULL;
7038 if (((BINOP*)o)->op_last->op_type != OP_CONST)
7041 /* Make the CONST have a shared SV */
7042 svp = cSVOPx_svp(((BINOP*)o)->op_last);
7043 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
7044 key = SvPV_const(sv, keylen);
7045 lexname = newSVpvn_share(key,
7046 SvUTF8(sv) ? -(I32)keylen : keylen,
7052 if ((o->op_private & (OPpLVAL_INTRO)))
7055 rop = (UNOP*)((BINOP*)o)->op_first;
7056 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
7058 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
7059 if (!(SvFLAGS(lexname) & SVpad_TYPED))
7061 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
7062 if (!fields || !GvHV(*fields))
7064 key = SvPV_const(*svp, keylen);
7065 if (!hv_fetch(GvHV(*fields), key,
7066 SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE))
7068 Perl_croak(aTHX_ "No such class field \"%s\" "
7069 "in variable %s of type %s",
7070 key, SvPV_nolen_const(lexname), HvNAME_get(SvSTASH(lexname)));
7083 SVOP *first_key_op, *key_op;
7085 if ((o->op_private & (OPpLVAL_INTRO))
7086 /* I bet there's always a pushmark... */
7087 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
7088 /* hmmm, no optimization if list contains only one key. */
7090 rop = (UNOP*)((LISTOP*)o)->op_last;
7091 if (rop->op_type != OP_RV2HV)
7093 if (rop->op_first->op_type == OP_PADSV)
7094 /* @$hash{qw(keys here)} */
7095 rop = (UNOP*)rop->op_first;
7097 /* @{$hash}{qw(keys here)} */
7098 if (rop->op_first->op_type == OP_SCOPE
7099 && cLISTOPx(rop->op_first)->op_last->op_type == OP_PADSV)
7101 rop = (UNOP*)cLISTOPx(rop->op_first)->op_last;
7107 lexname = *av_fetch(PL_comppad_name, rop->op_targ, TRUE);
7108 if (!(SvFLAGS(lexname) & SVpad_TYPED))
7110 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
7111 if (!fields || !GvHV(*fields))
7113 /* Again guessing that the pushmark can be jumped over.... */
7114 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
7115 ->op_first->op_sibling;
7116 for (key_op = first_key_op; key_op;
7117 key_op = (SVOP*)key_op->op_sibling) {
7118 if (key_op->op_type != OP_CONST)
7120 svp = cSVOPx_svp(key_op);
7121 key = SvPV_const(*svp, keylen);
7122 if (!hv_fetch(GvHV(*fields), key,
7123 SvUTF8(*svp) ? -(I32)keylen : keylen, FALSE))
7125 Perl_croak(aTHX_ "No such class field \"%s\" "
7126 "in variable %s of type %s",
7127 key, SvPV_nolen(lexname), HvNAME_get(SvSTASH(lexname)));
7134 /* will point to RV2AV or PADAV op on LHS/RHS of assign */
7138 /* check that RHS of sort is a single plain array */
7139 OP *oright = cUNOPo->op_first;
7140 if (!oright || oright->op_type != OP_PUSHMARK)
7143 /* reverse sort ... can be optimised. */
7144 if (!cUNOPo->op_sibling) {
7145 /* Nothing follows us on the list. */
7146 OP * const reverse = o->op_next;
7148 if (reverse->op_type == OP_REVERSE &&
7149 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
7150 OP * const pushmark = cUNOPx(reverse)->op_first;
7151 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
7152 && (cUNOPx(pushmark)->op_sibling == o)) {
7153 /* reverse -> pushmark -> sort */
7154 o->op_private |= OPpSORT_REVERSE;
7156 pushmark->op_next = oright->op_next;
7162 /* make @a = sort @a act in-place */
7166 oright = cUNOPx(oright)->op_sibling;
7169 if (oright->op_type == OP_NULL) { /* skip sort block/sub */
7170 oright = cUNOPx(oright)->op_sibling;
7174 (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
7175 || oright->op_next != o
7176 || (oright->op_private & OPpLVAL_INTRO)
7180 /* o2 follows the chain of op_nexts through the LHS of the
7181 * assign (if any) to the aassign op itself */
7183 if (!o2 || o2->op_type != OP_NULL)
7186 if (!o2 || o2->op_type != OP_PUSHMARK)
7189 if (o2 && o2->op_type == OP_GV)
7192 || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
7193 || (o2->op_private & OPpLVAL_INTRO)
7198 if (!o2 || o2->op_type != OP_NULL)
7201 if (!o2 || o2->op_type != OP_AASSIGN
7202 || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
7205 /* check that the sort is the first arg on RHS of assign */
7207 o2 = cUNOPx(o2)->op_first;
7208 if (!o2 || o2->op_type != OP_NULL)
7210 o2 = cUNOPx(o2)->op_first;
7211 if (!o2 || o2->op_type != OP_PUSHMARK)
7213 if (o2->op_sibling != o)
7216 /* check the array is the same on both sides */
7217 if (oleft->op_type == OP_RV2AV) {
7218 if (oright->op_type != OP_RV2AV
7219 || !cUNOPx(oright)->op_first
7220 || cUNOPx(oright)->op_first->op_type != OP_GV
7221 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
7222 cGVOPx_gv(cUNOPx(oright)->op_first)
7226 else if (oright->op_type != OP_PADAV
7227 || oright->op_targ != oleft->op_targ
7231 /* transfer MODishness etc from LHS arg to RHS arg */
7232 oright->op_flags = oleft->op_flags;
7233 o->op_private |= OPpSORT_INPLACE;
7235 /* excise push->gv->rv2av->null->aassign */
7236 o2 = o->op_next->op_next;
7237 op_null(o2); /* PUSHMARK */
7239 if (o2->op_type == OP_GV) {
7240 op_null(o2); /* GV */
7243 op_null(o2); /* RV2AV or PADAV */
7244 o2 = o2->op_next->op_next;
7245 op_null(o2); /* AASSIGN */
7247 o->op_next = o2->op_next;
7253 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
7255 LISTOP *enter, *exlist;
7258 enter = (LISTOP *) o->op_next;
7261 if (enter->op_type == OP_NULL) {
7262 enter = (LISTOP *) enter->op_next;
7266 /* for $a (...) will have OP_GV then OP_RV2GV here.
7267 for (...) just has an OP_GV. */
7268 if (enter->op_type == OP_GV) {
7269 gvop = (OP *) enter;
7270 enter = (LISTOP *) enter->op_next;
7273 if (enter->op_type == OP_RV2GV) {
7274 enter = (LISTOP *) enter->op_next;
7280 if (enter->op_type != OP_ENTERITER)
7283 iter = enter->op_next;
7284 if (!iter || iter->op_type != OP_ITER)
7287 expushmark = enter->op_first;
7288 if (!expushmark || expushmark->op_type != OP_NULL
7289 || expushmark->op_targ != OP_PUSHMARK)
7292 exlist = (LISTOP *) expushmark->op_sibling;
7293 if (!exlist || exlist->op_type != OP_NULL
7294 || exlist->op_targ != OP_LIST)
7297 if (exlist->op_last != o) {
7298 /* Mmm. Was expecting to point back to this op. */
7301 theirmark = exlist->op_first;
7302 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
7305 if (theirmark->op_sibling != o) {
7306 /* There's something between the mark and the reverse, eg
7307 for (1, reverse (...))
7312 ourmark = ((LISTOP *)o)->op_first;
7313 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
7316 ourlast = ((LISTOP *)o)->op_last;
7317 if (!ourlast || ourlast->op_next != o)
7320 rv2av = ourmark->op_sibling;
7321 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
7322 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
7323 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
7324 /* We're just reversing a single array. */
7325 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
7326 enter->op_flags |= OPf_STACKED;
7329 /* We don't have control over who points to theirmark, so sacrifice
7331 theirmark->op_next = ourmark->op_next;
7332 theirmark->op_flags = ourmark->op_flags;
7333 ourlast->op_next = gvop ? gvop : (OP *) enter;
7336 enter->op_private |= OPpITER_REVERSED;
7337 iter->op_private |= OPpITER_REVERSED;
7344 UNOP *refgen, *rv2cv;
7347 /* I do not understand this, but if o->op_opt isn't set to 1,
7348 various tests in ext/B/t/bytecode.t fail with no readily
7354 if ((o->op_flags && OPf_WANT) != OPf_WANT_VOID)
7357 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
7360 rv2gv = ((BINOP *)o)->op_last;
7361 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
7364 refgen = (UNOP *)((BINOP *)o)->op_first;
7366 if (!refgen || refgen->op_type != OP_REFGEN)
7369 exlist = (LISTOP *)refgen->op_first;
7370 if (!exlist || exlist->op_type != OP_NULL
7371 || exlist->op_targ != OP_LIST)
7374 if (exlist->op_first->op_type != OP_PUSHMARK)
7377 rv2cv = (UNOP*)exlist->op_last;
7379 if (rv2cv->op_type != OP_RV2CV)
7382 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
7383 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
7384 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
7386 o->op_private |= OPpASSIGN_CV_TO_GV;
7387 rv2gv->op_private |= OPpDONT_INIT_GV;
7388 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
7404 Perl_custom_op_name(pTHX_ const OP* o)
7406 const IV index = PTR2IV(o->op_ppaddr);
7410 if (!PL_custom_op_names) /* This probably shouldn't happen */
7411 return (char *)PL_op_name[OP_CUSTOM];
7413 keysv = sv_2mortal(newSViv(index));
7415 he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
7417 return (char *)PL_op_name[OP_CUSTOM]; /* Don't know who you are */
7419 return SvPV_nolen(HeVAL(he));
7423 Perl_custom_op_desc(pTHX_ const OP* o)
7425 const IV index = PTR2IV(o->op_ppaddr);
7429 if (!PL_custom_op_descs)
7430 return (char *)PL_op_desc[OP_CUSTOM];
7432 keysv = sv_2mortal(newSViv(index));
7434 he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
7436 return (char *)PL_op_desc[OP_CUSTOM];
7438 return SvPV_nolen(HeVAL(he));
7443 /* Efficient sub that returns a constant scalar value. */
7445 const_sv_xsub(pTHX_ CV* cv)
7450 Perl_croak(aTHX_ "usage: %s::%s()",
7451 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
7455 ST(0) = (SV*)XSANY.any_ptr;
7461 * c-indentation-style: bsd
7463 * indent-tabs-mode: t
7466 * ex: set ts=8 sts=4 sw=4 noet: