]> khamidou — GitHub mirrors - miniforth.git/commitdiff
finally, the bytecode reading code works
authorkhamidou <khamidou@gmail.com>
Tue, 18 Jan 2011 07:32:59 +0000 (08:32 +0100)
committerkhamidou <khamidou@gmail.com>
Tue, 18 Jan 2011 07:32:59 +0000 (08:32 +0100)
Makefile
as.c
aslexer.l
fail.c
tokens.h
vm.c
vm.h

index f6bcbb6f2ce5f45e36dd5e258736f7eb0b97ec53..b9c6a73d554fb69e365d75dd821fd8715ce678fe 100644 (file)
--- 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 cd053b92e2131c69d857354143de73a607390950..3963b450ec50037267db446bda1ed2d97eee6222 100644 (file)
--- 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 <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <string.h>
 #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();
 }
 
index 2fd5eb767c7ec91aebacbe4505cb33bfbf4ceb9c..37f1e6b7f965b98b65a2a74de331e40b49139c06 100644 (file)
--- a/aslexer.l
+++ b/aslexer.l
 "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 9b2636d4d5ca66bcecdbc8bf8f1f121c0bf9e390..cdfda174804b6f186239a0ec130473eda5154a82 100644 (file)
--- 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);
 }
 
index c262cf719bc32ce71ca268bdc34f4ab4a485a06f..354a20cf37e8fbb782d59a47bb0ac03bb66c09c9 100644 (file)
--- 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 76997acc4069001943d3cae72c663da4519e7ff3..7115e68e7808f326f45ea5a1180aece2a88c7c11 100644 (file)
--- 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 8d1306f3500a27804b078c25094156b4fdb90f76..5a29af1fc787d03c38b2149231b3be02dda920cd 100644 (file)
--- a/vm.h
+++ b/vm.h
@@ -4,7 +4,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-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;