1 /* $Header: cons.c,v 3.0 89/10/18 15:10:23 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 89/10/18 15:10:23 lwall
18 extern char *tokename[];
21 static int cmd_tosave();
22 static int arg_tosave();
23 static int spat_tosave();
25 static bool saw_return;
33 STAB *stab = stabent(name,TRUE);
38 line_t oldline = line;
42 warn("Subroutine %s redefined",name);
45 cmd_free(stab_sub(stab)->cmd);
46 afree(stab_sub(stab)->tosave);
47 Safefree(stab_sub(stab));
49 sub->filename = filename;
51 tosave = anew(Nullstab);
52 tosave->ary_fill = 0; /* make 1 based */
53 (void)cmd_tosave(cmd,FALSE); /* this builds the tosave array */
56 struct compcmd mycompblock;
58 mycompblock.comp_true = cmd;
59 mycompblock.comp_alt = Nullcmd;
60 cmd = add_label(savestr("SUB"),make_ccmd(C_BLOCK,Nullarg,mycompblock));
66 STR *str = str_nmake((double)subline);
69 sprintf(buf,"%ld",(long)line);
71 name = str_get(subname);
72 hstore(stab_xhash(DBsub),name,strlen(name),str,0);
73 str_set(subname,"main");
85 register int last_opt = 0;
86 register STAB *last_stab = Nullstab;
87 register int count = 0;
88 register CMD *switchbeg = Nullcmd;
90 if (tail == Nullcmd) {
95 for (tail = head; tail; tail = tail->c_next) {
97 /* save one measly dereference at runtime */
98 if (tail->c_type == C_IF) {
99 if (!(tail->ucmd.ccmd.cc_alt = tail->ucmd.ccmd.cc_alt->c_next))
100 tail->c_flags |= CF_TERM;
102 else if (tail->c_type == C_EXPR) {
105 if (tail->ucmd.acmd.ac_expr)
106 arg = tail->ucmd.acmd.ac_expr;
110 if (arg->arg_type == O_RETURN)
111 tail->c_flags |= CF_TERM;
112 else if (arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
113 tail->c_flags |= CF_TERM;
117 tail->c_flags |= CF_TERM;
119 if (tail->c_expr && (tail->c_flags & CF_OPTIMIZE) == CFT_FALSE)
120 opt_arg(tail,1, tail->c_type == C_EXPR);
122 /* now do a little optimization on case-ish structures */
123 switch(tail->c_flags & (CF_OPTIMIZE|CF_FIRSTNEG|CF_INVERT)) {
125 if (stabent("*",FALSE)) { /* bad assumption here!!! */
131 opt = (tail->c_flags & CF_NESURE) ? CFT_STROP : 0;
137 opt = (tail->c_slen == O_NE ? 0 : CFT_NUMOP);
138 if ((tail->c_flags&(CF_NESURE|CF_EQSURE)) != (CF_NESURE|CF_EQSURE))
144 if (opt && opt == last_opt && tail->c_stab == last_stab)
147 if (count >= 3) { /* is this the breakeven point? */
148 if (last_opt == CFT_NUMOP)
149 make_nswitch(switchbeg,count);
151 make_cswitch(switchbeg,count);
161 last_stab = tail->c_stab;
163 if (count >= 3) { /* is this the breakeven point? */
164 if (last_opt == CFT_NUMOP)
165 make_nswitch(switchbeg,count);
167 make_cswitch(switchbeg,count);
172 /* We've spotted a sequence of CMDs that all test the value of the same
173 * spat. Thus we can insert a SWITCH in front and jump directly
174 * to the correct one.
176 make_cswitch(head,count)
183 register int min = 255;
184 register int max = 0;
186 /* make a new head in the exact same spot */
187 New(102,cur, 1, CMD);
191 Copy(head,cur,1,CMD);
194 head->c_type = C_CSWITCH;
195 head->c_next = cur; /* insert new cmd at front of list */
196 head->c_stab = cur->c_stab;
198 Newz(103,loc,258,CMD*);
199 loc++; /* lie a little */
201 if ((cur->c_flags & CF_OPTIMIZE) == CFT_CCLASS) {
202 for (i = 0; i <= 255; i++) {
203 if (!loc[i] && cur->c_short->str_ptr[i>>3] & (1 << (i&7))) {
213 i = *cur->c_short->str_ptr & 255;
226 Copy(&loc[min],&loc[0], max - min, CMD*);
230 for (i = 0; i <= max; i++)
233 Renew(loc,max+1,CMD*); /* chop it down to size */
234 head->ucmd.scmd.sc_offset = min;
235 head->ucmd.scmd.sc_max = max;
236 head->ucmd.scmd.sc_next = loc;
239 make_nswitch(head,count)
243 register CMD *cur = head;
246 register int min = 32767;
247 register int max = -32768;
248 int origcount = count;
249 double value; /* or your money back! */
250 short changed; /* so triple your money back! */
253 i = (int)str_gnum(cur->c_short);
255 if (value != cur->c_short->str_u.str_nval)
256 return; /* fractional values--just forget it */
259 return; /* too big for a short */
260 if (cur->c_slen == O_LE)
262 else if (cur->c_slen == O_GE) /* we only do < or > here */
271 if (max - min > count * 2 + 10) /* too sparse? */
274 /* now make a new head in the exact same spot */
275 New(104,cur, 1, CMD);
279 Copy(head,cur,1,CMD);
282 head->c_type = C_NSWITCH;
283 head->c_next = cur; /* insert new cmd at front of list */
284 head->c_stab = cur->c_stab;
286 Newz(105,loc, max - min + 3, CMD*);
289 i = (int)str_gnum(cur->c_short);
293 switch(cur->c_slen) {
297 for (i--; i >= -1; i--)
304 for (i++; i <= max; i++)
317 for (i = 0; i <= max; i++)
320 head->ucmd.scmd.sc_offset = min;
321 head->ucmd.scmd.sc_max = max;
322 head->ucmd.scmd.sc_next = loc;
326 append_line(head,tail)
332 if (!tail->c_head) /* make sure tail is well formed */
334 if (head != Nullcmd) {
335 tail = tail->c_head; /* get to start of tail list */
337 head->c_head = head; /* start a new head list */
338 while (head->c_next) {
339 head->c_next->c_head = head->c_head;
340 head = head->c_next; /* get to end of head list */
342 head->c_next = tail; /* link to end of old list */
343 tail->c_head = head->c_head; /* propagate head pointer */
345 while (tail->c_next) {
346 tail->c_next->c_head = tail->c_head;
357 register CMD *head = cur->c_head;
365 str = afetch(lineary,(int)head->c_line,FALSE);
366 if (!str || str->str_nok)
368 str->str_u.str_nval = (double)head->c_line;
371 cmd->c_type = C_EXPR;
372 cmd->ucmd.acmd.ac_stab = Nullstab;
373 cmd->ucmd.acmd.ac_expr = Nullarg;
374 arg = make_op(O_ITEM,1,Nullarg,Nullarg,Nullarg);
375 arg[1].arg_type = A_SINGLE;
376 arg[1].arg_ptr.arg_str = str_nmake((double)head->c_line);
377 cmd->c_expr = make_op(O_SUBR, 2,
378 stab2arg(A_WORD,DBstab),
381 cmd->c_flags |= CF_COND;
382 cmd->c_line = head->c_line;
383 cmd->c_label = head->c_label;
384 cmd->c_file = filename;
385 return append_line(cmd, cur);
389 make_acmd(type,stab,cond,arg)
399 cmd->ucmd.acmd.ac_stab = stab;
400 cmd->ucmd.acmd.ac_expr = arg;
403 cmd->c_flags |= CF_COND;
404 if (cmdline != NOLINE) {
405 cmd->c_line = cmdline;
408 cmd->c_file = filename;
415 make_ccmd(type,arg,cblock)
418 struct compcmd cblock;
422 Newz(108,cmd, 1, CMD);
425 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
426 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
428 cmd->c_flags |= CF_COND;
429 if (cmdline != NOLINE) {
430 cmd->c_line = cmdline;
439 make_icmd(type,arg,cblock)
442 struct compcmd cblock;
448 struct compcmd ncblock;
450 Newz(109,cmd, 1, CMD);
454 cmd->ucmd.ccmd.cc_true = cblock.comp_true;
455 cmd->ucmd.ccmd.cc_alt = cblock.comp_alt;
457 cmd->c_flags |= CF_COND;
458 if (cmdline != NOLINE) {
459 cmd->c_line = cmdline;
463 alt = cblock.comp_alt;
464 while (alt && alt->c_type == C_ELSIF) {
466 alt = alt->ucmd.ccmd.cc_alt;
468 if (alt) { /* a real life ELSE at the end? */
469 ncblock.comp_true = alt;
470 ncblock.comp_alt = Nullcmd;
471 alt = append_line(cur,make_ccmd(C_ELSE,Nullarg,ncblock));
472 cur->ucmd.ccmd.cc_alt = alt;
475 alt = cur; /* no ELSE, so cur is proxy ELSE */
478 while (cmd) { /* now point everyone at the ELSE */
480 cmd = cur->ucmd.ccmd.cc_alt;
482 if (cur->c_type == C_ELSIF)
484 if (cur->c_type == C_IF)
485 cur->ucmd.ccmd.cc_alt = alt;
496 opt_arg(cmd,fliporflop,acmd)
505 int context = 0; /* 0 = normal, 1 = before &&, 2 = before || */
506 int flp = fliporflop;
510 if (!(arg = cmd->c_expr)) {
511 cmd->c_flags &= ~CF_COND;
515 /* Can we turn && and || into if and unless? */
517 if (acmd && !cmd->ucmd.acmd.ac_expr && !(cmd->c_flags & CF_TERM) &&
518 (arg->arg_type == O_AND || arg->arg_type == O_OR) ) {
520 arg[2].arg_type &= A_MASK; /* don't suppress eval */
522 cmd->ucmd.acmd.ac_expr = arg[2].arg_ptr.arg_arg;
523 cmd->c_expr = arg[1].arg_ptr.arg_arg;
524 if (arg->arg_type == O_OR)
525 cmd->c_flags ^= CF_INVERT; /* || is like unless */
531 /* Turn "if (!expr)" into "unless (expr)" */
533 if (!(cmd->c_flags & CF_TERM)) { /* unless return value wanted */
534 while (arg->arg_type == O_NOT) {
536 cmd->c_flags ^= CF_INVERT; /* flip sense of cmd */
537 cmd->c_expr = arg[1].arg_ptr.arg_arg; /* hoist the rest of expr */
539 arg = cmd->c_expr; /* here we go again */
543 if (!arg->arg_len) { /* sanity check */
548 /* for "cond .. cond" we set up for the initial check */
550 if (arg->arg_type == O_FLIP)
553 /* for "cond && expr" and "cond || expr" we can ignore expr, sort of */
556 if (arg->arg_type == O_AND)
558 else if (arg->arg_type == O_OR)
560 if (context && (arg[flp].arg_type & A_MASK) == A_EXPR) {
561 arg = arg[flp].arg_ptr.arg_arg;
563 if (arg->arg_type == O_AND || arg->arg_type == O_OR)
566 if ((context & 3) == 3)
569 if (arg[flp].arg_flags & (AF_PRE|AF_POST)) {
571 return; /* side effect, can't optimize */
574 if (arg->arg_type == O_ITEM || arg->arg_type == O_FLIP ||
575 arg->arg_type == O_AND || arg->arg_type == O_OR) {
576 if ((arg[flp].arg_type & A_MASK) == A_SINGLE) {
577 opt = (str_true(arg[flp].arg_ptr.arg_str) ? CFT_TRUE : CFT_FALSE);
578 cmd->c_short = str_smake(arg[flp].arg_ptr.arg_str);
581 else if ((arg[flp].arg_type & A_MASK) == A_STAB ||
582 (arg[flp].arg_type & A_MASK) == A_LVAL) {
583 cmd->c_stab = arg[flp].arg_ptr.arg_stab;
586 if (!context) { /* no && or ||? */
588 cmd->c_expr = Nullarg;
591 cmd->c_flags |= CF_EQSURE;
593 cmd->c_flags |= CF_NESURE;
596 else if (arg->arg_type == O_MATCH || arg->arg_type == O_SUBST ||
597 arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST) {
598 if ((arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
599 (arg[2].arg_type & A_MASK) == A_SPAT &&
600 arg[2].arg_ptr.arg_spat->spat_short ) {
601 cmd->c_stab = arg[1].arg_ptr.arg_stab;
602 cmd->c_short = str_smake(arg[2].arg_ptr.arg_spat->spat_short);
603 cmd->c_slen = arg[2].arg_ptr.arg_spat->spat_slen;
604 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ALL &&
605 !(arg[2].arg_ptr.arg_spat->spat_flags & SPAT_ONCE) &&
606 (arg->arg_type == O_MATCH || arg->arg_type == O_NMATCH) )
607 sure |= CF_EQSURE; /* (SUBST must be forced even */
608 /* if we know it will work.) */
609 if (arg->arg_type != O_SUBST) {
610 arg[2].arg_ptr.arg_spat->spat_short = Nullstr;
611 arg[2].arg_ptr.arg_spat->spat_slen = 0; /* only one chk */
613 sure |= CF_NESURE; /* normally only sure if it fails */
614 if (arg->arg_type == O_NMATCH || arg->arg_type == O_NSUBST)
615 cmd->c_flags |= CF_FIRSTNEG;
616 if (context & 1) { /* only sure if thing is false */
617 if (cmd->c_flags & CF_FIRSTNEG)
622 else if (context & 2) { /* only sure if thing is true */
623 if (cmd->c_flags & CF_FIRSTNEG)
628 if (sure & (CF_EQSURE|CF_NESURE)) { /* if we know anything*/
629 if (arg[2].arg_ptr.arg_spat->spat_flags & SPAT_SCANFIRST)
633 if (sure == (CF_EQSURE|CF_NESURE) /* really sure? */
634 && arg->arg_type == O_MATCH
636 && fliporflop == 1) {
637 spat_free(arg[2].arg_ptr.arg_spat);
638 arg[2].arg_ptr.arg_spat = Nullspat; /* don't do twice */
640 cmd->c_flags |= sure;
644 else if (arg->arg_type == O_SEQ || arg->arg_type == O_SNE ||
645 arg->arg_type == O_SLT || arg->arg_type == O_SGT) {
646 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
647 if (arg[2].arg_type == A_SINGLE) {
648 cmd->c_stab = arg[1].arg_ptr.arg_stab;
649 cmd->c_short = str_smake(arg[2].arg_ptr.arg_str);
650 cmd->c_slen = cmd->c_short->str_cur+1;
651 switch (arg->arg_type) {
652 case O_SLT: case O_SGT:
654 cmd->c_flags |= CF_FIRSTNEG;
657 cmd->c_flags |= CF_FIRSTNEG;
660 sure |= CF_NESURE|CF_EQSURE;
663 if (context & 1) { /* only sure if thing is false */
664 if (cmd->c_flags & CF_FIRSTNEG)
669 else if (context & 2) { /* only sure if thing is true */
670 if (cmd->c_flags & CF_FIRSTNEG)
675 if (sure & (CF_EQSURE|CF_NESURE)) {
677 cmd->c_flags |= sure;
682 else if (arg->arg_type == O_EQ || arg->arg_type == O_NE ||
683 arg->arg_type == O_LE || arg->arg_type == O_GE ||
684 arg->arg_type == O_LT || arg->arg_type == O_GT) {
685 if (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) {
686 if (arg[2].arg_type == A_SINGLE) {
687 cmd->c_stab = arg[1].arg_ptr.arg_stab;
689 STR *str = arg[2].arg_ptr.arg_str;
691 if ((!str->str_nok && !looks_like_number(str)))
692 warn("Possible use of == on string value");
694 cmd->c_short = str_nmake(str_gnum(arg[2].arg_ptr.arg_str));
695 cmd->c_slen = arg->arg_type;
696 sure |= CF_NESURE|CF_EQSURE;
697 if (context & 1) { /* only sure if thing is false */
700 else if (context & 2) { /* only sure if thing is true */
703 if (sure & (CF_EQSURE|CF_NESURE)) {
705 cmd->c_flags |= sure;
710 else if (arg->arg_type == O_ASSIGN &&
711 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) &&
712 arg[1].arg_ptr.arg_stab == defstab &&
713 arg[2].arg_type == A_EXPR ) {
714 arg2 = arg[2].arg_ptr.arg_arg;
715 if (arg2->arg_type == O_ITEM && arg2[1].arg_type == A_READ) {
717 cmd->c_stab = arg2[1].arg_ptr.arg_stab;
718 if (!(stab_io(arg2[1].arg_ptr.arg_stab)->flags & IOF_ARGV)) {
721 cmd->c_expr = Nullarg;
725 else if (arg->arg_type == O_CHOP &&
726 (arg[1].arg_type == A_STAB || arg[1].arg_type == A_LVAL) ) {
728 cmd->c_stab = arg[1].arg_ptr.arg_stab;
730 cmd->c_expr = Nullarg;
736 if (cmd->c_flags & CF_FLIP) {
737 if (fliporflop == 1) {
738 arg = cmd->c_expr; /* get back to O_FLIP arg */
739 New(110,arg[3].arg_ptr.arg_cmd, 1, CMD);
740 Copy(cmd, arg[3].arg_ptr.arg_cmd, 1, CMD);
741 New(111,arg[4].arg_ptr.arg_cmd,1,CMD);
742 Copy(cmd, arg[4].arg_ptr.arg_cmd, 1, CMD);
743 opt_arg(arg[4].arg_ptr.arg_cmd,2,acmd);
744 arg->arg_len = 2; /* this is a lie */
747 if ((opt & CF_OPTIMIZE) == CFT_EVAL)
748 cmd->c_flags = (cmd->c_flags & ~CF_OPTIMIZE) | CFT_UNFLIP;
769 cmd->c_flags |= CF_COND;
781 cmd->c_flags |= CF_COND|CF_LOOP;
783 if (!(cmd->c_flags & CF_INVERT))
784 while_io(cmd); /* add $_ =, if necessary */
786 if (cmd->c_type == C_BLOCK)
787 cmd->c_flags &= ~CF_COND;
789 arg = cmd->ucmd.acmd.ac_expr;
790 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_CMD)
791 cmd->c_flags &= ~CF_COND; /* "do {} while" happens at least once */
792 if (arg && arg->arg_type == O_SUBR)
793 cmd->c_flags &= ~CF_COND; /* likewise for "do subr() while" */
803 cmd->c_head->c_flags ^= CF_INVERT;
805 cmd->c_flags ^= CF_INVERT;
814 char *tname = tmpbuf;
816 if (bufptr > oldoldbufptr && bufptr - oldoldbufptr < 200 &&
817 oldoldbufptr != oldbufptr && oldbufptr != bufptr) {
818 while (isspace(*oldoldbufptr))
820 strncpy(tmp2buf, oldoldbufptr, bufptr - oldoldbufptr);
821 tmp2buf[bufptr - oldoldbufptr] = '\0';
822 sprintf(tname,"next 2 tokens \"%s\"",tmp2buf);
824 else if (bufptr > oldbufptr && bufptr - oldbufptr < 200 &&
825 oldbufptr != bufptr) {
826 while (isspace(*oldbufptr))
828 strncpy(tmp2buf, oldbufptr, bufptr - oldbufptr);
829 tmp2buf[bufptr - oldbufptr] = '\0';
830 sprintf(tname,"next token \"%s\"",tmp2buf);
832 else if (yychar > 256)
833 tname = "next token ???";
835 (void)strcpy(tname,"at EOF");
836 else if (yychar < 32)
837 (void)sprintf(tname,"next char ^%c",yychar+64);
838 else if (yychar == 127)
839 (void)strcpy(tname,"at EOF");
841 (void)sprintf(tname,"next char %c",yychar);
842 (void)sprintf(buf, "%s in file %s at line %d, %s\n",
843 s,filename,line,tname);
844 if (line == multi_end && multi_start < multi_end)
845 sprintf(buf+strlen(buf),
846 " (Might be a runaway multi-line %c%c string starting on line %d)\n",
847 multi_open,multi_close,multi_start);
849 str_cat(stab_val(stabent("@",TRUE)),buf);
852 if (++error_count >= 10)
853 fatal("Too many errors\n");
860 register ARG *arg = cmd->c_expr;
863 /* hoist "while (<channel>)" up into command block */
865 if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_READ) {
866 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
867 cmd->c_flags |= CFT_GETS; /* and set it to do the input */
868 cmd->c_stab = arg[1].arg_ptr.arg_stab;
869 if (stab_io(arg[1].arg_ptr.arg_stab)->flags & IOF_ARGV) {
870 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$_ =" */
871 stab2arg(A_LVAL,defstab), arg, Nullarg));
875 cmd->c_expr = Nullarg;
878 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_INDREAD) {
879 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
880 cmd->c_flags |= CFT_INDGETS; /* and set it to do the input */
881 cmd->c_stab = arg[1].arg_ptr.arg_stab;
883 cmd->c_expr = Nullarg;
885 else if (arg && arg->arg_type == O_ITEM && arg[1].arg_type == A_GLOB) {
886 if ((cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY)
887 asgnstab = cmd->c_stab;
890 cmd->c_expr = l(make_op(O_ASSIGN, 2, /* fake up "$foo =" */
891 stab2arg(A_LVAL,asgnstab), arg, Nullarg));
892 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
904 if (cmd->c_expr && (cmd->c_flags & CF_OPTIMIZE) == CFT_FALSE)
905 opt_arg(cmd,1, cmd->c_type == C_EXPR);
907 while_io(cmd); /* add $_ =, if necessary */
909 /* First find the end of the true list */
911 tail = cmd->ucmd.ccmd.cc_true;
914 New(112,newtail, 1, CMD); /* guaranteed continue */
916 /* optimize "next" to point directly to continue block */
917 if (tail->c_type == C_EXPR &&
918 tail->ucmd.acmd.ac_expr &&
919 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
920 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
923 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
925 arg_free(tail->ucmd.acmd.ac_expr);
926 tail->c_type = C_NEXT;
927 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
928 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
930 tail->ucmd.ccmd.cc_alt = newtail;
931 tail->ucmd.ccmd.cc_true = Nullcmd;
933 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
934 if (cmd->ucmd.ccmd.cc_alt != Nullcmd)
935 tail->ucmd.ccmd.cc_alt = cmd->ucmd.ccmd.cc_alt;
937 tail->ucmd.ccmd.cc_alt = newtail;
939 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
940 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
941 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
942 if (!tail->ucmd.scmd.sc_next[i])
943 tail->ucmd.scmd.sc_next[i] = cmd->ucmd.ccmd.cc_alt;
946 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
947 if (!tail->ucmd.scmd.sc_next[i])
948 tail->ucmd.scmd.sc_next[i] = newtail;
957 /* if there's a continue block, link it to true block and find end */
959 if (cmd->ucmd.ccmd.cc_alt != Nullcmd) {
960 tail->c_next = cmd->ucmd.ccmd.cc_alt;
963 /* optimize "next" to point directly to continue block */
964 if (tail->c_type == C_EXPR &&
965 tail->ucmd.acmd.ac_expr &&
966 tail->ucmd.acmd.ac_expr->arg_type == O_NEXT &&
967 (tail->ucmd.acmd.ac_expr->arg_len == 0 ||
970 tail->ucmd.acmd.ac_expr[1].arg_ptr.arg_str->str_ptr) )))
972 arg_free(tail->ucmd.acmd.ac_expr);
973 tail->c_type = C_NEXT;
974 tail->ucmd.ccmd.cc_alt = newtail;
975 tail->ucmd.ccmd.cc_true = Nullcmd;
977 else if (tail->c_type == C_IF && !tail->ucmd.ccmd.cc_alt) {
978 tail->ucmd.ccmd.cc_alt = newtail;
980 else if (tail->c_type == C_CSWITCH || tail->c_type == C_NSWITCH) {
981 for (i = tail->ucmd.scmd.sc_max; i >= 0; i--)
982 if (!tail->ucmd.scmd.sc_next[i])
983 tail->ucmd.scmd.sc_next[i] = newtail;
990 for ( ; tail->c_next; tail = tail->c_next) ;
993 /* Here's the real trick: link the end of the list back to the beginning,
994 * inserting a "last" block to break out of the loop. This saves one or
995 * two procedure calls every time through the loop, because of how cmd_exec
996 * does tail recursion.
999 tail->c_next = newtail;
1001 if (!cmd->ucmd.ccmd.cc_alt)
1002 cmd->ucmd.ccmd.cc_alt = tail; /* every loop has a continue now */
1005 (void)bcopy((char *)cmd, (char *)tail, sizeof(CMD));
1007 tail->c_type = C_EXPR;
1008 tail->c_flags ^= CF_INVERT; /* turn into "last unless" */
1009 tail->c_next = tail->ucmd.ccmd.cc_true; /* loop directly back to top */
1010 tail->ucmd.acmd.ac_expr = make_op(O_LAST,0,Nullarg,Nullarg,Nullarg);
1011 tail->ucmd.acmd.ac_stab = Nullstab;
1020 /* hoist "for $foo (@bar)" up into command block */
1022 cmd->c_flags &= ~CF_OPTIMIZE; /* clear optimization type */
1023 cmd->c_flags |= CFT_ARRAY; /* and set it to do the iteration */
1024 cmd->c_stab = eachstab;
1032 register CMD *tofree;
1033 register CMD *head = cmd;
1036 if (cmd->c_type != C_WHILE) { /* WHILE block is duplicated */
1038 Safefree(cmd->c_label);
1040 str_free(cmd->c_short);
1042 spat_free(cmd->c_spat);
1044 arg_free(cmd->c_expr);
1046 switch (cmd->c_type) {
1051 if (cmd->ucmd.ccmd.cc_true)
1052 cmd_free(cmd->ucmd.ccmd.cc_true);
1055 if (cmd->ucmd.acmd.ac_expr)
1056 arg_free(cmd->ucmd.acmd.ac_expr);
1062 if (cmd && cmd == head) /* reached end of while loop */
1072 for (i = 1; i <= arg->arg_len; i++) {
1073 switch (arg[i].arg_type & A_MASK) {
1077 if (arg->arg_type == O_AASSIGN &&
1078 arg[i].arg_ptr.arg_arg->arg_type == O_LARRAY) {
1080 stab_name(arg[i].arg_ptr.arg_arg[1].arg_ptr.arg_stab);
1082 if (strnEQ("_GEN_",name, 5)) /* array for foreach */
1083 hdelete(defstash,name,strlen(name));
1087 arg_free(arg[i].arg_ptr.arg_arg);
1090 cmd_free(arg[i].arg_ptr.arg_cmd);
1105 str_free(arg[i].arg_ptr.arg_str);
1108 spat_free(arg[i].arg_ptr.arg_spat);
1116 register SPAT *spat;
1121 if (spat->spat_runtime)
1122 arg_free(spat->spat_runtime);
1123 if (spat->spat_repl) {
1124 arg_free(spat->spat_repl);
1126 if (spat->spat_short) {
1127 str_free(spat->spat_short);
1129 if (spat->spat_regexp) {
1130 regfree(spat->spat_regexp);
1133 /* now unlink from spat list */
1135 for (entry = defstash->tbl_array['_']; entry; entry = entry->hent_next) {
1136 register HASH *stash;
1137 STAB *stab = (STAB*)entry->hent_val;
1141 stash = stab_hash(stab);
1142 if (!stash || stash->tbl_spatroot == Null(SPAT*))
1144 if (stash->tbl_spatroot == spat)
1145 stash->tbl_spatroot = spat->spat_next;
1147 for (sp = stash->tbl_spatroot;
1148 sp && sp->spat_next != spat;
1152 sp->spat_next = spat->spat_next;
1158 /* Recursively descend a command sequence and push the address of any string
1159 * that needs saving on recursion onto the tosave array.
1163 cmd_tosave(cmd,willsave)
1165 int willsave; /* willsave passes down the tree */
1167 register CMD *head = cmd;
1168 int shouldsave = FALSE; /* shouldsave passes up the tree */
1170 register CMD *lastcmd = Nullcmd;
1174 shouldsave |= spat_tosave(cmd->c_spat);
1176 shouldsave |= arg_tosave(cmd->c_expr,willsave);
1177 switch (cmd->c_type) {
1179 if (cmd->ucmd.ccmd.cc_true) {
1180 tmpsave = cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1182 /* Here we check to see if the temporary array generated for
1183 * a foreach needs to be localized because of recursion.
1185 if (tmpsave && (cmd->c_flags & CF_OPTIMIZE) == CFT_ARRAY &&
1187 lastcmd->c_type == C_EXPR &&
1188 lastcmd->ucmd.acmd.ac_expr) {
1189 ARG *arg = lastcmd->ucmd.acmd.ac_expr;
1191 if (arg->arg_type == O_ASSIGN &&
1192 arg[1].arg_type == A_LEXPR &&
1193 arg[1].arg_ptr.arg_arg->arg_type == O_LARRAY &&
1195 stab_name(arg[1].arg_ptr.arg_arg[1].arg_ptr.arg_stab),
1196 5)) { /* array generated for foreach */
1197 (void)localize(arg[1].arg_ptr.arg_arg);
1200 shouldsave |= tmpsave;
1206 if (cmd->ucmd.ccmd.cc_true)
1207 shouldsave |= cmd_tosave(cmd->ucmd.ccmd.cc_true,willsave);
1210 if (cmd->ucmd.acmd.ac_expr)
1211 shouldsave |= arg_tosave(cmd->ucmd.acmd.ac_expr,willsave);
1216 if (cmd && cmd == head) /* reached end of while loop */
1223 arg_tosave(arg,willsave)
1228 int shouldsave = FALSE;
1230 for (i = arg->arg_len; i >= 1; i--) {
1231 switch (arg[i].arg_type & A_MASK) {
1236 shouldsave |= arg_tosave(arg[i].arg_ptr.arg_arg,shouldsave);
1239 shouldsave |= cmd_tosave(arg[i].arg_ptr.arg_cmd,shouldsave);
1252 shouldsave |= spat_tosave(arg[i].arg_ptr.arg_spat);
1256 switch (arg->arg_type) {
1266 (void)apush(tosave,arg->arg_ptr.arg_str);
1272 register SPAT *spat;
1274 int shouldsave = FALSE;
1276 if (spat->spat_runtime)
1277 shouldsave |= arg_tosave(spat->spat_runtime,FALSE);
1278 if (spat->spat_repl) {
1279 shouldsave |= arg_tosave(spat->spat_repl,FALSE);