scc

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

commit 2d9f4dcdec7a6a85817ec1a58b7f106aa9f43b0d
parent d6ff7f1f2ae3daa13b2d910ff41b6bc4d982d087
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  8 Aug 2014 15:46:54 +0200

Add emit() stub in cc2

emit is the function that is going to generate the assembler code.
We want to avoid the generation of assembler code and posterior
assembler stage, but it is early for it and printing to stdout
makes easier debug at this stage.

Diffstat:
Mcc1/code.c | 3++-
Mcc2/cc2.h | 19++++++++++++-------
Mcc2/cgen.c | 25+++++++++++++++++++++++--
Mcc2/parser.c | 8+++++++-
4 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/cc1/code.c b/cc1/code.c @@ -189,7 +189,8 @@ emitprint(Node *np) void emitfun(Symbol *sym) { - printf("%c%s\t{\n", sym->s.isglobal ? 'X' : 'Y', sym->name); + printf("%c%d\t%s\t{\n", + sym->s.isglobal ? 'X' : 'Y', sym->id, sym->name); } void diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -8,6 +8,9 @@ typedef struct { struct { short addr; } l; + struct { + char *name; + } f; } u; } Symbol; @@ -38,13 +41,15 @@ enum nerrors { ENUMERR }; -#define AUTO 'A' -#define REGISTER 'R' -#define STATIC 'S' -#define CONST '#' -#define LABEL 'L' -#define OADD '+' -#define OSUB '-' +#define FUNCTION 0 +#define AUTO 'A' +#define REGISTER 'R' +#define STATIC 'T' +#define CONST '#' +#define LABEL 'L' +#define OADD '+' +#define OSUB '-' extern void error(unsigned nerror, ...); extern void genaddable(Node *list[]); +extern void cgen(Symbol *sym, Node *list[]); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -1,10 +1,31 @@ -#include <assert.h> -#include <stddef.h> #include <stdint.h> +#include <stdlib.h> #include "cc2.h" + +#include <stdio.h> + +static void +emit(char what, void *data) +{ + switch (what) { + case FUNCTION: + printf("%s:\n", data); + break; + default: + fputs("internal error: incorrect emit\n", stderr); + abort(); + } +} + +void +cgen(Symbol *sym, Node *list[]) +{ + emit(FUNCTION, sym->u.f.name); +} + /* * calculate addresability as follows * AUTO => 11 diff --git a/cc2/parser.c b/cc2/parser.c @@ -15,7 +15,8 @@ #define NR_NODEPOOL 128 #define NR_EXPRESSIONS 64 -char funbody; +static char funbody; +static Symbol *curfun; static Node *stack[NR_STACKSIZ], **stackp = stack; static Node *listexp[NR_EXPRESSIONS], **listp = listexp; static Node nodepool[NR_NODEPOOL], *newp = nodepool; @@ -211,6 +212,10 @@ static void function(char *token) { funbody = 1; + curfun = global(token); + if ((token = strtok(NULL, "\t")) == NULL) + error(ESYNTAX); + curfun->u.f.name = xstrdup(token); listp = listexp; newp = nodepool; } @@ -221,6 +226,7 @@ endfunction(char *token) funbody = 0; listp = NULL; genaddable(listexp); + cgen(curfun, listexp); } void