-
Notifications
You must be signed in to change notification settings - Fork 67
/
StackDepthFinder.class.st
443 lines (365 loc) · 11.8 KB
/
StackDepthFinder.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
Class {
#name : #StackDepthFinder,
#superclass : #InstructionClient,
#instVars : [
'stackp',
'joins',
'encoderClass',
'deadCode',
'instructionStream'
],
#category : #'VMMaker-Support'
}
{ #category : #'instance creation' }
StackDepthFinder class >> on: aVMCompiledMethodProxy [
^ self new
method: aVMCompiledMethodProxy pc: aVMCompiledMethodProxy initialPC;
yourself
]
{ #category : #'extended instruction decoding' }
StackDepthFinder >> addFloat32Vector [
self drop.
]
{ #category : #'extended instruction decoding' }
StackDepthFinder >> addFloat64Vector [
self drop.
]
{ #category : #'instruction decoding' }
StackDepthFinder >> blockReturnConstant: value [
"Return Constant From Block bytecode."
self resetStackAfterBranchOrReturn
]
{ #category : #'instruction decoding' }
StackDepthFinder >> blockReturnTop [
"Return Top Of Stack bytecode."
self drop.
self resetStackAfterBranchOrReturn
]
{ #category : #'instruction decoding' }
StackDepthFinder >> branchIfInstanceOf: behaviorOrArrayOfBehavior distance: delta [
self drop.
self doJump: delta
]
{ #category : #'instruction decoding' }
StackDepthFinder >> branchIfNotInstanceOf: behaviorOrArrayOfBehavior distance: delta [
self drop.
self doJump: delta
]
{ #category : #'instruction decoding' }
StackDepthFinder >> callPrimitive: primitiveIndex [
"Call Primitive bytecode."
"Phhh.... stackDeltaForPrimitive:in: is in BytecodeSets. But since the default response
to callPrimitive: is nothing, just handle this with an MNU for now."
[self drop: (encoderClass stackDeltaForPrimitive: primitiveIndex in: self method) negated]
on: MessageNotUnderstood
do: [:ex|
ex message selector ~~ #stackDeltaForPrimitive:in: ifTrue:
[ex pass]]
]
{ #category : #'instruction decoding' }
StackDepthFinder >> directedSuperSend: selector numArgs: numArgs [
self drop: numArgs + 1
]
{ #category : #'instruction decoding' }
StackDepthFinder >> doDup [
"Duplicate Top Of Stack bytecode."
self push
]
{ #category : #'stack depth' }
StackDepthFinder >> doJump: delta [
deadCode ifTrue:
[^self].
delta < 0
ifTrue:
[(joins at: self pc + delta) ~= stackp ifTrue: [(Notification new tag: #'bad join'; signal)]]
ifFalse:
[joins at: self pc + delta put: stackp]
]
{ #category : #'instruction decoding' }
StackDepthFinder >> doNop [
"do nothing ;-)"
]
{ #category : #'instruction decoding' }
StackDepthFinder >> doPop [
"Remove Top Of Stack bytecode."
deadCode ifFalse:
[self drop]
]
{ #category : #'stack depth' }
StackDepthFinder >> drop [
stackp := stackp - 1
]
{ #category : #'stack depth' }
StackDepthFinder >> drop: n [
stackp := stackp - n
]
{ #category : #accessing }
StackDepthFinder >> encoderClass [
^encoderClass
]
{ #category : #accessing }
StackDepthFinder >> encoderClass: anObject [
encoderClass := anObject
]
{ #category : #decoding }
StackDepthFinder >> interpretNextInstructionFor: client [
(deadCode and: [(joins at: self pc) notNil]) ifTrue:
[deadCode := false].
joins at: self pc put: stackp.
^encoderClass
ifNil: [super interpretNextInstructionFor: client]
ifNotNil: [encoderClass interpretNextInstructionFor: client in: instructionStream]
]
{ #category : #accessing }
StackDepthFinder >> joins [
^joins
]
{ #category : #'instruction decoding' }
StackDepthFinder >> jump: delta [
"Unconditional Jump bytecode."
self doJump: delta.
self resetStackAfterBranchOrReturn
]
{ #category : #'instruction decoding' }
StackDepthFinder >> jump: delta if: condition [
"Conditional Jump bytecode."
self drop.
self doJump: delta
]
{ #category : #accessing }
StackDepthFinder >> method [
^ instructionStream method
]
{ #category : #private }
StackDepthFinder >> method: method pc: startpc [
"The + 1 is there for full blocks ending with non local return,
as the following pc is mapped and beyong endPC"
instructionStream := InstructionStream on: method pc: startpc.
joins := Array new: method endPC + 1.
stackp := method numTemps.
deadCode := false
]
{ #category : #'instruction decoding' }
StackDepthFinder >> methodReturnConstant: value [
"Return Constant bytecode."
self resetStackAfterBranchOrReturn
]
{ #category : #'instruction decoding' }
StackDepthFinder >> methodReturnReceiver [
"Return Self bytecode."
self resetStackAfterBranchOrReturn
]
{ #category : #'instruction decoding' }
StackDepthFinder >> methodReturnTop [
"Return Top Of Stack bytecode."
self drop.
self resetStackAfterBranchOrReturn
]
{ #category : #accessing }
StackDepthFinder >> pc [
^ instructionStream pc
]
{ #category : #'instruction decoding' }
StackDepthFinder >> popIntoLiteralVariable: anAssociation [
"Remove Top Of Stack And Store Into Literal Variable bytecode."
self drop
]
{ #category : #'instruction decoding' }
StackDepthFinder >> popIntoReceiverVariable: offset [
"Remove Top Of Stack And Store Into Instance Variable bytecode."
self drop
]
{ #category : #'instruction decoding' }
StackDepthFinder >> popIntoRemoteTemp: remoteTempIndex inVectorAt: tempVectorIndex [
"Remove Top Of Stack And Store Into Offset of Temp Vector bytecode."
self drop
]
{ #category : #'instruction decoding' }
StackDepthFinder >> popIntoTemporaryVariable: offset [
"Remove Top Of Stack And Store Into Temporary Variable bytecode."
self drop
]
{ #category : #'stack depth' }
StackDepthFinder >> push [
stackp := stackp + 1
]
{ #category : #'stack depth' }
StackDepthFinder >> push: n [
stackp := stackp + n
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushActiveContext [
"Push Active Context On Top Of Its Own Stack bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushClosureCopyNumCopiedValues: numCopied numArgs: numArgs blockSize: blockSize [
"Push Closure bytecode."
numCopied = 0
ifTrue: [self push]
ifFalse: [numCopied > 1 ifTrue:
[self drop: numCopied - 1]].
self doJump: blockSize.
stackp := numCopied + numArgs
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushConsArrayWithElements: numElements [
"Push Cons Array of size numElements popping numElements items from the stack into the array bytecode."
self drop: numElements - 1
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushConstant: value [
"Push Constant, value, on Top Of Stack bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushExplicitOuter: level [
"Push receiver for explcit outer, on Top Of Stack bytecode."
self push
]
{ #category : #'extended instruction decoding' }
StackDepthFinder >> pushFloat32ArrayToRegister [
self drop.
]
{ #category : #'extended instruction decoding' }
StackDepthFinder >> pushFloat64ArrayToRegister [
self drop.
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushFullClosure: literal numCopied: numCopied [
"Push Closure bytecode."
numCopied = 0
ifTrue: [self push]
ifFalse: [numCopied > 1 ifTrue:
[self drop: numCopied - 1]]
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushFullClosure: literal numCopied: numCopied receiverOnStack: receiverOnStack ignoreOuterContext: ignoreOuterContext [
"Push Closure bytecode."
| delta |
delta := 1 - (receiverOnStack ifTrue: [1] ifFalse: [0]) - numCopied.
delta ~= 0 ifTrue: [self drop: delta negated]
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushLiteralVariable: anAssociation [
"Push Contents Of anAssociation On Top Of Stack bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushNewArrayOfSize: numElements [
"Push New Array of size numElements bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushReceiver [
"Push Active Context's Receiver on Top Of Stack bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushReceiverVariable: offset [
"Push Contents Of the Receiver's Instance Variable Whose Index
is the argument, offset, On Top Of Stack bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushRemoteTemp: remoteTempIndex inVectorAt: tempVectorIndex [
"Push Contents at Offset in Temp Vector bytecode."
self push
]
{ #category : #'instruction decoding' }
StackDepthFinder >> pushTemporaryVariable: offset [
"Push Contents Of Temporary Variable Whose Index Is the
argument, offset, On Top Of Stack bytecode."
self push
]
{ #category : #'stack depth' }
StackDepthFinder >> resetStackAfterBranchOrReturn [
self pc <= joins size ifTrue:
[(joins at: self pc) ifNil:
["We would like to raise an error here, but we can't because the bytecode
compiler doesn't remove dead code. e.g. the following doesn't reach ^nil
but is legal Smalltalk:
self foo
ifTrue: [^self bar]
ifFalse: [^self baz].
^nil"
deadCode := true.
^self].
stackp := joins at: self pc]
]
{ #category : #'instruction decoding' }
StackDepthFinder >> send: selector super: supered numArgs: numArgs [
"Send Message With Selector, selector, bytecode. The argument,
supered, indicates whether the receiver of the message is specified with
'super' in the source method. The arguments of the message are found in
the top numArguments locations on the stack and the receiver just
below them."
self drop: numArgs
]
{ #category : #'instruction decoding' }
StackDepthFinder >> sendToAbsentDynamicSuperclass: selector numArgs: numArgs [
"Dynamic Superclass Send Message With Selector, selector, to absent implicit receiver bytecode."
self drop: numArgs - 1 "e.g. if no args pushes a result"
]
{ #category : #'instruction decoding' }
StackDepthFinder >> sendToAbsentImplicitReceiver: selector numArgs: numArgs [
"Send Message With Selector, selector, to absent implicit receiver bytecode."
self drop: numArgs - 1 "e.g. if no args pushes a result"
]
{ #category : #'instruction decoding' }
StackDepthFinder >> sendToAbsentOuter: selector numArgs: numArgs depth: depth [
self drop: numArgs - 1 "e.g. if no args pushes a result"
]
{ #category : #'instruction decoding' }
StackDepthFinder >> sendToAbsentSelf: selector numArgs: numArgs [
self drop: numArgs - 1 "e.g. if no args pushes a result"
]
{ #category : #accessing }
StackDepthFinder >> stackPointers [
"Collect the stack depth for each bytecode in the method.
For the convenience of clients, write the stack depth for extended bytecodes
to the bytecode pc, as well as the bytecocdes' extensions' pcs."
| method stackPointers end |
"endPC +1 because of full blocks that can end with NLR effectively mapping the pc right after the end PC"
stackPointers := Array new: (end := (method := self method) endPC) + 1.
encoderClass ifNil: [encoderClass := method encoderClass].
[self pc <= end] whileTrue:
[stackPointers at: self pc put: stackp.
encoderClass extensionsAt: self pc in: method into:
[:extA :extB :delta|
2 to: delta by: 2 do: [ :dlt |
stackPointers at: self pc + dlt put: stackp]].
self interpretNextInstructionFor: self].
^stackPointers
]
{ #category : #accessing }
StackDepthFinder >> stackp [
^stackp
]
{ #category : #'extended instruction decoding' }
StackDepthFinder >> storeFloat32RegisterIntoArray [
self drop: 2.
]
{ #category : #'extended instruction decoding' }
StackDepthFinder >> storeFloat64RegisterIntoArray [
self drop: 2.
]
{ #category : #'instruction decoding' }
StackDepthFinder >> storeIntoLiteralVariable: anAssociation [
"Store Top Of Stack Into Literal Variable Of Method bytecode."
]
{ #category : #'instruction decoding' }
StackDepthFinder >> storeIntoReceiverVariable: offset [
"Store Top Of Stack Into Instance Variable Of Method bytecode."
]
{ #category : #'instruction decoding' }
StackDepthFinder >> storeIntoRemoteTemp: remoteTempIndex inVectorAt: tempVectorIndex [
"Store Top Of Stack And Store Into Offset of Temp Vector bytecode."
]
{ #category : #'instruction decoding' }
StackDepthFinder >> storeIntoTemporaryVariable: offset [
"Store Top Of Stack Into Temporary Variable Of Method bytecode."
]
{ #category : #'instruction decoding' }
StackDepthFinder >> trap [
]