3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
22 #define CHECKCALL this->*check
24 #define CHECKCALL *check
28 * In the following definition, the ", Nullop" is just to make the compiler
29 * think the expression is of the right type: croak actually does a Siglongjmp.
31 #define CHECKOP(type,o) \
32 ((op_mask && op_mask[type]) \
33 ? ( op_free((OP*)o), \
34 croak("%s trapped by operation mask", op_desc[type]), \
36 : (CHECKCALL[type])((OP*)o))
38 static bool scalar_mod_type _((OP *o, I32 type));
40 static I32 list_assignment _((OP *o));
41 static void bad_type _((I32 n, char *t, char *name, OP *kid));
42 static OP *modkids _((OP *o, I32 type));
43 static OP *no_fh_allowed _((OP *o));
44 static OP *scalarboolean _((OP *o));
45 static OP *too_few_arguments _((OP *o, char* name));
46 static OP *too_many_arguments _((OP *o, char* name));
47 static void null _((OP* o));
48 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, U32 seq,
49 CV* startcv, I32 cx_ix));
50 static OP *newDEFSVOP _((void));
56 SV* tmpsv = sv_newmortal();
57 gv_efullname3(tmpsv, gv, Nullch);
58 return SvPV(tmpsv,na);
64 yyerror(form("Missing comma after first argument to %s function",
65 op_desc[o->op_type]));
70 too_few_arguments(OP *o, char *name)
72 yyerror(form("Not enough arguments for %s", name));
77 too_many_arguments(OP *o, char *name)
79 yyerror(form("Too many arguments for %s", name));
84 bad_type(I32 n, char *t, char *name, OP *kid)
86 yyerror(form("Type of arg %d to %s must be %s (not %s)",
87 (int)n, name, t, op_desc[kid->op_type]));
93 int type = o->op_type;
94 if (type != OP_AELEM && type != OP_HELEM) {
95 yyerror(form("Can't use subscript on %s", op_desc[type]));
96 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV)
97 warn("(Did you mean $ or @ instead of %c?)\n",
98 type == OP_ENTERSUB ? '&' : '%');
102 /* "register" allocation */
105 pad_allocmy(char *name)
111 if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
112 if (!isPRINT(name[1])) {
114 name[2] = toCTRL(name[1]);
117 croak("Can't use global %s in \"my\"",name);
119 if (dowarn && AvFILL(comppad_name) >= 0) {
120 SV **svp = AvARRAY(comppad_name);
121 for (off = AvFILL(comppad_name); off > comppad_name_floor; off--) {
124 && SvIVX(sv) == 999999999 /* var is in open scope */
125 && strEQ(name, SvPVX(sv)))
127 warn("\"my\" variable %s masks earlier declaration in same scope", name);
132 off = pad_alloc(OP_PADSV, SVs_PADMY);
134 sv_upgrade(sv, SVt_PVNV);
138 croak("Can't declare class for non-scalar %s in \"my\"",name);
140 (void)SvUPGRADE(sv, SVt_PVMG);
141 SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash);
144 av_store(comppad_name, off, sv);
145 SvNVX(sv) = (double)999999999;
146 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
147 if (!min_intro_pending)
148 min_intro_pending = off;
149 max_intro_pending = off;
151 av_store(comppad, off, (SV*)newAV());
152 else if (*name == '%')
153 av_store(comppad, off, (SV*)newHV());
154 SvPADMY_on(curpad[off]);
159 #ifndef CAN_PROTOTYPE
160 pad_findlex(name, newoff, seq, startcv, cx_ix)
167 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
175 register PERL_CONTEXT *cx;
178 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
179 AV *curlist = CvPADLIST(cv);
180 SV **svp = av_fetch(curlist, 0, FALSE);
183 if (!svp || *svp == &sv_undef)
186 svp = AvARRAY(curname);
187 for (off = AvFILL(curname); off > 0; off--) {
188 if ((sv = svp[off]) &&
191 seq > I_32(SvNVX(sv)) &&
192 strEQ(SvPVX(sv), name))
203 return 0; /* don't clone from inactive stack frame */
207 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
208 oldsv = *av_fetch(oldpad, off, TRUE);
209 if (!newoff) { /* Not a mere clone operation. */
210 SV *namesv = NEWSV(1103,0);
211 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
212 sv_upgrade(namesv, SVt_PVNV);
213 sv_setpv(namesv, name);
214 av_store(comppad_name, newoff, namesv);
215 SvNVX(namesv) = (double)curcop->cop_seq;
216 SvIVX(namesv) = 999999999; /* A ref, intro immediately */
217 SvFAKE_on(namesv); /* A ref, not a real var */
218 if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) {
219 /* "It's closures all the way down." */
223 oldsv = Nullsv; /* no need to keep ref */
228 bcv && bcv != cv && !CvCLONE(bcv);
229 bcv = CvOUTSIDE(bcv)) {
233 if (dowarn && !CvUNIQUE(cv))
235 "Variable \"%s\" may be unavailable",
242 else if (!CvUNIQUE(compcv)) {
243 if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv))
244 warn("Variable \"%s\" will not stay shared", name);
247 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
253 /* Nothing in current lexical context--try eval's context, if any.
254 * This is necessary to let the perldb get at lexically scoped variables.
255 * XXX This will also probably interact badly with eval tree caching.
259 for (i = cx_ix; i >= 0; i--) {
261 switch (cx->cx_type) {
263 if (i == 0 && saweval) {
264 seq = cxstack[saweval].blk_oldcop->cop_seq;
265 return pad_findlex(name, newoff, seq, main_cv, 0);
269 switch (cx->blk_eval.old_op_type) {
274 /* require must have its own scope */
282 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
283 saweval = i; /* so we know where we were called from */
286 seq = cxstack[saweval].blk_oldcop->cop_seq;
287 return pad_findlex(name, newoff, seq, cv, i-1);
295 pad_findmy(char *name)
301 SV **svp = AvARRAY(comppad_name);
302 U32 seq = cop_seqmax;
306 * Special case to get lexical (and hence per-thread) @_.
307 * XXX I need to find out how to tell at parse-time whether use
308 * of @_ should refer to a lexical (from a sub) or defgv (global
309 * scope and maybe weird sub-ish things like formats). See
310 * startsub in perly.y. It's possible that @_ could be lexical
311 * (at least from subs) even in non-threaded perl.
313 if (strEQ(name, "@_"))
314 return 0; /* success. (NOT_IN_PAD indicates failure) */
315 #endif /* USE_THREADS */
317 /* The one we're looking for is probably just before comppad_name_fill. */
318 for (off = AvFILL(comppad_name); off > 0; off--) {
319 if ((sv = svp[off]) &&
323 seq > I_32(SvNVX(sv)))) &&
324 strEQ(SvPVX(sv), name))
327 return (PADOFFSET)off;
328 pendoff = off; /* this pending def. will override import */
332 /* See if it's in a nested scope */
333 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
335 /* If there is a pending local definition, this new alias must die */
337 SvIVX(AvARRAY(comppad_name)[off]) = seq;
338 return off; /* pad_findlex returns 0 for failure...*/
340 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
344 pad_leavemy(I32 fill)
347 SV **svp = AvARRAY(comppad_name);
349 if (min_intro_pending && fill < min_intro_pending) {
350 for (off = max_intro_pending; off >= min_intro_pending; off--) {
351 if ((sv = svp[off]) && sv != &sv_undef)
352 warn("%s never introduced", SvPVX(sv));
355 /* "Deintroduce" my variables that are leaving with this scope. */
356 for (off = AvFILL(comppad_name); off > fill; off--) {
357 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
358 SvIVX(sv) = cop_seqmax;
363 pad_alloc(I32 optype, U32 tmptype)
369 if (AvARRAY(comppad) != curpad)
370 croak("panic: pad_alloc");
371 if (pad_reset_pending)
373 if (tmptype & SVs_PADMY) {
375 sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
376 } while (SvPADBUSY(sv)); /* need a fresh one */
377 retval = AvFILL(comppad);
380 SV **names = AvARRAY(comppad_name);
381 SSize_t names_fill = AvFILL(comppad_name);
384 * "foreach" index vars temporarily become aliases to non-"my"
385 * values. Thus we must skip, not just pad values that are
386 * marked as current pad values, but also those with names.
388 if (++padix <= names_fill &&
389 (sv = names[padix]) && sv != &sv_undef)
391 sv = *av_fetch(comppad, padix, TRUE);
392 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
397 SvFLAGS(sv) |= tmptype;
398 curpad = AvARRAY(comppad);
400 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
401 (unsigned long) thr, (unsigned long) curpad,
402 (long) retval, op_name[optype]));
404 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad alloc %ld for %s\n",
405 (long) retval, op_name[optype]));
406 #endif /* USE_THREADS */
407 return (PADOFFSET)retval;
411 #ifndef CAN_PROTOTYPE
416 #endif /* CAN_PROTOTYPE */
420 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
421 (unsigned long) thr, (unsigned long) curpad, po));
424 croak("panic: pad_sv po");
425 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad sv %d\n", po));
426 #endif /* USE_THREADS */
427 return curpad[po]; /* eventually we'll turn this into a macro */
431 #ifndef CAN_PROTOTYPE
435 pad_free(PADOFFSET po)
436 #endif /* CAN_PROTOTYPE */
441 if (AvARRAY(comppad) != curpad)
442 croak("panic: pad_free curpad");
444 croak("panic: pad_free po");
446 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
447 (unsigned long) thr, (unsigned long) curpad, po));
449 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad free %d\n", po));
450 #endif /* USE_THREADS */
451 if (curpad[po] && curpad[po] != &sv_undef)
452 SvPADTMP_off(curpad[po]);
458 #ifndef CAN_PROTOTYPE
462 pad_swipe(PADOFFSET po)
463 #endif /* CAN_PROTOTYPE */
466 if (AvARRAY(comppad) != curpad)
467 croak("panic: pad_swipe curpad");
469 croak("panic: pad_swipe po");
471 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
472 (unsigned long) thr, (unsigned long) curpad, po));
474 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad swipe %d\n", po));
475 #endif /* USE_THREADS */
476 SvPADTMP_off(curpad[po]);
477 curpad[po] = NEWSV(1107,0);
478 SvPADTMP_on(curpad[po]);
489 if (AvARRAY(comppad) != curpad)
490 croak("panic: pad_reset curpad");
492 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
493 (unsigned long) thr, (unsigned long) curpad));
495 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad reset\n"));
496 #endif /* USE_THREADS */
497 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
498 for (po = AvMAX(comppad); po > padix_floor; po--) {
499 if (curpad[po] && !SvIMMORTAL(curpad[po]))
500 SvPADTMP_off(curpad[po]);
504 pad_reset_pending = FALSE;
508 /* find_threadsv is not reentrant */
510 find_threadsv(char *name)
516 /* We currently only handle names of a single character */
517 p = strchr(threadsv_names, *name);
520 key = p - threadsv_names;
521 svp = av_fetch(thr->threadsv, key, FALSE);
523 SV *sv = NEWSV(0, 0);
524 av_store(thr->threadsv, key, sv);
526 * Some magic variables used to be automagically initialised
527 * in gv_fetchpv. Those which are now per-thread magicals get
528 * initialised here instead.
534 sv_setpv(sv, "\034");
535 sv_magic(sv, 0, 0, name, 1);
544 sv_magic(sv, 0, 0, name, 1);
546 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
547 "find_threadsv: new SV %p for $%s%c\n",
548 sv, (*name < 32) ? "^" : "",
549 (*name < 32) ? toCTRL(*name) : *name));
553 #endif /* USE_THREADS */
560 register OP *kid, *nextkid;
562 if (!o || o->op_seq == (U16)-1)
565 if (o->op_flags & OPf_KIDS) {
566 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
567 nextkid = kid->op_sibling; /* Get before next freeing kid */
572 switch (o->op_type) {
574 o->op_targ = 0; /* Was holding old type, if any. */
577 o->op_targ = 0; /* Was holding hints. */
581 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
583 #endif /* USE_THREADS */
585 if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst))
591 SvREFCNT_dec(cGVOPo->op_gv);
595 Safefree(cCOPo->cop_label);
596 SvREFCNT_dec(cCOPo->cop_filegv);
599 SvREFCNT_dec(cSVOPo->op_sv);
605 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
609 Safefree(cPVOPo->op_pv);
612 op_free(cPMOPo->op_pmreplroot);
616 ReREFCNT_dec(cPMOPo->op_pmregexp);
621 pad_free(o->op_targ);
629 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
630 pad_free(o->op_targ);
631 o->op_targ = o->op_type;
632 o->op_type = OP_NULL;
633 o->op_ppaddr = ppaddr[OP_NULL];
636 /* Contextualizers */
638 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
648 /* establish postfix order */
649 if (cUNOPo->op_first) {
650 o->op_next = LINKLIST(cUNOPo->op_first);
651 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
653 kid->op_next = LINKLIST(kid->op_sibling);
668 if (o && o->op_flags & OPf_KIDS) {
669 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
679 o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
681 line_t oldline = curcop->cop_line;
683 if (copline != NOLINE)
684 curcop->cop_line = copline;
685 warn("Found = in conditional, should be ==");
686 curcop->cop_line = oldline;
696 /* assumes no premature commitment */
697 if (!o || (o->op_flags & OPf_WANT) || error_count
698 || o->op_type == OP_RETURN)
701 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
703 switch (o->op_type) {
705 if (o->op_private & OPpREPEAT_DOLIST)
706 null(((LISTOP*)cBINOPo->op_first)->op_first);
707 scalar(cBINOPo->op_first);
712 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
716 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
717 if (!kPMOP->op_pmreplroot)
718 deprecate("implicit split to @_");
725 if (o->op_flags & OPf_KIDS) {
726 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
732 kid = cLISTOPo->op_first;
734 while (kid = kid->op_sibling) {
740 WITH_THR(curcop = &compiling);
745 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
751 WITH_THR(curcop = &compiling);
764 /* assumes no premature commitment */
765 if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count
766 || o->op_type == OP_RETURN)
769 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
771 switch (o->op_type) {
773 if (!(opargs[o->op_type] & OA_FOLDCONST))
777 if (o->op_flags & OPf_STACKED)
848 if (!(o->op_private & OPpLVAL_INTRO))
849 useless = op_desc[o->op_type];
856 if (!(o->op_private & OPpLVAL_INTRO) &&
857 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
858 useless = "a variable";
863 WITH_THR(curcop = ((COP*)o)); /* for warning below */
869 useless = "a constant";
870 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
872 else if (SvPOK(sv)) {
873 if (strnEQ(SvPVX(sv), "di", 2) ||
874 strnEQ(SvPVX(sv), "ds", 2) ||
875 strnEQ(SvPVX(sv), "ig", 2))
879 null(o); /* don't execute a constant */
880 SvREFCNT_dec(sv); /* don't even remember it */
884 o->op_type = OP_PREINC; /* pre-increment is faster */
885 o->op_ppaddr = ppaddr[OP_PREINC];
889 o->op_type = OP_PREDEC; /* pre-decrement is faster */
890 o->op_ppaddr = ppaddr[OP_PREDEC];
896 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
901 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
902 WITH_THR(curcop = ((COP*)o)); /* for warning below */
903 if (o->op_flags & OPf_STACKED)
909 if (!(o->op_flags & OPf_KIDS))
918 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
925 /* all requires must return a boolean value */
926 o->op_flags &= ~OPf_WANT;
929 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
930 if (!kPMOP->op_pmreplroot)
931 deprecate("implicit split to @_");
935 if (useless && dowarn)
936 warn("Useless use of %s in void context", useless);
944 if (o && o->op_flags & OPf_KIDS) {
945 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
956 /* assumes no premature commitment */
957 if (!o || (o->op_flags & OPf_WANT) || error_count
958 || o->op_type == OP_RETURN)
961 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
963 switch (o->op_type) {
966 list(cBINOPo->op_first);
971 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
978 if (!(o->op_flags & OPf_KIDS))
980 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
981 list(cBINOPo->op_first);
982 return gen_constant_list(o);
989 kid = cLISTOPo->op_first;
991 while (kid = kid->op_sibling) {
997 WITH_THR(curcop = &compiling);
1001 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1002 if (kid->op_sibling)
1007 WITH_THR(curcop = &compiling);
1010 /* all requires must return a boolean value */
1011 o->op_flags &= ~OPf_WANT;
1023 if (o->op_type == OP_LINESEQ ||
1024 o->op_type == OP_SCOPE ||
1025 o->op_type == OP_LEAVE ||
1026 o->op_type == OP_LEAVETRY)
1029 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1030 if (kid->op_sibling) {
1034 curcop = &compiling;
1036 o->op_flags &= ~OPf_PARENS;
1037 if (hints & HINT_BLOCK_SCOPE)
1038 o->op_flags |= OPf_PARENS;
1041 o = newOP(OP_STUB, 0);
1046 modkids(OP *o, I32 type)
1049 if (o && o->op_flags & OPf_KIDS) {
1050 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1056 static I32 modcount;
1059 mod(OP *o, I32 type)
1065 if (!o || error_count)
1068 switch (o->op_type) {
1073 if (!(o->op_private & (OPpCONST_ARYBASE)))
1075 if (eval_start && eval_start->op_type == OP_CONST) {
1076 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
1080 SAVEI32(compiling.cop_arybase);
1081 compiling.cop_arybase = 0;
1083 else if (type == OP_REFGEN)
1086 croak("That use of $[ is unsupported");
1089 if (o->op_flags & OPf_PARENS)
1093 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1094 !(o->op_flags & OPf_STACKED)) {
1095 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1096 o->op_ppaddr = ppaddr[OP_RV2CV];
1097 assert(cUNOPo->op_first->op_type == OP_NULL);
1098 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1104 /* grep, foreach, subcalls, refgen */
1105 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1107 yyerror(form("Can't modify %s in %s",
1108 op_desc[o->op_type],
1109 type ? op_desc[type] : "local"));
1123 case OP_RIGHT_SHIFT:
1132 if (!(o->op_flags & OPf_STACKED))
1138 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1144 if (!type && cUNOPo->op_first->op_type != OP_GV)
1145 croak("Can't localize through a reference");
1146 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1148 return o; /* Treat \(@foo) like ordinary list. */
1152 if (scalar_mod_type(o, type))
1154 ref(cUNOPo->op_first, o->op_type);
1166 if (!type && cUNOPo->op_first->op_type != OP_GV)
1167 croak("Can't localize through a reference");
1168 ref(cUNOPo->op_first, o->op_type);
1180 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1181 return o; /* Treat \(@foo) like ordinary list. */
1182 if (scalar_mod_type(o, type))
1188 croak("Can't localize lexical variable %s",
1189 SvPV(*av_fetch(comppad_name, o->op_targ, 4), na));
1194 modcount++; /* XXX ??? */
1196 #endif /* USE_THREADS */
1202 if (type != OP_SASSIGN)
1208 pad_free(o->op_targ);
1209 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1210 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1211 if (o->op_flags & OPf_KIDS)
1212 mod(cBINOPo->op_first->op_sibling, type);
1217 ref(cBINOPo->op_first, o->op_type);
1218 if (type == OP_ENTERSUB &&
1219 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1220 o->op_private |= OPpLVAL_DEFER;
1227 if (o->op_flags & OPf_KIDS)
1228 mod(cLISTOPo->op_last, type);
1232 if (!(o->op_flags & OPf_KIDS))
1234 if (o->op_targ != OP_LIST) {
1235 mod(cBINOPo->op_first, type);
1240 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1244 o->op_flags |= OPf_MOD;
1246 if (type == OP_AASSIGN || type == OP_SASSIGN)
1247 o->op_flags |= OPf_SPECIAL|OPf_REF;
1249 o->op_private |= OPpLVAL_INTRO;
1250 o->op_flags &= ~OPf_SPECIAL;
1252 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1253 o->op_flags |= OPf_REF;
1258 scalar_mod_type(OP *o, I32 type)
1262 if (o->op_type == OP_RV2GV)
1286 case OP_RIGHT_SHIFT:
1293 case OP_ANDASSIGN: /* may work later */
1294 case OP_ORASSIGN: /* may work later */
1302 refkids(OP *o, I32 type)
1305 if (o && o->op_flags & OPf_KIDS) {
1306 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1313 ref(OP *o, I32 type)
1317 if (!o || error_count)
1320 switch (o->op_type) {
1322 if ((type == OP_DEFINED || type == OP_LOCK) &&
1323 !(o->op_flags & OPf_STACKED)) {
1324 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1325 o->op_ppaddr = ppaddr[OP_RV2CV];
1326 assert(cUNOPo->op_first->op_type == OP_NULL);
1327 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1328 o->op_flags |= OPf_SPECIAL;
1333 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1337 ref(cUNOPo->op_first, o->op_type);
1340 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1341 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1342 : type == OP_RV2HV ? OPpDEREF_HV
1344 o->op_flags |= OPf_MOD;
1349 o->op_flags |= OPf_MOD; /* XXX ??? */
1354 o->op_flags |= OPf_REF;
1357 ref(cUNOPo->op_first, o->op_type);
1362 o->op_flags |= OPf_REF;
1367 if (!(o->op_flags & OPf_KIDS))
1369 ref(cBINOPo->op_first, type);
1373 ref(cBINOPo->op_first, o->op_type);
1374 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1375 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1376 : type == OP_RV2HV ? OPpDEREF_HV
1378 o->op_flags |= OPf_MOD;
1386 if (!(o->op_flags & OPf_KIDS))
1388 ref(cLISTOPo->op_last, type);
1403 if (!o || error_count)
1407 if (type == OP_LIST) {
1408 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1411 else if (type != OP_PADSV &&
1414 type != OP_PUSHMARK)
1416 yyerror(form("Can't declare %s in my", op_desc[o->op_type]));
1419 o->op_flags |= OPf_MOD;
1420 o->op_private |= OPpLVAL_INTRO;
1428 o->op_flags |= OPf_PARENS;
1433 bind_match(I32 type, OP *left, OP *right)
1438 (left->op_type == OP_RV2AV ||
1439 left->op_type == OP_RV2HV ||
1440 left->op_type == OP_PADAV ||
1441 left->op_type == OP_PADHV)) {
1442 char *desc = op_desc[(right->op_type == OP_SUBST ||
1443 right->op_type == OP_TRANS)
1444 ? right->op_type : OP_MATCH];
1445 char *sample = ((left->op_type == OP_RV2AV ||
1446 left->op_type == OP_PADAV)
1447 ? "@array" : "%hash");
1448 warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample);
1451 if (right->op_type == OP_MATCH ||
1452 right->op_type == OP_SUBST ||
1453 right->op_type == OP_TRANS) {
1454 right->op_flags |= OPf_STACKED;
1455 if (right->op_type != OP_MATCH)
1456 left = mod(left, right->op_type);
1457 if (right->op_type == OP_TRANS)
1458 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1460 o = prepend_elem(right->op_type, scalar(left), right);
1462 return newUNOP(OP_NOT, 0, scalar(o));
1466 return bind_match(type, left,
1467 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1475 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1476 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1483 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) {
1484 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1485 o->op_type = OP_LEAVE;
1486 o->op_ppaddr = ppaddr[OP_LEAVE];
1489 if (o->op_type == OP_LINESEQ) {
1491 o->op_type = OP_SCOPE;
1492 o->op_ppaddr = ppaddr[OP_SCOPE];
1493 kid = ((LISTOP*)o)->op_first;
1494 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1495 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1500 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1507 block_start(int full)
1510 int retval = savestack_ix;
1511 SAVEI32(comppad_name_floor);
1513 if ((comppad_name_fill = AvFILL(comppad_name)) > 0)
1514 comppad_name_floor = comppad_name_fill;
1516 comppad_name_floor = 0;
1518 SAVEI32(min_intro_pending);
1519 SAVEI32(max_intro_pending);
1520 min_intro_pending = 0;
1521 SAVEI32(comppad_name_fill);
1522 SAVEI32(padix_floor);
1523 padix_floor = padix;
1524 pad_reset_pending = FALSE;
1526 hints &= ~HINT_BLOCK_SCOPE;
1531 block_end(I32 floor, OP *seq)
1534 int needblockscope = hints & HINT_BLOCK_SCOPE;
1535 OP* retval = scalarseq(seq);
1537 pad_reset_pending = FALSE;
1539 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1540 pad_leavemy(comppad_name_fill);
1549 OP *o = newOP(OP_THREADSV, 0);
1550 o->op_targ = find_threadsv("_");
1553 return newSVREF(newGVOP(OP_GV, 0, defgv));
1554 #endif /* USE_THREADS */
1562 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o);
1563 eval_start = linklist(eval_root);
1564 eval_root->op_next = 0;
1570 main_root = scope(sawparens(scalarvoid(o)));
1571 curcop = &compiling;
1572 main_start = LINKLIST(main_root);
1573 main_root->op_next = 0;
1577 /* Register with debugger */
1579 CV *cv = perl_get_cv("DB::postponed", FALSE);
1583 XPUSHs((SV*)compiling.cop_filegv);
1585 perl_call_sv((SV*)cv, G_DISCARD);
1592 localize(OP *o, I32 lex)
1594 if (o->op_flags & OPf_PARENS)
1598 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1600 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1601 if (*s == ';' || *s == '=')
1602 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1606 in_my_stash = Nullhv;
1610 return mod(o, OP_NULL); /* a bit kludgey */
1616 if (o->op_type == OP_LIST) {
1619 o2 = newOP(OP_THREADSV, 0);
1620 o2->op_targ = find_threadsv(";");
1622 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1623 #endif /* USE_THREADS */
1624 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1630 fold_constants(register OP *o)
1634 I32 type = o->op_type;
1637 if (opargs[type] & OA_RETSCALAR)
1639 if (opargs[type] & OA_TARGET)
1640 o->op_targ = pad_alloc(type, SVs_PADTMP);
1642 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1643 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1645 if (!(opargs[type] & OA_FOLDCONST))
1654 if (o->op_private & OPpLOCALE)
1659 goto nope; /* Don't try to run w/ errors */
1661 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1662 if (curop->op_type != OP_CONST &&
1663 curop->op_type != OP_LIST &&
1664 curop->op_type != OP_SCALAR &&
1665 curop->op_type != OP_NULL &&
1666 curop->op_type != OP_PUSHMARK) {
1671 curop = LINKLIST(o);
1676 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1677 pad_swipe(o->op_targ);
1678 else if (SvTEMP(sv)) { /* grab mortal temp? */
1679 (void)SvREFCNT_inc(sv);
1683 if (type == OP_RV2GV)
1684 return newGVOP(OP_GV, 0, (GV*)sv);
1686 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1688 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1693 SvIOK_off(sv); /* undo SvIV() damage */
1695 return newSVOP(OP_CONST, 0, sv);
1699 if (!(opargs[type] & OA_OTHERINT))
1702 if (!(hints & HINT_INTEGER)) {
1703 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1706 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1707 if (curop->op_type == OP_CONST) {
1708 if (SvIOK(((SVOP*)curop)->op_sv))
1712 if (opargs[curop->op_type] & OA_RETINTEGER)
1716 o->op_ppaddr = ppaddr[++(o->op_type)];
1723 gen_constant_list(register OP *o)
1727 I32 oldtmps_floor = tmps_floor;
1731 return o; /* Don't attempt to run with errors */
1733 op = curop = LINKLIST(o);
1739 tmps_floor = oldtmps_floor;
1741 o->op_type = OP_RV2AV;
1742 o->op_ppaddr = ppaddr[OP_RV2AV];
1743 curop = ((UNOP*)o)->op_first;
1744 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1751 convert(I32 type, I32 flags, OP *o)
1756 if (!o || o->op_type != OP_LIST)
1757 o = newLISTOP(OP_LIST, 0, o, Nullop);
1759 o->op_flags &= ~OPf_WANT;
1761 if (!(opargs[type] & OA_MARK))
1762 null(cLISTOPo->op_first);
1765 o->op_ppaddr = ppaddr[type];
1766 o->op_flags |= flags;
1768 o = CHECKOP(type, o);
1769 if (o->op_type != type)
1772 if (cLISTOPo->op_children < 7) {
1773 /* XXX do we really need to do this if we're done appending?? */
1774 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1776 cLISTOPo->op_last = last; /* in case check substituted last arg */
1779 return fold_constants(o);
1782 /* List constructors */
1785 append_elem(I32 type, OP *first, OP *last)
1793 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1794 return newLISTOP(type, 0, first, last);
1796 if (first->op_flags & OPf_KIDS)
1797 ((LISTOP*)first)->op_last->op_sibling = last;
1799 first->op_flags |= OPf_KIDS;
1800 ((LISTOP*)first)->op_first = last;
1802 ((LISTOP*)first)->op_last = last;
1803 ((LISTOP*)first)->op_children++;
1808 append_list(I32 type, LISTOP *first, LISTOP *last)
1816 if (first->op_type != type)
1817 return prepend_elem(type, (OP*)first, (OP*)last);
1819 if (last->op_type != type)
1820 return append_elem(type, (OP*)first, (OP*)last);
1822 first->op_last->op_sibling = last->op_first;
1823 first->op_last = last->op_last;
1824 first->op_children += last->op_children;
1825 if (first->op_children)
1826 last->op_flags |= OPf_KIDS;
1833 prepend_elem(I32 type, OP *first, OP *last)
1841 if (last->op_type == type) {
1842 if (type == OP_LIST) { /* already a PUSHMARK there */
1843 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1844 ((LISTOP*)last)->op_first->op_sibling = first;
1847 if (!(last->op_flags & OPf_KIDS)) {
1848 ((LISTOP*)last)->op_last = first;
1849 last->op_flags |= OPf_KIDS;
1851 first->op_sibling = ((LISTOP*)last)->op_first;
1852 ((LISTOP*)last)->op_first = first;
1854 ((LISTOP*)last)->op_children++;
1858 return newLISTOP(type, 0, first, last);
1866 return newOP(OP_STUB, 0);
1872 if (!o || o->op_type != OP_LIST)
1873 o = newLISTOP(OP_LIST, 0, o, Nullop);
1879 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
1883 Newz(1101, listop, 1, LISTOP);
1885 listop->op_type = type;
1886 listop->op_ppaddr = ppaddr[type];
1887 listop->op_children = (first != 0) + (last != 0);
1888 listop->op_flags = flags;
1892 else if (!first && last)
1895 first->op_sibling = last;
1896 listop->op_first = first;
1897 listop->op_last = last;
1898 if (type == OP_LIST) {
1900 pushop = newOP(OP_PUSHMARK, 0);
1901 pushop->op_sibling = first;
1902 listop->op_first = pushop;
1903 listop->op_flags |= OPf_KIDS;
1905 listop->op_last = pushop;
1907 else if (listop->op_children)
1908 listop->op_flags |= OPf_KIDS;
1914 newOP(I32 type, I32 flags)
1917 Newz(1101, o, 1, OP);
1919 o->op_ppaddr = ppaddr[type];
1920 o->op_flags = flags;
1923 o->op_private = 0 + (flags >> 8);
1924 if (opargs[type] & OA_RETSCALAR)
1926 if (opargs[type] & OA_TARGET)
1927 o->op_targ = pad_alloc(type, SVs_PADTMP);
1928 return CHECKOP(type, o);
1932 newUNOP(I32 type, I32 flags, OP *first)
1937 first = newOP(OP_STUB, 0);
1938 if (opargs[type] & OA_MARK)
1939 first = force_list(first);
1941 Newz(1101, unop, 1, UNOP);
1942 unop->op_type = type;
1943 unop->op_ppaddr = ppaddr[type];
1944 unop->op_first = first;
1945 unop->op_flags = flags | OPf_KIDS;
1946 unop->op_private = 1 | (flags >> 8);
1948 if(type == OP_STUDY && first->op_type == OP_MATCH) {
1949 first->op_type = OP_PUSHRE;
1950 first->op_ppaddr = ppaddr[OP_PUSHRE];
1953 unop = (UNOP*) CHECKOP(type, unop);
1957 return fold_constants((OP *) unop);
1961 newBINOP(I32 type, I32 flags, OP *first, OP *last)
1964 Newz(1101, binop, 1, BINOP);
1967 first = newOP(OP_NULL, 0);
1969 binop->op_type = type;
1970 binop->op_ppaddr = ppaddr[type];
1971 binop->op_first = first;
1972 binop->op_flags = flags | OPf_KIDS;
1975 binop->op_private = 1 | (flags >> 8);
1978 binop->op_private = 2 | (flags >> 8);
1979 first->op_sibling = last;
1982 binop = (BINOP*)CHECKOP(type, binop);
1986 binop->op_last = last = binop->op_first->op_sibling;
1988 return fold_constants((OP *)binop);
1992 pmtrans(OP *o, OP *expr, OP *repl)
1994 SV *tstr = ((SVOP*)expr)->op_sv;
1995 SV *rstr = ((SVOP*)repl)->op_sv;
1998 register U8 *t = (U8*)SvPV(tstr, tlen);
1999 register U8 *r = (U8*)SvPV(rstr, rlen);
2005 register short *tbl;
2007 tbl = (short*)cPVOPo->op_pv;
2008 complement = o->op_private & OPpTRANS_COMPLEMENT;
2009 Delete = o->op_private & OPpTRANS_DELETE;
2010 squash = o->op_private & OPpTRANS_SQUASH;
2013 Zero(tbl, 256, short);
2014 for (i = 0; i < tlen; i++)
2016 for (i = 0, j = 0; i < 256; i++) {
2032 if (!rlen && !Delete) {
2035 o->op_private |= OPpTRANS_COUNTONLY;
2037 for (i = 0; i < 256; i++)
2039 for (i = 0, j = 0; i < tlen; i++,j++) {
2042 if (tbl[t[i]] == -1)
2048 if (tbl[t[i]] == -1)
2059 newPMOP(I32 type, I32 flags)
2064 Newz(1101, pmop, 1, PMOP);
2065 pmop->op_type = type;
2066 pmop->op_ppaddr = ppaddr[type];
2067 pmop->op_flags = flags;
2068 pmop->op_private = 0 | (flags >> 8);
2070 if (hints & HINT_LOCALE)
2071 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2073 /* link into pm list */
2074 if (type != OP_TRANS && curstash) {
2075 pmop->op_pmnext = HvPMROOT(curstash);
2076 HvPMROOT(curstash) = pmop;
2083 pmruntime(OP *o, OP *expr, OP *repl)
2088 if (o->op_type == OP_TRANS)
2089 return pmtrans(o, expr, repl);
2091 hints |= HINT_BLOCK_SCOPE;
2094 if (expr->op_type == OP_CONST) {
2096 SV *pat = ((SVOP*)expr)->op_sv;
2097 char *p = SvPV(pat, plen);
2098 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2099 sv_setpvn(pat, "\\s+", 3);
2100 p = SvPV(pat, plen);
2101 pm->op_pmflags |= PMf_SKIPWHITE;
2103 pm->op_pmregexp = pregcomp(p, p + plen, pm);
2104 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2105 pm->op_pmflags |= PMf_WHITE;
2109 if (pm->op_pmflags & PMf_KEEP)
2110 expr = newUNOP(OP_REGCMAYBE,0,expr);
2112 Newz(1101, rcop, 1, LOGOP);
2113 rcop->op_type = OP_REGCOMP;
2114 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2115 rcop->op_first = scalar(expr);
2116 rcop->op_flags |= OPf_KIDS;
2117 rcop->op_private = 1;
2120 /* establish postfix order */
2121 if (pm->op_pmflags & PMf_KEEP) {
2123 rcop->op_next = expr;
2124 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2127 rcop->op_next = LINKLIST(expr);
2128 expr->op_next = (OP*)rcop;
2131 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2136 if (pm->op_pmflags & PMf_EVAL)
2139 else if (repl->op_type == OP_THREADSV
2140 && strchr("&`'123456789+",
2141 threadsv_names[repl->op_targ]))
2145 #endif /* USE_THREADS */
2146 else if (repl->op_type == OP_CONST)
2150 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2151 if (opargs[curop->op_type] & OA_DANGEROUS) {
2153 if (curop->op_type == OP_THREADSV
2154 && strchr("&`'123456789+", curop->op_private)) {
2158 if (curop->op_type == OP_GV) {
2159 GV *gv = ((GVOP*)curop)->op_gv;
2160 if (strchr("&`'123456789+", *GvENAME(gv)))
2163 #endif /* USE_THREADS */
2164 else if (curop->op_type == OP_RV2CV)
2166 else if (curop->op_type == OP_RV2SV ||
2167 curop->op_type == OP_RV2AV ||
2168 curop->op_type == OP_RV2HV ||
2169 curop->op_type == OP_RV2GV) {
2170 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2173 else if (curop->op_type == OP_PADSV ||
2174 curop->op_type == OP_PADAV ||
2175 curop->op_type == OP_PADHV ||
2176 curop->op_type == OP_PADANY) {
2185 if (curop == repl) {
2186 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2187 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2188 prepend_elem(o->op_type, scalar(repl), o);
2191 Newz(1101, rcop, 1, LOGOP);
2192 rcop->op_type = OP_SUBSTCONT;
2193 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2194 rcop->op_first = scalar(repl);
2195 rcop->op_flags |= OPf_KIDS;
2196 rcop->op_private = 1;
2199 /* establish postfix order */
2200 rcop->op_next = LINKLIST(repl);
2201 repl->op_next = (OP*)rcop;
2203 pm->op_pmreplroot = scalar((OP*)rcop);
2204 pm->op_pmreplstart = LINKLIST(rcop);
2213 newSVOP(I32 type, I32 flags, SV *sv)
2216 Newz(1101, svop, 1, SVOP);
2217 svop->op_type = type;
2218 svop->op_ppaddr = ppaddr[type];
2220 svop->op_next = (OP*)svop;
2221 svop->op_flags = flags;
2222 if (opargs[type] & OA_RETSCALAR)
2224 if (opargs[type] & OA_TARGET)
2225 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2226 return CHECKOP(type, svop);
2230 newGVOP(I32 type, I32 flags, GV *gv)
2234 Newz(1101, gvop, 1, GVOP);
2235 gvop->op_type = type;
2236 gvop->op_ppaddr = ppaddr[type];
2237 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2238 gvop->op_next = (OP*)gvop;
2239 gvop->op_flags = flags;
2240 if (opargs[type] & OA_RETSCALAR)
2242 if (opargs[type] & OA_TARGET)
2243 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2244 return CHECKOP(type, gvop);
2248 newPVOP(I32 type, I32 flags, char *pv)
2251 Newz(1101, pvop, 1, PVOP);
2252 pvop->op_type = type;
2253 pvop->op_ppaddr = ppaddr[type];
2255 pvop->op_next = (OP*)pvop;
2256 pvop->op_flags = flags;
2257 if (opargs[type] & OA_RETSCALAR)
2259 if (opargs[type] & OA_TARGET)
2260 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2261 return CHECKOP(type, pvop);
2270 save_hptr(&curstash);
2271 save_item(curstname);
2276 name = SvPV(sv, len);
2277 curstash = gv_stashpvn(name,len,TRUE);
2278 sv_setpvn(curstname, name, len);
2282 sv_setpv(curstname,"<none>");
2290 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2298 if (id->op_type != OP_CONST)
2299 croak("Module name must be constant");
2303 if(version != Nullop) {
2304 SV *vesv = ((SVOP*)version)->op_sv;
2306 if (arg == Nullop && !SvNIOK(vesv)) {
2313 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2314 croak("Version number must be constant number");
2316 /* Make copy of id so we don't free it twice */
2317 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2319 /* Fake up a method call to VERSION */
2320 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2321 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2322 append_elem(OP_LIST,
2323 prepend_elem(OP_LIST, pack, list(version)),
2324 newUNOP(OP_METHOD, 0, meth)));
2328 /* Fake up an import/unimport */
2329 if (arg && arg->op_type == OP_STUB)
2330 imop = arg; /* no import on explicit () */
2331 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2332 imop = Nullop; /* use 5.0; */
2335 /* Make copy of id so we don't free it twice */
2336 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2337 meth = newSVOP(OP_CONST, 0,
2339 ? newSVpv("import", 6)
2340 : newSVpv("unimport", 8)
2342 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2343 append_elem(OP_LIST,
2344 prepend_elem(OP_LIST, pack, list(arg)),
2345 newUNOP(OP_METHOD, 0, meth)));
2348 /* Fake up a require */
2349 rqop = newUNOP(OP_REQUIRE, 0, id);
2351 /* Fake up the BEGIN {}, which does its thing immediately. */
2353 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2355 append_elem(OP_LINESEQ,
2356 append_elem(OP_LINESEQ,
2357 newSTATEOP(0, Nullch, rqop),
2358 newSTATEOP(0, Nullch, veop)),
2359 newSTATEOP(0, Nullch, imop) ));
2366 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2368 return newBINOP(OP_LSLICE, flags,
2369 list(force_list(subscript)),
2370 list(force_list(listval)) );
2374 list_assignment(register OP *o)
2379 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2380 o = cUNOPo->op_first;
2382 if (o->op_type == OP_COND_EXPR) {
2383 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2384 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2389 yyerror("Assignment to both a list and a scalar");
2393 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2394 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2395 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2398 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2401 if (o->op_type == OP_RV2SV)
2408 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2413 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2414 return newLOGOP(optype, 0,
2415 mod(scalar(left), optype),
2416 newUNOP(OP_SASSIGN, 0, scalar(right)));
2419 return newBINOP(optype, OPf_STACKED,
2420 mod(scalar(left), optype), scalar(right));
2424 if (list_assignment(left)) {
2426 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2427 left = mod(left, OP_AASSIGN);
2435 o = newBINOP(OP_AASSIGN, flags,
2436 list(force_list(right)),
2437 list(force_list(left)) );
2438 o->op_private = 0 | (flags >> 8);
2439 if (!(left->op_private & OPpLVAL_INTRO)) {
2440 static int generation = 100;
2444 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2445 if (opargs[curop->op_type] & OA_DANGEROUS) {
2446 if (curop->op_type == OP_GV) {
2447 GV *gv = ((GVOP*)curop)->op_gv;
2448 if (gv == defgv || SvCUR(gv) == generation)
2450 SvCUR(gv) = generation;
2452 else if (curop->op_type == OP_PADSV ||
2453 curop->op_type == OP_PADAV ||
2454 curop->op_type == OP_PADHV ||
2455 curop->op_type == OP_PADANY) {
2456 SV **svp = AvARRAY(comppad_name);
2457 SV *sv = svp[curop->op_targ];
2458 if (SvCUR(sv) == generation)
2460 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2462 else if (curop->op_type == OP_RV2CV)
2464 else if (curop->op_type == OP_RV2SV ||
2465 curop->op_type == OP_RV2AV ||
2466 curop->op_type == OP_RV2HV ||
2467 curop->op_type == OP_RV2GV) {
2468 if (lastop->op_type != OP_GV) /* funny deref? */
2477 o->op_private = OPpASSIGN_COMMON;
2479 if (right && right->op_type == OP_SPLIT) {
2481 if ((tmpop = ((LISTOP*)right)->op_first) &&
2482 tmpop->op_type == OP_PUSHRE)
2484 PMOP *pm = (PMOP*)tmpop;
2485 if (left->op_type == OP_RV2AV &&
2486 !(left->op_private & OPpLVAL_INTRO) &&
2487 !(o->op_private & OPpASSIGN_COMMON) )
2489 tmpop = ((UNOP*)left)->op_first;
2490 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2491 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2492 pm->op_pmflags |= PMf_ONCE;
2493 tmpop = cUNOPo->op_first; /* to list (nulled) */
2494 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2495 tmpop->op_sibling = Nullop; /* don't free split */
2496 right->op_next = tmpop->op_next; /* fix starting loc */
2497 op_free(o); /* blow off assign */
2498 right->op_flags &= ~OPf_WANT;
2499 /* "I don't know and I don't care." */
2504 if (modcount < 10000 &&
2505 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2507 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2509 sv_setiv(sv, modcount+1);
2517 right = newOP(OP_UNDEF, 0);
2518 if (right->op_type == OP_READLINE) {
2519 right->op_flags |= OPf_STACKED;
2520 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2523 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2524 o = newBINOP(OP_SASSIGN, flags,
2525 scalar(right), mod(scalar(left), OP_SASSIGN) );
2537 newSTATEOP(I32 flags, char *label, OP *o)
2540 U32 seq = intro_my();
2543 Newz(1101, cop, 1, COP);
2544 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2545 cop->op_type = OP_DBSTATE;
2546 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2549 cop->op_type = OP_NEXTSTATE;
2550 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2552 cop->op_flags = flags;
2553 cop->op_private = 0 | (flags >> 8);
2555 cop->op_private |= NATIVE_HINTS;
2557 cop->op_next = (OP*)cop;
2560 cop->cop_label = label;
2561 hints |= HINT_BLOCK_SCOPE;
2564 cop->cop_arybase = curcop->cop_arybase;
2566 if (copline == NOLINE)
2567 cop->cop_line = curcop->cop_line;
2569 cop->cop_line = copline;
2572 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2573 cop->cop_stash = curstash;
2575 if (PERLDB_LINE && curstash != debstash) {
2576 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2577 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2578 (void)SvIOK_on(*svp);
2580 SvSTASH(*svp) = (HV*)cop;
2584 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2587 /* "Introduce" my variables to visible status. */
2595 if (! min_intro_pending)
2598 svp = AvARRAY(comppad_name);
2599 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2600 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2601 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2602 SvNVX(sv) = (double)cop_seqmax;
2605 min_intro_pending = 0;
2606 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2607 return cop_seqmax++;
2611 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2617 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2618 return newBINOP(type, flags, scalar(first), scalar(other));
2620 scalarboolean(first);
2621 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2622 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2623 if (type == OP_AND || type == OP_OR) {
2629 first = cUNOPo->op_first;
2631 first->op_next = o->op_next;
2632 cUNOPo->op_first = Nullop;
2636 if (first->op_type == OP_CONST) {
2637 if (dowarn && (first->op_private & OPpCONST_BARE))
2638 warn("Probable precedence problem on %s", op_desc[type]);
2639 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2648 else if (first->op_type == OP_WANTARRAY) {
2654 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2655 OP *k1 = ((UNOP*)first)->op_first;
2656 OP *k2 = k1->op_sibling;
2658 switch (first->op_type)
2661 if (k2 && k2->op_type == OP_READLINE
2662 && (k2->op_flags & OPf_STACKED)
2663 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2664 warnop = k2->op_type;
2668 if (k1->op_type == OP_READDIR
2669 || k1->op_type == OP_GLOB
2670 || k1->op_type == OP_EACH)
2671 warnop = k1->op_type;
2675 line_t oldline = curcop->cop_line;
2676 curcop->cop_line = copline;
2677 warn("Value of %s%s can be \"0\"; test with defined()",
2679 ((warnop == OP_READLINE || warnop == OP_GLOB)
2680 ? " construct" : "() operator"));
2681 curcop->cop_line = oldline;
2688 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2689 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2691 Newz(1101, logop, 1, LOGOP);
2693 logop->op_type = type;
2694 logop->op_ppaddr = ppaddr[type];
2695 logop->op_first = first;
2696 logop->op_flags = flags | OPf_KIDS;
2697 logop->op_other = LINKLIST(other);
2698 logop->op_private = 1 | (flags >> 8);
2700 /* establish postfix order */
2701 logop->op_next = LINKLIST(first);
2702 first->op_next = (OP*)logop;
2703 first->op_sibling = other;
2705 o = newUNOP(OP_NULL, 0, (OP*)logop);
2712 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2719 return newLOGOP(OP_AND, 0, first, trueop);
2721 return newLOGOP(OP_OR, 0, first, falseop);
2723 scalarboolean(first);
2724 if (first->op_type == OP_CONST) {
2725 if (SvTRUE(((SVOP*)first)->op_sv)) {
2736 else if (first->op_type == OP_WANTARRAY) {
2740 Newz(1101, condop, 1, CONDOP);
2742 condop->op_type = OP_COND_EXPR;
2743 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2744 condop->op_first = first;
2745 condop->op_flags = flags | OPf_KIDS;
2746 condop->op_true = LINKLIST(trueop);
2747 condop->op_false = LINKLIST(falseop);
2748 condop->op_private = 1 | (flags >> 8);
2750 /* establish postfix order */
2751 condop->op_next = LINKLIST(first);
2752 first->op_next = (OP*)condop;
2754 first->op_sibling = trueop;
2755 trueop->op_sibling = falseop;
2756 o = newUNOP(OP_NULL, 0, (OP*)condop);
2758 trueop->op_next = o;
2759 falseop->op_next = o;
2765 newRANGE(I32 flags, OP *left, OP *right)
2773 Newz(1101, condop, 1, CONDOP);
2775 condop->op_type = OP_RANGE;
2776 condop->op_ppaddr = ppaddr[OP_RANGE];
2777 condop->op_first = left;
2778 condop->op_flags = OPf_KIDS;
2779 condop->op_true = LINKLIST(left);
2780 condop->op_false = LINKLIST(right);
2781 condop->op_private = 1 | (flags >> 8);
2783 left->op_sibling = right;
2785 condop->op_next = (OP*)condop;
2786 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2787 flop = newUNOP(OP_FLOP, 0, flip);
2788 o = newUNOP(OP_NULL, 0, flop);
2791 left->op_next = flip;
2792 right->op_next = flop;
2794 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2795 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2796 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2797 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2799 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2800 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2803 if (!flip->op_private || !flop->op_private)
2804 linklist(o); /* blow off optimizer unless constant */
2810 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2815 int once = block && block->op_flags & OPf_SPECIAL &&
2816 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2819 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2820 return block; /* do {} while 0 does once */
2821 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2822 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2823 expr = newUNOP(OP_DEFINED, 0,
2824 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2828 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2829 o = newLOGOP(OP_AND, 0, expr, listop);
2831 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2833 if (once && o != listop)
2834 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2837 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
2839 o->op_flags |= flags;
2841 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2846 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2855 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2856 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2857 expr = newUNOP(OP_DEFINED, 0,
2858 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2862 block = newOP(OP_NULL, 0);
2865 next = LINKLIST(cont);
2867 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2868 if ((line_t)whileline != NOLINE) {
2869 copline = whileline;
2870 cont = append_elem(OP_LINESEQ, cont,
2871 newSTATEOP(0, Nullch, Nullop));
2875 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2876 redo = LINKLIST(listop);
2879 o = newLOGOP(OP_AND, 0, expr, scalar(listop));
2880 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2881 op_free(expr); /* oops, it's a while (0) */
2883 return Nullop; /* (listop already freed by newLOGOP) */
2885 ((LISTOP*)listop)->op_last->op_next = condop =
2886 (o == listop ? redo : LINKLIST(o));
2894 Newz(1101,loop,1,LOOP);
2895 loop->op_type = OP_ENTERLOOP;
2896 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2897 loop->op_private = 0;
2898 loop->op_next = (OP*)loop;
2901 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
2903 loop->op_redoop = redo;
2904 loop->op_lastop = o;
2907 loop->op_nextop = next;
2909 loop->op_nextop = o;
2911 o->op_flags |= flags;
2912 o->op_private |= (flags >> 8);
2917 #ifndef CAN_PROTOTYPE
2918 newFOROP(flags,label,forline,sv,expr,block,cont)
2927 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2928 #endif /* CAN_PROTOTYPE */
2936 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2937 sv->op_type = OP_RV2GV;
2938 sv->op_ppaddr = ppaddr[OP_RV2GV];
2940 else if (sv->op_type == OP_PADSV) { /* private variable */
2941 padoff = sv->op_targ;
2945 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
2946 padoff = sv->op_targ;
2947 iterflags |= OPf_SPECIAL;
2952 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2956 padoff = find_threadsv("_");
2957 iterflags |= OPf_SPECIAL;
2959 sv = newGVOP(OP_GV, 0, defgv);
2962 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2963 expr = scalar(ref(expr, OP_ITER));
2964 iterflags |= OPf_STACKED;
2966 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2967 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2969 assert(!loop->op_next);
2970 Renew(loop, 1, LOOP);
2971 loop->op_targ = padoff;
2972 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
2974 return newSTATEOP(0, label, wop);
2978 newLOOPEX(I32 type, OP *label)
2982 if (type != OP_GOTO || label->op_type == OP_CONST) {
2983 o = newPVOP(type, 0, savepv(
2984 label->op_type == OP_CONST
2985 ? SvPVx(((SVOP*)label)->op_sv, na)
2990 if (label->op_type == OP_ENTERSUB)
2991 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2992 o = newUNOP(type, OPf_STACKED, label);
2994 hints |= HINT_BLOCK_SCOPE;
3004 MUTEX_DESTROY(CvMUTEXP(cv));
3005 Safefree(CvMUTEXP(cv));
3008 #endif /* USE_THREADS */
3010 if (!CvXSUB(cv) && CvROOT(cv)) {
3012 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3013 croak("Can't undef active subroutine");
3016 croak("Can't undef active subroutine");
3017 #endif /* USE_THREADS */
3024 op_free(CvROOT(cv));
3025 CvROOT(cv) = Nullop;
3028 SvPOK_off((SV*)cv); /* forget prototype */
3030 SvREFCNT_dec(CvGV(cv));
3032 SvREFCNT_dec(CvOUTSIDE(cv));
3033 CvOUTSIDE(cv) = Nullcv;
3034 if (CvPADLIST(cv)) {
3035 /* may be during global destruction */
3036 if (SvREFCNT(CvPADLIST(cv))) {
3037 I32 i = AvFILL(CvPADLIST(cv));
3039 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3040 SV* sv = svp ? *svp : Nullsv;
3043 if (sv == (SV*)comppad_name)
3044 comppad_name = Nullav;
3045 else if (sv == (SV*)comppad) {
3047 curpad = Null(SV**);
3051 SvREFCNT_dec((SV*)CvPADLIST(cv));
3053 CvPADLIST(cv) = Nullav;
3057 #ifdef DEBUG_CLOSURES
3062 CV *outside = CvOUTSIDE(cv);
3063 AV* padlist = CvPADLIST(cv);
3070 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3072 (CvANON(cv) ? "ANON"
3073 : (cv == main_cv) ? "MAIN"
3074 : CvUNIQUE(outside) ? "UNIQUE"
3075 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3078 : CvANON(outside) ? "ANON"
3079 : (outside == main_cv) ? "MAIN"
3080 : CvUNIQUE(outside) ? "UNIQUE"
3081 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3086 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3087 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3088 pname = AvARRAY(pad_name);
3089 ppad = AvARRAY(pad);
3091 for (ix = 1; ix <= AvFILL(pad_name); ix++) {
3092 if (SvPOK(pname[ix]))
3093 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3095 SvFAKE(pname[ix]) ? "FAKE " : "",
3097 (long)I_32(SvNVX(pname[ix])),
3098 (long)SvIVX(pname[ix]));
3101 #endif /* DEBUG_CLOSURES */
3104 cv_clone2(CV *proto, CV *outside)
3109 AV* protopadlist = CvPADLIST(proto);
3110 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3111 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3112 SV** pname = AvARRAY(protopad_name);
3113 SV** ppad = AvARRAY(protopad);
3114 I32 fname = AvFILL(protopad_name);
3115 I32 fpad = AvFILL(protopad);
3119 assert(!CvUNIQUE(proto));
3124 SAVESPTR(comppad_name);
3127 cv = compcv = (CV*)NEWSV(1104,0);
3128 sv_upgrade((SV *)cv, SvTYPE(proto));
3134 New(666, CvMUTEXP(cv), 1, perl_mutex);
3135 MUTEX_INIT(CvMUTEXP(cv));
3137 #endif /* USE_THREADS */
3138 CvFILEGV(cv) = CvFILEGV(proto);
3139 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3140 CvSTASH(cv) = CvSTASH(proto);
3141 CvROOT(cv) = CvROOT(proto);
3142 CvSTART(cv) = CvSTART(proto);
3144 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3147 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3149 comppad_name = newAV();
3150 for (ix = fname; ix >= 0; ix--)
3151 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3155 comppadlist = newAV();
3156 AvREAL_off(comppadlist);
3157 av_store(comppadlist, 0, (SV*)comppad_name);
3158 av_store(comppadlist, 1, (SV*)comppad);
3159 CvPADLIST(cv) = comppadlist;
3160 av_fill(comppad, AvFILL(protopad));
3161 curpad = AvARRAY(comppad);
3163 av = newAV(); /* will be @_ */
3165 av_store(comppad, 0, (SV*)av);
3166 AvFLAGS(av) = AVf_REIFY;
3168 for (ix = fpad; ix > 0; ix--) {
3169 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3170 if (namesv && namesv != &sv_undef) {
3171 char *name = SvPVX(namesv); /* XXX */
3172 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3173 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3174 CvOUTSIDE(cv), cxstack_ix);
3176 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3178 croak("panic: cv_clone: %s", name);
3180 else { /* our own lexical */
3183 /* anon code -- we'll come back for it */
3184 sv = SvREFCNT_inc(ppad[ix]);
3186 else if (*name == '@')
3188 else if (*name == '%')
3198 SV* sv = NEWSV(0,0);
3204 /* Now that vars are all in place, clone nested closures. */
3206 for (ix = fpad; ix > 0; ix--) {
3207 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3209 && namesv != &sv_undef
3210 && !(SvFLAGS(namesv) & SVf_FAKE)
3211 && *SvPVX(namesv) == '&'
3212 && CvCLONE(ppad[ix]))
3214 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3215 SvREFCNT_dec(ppad[ix]);
3218 curpad[ix] = (SV*)kid;
3222 #ifdef DEBUG_CLOSURES
3223 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3225 PerlIO_printf(Perl_debug_log, " from:\n");
3227 PerlIO_printf(Perl_debug_log, " to:\n");
3238 return cv_clone2(proto, CvOUTSIDE(proto));
3242 cv_ckproto(CV *cv, GV *gv, char *p)
3244 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3245 SV* msg = sv_newmortal();
3249 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3250 sv_setpv(msg, "Prototype mismatch:");
3252 sv_catpvf(msg, " sub %_", name);
3254 sv_catpvf(msg, " (%s)", SvPVX(cv));
3255 sv_catpv(msg, " vs ");
3257 sv_catpvf(msg, "(%s)", p);
3259 sv_catpv(msg, "none");
3270 if (!cv || !SvPOK(cv) || SvCUR(cv))
3274 for (o = CvSTART(cv); o; o = o->op_next) {
3275 OPCODE type = o->op_type;
3277 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3279 if (type == OP_LEAVESUB || type == OP_RETURN)
3283 if (type == OP_CONST)
3285 else if (type == OP_PADSV) {
3286 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3287 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3288 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3300 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3303 char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3304 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3305 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3314 if (!name || GvCVGEN(gv))
3316 else if (cv = GvCV(gv)) {
3317 cv_ckproto(cv, gv, ps);
3318 /* already defined (or promised)? */
3319 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3322 /* just a "sub foo;" when &foo is already defined */
3326 /* ahem, death to those who redefine active sort subs */
3327 if (curstack == sortstack && sortcop == CvSTART(cv))
3328 croak("Can't redefine active sort subroutine %s", name);
3329 const_sv = cv_const_sv(cv);
3330 if (const_sv || dowarn) {
3331 line_t oldline = curcop->cop_line;
3332 curcop->cop_line = copline;
3333 warn(const_sv ? "Constant subroutine %s redefined"
3334 : "Subroutine %s redefined", name);
3335 curcop->cop_line = oldline;
3341 if (cv) { /* must reuse cv if autoloaded */
3343 CvFLAGS(cv) = CvFLAGS(compcv);
3344 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3345 CvOUTSIDE(compcv) = 0;
3346 CvPADLIST(cv) = CvPADLIST(compcv);
3347 CvPADLIST(compcv) = 0;
3348 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3349 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3350 SvREFCNT_dec(compcv);
3360 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3361 CvFILEGV(cv) = curcop->cop_filegv;
3362 CvSTASH(cv) = curstash;
3365 New(666, CvMUTEXP(cv), 1, perl_mutex);
3366 MUTEX_INIT(CvMUTEXP(cv));
3367 #endif /* USE_THREADS */
3370 sv_setpv((SV*)cv, ps);
3376 char *s = strrchr(name, ':');
3378 if (strEQ(s, "BEGIN")) {
3380 "BEGIN not safe after errors--compilation aborted";
3384 /* force display of errors found but not reported */
3385 sv_catpv(ERRSV, not_safe);
3386 croak("%s", SvPVx(ERRSV, na));
3397 if (AvFILL(comppad_name) < AvFILL(comppad))
3398 av_store(comppad_name, AvFILL(comppad), Nullsv);
3401 SV **namep = AvARRAY(comppad_name);
3402 for (ix = AvFILL(comppad); ix > 0; ix--) {
3405 if (SvIMMORTAL(curpad[ix]))
3408 * The only things that a clonable function needs in its
3409 * pad are references to outer lexicals and anonymous subs.
3410 * The rest are created anew during cloning.
3412 if (!((namesv = namep[ix]) != Nullsv &&
3413 namesv != &sv_undef &&
3415 *SvPVX(namesv) == '&')))
3417 SvREFCNT_dec(curpad[ix]);
3418 curpad[ix] = Nullsv;
3423 AV *av = newAV(); /* Will be @_ */
3425 av_store(comppad, 0, (SV*)av);
3426 AvFLAGS(av) = AVf_REIFY;
3428 for (ix = AvFILL(comppad); ix > 0; ix--) {
3429 if (SvIMMORTAL(curpad[ix]))
3431 if (!SvPADMY(curpad[ix]))
3432 SvPADTMP_on(curpad[ix]);
3436 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3437 CvSTART(cv) = LINKLIST(CvROOT(cv));
3438 CvROOT(cv)->op_next = 0;
3444 if (PERLDB_SUBLINE && curstash != debstash) {
3445 SV *sv = NEWSV(0,0);
3446 SV *tmpstr = sv_newmortal();
3447 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3451 sv_setpvf(sv, "%_:%ld-%ld",
3452 GvSV(curcop->cop_filegv),
3453 (long)subline, (long)curcop->cop_line);
3454 gv_efullname3(tmpstr, gv, Nullch);
3455 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3456 hv = GvHVn(db_postponed);
3457 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3458 && (cv = GvCV(db_postponed))) {
3463 perl_call_sv((SV*)cv, G_DISCARD);
3467 if ((s = strrchr(name,':')))
3471 if (strEQ(s, "BEGIN")) {
3472 I32 oldscope = scopestack_ix;
3474 SAVESPTR(compiling.cop_filegv);
3475 SAVEI16(compiling.cop_line);
3482 DEBUG_x( dump_sub(gv) );
3483 av_push(beginav, (SV *)cv);
3485 call_list(oldscope, beginav);
3487 curcop = &compiling;
3490 else if (strEQ(s, "END") && !error_count) {
3493 av_unshift(endav, 1);
3494 av_store(endav, 0, (SV *)cv);
3497 else if (strEQ(s, "INIT") && !error_count) {
3500 av_push(initav, SvREFCNT_inc(cv));
3511 newXS(char *name, void (*subaddr) (CPERLproto_ CV *), char *filename)
3514 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3517 if (cv = (name ? GvCV(gv) : Nullcv)) {
3519 /* just a cached method */
3523 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3524 /* already defined (or promised) */
3526 line_t oldline = curcop->cop_line;
3527 curcop->cop_line = copline;
3528 warn("Subroutine %s redefined",name);
3529 curcop->cop_line = oldline;
3536 if (cv) /* must reuse cv if autoloaded */
3539 cv = (CV*)NEWSV(1105,0);
3540 sv_upgrade((SV *)cv, SVt_PVCV);
3547 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3549 New(666, CvMUTEXP(cv), 1, perl_mutex);
3550 MUTEX_INIT(CvMUTEXP(cv));
3552 #endif /* USE_THREADS */
3553 CvFILEGV(cv) = gv_fetchfile(filename);
3554 CvXSUB(cv) = subaddr;
3557 char *s = strrchr(name,':');
3562 if (strEQ(s, "BEGIN")) {
3565 av_push(beginav, (SV *)cv);
3568 else if (strEQ(s, "END")) {
3571 av_unshift(endav, 1);
3572 av_store(endav, 0, (SV *)cv);
3575 else if (strEQ(s, "INIT")) {
3578 av_push(initav, (SV *)cv);
3588 newFORM(I32 floor, OP *o, OP *block)
3597 name = SvPVx(cSVOPo->op_sv, na);
3600 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3602 if (cv = GvFORM(gv)) {
3604 line_t oldline = curcop->cop_line;
3606 curcop->cop_line = copline;
3607 warn("Format %s redefined",name);
3608 curcop->cop_line = oldline;
3614 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3615 CvFILEGV(cv) = curcop->cop_filegv;
3617 for (ix = AvFILL(comppad); ix > 0; ix--) {
3618 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3619 SvPADTMP_on(curpad[ix]);
3622 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3623 CvSTART(cv) = LINKLIST(CvROOT(cv));
3624 CvROOT(cv)->op_next = 0;
3634 return newUNOP(OP_REFGEN, 0,
3635 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3641 return newUNOP(OP_REFGEN, 0,
3642 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3646 newANONSUB(I32 floor, OP *proto, OP *block)
3648 return newUNOP(OP_REFGEN, 0,
3649 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3655 switch (o->op_type) {
3657 o->op_type = OP_PADAV;
3658 o->op_ppaddr = ppaddr[OP_PADAV];
3659 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3662 o->op_type = OP_RV2AV;
3663 o->op_ppaddr = ppaddr[OP_RV2AV];
3668 warn("oops: oopsAV");
3677 switch (o->op_type) {
3680 o->op_type = OP_PADHV;
3681 o->op_ppaddr = ppaddr[OP_PADHV];
3682 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3686 o->op_type = OP_RV2HV;
3687 o->op_ppaddr = ppaddr[OP_RV2HV];
3692 warn("oops: oopsHV");
3701 if (o->op_type == OP_PADANY) {
3702 o->op_type = OP_PADAV;
3703 o->op_ppaddr = ppaddr[OP_PADAV];
3706 return newUNOP(OP_RV2AV, 0, scalar(o));
3710 newGVREF(I32 type, OP *o)
3712 if (type == OP_MAPSTART)
3713 return newUNOP(OP_NULL, 0, o);
3714 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3720 if (o->op_type == OP_PADANY) {
3721 o->op_type = OP_PADHV;
3722 o->op_ppaddr = ppaddr[OP_PADHV];
3725 return newUNOP(OP_RV2HV, 0, scalar(o));
3731 croak("NOT IMPL LINE %d",__LINE__);
3737 newCVREF(I32 flags, OP *o)
3739 return newUNOP(OP_RV2CV, flags, scalar(o));
3745 if (o->op_type == OP_PADANY) {
3746 o->op_type = OP_PADSV;
3747 o->op_ppaddr = ppaddr[OP_PADSV];
3750 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3751 o->op_flags |= OPpDONE_SVREF;
3754 return newUNOP(OP_RV2SV, 0, scalar(o));
3757 /* Check routines. */
3765 name = NEWSV(1106,0);
3766 sv_upgrade(name, SVt_PVNV);
3767 sv_setpvn(name, "&", 1);
3770 ix = pad_alloc(o->op_type, SVs_PADMY);
3771 av_store(comppad_name, ix, name);
3772 av_store(comppad, ix, cSVOPo->op_sv);
3773 SvPADMY_on(cSVOPo->op_sv);
3774 cSVOPo->op_sv = Nullsv;
3775 cSVOPo->op_targ = ix;
3782 o->op_private = hints;
3789 if (cUNOPo->op_first->op_type == OP_CONCAT)
3790 o->op_flags |= OPf_STACKED;
3797 if (o->op_flags & OPf_KIDS) {
3800 OPCODE type = o->op_type;
3801 o = modkids(ck_fun(o), type);
3802 kid = cUNOPo->op_first;
3803 newop = kUNOP->op_first->op_sibling;
3805 (newop->op_sibling ||
3806 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3807 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3808 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3812 op_free(kUNOP->op_first);
3813 kUNOP->op_first = newop;
3815 o->op_ppaddr = ppaddr[++o->op_type];
3824 if (o->op_flags & OPf_KIDS) {
3825 OP *kid = cUNOPo->op_first;
3826 if (kid->op_type == OP_HSLICE)
3827 o->op_private |= OPpSLICE;
3828 else if (kid->op_type != OP_HELEM)
3829 croak("%s argument is not a HASH element or slice",
3830 op_desc[o->op_type]);
3839 I32 type = o->op_type;
3841 if (o->op_flags & OPf_KIDS) {
3842 if (cLISTOPo->op_first->op_type == OP_STUB) {
3844 o = newUNOP(type, OPf_SPECIAL,
3845 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
3855 hints |= HINT_BLOCK_SCOPE;
3856 if (o->op_flags & OPf_KIDS) {
3857 SVOP *kid = (SVOP*)cUNOPo->op_first;
3860 o->op_flags &= ~OPf_KIDS;
3863 else if (kid->op_type == OP_LINESEQ) {
3866 kid->op_next = o->op_next;
3867 cUNOPo->op_first = 0;
3870 Newz(1101, enter, 1, LOGOP);
3871 enter->op_type = OP_ENTERTRY;
3872 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3873 enter->op_private = 0;
3875 /* establish postfix order */
3876 enter->op_next = (OP*)enter;
3878 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3879 o->op_type = OP_LEAVETRY;
3880 o->op_ppaddr = ppaddr[OP_LEAVETRY];
3881 enter->op_other = o;
3889 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
3891 o->op_targ = (PADOFFSET)hints;
3899 if (o->op_flags & OPf_STACKED) {
3901 kid = cUNOPo->op_first->op_sibling;
3902 if (kid->op_type == OP_RV2GV)
3914 if (o->op_flags & OPf_KIDS) {
3915 OP *kid = cUNOPo->op_first;
3916 if (kid->op_type != OP_HELEM)
3917 croak("%s argument is not a HASH element", op_desc[o->op_type]);
3924 ck_gvconst(register OP *o)
3926 o = fold_constants(o);
3927 if (o->op_type == OP_CONST)
3933 ck_rvconst(register OP *o)
3936 SVOP *kid = (SVOP*)cUNOPo->op_first;
3938 o->op_private |= (hints & HINT_STRICT_REFS);
3939 if (kid->op_type == OP_CONST) {
3944 name = SvPV(kid->op_sv, na);
3945 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3946 char *badthing = Nullch;
3947 switch (o->op_type) {
3949 badthing = "a SCALAR";
3952 badthing = "an ARRAY";
3955 badthing = "a HASH";
3960 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3963 kid->op_type = OP_GV;
3964 iscv = (o->op_type == OP_RV2CV) * 2;
3965 for (gv = 0; !gv; iscv++) {
3967 * This is a little tricky. We only want to add the symbol if we
3968 * didn't add it in the lexer. Otherwise we get duplicate strict
3969 * warnings. But if we didn't add it in the lexer, we must at
3970 * least pretend like we wanted to add it even if it existed before,
3971 * or we get possible typo warnings. OPpCONST_ENTERED says
3972 * whether the lexer already added THIS instance of this symbol.
3974 gv = gv_fetchpv(name,
3975 iscv | !(kid->op_private & OPpCONST_ENTERED),
3978 : o->op_type == OP_RV2SV
3980 : o->op_type == OP_RV2AV
3982 : o->op_type == OP_RV2HV
3986 SvREFCNT_dec(kid->op_sv);
3987 kid->op_sv = SvREFCNT_inc(gv);
3996 I32 type = o->op_type;
3998 if (o->op_flags & OPf_REF)
4001 if (o->op_flags & OPf_KIDS) {
4002 SVOP *kid = (SVOP*)cUNOPo->op_first;
4004 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4005 OP *newop = newGVOP(type, OPf_REF,
4006 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
4013 if (type == OP_FTTTY)
4014 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4017 return newUNOP(type, 0, newDEFSVOP());
4030 int type = o->op_type;
4031 register I32 oa = opargs[type] >> OASHIFT;
4033 if (o->op_flags & OPf_STACKED) {
4034 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4037 return no_fh_allowed(o);
4040 if (o->op_flags & OPf_KIDS) {
4041 tokid = &cLISTOPo->op_first;
4042 kid = cLISTOPo->op_first;
4043 if (kid->op_type == OP_PUSHMARK ||
4044 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4046 tokid = &kid->op_sibling;
4047 kid = kid->op_sibling;
4049 if (!kid && opargs[type] & OA_DEFGV)
4050 *tokid = kid = newDEFSVOP();
4054 sibl = kid->op_sibling;
4068 if (kid->op_type == OP_CONST &&
4069 (kid->op_private & OPpCONST_BARE)) {
4070 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4071 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4072 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4074 warn("Array @%s missing the @ in argument %ld of %s()",
4075 name, (long)numargs, op_desc[type]);
4078 kid->op_sibling = sibl;
4081 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4082 bad_type(numargs, "array", op_desc[o->op_type], kid);
4086 if (kid->op_type == OP_CONST &&
4087 (kid->op_private & OPpCONST_BARE)) {
4088 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4089 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4090 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4092 warn("Hash %%%s missing the %% in argument %ld of %s()",
4093 name, (long)numargs, op_desc[type]);
4096 kid->op_sibling = sibl;
4099 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4100 bad_type(numargs, "hash", op_desc[o->op_type], kid);
4105 OP *newop = newUNOP(OP_NULL, 0, kid);
4106 kid->op_sibling = 0;
4108 newop->op_next = newop;
4110 kid->op_sibling = sibl;
4115 if (kid->op_type != OP_GV) {
4116 if (kid->op_type == OP_CONST &&
4117 (kid->op_private & OPpCONST_BARE)) {
4118 OP *newop = newGVOP(OP_GV, 0,
4119 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4125 kid->op_sibling = 0;
4126 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4128 kid->op_sibling = sibl;
4134 mod(scalar(kid), type);
4138 tokid = &kid->op_sibling;
4139 kid = kid->op_sibling;
4141 o->op_private |= numargs;
4143 return too_many_arguments(o,op_desc[o->op_type]);
4146 else if (opargs[type] & OA_DEFGV) {
4148 return newUNOP(type, 0, newDEFSVOP());
4152 while (oa & OA_OPTIONAL)
4154 if (oa && oa != OA_LIST)
4155 return too_few_arguments(o,op_desc[o->op_type]);
4165 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4166 append_elem(OP_GLOB, o, newDEFSVOP());
4168 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4169 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4171 if (gv && GvIMPORTED_CV(gv)) {
4172 static int glob_index;
4174 append_elem(OP_GLOB, o,
4175 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4176 o->op_type = OP_LIST;
4177 o->op_ppaddr = ppaddr[OP_LIST];
4178 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4179 cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4180 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4181 append_elem(OP_LIST, o,
4182 scalar(newUNOP(OP_RV2CV, 0,
4183 newGVOP(OP_GV, 0, gv)))));
4184 o = newUNOP(OP_NULL, 0, ck_subr(o));
4185 o->op_targ = OP_GLOB; /* hint at what it used to be */
4188 gv = newGVgen("main");
4190 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4200 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4202 o->op_ppaddr = ppaddr[OP_GREPSTART];
4203 Newz(1101, gwop, 1, LOGOP);
4205 if (o->op_flags & OPf_STACKED) {
4208 kid = cLISTOPo->op_first->op_sibling;
4209 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4212 kid->op_next = (OP*)gwop;
4213 o->op_flags &= ~OPf_STACKED;
4215 kid = cLISTOPo->op_first->op_sibling;
4216 if (type == OP_MAPWHILE)
4223 kid = cLISTOPo->op_first->op_sibling;
4224 if (kid->op_type != OP_NULL)
4225 croak("panic: ck_grep");
4226 kid = kUNOP->op_first;
4228 gwop->op_type = type;
4229 gwop->op_ppaddr = ppaddr[type];
4230 gwop->op_first = listkids(o);
4231 gwop->op_flags |= OPf_KIDS;
4232 gwop->op_private = 1;
4233 gwop->op_other = LINKLIST(kid);
4234 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4235 kid->op_next = (OP*)gwop;
4237 kid = cLISTOPo->op_first->op_sibling;
4238 if (!kid || !kid->op_sibling)
4239 return too_few_arguments(o,op_desc[o->op_type]);
4240 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4241 mod(kid, OP_GREPSTART);
4249 if (o->op_flags & OPf_KIDS) {
4250 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4251 if (kid && kid->op_type == OP_CONST)
4252 fbm_compile(((SVOP*)kid)->op_sv);
4258 ck_lengthconst(OP *o)
4260 /* XXX length optimization goes here */
4267 OPCODE type = o->op_type;
4268 return modkids(ck_fun(o), type);
4274 OPCODE type = o->op_type;
4275 return refkids(ck_fun(o), type);
4283 kid = cLISTOPo->op_first;
4286 kid = cLISTOPo->op_first;
4288 if (kid->op_type == OP_PUSHMARK)
4289 kid = kid->op_sibling;
4290 if (kid && o->op_flags & OPf_STACKED)
4291 kid = kid->op_sibling;
4292 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4293 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4294 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4295 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4296 cLISTOPo->op_first->op_sibling = kid;
4297 cLISTOPo->op_last = kid;
4298 kid = kid->op_sibling;
4303 append_elem(o->op_type, o, newDEFSVOP());
4309 if (hints & HINT_LOCALE)
4310 o->op_private |= OPpLOCALE;
4317 ck_fun_locale(OP *o)
4323 if (hints & HINT_LOCALE)
4324 o->op_private |= OPpLOCALE;
4335 if (hints & HINT_LOCALE)
4336 o->op_private |= OPpLOCALE;
4345 o->op_private |= OPpRUNTIME;
4358 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4359 o->op_private |= OPpREPEAT_DOLIST;
4360 cBINOPo->op_first = force_list(cBINOPo->op_first);
4370 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4371 SVOP *kid = (SVOP*)cUNOPo->op_first;
4373 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4375 for (s = SvPVX(kid->op_sv); *s; s++) {
4376 if (*s == ':' && s[1] == ':') {
4378 Move(s+2, s+1, strlen(s+2)+1, char);
4379 --SvCUR(kid->op_sv);
4382 sv_catpvn(kid->op_sv, ".pm", 3);
4391 croak("NOT IMPL LINE %d",__LINE__);
4400 if (o->op_flags & OPf_KIDS) {
4401 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4402 if (kid && kid->op_sibling) {
4403 o->op_type = OP_SSELECT;
4404 o->op_ppaddr = ppaddr[OP_SSELECT];
4406 return fold_constants(o);
4410 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4411 if (kid && kid->op_type == OP_RV2GV)
4412 kid->op_private &= ~HINT_STRICT_REFS;
4419 I32 type = o->op_type;
4421 if (!(o->op_flags & OPf_KIDS)) {
4426 if (!CvUNIQUE(compcv)) {
4427 argop = newOP(OP_PADAV, OPf_REF);
4428 argop->op_targ = 0; /* curpad[0] is @_ */
4431 argop = newUNOP(OP_RV2AV, 0,
4432 scalar(newGVOP(OP_GV, 0,
4433 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4436 argop = newUNOP(OP_RV2AV, 0,
4437 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4438 defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4439 #endif /* USE_THREADS */
4440 return newUNOP(type, 0, scalar(argop));
4442 return scalar(modkids(ck_fun(o), type));
4450 if (hints & HINT_LOCALE)
4451 o->op_private |= OPpLOCALE;
4454 if (o->op_flags & OPf_STACKED) {
4455 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4457 kid = kUNOP->op_first; /* get past rv2gv */
4459 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4461 if (kid->op_type == OP_SCOPE) {
4465 else if (kid->op_type == OP_LEAVE) {
4466 if (o->op_type == OP_SORT) {
4467 null(kid); /* wipe out leave */
4470 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4471 if (k->op_next == kid)
4476 kid->op_next = 0; /* just disconnect the leave */
4477 k = kLISTOP->op_first;
4481 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4482 null(kid); /* wipe out rv2gv */
4483 if (o->op_type == OP_SORT)
4487 o->op_flags |= OPf_SPECIAL;
4499 if (o->op_flags & OPf_STACKED)
4500 return no_fh_allowed(o);
4502 kid = cLISTOPo->op_first;
4503 if (kid->op_type != OP_NULL)
4504 croak("panic: ck_split");
4505 kid = kid->op_sibling;
4506 op_free(cLISTOPo->op_first);
4507 cLISTOPo->op_first = kid;
4509 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4510 cLISTOPo->op_last = kid; /* There was only one element previously */
4513 if (kid->op_type != OP_MATCH) {
4514 OP *sibl = kid->op_sibling;
4515 kid->op_sibling = 0;
4516 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4517 if (cLISTOPo->op_first == cLISTOPo->op_last)
4518 cLISTOPo->op_last = kid;
4519 cLISTOPo->op_first = kid;
4520 kid->op_sibling = sibl;
4523 kid->op_type = OP_PUSHRE;
4524 kid->op_ppaddr = ppaddr[OP_PUSHRE];
4527 if (!kid->op_sibling)
4528 append_elem(OP_SPLIT, o, newDEFSVOP());
4530 kid = kid->op_sibling;
4533 if (!kid->op_sibling)
4534 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4536 kid = kid->op_sibling;
4539 if (kid->op_sibling)
4540 return too_many_arguments(o,op_desc[o->op_type]);
4549 OP *prev = ((cUNOPo->op_first->op_sibling)
4550 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4551 OP *o2 = prev->op_sibling;
4559 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4560 if (cvop->op_type == OP_RV2CV) {
4562 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4563 null(cvop); /* disable rv2cv */
4564 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4565 if (tmpop->op_type == OP_GV) {
4566 cv = GvCVu(tmpop->op_sv);
4567 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4568 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4569 proto = SvPV((SV*)cv, na);
4573 o->op_private |= (hints & HINT_STRICT_REFS);
4574 if (PERLDB_SUB && curstash != debstash)
4575 o->op_private |= OPpENTERSUB_DB;
4576 while (o2 != cvop) {
4580 return too_many_arguments(o, gv_ename(namegv));
4598 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4599 bad_type(arg, "block", gv_ename(namegv), o2);
4604 if (o2->op_type == OP_RV2GV)
4608 o2 = newUNOP(OP_RV2GV, 0, kid);
4609 o2->op_sibling = kid->op_sibling;
4610 kid->op_sibling = 0;
4611 prev->op_sibling = o;
4619 if (o2->op_type != OP_RV2GV)
4620 bad_type(arg, "symbol", gv_ename(namegv), o2);
4623 if (o2->op_type != OP_RV2CV)
4624 bad_type(arg, "sub", gv_ename(namegv), o2);
4627 if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4628 bad_type(arg, "scalar", gv_ename(namegv), o2);
4631 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4632 bad_type(arg, "array", gv_ename(namegv), o2);
4635 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4636 bad_type(arg, "hash", gv_ename(namegv), o2);
4640 o2 = newUNOP(OP_REFGEN, 0, kid);
4641 o2->op_sibling = kid->op_sibling;
4642 kid->op_sibling = 0;
4643 prev->op_sibling = o2;
4654 croak("Malformed prototype for %s: %s",
4655 gv_ename(namegv), SvPV((SV*)cv, na));
4660 mod(o2, OP_ENTERSUB);
4662 o2 = o2->op_sibling;
4664 if (proto && !optional &&
4665 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4666 return too_few_arguments(o, gv_ename(namegv));
4673 SvREADONLY_on(cSVOPo->op_sv);
4680 if (o->op_flags & OPf_KIDS) {
4681 SVOP *kid = (SVOP*)cUNOPo->op_first;
4683 if (kid->op_type == OP_NULL)
4684 kid = (SVOP*)kid->op_sibling;
4686 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4687 o->op_flags |= OPf_SPECIAL;
4692 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4695 peep(register OP *o)
4698 register OP* oldop = 0;
4699 if (!o || o->op_seq)
4704 for (; o; o = o->op_next) {
4710 switch (o->op_type) {
4713 curcop = ((COP*)o); /* for warnings */
4714 o->op_seq = op_seqmax++;
4725 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4727 o->op_seq = op_seqmax++;
4730 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4731 o->op_seq = op_seqmax++;
4732 break; /* Scalar stub must produce undef. List stub is noop */
4736 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4737 curcop = ((COP*)op);
4743 if (oldop && o->op_next) {
4744 oldop->op_next = o->op_next;
4747 o->op_seq = op_seqmax++;
4751 if (o->op_next->op_type == OP_RV2SV) {
4752 if (!(o->op_next->op_private & OPpDEREF)) {
4754 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4755 o->op_next = o->op_next->op_next;
4756 o->op_type = OP_GVSV;
4757 o->op_ppaddr = ppaddr[OP_GVSV];
4760 else if (o->op_next->op_type == OP_RV2AV) {
4761 OP* pop = o->op_next->op_next;
4763 if (pop->op_type == OP_CONST &&
4764 (op = pop->op_next) &&
4765 pop->op_next->op_type == OP_AELEM &&
4766 !(pop->op_next->op_private &
4767 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4768 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4772 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4776 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4777 o->op_next = pop->op_next->op_next;
4778 o->op_type = OP_AELEMFAST;
4779 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4780 o->op_private = (U8)i;
4781 GvAVn(((GVOP*)o)->op_gv);
4784 o->op_seq = op_seqmax++;
4788 if (o->op_next->op_type == OP_RV2AV
4789 && (o->op_next->op_flags && OPf_REF))
4792 o->op_next = o->op_next->op_next;
4797 if (o->op_next->op_type == OP_RV2HV
4798 && (o->op_next->op_flags && OPf_REF))
4801 o->op_next = o->op_next->op_next;
4809 o->op_seq = op_seqmax++;
4810 peep(cLOGOP->op_other);
4814 o->op_seq = op_seqmax++;
4815 peep(cCONDOP->op_true);
4816 peep(cCONDOP->op_false);
4820 o->op_seq = op_seqmax++;
4821 peep(cLOOP->op_redoop);
4822 peep(cLOOP->op_nextop);
4823 peep(cLOOP->op_lastop);
4828 o->op_seq = op_seqmax++;
4829 peep(cPMOP->op_pmreplstart);
4833 o->op_seq = op_seqmax++;
4834 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4835 if (o->op_next->op_sibling &&
4836 o->op_next->op_sibling->op_type != OP_DIE) {
4837 line_t oldline = curcop->cop_line;
4839 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4840 warn("Statement unlikely to be reached");
4841 warn("(Maybe you meant system() when you said exec()?)\n");
4842 curcop->cop_line = oldline;
4856 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
4857 || ((BINOP*)o)->op_last->op_type != OP_CONST)
4859 rop = (UNOP*)((BINOP*)o)->op_first;
4860 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
4862 lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
4863 if (!SvOBJECT(lexname))
4865 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
4866 if (!fields || !GvHV(*fields))
4868 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
4869 key = SvPV(*svp, keylen);
4870 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
4872 croak("No such field \"%s\" in variable %s of type %s",
4873 key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
4875 ind = SvIV(*indsvp);
4877 croak("Bad index while coercing array into hash");
4878 rop->op_type = OP_RV2AV;
4879 rop->op_ppaddr = ppaddr[OP_RV2AV];
4880 o->op_type = OP_AELEM;
4881 o->op_ppaddr = ppaddr[OP_AELEM];
4883 *svp = newSViv(ind);
4888 o->op_seq = op_seqmax++;