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 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)
1057 mod(OP *o, I32 type)
1063 if (!o || error_count)
1066 switch (o->op_type) {
1071 if (!(o->op_private & (OPpCONST_ARYBASE)))
1073 if (eval_start && eval_start->op_type == OP_CONST) {
1074 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
1078 SAVEI32(compiling.cop_arybase);
1079 compiling.cop_arybase = 0;
1081 else if (type == OP_REFGEN)
1084 croak("That use of $[ is unsupported");
1087 if (o->op_flags & OPf_PARENS)
1091 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1092 !(o->op_flags & OPf_STACKED)) {
1093 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1094 o->op_ppaddr = ppaddr[OP_RV2CV];
1095 assert(cUNOPo->op_first->op_type == OP_NULL);
1096 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1102 /* grep, foreach, subcalls, refgen */
1103 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1105 yyerror(form("Can't modify %s in %s",
1106 op_desc[o->op_type],
1107 type ? op_desc[type] : "local"));
1121 case OP_RIGHT_SHIFT:
1130 if (!(o->op_flags & OPf_STACKED))
1136 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1142 if (!type && cUNOPo->op_first->op_type != OP_GV)
1143 croak("Can't localize through a reference");
1144 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1146 return o; /* Treat \(@foo) like ordinary list. */
1150 if (scalar_mod_type(o, type))
1152 ref(cUNOPo->op_first, o->op_type);
1164 if (!type && cUNOPo->op_first->op_type != OP_GV)
1165 croak("Can't localize through a reference");
1166 ref(cUNOPo->op_first, o->op_type);
1170 hints |= HINT_BLOCK_SCOPE;
1179 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1180 return o; /* Treat \(@foo) like ordinary list. */
1181 if (scalar_mod_type(o, type))
1187 croak("Can't localize lexical variable %s",
1188 SvPV(*av_fetch(comppad_name, o->op_targ, 4), na));
1193 modcount++; /* XXX ??? */
1195 #endif /* USE_THREADS */
1201 if (type != OP_SASSIGN)
1207 pad_free(o->op_targ);
1208 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1209 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1210 if (o->op_flags & OPf_KIDS)
1211 mod(cBINOPo->op_first->op_sibling, type);
1216 ref(cBINOPo->op_first, o->op_type);
1217 if (type == OP_ENTERSUB &&
1218 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1219 o->op_private |= OPpLVAL_DEFER;
1226 if (o->op_flags & OPf_KIDS)
1227 mod(cLISTOPo->op_last, type);
1231 if (!(o->op_flags & OPf_KIDS))
1233 if (o->op_targ != OP_LIST) {
1234 mod(cBINOPo->op_first, type);
1239 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1243 o->op_flags |= OPf_MOD;
1245 if (type == OP_AASSIGN || type == OP_SASSIGN)
1246 o->op_flags |= OPf_SPECIAL|OPf_REF;
1248 o->op_private |= OPpLVAL_INTRO;
1249 o->op_flags &= ~OPf_SPECIAL;
1251 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1252 o->op_flags |= OPf_REF;
1257 scalar_mod_type(OP *o, I32 type)
1261 if (o->op_type == OP_RV2GV)
1285 case OP_RIGHT_SHIFT:
1292 case OP_ANDASSIGN: /* may work later */
1293 case OP_ORASSIGN: /* may work later */
1301 refkids(OP *o, I32 type)
1304 if (o && o->op_flags & OPf_KIDS) {
1305 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1312 ref(OP *o, I32 type)
1316 if (!o || error_count)
1319 switch (o->op_type) {
1321 if ((type == OP_DEFINED || type == OP_LOCK) &&
1322 !(o->op_flags & OPf_STACKED)) {
1323 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1324 o->op_ppaddr = ppaddr[OP_RV2CV];
1325 assert(cUNOPo->op_first->op_type == OP_NULL);
1326 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1327 o->op_flags |= OPf_SPECIAL;
1332 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1336 ref(cUNOPo->op_first, o->op_type);
1339 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1340 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1341 : type == OP_RV2HV ? OPpDEREF_HV
1343 o->op_flags |= OPf_MOD;
1348 o->op_flags |= OPf_MOD; /* XXX ??? */
1353 o->op_flags |= OPf_REF;
1356 ref(cUNOPo->op_first, o->op_type);
1361 o->op_flags |= OPf_REF;
1366 if (!(o->op_flags & OPf_KIDS))
1368 ref(cBINOPo->op_first, type);
1372 ref(cBINOPo->op_first, o->op_type);
1373 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1374 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1375 : type == OP_RV2HV ? OPpDEREF_HV
1377 o->op_flags |= OPf_MOD;
1385 if (!(o->op_flags & OPf_KIDS))
1387 ref(cLISTOPo->op_last, type);
1402 if (!o || error_count)
1406 if (type == OP_LIST) {
1407 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1409 } else if (type == OP_UNDEF) {
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 = AvFILLp(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)
1597 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1599 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1600 if (*s == ';' || *s == '=')
1601 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1605 in_my_stash = Nullhv;
1609 return mod(o, OP_NULL); /* a bit kludgey */
1615 if (o->op_type == OP_LIST) {
1618 o2 = newOP(OP_THREADSV, 0);
1619 o2->op_targ = find_threadsv(";");
1621 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1622 #endif /* USE_THREADS */
1623 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1629 fold_constants(register OP *o)
1633 I32 type = o->op_type;
1636 if (opargs[type] & OA_RETSCALAR)
1638 if (opargs[type] & OA_TARGET)
1639 o->op_targ = pad_alloc(type, SVs_PADTMP);
1641 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1642 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1644 if (!(opargs[type] & OA_FOLDCONST))
1659 if (o->op_private & OPpLOCALE)
1664 goto nope; /* Don't try to run w/ errors */
1666 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1667 if (curop->op_type != OP_CONST &&
1668 curop->op_type != OP_LIST &&
1669 curop->op_type != OP_SCALAR &&
1670 curop->op_type != OP_NULL &&
1671 curop->op_type != OP_PUSHMARK) {
1676 curop = LINKLIST(o);
1681 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1682 pad_swipe(o->op_targ);
1683 else if (SvTEMP(sv)) { /* grab mortal temp? */
1684 (void)SvREFCNT_inc(sv);
1688 if (type == OP_RV2GV)
1689 return newGVOP(OP_GV, 0, (GV*)sv);
1691 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1693 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1698 SvIOK_off(sv); /* undo SvIV() damage */
1700 return newSVOP(OP_CONST, 0, sv);
1704 if (!(opargs[type] & OA_OTHERINT))
1707 if (!(hints & HINT_INTEGER)) {
1708 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1711 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1712 if (curop->op_type == OP_CONST) {
1713 if (SvIOK(((SVOP*)curop)->op_sv))
1717 if (opargs[curop->op_type] & OA_RETINTEGER)
1721 o->op_ppaddr = ppaddr[++(o->op_type)];
1728 gen_constant_list(register OP *o)
1732 I32 oldtmps_floor = tmps_floor;
1736 return o; /* Don't attempt to run with errors */
1738 op = curop = LINKLIST(o);
1744 tmps_floor = oldtmps_floor;
1746 o->op_type = OP_RV2AV;
1747 o->op_ppaddr = ppaddr[OP_RV2AV];
1748 curop = ((UNOP*)o)->op_first;
1749 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1756 convert(I32 type, I32 flags, OP *o)
1761 if (!o || o->op_type != OP_LIST)
1762 o = newLISTOP(OP_LIST, 0, o, Nullop);
1764 o->op_flags &= ~OPf_WANT;
1766 if (!(opargs[type] & OA_MARK))
1767 null(cLISTOPo->op_first);
1770 o->op_ppaddr = ppaddr[type];
1771 o->op_flags |= flags;
1773 o = CHECKOP(type, o);
1774 if (o->op_type != type)
1777 if (cLISTOPo->op_children < 7) {
1778 /* XXX do we really need to do this if we're done appending?? */
1779 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1781 cLISTOPo->op_last = last; /* in case check substituted last arg */
1784 return fold_constants(o);
1787 /* List constructors */
1790 append_elem(I32 type, OP *first, OP *last)
1798 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1799 return newLISTOP(type, 0, first, last);
1801 if (first->op_flags & OPf_KIDS)
1802 ((LISTOP*)first)->op_last->op_sibling = last;
1804 first->op_flags |= OPf_KIDS;
1805 ((LISTOP*)first)->op_first = last;
1807 ((LISTOP*)first)->op_last = last;
1808 ((LISTOP*)first)->op_children++;
1813 append_list(I32 type, LISTOP *first, LISTOP *last)
1821 if (first->op_type != type)
1822 return prepend_elem(type, (OP*)first, (OP*)last);
1824 if (last->op_type != type)
1825 return append_elem(type, (OP*)first, (OP*)last);
1827 first->op_last->op_sibling = last->op_first;
1828 first->op_last = last->op_last;
1829 first->op_children += last->op_children;
1830 if (first->op_children)
1831 last->op_flags |= OPf_KIDS;
1838 prepend_elem(I32 type, OP *first, OP *last)
1846 if (last->op_type == type) {
1847 if (type == OP_LIST) { /* already a PUSHMARK there */
1848 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1849 ((LISTOP*)last)->op_first->op_sibling = first;
1852 if (!(last->op_flags & OPf_KIDS)) {
1853 ((LISTOP*)last)->op_last = first;
1854 last->op_flags |= OPf_KIDS;
1856 first->op_sibling = ((LISTOP*)last)->op_first;
1857 ((LISTOP*)last)->op_first = first;
1859 ((LISTOP*)last)->op_children++;
1863 return newLISTOP(type, 0, first, last);
1871 return newOP(OP_STUB, 0);
1877 if (!o || o->op_type != OP_LIST)
1878 o = newLISTOP(OP_LIST, 0, o, Nullop);
1884 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
1888 Newz(1101, listop, 1, LISTOP);
1890 listop->op_type = type;
1891 listop->op_ppaddr = ppaddr[type];
1892 listop->op_children = (first != 0) + (last != 0);
1893 listop->op_flags = flags;
1897 else if (!first && last)
1900 first->op_sibling = last;
1901 listop->op_first = first;
1902 listop->op_last = last;
1903 if (type == OP_LIST) {
1905 pushop = newOP(OP_PUSHMARK, 0);
1906 pushop->op_sibling = first;
1907 listop->op_first = pushop;
1908 listop->op_flags |= OPf_KIDS;
1910 listop->op_last = pushop;
1912 else if (listop->op_children)
1913 listop->op_flags |= OPf_KIDS;
1919 newOP(I32 type, I32 flags)
1922 Newz(1101, o, 1, OP);
1924 o->op_ppaddr = ppaddr[type];
1925 o->op_flags = flags;
1928 o->op_private = 0 + (flags >> 8);
1929 if (opargs[type] & OA_RETSCALAR)
1931 if (opargs[type] & OA_TARGET)
1932 o->op_targ = pad_alloc(type, SVs_PADTMP);
1933 return CHECKOP(type, o);
1937 newUNOP(I32 type, I32 flags, OP *first)
1942 first = newOP(OP_STUB, 0);
1943 if (opargs[type] & OA_MARK)
1944 first = force_list(first);
1946 Newz(1101, unop, 1, UNOP);
1947 unop->op_type = type;
1948 unop->op_ppaddr = ppaddr[type];
1949 unop->op_first = first;
1950 unop->op_flags = flags | OPf_KIDS;
1951 unop->op_private = 1 | (flags >> 8);
1953 if(type == OP_STUDY && first->op_type == OP_MATCH) {
1954 first->op_type = OP_PUSHRE;
1955 first->op_ppaddr = ppaddr[OP_PUSHRE];
1958 unop = (UNOP*) CHECKOP(type, unop);
1962 return fold_constants((OP *) unop);
1966 newBINOP(I32 type, I32 flags, OP *first, OP *last)
1969 Newz(1101, binop, 1, BINOP);
1972 first = newOP(OP_NULL, 0);
1974 binop->op_type = type;
1975 binop->op_ppaddr = ppaddr[type];
1976 binop->op_first = first;
1977 binop->op_flags = flags | OPf_KIDS;
1980 binop->op_private = 1 | (flags >> 8);
1983 binop->op_private = 2 | (flags >> 8);
1984 first->op_sibling = last;
1987 binop = (BINOP*)CHECKOP(type, binop);
1991 binop->op_last = last = binop->op_first->op_sibling;
1993 return fold_constants((OP *)binop);
1997 pmtrans(OP *o, OP *expr, OP *repl)
1999 SV *tstr = ((SVOP*)expr)->op_sv;
2000 SV *rstr = ((SVOP*)repl)->op_sv;
2003 register U8 *t = (U8*)SvPV(tstr, tlen);
2004 register U8 *r = (U8*)SvPV(rstr, rlen);
2010 register short *tbl;
2012 tbl = (short*)cPVOPo->op_pv;
2013 complement = o->op_private & OPpTRANS_COMPLEMENT;
2014 Delete = o->op_private & OPpTRANS_DELETE;
2015 squash = o->op_private & OPpTRANS_SQUASH;
2018 Zero(tbl, 256, short);
2019 for (i = 0; i < tlen; i++)
2021 for (i = 0, j = 0; i < 256; i++) {
2037 if (!rlen && !Delete) {
2040 o->op_private |= OPpTRANS_COUNTONLY;
2042 for (i = 0; i < 256; i++)
2044 for (i = 0, j = 0; i < tlen; i++,j++) {
2047 if (tbl[t[i]] == -1)
2053 if (tbl[t[i]] == -1)
2064 newPMOP(I32 type, I32 flags)
2069 Newz(1101, pmop, 1, PMOP);
2070 pmop->op_type = type;
2071 pmop->op_ppaddr = ppaddr[type];
2072 pmop->op_flags = flags;
2073 pmop->op_private = 0 | (flags >> 8);
2075 if (hints & HINT_LOCALE)
2076 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2078 /* link into pm list */
2079 if (type != OP_TRANS && curstash) {
2080 pmop->op_pmnext = HvPMROOT(curstash);
2081 HvPMROOT(curstash) = pmop;
2088 pmruntime(OP *o, OP *expr, OP *repl)
2093 if (o->op_type == OP_TRANS)
2094 return pmtrans(o, expr, repl);
2096 hints |= HINT_BLOCK_SCOPE;
2099 if (expr->op_type == OP_CONST) {
2101 SV *pat = ((SVOP*)expr)->op_sv;
2102 char *p = SvPV(pat, plen);
2103 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2104 sv_setpvn(pat, "\\s+", 3);
2105 p = SvPV(pat, plen);
2106 pm->op_pmflags |= PMf_SKIPWHITE;
2108 pm->op_pmregexp = pregcomp(p, p + plen, pm);
2109 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2110 pm->op_pmflags |= PMf_WHITE;
2114 if (pm->op_pmflags & PMf_KEEP)
2115 expr = newUNOP(OP_REGCMAYBE,0,expr);
2117 Newz(1101, rcop, 1, LOGOP);
2118 rcop->op_type = OP_REGCOMP;
2119 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2120 rcop->op_first = scalar(expr);
2121 rcop->op_flags |= OPf_KIDS;
2122 rcop->op_private = 1;
2125 /* establish postfix order */
2126 if (pm->op_pmflags & PMf_KEEP) {
2128 rcop->op_next = expr;
2129 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2132 rcop->op_next = LINKLIST(expr);
2133 expr->op_next = (OP*)rcop;
2136 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2141 if (pm->op_pmflags & PMf_EVAL)
2144 else if (repl->op_type == OP_THREADSV
2145 && strchr("&`'123456789+",
2146 threadsv_names[repl->op_targ]))
2150 #endif /* USE_THREADS */
2151 else if (repl->op_type == OP_CONST)
2155 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2156 if (opargs[curop->op_type] & OA_DANGEROUS) {
2158 if (curop->op_type == OP_THREADSV
2159 && strchr("&`'123456789+", curop->op_private)) {
2163 if (curop->op_type == OP_GV) {
2164 GV *gv = ((GVOP*)curop)->op_gv;
2165 if (strchr("&`'123456789+", *GvENAME(gv)))
2168 #endif /* USE_THREADS */
2169 else if (curop->op_type == OP_RV2CV)
2171 else if (curop->op_type == OP_RV2SV ||
2172 curop->op_type == OP_RV2AV ||
2173 curop->op_type == OP_RV2HV ||
2174 curop->op_type == OP_RV2GV) {
2175 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2178 else if (curop->op_type == OP_PADSV ||
2179 curop->op_type == OP_PADAV ||
2180 curop->op_type == OP_PADHV ||
2181 curop->op_type == OP_PADANY) {
2190 if (curop == repl) {
2191 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2192 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2193 prepend_elem(o->op_type, scalar(repl), o);
2196 Newz(1101, rcop, 1, LOGOP);
2197 rcop->op_type = OP_SUBSTCONT;
2198 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2199 rcop->op_first = scalar(repl);
2200 rcop->op_flags |= OPf_KIDS;
2201 rcop->op_private = 1;
2204 /* establish postfix order */
2205 rcop->op_next = LINKLIST(repl);
2206 repl->op_next = (OP*)rcop;
2208 pm->op_pmreplroot = scalar((OP*)rcop);
2209 pm->op_pmreplstart = LINKLIST(rcop);
2218 newSVOP(I32 type, I32 flags, SV *sv)
2221 Newz(1101, svop, 1, SVOP);
2222 svop->op_type = type;
2223 svop->op_ppaddr = ppaddr[type];
2225 svop->op_next = (OP*)svop;
2226 svop->op_flags = flags;
2227 if (opargs[type] & OA_RETSCALAR)
2229 if (opargs[type] & OA_TARGET)
2230 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2231 return CHECKOP(type, svop);
2235 newGVOP(I32 type, I32 flags, GV *gv)
2239 Newz(1101, gvop, 1, GVOP);
2240 gvop->op_type = type;
2241 gvop->op_ppaddr = ppaddr[type];
2242 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2243 gvop->op_next = (OP*)gvop;
2244 gvop->op_flags = flags;
2245 if (opargs[type] & OA_RETSCALAR)
2247 if (opargs[type] & OA_TARGET)
2248 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2249 return CHECKOP(type, gvop);
2253 newPVOP(I32 type, I32 flags, char *pv)
2256 Newz(1101, pvop, 1, PVOP);
2257 pvop->op_type = type;
2258 pvop->op_ppaddr = ppaddr[type];
2260 pvop->op_next = (OP*)pvop;
2261 pvop->op_flags = flags;
2262 if (opargs[type] & OA_RETSCALAR)
2264 if (opargs[type] & OA_TARGET)
2265 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2266 return CHECKOP(type, pvop);
2275 save_hptr(&curstash);
2276 save_item(curstname);
2281 name = SvPV(sv, len);
2282 curstash = gv_stashpvn(name,len,TRUE);
2283 sv_setpvn(curstname, name, len);
2287 sv_setpv(curstname,"<none>");
2295 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2303 if (id->op_type != OP_CONST)
2304 croak("Module name must be constant");
2308 if(version != Nullop) {
2309 SV *vesv = ((SVOP*)version)->op_sv;
2311 if (arg == Nullop && !SvNIOK(vesv)) {
2318 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2319 croak("Version number must be constant number");
2321 /* Make copy of id so we don't free it twice */
2322 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2324 /* Fake up a method call to VERSION */
2325 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2326 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2327 append_elem(OP_LIST,
2328 prepend_elem(OP_LIST, pack, list(version)),
2329 newUNOP(OP_METHOD, 0, meth)));
2333 /* Fake up an import/unimport */
2334 if (arg && arg->op_type == OP_STUB)
2335 imop = arg; /* no import on explicit () */
2336 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2337 imop = Nullop; /* use 5.0; */
2340 /* Make copy of id so we don't free it twice */
2341 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2342 meth = newSVOP(OP_CONST, 0,
2344 ? newSVpv("import", 6)
2345 : newSVpv("unimport", 8)
2347 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2348 append_elem(OP_LIST,
2349 prepend_elem(OP_LIST, pack, list(arg)),
2350 newUNOP(OP_METHOD, 0, meth)));
2353 /* Fake up a require */
2354 rqop = newUNOP(OP_REQUIRE, 0, id);
2356 /* Fake up the BEGIN {}, which does its thing immediately. */
2358 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2360 append_elem(OP_LINESEQ,
2361 append_elem(OP_LINESEQ,
2362 newSTATEOP(0, Nullch, rqop),
2363 newSTATEOP(0, Nullch, veop)),
2364 newSTATEOP(0, Nullch, imop) ));
2371 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2373 return newBINOP(OP_LSLICE, flags,
2374 list(force_list(subscript)),
2375 list(force_list(listval)) );
2379 list_assignment(register OP *o)
2384 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2385 o = cUNOPo->op_first;
2387 if (o->op_type == OP_COND_EXPR) {
2388 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2389 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2394 yyerror("Assignment to both a list and a scalar");
2398 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2399 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2400 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2403 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2406 if (o->op_type == OP_RV2SV)
2413 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2418 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2419 return newLOGOP(optype, 0,
2420 mod(scalar(left), optype),
2421 newUNOP(OP_SASSIGN, 0, scalar(right)));
2424 return newBINOP(optype, OPf_STACKED,
2425 mod(scalar(left), optype), scalar(right));
2429 if (list_assignment(left)) {
2431 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2432 left = mod(left, OP_AASSIGN);
2440 o = newBINOP(OP_AASSIGN, flags,
2441 list(force_list(right)),
2442 list(force_list(left)) );
2443 o->op_private = 0 | (flags >> 8);
2444 if (!(left->op_private & OPpLVAL_INTRO)) {
2448 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2449 if (opargs[curop->op_type] & OA_DANGEROUS) {
2450 if (curop->op_type == OP_GV) {
2451 GV *gv = ((GVOP*)curop)->op_gv;
2452 if (gv == defgv || SvCUR(gv) == generation)
2454 SvCUR(gv) = generation;
2456 else if (curop->op_type == OP_PADSV ||
2457 curop->op_type == OP_PADAV ||
2458 curop->op_type == OP_PADHV ||
2459 curop->op_type == OP_PADANY) {
2460 SV **svp = AvARRAY(comppad_name);
2461 SV *sv = svp[curop->op_targ];
2462 if (SvCUR(sv) == generation)
2464 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2466 else if (curop->op_type == OP_RV2CV)
2468 else if (curop->op_type == OP_RV2SV ||
2469 curop->op_type == OP_RV2AV ||
2470 curop->op_type == OP_RV2HV ||
2471 curop->op_type == OP_RV2GV) {
2472 if (lastop->op_type != OP_GV) /* funny deref? */
2481 o->op_private = OPpASSIGN_COMMON;
2483 if (right && right->op_type == OP_SPLIT) {
2485 if ((tmpop = ((LISTOP*)right)->op_first) &&
2486 tmpop->op_type == OP_PUSHRE)
2488 PMOP *pm = (PMOP*)tmpop;
2489 if (left->op_type == OP_RV2AV &&
2490 !(left->op_private & OPpLVAL_INTRO) &&
2491 !(o->op_private & OPpASSIGN_COMMON) )
2493 tmpop = ((UNOP*)left)->op_first;
2494 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2495 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2496 pm->op_pmflags |= PMf_ONCE;
2497 tmpop = cUNOPo->op_first; /* to list (nulled) */
2498 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2499 tmpop->op_sibling = Nullop; /* don't free split */
2500 right->op_next = tmpop->op_next; /* fix starting loc */
2501 op_free(o); /* blow off assign */
2502 right->op_flags &= ~OPf_WANT;
2503 /* "I don't know and I don't care." */
2508 if (modcount < 10000 &&
2509 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2511 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2513 sv_setiv(sv, modcount+1);
2521 right = newOP(OP_UNDEF, 0);
2522 if (right->op_type == OP_READLINE) {
2523 right->op_flags |= OPf_STACKED;
2524 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2527 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2528 o = newBINOP(OP_SASSIGN, flags,
2529 scalar(right), mod(scalar(left), OP_SASSIGN) );
2541 newSTATEOP(I32 flags, char *label, OP *o)
2544 U32 seq = intro_my();
2547 Newz(1101, cop, 1, COP);
2548 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2549 cop->op_type = OP_DBSTATE;
2550 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2553 cop->op_type = OP_NEXTSTATE;
2554 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2556 cop->op_flags = flags;
2557 cop->op_private = 0 | (flags >> 8);
2559 cop->op_private |= NATIVE_HINTS;
2561 cop->op_next = (OP*)cop;
2564 cop->cop_label = label;
2565 hints |= HINT_BLOCK_SCOPE;
2568 cop->cop_arybase = curcop->cop_arybase;
2570 if (copline == NOLINE)
2571 cop->cop_line = curcop->cop_line;
2573 cop->cop_line = copline;
2576 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2577 cop->cop_stash = curstash;
2579 if (PERLDB_LINE && curstash != debstash) {
2580 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2581 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2582 (void)SvIOK_on(*svp);
2584 SvSTASH(*svp) = (HV*)cop;
2588 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2591 /* "Introduce" my variables to visible status. */
2599 if (! min_intro_pending)
2602 svp = AvARRAY(comppad_name);
2603 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2604 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2605 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2606 SvNVX(sv) = (double)cop_seqmax;
2609 min_intro_pending = 0;
2610 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2611 return cop_seqmax++;
2615 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2617 return new_logop(type, flags, &first, &other);
2621 new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
2626 OP *first = *firstp;
2627 OP *other = *otherp;
2629 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2630 return newBINOP(type, flags, scalar(first), scalar(other));
2632 scalarboolean(first);
2633 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2634 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2635 if (type == OP_AND || type == OP_OR) {
2641 first = *firstp = cUNOPo->op_first;
2643 first->op_next = o->op_next;
2644 cUNOPo->op_first = Nullop;
2648 if (first->op_type == OP_CONST) {
2649 if (dowarn && (first->op_private & OPpCONST_BARE))
2650 warn("Probable precedence problem on %s", op_desc[type]);
2651 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2662 else if (first->op_type == OP_WANTARRAY) {
2668 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2669 OP *k1 = ((UNOP*)first)->op_first;
2670 OP *k2 = k1->op_sibling;
2672 switch (first->op_type)
2675 if (k2 && k2->op_type == OP_READLINE
2676 && (k2->op_flags & OPf_STACKED)
2677 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2678 warnop = k2->op_type;
2682 if (k1->op_type == OP_READDIR
2683 || k1->op_type == OP_GLOB
2684 || k1->op_type == OP_EACH)
2685 warnop = k1->op_type;
2689 line_t oldline = curcop->cop_line;
2690 curcop->cop_line = copline;
2691 warn("Value of %s%s can be \"0\"; test with defined()",
2693 ((warnop == OP_READLINE || warnop == OP_GLOB)
2694 ? " construct" : "() operator"));
2695 curcop->cop_line = oldline;
2702 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2703 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2705 Newz(1101, logop, 1, LOGOP);
2707 logop->op_type = type;
2708 logop->op_ppaddr = ppaddr[type];
2709 logop->op_first = first;
2710 logop->op_flags = flags | OPf_KIDS;
2711 logop->op_other = LINKLIST(other);
2712 logop->op_private = 1 | (flags >> 8);
2714 /* establish postfix order */
2715 logop->op_next = LINKLIST(first);
2716 first->op_next = (OP*)logop;
2717 first->op_sibling = other;
2719 o = newUNOP(OP_NULL, 0, (OP*)logop);
2726 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2733 return newLOGOP(OP_AND, 0, first, trueop);
2735 return newLOGOP(OP_OR, 0, first, falseop);
2737 scalarboolean(first);
2738 if (first->op_type == OP_CONST) {
2739 if (SvTRUE(((SVOP*)first)->op_sv)) {
2750 else if (first->op_type == OP_WANTARRAY) {
2754 Newz(1101, condop, 1, CONDOP);
2756 condop->op_type = OP_COND_EXPR;
2757 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2758 condop->op_first = first;
2759 condop->op_flags = flags | OPf_KIDS;
2760 condop->op_true = LINKLIST(trueop);
2761 condop->op_false = LINKLIST(falseop);
2762 condop->op_private = 1 | (flags >> 8);
2764 /* establish postfix order */
2765 condop->op_next = LINKLIST(first);
2766 first->op_next = (OP*)condop;
2768 first->op_sibling = trueop;
2769 trueop->op_sibling = falseop;
2770 o = newUNOP(OP_NULL, 0, (OP*)condop);
2772 trueop->op_next = o;
2773 falseop->op_next = o;
2779 newRANGE(I32 flags, OP *left, OP *right)
2787 Newz(1101, condop, 1, CONDOP);
2789 condop->op_type = OP_RANGE;
2790 condop->op_ppaddr = ppaddr[OP_RANGE];
2791 condop->op_first = left;
2792 condop->op_flags = OPf_KIDS;
2793 condop->op_true = LINKLIST(left);
2794 condop->op_false = LINKLIST(right);
2795 condop->op_private = 1 | (flags >> 8);
2797 left->op_sibling = right;
2799 condop->op_next = (OP*)condop;
2800 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2801 flop = newUNOP(OP_FLOP, 0, flip);
2802 o = newUNOP(OP_NULL, 0, flop);
2805 left->op_next = flip;
2806 right->op_next = flop;
2808 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2809 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2810 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2811 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2813 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2814 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2817 if (!flip->op_private || !flop->op_private)
2818 linklist(o); /* blow off optimizer unless constant */
2824 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2829 int once = block && block->op_flags & OPf_SPECIAL &&
2830 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2833 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2834 return block; /* do {} while 0 does once */
2835 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2836 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2837 expr = newUNOP(OP_DEFINED, 0,
2838 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2842 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2843 o = new_logop(OP_AND, 0, &expr, &listop);
2846 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2848 if (once && o != listop)
2849 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2852 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
2854 o->op_flags |= flags;
2856 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2861 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2870 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2871 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2872 expr = newUNOP(OP_DEFINED, 0,
2873 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2877 block = newOP(OP_NULL, 0);
2880 next = LINKLIST(cont);
2882 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2883 if ((line_t)whileline != NOLINE) {
2884 copline = whileline;
2885 cont = append_elem(OP_LINESEQ, cont,
2886 newSTATEOP(0, Nullch, Nullop));
2890 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2891 redo = LINKLIST(listop);
2894 copline = whileline;
2896 o = new_logop(OP_AND, 0, &expr, &listop);
2897 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2898 op_free(expr); /* oops, it's a while (0) */
2900 return Nullop; /* listop already freed by new_logop */
2903 ((LISTOP*)listop)->op_last->op_next = condop =
2904 (o == listop ? redo : LINKLIST(o));
2912 Newz(1101,loop,1,LOOP);
2913 loop->op_type = OP_ENTERLOOP;
2914 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2915 loop->op_private = 0;
2916 loop->op_next = (OP*)loop;
2919 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
2921 loop->op_redoop = redo;
2922 loop->op_lastop = o;
2925 loop->op_nextop = next;
2927 loop->op_nextop = o;
2929 o->op_flags |= flags;
2930 o->op_private |= (flags >> 8);
2935 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2943 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2944 sv->op_type = OP_RV2GV;
2945 sv->op_ppaddr = ppaddr[OP_RV2GV];
2947 else if (sv->op_type == OP_PADSV) { /* private variable */
2948 padoff = sv->op_targ;
2952 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
2953 padoff = sv->op_targ;
2954 iterflags |= OPf_SPECIAL;
2959 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2963 padoff = find_threadsv("_");
2964 iterflags |= OPf_SPECIAL;
2966 sv = newGVOP(OP_GV, 0, defgv);
2969 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2970 expr = scalar(ref(expr, OP_ITER));
2971 iterflags |= OPf_STACKED;
2973 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2974 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2976 assert(!loop->op_next);
2977 Renew(loop, 1, LOOP);
2978 loop->op_targ = padoff;
2979 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
2981 return newSTATEOP(0, label, wop);
2985 newLOOPEX(I32 type, OP *label)
2989 if (type != OP_GOTO || label->op_type == OP_CONST) {
2990 /* "last()" means "last" */
2991 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
2992 o = newOP(type, OPf_SPECIAL);
2994 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
2995 ? SvPVx(((SVOP*)label)->op_sv, na)
3001 if (label->op_type == OP_ENTERSUB)
3002 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
3003 o = newUNOP(type, OPf_STACKED, label);
3005 hints |= HINT_BLOCK_SCOPE;
3015 MUTEX_DESTROY(CvMUTEXP(cv));
3016 Safefree(CvMUTEXP(cv));
3019 #endif /* USE_THREADS */
3021 if (!CvXSUB(cv) && CvROOT(cv)) {
3023 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3024 croak("Can't undef active subroutine");
3027 croak("Can't undef active subroutine");
3028 #endif /* USE_THREADS */
3035 op_free(CvROOT(cv));
3036 CvROOT(cv) = Nullop;
3039 SvPOK_off((SV*)cv); /* forget prototype */
3041 SvREFCNT_dec(CvGV(cv));
3043 SvREFCNT_dec(CvOUTSIDE(cv));
3044 CvOUTSIDE(cv) = Nullcv;
3045 if (CvPADLIST(cv)) {
3046 /* may be during global destruction */
3047 if (SvREFCNT(CvPADLIST(cv))) {
3048 I32 i = AvFILLp(CvPADLIST(cv));
3050 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3051 SV* sv = svp ? *svp : Nullsv;
3054 if (sv == (SV*)comppad_name)
3055 comppad_name = Nullav;
3056 else if (sv == (SV*)comppad) {
3058 curpad = Null(SV**);
3062 SvREFCNT_dec((SV*)CvPADLIST(cv));
3064 CvPADLIST(cv) = Nullav;
3068 #ifdef DEBUG_CLOSURES
3073 CV *outside = CvOUTSIDE(cv);
3074 AV* padlist = CvPADLIST(cv);
3081 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3083 (CvANON(cv) ? "ANON"
3084 : (cv == main_cv) ? "MAIN"
3085 : CvUNIQUE(outside) ? "UNIQUE"
3086 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3089 : CvANON(outside) ? "ANON"
3090 : (outside == main_cv) ? "MAIN"
3091 : CvUNIQUE(outside) ? "UNIQUE"
3092 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3097 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3098 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3099 pname = AvARRAY(pad_name);
3100 ppad = AvARRAY(pad);
3102 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3103 if (SvPOK(pname[ix]))
3104 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3106 SvFAKE(pname[ix]) ? "FAKE " : "",
3108 (long)I_32(SvNVX(pname[ix])),
3109 (long)SvIVX(pname[ix]));
3112 #endif /* DEBUG_CLOSURES */
3115 cv_clone2(CV *proto, CV *outside)
3120 AV* protopadlist = CvPADLIST(proto);
3121 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3122 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3123 SV** pname = AvARRAY(protopad_name);
3124 SV** ppad = AvARRAY(protopad);
3125 I32 fname = AvFILLp(protopad_name);
3126 I32 fpad = AvFILLp(protopad);
3130 assert(!CvUNIQUE(proto));
3135 SAVESPTR(comppad_name);
3138 cv = compcv = (CV*)NEWSV(1104,0);
3139 sv_upgrade((SV *)cv, SvTYPE(proto));
3145 New(666, CvMUTEXP(cv), 1, perl_mutex);
3146 MUTEX_INIT(CvMUTEXP(cv));
3148 #endif /* USE_THREADS */
3149 CvFILEGV(cv) = CvFILEGV(proto);
3150 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3151 CvSTASH(cv) = CvSTASH(proto);
3152 CvROOT(cv) = CvROOT(proto);
3153 CvSTART(cv) = CvSTART(proto);
3155 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3158 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3160 comppad_name = newAV();
3161 for (ix = fname; ix >= 0; ix--)
3162 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3166 comppadlist = newAV();
3167 AvREAL_off(comppadlist);
3168 av_store(comppadlist, 0, (SV*)comppad_name);
3169 av_store(comppadlist, 1, (SV*)comppad);
3170 CvPADLIST(cv) = comppadlist;
3171 av_fill(comppad, AvFILLp(protopad));
3172 curpad = AvARRAY(comppad);
3174 av = newAV(); /* will be @_ */
3176 av_store(comppad, 0, (SV*)av);
3177 AvFLAGS(av) = AVf_REIFY;
3179 for (ix = fpad; ix > 0; ix--) {
3180 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3181 if (namesv && namesv != &sv_undef) {
3182 char *name = SvPVX(namesv); /* XXX */
3183 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3184 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3185 CvOUTSIDE(cv), cxstack_ix);
3187 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3189 croak("panic: cv_clone: %s", name);
3191 else { /* our own lexical */
3194 /* anon code -- we'll come back for it */
3195 sv = SvREFCNT_inc(ppad[ix]);
3197 else if (*name == '@')
3199 else if (*name == '%')
3209 SV* sv = NEWSV(0,0);
3215 /* Now that vars are all in place, clone nested closures. */
3217 for (ix = fpad; ix > 0; ix--) {
3218 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3220 && namesv != &sv_undef
3221 && !(SvFLAGS(namesv) & SVf_FAKE)
3222 && *SvPVX(namesv) == '&'
3223 && CvCLONE(ppad[ix]))
3225 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3226 SvREFCNT_dec(ppad[ix]);
3229 curpad[ix] = (SV*)kid;
3233 #ifdef DEBUG_CLOSURES
3234 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3236 PerlIO_printf(Perl_debug_log, " from:\n");
3238 PerlIO_printf(Perl_debug_log, " to:\n");
3249 return cv_clone2(proto, CvOUTSIDE(proto));
3253 cv_ckproto(CV *cv, GV *gv, char *p)
3255 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3256 SV* msg = sv_newmortal();
3260 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3261 sv_setpv(msg, "Prototype mismatch:");
3263 sv_catpvf(msg, " sub %_", name);
3265 sv_catpvf(msg, " (%s)", SvPVX(cv));
3266 sv_catpv(msg, " vs ");
3268 sv_catpvf(msg, "(%s)", p);
3270 sv_catpv(msg, "none");
3281 if (!cv || !SvPOK(cv) || SvCUR(cv))
3285 for (o = CvSTART(cv); o; o = o->op_next) {
3286 OPCODE type = o->op_type;
3288 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3290 if (type == OP_LEAVESUB || type == OP_RETURN)
3294 if (type == OP_CONST)
3296 else if (type == OP_PADSV) {
3297 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3298 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3299 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3311 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3314 char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3315 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3316 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3325 if (!name || GvCVGEN(gv))
3327 else if (cv = GvCV(gv)) {
3328 cv_ckproto(cv, gv, ps);
3329 /* already defined (or promised)? */
3330 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3333 /* just a "sub foo;" when &foo is already defined */
3337 /* ahem, death to those who redefine active sort subs */
3338 if (curstack == sortstack && sortcop == CvSTART(cv))
3339 croak("Can't redefine active sort subroutine %s", name);
3340 const_sv = cv_const_sv(cv);
3341 if (const_sv || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3342 && HvNAME(GvSTASH(CvGV(cv)))
3343 && strEQ(HvNAME(GvSTASH(CvGV(cv))),
3345 line_t oldline = curcop->cop_line;
3346 curcop->cop_line = copline;
3347 warn(const_sv ? "Constant subroutine %s redefined"
3348 : "Subroutine %s redefined", name);
3349 curcop->cop_line = oldline;
3355 if (cv) { /* must reuse cv if autoloaded */
3357 CvFLAGS(cv) = CvFLAGS(compcv);
3358 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3359 CvOUTSIDE(compcv) = 0;
3360 CvPADLIST(cv) = CvPADLIST(compcv);
3361 CvPADLIST(compcv) = 0;
3362 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3363 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3364 SvREFCNT_dec(compcv);
3374 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3375 CvFILEGV(cv) = curcop->cop_filegv;
3376 CvSTASH(cv) = curstash;
3379 New(666, CvMUTEXP(cv), 1, perl_mutex);
3380 MUTEX_INIT(CvMUTEXP(cv));
3381 #endif /* USE_THREADS */
3384 sv_setpv((SV*)cv, ps);
3390 char *s = strrchr(name, ':');
3392 if (strEQ(s, "BEGIN")) {
3394 "BEGIN not safe after errors--compilation aborted";
3398 /* force display of errors found but not reported */
3399 sv_catpv(ERRSV, not_safe);
3400 croak("%s", SvPVx(ERRSV, na));
3411 if (AvFILLp(comppad_name) < AvFILLp(comppad))
3412 av_store(comppad_name, AvFILLp(comppad), Nullsv);
3415 SV **namep = AvARRAY(comppad_name);
3416 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3419 if (SvIMMORTAL(curpad[ix]))
3422 * The only things that a clonable function needs in its
3423 * pad are references to outer lexicals and anonymous subs.
3424 * The rest are created anew during cloning.
3426 if (!((namesv = namep[ix]) != Nullsv &&
3427 namesv != &sv_undef &&
3429 *SvPVX(namesv) == '&')))
3431 SvREFCNT_dec(curpad[ix]);
3432 curpad[ix] = Nullsv;
3437 AV *av = newAV(); /* Will be @_ */
3439 av_store(comppad, 0, (SV*)av);
3440 AvFLAGS(av) = AVf_REIFY;
3442 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3443 if (SvIMMORTAL(curpad[ix]))
3445 if (!SvPADMY(curpad[ix]))
3446 SvPADTMP_on(curpad[ix]);
3450 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3451 CvSTART(cv) = LINKLIST(CvROOT(cv));
3452 CvROOT(cv)->op_next = 0;
3458 if (PERLDB_SUBLINE && curstash != debstash) {
3459 SV *sv = NEWSV(0,0);
3460 SV *tmpstr = sv_newmortal();
3461 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3465 sv_setpvf(sv, "%_:%ld-%ld",
3466 GvSV(curcop->cop_filegv),
3467 (long)subline, (long)curcop->cop_line);
3468 gv_efullname3(tmpstr, gv, Nullch);
3469 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3470 hv = GvHVn(db_postponed);
3471 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3472 && (cv = GvCV(db_postponed))) {
3477 perl_call_sv((SV*)cv, G_DISCARD);
3481 if ((s = strrchr(name,':')))
3485 if (strEQ(s, "BEGIN")) {
3486 I32 oldscope = scopestack_ix;
3488 SAVESPTR(compiling.cop_filegv);
3489 SAVEI16(compiling.cop_line);
3496 DEBUG_x( dump_sub(gv) );
3497 av_push(beginav, (SV *)cv);
3499 call_list(oldscope, beginav);
3501 curcop = &compiling;
3504 else if (strEQ(s, "END") && !error_count) {
3507 av_unshift(endav, 1);
3508 av_store(endav, 0, (SV *)cv);
3511 else if (strEQ(s, "INIT") && !error_count) {
3514 av_push(initav, SvREFCNT_inc(cv));
3525 newXS(char *name, void (*subaddr) (CPERLproto_ CV *), char *filename)
3528 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3531 if (cv = (name ? GvCV(gv) : Nullcv)) {
3533 /* just a cached method */
3537 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3538 /* already defined (or promised) */
3539 if (dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
3540 && HvNAME(GvSTASH(CvGV(cv)))
3541 && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) {
3542 line_t oldline = curcop->cop_line;
3543 curcop->cop_line = copline;
3544 warn("Subroutine %s redefined",name);
3545 curcop->cop_line = oldline;
3552 if (cv) /* must reuse cv if autoloaded */
3555 cv = (CV*)NEWSV(1105,0);
3556 sv_upgrade((SV *)cv, SVt_PVCV);
3563 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3565 New(666, CvMUTEXP(cv), 1, perl_mutex);
3566 MUTEX_INIT(CvMUTEXP(cv));
3568 #endif /* USE_THREADS */
3569 CvFILEGV(cv) = gv_fetchfile(filename);
3570 CvXSUB(cv) = subaddr;
3573 char *s = strrchr(name,':');
3578 if (strEQ(s, "BEGIN")) {
3581 av_push(beginav, (SV *)cv);
3584 else if (strEQ(s, "END")) {
3587 av_unshift(endav, 1);
3588 av_store(endav, 0, (SV *)cv);
3591 else if (strEQ(s, "INIT")) {
3594 av_push(initav, (SV *)cv);
3604 newFORM(I32 floor, OP *o, OP *block)
3613 name = SvPVx(cSVOPo->op_sv, na);
3616 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3618 if (cv = GvFORM(gv)) {
3620 line_t oldline = curcop->cop_line;
3622 curcop->cop_line = copline;
3623 warn("Format %s redefined",name);
3624 curcop->cop_line = oldline;
3630 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3631 CvFILEGV(cv) = curcop->cop_filegv;
3633 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3634 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3635 SvPADTMP_on(curpad[ix]);
3638 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3639 CvSTART(cv) = LINKLIST(CvROOT(cv));
3640 CvROOT(cv)->op_next = 0;
3650 return newUNOP(OP_REFGEN, 0,
3651 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3657 return newUNOP(OP_REFGEN, 0,
3658 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3662 newANONSUB(I32 floor, OP *proto, OP *block)
3664 return newUNOP(OP_REFGEN, 0,
3665 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3671 switch (o->op_type) {
3673 o->op_type = OP_PADAV;
3674 o->op_ppaddr = ppaddr[OP_PADAV];
3675 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3678 o->op_type = OP_RV2AV;
3679 o->op_ppaddr = ppaddr[OP_RV2AV];
3684 warn("oops: oopsAV");
3693 switch (o->op_type) {
3696 o->op_type = OP_PADHV;
3697 o->op_ppaddr = ppaddr[OP_PADHV];
3698 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3702 o->op_type = OP_RV2HV;
3703 o->op_ppaddr = ppaddr[OP_RV2HV];
3708 warn("oops: oopsHV");
3717 if (o->op_type == OP_PADANY) {
3718 o->op_type = OP_PADAV;
3719 o->op_ppaddr = ppaddr[OP_PADAV];
3722 return newUNOP(OP_RV2AV, 0, scalar(o));
3726 newGVREF(I32 type, OP *o)
3728 if (type == OP_MAPSTART)
3729 return newUNOP(OP_NULL, 0, o);
3730 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3736 if (o->op_type == OP_PADANY) {
3737 o->op_type = OP_PADHV;
3738 o->op_ppaddr = ppaddr[OP_PADHV];
3741 return newUNOP(OP_RV2HV, 0, scalar(o));
3747 croak("NOT IMPL LINE %d",__LINE__);
3753 newCVREF(I32 flags, OP *o)
3755 return newUNOP(OP_RV2CV, flags, scalar(o));
3761 if (o->op_type == OP_PADANY) {
3762 o->op_type = OP_PADSV;
3763 o->op_ppaddr = ppaddr[OP_PADSV];
3766 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3767 o->op_flags |= OPpDONE_SVREF;
3770 return newUNOP(OP_RV2SV, 0, scalar(o));
3773 /* Check routines. */
3781 name = NEWSV(1106,0);
3782 sv_upgrade(name, SVt_PVNV);
3783 sv_setpvn(name, "&", 1);
3786 ix = pad_alloc(o->op_type, SVs_PADMY);
3787 av_store(comppad_name, ix, name);
3788 av_store(comppad, ix, cSVOPo->op_sv);
3789 SvPADMY_on(cSVOPo->op_sv);
3790 cSVOPo->op_sv = Nullsv;
3791 cSVOPo->op_targ = ix;
3798 o->op_private = hints;
3805 if (cUNOPo->op_first->op_type == OP_CONCAT)
3806 o->op_flags |= OPf_STACKED;
3813 if (o->op_flags & OPf_KIDS) {
3816 OPCODE type = o->op_type;
3817 o = modkids(ck_fun(o), type);
3818 kid = cUNOPo->op_first;
3819 newop = kUNOP->op_first->op_sibling;
3821 (newop->op_sibling ||
3822 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3823 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3824 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3828 op_free(kUNOP->op_first);
3829 kUNOP->op_first = newop;
3831 o->op_ppaddr = ppaddr[++o->op_type];
3840 if (o->op_flags & OPf_KIDS) {
3841 OP *kid = cUNOPo->op_first;
3842 if (kid->op_type == OP_HSLICE)
3843 o->op_private |= OPpSLICE;
3844 else if (kid->op_type != OP_HELEM)
3845 croak("%s argument is not a HASH element or slice",
3846 op_desc[o->op_type]);
3855 I32 type = o->op_type;
3857 if (o->op_flags & OPf_KIDS) {
3858 if (cLISTOPo->op_first->op_type == OP_STUB) {
3860 o = newUNOP(type, OPf_SPECIAL,
3861 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
3871 hints |= HINT_BLOCK_SCOPE;
3872 if (o->op_flags & OPf_KIDS) {
3873 SVOP *kid = (SVOP*)cUNOPo->op_first;
3876 o->op_flags &= ~OPf_KIDS;
3879 else if (kid->op_type == OP_LINESEQ) {
3882 kid->op_next = o->op_next;
3883 cUNOPo->op_first = 0;
3886 Newz(1101, enter, 1, LOGOP);
3887 enter->op_type = OP_ENTERTRY;
3888 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3889 enter->op_private = 0;
3891 /* establish postfix order */
3892 enter->op_next = (OP*)enter;
3894 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3895 o->op_type = OP_LEAVETRY;
3896 o->op_ppaddr = ppaddr[OP_LEAVETRY];
3897 enter->op_other = o;
3905 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
3907 o->op_targ = (PADOFFSET)hints;
3915 if (o->op_flags & OPf_STACKED) {
3917 kid = cUNOPo->op_first->op_sibling;
3918 if (kid->op_type == OP_RV2GV)
3930 if (o->op_flags & OPf_KIDS) {
3931 OP *kid = cUNOPo->op_first;
3932 if (kid->op_type != OP_HELEM)
3933 croak("%s argument is not a HASH element", op_desc[o->op_type]);
3940 ck_gvconst(register OP *o)
3942 o = fold_constants(o);
3943 if (o->op_type == OP_CONST)
3949 ck_rvconst(register OP *o)
3952 SVOP *kid = (SVOP*)cUNOPo->op_first;
3954 o->op_private |= (hints & HINT_STRICT_REFS);
3955 if (kid->op_type == OP_CONST) {
3960 name = SvPV(kid->op_sv, na);
3961 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3962 char *badthing = Nullch;
3963 switch (o->op_type) {
3965 badthing = "a SCALAR";
3968 badthing = "an ARRAY";
3971 badthing = "a HASH";
3976 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3980 * This is a little tricky. We only want to add the symbol if we
3981 * didn't add it in the lexer. Otherwise we get duplicate strict
3982 * warnings. But if we didn't add it in the lexer, we must at
3983 * least pretend like we wanted to add it even if it existed before,
3984 * or we get possible typo warnings. OPpCONST_ENTERED says
3985 * whether the lexer already added THIS instance of this symbol.
3987 iscv = (o->op_type == OP_RV2CV) * 2;
3989 gv = gv_fetchpv(name,
3990 iscv | !(kid->op_private & OPpCONST_ENTERED),
3993 : o->op_type == OP_RV2SV
3995 : o->op_type == OP_RV2AV
3997 : o->op_type == OP_RV2HV
4000 } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++);
4002 kid->op_type = OP_GV;
4003 SvREFCNT_dec(kid->op_sv);
4004 kid->op_sv = SvREFCNT_inc(gv);
4014 I32 type = o->op_type;
4016 if (o->op_flags & OPf_REF)
4019 if (o->op_flags & OPf_KIDS) {
4020 SVOP *kid = (SVOP*)cUNOPo->op_first;
4022 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4023 OP *newop = newGVOP(type, OPf_REF,
4024 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
4031 if (type == OP_FTTTY)
4032 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4035 return newUNOP(type, 0, newDEFSVOP());
4048 int type = o->op_type;
4049 register I32 oa = opargs[type] >> OASHIFT;
4051 if (o->op_flags & OPf_STACKED) {
4052 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4055 return no_fh_allowed(o);
4058 if (o->op_flags & OPf_KIDS) {
4059 tokid = &cLISTOPo->op_first;
4060 kid = cLISTOPo->op_first;
4061 if (kid->op_type == OP_PUSHMARK ||
4062 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4064 tokid = &kid->op_sibling;
4065 kid = kid->op_sibling;
4067 if (!kid && opargs[type] & OA_DEFGV)
4068 *tokid = kid = newDEFSVOP();
4072 sibl = kid->op_sibling;
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 = newAVREF(newGVOP(OP_GV, 0,
4090 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4092 warn("Array @%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_RV2AV && kid->op_type != OP_PADAV)
4100 bad_type(numargs, "array", op_desc[o->op_type], kid);
4104 if (kid->op_type == OP_CONST &&
4105 (kid->op_private & OPpCONST_BARE)) {
4106 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4107 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4108 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4110 warn("Hash %%%s missing the %% in argument %ld of %s()",
4111 name, (long)numargs, op_desc[type]);
4114 kid->op_sibling = sibl;
4117 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4118 bad_type(numargs, "hash", op_desc[o->op_type], kid);
4123 OP *newop = newUNOP(OP_NULL, 0, kid);
4124 kid->op_sibling = 0;
4126 newop->op_next = newop;
4128 kid->op_sibling = sibl;
4133 if (kid->op_type != OP_GV) {
4134 if (kid->op_type == OP_CONST &&
4135 (kid->op_private & OPpCONST_BARE)) {
4136 OP *newop = newGVOP(OP_GV, 0,
4137 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4143 kid->op_sibling = 0;
4144 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4146 kid->op_sibling = sibl;
4152 mod(scalar(kid), type);
4156 tokid = &kid->op_sibling;
4157 kid = kid->op_sibling;
4159 o->op_private |= numargs;
4161 return too_many_arguments(o,op_desc[o->op_type]);
4164 else if (opargs[type] & OA_DEFGV) {
4166 return newUNOP(type, 0, newDEFSVOP());
4170 while (oa & OA_OPTIONAL)
4172 if (oa && oa != OA_LIST)
4173 return too_few_arguments(o,op_desc[o->op_type]);
4183 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4184 append_elem(OP_GLOB, o, newDEFSVOP());
4186 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4187 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4189 if (gv && GvIMPORTED_CV(gv)) {
4190 static int glob_index;
4192 append_elem(OP_GLOB, o,
4193 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4194 o->op_type = OP_LIST;
4195 o->op_ppaddr = ppaddr[OP_LIST];
4196 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4197 cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4198 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4199 append_elem(OP_LIST, o,
4200 scalar(newUNOP(OP_RV2CV, 0,
4201 newGVOP(OP_GV, 0, gv)))));
4202 o = newUNOP(OP_NULL, 0, ck_subr(o));
4203 o->op_targ = OP_GLOB; /* hint at what it used to be */
4206 gv = newGVgen("main");
4208 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4218 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4220 o->op_ppaddr = ppaddr[OP_GREPSTART];
4221 Newz(1101, gwop, 1, LOGOP);
4223 if (o->op_flags & OPf_STACKED) {
4226 kid = cLISTOPo->op_first->op_sibling;
4227 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4230 kid->op_next = (OP*)gwop;
4231 o->op_flags &= ~OPf_STACKED;
4233 kid = cLISTOPo->op_first->op_sibling;
4234 if (type == OP_MAPWHILE)
4241 kid = cLISTOPo->op_first->op_sibling;
4242 if (kid->op_type != OP_NULL)
4243 croak("panic: ck_grep");
4244 kid = kUNOP->op_first;
4246 gwop->op_type = type;
4247 gwop->op_ppaddr = ppaddr[type];
4248 gwop->op_first = listkids(o);
4249 gwop->op_flags |= OPf_KIDS;
4250 gwop->op_private = 1;
4251 gwop->op_other = LINKLIST(kid);
4252 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4253 kid->op_next = (OP*)gwop;
4255 kid = cLISTOPo->op_first->op_sibling;
4256 if (!kid || !kid->op_sibling)
4257 return too_few_arguments(o,op_desc[o->op_type]);
4258 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4259 mod(kid, OP_GREPSTART);
4267 if (o->op_flags & OPf_KIDS) {
4268 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4269 if (kid && kid->op_type == OP_CONST)
4270 fbm_compile(((SVOP*)kid)->op_sv, 0);
4276 ck_lengthconst(OP *o)
4278 /* XXX length optimization goes here */
4285 OPCODE type = o->op_type;
4286 return modkids(ck_fun(o), type);
4292 OPCODE type = o->op_type;
4293 return refkids(ck_fun(o), type);
4301 kid = cLISTOPo->op_first;
4304 kid = cLISTOPo->op_first;
4306 if (kid->op_type == OP_PUSHMARK)
4307 kid = kid->op_sibling;
4308 if (kid && o->op_flags & OPf_STACKED)
4309 kid = kid->op_sibling;
4310 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4311 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4312 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4313 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4314 cLISTOPo->op_first->op_sibling = kid;
4315 cLISTOPo->op_last = kid;
4316 kid = kid->op_sibling;
4321 append_elem(o->op_type, o, newDEFSVOP());
4327 if (hints & HINT_LOCALE)
4328 o->op_private |= OPpLOCALE;
4335 ck_fun_locale(OP *o)
4341 if (hints & HINT_LOCALE)
4342 o->op_private |= OPpLOCALE;
4353 if (hints & HINT_LOCALE)
4354 o->op_private |= OPpLOCALE;
4363 o->op_private |= OPpRUNTIME;
4376 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4377 o->op_private |= OPpREPEAT_DOLIST;
4378 cBINOPo->op_first = force_list(cBINOPo->op_first);
4388 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4389 SVOP *kid = (SVOP*)cUNOPo->op_first;
4391 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4393 for (s = SvPVX(kid->op_sv); *s; s++) {
4394 if (*s == ':' && s[1] == ':') {
4396 Move(s+2, s+1, strlen(s+2)+1, char);
4397 --SvCUR(kid->op_sv);
4400 sv_catpvn(kid->op_sv, ".pm", 3);
4409 croak("NOT IMPL LINE %d",__LINE__);
4418 if (o->op_flags & OPf_KIDS) {
4419 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4420 if (kid && kid->op_sibling) {
4421 o->op_type = OP_SSELECT;
4422 o->op_ppaddr = ppaddr[OP_SSELECT];
4424 return fold_constants(o);
4428 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4429 if (kid && kid->op_type == OP_RV2GV)
4430 kid->op_private &= ~HINT_STRICT_REFS;
4437 I32 type = o->op_type;
4439 if (!(o->op_flags & OPf_KIDS)) {
4444 if (!CvUNIQUE(compcv)) {
4445 argop = newOP(OP_PADAV, OPf_REF);
4446 argop->op_targ = 0; /* curpad[0] is @_ */
4449 argop = newUNOP(OP_RV2AV, 0,
4450 scalar(newGVOP(OP_GV, 0,
4451 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4454 argop = newUNOP(OP_RV2AV, 0,
4455 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4456 defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4457 #endif /* USE_THREADS */
4458 return newUNOP(type, 0, scalar(argop));
4460 return scalar(modkids(ck_fun(o), type));
4468 if (hints & HINT_LOCALE)
4469 o->op_private |= OPpLOCALE;
4472 if (o->op_flags & OPf_STACKED) {
4473 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4475 kid = kUNOP->op_first; /* get past rv2gv */
4477 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4479 if (kid->op_type == OP_SCOPE) {
4483 else if (kid->op_type == OP_LEAVE) {
4484 if (o->op_type == OP_SORT) {
4485 null(kid); /* wipe out leave */
4488 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4489 if (k->op_next == kid)
4494 kid->op_next = 0; /* just disconnect the leave */
4495 k = kLISTOP->op_first;
4499 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4500 null(kid); /* wipe out rv2gv */
4501 if (o->op_type == OP_SORT)
4505 o->op_flags |= OPf_SPECIAL;
4517 if (o->op_flags & OPf_STACKED)
4518 return no_fh_allowed(o);
4520 kid = cLISTOPo->op_first;
4521 if (kid->op_type != OP_NULL)
4522 croak("panic: ck_split");
4523 kid = kid->op_sibling;
4524 op_free(cLISTOPo->op_first);
4525 cLISTOPo->op_first = kid;
4527 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4528 cLISTOPo->op_last = kid; /* There was only one element previously */
4531 if (kid->op_type != OP_MATCH) {
4532 OP *sibl = kid->op_sibling;
4533 kid->op_sibling = 0;
4534 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4535 if (cLISTOPo->op_first == cLISTOPo->op_last)
4536 cLISTOPo->op_last = kid;
4537 cLISTOPo->op_first = kid;
4538 kid->op_sibling = sibl;
4541 kid->op_type = OP_PUSHRE;
4542 kid->op_ppaddr = ppaddr[OP_PUSHRE];
4545 if (!kid->op_sibling)
4546 append_elem(OP_SPLIT, o, newDEFSVOP());
4548 kid = kid->op_sibling;
4551 if (!kid->op_sibling)
4552 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4554 kid = kid->op_sibling;
4557 if (kid->op_sibling)
4558 return too_many_arguments(o,op_desc[o->op_type]);
4567 OP *prev = ((cUNOPo->op_first->op_sibling)
4568 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4569 OP *o2 = prev->op_sibling;
4577 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4578 if (cvop->op_type == OP_RV2CV) {
4580 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4581 null(cvop); /* disable rv2cv */
4582 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4583 if (tmpop->op_type == OP_GV) {
4584 cv = GvCVu(tmpop->op_sv);
4585 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4586 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4587 proto = SvPV((SV*)cv, na);
4591 o->op_private |= (hints & HINT_STRICT_REFS);
4592 if (PERLDB_SUB && curstash != debstash)
4593 o->op_private |= OPpENTERSUB_DB;
4594 while (o2 != cvop) {
4598 return too_many_arguments(o, gv_ename(namegv));
4616 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4617 bad_type(arg, "block", gv_ename(namegv), o2);
4622 if (o2->op_type == OP_RV2GV)
4626 OP* sib = kid->op_sibling;
4627 kid->op_sibling = 0;
4628 o2 = newUNOP(OP_RV2GV, 0, kid);
4629 o2->op_sibling = sib;
4630 prev->op_sibling = o2;
4638 if (o2->op_type != OP_RV2GV)
4639 bad_type(arg, "symbol", gv_ename(namegv), o2);
4642 if (o2->op_type != OP_RV2CV)
4643 bad_type(arg, "sub", gv_ename(namegv), o2);
4646 if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4647 bad_type(arg, "scalar", gv_ename(namegv), o2);
4650 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4651 bad_type(arg, "array", gv_ename(namegv), o2);
4654 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4655 bad_type(arg, "hash", gv_ename(namegv), o2);
4659 OP* sib = kid->op_sibling;
4660 kid->op_sibling = 0;
4661 o2 = newUNOP(OP_REFGEN, 0, kid);
4662 o2->op_sibling = sib;
4663 prev->op_sibling = o2;
4674 croak("Malformed prototype for %s: %s",
4675 gv_ename(namegv), SvPV((SV*)cv, na));
4680 mod(o2, OP_ENTERSUB);
4682 o2 = o2->op_sibling;
4684 if (proto && !optional &&
4685 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4686 return too_few_arguments(o, gv_ename(namegv));
4693 SvREADONLY_on(cSVOPo->op_sv);
4700 if (o->op_flags & OPf_KIDS) {
4701 SVOP *kid = (SVOP*)cUNOPo->op_first;
4703 if (kid->op_type == OP_NULL)
4704 kid = (SVOP*)kid->op_sibling;
4706 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4707 o->op_flags |= OPf_SPECIAL;
4712 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4715 peep(register OP *o)
4718 register OP* oldop = 0;
4719 if (!o || o->op_seq)
4724 for (; o; o = o->op_next) {
4730 switch (o->op_type) {
4733 curcop = ((COP*)o); /* for warnings */
4734 o->op_seq = op_seqmax++;
4745 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4747 o->op_seq = op_seqmax++;
4750 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4751 o->op_seq = op_seqmax++;
4752 break; /* Scalar stub must produce undef. List stub is noop */
4756 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4763 if (oldop && o->op_next) {
4764 oldop->op_next = o->op_next;
4767 o->op_seq = op_seqmax++;
4771 if (o->op_next->op_type == OP_RV2SV) {
4772 if (!(o->op_next->op_private & OPpDEREF)) {
4774 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4775 o->op_next = o->op_next->op_next;
4776 o->op_type = OP_GVSV;
4777 o->op_ppaddr = ppaddr[OP_GVSV];
4780 else if (o->op_next->op_type == OP_RV2AV) {
4781 OP* pop = o->op_next->op_next;
4783 if (pop->op_type == OP_CONST &&
4784 (op = pop->op_next) &&
4785 pop->op_next->op_type == OP_AELEM &&
4786 !(pop->op_next->op_private &
4787 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4788 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4792 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4796 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4797 o->op_next = pop->op_next->op_next;
4798 o->op_type = OP_AELEMFAST;
4799 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4800 o->op_private = (U8)i;
4801 GvAVn(((GVOP*)o)->op_gv);
4804 o->op_seq = op_seqmax++;
4808 if (o->op_next->op_type == OP_RV2AV
4809 && (o->op_next->op_flags && OPf_REF))
4812 o->op_next = o->op_next->op_next;
4817 if (o->op_next->op_type == OP_RV2HV
4818 && (o->op_next->op_flags && OPf_REF))
4821 o->op_next = o->op_next->op_next;
4829 o->op_seq = op_seqmax++;
4830 while (cLOGOP->op_other->op_type == OP_NULL)
4831 cLOGOP->op_other = cLOGOP->op_other->op_next;
4832 peep(cLOGOP->op_other);
4836 o->op_seq = op_seqmax++;
4837 peep(cCONDOP->op_true);
4838 peep(cCONDOP->op_false);
4842 o->op_seq = op_seqmax++;
4843 peep(cLOOP->op_redoop);
4844 peep(cLOOP->op_nextop);
4845 peep(cLOOP->op_lastop);
4850 o->op_seq = op_seqmax++;
4851 peep(cPMOP->op_pmreplstart);
4855 o->op_seq = op_seqmax++;
4856 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4857 if (o->op_next->op_sibling &&
4858 o->op_next->op_sibling->op_type != OP_DIE) {
4859 line_t oldline = curcop->cop_line;
4861 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4862 warn("Statement unlikely to be reached");
4863 warn("(Maybe you meant system() when you said exec()?)\n");
4864 curcop->cop_line = oldline;
4878 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
4879 || ((BINOP*)o)->op_last->op_type != OP_CONST)
4881 rop = (UNOP*)((BINOP*)o)->op_first;
4882 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
4884 lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
4885 if (!SvOBJECT(lexname))
4887 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
4888 if (!fields || !GvHV(*fields))
4890 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
4891 key = SvPV(*svp, keylen);
4892 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
4894 croak("No such field \"%s\" in variable %s of type %s",
4895 key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
4897 ind = SvIV(*indsvp);
4899 croak("Bad index while coercing array into hash");
4900 rop->op_type = OP_RV2AV;
4901 rop->op_ppaddr = ppaddr[OP_RV2AV];
4902 o->op_type = OP_AELEM;
4903 o->op_ppaddr = ppaddr[OP_AELEM];
4905 *svp = newSViv(ind);
4910 o->op_seq = op_seqmax++;