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 && AvFILL(comppad_name) >= 0) {
112 SV **svp = AvARRAY(comppad_name);
113 for (off = AvFILL(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 #ifndef CAN_PROTOTYPE
152 pad_findlex(name, newoff, seq, startcv, cx_ix)
159 pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix)
167 register PERL_CONTEXT *cx;
170 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
171 AV *curlist = CvPADLIST(cv);
172 SV **svp = av_fetch(curlist, 0, FALSE);
175 if (!svp || *svp == &sv_undef)
178 svp = AvARRAY(curname);
179 for (off = AvFILL(curname); off > 0; off--) {
180 if ((sv = svp[off]) &&
183 seq > I_32(SvNVX(sv)) &&
184 strEQ(SvPVX(sv), name))
195 return 0; /* don't clone from inactive stack frame */
199 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
200 oldsv = *av_fetch(oldpad, off, TRUE);
201 if (!newoff) { /* Not a mere clone operation. */
202 SV *namesv = NEWSV(1103,0);
203 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
204 sv_upgrade(namesv, SVt_PVNV);
205 sv_setpv(namesv, name);
206 av_store(comppad_name, newoff, namesv);
207 SvNVX(namesv) = (double)curcop->cop_seq;
208 SvIVX(namesv) = 999999999; /* A ref, intro immediately */
209 SvFAKE_on(namesv); /* A ref, not a real var */
210 if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) {
211 /* "It's closures all the way down." */
215 oldsv = Nullsv; /* no need to keep ref */
220 bcv && bcv != cv && !CvCLONE(bcv);
221 bcv = CvOUTSIDE(bcv)) {
225 if (dowarn && !CvUNIQUE(cv))
227 "Variable \"%s\" may be unavailable",
234 else if (!CvUNIQUE(compcv)) {
235 if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv))
236 warn("Variable \"%s\" will not stay shared", name);
239 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
245 /* Nothing in current lexical context--try eval's context, if any.
246 * This is necessary to let the perldb get at lexically scoped variables.
247 * XXX This will also probably interact badly with eval tree caching.
251 for (i = cx_ix; i >= 0; i--) {
253 switch (cx->cx_type) {
255 if (i == 0 && saweval) {
256 seq = cxstack[saweval].blk_oldcop->cop_seq;
257 return pad_findlex(name, newoff, seq, main_cv, 0);
261 switch (cx->blk_eval.old_op_type) {
266 /* require must have its own scope */
274 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
275 saweval = i; /* so we know where we were called from */
278 seq = cxstack[saweval].blk_oldcop->cop_seq;
279 return pad_findlex(name, newoff, seq, cv, i-1);
287 pad_findmy(char *name)
293 SV **svp = AvARRAY(comppad_name);
294 U32 seq = cop_seqmax;
298 * Special case to get lexical (and hence per-thread) @_.
299 * XXX I need to find out how to tell at parse-time whether use
300 * of @_ should refer to a lexical (from a sub) or defgv (global
301 * scope and maybe weird sub-ish things like formats). See
302 * startsub in perly.y. It's possible that @_ could be lexical
303 * (at least from subs) even in non-threaded perl.
305 if (strEQ(name, "@_"))
306 return 0; /* success. (NOT_IN_PAD indicates failure) */
307 #endif /* USE_THREADS */
309 /* The one we're looking for is probably just before comppad_name_fill. */
310 for (off = AvFILL(comppad_name); off > 0; off--) {
311 if ((sv = svp[off]) &&
315 seq > I_32(SvNVX(sv)))) &&
316 strEQ(SvPVX(sv), name))
319 return (PADOFFSET)off;
320 pendoff = off; /* this pending def. will override import */
324 /* See if it's in a nested scope */
325 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
327 /* If there is a pending local definition, this new alias must die */
329 SvIVX(AvARRAY(comppad_name)[off]) = seq;
330 return off; /* pad_findlex returns 0 for failure...*/
332 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
336 pad_leavemy(I32 fill)
339 SV **svp = AvARRAY(comppad_name);
341 if (min_intro_pending && fill < min_intro_pending) {
342 for (off = max_intro_pending; off >= min_intro_pending; off--) {
343 if ((sv = svp[off]) && sv != &sv_undef)
344 warn("%s never introduced", SvPVX(sv));
347 /* "Deintroduce" my variables that are leaving with this scope. */
348 for (off = AvFILL(comppad_name); off > fill; off--) {
349 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
350 SvIVX(sv) = cop_seqmax;
355 pad_alloc(I32 optype, U32 tmptype)
361 if (AvARRAY(comppad) != curpad)
362 croak("panic: pad_alloc");
363 if (pad_reset_pending)
365 if (tmptype & SVs_PADMY) {
367 sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
368 } while (SvPADBUSY(sv)); /* need a fresh one */
369 retval = AvFILL(comppad);
372 SV **names = AvARRAY(comppad_name);
373 SSize_t names_fill = AvFILL(comppad_name);
376 * "foreach" index vars temporarily become aliases to non-"my"
377 * values. Thus we must skip, not just pad values that are
378 * marked as current pad values, but also those with names.
380 if (++padix <= names_fill &&
381 (sv = names[padix]) && sv != &sv_undef)
383 sv = *av_fetch(comppad, padix, TRUE);
384 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)))
389 SvFLAGS(sv) |= tmptype;
390 curpad = AvARRAY(comppad);
392 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n",
393 (unsigned long) thr, (unsigned long) curpad,
394 (long) retval, op_name[optype]));
396 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad alloc %ld for %s\n",
397 (long) retval, op_name[optype]));
398 #endif /* USE_THREADS */
399 return (PADOFFSET)retval;
403 #ifndef CAN_PROTOTYPE
408 #endif /* CAN_PROTOTYPE */
412 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n",
413 (unsigned long) thr, (unsigned long) curpad, po));
416 croak("panic: pad_sv po");
417 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad sv %d\n", po));
418 #endif /* USE_THREADS */
419 return curpad[po]; /* eventually we'll turn this into a macro */
423 #ifndef CAN_PROTOTYPE
427 pad_free(PADOFFSET po)
428 #endif /* CAN_PROTOTYPE */
433 if (AvARRAY(comppad) != curpad)
434 croak("panic: pad_free curpad");
436 croak("panic: pad_free po");
438 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n",
439 (unsigned long) thr, (unsigned long) curpad, po));
441 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad free %d\n", po));
442 #endif /* USE_THREADS */
443 if (curpad[po] && curpad[po] != &sv_undef)
444 SvPADTMP_off(curpad[po]);
450 #ifndef CAN_PROTOTYPE
454 pad_swipe(PADOFFSET po)
455 #endif /* CAN_PROTOTYPE */
458 if (AvARRAY(comppad) != curpad)
459 croak("panic: pad_swipe curpad");
461 croak("panic: pad_swipe po");
463 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n",
464 (unsigned long) thr, (unsigned long) curpad, po));
466 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad swipe %d\n", po));
467 #endif /* USE_THREADS */
468 SvPADTMP_off(curpad[po]);
469 curpad[po] = NEWSV(1107,0);
470 SvPADTMP_on(curpad[po]);
481 if (AvARRAY(comppad) != curpad)
482 croak("panic: pad_reset curpad");
484 DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n",
485 (unsigned long) thr, (unsigned long) curpad));
487 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad reset\n"));
488 #endif /* USE_THREADS */
489 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
490 for (po = AvMAX(comppad); po > padix_floor; po--) {
491 if (curpad[po] && !SvIMMORTAL(curpad[po]))
492 SvPADTMP_off(curpad[po]);
496 pad_reset_pending = FALSE;
500 /* find_threadsv is not reentrant */
502 find_threadsv(char *name)
508 /* We currently only handle names of a single character */
509 p = strchr(threadsv_names, *name);
512 key = p - threadsv_names;
513 svp = av_fetch(thr->threadsv, key, FALSE);
515 SV *sv = NEWSV(0, 0);
516 av_store(thr->threadsv, key, sv);
518 * Some magic variables used to be automagically initialised
519 * in gv_fetchpv. Those which are now per-thread magicals get
520 * initialised here instead.
526 sv_setpv(sv, "\034");
527 sv_magic(sv, 0, 0, name, 1);
536 sv_magic(sv, 0, 0, name, 1);
538 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
539 "find_threadsv: new SV %p for $%s%c\n",
540 sv, (*name < 32) ? "^" : "",
541 (*name < 32) ? toCTRL(*name) : *name));
545 #endif /* USE_THREADS */
552 register OP *kid, *nextkid;
554 if (!o || o->op_seq == (U16)-1)
557 if (o->op_flags & OPf_KIDS) {
558 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
559 nextkid = kid->op_sibling; /* Get before next freeing kid */
564 switch (o->op_type) {
566 o->op_targ = 0; /* Was holding old type, if any. */
569 o->op_targ = 0; /* Was holding hints. */
573 o->op_targ = 0; /* Was holding index into thr->threadsv AV. */
575 #endif /* USE_THREADS */
577 if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst))
583 SvREFCNT_dec(cGVOPo->op_gv);
587 Safefree(cCOPo->cop_label);
588 SvREFCNT_dec(cCOPo->cop_filegv);
591 SvREFCNT_dec(cSVOPo->op_sv);
597 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
601 Safefree(cPVOPo->op_pv);
604 op_free(cPMOPo->op_pmreplroot);
608 ReREFCNT_dec(cPMOPo->op_pmregexp);
613 pad_free(o->op_targ);
621 if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0)
622 pad_free(o->op_targ);
623 o->op_targ = o->op_type;
624 o->op_type = OP_NULL;
625 o->op_ppaddr = ppaddr[OP_NULL];
628 /* Contextualizers */
630 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
640 /* establish postfix order */
641 if (cUNOPo->op_first) {
642 o->op_next = LINKLIST(cUNOPo->op_first);
643 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
645 kid->op_next = LINKLIST(kid->op_sibling);
660 if (o && o->op_flags & OPf_KIDS) {
661 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
671 o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
673 line_t oldline = curcop->cop_line;
675 if (copline != NOLINE)
676 curcop->cop_line = copline;
677 warn("Found = in conditional, should be ==");
678 curcop->cop_line = oldline;
688 /* assumes no premature commitment */
689 if (!o || (o->op_flags & OPf_WANT) || error_count
690 || o->op_type == OP_RETURN)
693 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
695 switch (o->op_type) {
697 if (o->op_private & OPpREPEAT_DOLIST)
698 null(((LISTOP*)cBINOPo->op_first)->op_first);
699 scalar(cBINOPo->op_first);
704 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
708 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
709 if (!kPMOP->op_pmreplroot)
710 deprecate("implicit split to @_");
717 if (o->op_flags & OPf_KIDS) {
718 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
724 kid = cLISTOPo->op_first;
726 while (kid = kid->op_sibling) {
732 WITH_THR(curcop = &compiling);
737 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
743 WITH_THR(curcop = &compiling);
756 /* assumes no premature commitment */
757 if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count
758 || o->op_type == OP_RETURN)
761 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
763 switch (o->op_type) {
765 if (!(opargs[o->op_type] & OA_FOLDCONST))
769 if (o->op_flags & OPf_STACKED)
840 if (!(o->op_private & OPpLVAL_INTRO))
841 useless = op_desc[o->op_type];
848 if (!(o->op_private & OPpLVAL_INTRO) &&
849 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
850 useless = "a variable";
855 WITH_THR(curcop = ((COP*)o)); /* for warning below */
861 useless = "a constant";
862 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
864 else if (SvPOK(sv)) {
865 if (strnEQ(SvPVX(sv), "di", 2) ||
866 strnEQ(SvPVX(sv), "ds", 2) ||
867 strnEQ(SvPVX(sv), "ig", 2))
871 null(o); /* don't execute a constant */
872 SvREFCNT_dec(sv); /* don't even remember it */
876 o->op_type = OP_PREINC; /* pre-increment is faster */
877 o->op_ppaddr = ppaddr[OP_PREINC];
881 o->op_type = OP_PREDEC; /* pre-decrement is faster */
882 o->op_ppaddr = ppaddr[OP_PREDEC];
888 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
893 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
894 WITH_THR(curcop = ((COP*)o)); /* for warning below */
895 if (o->op_flags & OPf_STACKED)
901 if (!(o->op_flags & OPf_KIDS))
910 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
917 /* all requires must return a boolean value */
918 o->op_flags &= ~OPf_WANT;
921 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
922 if (!kPMOP->op_pmreplroot)
923 deprecate("implicit split to @_");
927 if (useless && dowarn)
928 warn("Useless use of %s in void context", useless);
936 if (o && o->op_flags & OPf_KIDS) {
937 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
948 /* assumes no premature commitment */
949 if (!o || (o->op_flags & OPf_WANT) || error_count
950 || o->op_type == OP_RETURN)
953 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
955 switch (o->op_type) {
958 list(cBINOPo->op_first);
963 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
970 if (!(o->op_flags & OPf_KIDS))
972 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
973 list(cBINOPo->op_first);
974 return gen_constant_list(o);
981 kid = cLISTOPo->op_first;
983 while (kid = kid->op_sibling) {
989 WITH_THR(curcop = &compiling);
993 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
999 WITH_THR(curcop = &compiling);
1002 /* all requires must return a boolean value */
1003 o->op_flags &= ~OPf_WANT;
1015 if (o->op_type == OP_LINESEQ ||
1016 o->op_type == OP_SCOPE ||
1017 o->op_type == OP_LEAVE ||
1018 o->op_type == OP_LEAVETRY)
1021 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
1022 if (kid->op_sibling) {
1026 curcop = &compiling;
1028 o->op_flags &= ~OPf_PARENS;
1029 if (hints & HINT_BLOCK_SCOPE)
1030 o->op_flags |= OPf_PARENS;
1033 o = newOP(OP_STUB, 0);
1038 modkids(OP *o, I32 type)
1041 if (o && o->op_flags & OPf_KIDS) {
1042 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1048 static I32 modcount;
1051 mod(OP *o, I32 type)
1057 if (!o || error_count)
1060 switch (o->op_type) {
1065 if (!(o->op_private & (OPpCONST_ARYBASE)))
1067 if (eval_start && eval_start->op_type == OP_CONST) {
1068 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
1072 SAVEI32(compiling.cop_arybase);
1073 compiling.cop_arybase = 0;
1075 else if (type == OP_REFGEN)
1078 croak("That use of $[ is unsupported");
1081 if (o->op_flags & OPf_PARENS)
1085 if ((type == OP_UNDEF || type == OP_REFGEN) &&
1086 !(o->op_flags & OPf_STACKED)) {
1087 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1088 o->op_ppaddr = ppaddr[OP_RV2CV];
1089 assert(cUNOPo->op_first->op_type == OP_NULL);
1090 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
1096 /* grep, foreach, subcalls, refgen */
1097 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1099 yyerror(form("Can't modify %s in %s",
1100 op_desc[o->op_type],
1101 type ? op_desc[type] : "local"));
1115 case OP_RIGHT_SHIFT:
1124 if (!(o->op_flags & OPf_STACKED))
1130 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1136 if (!type && cUNOPo->op_first->op_type != OP_GV)
1137 croak("Can't localize through a reference");
1138 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
1140 return o; /* Treat \(@foo) like ordinary list. */
1144 if (scalar_mod_type(o, type))
1146 ref(cUNOPo->op_first, o->op_type);
1158 if (!type && cUNOPo->op_first->op_type != OP_GV)
1159 croak("Can't localize through a reference");
1160 ref(cUNOPo->op_first, o->op_type);
1172 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1173 return o; /* Treat \(@foo) like ordinary list. */
1174 if (scalar_mod_type(o, type))
1180 croak("Can't localize lexical variable %s",
1181 SvPV(*av_fetch(comppad_name, o->op_targ, 4), na));
1186 modcount++; /* XXX ??? */
1188 #endif /* USE_THREADS */
1194 if (type != OP_SASSIGN)
1200 pad_free(o->op_targ);
1201 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
1202 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
1203 if (o->op_flags & OPf_KIDS)
1204 mod(cBINOPo->op_first->op_sibling, type);
1209 ref(cBINOPo->op_first, o->op_type);
1210 if (type == OP_ENTERSUB &&
1211 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1212 o->op_private |= OPpLVAL_DEFER;
1219 if (o->op_flags & OPf_KIDS)
1220 mod(cLISTOPo->op_last, type);
1224 if (!(o->op_flags & OPf_KIDS))
1226 if (o->op_targ != OP_LIST) {
1227 mod(cBINOPo->op_first, type);
1232 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1236 o->op_flags |= OPf_MOD;
1238 if (type == OP_AASSIGN || type == OP_SASSIGN)
1239 o->op_flags |= OPf_SPECIAL|OPf_REF;
1241 o->op_private |= OPpLVAL_INTRO;
1242 o->op_flags &= ~OPf_SPECIAL;
1244 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1245 o->op_flags |= OPf_REF;
1250 scalar_mod_type(OP *o, I32 type)
1254 if (o->op_type == OP_RV2GV)
1278 case OP_RIGHT_SHIFT:
1285 case OP_ANDASSIGN: /* may work later */
1286 case OP_ORASSIGN: /* may work later */
1294 refkids(OP *o, I32 type)
1297 if (o && o->op_flags & OPf_KIDS) {
1298 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1305 ref(OP *o, I32 type)
1309 if (!o || error_count)
1312 switch (o->op_type) {
1314 if ((type == OP_DEFINED || type == OP_LOCK) &&
1315 !(o->op_flags & OPf_STACKED)) {
1316 o->op_type = OP_RV2CV; /* entersub => rv2cv */
1317 o->op_ppaddr = ppaddr[OP_RV2CV];
1318 assert(cUNOPo->op_first->op_type == OP_NULL);
1319 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1320 o->op_flags |= OPf_SPECIAL;
1325 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
1329 ref(cUNOPo->op_first, o->op_type);
1332 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1333 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1334 : type == OP_RV2HV ? OPpDEREF_HV
1336 o->op_flags |= OPf_MOD;
1341 o->op_flags |= OPf_MOD; /* XXX ??? */
1346 o->op_flags |= OPf_REF;
1349 ref(cUNOPo->op_first, o->op_type);
1354 o->op_flags |= OPf_REF;
1359 if (!(o->op_flags & OPf_KIDS))
1361 ref(cBINOPo->op_first, type);
1365 ref(cBINOPo->op_first, o->op_type);
1366 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
1367 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1368 : type == OP_RV2HV ? OPpDEREF_HV
1370 o->op_flags |= OPf_MOD;
1378 if (!(o->op_flags & OPf_KIDS))
1380 ref(cLISTOPo->op_last, type);
1395 if (!o || error_count)
1399 if (type == OP_LIST) {
1400 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1403 else if (type != OP_PADSV &&
1406 type != OP_PUSHMARK)
1408 yyerror(form("Can't declare %s in my", op_desc[o->op_type]));
1411 o->op_flags |= OPf_MOD;
1412 o->op_private |= OPpLVAL_INTRO;
1420 o->op_flags |= OPf_PARENS;
1425 bind_match(I32 type, OP *left, OP *right)
1430 (left->op_type == OP_RV2AV ||
1431 left->op_type == OP_RV2HV ||
1432 left->op_type == OP_PADAV ||
1433 left->op_type == OP_PADHV)) {
1434 char *desc = op_desc[(right->op_type == OP_SUBST ||
1435 right->op_type == OP_TRANS)
1436 ? right->op_type : OP_MATCH];
1437 char *sample = ((left->op_type == OP_RV2AV ||
1438 left->op_type == OP_PADAV)
1439 ? "@array" : "%hash");
1440 warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample);
1443 if (right->op_type == OP_MATCH ||
1444 right->op_type == OP_SUBST ||
1445 right->op_type == OP_TRANS) {
1446 right->op_flags |= OPf_STACKED;
1447 if (right->op_type != OP_MATCH)
1448 left = mod(left, right->op_type);
1449 if (right->op_type == OP_TRANS)
1450 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1452 o = prepend_elem(right->op_type, scalar(left), right);
1454 return newUNOP(OP_NOT, 0, scalar(o));
1458 return bind_match(type, left,
1459 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1467 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1468 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
1475 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) {
1476 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1477 o->op_type = OP_LEAVE;
1478 o->op_ppaddr = ppaddr[OP_LEAVE];
1481 if (o->op_type == OP_LINESEQ) {
1483 o->op_type = OP_SCOPE;
1484 o->op_ppaddr = ppaddr[OP_SCOPE];
1485 kid = ((LISTOP*)o)->op_first;
1486 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1487 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1492 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1499 block_start(int full)
1502 int retval = savestack_ix;
1503 SAVEI32(comppad_name_floor);
1505 if ((comppad_name_fill = AvFILL(comppad_name)) > 0)
1506 comppad_name_floor = comppad_name_fill;
1508 comppad_name_floor = 0;
1510 SAVEI32(min_intro_pending);
1511 SAVEI32(max_intro_pending);
1512 min_intro_pending = 0;
1513 SAVEI32(comppad_name_fill);
1514 SAVEI32(padix_floor);
1515 padix_floor = padix;
1516 pad_reset_pending = FALSE;
1518 hints &= ~HINT_BLOCK_SCOPE;
1523 block_end(I32 floor, OP *seq)
1526 int needblockscope = hints & HINT_BLOCK_SCOPE;
1527 OP* retval = scalarseq(seq);
1529 pad_reset_pending = FALSE;
1531 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1532 pad_leavemy(comppad_name_fill);
1541 OP *o = newOP(OP_THREADSV, 0);
1542 o->op_targ = find_threadsv("_");
1545 return newSVREF(newGVOP(OP_GV, 0, defgv));
1546 #endif /* USE_THREADS */
1554 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o);
1555 eval_start = linklist(eval_root);
1556 eval_root->op_next = 0;
1562 main_root = scope(sawparens(scalarvoid(o)));
1563 curcop = &compiling;
1564 main_start = LINKLIST(main_root);
1565 main_root->op_next = 0;
1569 /* Register with debugger */
1571 CV *cv = perl_get_cv("DB::postponed", FALSE);
1575 XPUSHs((SV*)compiling.cop_filegv);
1577 perl_call_sv((SV*)cv, G_DISCARD);
1584 localize(OP *o, I32 lex)
1586 if (o->op_flags & OPf_PARENS)
1590 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1592 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1593 if (*s == ';' || *s == '=')
1594 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1598 in_my_stash = Nullhv;
1602 return mod(o, OP_NULL); /* a bit kludgey */
1608 if (o->op_type == OP_LIST) {
1611 o2 = newOP(OP_THREADSV, 0);
1612 o2->op_targ = find_threadsv(";");
1614 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1615 #endif /* USE_THREADS */
1616 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
1622 fold_constants(register OP *o)
1626 I32 type = o->op_type;
1629 if (opargs[type] & OA_RETSCALAR)
1631 if (opargs[type] & OA_TARGET)
1632 o->op_targ = pad_alloc(type, SVs_PADTMP);
1634 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1635 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1637 if (!(opargs[type] & OA_FOLDCONST))
1646 if (o->op_private & OPpLOCALE)
1651 goto nope; /* Don't try to run w/ errors */
1653 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1654 if (curop->op_type != OP_CONST &&
1655 curop->op_type != OP_LIST &&
1656 curop->op_type != OP_SCALAR &&
1657 curop->op_type != OP_NULL &&
1658 curop->op_type != OP_PUSHMARK) {
1663 curop = LINKLIST(o);
1668 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1669 pad_swipe(o->op_targ);
1670 else if (SvTEMP(sv)) { /* grab mortal temp? */
1671 (void)SvREFCNT_inc(sv);
1675 if (type == OP_RV2GV)
1676 return newGVOP(OP_GV, 0, (GV*)sv);
1678 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1680 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1685 SvIOK_off(sv); /* undo SvIV() damage */
1687 return newSVOP(OP_CONST, 0, sv);
1691 if (!(opargs[type] & OA_OTHERINT))
1694 if (!(hints & HINT_INTEGER)) {
1695 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1698 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1699 if (curop->op_type == OP_CONST) {
1700 if (SvIOK(((SVOP*)curop)->op_sv))
1704 if (opargs[curop->op_type] & OA_RETINTEGER)
1708 o->op_ppaddr = ppaddr[++(o->op_type)];
1715 gen_constant_list(register OP *o)
1719 I32 oldtmps_floor = tmps_floor;
1723 return o; /* Don't attempt to run with errors */
1725 op = curop = LINKLIST(o);
1731 tmps_floor = oldtmps_floor;
1733 o->op_type = OP_RV2AV;
1734 o->op_ppaddr = ppaddr[OP_RV2AV];
1735 curop = ((UNOP*)o)->op_first;
1736 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1743 convert(I32 type, I32 flags, OP *o)
1748 if (!o || o->op_type != OP_LIST)
1749 o = newLISTOP(OP_LIST, 0, o, Nullop);
1751 o->op_flags &= ~OPf_WANT;
1753 if (!(opargs[type] & OA_MARK))
1754 null(cLISTOPo->op_first);
1757 o->op_ppaddr = ppaddr[type];
1758 o->op_flags |= flags;
1760 o = CHECKOP(type, o);
1761 if (o->op_type != type)
1764 if (cLISTOPo->op_children < 7) {
1765 /* XXX do we really need to do this if we're done appending?? */
1766 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
1768 cLISTOPo->op_last = last; /* in case check substituted last arg */
1771 return fold_constants(o);
1774 /* List constructors */
1777 append_elem(I32 type, OP *first, OP *last)
1785 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1786 return newLISTOP(type, 0, first, last);
1788 if (first->op_flags & OPf_KIDS)
1789 ((LISTOP*)first)->op_last->op_sibling = last;
1791 first->op_flags |= OPf_KIDS;
1792 ((LISTOP*)first)->op_first = last;
1794 ((LISTOP*)first)->op_last = last;
1795 ((LISTOP*)first)->op_children++;
1800 append_list(I32 type, LISTOP *first, LISTOP *last)
1808 if (first->op_type != type)
1809 return prepend_elem(type, (OP*)first, (OP*)last);
1811 if (last->op_type != type)
1812 return append_elem(type, (OP*)first, (OP*)last);
1814 first->op_last->op_sibling = last->op_first;
1815 first->op_last = last->op_last;
1816 first->op_children += last->op_children;
1817 if (first->op_children)
1818 last->op_flags |= OPf_KIDS;
1825 prepend_elem(I32 type, OP *first, OP *last)
1833 if (last->op_type == type) {
1834 if (type == OP_LIST) { /* already a PUSHMARK there */
1835 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1836 ((LISTOP*)last)->op_first->op_sibling = first;
1839 if (!(last->op_flags & OPf_KIDS)) {
1840 ((LISTOP*)last)->op_last = first;
1841 last->op_flags |= OPf_KIDS;
1843 first->op_sibling = ((LISTOP*)last)->op_first;
1844 ((LISTOP*)last)->op_first = first;
1846 ((LISTOP*)last)->op_children++;
1850 return newLISTOP(type, 0, first, last);
1858 return newOP(OP_STUB, 0);
1864 if (!o || o->op_type != OP_LIST)
1865 o = newLISTOP(OP_LIST, 0, o, Nullop);
1871 newLISTOP(I32 type, I32 flags, OP *first, OP *last)
1875 Newz(1101, listop, 1, LISTOP);
1877 listop->op_type = type;
1878 listop->op_ppaddr = ppaddr[type];
1879 listop->op_children = (first != 0) + (last != 0);
1880 listop->op_flags = flags;
1884 else if (!first && last)
1887 first->op_sibling = last;
1888 listop->op_first = first;
1889 listop->op_last = last;
1890 if (type == OP_LIST) {
1892 pushop = newOP(OP_PUSHMARK, 0);
1893 pushop->op_sibling = first;
1894 listop->op_first = pushop;
1895 listop->op_flags |= OPf_KIDS;
1897 listop->op_last = pushop;
1899 else if (listop->op_children)
1900 listop->op_flags |= OPf_KIDS;
1906 newOP(I32 type, I32 flags)
1909 Newz(1101, o, 1, OP);
1911 o->op_ppaddr = ppaddr[type];
1912 o->op_flags = flags;
1915 o->op_private = 0 + (flags >> 8);
1916 if (opargs[type] & OA_RETSCALAR)
1918 if (opargs[type] & OA_TARGET)
1919 o->op_targ = pad_alloc(type, SVs_PADTMP);
1920 return CHECKOP(type, o);
1924 newUNOP(I32 type, I32 flags, OP *first)
1929 first = newOP(OP_STUB, 0);
1930 if (opargs[type] & OA_MARK)
1931 first = force_list(first);
1933 Newz(1101, unop, 1, UNOP);
1934 unop->op_type = type;
1935 unop->op_ppaddr = ppaddr[type];
1936 unop->op_first = first;
1937 unop->op_flags = flags | OPf_KIDS;
1938 unop->op_private = 1 | (flags >> 8);
1940 if(type == OP_STUDY && first->op_type == OP_MATCH) {
1941 first->op_type = OP_PUSHRE;
1942 first->op_ppaddr = ppaddr[OP_PUSHRE];
1945 unop = (UNOP*) CHECKOP(type, unop);
1949 return fold_constants((OP *) unop);
1953 newBINOP(I32 type, I32 flags, OP *first, OP *last)
1956 Newz(1101, binop, 1, BINOP);
1959 first = newOP(OP_NULL, 0);
1961 binop->op_type = type;
1962 binop->op_ppaddr = ppaddr[type];
1963 binop->op_first = first;
1964 binop->op_flags = flags | OPf_KIDS;
1967 binop->op_private = 1 | (flags >> 8);
1970 binop->op_private = 2 | (flags >> 8);
1971 first->op_sibling = last;
1974 binop = (BINOP*)CHECKOP(type, binop);
1978 binop->op_last = last = binop->op_first->op_sibling;
1980 return fold_constants((OP *)binop);
1984 pmtrans(OP *o, OP *expr, OP *repl)
1986 SV *tstr = ((SVOP*)expr)->op_sv;
1987 SV *rstr = ((SVOP*)repl)->op_sv;
1990 register U8 *t = (U8*)SvPV(tstr, tlen);
1991 register U8 *r = (U8*)SvPV(rstr, rlen);
1997 register short *tbl;
1999 tbl = (short*)cPVOPo->op_pv;
2000 complement = o->op_private & OPpTRANS_COMPLEMENT;
2001 Delete = o->op_private & OPpTRANS_DELETE;
2002 squash = o->op_private & OPpTRANS_SQUASH;
2005 Zero(tbl, 256, short);
2006 for (i = 0; i < tlen; i++)
2008 for (i = 0, j = 0; i < 256; i++) {
2024 if (!rlen && !Delete) {
2027 o->op_private |= OPpTRANS_COUNTONLY;
2029 for (i = 0; i < 256; i++)
2031 for (i = 0, j = 0; i < tlen; i++,j++) {
2034 if (tbl[t[i]] == -1)
2040 if (tbl[t[i]] == -1)
2051 newPMOP(I32 type, I32 flags)
2056 Newz(1101, pmop, 1, PMOP);
2057 pmop->op_type = type;
2058 pmop->op_ppaddr = ppaddr[type];
2059 pmop->op_flags = flags;
2060 pmop->op_private = 0 | (flags >> 8);
2062 if (hints & HINT_LOCALE)
2063 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2065 /* link into pm list */
2066 if (type != OP_TRANS && curstash) {
2067 pmop->op_pmnext = HvPMROOT(curstash);
2068 HvPMROOT(curstash) = pmop;
2075 pmruntime(OP *o, OP *expr, OP *repl)
2080 if (o->op_type == OP_TRANS)
2081 return pmtrans(o, expr, repl);
2083 hints |= HINT_BLOCK_SCOPE;
2086 if (expr->op_type == OP_CONST) {
2088 SV *pat = ((SVOP*)expr)->op_sv;
2089 char *p = SvPV(pat, plen);
2090 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
2091 sv_setpvn(pat, "\\s+", 3);
2092 p = SvPV(pat, plen);
2093 pm->op_pmflags |= PMf_SKIPWHITE;
2095 pm->op_pmregexp = pregcomp(p, p + plen, pm);
2096 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2097 pm->op_pmflags |= PMf_WHITE;
2101 if (pm->op_pmflags & PMf_KEEP)
2102 expr = newUNOP(OP_REGCMAYBE,0,expr);
2104 Newz(1101, rcop, 1, LOGOP);
2105 rcop->op_type = OP_REGCOMP;
2106 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2107 rcop->op_first = scalar(expr);
2108 rcop->op_flags |= OPf_KIDS;
2109 rcop->op_private = 1;
2112 /* establish postfix order */
2113 if (pm->op_pmflags & PMf_KEEP) {
2115 rcop->op_next = expr;
2116 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2119 rcop->op_next = LINKLIST(expr);
2120 expr->op_next = (OP*)rcop;
2123 prepend_elem(o->op_type, scalar((OP*)rcop), o);
2128 if (pm->op_pmflags & PMf_EVAL)
2131 else if (repl->op_type == OP_THREADSV
2132 && strchr("&`'123456789+",
2133 threadsv_names[repl->op_targ]))
2137 #endif /* USE_THREADS */
2138 else if (repl->op_type == OP_CONST)
2142 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2143 if (opargs[curop->op_type] & OA_DANGEROUS) {
2145 if (curop->op_type == OP_THREADSV
2146 && strchr("&`'123456789+", curop->op_private)) {
2150 if (curop->op_type == OP_GV) {
2151 GV *gv = ((GVOP*)curop)->op_gv;
2152 if (strchr("&`'123456789+", *GvENAME(gv)))
2155 #endif /* USE_THREADS */
2156 else if (curop->op_type == OP_RV2CV)
2158 else if (curop->op_type == OP_RV2SV ||
2159 curop->op_type == OP_RV2AV ||
2160 curop->op_type == OP_RV2HV ||
2161 curop->op_type == OP_RV2GV) {
2162 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2165 else if (curop->op_type == OP_PADSV ||
2166 curop->op_type == OP_PADAV ||
2167 curop->op_type == OP_PADHV ||
2168 curop->op_type == OP_PADANY) {
2177 if (curop == repl) {
2178 pm->op_pmflags |= PMf_CONST; /* const for long enough */
2179 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
2180 prepend_elem(o->op_type, scalar(repl), o);
2183 Newz(1101, rcop, 1, LOGOP);
2184 rcop->op_type = OP_SUBSTCONT;
2185 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2186 rcop->op_first = scalar(repl);
2187 rcop->op_flags |= OPf_KIDS;
2188 rcop->op_private = 1;
2191 /* establish postfix order */
2192 rcop->op_next = LINKLIST(repl);
2193 repl->op_next = (OP*)rcop;
2195 pm->op_pmreplroot = scalar((OP*)rcop);
2196 pm->op_pmreplstart = LINKLIST(rcop);
2205 newSVOP(I32 type, I32 flags, SV *sv)
2208 Newz(1101, svop, 1, SVOP);
2209 svop->op_type = type;
2210 svop->op_ppaddr = ppaddr[type];
2212 svop->op_next = (OP*)svop;
2213 svop->op_flags = flags;
2214 if (opargs[type] & OA_RETSCALAR)
2216 if (opargs[type] & OA_TARGET)
2217 svop->op_targ = pad_alloc(type, SVs_PADTMP);
2218 return CHECKOP(type, svop);
2222 newGVOP(I32 type, I32 flags, GV *gv)
2226 Newz(1101, gvop, 1, GVOP);
2227 gvop->op_type = type;
2228 gvop->op_ppaddr = ppaddr[type];
2229 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
2230 gvop->op_next = (OP*)gvop;
2231 gvop->op_flags = flags;
2232 if (opargs[type] & OA_RETSCALAR)
2234 if (opargs[type] & OA_TARGET)
2235 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
2236 return CHECKOP(type, gvop);
2240 newPVOP(I32 type, I32 flags, char *pv)
2243 Newz(1101, pvop, 1, PVOP);
2244 pvop->op_type = type;
2245 pvop->op_ppaddr = ppaddr[type];
2247 pvop->op_next = (OP*)pvop;
2248 pvop->op_flags = flags;
2249 if (opargs[type] & OA_RETSCALAR)
2251 if (opargs[type] & OA_TARGET)
2252 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2253 return CHECKOP(type, pvop);
2262 save_hptr(&curstash);
2263 save_item(curstname);
2268 name = SvPV(sv, len);
2269 curstash = gv_stashpvn(name,len,TRUE);
2270 sv_setpvn(curstname, name, len);
2274 sv_setpv(curstname,"<none>");
2282 utilize(int aver, I32 floor, OP *version, OP *id, OP *arg)
2290 if (id->op_type != OP_CONST)
2291 croak("Module name must be constant");
2295 if(version != Nullop) {
2296 SV *vesv = ((SVOP*)version)->op_sv;
2298 if (arg == Nullop && !SvNIOK(vesv)) {
2305 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2306 croak("Version number must be constant number");
2308 /* Make copy of id so we don't free it twice */
2309 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2311 /* Fake up a method call to VERSION */
2312 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2313 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2314 append_elem(OP_LIST,
2315 prepend_elem(OP_LIST, pack, list(version)),
2316 newUNOP(OP_METHOD, 0, meth)));
2320 /* Fake up an import/unimport */
2321 if (arg && arg->op_type == OP_STUB)
2322 imop = arg; /* no import on explicit () */
2323 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2324 imop = Nullop; /* use 5.0; */
2327 /* Make copy of id so we don't free it twice */
2328 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2329 meth = newSVOP(OP_CONST, 0,
2331 ? newSVpv("import", 6)
2332 : newSVpv("unimport", 8)
2334 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2335 append_elem(OP_LIST,
2336 prepend_elem(OP_LIST, pack, list(arg)),
2337 newUNOP(OP_METHOD, 0, meth)));
2340 /* Fake up a require */
2341 rqop = newUNOP(OP_REQUIRE, 0, id);
2343 /* Fake up the BEGIN {}, which does its thing immediately. */
2345 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2347 append_elem(OP_LINESEQ,
2348 append_elem(OP_LINESEQ,
2349 newSTATEOP(0, Nullch, rqop),
2350 newSTATEOP(0, Nullch, veop)),
2351 newSTATEOP(0, Nullch, imop) ));
2358 newSLICEOP(I32 flags, OP *subscript, OP *listval)
2360 return newBINOP(OP_LSLICE, flags,
2361 list(force_list(subscript)),
2362 list(force_list(listval)) );
2366 list_assignment(register OP *o)
2371 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
2372 o = cUNOPo->op_first;
2374 if (o->op_type == OP_COND_EXPR) {
2375 I32 t = list_assignment(cCONDOPo->op_first->op_sibling);
2376 I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling);
2381 yyerror("Assignment to both a list and a scalar");
2385 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
2386 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
2387 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
2390 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
2393 if (o->op_type == OP_RV2SV)
2400 newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
2405 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2406 return newLOGOP(optype, 0,
2407 mod(scalar(left), optype),
2408 newUNOP(OP_SASSIGN, 0, scalar(right)));
2411 return newBINOP(optype, OPf_STACKED,
2412 mod(scalar(left), optype), scalar(right));
2416 if (list_assignment(left)) {
2418 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2419 left = mod(left, OP_AASSIGN);
2427 o = newBINOP(OP_AASSIGN, flags,
2428 list(force_list(right)),
2429 list(force_list(left)) );
2430 o->op_private = 0 | (flags >> 8);
2431 if (!(left->op_private & OPpLVAL_INTRO)) {
2432 static int generation = 100;
2436 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
2437 if (opargs[curop->op_type] & OA_DANGEROUS) {
2438 if (curop->op_type == OP_GV) {
2439 GV *gv = ((GVOP*)curop)->op_gv;
2440 if (gv == defgv || SvCUR(gv) == generation)
2442 SvCUR(gv) = generation;
2444 else if (curop->op_type == OP_PADSV ||
2445 curop->op_type == OP_PADAV ||
2446 curop->op_type == OP_PADHV ||
2447 curop->op_type == OP_PADANY) {
2448 SV **svp = AvARRAY(comppad_name);
2449 SV *sv = svp[curop->op_targ];
2450 if (SvCUR(sv) == generation)
2452 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2454 else if (curop->op_type == OP_RV2CV)
2456 else if (curop->op_type == OP_RV2SV ||
2457 curop->op_type == OP_RV2AV ||
2458 curop->op_type == OP_RV2HV ||
2459 curop->op_type == OP_RV2GV) {
2460 if (lastop->op_type != OP_GV) /* funny deref? */
2469 o->op_private = OPpASSIGN_COMMON;
2471 if (right && right->op_type == OP_SPLIT) {
2473 if ((tmpop = ((LISTOP*)right)->op_first) &&
2474 tmpop->op_type == OP_PUSHRE)
2476 PMOP *pm = (PMOP*)tmpop;
2477 if (left->op_type == OP_RV2AV &&
2478 !(left->op_private & OPpLVAL_INTRO) &&
2479 !(o->op_private & OPpASSIGN_COMMON) )
2481 tmpop = ((UNOP*)left)->op_first;
2482 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2483 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2484 pm->op_pmflags |= PMf_ONCE;
2485 tmpop = cUNOPo->op_first; /* to list (nulled) */
2486 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2487 tmpop->op_sibling = Nullop; /* don't free split */
2488 right->op_next = tmpop->op_next; /* fix starting loc */
2489 op_free(o); /* blow off assign */
2490 right->op_flags &= ~OPf_WANT;
2491 /* "I don't know and I don't care." */
2496 if (modcount < 10000 &&
2497 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2499 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2501 sv_setiv(sv, modcount+1);
2509 right = newOP(OP_UNDEF, 0);
2510 if (right->op_type == OP_READLINE) {
2511 right->op_flags |= OPf_STACKED;
2512 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2515 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2516 o = newBINOP(OP_SASSIGN, flags,
2517 scalar(right), mod(scalar(left), OP_SASSIGN) );
2529 newSTATEOP(I32 flags, char *label, OP *o)
2532 U32 seq = intro_my();
2535 Newz(1101, cop, 1, COP);
2536 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
2537 cop->op_type = OP_DBSTATE;
2538 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2541 cop->op_type = OP_NEXTSTATE;
2542 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2544 cop->op_flags = flags;
2545 cop->op_private = 0 | (flags >> 8);
2547 cop->op_private |= NATIVE_HINTS;
2549 cop->op_next = (OP*)cop;
2552 cop->cop_label = label;
2553 hints |= HINT_BLOCK_SCOPE;
2556 cop->cop_arybase = curcop->cop_arybase;
2558 if (copline == NOLINE)
2559 cop->cop_line = curcop->cop_line;
2561 cop->cop_line = copline;
2564 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
2565 cop->cop_stash = curstash;
2567 if (PERLDB_LINE && curstash != debstash) {
2568 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2569 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2570 (void)SvIOK_on(*svp);
2572 SvSTASH(*svp) = (HV*)cop;
2576 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
2579 /* "Introduce" my variables to visible status. */
2587 if (! min_intro_pending)
2590 svp = AvARRAY(comppad_name);
2591 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2592 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2593 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2594 SvNVX(sv) = (double)cop_seqmax;
2597 min_intro_pending = 0;
2598 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2599 return cop_seqmax++;
2603 newLOGOP(I32 type, I32 flags, OP *first, OP *other)
2609 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2610 return newBINOP(type, flags, scalar(first), scalar(other));
2612 scalarboolean(first);
2613 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2614 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2615 if (type == OP_AND || type == OP_OR) {
2621 first = cUNOPo->op_first;
2623 first->op_next = o->op_next;
2624 cUNOPo->op_first = Nullop;
2628 if (first->op_type == OP_CONST) {
2629 if (dowarn && (first->op_private & OPpCONST_BARE))
2630 warn("Probable precedence problem on %s", op_desc[type]);
2631 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2640 else if (first->op_type == OP_WANTARRAY) {
2646 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2647 OP *k1 = ((UNOP*)first)->op_first;
2648 OP *k2 = k1->op_sibling;
2650 switch (first->op_type)
2653 if (k2 && k2->op_type == OP_READLINE
2654 && (k2->op_flags & OPf_STACKED)
2655 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2656 warnop = k2->op_type;
2660 if (k1->op_type == OP_READDIR
2661 || k1->op_type == OP_GLOB
2662 || k1->op_type == OP_EACH)
2663 warnop = k1->op_type;
2667 line_t oldline = curcop->cop_line;
2668 curcop->cop_line = copline;
2669 warn("Value of %s%s can be \"0\"; test with defined()",
2671 ((warnop == OP_READLINE || warnop == OP_GLOB)
2672 ? " construct" : "() operator"));
2673 curcop->cop_line = oldline;
2680 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2681 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2683 Newz(1101, logop, 1, LOGOP);
2685 logop->op_type = type;
2686 logop->op_ppaddr = ppaddr[type];
2687 logop->op_first = first;
2688 logop->op_flags = flags | OPf_KIDS;
2689 logop->op_other = LINKLIST(other);
2690 logop->op_private = 1 | (flags >> 8);
2692 /* establish postfix order */
2693 logop->op_next = LINKLIST(first);
2694 first->op_next = (OP*)logop;
2695 first->op_sibling = other;
2697 o = newUNOP(OP_NULL, 0, (OP*)logop);
2704 newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop)
2711 return newLOGOP(OP_AND, 0, first, trueop);
2713 return newLOGOP(OP_OR, 0, first, falseop);
2715 scalarboolean(first);
2716 if (first->op_type == OP_CONST) {
2717 if (SvTRUE(((SVOP*)first)->op_sv)) {
2728 else if (first->op_type == OP_WANTARRAY) {
2732 Newz(1101, condop, 1, CONDOP);
2734 condop->op_type = OP_COND_EXPR;
2735 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2736 condop->op_first = first;
2737 condop->op_flags = flags | OPf_KIDS;
2738 condop->op_true = LINKLIST(trueop);
2739 condop->op_false = LINKLIST(falseop);
2740 condop->op_private = 1 | (flags >> 8);
2742 /* establish postfix order */
2743 condop->op_next = LINKLIST(first);
2744 first->op_next = (OP*)condop;
2746 first->op_sibling = trueop;
2747 trueop->op_sibling = falseop;
2748 o = newUNOP(OP_NULL, 0, (OP*)condop);
2750 trueop->op_next = o;
2751 falseop->op_next = o;
2757 newRANGE(I32 flags, OP *left, OP *right)
2765 Newz(1101, condop, 1, CONDOP);
2767 condop->op_type = OP_RANGE;
2768 condop->op_ppaddr = ppaddr[OP_RANGE];
2769 condop->op_first = left;
2770 condop->op_flags = OPf_KIDS;
2771 condop->op_true = LINKLIST(left);
2772 condop->op_false = LINKLIST(right);
2773 condop->op_private = 1 | (flags >> 8);
2775 left->op_sibling = right;
2777 condop->op_next = (OP*)condop;
2778 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2779 flop = newUNOP(OP_FLOP, 0, flip);
2780 o = newUNOP(OP_NULL, 0, flop);
2783 left->op_next = flip;
2784 right->op_next = flop;
2786 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2787 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2788 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2789 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2791 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2792 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2795 if (!flip->op_private || !flop->op_private)
2796 linklist(o); /* blow off optimizer unless constant */
2802 newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
2807 int once = block && block->op_flags & OPf_SPECIAL &&
2808 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2811 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2812 return block; /* do {} while 0 does once */
2813 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2814 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
2815 expr = newUNOP(OP_DEFINED, 0,
2816 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2820 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2821 o = newLOGOP(OP_AND, 0, expr, listop);
2823 ((LISTOP*)listop)->op_last->op_next = LINKLIST(o);
2825 if (once && o != listop)
2826 o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other;
2829 o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */
2831 o->op_flags |= flags;
2833 o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2838 newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont)
2847 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB
2848 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
2849 expr = newUNOP(OP_DEFINED, 0,
2850 newASSIGNOP(0, newDEFSVOP(), 0, expr) );
2854 block = newOP(OP_NULL, 0);
2857 next = LINKLIST(cont);
2859 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2860 if ((line_t)whileline != NOLINE) {
2861 copline = whileline;
2862 cont = append_elem(OP_LINESEQ, cont,
2863 newSTATEOP(0, Nullch, Nullop));
2867 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2868 redo = LINKLIST(listop);
2871 o = newLOGOP(OP_AND, 0, expr, scalar(listop));
2872 if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) {
2873 op_free(expr); /* oops, it's a while (0) */
2875 return Nullop; /* (listop already freed by newLOGOP) */
2877 ((LISTOP*)listop)->op_last->op_next = condop =
2878 (o == listop ? redo : LINKLIST(o));
2886 Newz(1101,loop,1,LOOP);
2887 loop->op_type = OP_ENTERLOOP;
2888 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2889 loop->op_private = 0;
2890 loop->op_next = (OP*)loop;
2893 o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o);
2895 loop->op_redoop = redo;
2896 loop->op_lastop = o;
2899 loop->op_nextop = next;
2901 loop->op_nextop = o;
2903 o->op_flags |= flags;
2904 o->op_private |= (flags >> 8);
2909 #ifndef CAN_PROTOTYPE
2910 newFOROP(flags,label,forline,sv,expr,block,cont)
2919 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2920 #endif /* CAN_PROTOTYPE */
2928 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2929 sv->op_type = OP_RV2GV;
2930 sv->op_ppaddr = ppaddr[OP_RV2GV];
2932 else if (sv->op_type == OP_PADSV) { /* private variable */
2933 padoff = sv->op_targ;
2937 else if (sv->op_type == OP_THREADSV) { /* per-thread variable */
2938 padoff = sv->op_targ;
2939 iterflags |= OPf_SPECIAL;
2944 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2948 padoff = find_threadsv("_");
2949 iterflags |= OPf_SPECIAL;
2951 sv = newGVOP(OP_GV, 0, defgv);
2954 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
2955 expr = scalar(ref(expr, OP_ITER));
2956 iterflags |= OPf_STACKED;
2958 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2959 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2961 assert(!loop->op_next);
2962 Renew(loop, 1, LOOP);
2963 loop->op_targ = padoff;
2964 wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont);
2966 return newSTATEOP(0, label, wop);
2970 newLOOPEX(I32 type, OP *label)
2974 if (type != OP_GOTO || label->op_type == OP_CONST) {
2975 o = newPVOP(type, 0, savepv(
2976 label->op_type == OP_CONST
2977 ? SvPVx(((SVOP*)label)->op_sv, na)
2982 if (label->op_type == OP_ENTERSUB)
2983 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2984 o = newUNOP(type, OPf_STACKED, label);
2986 hints |= HINT_BLOCK_SCOPE;
2996 MUTEX_DESTROY(CvMUTEXP(cv));
2997 Safefree(CvMUTEXP(cv));
3000 #endif /* USE_THREADS */
3002 if (!CvXSUB(cv) && CvROOT(cv)) {
3004 if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr))
3005 croak("Can't undef active subroutine");
3008 croak("Can't undef active subroutine");
3009 #endif /* USE_THREADS */
3016 op_free(CvROOT(cv));
3017 CvROOT(cv) = Nullop;
3020 SvPOK_off((SV*)cv); /* forget prototype */
3022 SvREFCNT_dec(CvGV(cv));
3024 SvREFCNT_dec(CvOUTSIDE(cv));
3025 CvOUTSIDE(cv) = Nullcv;
3026 if (CvPADLIST(cv)) {
3027 /* may be during global destruction */
3028 if (SvREFCNT(CvPADLIST(cv))) {
3029 I32 i = AvFILL(CvPADLIST(cv));
3031 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
3032 SV* sv = svp ? *svp : Nullsv;
3035 if (sv == (SV*)comppad_name)
3036 comppad_name = Nullav;
3037 else if (sv == (SV*)comppad) {
3039 curpad = Null(SV**);
3043 SvREFCNT_dec((SV*)CvPADLIST(cv));
3045 CvPADLIST(cv) = Nullav;
3049 #ifdef DEBUG_CLOSURES
3054 CV *outside = CvOUTSIDE(cv);
3055 AV* padlist = CvPADLIST(cv);
3062 PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n",
3064 (CvANON(cv) ? "ANON"
3065 : (cv == main_cv) ? "MAIN"
3066 : CvUNIQUE(outside) ? "UNIQUE"
3067 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
3070 : CvANON(outside) ? "ANON"
3071 : (outside == main_cv) ? "MAIN"
3072 : CvUNIQUE(outside) ? "UNIQUE"
3073 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
3078 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3079 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3080 pname = AvARRAY(pad_name);
3081 ppad = AvARRAY(pad);
3083 for (ix = 1; ix <= AvFILL(pad_name); ix++) {
3084 if (SvPOK(pname[ix]))
3085 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n",
3087 SvFAKE(pname[ix]) ? "FAKE " : "",
3089 (long)I_32(SvNVX(pname[ix])),
3090 (long)SvIVX(pname[ix]));
3093 #endif /* DEBUG_CLOSURES */
3096 cv_clone2(CV *proto, CV *outside)
3101 AV* protopadlist = CvPADLIST(proto);
3102 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3103 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
3104 SV** pname = AvARRAY(protopad_name);
3105 SV** ppad = AvARRAY(protopad);
3106 I32 fname = AvFILL(protopad_name);
3107 I32 fpad = AvFILL(protopad);
3111 assert(!CvUNIQUE(proto));
3116 SAVESPTR(comppad_name);
3119 cv = compcv = (CV*)NEWSV(1104,0);
3120 sv_upgrade((SV *)cv, SvTYPE(proto));
3126 New(666, CvMUTEXP(cv), 1, perl_mutex);
3127 MUTEX_INIT(CvMUTEXP(cv));
3129 #endif /* USE_THREADS */
3130 CvFILEGV(cv) = CvFILEGV(proto);
3131 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
3132 CvSTASH(cv) = CvSTASH(proto);
3133 CvROOT(cv) = CvROOT(proto);
3134 CvSTART(cv) = CvSTART(proto);
3136 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
3139 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3141 comppad_name = newAV();
3142 for (ix = fname; ix >= 0; ix--)
3143 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3147 comppadlist = newAV();
3148 AvREAL_off(comppadlist);
3149 av_store(comppadlist, 0, (SV*)comppad_name);
3150 av_store(comppadlist, 1, (SV*)comppad);
3151 CvPADLIST(cv) = comppadlist;
3152 av_fill(comppad, AvFILL(protopad));
3153 curpad = AvARRAY(comppad);
3155 av = newAV(); /* will be @_ */
3157 av_store(comppad, 0, (SV*)av);
3158 AvFLAGS(av) = AVf_REIFY;
3160 for (ix = fpad; ix > 0; ix--) {
3161 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3162 if (namesv && namesv != &sv_undef) {
3163 char *name = SvPVX(namesv); /* XXX */
3164 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3165 I32 off = pad_findlex(name, ix, SvIVX(namesv),
3166 CvOUTSIDE(cv), cxstack_ix);
3168 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3170 croak("panic: cv_clone: %s", name);
3172 else { /* our own lexical */
3175 /* anon code -- we'll come back for it */
3176 sv = SvREFCNT_inc(ppad[ix]);
3178 else if (*name == '@')
3180 else if (*name == '%')
3190 SV* sv = NEWSV(0,0);
3196 /* Now that vars are all in place, clone nested closures. */
3198 for (ix = fpad; ix > 0; ix--) {
3199 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
3201 && namesv != &sv_undef
3202 && !(SvFLAGS(namesv) & SVf_FAKE)
3203 && *SvPVX(namesv) == '&'
3204 && CvCLONE(ppad[ix]))
3206 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3207 SvREFCNT_dec(ppad[ix]);
3210 curpad[ix] = (SV*)kid;
3214 #ifdef DEBUG_CLOSURES
3215 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3217 PerlIO_printf(Perl_debug_log, " from:\n");
3219 PerlIO_printf(Perl_debug_log, " to:\n");
3230 return cv_clone2(proto, CvOUTSIDE(proto));
3234 cv_ckproto(CV *cv, GV *gv, char *p)
3236 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
3237 SV* msg = sv_newmortal();
3241 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3242 sv_setpv(msg, "Prototype mismatch:");
3244 sv_catpvf(msg, " sub %_", name);
3246 sv_catpvf(msg, " (%s)", SvPVX(cv));
3247 sv_catpv(msg, " vs ");
3249 sv_catpvf(msg, "(%s)", p);
3251 sv_catpv(msg, "none");
3262 if (!cv || !SvPOK(cv) || SvCUR(cv))
3266 for (o = CvSTART(cv); o; o = o->op_next) {
3267 OPCODE type = o->op_type;
3269 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3271 if (type == OP_LEAVESUB || type == OP_RETURN)
3275 if (type == OP_CONST)
3277 else if (type == OP_PADSV) {
3278 AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3279 sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
3280 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
3292 newSUB(I32 floor, OP *o, OP *proto, OP *block)
3295 char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
3296 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3297 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
3306 if (!name || GvCVGEN(gv))
3308 else if (cv = GvCV(gv)) {
3309 cv_ckproto(cv, gv, ps);
3310 /* already defined (or promised)? */
3311 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3314 /* just a "sub foo;" when &foo is already defined */
3318 /* ahem, death to those who redefine active sort subs */
3319 if (curstack == sortstack && sortcop == CvSTART(cv))
3320 croak("Can't redefine active sort subroutine %s", name);
3321 const_sv = cv_const_sv(cv);
3322 if (const_sv || dowarn) {
3323 line_t oldline = curcop->cop_line;
3324 curcop->cop_line = copline;
3325 warn(const_sv ? "Constant subroutine %s redefined"
3326 : "Subroutine %s redefined", name);
3327 curcop->cop_line = oldline;
3333 if (cv) { /* must reuse cv if autoloaded */
3335 CvFLAGS(cv) = CvFLAGS(compcv);
3336 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
3337 CvOUTSIDE(compcv) = 0;
3338 CvPADLIST(cv) = CvPADLIST(compcv);
3339 CvPADLIST(compcv) = 0;
3340 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3341 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
3342 SvREFCNT_dec(compcv);
3352 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3353 CvFILEGV(cv) = curcop->cop_filegv;
3354 CvSTASH(cv) = curstash;
3357 New(666, CvMUTEXP(cv), 1, perl_mutex);
3358 MUTEX_INIT(CvMUTEXP(cv));
3359 #endif /* USE_THREADS */
3362 sv_setpv((SV*)cv, ps);
3368 char *s = strrchr(name, ':');
3370 if (strEQ(s, "BEGIN")) {
3372 "BEGIN not safe after errors--compilation aborted";
3376 /* force display of errors found but not reported */
3377 sv_catpv(ERRSV, not_safe);
3378 croak("%s", SvPVx(ERRSV, na));
3389 if (AvFILL(comppad_name) < AvFILL(comppad))
3390 av_store(comppad_name, AvFILL(comppad), Nullsv);
3393 SV **namep = AvARRAY(comppad_name);
3394 for (ix = AvFILL(comppad); ix > 0; ix--) {
3397 if (SvIMMORTAL(curpad[ix]))
3400 * The only things that a clonable function needs in its
3401 * pad are references to outer lexicals and anonymous subs.
3402 * The rest are created anew during cloning.
3404 if (!((namesv = namep[ix]) != Nullsv &&
3405 namesv != &sv_undef &&
3407 *SvPVX(namesv) == '&')))
3409 SvREFCNT_dec(curpad[ix]);
3410 curpad[ix] = Nullsv;
3415 AV *av = newAV(); /* Will be @_ */
3417 av_store(comppad, 0, (SV*)av);
3418 AvFLAGS(av) = AVf_REIFY;
3420 for (ix = AvFILL(comppad); ix > 0; ix--) {
3421 if (SvIMMORTAL(curpad[ix]))
3423 if (!SvPADMY(curpad[ix]))
3424 SvPADTMP_on(curpad[ix]);
3428 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
3429 CvSTART(cv) = LINKLIST(CvROOT(cv));
3430 CvROOT(cv)->op_next = 0;
3436 if (PERLDB_SUBLINE && curstash != debstash) {
3437 SV *sv = NEWSV(0,0);
3438 SV *tmpstr = sv_newmortal();
3439 GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
3443 sv_setpvf(sv, "%_:%ld-%ld",
3444 GvSV(curcop->cop_filegv),
3445 (long)subline, (long)curcop->cop_line);
3446 gv_efullname3(tmpstr, gv, Nullch);
3447 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3448 hv = GvHVn(db_postponed);
3449 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3450 && (cv = GvCV(db_postponed))) {
3455 perl_call_sv((SV*)cv, G_DISCARD);
3459 if ((s = strrchr(name,':')))
3463 if (strEQ(s, "BEGIN")) {
3464 I32 oldscope = scopestack_ix;
3466 SAVESPTR(compiling.cop_filegv);
3467 SAVEI16(compiling.cop_line);
3474 DEBUG_x( dump_sub(gv) );
3475 av_push(beginav, (SV *)cv);
3477 call_list(oldscope, beginav);
3479 curcop = &compiling;
3482 else if (strEQ(s, "END") && !error_count) {
3485 av_unshift(endav, 1);
3486 av_store(endav, 0, (SV *)cv);
3489 else if (strEQ(s, "INIT") && !error_count) {
3492 av_push(initav, SvREFCNT_inc(cv));
3503 newXS(char *name, void (*subaddr) (CV *), char *filename)
3506 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3509 if (cv = (name ? GvCV(gv) : Nullcv)) {
3511 /* just a cached method */
3515 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3516 /* already defined (or promised) */
3518 line_t oldline = curcop->cop_line;
3519 curcop->cop_line = copline;
3520 warn("Subroutine %s redefined",name);
3521 curcop->cop_line = oldline;
3528 if (cv) /* must reuse cv if autoloaded */
3531 cv = (CV*)NEWSV(1105,0);
3532 sv_upgrade((SV *)cv, SVt_PVCV);
3539 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3541 New(666, CvMUTEXP(cv), 1, perl_mutex);
3542 MUTEX_INIT(CvMUTEXP(cv));
3544 #endif /* USE_THREADS */
3545 CvFILEGV(cv) = gv_fetchfile(filename);
3546 CvXSUB(cv) = subaddr;
3549 char *s = strrchr(name,':');
3554 if (strEQ(s, "BEGIN")) {
3557 av_push(beginav, (SV *)cv);
3560 else if (strEQ(s, "END")) {
3563 av_unshift(endav, 1);
3564 av_store(endav, 0, (SV *)cv);
3567 else if (strEQ(s, "INIT")) {
3570 av_push(initav, (SV *)cv);
3580 newFORM(I32 floor, OP *o, OP *block)
3589 name = SvPVx(cSVOPo->op_sv, na);
3592 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3594 if (cv = GvFORM(gv)) {
3596 line_t oldline = curcop->cop_line;
3598 curcop->cop_line = copline;
3599 warn("Format %s redefined",name);
3600 curcop->cop_line = oldline;
3606 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
3607 CvFILEGV(cv) = curcop->cop_filegv;
3609 for (ix = AvFILL(comppad); ix > 0; ix--) {
3610 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
3611 SvPADTMP_on(curpad[ix]);
3614 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3615 CvSTART(cv) = LINKLIST(CvROOT(cv));
3616 CvROOT(cv)->op_next = 0;
3626 return newUNOP(OP_REFGEN, 0,
3627 mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN));
3633 return newUNOP(OP_REFGEN, 0,
3634 mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN));
3638 newANONSUB(I32 floor, OP *proto, OP *block)
3640 return newUNOP(OP_REFGEN, 0,
3641 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3647 switch (o->op_type) {
3649 o->op_type = OP_PADAV;
3650 o->op_ppaddr = ppaddr[OP_PADAV];
3651 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3654 o->op_type = OP_RV2AV;
3655 o->op_ppaddr = ppaddr[OP_RV2AV];
3660 warn("oops: oopsAV");
3669 switch (o->op_type) {
3672 o->op_type = OP_PADHV;
3673 o->op_ppaddr = ppaddr[OP_PADHV];
3674 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3678 o->op_type = OP_RV2HV;
3679 o->op_ppaddr = ppaddr[OP_RV2HV];
3684 warn("oops: oopsHV");
3693 if (o->op_type == OP_PADANY) {
3694 o->op_type = OP_PADAV;
3695 o->op_ppaddr = ppaddr[OP_PADAV];
3698 return newUNOP(OP_RV2AV, 0, scalar(o));
3702 newGVREF(I32 type, OP *o)
3704 if (type == OP_MAPSTART)
3705 return newUNOP(OP_NULL, 0, o);
3706 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3712 if (o->op_type == OP_PADANY) {
3713 o->op_type = OP_PADHV;
3714 o->op_ppaddr = ppaddr[OP_PADHV];
3717 return newUNOP(OP_RV2HV, 0, scalar(o));
3723 croak("NOT IMPL LINE %d",__LINE__);
3729 newCVREF(I32 flags, OP *o)
3731 return newUNOP(OP_RV2CV, flags, scalar(o));
3737 if (o->op_type == OP_PADANY) {
3738 o->op_type = OP_PADSV;
3739 o->op_ppaddr = ppaddr[OP_PADSV];
3742 else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) {
3743 o->op_flags |= OPpDONE_SVREF;
3746 return newUNOP(OP_RV2SV, 0, scalar(o));
3749 /* Check routines. */
3757 name = NEWSV(1106,0);
3758 sv_upgrade(name, SVt_PVNV);
3759 sv_setpvn(name, "&", 1);
3762 ix = pad_alloc(o->op_type, SVs_PADMY);
3763 av_store(comppad_name, ix, name);
3764 av_store(comppad, ix, cSVOPo->op_sv);
3765 SvPADMY_on(cSVOPo->op_sv);
3766 cSVOPo->op_sv = Nullsv;
3767 cSVOPo->op_targ = ix;
3774 o->op_private = hints;
3781 if (cUNOPo->op_first->op_type == OP_CONCAT)
3782 o->op_flags |= OPf_STACKED;
3789 if (o->op_flags & OPf_KIDS) {
3792 OPCODE type = o->op_type;
3793 o = modkids(ck_fun(o), type);
3794 kid = cUNOPo->op_first;
3795 newop = kUNOP->op_first->op_sibling;
3797 (newop->op_sibling ||
3798 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3799 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3800 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3804 op_free(kUNOP->op_first);
3805 kUNOP->op_first = newop;
3807 o->op_ppaddr = ppaddr[++o->op_type];
3816 if (o->op_flags & OPf_KIDS) {
3817 OP *kid = cUNOPo->op_first;
3818 if (kid->op_type == OP_HSLICE)
3819 o->op_private |= OPpSLICE;
3820 else if (kid->op_type != OP_HELEM)
3821 croak("%s argument is not a HASH element or slice",
3822 op_desc[o->op_type]);
3831 I32 type = o->op_type;
3833 if (o->op_flags & OPf_KIDS) {
3834 if (cLISTOPo->op_first->op_type == OP_STUB) {
3836 o = newUNOP(type, OPf_SPECIAL,
3837 newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV)));
3847 hints |= HINT_BLOCK_SCOPE;
3848 if (o->op_flags & OPf_KIDS) {
3849 SVOP *kid = (SVOP*)cUNOPo->op_first;
3852 o->op_flags &= ~OPf_KIDS;
3855 else if (kid->op_type == OP_LINESEQ) {
3858 kid->op_next = o->op_next;
3859 cUNOPo->op_first = 0;
3862 Newz(1101, enter, 1, LOGOP);
3863 enter->op_type = OP_ENTERTRY;
3864 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3865 enter->op_private = 0;
3867 /* establish postfix order */
3868 enter->op_next = (OP*)enter;
3870 o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3871 o->op_type = OP_LEAVETRY;
3872 o->op_ppaddr = ppaddr[OP_LEAVETRY];
3873 enter->op_other = o;
3881 o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP());
3883 o->op_targ = (PADOFFSET)hints;
3891 if (o->op_flags & OPf_STACKED) {
3893 kid = cUNOPo->op_first->op_sibling;
3894 if (kid->op_type == OP_RV2GV)
3906 if (o->op_flags & OPf_KIDS) {
3907 OP *kid = cUNOPo->op_first;
3908 if (kid->op_type != OP_HELEM)
3909 croak("%s argument is not a HASH element", op_desc[o->op_type]);
3916 ck_gvconst(register OP *o)
3918 o = fold_constants(o);
3919 if (o->op_type == OP_CONST)
3925 ck_rvconst(register OP *o)
3928 SVOP *kid = (SVOP*)cUNOPo->op_first;
3930 o->op_private |= (hints & HINT_STRICT_REFS);
3931 if (kid->op_type == OP_CONST) {
3936 name = SvPV(kid->op_sv, na);
3937 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3938 char *badthing = Nullch;
3939 switch (o->op_type) {
3941 badthing = "a SCALAR";
3944 badthing = "an ARRAY";
3947 badthing = "a HASH";
3952 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3955 kid->op_type = OP_GV;
3956 iscv = (o->op_type == OP_RV2CV) * 2;
3957 for (gv = 0; !gv; iscv++) {
3959 * This is a little tricky. We only want to add the symbol if we
3960 * didn't add it in the lexer. Otherwise we get duplicate strict
3961 * warnings. But if we didn't add it in the lexer, we must at
3962 * least pretend like we wanted to add it even if it existed before,
3963 * or we get possible typo warnings. OPpCONST_ENTERED says
3964 * whether the lexer already added THIS instance of this symbol.
3966 gv = gv_fetchpv(name,
3967 iscv | !(kid->op_private & OPpCONST_ENTERED),
3970 : o->op_type == OP_RV2SV
3972 : o->op_type == OP_RV2AV
3974 : o->op_type == OP_RV2HV
3978 SvREFCNT_dec(kid->op_sv);
3979 kid->op_sv = SvREFCNT_inc(gv);
3988 I32 type = o->op_type;
3990 if (o->op_flags & OPf_REF)
3993 if (o->op_flags & OPf_KIDS) {
3994 SVOP *kid = (SVOP*)cUNOPo->op_first;
3996 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3997 OP *newop = newGVOP(type, OPf_REF,
3998 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
4005 if (type == OP_FTTTY)
4006 return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE,
4009 return newUNOP(type, 0, newDEFSVOP());
4022 int type = o->op_type;
4023 register I32 oa = opargs[type] >> OASHIFT;
4025 if (o->op_flags & OPf_STACKED) {
4026 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
4029 return no_fh_allowed(o);
4032 if (o->op_flags & OPf_KIDS) {
4033 tokid = &cLISTOPo->op_first;
4034 kid = cLISTOPo->op_first;
4035 if (kid->op_type == OP_PUSHMARK ||
4036 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
4038 tokid = &kid->op_sibling;
4039 kid = kid->op_sibling;
4041 if (!kid && opargs[type] & OA_DEFGV)
4042 *tokid = kid = newDEFSVOP();
4046 sibl = kid->op_sibling;
4060 if (kid->op_type == OP_CONST &&
4061 (kid->op_private & OPpCONST_BARE)) {
4062 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4063 OP *newop = newAVREF(newGVOP(OP_GV, 0,
4064 gv_fetchpv(name, TRUE, SVt_PVAV) ));
4066 warn("Array @%s missing the @ in argument %ld of %s()",
4067 name, (long)numargs, op_desc[type]);
4070 kid->op_sibling = sibl;
4073 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
4074 bad_type(numargs, "array", op_desc[o->op_type], kid);
4078 if (kid->op_type == OP_CONST &&
4079 (kid->op_private & OPpCONST_BARE)) {
4080 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
4081 OP *newop = newHVREF(newGVOP(OP_GV, 0,
4082 gv_fetchpv(name, TRUE, SVt_PVHV) ));
4084 warn("Hash %%%s missing the %% in argument %ld of %s()",
4085 name, (long)numargs, op_desc[type]);
4088 kid->op_sibling = sibl;
4091 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
4092 bad_type(numargs, "hash", op_desc[o->op_type], kid);
4097 OP *newop = newUNOP(OP_NULL, 0, kid);
4098 kid->op_sibling = 0;
4100 newop->op_next = newop;
4102 kid->op_sibling = sibl;
4107 if (kid->op_type != OP_GV) {
4108 if (kid->op_type == OP_CONST &&
4109 (kid->op_private & OPpCONST_BARE)) {
4110 OP *newop = newGVOP(OP_GV, 0,
4111 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
4117 kid->op_sibling = 0;
4118 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
4120 kid->op_sibling = sibl;
4126 mod(scalar(kid), type);
4130 tokid = &kid->op_sibling;
4131 kid = kid->op_sibling;
4133 o->op_private |= numargs;
4135 return too_many_arguments(o,op_desc[o->op_type]);
4138 else if (opargs[type] & OA_DEFGV) {
4140 return newUNOP(type, 0, newDEFSVOP());
4144 while (oa & OA_OPTIONAL)
4146 if (oa && oa != OA_LIST)
4147 return too_few_arguments(o,op_desc[o->op_type]);
4157 if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling)
4158 append_elem(OP_GLOB, o, newDEFSVOP());
4160 if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv)))
4161 gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV);
4163 if (gv && GvIMPORTED_CV(gv)) {
4164 static int glob_index;
4166 append_elem(OP_GLOB, o,
4167 newSVOP(OP_CONST, 0, newSViv(glob_index++)));
4168 o->op_type = OP_LIST;
4169 o->op_ppaddr = ppaddr[OP_LIST];
4170 cLISTOPo->op_first->op_type = OP_PUSHMARK;
4171 cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK];
4172 o = newUNOP(OP_ENTERSUB, OPf_STACKED,
4173 append_elem(OP_LIST, o,
4174 scalar(newUNOP(OP_RV2CV, 0,
4175 newGVOP(OP_GV, 0, gv)))));
4176 o = newUNOP(OP_NULL, 0, ck_subr(o));
4177 o->op_targ = OP_GLOB; /* hint at what it used to be */
4180 gv = newGVgen("main");
4182 append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv));
4192 OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
4194 o->op_ppaddr = ppaddr[OP_GREPSTART];
4195 Newz(1101, gwop, 1, LOGOP);
4197 if (o->op_flags & OPf_STACKED) {
4200 kid = cLISTOPo->op_first->op_sibling;
4201 for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) {
4204 kid->op_next = (OP*)gwop;
4205 o->op_flags &= ~OPf_STACKED;
4207 kid = cLISTOPo->op_first->op_sibling;
4208 if (type == OP_MAPWHILE)
4215 kid = cLISTOPo->op_first->op_sibling;
4216 if (kid->op_type != OP_NULL)
4217 croak("panic: ck_grep");
4218 kid = kUNOP->op_first;
4220 gwop->op_type = type;
4221 gwop->op_ppaddr = ppaddr[type];
4222 gwop->op_first = listkids(o);
4223 gwop->op_flags |= OPf_KIDS;
4224 gwop->op_private = 1;
4225 gwop->op_other = LINKLIST(kid);
4226 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
4227 kid->op_next = (OP*)gwop;
4229 kid = cLISTOPo->op_first->op_sibling;
4230 if (!kid || !kid->op_sibling)
4231 return too_few_arguments(o,op_desc[o->op_type]);
4232 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
4233 mod(kid, OP_GREPSTART);
4241 if (o->op_flags & OPf_KIDS) {
4242 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4243 if (kid && kid->op_type == OP_CONST)
4244 fbm_compile(((SVOP*)kid)->op_sv);
4250 ck_lengthconst(OP *o)
4252 /* XXX length optimization goes here */
4259 OPCODE type = o->op_type;
4260 return modkids(ck_fun(o), type);
4266 OPCODE type = o->op_type;
4267 return refkids(ck_fun(o), type);
4275 kid = cLISTOPo->op_first;
4278 kid = cLISTOPo->op_first;
4280 if (kid->op_type == OP_PUSHMARK)
4281 kid = kid->op_sibling;
4282 if (kid && o->op_flags & OPf_STACKED)
4283 kid = kid->op_sibling;
4284 else if (kid && !kid->op_sibling) { /* print HANDLE; */
4285 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
4286 o->op_flags |= OPf_STACKED; /* make it a filehandle */
4287 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
4288 cLISTOPo->op_first->op_sibling = kid;
4289 cLISTOPo->op_last = kid;
4290 kid = kid->op_sibling;
4295 append_elem(o->op_type, o, newDEFSVOP());
4301 if (hints & HINT_LOCALE)
4302 o->op_private |= OPpLOCALE;
4309 ck_fun_locale(OP *o)
4315 if (hints & HINT_LOCALE)
4316 o->op_private |= OPpLOCALE;
4327 if (hints & HINT_LOCALE)
4328 o->op_private |= OPpLOCALE;
4337 o->op_private |= OPpRUNTIME;
4350 if (cBINOPo->op_first->op_flags & OPf_PARENS) {
4351 o->op_private |= OPpREPEAT_DOLIST;
4352 cBINOPo->op_first = force_list(cBINOPo->op_first);
4362 if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
4363 SVOP *kid = (SVOP*)cUNOPo->op_first;
4365 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
4367 for (s = SvPVX(kid->op_sv); *s; s++) {
4368 if (*s == ':' && s[1] == ':') {
4370 Move(s+2, s+1, strlen(s+2)+1, char);
4371 --SvCUR(kid->op_sv);
4374 sv_catpvn(kid->op_sv, ".pm", 3);
4383 croak("NOT IMPL LINE %d",__LINE__);
4392 if (o->op_flags & OPf_KIDS) {
4393 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4394 if (kid && kid->op_sibling) {
4395 o->op_type = OP_SSELECT;
4396 o->op_ppaddr = ppaddr[OP_SSELECT];
4398 return fold_constants(o);
4402 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4403 if (kid && kid->op_type == OP_RV2GV)
4404 kid->op_private &= ~HINT_STRICT_REFS;
4411 I32 type = o->op_type;
4413 if (!(o->op_flags & OPf_KIDS)) {
4418 if (!CvUNIQUE(compcv)) {
4419 argop = newOP(OP_PADAV, OPf_REF);
4420 argop->op_targ = 0; /* curpad[0] is @_ */
4423 argop = newUNOP(OP_RV2AV, 0,
4424 scalar(newGVOP(OP_GV, 0,
4425 gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4428 argop = newUNOP(OP_RV2AV, 0,
4429 scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ?
4430 defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV))));
4431 #endif /* USE_THREADS */
4432 return newUNOP(type, 0, scalar(argop));
4434 return scalar(modkids(ck_fun(o), type));
4442 if (hints & HINT_LOCALE)
4443 o->op_private |= OPpLOCALE;
4446 if (o->op_flags & OPf_STACKED) {
4447 OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4449 kid = kUNOP->op_first; /* get past rv2gv */
4451 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
4453 if (kid->op_type == OP_SCOPE) {
4457 else if (kid->op_type == OP_LEAVE) {
4458 if (o->op_type == OP_SORT) {
4459 null(kid); /* wipe out leave */
4462 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
4463 if (k->op_next == kid)
4468 kid->op_next = 0; /* just disconnect the leave */
4469 k = kLISTOP->op_first;
4473 kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */
4474 null(kid); /* wipe out rv2gv */
4475 if (o->op_type == OP_SORT)
4479 o->op_flags |= OPf_SPECIAL;
4491 if (o->op_flags & OPf_STACKED)
4492 return no_fh_allowed(o);
4494 kid = cLISTOPo->op_first;
4495 if (kid->op_type != OP_NULL)
4496 croak("panic: ck_split");
4497 kid = kid->op_sibling;
4498 op_free(cLISTOPo->op_first);
4499 cLISTOPo->op_first = kid;
4501 cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
4502 cLISTOPo->op_last = kid; /* There was only one element previously */
4505 if (kid->op_type != OP_MATCH) {
4506 OP *sibl = kid->op_sibling;
4507 kid->op_sibling = 0;
4508 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
4509 if (cLISTOPo->op_first == cLISTOPo->op_last)
4510 cLISTOPo->op_last = kid;
4511 cLISTOPo->op_first = kid;
4512 kid->op_sibling = sibl;
4515 kid->op_type = OP_PUSHRE;
4516 kid->op_ppaddr = ppaddr[OP_PUSHRE];
4519 if (!kid->op_sibling)
4520 append_elem(OP_SPLIT, o, newDEFSVOP());
4522 kid = kid->op_sibling;
4525 if (!kid->op_sibling)
4526 append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0)));
4528 kid = kid->op_sibling;
4531 if (kid->op_sibling)
4532 return too_many_arguments(o,op_desc[o->op_type]);
4541 OP *prev = ((cUNOPo->op_first->op_sibling)
4542 ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first;
4543 OP *o2 = prev->op_sibling;
4551 for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ;
4552 if (cvop->op_type == OP_RV2CV) {
4554 o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
4555 null(cvop); /* disable rv2cv */
4556 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
4557 if (tmpop->op_type == OP_GV) {
4558 cv = GvCVu(tmpop->op_sv);
4559 if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) {
4560 namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv);
4561 proto = SvPV((SV*)cv, na);
4565 o->op_private |= (hints & HINT_STRICT_REFS);
4566 if (PERLDB_SUB && curstash != debstash)
4567 o->op_private |= OPpENTERSUB_DB;
4568 while (o2 != cvop) {
4572 return too_many_arguments(o, gv_ename(namegv));
4590 if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF)
4591 bad_type(arg, "block", gv_ename(namegv), o2);
4596 if (o2->op_type == OP_RV2GV)
4600 o2 = newUNOP(OP_RV2GV, 0, kid);
4601 o2->op_sibling = kid->op_sibling;
4602 kid->op_sibling = 0;
4603 prev->op_sibling = o;
4611 if (o2->op_type != OP_RV2GV)
4612 bad_type(arg, "symbol", gv_ename(namegv), o2);
4615 if (o2->op_type != OP_RV2CV)
4616 bad_type(arg, "sub", gv_ename(namegv), o2);
4619 if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV)
4620 bad_type(arg, "scalar", gv_ename(namegv), o2);
4623 if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV)
4624 bad_type(arg, "array", gv_ename(namegv), o2);
4627 if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV)
4628 bad_type(arg, "hash", gv_ename(namegv), o2);
4632 o2 = newUNOP(OP_REFGEN, 0, kid);
4633 o2->op_sibling = kid->op_sibling;
4634 kid->op_sibling = 0;
4635 prev->op_sibling = o2;
4646 croak("Malformed prototype for %s: %s",
4647 gv_ename(namegv), SvPV((SV*)cv, na));
4652 mod(o2, OP_ENTERSUB);
4654 o2 = o2->op_sibling;
4656 if (proto && !optional &&
4657 (*proto && *proto != '@' && *proto != '%' && *proto != ';'))
4658 return too_few_arguments(o, gv_ename(namegv));
4665 SvREADONLY_on(cSVOPo->op_sv);
4672 if (o->op_flags & OPf_KIDS) {
4673 SVOP *kid = (SVOP*)cUNOPo->op_first;
4675 if (kid->op_type == OP_NULL)
4676 kid = (SVOP*)kid->op_sibling;
4678 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4679 o->op_flags |= OPf_SPECIAL;
4684 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4687 peep(register OP *o)
4690 register OP* oldop = 0;
4691 if (!o || o->op_seq)
4696 for (; o; o = o->op_next) {
4702 switch (o->op_type) {
4705 curcop = ((COP*)o); /* for warnings */
4706 o->op_seq = op_seqmax++;
4717 if (o->op_next && o->op_next->op_type == OP_STRINGIFY)
4719 o->op_seq = op_seqmax++;
4722 if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) {
4723 o->op_seq = op_seqmax++;
4724 break; /* Scalar stub must produce undef. List stub is noop */
4728 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4729 curcop = ((COP*)op);
4735 if (oldop && o->op_next) {
4736 oldop->op_next = o->op_next;
4739 o->op_seq = op_seqmax++;
4743 if (o->op_next->op_type == OP_RV2SV) {
4744 if (!(o->op_next->op_private & OPpDEREF)) {
4746 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4747 o->op_next = o->op_next->op_next;
4748 o->op_type = OP_GVSV;
4749 o->op_ppaddr = ppaddr[OP_GVSV];
4752 else if (o->op_next->op_type == OP_RV2AV) {
4753 OP* pop = o->op_next->op_next;
4755 if (pop->op_type == OP_CONST &&
4756 (op = pop->op_next) &&
4757 pop->op_next->op_type == OP_AELEM &&
4758 !(pop->op_next->op_private &
4759 (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) &&
4760 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4764 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4768 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4769 o->op_next = pop->op_next->op_next;
4770 o->op_type = OP_AELEMFAST;
4771 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4772 o->op_private = (U8)i;
4773 GvAVn(((GVOP*)o)->op_gv);
4776 o->op_seq = op_seqmax++;
4780 if (o->op_next->op_type == OP_RV2AV
4781 && (o->op_next->op_flags && OPf_REF))
4784 o->op_next = o->op_next->op_next;
4789 if (o->op_next->op_type == OP_RV2HV
4790 && (o->op_next->op_flags && OPf_REF))
4793 o->op_next = o->op_next->op_next;
4801 o->op_seq = op_seqmax++;
4802 peep(cLOGOP->op_other);
4806 o->op_seq = op_seqmax++;
4807 peep(cCONDOP->op_true);
4808 peep(cCONDOP->op_false);
4812 o->op_seq = op_seqmax++;
4813 peep(cLOOP->op_redoop);
4814 peep(cLOOP->op_nextop);
4815 peep(cLOOP->op_lastop);
4820 o->op_seq = op_seqmax++;
4821 peep(cPMOP->op_pmreplstart);
4825 o->op_seq = op_seqmax++;
4826 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4827 if (o->op_next->op_sibling &&
4828 o->op_next->op_sibling->op_type != OP_DIE) {
4829 line_t oldline = curcop->cop_line;
4831 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4832 warn("Statement unlikely to be reached");
4833 warn("(Maybe you meant system() when you said exec()?)\n");
4834 curcop->cop_line = oldline;
4848 if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)
4849 || ((BINOP*)o)->op_last->op_type != OP_CONST)
4851 rop = (UNOP*)((BINOP*)o)->op_first;
4852 if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV)
4854 lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE);
4855 if (!SvOBJECT(lexname))
4857 fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE);
4858 if (!fields || !GvHV(*fields))
4860 svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv;
4861 key = SvPV(*svp, keylen);
4862 indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE);
4864 croak("No such field \"%s\" in variable %s of type %s",
4865 key, SvPV(lexname, na), HvNAME(SvSTASH(lexname)));
4867 ind = SvIV(*indsvp);
4869 croak("Bad index while coercing array into hash");
4870 rop->op_type = OP_RV2AV;
4871 rop->op_ppaddr = ppaddr[OP_RV2AV];
4872 o->op_type = OP_AELEM;
4873 o->op_ppaddr = ppaddr[OP_AELEM];
4875 *svp = newSViv(ind);
4880 o->op_seq = op_seqmax++;