]> khamidou — GitHub mirrors - miniforth.git/commitdiff
added push/pop/inc/dec, and beginning to implement labels
authorkhamidou <khamidou@gmail.com>
Mon, 17 Jan 2011 18:04:12 +0000 (19:04 +0100)
committerkhamidou <khamidou@gmail.com>
Mon, 17 Jan 2011 18:04:12 +0000 (19:04 +0100)
README
as.c
aslexer.l
tests/t1.as
tests/t2.as
tokens.h
vm.c
vm.h

diff --git a/README b/README
index b3076183a14dc4dcde738608557b8f6448a21ff3..6bd49623a111656465393554fb4d04ff15809313 100644 (file)
--- 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 99cd25a7a6b6f83112890c42c119ce6c5aa67adf..cd053b92e2131c69d857354143de73a607390950 100644 (file)
--- 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();
 }
 
index 8ed3d121b614cc05f1d4f2ed47c199a4a88bc8a6..2fd5eb767c7ec91aebacbe4505cb33bfbf4ceb9c 100644 (file)
--- a/aslexer.l
+++ b/aslexer.l
 "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;  }
 
index 409940768f2a684935a7d15a29f96e82c487f439..c4e2dbcb132f0f94d180f9ce99b2a9c97830b34b 100644 (file)
@@ -1 +1,2 @@
-23
+nop
+nop
index f3b5056ded0baac3b1aa9362333d05fc888bf924..062842929dd60ac0d1a04085a027d280b0b79cf5 100644 (file)
@@ -2,3 +2,5 @@ NOP
 NOP
 PUSH 23
 DPRINT
+INC
+DPRINT
index 53831d1da3fd471a6490d3f4beabbfa0fd6bec6d..c262cf719bc32ce71ca268bdc34f4ab4a485a06f 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}; 
+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 0c170ca5ca8c09b0b9e62999fe60140bf3e49462..76997acc4069001943d3cae72c663da4519e7ff3 100644 (file)
--- 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 466745c5c09c524a0809b429671eabf5cd0f1df1..8d1306f3500a27804b078c25094156b4fdb90f76 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};
+enum opcodes {NOP = 0, PUSH, POP, ADD, CALL, RET, DPRINT, INC, DEC};
 
 struct vm {
        int *mem;