home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / src / gen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  7.1 KB  |  250 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)gen.c    5.2 (Berkeley) 4/16/91";
  36. #endif /* not lint */
  37.  
  38. #include "whoami.h"
  39. #ifdef OBJ
  40.     /*
  41.      *    and the rest of the file
  42.      */
  43. #include "0.h"
  44. #include "tree.h"
  45. #include "opcode.h"
  46. #include "objfmt.h"
  47.  
  48. /*
  49.  * This array tells the type
  50.  * returned by an arithmetic
  51.  * operation.  It is indexed
  52.  * by the logarithm of the
  53.  * lengths base 2.
  54.  */
  55. #ifndef    DEBUG
  56. char    arret[]    = {
  57.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  58.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  59.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  60.     TDOUBLE,    TDOUBLE,    TDOUBLE,    TDOUBLE
  61. };
  62. #else
  63. char    arret0[] = {
  64.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  65.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  66.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  67.     TDOUBLE,    TDOUBLE,    TDOUBLE,    TDOUBLE
  68. };
  69. char    arret1[] = {
  70.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  71.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  72.     T4INT,        T4INT,        T4INT,        TDOUBLE,
  73.     TDOUBLE,    TDOUBLE,    TDOUBLE,    TDOUBLE
  74. };
  75. char    *arret = arret0;
  76. #endif
  77.  
  78. /*
  79.  * These array of arithmetic and set
  80.  * operators are indexed by the
  81.  * tree nodes and is highly dependent
  82.  * on their order.  They thus take
  83.  * on the flavor of magic.
  84.  */
  85. int    arop[] = {
  86.     0, O_NEG2, O_MOD2, O_DIV2, O_DVD2, O_MUL2, O_ADD2, O_SUB2,
  87.     O_REL2, O_REL2, O_REL2, O_REL2, O_REL2, O_REL2
  88. };
  89. int    setop[] = {
  90.     O_MULT, O_ADDT, O_SUBT,
  91.     O_RELT, O_RELT, O_RELT, O_RELT, O_RELT, O_RELT,
  92. };
  93.  
  94. /*
  95.  * The following array is
  96.  * used when operating on
  97.  * two reals since they are
  98.  * shoved off in a corner in
  99.  * the interpreter table.
  100.  */
  101. int    ar8op[] = {
  102.     O_DVD8, O_MUL8, O_ADD8, O_SUB8,
  103.     O_REL8, O_REL8, O_REL8, O_REL8, O_REL8, O_REL8,
  104. };
  105.  
  106. /*
  107.  * The following arrays, which are linearizations
  108.  * of two dimensional arrays, are the offsets for
  109.  * arithmetic, relational and assignment operations
  110.  * indexed by the logarithms of the argument widths.
  111.  */
  112. #ifndef    DEBUG
  113. char artab[] = {
  114.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  115.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  116.     O_ADD24-O_ADD2,    O_ADD24-O_ADD2,    O_ADD4-O_ADD2,    O_ADD84-O_ADD2,
  117.     O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    O_ADD48-O_ADD2,    -1
  118. };
  119. #else
  120. char artab0[] = {
  121.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  122.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD42-O_ADD2,    O_ADD82-O_ADD2,
  123.     O_ADD24-O_ADD2,    O_ADD24-O_ADD2,    O_ADD4-O_ADD2,    O_ADD84-O_ADD2,
  124.     O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    O_ADD48-O_ADD2,    -1
  125. };
  126. char artab1[] = {
  127.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD82-O_ADD2,
  128.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD82-O_ADD2,
  129.     O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD2-O_ADD2,    O_ADD84-O_ADD2,
  130.     O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    O_ADD28-O_ADD2,    -1
  131. };
  132. char    *artab = artab0;
  133. #endif
  134. #ifndef DEBUG
  135. char reltab[] = {
  136.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  137.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  138.     O_REL24-O_REL2,    O_REL24-O_REL2,    O_REL4-O_REL2,    O_REL84-O_REL2,
  139.     O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL48-O_REL2,    O_REL8-O_REL2
  140. };
  141. #else
  142. char reltab0[] = {
  143.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  144.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL42-O_REL2,    O_REL82-O_REL2,
  145.     O_REL24-O_REL2,    O_REL24-O_REL2,    O_REL4-O_REL2,    O_REL84-O_REL2,
  146.     O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL48-O_REL2,    O_REL8-O_REL2
  147. };
  148. char reltab1[] = {
  149.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL82-O_REL2,
  150.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL82-O_REL2,
  151.     O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL2-O_REL2,    O_REL82-O_REL2,
  152.     O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL28-O_REL2,    O_REL8-O_REL2
  153. };
  154. char *reltab = reltab0;
  155. #endif
  156.  
  157. #ifndef DEBUG
  158. char asgntab[] = {
  159.     O_AS21-O_AS2,    O_AS21-O_AS2,    O_AS41-O_AS2,    -1,
  160.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS42-O_AS2,    -1,
  161.     O_AS24-O_AS2,    O_AS24-O_AS2,    O_AS4-O_AS2,    -1,
  162.     O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS48-O_AS2,    O_AS8-O_AS2,
  163. };
  164. #else
  165. char asgntb0[] = {
  166.     O_AS21-O_AS2,    O_AS21-O_AS2,    O_AS41-O_AS2,    -1,
  167.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS42-O_AS2,    -1,
  168.     O_AS24-O_AS2,    O_AS24-O_AS2,    O_AS4-O_AS2,    -1,
  169.     O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS48-O_AS2,    O_AS8-O_AS2,
  170. };
  171. char asgntb1[] = {
  172.     O_AS21-O_AS2,    O_AS21-O_AS2,    O_AS21-O_AS2,    -1,
  173.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS2-O_AS2,    -1,
  174.     O_AS2-O_AS2,    O_AS2-O_AS2,    O_AS2-O_AS2,    -1,
  175.     O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS28-O_AS2,    O_AS4-O_AS2,
  176. };
  177. char *asgntab = asgntb0;
  178. #endif
  179.  
  180. #ifdef DEBUG
  181. genmx()
  182. {
  183.  
  184.     arret = arret1;
  185.     artab = artab1;
  186.     reltab = reltab1;
  187.     asgntab = asgntb1;
  188. }
  189. #endif
  190.  
  191. /*
  192.  * Gen generates code for assignments,
  193.  * and arithmetic and string operations
  194.  * and comparisons.
  195.  */
  196. struct nl *
  197. gen(p, o, w1, w2)
  198.     int p, o, w1, w2;
  199. {
  200.     register i, j;
  201.     int op;
  202.  
  203.     switch (p) {
  204.         default:
  205.             panic("gen");
  206.         case O_AS2:
  207.         case NIL:
  208.             i = j = -1;
  209.             /*
  210.              * Take the log2 of the widths
  211.              * and linearize them for indexing.
  212.              * width for indexing.
  213.              */
  214. #ifdef DEBUG
  215.             if (hp21mx) {
  216.                 if (w1 == 4)
  217.                     w1 = 8;
  218.                 if (w2 == 4)
  219.                     w2 = 8;
  220.             }
  221. #endif
  222.             do i++; while (w1 >>= 1);
  223.             do j++; while (w2 >>= 1);
  224.             i <<= 2;
  225.             i |= j;
  226.             if (p == O_AS2) {
  227.                 (void) put(1, O_AS2 + asgntab[i]);
  228.                 return (NIL);
  229.             }
  230.             op = arop[o];
  231.             if (op == O_REL2) {
  232.                 (void) put(1, (op + reltab[i]) | (o - T_EQ) << 8+INDX);
  233.                 return (nl+TBOOL);
  234.             }
  235.             (void) put(1, i == 15 ? ar8op[o-T_DIVD] : op | artab[i]);
  236.             return (op == O_DVD2 && !divchk ? nl+TDOUBLE : nl+arret[i]);
  237.         case TREC:
  238.         case TSTR:
  239.             (void) put(2, O_RELG | (o - T_EQ) << 8+INDX, w1);
  240.             return (nl+TBOOL);
  241.         case TSET:
  242.             op = setop[o-T_MULT];
  243.             if (op == O_RELT)
  244.                 op |= (o - T_EQ)<<8+INDX;
  245.             (void) put(2, op, w1);
  246.             return (o >= T_EQ ? nl+TBOOL : nl+TSET);
  247.     }
  248. }
  249. #endif OBJ
  250.