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
23 * In the following definition, the ", (OP *) op" is just to make the compiler
24 * think the expression is of the right type: croak actually does a longjmp.
26 #define CHECKOP(type,op) ((op_mask && op_mask[type]) ? \
27 (croak("%s trapped by operation mask", op_name[type]), (OP *) op) \
28 : (*check[type])((OP *) op))
30 #define CHECKOP(type,op) (*check[type])(op)
31 #endif /* USE_OP_MASK */
33 static I32 list_assignment _((OP *op));
34 static OP *bad_type _((I32 n, char *t, char *name, OP *kid));
35 static OP *modkids _((OP *op, I32 type));
36 static OP *no_fh_allowed _((OP *op));
37 static OP *scalarboolean _((OP *op));
38 static OP *too_few_arguments _((OP *op, char* name));
39 static OP *too_many_arguments _((OP *op, char* name));
40 static void null _((OP* op));
41 static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, I32 seq,
42 CV* startcv, I32 cx_ix));
48 SV* tmpsv = sv_newmortal();
49 gv_efullname(tmpsv, CvGV(cv));
50 return SvPV(tmpsv,na);
57 sprintf(tokenbuf,"Missing comma after first argument to %s function",
58 op_name[op->op_type]);
64 too_few_arguments(op, name)
68 sprintf(tokenbuf,"Not enough arguments for %s", name);
74 too_many_arguments(op, name)
78 sprintf(tokenbuf,"Too many arguments for %s", name);
84 bad_type(n, t, name, kid)
90 sprintf(tokenbuf, "Type of arg %d to %s must be %s (not %s)",
91 (int) n, name, t, op_name[kid->op_type]);
100 int type = op->op_type;
101 if (type != OP_AELEM && type != OP_HELEM) {
102 sprintf(tokenbuf, "Can't use subscript on %s",
105 if (type == OP_RV2HV || type == OP_ENTERSUB)
106 warn("(Did you mean $ or @ instead of %c?)\n",
107 type == OP_RV2HV ? '%' : '&');
111 /* "register" allocation */
120 if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) {
121 if (!isprint(name[1]))
122 sprintf(name+1, "^%c", name[1] ^ 64); /* XXX is tokenbuf, really */
123 croak("Can't use global %s in \"my\"",name);
125 off = pad_alloc(OP_PADSV, SVs_PADMY);
127 sv_upgrade(sv, SVt_PVNV);
129 av_store(comppad_name, off, sv);
130 SvNVX(sv) = (double)999999999;
131 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
132 if (!min_intro_pending)
133 min_intro_pending = off;
134 max_intro_pending = off;
136 av_store(comppad, off, (SV*)newAV());
137 else if (*name == '%')
138 av_store(comppad, off, (SV*)newHV());
139 SvPADMY_on(curpad[off]);
144 #ifndef CAN_PROTOTYPE
145 pad_findlex(name, newoff, seq, startcv, cx_ix)
152 pad_findlex(char *name, PADOFFSET newoff, I32 seq, CV* startcv, I32 cx_ix)
159 register CONTEXT *cx;
162 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
163 AV* curlist = CvPADLIST(cv);
164 SV** svp = av_fetch(curlist, 0, FALSE);
166 if (!svp || *svp == &sv_undef)
169 svp = AvARRAY(curname);
170 for (off = AvFILL(curname); off > 0; off--) {
171 if ((sv = svp[off]) &&
174 seq > (I32)SvNVX(sv) &&
175 strEQ(SvPVX(sv), name))
177 I32 depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1;
178 AV *oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
179 SV *oldsv = *av_fetch(oldpad, off, TRUE);
180 if (!newoff) { /* Not a mere clone operation. */
181 SV *sv = NEWSV(1103,0);
182 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
183 sv_upgrade(sv, SVt_PVNV);
185 av_store(comppad_name, newoff, sv);
186 SvNVX(sv) = (double)curcop->cop_seq;
187 SvIVX(sv) = 999999999; /* A ref, intro immediately */
188 SvFLAGS(sv) |= SVf_FAKE;
190 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
191 SvFLAGS(compcv) |= SVpcv_CLONE;
197 /* Nothing in current lexical context--try eval's context, if any.
198 * This is necessary to let the perldb get at lexically scoped variables.
199 * XXX This will also probably interact badly with eval tree caching.
203 for (i = cx_ix; i >= 0; i--) {
205 switch (cx->cx_type) {
207 if (i == 0 && saweval) {
208 seq = cxstack[saweval].blk_oldcop->cop_seq;
209 return pad_findlex(name, newoff, seq, main_cv, 0);
213 if (cx->blk_eval.old_op_type != OP_ENTEREVAL)
214 return 0; /* require must have its own scope */
221 if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */
222 saweval = i; /* so we know where we were called from */
225 seq = cxstack[saweval].blk_oldcop->cop_seq;
226 return pad_findlex(name, newoff, seq, cv, i-1);
239 SV **svp = AvARRAY(comppad_name);
240 I32 seq = cop_seqmax;
242 /* The one we're looking for is probably just before comppad_name_fill. */
243 for (off = AvFILL(comppad_name); off > 0; off--) {
244 if ((sv = svp[off]) &&
247 seq > (I32)SvNVX(sv) &&
248 strEQ(SvPVX(sv), name))
250 return (PADOFFSET)off;
254 /* See if it's in a nested scope */
255 off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix);
267 SV **svp = AvARRAY(comppad_name);
269 if (min_intro_pending && fill < min_intro_pending) {
270 for (off = max_intro_pending; off >= min_intro_pending; off--) {
271 if ((sv = svp[off]) && sv != &sv_undef)
272 warn("%s never introduced", SvPVX(sv));
275 /* "Deintroduce" my variables that are leaving with this scope. */
276 for (off = AvFILL(comppad_name); off > fill; off--) {
277 if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999)
278 SvIVX(sv) = cop_seqmax;
283 pad_alloc(optype,tmptype)
290 if (AvARRAY(comppad) != curpad)
291 croak("panic: pad_alloc");
292 if (pad_reset_pending)
294 if (tmptype & SVs_PADMY) {
296 sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
297 } while (SvPADBUSY(sv)); /* need a fresh one */
298 retval = AvFILL(comppad);
302 sv = *av_fetch(comppad, ++padix, TRUE);
303 } while (SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY));
306 SvFLAGS(sv) |= tmptype;
307 curpad = AvARRAY(comppad);
308 DEBUG_X(fprintf(stderr, "Pad alloc %ld for %s\n", (long) retval, op_name[optype]));
309 return (PADOFFSET)retval;
313 #ifndef CAN_PROTOTYPE
318 #endif /* CAN_PROTOTYPE */
321 croak("panic: pad_sv po");
322 DEBUG_X(fprintf(stderr, "Pad sv %d\n", po));
323 return curpad[po]; /* eventually we'll turn this into a macro */
327 #ifndef CAN_PROTOTYPE
331 pad_free(PADOFFSET po)
332 #endif /* CAN_PROTOTYPE */
336 if (AvARRAY(comppad) != curpad)
337 croak("panic: pad_free curpad");
339 croak("panic: pad_free po");
340 DEBUG_X(fprintf(stderr, "Pad free %d\n", po));
341 if (curpad[po] && curpad[po] != &sv_undef)
342 SvPADTMP_off(curpad[po]);
348 #ifndef CAN_PROTOTYPE
352 pad_swipe(PADOFFSET po)
353 #endif /* CAN_PROTOTYPE */
355 if (AvARRAY(comppad) != curpad)
356 croak("panic: pad_swipe curpad");
358 croak("panic: pad_swipe po");
359 DEBUG_X(fprintf(stderr, "Pad swipe %d\n", po));
360 SvPADTMP_off(curpad[po]);
361 curpad[po] = NEWSV(1107,0);
362 SvPADTMP_on(curpad[po]);
372 if (AvARRAY(comppad) != curpad)
373 croak("panic: pad_reset curpad");
374 DEBUG_X(fprintf(stderr, "Pad reset\n"));
375 if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */
376 for (po = AvMAX(comppad); po > padix_floor; po--) {
377 if (curpad[po] && curpad[po] != &sv_undef)
378 SvPADTMP_off(curpad[po]);
382 pad_reset_pending = FALSE;
391 register OP *kid, *nextkid;
396 if (op->op_flags & OPf_KIDS) {
397 for (kid = cUNOP->op_first; kid; kid = nextkid) {
398 nextkid = kid->op_sibling; /* Get before next freeing kid */
403 switch (op->op_type) {
405 op->op_targ = 0; /* Was holding old type, if any. */
408 op->op_targ = 0; /* Was holding hints. */
412 SvREFCNT_dec(cGVOP->op_gv);
416 SvREFCNT_dec(cCOP->cop_filegv);
419 SvREFCNT_dec(cSVOP->op_sv);
425 if (op->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
429 Safefree(cPVOP->op_pv);
432 op_free(cPMOP->op_pmreplroot);
436 pregfree(cPMOP->op_pmregexp);
437 SvREFCNT_dec(cPMOP->op_pmshort);
444 pad_free(op->op_targ);
453 if (op->op_type != OP_NULL && op->op_targ > 0)
454 pad_free(op->op_targ);
455 op->op_targ = op->op_type;
456 op->op_type = OP_NULL;
457 op->op_ppaddr = ppaddr[OP_NULL];
460 /* Contextualizers */
462 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
473 /* establish postfix order */
474 if (cUNOP->op_first) {
475 op->op_next = LINKLIST(cUNOP->op_first);
476 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
478 kid->op_next = LINKLIST(kid->op_sibling);
494 if (op && op->op_flags & OPf_KIDS) {
495 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
506 op->op_type == OP_SASSIGN && cBINOP->op_first->op_type == OP_CONST) {
507 line_t oldline = curcop->cop_line;
509 if (copline != NOLINE)
510 curcop->cop_line = copline;
511 warn("Found = in conditional, should be ==");
512 curcop->cop_line = oldline;
523 /* assumes no premature commitment */
524 if (!op || (op->op_flags & OPf_KNOW) || error_count)
527 op->op_flags &= ~OPf_LIST;
528 op->op_flags |= OPf_KNOW;
530 switch (op->op_type) {
532 if (op->op_private & OPpREPEAT_DOLIST)
533 null(((LISTOP*)cBINOP->op_first)->op_first);
534 scalar(cBINOP->op_first);
539 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
543 if ((kid = ((LISTOP*)op)->op_first) && kid->op_type == OP_PUSHRE) {
544 if (!kPMOP->op_pmreplroot)
545 deprecate("implicit split to @_");
552 if (op->op_flags & OPf_KIDS) {
553 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
559 scalar(cLISTOP->op_first);
564 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
584 if (!op || error_count)
586 if (op->op_flags & OPf_LIST)
589 op->op_flags |= OPf_KNOW;
591 switch (op->op_type) {
593 if (!(opargs[op->op_type] & OA_FOLDCONST))
595 if (op->op_flags & OPf_STACKED)
669 if (!(op->op_private & OPpLVAL_INTRO))
670 useless = op_name[op->op_type];
677 if (!(op->op_private & OPpLVAL_INTRO) &&
678 (!op->op_sibling || op->op_sibling->op_type != OP_READLINE))
679 useless = "a variable";
684 curcop = ((COP*)op); /* for warning below */
690 useless = "a constant";
691 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
693 else if (SvPOK(sv)) {
694 if (strnEQ(SvPVX(sv), "di", 2) ||
695 strnEQ(SvPVX(sv), "ds", 2) ||
696 strnEQ(SvPVX(sv), "ig", 2))
700 null(op); /* don't execute a constant */
701 SvREFCNT_dec(sv); /* don't even remember it */
705 op->op_type = OP_PREINC; /* pre-increment is faster */
706 op->op_ppaddr = ppaddr[OP_PREINC];
710 op->op_type = OP_PREDEC; /* pre-decrement is faster */
711 op->op_ppaddr = ppaddr[OP_PREDEC];
715 scalarvoid(cBINOP->op_first);
716 useless = op_name[op->op_type];
722 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
726 if (op->op_targ == OP_NEXTSTATE || op->op_targ == OP_DBSTATE)
727 curcop = ((COP*)op); /* for warning below */
728 if (op->op_flags & OPf_STACKED)
733 if (!(op->op_flags & OPf_KIDS))
739 op->op_private |= OPpLEAVE_VOID;
742 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
746 if ((kid = ((LISTOP*)op)->op_first) && kid->op_type == OP_PUSHRE) {
747 if (!kPMOP->op_pmreplroot)
748 deprecate("implicit split to @_");
752 op->op_private |= OPpLEAVE_VOID;
755 if (useless && dowarn)
756 warn("Useless use of %s in void context", useless);
765 if (op && op->op_flags & OPf_KIDS) {
766 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
778 /* assumes no premature commitment */
779 if (!op || (op->op_flags & OPf_KNOW) || error_count)
782 op->op_flags |= (OPf_KNOW | OPf_LIST);
784 switch (op->op_type) {
787 list(cBINOP->op_first);
792 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
799 if (!(op->op_flags & OPf_KIDS))
801 if (!op->op_next && cUNOP->op_first->op_type == OP_FLOP) {
802 list(cBINOP->op_first);
803 return gen_constant_list(op);
810 list(cLISTOP->op_first);
814 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
833 if (op->op_type == OP_LINESEQ ||
834 op->op_type == OP_SCOPE ||
835 op->op_type == OP_LEAVE ||
836 op->op_type == OP_LEAVETRY)
838 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
839 if (kid->op_sibling) {
845 op->op_flags &= ~OPf_PARENS;
846 if (hints & HINT_BLOCK_SCOPE)
847 op->op_flags |= OPf_PARENS;
850 op = newOP(OP_STUB, 0);
860 if (op && op->op_flags & OPf_KIDS) {
861 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
878 if (!op || error_count)
881 switch (op->op_type) {
883 if (!(op->op_private & (OPpCONST_ARYBASE)))
885 if (eval_start && eval_start->op_type == OP_CONST) {
886 compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv);
890 SAVEI32(compiling.cop_arybase);
891 compiling.cop_arybase = 0;
893 else if (type == OP_REFGEN)
896 croak("That use of $[ is unsupported");
899 if ((type == OP_UNDEF || type == OP_REFGEN) &&
900 !(op->op_flags & OPf_STACKED)) {
901 op->op_type = OP_RV2CV; /* entersub => rv2cv */
902 op->op_ppaddr = ppaddr[OP_RV2CV];
903 assert(cUNOP->op_first->op_type == OP_NULL);
904 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
910 /* grep, foreach, subcalls, refgen */
911 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
913 sprintf(tokenbuf, "Can't modify %s in %s",
914 op_name[op->op_type],
915 type ? op_name[type] : "local");
939 if (!(op->op_flags & OPf_STACKED))
945 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
951 if (type == OP_REFGEN && op->op_flags & OPf_PARENS) {
953 return op; /* Treat \(@foo) like ordinary list. */
957 ref(cUNOP->op_first, op->op_type);
969 if (!type && cUNOP->op_first->op_type != OP_GV)
970 croak("Can't localize a reference");
971 ref(cUNOP->op_first, op->op_type);
988 croak("Can't localize lexical variable %s",
989 SvPV(*av_fetch(comppad_name, op->op_targ, 4), na));
1004 pad_free(op->op_targ);
1005 op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
1006 sv = PAD_SV(op->op_targ);
1007 sv_upgrade(sv, SVt_PVLV);
1008 sv_magic(sv, Nullsv, mtype, Nullch, 0);
1009 curpad[op->op_targ] = sv;
1010 if (op->op_flags & OPf_KIDS)
1011 mod(cBINOP->op_first->op_sibling, type);
1016 ref(cBINOP->op_first, op->op_type);
1023 if (op->op_flags & OPf_KIDS)
1024 mod(cLISTOP->op_last, type);
1028 if (!(op->op_flags & OPf_KIDS))
1030 if (op->op_targ != OP_LIST) {
1031 mod(cBINOP->op_first, type);
1036 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1040 op->op_flags |= OPf_MOD;
1042 if (type == OP_AASSIGN || type == OP_SASSIGN)
1043 op->op_flags |= OPf_SPECIAL|OPf_REF;
1045 op->op_private |= OPpLVAL_INTRO;
1046 op->op_flags &= ~OPf_SPECIAL;
1048 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1049 op->op_flags |= OPf_REF;
1059 if (op && op->op_flags & OPf_KIDS) {
1060 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1073 if (!op || error_count)
1076 switch (op->op_type) {
1078 if ((type == OP_DEFINED) &&
1079 !(op->op_flags & OPf_STACKED)) {
1080 op->op_type = OP_RV2CV; /* entersub => rv2cv */
1081 op->op_ppaddr = ppaddr[OP_RV2CV];
1082 assert(cUNOP->op_first->op_type == OP_NULL);
1083 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
1084 op->op_flags |= OPf_SPECIAL;
1089 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
1093 ref(cUNOP->op_first, op->op_type);
1096 if (type == OP_RV2AV || type == OP_RV2HV) {
1097 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
1098 op->op_flags |= OPf_MOD;
1104 op->op_flags |= OPf_REF;
1107 ref(cUNOP->op_first, op->op_type);
1112 op->op_flags |= OPf_REF;
1117 if (!(op->op_flags & OPf_KIDS))
1119 ref(cBINOP->op_first, type);
1123 ref(cBINOP->op_first, op->op_type);
1124 if (type == OP_RV2AV || type == OP_RV2HV) {
1125 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
1126 op->op_flags |= OPf_MOD;
1134 if (!(op->op_flags & OPf_KIDS))
1136 ref(cLISTOP->op_last, type);
1152 if (!op || error_count)
1156 if (type == OP_LIST) {
1157 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1160 else if (type != OP_PADSV &&
1163 type != OP_PUSHMARK)
1165 sprintf(tokenbuf, "Can't declare %s in my", op_name[op->op_type]);
1169 op->op_flags |= OPf_MOD;
1170 op->op_private |= OPpLVAL_INTRO;
1179 o->op_flags |= OPf_PARENS;
1184 bind_match(type, left, right)
1191 if (right->op_type == OP_MATCH ||
1192 right->op_type == OP_SUBST ||
1193 right->op_type == OP_TRANS) {
1194 right->op_flags |= OPf_STACKED;
1195 if (right->op_type != OP_MATCH)
1196 left = mod(left, right->op_type);
1197 if (right->op_type == OP_TRANS)
1198 op = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1200 op = prepend_elem(right->op_type, scalar(left), right);
1202 return newUNOP(OP_NOT, 0, scalar(op));
1206 return bind_match(type, left,
1207 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1216 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1217 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
1225 if (o->op_flags & OPf_PARENS || perldb || tainting) {
1226 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1227 o->op_type = OP_LEAVE;
1228 o->op_ppaddr = ppaddr[OP_LEAVE];
1231 if (o->op_type == OP_LINESEQ) {
1233 o->op_type = OP_SCOPE;
1234 o->op_ppaddr = ppaddr[OP_SCOPE];
1235 kid = ((LISTOP*)o)->op_first;
1236 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1237 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1242 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1251 int retval = savestack_ix;
1252 comppad_name_fill = AvFILL(comppad_name);
1253 SAVEINT(min_intro_pending);
1254 SAVEINT(max_intro_pending);
1255 min_intro_pending = 0;
1256 SAVEINT(comppad_name_fill);
1257 SAVEINT(padix_floor);
1258 padix_floor = padix;
1259 pad_reset_pending = FALSE;
1261 hints &= ~HINT_BLOCK_SCOPE;
1266 block_end(line, floor, seq)
1271 int needblockscope = hints & HINT_BLOCK_SCOPE;
1272 OP* retval = scalarseq(seq);
1273 if (copline > (line_t)line)
1276 pad_reset_pending = FALSE;
1278 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1279 pad_leavemy(comppad_name_fill);
1288 eval_root = newUNOP(OP_LEAVEEVAL, 0, op);
1289 eval_start = linklist(eval_root);
1290 eval_root->op_next = 0;
1298 main_root = scope(sawparens(scalarvoid(op)));
1299 curcop = &compiling;
1300 main_start = LINKLIST(main_root);
1301 main_root->op_next = 0;
1313 if (o->op_flags & OPf_PARENS)
1317 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1319 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1320 if (*s == ';' || *s == '=')
1321 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1328 return mod(o, OP_NULL); /* a bit kludgey */
1335 if (o->op_type == OP_LIST) {
1336 o = convert(OP_JOIN, 0,
1337 prepend_elem(OP_LIST,
1338 newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1349 I32 type = o->op_type;
1352 if (opargs[type] & OA_RETSCALAR)
1354 if (opargs[type] & OA_TARGET)
1355 o->op_targ = pad_alloc(type, SVs_PADTMP);
1357 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1358 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1360 if (!(opargs[type] & OA_FOLDCONST))
1364 goto nope; /* Don't try to run w/ errors */
1366 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1367 if (curop->op_type != OP_CONST &&
1368 curop->op_type != OP_LIST &&
1369 curop->op_type != OP_SCALAR &&
1370 curop->op_type != OP_NULL &&
1371 curop->op_type != OP_PUSHMARK) {
1376 curop = LINKLIST(o);
1381 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1382 pad_swipe(o->op_targ);
1383 else if (SvTEMP(sv)) { /* grab mortal temp? */
1384 (void)SvREFCNT_inc(sv);
1388 if (type == OP_RV2GV)
1389 return newGVOP(OP_GV, 0, sv);
1391 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1393 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1398 return newSVOP(OP_CONST, 0, sv);
1402 if (!(opargs[type] & OA_OTHERINT))
1405 if (!(hints & HINT_INTEGER)) {
1406 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1409 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1410 if (curop->op_type == OP_CONST) {
1411 if (SvIOK(((SVOP*)curop)->op_sv))
1415 if (opargs[curop->op_type] & OA_RETINTEGER)
1419 o->op_ppaddr = ppaddr[++(o->op_type)];
1426 gen_constant_list(o)
1430 I32 oldtmps_floor = tmps_floor;
1434 return o; /* Don't attempt to run with errors */
1436 op = curop = LINKLIST(o);
1442 tmps_floor = oldtmps_floor;
1444 o->op_type = OP_RV2AV;
1445 o->op_ppaddr = ppaddr[OP_RV2AV];
1446 curop = ((UNOP*)o)->op_first;
1447 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1454 convert(type, flags, op)
1462 if (!op || op->op_type != OP_LIST)
1463 op = newLISTOP(OP_LIST, 0, op, Nullop);
1465 op->op_flags &= ~(OPf_KNOW|OPf_LIST);
1467 if (!(opargs[type] & OA_MARK))
1468 null(cLISTOP->op_first);
1471 op->op_ppaddr = ppaddr[type];
1472 op->op_flags |= flags;
1474 op = CHECKOP(type, op);
1475 if (op->op_type != type)
1478 if (cLISTOP->op_children < 7) {
1479 /* XXX do we really need to do this if we're done appending?? */
1480 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1482 cLISTOP->op_last = last; /* in case check substituted last arg */
1485 return fold_constants(op);
1488 /* List constructors */
1491 append_elem(type, first, last)
1502 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1503 return newLISTOP(type, 0, first, last);
1505 if (first->op_flags & OPf_KIDS)
1506 ((LISTOP*)first)->op_last->op_sibling = last;
1508 first->op_flags |= OPf_KIDS;
1509 ((LISTOP*)first)->op_first = last;
1511 ((LISTOP*)first)->op_last = last;
1512 ((LISTOP*)first)->op_children++;
1517 append_list(type, first, last)
1528 if (first->op_type != type)
1529 return prepend_elem(type, (OP*)first, (OP*)last);
1531 if (last->op_type != type)
1532 return append_elem(type, (OP*)first, (OP*)last);
1534 first->op_last->op_sibling = last->op_first;
1535 first->op_last = last->op_last;
1536 first->op_children += last->op_children;
1537 if (first->op_children)
1538 last->op_flags |= OPf_KIDS;
1545 prepend_elem(type, first, last)
1556 if (last->op_type == type) {
1557 if (type == OP_LIST) { /* already a PUSHMARK there */
1558 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1559 ((LISTOP*)last)->op_first->op_sibling = first;
1562 if (!(last->op_flags & OPf_KIDS)) {
1563 ((LISTOP*)last)->op_last = first;
1564 last->op_flags |= OPf_KIDS;
1566 first->op_sibling = ((LISTOP*)last)->op_first;
1567 ((LISTOP*)last)->op_first = first;
1569 ((LISTOP*)last)->op_children++;
1573 return newLISTOP(type, 0, first, last);
1581 return newOP(OP_STUB, 0);
1588 if (!op || op->op_type != OP_LIST)
1589 op = newLISTOP(OP_LIST, 0, op, Nullop);
1595 newLISTOP(type, flags, first, last)
1603 Newz(1101, listop, 1, LISTOP);
1605 listop->op_type = type;
1606 listop->op_ppaddr = ppaddr[type];
1607 listop->op_children = (first != 0) + (last != 0);
1608 listop->op_flags = flags;
1612 else if (!first && last)
1615 first->op_sibling = last;
1616 listop->op_first = first;
1617 listop->op_last = last;
1618 if (type == OP_LIST) {
1620 pushop = newOP(OP_PUSHMARK, 0);
1621 pushop->op_sibling = first;
1622 listop->op_first = pushop;
1623 listop->op_flags |= OPf_KIDS;
1625 listop->op_last = pushop;
1627 else if (listop->op_children)
1628 listop->op_flags |= OPf_KIDS;
1639 Newz(1101, op, 1, OP);
1641 op->op_ppaddr = ppaddr[type];
1642 op->op_flags = flags;
1645 /* op->op_private = 0; */
1646 if (opargs[type] & OA_RETSCALAR)
1648 if (opargs[type] & OA_TARGET)
1649 op->op_targ = pad_alloc(type, SVs_PADTMP);
1650 return CHECKOP(type, op);
1654 newUNOP(type, flags, first)
1662 first = newOP(OP_STUB, 0);
1663 if (opargs[type] & OA_MARK)
1664 first = force_list(first);
1666 Newz(1101, unop, 1, UNOP);
1667 unop->op_type = type;
1668 unop->op_ppaddr = ppaddr[type];
1669 unop->op_first = first;
1670 unop->op_flags = flags | OPf_KIDS;
1671 unop->op_private = 1;
1673 unop = (UNOP*) CHECKOP(type, unop);
1677 return fold_constants((OP *) unop);
1681 newBINOP(type, flags, first, last)
1688 Newz(1101, binop, 1, BINOP);
1691 first = newOP(OP_NULL, 0);
1693 binop->op_type = type;
1694 binop->op_ppaddr = ppaddr[type];
1695 binop->op_first = first;
1696 binop->op_flags = flags | OPf_KIDS;
1699 binop->op_private = 1;
1702 binop->op_private = 2;
1703 first->op_sibling = last;
1706 binop = (BINOP*)CHECKOP(type, binop);
1710 binop->op_last = last = binop->op_first->op_sibling;
1712 return fold_constants((OP *)binop);
1716 pmtrans(op, expr, repl)
1721 SV *tstr = ((SVOP*)expr)->op_sv;
1722 SV *rstr = ((SVOP*)repl)->op_sv;
1725 register U8 *t = (U8*)SvPV(tstr, tlen);
1726 register U8 *r = (U8*)SvPV(rstr, rlen);
1731 register short *tbl;
1733 tbl = (short*)cPVOP->op_pv;
1734 complement = op->op_private & OPpTRANS_COMPLEMENT;
1735 delete = op->op_private & OPpTRANS_DELETE;
1736 /* squash = op->op_private & OPpTRANS_SQUASH; */
1739 Zero(tbl, 256, short);
1740 for (i = 0; i < tlen; i++)
1742 for (i = 0, j = 0; i < 256; i++) {
1758 if (!rlen && !delete) {
1761 for (i = 0; i < 256; i++)
1763 for (i = 0, j = 0; i < tlen; i++,j++) {
1766 if (tbl[t[i]] == -1)
1772 if (tbl[t[i]] == -1)
1783 newPMOP(type, flags)
1789 Newz(1101, pmop, 1, PMOP);
1790 pmop->op_type = type;
1791 pmop->op_ppaddr = ppaddr[type];
1792 pmop->op_flags = flags;
1793 pmop->op_private = 0;
1795 /* link into pm list */
1796 if (type != OP_TRANS && curstash) {
1797 pmop->op_pmnext = HvPMROOT(curstash);
1798 HvPMROOT(curstash) = pmop;
1805 pmruntime(op, expr, repl)
1813 if (op->op_type == OP_TRANS)
1814 return pmtrans(op, expr, repl);
1818 if (expr->op_type == OP_CONST) {
1820 SV *pat = ((SVOP*)expr)->op_sv;
1821 char *p = SvPV(pat, plen);
1822 if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
1823 sv_setpvn(pat, "\\s+", 3);
1824 p = SvPV(pat, plen);
1825 pm->op_pmflags |= PMf_SKIPWHITE;
1827 pm->op_pmregexp = pregcomp(p, p + plen, pm);
1828 if (strEQ("\\s+", pm->op_pmregexp->precomp))
1829 pm->op_pmflags |= PMf_WHITE;
1834 if (pm->op_pmflags & PMf_KEEP)
1835 expr = newUNOP(OP_REGCMAYBE,0,expr);
1837 Newz(1101, rcop, 1, LOGOP);
1838 rcop->op_type = OP_REGCOMP;
1839 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1840 rcop->op_first = scalar(expr);
1841 rcop->op_flags |= OPf_KIDS;
1842 rcop->op_private = 1;
1843 rcop->op_other = op;
1845 /* establish postfix order */
1846 if (pm->op_pmflags & PMf_KEEP) {
1848 rcop->op_next = expr;
1849 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1852 rcop->op_next = LINKLIST(expr);
1853 expr->op_next = (OP*)rcop;
1856 prepend_elem(op->op_type, scalar((OP*)rcop), op);
1861 if (pm->op_pmflags & PMf_EVAL)
1863 else if (repl->op_type == OP_CONST)
1867 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1868 if (opargs[curop->op_type] & OA_DANGEROUS) {
1869 if (curop->op_type == OP_GV) {
1870 GV *gv = ((GVOP*)curop)->op_gv;
1871 if (strchr("&`'123456789+", *GvENAME(gv)))
1874 else if (curop->op_type == OP_RV2CV)
1876 else if (curop->op_type == OP_RV2SV ||
1877 curop->op_type == OP_RV2AV ||
1878 curop->op_type == OP_RV2HV ||
1879 curop->op_type == OP_RV2GV) {
1880 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1883 else if (curop->op_type == OP_PADSV ||
1884 curop->op_type == OP_PADAV ||
1885 curop->op_type == OP_PADHV ||
1886 curop->op_type == OP_PADANY) {
1895 if (curop == repl) {
1896 pm->op_pmflags |= PMf_CONST; /* const for long enough */
1897 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
1898 prepend_elem(op->op_type, scalar(repl), op);
1901 Newz(1101, rcop, 1, LOGOP);
1902 rcop->op_type = OP_SUBSTCONT;
1903 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1904 rcop->op_first = scalar(repl);
1905 rcop->op_flags |= OPf_KIDS;
1906 rcop->op_private = 1;
1907 rcop->op_other = op;
1909 /* establish postfix order */
1910 rcop->op_next = LINKLIST(repl);
1911 repl->op_next = (OP*)rcop;
1913 pm->op_pmreplroot = scalar((OP*)rcop);
1914 pm->op_pmreplstart = LINKLIST(rcop);
1923 newSVOP(type, flags, sv)
1929 Newz(1101, svop, 1, SVOP);
1930 svop->op_type = type;
1931 svop->op_ppaddr = ppaddr[type];
1933 svop->op_next = (OP*)svop;
1934 svop->op_flags = flags;
1935 if (opargs[type] & OA_RETSCALAR)
1937 if (opargs[type] & OA_TARGET)
1938 svop->op_targ = pad_alloc(type, SVs_PADTMP);
1939 return CHECKOP(type, svop);
1943 newGVOP(type, flags, gv)
1949 Newz(1101, gvop, 1, GVOP);
1950 gvop->op_type = type;
1951 gvop->op_ppaddr = ppaddr[type];
1952 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
1953 gvop->op_next = (OP*)gvop;
1954 gvop->op_flags = flags;
1955 if (opargs[type] & OA_RETSCALAR)
1957 if (opargs[type] & OA_TARGET)
1958 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
1959 return CHECKOP(type, gvop);
1963 newPVOP(type, flags, pv)
1969 Newz(1101, pvop, 1, PVOP);
1970 pvop->op_type = type;
1971 pvop->op_ppaddr = ppaddr[type];
1973 pvop->op_next = (OP*)pvop;
1974 pvop->op_flags = flags;
1975 if (opargs[type] & OA_RETSCALAR)
1977 if (opargs[type] & OA_TARGET)
1978 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
1979 return CHECKOP(type, pvop);
1983 newCVOP(type, flags, cv, cont)
1990 Newz(1101, cvop, 1, CVOP);
1991 cvop->op_type = type;
1992 cvop->op_ppaddr = ppaddr[type];
1994 cvop->op_cont = cont;
1995 cvop->op_next = (OP*)cvop;
1996 cvop->op_flags = flags;
1997 if (opargs[type] & OA_RETSCALAR)
1999 if (opargs[type] & OA_TARGET)
2000 cvop->op_targ = pad_alloc(type, SVs_PADTMP);
2001 return CHECKOP(type, cvop);
2010 save_hptr(&curstash);
2011 save_item(curstname);
2016 name = SvPV(sv, len);
2017 curstash = gv_stashpv(name,TRUE);
2018 sv_setpvn(curstname, name, len);
2022 sv_setpv(curstname,"<none>");
2030 utilize(aver, id, arg)
2040 if (id->op_type != OP_CONST)
2041 croak("Module name must be constant");
2043 /* Fake up an import/unimport */
2044 if (arg && arg->op_type == OP_STUB)
2045 imop = arg; /* no import on explicit () */
2047 /* Make copy of id so we don't free it twice */
2048 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2050 meth = newSVOP(OP_CONST, 0,
2052 ? newSVpv("import", 6)
2053 : newSVpv("unimport", 8)
2055 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2056 append_elem(OP_LIST,
2057 prepend_elem(OP_LIST, pack, list(arg)),
2058 newUNOP(OP_METHOD, 0, meth)));
2061 /* Fake up a require */
2062 rqop = newUNOP(OP_REQUIRE, 0, id);
2064 /* Fake up the BEGIN {}, which does its thing immediately. */
2065 newSUB(start_subparse(),
2066 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2068 append_elem(OP_LINESEQ,
2069 newSTATEOP(0, Nullch, rqop),
2070 newSTATEOP(0, Nullch, imop) ));
2077 newSLICEOP(flags, subscript, listval)
2082 return newBINOP(OP_LSLICE, flags,
2083 list(force_list(subscript)),
2084 list(force_list(listval)) );
2094 if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
2095 op = cUNOP->op_first;
2097 if (op->op_type == OP_COND_EXPR) {
2098 I32 t = list_assignment(cCONDOP->op_first->op_sibling);
2099 I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
2104 yyerror("Assignment to both a list and a scalar");
2108 if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
2109 op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
2110 op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
2113 if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
2116 if (op->op_type == OP_RV2SV)
2123 newASSIGNOP(flags, left, optype, right)
2132 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2133 return newLOGOP(optype, 0,
2134 mod(scalar(left), optype),
2135 newUNOP(OP_SASSIGN, 0, scalar(right)));
2138 return newBINOP(optype, OPf_STACKED,
2139 mod(scalar(left), optype), scalar(right));
2143 if (list_assignment(left)) {
2145 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2146 left = mod(left, OP_AASSIGN);
2154 if (right && right->op_type == OP_SPLIT) {
2155 if ((op = ((LISTOP*)right)->op_first) && op->op_type == OP_PUSHRE) {
2156 PMOP *pm = (PMOP*)op;
2157 if (left->op_type == OP_RV2AV &&
2158 !(left->op_private & OPpLVAL_INTRO) )
2160 op = ((UNOP*)left)->op_first;
2161 if (op->op_type == OP_GV && !pm->op_pmreplroot) {
2162 pm->op_pmreplroot = (OP*)((GVOP*)op)->op_gv;
2163 pm->op_pmflags |= PMf_ONCE;
2169 if (modcount < 10000) {
2170 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2172 sv_setiv(sv, modcount+1);
2177 op = newBINOP(OP_AASSIGN, flags,
2178 list(force_list(right)),
2179 list(force_list(left)) );
2181 if (!(left->op_private & OPpLVAL_INTRO)) {
2182 static int generation = 100;
2186 for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
2187 if (opargs[curop->op_type] & OA_DANGEROUS) {
2188 if (curop->op_type == OP_GV) {
2189 GV *gv = ((GVOP*)curop)->op_gv;
2190 if (gv == defgv || SvCUR(gv) == generation)
2192 SvCUR(gv) = generation;
2194 else if (curop->op_type == OP_PADSV ||
2195 curop->op_type == OP_PADAV ||
2196 curop->op_type == OP_PADHV ||
2197 curop->op_type == OP_PADANY) {
2198 SV **svp = AvARRAY(comppad_name);
2199 SV *sv = svp[curop->op_targ];
2200 if (SvCUR(sv) == generation)
2202 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2204 else if (curop->op_type == OP_RV2CV)
2206 else if (curop->op_type == OP_RV2SV ||
2207 curop->op_type == OP_RV2AV ||
2208 curop->op_type == OP_RV2HV ||
2209 curop->op_type == OP_RV2GV) {
2210 if (lastop->op_type != OP_GV) /* funny deref? */
2219 op->op_private = OPpASSIGN_COMMON;
2224 right = newOP(OP_UNDEF, 0);
2225 if (right->op_type == OP_READLINE) {
2226 right->op_flags |= OPf_STACKED;
2227 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2230 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2231 op = newBINOP(OP_SASSIGN, flags,
2232 scalar(right), mod(scalar(left), OP_SASSIGN) );
2244 newSTATEOP(flags, label, op)
2251 /* Introduce my variables. */
2252 if (min_intro_pending) {
2253 SV **svp = AvARRAY(comppad_name);
2256 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2257 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2258 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2259 SvNVX(sv) = (double)cop_seqmax;
2262 min_intro_pending = 0;
2263 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2266 Newz(1101, cop, 1, COP);
2267 if (perldb && curcop->cop_line && curstash != debstash) {
2268 cop->op_type = OP_DBSTATE;
2269 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2272 cop->op_type = OP_NEXTSTATE;
2273 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2275 cop->op_flags = flags;
2276 cop->op_private = 0;
2277 cop->op_next = (OP*)cop;
2280 cop->cop_label = label;
2281 hints |= HINT_BLOCK_SCOPE;
2283 cop->cop_seq = cop_seqmax++;
2284 cop->cop_arybase = curcop->cop_arybase;
2286 if (copline == NOLINE)
2287 cop->cop_line = curcop->cop_line;
2289 cop->cop_line = copline;
2292 cop->cop_filegv = SvREFCNT_inc(curcop->cop_filegv);
2293 cop->cop_stash = curstash;
2295 if (perldb && curstash != debstash) {
2296 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2297 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2299 (void)SvIOK_on(*svp);
2300 SvSTASH(*svp) = (HV*)cop;
2304 return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2308 newLOGOP(type, flags, first, other)
2317 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2318 return newBINOP(type, flags, scalar(first), scalar(other));
2320 scalarboolean(first);
2321 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2322 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2323 if (type == OP_AND || type == OP_OR) {
2329 first = cUNOP->op_first;
2331 first->op_next = op->op_next;
2332 cUNOP->op_first = Nullop;
2336 if (first->op_type == OP_CONST) {
2337 if (dowarn && (first->op_private & OPpCONST_BARE))
2338 warn("Probable precedence problem on %s", op_name[type]);
2339 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2348 else if (first->op_type == OP_WANTARRAY) {
2358 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2359 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2361 Newz(1101, logop, 1, LOGOP);
2363 logop->op_type = type;
2364 logop->op_ppaddr = ppaddr[type];
2365 logop->op_first = first;
2366 logop->op_flags = flags | OPf_KIDS;
2367 logop->op_other = LINKLIST(other);
2368 logop->op_private = 1;
2370 /* establish postfix order */
2371 logop->op_next = LINKLIST(first);
2372 first->op_next = (OP*)logop;
2373 first->op_sibling = other;
2375 op = newUNOP(OP_NULL, 0, (OP*)logop);
2376 other->op_next = op;
2382 newCONDOP(flags, first, true, false)
2392 return newLOGOP(OP_AND, 0, first, true);
2394 return newLOGOP(OP_OR, 0, first, false);
2396 scalarboolean(first);
2397 if (first->op_type == OP_CONST) {
2398 if (SvTRUE(((SVOP*)first)->op_sv)) {
2409 else if (first->op_type == OP_WANTARRAY) {
2413 Newz(1101, condop, 1, CONDOP);
2415 condop->op_type = OP_COND_EXPR;
2416 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2417 condop->op_first = first;
2418 condop->op_flags = flags | OPf_KIDS;
2419 condop->op_true = LINKLIST(true);
2420 condop->op_false = LINKLIST(false);
2421 condop->op_private = 1;
2423 /* establish postfix order */
2424 condop->op_next = LINKLIST(first);
2425 first->op_next = (OP*)condop;
2427 first->op_sibling = true;
2428 true->op_sibling = false;
2429 op = newUNOP(OP_NULL, 0, (OP*)condop);
2432 false->op_next = op;
2438 newRANGE(flags, left, right)
2448 Newz(1101, condop, 1, CONDOP);
2450 condop->op_type = OP_RANGE;
2451 condop->op_ppaddr = ppaddr[OP_RANGE];
2452 condop->op_first = left;
2453 condop->op_flags = OPf_KIDS;
2454 condop->op_true = LINKLIST(left);
2455 condop->op_false = LINKLIST(right);
2456 condop->op_private = 1;
2458 left->op_sibling = right;
2460 condop->op_next = (OP*)condop;
2461 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2462 flop = newUNOP(OP_FLOP, 0, flip);
2463 op = newUNOP(OP_NULL, 0, flop);
2466 left->op_next = flip;
2467 right->op_next = flop;
2469 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2470 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2471 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2472 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2474 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2475 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2478 if (!flip->op_private || !flop->op_private)
2479 linklist(op); /* blow off optimizer unless constant */
2485 newLOOPOP(flags, debuggable, expr, block)
2493 int once = block && block->op_flags & OPf_SPECIAL &&
2494 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2497 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2498 return block; /* do {} while 0 does once */
2499 else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
2500 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr);
2503 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2504 op = newLOGOP(OP_AND, 0, expr, listop);
2506 ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2508 if (once && op != listop)
2509 op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2512 op = newUNOP(OP_NULL, 0, op); /* or do {} while 1 loses outer block */
2514 op->op_flags |= flags;
2516 op->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2521 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2535 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2536 expr = newUNOP(OP_DEFINED, 0,
2537 newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2541 block = newOP(OP_NULL, 0);
2544 next = LINKLIST(cont);
2546 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2548 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2549 redo = LINKLIST(listop);
2552 op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2553 if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2554 op_free(expr); /* oops, it's a while (0) */
2556 return Nullop; /* (listop already freed by newLOGOP) */
2558 ((LISTOP*)listop)->op_last->op_next = condop =
2559 (op == listop ? redo : LINKLIST(op));
2567 Newz(1101,loop,1,LOOP);
2568 loop->op_type = OP_ENTERLOOP;
2569 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2570 loop->op_private = 0;
2571 loop->op_next = (OP*)loop;
2574 op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2576 loop->op_redoop = redo;
2577 loop->op_lastop = op;
2580 loop->op_nextop = next;
2582 loop->op_nextop = op;
2584 op->op_flags |= flags;
2589 #ifndef CAN_PROTOTYPE
2590 newFOROP(flags,label,forline,sv,expr,block,cont)
2599 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2600 #endif /* CAN_PROTOTYPE */
2608 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2609 sv->op_type = OP_RV2GV;
2610 sv->op_ppaddr = ppaddr[OP_RV2GV];
2612 else if (sv->op_type == OP_PADSV) { /* private variable */
2613 padoff = sv->op_targ;
2618 croak("Can't use %s for loop variable", op_name[sv->op_type]);
2621 sv = newGVOP(OP_GV, 0, defgv);
2623 if (expr->op_type == OP_RV2AV) {
2624 expr = scalar(ref(expr, OP_ITER));
2625 iterflags |= OPf_STACKED;
2627 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2628 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2630 assert(!loop->op_next);
2631 Renew(loop, 1, LOOP);
2632 loop->op_targ = padoff;
2633 return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2634 newOP(OP_ITER, 0), block, cont));
2638 newLOOPEX(type, label)
2643 if (type != OP_GOTO || label->op_type == OP_CONST) {
2644 op = newPVOP(type, 0, savepv(
2645 label->op_type == OP_CONST
2646 ? SvPVx(((SVOP*)label)->op_sv, na)
2651 if (label->op_type == OP_ENTERSUB)
2652 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2653 op = newUNOP(type, OPf_STACKED, label);
2655 hints |= HINT_BLOCK_SCOPE;
2663 if (!CvXSUB(cv) && CvROOT(cv)) {
2665 croak("Can't undef active subroutine");
2671 if (!(SvFLAGS(cv) & SVpcv_CLONED))
2672 op_free(CvROOT(cv));
2673 CvROOT(cv) = Nullop;
2676 SvREFCNT_dec(CvGV(cv));
2678 SvREFCNT_dec(CvOUTSIDE(cv));
2679 CvOUTSIDE(cv) = Nullcv;
2680 if (CvPADLIST(cv)) {
2681 I32 i = AvFILL(CvPADLIST(cv));
2683 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2687 SvREFCNT_dec((SV*)CvPADLIST(cv));
2688 CvPADLIST(cv) = Nullav;
2698 AV* protopadlist = CvPADLIST(proto);
2699 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
2700 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
2701 SV** svp = AvARRAY(protopad);
2710 cv = compcv = (CV*)NEWSV(1104,0);
2711 sv_upgrade((SV *)cv, SVt_PVCV);
2712 SvFLAGS(cv) |= SVpcv_CLONED;
2714 CvFILEGV(cv) = CvFILEGV(proto);
2715 CvGV(cv) = SvREFCNT_inc(CvGV(proto));
2716 CvSTASH(cv) = CvSTASH(proto);
2717 CvROOT(cv) = CvROOT(proto);
2718 CvSTART(cv) = CvSTART(proto);
2719 if (CvOUTSIDE(proto))
2720 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc((SV*)CvOUTSIDE(proto));
2724 comppadlist = newAV();
2725 AvREAL_off(comppadlist);
2726 av_store(comppadlist, 0, SvREFCNT_inc((SV*)protopad_name));
2727 av_store(comppadlist, 1, (SV*)comppad);
2728 CvPADLIST(cv) = comppadlist;
2729 av_extend(comppad, AvFILL(protopad));
2730 curpad = AvARRAY(comppad);
2732 av = newAV(); /* will be @_ */
2734 av_store(comppad, 0, (SV*)av);
2735 AvFLAGS(av) = AVf_REIFY;
2737 svp = AvARRAY(protopad_name);
2738 for ( ix = AvFILL(protopad); ix > 0; ix--) {
2740 if (svp[ix] != &sv_undef) {
2741 char *name = SvPVX(svp[ix]); /* XXX */
2742 if (SvFLAGS(svp[ix]) & SVf_FAKE) { /* lexical from outside? */
2743 I32 off = pad_findlex(name,ix,curcop->cop_seq, CvOUTSIDE(proto),
2746 croak("panic: cv_clone: %s", name);
2748 else { /* our own lexical */
2750 av_store(comppad, ix, sv = (SV*)newAV());
2751 else if (*name == '%')
2752 av_store(comppad, ix, sv = (SV*)newHV());
2754 av_store(comppad, ix, sv = NEWSV(0,0));
2759 av_store(comppad, ix, sv = NEWSV(0,0));
2769 newSUB(floor,op,proto,block)
2776 char *name = op ? SvPVx(cSVOP->op_sv, na) : "__ANON__";
2777 GV *gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV);
2784 if (cv = GvCV(gv)) {
2786 cv = 0; /* just a cached method */
2787 else if (CvROOT(cv) || CvXSUB(cv) || GvFLAGS(gv) & GVf_IMPORTED) {
2788 if (dowarn) { /* already defined (or promised)? */
2789 line_t oldline = curcop->cop_line;
2791 curcop->cop_line = copline;
2792 warn("Subroutine %s redefined",name);
2793 curcop->cop_line = oldline;
2799 if (cv) { /* must reuse cv if autoloaded */
2801 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
2802 CvOUTSIDE(compcv) = 0;
2803 CvPADLIST(cv) = CvPADLIST(compcv);
2804 CvPADLIST(compcv) = 0;
2805 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
2806 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
2807 SvREFCNT_dec(compcv);
2814 CvFILEGV(cv) = curcop->cop_filegv;
2815 CvGV(cv) = SvREFCNT_inc(gv);
2816 CvSTASH(cv) = curstash;
2819 char *p = SvPVx(((SVOP*)proto)->op_sv, na);
2820 if (SvPOK(cv) && strNE(SvPV((SV*)cv,na), p))
2821 warn("Prototype mismatch: (%s) vs (%s)", SvPV((SV*)cv, na), p);
2822 sv_setpv((SV*)cv, p);
2833 av = newAV(); /* Will be @_ */
2835 av_store(comppad, 0, (SV*)av);
2836 AvFLAGS(av) = AVf_REIFY;
2838 for (ix = AvFILL(comppad); ix > 0; ix--) {
2839 if (!SvPADMY(curpad[ix]))
2840 SvPADTMP_on(curpad[ix]);
2843 if (AvFILL(comppad_name) < AvFILL(comppad))
2844 av_store(comppad_name, AvFILL(comppad), Nullsv);
2846 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
2847 CvSTART(cv) = LINKLIST(CvROOT(cv));
2848 CvROOT(cv)->op_next = 0;
2850 if (s = strrchr(name,':'))
2854 if (strEQ(s, "BEGIN") && !error_count) {
2855 line_t oldline = compiling.cop_line;
2858 SAVESPTR(compiling.cop_filegv);
2862 av_push(beginav, (SV *)cv);
2863 DEBUG_x( dump_sub(gv) );
2867 rspara = (nrslen == 2);
2874 curcop = &compiling;
2875 curcop->cop_line = oldline; /* might have recursed to yylex */
2878 else if (strEQ(s, "END") && !error_count) {
2881 av_unshift(endav, 1);
2882 av_store(endav, 0, SvREFCNT_inc(cv));
2884 if (perldb && curstash != debstash) {
2886 SV *tmpstr = sv_newmortal();
2888 sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), (long)subline);
2889 sv = newSVpv(buf,0);
2891 sprintf(buf,"%ld",(long)curcop->cop_line);
2893 gv_efullname(tmpstr,gv);
2894 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
2900 GvCV(gv) = 0; /* Will remember in SVOP instead. */
2901 SvFLAGS(cv) |= SVpcv_ANON;
2908 newXSUB(name, ix, subaddr, filename)
2914 CV* cv = newXS(name, (void(*)())subaddr, filename);
2915 CvOLDSTYLE(cv) = TRUE;
2916 CvXSUBANY(cv).any_i32 = ix;
2922 newXS(name, subaddr, filename)
2924 void (*subaddr) _((CV*));
2928 GV *gv = gv_fetchpv((name ? name : "__ANON__"), GV_ADDMULTI, SVt_PVCV);
2933 if (cv = GvCV(gv)) {
2935 cv = 0; /* just a cached method */
2936 else if (CvROOT(cv) || CvXSUB(cv)) { /* already defined? */
2938 line_t oldline = curcop->cop_line;
2940 curcop->cop_line = copline;
2941 warn("Subroutine %s redefined",name);
2942 curcop->cop_line = oldline;
2948 if (cv) { /* must reuse cv if autoloaded */
2949 assert(SvREFCNT(CvGV(cv)) > 1);
2950 SvREFCNT_dec(CvGV(cv));
2953 cv = (CV*)NEWSV(1105,0);
2954 sv_upgrade((SV *)cv, SVt_PVCV);
2957 CvGV(cv) = SvREFCNT_inc(gv);
2959 CvFILEGV(cv) = gv_fetchfile(filename);
2960 CvXSUB(cv) = subaddr;
2963 else if (s = strrchr(name,':'))
2967 if (strEQ(s, "BEGIN")) {
2970 av_push(beginav, SvREFCNT_inc(gv));
2972 else if (strEQ(s, "END")) {
2975 av_unshift(endav, 1);
2976 av_store(endav, 0, SvREFCNT_inc(gv));
2979 GvCV(gv) = 0; /* Will remember elsewhere instead. */
2980 SvFLAGS(cv) |= SVpcv_ANON;
2986 newFORM(floor,op,block)
2997 name = SvPVx(cSVOP->op_sv, na);
3000 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3002 if (cv = GvFORM(gv)) {
3004 line_t oldline = curcop->cop_line;
3006 curcop->cop_line = copline;
3007 warn("Format %s redefined",name);
3008 curcop->cop_line = oldline;
3014 CvGV(cv) = SvREFCNT_inc(gv);
3015 CvFILEGV(cv) = curcop->cop_filegv;
3017 for (ix = AvFILL(comppad); ix > 0; ix--) {
3018 if (!SvPADMY(curpad[ix]))
3019 SvPADTMP_on(curpad[ix]);
3022 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3023 CvSTART(cv) = LINKLIST(CvROOT(cv));
3024 CvROOT(cv)->op_next = 0;
3038 Newz(1101, mop, 1, LOGOP);
3039 mop->op_type = OP_METHOD;
3040 mop->op_ppaddr = ppaddr[OP_METHOD];
3041 mop->op_first = scalar(ref);
3042 mop->op_flags |= OPf_KIDS;
3043 mop->op_private = 1;
3044 mop->op_other = LINKLIST(name);
3045 mop->op_targ = pad_alloc(OP_METHOD, SVs_PADTMP);
3046 mop->op_next = LINKLIST(ref);
3047 ref->op_next = (OP*)mop;
3048 return scalar((OP*)mop);
3055 return newUNOP(OP_REFGEN, 0,
3056 mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
3063 return newUNOP(OP_REFGEN, 0,
3064 mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3068 newANONSUB(floor, proto, block)
3073 return newUNOP(OP_REFGEN, 0,
3074 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3081 switch (o->op_type) {
3083 o->op_type = OP_PADAV;
3084 o->op_ppaddr = ppaddr[OP_PADAV];
3085 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3088 o->op_type = OP_RV2AV;
3089 o->op_ppaddr = ppaddr[OP_RV2AV];
3094 warn("oops: oopsAV");
3104 switch (o->op_type) {
3107 o->op_type = OP_PADHV;
3108 o->op_ppaddr = ppaddr[OP_PADHV];
3109 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3113 o->op_type = OP_RV2HV;
3114 o->op_ppaddr = ppaddr[OP_RV2HV];
3119 warn("oops: oopsHV");
3129 if (o->op_type == OP_PADANY) {
3130 o->op_type = OP_PADAV;
3131 o->op_ppaddr = ppaddr[OP_PADAV];
3134 return newUNOP(OP_RV2AV, 0, scalar(o));
3142 if (type == OP_MAPSTART)
3143 return newUNOP(OP_NULL, 0, o);
3144 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3151 if (o->op_type == OP_PADANY) {
3152 o->op_type = OP_PADHV;
3153 o->op_ppaddr = ppaddr[OP_PADHV];
3156 return newUNOP(OP_RV2HV, 0, scalar(o));
3163 croak("NOT IMPL LINE %d",__LINE__);
3172 return newUNOP(OP_RV2CV, 0, scalar(o));
3179 if (o->op_type == OP_PADANY) {
3180 o->op_type = OP_PADSV;
3181 o->op_ppaddr = ppaddr[OP_PADSV];
3184 return newUNOP(OP_RV2SV, 0, scalar(o));
3187 /* Check routines. */
3193 if (cUNOP->op_first->op_type == OP_CONCAT)
3194 op->op_flags |= OPf_STACKED;
3202 if (op->op_flags & OPf_KIDS) {
3205 op = modkids(ck_fun(op), op->op_type);
3206 kid = cUNOP->op_first;
3207 newop = kUNOP->op_first->op_sibling;
3209 (newop->op_sibling ||
3210 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3211 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3212 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3216 op_free(kUNOP->op_first);
3217 kUNOP->op_first = newop;
3219 op->op_ppaddr = ppaddr[++op->op_type];
3228 if (op->op_flags & OPf_KIDS) {
3229 OP *kid = cUNOP->op_first;
3230 if (kid->op_type != OP_HELEM)
3231 croak("%s argument is not a HASH element", op_name[op->op_type]);
3241 I32 type = op->op_type;
3243 if (op->op_flags & OPf_KIDS) {
3244 if (cLISTOP->op_first->op_type == OP_STUB) {
3246 op = newUNOP(type, OPf_SPECIAL,
3247 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
3258 hints |= HINT_BLOCK_SCOPE;
3259 if (op->op_flags & OPf_KIDS) {
3260 SVOP *kid = (SVOP*)cUNOP->op_first;
3263 op->op_flags &= ~OPf_KIDS;
3266 else if (kid->op_type == OP_LINESEQ) {
3269 kid->op_next = op->op_next;
3270 cUNOP->op_first = 0;
3273 Newz(1101, enter, 1, LOGOP);
3274 enter->op_type = OP_ENTERTRY;
3275 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3276 enter->op_private = 0;
3278 /* establish postfix order */
3279 enter->op_next = (OP*)enter;
3281 op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3282 op->op_type = OP_LEAVETRY;
3283 op->op_ppaddr = ppaddr[OP_LEAVETRY];
3284 enter->op_other = op;
3290 op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3292 op->op_targ = (PADOFFSET)hints;
3301 if (op->op_flags & OPf_STACKED) {
3303 kid = cUNOP->op_first->op_sibling;
3304 if (kid->op_type == OP_RV2GV)
3316 o = fold_constants(o);
3317 if (o->op_type == OP_CONST)
3326 SVOP *kid = (SVOP*)cUNOP->op_first;
3328 op->op_private = (hints & HINT_STRICT_REFS);
3329 if (kid->op_type == OP_CONST) {
3330 int iscv = (op->op_type==OP_RV2CV)*2;
3332 kid->op_type = OP_GV;
3333 for (gv = 0; !gv; iscv++) {
3335 * This is a little tricky. We only want to add the symbol if we
3336 * didn't add it in the lexer. Otherwise we get duplicate strict
3337 * warnings. But if we didn't add it in the lexer, we must at
3338 * least pretend like we wanted to add it even if it existed before,
3339 * or we get possible typo warnings. OPpCONST_ENTERED says
3340 * whether the lexer already added THIS instance of this symbol.
3342 gv = gv_fetchpv(SvPVx(kid->op_sv, na),
3343 iscv | !(kid->op_private & OPpCONST_ENTERED),
3346 : op->op_type == OP_RV2SV
3348 : op->op_type == OP_RV2AV
3350 : op->op_type == OP_RV2HV
3354 SvREFCNT_dec(kid->op_sv);
3355 kid->op_sv = SvREFCNT_inc(gv);
3371 I32 type = op->op_type;
3373 if (op->op_flags & OPf_REF)
3376 if (op->op_flags & OPf_KIDS) {
3377 SVOP *kid = (SVOP*)cUNOP->op_first;
3379 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3380 OP *newop = newGVOP(type, OPf_REF,
3381 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3388 if (type == OP_FTTTY)
3389 return newGVOP(type, OPf_REF, gv_fetchpv("main'STDIN", TRUE,
3392 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3405 int type = op->op_type;
3406 register I32 oa = opargs[type] >> OASHIFT;
3408 if (op->op_flags & OPf_STACKED) {
3409 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
3412 return no_fh_allowed(op);
3415 if (op->op_flags & OPf_KIDS) {
3416 tokid = &cLISTOP->op_first;
3417 kid = cLISTOP->op_first;
3418 if (kid->op_type == OP_PUSHMARK ||
3419 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
3421 tokid = &kid->op_sibling;
3422 kid = kid->op_sibling;
3424 if (!kid && opargs[type] & OA_DEFGV)
3425 *tokid = kid = newSVREF(newGVOP(OP_GV, 0, defgv));
3429 sibl = kid->op_sibling;
3443 if (kid->op_type == OP_CONST &&
3444 (kid->op_private & OPpCONST_BARE)) {
3445 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3446 OP *newop = newAVREF(newGVOP(OP_GV, 0,
3447 gv_fetchpv(name, TRUE, SVt_PVAV) ));
3449 warn("Array @%s missing the @ in argument %d of %s()",
3450 name, numargs, op_name[type]);
3453 kid->op_sibling = sibl;
3456 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
3457 bad_type(numargs, "array", op_name[op->op_type], kid);
3461 if (kid->op_type == OP_CONST &&
3462 (kid->op_private & OPpCONST_BARE)) {
3463 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3464 OP *newop = newHVREF(newGVOP(OP_GV, 0,
3465 gv_fetchpv(name, TRUE, SVt_PVHV) ));
3467 warn("Hash %%%s missing the %% in argument %d of %s()",
3468 name, numargs, op_name[type]);
3471 kid->op_sibling = sibl;
3474 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
3475 bad_type(numargs, "hash", op_name[op->op_type], kid);
3480 OP *newop = newUNOP(OP_NULL, 0, kid);
3481 kid->op_sibling = 0;
3483 newop->op_next = newop;
3485 kid->op_sibling = sibl;
3490 if (kid->op_type != OP_GV) {
3491 if (kid->op_type == OP_CONST &&
3492 (kid->op_private & OPpCONST_BARE)) {
3493 OP *newop = newGVOP(OP_GV, 0,
3494 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3500 kid->op_sibling = 0;
3501 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3503 kid->op_sibling = sibl;
3509 mod(scalar(kid), type);
3513 tokid = &kid->op_sibling;
3514 kid = kid->op_sibling;
3516 op->op_private = numargs;
3518 return too_many_arguments(op,op_name[op->op_type]);
3521 else if (opargs[type] & OA_DEFGV) {
3523 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3527 while (oa & OA_OPTIONAL)
3529 if (oa && oa != OA_LIST)
3530 return too_few_arguments(op,op_name[op->op_type]);
3539 GV *gv = newGVgen("main");
3541 append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3552 OPCODE type = op->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
3554 op->op_ppaddr = ppaddr[OP_GREPSTART];
3555 Newz(1101, gwop, 1, LOGOP);
3557 if (op->op_flags & OPf_STACKED) {
3560 kid = cLISTOP->op_first->op_sibling;
3561 for (k = cLISTOP->op_first->op_sibling->op_next; k; k = k->op_next) {
3564 kid->op_next = (OP*)gwop;
3565 op->op_flags &= ~OPf_STACKED;
3567 kid = cLISTOP->op_first->op_sibling;
3568 if (type == OP_MAPWHILE)
3575 kid = cLISTOP->op_first->op_sibling;
3576 if (kid->op_type != OP_NULL)
3577 croak("panic: ck_grep");
3578 kid = kUNOP->op_first;
3580 gwop->op_type = type;
3581 gwop->op_ppaddr = ppaddr[type];
3582 gwop->op_first = listkids(op);
3583 gwop->op_flags |= OPf_KIDS;
3584 gwop->op_private = 1;
3585 gwop->op_other = LINKLIST(kid);
3586 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
3587 kid->op_next = (OP*)gwop;
3589 kid = cLISTOP->op_first->op_sibling;
3590 if (!kid || !kid->op_sibling)
3591 return too_few_arguments(op,op_name[op->op_type]);
3592 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
3593 mod(kid, OP_GREPSTART);
3602 if (op->op_flags & OPf_KIDS) {
3603 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3604 if (kid && kid->op_type == OP_CONST)
3605 fbm_compile(((SVOP*)kid)->op_sv, 0);
3614 /* XXX length optimization goes here */
3622 return modkids(ck_fun(op), op->op_type);
3629 return refkids(ck_fun(op), op->op_type);
3638 kid = cLISTOP->op_first;
3640 op = force_list(op);
3641 kid = cLISTOP->op_first;
3643 if (kid->op_type == OP_PUSHMARK)
3644 kid = kid->op_sibling;
3645 if (kid && op->op_flags & OPf_STACKED)
3646 kid = kid->op_sibling;
3647 else if (kid && !kid->op_sibling) { /* print HANDLE; */
3648 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
3649 op->op_flags |= OPf_STACKED; /* make it a filehandle */
3650 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
3651 cLISTOP->op_first->op_sibling = kid;
3652 cLISTOP->op_last = kid;
3653 kid = kid->op_sibling;
3658 append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3660 return listkids(op);
3667 cPMOP->op_pmflags |= PMf_RUNTIME;
3668 cPMOP->op_pmpermflags |= PMf_RUNTIME;
3683 if (cBINOP->op_first->op_flags & OPf_PARENS) {
3684 op->op_private = OPpREPEAT_DOLIST;
3685 cBINOP->op_first = force_list(cBINOP->op_first);
3696 if (op->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
3697 SVOP *kid = (SVOP*)cUNOP->op_first;
3699 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3701 for (s = SvPVX(kid->op_sv); *s; s++) {
3702 if (*s == ':' && s[1] == ':') {
3704 Move(s+2, s+1, strlen(s+2)+1, char);
3705 --SvCUR(kid->op_sv);
3708 sv_catpvn(kid->op_sv, ".pm", 3);
3718 croak("NOT IMPL LINE %d",__LINE__);
3727 if (op->op_flags & OPf_KIDS) {
3728 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3729 if (kid && kid->op_sibling) {
3730 op->op_type = OP_SSELECT;
3731 op->op_ppaddr = ppaddr[OP_SSELECT];
3733 return fold_constants(op);
3743 I32 type = op->op_type;
3745 if (!(op->op_flags & OPf_KIDS)) {
3747 return newUNOP(type, 0,
3748 scalar(newUNOP(OP_RV2AV, 0,
3749 scalar(newGVOP(OP_GV, 0,
3750 gv_fetchpv((subline ? "_" : "ARGV"), TRUE, SVt_PVAV) )))));
3752 return scalar(modkids(ck_fun(op), type));
3759 if (op->op_flags & OPf_STACKED) {
3760 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3762 kid = kUNOP->op_first; /* get past rv2gv */
3764 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
3766 if (kid->op_type == OP_SCOPE) {
3770 else if (kid->op_type == OP_LEAVE) {
3771 if (op->op_type == OP_SORT) {
3772 null(kid); /* wipe out leave */
3775 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
3776 if (k->op_next == kid)
3781 kid->op_next = 0; /* just disconnect the leave */
3782 k = kLISTOP->op_first;
3786 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3787 null(kid); /* wipe out rv2gv */
3788 if (op->op_type == OP_SORT)
3792 op->op_flags |= OPf_SPECIAL;
3805 if (op->op_flags & OPf_STACKED)
3806 return no_fh_allowed(op);
3808 kid = cLISTOP->op_first;
3809 if (kid->op_type != OP_NULL)
3810 croak("panic: ck_split");
3811 kid = kid->op_sibling;
3812 op_free(cLISTOP->op_first);
3813 cLISTOP->op_first = kid;
3815 cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
3816 cLISTOP->op_last = kid; /* There was only one element previously */
3819 if (kid->op_type != OP_MATCH) {
3820 OP *sibl = kid->op_sibling;
3821 kid->op_sibling = 0;
3822 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
3823 if (cLISTOP->op_first == cLISTOP->op_last)
3824 cLISTOP->op_last = kid;
3825 cLISTOP->op_first = kid;
3826 kid->op_sibling = sibl;
3829 if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
3830 SvREFCNT_dec(pm->op_pmshort); /* can't use substring to optimize */
3834 kid->op_type = OP_PUSHRE;
3835 kid->op_ppaddr = ppaddr[OP_PUSHRE];
3838 if (!kid->op_sibling)
3839 append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3841 kid = kid->op_sibling;
3844 if (!kid->op_sibling)
3845 append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
3847 kid = kid->op_sibling;
3850 if (kid->op_sibling)
3851 return too_many_arguments(op,op_name[op->op_type]);
3860 OP *prev = ((cUNOP->op_first->op_sibling)
3861 ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first;
3862 OP *o = prev->op_sibling;
3869 for (cvop = o; cvop->op_sibling; cvop = cvop->op_sibling) ;
3870 if (cvop->op_type == OP_RV2CV) {
3872 null(cvop); /* disable rv2cv */
3873 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
3874 if (tmpop->op_type == OP_GV) {
3875 cv = GvCV(tmpop->op_sv);
3876 if (cv && SvPOK(cv) && (op->op_flags & OPf_STACKED))
3877 proto = SvPV((SV*)cv,na);
3880 op->op_private = (hints & HINT_STRICT_REFS);
3881 if (perldb && curstash != debstash)
3882 op->op_private |= OPpDEREF_DB;
3887 return too_many_arguments(op, CvNAME(cv));
3905 if (o->op_type != OP_REFGEN && o->op_type != OP_UNDEF)
3906 bad_type(arg, "block", CvNAME(cv), o);
3911 if (o->op_type == OP_RV2GV)
3915 o = newUNOP(OP_RV2GV, 0, kid);
3916 o->op_sibling = kid->op_sibling;
3917 kid->op_sibling = 0;
3918 prev->op_sibling = o;
3926 if (o->op_type != OP_RV2GV)
3927 bad_type(arg, "symbol", CvNAME(cv), o);
3930 if (o->op_type != OP_RV2CV)
3931 bad_type(arg, "sub", CvNAME(cv), o);
3934 if (o->op_type != OP_RV2SV && o->op_type != OP_PADSV)
3935 bad_type(arg, "scalar", CvNAME(cv), o);
3938 if (o->op_type != OP_RV2AV && o->op_type != OP_PADAV)
3939 bad_type(arg, "array", CvNAME(cv), o);
3942 if (o->op_type != OP_RV2HV && o->op_type != OP_PADHV)
3943 bad_type(arg, "hash", CvNAME(cv), o);
3947 o = newUNOP(OP_REFGEN, 0, kid);
3948 o->op_sibling = kid->op_sibling;
3949 kid->op_sibling = 0;
3950 prev->op_sibling = o;
3958 croak("Malformed prototype for %s: %s",
3959 CvNAME(cv),SvPV((SV*)cv,na));
3964 mod(o, OP_ENTERSUB);
3968 if (proto && !optional && *proto == '$')
3969 return too_few_arguments(op, CvNAME(cv));
3977 SvREADONLY_on(cSVOP->op_sv);
3985 if (op->op_flags & OPf_KIDS) {
3986 SVOP *kid = (SVOP*)cUNOP->op_first;
3988 if (kid->op_type == OP_NULL)
3989 kid = (SVOP*)kid->op_sibling;
3991 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
3992 op->op_flags |= OPf_SPECIAL;
3997 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4003 register OP* oldop = 0;
4004 if (!o || o->op_seq)
4009 for (; o; o = o->op_next) {
4013 switch (o->op_type) {
4016 curcop = ((COP*)o); /* for warnings */
4017 o->op_seq = ++op_seqmax;
4028 if (o->op_next->op_type == OP_STRINGIFY)
4030 o->op_seq = ++op_seqmax;
4033 if ((o->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
4034 o->op_seq = ++op_seqmax;
4035 break; /* Scalar stub must produce undef. List stub is noop */
4039 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4040 curcop = ((COP*)op);
4046 if (oldop && o->op_next) {
4047 oldop->op_next = o->op_next;
4050 o->op_seq = ++op_seqmax;
4054 if (o->op_next->op_type == OP_RV2SV) {
4055 if (!(o->op_next->op_private & (OPpDEREF_HV|OPpDEREF_AV))) {
4057 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4058 o->op_next = o->op_next->op_next;
4059 o->op_type = OP_GVSV;
4060 o->op_ppaddr = ppaddr[OP_GVSV];
4063 else if (o->op_next->op_type == OP_RV2AV) {
4064 OP* pop = o->op_next->op_next;
4066 if (pop->op_type == OP_CONST &&
4067 (op = pop->op_next) &&
4068 pop->op_next->op_type == OP_AELEM &&
4069 !(pop->op_next->op_private &
4070 (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)) &&
4071 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4075 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4079 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4080 o->op_next = pop->op_next->op_next;
4081 o->op_type = OP_AELEMFAST;
4082 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4083 o->op_private = (U8)i;
4084 GvAVn((GV*)(((SVOP*)o)->op_sv));
4087 o->op_seq = ++op_seqmax;
4094 o->op_seq = ++op_seqmax;
4095 peep(cLOGOP->op_other);
4099 o->op_seq = ++op_seqmax;
4100 peep(cCONDOP->op_true);
4101 peep(cCONDOP->op_false);
4105 o->op_seq = ++op_seqmax;
4106 peep(cLOOP->op_redoop);
4107 peep(cLOOP->op_nextop);
4108 peep(cLOOP->op_lastop);
4113 o->op_seq = ++op_seqmax;
4114 peep(cPMOP->op_pmreplstart);
4118 o->op_seq = ++op_seqmax;
4119 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4120 if (o->op_next->op_sibling &&
4121 o->op_next->op_sibling->op_type != OP_DIE) {
4122 line_t oldline = curcop->cop_line;
4124 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4125 warn("Statement unlikely to be reached");
4126 warn("(Maybe you meant system() when you said exec()?)\n");
4127 curcop->cop_line = oldline;
4132 o->op_seq = ++op_seqmax;