3 * Copyright (c) 1991-1994, 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
21 #define USE_OP_MASK /* Turned on by default in 5.002beta1h */
25 * In the following definition, the ", (OP *) op" is just to make the compiler
26 * think the expression is of the right type: croak actually does a Siglongjmp.
28 #define CHECKOP(type,op) \
29 ((op_mask && op_mask[type]) \
30 ? (croak("%s trapped by operation mask", op_desc[type]), (OP*)op) \
31 : (*check[type])((OP*)op))
33 #define CHECKOP(type,op) (*check[type])(op)
34 #endif /* USE_OP_MASK */
36 static I32 list_assignment _((OP *op));
37 static OP *bad_type _((I32 n, char *t, char *name, OP *kid));
38 static OP *modkids _((OP *op, I32 type));
39 static OP *no_fh_allowed _((OP *op));
40 static OP *scalarboolean _((OP *op));
41 static OP *too_few_arguments _((OP *op, char* name));
42 static OP *too_many_arguments _((OP *op, char* name));
43 static void null _((OP* op));
44 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, I32 seq,
45 CV* startcv, I32 cx_ix));
51 SV* tmpsv = sv_newmortal();
52 gv_efullname3(tmpsv, CvGV(cv), Nullch);
53 return SvPV(tmpsv,na);
60 sprintf(tokenbuf,"Missing comma after first argument to %s function",
61 op_desc[op->op_type]);
67 too_few_arguments(op, name)
71 sprintf(tokenbuf,"Not enough arguments for %s", name);
77 too_many_arguments(op, name)
81 sprintf(tokenbuf,"Too many arguments for %s", name);
87 bad_type(n, t, name, kid)
93 sprintf(tokenbuf, "Type of arg %d to %s must be %s (not %s)",
94 (int) n, name, t, op_desc[kid->op_type]);
103 int type = op->op_type;
104 if (type != OP_AELEM && type != OP_HELEM) {
105 sprintf(tokenbuf, "Can't use subscript on %s", op_desc[type]);
107 if (type == OP_RV2HV || type == OP_ENTERSUB)
108 warn("(Did you mean $ or @ instead of %c?)\n",
109 type == OP_RV2HV ? '%' : '&');
113 /* "register" allocation */
122 if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
123 if (!isprint(name[1]))
124 sprintf(name+1, "^%c", name[1] ^ 64); /* XXX is tokenbuf, really */
125 croak("Can't use global %s in \"my\"",name);
127 if (AvFILL(comppad_name) >= 0) {
128 SV **svp = AvARRAY(comppad_name);
129 for (off = AvFILL(comppad_name); off > comppad_name_floor; off--) {
132 && SvIVX(sv) == 999999999 /* var is in open scope */
133 && strEQ(name, SvPVX(sv)))
135 warn("\"my\" variable %s masks earlier declaration in same scope", name);
140 off = pad_alloc(OP_PADSV, SVs_PADMY);
142 sv_upgrade(sv, SVt_PVNV);
144 av_store(comppad_name, off, sv);
145 SvNVX(sv) = (double)999999999;
146 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
147 if (!min_intro_pending)
148 min_intro_pending = off;
149 max_intro_pending = off;
151 av_store(comppad, off, (SV*)newAV());
152 else if (*name == '%')
153 av_store(comppad, off, (SV*)newHV());
154 SvPADMY_on(curpad[off]);
159 #ifndef CAN_PROTOTYPE
160 pad_findlex(name, newoff, seq, startcv, cx_ix)
167 pad_findlex(char *name, PADOFFSET newoff, I32 seq, CV* startcv, I32 cx_ix)
174 register CONTEXT *cx;
177 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
178 AV* curlist = CvPADLIST(cv);
179 SV** svp = av_fetch(curlist, 0, FALSE);
181 if (!svp || *svp == &sv_undef)
184 svp = AvARRAY(curname);
185 for (off = AvFILL(curname); off > 0; off--) {
186 if ((sv = svp[off]) &&
189 seq > (I32)SvNVX(sv) &&
190 strEQ(SvPVX(sv), name))
192 I32 depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1;
193 AV *oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
194 SV *oldsv = *av_fetch(oldpad, off, TRUE);
195 if (!newoff) { /* Not a mere clone operation. */
196 SV *sv = NEWSV(1103,0);
197 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
198 sv_upgrade(sv, SVt_PVNV);
200 av_store(comppad_name, newoff, sv);
201 SvNVX(sv) = (double)curcop->cop_seq;
202 SvIVX(sv) = 999999999; /* A ref, intro immediately */
203 SvFLAGS(sv) |= SVf_FAKE;
205 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
212 /* Nothing in current lexical context--try eval's context, if any.
213 * This is necessary to let the perldb get at lexically scoped variables.
214 * XXX This will also probably interact badly with eval tree caching.
218 for (i = cx_ix; i >= 0; i--) {
220 switch (cx->cx_type) {
222 if (i == 0 && saweval) {
223 seq = cxstack[saweval].blk_oldcop->cop_seq;
224 return pad_findlex(name, newoff, seq, main_cv, 0);
228 if (cx->blk_eval.old_op_type != OP_ENTEREVAL &&
229 cx->blk_eval.old_op_type != OP_ENTERTRY)
230 return 0; /* require must have its own scope */
237 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
238 saweval = i; /* so we know where we were called from */
241 seq = cxstack[saweval].blk_oldcop->cop_seq;
242 return pad_findlex(name, newoff, seq, cv, i-1);
255 SV **svp = AvARRAY(comppad_name);
256 I32 seq = cop_seqmax;
258 /* The one we're looking for is probably just before comppad_name_fill. */
259 for (off = AvFILL(comppad_name); off > 0; off--) {
260 if ((sv = svp[off]) &&
263 seq > (I32)SvNVX(sv) &&
264 strEQ(SvPVX(sv), name))
266 return (PADOFFSET)off;
270 /* See if it's in a nested scope */
271 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
283 SV **svp = AvARRAY(comppad_name);
285 if (min_intro_pending && fill < min_intro_pending) {
286 for (off = max_intro_pending; off >= min_intro_pending; off--) {
287 if ((sv = svp[off]) && sv != &sv_undef)
288 warn("%s never introduced", SvPVX(sv));
291 /* "Deintroduce" my variables that are leaving with this scope. */
292 for (off = AvFILL(comppad_name); off > fill; off--) {
293 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
294 SvIVX(sv) = cop_seqmax;
299 pad_alloc(optype,tmptype)
306 if (AvARRAY(comppad) != curpad)
307 croak("panic: pad_alloc");
308 if (pad_reset_pending)
310 if (tmptype & SVs_PADMY) {
312 sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
313 } while (SvPADBUSY(sv)); /* need a fresh one */
314 retval = AvFILL(comppad);
318 sv = *av_fetch(comppad, ++padix, TRUE);
319 } while (SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY));
322 SvFLAGS(sv) |= tmptype;
323 curpad = AvARRAY(comppad);
324 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad alloc %ld for %s\n", (long) retval, op_name[optype]));
325 return (PADOFFSET)retval;
329 #ifndef CAN_PROTOTYPE
334 #endif /* CAN_PROTOTYPE */
337 croak("panic: pad_sv po");
338 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad sv %d\n", po));
339 return curpad[po]; /* eventually we'll turn this into a macro */
343 #ifndef CAN_PROTOTYPE
347 pad_free(PADOFFSET po)
348 #endif /* CAN_PROTOTYPE */
352 if (AvARRAY(comppad) != curpad)
353 croak("panic: pad_free curpad");
355 croak("panic: pad_free po");
356 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad free %d\n", po));
357 if (curpad[po] && curpad[po] != &sv_undef)
358 SvPADTMP_off(curpad[po]);
364 #ifndef CAN_PROTOTYPE
368 pad_swipe(PADOFFSET po)
369 #endif /* CAN_PROTOTYPE */
371 if (AvARRAY(comppad) != curpad)
372 croak("panic: pad_swipe curpad");
374 croak("panic: pad_swipe po");
375 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad swipe %d\n", po));
376 SvPADTMP_off(curpad[po]);
377 curpad[po] = NEWSV(1107,0);
378 SvPADTMP_on(curpad[po]);
388 if (AvARRAY(comppad) != curpad)
389 croak("panic: pad_reset curpad");
390 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad reset\n"));
391 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
392 for (po = AvMAX(comppad); po > padix_floor; po--) {
393 if (curpad[po] && curpad[po] != &sv_undef)
394 SvPADTMP_off(curpad[po]);
398 pad_reset_pending = FALSE;
407 register OP *kid, *nextkid;
409 if (!op || op->op_seq == (U16)-1)
412 if (op->op_flags & OPf_KIDS) {
413 for (kid = cUNOP->op_first; kid; kid = nextkid) {
414 nextkid = kid->op_sibling; /* Get before next freeing kid */
419 switch (op->op_type) {
421 op->op_targ = 0; /* Was holding old type, if any. */
424 op->op_targ = 0; /* Was holding hints. */
428 SvREFCNT_dec(cGVOP->op_gv);
432 SvREFCNT_dec(cCOP->cop_filegv);
434 /* case OP_ANONCODE: XXX breaks eval of anon subs in closures (cf. Opcode) */
436 SvREFCNT_dec(cSVOP->op_sv);
442 if (op->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
446 Safefree(cPVOP->op_pv);
449 op_free(cPMOP->op_pmreplroot);
453 pregfree(cPMOP->op_pmregexp);
454 SvREFCNT_dec(cPMOP->op_pmshort);
461 pad_free(op->op_targ);
470 if (op->op_type != OP_NULL && op->op_targ > 0)
471 pad_free(op->op_targ);
472 op->op_targ = op->op_type;
473 op->op_type = OP_NULL;
474 op->op_ppaddr = ppaddr[OP_NULL];
477 /* Contextualizers */
479 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
490 /* establish postfix order */
491 if (cUNOP->op_first) {
492 op->op_next = LINKLIST(cUNOP->op_first);
493 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
495 kid->op_next = LINKLIST(kid->op_sibling);
511 if (op && op->op_flags & OPf_KIDS) {
512 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
523 op->op_type == OP_SASSIGN && cBINOP->op_first->op_type == OP_CONST) {
524 line_t oldline = curcop->cop_line;
526 if (copline != NOLINE)
527 curcop->cop_line = copline;
528 warn("Found = in conditional, should be ==");
529 curcop->cop_line = oldline;
540 /* assumes no premature commitment */
541 if (!op || (op->op_flags & OPf_KNOW) || error_count)
544 op->op_flags &= ~OPf_LIST;
545 op->op_flags |= OPf_KNOW;
547 switch (op->op_type) {
549 if (op->op_private & OPpREPEAT_DOLIST)
550 null(((LISTOP*)cBINOP->op_first)->op_first);
551 scalar(cBINOP->op_first);
556 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
560 if ((kid = ((LISTOP*)op)->op_first) && kid->op_type == OP_PUSHRE) {
561 if (!kPMOP->op_pmreplroot)
562 deprecate("implicit split to @_");
569 if (op->op_flags & OPf_KIDS) {
570 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
576 scalar(cLISTOP->op_first);
581 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
601 if (!op || error_count)
603 if (op->op_flags & OPf_LIST)
606 op->op_flags |= OPf_KNOW;
608 switch (op->op_type) {
610 if (!(opargs[op->op_type] & OA_FOLDCONST))
612 if (op->op_flags & OPf_STACKED)
685 if (!(op->op_private & OPpLVAL_INTRO))
686 useless = op_desc[op->op_type];
693 if (!(op->op_private & OPpLVAL_INTRO) &&
694 (!op->op_sibling || op->op_sibling->op_type != OP_READLINE))
695 useless = "a variable";
700 curcop = ((COP*)op); /* for warning below */
706 useless = "a constant";
707 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
709 else if (SvPOK(sv)) {
710 if (strnEQ(SvPVX(sv), "di", 2) ||
711 strnEQ(SvPVX(sv), "ds", 2) ||
712 strnEQ(SvPVX(sv), "ig", 2))
716 null(op); /* don't execute a constant */
717 SvREFCNT_dec(sv); /* don't even remember it */
721 op->op_type = OP_PREINC; /* pre-increment is faster */
722 op->op_ppaddr = ppaddr[OP_PREINC];
726 op->op_type = OP_PREDEC; /* pre-decrement is faster */
727 op->op_ppaddr = ppaddr[OP_PREDEC];
731 scalarvoid(cBINOP->op_first);
732 useless = op_desc[op->op_type];
738 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
742 if (op->op_targ == OP_NEXTSTATE || op->op_targ == OP_DBSTATE)
743 curcop = ((COP*)op); /* for warning below */
744 if (op->op_flags & OPf_STACKED)
749 if (!(op->op_flags & OPf_KIDS))
755 op->op_private |= OPpLEAVE_VOID;
758 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
762 if ((kid = ((LISTOP*)op)->op_first) && kid->op_type == OP_PUSHRE) {
763 if (!kPMOP->op_pmreplroot)
764 deprecate("implicit split to @_");
768 op->op_private |= OPpLEAVE_VOID;
771 if (useless && dowarn)
772 warn("Useless use of %s in void context", useless);
781 if (op && op->op_flags & OPf_KIDS) {
782 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
794 /* assumes no premature commitment */
795 if (!op || (op->op_flags & OPf_KNOW) || error_count)
798 op->op_flags |= (OPf_KNOW | OPf_LIST);
800 switch (op->op_type) {
803 list(cBINOP->op_first);
808 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
815 if (!(op->op_flags & OPf_KIDS))
817 if (!op->op_next && cUNOP->op_first->op_type == OP_FLOP) {
818 list(cBINOP->op_first);
819 return gen_constant_list(op);
826 list(cLISTOP->op_first);
830 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
849 if (op->op_type == OP_LINESEQ ||
850 op->op_type == OP_SCOPE ||
851 op->op_type == OP_LEAVE ||
852 op->op_type == OP_LEAVETRY)
854 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
855 if (kid->op_sibling) {
861 op->op_flags &= ~OPf_PARENS;
862 if (hints & HINT_BLOCK_SCOPE)
863 op->op_flags |= OPf_PARENS;
866 op = newOP(OP_STUB, 0);
876 if (op && op->op_flags & OPf_KIDS) {
877 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
894 if (!op || error_count)
897 switch (op->op_type) {
899 if (!(op->op_private & (OPpCONST_ARYBASE)))
901 if (eval_start && eval_start->op_type == OP_CONST) {
902 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
906 SAVEI32(compiling.cop_arybase);
907 compiling.cop_arybase = 0;
909 else if (type == OP_REFGEN)
912 croak("That use of $[ is unsupported");
915 if ((type == OP_UNDEF || type == OP_REFGEN) &&
916 !(op->op_flags & OPf_STACKED)) {
917 op->op_type = OP_RV2CV; /* entersub => rv2cv */
918 op->op_ppaddr = ppaddr[OP_RV2CV];
919 assert(cUNOP->op_first->op_type == OP_NULL);
920 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
926 /* grep, foreach, subcalls, refgen */
927 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
929 sprintf(tokenbuf, "Can't modify %s in %s",
930 op_desc[op->op_type],
931 type ? op_desc[type] : "local");
955 if (!(op->op_flags & OPf_STACKED))
961 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
967 if (type == OP_REFGEN && op->op_flags & OPf_PARENS) {
969 return op; /* Treat \(@foo) like ordinary list. */
973 ref(cUNOP->op_first, op->op_type);
985 if (!type && cUNOP->op_first->op_type != OP_GV)
986 croak("Can't localize a reference");
987 ref(cUNOP->op_first, op->op_type);
1000 if (type == OP_REFGEN && op->op_flags & OPf_PARENS)
1001 return op; /* Treat \(@foo) like ordinary list. */
1006 croak("Can't localize lexical variable %s",
1007 SvPV(*av_fetch(comppad_name, op->op_targ, 4), na));
1014 if (type != OP_SASSIGN)
1027 pad_free(op->op_targ);
1028 op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
1029 sv = PAD_SV(op->op_targ);
1030 sv_upgrade(sv, SVt_PVLV);
1031 sv_magic(sv, Nullsv, mtype, Nullch, 0);
1032 curpad[op->op_targ] = sv;
1033 if (op->op_flags & OPf_KIDS)
1034 mod(cBINOP->op_first->op_sibling, type);
1039 ref(cBINOP->op_first, op->op_type);
1046 if (op->op_flags & OPf_KIDS)
1047 mod(cLISTOP->op_last, type);
1051 if (!(op->op_flags & OPf_KIDS))
1053 if (op->op_targ != OP_LIST) {
1054 mod(cBINOP->op_first, type);
1059 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1063 op->op_flags |= OPf_MOD;
1065 if (type == OP_AASSIGN || type == OP_SASSIGN)
1066 op->op_flags |= OPf_SPECIAL|OPf_REF;
1068 op->op_private |= OPpLVAL_INTRO;
1069 op->op_flags &= ~OPf_SPECIAL;
1071 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1072 op->op_flags |= OPf_REF;
1082 if (op && op->op_flags & OPf_KIDS) {
1083 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1096 if (!op || error_count)
1099 switch (op->op_type) {
1101 if ((type == OP_DEFINED) &&
1102 !(op->op_flags & OPf_STACKED)) {
1103 op->op_type = OP_RV2CV; /* entersub => rv2cv */
1104 op->op_ppaddr = ppaddr[OP_RV2CV];
1105 assert(cUNOP->op_first->op_type == OP_NULL);
1106 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
1107 op->op_flags |= OPf_SPECIAL;
1112 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
1116 ref(cUNOP->op_first, op->op_type);
1119 if (type == OP_RV2AV || type == OP_RV2HV) {
1120 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
1121 op->op_flags |= OPf_MOD;
1127 op->op_flags |= OPf_REF;
1130 ref(cUNOP->op_first, op->op_type);
1135 op->op_flags |= OPf_REF;
1140 if (!(op->op_flags & OPf_KIDS))
1142 ref(cBINOP->op_first, type);
1146 ref(cBINOP->op_first, op->op_type);
1147 if (type == OP_RV2AV || type == OP_RV2HV) {
1148 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
1149 op->op_flags |= OPf_MOD;
1157 if (!(op->op_flags & OPf_KIDS))
1159 ref(cLISTOP->op_last, type);
1175 if (!op || error_count)
1179 if (type == OP_LIST) {
1180 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1183 else if (type != OP_PADSV &&
1186 type != OP_PUSHMARK)
1188 sprintf(tokenbuf, "Can't declare %s in my", op_desc[op->op_type]);
1192 op->op_flags |= OPf_MOD;
1193 op->op_private |= OPpLVAL_INTRO;
1202 o->op_flags |= OPf_PARENS;
1207 bind_match(type, left, right)
1214 if (right->op_type == OP_MATCH ||
1215 right->op_type == OP_SUBST ||
1216 right->op_type == OP_TRANS) {
1217 right->op_flags |= OPf_STACKED;
1218 if (right->op_type != OP_MATCH)
1219 left = mod(left, right->op_type);
1220 if (right->op_type == OP_TRANS)
1221 op = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1223 op = prepend_elem(right->op_type, scalar(left), right);
1225 return newUNOP(OP_NOT, 0, scalar(op));
1229 return bind_match(type, left,
1230 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1239 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1240 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
1248 if (o->op_flags & OPf_PARENS || perldb || tainting) {
1249 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1250 o->op_type = OP_LEAVE;
1251 o->op_ppaddr = ppaddr[OP_LEAVE];
1254 if (o->op_type == OP_LINESEQ) {
1256 o->op_type = OP_SCOPE;
1257 o->op_ppaddr = ppaddr[OP_SCOPE];
1258 kid = ((LISTOP*)o)->op_first;
1259 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1260 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1265 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1274 int retval = savestack_ix;
1275 SAVEINT(comppad_name_floor);
1276 if ((comppad_name_fill = AvFILL(comppad_name)) > 0)
1277 comppad_name_floor = comppad_name_fill;
1279 comppad_name_floor = 0;
1280 SAVEINT(min_intro_pending);
1281 SAVEINT(max_intro_pending);
1282 min_intro_pending = 0;
1283 SAVEINT(comppad_name_fill);
1284 SAVEINT(padix_floor);
1285 padix_floor = padix;
1286 pad_reset_pending = FALSE;
1288 hints &= ~HINT_BLOCK_SCOPE;
1293 block_end(line, floor, seq)
1298 int needblockscope = hints & HINT_BLOCK_SCOPE;
1299 OP* retval = scalarseq(seq);
1300 if (copline > (line_t)line)
1303 pad_reset_pending = FALSE;
1305 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1306 pad_leavemy(comppad_name_fill);
1315 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), op);
1316 eval_start = linklist(eval_root);
1317 eval_root->op_next = 0;
1325 main_root = scope(sawparens(scalarvoid(op)));
1326 curcop = &compiling;
1327 main_start = LINKLIST(main_root);
1328 main_root->op_next = 0;
1340 if (o->op_flags & OPf_PARENS)
1344 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1346 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1347 if (*s == ';' || *s == '=')
1348 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1355 return mod(o, OP_NULL); /* a bit kludgey */
1362 if (o->op_type == OP_LIST) {
1363 o = convert(OP_JOIN, 0,
1364 prepend_elem(OP_LIST,
1365 newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1376 I32 type = o->op_type;
1379 if (opargs[type] & OA_RETSCALAR)
1381 if (opargs[type] & OA_TARGET)
1382 o->op_targ = pad_alloc(type, SVs_PADTMP);
1384 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1385 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1387 if (!(opargs[type] & OA_FOLDCONST))
1391 goto nope; /* Don't try to run w/ errors */
1393 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1394 if (curop->op_type != OP_CONST &&
1395 curop->op_type != OP_LIST &&
1396 curop->op_type != OP_SCALAR &&
1397 curop->op_type != OP_NULL &&
1398 curop->op_type != OP_PUSHMARK) {
1403 curop = LINKLIST(o);
1408 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1409 pad_swipe(o->op_targ);
1410 else if (SvTEMP(sv)) { /* grab mortal temp? */
1411 (void)SvREFCNT_inc(sv);
1415 if (type == OP_RV2GV)
1416 return newGVOP(OP_GV, 0, (GV*)sv);
1418 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1420 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1425 SvIOK_off(sv); /* undo SvIV() damage */
1427 return newSVOP(OP_CONST, 0, sv);
1431 if (!(opargs[type] & OA_OTHERINT))
1434 if (!(hints & HINT_INTEGER)) {
1435 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1438 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1439 if (curop->op_type == OP_CONST) {
1440 if (SvIOK(((SVOP*)curop)->op_sv))
1444 if (opargs[curop->op_type] & OA_RETINTEGER)
1448 o->op_ppaddr = ppaddr[++(o->op_type)];
1455 gen_constant_list(o)
1459 I32 oldtmps_floor = tmps_floor;
1463 return o; /* Don't attempt to run with errors */
1465 op = curop = LINKLIST(o);
1471 tmps_floor = oldtmps_floor;
1473 o->op_type = OP_RV2AV;
1474 o->op_ppaddr = ppaddr[OP_RV2AV];
1475 curop = ((UNOP*)o)->op_first;
1476 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1483 convert(type, flags, op)
1491 if (!op || op->op_type != OP_LIST)
1492 op = newLISTOP(OP_LIST, 0, op, Nullop);
1494 op->op_flags &= ~(OPf_KNOW|OPf_LIST);
1496 if (!(opargs[type] & OA_MARK))
1497 null(cLISTOP->op_first);
1500 op->op_ppaddr = ppaddr[type];
1501 op->op_flags |= flags;
1503 op = CHECKOP(type, op);
1504 if (op->op_type != type)
1507 if (cLISTOP->op_children < 7) {
1508 /* XXX do we really need to do this if we're done appending?? */
1509 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1511 cLISTOP->op_last = last; /* in case check substituted last arg */
1514 return fold_constants(op);
1517 /* List constructors */
1520 append_elem(type, first, last)
1531 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1532 return newLISTOP(type, 0, first, last);
1534 if (first->op_flags & OPf_KIDS)
1535 ((LISTOP*)first)->op_last->op_sibling = last;
1537 first->op_flags |= OPf_KIDS;
1538 ((LISTOP*)first)->op_first = last;
1540 ((LISTOP*)first)->op_last = last;
1541 ((LISTOP*)first)->op_children++;
1546 append_list(type, first, last)
1557 if (first->op_type != type)
1558 return prepend_elem(type, (OP*)first, (OP*)last);
1560 if (last->op_type != type)
1561 return append_elem(type, (OP*)first, (OP*)last);
1563 first->op_last->op_sibling = last->op_first;
1564 first->op_last = last->op_last;
1565 first->op_children += last->op_children;
1566 if (first->op_children)
1567 last->op_flags |= OPf_KIDS;
1574 prepend_elem(type, first, last)
1585 if (last->op_type == type) {
1586 if (type == OP_LIST) { /* already a PUSHMARK there */
1587 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1588 ((LISTOP*)last)->op_first->op_sibling = first;
1591 if (!(last->op_flags & OPf_KIDS)) {
1592 ((LISTOP*)last)->op_last = first;
1593 last->op_flags |= OPf_KIDS;
1595 first->op_sibling = ((LISTOP*)last)->op_first;
1596 ((LISTOP*)last)->op_first = first;
1598 ((LISTOP*)last)->op_children++;
1602 return newLISTOP(type, 0, first, last);
1610 return newOP(OP_STUB, 0);
1617 if (!op || op->op_type != OP_LIST)
1618 op = newLISTOP(OP_LIST, 0, op, Nullop);
1624 newLISTOP(type, flags, first, last)
1632 Newz(1101, listop, 1, LISTOP);
1634 listop->op_type = type;
1635 listop->op_ppaddr = ppaddr[type];
1636 listop->op_children = (first != 0) + (last != 0);
1637 listop->op_flags = flags;
1641 else if (!first && last)
1644 first->op_sibling = last;
1645 listop->op_first = first;
1646 listop->op_last = last;
1647 if (type == OP_LIST) {
1649 pushop = newOP(OP_PUSHMARK, 0);
1650 pushop->op_sibling = first;
1651 listop->op_first = pushop;
1652 listop->op_flags |= OPf_KIDS;
1654 listop->op_last = pushop;
1656 else if (listop->op_children)
1657 listop->op_flags |= OPf_KIDS;
1668 Newz(1101, op, 1, OP);
1670 op->op_ppaddr = ppaddr[type];
1671 op->op_flags = flags;
1674 op->op_private = 0 + (flags >> 8);
1675 if (opargs[type] & OA_RETSCALAR)
1677 if (opargs[type] & OA_TARGET)
1678 op->op_targ = pad_alloc(type, SVs_PADTMP);
1679 return CHECKOP(type, op);
1683 newUNOP(type, flags, first)
1691 first = newOP(OP_STUB, 0);
1692 if (opargs[type] & OA_MARK)
1693 first = force_list(first);
1695 Newz(1101, unop, 1, UNOP);
1696 unop->op_type = type;
1697 unop->op_ppaddr = ppaddr[type];
1698 unop->op_first = first;
1699 unop->op_flags = flags | OPf_KIDS;
1700 unop->op_private = 1 | (flags >> 8);
1702 unop = (UNOP*) CHECKOP(type, unop);
1706 return fold_constants((OP *) unop);
1710 newBINOP(type, flags, first, last)
1717 Newz(1101, binop, 1, BINOP);
1720 first = newOP(OP_NULL, 0);
1722 binop->op_type = type;
1723 binop->op_ppaddr = ppaddr[type];
1724 binop->op_first = first;
1725 binop->op_flags = flags | OPf_KIDS;
1728 binop->op_private = 1 | (flags >> 8);
1731 binop->op_private = 2 | (flags >> 8);
1732 first->op_sibling = last;
1735 binop = (BINOP*)CHECKOP(type, binop);
1739 binop->op_last = last = binop->op_first->op_sibling;
1741 return fold_constants((OP *)binop);
1745 pmtrans(op, expr, repl)
1750 SV *tstr = ((SVOP*)expr)->op_sv;
1751 SV *rstr = ((SVOP*)repl)->op_sv;
1754 register U8 *t = (U8*)SvPV(tstr, tlen);
1755 register U8 *r = (U8*)SvPV(rstr, rlen);
1760 register short *tbl;
1762 tbl = (short*)cPVOP->op_pv;
1763 complement = op->op_private & OPpTRANS_COMPLEMENT;
1764 delete = op->op_private & OPpTRANS_DELETE;
1765 /* squash = op->op_private & OPpTRANS_SQUASH; */
1768 Zero(tbl, 256, short);
1769 for (i = 0; i < tlen; i++)
1771 for (i = 0, j = 0; i < 256; i++) {
1787 if (!rlen && !delete) {
1790 for (i = 0; i < 256; i++)
1792 for (i = 0, j = 0; i < tlen; i++,j++) {
1795 if (tbl[t[i]] == -1)
1801 if (tbl[t[i]] == -1)
1812 newPMOP(type, flags)
1818 Newz(1101, pmop, 1, PMOP);
1819 pmop->op_type = type;
1820 pmop->op_ppaddr = ppaddr[type];
1821 pmop->op_flags = flags;
1822 pmop->op_private = 0 | (flags >> 8);
1824 /* link into pm list */
1825 if (type != OP_TRANS && curstash) {
1826 pmop->op_pmnext = HvPMROOT(curstash);
1827 HvPMROOT(curstash) = pmop;
1834 pmruntime(op, expr, repl)
1842 if (op->op_type == OP_TRANS)
1843 return pmtrans(op, expr, repl);
1847 if (expr->op_type == OP_CONST) {
1849 SV *pat = ((SVOP*)expr)->op_sv;
1850 char *p = SvPV(pat, plen);
1851 if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
1852 sv_setpvn(pat, "\\s+", 3);
1853 p = SvPV(pat, plen);
1854 pm->op_pmflags |= PMf_SKIPWHITE;
1856 pm->op_pmregexp = pregcomp(p, p + plen, pm);
1857 if (strEQ("\\s+", pm->op_pmregexp->precomp))
1858 pm->op_pmflags |= PMf_WHITE;
1863 if (pm->op_pmflags & PMf_KEEP)
1864 expr = newUNOP(OP_REGCMAYBE,0,expr);
1866 Newz(1101, rcop, 1, LOGOP);
1867 rcop->op_type = OP_REGCOMP;
1868 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1869 rcop->op_first = scalar(expr);
1870 rcop->op_flags |= OPf_KIDS;
1871 rcop->op_private = 1;
1872 rcop->op_other = op;
1874 /* establish postfix order */
1875 if (pm->op_pmflags & PMf_KEEP) {
1877 rcop->op_next = expr;
1878 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1881 rcop->op_next = LINKLIST(expr);
1882 expr->op_next = (OP*)rcop;
1885 prepend_elem(op->op_type, scalar((OP*)rcop), op);
1890 if (pm->op_pmflags & PMf_EVAL)
1892 else if (repl->op_type == OP_CONST)
1896 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1897 if (opargs[curop->op_type] & OA_DANGEROUS) {
1898 if (curop->op_type == OP_GV) {
1899 GV *gv = ((GVOP*)curop)->op_gv;
1900 if (strchr("&`'123456789+", *GvENAME(gv)))
1903 else if (curop->op_type == OP_RV2CV)
1905 else if (curop->op_type == OP_RV2SV ||
1906 curop->op_type == OP_RV2AV ||
1907 curop->op_type == OP_RV2HV ||
1908 curop->op_type == OP_RV2GV) {
1909 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1912 else if (curop->op_type == OP_PADSV ||
1913 curop->op_type == OP_PADAV ||
1914 curop->op_type == OP_PADHV ||
1915 curop->op_type == OP_PADANY) {
1924 if (curop == repl) {
1925 pm->op_pmflags |= PMf_CONST; /* const for long enough */
1926 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
1927 prepend_elem(op->op_type, scalar(repl), op);
1930 Newz(1101, rcop, 1, LOGOP);
1931 rcop->op_type = OP_SUBSTCONT;
1932 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1933 rcop->op_first = scalar(repl);
1934 rcop->op_flags |= OPf_KIDS;
1935 rcop->op_private = 1;
1936 rcop->op_other = op;
1938 /* establish postfix order */
1939 rcop->op_next = LINKLIST(repl);
1940 repl->op_next = (OP*)rcop;
1942 pm->op_pmreplroot = scalar((OP*)rcop);
1943 pm->op_pmreplstart = LINKLIST(rcop);
1952 newSVOP(type, flags, sv)
1958 Newz(1101, svop, 1, SVOP);
1959 svop->op_type = type;
1960 svop->op_ppaddr = ppaddr[type];
1962 svop->op_next = (OP*)svop;
1963 svop->op_flags = flags;
1964 if (opargs[type] & OA_RETSCALAR)
1966 if (opargs[type] & OA_TARGET)
1967 svop->op_targ = pad_alloc(type, SVs_PADTMP);
1968 return CHECKOP(type, svop);
1972 newGVOP(type, flags, gv)
1978 Newz(1101, gvop, 1, GVOP);
1979 gvop->op_type = type;
1980 gvop->op_ppaddr = ppaddr[type];
1981 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
1982 gvop->op_next = (OP*)gvop;
1983 gvop->op_flags = flags;
1984 if (opargs[type] & OA_RETSCALAR)
1986 if (opargs[type] & OA_TARGET)
1987 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
1988 return CHECKOP(type, gvop);
1992 newPVOP(type, flags, pv)
1998 Newz(1101, pvop, 1, PVOP);
1999 pvop->op_type = type;
2000 pvop->op_ppaddr = ppaddr[type];
2002 pvop->op_next = (OP*)pvop;
2003 pvop->op_flags = flags;
2004 if (opargs[type] & OA_RETSCALAR)
2006 if (opargs[type] & OA_TARGET)
2007 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2008 return CHECKOP(type, pvop);
2017 save_hptr(&curstash);
2018 save_item(curstname);
2023 name = SvPV(sv, len);
2024 curstash = gv_stashpvn(name,len,TRUE);
2025 sv_setpvn(curstname, name, len);
2029 sv_setpv(curstname,"<none>");
2037 utilize(aver, floor, version, id, arg)
2050 if (id->op_type != OP_CONST)
2051 croak("Module name must be constant");
2055 if(version != Nullop) {
2056 SV *vesv = ((SVOP*)version)->op_sv;
2058 if (arg == Nullop && !SvNIOK(vesv)) {
2065 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2066 croak("Version number must be constant number");
2068 /* Make copy of id so we don't free it twice */
2069 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2071 /* Fake up a method call to VERSION */
2072 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2073 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2074 append_elem(OP_LIST,
2075 prepend_elem(OP_LIST, pack, list(version)),
2076 newUNOP(OP_METHOD, 0, meth)));
2080 /* Fake up an import/unimport */
2081 if (arg && arg->op_type == OP_STUB)
2082 imop = arg; /* no import on explicit () */
2083 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2084 imop = Nullop; /* use 5.0; */
2087 /* Make copy of id so we don't free it twice */
2088 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2089 meth = newSVOP(OP_CONST, 0,
2091 ? newSVpv("import", 6)
2092 : newSVpv("unimport", 8)
2094 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2095 append_elem(OP_LIST,
2096 prepend_elem(OP_LIST, pack, list(arg)),
2097 newUNOP(OP_METHOD, 0, meth)));
2100 /* Fake up a require */
2101 rqop = newUNOP(OP_REQUIRE, 0, id);
2103 /* Fake up the BEGIN {}, which does its thing immediately. */
2105 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2107 append_elem(OP_LINESEQ,
2108 append_elem(OP_LINESEQ,
2109 newSTATEOP(0, Nullch, rqop),
2110 newSTATEOP(0, Nullch, veop)),
2111 newSTATEOP(0, Nullch, imop) ));
2118 newSLICEOP(flags, subscript, listval)
2123 return newBINOP(OP_LSLICE, flags,
2124 list(force_list(subscript)),
2125 list(force_list(listval)) );
2135 if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
2136 op = cUNOP->op_first;
2138 if (op->op_type == OP_COND_EXPR) {
2139 I32 t = list_assignment(cCONDOP->op_first->op_sibling);
2140 I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
2145 yyerror("Assignment to both a list and a scalar");
2149 if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
2150 op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
2151 op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
2154 if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
2157 if (op->op_type == OP_RV2SV)
2164 newASSIGNOP(flags, left, optype, right)
2173 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2174 return newLOGOP(optype, 0,
2175 mod(scalar(left), optype),
2176 newUNOP(OP_SASSIGN, 0, scalar(right)));
2179 return newBINOP(optype, OPf_STACKED,
2180 mod(scalar(left), optype), scalar(right));
2184 if (list_assignment(left)) {
2186 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2187 left = mod(left, OP_AASSIGN);
2195 op = newBINOP(OP_AASSIGN, flags,
2196 list(force_list(right)),
2197 list(force_list(left)) );
2198 op->op_private = 0 | (flags >> 8);
2199 if (!(left->op_private & OPpLVAL_INTRO)) {
2200 static int generation = 100;
2204 for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
2205 if (opargs[curop->op_type] & OA_DANGEROUS) {
2206 if (curop->op_type == OP_GV) {
2207 GV *gv = ((GVOP*)curop)->op_gv;
2208 if (gv == defgv || SvCUR(gv) == generation)
2210 SvCUR(gv) = generation;
2212 else if (curop->op_type == OP_PADSV ||
2213 curop->op_type == OP_PADAV ||
2214 curop->op_type == OP_PADHV ||
2215 curop->op_type == OP_PADANY) {
2216 SV **svp = AvARRAY(comppad_name);
2217 SV *sv = svp[curop->op_targ];
2218 if (SvCUR(sv) == generation)
2220 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2222 else if (curop->op_type == OP_RV2CV)
2224 else if (curop->op_type == OP_RV2SV ||
2225 curop->op_type == OP_RV2AV ||
2226 curop->op_type == OP_RV2HV ||
2227 curop->op_type == OP_RV2GV) {
2228 if (lastop->op_type != OP_GV) /* funny deref? */
2237 op->op_private = OPpASSIGN_COMMON;
2239 if (right && right->op_type == OP_SPLIT) {
2241 if ((tmpop = ((LISTOP*)right)->op_first) &&
2242 tmpop->op_type == OP_PUSHRE)
2244 PMOP *pm = (PMOP*)tmpop;
2245 if (left->op_type == OP_RV2AV &&
2246 !(left->op_private & OPpLVAL_INTRO) &&
2247 !(op->op_private & OPpASSIGN_COMMON) )
2249 tmpop = ((UNOP*)left)->op_first;
2250 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2251 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2252 pm->op_pmflags |= PMf_ONCE;
2253 tmpop = ((UNOP*)op)->op_first; /* to list (nulled) */
2254 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2255 tmpop->op_sibling = Nullop; /* don't free split */
2256 right->op_next = tmpop->op_next; /* fix starting loc */
2257 op_free(op); /* blow off assign */
2258 right->op_flags &= ~(OPf_KNOW|OPf_LIST);
2259 /* "I don't know and I don't care." */
2264 if (modcount < 10000 &&
2265 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2267 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2269 sv_setiv(sv, modcount+1);
2277 right = newOP(OP_UNDEF, 0);
2278 if (right->op_type == OP_READLINE) {
2279 right->op_flags |= OPf_STACKED;
2280 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2283 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2284 op = newBINOP(OP_SASSIGN, flags,
2285 scalar(right), mod(scalar(left), OP_SASSIGN) );
2297 newSTATEOP(flags, label, op)
2304 /* Introduce my variables. */
2305 if (min_intro_pending) {
2306 SV **svp = AvARRAY(comppad_name);
2309 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2310 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2311 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2312 SvNVX(sv) = (double)cop_seqmax;
2315 min_intro_pending = 0;
2316 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2319 Newz(1101, cop, 1, COP);
2320 if (perldb && curcop->cop_line && curstash != debstash) {
2321 cop->op_type = OP_DBSTATE;
2322 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2325 cop->op_type = OP_NEXTSTATE;
2326 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2328 cop->op_flags = flags;
2329 cop->op_private = 0 | (flags >> 8);
2330 cop->op_next = (OP*)cop;
2333 cop->cop_label = label;
2334 hints |= HINT_BLOCK_SCOPE;
2336 cop->cop_seq = cop_seqmax++;
2337 cop->cop_arybase = curcop->cop_arybase;
2339 if (copline == NOLINE)
2340 cop->cop_line = curcop->cop_line;
2342 cop->cop_line = copline;
2345 cop->cop_filegv = GvREFCNT_inc(curcop->cop_filegv);
2346 cop->cop_stash = curstash;
2348 if (perldb && curstash != debstash) {
2349 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2350 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2351 (void)SvIOK_on(*svp);
2353 SvSTASH(*svp) = (HV*)cop;
2357 return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2361 newLOGOP(type, flags, first, other)
2370 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2371 return newBINOP(type, flags, scalar(first), scalar(other));
2373 scalarboolean(first);
2374 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2375 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2376 if (type == OP_AND || type == OP_OR) {
2382 first = cUNOP->op_first;
2384 first->op_next = op->op_next;
2385 cUNOP->op_first = Nullop;
2389 if (first->op_type == OP_CONST) {
2390 if (dowarn && (first->op_private & OPpCONST_BARE))
2391 warn("Probable precedence problem on %s", op_desc[type]);
2392 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2401 else if (first->op_type == OP_WANTARRAY) {
2411 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2412 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2414 Newz(1101, logop, 1, LOGOP);
2416 logop->op_type = type;
2417 logop->op_ppaddr = ppaddr[type];
2418 logop->op_first = first;
2419 logop->op_flags = flags | OPf_KIDS;
2420 logop->op_other = LINKLIST(other);
2421 logop->op_private = 1 | (flags >> 8);
2423 /* establish postfix order */
2424 logop->op_next = LINKLIST(first);
2425 first->op_next = (OP*)logop;
2426 first->op_sibling = other;
2428 op = newUNOP(OP_NULL, 0, (OP*)logop);
2429 other->op_next = op;
2435 newCONDOP(flags, first, trueop, falseop)
2445 return newLOGOP(OP_AND, 0, first, trueop);
2447 return newLOGOP(OP_OR, 0, first, falseop);
2449 scalarboolean(first);
2450 if (first->op_type == OP_CONST) {
2451 if (SvTRUE(((SVOP*)first)->op_sv)) {
2462 else if (first->op_type == OP_WANTARRAY) {
2466 Newz(1101, condop, 1, CONDOP);
2468 condop->op_type = OP_COND_EXPR;
2469 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2470 condop->op_first = first;
2471 condop->op_flags = flags | OPf_KIDS;
2472 condop->op_true = LINKLIST(trueop);
2473 condop->op_false = LINKLIST(falseop);
2474 condop->op_private = 1 | (flags >> 8);
2476 /* establish postfix order */
2477 condop->op_next = LINKLIST(first);
2478 first->op_next = (OP*)condop;
2480 first->op_sibling = trueop;
2481 trueop->op_sibling = falseop;
2482 op = newUNOP(OP_NULL, 0, (OP*)condop);
2484 trueop->op_next = op;
2485 falseop->op_next = op;
2491 newRANGE(flags, left, right)
2501 Newz(1101, condop, 1, CONDOP);
2503 condop->op_type = OP_RANGE;
2504 condop->op_ppaddr = ppaddr[OP_RANGE];
2505 condop->op_first = left;
2506 condop->op_flags = OPf_KIDS;
2507 condop->op_true = LINKLIST(left);
2508 condop->op_false = LINKLIST(right);
2509 condop->op_private = 1 | (flags >> 8);
2511 left->op_sibling = right;
2513 condop->op_next = (OP*)condop;
2514 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2515 flop = newUNOP(OP_FLOP, 0, flip);
2516 op = newUNOP(OP_NULL, 0, flop);
2519 left->op_next = flip;
2520 right->op_next = flop;
2522 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2523 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2524 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2525 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2527 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2528 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2531 if (!flip->op_private || !flop->op_private)
2532 linklist(op); /* blow off optimizer unless constant */
2538 newLOOPOP(flags, debuggable, expr, block)
2546 int once = block && block->op_flags & OPf_SPECIAL &&
2547 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2550 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2551 return block; /* do {} while 0 does once */
2552 else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
2553 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr);
2556 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2557 op = newLOGOP(OP_AND, 0, expr, listop);
2559 ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2561 if (once && op != listop)
2562 op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2565 op = newUNOP(OP_NULL, 0, op); /* or do {} while 1 loses outer block */
2567 op->op_flags |= flags;
2569 op->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2574 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2588 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2589 expr = newUNOP(OP_DEFINED, 0,
2590 newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2594 block = newOP(OP_NULL, 0);
2597 next = LINKLIST(cont);
2599 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2601 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2602 redo = LINKLIST(listop);
2605 op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2606 if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2607 op_free(expr); /* oops, it's a while (0) */
2609 return Nullop; /* (listop already freed by newLOGOP) */
2611 ((LISTOP*)listop)->op_last->op_next = condop =
2612 (op == listop ? redo : LINKLIST(op));
2620 Newz(1101,loop,1,LOOP);
2621 loop->op_type = OP_ENTERLOOP;
2622 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2623 loop->op_private = 0;
2624 loop->op_next = (OP*)loop;
2627 op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2629 loop->op_redoop = redo;
2630 loop->op_lastop = op;
2633 loop->op_nextop = next;
2635 loop->op_nextop = op;
2637 op->op_flags |= flags;
2638 op->op_private |= (flags >> 8);
2643 #ifndef CAN_PROTOTYPE
2644 newFOROP(flags,label,forline,sv,expr,block,cont)
2653 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2654 #endif /* CAN_PROTOTYPE */
2662 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2663 sv->op_type = OP_RV2GV;
2664 sv->op_ppaddr = ppaddr[OP_RV2GV];
2666 else if (sv->op_type == OP_PADSV) { /* private variable */
2667 padoff = sv->op_targ;
2672 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2675 sv = newGVOP(OP_GV, 0, defgv);
2677 if (expr->op_type == OP_RV2AV) {
2678 expr = scalar(ref(expr, OP_ITER));
2679 iterflags |= OPf_STACKED;
2681 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2682 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2684 assert(!loop->op_next);
2685 Renew(loop, 1, LOOP);
2686 loop->op_targ = padoff;
2687 return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2688 newOP(OP_ITER, 0), block, cont));
2692 newLOOPEX(type, label)
2697 if (type != OP_GOTO || label->op_type == OP_CONST) {
2698 op = newPVOP(type, 0, savepv(
2699 label->op_type == OP_CONST
2700 ? SvPVx(((SVOP*)label)->op_sv, na)
2705 if (label->op_type == OP_ENTERSUB)
2706 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2707 op = newUNOP(type, OPf_STACKED, label);
2709 hints |= HINT_BLOCK_SCOPE;
2717 if (!CvXSUB(cv) && CvROOT(cv)) {
2719 croak("Can't undef active subroutine");
2726 op_free(CvROOT(cv));
2727 CvROOT(cv) = Nullop;
2730 SvREFCNT_dec(CvGV(cv));
2732 SvREFCNT_dec(CvOUTSIDE(cv));
2733 CvOUTSIDE(cv) = Nullcv;
2734 if (CvPADLIST(cv)) {
2735 I32 i = AvFILL(CvPADLIST(cv));
2737 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2741 SvREFCNT_dec((SV*)CvPADLIST(cv));
2742 CvPADLIST(cv) = Nullav;
2752 AV* protopadlist = CvPADLIST(proto);
2753 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
2754 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
2755 SV** svp = AvARRAY(protopad);
2764 cv = compcv = (CV*)NEWSV(1104,0);
2765 sv_upgrade((SV *)cv, SVt_PVCV);
2768 CvFILEGV(cv) = CvFILEGV(proto);
2769 CvGV(cv) = GvREFCNT_inc(CvGV(proto));
2770 CvSTASH(cv) = CvSTASH(proto);
2771 CvROOT(cv) = CvROOT(proto);
2772 CvSTART(cv) = CvSTART(proto);
2773 if (CvOUTSIDE(proto))
2774 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc((SV*)CvOUTSIDE(proto));
2778 comppadlist = newAV();
2779 AvREAL_off(comppadlist);
2780 av_store(comppadlist, 0, SvREFCNT_inc((SV*)protopad_name));
2781 av_store(comppadlist, 1, (SV*)comppad);
2782 CvPADLIST(cv) = comppadlist;
2783 av_extend(comppad, AvFILL(protopad));
2784 curpad = AvARRAY(comppad);
2786 av = newAV(); /* will be @_ */
2788 av_store(comppad, 0, (SV*)av);
2789 AvFLAGS(av) = AVf_REIFY;
2791 svp = AvARRAY(protopad_name);
2792 for ( ix = AvFILL(protopad); ix > 0; ix--) {
2794 if (svp[ix] != &sv_undef) {
2795 char *name = SvPVX(svp[ix]); /* XXX */
2796 if (SvFLAGS(svp[ix]) & SVf_FAKE) { /* lexical from outside? */
2797 I32 off = pad_findlex(name,ix,curcop->cop_seq, CvOUTSIDE(proto),
2800 croak("panic: cv_clone: %s", name);
2802 else { /* our own lexical */
2804 av_store(comppad, ix, sv = (SV*)newAV());
2805 else if (*name == '%')
2806 av_store(comppad, ix, sv = (SV*)newHV());
2808 av_store(comppad, ix, sv = NEWSV(0,0));
2813 av_store(comppad, ix, sv = NEWSV(0,0));
2829 if(cv && SvPOK(cv) && !SvCUR(cv)) {
2830 for (o = CvSTART(cv); o; o = o->op_next) {
2831 OPCODE type = o->op_type;
2833 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
2835 if (type == OP_LEAVESUB || type == OP_RETURN)
2837 if (type != OP_CONST || sv)
2840 sv = ((SVOP*)o)->op_sv;
2847 newSUB(floor,op,proto,block)
2854 char *name = op ? SvPVx(cSVOP->op_sv, na) : "__ANON__";
2855 GV* gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV);
2862 if (cv = GvCV(gv)) {
2864 cv = 0; /* just a cached method */
2865 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
2866 SV* const_sv = cv_const_sv(cv);
2868 char *p = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
2870 if((!proto != !SvPOK(cv)) || (p && strNE(SvPV((SV*)cv,na), p))) {
2871 warn("Prototype mismatch: (%s) vs (%s)",
2872 SvPOK(cv) ? SvPV((SV*)cv,na) : "none",
2876 if ((const_sv || dowarn) && strNE(name, "BEGIN")) {/* already defined (or promised)? */
2877 line_t oldline = curcop->cop_line;
2879 curcop->cop_line = copline;
2880 warn(const_sv ? "Constant subroutine %s redefined"
2881 : "Subroutine %s redefined",name);
2882 curcop->cop_line = oldline;
2888 if (cv) { /* must reuse cv if autoloaded */
2890 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
2891 CvOUTSIDE(compcv) = 0;
2892 CvPADLIST(cv) = CvPADLIST(compcv);
2893 CvPADLIST(compcv) = 0;
2894 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
2895 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
2896 SvREFCNT_dec(compcv);
2903 CvFILEGV(cv) = curcop->cop_filegv;
2904 CvGV(cv) = GvREFCNT_inc(gv);
2905 CvSTASH(cv) = curstash;
2908 char *p = SvPVx(((SVOP*)proto)->op_sv, na);
2909 sv_setpv((SV*)cv, p);
2925 av = newAV(); /* Will be @_ */
2927 av_store(comppad, 0, (SV*)av);
2928 AvFLAGS(av) = AVf_REIFY;
2930 for (ix = AvFILL(comppad); ix > 0; ix--) {
2931 if (!SvPADMY(curpad[ix]))
2932 SvPADTMP_on(curpad[ix]);
2935 if (AvFILL(comppad_name) < AvFILL(comppad))
2936 av_store(comppad_name, AvFILL(comppad), Nullsv);
2938 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
2939 CvSTART(cv) = LINKLIST(CvROOT(cv));
2940 CvROOT(cv)->op_next = 0;
2942 if (s = strrchr(name,':'))
2946 if (strEQ(s, "BEGIN") && !error_count) {
2947 line_t oldline = compiling.cop_line;
2951 SAVESPTR(compiling.cop_filegv);
2955 av_push(beginav, (SV *)cv);
2956 DEBUG_x( dump_sub(gv) );
2957 rs = SvREFCNT_inc(nrs);
2960 if (GvCV(gv) == cv) { /* Detach it. */
2962 GvCV(gv) = 0; /* Was above calllist, why? IZ */
2966 curcop = &compiling;
2967 curcop->cop_line = oldline; /* might have recursed to yylex */
2970 else if (strEQ(s, "END") && !error_count) {
2973 av_unshift(endav, 1);
2974 av_store(endav, 0, SvREFCNT_inc(cv));
2976 if (perldb && curstash != debstash) {
2978 SV *tmpstr = sv_newmortal();
2980 sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), (long)subline);
2981 sv = newSVpv(buf,0);
2983 sprintf(buf,"%ld",(long)curcop->cop_line);
2985 gv_efullname3(tmpstr, gv, Nullch);
2986 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
2992 GvCV(gv) = 0; /* Will remember in SVOP instead. */
3000 newXSUB(name, ix, subaddr, filename)
3006 CV* cv = newXS(name, (void(*)())subaddr, filename);
3008 CvXSUBANY(cv).any_i32 = ix;
3014 newXS(name, subaddr, filename)
3016 void (*subaddr) _((CV*));
3020 GV *gv = gv_fetchpv((name ? name : "__ANON__"), GV_ADDMULTI, SVt_PVCV);
3025 if (cv = GvCV(gv)) {
3027 cv = 0; /* just a cached method */
3028 else if (CvROOT(cv) || CvXSUB(cv)) { /* already defined? */
3030 line_t oldline = curcop->cop_line;
3032 curcop->cop_line = copline;
3033 warn("Subroutine %s redefined",name);
3034 curcop->cop_line = oldline;
3040 if (cv) { /* must reuse cv if autoloaded */
3041 assert(SvREFCNT(CvGV(cv)) > 1);
3042 SvREFCNT_dec(CvGV(cv));
3045 cv = (CV*)NEWSV(1105,0);
3046 sv_upgrade((SV *)cv, SVt_PVCV);
3049 CvGV(cv) = GvREFCNT_inc(gv);
3051 CvFILEGV(cv) = gv_fetchfile(filename);
3052 CvXSUB(cv) = subaddr;
3055 else if (s = strrchr(name,':'))
3059 if (strEQ(s, "BEGIN")) {
3062 av_push(beginav, SvREFCNT_inc(gv));
3064 else if (strEQ(s, "END")) {
3067 av_unshift(endav, 1);
3068 av_store(endav, 0, SvREFCNT_inc(gv));
3071 GvCV(gv) = 0; /* Will remember elsewhere instead. */
3078 newFORM(floor,op,block)
3089 name = SvPVx(cSVOP->op_sv, na);
3092 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3094 if (cv = GvFORM(gv)) {
3096 line_t oldline = curcop->cop_line;
3098 curcop->cop_line = copline;
3099 warn("Format %s redefined",name);
3100 curcop->cop_line = oldline;
3106 CvGV(cv) = GvREFCNT_inc(gv);
3107 CvFILEGV(cv) = curcop->cop_filegv;
3109 for (ix = AvFILL(comppad); ix > 0; ix--) {
3110 if (!SvPADMY(curpad[ix]))
3111 SvPADTMP_on(curpad[ix]);
3114 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3115 CvSTART(cv) = LINKLIST(CvROOT(cv));
3116 CvROOT(cv)->op_next = 0;
3128 return newUNOP(OP_REFGEN, 0,
3129 mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
3136 return newUNOP(OP_REFGEN, 0,
3137 mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3141 newANONSUB(floor, proto, block)
3146 return newUNOP(OP_REFGEN, 0,
3147 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3154 switch (o->op_type) {
3156 o->op_type = OP_PADAV;
3157 o->op_ppaddr = ppaddr[OP_PADAV];
3158 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3161 o->op_type = OP_RV2AV;
3162 o->op_ppaddr = ppaddr[OP_RV2AV];
3167 warn("oops: oopsAV");
3177 switch (o->op_type) {
3180 o->op_type = OP_PADHV;
3181 o->op_ppaddr = ppaddr[OP_PADHV];
3182 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3186 o->op_type = OP_RV2HV;
3187 o->op_ppaddr = ppaddr[OP_RV2HV];
3192 warn("oops: oopsHV");
3202 if (o->op_type == OP_PADANY) {
3203 o->op_type = OP_PADAV;
3204 o->op_ppaddr = ppaddr[OP_PADAV];
3207 return newUNOP(OP_RV2AV, 0, scalar(o));
3215 if (type == OP_MAPSTART)
3216 return newUNOP(OP_NULL, 0, o);
3217 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3224 if (o->op_type == OP_PADANY) {
3225 o->op_type = OP_PADHV;
3226 o->op_ppaddr = ppaddr[OP_PADHV];
3229 return newUNOP(OP_RV2HV, 0, scalar(o));
3236 croak("NOT IMPL LINE %d",__LINE__);
3246 return newUNOP(OP_RV2CV, flags, scalar(o));
3253 if (o->op_type == OP_PADANY) {
3254 o->op_type = OP_PADSV;
3255 o->op_ppaddr = ppaddr[OP_PADSV];
3258 return newUNOP(OP_RV2SV, 0, scalar(o));
3261 /* Check routines. */
3267 if (cUNOP->op_first->op_type == OP_CONCAT)
3268 op->op_flags |= OPf_STACKED;
3276 if (op->op_flags & OPf_KIDS) {
3279 op = modkids(ck_fun(op), op->op_type);
3280 kid = cUNOP->op_first;
3281 newop = kUNOP->op_first->op_sibling;
3283 (newop->op_sibling ||
3284 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3285 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3286 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3290 op_free(kUNOP->op_first);
3291 kUNOP->op_first = newop;
3293 op->op_ppaddr = ppaddr[++op->op_type];
3302 if (op->op_flags & OPf_KIDS) {
3303 OP *kid = cUNOP->op_first;
3304 if (kid->op_type != OP_HELEM)
3305 croak("%s argument is not a HASH element", op_desc[op->op_type]);
3315 I32 type = op->op_type;
3317 if (op->op_flags & OPf_KIDS) {
3318 if (cLISTOP->op_first->op_type == OP_STUB) {
3320 op = newUNOP(type, OPf_SPECIAL,
3321 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
3332 hints |= HINT_BLOCK_SCOPE;
3333 if (op->op_flags & OPf_KIDS) {
3334 SVOP *kid = (SVOP*)cUNOP->op_first;
3337 op->op_flags &= ~OPf_KIDS;
3340 else if (kid->op_type == OP_LINESEQ) {
3343 kid->op_next = op->op_next;
3344 cUNOP->op_first = 0;
3347 Newz(1101, enter, 1, LOGOP);
3348 enter->op_type = OP_ENTERTRY;
3349 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3350 enter->op_private = 0;
3352 /* establish postfix order */
3353 enter->op_next = (OP*)enter;
3355 op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3356 op->op_type = OP_LEAVETRY;
3357 op->op_ppaddr = ppaddr[OP_LEAVETRY];
3358 enter->op_other = op;
3364 op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3366 op->op_targ = (PADOFFSET)hints;
3375 if (op->op_flags & OPf_STACKED) {
3377 kid = cUNOP->op_first->op_sibling;
3378 if (kid->op_type == OP_RV2GV)
3390 o = fold_constants(o);
3391 if (o->op_type == OP_CONST)
3400 SVOP *kid = (SVOP*)cUNOP->op_first;
3402 op->op_private |= (hints & HINT_STRICT_REFS);
3403 if (kid->op_type == OP_CONST) {
3404 int iscv = (op->op_type==OP_RV2CV)*2;
3406 kid->op_type = OP_GV;
3407 for (gv = 0; !gv; iscv++) {
3409 * This is a little tricky. We only want to add the symbol if we
3410 * didn't add it in the lexer. Otherwise we get duplicate strict
3411 * warnings. But if we didn't add it in the lexer, we must at
3412 * least pretend like we wanted to add it even if it existed before,
3413 * or we get possible typo warnings. OPpCONST_ENTERED says
3414 * whether the lexer already added THIS instance of this symbol.
3416 gv = gv_fetchpv(SvPVx(kid->op_sv, na),
3417 iscv | !(kid->op_private & OPpCONST_ENTERED),
3420 : op->op_type == OP_RV2SV
3422 : op->op_type == OP_RV2AV
3424 : op->op_type == OP_RV2HV
3428 SvREFCNT_dec(kid->op_sv);
3429 kid->op_sv = SvREFCNT_inc(gv);
3445 I32 type = op->op_type;
3447 if (op->op_flags & OPf_REF)
3450 if (op->op_flags & OPf_KIDS) {
3451 SVOP *kid = (SVOP*)cUNOP->op_first;
3453 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3454 OP *newop = newGVOP(type, OPf_REF,
3455 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3462 if (type == OP_FTTTY)
3463 return newGVOP(type, OPf_REF, gv_fetchpv("main'STDIN", TRUE,
3466 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3479 int type = op->op_type;
3480 register I32 oa = opargs[type] >> OASHIFT;
3482 if (op->op_flags & OPf_STACKED) {
3483 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
3486 return no_fh_allowed(op);
3489 if (op->op_flags & OPf_KIDS) {
3490 tokid = &cLISTOP->op_first;
3491 kid = cLISTOP->op_first;
3492 if (kid->op_type == OP_PUSHMARK ||
3493 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
3495 tokid = &kid->op_sibling;
3496 kid = kid->op_sibling;
3498 if (!kid && opargs[type] & OA_DEFGV)
3499 *tokid = kid = newSVREF(newGVOP(OP_GV, 0, defgv));
3503 sibl = kid->op_sibling;
3517 if (kid->op_type == OP_CONST &&
3518 (kid->op_private & OPpCONST_BARE)) {
3519 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3520 OP *newop = newAVREF(newGVOP(OP_GV, 0,
3521 gv_fetchpv(name, TRUE, SVt_PVAV) ));
3523 warn("Array @%s missing the @ in argument %d of %s()",
3524 name, numargs, op_desc[type]);
3527 kid->op_sibling = sibl;
3530 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
3531 bad_type(numargs, "array", op_desc[op->op_type], kid);
3535 if (kid->op_type == OP_CONST &&
3536 (kid->op_private & OPpCONST_BARE)) {
3537 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3538 OP *newop = newHVREF(newGVOP(OP_GV, 0,
3539 gv_fetchpv(name, TRUE, SVt_PVHV) ));
3541 warn("Hash %%%s missing the %% in argument %d of %s()",
3542 name, numargs, op_desc[type]);
3545 kid->op_sibling = sibl;
3548 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
3549 bad_type(numargs, "hash", op_desc[op->op_type], kid);
3554 OP *newop = newUNOP(OP_NULL, 0, kid);
3555 kid->op_sibling = 0;
3557 newop->op_next = newop;
3559 kid->op_sibling = sibl;
3564 if (kid->op_type != OP_GV) {
3565 if (kid->op_type == OP_CONST &&
3566 (kid->op_private & OPpCONST_BARE)) {
3567 OP *newop = newGVOP(OP_GV, 0,
3568 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3574 kid->op_sibling = 0;
3575 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3577 kid->op_sibling = sibl;
3583 mod(scalar(kid), type);
3587 tokid = &kid->op_sibling;
3588 kid = kid->op_sibling;
3590 op->op_private |= numargs;
3592 return too_many_arguments(op,op_desc[op->op_type]);
3595 else if (opargs[type] & OA_DEFGV) {
3597 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3601 while (oa & OA_OPTIONAL)
3603 if (oa && oa != OA_LIST)
3604 return too_few_arguments(op,op_desc[op->op_type]);
3613 GV *gv = gv_fetchpv("glob", FALSE, SVt_PVCV);
3615 if (gv && GvIMPORTED_CV(gv)) {
3616 op->op_type = OP_LIST;
3617 op->op_ppaddr = ppaddr[OP_LIST];
3618 op = newUNOP(OP_ENTERSUB, OPf_STACKED,
3619 append_elem(OP_LIST, op,
3620 scalar(newUNOP(OP_RV2CV, 0,
3621 newGVOP(OP_GV, 0, gv)))));
3624 gv = newGVgen("main");
3626 append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3637 OPCODE type = op->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
3639 op->op_ppaddr = ppaddr[OP_GREPSTART];
3640 Newz(1101, gwop, 1, LOGOP);
3642 if (op->op_flags & OPf_STACKED) {
3645 kid = cLISTOP->op_first->op_sibling;
3646 for (k = cLISTOP->op_first->op_sibling->op_next; k; k = k->op_next) {
3649 kid->op_next = (OP*)gwop;
3650 op->op_flags &= ~OPf_STACKED;
3652 kid = cLISTOP->op_first->op_sibling;
3653 if (type == OP_MAPWHILE)
3660 kid = cLISTOP->op_first->op_sibling;
3661 if (kid->op_type != OP_NULL)
3662 croak("panic: ck_grep");
3663 kid = kUNOP->op_first;
3665 gwop->op_type = type;
3666 gwop->op_ppaddr = ppaddr[type];
3667 gwop->op_first = listkids(op);
3668 gwop->op_flags |= OPf_KIDS;
3669 gwop->op_private = 1;
3670 gwop->op_other = LINKLIST(kid);
3671 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
3672 kid->op_next = (OP*)gwop;
3674 kid = cLISTOP->op_first->op_sibling;
3675 if (!kid || !kid->op_sibling)
3676 return too_few_arguments(op,op_desc[op->op_type]);
3677 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
3678 mod(kid, OP_GREPSTART);
3687 if (op->op_flags & OPf_KIDS) {
3688 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3689 if (kid && kid->op_type == OP_CONST)
3690 fbm_compile(((SVOP*)kid)->op_sv, 0);
3699 /* XXX length optimization goes here */
3707 return modkids(ck_fun(op), op->op_type);
3714 return refkids(ck_fun(op), op->op_type);
3723 kid = cLISTOP->op_first;
3725 op = force_list(op);
3726 kid = cLISTOP->op_first;
3728 if (kid->op_type == OP_PUSHMARK)
3729 kid = kid->op_sibling;
3730 if (kid && op->op_flags & OPf_STACKED)
3731 kid = kid->op_sibling;
3732 else if (kid && !kid->op_sibling) { /* print HANDLE; */
3733 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
3734 op->op_flags |= OPf_STACKED; /* make it a filehandle */
3735 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
3736 cLISTOP->op_first->op_sibling = kid;
3737 cLISTOP->op_last = kid;
3738 kid = kid->op_sibling;
3743 append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3745 return listkids(op);
3752 cPMOP->op_pmflags |= PMf_RUNTIME;
3753 cPMOP->op_pmpermflags |= PMf_RUNTIME;
3768 if (cBINOP->op_first->op_flags & OPf_PARENS) {
3769 op->op_private |= OPpREPEAT_DOLIST;
3770 cBINOP->op_first = force_list(cBINOP->op_first);
3781 if (op->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
3782 SVOP *kid = (SVOP*)cUNOP->op_first;
3784 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3786 for (s = SvPVX(kid->op_sv); *s; s++) {
3787 if (*s == ':' && s[1] == ':') {
3789 Move(s+2, s+1, strlen(s+2)+1, char);
3790 --SvCUR(kid->op_sv);
3793 sv_catpvn(kid->op_sv, ".pm", 3);
3803 croak("NOT IMPL LINE %d",__LINE__);
3813 if (op->op_flags & OPf_KIDS) {
3814 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3815 if (kid && kid->op_sibling) {
3816 op->op_type = OP_SSELECT;
3817 op->op_ppaddr = ppaddr[OP_SSELECT];
3819 return fold_constants(op);
3823 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3824 if (kid && kid->op_type == OP_RV2GV)
3825 kid->op_private &= ~HINT_STRICT_REFS;
3833 I32 type = op->op_type;
3835 if (!(op->op_flags & OPf_KIDS)) {
3837 return newUNOP(type, 0,
3838 scalar(newUNOP(OP_RV2AV, 0,
3839 scalar(newGVOP(OP_GV, 0, subline
3841 : gv_fetchpv("ARGV", TRUE, SVt_PVAV) )))));
3843 return scalar(modkids(ck_fun(op), type));
3850 if (op->op_flags & OPf_STACKED) {
3851 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3853 kid = kUNOP->op_first; /* get past rv2gv */
3855 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
3857 if (kid->op_type == OP_SCOPE) {
3861 else if (kid->op_type == OP_LEAVE) {
3862 if (op->op_type == OP_SORT) {
3863 null(kid); /* wipe out leave */
3866 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
3867 if (k->op_next == kid)
3872 kid->op_next = 0; /* just disconnect the leave */
3873 k = kLISTOP->op_first;
3877 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3878 null(kid); /* wipe out rv2gv */
3879 if (op->op_type == OP_SORT)
3883 op->op_flags |= OPf_SPECIAL;
3896 if (op->op_flags & OPf_STACKED)
3897 return no_fh_allowed(op);
3899 kid = cLISTOP->op_first;
3900 if (kid->op_type != OP_NULL)
3901 croak("panic: ck_split");
3902 kid = kid->op_sibling;
3903 op_free(cLISTOP->op_first);
3904 cLISTOP->op_first = kid;
3906 cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
3907 cLISTOP->op_last = kid; /* There was only one element previously */
3910 if (kid->op_type != OP_MATCH) {
3911 OP *sibl = kid->op_sibling;
3912 kid->op_sibling = 0;
3913 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
3914 if (cLISTOP->op_first == cLISTOP->op_last)
3915 cLISTOP->op_last = kid;
3916 cLISTOP->op_first = kid;
3917 kid->op_sibling = sibl;
3920 if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
3921 SvREFCNT_dec(pm->op_pmshort); /* can't use substring to optimize */
3925 kid->op_type = OP_PUSHRE;
3926 kid->op_ppaddr = ppaddr[OP_PUSHRE];
3929 if (!kid->op_sibling)
3930 append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3932 kid = kid->op_sibling;
3935 if (!kid->op_sibling)
3936 append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
3938 kid = kid->op_sibling;
3941 if (kid->op_sibling)
3942 return too_many_arguments(op,op_desc[op->op_type]);
3951 OP *prev = ((cUNOP->op_first->op_sibling)
3952 ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first;
3953 OP *o = prev->op_sibling;
3960 for (cvop = o; cvop->op_sibling; cvop = cvop->op_sibling) ;
3961 if (cvop->op_type == OP_RV2CV) {
3963 op->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
3964 null(cvop); /* disable rv2cv */
3965 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
3966 if (tmpop->op_type == OP_GV) {
3967 cv = GvCV(tmpop->op_sv);
3968 if (cv && SvPOK(cv) && !(op->op_private & OPpENTERSUB_AMPER))
3969 proto = SvPV((SV*)cv,na);
3972 op->op_private |= (hints & HINT_STRICT_REFS);
3973 if (perldb && curstash != debstash)
3974 op->op_private |= OPpENTERSUB_DB;
3979 return too_many_arguments(op, CvNAME(cv));
3997 if (o->op_type != OP_REFGEN && o->op_type != OP_UNDEF)
3998 bad_type(arg, "block", CvNAME(cv), o);
4003 if (o->op_type == OP_RV2GV)
4007 o = newUNOP(OP_RV2GV, 0, kid);
4008 o->op_sibling = kid->op_sibling;
4009 kid->op_sibling = 0;
4010 prev->op_sibling = o;
4018 if (o->op_type != OP_RV2GV)
4019 bad_type(arg, "symbol", CvNAME(cv), o);
4022 if (o->op_type != OP_RV2CV)
4023 bad_type(arg, "sub", CvNAME(cv), o);
4026 if (o->op_type != OP_RV2SV && o->op_type != OP_PADSV)
4027 bad_type(arg, "scalar", CvNAME(cv), o);
4030 if (o->op_type != OP_RV2AV && o->op_type != OP_PADAV)
4031 bad_type(arg, "array", CvNAME(cv), o);
4034 if (o->op_type != OP_RV2HV && o->op_type != OP_PADHV)
4035 bad_type(arg, "hash", CvNAME(cv), o);
4039 o = newUNOP(OP_REFGEN, 0, kid);
4040 o->op_sibling = kid->op_sibling;
4041 kid->op_sibling = 0;
4042 prev->op_sibling = o;
4053 croak("Malformed prototype for %s: %s",
4054 CvNAME(cv),SvPV((SV*)cv,na));
4059 mod(o, OP_ENTERSUB);
4063 if (proto && !optional && *proto == '$')
4064 return too_few_arguments(op, CvNAME(cv));
4072 SvREADONLY_on(cSVOP->op_sv);
4080 if (op->op_flags & OPf_KIDS) {
4081 SVOP *kid = (SVOP*)cUNOP->op_first;
4083 if (kid->op_type == OP_NULL)
4084 kid = (SVOP*)kid->op_sibling;
4086 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4087 op->op_flags |= OPf_SPECIAL;
4092 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4098 register OP* oldop = 0;
4099 if (!o || o->op_seq)
4104 for (; o; o = o->op_next) {
4110 switch (o->op_type) {
4113 curcop = ((COP*)o); /* for warnings */
4114 o->op_seq = op_seqmax++;
4125 if (o->op_next->op_type == OP_STRINGIFY)
4127 o->op_seq = op_seqmax++;
4130 if ((o->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
4131 o->op_seq = op_seqmax++;
4132 break; /* Scalar stub must produce undef. List stub is noop */
4136 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4137 curcop = ((COP*)op);
4143 if (oldop && o->op_next) {
4144 oldop->op_next = o->op_next;
4147 o->op_seq = op_seqmax++;
4151 if (o->op_next->op_type == OP_RV2SV) {
4152 if (!(o->op_next->op_private & (OPpDEREF_HV|OPpDEREF_AV))) {
4154 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4155 o->op_next = o->op_next->op_next;
4156 o->op_type = OP_GVSV;
4157 o->op_ppaddr = ppaddr[OP_GVSV];
4160 else if (o->op_next->op_type == OP_RV2AV) {
4161 OP* pop = o->op_next->op_next;
4163 if (pop->op_type == OP_CONST &&
4164 (op = pop->op_next) &&
4165 pop->op_next->op_type == OP_AELEM &&
4166 !(pop->op_next->op_private &
4167 (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)) &&
4168 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4172 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4176 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4177 o->op_next = pop->op_next->op_next;
4178 o->op_type = OP_AELEMFAST;
4179 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4180 o->op_private = (U8)i;
4181 GvAVn((GV*)(((SVOP*)o)->op_sv));
4184 o->op_seq = op_seqmax++;
4191 o->op_seq = op_seqmax++;
4192 peep(cLOGOP->op_other);
4196 o->op_seq = op_seqmax++;
4197 peep(cCONDOP->op_true);
4198 peep(cCONDOP->op_false);
4202 o->op_seq = op_seqmax++;
4203 peep(cLOOP->op_redoop);
4204 peep(cLOOP->op_nextop);
4205 peep(cLOOP->op_lastop);
4210 o->op_seq = op_seqmax++;
4211 peep(cPMOP->op_pmreplstart);
4215 o->op_seq = op_seqmax++;
4216 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4217 if (o->op_next->op_sibling &&
4218 o->op_next->op_sibling->op_type != OP_DIE) {
4219 line_t oldline = curcop->cop_line;
4221 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4222 warn("Statement unlikely to be reached");
4223 warn("(Maybe you meant system() when you said exec()?)\n");
4224 curcop->cop_line = oldline;
4229 o->op_seq = op_seqmax++;