1 /* $Header: cons.c,v 3.0.1.2 89/11/17 15:08:53 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.2 89/11/17 15:08:53 lwall
10 * patch5: nested foreach on same array didn't work
12 * Revision 3.0.1.1 89/10/26 23:09:01 lwall
13 * patch1: numeric switch optimization was broken
14 * patch1: unless was broken when run under the debugger
16 * Revision 3.0 89/10/18 15:10:23 lwall
25 extern char *tokename[];
28 static int cmd_tosave();
29 static int arg_tosave();
30 static int spat_tosave();
32 static bool saw_return;
40 STAB *stab = stabent(name,TRUE);
45 line_t oldline = line;
49 warn("Subroutine %s redefined",name);
52 cmd_free(stab_sub(stab)->cmd);
53 afree(stab_sub(stab)->tosave);
54 Safefree(stab_sub(stab));
56 sub->filename = filename;
58 tosave = anew(Nullstab);
59 tosave->ary_fill = 0; /* make 1 based */
60 (void)cmd_tosave(cmd,FALSE); /* this builds the tosave array */
63 struct compcmd mycompblock;
65 mycompblock.comp_true = cmd;
66 mycompblock.comp_alt = Nullcmd;
67 cmd = add_label(savestr("SUB"),make_ccmd(C_BLOCK,Nullarg,mycompblock));
73 STR *str = str_nmake((double)subline);
76 sprintf(buf,"%ld",(long)line);
78 name = str_get(subname);
79 hstore(stab_xhash(DBsub),name,strlen(name),str,0);
80 str_set(subname,"main");
92 register int last_opt = 0;
93 register STAB *last_stab = Nullstab;
94 register int count = 0;
95 register CMD *switchbeg = Nullcmd;
97 if (tail == Nullcmd) {
102 for (tail = head; tail; tail = tail->c_next) {
104 /* save one measly dereference at runtime */
105 if (tail->c_type == C_IF) {
106 if (!(tail->ucmd.ccmd.cc_alt = tail->ucmd.ccmd.cc_alt->c_next))
107 tail->c_flags |= CF_TERM;
109 else if (tail->c_type == C_EXPR) {
112 if (tail->ucmd.acmd.ac_expr)
113 arg = tail->ucmd.acmd.ac_expr;
117 if (arg->arg_type == O_RETURN)
118 tail->c_flags |= CF_TERM;
119 else if (arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
120 tail->c_flags |= CF_TERM;
124 tail->c_flags |= CF_TERM;
126 if (tail->c_expr && (tail->c_flags & CF_OPTIMIZE) == CFT_FALSE)
127 opt_arg(tail,1, tail->c_type == C_EXPR);
129 /* now do a little optimization on case-ish structures */
130 switch(tail->c_flags & (CF_OPTIMIZE|CF_FIRSTNEG|CF_INVERT)) {
132 if (stabent("*",FALSE)) { /* bad assumption here!!! */
138 opt = (tail->c_flags & CF_NESURE) ? CFT_STROP : 0;
144 opt = (tail->c_slen == O_NE ? 0 : CFT_NUMOP);
145 if ((tail->c_flags&(CF_NESURE|CF_EQSURE)) != (CF_NESURE|CF_EQSURE))
151 if (opt && opt == last_opt && tail->c_stab == last_stab)
154 if (count >= 3) { /* is this the breakeven point? */
155 if (last_opt == CFT_NUMOP)
156 make_nswitch(switchbeg,count);
158 make_cswitch(switchbeg,count);
168 last_stab = tail->c_stab;
170 if (count >= 3) { /* is this the breakeven point? */
171 if (last_opt == CFT_NUMOP)
172 make_nswitch(switchbeg,count);
174 make_cswitch(switchbeg,count);
179 /* We've spotted a sequence of CMDs that all test the value of the same
180 * spat. Thus we can insert a SWITCH in front and jump directly
181 * to the correct one.
183 make_cswitch(head,count)
190 register int min = 255;
191 register int max = 0;
193 /* make a new head in the exact same spot */
194 New(102,cur, 1, CMD);
198 Copy(head,cur,1,CMD);
201 head->c_type = C_CSWITCH;
202 head->c_next = cur; /* insert new cmd at front of list */
203 head->c_stab = cur->c_stab;
205 Newz(103,loc,258,CMD*);
206 loc++; /* lie a little */
208 if ((cur->c_flags & CF_OPTIMIZE) == CFT_CCLASS) {
209 for (i = 0; i <= 255; i++) {
210 if (!loc[i] && cur->c_short->str_ptr[i>>3] & (1 << (i&7))) {
220 i = *cur->c_short->str_ptr & 255;
233 Copy(&loc[min],&loc[0], max - min, CMD*);
237 for (i = 0; i <= max; i++)
240 Renew(loc,max+1,CMD*); /* chop it down to size */
241 head->ucmd.scmd.sc_offset = min;
242 head->ucmd.scmd.sc_max = max;
243 head->ucmd.scmd.sc_next = loc;
246 make_nswitch(head,count)
250 register CMD *cur = head;
253 register int min = 32767;
254 register int max = -32768;
255 int origcount = count;
256 double value; /* or your money back! */
257 short changed; /* so triple your money back! */
260 i = (int)str_gnum(cur->c_short);
262 if (value != cur->c_short->str_u.str_nval)
263 return; /* fractional values--just forget it */
266 return; /* too big for a short */
267 if (cur->c_slen == O_LE)
269 else if (cur->c_slen == O_GE) /* we only do < or > here */
278 if (max - min > count * 2 + 10) /* too sparse? */
281 /* now make a new head in the exact same spot */
282 New(104,cur, 1, CMD);
286 Copy(head,cur,1,CMD);
289 head->c_type = C_NSWITCH;
290 head->c_next = cur; /* insert new cmd at front of list */
291 head->c_stab = cur->c_stab;
293 Newz(105,loc, max - min + 3, CMD*);
298 i = (int)str_gnum(cur->c_short);
300 switch(cur->c_slen) {
304 for (i--; i >= -1; i--)
311 for (i++; i <= max; i++)
325 for (i = 0; i <= max; i++)
328 head->ucmd.scmd.sc_offset = min;
329 head->ucmd.scmd.sc_max = max;
330 head->ucmd.scmd.sc_next = loc;
334 append_line(head,tail)
340 if (!tail->c_head) /* make sure tail is well formed */
342 if (head != Nullcmd) {
343 tail = tail->c_head; /* get to start of tail list */
345 head->c_head = head; /* start a new head list */
346 while (head->c_next) {
347 head->c_next->c_head = head->c_head;
348 head = head->c_next; /* get to end of head list */
350 head->c_next = tail; /* link to end of old list */
351 tail->c_head = head->c_head; /* propagate head pointer */
353 while (tail->c_next) {
354 tail->c_next->c_head = tail->c_head;
365 register CMD *head = cur->c_head;
373 str = afetch(lineary,(int)head->c_line,FALSE);
374 if (!str || str->str_nok)
376 str->str_u.str_nval = (double)head->c_line;
379 cmd->c_type = C_EXPR;
380 cmd->ucmd.acmd.ac_stab = Nullstab;
381 cmd->ucmd.acmd.ac_expr = Nullarg;
382 arg = make_op(O_ITEM,1,Nullarg,Nullarg,Nullarg);
383 arg[1].arg_type = A_SINGLE;
384 arg[1].arg_ptr.arg_str = str_nmake((double)head->c_line);
385 cmd->c_expr = make_op(O_SUBR, 2,
386 stab2arg(A_WORD,DBstab),
389 cmd->c_flags |= CF_COND|CF_DBSUB;
390 cmd->c_line = head->c_line;
391 cmd->c_label = head->c_label;
392 cmd->c_file = filename;
393 return append_line(cmd, cur);
397 make_acmd(type,stab,cond,arg)
407 cmd->ucmd.acmd.ac_stab = stab;
408 cmd->ucmd.acmd.ac_expr = arg;
411 cmd->c_flags |= CF_COND;
412 if (cmdline != NOLINE) {
413 cmd->c_line = cmdline;
416 cmd->c_file = filename;
423 make_ccmd(type,arg,cblock)
426 struct compcmd cblock;
430 Newz(108,cmd, 1, CMD);
433 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
434 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
436 cmd->c_flags |= CF_COND;
437 if (cmdline != NOLINE) {
438 cmd->c_line = cmdline;
447 make_icmd(type,arg,cblock)
450 struct compcmd cblock;
456 struct compcmd ncblock;
458 Newz(109,cmd, 1, CMD);
462 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
463 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
465 cmd->c_flags |= CF_COND;
466 if (cmdline != NOLINE) {
467 cmd->c_line = cmdline;
471 alt = cblock.comp_alt;
472 while (alt && alt->c_type == C_ELSIF) {
474 alt = alt->ucmd.ccmd.cc_alt;
476 if (alt) { /* a real life ELSE at the end? */
477 ncblock.comp_true = alt;
478 ncblock.comp_alt = Nullcmd;
479 alt = append_line(cur,make_ccmd(C_ELSE,Nullarg,ncblock));
480 cur->ucmd.ccmd.cc_alt = alt;
483 alt = cur; /* no ELSE, so cur is proxy ELSE */
486 while (cmd) { /* now point everyone at the ELSE */
488 cmd = cur->ucmd.ccmd.cc_alt;
490 if (cur->c_type == C_ELSIF)
492 if (cur->c_type == C_IF)
493 cur->ucmd.ccmd.cc_alt = alt;
504 opt_arg(cmd,fliporflop,acmd)
513 int context = 0; /* 0 = normal, 1 = before &&, 2 = before || */
514 int flp = fliporflop;
518 if (!(arg = cmd->c_expr)) {
519 cmd->c_flags &= ~CF_COND;
523 /* Can we turn && and || into if and unless? */
525 if (acmd && !cmd->ucmd.acmd.ac_expr && !(cmd->c_flags & CF_TERM) &&
526 (arg->arg_type == O_AND || arg->arg_type == O_OR) ) {
528 arg[2].arg_type &= A_MASK; /* don't suppress eval */
530 cmd->ucmd.acmd.ac_expr = arg[2].arg_ptr.arg_arg;
531 cmd->c_expr = arg[1].arg_ptr.arg_arg;
532 if (arg->arg_type == O_OR)
533 cmd->c_flags ^= CF_INVERT; /* || is like unless */
539 /* Turn "if (!expr)" into "unless (expr)" */
541 if (!(cmd->c_flags & CF_TERM)) { /* unless return value wanted */
542 while (arg->arg_type == O_NOT) {
544 cmd->c_flags ^= CF_INVERT; /* flip sense of cmd */
545 cmd->c_expr = arg[1].arg_ptr.arg_arg; /* hoist the rest of expr */
547 arg = cmd->c_expr; /* here we go again */
551 if (!arg->arg_len) { /* sanity check */
556 /* for "cond .. cond" we set up for the initial check */
558 if (arg->arg_type == O_FLIP)
561 /* for "cond && expr" and "cond || expr" we can ignore expr, sort of */
564 if (arg->arg_type == O_AND)
566 else if (arg->arg_type == O_OR)
568 if (context && (arg[flp].arg_type & A_MASK) == A_EXPR) {
569 arg = arg[flp].arg_ptr.arg_arg;
571 if (arg->arg_type == O_AND || arg->arg_type == O_OR)
574 if ((context & 3) == 3)
577 if (arg[flp].arg_flags & (AF_PRE|AF_POST)) {
579 return; /* side effect, can't optimize */
582 if (arg->arg_type == O_ITEM || arg->arg_type == O_FLIP ||
583 arg->arg_type == O_AND || arg->arg_type == O_OR) {
584 if ((arg[flp].arg_type & A_MASK) == A_SINGLE) {
585 opt = (str_true(arg[flp].arg_ptr.arg_str) ? CFT_TRUE : CFT_FALSE);
586 cmd->c_short = str_smake(arg[flp].arg_ptr.arg_str);
589 else if ((arg[flp].arg_type & A_MASK) == A_STAB ||
590 (arg[flp].arg_type & A_MASK) == A_LVAL) {
591 cmd->c_stab = arg[flp].arg_ptr.arg_stab;
594 if (!context) { /* no && or ||? */
596 cmd->c_expr = Nullarg;
599 cmd->c_flags |= CF_EQSURE;
601 cmd->c_flags |= CF_NESURE;
604 else if (arg->arg_type == O_MATCH || arg->arg_type == O_SUBST ||
605 arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST) {
606 if ((arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
607 (arg[2].arg_type & A_MASK) == A_SPAT &&
608 arg[2].arg_ptr.arg_spat->spat_short ) {
609 cmd->c_stab = arg[1].arg_ptr.arg_stab;
610 cmd->c_short = str_smake(arg[2].arg_ptr.arg_spat->spat_short);
611 cmd->c_slen = arg[2].arg_ptr.arg_spat->spat_slen;
612 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ALL &&
613 !(arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ONCE) &&
614 (arg->arg_type == O_MATCH || arg->arg_type == O_NMATCH) )
615 sure |= CF_EQSURE; /* (SUBST must be forced even */
616 /* if we know it will work.) */
617 if (arg->arg_type != O_SUBST) {
618 arg[2].arg_ptr.arg_spat->spat_short = Nullstr;
619 arg[2].arg_ptr.arg_spat->spat_slen = 0; /* only one chk */
621 sure |= CF_NESURE; /* normally only sure if it fails */
622 if (arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST)
623 cmd->c_flags |= CF_FIRSTNEG;
624 if (context & 1) { /* only sure if thing is false */
625 if (cmd->c_flags & CF_FIRSTNEG)
630 else if (context & 2) { /* only sure if thing is true */
631 if (cmd->c_flags & CF_FIRSTNEG)
636 if (sure & (CF_EQSURE|CF_NESURE)) { /* if we know anything*/
637 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_SCANFIRST)
641 if (sure == (CF_EQSURE|CF_NESURE) /* really sure? */
642 && arg->arg_type == O_MATCH
644 && fliporflop == 1) {
645 spat_free(arg[2].arg_ptr.arg_spat);
646 arg[2].arg_ptr.arg_spat = Nullspat; /* don't do twice */
648 cmd->c_flags |= sure;
652 else if (arg->arg_type == O_SEQ || arg->arg_type == O_SNE ||
653 arg->arg_type == O_SLT || arg->arg_type == O_SGT) {
654 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
655 if (arg[2].arg_type == A_SINGLE) {
656 cmd->c_stab = arg[1].arg_ptr.arg_stab;
657 cmd->c_short = str_smake(arg[2].arg_ptr.arg_str);
658 cmd->c_slen = cmd->c_short->str_cur+1;
659 switch (arg->arg_type) {
660 case O_SLT: case O_SGT:
662 cmd->c_flags |= CF_FIRSTNEG;
665 cmd->c_flags |= CF_FIRSTNEG;
668 sure |= CF_NESURE|CF_EQSURE;
671 if (context & 1) { /* only sure if thing is false */
672 if (cmd->c_flags & CF_FIRSTNEG)
677 else if (context & 2) { /* only sure if thing is true */
678 if (cmd->c_flags & CF_FIRSTNEG)
683 if (sure & (CF_EQSURE|CF_NESURE)) {
685 cmd->c_flags |= sure;
690 else if (arg->arg_type == O_EQ || arg->arg_type == O_NE ||
691 arg->arg_type == O_LE || arg->arg_type == O_GE ||
692 arg->arg_type == O_LT || arg->arg_type == O_GT) {
693 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
694 if (arg[2].arg_type == A_SINGLE) {
695 cmd->c_stab = arg[1].arg_ptr.arg_stab;
697 STR *str = arg[2].arg_ptr.arg_str;
699 if ((!str->str_nok && !looks_like_number(str)))
700 warn("Possible use of == on string value");
702 cmd->c_short = str_nmake(str_gnum(arg[2].arg_ptr.arg_str));
703 cmd->c_slen = arg->arg_type;
704 sure |= CF_NESURE|CF_EQSURE;
705 if (context & 1) { /* only sure if thing is false */
708 else if (context & 2) { /* only sure if thing is true */
711 if (sure & (CF_EQSURE|CF_NESURE)) {
713 cmd->c_flags |= sure;
718 else if (arg->arg_type == O_ASSIGN &&
719 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
720 arg[1].arg_ptr.arg_stab == defstab &&
721 arg[2].arg_type == A_EXPR ) {
722 arg2 = arg[2].arg_ptr.arg_arg;
723 if (arg2->arg_type == O_ITEM && arg2[1].arg_type == A_READ) {
725 cmd->c_stab = arg2[1].arg_ptr.arg_stab;
726 if (!(stab_io(arg2[1].arg_ptr.arg_stab)->flags & IOF_ARGV)) {
729 cmd->c_expr = Nullarg;
733 else if (arg->arg_type == O_CHOP &&
734 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) ) {
736 cmd->c_stab = arg[1].arg_ptr.arg_stab;
738 cmd->c_expr = Nullarg;
744 if (cmd->c_flags & CF_FLIP) {
745 if (fliporflop == 1) {
746 arg = cmd->c_expr; /* get back to O_FLIP arg */
747 New(110,arg[3].arg_ptr.arg_cmd, 1, CMD);
748 Copy(cmd, arg[3].arg_ptr.arg_cmd, 1, CMD);
749 New(111,arg[4].arg_ptr.arg_cmd,1,CMD);
750 Copy(cmd, arg[4].arg_ptr.arg_cmd, 1, CMD);
751 opt_arg(arg[4].arg_ptr.arg_cmd,2,acmd);
752 arg->arg_len = 2; /* this is a lie */
755 if ((opt & CF_OPTIMIZE) == CFT_EVAL)
756 cmd->c_flags = (cmd->c_flags & ~CF_OPTIMIZE) | CFT_UNFLIP;
777 cmd->c_flags |= CF_COND;
789 cmd->c_flags |= CF_COND|CF_LOOP;
791 if (!(cmd->c_flags & CF_INVERT))
792 while_io(cmd); /* add $_ =, if necessary */
794 if (cmd->c_type == C_BLOCK)
795 cmd->c_flags &= ~CF_COND;
797 arg = cmd->ucmd.acmd.ac_expr;
798 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
799 cmd->c_flags &= ~CF_COND; /* "do {} while" happens at least once */
800 if (arg && arg->arg_type == O_SUBR)
801 cmd->c_flags &= ~CF_COND; /* likewise for "do subr() while" */
810 register CMD *targ = cmd;
813 if (targ->c_flags & CF_DBSUB)
815 targ->c_flags ^= CF_INVERT;
824 char *tname = tmpbuf;
826 if (bufptr > oldoldbufptr && bufptr - oldoldbufptr < 200 &&
827 oldoldbufptr != oldbufptr && oldbufptr != bufptr) {
828 while (isspace(*oldoldbufptr))
830 strncpy(tmp2buf, oldoldbufptr, bufptr - oldoldbufptr);
831 tmp2buf[bufptr - oldoldbufptr] = '\0';
832 sprintf(tname,"next 2 tokens \"%s\"",tmp2buf);
834 else if (bufptr > oldbufptr && bufptr - oldbufptr < 200 &&
835 oldbufptr != bufptr) {
836 while (isspace(*oldbufptr))
838 strncpy(tmp2buf, oldbufptr, bufptr - oldbufptr);
839 tmp2buf[bufptr - oldbufptr] = '\0';
840 sprintf(tname,"next token \"%s\"",tmp2buf);
842 else if (yychar > 256)
843 tname = "next token ???";
845 (void)strcpy(tname,"at EOF");
846 else if (yychar < 32)
847 (void)sprintf(tname,"next char ^%c",yychar+64);
848 else if (yychar == 127)
849 (void)strcpy(tname,"at EOF");
851 (void)sprintf(tname,"next char %c",yychar);
852 (void)sprintf(buf, "%s in file %s at line %d, %s\n",
853 s,filename,line,tname);
854 if (line == multi_end && multi_start < multi_end)
855 sprintf(buf+strlen(buf),
856 " (Might be a runaway multi-line %c%c string starting on line %d)\n",
857 multi_open,multi_close,multi_start);
859 str_cat(stab_val(stabent("@",TRUE)),buf);
862 if (++error_count >= 10)
863 fatal("Too many errors\n");
870 register ARG *arg = cmd->c_expr;
873 /* hoist "while (<channel>)" up into command block */
875 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_READ) {
876 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
877 cmd->c_flags |= CFT_GETS; /* and set it to do the input */
878 cmd->c_stab = arg[1].arg_ptr.arg_stab;
879 if (stab_io(arg[1].arg_ptr.arg_stab)->flags & IOF_ARGV) {
880 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$_ =" */
881 stab2arg(A_LVAL,defstab), arg, Nullarg));
885 cmd->c_expr = Nullarg;
888 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_INDREAD) {
889 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
890 cmd->c_flags |= CFT_INDGETS; /* and set it to do the input */
891 cmd->c_stab = arg[1].arg_ptr.arg_stab;
893 cmd->c_expr = Nullarg;
895 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_GLOB) {
896 if ((cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY)
897 asgnstab = cmd->c_stab;
900 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$foo =" */
901 stab2arg(A_LVAL,asgnstab), arg, Nullarg));
902 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
914 if (cmd->c_expr && (cmd->c_flags & CF_OPTIMIZE) == CFT_FALSE)
915 opt_arg(cmd,1, cmd->c_type == C_EXPR);
917 while_io(cmd); /* add $_ =, if necessary */
919 /* First find the end of the true list */
921 tail = cmd->ucmd.ccmd.cc_true;
924 New(112,newtail, 1, CMD); /* guaranteed continue */
926 /* optimize "next" to point directly to continue block */
927 if (tail->c_type == C_EXPR &&
928 tail->ucmd.acmd.ac_expr &&
929 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
930 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
933 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
935 arg_free(tail->ucmd.acmd.ac_expr);
936 tail->c_type = C_NEXT;
937 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
938 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
940 tail->ucmd.ccmd.cc_alt = newtail;
941 tail->ucmd.ccmd.cc_true = Nullcmd;
943 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
944 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
945 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
947 tail->ucmd.ccmd.cc_alt = newtail;
949 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
950 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
951 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
952 if (!tail->ucmd.scmd.sc_next[i])
953 tail->ucmd.scmd.sc_next[i] = cmd->ucmd.ccmd.cc_alt;
956 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
957 if (!tail->ucmd.scmd.sc_next[i])
958 tail->ucmd.scmd.sc_next[i] = newtail;
967 /* if there's a continue block, link it to true block and find end */
969 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
970 tail->c_next = cmd->ucmd.ccmd.cc_alt;
973 /* optimize "next" to point directly to continue block */
974 if (tail->c_type == C_EXPR &&
975 tail->ucmd.acmd.ac_expr &&
976 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
977 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
980 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
982 arg_free(tail->ucmd.acmd.ac_expr);
983 tail->c_type = C_NEXT;
984 tail->ucmd.ccmd.cc_alt = newtail;
985 tail->ucmd.ccmd.cc_true = Nullcmd;
987 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
988 tail->ucmd.ccmd.cc_alt = newtail;
990 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
991 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
992 if (!tail->ucmd.scmd.sc_next[i])
993 tail->ucmd.scmd.sc_next[i] = newtail;
1000 for ( ; tail->c_next; tail = tail->c_next) ;
1003 /* Here's the real trick: link the end of the list back to the beginning,
1004 * inserting a "last" block to break out of the loop. This saves one or
1005 * two procedure calls every time through the loop, because of how cmd_exec
1006 * does tail recursion.
1009 tail->c_next = newtail;
1011 if (!cmd->ucmd.ccmd.cc_alt)
1012 cmd->ucmd.ccmd.cc_alt = tail; /* every loop has a continue now */
1015 (void)bcopy((char *)cmd, (char *)tail, sizeof(CMD));
1017 tail->c_type = C_EXPR;
1018 tail->c_flags ^= CF_INVERT; /* turn into "last unless" */
1019 tail->c_next = tail->ucmd.ccmd.cc_true; /* loop directly back to top */
1020 tail->ucmd.acmd.ac_expr = make_op(O_LAST,0,Nullarg,Nullarg,Nullarg);
1021 tail->ucmd.acmd.ac_stab = Nullstab;
1030 /* hoist "for $foo (@bar)" up into command block */
1032 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
1033 cmd->c_flags |= CFT_ARRAY; /* and set it to do the iteration */
1034 cmd->c_stab = eachstab;
1035 cmd->c_short = str_new(0); /* just to save a field in struct cmd */
1036 cmd->c_short->str_u.str_useful = -1;
1044 register CMD *tofree;
1045 register CMD *head = cmd;
1048 if (cmd->c_type != C_WHILE) { /* WHILE block is duplicated */
1050 Safefree(cmd->c_label);
1052 str_free(cmd->c_short);
1054 spat_free(cmd->c_spat);
1056 arg_free(cmd->c_expr);
1058 switch (cmd->c_type) {
1063 if (cmd->ucmd.ccmd.cc_true)
1064 cmd_free(cmd->ucmd.ccmd.cc_true);
1067 if (cmd->ucmd.acmd.ac_expr)
1068 arg_free(cmd->ucmd.acmd.ac_expr);
1074 if (cmd && cmd == head) /* reached end of while loop */
1084 for (i = 1; i <= arg->arg_len; i++) {
1085 switch (arg[i].arg_type & A_MASK) {
1089 if (arg->arg_type == O_AASSIGN &&
1090 arg[i].arg_ptr.arg_arg->arg_type == O_LARRAY) {
1092 stab_name(arg[i].arg_ptr.arg_arg[1].arg_ptr.arg_stab);
1094 if (strnEQ("_GEN_",name, 5)) /* array for foreach */
1095 hdelete(defstash,name,strlen(name));
1099 arg_free(arg[i].arg_ptr.arg_arg);
1102 cmd_free(arg[i].arg_ptr.arg_cmd);
1117 str_free(arg[i].arg_ptr.arg_str);
1120 spat_free(arg[i].arg_ptr.arg_spat);
1128 register SPAT *spat;
1133 if (spat->spat_runtime)
1134 arg_free(spat->spat_runtime);
1135 if (spat->spat_repl) {
1136 arg_free(spat->spat_repl);
1138 if (spat->spat_short) {
1139 str_free(spat->spat_short);
1141 if (spat->spat_regexp) {
1142 regfree(spat->spat_regexp);
1145 /* now unlink from spat list */
1147 for (entry = defstash->tbl_array['_']; entry; entry = entry->hent_next) {
1148 register HASH *stash;
1149 STAB *stab = (STAB*)entry->hent_val;
1153 stash = stab_hash(stab);
1154 if (!stash || stash->tbl_spatroot == Null(SPAT*))
1156 if (stash->tbl_spatroot == spat)
1157 stash->tbl_spatroot = spat->spat_next;
1159 for (sp = stash->tbl_spatroot;
1160 sp && sp->spat_next != spat;
1164 sp->spat_next = spat->spat_next;
1170 /* Recursively descend a command sequence and push the address of any string
1171 * that needs saving on recursion onto the tosave array.
1175 cmd_tosave(cmd,willsave)
1177 int willsave; /* willsave passes down the tree */
1179 register CMD *head = cmd;
1180 int shouldsave = FALSE; /* shouldsave passes up the tree */
1182 register CMD *lastcmd = Nullcmd;
1186 shouldsave |= spat_tosave(cmd->c_spat);
1188 shouldsave |= arg_tosave(cmd->c_expr,willsave);
1189 switch (cmd->c_type) {
1191 if (cmd->ucmd.ccmd.cc_true) {
1192 tmpsave = cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1194 /* Here we check to see if the temporary array generated for
1195 * a foreach needs to be localized because of recursion.
1197 if (tmpsave && (cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY &&
1199 lastcmd->c_type == C_EXPR &&
1200 lastcmd->ucmd.acmd.ac_expr) {
1201 ARG *arg = lastcmd->ucmd.acmd.ac_expr;
1203 if (arg->arg_type == O_ASSIGN &&
1204 arg[1].arg_type == A_LEXPR &&
1205 arg[1].arg_ptr.arg_arg->arg_type == O_LARRAY &&
1207 stab_name(arg[1].arg_ptr.arg_arg[1].arg_ptr.arg_stab),
1208 5)) { /* array generated for foreach */
1209 (void)localize(arg[1].arg_ptr.arg_arg);
1212 shouldsave |= tmpsave;
1218 if (cmd->ucmd.ccmd.cc_true)
1219 shouldsave |= cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1222 if (cmd->ucmd.acmd.ac_expr)
1223 shouldsave |= arg_tosave(cmd->ucmd.acmd.ac_expr,willsave);
1228 if (cmd && cmd == head) /* reached end of while loop */
1235 arg_tosave(arg,willsave)
1240 int shouldsave = FALSE;
1242 for (i = arg->arg_len; i >= 1; i--) {
1243 switch (arg[i].arg_type & A_MASK) {
1248 shouldsave |= arg_tosave(arg[i].arg_ptr.arg_arg,shouldsave);
1251 shouldsave |= cmd_tosave(arg[i].arg_ptr.arg_cmd,shouldsave);
1264 shouldsave |= spat_tosave(arg[i].arg_ptr.arg_spat);
1268 switch (arg->arg_type) {
1278 (void)apush(tosave,arg->arg_ptr.arg_str);
1284 register SPAT *spat;
1286 int shouldsave = FALSE;
1288 if (spat->spat_runtime)
1289 shouldsave |= arg_tosave(spat->spat_runtime,FALSE);
1290 if (spat->spat_repl) {
1291 shouldsave |= arg_tosave(spat->spat_repl,FALSE);