home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 342b.lha / cpp / cpp5.c < prev    next >
C/C++ Source or Header  |  1990-01-26  |  23KB  |  931 lines

  1. /*
  2.  *                C P P 5 . C
  3.  *        E x p r e s s i o n   E v a l u a t i o n
  4.  *
  5.  * Edit History
  6.  * 31-Aug-84    MM    USENET net.sources release
  7.  * 04-Oct-84    MM    __LINE__ and __FILE__ must call ungetstring()
  8.  *            so they work correctly with token concatenation.
  9.  *            Added string formal recognition.
  10.  * 25-Oct-84    MM    "Short-circuit" evaluate #if's so that we
  11.  *            don't print unnecessary error messages for
  12.  *            #if !defined(FOO) && FOO != 0 && 10 / FOO ...
  13.  * 31-Oct-84    ado/MM    Added token concatenation
  14.  *  6-Nov-84    MM    Split from #define stuff, added sizeof stuff
  15.  * 19-Nov-84    ado    #if error returns TRUE for (sigh) compatibility
  16.  * 21-Oct-85    RMS    Rename `token' to `tokenbuf'
  17.  * 23-Oct-85    RMS    Treat undefined symbols as having value zero.
  18.  * 14-Mar-86    FNF    Incorporate macro based C debugging package.
  19.  *            Port to Commodore Amiga.
  20.  * 20-Aug-88    Ois    Conditionally compile sizeof stuff.
  21.  */
  22.  
  23. #include    <stdio.h>
  24. #include    <ctype.h>
  25. #include    "cppdef.h"
  26. #include    "cpp.h"
  27.  
  28. /*
  29.  * Evaluate an #if expression.
  30.  */
  31.  
  32. static char    *opname[] = {        /* For debug and error messages */
  33. "end of expression", "val", "id",
  34.   "+",   "-",  "*",  "/",  "%",
  35.   "<<", ">>",  "&",  "|",  "^",
  36.   "==", "!=",  "<", "<=", ">=",  ">",
  37.   "&&", "||",  "?",  ":",  ",",
  38.   "unary +", "unary -", "~", "!",  "(",  ")", "(none)",
  39. };
  40.  
  41. /*
  42.  * opdope[] has the operator precedence:
  43.  *     Bits
  44.  *      7    Unused (so the value is always positive)
  45.  *    6-2    Precedence (000x .. 017x)
  46.  *    1-0    Binary op. flags:
  47.  *        01    The binop flag should be set/cleared when this op is seen.
  48.  *        10    The new value of the binop flag.
  49.  * Note:  Expected, New binop
  50.  * constant    0    1    Binop, end, or ) should follow constants
  51.  * End of line    1    0    End may not be preceeded by an operator
  52.  * binary    1    0    Binary op follows a value, value follows.
  53.  * unary    0    0    Unary op doesn't follow a value, value follows
  54.  *   (          0       0       Doesn't follow value, value or unop follows
  55.  *   )        1    1    Follows value.    Op follows.
  56.  */
  57.  
  58. static char    opdope[OP_MAX] = {
  59.   0001,                 /* End of expression        */
  60.   0002,                 /* Digit            */
  61.   0000,                 /* Letter (identifier)          */
  62.   0141, 0141, 0151, 0151, 0151,     /* ADD, SUB, MUL, DIV, MOD    */
  63.   0131, 0131, 0101, 0071, 0071,     /* ASL, ASR, AND,  OR, XOR    */
  64.   0111, 0111, 0121, 0121, 0121, 0121,    /*  EQ,  NE,  LT,  LE,    GE,  GT */
  65.   0061, 0051, 0041, 0041, 0031,     /* ANA, ORO, QUE, COL, CMA    */
  66. /*
  67.  * Unary op's follow
  68.  */
  69.   0160, 0160, 0160, 0160,        /* NEG, PLU, COM, NOT        */
  70.   0170, 0013, 0023,            /* LPA, RPA, END        */
  71. };
  72. /*
  73.  * OP_QUE and OP_RPA have alternate precedences:
  74.  */
  75. #define OP_RPA_PREC    0013
  76. #define OP_QUE_PREC    0034
  77.  
  78. /*
  79.  * S_ANDOR and S_QUEST signal "short-circuit" boolean evaluation, so that
  80.  *    #if FOO != 0 && 10 / FOO ...
  81.  * doesn't generate an error message.  They are stored in optab.skip.
  82.  */
  83. #define S_ANDOR     2
  84. #define S_QUEST     1
  85.  
  86. typedef struct optab {
  87.     char    op;            /* Operator            */
  88.     char    prec;            /* Its precedence        */
  89.     char    skip;            /* Short-circuit: TRUE to skip    */
  90. } OPTAB;
  91. static int    evalue;         /* Current value from evallex() */
  92.  
  93. #ifdef    nomacargs
  94. FILE_LOCAL int
  95. isbinary(op)
  96. register int    op;
  97. {
  98.     return (op >= FIRST_BINOP && op <= LAST_BINOP);
  99. }
  100.  
  101. FILE_LOCAL int
  102. isunary(op)
  103. register int    op;
  104. {
  105.     return (op >= FIRST_UNOP && op <= LAST_UNOP);
  106. }
  107. #else
  108. #define isbinary(op)    (op >= FIRST_BINOP && op <= LAST_BINOP)
  109. #define isunary(op)     (op >= FIRST_UNOP  && op <= LAST_UNOP)
  110. #endif
  111.  
  112. /*
  113.  * The following definitions are used to specify basic variable sizes.
  114.  */
  115.  
  116. #if OK_SIZEOF
  117.  
  118. #ifndef S_CHAR
  119. #define S_CHAR        (sizeof (char))
  120. #endif
  121. #ifndef S_SINT
  122. #ifdef manx        /* Aztec/Manx C does not like "short int" */
  123. #define S_SINT        (sizeof (short))
  124. #else
  125. #define S_SINT        (sizeof (short int))
  126. #endif
  127. #endif
  128. #ifndef S_INT
  129. #define S_INT        (sizeof (int))
  130. #endif
  131. #ifndef S_LINT
  132. #define S_LINT        (sizeof (long int))
  133. #endif
  134. #ifndef S_FLOAT
  135. #define S_FLOAT     (sizeof (float))
  136. #endif
  137. #ifndef S_DOUBLE
  138. #define S_DOUBLE    (sizeof (double))
  139. #endif
  140. #ifndef S_PCHAR
  141. #define S_PCHAR     (sizeof (char *))
  142. #endif
  143. #ifndef S_PSINT
  144. #ifdef manx        /* Aztec/Manx C does not like "short int" */
  145. #define S_PSINT     (sizeof (short *))
  146. #else
  147. #define S_PSINT     (sizeof (short int *))
  148. #endif
  149. #endif
  150. #ifndef S_PINT
  151. #define S_PINT        (sizeof (int *))
  152. #endif
  153. #ifndef S_PLINT
  154. #define S_PLINT     (sizeof (long int *))
  155. #endif
  156. #ifndef S_PFLOAT
  157. #define S_PFLOAT    (sizeof (float *))
  158. #endif
  159. #ifndef S_PDOUBLE
  160. #define S_PDOUBLE    (sizeof (double *))
  161. #endif
  162. #ifndef S_PFPTR
  163. #define S_PFPTR     (sizeof (int (*)()))
  164. #endif
  165.  
  166.  
  167. typedef struct types {
  168.     short    type;            /* This is the bit if        */
  169.     char    *name;            /* this is the token word    */
  170. } TYPES;
  171.  
  172. static TYPES basic_types[] = {
  173.     { T_CHAR,    "char",         },
  174.     { T_INT,    "int",          },
  175.     { T_FLOAT,    "float",        },
  176.     { T_DOUBLE,    "double",       },
  177.     { T_SHORT,    "short",        },
  178.     { T_LONG,    "long",         },
  179.     { T_SIGNED,    "signed",       },
  180.     { T_UNSIGNED,    "unsigned",     },
  181.     { 0,        NULL,        },    /* Signal end        */
  182. };
  183.  
  184. /*
  185.  * Test_table[] is used to test for illegal combinations.
  186.  */
  187. static short test_table[] = {
  188.     T_FLOAT | T_DOUBLE | T_LONG | T_SHORT,
  189.     T_FLOAT | T_DOUBLE | T_CHAR | T_INT,
  190.     T_FLOAT | T_DOUBLE | T_SIGNED | T_UNSIGNED,
  191.     T_LONG    | T_SHORT  | T_CHAR,
  192.     0                        /* end marker    */
  193. };
  194.  
  195. /*
  196.  * The order of this table is important -- it is also referenced by
  197.  * the command line processor to allow run-time overriding of the
  198.  * built-in size values.  The order must not be changed:
  199.  *    char, short, int, long, float, double (func pointer)
  200.  */
  201. SIZES size_table[] = {
  202.     { T_CHAR,    S_CHAR,     S_PCHAR     },    /* char     */
  203.     { T_SHORT,    S_SINT,     S_PSINT     },    /* short int    */
  204.     { T_INT,    S_INT,        S_PINT        },    /* int        */
  205.     { T_LONG,    S_LINT,     S_PLINT     },    /* long     */
  206.     { T_FLOAT,    S_FLOAT,    S_PFLOAT    },    /* float    */
  207.     { T_DOUBLE, S_DOUBLE,    S_PDOUBLE    },    /* double    */
  208.     { T_FPTR,    0,        S_PFPTR     },    /* int (*())    */
  209.     { 0,    0,        0        },    /* End of table */
  210. };
  211.  
  212. #endif /* OK_SIZEOF */
  213.  
  214. int
  215. eval()
  216. /*
  217.  * Evaluate an expression.  Straight-forward operator precedence.
  218.  * This is called from control() on encountering an #if statement.
  219.  * It calls the following routines:
  220.  * evallex    Lexical analyser -- returns the type and value of
  221.  *        the next input token.
  222.  * evaleval    Evaluate the current operator, given the values on
  223.  *        the value stack.  Returns a pointer to the (new)
  224.  *        value stack.
  225.  * For compatiblity with older cpp's, this return returns 1 (TRUE)
  226.  * if a syntax error is detected.
  227.  */
  228. {
  229.     register int    op;        /* Current operator        */
  230.     register int    *valp;        /* -> value vector        */
  231.     register OPTAB    *opp;        /* Operator stack        */
  232.     int        prec;        /* Op precedence        */
  233.     int        binop;        /* Set if binary op. needed    */
  234.     int        op1;        /* Operand from stack        */
  235.     int        skip;        /* For short-circuit testing    */
  236.     int        value[NEXP];    /* Value stack            */
  237.     OPTAB        opstack[NEXP];    /* Operand stack        */
  238.     extern int    *evaleval();    /* Does actual evaluation       */
  239.  
  240.     DBUG_ENTER ("eval");
  241.     valp = value;
  242.     opp = opstack;
  243.     opp->op = OP_END;        /* Mark bottom of stack     */
  244.     opp->prec = opdope[OP_END];    /* And its precedence        */
  245.     opp->skip = 0;            /* Not skipping now        */
  246.     binop = 0;
  247. again:    ;
  248. #ifdef    DEBUG_EVAL
  249.     printf("In #if at again: skip = %d, binop = %d, line is: %s",
  250.         opp->skip, binop, infile->bptr);
  251. #endif
  252.     if ((op = evallex(opp->skip)) == OP_SUB && binop == 0)
  253.         op = OP_NEG;            /* Unary minus        */
  254.     else if (op == OP_ADD && binop == 0)
  255.         op = OP_PLU;            /* Unary plus        */
  256.     else if (op == OP_FAIL)
  257.         DBUG_RETURN (1);                    /* Error in evallex     */
  258. #ifdef    DEBUG_EVAL
  259.     printf("op = %s, opdope = %03o, binop = %d, skip = %d\n",
  260.         opname[op], opdope[op], binop, opp->skip);
  261. #endif
  262.     if (op == DIG) {                        /* Value?               */
  263.         if (binop != 0) {
  264.         cerror("misplaced constant in #if", NULLST);
  265.         DBUG_RETURN (1);
  266.         }
  267.         else if (valp >= &value[NEXP-1]) {
  268.         cerror("#if value stack overflow", NULLST);
  269.         DBUG_RETURN (1);
  270.         }
  271.         else {
  272. #ifdef    DEBUG_EVAL
  273.         printf("pushing %d onto value stack[%d]\n",
  274.             evalue, valp - value);
  275. #endif
  276.         *valp++ = evalue;
  277.         binop = 1;
  278.         }
  279.         goto again;
  280.     }
  281.     else if (op > OP_END) {
  282.         cerror("Illegal #if line", NULLST);
  283.         DBUG_RETURN (1);
  284.     }
  285.     prec = opdope[op];
  286.     if (binop != (prec & 1)) {
  287.         cerror("Operator %s in incorrect context", opname[op]);
  288.         DBUG_RETURN (1);
  289.     }
  290.     binop = (prec & 2) >> 1;
  291.     for (;;) {
  292. #ifdef    DEBUG_EVAL
  293.         printf("op %s, prec %d., stacked op %s, prec %d, skip %d\n",
  294.         opname[op], prec, opname[opp->op], opp->prec, opp->skip);
  295. #endif
  296.         if (prec > opp->prec) {
  297.         if (op == OP_LPA)
  298.             prec = OP_RPA_PREC;
  299.         else if (op == OP_QUE)
  300.             prec = OP_QUE_PREC;
  301.         op1 = opp->skip;        /* Save skip for test    */
  302.         /*
  303.          * Push operator onto op. stack.
  304.          */
  305.         opp++;
  306.         if (opp >= &opstack[NEXP]) {
  307.             cerror("expression stack overflow at op \"%s\"",
  308.             opname[op]);
  309.             DBUG_RETURN (1);
  310.         }
  311.         opp->op = op;
  312.         opp->prec = prec;
  313.         skip = (valp[-1] != 0);         /* Short-circuit tester */
  314.         /*
  315.          * Do the short-circuit stuff here.  Short-circuiting
  316.          * stops automagically when operators are evaluated.
  317.          */
  318.         if ((op == OP_ANA && !skip)
  319.          || (op == OP_ORO && skip))
  320.             opp->skip = S_ANDOR;    /* And/or skip starts    */
  321.         else if (op == OP_QUE)          /* Start of ?: operator */
  322.             opp->skip = (op1 & S_ANDOR) | ((!skip) ? S_QUEST : 0);
  323.         else if (op == OP_COL) {        /* : inverts S_QUEST    */
  324.             opp->skip = (op1 & S_ANDOR)
  325.                   | (((op1 & S_QUEST) != 0) ? 0 : S_QUEST);
  326.         }
  327.         else {                /* Other ops leave    */
  328.             opp->skip = op1;        /*  skipping unchanged. */
  329.         }
  330. #ifdef    DEBUG_EVAL
  331.         printf("stacking %s, valp[-1] == %d at %s",
  332.             opname[op], valp[-1], infile->bptr);
  333.         dumpstack(opstack, opp, value, valp);
  334. #endif
  335.         goto again;
  336.         }
  337.         /*
  338.          * Pop operator from op. stack and evaluate it.
  339.          * End of stack and '(' are specials.
  340.          */
  341.         skip = opp->skip;            /* Remember skip value    */
  342.         switch ((op1 = opp->op)) {          /* Look at stacked op   */
  343.         case OP_END:            /* Stack end marker    */
  344.         if (op == OP_EOE)
  345.             DBUG_RETURN (valp[-1]);     /* Finished ok.         */
  346.         goto again;            /* Read another op.    */
  347.  
  348.         case OP_LPA:            /* ( on stack           */
  349.         if (op != OP_RPA) {             /* Matches ) on input   */
  350.             cerror("unbalanced paren's, op is \"%s\"", opname[op]);
  351.             DBUG_RETURN (1);
  352.         }
  353.         opp--;                /* Unstack it        */
  354.         /* goto again;            -- Fall through     */
  355.  
  356.         case OP_QUE:
  357.         goto again;            /* Evaluate true expr.    */
  358.  
  359.         case OP_COL:            /* : on stack.        */
  360.         opp--;                /* Unstack :        */
  361.         if (opp->op != OP_QUE) {        /* Matches ? on stack?  */
  362.             cerror("Misplaced '?' or ':', previous operator is %s",
  363.             opname[opp->op]);
  364.             DBUG_RETURN (1);
  365.         }
  366.         /*
  367.          * Evaluate op1.
  368.          */
  369.         default:                /* Others:        */
  370.         opp--;                /* Unstack the operator */
  371. #ifdef    DEBUG_EVAL
  372.         printf("Stack before evaluation of %s\n", opname[op1]);
  373.         dumpstack(opstack, opp, value, valp);
  374. #endif
  375.         valp = evaleval(valp, op1, skip);
  376. #ifdef    DEBUG_EVAL
  377.         printf("Stack after evaluation\n");
  378.         dumpstack(opstack, opp, value, valp);
  379. #endif
  380.         }                    /* op1 switch end    */
  381.     }                    /* Stack unwind loop    */
  382. }
  383.  
  384. FILE_LOCAL int
  385. evallex(skip)
  386. int        skip;        /* TRUE if short-circuit evaluation    */
  387. /*
  388.  * Return next eval operator or value.    Called from eval().  It
  389.  * calls a special-purpose routines for 'char' strings and
  390.  * numeric values:
  391.  * evalchar    called to evaluate 'x'
  392.  * evalnum    called to evaluate numbers.
  393.  */
  394. {
  395.     register int    c, c1, t;
  396.  
  397.     DBUG_ENTER ("evallex");
  398. again:    do {                    /* Collect the token    */
  399.         c = skipws();
  400.         if ((c = macroid(c)) == EOF_CHAR || c == '\n') {
  401.         unget();
  402.         DBUG_RETURN (OP_EOE);           /* End of expression    */
  403.         }
  404.     } while ((t = type[c]) == LET && catenate());
  405.     if (t == INV) {                         /* Total nonsense       */
  406.         if (!skip) {
  407.         if (isascii(c) && isprint(c))
  408.             cierror("illegal character '%c' in #if", c);
  409.         else
  410.             cierror("illegal character (%d decimal) in #if", c);
  411.         }
  412.         DBUG_RETURN (OP_FAIL);
  413.     }
  414.     else if (t == QUO) {                    /* ' or "               */
  415.         if (c == '\'') {                    /* Character constant   */
  416.         evalue = evalchar(skip);        /* Somewhat messy       */
  417. #ifdef    DEBUG_EVAL
  418.         printf("evalchar returns %d.\n", evalue);
  419. #endif
  420.         DBUG_RETURN (DIG);              /* Return a value       */
  421.         }
  422.         cerror("Can't use a string in an #if", NULLST);
  423.         DBUG_RETURN (OP_FAIL);
  424.     }
  425.     else if (t == LET) {                    /* ID must be a macro   */
  426.         if (streq(tokenbuf, "defined")) {   /* Or defined name      */
  427.         c1 = c = skipws();
  428.         if (c == '(')                   /* Allow defined(name)  */
  429.             c = skipws();
  430.         if (type[c] == LET) {
  431.             evalue = (lookid(c) != NULL);
  432.             if (c1 != '('               /* Need to balance      */
  433.              || skipws() == ')')        /* Did we balance?      */
  434.             DBUG_RETURN (DIG);      /* Parsed ok            */
  435.         }
  436.         cerror("Bad #if ... defined() syntax", NULLST);
  437.         DBUG_RETURN (OP_FAIL);
  438.         }
  439. #if OK_SIZEOF
  440.         else if (streq(tokenbuf, "sizeof")) /* New sizeof hackery   */
  441.         DBUG_RETURN (dosizeof());       /* Gets own routine     */
  442. #endif
  443.         evalue = 0;
  444.         DBUG_RETURN (DIG);
  445.     }
  446.     else if (t == DIG) {                    /* Numbers are harder   */
  447.         evalue = evalnum(c);
  448. #ifdef    DEBUG_EVAL
  449.         printf("evalnum returns %d.\n", evalue);
  450. #endif
  451.     }
  452.     else if (strchr("!=<>&|\\", c) != NULL) {
  453.         /*
  454.          * Process a possible multi-byte lexeme.
  455.          */
  456.         c1 = cget();                        /* Peek at next char    */
  457.         switch (c) {
  458.         case '!':
  459.         if (c1 == '=')
  460.             DBUG_RETURN (OP_NE);
  461.         break;
  462.  
  463.         case '=':
  464.         if (c1 != '=') {                /* Can't say a=b in #if */
  465.             unget();
  466.             cerror("= not allowed in #if", NULLST);
  467.             DBUG_RETURN (OP_FAIL);
  468.         }
  469.         DBUG_RETURN (OP_EQ);
  470.  
  471.         case '>':
  472.         case '<':
  473.         if (c1 == c)
  474.             DBUG_RETURN ((c == '<') ? OP_ASL : OP_ASR);
  475.         else if (c1 == '=')
  476.             DBUG_RETURN ((c == '<') ? OP_LE  : OP_GE);
  477.         break;
  478.  
  479.         case '|':
  480.         case '&':
  481.         if (c1 == c)
  482.             DBUG_RETURN ((c == '|') ? OP_ORO : OP_ANA);
  483.         break;
  484.  
  485.         case '\\':
  486.         if (c1 == '\n')                 /* Multi-line if        */
  487.             goto again;
  488.         cerror("Unexpected \\ in #if", NULLST);
  489.         DBUG_RETURN (OP_FAIL);
  490.         }
  491.         unget();
  492.     }
  493.     DBUG_RETURN (t);
  494. }
  495.  
  496. #if OK_SIZEOF
  497.  
  498. FILE_LOCAL int
  499. dosizeof()
  500. /*
  501.  * Process the sizeof (basic type) operation in an #if string.
  502.  * Sets evalue to the size and returns
  503.  *    DIG        success
  504.  *    OP_FAIL     bad parse or something.
  505.  */
  506. {
  507.     register int    c;
  508.     register TYPES    *tp;
  509.     register SIZES    *sizp;
  510.     register short    *testp;
  511.     short        typecode;
  512.  
  513.     DBUG_ENTER ("dosizeof");
  514.     if ((c = skipws()) != '(')
  515.         goto nogood;
  516.     /*
  517.      * Scan off the tokens.
  518.      */
  519.     typecode = 0;
  520.     while ((c = skipws())) {
  521.         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
  522.         goto nogood;            /* End of line is a bug */
  523.         else if (c == '(') {                /* thing (*)() func ptr */
  524.         if (skipws() == '*'
  525.          && skipws() == ')') {          /* We found (*)         */
  526.             if (skipws() != '(')        /* Let () be optional   */
  527.             unget();
  528.             else if (skipws() != ')')
  529.             goto nogood;
  530.             typecode |= T_FPTR;     /* Function pointer    */
  531.         }
  532.         else {                /* Junk is a bug    */
  533.             goto nogood;
  534.         }
  535.         }
  536.         else if (type[c] != LET)            /* Exit if not a type   */
  537.         break;
  538.         else if (!catenate()) {             /* Maybe combine tokens */
  539.         /*
  540.          * Look for this unexpandable token in basic_types.
  541.          * The code accepts "int long" as well as "long int"
  542.          * which is a minor bug as bugs go (and one shared with
  543.          * a lot of C compilers).
  544.          */
  545.         for (tp = basic_types; tp->name != NULLST; tp++) {
  546.             if (streq(tokenbuf, tp->name))
  547.             break;
  548.         }
  549.         if (tp->name == NULLST) {
  550.             cerror("#if sizeof, unknown type \"%s\"", tokenbuf);
  551.             DBUG_RETURN (OP_FAIL);
  552.         }
  553.         typecode |= tp->type;        /* Or in the type bit    */
  554.         }
  555.     }
  556.     /*
  557.      * We are at the end of the type scan.    Chew off '*' if necessary.
  558.      */
  559.     if (c == '*') {
  560.         typecode |= T_PTR;
  561.         c = skipws();
  562.     }
  563.     if (c == ')') {                         /* Last syntax check    */
  564.         for (testp = test_table; *testp != 0; testp++) {
  565.         if (!bittest(typecode & *testp)) {
  566.             cerror("#if ... sizeof: illegal type combination", NULLST);
  567.             DBUG_RETURN (OP_FAIL);
  568.         }
  569.         }
  570.         /*
  571.          * We assume that all function pointers are the same size:
  572.          *        sizeof (int (*)()) == sizeof (float (*)())
  573.          * We assume that signed and unsigned don't change the size:
  574.          *        sizeof (signed int) == (sizeof unsigned int)
  575.          */
  576.         if ((typecode & T_FPTR) != 0)       /* Function pointer     */
  577.         typecode = T_FPTR | T_PTR;
  578.         else {                /* Var or var * datum    */
  579.         typecode &= ~(T_SIGNED | T_UNSIGNED);
  580.         if ((typecode & (T_SHORT | T_LONG)) != 0)
  581.             typecode &= ~T_INT;
  582.         }
  583.         if ((typecode & ~T_PTR) == 0) {
  584.         cerror("#if sizeof() error, no type specified", NULLST);
  585.         DBUG_RETURN (OP_FAIL);
  586.         }
  587.         /*
  588.          * Exactly one bit (and possibly T_PTR) may be set.
  589.          */
  590.         for (sizp = size_table; sizp->bits != 0; sizp++) {
  591.         if ((typecode & ~T_PTR) == sizp->bits) {
  592.             evalue = ((typecode & T_PTR) != 0)
  593.             ? sizp->psize : sizp->size;
  594.             DBUG_RETURN (DIG);
  595.         }
  596.         }                    /* We shouldn't fail    */
  597.         cierror("#if ... sizeof: bug, unknown type code 0x%x", typecode);
  598.         DBUG_RETURN (OP_FAIL);
  599.     }
  600.  
  601. nogood: unget();
  602.     cerror("#if ... sizeof() syntax error", NULLST);
  603.     DBUG_RETURN (OP_FAIL);
  604. }
  605.  
  606. FILE_LOCAL int
  607. bittest(value)
  608. /*
  609.  * TRUE if value is zero or exactly one bit is set in value.
  610.  */
  611. {
  612. #if (4096 & ~(-4096)) == 0
  613.     return ((value & ~(-value)) == 0);
  614. #else
  615.     /*
  616.      * Do it the hard way (for non 2's complement machines)
  617.      */
  618.     return (value == 0 || value ^ (value - 1) == (value * 2 - 1));
  619. #endif
  620. }
  621.  
  622. #endif /* OK_SIZEOF */
  623.  
  624. FILE_LOCAL int
  625. evalnum(c)
  626. register int    c;
  627. /*
  628.  * Expand number for #if lexical analysis.  Note: evalnum recognizes
  629.  * the unsigned suffix, but only returns a signed int value.
  630.  */
  631. {
  632.     register int    value;
  633.     register int    base;
  634.     register int    c1;
  635.  
  636.     DBUG_ENTER ("evalnum");
  637.     if (c != '0')
  638.         base = 10;
  639.     else if ((c = cget()) == 'x' || c == 'X') {
  640.         base = 16;
  641.         c = cget();
  642.     }
  643.     else base = 8;
  644.     value = 0;
  645.     for (;;) {
  646.         c1 = c;
  647.         if (isascii(c) && isupper(c1))
  648.         c1 = tolower(c1);
  649.         if (c1 >= 'a')
  650.         c1 -= ('a' - 10);
  651.         else c1 -= '0';
  652.         if (c1 < 0 || c1 >= base)
  653.         break;
  654.         value *= base;
  655.         value += c1;
  656.         c = cget();
  657.     }
  658.     if (c == 'u' || c == 'U')       /* Unsigned nonsense            */
  659.         c = cget();
  660.     unget();
  661.     DBUG_RETURN (value);
  662. }
  663.  
  664. FILE_LOCAL int
  665. evalchar(skip)
  666. int        skip;        /* TRUE if short-circuit evaluation    */
  667. /*
  668.  * Get a character constant
  669.  */
  670. {
  671.     register int    c;
  672.     register int    value;
  673.     register int    count;
  674.  
  675.     DBUG_ENTER ("evalchar");
  676.     instring = TRUE;
  677.     if ((c = cget()) == '\\') {
  678.         switch ((c = cget())) {
  679.         case 'a':                           /* New in Standard      */
  680. #if ('a' == '\a' || '\a' == ALERT)
  681.         value = ALERT;            /* Use predefined value */
  682. #else
  683.         value = '\a';                   /* Use compiler's value */
  684. #endif
  685.         break;
  686.  
  687.         case 'b':
  688.         value = '\b';
  689.         break;
  690.  
  691.         case 'f':
  692.         value = '\f';
  693.         break;
  694.  
  695.         case 'n':
  696.         value = '\n';
  697.         break;
  698.  
  699.         case 'r':
  700.         value = '\r';
  701.         break;
  702.  
  703.         case 't':
  704.         value = '\t';
  705.         break;
  706.  
  707.         case 'v':                           /* New in Standard      */
  708. #if ('v' == '\v' || '\v' == VT)
  709.         value = VT;            /* Use predefined value */
  710. #else
  711.         value = '\v';                   /* Use compiler's value */
  712. #endif
  713.         break;
  714.  
  715.         case 'x':                           /* '\xFF'               */
  716.         count = 3;
  717.         value = 0;
  718.         while ((((c = get()) >= '0' && c <= '9')
  719.              || (c >= 'a' && c <= 'f')
  720.              || (c >= 'A' && c <= 'F'))
  721.             && (--count >= 0)) {
  722.             value *= 16;
  723.             value += (c <= '9') ? (c - '0') : ((c & 0xF) + 9);
  724.         }
  725.         unget();
  726.         break;
  727.  
  728.         default:
  729.         if (c >= '0' && c <= '7') {
  730.             count = 3;
  731.             value = 0;
  732.             while (c >= '0' && c <= '7' && --count >= 0) {
  733.             value *= 8;
  734.             value += (c - '0');
  735.             c = get();
  736.             }
  737.             unget();
  738.         }
  739.         else value = c;
  740.         break;
  741.         }
  742.     }
  743.     else if (c == '\'')
  744.         value = 0;
  745.     else value = c;
  746.     /*
  747.      * We warn on multi-byte constants and try to hack
  748.      * (big|little)endian machines.
  749.      */
  750. #if BIG_ENDIAN
  751.     count = 0;
  752. #endif
  753.     while ((c = get()) != '\'' && c != EOF_CHAR && c != '\n') {
  754.         if (!skip)
  755.         ciwarn("multi-byte constant '%c' isn't portable", c);
  756. #if BIG_ENDIAN
  757.         count += BITS_CHAR;
  758.         value += (c << count);
  759. #else
  760.         value <<= BITS_CHAR;
  761.         value += c;
  762. #endif
  763.     }
  764.     instring = FALSE;
  765.     DBUG_RETURN (value);
  766. }
  767.  
  768. FILE_LOCAL int *
  769. evaleval(valp, op, skip)
  770. register int    *valp;
  771. int        op;
  772. int        skip;        /* TRUE if short-circuit evaluation    */
  773. /*
  774.  * Apply the argument operator to the data on the value stack.
  775.  * One or two values are popped from the value stack and the result
  776.  * is pushed onto the value stack.
  777.  *
  778.  * OP_COL is a special case.
  779.  *
  780.  * evaleval() returns the new pointer to the top of the value stack.
  781.  */
  782. {
  783.     register int    v1, v2;
  784.  
  785.     DBUG_ENTER ("evaleval");
  786.     if (isbinary(op))
  787.         v2 = *--valp;
  788.     v1 = *--valp;
  789. #ifdef    DEBUG_EVAL
  790.     printf("%s op %s", (isbinary(op)) ? "binary" : "unary",
  791.         opname[op]);
  792.     if (isbinary(op))
  793.         printf(", v2 = %d.", v2);
  794.     printf(", v1 = %d.\n", v1);
  795. #endif
  796.     switch (op) {
  797.     case OP_EOE:
  798.          break;
  799.  
  800.     case OP_ADD:
  801.         v1 += v2;
  802.         break;
  803.  
  804.     case OP_SUB:
  805.         v1 -= v2;
  806.         break;
  807.  
  808.     case OP_MUL:
  809.         v1 *= v2;
  810.         break;
  811.  
  812.     case OP_DIV:
  813.     case OP_MOD:
  814.         if (v2 == 0) {
  815.         if (!skip) {
  816.             cwarn("%s by zero in #if, zero result assumed",
  817.             (op == OP_DIV) ? "divide" : "mod");
  818.         }
  819.         v1 = 0;
  820.         }
  821.         else if (op == OP_DIV)
  822.         v1 /= v2;
  823.         else
  824.         v1 %= v2;
  825.         break;
  826.  
  827.     case OP_ASL:
  828.         v1 <<= v2;
  829.         break;
  830.  
  831.     case OP_ASR:
  832.         v1 >>= v2;
  833.         break;
  834.  
  835.     case OP_AND:
  836.         v1 &= v2;
  837.         break;
  838.  
  839.     case OP_OR:
  840.         v1 |= v2;
  841.         break;
  842.  
  843.     case OP_XOR:
  844.         v1 ^= v2;
  845.         break;
  846.  
  847.     case OP_EQ:
  848.         v1 = (v1 == v2);
  849.         break;
  850.  
  851.     case OP_NE:
  852.         v1 = (v1 != v2);
  853.         break;
  854.  
  855.     case OP_LT:
  856.         v1 = (v1 < v2);
  857.         break;
  858.  
  859.     case OP_LE:
  860.         v1 = (v1 <= v2);
  861.         break;
  862.  
  863.     case OP_GE:
  864.         v1 = (v1 >= v2);
  865.         break;
  866.  
  867.     case OP_GT:
  868.         v1 = (v1 > v2);
  869.         break;
  870.  
  871.     case OP_ANA:
  872.         v1 = (v1 && v2);
  873.         break;
  874.  
  875.     case OP_ORO:
  876.         v1 = (v1 || v2);
  877.         break;
  878.  
  879.     case OP_COL:
  880.         /*
  881.          * v1 has the "true" value, v2 the "false" value.
  882.          * The top of the value stack has the test.
  883.          */
  884.         v1 = (*--valp) ? v1 : v2;
  885.         break;
  886.  
  887.     case OP_NEG:
  888.         v1 = (-v1);
  889.         break;
  890.  
  891.     case OP_PLU:
  892.         break;
  893.  
  894.     case OP_COM:
  895.         v1 = ~v1;
  896.         break;
  897.  
  898.     case OP_NOT:
  899.         v1 = !v1;
  900.         break;
  901.  
  902.     default:
  903.         cierror("#if bug, operand = %d.", op);
  904.         v1 = 0;
  905.     }
  906.     *valp++ = v1;
  907.     DBUG_RETURN (valp);
  908. }
  909.  
  910. #ifdef    DEBUG_EVAL
  911. dumpstack(opstack, opp, value, valp)
  912. OPTAB        opstack[NEXP];    /* Operand stack        */
  913. register OPTAB    *opp;        /* Operator stack        */
  914. int        value[NEXP];    /* Value stack            */
  915. register int    *valp;        /* -> value vector        */
  916. {
  917.     DBUG_ENTER ("dumpstack");
  918.     printf("index op prec skip name -- op stack at %s", infile->bptr);
  919.     while (opp > opstack) {
  920.         printf(" [%2d] %2d  %03o    %d %s\n", opp - opstack,
  921.         opp->op, opp->prec, opp->skip, opname[opp->op]);
  922.         opp--;
  923.     }
  924.     while (--valp >= value) {
  925.         printf("value[%d] = %d\n", (valp - value), *valp);
  926.     }
  927.     DBUG_VOID_RETURN;
  928. }
  929. #endif
  930.  
  931.