Skip to content
← Back

src/ir/llvm/llvm_display.ghul

1
namespace IR.LLVMTarget is
2
use IR.Program
3
4
use Collections.LIST
5
6
use LLVM.FUNCTION
7
use LLVM.IntegerPredicate
8
use LLVM.MODULE
9
use LLVM.PARAMETER
10
use LLVM.Types
11
use LLVM.Values
12
13
/// The displayers: for each type with no text of its own, a function
14
/// that writes a value of it for display through the core library's
15
/// display state. A sequence is written as its elements, a tuple as its
16
/// parts, and anything else as its type and public members.
17
///
18
/// A displayer takes the value and the state, and is reached through
19
/// the value's descriptor, only where the program renders values for
20
/// display at all.
21
class LLVM_DISPLAY is
22
_module: MODULE
23
_objects: LLVM_OBJECTS
24
_plan: DISPLAY_PLAN
25
_strings: LLVM_STRINGS
26
_callee: (FUNCTION_INSTANCE) -> FUNCTION?
27
28
/// - module: where the displayers are added
29
/// - objects: the program's classes, whose descriptors hold the displayers
30
/// - plan: which classes get a displayer, and what each calls
31
/// - strings: where the names a displayer writes are made
32
/// - callee: the compiled function of each instance a displayer calls
33
init(module: MODULE, objects: LLVM_OBJECTS, plan: DISPLAY_PLAN, strings: LLVM_STRINGS, callee: (FUNCTION_INSTANCE) -> FUNCTION?) is
34
_module = module
35
_objects = objects
36
_plan = plan
37
_strings = strings
38
_callee = callee
39
si
40
41
/// Adds the displayer of each class that has one, and records it
42
/// for the class's descriptor.
43
add() is
44
let helpers = _plan.helpers
45
46
if !helpers? then
47
return
48
fi
49
50
for type in _objects.classes.classes_and_arrays do
51
if _objects.is_instantiated(type) then
52
if let displayer = _displayer(type, helpers) then
53
_module.add_function(displayer)
54
_objects.set_displayer(type, displayer)
55
fi
56
fi
57
od
58
si
59
60
// The displayer of `type`, or null where it has none, or something
61
// it calls was not compiled.
62
_displayer(type: CLASS_INSTANCE, helpers: DISPLAY_HELPERS) -> FUNCTION? is
63
let calls = _plan.calls(type, helpers)
64
65
if calls.count == 0 then
66
return null
67
fi
68
69
let functions = LIST[FUNCTION]()
70
71
for call in calls do
72
if let function = _callee(call) then
73
functions.add(function)
74
else
75
return null
76
fi
77
od
78
79
let displayer =
80
FUNCTION(
81
_objects.names.unique("{_objects.names.type_label(type)}.$display"),
82
LLVM.Type.VOID,
83
[PARAMETER(Types.pointer, "value"), PARAMETER(Types.pointer, "state")])
84
85
displayer.linkage = LLVM.Linkage.INTERNAL
86
87
let value = displayer.parameter(0)
88
let state = displayer.parameter(1)
89
let block mut = displayer.add_block("entry")
90
91
// A struct is displayed from its box, which holds the struct after
92
// the descriptor, and its methods are given the struct's address.
93
let receiver =
94
if _objects.is_struct(type) then
95
block.element_pointer(Types.structure([Types.pointer, _objects.struct_type(type)]), value, [Values.i32(0), Values.i32(1)], true)
96
else
97
value
98
fi
99
100
if _plan.element_type(type)? then
101
block.call(functions[0], [state, value])
102
block.return_void()
103
104
return displayer
105
fi
106
107
if _plan.is_tuple(type) then
108
block.call(functions[0], [state])
109
110
for i in 0..type.arguments.count do
111
if let part = _objects.load_stored(block, type.arguments[i], block.element_pointer(_objects.struct_type(type), receiver, [Values.i32(0), Values.i32(i)], true)) then
112
113
block.call(functions[i + 1], [state, part])
114
else
115
return null
116
fi
117
od
118
119
block.call(functions[functions.count - 1], [state])
120
block.return_void()
121
122
return displayer
123
fi
124
125
block.call(functions[0], [state, _strings.literal(_plan.display_name(type))])
126
127
let done = displayer.add_block("done")
128
129
// The calls after the opening one, in the order the plan gives
130
// them.
131
let next mut = 1
132
133
for member in _plan.members(type) do
134
let read: LLVM.Value mut
135
136
if let _ = member.getter then
137
read = block.call(functions[next], [receiver])!
138
next = next + 1
139
elif let `field = member.`field, position = _objects.field_position(`field), loaded = _objects.load_stored(block, member.type, block.element_pointer(_objects.struct_type(type), receiver, [Values.i32(0), Values.i32(position)], true)) then
140
read = loaded
141
else
142
return null
143
fi
144
145
let shown = block.call(functions[next], [state, _strings.literal(member.name), read])!
146
147
next = next + 1
148
149
// The state answers false once its element budget is spent.
150
let carry_on = displayer.add_block("member{next}")
151
152
block.branch(shown, carry_on, done)
153
block = carry_on
154
od
155
156
block.branch(done)
157
158
done.call(functions[functions.count - 1], [state])
159
done.return_void()
160
161
return displayer
162
si
163
si
164
si