scc

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

commit f2c0749ff6ad1041feb2cdb8ac164fd37b6f2e8c
parent 34628a2ca10f9ca38785ec744ffc4b5e012fe86e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 10 Feb 2015 17:54:31 +0100

Move emit to a different source file in cc2

This will allow to define a better function, because at this moment
emit sucks a lot.

Diffstat:
cc2/Makefile | 2+-
cc2/cc2.h | 12++++++++++++
cc2/cgen.c | 62--------------------------------------------------------------
cc2/code.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 88 insertions(+), 63 deletions(-)

diff --git a/cc2/Makefile b/cc2/Makefile @@ -1,5 +1,5 @@ -OBJS = main.o parser.o cgen.o +OBJS = main.o parser.o cgen.o code.o CPPFLAGS = -I../inc LDFLAGS = -L../lib diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -98,9 +98,21 @@ enum nerrors { #define ADDABLE 10 + +enum { + PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDR, ADDX, ADCX, LDX, + LDFX +}; + +enum { + A = 1, B, C, D, E, H, L, IYL, IYH, NREGS, + IXL, IXH, F, I, SP, AF, HL, DE, BC, IX, IY +}; + extern void error(unsigned nerror, ...); extern void genaddable(Node *np); extern void generate(Symbol *fun); extern void genstack(Symbol *fun); extern void apply(Node *list[], void (*fun)(Node *)); extern Symbol *parse(void); +extern void emit(char op, ...); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -9,17 +9,6 @@ #include <stdio.h> -/* TODO: Change emit to code */ -enum { - PUSH, POP, LD, ADD, RET, ADDI, LDI, ADDR, ADDX, ADCX, LDX, - LDFX -}; - -enum { - A = 1, B, C, D, E, H, L, IYL, IYH, NREGS, - IXL, IXH, F, I, SP, AF, HL, DE, BC, IX, IY -}; - static char *opnames[] = { [PUSH] = "PUSH", [POP] = "POP", [LD] = "LD", [ADD] = "ADD", [RET] = "RET" , [ADDI]= "ADD", [LDI] = "LD", [ADDX] = "ADD", @@ -37,57 +26,6 @@ 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, ...) -{ - va_list va; - uint8_t reg1, reg2; - TINT imm; - short off; - char *label; - - 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; - } - - va_end(va); -} static char allocreg(Node *np) diff --git a/cc2/code.c b/cc2/code.c @@ -0,0 +1,75 @@ + +#include <stdarg.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> + +#include <cc.h> +#include "cc2.h" + + +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" +}; + +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" +}; + +void +emit(char op, ...) +{ + va_list va; + uint8_t reg1, reg2; + TINT imm; + short off; + char *label; + + 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; + } + + va_end(va); +}