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 * In the following definition, the ", Nullop" is just to make the compiler
23 * think the expression is of the right type: croak actually does a Siglongjmp.
25 #define CHECKOP(type,o) \
26 ((op_mask && op_mask[type]) \
27 ? ( op_free((OP*)o), \
28 croak("%s trapped by operation mask", op_desc[type]), \
30 : (*check[type])((OP*)o))
32 static I32 list_assignment _((OP *o));
33 static void bad_type _((I32 n, char *t, char *name, OP *kid));
34 static OP *modkids _((OP *o, I32 type));
35 static OP *no_fh_allowed _((OP *o));
36 static bool scalar_mod_type _((OP *o, I32 type));
37 static OP *scalarboolean _((OP *o));
38 static OP *too_few_arguments _((OP *o, char* name));
39 static OP *too_many_arguments _((OP *o, char* name));
40 static void null _((OP* o));
41 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, U32 seq,
42 CV* startcv, I32 cx_ix));
43 static OP *newDEFSVOP _((void));
48 SV* tmpsv = sv_newmortal();
49 gv_efullname3(tmpsv, gv, Nullch);
50 return SvPV(tmpsv,na);
56 yyerror(form("Missing comma after first argument to %s function",
57 op_desc[o->op_type]));
62 too_few_arguments(OP *o, char *name)
64 yyerror(form("Not enough arguments for %s", name));
69 too_many_arguments(OP *o, char *name)
71 yyerror(form("Too many arguments for %s", name));
76 bad_type(I32 n, char *t, char *name, OP *kid)
78 yyerror(form("Type of arg %d to %s must be %s (not %s)",
79 (int)n, name, t, op_desc[kid->op_type]));
85 int type = o->op_type;
86 if (type != OP_AELEM && type != OP_HELEM) {
87 yyerror(form("Can't use subscript on %s", op_desc[type]));
88 if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV)
89 warn("(Did you mean $ or @ instead of %c?)\n",
90 type == OP_ENTERSUB ? '&' : '%');
94 /* "register" allocation */
97 pad_allocmy(char *name)
103 if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
104 if (!isPRINT(name[1])) {
106 name[2] = toCTRL(name[1]);
109 croak("Can't use global %s in \"my\"",name);
111 if (dowarn && AvFILLp(comppad_name) >= 0) {
112 SV **svp = AvARRAY(comppad_name);
113 for (off = AvFILLp(comppad_name); off > comppad_name_floor; off--) {
116 && SvIVX(sv) == 999999999 /* var is in open scope */
117 && strEQ(name, SvPVX(sv)))
119 warn("\"my\" variable %s masks earlier declaration in same scope", name);
124 off = pad_alloc(OP_PADSV, SVs_PADMY);
126 sv_upgrade(sv, SVt_PVNV);
130 croak("Can't declare class for non-scalar %s in \"my\"",name);
132 (void)SvUPGRADE(sv, SVt_PVMG);
133 SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash);
136 av_store(comppad_name, off, sv);
137 SvNVX(sv) = (double)999999999;
138 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
139 if (!min_intro_pending)
140 min_intro_pending = off;
141 max_intro_pending = off;
143 av_store(comppad, off, (SV*)newAV());
144 else if (*name == '%')
145 av_store(comppad, off, (SV*)newHV());
146 SvPADMY_on(curpad[off]);
151 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
158 register PERL_CONTEXT *cx;
161 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
162 AV *curlist = CvPADLIST(cv);
163 SV **svp = av_fetch(curlist, 0, FALSE);
166 if (!svp || *svp == &sv_undef)
169 svp = AvARRAY(curname);
170 for (off = AvFILLp(curname); off > 0; off--) {
171 if ((sv = svp[off]) &&
174 seq > I_32(SvNVX(sv)) &&
175 strEQ(SvPVX(sv), name))
186 return 0; /* don't clone from inactive stack frame */
190 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
191 oldsv = *av_fetch(oldpad, off, TRUE);
192 if (!newoff) { /* Not a mere clone operation. */
193 SV *namesv = NEWSV(1103,0);
194 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
195 sv_upgrade(namesv, SVt_PVNV);
196 sv_setpv(namesv, name);
197 av_store(comppad_name, newoff, namesv);
198 SvNVX(namesv) = (double)curcop->cop_seq;
199 SvIVX(namesv) = 999999999; /* A ref, intro immediately */
200 SvFAKE_on(namesv); /* A ref, not a real var */
201 if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) {
202 /* "It's closures all the way down." */
206 oldsv = Nullsv; /* no need to keep ref */
211 bcv && bcv != cv && !CvCLONE(bcv);
212 bcv = CvOUTSIDE(bcv)) {
216 if (dowarn && !CvUNIQUE(cv))
218 "Variable \"%s\" may be unavailable",
225 else if (!CvUNIQUE(compcv)) {
226 if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv))
227 warn("Variable \"%s\" will not stay shared", name);
230 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
236 /* Nothing in current lexical context--try eval's context, if any.
237 * This is necessary to let the perldb get at lexically scoped variables.
238 * XXX This will also probably interact badly with eval tree caching.
242 for (i = cx_ix; i >= 0; i--) {
244 switch (cx->cx_type) {
246 if (i == 0 && saweval) {
247 seq = cxstack[saweval].blk_oldcop->cop_seq;
248 return pad_findlex(name, newoff, seq, main_cv, 0);
252 switch (cx->blk_eval.old_op_type) {
257 /* require must have its own scope */
265 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
266 saweval = i; /* so we know where we were called from */
269 seq = cxstack[saweval].blk_oldcop->cop_seq;
270 return pad_findlex(name, newoff, seq, cv, i-1);
278 pad_findmy(char *name)
284 SV **svp = AvARRAY(comppad_name);
285 U32 seq = cop_seqmax;
289 * Special case to get lexical (and hence per-thread) @_.
290 * XXX I need to find out how to tell at parse-time whether use
291 * of @_ should refer to a lexical (from a sub) or defgv (global
292 * scope and maybe weird sub-ish things like formats). See
293 * startsub in perly.y. It's possible that @_ could be lexical
294 * (at least from subs) even in non-threaded perl.
296 if (strEQ(name, "@_"))
297 return 0; /* success. (NOT_IN_PAD indicates failure) */
298 #endif /* USE_THREADS */
300 /* The one we're looking for is probably just before comppad_name_fill. */
301 for (off = AvFILLp(comppad_name); off > 0; off--) {
302 if ((sv = svp[off]) &&
306 seq > I_32(SvNVX(sv)))) &&
307 strEQ(SvPVX(sv), name))
310 return (PADOFFSET)off;
311 pendoff = off; /* this pending def. will override import */
315 /* See if it's in a nested scope */
316 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
318 /* If there is a pending local definition, this new alias must die */
320 SvIVX(AvARRAY(comppad_name)[off]) = seq;
321 return off; /* pad_findlex returns 0 for failure...*/
323 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
327 pad_leavemy(I32 fill)
330 SV **svp = AvARRAY(comppad_name);
332 if (min_intro_pending && fill < min_intro_pending) {
333 for (off = max_intro_pending; off >= min_intro_pending; off--) {
334 if ((sv = svp[off]) && sv != &sv_undef)
335 warn("%s never introduced", SvPVX(sv));
338 /* "Deintroduce" my variables that are leaving with this scope. */
339 for (off = AvFILLp(comppad_name); off > fill; off--) {
340 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
341 SvIVX(sv) = cop_seqmax;
346 pad_alloc(I32 optype, U32 tmptype)
352 if (AvARRAY(comppad) != curpad)
353 croak("panic: pad_alloc");
354 if (pad_reset_pending)
356 if (tmptype & SVs_PADMY) {
358 sv = *av_fetch(comppad, AvFILLp(comppad) + 1, TRUE);
359 } while (SvPADBUSY(sv)); /* need a fresh one */
360 retval = AvFILLp(comppad);
363 SV **names = AvARRAY(comppad_name);
364 SSize_t names_fill = AvFILLp(comppad_name);
367 * "foreach" index vars temporarily become aliases to non-"my"
368 * values. Thus we must skip, not just pad values that are
369 * marked as current pad values, but also those with names.
371 if (++padix <= names_fill &&
372 (sv = names[padix]) && sv != &sv_undef)
374 sv = *av_fetch(comppad, padix, TRUE);
375 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
380 SvFLAGS(sv) |= tmptype;
381 curpad = AvARRAY(comppad);
383 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
384 (unsigned long) thr, (unsigned long) curpad,
385 (long) retval, op_name[optype]));
387 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n",
388 (unsigned long) curpad,
389 (long) retval, op_name[optype]));
390 #endif /* USE_THREADS */
391 return (PADOFFSET)retval;
399 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
400 (unsigned long) thr, (unsigned long) curpad, po));
403 croak("panic: pad_sv po");
404 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n",
405 (unsigned long) curpad, po));
406 #endif /* USE_THREADS */
407 return curpad[po]; /* eventually we'll turn this into a macro */
411 pad_free(PADOFFSET po)
416 if (AvARRAY(comppad) != curpad)
417 croak("panic: pad_free curpad");
419 croak("panic: pad_free po");
421 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
422 (unsigned long) thr, (unsigned long) curpad, po));
424 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n",
425 (unsigned long) curpad, po));
426 #endif /* USE_THREADS */
427 if (curpad[po] && curpad[po] != &sv_undef)
428 SvPADTMP_off(curpad[po]);
434 pad_swipe(PADOFFSET po)
437 if (AvARRAY(comppad) != curpad)
438 croak("panic: pad_swipe curpad");
440 croak("panic: pad_swipe po");
442 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
443 (unsigned long) thr, (unsigned long) curpad, po));
445 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n",
446 (unsigned long) curpad, po));
447 #endif /* USE_THREADS */
448 SvPADTMP_off(curpad[po]);
449 curpad[po] = NEWSV(1107,0);
450 SvPADTMP_on(curpad[po]);
455 /* XXX pad_reset() is currently disabled because it results in serious bugs.
456 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
457 * on the stack by OPs that use them, there are several ways to get an alias
458 * to a shared TARG. Such an alias will change randomly and unpredictably.
459 * We avoid doing this until we can think of a Better Way.
464 #ifdef USE_BROKEN_PAD_RESET
468 if (AvARRAY(comppad) != curpad)
469 croak("panic: pad_reset curpad");
471 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
472 (unsigned long) thr, (unsigned long) curpad));
474 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n",
475 (unsigned long) curpad));
476 #endif /* USE_THREADS */
477 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
478 for (po = AvMAX(comppad); po > padix_floor; po--) {
479 if (curpad[po] && !SvIMMORTAL(curpad[po]))
480 SvPADTMP_off(curpad[po]);
485 pad_reset_pending = FALSE;
489 /* find_threadsv is not reentrant */
491 find_threadsv(char *name)
497 /* We currently only handle names of a single character */
498 p = strchr(threadsv_names, *name);
501 key = p - threadsv_names;
502 svp = av_fetch(thr->threadsv, key, FALSE);
504 SV *sv = NEWSV(0, 0);
505 av_store(thr->threadsv, key, sv);
506 thr->threadsvp = AvARRAY(thr->threadsv);
508 * Some magic variables used to be automagically initialised
509 * in gv_fetchpv. Those which are now per-thread magicals get
510 * initialised here instead.
516 sv_setpv(sv, "\034");
517 sv_magic(sv, 0, 0, name, 1);
526 sv_magic(sv, 0, 0, name, 1);
528 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
529 "find_threadsv: new SV %p for $%s%c\n",
530 sv, (*name < 32) ? "^" : "",
531 (*name < 32) ? toCTRL(*name) : *name));
535 #endif /* USE_THREADS */
542 register OP *kid, *nextkid;
544 if (!o || o->op_seq == (U16)-1)
547 if (o->op_flags & OPf_KIDS) {
548 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
549 nextkid = kid->op_sibling; /* Get before next freeing kid */
554 switch (o->op_type) {
556 o->op_targ = 0; /* Was holding old type, if any. */
559 o->op_targ = 0; /* Was holding hints. */
563 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
565 #endif /* USE_THREADS */
567 if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst))
573 SvREFCNT_dec(cGVOPo->op_gv);
577 Safefree(cCOPo->cop_label);
578 SvREFCNT_dec(cCOPo->cop_filegv);
581 SvREFCNT_dec(cSVOPo->op_sv);
587 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
591 Safefree(cPVOPo->op_pv);
594 op_free(cPMOPo->op_pmreplroot);
598 ReREFCNT_dec(cPMOPo->op_pmregexp);
603 pad_free(o->op_targ);
611 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
612 pad_free(o->op_targ);
613 o->op_targ = o->op_type;
614 o->op_type = OP_NULL;
615 o->op_ppaddr = ppaddr[OP_NULL];
618 /* Contextualizers */
620 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
630 /* establish postfix order */
631 if (cUNOPo->op_first) {
632 o->op_next = LINKLIST(cUNOPo->op_first);
633 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
635 kid->op_next = LINKLIST(kid->op_sibling);
650 if (o && o->op_flags & OPf_KIDS) {
651 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
661 o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
663 line_t oldline = curcop->cop_line;
665 if (copline != NOLINE)
666 curcop->cop_line = copline;
667 warn("Found = in conditional, should be ==");
668 curcop->cop_line = oldline;
678 /* assumes no premature commitment */
679 if (!o || (o->op_flags & OPf_WANT) || error_count
680 || o->op_type == OP_RETURN)
683 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
685 switch (o->op_type) {
687 if (o->op_private & OPpREPEAT_DOLIST)
688 null(((LISTOP*)cBINOPo->op_first)->op_first);
689 scalar(cBINOPo->op_first);
694 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
698 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
699 if (!kPMOP->op_pmreplroot)
700 deprecate("implicit split to @_");
707 if (o->op_flags & OPf_KIDS) {
708 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
714 kid = cLISTOPo->op_first;
716 while (kid = kid->op_sibling) {
722 WITH_THR(curcop = &compiling);
727 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
733 WITH_THR(curcop = &compiling);
746 /* assumes no premature commitment */
747 if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count
748 || o->op_type == OP_RETURN)
751 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
753 switch (o->op_type) {
755 if (!(opargs[o->op_type] & OA_FOLDCONST))
759 if (o->op_flags & OPf_STACKED)
830 if (!(o->op_private & OPpLVAL_INTRO))
831 useless = op_desc[o->op_type];
838 if (!(o->op_private & OPpLVAL_INTRO) &&
839 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
840 useless = "a variable";
845 WITH_THR(curcop = ((COP*)o)); /* for warning below */
851 useless = "a constant";
852 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
854 else if (SvPOK(sv)) {
855 if (strnEQ(SvPVX(sv), "di", 2) ||
856 strnEQ(SvPVX(sv), "ds", 2) ||
857 strnEQ(SvPVX(sv), "ig", 2))
861 null(o); /* don't execute a constant */
862 SvREFCNT_dec(sv); /* don't even remember it */
866 o->op_type = OP_PREINC; /* pre-increment is faster */
867 o->op_ppaddr = ppaddr[OP_PREINC];
871 o->op_type = OP_PREDEC; /* pre-decrement is faster */
872 o->op_ppaddr = ppaddr[OP_PREDEC];
878 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
883 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
884 WITH_THR(curcop = ((COP*)o)); /* for warning below */
885 if (o->op_flags & OPf_STACKED)
891 if (!(o->op_flags & OPf_KIDS))
900 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
907 /* all requires must return a boolean value */
908 o->op_flags &= ~OPf_WANT;
911 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
912 if (!kPMOP->op_pmreplroot)
913 deprecate("implicit split to @_");
917 if (useless && dowarn)
918 warn("Useless use of %s in void context", useless);
926 if (o && o->op_flags & OPf_KIDS) {
927 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
938 /* assumes no premature commitment */
939 if (!o || (o->op_flags & OPf_WANT) || error_count
940 || o->op_type == OP_RETURN)
943 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
945 switch (o->op_type) {
948 list(cBINOPo->op_first);
953 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
960 if (!(o->op_flags & OPf_KIDS))
962 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
963 list(cBINOPo->op_first);
964 return gen_constant_list(o);
971 kid = cLISTOPo->op_first;
973 while (kid = kid->op_sibling) {
979 WITH_THR(curcop = &compiling);
983 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
989 WITH_THR(curcop = &compiling);
992 /* all requires must return a boolean value */
993 o->op_flags &= ~OPf_WANT;
1005 if (o->op_type == OP_LINESEQ ||
1006 o->op_type == OP_SCOPE ||
1007 o->op_type == OP_LEAVE ||
1008 o->op_type == OP_LEAVETRY)
1011 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1012 if (kid->op_sibling) {
1016 curcop = &compiling;
1018 o->op_flags &= ~OPf_PARENS;
1019 if (hints & HINT_BLOCK_SCOPE)
1020 o->op_flags |= OPf_PARENS;
1023 o = newOP(OP_STUB, 0);
1028 modkids(OP *o, I32 type)
1031 if (o && o->op_flags & OPf_KIDS) {
1032 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1038 static I32 modcount;
1041 mod(OP *o, I32 type)
1047 if (!o || error_count)
1050 switch (o->op_type) {
1055 if (!(o->op_private & (OPpCONST_ARYBASE)))
1057 if (eval_start && eval_start->op_type == OP_CONST) {
1058 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
1062 SAVEI32(compiling.cop_arybase);
1063 compiling.cop_arybase = 0;
1065 else if (type == OP_REFGEN)
1068 croak("That use of $[ is unsupported");
1071 if (o->op_flags & OPf_PARENS)
1075 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1076 !(o->op_flags & OPf_STACKED)) {
1077 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1078 o->op_ppaddr = ppaddr[OP_RV2CV];
1079 assert(cUNOPo->op_first->op_type == OP_NULL);
1080 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1086 /* grep, foreach, subcalls, refgen */
1087 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1089 yyerror(form("Can't modify %s in %s",
1090 op_desc[o->op_type],
1091 type ? op_desc[type] : "local"));
1105 case OP_RIGHT_SHIFT:
1114 if (!(o->op_flags & OPf_STACKED))
1120 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1126 if (!type && cUNOPo->op_first->op_type != OP_GV)
1127 croak("Can't localize through a reference");
1128 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1130 return o; /* Treat \(@foo) like ordinary list. */
1134 if (scalar_mod_type(o, type))
1136 ref(cUNOPo->op_first, o->op_type);
1148 if (!type && cUNOPo->op_first->op_type != OP_GV)
1149 croak("Can't localize through a reference");
1150 ref(cUNOPo->op_first, o->op_type);
1154 hints |= HINT_BLOCK_SCOPE;
1163 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1164 return o; /* Treat \(@foo) like ordinary list. */
1165 if (scalar_mod_type(o, type))
1171 croak("Can't localize lexical variable %s",
1172 SvPV(*av_fetch(comppad_name, o->op_targ, 4), na));
1177 modcount++; /* XXX ??? */
1179 #endif /* USE_THREADS */
1185 if (type != OP_SASSIGN)
1191 pad_free(o->op_targ);
1192 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1193 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1194 if (o->op_flags & OPf_KIDS)
1195 mod(cBINOPo->op_first->op_sibling, type);
1200 ref(cBINOPo->op_first, o->op_type);
1201 if (type == OP_ENTERSUB &&
1202 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1203 o->op_private |= OPpLVAL_DEFER;
1210 if (o->op_flags & OPf_KIDS)
1211 mod(cLISTOPo->op_last, type);
1215 if (!(o->op_flags & OPf_KIDS))
1217 if (o->op_targ != OP_LIST) {
1218 mod(cBINOPo->op_first, type);
1223 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1227 o->op_flags |= OPf_MOD;
1229 if (type == OP_AASSIGN || type == OP_SASSIGN)
1230 o->op_flags |= OPf_SPECIAL|OPf_REF;
1232 o->op_private |= OPpLVAL_INTRO;
1233 o->op_flags &= ~OPf_SPECIAL;
1235 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1236 o->op_flags |= OPf_REF;
1241 scalar_mod_type(OP *o, I32 type)
1245 if (o->op_type == OP_RV2GV)
1269 case OP_RIGHT_SHIFT:
1276 case OP_ANDASSIGN: /* may work later */
1277 case OP_ORASSIGN: /* may work later */
1285 refkids(OP *o, I32 type)
1288 if (o && o->op_flags & OPf_KIDS) {
1289 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1296 ref(OP *o, I32 type)
1300 if (!o || error_count)
1303 switch (o->op_type) {
1305 if ((type == OP_DEFINED || type == OP_LOCK) &&
1306 !(o->op_flags & OPf_STACKED)) {
1307 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1308 o->op_ppaddr = ppaddr[OP_RV2CV];
1309 assert(cUNOPo->op_first->op_type == OP_NULL);
1310 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1311 o->op_flags |= OPf_SPECIAL;
1316 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1320 ref(cUNOPo->op_first, o->op_type);
1323 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1324 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1325 : type == OP_RV2HV ? OPpDEREF_HV
1327 o->op_flags |= OPf_MOD;
1332 o->op_flags |= OPf_MOD; /* XXX ??? */
1337 o->op_flags |= OPf_REF;
1340 ref(cUNOPo->op_first, o->op_type);
1345 o->op_flags |= OPf_REF;
1350 if (!(o->op_flags & OPf_KIDS))
1352 ref(cBINOPo->op_first, type);
1356 ref(cBINOPo->op_first, o->op_type);
1357 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1358 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1359 : type == OP_RV2HV ? OPpDEREF_HV
1361 o->op_flags |= OPf_MOD;
1369 if (!(o->op_flags & OPf_KIDS))
1371 ref(cLISTOPo->op_last, type);
1386 if (!o || error_count)
1390 if (type == OP_LIST) {
1391 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1394 else if (type != OP_PADSV &&
1397 type != OP_PUSHMARK)
1399 yyerror(form("Can't declare %s in my", op_desc[o->op_type]));
1402 o->op_flags |= OPf_MOD;
1403 o->op_private |= OPpLVAL_INTRO;
1411 o->op_flags |= OPf_PARENS;
1416 bind_match(I32 type, OP *left, OP *right)
1421 (left->op_type == OP_RV2AV ||
1422 left->op_type == OP_RV2HV ||
1423 left->op_type == OP_PADAV ||
1424 left->op_type == OP_PADHV)) {
1425 char *desc = op_desc[(right->op_type == OP_SUBST ||
1426 right->op_type == OP_TRANS)
1427 ? right->op_type : OP_MATCH];
1428 char *sample = ((left->op_type == OP_RV2AV ||
1429 left->op_type == OP_PADAV)
1430 ? "@array" : "%hash");
1431 warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample);
1434 if (right->op_type == OP_MATCH ||
1435 right->op_type == OP_SUBST ||
1436 right->op_type == OP_TRANS) {
1437 right->op_flags |= OPf_STACKED;
1438 if (right->op_type != OP_MATCH)
1439 left = mod(left, right->op_type);
1440 if (right->op_type == OP_TRANS)
1441 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1443 o = prepend_elem(right->op_type, scalar(left), right);
1445 return newUNOP(OP_NOT, 0, scalar(o));
1449 return bind_match(type, left,
1450 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1458 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1459 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1466 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) {
1467 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1468 o->op_type = OP_LEAVE;
1469 o->op_ppaddr = ppaddr[OP_LEAVE];
1472 if (o->op_type == OP_LINESEQ) {
1474 o->op_type = OP_SCOPE;
1475 o->op_ppaddr = ppaddr[OP_SCOPE];
1476 kid = ((LISTOP*)o)->op_first;
1477 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1478 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1483 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1490 block_start(int full)
1493 int retval = savestack_ix;
1494 SAVEI32(comppad_name_floor);
1496 if ((comppad_name_fill = AvFILLp(comppad_name)) > 0)
1497 comppad_name_floor = comppad_name_fill;
1499 comppad_name_floor = 0;
1501 SAVEI32(min_intro_pending);
1502 SAVEI32(max_intro_pending);
1503 min_intro_pending = 0;
1504 SAVEI32(comppad_name_fill);
1505 SAVEI32(padix_floor);
1506 padix_floor = padix;
1507 pad_reset_pending = FALSE;
1509 hints &= ~HINT_BLOCK_SCOPE;
1514 block_end(I32 floor, OP *seq)
1517 int needblockscope = hints & HINT_BLOCK_SCOPE;
1518 OP* retval = scalarseq(seq);
1520 pad_reset_pending = FALSE;
1522 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1523 pad_leavemy(comppad_name_fill);
1532 OP *o = newOP(OP_THREADSV, 0);
1533 o->op_targ = find_threadsv("_");
1536 return newSVREF(newGVOP(OP_GV, 0, defgv));
1537 #endif /* USE_THREADS */
1545 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o);
1546 eval_start = linklist(eval_root);
1547 eval_root->op_next = 0;
1553 main_root = scope(sawparens(scalarvoid(o)));
1554 curcop = &compiling;
1555 main_start = LINKLIST(main_root);
1556 main_root->op_next = 0;
1560 /* Register with debugger */
1562 CV *cv = perl_get_cv("DB::postponed", FALSE);
1566 XPUSHs((SV*)compiling.cop_filegv);
1568 perl_call_sv((SV*)cv, G_DISCARD);
1575 localize(OP *o, I32 lex)
1577 if (o->op_flags & OPf_PARENS)
1580 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1582 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1583 if (*s == ';' || *s == '=')
1584 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1588 in_my_stash = Nullhv;
1592 return mod(o, OP_NULL); /* a bit kludgey */
1598 if (o->op_type == OP_LIST) {
1601 o2 = newOP(OP_THREADSV, 0);
1602 o2->op_targ = find_threadsv(";");
1604 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1605 #endif /* USE_THREADS */
1606 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1612 fold_constants(register OP *o)
1616 I32 type = o->op_type;
1619 if (opargs[type] & OA_RETSCALAR)
1621 if (opargs[type] & OA_TARGET)
1622 o->op_targ = pad_alloc(type, SVs_PADTMP);
1624 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1625 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1627 if (!(opargs[type] & OA_FOLDCONST))
1642 if (o->op_private & OPpLOCALE)
1647 goto nope; /* Don't try to run w/ errors */
1649 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1650 if (curop->op_type != OP_CONST &&
1651 curop->op_type != OP_LIST &&
1652 curop->op_type != OP_SCALAR &&
1653 curop->op_type != OP_NULL &&
1654 curop->op_type != OP_PUSHMARK) {
1659 curop = LINKLIST(o);
1664 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1665 pad_swipe(o->op_targ);
1666 else if (SvTEMP(sv)) { /* grab mortal temp? */
1667 (void)SvREFCNT_inc(sv);
1671 if (type == OP_RV2GV)
1672 return newGVOP(OP_GV, 0, (GV*)sv);
1674 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1676 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1681 SvIOK_off(sv); /* undo SvIV() damage */
1683 return newSVOP(OP_CONST, 0, sv);
1687 if (!(opargs[type] & OA_OTHERINT))
1690 if (!(hints & HINT_INTEGER)) {
1691 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1694 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1695 if (curop->op_type == OP_CONST) {
1696 if (SvIOK(((SVOP*)curop)->op_sv))
1700 if (opargs[curop->op_type] & OA_RETINTEGER)
1704 o->op_ppaddr = ppaddr[++(o->op_type)];
1711 gen_constant_list(register OP *o)
1715 I32 oldtmps_floor = tmps_floor;
1719 return o; /* Don't attempt to run with errors */
1721 op = curop = LINKLIST(o);
1727 tmps_floor = oldtmps_floor;
1729 o->op_type = OP_RV2AV;
1730 o->op_ppaddr = ppaddr[OP_RV2AV];
1731 curop = ((UNOP*)o)->op_first;
1732 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1739 convert(I32 type, I32 flags, OP *o)
1744 if (!o || o->op_type != OP_LIST)
1745 o = newLISTOP(OP_LIST, 0, o, Nullop);
1747 o->op_flags &= ~OPf_WANT;
1749 if (!(opargs[type] & OA_MARK))
1750 null(cLISTOPo->op_first);
1753 o->op_ppaddr = ppaddr[type];
1754 o->op_flags |= flags;
1756 o = CHECKOP(type, o);
1757 if (o->op_type != type)
1760 if (cLISTOPo->op_children < 7) {
1761 /* XXX do we really need to do this if we're done appending?? */
1762 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1764 cLISTOPo->op_last = last; /* in case check substituted last arg */
1767 return fold_constants(o);
1770 /* List constructors */
1773 append_elem(I32 type, OP *first, OP *last)
1781 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1782 return newLISTOP(type, 0, first, last);
1784 if (first->op_flags & OPf_KIDS)
1785 ((LISTOP*)first)->op_last->op_sibling = last;
1787 first->op_flags |= OPf_KIDS;
1788 ((LISTOP*)first)->op_first = last;
1790 ((LISTOP*)first)->op_last = last;
1791 ((LISTOP*)first)->op_children++;
1796 append_list(I32 type, LISTOP *first, LISTOP *last)
1804 if (first->op_type != type)
1805 return prepend_elem(type, (OP*)first, (OP*)last);
1807 if (last->op_type != type)
1808 return append_elem(type, (OP*)first, (OP*)last);
1810 first->op_last->op_sibling = last->op_first;
1811 first->op_last = last->op_last;
1812 first->op_children += last->op_children;
1813 if (first->op_children)
1814 last->op_flags |= OPf_KIDS;
1821 prepend_elem(I32 type, OP *first, OP *last)
1829 if (last->op_type == type) {
1830 if (type == OP_LIST) { /* already a PUSHMARK there */
1831 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1832 ((LISTOP*)last)->op_first->op_sibling = first;
1835 if (!(last->op_flags & OPf_KIDS)) {
1836 ((LISTOP*)last)->op_last = first;
1837 last->op_flags |= OPf_KIDS;
1839 first->op_sibling = ((LISTOP*)last)->op_first;
1840 ((LISTOP*)last)->op_first = first;
1842 ((LISTOP*)last)->op_children++;
1846 return newLISTOP(type, 0, first, last);
1854 return newOP(OP_STUB, 0);
1860 if (!o || o->op_type != OP_LIST)
1861 o = newLISTOP(OP_LIST, 0, o, Nullop);
1867 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
1871 Newz(1101, listop, 1, LISTOP);
1873 listop->op_type = type;
1874 listop->op_ppaddr = ppaddr[type];
1875 listop->op_children = (first != 0) + (last != 0);
1876 listop->op_flags = flags;
1880 else if (!first && last)
1883 first->op_sibling = last;
1884 listop->op_first = first;
1885 listop->op_last = last;
1886 if (type == OP_LIST) {
1888 pushop = newOP(OP_PUSHMARK, 0);
1889 pushop->op_sibling = first;
1890 listop->op_first = pushop;
1891 listop->op_flags |= OPf_KIDS;
1893 listop->op_last = pushop;
1895 else if (listop->op_children)
1896 listop->op_flags |= OPf_KIDS;
1902 newOP(I32 type, I32 flags)
1905 Newz(1101, o, 1, OP);
1907 o->op_ppaddr = ppaddr[type];
1908 o->op_flags = flags;
1911 o->op_private = 0 + (flags >> 8);
1912 if (opargs[type] & OA_RETSCALAR)
1914 if (opargs[type] & OA_TARGET)
1915 o->op_targ = pad_alloc(type, SVs_PADTMP);
1916 return CHECKOP(type, o);
1920 newUNOP(I32 type, I32 flags, OP *first)
1925 first = newOP(OP_STUB, 0);
1926 if (opargs[type] & OA_MARK)
1927 first = force_list(first);
1929 Newz(1101, unop, 1, UNOP);
1930 unop->op_type = type;
1931 unop->op_ppaddr = ppaddr[type];
1932 unop->op_first = first;
1933 unop->op_flags = flags | OPf_KIDS;
1934 unop->op_private = 1 | (flags >> 8);
1936 if(type == OP_STUDY && first->op_type == OP_MATCH) {
1937 first->op_type = OP_PUSHRE;
1938 first->op_ppaddr = ppaddr[OP_PUSHRE];
1941 unop = (UNOP*) CHECKOP(type, unop);
1945 return fold_constants((OP *) unop);
1949 newBINOP(I32 type, I32 flags, OP *first, OP *last)
1952 Newz(1101, binop, 1, BINOP);
1955 first = newOP(OP_NULL, 0);
1957 binop->op_type = type;
1958 binop->op_ppaddr = ppaddr[type];
1959 binop->op_first = first;
1960 binop->op_flags = flags | OPf_KIDS;
1963 binop->op_private = 1 | (flags >> 8);
1966 binop->op_private = 2 | (flags >> 8);
1967 first->op_sibling = last;
1970 binop = (BINOP*)CHECKOP(type, binop);
1974 binop->op_last = last = binop->op_first->op_sibling;
1976 return fold_constants((OP *)binop);
1980 pmtrans(OP *o, OP *expr, OP *repl)
1982 SV *tstr = ((SVOP*)expr)->op_sv;
1983 SV *rstr = ((SVOP*)repl)->op_sv;
1986 register U8 *t = (U8*)SvPV(tstr, tlen);
1987 register U8 *r = (U8*)SvPV(rstr, rlen);
1993 register short *tbl;
1995 tbl = (short*)cPVOPo->op_pv;
1996 complement = o->op_private & OPpTRANS_COMPLEMENT;
1997 Delete = o->op_private & OPpTRANS_DELETE;
1998 squash = o->op_private & OPpTRANS_SQUASH;
2001 Zero(tbl, 256, short);
2002 for (i = 0; i < tlen; i++)
2004 for (i = 0, j = 0; i < 256; i++) {
2020 if (!rlen && !Delete) {
2023 o->op_private |= OPpTRANS_COUNTONLY;
2025 for (i = 0; i < 256; i++)
2027 for (i = 0, j = 0; i < tlen; i++,j++) {
2030 if (tbl[t[i]] == -1)
2036 if (tbl[t[i]] == -1)
2047 newPMOP(I32 type, I32 flags)
2052 Newz(1101, pmop, 1, PMOP);
2053 pmop->op_type = type;
2054 pmop->op_ppaddr = ppaddr[type];
2055 pmop->op_flags = flags;
2056 pmop->op_private = 0 | (flags >> 8);
2058 if (hints & HINT_LOCALE)
2059 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2061 /* link into pm list */
2062 if (type != OP_TRANS && curstash) {
2063 pmop->op_pmnext = HvPMROOT(curstash);
2064 HvPMROOT(curstash) = pmop;
2071 pmruntime(OP *o, OP *expr, OP *repl)
2076 if (o->op_type == OP_TRANS)
2077 return pmtrans(o, expr, repl);
2079 hints |= HINT_BLOCK_SCOPE;
2082 if (expr->op_type == OP_CONST) {
2084 SV *pat = ((SVOP*)expr)->op_sv;
2085 char *p = SvPV(pat, plen);
2086 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2087 sv_setpvn(pat, "\\s+", 3);
2088 p = SvPV(pat, plen);
2089 pm->op_pmflags |= PMf_SKIPWHITE;
2091 pm->op_pmregexp = pregcomp(p, p + plen, pm);
2092 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2093 pm->op_pmflags |= PMf_WHITE;
2097 if (pm->op_pmflags & PMf_KEEP)
2098 expr = newUNOP(OP_REGCMAYBE,0,expr);
2100 Newz(1101, rcop, 1, LOGOP);
2101 rcop->op_type = OP_REGCOMP;
2102 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2103 rcop->op_first = scalar(expr);
2104 rcop->op_flags |= OPf_KIDS;
2105 rcop->op_private = 1;
2108 /* establish postfix order */
2109 if (pm->op_pmflags & PMf_KEEP) {
2111 rcop->op_next = expr;
2112 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2115 rcop->op_next = LINKLIST(expr);
2116 expr->op_next = (OP*)rcop;
2119 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2124 if (pm->op_pmflags & PMf_EVAL)
2127 else if (repl->op_type == OP_THREADSV
2128 && strchr("&`'123456789+",
2129 threadsv_names[repl->op_targ]))
2133 #endif /* USE_THREADS */
2134 else if (repl->op_type == OP_CONST)
2138 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2139 if (opargs[curop->op_type] & OA_DANGEROUS) {
2141 if (curop->op_type == OP_THREADSV
2142 && strchr("&`'123456789+", curop->op_private)) {
2146 if (curop->op_type == OP_GV) {
2147 GV *gv = ((GVOP*)curop)->op_gv;
2148 if (strchr("&`'123456789+", *GvENAME(gv)))
2151 #endif /* USE_THREADS */
2152 else if (curop->op_type == OP_RV2CV)
2154 else if (curop->op_type == OP_RV2SV ||
2155 curop->op_type == OP_RV2AV ||
2156 curop->op_type == OP_RV2HV ||
2157 curop->op_type == OP_RV2GV) {
2158 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2161 else if (curop->op_type == OP_PADSV ||
2162 curop->op_type == OP_PADAV ||
2163 curop->op_type == OP_PADHV ||
2164 curop->op_type == OP_PADANY) {
2173 if (curop == repl) {
2174 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2175 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2176 prepend_elem(o->op_type, scalar(repl), o);
2179 Newz(1101, rcop, 1, LOGOP);
2180 rcop->op_type = OP_SUBSTCONT;
2181 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2182 rcop->op_first = scalar(repl);
2183 rcop->op_flags |= OPf_KIDS;
2184 rcop->op_private = 1;
2187 /* establish postfix order */
2188 rcop->op_next = LINKLIST(repl);
2189 repl->op_next = (OP*)rcop;
2191 pm->op_pmreplroot = scalar((OP*)rcop);
2192 pm->op_pmreplstart = LINKLIST(rcop);
2201 newSVOP(I32 type, I32 flags, SV *sv)
2204 Newz(1101, svop, 1, SVOP);
2205 svop->op_type = type;
2206 svop->op_ppaddr = ppaddr[type];
2208 svop->op_next = (OP*)svop;
2209 svop->op_flags = flags;
2210 if (opargs[type] & OA_RETSCALAR)
2212 if (opargs[type] & OA_TARGET)
2213 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2214 return CHECKOP(type, svop);
2218 newGVOP(I32 type, I32 flags, GV *gv)
2222 Newz(1101, gvop, 1, GVOP);
2223 gvop->op_type = type;
2224 gvop->op_ppaddr = ppaddr[type];
2225 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2226 gvop->op_next = (OP*)gvop;
2227 gvop->op_flags = flags;
2228 if (opargs[type] & OA_RETSCALAR)
2230 if (opargs[type] & OA_TARGET)
2231 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2232 return CHECKOP(type, gvop);
2236 newPVOP(I32 type, I32 flags, char *pv)
2239 Newz(1101, pvop, 1, PVOP);
2240 pvop->op_type = type;
2241 pvop->op_ppaddr = ppaddr[type];
2243 pvop->op_next = (OP*)pvop;
2244 pvop->op_flags = flags;
2245 if (opargs[type] & OA_RETSCALAR)
2247 if (opargs[type] & OA_TARGET)
2248 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2249 return CHECKOP(type, pvop);
2258 save_hptr(&curstash);
2259 save_item(curstname);
2264 name = SvPV(sv, len);
2265 curstash = gv_stashpvn(name,len,TRUE);
2266 sv_setpvn(curstname, name, len);
2270 sv_setpv(curstname,"<none>");
2278 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2286 if (id->op_type != OP_CONST)
2287 croak("Module name must be constant");
2291 if(version != Nullop) {
2292 SV *vesv = ((SVOP*)version)->op_sv;
2294 if (arg == Nullop && !SvNIOK(vesv)) {
2301 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2302 croak("Version number must be constant number");
2304 /* Make copy of id so we don't free it twice */
2305 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2307 /* Fake up a method call to VERSION */
2308 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2309 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2310 append_elem(OP_LIST,
2311 prepend_elem(OP_LIST, pack, list(version)),
2312 newUNOP(OP_METHOD, 0, meth)));
2316 /* Fake up an import/unimport */
2317 if (arg && arg->op_type == OP_STUB)
2318 imop = arg; /* no import on explicit () */
2319 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2320 imop = Nullop; /* use 5.0; */
2323 /* Make copy of id so we don't free it twice */
2324 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2325 meth = newSVOP(OP_CONST, 0,
2327 ? newSVpv("import", 6)
2328 : newSVpv("unimport", 8)
2330 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2331 append_elem(OP_LIST,
2332 prepend_elem(OP_LIST, pack, list(arg)),
2333 newUNOP(OP_METHOD, 0, meth)));
2336 /* Fake up a require */
2337 rqop = newUNOP(OP_REQUIRE, 0, id);
2339 /* Fake up the BEGIN {}, which does its thing immediately. */
2341 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2343 append_elem(OP_LINESEQ,
2344 append_elem(OP_LINESEQ,
2345 newSTATEOP(0, Nullch, rqop),
2346 newSTATEOP(0, Nullch, veop)),
2347 newSTATEOP(0, Nullch, imop) ));
2354 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2356 return newBINOP(OP_LSLICE, flags,
2357 list(force_list(subscript)),
2358 list(force_list(listval)) );
2362 list_assignment(register OP *o)
2367 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2368 o = cUNOPo->op_first;
2370 if (o->op_type == OP_COND_EXPR) {
2371 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2372 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2377 yyerror("Assignment to both a list and a scalar");
2381 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2382 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2383 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2386 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2389 if (o->op_type == OP_RV2SV)
2396 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2401 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2402 return newLOGOP(optype, 0,
2403 mod(scalar(left), optype),
2404 newUNOP(OP_SASSIGN, 0, scalar(right)));
2407 return newBINOP(optype, OPf_STACKED,
2408 mod(scalar(left), optype), scalar(right));
2412 if (list_assignment(left)) {
2414 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2415 left = mod(left, OP_AASSIGN);
2423 o = newBINOP(OP_AASSIGN, flags,
2424 list(force_list(right)),
2425 list(force_list(left)) );
2426 o->op_private = 0 | (flags >> 8);
2427 if (!(left->op_private & OPpLVAL_INTRO)) {
2428 static int generation = 100;
2432 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2433 if (opargs[curop->op_type] & OA_DANGEROUS) {
2434 if (curop->op_type == OP_GV) {
2435 GV *gv = ((GVOP*)curop)->op_gv;
2436 if (gv == defgv || SvCUR(gv) == generation)
2438 SvCUR(gv) = generation;
2440 else if (curop->op_type == OP_PADSV ||
2441 curop->op_type == OP_PADAV ||
2442 curop->op_type == OP_PADHV ||
2443 curop->op_type == OP_PADANY) {
2444 SV **svp = AvARRAY(comppad_name);
2445 SV *sv = svp[curop->op_targ];
2446 if (SvCUR(sv) == generation)
2448 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2450 else if (curop->op_type == OP_RV2CV)
2452 else if (curop->op_type == OP_RV2SV ||
2453 curop->op_type == OP_RV2AV ||
2454 curop->op_type == OP_RV2HV ||
2455 curop->op_type == OP_RV2GV) {
2456 if (lastop->op_type != OP_GV) /* funny deref? */
2465 o->op_private = OPpASSIGN_COMMON;
2467 if (right && right->op_type == OP_SPLIT) {
2469 if ((tmpop = ((LISTOP*)right)->op_first) &&
2470 tmpop->op_type == OP_PUSHRE)
2472 PMOP *pm = (PMOP*)tmpop;
2473 if (left->op_type == OP_RV2AV &&
2474 !(left->op_private & OPpLVAL_INTRO) &&
2475 !(o->op_private & OPpASSIGN_COMMON) )
2477 tmpop = ((UNOP*)left)->op_first;
2478 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2479 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2480 pm->op_pmflags |= PMf_ONCE;
2481 tmpop = cUNOPo->op_first; /* to list (nulled) */
2482 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2483 tmpop->op_sibling = Nullop; /* don't free split */
2484 right->op_next = tmpop->op_next; /* fix starting loc */
2485 op_free(o); /* blow off assign */
2486 right->op_flags &= ~OPf_WANT;
2487 /* "I don't know and I don't care." */
2492 if (modcount < 10000 &&
2493 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2495 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2497 sv_setiv(sv, modcount+1);
2505 right = newOP(OP_UNDEF, 0);
2506 if (right->op_type == OP_READLINE) {
2507 right->op_flags |= OPf_STACKED;
2508 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2511 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2512 o = newBINOP(OP_SASSIGN, flags,
2513 scalar(right), mod(scalar(left), OP_SASSIGN) );
2525 newSTATEOP(I32 flags, char *label, OP *o)
2528 U32 seq = intro_my();
2531 Newz(1101, cop, 1, COP);
2532 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2533 cop->op_type = OP_DBSTATE;
2534 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2537 cop->op_type = OP_NEXTSTATE;
2538 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2540 cop->op_flags = flags;
2541 cop->op_private = 0 | (flags >> 8);
2543 cop->op_private |= NATIVE_HINTS;
2545 cop->op_next = (OP*)cop;
2548 cop->cop_label = label;
2549 hints |= HINT_BLOCK_SCOPE;
2552 cop->cop_arybase = curcop->cop_arybase;
2554 if (copline == NOLINE)
2555 cop->cop_line = curcop->cop_line;
2557 cop->cop_line = copline;
2560 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2561 cop->cop_stash = curstash;
2563 if (PERLDB_LINE && curstash != debstash) {
2564 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2565 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2566 (void)SvIOK_on(*svp);
2568 SvSTASH(*svp) = (HV*)cop;
2572 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2575 /* "Introduce" my variables to visible status. */
2583 if (! min_intro_pending)
2586 svp = AvARRAY(comppad_name);
2587 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2588 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2589 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2590 SvNVX(sv) = (double)cop_seqmax;
2593 min_intro_pending = 0;
2594 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2595 return cop_seqmax++;
2599 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2605 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2606 return newBINOP(type, flags, scalar(first), scalar(other));
2608 scalarboolean(first);
2609 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2610 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2611 if (type == OP_AND || type == OP_OR) {
2617 first = cUNOPo->op_first;
2619 first->op_next = o->op_next;
2620 cUNOPo->op_first = Nullop;
2624 if (first->op_type == OP_CONST) {
2625 if (dowarn && (first->op_private & OPpCONST_BARE))
2626 warn("Probable precedence problem on %s", op_desc[type]);
2627 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2636 else if (first->op_type == OP_WANTARRAY) {
2642 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2643 OP *k1 = ((UNOP*)first)->op_first;
2644 OP *k2 = k1->op_sibling;
2646 switch (first->op_type)
2649 if (k2 && k2->op_type == OP_READLINE
2650 && (k2->op_flags & OPf_STACKED)
2651 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2652 warnop = k2->op_type;
2656 if (k1->op_type == OP_READDIR
2657 || k1->op_type == OP_GLOB
2658 || k1->op_type == OP_EACH)
2659 warnop = k1->op_type;
2663 line_t oldline = curcop->cop_line;
2664 curcop->cop_line = copline;
2665 warn("Value of %s%s can be \"0\"; test with defined()",
2667 ((warnop == OP_READLINE || warnop == OP_GLOB)
2668 ? " construct" : "() operator"));
2669 curcop->cop_line = oldline;
2676 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2677 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2679 Newz(1101, logop, 1, LOGOP);
2681 logop->op_type = type;
2682 logop->op_ppaddr = ppaddr[type];
2683 logop->op_first = first;
2684 logop->op_flags = flags | OPf_KIDS;
2685 logop->op_other = LINKLIST(other);
2686 logop->op_private = 1 | (flags >> 8);
2688 /* establish postfix order */
2689 logop->op_next = LINKLIST(first);
2690 first->op_next = (OP*)logop;
2691 first->op_sibling = other;
2693 o = newUNOP(OP_NULL, 0, (OP*)logop);
2700 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2707 return newLOGOP(OP_AND, 0, first, trueop);
2709 return newLOGOP(OP_OR, 0, first, falseop);
2711 scalarboolean(first);
2712 if (first->op_type == OP_CONST) {
2713 if (SvTRUE(((SVOP*)first)->op_sv)) {
2724 else if (first->op_type == OP_WANTARRAY) {
2728 Newz(1101, condop, 1, CONDOP);
2730 condop->op_type = OP_COND_EXPR;
2731 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2732 condop->op_first = first;
2733 condop->op_flags = flags | OPf_KIDS;
2734 condop->op_true = LINKLIST(trueop);
2735 condop->op_false = LINKLIST(falseop);
2736 condop->op_private = 1 | (flags >> 8);
2738 /* establish postfix order */
2739 condop->op_next = LINKLIST(first);
2740 first->op_next = (OP*)condop;
2742 first->op_sibling = trueop;
2743 trueop->op_sibling = falseop;
2744 o = newUNOP(OP_NULL, 0, (OP*)condop);
2746 trueop->op_next = o;
2747 falseop->op_next = o;
2753 newRANGE(I32 flags, OP *left, OP *right)
2761 Newz(1101, condop, 1, CONDOP);
2763 condop->op_type = OP_RANGE;
2764 condop->op_ppaddr = ppaddr[OP_RANGE];
2765 condop->op_first = left;
2766 condop->op_flags = OPf_KIDS;
2767 condop->op_true = LINKLIST(left);
2768 condop->op_false = LINKLIST(right);
2769 condop->op_private = 1 | (flags >> 8);
2771 left->op_sibling = right;
2773 condop->op_next = (OP*)condop;
2774 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2775 flop = newUNOP(OP_FLOP, 0, flip);
2776 o = newUNOP(OP_NULL, 0, flop);
2779 left->op_next = flip;
2780 right->op_next = flop;
2782 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2783 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2784 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2785 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2787 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2788 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2791 if (!flip->op_private || !flop->op_private)
2792 linklist(o); /* blow off optimizer unless constant */
2798 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2803 int once = block && block->op_flags & OPf_SPECIAL &&
2804 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2807 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2808 return block; /* do {} while 0 does once */
2809 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2810 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2811 expr = newUNOP(OP_DEFINED, 0,
2812 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2816 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2817 o = newLOGOP(OP_AND, 0, expr, listop);
2819 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2821 if (once && o != listop)
2822 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2825 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
2827 o->op_flags |= flags;
2829 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2834 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2843 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2844 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2845 expr = newUNOP(OP_DEFINED, 0,
2846 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2850 block = newOP(OP_NULL, 0);
2853 next = LINKLIST(cont);
2855 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2856 if ((line_t)whileline != NOLINE) {
2857 copline = whileline;
2858 cont = append_elem(OP_LINESEQ, cont,
2859 newSTATEOP(0, Nullch, Nullop));
2863 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2864 redo = LINKLIST(listop);
2867 o = newLOGOP(OP_AND, 0, expr, scalar(listop));
2868 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2869 op_free(expr); /* oops, it's a while (0) */
2871 return Nullop; /* (listop already freed by newLOGOP) */
2873 ((LISTOP*)listop)->op_last->op_next = condop =
2874 (o == listop ? redo : LINKLIST(o));
2882 Newz(1101,loop,1,LOOP);
2883 loop->op_type = OP_ENTERLOOP;
2884 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2885 loop->op_private = 0;
2886 loop->op_next = (OP*)loop;
2889 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
2891 loop->op_redoop = redo;
2892 loop->op_lastop = o;
2895 loop->op_nextop = next;
2897 loop->op_nextop = o;
2899 o->op_flags |= flags;
2900 o->op_private |= (flags >> 8);
2905 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2913 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2914 sv->op_type = OP_RV2GV;
2915 sv->op_ppaddr = ppaddr[OP_RV2GV];
2917 else if (sv->op_type == OP_PADSV) { /* private variable */
2918 padoff = sv->op_targ;
2922 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
2923 padoff = sv->op_targ;
2924 iterflags |= OPf_SPECIAL;
2929 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2933 padoff = find_threadsv("_");
2934 iterflags |= OPf_SPECIAL;
2936 sv = newGVOP(OP_GV, 0, defgv);
2939 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2940 expr = scalar(ref(expr, OP_ITER));
2941 iterflags |= OPf_STACKED;
2943 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2944 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2946 assert(!loop->op_next);
2947 Renew(loop, 1, LOOP);
2948 loop->op_targ = padoff;
2949 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
2951 return newSTATEOP(0, label, wop);
2955 newLOOPEX(I32 type, OP *label)
2959 if (type != OP_GOTO || label->op_type == OP_CONST) {
2960 /* "last()" means "last" */
2961 if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS))
2962 o = newOP(type, OPf_SPECIAL);
2964 o = newPVOP(type, 0, savepv(label->op_type == OP_CONST
2965 ? SvPVx(((SVOP*)label)->op_sv, na)
2971 if (label->op_type == OP_ENTERSUB)
2972 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2973 o = newUNOP(type, OPf_STACKED, label);
2975 hints |= HINT_BLOCK_SCOPE;
2985 MUTEX_DESTROY(CvMUTEXP(cv));
2986 Safefree(CvMUTEXP(cv));
2989 #endif /* USE_THREADS */
2991 if (!CvXSUB(cv) && CvROOT(cv)) {
2993 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
2994 croak("Can't undef active subroutine");
2997 croak("Can't undef active subroutine");
2998 #endif /* USE_THREADS */
3005 op_free(CvROOT(cv));
3006 CvROOT(cv) = Nullop;
3009 SvPOK_off((SV*)cv); /* forget prototype */
3011 SvREFCNT_dec(CvGV(cv));
3013 SvREFCNT_dec(CvOUTSIDE(cv));
3014 CvOUTSIDE(cv) = Nullcv;
3015 if (CvPADLIST(cv)) {
3016 /* may be during global destruction */
3017 if (SvREFCNT(CvPADLIST(cv))) {
3018 I32 i = AvFILLp(CvPADLIST(cv));
3020 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3021 SV* sv = svp ? *svp : Nullsv;
3024 if (sv == (SV*)comppad_name)
3025 comppad_name = Nullav;
3026 else if (sv == (SV*)comppad) {
3028 curpad = Null(SV**);
3032 SvREFCNT_dec((SV*)CvPADLIST(cv));
3034 CvPADLIST(cv) = Nullav;
3038 #ifdef DEBUG_CLOSURES
3043 CV *outside = CvOUTSIDE(cv);
3044 AV* padlist = CvPADLIST(cv);
3051 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3053 (CvANON(cv) ? "ANON"
3054 : (cv == main_cv) ? "MAIN"
3055 : CvUNIQUE(outside) ? "UNIQUE"
3056 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3059 : CvANON(outside) ? "ANON"
3060 : (outside == main_cv) ? "MAIN"
3061 : CvUNIQUE(outside) ? "UNIQUE"
3062 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3067 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3068 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3069 pname = AvARRAY(pad_name);
3070 ppad = AvARRAY(pad);
3072 for (ix = 1; ix <= AvFILLp(pad_name); ix++) {
3073 if (SvPOK(pname[ix]))
3074 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3076 SvFAKE(pname[ix]) ? "FAKE " : "",
3078 (long)I_32(SvNVX(pname[ix])),
3079 (long)SvIVX(pname[ix]));
3082 #endif /* DEBUG_CLOSURES */
3085 cv_clone2(CV *proto, CV *outside)
3090 AV* protopadlist = CvPADLIST(proto);
3091 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3092 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3093 SV** pname = AvARRAY(protopad_name);
3094 SV** ppad = AvARRAY(protopad);
3095 I32 fname = AvFILLp(protopad_name);
3096 I32 fpad = AvFILLp(protopad);
3100 assert(!CvUNIQUE(proto));
3105 SAVESPTR(comppad_name);
3108 cv = compcv = (CV*)NEWSV(1104,0);
3109 sv_upgrade((SV *)cv, SvTYPE(proto));
3115 New(666, CvMUTEXP(cv), 1, perl_mutex);
3116 MUTEX_INIT(CvMUTEXP(cv));
3118 #endif /* USE_THREADS */
3119 CvFILEGV(cv) = CvFILEGV(proto);
3120 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3121 CvSTASH(cv) = CvSTASH(proto);
3122 CvROOT(cv) = CvROOT(proto);
3123 CvSTART(cv) = CvSTART(proto);
3125 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3128 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3130 comppad_name = newAV();
3131 for (ix = fname; ix >= 0; ix--)
3132 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3136 comppadlist = newAV();
3137 AvREAL_off(comppadlist);
3138 av_store(comppadlist, 0, (SV*)comppad_name);
3139 av_store(comppadlist, 1, (SV*)comppad);
3140 CvPADLIST(cv) = comppadlist;
3141 av_fill(comppad, AvFILLp(protopad));
3142 curpad = AvARRAY(comppad);
3144 av = newAV(); /* will be @_ */
3146 av_store(comppad, 0, (SV*)av);
3147 AvFLAGS(av) = AVf_REIFY;
3149 for (ix = fpad; ix > 0; ix--) {
3150 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3151 if (namesv && namesv != &sv_undef) {
3152 char *name = SvPVX(namesv); /* XXX */
3153 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3154 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3155 CvOUTSIDE(cv), cxstack_ix);
3157 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3159 croak("panic: cv_clone: %s", name);
3161 else { /* our own lexical */
3164 /* anon code -- we'll come back for it */
3165 sv = SvREFCNT_inc(ppad[ix]);
3167 else if (*name == '@')
3169 else if (*name == '%')
3179 SV* sv = NEWSV(0,0);
3185 /* Now that vars are all in place, clone nested closures. */
3187 for (ix = fpad; ix > 0; ix--) {
3188 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3190 && namesv != &sv_undef
3191 && !(SvFLAGS(namesv) & SVf_FAKE)
3192 && *SvPVX(namesv) == '&'
3193 && CvCLONE(ppad[ix]))
3195 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3196 SvREFCNT_dec(ppad[ix]);
3199 curpad[ix] = (SV*)kid;
3203 #ifdef DEBUG_CLOSURES
3204 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3206 PerlIO_printf(Perl_debug_log, " from:\n");
3208 PerlIO_printf(Perl_debug_log, " to:\n");
3219 return cv_clone2(proto, CvOUTSIDE(proto));
3223 cv_ckproto(CV *cv, GV *gv, char *p)
3225 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3226 SV* msg = sv_newmortal();
3230 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3231 sv_setpv(msg, "Prototype mismatch:");
3233 sv_catpvf(msg, " sub %_", name);
3235 sv_catpvf(msg, " (%s)", SvPVX(cv));
3236 sv_catpv(msg, " vs ");
3238 sv_catpvf(msg, "(%s)", p);
3240 sv_catpv(msg, "none");
3251 if (!cv || !SvPOK(cv) || SvCUR(cv))
3255 for (o = CvSTART(cv); o; o = o->op_next) {
3256 OPCODE type = o->op_type;
3258 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3260 if (type == OP_LEAVESUB || type == OP_RETURN)
3264 if (type == OP_CONST)
3266 else if (type == OP_PADSV) {
3267 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3268 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3269 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3281 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3284 char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3285 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3286 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3295 if (!name || GvCVGEN(gv))
3297 else if (cv = GvCV(gv)) {
3298 cv_ckproto(cv, gv, ps);
3299 /* already defined (or promised)? */
3300 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3303 /* just a "sub foo;" when &foo is already defined */
3307 /* ahem, death to those who redefine active sort subs */
3308 if (curstack == sortstack && sortcop == CvSTART(cv))
3309 croak("Can't redefine active sort subroutine %s", name);
3310 const_sv = cv_const_sv(cv);
3311 if (const_sv || dowarn) {
3312 line_t oldline = curcop->cop_line;
3313 curcop->cop_line = copline;
3314 warn(const_sv ? "Constant subroutine %s redefined"
3315 : "Subroutine %s redefined", name);
3316 curcop->cop_line = oldline;
3322 if (cv) { /* must reuse cv if autoloaded */
3324 CvFLAGS(cv) = CvFLAGS(compcv);
3325 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3326 CvOUTSIDE(compcv) = 0;
3327 CvPADLIST(cv) = CvPADLIST(compcv);
3328 CvPADLIST(compcv) = 0;
3329 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3330 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3331 SvREFCNT_dec(compcv);
3341 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3342 CvFILEGV(cv) = curcop->cop_filegv;
3343 CvSTASH(cv) = curstash;
3346 New(666, CvMUTEXP(cv), 1, perl_mutex);
3347 MUTEX_INIT(CvMUTEXP(cv));
3348 #endif /* USE_THREADS */
3351 sv_setpv((SV*)cv, ps);
3357 char *s = strrchr(name, ':');
3359 if (strEQ(s, "BEGIN")) {
3361 "BEGIN not safe after errors--compilation aborted";
3365 /* force display of errors found but not reported */
3366 sv_catpv(ERRSV, not_safe);
3367 croak("%s", SvPVx(ERRSV, na));
3378 if (AvFILLp(comppad_name) < AvFILLp(comppad))
3379 av_store(comppad_name, AvFILLp(comppad), Nullsv);
3382 SV **namep = AvARRAY(comppad_name);
3383 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3386 if (SvIMMORTAL(curpad[ix]))
3389 * The only things that a clonable function needs in its
3390 * pad are references to outer lexicals and anonymous subs.
3391 * The rest are created anew during cloning.
3393 if (!((namesv = namep[ix]) != Nullsv &&
3394 namesv != &sv_undef &&
3396 *SvPVX(namesv) == '&')))
3398 SvREFCNT_dec(curpad[ix]);
3399 curpad[ix] = Nullsv;
3404 AV *av = newAV(); /* Will be @_ */
3406 av_store(comppad, 0, (SV*)av);
3407 AvFLAGS(av) = AVf_REIFY;
3409 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3410 if (SvIMMORTAL(curpad[ix]))
3412 if (!SvPADMY(curpad[ix]))
3413 SvPADTMP_on(curpad[ix]);
3417 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3418 CvSTART(cv) = LINKLIST(CvROOT(cv));
3419 CvROOT(cv)->op_next = 0;
3425 if (PERLDB_SUBLINE && curstash != debstash) {
3426 SV *sv = NEWSV(0,0);
3427 SV *tmpstr = sv_newmortal();
3428 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3432 sv_setpvf(sv, "%_:%ld-%ld",
3433 GvSV(curcop->cop_filegv),
3434 (long)subline, (long)curcop->cop_line);
3435 gv_efullname3(tmpstr, gv, Nullch);
3436 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3437 hv = GvHVn(db_postponed);
3438 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3439 && (cv = GvCV(db_postponed))) {
3444 perl_call_sv((SV*)cv, G_DISCARD);
3448 if ((s = strrchr(name,':')))
3452 if (strEQ(s, "BEGIN")) {
3453 I32 oldscope = scopestack_ix;
3455 SAVESPTR(compiling.cop_filegv);
3456 SAVEI16(compiling.cop_line);
3463 DEBUG_x( dump_sub(gv) );
3464 av_push(beginav, (SV *)cv);
3466 call_list(oldscope, beginav);
3468 curcop = &compiling;
3471 else if (strEQ(s, "END") && !error_count) {
3474 av_unshift(endav, 1);
3475 av_store(endav, 0, (SV *)cv);
3478 else if (strEQ(s, "INIT") && !error_count) {
3481 av_push(initav, SvREFCNT_inc(cv));
3492 newXS(char *name, void (*subaddr) (CV *), char *filename)
3495 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3498 if (cv = (name ? GvCV(gv) : Nullcv)) {
3500 /* just a cached method */
3504 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3505 /* already defined (or promised) */
3507 line_t oldline = curcop->cop_line;
3508 curcop->cop_line = copline;
3509 warn("Subroutine %s redefined",name);
3510 curcop->cop_line = oldline;
3517 if (cv) /* must reuse cv if autoloaded */
3520 cv = (CV*)NEWSV(1105,0);
3521 sv_upgrade((SV *)cv, SVt_PVCV);
3528 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3530 New(666, CvMUTEXP(cv), 1, perl_mutex);
3531 MUTEX_INIT(CvMUTEXP(cv));
3533 #endif /* USE_THREADS */
3534 CvFILEGV(cv) = gv_fetchfile(filename);
3535 CvXSUB(cv) = subaddr;
3538 char *s = strrchr(name,':');
3543 if (strEQ(s, "BEGIN")) {
3546 av_push(beginav, (SV *)cv);
3549 else if (strEQ(s, "END")) {
3552 av_unshift(endav, 1);
3553 av_store(endav, 0, (SV *)cv);
3556 else if (strEQ(s, "INIT")) {
3559 av_push(initav, (SV *)cv);
3569 newFORM(I32 floor, OP *o, OP *block)
3578 name = SvPVx(cSVOPo->op_sv, na);
3581 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3583 if (cv = GvFORM(gv)) {
3585 line_t oldline = curcop->cop_line;
3587 curcop->cop_line = copline;
3588 warn("Format %s redefined",name);
3589 curcop->cop_line = oldline;
3595 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3596 CvFILEGV(cv) = curcop->cop_filegv;
3598 for (ix = AvFILLp(comppad); ix > 0; ix--) {
3599 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3600 SvPADTMP_on(curpad[ix]);
3603 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3604 CvSTART(cv) = LINKLIST(CvROOT(cv));
3605 CvROOT(cv)->op_next = 0;
3615 return newUNOP(OP_REFGEN, 0,
3616 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3622 return newUNOP(OP_REFGEN, 0,
3623 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3627 newANONSUB(I32 floor, OP *proto, OP *block)
3629 return newUNOP(OP_REFGEN, 0,
3630 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3636 switch (o->op_type) {
3638 o->op_type = OP_PADAV;
3639 o->op_ppaddr = ppaddr[OP_PADAV];
3640 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3643 o->op_type = OP_RV2AV;
3644 o->op_ppaddr = ppaddr[OP_RV2AV];
3649 warn("oops: oopsAV");
3658 switch (o->op_type) {
3661 o->op_type = OP_PADHV;
3662 o->op_ppaddr = ppaddr[OP_PADHV];
3663 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3667 o->op_type = OP_RV2HV;
3668 o->op_ppaddr = ppaddr[OP_RV2HV];
3673 warn("oops: oopsHV");
3682 if (o->op_type == OP_PADANY) {
3683 o->op_type = OP_PADAV;
3684 o->op_ppaddr = ppaddr[OP_PADAV];
3687 return newUNOP(OP_RV2AV, 0, scalar(o));
3691 newGVREF(I32 type, OP *o)
3693 if (type == OP_MAPSTART)
3694 return newUNOP(OP_NULL, 0, o);
3695 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3701 if (o->op_type == OP_PADANY) {
3702 o->op_type = OP_PADHV;
3703 o->op_ppaddr = ppaddr[OP_PADHV];
3706 return newUNOP(OP_RV2HV, 0, scalar(o));
3712 croak("NOT IMPL LINE %d",__LINE__);
3718 newCVREF(I32 flags, OP *o)
3720 return newUNOP(OP_RV2CV, flags, scalar(o));
3726 if (o->op_type == OP_PADANY) {
3727 o->op_type = OP_PADSV;
3728 o->op_ppaddr = ppaddr[OP_PADSV];
3731 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3732 o->op_flags |= OPpDONE_SVREF;
3735 return newUNOP(OP_RV2SV, 0, scalar(o));
3738 /* Check routines. */
3746 name = NEWSV(1106,0);
3747 sv_upgrade(name, SVt_PVNV);
3748 sv_setpvn(name, "&", 1);
3751 ix = pad_alloc(o->op_type, SVs_PADMY);
3752 av_store(comppad_name, ix, name);
3753 av_store(comppad, ix, cSVOPo->op_sv);
3754 SvPADMY_on(cSVOPo->op_sv);
3755 cSVOPo->op_sv = Nullsv;
3756 cSVOPo->op_targ = ix;
3763 o->op_private = hints;
3770 if (cUNOPo->op_first->op_type == OP_CONCAT)
3771 o->op_flags |= OPf_STACKED;
3778 if (o->op_flags & OPf_KIDS) {
3781 OPCODE type = o->op_type;
3782 o = modkids(ck_fun(o), type);
3783 kid = cUNOPo->op_first;
3784 newop = kUNOP->op_first->op_sibling;
3786 (newop->op_sibling ||
3787 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3788 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3789 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3793 op_free(kUNOP->op_first);
3794 kUNOP->op_first = newop;
3796 o->op_ppaddr = ppaddr[++o->op_type];
3805 if (o->op_flags & OPf_KIDS) {
3806 OP *kid = cUNOPo->op_first;
3807 if (kid->op_type == OP_HSLICE)
3808 o->op_private |= OPpSLICE;
3809 else if (kid->op_type != OP_HELEM)
3810 croak("%s argument is not a HASH element or slice",
3811 op_desc[o->op_type]);
3820 I32 type = o->op_type;
3822 if (o->op_flags & OPf_KIDS) {
3823 if (cLISTOPo->op_first->op_type == OP_STUB) {
3825 o = newUNOP(type, OPf_SPECIAL,
3826 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
3836 hints |= HINT_BLOCK_SCOPE;
3837 if (o->op_flags & OPf_KIDS) {
3838 SVOP *kid = (SVOP*)cUNOPo->op_first;
3841 o->op_flags &= ~OPf_KIDS;
3844 else if (kid->op_type == OP_LINESEQ) {
3847 kid->op_next = o->op_next;
3848 cUNOPo->op_first = 0;
3851 Newz(1101, enter, 1, LOGOP);
3852 enter->op_type = OP_ENTERTRY;
3853 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3854 enter->op_private = 0;
3856 /* establish postfix order */
3857 enter->op_next = (OP*)enter;
3859 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3860 o->op_type = OP_LEAVETRY;
3861 o->op_ppaddr = ppaddr[OP_LEAVETRY];
3862 enter->op_other = o;
3870 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
3872 o->op_targ = (PADOFFSET)hints;
3880 if (o->op_flags & OPf_STACKED) {
3882 kid = cUNOPo->op_first->op_sibling;
3883 if (kid->op_type == OP_RV2GV)
3895 if (o->op_flags & OPf_KIDS) {
3896 OP *kid = cUNOPo->op_first;
3897 if (kid->op_type != OP_HELEM)
3898 croak("%s argument is not a HASH element", op_desc[o->op_type]);
3905 ck_gvconst(register OP *o)
3907 o = fold_constants(o);
3908 if (o->op_type == OP_CONST)
3914 ck_rvconst(register OP *o)
3917 SVOP *kid = (SVOP*)cUNOPo->op_first;
3919 o->op_private |= (hints & HINT_STRICT_REFS);
3920 if (kid->op_type == OP_CONST) {
3925 name = SvPV(kid->op_sv, na);
3926 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3927 char *badthing = Nullch;
3928 switch (o->op_type) {
3930 badthing = "a SCALAR";
3933 badthing = "an ARRAY";
3936 badthing = "a HASH";
3941 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3944 kid->op_type = OP_GV;
3945 iscv = (o->op_type == OP_RV2CV) * 2;
3946 for (gv = 0; !gv; iscv++) {
3948 * This is a little tricky. We only want to add the symbol if we
3949 * didn't add it in the lexer. Otherwise we get duplicate strict
3950 * warnings. But if we didn't add it in the lexer, we must at
3951 * least pretend like we wanted to add it even if it existed before,
3952 * or we get possible typo warnings. OPpCONST_ENTERED says
3953 * whether the lexer already added THIS instance of this symbol.
3955 gv = gv_fetchpv(name,
3956 iscv | !(kid->op_private & OPpCONST_ENTERED),
3959 : o->op_type == OP_RV2SV
3961 : o->op_type == OP_RV2AV
3963 : o->op_type == OP_RV2HV
3967 SvREFCNT_dec(kid->op_sv);
3968 kid->op_sv = SvREFCNT_inc(gv);
3977 I32 type = o->op_type;
3979 if (o->op_flags & OPf_REF)
3982 if (o->op_flags & OPf_KIDS) {
3983 SVOP *kid = (SVOP*)cUNOPo->op_first;
3985 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3986 OP *newop = newGVOP(type, OPf_REF,
3987 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3994 if (type == OP_FTTTY)
3995 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
3998 return newUNOP(type, 0, newDEFSVOP());
4011 int type = o->op_type;
4012 register I32 oa = opargs[type] >> OASHIFT;
4014 if (o->op_flags & OPf_STACKED) {
4015 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4018 return no_fh_allowed(o);
4021 if (o->op_flags & OPf_KIDS) {
4022 tokid = &cLISTOPo->op_first;
4023 kid = cLISTOPo->op_first;
4024 if (kid->op_type == OP_PUSHMARK ||
4025 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4027 tokid = &kid->op_sibling;
4028 kid = kid->op_sibling;
4030 if (!kid && opargs[type] & OA_DEFGV)
4031 *tokid = kid = newDEFSVOP();
4035 sibl = kid->op_sibling;
4049 if (kid->op_type == OP_CONST &&
4050 (kid->op_private & OPpCONST_BARE)) {
4051 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4052 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4053 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4055 warn("Array @%s missing the @ in argument %ld of %s()",
4056 name, (long)numargs, op_desc[type]);
4059 kid->op_sibling = sibl;
4062 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4063 bad_type(numargs, "array", op_desc[o->op_type], kid);
4067 if (kid->op_type == OP_CONST &&
4068 (kid->op_private & OPpCONST_BARE)) {
4069 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4070 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4071 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4073 warn("Hash %%%s missing the %% in argument %ld of %s()",
4074 name, (long)numargs, op_desc[type]);
4077 kid->op_sibling = sibl;
4080 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4081 bad_type(numargs, "hash", op_desc[o->op_type], kid);
4086 OP *newop = newUNOP(OP_NULL, 0, kid);
4087 kid->op_sibling = 0;
4089 newop->op_next = newop;
4091 kid->op_sibling = sibl;
4096 if (kid->op_type != OP_GV) {
4097 if (kid->op_type == OP_CONST &&
4098 (kid->op_private & OPpCONST_BARE)) {
4099 OP *newop = newGVOP(OP_GV, 0,
4100 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4106 kid->op_sibling = 0;
4107 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4109 kid->op_sibling = sibl;
4115 mod(scalar(kid), type);
4119 tokid = &kid->op_sibling;
4120 kid = kid->op_sibling;
4122 o->op_private |= numargs;
4124 return too_many_arguments(o,op_desc[o->op_type]);
4127 else if (opargs[type] & OA_DEFGV) {
4129 return newUNOP(type, 0, newDEFSVOP());
4133 while (oa & OA_OPTIONAL)
4135 if (oa && oa != OA_LIST)
4136 return too_few_arguments(o,op_desc[o->op_type]);
4146 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4147 append_elem(OP_GLOB, o, newDEFSVOP());
4149 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4150 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4152 if (gv && GvIMPORTED_CV(gv)) {
4153 static int glob_index;
4155 append_elem(OP_GLOB, o,
4156 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4157 o->op_type = OP_LIST;
4158 o->op_ppaddr = ppaddr[OP_LIST];
4159 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4160 cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4161 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4162 append_elem(OP_LIST, o,
4163 scalar(newUNOP(OP_RV2CV, 0,
4164 newGVOP(OP_GV, 0, gv)))));
4165 o = newUNOP(OP_NULL, 0, ck_subr(o));
4166 o->op_targ = OP_GLOB; /* hint at what it used to be */
4169 gv = newGVgen("main");
4171 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4181 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4183 o->op_ppaddr = ppaddr[OP_GREPSTART];
4184 Newz(1101, gwop, 1, LOGOP);
4186 if (o->op_flags & OPf_STACKED) {
4189 kid = cLISTOPo->op_first->op_sibling;
4190 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4193 kid->op_next = (OP*)gwop;
4194 o->op_flags &= ~OPf_STACKED;
4196 kid = cLISTOPo->op_first->op_sibling;
4197 if (type == OP_MAPWHILE)
4204 kid = cLISTOPo->op_first->op_sibling;
4205 if (kid->op_type != OP_NULL)
4206 croak("panic: ck_grep");
4207 kid = kUNOP->op_first;
4209 gwop->op_type = type;
4210 gwop->op_ppaddr = ppaddr[type];
4211 gwop->op_first = listkids(o);
4212 gwop->op_flags |= OPf_KIDS;
4213 gwop->op_private = 1;
4214 gwop->op_other = LINKLIST(kid);
4215 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4216 kid->op_next = (OP*)gwop;
4218 kid = cLISTOPo->op_first->op_sibling;
4219 if (!kid || !kid->op_sibling)
4220 return too_few_arguments(o,op_desc[o->op_type]);
4221 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4222 mod(kid, OP_GREPSTART);
4230 if (o->op_flags & OPf_KIDS) {
4231 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4232 if (kid && kid->op_type == OP_CONST)
4233 fbm_compile(((SVOP*)kid)->op_sv);
4239 ck_lengthconst(OP *o)
4241 /* XXX length optimization goes here */
4248 OPCODE type = o->op_type;
4249 return modkids(ck_fun(o), type);
4255 OPCODE type = o->op_type;
4256 return refkids(ck_fun(o), type);
4264 kid = cLISTOPo->op_first;
4267 kid = cLISTOPo->op_first;
4269 if (kid->op_type == OP_PUSHMARK)
4270 kid = kid->op_sibling;
4271 if (kid && o->op_flags & OPf_STACKED)
4272 kid = kid->op_sibling;
4273 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4274 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4275 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4276 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4277 cLISTOPo->op_first->op_sibling = kid;
4278 cLISTOPo->op_last = kid;
4279 kid = kid->op_sibling;
4284 append_elem(o->op_type, o, newDEFSVOP());
4290 if (hints & HINT_LOCALE)
4291 o->op_private |= OPpLOCALE;
4298 ck_fun_locale(OP *o)
4304 if (hints & HINT_LOCALE)
4305 o->op_private |= OPpLOCALE;
4316 if (hints & HINT_LOCALE)
4317 o->op_private |= OPpLOCALE;
4326 o->op_private |= OPpRUNTIME;
4339 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4340 o->op_private |= OPpREPEAT_DOLIST;
4341 cBINOPo->op_first = force_list(cBINOPo->op_first);
4351 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4352 SVOP *kid = (SVOP*)cUNOPo->op_first;
4354 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4356 for (s = SvPVX(kid->op_sv); *s; s++) {
4357 if (*s == ':' && s[1] == ':') {
4359 Move(s+2, s+1, strlen(s+2)+1, char);
4360 --SvCUR(kid->op_sv);
4363 sv_catpvn(kid->op_sv, ".pm", 3);
4372 croak("NOT IMPL LINE %d",__LINE__);
4381 if (o->op_flags & OPf_KIDS) {
4382 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4383 if (kid && kid->op_sibling) {
4384 o->op_type = OP_SSELECT;
4385 o->op_ppaddr = ppaddr[OP_SSELECT];
4387 return fold_constants(o);
4391 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4392 if (kid && kid->op_type == OP_RV2GV)
4393 kid->op_private &= ~HINT_STRICT_REFS;
4400 I32 type = o->op_type;
4402 if (!(o->op_flags & OPf_KIDS)) {
4407 if (!CvUNIQUE(compcv)) {
4408 argop = newOP(OP_PADAV, OPf_REF);
4409 argop->op_targ = 0; /* curpad[0] is @_ */
4412 argop = newUNOP(OP_RV2AV, 0,
4413 scalar(newGVOP(OP_GV, 0,
4414 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4417 argop = newUNOP(OP_RV2AV, 0,
4418 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4419 defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4420 #endif /* USE_THREADS */
4421 return newUNOP(type, 0, scalar(argop));
4423 return scalar(modkids(ck_fun(o), type));
4431 if (hints & HINT_LOCALE)
4432 o->op_private |= OPpLOCALE;
4435 if (o->op_flags & OPf_STACKED) {
4436 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4438 kid = kUNOP->op_first; /* get past rv2gv */
4440 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4442 if (kid->op_type == OP_SCOPE) {
4446 else if (kid->op_type == OP_LEAVE) {
4447 if (o->op_type == OP_SORT) {
4448 null(kid); /* wipe out leave */
4451 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4452 if (k->op_next == kid)
4457 kid->op_next = 0; /* just disconnect the leave */
4458 k = kLISTOP->op_first;
4462 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4463 null(kid); /* wipe out rv2gv */
4464 if (o->op_type == OP_SORT)
4468 o->op_flags |= OPf_SPECIAL;
4480 if (o->op_flags & OPf_STACKED)
4481 return no_fh_allowed(o);
4483 kid = cLISTOPo->op_first;
4484 if (kid->op_type != OP_NULL)
4485 croak("panic: ck_split");
4486 kid = kid->op_sibling;
4487 op_free(cLISTOPo->op_first);
4488 cLISTOPo->op_first = kid;
4490 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4491 cLISTOPo->op_last = kid; /* There was only one element previously */
4494 if (kid->op_type != OP_MATCH) {
4495 OP *sibl = kid->op_sibling;
4496 kid->op_sibling = 0;
4497 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4498 if (cLISTOPo->op_first == cLISTOPo->op_last)
4499 cLISTOPo->op_last = kid;
4500 cLISTOPo->op_first = kid;
4501 kid->op_sibling = sibl;
4504 kid->op_type = OP_PUSHRE;
4505 kid->op_ppaddr = ppaddr[OP_PUSHRE];
4508 if (!kid->op_sibling)
4509 append_elem(OP_SPLIT, o, newDEFSVOP());
4511 kid = kid->op_sibling;
4514 if (!kid->op_sibling)
4515 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4517 kid = kid->op_sibling;
4520 if (kid->op_sibling)
4521 return too_many_arguments(o,op_desc[o->op_type]);
4530 OP *prev = ((cUNOPo->op_first->op_sibling)
4531 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4532 OP *o2 = prev->op_sibling;
4540 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4541 if (cvop->op_type == OP_RV2CV) {
4543 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4544 null(cvop); /* disable rv2cv */
4545 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4546 if (tmpop->op_type == OP_GV) {
4547 cv = GvCVu(tmpop->op_sv);
4548 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4549 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4550 proto = SvPV((SV*)cv, na);
4554 o->op_private |= (hints & HINT_STRICT_REFS);
4555 if (PERLDB_SUB && curstash != debstash)
4556 o->op_private |= OPpENTERSUB_DB;
4557 while (o2 != cvop) {
4561 return too_many_arguments(o, gv_ename(namegv));
4579 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4580 bad_type(arg, "block", gv_ename(namegv), o2);
4585 if (o2->op_type == OP_RV2GV)
4589 OP* sib = kid->op_sibling;
4590 kid->op_sibling = 0;
4591 o2 = newUNOP(OP_RV2GV, 0, kid);
4592 o2->op_sibling = sib;
4593 prev->op_sibling = o2;
4601 if (o2->op_type != OP_RV2GV)
4602 bad_type(arg, "symbol", gv_ename(namegv), o2);
4605 if (o2->op_type != OP_RV2CV)
4606 bad_type(arg, "sub", gv_ename(namegv), o2);
4609 if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4610 bad_type(arg, "scalar", gv_ename(namegv), o2);
4613 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4614 bad_type(arg, "array", gv_ename(namegv), o2);
4617 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4618 bad_type(arg, "hash", gv_ename(namegv), o2);
4622 OP* sib = kid->op_sibling;
4623 kid->op_sibling = 0;
4624 o2 = newUNOP(OP_REFGEN, 0, kid);
4625 o2->op_sibling = sib;
4626 prev->op_sibling = o2;
4637 croak("Malformed prototype for %s: %s",
4638 gv_ename(namegv), SvPV((SV*)cv, na));
4643 mod(o2, OP_ENTERSUB);
4645 o2 = o2->op_sibling;
4647 if (proto && !optional &&
4648 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4649 return too_few_arguments(o, gv_ename(namegv));
4656 SvREADONLY_on(cSVOPo->op_sv);
4663 if (o->op_flags & OPf_KIDS) {
4664 SVOP *kid = (SVOP*)cUNOPo->op_first;
4666 if (kid->op_type == OP_NULL)
4667 kid = (SVOP*)kid->op_sibling;
4669 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4670 o->op_flags |= OPf_SPECIAL;
4675 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4678 peep(register OP *o)
4681 register OP* oldop = 0;
4682 if (!o || o->op_seq)
4687 for (; o; o = o->op_next) {
4693 switch (o->op_type) {
4696 curcop = ((COP*)o); /* for warnings */
4697 o->op_seq = op_seqmax++;
4708 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4710 o->op_seq = op_seqmax++;
4713 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4714 o->op_seq = op_seqmax++;
4715 break; /* Scalar stub must produce undef. List stub is noop */
4719 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4720 curcop = ((COP*)op);
4726 if (oldop && o->op_next) {
4727 oldop->op_next = o->op_next;
4730 o->op_seq = op_seqmax++;
4734 if (o->op_next->op_type == OP_RV2SV) {
4735 if (!(o->op_next->op_private & OPpDEREF)) {
4737 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4738 o->op_next = o->op_next->op_next;
4739 o->op_type = OP_GVSV;
4740 o->op_ppaddr = ppaddr[OP_GVSV];
4743 else if (o->op_next->op_type == OP_RV2AV) {
4744 OP* pop = o->op_next->op_next;
4746 if (pop->op_type == OP_CONST &&
4747 (op = pop->op_next) &&
4748 pop->op_next->op_type == OP_AELEM &&
4749 !(pop->op_next->op_private &
4750 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4751 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4755 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4759 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4760 o->op_next = pop->op_next->op_next;
4761 o->op_type = OP_AELEMFAST;
4762 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4763 o->op_private = (U8)i;
4764 GvAVn(((GVOP*)o)->op_gv);
4767 o->op_seq = op_seqmax++;
4771 if (o->op_next->op_type == OP_RV2AV
4772 && (o->op_next->op_flags && OPf_REF))
4775 o->op_next = o->op_next->op_next;
4780 if (o->op_next->op_type == OP_RV2HV
4781 && (o->op_next->op_flags && OPf_REF))
4784 o->op_next = o->op_next->op_next;
4792 o->op_seq = op_seqmax++;
4793 while (cLOGOP->op_other->op_type == OP_NULL)
4794 cLOGOP->op_other = cLOGOP->op_other->op_next;
4795 peep(cLOGOP->op_other);
4799 o->op_seq = op_seqmax++;
4800 peep(cCONDOP->op_true);
4801 peep(cCONDOP->op_false);
4805 o->op_seq = op_seqmax++;
4806 peep(cLOOP->op_redoop);
4807 peep(cLOOP->op_nextop);
4808 peep(cLOOP->op_lastop);
4813 o->op_seq = op_seqmax++;
4814 peep(cPMOP->op_pmreplstart);
4818 o->op_seq = op_seqmax++;
4819 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4820 if (o->op_next->op_sibling &&
4821 o->op_next->op_sibling->op_type != OP_DIE) {
4822 line_t oldline = curcop->cop_line;
4824 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4825 warn("Statement unlikely to be reached");
4826 warn("(Maybe you meant system() when you said exec()?)\n");
4827 curcop->cop_line = oldline;
4841 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
4842 || ((BINOP*)o)->op_last->op_type != OP_CONST)
4844 rop = (UNOP*)((BINOP*)o)->op_first;
4845 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
4847 lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
4848 if (!SvOBJECT(lexname))
4850 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
4851 if (!fields || !GvHV(*fields))
4853 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
4854 key = SvPV(*svp, keylen);
4855 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
4857 croak("No such field \"%s\" in variable %s of type %s",
4858 key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
4860 ind = SvIV(*indsvp);
4862 croak("Bad index while coercing array into hash");
4863 rop->op_type = OP_RV2AV;
4864 rop->op_ppaddr = ppaddr[OP_RV2AV];
4865 o->op_type = OP_AELEM;
4866 o->op_ppaddr = ppaddr[OP_AELEM];
4868 *svp = newSViv(ind);
4873 o->op_seq = op_seqmax++;