From 6355cf5e273ff7e5acabb8932077e77c9cff6571 Mon Sep 17 00:00:00 2001 From: khamidou Date: Tue, 18 Jan 2011 08:32:59 +0100 Subject: [PATCH] finally, the bytecode reading code works --- Makefile | 10 +++---- as.c | 85 ++++++++++++++++++++++++++++++++++++++----------------- aslexer.l | 4 ++- fail.c | 7 ++--- tokens.h | 2 +- vm.c | 23 ++++++++------- vm.h | 2 +- 7 files changed, 85 insertions(+), 48 deletions(-) diff --git a/Makefile b/Makefile index f6bcbb6..b9c6a73 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ -OBJS=vm.o main.o error.o +OBJS=vm.o main.o fail.o CFLAGS=-c -g vm_and_as: vm as -as: as.o lexer error.o - $(CC) as.o aslexer.o -o as -lfl +as: as.o lexer fail.o + $(CC) as.o aslexer.o fail.o -o as -lfl as.o: as.c $(CC) $(CFLAGS) as.c @@ -23,8 +23,8 @@ main.o: main.c $(CC) $(CFLAGS) main.c -error.o: error.c error.h - $(CC) $(CFLAGS) error.c +fail.o: fail.c fail.h + $(CC) $(CFLAGS) fail.c clean: rm *.o diff --git a/as.c b/as.c index cd053b9..3963b45 100644 --- a/as.c +++ b/as.c @@ -1,13 +1,14 @@ -/* a very crude assembler for the vm - it's one pass and - * spits out error messages the ken thompson way +/* a very crude assembler for the vm - + * it spits out fail messages the ken thompson way * */ #include #include #include +#include #include "vm.h" #include "tokens.h" -#include "error.h" +#include "fail.h" extern FILE* yyin; extern char* yytext; @@ -27,7 +28,7 @@ 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"); + fail("Unable to allocate memory for one label"); la->name = strdup(name); la->offset = offset; @@ -60,27 +61,37 @@ int writeint(int val, FILE* fp) { if (fp == NULL) return -1; + return fwrite(&val, sizeof(int), 1, fp); +} - /* 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; +/* the first pass only scans the file for labels */ +int first_pass() { + int t; + int current_offset = 0; /* the offset in the generated binary file */ + int s; + struct label *l; - fputc(c1, fp); - fputc(c2, fp); - fputc(c3, fp); - fputc(c4, fp); + while((t = yylex()) != -1) { + switch(t) { + case T_LABEL: + s = strlen(yytext); + yytext[s-1] = '\0'; /* get rid of the ':' */ - return 0; + l = alloc_label(yytext, current_offset); + append_label(l, label_head); + break; + default: + current_offset++; + break; + } + } } int parse() { int t; int current_offset = 0; /* the offset in the generated binary file */ + int s; + struct label *l; while((t = yylex()) != -1) { switch(t) { @@ -115,6 +126,29 @@ int parse() { writeint(DEC, outfp); break; + case T_LABEL: + break; + + case T_CALL: + yylex(); + l = search_label(yytext); + if (l == NULL) + fail("the label %s doesn't exists", yytext); + + writeint(CALL, outfp); + writeint(l->offset, outfp); + break; + + case T_JMP: + yylex(); + l = search_label(yytext); + if (l == NULL) + fail("the label %s doesn't exists", yytext); + + writeint(JMP, outfp); + writeint(l->offset, outfp); + break; + default: printf("unexpected %d in input", t); break; @@ -126,19 +160,14 @@ int parse() { int main(int argc, char **argv) { - if (argc > 2) { + if (argc != 2) { puts("as file.as"); exit(0); } - if (argc == 1) { - yyin = stdin; - } else { - yyin = fopen(filename, "r"); - - if (yyin == NULL) - error("Unable to open input file"); - } + yyin = fopen(argv[1], "r"); + if (yyin == NULL) + error("Unable to open input file"); outfp = fopen("a.bin", "w+"); if (outfp == NULL) @@ -146,6 +175,10 @@ int main(int argc, char **argv) label_head = alloc_label("", 0); + first_pass(); + fclose(yyin); + fopen(argv[1], "r"); + yyrestart(yyin); parse(); } diff --git a/aslexer.l b/aslexer.l index 2fd5eb7..37f1e6b 100644 --- a/aslexer.l +++ b/aslexer.l @@ -12,10 +12,12 @@ "dprint" { return T_DPRINT; } "inc" { return T_INC; } "dec" { return T_DEC; } +"call" { return T_CALL; } +"jmp" { return T_JMP; } [ \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 T_LABEL; } [a-zA-Z][a-zA-Z0-9]* { return WORD; } diff --git a/fail.c b/fail.c index 9b2636d..cdfda17 100644 --- a/fail.c +++ b/fail.c @@ -1,13 +1,12 @@ -#include "error.h" +#include "fail.h" -void error(char *fmt, ...) +void fail(char *fmt, ...) { va_list ap; va_start(ap, fmt); vprintf(fmt, ap); - va_end(ap); puts(""); - fflush(stdout); + va_end(ap); exit(EXIT_FAILURE); } diff --git a/tokens.h b/tokens.h index c262cf7..354a20c 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, T_INC, T_DEC}; +enum tokens {NUMBER = 0, COMMA, WORD, T_LABEL, T_PUSH, T_POP, T_NOP, T_DPRINT, T_INC, T_DEC, T_CALL, T_JMP}; #endif diff --git a/vm.c b/vm.c index 76997ac..7115e68 100644 --- a/vm.c +++ b/vm.c @@ -4,15 +4,15 @@ struct vm* alloc_vm(int memsize, int stacksize) { struct vm *v = calloc(1, sizeof(struct vm)); if (v == NULL) - error("Unable to allocate virtual machine"); + fail("Unable to allocate virtual machine"); v->mem = calloc(memsize, sizeof(char)); if (v->mem == NULL) - error("Unable to allocate memory for vm"); + fail("Unable to allocate memory for vm"); v->stack = calloc(stacksize, sizeof(char)); if (v->stack == NULL) - error("Unable to allocate memory for vm stack"); + fail("Unable to allocate memory for vm stack"); v->memsize = memsize; v->stacksize = stacksize; @@ -23,7 +23,7 @@ struct vm* alloc_vm(int memsize, int stacksize) int load_file(struct vm *v, FILE *fd) { if (fd == NULL || v == NULL) - error("Unable to load file"); + fail("Unable to load file"); fread(v->mem, sizeof(int), v->memsize, fd); } @@ -41,7 +41,7 @@ int run(struct vm *v) case PUSH: if (v->stackoffset >= v->stacksize) - error("stack overflow"); + fail("stack overflow"); ip++; v->stackoffset++; @@ -50,7 +50,7 @@ int run(struct vm *v) case POP: if (v->stackoffset < 0) - error("stack underflow"); + fail("stack underflow"); v->stackoffset--; break; @@ -71,15 +71,18 @@ int run(struct vm *v) case CALL: /* save the previous instruction pointer */ if (v->stackoffset >= v->stacksize) - error("stack overflow"); + fail("stack overflow"); v->stackoffset++; v->stack[v->stackoffset] = (int) ip; - - //yylex(); - //ip = atoi( + ip++; + ip = v->stack + (*ip); break; + case JMP: + ip++; + ip = v->stack + (*ip); + break; } diff --git a/vm.h b/vm.h index 8d1306f..5a29af1 100644 --- a/vm.h +++ b/vm.h @@ -4,7 +4,7 @@ #include #include -enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT, INC, DEC}; +enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, JMP, RET, DPRINT, INC, DEC}; struct vm { int *mem; -- 2.43.0