Class: MilkTea::Lowerer
- Inherits:
-
Object
show all
- Includes:
- CompatibilityHelpers, LowererAsync, LowererBlock, LowererCalls, LowererDeclarations, LowererDyn, LowererEvents, LowererExpressions, LowererForeignCstr, LowererFunctions, LowererLoops, LowererProc, LowererResolve, LowererScans, LowererStrBuffer, LowererUtils
- Defined in:
- lib/milk_tea/core/lowering.rb,
lib/milk_tea/core/lowering/artifacts.rb,
lib/milk_tea/core/lowering/lowering_context.rb
Defined Under Namespace
Classes: Artifacts, ModuleContext
Constant Summary
CompatibilityHelpers::ATOMIC_METHOD_KINDS, CompatibilityHelpers::BUILTIN_CSTR, CompatibilityHelpers::EVENT_METHOD_KINDS, CompatibilityHelpers::EVENT_METHOD_NAMES, CompatibilityHelpers::SIMD_METHOD_KINDS, CompatibilityHelpers::STR_BUFFER_METHOD_KINDS, CompatibilityHelpers::STR_BUFFER_METHOD_NAMES
Instance Attribute Summary collapse
Instance Method Summary
collapse
#event_subscription_result_type, #event_wait_result_type, #range_expr?, #type_ref_from_specialization
#arity_error_message, #atomic_element_type, #atomic_type?, #call_arity_matches?, #callable_param_ref_supported?, #char_pointer_type?, #char_type?, #collection_loop_binding_type, #collection_loop_ref_element_type?, #collection_loop_type, #const_pointer_to, #const_pointer_type?, #contains_ref_type?, #contains_type_var?, #contextual_int_to_float_compatibility?, #contextual_int_to_float_target?, #dyn_type?, #exact_integer_constant_value, #exactly_representable_float32?, #external_numeric_compatibility?, #external_typed_null_pointer_compatibility?, #flatten_field_types, #float_constant_fits_type?, #foreign_boundary_element_compatible?, #foreign_char_pointer_buffer_boundary_compatible?, #foreign_external_layout_compatible?, #foreign_external_layout_field_compatible?, #foreign_function_type_projection_compatible?, #foreign_identity_projection_cast_compatible?, #foreign_identity_projection_compatible?, #foreign_identity_projection_reinterpret_compatible?, #foreign_opaque_c_name, #foreign_span_boundary_compatible?, #function_type_matches_proc_type?, #generic_integer_type_argument?, #integer_constant_fits_type?, #integer_like_char_source_type?, #integer_to_char_compatibility?, #integer_type_argument?, #lossless_external_integer_compatibility?, #lossless_integer_compatibility?, #method_dispatch_receiver_type, #mutable_pointer_type?, #mutable_to_const_pointer_compatibility?, #native_foreign_layout_compatible?, #null_assignable_to?, #numeric_constant_fits_type?, #opaque_type?, #own_to_raw_pointer_compatibility?, #own_type?, #owned_referent_type, #pointee_type, #pointer_type?, #proc_type?, #quat_vec4_layout_compatible?, #ref_lifetime, #ref_type?, #ref_type_without_lifetime?, #referenced_type, #same_external_opaque_c_name?, #same_external_opaque_handle_pointer_compatibility?, #string_builder_ref_type?, #string_builder_type?, #struct_with_target_type?, #task_root_proc_type?, #task_type?, #validate_generic_type!, #value_fits_integer_type?, #void_pointer_type?
Constructor Details
#initialize(program) ⇒ Lowerer
Returns a new instance of Lowerer.
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/milk_tea/core/lowering.rb', line 93
def initialize(program)
@program = program
@ctx = ModuleContext.new
@artifacts = Artifacts.new
@synthetic_proc_counter = 0
@parallel_for_counter = 0
@async_binding_counter = 0
@method_definitions = build_method_definitions
@bypass_sema_type_cache = false
end
|
Instance Attribute Details
#bypass_sema_type_cache ⇒ Object
Returns the value of attribute bypass_sema_type_cache.
90
91
92
|
# File 'lib/milk_tea/core/lowering.rb', line 90
def bypass_sema_type_cache
@bypass_sema_type_cache
end
|
#recorded_expr_types ⇒ Object
Returns the value of attribute recorded_expr_types.
91
92
93
|
# File 'lib/milk_tea/core/lowering.rb', line 91
def recorded_expr_types
@recorded_expr_types
end
|
Instance Method Details
#assemble_modules(modules) ⇒ Object
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
|
# File 'lib/milk_tea/core/lowering.rb', line 229
def assemble_modules(modules)
if @program.root_analysis.module_kind == :raw_module
raise LoweringError, "cannot emit C for external file #{@program.root_analysis.module_name}"
end
regenerate_cross_module_synthetics
includes = collect_includes
ordered = ordered_module_fragments(modules)
all_constants = ordered.flat_map(&:constants)
all_constants.concat(@artifacts.synthetic_constants)
all_globals = ordered.flat_map(&:globals)
all_opaques = ordered.flat_map(&:opaques)
all_structs = ordered.flat_map(&:structs)
all_unions = ordered.flat_map(&:unions)
all_enums = ordered.flat_map(&:enums)
all_variants = ordered.flat_map(&:variants)
all_static_asserts = ordered.flat_map(&:static_asserts)
all_static_asserts.concat(@artifacts.external_layout_assertions)
all_functions = ordered.flat_map(&:functions)
all_opaques.concat(lower_imported_external_opaques)
all_structs.concat(@artifacts.synthetic_structs.uniq { |s| s.linkage_name })
all_enums.concat(@artifacts.synthetic_enums.uniq { |e| e.linkage_name })
all_functions.concat(@artifacts.synthetic_functions.uniq { |f| f.linkage_name })
@artifacts.emitted_declarations.each do |emitted|
case emitted
when IR::Function
all_functions << emitted
when IR::StructDecl
all_structs << emitted
when IR::Constant
all_constants << emitted
end
end
IR::Program.new(
module_name: @program.root_analysis.module_name,
includes:,
constants: all_constants,
globals: all_globals,
opaques: all_opaques,
structs: all_structs,
unions: all_unions,
enums: all_enums,
variants: all_variants,
static_asserts: all_static_asserts,
functions: all_functions,
source_path: @program.root_path,
)
end
|
#canonical_module_names(names, deps_of) ⇒ Object
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
# File 'lib/milk_tea/core/lowering.rb', line 336
def canonical_module_names(names, deps_of)
visited = {}
on_stack = {}
order = []
visit = nil
visit = lambda do |name|
return if visited[name] || on_stack[name]
on_stack[name] = true
(deps_of[name] || []).sort.each { |dep| visit.call(dep) }
on_stack.delete(name)
visited[name] = true
order << name
end
names.sort.each { |name| visit.call(name) }
order
end
|
#compute_ordered_analysis_pairs ⇒ Object
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/milk_tea/core/lowering.rb', line 306
def compute_ordered_analysis_pairs
pairs = @program.analyses_by_path.to_a
by_name = {}
pairs.each do |path, analysis|
name = analysis.module_name.to_s
by_name[name] ||= [path, analysis]
end
present = by_name.keys.to_set
deps_of = Hash.new { |hash, key| hash[key] = [] }
pairs.each do |_path, analysis|
name = analysis.module_name.to_s
imported_module_names(analysis).each do |dep|
deps_of[name] << dep if present.include?(dep) && dep != name
end
end
deps_of.each_value(&:uniq!)
canonical_module_names(by_name.keys, deps_of).map { |name| by_name[name] }
end
|
#ensure_events_for_analysis(analysis) ⇒ Object
222
223
224
225
226
227
|
# File 'lib/milk_tea/core/lowering.rb', line 222
def ensure_events_for_analysis(analysis)
analysis.ast.declarations.grep(AST::EventDecl).each do |decl|
event_type = analysis.values.fetch(decl.name).type
ensure_event_runtime(event_type)
end
end
|
#imported_module_names(analysis) ⇒ Object
327
328
329
330
331
332
333
334
|
# File 'lib/milk_tea/core/lowering.rb', line 327
def imported_module_names(analysis)
imports = analysis.respond_to?(:imports) ? analysis.imports : nil
return [] unless imports.respond_to?(:values)
imports.values.filter_map do |binding|
binding.name.to_s if binding.respond_to?(:name) && binding.name
end
end
|
#lower ⇒ Object
104
105
106
107
108
|
# File 'lib/milk_tea/core/lowering.rb', line 104
def lower
@recorded_expr_types = {} if @bypass_sema_type_cache
ir_program, _modules, _synths = lower_and_assemble
ir_program
end
|
#lower_and_assemble(cached: nil, cached_synthetics: nil) ⇒ Object
110
111
112
113
|
# File 'lib/milk_tea/core/lowering.rb', line 110
def lower_and_assemble(cached: nil, cached_synthetics: nil)
modules, per_module_synthetics = lower_modules(cached:, cached_synthetics:)
[assemble_modules(modules), modules, per_module_synthetics]
end
|
#lower_modules(cached: nil, cached_synthetics: nil) ⇒ Object
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
|
# File 'lib/milk_tea/core/lowering.rb', line 115
def lower_modules(cached: nil, cached_synthetics: nil)
if @program.root_analysis.module_kind == :raw_module
raise LoweringError, "cannot emit C for external file #{@program.root_analysis.module_name}"
end
per_module_synthetics = {}
per_module_funcs = Hash.new { |h, k| h[k] = [] }
modules = {}
cached_synthetics&.each do |_module_name, synths|
@artifacts.synthetic_structs.concat(synths[:structs] || [])
@artifacts.synthetic_enums.concat(synths[:enums] || [])
@artifacts.synthetic_functions.concat(synths[:functions] || [])
@artifacts.synthetic_constants.concat(synths[:constants] || [])
end
cached&.each do |module_name, cached_ir|
modules[module_name] = cached_ir.with(functions: [])
per_module_funcs[module_name] = cached_ir.functions.dup
cached_ir.functions.each { |f| @artifacts.lowered_function_linkage_names[f.linkage_name] = true }
end
ordered_analysis_pairs.each do |path, analysis|
next if analysis.module_kind == :raw_module
next if modules.key?(analysis.module_name)
prepare_analysis(analysis, source_path: path)
collect_structs
synth_before_s = @artifacts.synthetic_structs.length
synth_before_e = @artifacts.synthetic_enums.length
synth_before_f = @artifacts.synthetic_functions.length
synth_before_c = @artifacts.synthetic_constants.length
modules[analysis.module_name] = IR::Program.new(
module_name: analysis.module_name,
includes: [],
constants: lower_constants.dup,
globals: lower_globals.dup,
opaques: lower_opaques.dup,
structs: lower_structs.dup,
unions: lower_unions.dup,
enums: lower_enums.dup,
variants: lower_variants.dup,
static_asserts: lower_static_asserts.dup,
functions: [],
source_path: path,
)
per_module_funcs[analysis.module_name].concat(lower_functions)
per_module_synthetics[analysis.module_name] = {
structs: @artifacts.synthetic_structs[synth_before_s..] || [],
enums: @artifacts.synthetic_enums[synth_before_e..] || [],
functions: @artifacts.synthetic_functions[synth_before_f..] || [],
constants: @artifacts.synthetic_constants[synth_before_c..] || [],
}
end
pending = true
while pending
pending = false
ordered_analysis_pairs.each do |path, analysis|
next if analysis.module_kind == :raw_module
prepare_analysis(analysis, source_path: path)
ensure_events_for_analysis(analysis)
synth_before_s = @artifacts.synthetic_structs.length
synth_before_e = @artifacts.synthetic_enums.length
synth_before_f = @artifacts.synthetic_functions.length
synth_before_c = @artifacts.synthetic_constants.length
newly_lowered = lower_functions
next if newly_lowered.empty?
per_module_funcs[analysis.module_name].concat(newly_lowered)
delta = {
structs: @artifacts.synthetic_structs[synth_before_s..] || [],
enums: @artifacts.synthetic_enums[synth_before_e..] || [],
functions: @artifacts.synthetic_functions[synth_before_f..] || [],
constants: @artifacts.synthetic_constants[synth_before_c..] || [],
}
existing = per_module_synthetics[analysis.module_name]
per_module_synthetics[analysis.module_name] = existing ? merge_synthetics(existing, delta) : delta
pending = true
end
end
modules.transform_values! do |fragment|
fragment.with(functions: per_module_funcs[fragment.module_name])
end
[modules, per_module_synthetics]
end
|
#merge_synthetics(a, b) ⇒ Object
213
214
215
216
217
218
219
220
|
# File 'lib/milk_tea/core/lowering.rb', line 213
def merge_synthetics(a, b)
{
structs: (a[:structs] || []) + (b[:structs] || []),
enums: (a[:enums] || []) + (b[:enums] || []),
functions: (a[:functions] || []) + (b[:functions] || []),
constants: (a[:constants] || []) + (b[:constants] || []),
}
end
|
#ordered_analysis_pairs ⇒ Object
Canonical, deterministic iteration order over the program's analyses so
that lowering — and therefore the emitted C — is byte-identical regardless
of how the analyses were assembled (live ModuleLoader vs. JSON-reconstructed
bundle). Modules are emitted in dependency-first topological order, with
lexicographic module-name order as a stable tiebreaker for independent
modules and as a cycle-breaking fallback.
302
303
304
|
# File 'lib/milk_tea/core/lowering.rb', line 302
def ordered_analysis_pairs
@ordered_analysis_pairs ||= compute_ordered_analysis_pairs
end
|
#ordered_module_fragments(modules) ⇒ Object
354
355
356
357
358
|
# File 'lib/milk_tea/core/lowering.rb', line 354
def ordered_module_fragments(modules)
ordered = ordered_analysis_pairs.filter_map { |_path, analysis| modules[analysis.module_name] }
ordered.concat(modules.values - ordered)
ordered
end
|
#regenerate_cross_module_synthetics ⇒ Object
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/milk_tea/core/lowering.rb', line 284
def regenerate_cross_module_synthetics
ordered_analysis_pairs.each do |path, analysis|
next if analysis.module_kind == :raw_module
prepare_analysis(analysis, source_path: path)
analysis.ast.declarations.grep(AST::EventDecl).each do |decl|
event_type = analysis.values.fetch(decl.name).type
ensure_event_runtime(event_type) if event_type.is_a?(Types::Event)
end
end
end
|