scc

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

commit 3dc7f81061afadec94640f67cad310142f5256fb
parent fc93bb35a8cebade80d9f0b920bb93b2b78aa81d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 11 Feb 2015 10:36:38 +0100

Use a custom language for code() in cc2

This function can be simplified if we use a simple language for it,
because at this moment, it is only a format issue.

Diffstat:
Mcc2/code.c | 76+++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 35 insertions(+), 41 deletions(-)

diff --git a/cc2/code.c b/cc2/code.c @@ -22,54 +22,48 @@ static char *regnames[] = { [IXL]= "IXL",[IXH]= "IXH",[IYL]= "IYL",[IYH]= "IYH", [I] = "I" }; +static char *opfmt[] = { + [RET] = "\to", + [PUSH] = "\to\tr", + [POP] = "\to\tr", + [ADD] = "\to\tr,r", + [LD] = "\to\tr,r", + [ADDI] = "\to\tr,i", + [LDI] = "\to\tr,i", + [ADDX] = "\to\tr,(r+i)", + [ADCX] = "\to\tr,(r+i)", + [LDFX] = "\to\tr,(r+i)", + [LDX] = "\to\t(r+i),r", + [ADDR] = "a:" +}; + void code(char op, ...) { va_list va; - uint8_t reg1, reg2; - TINT imm; - short off; - char *label; + char *cp, c; va_start(va, op); - switch (op) { - case RET: - printf("\t%s\n", opnames[op]); - break; - case PUSH: case POP: - reg1 = va_arg(va, int); - printf("\t%s\t%s\n", opnames[op], regnames[reg1]); - break; - case ADD: case LD: - reg1 = va_arg(va, int); - reg2 = va_arg(va, int); - printf("\t%s\t%s,%s\n", - opnames[op], regnames[reg1], regnames[reg2]); - break; - case ADDI: case LDI: - reg1 = va_arg(va, int); - imm = va_arg(va, int); - printf("\t%s\t%s,%d\n", opnames[op], regnames[reg1], imm); - break; - case ADDX: case ADCX: case LDFX: - reg1 = va_arg(va, int); - reg2 = va_arg(va, int); - off = va_arg(va, int); - printf("\t%s\t%s,(%s%+d)\n", - opnames[op], regnames[reg1], regnames[reg2], off); - break; - case LDX: - reg1 = va_arg(va, int); - off = va_arg(va, int); - reg2 = va_arg(va, int); - printf("\t%s\t(%s%+d),%s\n", - opnames[op], regnames[reg1], off, regnames[reg2]); - break; - case ADDR: - label = va_arg(va, char *); - printf("%s:\n", label); - break; + for (cp = opfmt[op]; c = *cp; ++cp) { + switch (c) { + case 'o': + fputs(opnames[op], stdout); + break; + case 'r': + fputs(regnames[va_arg(va, int)], stdout); + break; + case 'i': + printf("%d", va_arg(va, int)); + break; + case 'a': + fputs(va_arg(va, char *), stdout); + break; + default: + putchar(c); + break; + } } + putchar('\n'); va_end(va); }