-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunvm.c
executable file
·92 lines (77 loc) · 1.63 KB
/
runvm.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "vmenkel.h"
// enough for the samples?
int VARS = 8192;
int ARGS = 2048;
int ARRAYS = 4096;
int LOCALS = 400;
int MAXPROGLEN = 32768;
int* program;
void allocateprogram() {
program = (int*) malloc(MAXPROGLEN * sizeof(int));
}
long fsize(FILE* file) {
fseek(file, 0L, SEEK_END);
long size = ftell(file);
rewind(file);
return size;
}
char* read(char *path) {
FILE* file;
file = fopen(path, "rb");
long size = fsize(file);
char* buf = (char*) calloc(1, size + 1);
fread(buf, size, 1, file);
fclose(file);
return buf;
}
void exec(int* code, int start) {
VM* vm = newVM(code, start, VARS, ARGS, ARRAYS, LOCALS);
if (vm != NULL) {
run(vm);
freeVM(vm);
}
}
int main(int argc, char *argv[]) {
printf("loading ..\n");
// get the "binary" file
char* source = read(argv[1]);
allocateprogram();
// parse numbers separated by comma
const char s[2] = ",";
char *token;
token = strtok(source, s);
// header
int start = atoi(token);
// body
int i = 0;
token = strtok(NULL, s);
while (token != NULL) {
program[i] = atoi(token);
token = strtok(NULL, s);
i++;
}
// print loaded prog (change \r to \n)
printf("%d:\r", start);
int j = 0;
do {
printf("%d\r", program[j]);
j++;
}
while (j < i);
printf("running ..\n");
printf("- - - - - - - - - - - -\n");
clock_t t;
t = clock();
exec(program, start);
t = clock() - t;
printf("- - - - - - - - - - - -\n");
double duration = ((double) t) / CLOCKS_PER_SEC;
printf("duration %f seconds\n", duration);
printf("done running.\n");
return 0;
}
/* EOF */