Skip to content
← Back

src/ir/llvm/llvm_names.ghul

1
namespace IR.LLVMTarget is
2
use IR.Program
3
4
use Collections.MAP
5
use Collections.SET
6
7
use Semantic.Symbols.Function
8
use Semantic.Symbols.FRAME
9
use Semantic.Symbols.ScopedWithEnclosingScope
10
use Semantic.Symbols.Symbol
11
12
/// The names functions and globals are written with in an LLVM module.
13
///
14
/// Each name is a readable label for what it stands for: a function's
15
/// qualified name and type arguments, a type's descriptor as the type's
16
/// name with `.$descriptor` after it, and so on. Where an earlier name
17
/// already holds a label, a numeric suffix tells them apart. Names are
18
/// given in the order they are asked for, so a program is named the
19
/// same way every time it is compiled.
20
///
21
/// A part the compiler adds starts with `$`, which no ghūl identifier
22
/// can, so a label never clashes with a declared name. Only the names
23
/// the runtime library itself defines or expects start `$ghul_`.
24
class LLVM_NAMES is
25
_names: MAP[FUNCTION_INSTANCE, string]
26
_taken: SET[string]
27
28
init() is
29
_names = MAP[FUNCTION_INSTANCE, string]()
30
_taken = SET[string]()
31
si
32
33
/// Keeps `name` from being given to anything else, for a name the
34
/// module declares for itself.
35
///
36
/// - name: the name to keep
37
reserve(name: string) is
38
_taken.add(name)
39
si
40
41
/// The name of the function `instance`, chosen the first time it is
42
/// asked for.
43
///
44
/// - instance: the function instance to name
45
name_of(instance: FUNCTION_INSTANCE) -> string is
46
let name mut = ""
47
48
if _names.try_get_value(instance, name ref) then
49
return name
50
fi
51
52
name = unique(label(instance))
53
54
_names[instance] = name
55
56
return name
57
si
58
59
/// The longest a label is kept before a name is made from it.
60
MAX_LABEL_LENGTH: int static => 96
61
62
/// A name no earlier one holds: `label`, cut to `MAX_LABEL_LENGTH`
63
/// code units, itself or with the first numeric suffix that is free.
64
///
65
/// - label: what the name should read as
66
unique(label: string) -> string is
67
let cut = shortened(label)
68
let name mut = cut
69
let suffix mut = 1
70
71
while !_taken.add(name) do
72
name = "{cut}.{suffix}"
73
suffix = suffix + 1
74
od
75
76
return name
77
si
78
79
/// `label`, cut to at most `MAX_LABEL_LENGTH` code units without
80
/// splitting a surrogate pair.
81
///
82
/// - label: the label to cut
83
shortened(label: string) -> string static is
84
if label.length <= MAX_LABEL_LENGTH then
85
return label
86
fi
87
88
let length = if label[MAX_LABEL_LENGTH - 1] >= '\uD800' /\ label[MAX_LABEL_LENGTH - 1] <= '\uDBFF' then MAX_LABEL_LENGTH - 1 else MAX_LABEL_LENGTH fi
89
90
return label.substring(0, length)
91
si
92
93
/// The label of the function `instance`, without giving it a name.
94
///
95
/// - instance: the function instance to label
96
label(instance: FUNCTION_INSTANCE) -> string =>
97
if instance.type_map.count == 0 then
98
function_label(instance.definition)
99
else
100
"{function_label(instance.definition)}[{instance.type_map.values |> Ghul.Pipes.join(",")}]"
101
fi
102
103
/// The label of the class instance `type`.
104
///
105
/// - type: the class instance to label
106
type_label(type: CLASS_INSTANCE) -> string is
107
let name =
108
if let frame: FRAME = type.definition then
109
frame_label(frame)
110
else
111
type.definition.qualified_name
112
fi
113
114
return if type.arguments.count == 0 then name else "{name}[{type.arguments |> Ghul.Pipes.join(",")}]" fi
115
si
116
117
// A function literal has no name of its own, so it and the frame
118
// holding what it captures are labelled after the function it is
119
// written in. A function a frame declares is labelled after the frame.
120
function_label(definition: Function) -> string =>
121
if definition.is_closure then
122
"{_enclosing_label(definition)}.$closure"
123
elif let frame: FRAME = definition.owner then
124
"{frame_label(frame)}.{definition.name}"
125
else
126
definition.qualified_name
127
fi
128
129
frame_label(frame: FRAME) -> string => "{_enclosing_label(frame.closure)}.$frame"
130
131
// The label of the function a literal is written in, through any
132
// block or literal it is nested in; at namespace scope, the
133
// namespace's name.
134
_enclosing_label(literal: Function) -> string is
135
let scope: Semantic.Scope? mut = literal.enclosing_scope
136
137
while scope? do
138
if let function: Function = scope /\ !function.is_closure then
139
return function_label(function)
140
elif let block: Semantic.BLOCK_SCOPE = scope then
141
scope = block.enclosing
142
elif let scoped: ScopedWithEnclosingScope = scope then
143
scope = scoped.enclosing_scope
144
elif let symbol: Symbol = scope then
145
return symbol.qualified_name
146
else
147
scope = null
148
fi
149
od
150
151
return "$global"
152
si
153
si
154
si