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