home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / exec.c < prev    next >
C/C++ Source or Header  |  1994-07-06  |  21KB  |  923 lines

  1. /****************************************************************
  2. Copyright 1990, 1993, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "p1defs.h"
  26. #include "names.h"
  27.  
  28. static void exar2 Argdcl((int, tagptr, struct Labelblock*, struct Labelblock*));
  29. static void popctl Argdcl((void));
  30. static void pushctl Argdcl((int));
  31.  
  32. /*   Logical IF codes
  33. */
  34.  
  35.  void
  36. #ifdef KR_headers
  37. exif(p)
  38.     expptr p;
  39. #else
  40. exif(expptr p)
  41. #endif
  42. {
  43.     pushctl(CTLIF);
  44.     putif(p, 0);    /* 0 => if, not elseif */
  45. }
  46.  
  47.  
  48.  void
  49. #ifdef KR_headers
  50. exelif(p)
  51.     expptr p;
  52. #else
  53. exelif(expptr p)
  54. #endif
  55. {
  56.     if (ctlstack->ctltype == CTLIF || ctlstack->ctltype == CTLIFX)
  57.     putif(p, 1);    /* 1 ==> elseif */
  58.     else
  59.     execerr("elseif out of place", CNULL);
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  void
  66. exelse(Void)
  67. {
  68.     register struct Ctlframe *c;
  69.  
  70.     for(c = ctlstack; c->ctltype == CTLIFX; --c);
  71.     if(c->ctltype == CTLIF) {
  72.         p1_else ();
  73.         c->ctltype = CTLELSE;
  74.         }
  75.     else
  76.         execerr("else out of place", CNULL);
  77.     }
  78.  
  79.  void
  80. #ifdef KR_headers
  81. exendif()
  82. #else
  83. exendif()
  84. #endif
  85. {
  86.     while(ctlstack->ctltype == CTLIFX) {
  87.         popctl();
  88.         p1else_end();
  89.         }
  90.     if(ctlstack->ctltype == CTLIF) {
  91.         popctl();
  92.         p1_endif ();
  93.         }
  94.     else if(ctlstack->ctltype == CTLELSE) {
  95.         popctl();
  96.         p1else_end ();
  97.         }
  98.     else
  99.         execerr("endif out of place", CNULL);
  100.     }
  101.  
  102.  
  103.  void
  104. #ifdef KR_headers
  105. new_endif()
  106. #else
  107. new_endif()
  108. #endif
  109. {
  110.     if (ctlstack->ctltype == CTLIF || ctlstack->ctltype == CTLIFX)
  111.         pushctl(CTLIFX);
  112.     else
  113.         err("new_endif bug");
  114.     }
  115.  
  116. /* pushctl -- Start a new control construct, initialize the labels (to
  117.    zero) */
  118.  
  119.  LOCAL void
  120. #ifdef KR_headers
  121. pushctl(code)
  122.     int code;
  123. #else
  124. pushctl(int code)
  125. #endif
  126. {
  127.     register int i;
  128.  
  129.     if(++ctlstack >= lastctl)
  130.         many("loops or if-then-elses", 'c', maxctl);
  131.     ctlstack->ctltype = code;
  132.     for(i = 0 ; i < 4 ; ++i)
  133.         ctlstack->ctlabels[i] = 0;
  134.     ctlstack->dowhile = 0;
  135.     ++blklevel;
  136. }
  137.  
  138.  
  139.  LOCAL void
  140. popctl(Void)
  141. {
  142.     if( ctlstack-- < ctls )
  143.         Fatal("control stack empty");
  144.     --blklevel;
  145. }
  146.  
  147.  
  148.  
  149. /* poplab -- update the flags in   labeltab   */
  150.  
  151.  LOCAL void
  152. poplab(Void)
  153. {
  154.     register struct Labelblock  *lp;
  155.  
  156.     for(lp = labeltab ; lp < highlabtab ; ++lp)
  157.         if(lp->labdefined)
  158.         {
  159.             /* mark all labels in inner blocks unreachable */
  160.             if(lp->blklevel > blklevel)
  161.                 lp->labinacc = YES;
  162.         }
  163.         else if(lp->blklevel > blklevel)
  164.         {
  165.             /* move all labels referred to in inner blocks out a level */
  166.             lp->blklevel = blklevel;
  167.         }
  168. }
  169.  
  170.  
  171. /*  BRANCHING CODE
  172. */
  173.  void
  174. #ifdef KR_headers
  175. exgoto(lab)
  176.     struct Labelblock *lab;
  177. #else
  178. exgoto(struct Labelblock *lab)
  179. #endif
  180. {
  181.     lab->labused = 1;
  182.     p1_goto (lab -> stateno);
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  void
  191. #ifdef KR_headers
  192. exequals(lp, rp)
  193.     register struct Primblock *lp;
  194.     register expptr rp;
  195. #else
  196. exequals(register struct Primblock *lp, register expptr rp)
  197. #endif
  198. {
  199.     if(lp->tag != TPRIM)
  200.     {
  201.         err("assignment to a non-variable");
  202.         frexpr((expptr)lp);
  203.         frexpr(rp);
  204.     }
  205.     else if(lp->namep->vclass!=CLVAR && lp->argsp)
  206.     {
  207.         if(parstate >= INEXEC)
  208.             errstr("statement function %.62s amid executables.",
  209.                 lp->namep->fvarname);
  210.         mkstfunct(lp, rp);
  211.     }
  212.     else
  213.     {
  214.         expptr new_lp, new_rp;
  215.  
  216.         if(parstate < INDATA)
  217.             enddcl();
  218.         new_lp = mklhs (lp, keepsubs);
  219.         new_rp = fixtype (rp);
  220.         puteq(new_lp, new_rp);
  221.     }
  222. }
  223.  
  224.  
  225.  
  226. /* Make Statement Function */
  227.  
  228. long laststfcn = -1, thisstno;
  229. int doing_stmtfcn;
  230.  
  231.  void
  232. #ifdef KR_headers
  233. mkstfunct(lp, rp)
  234.     struct Primblock *lp;
  235.     expptr rp;
  236. #else
  237. mkstfunct(struct Primblock *lp, expptr rp)
  238. #endif
  239. {
  240.     register struct Primblock *p;
  241.     register Namep np;
  242.     chainp args;
  243.  
  244.     laststfcn = thisstno;
  245.     np = lp->namep;
  246.     if(np->vclass == CLUNKNOWN)
  247.         np->vclass = CLPROC;
  248.     else
  249.     {
  250.         dclerr("redeclaration of statement function", np);
  251.         return;
  252.     }
  253.     np->vprocclass = PSTFUNCT;
  254.     np->vstg = STGSTFUNCT;
  255.  
  256. /* Set the type of the function */
  257.  
  258.     impldcl(np);
  259.     if (np->vtype == TYCHAR && !np->vleng)
  260.         err("character statement function with length (*)");
  261.     args = (lp->argsp ? lp->argsp->listp : CHNULL);
  262.     np->varxptr.vstfdesc = mkchain((char *)args, (chainp)rp);
  263.  
  264.     for(doing_stmtfcn = 1 ; args ; args = args->nextp)
  265.  
  266. /* It is an error for the formal parameters to have arguments or
  267.    subscripts */
  268.  
  269.         if( ((tagptr)(args->datap))->tag!=TPRIM ||
  270.             (p = (struct Primblock *)(args->datap) )->argsp ||
  271.             p->fcharp || p->lcharp ) {
  272.             err("non-variable argument in statement function definition");
  273.             args->datap = 0;
  274.             }
  275.         else
  276.         {
  277.  
  278. /* Replace the name on the left-hand side */
  279.  
  280.             args->datap = (char *)p->namep;
  281.             vardcl(p -> namep);
  282.             free((char *)p);
  283.         }
  284.     doing_stmtfcn = 0;
  285. }
  286.  
  287.  static void
  288. #ifdef KR_headers
  289. mixed_type(np)
  290.     Namep np;
  291. #else
  292. mixed_type(Namep np)
  293. #endif
  294. {
  295.     char buf[128];
  296.     sprintf(buf, "%s function %.90s invoked as subroutine",
  297.         ftn_types[np->vtype], np->fvarname);
  298.     warn(buf);
  299.     }
  300.  
  301.  void
  302. #ifdef KR_headers
  303. excall(name, args, nstars, labels)
  304.     Namep name;
  305.     struct Listblock *args;
  306.     int nstars;
  307.     struct Labelblock **labels;
  308. #else
  309. excall(Namep name, struct Listblock *args, int nstars, struct Labelblock **labels)
  310. #endif
  311. {
  312.     register expptr p;
  313.  
  314.     if (name->vtype != TYSUBR) {
  315.         if (name->vinfproc && !name->vcalled) {
  316.             name->vtype = TYSUBR;
  317.             frexpr(name->vleng);
  318.             name->vleng = 0;
  319.             }
  320.         else if (!name->vimpltype && name->vtype != TYUNKNOWN)
  321.             mixed_type(name);
  322.         else
  323.             settype(name, TYSUBR, (ftnint)0);
  324.         }
  325.     p = mkfunct( mkprim(name, args, CHNULL) );
  326.     if (p->tag == TERROR)
  327.         return;
  328.  
  329. /* Subroutines and their identifiers acquire the type INT */
  330.  
  331.     p->exprblock.vtype = p->exprblock.leftp->headblock.vtype = TYINT;
  332.  
  333. /* Handle the alternate return mechanism */
  334.  
  335.     if(nstars > 0)
  336.         putcmgo(putx(fixtype(p)), nstars, labels);
  337.     else
  338.         putexpr(p);
  339. }
  340.  
  341.  
  342.  void
  343. #ifdef KR_headers
  344. exstop(stop, p)
  345.     int stop;
  346.     register expptr p;
  347. #else
  348. exstop(int stop, register expptr p)
  349. #endif
  350. {
  351.     char *str;
  352.     int n;
  353.  
  354.     if(p)
  355.     {
  356.         if( ! ISCONST(p) )
  357.         {
  358.             execerr("pause/stop argument must be constant", CNULL);
  359.             frexpr(p);
  360.             p = mkstrcon(0, CNULL);
  361.         }
  362.         else if( ISINT(p->constblock.vtype) )
  363.         {
  364.             str = convic(p->constblock.Const.ci);
  365.             n = strlen(str);
  366.             if(n > 0)
  367.             {
  368.                 p->constblock.Const.ccp = copyn(n, str);
  369.                 p->constblock.Const.ccp1.blanks = 0;
  370.                 p->constblock.vtype = TYCHAR;
  371.                 p->constblock.vleng = (expptr) ICON(n);
  372.             }
  373.             else
  374.                 p = (expptr) mkstrcon(0, CNULL);
  375.         }
  376.         else if(p->constblock.vtype != TYCHAR)
  377.         {
  378.             execerr("pause/stop argument must be integer or string", CNULL);
  379.             p = (expptr) mkstrcon(0, CNULL);
  380.         }
  381.     }
  382.     else    p = (expptr) mkstrcon(0, CNULL);
  383.  
  384.     {
  385.     expptr subr_call;
  386.  
  387.     subr_call = call1(TYSUBR, (stop ? "s_stop" : "s_paus"), p);
  388.     putexpr( subr_call );
  389.     }
  390. }
  391.  
  392. /* DO LOOP CODE */
  393.  
  394. #define DOINIT    par[0]
  395. #define DOLIMIT    par[1]
  396. #define DOINCR    par[2]
  397.  
  398.  
  399. /* Macros for   ctlstack -> dostepsign   */
  400.  
  401. #define VARSTEP    0
  402. #define POSSTEP    1
  403. #define NEGSTEP    2
  404.  
  405.  
  406. /* exdo -- generate DO loop code.  In the case of a variable increment,
  407.    positive increment tests are placed above the body, negative increment
  408.    tests are placed below (see   enddo()   ) */
  409.  
  410.  void
  411. #ifdef KR_headers
  412. exdo(range, loopname, spec)
  413.     int range;
  414.     Namep loopname;
  415.     chainp spec;
  416. #else
  417. exdo(int range, Namep loopname, chainp spec)
  418. #endif
  419.             /* range = end label */
  420.             /* input spec must have at least 2 exprs */
  421. {
  422.     register expptr p;
  423.     register Namep np;
  424.     chainp cp;        /* loops over the fields in   spec */
  425.     register int i;
  426.     int dotype;        /* type of the index variable */
  427.     int incsign;        /* sign of the increment, if it's constant
  428.                    */
  429.     Addrp dovarp;        /* loop index variable */
  430.     expptr doinit;        /* constant or register for init param */
  431.     expptr par[3];        /* local specification parameters */
  432.  
  433.     expptr init, test, inc;    /* Expressions in the resulting FOR loop */
  434.  
  435.  
  436.     test = ENULL;
  437.  
  438.     pushctl(CTLDO);
  439.     dorange = ctlstack->dolabel = range;
  440.     ctlstack->loopname = loopname;
  441.  
  442. /* Declare the loop index */
  443.  
  444.     np = (Namep)spec->datap;
  445.     ctlstack->donamep = NULL;
  446.     if (!np) { /* do while */
  447.         ctlstack->dowhile = 1;
  448. #if 0
  449.         if (loopname) {
  450.             if (loopname->vtype == TYUNKNOWN) {
  451.                 loopname->vdcldone = 1;
  452.                 loopname->vclass = CLLABEL;
  453.                 loopname->vprocclass = PLABEL;
  454.                 loopname->vtype = TYLABEL;
  455.                 }
  456.             if (loopname->vtype == TYLABEL)
  457.                 if (loopname->vdovar)
  458.                     dclerr("already in use as a loop name",
  459.                         loopname);
  460.                 else
  461.                     loopname->vdovar = 1;
  462.             else
  463.                 dclerr("already declared; cannot be a loop name",
  464.                     loopname);
  465.             }
  466. #endif
  467.         putwhile((expptr)spec->nextp);
  468.         NOEXT("do while");
  469.         spec->nextp = 0;
  470.         frchain(&spec);
  471.         return;
  472.         }
  473.     if(np->vdovar)
  474.     {
  475.         errstr("nested loops with variable %s", np->fvarname);
  476.         ctlstack->donamep = NULL;
  477.         return;
  478.     }
  479.  
  480. /* Create a memory-resident version of the index variable */
  481.  
  482.     dovarp = mkplace(np);
  483.     if( ! ONEOF(dovarp->vtype, MSKINT|MSKREAL) )
  484.     {
  485.         err("bad type on do variable");
  486.         return;
  487.     }
  488.     ctlstack->donamep = np;
  489.  
  490.     np->vdovar = YES;
  491.  
  492. /* Now   dovarp   points to the index to be used within the loop,   dostgp
  493.    points to the one which may need to be stored */
  494.  
  495.     dotype = dovarp->vtype;
  496.  
  497. /* Count the input specifications and type-check each one independently;
  498.    this just eliminates non-numeric values from the specification */
  499.  
  500.     for(i=0 , cp = spec->nextp ; cp!=NULL && i<3 ; cp = cp->nextp)
  501.     {
  502.         p = par[i++] = fixtype((tagptr)cp->datap);
  503.         if( ! ONEOF(p->headblock.vtype, MSKINT|MSKREAL) )
  504.         {
  505.             err("bad type on DO parameter");
  506.             return;
  507.         }
  508.     }
  509.  
  510.     frchain(&spec);
  511.     switch(i)
  512.     {
  513.     case 0:
  514.     case 1:
  515.         err("too few DO parameters");
  516.         return;
  517.  
  518.     default:
  519.         err("too many DO parameters");
  520.         return;
  521.  
  522.     case 2:
  523.         DOINCR = (expptr) ICON(1);
  524.  
  525.     case 3:
  526.         break;
  527.     }
  528.  
  529.  
  530. /* Now all of the local specification fields are set, but their types are
  531.    not yet consistent */
  532.  
  533. /* Declare the loop initialization value, casting it properly and declaring a
  534.    register if need be */
  535.  
  536.     if (ISCONST (DOINIT) || !onetripflag)
  537. /* putx added 6-29-89 (mwm), not sure if fixtype is required, but I doubt it
  538.    since mkconv is called just before */
  539.         doinit = putx (mkconv (dotype, DOINIT));
  540.     else {
  541.         doinit = (expptr) mktmp(dotype, ENULL);
  542.         puteq (cpexpr (doinit), DOINIT);
  543.     } /* else */
  544.  
  545. /* Declare the loop ending value, casting it to the type of the index
  546.    variable */
  547.  
  548.     if( ISCONST(DOLIMIT) )
  549.         ctlstack->domax = mkconv(dotype, DOLIMIT);
  550.     else {
  551.         ctlstack->domax = (expptr) mktmp0(dotype, ENULL);
  552.         puteq (cpexpr (ctlstack -> domax), DOLIMIT);
  553.     } /* else */
  554.  
  555. /* Declare the loop increment value, casting it to the type of the index
  556.    variable */
  557.  
  558.     if( ISCONST(DOINCR) )
  559.     {
  560.         ctlstack->dostep = mkconv(dotype, DOINCR);
  561.         if( (incsign = conssgn(ctlstack->dostep)) == 0)
  562.             err("zero DO increment");
  563.         ctlstack->dostepsign = (incsign > 0 ? POSSTEP : NEGSTEP);
  564.     }
  565.     else
  566.     {
  567.         ctlstack->dostep = (expptr) mktmp0(dotype, ENULL);
  568.         ctlstack->dostepsign = VARSTEP;
  569.         puteq (cpexpr (ctlstack -> dostep), DOINCR);
  570.     }
  571.  
  572. /* All data is now properly typed and in the   ctlstack,   except for the
  573.    initial value.  Assignments of temps have been generated already */
  574.  
  575.     switch (ctlstack -> dostepsign) {
  576.         case VARSTEP:
  577.         test = mkexpr (OPQUEST, mkexpr (OPLT,
  578.             cpexpr (ctlstack -> dostep), ICON(0)),
  579.             mkexpr (OPCOLON,
  580.                 mkexpr (OPGE, cpexpr((expptr)dovarp),
  581.                     cpexpr (ctlstack -> domax)),
  582.                 mkexpr (OPLE, cpexpr((expptr)dovarp),
  583.                     cpexpr (ctlstack -> domax))));
  584.         break;
  585.         case POSSTEP:
  586.             test = mkexpr (OPLE, cpexpr((expptr)dovarp),
  587.             cpexpr (ctlstack -> domax));
  588.             break;
  589.         case NEGSTEP:
  590.             test = mkexpr (OPGE, cpexpr((expptr)dovarp),
  591.             cpexpr (ctlstack -> domax));
  592.             break;
  593.         default:
  594.             erri ("exdo:  bad dostepsign '%d'", ctlstack -> dostepsign);
  595.             break;
  596.     } /* switch (ctlstack -> dostepsign) */
  597.  
  598.     if (onetripflag)
  599.         test = mkexpr (OPOR, test,
  600.             mkexpr (OPEQ, cpexpr((expptr)dovarp), cpexpr (doinit)));
  601.     init = mkexpr (OPASSIGN, cpexpr((expptr)dovarp), doinit);
  602.     inc = mkexpr (OPPLUSEQ, (expptr)dovarp, cpexpr (ctlstack -> dostep));
  603.  
  604.     if (!onetripflag && ISCONST (ctlstack -> domax) && ISCONST (doinit)
  605.         && ctlstack -> dostepsign != VARSTEP) {
  606.         expptr tester;
  607.  
  608.         tester = mkexpr (OPMINUS, cpexpr (doinit),
  609.             cpexpr (ctlstack -> domax));
  610.         if (incsign == conssgn (tester))
  611.         warn ("DO range never executed");
  612.         frexpr (tester);
  613.     } /* if !onetripflag && */
  614.  
  615.     p1_for (init, test, inc);
  616. }
  617.  
  618.  void
  619. #ifdef KR_headers
  620. exenddo(np)
  621.     Namep np;
  622. #else
  623. exenddo(Namep np)
  624. #endif
  625. {
  626.     Namep np1;
  627.     int here;
  628.     struct Ctlframe *cf;
  629.  
  630.     if( ctlstack < ctls )
  631.         goto misplaced;
  632.     here = ctlstack->dolabel;
  633.     if (ctlstack->ctltype != CTLDO
  634.     || here >= 0 && (!thislabel || thislabel->labelno != here)) {
  635.  misplaced:
  636.         err("misplaced ENDDO");
  637.         return;
  638.         }
  639.     if (np != ctlstack->loopname) {
  640.         if (np1 = ctlstack->loopname)
  641.             errstr("expected \"enddo %s\"", np1->fvarname);
  642.         else
  643.             err("expected unnamed ENDDO");
  644.         for(cf = ctls; cf < ctlstack; cf++)
  645.             if (cf->ctltype == CTLDO && cf->loopname == np) {
  646.                 here = cf->dolabel;
  647.                 break;
  648.                 }
  649.         }
  650.     enddo(here);
  651.     }
  652.  
  653.  void
  654. #ifdef KR_headers
  655. enddo(here)
  656.     int here;
  657. #else
  658. enddo(int here)
  659. #endif
  660. {
  661.     register struct Ctlframe *q;
  662.     Namep np;            /* name of the current DO index */
  663.     Addrp ap;
  664.     register int i;
  665.     register expptr e;
  666.  
  667. /* Many DO's can end at the same statement, so keep looping over all
  668.    nested indicies */
  669.  
  670.     while(here == dorange)
  671.     {
  672.         if(np = ctlstack->donamep)
  673.             {
  674.             p1for_end ();
  675.  
  676. /* Now we're done with all of the tests, and the loop has terminated.
  677.    Store the index value back in long-term memory */
  678.  
  679.             if(ap = memversion(np))
  680.                 puteq((expptr)ap, (expptr)mkplace(np));
  681.             for(i = 0 ; i < 4 ; ++i)
  682.                 ctlstack->ctlabels[i] = 0;
  683.             deregister(ctlstack->donamep);
  684.             ctlstack->donamep->vdovar = NO;
  685.             e = ctlstack->dostep;
  686.             if (e->tag == TADDR && e->addrblock.istemp)
  687.                 frtemp((Addrp)e);
  688.             else
  689.                 frexpr(e);
  690.             e = ctlstack->domax;
  691.             if (e->tag == TADDR && e->addrblock.istemp)
  692.                 frtemp((Addrp)e);
  693.             else
  694.                 frexpr(e);
  695.             }
  696.         else if (ctlstack->dowhile)
  697.             p1for_end ();
  698.  
  699. /* Set   dorange   to the closing label of the next most enclosing DO loop
  700.    */
  701.  
  702.         popctl();
  703.         poplab();
  704.         dorange = 0;
  705.         for(q = ctlstack ; q>=ctls ; --q)
  706.             if(q->ctltype == CTLDO)
  707.             {
  708.                 dorange = q->dolabel;
  709.                 break;
  710.             }
  711.     }
  712. }
  713.  
  714.  void
  715. #ifdef KR_headers
  716. exassign(vname, labelval)
  717.     register Namep vname;
  718.     struct Labelblock *labelval;
  719. #else
  720. exassign(register Namep vname, struct Labelblock *labelval)
  721. #endif
  722. {
  723.     Addrp p;
  724.     register Addrp q;
  725.     char *fs;
  726.     register chainp cp, cpprev;
  727.     register ftnint k, stno;
  728.  
  729.     p = mkplace(vname);
  730.     if( ! ONEOF(p->vtype, MSKINT|MSKADDR) ) {
  731.         err("noninteger assign variable");
  732.         return;
  733.         }
  734.  
  735.     /* If the label hasn't been defined, then we do things twice:
  736.      * once for an executable stmt label, once for a format
  737.      */
  738.  
  739.     /* code for executable label... */
  740.  
  741. /* Now store the assigned value in a list associated with this variable.
  742.    This will be used later to generate a switch() statement in the C output */
  743.  
  744.     fs = labelval->fmtstring;
  745.     if (!labelval->labdefined || !fs) {
  746.  
  747.         if (vname -> vis_assigned == 0) {
  748.             vname -> varxptr.assigned_values = CHNULL;
  749.             vname -> vis_assigned = 1;
  750.             }
  751.  
  752.         /* don't duplicate labels... */
  753.  
  754.         stno = labelval->stateno;
  755.         cpprev = 0;
  756.         for(k = 0, cp = vname->varxptr.assigned_values;
  757.                 cp; cpprev = cp, cp = cp->nextp, k++)
  758.             if ((ftnint)cp->datap == stno)
  759.                 break;
  760.         if (!cp) {
  761.             cp = mkchain((char *)stno, CHNULL);
  762.             if (cpprev)
  763.                 cpprev->nextp = cp;
  764.             else
  765.                 vname->varxptr.assigned_values = cp;
  766.             labelval->labused = 1;
  767.             }
  768.         putout(mkexpr(OPASSIGN, (expptr)p, mkintcon(k)));
  769.         }
  770.  
  771.     /* Code for FORMAT label... */
  772.  
  773.     if (!labelval->labdefined || fs) {
  774.  
  775.         labelval->fmtlabused = 1;
  776.         p = ALLOC(Addrblock);
  777.         p->tag = TADDR;
  778.         p->vtype = TYCHAR;
  779.         p->vstg = STGAUTO;
  780.         p->memoffset = ICON(0);
  781.         fmtname(vname, p);
  782.         q = ALLOC(Addrblock);
  783.         q->tag = TADDR;
  784.         q->vtype = TYCHAR;
  785.         q->vstg = STGAUTO;
  786.         q->ntempelt = 1;
  787.         q->memoffset = ICON(0);
  788.         q->uname_tag = UNAM_IDENT;
  789.         sprintf(q->user.ident, "fmt_%ld", labelval->stateno);
  790.         putout(mkexpr(OPASSIGN, (expptr)p, (expptr)q));
  791.         }
  792.  
  793. } /* exassign */
  794.  
  795.  
  796.  void
  797. #ifdef KR_headers
  798. exarif(expr, neglab, zerlab, poslab)
  799.     expptr expr;
  800.     struct Labelblock *neglab;
  801.     struct Labelblock *zerlab;
  802.     struct Labelblock *poslab;
  803. #else
  804. exarif(expptr expr, struct Labelblock *neglab, struct Labelblock *zerlab, struct Labelblock *poslab)
  805. #endif
  806. {
  807.     register int lm, lz, lp;
  808.  
  809.     lm = neglab->stateno;
  810.     lz = zerlab->stateno;
  811.     lp = poslab->stateno;
  812.     expr = fixtype(expr);
  813.  
  814.     if( ! ONEOF(expr->headblock.vtype, MSKINT|MSKREAL) )
  815.     {
  816.         err("invalid type of arithmetic if expression");
  817.         frexpr(expr);
  818.     }
  819.     else
  820.     {
  821.         if (lm == lz && lz == lp)
  822.             exgoto (neglab);
  823.         else if(lm == lz)
  824.             exar2(OPLE, expr, neglab, poslab);
  825.         else if(lm == lp)
  826.             exar2(OPNE, expr, neglab, zerlab);
  827.         else if(lz == lp)
  828.             exar2(OPGE, expr, zerlab, neglab);
  829.         else {
  830.             expptr t;
  831.  
  832.         if (!addressable (expr)) {
  833.         t = (expptr) mktmp(expr -> headblock.vtype, ENULL);
  834.         expr = mkexpr (OPASSIGN, cpexpr (t), expr);
  835.         } else
  836.         t = (expptr) cpexpr (expr);
  837.  
  838.         p1_if(putx(fixtype(mkexpr (OPLT, expr, ICON (0)))));
  839.         exgoto(neglab);
  840.         p1_elif (mkexpr (OPEQ, t, ICON (0)));
  841.         exgoto(zerlab);
  842.         p1_else ();
  843.         exgoto(poslab);
  844.         p1else_end ();
  845.         } /* else */
  846.     }
  847. }
  848.  
  849.  
  850.  
  851. /* exar2 -- Do arithmetic IF for only 2 distinct labels;   if !(e.op.0)
  852.    goto l2 else goto l1.  If this seems backwards, that's because it is,
  853.    in order to make the 1 pass algorithm work. */
  854.  
  855.  LOCAL void
  856. #ifdef KR_headers
  857. exar2(op, e, l1, l2)
  858.     int op;
  859.     expptr e;
  860.     struct Labelblock *l1;
  861.     struct Labelblock *l2;
  862. #else
  863. exar2(int op, expptr e, struct Labelblock *l1, struct Labelblock *l2)
  864. #endif
  865. {
  866.     expptr comp;
  867.  
  868.     comp = mkexpr (op, e, ICON (0));
  869.     p1_if(putx(fixtype(comp)));
  870.     exgoto(l1);
  871.     p1_else ();
  872.     exgoto(l2);
  873.     p1else_end ();
  874. }
  875.  
  876.  
  877. /* exreturn -- return the value in   p  from a SUBROUTINE call -- used to
  878.    implement the alternate return mechanism */
  879.  
  880.  void
  881. #ifdef KR_headers
  882. exreturn(p)
  883.     register expptr p;
  884. #else
  885. exreturn(register expptr p)
  886. #endif
  887. {
  888.     if(procclass != CLPROC)
  889.         warn("RETURN statement in main or block data");
  890.     if(p && (proctype!=TYSUBR || procclass!=CLPROC) )
  891.     {
  892.         err("alternate return in nonsubroutine");
  893.         p = 0;
  894.     }
  895.  
  896.     if (p || proctype == TYSUBR) {
  897.         if (p == ENULL) p = ICON (0);
  898.         p = mkconv (TYLONG, fixtype (p));
  899.         p1_subr_ret (p);
  900.     } /* if p || proctype == TYSUBR */
  901.     else
  902.         p1_subr_ret((expptr)retslot);
  903. }
  904.  
  905.  
  906.  void
  907. #ifdef KR_headers
  908. exasgoto(labvar)
  909.     Namep labvar;
  910. #else
  911. exasgoto(Namep labvar)
  912. #endif
  913. {
  914.     register Addrp p;
  915.  
  916.     p = mkplace(labvar);
  917.     if( ! ISINT(p->vtype) )
  918.         err("assigned goto variable must be integer");
  919.     else {
  920.         p1_asgoto (p);
  921.     } /* else */
  922. }
  923.