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));
51 static OP *new_logop _((I32 type, I32 flags, OP **firstp, OP **otherp));
57 SV* tmpsv = sv_newmortal();
58 gv_efullname3(tmpsv, gv, Nullch);
59 return SvPV(tmpsv,na);
65 yyerror(form("Missing comma after first argument to %s function",
66 op_desc[o->op_type]));
71 too_few_arguments(OP *o, char *name)
73 yyerror(form("Not enough arguments for %s", name));
78 too_many_arguments(OP *o, char *name)
80 yyerror(form("Too many arguments for %s", name));
85 bad_type(I32 n, char *t, char *name, OP *kid)
87 yyerror(form("Type of arg %d to %s must be %s (not %s)",
88 (int)n, name, t, op_desc[kid->op_type]));
94 int type = o->op_type;
95 if (type != OP_AELEM && type != OP_HELEM) {
96 yyerror(form("Can't use subscript on %s", op_desc[type]));
97 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) {
100 newSVpvf("(Did you mean $ or @ instead of %c?)\n",
101 type == OP_ENTERSUB ? '&' : '%'));
105 sv_catsv(GvSV(errgv), msg);
107 PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg));
112 /* "register" allocation */
115 pad_allocmy(char *name)
121 if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
122 if (!isPRINT(name[1])) {
124 name[2] = toCTRL(name[1]);
127 croak("Can't use global %s in \"my\"",name);
129 if (dowarn && AvFILLp(comppad_name) >= 0) {
130 SV **svp = AvARRAY(comppad_name);
131 for (off = AvFILLp(comppad_name); off > comppad_name_floor; off--) {
134 && SvIVX(sv) == 999999999 /* var is in open scope */
135 && strEQ(name, SvPVX(sv)))
137 warn("\"my\" variable %s masks earlier declaration in same scope", name);
142 off = pad_alloc(OP_PADSV, SVs_PADMY);
144 sv_upgrade(sv, SVt_PVNV);
148 croak("Can't declare class for non-scalar %s in \"my\"",name);
150 (void)SvUPGRADE(sv, SVt_PVMG);
151 SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash);
154 av_store(comppad_name, off, sv);
155 SvNVX(sv) = (double)999999999;
156 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
157 if (!min_intro_pending)
158 min_intro_pending = off;
159 max_intro_pending = off;
161 av_store(comppad, off, (SV*)newAV());
162 else if (*name == '%')
163 av_store(comppad, off, (SV*)newHV());
164 SvPADMY_on(curpad[off]);
169 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
176 register PERL_CONTEXT *cx;
179 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
180 AV *curlist = CvPADLIST(cv);
181 SV **svp = av_fetch(curlist, 0, FALSE);
184 if (!svp || *svp == &sv_undef)
187 svp = AvARRAY(curname);
188 for (off = AvFILLp(curname); off > 0; off--) {
189 if ((sv = svp[off]) &&
192 seq > I_32(SvNVX(sv)) &&
193 strEQ(SvPVX(sv), name))
204 return 0; /* don't clone from inactive stack frame */
208 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
209 oldsv = *av_fetch(oldpad, off, TRUE);
210 if (!newoff) { /* Not a mere clone operation. */
211 SV *namesv = NEWSV(1103,0);
212 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
213 sv_upgrade(namesv, SVt_PVNV);
214 sv_setpv(namesv, name);
215 av_store(comppad_name, newoff, namesv);
216 SvNVX(namesv) = (double)curcop->cop_seq;
217 SvIVX(namesv) = 999999999; /* A ref, intro immediately */
218 SvFAKE_on(namesv); /* A ref, not a real var */
219 if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) {
220 /* "It's closures all the way down." */
224 oldsv = Nullsv; /* no need to keep ref */
229 bcv && bcv != cv && !CvCLONE(bcv);
230 bcv = CvOUTSIDE(bcv)) {
234 if (dowarn && !CvUNIQUE(cv))
236 "Variable \"%s\" may be unavailable",
243 else if (!CvUNIQUE(compcv)) {
244 if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv))
245 warn("Variable \"%s\" will not stay shared", name);
248 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
254 /* Nothing in current lexical context--try eval's context, if any.
255 * This is necessary to let the perldb get at lexically scoped variables.
256 * XXX This will also probably interact badly with eval tree caching.
260 for (i = cx_ix; i >= 0; i--) {
262 switch (cx->cx_type) {
264 if (i == 0 && saweval) {
265 seq = cxstack[saweval].blk_oldcop->cop_seq;
266 return pad_findlex(name, newoff, seq, main_cv, 0);
270 switch (cx->blk_eval.old_op_type) {
275 /* require must have its own scope */
283 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
284 saweval = i; /* so we know where we were called from */
287 seq = cxstack[saweval].blk_oldcop->cop_seq;
288 return pad_findlex(name, newoff, seq, cv, i-1);
296 pad_findmy(char *name)
302 SV **svp = AvARRAY(comppad_name);
303 U32 seq = cop_seqmax;
307 * Special case to get lexical (and hence per-thread) @_.
308 * XXX I need to find out how to tell at parse-time whether use
309 * of @_ should refer to a lexical (from a sub) or defgv (global
310 * scope and maybe weird sub-ish things like formats). See
311 * startsub in perly.y. It's possible that @_ could be lexical
312 * (at least from subs) even in non-threaded perl.
314 if (strEQ(name, "@_"))
315 return 0; /* success. (NOT_IN_PAD indicates failure) */
316 #endif /* USE_THREADS */
318 /* The one we're looking for is probably just before comppad_name_fill. */
319 for (off = AvFILLp(comppad_name); off > 0; off--) {
320 if ((sv = svp[off]) &&
324 seq > I_32(SvNVX(sv)))) &&
325 strEQ(SvPVX(sv), name))
328 return (PADOFFSET)off;
329 pendoff = off; /* this pending def. will override import */
333 /* See if it's in a nested scope */
334 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
336 /* If there is a pending local definition, this new alias must die */
338 SvIVX(AvARRAY(comppad_name)[off]) = seq;
339 return off; /* pad_findlex returns 0 for failure...*/
341 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
345 pad_leavemy(I32 fill)
348 SV **svp = AvARRAY(comppad_name);
350 if (min_intro_pending && fill < min_intro_pending) {
351 for (off = max_intro_pending; off >= min_intro_pending; off--) {
352 if ((sv = svp[off]) && sv != &sv_undef)
353 warn("%s never introduced", SvPVX(sv));
356 /* "Deintroduce" my variables that are leaving with this scope. */
357 for (off = AvFILLp(comppad_name); off > fill; off--) {
358 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
359 SvIVX(sv) = cop_seqmax;
364 pad_alloc(I32 optype, U32 tmptype)
370 if (AvARRAY(comppad) != curpad)
371 croak("panic: pad_alloc");
372 if (pad_reset_pending)
374 if (tmptype & SVs_PADMY) {
376 sv = *av_fetch(comppad, AvFILLp(comppad) + 1, TRUE);
377 } while (SvPADBUSY(sv)); /* need a fresh one */
378 retval = AvFILLp(comppad);
381 SV **names = AvARRAY(comppad_name);
382 SSize_t names_fill = AvFILLp(comppad_name);
385 * "foreach" index vars temporarily become aliases to non-"my"
386 * values. Thus we must skip, not just pad values that are
387 * marked as current pad values, but also those with names.
389 if (++padix <= names_fill &&
390 (sv = names[padix]) && sv != &sv_undef)
392 sv = *av_fetch(comppad, padix, TRUE);
393 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
398 SvFLAGS(sv) |= tmptype;
399 curpad = AvARRAY(comppad);
401 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
402 (unsigned long) thr, (unsigned long) curpad,
403 (long) retval, op_name[optype]));
405 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
406 (unsigned long) curpad,
407 (long) retval, op_name[optype]));
408 #endif /* USE_THREADS */
409 return (PADOFFSET)retval;
417 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
418 (unsigned long) thr, (unsigned long) curpad, po));
421 croak("panic: pad_sv po");
422 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
423 (unsigned long) curpad, po));
424 #endif /* USE_THREADS */
425 return curpad[po]; /* eventually we'll turn this into a macro */
429 pad_free(PADOFFSET po)
434 if (AvARRAY(comppad) != curpad)
435 croak("panic: pad_free curpad");
437 croak("panic: pad_free po");
439 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
440 (unsigned long) thr, (unsigned long) curpad, po));
442 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
443 (unsigned long) curpad, po));
444 #endif /* USE_THREADS */
445 if (curpad[po] && curpad[po] != &sv_undef)
446 SvPADTMP_off(curpad[po]);
452 pad_swipe(PADOFFSET po)
455 if (AvARRAY(comppad) != curpad)
456 croak("panic: pad_swipe curpad");
458 croak("panic: pad_swipe po");
460 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
461 (unsigned long) thr, (unsigned long) curpad, po));
463 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
464 (unsigned long) curpad, po));
465 #endif /* USE_THREADS */
466 SvPADTMP_off(curpad[po]);
467 curpad[po] = NEWSV(1107,0);
468 SvPADTMP_on(curpad[po]);
473 /* XXX pad_reset() is currently disabled because it results in serious bugs.
474 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
475 * on the stack by OPs that use them, there are several ways to get an alias
476 * to a shared TARG. Such an alias will change randomly and unpredictably.
477 * We avoid doing this until we can think of a Better Way.
482 #ifdef USE_BROKEN_PAD_RESET
486 if (AvARRAY(comppad) != curpad)
487 croak("panic: pad_reset curpad");
489 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
490 (unsigned long) thr, (unsigned long) curpad));
492 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
493 (unsigned long) curpad));
494 #endif /* USE_THREADS */
495 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
496 for (po = AvMAX(comppad); po > padix_floor; po--) {
497 if (curpad[po] && !SvIMMORTAL(curpad[po]))
498 SvPADTMP_off(curpad[po]);
503 pad_reset_pending = FALSE;
507 /* find_threadsv is not reentrant */
509 find_threadsv(char *name)
515 /* We currently only handle names of a single character */
516 p = strchr(threadsv_names, *name);
519 key = p - threadsv_names;
520 svp = av_fetch(thr->threadsv, key, FALSE);
522 SV *sv = NEWSV(0, 0);
523 av_store(thr->threadsv, key, sv);
524 thr->threadsvp = AvARRAY(thr->threadsv);
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 /* XXX %! tied to Errno.pm needs to be added here.
545 * See gv_fetchpv(). */
549 sv_magic(sv, 0, 0, name, 1);
551 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
552 "find_threadsv: new SV %p for $%s%c\n",
553 sv, (*name < 32) ? "^" : "",
554 (*name < 32) ? toCTRL(*name) : *name));
558 #endif /* USE_THREADS */
565 register OP *kid, *nextkid;
567 if (!o || o->op_seq == (U16)-1)
570 if (o->op_flags & OPf_KIDS) {
571 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
572 nextkid = kid->op_sibling; /* Get before next freeing kid */
577 switch (o->op_type) {
579 o->op_targ = 0; /* Was holding old type, if any. */
582 o->op_targ = 0; /* Was holding hints. */
586 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
588 #endif /* USE_THREADS */
590 if (!(o->op_flags & OPf_REF)
591 || (check[o->op_type] != FUNC_NAME_TO_PTR(ck_ftst)))
597 SvREFCNT_dec(cGVOPo->op_gv);
601 Safefree(cCOPo->cop_label);
602 SvREFCNT_dec(cCOPo->cop_filegv);
605 SvREFCNT_dec(cSVOPo->op_sv);
611 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
615 Safefree(cPVOPo->op_pv);
618 op_free(cPMOPo->op_pmreplroot);
622 ReREFCNT_dec(cPMOPo->op_pmregexp);
627 pad_free(o->op_targ);
635 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
636 pad_free(o->op_targ);
637 o->op_targ = o->op_type;
638 o->op_type = OP_NULL;
639 o->op_ppaddr = ppaddr[OP_NULL];
642 /* Contextualizers */
644 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
654 /* establish postfix order */
655 if (cUNOPo->op_first) {
656 o->op_next = LINKLIST(cUNOPo->op_first);
657 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
659 kid->op_next = LINKLIST(kid->op_sibling);
674 if (o && o->op_flags & OPf_KIDS) {
675 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
685 o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
687 line_t oldline = curcop->cop_line;
689 if (copline != NOLINE)
690 curcop->cop_line = copline;
691 warn("Found = in conditional, should be ==");
692 curcop->cop_line = oldline;
702 /* assumes no premature commitment */
703 if (!o || (o->op_flags & OPf_WANT) || error_count
704 || o->op_type == OP_RETURN)
707 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
709 switch (o->op_type) {
711 if (o->op_private & OPpREPEAT_DOLIST)
712 null(((LISTOP*)cBINOPo->op_first)->op_first);
713 scalar(cBINOPo->op_first);
718 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
722 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
723 if (!kPMOP->op_pmreplroot)
724 deprecate("implicit split to @_");
731 if (o->op_flags & OPf_KIDS) {
732 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
738 kid = cLISTOPo->op_first;
740 while (kid = kid->op_sibling) {
746 WITH_THR(curcop = &compiling);
751 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
757 WITH_THR(curcop = &compiling);
770 /* assumes no premature commitment */
771 if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count
772 || o->op_type == OP_RETURN)
775 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
777 switch (o->op_type) {
779 if (!(opargs[o->op_type] & OA_FOLDCONST))
783 if (o->op_flags & OPf_STACKED)
787 if (o->op_private == 4)
858 if (!(o->op_private & OPpLVAL_INTRO))
859 useless = op_desc[o->op_type];
866 if (!(o->op_private & OPpLVAL_INTRO) &&
867 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
868 useless = "a variable";
873 WITH_THR(curcop = ((COP*)o)); /* for warning below */
879 useless = "a constant";
880 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
882 else if (SvPOK(sv)) {
883 if (strnEQ(SvPVX(sv), "di", 2) ||
884 strnEQ(SvPVX(sv), "ds", 2) ||
885 strnEQ(SvPVX(sv), "ig", 2))
889 null(o); /* don't execute a constant */
890 SvREFCNT_dec(sv); /* don't even remember it */
894 o->op_type = OP_PREINC; /* pre-increment is faster */
895 o->op_ppaddr = ppaddr[OP_PREINC];
899 o->op_type = OP_PREDEC; /* pre-decrement is faster */
900 o->op_ppaddr = ppaddr[OP_PREDEC];
906 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
911 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
912 WITH_THR(curcop = ((COP*)o)); /* for warning below */
913 if (o->op_flags & OPf_STACKED)
919 if (!(o->op_flags & OPf_KIDS))
928 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
935 /* all requires must return a boolean value */
936 o->op_flags &= ~OPf_WANT;
939 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
940 if (!kPMOP->op_pmreplroot)
941 deprecate("implicit split to @_");
945 if (useless && dowarn)
946 warn("Useless use of %s in void context", useless);
954 if (o && o->op_flags & OPf_KIDS) {
955 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
966 /* assumes no premature commitment */
967 if (!o || (o->op_flags & OPf_WANT) || error_count
968 || o->op_type == OP_RETURN)
971 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
973 switch (o->op_type) {
976 list(cBINOPo->op_first);
981 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
988 if (!(o->op_flags & OPf_KIDS))
990 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
991 list(cBINOPo->op_first);
992 return gen_constant_list(o);
999 kid = cLISTOPo->op_first;
1001 while (kid = kid->op_sibling) {
1002 if (kid->op_sibling)
1007 WITH_THR(curcop = &compiling);
1011 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1012 if (kid->op_sibling)
1017 WITH_THR(curcop = &compiling);
1020 /* all requires must return a boolean value */
1021 o->op_flags &= ~OPf_WANT;
1033 if (o->op_type == OP_LINESEQ ||
1034 o->op_type == OP_SCOPE ||
1035 o->op_type == OP_LEAVE ||
1036 o->op_type == OP_LEAVETRY)
1039 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1040 if (kid->op_sibling) {
1044 curcop = &compiling;
1046 o->op_flags &= ~OPf_PARENS;
1047 if (hints & HINT_BLOCK_SCOPE)
1048 o->op_flags |= OPf_PARENS;
1051 o = newOP(OP_STUB, 0);
1056 modkids(OP *o, I32 type)
1059 if (o && o->op_flags & OPf_KIDS) {
1060 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1067 mod(OP *o, I32 type)
1073 if (!o || error_count)
1076 switch (o->op_type) {
1081 if (!(o->op_private & (OPpCONST_ARYBASE)))
1083 if (eval_start && eval_start->op_type == OP_CONST) {
1084 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
1088 SAVEI32(compiling.cop_arybase);
1089 compiling.cop_arybase = 0;
1091 else if (type == OP_REFGEN)
1094 croak("That use of $[ is unsupported");
1097 if (o->op_flags & OPf_PARENS)
1101 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1102 !(o->op_flags & OPf_STACKED)) {
1103 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1104 o->op_ppaddr = ppaddr[OP_RV2CV];
1105 assert(cUNOPo->op_first->op_type == OP_NULL);
1106 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1112 /* grep, foreach, subcalls, refgen */
1113 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1115 yyerror(form("Can't modify %s in %s",
1116 op_desc[o->op_type],
1117 type ? op_desc[type] : "local"));
1131 case OP_RIGHT_SHIFT:
1140 if (!(o->op_flags & OPf_STACKED))
1146 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1152 if (!type && cUNOPo->op_first->op_type != OP_GV)
1153 croak("Can't localize through a reference");
1154 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1156 return o; /* Treat \(@foo) like ordinary list. */
1160 if (scalar_mod_type(o, type))
1162 ref(cUNOPo->op_first, o->op_type);
1174 if (!type && cUNOPo->op_first->op_type != OP_GV)
1175 croak("Can't localize through a reference");
1176 ref(cUNOPo->op_first, o->op_type);
1180 hints |= HINT_BLOCK_SCOPE;
1189 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1190 return o; /* Treat \(@foo) like ordinary list. */
1191 if (scalar_mod_type(o, type))
1197 croak("Can't localize lexical variable %s",
1198 SvPV(*av_fetch(comppad_name, o->op_targ, 4), na));
1203 modcount++; /* XXX ??? */
1205 #endif /* USE_THREADS */
1211 if (type != OP_SASSIGN)
1215 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1221 pad_free(o->op_targ);
1222 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1223 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1224 if (o->op_flags & OPf_KIDS)
1225 mod(cBINOPo->op_first->op_sibling, type);
1230 ref(cBINOPo->op_first, o->op_type);
1231 if (type == OP_ENTERSUB &&
1232 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1233 o->op_private |= OPpLVAL_DEFER;
1240 if (o->op_flags & OPf_KIDS)
1241 mod(cLISTOPo->op_last, type);
1245 if (!(o->op_flags & OPf_KIDS))
1247 if (o->op_targ != OP_LIST) {
1248 mod(cBINOPo->op_first, type);
1253 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1257 o->op_flags |= OPf_MOD;
1259 if (type == OP_AASSIGN || type == OP_SASSIGN)
1260 o->op_flags |= OPf_SPECIAL|OPf_REF;
1262 o->op_private |= OPpLVAL_INTRO;
1263 o->op_flags &= ~OPf_SPECIAL;
1264 hints |= HINT_BLOCK_SCOPE;
1266 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1267 o->op_flags |= OPf_REF;
1272 scalar_mod_type(OP *o, I32 type)
1276 if (o->op_type == OP_RV2GV)
1300 case OP_RIGHT_SHIFT:
1307 case OP_ANDASSIGN: /* may work later */
1308 case OP_ORASSIGN: /* may work later */
1316 refkids(OP *o, I32 type)
1319 if (o && o->op_flags & OPf_KIDS) {
1320 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1327 ref(OP *o, I32 type)
1331 if (!o || error_count)
1334 switch (o->op_type) {
1336 if ((type == OP_DEFINED || type == OP_LOCK) &&
1337 !(o->op_flags & OPf_STACKED)) {
1338 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1339 o->op_ppaddr = ppaddr[OP_RV2CV];
1340 assert(cUNOPo->op_first->op_type == OP_NULL);
1341 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1342 o->op_flags |= OPf_SPECIAL;
1347 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1351 ref(cUNOPo->op_first, o->op_type);
1354 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1355 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1356 : type == OP_RV2HV ? OPpDEREF_HV
1358 o->op_flags |= OPf_MOD;
1363 o->op_flags |= OPf_MOD; /* XXX ??? */
1368 o->op_flags |= OPf_REF;
1371 ref(cUNOPo->op_first, o->op_type);
1376 o->op_flags |= OPf_REF;
1381 if (!(o->op_flags & OPf_KIDS))
1383 ref(cBINOPo->op_first, type);
1387 ref(cBINOPo->op_first, o->op_type);
1388 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1389 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1390 : type == OP_RV2HV ? OPpDEREF_HV
1392 o->op_flags |= OPf_MOD;
1400 if (!(o->op_flags & OPf_KIDS))
1402 ref(cLISTOPo->op_last, type);
1417 if (!o || error_count)
1421 if (type == OP_LIST) {
1422 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1424 } else if (type == OP_UNDEF) {
1426 } else if (type != OP_PADSV &&
1429 type != OP_PUSHMARK)
1431 yyerror(form("Can't declare %s in my", op_desc[o->op_type]));
1434 o->op_flags |= OPf_MOD;
1435 o->op_private |= OPpLVAL_INTRO;
1443 o->op_flags |= OPf_PARENS;
1448 bind_match(I32 type, OP *left, OP *right)
1453 (left->op_type == OP_RV2AV ||
1454 left->op_type == OP_RV2HV ||
1455 left->op_type == OP_PADAV ||
1456 left->op_type == OP_PADHV)) {
1457 char *desc = op_desc[(right->op_type == OP_SUBST ||
1458 right->op_type == OP_TRANS)
1459 ? right->op_type : OP_MATCH];
1460 char *sample = ((left->op_type == OP_RV2AV ||
1461 left->op_type == OP_PADAV)
1462 ? "@array" : "%hash");
1463 warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample);
1466 if (right->op_type == OP_MATCH ||
1467 right->op_type == OP_SUBST ||
1468 right->op_type == OP_TRANS) {
1469 right->op_flags |= OPf_STACKED;
1470 if (right->op_type != OP_MATCH)
1471 left = mod(left, right->op_type);
1472 if (right->op_type == OP_TRANS)
1473 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1475 o = prepend_elem(right->op_type, scalar(left), right);
1477 return newUNOP(OP_NOT, 0, scalar(o));
1481 return bind_match(type, left,
1482 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1490 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1491 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1498 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) {
1499 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1500 o->op_type = OP_LEAVE;
1501 o->op_ppaddr = ppaddr[OP_LEAVE];
1504 if (o->op_type == OP_LINESEQ) {
1506 o->op_type = OP_SCOPE;
1507 o->op_ppaddr = ppaddr[OP_SCOPE];
1508 kid = ((LISTOP*)o)->op_first;
1509 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1510 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1515 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1522 block_start(int full)
1525 int retval = savestack_ix;
1526 SAVEI32(comppad_name_floor);
1528 if ((comppad_name_fill = AvFILLp(comppad_name)) > 0)
1529 comppad_name_floor = comppad_name_fill;
1531 comppad_name_floor = 0;
1533 SAVEI32(min_intro_pending);
1534 SAVEI32(max_intro_pending);
1535 min_intro_pending = 0;
1536 SAVEI32(comppad_name_fill);
1537 SAVEI32(padix_floor);
1538 padix_floor = padix;
1539 pad_reset_pending = FALSE;
1541 hints &= ~HINT_BLOCK_SCOPE;
1546 block_end(I32 floor, OP *seq)
1549 int needblockscope = hints & HINT_BLOCK_SCOPE;
1550 OP* retval = scalarseq(seq);
1552 pad_reset_pending = FALSE;
1554 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1555 pad_leavemy(comppad_name_fill);
1564 OP *o = newOP(OP_THREADSV, 0);
1565 o->op_targ = find_threadsv("_");
1568 return newSVREF(newGVOP(OP_GV, 0, defgv));
1569 #endif /* USE_THREADS */
1577 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o);
1578 eval_start = linklist(eval_root);
1579 eval_root->op_next = 0;
1585 main_root = scope(sawparens(scalarvoid(o)));
1586 curcop = &compiling;
1587 main_start = LINKLIST(main_root);
1588 main_root->op_next = 0;
1592 /* Register with debugger */
1594 CV *cv = perl_get_cv("DB::postponed", FALSE);
1598 XPUSHs((SV*)compiling.cop_filegv);
1600 perl_call_sv((SV*)cv, G_DISCARD);
1607 localize(OP *o, I32 lex)
1609 if (o->op_flags & OPf_PARENS)
1612 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1614 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1615 if (*s == ';' || *s == '=')
1616 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1620 in_my_stash = Nullhv;
1624 return mod(o, OP_NULL); /* a bit kludgey */
1630 if (o->op_type == OP_LIST) {
1633 o2 = newOP(OP_THREADSV, 0);
1634 o2->op_targ = find_threadsv(";");
1636 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1637 #endif /* USE_THREADS */
1638 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1644 fold_constants(register OP *o)
1648 I32 type = o->op_type;
1651 if (opargs[type] & OA_RETSCALAR)
1653 if (opargs[type] & OA_TARGET)
1654 o->op_targ = pad_alloc(type, SVs_PADTMP);
1656 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1657 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1659 if (!(opargs[type] & OA_FOLDCONST))
1674 if (o->op_private & OPpLOCALE)
1679 goto nope; /* Don't try to run w/ errors */
1681 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1682 if (curop->op_type != OP_CONST &&
1683 curop->op_type != OP_LIST &&
1684 curop->op_type != OP_SCALAR &&
1685 curop->op_type != OP_NULL &&
1686 curop->op_type != OP_PUSHMARK) {
1691 curop = LINKLIST(o);
1696 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1697 pad_swipe(o->op_targ);
1698 else if (SvTEMP(sv)) { /* grab mortal temp? */
1699 (void)SvREFCNT_inc(sv);
1703 if (type == OP_RV2GV)
1704 return newGVOP(OP_GV, 0, (GV*)sv);
1706 /* try to smush double to int, but don't smush -2.0 to -2 */
1707 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
1711 if ((double)iv == SvNV(sv)) {
1716 SvIOK_off(sv); /* undo SvIV() damage */
1718 return newSVOP(OP_CONST, 0, sv);
1722 if (!(opargs[type] & OA_OTHERINT))
1725 if (!(hints & HINT_INTEGER)) {
1726 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1729 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1730 if (curop->op_type == OP_CONST) {
1731 if (SvIOK(((SVOP*)curop)->op_sv))
1735 if (opargs[curop->op_type] & OA_RETINTEGER)
1739 o->op_ppaddr = ppaddr[++(o->op_type)];
1746 gen_constant_list(register OP *o)
1750 I32 oldtmps_floor = tmps_floor;
1754 return o; /* Don't attempt to run with errors */
1756 op = curop = LINKLIST(o);
1762 tmps_floor = oldtmps_floor;
1764 o->op_type = OP_RV2AV;
1765 o->op_ppaddr = ppaddr[OP_RV2AV];
1766 curop = ((UNOP*)o)->op_first;
1767 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1774 convert(I32 type, I32 flags, OP *o)
1779 if (!o || o->op_type != OP_LIST)
1780 o = newLISTOP(OP_LIST, 0, o, Nullop);
1782 o->op_flags &= ~OPf_WANT;
1784 if (!(opargs[type] & OA_MARK))
1785 null(cLISTOPo->op_first);
1788 o->op_ppaddr = ppaddr[type];
1789 o->op_flags |= flags;
1791 o = CHECKOP(type, o);
1792 if (o->op_type != type)
1795 if (cLISTOPo->op_children < 7) {
1796 /* XXX do we really need to do this if we're done appending?? */
1797 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1799 cLISTOPo->op_last = last; /* in case check substituted last arg */
1802 return fold_constants(o);
1805 /* List constructors */
1808 append_elem(I32 type, OP *first, OP *last)
1816 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1817 return newLISTOP(type, 0, first, last);
1819 if (first->op_flags & OPf_KIDS)
1820 ((LISTOP*)first)->op_last->op_sibling = last;
1822 first->op_flags |= OPf_KIDS;
1823 ((LISTOP*)first)->op_first = last;
1825 ((LISTOP*)first)->op_last = last;
1826 ((LISTOP*)first)->op_children++;
1831 append_list(I32 type, LISTOP *first, LISTOP *last)
1839 if (first->op_type != type)
1840 return prepend_elem(type, (OP*)first, (OP*)last);
1842 if (last->op_type != type)
1843 return append_elem(type, (OP*)first, (OP*)last);
1845 first->op_last->op_sibling = last->op_first;
1846 first->op_last = last->op_last;
1847 first->op_children += last->op_children;
1848 if (first->op_children)
1849 last->op_flags |= OPf_KIDS;
1856 prepend_elem(I32 type, OP *first, OP *last)
1864 if (last->op_type == type) {
1865 if (type == OP_LIST) { /* already a PUSHMARK there */
1866 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1867 ((LISTOP*)last)->op_first->op_sibling = first;
1870 if (!(last->op_flags & OPf_KIDS)) {
1871 ((LISTOP*)last)->op_last = first;
1872 last->op_flags |= OPf_KIDS;
1874 first->op_sibling = ((LISTOP*)last)->op_first;
1875 ((LISTOP*)last)->op_first = first;
1877 ((LISTOP*)last)->op_children++;
1881 return newLISTOP(type, 0, first, last);
1889 return newOP(OP_STUB, 0);
1895 if (!o || o->op_type != OP_LIST)
1896 o = newLISTOP(OP_LIST, 0, o, Nullop);
1902 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
1906 Newz(1101, listop, 1, LISTOP);
1908 listop->op_type = type;
1909 listop->op_ppaddr = ppaddr[type];
1910 listop->op_children = (first != 0) + (last != 0);
1911 listop->op_flags = flags;
1915 else if (!first && last)
1918 first->op_sibling = last;
1919 listop->op_first = first;
1920 listop->op_last = last;
1921 if (type == OP_LIST) {
1923 pushop = newOP(OP_PUSHMARK, 0);
1924 pushop->op_sibling = first;
1925 listop->op_first = pushop;
1926 listop->op_flags |= OPf_KIDS;
1928 listop->op_last = pushop;
1930 else if (listop->op_children)
1931 listop->op_flags |= OPf_KIDS;
1937 newOP(I32 type, I32 flags)
1940 Newz(1101, o, 1, OP);
1942 o->op_ppaddr = ppaddr[type];
1943 o->op_flags = flags;
1946 o->op_private = 0 + (flags >> 8);
1947 if (opargs[type] & OA_RETSCALAR)
1949 if (opargs[type] & OA_TARGET)
1950 o->op_targ = pad_alloc(type, SVs_PADTMP);
1951 return CHECKOP(type, o);
1955 newUNOP(I32 type, I32 flags, OP *first)
1960 first = newOP(OP_STUB, 0);
1961 if (opargs[type] & OA_MARK)
1962 first = force_list(first);
1964 Newz(1101, unop, 1, UNOP);
1965 unop->op_type = type;
1966 unop->op_ppaddr = ppaddr[type];
1967 unop->op_first = first;
1968 unop->op_flags = flags | OPf_KIDS;
1969 unop->op_private = 1 | (flags >> 8);
1971 if(type == OP_STUDY && first->op_type == OP_MATCH) {
1972 first->op_type = OP_PUSHRE;
1973 first->op_ppaddr = ppaddr[OP_PUSHRE];
1976 unop = (UNOP*) CHECKOP(type, unop);
1980 return fold_constants((OP *) unop);
1984 newBINOP(I32 type, I32 flags, OP *first, OP *last)
1987 Newz(1101, binop, 1, BINOP);
1990 first = newOP(OP_NULL, 0);
1992 binop->op_type = type;
1993 binop->op_ppaddr = ppaddr[type];
1994 binop->op_first = first;
1995 binop->op_flags = flags | OPf_KIDS;
1998 binop->op_private = 1 | (flags >> 8);
2001 binop->op_private = 2 | (flags >> 8);
2002 first->op_sibling = last;
2005 binop = (BINOP*)CHECKOP(type, binop);
2009 binop->op_last = last = binop->op_first->op_sibling;
2011 return fold_constants((OP *)binop);
2015 pmtrans(OP *o, OP *expr, OP *repl)
2017 SV *tstr = ((SVOP*)expr)->op_sv;
2018 SV *rstr = ((SVOP*)repl)->op_sv;
2021 register U8 *t = (U8*)SvPV(tstr, tlen);
2022 register U8 *r = (U8*)SvPV(rstr, rlen);
2028 register short *tbl;
2030 tbl = (short*)cPVOPo->op_pv;
2031 complement = o->op_private & OPpTRANS_COMPLEMENT;
2032 Delete = o->op_private & OPpTRANS_DELETE;
2033 squash = o->op_private & OPpTRANS_SQUASH;
2036 Zero(tbl, 256, short);
2037 for (i = 0; i < tlen; i++)
2039 for (i = 0, j = 0; i < 256; i++) {
2055 if (!rlen && !Delete) {
2058 o->op_private |= OPpTRANS_COUNTONLY;
2060 for (i = 0; i < 256; i++)
2062 for (i = 0, j = 0; i < tlen; i++,j++) {
2065 if (tbl[t[i]] == -1)
2071 if (tbl[t[i]] == -1)
2082 newPMOP(I32 type, I32 flags)
2087 Newz(1101, pmop, 1, PMOP);
2088 pmop->op_type = type;
2089 pmop->op_ppaddr = ppaddr[type];
2090 pmop->op_flags = flags;
2091 pmop->op_private = 0 | (flags >> 8);
2093 if (hints & HINT_LOCALE)
2094 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2096 /* link into pm list */
2097 if (type != OP_TRANS && curstash) {
2098 pmop->op_pmnext = HvPMROOT(curstash);
2099 HvPMROOT(curstash) = pmop;
2106 pmruntime(OP *o, OP *expr, OP *repl)
2110 I32 repl_has_vars = 0;
2112 if (o->op_type == OP_TRANS)
2113 return pmtrans(o, expr, repl);
2115 hints |= HINT_BLOCK_SCOPE;
2118 if (expr->op_type == OP_CONST) {
2120 SV *pat = ((SVOP*)expr)->op_sv;
2121 char *p = SvPV(pat, plen);
2122 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2123 sv_setpvn(pat, "\\s+", 3);
2124 p = SvPV(pat, plen);
2125 pm->op_pmflags |= PMf_SKIPWHITE;
2127 pm->op_pmregexp = pregcomp(p, p + plen, pm);
2128 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2129 pm->op_pmflags |= PMf_WHITE;
2133 if (pm->op_pmflags & PMf_KEEP)
2134 expr = newUNOP(OP_REGCMAYBE,0,expr);
2136 Newz(1101, rcop, 1, LOGOP);
2137 rcop->op_type = OP_REGCOMP;
2138 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2139 rcop->op_first = scalar(expr);
2140 rcop->op_flags |= OPf_KIDS;
2141 rcop->op_private = 1;
2144 /* establish postfix order */
2145 if (pm->op_pmflags & PMf_KEEP) {
2147 rcop->op_next = expr;
2148 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2151 rcop->op_next = LINKLIST(expr);
2152 expr->op_next = (OP*)rcop;
2155 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2160 if (pm->op_pmflags & PMf_EVAL)
2163 else if (repl->op_type == OP_THREADSV
2164 && strchr("&`'123456789+",
2165 threadsv_names[repl->op_targ]))
2169 #endif /* USE_THREADS */
2170 else if (repl->op_type == OP_CONST)
2174 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2175 if (opargs[curop->op_type] & OA_DANGEROUS) {
2177 if (curop->op_type == OP_THREADSV) {
2179 if (strchr("&`'123456789+", curop->op_private))
2183 if (curop->op_type == OP_GV) {
2184 GV *gv = ((GVOP*)curop)->op_gv;
2186 if (strchr("&`'123456789+", *GvENAME(gv)))
2189 #endif /* USE_THREADS */
2190 else if (curop->op_type == OP_RV2CV)
2192 else if (curop->op_type == OP_RV2SV ||
2193 curop->op_type == OP_RV2AV ||
2194 curop->op_type == OP_RV2HV ||
2195 curop->op_type == OP_RV2GV) {
2196 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2199 else if (curop->op_type == OP_PADSV ||
2200 curop->op_type == OP_PADAV ||
2201 curop->op_type == OP_PADHV ||
2202 curop->op_type == OP_PADANY) {
2213 && (!pm->op_pmregexp
2214 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
2215 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2216 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2217 prepend_elem(o->op_type, scalar(repl), o);
2220 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2221 pm->op_pmflags |= PMf_MAYBE_CONST;
2222 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2224 Newz(1101, rcop, 1, LOGOP);
2225 rcop->op_type = OP_SUBSTCONT;
2226 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2227 rcop->op_first = scalar(repl);
2228 rcop->op_flags |= OPf_KIDS;
2229 rcop->op_private = 1;
2232 /* establish postfix order */
2233 rcop->op_next = LINKLIST(repl);
2234 repl->op_next = (OP*)rcop;
2236 pm->op_pmreplroot = scalar((OP*)rcop);
2237 pm->op_pmreplstart = LINKLIST(rcop);
2246 newSVOP(I32 type, I32 flags, SV *sv)
2249 Newz(1101, svop, 1, SVOP);
2250 svop->op_type = type;
2251 svop->op_ppaddr = ppaddr[type];
2253 svop->op_next = (OP*)svop;
2254 svop->op_flags = flags;
2255 if (opargs[type] & OA_RETSCALAR)
2257 if (opargs[type] & OA_TARGET)
2258 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2259 return CHECKOP(type, svop);
2263 newGVOP(I32 type, I32 flags, GV *gv)
2267 Newz(1101, gvop, 1, GVOP);
2268 gvop->op_type = type;
2269 gvop->op_ppaddr = ppaddr[type];
2270 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2271 gvop->op_next = (OP*)gvop;
2272 gvop->op_flags = flags;
2273 if (opargs[type] & OA_RETSCALAR)
2275 if (opargs[type] & OA_TARGET)
2276 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2277 return CHECKOP(type, gvop);
2281 newPVOP(I32 type, I32 flags, char *pv)
2284 Newz(1101, pvop, 1, PVOP);
2285 pvop->op_type = type;
2286 pvop->op_ppaddr = ppaddr[type];
2288 pvop->op_next = (OP*)pvop;
2289 pvop->op_flags = flags;
2290 if (opargs[type] & OA_RETSCALAR)
2292 if (opargs[type] & OA_TARGET)
2293 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2294 return CHECKOP(type, pvop);
2303 save_hptr(&curstash);
2304 save_item(curstname);
2309 name = SvPV(sv, len);
2310 curstash = gv_stashpvn(name,len,TRUE);
2311 sv_setpvn(curstname, name, len);
2315 sv_setpv(curstname,"<none>");
2323 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2331 if (id->op_type != OP_CONST)
2332 croak("Module name must be constant");
2336 if(version != Nullop) {
2337 SV *vesv = ((SVOP*)version)->op_sv;
2339 if (arg == Nullop && !SvNIOK(vesv)) {
2346 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2347 croak("Version number must be constant number");
2349 /* Make copy of id so we don't free it twice */
2350 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2352 /* Fake up a method call to VERSION */
2353 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2354 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2355 append_elem(OP_LIST,
2356 prepend_elem(OP_LIST, pack, list(version)),
2357 newUNOP(OP_METHOD, 0, meth)));
2361 /* Fake up an import/unimport */
2362 if (arg && arg->op_type == OP_STUB)
2363 imop = arg; /* no import on explicit () */
2364 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2365 imop = Nullop; /* use 5.0; */
2368 /* Make copy of id so we don't free it twice */
2369 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2370 meth = newSVOP(OP_CONST, 0,
2372 ? newSVpv("import", 6)
2373 : newSVpv("unimport", 8)
2375 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2376 append_elem(OP_LIST,
2377 prepend_elem(OP_LIST, pack, list(arg)),
2378 newUNOP(OP_METHOD, 0, meth)));
2381 /* Fake up a require */
2382 rqop = newUNOP(OP_REQUIRE, 0, id);
2384 /* Fake up the BEGIN {}, which does its thing immediately. */
2386 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2388 append_elem(OP_LINESEQ,
2389 append_elem(OP_LINESEQ,
2390 newSTATEOP(0, Nullch, rqop),
2391 newSTATEOP(0, Nullch, veop)),
2392 newSTATEOP(0, Nullch, imop) ));
2399 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2401 return newBINOP(OP_LSLICE, flags,
2402 list(force_list(subscript)),
2403 list(force_list(listval)) );
2407 list_assignment(register OP *o)
2412 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2413 o = cUNOPo->op_first;
2415 if (o->op_type == OP_COND_EXPR) {
2416 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2417 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2422 yyerror("Assignment to both a list and a scalar");
2426 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2427 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2428 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2431 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2434 if (o->op_type == OP_RV2SV)
2441 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2446 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2447 return newLOGOP(optype, 0,
2448 mod(scalar(left), optype),
2449 newUNOP(OP_SASSIGN, 0, scalar(right)));
2452 return newBINOP(optype, OPf_STACKED,
2453 mod(scalar(left), optype), scalar(right));
2457 if (list_assignment(left)) {
2460 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2461 left = mod(left, OP_AASSIGN);
2469 o = newBINOP(OP_AASSIGN, flags,
2470 list(force_list(right)),
2471 list(force_list(left)) );
2472 o->op_private = 0 | (flags >> 8);
2473 if (!(left->op_private & OPpLVAL_INTRO)) {
2477 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2478 if (opargs[curop->op_type] & OA_DANGEROUS) {
2479 if (curop->op_type == OP_GV) {
2480 GV *gv = ((GVOP*)curop)->op_gv;
2481 if (gv == defgv || SvCUR(gv) == generation)
2483 SvCUR(gv) = generation;
2485 else if (curop->op_type == OP_PADSV ||
2486 curop->op_type == OP_PADAV ||
2487 curop->op_type == OP_PADHV ||
2488 curop->op_type == OP_PADANY) {
2489 SV **svp = AvARRAY(comppad_name);
2490 SV *sv = svp[curop->op_targ];
2491 if (SvCUR(sv) == generation)
2493 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2495 else if (curop->op_type == OP_RV2CV)
2497 else if (curop->op_type == OP_RV2SV ||
2498 curop->op_type == OP_RV2AV ||
2499 curop->op_type == OP_RV2HV ||
2500 curop->op_type == OP_RV2GV) {
2501 if (lastop->op_type != OP_GV) /* funny deref? */
2510 o->op_private = OPpASSIGN_COMMON;
2512 if (right && right->op_type == OP_SPLIT) {
2514 if ((tmpop = ((LISTOP*)right)->op_first) &&
2515 tmpop->op_type == OP_PUSHRE)
2517 PMOP *pm = (PMOP*)tmpop;
2518 if (left->op_type == OP_RV2AV &&
2519 !(left->op_private & OPpLVAL_INTRO) &&
2520 !(o->op_private & OPpASSIGN_COMMON) )
2522 tmpop = ((UNOP*)left)->op_first;
2523 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2524 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2525 pm->op_pmflags |= PMf_ONCE;
2526 tmpop = cUNOPo->op_first; /* to list (nulled) */
2527 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2528 tmpop->op_sibling = Nullop; /* don't free split */
2529 right->op_next = tmpop->op_next; /* fix starting loc */
2530 op_free(o); /* blow off assign */
2531 right->op_flags &= ~OPf_WANT;
2532 /* "I don't know and I don't care." */
2537 if (modcount < 10000 &&
2538 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2540 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2542 sv_setiv(sv, modcount+1);
2550 right = newOP(OP_UNDEF, 0);
2551 if (right->op_type == OP_READLINE) {
2552 right->op_flags |= OPf_STACKED;
2553 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2556 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2557 o = newBINOP(OP_SASSIGN, flags,
2558 scalar(right), mod(scalar(left), OP_SASSIGN) );
2570 newSTATEOP(I32 flags, char *label, OP *o)
2573 U32 seq = intro_my();
2576 Newz(1101, cop, 1, COP);
2577 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2578 cop->op_type = OP_DBSTATE;
2579 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2582 cop->op_type = OP_NEXTSTATE;
2583 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2585 cop->op_flags = flags;
2586 cop->op_private = 0 | (flags >> 8);
2588 cop->op_private |= NATIVE_HINTS;
2590 cop->op_next = (OP*)cop;
2593 cop->cop_label = label;
2594 hints |= HINT_BLOCK_SCOPE;
2597 cop->cop_arybase = curcop->cop_arybase;
2599 if (copline == NOLINE)
2600 cop->cop_line = curcop->cop_line;
2602 cop->cop_line = copline;
2605 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2606 cop->cop_stash = curstash;
2608 if (PERLDB_LINE && curstash != debstash) {
2609 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2610 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2611 (void)SvIOK_on(*svp);
2613 SvSTASH(*svp) = (HV*)cop;
2617 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2620 /* "Introduce" my variables to visible status. */
2628 if (! min_intro_pending)
2631 svp = AvARRAY(comppad_name);
2632 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2633 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2634 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2635 SvNVX(sv) = (double)cop_seqmax;
2638 min_intro_pending = 0;
2639 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2640 return cop_seqmax++;
2644 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2646 return new_logop(type, flags, &first, &other);
2650 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
2655 OP *first = *firstp;
2656 OP *other = *otherp;
2658 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2659 return newBINOP(type, flags, scalar(first), scalar(other));
2661 scalarboolean(first);
2662 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2663 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2664 if (type == OP_AND || type == OP_OR) {
2670 first = *firstp = cUNOPo->op_first;
2672 first->op_next = o->op_next;
2673 cUNOPo->op_first = Nullop;
2677 if (first->op_type == OP_CONST) {
2678 if (dowarn && (first->op_private & OPpCONST_BARE))
2679 warn("Probable precedence problem on %s", op_desc[type]);
2680 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2691 else if (first->op_type == OP_WANTARRAY) {
2697 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2698 OP *k1 = ((UNOP*)first)->op_first;
2699 OP *k2 = k1->op_sibling;
2701 switch (first->op_type)
2704 if (k2 && k2->op_type == OP_READLINE
2705 && (k2->op_flags & OPf_STACKED)
2706 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
2707 warnop = k2->op_type;
2711 if (k1->op_type == OP_READDIR
2712 || k1->op_type == OP_GLOB
2713 || k1->op_type == OP_EACH)
2714 warnop = k1->op_type;
2718 line_t oldline = curcop->cop_line;
2719 curcop->cop_line = copline;
2720 warn("Value of %s%s can be \"0\"; test with defined()",
2722 ((warnop == OP_READLINE || warnop == OP_GLOB)
2723 ? " construct" : "() operator"));
2724 curcop->cop_line = oldline;
2731 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2732 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2734 Newz(1101, logop, 1, LOGOP);
2736 logop->op_type = type;
2737 logop->op_ppaddr = ppaddr[type];
2738 logop->op_first = first;
2739 logop->op_flags = flags | OPf_KIDS;
2740 logop->op_other = LINKLIST(other);
2741 logop->op_private = 1 | (flags >> 8);
2743 /* establish postfix order */
2744 logop->op_next = LINKLIST(first);
2745 first->op_next = (OP*)logop;
2746 first->op_sibling = other;
2748 o = newUNOP(OP_NULL, 0, (OP*)logop);
2755 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2762 return newLOGOP(OP_AND, 0, first, trueop);
2764 return newLOGOP(OP_OR, 0, first, falseop);
2766 scalarboolean(first);
2767 if (first->op_type == OP_CONST) {
2768 if (SvTRUE(((SVOP*)first)->op_sv)) {
2779 else if (first->op_type == OP_WANTARRAY) {
2783 Newz(1101, condop, 1, CONDOP);
2785 condop->op_type = OP_COND_EXPR;
2786 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2787 condop->op_first = first;
2788 condop->op_flags = flags | OPf_KIDS;
2789 condop->op_true = LINKLIST(trueop);
2790 condop->op_false = LINKLIST(falseop);
2791 condop->op_private = 1 | (flags >> 8);
2793 /* establish postfix order */
2794 condop->op_next = LINKLIST(first);
2795 first->op_next = (OP*)condop;
2797 first->op_sibling = trueop;
2798 trueop->op_sibling = falseop;
2799 o = newUNOP(OP_NULL, 0, (OP*)condop);
2801 trueop->op_next = o;
2802 falseop->op_next = o;
2808 newRANGE(I32 flags, OP *left, OP *right)
2816 Newz(1101, condop, 1, CONDOP);
2818 condop->op_type = OP_RANGE;
2819 condop->op_ppaddr = ppaddr[OP_RANGE];
2820 condop->op_first = left;
2821 condop->op_flags = OPf_KIDS;
2822 condop->op_true = LINKLIST(left);
2823 condop->op_false = LINKLIST(right);
2824 condop->op_private = 1 | (flags >> 8);
2826 left->op_sibling = right;
2828 condop->op_next = (OP*)condop;
2829 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2830 flop = newUNOP(OP_FLOP, 0, flip);
2831 o = newUNOP(OP_NULL, 0, flop);
2834 left->op_next = flip;
2835 right->op_next = flop;
2837 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2838 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2839 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2840 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2842 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2843 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2846 if (!flip->op_private || !flop->op_private)
2847 linklist(o); /* blow off optimizer unless constant */
2853 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2858 int once = block && block->op_flags & OPf_SPECIAL &&
2859 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2862 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2863 return block; /* do {} while 0 does once */
2864 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2865 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2866 expr = newUNOP(OP_DEFINED, 0,
2867 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2868 } else if (expr->op_flags & OPf_KIDS) {
2869 OP *k1 = ((UNOP*)expr)->op_first;
2870 OP *k2 = (k1) ? k1->op_sibling : NULL;
2871 switch (expr->op_type) {
2873 if (k2 && k2->op_type == OP_READLINE
2874 && (k2->op_flags & OPf_STACKED)
2875 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
2876 expr = newUNOP(OP_DEFINED, 0, expr);
2880 if (k1->op_type == OP_READDIR
2881 || k1->op_type == OP_GLOB
2882 || k1->op_type == OP_EACH)
2883 expr = newUNOP(OP_DEFINED, 0, expr);
2889 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2890 o = new_logop(OP_AND, 0, &expr, &listop);
2893 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2895 if (once && o != listop)
2896 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2899 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
2901 o->op_flags |= flags;
2903 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2908 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2917 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2918 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2919 expr = newUNOP(OP_DEFINED, 0,
2920 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2921 } else if (expr && (expr->op_flags & OPf_KIDS)) {
2922 OP *k1 = ((UNOP*)expr)->op_first;
2923 OP *k2 = (k1) ? k1->op_sibling : NULL;
2924 switch (expr->op_type) {
2926 if (k2 && k2->op_type == OP_READLINE
2927 && (k2->op_flags & OPf_STACKED)
2928 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
2929 expr = newUNOP(OP_DEFINED, 0, expr);
2933 if (k1->op_type == OP_READDIR
2934 || k1->op_type == OP_GLOB
2935 || k1->op_type == OP_EACH)
2936 expr = newUNOP(OP_DEFINED, 0, expr);
2942 block = newOP(OP_NULL, 0);
2945 next = LINKLIST(cont);
2947 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2948 if ((line_t)whileline != NOLINE) {
2949 copline = whileline;
2950 cont = append_elem(OP_LINESEQ, cont,
2951 newSTATEOP(0, Nullch, Nullop));
2955 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2956 redo = LINKLIST(listop);
2959 copline = whileline;
2961 o = new_logop(OP_AND, 0, &expr, &listop);
2962 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2963 op_free(expr); /* oops, it's a while (0) */
2965 return Nullop; /* listop already freed by new_logop */
2968 ((LISTOP*)listop)->op_last->op_next = condop =
2969 (o == listop ? redo : LINKLIST(o));
2977 Newz(1101,loop,1,LOOP);
2978 loop->op_type = OP_ENTERLOOP;
2979 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2980 loop->op_private = 0;
2981 loop->op_next = (OP*)loop;
2984 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
2986 loop->op_redoop = redo;
2987 loop->op_lastop = o;
2990 loop->op_nextop = next;
2992 loop->op_nextop = o;
2994 o->op_flags |= flags;
2995 o->op_private |= (flags >> 8);
3000 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
3008 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
3009 sv->op_type = OP_RV2GV;
3010 sv->op_ppaddr = ppaddr[OP_RV2GV];
3012 else if (sv->op_type == OP_PADSV) { /* private variable */
3013 padoff = sv->op_targ;
3017 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
3018 padoff = sv->op_targ;
3019 iterflags |= OPf_SPECIAL;
3024 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
3028 padoff = find_threadsv("_");
3029 iterflags |= OPf_SPECIAL;
3031 sv = newGVOP(OP_GV, 0, defgv);
3034 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
3035 expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
3036 iterflags |= OPf_STACKED;
3038 else if (expr->op_type == OP_NULL &&
3039 (expr->op_flags & OPf_KIDS) &&
3040 ((BINOP*)expr)->op_first->op_type == OP_FLOP)
3042 /* Basically turn for($x..$y) into the same as for($x,$y), but we
3043 * set the STACKED flag to indicate that these values are to be
3044 * treated as min/max values by 'pp_iterinit'.
3046 UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
3047 CONDOP* range = (CONDOP*) flip->op_first;
3048 OP* left = range->op_first;
3049 OP* right = left->op_sibling;
3052 range->op_flags &= ~OPf_KIDS;
3053 range->op_first = Nullop;
3055 list = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
3056 list->op_first->op_next = range->op_true;
3057 left->op_next = range->op_false;
3058 right->op_next = (OP*)list;
3059 list->op_next = list->op_first;
3064 iterflags |= OPf_STACKED;
3067 expr = mod(force_list(expr), OP_GREPSTART);
3071 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
3072 append_elem(OP_LIST, expr, scalar(sv))));
3073 assert(!loop->op_next);
3074 Renew(loop, 1, LOOP);
3075 loop->op_targ = padoff;
3076 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
3078 return newSTATEOP(0, label, wop);
3082 newLOOPEX(I32 type, OP *label)
3086 if (type != OP_GOTO || label->op_type == OP_CONST) {
3087 /* "last()" means "last" */
3088 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
3089 o = newOP(type, OPf_SPECIAL);
3091 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
3092 ? SvPVx(((SVOP*)label)->op_sv, na)
3098 if (label->op_type == OP_ENTERSUB)
3099 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3100 o = newUNOP(type, OPf_STACKED, label);
3102 hints |= HINT_BLOCK_SCOPE;
3112 MUTEX_DESTROY(CvMUTEXP(cv));
3113 Safefree(CvMUTEXP(cv));
3116 #endif /* USE_THREADS */
3118 if (!CvXSUB(cv) && CvROOT(cv)) {
3120 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3121 croak("Can't undef active subroutine");
3124 croak("Can't undef active subroutine");
3125 #endif /* USE_THREADS */
3132 op_free(CvROOT(cv));
3133 CvROOT(cv) = Nullop;
3136 SvPOK_off((SV*)cv); /* forget prototype */
3138 SvREFCNT_dec(CvGV(cv));
3140 SvREFCNT_dec(CvOUTSIDE(cv));
3141 CvOUTSIDE(cv) = Nullcv;
3142 if (CvPADLIST(cv)) {
3143 /* may be during global destruction */
3144 if (SvREFCNT(CvPADLIST(cv))) {
3145 I32 i = AvFILLp(CvPADLIST(cv));
3147 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3148 SV* sv = svp ? *svp : Nullsv;
3151 if (sv == (SV*)comppad_name)
3152 comppad_name = Nullav;
3153 else if (sv == (SV*)comppad) {
3155 curpad = Null(SV**);
3159 SvREFCNT_dec((SV*)CvPADLIST(cv));
3161 CvPADLIST(cv) = Nullav;
3165 #ifdef DEBUG_CLOSURES
3170 CV *outside = CvOUTSIDE(cv);
3171 AV* padlist = CvPADLIST(cv);
3178 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3180 (CvANON(cv) ? "ANON"
3181 : (cv == main_cv) ? "MAIN"
3182 : CvUNIQUE(outside) ? "UNIQUE"
3183 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3186 : CvANON(outside) ? "ANON"
3187 : (outside == main_cv) ? "MAIN"
3188 : CvUNIQUE(outside) ? "UNIQUE"
3189 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3194 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3195 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3196 pname = AvARRAY(pad_name);
3197 ppad = AvARRAY(pad);
3199 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3200 if (SvPOK(pname[ix]))
3201 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3203 SvFAKE(pname[ix]) ? "FAKE " : "",
3205 (long)I_32(SvNVX(pname[ix])),
3206 (long)SvIVX(pname[ix]));
3209 #endif /* DEBUG_CLOSURES */
3212 cv_clone2(CV *proto, CV *outside)
3217 AV* protopadlist = CvPADLIST(proto);
3218 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3219 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3220 SV** pname = AvARRAY(protopad_name);
3221 SV** ppad = AvARRAY(protopad);
3222 I32 fname = AvFILLp(protopad_name);
3223 I32 fpad = AvFILLp(protopad);
3227 assert(!CvUNIQUE(proto));
3232 SAVESPTR(comppad_name);
3235 cv = compcv = (CV*)NEWSV(1104,0);
3236 sv_upgrade((SV *)cv, SvTYPE(proto));
3242 New(666, CvMUTEXP(cv), 1, perl_mutex);
3243 MUTEX_INIT(CvMUTEXP(cv));
3245 #endif /* USE_THREADS */
3246 CvFILEGV(cv) = CvFILEGV(proto);
3247 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3248 CvSTASH(cv) = CvSTASH(proto);
3249 CvROOT(cv) = CvROOT(proto);
3250 CvSTART(cv) = CvSTART(proto);
3252 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3255 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3257 comppad_name = newAV();
3258 for (ix = fname; ix >= 0; ix--)
3259 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3263 comppadlist = newAV();
3264 AvREAL_off(comppadlist);
3265 av_store(comppadlist, 0, (SV*)comppad_name);
3266 av_store(comppadlist, 1, (SV*)comppad);
3267 CvPADLIST(cv) = comppadlist;
3268 av_fill(comppad, AvFILLp(protopad));
3269 curpad = AvARRAY(comppad);
3271 av = newAV(); /* will be @_ */
3273 av_store(comppad, 0, (SV*)av);
3274 AvFLAGS(av) = AVf_REIFY;
3276 for (ix = fpad; ix > 0; ix--) {
3277 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3278 if (namesv && namesv != &sv_undef) {
3279 char *name = SvPVX(namesv); /* XXX */
3280 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3281 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3282 CvOUTSIDE(cv), cxstack_ix);
3284 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3286 croak("panic: cv_clone: %s", name);
3288 else { /* our own lexical */
3291 /* anon code -- we'll come back for it */
3292 sv = SvREFCNT_inc(ppad[ix]);
3294 else if (*name == '@')
3296 else if (*name == '%')
3306 SV* sv = NEWSV(0,0);
3312 /* Now that vars are all in place, clone nested closures. */
3314 for (ix = fpad; ix > 0; ix--) {
3315 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3317 && namesv != &sv_undef
3318 && !(SvFLAGS(namesv) & SVf_FAKE)
3319 && *SvPVX(namesv) == '&'
3320 && CvCLONE(ppad[ix]))
3322 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3323 SvREFCNT_dec(ppad[ix]);
3326 curpad[ix] = (SV*)kid;
3330 #ifdef DEBUG_CLOSURES
3331 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3333 PerlIO_printf(Perl_debug_log, " from:\n");
3335 PerlIO_printf(Perl_debug_log, " to:\n");
3346 return cv_clone2(proto, CvOUTSIDE(proto));
3350 cv_ckproto(CV *cv, GV *gv, char *p)
3352 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3353 SV* msg = sv_newmortal();
3357 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3358 sv_setpv(msg, "Prototype mismatch:");
3360 sv_catpvf(msg, " sub %_", name);
3362 sv_catpvf(msg, " (%s)", SvPVX(cv));
3363 sv_catpv(msg, " vs ");
3365 sv_catpvf(msg, "(%s)", p);
3367 sv_catpv(msg, "none");
3375 if (!cv || !SvPOK(cv) || SvCUR(cv))
3377 return op_const_sv(CvSTART(cv), cv);
3381 op_const_sv(OP *o, CV *cv)
3388 if(o->op_type == OP_LINESEQ && cLISTOPo->op_first)
3389 o = cLISTOPo->op_first->op_sibling;
3391 for (; o; o = o->op_next) {
3392 OPCODE type = o->op_type;
3394 if(sv && o->op_next == o)
3396 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3398 if (type == OP_LEAVESUB || type == OP_RETURN)
3402 if (type == OP_CONST)
3404 else if (type == OP_PADSV && cv) {
3405 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3406 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3407 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3419 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3422 char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3423 GV *gv = gv_fetchpv(name ? name : "__ANON__",
3424 GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
3425 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3434 if (SvTYPE(gv) != SVt_PVGV) { /* Prototype now, and had
3435 maximum a prototype before. */
3436 if (SvTYPE(gv) > SVt_NULL) {
3437 if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1))
3438 warn("Runaway prototype");
3439 cv_ckproto((CV*)gv, NULL, ps);
3442 sv_setpv((SV*)gv, ps);
3444 sv_setiv((SV*)gv, -1);
3445 SvREFCNT_dec(compcv);
3451 if (!name || GvCVGEN(gv))
3453 else if (cv = GvCV(gv)) {
3454 cv_ckproto(cv, gv, ps);
3455 /* already defined (or promised)? */
3456 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3458 bool const_changed = TRUE;
3460 /* just a "sub foo;" when &foo is already defined */
3464 /* ahem, death to those who redefine active sort subs */
3465 if (curstackinfo->si_type == SI_SORT && sortcop == CvSTART(cv))
3466 croak("Can't redefine active sort subroutine %s", name);
3467 if(const_sv = cv_const_sv(cv))
3468 const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
3469 if ((const_sv && const_changed) || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3470 && HvNAME(GvSTASH(CvGV(cv)))
3471 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3473 line_t oldline = curcop->cop_line;
3474 curcop->cop_line = copline;
3475 warn(const_sv ? "Constant subroutine %s redefined"
3476 : "Subroutine %s redefined", name);
3477 curcop->cop_line = oldline;
3483 if (cv) { /* must reuse cv if autoloaded */
3485 CvFLAGS(cv) = CvFLAGS(compcv);
3486 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3487 CvOUTSIDE(compcv) = 0;
3488 CvPADLIST(cv) = CvPADLIST(compcv);
3489 CvPADLIST(compcv) = 0;
3490 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3491 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3492 SvREFCNT_dec(compcv);
3502 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3503 CvFILEGV(cv) = curcop->cop_filegv;
3504 CvSTASH(cv) = curstash;
3507 New(666, CvMUTEXP(cv), 1, perl_mutex);
3508 MUTEX_INIT(CvMUTEXP(cv));
3509 #endif /* USE_THREADS */
3512 sv_setpv((SV*)cv, ps);
3518 char *s = strrchr(name, ':');
3520 if (strEQ(s, "BEGIN")) {
3522 "BEGIN not safe after errors--compilation aborted";
3526 /* force display of errors found but not reported */
3527 sv_catpv(ERRSV, not_safe);
3528 croak("%s", SvPVx(ERRSV, na));
3540 if (AvFILLp(comppad_name) < AvFILLp(comppad))
3541 av_store(comppad_name, AvFILLp(comppad), Nullsv);
3544 SV **namep = AvARRAY(comppad_name);
3545 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3548 if (SvIMMORTAL(curpad[ix]))
3551 * The only things that a clonable function needs in its
3552 * pad are references to outer lexicals and anonymous subs.
3553 * The rest are created anew during cloning.
3555 if (!((namesv = namep[ix]) != Nullsv &&
3556 namesv != &sv_undef &&
3558 *SvPVX(namesv) == '&')))
3560 SvREFCNT_dec(curpad[ix]);
3561 curpad[ix] = Nullsv;
3566 AV *av = newAV(); /* Will be @_ */
3568 av_store(comppad, 0, (SV*)av);
3569 AvFLAGS(av) = AVf_REIFY;
3571 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3572 if (SvIMMORTAL(curpad[ix]))
3574 if (!SvPADMY(curpad[ix]))
3575 SvPADTMP_on(curpad[ix]);
3579 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3580 CvSTART(cv) = LINKLIST(CvROOT(cv));
3581 CvROOT(cv)->op_next = 0;
3587 if (PERLDB_SUBLINE && curstash != debstash) {
3588 SV *sv = NEWSV(0,0);
3589 SV *tmpstr = sv_newmortal();
3590 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3594 sv_setpvf(sv, "%_:%ld-%ld",
3595 GvSV(curcop->cop_filegv),
3596 (long)subline, (long)curcop->cop_line);
3597 gv_efullname3(tmpstr, gv, Nullch);
3598 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3599 hv = GvHVn(db_postponed);
3600 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3601 && (cv = GvCV(db_postponed))) {
3606 perl_call_sv((SV*)cv, G_DISCARD);
3610 if ((s = strrchr(name,':')))
3614 if (strEQ(s, "BEGIN")) {
3615 I32 oldscope = scopestack_ix;
3617 SAVESPTR(compiling.cop_filegv);
3618 SAVEI16(compiling.cop_line);
3624 DEBUG_x( dump_sub(gv) );
3625 av_push(beginav, (SV *)cv);
3627 call_list(oldscope, beginav);
3629 curcop = &compiling;
3632 else if (strEQ(s, "END") && !error_count) {
3635 av_unshift(endav, 1);
3636 av_store(endav, 0, (SV *)cv);
3639 else if (strEQ(s, "INIT") && !error_count) {
3642 av_push(initav, SvREFCNT_inc(cv));
3653 newCONSTSUB(HV *stash, char *name, SV *sv)
3656 U32 oldhints = hints;
3657 HV *old_cop_stash = curcop->cop_stash;
3658 HV *old_curstash = curstash;
3659 line_t oldline = curcop->cop_line;
3660 curcop->cop_line = copline;
3662 hints &= ~HINT_BLOCK_SCOPE;
3664 curstash = curcop->cop_stash = stash;
3667 start_subparse(FALSE, 0),
3668 newSVOP(OP_CONST, 0, newSVpv(name,0)),
3669 newSVOP(OP_CONST, 0, &sv_no), /* SvPV(&sv_no) == "" -- GMB */
3670 newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
3674 curcop->cop_stash = old_cop_stash;
3675 curstash = old_curstash;
3676 curcop->cop_line = oldline;
3680 newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
3683 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3686 if (cv = (name ? GvCV(gv) : Nullcv)) {
3688 /* just a cached method */
3692 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3693 /* already defined (or promised) */
3694 if (dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3695 && HvNAME(GvSTASH(CvGV(cv)))
3696 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
3697 line_t oldline = curcop->cop_line;
3698 curcop->cop_line = copline;
3699 warn("Subroutine %s redefined",name);
3700 curcop->cop_line = oldline;
3707 if (cv) /* must reuse cv if autoloaded */
3710 cv = (CV*)NEWSV(1105,0);
3711 sv_upgrade((SV *)cv, SVt_PVCV);
3718 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3720 New(666, CvMUTEXP(cv), 1, perl_mutex);
3721 MUTEX_INIT(CvMUTEXP(cv));
3723 #endif /* USE_THREADS */
3724 CvFILEGV(cv) = gv_fetchfile(filename);
3725 CvXSUB(cv) = subaddr;
3728 char *s = strrchr(name,':');
3733 if (strEQ(s, "BEGIN")) {
3736 av_push(beginav, (SV *)cv);
3739 else if (strEQ(s, "END")) {
3742 av_unshift(endav, 1);
3743 av_store(endav, 0, (SV *)cv);
3746 else if (strEQ(s, "INIT")) {
3749 av_push(initav, (SV *)cv);
3759 newFORM(I32 floor, OP *o, OP *block)
3768 name = SvPVx(cSVOPo->op_sv, na);
3771 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3773 if (cv = GvFORM(gv)) {
3775 line_t oldline = curcop->cop_line;
3777 curcop->cop_line = copline;
3778 warn("Format %s redefined",name);
3779 curcop->cop_line = oldline;
3785 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3786 CvFILEGV(cv) = curcop->cop_filegv;
3788 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3789 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3790 SvPADTMP_on(curpad[ix]);
3793 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3794 CvSTART(cv) = LINKLIST(CvROOT(cv));
3795 CvROOT(cv)->op_next = 0;
3805 return newUNOP(OP_REFGEN, 0,
3806 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3812 return newUNOP(OP_REFGEN, 0,
3813 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3817 newANONSUB(I32 floor, OP *proto, OP *block)
3819 return newUNOP(OP_REFGEN, 0,
3820 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3826 switch (o->op_type) {
3828 o->op_type = OP_PADAV;
3829 o->op_ppaddr = ppaddr[OP_PADAV];
3830 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3833 o->op_type = OP_RV2AV;
3834 o->op_ppaddr = ppaddr[OP_RV2AV];
3839 warn("oops: oopsAV");
3848 switch (o->op_type) {
3851 o->op_type = OP_PADHV;
3852 o->op_ppaddr = ppaddr[OP_PADHV];
3853 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3857 o->op_type = OP_RV2HV;
3858 o->op_ppaddr = ppaddr[OP_RV2HV];
3863 warn("oops: oopsHV");
3872 if (o->op_type == OP_PADANY) {
3873 o->op_type = OP_PADAV;
3874 o->op_ppaddr = ppaddr[OP_PADAV];
3877 return newUNOP(OP_RV2AV, 0, scalar(o));
3881 newGVREF(I32 type, OP *o)
3883 if (type == OP_MAPSTART)
3884 return newUNOP(OP_NULL, 0, o);
3885 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3891 if (o->op_type == OP_PADANY) {
3892 o->op_type = OP_PADHV;
3893 o->op_ppaddr = ppaddr[OP_PADHV];
3896 return newUNOP(OP_RV2HV, 0, scalar(o));
3902 croak("NOT IMPL LINE %d",__LINE__);
3908 newCVREF(I32 flags, OP *o)
3910 return newUNOP(OP_RV2CV, flags, scalar(o));
3916 if (o->op_type == OP_PADANY) {
3917 o->op_type = OP_PADSV;
3918 o->op_ppaddr = ppaddr[OP_PADSV];
3921 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3922 o->op_flags |= OPpDONE_SVREF;
3925 return newUNOP(OP_RV2SV, 0, scalar(o));
3928 /* Check routines. */
3936 name = NEWSV(1106,0);
3937 sv_upgrade(name, SVt_PVNV);
3938 sv_setpvn(name, "&", 1);
3941 ix = pad_alloc(o->op_type, SVs_PADMY);
3942 av_store(comppad_name, ix, name);
3943 av_store(comppad, ix, cSVOPo->op_sv);
3944 SvPADMY_on(cSVOPo->op_sv);
3945 cSVOPo->op_sv = Nullsv;
3946 cSVOPo->op_targ = ix;
3953 o->op_private = hints;
3960 if (cUNOPo->op_first->op_type == OP_CONCAT)
3961 o->op_flags |= OPf_STACKED;
3968 if (o->op_flags & OPf_KIDS) {
3971 OPCODE type = o->op_type;
3972 o = modkids(ck_fun(o), type);
3973 kid = cUNOPo->op_first;
3974 newop = kUNOP->op_first->op_sibling;
3976 (newop->op_sibling ||
3977 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3978 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3979 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3983 op_free(kUNOP->op_first);
3984 kUNOP->op_first = newop;
3986 o->op_ppaddr = ppaddr[++o->op_type];
3995 if (o->op_flags & OPf_KIDS) {
3996 OP *kid = cUNOPo->op_first;
3997 if (kid->op_type == OP_HSLICE)
3998 o->op_private |= OPpSLICE;
3999 else if (kid->op_type != OP_HELEM)
4000 croak("%s argument is not a HASH element or slice",
4001 op_desc[o->op_type]);
4010 I32 type = o->op_type;
4012 if (o->op_flags & OPf_KIDS) {
4013 if (cLISTOPo->op_first->op_type == OP_STUB) {
4015 o = newUNOP(type, OPf_SPECIAL,
4016 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
4026 hints |= HINT_BLOCK_SCOPE;
4027 if (o->op_flags & OPf_KIDS) {
4028 SVOP *kid = (SVOP*)cUNOPo->op_first;
4031 o->op_flags &= ~OPf_KIDS;
4034 else if (kid->op_type == OP_LINESEQ) {
4037 kid->op_next = o->op_next;
4038 cUNOPo->op_first = 0;
4041 Newz(1101, enter, 1, LOGOP);
4042 enter->op_type = OP_ENTERTRY;
4043 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
4044 enter->op_private = 0;
4046 /* establish postfix order */
4047 enter->op_next = (OP*)enter;
4049 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
4050 o->op_type = OP_LEAVETRY;
4051 o->op_ppaddr = ppaddr[OP_LEAVETRY];
4052 enter->op_other = o;
4060 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
4062 o->op_targ = (PADOFFSET)hints;
4070 if (o->op_flags & OPf_STACKED) {
4072 kid = cUNOPo->op_first->op_sibling;
4073 if (kid->op_type == OP_RV2GV)
4085 if (o->op_flags & OPf_KIDS) {
4086 OP *kid = cUNOPo->op_first;
4087 if (kid->op_type != OP_HELEM)
4088 croak("%s argument is not a HASH element", op_desc[o->op_type]);
4095 ck_gvconst(register OP *o)
4097 o = fold_constants(o);
4098 if (o->op_type == OP_CONST)
4104 ck_rvconst(register OP *o)
4107 SVOP *kid = (SVOP*)cUNOPo->op_first;
4109 o->op_private |= (hints & HINT_STRICT_REFS);
4110 if (kid->op_type == OP_CONST) {
4115 name = SvPV(kid->op_sv, na);
4116 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
4117 char *badthing = Nullch;
4118 switch (o->op_type) {
4120 badthing = "a SCALAR";
4123 badthing = "an ARRAY";
4126 badthing = "a HASH";
4131 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
4135 * This is a little tricky. We only want to add the symbol if we
4136 * didn't add it in the lexer. Otherwise we get duplicate strict
4137 * warnings. But if we didn't add it in the lexer, we must at
4138 * least pretend like we wanted to add it even if it existed before,
4139 * or we get possible typo warnings. OPpCONST_ENTERED says
4140 * whether the lexer already added THIS instance of this symbol.
4142 iscv = (o->op_type == OP_RV2CV) * 2;
4144 gv = gv_fetchpv(name,
4145 iscv | !(kid->op_private & OPpCONST_ENTERED),
4148 : o->op_type == OP_RV2SV
4150 : o->op_type == OP_RV2AV
4152 : o->op_type == OP_RV2HV
4155 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4157 kid->op_type = OP_GV;
4158 SvREFCNT_dec(kid->op_sv);
4159 kid->op_sv = SvREFCNT_inc(gv);
4169 I32 type = o->op_type;
4171 if (o->op_flags & OPf_REF)
4174 if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
4175 SVOP *kid = (SVOP*)cUNOPo->op_first;
4177 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4178 OP *newop = newGVOP(type, OPf_REF,
4179 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
4186 if (type == OP_FTTTY)
4187 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4190 return newUNOP(type, 0, newDEFSVOP());
4203 int type = o->op_type;
4204 register I32 oa = opargs[type] >> OASHIFT;
4206 if (o->op_flags & OPf_STACKED) {
4207 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4210 return no_fh_allowed(o);
4213 if (o->op_flags & OPf_KIDS) {
4214 tokid = &cLISTOPo->op_first;
4215 kid = cLISTOPo->op_first;
4216 if (kid->op_type == OP_PUSHMARK ||
4217 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4219 tokid = &kid->op_sibling;
4220 kid = kid->op_sibling;
4222 if (!kid && opargs[type] & OA_DEFGV)
4223 *tokid = kid = newDEFSVOP();
4227 sibl = kid->op_sibling;
4241 if (kid->op_type == OP_CONST &&
4242 (kid->op_private & OPpCONST_BARE)) {
4243 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4244 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4245 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4247 warn("Array @%s missing the @ in argument %ld of %s()",
4248 name, (long)numargs, op_desc[type]);
4251 kid->op_sibling = sibl;
4254 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4255 bad_type(numargs, "array", op_desc[o->op_type], kid);
4259 if (kid->op_type == OP_CONST &&
4260 (kid->op_private & OPpCONST_BARE)) {
4261 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4262 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4263 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4265 warn("Hash %%%s missing the %% in argument %ld of %s()",
4266 name, (long)numargs, op_desc[type]);
4269 kid->op_sibling = sibl;
4272 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4273 bad_type(numargs, "hash", op_desc[o->op_type], kid);
4278 OP *newop = newUNOP(OP_NULL, 0, kid);
4279 kid->op_sibling = 0;
4281 newop->op_next = newop;
4283 kid->op_sibling = sibl;
4288 if (kid->op_type != OP_GV) {
4289 if (kid->op_type == OP_CONST &&
4290 (kid->op_private & OPpCONST_BARE)) {
4291 OP *newop = newGVOP(OP_GV, 0,
4292 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4298 kid->op_sibling = 0;
4299 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4301 kid->op_sibling = sibl;
4307 mod(scalar(kid), type);
4311 tokid = &kid->op_sibling;
4312 kid = kid->op_sibling;
4314 o->op_private |= numargs;
4316 return too_many_arguments(o,op_desc[o->op_type]);
4319 else if (opargs[type] & OA_DEFGV) {
4321 return newUNOP(type, 0, newDEFSVOP());
4325 while (oa & OA_OPTIONAL)
4327 if (oa && oa != OA_LIST)
4328 return too_few_arguments(o,op_desc[o->op_type]);
4338 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4339 append_elem(OP_GLOB, o, newDEFSVOP());
4341 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4342 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4344 if (gv && GvIMPORTED_CV(gv)) {
4345 static int glob_index;
4347 append_elem(OP_GLOB, o,
4348 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4349 o->op_type = OP_LIST;
4350 o->op_ppaddr = ppaddr[OP_LIST];
4351 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4352 cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4353 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4354 append_elem(OP_LIST, o,
4355 scalar(newUNOP(OP_RV2CV, 0,
4356 newGVOP(OP_GV, 0, gv)))));
4357 o = newUNOP(OP_NULL, 0, ck_subr(o));
4358 o->op_targ = OP_GLOB; /* hint at what it used to be */
4361 gv = newGVgen("main");
4363 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4373 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4375 o->op_ppaddr = ppaddr[OP_GREPSTART];
4376 Newz(1101, gwop, 1, LOGOP);
4378 if (o->op_flags & OPf_STACKED) {
4381 kid = cLISTOPo->op_first->op_sibling;
4382 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4385 kid->op_next = (OP*)gwop;
4386 o->op_flags &= ~OPf_STACKED;
4388 kid = cLISTOPo->op_first->op_sibling;
4389 if (type == OP_MAPWHILE)
4396 kid = cLISTOPo->op_first->op_sibling;
4397 if (kid->op_type != OP_NULL)
4398 croak("panic: ck_grep");
4399 kid = kUNOP->op_first;
4401 gwop->op_type = type;
4402 gwop->op_ppaddr = ppaddr[type];
4403 gwop->op_first = listkids(o);
4404 gwop->op_flags |= OPf_KIDS;
4405 gwop->op_private = 1;
4406 gwop->op_other = LINKLIST(kid);
4407 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4408 kid->op_next = (OP*)gwop;
4410 kid = cLISTOPo->op_first->op_sibling;
4411 if (!kid || !kid->op_sibling)
4412 return too_few_arguments(o,op_desc[o->op_type]);
4413 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4414 mod(kid, OP_GREPSTART);
4422 if (o->op_flags & OPf_KIDS) {
4423 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4424 if (kid && kid->op_type == OP_CONST)
4425 fbm_compile(((SVOP*)kid)->op_sv, 0);
4431 ck_lengthconst(OP *o)
4433 /* XXX length optimization goes here */
4440 OPCODE type = o->op_type;
4441 return modkids(ck_fun(o), type);
4447 OPCODE type = o->op_type;
4448 return refkids(ck_fun(o), type);
4456 kid = cLISTOPo->op_first;
4459 kid = cLISTOPo->op_first;
4461 if (kid->op_type == OP_PUSHMARK)
4462 kid = kid->op_sibling;
4463 if (kid && o->op_flags & OPf_STACKED)
4464 kid = kid->op_sibling;
4465 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4466 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4467 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4468 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4469 cLISTOPo->op_first->op_sibling = kid;
4470 cLISTOPo->op_last = kid;
4471 kid = kid->op_sibling;
4476 append_elem(o->op_type, o, newDEFSVOP());
4482 if (hints & HINT_LOCALE)
4483 o->op_private |= OPpLOCALE;
4490 ck_fun_locale(OP *o)
4496 if (hints & HINT_LOCALE)
4497 o->op_private |= OPpLOCALE;
4508 if (hints & HINT_LOCALE)
4509 o->op_private |= OPpLOCALE;
4518 o->op_private |= OPpRUNTIME;
4531 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4532 o->op_private |= OPpREPEAT_DOLIST;
4533 cBINOPo->op_first = force_list(cBINOPo->op_first);
4543 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4544 SVOP *kid = (SVOP*)cUNOPo->op_first;
4546 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4548 for (s = SvPVX(kid->op_sv); *s; s++) {
4549 if (*s == ':' && s[1] == ':') {
4551 Move(s+2, s+1, strlen(s+2)+1, char);
4552 --SvCUR(kid->op_sv);
4555 sv_catpvn(kid->op_sv, ".pm", 3);
4564 croak("NOT IMPL LINE %d",__LINE__);
4573 if (o->op_flags & OPf_KIDS) {
4574 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4575 if (kid && kid->op_sibling) {
4576 o->op_type = OP_SSELECT;
4577 o->op_ppaddr = ppaddr[OP_SSELECT];
4579 return fold_constants(o);
4583 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4584 if (kid && kid->op_type == OP_RV2GV)
4585 kid->op_private &= ~HINT_STRICT_REFS;
4592 I32 type = o->op_type;
4594 if (!(o->op_flags & OPf_KIDS)) {
4599 if (!CvUNIQUE(compcv)) {
4600 argop = newOP(OP_PADAV, OPf_REF);
4601 argop->op_targ = 0; /* curpad[0] is @_ */
4604 argop = newUNOP(OP_RV2AV, 0,
4605 scalar(newGVOP(OP_GV, 0,
4606 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4609 argop = newUNOP(OP_RV2AV, 0,
4610 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4611 defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4612 #endif /* USE_THREADS */
4613 return newUNOP(type, 0, scalar(argop));
4615 return scalar(modkids(ck_fun(o), type));
4623 if (hints & HINT_LOCALE)
4624 o->op_private |= OPpLOCALE;
4627 if (o->op_flags & OPf_STACKED) {
4628 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4630 kid = kUNOP->op_first; /* get past rv2gv */
4632 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4634 if (kid->op_type == OP_SCOPE) {
4638 else if (kid->op_type == OP_LEAVE) {
4639 if (o->op_type == OP_SORT) {
4640 null(kid); /* wipe out leave */
4643 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4644 if (k->op_next == kid)
4649 kid->op_next = 0; /* just disconnect the leave */
4650 k = kLISTOP->op_first;
4654 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4655 null(kid); /* wipe out rv2gv */
4656 if (o->op_type == OP_SORT)
4660 o->op_flags |= OPf_SPECIAL;
4672 if (o->op_flags & OPf_STACKED)
4673 return no_fh_allowed(o);
4675 kid = cLISTOPo->op_first;
4676 if (kid->op_type != OP_NULL)
4677 croak("panic: ck_split");
4678 kid = kid->op_sibling;
4679 op_free(cLISTOPo->op_first);
4680 cLISTOPo->op_first = kid;
4682 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4683 cLISTOPo->op_last = kid; /* There was only one element previously */
4686 if (kid->op_type != OP_MATCH) {
4687 OP *sibl = kid->op_sibling;
4688 kid->op_sibling = 0;
4689 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4690 if (cLISTOPo->op_first == cLISTOPo->op_last)
4691 cLISTOPo->op_last = kid;
4692 cLISTOPo->op_first = kid;
4693 kid->op_sibling = sibl;
4696 kid->op_type = OP_PUSHRE;
4697 kid->op_ppaddr = ppaddr[OP_PUSHRE];
4700 if (!kid->op_sibling)
4701 append_elem(OP_SPLIT, o, newDEFSVOP());
4703 kid = kid->op_sibling;
4706 if (!kid->op_sibling)
4707 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4709 kid = kid->op_sibling;
4712 if (kid->op_sibling)
4713 return too_many_arguments(o,op_desc[o->op_type]);
4722 OP *prev = ((cUNOPo->op_first->op_sibling)
4723 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4724 OP *o2 = prev->op_sibling;
4732 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4733 if (cvop->op_type == OP_RV2CV) {
4735 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4736 null(cvop); /* disable rv2cv */
4737 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4738 if (tmpop->op_type == OP_GV) {
4739 cv = GvCVu(tmpop->op_sv);
4740 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4741 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4742 proto = SvPV((SV*)cv, na);
4746 o->op_private |= (hints & HINT_STRICT_REFS);
4747 if (PERLDB_SUB && curstash != debstash)
4748 o->op_private |= OPpENTERSUB_DB;
4749 while (o2 != cvop) {
4753 return too_many_arguments(o, gv_ename(namegv));
4771 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4772 bad_type(arg, "block", gv_ename(namegv), o2);
4777 if (o2->op_type == OP_RV2GV)
4781 OP* sib = kid->op_sibling;
4782 kid->op_sibling = 0;
4783 o2 = newUNOP(OP_RV2GV, 0, kid);
4784 o2->op_sibling = sib;
4785 prev->op_sibling = o2;
4793 if (o2->op_type != OP_RV2GV)
4794 bad_type(arg, "symbol", gv_ename(namegv), o2);
4797 if (o2->op_type != OP_RV2CV)
4798 bad_type(arg, "sub", gv_ename(namegv), o2);
4801 if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4802 bad_type(arg, "scalar", gv_ename(namegv), o2);
4805 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4806 bad_type(arg, "array", gv_ename(namegv), o2);
4809 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4810 bad_type(arg, "hash", gv_ename(namegv), o2);
4814 OP* sib = kid->op_sibling;
4815 kid->op_sibling = 0;
4816 o2 = newUNOP(OP_REFGEN, 0, kid);
4817 o2->op_sibling = sib;
4818 prev->op_sibling = o2;
4829 croak("Malformed prototype for %s: %s",
4830 gv_ename(namegv), SvPV((SV*)cv, na));
4835 mod(o2, OP_ENTERSUB);
4837 o2 = o2->op_sibling;
4839 if (proto && !optional &&
4840 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4841 return too_few_arguments(o, gv_ename(namegv));
4848 SvREADONLY_on(cSVOPo->op_sv);
4855 if (o->op_flags & OPf_KIDS) {
4856 SVOP *kid = (SVOP*)cUNOPo->op_first;
4858 if (kid->op_type == OP_NULL)
4859 kid = (SVOP*)kid->op_sibling;
4861 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4862 o->op_flags |= OPf_SPECIAL;
4867 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4870 peep(register OP *o)
4873 register OP* oldop = 0;
4874 if (!o || o->op_seq)
4879 for (; o; o = o->op_next) {
4885 switch (o->op_type) {
4888 curcop = ((COP*)o); /* for warnings */
4889 o->op_seq = op_seqmax++;
4900 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4902 o->op_seq = op_seqmax++;
4905 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4906 o->op_seq = op_seqmax++;
4907 break; /* Scalar stub must produce undef. List stub is noop */
4911 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4918 if (oldop && o->op_next) {
4919 oldop->op_next = o->op_next;
4922 o->op_seq = op_seqmax++;
4926 if (o->op_next->op_type == OP_RV2SV) {
4927 if (!(o->op_next->op_private & OPpDEREF)) {
4929 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4930 o->op_next = o->op_next->op_next;
4931 o->op_type = OP_GVSV;
4932 o->op_ppaddr = ppaddr[OP_GVSV];
4935 else if (o->op_next->op_type == OP_RV2AV) {
4936 OP* pop = o->op_next->op_next;
4938 if (pop->op_type == OP_CONST &&
4939 (op = pop->op_next) &&
4940 pop->op_next->op_type == OP_AELEM &&
4941 !(pop->op_next->op_private &
4942 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4943 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4947 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4951 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4952 o->op_next = pop->op_next->op_next;
4953 o->op_type = OP_AELEMFAST;
4954 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4955 o->op_private = (U8)i;
4956 GvAVn(((GVOP*)o)->op_gv);
4959 o->op_seq = op_seqmax++;
4963 if (o->op_next->op_type == OP_RV2AV
4964 && (o->op_next->op_flags && OPf_REF))
4967 o->op_next = o->op_next->op_next;
4972 if (o->op_next->op_type == OP_RV2HV
4973 && (o->op_next->op_flags && OPf_REF))
4976 o->op_next = o->op_next->op_next;
4984 o->op_seq = op_seqmax++;
4985 while (cLOGOP->op_other->op_type == OP_NULL)
4986 cLOGOP->op_other = cLOGOP->op_other->op_next;
4987 peep(cLOGOP->op_other);
4991 o->op_seq = op_seqmax++;
4992 peep(cCONDOP->op_true);
4993 peep(cCONDOP->op_false);
4997 o->op_seq = op_seqmax++;
4998 peep(cLOOP->op_redoop);
4999 peep(cLOOP->op_nextop);
5000 peep(cLOOP->op_lastop);
5005 o->op_seq = op_seqmax++;
5006 peep(cPMOP->op_pmreplstart);
5010 o->op_seq = op_seqmax++;
5011 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
5012 if (o->op_next->op_sibling &&
5013 o->op_next->op_sibling->op_type != OP_EXIT &&
5014 o->op_next->op_sibling->op_type != OP_WARN &&
5015 o->op_next->op_sibling->op_type != OP_DIE) {
5016 line_t oldline = curcop->cop_line;
5018 curcop->cop_line = ((COP*)o->op_next)->cop_line;
5019 warn("Statement unlikely to be reached");
5020 warn("(Maybe you meant system() when you said exec()?)\n");
5021 curcop->cop_line = oldline;
5035 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
5036 || ((BINOP*)o)->op_last->op_type != OP_CONST)
5038 rop = (UNOP*)((BINOP*)o)->op_first;
5039 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
5041 lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
5042 if (!SvOBJECT(lexname))
5044 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
5045 if (!fields || !GvHV(*fields))
5047 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
5048 key = SvPV(*svp, keylen);
5049 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
5051 croak("No such field \"%s\" in variable %s of type %s",
5052 key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
5054 ind = SvIV(*indsvp);
5056 croak("Bad index while coercing array into hash");
5057 rop->op_type = OP_RV2AV;
5058 rop->op_ppaddr = ppaddr[OP_RV2AV];
5059 o->op_type = OP_AELEM;
5060 o->op_ppaddr = ppaddr[OP_AELEM];
5062 *svp = newSViv(ind);
5067 o->op_seq = op_seqmax++;