home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / icon / dos / src / icont / tcode.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  25KB  |  1,072 lines

  1. /*
  2.  * tcode.c -- translator functions for traversing parse trees and generating
  3.  *  code.
  4.  */
  5.  
  6. #include "../h/gsupport.h"
  7. #include "tproto.h"
  8. #include "globals.h"
  9. #include "trans.h"
  10. #include "tree.h"
  11. #include "token.h"
  12. #include "tsym.h"
  13.  
  14. /*
  15.  * Prototypes.
  16.  */
  17.  
  18. hidden int    alclab        Params((int n));
  19. hidden novalue    binop        Params((int op));
  20. hidden novalue    emit        Params((char *s));
  21. hidden novalue    emitl        Params((char *s,int a));
  22. hidden novalue    emitlab        Params((int l));
  23. hidden novalue    emitn        Params((char *s,int a));
  24. hidden novalue    emits        Params((char *s,char *a));
  25. hidden novalue    setloc        Params((nodeptr n));
  26. hidden int    traverse    Params((nodeptr t));
  27. hidden novalue    unopa        Params((int op, nodeptr t));
  28. hidden novalue    unopb        Params((int op));
  29.  
  30. extern int tfatals;
  31. extern int nocode;
  32. extern char *comfile;
  33.  
  34. /*
  35.  * Code generator parameters.
  36.  */
  37.  
  38. #define LoopDepth   20        /* max. depth of nested loops */
  39. #define CaseDepth   10        /* max. depth of nested case statements */
  40. #define CreatDepth  10        /* max. depth of nested create statements */
  41.  
  42. /*
  43.  * loopstk structures hold information about nested loops.
  44.  */
  45. struct loopstk {
  46.    int nextlab;            /* label for next exit */
  47.    int breaklab;        /* label for break exit */
  48.    int markcount;        /* number of marks */
  49.    int ltype;            /* loop type */
  50.    };
  51.  
  52. /*
  53.  * casestk structure hold information about case statements.
  54.  */
  55. struct casestk {
  56.    int endlab;            /* label for exit from case statement */
  57.    nodeptr deftree;        /* pointer to tree for default clause */
  58.    };
  59.  
  60. /*
  61.  * creatstk structures hold information about create statements.
  62.  */
  63. struct creatstk {
  64.    int nextlab;            /* previous value of nextlab */
  65.    int breaklab;        /* previous value of breaklab */
  66.    };
  67. static int nextlab;        /* next label allocated by alclab() */
  68.  
  69. /*
  70.  * codegen - traverse tree t, generating code.
  71.  */
  72.  
  73. novalue codegen(t)
  74. nodeptr t;
  75.    {
  76.    nextlab = 1;
  77.    traverse(t);
  78.    }
  79.  
  80. /*
  81.  * traverse - traverse tree rooted at t and generate code.  This is just
  82.  *  plug and chug code for each of the node types.
  83.  */
  84.  
  85. static int traverse(t)
  86. register nodeptr t;
  87.    {
  88.    register int lab, n, i;
  89.    struct loopstk loopsave;
  90.    static struct loopstk loopstk[LoopDepth];    /* loop stack */
  91.    static struct loopstk *loopsp;
  92.    static struct casestk casestk[CaseDepth];    /* case stack */
  93.    static struct casestk *casesp;
  94.    static struct creatstk creatstk[CreatDepth]; /* create stack */
  95.    static struct creatstk *creatsp;
  96.  
  97.    n = 1;
  98.    switch (TType(t)) {
  99.  
  100.       case N_Activat:            /* co-expression activation */
  101.      if (Val0(Tree0(t)) == AUGACT) {
  102.         emit("pnull");
  103.         }
  104.      traverse(Tree2(t));        /* evaluate result expression */
  105.      if (Val0(Tree0(t)) == AUGACT)
  106.         emit("sdup");
  107.      traverse(Tree1(t));        /* evaluate activate expression */
  108.      setloc(t);
  109.      emit("coact");
  110.      if (Val0(Tree0(t)) == AUGACT)
  111.         emit("asgn");
  112.      break;
  113.  
  114.       case N_Alt:            /* alternation */
  115.      lab = alclab(2);
  116.      emitl("mark", lab);
  117.      loopsp->markcount++;
  118.      traverse(Tree0(t));        /* evaluate first alternative */
  119.      loopsp->markcount--;
  120.  
  121. #ifdef EventMon
  122.          setloc(t);
  123. #endif                    /* EventMon */
  124.  
  125.      emit("esusp");                 /*  and suspend with its result */
  126.      emitl("goto", lab+1);
  127.      emitlab(lab);
  128.      traverse(Tree1(t));        /* evaluate second alternative */
  129.      emitlab(lab+1);
  130.      break;
  131.  
  132.       case N_Augop:            /* augmented assignment */
  133.       case N_Binop:            /*  or a binary operator */
  134.      emit("pnull");
  135.      traverse(Tree1(t));
  136.      if (TType(t) == N_Augop)
  137.         emit("dup");
  138.      traverse(Tree2(t));
  139.      setloc(t);
  140.      binop((int)Val0(Tree0(t)));
  141.      break;
  142.  
  143.       case N_Bar:            /* repeated alternation */
  144.      lab = alclab(1);
  145.      emitlab(lab);
  146.      emit("mark0");         /* fail if expr fails first time */
  147.      loopsp->markcount++;
  148.      traverse(Tree0(t));        /* evaluate first alternative */
  149.      loopsp->markcount--;
  150.      emitl("chfail", lab);          /* change to loop on failure */
  151.      emit("esusp");                 /* suspend result */
  152.      break;
  153.  
  154.       case N_Break:            /* break expression */
  155.      if (loopsp->breaklab <= 0)
  156.         nfatal(t, "invalid context for break");
  157.      else {
  158.         for (i = 0; i < loopsp->markcount; i++)
  159.            emit("unmark");
  160.         loopsave = *loopsp--;
  161.         traverse(Tree0(t));
  162.         *++loopsp = loopsave;
  163.         emitl("goto", loopsp->breaklab);
  164.         }
  165.      break;
  166.  
  167.       case N_Case:            /* case expression */
  168.      lab = alclab(1);
  169.      casesp++;
  170.      casesp->endlab = lab;
  171.      casesp->deftree = NULL;
  172.      emit("mark0");
  173.      loopsp->markcount++;
  174.      traverse(Tree0(t));        /* evaluate control expression */
  175.      loopsp->markcount--;
  176.      emit("eret");
  177.      traverse(Tree1(t));        /* do rest of case (CLIST) */
  178.      if (casesp->deftree != NULL) { /* evaluate default clause */
  179.         emit("pop");
  180.         traverse(casesp->deftree);
  181.         }
  182.      else
  183.         emit("efail");
  184.      emitlab(lab);            /* end label */
  185.      casesp--;
  186.      break;
  187.  
  188.       case N_Ccls:            /* case expression clause */
  189.      if (TType(Tree0(t)) == N_Res && /* default clause */
  190.          Val0(Tree0(t)) == DEFAULT) {
  191.         if (casesp->deftree != NULL)
  192.            nfatal(t, "more than one default clause");
  193.         else
  194.            casesp->deftree = Tree1(t);
  195.         }
  196.      else {                /* case clause */
  197.         lab = alclab(1);
  198.         emitl("mark", lab);
  199.         loopsp->markcount++;
  200.         emit("ccase");
  201.         traverse(Tree0(t));        /* evaluate selector */
  202.         setloc(t);
  203.         emit("eqv");
  204.         loopsp->markcount--;
  205.         emit("unmark");
  206.         emit("pop");
  207.         traverse(Tree1(t));        /* evaluate expression */
  208.         emitl("goto", casesp->endlab); /* goto end label */
  209.         emitlab(lab);        /* label for next clause */
  210.         }
  211.      break;
  212.  
  213.       case N_Clist:            /* list of case clauses */
  214.      traverse(Tree0(t));
  215.      traverse(Tree1(t));
  216.      break;
  217.  
  218.       case N_Conj:            /* conjunction */
  219.      if (Val0(Tree0(t)) == AUGAND) {
  220.         emit("pnull");
  221.         }
  222.      traverse(Tree1(t));
  223.      if (Val0(Tree0(t)) != AUGAND)
  224.         emit("pop");
  225.      traverse(Tree2(t));
  226.      if (Val0(Tree0(t)) == AUGAND) {
  227.         setloc(t);
  228.         emit("asgn");
  229.         }
  230.      break;
  231.  
  232.       case N_Create:            /* create expression */
  233.      creatsp++;
  234.      creatsp->nextlab = loopsp->nextlab;
  235.      creatsp->breaklab = loopsp->breaklab;
  236.      loopsp->nextlab = 0;        /* make break and next illegal */
  237.      loopsp->breaklab = 0;
  238.      lab = alclab(3);
  239.      emitl("goto", lab+2);          /* skip over code for co-expression */
  240.      emitlab(lab);            /* entry point */
  241.      emit("pop");                   /* pop the result from activation */
  242.      emitl("mark", lab+1);
  243.      loopsp->markcount++;
  244.      traverse(Tree0(t));        /* traverse code for co-expression */
  245.      loopsp->markcount--;
  246.      setloc(t);
  247.      emit("coret");                 /* return to activator */
  248.      emit("efail");                 /* drive co-expression */
  249.      emitlab(lab+1);        /* loop on exhaustion */
  250.      emit("cofail");                /* and fail each time */
  251.      emitl("goto", lab+1);
  252.      emitlab(lab+2);
  253.      emitl("create", lab);          /* create entry block */
  254.      loopsp->nextlab = creatsp->nextlab;   /* legalize break and next */
  255.      loopsp->breaklab = creatsp->breaklab;
  256.      creatsp--;
  257.      break;
  258.  
  259.       case N_Cset:            /* cset literal */
  260.      emitn("cset", (int)Val0(t));
  261.      break;
  262.  
  263.       case N_Elist:            /* expression list */
  264.      n = traverse(Tree0(t));
  265.      n += traverse(Tree1(t));
  266.      break;
  267.  
  268.       case N_Empty:            /* a missing expression */
  269.      emit("pnull");
  270.      break;
  271.  
  272.       case N_Field:            /* field reference */
  273.      emit("pnull");
  274.      traverse(Tree0(t));
  275.      setloc(t);
  276.      emits("field", Str0(Tree1(t)));
  277.      break;
  278.  
  279.  
  280.       case N_Id:            /* identifier */
  281.      emitn("var", (int)Val0(t));
  282.      break;
  283.  
  284.       case N_If:            /* if expression */
  285.      if (TType(Tree2(t)) == N_Empty) {
  286.         lab = 0;
  287.         emit("mark0");
  288.         }
  289.      else {
  290.         lab = alclab(2);
  291.         emitl("mark", lab);
  292.         }
  293.      loopsp->markcount++;
  294.      traverse(Tree0(t));
  295.      loopsp->markcount--;
  296.      emit("unmark");
  297.      traverse(Tree1(t));
  298.      if (lab > 0) {
  299.         emitl("goto", lab+1);
  300.         emitlab(lab);
  301.         traverse(Tree2(t));
  302.         emitlab(lab+1);
  303.         }
  304.      break;
  305.  
  306.       case N_Int:            /* integer literal */
  307.      emitn("int", (int)Val0(t));
  308.      break;
  309.  
  310.  
  311.       case N_Apply:            /* application */
  312.          traverse(Tree0(t));
  313.          traverse(Tree1(t));
  314.          emitn("invoke", -1);
  315.          break;
  316.  
  317.       case N_Invok:            /* invocation */
  318.      if (TType(Tree0(t)) != N_Empty) {
  319.         traverse(Tree0(t));
  320.          }
  321.      else {
  322.         emit("pushn1");             /* default to -1(e1,...,en) */
  323.         }
  324.      if (TType(Tree1(t)) == N_Empty)
  325.             n = 0;
  326.          else
  327.         n = traverse(Tree1(t));
  328.      setloc(t);
  329.      emitn("invoke", n);
  330.      n = 1;
  331.      break;
  332.  
  333.       case N_Key:            /* keyword reference */
  334.      setloc(t);
  335.      emitn("keywd", (int)Val0(t));
  336.      break;
  337.  
  338.       case N_Limit:            /* limitation */
  339.      traverse(Tree1(t));
  340.      setloc(t);
  341.      emit("limit");
  342.      loopsp->markcount++;
  343.      traverse(Tree0(t));
  344.      loopsp->markcount--;
  345.      emit("lsusp");
  346.      break;
  347.  
  348.       case N_List:            /* list construction */
  349.      emit("pnull");
  350.      if (TType(Tree0(t)) == N_Empty)
  351.         n = 0;
  352.      else
  353.         n = traverse(Tree0(t));
  354.      setloc(t);
  355.      emitn("llist", n);
  356.      n = 1;
  357.      break;
  358.  
  359.       case N_Loop:            /* loop */
  360.      switch ((int)Val0(Tree0(t))) {
  361.         case EVERY:
  362.            lab = alclab(2);
  363.            loopsp++;
  364.            loopsp->ltype = EVERY;
  365.            loopsp->nextlab = lab;
  366.            loopsp->breaklab = lab + 1;
  367.            loopsp->markcount = 1;
  368.            emit("mark0");
  369.            traverse(Tree1(t));
  370.            emit("pop");
  371.            if (TType(Tree2(t)) != N_Empty) {   /* every e1 do e2 */
  372.           emit("mark0");
  373.           loopsp->ltype = N_Loop;
  374.           loopsp->markcount++;
  375.           traverse(Tree2(t));
  376.           loopsp->markcount--;
  377.           emit("unmark");
  378.           }
  379.            emitlab(loopsp->nextlab);
  380.            emit("efail");
  381.            emitlab(loopsp->breaklab);
  382.            loopsp--;
  383.            break;
  384.  
  385.         case REPEAT:
  386.            lab = alclab(3);
  387.            loopsp++;
  388.            loopsp->ltype = N_Loop;
  389.            loopsp->nextlab = lab + 1;
  390.            loopsp->breaklab = lab + 2;
  391.            loopsp->markcount = 1;
  392.            emitlab(lab);
  393.            emitl("mark", lab);
  394.            traverse(Tree1(t));
  395.            emitlab(loopsp->nextlab);
  396.            emit("unmark");
  397.            emitl("goto", lab);
  398.            emitlab(loopsp->breaklab);
  399.            loopsp--;
  400.            break;
  401.  
  402.         case SUSPEND:            /* suspension expression */
  403.            if (creatsp > creatstk)
  404.           nfatal(t, "invalid context for suspend");
  405.            lab = alclab(2);
  406.            loopsp++;
  407.            loopsp->ltype = EVERY;        /* like every ... do for next */
  408.            loopsp->nextlab = lab;
  409.            loopsp->breaklab = lab + 1;
  410.            loopsp->markcount = 1;
  411.            emit("mark0");
  412.            traverse(Tree1(t));
  413.            setloc(t);
  414.            emit("psusp");
  415.            emit("pop");
  416.            if (TType(Tree2(t)) != N_Empty) { /* suspend e1 do e2 */
  417.           emit("mark0");
  418.           loopsp->ltype = N_Loop;
  419.           loopsp->markcount++;
  420.           traverse(Tree2(t));
  421.           loopsp->markcount--;
  422.           emit("unmark");
  423.           }
  424.            emitlab(loopsp->nextlab);
  425.            emit("efail");
  426.            emitlab(loopsp->breaklab);
  427.            loopsp--;
  428.            break;
  429.  
  430.         case WHILE:
  431.            lab = alclab(3);
  432.            loopsp++;
  433.            loopsp->ltype = N_Loop;
  434.            loopsp->nextlab = lab + 1;
  435.            loopsp->breaklab = lab + 2;
  436.            loopsp->markcount = 1;
  437.            emitlab(lab);
  438.            emit("mark0");
  439.            traverse(Tree1(t));
  440.            if (TType(Tree2(t)) != N_Empty) {
  441.           emit("unmark");
  442.           emitl("mark", lab);
  443.           traverse(Tree2(t));
  444.           }
  445.            emitlab(loopsp->nextlab);
  446.            emit("unmark");
  447.            emitl("goto", lab);
  448.            emitlab(loopsp->breaklab);
  449.            loopsp--;
  450.            break;
  451.  
  452.         case UNTIL:
  453.            lab = alclab(4);
  454.            loopsp++;
  455.            loopsp->ltype = N_Loop;
  456.            loopsp->nextlab = lab + 2;
  457.            loopsp->breaklab = lab + 3;
  458.            loopsp->markcount = 1;
  459.            emitlab(lab);
  460.            emitl("mark", lab+1);
  461.            traverse(Tree1(t));
  462.            emit("unmark");
  463.            emit("efail");
  464.            emitlab(lab+1);
  465.            emitl("mark", lab);
  466.            traverse(Tree2(t));
  467.            emitlab(loopsp->nextlab);
  468.            emit("unmark");
  469.            emitl("goto", lab);
  470.            emitlab(loopsp->breaklab);
  471.            loopsp--;
  472.            break;
  473.         }
  474.      break;
  475.  
  476.       case N_Next:            /* next expression */
  477.      if (loopsp < loopstk || loopsp->nextlab <= 0)
  478.         nfatal(t, "invalid context for next");
  479.      else {
  480.         if (loopsp->ltype != EVERY && loopsp->markcount > 1)
  481.            for (i = 0; i < loopsp->markcount - 1; i++)
  482.           emit("unmark");
  483.         emitl("goto", loopsp->nextlab);
  484.         }
  485.      break;
  486.  
  487.       case N_Not:            /* not expression */
  488.      lab = alclab(1);
  489.      emitl("mark", lab);
  490.      loopsp->markcount++;
  491.      traverse(Tree0(t));
  492.      loopsp->markcount--;
  493.      emit("unmark");
  494.      emit("efail");
  495.      emitlab(lab);
  496.      emit("pnull");
  497.      break;
  498.  
  499.       case N_Proc:            /* procedure */
  500.      loopsp = loopstk;
  501.      loopsp->nextlab = 0;
  502.      loopsp->breaklab = 0;
  503.      loopsp->markcount = 0;
  504.      casesp = casestk;
  505.      creatsp = creatstk;
  506.  
  507.  
  508.      writecheck(fprintf(codefile, "proc %s\n", Str0(Tree0(t))));
  509.      lout(codefile);
  510.      cout(codefile);
  511.  
  512.      emit("declend");
  513.      setloc(t);
  514.      if (TType(Tree1(t)) != N_Empty) {
  515.         lab = alclab(1);
  516.         emitl("init", lab);
  517.         emitl("mark", lab);
  518.         traverse(Tree1(t));
  519.         emit("unmark");
  520.         emitlab(lab);
  521.         }
  522.      if (TType(Tree2(t)) != N_Empty)
  523.         traverse(Tree2(t));
  524.      setloc(Tree3(t));
  525.      emit("pfail");
  526.      emit("end");
  527.      if (!silent)
  528.         fprintf(stderr, "  %s (%lu/%ld)\n", Str0(Tree0(t)),
  529.                (unsigned long)DiffPtrs((char *)tfree,(char *)tree)/sizeof(word),
  530.                   (long)tsize);
  531.      break;
  532.  
  533.       case N_Real:            /* real literal */
  534.      emitn("real", (int)Val0(t));
  535.      break;
  536.  
  537.       case N_Ret:            /* return expression */
  538.      if (creatsp > creatstk)
  539.         nfatal(t, "invalid context for return or fail");
  540.      if (Val0(Tree0(t)) != FAIL) {
  541.         lab = alclab(1);
  542.         emitl("mark", lab);
  543.         loopsp->markcount++;
  544.         traverse(Tree1(t));
  545.         loopsp->markcount--;
  546.         setloc(t);
  547.         emit("pret");
  548.         emitlab(lab);
  549.         }
  550.      setloc(t);
  551.      emit("pfail");
  552.      break;
  553.  
  554.       case N_Scan:            /* scanning expression */
  555.      if (Val0(Tree0(t)) == SCANASGN)
  556.         emit("pnull");
  557.      traverse(Tree1(t));
  558.      if (Val0(Tree0(t)) == SCANASGN)
  559.         emit("sdup");
  560.      setloc(t);
  561.      emit("bscan");
  562.      traverse(Tree2(t));
  563.      setloc(t);
  564.      emit("escan");
  565.      if (Val0(Tree0(t)) == SCANASGN)
  566.         emit("asgn");
  567.      break;
  568.  
  569.       case N_Sect:            /* section operation */
  570.      emit("pnull");
  571.      traverse(Tree1(t));
  572.      traverse(Tree2(t));
  573.      if (Val0(Tree0(t)) == PCOLON || Val0(Tree0(t)) == MCOLON)
  574.         emit("dup");
  575.      traverse(Tree3(t));
  576.      setloc(Tree0(t));
  577.      if (Val0(Tree0(t)) == PCOLON)
  578.         emit("plus");
  579.      else if (Val0(Tree0(t)) == MCOLON)
  580.         emit("minus");
  581.      setloc(t);
  582.      emit("sect");
  583.      break;
  584.  
  585.       case N_Slist:            /* semicolon-separated expr list */
  586.      lab = alclab(1);
  587.      emitl("mark", lab);
  588.      loopsp->markcount++;
  589.      traverse(Tree0(t));
  590.      loopsp->markcount--;
  591.      emit("unmark");
  592.      emitlab(lab);
  593.      traverse(Tree1(t));
  594.      break;
  595.  
  596.       case N_Str:            /* string literal */
  597.      emitn("str", (int)Val0(t));
  598.      break;
  599.  
  600.       case N_To:            /* to expression */
  601.      emit("pnull");
  602.      traverse(Tree0(t));
  603.      traverse(Tree1(t));
  604.      emit("push1");
  605.      setloc(t);
  606.      emit("toby");
  607.      break;
  608.  
  609.       case N_ToBy:            /* to-by expression */
  610.      emit("pnull");
  611.      traverse(Tree0(t));
  612.      traverse(Tree1(t));
  613.      traverse(Tree2(t));
  614.      setloc(t);
  615.      emit("toby");
  616.      break;
  617.  
  618.       case N_Unop:            /* unary operator */
  619.      unopa((int)Val0(Tree0(t)),t);
  620.      traverse(Tree1(t));
  621.      setloc(t);
  622.      unopb((int)Val0(Tree0(t)));
  623.      break;
  624.  
  625.       default:
  626.      emitn("?????", TType(t));
  627.      tsyserr("traverse: undefined node type");
  628.       }
  629.    return n;
  630.    }
  631.  
  632. /*
  633.  * binop emits code for binary operators.  For non-augmented operators,
  634.  *  the name of operator is emitted.  For augmented operators, an "asgn"
  635.  *  is emitted after the name of the operator.
  636.  */
  637. static novalue binop(op)
  638. int op;
  639.    {
  640.    register int asgn;
  641.    register char *name;
  642.  
  643.    asgn = 0;
  644.    switch (op) {
  645.  
  646.       case ASSIGN:
  647.      name = "asgn";
  648.      break;
  649.  
  650.       case CARETASGN:
  651.      asgn++;
  652.       case CARET:
  653.      name = "power";
  654.      break;
  655.  
  656.       case CONCATASGN:
  657.      asgn++;
  658.       case CONCAT:
  659.      name = "cat";
  660.      break;
  661.  
  662.       case DIFFASGN:
  663.      asgn++;
  664.       case DIFF:
  665.      name = "diff";
  666.      break;
  667.  
  668.       case AUGEQV:
  669.      asgn++;
  670.       case EQUIV:
  671.      name = "eqv";
  672.      break;
  673.  
  674.       case INTERASGN:
  675.      asgn++;
  676.       case INTER:
  677.      name = "inter";
  678.      break;
  679.  
  680.       case LBRACK:
  681.      name = "subsc";
  682.      break;
  683.  
  684.       case LCONCATASGN:
  685.      asgn++;
  686.       case LCONCAT:
  687.      name = "lconcat";
  688.      break;
  689.  
  690.       case AUGSEQ:
  691.      asgn++;
  692.       case LEXEQ:
  693.      name = "lexeq";
  694.      break;
  695.  
  696.       case AUGSGE:
  697.      asgn++;
  698.       case LEXGE:
  699.      name = "lexge";
  700.      break;
  701.  
  702.       case AUGSGT:
  703.      asgn++;
  704.       case LEXGT:
  705.      name = "lexgt";
  706.      break;
  707.  
  708.       case AUGSLE:
  709.      asgn++;
  710.       case LEXLE:
  711.      name = "lexle";
  712.      break;
  713.  
  714.       case AUGSLT:
  715.      asgn++;
  716.       case LEXLT:
  717.      name = "lexlt";
  718.      break;
  719.  
  720.       case AUGSNE:
  721.      asgn++;
  722.       case LEXNE:
  723.      name = "lexne";
  724.      break;
  725.  
  726.       case MINUSASGN:
  727.      asgn++;
  728.       case MINUS:
  729.      name = "minus";
  730.      break;
  731.  
  732.       case MODASGN:
  733.      asgn++;
  734.       case MOD:
  735.      name = "mod";
  736.      break;
  737.  
  738.       case AUGNEQV:
  739.      asgn++;
  740.       case NOTEQUIV:
  741.      name = "neqv";
  742.      break;
  743.  
  744.       case AUGEQ:
  745.      asgn++;
  746.       case NUMEQ:
  747.      name = "numeq";
  748.      break;
  749.  
  750.       case AUGGE:
  751.      asgn++;
  752.       case NUMGE:
  753.      name = "numge";
  754.      break;
  755.  
  756.       case AUGGT:
  757.      asgn++;
  758.       case NUMGT:
  759.      name = "numgt";
  760.      break;
  761.  
  762.       case AUGLE:
  763.      asgn++;
  764.       case NUMLE:
  765.      name = "numle";
  766.      break;
  767.  
  768.       case AUGLT:
  769.      asgn++;
  770.       case NUMLT:
  771.      name = "numlt";
  772.      break;
  773.  
  774.       case AUGNE:
  775.      asgn++;
  776.       case NUMNE:
  777.      name = "numne";
  778.      break;
  779.  
  780.       case PLUSASGN:
  781.      asgn++;
  782.       case PLUS:
  783.      name = "plus";
  784.      break;
  785.  
  786.       case REVASSIGN:
  787.      name = "rasgn";
  788.      break;
  789.  
  790.       case REVSWAP:
  791.      name = "rswap";
  792.      break;
  793.  
  794.       case SLASHASGN:
  795.      asgn++;
  796.       case SLASH:
  797.      name = "div";
  798.      break;
  799.  
  800.       case STARASGN:
  801.      asgn++;
  802.       case STAR:
  803.      name = "mult";
  804.      break;
  805.  
  806.       case SWAP:
  807.      name = "swap";
  808.      break;
  809.  
  810.       case UNIONASGN:
  811.      asgn++;
  812.       case UNION:
  813.      name = "unions";
  814.      break;
  815.  
  816.       default:
  817.      emitn("?binop", op);
  818.      tsyserr("binop: undefined binary operator");
  819.       }
  820.    emit(name);
  821.    if (asgn)
  822.       emit("asgn");
  823.  
  824.    }
  825. /*
  826.  * unopa and unopb handle code emission for unary operators. unary operator
  827.  *  sequences that are the same as binary operator sequences are recognized
  828.  *  by the lexical analyzer as binary operators.  For example, ~===x means to
  829.  *  do three tab(match(...)) operations and then a cset complement, but the
  830.  *  lexical analyzer sees the operator sequence as the "neqv" binary
  831.  *  operation.    unopa and unopb unravel tokens of this form.
  832.  *
  833.  * When a N_Unop node is encountered, unopa is called to emit the necessary
  834.  *  number of "pnull" operations to receive the intermediate results.  This
  835.  *  amounts to a pnull for each operation.
  836.  */
  837. static novalue unopa(op,t)
  838. int op;
  839. nodeptr t;
  840.    {
  841.    switch (op) {
  842.       case NOTEQUIV:        /* unary ~ and three = operators */
  843.      emit("pnull");
  844.       case LEXNE:        /* unary ~ and two = operators */
  845.       case EQUIV:        /* three unary = operators */
  846.      emit("pnull");
  847.       case NUMNE:        /* unary ~ and = operators */
  848.       case UNION:        /* two unary + operators */
  849.       case DIFF:        /* two unary - operators */
  850.       case LEXEQ:        /* two unary = operators */
  851.       case INTER:        /* two unary * operators */
  852.      emit("pnull");
  853.       case BACKSLASH:        /* unary \ operator */
  854.       case BANG:        /* unary ! operator */
  855.       case CARET:        /* unary ^ operator */
  856.       case PLUS:        /* unary + operator */
  857.       case TILDE:        /* unary ~ operator */
  858.       case MINUS:        /* unary - operator */
  859.       case NUMEQ:        /* unary = operator */
  860.       case STAR:        /* unary * operator */
  861.       case QMARK:        /* unary ? operator */
  862.       case SLASH:        /* unary / operator */
  863.       case DOT:            /* unary . operator */
  864.          emit("pnull");
  865.          break;
  866.       default:
  867.      tsyserr("unopa: undefined unary operator");
  868.       }
  869.    }
  870.  
  871. /*
  872.  * unopb is the back-end code emitter for unary operators.  It emits
  873.  *  the operations represented by the token op.  For tokens representing
  874.  *  a single operator, the name of the operator is emitted.  For tokens
  875.  *  representing a sequence of operators, recursive calls are used.  In
  876.  *  such a case, the operator sequence is "scanned" from right to left
  877.  *  and unopb is called with the token for the appropriate operation.
  878.  *
  879.  * For example, consider the sequence of calls and code emission for "~===":
  880.  *    unopb(NOTEQUIV)        ~===
  881.  *        unopb(NUMEQ)    =
  882.  *        emits "tabmat"
  883.  *        unopb(NUMEQ)    =
  884.  *        emits "tabmat"
  885.  *        unopb(NUMEQ)    =
  886.  *        emits "tabmat"
  887.  *        emits "compl"
  888.  */
  889. static novalue unopb(op)
  890. int op;
  891.    {
  892.    register char *name;
  893.  
  894.    switch (op) {
  895.  
  896.       case DOT:            /* unary . operator */
  897.      name = "value";
  898.      break;
  899.  
  900.       case BACKSLASH:        /* unary \ operator */
  901.      name = "nonnull";
  902.      break;
  903.  
  904.       case BANG:        /* unary ! operator */
  905.      name = "bang";
  906.      break;
  907.  
  908.       case CARET:        /* unary ^ operator */
  909.      name = "refresh";
  910.      break;
  911.  
  912.       case UNION:        /* two unary + operators */
  913.      unopb(PLUS);
  914.       case PLUS:        /* unary + operator */
  915.      name = "number";
  916.      break;
  917.  
  918.       case NOTEQUIV:        /* unary ~ and three = operators */
  919.      unopb(NUMEQ);
  920.       case LEXNE:        /* unary ~ and two = operators */
  921.      unopb(NUMEQ);
  922.       case NUMNE:        /* unary ~ and = operators */
  923.      unopb(NUMEQ);
  924.       case TILDE:        /* unary ~ operator (cset compl) */
  925.      name = "compl";
  926.      break;
  927.  
  928.       case DIFF:        /* two unary - operators */
  929.      unopb(MINUS);
  930.       case MINUS:        /* unary - operator */
  931.      name = "neg";
  932.      break;
  933.  
  934.       case EQUIV:        /* three unary = operators */
  935.      unopb(NUMEQ);
  936.       case LEXEQ:        /* two unary = operators */
  937.      unopb(NUMEQ);
  938.       case NUMEQ:        /* unary = operator */
  939.      name = "tabmat";
  940.      break;
  941.  
  942.       case INTER:        /* two unary * operators */
  943.      unopb(STAR);
  944.       case STAR:        /* unary * operator */
  945.      name = "size";
  946.      break;
  947.  
  948.       case QMARK:        /* unary ? operator */
  949.      name = "random";
  950.      break;
  951.  
  952.       case SLASH:        /* unary / operator */
  953.      name = "null";
  954.      break;
  955.  
  956.       default:
  957.      emitn("?unop", op);
  958.      tsyserr("unopb: undefined unary operator");
  959.       }
  960.    emit(name);
  961.    }
  962.  
  963. /*
  964.  * setloc emits "filen" and "line" directives for the source location of
  965.  *  node n.  A directive is only emitted if the corrosponding value
  966.  *  has changed since the last time setloc was called.  Note:  File(n)
  967.  *  reportedly occasionally points at uninitialized data, producing
  968.  *  bogus results (as well as reams of filen commands).  We could use
  969.  *  comfile here instead; that would ignore any #line directives.
  970.  */
  971. static char *lastfiln = NULL;
  972. static int lastline = 0;
  973.  
  974. static novalue setloc(n)
  975. nodeptr n;
  976.    {
  977.    if ((n != NULL) &&
  978.       (TType(n) != N_Empty) &&
  979.       (File(n) != NULL) &&
  980.       (lastfiln == NULL || strcmp(File(n), lastfiln) != 0)) {
  981.          lastfiln = File(n);
  982.          emits("filen", lastfiln);
  983.          }
  984.  
  985. #ifdef EventMon
  986.    emitn("line", Line(n));
  987. #else                    /* EventMon */
  988.    if (Line(n) != lastline) {
  989.       lastline = Line(n);
  990.       emitn("line", Line(n));
  991.          }
  992. #endif                    /* EventMon */
  993.  
  994. #ifdef EventMon
  995.    emitn("colm", Col(n));
  996. #endif                    /* EventMon */
  997.  
  998.  
  999.    }
  1000.  
  1001. #ifdef MultipleRuns
  1002. /*
  1003.  * Reinitialize last file name and line number for repeated runs.
  1004.  */
  1005. novalue tcodeinit()
  1006.    {
  1007.    lastfiln = NULL;
  1008.  
  1009. #ifdef EventMon
  1010.    lastcol = 0;
  1011. #endif                    /* EventMon */
  1012.  
  1013.    }
  1014. #endif                    /* Multiple Runs */
  1015.  
  1016. /*
  1017.  * The emit* routines output ucode to codefile.  The various routines are:
  1018.  *
  1019.  *  emitlab(l) - emit "lab" instruction for label l.
  1020.  *  emit(s) - emit instruction s.
  1021.  *  emitl(s,a) - emit instruction s with reference to label a.
  1022.  *  emitn(s,n) - emit instruction s with numeric argument a.
  1023.  *  emits(s,a) - emit instruction s with string argument a.
  1024.  */
  1025. static novalue emitlab(l)
  1026. int l;
  1027.    {
  1028.    writecheck(fprintf(codefile, "lab L%d\n", l));
  1029.    }
  1030.  
  1031. static novalue emit(s)
  1032. char *s;
  1033.    {
  1034.    writecheck(fprintf(codefile, "\t%s\n", s));
  1035.    }
  1036.  
  1037. static novalue emitl(s, a)
  1038. char *s;
  1039. int a;
  1040.    {
  1041.    writecheck(fprintf(codefile, "\t%s\tL%d\n", s, a));
  1042.    }
  1043.  
  1044. static novalue emitn(s, a)
  1045. char *s;
  1046. int a;
  1047.    {
  1048.    writecheck(fprintf(codefile, "\t%s\t%d\n", s, a));
  1049.    }
  1050.  
  1051.  
  1052. static novalue emits(s, a)
  1053. char *s, *a;
  1054.    {
  1055.    writecheck(fprintf(codefile, "\t%s\t%s\n", s, a));
  1056.    }
  1057.  
  1058. /*
  1059.  * alclab allocates n labels and returns the first.  For the interpreter,
  1060.  *  labels are restarted at 1 for each procedure, while in the compiler,
  1061.  *  they start at 1 and increase throughout the entire compilation.
  1062.  */
  1063. static int alclab(n)
  1064. int n;
  1065.    {
  1066.    register int lab;
  1067.  
  1068.    lab = nextlab;
  1069.    nextlab += n;
  1070.    return lab;
  1071.    }
  1072.