-
Notifications
You must be signed in to change notification settings - Fork 65
/
InstructionStream.class.st
521 lines (489 loc) · 18.5 KB
/
InstructionStream.class.st
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
"
Instance variables:
sender: <Context|nil> context that invoked this context
pc: <SmallInteger> (pc = program counter) offset of the bytecode instruction currently executed
My instances can interpret the byte-encoded Smalltalk instruction set. They maintain a program counter (pc) for streaming through CompiledMethods. My subclasses are Contexts, which inherit this capability. They store the return pointer in the instance variable sender, and the current position in their method in the instance variable pc. For other users, sender can hold a method to be similarly interpreted. The unclean re-use of sender to hold the method was to avoid a trivial subclass for the stand-alone scanning function.
"
Class {
#name : #InstructionStream,
#superclass : #Object,
#instVars : [
'sender',
'pc'
],
#classVars : [
'SpecialConstants'
],
#category : #Cog
}
{ #category : #'class initialization' }
InstructionStream class >> initialize [
"Initialize an array of special constants returned by single-bytecode returns."
SpecialConstants :=
(Array with: true with: false with: nil)
, (Array with: -1 with: 0 with: 1 with: 2)
"InstructionStream initialize."
]
{ #category : #testing }
InstructionStream class >> isContextClass [
^false
]
{ #category : #'instance creation' }
InstructionStream class >> on: method [
"Answer an instance of me on the argument, method."
^self new method: method pc: method initialPC
]
{ #category : #accessing }
InstructionStream >> compiledCode [
^self method
]
{ #category : #interpreting }
InstructionStream >> interpretNext2ByteSistaV1Instruction: bytecode for: client extA: extA extB: extB startPC: startPC [
"Send to the argument, client, a message that specifies the next instruction.
This method handles the two-byte codes.
For a table of the bytecode set, see EncoderForV1's class comment."
| byte method |
method := self compiledCode.
byte := self compiledCode at: pc.
pc := pc + 1.
"We do an inline quasi-binary search on bytecode"
bytecode < 234 ifTrue: "pushes"
[bytecode < 231 ifTrue:
[bytecode < 229 ifTrue:
[| literal |
bytecode = 226 ifTrue:
[^client pushReceiverVariable: (extA bitShift: 8) + byte].
literal := method literalAt: (extA bitShift: 8) + byte + 1.
bytecode = 227 ifTrue:
[^client pushLiteralVariable: literal].
^client pushConstant: literal].
bytecode = 229 ifTrue:
[^client pushTemporaryVariable: byte].
^client pushClosureTemps: byte].
bytecode = 231 ifTrue:
[^byte < 128
ifTrue: [client pushNewArrayOfSize: byte]
ifFalse: [client pushConsArrayWithElements: byte - 128]].
bytecode = 232 ifTrue:
[^client pushConstant: (extB bitShift: 8) + byte].
^client pushConstant: (Character value: (extB bitShift: 8) + byte)].
bytecode < 240 ifTrue: "sends, trap and jump"
[bytecode < 236 ifTrue: "sends"
[^client
send: (method literalAt: (extA bitShift: 5) + (byte // 8) + 1)
super: bytecode = 235
numArgs: (extB bitShift: 3) + (byte \\ 8)].
bytecode = 236 ifTrue:
[^client trapIfNotInstanceOf: (method literalAt: (extA bitShift: 8) + byte + 1)].
bytecode = 237 ifTrue:
[^client jump: (extB bitShift: 8) + byte].
^client jump: (extB bitShift: 8) + byte if: bytecode = 238].
bytecode < 243 ifTrue:
[bytecode = 240 ifTrue:
[^client popIntoReceiverVariable: (extA bitShift: 8) + byte].
bytecode = 241 ifTrue:
[^client popIntoLiteralVariable: (method literalAt: (extA bitShift: 8) + byte + 1)].
^client popIntoTemporaryVariable: byte].
bytecode = 243 ifTrue:
[^client storeIntoReceiverVariable: (extA bitShift: 8) + byte].
bytecode = 244 ifTrue:
[^client storeIntoLiteralVariable: (method literalAt: (extA bitShift: 8) + byte + 1)].
bytecode = 245 ifTrue:
[^client storeIntoTemporaryVariable: byte].
"246-247 1111011 i xxxxxxxx UNASSIGNED"
^self unusedBytecode: client at: startPC
]
{ #category : #interpreting }
InstructionStream >> interpretNext3ByteSistaV1Instruction: bytecode for: client extA: extA extB: extB startPC: startPC [
"Send to the argument, client, a message that specifies the next instruction.
This method handles the three-byte codes.
For a table of the bytecode set, see EncoderForSistaV1's class comment."
| method byte2 byte3 |
method := self compiledCode.
byte2 := method at: pc.
byte3 := method at: pc + 1.
pc := pc + 2.
"we search the bytecodes by what we expect to be the static frequency."
bytecode = 248 ifTrue:
[^client callPrimitive: byte2 + (byte3 bitShift: 8)].
bytecode = 250 ifTrue:
["** 250 11111010 eeiiikkk jjjjjjjj Push Closure Num Copied iii (+ExtA//16*8) Num Args kkk (+ ExtA\\16*8) BlockSize jjjjjjjj (+ExtB*256). ee = num extensions"
^client
pushClosureCopyNumCopiedValues: ((byte2 bitShift: -3) bitAnd: 7) + (extA // 16 bitShift: 3)
numArgs: (byte2 bitAnd: 7) + (extA \\ 16 bitShift: 3)
blockSize: byte3 + (extB bitShift: 8)].
bytecode = 251 ifTrue:
[^client pushRemoteTemp: byte2 inVectorAt: byte3].
bytecode = 252 ifTrue:
[^client storeIntoRemoteTemp: byte2 inVectorAt: byte3].
bytecode = 253 ifTrue:
[^client popIntoRemoteTemp: byte2 inVectorAt: byte3].
"249 11111001 xxxxxxxx syyyyyyy Reserved for Push Float"
bytecode = 254 ifTrue:
[^client
jumpOrPopIfNotInstanceOf: (method literalAt: (extA bitShift: 8) + byte2 + 1)
distance: ((extB bitShift: 8) + byte3) ].
bytecode = 249 ifTrue:
[^client
pushFullClosure: (method literalAt: (extA bitShift: 8) + byte2 + 1)
numCopied: (byte3 bitAnd: 16r3F)
receiverOnStack: (byte3 bitAt: 7) = 1
ignoreOuterContext: (byte3 bitAt: 8) = 1 ].
^self unusedBytecode: client at: startPC
]
{ #category : #interpreting }
InstructionStream >> interpretNextInstructionFor: client [
"Send to the argument, client, a message that specifies the type of the
next instruction."
^ self compiledCode encoderClass interpretNextInstructionFor: client in: self
]
{ #category : #interpreting }
InstructionStream >> interpretNextSistaV1InstructionFor: client [
"Send to the argument, client, a message that specifies the next instruction."
| byte div16 offset method extA extB savedPC |
method := self compiledCode.
"For a table of the bytecode set, see EncoderForSistaV1's class comment."
"consume and compute any extensions first."
extA := extB := 0.
savedPC := pc.
[byte := self compiledCode at: pc.
pc := pc + 1.
byte between: 16rE0 and: 16rE1] whileTrue:
[| extByte |
extByte := self compiledCode at: pc.
pc := pc + 1.
byte = 16rE0
ifTrue:
[extA := (extA bitShift: 8) + extByte]
ifFalse:
[extB := (extB = 0 and: [extByte > 127])
ifTrue: [extByte - 256]
ifFalse: [(extB bitShift: 8) + extByte]]].
div16 := byte // 16.
offset := byte \\ 16.
"We do an inline quasi-binary search on each of the possible 16 values of div16"
div16 < 11 ifTrue:
[div16 < 6 ifTrue:
[div16 < 4 ifTrue:
[div16 < 2 ifTrue:
[div16 = 0 ifTrue:
[^client pushReceiverVariable: offset].
^client pushLiteralVariable: (method literalAt: offset + 1)]. "div16 = 1"
^client pushConstant: (method literalAt: byte \\ 32 + 1)].
div16 = 4 ifTrue:
[offset < 12 ifTrue:
[^client pushTemporaryVariable: offset].
offset = 12 ifTrue:
[^client pushReceiver].
offset = 13 ifTrue:
[^client pushConstant: true].
offset = 14 ifTrue:
[^client pushConstant: false].
offset = 15 ifTrue:
[^client pushConstant: nil]].
"div16 = 5"
offset < 2 ifTrue:
[^client pushConstant: offset].
offset = 2 ifTrue:
[^self interpretSistaV1ExtendedPush: extB for: client].
offset = 3 ifTrue:
[^client doDup].
offset = 8 ifTrue:
[^client methodReturnReceiver].
offset = 9 ifTrue:
[^client methodReturnConstant: true].
offset = 10 ifTrue:
[^client methodReturnConstant: false].
offset = 11 ifTrue:
[^client methodReturnConstant: nil].
offset = 12 ifTrue:
[^client methodReturnTop].
offset = 13 ifTrue:
[^client blockReturnConstant: nil].
offset = 14 ifTrue:
[^client blockReturnTop].
offset = 15 ifTrue:
[^client doNop].
^self unusedBytecode: client at: savedPC].
"short sends"
div16 = 6 ifTrue:
[^client
send: (Smalltalk specialSelectorAt: offset + 1)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 1)].
div16 = 7 ifTrue:
[^client
send: (Smalltalk specialSelectorAt: offset + 17)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 17)].
^client
send: (method literalAt: offset + 1)
super: false
numArgs: div16 - 8].
"div16 >= 11; bytecode >= 176"
div16 < 14 ifTrue:
[div16 = 11 ifTrue:
[offset < 8 ifTrue:
[^client jump: offset + 1].
^client jump: offset - 7 if: true].
div16 = 12 ifTrue:
[offset < 8 ifTrue:
[^client jump: offset + 1 if: false].
^client popIntoReceiverVariable: offset - 8].
"div16 = 13"
offset < 8 ifTrue:
[^client popIntoTemporaryVariable: offset].
offset = 8 ifTrue: [ ^ client doPop ].
offset = 9 ifTrue: [ ^ client trap ].
^self unusedBytecode: client at: savedPC].
"2 byte and 3 byte codes"
byte < 248 ifTrue:
[^self interpretNext2ByteSistaV1Instruction: byte for: client extA: extA extB: extB startPC: savedPC].
^self interpretNext3ByteSistaV1Instruction: byte for: client extA: extA extB: extB startPC: savedPC
]
{ #category : #interpreting }
InstructionStream >> interpretNextV3PlusClosureInstructionFor: client [
"Send to the argument, client, a message that specifies the type of the
next instruction."
| byte type offset method |
method := self compiledCode.
byte := method at: pc.
type := byte // 16.
offset := byte \\ 16.
pc := pc+1.
"We do an inline binary search on each of the possible 16 values of type:
The old, cleaner but slowe code is retained as a comment below"
type < 8
ifTrue: [type < 4
ifTrue: [type < 2
ifTrue: [type < 1
ifTrue: ["type = 0"
^ client pushReceiverVariable: offset]
ifFalse: ["type = 1"
^ client pushTemporaryVariable: offset]]
ifFalse: [type < 3
ifTrue: ["type = 2"
^ client
pushConstant: (method literalAt: offset + 1)]
ifFalse: ["type = 3"
^ client
pushConstant: (method literalAt: offset + 17)]]]
ifFalse: [type < 6
ifTrue: [type < 5
ifTrue: ["type = 4"
^ client
pushLiteralVariable: (method literalAt: offset + 1)]
ifFalse: ["type = 5"
^ client
pushLiteralVariable: (method literalAt: offset + 17)]]
ifFalse: [type < 7
ifTrue: ["type = 6"
offset < 8
ifTrue: [^ client popIntoReceiverVariable: offset]
ifFalse: [^ client popIntoTemporaryVariable: offset - 8]]
ifFalse: ["type = 7"
offset = 0
ifTrue: [^ client pushReceiver].
offset < 8
ifTrue: [^ client
pushConstant: (SpecialConstants at: offset)].
offset = 8
ifTrue: [^ client methodReturnReceiver].
offset < 12
ifTrue: [^ client
methodReturnConstant: (SpecialConstants at: offset - 8)].
offset = 12
ifTrue: [^ client methodReturnTop].
offset = 13
ifTrue: [^ client blockReturnTop].
offset > 13
ifTrue: [^ self unusedBytecode: client at: pc - 1 ]]]]]
ifFalse: [type < 12
ifTrue: [type < 10
ifTrue: [type < 9
ifTrue: ["type = 8"
^ self
interpretV3PlusClosureExtension: offset
in: method
for: client]
ifFalse: ["type = 9 (short jumps)"
offset < 8
ifTrue: [^ client jump: offset + 1].
^ client jump: offset - 8 + 1 if: false]]
ifFalse: [type < 11
ifTrue: ["type = 10 (long jumps)"
byte := method at: pc.
pc := pc + 1.
offset < 8
ifTrue: [^ client jump: offset - 4 * 256 + byte].
^ client jump: (offset bitAnd: 3)
* 256 + byte if: offset < 12]
ifFalse: ["type = 11"
^ client
send: (Smalltalk specialSelectorAt: offset + 1)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 1)]]]
ifFalse: [type = 12
ifTrue: [^ client
send: (Smalltalk specialSelectorAt: offset + 17)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 17)]
ifFalse: ["type = 13, 14 or 15"
^ client
send: (method literalAt: offset + 1)
super: false
numArgs: type - 13]]].
" old code
type=0 ifTrue: [^client pushReceiverVariable: offset].
type=1 ifTrue: [^client pushTemporaryVariable: offset].
type=2 ifTrue: [^client pushConstant: (method literalAt: offset+1)].
type=3 ifTrue: [^client pushConstant: (method literalAt: offset+17)].
type=4 ifTrue: [^client pushLiteralVariable: (method literalAt: offset+1)].
type=5 ifTrue: [^client pushLiteralVariable: (method literalAt: offset+17)].
type=6
ifTrue: [offset<8
ifTrue: [^client popIntoReceiverVariable: offset]
ifFalse: [^client popIntoTemporaryVariable: offset-8]].
type=7
ifTrue: [offset=0 ifTrue: [^client pushReceiver].
offset<8 ifTrue: [^client pushConstant: (SpecialConstants at: offset)].
offset=8 ifTrue: [^client methodReturnReceiver].
offset<12 ifTrue: [^client methodReturnConstant:
(SpecialConstants at: offset-8)].
offset=12 ifTrue: [^client methodReturnTop].
offset=13 ifTrue: [^client blockReturnTop].
offset>13 ifTrue: [^self error: 'unusedBytecode']].
type=8 ifTrue: [^self interpretExtension: offset in: method for: client].
type=9
ifTrue: short jumps
[offset<8 ifTrue: [^client jump: offset+1].
^client jump: offset-8+1 if: false].
type=10
ifTrue: long jumps
[byte:= method at: pc. pc:= pc+1.
offset<8 ifTrue: [^client jump: offset-4*256 + byte].
^client jump: (offset bitAnd: 3)*256 + byte if: offset<12].
type=11
ifTrue:
[^client
send: (Smalltalk specialSelectorAt: offset+1)
super: false
numArgs: (Smalltalk specialNargsAt: offset+1)].
type=12
ifTrue:
[^client
send: (Smalltalk specialSelectorAt: offset+17)
super: false
numArgs: (Smalltalk specialNargsAt: offset+17)].
type>12
ifTrue:
[^client send: (method literalAt: offset+1)
super: false
numArgs: type-13]"
]
{ #category : #interpreting }
InstructionStream >> interpretSistaV1ExtendedPush: extB for: client [
"Implement the extended push for non-zero extensions."
"* 82 01010010 Push thisContext, (then Extend B = 1 => push
thisProcess) "
extB = 0
ifTrue: [^ client pushActiveContext].
extB = 1
ifTrue: [^ client pushActiveProcess].
self error: 'undefined extended push'
]
{ #category : #interpreting }
InstructionStream >> interpretV3PlusClosureExtension: offset in: method for: client [
| type offset2 byte2 byte3 byte4 |
offset <= 6 ifTrue:
["Extended op codes 128-134"
byte2 := method at: pc. pc := pc + 1.
offset <= 2 ifTrue:
["128-130: extended pushes and pops"
type := byte2 // 64.
offset2 := byte2 \\ 64.
offset = 0 ifTrue:
[type = 0 ifTrue: [^client pushReceiverVariable: offset2].
type = 1 ifTrue: [^client pushTemporaryVariable: offset2].
type = 2 ifTrue: [^client pushConstant: (method literalAt: offset2 + 1)].
type = 3 ifTrue: [^client pushLiteralVariable: (method literalAt: offset2 + 1)]].
offset = 1 ifTrue:
[type = 0 ifTrue: [^client storeIntoReceiverVariable: offset2].
type = 1 ifTrue: [^client storeIntoTemporaryVariable: offset2].
type = 2 ifTrue: [self error: 'illegalStore'].
type = 3 ifTrue: [^client storeIntoLiteralVariable: (method literalAt: offset2 + 1)]].
offset = 2 ifTrue:
[type = 0 ifTrue: [^client popIntoReceiverVariable: offset2].
type = 1 ifTrue: [^client popIntoTemporaryVariable: offset2].
type = 2 ifTrue: [self error: 'illegalStore'].
type = 3 ifTrue: [^client popIntoLiteralVariable: (method literalAt: offset2 + 1)]]].
"131-134: extended sends"
offset = 3 ifTrue: "Single extended send"
[^client send: (method literalAt: byte2 \\ 32 + 1)
super: false numArgs: byte2 // 32].
offset = 4 ifTrue: "Double extended do-anything"
[byte3 := method at: pc. pc := pc + 1.
type := byte2 // 32.
type = 0 ifTrue: [^client send: (method literalAt: byte3 + 1)
super: false numArgs: byte2 \\ 32].
type = 1 ifTrue: [^client send: (method literalAt: byte3 + 1)
super: true numArgs: byte2 \\ 32].
type = 2 ifTrue: [^client pushReceiverVariable: byte3].
type = 3 ifTrue: [^client pushConstant: (method literalAt: byte3 + 1)].
type = 4 ifTrue: [^client pushLiteralVariable: (method literalAt: byte3 + 1)].
type = 5 ifTrue: [^client storeIntoReceiverVariable: byte3].
type = 6 ifTrue: [^client popIntoReceiverVariable: byte3].
type = 7 ifTrue: [^client storeIntoLiteralVariable: (method literalAt: byte3 + 1)]].
offset = 5 ifTrue: "Single extended send to super"
[^client send: (method literalAt: byte2 \\ 32 + 1)
super: true numArgs: byte2 // 32].
offset = 6 ifTrue: "Second extended send"
[^client send: (method literalAt: byte2 \\ 64 + 1)
super: false numArgs: byte2 // 64]].
offset = 7 ifTrue: [^client doPop].
offset = 8 ifTrue: [^client doDup].
offset = 9 ifTrue: [^client pushActiveContext].
byte2 := method at: pc. pc := pc + 1.
offset = 10 ifTrue:
[^byte2 < 128
ifTrue: [client pushNewArrayOfSize: byte2]
ifFalse: [client pushConsArrayWithElements: byte2 - 128]].
byte3 := method at: pc. pc := pc + 1.
offset = 11 ifTrue: [^client callPrimitive: byte2 + (byte3 * 256) ].
offset = 12 ifTrue: [^client pushRemoteTemp: byte2 inVectorAt: byte3].
offset = 13 ifTrue: [^client storeIntoRemoteTemp: byte2 inVectorAt: byte3].
offset = 14 ifTrue: [^client popIntoRemoteTemp: byte2 inVectorAt: byte3].
"offset = 15"
byte4 := method at: pc. pc := pc + 1.
^client
pushClosureCopyNumCopiedValues: (byte2 bitShift: -4)
numArgs: (byte2 bitAnd: 16rF)
blockSize: (byte3 * 256) + byte4
]
{ #category : #scanning }
InstructionStream >> method [
"Answer the compiled method that supplies the receiver's bytecodes."
^sender "method access when used alone (not as part of a context)"
]
{ #category : #private }
InstructionStream >> method: method pc: startpc [
sender := method.
"allows this class to stand alone as a method scanner"
pc := startpc
]
{ #category : #scanning }
InstructionStream >> pc [
"Answer the index of the next bytecode."
^pc
]
{ #category : #private }
InstructionStream >> pc: anInteger [
pc := anInteger
]
{ #category : #'as yet unclassified' }
InstructionStream >> willBranch [
"Answer whether the next bytecode is a conditional branch."
| method |
method := self method.
^method encoderClass isBranchAt: pc in: method
]