-
Notifications
You must be signed in to change notification settings - Fork 77
/
jit.js
1169 lines (1142 loc) · 57.3 KB
/
jit.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
/*
* Copyright (c) 2014-2024 Vanessa Freudenberg
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Object.subclass('Squeak.Compiler',
/****************************************************************************
VM and Compiler
===============
The VM has an interpreter, it will work fine (and much more memory-efficient)
without loading a compiler. The compiler plugs into the VM by providing the
Squeak.Compiler global. It can be easily replaced by just loading a different
script providing Squeak.Compiler.
The VM creates the compiler instance after an image has been loaded and the VM
been initialized. Whenever a method is activated that was not compiled yet, the
compiler gets a chance to compile it. The compiler may decide to wait for a couple
of activations before actually compiling it. This might prevent do-its from ever
getting compiled, because they are only activated once. Therefore, the compiler
is also called when a long-running non-optimized loop calls checkForInterrupts.
Finally, whenever the interpreter is about to execute a bytecode, it calls the
compiled method instead (which typically will execute many bytecodes):
initialize:
compiler = new Squeak.Compiler(vm);
executeNewMethod, checkForInterrupts:
if (!method.compiled && compiler)
compiler.compile(method);
interpret:
if (method.compiled) method.compiled(vm);
Note that a compiler could hook itself into a compiled method by dispatching
to vm.compiler in the generated code. This would allow gathering statistics,
recompiling with optimized code etc.
About This Compiler
===================
The compiler in this file is meant to be simple, fast-compiling, and general.
It transcribes bytecodes 1-to-1 into equivalent JavaScript code using
templates (and thus can even support single-stepping). It uses the
interpreter's stack pointer (SP) and program counter (PC), actual context
objects just like the interpreter, no register mapping, it does not optimize
sends, etc.
Jumps are handled by wrapping the whole method in a loop and switch. This also
enables continuing in the middle of a compiled method: whenever another context
is activated, the method returns to the main loop, and is entered again later
with a different PC. Here is an example method, its bytecodes, and a simplified
version of the generated JavaScript code:
method
[value selector] whileFalse.
^ 42
0 <00> pushInstVar: 0
1 <D0> send: #selector
2 <A8 02> jumpIfTrue: 6
4 <A3 FA> jumpTo: 0
6 <21> pushConst: 42
7 <7C> return: topOfStack
context = vm.activeContext
while (true) switch (vm.pc) {
case 0:
stack[++vm.sp] = inst[0];
vm.pc = 2; vm.send(#selector); // activate new method
return; // return to main loop
// Main loop will execute the activated method. When
// that method returns, this method will be called
// again with vm.pc == 2 and jump directly to case 2
case 2:
if (stack[vm.sp--] === vm.trueObj) {
vm.pc = 6;
continue; // jump to case 6
}
// otherwise fall through to next case
case 4:
vm.pc = 0;
continue; // jump to case 0
case 6:
stack[++vm.sp] = 42;
vm.pc = 7; vm.doReturn(stack[vm.sp]);
return;
}
Debugging support
=================
This compiler supports generating single-stepping code and comments, which are
rather helpful during debugging.
Normally, only bytecodes that can be a jump target are given a label. Also,
bytecodes following a send operation need a label, to enable returning to that
spot after the context switch. All other bytecodes are executed continuously.
When compiling for single-stepping, each bytecode gets a label, and after each
bytecode a flag is checked and the method returns if needed. Because this is
a performance penalty, methods are first compiled without single-step support,
and recompiled for single-stepping on demand.
This is optional, another compiler can answer false from enableSingleStepping().
In that case the VM will delete the compiled method and invoke the interpreter
to single-step.
*****************************************************************************/
'initialization', {
initialize: function(vm) {
this.vm = vm;
this.comments = !!Squeak.Compiler.comments, // generate comments
// for debug-printing only
this.specialSelectors = ['+', '-', '<', '>', '<=', '>=', '=', '~=', '*', '/', '\\\\', '@',
'bitShift:', '//', 'bitAnd:', 'bitOr:', 'at:', 'at:put:', 'size', 'next', 'nextPut:',
'atEnd', '==', 'class', 'blockCopy:', 'value', 'value:', 'do:', 'new', 'new:', 'x', 'y'];
this.doitCounter = 0;
this.blockCounter = 0;
},
},
'accessing', {
compile: function(method, optClassObj, optSelObj) {
if (method.compiled === undefined) {
// 1st time
method.compiled = false;
} else {
// 2nd time
this.singleStep = false;
this.debug = this.comments;
var clsName, sel, instVars;
if (this.debug && !optClassObj) {
// this is expensive, so only do it when debugging
var isMethod = method.sqClass === this.vm.specialObjects[Squeak.splOb_ClassCompiledMethod];
this.vm.allMethodsDo(function(classObj, methodObj, selectorObj) {
if (isMethod ? methodObj === method : methodObj.pointers.includes(method)) {
optClassObj = classObj;
optSelObj = selectorObj;
return true;
}
});
}
if (optClassObj) {
clsName = optClassObj.className();
sel = optSelObj.bytesAsString();
if (this.debug) {
// only when debugging
var isMethod = method.sqClass === this.vm.specialObjects[Squeak.splOb_ClassCompiledMethod];
if (!isMethod) {
clsName = "[] in " + clsName;
}
instVars = optClassObj.allInstVarNames();
}
}
method.compiled = this.generate(method, clsName, sel, instVars);
}
},
enableSingleStepping: function(method, optClass, optSel) {
// recompile method for single-stepping
if (!method.compiled || !method.compiled.canSingleStep) {
this.singleStep = true; // generate breakpoint support
this.debug = true;
if (!optClass) {
this.vm.allMethodsDo(function(classObj, methodObj, selectorObj) {
if (methodObj === method) {
optClass = classObj;
optSel = selectorObj;
return true;
}
});
}
var cls = optClass && optClass.className();
var sel = optSel && optSel.bytesAsString();
var instVars = optClass && optClass.allInstVarNames();
method.compiled = this.generate(method, cls, sel, instVars);
method.compiled.canSingleStep = true;
}
// if a compiler does not support single-stepping, return false
return true;
},
functionNameFor: function(cls, sel) {
if (cls === undefined || cls === '?') {
var isMethod = this.method.sqClass === this.vm.specialObjects[Squeak.splOb_ClassCompiledMethod];
return isMethod ? "DOIT_" + ++this.doitCounter : "BLOCK_" + ++this.blockCounter;
}
cls = cls.replace(/ /g, "_").replace("[]", "Block");
if (!/[^a-zA-Z0-9:_]/.test(sel))
return cls + "_" + sel.replace(/:/g, "ː"); // unicode colon is valid in JS identifiers
var op = sel.replace(/./g, function(char) {
var repl = {'|': "OR", '~': "NOT", '<': "LT", '=': "EQ", '>': "GT",
'&': "AND", '@': "AT", '*': "TIMES", '+': "PLUS", '\\': "MOD",
'-': "MINUS", ',': "COMMA", '/': "DIV", '?': "IF"}[char];
return repl || 'OPERATOR';
});
return cls + "__" + op + "__";
},
},
'generating', {
generate: function(method, optClass, optSel, optInstVarNames) {
this.method = method;
this.sista = method.methodSignFlag();
this.pc = 0; // next bytecode
this.endPC = 0; // pc of furthest jump target
this.prevPC = 0; // pc at start of current instruction
this.source = []; // snippets will be joined in the end
this.sourceLabels = {}; // source pos of generated jump labels
this.needsLabel = {}; // jump targets
this.sourcePos = {}; // source pos of optional vars / statements
this.needsVar = {}; // true if var was used
this.needsBreak = false; // insert break check for previous bytecode
if (optClass && optSel)
this.source.push("// ", optClass, ">>", optSel, "\n");
this.instVarNames = optInstVarNames;
this.allVars = ['context', 'stack', 'rcvr', 'inst[', 'temp[', 'lit['];
this.sourcePos['context'] = this.source.length; this.source.push("var context = vm.activeContext;\n");
this.sourcePos['stack'] = this.source.length; this.source.push("var stack = context.pointers;\n");
this.sourcePos['rcvr'] = this.source.length; this.source.push("var rcvr = vm.receiver;\n");
this.sourcePos['inst['] = this.source.length; this.source.push("var inst = rcvr.pointers;\n");
this.sourcePos['temp['] = this.source.length; this.source.push("var temp = vm.homeContext.pointers;\n");
this.sourcePos['lit['] = this.source.length; this.source.push("var lit = vm.method.pointers;\n");
this.sourcePos['loop-start'] = this.source.length; this.source.push("while (true) switch (vm.pc) {\ncase 0:\n");
if (this.sista) this.generateSista(method);
else this.generateV3(method);
var funcName = this.functionNameFor(optClass, optSel);
if (this.singleStep) {
if (this.debug) this.source.push("// all valid PCs have a label;\n");
this.source.push("default: throw Error('invalid PC');\n}"); // all PCs handled
} else {
this.sourcePos['loop-end'] = this.source.length; this.source.push("default: vm.interpretOne(true); return;\n}");
this.deleteUnneededLabels();
}
this.deleteUnneededVariables();
var source = "'use strict';\nreturn function " + funcName + "(vm) {\n" + this.source.join("") + "}";
return new Function(source)();
},
generateV3: function(method) {
this.done = false;
while (!this.done) {
var byte = method.bytes[this.pc++],
byte2 = 0;
switch (byte & 0xF8) {
// load inst var
case 0x00: case 0x08:
this.generatePush("inst[", byte & 0x0F, "]");
break;
// load temp var
case 0x10: case 0x18:
this.generatePush("temp[", 6 + (byte & 0xF), "]");
break;
// loadLiteral
case 0x20: case 0x28: case 0x30: case 0x38:
this.generatePush("lit[", 1 + (byte & 0x1F), "]");
break;
// loadLiteralIndirect
case 0x40: case 0x48: case 0x50: case 0x58:
this.generatePush("lit[", 1 + (byte & 0x1F), "].pointers[1]");
break;
// storeAndPop inst var
case 0x60:
this.generatePopInto("inst[", byte & 0x07, "]");
break;
// storeAndPop temp var
case 0x68:
this.generatePopInto("temp[", 6 + (byte & 0x07), "]");
break;
// Quick push
case 0x70:
switch (byte) {
case 0x70: this.generatePush("rcvr"); break;
case 0x71: this.generatePush("vm.trueObj"); break;
case 0x72: this.generatePush("vm.falseObj"); break;
case 0x73: this.generatePush("vm.nilObj"); break;
case 0x74: this.generatePush("-1"); break;
case 0x75: this.generatePush("0"); break;
case 0x76: this.generatePush("1"); break;
case 0x77: this.generatePush("2"); break;
}
break;
// Quick return
case 0x78:
switch (byte) {
case 0x78: this.generateReturn("rcvr"); break;
case 0x79: this.generateReturn("vm.trueObj"); break;
case 0x7A: this.generateReturn("vm.falseObj"); break;
case 0x7B: this.generateReturn("vm.nilObj"); break;
case 0x7C: this.generateReturn("stack[vm.sp]"); break;
case 0x7D: this.generateBlockReturn(); break;
default: throw Error("unusedBytecode " + byte);
}
break;
// Extended bytecodes
case 0x80: case 0x88:
this.generateV3Extended(byte);
break;
// short jump
case 0x90:
this.generateJump((byte & 0x07) + 1);
break;
// short conditional jump
case 0x98:
this.generateJumpIf(false, (byte & 0x07) + 1);
break;
// long jump, forward and back
case 0xA0:
byte2 = method.bytes[this.pc++];
this.generateJump(((byte&7)-4) * 256 + byte2);
break;
// long conditional jump
case 0xA8:
byte2 = method.bytes[this.pc++];
this.generateJumpIf(byte < 0xAC, (byte & 3) * 256 + byte2);
break;
// SmallInteger ops: + - < > <= >= = ~= * / @ lshift: lxor: land: lor:
case 0xB0: case 0xB8:
this.generateNumericOp(byte);
break;
// quick primitives: // at:, at:put:, size, next, nextPut:, ...
case 0xC0: case 0xC8:
this.generateQuickPrim(byte);
break;
// send literal selector
case 0xD0: case 0xD8:
this.generateSend("lit[", 1 + (byte & 0x0F), "]", 0, false);
break;
case 0xE0: case 0xE8:
this.generateSend("lit[", 1 + (byte & 0x0F), "]", 1, false);
break;
case 0xF0: case 0xF8:
this.generateSend("lit[", 1 + (byte & 0x0F), "]", 2, false);
break;
}
}
},
generateV3Extended: function(bytecode) {
var byte2, byte3;
switch (bytecode) {
// extended push
case 0x80:
byte2 = this.method.bytes[this.pc++];
switch (byte2 >> 6) {
case 0: this.generatePush("inst[", byte2 & 0x3F, "]"); return;
case 1: this.generatePush("temp[", 6 + (byte2 & 0x3F), "]"); return;
case 2: this.generatePush("lit[", 1 + (byte2 & 0x3F), "]"); return;
case 3: this.generatePush("lit[", 1 + (byte2 & 0x3F), "].pointers[1]"); return;
}
// extended store
case 0x81:
byte2 = this.method.bytes[this.pc++];
switch (byte2 >> 6) {
case 0: this.generateStoreInto("inst[", byte2 & 0x3F, "]"); return;
case 1: this.generateStoreInto("temp[", 6 + (byte2 & 0x3F), "]"); return;
case 2: throw Error("illegal store into literal");
case 3: this.generateStoreInto("lit[", 1 + (byte2 & 0x3F), "].pointers[1]"); return;
}
return;
// extended pop into
case 0x82:
byte2 = this.method.bytes[this.pc++];
switch (byte2 >> 6) {
case 0: this.generatePopInto("inst[", byte2 & 0x3F, "]"); return;
case 1: this.generatePopInto("temp[", 6 + (byte2 & 0x3F), "]"); return;
case 2: throw Error("illegal pop into literal");
case 3: this.generatePopInto("lit[", 1 + (byte2 & 0x3F), "].pointers[1]"); return;
}
// Single extended send
case 0x83:
byte2 = this.method.bytes[this.pc++];
this.generateSend("lit[", 1 + (byte2 & 0x1F), "]", byte2 >> 5, false);
return;
// Double extended do-anything
case 0x84:
byte2 = this.method.bytes[this.pc++];
byte3 = this.method.bytes[this.pc++];
switch (byte2 >> 5) {
case 0: this.generateSend("lit[", 1 + byte3, "]", byte2 & 31, false); return;
case 1: this.generateSend("lit[", 1 + byte3, "]", byte2 & 31, true); return;
case 2: this.generatePush("inst[", byte3, "]"); return;
case 3: this.generatePush("lit[", 1 + byte3, "]"); return;
case 4: this.generatePush("lit[", 1 + byte3, "].pointers[1]"); return;
case 5: this.generateStoreInto("inst[", byte3, "]"); return;
case 6: this.generatePopInto("inst[", byte3, "]"); return;
case 7: this.generateStoreInto("lit[", 1 + byte3, "].pointers[1]"); return;
}
// Single extended send to super
case 0x85:
byte2 = this.method.bytes[this.pc++];
this.generateSend("lit[", 1 + (byte2 & 0x1F), "]", byte2 >> 5, true);
return;
// Second extended send
case 0x86:
byte2 = this.method.bytes[this.pc++];
this.generateSend("lit[", 1 + (byte2 & 0x3F), "]", byte2 >> 6, false);
return;
// pop
case 0x87:
this.generateInstruction("pop", "vm.sp--");
return;
// dup
case 0x88:
this.needsVar['stack'] = true;
this.generateInstruction("dup", "var dup = stack[vm.sp]; stack[++vm.sp] = dup");
return;
// thisContext
case 0x89:
this.needsVar['stack'] = true;
this.generateInstruction("push thisContext", "stack[++vm.sp] = vm.exportThisContext()");
return;
// closures
case 0x8A:
byte2 = this.method.bytes[this.pc++];
var popValues = byte2 > 127,
count = byte2 & 127;
this.generateClosureTemps(count, popValues);
return;
// call primitive
case 0x8B:
byte2 = this.method.bytes[this.pc++];
byte3 = this.method.bytes[this.pc++];
this.generateCallPrimitive(byte2 + 256 * byte3, 0x81);
return
// remote push from temp vector
case 0x8C:
byte2 = this.method.bytes[this.pc++];
byte3 = this.method.bytes[this.pc++];
this.generatePush("temp[", 6 + byte3, "].pointers[", byte2, "]");
return;
// remote store into temp vector
case 0x8D:
byte2 = this.method.bytes[this.pc++];
byte3 = this.method.bytes[this.pc++];
this.generateStoreInto("temp[", 6 + byte3, "].pointers[", byte2, "]");
return;
// remote store and pop into temp vector
case 0x8E:
byte2 = this.method.bytes[this.pc++];
byte3 = this.method.bytes[this.pc++];
this.generatePopInto("temp[", 6 + byte3, "].pointers[", byte2, "]");
return;
// pushClosureCopy
case 0x8F:
byte2 = this.method.bytes[this.pc++];
byte3 = this.method.bytes[this.pc++];
var byte4 = this.method.bytes[this.pc++];
var numArgs = byte2 & 0xF,
numCopied = byte2 >> 4,
blockSize = byte3 << 8 | byte4;
this.generateClosureCopy(numArgs, numCopied, blockSize);
return;
}
},
generateSista: function() {
var bytes = this.method.bytes,
b,
b2,
b3,
extA = 0,
extB = 0;
this.done = false;
while (!this.done) {
b = bytes[this.pc++];
switch (b) {
// 1 Byte Bytecodes
// load receiver variable
case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
case 0x08: case 0x09: case 0x0A: case 0x0B: case 0x0C: case 0x0D: case 0x0E: case 0x0F:
this.generatePush("inst[", b & 0x0F, "]");
break;
// load literal variable
case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17:
case 0x18: case 0x19: case 0x1A: case 0x1B: case 0x1C: case 0x1D: case 0x1E: case 0x1F:
this.generatePush("lit[", 1 + (b & 0x0F), "].pointers[1]");
break;
// load literal constant
case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
case 0x28: case 0x29: case 0x2A: case 0x2B: case 0x2C: case 0x2D: case 0x2E: case 0x2F:
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
case 0x38: case 0x39: case 0x3A: case 0x3B: case 0x3C: case 0x3D: case 0x3E: case 0x3F:
this.generatePush("lit[", 1 + (b & 0x1F), "]");
break;
// load temporary variable
case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
this.generatePush("temp[", 6 + (b & 0x07), "]");
break;
case 0x48: case 0x49: case 0x4A: case 0x4B:
this.generatePush("temp[", 6 + (b & 0x03) + 8, "]");
break;
case 0x4C: this.generatePush("rcvr");
break;
case 0x4D: this.generatePush("vm.trueObj");
break;
case 0x4E: this.generatePush("vm.falseObj");
break;
case 0x4F: this.generatePush("vm.nilObj");
break;
case 0x50: this.generatePush(0);
break;
case 0x51: this.generatePush(1);
break;
case 0x52:
this.needsVar['stack'] = true;
this.generateInstruction("push thisContext", "stack[++vm.sp] = vm.exportThisContext()");
break;
case 0x53:
this.needsVar['stack'] = true;
this.generateInstruction("dup", "var dup = stack[vm.sp]; stack[++vm.sp] = dup");
break;
case 0x54: case 0x55: case 0x56: case 0x57:
throw Error("unusedBytecode " + b);
case 0x58: this.generateReturn("rcvr");
break;
case 0x59: this.generateReturn("vm.trueObj");
break;
case 0x5A: this.generateReturn("vm.falseObj");
break;
case 0x5B: this.generateReturn("vm.nilObj");
break;
case 0x5C: this.generateReturn("stack[vm.sp]");
break;
case 0x5D: this.generateBlockReturn("vm.nilObj");
break;
case 0x5E: this.generateBlockReturn();
break;
case 0x5F: break; // nop
case 0x60: case 0x61: case 0x62: case 0x63: case 0x64: case 0x65: case 0x66: case 0x67:
case 0x68: case 0x69: case 0x6A: case 0x6B: case 0x6C: case 0x6D: case 0x6E: case 0x6F:
this.generateNumericOp(b);
break;
case 0x70: case 0x71: case 0x72: case 0x73: case 0x74: case 0x75: case 0x76: case 0x77:
case 0x78: case 0x79: case 0x7A: case 0x7B: case 0x7C: case 0x7D: case 0x7E: case 0x7F:
this.generateQuickPrim(b);
break;
case 0x80: case 0x81: case 0x82: case 0x83: case 0x84: case 0x85: case 0x86: case 0x87:
case 0x88: case 0x89: case 0x8A: case 0x8B: case 0x8C: case 0x8D: case 0x8E: case 0x8F:
this.generateSend("lit[", 1 + (b & 0x0F), "]", 0, false);
break;
case 0x90: case 0x91: case 0x92: case 0x93: case 0x94: case 0x95: case 0x96: case 0x97:
case 0x98: case 0x99: case 0x9A: case 0x9B: case 0x9C: case 0x9D: case 0x9E: case 0x9F:
this.generateSend("lit[", 1 + (b & 0x0F), "]", 1, false);
break;
case 0xA0: case 0xA1: case 0xA2: case 0xA3: case 0xA4: case 0xA5: case 0xA6: case 0xA7:
case 0xA8: case 0xA9: case 0xAA: case 0xAB: case 0xAC: case 0xAD: case 0xAE: case 0xAF:
this.generateSend("lit[", 1 + (b & 0x0F), "]", 2, false);
break;
case 0xB0: case 0xB1: case 0xB2: case 0xB3: case 0xB4: case 0xB5: case 0xB6: case 0xB7:
this.generateJump((b & 0x07) + 1);
break;
case 0xB8: case 0xB9: case 0xBA: case 0xBB: case 0xBC: case 0xBD: case 0xBE: case 0xBF:
this.generateJumpIf(true, (b & 0x07) + 1);
break;
case 0xC0: case 0xC1: case 0xC2: case 0xC3: case 0xC4: case 0xC5: case 0xC6: case 0xC7:
this.generateJumpIf(false, (b & 0x07) + 1);
break;
case 0xC8: case 0xC9: case 0xCA: case 0xCB: case 0xCC: case 0xCD: case 0xCE: case 0xCF:
this.generatePopInto("inst[", b & 0x07, "]");
break;
case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4: case 0xD5: case 0xD6: case 0xD7:
this.generatePopInto("temp[", 6 + (b & 0x07), "]");
break;
case 0xD8: this.generateInstruction("pop", "vm.sp--");
break;
case 0xD9:
throw Error("unumplementedBytecode: 0xD9 (unconditional trap)");
case 0xDA: case 0xDB: case 0xDC: case 0xDD: case 0xDE: case 0xDF:
throw Error("unusedBytecode " + b);
// 2 Byte Bytecodes
case 0xE0:
b2 = bytes[this.pc++];
extA = extA * 256 + b2;
continue;
case 0xE1:
b2 = bytes[this.pc++];
extB = extB * 256 + (b2 < 128 ? b2 : b2 - 256);
continue;
case 0xE2:
b2 = bytes[this.pc++];
this.generatePush("inst[", b2 + extA * 256, "]");
break;
case 0xE3:
b2 = bytes[this.pc++];
this.generatePush("lit[", 1 + b2 + extA * 256, "].pointers[1]");
break;
case 0xE4:
b2 = bytes[this.pc++];
this.generatePush("lit[", 1 + b2 + extA * 256, "]");
break;
case 0xE5:
b2 = bytes[this.pc++];
this.generatePush("temp[", 6 + b2, "]");
break;
case 0xE6:
throw Error("unusedBytecode 0xE6");
case 0xE7:
b2 = bytes[this.pc++];
var popValues = b2 > 127,
count = b2 & 127;
this.generateClosureTemps(count, popValues);
break;
case 0xE8:
b2 = bytes[this.pc++];
this.generatePush(b2 + extB * 256);
break;
case 0xE9:
b2 = bytes[this.pc++];
this.generatePush("vm.image.getCharacter(", b2 + extB * 256, ")");
break;
case 0xEA:
b2 = bytes[this.pc++];
this.generateSend("lit[", 1 + (b2 >> 3) + (extA << 5), "]", (b2 & 7) + (extB << 3), false);
break;
case 0xEB:
b2 = bytes[this.pc++];
var lit = (b2 >> 3) + (extA << 5),
numArgs = (b2 & 7) + ((extB & 63) << 3),
directed = extB >= 64;
this.generateSend("lit[", 1 + lit, "]", numArgs, directed ? "directed" : true);
break;
case 0xEC:
throw Error("unimplemented bytecode: 0xEC (class trap)");
case 0xED:
b2 = bytes[this.pc++];
this.generateJump(b2 + extB * 256);
break;
case 0xEE:
b2 = bytes[this.pc++];
this.generateJumpIf(true, b2 + extB * 256);
break;
case 0xEF:
b2 = bytes[this.pc++];
this.generateJumpIf(false, b2 + extB * 256);
break;
case 0xF0:
b2 = bytes[this.pc++];
this.generatePopInto("inst[", b2 + extA * 256, "]");
break;
case 0xF1:
b2 = bytes[this.pc++];
this.generatePopInto("lit[", 1 + b2 + extA * 256, "].pointers[1]");
break;
case 0xF2:
b2 = bytes[this.pc++];
this.generatePopInto("temp[", 6 + b2, "]");
break;
case 0xF3:
b2 = bytes[this.pc++];
this.generateStoreInto("inst[", b2 + extA * 256, "]");
break;
case 0xF4:
b2 = bytes[this.pc++];
this.generateStoreInto("lit[", 1 + b2 + extA * 256, "].pointers[1]");
break;
case 0xF5:
b2 = bytes[this.pc++];
this.generateStoreInto("temp[", 6 + b2, "]");
break;
case 0xF6: case 0xF7:
throw Error("unusedBytecode " + b);
// 3 Byte Bytecodes
case 0xF8:
b2 = bytes[this.pc++];
b3 = bytes[this.pc++];
this.generateCallPrimitive(b2 + b3 * 256, 0xF5);
break;
case 0xF9:
b2 = bytes[this.pc++];
b3 = bytes[this.pc++];
this.generatePushFullClosure(b2 + extA * 255, b3);
break;
case 0xFA:
b2 = bytes[this.pc++];
b3 = bytes[this.pc++];
var numArgs = (b2 & 0x07) + (extA & 0x0F) * 8,
numCopied = (b2 >> 3 & 0x7) + (extA >> 4) * 8,
blockSize = b3 + (extB << 8);
this.generateClosureCopy(numArgs, numCopied, blockSize);
break;
case 0xFB:
b2 = bytes[this.pc++];
b3 = bytes[this.pc++];
this.generatePush("temp[", 6 + b3, "].pointers[", b2, "]");
break;
case 0xFC:
b2 = bytes[this.pc++];
b3 = bytes[this.pc++];
this.generateStoreInto("temp[", 6 + b3, "].pointers[", b2, "]");
break;
case 0xFD:
b2 = bytes[this.pc++];
b3 = bytes[this.pc++];
this.generatePopInto("temp[", 6 + b3, "].pointers[", b2, "]");
break;
case 0xFE: case 0xFF:
throw Error("unusedBytecode " + b);
default:
throw Error("illegal bytecode: " + b);
}
extA = 0;
extB = 0;
}
},
generatePush: function(target, arg1, suffix1, arg2, suffix2) {
if (this.debug) this.generateDebugCode("push", target, arg1, suffix1, arg2, suffix2);
this.generateLabel();
this.needsVar[target] = true;
this.needsVar['stack'] = true;
this.source.push("stack[++vm.sp] = ", target);
if (arg1 !== undefined) {
this.source.push(arg1, suffix1);
if (arg2 !== undefined) {
this.source.push(arg2, suffix2);
}
}
this.source.push(";\n");
},
generateStoreInto: function(target, arg1, suffix1, arg2, suffix2) {
if (this.debug) this.generateDebugCode("store into", target, arg1, suffix1, arg2, suffix2);
this.generateLabel();
this.needsVar[target] = true;
this.needsVar['stack'] = true;
this.source.push(target);
if (arg1 !== undefined) {
this.source.push(arg1, suffix1);
if (arg2 !== undefined) {
this.source.push(arg2, suffix2);
}
}
this.source.push(" = stack[vm.sp];\n");
this.generateDirty(target, arg1, suffix1);
},
generatePopInto: function(target, arg1, suffix1, arg2, suffix2) {
if (this.debug) this.generateDebugCode("pop into", target, arg1, suffix1, arg2, suffix2);
this.generateLabel();
this.needsVar[target] = true;
this.needsVar['stack'] = true;
this.source.push(target);
if (arg1 !== undefined) {
this.source.push(arg1, suffix1);
if (arg2 !== undefined) {
this.source.push(arg2, suffix2);
}
}
this.source.push(" = stack[vm.sp--];\n");
this.generateDirty(target, arg1, suffix1);
},
generateReturn: function(what) {
if (this.debug) this.generateDebugCode("return", what);
this.generateLabel();
this.needsVar[what] = true;
this.source.push(
"vm.pc = ", this.pc, "; vm.doReturn(", what, "); return;\n");
this.needsBreak = false; // returning anyway
this.done = this.pc > this.endPC;
},
generateBlockReturn: function(retVal) {
if (this.debug) this.generateDebugCode("block return");
this.generateLabel();
if (!retVal) {
this.needsVar['stack'] = true;
retVal = "stack[vm.sp--]";
}
// actually stack === context.pointers but that would look weird
this.needsVar['context'] = true;
this.source.push(
"vm.pc = ", this.pc, "; vm.doReturn(", retVal, ", context.pointers[0]); return;\n");
this.needsBreak = false; // returning anyway
this.done = this.pc > this.endPC;
},
generateJump: function(distance) {
var destination = this.pc + distance;
if (this.debug) this.generateDebugCode("jump to " + destination);
this.generateLabel();
this.needsVar['context'] = true;
this.source.push("vm.pc = ", destination, "; ");
if (distance < 0) this.source.push(
"\nif (vm.interruptCheckCounter-- <= 0) {\n",
" vm.checkForInterrupts();\n",
" if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return;\n",
"}\n");
if (this.singleStep) this.source.push("\nif (vm.breakOutOfInterpreter) return;\n");
this.source.push("continue;\n");
this.needsBreak = false; // already checked
this.needsLabel[destination] = true;
if (destination > this.endPC) this.endPC = destination;
},
generateJumpIf: function(condition, distance) {
var destination = this.pc + distance;
if (this.debug) this.generateDebugCode("jump if " + condition + " to " + destination);
this.generateLabel();
this.needsVar['stack'] = true;
this.source.push(
"var cond = stack[vm.sp--]; if (cond === vm.", condition, "Obj) {vm.pc = ", destination, "; ");
if (this.singleStep) this.source.push("if (vm.breakOutOfInterpreter) return; else ");
this.source.push("continue}\n",
"else if (cond !== vm.", !condition, "Obj) {vm.sp++; vm.pc = ", this.pc, "; vm.send(vm.specialObjects[25], 0, false); return}\n");
this.needsLabel[this.pc] = true; // for coming back after #mustBeBoolean send
this.needsLabel[destination] = true; // obviously
if (destination > this.endPC) this.endPC = destination;
},
generateQuickPrim: function(byte) {
if (this.debug) this.generateDebugCode("quick send #" + this.specialSelectors[(byte & 0x0F) + 16]);
this.generateLabel();
switch (byte & 0x0F) {
case 0x0: // at:
this.needsVar['stack'] = true;
this.source.push(
"var a, b; if ((a=stack[vm.sp-1]).sqClass === vm.specialObjects[7] && typeof (b=stack[vm.sp]) === 'number' && b>0 && b<=a.pointers.length) {\n",
" stack[--vm.sp] = a.pointers[b-1];",
"} else { var c = vm.primHandler.objectAt(true,true,false); if (vm.primHandler.success) stack[--vm.sp] = c; else {\n",
" vm.pc = ", this.pc, "; vm.sendSpecial(16); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return; }}\n");
this.needsLabel[this.pc] = true;
return;
case 0x1: // at:put:
this.needsVar['stack'] = true;
this.source.push(
"var a, b; if ((a=stack[vm.sp-2]).sqClass === vm.specialObjects[7] && typeof (b=stack[vm.sp-1]) === 'number' && b>0 && b<=a.pointers.length) {\n",
" var c = stack[vm.sp]; stack[vm.sp-=2] = a.pointers[b-1] = c; a.dirty = true;",
"} else { vm.primHandler.objectAtPut(true,true,false); if (vm.primHandler.success) stack[vm.sp-=2] = c; else {\n",
" vm.pc = ", this.pc, "; vm.sendSpecial(17); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return; }}\n");
this.needsLabel[this.pc] = true;
return;
case 0x2: // size
this.needsVar['stack'] = true;
this.source.push(
"if (stack[vm.sp].sqClass === vm.specialObjects[7]) stack[vm.sp] = stack[vm.sp].pointersSize();\n", // Array
"else if (stack[vm.sp].sqClass === vm.specialObjects[6]) stack[vm.sp] = stack[vm.sp].bytesSize();\n", // ByteString
"else { vm.pc = ", this.pc, "; vm.sendSpecial(18); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return; }\n");
this.needsLabel[this.pc] = true;
return;
//case 0x3: return false; // next
//case 0x4: return false; // nextPut:
//case 0x5: return false; // atEnd
case 0x6: // ==
this.needsVar['stack'] = true;
this.source.push("var cond = stack[vm.sp-1] === stack[vm.sp];\nstack[--vm.sp] = cond ? vm.trueObj : vm.falseObj;\n");
return;
case 0x7: // class
this.needsVar['stack'] = true;
this.source.push("stack[vm.sp] = typeof stack[vm.sp] === 'number' ? vm.specialObjects[5] : stack[vm.sp].sqClass;\n");
return;
case 0x8: // blockCopy:
this.needsVar['rcvr'] = true;
this.source.push(
"vm.pc = ", this.pc, "; if (!vm.primHandler.quickSendOther(rcvr, ", (byte & 0x0F), ")) ",
"{vm.sendSpecial(", ((byte & 0x0F) + 16), "); return}\n");
this.needsLabel[this.pc] = true; // for send
this.needsLabel[this.pc + 2] = true; // for start of block
return;
case 0x9: // value
case 0xA: // value:
case 0xB: // do:
this.needsVar['rcvr'] = true;
this.source.push(
"vm.pc = ", this.pc, "; if (!vm.primHandler.quickSendOther(rcvr, ", (byte & 0x0F), ")) vm.sendSpecial(", ((byte & 0x0F) + 16), "); return;\n");
this.needsLabel[this.pc] = true;
return;
//case 0xC: return false; // new
//case 0xD: return false; // new:
//case 0xE: return false; // x
//case 0xF: return false; // y
}
// generic version for the bytecodes not yet handled above
this.needsVar['rcvr'] = true;
this.needsVar['context'] = true;
this.source.push(
"vm.pc = ", this.pc, "; if (!vm.primHandler.quickSendOther(rcvr, ", (byte & 0x0F), "))",
" vm.sendSpecial(", ((byte & 0x0F) + 16), ");\n",
"if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return;\n");
this.needsBreak = false; // already checked
// if falling back to a full send we need a label for coming back
this.needsLabel[this.pc] = true;
},
generateNumericOp: function(byte) {
if (this.debug) this.generateDebugCode("quick send #" + this.specialSelectors[byte & 0x0F]);
this.generateLabel();
// if the op cannot be executed here, do a full send and return to main loop
// we need a label for coming back
this.needsLabel[this.pc] = true;
switch (byte & 0x0F) {
case 0x0: // PLUS +
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = vm.primHandler.signed32BitIntegerFor(a + b);\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(0); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x1: // MINUS -
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = vm.primHandler.signed32BitIntegerFor(a - b);\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(1); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x2: // LESS <
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = a < b ? vm.trueObj : vm.falseObj;\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(2); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x3: // GRTR >
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = a > b ? vm.trueObj : vm.falseObj;\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(3); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x4: // LEQ <=
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = a <= b ? vm.trueObj : vm.falseObj;\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(4); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x5: // GEQ >=
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = a >= b ? vm.trueObj : vm.falseObj;\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(5); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x6: // EQU =
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = a === b ? vm.trueObj : vm.falseObj;\n",
"} else if (a === b && a.float === a.float) {\n", // NaN check
" stack[--vm.sp] = vm.trueObj;\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(6); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x7: // NEQ ~=
this.needsVar['stack'] = true;
this.source.push("var a = stack[vm.sp - 1], b = stack[vm.sp];\n",
"if (typeof a === 'number' && typeof b === 'number') {\n",
" stack[--vm.sp] = a !== b ? vm.trueObj : vm.falseObj;\n",
"} else if (a === b && a.float === a.float) {\n", // NaN check
" stack[--vm.sp] = vm.falseObj;\n",
"} else { vm.pc = ", this.pc, "; vm.sendSpecial(7); if (context !== vm.activeContext || vm.breakOutOfInterpreter !== false) return}\n");
return;
case 0x8: // TIMES *
this.source.push("vm.success = true; vm.resultIsFloat = false; if(!vm.pop2AndPushNumResult(vm.stackIntOrFloat(1) * vm.stackIntOrFloat(0))) { vm.pc = ", this.pc, "; vm.sendSpecial(8); return}\n");
return;
case 0x9: // DIV /
this.source.push("vm.success = true; if(!vm.pop2AndPushIntResult(vm.quickDivide(vm.stackInteger(1),vm.stackInteger(0)))) { vm.pc = ", this.pc, "; vm.sendSpecial(9); return}\n");
return;
case 0xA: // MOD \
this.source.push("vm.success = true; if(!vm.pop2AndPushIntResult(vm.mod(vm.stackInteger(1),vm.stackInteger(0)))) { vm.pc = ", this.pc, "; vm.sendSpecial(10); return}\n");
return;
case 0xB: // MakePt int@int
this.source.push("vm.success = true; if(!vm.primHandler.primitiveMakePoint(1, true)) { vm.pc = ", this.pc, "; vm.sendSpecial(11); return}\n");
return;
case 0xC: // bitShift:
this.source.push("vm.success = true; if(!vm.pop2AndPushIntResult(vm.safeShift(vm.stackInteger(1),vm.stackInteger(0)))) { vm.pc = ", this.pc, "; vm.sendSpecial(12); return}\n");
return;
case 0xD: // Divide //
this.source.push("vm.success = true; if(!vm.pop2AndPushIntResult(vm.div(vm.stackInteger(1),vm.stackInteger(0)))) { vm.pc = ", this.pc, "; vm.sendSpecial(13); return}\n");
return;
case 0xE: // bitAnd:
this.source.push("vm.success = true; if(!vm.pop2AndPushIntResult(vm.stackInteger(1) & vm.stackInteger(0))) { vm.pc = ", this.pc, "; vm.sendSpecial(14); return}\n");
return;
case 0xF: // bitOr:
this.source.push("vm.success = true; if(!vm.pop2AndPushIntResult(vm.stackInteger(1) | vm.stackInteger(0))) { vm.pc = ", this.pc, "; vm.sendSpecial(15); return}\n");
return;
}
},
generateSend: function(prefix, num, suffix, numArgs, superSend) {
if (this.debug) this.generateDebugCode(
(superSend === "directed" ? "directed super send " : superSend ? "super send " : "send ")
+ (prefix === "lit[" ? this.method.pointers[num].bytesAsString() : "..."));
this.generateLabel();
this.needsVar[prefix] = true;
this.needsVar['context'] = true;
// set pc, activate new method, and return to main loop
// unless the method was a successfull primitive call (no context change)
this.source.push("vm.pc = ", this.pc);
if (superSend === "directed") {