home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / pi1 / gen.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  5KB  |  213 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #
  3. /*
  4.  * pi - Pascal interpreter code translator
  5.  *
  6.  * Charles Haley, Bill Joy UCB
  7.  * Version 1.2 January 1979
  8.  */
  9.  
  10. #include "0.h"
  11. #include "tree.h"
  12. #include "opcode.h"
  13.  
  14. /*
  15.  * This array tells the type
  16.  * returned by an arithmetic
  17.  * operation.  It is indexed
  18.  * by the logarithm of the
  19.  * lengths base 2.
  20.  */
  21. #ifndef    DEBUG
  22. char    arret[]    {
  23.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  24.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  25.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  26.     TDOUBLE,    TDOUBLE,    TDOUBLE,    TDOUBLE
  27. };
  28. #else
  29. char    arret0[] {
  30.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  31.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  32.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  33.     TDOUBLE,    TDOUBLE,    TDOUBLE,    TDOUBLE
  34. };
  35. char    arret1[] {
  36.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  37.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  38.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  39.     TDOUBLE,    TDOUBLE,    TDOUBLE,    TDOUBLE
  40. };
  41. char    *arret    arret0;
  42. #endif
  43.  
  44. /*
  45.  * These array of arithmetic and set
  46.  * operators are indexed by the
  47.  * tree nodes and is highly dependent
  48.  * on their order.  They thus take
  49.  * on the flavor of magic.
  50.  */
  51. int    arop[] {
  52.     0, O_NEG2, O_MOD2, O_DIV2, O_DVD2, O_MUL2, O_ADD2, O_SUB2,
  53.     O_REL2, O_REL2, O_REL2, O_REL2, O_REL2, O_REL2
  54. };
  55. int    setop[] {
  56.     O_MULT, O_ADDT, O_SUBT,
  57.     O_RELT, O_RELT, O_RELT, O_RELT, O_RELT, O_RELT,
  58. };
  59.  
  60. /*
  61.  * The following array is
  62.  * used when operating on
  63.  * two reals since they are
  64.  * shoved off in a corner in
  65.  * the interpreter table.
  66.  */
  67. int    ar8op[] {
  68.     O_DVD8, O_MUL8, O_ADD8, O_SUB8,
  69.     O_REL8, O_REL8, O_REL8, O_REL8, O_REL8, O_REL8,
  70. };
  71.  
  72. /*
  73.  * The following arrays, which are linearizations
  74.  * of two dimensional arrays, are the offsets for
  75.  * arithmetic, relational and assignment operations
  76.  * indexed by the logarithms of the argument widths.
  77.  */
  78. #ifndef    DEBUG
  79. char artab[]{
  80.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  81.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  82.     O_ADD24-O_ADD2,    O_ADD24-O_ADD2,    O_ADD4-O_ADD2,    O_ADD84-O_ADD2,
  83.     O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    O_ADD48-O_ADD2,    -1
  84. };
  85. #else
  86. char artab0[]{
  87.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  88.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  89.     O_ADD24-O_ADD2,    O_ADD24-O_ADD2,    O_ADD4-O_ADD2,    O_ADD84-O_ADD2,
  90.     O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    O_ADD48-O_ADD2,    -1
  91. };
  92. char artab1[]{
  93.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD82-O_ADD2,
  94.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD82-O_ADD2,
  95.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD84-O_ADD2,
  96.     O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    -1
  97. };
  98. char    *artab artab0;
  99. #endif
  100. #ifndef DEBUG
  101. char reltab[] {
  102.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  103.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  104.     O_REL24-O_REL2,    O_REL24-O_REL2,    O_REL4-O_REL2,    O_REL84-O_REL2,
  105.     O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL48-O_REL2,    O_REL8-O_REL2
  106. };
  107. #else
  108. char reltab0[] {
  109.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  110.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  111.     O_REL24-O_REL2,    O_REL24-O_REL2,    O_REL4-O_REL2,    O_REL84-O_REL2,
  112.     O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL48-O_REL2,    O_REL8-O_REL2
  113. };
  114. char reltab1[] {
  115.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL82-O_REL2,
  116.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL82-O_REL2,
  117.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL82-O_REL2,
  118.     O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL8-O_REL2
  119. };
  120. char *reltab reltab0;
  121. #endif
  122.  
  123. #ifndef DEBUG
  124. char asgntab[] {
  125.     O_AS21-O_AS2,    O_AS21-O_AS2,    O_AS41-O_AS2,    -1,
  126.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS42-O_AS2,    -1,
  127.     O_AS24-O_AS2,    O_AS24-O_AS2,    O_AS4-O_AS2,    -1,
  128.     O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS48-O_AS2,    O_AS8-O_AS2,
  129. };
  130. #else
  131. char asgntb0[] {
  132.     O_AS21-O_AS2,    O_AS21-O_AS2,    O_AS41-O_AS2,    -1,
  133.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS42-O_AS2,    -1,
  134.     O_AS24-O_AS2,    O_AS24-O_AS2,    O_AS4-O_AS2,    -1,
  135.     O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS48-O_AS2,    O_AS8-O_AS2,
  136. };
  137. char asgntb1[] {
  138.     O_AS21-O_AS2,    O_AS21-O_AS2,    O_AS21-O_AS2,    -1,
  139.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS2-O_AS2,    -1,
  140.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS2-O_AS2,    -1,
  141.     O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS4-O_AS2,
  142. };
  143. char *asgntab asgntb0;
  144. #endif
  145.  
  146. #ifdef DEBUG
  147. genmx()
  148. {
  149.  
  150.     arret = arret1;
  151.     artab = artab1;
  152.     reltab = reltab1;
  153.     asgntab = asgntb1;
  154. }
  155. #endif
  156.  
  157. /*
  158.  * Gen generates code for assignments,
  159.  * and arithmetic and string operations
  160.  * and comparisons.
  161.  */
  162. gen(p, o, w1, w2)
  163.     int p, o, w1, w2;
  164. {
  165.     register i, j;
  166.     int op, off;
  167.  
  168.     switch (p) {
  169.         case O_AS2:
  170.         case NIL:
  171.             i = j = -1;
  172.             /*
  173.              * Take the log2 of the widths
  174.              * and linearize them for indexing.
  175.              * width for indexing.
  176.              */
  177. #ifdef DEBUG
  178.             if (hp21mx) {
  179.                 if (w1 == 4)
  180.                     w1 = 8;
  181.                 if (w2 == 4)
  182.                     w2 = 8;
  183.             }
  184. #endif
  185.             do i++; while (w1=>> 1);
  186.             do j++; while (w2=>> 1);
  187.             i =<< 2;
  188.             i =| j;
  189.             if (p == O_AS2) {
  190.                 put1(O_AS2 + asgntab[i]);
  191.                 return (NIL);
  192.             }
  193.             op = arop[o];
  194.             if (op == O_REL2) {
  195.                 put1((op + reltab[i]) | (o - T_EQ) << 9);
  196.                 return (nl+TBOOL);
  197.             }
  198.             put1(i == 15 ? ar8op[o-T_DIVD] : op | artab[i]);
  199.             return (op == O_DVD2 && !divchk ? nl+TDOUBLE : nl+arret[i]);
  200.         case TSTR:
  201.             put2(O_RELG | (o - T_EQ) << 9, w1);
  202.             return (nl+TBOOL);
  203.         case TSET:
  204.             op = setop[o-T_MULT];
  205.             if (op == O_RELT)
  206.                 op =| (o - T_EQ)<<9;
  207.             put2(op, w1);
  208.             return (o >= T_EQ ? nl+TBOOL : nl+TSET);
  209.         default:
  210.             panic("gen");
  211.     }
  212. }
  213.