From c21e2b1ce33eb3fb3269a91efe05aa5de39d3165 Mon Sep 17 00:00:00 2001 From: Tiago Gasiba Date: Thu, 18 Aug 2016 17:29:33 +0200 Subject: [PATCH] towards function calling with ret val --- cucu.c | 52 ++++++++++++++++++++++++++++++------------------ gen-zpu/gen.c | 25 ++++++++++++++++------- tests/test_001.c | 11 ++++++++-- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/cucu.c b/cucu.c index 9cfec32..111f757 100644 --- a/cucu.c +++ b/cucu.c @@ -5,6 +5,8 @@ #include #include +#define MAXTOKSZ 256 + /* print fatal error message and exit */ static void error(const char *fmt, ...) { va_list args; @@ -14,10 +16,24 @@ static void error(const char *fmt, ...) { exit(1); } -/* - * LEXER - */ -#define MAXTOKSZ 256 +// +// SYMBOLS +// +#define MAXSYMBOLS 4096 +static struct sym { + char type; + int addr; + char name[MAXTOKSZ]; + int nParams; +} sym[MAXSYMBOLS]; + +static int sympos = 0; +int stack_pos = 0; + + +// +// LEXER +// static FILE *f; /* input source file */ static char tok[MAXTOKSZ]; /* current token */ static int tokpos; /* offset inside the current token */ @@ -30,6 +46,7 @@ static int numPreambleVars = 0; static int numGlobalVars = 0; static int lastIsReturn = 0; static int flagScanGlobalVars = 1; +static struct sym *currFunction = NULL; /* read next char */ void readchr() { @@ -134,22 +151,10 @@ void expect(int srclinenum, char *s) { } } -/* - * SYMBOLS - */ -#define MAXSYMBOLS 4096 -static struct sym { - char type; - int addr; - char name[MAXTOKSZ]; -} sym[MAXSYMBOLS]; -static int sympos = 0; - -int stack_pos = 0; - static struct sym *sym_find(char *s) { int i; struct sym *symbol = NULL; + for (i = 0; i < sympos; i++) { if (strcmp(sym[i].name, s) == 0) { symbol = &sym[i]; @@ -202,7 +207,7 @@ static void emit(void *buf, size_t len) { codepos += len; } -#define TYPE_NUM 0 +#define TYPE_NUM 0 #define TYPE_CHARVAR 1 #define TYPE_INTVAR 2 @@ -355,6 +360,11 @@ static int postfix_expr() { gen_stack_addr(stack_pos - call_addr - 1); gen_unref(TYPE_INTVAR); gen_call(); + if (currFunction) { + gen_call_cleanup(currFunction->nParams); + } else { + error("[line %d] Error: unexpected function exit\n",linenum); + } /* remove function address and args */ gen_pop(stack_pos - prev_stack_pos); stack_pos = prev_stack_pos; @@ -433,6 +443,7 @@ static int expr() { int type = bitwise_expr(); if (type != TYPE_NUM) { if (accept("=")) { + printf("HERE 1=\n"); gen_push(); expr(); if (type == TYPE_INTVAR) { emit(GEN_ASSIGN, GEN_ASSIGNSZ); @@ -467,6 +478,7 @@ static void statement() { printf("GENERATE_VAR %s_%s\n",context,tok); readtok(); if (accept("=")) { + printf("HERE 2=\n"); expr(); } numPreambleVars++; @@ -574,11 +586,13 @@ static void compile() { stack_pos = 0; var->addr = codepos; var->type = 'F'; + var->nParams = argc; gen_sym(var); - printf("FUNCTION: %s\n",var->name); + printf("FUNCTION: %s with %d params\n",var->name, argc); strcpy(context,var->name); genPreamble = 1; numPreambleVars = 0; + currFunction = var; statement(); // function body if (!lastIsReturn) { gen_ret(numPreambleVars); // issue a ret if user forgets to put 'return' diff --git a/gen-zpu/gen.c b/gen-zpu/gen.c index 9dbfa55..461b451 100644 --- a/gen-zpu/gen.c +++ b/gen-zpu/gen.c @@ -49,17 +49,23 @@ static int mem_pos = 0; #define GEN_JZSZ strlen(GEN_JZ) - struct _imm_struct { - int nImm; - int v[5]; - }; +struct _imm_struct { + int nImm; + int v[5]; +}; + +int fixme_offset = 0; +int addrCnt = 0; static struct _imm_struct _load_immediate( int32_t v ); static void gen_start(int nGlobalVars) { char buf[100]; - sprintf(buf,"GLOBALS %d\n",nGlobalVars); - emits("jmpCAFE\n"); + sprintf(buf,"GLOBALS %d\n", nGlobalVars); + strcat(buf,"---\n"); + fixme_offset = strlen(buf) + 1 + 3; + strcat(buf,"JMP xxxx\n"); + strcat(buf,"---\n"); emits(buf); } @@ -70,7 +76,7 @@ static void gen_finish() { error("ERROR: could not find main function\n"); } sprintf(s, "%04x", funcmain->addr); - memcpy(code+3, s, 4); + memcpy(code+fixme_offset, s, 4); printf("%s", code); } @@ -90,6 +96,11 @@ static void gen_postamble(int nVars) { stack_pos = stack_pos + 1; } +static void gen_call_cleanup(int nVars) { + char buf[100]; + sprintf(buf,"DO CLEAN %d\n",nVars); + emits(buf); +} static void gen_ret(int nVars) { gen_postamble(nVars); diff --git a/tests/test_001.c b/tests/test_001.c index f45d7e4..03f04da 100644 --- a/tests/test_001.c +++ b/tests/test_001.c @@ -1,10 +1,17 @@ int g_a; -int main( int c0, int c1 ) { +int func( int a ) { + g_a = 1; +} + +//int main( int c0, int c1, int c2, int c3 ) { +int main( void ) { int a; int b; // a = 0x4f; -// func(0x44); + func(0x44); + a = func(0x44); + a = 1 + func(2+0x44); return 0; }