Skip to content
← Back

src/ir/emitter/cil_backend.ghul

1
@@IF.dotnet()
2
3
namespace IR.Emitter is
4
use Semantic.Symbols.Symbol
5
use Semantic.Symbols.Classy
6
use Semantic.Symbols.Function
7
8
// Writes each body as a CIL method body, encoded into the assembly
9
// the context holds.
10
class CIL_BACKEND: Backend is
11
_context: CIL_CONTEXT
12
13
_enclosing: Collections.STACK[(body: SRM_METHOD_BODY_EMITTER?, member: Symbol?)]
14
15
init(context: CIL_CONTEXT) is
16
_context = context
17
_enclosing = Collections.STACK[(body: SRM_METHOD_BODY_EMITTER?, member: Symbol?)]()
18
si
19
20
install(module_name: string, module_version: string, want_library: bool) is
21
_context.install_srm_assembly_emitter(module_name, module_version, want_library)
22
si
23
24
reset() is
25
_context.reset_srm_assembly_emitter()
26
si
27
28
begin_assembly() is
29
let container = IoC.CONTAINER.instance
30
let emitter = _context.srm_assembly_emitter
31
32
emitter.referenced_assemblies = container.referenced_assemblies
33
34
// Not at the point the emitter is constructed: the
35
// constructor signature these reference is built from
36
// the string type, which the symbol table does not
37
// hold until the reflected assemblies have been read.
38
CIL_ASSEMBLY_ATTRIBUTES.gen_requested(emitter, container.assembly_info)
39
40
CIL_ASSEMBLY_ATTRIBUTES.gen_session_cell(
41
emitter,
42
container.build_flags.submission_name,
43
container.assemblies)
44
45
// Assign every definition its metadata row before any
46
// body is encoded, so a body can name a target whose
47
// row is not written yet.
48
SRM_STRUCTURE_WALK(emitter).number(container.symbol_table.global_scope)
49
si
50
51
// Writes the rows the numbering promised, now that every body
52
// has been encoded and deposited. Driven by the sequence the
53
// numbering recorded, not by the symbol table, which has moved
54
// on since.
55
end_assembly() is
56
SRM_STRUCTURE_WALK(_context.srm_assembly_emitter).write_rows()
57
si
58
59
add_resource(name: string, content: ubyte[]) is
60
_context.srm_assembly_emitter.add_resource(name, content)
61
si
62
63
write(path: string) is
64
_context.srm_assembly_emitter.write_to(path)
65
si
66
67
begin_body() is
68
_begin(_context.srm_assembly_emitter.current_emission_member)
69
si
70
71
begin_body(member: Symbol) is
72
_begin(member)
73
si
74
75
_begin(member: Symbol?) is
76
let assembly_emitter = _context.srm_assembly_emitter
77
78
_enclosing.push((body = _context.current_srm_body_emitter, member = assembly_emitter.current_emission_member))
79
80
_context.current_srm_body_emitter = SRM_METHOD_BODY_EMITTER()
81
assembly_emitter.current_emission_member = member
82
si
83
84
write(value: Values.Value) is
85
_context.lowering.gen(value)
86
si
87
88
finish_body(function: Function) is
89
let assembly_emitter = _context.srm_assembly_emitter
90
let body = _context.current_srm_body_emitter!
91
92
assembly_emitter.handles.set_body_offset(function, body.flush(assembly_emitter))
93
assembly_emitter.handles.set_il_outputs(function, body.il_outputs)
94
si
95
96
finish_frame_member_body(frame: Classy, member: FrameMember) is
97
let assembly_emitter = _context.srm_assembly_emitter
98
let body = _context.current_srm_body_emitter!
99
100
assembly_emitter.handles.set_frame_member_body(frame, member, body.flush(assembly_emitter))
101
si
102
103
finish_unit_variant_initializer(variant: Classy) is
104
let assembly_emitter = _context.srm_assembly_emitter
105
let body = _context.current_srm_body_emitter!
106
107
assembly_emitter.handles.set_unit_variant_initializer_body(variant, body.flush(assembly_emitter))
108
si
109
110
end_body() is
111
assert _enclosing.count > 0 else "body ended without one begun"
112
113
let enclosing = _enclosing.pop()
114
115
_context.current_srm_body_emitter = enclosing.body
116
_context.srm_assembly_emitter.current_emission_member = enclosing.member
117
si
118
119
abandon_bodies() is
120
_enclosing.clear()
121
122
_context.current_srm_body_emitter = null
123
si
124
125
has_unit_variant_instance(variant: Classy) -> bool =>
126
_context.srm_assembly_emitter.handles.unit_variant_instance(variant)?
127
128
mark_entry_point(function: Function) is
129
// The entry point is marked by handle once the method's row
130
// is written, so this records the function rather than
131
// emitting anything here.
132
_context.srm_assembly_emitter.entry_point_function = function
133
si
134
si
135
si