Skip to content
← Back

src/syntax/process/generate-il/generate_il_comprehensions.ghul

1
namespace Syntax.Process is
2
use Trees
3
4
use IR
5
use IR.Values
6
7
// A list comprehension emits its loops directly into the BLOCK that
8
// compile-expressions left as its value: one iterator loop per `for`
9
// clause, outermost first, a branch past the rest of the body for each
10
// `if` clause, and at the innermost point the element stored. Where the
11
// length is known up front the element goes straight into an array
12
// allocated at that length; otherwise it is added to a list, which is
13
// turned into the array after the loops finish.
14
partial GENERATE_IL is
15
pre(comprehension: Expressions.COMPREHENSION) -> bool is
16
super.pre(comprehension)
17
18
if comprehension.is_lazy then
19
_emit_lazy_comprehension(comprehension)
20
21
return true
22
fi
23
24
if
25
!comprehension.value? \/
26
!isa Values.BLOCK(comprehension.value) \/
27
!comprehension.list_type? \/
28
!comprehension.list_constructor? \/
29
!comprehension.list_add? \/
30
!comprehension.list_to_array? \/
31
!comprehension.element_type?
32
then
33
// compile-expressions has already reported why there is
34
// nothing to emit; walk on so any function literal inside
35
// is still generated.
36
return false
37
fi
38
39
let spiller = COMPOSITE_VALUE_SPILLER(self, comprehension, comprehension.value, _current_state_machine_frame())
40
41
spiller.enter()
42
43
try
44
if
45
let count = comprehension.presize_count,
46
source_local = comprehension.presize_source
47
then
48
let source = comprehension.first.source
49
50
source.walk(self)
51
52
add(Values.DECLARE_LOCAL(source_local))
53
add(source.value!)
54
add(Values.STORE_TEMP(source_local))
55
56
let array_type = comprehension.value!.type!
57
let int_type = _innate_symbol_lookup.get_int_type()
58
59
let fill =
60
COMPREHENSION_FILL(
61
TEMP(current_block, "comprehension", Values.NEW_ARRAY(array_type, comprehension.element_type!, count)),
62
TEMP(current_block, "comprehension_index", Values.Literal.NUMBER(0, int_type)),
63
int_type
64
)
65
66
_gen_comprehension_for(comprehension, comprehension.first, fill, Values.Load.TEMP(source_local))
67
68
spiller.emit_value(fill.result!.load())
69
else
70
let fill =
71
COMPREHENSION_FILL(
72
TEMP(
73
current_block,
74
"comprehension",
75
Values.NEW(comprehension.list_type!, comprehension.list_constructor!, Collections.LIST[Value](0))
76
)
77
)
78
79
_gen_comprehension_clause(comprehension, comprehension.first, fill)
80
81
spiller.emit_value(
82
comprehension.list_to_array!.call(
83
comprehension.location,
84
fill.result!.load(),
85
Collections.LIST[Value](0),
86
null,
87
_function_caller
88
)
89
)
90
fi
91
finally
92
spiller.leave()
93
yrt
94
95
return true
96
si
97
98
visit(comprehension: Expressions.COMPREHENSION) is
99
super.visit(comprehension)
100
si
101
102
// A lazy comprehension: the generator literal it owns is emitted as
103
// any generator literal is, with the clause loops as its MoveNext
104
// body and each element yielded rather than stored. The value
105
// compile-expressions left, a call to the literal, is emitted where
106
// the comprehension is used.
107
_emit_lazy_comprehension(comprehension: Expressions.COMPREHENSION) is
108
let generator = cast Semantic.Symbols.Closure?(comprehension.scope)
109
110
if !generator? \/ isa Values.DUMMY(comprehension.value) then
111
return
112
fi
113
114
let state_machine = Semantic.Symbols.state_machine_for(generator)
115
116
if !state_machine? then
117
return
118
fi
119
120
generator.map_type_arguments()
121
122
if let frame = state_machine.frame then
123
frame.declare()
124
125
let enclosing_loops = _loops
126
127
_loops = LOOP_LABEL_STACK()
128
129
_gen_generator_move_next(
130
comprehension,
131
state_machine,
132
frame,
133
() is
134
_gen_comprehension_clause(comprehension, comprehension.first, COMPREHENSION_FILL(state_machine))
135
si
136
)
137
138
_loops = enclosing_loops
139
140
_without_position(() is _emit_generator_outer_method(generator, frame, state_machine) si)
141
fi
142
143
generator.unmap_type_arguments()
144
si
145
146
_gen_comprehension_clause(
147
comprehension: Expressions.COMPREHENSION,
148
clause: Expressions.COMPREHENSION_CLAUSE?,
149
fill: COMPREHENSION_FILL
150
) is
151
if !clause? then
152
_gen_comprehension_element(comprehension, fill)
153
elif let for_clause: Expressions.COMPREHENSION_FOR = clause then
154
_gen_comprehension_for(comprehension, for_clause, fill, null)
155
elif let if_clause: Expressions.COMPREHENSION_IF = clause then
156
_gen_comprehension_if(comprehension, if_clause, fill)
157
fi
158
si
159
160
_gen_comprehension_element(comprehension: Expressions.COMPREHENSION, fill: COMPREHENSION_FILL) is
161
let element = comprehension.element
162
163
// Walked for what it contains - a function literal's body, a
164
// block's captured IL - before its value is emitted.
165
element.walk(self)
166
167
if let state_machine = fill.state_machine then
168
_gen_yield_value(state_machine, element.value)
169
elif let index = fill.index then
170
add(Values.STORE_ARRAY_ELEMENT(fill.result!.load(), index.load(), element.value!, comprehension.element_type!))
171
172
index.store(
173
Values.ARITHMETIC(
174
IR.ArithmeticOperation.ADD,
175
index.load(),
176
Values.Literal.NUMBER(1, fill.int_type!),
177
fill.int_type!
178
).unmarked()
179
)
180
else
181
// The call converts the element to the list's element type,
182
// boxing or wrapping it as its parameter needs.
183
let arguments = Collections.LIST[Value]()
184
arguments.add(element.value!)
185
186
add(
187
comprehension.list_add!.call(
188
element.location,
189
fill.result!.load(),
190
arguments,
191
null,
192
_function_caller
193
)
194
)
195
fi
196
si
197
198
_gen_comprehension_for(
199
comprehension: Expressions.COMPREHENSION,
200
clause: Expressions.COMPREHENSION_FOR,
201
fill: COMPREHENSION_FILL,
202
evaluated_source: Value?
203
) is
204
super.pre(clause)
205
206
try
207
let source = clause.source
208
209
// A source already evaluated into a local, for its count,
210
// has been walked too.
211
let source_value: Value =
212
if let evaluated = evaluated_source then
213
evaluated
214
else
215
source.walk(self)
216
source.value!
217
fi
218
219
let iterator: Value =
220
if let read_iterator = clause.read_iterator then
221
read_iterator.call(source.location, source_value, Collections.LIST[Value](0), null, _function_caller)
222
else
223
source_value
224
fi
225
226
clause.variable.walk(self)
227
228
// Inside a state machine the iterator is kept in the frame
229
// field compile-expressions allocated for it, as a `for`
230
// loop's is, so it survives each suspend between elements.
231
let iterator_field = clause.iterator_field
232
let iterator_frame = _current_state_machine_frame()
233
234
let load_iterator: () -> Value =
235
if let stored = iterator_field, frame = iterator_frame then
236
add(_build_frame_field_store(frame, stored, iterator))
237
238
() => cast Value(IR.Values.Load.INSTANCE_FIELD(IR.Values.Load.REFERENCE_SELF(frame, frame.type), stored))
239
else
240
let temp = TEMP(current_block, "comprehension_iterator", iterator)
241
242
() => cast Value(temp.load())
243
fi
244
245
let loop = Values.LOOP(LABEL(), LABEL(), _innate_symbol_lookup.get_void_type())
246
247
_open_structured(loop)
248
249
let has_next = clause.move_next!.call(source.location, load_iterator(), Collections.LIST[Value](0), null, _function_caller)
250
251
add(Values.BREAK(loop, BRANCH.Z, has_next))
252
253
let current = clause.read_current!.call(source.location, load_iterator(), Collections.LIST[Value](0), null, _function_caller)
254
255
gen_destructuring_initialize(clause.variable.left, current)
256
257
_gen_comprehension_clause(comprehension, clause.inner, fill)
258
259
_close_structured(loop)
260
finally
261
super.visit(clause)
262
yrt
263
si
264
265
_gen_comprehension_if(
266
comprehension: Expressions.COMPREHENSION,
267
clause: Expressions.COMPREHENSION_IF,
268
fill: COMPREHENSION_FILL
269
) is
270
let condition = clause.condition
271
272
condition.walk(self)
273
274
let skip = _open_region(LABEL())
275
276
add(Values.BREAK(skip, BRANCH.Z, condition.value!))
277
278
_gen_comprehension_clause(comprehension, clause.inner, fill)
279
280
_close_structured(skip)
281
si
282
si
283
284
// Where a comprehension's elements go: straight into an array at a
285
// running index when its length is known up front, into a list, or,
286
// for a lazy comprehension, out of its generator one yield at a time.
287
class COMPREHENSION_FILL is
288
result: TEMP?
289
state_machine: Semantic.Symbols.STATE_MACHINE?
290
index: TEMP?
291
int_type: Semantic.Types.Type?
292
293
init(result: TEMP) is
294
self.result = result
295
si
296
297
init(result: TEMP, index: TEMP, int_type: Semantic.Types.Type) is
298
self.result = result
299
self.index = index
300
self.int_type = int_type
301
si
302
303
init(state_machine: Semantic.Symbols.STATE_MACHINE) is
304
self.state_machine = state_machine
305
si
306
si
307
si