author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Thu, 30 Oct 2014 22:45:30 +0000 | |
changeset 46 | 2fb37cf149fb |
parent 45 | 04a50b0d540a |
permissions | -rw-r--r-- |
41
f3898a3b378d
Package renamed from cvut:stx/goodies/newcompiler to ctu:ircompiler
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
37
diff
changeset
|
1 |
"{ Package: 'ctu:ircompiler' }" |
37 | 2 |
|
3 |
IRInterpreter subclass:#IRDecompiler |
|
4 |
instanceVariableNames:'stack sp scope currentInstr valueLabelMap mapEmptyStatement' |
|
5 |
classVariableNames:'' |
|
6 |
poolDictionaries:'' |
|
42
acdc3ec6d152
Better class category name - IR Compiler-*
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
41
diff
changeset
|
7 |
category:'IR Compiler-IR' |
37 | 8 |
! |
9 |
||
10 |
IRDecompiler comment:'I interpret IRMethod instructions and generate a Smalltalk abstract syntax tree rooted at a RBMethodNode. |
|
11 |
This is implemented like a shift-reduce parser. Each instruction either causes a node to be pushed on the stack (shift), or causes one or more nodes to be popped and combined into a single node which is push back on the stack (reduce). Most reduction is done at the "label: labelNum" instruction where it tries to reduce jump structures into control messages like #ifTrue:, whileFalse:, etc. |
|
12 |
Several pseudo nodes (RBPseudoNode and subclasses) are used to represent basic instructions that have not been reduced to real AST nodes yet. |
|
13 |
' |
|
14 |
! |
|
15 |
||
16 |
||
17 |
!IRDecompiler class methodsFor:'as yet unclassified'! |
|
18 |
||
19 |
dummySelector: numArgs |
|
20 |
"Answer a dummy selector with number of args" |
|
21 |
||
22 |
| sel | |
|
23 |
sel _ 'unknown'. |
|
24 |
1 to: numArgs do: [:i | |
|
25 |
sel _ sel, 'with:']. |
|
26 |
^ sel asSymbol |
|
27 |
! ! |
|
28 |
||
29 |
!IRDecompiler methodsFor:'accessing'! |
|
30 |
||
31 |
scope |
|
32 |
||
33 |
^scope |
|
34 |
! ! |
|
35 |
||
36 |
!IRDecompiler methodsFor:'init'! |
|
37 |
||
38 |
addTempToScope: ir |
|
39 |
||
40 |
"Temp may be created only if they are not used in the method" |
|
41 |
0 to: ir numRargs - 1 do: [:i | (scope |
|
42 |
rawVarAt: i |
|
43 |
ifNone: [ |
|
44 |
scope capturedVars do: [:each | |
|
45 |
each index = i ifTrue:[ |
|
46 |
scope tempVarAt: scope capturedVars size + scope tempVars size. |
|
47 |
^self]]. |
|
48 |
scope tempVarAt: i]) markArg] |
|
49 |
! |
|
50 |
||
51 |
decompileIR: ir |
|
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
52 |
"/ | sequenceNode temps args goto seq value method | |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
53 |
"/ scope isBlockScope |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
54 |
"/ ifTrue:[(scope addTemp: 'parent env') markArg] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
55 |
"/ ifFalse:[(scope addTemp: 'self') markArg]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
56 |
"/ ir tempKeys do: [:temp | scope tempVarAt: temp]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
57 |
"/ 0 to: ir numRargs - 1 do: [:i | (scope tempVarAt: i) markArg]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
58 |
"/ self interpret: ir. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
59 |
"/ |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
60 |
"/ self addTempToScope: ir. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
61 |
"/ self label: #return. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
62 |
"/ self Label: #return. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
63 |
"/ (self endCase: #lastReturn) ifFalse:[self Label: #return.]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
64 |
"/ goto := self Goto. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
65 |
"/ value := self ValueOrNone. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
66 |
"/ seq := self Sequence. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
67 |
"/ self removeClosureCreation: seq. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
68 |
"/ sp = 1 ifFalse: [stack explore. self error: 'error']. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
69 |
"/ value ifNotNil: [seq addNode: value]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
70 |
"/ sequenceNode := (self newBlock: seq return: goto) body. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
71 |
"/ temps := scope compactIndexTemps asArray. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
72 |
"/ ir tempKeys: temps. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
73 |
"/ args := (temps first: ir numRargs) allButFirst. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
74 |
"/ args := args collect: [:var | self newVar: var]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
75 |
"/ temps := temps allButFirst: ir numRargs. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
76 |
"/ sequenceNode temporaries: (temps collect: [:var | self newVar: var]), |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
77 |
"/ ((scope capturedVars select:[:var | var name ~= 'self' and: [var sourceTemp == nil]]) |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
78 |
"/ collect:[:var | self newVar: var]). |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
79 |
"/ method := (RBMethodNode new) |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
80 |
"/ selectorParts: (self |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
81 |
"/ newSelectorParts: (self class dummySelector: args size)); |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
82 |
"/ arguments: args; |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
83 |
"/ body: sequenceNode; |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
84 |
"/ primitiveNode: ir primitiveNode; |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
85 |
"/ scope: scope. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
86 |
"/ sequenceNode parent: method. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
87 |
"/ Preferences compileBlocksAsClosures |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
88 |
"/ ifFalse: [ASTFixDecompileBlockScope new visitNode: method]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
89 |
"/ ^ method |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
90 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
91 |
"Modified: / 30-10-2014 / 22:36:32 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 92 |
! |
93 |
||
94 |
removeClosureCreation: seq |
|
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
95 |
("Preferences compileBlocksAsClosures"true |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
96 |
and: [seq statements size > 0] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
97 |
and: [seq statements first isClosureEnvironmentCreation]) ifTrue: [ |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
98 |
seq statements removeFirst. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
99 |
(seq statements size > 0 |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
100 |
and: [seq statements first isClosureEnvironmentRegistration]) |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
101 |
ifTrue: [seq statements removeFirst]]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
102 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
103 |
["Preferences compileBlocksAsClosures"true |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
104 |
and: [seq statements size > 0] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
105 |
and: [seq statements first isClosureRegistrationAndCreation |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
106 |
or: [seq statements first isSelfClosureRegistration] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
107 |
or: [seq statements first isTempClosureRegistration]]] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
108 |
whileTrue: [seq statements removeFirst] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
109 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
110 |
"Modified: / 30-10-2014 / 22:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 111 |
! |
112 |
||
113 |
scope: aLexicalScope |
|
114 |
||
115 |
scope := aLexicalScope |
|
116 |
! ! |
|
117 |
||
118 |
!IRDecompiler methodsFor:'instructions'! |
|
119 |
||
120 |
goto: seqNum |
|
121 |
||
122 |
self stackPush: (RBPseudoGotoNode new destination: seqNum). |
|
123 |
! |
|
124 |
||
125 |
if: bool goto: seqNum1 otherwise: seqNum2 |
|
126 |
||
127 |
self stackPush: (RBPseudoIfNode new |
|
128 |
boolean: bool; |
|
129 |
destination: seqNum1; |
|
130 |
otherwise: seqNum2) |
|
131 |
! |
|
132 |
||
133 |
label: seqNum |
|
134 |
||
135 |
stack isEmpty ifTrue: [ "start" |
|
136 |
^ stack addLast: (RBPseudoLabelNode new destination: seqNum)]. |
|
137 |
||
138 |
self captureEmptyStatement. |
|
139 |
"Reduce jump structures to one of the following if possible" |
|
140 |
[ (self endBlock: seqNum) or: [ |
|
141 |
(self endAndOr: seqNum) or: [ |
|
142 |
(self endAndOr2: seqNum) or: [ |
|
143 |
(self endIfThen: seqNum) or: [ |
|
144 |
(self endIfThen2: seqNum) or:[ |
|
145 |
(self endIfThenElse: seqNum) or: [ |
|
146 |
(self endCase: seqNum) or: [ |
|
147 |
(self endToDo: seqNum) or: [ |
|
148 |
(self endWhile: seqNum) or: [ |
|
149 |
(self endWhile2: seqNum) or: [ |
|
150 |
(self endIfNil: seqNum)]]]]]]]]]] |
|
151 |
] whileTrue. |
|
152 |
||
153 |
stack addLast: (RBPseudoLabelNode new destination: seqNum). |
|
154 |
! |
|
155 |
||
156 |
popTop |
|
157 |
||
158 |
| value | |
|
159 |
stack last ifNil: [^ stack removeLast]. "pop no-op from #simplifyTempAssign:" |
|
160 |
[stack last isLabel |
|
161 |
and: [(stack atLast:2) isGoto] |
|
162 |
and: [stack last destination = (stack atLast: 2) destination]] |
|
163 |
whileTrue: [ |
|
164 |
stack removeLast. |
|
165 |
stack removeLast]. |
|
166 |
stack last isValue ifTrue: [ |
|
167 |
(stack atLast: 2) isSequence ifTrue: [ |
|
168 |
value := stack removeLast. |
|
169 |
^ stack last addNode: value. |
|
170 |
] ifFalse: [(stack atLast: 2) isPseudo ifTrue: [ |
|
171 |
value := stack removeLast. |
|
172 |
^ stack addLast: (RBSequenceNode statements: {value}). |
|
173 |
]]. |
|
174 |
]. |
|
175 |
stack addLast: RBPseudoPopNode new |
|
176 |
! |
|
177 |
||
178 |
pushBlock: irMethod |
|
179 |
||
180 |
self block: irMethod env: nil |
|
181 |
! |
|
182 |
||
183 |
pushBlockMethod: irMethod |
|
184 |
||
185 |
"block will recognized when send: #createBlock:" |
|
186 |
self pushLiteral: irMethod |
|
187 |
! |
|
188 |
||
189 |
pushDup |
|
190 |
||
191 |
stack addLast: RBPseudoDupNode new |
|
192 |
! |
|
193 |
||
194 |
pushInstVar: index |
|
195 |
||
196 |
self stackPush: (self newVar: (scope instanceScope instVar: index)) |
|
197 |
! |
|
198 |
||
199 |
pushLiteral: object |
|
200 |
||
201 |
self stackPush: (self newLiteral: object). |
|
202 |
! |
|
203 |
||
204 |
pushLiteralVariable: object |
|
205 |
||
206 |
| var | |
|
207 |
var := scope lookupVar: object key asString. |
|
208 |
self stackPush: (self newVar: var) |
|
209 |
! |
|
210 |
||
211 |
pushTemp: tempIndex |
|
212 |
||
213 |
| var | |
|
214 |
var := scope basicTempVarAt: tempIndex. |
|
215 |
var isTemp ifTrue: [var cantBeCapture]. |
|
216 |
self stackPush: (self newVar: var). |
|
217 |
! |
|
218 |
||
219 |
remoteReturn |
|
220 |
||
221 |
stack removeLast. "pop home context free var" |
|
222 |
self goto: #return. |
|
223 |
! |
|
224 |
||
225 |
returnTop |
|
226 |
||
227 |
self goto: #return. |
|
228 |
! |
|
229 |
||
230 |
send: selector numArgs: numArgs |
|
231 |
||
232 |
| args rcvr | |
|
233 |
selector = #caseError ifTrue:[^self stackPush: (RBPseudoSendNode new selector: selector)]. |
|
234 |
args := OrderedCollection new. |
|
235 |
[ selector numArgs timesRepeat: [args addFirst: self Value]. |
|
236 |
rcvr := self Value. |
|
237 |
] on: Abort do: [ |
|
238 |
[self stackPush: (RBPseudoSendNode new selector: selector). |
|
239 |
^self cascade] on: Abort do:[^false] |
|
240 |
]. |
|
241 |
||
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
242 |
"Preferences compileBlocksAsClosures"true |
37 | 243 |
ifTrue: [ (rcvr isLiteral and: [selector = #createBlock:]) ifTrue: [ |
244 |
^ self block: rcvr value env: args first]] |
|
245 |
ifFalse: [ (selector = #blockCopy:) ifTrue: [ |
|
246 |
^ self stackPush: (RBPseudoSendNode new selector: selector; arguments: args)]]. |
|
247 |
||
248 |
self stackPush: (self simplify: (RBMessageNode new |
|
249 |
receiver: rcvr |
|
250 |
selectorParts: (self newSelectorParts: selector) |
|
251 |
arguments: args)). |
|
252 |
||
253 |
"Created: / 01-12-2008 / 19:40:52 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
254 |
"Modified: / 30-10-2014 / 22:38:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 255 |
! |
256 |
||
257 |
send: selector numArgs: numArgs toSuperOf: behavior |
|
258 |
||
259 |
| args rcvr | |
|
260 |
args := OrderedCollection new. |
|
261 |
selector numArgs timesRepeat: [args addFirst: self Value]. |
|
262 |
rcvr := self Value. |
|
263 |
(rcvr isVariable and: [rcvr name = 'self']) ifFalse: [self patternError]. |
|
264 |
||
265 |
rcvr identifierToken: (SqueakToken value: 'super' start: 0). |
|
266 |
self stackPush: (RBMessageNode new |
|
267 |
receiver: rcvr |
|
268 |
selectorParts: (self newSelectorParts: selector) |
|
269 |
arguments: args). |
|
270 |
||
271 |
"Created: / 01-12-2008 / 19:45:52 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
272 |
! |
|
273 |
||
274 |
storeIntoLiteralVariable: association |
|
275 |
||
276 |
| var | |
|
277 |
var := scope lookupVar: association key asString. |
|
278 |
self stackPush: (self simplifyTempAssign: |
|
279 |
(RBAssignmentNode variable: (self newVar: (var markWrite)) value: self Value)) |
|
280 |
! |
|
281 |
||
282 |
storeTemp: tempIndex |
|
283 |
||
284 |
| var | |
|
285 |
var := scope basicTempVarAt: tempIndex. |
|
286 |
var isCaptured ifFalse: [var cantBeCapture]. |
|
287 |
var isTemp ifTrue:[ |
|
288 |
var isArg: false]. |
|
289 |
self stackPush: (self simplifyTempAssign: |
|
290 |
(RBAssignmentNode variable: (self newVar: (var markWrite)) value: self Value)). |
|
291 |
! ! |
|
292 |
||
293 |
!IRDecompiler methodsFor:'interpret'! |
|
294 |
||
295 |
interpretInstruction: irInstruction |
|
296 |
||
297 |
currentInstr := irInstruction. |
|
298 |
super interpretInstruction: irInstruction. |
|
299 |
! |
|
300 |
||
301 |
interpretSequence: instructionSequence |
|
302 |
||
303 |
super interpretSequence: instructionSequence. |
|
304 |
"currentInstr := nil." |
|
305 |
! ! |
|
306 |
||
307 |
!IRDecompiler methodsFor:'old blocks'! |
|
308 |
||
309 |
blockReturnTop |
|
310 |
||
311 |
self goto: #return. |
|
312 |
! |
|
313 |
||
314 |
endBlock: seqNum |
|
315 |
||
316 |
| blockSeq block goto startBlock | |
|
317 |
[ |
|
318 |
goto := self GotoOrReturn: seqNum. |
|
319 |
(goto isRet |
|
320 |
or:[goto mapInstr notNil |
|
321 |
and: [goto mapInstr isBlockReturnTop]]) ifFalse: [self abort]. |
|
322 |
sp = 0 ifTrue: [self abort]. |
|
323 |
blockSeq := self Sequence2. |
|
324 |
startBlock := self Label. |
|
325 |
block := self Block. |
|
326 |
(goto isRet not |
|
327 |
and:[goto mapInstr notNil] |
|
328 |
and: [goto mapInstr isBlockReturnTop] |
|
329 |
and: [block successor ~= seqNum]) ifTrue:[ |
|
330 |
self stackPush: block. |
|
331 |
self stackPush: startBlock. |
|
332 |
self stackPush: blockSeq. |
|
333 |
self stackPush: goto. |
|
334 |
self abort]. |
|
335 |
self Send. |
|
336 |
] on: Abort do: [^ false]. |
|
337 |
||
338 |
self stackPush: (self newBlock: blockSeq return: goto). |
|
339 |
stack last arguments: block arguments. |
|
340 |
"No extra scope is need if we don't use any temporaries and arguments. |
|
341 |
so we remove them" |
|
342 |
(stack last arguments isEmpty and: [stack last body temporaries isEmpty]) |
|
343 |
ifTrue:[ASTReplaceVariableScope replace: stack last scope: scope outerScope ]. |
|
344 |
scope := scope outerScope. |
|
345 |
currentInstr := nil. |
|
346 |
self goto: block successor. |
|
347 |
^ true |
|
348 |
! |
|
349 |
||
350 |
jumpOverBlock: seqNum1 to: seqNum2 |
|
351 |
| numArgs args oldscope pseudoBlock | |
|
352 |
||
353 |
oldscope := scope. |
|
354 |
self scope: (scope newBlockScope). |
|
355 |
oldscope tempVarAt: 0. |
|
356 |
(scope addObjectTemp: (oldscope tempVarAt: 0)). |
|
357 |
numArgs := stack last arguments first value. |
|
358 |
self stackPush: (pseudoBlock := RBPseudoBlockNode new). |
|
359 |
||
360 |
args := OrderedCollection new. |
|
361 |
numArgs timesRepeat: [ | var instr | |
|
362 |
instr := currentInstr blockSequence removeFirst. |
|
363 |
var := oldscope tempVarAt: instr number. |
|
364 |
args add: (self newVar: var). |
|
365 |
var isUnused ifTrue: [oldscope removeTempFromOldBlock: var]. |
|
366 |
scope addObjectTemp: var. |
|
367 |
currentInstr blockSequence first isPop |
|
368 |
ifFalse: [ |
|
369 |
currentInstr blockSequence sequence addFirst: (IRInstruction pushTemp: var index)] |
|
370 |
ifTrue:[currentInstr blockSequence removeFirst]. |
|
371 |
||
372 |
]. |
|
373 |
args := args reverse. |
|
374 |
pseudoBlock |
|
375 |
block: seqNum1; |
|
376 |
successor: seqNum2; |
|
377 |
arguments: args |
|
378 |
||
379 |
! |
|
380 |
||
381 |
storeInstVar: number |
|
382 |
||
383 |
| var | |
|
384 |
var := scope instanceScope instVar: number. |
|
385 |
self stackPush: (RBAssignmentNode variable: (self newVar: var) value: self Value) |
|
386 |
! ! |
|
387 |
||
388 |
!IRDecompiler methodsFor:'priv instructions'! |
|
389 |
||
390 |
addReturn: statements from: goto |
|
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
391 |
"/ | | |
37 | 392 |
|
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
393 |
self error: 'IRDecompiler is an unfinished code'. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
394 |
"/ statements last isReturn ifTrue:[^self]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
395 |
"/ ret := RBReturnNode value: statements last. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
396 |
"/ Preferences compileBlocksAsClosures ifTrue:[ |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
397 |
"/ scope isHome ifFalse: [ret homeBinding: scope outerEnvScope thisEnvVar]]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
398 |
"/ goto mapInstr sourceNode: ret. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
399 |
"/ statements atLast: 1 put: ret. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
400 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
401 |
"Modified: / 30-10-2014 / 22:35:15 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 402 |
! |
403 |
||
404 |
block: method env: envRefNode |
|
405 |
||
406 |
self stackPush: (IRDecompiler new |
|
407 |
scope: (scope newBlockScope "capturedVars: vars"); |
|
408 |
decompileIR: method ir) |
|
409 |
asBlock |
|
410 |
! |
|
411 |
||
412 |
cascade |
|
413 |
||
414 |
| messages selector args rcvr | |
|
415 |
messages := OrderedCollection new. |
|
416 |
"last message" |
|
417 |
selector _ self Send selector. |
|
418 |
args := OrderedCollection new. |
|
419 |
selector numArgs timesRepeat: [args addFirst: self Value]. |
|
420 |
messages addFirst: selector -> args. |
|
421 |
||
422 |
"rest of messages" |
|
423 |
[(rcvr := self ValueOrNone) isNil] whileTrue: [ |
|
424 |
self Pop. |
|
425 |
selector := self Send selector. |
|
426 |
args := OrderedCollection new. |
|
427 |
selector numArgs timesRepeat: [args addFirst: self Value]. |
|
428 |
self Dup. |
|
429 |
messages addFirst: selector -> args. |
|
430 |
]. |
|
431 |
||
432 |
messages := messages collect: [:assoc | |
|
433 |
RBMessageNode |
|
434 |
receiver: rcvr |
|
435 |
selector: assoc key |
|
436 |
arguments: assoc value]. |
|
437 |
self stackPush: (RBCascadeNode messages: messages). |
|
438 |
! |
|
439 |
||
440 |
endAndOr2: seqNum |
|
441 |
||
442 |
| goto seq p if2 test else o if1 seqValue elseTest otherwise | |
|
443 |
[ |
|
444 |
goto _ self Goto. |
|
445 |
seqValue _ self ValueOrNone. |
|
446 |
seq _ self Sequence. |
|
447 |
p _ self Label destination. |
|
448 |
if2 _ self IfGoto: seqNum otherwise: p. |
|
449 |
elseTest _ self Value. |
|
450 |
else _ self SequenceBackTo: goto destination. |
|
451 |
o _ self Label destination. |
|
452 |
o = goto destination ifTrue: [self abort]. |
|
453 |
if1 _ self IfGoto: seqNum otherwise: o. |
|
454 |
test _ self Value. |
|
455 |
] on: Abort do: [^ false]. |
|
456 |
||
457 |
if1 boolean = if2 boolean |
|
458 |
ifFalse: [ |
|
459 |
otherwise := RBSequenceNode statements: #(). |
|
460 |
otherwise addNode: (self newLiteral: if2 boolean). |
|
461 |
self stackPush: (RBMessageNode |
|
462 |
receiver: test |
|
463 |
selector: (if2 boolean ifTrue: [#ifTrue:ifFalse:] ifFalse: [#ifFalse:ifTrue:]) |
|
464 |
arguments: {self newBlock: (else addNode: elseTest). |
|
465 |
self newBlock: otherwise}).] |
|
466 |
ifTrue:[self stackPush: (RBMessageNode |
|
467 |
receiver: test |
|
468 |
selector: (if2 boolean ifTrue: [#or:] ifFalse: [#and:]) |
|
469 |
arguments: {self newBlock: (else addNode: elseTest)})]. |
|
470 |
stack addLast: if2. |
|
471 |
self label: p. |
|
472 |
stack addLast: seq. |
|
473 |
seqValue ifNotNil: [stack addLast: seqValue]. |
|
474 |
stack addLast: goto. |
|
475 |
^ true |
|
476 |
! |
|
477 |
||
478 |
endAndOr: seqNum |
|
479 |
||
480 |
| o test branches if body block sel1 sel2 if2 | |
|
481 |
branches := OrderedCollection new. |
|
482 |
[ |
|
483 |
(if2 := self If) otherwise = seqNum ifFalse: [self abort]. |
|
484 |
[ test := self Value. |
|
485 |
body := self Sequence. |
|
486 |
branches add: {body. test}. |
|
487 |
o := self Label destination. |
|
488 |
(if := self If) otherwise = o ifFalse: [self abort]. |
|
489 |
if destination = seqNum |
|
490 |
] whileFalse: [ |
|
491 |
if boolean = if2 boolean ifFalse: [self abort]. |
|
492 |
if destination = if2 destination ifFalse: [self abort]. |
|
493 |
]. |
|
494 |
if boolean = if2 boolean ifTrue: [self abort]. |
|
495 |
test := self Value. |
|
496 |
] on: Abort do: [^ false]. |
|
497 |
||
498 |
if boolean |
|
499 |
ifTrue: [sel1 := #or:. sel2 := #and:] |
|
500 |
ifFalse: [sel1 := #and:. sel2 := #or:]. |
|
501 |
block := self newBlock: (branches first first addNode: branches first second). |
|
502 |
branches allButFirstDo: [:pair | |
|
503 |
block := self newBlock: (pair first addNode: (RBMessageNode |
|
504 |
receiver: pair second |
|
505 |
selector: sel2 |
|
506 |
arguments: {block})). |
|
507 |
]. |
|
508 |
self stackPush: (RBMessageNode |
|
509 |
receiver: test |
|
510 |
selector: sel1 |
|
511 |
arguments: {block}). |
|
512 |
stack addLast: if2. |
|
513 |
^ true |
|
514 |
! |
|
515 |
||
516 |
endCase: seqNum |
|
517 |
||
518 |
| otherwiseGoto goto node otherwiseValue otherwiseSeq n branchValue branchSeq f caseValue caseSeq rcvr branches message seqEnd afterOterwise seq afterOterwiseValue | |
|
519 |
branches := OrderedCollection new. |
|
520 |
[ "otherwise" |
|
521 |
otherwiseGoto := self Goto. |
|
522 |
node := self stackDown. |
|
523 |
node isSequence ifTrue: [(node statements size = 1 |
|
524 |
and:[node statements first isSend] |
|
525 |
and: [ |
|
526 |
node := node statements first. |
|
527 |
node selector == #caseError]) ifFalse: [ |
|
528 |
otherwiseSeq := node] ]. |
|
529 |
(node isPop or: [node isSend and: [node selector == #caseError]]) ifTrue: [ |
|
530 |
node isPop ifTrue: [node := self Send]. |
|
531 |
node selector == #caseError ifFalse: [self abort]. |
|
532 |
] ifFalse: [ |
|
533 |
sp := sp + 1. "stackUp" |
|
534 |
||
535 |
seqNum == #lastReturn |
|
536 |
ifFalse: [ |
|
537 |
otherwiseValue := self ValueOrNone. |
|
538 |
otherwiseSeq := self Sequence] |
|
539 |
ifTrue: [ |
|
540 |
afterOterwiseValue := self ValueOrNone. |
|
541 |
otherwiseSeq := RBSequenceNode statements: #(). |
|
542 |
afterOterwise := self SequenceOtherwise]. |
|
543 |
]. |
|
544 |
n := self Label destination. |
|
545 |
"last case branch" |
|
546 |
seqNum == #lastReturn |
|
547 |
ifFalse: [goto := self GotoOrReturn: seqNum] |
|
548 |
ifTrue: [ |
|
549 |
seqEnd := n. |
|
550 |
goto := self GotoOrReturn: n. |
|
551 |
otherwiseGoto := goto]. |
|
552 |
branchValue := self ValueOrNone. |
|
553 |
branchSeq := self Sequence. |
|
554 |
(stack at: sp) isPop ifTrue: [self stackDown]. |
|
555 |
f := self Label destination. |
|
556 |
||
557 |
"last case" |
|
558 |
self IfGoto: n otherwise: f. |
|
559 |
self Send selector == #= ifFalse: [self abort]. |
|
560 |
caseValue := self Value. |
|
561 |
caseSeq := self Sequence. |
|
562 |
otherwiseSeq ifNil: [self Dup]. |
|
563 |
branches addFirst: ({caseSeq. caseValue} -> {branchSeq. branchValue. goto}). |
|
564 |
||
565 |
[(rcvr := self ValueOrNone) isNil] whileTrue: [ |
|
566 |
"case branch" |
|
567 |
n := self Label destination. |
|
568 |
seqNum == #lastReturn |
|
569 |
ifFalse: [goto := self GotoOrReturn: seqNum] |
|
570 |
ifTrue: [goto := self GotoOrReturn: seqEnd]. |
|
571 |
branchValue := self ValueOrNone. |
|
572 |
branchSeq := self Sequence. |
|
573 |
self Pop. |
|
574 |
f := self Label destination. |
|
575 |
"case" |
|
576 |
self IfGoto: n otherwise: f. |
|
577 |
self Send selector == #= ifFalse: [self abort]. |
|
578 |
caseValue := self Value. |
|
579 |
caseSeq := self Sequence. |
|
580 |
self Dup. |
|
581 |
branches addFirst: ({caseSeq. caseValue} -> {branchSeq. branchValue. goto}). |
|
582 |
]. |
|
583 |
] on: Abort do: [^ false]. |
|
584 |
||
585 |
branches := branches collect: [:assoc | |
|
586 |
assoc key second |
|
587 |
ifNotNil: [assoc key first addNode: assoc key second]. |
|
588 |
assoc value second |
|
589 |
ifNotNil: [assoc value first addNode: assoc value second]. |
|
590 |
RBMessageNode |
|
591 |
receiver: (self newBlock: assoc key first return: nil) |
|
592 |
selector: #-> |
|
593 |
arguments: |
|
594 |
{self newBlock: assoc value first return: assoc value third} |
|
595 |
]. |
|
596 |
message := otherwiseSeq |
|
597 |
ifNil: [ |
|
598 |
RBMessageNode |
|
599 |
receiver: rcvr |
|
600 |
selector: #caseOf: |
|
601 |
arguments: {RBArrayNode statements: branches}] |
|
602 |
ifNotNil: [ |
|
603 |
otherwiseValue |
|
604 |
ifNotNil: [otherwiseSeq addNode: otherwiseValue]. |
|
605 |
RBMessageNode |
|
606 |
receiver: rcvr |
|
607 |
selector: #caseOf:otherwise: |
|
608 |
arguments: |
|
609 |
{RBArrayNode statements: branches. |
|
610 |
self newBlock: otherwiseSeq return: otherwiseGoto}. |
|
611 |
]. |
|
612 |
self stackPush: message. |
|
613 |
seqNum == #lastReturn ifTrue: [ |
|
614 |
self popTop. |
|
615 |
seq := self Sequence. |
|
616 |
afterOterwise ifNotNil:[seq statements addAllLast: afterOterwise statements]. |
|
617 |
self stackPush: seq. |
|
618 |
afterOterwiseValue ifNotNil:[self stackPush: afterOterwiseValue]. |
|
619 |
branchValue := 1]. |
|
620 |
branchValue ifNil: [self popTop]. |
|
621 |
self stackPush: otherwiseGoto. |
|
622 |
^ true |
|
623 |
! |
|
624 |
||
625 |
endIfNil: seqNum |
|
626 |
||
627 |
| goto branch o if rcvr value | |
|
628 |
[ |
|
629 |
goto := self Goto. |
|
630 |
value := self Value. |
|
631 |
branch := self Sequence. |
|
632 |
self Pop. |
|
633 |
o := self Label destination. |
|
634 |
if := self IfGoto: seqNum otherwise: o. |
|
635 |
self Send selector == #== ifFalse: [self abort]. |
|
636 |
(self Value isLiteral: [:v | v isNil]) ifFalse: [self abort]. |
|
637 |
self Dup. |
|
638 |
rcvr := self Value. |
|
639 |
] on: Abort do: [^ false]. |
|
640 |
||
641 |
branch addNode: value. |
|
642 |
self stackPush: (RBMessageNode |
|
643 |
receiver: rcvr |
|
644 |
selector: (if boolean ifTrue: [#ifNotNil:] ifFalse: [#ifNil:]) |
|
645 |
arguments: {self newBlock: branch return: goto}). |
|
646 |
self goto: seqNum. |
|
647 |
^ true |
|
648 |
! |
|
649 |
||
650 |
endIfThen2: seqNum |
|
651 |
||
652 |
| goto branch o if test value gotoNum branch2 | |
|
653 |
[ |
|
654 |
goto := self Goto. |
|
655 |
(goto mapInstr ~= nil |
|
656 |
and: [goto mapInstr isJump] |
|
657 |
and: [goto mapInstr destination size = 1] |
|
658 |
and: [goto mapInstr destination last isJump]) |
|
659 |
ifTrue: [gotoNum := goto |
|
660 |
mapInstr destination last destination orderNumber] |
|
661 |
ifFalse:[self abort]. |
|
662 |
(currentInstr ~= nil |
|
663 |
and: [currentInstr isJump] |
|
664 |
and: [currentInstr destination orderNumber = goto destination]) |
|
665 |
ifFalse: [self abort]. |
|
666 |
value := self Value. |
|
667 |
branch := self Sequence. |
|
668 |
o := self Label destination. |
|
669 |
seqNum = gotoNum |
|
670 |
ifFalse:[if := self IfGoto: gotoNum otherwise: o] |
|
671 |
ifTrue:[self abort]. |
|
672 |
test := self Value. |
|
673 |
] on: Abort do: [^ false]. |
|
674 |
||
675 |
value ifNotNil: [branch addNode: value]. |
|
676 |
branch2 := RBSequenceNode statements: #(). |
|
677 |
branch2 addNode: (self newLiteral: if boolean). |
|
678 |
self stackPush: (self simplify: (RBMessageNode |
|
679 |
receiver: test |
|
680 |
selector: (if boolean ifTrue: [#ifFalse:ifTrue:] ifFalse: [#ifTrue:ifFalse:]) |
|
681 |
arguments: {self newBlock: branch return: goto. |
|
682 |
self newBlock: branch2})). |
|
683 |
self goto: goto destination. |
|
684 |
^true |
|
685 |
! |
|
686 |
||
687 |
endIfThen3: seqNum |
|
688 |
||
689 |
| goto branch o if test value | |
|
690 |
[ |
|
691 |
goto := self Goto. |
|
692 |
(goto destination == seqNum or: [self isExplicitReturn: goto]) |
|
693 |
ifFalse: [self abort]. |
|
694 |
goto isRet ifTrue: [value := self Value]. |
|
695 |
branch := self Sequence. |
|
696 |
o := self Label destination. |
|
697 |
if := self If. |
|
698 |
((if destination = seqNum |
|
699 |
or: [if destination = (mapEmptyStatement at: seqNum ifAbsent:[seqNum])]) |
|
700 |
and: [if otherwise = o]) |
|
701 |
ifFalse:[self abort]. |
|
702 |
test := self Value. |
|
703 |
] on: Abort do: [^ false]. |
|
704 |
||
705 |
||
706 |
value ifNotNil: [branch addNode: value]. |
|
707 |
self stackPush: (self simplify: (RBMessageNode |
|
708 |
receiver: test |
|
709 |
selector: (if boolean ifTrue: [#ifFalse:] ifFalse: [#ifTrue:]) |
|
710 |
arguments: {self newBlock: branch return: goto})). |
|
711 |
self popTop. |
|
712 |
self goto: seqNum. |
|
713 |
^ true |
|
714 |
! |
|
715 |
||
716 |
endIfThen: seqNum |
|
717 |
||
718 |
| goto branch o if test value | |
|
719 |
[ |
|
720 |
goto := self Goto. |
|
721 |
(goto destination == seqNum or: [self isExplicitReturn: goto]) |
|
722 |
ifFalse: [self abort]. |
|
723 |
goto isRet ifTrue: [value := self Value]. |
|
724 |
branch := self Sequence. |
|
725 |
o := self Label destination. |
|
726 |
if := self IfGoto: seqNum otherwise: o. |
|
727 |
test := self Value. |
|
728 |
] on: Abort do: [^ false]. |
|
729 |
||
730 |
||
731 |
value ifNotNil: [branch addNode: value]. |
|
732 |
self stackPush: (self simplify: (RBMessageNode |
|
733 |
receiver: test |
|
734 |
selector: (if boolean ifTrue: [#ifFalse:] ifFalse: [#ifTrue:]) |
|
735 |
arguments: {self newBlock: branch return: goto})). |
|
736 |
self popTop. |
|
737 |
self goto: seqNum. |
|
738 |
^ true |
|
739 |
! |
|
740 |
||
741 |
endIfThenElse: seqNum |
|
742 |
||
743 |
| goto2 else d goto1 then o if test value2 value1 | |
|
744 |
[ |
|
745 |
goto2 := self Goto. |
|
746 |
value2 := self ValueOrNone. |
|
747 |
else := self Sequence. |
|
748 |
d := self Label destination. |
|
749 |
goto1 := self Goto. |
|
750 |
((self isExplicitReturn: goto2) or: [goto2 destination == goto1 destination]) ifFalse: [self abort]. |
|
751 |
value1 := self ValueOrNone. |
|
752 |
then := self Sequence. |
|
753 |
o := self Label destination. |
|
754 |
if := self IfGoto: d otherwise: o. |
|
755 |
test := self Value. |
|
756 |
] on: Abort do: [^ false]. |
|
757 |
||
758 |
value2 ifNotNil: [else addNode: value2]. |
|
759 |
value1 ifNotNil: [then addNode: value1]. |
|
760 |
(self isExplicitReturn: goto1) ifTrue:[self addReturn: then statements from: goto1]. |
|
761 |
(self isExplicitReturn: goto2) ifTrue:[self addReturn: else statements from: goto2]. |
|
762 |
self stackPush: (self simplify: (else isEmpty |
|
763 |
ifTrue: [RBMessageNode |
|
764 |
receiver: test |
|
765 |
selector: (if boolean ifTrue: [#ifFalse:] ifFalse: [#ifTrue:]) |
|
766 |
arguments: {self newBlock: then return: goto1}] |
|
767 |
ifFalse: [RBMessageNode |
|
768 |
receiver: test |
|
769 |
selector: (if boolean |
|
770 |
ifTrue: [#ifFalse:ifTrue:] |
|
771 |
ifFalse: [#ifTrue:ifFalse:]) |
|
772 |
arguments: { |
|
773 |
self newBlock: then return: goto1. |
|
774 |
self newBlock: else return: goto2}])). |
|
775 |
value1 ifNil: [self popTop]. |
|
776 |
currentInstr := goto1 mapInstr. |
|
777 |
self stackPush: goto1. |
|
778 |
(else statements isEmpty and: |
|
779 |
[stack anySatisfy: [:n | n isIf and: [n destination = d]]] |
|
780 |
) ifTrue: [ |
|
781 |
self label: d. |
|
782 |
currentInstr := goto2 mapInstr. |
|
783 |
self stackPush: goto2. |
|
784 |
]. |
|
785 |
^ true |
|
786 |
! |
|
787 |
||
788 |
endToDo: seqNum |
|
789 |
||
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
790 |
| start limit incr iter step loopBlock o if test limitExpr init | |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
791 |
[ |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
792 |
start := self Goto destination. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
793 |
limit := self Value. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
794 |
incr := self Assignment. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
795 |
iter := incr variable. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
796 |
(incr value isMessage and: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
797 |
[incr value selector == #+ and: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
798 |
[incr value receiver isVariable and: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
799 |
[incr value receiver binding == iter binding]]] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
800 |
) ifFalse: [self abort]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
801 |
step := incr value arguments first. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
802 |
loopBlock := self Sequence. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
803 |
o := self Label destination. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
804 |
if := self IfGoto: seqNum otherwise: o. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
805 |
test := self Value. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
806 |
(test isMessage and: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
807 |
[(test selector == #<= or: [test selector == #>=]) and: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
808 |
[(valueLabelMap at: test arguments first ifAbsent: [self abort]) destination = start]] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
809 |
) ifFalse: [self abort]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
810 |
limitExpr := test arguments first. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
811 |
limitExpr isAssignment ifTrue: [ |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
812 |
(limitExpr variable binding index == limit binding index |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
813 |
and:[limitExpr variable binding scope == limit binding scope]) ifFalse: [self abort]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
814 |
limitExpr := limitExpr value. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
815 |
]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
816 |
init := test receiver. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
817 |
(init isAssignment and: [init variable binding == iter binding]) |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
818 |
ifFalse: [self abort]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
819 |
] on: Abort do: [^ false]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
820 |
limit isVariable |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
821 |
ifTrue:[scope |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
822 |
removeTemp: limit binding |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
823 |
ifAbsent:["Preferences compileBlocksAsClosures"true |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
824 |
ifFalse:[scope removeTempFromOldBlock: limit]]]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
825 |
loopBlock := self newBlock: loopBlock. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
826 |
loopBlock arguments: {iter}. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
827 |
self stackPush: ((step isLiteral: [:c | c = 1]) |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
828 |
ifTrue: [RBMessageNode |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
829 |
receiver: init value |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
830 |
selector: #to:do: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
831 |
arguments: {limitExpr. loopBlock}] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
832 |
ifFalse: [RBMessageNode |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
833 |
receiver: init value |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
834 |
selector: #to:by:do: |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
835 |
arguments: {limitExpr. step. loopBlock}]). |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
836 |
self popTop. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
837 |
self goto: seqNum. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
838 |
^ true |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
839 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
840 |
"Modified: / 30-10-2014 / 22:37:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 841 |
! |
842 |
||
843 |
endWhile2: seqNum |
|
844 |
||
845 |
| start loopBlock if test sequence o goto previousStack | |
|
846 |
[ |
|
847 |
stack := (previousStack := stack) copy. |
|
848 |
start := (goto := self Goto) destination. |
|
849 |
self stackPush: goto. |
|
850 |
[self endIfThen3: start] whileTrue. |
|
851 |
start := self Goto destination. |
|
852 |
loopBlock _ self Sequence. |
|
853 |
o _ self Label destination. |
|
854 |
if _ self IfGoto: seqNum otherwise: o. |
|
855 |
test _ self Value. |
|
856 |
sequence _ self SequenceBackTo: start. |
|
857 |
self Label: start. |
|
858 |
sp _ sp + 1. "stackUp" |
|
859 |
] on: Abort do: [stack := previousStack. ^ false]. |
|
860 |
loopBlock isEmpty |
|
861 |
ifTrue:[self stackPush: (self simplify: (RBMessageNode |
|
862 |
receiver: (self newBlock: (sequence addNode: test)) |
|
863 |
selector: (if boolean ifTrue: [#whileFalse] ifFalse: [#whileTrue]) |
|
864 |
arguments: #()))] |
|
865 |
ifFalse:[self stackPush: (self simplify: (RBMessageNode |
|
866 |
receiver: (self newBlock: (sequence addNode: test)) |
|
867 |
selector: (if boolean ifTrue: [#whileFalse:] ifFalse: [#whileTrue:]) |
|
868 |
arguments: {self newBlock: loopBlock}))]. |
|
869 |
self popTop. |
|
870 |
self goto: seqNum. |
|
871 |
^ true |
|
872 |
! |
|
873 |
||
874 |
endWhile: seqNum |
|
875 |
||
876 |
| start loopBlock if test sequence o | |
|
877 |
[ |
|
878 |
start _ self Goto destination. |
|
879 |
loopBlock _ self Sequence. |
|
880 |
o _ self Label destination. |
|
881 |
if _ self IfGoto: seqNum otherwise: o. |
|
882 |
test _ self Value. |
|
883 |
sequence _ self SequenceBackTo: start. |
|
884 |
self Label: start. |
|
885 |
sp _ sp + 1. "stackUp" |
|
886 |
] on: Abort do: [^ false]. |
|
887 |
loopBlock isEmpty |
|
888 |
ifTrue:[self stackPush: (self simplify: (RBMessageNode |
|
889 |
receiver: (self newBlock: (sequence addNode: test)) |
|
890 |
selector: (if boolean ifTrue: [#whileFalse] ifFalse: [#whileTrue]) |
|
891 |
arguments: #()))] |
|
892 |
ifFalse:[self stackPush: (self simplify: (RBMessageNode |
|
893 |
receiver: (self newBlock: (sequence addNode: test)) |
|
894 |
selector: (if boolean ifTrue: [#whileFalse:] ifFalse: [#whileTrue:]) |
|
895 |
arguments: {self newBlock: loopBlock}))]. |
|
896 |
self popTop. |
|
897 |
self goto: seqNum. |
|
898 |
^ true |
|
899 |
! ! |
|
900 |
||
901 |
!IRDecompiler methodsFor:'private'! |
|
902 |
||
903 |
captureEmptyStatement |
|
904 |
| by replace node | |
|
905 |
||
906 |
[by := self Goto destination. |
|
907 |
replace := self Label destination. |
|
908 |
replace = 0 ifTrue: [self abort]] |
|
909 |
on: Abort |
|
910 |
do: [^ false]. |
|
911 |
mapEmptyStatement at: by put: replace. |
|
912 |
sp := nil. |
|
913 |
^ true |
|
914 |
! |
|
915 |
||
916 |
fixInnerFreeVar: aRcvrTemp |
|
917 |
||
918 |
| scopeInnerFreeVar | |
|
919 |
scopeInnerFreeVar := scope outerScope. |
|
920 |
[aRcvrTemp scope = scopeInnerFreeVar] whileFalse:[ |
|
921 |
scopeInnerFreeVar hasInnerFreeVars: true. |
|
922 |
scopeInnerFreeVar := scopeInnerFreeVar outerScope]. |
|
923 |
aRcvrTemp scope hasInnerFreeVars: true |
|
924 |
! |
|
925 |
||
926 |
initialize |
|
927 |
||
928 |
stack := OrderedCollection new. |
|
929 |
scope := nil parseScope newMethodScope. "in case never set" |
|
930 |
valueLabelMap := IdentityDictionary new. |
|
931 |
mapEmptyStatement := IdentityDictionary new |
|
932 |
! |
|
933 |
||
934 |
isExplicitReturn: goto |
|
935 |
||
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
936 |
"Preferences compileBlocksAsClosures"true |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
937 |
ifTrue:[^ goto isRet |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
938 |
and: [goto mapInstr notNil] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
939 |
and: [goto mapInstr isRemote or: [scope isBlockScope not]]] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
940 |
ifFalse: [^goto isRet and: [goto mapInstr isBlockReturnTop not]] |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
941 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
942 |
"Modified: / 30-10-2014 / 22:37:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 943 |
! |
944 |
||
945 |
mapNode: node |
|
946 |
||
947 |
currentInstr ifNil: [^ self]. |
|
948 |
node isPseudo |
|
949 |
ifTrue: [node mapInstr: currentInstr] |
|
950 |
ifFalse: [currentInstr sourceNode: node] |
|
951 |
! |
|
952 |
||
953 |
newBlock: sequence |
|
954 |
||
955 |
^ self newBlock: sequence return: nil |
|
956 |
! |
|
957 |
||
958 |
newBlock: sequence return: goto |
|
959 |
||
45
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
960 |
| statements block | |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
961 |
statements := sequence statements. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
962 |
(goto notNil and: [self isExplicitReturn: goto]) ifTrue: [ |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
963 |
self addReturn: statements from: goto |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
964 |
]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
965 |
sequence statements: statements. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
966 |
block := RBBlockNode body: sequence. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
967 |
sequence parent: block. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
968 |
"Preferences compileBlocksAsClosures"true ifFalse: [block scope: scope]. |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
969 |
^block |
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
970 |
|
04a50b0d540a
Reduced dependencies to only stx:libbasic and stx:libcomp.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
43
diff
changeset
|
971 |
"Modified: / 30-10-2014 / 22:37:39 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
37 | 972 |
! |
973 |
||
974 |
newLiteral: literal |
|
975 |
||
976 |
^ RBLiteralNode value: literal |
|
977 |
! |
|
978 |
||
979 |
newSelectorParts: selector |
|
980 |
||
981 |
^ selector keywords collect: [:word | |
|
982 |
RBLiteralToken value: word] |
|
983 |
! |
|
984 |
||
985 |
newVar: semVar |
|
986 |
||
987 |
^ RBVariableNode new |
|
988 |
identifierToken: (RBIdentifierToken value: semVar name start: 0); |
|
989 |
binding: semVar |
|
990 |
! |
|
991 |
||
992 |
simplify: mess |
|
993 |
"mess is a messageNode. If it is a message created by the compiler convert it back to its normal form" |
|
994 |
||
995 |
| rcvr var | |
|
996 |
" (mess selector == #value and: [mess receiver isLiteral]) ifTrue: [ |
|
997 |
^ self newVar: (GlobalVar new assoc: mess receiver value; scope: scope) |
|
998 |
]." |
|
999 |
||
1000 |
(mess selector = #privSetInHolder: and: [mess arguments first isLiteral]) ifTrue: [ |
|
1001 |
^ RBAssignmentNode |
|
1002 |
variable: (self newVar: (GlobalVar new assoc: mess arguments first value; scope: scope) markWrite) |
|
1003 |
value: mess receiver |
|
1004 |
]. |
|
1005 |
||
1006 |
(mess selector = #privGetInstVar: and: |
|
1007 |
[mess arguments first isLiteral and: |
|
1008 |
[mess receiver isVariable]]) ifTrue: [ |
|
1009 |
rcvr := mess receiver binding. |
|
1010 |
rcvr == scope receiverVar ifTrue: [ |
|
1011 |
^ self newVar: (scope receiverVarAt: mess arguments first value)]. |
|
1012 |
(rcvr isContextVar and: [mess arguments first value == 5]) ifTrue: [ |
|
1013 |
var := scope tempVarAt: -1. |
|
1014 |
^self newVar: var]. |
|
1015 |
(rcvr isCaptured and:[rcvr sourceTemp = rcvr scope receiverVar]) |
|
1016 |
ifTrue:[ |
|
1017 |
self fixInnerFreeVar: rcvr. |
|
1018 |
^self newVar: (rcvr scope receiverVarAt: mess arguments first value)]. |
|
1019 |
rcvr isEnv ifTrue: [^self newVar: (rcvr scope captureVarAt: mess arguments first value)]]. |
|
1020 |
||
1021 |
(mess selector = #privStoreIn:instVar: and: |
|
1022 |
[mess arguments last isLiteral and: |
|
1023 |
[mess arguments first isVariable]]) ifTrue: [ |
|
1024 |
rcvr := mess arguments first binding. |
|
1025 |
(mess receiver name = 'self' and: [rcvr isEnv]) |
|
1026 |
ifTrue:[scope captureSelf: mess arguments last value. |
|
1027 |
^mess]. |
|
1028 |
rcvr == scope receiverVar ifTrue: [^ RBAssignmentNode |
|
1029 |
variable: (self newVar: (scope receiverVarForAssignmentAt: mess arguments last value) markWrite) |
|
1030 |
value: mess receiver]. |
|
1031 |
(rcvr isCaptured and:[rcvr sourceTemp = rcvr scope receiverVar]) |
|
1032 |
ifTrue:[ |
|
1033 |
self fixInnerFreeVar: rcvr. |
|
1034 |
^RBAssignmentNode |
|
1035 |
variable: (self newVar: (rcvr scope receiverVarForAssignmentAt: mess arguments last value) markWrite) |
|
1036 |
value: mess receiver]. |
|
1037 |
mess isClosureEnvironmentRegistration |
|
1038 |
ifTrue: [ |
|
1039 |
scope captureSelf: mess arguments last value. |
|
1040 |
^mess]. |
|
1041 |
rcvr isEnv ifTrue:[ |
|
1042 |
mess receiver isTemp |
|
1043 |
ifTrue:[var := (scope |
|
1044 |
captureVarAt: mess arguments last value |
|
1045 |
sourceTemp: mess receiver binding) markWrite.] |
|
1046 |
ifFalse:[var := (scope |
|
1047 |
captureVarAt: mess arguments last value sourceTemp: ((TempVar new) |
|
1048 |
name: (scope captureVarName: mess arguments last value); |
|
1049 |
index: mess arguments last value; |
|
1050 |
scope: self; |
|
1051 |
cantBeCapture)) markWrite |
|
1052 |
]. |
|
1053 |
^ RBAssignmentNode |
|
1054 |
variable: (self newVar: var) |
|
1055 |
value: mess receiver]]. |
|
1056 |
^mess |
|
1057 |
! |
|
1058 |
||
1059 |
simplifyTempAssign: assignment |
|
1060 |
"If it is a assignment created by the compiler convert it back to its normal form" |
|
1061 |
||
1062 |
| mess | |
|
1063 |
((mess := assignment value) isMessage and: |
|
1064 |
[mess selector = #wrapInTempHolder and: |
|
1065 |
[mess receiver isLiteral: [:v | v isNil]]] |
|
1066 |
) ifTrue: [ |
|
1067 |
^ nil "no-op" |
|
1068 |
]. |
|
1069 |
||
1070 |
^ assignment |
|
1071 |
! ! |
|
1072 |
||
1073 |
!IRDecompiler methodsFor:'stack'! |
|
1074 |
||
1075 |
Assignment |
|
1076 |
||
1077 |
| node | |
|
1078 |
(node := self stackDown) isAssignment ifTrue: [^ node]. |
|
1079 |
self abort |
|
1080 |
! |
|
1081 |
||
1082 |
Block |
|
1083 |
||
1084 |
| node | |
|
1085 |
(node := self stackDown) isBlock ifTrue: [^ node]. |
|
1086 |
self abort |
|
1087 |
! |
|
1088 |
||
1089 |
Dup |
|
1090 |
||
1091 |
| node | |
|
1092 |
(node := self stackDown) isDup ifTrue: [^ node]. |
|
1093 |
self abort |
|
1094 |
! |
|
1095 |
||
1096 |
Goto |
|
1097 |
||
1098 |
| node | |
|
1099 |
(node := self stackDown) isGoto ifTrue: [^ node]. |
|
1100 |
self abort |
|
1101 |
! |
|
1102 |
||
1103 |
Goto: seqNum |
|
1104 |
||
1105 |
| goto | |
|
1106 |
(goto := self Goto) destination = seqNum ifTrue: [^ goto]. |
|
1107 |
self abort |
|
1108 |
! |
|
1109 |
||
1110 |
GotoOrReturn: seqNum |
|
1111 |
||
1112 |
| goto | |
|
1113 |
goto := self Goto. |
|
1114 |
(goto destination = seqNum or: [goto isRet]) ifTrue: [^ goto]. |
|
1115 |
self abort |
|
1116 |
! |
|
1117 |
||
1118 |
If |
|
1119 |
||
1120 |
| node | |
|
1121 |
(node := self stackDown) isIf ifTrue: [^ node]. |
|
1122 |
self abort |
|
1123 |
! |
|
1124 |
||
1125 |
IfGoto: seqNum otherwise: seqNum2 |
|
1126 |
||
1127 |
| if | |
|
1128 |
((if := self If) destination = seqNum and: [if otherwise = seqNum2]) |
|
1129 |
ifTrue: [^ if]. |
|
1130 |
self abort |
|
1131 |
! |
|
1132 |
||
1133 |
Label |
|
1134 |
||
1135 |
| node | |
|
1136 |
(node := self stackDown) isLabel ifTrue: [^ node]. |
|
1137 |
self abort |
|
1138 |
! |
|
1139 |
||
1140 |
Label: seqNum |
|
1141 |
||
1142 |
| label | |
|
1143 |
(label := self Label) destination = seqNum ifTrue: [^ label]. |
|
1144 |
self abort |
|
1145 |
! |
|
1146 |
||
1147 |
Pop |
|
1148 |
||
1149 |
| node | |
|
1150 |
(node := self stackDown) isPop ifTrue: [^ node]. |
|
1151 |
self abort |
|
1152 |
! |
|
1153 |
||
1154 |
Send |
|
1155 |
||
1156 |
| node | |
|
1157 |
(node := self stackDown) isPseudoSend ifTrue: [^ node]. |
|
1158 |
self abort |
|
1159 |
! |
|
1160 |
||
1161 |
Sequence |
|
1162 |
| node seq i goto | |
|
1163 |
seq := RBSequenceNode statements: #(). |
|
1164 |
i := self spIndex. |
|
1165 |
[node := stack at: i. |
|
1166 |
node isSequence |
|
1167 |
ifTrue: |
|
1168 |
[seq addNodesFirst: node statements. |
|
1169 |
node := stack at: (i := i - 1)]. |
|
1170 |
(node isLabel and: [i > 1]) |
|
1171 |
ifFalse: |
|
1172 |
[sp := i. |
|
1173 |
^ seq]. |
|
1174 |
goto := stack at: (i := i - 1). |
|
1175 |
goto isGoto and: [goto destination = node destination]] |
|
1176 |
whileTrue: [i := i - 1]. |
|
1177 |
sp := i + 1. |
|
1178 |
^ seq |
|
1179 |
! |
|
1180 |
||
1181 |
Sequence2 |
|
1182 |
| node seq i block temps label | |
|
1183 |
seq := RBSequenceNode statements: #(). |
|
1184 |
i := self spIndex. |
|
1185 |
node := stack at: i. |
|
1186 |
[(node isLabel and: [(stack at: i - 1) isGoto] and:[node destination = (stack at: i - 1) destination]) |
|
1187 |
ifTrue:[ |
|
1188 |
i := i - 2. |
|
1189 |
node := stack at: i]. |
|
1190 |
(node isLabel not and: [i > 1])] whileTrue: |
|
1191 |
[ |
|
1192 |
node isSequence |
|
1193 |
ifTrue: [seq addNodesFirst: node statements] |
|
1194 |
ifFalse: [seq addNodeFirst: node]. |
|
1195 |
i := i - 1. |
|
1196 |
node := stack at: i]. |
|
1197 |
sp := i. |
|
1198 |
label := self Label. |
|
1199 |
block := self Block. |
|
1200 |
self stackPush: block. |
|
1201 |
self stackPush: label. |
|
1202 |
"Add the temporaries find" |
|
1203 |
temps := scope tempVars asArray allButFirst. |
|
1204 |
temps := temps select: [:each | ((block arguments |
|
1205 |
collect: [:var | var binding]) includes: each) not]. |
|
1206 |
seq temporaries: (temps collect: [:var | self newVar: var]). |
|
1207 |
^ seq |
|
1208 |
! |
|
1209 |
||
1210 |
SequenceBackTo: labelNum |
|
1211 |
| node seq i goto | |
|
1212 |
seq := RBSequenceNode statements: #(). |
|
1213 |
i := self spIndex. |
|
1214 |
[node := stack at: i. |
|
1215 |
node isSequence |
|
1216 |
ifTrue: |
|
1217 |
[seq addNodesFirst: node statements. |
|
1218 |
node := stack at: (i := i - 1)]. |
|
1219 |
(node isLabel and: [i > 1]) |
|
1220 |
ifFalse: |
|
1221 |
[sp := i. |
|
1222 |
^ seq]. |
|
1223 |
node destination = labelNum |
|
1224 |
ifTrue: |
|
1225 |
[sp := i. |
|
1226 |
^ seq]. |
|
1227 |
goto := stack at: (i := i - 1). |
|
1228 |
goto isGoto and: [goto destination = node destination]] |
|
1229 |
whileTrue: [i := i - 1]. |
|
1230 |
sp := i + 1. |
|
1231 |
^ seq |
|
1232 |
! |
|
1233 |
||
1234 |
SequenceOtherwise |
|
1235 |
| node seq i | |
|
1236 |
seq := RBSequenceNode statements: #(). |
|
1237 |
i := self spIndex. |
|
1238 |
node := stack at: i. |
|
1239 |
node isSequence ifTrue: [ |
|
1240 |
seq addNodesFirst: node statements. |
|
1241 |
self stackDown] |
|
1242 |
ifFalse:[node isLabel ifFalse:[self abort]]. |
|
1243 |
^ seq |
|
1244 |
! |
|
1245 |
||
1246 |
Value |
|
1247 |
||
1248 |
| node | |
|
1249 |
node := self ValueOrNone. |
|
1250 |
node ifNil: [self abort]. |
|
1251 |
^ node |
|
1252 |
! |
|
1253 |
||
1254 |
ValueOrNone |
|
1255 |
| node i label | |
|
1256 |
i := self spIndex. |
|
1257 |
[node := stack at: i. |
|
1258 |
node isValue |
|
1259 |
ifTrue: |
|
1260 |
[label ifNotNil: [valueLabelMap at: node put: label]. |
|
1261 |
sp := i - 1. |
|
1262 |
^ node]. |
|
1263 |
(node isLabel and: [i > 1]) ifFalse: [^ nil]. |
|
1264 |
label := node. |
|
1265 |
node := stack at: (i := i - 1). |
|
1266 |
node isGoto and: [node destination = label destination]] |
|
1267 |
whileTrue: [i := i - 1]. |
|
1268 |
^ nil |
|
1269 |
! |
|
1270 |
||
1271 |
abort |
|
1272 |
||
1273 |
| spWas | |
|
1274 |
spWas := sp. |
|
1275 |
sp := nil. |
|
1276 |
Abort signal |
|
1277 |
! |
|
1278 |
||
1279 |
fixStack |
|
1280 |
||
1281 |
sp ifNotNil: [stack removeLast: (stack size - sp)]. |
|
1282 |
sp := nil. |
|
1283 |
! |
|
1284 |
||
1285 |
spIndex |
|
1286 |
^ sp ifNil: [sp := stack size] |
|
1287 |
! |
|
1288 |
||
1289 |
stackDown |
|
1290 |
||
1291 |
| node | |
|
1292 |
sp ifNil: [sp _ stack size]. |
|
1293 |
sp = 0 ifTrue: [self abort]. |
|
1294 |
node _ stack at: sp. |
|
1295 |
sp _ sp - 1. |
|
1296 |
^ node |
|
1297 |
! |
|
1298 |
||
1299 |
stackPush: node |
|
1300 |
||
1301 |
self fixStack. |
|
1302 |
stack addLast: node. |
|
1303 |
node ifNil: [^ self]. "no op" |
|
1304 |
self mapNode: node. |
|
1305 |
! ! |
|
1306 |
||
1307 |
!IRDecompiler class methodsFor:'documentation'! |
|
1308 |
||
1309 |
version_CVS |
|
41
f3898a3b378d
Package renamed from cvut:stx/goodies/newcompiler to ctu:ircompiler
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
37
diff
changeset
|
1310 |
^ 'Header: /cvs/stx/cvut/stx/goodies/newcompiler/IRDecompiler.st,v 1.3 2009/10/08 12:04:20 fm Exp ' |
37 | 1311 |
! |
1312 |
||
1313 |
version_SVN |
|
1314 |
^ '$Id:: $' |
|
1315 |
! ! |
|
42
acdc3ec6d152
Better class category name - IR Compiler-*
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
41
diff
changeset
|
1316 |