1 /* $Header: cons.c,v 3.0.1.9 90/11/10 01:10:50 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.9 90/11/10 01:10:50 lwall
10 * patch38: random cleanup
12 * Revision 3.0.1.8 90/10/15 15:41:09 lwall
13 * patch29: added caller
14 * patch29: scripts now run at almost full speed under the debugger
15 * patch29: the debugger now understands packages and evals
16 * patch29: package behavior is now more consistent
18 * Revision 3.0.1.7 90/08/09 02:35:52 lwall
19 * patch19: did preliminary work toward debugging packages and evals
20 * patch19: Added support for linked-in C subroutines
21 * patch19: Numeric literals are now stored only in floating point
22 * patch19: Added -c switch to do compilation only
24 * Revision 3.0.1.6 90/03/27 15:35:21 lwall
25 * patch16: formats didn't work inside eval
26 * patch16: $foo++ now optimized to ++$foo where value not required
28 * Revision 3.0.1.5 90/03/12 16:23:10 lwall
29 * patch13: perl -d coredumped on scripts with subs that did explicit return
31 * Revision 3.0.1.4 90/02/28 16:44:00 lwall
32 * patch9: subs which return by both mechanisms can clobber local return data
33 * patch9: changed internal SUB label to _SUB_
34 * patch9: line numbers were bogus during certain portions of foreach evaluation
36 * Revision 3.0.1.3 89/12/21 19:20:25 lwall
37 * patch7: made nested or recursive foreach work right
39 * Revision 3.0.1.2 89/11/17 15:08:53 lwall
40 * patch5: nested foreach on same array didn't work
42 * Revision 3.0.1.1 89/10/26 23:09:01 lwall
43 * patch1: numeric switch optimization was broken
44 * patch1: unless was broken when run under the debugger
46 * Revision 3.0 89/10/18 15:10:23 lwall
55 extern char *tokename[];
58 static int cmd_tosave();
59 static int arg_tosave();
60 static int spat_tosave();
62 static bool saw_return;
70 STAB *stab = stabent(name,TRUE);
75 CMD *oldcurcmd = curcmd;
79 warn("Subroutine %s redefined",name);
82 if (stab_sub(stab)->cmd) {
83 cmd_free(stab_sub(stab)->cmd);
84 afree(stab_sub(stab)->tosave);
86 Safefree(stab_sub(stab));
88 sub->filestab = curcmd->c_filestab;
90 tosave = anew(Nullstab);
91 tosave->ary_fill = 0; /* make 1 based */
92 (void)cmd_tosave(cmd,FALSE); /* this builds the tosave array */
95 struct compcmd mycompblock;
97 mycompblock.comp_true = cmd;
98 mycompblock.comp_alt = Nullcmd;
99 cmd = add_label(savestr("_SUB_"),make_ccmd(C_BLOCK,Nullarg,mycompblock));
101 cmd->c_flags |= CF_TERM;
104 stab_sub(stab) = sub;
107 STR *tmpstr = str_static(&str_undef);
109 sprintf(buf,"%s:%ld",stab_val(curcmd->c_filestab)->str_ptr,
111 str = str_make(buf,0);
113 sprintf(buf,"%ld",(long)curcmd->c_line);
115 name = str_get(subname);
116 stab_fullname(tmpstr,stab);
117 hstore(stab_xhash(DBsub), tmpstr->str_ptr, tmpstr->str_cur, str, 0);
118 str_set(subname,"main");
125 make_usub(name, ix, subaddr, filename)
132 STAB *stab = stabent(name,allstabs);
134 if (!stab) /* unused function */
136 Newz(101,sub,1,SUBR);
137 if (stab_sub(stab)) {
139 warn("Subroutine %s redefined",name);
140 if (stab_sub(stab)->cmd) {
141 cmd_free(stab_sub(stab)->cmd);
142 afree(stab_sub(stab)->tosave);
144 Safefree(stab_sub(stab));
146 sub->filestab = fstab(filename);
147 sub->usersub = subaddr;
149 stab_sub(stab) = sub;
157 if (stab_form(stab)) {
161 for (tmpfcmd = stab_form(stab); tmpfcmd; tmpfcmd = nextfcmd) {
162 nextfcmd = tmpfcmd->f_next;
164 arg_free(tmpfcmd->f_expr);
165 if (tmpfcmd->f_unparsed)
166 str_free(tmpfcmd->f_unparsed);
168 Safefree(tmpfcmd->f_pre);
172 stab_form(stab) = fcmd;
181 register int last_opt = 0;
182 register STAB *last_stab = Nullstab;
183 register int count = 0;
184 register CMD *switchbeg = Nullcmd;
186 if (tail == Nullcmd) {
191 for (tail = head; tail; tail = tail->c_next) {
193 /* save one measly dereference at runtime */
194 if (tail->c_type == C_IF) {
195 if (!(tail->ucmd.ccmd.cc_alt = tail->ucmd.ccmd.cc_alt->c_next))
196 tail->c_flags |= CF_TERM;
198 else if (tail->c_type == C_EXPR) {
201 if (tail->ucmd.acmd.ac_expr)
202 arg = tail->ucmd.acmd.ac_expr;
206 if (arg->arg_type == O_RETURN)
207 tail->c_flags |= CF_TERM;
208 else if (arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
209 tail->c_flags |= CF_TERM;
213 tail->c_flags |= CF_TERM;
215 if (tail->c_expr && (tail->c_flags & CF_OPTIMIZE) == CFT_FALSE)
216 opt_arg(tail,1, tail->c_type == C_EXPR);
218 /* now do a little optimization on case-ish structures */
219 switch(tail->c_flags & (CF_OPTIMIZE|CF_FIRSTNEG|CF_INVERT)) {
221 if (stabent("*",FALSE)) { /* bad assumption here!!! */
227 opt = (tail->c_flags & CF_NESURE) ? CFT_STROP : 0;
233 opt = (tail->c_slen == O_NE ? 0 : CFT_NUMOP);
234 if ((tail->c_flags&(CF_NESURE|CF_EQSURE)) != (CF_NESURE|CF_EQSURE))
240 if (opt && opt == last_opt && tail->c_stab == last_stab)
243 if (count >= 3) { /* is this the breakeven point? */
244 if (last_opt == CFT_NUMOP)
245 make_nswitch(switchbeg,count);
247 make_cswitch(switchbeg,count);
257 last_stab = tail->c_stab;
259 if (count >= 3) { /* is this the breakeven point? */
260 if (last_opt == CFT_NUMOP)
261 make_nswitch(switchbeg,count);
263 make_cswitch(switchbeg,count);
268 /* We've spotted a sequence of CMDs that all test the value of the same
269 * spat. Thus we can insert a SWITCH in front and jump directly
270 * to the correct one.
272 make_cswitch(head,count)
279 register int min = 255;
280 register int max = 0;
282 /* make a new head in the exact same spot */
283 New(102,cur, 1, CMD);
287 Copy(head,cur,1,CMD);
290 head->c_type = C_CSWITCH;
291 head->c_next = cur; /* insert new cmd at front of list */
292 head->c_stab = cur->c_stab;
294 Newz(103,loc,258,CMD*);
295 loc++; /* lie a little */
297 if ((cur->c_flags & CF_OPTIMIZE) == CFT_CCLASS) {
298 for (i = 0; i <= 255; i++) {
299 if (!loc[i] && cur->c_short->str_ptr[i>>3] & (1 << (i&7))) {
309 i = *cur->c_short->str_ptr & 255;
322 Copy(&loc[min],&loc[0], max - min, CMD*);
326 for (i = 0; i <= max; i++)
329 Renew(loc,max+1,CMD*); /* chop it down to size */
330 head->ucmd.scmd.sc_offset = min;
331 head->ucmd.scmd.sc_max = max;
332 head->ucmd.scmd.sc_next = loc;
335 make_nswitch(head,count)
339 register CMD *cur = head;
342 register int min = 32767;
343 register int max = -32768;
344 int origcount = count;
345 double value; /* or your money back! */
346 short changed; /* so triple your money back! */
349 i = (int)str_gnum(cur->c_short);
351 if (value != cur->c_short->str_u.str_nval)
352 return; /* fractional values--just forget it */
355 return; /* too big for a short */
356 if (cur->c_slen == O_LE)
358 else if (cur->c_slen == O_GE) /* we only do < or > here */
367 if (max - min > count * 2 + 10) /* too sparse? */
370 /* now make a new head in the exact same spot */
371 New(104,cur, 1, CMD);
375 Copy(head,cur,1,CMD);
378 head->c_type = C_NSWITCH;
379 head->c_next = cur; /* insert new cmd at front of list */
380 head->c_stab = cur->c_stab;
382 Newz(105,loc, max - min + 3, CMD*);
387 i = (int)str_gnum(cur->c_short);
389 switch(cur->c_slen) {
393 for (i--; i >= -1; i--)
400 for (i++; i <= max; i++)
414 for (i = 0; i <= max; i++)
417 head->ucmd.scmd.sc_offset = min;
418 head->ucmd.scmd.sc_max = max;
419 head->ucmd.scmd.sc_next = loc;
423 append_line(head,tail)
429 if (!tail->c_head) /* make sure tail is well formed */
431 if (head != Nullcmd) {
432 tail = tail->c_head; /* get to start of tail list */
434 head->c_head = head; /* start a new head list */
435 while (head->c_next) {
436 head->c_next->c_head = head->c_head;
437 head = head->c_next; /* get to end of head list */
439 head->c_next = tail; /* link to end of old list */
440 tail->c_head = head->c_head; /* propagate head pointer */
442 while (tail->c_next) {
443 tail->c_next->c_head = tail->c_head;
454 register CMD *head = cur->c_head;
461 str = afetch(stab_xarray(curcmd->c_filestab),(int)head->c_line,FALSE);
462 if (str == &str_undef || str->str_nok)
464 str->str_u.str_nval = (double)head->c_line;
467 str_magic(str, curcmd->c_filestab, 0, Nullch, 0);
468 str->str_magic->str_u.str_cmd = cmd;
469 cmd->c_type = C_EXPR;
470 cmd->ucmd.acmd.ac_stab = Nullstab;
471 cmd->ucmd.acmd.ac_expr = Nullarg;
472 cmd->c_expr = make_op(O_SUBR, 1,
473 stab2arg(A_WORD,DBstab),
476 cmd->c_flags |= CF_COND|CF_DBSUB|CFT_D0;
477 cmd->c_line = head->c_line;
478 cmd->c_label = head->c_label;
479 cmd->c_filestab = curcmd->c_filestab;
480 cmd->c_stash = curstash;
481 return append_line(cmd, cur);
485 make_acmd(type,stab,cond,arg)
495 cmd->ucmd.acmd.ac_stab = stab;
496 cmd->ucmd.acmd.ac_expr = arg;
499 cmd->c_flags |= CF_COND;
500 if (cmdline == NOLINE)
501 cmd->c_line = curcmd->c_line;
503 cmd->c_line = cmdline;
506 cmd->c_filestab = curcmd->c_filestab;
507 cmd->c_stash = curstash;
514 make_ccmd(type,arg,cblock)
517 struct compcmd cblock;
521 Newz(108,cmd, 1, CMD);
524 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
525 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
527 cmd->c_flags |= CF_COND;
528 if (cmdline == NOLINE)
529 cmd->c_line = curcmd->c_line;
531 cmd->c_line = cmdline;
534 cmd->c_filestab = curcmd->c_filestab;
535 cmd->c_stash = curstash;
542 make_icmd(type,arg,cblock)
545 struct compcmd cblock;
551 struct compcmd ncblock;
553 Newz(109,cmd, 1, CMD);
557 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
558 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
560 cmd->c_flags |= CF_COND;
561 if (cmdline == NOLINE)
562 cmd->c_line = curcmd->c_line;
564 cmd->c_line = cmdline;
567 cmd->c_filestab = curcmd->c_filestab;
568 cmd->c_stash = curstash;
570 alt = cblock.comp_alt;
571 while (alt && alt->c_type == C_ELSIF) {
573 alt = alt->ucmd.ccmd.cc_alt;
575 if (alt) { /* a real life ELSE at the end? */
576 ncblock.comp_true = alt;
577 ncblock.comp_alt = Nullcmd;
578 alt = append_line(cur,make_ccmd(C_ELSE,Nullarg,ncblock));
579 cur->ucmd.ccmd.cc_alt = alt;
582 alt = cur; /* no ELSE, so cur is proxy ELSE */
585 while (cmd) { /* now point everyone at the ELSE */
587 cmd = cur->ucmd.ccmd.cc_alt;
589 if (cur->c_type == C_ELSIF)
591 if (cur->c_type == C_IF)
592 cur->ucmd.ccmd.cc_alt = alt;
603 opt_arg(cmd,fliporflop,acmd)
612 int context = 0; /* 0 = normal, 1 = before &&, 2 = before || */
613 int flp = fliporflop;
617 if (!(arg = cmd->c_expr)) {
618 cmd->c_flags &= ~CF_COND;
622 /* Can we turn && and || into if and unless? */
624 if (acmd && !cmd->ucmd.acmd.ac_expr && !(cmd->c_flags & CF_TERM) &&
625 (arg->arg_type == O_AND || arg->arg_type == O_OR) ) {
627 arg[2].arg_type &= A_MASK; /* don't suppress eval */
629 cmd->ucmd.acmd.ac_expr = arg[2].arg_ptr.arg_arg;
630 cmd->c_expr = arg[1].arg_ptr.arg_arg;
631 if (arg->arg_type == O_OR)
632 cmd->c_flags ^= CF_INVERT; /* || is like unless */
638 /* Turn "if (!expr)" into "unless (expr)" */
640 if (!(cmd->c_flags & CF_TERM)) { /* unless return value wanted */
641 while (arg->arg_type == O_NOT) {
643 cmd->c_flags ^= CF_INVERT; /* flip sense of cmd */
644 cmd->c_expr = arg[1].arg_ptr.arg_arg; /* hoist the rest of expr */
646 arg = cmd->c_expr; /* here we go again */
650 if (!arg->arg_len) { /* sanity check */
655 /* for "cond .. cond" we set up for the initial check */
657 if (arg->arg_type == O_FLIP)
660 /* for "cond && expr" and "cond || expr" we can ignore expr, sort of */
663 if (arg->arg_type == O_AND)
665 else if (arg->arg_type == O_OR)
667 if (context && (arg[flp].arg_type & A_MASK) == A_EXPR) {
668 arg = arg[flp].arg_ptr.arg_arg;
670 if (arg->arg_type == O_AND || arg->arg_type == O_OR)
673 if ((context & 3) == 3)
676 if (arg[flp].arg_flags & (AF_PRE|AF_POST)) {
678 if (acmd && !cmd->ucmd.acmd.ac_expr && !(cmd->c_flags & CF_TERM)) {
679 arg[flp].arg_flags &= ~AF_POST; /* prefer ++$foo to $foo++ */
680 arg[flp].arg_flags |= AF_PRE; /* if value not wanted */
682 return; /* side effect, can't optimize */
685 if (arg->arg_type == O_ITEM || arg->arg_type == O_FLIP ||
686 arg->arg_type == O_AND || arg->arg_type == O_OR) {
687 if ((arg[flp].arg_type & A_MASK) == A_SINGLE) {
688 opt = (str_true(arg[flp].arg_ptr.arg_str) ? CFT_TRUE : CFT_FALSE);
689 cmd->c_short = str_smake(arg[flp].arg_ptr.arg_str);
692 else if ((arg[flp].arg_type & A_MASK) == A_STAB ||
693 (arg[flp].arg_type & A_MASK) == A_LVAL) {
694 cmd->c_stab = arg[flp].arg_ptr.arg_stab;
697 if (!context) { /* no && or ||? */
699 cmd->c_expr = Nullarg;
702 cmd->c_flags |= CF_EQSURE;
704 cmd->c_flags |= CF_NESURE;
707 else if (arg->arg_type == O_MATCH || arg->arg_type == O_SUBST ||
708 arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST) {
709 if ((arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
710 (arg[2].arg_type & A_MASK) == A_SPAT &&
711 arg[2].arg_ptr.arg_spat->spat_short ) {
712 cmd->c_stab = arg[1].arg_ptr.arg_stab;
713 cmd->c_short = str_smake(arg[2].arg_ptr.arg_spat->spat_short);
714 cmd->c_slen = arg[2].arg_ptr.arg_spat->spat_slen;
715 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ALL &&
716 !(arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ONCE) &&
717 (arg->arg_type == O_MATCH || arg->arg_type == O_NMATCH) )
718 sure |= CF_EQSURE; /* (SUBST must be forced even */
719 /* if we know it will work.) */
720 if (arg->arg_type != O_SUBST) {
721 arg[2].arg_ptr.arg_spat->spat_short = Nullstr;
722 arg[2].arg_ptr.arg_spat->spat_slen = 0; /* only one chk */
724 sure |= CF_NESURE; /* normally only sure if it fails */
725 if (arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST)
726 cmd->c_flags |= CF_FIRSTNEG;
727 if (context & 1) { /* only sure if thing is false */
728 if (cmd->c_flags & CF_FIRSTNEG)
733 else if (context & 2) { /* only sure if thing is true */
734 if (cmd->c_flags & CF_FIRSTNEG)
739 if (sure & (CF_EQSURE|CF_NESURE)) { /* if we know anything*/
740 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_SCANFIRST)
744 if (sure == (CF_EQSURE|CF_NESURE) /* really sure? */
745 && arg->arg_type == O_MATCH
747 && fliporflop == 1) {
748 spat_free(arg[2].arg_ptr.arg_spat);
749 arg[2].arg_ptr.arg_spat = Nullspat; /* don't do twice */
751 cmd->c_flags |= sure;
755 else if (arg->arg_type == O_SEQ || arg->arg_type == O_SNE ||
756 arg->arg_type == O_SLT || arg->arg_type == O_SGT) {
757 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
758 if (arg[2].arg_type == A_SINGLE) {
759 char *junk = str_get(arg[2].arg_ptr.arg_str);
761 cmd->c_stab = arg[1].arg_ptr.arg_stab;
762 cmd->c_short = str_smake(arg[2].arg_ptr.arg_str);
763 cmd->c_slen = cmd->c_short->str_cur+1;
764 switch (arg->arg_type) {
765 case O_SLT: case O_SGT:
767 cmd->c_flags |= CF_FIRSTNEG;
770 cmd->c_flags |= CF_FIRSTNEG;
773 sure |= CF_NESURE|CF_EQSURE;
776 if (context & 1) { /* only sure if thing is false */
777 if (cmd->c_flags & CF_FIRSTNEG)
782 else if (context & 2) { /* only sure if thing is true */
783 if (cmd->c_flags & CF_FIRSTNEG)
788 if (sure & (CF_EQSURE|CF_NESURE)) {
790 cmd->c_flags |= sure;
795 else if (arg->arg_type == O_EQ || arg->arg_type == O_NE ||
796 arg->arg_type == O_LE || arg->arg_type == O_GE ||
797 arg->arg_type == O_LT || arg->arg_type == O_GT) {
798 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
799 if (arg[2].arg_type == A_SINGLE) {
800 cmd->c_stab = arg[1].arg_ptr.arg_stab;
802 STR *str = arg[2].arg_ptr.arg_str;
804 if ((!str->str_nok && !looks_like_number(str)))
805 warn("Possible use of == on string value");
807 cmd->c_short = str_nmake(str_gnum(arg[2].arg_ptr.arg_str));
808 cmd->c_slen = arg->arg_type;
809 sure |= CF_NESURE|CF_EQSURE;
810 if (context & 1) { /* only sure if thing is false */
813 else if (context & 2) { /* only sure if thing is true */
816 if (sure & (CF_EQSURE|CF_NESURE)) {
818 cmd->c_flags |= sure;
823 else if (arg->arg_type == O_ASSIGN &&
824 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
825 arg[1].arg_ptr.arg_stab == defstab &&
826 arg[2].arg_type == A_EXPR ) {
827 arg2 = arg[2].arg_ptr.arg_arg;
828 if (arg2->arg_type == O_ITEM && arg2[1].arg_type == A_READ) {
830 cmd->c_stab = arg2[1].arg_ptr.arg_stab;
831 if (!(stab_io(arg2[1].arg_ptr.arg_stab)->flags & IOF_ARGV)) {
834 cmd->c_expr = Nullarg;
838 else if (arg->arg_type == O_CHOP &&
839 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) ) {
841 cmd->c_stab = arg[1].arg_ptr.arg_stab;
843 cmd->c_expr = Nullarg;
849 if (cmd->c_flags & CF_FLIP) {
850 if (fliporflop == 1) {
851 arg = cmd->c_expr; /* get back to O_FLIP arg */
852 New(110,arg[3].arg_ptr.arg_cmd, 1, CMD);
853 Copy(cmd, arg[3].arg_ptr.arg_cmd, 1, CMD);
854 New(111,arg[4].arg_ptr.arg_cmd,1,CMD);
855 Copy(cmd, arg[4].arg_ptr.arg_cmd, 1, CMD);
856 opt_arg(arg[4].arg_ptr.arg_cmd,2,acmd);
857 arg->arg_len = 2; /* this is a lie */
860 if ((opt & CF_OPTIMIZE) == CFT_EVAL)
861 cmd->c_flags = (cmd->c_flags & ~CF_OPTIMIZE) | CFT_UNFLIP;
882 cmd->c_flags |= CF_COND;
894 cmd->c_flags |= CF_COND|CF_LOOP;
896 if (!(cmd->c_flags & CF_INVERT))
897 while_io(cmd); /* add $_ =, if necessary */
899 if (cmd->c_type == C_BLOCK)
900 cmd->c_flags &= ~CF_COND;
902 arg = cmd->ucmd.acmd.ac_expr;
903 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
904 cmd->c_flags &= ~CF_COND; /* "do {} while" happens at least once */
905 if (arg && arg->arg_type == O_SUBR)
906 cmd->c_flags &= ~CF_COND; /* likewise for "do subr() while" */
915 register CMD *targ = cmd;
918 if (targ->c_flags & CF_DBSUB)
920 targ->c_flags ^= CF_INVERT;
929 char *tname = tmpbuf;
931 if (bufptr > oldoldbufptr && bufptr - oldoldbufptr < 200 &&
932 oldoldbufptr != oldbufptr && oldbufptr != bufptr) {
933 while (isspace(*oldoldbufptr))
935 strncpy(tmp2buf, oldoldbufptr, bufptr - oldoldbufptr);
936 tmp2buf[bufptr - oldoldbufptr] = '\0';
937 sprintf(tname,"next 2 tokens \"%s\"",tmp2buf);
939 else if (bufptr > oldbufptr && bufptr - oldbufptr < 200 &&
940 oldbufptr != bufptr) {
941 while (isspace(*oldbufptr))
943 strncpy(tmp2buf, oldbufptr, bufptr - oldbufptr);
944 tmp2buf[bufptr - oldbufptr] = '\0';
945 sprintf(tname,"next token \"%s\"",tmp2buf);
947 else if (yychar > 256)
948 tname = "next token ???";
950 (void)strcpy(tname,"at EOF");
951 else if (yychar < 32)
952 (void)sprintf(tname,"next char ^%c",yychar+64);
953 else if (yychar == 127)
954 (void)strcpy(tname,"at EOF");
956 (void)sprintf(tname,"next char %c",yychar);
957 (void)sprintf(buf, "%s in file %s at line %d, %s\n",
958 s,stab_val(curcmd->c_filestab)->str_ptr,curcmd->c_line,tname);
959 if (curcmd->c_line == multi_end && multi_start < multi_end)
960 sprintf(buf+strlen(buf),
961 " (Might be a runaway multi-line %c%c string starting on line %d)\n",
962 multi_open,multi_close,multi_start);
964 str_cat(stab_val(stabent("@",TRUE)),buf);
967 if (++error_count >= 10)
968 fatal("%s has too many errors.\n",
969 stab_val(curcmd->c_filestab)->str_ptr);
976 register ARG *arg = cmd->c_expr;
979 /* hoist "while (<channel>)" up into command block */
981 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_READ) {
982 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
983 cmd->c_flags |= CFT_GETS; /* and set it to do the input */
984 cmd->c_stab = arg[1].arg_ptr.arg_stab;
985 if (stab_io(arg[1].arg_ptr.arg_stab)->flags & IOF_ARGV) {
986 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$_ =" */
987 stab2arg(A_LVAL,defstab), arg, Nullarg));
991 cmd->c_expr = Nullarg;
994 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_INDREAD) {
995 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
996 cmd->c_flags |= CFT_INDGETS; /* and set it to do the input */
997 cmd->c_stab = arg[1].arg_ptr.arg_stab;
999 cmd->c_expr = Nullarg;
1001 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_GLOB) {
1002 if ((cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY)
1003 asgnstab = cmd->c_stab;
1006 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$foo =" */
1007 stab2arg(A_LVAL,asgnstab), arg, Nullarg));
1008 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
1020 if (cmd->c_expr && (cmd->c_flags & CF_OPTIMIZE) == CFT_FALSE)
1021 opt_arg(cmd,1, cmd->c_type == C_EXPR);
1023 while_io(cmd); /* add $_ =, if necessary */
1025 /* First find the end of the true list */
1027 tail = cmd->ucmd.ccmd.cc_true;
1028 if (tail == Nullcmd)
1030 New(112,newtail, 1, CMD); /* guaranteed continue */
1032 /* optimize "next" to point directly to continue block */
1033 if (tail->c_type == C_EXPR &&
1034 tail->ucmd.acmd.ac_expr &&
1035 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
1036 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
1039 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
1041 arg_free(tail->ucmd.acmd.ac_expr);
1042 tail->c_type = C_NEXT;
1043 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
1044 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
1046 tail->ucmd.ccmd.cc_alt = newtail;
1047 tail->ucmd.ccmd.cc_true = Nullcmd;
1049 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
1050 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
1051 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
1053 tail->ucmd.ccmd.cc_alt = newtail;
1055 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
1056 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
1057 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
1058 if (!tail->ucmd.scmd.sc_next[i])
1059 tail->ucmd.scmd.sc_next[i] = cmd->ucmd.ccmd.cc_alt;
1062 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
1063 if (!tail->ucmd.scmd.sc_next[i])
1064 tail->ucmd.scmd.sc_next[i] = newtail;
1070 tail = tail->c_next;
1073 /* if there's a continue block, link it to true block and find end */
1075 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
1076 tail->c_next = cmd->ucmd.ccmd.cc_alt;
1077 tail = tail->c_next;
1079 /* optimize "next" to point directly to continue block */
1080 if (tail->c_type == C_EXPR &&
1081 tail->ucmd.acmd.ac_expr &&
1082 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
1083 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
1086 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
1088 arg_free(tail->ucmd.acmd.ac_expr);
1089 tail->c_type = C_NEXT;
1090 tail->ucmd.ccmd.cc_alt = newtail;
1091 tail->ucmd.ccmd.cc_true = Nullcmd;
1093 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
1094 tail->ucmd.ccmd.cc_alt = newtail;
1096 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
1097 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
1098 if (!tail->ucmd.scmd.sc_next[i])
1099 tail->ucmd.scmd.sc_next[i] = newtail;
1104 tail = tail->c_next;
1106 for ( ; tail->c_next; tail = tail->c_next) ;
1109 /* Here's the real trick: link the end of the list back to the beginning,
1110 * inserting a "last" block to break out of the loop. This saves one or
1111 * two procedure calls every time through the loop, because of how cmd_exec
1112 * does tail recursion.
1115 tail->c_next = newtail;
1117 if (!cmd->ucmd.ccmd.cc_alt)
1118 cmd->ucmd.ccmd.cc_alt = tail; /* every loop has a continue now */
1121 (void)bcopy((char *)cmd, (char *)tail, sizeof(CMD));
1123 tail->c_type = C_EXPR;
1124 tail->c_flags ^= CF_INVERT; /* turn into "last unless" */
1125 tail->c_next = tail->ucmd.ccmd.cc_true; /* loop directly back to top */
1126 tail->ucmd.acmd.ac_expr = make_op(O_LAST,0,Nullarg,Nullarg,Nullarg);
1127 tail->ucmd.acmd.ac_stab = Nullstab;
1136 /* hoist "for $foo (@bar)" up into command block */
1138 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
1139 cmd->c_flags |= CFT_ARRAY; /* and set it to do the iteration */
1140 cmd->c_stab = eachstab;
1141 cmd->c_short = str_new(0); /* just to save a field in struct cmd */
1142 cmd->c_short->str_u.str_useful = -1;
1150 register CMD *tofree;
1151 register CMD *head = cmd;
1154 if (cmd->c_type != C_WHILE) { /* WHILE block is duplicated */
1156 Safefree(cmd->c_label);
1158 str_free(cmd->c_short);
1160 spat_free(cmd->c_spat);
1162 arg_free(cmd->c_expr);
1164 switch (cmd->c_type) {
1169 if (cmd->ucmd.ccmd.cc_true)
1170 cmd_free(cmd->ucmd.ccmd.cc_true);
1173 if (cmd->ucmd.acmd.ac_expr)
1174 arg_free(cmd->ucmd.acmd.ac_expr);
1179 if (tofree != head) /* to get Saber to shut up */
1181 if (cmd && cmd == head) /* reached end of while loop */
1192 for (i = 1; i <= arg->arg_len; i++) {
1193 switch (arg[i].arg_type & A_MASK) {
1197 if (arg->arg_type == O_AASSIGN &&
1198 arg[i].arg_ptr.arg_arg->arg_type == O_LARRAY) {
1200 stab_name(arg[i].arg_ptr.arg_arg[1].arg_ptr.arg_stab);
1202 if (strnEQ("_GEN_",name, 5)) /* array for foreach */
1203 hdelete(defstash,name,strlen(name));
1207 arg_free(arg[i].arg_ptr.arg_arg);
1210 cmd_free(arg[i].arg_ptr.arg_cmd);
1225 str_free(arg[i].arg_ptr.arg_str);
1228 spat_free(arg[i].arg_ptr.arg_spat);
1236 register SPAT *spat;
1241 if (spat->spat_runtime)
1242 arg_free(spat->spat_runtime);
1243 if (spat->spat_repl) {
1244 arg_free(spat->spat_repl);
1246 if (spat->spat_short) {
1247 str_free(spat->spat_short);
1249 if (spat->spat_regexp) {
1250 regfree(spat->spat_regexp);
1253 /* now unlink from spat list */
1255 for (entry = defstash->tbl_array['_']; entry; entry = entry->hent_next) {
1256 register HASH *stash;
1257 STAB *stab = (STAB*)entry->hent_val;
1261 stash = stab_hash(stab);
1262 if (!stash || stash->tbl_spatroot == Null(SPAT*))
1264 if (stash->tbl_spatroot == spat)
1265 stash->tbl_spatroot = spat->spat_next;
1267 for (sp = stash->tbl_spatroot;
1268 sp && sp->spat_next != spat;
1272 sp->spat_next = spat->spat_next;
1278 /* Recursively descend a command sequence and push the address of any string
1279 * that needs saving on recursion onto the tosave array.
1283 cmd_tosave(cmd,willsave)
1285 int willsave; /* willsave passes down the tree */
1287 register CMD *head = cmd;
1288 int shouldsave = FALSE; /* shouldsave passes up the tree */
1290 register CMD *lastcmd = Nullcmd;
1294 shouldsave |= spat_tosave(cmd->c_spat);
1296 shouldsave |= arg_tosave(cmd->c_expr,willsave);
1297 switch (cmd->c_type) {
1299 if (cmd->ucmd.ccmd.cc_true) {
1300 tmpsave = cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1302 /* Here we check to see if the temporary array generated for
1303 * a foreach needs to be localized because of recursion.
1305 if (tmpsave && (cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY) {
1307 lastcmd->c_type == C_EXPR &&
1308 lastcmd->ucmd.acmd.ac_expr) {
1309 ARG *arg = lastcmd->ucmd.acmd.ac_expr;
1311 if (arg->arg_type == O_ASSIGN &&
1312 arg[1].arg_type == A_LEXPR &&
1313 arg[1].arg_ptr.arg_arg->arg_type == O_LARRAY &&
1316 arg[1].arg_ptr.arg_arg[1].arg_ptr.arg_stab),
1317 5)) { /* array generated for foreach */
1318 (void)localize(arg[1].arg_ptr.arg_arg);
1322 /* in any event, save the iterator */
1324 (void)apush(tosave,cmd->c_short);
1326 shouldsave |= tmpsave;
1332 if (cmd->ucmd.ccmd.cc_true)
1333 shouldsave |= cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1336 if (cmd->ucmd.acmd.ac_expr)
1337 shouldsave |= arg_tosave(cmd->ucmd.acmd.ac_expr,willsave);
1342 if (cmd && cmd == head) /* reached end of while loop */
1349 arg_tosave(arg,willsave)
1354 int shouldsave = FALSE;
1356 for (i = arg->arg_len; i >= 1; i--) {
1357 switch (arg[i].arg_type & A_MASK) {
1362 shouldsave |= arg_tosave(arg[i].arg_ptr.arg_arg,shouldsave);
1365 shouldsave |= cmd_tosave(arg[i].arg_ptr.arg_cmd,shouldsave);
1378 shouldsave |= spat_tosave(arg[i].arg_ptr.arg_spat);
1382 switch (arg->arg_type) {
1392 (void)apush(tosave,arg->arg_ptr.arg_str);
1398 register SPAT *spat;
1400 int shouldsave = FALSE;
1402 if (spat->spat_runtime)
1403 shouldsave |= arg_tosave(spat->spat_runtime,FALSE);
1404 if (spat->spat_repl) {
1405 shouldsave |= arg_tosave(spat->spat_repl,FALSE);