Skip to content

Commit

Permalink
towards function calling with ret val
Browse files Browse the repository at this point in the history
  • Loading branch information
tjemg committed Aug 18, 2016
1 parent ac08b62 commit c21e2b1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
52 changes: 33 additions & 19 deletions cucu.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <ctype.h>
#include <string.h>

#define MAXTOKSZ 256

/* print fatal error message and exit */
static void error(const char *fmt, ...) {
va_list args;
Expand All @@ -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 */
Expand All @@ -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() {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -467,6 +478,7 @@ static void statement() {
printf("GENERATE_VAR %s_%s\n",context,tok);
readtok();
if (accept("=")) {
printf("HERE 2=\n");
expr();
}
numPreambleVars++;
Expand Down Expand Up @@ -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'
Expand Down
25 changes: 18 additions & 7 deletions gen-zpu/gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions tests/test_001.c
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit c21e2b1

Please sign in to comment.