From: khamidou Date: Mon, 17 Jan 2011 18:04:12 +0000 (+0100) Subject: added push/pop/inc/dec, and beginning to implement labels X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=c77e5be0b6c52f6d0b2f953a95fd42c70133c6fd;p=miniforth.git added push/pop/inc/dec, and beginning to implement labels --- diff --git a/README b/README index b307618..6bd4962 100644 --- a/README +++ b/README @@ -3,3 +3,7 @@ miniforth at the moment, there is only a minimal vm implemented. see as.c and vm.c to see the assembler and the vm source code. + += format of binary files + + diff --git a/as.c b/as.c index 99cd25a..cd053b9 100644 --- a/as.c +++ b/as.c @@ -14,57 +14,138 @@ extern char* yytext; extern int yylex(); FILE *outfp; -int setup_parser(char *filename) { - yyin = fopen(filename, "r"); - if (yyin == NULL) - error("Unable to open input file"); +/* labels are represented by a linked list */ +struct label { + char *name; + int offset; + struct label *next; +}; - outfp = fopen("a.bin", "w+"); - if (outfp == NULL) - error("Unable to open output file"); +struct label* label_head; + +struct label* alloc_label(char *name, int offset) { + struct label* la = calloc(1, sizeof(struct label)); + + if (la == NULL) + error("Unable to allocate memory for one label"); + + la->name = strdup(name); + la->offset = offset; + + return la; +} + +struct label* search_label(char *name) { + struct label* l = label_head->next; + + while(l != NULL) { + if (strcmp(name, l->name) == 0) + return l; + + l = l->next; + } +} + +void append_label(struct label* l, struct label* head) { + struct label *t = head->next; + head->next = l; + l->next = t; +} + + +/* writeint: write an int in a file by breaking it + * in 8 chars + */ +int writeint(int val, FILE* fp) { + + if (fp == NULL) + return -1; + + /* comes from: + * http://stackoverflow.com/questions/1105938/writing-and-reading-long-int-value-in-c-code/1105961#1105961 + */ + + unsigned char c1 =(val >> 0) & 0xff; + unsigned char c2 = (val >> 8) & 0xff; + unsigned char c3 = (val >> 16) & 0xff; + unsigned char c4 = (val >> 24) & 0xff; + + fputc(c1, fp); + fputc(c2, fp); + fputc(c3, fp); + fputc(c4, fp); return 0; } int parse() { int t; + int current_offset = 0; /* the offset in the generated binary file */ + while((t = yylex()) != -1) { switch(t) { case T_PUSH: yylex(); - fputc(PUSH, outfp); - fputc(atoi(yytext), outfp); + writeint(PUSH, outfp); + writeint(atoi(yytext), outfp); + current_offset++; /* we're pushing to values to the file, so update + current_offset accordingly + */ break; case T_POP: yylex(); - fputc(POP, outfp); - fputc(atoi(yytext), outfp); + writeint(POP, outfp); + writeint(atoi(yytext), outfp); break; case T_DPRINT: - fputc(DPRINT, outfp); + writeint(DPRINT, outfp); break; case T_NOP: - fputc(NOP, outfp); + writeint(NOP, outfp); + break; + + case T_INC: + writeint(INC, outfp); + break; + + case T_DEC: + writeint(DEC, outfp); break; default: printf("unexpected %d in input", t); break; } + + current_offset++; } } int main(int argc, char **argv) { - if (argc != 2) { + if (argc > 2) { puts("as file.as"); exit(0); } - setup_parser(argv[1]); + if (argc == 1) { + yyin = stdin; + } else { + yyin = fopen(filename, "r"); + + if (yyin == NULL) + error("Unable to open input file"); + } + + outfp = fopen("a.bin", "w+"); + if (outfp == NULL) + error("Unable to open output file"); + + label_head = alloc_label("", 0); + parse(); } diff --git a/aslexer.l b/aslexer.l index 8ed3d12..2fd5eb7 100644 --- a/aslexer.l +++ b/aslexer.l @@ -10,7 +10,12 @@ "pop" { return T_POP; } "nop" { return T_NOP; } "dprint" { return T_DPRINT; } +"inc" { return T_INC; } +"dec" { return T_DEC; } +[ \t\n]+ /* ignore whitespace */ + [0-9]+ { return NUMBER; } ; { return COMMA; } +[a-zA-Z][a-zA-Z0-9]*: { print("ho"); return LABEL; } [a-zA-Z][a-zA-Z0-9]* { return WORD; } diff --git a/tests/t1.as b/tests/t1.as index 4099407..c4e2dbc 100644 --- a/tests/t1.as +++ b/tests/t1.as @@ -1 +1,2 @@ -23 +nop +nop diff --git a/tests/t2.as b/tests/t2.as index f3b5056..0628429 100644 --- a/tests/t2.as +++ b/tests/t2.as @@ -2,3 +2,5 @@ NOP NOP PUSH 23 DPRINT +INC +DPRINT diff --git a/tokens.h b/tokens.h index 53831d1..c262cf7 100644 --- a/tokens.h +++ b/tokens.h @@ -1,6 +1,6 @@ #ifndef __TOKENS_H__ #define __TOKENS_H__ -enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT}; +enum tokens {NUMBER = 0, COMMA, WORD, EOL, T_PUSH, T_POP, T_NOP, T_DPRINT, T_INC, T_DEC}; #endif diff --git a/vm.c b/vm.c index 0c170ca..76997ac 100644 --- a/vm.c +++ b/vm.c @@ -6,16 +6,17 @@ struct vm* alloc_vm(int memsize, int stacksize) if (v == NULL) error("Unable to allocate virtual machine"); - v->mem = calloc(memsize, sizeof(int)); + v->mem = calloc(memsize, sizeof(char)); if (v->mem == NULL) error("Unable to allocate memory for vm"); - v->stack = calloc(stacksize, sizeof(int)); + v->stack = calloc(stacksize, sizeof(char)); if (v->stack == NULL) error("Unable to allocate memory for vm stack"); v->memsize = memsize; v->stacksize = stacksize; + v->stackoffset = -1; return v; } @@ -24,7 +25,7 @@ int load_file(struct vm *v, FILE *fd) if (fd == NULL || v == NULL) error("Unable to load file"); - fread(v->mem, sizeof(char), v->memsize, fd); + fread(v->mem, sizeof(int), v->memsize, fd); } int run(struct vm *v) @@ -39,18 +40,16 @@ int run(struct vm *v) break; case PUSH: - puts("ho"); if (v->stackoffset >= v->stacksize) error("stack overflow"); ip++; - printf("%d", *ip); - v->stack[v->stackoffset] = *ip; v->stackoffset++; + v->stack[v->stackoffset] = *ip; break; case POP: - if (v->stackoffset == 0) + if (v->stackoffset < 0) error("stack underflow"); v->stackoffset--; @@ -60,6 +59,28 @@ int run(struct vm *v) /* a debug instruction - print the top of the stack */ printf("%d\n", v->stack[v->stackoffset]); break; + + case INC: + v->stack[v->stackoffset]++; + break; + + case DEC: + v->stack[v->stackoffset]--; + break; + + case CALL: + /* save the previous instruction pointer */ + if (v->stackoffset >= v->stacksize) + error("stack overflow"); + + v->stackoffset++; + v->stack[v->stackoffset] = (int) ip; + + //yylex(); + //ip = atoi( + break; + + } ip++; diff --git a/vm.h b/vm.h index 466745c..8d1306f 100644 --- a/vm.h +++ b/vm.h @@ -4,7 +4,7 @@ #include #include -enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT}; +enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT, INC, DEC}; struct vm { int *mem;