-
Notifications
You must be signed in to change notification settings - Fork 65
/
InstructionStream.extension.st
443 lines (428 loc) · 16.8 KB
/
InstructionStream.extension.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
Extension { #name : #InstructionStream }
{ #category : #'*BytecodeSets-NewsqueakV3-decoding' }
InstructionStream >> interpretNSV3Extension: 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]].
offset = 11 ifTrue: "139: pushExplicitOuter"
[^client pushExplicitOuter: (method literalAt: byte2 + 1)].
byte3 := method at: pc. pc := pc + 1.
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 : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> interpretNSV4ExtendedPush: extB for: client [
"Implement the extended push for non-zero extensions."
"77 01001101 Push false [* 1:true, 2:nil, 3:thisContext, ..., -N: pushExplicitOuter: N, N = Extend B]"
extB < 0 ifTrue:
[^client pushExplicitOuter: extB negated].
extB < 2 ifTrue:
[^client pushConstant: extB = 1].
extB = 2 ifTrue:
[^client pushConstant: nil].
extB = 3 ifTrue:
[^client pushActiveContext].
self error: 'undefined extended push'
]
{ #category : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> interpretNSV4Jump [
"If the instruction at pc is an unconditional jump, interpret it, advancing the pc,
and answering the jump distance. Otherwise answer nil."
" 192-199 11000 i i i Jump iii + 1 (i.e., 1 through 8)
* 225 11100001 sbbbbbbb Extend B (Ext B = Ext B prev * 256 + Ext B)
* 242 11110010 i i i i i i i i Jump i i i i i i i i (+ Extend B * 256, where bbbbbbbb = sddddddd, e.g. -32768 = i=0, a=0, s=1)"
| method byte nextpc extA extB |
method := self method.
"consume and compute any extensions first."
extA := extB := 0.
nextpc := pc. "must not advance pc unless this is a jump."
[byte := self method at: nextpc.
nextpc := nextpc + 1.
byte >= 224 and: [byte <= 225]] whileTrue:
[| extByte |
extByte := self method at: nextpc.
nextpc := nextpc + 1.
byte = 224
ifTrue:
[extA := (extA bitShift: 8) + extByte]
ifFalse:
[extB := (extB = 0 and: [extByte > 127])
ifTrue: [extByte - 256]
ifFalse: [(extB bitShift: 8) + extByte]]].
(byte between: 192 and: 199) ifTrue:
[pc := nextpc.
^byte - 191].
byte = 242 ifTrue:
[byte := method at: nextpc.
pc := nextpc + 1.
^(extB bitShift: 8) + byte].
^nil
]
{ #category : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> interpretNSV4JumpIfCond [
"If the instruction at pc is a conditional jump, interpret it, advancing the pc,
and answering the jump distance. Otherwise answer nil."
" 200-207 11001 i i i Pop and Jump 0n True iii +1 (i.e., 1 through 8)
208-215 11010 i i i Pop and Jump 0n False iii +1 (i.e., 1 through 8)
* 225 11100001 sbbbbbbb Extend B (Ext B = Ext B prev * 256 + Ext B)
* 243 11110011 i i i i i i i i Pop and Jump 0n True i i i i i i i i (+ Extend B * 256, where Extend B >= 0)
* 244 11110100 i i i i i i i i Pop and Jump 0n False i i i i i i i i (+ Extend B * 256, where Extend B >= 0)"
| method byte nextpc extA extB |
method := self method.
"consume and compute any extensions first."
extA := extB := 0.
nextpc := pc. "must not advance pc unless this is a jump."
[byte := self method at: nextpc.
nextpc := nextpc + 1.
byte >= 224 and: [byte <= 225]] whileTrue:
[| extByte |
extByte := self method at: nextpc.
nextpc := nextpc + 1.
byte = 224
ifTrue:
[extA := (extA bitShift: 8) + extByte]
ifFalse:
[extB := (extB = 0 and: [extByte > 127])
ifTrue: [extByte - 256]
ifFalse: [(extB bitShift: 8) + extByte]]].
(byte between: 200 and: 215) ifTrue:
[pc := nextpc.
^(byte bitAnd: 7) + 1].
(byte between: 243 and: 244) ifTrue:
[byte := method at: nextpc.
pc := nextpc + 1.
^(extB bitShift: 8) + byte].
^nil
]
{ #category : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> interpretNext2ByteNSV4Instruction: 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 EncoderForNewsqueakV4's class comment."
| byte method |
method := self method.
byte := self method at: pc.
pc := pc + 1.
"We do an inline quasi-binary search on bytecode"
bytecode < 235 ifTrue:
[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 pushConstant: (extB bitShift: 8) + byte].
^client pushTemporaryVariable: byte].
bytecode = 231 ifTrue:
[^byte < 128
ifTrue: [client pushNewArrayOfSize: byte]
ifFalse: [client pushConsArrayWithElements: byte - 128]].
bytecode = 232 ifTrue:
[^client storeIntoReceiverVariable: (extA bitShift: 8) + byte].
bytecode = 233 ifTrue:
[^client storeIntoLiteralVariable: (method literalAt: (extA bitShift: 8) + byte + 1)].
^client storeIntoTemporaryVariable: byte].
bytecode < 238 ifTrue:
[bytecode = 235 ifTrue:
[^client popIntoReceiverVariable: (extA bitShift: 8) + byte].
bytecode = 236 ifTrue:
[^client popIntoLiteralVariable: (method literalAt: (extA bitShift: 8) + byte + 1)].
^client popIntoTemporaryVariable: byte].
bytecode < 242 ifTrue:
[| selector numArgs |
selector := method literalAt: (extA bitShift: 5) + (byte // 8) + 1.
numArgs := (extB bitShift: 3) + (byte \\ 8).
bytecode = 238 ifTrue:
[^client send: selector super: false numArgs: numArgs].
bytecode = 239 ifTrue:
[^client send: selector super: true numArgs: numArgs].
bytecode = 240 ifTrue:
[^client sendToAbsentImplicitReceiver: selector numArgs: numArgs].
^client sendToAbsentDynamicSuperclass: selector numArgs: numArgs].
bytecode < 245 ifTrue:
[bytecode = 242 ifTrue:
[^client jump: (extB bitShift: 8) + byte].
^client jump: (extB bitShift: 8) + byte if: bytecode = 243].
bytecode = 245 ifTrue:
[| selector numArgs |
selector := method literalAt: (extA bitShift: 5) + (byte // 8) + 1.
numArgs := (extB bitShift: 3) + (byte \\ 8).
^client sendToAbsentSelf: selector numArgs: numArgs].
"246-247 1111011 i xxxxxxxx UNASSIGNED
248-249 1111100 i xxxxxxxx UNASSIGNED"
^self unusedBytecode: client at: startPC
]
{ #category : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> interpretNext3ByteNSV4Instruction: 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 EncoderForNewsqueakV4's class comment."
| method byte2 byte3 |
method := self method.
byte2 := method at: pc.
byte3 := method at: pc + 1.
pc := pc + 2.
"we search the bytecodes by static frequency"
bytecode = 253 ifTrue:
["253 11111101 eeiiikkk jjjjjjjj Push Closure Num Copied iii (+ Ext A // 16 * 8) Num Args kkk (+ Ext A \\ 16 * 8) BlockSize jjjjjjjj (+ Ext B * 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 = 250 ifTrue:
[^client pushRemoteTemp: byte2 inVectorAt: byte3].
bytecode = 252 ifTrue:
[^client popIntoRemoteTemp: byte2 inVectorAt: byte3].
bytecode = 251 ifTrue:
[^client storeIntoRemoteTemp: byte2 inVectorAt: byte3].
bytecode = 249 ifTrue:
[^client callPrimitive: byte2 + (byte3 bitShift: 8)].
bytecode = 254 ifTrue:
[| selector numArgs depth |
selector := method literalAt: (extA bitShift: 5) + (byte2 // 8) + 1.
numArgs := (extB bitShift: 3) + (byte2 \\ 8).
depth := byte3.
^client sendToAbsentOuter: selector numArgs: numArgs depth: depth].
"255-255 11111111 xxxxxxxx yyyyyyyy UNASSIGNED"
^self unusedBytecode: client at: startPC
]
{ #category : #'*BytecodeSets-NewsqueakV3-decoding' }
InstructionStream >> interpretNextNSV3InstructionFor: client [
"Send to the argument, client, a message that specifies the type of the
next instruction."
| byte type offset method |
method := self method.
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."
type < 8 ifTrue:
[type < 4 ifTrue:
[type < 2 ifTrue:
[type = 0 ifTrue:
[^ client pushReceiverVariable: offset].
^ client pushTemporaryVariable: offset]. "type = 1"
type = 2 ifTrue:
[^ client pushConstant: (method literalAt: offset + 1)].
^ client pushConstant: (method literalAt: offset + 17)]. "type = 3"
type < 6 ifTrue:
[type = 4 ifTrue:
[^ client pushLiteralVariable: (method literalAt: offset + 1)].
^ client pushLiteralVariable: (method literalAt: offset + 17)]."type = 5"
type = 6 ifTrue:
[offset < 8 ifTrue:
[^ client popIntoReceiverVariable: offset].
^ client popIntoTemporaryVariable: offset - 8].
"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 = 14 ifTrue:
[| byte2 |
byte := method at: pc.
pc := pc+1.
byte2 := method at: pc.
pc := pc+1.
^ client sendToDynamicSuperclass: (method literalAt: byte2 + 1) numArgs: byte].
"offset = 15"
byte := method at: pc.
pc := pc+1.
^ client pushImplicitReceiverForMessage: (method literalAt: byte + 1)].
type < 12 ifTrue:
[type < 10 ifTrue:
[type = 8 ifTrue:
[^ self
interpretNSV3Extension: offset
in: method
for: client].
"type = 9 (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; arithmetic special selector sends"
^ client
send: (Smalltalk specialSelectorAt: offset + 1)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 1)].
type = 12 ifTrue: "non-arithmetic special selector sends"
[^ client
send: (Smalltalk specialSelectorAt: offset + 17)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 17)].
"type = 13, 14 or 15"
^ client
send: (method literalAt: offset + 1)
super: false
numArgs: type - 13 "0, 1 & 2"
]
{ #category : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> interpretNextNSV4InstructionFor: client [
"Send to the argument, client, a message that specifies the next instruction."
| byte div16 offset method extA extB savedPC |
method := self method.
"For a table of the bytecode set, see EncoderForNewsqueakV4's class comment."
"consume and compute any extensions first."
extA := extB := 0.
savedPC := pc.
[byte := self method at: pc.
pc := pc + 1.
byte >= 224 and: [byte <= 225]] whileTrue:
[| extByte |
extByte := self method at: pc.
pc := pc + 1.
byte = 224
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 < 12 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 < 14 ifTrue:
[offset = 12 ifTrue:
[^client pushReceiver].
extB = 0 ifTrue:
[^client pushConstant: false].
^self interpretNSV4ExtendedPush: extB for: client].
^client pushConstant: offset - 14]. "0 & 1"
"short sends"
div16 < 7 ifTrue: "special selector sends"
[div16 = 5 ifTrue:
[^client
send: (Smalltalk specialSelectorAt: offset + 1)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 1)].
^client
send: (Smalltalk specialSelectorAt: offset + 17)
super: false
numArgs: (Smalltalk specialNargsAt: offset + 17)].
div16 < 10 ifTrue: "vanilla sends, div16 = 7, 8 & 9"
[^client
send: (method literalAt: offset + 1)
super: false
numArgs: div16 - 7].
"implicit receiver send, div16 = 10"
div16 = 10 ifTrue:
[^client
sendToAbsentImplicitReceiver: (method literalAt: offset + 1)
numArgs: 0].
"short store pops"
offset < 8 ifTrue:
[^client popIntoReceiverVariable: offset].
^client popIntoTemporaryVariable: offset - 8].
"div16 >= 12"
div16 < 14 ifTrue:
[div16 = 12 ifTrue:
[offset < 8 ifTrue:
[^client jump: offset + 1].
^client jump: offset - 7 if: true].
offset < 8 ifTrue:
[^client jump: offset + 1 if: false].
offset < 11 ifTrue:
[offset = 8 ifTrue: [^client methodReturnReceiver].
offset = 9 ifTrue: [^client methodReturnTop].
extA > 0 ifTrue:
[^client blockReturnTopFromLevel: extA].
^client blockReturnTop].
offset = 11 ifTrue:
[^client doDup].
offset = 12 ifTrue:
[^client doPop].
offset = 13 ifTrue:
[^client doNop].
"222 11011110 break ? could have blockCopy: send break, but need a receiver and a pop result"
"223 11011111 UNASSIGNED"
^self unusedBytecode: client at: savedPC].
"2 byte and 3 byte codes"
byte < 249 ifTrue:
[^self interpretNext2ByteNSV4Instruction: byte for: client extA: extA extB: extB startPC: savedPC].
^self interpretNext3ByteNSV4Instruction: byte for: client extA: extA extB: extB startPC: savedPC
]
{ #category : #'*BytecodeSets-NewsqueakV4-decoding' }
InstructionStream >> pushExplicitOuter: n [ "<Integer>"
self subclassResponsibility
]