Browse Source

simplify asm language

Mistivia 3 months ago
parent
commit
3e6bf6091c
6 changed files with 26 additions and 66 deletions
  1. 3 0
      .gitignore
  2. 6 2
      Makefile
  3. 5 4
      assembler/fvm-as.scm
  4. 0 43
      compile_commands.json
  5. 8 17
      examples/fibo.asm
  6. 4 0
      src/fvm.c

+ 3 - 0
.gitignore

@@ -3,3 +3,6 @@ fvm
 *.d
 *.bin
 .cache
+compile_commands.json
+fvm-as
+debug-*.sh

+ 6 - 2
Makefile

@@ -1,14 +1,18 @@
 target = fvm
-cflags = -g
+cflags = -g -O3
 ldflags = -lm
 cc = gcc
+csc = chicken-csc
 
 src = $(shell find src/ -name '*.c' -not -name 'main.c')
 obj = $(src:.c=.o)
 tests=$(shell find tests/ -name '*.c')
 tests_bin=$(tests:.c=.bin)
 
-all: $(target)
+all: $(target) fvm-as
+
+fvm-as: assembler/fvm-as.scm
+	$(csc) $< -o $@
 
 full: all $(tests_bin)
 

+ 5 - 4
assembler/fvm-as.scm

@@ -39,10 +39,10 @@
 (define pseudo-op-table
   (alist->hash-table
    '((rel . (pc add))
-     (bpick . ((imm 8) mul bp add ld))
-     (spick . ((imm 8) mul sp add ld))
-     (bput  . ((imm 8) mul bp add st))
-     (sput . ((imm 8) mul bp add st)))))
+     (ldarg . ((imm 2) add (imm 8) mul bp add ld))
+     (ldvar . ((imm -1) swap sub (imm 8) mul bp add ld))
+     (starg  . ((imm 2) add (imm 8) mul bp add st))
+     (stvar . ((imm -1) swap sub (imm 8) mul bp add st)))))
 
 (define (is-pseudo-op? sym)
   (hash-table-ref/default pseudo-op-table sym #f))
@@ -60,6 +60,7 @@
     (match lst
       (() (reverse ret))
       (('imm num . r) (loop r (cons (list 'imm num) ret)))
+      (((? number? x) . r) (loop r (cons (list 'imm x) ret)))
       ((x . r) (if (keyword? x)
                    (loop r
                          (cons (list 'tag

+ 0 - 43
compile_commands.json

@@ -1,43 +0,0 @@
-[
-  {
-    "arguments": [
-      "/usr/bin/gcc",
-      "-c",
-      "-g",
-      "-o",
-      "src/fvm.o",
-      "src/fvm.c"
-    ],
-    "directory": "/home/mistivia/repos/fvm",
-    "file": "/home/mistivia/repos/fvm/src/fvm.c",
-    "output": "/home/mistivia/repos/fvm/src/fvm.o"
-  },
-  {
-    "arguments": [
-      "/usr/bin/gcc",
-      "-c",
-      "-g",
-      "-o",
-      "src/main.o",
-      "src/main.c"
-    ],
-    "directory": "/home/mistivia/repos/fvm",
-    "file": "/home/mistivia/repos/fvm/src/main.c",
-    "output": "/home/mistivia/repos/fvm/src/main.o"
-  },
-  {
-    "arguments": [
-      "/usr/bin/gcc",
-      "-c",
-      "-Isrc/",
-      "-g",
-      "src/fvm.o",
-      "-o",
-      "tests/test_opcodes.bin",
-      "tests/test_opcodes.c"
-    ],
-    "directory": "/home/mistivia/repos/fvm",
-    "file": "/home/mistivia/repos/fvm/tests/test_opcodes.c",
-    "output": "/home/mistivia/repos/fvm/tests/test_opcodes.bin"
-  }
-]

+ 8 - 17
examples/fibo.asm

@@ -1,34 +1,25 @@
     ;; fibo(30)
-    imm 30
+    30
     fibo rel call
     ;; pop arguments and push return value to stack
     pop rv
     ;; call print_number (syscall number is 1)
-    imm 1 syscall
+    1 syscall
     exit
-fibo:
-    ;; setup stack pointer and base pointer
-    bp sp sbp
-    ;; get first argument
-    imm 2 bpick
+fibo: ;; function fibo(x)
     ;; if (x > 2) then
-    imm 2 gt
+    0 ldarg 2 gt
     .else rel jz
     ;; return fibo(x - 2) + fibo(x - 1)
-    imm 2 bpick
-    imm 1 sub
+    0 ldarg 1 sub
     fibo rel call
     pop rv
-    imm 2 bpick
-    imm 2 sub
+    0 ldarg 2 sub
     fibo rel call
     pop rv add
     srv
-    .end rel jmp
+    ret
 .else:
     ;; else return 1
-    imm 1 srv
-    .end rel jmp
-.end:
-    bp ssp sbp
+    1 srv
     ret

+ 4 - 0
src/fvm.c

@@ -340,8 +340,12 @@ int64_t fvm_execute(struct fvm *vm) {
       a = fvm_pop(vm);
       fvm_push(vm, vm->pc + 1);
       vm->pc = a;
+      fvm_push(vm, vm->bp);
+      vm->bp = vm->sp;
       break;
     case FVM_OP_RET:
+      vm->sp = vm->bp;
+      vm->bp = fvm_pop(vm);      
       a = fvm_pop(vm);
       vm->pc = a;
       break;