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 ", (OP*)0" 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, NULL);
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((OP *)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 $_" etc etc */
218 (USE_UTF8_IN_NAMES && UTF8_IS_START(name[1])) ||
219 (name[1] == '_' && name[2])))
221 /* name[2] is true if strlen(name) > 2 */
222 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
223 yyerror(Perl_form(aTHX_ "Can't use global %c^%c%s in \"my\"",
224 name[0], toCTRL(name[1]), name + 2));
226 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
229 /* check for duplicate declaration */
230 pad_check_dup(name, is_our, (PL_curstash ? PL_curstash : PL_defstash));
232 if (PL_in_my_stash && *name != '$') {
233 yyerror(Perl_form(aTHX_
234 "Can't declare class for non-scalar %s in \"%s\"",
235 name, is_our ? "our" : "my"));
238 /* allocate a spare slot and store the name in that slot */
240 off = pad_add_name(name,
243 ? (PL_curstash ? PL_curstash : PL_defstash)
252 #ifdef USE_5005THREADS
253 /* find_threadsv is not reentrant */
255 Perl_find_threadsv(pTHX_ const char *name)
260 /* We currently only handle names of a single character */
261 p = strchr(PL_threadsv_names, *name);
264 key = p - PL_threadsv_names;
265 MUTEX_LOCK(&thr->mutex);
266 svp = av_fetch(thr->threadsv, key, FALSE);
268 MUTEX_UNLOCK(&thr->mutex);
271 av_store(thr->threadsv, key, sv);
272 thr->threadsvp = AvARRAY(thr->threadsv);
273 MUTEX_UNLOCK(&thr->mutex);
275 * Some magic variables used to be automagically initialised
276 * in gv_fetchpv. Those which are now per-thread magicals get
277 * initialised here instead.
283 sv_setpv(sv, "\034");
284 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
289 PL_sawampersand = TRUE;
303 /* XXX %! tied to Errno.pm needs to be added here.
304 * See gv_fetchpv(). */
308 sv_magic(sv, 0, PERL_MAGIC_sv, name, 1);
310 DEBUG_S(PerlIO_printf(Perl_error_log,
311 "find_threadsv: new SV %p for $%s%c\n",
312 sv, (*name < 32) ? "^" : "",
313 (*name < 32) ? toCTRL(*name) : *name));
317 #endif /* USE_5005THREADS */
322 Perl_op_free(pTHX_ OP *o)
326 if (!o || o->op_seq == (U16)-1)
330 if (o->op_private & OPpREFCOUNTED) {
341 refcnt = OpREFCNT_dec(o);
352 if (o->op_flags & OPf_KIDS) {
353 register OP *kid, *nextkid;
354 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
355 nextkid = kid->op_sibling; /* Get before next freeing kid */
360 type = (OPCODE)o->op_targ;
362 /* COP* is not cleared by op_clear() so that we may track line
363 * numbers etc even after null() */
364 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
372 Perl_op_clear(pTHX_ OP *o)
375 switch (o->op_type) {
376 case OP_NULL: /* Was holding old type, if any. */
377 case OP_ENTEREVAL: /* Was holding hints. */
378 #ifdef USE_5005THREADS
379 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
383 #ifdef USE_5005THREADS
385 if (!(o->op_flags & OPf_SPECIAL))
388 #endif /* USE_5005THREADS */
390 if (!(o->op_flags & OPf_REF)
391 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
397 if (! (o->op_type == OP_AELEMFAST && o->op_flags & OPf_SPECIAL)) {
398 /* not an OP_PADAV replacement */
400 if (cPADOPo->op_padix > 0) {
401 /* No GvIN_PAD_off(cGVOPo_gv) here, because other references
402 * may still exist on the pad */
403 pad_swipe(cPADOPo->op_padix, TRUE);
404 cPADOPo->op_padix = 0;
407 SvREFCNT_dec(cSVOPo->op_sv);
408 cSVOPo->op_sv = NULL;
412 case OP_METHOD_NAMED:
414 SvREFCNT_dec(cSVOPo->op_sv);
415 cSVOPo->op_sv = NULL;
418 Even if op_clear does a pad_free for the target of the op,
419 pad_free doesn't actually remove the sv that exists in the pad;
420 instead it lives on. This results in that it could be reused as
421 a target later on when the pad was reallocated.
424 pad_swipe(o->op_targ,1);
433 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
437 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
438 SvREFCNT_dec(cSVOPo->op_sv);
439 cSVOPo->op_sv = NULL;
442 Safefree(cPVOPo->op_pv);
443 cPVOPo->op_pv = NULL;
447 op_free(cPMOPo->op_pmreplroot);
451 if (INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot)) {
452 /* No GvIN_PAD_off here, because other references may still
453 * exist on the pad */
454 pad_swipe(INT2PTR(PADOFFSET, cPMOPo->op_pmreplroot), TRUE);
457 SvREFCNT_dec((SV*)cPMOPo->op_pmreplroot);
464 HV * const pmstash = PmopSTASH(cPMOPo);
465 if (pmstash && !SvIS_FREED(pmstash)) {
466 PMOP *pmop = HvPMROOT(pmstash);
467 PMOP *lastpmop = NULL;
469 if (cPMOPo == pmop) {
471 lastpmop->op_pmnext = pmop->op_pmnext;
473 HvPMROOT(pmstash) = pmop->op_pmnext;
477 pmop = pmop->op_pmnext;
480 PmopSTASH_free(cPMOPo);
482 cPMOPo->op_pmreplroot = NULL;
483 /* we use the "SAFE" version of the PM_ macros here
484 * since sv_clean_all might release some PMOPs
485 * after PL_regex_padav has been cleared
486 * and the clearing of PL_regex_padav needs to
487 * happen before sv_clean_all
489 ReREFCNT_dec(PM_GETRE_SAFE(cPMOPo));
490 PM_SETRE_SAFE(cPMOPo, NULL);
492 if(PL_regex_pad) { /* We could be in destruction */
493 av_push((AV*) PL_regex_pad[0],(SV*) PL_regex_pad[(cPMOPo)->op_pmoffset]);
494 SvREPADTMP_on(PL_regex_pad[(cPMOPo)->op_pmoffset]);
495 PM_SETRE(cPMOPo, (cPMOPo)->op_pmoffset);
502 if (o->op_targ > 0) {
503 pad_free(o->op_targ);
509 S_cop_free(pTHX_ COP* cop)
511 Safefree(cop->cop_label); /* FIXME: treaddead ??? */
514 if (! specialWARN(cop->cop_warnings))
515 SvREFCNT_dec(cop->cop_warnings);
516 if (! specialCopIO(cop->cop_io)) {
520 SvREFCNT_dec(cop->cop_io);
523 if (PL_curcop == cop)
528 Perl_op_null(pTHX_ OP *o)
530 if (o->op_type == OP_NULL)
533 o->op_targ = o->op_type;
534 o->op_type = OP_NULL;
535 o->op_ppaddr = PL_ppaddr[OP_NULL];
539 Perl_op_refcnt_lock(pTHX)
546 Perl_op_refcnt_unlock(pTHX)
552 /* Contextualizers */
554 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
557 Perl_linklist(pTHX_ OP *o)
564 /* establish postfix order */
565 first = cUNOPo->op_first;
568 o->op_next = LINKLIST(first);
571 if (kid->op_sibling) {
572 kid->op_next = LINKLIST(kid->op_sibling);
573 kid = kid->op_sibling;
587 Perl_scalarkids(pTHX_ OP *o)
589 if (o && o->op_flags & OPf_KIDS) {
591 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
598 S_scalarboolean(pTHX_ OP *o)
600 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
601 if (ckWARN(WARN_SYNTAX)) {
602 const line_t oldline = CopLINE(PL_curcop);
604 if (PL_copline != NOLINE)
605 CopLINE_set(PL_curcop, PL_copline);
606 Perl_warner(aTHX_ packWARN(WARN_SYNTAX), "Found = in conditional, should be ==");
607 CopLINE_set(PL_curcop, oldline);
614 Perl_scalar(pTHX_ OP *o)
618 /* assumes no premature commitment */
619 if (!o || PL_error_count || (o->op_flags & OPf_WANT)
620 || o->op_type == OP_RETURN)
625 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
627 switch (o->op_type) {
629 scalar(cBINOPo->op_first);
634 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
638 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
639 if (!kPMOP->op_pmreplroot)
640 deprecate_old("implicit split to @_");
648 if (o->op_flags & OPf_KIDS) {
649 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
655 kid = cLISTOPo->op_first;
657 while ((kid = kid->op_sibling)) {
663 PL_curcop = &PL_compiling;
668 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
674 PL_curcop = &PL_compiling;
677 if (ckWARN(WARN_VOID))
678 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of sort in scalar context");
684 Perl_scalarvoid(pTHX_ OP *o)
687 const char* useless = NULL;
691 if (o->op_type == OP_NEXTSTATE
692 || o->op_type == OP_SETSTATE
693 || o->op_type == OP_DBSTATE
694 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
695 || o->op_targ == OP_SETSTATE
696 || o->op_targ == OP_DBSTATE)))
697 PL_curcop = (COP*)o; /* for warning below */
699 /* assumes no premature commitment */
700 want = o->op_flags & OPf_WANT;
701 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
702 || o->op_type == OP_RETURN)
707 if ((o->op_private & OPpTARGET_MY)
708 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
710 return scalar(o); /* As if inside SASSIGN */
713 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
715 switch (o->op_type) {
717 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
721 if (o->op_flags & OPf_STACKED)
725 if (o->op_private == 4)
797 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)))
798 useless = OP_DESC(o);
805 if (!(o->op_private & (OPpLVAL_INTRO|OPpOUR_INTRO)) &&
806 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
807 useless = "a variable";
812 if (cSVOPo->op_private & OPpCONST_STRICT)
813 no_bareword_allowed(o);
815 if (ckWARN(WARN_VOID)) {
816 useless = "a constant";
817 if (o->op_private & OPpCONST_ARYBASE)
819 /* don't warn on optimised away booleans, eg
820 * use constant Foo, 5; Foo || print; */
821 if (cSVOPo->op_private & OPpCONST_SHORTCIRCUIT)
823 /* the constants 0 and 1 are permitted as they are
824 conventionally used as dummies in constructs like
825 1 while some_condition_with_side_effects; */
826 else if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
828 else if (SvPOK(sv)) {
829 /* perl4's way of mixing documentation and code
830 (before the invention of POD) was based on a
831 trick to mix nroff and perl code. The trick was
832 built upon these three nroff macros being used in
833 void context. The pink camel has the details in
834 the script wrapman near page 319. */
835 const char * const maybe_macro = SvPVX_const(sv);
836 if (strnEQ(maybe_macro, "di", 2) ||
837 strnEQ(maybe_macro, "ds", 2) ||
838 strnEQ(maybe_macro, "ig", 2))
843 op_null(o); /* don't execute or even remember it */
847 o->op_type = OP_PREINC; /* pre-increment is faster */
848 o->op_ppaddr = PL_ppaddr[OP_PREINC];
852 o->op_type = OP_PREDEC; /* pre-decrement is faster */
853 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
857 o->op_type = OP_I_PREINC; /* pre-increment is faster */
858 o->op_ppaddr = PL_ppaddr[OP_I_PREINC];
862 o->op_type = OP_I_PREDEC; /* pre-decrement is faster */
863 o->op_ppaddr = PL_ppaddr[OP_I_PREDEC];
869 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
874 if (o->op_flags & OPf_STACKED)
881 if (!(o->op_flags & OPf_KIDS))
890 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
897 /* all requires must return a boolean value */
898 o->op_flags &= ~OPf_WANT;
903 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
904 if (!kPMOP->op_pmreplroot)
905 deprecate_old("implicit split to @_");
909 if (useless && ckWARN(WARN_VOID))
910 Perl_warner(aTHX_ packWARN(WARN_VOID), "Useless use of %s in void context", useless);
915 Perl_listkids(pTHX_ OP *o)
917 if (o && o->op_flags & OPf_KIDS) {
919 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
926 Perl_list(pTHX_ OP *o)
930 /* assumes no premature commitment */
931 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
932 || o->op_type == OP_RETURN)
937 if ((o->op_private & OPpTARGET_MY)
938 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
940 return o; /* As if inside SASSIGN */
943 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
945 switch (o->op_type) {
948 list(cBINOPo->op_first);
953 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
961 if (!(o->op_flags & OPf_KIDS))
963 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
964 list(cBINOPo->op_first);
965 return gen_constant_list(o);
972 kid = cLISTOPo->op_first;
974 while ((kid = kid->op_sibling)) {
980 PL_curcop = &PL_compiling;
984 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
990 PL_curcop = &PL_compiling;
993 /* all requires must return a boolean value */
994 o->op_flags &= ~OPf_WANT;
1001 Perl_scalarseq(pTHX_ OP *o)
1004 const OPCODE type = o->op_type;
1006 if (type == OP_LINESEQ || type == OP_SCOPE ||
1007 type == OP_LEAVE || type == OP_LEAVETRY)
1010 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1011 if (kid->op_sibling) {
1015 PL_curcop = &PL_compiling;
1017 o->op_flags &= ~OPf_PARENS;
1018 if (PL_hints & HINT_BLOCK_SCOPE)
1019 o->op_flags |= OPf_PARENS;
1022 o = newOP(OP_STUB, 0);
1027 S_modkids(pTHX_ OP *o, I32 type)
1029 if (o && o->op_flags & OPf_KIDS) {
1031 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1038 Perl_mod(pTHX_ OP *o, I32 type)
1042 if (!o || PL_error_count)
1045 if ((o->op_private & OPpTARGET_MY)
1046 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1051 switch (o->op_type) {
1056 if (!(o->op_private & OPpCONST_ARYBASE))
1058 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1059 CopARYBASE_set(&PL_compiling,
1060 (I32)SvIV(cSVOPx(PL_eval_start)->op_sv));
1064 SAVECOPARYBASE(&PL_compiling);
1065 CopARYBASE_set(&PL_compiling, 0);
1067 else if (type == OP_REFGEN)
1070 Perl_croak(aTHX_ "That use of $[ is unsupported");
1073 if (o->op_flags & OPf_PARENS)
1077 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1078 !(o->op_flags & OPf_STACKED)) {
1079 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1080 /* The default is to set op_private to the number of children,
1081 which for a UNOP such as RV2CV is always 1. And w're using
1082 the bit for a flag in RV2CV, so we need it clear. */
1083 o->op_private &= ~1;
1084 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1085 assert(cUNOPo->op_first->op_type == OP_NULL);
1086 op_null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1089 else if (o->op_private & OPpENTERSUB_NOMOD)
1091 else { /* lvalue subroutine call */
1092 o->op_private |= OPpLVAL_INTRO;
1093 PL_modcount = RETURN_UNLIMITED_NUMBER;
1094 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) {
1095 /* Backward compatibility mode: */
1096 o->op_private |= OPpENTERSUB_INARGS;
1099 else { /* Compile-time error message: */
1100 OP *kid = cUNOPo->op_first;
1104 if (kid->op_type != OP_PUSHMARK) {
1105 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1107 "panic: unexpected lvalue entersub "
1108 "args: type/targ %ld:%"UVuf,
1109 (long)kid->op_type, (UV)kid->op_targ);
1110 kid = kLISTOP->op_first;
1112 while (kid->op_sibling)
1113 kid = kid->op_sibling;
1114 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1116 if (kid->op_type == OP_METHOD_NAMED
1117 || kid->op_type == OP_METHOD)
1121 NewOp(1101, newop, 1, UNOP);
1122 newop->op_type = OP_RV2CV;
1123 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1124 newop->op_first = NULL;
1125 newop->op_next = (OP*)newop;
1126 kid->op_sibling = (OP*)newop;
1127 newop->op_private |= OPpLVAL_INTRO;
1128 newop->op_private &= ~1;
1132 if (kid->op_type != OP_RV2CV)
1134 "panic: unexpected lvalue entersub "
1135 "entry via type/targ %ld:%"UVuf,
1136 (long)kid->op_type, (UV)kid->op_targ);
1137 kid->op_private |= OPpLVAL_INTRO;
1138 break; /* Postpone until runtime */
1142 kid = kUNOP->op_first;
1143 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1144 kid = kUNOP->op_first;
1145 if (kid->op_type == OP_NULL)
1147 "Unexpected constant lvalue entersub "
1148 "entry via type/targ %ld:%"UVuf,
1149 (long)kid->op_type, (UV)kid->op_targ);
1150 if (kid->op_type != OP_GV) {
1151 /* Restore RV2CV to check lvalueness */
1153 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1154 okid->op_next = kid->op_next;
1155 kid->op_next = okid;
1158 okid->op_next = NULL;
1159 okid->op_type = OP_RV2CV;
1161 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1162 okid->op_private |= OPpLVAL_INTRO;
1163 okid->op_private &= ~1;
1167 cv = GvCV(kGVOP_gv);
1177 /* grep, foreach, subcalls, refgen */
1178 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1180 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
1181 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
1183 : (o->op_type == OP_ENTERSUB
1184 ? "non-lvalue subroutine call"
1186 type ? PL_op_desc[type] : "local"));
1200 case OP_RIGHT_SHIFT:
1209 if (!(o->op_flags & OPf_STACKED))
1215 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1221 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1222 PL_modcount = RETURN_UNLIMITED_NUMBER;
1223 return o; /* Treat \(@foo) like ordinary list. */
1227 if (scalar_mod_type(o, type))
1229 ref(cUNOPo->op_first, o->op_type);
1233 if (type == OP_LEAVESUBLV)
1234 o->op_private |= OPpMAYBE_LVSUB;
1239 PL_modcount = RETURN_UNLIMITED_NUMBER;
1242 ref(cUNOPo->op_first, o->op_type);
1246 PL_hints |= HINT_BLOCK_SCOPE;
1251 /* Needed if maint gets patch 19588
1259 PL_modcount = RETURN_UNLIMITED_NUMBER;
1260 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1261 return o; /* Treat \(@foo) like ordinary list. */
1262 if (scalar_mod_type(o, type))
1264 if (type == OP_LEAVESUBLV)
1265 o->op_private |= OPpMAYBE_LVSUB;
1270 { /* XXX DAPM 2002.08.25 tmp assert test */
1271 /* XXX */ assert(av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1272 /* XXX */ assert(*av_fetch(PL_comppad_name, (o->op_targ), FALSE));
1274 Perl_croak(aTHX_ "Can't localize lexical variable %s",
1275 PAD_COMPNAME_PV(o->op_targ));
1279 #ifdef USE_5005THREADS
1281 PL_modcount++; /* XXX ??? */
1283 #endif /* USE_5005THREADS */
1289 if (type != OP_SASSIGN)
1293 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1298 if (type == OP_LEAVESUBLV)
1299 o->op_private |= OPpMAYBE_LVSUB;
1301 pad_free(o->op_targ);
1302 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1303 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1304 if (o->op_flags & OPf_KIDS)
1305 mod(cBINOPo->op_first->op_sibling, type);
1310 ref(cBINOPo->op_first, o->op_type);
1311 if (type == OP_ENTERSUB &&
1312 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1313 o->op_private |= OPpLVAL_DEFER;
1314 if (type == OP_LEAVESUBLV)
1315 o->op_private |= OPpMAYBE_LVSUB;
1323 if (o->op_flags & OPf_KIDS)
1324 mod(cLISTOPo->op_last, type);
1328 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1330 else if (!(o->op_flags & OPf_KIDS))
1332 if (o->op_targ != OP_LIST) {
1333 mod(cBINOPo->op_first, type);
1338 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1343 if (type != OP_LEAVESUBLV)
1345 break; /* mod()ing was handled by ck_return() */
1348 /* [20011101.069] File test operators interpret OPf_REF to mean that
1349 their argument is a filehandle; thus \stat(".") should not set
1351 if (type == OP_REFGEN &&
1352 PL_check[o->op_type] == MEMBER_TO_FPTR(Perl_ck_ftst))
1355 if (type != OP_LEAVESUBLV)
1356 o->op_flags |= OPf_MOD;
1358 if (type == OP_AASSIGN || type == OP_SASSIGN)
1359 o->op_flags |= OPf_SPECIAL|OPf_REF;
1361 o->op_private |= OPpLVAL_INTRO;
1362 o->op_flags &= ~OPf_SPECIAL;
1363 PL_hints |= HINT_BLOCK_SCOPE;
1365 else if (type != OP_GREPSTART && type != OP_ENTERSUB
1366 && type != OP_LEAVESUBLV)
1367 o->op_flags |= OPf_REF;
1372 S_scalar_mod_type(const OP *o, I32 type)
1376 if (o->op_type == OP_RV2GV)
1400 case OP_RIGHT_SHIFT:
1419 S_is_handle_constructor(const OP *o, I32 numargs)
1421 switch (o->op_type) {
1429 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
1442 Perl_refkids(pTHX_ OP *o, I32 type)
1444 if (o && o->op_flags & OPf_KIDS) {
1446 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1453 Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
1457 if (!o || PL_error_count)
1460 switch (o->op_type) {
1462 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
1463 !(o->op_flags & OPf_STACKED)) {
1464 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1465 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
1466 assert(cUNOPo->op_first->op_type == OP_NULL);
1467 op_null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1468 o->op_flags |= OPf_SPECIAL;
1469 o->op_private &= ~1;
1474 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1475 doref(kid, type, set_op_ref);
1478 if (type == OP_DEFINED)
1479 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1480 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1483 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1484 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1485 : type == OP_RV2HV ? OPpDEREF_HV
1487 o->op_flags |= OPf_MOD;
1491 #ifdef USE_5005THREADS
1493 o->op_flags |= OPf_MOD; /* XXX ??? */
1500 o->op_flags |= OPf_REF;
1503 if (type == OP_DEFINED)
1504 o->op_flags |= OPf_SPECIAL; /* don't create GV */
1505 doref(cUNOPo->op_first, o->op_type, set_op_ref);
1511 o->op_flags |= OPf_REF;
1516 if (!(o->op_flags & OPf_KIDS))
1518 doref(cBINOPo->op_first, type, set_op_ref);
1522 doref(cBINOPo->op_first, o->op_type, set_op_ref);
1523 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1524 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1525 : type == OP_RV2HV ? OPpDEREF_HV
1527 o->op_flags |= OPf_MOD;
1537 if (!(o->op_flags & OPf_KIDS))
1539 doref(cLISTOPo->op_last, type, set_op_ref);
1549 S_dup_attrlist(pTHX_ OP *o)
1553 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1554 * where the first kid is OP_PUSHMARK and the remaining ones
1555 * are OP_CONST. We need to push the OP_CONST values.
1557 if (o->op_type == OP_CONST)
1558 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc_NN(cSVOPo->op_sv));
1560 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1562 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1563 if (o->op_type == OP_CONST)
1564 rop = append_elem(OP_LIST, rop,
1565 newSVOP(OP_CONST, o->op_flags,
1566 SvREFCNT_inc_NN(cSVOPo->op_sv)));
1573 S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs, bool for_my)
1577 /* fake up C<use attributes $pkg,$rv,@attrs> */
1578 ENTER; /* need to protect against side-effects of 'use' */
1581 stashsv = newSVpv(HvNAME_get(stash), 0);
1583 stashsv = &PL_sv_no;
1585 #define ATTRSMODULE "attributes"
1586 #define ATTRSMODULE_PM "attributes.pm"
1589 /* Don't force the C<use> if we don't need it. */
1590 SV * const * const svp = hv_fetchs(GvHVn(PL_incgv), ATTRSMODULE_PM, FALSE);
1591 if (svp && *svp != &PL_sv_undef)
1592 NOOP; /* already in %INC */
1594 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
1595 newSVpvs(ATTRSMODULE), NULL);
1598 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1599 newSVpvs(ATTRSMODULE),
1601 prepend_elem(OP_LIST,
1602 newSVOP(OP_CONST, 0, stashsv),
1603 prepend_elem(OP_LIST,
1604 newSVOP(OP_CONST, 0,
1606 dup_attrlist(attrs))));
1612 S_apply_attrs_my(pTHX_ HV *stash, OP *target, OP *attrs, OP **imopsp)
1614 OP *pack, *imop, *arg;
1620 assert(target->op_type == OP_PADSV ||
1621 target->op_type == OP_PADHV ||
1622 target->op_type == OP_PADAV);
1624 /* Ensure that attributes.pm is loaded. */
1625 apply_attrs(stash, PAD_SV(target->op_targ), attrs, TRUE);
1627 /* Need package name for method call. */
1628 pack = newSVOP(OP_CONST, 0, newSVpvs(ATTRSMODULE));
1630 /* Build up the real arg-list. */
1632 stashsv = newSVpv(HvNAME_get(stash), 0);
1634 stashsv = &PL_sv_no;
1635 arg = newOP(OP_PADSV, 0);
1636 arg->op_targ = target->op_targ;
1637 arg = prepend_elem(OP_LIST,
1638 newSVOP(OP_CONST, 0, stashsv),
1639 prepend_elem(OP_LIST,
1640 newUNOP(OP_REFGEN, 0,
1641 mod(arg, OP_REFGEN)),
1642 dup_attrlist(attrs)));
1644 /* Fake up a method call to import */
1645 meth = newSVpvs("import");
1646 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL|OPf_WANT_VOID,
1647 append_elem(OP_LIST,
1648 prepend_elem(OP_LIST, pack, list(arg)),
1649 newSVOP(OP_METHOD_NAMED, 0, meth)));
1650 imop->op_private |= OPpENTERSUB_NOMOD;
1652 /* Combine the ops. */
1653 *imopsp = append_elem(OP_LIST, *imopsp, imop);
1657 =notfor apidoc apply_attrs_string
1659 Attempts to apply a list of attributes specified by the C<attrstr> and
1660 C<len> arguments to the subroutine identified by the C<cv> argument which
1661 is expected to be associated with the package identified by the C<stashpv>
1662 argument (see L<attributes>). It gets this wrong, though, in that it
1663 does not correctly identify the boundaries of the individual attribute
1664 specifications within C<attrstr>. This is not really intended for the
1665 public API, but has to be listed here for systems such as AIX which
1666 need an explicit export list for symbols. (It's called from XS code
1667 in support of the C<ATTRS:> keyword from F<xsubpp>.) Patches to fix it
1668 to respect attribute syntax properly would be welcome.
1674 Perl_apply_attrs_string(pTHX_ char *stashpv, CV *cv,
1675 char *attrstr, STRLEN len)
1680 len = strlen(attrstr);
1684 for (; isSPACE(*attrstr) && len; --len, ++attrstr) ;
1686 const char * const sstr = attrstr;
1687 for (; !isSPACE(*attrstr) && len; --len, ++attrstr) ;
1688 attrs = append_elem(OP_LIST, attrs,
1689 newSVOP(OP_CONST, 0,
1690 newSVpvn(sstr, attrstr-sstr)));
1694 Perl_load_module(aTHX_ PERL_LOADMOD_IMPORT_OPS,
1695 newSVpvs(ATTRSMODULE),
1696 NULL, prepend_elem(OP_LIST,
1697 newSVOP(OP_CONST, 0, newSVpv(stashpv,0)),
1698 prepend_elem(OP_LIST,
1699 newSVOP(OP_CONST, 0,
1705 S_my_kid(pTHX_ OP *o, OP *attrs, OP **imopsp)
1709 if (!o || PL_error_count)
1713 if (type == OP_LIST) {
1715 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1716 my_kid(kid, attrs, imopsp);
1717 } else if (type == OP_UNDEF) {
1719 } else if (type == OP_RV2SV || /* "our" declaration */
1721 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1722 if (cUNOPo->op_first->op_type != OP_GV) { /* MJD 20011224 */
1723 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1724 OP_DESC(o), PL_in_my == KEY_our ? "our" : "my"));
1726 GV * const gv = cGVOPx_gv(cUNOPo->op_first);
1728 PL_in_my_stash = NULL;
1729 apply_attrs(GvSTASH(gv),
1730 (type == OP_RV2SV ? GvSV(gv) :
1731 type == OP_RV2AV ? (SV*)GvAV(gv) :
1732 type == OP_RV2HV ? (SV*)GvHV(gv) : (SV*)gv),
1735 o->op_private |= OPpOUR_INTRO;
1738 else if (type != OP_PADSV &&
1741 type != OP_PUSHMARK)
1743 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1745 PL_in_my == KEY_our ? "our" : "my"));
1748 else if (attrs && type != OP_PUSHMARK) {
1752 PL_in_my_stash = NULL;
1754 /* check for C<my Dog $spot> when deciding package */
1755 stash = PAD_COMPNAME_TYPE(o->op_targ);
1757 stash = PL_curstash;
1758 apply_attrs_my(stash, o, attrs, imopsp);
1760 o->op_flags |= OPf_MOD;
1761 o->op_private |= OPpLVAL_INTRO;
1766 Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1769 int maybe_scalar = 0;
1771 /* [perl #17376]: this appears to be premature, and results in code such as
1772 C< our(%x); > executing in list mode rather than void mode */
1774 if (o->op_flags & OPf_PARENS)
1784 o = my_kid(o, attrs, &rops);
1786 if (maybe_scalar && o->op_type == OP_PADSV) {
1787 o = scalar(append_list(OP_LIST, (LISTOP*)rops, (LISTOP*)o));
1788 o->op_private |= OPpLVAL_INTRO;
1791 o = append_list(OP_LIST, (LISTOP*)o, (LISTOP*)rops);
1794 PL_in_my_stash = NULL;
1799 Perl_my(pTHX_ OP *o)
1801 return my_attrs(o, NULL);
1805 Perl_sawparens(pTHX_ OP *o)
1807 PERL_UNUSED_CONTEXT;
1809 o->op_flags |= OPf_PARENS;
1814 Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
1818 const OPCODE ltype = left->op_type;
1819 const OPCODE rtype = right->op_type;
1821 if ( (ltype == OP_RV2AV || ltype == OP_RV2HV || ltype == OP_PADAV
1822 || ltype == OP_PADHV) && ckWARN(WARN_MISC))
1824 const char * const desc
1825 = PL_op_desc[(rtype == OP_SUBST || rtype == OP_TRANS)
1826 ? (int)rtype : OP_MATCH];
1827 const char * const sample = ((ltype == OP_RV2AV || ltype == OP_PADAV)
1828 ? "@array" : "%hash");
1829 Perl_warner(aTHX_ packWARN(WARN_MISC),
1830 "Applying %s to %s will act on scalar(%s)",
1831 desc, sample, sample);
1834 if (rtype == OP_CONST &&
1835 cSVOPx(right)->op_private & OPpCONST_BARE &&
1836 cSVOPx(right)->op_private & OPpCONST_STRICT)
1838 no_bareword_allowed(right);
1841 ismatchop = rtype == OP_MATCH ||
1842 rtype == OP_SUBST ||
1844 if (!(right->op_flags & OPf_STACKED) && ismatchop) {
1847 right->op_flags |= OPf_STACKED;
1848 if (rtype != OP_MATCH &&
1849 ! (rtype == OP_TRANS &&
1850 right->op_private & OPpTRANS_IDENTICAL))
1851 newleft = mod(left, rtype);
1854 if (right->op_type == OP_TRANS)
1855 o = newBINOP(OP_NULL, OPf_STACKED, scalar(newleft), right);
1857 o = prepend_elem(rtype, scalar(newleft), right);
1859 return newUNOP(OP_NOT, 0, scalar(o));
1863 return bind_match(type, left,
1864 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1868 Perl_invert(pTHX_ OP *o)
1872 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1873 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1877 Perl_scope(pTHX_ OP *o)
1880 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
1881 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1882 o->op_type = OP_LEAVE;
1883 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
1885 else if (o->op_type == OP_LINESEQ) {
1887 o->op_type = OP_SCOPE;
1888 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
1889 kid = ((LISTOP*)o)->op_first;
1890 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
1893 /* The following deals with things like 'do {1 for 1}' */
1894 kid = kid->op_sibling;
1896 (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE))
1901 o = newLISTOP(OP_SCOPE, 0, o, NULL);
1907 Perl_block_start(pTHX_ int full)
1909 const int retval = PL_savestack_ix;
1910 /* If there were syntax errors, don't try to start a block */
1911 if (PL_yynerrs) return retval;
1913 pad_block_start(full);
1915 PL_hints &= ~HINT_BLOCK_SCOPE;
1916 SAVESPTR(PL_compiling.cop_warnings);
1917 if (! specialWARN(PL_compiling.cop_warnings)) {
1918 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1919 SAVEFREESV(PL_compiling.cop_warnings) ;
1921 SAVESPTR(PL_compiling.cop_io);
1922 if (! specialCopIO(PL_compiling.cop_io)) {
1923 PL_compiling.cop_io = newSVsv(PL_compiling.cop_io) ;
1924 SAVEFREESV(PL_compiling.cop_io) ;
1930 Perl_block_end(pTHX_ I32 floor, OP *seq)
1932 const int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
1933 OP* const retval = scalarseq(seq);
1934 /* If there were syntax errors, don't try to close a block */
1935 if (PL_yynerrs) return retval;
1937 CopHINTS_set(&PL_compiling, PL_hints);
1939 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1947 #ifdef USE_5005THREADS
1948 OP *const o = newOP(OP_THREADSV, 0);
1949 o->op_targ = find_threadsv("_");
1952 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
1953 #endif /* USE_5005THREADS */
1957 Perl_newPROG(pTHX_ OP *o)
1962 PL_eval_root = newUNOP(OP_LEAVEEVAL,
1963 ((PL_in_eval & EVAL_KEEPERR)
1964 ? OPf_SPECIAL : 0), o);
1965 PL_eval_start = linklist(PL_eval_root);
1966 PL_eval_root->op_private |= OPpREFCOUNTED;
1967 OpREFCNT_set(PL_eval_root, 1);
1968 PL_eval_root->op_next = 0;
1969 CALL_PEEP(PL_eval_start);
1972 if (o->op_type == OP_STUB) {
1973 PL_comppad_name = 0;
1978 PL_main_root = scope(sawparens(scalarvoid(o)));
1979 PL_curcop = &PL_compiling;
1980 PL_main_start = LINKLIST(PL_main_root);
1981 PL_main_root->op_private |= OPpREFCOUNTED;
1982 OpREFCNT_set(PL_main_root, 1);
1983 PL_main_root->op_next = 0;
1984 CALL_PEEP(PL_main_start);
1987 /* Register with debugger */
1989 CV * const cv = get_cv("DB::postponed", FALSE);
1993 XPUSHs((SV*)CopFILEGV(&PL_compiling));
1995 call_sv((SV*)cv, G_DISCARD);
2002 Perl_localize(pTHX_ OP *o, I32 lex)
2004 if (o->op_flags & OPf_PARENS)
2005 /* [perl #17376]: this appears to be premature, and results in code such as
2006 C< our(%x); > executing in list mode rather than void mode */
2013 if ( PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ','
2014 && ckWARN(WARN_PARENTHESIS))
2016 char *s = PL_bufptr;
2019 /* some heuristics to detect a potential error */
2020 while (*s && (strchr(", \t\n", *s)))
2024 if (*s && strchr("@$%*", *s) && *++s
2025 && (isALNUM(*s) || UTF8_IS_CONTINUED(*s))) {
2028 while (*s && (isALNUM(*s) || UTF8_IS_CONTINUED(*s)))
2030 while (*s && (strchr(", \t\n", *s)))
2036 if (sigil && (*s == ';' || *s == '=')) {
2037 Perl_warner(aTHX_ packWARN(WARN_PARENTHESIS),
2038 "Parentheses missing around \"%s\" list",
2039 lex ? (PL_in_my == KEY_our ? "our" : "my")
2047 o = mod(o, OP_NULL); /* a bit kludgey */
2049 PL_in_my_stash = NULL;
2054 Perl_jmaybe(pTHX_ OP *o)
2056 if (o->op_type == OP_LIST) {
2057 #ifdef USE_5005THREADS
2058 OP * const o2 = newOP(OP_THREADSV, 0);
2059 o2->op_targ = find_threadsv(";");
2062 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpvs(";", GV_ADD|GV_NOTQUAL, SVt_PV)));
2063 #endif /* USE_5005THREADS */
2064 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
2069 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2071 S_vcall_runops(pTHX_ va_list args)
2079 Perl_fold_constants(pTHX_ register OP *o)
2082 VOL I32 type = o->op_type;
2087 SV * const oldwarnhook = PL_warnhook;
2088 SV * const olddiehook = PL_diehook;
2091 if (PL_opargs[type] & OA_RETSCALAR)
2093 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
2094 o->op_targ = pad_alloc(type, SVs_PADTMP);
2096 /* integerize op, unless it happens to be C<-foo>.
2097 * XXX should pp_i_negate() do magic string negation instead? */
2098 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2099 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2100 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2102 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
2105 if (!(PL_opargs[type] & OA_FOLDCONST))
2110 /* XXX might want a ck_negate() for this */
2111 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2122 /* XXX what about the numeric ops? */
2123 if (PL_hints & HINT_LOCALE)
2128 goto nope; /* Don't try to run w/ errors */
2130 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2131 const OPCODE type = curop->op_type;
2132 if ((type != OP_CONST || (curop->op_private & OPpCONST_BARE)) &&
2134 type != OP_SCALAR &&
2136 type != OP_PUSHMARK)
2142 curop = LINKLIST(o);
2143 old_next = o->op_next;
2147 oldscope = PL_scopestack_ix;
2149 create_eval_scope(G_FAKINGEVAL);
2151 PL_warnhook = PERL_WARNHOOK_FATAL;
2154 #ifdef PERL_FLEXIBLE_EXCEPTIONS
2155 CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_runops));
2161 #ifndef PERL_FLEXIBLE_EXCEPTIONS
2164 sv = *(PL_stack_sp--);
2165 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
2166 pad_swipe(o->op_targ, FALSE);
2167 else if (SvTEMP(sv)) { /* grab mortal temp? */
2168 SvREFCNT_inc_simple_void(sv);
2173 /* Something tried to die. Abandon constant folding. */
2174 /* Pretend the error never happened. */
2175 sv_setpvn(ERRSV,"",0);
2176 o->op_next = old_next;
2180 /* Don't expect 1 (setjmp failed) or 2 (something called my_exit) */
2181 PL_warnhook = oldwarnhook;
2182 PL_diehook = olddiehook;
2183 /* XXX note that this croak may fail as we've already blown away
2184 * the stack - eg any nested evals */
2185 Perl_croak(aTHX_ "panic: fold_constants JMPENV_PUSH returned %d", ret);
2188 PL_warnhook = oldwarnhook;
2189 PL_diehook = olddiehook;
2191 if (PL_scopestack_ix > oldscope) {
2192 delete_eval_scope();
2201 if (type == OP_RV2GV)
2202 return newGVOP(OP_GV, 0, (GV*)sv);
2203 return newSVOP(OP_CONST, 0, (SV*)sv);
2210 Perl_gen_constant_list(pTHX_ register OP *o)
2213 const I32 oldtmps_floor = PL_tmps_floor;
2217 return o; /* Don't attempt to run with errors */
2219 PL_op = curop = LINKLIST(o);
2225 assert (!(curop->op_flags & OPf_SPECIAL));
2226 assert(curop->op_type == OP_RANGE);
2228 PL_tmps_floor = oldtmps_floor;
2230 o->op_type = OP_RV2AV;
2231 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
2232 o->op_flags &= ~OPf_REF; /* treat \(1..2) like an ordinary list */
2233 o->op_flags |= OPf_PARENS; /* and flatten \(1..2,3) */
2234 o->op_seq = 0; /* needs to be revisited in peep() */
2235 curop = ((UNOP*)o)->op_first;
2236 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc_NN(*PL_stack_sp--));
2243 Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
2245 if (!o || o->op_type != OP_LIST)
2246 o = newLISTOP(OP_LIST, 0, o, NULL);
2248 o->op_flags &= ~OPf_WANT;
2250 if (!(PL_opargs[type] & OA_MARK))
2251 op_null(cLISTOPo->op_first);
2253 o->op_type = (OPCODE)type;
2254 o->op_ppaddr = PL_ppaddr[type];
2255 o->op_flags |= flags;
2257 o = CHECKOP(type, o);
2258 if (o->op_type != (unsigned)type)
2261 return fold_constants(o);
2264 /* List constructors */
2267 Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
2275 if (first->op_type != (unsigned)type
2276 || (type == OP_LIST && (first->op_flags & OPf_PARENS)))
2278 return newLISTOP(type, 0, first, last);
2281 if (first->op_flags & OPf_KIDS)
2282 ((LISTOP*)first)->op_last->op_sibling = last;
2284 first->op_flags |= OPf_KIDS;
2285 ((LISTOP*)first)->op_first = last;
2287 ((LISTOP*)first)->op_last = last;
2292 Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
2300 if (first->op_type != (unsigned)type)
2301 return prepend_elem(type, (OP*)first, (OP*)last);
2303 if (last->op_type != (unsigned)type)
2304 return append_elem(type, (OP*)first, (OP*)last);
2306 first->op_last->op_sibling = last->op_first;
2307 first->op_last = last->op_last;
2308 first->op_flags |= (last->op_flags & OPf_KIDS);
2316 Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
2324 if (last->op_type == (unsigned)type) {
2325 if (type == OP_LIST) { /* already a PUSHMARK there */
2326 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2327 ((LISTOP*)last)->op_first->op_sibling = first;
2328 if (!(first->op_flags & OPf_PARENS))
2329 last->op_flags &= ~OPf_PARENS;
2332 if (!(last->op_flags & OPf_KIDS)) {
2333 ((LISTOP*)last)->op_last = first;
2334 last->op_flags |= OPf_KIDS;
2336 first->op_sibling = ((LISTOP*)last)->op_first;
2337 ((LISTOP*)last)->op_first = first;
2339 last->op_flags |= OPf_KIDS;
2343 return newLISTOP(type, 0, first, last);
2349 Perl_newNULLLIST(pTHX)
2351 return newOP(OP_STUB, 0);
2355 Perl_force_list(pTHX_ OP *o)
2357 if (!o || o->op_type != OP_LIST)
2358 o = newLISTOP(OP_LIST, 0, o, NULL);
2364 Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2368 NewOp(1101, listop, 1, LISTOP);
2370 listop->op_type = (OPCODE)type;
2371 listop->op_ppaddr = PL_ppaddr[type];
2374 listop->op_flags = (U8)flags;
2378 else if (!first && last)
2381 first->op_sibling = last;
2382 listop->op_first = first;
2383 listop->op_last = last;
2384 if (type == OP_LIST) {
2385 OP* const pushop = newOP(OP_PUSHMARK, 0);
2386 pushop->op_sibling = first;
2387 listop->op_first = pushop;
2388 listop->op_flags |= OPf_KIDS;
2390 listop->op_last = pushop;
2393 return CHECKOP(type, listop);
2397 Perl_newOP(pTHX_ I32 type, I32 flags)
2400 NewOp(1101, o, 1, OP);
2401 o->op_type = (OPCODE)type;
2402 o->op_ppaddr = PL_ppaddr[type];
2403 o->op_flags = (U8)flags;
2406 o->op_private = (U8)(0 | (flags >> 8));
2407 if (PL_opargs[type] & OA_RETSCALAR)
2409 if (PL_opargs[type] & OA_TARGET)
2410 o->op_targ = pad_alloc(type, SVs_PADTMP);
2411 return CHECKOP(type, o);
2415 Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
2420 first = newOP(OP_STUB, 0);
2421 if (PL_opargs[type] & OA_MARK)
2422 first = force_list(first);
2424 NewOp(1101, unop, 1, UNOP);
2425 unop->op_type = (OPCODE)type;
2426 unop->op_ppaddr = PL_ppaddr[type];
2427 unop->op_first = first;
2428 unop->op_flags = (U8)(flags | OPf_KIDS);
2429 unop->op_private = (U8)(1 | (flags >> 8));
2430 unop = (UNOP*) CHECKOP(type, unop);
2434 return fold_constants((OP *) unop);
2438 Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
2441 NewOp(1101, binop, 1, BINOP);
2444 first = newOP(OP_NULL, 0);
2446 binop->op_type = (OPCODE)type;
2447 binop->op_ppaddr = PL_ppaddr[type];
2448 binop->op_first = first;
2449 binop->op_flags = (U8)(flags | OPf_KIDS);
2452 binop->op_private = (U8)(1 | (flags >> 8));
2455 binop->op_private = (U8)(2 | (flags >> 8));
2456 first->op_sibling = last;
2459 binop = (BINOP*)CHECKOP(type, binop);
2460 if (binop->op_next || binop->op_type != (OPCODE)type)
2463 binop->op_last = binop->op_first->op_sibling;
2465 return fold_constants((OP *)binop);
2468 static int uvcompare(const void *a, const void *b)
2469 __attribute__nonnull__(1)
2470 __attribute__nonnull__(2)
2471 __attribute__pure__;
2472 static int uvcompare(const void *a, const void *b)
2474 if (*((const UV *)a) < (*(const UV *)b))
2476 if (*((const UV *)a) > (*(const UV *)b))
2478 if (*((const UV *)a+1) < (*(const UV *)b+1))
2480 if (*((const UV *)a+1) > (*(const UV *)b+1))
2486 Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
2488 SV * const tstr = ((SVOP*)expr)->op_sv;
2489 SV * const rstr = ((SVOP*)repl)->op_sv;
2492 const U8 *t = (U8*)SvPV_const(tstr, tlen);
2493 const U8 *r = (U8*)SvPV_const(rstr, rlen);
2497 register short *tbl;
2499 const I32 complement = o->op_private & OPpTRANS_COMPLEMENT;
2500 const I32 squash = o->op_private & OPpTRANS_SQUASH;
2501 I32 del = o->op_private & OPpTRANS_DELETE;
2502 PL_hints |= HINT_BLOCK_SCOPE;
2505 o->op_private |= OPpTRANS_FROM_UTF;
2508 o->op_private |= OPpTRANS_TO_UTF;
2510 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
2511 SV* const listsv = newSVpvs("# comment\n");
2513 const U8* tend = t + tlen;
2514 const U8* rend = r + rlen;
2528 const I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2529 const I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2535 t = tsave = bytes_to_utf8((U8 *)t, &len);
2538 if (!to_utf && rlen) {
2540 r = rsave = bytes_to_utf8((U8 *)r, &len);
2544 /* There are several snags with this code on EBCDIC:
2545 1. 0xFF is a legal UTF-EBCDIC byte (there are no illegal bytes).
2546 2. scan_const() in toke.c has encoded chars in native encoding which makes
2547 ranges at least in EBCDIC 0..255 range the bottom odd.
2551 U8 tmpbuf[UTF8_MAXBYTES+1];
2554 Newx(cp, 2*tlen, UV);
2556 transv = newSVpvs("");
2558 cp[2*i] = utf8n_to_uvuni((U8 *)t, tend-t, &ulen, 0);
2560 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) {
2562 cp[2*i+1] = utf8n_to_uvuni((U8 *)t, tend-t, &ulen, 0);
2566 cp[2*i+1] = cp[2*i];
2570 qsort(cp, i, 2*sizeof(UV), uvcompare);
2571 for (j = 0; j < i; j++) {
2573 diff = val - nextmin;
2575 t = uvuni_to_utf8(tmpbuf,nextmin);
2576 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2578 U8 range_mark = UTF_TO_NATIVE(0xff);
2579 t = uvuni_to_utf8(tmpbuf, val - 1);
2580 sv_catpvn(transv, (char *)&range_mark, 1);
2581 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2588 t = uvuni_to_utf8(tmpbuf,nextmin);
2589 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2591 U8 range_mark = UTF_TO_NATIVE(0xff);
2592 sv_catpvn(transv, (char *)&range_mark, 1);
2594 t = uvuni_to_utf8_flags(tmpbuf, 0x7fffffff,
2595 UNICODE_ALLOW_SUPER);
2596 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2597 t = (const U8*)SvPVX_const(transv);
2598 tlen = SvCUR(transv);
2602 else if (!rlen && !del) {
2603 r = t; rlen = tlen; rend = tend;
2606 if ((!rlen && !del) || t == r ||
2607 (tlen == rlen && memEQ((char *)t, (char *)r, tlen)))
2609 o->op_private |= OPpTRANS_IDENTICAL;
2613 while (t < tend || tfirst <= tlast) {
2614 /* see if we need more "t" chars */
2615 if (tfirst > tlast) {
2616 tfirst = (I32)utf8n_to_uvuni((U8 *)t, tend - t, &ulen, 0);
2618 if (t < tend && NATIVE_TO_UTF(*t) == 0xff) { /* illegal utf8 val indicates range */
2620 tlast = (I32)utf8n_to_uvuni((U8 *)t, tend - t, &ulen, 0);
2627 /* now see if we need more "r" chars */
2628 if (rfirst > rlast) {
2630 rfirst = (I32)utf8n_to_uvuni((U8 *)r, rend - r, &ulen, 0);
2632 if (r < rend && NATIVE_TO_UTF(*r) == 0xff) { /* illegal utf8 val indicates range */
2634 rlast = (I32)utf8n_to_uvuni((U8 *)r, rend - r, &ulen,
2644 rfirst = rlast = 0xffffffff;
2648 /* now see which range will peter our first, if either. */
2649 tdiff = tlast - tfirst;
2650 rdiff = rlast - rfirst;
2657 if (rfirst == 0xffffffff) {
2658 diff = tdiff; /* oops, pretend rdiff is infinite */
2660 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2661 (long)tfirst, (long)tlast);
2663 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
2667 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2668 (long)tfirst, (long)(tfirst + diff),
2671 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2672 (long)tfirst, (long)rfirst);
2674 if (rfirst + diff > max)
2675 max = rfirst + diff;
2677 grows = (tfirst < rfirst &&
2678 UNISKIP(tfirst) < UNISKIP(rfirst + diff));
2690 else if (max > 0xff)
2695 Safefree(cPVOPo->op_pv);
2696 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2697 SvREFCNT_dec(listsv);
2698 SvREFCNT_dec(transv);
2700 if (!del && havefinal && rlen)
2701 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5,
2702 newSVuv((UV)final), 0);
2705 o->op_private |= OPpTRANS_GROWS;
2715 tbl = (short*)cPVOPo->op_pv;
2717 Zero(tbl, 256, short);
2718 for (i = 0; i < (I32)tlen; i++)
2720 for (i = 0, j = 0; i < 256; i++) {
2722 if (j >= (I32)rlen) {
2731 if (i < 128 && r[j] >= 128)
2741 o->op_private |= OPpTRANS_IDENTICAL;
2743 else if (j >= (I32)rlen)
2746 cPVOPo->op_pv = (char*)Renew(tbl, 0x101+rlen-j, short);
2747 tbl[0x100] = (short)(rlen - j);
2748 for (i=0; i < (I32)rlen - j; i++)
2749 tbl[0x101+i] = r[j+i];
2753 if (!rlen && !del) {
2756 o->op_private |= OPpTRANS_IDENTICAL;
2758 else if (!squash && rlen == tlen && memEQ((char*)t, (char*)r, tlen)) {
2759 o->op_private |= OPpTRANS_IDENTICAL;
2761 for (i = 0; i < 256; i++)
2763 for (i = 0, j = 0; i < (I32)tlen; i++,j++) {
2764 if (j >= (I32)rlen) {
2766 if (tbl[t[i]] == -1)
2772 if (tbl[t[i]] == -1) {
2773 if (t[i] < 128 && r[j] >= 128)
2780 o->op_private |= OPpTRANS_GROWS;
2788 Perl_newPMOP(pTHX_ I32 type, I32 flags)
2792 NewOp(1101, pmop, 1, PMOP);
2793 pmop->op_type = (OPCODE)type;
2794 pmop->op_ppaddr = PL_ppaddr[type];
2795 pmop->op_flags = (U8)flags;
2796 pmop->op_private = (U8)(0 | (flags >> 8));
2798 if (PL_hints & HINT_RE_TAINT)
2799 pmop->op_pmpermflags |= PMf_RETAINT;
2800 if (PL_hints & HINT_LOCALE)
2801 pmop->op_pmpermflags |= PMf_LOCALE;
2802 pmop->op_pmflags = pmop->op_pmpermflags;
2805 if (av_len((AV*) PL_regex_pad[0]) > -1) {
2806 SV * const repointer = av_pop((AV*)PL_regex_pad[0]);
2807 pmop->op_pmoffset = SvIV(repointer);
2808 SvREPADTMP_off(repointer);
2809 sv_setiv(repointer,0);
2811 SV * const repointer = newSViv(0);
2812 av_push(PL_regex_padav, SvREFCNT_inc_simple_NN(repointer));
2813 pmop->op_pmoffset = av_len(PL_regex_padav);
2814 PL_regex_pad = AvARRAY(PL_regex_padav);
2818 /* link into pm list */
2819 if (type != OP_TRANS && PL_curstash) {
2820 pmop->op_pmnext = HvPMROOT(PL_curstash);
2821 HvPMROOT(PL_curstash) = pmop;
2822 PmopSTASH_set(pmop,PL_curstash);
2825 return CHECKOP(type, pmop);
2829 Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
2833 I32 repl_has_vars = 0;
2835 if (o->op_type == OP_TRANS)
2836 return pmtrans(o, expr, repl);
2838 PL_hints |= HINT_BLOCK_SCOPE;
2841 if (expr->op_type == OP_CONST) {
2843 SV * const pat = ((SVOP*)expr)->op_sv;
2844 const char *p = SvPV_const(pat, plen);
2845 if ((o->op_flags & OPf_SPECIAL) && (*p == ' ' && p[1] == '\0')) {
2846 U32 was_readonly = SvREADONLY(pat);
2850 sv_force_normal_flags(pat, 0);
2851 assert(!SvREADONLY(pat));
2854 SvREADONLY_off(pat);
2858 sv_setpvn(pat, "\\s+", 3);
2860 SvFLAGS(pat) |= was_readonly;
2862 p = SvPV_const(pat, plen);
2863 pm->op_pmflags |= PMf_SKIPWHITE;
2866 pm->op_pmdynflags |= PMdf_UTF8;
2867 /* FIXME - can we make this function take const char * args? */
2868 PM_SETRE(pm, CALLREGCOMP(aTHX_ (char*)p, (char*)p + plen, pm));
2869 if (strEQ("\\s+", PM_GETRE(pm)->precomp))
2870 pm->op_pmflags |= PMf_WHITE;
2874 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2875 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2877 : OP_REGCMAYBE),0,expr);
2879 NewOp(1101, rcop, 1, LOGOP);
2880 rcop->op_type = OP_REGCOMP;
2881 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
2882 rcop->op_first = scalar(expr);
2883 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2884 ? (OPf_SPECIAL | OPf_KIDS)
2886 rcop->op_private = 1;
2889 /* establish postfix order */
2890 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
2892 rcop->op_next = expr;
2893 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2896 rcop->op_next = LINKLIST(expr);
2897 expr->op_next = (OP*)rcop;
2900 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2905 if (pm->op_pmflags & PMf_EVAL) {
2907 if (CopLINE(PL_curcop) < (line_t)PL_multi_end)
2908 CopLINE_set(PL_curcop, (line_t)PL_multi_end);
2910 #ifdef USE_5005THREADS
2911 else if (repl->op_type == OP_THREADSV
2912 && strchr("&`'123456789+",
2913 PL_threadsv_names[repl->op_targ]))
2917 #endif /* USE_5005THREADS */
2918 else if (repl->op_type == OP_CONST)
2922 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2923 if (curop->op_type == OP_SCOPE
2924 || curop->op_type == OP_LEAVE
2925 || (PL_opargs[curop->op_type] & OA_DANGEROUS)) {
2926 #ifdef USE_5005THREADS
2927 if (curop->op_type == OP_THREADSV) {
2929 if (strchr("&`'123456789+", curop->op_private))
2933 if (curop->op_type == OP_GV) {
2934 GV * const gv = cGVOPx_gv(curop);
2936 if (strchr("&`'123456789+-\016\022", *GvENAME(gv)))
2939 #endif /* USE_5005THREADS */
2940 else if (curop->op_type == OP_RV2CV)
2942 else if (curop->op_type == OP_RV2SV ||
2943 curop->op_type == OP_RV2AV ||
2944 curop->op_type == OP_RV2HV ||
2945 curop->op_type == OP_RV2GV) {
2946 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2949 else if (curop->op_type == OP_PADSV ||
2950 curop->op_type == OP_PADAV ||
2951 curop->op_type == OP_PADHV ||
2952 curop->op_type == OP_PADANY)
2956 else if (curop->op_type == OP_PUSHRE)
2957 NOOP; /* Okay here, dangerous in newASSIGNOP */
2967 || PM_GETRE(pm)->reganch & ROPT_EVAL_SEEN))) {
2968 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2969 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2970 prepend_elem(o->op_type, scalar(repl), o);
2973 if (curop == repl && !PM_GETRE(pm)) { /* Has variables. */
2974 pm->op_pmflags |= PMf_MAYBE_CONST;
2975 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2977 NewOp(1101, rcop, 1, LOGOP);
2978 rcop->op_type = OP_SUBSTCONT;
2979 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
2980 rcop->op_first = scalar(repl);
2981 rcop->op_flags |= OPf_KIDS;
2982 rcop->op_private = 1;
2985 /* establish postfix order */
2986 rcop->op_next = LINKLIST(repl);
2987 repl->op_next = (OP*)rcop;
2989 pm->op_pmreplroot = scalar((OP*)rcop);
2990 pm->op_pmreplstart = LINKLIST(rcop);
2999 Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
3002 NewOp(1101, svop, 1, SVOP);
3003 svop->op_type = (OPCODE)type;
3004 svop->op_ppaddr = PL_ppaddr[type];
3006 svop->op_next = (OP*)svop;
3007 svop->op_flags = (U8)flags;
3008 if (PL_opargs[type] & OA_RETSCALAR)
3010 if (PL_opargs[type] & OA_TARGET)
3011 svop->op_targ = pad_alloc(type, SVs_PADTMP);
3012 return CHECKOP(type, svop);
3016 Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3019 NewOp(1101, padop, 1, PADOP);
3020 padop->op_type = (OPCODE)type;
3021 padop->op_ppaddr = PL_ppaddr[type];
3022 padop->op_padix = pad_alloc(type, SVs_PADTMP);
3023 SvREFCNT_dec(PAD_SVl(padop->op_padix));
3024 PAD_SETSV(padop->op_padix, sv);
3027 padop->op_next = (OP*)padop;
3028 padop->op_flags = (U8)flags;
3029 if (PL_opargs[type] & OA_RETSCALAR)
3031 if (PL_opargs[type] & OA_TARGET)
3032 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3033 return CHECKOP(type, padop);
3037 Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
3042 return newPADOP(type, flags, SvREFCNT_inc_simple(gv));
3044 return newSVOP(type, flags, SvREFCNT_inc_simple(gv));
3049 Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
3052 NewOp(1101, pvop, 1, PVOP);
3053 pvop->op_type = (OPCODE)type;
3054 pvop->op_ppaddr = PL_ppaddr[type];
3056 pvop->op_next = (OP*)pvop;
3057 pvop->op_flags = (U8)flags;
3058 if (PL_opargs[type] & OA_RETSCALAR)
3060 if (PL_opargs[type] & OA_TARGET)
3061 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
3062 return CHECKOP(type, pvop);
3066 Perl_package(pTHX_ OP *o)
3070 save_hptr(&PL_curstash);
3071 save_item(PL_curstname);
3076 name = SvPV_const(sv, len);
3077 PL_curstash = gv_stashpvn(name,len,TRUE);
3078 sv_setpvn(PL_curstname, name, len);
3082 deprecate("\"package\" with no arguments");
3083 sv_setpv(PL_curstname,"<none>");
3084 PL_curstash = Nullhv;
3086 PL_hints |= HINT_BLOCK_SCOPE;
3087 PL_copline = NOLINE;
3092 Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *idop, OP *arg)
3098 if (idop->op_type != OP_CONST)
3099 Perl_croak(aTHX_ "Module name must be constant");
3104 SV * const vesv = ((SVOP*)version)->op_sv;
3106 if (!arg && !SvNIOKp(vesv)) {
3113 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
3114 Perl_croak(aTHX_ "Version number must be constant number");
3116 /* Make copy of idop so we don't free it twice */
3117 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3119 /* Fake up a method call to VERSION */
3120 meth = newSVpvs("VERSION");
3121 sv_upgrade(meth, SVt_PVIV);
3122 (void)SvIOK_on(meth);
3125 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3126 SvUV_set(meth, hash);
3128 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3129 append_elem(OP_LIST,
3130 prepend_elem(OP_LIST, pack, list(version)),
3131 newSVOP(OP_METHOD_NAMED, 0, meth)));
3135 /* Fake up an import/unimport */
3136 if (arg && arg->op_type == OP_STUB)
3137 imop = arg; /* no import on explicit () */
3138 else if (SvNIOKp(((SVOP*)idop)->op_sv)) {
3139 imop = NULL; /* use 5.0; */
3141 idop->op_private |= OPpCONST_NOVER;
3146 /* Make copy of idop so we don't free it twice */
3147 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)idop)->op_sv));
3149 /* Fake up a method call to import/unimport */
3150 meth = aver ? newSVpvs("import") : newSVpvs("unimport");
3151 (void)SvUPGRADE(meth, SVt_PVIV);
3152 (void)SvIOK_on(meth);
3155 PERL_HASH(hash, SvPVX_const(meth), SvCUR(meth));
3156 SvUV_set(meth, hash);
3158 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3159 append_elem(OP_LIST,
3160 prepend_elem(OP_LIST, pack, list(arg)),
3161 newSVOP(OP_METHOD_NAMED, 0, meth)));
3164 /* Fake up the BEGIN {}, which does its thing immediately. */
3166 newSVOP(OP_CONST, 0, newSVpvs("BEGIN")),
3169 append_elem(OP_LINESEQ,
3170 append_elem(OP_LINESEQ,
3171 newSTATEOP(0, NULL, newUNOP(OP_REQUIRE, 0, idop)),
3172 newSTATEOP(0, NULL, veop)),
3173 newSTATEOP(0, NULL, imop) ));
3175 /* The "did you use incorrect case?" warning used to be here.
3176 * The problem is that on case-insensitive filesystems one
3177 * might get false positives for "use" (and "require"):
3178 * "use Strict" or "require CARP" will work. This causes
3179 * portability problems for the script: in case-strict
3180 * filesystems the script will stop working.
3182 * The "incorrect case" warning checked whether "use Foo"
3183 * imported "Foo" to your namespace, but that is wrong, too:
3184 * there is no requirement nor promise in the language that
3185 * a Foo.pm should or would contain anything in package "Foo".
3187 * There is very little Configure-wise that can be done, either:
3188 * the case-sensitivity of the build filesystem of Perl does not
3189 * help in guessing the case-sensitivity of the runtime environment.
3192 PL_hints |= HINT_BLOCK_SCOPE;
3193 PL_copline = NOLINE;
3195 PL_cop_seqmax++; /* Purely for B::*'s benefit */
3199 =head1 Embedding Functions
3201 =for apidoc load_module
3203 Loads the module whose name is pointed to by the string part of name.
3204 Note that the actual module name, not its filename, should be given.
3205 Eg, "Foo::Bar" instead of "Foo/Bar.pm". flags can be any of
3206 PERL_LOADMOD_DENY, PERL_LOADMOD_NOIMPORT, or PERL_LOADMOD_IMPORT_OPS
3207 (or 0 for no flags). ver, if specified, provides version semantics
3208 similar to C<use Foo::Bar VERSION>. The optional trailing SV*
3209 arguments can be used to specify arguments to the module's import()
3210 method, similar to C<use Foo::Bar VERSION LIST>.
3215 Perl_load_module(pTHX_ U32 flags, SV *name, SV *ver, ...)
3218 va_start(args, ver);
3219 vload_module(flags, name, ver, &args);
3223 #ifdef PERL_IMPLICIT_CONTEXT
3225 Perl_load_module_nocontext(U32 flags, SV *name, SV *ver, ...)
3229 va_start(args, ver);
3230 vload_module(flags, name, ver, &args);
3236 Perl_vload_module(pTHX_ U32 flags, SV *name, SV *ver, va_list *args)
3240 OP * const modname = newSVOP(OP_CONST, 0, name);
3241 modname->op_private |= OPpCONST_BARE;
3243 veop = newSVOP(OP_CONST, 0, ver);
3247 if (flags & PERL_LOADMOD_NOIMPORT) {
3248 imop = sawparens(newNULLLIST());
3250 else if (flags & PERL_LOADMOD_IMPORT_OPS) {
3251 imop = va_arg(*args, OP*);
3256 sv = va_arg(*args, SV*);
3258 imop = append_elem(OP_LIST, imop, newSVOP(OP_CONST, 0, sv));
3259 sv = va_arg(*args, SV*);
3263 const line_t ocopline = PL_copline;
3264 COP * const ocurcop = PL_curcop;
3265 const int oexpect = PL_expect;
3267 utilize(!(flags & PERL_LOADMOD_DENY), start_subparse(FALSE, 0),
3268 veop, modname, imop);
3269 PL_expect = oexpect;
3270 PL_copline = ocopline;
3271 PL_curcop = ocurcop;
3276 Perl_dofile(pTHX_ OP *term)
3278 return dofile2(term, 0);
3282 Perl_dofile2(pTHX_ OP *term, I32 force_builtin)
3287 if (!force_builtin) {
3288 gv = gv_fetchpvs("do", GV_NOTQUAL, SVt_PVCV);
3289 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
3290 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "do", FALSE);
3291 gv = gvp ? *gvp : Nullgv;
3295 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
3296 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3297 append_elem(OP_LIST, term,
3298 scalar(newUNOP(OP_RV2CV, 0,
3299 newGVOP(OP_GV, 0, gv))))));
3302 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3308 Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
3310 return newBINOP(OP_LSLICE, flags,
3311 list(force_list(subscript)),
3312 list(force_list(listval)) );
3316 S_is_list_assignment(pTHX_ register const OP *o)
3324 if ((o->op_type == OP_NULL) && (o->op_flags & OPf_KIDS))
3325 o = cUNOPo->op_first;
3327 flags = o->op_flags;
3329 if (type == OP_COND_EXPR) {
3330 const I32 t = is_list_assignment(cLOGOPo->op_first->op_sibling);
3331 const I32 f = is_list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
3336 yyerror("Assignment to both a list and a scalar");
3340 if (type == OP_LIST &&
3341 (flags & OPf_WANT) == OPf_WANT_SCALAR &&
3342 o->op_private & OPpLVAL_INTRO)
3345 if (type == OP_LIST || flags & OPf_PARENS ||
3346 type == OP_RV2AV || type == OP_RV2HV ||
3347 type == OP_ASLICE || type == OP_HSLICE)
3350 if (type == OP_PADAV || type == OP_PADHV)
3353 if (type == OP_RV2SV)
3360 Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
3365 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3366 return newLOGOP(optype, 0,
3367 mod(scalar(left), optype),
3368 newUNOP(OP_SASSIGN, 0, scalar(right)));
3371 return newBINOP(optype, OPf_STACKED,
3372 mod(scalar(left), optype), scalar(right));
3376 if (is_list_assignment(left)) {
3380 /* Grandfathering $[ assignment here. Bletch.*/
3381 /* Only simple assignments like C<< ($[) = 1 >> are allowed */
3382 PL_eval_start = (left->op_type == OP_CONST) ? right : 0;
3383 left = mod(left, OP_AASSIGN);
3386 else if (left->op_type == OP_CONST) {
3387 /* Result of assignment is always 1 (or we'd be dead already) */
3388 return newSVOP(OP_CONST, 0, newSViv(1));
3390 curop = list(force_list(left));
3391 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
3392 o->op_private = (U8)(0 | (flags >> 8));
3393 for (curop = ((LISTOP*)curop)->op_first;
3394 curop; curop = curop->op_sibling)
3396 if (curop->op_type == OP_RV2HV &&
3397 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3398 o->op_private |= OPpASSIGN_HASH;
3403 /* PL_generation sorcery:
3404 * an assignment like ($a,$b) = ($c,$d) is easier than
3405 * ($a,$b) = ($c,$a), since there is no need for temporary vars.
3406 * To detect whether there are common vars, the global var
3407 * PL_generation is incremented for each assign op we compile.
3408 * Then, while compiling the assign op, we run through all the
3409 * variables on both sides of the assignment, setting a spare slot
3410 * in each of them to PL_generation. If any of them already have
3411 * that value, we know we've got commonality. We could use a
3412 * single bit marker, but then we'd have to make 2 passes, first
3413 * to clear the flag, then to test and set it. To find somewhere
3414 * to store these values, evil chicanery is done with SvCUR().
3419 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
3420 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
3421 if (curop->op_type == OP_GV) {
3422 GV *gv = cGVOPx_gv(curop);
3424 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
3426 GvASSIGN_GENERATION_set(gv, PL_generation);
3428 else if (curop->op_type == OP_PADSV ||
3429 curop->op_type == OP_PADAV ||
3430 curop->op_type == OP_PADHV ||
3431 curop->op_type == OP_PADANY)
3433 if ((int)PAD_COMPNAME_GEN(curop->op_targ)
3436 PAD_COMPNAME_GEN_set(curop->op_targ, PL_generation);
3439 else if (curop->op_type == OP_RV2CV)
3441 else if (curop->op_type == OP_RV2SV ||
3442 curop->op_type == OP_RV2AV ||
3443 curop->op_type == OP_RV2HV ||
3444 curop->op_type == OP_RV2GV) {
3445 if (lastop->op_type != OP_GV) /* funny deref? */
3448 else if (curop->op_type == OP_PUSHRE) {
3449 if (((PMOP*)curop)->op_pmreplroot) {
3451 GV *gv = (GV*)PAD_SVl(INT2PTR(PADOFFSET,
3452 ((PMOP*)curop)->op_pmreplroot));
3454 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3457 || (int)GvASSIGN_GENERATION(gv) == PL_generation)
3459 GvASSIGN_GENERATION_set(gv, PL_generation);
3460 GvASSIGN_GENERATION_set(gv, PL_generation);
3469 o->op_private |= OPpASSIGN_COMMON;
3471 if (right && right->op_type == OP_SPLIT) {
3472 OP* tmpop = ((LISTOP*)right)->op_first;
3473 if (tmpop && (tmpop->op_type == OP_PUSHRE)) {
3474 PMOP * const pm = (PMOP*)tmpop;
3475 if (left->op_type == OP_RV2AV &&
3476 !(left->op_private & OPpLVAL_INTRO) &&
3477 !(o->op_private & OPpASSIGN_COMMON) )
3479 tmpop = ((UNOP*)left)->op_first;
3480 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
3482 pm->op_pmreplroot = INT2PTR(OP*, cPADOPx(tmpop)->op_padix);
3483 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3485 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3486 cSVOPx(tmpop)->op_sv = NULL; /* steal it */
3488 pm->op_pmflags |= PMf_ONCE;
3489 tmpop = cUNOPo->op_first; /* to list (nulled) */
3490 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3491 tmpop->op_sibling = NULL; /* don't free split */
3492 right->op_next = tmpop->op_next; /* fix starting loc */
3493 op_free(o); /* blow off assign */
3494 right->op_flags &= ~OPf_WANT;
3495 /* "I don't know and I don't care." */
3500 if (PL_modcount < RETURN_UNLIMITED_NUMBER &&
3501 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3503 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3505 sv_setiv(sv, PL_modcount+1);
3513 right = newOP(OP_UNDEF, 0);
3514 if (right->op_type == OP_READLINE) {
3515 right->op_flags |= OPf_STACKED;
3516 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
3519 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
3520 o = newBINOP(OP_SASSIGN, flags,
3521 scalar(right), mod(scalar(left), OP_SASSIGN) );
3526 o = newSVOP(OP_CONST, 0, newSViv(CopARYBASE_get(&PL_compiling)));
3527 o->op_private |= OPpCONST_ARYBASE;
3534 Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
3536 const U32 seq = intro_my();
3539 NewOp(1101, cop, 1, COP);
3540 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
3541 cop->op_type = OP_DBSTATE;
3542 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
3545 cop->op_type = OP_NEXTSTATE;
3546 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
3548 cop->op_flags = (U8)flags;
3549 CopHINTS_set(cop, PL_hints);
3551 cop->op_private |= NATIVE_HINTS;
3553 CopHINTS_set(&PL_compiling, CopHINTS_get(cop));
3554 cop->op_next = (OP*)cop;
3557 cop->cop_label = label;
3558 PL_hints |= HINT_BLOCK_SCOPE;
3561 CopARYBASE_set(cop, CopARYBASE_get(PL_curcop));
3562 if (specialWARN(PL_curcop->cop_warnings))
3563 cop->cop_warnings = PL_curcop->cop_warnings ;
3565 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3566 if (specialCopIO(PL_curcop->cop_io))
3567 cop->cop_io = PL_curcop->cop_io;
3569 cop->cop_io = newSVsv(PL_curcop->cop_io) ;
3572 if (PL_copline == NOLINE)
3573 CopLINE_set(cop, CopLINE(PL_curcop));
3575 CopLINE_set(cop, PL_copline);
3576 PL_copline = NOLINE;
3579 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXX share in a pvtable? */
3581 CopFILEGV_set(cop, CopFILEGV(PL_curcop));
3583 CopSTASH_set(cop, PL_curstash);
3585 if (PERLDB_LINE && PL_curstash != PL_debstash) {
3586 AV *av = CopFILEAVx(PL_curcop);
3588 SV * const * const svp = av_fetch(av, (I32)CopLINE(cop), FALSE);
3589 if (svp && *svp != &PL_sv_undef ) {
3590 (void)SvIOK_on(*svp);
3591 SvIV_set(*svp, PTR2IV(cop));
3596 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
3601 Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
3603 return new_logop(type, flags, &first, &other);
3607 S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
3611 OP *first = *firstp;
3612 OP * const other = *otherp;
3614 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3615 return newBINOP(type, flags, scalar(first), scalar(other));
3617 scalarboolean(first);
3618 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3619 if (first->op_type == OP_NOT
3620 && (first->op_flags & OPf_SPECIAL)
3621 && (first->op_flags & OPf_KIDS)) {
3622 if (type == OP_AND || type == OP_OR) {
3628 first = *firstp = cUNOPo->op_first;
3630 first->op_next = o->op_next;
3631 cUNOPo->op_first = NULL;
3635 if (first->op_type == OP_CONST) {
3636 if (first->op_private & OPpCONST_STRICT)
3637 no_bareword_allowed(first);
3638 else if ((first->op_private & OPpCONST_BARE) && ckWARN(WARN_BAREWORD))
3639 Perl_warner(aTHX_ packWARN(WARN_BAREWORD), "Bareword found in conditional");
3640 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3643 if (other->op_type == OP_CONST)
3644 other->op_private |= OPpCONST_SHORTCIRCUIT;
3650 if (first->op_type == OP_CONST)
3651 first->op_private |= OPpCONST_SHORTCIRCUIT;
3655 else if ((first->op_flags & OPf_KIDS) && ckWARN(WARN_MISC)) {
3656 const OP * const k1 = ((UNOP*)first)->op_first;
3657 const OP * const k2 = k1->op_sibling;
3659 switch (first->op_type)
3662 if (k2 && k2->op_type == OP_READLINE
3663 && (k2->op_flags & OPf_STACKED)
3664 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3666 warnop = k2->op_type;
3671 if (k1->op_type == OP_READDIR
3672 || k1->op_type == OP_GLOB
3673 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3674 || k1->op_type == OP_EACH)
3676 warnop = ((k1->op_type == OP_NULL)
3677 ? (OPCODE)k1->op_targ : k1->op_type);
3682 const line_t oldline = CopLINE(PL_curcop);
3683 CopLINE_set(PL_curcop, PL_copline);
3684 Perl_warner(aTHX_ packWARN(WARN_MISC),
3685 "Value of %s%s can be \"0\"; test with defined()",
3687 ((warnop == OP_READLINE || warnop == OP_GLOB)
3688 ? " construct" : "() operator"));
3689 CopLINE_set(PL_curcop, oldline);
3696 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3697 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3699 NewOp(1101, logop, 1, LOGOP);
3701 logop->op_type = (OPCODE)type;
3702 logop->op_ppaddr = PL_ppaddr[type];
3703 logop->op_first = first;
3704 logop->op_flags = (U8)(flags | OPf_KIDS);
3705 logop->op_other = LINKLIST(other);
3706 logop->op_private = (U8)(1 | (flags >> 8));
3708 /* establish postfix order */
3709 logop->op_next = LINKLIST(first);
3710 first->op_next = (OP*)logop;
3711 first->op_sibling = other;
3713 CHECKOP(type,logop);
3715 o = newUNOP(OP_NULL, 0, (OP*)logop);
3722 Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
3729 return newLOGOP(OP_AND, 0, first, trueop);
3731 return newLOGOP(OP_OR, 0, first, falseop);
3733 scalarboolean(first);
3734 if (first->op_type == OP_CONST) {
3735 if (first->op_private & OPpCONST_BARE &&
3736 first->op_private & OPpCONST_STRICT) {
3737 no_bareword_allowed(first);
3739 if (SvTRUE(((SVOP*)first)->op_sv)) {
3750 NewOp(1101, logop, 1, LOGOP);
3751 logop->op_type = OP_COND_EXPR;
3752 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3753 logop->op_first = first;
3754 logop->op_flags = (U8)(flags | OPf_KIDS);
3755 logop->op_private = (U8)(1 | (flags >> 8));
3756 logop->op_other = LINKLIST(trueop);
3757 logop->op_next = LINKLIST(falseop);
3759 CHECKOP(OP_COND_EXPR, /* that's logop->op_type */
3762 /* establish postfix order */
3763 start = LINKLIST(first);
3764 first->op_next = (OP*)logop;
3766 first->op_sibling = trueop;
3767 trueop->op_sibling = falseop;
3768 o = newUNOP(OP_NULL, 0, (OP*)logop);
3770 trueop->op_next = falseop->op_next = o;
3777 Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
3785 NewOp(1101, range, 1, LOGOP);
3787 range->op_type = OP_RANGE;
3788 range->op_ppaddr = PL_ppaddr[OP_RANGE];
3789 range->op_first = left;
3790 range->op_flags = OPf_KIDS;
3791 leftstart = LINKLIST(left);
3792 range->op_other = LINKLIST(right);
3793 range->op_private = (U8)(1 | (flags >> 8));
3795 left->op_sibling = right;
3797 range->op_next = (OP*)range;
3798 flip = newUNOP(OP_FLIP, flags, (OP*)range);
3799 flop = newUNOP(OP_FLOP, 0, flip);
3800 o = newUNOP(OP_NULL, 0, flop);
3802 range->op_next = leftstart;
3804 left->op_next = flip;
3805 right->op_next = flop;
3807 range->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3808 sv_upgrade(PAD_SV(range->op_targ), SVt_PVNV);
3809 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
3810 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
3812 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3813 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
3816 if (!flip->op_private || !flop->op_private)
3817 linklist(o); /* blow off optimizer unless constant */
3823 Perl_newLOOPOP(pTHX_ I32 flags, I32 debuggable, OP *expr, OP *block)
3827 const bool once = block && block->op_flags & OPf_SPECIAL &&
3828 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
3830 PERL_UNUSED_ARG(debuggable);
3833 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
3834 return block; /* do {} while 0 does once */
3835 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3836 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3837 expr = newUNOP(OP_DEFINED, 0,
3838 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3839 } else if (expr->op_flags & OPf_KIDS) {
3840 const OP * const k1 = ((UNOP*)expr)->op_first;
3841 const OP * const k2 = k1 ? k1->op_sibling : NULL;
3842 switch (expr->op_type) {
3844 if (k2 && k2->op_type == OP_READLINE
3845 && (k2->op_flags & OPf_STACKED)
3846 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3847 expr = newUNOP(OP_DEFINED, 0, expr);
3851 if (k1 && (k1->op_type == OP_READDIR
3852 || k1->op_type == OP_GLOB
3853 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3854 || k1->op_type == OP_EACH))
3855 expr = newUNOP(OP_DEFINED, 0, expr);
3861 /* if block is null, the next append_elem() would put UNSTACK, a scalar
3862 * op, in listop. This is wrong. [perl #27024] */
3864 block = newOP(OP_NULL, 0);
3865 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
3866 o = new_logop(OP_AND, 0, &expr, &listop);
3869 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
3871 if (once && o != listop)
3872 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
3875 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
3877 o->op_flags |= flags;
3879 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
3885 Perl_newWHILEOP(pTHX_ I32 flags, I32 debuggable, LOOP *loop,
3886 I32 whileline, OP *expr, OP *block, OP *cont)
3888 return newWHILEOP8(flags, debuggable, loop, whileline, expr, block, cont,
3893 Perl_newWHILEOP8(pTHX_ I32 flags, I32 debuggable, LOOP *loop, I32
3894 whileline, OP *expr, OP *block, OP *cont, I32 has_my)
3902 PERL_UNUSED_ARG(debuggable);
3905 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
3906 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
3907 expr = newUNOP(OP_DEFINED, 0,
3908 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
3909 } else if (expr->op_flags & OPf_KIDS) {
3910 const OP * const k1 = ((UNOP*)expr)->op_first;
3911 const OP * const k2 = (k1) ? k1->op_sibling : NULL;
3912 switch (expr->op_type) {
3914 if (k2 && k2->op_type == OP_READLINE
3915 && (k2->op_flags & OPf_STACKED)
3916 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
3917 expr = newUNOP(OP_DEFINED, 0, expr);
3921 if (k1 && (k1->op_type == OP_READDIR
3922 || k1->op_type == OP_GLOB
3923 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
3924 || k1->op_type == OP_EACH))
3925 expr = newUNOP(OP_DEFINED, 0, expr);
3932 block = newOP(OP_NULL, 0);
3933 else if (cont || has_my) {
3934 block = scope(block);
3938 next = LINKLIST(cont);
3941 OP * const unstack = newOP(OP_UNSTACK, 0);
3944 cont = append_elem(OP_LINESEQ, cont, unstack);
3948 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
3950 redo = LINKLIST(listop);
3953 PL_copline = (line_t)whileline;
3955 o = new_logop(OP_AND, 0, &expr, &listop);
3956 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
3957 op_free(expr); /* oops, it's a while (0) */
3959 return NULL; /* listop already freed by new_logop */
3962 ((LISTOP*)listop)->op_last->op_next =
3963 (o == listop ? redo : LINKLIST(o));
3969 NewOp(1101,loop,1,LOOP);
3970 loop->op_type = OP_ENTERLOOP;
3971 loop->op_ppaddr = PL_ppaddr[OP_ENTERLOOP];
3972 loop->op_private = 0;
3973 loop->op_next = (OP*)loop;
3976 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
3978 loop->op_redoop = redo;
3979 loop->op_lastop = o;
3980 o->op_private |= loopflags;
3983 loop->op_nextop = next;
3985 loop->op_nextop = o;
3987 o->op_flags |= flags;
3988 o->op_private |= (flags >> 8);
3993 Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP *block, OP *cont)
3997 PADOFFSET padoff = 0;
4002 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
4003 iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
4004 sv->op_type = OP_RV2GV;
4005 sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
4007 else if (sv->op_type == OP_PADSV) { /* private variable */
4008 iterpflags = sv->op_private & OPpLVAL_INTRO; /* for my $x () */
4009 padoff = sv->op_targ;
4014 #ifdef USE_5005THREADS
4015 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
4016 padoff = sv->op_targ;
4018 iterflags |= OPf_SPECIAL;
4024 Perl_croak(aTHX_ "Can't use %s for loop variable", PL_op_desc[sv->op_type]);
4027 #ifdef USE_5005THREADS
4028 padoff = find_threadsv("_");
4029 iterflags |= OPf_SPECIAL;
4031 sv = newGVOP(OP_GV, 0, PL_defgv);
4034 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
4035 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
4036 iterflags |= OPf_STACKED;
4038 else if (expr->op_type == OP_NULL &&
4039 (expr->op_flags & OPf_KIDS) &&
4040 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
4042 /* Basically turn for($x..$y) into the same as for($x,$y), but we
4043 * set the STACKED flag to indicate that these values are to be
4044 * treated as min/max values by 'pp_iterinit'.
4046 const UNOP* const flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
4047 LOGOP* const range = (LOGOP*) flip->op_first;
4048 OP* const left = range->op_first;
4049 OP* const right = left->op_sibling;
4052 range->op_flags &= ~OPf_KIDS;
4053 range->op_first = NULL;
4055 listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
4056 listop->op_first->op_next = range->op_next;
4057 left->op_next = range->op_other;
4058 right->op_next = (OP*)listop;
4059 listop->op_next = listop->op_first;
4062 expr = (OP*)(listop);
4064 iterflags |= OPf_STACKED;
4067 expr = mod(force_list(expr), OP_GREPSTART);
4070 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
4071 append_elem(OP_LIST, expr, scalar(sv))));
4072 assert(!loop->op_next);
4073 /* for my $x () sets OPpLVAL_INTRO;
4074 * for our $x () sets OPpOUR_INTRO; both only used by Deparse.pm */
4075 loop->op_private = (U8)iterpflags;
4076 #ifdef PL_OP_SLAB_ALLOC
4079 NewOp(1234,tmp,1,LOOP);
4080 Copy(loop,tmp,1,LISTOP);
4085 Renew(loop, 1, LOOP);
4087 loop->op_targ = padoff;
4088 wop = newWHILEOP8(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont,
4090 PL_copline = forline;
4091 return newSTATEOP(0, label, wop);
4095 Perl_newLOOPEX(pTHX_ I32 type, OP *label)
4099 if (type != OP_GOTO || label->op_type == OP_CONST) {
4100 /* "last()" means "last" */
4101 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
4102 o = newOP(type, OPf_SPECIAL);
4104 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
4105 ? SvPVx_nolen_const(((SVOP*)label)->op_sv)
4111 /* Check whether it's going to be a goto &function */
4112 if (label->op_type == OP_ENTERSUB
4113 && !(label->op_flags & OPf_STACKED))
4114 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
4115 o = newUNOP(type, OPf_STACKED, label);
4117 PL_hints |= HINT_BLOCK_SCOPE;
4122 =for apidoc cv_undef
4124 Clear out all the active components of a CV. This can happen either
4125 by an explicit C<undef &foo>, or by the reference count going to zero.
4126 In the former case, we keep the CvOUTSIDE pointer, so that any anonymous
4127 children can still follow the full lexical scope chain.
4133 Perl_cv_undef(pTHX_ CV *cv)
4135 #ifdef USE_5005THREADS
4137 MUTEX_DESTROY(CvMUTEXP(cv));
4138 Safefree(CvMUTEXP(cv));
4141 #endif /* USE_5005THREADS */
4144 if (CvFILE(cv) && !CvISXSUB(cv)) {
4145 /* for XSUBs CvFILE point directly to static memory; __FILE__ */
4146 Safefree(CvFILE(cv));
4151 if (!CvISXSUB(cv) && CvROOT(cv)) {
4152 #ifdef USE_5005THREADS
4153 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
4154 Perl_croak(aTHX_ "Can't undef active subroutine");
4157 Perl_croak(aTHX_ "Can't undef active subroutine");
4158 #endif /* USE_5005THREADS */
4161 PAD_SAVE_SETNULLPAD();
4163 op_free(CvROOT(cv));
4168 SvPOK_off((SV*)cv); /* forget prototype */
4173 /* remove CvOUTSIDE unless this is an undef rather than a free */
4174 if (!SvREFCNT(cv) && CvOUTSIDE(cv)) {
4175 if (!CvWEAKOUTSIDE(cv))
4176 SvREFCNT_dec(CvOUTSIDE(cv));
4177 CvOUTSIDE(cv) = Nullcv;
4180 SvREFCNT_dec((SV*)CvXSUBANY(cv).any_ptr);
4186 /* delete all flags except WEAKOUTSIDE */
4187 CvFLAGS(cv) &= CVf_WEAKOUTSIDE;
4191 Perl_cv_ckproto_len(pTHX_ const CV *cv, const GV *gv, const char *p,
4194 /* Can't just use a strcmp on the prototype, as CONSTSUBs "cheat" by
4195 relying on SvCUR, and doubling up the buffer to hold CvFILE(). */
4196 if (((!p != !SvPOK(cv)) /* One has prototype, one has not. */
4197 || (p && (len != SvCUR(cv) /* Not the same length. */
4198 || memNE(p, SvPVX_const(cv), len))))
4199 && ckWARN_d(WARN_PROTOTYPE)) {
4200 SV* const msg = sv_newmortal();
4204 gv_efullname3(name = sv_newmortal(), (GV *)gv, NULL);
4205 sv_setpv(msg, "Prototype mismatch:");
4207 Perl_sv_catpvf(aTHX_ msg, " sub %"SVf, (void*)name);
4209 Perl_sv_catpvf(aTHX_ msg, " (%"SVf")", (void*)cv);
4211 sv_catpvs(msg, ": none");
4212 sv_catpvs(msg, " vs ");
4214 Perl_sv_catpvf(aTHX_ msg, "(%.*s)", (int) len, p);
4216 sv_catpvs(msg, "none");
4217 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "%"SVf, (void*)msg);
4221 static void const_sv_xsub(pTHX_ CV* cv);
4225 =head1 Optree Manipulation Functions
4227 =for apidoc cv_const_sv
4229 If C<cv> is a constant sub eligible for inlining. returns the constant
4230 value returned by the sub. Otherwise, returns NULL.
4232 Constant subs can be created with C<newCONSTSUB> or as described in
4233 L<perlsub/"Constant Functions">.
4238 Perl_cv_const_sv(pTHX_ CV *cv)
4240 PERL_UNUSED_CONTEXT;
4243 if (!(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM))
4245 return CvCONST(cv) ? (SV*)CvXSUBANY(cv).any_ptr : NULL;
4249 Perl_op_const_sv(pTHX_ OP *o, CV *cv)
4256 if (o->op_type == OP_LINESEQ && cLISTOPo->op_first)
4257 o = cLISTOPo->op_first->op_sibling;
4259 for (; o; o = o->op_next) {
4260 const OPCODE type = o->op_type;
4262 if (sv && o->op_next == o)
4264 if (o->op_next != o) {
4265 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
4267 if (type == OP_DBSTATE)
4270 if (type == OP_LEAVESUB || type == OP_RETURN)
4274 if (type == OP_CONST && cSVOPo->op_sv)
4276 else if ((type == OP_PADSV || type == OP_CONST) && cv) {
4277 sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ);
4281 /* We get here only from cv_clone2() while creating a closure.
4282 Copy the const value here instead of in cv_clone2 so that
4283 SvREADONLY_on doesn't lead to problems when leaving
4288 if (!SvREADONLY(sv) && SvREFCNT(sv) > 1)
4300 Perl_newMYSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4302 PERL_UNUSED_ARG(floor);
4312 Perl_croak(aTHX_ "\"my sub\" not yet implemented");
4316 Perl_newSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *block)
4318 return Perl_newATTRSUB(aTHX_ floor, o, proto, NULL, block);
4322 Perl_newATTRSUB(pTHX_ I32 floor, OP *o, OP *proto, OP *attrs, OP *block)
4328 register CV *cv = NULL;
4330 /* If the subroutine has no body, no attributes, and no builtin attributes
4331 then it's just a sub declaration, and we may be able to get away with
4332 storing with a placeholder scalar in the symbol table, rather than a
4333 full GV and CV. If anything is present then it will take a full CV to
4335 const I32 gv_fetch_flags
4336 = (block || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4337 ? GV_ADDMULTI : GV_ADDMULTI | GV_NOINIT;
4338 const char * const name = o ? SvPVx_nolen_const(cSVOPo->op_sv) : NULL;
4341 assert(proto->op_type == OP_CONST);
4342 ps = SvPVx_const(((SVOP*)proto)->op_sv, ps_len);
4347 if (!name && PERLDB_NAMEANON && CopLINE(PL_curcop)) {
4348 SV * const sv = sv_newmortal();
4349 Perl_sv_setpvf(aTHX_ sv, "%s[%s:%"IVdf"]",
4350 PL_curstash ? "__ANON__" : "__ANON__::__ANON__",
4351 CopFILE(PL_curcop), (IV)CopLINE(PL_curcop));
4352 aname = SvPVX_const(sv);
4357 /* There may be future conflict here as change 23766 is not yet merged. */
4358 gv = name ? gv_fetchsv(cSVOPo->op_sv, gv_fetch_flags, SVt_PVCV)
4359 : gv_fetchpv(aname ? aname
4360 : (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4361 gv_fetch_flags, SVt_PVCV);
4370 if (SvTYPE(gv) != SVt_PVGV) { /* Maybe prototype now, and had at
4371 maximum a prototype before. */
4372 if (SvTYPE(gv) > SVt_NULL) {
4373 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1)
4374 && ckWARN_d(WARN_PROTOTYPE))
4376 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE), "Runaway prototype");
4378 cv_ckproto_len((CV*)gv, NULL, ps, ps_len);
4381 sv_setpvn((SV*)gv, ps, ps_len);
4383 sv_setiv((SV*)gv, -1);
4384 SvREFCNT_dec(PL_compcv);
4385 cv = PL_compcv = NULL;
4386 PL_sub_generation++;
4390 cv = (!name || GvCVGEN(gv)) ? Nullcv : GvCV(gv);
4392 #ifdef GV_UNIQUE_CHECK
4393 if (cv && GvUNIQUE(gv) && SvREADONLY(cv)) {
4394 Perl_croak(aTHX_ "Can't define subroutine %s (GV is unique)", name);
4398 if (!block || !ps || *ps || attrs || (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS))
4401 const_sv = op_const_sv(block, Nullcv);
4404 const bool exists = CvROOT(cv) || CvXSUB(cv);
4406 #ifdef GV_UNIQUE_CHECK
4407 if (exists && GvUNIQUE(gv)) {
4408 Perl_croak(aTHX_ "Can't redefine unique subroutine %s", name);
4412 /* if the subroutine doesn't exist and wasn't pre-declared
4413 * with a prototype, assume it will be AUTOLOADed,
4414 * skipping the prototype check
4416 if (exists || SvPOK(cv))
4417 cv_ckproto_len(cv, gv, ps, ps_len);
4418 /* already defined (or promised)? */
4419 if (exists || GvASSUMECV(gv)) {
4420 if (!block && !attrs) {
4421 if (CvFLAGS(PL_compcv)) {
4422 /* might have had built-in attrs applied */
4423 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4425 /* just a "sub foo;" when &foo is already defined */
4426 SAVEFREESV(PL_compcv);
4430 if (ckWARN(WARN_REDEFINE)
4432 && (!const_sv || sv_cmp(cv_const_sv(cv), const_sv))))
4434 const line_t oldline = CopLINE(PL_curcop);
4435 if (PL_copline != NOLINE)
4436 CopLINE_set(PL_curcop, PL_copline);
4437 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4438 CvCONST(cv) ? "Constant subroutine %s redefined"
4439 : "Subroutine %s redefined", name);
4440 CopLINE_set(PL_curcop, oldline);
4448 SvREFCNT_inc_simple_void_NN(const_sv);
4450 assert(!CvROOT(cv) && !CvCONST(cv));
4451 sv_setpvn((SV*)cv, "", 0); /* prototype is "" */
4452 CvXSUBANY(cv).any_ptr = const_sv;
4453 CvXSUB(cv) = const_sv_xsub;
4458 cv = newCONSTSUB(NULL, (char *)name, const_sv);
4461 SvREFCNT_dec(PL_compcv);
4463 PL_sub_generation++;
4470 /* Need to do a C<use attributes $stash_of_cv,\&cv,@attrs>
4471 * before we clobber PL_compcv.
4475 /* Might have had built-in attributes applied -- propagate them. */
4476 CvFLAGS(cv) |= (CvFLAGS(PL_compcv) & CVf_BUILTIN_ATTRS);
4477 if (CvGV(cv) && GvSTASH(CvGV(cv)))
4478 stash = GvSTASH(CvGV(cv));
4479 else if (CvSTASH(cv))
4480 stash = CvSTASH(cv);
4482 stash = PL_curstash;
4485 /* possibly about to re-define existing subr -- ignore old cv */
4486 rcv = (SV*)PL_compcv;
4487 if (name && GvSTASH(gv))
4488 stash = GvSTASH(gv);
4490 stash = PL_curstash;
4492 apply_attrs(stash, rcv, attrs, FALSE);
4494 if (cv) { /* must reuse cv if autoloaded */
4496 /* got here with just attrs -- work done, so bug out */
4497 SAVEFREESV(PL_compcv);
4500 /* transfer PL_compcv to cv */
4502 CvFLAGS(cv) = CvFLAGS(PL_compcv);
4503 if (!CvWEAKOUTSIDE(cv))
4504 SvREFCNT_dec(CvOUTSIDE(cv));
4505 CvOUTSIDE(cv) = CvOUTSIDE(PL_compcv);
4506 CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(PL_compcv);
4507 CvOUTSIDE(PL_compcv) = 0;
4508 CvPADLIST(cv) = CvPADLIST(PL_compcv);
4509 CvPADLIST(PL_compcv) = 0;
4510 /* inner references to PL_compcv must be fixed up ... */
4511 pad_fixup_inner_anons(CvPADLIST(cv), PL_compcv, cv);
4512 /* ... before we throw it away */
4513 SvREFCNT_dec(PL_compcv);
4514 if (PERLDB_INTER)/* Advice debugger on the new sub. */
4515 ++PL_sub_generation;
4522 PL_sub_generation++;
4526 CvFILE_set_from_cop(cv, PL_curcop);
4527 CvSTASH(cv) = PL_curstash;
4528 #ifdef USE_5005THREADS
4530 if (!CvMUTEXP(cv)) {
4531 New(666, CvMUTEXP(cv), 1, perl_mutex);
4532 MUTEX_INIT(CvMUTEXP(cv));
4534 #endif /* USE_5005THREADS */
4537 sv_setpvn((SV*)cv, ps, ps_len);
4539 if (PL_error_count) {
4543 const char *s = strrchr(name, ':');
4545 if (strEQ(s, "BEGIN")) {
4546 const char not_safe[] =
4547 "BEGIN not safe after errors--compilation aborted";
4548 if (PL_in_eval & EVAL_KEEPERR)
4549 Perl_croak(aTHX_ not_safe);
4551 /* force display of errors found but not reported */
4552 sv_catpv(ERRSV, not_safe);
4553 Perl_croak(aTHX_ "%"SVf, (void*)ERRSV);
4562 CvROOT(cv) = newUNOP(OP_LEAVESUBLV, 0,
4563 mod(scalarseq(block), OP_LEAVESUBLV));
4566 /* This makes sub {}; work as expected. */
4567 if (block->op_type == OP_STUB) {
4569 block = newSTATEOP(0, NULL, 0);
4571 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
4573 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4574 OpREFCNT_set(CvROOT(cv), 1);
4575 CvSTART(cv) = LINKLIST(CvROOT(cv));
4576 CvROOT(cv)->op_next = 0;
4577 CALL_PEEP(CvSTART(cv));
4579 /* now that optimizer has done its work, adjust pad values */
4581 pad_tidy(CvCLONE(cv) ? padtidy_SUBCLONE : padtidy_SUB);
4584 assert(!CvCONST(cv));
4585 if (ps && !*ps && op_const_sv(block, cv))
4589 if (name || aname) {
4591 const char * const tname = (name ? name : aname);
4593 if (PERLDB_SUBLINE && PL_curstash != PL_debstash) {
4594 SV * const sv = newSV(0);
4595 SV * const tmpstr = sv_newmortal();
4596 GV * const db_postponed = gv_fetchpvs("DB::postponed",
4597 GV_ADDMULTI, SVt_PVHV);
4600 Perl_sv_setpvf(aTHX_ sv, "%s:%ld-%ld",
4602 (long)PL_subline, (long)CopLINE(PL_curcop));
4603 gv_efullname3(tmpstr, gv, NULL);
4604 hv_store(GvHV(PL_DBsub), SvPVX_const(tmpstr), SvCUR(tmpstr), sv, 0);
4605 hv = GvHVn(db_postponed);
4606 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX_const(tmpstr), SvCUR(tmpstr))) {
4607 CV * const pcv = GvCV(db_postponed);
4613 call_sv((SV*)pcv, G_DISCARD);
4618 if ((s = strrchr(tname,':')))
4623 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4626 if (strEQ(s, "BEGIN")) {
4627 const I32 oldscope = PL_scopestack_ix;
4629 SAVECOPFILE(&PL_compiling);
4630 SAVECOPLINE(&PL_compiling);
4633 PL_beginav = newAV();
4634 DEBUG_x( dump_sub(gv) );
4635 av_push(PL_beginav, (SV*)cv);
4636 GvCV(gv) = 0; /* cv has been hijacked */
4637 call_list(oldscope, PL_beginav);
4639 PL_curcop = &PL_compiling;
4640 CopHINTS_set(&PL_compiling, PL_hints);
4643 else if (strEQ(s, "END") && !PL_error_count) {
4646 DEBUG_x( dump_sub(gv) );
4647 av_unshift(PL_endav, 1);
4648 av_store(PL_endav, 0, (SV*)cv);
4649 GvCV(gv) = 0; /* cv has been hijacked */
4651 else if (strEQ(s, "CHECK") && !PL_error_count) {
4653 PL_checkav = newAV();
4654 DEBUG_x( dump_sub(gv) );
4655 if (PL_main_start && ckWARN(WARN_VOID))
4656 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4657 av_unshift(PL_checkav, 1);
4658 av_store(PL_checkav, 0, (SV*)cv);
4659 GvCV(gv) = 0; /* cv has been hijacked */
4661 else if (strEQ(s, "INIT") && !PL_error_count) {
4663 PL_initav = newAV();
4664 DEBUG_x( dump_sub(gv) );
4665 if (PL_main_start && ckWARN(WARN_VOID))
4666 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4667 av_push(PL_initav, (SV*)cv);
4668 GvCV(gv) = 0; /* cv has been hijacked */
4673 PL_copline = NOLINE;
4678 /* XXX unsafe for 5005 threads if eval_owner isn't held */
4680 =for apidoc newCONSTSUB
4682 Creates a constant sub equivalent to Perl C<sub FOO () { 123 }> which is
4683 eligible for inlining at compile-time.
4689 Perl_newCONSTSUB(pTHX_ HV *stash, char *name, SV *sv)
4693 const char *const temp_p = CopFILE(PL_curcop);
4694 const STRLEN len = temp_p ? strlen(temp_p) : 0;
4696 SV *const temp_sv = CopFILESV(PL_curcop);
4698 const char *const temp_p = temp_sv ? SvPV_const(temp_sv, len) : NULL;
4700 char *const file = savepvn(temp_p, temp_p ? len : 0);
4704 SAVECOPLINE(PL_curcop);
4705 CopLINE_set(PL_curcop, PL_copline);
4708 PL_hints &= ~HINT_BLOCK_SCOPE;
4711 SAVESPTR(PL_curstash);
4712 SAVECOPSTASH(PL_curcop);
4713 PL_curstash = stash;
4714 CopSTASH_set(PL_curcop,stash);
4717 /* file becomes the CvFILE. For an XS, it's supposed to be static storage,
4718 and so doesn't get free()d. (It's expected to be from the C pre-
4719 processor __FILE__ directive). But we need a dynamically allocated one,
4720 and we need it to get freed. */
4721 cv = newXS_flags(name, const_sv_xsub, file, "", XS_DYNAMIC_FILENAME);
4722 CvXSUBANY(cv).any_ptr = sv;
4728 CopSTASH_free(PL_curcop);
4736 Perl_newXS_flags(pTHX_ const char *name, XSUBADDR_t subaddr,
4737 const char *const filename, const char *const proto,
4740 CV *cv = newXS((char*)name, subaddr, (char*)filename);
4742 if (flags & XS_DYNAMIC_FILENAME) {
4743 /* We need to "make arrangements" (ie cheat) to ensure that the
4744 filename lasts as long as the PVCV we just created, but also doesn't
4746 STRLEN filename_len = strlen(filename);
4747 STRLEN proto_and_file_len = filename_len;
4748 char *proto_and_file;
4752 proto_len = strlen(proto);
4753 proto_and_file_len += proto_len;
4755 Newx(proto_and_file, proto_and_file_len + 1, char);
4756 Copy(proto, proto_and_file, proto_len, char);
4757 Copy(filename, proto_and_file + proto_len, filename_len + 1, char);
4760 proto_and_file = savepvn(filename, filename_len);
4763 /* This gets free()d. :-) */
4764 sv_usepvn_flags((SV*)cv, proto_and_file, proto_and_file_len,
4765 SV_HAS_TRAILING_NUL);
4767 /* This gives us the correct prototype, rather than one with the
4768 file name appended. */
4769 SvCUR_set(cv, proto_len);
4773 CvFILE(cv) = proto_and_file + proto_len;
4775 sv_setpv((SV *)cv, proto);
4781 =for apidoc U||newXS
4783 Used by C<xsubpp> to hook up XSUBs as Perl subs. I<filename> needs to be
4784 static storage, as it is used directly as CvFILE(), without a copy being made.
4790 Perl_newXS(pTHX_ char *name, XSUBADDR_t subaddr, char *filename)
4792 GV * const gv = gv_fetchpv(name ? name :
4793 (PL_curstash ? "__ANON__" : "__ANON__::__ANON__"),
4794 GV_ADDMULTI, SVt_PVCV);
4797 if ((cv = (name ? GvCV(gv) : Nullcv))) {
4799 /* just a cached method */
4803 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
4804 /* already defined (or promised) */
4805 /* XXX It's possible for this HvNAME_get to return null, and get passed into strEQ */
4806 if (ckWARN(WARN_REDEFINE)) {
4807 GV * const gvcv = CvGV(cv);
4809 HV * const stash = GvSTASH(gvcv);
4811 const char *redefined_name = HvNAME_get(stash);
4812 if ( strEQ(redefined_name,"autouse") ) {
4813 const line_t oldline = CopLINE(PL_curcop);
4814 if (PL_copline != NOLINE)
4815 CopLINE_set(PL_curcop, PL_copline);
4816 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4817 CvCONST(cv) ? "Constant subroutine %s redefined"
4818 : "Subroutine %s redefined"
4820 CopLINE_set(PL_curcop, oldline);
4830 if (cv) /* must reuse cv if autoloaded */
4834 sv_upgrade((SV *)cv, SVt_PVCV);
4838 PL_sub_generation++;
4842 #ifdef USE_5005THREADS
4843 New(666, CvMUTEXP(cv), 1, perl_mutex);
4844 MUTEX_INIT(CvMUTEXP(cv));
4846 #endif /* USE_5005THREADS */
4847 (void)gv_fetchfile(filename);
4848 CvFILE(cv) = (char *)filename; /* NOTE: not copied, as it is expected to be
4849 an external constant string */
4850 CvXSUB(cv) = subaddr;
4853 const char *s = strrchr(name,':');
4859 if (*s != 'B' && *s != 'E' && *s != 'C' && *s != 'I')
4862 if (strEQ(s, "BEGIN")) {
4864 PL_beginav = newAV();
4865 av_push(PL_beginav, (SV*)cv);
4866 GvCV(gv) = 0; /* cv has been hijacked */
4868 else if (strEQ(s, "END")) {
4871 av_unshift(PL_endav, 1);
4872 av_store(PL_endav, 0, (SV*)cv);
4873 GvCV(gv) = 0; /* cv has been hijacked */
4875 else if (strEQ(s, "CHECK")) {
4877 PL_checkav = newAV();
4878 if (PL_main_start && ckWARN(WARN_VOID))
4879 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run CHECK block");
4880 av_unshift(PL_checkav, 1);
4881 av_store(PL_checkav, 0, (SV*)cv);
4882 GvCV(gv) = 0; /* cv has been hijacked */
4884 else if (strEQ(s, "INIT")) {
4886 PL_initav = newAV();
4887 if (PL_main_start && ckWARN(WARN_VOID))
4888 Perl_warner(aTHX_ packWARN(WARN_VOID), "Too late to run INIT block");
4889 av_push(PL_initav, (SV*)cv);
4890 GvCV(gv) = 0; /* cv has been hijacked */
4901 Perl_newFORM(pTHX_ I32 floor, OP *o, OP *block)
4906 ? gv_fetchsv(cSVOPo->op_sv, TRUE, SVt_PVFM)
4907 : gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVFM);
4909 #ifdef GV_UNIQUE_CHECK
4911 Perl_croak(aTHX_ "Bad symbol for form (GV is unique)");
4915 if ((cv = GvFORM(gv))) {
4916 if (ckWARN(WARN_REDEFINE)) {
4917 const line_t oldline = CopLINE(PL_curcop);
4918 if (PL_copline != NOLINE)
4919 CopLINE_set(PL_curcop, PL_copline);
4920 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
4921 o ? "Format %"SVf" redefined"
4922 : "Format STDOUT redefined", (void*)cSVOPo->op_sv);
4923 CopLINE_set(PL_curcop, oldline);
4930 CvFILE_set_from_cop(cv, PL_curcop);
4933 pad_tidy(padtidy_FORMAT);
4934 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
4935 CvROOT(cv)->op_private |= OPpREFCOUNTED;
4936 OpREFCNT_set(CvROOT(cv), 1);
4937 CvSTART(cv) = LINKLIST(CvROOT(cv));
4938 CvROOT(cv)->op_next = 0;
4939 CALL_PEEP(CvSTART(cv));
4941 PL_copline = NOLINE;
4946 Perl_newANONLIST(pTHX_ OP *o)
4948 return convert(OP_ANONLIST, OPf_SPECIAL, o);
4952 Perl_newANONHASH(pTHX_ OP *o)
4954 return convert(OP_ANONHASH, OPf_SPECIAL, o);
4958 Perl_newANONSUB(pTHX_ I32 floor, OP *proto, OP *block)
4960 return newANONATTRSUB(floor, proto, NULL, block);
4964 Perl_newANONATTRSUB(pTHX_ I32 floor, OP *proto, OP *attrs, OP *block)
4966 return newUNOP(OP_REFGEN, 0,
4967 newSVOP(OP_ANONCODE, 0,
4968 (SV*)newATTRSUB(floor, 0, proto, attrs, block)));
4972 Perl_oopsAV(pTHX_ OP *o)
4974 switch (o->op_type) {
4976 o->op_type = OP_PADAV;
4977 o->op_ppaddr = PL_ppaddr[OP_PADAV];
4978 return ref(o, OP_RV2AV);
4981 o->op_type = OP_RV2AV;
4982 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
4987 if (ckWARN_d(WARN_INTERNAL))
4988 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsAV");
4995 Perl_oopsHV(pTHX_ OP *o)
4997 switch (o->op_type) {
5000 o->op_type = OP_PADHV;
5001 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5002 return ref(o, OP_RV2HV);
5006 o->op_type = OP_RV2HV;
5007 o->op_ppaddr = PL_ppaddr[OP_RV2HV];
5012 if (ckWARN_d(WARN_INTERNAL))
5013 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "oops: oopsHV");
5020 Perl_newAVREF(pTHX_ OP *o)
5022 if (o->op_type == OP_PADANY) {
5023 o->op_type = OP_PADAV;
5024 o->op_ppaddr = PL_ppaddr[OP_PADAV];
5027 else if ((o->op_type == OP_RV2AV || o->op_type == OP_PADAV)
5028 && ckWARN(WARN_DEPRECATED)) {
5029 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5030 "Using an array as a reference is deprecated");
5032 return newUNOP(OP_RV2AV, 0, scalar(o));
5036 Perl_newGVREF(pTHX_ I32 type, OP *o)
5038 if (type == OP_MAPSTART || type == OP_GREPSTART || type == OP_SORT)
5039 return newUNOP(OP_NULL, 0, o);
5040 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
5044 Perl_newHVREF(pTHX_ OP *o)
5046 if (o->op_type == OP_PADANY) {
5047 o->op_type = OP_PADHV;
5048 o->op_ppaddr = PL_ppaddr[OP_PADHV];
5051 else if ((o->op_type == OP_RV2HV || o->op_type == OP_PADHV)
5052 && ckWARN(WARN_DEPRECATED)) {
5053 Perl_warner(aTHX_ packWARN(WARN_DEPRECATED),
5054 "Using a hash as a reference is deprecated");
5056 return newUNOP(OP_RV2HV, 0, scalar(o));
5060 Perl_newCVREF(pTHX_ I32 flags, OP *o)
5062 return newUNOP(OP_RV2CV, flags, scalar(o));
5066 Perl_newSVREF(pTHX_ OP *o)
5068 if (o->op_type == OP_PADANY) {
5069 o->op_type = OP_PADSV;
5070 o->op_ppaddr = PL_ppaddr[OP_PADSV];
5073 #ifdef USE_5005THREADS
5074 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
5075 o->op_flags |= OPpDONE_SVREF;
5079 return newUNOP(OP_RV2SV, 0, scalar(o));
5082 /* Check routines. See the comments at the top of this file for details
5083 * on when these are called */
5086 Perl_ck_anoncode(pTHX_ OP *o)
5088 cSVOPo->op_targ = pad_add_anon(cSVOPo->op_sv, o->op_type);
5089 cSVOPo->op_sv = NULL;
5094 Perl_ck_bitop(pTHX_ OP *o)
5096 #define OP_IS_NUMCOMPARE(op) \
5097 ((op) == OP_LT || (op) == OP_I_LT || \
5098 (op) == OP_GT || (op) == OP_I_GT || \
5099 (op) == OP_LE || (op) == OP_I_LE || \
5100 (op) == OP_GE || (op) == OP_I_GE || \
5101 (op) == OP_EQ || (op) == OP_I_EQ || \
5102 (op) == OP_NE || (op) == OP_I_NE || \
5103 (op) == OP_NCMP || (op) == OP_I_NCMP)
5104 o->op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);
5105 if (!(o->op_flags & OPf_STACKED) /* Not an assignment */
5106 && (o->op_type == OP_BIT_OR
5107 || o->op_type == OP_BIT_AND
5108 || o->op_type == OP_BIT_XOR))
5110 const OP * const left = cBINOPo->op_first;
5111 const OP * const right = left->op_sibling;
5112 if ((OP_IS_NUMCOMPARE(left->op_type) &&
5113 (left->op_flags & OPf_PARENS) == 0) ||
5114 (OP_IS_NUMCOMPARE(right->op_type) &&
5115 (right->op_flags & OPf_PARENS) == 0))
5116 if (ckWARN(WARN_PRECEDENCE))
5117 Perl_warner(aTHX_ packWARN(WARN_PRECEDENCE),
5118 "Possible precedence problem on bitwise %c operator",
5119 o->op_type == OP_BIT_OR ? '|'
5120 : o->op_type == OP_BIT_AND ? '&' : '^'
5127 Perl_ck_concat(pTHX_ OP *o)
5129 const OP * const kid = cUNOPo->op_first;
5130 PERL_UNUSED_CONTEXT;
5131 if (kid->op_type == OP_CONCAT && !(kid->op_private & OPpTARGET_MY) &&
5132 !(kUNOP->op_first->op_flags & OPf_MOD))
5133 o->op_flags |= OPf_STACKED;
5138 Perl_ck_spair(pTHX_ OP *o)
5140 if (o->op_flags & OPf_KIDS) {
5143 const OPCODE type = o->op_type;
5144 o = modkids(ck_fun(o), type);
5145 kid = cUNOPo->op_first;
5146 newop = kUNOP->op_first->op_sibling;
5148 const OPCODE type = newop->op_type;
5149 if (newop->op_sibling || !(PL_opargs[type] & OA_RETSCALAR) ||
5150 type == OP_PADAV || type == OP_PADHV ||
5151 type == OP_RV2AV || type == OP_RV2HV)
5154 op_free(kUNOP->op_first);
5155 kUNOP->op_first = newop;
5157 o->op_ppaddr = PL_ppaddr[++o->op_type];
5162 Perl_ck_delete(pTHX_ OP *o)
5166 if (o->op_flags & OPf_KIDS) {
5167 OP * const kid = cUNOPo->op_first;
5168 switch (kid->op_type) {
5170 o->op_flags |= OPf_SPECIAL;
5173 o->op_private |= OPpSLICE;
5176 o->op_flags |= OPf_SPECIAL;
5181 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element or slice",
5190 Perl_ck_die(pTHX_ OP *o)
5193 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5199 Perl_ck_eof(pTHX_ OP *o)
5201 const I32 type = o->op_type;
5203 if (o->op_flags & OPf_KIDS) {
5204 if (cLISTOPo->op_first->op_type == OP_STUB) {
5206 o = newUNOP(type, OPf_SPECIAL, newGVOP(OP_GV, 0, PL_argvgv));
5214 Perl_ck_eval(pTHX_ OP *o)
5216 PL_hints |= HINT_BLOCK_SCOPE;
5217 if (o->op_flags & OPf_KIDS) {
5218 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5221 o->op_flags &= ~OPf_KIDS;
5224 else if (kid->op_type == OP_LINESEQ || kid->op_type == OP_STUB) {
5227 cUNOPo->op_first = 0;
5230 NewOp(1101, enter, 1, LOGOP);
5231 enter->op_type = OP_ENTERTRY;
5232 enter->op_ppaddr = PL_ppaddr[OP_ENTERTRY];
5233 enter->op_private = 0;
5235 /* establish postfix order */
5236 enter->op_next = (OP*)enter;
5238 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
5239 o->op_type = OP_LEAVETRY;
5240 o->op_ppaddr = PL_ppaddr[OP_LEAVETRY];
5241 enter->op_other = o;
5249 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
5251 o->op_targ = (PADOFFSET)PL_hints;
5256 Perl_ck_exit(pTHX_ OP *o)
5259 HV * const table = GvHV(PL_hintgv);
5261 SV * const * const svp = hv_fetchs(table, "vmsish_exit", FALSE);
5262 if (svp && *svp && SvTRUE(*svp))
5263 o->op_private |= OPpEXIT_VMSISH;
5265 if (VMSISH_HUSHED) o->op_private |= OPpHUSH_VMSISH;
5271 Perl_ck_exec(pTHX_ OP *o)
5273 if (o->op_flags & OPf_STACKED) {
5276 kid = cUNOPo->op_first->op_sibling;
5277 if (kid->op_type == OP_RV2GV)
5286 Perl_ck_exists(pTHX_ OP *o)
5289 if (o->op_flags & OPf_KIDS) {
5290 OP * const kid = cUNOPo->op_first;
5291 if (kid->op_type == OP_ENTERSUB) {
5292 (void) ref(kid, o->op_type);
5293 if (kid->op_type != OP_RV2CV && !PL_error_count)
5294 Perl_croak(aTHX_ "%s argument is not a subroutine name",
5296 o->op_private |= OPpEXISTS_SUB;
5298 else if (kid->op_type == OP_AELEM)
5299 o->op_flags |= OPf_SPECIAL;
5300 else if (kid->op_type != OP_HELEM)
5301 Perl_croak(aTHX_ "%s argument is not a HASH or ARRAY element",
5309 Perl_ck_rvconst(pTHX_ register OP *o)
5311 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5313 o->op_private |= (PL_hints & HINT_STRICT_REFS);
5314 if (o->op_type == OP_RV2CV)
5315 o->op_private &= ~1;
5317 if (kid->op_type == OP_CONST) {
5320 SV * const kidsv = kid->op_sv;
5322 /* Is it a constant from cv_const_sv()? */
5323 if (SvROK(kidsv) && SvREADONLY(kidsv)) {
5324 SV * const rsv = SvRV(kidsv);
5325 const svtype type = SvTYPE(rsv);
5326 const char *badtype = NULL;
5328 switch (o->op_type) {
5330 if (type > SVt_PVMG)
5331 badtype = "a SCALAR";
5334 if (type != SVt_PVAV)
5335 badtype = "an ARRAY";
5338 if (type != SVt_PVHV) {
5339 if (type == SVt_PVAV) { /* pseudohash? */
5340 SV **ksv = av_fetch((AV*)rsv, 0, FALSE);
5341 if (ksv && SvROK(*ksv)
5342 && SvTYPE(SvRV(*ksv)) == SVt_PVHV)
5351 if (type != SVt_PVCV)
5356 Perl_croak(aTHX_ "Constant is not %s reference", badtype);
5359 if ((PL_hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
5360 const char *badthing;
5361 switch (o->op_type) {
5363 badthing = "a SCALAR";
5366 badthing = "an ARRAY";
5369 badthing = "a HASH";
5377 "Can't use bareword (\"%"SVf"\") as %s ref while \"strict refs\" in use",
5378 (void*)kidsv, badthing);
5381 * This is a little tricky. We only want to add the symbol if we
5382 * didn't add it in the lexer. Otherwise we get duplicate strict
5383 * warnings. But if we didn't add it in the lexer, we must at
5384 * least pretend like we wanted to add it even if it existed before,
5385 * or we get possible typo warnings. OPpCONST_ENTERED says
5386 * whether the lexer already added THIS instance of this symbol.
5388 iscv = (o->op_type == OP_RV2CV) * 2;
5390 gv = gv_fetchsv(kidsv,
5391 iscv | !(kid->op_private & OPpCONST_ENTERED),
5394 : o->op_type == OP_RV2SV
5396 : o->op_type == OP_RV2AV
5398 : o->op_type == OP_RV2HV
5401 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
5403 kid->op_type = OP_GV;
5404 SvREFCNT_dec(kid->op_sv);
5406 /* XXX hack: dependence on sizeof(PADOP) <= sizeof(SVOP) */
5407 kPADOP->op_padix = pad_alloc(OP_GV, SVs_PADTMP);
5408 SvREFCNT_dec(PAD_SVl(kPADOP->op_padix));
5410 PAD_SETSV(kPADOP->op_padix, (SV*) SvREFCNT_inc_simple_NN(gv));
5412 kid->op_sv = SvREFCNT_inc_simple_NN(gv);
5414 kid->op_private = 0;
5415 kid->op_ppaddr = PL_ppaddr[OP_GV];
5422 Perl_ck_ftst(pTHX_ OP *o)
5424 const I32 type = o->op_type;
5426 if (o->op_flags & OPf_REF) {
5429 else if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
5430 SVOP * const kid = (SVOP*)cUNOPo->op_first;
5431 const OPCODE kidtype = kid->op_type;
5433 if (kidtype == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
5434 OP * const newop = newGVOP(type, OPf_REF,
5435 gv_fetchsv(kid->op_sv, GV_ADD, SVt_PVIO));
5440 if ((PL_hints & HINT_FILETEST_ACCESS) &&
5441 OP_IS_FILETEST_ACCESS(o))
5442 o->op_private |= OPpFT_ACCESS;
5447 if (type == OP_FTTTY)
5448 o = newGVOP(type, OPf_REF, PL_stdingv);
5450 o = newUNOP(type, 0, newDEFSVOP());
5456 Perl_ck_fun(pTHX_ OP *o)
5458 const int type = o->op_type;
5459 register I32 oa = PL_opargs[type] >> OASHIFT;
5461 if (o->op_flags & OPf_STACKED) {
5462 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
5465 return no_fh_allowed(o);
5468 if (o->op_flags & OPf_KIDS) {
5469 OP **tokid = &cLISTOPo->op_first;
5470 register OP *kid = cLISTOPo->op_first;
5474 if (kid->op_type == OP_PUSHMARK ||
5475 (kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK))
5477 tokid = &kid->op_sibling;
5478 kid = kid->op_sibling;
5480 if (!kid && PL_opargs[type] & OA_DEFGV)
5481 *tokid = kid = newDEFSVOP();
5485 sibl = kid->op_sibling;
5488 /* list seen where single (scalar) arg expected? */
5489 if (numargs == 1 && !(oa >> 4)
5490 && kid->op_type == OP_LIST && type != OP_SCALAR)
5492 return too_many_arguments(o,PL_op_desc[type]);
5505 if ((type == OP_PUSH || type == OP_UNSHIFT)
5506 && !kid->op_sibling && ckWARN(WARN_SYNTAX))
5507 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
5508 "Useless use of %s with no values",
5511 if (kid->op_type == OP_CONST &&
5512 (kid->op_private & OPpCONST_BARE))
5514 OP * const newop = newAVREF(newGVOP(OP_GV, 0,
5515 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVAV) ));
5516 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5517 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5518 "Array @%"SVf" missing the @ in argument %"IVdf" of %s()",
5519 (void*)((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
5522 kid->op_sibling = sibl;
5525 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
5526 bad_type(numargs, "array", PL_op_desc[type], kid);
5530 if (kid->op_type == OP_CONST &&
5531 (kid->op_private & OPpCONST_BARE))
5533 OP * const newop = newHVREF(newGVOP(OP_GV, 0,
5534 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVHV) ));
5535 if (ckWARN2(WARN_DEPRECATED, WARN_SYNTAX))
5536 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5537 "Hash %%%"SVf" missing the %% in argument %"IVdf" of %s()",
5538 (void*)((SVOP*)kid)->op_sv, (IV)numargs, PL_op_desc[type]);
5541 kid->op_sibling = sibl;
5544 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
5545 bad_type(numargs, "hash", PL_op_desc[type], kid);
5550 OP * const newop = newUNOP(OP_NULL, 0, kid);
5551 kid->op_sibling = 0;
5553 newop->op_next = newop;
5555 kid->op_sibling = sibl;
5560 if (kid->op_type != OP_GV && kid->op_type != OP_RV2GV) {
5561 if (kid->op_type == OP_CONST &&
5562 (kid->op_private & OPpCONST_BARE))
5564 OP * const newop = newGVOP(OP_GV, 0,
5565 gv_fetchsv(((SVOP*)kid)->op_sv, GV_ADD, SVt_PVIO));
5566 if (!(o->op_private & 1) && /* if not unop */
5567 kid == cLISTOPo->op_last)
5568 cLISTOPo->op_last = newop;
5572 else if (kid->op_type == OP_READLINE) {
5573 /* neophyte patrol: open(<FH>), close(<FH>) etc. */
5574 bad_type(numargs, "HANDLE", OP_DESC(o), kid);
5577 I32 flags = OPf_SPECIAL;
5581 /* is this op a FH constructor? */
5582 if (is_handle_constructor(o,numargs)) {
5583 const char *name = NULL;
5587 /* Set a flag to tell rv2gv to vivify
5588 * need to "prove" flag does not mean something
5589 * else already - NI-S 1999/05/07
5592 if (kid->op_type == OP_PADSV) {
5593 /*XXX DAPM 2002.08.25 tmp assert test */
5594 /*XXX*/ assert(av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5595 /*XXX*/ assert(*av_fetch(PL_comppad_name, (kid->op_targ), FALSE));
5597 name = PAD_COMPNAME_PV(kid->op_targ);
5598 /* SvCUR of a pad namesv can't be trusted
5599 * (see PL_generation), so calc its length
5605 else if (kid->op_type == OP_RV2SV
5606 && kUNOP->op_first->op_type == OP_GV)
5608 GV * const gv = cGVOPx_gv(kUNOP->op_first);
5610 len = GvNAMELEN(gv);
5612 else if (kid->op_type == OP_AELEM
5613 || kid->op_type == OP_HELEM)
5616 OP *op = ((BINOP*)kid)->op_first;
5620 const char * const a =
5621 kid->op_type == OP_AELEM ?
5623 if (((op->op_type == OP_RV2AV) ||
5624 (op->op_type == OP_RV2HV)) &&
5625 (firstop = ((UNOP*)op)->op_first) &&
5626 (firstop->op_type == OP_GV)) {
5627 /* packagevar $a[] or $h{} */
5628 GV * const gv = cGVOPx_gv(firstop);
5636 else if (op->op_type == OP_PADAV
5637 || op->op_type == OP_PADHV) {
5638 /* lexicalvar $a[] or $h{} */
5639 const char * const padname =
5640 PAD_COMPNAME_PV(op->op_targ);
5649 name = SvPV_const(tmpstr, len);
5654 name = "__ANONIO__";
5661 targ = pad_alloc(OP_RV2GV, SVs_PADTMP);
5662 namesv = PAD_SVl(targ);
5663 (void)SvUPGRADE(namesv, SVt_PV);
5665 sv_setpvn(namesv, "$", 1);
5666 sv_catpvn(namesv, name, len);
5669 kid->op_sibling = 0;
5670 kid = newUNOP(OP_RV2GV, flags, scalar(kid));
5671 kid->op_targ = targ;
5672 kid->op_private |= priv;
5674 kid->op_sibling = sibl;
5680 mod(scalar(kid), type);
5684 tokid = &kid->op_sibling;
5685 kid = kid->op_sibling;
5687 o->op_private |= numargs;
5689 return too_many_arguments(o,OP_DESC(o));
5692 else if (PL_opargs[type] & OA_DEFGV) {
5694 return newUNOP(type, 0, newDEFSVOP());
5698 while (oa & OA_OPTIONAL)
5700 if (oa && oa != OA_LIST)
5701 return too_few_arguments(o,OP_DESC(o));
5707 Perl_ck_glob(pTHX_ OP *o)
5712 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
5713 append_elem(OP_GLOB, o, newDEFSVOP());
5715 if (!((gv = gv_fetchpvs("glob", GV_NOTQUAL, SVt_PVCV))
5716 && GvCVu(gv) && GvIMPORTED_CV(gv)))
5718 gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
5721 #if !defined(PERL_EXTERNAL_GLOB)
5722 /* XXX this can be tightened up and made more failsafe. */
5723 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
5726 Perl_load_module(aTHX_ PERL_LOADMOD_NOIMPORT,
5727 newSVpvs("File::Glob"), NULL, NULL, NULL);
5728 gv = gv_fetchpvs("CORE::GLOBAL::glob", 0, SVt_PVCV);
5729 glob_gv = gv_fetchpvs("File::Glob::csh_glob", 0, SVt_PVCV);
5730 GvCV(gv) = GvCV(glob_gv);
5731 SvREFCNT_inc_void((SV*)GvCV(gv));
5732 GvIMPORTED_CV_on(gv);
5735 #endif /* PERL_EXTERNAL_GLOB */
5737 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
5738 append_elem(OP_GLOB, o,
5739 newSVOP(OP_CONST, 0, newSViv(PL_glob_index++)));
5740 o->op_type = OP_LIST;
5741 o->op_ppaddr = PL_ppaddr[OP_LIST];
5742 cLISTOPo->op_first->op_type = OP_PUSHMARK;
5743 cLISTOPo->op_first->op_ppaddr = PL_ppaddr[OP_PUSHMARK];
5744 cLISTOPo->op_first->op_targ = 0;
5745 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
5746 append_elem(OP_LIST, o,
5747 scalar(newUNOP(OP_RV2CV, 0,
5748 newGVOP(OP_GV, 0, gv)))));
5749 o = newUNOP(OP_NULL, 0, ck_subr(o));
5750 o->op_targ = OP_GLOB; /* hint at what it used to be */
5753 gv = newGVgen("main");
5755 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
5761 Perl_ck_grep(pTHX_ OP *o)
5765 const OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
5767 o->op_ppaddr = PL_ppaddr[OP_GREPSTART];
5768 /* don't allocate gwop here, as we may leak it if PL_error_count > 0 */
5770 if (o->op_flags & OPf_STACKED) {
5773 kid = cLISTOPo->op_first->op_sibling;
5774 if (!cUNOPx(kid)->op_next)
5775 Perl_croak(aTHX_ "panic: ck_grep");
5776 for (k = cUNOPx(kid)->op_first; k; k = k->op_next) {
5779 NewOp(1101, gwop, 1, LOGOP);
5780 kid->op_next = (OP*)gwop;
5781 o->op_flags &= ~OPf_STACKED;
5783 kid = cLISTOPo->op_first->op_sibling;
5784 if (type == OP_MAPWHILE)
5791 kid = cLISTOPo->op_first->op_sibling;
5792 if (kid->op_type != OP_NULL)
5793 Perl_croak(aTHX_ "panic: ck_grep");
5794 kid = kUNOP->op_first;
5797 NewOp(1101, gwop, 1, LOGOP);
5798 gwop->op_type = type;
5799 gwop->op_ppaddr = PL_ppaddr[type];
5800 gwop->op_first = listkids(o);
5801 gwop->op_flags |= OPf_KIDS;
5802 gwop->op_private = 1;
5803 gwop->op_other = LINKLIST(kid);
5804 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
5805 kid->op_next = (OP*)gwop;
5807 kid = cLISTOPo->op_first->op_sibling;
5808 if (!kid || !kid->op_sibling)
5809 return too_few_arguments(o,OP_DESC(o));
5810 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
5811 mod(kid, OP_GREPSTART);
5817 Perl_ck_index(pTHX_ OP *o)
5819 if (o->op_flags & OPf_KIDS) {
5820 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
5822 kid = kid->op_sibling; /* get past "big" */
5823 if (kid && kid->op_type == OP_CONST)
5824 fbm_compile(((SVOP*)kid)->op_sv, 0);
5830 Perl_ck_lengthconst(pTHX_ OP *o)
5832 /* XXX length optimization goes here */
5837 Perl_ck_lfun(pTHX_ OP *o)
5839 const OPCODE type = o->op_type;
5840 return modkids(ck_fun(o), type);
5844 Perl_ck_defined(pTHX_ OP *o) /* 19990527 MJD */
5846 if ((o->op_flags & OPf_KIDS) && ckWARN2(WARN_DEPRECATED, WARN_SYNTAX)) {
5847 switch (cUNOPo->op_first->op_type) {
5849 /* This is needed for
5850 if (defined %stash::)
5851 to work. Do not break Tk.
5853 break; /* Globals via GV can be undef */
5855 case OP_AASSIGN: /* Is this a good idea? */
5856 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5857 "defined(@array) is deprecated");
5858 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5859 "\t(Maybe you should just omit the defined()?)\n");
5862 /* This is needed for
5863 if (defined %stash::)
5864 to work. Do not break Tk.
5866 break; /* Globals via GV can be undef */
5868 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5869 "defined(%%hash) is deprecated");
5870 Perl_warner(aTHX_ packWARN2(WARN_DEPRECATED, WARN_SYNTAX),
5871 "\t(Maybe you should just omit the defined()?)\n");
5882 Perl_ck_rfun(pTHX_ OP *o)
5884 const OPCODE type = o->op_type;
5885 return refkids(ck_fun(o), type);
5889 Perl_ck_listiob(pTHX_ OP *o)
5893 kid = cLISTOPo->op_first;
5896 kid = cLISTOPo->op_first;
5898 if (kid->op_type == OP_PUSHMARK)
5899 kid = kid->op_sibling;
5900 if (kid && o->op_flags & OPf_STACKED)
5901 kid = kid->op_sibling;
5902 else if (kid && !kid->op_sibling) { /* print HANDLE; */
5903 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
5904 o->op_flags |= OPf_STACKED; /* make it a filehandle */
5905 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
5906 cLISTOPo->op_first->op_sibling = kid;
5907 cLISTOPo->op_last = kid;
5908 kid = kid->op_sibling;
5913 append_elem(o->op_type, o, newDEFSVOP());
5919 Perl_ck_sassign(pTHX_ OP *o)
5921 OP * const kid = cLISTOPo->op_first;
5922 /* has a disposable target? */
5923 if ((PL_opargs[kid->op_type] & OA_TARGLEX)
5924 && !(kid->op_flags & OPf_STACKED)
5925 /* Cannot steal the second time! */
5926 && !(kid->op_private & OPpTARGET_MY))
5928 OP * const kkid = kid->op_sibling;
5930 /* Can just relocate the target. */
5931 if (kkid && kkid->op_type == OP_PADSV
5932 && !(kkid->op_private & OPpLVAL_INTRO))
5934 kid->op_targ = kkid->op_targ;
5936 /* Now we do not need PADSV and SASSIGN. */
5937 kid->op_sibling = o->op_sibling; /* NULL */
5938 cLISTOPo->op_first = NULL;
5941 kid->op_private |= OPpTARGET_MY; /* Used for context settings */
5949 Perl_ck_match(pTHX_ OP *o)
5951 o->op_private |= OPpRUNTIME;
5956 Perl_ck_method(pTHX_ OP *o)
5958 OP * const kid = cUNOPo->op_first;
5959 if (kid->op_type == OP_CONST) {
5960 SV* sv = kSVOP->op_sv;
5961 const char * const method = SvPVX_const(sv);
5962 if (!(strchr(method, ':') || strchr(method, '\''))) {
5964 if (!SvREADONLY(sv) || !SvFAKE(sv)) {
5965 sv = newSVpvn_share(method, SvCUR(sv), 0);
5968 kSVOP->op_sv = NULL;
5970 cmop = newSVOP(OP_METHOD_NAMED, 0, sv);
5979 Perl_ck_null(pTHX_ OP *o)
5981 PERL_UNUSED_CONTEXT;
5986 Perl_ck_open(pTHX_ OP *o)
5988 HV * const table = GvHV(PL_hintgv);
5990 SV **svp = hv_fetchs(table, "open_IN", FALSE);
5992 const I32 mode = mode_from_discipline(*svp);
5993 if (mode & O_BINARY)
5994 o->op_private |= OPpOPEN_IN_RAW;
5995 else if (mode & O_TEXT)
5996 o->op_private |= OPpOPEN_IN_CRLF;
5999 svp = hv_fetchs(table, "open_OUT", FALSE);
6001 const I32 mode = mode_from_discipline(*svp);
6002 if (mode & O_BINARY)
6003 o->op_private |= OPpOPEN_OUT_RAW;
6004 else if (mode & O_TEXT)
6005 o->op_private |= OPpOPEN_OUT_CRLF;
6008 if (o->op_type == OP_BACKTICK)
6011 /* In case of three-arg dup open remove strictness
6012 * from the last arg if it is a bareword. */
6013 OP * const first = cLISTOPx(o)->op_first; /* The pushmark. */
6014 OP * const last = cLISTOPx(o)->op_last; /* The bareword. */
6018 if ((last->op_type == OP_CONST) && /* The bareword. */
6019 (last->op_private & OPpCONST_BARE) &&
6020 (last->op_private & OPpCONST_STRICT) &&
6021 (oa = first->op_sibling) && /* The fh. */
6022 (oa = oa->op_sibling) && /* The mode. */
6023 (oa->op_type == OP_CONST) &&
6024 SvPOK(((SVOP*)oa)->op_sv) &&
6025 (mode = SvPVX_const(((SVOP*)oa)->op_sv)) &&
6026 mode[0] == '>' && mode[1] == '&' && /* A dup open. */
6027 (last == oa->op_sibling)) /* The bareword. */
6028 last->op_private &= ~OPpCONST_STRICT;
6034 Perl_ck_repeat(pTHX_ OP *o)
6036 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
6037 o->op_private |= OPpREPEAT_DOLIST;
6038 cBINOPo->op_first = force_list(cBINOPo->op_first);
6046 Perl_ck_require(pTHX_ OP *o)
6050 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
6051 SVOP * const kid = (SVOP*)cUNOPo->op_first;
6053 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
6054 SV * const sv = kid->op_sv;
6055 U32 was_readonly = SvREADONLY(sv);
6060 sv_force_normal_flags(sv, 0);
6061 assert(!SvREADONLY(sv));
6068 for (s = SvPVX(sv); *s; s++) {
6069 if (*s == ':' && s[1] == ':') {
6070 const STRLEN len = strlen(s+2)+1;
6072 Move(s+2, s+1, len, char);
6073 SvCUR_set(sv, SvCUR(sv) - 1);
6076 sv_catpvs(sv, ".pm");
6077 SvFLAGS(sv) |= was_readonly;
6081 if (!(o->op_flags & OPf_SPECIAL)) { /* Wasn't written as CORE::require */
6082 /* handle override, if any */
6083 gv = gv_fetchpvs("require", GV_NOTQUAL, SVt_PVCV);
6084 if (!(gv && GvCVu(gv) && GvIMPORTED_CV(gv))) {
6085 GV * const * const gvp = (GV**)hv_fetchs(PL_globalstash, "require", FALSE);
6086 gv = gvp ? *gvp : Nullgv;
6090 if (gv && GvCVu(gv) && GvIMPORTED_CV(gv)) {
6091 OP * const kid = cUNOPo->op_first;
6092 cUNOPo->op_first = 0;
6094 return ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
6095 append_elem(OP_LIST, kid,
6096 scalar(newUNOP(OP_RV2CV, 0,
6105 Perl_ck_return(pTHX_ OP *o)
6107 if (CvLVALUE(PL_compcv)) {
6109 for (kid = cLISTOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
6110 mod(kid, OP_LEAVESUBLV);
6116 Perl_ck_select(pTHX_ OP *o)
6119 if (o->op_flags & OPf_KIDS) {
6120 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6121 if (kid && kid->op_sibling) {
6122 o->op_type = OP_SSELECT;
6123 o->op_ppaddr = PL_ppaddr[OP_SSELECT];
6125 return fold_constants(o);
6129 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6130 if (kid && kid->op_type == OP_RV2GV)
6131 kid->op_private &= ~HINT_STRICT_REFS;
6136 Perl_ck_shift(pTHX_ OP *o)
6138 const I32 type = o->op_type;
6140 if (!(o->op_flags & OPf_KIDS)) {
6144 #ifdef USE_5005THREADS
6145 if (!CvUNIQUE(PL_compcv)) {
6146 argop = newOP(OP_PADAV, OPf_REF);
6147 argop->op_targ = 0; /* PAD_SV(0) is @_ */
6150 argop = newUNOP(OP_RV2AV, 0,
6151 scalar(newGVOP(OP_GV, 0,
6152 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
6155 argop = newUNOP(OP_RV2AV, 0,
6156 scalar(newGVOP(OP_GV, 0, CvUNIQUE(PL_compcv) ? PL_argvgv : PL_defgv)));
6157 #endif /* USE_5005THREADS */
6158 return newUNOP(type, 0, scalar(argop));
6160 return scalar(modkids(ck_fun(o), type));
6164 Perl_ck_sort(pTHX_ OP *o)
6168 if (o->op_type == OP_SORT && o->op_flags & OPf_STACKED)
6170 firstkid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6171 if (o->op_flags & OPf_STACKED) { /* may have been cleared */
6173 OP *kid = cUNOPx(firstkid)->op_first; /* get past null */
6175 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
6177 if (kid->op_type == OP_SCOPE) {
6181 else if (kid->op_type == OP_LEAVE) {
6182 if (o->op_type == OP_SORT) {
6183 op_null(kid); /* wipe out leave */
6186 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
6187 if (k->op_next == kid)
6189 /* don't descend into loops */
6190 else if (k->op_type == OP_ENTERLOOP
6191 || k->op_type == OP_ENTERITER)
6193 k = cLOOPx(k)->op_lastop;
6198 kid->op_next = 0; /* just disconnect the leave */
6199 k = kLISTOP->op_first;
6204 if (o->op_type == OP_SORT) {
6205 /* provide scalar context for comparison function/block */
6211 o->op_flags |= OPf_SPECIAL;
6213 else if (kid->op_type == OP_RV2SV || kid->op_type == OP_PADSV)
6216 firstkid = firstkid->op_sibling;
6219 /* provide list context for arguments */
6220 if (o->op_type == OP_SORT)
6227 S_simplify_sort(pTHX_ OP *o)
6229 register OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
6234 if (!(o->op_flags & OPf_STACKED))
6236 GvMULTI_on(gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV));
6237 GvMULTI_on(gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV));
6238 kid = kUNOP->op_first; /* get past null */
6239 if (kid->op_type != OP_SCOPE)
6241 kid = kLISTOP->op_last; /* get past scope */
6242 switch(kid->op_type) {
6250 k = kid; /* remember this node*/
6251 if (kBINOP->op_first->op_type != OP_RV2SV)
6253 kid = kBINOP->op_first; /* get past cmp */
6254 if (kUNOP->op_first->op_type != OP_GV)
6256 kid = kUNOP->op_first; /* get past rv2sv */
6258 if (GvSTASH(gv) != PL_curstash)
6260 gvname = GvNAME(gv);
6261 if (*gvname == 'a' && gvname[1] == '\0')
6263 else if (*gvname == 'b' && gvname[1] == '\0')
6268 kid = k; /* back to cmp */
6269 if (kBINOP->op_last->op_type != OP_RV2SV)
6271 kid = kBINOP->op_last; /* down to 2nd arg */
6272 if (kUNOP->op_first->op_type != OP_GV)
6274 kid = kUNOP->op_first; /* get past rv2sv */
6276 if (GvSTASH(gv) != PL_curstash)
6278 gvname = GvNAME(gv);
6280 ? !(*gvname == 'a' && gvname[1] == '\0')
6281 : !(*gvname == 'b' && gvname[1] == '\0'))
6283 o->op_flags &= ~(OPf_STACKED | OPf_SPECIAL);
6285 o->op_private |= OPpSORT_DESCEND;
6286 if (k->op_type == OP_NCMP)
6287 o->op_private |= OPpSORT_NUMERIC;
6288 if (k->op_type == OP_I_NCMP)
6289 o->op_private |= OPpSORT_NUMERIC | OPpSORT_INTEGER;
6290 kid = cLISTOPo->op_first->op_sibling;
6291 cLISTOPo->op_first->op_sibling = kid->op_sibling; /* bypass old block */
6292 op_free(kid); /* then delete it */
6296 Perl_ck_split(pTHX_ OP *o)
6300 if (o->op_flags & OPf_STACKED)
6301 return no_fh_allowed(o);
6303 kid = cLISTOPo->op_first;
6304 if (kid->op_type != OP_NULL)
6305 Perl_croak(aTHX_ "panic: ck_split");
6306 kid = kid->op_sibling;
6307 op_free(cLISTOPo->op_first);
6308 cLISTOPo->op_first = kid;
6310 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpvs(" "));
6311 cLISTOPo->op_last = kid; /* There was only one element previously */
6314 if (kid->op_type != OP_MATCH || kid->op_flags & OPf_STACKED) {
6315 OP * const sibl = kid->op_sibling;
6316 kid->op_sibling = 0;
6317 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
6318 if (cLISTOPo->op_first == cLISTOPo->op_last)
6319 cLISTOPo->op_last = kid;
6320 cLISTOPo->op_first = kid;
6321 kid->op_sibling = sibl;
6324 kid->op_type = OP_PUSHRE;
6325 kid->op_ppaddr = PL_ppaddr[OP_PUSHRE];
6327 if (((PMOP *)kid)->op_pmflags & PMf_GLOBAL && ckWARN(WARN_REGEXP)) {
6328 Perl_warner(aTHX_ packWARN(WARN_REGEXP),
6329 "Use of /g modifier is meaningless in split");
6332 if (!kid->op_sibling)
6333 append_elem(OP_SPLIT, o, newDEFSVOP());
6335 kid = kid->op_sibling;
6338 if (!kid->op_sibling)
6339 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
6340 assert(kid->op_sibling);
6342 kid = kid->op_sibling;
6345 if (kid->op_sibling)
6346 return too_many_arguments(o,OP_DESC(o));
6352 Perl_ck_join(pTHX_ OP *o)
6354 const OP * const kid = cLISTOPo->op_first->op_sibling;
6355 if (kid && kid->op_type == OP_MATCH) {
6356 if (ckWARN(WARN_SYNTAX)) {
6357 const REGEXP *re = PM_GETRE(kPMOP);
6358 const char *pmstr = re ? re->precomp : "STRING";
6359 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
6360 "/%s/ should probably be written as \"%s\"",
6368 Perl_ck_subr(pTHX_ OP *o)
6370 OP *prev = ((cUNOPo->op_first->op_sibling)
6371 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
6372 OP *o2 = prev->op_sibling;
6374 const char *proto = NULL;
6375 const char *proto_end = NULL;
6380 I32 contextclass = 0;
6381 const char *e = NULL;
6383 o->op_private |= OPpENTERSUB_HASTARG;
6384 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
6385 if (cvop->op_type == OP_RV2CV) {
6387 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
6388 op_null(cvop); /* disable rv2cv */
6389 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
6390 if (tmpop->op_type == OP_GV && !(o->op_private & OPpENTERSUB_AMPER)) {
6391 GV *gv = cGVOPx_gv(tmpop);
6394 tmpop->op_private |= OPpEARLY_CV;
6395 else if (SvPOK(cv)) {
6397 namegv = CvANON(cv) ? gv : CvGV(cv);
6398 proto = SvPV((SV*)cv, len);
6399 proto_end = proto + len;
6403 else if (cvop->op_type == OP_METHOD || cvop->op_type == OP_METHOD_NAMED) {
6404 if (o2->op_type == OP_CONST)
6405 o2->op_private &= ~OPpCONST_STRICT;
6406 else if (o2->op_type == OP_LIST) {
6407 OP * const sib = ((UNOP*)o2)->op_first->op_sibling;
6408 if (sib && sib->op_type == OP_CONST)
6409 sib->op_private &= ~OPpCONST_STRICT;
6412 o->op_private |= (PL_hints & HINT_STRICT_REFS);
6413 if (PERLDB_SUB && PL_curstash != PL_debstash)
6414 o->op_private |= OPpENTERSUB_DB;
6415 while (o2 != cvop) {
6417 if (proto >= proto_end)
6418 return too_many_arguments(o, gv_ename(namegv));
6438 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
6440 arg == 1 ? "block or sub {}" : "sub {}",
6441 gv_ename(namegv), o2);
6444 /* '*' allows any scalar type, including bareword */
6447 if (o2->op_type == OP_RV2GV)
6448 goto wrapref; /* autoconvert GLOB -> GLOBref */
6449 else if (o2->op_type == OP_CONST)
6450 o2->op_private &= ~OPpCONST_STRICT;
6451 else if (o2->op_type == OP_ENTERSUB) {
6452 /* accidental subroutine, revert to bareword */
6453 OP *gvop = ((UNOP*)o2)->op_first;
6454 if (gvop && gvop->op_type == OP_NULL) {
6455 gvop = ((UNOP*)gvop)->op_first;
6457 for (; gvop->op_sibling; gvop = gvop->op_sibling)
6460 (gvop->op_private & OPpENTERSUB_NOPAREN) &&
6461 (gvop = ((UNOP*)gvop)->op_first) &&
6462 gvop->op_type == OP_GV)
6464 GV * const gv = cGVOPx_gv(gvop);
6465 OP * const sibling = o2->op_sibling;
6466 SV * const n = newSVpvs("");
6468 gv_fullname4(n, gv, "", FALSE);
6469 o2 = newSVOP(OP_CONST, 0, n);
6470 prev->op_sibling = o2;
6471 o2->op_sibling = sibling;
6487 if (contextclass++ == 0) {
6488 e = strchr(proto, ']');
6489 if (!e || e == proto)
6498 const char *p = proto;
6499 const char *const end = proto;
6501 while (*--p != '[');
6502 bad_type(arg, Perl_form(aTHX_ "one of %.*s",
6504 gv_ename(namegv), o2);
6509 if (o2->op_type == OP_RV2GV)
6512 bad_type(arg, "symbol", gv_ename(namegv), o2);
6515 if (o2->op_type == OP_ENTERSUB)
6518 bad_type(arg, "subroutine entry", gv_ename(namegv), o2);
6521 if (o2->op_type == OP_RV2SV ||
6522 o2->op_type == OP_PADSV ||
6523 o2->op_type == OP_HELEM ||
6524 o2->op_type == OP_AELEM
6525 #ifdef USE_5005THREADS
6526 || o2->op_type == OP_THREADSV
6531 bad_type(arg, "scalar", gv_ename(namegv), o2);
6534 if (o2->op_type == OP_RV2AV ||
6535 o2->op_type == OP_PADAV)
6538 bad_type(arg, "array", gv_ename(namegv), o2);
6541 if (o2->op_type == OP_RV2HV ||
6542 o2->op_type == OP_PADHV)
6545 bad_type(arg, "hash", gv_ename(namegv), o2);
6550 OP* const sib = kid->op_sibling;
6551 kid->op_sibling = 0;
6552 o2 = newUNOP(OP_REFGEN, 0, kid);
6553 o2->op_sibling = sib;
6554 prev->op_sibling = o2;
6556 if (contextclass && e) {
6571 Perl_croak(aTHX_ "Malformed prototype for %s: %"SVf,
6572 gv_ename(namegv), (void*)cv);
6577 mod(o2, OP_ENTERSUB);
6579 o2 = o2->op_sibling;
6581 if (proto && !optional && proto_end > proto &&
6582 (*proto != '@' && *proto != '%' && *proto != ';'))
6583 return too_few_arguments(o, gv_ename(namegv));
6588 Perl_ck_svconst(pTHX_ OP *o)
6590 PERL_UNUSED_CONTEXT;
6591 SvREADONLY_on(cSVOPo->op_sv);
6596 Perl_ck_trunc(pTHX_ OP *o)
6598 if (o->op_flags & OPf_KIDS) {
6599 SVOP *kid = (SVOP*)cUNOPo->op_first;
6601 if (kid->op_type == OP_NULL)
6602 kid = (SVOP*)kid->op_sibling;
6603 if (kid && kid->op_type == OP_CONST &&
6604 (kid->op_private & OPpCONST_BARE))
6606 o->op_flags |= OPf_SPECIAL;
6607 kid->op_private &= ~OPpCONST_STRICT;
6614 Perl_ck_substr(pTHX_ OP *o)
6617 if ((o->op_flags & OPf_KIDS) && o->op_private == 4) {
6618 OP *kid = cLISTOPo->op_first;
6620 if (kid->op_type == OP_NULL)
6621 kid = kid->op_sibling;
6623 kid->op_flags |= OPf_MOD;
6629 /* A peephole optimizer. We visit the ops in the order they're to execute.
6630 * See the comments at the top of this file for more details about when
6631 * peep() is called */
6634 Perl_peep(pTHX_ register OP *o)
6636 register OP* oldop = NULL;
6639 if (!o || o->op_seq)
6643 SAVEVPTR(PL_curcop);
6644 for (; o; o = o->op_next) {
6647 /* The special value -1 is used by the B::C compiler backend to indicate
6648 * that an op is statically defined and should not be freed */
6649 if (!PL_op_seqmax || PL_op_seqmax == (U16)-1)
6652 switch (o->op_type) {
6656 PL_curcop = ((COP*)o); /* for warnings */
6657 o->op_seq = PL_op_seqmax++;
6661 if (cSVOPo->op_private & OPpCONST_STRICT)
6662 no_bareword_allowed(o);
6664 case OP_METHOD_NAMED:
6665 /* Relocate sv to the pad for thread safety.
6666 * Despite being a "constant", the SV is written to,
6667 * for reference counts, sv_upgrade() etc. */
6669 const PADOFFSET ix = pad_alloc(OP_CONST, SVs_PADTMP);
6670 if (o->op_type == OP_CONST && SvPADTMP(cSVOPo->op_sv)) {
6671 /* If op_sv is already a PADTMP then it is being used by
6672 * some pad, so make a copy. */
6673 sv_setsv(PAD_SVl(ix),cSVOPo->op_sv);
6674 SvREADONLY_on(PAD_SVl(ix));
6675 SvREFCNT_dec(cSVOPo->op_sv);
6677 else if (o->op_type == OP_CONST
6678 && cSVOPo->op_sv == &PL_sv_undef) {
6679 /* PL_sv_undef is hack - it's unsafe to store it in the
6680 AV that is the pad, because av_fetch treats values of
6681 PL_sv_undef as a "free" AV entry and will merrily
6682 replace them with a new SV, causing pad_alloc to think
6683 that this pad slot is free. (When, clearly, it is not)
6685 SvOK_off(PAD_SVl(ix));
6686 SvPADTMP_on(PAD_SVl(ix));
6687 SvREADONLY_on(PAD_SVl(ix));
6690 SvREFCNT_dec(PAD_SVl(ix));
6691 SvPADTMP_on(cSVOPo->op_sv);
6692 PAD_SETSV(ix, cSVOPo->op_sv);
6693 /* XXX I don't know how this isn't readonly already. */
6694 SvREADONLY_on(PAD_SVl(ix));
6696 cSVOPo->op_sv = NULL;
6700 o->op_seq = PL_op_seqmax++;
6704 if (o->op_next && o->op_next->op_type == OP_STRINGIFY) {
6705 if (o->op_next->op_private & OPpTARGET_MY) {
6706 if (o->op_flags & OPf_STACKED) /* chained concats */
6707 goto ignore_optimization;
6709 /* assert(PL_opargs[o->op_type] & OA_TARGLEX); */
6710 o->op_targ = o->op_next->op_targ;
6711 o->op_next->op_targ = 0;
6712 o->op_private |= OPpTARGET_MY;
6715 op_null(o->op_next);
6717 ignore_optimization:
6718 o->op_seq = PL_op_seqmax++;
6721 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
6722 o->op_seq = PL_op_seqmax++;
6723 break; /* Scalar stub must produce undef. List stub is noop */
6727 if (o->op_targ == OP_NEXTSTATE
6728 || o->op_targ == OP_DBSTATE
6729 || o->op_targ == OP_SETSTATE)
6731 PL_curcop = ((COP*)o);
6733 /* XXX: We avoid setting op_seq here to prevent later calls
6734 to peep() from mistakenly concluding that optimisation
6735 has already occurred. This doesn't fix the real problem,
6736 though (See 20010220.007). AMS 20010719 */
6737 if (oldop && o->op_next) {
6738 oldop->op_next = o->op_next;
6746 if (oldop && o->op_next) {
6747 oldop->op_next = o->op_next;
6750 o->op_seq = PL_op_seqmax++;
6755 if (o->op_type == OP_PADAV || o->op_next->op_type == OP_RV2AV) {
6756 OP* const pop = (o->op_type == OP_PADAV) ?
6757 o->op_next : o->op_next->op_next;
6759 if (pop && pop->op_type == OP_CONST &&
6760 ((PL_op = pop->op_next)) &&
6761 pop->op_next->op_type == OP_AELEM &&
6762 !(pop->op_next->op_private &
6763 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF|OPpMAYBE_LVSUB)) &&
6764 (i = SvIV(((SVOP*)pop)->op_sv) - CopARYBASE_get(PL_curcop))
6769 if (cSVOPx(pop)->op_private & OPpCONST_STRICT)
6770 no_bareword_allowed(pop);
6771 if (o->op_type == OP_GV)
6772 op_null(o->op_next);
6773 op_null(pop->op_next);
6775 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
6776 o->op_next = pop->op_next->op_next;
6777 o->op_ppaddr = PL_ppaddr[OP_AELEMFAST];
6778 o->op_private = (U8)i;
6779 if (o->op_type == OP_GV) {
6784 o->op_flags |= OPf_SPECIAL;
6785 o->op_type = OP_AELEMFAST;
6787 o->op_seq = PL_op_seqmax++;
6791 if (o->op_next->op_type == OP_RV2SV) {
6792 if (!(o->op_next->op_private & OPpDEREF)) {
6793 op_null(o->op_next);
6794 o->op_private |= o->op_next->op_private & (OPpLVAL_INTRO
6796 o->op_next = o->op_next->op_next;
6797 o->op_type = OP_GVSV;
6798 o->op_ppaddr = PL_ppaddr[OP_GVSV];
6801 else if ((o->op_private & OPpEARLY_CV) && ckWARN(WARN_PROTOTYPE)) {
6802 GV * const gv = cGVOPo_gv;
6803 if (SvTYPE(gv) == SVt_PVGV && GvCV(gv) && SvPVX_const(GvCV(gv))) {
6804 /* XXX could check prototype here instead of just carping */
6805 SV * const sv = sv_newmortal();
6806 gv_efullname3(sv, gv, NULL);
6807 Perl_warner(aTHX_ packWARN(WARN_PROTOTYPE),
6808 "%"SVf"() called too early to check prototype",
6812 else if (o->op_next->op_type == OP_READLINE
6813 && o->op_next->op_next->op_type == OP_CONCAT
6814 && (o->op_next->op_next->op_flags & OPf_STACKED))
6816 /* Turn "$a .= <FH>" into an OP_RCATLINE. AMS 20010917 */
6817 o->op_type = OP_RCATLINE;
6818 o->op_flags |= OPf_STACKED;
6819 o->op_ppaddr = PL_ppaddr[OP_RCATLINE];
6820 op_null(o->op_next->op_next);
6821 op_null(o->op_next);
6824 o->op_seq = PL_op_seqmax++;
6835 o->op_seq = PL_op_seqmax++;
6836 while (cLOGOP->op_other->op_type == OP_NULL)
6837 cLOGOP->op_other = cLOGOP->op_other->op_next;
6838 peep(cLOGOP->op_other); /* Recursive calls are not replaced by fptr calls */
6843 o->op_seq = PL_op_seqmax++;
6844 while (cLOOP->op_redoop->op_type == OP_NULL)
6845 cLOOP->op_redoop = cLOOP->op_redoop->op_next;
6846 peep(cLOOP->op_redoop);
6847 while (cLOOP->op_nextop->op_type == OP_NULL)
6848 cLOOP->op_nextop = cLOOP->op_nextop->op_next;
6849 peep(cLOOP->op_nextop);
6850 while (cLOOP->op_lastop->op_type == OP_NULL)
6851 cLOOP->op_lastop = cLOOP->op_lastop->op_next;
6852 peep(cLOOP->op_lastop);
6858 o->op_seq = PL_op_seqmax++;
6859 while (cPMOP->op_pmreplstart &&
6860 cPMOP->op_pmreplstart->op_type == OP_NULL)
6861 cPMOP->op_pmreplstart = cPMOP->op_pmreplstart->op_next;
6862 peep(cPMOP->op_pmreplstart);
6866 o->op_seq = PL_op_seqmax++;
6867 if (o->op_next && o->op_next->op_type == OP_NEXTSTATE
6868 && ckWARN(WARN_SYNTAX))
6870 if (o->op_next->op_sibling) {
6871 const OPCODE type = o->op_next->op_sibling->op_type;
6872 if (type != OP_EXIT && type != OP_WARN && type != OP_DIE) {
6873 const line_t oldline = CopLINE(PL_curcop);
6874 CopLINE_set(PL_curcop, CopLINE((COP*)o->op_next));
6875 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6876 "Statement unlikely to be reached");
6877 Perl_warner(aTHX_ packWARN(WARN_EXEC),
6878 "\t(Maybe you meant system() when you said exec()?)\n");
6879 CopLINE_set(PL_curcop, oldline);
6889 SV **svp, **indsvp, *sv;
6891 const char *key = NULL;
6894 o->op_seq = PL_op_seqmax++;
6896 if (((BINOP*)o)->op_last->op_type != OP_CONST)
6899 /* Make the CONST have a shared SV */
6900 svp = cSVOPx_svp(((BINOP*)o)->op_last);
6901 if ((!SvFAKE(sv = *svp) || !SvREADONLY(sv)) && !IS_PADCONST(sv)) {
6902 key = SvPV_const(sv, keylen);
6903 lexname = newSVpvn_share(key,
6904 SvUTF8(sv) ? -(I32)keylen : (I32)keylen,
6910 if ((o->op_private & (OPpLVAL_INTRO)))
6913 rop = (UNOP*)((BINOP*)o)->op_first;
6914 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6916 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6917 if (!SvPAD_TYPED(lexname))
6919 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
6920 if (!fields || !GvHV(*fields))
6922 key = SvPV_const(*svp, keylen);
6923 indsvp = hv_fetch(GvHV(*fields), key,
6924 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen, FALSE);
6926 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6927 "in variable %s of type %s",
6928 key, SvPV_nolen_const(lexname),
6929 HvNAME_get(SvSTASH(lexname)));
6931 ind = SvIV(*indsvp);
6933 Perl_croak(aTHX_ "Bad index while coercing array into hash");
6934 rop->op_type = OP_RV2AV;
6935 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6936 o->op_type = OP_AELEM;
6937 o->op_ppaddr = PL_ppaddr[OP_AELEM];
6939 if (SvREADONLY(*svp))
6941 SvFLAGS(sv) |= (SvFLAGS(*svp)
6942 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
6952 SV **svp, **indsvp, *sv;
6956 SVOP *first_key_op, *key_op;
6958 o->op_seq = PL_op_seqmax++;
6959 if ((o->op_private & (OPpLVAL_INTRO))
6960 /* I bet there's always a pushmark... */
6961 || ((LISTOP*)o)->op_first->op_sibling->op_type != OP_LIST)
6962 /* hmmm, no optimization if list contains only one key. */
6964 rop = (UNOP*)((LISTOP*)o)->op_last;
6965 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
6967 lexname = *av_fetch(PL_comppad_name, rop->op_first->op_targ, TRUE);
6968 if (!SvPAD_TYPED(lexname))
6970 fields = (GV**)hv_fetchs(SvSTASH(lexname), "FIELDS", FALSE);
6971 if (!fields || !GvHV(*fields))
6973 /* Again guessing that the pushmark can be jumped over.... */
6974 first_key_op = (SVOP*)((LISTOP*)((LISTOP*)o)->op_first->op_sibling)
6975 ->op_first->op_sibling;
6976 /* Check that the key list contains only constants. */
6977 for (key_op = first_key_op; key_op;
6978 key_op = (SVOP*)key_op->op_sibling)
6979 if (key_op->op_type != OP_CONST)
6983 rop->op_type = OP_RV2AV;
6984 rop->op_ppaddr = PL_ppaddr[OP_RV2AV];
6985 o->op_type = OP_ASLICE;
6986 o->op_ppaddr = PL_ppaddr[OP_ASLICE];
6987 for (key_op = first_key_op; key_op;
6988 key_op = (SVOP*)key_op->op_sibling) {
6989 svp = cSVOPx_svp(key_op);
6990 key = SvPV_const(*svp, keylen);
6991 indsvp = hv_fetch(GvHV(*fields), key,
6992 SvUTF8(*svp) ? -(I32)keylen : (I32)keylen,
6995 Perl_croak(aTHX_ "No such pseudo-hash field \"%s\" "
6996 "in variable %s of type %s",
6997 key, SvPV(lexname, n_a), HvNAME_get(SvSTASH(lexname)));
6999 ind = SvIV(*indsvp);
7001 Perl_croak(aTHX_ "Bad index while coercing array into hash");
7003 if (SvREADONLY(*svp))
7005 SvFLAGS(sv) |= (SvFLAGS(*svp)
7006 & (SVs_PADBUSY|SVs_PADTMP|SVs_PADMY));
7014 /* will point to RV2AV or PADAV op on LHS/RHS of assign */
7018 /* check that RHS of sort is a single plain array */
7019 OP *oright = cUNOPo->op_first;
7020 if (!oright || oright->op_type != OP_PUSHMARK)
7023 /* reverse sort ... can be optimised. */
7024 if (!cUNOPo->op_sibling) {
7025 /* Nothing follows us on the list. */
7026 OP * const reverse = o->op_next;
7028 if (reverse->op_type == OP_REVERSE &&
7029 (reverse->op_flags & OPf_WANT) == OPf_WANT_LIST) {
7030 OP * const pushmark = cUNOPx(reverse)->op_first;
7031 if (pushmark && (pushmark->op_type == OP_PUSHMARK)
7032 && (cUNOPx(pushmark)->op_sibling == o)) {
7033 /* reverse -> pushmark -> sort */
7034 o->op_private |= OPpSORT_REVERSE;
7036 pushmark->op_next = oright->op_next;
7042 /* make @a = sort @a act in-place */
7044 o->op_seq = PL_op_seqmax++;
7046 oright = cUNOPx(oright)->op_sibling;
7049 if (oright->op_type == OP_NULL) { /* skip sort block/sub */
7050 oright = cUNOPx(oright)->op_sibling;
7054 (oright->op_type != OP_RV2AV && oright->op_type != OP_PADAV)
7055 || oright->op_next != o
7056 || (oright->op_private & OPpLVAL_INTRO)
7060 /* o2 follows the chain of op_nexts through the LHS of the
7061 * assign (if any) to the aassign op itself */
7063 if (!o2 || o2->op_type != OP_NULL)
7066 if (!o2 || o2->op_type != OP_PUSHMARK)
7069 if (o2 && o2->op_type == OP_GV)
7072 || (o2->op_type != OP_PADAV && o2->op_type != OP_RV2AV)
7073 || (o2->op_private & OPpLVAL_INTRO)
7078 if (!o2 || o2->op_type != OP_NULL)
7081 if (!o2 || o2->op_type != OP_AASSIGN
7082 || (o2->op_flags & OPf_WANT) != OPf_WANT_VOID)
7085 /* check that the sort is the first arg on RHS of assign */
7087 o2 = cUNOPx(o2)->op_first;
7088 if (!o2 || o2->op_type != OP_NULL)
7090 o2 = cUNOPx(o2)->op_first;
7091 if (!o2 || o2->op_type != OP_PUSHMARK)
7093 if (o2->op_sibling != o)
7096 /* check the array is the same on both sides */
7097 if (oleft->op_type == OP_RV2AV) {
7098 if (oright->op_type != OP_RV2AV
7099 || !cUNOPx(oright)->op_first
7100 || cUNOPx(oright)->op_first->op_type != OP_GV
7101 || cGVOPx_gv(cUNOPx(oleft)->op_first) !=
7102 cGVOPx_gv(cUNOPx(oright)->op_first)
7106 else if (oright->op_type != OP_PADAV
7107 || oright->op_targ != oleft->op_targ
7111 /* transfer MODishness etc from LHS arg to RHS arg */
7112 oright->op_flags = oleft->op_flags;
7113 o->op_private |= OPpSORT_INPLACE;
7115 /* excise push->gv->rv2av->null->aassign */
7116 o2 = o->op_next->op_next;
7117 op_null(o2); /* PUSHMARK */
7119 if (o2->op_type == OP_GV) {
7120 op_null(o2); /* GV */
7123 op_null(o2); /* RV2AV or PADAV */
7124 o2 = o2->op_next->op_next;
7125 op_null(o2); /* AASSIGN */
7127 o->op_next = o2->op_next;
7133 OP *ourmark, *theirmark, *ourlast, *iter, *expushmark, *rv2av;
7135 LISTOP *enter, *exlist;
7136 o->op_seq = PL_op_seqmax++;
7138 enter = (LISTOP *) o->op_next;
7141 if (enter->op_type == OP_NULL) {
7142 enter = (LISTOP *) enter->op_next;
7146 /* for $a (...) will have OP_GV then OP_RV2GV here.
7147 for (...) just has an OP_GV. */
7148 if (enter->op_type == OP_GV) {
7149 gvop = (OP *) enter;
7150 enter = (LISTOP *) enter->op_next;
7153 if (enter->op_type == OP_RV2GV) {
7154 enter = (LISTOP *) enter->op_next;
7160 if (enter->op_type != OP_ENTERITER)
7163 iter = enter->op_next;
7164 if (!iter || iter->op_type != OP_ITER)
7167 expushmark = enter->op_first;
7168 if (!expushmark || expushmark->op_type != OP_NULL
7169 || expushmark->op_targ != OP_PUSHMARK)
7172 exlist = (LISTOP *) expushmark->op_sibling;
7173 if (!exlist || exlist->op_type != OP_NULL
7174 || exlist->op_targ != OP_LIST)
7177 if (exlist->op_last != o) {
7178 /* Mmm. Was expecting to point back to this op. */
7181 theirmark = exlist->op_first;
7182 if (!theirmark || theirmark->op_type != OP_PUSHMARK)
7185 if (theirmark->op_sibling != o) {
7186 /* There's something between the mark and the reverse, eg
7187 for (1, reverse (...))
7192 ourmark = ((LISTOP *)o)->op_first;
7193 if (!ourmark || ourmark->op_type != OP_PUSHMARK)
7196 ourlast = ((LISTOP *)o)->op_last;
7197 if (!ourlast || ourlast->op_next != o)
7200 rv2av = ourmark->op_sibling;
7201 if (rv2av && rv2av->op_type == OP_RV2AV && rv2av->op_sibling == 0
7202 && rv2av->op_flags == (OPf_WANT_LIST | OPf_KIDS)
7203 && enter->op_flags == (OPf_WANT_LIST | OPf_KIDS)) {
7204 /* We're just reversing a single array. */
7205 rv2av->op_flags = OPf_WANT_SCALAR | OPf_KIDS | OPf_REF;
7206 enter->op_flags |= OPf_STACKED;
7209 /* We don't have control over who points to theirmark, so sacrifice
7211 theirmark->op_next = ourmark->op_next;
7212 theirmark->op_flags = ourmark->op_flags;
7213 ourlast->op_next = gvop ? gvop : (OP *) enter;
7216 enter->op_private |= OPpITER_REVERSED;
7217 iter->op_private |= OPpITER_REVERSED;
7224 UNOP *refgen, *rv2cv;
7227 /* I do not understand this, but if o->op_opt isn't set to 1,
7228 various tests in ext/B/t/bytecode.t fail with no readily
7230 /* Converted from op_opt to op_seq for 5.8.x. */
7232 o->op_seq = PL_op_seqmax++;
7235 if ((o->op_flags && OPf_WANT) != OPf_WANT_VOID)
7238 if ((o->op_private & ~OPpASSIGN_BACKWARDS) != 2)
7241 rv2gv = ((BINOP *)o)->op_last;
7242 if (!rv2gv || rv2gv->op_type != OP_RV2GV)
7245 refgen = (UNOP *)((BINOP *)o)->op_first;
7247 if (!refgen || refgen->op_type != OP_REFGEN)
7250 exlist = (LISTOP *)refgen->op_first;
7251 if (!exlist || exlist->op_type != OP_NULL
7252 || exlist->op_targ != OP_LIST)
7255 if (exlist->op_first->op_type != OP_PUSHMARK)
7258 rv2cv = (UNOP*)exlist->op_last;
7260 if (rv2cv->op_type != OP_RV2CV)
7263 assert ((rv2gv->op_private & OPpDONT_INIT_GV) == 0);
7264 assert ((o->op_private & OPpASSIGN_CV_TO_GV) == 0);
7265 assert ((rv2cv->op_private & OPpMAY_RETURN_CONSTANT) == 0);
7267 o->op_private |= OPpASSIGN_CV_TO_GV;
7268 rv2gv->op_private |= OPpDONT_INIT_GV;
7269 rv2cv->op_private |= OPpMAY_RETURN_CONSTANT;
7276 o->op_seq = PL_op_seqmax++;
7285 Perl_custom_op_name(pTHX_ OP* o)
7287 const IV index = PTR2IV(o->op_ppaddr);
7291 if (!PL_custom_op_names) /* This probably shouldn't happen */
7292 return (char *)PL_op_name[OP_CUSTOM];
7294 keysv = sv_2mortal(newSViv(index));
7296 he = hv_fetch_ent(PL_custom_op_names, keysv, 0, 0);
7298 return (char *)PL_op_name[OP_CUSTOM]; /* Don't know who you are */
7300 return SvPV_nolen(HeVAL(he));
7304 Perl_custom_op_desc(pTHX_ OP* o)
7306 const IV index = PTR2IV(o->op_ppaddr);
7310 if (!PL_custom_op_descs)
7311 return (char *)PL_op_desc[OP_CUSTOM];
7313 keysv = sv_2mortal(newSViv(index));
7315 he = hv_fetch_ent(PL_custom_op_descs, keysv, 0, 0);
7317 return (char *)PL_op_desc[OP_CUSTOM];
7319 return SvPV_nolen(HeVAL(he));
7324 /* Efficient sub that returns a constant scalar value. */
7326 const_sv_xsub(pTHX_ CV* cv)
7332 Perl_croak(aTHX_ "usage: %s::%s()",
7333 HvNAME_get(GvSTASH(CvGV(cv))), GvNAME(CvGV(cv)));
7337 ST(0) = (SV*)XSANY.any_ptr;
7343 * c-indentation-style: bsd
7345 * indent-tabs-mode: t
7348 * ex: set ts=8 sts=4 sw=4 noet: