1 /* $RCSfile: cmd.h,v $$Revision: 4.1 $$Date: 92/08/07 17:19:19 $
3 * Copyright (c) 1991, 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.
14 /* Lowest byte of opargs */
16 #define OA_FOLDCONST 2
17 #define OA_RETSCALAR 4
19 #define OA_RETINTEGER 16
20 #define OA_OTHERINT 32
21 #define OA_DANGEROUS 64
23 /* Remaining nybbles of opargs */
30 #define OA_SCALARREF 7
48 sprintf(tokenbuf,"Missing comma after first argument to %s function",
49 op_name[op->op_type]);
58 sprintf(tokenbuf,"Not enough arguments for %s", op_name[op->op_type]);
64 too_many_arguments(op)
67 sprintf(tokenbuf,"Too many arguments for %s", op_name[op->op_type]);
73 bad_type(n, t, op, kid)
79 sprintf(tokenbuf, "Type of arg %d to %s must be %s (not %s)",
80 n, op_name[op->op_type], t, op_name[kid->op_type]);
85 /* "register" allocation */
91 PADOFFSET off = pad_alloc(OP_PADSV, SVs_PADMY);
93 sv_upgrade(sv, SVt_PVNV);
95 av_store(comppad_name, off, sv);
96 SvNVX(sv) = (double)cop_seqmax;
97 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
98 if (!min_intro_pending)
99 min_intro_pending = off;
100 max_intro_pending = off;
102 av_store(comppad, off, (SV*)newAV());
103 else if (*name == '%')
104 av_store(comppad, off, (SV*)newHV());
105 SvPADMY_on(curpad[off]);
115 SV **svp = AvARRAY(comppad_name);
117 register CONTEXT *cx;
122 I32 seq = cop_seqmax;
124 /* The one we're looking for is probably just before comppad_name_fill. */
125 for (off = comppad_name_fill; off > 0; off--) {
126 if ((sv = svp[off]) &&
128 seq > (I32)SvNVX(sv) &&
129 strEQ(SvPVX(sv), name))
131 return (PADOFFSET)off;
135 /* Nothing in current lexical context--try eval's context, if any.
136 * This is necessary to let the perldb get at lexically scoped variables.
137 * XXX This will also probably interact badly with eval tree caching.
141 for (i = cxstack_ix; i >= 0; i--) {
143 switch (cx->cx_type) {
153 if (debstash && CvSTASH(cv) == debstash) /* ignore DB'* scope */
155 seq = cxstack[i+1].blk_oldcop->cop_seq;
156 curlist = CvPADLIST(cv);
157 curname = (AV*)*av_fetch(curlist, 0, FALSE);
158 svp = AvARRAY(curname);
159 for (off = AvFILL(curname); off > 0; off--) {
160 if ((sv = svp[off]) &&
162 seq > (I32)SvNVX(sv) &&
163 strEQ(SvPVX(sv), name))
165 PADOFFSET newoff = pad_alloc(OP_PADSV, SVs_PADMY);
166 AV *oldpad = (AV*)*av_fetch(curlist, CvDEPTH(cv), FALSE);
167 SV *oldsv = *av_fetch(oldpad, off, TRUE);
169 sv_upgrade(sv, SVt_PVNV);
171 av_store(comppad_name, newoff, sv);
172 SvNVX(sv) = (double)curcop->cop_seq;
173 SvIVX(sv) = 999999999; /* A ref, intro immediately */
174 av_store(comppad, newoff, SvREFCNT_inc(oldsv));
190 SV **svp = AvARRAY(comppad_name);
192 if (min_intro_pending && fill < min_intro_pending) {
193 for (off = max_intro_pending; off >= min_intro_pending; off--) {
195 warn("%s never introduced", SvPVX(sv));
198 /* "Deintroduce" my variables that are leaving with this scope. */
199 for (off = AvFILL(comppad_name); off > fill; off--) {
201 SvIVX(sv) = cop_seqmax;
206 pad_alloc(optype,tmptype)
213 if (AvARRAY(comppad) != curpad)
214 croak("panic: pad_alloc");
215 if (tmptype & SVs_PADMY) {
217 sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE);
218 } while (SvPADBUSY(sv)); /* need a fresh one */
219 retval = AvFILL(comppad);
223 sv = *av_fetch(comppad, ++padix, TRUE);
224 } while (SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY));
227 SvFLAGS(sv) |= tmptype;
228 curpad = AvARRAY(comppad);
229 DEBUG_X(fprintf(stderr, "Pad alloc %d for %s\n", retval, op_name[optype]));
230 return (PADOFFSET)retval;
239 #endif /* STANDARD_C */
242 croak("panic: pad_sv po");
243 DEBUG_X(fprintf(stderr, "Pad sv %d\n", po));
244 return curpad[po]; /* eventually we'll turn this into a macro */
252 pad_free(PADOFFSET po)
253 #endif /* STANDARD_C */
255 if (AvARRAY(comppad) != curpad)
256 croak("panic: pad_free curpad");
258 croak("panic: pad_free po");
259 DEBUG_X(fprintf(stderr, "Pad free %d\n", po));
261 SvPADTMP_off(curpad[po]);
271 pad_swipe(PADOFFSET po)
272 #endif /* STANDARD_C */
274 if (AvARRAY(comppad) != curpad)
275 croak("panic: pad_swipe curpad");
277 croak("panic: pad_swipe po");
278 DEBUG_X(fprintf(stderr, "Pad swipe %d\n", po));
279 curpad[po] = NEWSV(0,0);
280 SvPADTMP_off(curpad[po]);
290 if (AvARRAY(comppad) != curpad)
291 croak("panic: pad_reset curpad");
292 DEBUG_X(fprintf(stderr, "Pad reset\n"));
293 for (po = AvMAX(comppad); po > 0; po--) {
295 SvPADTMP_off(curpad[po]);
306 register OP *kid, *nextkid;
311 if (op->op_flags & OPf_KIDS) {
312 for (kid = cUNOP->op_first; kid; kid = nextkid) {
313 nextkid = kid->op_sibling; /* Get before next freeing kid */
319 switch (op->op_type) {
321 op->op_targ = 0; /* Was holding old type, if any. */
325 SvREFCNT_dec((SV*)cGVOP->op_gv);
329 SvREFCNT_dec(cCOP->cop_filegv);
332 SvREFCNT_dec(cSVOP->op_sv);
337 pad_free(op->op_targ);
346 if (op->op_type != OP_NULL && op->op_targ > 0)
347 pad_free(op->op_targ);
348 op->op_targ = op->op_type;
349 op->op_type = OP_NULL;
350 op->op_ppaddr = ppaddr[OP_NULL];
357 OP* kid = cLISTOP->op_first;
358 assert(kid->op_type == OP_PUSHMARK);
359 cLISTOP->op_first = kid->op_sibling;
364 /* Contextualizers */
366 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
377 /* establish postfix order */
378 if (cUNOP->op_first) {
379 op->op_next = LINKLIST(cUNOP->op_first);
380 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling) {
382 kid->op_next = LINKLIST(kid->op_sibling);
398 if (op && op->op_flags & OPf_KIDS) {
399 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
410 op->op_type == OP_SASSIGN && cBINOP->op_first->op_type == OP_CONST)
411 warn("Found = in conditional, should be ==");
421 if (!op || (op->op_flags & OPf_KNOW)) /* assumes no premature commitment */
424 op->op_flags &= ~OPf_LIST;
425 op->op_flags |= OPf_KNOW;
427 switch (op->op_type) {
429 scalar(cBINOP->op_first);
434 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
441 if (op->op_flags & OPf_KIDS) {
442 for (kid = cUNOP->op_first; kid; kid = kid->op_sibling)
451 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
473 if (op->op_flags & OPf_LIST)
476 op->op_flags |= OPf_KNOW;
478 switch (op->op_type) {
480 if (!(opargs[op->op_type] & OA_FOLDCONST))
482 if (op->op_flags & OPf_STACKED)
554 if (!(op->op_flags & OPf_INTRO))
555 useless = op_name[op->op_type];
562 if (!(op->op_flags & OPf_INTRO) &&
563 (!op->op_sibling || op->op_sibling->op_type != OP_READLINE))
564 useless = "a variable";
569 curcop = ((COP*)op); /* for warning above */
575 useless = "a constant";
576 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
578 else if (SvPOK(sv)) {
579 if (strnEQ(SvPVX(sv), "di", 2) ||
580 strnEQ(SvPVX(sv), "ig", 2))
584 null(op); /* don't execute a constant */
585 SvREFCNT_dec(sv); /* don't even remember it */
589 op->op_type = OP_PREINC; /* pre-increment is faster */
590 op->op_ppaddr = ppaddr[OP_PREINC];
594 op->op_type = OP_PREDEC; /* pre-decrement is faster */
595 op->op_ppaddr = ppaddr[OP_PREDEC];
599 scalarvoid(cBINOP->op_first);
600 useless = op_name[op->op_type];
606 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
613 if (!(op->op_flags & OPf_KIDS))
620 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
624 if (useless && dowarn)
625 warn("Useless use of %s in void context", useless);
634 if (op && op->op_flags & OPf_KIDS) {
635 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
647 if (!op || (op->op_flags & OPf_KNOW)) /* assumes no premature commitment */
650 op->op_flags |= (OPf_KNOW | OPf_LIST);
652 switch (op->op_type) {
655 list(cBINOP->op_first);
660 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
667 if (!(op->op_flags & OPf_KIDS))
669 if (!op->op_next && cUNOP->op_first->op_type == OP_FLOP) {
670 list(cBINOP->op_first);
671 return gen_constant_list(op);
680 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
699 if (op->op_type == OP_LINESEQ ||
700 op->op_type == OP_SCOPE ||
701 op->op_type == OP_LEAVE ||
702 op->op_type == OP_LEAVETRY)
704 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling) {
705 if (kid->op_sibling) {
711 op->op_flags &= ~OPf_PARENS;
712 if (hints & HINT_BLOCK_SCOPE)
713 op->op_flags |= OPf_PARENS;
716 op = newOP(OP_STUB, 0);
726 if (op && op->op_flags & OPf_KIDS) {
727 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
746 switch (op->op_type) {
748 if ((type == OP_UNDEF) && !(op->op_flags & OPf_STACKED)) {
749 op->op_type = OP_RV2CV; /* entersubr => rv2cv */
750 op->op_ppaddr = ppaddr[OP_RV2CV];
751 assert(cUNOP->op_first->op_type == OP_NULL);
752 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
757 sprintf(tokenbuf, "Can't modify %s in %s",
758 op_name[op->op_type],
759 type ? op_name[type] : "local");
764 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
771 op->op_private = (hints & HINT_STRICT_REFS);
772 ref(cUNOP->op_first, op->op_type);
782 op->op_private = (hints & HINT_STRICT_REFS);
783 if (type == OP_RV2AV || type == OP_RV2HV)
784 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
785 ref(cUNOP->op_first, op->op_type);
807 pad_free(op->op_targ);
808 op->op_targ = pad_alloc(op->op_type, SVs_PADMY);
809 sv = PAD_SV(op->op_targ);
810 sv_upgrade(sv, SVt_PVLV);
811 sv_magic(sv, 0, op->op_type == OP_VEC ? 'v' : 'x', 0, 0);
812 curpad[op->op_targ] = sv;
815 if (op->op_flags & OPf_KIDS)
816 mod(cBINOP->op_first, type);
820 ref(cBINOP->op_first, op->op_type);
821 op->op_private = (hints & HINT_STRICT_REFS);
822 if (type == OP_RV2AV || type == OP_RV2HV)
823 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
829 if (!(op->op_flags & OPf_KIDS))
831 mod(cLISTOP->op_last, type);
835 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
839 op->op_flags |= OPf_LVAL;
841 op->op_flags &= ~OPf_SPECIAL;
842 op->op_flags |= OPf_INTRO;
844 else if (type == OP_AASSIGN || type == OP_SASSIGN)
845 op->op_flags |= OPf_SPECIAL;
855 if (op && op->op_flags & OPf_KIDS) {
856 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
873 switch (op->op_type) {
875 if ((type == OP_REFGEN || type == OP_DEFINED)
876 && !(op->op_flags & (OPf_STACKED|OPf_PARENS))) {
877 op->op_type = OP_RV2CV; /* entersubr => rv2cv */
878 op->op_ppaddr = ppaddr[OP_RV2CV];
879 assert(cUNOP->op_first->op_type == OP_NULL);
880 null(((LISTOP*)cUNOP->op_first)->op_first); /* disable pushmark */
885 for (kid = cUNOP->op_first->op_sibling; kid; kid = kid->op_sibling)
889 op->op_private = (hints & HINT_STRICT_REFS);
890 if (type == OP_RV2AV || type == OP_RV2HV)
891 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV : OPpDEREF_HV);
892 ref(cUNOP->op_first, op->op_type);
897 op->op_flags |= OPf_LVAL;
900 op->op_private = (hints & HINT_STRICT_REFS);
901 ref(cUNOP->op_first, op->op_type);
906 op->op_flags |= OPf_LVAL;
911 if (!(op->op_flags & OPf_KIDS))
913 ref(cBINOP->op_first, type);
917 ref(cBINOP->op_first, op->op_type);
918 op->op_private = (hints & HINT_STRICT_REFS);
919 if (type == OP_RV2AV || type == OP_RV2HV || type == OP_REFGEN) {
920 op->op_private |= (type == OP_RV2AV ? OPpDEREF_AV :
921 type == OP_RV2HV ? OPpDEREF_HV : 0);
922 op->op_flags |= OPf_LVAL;
930 if (!(op->op_flags & OPf_KIDS))
932 ref(cLISTOP->op_last, type);
951 if (type == OP_LIST) {
952 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
955 else if (type != OP_PADSV &&
960 sprintf(tokenbuf, "Can't declare %s in my", op_name[op->op_type]);
964 op->op_flags |= OPf_LVAL|OPf_INTRO;
973 o->op_flags |= OPf_PARENS;
978 bind_match(type, left, right)
985 if (right->op_type == OP_MATCH ||
986 right->op_type == OP_SUBST ||
987 right->op_type == OP_TRANS) {
988 right->op_flags |= OPf_STACKED;
989 if (right->op_type != OP_MATCH)
990 left = mod(left, right->op_type);
991 if (right->op_type == OP_TRANS)
992 op = newBINOP(OP_NULL, 0, scalar(left), right);
994 op = prepend_elem(right->op_type, scalar(left), right);
996 return newUNOP(OP_NOT, 0, scalar(op));
1000 return bind_match(type, left,
1001 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1010 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
1011 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(op));
1019 if (o->op_flags & OPf_PARENS) {
1020 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1021 o->op_type = OP_LEAVE;
1022 o->op_ppaddr = ppaddr[OP_LEAVE];
1025 if (o->op_type == OP_LINESEQ) {
1027 o->op_type = OP_SCOPE;
1028 o->op_ppaddr = ppaddr[OP_SCOPE];
1029 kid = ((LISTOP*)o)->op_first;
1030 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1034 o = newUNOP(OP_SCOPE, 0, o);
1041 block_head(o, startp)
1049 o = scope(sawparens(scalarvoid(o)));
1050 curcop = &compiling;
1051 *startp = LINKLIST(o);
1062 if (o->op_flags & OPf_PARENS)
1066 if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') {
1068 for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ;
1069 if (*s == ';' || *s == '=' && (s[1] == '@' || s[2] == '@'))
1070 warn("Parens missing around \"%s\" list", lex ? "my" : "local");
1077 return mod(o, OP_NULL); /* a bit kludgey */
1084 if (o->op_type == OP_LIST) {
1085 o = convert(OP_JOIN, 0,
1086 prepend_elem(OP_LIST,
1087 newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
1098 I32 type = o->op_type;
1101 if (opargs[type] & OA_RETSCALAR)
1103 if (opargs[type] & OA_TARGET)
1104 o->op_targ = pad_alloc(type, SVs_PADTMP);
1106 if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER))
1107 o->op_ppaddr = ppaddr[++(o->op_type)];
1109 if (!(opargs[type] & OA_FOLDCONST))
1112 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
1113 if (curop->op_type != OP_CONST &&
1114 curop->op_type != OP_LIST &&
1115 curop->op_type != OP_SCALAR &&
1116 curop->op_type != OP_PUSHMARK) {
1121 curop = LINKLIST(o);
1125 if (o->op_targ && *stack_sp == PAD_SV(o->op_targ)) /* grab pad temp? */
1126 pad_swipe(o->op_targ);
1127 else if (SvTEMP(*stack_sp)) { /* grab mortal temp? */
1128 SvREFCNT_inc(*stack_sp);
1129 SvTEMP_off(*stack_sp);
1132 if (type == OP_RV2GV)
1133 return newGVOP(OP_GV, 0, *(stack_sp--));
1135 return newSVOP(OP_CONST, 0, *(stack_sp--));
1138 if (!(opargs[type] & OA_OTHERINT))
1141 if (!(hints & HINT_INTEGER)) {
1142 if (!(o->op_flags & OPf_KIDS))
1145 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1146 if (curop->op_type == OP_CONST) {
1147 if (SvIOK(((SVOP*)curop)->op_sv))
1151 if (opargs[curop->op_type] & OA_RETINTEGER)
1157 o->op_ppaddr = ppaddr[++(o->op_type)];
1162 gen_constant_list(o)
1169 I32 oldtmps_floor = tmps_floor;
1173 tmpmark = stack_sp - stack_base;
1174 anonop = newANONLIST(o);
1175 curop = LINKLIST(anonop);
1176 anonop->op_next = 0;
1179 tmpsp = stack_sp - stack_base;
1180 tmps_floor = oldtmps_floor;
1181 stack_sp = stack_base + tmpmark;
1183 o->op_type = OP_RV2AV;
1184 o->op_ppaddr = ppaddr[OP_RV2AV];
1186 curop = ((UNOP*)o)->op_first;
1187 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, newSVsv(stack_sp[1]));
1189 curop = ((UNOP*)anonop)->op_first;
1190 curop = ((UNOP*)curop)->op_first;
1191 curop->op_sibling = 0;
1199 convert(type, flags, op)
1207 if (!op || op->op_type != OP_LIST)
1208 op = newLISTOP(OP_LIST, 0, op, Nullop);
1210 if (!(opargs[type] & OA_MARK))
1211 null(cLISTOP->op_first);
1214 op->op_ppaddr = ppaddr[type];
1215 op->op_flags |= flags;
1217 op = (*check[type])(op);
1218 if (op->op_type != type)
1221 if (cLISTOP->op_children < 7) {
1222 /* XXX do we really need to do this if we're done appending?? */
1223 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1225 cLISTOP->op_last = last; /* in case check substituted last arg */
1228 return fold_constants(op);
1231 /* List constructors */
1234 append_elem(type, first, last)
1245 if (first->op_type == type) {
1246 if (first->op_flags & OPf_KIDS)
1247 ((LISTOP*)first)->op_last->op_sibling = last;
1249 first->op_flags |= OPf_KIDS;
1250 ((LISTOP*)first)->op_first = last;
1252 ((LISTOP*)first)->op_last = last;
1253 ((LISTOP*)first)->op_children++;
1257 return newLISTOP(type, 0, first, last);
1261 append_list(type, first, last)
1272 if (first->op_type != type)
1273 return prepend_elem(type, (OP*)first, (OP*)last);
1275 if (last->op_type != type)
1276 return append_elem(type, (OP*)first, (OP*)last);
1278 first->op_last->op_sibling = last->op_first;
1279 first->op_last = last->op_last;
1280 first->op_children += last->op_children;
1281 if (first->op_children)
1282 last->op_flags |= OPf_KIDS;
1289 prepend_elem(type, first, last)
1300 if (last->op_type == type) {
1301 if (type == OP_LIST) { /* already a PUSHMARK there */
1302 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1303 ((LISTOP*)last)->op_first->op_sibling = first;
1306 if (!(last->op_flags & OPf_KIDS)) {
1307 ((LISTOP*)last)->op_last = first;
1308 last->op_flags |= OPf_KIDS;
1310 first->op_sibling = ((LISTOP*)last)->op_first;
1311 ((LISTOP*)last)->op_first = first;
1313 ((LISTOP*)last)->op_children++;
1317 return newLISTOP(type, 0, first, last);
1325 return newOP(OP_STUB, 0);
1332 if (!op || op->op_type != OP_LIST)
1333 op = newLISTOP(OP_LIST, 0, op, Nullop);
1339 newLISTOP(type, flags, first, last)
1347 Newz(1101, listop, 1, LISTOP);
1349 listop->op_type = type;
1350 listop->op_ppaddr = ppaddr[type];
1351 listop->op_children = (first != 0) + (last != 0);
1352 listop->op_flags = flags;
1356 else if (!first && last)
1359 first->op_sibling = last;
1360 listop->op_first = first;
1361 listop->op_last = last;
1362 if (type == OP_LIST) {
1364 pushop = newOP(OP_PUSHMARK, 0);
1365 pushop->op_sibling = first;
1366 listop->op_first = pushop;
1367 listop->op_flags |= OPf_KIDS;
1369 listop->op_last = pushop;
1371 else if (listop->op_children)
1372 listop->op_flags |= OPf_KIDS;
1383 Newz(1101, op, 1, OP);
1385 op->op_ppaddr = ppaddr[type];
1386 op->op_flags = flags;
1389 /* op->op_private = 0; */
1390 if (opargs[type] & OA_RETSCALAR)
1392 if (opargs[type] & OA_TARGET)
1393 op->op_targ = pad_alloc(type, SVs_PADTMP);
1394 return (*check[type])(op);
1398 newUNOP(type, flags, first)
1406 first = newOP(OP_STUB, 0);
1407 if (opargs[type] & OA_MARK)
1408 first = force_list(first);
1409 else if (first->op_type == OP_LIST)
1412 Newz(1101, unop, 1, UNOP);
1413 unop->op_type = type;
1414 unop->op_ppaddr = ppaddr[type];
1415 unop->op_first = first;
1416 unop->op_flags = flags | OPf_KIDS;
1417 unop->op_private = 1;
1419 unop = (UNOP*)(*check[type])((OP*)unop);
1423 return fold_constants(unop);
1427 newBINOP(type, flags, first, last)
1434 Newz(1101, binop, 1, BINOP);
1437 first = newOP(OP_NULL, 0);
1439 binop->op_type = type;
1440 binop->op_ppaddr = ppaddr[type];
1441 binop->op_first = first;
1442 binop->op_flags = flags | OPf_KIDS;
1445 binop->op_private = 1;
1448 binop->op_private = 2;
1449 first->op_sibling = last;
1452 binop = (BINOP*)(*check[type])((OP*)binop);
1456 binop->op_last = last = binop->op_first->op_sibling;
1458 return fold_constants(binop);
1462 pmtrans(op, expr, repl)
1467 PMOP *pm = (PMOP*)op;
1468 SV *tstr = ((SVOP*)expr)->op_sv;
1469 SV *rstr = ((SVOP*)repl)->op_sv;
1472 register char *t = SvPV(tstr, tlen);
1473 register char *r = SvPV(rstr, rlen);
1479 register short *tbl;
1481 tbl = (short*)cPVOP->op_pv;
1482 complement = op->op_private & OPpTRANS_COMPLEMENT;
1483 delete = op->op_private & OPpTRANS_DELETE;
1484 squash = op->op_private & OPpTRANS_SQUASH;
1487 Zero(tbl, 256, short);
1488 for (i = 0; i < tlen; i++)
1489 tbl[t[i] & 0377] = -1;
1490 for (i = 0, j = 0; i < 256; i++) {
1496 tbl[i] = r[j-1] & 0377;
1501 tbl[i] = r[j++] & 0377;
1506 if (!rlen && !delete) {
1509 for (i = 0; i < 256; i++)
1511 for (i = 0, j = 0; i < tlen; i++,j++) {
1514 if (tbl[t[i] & 0377] == -1)
1515 tbl[t[i] & 0377] = -2;
1520 if (tbl[t[i] & 0377] == -1)
1521 tbl[t[i] & 0377] = r[j] & 0377;
1531 newPMOP(type, flags)
1537 Newz(1101, pmop, 1, PMOP);
1538 pmop->op_type = type;
1539 pmop->op_ppaddr = ppaddr[type];
1540 pmop->op_flags = flags;
1541 pmop->op_private = 0;
1543 /* link into pm list */
1544 if (type != OP_TRANS) {
1545 pmop->op_pmnext = HvPMROOT(curstash);
1546 HvPMROOT(curstash) = pmop;
1553 pmruntime(op, expr, repl)
1561 if (op->op_type == OP_TRANS)
1562 return pmtrans(op, expr, repl);
1566 if (expr->op_type == OP_CONST) {
1568 SV *pat = ((SVOP*)expr)->op_sv;
1569 char *p = SvPV(pat, plen);
1570 if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
1571 sv_setpvn(pat, "\\s+", 3);
1572 p = SvPV(pat, plen);
1573 pm->op_pmflags |= PMf_SKIPWHITE;
1575 scan_prefix(pm, p, plen);
1576 if (pm->op_pmshort && (pm->op_pmflags & PMf_SCANFIRST))
1577 fbm_compile(pm->op_pmshort, pm->op_pmflags & PMf_FOLD);
1578 pm->op_pmregexp = regcomp(p, p + plen, pm->op_pmflags & PMf_FOLD);
1579 if (strEQ("\\s+", pm->op_pmregexp->precomp))
1580 pm->op_pmflags |= PMf_WHITE;
1585 if (pm->op_pmflags & PMf_KEEP)
1586 expr = newUNOP(OP_REGCMAYBE,0,expr);
1588 Newz(1101, rcop, 1, LOGOP);
1589 rcop->op_type = OP_REGCOMP;
1590 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
1591 rcop->op_first = scalar(expr);
1592 rcop->op_flags |= OPf_KIDS;
1593 rcop->op_private = 1;
1594 rcop->op_other = op;
1596 /* establish postfix order */
1597 if (pm->op_pmflags & PMf_KEEP) {
1599 rcop->op_next = expr;
1600 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
1603 rcop->op_next = LINKLIST(expr);
1604 expr->op_next = (OP*)rcop;
1607 prepend_elem(op->op_type, scalar((OP*)rcop), op);
1611 if (repl->op_type == OP_CONST) {
1612 pm->op_pmflags |= PMf_CONST;
1613 prepend_elem(op->op_type, scalar(repl), op);
1618 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
1619 if (opargs[curop->op_type] & OA_DANGEROUS) {
1620 if (curop->op_type == OP_GV) {
1621 GV *gv = ((GVOP*)curop)->op_gv;
1622 if (strchr("&`'123456789+", *GvENAME(gv)))
1625 else if (curop->op_type == OP_RV2CV)
1627 else if (curop->op_type == OP_RV2SV ||
1628 curop->op_type == OP_RV2AV ||
1629 curop->op_type == OP_RV2HV ||
1630 curop->op_type == OP_RV2GV) {
1631 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
1639 if (curop == repl) {
1640 pm->op_pmflags |= PMf_CONST; /* const for long enough */
1641 prepend_elem(op->op_type, scalar(repl), op);
1644 Newz(1101, rcop, 1, LOGOP);
1645 rcop->op_type = OP_SUBSTCONT;
1646 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
1647 rcop->op_first = scalar(repl);
1648 rcop->op_flags |= OPf_KIDS;
1649 rcop->op_private = 1;
1650 rcop->op_other = op;
1652 /* establish postfix order */
1653 rcop->op_next = LINKLIST(repl);
1654 repl->op_next = (OP*)rcop;
1656 pm->op_pmreplroot = scalar((OP*)rcop);
1657 pm->op_pmreplstart = LINKLIST(rcop);
1667 newSVOP(type, flags, sv)
1673 Newz(1101, svop, 1, SVOP);
1674 svop->op_type = type;
1675 svop->op_ppaddr = ppaddr[type];
1677 svop->op_next = (OP*)svop;
1678 svop->op_flags = flags;
1679 if (opargs[type] & OA_RETSCALAR)
1681 if (opargs[type] & OA_TARGET)
1682 svop->op_targ = pad_alloc(type, SVs_PADTMP);
1683 return (*check[type])((OP*)svop);
1687 newGVOP(type, flags, gv)
1693 Newz(1101, gvop, 1, GVOP);
1694 gvop->op_type = type;
1695 gvop->op_ppaddr = ppaddr[type];
1696 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
1697 gvop->op_next = (OP*)gvop;
1698 gvop->op_flags = flags;
1699 if (opargs[type] & OA_RETSCALAR)
1701 if (opargs[type] & OA_TARGET)
1702 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
1703 return (*check[type])((OP*)gvop);
1707 newPVOP(type, flags, pv)
1713 Newz(1101, pvop, 1, PVOP);
1714 pvop->op_type = type;
1715 pvop->op_ppaddr = ppaddr[type];
1717 pvop->op_next = (OP*)pvop;
1718 pvop->op_flags = flags;
1719 if (opargs[type] & OA_RETSCALAR)
1721 if (opargs[type] & OA_TARGET)
1722 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
1723 return (*check[type])((OP*)pvop);
1727 newCVOP(type, flags, cv, cont)
1734 Newz(1101, cvop, 1, CVOP);
1735 cvop->op_type = type;
1736 cvop->op_ppaddr = ppaddr[type];
1738 cvop->op_cont = cont;
1739 cvop->op_next = (OP*)cvop;
1740 cvop->op_flags = flags;
1741 if (opargs[type] & OA_RETSCALAR)
1743 if (opargs[type] & OA_TARGET)
1744 cvop->op_targ = pad_alloc(type, SVs_PADTMP);
1745 return (*check[type])((OP*)cvop);
1754 save_hptr(&curstash);
1755 save_item(curstname);
1760 curstash = fetch_stash(sv,TRUE);
1761 name = SvPV(sv, len);
1762 sv_setpvn(curstname, name, len);
1766 sv_setpv(curstname,"<none>");
1785 OP* curop = LINKLIST(arg);
1790 mark = stack_base + POPMARK;
1791 stack_sp = mark; /* Might as well reset sp now. */
1796 sv = ((SVOP*)id)->op_sv;
1797 name = SvPV(sv, len);
1799 if (strEQ(name, "integer"))
1800 bits = HINT_INTEGER;
1801 else if (strEQ(name, "strict")) {
1803 while (++mark <= sp) {
1804 if (strEQ(SvPV(*mark,na), "refs"))
1805 bits |= HINT_STRICT_REFS;
1806 else if (strEQ(SvPV(*mark,na), "subs"))
1807 bits |= HINT_STRICT_SUBS;
1808 else if (strEQ(SvPV(*mark,na), "vars"))
1809 bits |= HINT_STRICT_VARS;
1813 bits = HINT_STRICT_REFS|HINT_STRICT_SUBS|HINT_STRICT_VARS;
1830 fetch_stash(sv,create)
1837 char *name = SvPV(sv, na);
1838 sprintf(tmpbuf,"%s::",name);
1839 tmpgv = gv_fetchpv(tmpbuf,create, SVt_PVHV);
1843 GvHV(tmpgv) = newHV();
1844 stash = GvHV(tmpgv);
1846 HvNAME(stash) = savestr(name);
1851 newSLICEOP(flags, subscript, listval)
1856 return newBINOP(OP_LSLICE, flags,
1857 list(force_list(subscript)),
1858 list(force_list(listval)) );
1868 if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
1869 op = cUNOP->op_first;
1871 if (op->op_type == OP_COND_EXPR) {
1872 I32 t = list_assignment(cCONDOP->op_first->op_sibling);
1873 I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
1878 yyerror("Assignment to both a list and a scalar");
1882 if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
1883 op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
1884 op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
1887 if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
1890 if (op->op_type == OP_RV2SV)
1897 newASSIGNOP(flags, left, right)
1904 if (list_assignment(left)) {
1906 left = mod(left, OP_AASSIGN);
1907 if (right && right->op_type == OP_SPLIT) {
1908 if ((op = ((LISTOP*)right)->op_first) && op->op_type == OP_PUSHRE) {
1909 PMOP *pm = (PMOP*)op;
1910 if (left->op_type == OP_RV2AV) {
1911 op = ((UNOP*)left)->op_first;
1912 if (op->op_type == OP_GV && !pm->op_pmreplroot) {
1913 pm->op_pmreplroot = (OP*)((GVOP*)op)->op_gv;
1914 pm->op_pmflags |= PMf_ONCE;
1920 if (modcount < 10000) {
1921 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
1923 sv_setiv(sv, modcount+1);
1928 op = newBINOP(OP_AASSIGN, flags,
1929 list(force_list(right)),
1930 list(force_list(left)) );
1932 if (!(left->op_flags & OPf_INTRO)) {
1933 static int generation = 0;
1937 for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
1938 if (opargs[curop->op_type] & OA_DANGEROUS) {
1939 if (curop->op_type == OP_GV) {
1940 GV *gv = ((GVOP*)curop)->op_gv;
1941 if (gv == defgv || SvCUR(gv) == generation)
1943 SvCUR(gv) = generation;
1945 else if (curop->op_type == OP_RV2CV)
1947 else if (curop->op_type == OP_RV2SV ||
1948 curop->op_type == OP_RV2AV ||
1949 curop->op_type == OP_RV2HV ||
1950 curop->op_type == OP_RV2GV) {
1951 if (lastop->op_type != OP_GV) /* funny deref? */
1960 op->op_private = OPpASSIGN_COMMON;
1965 right = newOP(OP_UNDEF, 0);
1966 if (right->op_type == OP_READLINE) {
1967 right->op_flags |= OPf_STACKED;
1968 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
1971 op = newBINOP(OP_SASSIGN, flags,
1972 scalar(right), mod(scalar(left), OP_SASSIGN) );
1977 newSTATEOP(flags, label, op)
1984 /* Introduce my variables. */
1985 if (min_intro_pending) {
1986 SV **svp = AvARRAY(comppad_name);
1989 for (i = min_intro_pending; i <= max_intro_pending; i++) {
1991 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
1993 min_intro_pending = 0;
1994 comppad_name_fill = max_intro_pending; /* Needn't search higher */
1997 Newz(1101, cop, 1, COP);
1998 if (perldb && curcop->cop_line && curstash != debstash) {
1999 cop->op_type = OP_DBSTATE;
2000 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2003 cop->op_type = OP_NEXTSTATE;
2004 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2006 cop->op_flags = flags;
2007 cop->op_private = 0;
2008 cop->op_next = (OP*)cop;
2011 cop->cop_label = label;
2012 hints |= HINT_BLOCK_SCOPE;
2014 cop->cop_seq = cop_seqmax++;
2016 if (copline == NOLINE)
2017 cop->cop_line = curcop->cop_line;
2019 cop->cop_line = copline;
2022 cop->cop_filegv = SvREFCNT_inc(curcop->cop_filegv);
2023 cop->cop_stash = curstash;
2025 if (perldb && curstash != debstash) {
2026 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2027 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
2030 SvSTASH(*svp) = (HV*)cop;
2034 return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2038 newLOGOP(type, flags, first, other)
2047 scalarboolean(first);
2048 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2049 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2050 if (type == OP_AND || type == OP_OR) {
2056 first = cUNOP->op_first;
2058 first->op_next = op->op_next;
2059 cUNOP->op_first = Nullop;
2063 if (first->op_type == OP_CONST) {
2064 if (dowarn && (first->op_private & OPpCONST_BARE))
2065 warn("Probable precedence problem on %s", op_name[type]);
2066 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2075 else if (first->op_type == OP_WANTARRAY) {
2085 Newz(1101, logop, 1, LOGOP);
2087 logop->op_type = type;
2088 logop->op_ppaddr = ppaddr[type];
2089 logop->op_first = first;
2090 logop->op_flags = flags | OPf_KIDS;
2091 logop->op_other = LINKLIST(other);
2092 logop->op_private = 1;
2094 /* establish postfix order */
2095 logop->op_next = LINKLIST(first);
2096 first->op_next = (OP*)logop;
2097 first->op_sibling = other;
2099 op = newUNOP(OP_NULL, 0, (OP*)logop);
2100 other->op_next = op;
2106 newCONDOP(flags, first, true, false)
2116 return newLOGOP(OP_AND, 0, first, true);
2118 return newLOGOP(OP_OR, 0, first, false);
2120 scalarboolean(first);
2121 if (first->op_type == OP_CONST) {
2122 if (SvTRUE(((SVOP*)first)->op_sv)) {
2133 else if (first->op_type == OP_WANTARRAY) {
2137 Newz(1101, condop, 1, CONDOP);
2139 condop->op_type = OP_COND_EXPR;
2140 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2141 condop->op_first = first;
2142 condop->op_flags = flags | OPf_KIDS;
2143 condop->op_true = LINKLIST(true);
2144 condop->op_false = LINKLIST(false);
2145 condop->op_private = 1;
2147 /* establish postfix order */
2148 condop->op_next = LINKLIST(first);
2149 first->op_next = (OP*)condop;
2151 first->op_sibling = true;
2152 true->op_sibling = false;
2153 op = newUNOP(OP_NULL, 0, (OP*)condop);
2156 false->op_next = op;
2162 newRANGE(flags, left, right)
2172 Newz(1101, condop, 1, CONDOP);
2174 condop->op_type = OP_RANGE;
2175 condop->op_ppaddr = ppaddr[OP_RANGE];
2176 condop->op_first = left;
2177 condop->op_flags = OPf_KIDS;
2178 condop->op_true = LINKLIST(left);
2179 condop->op_false = LINKLIST(right);
2180 condop->op_private = 1;
2182 left->op_sibling = right;
2184 condop->op_next = (OP*)condop;
2185 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2186 flop = newUNOP(OP_FLOP, 0, flip);
2187 op = newUNOP(OP_NULL, 0, flop);
2190 left->op_next = flip;
2191 right->op_next = flop;
2193 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2194 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
2195 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
2196 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2198 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2199 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2202 if (!flip->op_private || !flop->op_private)
2203 linklist(op); /* blow off optimizer unless constant */
2209 newLOOPOP(flags, debuggable, expr, block)
2217 int once = block && block->op_flags & OPf_SPECIAL &&
2218 (block->op_type == OP_ENTERSUBR || block->op_type == OP_NULL);
2221 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2222 return block; /* do {} while 0 does once */
2223 else if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)
2224 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), expr);
2227 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
2228 op = newLOGOP(OP_AND, 0, expr, listop);
2230 ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2232 if (once && op != listop)
2233 op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2235 op->op_flags |= flags;
2240 newWHILEOP(flags, debuggable, loop, expr, block, cont)
2254 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB))
2255 expr = newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), expr);
2258 block = newOP(OP_NULL, 0);
2261 next = LINKLIST(cont);
2263 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2265 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
2266 redo = LINKLIST(listop);
2269 op = newLOGOP(OP_AND, 0, expr, scalar(listop));
2270 if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2271 op_free(expr); /* oops, it's a while (0) */
2273 return Nullop; /* (listop already freed by newLOGOP) */
2275 ((LISTOP*)listop)->op_last->op_next = condop =
2276 (op == listop ? redo : LINKLIST(op));
2284 Newz(1101,loop,1,LOOP);
2285 loop->op_type = OP_ENTERLOOP;
2286 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2287 loop->op_private = 0;
2288 loop->op_next = (OP*)loop;
2291 op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
2293 loop->op_redoop = redo;
2294 loop->op_lastop = op;
2297 loop->op_nextop = next;
2299 loop->op_nextop = op;
2301 op->op_flags |= flags;
2307 newFOROP(flags,label,forline,sv,expr,block,cont)
2316 newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
2317 #endif /* STANDARD_C */
2324 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
2326 sv = cUNOP->op_first;
2328 cUNOP->op_first = Nullop;
2331 else if (sv->op_type == OP_PADSV) { /* private variable */
2332 padoff = sv->op_targ;
2337 croak("Can't use %s for loop variable", op_name[sv->op_type]);
2340 sv = newGVOP(OP_GV, 0, defgv);
2342 loop = (LOOP*)list(convert(OP_ENTERITER, 0,
2343 append_elem(OP_LIST, force_list(expr), scalar(sv))));
2344 assert(!loop->op_next);
2345 Renew(loop, 1, LOOP);
2346 loop->op_targ = padoff;
2347 return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2348 newOP(OP_ITER, 0), block, cont));
2352 newLOOPEX(type, label)
2357 if (type != OP_GOTO || label->op_type == OP_CONST) {
2358 op = newPVOP(type, 0, savestr(SvPVx(((SVOP*)label)->op_sv, na)));
2362 if (label->op_type == OP_ENTERSUBR)
2363 label = newUNOP(OP_REFGEN, 0, ref(label, OP_REFGEN));
2364 op = newUNOP(type, OPf_STACKED, label);
2366 hints |= HINT_BLOCK_SCOPE;
2374 if (!CvUSERSUB(cv) && CvROOT(cv)) {
2376 if (CvPADLIST(cv)) {
2377 SV** svp = av_fetch(CvPADLIST(cv), 0, FALSE);
2381 comppad = (AV*)*svp; /* Need same context we had compiling */
2382 curpad = AvARRAY(comppad);
2385 op_free(CvROOT(cv));
2386 CvROOT(cv) = Nullop;
2388 warn("Deleting active subroutine"); /* XXX */
2389 if (CvPADLIST(cv)) {
2390 I32 i = AvFILL(CvPADLIST(cv));
2392 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
2396 sv_free(CvPADLIST(cv));
2398 SvREFCNT_dec(CvGV(cv));
2404 newSUB(floor,op,block)
2410 char *name = SvPVx(cSVOP->op_sv, na);
2411 GV *gv = gv_fetchpv(name,2, SVt_PVCV);
2416 if ((cv = GvCV(gv)) && !GvCVGEN(gv)) {
2418 CvDELETED(cv) = TRUE; /* probably an autoloader */
2420 if (dowarn && CvROOT(cv)) {
2421 line_t oldline = curcop->cop_line;
2423 curcop->cop_line = copline;
2424 warn("Subroutine %s redefined",name);
2425 curcop->cop_line = oldline;
2431 sv_upgrade(cv, SVt_PVCV);
2435 CvFILEGV(cv) = curcop->cop_filegv;
2436 CvGV(cv) = SvREFCNT_inc(gv);
2437 CvSTASH(cv) = curstash;
2440 av_store(av, 0, Nullsv);
2441 av_store(comppad, 0, (SV*)av);
2447 if (AvFILL(comppad_name) < AvFILL(comppad))
2448 av_store(comppad_name, AvFILL(comppad), Nullsv);
2449 av_store(av, 0, (SV*)comppad_name);
2450 av_store(av, 1, (SV*)comppad);
2453 comppad_name = newAV();
2462 CvROOT(cv) = newUNOP(OP_LEAVESUBR, 0, scalarseq(block));
2463 CvSTART(cv) = LINKLIST(CvROOT(cv));
2464 CvROOT(cv)->op_next = 0;
2466 CvDELETED(cv) = FALSE;
2467 if (s = strrchr(name,':'))
2471 if (strEQ(s, "BEGIN")) {
2472 line_t oldline = compiling.cop_line;
2475 SAVESPTR(compiling.cop_filegv);
2479 av_push(beginav, (SV *)cv);
2480 DEBUG_x( dump_sub(gv) );
2484 rspara = (nrslen == 2);
2491 curcop = &compiling;
2492 curcop->cop_line = oldline; /* might have recursed to yylex */
2495 else if (strEQ(s, "END")) {
2498 av_unshift(endav, 1);
2499 av_store(endav, 0, SvREFCNT_inc(cv));
2501 if (perldb && curstash != debstash) {
2503 SV *tmpstr = sv_newmortal();
2505 sprintf(buf,"%s:%ld",SvPVX(GvSV(curcop->cop_filegv)), subline);
2506 sv = newSVpv(buf,0);
2508 sprintf(buf,"%ld",(long)curcop->cop_line);
2510 gv_efullname(tmpstr,gv);
2511 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
2519 newXSUB(name, ix, subaddr, filename)
2526 GV *gv = gv_fetchpv(name,2, SVt_PVCV);
2530 if ((cv = GvCV(gv)) && !GvCVGEN(gv)) {
2532 warn("Subroutine %s redefined",name);
2533 if (!CvUSERSUB(cv) && CvROOT(cv)) {
2534 op_free(CvROOT(cv));
2535 CvROOT(cv) = Nullop;
2540 sv_upgrade(cv, SVt_PVCV);
2543 CvGV(cv) = SvREFCNT_inc(gv);
2545 CvFILEGV(cv) = gv_fetchfile(filename);
2546 CvUSERSUB(cv) = subaddr;
2547 CvUSERINDEX(cv) = ix;
2548 CvDELETED(cv) = FALSE;
2549 if (s = strrchr(name,':'))
2553 if (strEQ(s, "BEGIN")) {
2556 av_push(beginav, SvREFCNT_inc(gv));
2558 else if (strEQ(s, "END")) {
2561 av_unshift(endav, 1);
2562 av_store(endav, 0, SvREFCNT_inc(gv));
2567 newFORM(floor,op,block)
2578 name = SvPVx(cSVOP->op_sv, na);
2581 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
2582 if (cv = GvFORM(gv)) {
2584 line_t oldline = curcop->cop_line;
2586 curcop->cop_line = copline;
2587 warn("Format %s redefined",name);
2588 curcop->cop_line = oldline;
2593 sv_upgrade(cv, SVt_PVFM);
2596 CvGV(cv) = SvREFCNT_inc(gv);
2597 CvFILEGV(cv) = curcop->cop_filegv;
2599 CvPADLIST(cv) = av = newAV();
2601 av_store(av, 1, (SV*)comppad);
2604 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
2605 CvSTART(cv) = LINKLIST(CvROOT(cv));
2606 CvROOT(cv)->op_next = 0;
2608 CvDELETED(cv) = FALSE;
2621 Newz(1101, mop, 1, LOGOP);
2622 mop->op_type = OP_METHOD;
2623 mop->op_ppaddr = ppaddr[OP_METHOD];
2624 mop->op_first = scalar(ref);
2625 mop->op_flags |= OPf_KIDS;
2626 mop->op_private = 1;
2627 mop->op_other = LINKLIST(name);
2628 mop->op_targ = pad_alloc(OP_METHOD, SVs_PADTMP);
2629 mop->op_next = LINKLIST(ref);
2630 ref->op_next = (OP*)mop;
2631 return scalar((OP*)mop);
2638 return newUNOP(OP_REFGEN, 0,
2639 ref(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
2646 return newUNOP(OP_REFGEN, 0,
2647 ref(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
2654 switch (o->op_type) {
2656 o->op_type = OP_PADAV;
2657 o->op_ppaddr = ppaddr[OP_PADAV];
2658 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
2661 o->op_type = OP_RV2AV;
2662 o->op_ppaddr = ppaddr[OP_RV2AV];
2667 warn("oops: oopsAV");
2677 switch (o->op_type) {
2680 o->op_type = OP_PADHV;
2681 o->op_ppaddr = ppaddr[OP_PADHV];
2682 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
2686 o->op_type = OP_RV2HV;
2687 o->op_ppaddr = ppaddr[OP_RV2HV];
2692 warn("oops: oopsHV");
2702 if (o->op_type == OP_PADANY) {
2703 o->op_type = OP_PADAV;
2704 o->op_ppaddr = ppaddr[OP_PADAV];
2707 return newUNOP(OP_RV2AV, 0, scalar(o));
2714 return newUNOP(OP_RV2GV, 0, scalar(o));
2721 if (o->op_type == OP_PADANY) {
2722 o->op_type = OP_PADHV;
2723 o->op_ppaddr = ppaddr[OP_PADHV];
2726 return newUNOP(OP_RV2HV, 0, scalar(o));
2733 croak("NOT IMPL LINE %d",__LINE__);
2742 return newUNOP(OP_RV2CV, 0, scalar(o));
2749 if (o->op_type == OP_PADANY) {
2750 o->op_type = OP_PADSV;
2751 o->op_ppaddr = ppaddr[OP_PADSV];
2754 return newUNOP(OP_RV2SV, 0, scalar(o));
2757 /* Check routines. */
2763 if (cUNOP->op_first->op_type == OP_CONCAT)
2764 op->op_flags |= OPf_STACKED;
2772 if (op->op_flags & OPf_KIDS) {
2774 op = modkids(ck_fun(op), op->op_type);
2775 if (op->op_private != 1)
2777 newop = cUNOP->op_first->op_sibling;
2778 if (!newop || newop->op_type != OP_RV2SV)
2780 op_free(cUNOP->op_first);
2781 cUNOP->op_first = newop;
2783 op->op_type = OP_SCHOP;
2784 op->op_ppaddr = ppaddr[OP_SCHOP];
2792 I32 type = op->op_type;
2794 if (op->op_flags & OPf_KIDS) {
2795 if (cLISTOP->op_first->op_type == OP_STUB) {
2797 op = newUNOP(type, OPf_SPECIAL,
2798 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
2809 hints |= HINT_BLOCK_SCOPE;
2810 if (op->op_flags & OPf_KIDS) {
2811 SVOP *kid = (SVOP*)cUNOP->op_first;
2814 op->op_flags &= ~OPf_KIDS;
2817 else if (kid->op_type == OP_LINESEQ) {
2820 kid->op_next = op->op_next;
2821 cUNOP->op_first = 0;
2824 Newz(1101, enter, 1, LOGOP);
2825 enter->op_type = OP_ENTERTRY;
2826 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
2827 enter->op_private = 0;
2829 /* establish postfix order */
2830 enter->op_next = (OP*)enter;
2832 op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
2833 op->op_type = OP_LEAVETRY;
2834 op->op_ppaddr = ppaddr[OP_LEAVETRY];
2835 enter->op_other = op;
2841 op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
2851 if (op->op_flags & OPf_STACKED) {
2853 kid = cUNOP->op_first->op_sibling;
2854 if (kid->op_type == OP_RV2GV)
2866 o = fold_constants(o);
2867 if (o->op_type == OP_CONST)
2876 SVOP *kid = (SVOP*)cUNOP->op_first;
2877 int iscv = (op->op_type==OP_RV2CV);
2879 if (kid->op_type == OP_CONST) {
2880 kid->op_type = OP_GV;
2881 kid->op_sv = SvREFCNT_inc(gv_fetchpv(SvPVx(kid->op_sv, na),
2882 1+iscv, iscv ? SVt_PVCV : SVt_PVGV));
2898 I32 type = op->op_type;
2900 if (op->op_flags & OPf_SPECIAL)
2903 if (op->op_flags & OPf_KIDS) {
2904 SVOP *kid = (SVOP*)cUNOP->op_first;
2906 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
2907 OP *newop = newGVOP(type, OPf_SPECIAL,
2908 gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO));
2915 if (type == OP_FTTTY)
2916 return newGVOP(type, OPf_SPECIAL, gv_fetchpv("main'STDIN", TRUE,
2919 return newUNOP(type, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
2932 register I32 oa = opargs[op->op_type] >> 8;
2934 if (op->op_flags & OPf_STACKED) {
2935 if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL))
2938 return no_fh_allowed(op);
2941 if (op->op_flags & OPf_KIDS) {
2942 tokid = &cLISTOP->op_first;
2943 kid = cLISTOP->op_first;
2944 if (kid->op_type == OP_PUSHMARK ||
2945 kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK)
2947 tokid = &kid->op_sibling;
2948 kid = kid->op_sibling;
2953 sibl = kid->op_sibling;
2967 if (kid->op_type == OP_CONST &&
2968 (kid->op_private & OPpCONST_BARE)) {
2969 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
2970 OP *newop = newAVREF(newGVOP(OP_GV, 0,
2971 gv_fetchpv(name, TRUE, SVt_PVAV) ));
2973 warn("Array @%s missing the @ in argument %d of %s()",
2974 name, numargs, op_name[op->op_type]);
2977 kid->op_sibling = sibl;
2980 else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV)
2981 bad_type(numargs, "array", op, kid);
2982 mod(kid, op->op_type);
2985 if (kid->op_type == OP_CONST &&
2986 (kid->op_private & OPpCONST_BARE)) {
2987 char *name = SvPVx(((SVOP*)kid)->op_sv, na);
2988 OP *newop = newHVREF(newGVOP(OP_GV, 0,
2989 gv_fetchpv(name, TRUE, SVt_PVHV) ));
2991 warn("Hash %%%s missing the %% in argument %d of %s()",
2992 name, numargs, op_name[op->op_type]);
2995 kid->op_sibling = sibl;
2998 else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV)
2999 bad_type(numargs, "hash", op, kid);
3000 mod(kid, op->op_type);
3004 OP *newop = newUNOP(OP_NULL, 0, scalar(kid));
3005 kid->op_sibling = 0;
3007 newop->op_next = newop;
3009 kid->op_sibling = sibl;
3014 if (kid->op_type != OP_GV) {
3015 if (kid->op_type == OP_CONST &&
3016 (kid->op_private & OPpCONST_BARE)) {
3017 OP *newop = newGVOP(OP_GV, 0,
3018 gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE,
3024 kid->op_sibling = 0;
3025 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3027 kid->op_sibling = sibl;
3033 mod(scalar(kid), op->op_type);
3037 tokid = &kid->op_sibling;
3038 kid = kid->op_sibling;
3040 op->op_private = numargs;
3042 return too_many_arguments(op);
3046 while (oa & OA_OPTIONAL)
3048 if (oa && oa != OA_LIST)
3049 return too_few_arguments(op);
3058 GV *gv = newGVgen();
3060 append_elem(OP_GLOB, op, newGVOP(OP_GV, 0, gv));
3072 if (op->op_flags & OPf_STACKED) {
3074 op->op_flags &= ~OPf_STACKED;
3079 kid = cLISTOP->op_first->op_sibling;
3080 if (kid->op_type != OP_NULL)
3081 croak("panic: ck_grep");
3082 kid = kUNOP->op_first;
3084 Newz(1101, gwop, 1, LOGOP);
3085 gwop->op_type = OP_GREPWHILE;
3086 gwop->op_ppaddr = ppaddr[OP_GREPWHILE];
3087 gwop->op_first = list(op);
3088 gwop->op_flags |= OPf_KIDS;
3089 gwop->op_private = 1;
3090 gwop->op_other = LINKLIST(kid);
3091 gwop->op_targ = pad_alloc(OP_GREPWHILE, SVs_PADTMP);
3092 kid->op_next = (OP*)gwop;
3101 if (op->op_flags & OPf_KIDS) {
3102 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3103 if (kid && kid->op_type == OP_CONST)
3104 fbm_compile(((SVOP*)kid)->op_sv, 0);
3113 /* XXX length optimization goes here */
3121 return modkids(ck_fun(op), op->op_type);
3128 return refkids(ck_fun(op), op->op_type);
3137 kid = cLISTOP->op_first;
3139 op = force_list(op);
3140 kid = cLISTOP->op_first;
3142 if (kid->op_type == OP_PUSHMARK)
3143 kid = kid->op_sibling;
3144 if (kid && op->op_flags & OPf_STACKED)
3145 kid = kid->op_sibling;
3146 else if (kid && !kid->op_sibling) { /* print HANDLE; */
3147 if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) {
3148 op->op_flags |= OPf_STACKED; /* make it a filehandle */
3149 kid = newUNOP(OP_RV2GV, 0, scalar(kid));
3150 cLISTOP->op_first->op_sibling = kid;
3151 cLISTOP->op_last = kid;
3152 kid = kid->op_sibling;
3157 append_elem(op->op_type, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3159 return listkids(op);
3166 cPMOP->op_pmflags |= PMf_RUNTIME;
3181 if (cBINOP->op_first->op_flags & OPf_PARENS) {
3182 op->op_private = OPpREPEAT_DOLIST;
3183 cBINOP->op_first = force_list(cBINOP->op_first);
3194 if (op->op_flags & OPf_KIDS) { /* Shall we fake a BEGIN {}? */
3195 SVOP *kid = (SVOP*)cUNOP->op_first;
3197 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
3198 char *name = SvPVX(subname);
3200 sv_catpvn(kid->op_sv, ".pm", 3);
3201 if (s = strrchr(name,':'))
3205 if (strNE(s, "BEGIN")) {
3206 op = newSTATEOP(0, Nullch, op);
3207 newSUB(start_subparse(),
3208 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
3210 return newOP(OP_STUB,0);
3221 croak("NOT IMPL LINE %d",__LINE__);
3230 if (op->op_flags & OPf_KIDS) {
3231 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3232 if (kid && kid->op_sibling) {
3233 op->op_type = OP_SSELECT;
3234 op->op_ppaddr = ppaddr[OP_SSELECT];
3236 return fold_constants(op);
3246 I32 type = op->op_type;
3248 if (!(op->op_flags & OPf_KIDS)) {
3250 return newUNOP(type, 0,
3251 scalar(newUNOP(OP_RV2AV, 0,
3252 scalar(newGVOP(OP_GV, 0,
3253 gv_fetchpv((subline ? "_" : "ARGV"), TRUE, SVt_PVAV) )))));
3255 return scalar(modkids(ck_fun(op), type));
3262 if (op->op_flags & OPf_STACKED) {
3263 OP *kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3265 kid = kUNOP->op_first; /* get past rv2gv */
3267 if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) {
3269 if (kid->op_type == OP_SCOPE) {
3274 else if (kid->op_type == OP_LEAVE) {
3275 null(kid); /* wipe out leave */
3278 for (k = kLISTOP->op_first->op_next; k; k = k->op_next) {
3279 if (k->op_next == kid)
3282 peep(kLISTOP->op_first);
3284 kid = cLISTOP->op_first->op_sibling; /* get past pushmark */
3285 null(kid); /* wipe out rv2gv */
3287 op->op_flags |= OPf_SPECIAL;
3300 if (op->op_flags & OPf_STACKED)
3301 return no_fh_allowed(op);
3303 kid = cLISTOP->op_first;
3304 if (kid->op_type != OP_NULL)
3305 croak("panic: ck_split");
3306 kid = kid->op_sibling;
3307 op_free(cLISTOP->op_first);
3308 cLISTOP->op_first = kid;
3310 cLISTOP->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1));
3311 cLISTOP->op_last = kid; /* There was only one element previously */
3314 if (kid->op_type != OP_MATCH) {
3315 OP *sibl = kid->op_sibling;
3316 kid->op_sibling = 0;
3317 kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop);
3318 if (cLISTOP->op_first == cLISTOP->op_last)
3319 cLISTOP->op_last = kid;
3320 cLISTOP->op_first = kid;
3321 kid->op_sibling = sibl;
3324 if (pm->op_pmshort && !(pm->op_pmflags & PMf_ALL)) {
3325 SvREFCNT_dec(pm->op_pmshort); /* can't use substring to optimize */
3329 kid->op_type = OP_PUSHRE;
3330 kid->op_ppaddr = ppaddr[OP_PUSHRE];
3333 if (!kid->op_sibling)
3334 append_elem(OP_SPLIT, op, newSVREF(newGVOP(OP_GV, 0, defgv)) );
3336 kid = kid->op_sibling;
3339 if (!kid->op_sibling)
3340 append_elem(OP_SPLIT, op, newSVOP(OP_CONST, 0, newSViv(0)));
3342 kid = kid->op_sibling;
3345 if (kid->op_sibling)
3346 return too_many_arguments(op);
3355 OP *o = ((cUNOP->op_first->op_sibling)
3356 ? cUNOP : ((UNOP*)cUNOP->op_first))->op_first->op_sibling;
3358 if (o->op_type == OP_RV2CV)
3359 null(o); /* disable rv2cv */
3360 op->op_private = (hints & HINT_STRICT_REFS);
3361 if (perldb && curstash != debstash)
3362 op->op_private |= OPpDEREF_DB;
3370 SvREADONLY_on(cSVOP->op_sv);
3378 if (op->op_flags & OPf_KIDS) {
3379 SVOP *kid = (SVOP*)cUNOP->op_first;
3381 if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE))
3382 op->op_flags |= OPf_SPECIAL;
3387 /* A peephole optimizer. We visit the ops in the order they're to execute. */
3393 register OP* oldop = 0;
3394 if (!op || op->op_seq)
3396 for (; op; op = op->op_next) {
3399 switch (op->op_type) {
3401 if ((op->op_flags & (OPf_KNOW|OPf_LIST)) != (OPf_KNOW|OPf_LIST)) {
3402 op->op_seq = ++op_seqmax;
3403 break; /* Scalar stub must produce undef. List stub is noop */
3411 oldop->op_next = op->op_next;
3414 op->op_seq = ++op_seqmax;
3418 if (op->op_next->op_type == OP_RV2SV) {
3419 if (!(op->op_next->op_private & (OPpDEREF_HV|OPpDEREF_AV))) {
3421 op->op_flags |= op->op_next->op_flags & OPf_INTRO;
3422 op->op_next = op->op_next->op_next;
3423 op->op_type = OP_GVSV;
3424 op->op_ppaddr = ppaddr[OP_GVSV];
3427 else if (op->op_next->op_type == OP_RV2AV) {
3428 OP* pop = op->op_next->op_next;
3430 if (pop->op_type == OP_CONST &&
3431 pop->op_next->op_type == OP_AELEM &&
3432 !(pop->op_next->op_private & (OPpDEREF_HV|OPpDEREF_AV)) &&
3433 !(pop->op_next->op_flags & OPf_INTRO) &&
3434 (i = SvIV(((SVOP*)pop)->op_sv)) <= 255 &&
3440 op->op_flags &= ~OPf_LVAL;
3441 op->op_flags |= pop->op_next->op_flags & OPf_LVAL;
3442 op->op_next = pop->op_next->op_next;
3443 op->op_type = OP_AELEMFAST;
3444 op->op_ppaddr = ppaddr[OP_AELEMFAST];
3446 GvAVn((GV*)cSVOP->op_sv);
3449 op->op_seq = ++op_seqmax;
3455 op->op_seq = ++op_seqmax;
3456 peep(cLOGOP->op_other);
3460 op->op_seq = ++op_seqmax;
3461 peep(cCONDOP->op_true);
3462 peep(cCONDOP->op_false);
3466 op->op_seq = ++op_seqmax;
3467 peep(cLOOP->op_redoop);
3468 peep(cLOOP->op_nextop);
3469 peep(cLOOP->op_lastop);
3474 op->op_seq = ++op_seqmax;
3475 peep(cPMOP->op_pmreplroot);
3479 op->op_seq = ++op_seqmax;