-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
278 lines (230 loc) · 7.39 KB
/
main.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "b9.h"
constexpr ByteCode
getByteCodeFromInstruction(Instruction instruction)
{
return (instruction >> 16);
}
constexpr Parameter
getParameterFromInstruction(Instruction instruction)
{
return (instruction & 0xFFFF);
}
constexpr Instruction
createInstruction(ByteCode byteCode, Parameter parameter)
{
return byteCode << 16 | (parameter & 0xFFFF);
}
#define decl(argCount, tmpCount) (argCount << 16 | tmpCount)
#define progArgCount(a) (a >> 16)
#define progTmpCount(a) (a & 0xFFFF)
static Instruction fib_function[] = {
// one argument, 0 temps
decl(1, 0),
// if (arg1 < 3) return 1;
// so converted to jump if 3 <= arg1
createInstruction(PUSH_CONSTANT, 3),
createInstruction(PUSH_FROM_VAR, 0),
createInstruction(JMPLE, 2), // SKIP 2 instructions
// return 1;
createInstruction(PUSH_CONSTANT, 1), // 1
createInstruction(RETURN, 0), // 2
// return fib (n-1); + fib (n-2);
createInstruction(PUSH_FROM_VAR, 0),
createInstruction(PUSH_CONSTANT, 1),
createInstruction(SUB, 0),
createInstruction(CALL, 1), // fib (n-1)
createInstruction(PUSH_FROM_VAR, 0),
createInstruction(PUSH_CONSTANT, 2),
createInstruction(SUB, 0),
createInstruction(CALL, 1), // fib (n-2)
createInstruction(ADD, 0),
createInstruction(RETURN, 0),
createInstruction(NO_MORE_BYTECODES, 0)
};
static Instruction main_function[] = {
decl(0, 0),
createInstruction(PUSH_CONSTANT, 12),
createInstruction(CALL, 1),
createInstruction(RETURN, 0),
createInstruction(NO_MORE_BYTECODES, 0)
};
static Instruction loop_call_fib12_function[] = {
decl(1, 1),
createInstruction(PUSH_FROM_VAR, 0),
createInstruction(POP_INTO_VAR, 1), // t = 50000
// if (t <= 0) jmp exit
// loop test
createInstruction(PUSH_FROM_VAR, 1),
createInstruction(PUSH_CONSTANT, 0),
createInstruction(JMPLE, 8), // SKIP to past the JMP
createInstruction(PUSH_CONSTANT, 2), // 1
createInstruction(CALL, 1), // 2
createInstruction(DROP, 0), // 3
// t--;
createInstruction(PUSH_FROM_VAR, 1), // 4
createInstruction(PUSH_CONSTANT, 1), // 5
createInstruction(SUB, 0), // 6
createInstruction(POP_INTO_VAR, 1), // 7
createInstruction(JMP, -11), // 8
// exit
createInstruction(PUSH_CONSTANT, 999),
createInstruction(RETURN, 0),
createInstruction(NO_MORE_BYTECODES, 0)
};
/* Byte Code Program */
static Instruction* functions[] = {
main_function,
fib_function
};
/* Byte Code Implementations */
void push(ExecutionContext* context, uint16_t value)
{
*context->stackPointer = value;
++(context->stackPointer);
}
uint16_t
pop(ExecutionContext* context)
{
return *(--context->stackPointer);
}
void bc_call(ExecutionContext* context, uint16_t value)
{
Instruction* program = functions[value];
// printf("inside call\n");
uint16_t result = interpret(context, program);
// printf("return from call %d\n", result);
push(context, result);
}
void bc_push_from_arg(ExecutionContext* context, uint16_t* args, uint16_t offset)
{
// printf("bc_push_from_arg[%d] %d\n", offset, args[offset]);
push(context, args[offset]);
}
void bc_pop_into_arg(ExecutionContext* context, uint16_t* args, uint16_t offset)
{
args[offset] = pop(context);
// printf("bc_pop_into_arg[%d] %d\n", offset, args[offset]);
}
void bc_drop(ExecutionContext* context)
{
pop(context);
}
void bc_push_constant(ExecutionContext* context, uint16_t value)
{
// printf("bc_push_constant %d\n", value);
push(context, value);
}
uint16_t
bc_jmple(ExecutionContext* context, uint16_t delta)
{
// push(left); push(right); if (left <= right) jmp
int16_t right = pop(context);
int16_t left = pop(context);
// printf("jmple left %d, right %d\n", left, right);
if (left <= right) {
return delta;
}
return 0;
}
void bc_add(ExecutionContext* context)
{
// a+b is push(a);push(b); add
uint16_t right = pop(context);
uint16_t left = pop(context);
printf("add right %d operandB %d\n", right, left);
uint16_t result = left + right;
push(context, result);
}
void bc_sub(ExecutionContext* context)
{
// left-right is push(left);push(right); sub
uint16_t right = pop(context);
uint16_t left = pop(context);
uint16_t result = left - right;
push(context, result);
}
/* ByteCode Interpreter */
uint16_t interpret(ExecutionContext* context, Instruction* program)
{
int nargs = progArgCount(*program);
int tmps = progTmpCount(*program);
//printf("Prog Arg Count %d, tmp count %d\n", nargs, tmps);
Instruction* instructionPointer = program + 1;
uint16_t* args = context->stackPointer - nargs;
context->stackPointer += tmps; // local storage for temps
while (*instructionPointer != NO_MORE_BYTECODES) {
// uint16_t* base = context->stack;
// printf("------\n");
// while (base < context->stackPointer) {
// printf("Stack[%ld] = %d\n", base - context->stack, *base);
// base++;
// }
// printf("^^^^^^^^^^^^^^^^^\n");
//printf("about to run %d %d\n", getByteCodeFromInstruction(*instructionPointer), getParameterFromInstruction(*instructionPointer));
switch (getByteCodeFromInstruction(*instructionPointer)) {
case PUSH_CONSTANT:
// printf("push\n");
bc_push_constant(context, getParameterFromInstruction(*instructionPointer));
break;
case DROP:
// printf("drop\n");
bc_drop(context);
break;
case ADD:
// printf("add\n");
bc_add(context);
break;
case SUB:
// printf("sub\n");
bc_sub(context);
break;
case JMPLE:
// printf("jmple\n");
instructionPointer += bc_jmple(context, getParameterFromInstruction(*instructionPointer));
break;
case CALL:
// printf("call\n");
bc_call(context, getParameterFromInstruction(*instructionPointer));
break;
case PUSH_FROM_VAR:
// printf("push_from_var\n");
bc_push_from_arg(context, args, getParameterFromInstruction(*instructionPointer));
break;
case POP_INTO_VAR:
// printf("pop_into_var\n");
bc_pop_into_arg(context, args, getParameterFromInstruction(*instructionPointer));
break;
case JMP:
// printf("jumping %d\n", getParameterFromInstruction(*instructionPointer));
instructionPointer += getParameterFromInstruction(*instructionPointer);
break;
case RETURN:
// printf("return\n");
/* bc_return */
int16_t result = *(context->stackPointer - 1);
context->stackPointer = args;
return result;
break;
}
instructionPointer++;
}
return *(context->stackPointer - 1);
}
/* Main Loop */
int main()
{
ExecutionContext context;
uint16_t result = 0;
// run main, which just calls fib 12
// result = interpret(&context, main_function);
// printf("Program result is: %d\n", result);
// run a hard loop of fib(12)
int count = 1000;
while (count--) {
push (&context, 10000);
result = interpret(&context, loop_call_fib12_function);
//context.stackPointer = context.stack;
}
printf("loop_call_fib12_function result is: %d\n", result);
return 0;
}