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_efullname(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));
1022 pad_free(op->op_targ);
1023 op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
1024 sv = PAD_SV(op->op_targ);
1025 sv_upgrade(sv, SVt_PVLV);
1026 sv_magic(sv, Nullsv, mtype, Nullch, 0);
1027 curpad[op->op_targ] = sv;
1028 if (op->op_flags & OPf_KIDS)
1029 mod(cBINOP->op_first->op_sibling, type);
1034 ref(cBINOP->op_first, op->op_type);
1041 if (op->op_flags & OPf_KIDS)
1042 mod(cLISTOP->op_last, type);
1046 if (!(op->op_flags & OPf_KIDS))
1048 if (op->op_targ != OP_LIST) {
1049 mod(cBINOP->op_first, type);
1054 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1058 op->op_flags |= OPf_MOD;
1060 if (type == OP_AASSIGN || type == OP_SASSIGN)
1061 op->op_flags |= OPf_SPECIAL|OPf_REF;
1063 op->op_private |= OPpLVAL_INTRO;
1064 op->op_flags &= ~OPf_SPECIAL;
1066 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
1067 op->op_flags |= OPf_REF;
1077 if (op && op->op_flags & OPf_KIDS) {
1078 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1091 if (!op || error_count)
1094 switch (op->op_type) {
1096 if ((type == OP_DEFINED) &&
1097 !(op->op_flags & OPf_STACKED)) {
1098 op->op_type = OP_RV2CV; /* entersub => rv2cv */
1099 op->op_ppaddr = ppaddr[OP_RV2CV];
1100 assert(cUNOP->op_first->op_type == OP_NULL);
1101 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
1102 op->op_flags |= OPf_SPECIAL;
1107 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
1111 ref(cUNOP->op_first, op->op_type);
1114 if (type == OP_RV2AV || type == OP_RV2HV) {
1115 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
1116 op->op_flags |= OPf_MOD;
1122 op->op_flags |= OPf_REF;
1125 ref(cUNOP->op_first, op->op_type);
1130 op->op_flags |= OPf_REF;
1135 if (!(op->op_flags & OPf_KIDS))
1137 ref(cBINOP->op_first, type);
1141 ref(cBINOP->op_first, op->op_type);
1142 if (type == OP_RV2AV || type == OP_RV2HV) {
1143 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
1144 op->op_flags |= OPf_MOD;
1152 if (!(op->op_flags & OPf_KIDS))
1154 ref(cLISTOP->op_last, type);
1170 if (!op || error_count)
1174 if (type == OP_LIST) {
1175 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1178 else if (type != OP_PADSV &&
1181 type != OP_PUSHMARK)
1183 sprintf(tokenbuf, "Can't declare %s in my", op_desc[op->op_type]);
1187 op->op_flags |= OPf_MOD;
1188 op->op_private |= OPpLVAL_INTRO;
1197 o->op_flags |= OPf_PARENS;
1202 bind_match(type, left, right)
1209 if (right->op_type == OP_MATCH ||
1210 right->op_type == OP_SUBST ||
1211 right->op_type == OP_TRANS) {
1212 right->op_flags |= OPf_STACKED;
1213 if (right->op_type != OP_MATCH)
1214 left = mod(left, right->op_type);
1215 if (right->op_type == OP_TRANS)
1216 op = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
1218 op = prepend_elem(right->op_type, scalar(left), right);
1220 return newUNOP(OP_NOT, 0, scalar(op));
1224 return bind_match(type, left,
1225 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1234 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1235 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
1243 if (o->op_flags & OPf_PARENS || perldb || tainting) {
1244 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1245 o->op_type = OP_LEAVE;
1246 o->op_ppaddr = ppaddr[OP_LEAVE];
1249 if (o->op_type == OP_LINESEQ) {
1251 o->op_type = OP_SCOPE;
1252 o->op_ppaddr = ppaddr[OP_SCOPE];
1253 kid = ((LISTOP*)o)->op_first;
1254 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){
1255 SvREFCNT_dec(((COP*)kid)->cop_filegv);
1260 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
1269 int retval = savestack_ix;
1270 SAVEINT(comppad_name_floor);
1271 if ((comppad_name_fill = AvFILL(comppad_name)) > 0)
1272 comppad_name_floor = comppad_name_fill;
1274 comppad_name_floor = 0;
1275 SAVEINT(min_intro_pending);
1276 SAVEINT(max_intro_pending);
1277 min_intro_pending = 0;
1278 SAVEINT(comppad_name_fill);
1279 SAVEINT(padix_floor);
1280 padix_floor = padix;
1281 pad_reset_pending = FALSE;
1283 hints &= ~HINT_BLOCK_SCOPE;
1288 block_end(line, floor, seq)
1293 int needblockscope = hints & HINT_BLOCK_SCOPE;
1294 OP* retval = scalarseq(seq);
1295 if (copline > (line_t)line)
1298 pad_reset_pending = FALSE;
1300 hints |= HINT_BLOCK_SCOPE; /* propagate out */
1301 pad_leavemy(comppad_name_fill);
1310 eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), op);
1311 eval_start = linklist(eval_root);
1312 eval_root->op_next = 0;
1320 main_root = scope(sawparens(scalarvoid(op)));
1321 curcop = &compiling;
1322 main_start = LINKLIST(main_root);
1323 main_root->op_next = 0;
1335 if (o->op_flags & OPf_PARENS)
1339 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1341 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1342 if (*s == ';' || *s == '=')
1343 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1350 return mod(o, OP_NULL); /* a bit kludgey */
1357 if (o->op_type == OP_LIST) {
1358 o = convert(OP_JOIN, 0,
1359 prepend_elem(OP_LIST,
1360 newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1371 I32 type = o->op_type;
1374 if (opargs[type] & OA_RETSCALAR)
1376 if (opargs[type] & OA_TARGET)
1377 o->op_targ = pad_alloc(type, SVs_PADTMP);
1379 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1380 o->op_ppaddr = ppaddr[type = ++(o->op_type)];
1382 if (!(opargs[type] & OA_FOLDCONST))
1386 goto nope; /* Don't try to run w/ errors */
1388 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1389 if (curop->op_type != OP_CONST &&
1390 curop->op_type != OP_LIST &&
1391 curop->op_type != OP_SCALAR &&
1392 curop->op_type != OP_NULL &&
1393 curop->op_type != OP_PUSHMARK) {
1398 curop = LINKLIST(o);
1403 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
1404 pad_swipe(o->op_targ);
1405 else if (SvTEMP(sv)) { /* grab mortal temp? */
1406 (void)SvREFCNT_inc(sv);
1410 if (type == OP_RV2GV)
1411 return newGVOP(OP_GV, 0, (GV*)sv);
1413 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1415 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1420 SvIOK_off(sv); /* undo SvIV() damage */
1422 return newSVOP(OP_CONST, 0, sv);
1426 if (!(opargs[type] & OA_OTHERINT))
1429 if (!(hints & HINT_INTEGER)) {
1430 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
1433 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1434 if (curop->op_type == OP_CONST) {
1435 if (SvIOK(((SVOP*)curop)->op_sv))
1439 if (opargs[curop->op_type] & OA_RETINTEGER)
1443 o->op_ppaddr = ppaddr[++(o->op_type)];
1450 gen_constant_list(o)
1454 I32 oldtmps_floor = tmps_floor;
1458 return o; /* Don't attempt to run with errors */
1460 op = curop = LINKLIST(o);
1466 tmps_floor = oldtmps_floor;
1468 o->op_type = OP_RV2AV;
1469 o->op_ppaddr = ppaddr[OP_RV2AV];
1470 curop = ((UNOP*)o)->op_first;
1471 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
1478 convert(type, flags, op)
1486 if (!op || op->op_type != OP_LIST)
1487 op = newLISTOP(OP_LIST, 0, op, Nullop);
1489 op->op_flags &= ~(OPf_KNOW|OPf_LIST);
1491 if (!(opargs[type] & OA_MARK))
1492 null(cLISTOP->op_first);
1495 op->op_ppaddr = ppaddr[type];
1496 op->op_flags |= flags;
1498 op = CHECKOP(type, op);
1499 if (op->op_type != type)
1502 if (cLISTOP->op_children < 7) {
1503 /* XXX do we really need to do this if we're done appending?? */
1504 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1506 cLISTOP->op_last = last; /* in case check substituted last arg */
1509 return fold_constants(op);
1512 /* List constructors */
1515 append_elem(type, first, last)
1526 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1527 return newLISTOP(type, 0, first, last);
1529 if (first->op_flags & OPf_KIDS)
1530 ((LISTOP*)first)->op_last->op_sibling = last;
1532 first->op_flags |= OPf_KIDS;
1533 ((LISTOP*)first)->op_first = last;
1535 ((LISTOP*)first)->op_last = last;
1536 ((LISTOP*)first)->op_children++;
1541 append_list(type, first, last)
1552 if (first->op_type != type)
1553 return prepend_elem(type, (OP*)first, (OP*)last);
1555 if (last->op_type != type)
1556 return append_elem(type, (OP*)first, (OP*)last);
1558 first->op_last->op_sibling = last->op_first;
1559 first->op_last = last->op_last;
1560 first->op_children += last->op_children;
1561 if (first->op_children)
1562 last->op_flags |= OPf_KIDS;
1569 prepend_elem(type, first, last)
1580 if (last->op_type == type) {
1581 if (type == OP_LIST) { /* already a PUSHMARK there */
1582 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1583 ((LISTOP*)last)->op_first->op_sibling = first;
1586 if (!(last->op_flags & OPf_KIDS)) {
1587 ((LISTOP*)last)->op_last = first;
1588 last->op_flags |= OPf_KIDS;
1590 first->op_sibling = ((LISTOP*)last)->op_first;
1591 ((LISTOP*)last)->op_first = first;
1593 ((LISTOP*)last)->op_children++;
1597 return newLISTOP(type, 0, first, last);
1605 return newOP(OP_STUB, 0);
1612 if (!op || op->op_type != OP_LIST)
1613 op = newLISTOP(OP_LIST, 0, op, Nullop);
1619 newLISTOP(type, flags, first, last)
1627 Newz(1101, listop, 1, LISTOP);
1629 listop->op_type = type;
1630 listop->op_ppaddr = ppaddr[type];
1631 listop->op_children = (first != 0) + (last != 0);
1632 listop->op_flags = flags;
1636 else if (!first && last)
1639 first->op_sibling = last;
1640 listop->op_first = first;
1641 listop->op_last = last;
1642 if (type == OP_LIST) {
1644 pushop = newOP(OP_PUSHMARK, 0);
1645 pushop->op_sibling = first;
1646 listop->op_first = pushop;
1647 listop->op_flags |= OPf_KIDS;
1649 listop->op_last = pushop;
1651 else if (listop->op_children)
1652 listop->op_flags |= OPf_KIDS;
1663 Newz(1101, op, 1, OP);
1665 op->op_ppaddr = ppaddr[type];
1666 op->op_flags = flags;
1669 op->op_private = 0 + (flags >> 8);
1670 if (opargs[type] & OA_RETSCALAR)
1672 if (opargs[type] & OA_TARGET)
1673 op->op_targ = pad_alloc(type, SVs_PADTMP);
1674 return CHECKOP(type, op);
1678 newUNOP(type, flags, first)
1686 first = newOP(OP_STUB, 0);
1687 if (opargs[type] & OA_MARK)
1688 first = force_list(first);
1690 Newz(1101, unop, 1, UNOP);
1691 unop->op_type = type;
1692 unop->op_ppaddr = ppaddr[type];
1693 unop->op_first = first;
1694 unop->op_flags = flags | OPf_KIDS;
1695 unop->op_private = 1 | (flags >> 8);
1697 unop = (UNOP*) CHECKOP(type, unop);
1701 return fold_constants((OP *) unop);
1705 newBINOP(type, flags, first, last)
1712 Newz(1101, binop, 1, BINOP);
1715 first = newOP(OP_NULL, 0);
1717 binop->op_type = type;
1718 binop->op_ppaddr = ppaddr[type];
1719 binop->op_first = first;
1720 binop->op_flags = flags | OPf_KIDS;
1723 binop->op_private = 1 | (flags >> 8);
1726 binop->op_private = 2 | (flags >> 8);
1727 first->op_sibling = last;
1730 binop = (BINOP*)CHECKOP(type, binop);
1734 binop->op_last = last = binop->op_first->op_sibling;
1736 return fold_constants((OP *)binop);
1740 pmtrans(op, expr, repl)
1745 SV *tstr = ((SVOP*)expr)->op_sv;
1746 SV *rstr = ((SVOP*)repl)->op_sv;
1749 register U8 *t = (U8*)SvPV(tstr, tlen);
1750 register U8 *r = (U8*)SvPV(rstr, rlen);
1755 register short *tbl;
1757 tbl = (short*)cPVOP->op_pv;
1758 complement = op->op_private & OPpTRANS_COMPLEMENT;
1759 delete = op->op_private & OPpTRANS_DELETE;
1760 /* squash = op->op_private & OPpTRANS_SQUASH; */
1763 Zero(tbl, 256, short);
1764 for (i = 0; i < tlen; i++)
1766 for (i = 0, j = 0; i < 256; i++) {
1782 if (!rlen && !delete) {
1785 for (i = 0; i < 256; i++)
1787 for (i = 0, j = 0; i < tlen; i++,j++) {
1790 if (tbl[t[i]] == -1)
1796 if (tbl[t[i]] == -1)
1807 newPMOP(type, flags)
1813 Newz(1101, pmop, 1, PMOP);
1814 pmop->op_type = type;
1815 pmop->op_ppaddr = ppaddr[type];
1816 pmop->op_flags = flags;
1817 pmop->op_private = 0 | (flags >> 8);
1819 /* link into pm list */
1820 if (type != OP_TRANS && curstash) {
1821 pmop->op_pmnext = HvPMROOT(curstash);
1822 HvPMROOT(curstash) = pmop;
1829 pmruntime(op, expr, repl)
1837 if (op->op_type == OP_TRANS)
1838 return pmtrans(op, expr, repl);
1842 if (expr->op_type == OP_CONST) {
1844 SV *pat = ((SVOP*)expr)->op_sv;
1845 char *p = SvPV(pat, plen);
1846 if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
1847 sv_setpvn(pat, "\\s+", 3);
1848 p = SvPV(pat, plen);
1849 pm->op_pmflags |= PMf_SKIPWHITE;
1851 pm->op_pmregexp = pregcomp(p, p + plen, pm);
1852 if (strEQ("\\s+", pm->op_pmregexp->precomp))
1853 pm->op_pmflags |= PMf_WHITE;
1858 if (pm->op_pmflags & PMf_KEEP)
1859 expr = newUNOP(OP_REGCMAYBE,0,expr);
1861 Newz(1101, rcop, 1, LOGOP);
1862 rcop->op_type = OP_REGCOMP;
1863 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1864 rcop->op_first = scalar(expr);
1865 rcop->op_flags |= OPf_KIDS;
1866 rcop->op_private = 1;
1867 rcop->op_other = op;
1869 /* establish postfix order */
1870 if (pm->op_pmflags & PMf_KEEP) {
1872 rcop->op_next = expr;
1873 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1876 rcop->op_next = LINKLIST(expr);
1877 expr->op_next = (OP*)rcop;
1880 prepend_elem(op->op_type, scalar((OP*)rcop), op);
1885 if (pm->op_pmflags & PMf_EVAL)
1887 else if (repl->op_type == OP_CONST)
1891 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1892 if (opargs[curop->op_type] & OA_DANGEROUS) {
1893 if (curop->op_type == OP_GV) {
1894 GV *gv = ((GVOP*)curop)->op_gv;
1895 if (strchr("&`'123456789+", *GvENAME(gv)))
1898 else if (curop->op_type == OP_RV2CV)
1900 else if (curop->op_type == OP_RV2SV ||
1901 curop->op_type == OP_RV2AV ||
1902 curop->op_type == OP_RV2HV ||
1903 curop->op_type == OP_RV2GV) {
1904 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1907 else if (curop->op_type == OP_PADSV ||
1908 curop->op_type == OP_PADAV ||
1909 curop->op_type == OP_PADHV ||
1910 curop->op_type == OP_PADANY) {
1919 if (curop == repl) {
1920 pm->op_pmflags |= PMf_CONST; /* const for long enough */
1921 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
1922 prepend_elem(op->op_type, scalar(repl), op);
1925 Newz(1101, rcop, 1, LOGOP);
1926 rcop->op_type = OP_SUBSTCONT;
1927 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1928 rcop->op_first = scalar(repl);
1929 rcop->op_flags |= OPf_KIDS;
1930 rcop->op_private = 1;
1931 rcop->op_other = op;
1933 /* establish postfix order */
1934 rcop->op_next = LINKLIST(repl);
1935 repl->op_next = (OP*)rcop;
1937 pm->op_pmreplroot = scalar((OP*)rcop);
1938 pm->op_pmreplstart = LINKLIST(rcop);
1947 newSVOP(type, flags, sv)
1953 Newz(1101, svop, 1, SVOP);
1954 svop->op_type = type;
1955 svop->op_ppaddr = ppaddr[type];
1957 svop->op_next = (OP*)svop;
1958 svop->op_flags = flags;
1959 if (opargs[type] & OA_RETSCALAR)
1961 if (opargs[type] & OA_TARGET)
1962 svop->op_targ = pad_alloc(type, SVs_PADTMP);
1963 return CHECKOP(type, svop);
1967 newGVOP(type, flags, gv)
1973 Newz(1101, gvop, 1, GVOP);
1974 gvop->op_type = type;
1975 gvop->op_ppaddr = ppaddr[type];
1976 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
1977 gvop->op_next = (OP*)gvop;
1978 gvop->op_flags = flags;
1979 if (opargs[type] & OA_RETSCALAR)
1981 if (opargs[type] & OA_TARGET)
1982 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
1983 return CHECKOP(type, gvop);
1987 newPVOP(type, flags, pv)
1993 Newz(1101, pvop, 1, PVOP);
1994 pvop->op_type = type;
1995 pvop->op_ppaddr = ppaddr[type];
1997 pvop->op_next = (OP*)pvop;
1998 pvop->op_flags = flags;
1999 if (opargs[type] & OA_RETSCALAR)
2001 if (opargs[type] & OA_TARGET)
2002 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
2003 return CHECKOP(type, pvop);
2012 save_hptr(&curstash);
2013 save_item(curstname);
2018 name = SvPV(sv, len);
2019 curstash = gv_stashpvn(name,len,TRUE);
2020 sv_setpvn(curstname, name, len);
2024 sv_setpv(curstname,"<none>");
2032 utilize(aver, floor, version, id, arg)
2045 if (id->op_type != OP_CONST)
2046 croak("Module name must be constant");
2050 if(version != Nullop) {
2051 SV *vesv = ((SVOP*)version)->op_sv;
2053 if (arg == Nullop && !SvNIOK(vesv)) {
2060 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2061 croak("Version number must be constant number");
2063 /* Make copy of id so we don't free it twice */
2064 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2066 /* Fake up a method call to VERSION */
2067 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2068 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2069 append_elem(OP_LIST,
2070 prepend_elem(OP_LIST, pack, list(version)),
2071 newUNOP(OP_METHOD, 0, meth)));
2075 /* Fake up an import/unimport */
2076 if (arg && arg->op_type == OP_STUB)
2077 imop = arg; /* no import on explicit () */
2078 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2079 imop = Nullop; /* use 5.0; */
2082 /* Make copy of id so we don't free it twice */
2083 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2084 meth = newSVOP(OP_CONST, 0,
2086 ? newSVpv("import", 6)
2087 : newSVpv("unimport", 8)
2089 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2090 append_elem(OP_LIST,
2091 prepend_elem(OP_LIST, pack, list(arg)),
2092 newUNOP(OP_METHOD, 0, meth)));
2095 /* Fake up a require */
2096 rqop = newUNOP(OP_REQUIRE, 0, id);
2098 /* Fake up the BEGIN {}, which does its thing immediately. */
2100 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
2102 append_elem(OP_LINESEQ,
2103 append_elem(OP_LINESEQ,
2104 newSTATEOP(0, Nullch, rqop),
2105 newSTATEOP(0, Nullch, veop)),
2106 newSTATEOP(0, Nullch, imop) ));
2113 newSLICEOP(flags, subscript, listval)
2118 return newBINOP(OP_LSLICE, flags,
2119 list(force_list(subscript)),
2120 list(force_list(listval)) );
2130 if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
2131 op = cUNOP->op_first;
2133 if (op->op_type == OP_COND_EXPR) {
2134 I32 t = list_assignment(cCONDOP->op_first->op_sibling);
2135 I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
2140 yyerror("Assignment to both a list and a scalar");
2144 if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
2145 op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
2146 op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
2149 if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
2152 if (op->op_type == OP_RV2SV)
2159 newASSIGNOP(flags, left, optype, right)
2168 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2169 return newLOGOP(optype, 0,
2170 mod(scalar(left), optype),
2171 newUNOP(OP_SASSIGN, 0, scalar(right)));
2174 return newBINOP(optype, OPf_STACKED,
2175 mod(scalar(left), optype), scalar(right));
2179 if (list_assignment(left)) {
2181 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2182 left = mod(left, OP_AASSIGN);
2190 op = newBINOP(OP_AASSIGN, flags,
2191 list(force_list(right)),
2192 list(force_list(left)) );
2193 op->op_private = 0 | (flags >> 8);
2194 if (!(left->op_private & OPpLVAL_INTRO)) {
2195 static int generation = 100;
2199 for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
2200 if (opargs[curop->op_type] & OA_DANGEROUS) {
2201 if (curop->op_type == OP_GV) {
2202 GV *gv = ((GVOP*)curop)->op_gv;
2203 if (gv == defgv || SvCUR(gv) == generation)
2205 SvCUR(gv) = generation;
2207 else if (curop->op_type == OP_PADSV ||
2208 curop->op_type == OP_PADAV ||
2209 curop->op_type == OP_PADHV ||
2210 curop->op_type == OP_PADANY) {
2211 SV **svp = AvARRAY(comppad_name);
2212 SV *sv = svp[curop->op_targ];
2213 if (SvCUR(sv) == generation)
2215 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2217 else if (curop->op_type == OP_RV2CV)
2219 else if (curop->op_type == OP_RV2SV ||
2220 curop->op_type == OP_RV2AV ||
2221 curop->op_type == OP_RV2HV ||
2222 curop->op_type == OP_RV2GV) {
2223 if (lastop->op_type != OP_GV) /* funny deref? */
2232 op->op_private = OPpASSIGN_COMMON;
2234 if (right && right->op_type == OP_SPLIT) {
2236 if ((tmpop = ((LISTOP*)right)->op_first) &&
2237 tmpop->op_type == OP_PUSHRE)
2239 PMOP *pm = (PMOP*)tmpop;
2240 if (left->op_type == OP_RV2AV &&
2241 !(left->op_private & OPpLVAL_INTRO) &&
2242 !(op->op_private & OPpASSIGN_COMMON) )
2244 tmpop = ((UNOP*)left)->op_first;
2245 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2246 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2247 pm->op_pmflags |= PMf_ONCE;
2248 tmpop = ((UNOP*)op)->op_first; /* to list (nulled) */
2249 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2250 tmpop->op_sibling = Nullop; /* don't free split */
2251 right->op_next = tmpop->op_next; /* fix starting loc */
2252 op_free(op); /* blow off assign */
2253 right->op_flags &= ~(OPf_KNOW|OPf_LIST);
2254 /* "I don't know and I don't care." */
2259 if (modcount < 10000 &&
2260 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2262 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2264 sv_setiv(sv, modcount+1);
2272 right = newOP(OP_UNDEF, 0);
2273 if (right->op_type == OP_READLINE) {
2274 right->op_flags |= OPf_STACKED;
2275 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
2278 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
2279 op = newBINOP(OP_SASSIGN, flags,
2280 scalar(right), mod(scalar(left), OP_SASSIGN) );
2292 newSTATEOP(flags, label, op)
2299 /* Introduce my variables. */
2300 if (min_intro_pending) {
2301 SV **svp = AvARRAY(comppad_name);
2304 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2305 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2306 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2307 SvNVX(sv) = (double)cop_seqmax;
2310 min_intro_pending = 0;
2311 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2314 Newz(1101, cop, 1, COP);
2315 if (perldb && curcop->cop_line && curstash != debstash) {
2316 cop->op_type = OP_DBSTATE;
2317 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2320 cop->op_type = OP_NEXTSTATE;
2321 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2323 cop->op_flags = flags;
2324 cop->op_private = 0 | (flags >> 8);
2325 cop->op_next = (OP*)cop;
2328 cop->cop_label = label;
2329 hints |= HINT_BLOCK_SCOPE;
2331 cop->cop_seq = cop_seqmax++;
2332 cop->cop_arybase = curcop->cop_arybase;
2334 if (copline == NOLINE)
2335 cop->cop_line = curcop->cop_line;
2337 cop->cop_line = copline;
2340 cop->cop_filegv = GvREFCNT_inc(curcop->cop_filegv);
2341 cop->cop_stash = curstash;
2343 if (perldb && curstash != debstash) {
2344 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2345 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2346 (void)SvIOK_on(*svp);
2348 SvSTASH(*svp) = (HV*)cop;
2352 return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2356 newLOGOP(type, flags, first, other)
2365 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2366 return newBINOP(type, flags, scalar(first), scalar(other));
2368 scalarboolean(first);
2369 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2370 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2371 if (type == OP_AND || type == OP_OR) {
2377 first = cUNOP->op_first;
2379 first->op_next = op->op_next;
2380 cUNOP->op_first = Nullop;
2384 if (first->op_type == OP_CONST) {
2385 if (dowarn && (first->op_private & OPpCONST_BARE))
2386 warn("Probable precedence problem on %s", op_desc[type]);
2387 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2396 else if (first->op_type == OP_WANTARRAY) {
2406 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2407 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2409 Newz(1101, logop, 1, LOGOP);
2411 logop->op_type = type;
2412 logop->op_ppaddr = ppaddr[type];
2413 logop->op_first = first;
2414 logop->op_flags = flags | OPf_KIDS;
2415 logop->op_other = LINKLIST(other);
2416 logop->op_private = 1 | (flags >> 8);
2418 /* establish postfix order */
2419 logop->op_next = LINKLIST(first);
2420 first->op_next = (OP*)logop;
2421 first->op_sibling = other;
2423 op = newUNOP(OP_NULL, 0, (OP*)logop);
2424 other->op_next = op;
2430 newCONDOP(flags, first, trueop, falseop)
2440 return newLOGOP(OP_AND, 0, first, trueop);
2442 return newLOGOP(OP_OR, 0, first, falseop);
2444 scalarboolean(first);
2445 if (first->op_type == OP_CONST) {
2446 if (SvTRUE(((SVOP*)first)->op_sv)) {
2457 else if (first->op_type == OP_WANTARRAY) {
2461 Newz(1101, condop, 1, CONDOP);
2463 condop->op_type = OP_COND_EXPR;
2464 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2465 condop->op_first = first;
2466 condop->op_flags = flags | OPf_KIDS;
2467 condop->op_true = LINKLIST(trueop);
2468 condop->op_false = LINKLIST(falseop);
2469 condop->op_private = 1 | (flags >> 8);
2471 /* establish postfix order */
2472 condop->op_next = LINKLIST(first);
2473 first->op_next = (OP*)condop;
2475 first->op_sibling = trueop;
2476 trueop->op_sibling = falseop;
2477 op = newUNOP(OP_NULL, 0, (OP*)condop);
2479 trueop->op_next = op;
2480 falseop->op_next = op;
2486 newRANGE(flags, left, right)
2496 Newz(1101, condop, 1, CONDOP);
2498 condop->op_type = OP_RANGE;
2499 condop->op_ppaddr = ppaddr[OP_RANGE];
2500 condop->op_first = left;
2501 condop->op_flags = OPf_KIDS;
2502 condop->op_true = LINKLIST(left);
2503 condop->op_false = LINKLIST(right);
2504 condop->op_private = 1 | (flags >> 8);
2506 left->op_sibling = right;
2508 condop->op_next = (OP*)condop;
2509 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2510 flop = newUNOP(OP_FLOP, 0, flip);
2511 op = newUNOP(OP_NULL, 0, flop);
2514 left->op_next = flip;
2515 right->op_next = flop;
2517 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2518 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2519 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2520 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2522 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2523 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2526 if (!flip->op_private || !flop->op_private)
2527 linklist(op); /* blow off optimizer unless constant */
2533 newLOOPOP(flags, debuggable, expr, block)
2541 int once = block && block->op_flags & OPf_SPECIAL &&
2542 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
2545 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2546 return block; /* do {} while 0 does once */
2547 else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
2548 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr);
2551 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2552 op = newLOGOP(OP_AND, 0, expr, listop);
2554 ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2556 if (once && op != listop)
2557 op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2560 op = newUNOP(OP_NULL, 0, op); /* or do {} while 1 loses outer block */
2562 op->op_flags |= flags;
2564 op->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
2569 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2583 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2584 expr = newUNOP(OP_DEFINED, 0,
2585 newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2589 block = newOP(OP_NULL, 0);
2592 next = LINKLIST(cont);
2594 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2596 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2597 redo = LINKLIST(listop);
2600 op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2601 if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2602 op_free(expr); /* oops, it's a while (0) */
2604 return Nullop; /* (listop already freed by newLOGOP) */
2606 ((LISTOP*)listop)->op_last->op_next = condop =
2607 (op == listop ? redo : LINKLIST(op));
2615 Newz(1101,loop,1,LOOP);
2616 loop->op_type = OP_ENTERLOOP;
2617 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2618 loop->op_private = 0;
2619 loop->op_next = (OP*)loop;
2622 op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2624 loop->op_redoop = redo;
2625 loop->op_lastop = op;
2628 loop->op_nextop = next;
2630 loop->op_nextop = op;
2632 op->op_flags |= flags;
2633 op->op_private |= (flags >> 8);
2638 #ifndef CAN_PROTOTYPE
2639 newFOROP(flags,label,forline,sv,expr,block,cont)
2648 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2649 #endif /* CAN_PROTOTYPE */
2657 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2658 sv->op_type = OP_RV2GV;
2659 sv->op_ppaddr = ppaddr[OP_RV2GV];
2661 else if (sv->op_type == OP_PADSV) { /* private variable */
2662 padoff = sv->op_targ;
2667 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
2670 sv = newGVOP(OP_GV, 0, defgv);
2672 if (expr->op_type == OP_RV2AV) {
2673 expr = scalar(ref(expr, OP_ITER));
2674 iterflags |= OPf_STACKED;
2676 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
2677 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2679 assert(!loop->op_next);
2680 Renew(loop, 1, LOOP);
2681 loop->op_targ = padoff;
2682 return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2683 newOP(OP_ITER, 0), block, cont));
2687 newLOOPEX(type, label)
2692 if (type != OP_GOTO || label->op_type == OP_CONST) {
2693 op = newPVOP(type, 0, savepv(
2694 label->op_type == OP_CONST
2695 ? SvPVx(((SVOP*)label)->op_sv, na)
2700 if (label->op_type == OP_ENTERSUB)
2701 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
2702 op = newUNOP(type, OPf_STACKED, label);
2704 hints |= HINT_BLOCK_SCOPE;
2712 if (!CvXSUB(cv) && CvROOT(cv)) {
2714 croak("Can't undef active subroutine");
2721 op_free(CvROOT(cv));
2722 CvROOT(cv) = Nullop;
2725 SvREFCNT_dec(CvGV(cv));
2727 SvREFCNT_dec(CvOUTSIDE(cv));
2728 CvOUTSIDE(cv) = Nullcv;
2729 if (CvPADLIST(cv)) {
2730 I32 i = AvFILL(CvPADLIST(cv));
2732 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2736 SvREFCNT_dec((SV*)CvPADLIST(cv));
2737 CvPADLIST(cv) = Nullav;
2747 AV* protopadlist = CvPADLIST(proto);
2748 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
2749 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
2750 SV** svp = AvARRAY(protopad);
2759 cv = compcv = (CV*)NEWSV(1104,0);
2760 sv_upgrade((SV *)cv, SVt_PVCV);
2763 CvFILEGV(cv) = CvFILEGV(proto);
2764 CvGV(cv) = GvREFCNT_inc(CvGV(proto));
2765 CvSTASH(cv) = CvSTASH(proto);
2766 CvROOT(cv) = CvROOT(proto);
2767 CvSTART(cv) = CvSTART(proto);
2768 if (CvOUTSIDE(proto))
2769 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc((SV*)CvOUTSIDE(proto));
2773 comppadlist = newAV();
2774 AvREAL_off(comppadlist);
2775 av_store(comppadlist, 0, SvREFCNT_inc((SV*)protopad_name));
2776 av_store(comppadlist, 1, (SV*)comppad);
2777 CvPADLIST(cv) = comppadlist;
2778 av_extend(comppad, AvFILL(protopad));
2779 curpad = AvARRAY(comppad);
2781 av = newAV(); /* will be @_ */
2783 av_store(comppad, 0, (SV*)av);
2784 AvFLAGS(av) = AVf_REIFY;
2786 svp = AvARRAY(protopad_name);
2787 for ( ix = AvFILL(protopad); ix > 0; ix--) {
2789 if (svp[ix] != &sv_undef) {
2790 char *name = SvPVX(svp[ix]); /* XXX */
2791 if (SvFLAGS(svp[ix]) & SVf_FAKE) { /* lexical from outside? */
2792 I32 off = pad_findlex(name,ix,curcop->cop_seq, CvOUTSIDE(proto),
2795 croak("panic: cv_clone: %s", name);
2797 else { /* our own lexical */
2799 av_store(comppad, ix, sv = (SV*)newAV());
2800 else if (*name == '%')
2801 av_store(comppad, ix, sv = (SV*)newHV());
2803 av_store(comppad, ix, sv = NEWSV(0,0));
2808 av_store(comppad, ix, sv = NEWSV(0,0));
2824 if(cv && SvPOK(cv) && !SvCUR(cv)) {
2825 for (o = CvSTART(cv); o; o = o->op_next) {
2826 OPCODE type = o->op_type;
2828 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
2830 if (type == OP_LEAVESUB || type == OP_RETURN)
2832 if (type != OP_CONST || sv)
2835 sv = ((SVOP*)o)->op_sv;
2842 newSUB(floor,op,proto,block)
2849 char *name = op ? SvPVx(cSVOP->op_sv, na) : "__ANON__";
2850 GV* gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV);
2857 if (cv = GvCV(gv)) {
2859 cv = 0; /* just a cached method */
2860 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
2861 SV* const_sv = cv_const_sv(cv);
2863 char *p = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
2865 if((!proto != !SvPOK(cv)) || (p && strNE(SvPV((SV*)cv,na), p))) {
2866 warn("Prototype mismatch: (%s) vs (%s)",
2867 SvPOK(cv) ? SvPV((SV*)cv,na) : "none",
2871 if ((const_sv || dowarn) && strNE(name, "BEGIN")) {/* already defined (or promised)? */
2872 line_t oldline = curcop->cop_line;
2874 curcop->cop_line = copline;
2875 warn(const_sv ? "Constant subroutine %s redefined"
2876 : "Subroutine %s redefined",name);
2877 curcop->cop_line = oldline;
2883 if (cv) { /* must reuse cv if autoloaded */
2885 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
2886 CvOUTSIDE(compcv) = 0;
2887 CvPADLIST(cv) = CvPADLIST(compcv);
2888 CvPADLIST(compcv) = 0;
2889 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
2890 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
2891 SvREFCNT_dec(compcv);
2898 CvFILEGV(cv) = curcop->cop_filegv;
2899 CvGV(cv) = GvREFCNT_inc(gv);
2900 CvSTASH(cv) = curstash;
2903 char *p = SvPVx(((SVOP*)proto)->op_sv, na);
2904 sv_setpv((SV*)cv, p);
2920 av = newAV(); /* Will be @_ */
2922 av_store(comppad, 0, (SV*)av);
2923 AvFLAGS(av) = AVf_REIFY;
2925 for (ix = AvFILL(comppad); ix > 0; ix--) {
2926 if (!SvPADMY(curpad[ix]))
2927 SvPADTMP_on(curpad[ix]);
2930 if (AvFILL(comppad_name) < AvFILL(comppad))
2931 av_store(comppad_name, AvFILL(comppad), Nullsv);
2933 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
2934 CvSTART(cv) = LINKLIST(CvROOT(cv));
2935 CvROOT(cv)->op_next = 0;
2937 if (s = strrchr(name,':'))
2941 if (strEQ(s, "BEGIN") && !error_count) {
2942 line_t oldline = compiling.cop_line;
2946 SAVESPTR(compiling.cop_filegv);
2950 av_push(beginav, (SV *)cv);
2951 DEBUG_x( dump_sub(gv) );
2952 rs = SvREFCNT_inc(nrs);
2955 if (GvCV(gv) == cv) { /* Detach it. */
2957 GvCV(gv) = 0; /* Was above calllist, why? IZ */
2961 curcop = &compiling;
2962 curcop->cop_line = oldline; /* might have recursed to yylex */
2965 else if (strEQ(s, "END") && !error_count) {
2968 av_unshift(endav, 1);
2969 av_store(endav, 0, SvREFCNT_inc(cv));
2971 if (perldb && curstash != debstash) {
2973 SV *tmpstr = sv_newmortal();
2975 sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), (long)subline);
2976 sv = newSVpv(buf,0);
2978 sprintf(buf,"%ld",(long)curcop->cop_line);
2980 gv_efullname(tmpstr, gv, Nullch);
2981 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
2987 GvCV(gv) = 0; /* Will remember in SVOP instead. */
2995 newXSUB(name, ix, subaddr, filename)
3001 CV* cv = newXS(name, (void(*)())subaddr, filename);
3003 CvXSUBANY(cv).any_i32 = ix;
3009 newXS(name, subaddr, filename)
3011 void (*subaddr) _((CV*));
3015 GV *gv = gv_fetchpv((name ? name : "__ANON__"), GV_ADDMULTI, SVt_PVCV);
3020 if (cv = GvCV(gv)) {
3022 cv = 0; /* just a cached method */
3023 else if (CvROOT(cv) || CvXSUB(cv)) { /* already defined? */
3025 line_t oldline = curcop->cop_line;
3027 curcop->cop_line = copline;
3028 warn("Subroutine %s redefined",name);
3029 curcop->cop_line = oldline;
3035 if (cv) { /* must reuse cv if autoloaded */
3036 assert(SvREFCNT(CvGV(cv)) > 1);
3037 SvREFCNT_dec(CvGV(cv));
3040 cv = (CV*)NEWSV(1105,0);
3041 sv_upgrade((SV *)cv, SVt_PVCV);
3044 CvGV(cv) = GvREFCNT_inc(gv);
3046 CvFILEGV(cv) = gv_fetchfile(filename);
3047 CvXSUB(cv) = subaddr;
3050 else if (s = strrchr(name,':'))
3054 if (strEQ(s, "BEGIN")) {
3057 av_push(beginav, SvREFCNT_inc(gv));
3059 else if (strEQ(s, "END")) {
3062 av_unshift(endav, 1);
3063 av_store(endav, 0, SvREFCNT_inc(gv));
3066 GvCV(gv) = 0; /* Will remember elsewhere instead. */
3073 newFORM(floor,op,block)
3084 name = SvPVx(cSVOP->op_sv, na);
3087 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
3089 if (cv = GvFORM(gv)) {
3091 line_t oldline = curcop->cop_line;
3093 curcop->cop_line = copline;
3094 warn("Format %s redefined",name);
3095 curcop->cop_line = oldline;
3101 CvGV(cv) = GvREFCNT_inc(gv);
3102 CvFILEGV(cv) = curcop->cop_filegv;
3104 for (ix = AvFILL(comppad); ix > 0; ix--) {
3105 if (!SvPADMY(curpad[ix]))
3106 SvPADTMP_on(curpad[ix]);
3109 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3110 CvSTART(cv) = LINKLIST(CvROOT(cv));
3111 CvROOT(cv)->op_next = 0;
3123 return newUNOP(OP_REFGEN, 0,
3124 mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
3131 return newUNOP(OP_REFGEN, 0,
3132 mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3136 newANONSUB(floor, proto, block)
3141 return newUNOP(OP_REFGEN, 0,
3142 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
3149 switch (o->op_type) {
3151 o->op_type = OP_PADAV;
3152 o->op_ppaddr = ppaddr[OP_PADAV];
3153 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3156 o->op_type = OP_RV2AV;
3157 o->op_ppaddr = ppaddr[OP_RV2AV];
3162 warn("oops: oopsAV");
3172 switch (o->op_type) {
3175 o->op_type = OP_PADHV;
3176 o->op_ppaddr = ppaddr[OP_PADHV];
3177 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3181 o->op_type = OP_RV2HV;
3182 o->op_ppaddr = ppaddr[OP_RV2HV];
3187 warn("oops: oopsHV");
3197 if (o->op_type == OP_PADANY) {
3198 o->op_type = OP_PADAV;
3199 o->op_ppaddr = ppaddr[OP_PADAV];
3202 return newUNOP(OP_RV2AV, 0, scalar(o));
3210 if (type == OP_MAPSTART)
3211 return newUNOP(OP_NULL, 0, o);
3212 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
3219 if (o->op_type == OP_PADANY) {
3220 o->op_type = OP_PADHV;
3221 o->op_ppaddr = ppaddr[OP_PADHV];
3224 return newUNOP(OP_RV2HV, 0, scalar(o));
3231 croak("NOT IMPL LINE %d",__LINE__);
3241 return newUNOP(OP_RV2CV, flags, scalar(o));
3248 if (o->op_type == OP_PADANY) {
3249 o->op_type = OP_PADSV;
3250 o->op_ppaddr = ppaddr[OP_PADSV];
3253 return newUNOP(OP_RV2SV, 0, scalar(o));
3256 /* Check routines. */
3262 if (cUNOP->op_first->op_type == OP_CONCAT)
3263 op->op_flags |= OPf_STACKED;
3271 if (op->op_flags & OPf_KIDS) {
3274 op = modkids(ck_fun(op), op->op_type);
3275 kid = cUNOP->op_first;
3276 newop = kUNOP->op_first->op_sibling;
3278 (newop->op_sibling ||
3279 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3280 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3281 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3285 op_free(kUNOP->op_first);
3286 kUNOP->op_first = newop;
3288 op->op_ppaddr = ppaddr[++op->op_type];
3297 if (op->op_flags & OPf_KIDS) {
3298 OP *kid = cUNOP->op_first;
3299 if (kid->op_type != OP_HELEM)
3300 croak("%s argument is not a HASH element", op_desc[op->op_type]);
3310 I32 type = op->op_type;
3312 if (op->op_flags & OPf_KIDS) {
3313 if (cLISTOP->op_first->op_type == OP_STUB) {
3315 op = newUNOP(type, OPf_SPECIAL,
3316 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
3327 hints |= HINT_BLOCK_SCOPE;
3328 if (op->op_flags & OPf_KIDS) {
3329 SVOP *kid = (SVOP*)cUNOP->op_first;
3332 op->op_flags &= ~OPf_KIDS;
3335 else if (kid->op_type == OP_LINESEQ) {
3338 kid->op_next = op->op_next;
3339 cUNOP->op_first = 0;
3342 Newz(1101, enter, 1, LOGOP);
3343 enter->op_type = OP_ENTERTRY;
3344 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3345 enter->op_private = 0;
3347 /* establish postfix order */
3348 enter->op_next = (OP*)enter;
3350 op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
3351 op->op_type = OP_LEAVETRY;
3352 op->op_ppaddr = ppaddr[OP_LEAVETRY];
3353 enter->op_other = op;
3359 op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3361 op->op_targ = (PADOFFSET)hints;
3370 if (op->op_flags & OPf_STACKED) {
3372 kid = cUNOP->op_first->op_sibling;
3373 if (kid->op_type == OP_RV2GV)
3385 o = fold_constants(o);
3386 if (o->op_type == OP_CONST)
3395 SVOP *kid = (SVOP*)cUNOP->op_first;
3397 op->op_private |= (hints & HINT_STRICT_REFS);
3398 if (kid->op_type == OP_CONST) {
3399 int iscv = (op->op_type==OP_RV2CV)*2;
3401 kid->op_type = OP_GV;
3402 for (gv = 0; !gv; iscv++) {
3404 * This is a little tricky. We only want to add the symbol if we
3405 * didn't add it in the lexer. Otherwise we get duplicate strict
3406 * warnings. But if we didn't add it in the lexer, we must at
3407 * least pretend like we wanted to add it even if it existed before,
3408 * or we get possible typo warnings. OPpCONST_ENTERED says
3409 * whether the lexer already added THIS instance of this symbol.
3411 gv = gv_fetchpv(SvPVx(kid->op_sv, na),
3412 iscv | !(kid->op_private & OPpCONST_ENTERED),
3415 : op->op_type == OP_RV2SV
3417 : op->op_type == OP_RV2AV
3419 : op->op_type == OP_RV2HV
3423 SvREFCNT_dec(kid->op_sv);
3424 kid->op_sv = SvREFCNT_inc(gv);
3440 I32 type = op->op_type;
3442 if (op->op_flags & OPf_REF)
3445 if (op->op_flags & OPf_KIDS) {
3446 SVOP *kid = (SVOP*)cUNOP->op_first;
3448 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3449 OP *newop = newGVOP(type, OPf_REF,
3450 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
3457 if (type == OP_FTTTY)
3458 return newGVOP(type, OPf_REF, gv_fetchpv("main'STDIN", TRUE,
3461 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3474 int type = op->op_type;
3475 register I32 oa = opargs[type] >> OASHIFT;
3477 if (op->op_flags & OPf_STACKED) {
3478 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
3481 return no_fh_allowed(op);
3484 if (op->op_flags & OPf_KIDS) {
3485 tokid = &cLISTOP->op_first;
3486 kid = cLISTOP->op_first;
3487 if (kid->op_type == OP_PUSHMARK ||
3488 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
3490 tokid = &kid->op_sibling;
3491 kid = kid->op_sibling;
3493 if (!kid && opargs[type] & OA_DEFGV)
3494 *tokid = kid = newSVREF(newGVOP(OP_GV, 0, defgv));
3498 sibl = kid->op_sibling;
3512 if (kid->op_type == OP_CONST &&
3513 (kid->op_private & OPpCONST_BARE)) {
3514 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3515 OP *newop = newAVREF(newGVOP(OP_GV, 0,
3516 gv_fetchpv(name, TRUE, SVt_PVAV) ));
3518 warn("Array @%s missing the @ in argument %d of %s()",
3519 name, numargs, op_desc[type]);
3522 kid->op_sibling = sibl;
3525 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
3526 bad_type(numargs, "array", op_desc[op->op_type], kid);
3530 if (kid->op_type == OP_CONST &&
3531 (kid->op_private & OPpCONST_BARE)) {
3532 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
3533 OP *newop = newHVREF(newGVOP(OP_GV, 0,
3534 gv_fetchpv(name, TRUE, SVt_PVHV) ));
3536 warn("Hash %%%s missing the %% in argument %d of %s()",
3537 name, numargs, op_desc[type]);
3540 kid->op_sibling = sibl;
3543 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
3544 bad_type(numargs, "hash", op_desc[op->op_type], kid);
3549 OP *newop = newUNOP(OP_NULL, 0, kid);
3550 kid->op_sibling = 0;
3552 newop->op_next = newop;
3554 kid->op_sibling = sibl;
3559 if (kid->op_type != OP_GV) {
3560 if (kid->op_type == OP_CONST &&
3561 (kid->op_private & OPpCONST_BARE)) {
3562 OP *newop = newGVOP(OP_GV, 0,
3563 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3569 kid->op_sibling = 0;
3570 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3572 kid->op_sibling = sibl;
3578 mod(scalar(kid), type);
3582 tokid = &kid->op_sibling;
3583 kid = kid->op_sibling;
3585 op->op_private |= numargs;
3587 return too_many_arguments(op,op_desc[op->op_type]);
3590 else if (opargs[type] & OA_DEFGV) {
3592 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3596 while (oa & OA_OPTIONAL)
3598 if (oa && oa != OA_LIST)
3599 return too_few_arguments(op,op_desc[op->op_type]);
3608 GV *gv = gv_fetchpv("glob", FALSE, SVt_PVCV);
3610 if (gv && GvIMPORTED_CV(gv)) {
3611 op->op_type = OP_LIST;
3612 op->op_ppaddr = ppaddr[OP_LIST];
3613 op = newUNOP(OP_ENTERSUB, OPf_STACKED,
3614 append_elem(OP_LIST, op,
3615 scalar(newUNOP(OP_RV2CV, 0,
3616 newGVOP(OP_GV, 0, gv)))));
3619 gv = newGVgen("main");
3621 append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3632 OPCODE type = op->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE;
3634 op->op_ppaddr = ppaddr[OP_GREPSTART];
3635 Newz(1101, gwop, 1, LOGOP);
3637 if (op->op_flags & OPf_STACKED) {
3640 kid = cLISTOP->op_first->op_sibling;
3641 for (k = cLISTOP->op_first->op_sibling->op_next; k; k = k->op_next) {
3644 kid->op_next = (OP*)gwop;
3645 op->op_flags &= ~OPf_STACKED;
3647 kid = cLISTOP->op_first->op_sibling;
3648 if (type == OP_MAPWHILE)
3655 kid = cLISTOP->op_first->op_sibling;
3656 if (kid->op_type != OP_NULL)
3657 croak("panic: ck_grep");
3658 kid = kUNOP->op_first;
3660 gwop->op_type = type;
3661 gwop->op_ppaddr = ppaddr[type];
3662 gwop->op_first = listkids(op);
3663 gwop->op_flags |= OPf_KIDS;
3664 gwop->op_private = 1;
3665 gwop->op_other = LINKLIST(kid);
3666 gwop->op_targ = pad_alloc(type, SVs_PADTMP);
3667 kid->op_next = (OP*)gwop;
3669 kid = cLISTOP->op_first->op_sibling;
3670 if (!kid || !kid->op_sibling)
3671 return too_few_arguments(op,op_desc[op->op_type]);
3672 for (kid = kid->op_sibling; kid; kid = kid->op_sibling)
3673 mod(kid, OP_GREPSTART);
3682 if (op->op_flags & OPf_KIDS) {
3683 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3684 if (kid && kid->op_type == OP_CONST)
3685 fbm_compile(((SVOP*)kid)->op_sv, 0);
3694 /* XXX length optimization goes here */
3702 return modkids(ck_fun(op), op->op_type);
3709 return refkids(ck_fun(op), op->op_type);
3718 kid = cLISTOP->op_first;
3720 op = force_list(op);
3721 kid = cLISTOP->op_first;
3723 if (kid->op_type == OP_PUSHMARK)
3724 kid = kid->op_sibling;
3725 if (kid && op->op_flags & OPf_STACKED)
3726 kid = kid->op_sibling;
3727 else if (kid && !kid->op_sibling) { /* print HANDLE; */
3728 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
3729 op->op_flags |= OPf_STACKED; /* make it a filehandle */
3730 kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid));
3731 cLISTOP->op_first->op_sibling = kid;
3732 cLISTOP->op_last = kid;
3733 kid = kid->op_sibling;
3738 append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3740 return listkids(op);
3747 cPMOP->op_pmflags |= PMf_RUNTIME;
3748 cPMOP->op_pmpermflags |= PMf_RUNTIME;
3763 if (cBINOP->op_first->op_flags & OPf_PARENS) {
3764 op->op_private |= OPpREPEAT_DOLIST;
3765 cBINOP->op_first = force_list(cBINOP->op_first);
3776 if (op->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */
3777 SVOP *kid = (SVOP*)cUNOP->op_first;
3779 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3781 for (s = SvPVX(kid->op_sv); *s; s++) {
3782 if (*s == ':' && s[1] == ':') {
3784 Move(s+2, s+1, strlen(s+2)+1, char);
3785 --SvCUR(kid->op_sv);
3788 sv_catpvn(kid->op_sv, ".pm", 3);
3798 croak("NOT IMPL LINE %d",__LINE__);
3808 if (op->op_flags & OPf_KIDS) {
3809 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3810 if (kid && kid->op_sibling) {
3811 op->op_type = OP_SSELECT;
3812 op->op_ppaddr = ppaddr[OP_SSELECT];
3814 return fold_constants(op);
3818 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3819 if (kid && kid->op_type == OP_RV2GV)
3820 kid->op_private &= ~HINT_STRICT_REFS;
3828 I32 type = op->op_type;
3830 if (!(op->op_flags & OPf_KIDS)) {
3832 return newUNOP(type, 0,
3833 scalar(newUNOP(OP_RV2AV, 0,
3834 scalar(newGVOP(OP_GV, 0, subline
3836 : gv_fetchpv("ARGV", TRUE, SVt_PVAV) )))));
3838 return scalar(modkids(ck_fun(op), type));
3845 if (op->op_flags & OPf_STACKED) {
3846 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3848 kid = kUNOP->op_first; /* get past rv2gv */
3850 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
3852 if (kid->op_type == OP_SCOPE) {
3856 else if (kid->op_type == OP_LEAVE) {
3857 if (op->op_type == OP_SORT) {
3858 null(kid); /* wipe out leave */
3861 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
3862 if (k->op_next == kid)
3867 kid->op_next = 0; /* just disconnect the leave */
3868 k = kLISTOP->op_first;
3872 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3873 null(kid); /* wipe out rv2gv */
3874 if (op->op_type == OP_SORT)
3878 op->op_flags |= OPf_SPECIAL;
3891 if (op->op_flags & OPf_STACKED)
3892 return no_fh_allowed(op);
3894 kid = cLISTOP->op_first;
3895 if (kid->op_type != OP_NULL)
3896 croak("panic: ck_split");
3897 kid = kid->op_sibling;
3898 op_free(cLISTOP->op_first);
3899 cLISTOP->op_first = kid;
3901 cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
3902 cLISTOP->op_last = kid; /* There was only one element previously */
3905 if (kid->op_type != OP_MATCH) {
3906 OP *sibl = kid->op_sibling;
3907 kid->op_sibling = 0;
3908 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
3909 if (cLISTOP->op_first == cLISTOP->op_last)
3910 cLISTOP->op_last = kid;
3911 cLISTOP->op_first = kid;
3912 kid->op_sibling = sibl;
3915 if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
3916 SvREFCNT_dec(pm->op_pmshort); /* can't use substring to optimize */
3920 kid->op_type = OP_PUSHRE;
3921 kid->op_ppaddr = ppaddr[OP_PUSHRE];
3924 if (!kid->op_sibling)
3925 append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3927 kid = kid->op_sibling;
3930 if (!kid->op_sibling)
3931 append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
3933 kid = kid->op_sibling;
3936 if (kid->op_sibling)
3937 return too_many_arguments(op,op_desc[op->op_type]);
3946 OP *prev = ((cUNOP->op_first->op_sibling)
3947 ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first;
3948 OP *o = prev->op_sibling;
3955 for (cvop = o; cvop->op_sibling; cvop = cvop->op_sibling) ;
3956 if (cvop->op_type == OP_RV2CV) {
3958 op->op_private |= (cvop->op_private & OPpENTERSUB_AMPER);
3959 null(cvop); /* disable rv2cv */
3960 tmpop = (SVOP*)((UNOP*)cvop)->op_first;
3961 if (tmpop->op_type == OP_GV) {
3962 cv = GvCV(tmpop->op_sv);
3963 if (cv && SvPOK(cv) && !(op->op_private & OPpENTERSUB_AMPER))
3964 proto = SvPV((SV*)cv,na);
3967 op->op_private |= (hints & HINT_STRICT_REFS);
3968 if (perldb && curstash != debstash)
3969 op->op_private |= OPpENTERSUB_DB;
3974 return too_many_arguments(op, CvNAME(cv));
3992 if (o->op_type != OP_REFGEN && o->op_type != OP_UNDEF)
3993 bad_type(arg, "block", CvNAME(cv), o);
3998 if (o->op_type == OP_RV2GV)
4002 o = newUNOP(OP_RV2GV, 0, kid);
4003 o->op_sibling = kid->op_sibling;
4004 kid->op_sibling = 0;
4005 prev->op_sibling = o;
4013 if (o->op_type != OP_RV2GV)
4014 bad_type(arg, "symbol", CvNAME(cv), o);
4017 if (o->op_type != OP_RV2CV)
4018 bad_type(arg, "sub", CvNAME(cv), o);
4021 if (o->op_type != OP_RV2SV && o->op_type != OP_PADSV)
4022 bad_type(arg, "scalar", CvNAME(cv), o);
4025 if (o->op_type != OP_RV2AV && o->op_type != OP_PADAV)
4026 bad_type(arg, "array", CvNAME(cv), o);
4029 if (o->op_type != OP_RV2HV && o->op_type != OP_PADHV)
4030 bad_type(arg, "hash", CvNAME(cv), o);
4034 o = newUNOP(OP_REFGEN, 0, kid);
4035 o->op_sibling = kid->op_sibling;
4036 kid->op_sibling = 0;
4037 prev->op_sibling = o;
4048 croak("Malformed prototype for %s: %s",
4049 CvNAME(cv),SvPV((SV*)cv,na));
4054 mod(o, OP_ENTERSUB);
4058 if (proto && !optional && *proto == '$')
4059 return too_few_arguments(op, CvNAME(cv));
4067 SvREADONLY_on(cSVOP->op_sv);
4075 if (op->op_flags & OPf_KIDS) {
4076 SVOP *kid = (SVOP*)cUNOP->op_first;
4078 if (kid->op_type == OP_NULL)
4079 kid = (SVOP*)kid->op_sibling;
4081 kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
4082 op->op_flags |= OPf_SPECIAL;
4087 /* A peephole optimizer. We visit the ops in the order they're to execute. */
4093 register OP* oldop = 0;
4094 if (!o || o->op_seq)
4099 for (; o; o = o->op_next) {
4105 switch (o->op_type) {
4108 curcop = ((COP*)o); /* for warnings */
4109 o->op_seq = op_seqmax++;
4120 if (o->op_next->op_type == OP_STRINGIFY)
4122 o->op_seq = op_seqmax++;
4125 if ((o->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
4126 o->op_seq = op_seqmax++;
4127 break; /* Scalar stub must produce undef. List stub is noop */
4131 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
4132 curcop = ((COP*)op);
4138 if (oldop && o->op_next) {
4139 oldop->op_next = o->op_next;
4142 o->op_seq = op_seqmax++;
4146 if (o->op_next->op_type == OP_RV2SV) {
4147 if (!(o->op_next->op_private & (OPpDEREF_HV|OPpDEREF_AV))) {
4149 o->op_private |= o->op_next->op_private & OPpLVAL_INTRO;
4150 o->op_next = o->op_next->op_next;
4151 o->op_type = OP_GVSV;
4152 o->op_ppaddr = ppaddr[OP_GVSV];
4155 else if (o->op_next->op_type == OP_RV2AV) {
4156 OP* pop = o->op_next->op_next;
4158 if (pop->op_type == OP_CONST &&
4159 (op = pop->op_next) &&
4160 pop->op_next->op_type == OP_AELEM &&
4161 !(pop->op_next->op_private &
4162 (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO)) &&
4163 (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase)
4167 SvREFCNT_dec(((SVOP*)pop)->op_sv);
4171 o->op_flags |= pop->op_next->op_flags & OPf_MOD;
4172 o->op_next = pop->op_next->op_next;
4173 o->op_type = OP_AELEMFAST;
4174 o->op_ppaddr = ppaddr[OP_AELEMFAST];
4175 o->op_private = (U8)i;
4176 GvAVn((GV*)(((SVOP*)o)->op_sv));
4179 o->op_seq = op_seqmax++;
4186 o->op_seq = op_seqmax++;
4187 peep(cLOGOP->op_other);
4191 o->op_seq = op_seqmax++;
4192 peep(cCONDOP->op_true);
4193 peep(cCONDOP->op_false);
4197 o->op_seq = op_seqmax++;
4198 peep(cLOOP->op_redoop);
4199 peep(cLOOP->op_nextop);
4200 peep(cLOOP->op_lastop);
4205 o->op_seq = op_seqmax++;
4206 peep(cPMOP->op_pmreplstart);
4210 o->op_seq = op_seqmax++;
4211 if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
4212 if (o->op_next->op_sibling &&
4213 o->op_next->op_sibling->op_type != OP_DIE) {
4214 line_t oldline = curcop->cop_line;
4216 curcop->cop_line = ((COP*)o->op_next)->cop_line;
4217 warn("Statement unlikely to be reached");
4218 warn("(Maybe you meant system() when you said exec()?)\n");
4219 curcop->cop_line = oldline;
4224 o->op_seq = op_seqmax++;