scc

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

commit 7144207c5a7cc559901322704be568daf4df2eaf
parent 47e8c646766773c8fda7e0fe44b10544c3f91695
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 17 Mar 2015 19:42:00 +0000

Move reguse to index() and moveto()

These are the places where the register are really reserved,
so it is the best place for updating the status of the registers.
We cannot merge index() and moveto() because one move the address
while the other move the content.

Diffstat:
Mcc2/cc2.h | 3++-
Mcc2/cgen.c | 62+++++++++++++++++++++++++++++++++++++++-----------------------
2 files changed, 41 insertions(+), 24 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -119,7 +119,8 @@ enum { enum { A = 1, B, C, D, E, H, L, IYL, IYH, NREGS, - SP = NREGS, AF, HL, DE, BC, IX, IY + AF = NREGS, HL, DE, BC, IY, NPAIRS, + SP = NPAIRS, IX }; extern Symbol *curfun; diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -99,49 +99,55 @@ allocreg(Node *np) switch (np->type.size) { case 1: - for (bp = reg8; (c = *bp); ++bp) { - if (reguse[c]) - continue; - return c; - } + bp = reg8; break; case 2: - for (bp = reg16; (c = *bp); ++bp) { - char u = upper[c], l = lower[c]; - - if (reguse[u] || reguse[l]) - continue; - return c; - } + bp = reg16; break; + default: + abort(); + } + while (c = *bp++) { + if (reguse[c]) + continue; + return c; } + /* TODO: What to do here? */ abort(); } static void moveto(Node *np, uint8_t reg) { - Symbol *sym = np->sym; + Node *r = regs[reg], *u = reguse[reg]; char op = np->op; + if (u) { + Symbol *sym = np->sym; + if (op == u->op && sym && sym == u->sym) + return; + else + ; /* TODO: Push the value? */ + } + switch (np->type.size) { case 1: switch (op) { - case CONST: - code(LDI, regs[reg], np); - break; case AUTO: - code(LDL, regs[reg], np); + code(LDL, r, np); + break; + case CONST: + case INDEX: + code(LDI, r, np); break; default: abort(); } - reguse[reg] = np; break; case 2: switch (op) { case CONST: - code(LDL, regs[reg], np); + code(LDL, r, np); break; case AUTO: code(LDL, regs[lower[reg]], np); @@ -150,11 +156,12 @@ moveto(Node *np, uint8_t reg) default: abort(); } - reguse[upper[reg]] = reguse[lower[reg]] = np; + reguse[upper[reg]] = reguse[lower[reg]] = np; break; default: abort(); } + reguse[reg] = np; np->op = REG; np->reg = reg; } @@ -202,11 +209,20 @@ accum(Node *np) static void index(Node *np) { - if (reguse[H] || reguse[L]) - push(HL); + Node *u = reguse[HL]; + + if (u) { + Symbol *sym = np->sym; + + if (u->op == INDEX && sym && sym == u->sym) { + np->op = INDEX; + return; + } else + ; /* TODO: Push the value? */ + } code(LDI, &reg_HL, np); - reguse[H] = reguse[L] = np; np->op = INDEX; + reguse[HL] = reguse[H] = reguse[L] = np; } static void