-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvmdemo.c
191 lines (137 loc) · 2.9 KB
/
tvmdemo.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* factorial and man-or-boy-test for tiny vm
*
gcc -std=gnu11 -Wall -O2 -otvmdemo tvmdemo.c tinyvm/asm.c tinyvm/cpu.c
*
* Author: Martin Uecker <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "tinyvm/cpu.h"
#include "tinyvm/asm.h"
void prelude(ins2_t** p, unsigned int start)
{
cnst(p, C0, 0);
cnst(p, C1, 1);
cnst(p, SP, 32); // stack starts at 32
cnst(p, FP, 32);
push(p, RR);
cnst(p, 10, start); // jump to start
call(p, 1, 10);
stop(p);
}
void factorial(ins2_t** p, unsigned int start)
{
ins2_t* base = here(p);
ins2_t* f = here(p);
enter(p, 0);
arg(p, U1, 0);
sub(p, U1, U1, C1);
ins2_t* j = here(p);
cjmp(p, U1, -1);
leave(p, C1);
from(p, j);
push(p, U1);
cnst(p, U2, start + (f - base));
call(p, 1, U2);
arg(p, U2, 0);
mul(p, U2, U2, RR);
leave(p, U2);
}
void manorboy(ins2_t** p, unsigned int start)
{
ins2_t* base = here(p);
ins2_t* j = here(p);
jump(p, -1);
ins2_t* po = here(p);
move(p, RR, C1);
ret(p);
ins2_t* mo = here(p);
sub(p, RR, C0, C1);
ret(p);
ins2_t* zo = here(p);
move(p, RR, C0);
ret(p);
ins2_t* b = here(p);
enter(p, 0);
// move(p, U2, FP); // save frame pointer
// -- we don't need to because we restore SP directly
fp(p, 1); // get stack frame
load(p, A1, 0);
sub(p, A1, A1, C1);
store(p, 0, A1);
int t = tramp(p, A2, start + (b - base));
// prepare call to manorboy
load(p, A3, 1);
load(p, A4, 2);
load(p, U1, 3);
push(p, U1);
arg(p, U1, 1);
push(p, U1);
// move(p, FP, U2); // restore frame pointer
ins2_t* l = here(p);
cnst(p, U1, -1);
call(p, 2, U1);
//leave(p, RR);
cnst(p, AR, t);
sub(p, SP, SP, AR);
pop(p, FP);
ret(p);
ins2_t* mob = here(p);
cnst(&l, U1, start + (mob - base));
// store first arguments as local variables
enter(p, 4);
store(p, 0, A1);
store(p, 1, A2);
store(p, 2, A3);
store(p, 3, A4);
sub(p, U1, C0, A1);
cnst(p, U2, (1u << 31));
and(p, U1, U1, U2);
ins2_t* d = here(p);
cjmp(p, U1, -1);
// k <= 0
arg(p, U1, 1);
call(p, 0, U1);
push(p, RR);
arg(p, U1, 0);
call(p, 0, U1);
pop(p, U2);
add(p, RR, U2, RR);
leave(p, RR);
from(p, d); // k > 0
cnst(p, U1, start + (b - base));
call2(p, 0, U1);
leave(p, RR);
from(p, j);
cnst(p, A1, 10);
cnst(p, A2, start + (po - base));
cnst(p, A3, start + (mo - base));
cnst(p, A4, start + (mo - base));
cnst(p, U1, start + (po - base));
push(p, U1);
cnst(p, U1, start + (zo - base));
push(p, U1);
cnst(p, U2, start + (mob - base)); // manorboy
call(p, 2, U2);
ret(p);
}
int main(int argc, char** argv)
{
ins2_t* mm = malloc(100000 * sizeof(ins2_t));
ins2_t* p = &mm[0];
unsigned int start = 50000;
prelude(&p, start);
p = &mm[start];
factorial(&p, start);
//manorboy(&p, start);
printf("%ld words.\n", p - &mm[start]);
reg_t reg[256] = { [0 ... 255] = 0 };
reg[0] = 0;
reg[RR] = 7;
vm(reg, (mem_t*)mm);
assert(32 == reg[SP]);
printf("Result: %d\n", reg[RR]);
return 0;
}