-
Notifications
You must be signed in to change notification settings - Fork 37
/
Bytecode2cpp.hx
307 lines (289 loc) · 9.32 KB
/
Bytecode2cpp.hx
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import sys.io.FileOutput;
class Bytecode2cpp {
public function new(code : CodeMemory, output : FileOutput, profileInfo : ProfileInfo) {
this.code = code;
this.output = output;
bytecode2code = new FlowArray();
line2pc = new FlowArray();
labels = new Map();
// We need address 0
labels.set(0, true);
needpc = new Map();
while (!code.eof()) {
compileInstruction();
}
output.writeString("#if COMPILED
#include \"ByteCodeRunner.h\"
#define RUNNER_RefArgs1(arg0) \\
const StackSlot &arg0 = GetStackSlotRef(0);
#define RUNNER_RefArgs2(arg1, arg0) \\
const StackSlot &arg1 = GetStackSlotRef(1); \\
const StackSlot &arg0 = GetStackSlotRef(0);
#define RUNNER_RefArgsRet1(rv_arg0) \\
StackSlot &rv_arg0 = *GetStackSlotPtr(0);
#define RUNNER_RefArgsRet2(rv_arg1, arg0) \\
StackSlot &rv_arg1 = *GetStackSlotPtr(1); \\
const StackSlot &arg0 = GetStackSlotRef(0);
#define RUNNER_CheckTag(tag, slot) \\
if (unlikely(slot.Type != tag)) { \\
ReportTagError(slot, tag, #slot, NULL); \\
}
");
output.writeString("void ByteCodeRunner::run() {\n");
// Convert from pc to code position
output.writeString("static void * targets [ " + code.size + " ]= {\n");
for (i in 0...code.size) {
var l = labels.get(i);
if (l == null || !profileInfo.alive(i)) {
output.writeString("0,");
} else {
output.writeString("&&pc" + i + ",");
}
if ((i % 64) == 63) {
output.writeString("\n");
}
}
output.writeString("};\n");
output.writeString("dispatch: FlowPtr p = Code.GetPosition(); void * target = targets[FlowPtrToInt(p)];\n");
output.writeString("if (unlikely(target == NULL)) { printf(\"Missing jump target for address %d\", FlowPtrToInt(p)); return; }");
output.writeString("\ngoto *target;\n");
for (l in 0...bytecode2code.length) {
var code = bytecode2code[l];
var pc = line2pc[l];
if (profileInfo.alive(pc)) {
var label = labels.get(pc);
if (label != null) {
output.writeString("pc" + pc + ": ");
}
var npc = needpc.get(pc);
if (npc != null) {
output.writeString("LastInstructionPtr = MakeFlowPtr(" + (pc) + ");\n");
output.writeString("Code.SetPosition(MakeFlowPtr(" + (npc) + "));\n");
}
output.writeString(code);
output.writeString("\n");
}
}
output.writeString("pc" + code.size + ": ;");
output.writeString("}\n");
output.writeString("FlowPtr ByteCodeRunner::getLastCodeAddress() { return MakeFlowPtr(" + code.size + "); }\n");
output.writeString("#endif\n");
}
var code : CodeMemory;
var output : FileOutput;
var bytecode2code : FlowArray<String>;
var line2pc : FlowArray<Int>;
var labels : Map<Int,Bool>;
var needpc : Map<Int,Int>;
function compileInstruction() : Void {
var pc = code.getPosition();
var opcode = code.readByte();
var s = switch (opcode) {
case Bytecode.CVoid: "PushVoid();";
case Bytecode.CBool: "Push(TBool, " + code.readByte() + ");";
case Bytecode.CInt: {
var v = code.readInt32();
'Push(TInt, int(' + v + '));';
}
case Bytecode.CDouble: 'PushDouble(' + code.readDouble() + ');';
case Bytecode.CString: {
var s = readString();
var r = "";
for (i in 0...s.length) {
var c = s.charAt(i);
if (c == "\\") {
c = "\\\\";
} else if (c == "\"") {
c = "\\\"";
} else if (c == "\n") {
c = "\\n";
} else if (c == "\t") {
c = "\\t";
}
r += c;
}
"Push(\"" + r + "\");";
}
case Bytecode.CArray: {
var len = code.readInt31();
"Push(TArray, MoveStackToHeap(" + len + ")); // ALLOC";
}
case Bytecode.CStruct: {
var id = code.readInt31();
"Push(StackSlot::Make(TStruct, MoveStackToHeap(StructDefs->GetObjectAtIndex(" + id + ")->FieldsCount), " + id + "));";
}
case Bytecode.CSetLocal: {
var slot = code.readInt32();
"Memory.SetStackSlot(framepointer + " + slot + "* STACK_SLOT_SIZE, PopStackSlot());";
}
case Bytecode.CGetLocal: {
var slot = code.readInt32();
"DoGetLocal(" + slot + ");";
};
case Bytecode.CGetGlobal: {
var global = code.readInt32();
"PushFromMemory(DataStackStart + " + global + " * STACK_SLOT_SIZE);";
};
case Bytecode.CReturn: {
needpc.set(pc, pc + 1);
"DoReturn(); goto dispatch;";
}
case Bytecode.CGoto: {
var offset = code.readInt31();
var target = pc + offset + 5;
labels.set(target, true);
"goto pc" + target + ";";
}
case Bytecode.CCodePointer: {
var offset = code.readInt31();
var target = pc + offset + 5;
labels.set(target, true);
"Push(TCodePointer, " + target + ");";
}
case Bytecode.CCall: {
needpc.set(pc, pc + 1);
labels.set(pc, true);
labels.set(pc + 1, true);
"DoCall(); goto dispatch;";
}
case Bytecode.CNotImplemented: "// " + readString();
case Bytecode.CIfFalse: {
var offset = code.readInt31();
var target = pc + offset + 5;
labels.set(target, true);
"{RUNNER_RefArgs1(flag);
RUNNER_CheckTag(TBool, flag);
DiscardStackSlots(1);
if (flag.IntValue == 0)
goto pc" + target + ";
}";
}
case Bytecode.CNot: "DoNot();";
case Bytecode.CNegate: "DoNegate();";
case Bytecode.CMultiply: "DoMultiply();";
case Bytecode.CDivide: "DoDivide();";
case Bytecode.CModulo: "DoModulo();";
case Bytecode.CPlus: "DoPlus();";
case Bytecode.CMinus: "DoMinus();";
case Bytecode.CEqual: "DoEqual();";
case Bytecode.CLessThan: "DoLessThan();";
case Bytecode.CLessEqual: "DoLessEqual();";
case Bytecode.CNativeFn: {
var args = code.readInt31();
var fn = readString();
"Push(AllocNativeFn(MakeNativeFunction(\"" + fn + "\", " + args + "), MakeFlowPtr(" + pc + ")));";
}
case Bytecode.CPop: {
"DiscardStackSlots(1);";
}
case Bytecode.CArrayGet: {
"DoArrayGet();";
}
case Bytecode.CReserveLocals: {
var l = code.readInt32();
var v = code.readInt32();
// TODO: Not sure this is correct
"sp += " + l + " * STACK_SLOT_SIZE;" +
"framepointer -= " + v + " * STACK_SLOT_SIZE;";
}
case Bytecode.CRefTo: "DoRefTo();";
case Bytecode.CDeref: "DoDeref();";
case Bytecode.CSetRef: "DoSetRef();";
case Bytecode.CInt2Double: "DoInt2Double();";
case Bytecode.CInt2String: "DoInt2String();";
case Bytecode.CDouble2Int: "DoDouble2Int();";
case Bytecode.CDouble2String: "DoDouble2String();";
case Bytecode.CField: {
var f = code.readInt31();
"DoField(" + f + ");";
}
case Bytecode.CFieldName: {
var name = readString();
"DoFieldName(\"" + name + "\");";
}
case Bytecode.CStructDef: {
var id = code.readInt31();
var name = readString();
var n = code.readInt31();
var sdid = "sd" + id;
var r = "StructDef " + sdid + ";";
r += sdid + ".Name = \"" + name + "\";";
r += sdid + ".FieldsCount = " + n + ";";
r += sdid + ".FieldNames = new PCHAR[" + n + "];";
for (i in 0...n) {
var field = readString();
r += sdid + ".FieldNames[" + i + "] = \"" + field + "\";";
}
r += "StructDefs->SetObjectAtIndex(" + id + ", " + sdid + ");";
r += "StructNameIds.insert(\"" + name + "\", " + id + ");";
r;
}
case Bytecode.CGetFreeVar: {
var n = code.readInt31();
"PushFromMemory(closurepointer + " + n + " * STACK_SLOT_SIZE);";
}
case Bytecode.CDebugInfo: "// " + readString();
case Bytecode.CClosureReturn: {
needpc.set(pc, pc + 1);
"closurepointer = ClosureStackPop(); DoReturn(); goto dispatch;";
}
case Bytecode.CClosurePointer: {
var n = code.readInt31();
var offset = code.readInt31();
var target = pc + 9 + offset;
labels.set(target, true);
"Push(StackSlot::Make(TClosurePointer, MoveStackToHeap(" + n + "), " + target + "));";
}
case Bytecode.CSwitch: {
var n = code.readInt31();
var end = code.readInt31();
var pos = pc + 9 + n * 8;
var r = "{RUNNER_RefArgsRet1(struct_ref);
RUNNER_CheckTag(TStruct, struct_ref);
int structId = struct_ref.IntValue2;
// In the default case, we just eat the struct value
DiscardStackSlots(1);";
for (i in 0...n) {
var c = code.readInt31();
var offset = code.readInt31();
var add = pos + offset;
labels.set(add, true);
r += "if (structId == " + c + ") {
int len = Memory.GetInt32(struct_ref.PtrValue);
MoveHeapToStack(struct_ref.PtrValue+4, len); // struct_ref overwritten
goto pc" + add + ";
}";
}
r + "}";
}
case Bytecode.CSimpleSwitch: {
// TODO: implement similar to CSwitch, but without any copying of fields in a case. Ask Tommy.
"Bytecode.CSimpleSwitch unimplemented";
}
case Bytecode.CUncaughtSwitch: {
"ReportError(UncaughtSwitch, \"Unexpected case in switch.\");";
}
case Bytecode.CTailCall: {
needpc.set(pc, pc + 5);
var locals = code.readInt32();
labels.set(pc, true);
labels.set(pc + 5, true);
"DoTailCall(" + locals + "); goto dispatch;";
}
case Bytecode.CLast: {
needpc.set(pc, pc + 1);
labels.set(pc + 1, true);
"return;";
}
#if cpp
default: "?";
#end
}
bytecode2code.push(s);
line2pc.push(pc);
}
function readString() : String {
var l = code.readInt31();
return code.readString(l);
}
}