scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 4e83633b055cf9d8b411994d3ceb27d6341c34cf
parent 750ed60a7ec3af649c9cfa61f73077ed43b5e33d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 17 Sep 2014 17:41:38 +0200

Change lower and upper to arrays

They were functions, but they are going to be used in a lot of places,
so it is better to have an array that can be used in more places.
This patch also change the repition of code using list of registers.

Diffstat:
cc2/cgen.c | 79+++++++++++++++++++++++++++++--------------------------------------------------
1 file changed, 29 insertions(+), 50 deletions(-)

diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -16,22 +16,27 @@ enum { }; enum { - A, B, C, D, E, H, L, IYL, IYH, NREGS, IXL, IXH, F, I, SP, AF, HL, DE, BC, IX, IY + A = 1, B, C, D, E, H, L, IYL, IYH, NREGS, + IXL, IXH, F, I, SP, AF, HL, DE, BC, IX, IY }; -char *opnames[] = { +static char *opnames[] = { [PUSH] = "PUSH", [POP] = "POP", [LD] = "LD", [ADD] = "ADD", [RET] = "RET" , [ADDI]= "ADD", [LDI] = "LD", [ADDX] = "ADD", [ADCX] = "ADC" , [LDX] = "LD" , [LDFX] = "LD" }; -char *regnames[] = { +static char *regnames[] = { [AF] = "AF", [HL] = "HL", [DE] = "DE", [BC] = "BC", [IX] = "IX", [IY] = "IY", [SP] = "SP", [A] = "A", [F] = "F", [B] = "B", [C] = "C", [D] = "D", [E] = "E", [H] = "H", [L] = "L", [IXL]= "IXL",[IXH]= "IXH",[IYL]= "IYL",[IYH]= "IYH", [I] = "I" }; +static bool reguse[NREGS]; +static char upper[] = {[DE] = D, [HL] = H, [BC] = B, [IX] = IXH, [IY] = IYH}; +static char lower[] = {[DE] = E, [HL] = L, [BC] = C, [IX] = IXL, [IY] = IYL}; + void emit(char op, ...) { @@ -92,60 +97,34 @@ emit(char op, ...) } static char -upper(char pair) -{ - switch (pair) { - case HL: return H; - case BC: return B; - case DE: return D; - case IY: return IYH; - } -} - -static char -lower(char pair) -{ - switch (pair) { - case HL: return L; - case DE: return E; - case BC: return B; - case IY: return IYL; - } -} - -static char allocreg(Node *np) { - static bool regs[NREGS], *bp; + char reg8[] = {A, B, C, D, E, H, L, IYL, IY, 0}; + char reg16[] = {BC, HL, DE, IY, 0}; + char *bp, c; switch (np->type->size) { case 1: - for (bp = regs; bp < &regs[NREGS]; ++bp) { - if (*bp) + for (bp = reg8; (c = *bp); ++bp) { + if (reguse[c]) continue; - *bp = 1; - return bp - regs; + reguse[c] = 1; + return c; } /* TODO: Move variable to stack using PUSH/POP */ break; case 2: - if (!regs[H] && !regs[L]) { - regs[H] = regs [L] = 1; - return HL; - } - if (!regs[D] && !regs[E]) { - regs[D] = regs [E] = 1; - return DE; - } - if (!regs[C] && !regs[B]) { - regs[B] = regs [C] = 1; - return BC; - } - if (!regs[IYL] && !regs[IYH]) { - regs[IYL] = regs [IYH] = 1; - return IY; + for (bp = reg16; (c = *bp); ++bp) { + char u = upper[c], l = lower[c]; + + if (reguse[u] || reguse[l]) + continue; + reguse[l] = 1; + reguse[u]; + return c; } /* TODO: Move variable to stack using PUSH/POP */ + break; } abort(); } @@ -167,8 +146,8 @@ move(Node *np) emit(LDFX, reg, IX, sym->u.v.off); break; case 2: - emit(LDFX, lower(reg), IX, sym->u.v.off); - emit(LDFX, upper(reg), IX, sym->u.v.off+1); + emit(LDFX, lower[reg], IX, sym->u.v.off); + emit(LDFX, upper[reg], IX, sym->u.v.off+1); break; case 4: case 8: @@ -246,11 +225,11 @@ cgen(Node *np) conmute(np); lp = np->left; rp = np->right; - } else if (lp->u.reg != HL || lp->u.reg != IY) { + } else if (lp->u.reg != HL && lp->u.reg != IY) { /* TODO: Move HL to variable */ emit(PUSH, HL); - emit(LD, H, upper(lp->u.reg)); - emit(LD, L, lower(lp->u.reg)); + emit(LD, H, upper[lp->u.reg]); + emit(LD, L, lower[lp->u.reg]); } emit(ADD, lp->u.reg, rp->u.reg); break;