-
Notifications
You must be signed in to change notification settings - Fork 65
/
TAssignmentNode.class.st
350 lines (280 loc) · 9.97 KB
/
TAssignmentNode.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
Class {
#name : #TAssignmentNode,
#superclass : #TParseNode,
#instVars : [
'variable',
'expression'
],
#category : #'Slang-AST'
}
{ #category : #'instance creation' }
TAssignmentNode class >> variable: aVariable expression: anExpression [
^ self new
setVariable: aVariable
expression: anExpression;
yourself
]
{ #category : #'instance creation' }
TAssignmentNode class >> variableNamed: aVariableName expression: anExpression [
^ self new
setVariable: (TVariableNode named: aVariableName) expression: anExpression;
yourself
]
{ #category : #visiting }
TAssignmentNode >> accept: aVisitor [
^ aVisitor visitAssignmentNode: self
]
{ #category : #'C code generation' }
TAssignmentNode >> asCASTExpressionIn: aCodeGen [
(expression isStatementList and: [ expression statements size > 1 ])
ifTrue: [ ^ self asCASTStatementListExpansionAsExpression: aCodeGen ].
^ (self asCASTIn: aCodeGen)
needsParentheses: true;
yourself
]
{ #category : #tranforming }
TAssignmentNode >> asCASTIn: aBuilder [
| type |
expression isSwitch ifTrue: [
^ expression asCASTIn: aBuilder addToEndOfCases: self ].
expression isLiteralArrayDeclaration ifTrue: [
| literalArrayNode |
type := expression arguments last value.
self assert: type last = $*.
"Transform the literal array declaration to a C block containing:
- a static literal array declaration
- an assigment from this literal array to the assigned variable "
literalArrayNode := expression arguments first value collect: [ :e |
CConstantNode value: e ].
^ CCompoundStatementNode
declarations: { (CDeclarationNode
specifiers: {
'static'.
type allButLast }
declarator: (CInitDeclaratorNode
declarator:
(CArrayDeclaratorNode identifier: 'aLiteralArray')
initializer:
(CGLRInitializerNode initializers: literalArrayNode))) }
statements: { (CAssignmentNode
lvalue: (CIdentifierNode name: variable name)
rvalue: (CIdentifierNode name: 'aLiteralArray')) } ].
(expression isStatementList and: [ expression statements size > 1 ])
ifTrue: [
^ expression copy
assignLastExpressionTo: variable;
asCASTIn: aBuilder ].
self isVariableUpdatingAssignment ifTrue: [
^ CAssignmentNode
lvalue: (variable asCASTIn: aBuilder)
operator: expression selector , '='
rvalue: (expression arguments first asCASTExpressionIn: aBuilder) ].
(expression isSend and: [ expression isValueExpansion ]) ifTrue: [
^ self asCASTValueExpansionIn: aBuilder ].
^ CAssignmentNode
lvalue: (variable asCASTIn: aBuilder)
rvalue: (expression asCASTExpressionIn: aBuilder)
]
{ #category : #'C code generation' }
TAssignmentNode >> asCASTStatementListExpansionAsExpression: aCodeGen [
(variable isSameAs: expression statements last) ifTrue:
[^ expression asCASTExpressionIn: aCodeGen].
^ expression copy
assignLastExpressionTo: variable;
asCASTExpressionIn: aCodeGen
]
{ #category : #'C code generation' }
TAssignmentNode >> asCASTValueExpansionIn: aCodeGen [
| stmtList lastStmt copy |
"Transform assignments of the form:
var := [ a . block . lastexpression ] value.
into:
[ a . block . var := lastExpression ] value.
Takes into account variants of #value and #value:.
"
self assert: (expression isSend and: [ expression isValueExpansion ]).
stmtList := expression receiver.
lastStmt := stmtList statements last.
"Optimization. If the expression looks like:
var := [ ... var ] value.
Emit directly the block without the last statement, as the assignment has no impact in the translation.
"
stmtList statements last = variable ifTrue: [
^ expression asCASTIn: aCodeGen ].
copy := stmtList copy.
copy statements
at: stmtList statements size
put:
(TAssignmentNode new setVariable: variable expression: lastStmt).
^ (TSendNode new
setSelector: expression selector
receiver: copy
arguments: expression arguments) asCASTIn: aCodeGen
]
{ #category : #transformations }
TAssignmentNode >> bindVariableUsesIn: aDictionary [
"Do NOT bind the variable on the left-hand-side of an assignment statement."
"was bindVariablesIn:"
self expression: (expression bindVariableUsesIn: aDictionary).
]
{ #category : #transformations }
TAssignmentNode >> bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen [
"Answer either the receiver, if it contains no references to the given variables, or a new node with the given variables rebound."
"Do NOT bind the variable on the left-hand-side of an assignment statement."
| boundExpression |
boundExpression := expression bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen.
^boundExpression == expression
ifTrue: [self]
ifFalse: [self shallowCopy
setVariable: variable expression: boundExpression;
yourself]
]
{ #category : #transformations }
TAssignmentNode >> bindVariablesIn: aDictionary [
self variable: (variable bindVariablesIn: aDictionary).
self expression: (expression bindVariablesIn: aDictionary).
]
{ #category : #transformations }
TAssignmentNode >> bindVariablesIn: aDictionary unless: cautionaryBlock [
(cautionaryBlock value: self) ifTrue: [^self].
self variable: (variable bindVariablesIn: aDictionary unless: cautionaryBlock).
self expression: (expression bindVariablesIn: aDictionary unless: cautionaryBlock).
]
{ #category : #accessing }
TAssignmentNode >> children [
^ { variable . expression }
]
{ #category : #displaying }
TAssignmentNode >> displayString [
^ ' := '
]
{ #category : #accessing }
TAssignmentNode >> expression [
^expression
]
{ #category : #accessing }
TAssignmentNode >> expression: anExpression [
expression := anExpression.
expression parent: self.
]
{ #category : #testing }
TAssignmentNode >> isAssignment [
^true
]
{ #category : #testing }
TAssignmentNode >> isSameAs: aTParseNode [
^aTParseNode isAssignment
and: [(variable isSameAs: aTParseNode variable)
and: [expression isSameAs: aTParseNode expression]]
]
{ #category : #testing }
TAssignmentNode >> isVariableUpdatingAssignment [
"Return true if this assignment statement is of one of the forms:
var = var + ...
var = var - ...
Such assignments statements can exploit the C updating assignment operators. For example, 'x += 4' can be generated instead of 'x = x + 4'. This produces better code under some C compilers, most notably the CodeWarrior 68K compiler."
| sel |
(expression isSend and: [expression receiver isVariable]) ifFalse: [^ false].
sel := expression selector.
^ (expression receiver name = variable name) and: [(sel = #+) or: [sel = #-]]
]
{ #category : #enumerating }
TAssignmentNode >> nodesDo: aBlock [
"Apply aBlock to all nodes in the receiver.
N.B. This is assumed to be bottom-up, leaves first."
variable nodesDo: aBlock.
expression nodesDo: aBlock.
aBlock value: self
]
{ #category : #enumerating }
TAssignmentNode >> nodesDo: aBlock parent: parent [
"Apply aBlock to all nodes in the receiver with each node's parent.
N.B. This is assumed to be bottom-up, leaves first."
variable nodesDo: aBlock parent: self.
expression nodesDo: aBlock parent: self.
aBlock value: self value: parent
]
{ #category : #enumerating }
TAssignmentNode >> nodesDo: aBlock parent: parent unless: cautionaryBlock [
(cautionaryBlock value: self value: parent) ifTrue: [^self].
variable nodesDo: aBlock parent: self unless: cautionaryBlock.
expression nodesDo: aBlock parent: self unless: cautionaryBlock.
aBlock value: self value: parent
]
{ #category : #enumerating }
TAssignmentNode >> nodesDo: aBlock unless: cautionaryBlock [
(cautionaryBlock value: self) ifTrue: [^self].
variable nodesDo: aBlock unless: cautionaryBlock.
expression nodesDo: aBlock unless: cautionaryBlock.
aBlock value: self.
]
{ #category : #copying }
TAssignmentNode >> postCopy [
self variable: variable copy.
self expression: expression copy
]
{ #category : #'C code generation' }
TAssignmentNode >> prependCASTIn: aBuilder expression: nodeExp [
^ CAssignmentNode
lvalue: (variable asCASTExpressionIn: aBuilder)
operator: (self isVariableUpdatingAssignment
ifTrue: [ expression selector "+ or -" ]
ifFalse: [ '' ]) , '='
rvalue: (nodeExp asCASTExpressionIn: aBuilder)
]
{ #category : #printing }
TAssignmentNode >> printOn: aStream level: level [
variable printOn: aStream level: level.
aStream nextPutAll: ' := '.
expression printOn: aStream level: level + 2.
]
{ #category : #transformations }
TAssignmentNode >> removeAssertions [
expression removeAssertions
]
{ #category : #transformations }
TAssignmentNode >> replaceChild: aNode with: aReplacementNode [
variable == aNode ifTrue: [
self variable: aReplacementNode ].
expression == aNode ifTrue: [
self expression: aReplacementNode ]
]
{ #category : #transformations }
TAssignmentNode >> replaceNodesIn: aDictionary [
^aDictionary at: self ifAbsent: [
self variable: (variable replaceNodesIn: aDictionary).
self expression: (expression replaceNodesIn: aDictionary).
self]
]
{ #category : #accessing }
TAssignmentNode >> setVariable: varNode expression: expressionNode [
self assert: (expressionNode isGoTo or: [expressionNode isLabel]) not.
self variable: varNode.
self expression: expressionNode.
]
{ #category : #testing }
TAssignmentNode >> structTargetKindIn: aCodeGen [
"Answer if the recever evaluates to a struct or struct pointer
and hence can be dereferenced using . or ->. Answer any of
#struct #pointer or nil"
^variable structTargetKindIn: aCodeGen
]
{ #category : #'type inference' }
TAssignmentNode >> typeOrNilFrom: aCodeGenerator in: aTMethod [
"This is the default type in case of doubt"
^(variable typeOrNilFrom: aCodeGenerator in: aTMethod) ifNil:
[expression typeOrNilFrom: aCodeGenerator in: aTMethod]
]
{ #category : #accessing }
TAssignmentNode >> variable [
^variable
]
{ #category : #accessing }
TAssignmentNode >> variable: anObject [
variable := anObject.
variable parent: self.
]
{ #category : #accessing }
TAssignmentNode >> variableNameOrNil [
^variable variableNameOrNil
]