1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "fvm.h"
static void print_num(struct fvm *vm) {
int64_t n = fvm_pop(vm);
printf("%" PRId64 "\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] = &print_num;
FILE* fp = fopen(argv[1], "rb");
if (fp == NULL) {
fprintf(stderr, "Failed to open file.\n");
return -1;
}
fseek(fp, 0, SEEK_END);
int64_t flen = ftell(fp);
char *code = malloc(flen);
fseek(fp, 0, SEEK_SET);
int n = fread(code, 1, flen, fp);
if (n != flen) {
fprintf(stderr, "Failed to read from file.\n");
exit(-1);
}
fvm_init(&vm, code, 2*1024*1024);
fvm_execute(&vm);
return vm.rv;
}
|