scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit afa081af77e19c67b8ded5203f8bed341af1cba6
parent 63cde7ec0b3cc2c47cf7338ce3cc63b2591b473c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Aug 2014 11:17:42 +0200

Simplify generation of addresability numbers

This code is inspired in the code written by Ken Thompson, so
it is a good idea follow the rules he wrote in 8c (which in
spirit is similar to z80).

Diffstat:
Mcc2/cc2.h | 6+++---
Mcc2/cgen.c | 61++++++++++++++++++-------------------------------------------
Mcc2/parser.c | 2+-
3 files changed, 22 insertions(+), 47 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -14,8 +14,8 @@ typedef struct { typedef struct node { char op; char type; - int8_t sethi; - int8_t addrtype; + int8_t complex; + int8_t addable; union { Symbol *sym; int imm; @@ -41,4 +41,4 @@ enum nerrors { #define STATIC 'S' extern void error(unsigned nerror, ...); - +extern void genaddable(Node *np); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -5,49 +5,24 @@ #include "cc2.h" -struct addrtype { - char op; - char left; - char right; - char addrtype; -} addrtbl[] = { - {'A', 0, 0, 1}, - {'#', 0, 0, 2}, - {'+', 1, 2, 3}, - {0}, -}; - -struct nodeattr { - char addrtype; - char sethi; -}; - -struct nodeattr -genaddr(Node *np) +void +genaddable(Node *np) { - struct nodeattr left, right; - struct addrtype *bp; - - left = (np->left) ? genaddr(np->left) : (struct nodeattr) {0, -1}; - right = (np->right) ? genaddr(np->right) : (struct nodeattr) {0, -1}; - - for (bp = addrtbl; bp->op; ++bp) { - if (bp->op == np->op && - left.addrtype == bp->left && - right.addrtype == bp->right) { - break; - } + if (!np) + return; + + np->complex = 0; + np->addable = 0; + switch (np->op) { + case AUTO: + np->addable = 11; + break; + case REGISTER: + np->addable = 13; + break; + case STATIC: + np->addable = 12; + break; } - - if ((np->addrtype = bp->addrtype) == 0) { - np->sethi = 0; - } else if (right.sethi < 0) { - np->sethi = (left.sethi > 1) ? left.sethi : 1; - } else { - int8_t d = left.sethi - right.sethi; - np->sethi = ((d < 0) ? right.sethi : left.sethi) + 1; - } - - return (struct nodeattr) {np->addrtype, np->sethi}; + return; } - diff --git a/cc2/parser.c b/cc2/parser.c @@ -277,7 +277,7 @@ parse(void) break; case 'X': function(); - genaddr(listexp[0]); + genaddable(listexp[0]); break; case EOF: return;