forked from ivpravdin/MorphoIt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.cpp
More file actions
172 lines (142 loc) · 4.77 KB
/
compile.cpp
File metadata and controls
172 lines (142 loc) · 4.77 KB
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
// Include the headers
#include "blocks/c_code_generator.h"
#include "builder/static_var.h"
#include "builder/dyn_var.h"
#include <iostream>
extern "C"
{
// Hackily include bits of morpho we need
#include "morpho.h"
#define cmplx_h // Suppress headers that don't play nice with C++
#define platform_h //
#include "program.h"
#include "varray.h" // Define a missing type that's protected from casual use
typedef unsigned int instruction;
DECLARE_VARRAY(instruction, instruction);
// Prototypes for a couple of
varray_instruction *program_getbytecode(program *p);
objectfunction *program_getglobalfn(program *p);
}
// Include the BuildIt types
using builder::dyn_var;
using builder::static_var;
using namespace std;
/** Decode the opcode */
#define DECODE_OP(x) (x & 0xff)
/** Decode operand A */
#define DECODE_A(x) ((x>>8) & (0xff))
/** Decode operand B */
#define DECODE_B(x) ((x>>16) & (0xff))
/** Decode operand C */
#define DECODE_C(x) ((x>>24) & (0xff))
/** Decode long operand Bx */
#define DECODE_Bx(x) ((x>>16) & (0xffff))
/** Decode signed long operand Bx */
#define DECODE_sBx(x) ((short) ((x>>16) & (0xffff)))
dyn_var<void(int)> print(builder::as_global("print"));
static void print_wrapper_code(std::ostream &oss) {
oss << "#include <stdio.h>\n";
oss << "#include <stdlib.h>\n";
oss << "#include <stdbool.h>\n";
oss << "void print(int x) {printf(\"%d\\n\", x);}\n";
}
static dyn_var<int> morpho_vm(const int n, const uint32_t bytecode[], const uint32_t consts[]) {
//static_var<uint32_t> consts[] = {0, 1, 30000};
dyn_var<int32_t[255]> reg = {0};
dyn_var<int32_t[255]> globals = {0};
dyn_var<int32_t> left, right;
static_var<int32_t> a, b, c;
static_var<int32_t> bc;
static_var<int32_t> pc = 0;
while (pc < n) {
bc = bytecode[pc];
switch (DECODE_OP(bc)) {
case 0x0: // NOP
break;
case 0x1: // MOV
a=DECODE_A(bc); b=DECODE_B(bc);
reg[a] = reg[b];
break;
case 0x2: // LCT
a=DECODE_A(bc); b=DECODE_Bx(bc);
reg[a] = consts[b];
break;
case 0x3: // ADD
a=DECODE_A(bc); b=DECODE_B(bc); c=DECODE_C(bc);
reg[a] = reg[b] + reg[c];
break;
case 0xA: //LT
a=DECODE_A(bc); b=DECODE_B(bc); c=DECODE_C(bc);
left = reg[b];
right = reg[c];
reg[a] = (left<right);
break;
case 0xD: // B
b=DECODE_sBx(bc);
pc+=b;
break;
case 0xF: // BIFF
a=DECODE_A(bc);
left=reg[a];
if (!left) pc+=DECODE_sBx(bc);
break;
case 0x1D: // LGL
a=DECODE_A(bc);
b=DECODE_Bx(bc);
reg[a]=globals[b];
break;
case 0x1E: // SGL
a=DECODE_A(bc);
b=DECODE_Bx(bc);
globals[b]=reg[a];
break;
case 0x22: // PRINT
a=DECODE_A(bc);
left=reg[a];
print(left);
break;
case 0x24: // END
return 0;
default:
return 1;
}
pc++;
}
return 1;
}
int main(int argc, char* argv[]) {
builder::builder_context context;
morpho_initialize();
program *p = morpho_newprogram();
compiler *c = morpho_newcompiler(p);
error err;
error_init(&err);
char src[] = "for (var i=0; i<5; i+=1) print i";
if (morpho_compile(src, c, false, &err)) {
varray_instruction *code = program_getbytecode(p);
objectfunction *globalfn = program_getglobalfn(p);
uint32_t *bytecode = (uint32_t *) code->data;
int ninstructions = code->count;
int nconst = globalfn->konst.count;
uint32_t consts[255]; // Must be static in length
for (int i=0; i<nconst; i++) {
if (MORPHO_ISINTEGER(globalfn->konst.data[i])) {
consts[i]=MORPHO_GETINTEGERVALUE(globalfn->konst.data[i]);
} else {
consts[i]=0;
printf("Warning: constant %i '", i);
morpho_printvalue(NULL, globalfn->konst.data[i]);
printf("' will be ignored.\n");
}
}
auto ast = context.extract_function_ast(morpho_vm, "main", ninstructions, bytecode, consts);
print_wrapper_code(std::cout);
block::c_code_generator::generate_code(ast, std::cout, 0);
} else {
printf("Compilation error [%s]: %s\n", err.id, err.msg);
}
morpho_freeprogram(p);
morpho_freecompiler(c);
morpho_finalize();
return 0;
}