Skip to content
← Back

src/ir/llvm/llvm_arrays.ghul

1
namespace IR.LLVMTarget is
2
use IR.Program
3
4
use Semantic.Types.Type
5
6
use LLVM.BLOCK
7
use LLVM.Conversion
8
use LLVM.FUNCTION
9
use LLVM.IntegerFlags
10
use LLVM.Intrinsics
11
use LLVM.MODULE
12
use LLVM.PARAMETER
13
use LLVM.Types
14
use LLVM.Values
15
16
/// An array on the LLVM target: one object holding its type's
17
/// descriptor, a word nothing reads, the number of elements and the
18
/// elements, laid out as `{ ptr, i32, i32, [N x E] }`, so the count sits
19
/// where a string's does.
20
///
21
/// An element of a kind narrower than 32 bits is stored at its own
22
/// width, so a `ubyte[]` holds one byte for each element.
23
class LLVM_ARRAYS is
24
_module: MODULE
25
_strings: LLVM_STRINGS
26
_classes: CLASS_SET
27
_objects: LLVM_OBJECTS
28
29
/// The offset of the count of elements within an array.
30
COUNT_OFFSET: int static => 12
31
32
/// The offset of the first element within an array.
33
ELEMENTS_OFFSET: int static => 16
34
35
/// - module: where descriptors and the runtime's functions are added
36
/// - strings: where the runtime's functions are declared
37
/// - classes: the program's classes, which the array types it uses
38
/// are added to
39
/// - objects: where each array type's descriptor is kept, as a class's is
40
init(module: MODULE, strings: LLVM_STRINGS, classes: CLASS_SET, objects: LLVM_OBJECTS) is
41
_module = module
42
_strings = strings
43
_classes = classes
44
_objects = objects
45
si
46
47
/// Whether the target can hold an array of `element`: a scalar or a
48
/// reference, and a struct, a tuple or an optional value type, which
49
/// is held in place.
50
holds_element(element: Type) -> bool static =>
51
storage_type(element)? \/ element.is_value_type
52
53
// The type an element of `element` is stored as: a struct or a tuple
54
// as its fields, in place.
55
_storage(element: Type) -> LLVM.Type =>
56
storage_type(element) ?? _objects.held_type(element)!
57
58
/// The type an element of `element` is stored as, or null where the
59
/// target holds no values of it.
60
storage_type(element: Type) -> LLVM.Type? static =>
61
LLVM_TYPES.stored_type(element) ?? LLVM_TYPES.value_type(element)
62
63
/// The array type `type` is, or null where it is not an array the
64
/// target holds.
65
array_of(type: Type?) -> CLASS_INSTANCE? => _classes.array_of(type)
66
67
/// The element type of `array`.
68
element_of(array: CLASS_INSTANCE) -> Type => _classes.array_element(array)
69
70
/// The descriptor of `array`, one for each array type, numbered as a
71
/// class under the root.
72
descriptor(array: CLASS_INSTANCE) -> LLVM.Value => Values.global(_objects.descriptor_name(array))
73
74
/// A new array of `array` holding `count` elements, each zero. An
75
/// array whose elements can hold a pointer is one the collector scans.
76
/// - block: where the allocation goes
77
/// - array: the array type
78
/// - count: the number of elements, an `i32`
79
allocate(block: BLOCK, array: CLASS_INSTANCE, count: LLVM.Value) -> LLVM.Value is
80
let storage = _storage(element_of(array))
81
82
let wide = block.convert(Conversion.SIGN_EXTEND, count, Types.i64)
83
let bytes = block.binary(LLVM.BinaryOperator.MUL, IntegerFlags.NONE, wide, Values.i64(cast long(LLVM_TYPES.size_of(storage))))
84
let size = block.binary(LLVM.BinaryOperator.ADD, IntegerFlags.NONE, bytes, Values.i64(cast long(ELEMENTS_OFFSET)))
85
86
let scanned = LLVM_TYPES.holds_pointer(storage)
87
88
let result = _strings.allocate_object(block, descriptor(array), size, scanned)
89
90
block.store(count, block.element_pointer(Types.i8, result, [Values.i32(COUNT_OFFSET)], true), 4)
91
92
return result
93
si
94
95
/// The number of elements in the array `value` points at.
96
count(block: BLOCK, value: LLVM.Value) -> LLVM.Value =>
97
block.load(Types.i32, block.element_pointer(Types.i8, value, [Values.i32(COUNT_OFFSET)], true), 4)
98
99
/// The element at `index` in the array `value` points at, widened
100
/// to the type it is held in.
101
load(block: BLOCK, array: CLASS_INSTANCE, value: LLVM.Value, index: LLVM.Value) -> LLVM.Value is
102
let element = element_of(array)
103
let storage = _storage(element)
104
105
let loaded = block.load(storage, _address(block, storage, value, index), 0)
106
107
return if let scalar = LLVM_TYPES.scalar(element) then LLVM_TYPES.widen(block, loaded, scalar) else loaded fi
108
si
109
110
/// Stores `element` at `index` in the array `value` points at,
111
/// narrowed to the width it is stored at.
112
store(block: BLOCK, array: CLASS_INSTANCE, value: LLVM.Value, index: LLVM.Value, element: LLVM.Value) is
113
let element_type = element_of(array)
114
let storage = _storage(element_type)
115
116
let stored = if let scalar = LLVM_TYPES.scalar(element_type) then LLVM_TYPES.narrow(block, element, scalar) else element fi
117
118
block.store(stored, _address(block, storage, value, index), 0)
119
si
120
121
/// Adds the function the native runtime calls to make a `ubyte[]`,
122
/// which every program defines.
123
add_exports() is
124
let lookup = IoC.CONTAINER.instance.innate_symbol_lookup
125
126
let function =
127
FUNCTION(
128
LLVM_STRINGS.MAKE_BYTES_NAME,
129
Types.pointer,
130
[PARAMETER(Types.pointer, "bytes"), PARAMETER(Types.i32, "length")])
131
132
let block = function.add_block("entry")
133
134
if let array = array_of(lookup.get_array_type(lookup.get_ubyte_type())) then
135
let length = function.parameter(1)
136
let result = allocate(block, array, length)
137
138
block.call(
139
_module.declare_intrinsic(Intrinsics.memcpy()),
140
[
141
block.element_pointer(Types.i8, result, [Values.i32(ELEMENTS_OFFSET)], true),
142
function.parameter(0),
143
block.convert(Conversion.SIGN_EXTEND, length, Types.i64),
144
Values.boolean(false)
145
])
146
147
block.return_value(result)
148
else
149
block.return_value(Values.null_pointer)
150
fi
151
152
_module.add_function(function)
153
si
154
155
/// The address of the element at `index` in the array `value` points
156
/// at. The index is not checked.
157
element_address(block: BLOCK, array: CLASS_INSTANCE, value: LLVM.Value, index: LLVM.Value) -> LLVM.Value =>
158
_address(block, _storage(element_of(array)), value, index)
159
160
_address(block: BLOCK, storage: LLVM.Type, value: LLVM.Value, index: LLVM.Value) -> LLVM.Value =>
161
block.element_pointer(
162
storage,
163
block.element_pointer(Types.i8, value, [Values.i32(ELEMENTS_OFFSET)], true),
164
[index],
165
true)
166
167
si
168
si