-
Notifications
You must be signed in to change notification settings - Fork 71
/
MLStatementListBuider.class.st
206 lines (164 loc) · 5.76 KB
/
MLStatementListBuider.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
Class {
#name : #MLStatementListBuider,
#superclass : #Object,
#instVars : [
'parent',
'statements',
'expressionStack',
'declarations',
'codeGenerator'
],
#category : #'Slang-Optimizations'
}
{ #category : #building }
MLStatementListBuider >> addLinearisedStatement: expressionNode [
"Add the expression when used"
| name assignment declaration type |
"If it's a variable or constant, just return it, the caller will use it in the right place"
(expressionNode isTMethod or: [
expressionNode isStatementList or: [ expressionNode isLeaf ] ])
ifTrue: [ ^ expressionNode ].
"If it's a control flow structure, and we are using it's value, write its last expression to a variable:
v ifTrue: [ r := ... ] ifFalse: [ r := ...].
Return the value r that is holding the result"
(codeGenerator isControlFlowNode: expressionNode) ifTrue: [
| variable |
variable := TVariableNode named: self newVariableName.
type := codeGenerator
typeFor: expressionNode
in: codeGenerator currentMethod.
declaration := type , ' ' , variable name.
declarations at: variable name put: declaration.
self addStatement: (self
transformControlFlowNodeForValue: expressionNode
withVariable: variable).
^ variable ].
expressionNode isAssignment ifTrue: [
self addStatement: expressionNode.
^ expressionNode variable ].
name := self newVariableName.
type := codeGenerator
typeFor: expressionNode
in: codeGenerator currentMethod.
declaration := type , ' ' , name.
declarations at: name put: declaration.
assignment := TAssignmentNode
variableNamed: name
expression: expressionNode.
self addStatement: assignment.
^ assignment variable
]
{ #category : #building }
MLStatementListBuider >> addStatement: aStatement [
statements addLast: aStatement
]
{ #category : #transforming }
MLStatementListBuider >> assignLastExpressionOf: aNode toVariable: aTVariableNode [
"We need to take the expression and find its last expression recursively.
We should rewrite it to add an assignment to its last expression"
| worklist |
worklist := OrderedCollection with: aNode.
[ worklist isEmpty ] whileFalse: [ | current |
current := worklist removeLast.
current isStatementList ifTrue: [
worklist add: current last
] ifFalse: [ | replacement parentBeforeReplacement |
self flag: #polymorphism.
parentBeforeReplacement := current parent.
replacement := (current isSend and: [ current isConditionalSend ])
ifTrue: [ self transformControlFlowNodeForValue: current withVariable: aTVariableNode copy ]
ifFalse: [ current assignLastExpressionTo: aTVariableNode copy ].
parentBeforeReplacement replaceChild: current with: replacement ] ].
^ aNode
]
{ #category : #building }
MLStatementListBuider >> buildStatementList [
"Build a statement list that could be in an expression.
Add an explicit nil in case there were no expressions or statements in it"
^ TStatementListNode
declarations: declarations
statements: (statements ifEmpty: [ { TConstantNode value: nil } ])
]
{ #category : #accessing }
MLStatementListBuider >> codeGenerator: anObject [
codeGenerator := anObject
]
{ #category : #building }
MLStatementListBuider >> finishStatement [
"If there was an unused expression yet in the stack, pop it and add it as a statement"
expressionStack ifEmpty: [ ^ self ].
self popNoValue
]
{ #category : #initialization }
MLStatementListBuider >> initialize [
super initialize.
statements := OrderedCollection new.
expressionStack := OrderedCollection new.
declarations := Dictionary new
]
{ #category : #building }
MLStatementListBuider >> newVariableName [
^ 't' , statements size asString
]
{ #category : #accessing }
MLStatementListBuider >> parent [
^ parent
]
{ #category : #accessing }
MLStatementListBuider >> parent: anObject [
parent := anObject
]
{ #category : #building }
MLStatementListBuider >> popAsExpression [
^ expressionStack removeLast asExpressionIn: self
]
{ #category : #building }
MLStatementListBuider >> popAsStatement [
"Add the expression when used"
| expressionNode |
expressionNode := self popAsExpression.
^ self addLinearisedStatement: expressionNode
]
{ #category : #building }
MLStatementListBuider >> popNoValue [
"Add the expression when used"
| expressionNode |
expressionNode := expressionStack removeLast asStatementIn: self.
self addStatement: expressionNode
]
{ #category : #building }
MLStatementListBuider >> push: anExpression [
"pushes an expression to add it as a new statement"
expressionStack addLast: anExpression
]
{ #category : #pushing }
MLStatementListBuider >> pushAsStatement: anExpression [
self push: (MLLinerarisedStatement new expressionToLinearise: anExpression; yourself)
]
{ #category : #'control-flow' }
MLStatementListBuider >> transformControlFlowNodeForValue: aTSendNode withVariable: aVariable [
| arguments secondArgument |
secondArgument := (#( ifTrue: ifFalse: ) includes:
aTSendNode selector)
ifTrue: [
TStatementListNode statements:
{ (TAssignmentNode
variable: aVariable copy
expression: aTSendNode receiver) } ]
ifFalse: [
self
assignLastExpressionOf:
aTSendNode arguments second
toVariable: aVariable copy ].
arguments := {
(self
assignLastExpressionOf: aTSendNode arguments first
toVariable: aVariable copy).
secondArgument }.
aTSendNode selector = #ifFalse: ifTrue: [
arguments := arguments reversed ].
^ TSendNode
receiver: aTSendNode receiver
selector: #ifTrue:ifFalse:
arguments: arguments
]