scc

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

commit da493b024ec13e759e5f5efd2e165120ca301e37
parent a1db98fafab9e5f348a1a1e16564caad4fb59f52
Author: Roberto E. Vargas Caballero <roberto.vargas@igrid-td.com>
Date:   Tue, 12 Apr 2016 16:41:09 +0200

[cc2] Add skeleton for code.c

This file is going to have the functions that implement the common
functions about the 3 address instructions. This code is basically
imported from cc2.old, but this time the basic structure of the
instructions is a 3 address, instead of a 2 address. The original
cc2 had 2 address instructions because it was only z80 targered,
but this new version wants to be multi targered, so it is better
a more general approach, although it will waste memory in accumulator
based architectures.

Diffstat:
Mcc2/Makefile | 2+-
Mcc2/arch/z80/code.c | 5-----
Mcc2/cc2.h | 19+++++++++++++++++++
Acc2/code.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 101 insertions(+), 6 deletions(-)

diff --git a/cc2/Makefile b/cc2/Makefile @@ -2,7 +2,7 @@ include ../config.mk -OBJS = main.o parser.o optm.o peep.o symbol.o node.o \ +OBJS = main.o parser.o optm.o peep.o symbol.o node.o code.o\ arch/$(ARCH)/code.o arch/$(ARCH)/cgen.o arch/$(ARCH)/types.o all: cc2 diff --git a/cc2/arch/z80/code.c b/cc2/arch/z80/code.c @@ -55,11 +55,6 @@ symname(Symbol *sym) return name; } -void -code(int op, Node *to, Node *from) -{ -} - static void label(Symbol *sym) { diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -38,6 +38,7 @@ enum op { CONST = '#', STRING = '"', LABEL = 'L', + INDEX = 'I', /* storage class */ GLOB = 'G', EXTRN = 'X', @@ -116,6 +117,8 @@ enum nerrors { typedef struct node Node; typedef struct type Type; typedef struct symbol Symbol; +typedef struct addr Addr; +typedef struct inst Inst; struct type { TSIZE size; @@ -144,6 +147,7 @@ struct node { char address; union { TUINT i; + char reg; char *s; Symbol *sym; char subop; @@ -153,6 +157,21 @@ struct node { Node *stmt; }; +struct addr { + char kind; + union { + char reg; + TUINT i; + Symbol *sym; + } u; +}; + +struct inst { + char op; + Addr from1, from2, to; + Inst *next, *prev; +}; + /* main.c */ extern void error(unsigned nerror, ...); diff --git a/cc2/code.c b/cc2/code.c @@ -0,0 +1,81 @@ + +#include <stdlib.h> + +#include "arch.h" +#include "cc2.h" + +static Inst *pc, *prog; + +static void +nextpc(void) +{ + Inst *new; + + new = malloc(sizeof(*new)); /* TODO: create an arena */ + + if (!pc) { + new->next = NULL; + prog = new; + } else { + new->next = pc->next; + pc->next = new; + } + + new->prev = pc; + new->to.kind = new->from2.kind = new->from1.kind = NONE; + pc = new; +} + +void +addr(int op, Node *np, Addr *addr) +{ + switch (addr->kind = np->op) { + case REG: + addr->u.reg = np->u.reg; + break; + case CONST: + /* TODO: different type of constants*/ + np->u.i = np->u.i; + break; + case LABEL: + case MEM: + addr->u.sym = np->u.sym; + break; + case AUTO: + case INDEX: + break; + default: + abort(); + } + +} + +void +code(int op, Node *to, Node *from1, Node *from2) +{ + nextpc(); + if (from1) + addr(op, from1, &pc->from1); + if (from2) + addr(op, from2, &pc->from2); + if (to) + addr(op, to, &pc->to); +} + + +void +delcode(void) +{ + Inst *prev = pc->prev, *next = pc->next; + + free(pc); + if (!prev) { + pc = next; + prog = NULL; + } else { + pc = prev; + prev->next = next; + if (next) + next->prev = prev; + } +}