1 /* $Header: cons.c,v 3.0.1.4 90/02/28 16:44:00 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.4 90/02/28 16:44:00 lwall
10 * patch9: subs which return by both mechanisms can clobber local return data
11 * patch9: changed internal SUB label to _SUB_
12 * patch9: line numbers were bogus during certain portions of foreach evaluation
14 * Revision 3.0.1.3 89/12/21 19:20:25 lwall
15 * patch7: made nested or recursive foreach work right
17 * Revision 3.0.1.2 89/11/17 15:08:53 lwall
18 * patch5: nested foreach on same array didn't work
20 * Revision 3.0.1.1 89/10/26 23:09:01 lwall
21 * patch1: numeric switch optimization was broken
22 * patch1: unless was broken when run under the debugger
24 * Revision 3.0 89/10/18 15:10:23 lwall
33 extern char *tokename[];
36 static int cmd_tosave();
37 static int arg_tosave();
38 static int spat_tosave();
40 static bool saw_return;
48 STAB *stab = stabent(name,TRUE);
53 line_t oldline = line;
57 warn("Subroutine %s redefined",name);
60 cmd_free(stab_sub(stab)->cmd);
61 afree(stab_sub(stab)->tosave);
62 Safefree(stab_sub(stab));
64 sub->filename = filename;
66 tosave = anew(Nullstab);
67 tosave->ary_fill = 0; /* make 1 based */
68 (void)cmd_tosave(cmd,FALSE); /* this builds the tosave array */
71 struct compcmd mycompblock;
73 mycompblock.comp_true = cmd;
74 mycompblock.comp_alt = Nullcmd;
75 cmd = add_label(savestr("_SUB_"),make_ccmd(C_BLOCK,Nullarg,mycompblock));
78 cmd->c_next->c_flags |= CF_TERM;
80 cmd->c_flags |= CF_TERM;
85 STR *str = str_nmake((double)subline);
88 sprintf(buf,"%ld",(long)line);
90 name = str_get(subname);
91 hstore(stab_xhash(DBsub),name,strlen(name),str,0);
92 str_set(subname,"main");
104 register int last_opt = 0;
105 register STAB *last_stab = Nullstab;
106 register int count = 0;
107 register CMD *switchbeg = Nullcmd;
109 if (tail == Nullcmd) {
114 for (tail = head; tail; tail = tail->c_next) {
116 /* save one measly dereference at runtime */
117 if (tail->c_type == C_IF) {
118 if (!(tail->ucmd.ccmd.cc_alt = tail->ucmd.ccmd.cc_alt->c_next))
119 tail->c_flags |= CF_TERM;
121 else if (tail->c_type == C_EXPR) {
124 if (tail->ucmd.acmd.ac_expr)
125 arg = tail->ucmd.acmd.ac_expr;
129 if (arg->arg_type == O_RETURN)
130 tail->c_flags |= CF_TERM;
131 else if (arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
132 tail->c_flags |= CF_TERM;
136 tail->c_flags |= CF_TERM;
138 if (tail->c_expr && (tail->c_flags & CF_OPTIMIZE) == CFT_FALSE)
139 opt_arg(tail,1, tail->c_type == C_EXPR);
141 /* now do a little optimization on case-ish structures */
142 switch(tail->c_flags & (CF_OPTIMIZE|CF_FIRSTNEG|CF_INVERT)) {
144 if (stabent("*",FALSE)) { /* bad assumption here!!! */
150 opt = (tail->c_flags & CF_NESURE) ? CFT_STROP : 0;
156 opt = (tail->c_slen == O_NE ? 0 : CFT_NUMOP);
157 if ((tail->c_flags&(CF_NESURE|CF_EQSURE)) != (CF_NESURE|CF_EQSURE))
163 if (opt && opt == last_opt && tail->c_stab == last_stab)
166 if (count >= 3) { /* is this the breakeven point? */
167 if (last_opt == CFT_NUMOP)
168 make_nswitch(switchbeg,count);
170 make_cswitch(switchbeg,count);
180 last_stab = tail->c_stab;
182 if (count >= 3) { /* is this the breakeven point? */
183 if (last_opt == CFT_NUMOP)
184 make_nswitch(switchbeg,count);
186 make_cswitch(switchbeg,count);
191 /* We've spotted a sequence of CMDs that all test the value of the same
192 * spat. Thus we can insert a SWITCH in front and jump directly
193 * to the correct one.
195 make_cswitch(head,count)
202 register int min = 255;
203 register int max = 0;
205 /* make a new head in the exact same spot */
206 New(102,cur, 1, CMD);
210 Copy(head,cur,1,CMD);
213 head->c_type = C_CSWITCH;
214 head->c_next = cur; /* insert new cmd at front of list */
215 head->c_stab = cur->c_stab;
217 Newz(103,loc,258,CMD*);
218 loc++; /* lie a little */
220 if ((cur->c_flags & CF_OPTIMIZE) == CFT_CCLASS) {
221 for (i = 0; i <= 255; i++) {
222 if (!loc[i] && cur->c_short->str_ptr[i>>3] & (1 << (i&7))) {
232 i = *cur->c_short->str_ptr & 255;
245 Copy(&loc[min],&loc[0], max - min, CMD*);
249 for (i = 0; i <= max; i++)
252 Renew(loc,max+1,CMD*); /* chop it down to size */
253 head->ucmd.scmd.sc_offset = min;
254 head->ucmd.scmd.sc_max = max;
255 head->ucmd.scmd.sc_next = loc;
258 make_nswitch(head,count)
262 register CMD *cur = head;
265 register int min = 32767;
266 register int max = -32768;
267 int origcount = count;
268 double value; /* or your money back! */
269 short changed; /* so triple your money back! */
272 i = (int)str_gnum(cur->c_short);
274 if (value != cur->c_short->str_u.str_nval)
275 return; /* fractional values--just forget it */
278 return; /* too big for a short */
279 if (cur->c_slen == O_LE)
281 else if (cur->c_slen == O_GE) /* we only do < or > here */
290 if (max - min > count * 2 + 10) /* too sparse? */
293 /* now make a new head in the exact same spot */
294 New(104,cur, 1, CMD);
298 Copy(head,cur,1,CMD);
301 head->c_type = C_NSWITCH;
302 head->c_next = cur; /* insert new cmd at front of list */
303 head->c_stab = cur->c_stab;
305 Newz(105,loc, max - min + 3, CMD*);
310 i = (int)str_gnum(cur->c_short);
312 switch(cur->c_slen) {
316 for (i--; i >= -1; i--)
323 for (i++; i <= max; i++)
337 for (i = 0; i <= max; i++)
340 head->ucmd.scmd.sc_offset = min;
341 head->ucmd.scmd.sc_max = max;
342 head->ucmd.scmd.sc_next = loc;
346 append_line(head,tail)
352 if (!tail->c_head) /* make sure tail is well formed */
354 if (head != Nullcmd) {
355 tail = tail->c_head; /* get to start of tail list */
357 head->c_head = head; /* start a new head list */
358 while (head->c_next) {
359 head->c_next->c_head = head->c_head;
360 head = head->c_next; /* get to end of head list */
362 head->c_next = tail; /* link to end of old list */
363 tail->c_head = head->c_head; /* propagate head pointer */
365 while (tail->c_next) {
366 tail->c_next->c_head = tail->c_head;
377 register CMD *head = cur->c_head;
385 str = afetch(lineary,(int)head->c_line,FALSE);
386 if (!str || str->str_nok)
388 str->str_u.str_nval = (double)head->c_line;
391 cmd->c_type = C_EXPR;
392 cmd->ucmd.acmd.ac_stab = Nullstab;
393 cmd->ucmd.acmd.ac_expr = Nullarg;
394 arg = make_op(O_ITEM,1,Nullarg,Nullarg,Nullarg);
395 arg[1].arg_type = A_SINGLE;
396 arg[1].arg_ptr.arg_str = str_nmake((double)head->c_line);
397 cmd->c_expr = make_op(O_SUBR, 2,
398 stab2arg(A_WORD,DBstab),
401 cmd->c_flags |= CF_COND|CF_DBSUB;
402 cmd->c_line = head->c_line;
403 cmd->c_label = head->c_label;
404 cmd->c_file = filename;
405 return append_line(cmd, cur);
409 make_acmd(type,stab,cond,arg)
419 cmd->ucmd.acmd.ac_stab = stab;
420 cmd->ucmd.acmd.ac_expr = arg;
423 cmd->c_flags |= CF_COND;
424 if (cmdline == NOLINE)
427 cmd->c_line = cmdline;
430 cmd->c_file = filename;
437 make_ccmd(type,arg,cblock)
440 struct compcmd cblock;
444 Newz(108,cmd, 1, CMD);
447 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
448 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
450 cmd->c_flags |= CF_COND;
451 if (cmdline == NOLINE)
454 cmd->c_line = cmdline;
463 make_icmd(type,arg,cblock)
466 struct compcmd cblock;
472 struct compcmd ncblock;
474 Newz(109,cmd, 1, CMD);
478 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
479 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
481 cmd->c_flags |= CF_COND;
482 if (cmdline == NOLINE)
485 cmd->c_line = cmdline;
489 alt = cblock.comp_alt;
490 while (alt && alt->c_type == C_ELSIF) {
492 alt = alt->ucmd.ccmd.cc_alt;
494 if (alt) { /* a real life ELSE at the end? */
495 ncblock.comp_true = alt;
496 ncblock.comp_alt = Nullcmd;
497 alt = append_line(cur,make_ccmd(C_ELSE,Nullarg,ncblock));
498 cur->ucmd.ccmd.cc_alt = alt;
501 alt = cur; /* no ELSE, so cur is proxy ELSE */
504 while (cmd) { /* now point everyone at the ELSE */
506 cmd = cur->ucmd.ccmd.cc_alt;
508 if (cur->c_type == C_ELSIF)
510 if (cur->c_type == C_IF)
511 cur->ucmd.ccmd.cc_alt = alt;
522 opt_arg(cmd,fliporflop,acmd)
531 int context = 0; /* 0 = normal, 1 = before &&, 2 = before || */
532 int flp = fliporflop;
536 if (!(arg = cmd->c_expr)) {
537 cmd->c_flags &= ~CF_COND;
541 /* Can we turn && and || into if and unless? */
543 if (acmd && !cmd->ucmd.acmd.ac_expr && !(cmd->c_flags & CF_TERM) &&
544 (arg->arg_type == O_AND || arg->arg_type == O_OR) ) {
546 arg[2].arg_type &= A_MASK; /* don't suppress eval */
548 cmd->ucmd.acmd.ac_expr = arg[2].arg_ptr.arg_arg;
549 cmd->c_expr = arg[1].arg_ptr.arg_arg;
550 if (arg->arg_type == O_OR)
551 cmd->c_flags ^= CF_INVERT; /* || is like unless */
557 /* Turn "if (!expr)" into "unless (expr)" */
559 if (!(cmd->c_flags & CF_TERM)) { /* unless return value wanted */
560 while (arg->arg_type == O_NOT) {
562 cmd->c_flags ^= CF_INVERT; /* flip sense of cmd */
563 cmd->c_expr = arg[1].arg_ptr.arg_arg; /* hoist the rest of expr */
565 arg = cmd->c_expr; /* here we go again */
569 if (!arg->arg_len) { /* sanity check */
574 /* for "cond .. cond" we set up for the initial check */
576 if (arg->arg_type == O_FLIP)
579 /* for "cond && expr" and "cond || expr" we can ignore expr, sort of */
582 if (arg->arg_type == O_AND)
584 else if (arg->arg_type == O_OR)
586 if (context && (arg[flp].arg_type & A_MASK) == A_EXPR) {
587 arg = arg[flp].arg_ptr.arg_arg;
589 if (arg->arg_type == O_AND || arg->arg_type == O_OR)
592 if ((context & 3) == 3)
595 if (arg[flp].arg_flags & (AF_PRE|AF_POST)) {
597 return; /* side effect, can't optimize */
600 if (arg->arg_type == O_ITEM || arg->arg_type == O_FLIP ||
601 arg->arg_type == O_AND || arg->arg_type == O_OR) {
602 if ((arg[flp].arg_type & A_MASK) == A_SINGLE) {
603 opt = (str_true(arg[flp].arg_ptr.arg_str) ? CFT_TRUE : CFT_FALSE);
604 cmd->c_short = str_smake(arg[flp].arg_ptr.arg_str);
607 else if ((arg[flp].arg_type & A_MASK) == A_STAB ||
608 (arg[flp].arg_type & A_MASK) == A_LVAL) {
609 cmd->c_stab = arg[flp].arg_ptr.arg_stab;
612 if (!context) { /* no && or ||? */
614 cmd->c_expr = Nullarg;
617 cmd->c_flags |= CF_EQSURE;
619 cmd->c_flags |= CF_NESURE;
622 else if (arg->arg_type == O_MATCH || arg->arg_type == O_SUBST ||
623 arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST) {
624 if ((arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
625 (arg[2].arg_type & A_MASK) == A_SPAT &&
626 arg[2].arg_ptr.arg_spat->spat_short ) {
627 cmd->c_stab = arg[1].arg_ptr.arg_stab;
628 cmd->c_short = str_smake(arg[2].arg_ptr.arg_spat->spat_short);
629 cmd->c_slen = arg[2].arg_ptr.arg_spat->spat_slen;
630 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ALL &&
631 !(arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ONCE) &&
632 (arg->arg_type == O_MATCH || arg->arg_type == O_NMATCH) )
633 sure |= CF_EQSURE; /* (SUBST must be forced even */
634 /* if we know it will work.) */
635 if (arg->arg_type != O_SUBST) {
636 arg[2].arg_ptr.arg_spat->spat_short = Nullstr;
637 arg[2].arg_ptr.arg_spat->spat_slen = 0; /* only one chk */
639 sure |= CF_NESURE; /* normally only sure if it fails */
640 if (arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST)
641 cmd->c_flags |= CF_FIRSTNEG;
642 if (context & 1) { /* only sure if thing is false */
643 if (cmd->c_flags & CF_FIRSTNEG)
648 else if (context & 2) { /* only sure if thing is true */
649 if (cmd->c_flags & CF_FIRSTNEG)
654 if (sure & (CF_EQSURE|CF_NESURE)) { /* if we know anything*/
655 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_SCANFIRST)
659 if (sure == (CF_EQSURE|CF_NESURE) /* really sure? */
660 && arg->arg_type == O_MATCH
662 && fliporflop == 1) {
663 spat_free(arg[2].arg_ptr.arg_spat);
664 arg[2].arg_ptr.arg_spat = Nullspat; /* don't do twice */
666 cmd->c_flags |= sure;
670 else if (arg->arg_type == O_SEQ || arg->arg_type == O_SNE ||
671 arg->arg_type == O_SLT || arg->arg_type == O_SGT) {
672 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
673 if (arg[2].arg_type == A_SINGLE) {
674 cmd->c_stab = arg[1].arg_ptr.arg_stab;
675 cmd->c_short = str_smake(arg[2].arg_ptr.arg_str);
676 cmd->c_slen = cmd->c_short->str_cur+1;
677 switch (arg->arg_type) {
678 case O_SLT: case O_SGT:
680 cmd->c_flags |= CF_FIRSTNEG;
683 cmd->c_flags |= CF_FIRSTNEG;
686 sure |= CF_NESURE|CF_EQSURE;
689 if (context & 1) { /* only sure if thing is false */
690 if (cmd->c_flags & CF_FIRSTNEG)
695 else if (context & 2) { /* only sure if thing is true */
696 if (cmd->c_flags & CF_FIRSTNEG)
701 if (sure & (CF_EQSURE|CF_NESURE)) {
703 cmd->c_flags |= sure;
708 else if (arg->arg_type == O_EQ || arg->arg_type == O_NE ||
709 arg->arg_type == O_LE || arg->arg_type == O_GE ||
710 arg->arg_type == O_LT || arg->arg_type == O_GT) {
711 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
712 if (arg[2].arg_type == A_SINGLE) {
713 cmd->c_stab = arg[1].arg_ptr.arg_stab;
715 STR *str = arg[2].arg_ptr.arg_str;
717 if ((!str->str_nok && !looks_like_number(str)))
718 warn("Possible use of == on string value");
720 cmd->c_short = str_nmake(str_gnum(arg[2].arg_ptr.arg_str));
721 cmd->c_slen = arg->arg_type;
722 sure |= CF_NESURE|CF_EQSURE;
723 if (context & 1) { /* only sure if thing is false */
726 else if (context & 2) { /* only sure if thing is true */
729 if (sure & (CF_EQSURE|CF_NESURE)) {
731 cmd->c_flags |= sure;
736 else if (arg->arg_type == O_ASSIGN &&
737 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
738 arg[1].arg_ptr.arg_stab == defstab &&
739 arg[2].arg_type == A_EXPR ) {
740 arg2 = arg[2].arg_ptr.arg_arg;
741 if (arg2->arg_type == O_ITEM && arg2[1].arg_type == A_READ) {
743 cmd->c_stab = arg2[1].arg_ptr.arg_stab;
744 if (!(stab_io(arg2[1].arg_ptr.arg_stab)->flags & IOF_ARGV)) {
747 cmd->c_expr = Nullarg;
751 else if (arg->arg_type == O_CHOP &&
752 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) ) {
754 cmd->c_stab = arg[1].arg_ptr.arg_stab;
756 cmd->c_expr = Nullarg;
762 if (cmd->c_flags & CF_FLIP) {
763 if (fliporflop == 1) {
764 arg = cmd->c_expr; /* get back to O_FLIP arg */
765 New(110,arg[3].arg_ptr.arg_cmd, 1, CMD);
766 Copy(cmd, arg[3].arg_ptr.arg_cmd, 1, CMD);
767 New(111,arg[4].arg_ptr.arg_cmd,1,CMD);
768 Copy(cmd, arg[4].arg_ptr.arg_cmd, 1, CMD);
769 opt_arg(arg[4].arg_ptr.arg_cmd,2,acmd);
770 arg->arg_len = 2; /* this is a lie */
773 if ((opt & CF_OPTIMIZE) == CFT_EVAL)
774 cmd->c_flags = (cmd->c_flags & ~CF_OPTIMIZE) | CFT_UNFLIP;
795 cmd->c_flags |= CF_COND;
807 cmd->c_flags |= CF_COND|CF_LOOP;
809 if (!(cmd->c_flags & CF_INVERT))
810 while_io(cmd); /* add $_ =, if necessary */
812 if (cmd->c_type == C_BLOCK)
813 cmd->c_flags &= ~CF_COND;
815 arg = cmd->ucmd.acmd.ac_expr;
816 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
817 cmd->c_flags &= ~CF_COND; /* "do {} while" happens at least once */
818 if (arg && arg->arg_type == O_SUBR)
819 cmd->c_flags &= ~CF_COND; /* likewise for "do subr() while" */
828 register CMD *targ = cmd;
831 if (targ->c_flags & CF_DBSUB)
833 targ->c_flags ^= CF_INVERT;
842 char *tname = tmpbuf;
844 if (bufptr > oldoldbufptr && bufptr - oldoldbufptr < 200 &&
845 oldoldbufptr != oldbufptr && oldbufptr != bufptr) {
846 while (isspace(*oldoldbufptr))
848 strncpy(tmp2buf, oldoldbufptr, bufptr - oldoldbufptr);
849 tmp2buf[bufptr - oldoldbufptr] = '\0';
850 sprintf(tname,"next 2 tokens \"%s\"",tmp2buf);
852 else if (bufptr > oldbufptr && bufptr - oldbufptr < 200 &&
853 oldbufptr != bufptr) {
854 while (isspace(*oldbufptr))
856 strncpy(tmp2buf, oldbufptr, bufptr - oldbufptr);
857 tmp2buf[bufptr - oldbufptr] = '\0';
858 sprintf(tname,"next token \"%s\"",tmp2buf);
860 else if (yychar > 256)
861 tname = "next token ???";
863 (void)strcpy(tname,"at EOF");
864 else if (yychar < 32)
865 (void)sprintf(tname,"next char ^%c",yychar+64);
866 else if (yychar == 127)
867 (void)strcpy(tname,"at EOF");
869 (void)sprintf(tname,"next char %c",yychar);
870 (void)sprintf(buf, "%s in file %s at line %d, %s\n",
871 s,filename,line,tname);
872 if (line == multi_end && multi_start < multi_end)
873 sprintf(buf+strlen(buf),
874 " (Might be a runaway multi-line %c%c string starting on line %d)\n",
875 multi_open,multi_close,multi_start);
877 str_cat(stab_val(stabent("@",TRUE)),buf);
880 if (++error_count >= 10)
881 fatal("Too many errors\n");
888 register ARG *arg = cmd->c_expr;
891 /* hoist "while (<channel>)" up into command block */
893 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_READ) {
894 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
895 cmd->c_flags |= CFT_GETS; /* and set it to do the input */
896 cmd->c_stab = arg[1].arg_ptr.arg_stab;
897 if (stab_io(arg[1].arg_ptr.arg_stab)->flags & IOF_ARGV) {
898 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$_ =" */
899 stab2arg(A_LVAL,defstab), arg, Nullarg));
903 cmd->c_expr = Nullarg;
906 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_INDREAD) {
907 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
908 cmd->c_flags |= CFT_INDGETS; /* and set it to do the input */
909 cmd->c_stab = arg[1].arg_ptr.arg_stab;
911 cmd->c_expr = Nullarg;
913 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_GLOB) {
914 if ((cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY)
915 asgnstab = cmd->c_stab;
918 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$foo =" */
919 stab2arg(A_LVAL,asgnstab), arg, Nullarg));
920 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
932 if (cmd->c_expr && (cmd->c_flags & CF_OPTIMIZE) == CFT_FALSE)
933 opt_arg(cmd,1, cmd->c_type == C_EXPR);
935 while_io(cmd); /* add $_ =, if necessary */
937 /* First find the end of the true list */
939 tail = cmd->ucmd.ccmd.cc_true;
942 New(112,newtail, 1, CMD); /* guaranteed continue */
944 /* optimize "next" to point directly to continue block */
945 if (tail->c_type == C_EXPR &&
946 tail->ucmd.acmd.ac_expr &&
947 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
948 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
951 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
953 arg_free(tail->ucmd.acmd.ac_expr);
954 tail->c_type = C_NEXT;
955 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
956 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
958 tail->ucmd.ccmd.cc_alt = newtail;
959 tail->ucmd.ccmd.cc_true = Nullcmd;
961 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
962 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
963 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
965 tail->ucmd.ccmd.cc_alt = newtail;
967 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
968 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
969 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
970 if (!tail->ucmd.scmd.sc_next[i])
971 tail->ucmd.scmd.sc_next[i] = cmd->ucmd.ccmd.cc_alt;
974 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
975 if (!tail->ucmd.scmd.sc_next[i])
976 tail->ucmd.scmd.sc_next[i] = newtail;
985 /* if there's a continue block, link it to true block and find end */
987 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
988 tail->c_next = cmd->ucmd.ccmd.cc_alt;
991 /* optimize "next" to point directly to continue block */
992 if (tail->c_type == C_EXPR &&
993 tail->ucmd.acmd.ac_expr &&
994 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
995 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
998 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
1000 arg_free(tail->ucmd.acmd.ac_expr);
1001 tail->c_type = C_NEXT;
1002 tail->ucmd.ccmd.cc_alt = newtail;
1003 tail->ucmd.ccmd.cc_true = Nullcmd;
1005 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
1006 tail->ucmd.ccmd.cc_alt = newtail;
1008 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
1009 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
1010 if (!tail->ucmd.scmd.sc_next[i])
1011 tail->ucmd.scmd.sc_next[i] = newtail;
1016 tail = tail->c_next;
1018 for ( ; tail->c_next; tail = tail->c_next) ;
1021 /* Here's the real trick: link the end of the list back to the beginning,
1022 * inserting a "last" block to break out of the loop. This saves one or
1023 * two procedure calls every time through the loop, because of how cmd_exec
1024 * does tail recursion.
1027 tail->c_next = newtail;
1029 if (!cmd->ucmd.ccmd.cc_alt)
1030 cmd->ucmd.ccmd.cc_alt = tail; /* every loop has a continue now */
1033 (void)bcopy((char *)cmd, (char *)tail, sizeof(CMD));
1035 tail->c_type = C_EXPR;
1036 tail->c_flags ^= CF_INVERT; /* turn into "last unless" */
1037 tail->c_next = tail->ucmd.ccmd.cc_true; /* loop directly back to top */
1038 tail->ucmd.acmd.ac_expr = make_op(O_LAST,0,Nullarg,Nullarg,Nullarg);
1039 tail->ucmd.acmd.ac_stab = Nullstab;
1048 /* hoist "for $foo (@bar)" up into command block */
1050 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
1051 cmd->c_flags |= CFT_ARRAY; /* and set it to do the iteration */
1052 cmd->c_stab = eachstab;
1053 cmd->c_short = str_new(0); /* just to save a field in struct cmd */
1054 cmd->c_short->str_u.str_useful = -1;
1062 register CMD *tofree;
1063 register CMD *head = cmd;
1066 if (cmd->c_type != C_WHILE) { /* WHILE block is duplicated */
1068 Safefree(cmd->c_label);
1070 str_free(cmd->c_short);
1072 spat_free(cmd->c_spat);
1074 arg_free(cmd->c_expr);
1076 switch (cmd->c_type) {
1081 if (cmd->ucmd.ccmd.cc_true)
1082 cmd_free(cmd->ucmd.ccmd.cc_true);
1085 if (cmd->ucmd.acmd.ac_expr)
1086 arg_free(cmd->ucmd.acmd.ac_expr);
1092 if (cmd && cmd == head) /* reached end of while loop */
1102 for (i = 1; i <= arg->arg_len; i++) {
1103 switch (arg[i].arg_type & A_MASK) {
1107 if (arg->arg_type == O_AASSIGN &&
1108 arg[i].arg_ptr.arg_arg->arg_type == O_LARRAY) {
1110 stab_name(arg[i].arg_ptr.arg_arg[1].arg_ptr.arg_stab);
1112 if (strnEQ("_GEN_",name, 5)) /* array for foreach */
1113 hdelete(defstash,name,strlen(name));
1117 arg_free(arg[i].arg_ptr.arg_arg);
1120 cmd_free(arg[i].arg_ptr.arg_cmd);
1135 str_free(arg[i].arg_ptr.arg_str);
1138 spat_free(arg[i].arg_ptr.arg_spat);
1146 register SPAT *spat;
1151 if (spat->spat_runtime)
1152 arg_free(spat->spat_runtime);
1153 if (spat->spat_repl) {
1154 arg_free(spat->spat_repl);
1156 if (spat->spat_short) {
1157 str_free(spat->spat_short);
1159 if (spat->spat_regexp) {
1160 regfree(spat->spat_regexp);
1163 /* now unlink from spat list */
1165 for (entry = defstash->tbl_array['_']; entry; entry = entry->hent_next) {
1166 register HASH *stash;
1167 STAB *stab = (STAB*)entry->hent_val;
1171 stash = stab_hash(stab);
1172 if (!stash || stash->tbl_spatroot == Null(SPAT*))
1174 if (stash->tbl_spatroot == spat)
1175 stash->tbl_spatroot = spat->spat_next;
1177 for (sp = stash->tbl_spatroot;
1178 sp && sp->spat_next != spat;
1182 sp->spat_next = spat->spat_next;
1188 /* Recursively descend a command sequence and push the address of any string
1189 * that needs saving on recursion onto the tosave array.
1193 cmd_tosave(cmd,willsave)
1195 int willsave; /* willsave passes down the tree */
1197 register CMD *head = cmd;
1198 int shouldsave = FALSE; /* shouldsave passes up the tree */
1200 register CMD *lastcmd = Nullcmd;
1204 shouldsave |= spat_tosave(cmd->c_spat);
1206 shouldsave |= arg_tosave(cmd->c_expr,willsave);
1207 switch (cmd->c_type) {
1209 if (cmd->ucmd.ccmd.cc_true) {
1210 tmpsave = cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1212 /* Here we check to see if the temporary array generated for
1213 * a foreach needs to be localized because of recursion.
1215 if (tmpsave && (cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY) {
1217 lastcmd->c_type == C_EXPR &&
1218 lastcmd->ucmd.acmd.ac_expr) {
1219 ARG *arg = lastcmd->ucmd.acmd.ac_expr;
1221 if (arg->arg_type == O_ASSIGN &&
1222 arg[1].arg_type == A_LEXPR &&
1223 arg[1].arg_ptr.arg_arg->arg_type == O_LARRAY &&
1226 arg[1].arg_ptr.arg_arg[1].arg_ptr.arg_stab),
1227 5)) { /* array generated for foreach */
1228 (void)localize(arg[1].arg_ptr.arg_arg);
1232 /* in any event, save the iterator */
1234 (void)apush(tosave,cmd->c_short);
1236 shouldsave |= tmpsave;
1242 if (cmd->ucmd.ccmd.cc_true)
1243 shouldsave |= cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1246 if (cmd->ucmd.acmd.ac_expr)
1247 shouldsave |= arg_tosave(cmd->ucmd.acmd.ac_expr,willsave);
1252 if (cmd && cmd == head) /* reached end of while loop */
1259 arg_tosave(arg,willsave)
1264 int shouldsave = FALSE;
1266 for (i = arg->arg_len; i >= 1; i--) {
1267 switch (arg[i].arg_type & A_MASK) {
1272 shouldsave |= arg_tosave(arg[i].arg_ptr.arg_arg,shouldsave);
1275 shouldsave |= cmd_tosave(arg[i].arg_ptr.arg_cmd,shouldsave);
1288 shouldsave |= spat_tosave(arg[i].arg_ptr.arg_spat);
1292 switch (arg->arg_type) {
1302 (void)apush(tosave,arg->arg_ptr.arg_str);
1308 register SPAT *spat;
1310 int shouldsave = FALSE;
1312 if (spat->spat_runtime)
1313 shouldsave |= arg_tosave(spat->spat_runtime,FALSE);
1314 if (spat->spat_repl) {
1315 shouldsave |= arg_tosave(spat->spat_repl,FALSE);