]> khamidou — GitHub mirrors - miniforth.git/commitdiff
finished implementing call/ret
authorkhamidou <khamidou@gmail.com>
Thu, 20 Jan 2011 12:19:34 +0000 (13:19 +0100)
committerkhamidou <khamidou@gmail.com>
Thu, 20 Jan 2011 12:19:34 +0000 (13:19 +0100)
as.c
aslexer.l
tests/t4.as
tokens.h
vm.c
vm.h

diff --git a/as.c b/as.c
index 3963b450ec50037267db446bda1ed2d97eee6222..5ace3cacb89a2105bed739e0f0da1bb6d99a9197 100644 (file)
--- a/as.c
+++ b/as.c
@@ -13,6 +13,7 @@
 extern FILE* yyin;
 extern char* yytext;
 extern int yylex();
+extern int yylineno;
 FILE *outfp;
 
 /* labels are represented by a linked list */
@@ -139,6 +140,10 @@ int parse() {
                                writeint(l->offset, outfp);
                                break;
 
+                       case T_RET:
+                               writeint(RET, outfp);
+                               break;
+
                        case T_JMP:
                                yylex();
                                l = search_label(yytext);
@@ -150,7 +155,7 @@ int parse() {
                                break;
 
                        default:
-                               printf("unexpected %d in input", t);
+                               fail("unexpected %s in input at line %d", yytext, yylineno);
                                break;
                }
 
index d03f84af0d5831ea26c600f545d52116d93a9d63..2761acc3a8672a06fdc0650151bc73f3fc0ce80d 100644 (file)
--- a/aslexer.l
+++ b/aslexer.l
@@ -6,8 +6,6 @@
 %s in_comment
 %%
 
-<<EOF>>                { return -1;            }
-
 
 ;              BEGIN(in_comment);
 <in_comment>\n   BEGIN(INITIAL);
@@ -15,6 +13,7 @@
 <in_comment>;       // eat the lone ';'
 
 
+
 "push"         { return T_PUSH;        }
 "pop"          { return T_POP;         }
 "nop"          { return T_NOP;         }
@@ -22,7 +21,7 @@
 "inc"          { return T_INC;         }
 "dec"          { return T_DEC;         }
 "call"         { return T_CALL;        }
-"ret"          { return T_RET; }
+"ret"          { return T_RET;         }
 "jmp"          { return T_JMP;         }
 [ \t\n]+       /* ignore whitespace */
 
@@ -30,3 +29,4 @@
 [a-zA-Z][a-zA-Z0-9]*:  { return T_LABEL;       }
 [a-zA-Z][a-zA-Z0-9]*   { return WORD;  }
 
+<<EOF>>                { return -1;            }
index e07085c75bf2af7eeb0d448a2afa85482aec9059..1e73935a9c545f1a9e59ea1db61954a5aa65c4b2 100644 (file)
@@ -1,3 +1,4 @@
+;; ceci est un commentaire
 push 23
 dprint
 jmp t
index 354a20cf37e8fbb782d59a47bb0ac03bb66c09c9..7ba151619924f5191350fd9b252ed592f789aca8 100644 (file)
--- a/tokens.h
+++ b/tokens.h
@@ -1,6 +1,17 @@
 #ifndef __TOKENS_H__
 #define __TOKENS_H__
 
-enum tokens {NUMBER = 0, COMMA, WORD, T_LABEL, T_PUSH, T_POP, T_NOP, T_DPRINT, T_INC, T_DEC, T_CALL, T_JMP}; 
+enum tokens {  NUMBER, 
+               WORD, 
+               T_LABEL, 
+               T_PUSH, 
+               T_POP, 
+               T_NOP, 
+               T_DPRINT, 
+               T_INC, 
+               T_DEC, 
+               T_CALL, 
+               T_RET,
+               T_JMP}; 
 
 #endif
diff --git a/vm.c b/vm.c
index 5fb06f558aa0be0e80fab9c18747e51b454ac014..34838fe2fa6f90d212d9d67d138da5ee1bcc570c 100644 (file)
--- a/vm.c
+++ b/vm.c
@@ -2,6 +2,8 @@
 
 struct vm* alloc_vm(int memsize, int stacksize)
 {
+       int callstacksize = stacksize;
+
        struct vm *v = calloc(1, sizeof(struct vm));
        if (v == NULL)
                fail("Unable to allocate virtual machine");
@@ -14,9 +16,15 @@ struct vm* alloc_vm(int memsize, int stacksize)
        if (v->stack == NULL)
                fail("Unable to allocate memory for vm stack");
 
+       v->callstack = calloc(callstacksize, sizeof(char));
+       if (v->callstack == NULL)
+               fail("Unable to allocate memory for vm call stack");
+
        v->memsize = memsize;
        v->stacksize = stacksize;
        v->stackoffset = -1;
+       v->callstacksize = callstacksize;
+       v->callstackoffset = -1;
        return v;
 }
 
@@ -70,13 +78,25 @@ int run(struct vm *v)
 
                        case CALL:
                                /* save the previous instruction pointer */
-                               if (v->stackoffset >= v->stacksize) 
-                                       fail("stack overflow");
+                               if (v->callstackoffset >= v->callstacksize) 
+                                       fail("call stack overflow");
                        
-                               v->stackoffset++;
-                               v->stack[v->stackoffset] = (int) ip;
+                               v->callstackoffset++;
+                               v->callstack[v->callstackoffset] = (int) ip;
                                ip++;
-                               ip = v->stack + (*ip);
+                               ip = v->mem + (*ip) - 1;
+                               break;
+
+                       case RET:
+                               /* get ip from the stack. it must be the thing just before
+                                * the top of the stack - it implies that programs must return
+                                * one and only one return value
+                                */
+                               if (v->callstackoffset < 0) 
+                                       fail("call stack underflow");
+                       
+                               ip = v->callstack[v->callstackoffset];
+                               v->callstackoffset--;
                                break;
 
                        case JMP:
diff --git a/vm.h b/vm.h
index 5a29af1fc787d03c38b2149231b3be02dda920cd..6e55d41172a4547bb86725fc8f2177cab67c2cf9 100644 (file)
--- a/vm.h
+++ b/vm.h
@@ -12,6 +12,9 @@ struct vm {
        int *stack;
        int stacksize;
        int stackoffset;
+       int *callstack;
+       int callstacksize;
+       int callstackoffset;
 };
 
 struct vm* alloc_vm(int memsize, int stacksize);