home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 149_01 / a68eval.c < prev    next >
Text File  |  1989-01-13  |  12KB  |  463 lines

  1. /*
  2.     HEADER:        CUG149;
  3.     TITLE:        6801 Cross-Assembler (Portable);
  4.     FILENAME:    A68EVAL.C;
  5.     VERSION:    3.5;
  6.     DATE:        08/27/1988;
  7.  
  8.     DESCRIPTION:    "This program lets you use your computer to assemble
  9.             code for the Motorola 6800, 6801, 6802, 6803, 6808,
  10.             and 68701 microprocessors.  The program is written in
  11.             portable C rather than BDS C.  All assembler features
  12.             are supported except relocation, linkage, and macros.";
  13.  
  14.     KEYWORDS:    Software Development, Assemblers, Cross-Assemblers,
  15.             Motorola, MC6800, MC6801;
  16.  
  17.     SEE-ALSO:    CUG113, 6800 Cross-Assembler;
  18.  
  19.     SYSTEM:        CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
  20.     COMPILERS:    Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
  21.             Lattice C, Microsoft C,    QNIX C;
  22.  
  23.     WARNINGS:    "This program has compiled successfully on 2 UNIX
  24.             compilers, 5 MSDOS compilers, and 2 CP/M compilers.
  25.             A port to BDS C would be extremely difficult, but see
  26.             volume CUG113.  A port to Toolworks C is untried."
  27.  
  28.     AUTHORS:    William C. Colley III;
  29. */
  30.  
  31. /*
  32.               6801 Cross-Assembler in Portable C
  33.  
  34.            Copyright (c) 1985 William C. Colley, III
  35.  
  36. Revision History:
  37.  
  38. Ver    Date        Description
  39.  
  40. 3.0    APR 1985    Recoded from BDS C version 2.5.  WCC3.
  41.  
  42. 3.1    AUG 1985    Greatly shortened the routines find_symbol() and
  43.             new_symbol().  Fixed bugs in expression evaluator.
  44.             Added compilation instructions for Aztec C86,
  45.             Microsoft C, and QNIX C.  Added optional optimizations
  46.             for 16-bit machines.  Adjusted structure members for
  47.             fussy compilers.  WCC3.
  48.  
  49. 3.2    SEP 1985    Added the INCL pseudo-op and associated stuff.  WCC3.
  50.  
  51. 3.3    JUL 1986    Added compilation instructions and tweaks for CI-C86,
  52.             Eco-C88, and Lattice C.  WCC3.
  53.  
  54. 3.4    JAN 1987    Fixed bug that made "FCB 0," legal syntax.  WCC3.
  55.  
  56. 3.5    AUG 1988    Fixed a bug in the command line parser that puts it
  57.             into a VERY long loop if the user types a command line
  58.             like "A68 FILE.ASM -L".  WCC3 per Alex Cameron.
  59.  
  60. This file contains the assembler's expression evaluator and lexical analyzer.
  61. The lexical analyzer chops the input character stream up into discrete tokens
  62. that are processed by the expression analyzer and the line assembler.  The
  63. expression analyzer processes the token stream into unsigned results of
  64. arithmetic expressions.
  65. */
  66.  
  67. /*  Get global goodies:  */
  68.  
  69. #include "a68.h"
  70.  
  71. /*  Get access to global mailboxes defined in A68.C:            */
  72.  
  73. extern char line[];
  74. extern int filesp, forwd, pass;
  75. extern unsigned pc;
  76. extern FILE *filestk[], *source;
  77. extern TOKEN token;
  78.  
  79. /*  Expression analysis routine.  The token stream from the lexical    */
  80. /*  analyzer is processed as an arithmetic expression and reduced to an    */
  81. /*  unsigned value.  If an error occurs during the evaluation, the    */
  82. /*  global flag    forwd is set to indicate to the line assembler that it    */
  83. /*  should not base certain decisions on the result of the evaluation.    */
  84.  
  85. static int bad;
  86.  
  87. unsigned expr()
  88. {
  89.     SCRATCH unsigned u;
  90.     unsigned eval();
  91.  
  92.     bad = FALSE;
  93.     u = eval(START);
  94.     return bad ? 0 : u;
  95. }
  96.  
  97. static unsigned eval(pre)
  98. unsigned pre;
  99. {
  100.     register unsigned op, u, v;
  101.     TOKEN *lex();
  102.     void exp_error(), unlex();
  103.  
  104.     for (;;) {
  105.     u = op = lex() -> valu;
  106.     switch (token.attr & TYPE) {
  107.         case REG:
  108.         case IMM:    exp_error('S');  break;
  109.  
  110.         case SEP:    if (pre != START) unlex();
  111.         case EOL:    exp_error('E');  return;
  112.  
  113.         case OPR:    if (!(token.attr & UNARY)) { exp_error('E');  break; }
  114.             u = (op == '*' ? pc :
  115.                 eval((op == '+' || op == '-') ?
  116.                 (unsigned) UOP1 : token.attr & PREC));
  117.             switch (op) {
  118.                 case '-':    u = word(-u);  break;
  119.  
  120.                 case NOT:    u ^= 0xffff;  break;
  121.  
  122.                 case HIGH:    u = high(u);  break;
  123.  
  124.                 case LOW:    u = low(u);  break;
  125.             }
  126.  
  127.         case VAL:    
  128.         case STR:    for (;;) {
  129.                 op = lex() -> valu;
  130.                 switch (token.attr & TYPE) {
  131.                 case REG:
  132.                 case IMM:   exp_error('S');  break;
  133.  
  134.                 case SEP:   if (pre != START) unlex();
  135.                 case EOL:   if (pre == LPREN) exp_error('(');
  136.                         return u;
  137.  
  138.                 case STR:
  139.                 case VAL:   exp_error('E');  break;
  140.  
  141.                 case OPR:   if (!(token.attr & BINARY)) {
  142.                         exp_error('E');  break;
  143.                         }
  144.                         if ((token.attr & PREC) >= pre) {
  145.                         unlex();  return u;
  146.                         }
  147.                         if (op != ')')
  148.                         v = eval(token.attr & PREC);
  149.                         switch (op) {
  150.                         case '+':   u += v;  break;
  151.  
  152.                         case '-':   u -= v;  break;
  153.  
  154.                         case '*':   u *= v;  break;
  155.  
  156.                         case '/':   u /= v;  break;
  157.  
  158.                         case MOD:   u %= v;  break;
  159.  
  160.                         case AND:   u &= v;  break;
  161.  
  162.                         case OR:    u |= v;  break;
  163.  
  164.                         case XOR:   u ^= v;  break;
  165.  
  166.                         case '<':   u = u < v;  break;
  167.  
  168.                         case LE:    u = u <= v;  break;
  169.  
  170.                         case '=':   u = u == v;  break;
  171.  
  172.                         case GE:    u = u >= v;  break;
  173.  
  174.                         case '>':   u = u > v;  break;
  175.  
  176.                         case NE:    u = u != v;  break;
  177.  
  178.                         case SHL:   if (v > 15)
  179.                                 exp_error('E');
  180.                                 else u <<= v;
  181.                                 break;
  182.  
  183.                         case SHR:   if (v > 15)
  184.                                 exp_error('E');
  185.                                 else u >>= v;
  186.                                 break;
  187.  
  188.                         case ')':   if (pre == LPREN)
  189.                                 return u;
  190.                                 exp_error('(');
  191.                                 break;
  192.                         }
  193.                         clamp(u);
  194.                         break;
  195.                 }
  196.             }
  197.             break;
  198.     }
  199.     }
  200. }
  201.  
  202. static void exp_error(c)
  203. char c;
  204. {
  205.     forwd = bad = TRUE;  error(c);
  206. }
  207.  
  208. /*  Lexical analyzer.  The source input character stream is chopped up    */
  209. /*  into its component parts and the pieces are evaluated.  Symbols are    */
  210. /*  looked up, operators are looked up, etc.  Everything gets reduced    */
  211. /*  to an attribute word, a numeric value, and (possibly) a string    */
  212. /*  value.                                */
  213.  
  214. static int oldt = FALSE;
  215. static int quote = FALSE;
  216.  
  217. TOKEN *lex()
  218. {
  219.     SCRATCH char c, *p;
  220.     SCRATCH unsigned b;
  221.     SCRATCH OPCODE *o;
  222.     SCRATCH SYMBOL *s;
  223.     OPCODE *find_operator();
  224.     SYMBOL *find_symbol();
  225.     void exp_error(), make_number(), pops(), pushc(), trash();
  226.  
  227.     if (oldt) { oldt = FALSE;  return &token; }
  228.     trash();
  229.     if (isalph(c = popc())) {
  230.     pushc(c);  pops(token.sval);
  231.     if (o = find_operator(token.sval)) {
  232.         token.attr = o -> attr;
  233.         token.valu = o -> valu;
  234.     }
  235.     else {
  236.         token.attr = VAL;  token.valu = 0;
  237.         if (s = find_symbol(token.sval)) {
  238.         token.valu = s -> valu;
  239.         if (pass == 2 && s -> attr & FORWD) forwd = TRUE;
  240.         }
  241.         else exp_error('U');
  242.     }
  243.     }
  244.     else if (isnum(c)) {
  245.     pushc(c);  pops(token.sval);
  246.     for (p = token.sval; *p; ++p);
  247.     switch (toupper(*--p)) {
  248.         case 'B':    b = 2;  break;
  249.  
  250.         case 'O':
  251.         case 'Q':    b = 8;  break;
  252.  
  253.         default:    ++p;
  254.         case 'D':    b = 10;  break;
  255.  
  256.         case 'H':    b = 16;  break;
  257.     }
  258.     *p = '\0';  make_number(b);
  259.     }
  260.     else switch (c) {
  261.     case '%':   b = 2;  goto num;
  262.  
  263.     case '@':   b = 8;  goto num;
  264.  
  265.     case '$':   b = 16;
  266. num:            pops(token.sval);
  267.             make_number(b);
  268.             break;
  269.  
  270.     case '#':   token.attr = IMM;
  271.             break;
  272.  
  273.     case '(':   token.attr = UNARY + LPREN + OPR;
  274.             goto opr1;
  275.  
  276.     case ')':   token.attr = BINARY + RPREN + OPR;
  277.             goto opr1;
  278.  
  279.     case '+':   token.attr = BINARY + UNARY + ADDIT + OPR;
  280.             goto opr1;
  281.  
  282.     case '-':   token.attr = BINARY + UNARY + ADDIT + OPR;
  283.             goto opr1;
  284.  
  285.     case '*':   token.attr = BINARY + UNARY + MULT + OPR;
  286.             goto opr1;
  287.  
  288.     case '/':   token.attr = BINARY + MULT + OPR;
  289. opr1:            token.valu = c;
  290.             break;
  291.  
  292.     case '<':   token.valu = c;
  293.             if ((c = popc()) == '=') token.valu = LE;
  294.             else if (c == '>') token.valu = NE;
  295.             else pushc(c);
  296.             goto opr2;
  297.  
  298.     case '=':   token.valu = c;
  299.             if ((c = popc()) == '<') token.valu = LE;
  300.             else if (c == '>') token.valu = GE;
  301.             else pushc(c);
  302.             goto opr2;
  303.  
  304.     case '>':   token.valu = c;
  305.             if ((c = popc()) == '<') token.valu = NE;
  306.             else if (c == '=') token.valu = GE;
  307.             else pushc(c);
  308. opr2:            token.attr = BINARY + RELAT + OPR;
  309.             break;
  310.  
  311.     case '\'':
  312.     case '"':   quote = TRUE;  token.attr = STR;
  313.             for (p = token.sval; (*p = popc()) != c; ++p)
  314.             if (*p == '\n') { exp_error('"');  break; }
  315.             *p = '\0';  quote = FALSE;
  316.             if ((token.valu = token.sval[0]) && token.sval[1])
  317.             token.valu = (token.valu << 8) + token.sval[1];
  318.             break;
  319.  
  320.     case ',':   token.attr = SEP;
  321.             break;
  322.  
  323.         case '\n':  token.attr = EOL;
  324.             break;
  325.     }
  326.     return &token;
  327. }
  328.  
  329. static void make_number(base)
  330. unsigned base;
  331. {
  332.     SCRATCH char *p;
  333.     SCRATCH unsigned d;
  334.     void exp_error();
  335.  
  336.     token.attr = VAL;
  337.     token.valu = 0;
  338.     for (p = token.sval; *p; ++p) {
  339.     d = toupper(*p) - (isnum(*p) ? '0' : 'A' - 10);
  340.     token.valu = token.valu * base + d;
  341.     if (!ishex(*p) || d >= base) { exp_error('D');  break; }
  342.     }
  343.     clamp(token.valu);
  344.     return;
  345. }
  346.  
  347. int isalph(c)
  348. char c;
  349. {
  350.     return (c >= 'A' && c <= '~') || c == '!' ||
  351.     c == '&' || c == '.' || c == ':' || c == '?';
  352. }
  353.  
  354. static int isnum(c)
  355. char c;
  356. {
  357.     return c >= '0' && c <= '9';
  358. }
  359.  
  360. static int ishex(c)
  361. char c;
  362. {
  363.     return isnum(c) || ((c = toupper(c)) >= 'A' && c <= 'F');
  364. }
  365.  
  366. static int isalnum(c)
  367. char c;
  368. {
  369.     return isalph(c) || isnum(c);
  370. }
  371.  
  372. /*  Push back the current token into the input stream.  One level of    */
  373. /*  pushback is supported.                        */
  374.  
  375. void unlex()
  376. {
  377.     oldt = TRUE;
  378.     return;
  379. }
  380.  
  381. /*  Get an alphanumeric string into the string value part of the    */
  382. /*  current token.  Leading blank space is trashed.            */
  383.  
  384. void pops(s)
  385. char *s;
  386. {
  387.     void pushc(), trash();
  388.  
  389.     trash();
  390.     for (; isalnum(*s = popc()); ++s);
  391.     pushc(*s);  *s = '\0';
  392.     return;
  393. }
  394.  
  395. /*  Trash blank space and push back the character following it.        */
  396.  
  397. void trash()
  398. {
  399.     SCRATCH char c;
  400.     void pushc();
  401.  
  402.     while ((c = popc()) == ' ');
  403.     pushc(c);
  404.     return;
  405. }
  406.  
  407. /*  Get character from input stream.  This routine does a number of    */
  408. /*  other things while it's passing back characters.  All control    */
  409. /*  characters except \t and \n are ignored.  \t is mapped into ' '.    */
  410. /*  Semicolon is mapped to \n.  In addition, a copy of all input is set    */
  411. /*  up in a line buffer for the benefit of the listing.            */
  412.  
  413. static int oldc, eol;
  414. static char *lptr;
  415.  
  416. int popc()
  417. {
  418.     SCRATCH int c;
  419.  
  420.     if (oldc) { c = oldc;  oldc = '\0';  return c; }
  421.     if (eol) return '\n';
  422.     for (;;) {
  423.     if ((c = getc(source)) != EOF && (c &= 0377) == ';' && !quote) {
  424.         do *lptr++ = c;
  425.         while ((c = getc(source)) != EOF && (c &= 0377) != '\n');
  426.     }
  427.     if (c == EOF) c = '\n';
  428.     if ((*lptr++ = c) >= ' ' && c <= '~') return c;
  429.     if (c == '\n') { eol = TRUE;  *lptr = '\0';  return '\n'; }
  430.     if (c == '\t') return quote ? '\t' : ' ';
  431.     }
  432. }
  433.  
  434. /*  Push character back onto input stream.  Only one level of push-back    */
  435. /*  supported.  \0 cannot be pushed back, but nobody would want to.    */
  436.  
  437. void pushc(c)
  438. char c;
  439. {
  440.     oldc = c;
  441.     return;
  442. }
  443.  
  444. /*  Begin new line of source input.  This routine returns non-zero if    */
  445. /*  EOF    has been reached on the main source file, zero otherwise.    */
  446.  
  447. int newline()
  448. {
  449.     void fatal_error();
  450.  
  451.     oldc = '\0';  lptr = line;
  452.     oldt = eol = FALSE;
  453.     while (feof(source)) {
  454.     if (ferror(source)) fatal_error(ASMREAD);
  455.     if (filesp) {
  456.         fclose(source);
  457.         source = filestk[--filesp];
  458.     }
  459.     else return TRUE;
  460.     }
  461.     return FALSE;
  462. }
  463.