-
Notifications
You must be signed in to change notification settings - Fork 13
/
compiler.js
529 lines (509 loc) · 14.2 KB
/
compiler.js
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
( // Module boilerplate to support browser globals, node.js and AMD.
(typeof module !== "undefined" && function (m) { module.exports = m(); }) ||
(typeof define === "function" && function (m) { define(m); }) ||
(function (m) { window.brozula = window.brozula || {}; window.brozula.compile = m(); })
)(function () {
"use strict";
// Generate a variable name for a stack slot
function slot(i) {
return "$" + i.toString(36);
}
function pr(i) {
return "fn" + i.toString(36);
}
function stateLabel(i) {
return '"' + i.toString(36) + '"';
}
function compile(protos, skipBuiltins) {
var code = [];
if (!skipBuiltins) {
code.push(builtins);
}
protos.forEach(function (proto, protoIndex) {
// console.error(proto);
// Scan for the blocks by looking for jump targets
var targets = {};
var needBlock = false;
for (var i = 0, l = proto.bcins.length; i < l; i++) {
var bc = proto.bcins[i];
var def = bcdef[bc.op];
Object.keys(def).forEach(function (key, j) {
if (def[key] !== "jump") return;
targets[bc.args[j]] = true;
needBlock = true;
});
}
// Form the function header
var line = "function " + pr(protoIndex) + "(";
for (var i = 0; i < proto.numparams; i++) {
if (i) line += ", ";
line += slot(i);
}
line += "){";
code.push(line);
// Reserve a slow for the var line
var varIndex = code.length;
code.push(null);
// Reserve the stack
var state = {
framesize: proto.framesize,
need$: false,
needthis: false
};
if (needBlock) {
code.push("var state=" + stateLabel(0) + ";");
code.push("for(;;){");
code.push("switch(state){");
targets[0] = true;
}
// Compile the opcodes
for (var i = 0, l = proto.bcins.length; i < l; i++) {
var bc = proto.bcins[i];
if (targets[i]) {
code.push("case " + stateLabel(i) + ":");
}
// console.error(bc);
var line = generators[bc.op].apply(state, bc.args);
if (conditionals[bc.op]) {
var next = proto.bcins[++i];
line += "{" + generators[next.op].apply(state, next.args) + "}"
}
// console.error(line);
code.push(line);
}
if (needBlock) {
code.push("}}");
}
var vars = [];
if (state.need$) vars.push("$");
for (var i = proto.numparams; i < proto.framesize; i++) {
vars.push(slot(i));
}
if (vars.length) {
code[varIndex] = "var " + vars.join(",") + ";";
}
else {
code[varIndex] = "";
}
code.push("}");
});
code.push("return " + pr(protos.length - 1) + ".apply(builtins, arguments);");
return code.join("\n");
}
var generators = {
ISLT: function (a, d) {
return "if(" + slot(a) + "<" + slot(d) + ")";
},
ISGE: function (a, d) {
return "if(" + slot(a) + ">" + slot(d) + ")";
},
ISLE: function (a, d) {
return "if(" + slot(a) + "<=" + slot(d) + ")";
},
ISGT: function (a, d) {
return "if(" + slot(a) + ">=" + slot(d) + ")";
},
ISEQV: function (a, d) {
return "if(" + slot(a) + "===" + slot(d) + ")";
},
ISNEV: function (a, d) {
return "if(" + slot(a) + "!==" + slot(d) + ")";
},
ISEQS: function (a, d) {
return "if(" + slot(a) + "===" + JSON.stringify(d) + ")";
},
ISNES: function (a, d) {
return "if(" + slot(a) + "!==" + JSON.stringify(d) + ")";
},
ISEQN: function (a, d) {
return "if(" + slot(a) + "===" + d + ")";
},
ISNEN: function (a, d) {
return "if(" + slot(a) + "!==" + d + ")";
},
ISEQP: function (a, d) {
return "if(" + slot(a) + "===" + JSON.stringify(d) + ")";
},
ISNEP: function (a, d) {
return "if(" + slot(a) + "!==" + JSON.stringify(d) + ")";
},
ISTC: function (a, d) {
return 'throw new Error("TODO: Implement ISTC");';
},
ISFC: function (a, d) {
return 'throw new Error("TODO: Implement ISFC");';
},
IST: function (d) {
return "if(!falsy(" + slot(d) + "))";
},
ISF: function (d) {
return "if(falsy(" + slot(d) + "))";
},
MOV: function (a, d) {
return slot(a) + "=" + slot(d) + ";";
},
NOT: function (a, d) {
return slot(a) + "=falsy(" + slot(d) + ");";
},
UNM: function (a, d) {
return slot(a) + "=-" + slot(d) + ";";
},
LEN: function (a, d) {
return slot(a) + "=length(" + slot(d) + ");";
},
ADDVN: function (a, b, c) {
return slot(a) + "=" + slot(b) + "+" + c + ";";
},
SUBVN: function (a, b, c) {
return slot(a) + "=" + slot(b) + "-" + c + ";";
},
MULVN: function (a, b, c) {
return slot(a) + "=" + slot(b) + "*" + c + ";";
},
DIVVN: function (a, b, c) {
return slot(a) + "=" + slot(b) + "/" + c + ";";
},
MODVN: function (a, b, c) {
return slot(a) + "=" + slot(b) + "%" + c + ";";
},
ADDNV: function (a, b, c) {
return slot(a) + "=" + slot(c) + "+" + b + ";";
},
SUBNV: function (a, b, c) {
return slot(a) + "=" + slot(c) + "-" + b + ";";
},
MULNV: function (a, b, c) {
return slot(a) + "=" + slot(c) + "*" + b + ";";
},
DIVNV: function (a, b, c) {
return slot(a) + "=" + slot(c) + "/" + b + ";";
},
MODNV: function (a, b, c) {
return slot(a) + "=" + slot(c) + "%" + b + ";";
},
ADDVV: function (a, b, c) {
return slot(a) + "=" + slot(b) + "+" + slot(c) + ";";
},
SUBVV: function (a, b, c) {
return slot(a) + "=" + slot(b) + "-" + slot(c) + ";";
},
MULVV: function (a, b, c) {
return slot(a) + "=" + slot(b) + "*" + slot(c) + ";";
},
DIVVV: function (a, b, c) {
return slot(a) + "=" + slot(b) + "/" + slot(c) + ";";
},
MODVV: function (a, b, c) {
return slot(a) + "=" + slot(b) + "%" + slot(c) + ";";
},
POW: function (a, b, c) {
return slot(a) + "=Math.pow(" + slot(b) + "," + slot(c) + ");";
},
CAT: function (a, b, c) {
var line = slot(a) + "=''";
for (var i = b; i <= c; i++) {
line += "+" + slot(i);
}
return line + ";";
},
KSTR: function (a, d) {
return slot(a) + "=" + JSON.stringify(d) + ";";
},
KCDATA: function (a, d) {
return 'throw new Error("TODO: Implement KCDATA");';
},
KSHORT: function (a, d) {
return slot(a) + "=" + d + ";";
},
KNUM: function (a, d) {
return slot(a) + "=" + d + ";";
},
KPRI: function (a, d) {
return slot(a) + "=" + JSON.stringify(d) + ";";
},
KNIL: function (a, d) {
var line = "";
for (var i = a; i <= d; i++) {
line += slot(i) + "=null;";
}
return line;
},
UGET: function (a, d) {
var local = d & 0x8000;
var immutable = d & 0x4000;
d = d & 0x3fff;
if (local) {
return slot(a) + "=this.__proto__.closure[" + d + "]";
}
return 'new Error("TODO: Implement UGET with non-local");';
},
USETV: function () {
return 'throw new Error("TODO: Implement USETV");';
},
USETS: function () {
return 'throw new Error("TODO: Implement USETS");';
},
USETN: function () {
return 'throw new Error("TODO: Implement USETN");';
},
USETP: function () {
return 'throw new Error("TODO: Implement USETP");';
},
UCLO: function (a, d) {
var items = [];
for (var i = a; i < this.framesize; i++) {
items.push(slot(i));
}
return "this.closure = [" + items.join(",") + "];state=" + stateLabel(d) + ";break;";
},
FNEW: function (a, d) {
return slot(a) + "=" + pr(d) + ".bind(Object.create(this));";
},
TNEW: function (a, d) {
var arrayn = d & 0x7ff;
var hashn = d >>> 11;
if (!arrayn || hashn) return slot(a) + "={};";
return slot(a) + "=new Array(" + arrayn + ");";
},
TDUP: function (a, d) {
// TODO: handle more table types properly
return slot(a) + "=" + JSON.stringify(d) + ";";
},
GGET: function (a, d) {
return slot(a) + "=index(this," + JSON.stringify(d) + ");";
},
GSET: function (a, d) {
return "newindex(this," + slot(a) + "," + JSON.stringify(d) + ");";
},
TGETV: function (a, b, c) {
return slot(a) + "=index(" + slot(b) + "," + slot(c) + ");";
},
TGETS: function (a, b, c) {
return slot(a) + "=index(" + slot(b) + "," + JSON.stringify(c) + ");";
},
TGETB: function (a, b, c) {
return slot(a) + "=index(" + slot(b) + "," + c + ");";
},
TSETV: function (a, b, c) {
return "newindex(" + slot(b) + "," + slot(c) + "," + slot(a) + ");";
},
TSETS: function (a, b, c) {
return "newindex(" + slot(b) + "," + JSON.stringify(c) + "," + slot(a) + ");";
},
TSETB: function (a, b, c) {
return "newindex(" + slot(b) + "," + c + "," + slot(a) + ");";
},
TSETM: function (a, d) {
var b = new Buffer(8);
b.writeDoubleLE(d, 0);
d = b.readUInt32LE(0);
return '$.forEach(function(v,i){' + slot(a-1) + '[i+' + d + '];});' +
'$=undefined;';
},
CALLM: function (a, b, c) {
var args;
if (c) {
args = [];
for (var i = a + 1; i <= a + c; i++) {
args.push(slot(i));
}
args = "[" + args.join(",") + "].concat($)";
}
else {
args = "$";
}
this.needthis = true;
var fn = "call(" + slot(a) + "," + args + ")";
if (b === 0) { // multires
this.need$ = true;
return "$=" + fn + ";";
}
if (b === 1) { // No return values
return fn + ";";
}
if (b === 2) { // one return value
return slot(a) + "=" + fn + "[0] || null;";
}
this.need$ = true;
var line = "$=" + fn + ";";
for (var i = 0; i < b - 1; i++) {
line += slot(a + i) + "=$[" + i + "];";
}
line += "$=undefined;";
return line;
},
CALL: function (a, b, c) {
var args = [];
for (var i = a + 1; i < a + c; i++) {
args.push(slot(i));
}
args = "[" + args.join(",") + "]";
this.needthis = true;
var fn = "call(" + slot(a) + "," + args + ")";
if (b === 0) { // multires
this.need$ = true;
return "$=" + fn + ";";
}
if (b === 1) { // No return values
return fn + ";";
}
if (b === 2) { // one return value
return slot(a) + "=" + fn + "[0] || null;";
}
this.need$ = true;
var line = "$=" + fn + ";";
for (var i = 0; i < b - 1; i++) {
line += slot(a + i) + "=$[" + i + "];";
}
line += "$=undefined;";
return line;
},
CALLMT: function (a) {
this.needthis = true;
return "return call(" + slot(a) + ",$);";
},
CALLT: function (a, d) {
var args = [];
for (var i = a + 1; i < a + d; i++) {
args.push(slot(i));
}
args = "[" + args.join(",") + "]";
this.needthis = true;
return "return call(" + slot(a) + "," + args + ");";
},
ITERC: function (a, b, c) {
var line =
slot(a) + "=" + slot(a - 3) + ";" +
slot(a + 1) + "=" + slot(a - 2) + ";" +
slot(a + 2) + "=" + slot(a - 1) + ";";
this.needthis = true;
var fn = "call(" + slot(a) + ",[" + slot(a + 1) + "," + slot(a + 2) + "])";
if (b === 0) { // multires
this.need$ = true;
return line + "$=" + fn + ";";
}
if (b === 1) { // No return values
return line + fn + ";";
}
if (b === 2) { // one return value
return line + slot(a) + "=" + fn + "[0] || null;";
}
this.need$ = true;
line += "$=" + fn + ";";
for (var i = 0; i <= b - 2; i++) {
line += slot(a + i) + "=$[" + i + "];";
}
line += "$=undefined;";
return line;
},
ITERN: function (a, b, c) {
var line =
slot(a) + "=" + slot(a - 3) + ";" +
slot(a + 1) + "=" + slot(a - 2) + ";" +
slot(a + 2) + "=" + slot(a - 1) + ";";
var fn = "next(" + slot(a - 2) + "," + slot(a - 1) + ")";
if (b === 0) { // multires
this.need$ = true;
return line + "$=" + fn + ";";
}
if (b === 1) { // No return values
return line + fn + ";";
}
if (b === 2) { // one return value
return line + slot(a) + "=" + fn + "[0] || null;";
}
this.need$ = true;
line += "$=" + fn + ";";
for (var i = 0; i < b - 1; i++) {
line += slot(a + i) + "=$[" + i + "];";
}
line += "$=undefined;";
return line;
},
VARG: function () {
return 'throw new Error("TODO: Implement VARG");';
},
ISNEXT: function (a, d) {
return 'if('+ slot(a - 3) + '===next&&rawType(' + slot(a - 2) + ')==="table"&&' + slot(a - 1) + '===null)' +
'{state=' + stateLabel(d) + ';break;}' +
'else{throw"TODO: Implement ISNEXT failure";}';
},
RETM: function () {
return 'throw new Error("TODO: Implement RETM");';
},
RET: function () {
return 'throw new Error("TODO: Implement RET");';
},
RET0: function () {
return "return[];";
},
RET1: function (a) {
return "return[" + slot(a) + "];"
},
FORI: function (a, d) {
return slot(a + 3) + "=" + slot(a) + ";" +
"var cmp=" + slot(a) + "<" + slot(a+1) + "?le:ge;";
},
JFORI: function () {
return 'throw new Error("TODO: Implement JFORI");';
},
FORL: function (a, d) {
return slot(a + 3) + "+=" + slot(a + 2) + ";" +
"if(cmp(" + slot(a + 3) + "," + slot(a + 1) + ")){" +
"state=" + stateLabel(d) + ";break;}" +
"else{cmp=undefined;}"
},
IFORL: function (a, d) {
return 'throw new Error("TODO: Implement IFORL");';
},
JFORL: function () {
return 'throw new Error("TODO: Implement JFORL");';
},
ITERL: function (a, d) {
return "if (" + slot(a) + "){" + slot(a-1) + "=" + slot(a) + ";state=" + stateLabel(d) + ";break;}";
},
IITERL: function () {
return 'throw new Error("TODO: Implement IITERL");';
},
JITERL: function () {
return 'throw new Error("TODO: Implement JITERL");';
},
LOOP: function () {
return ""; // tracing noop
},
ILOOP: function () {
return 'throw new Error("TODO: Implement ILOOP");';
},
JLOOP: function () {
return 'throw new Error("TODO: Implement JLOOP");';
},
JMP: function (a, d) {
return "state=" + stateLabel(d) + ";break;";
},
FUNCF: function () {
return 'throw new Error("TODO: Implement FUNCF");';
},
IFUNCF: function () {
return 'throw new Error("TODO: Implement IFUNCF");';
},
JFUNCF: function () {
return 'throw new Error("TODO: Implement JFUNCF");';
},
FUNCV: function () {
return 'throw new Error("TODO: Implement FUNCV");';
},
IFUNCV: function () {
return 'throw new Error("TODO: Implement IFUNCV");';
},
JFUNCV: function () {
return 'throw new Error("TODO: Implement JFUNCV");';
},
FUNCC: function () {
return 'throw new Error("TODO: Implement FUNCC");';
},
FUNCCW: function () {
return 'throw new Error("TODO: Implement FUNCCW");';
}
};
return compile;
});