Browse Source

add syscall

Mistivia 4 months ago
parent
commit
cd81899339
3 changed files with 13 additions and 7 deletions
  1. 5 6
      assembler/fvm-as.scm
  2. 1 1
      src/fvm.h
  3. 7 0
      src/main.c

+ 5 - 6
assembler/fvm-as.scm

@@ -153,15 +153,14 @@
 
 (define (output-prog prog output-file)
   (define (write-number x port)
-    (cond ((or (rational? x)
-               (real? x))    (write-string
-                              (blob->string (f64vector->blob (f64vector x)))
-                              #f
-                              port))
-          ((integer? x) (write-string (blob->string
+    (cond ((exact-integer? x) (write-string (blob->string
                                        (u64vector->blob (u64vector x)))
                                       #f
                                       port))
+          ((real? x) (write-string
+                       (blob->string (f64vector->blob (f64vector x)))
+                       #f
+                       port))
           (else (abort "Expect a number"))))
   (call-with-output-file output-file
     (lambda (port)

+ 1 - 1
src/fvm.h

@@ -9,7 +9,7 @@ typedef double fvm_float_t;;
 struct fvm;
 
 // return non-zero if vm should be suspended
-typedef int64_t (*fvm_syscall_fn_t)(struct fvm* vm);
+typedef void (*fvm_syscall_fn_t)(struct fvm* vm);
 
 struct fvm {
   fvm_word_t sp;  // stack pointer

+ 7 - 0
src/main.c

@@ -3,12 +3,19 @@
 
 #include "fvm.h"
 
+static void printnum(struct fvm *vm) {
+  int64_t n = fvm_pop(vm);
+  printf("%lld\n", n);
+}
+
 int main(int argc, char **argv) {
   struct fvm vm;
   if (argc < 2) {
     fprintf(stderr, "Usage: fvm <program-file>\n");
     return -1;
   }
+  vm.syscall_table = malloc(256 * sizeof(fvm_syscall_fn_t));
+  vm.syscall_table[1] = &printnum;
   FILE* fp = fopen(argv[1], "rb");
   if (fp == NULL) {
     fprintf(stderr, "Failed to open file.\n");