commit 277d04a458bad76df8427891e3b8bfb694a7c238
parent c301d4451f6034fc462c8b852c1ba2e397a7892c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 8 Aug 2014 19:00:09 +0200
Emit function preambule
This is a first step in the code generation direction. It is
ugly, but we can test with it.
Diffstat:
3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/cc2/cc2.h b/cc2/cc2.h
@@ -19,6 +19,7 @@ typedef struct symbol {
} l;
struct {
char *name;
+ short stack;
struct symbol *pars;
struct symbol *vars;
} f;
@@ -66,3 +67,4 @@ enum nerrors {
extern void error(unsigned nerror, ...);
extern void genaddable(Node *list[]);
extern void cgen(Symbol *sym, Node *list[]);
+extern void genstack(Symbol *fun);
+\ No newline at end of file
diff --git a/cc2/cgen.c b/cc2/cgen.c
@@ -7,14 +7,32 @@
#include <stdio.h>
+void
+genstack(Symbol *fun)
+{
+ Symbol *p;
+ short siz;
+
+ for (siz = 0, p = fun->u.f.vars; p; p = p->next)
+ siz += p->u.v.type->size;
+ fun->u.f.stack = siz;
+}
static void
emit(char what, void *data)
{
+ Symbol *sym, *p;
+
switch (what) {
case FUN:
- printf("%s:\n", data);
- break;
+ sym = data;
+ printf("%s:\n"
+ "\tPUSH\tIX\n"
+ "\tLD\tIX,SP\n"
+ "\tLD\tHL,%d\n"
+ "\tADD\tHL,SP\n"
+ "\tLD\tSP,HL\n", sym->u.f.name, -sym->u.f.stack);
+ return;
default:
fputs("internal error: incorrect emit\n", stderr);
abort();
@@ -24,7 +42,7 @@ emit(char what, void *data)
void
cgen(Symbol *sym, Node *list[])
{
- emit(FUN, sym->u.f.name);
+ emit(FUN, sym);
}
/*
diff --git a/cc2/parser.c b/cc2/parser.c
@@ -305,6 +305,7 @@ endfunction(char *token)
{
funbody = 0;
listp = NULL;
+ genstack(curfun);
genaddable(listexp);
cgen(curfun, listexp);
}