Skip to content
← Back

src/ir/values/declare_local.ghul

1
namespace IR.Values is
2
use Semantic.Types.Type
3
4
// Declares a local.
5
//
6
// Being in the block gets the ordering right for free: a
7
// declaration is emitted before the loads that follow it, so the
8
// local has a slot by the time one is encoded.
9
class DECLARE_LOCAL: Value is
10
local: IR.LOCAL
11
12
type: Type => local.type
13
14
init(local: IR.LOCAL) is
15
super.init()
16
17
self.local = local
18
si
19
20
accept(visitor: Visitor) is
21
visitor.visit(self)
22
si
23
24
to_string() -> string => "declare-local:[{type}]({local})"
25
si
26
27
// Stores the result an earlier value left into a local.
28
class STORE_TEMP: Value is
29
local: IR.LOCAL
30
31
type: Type => local.type
32
33
init(local: IR.LOCAL) is
34
super.init()
35
36
self.local = local
37
si
38
39
accept(visitor: Visitor) is
40
visitor.visit(self)
41
si
42
43
to_string() -> string => "store-temp:[{type}]({local})"
44
si
45
46
// Stores `value` in a local and also leaves it as this value's
47
// result.
48
class TEE_TEMP: Value is
49
local: IR.LOCAL
50
value: Value
51
52
type: Type? pure => value.type
53
54
has_location: bool => false
55
56
init(local: IR.LOCAL, value: Value) is
57
super.init()
58
59
self.local = local
60
self.value = value
61
si
62
63
accept(visitor: Visitor) is
64
visitor.visit(self)
65
si
66
67
to_string() -> string => "tee-temp:[{type}]({local}, {value})"
68
si
69
70
// Evaluates `value` for its effects and discards its result, or,
71
// with no value, discards the result an earlier value left: the
72
// exception a catch handler starts with, or a value a join leaves
73
// where nothing consumes it.
74
class DISCARD: Value is
75
value: Value?
76
77
has_location: bool => false
78
79
init() is
80
super.init()
81
si
82
83
init(value: Value) is
84
super.init()
85
86
self.value = value
87
si
88
89
accept(visitor: Visitor) is
90
visitor.visit(self)
91
si
92
93
to_string() -> string => "discard({value})"
94
si
95
96
// Throws `exception`, or, with no value, the exception an earlier
97
// value left.
98
class THROW: Value is
99
exception: Value?
100
101
has_location: bool => false
102
103
init() is
104
super.init()
105
si
106
107
init(exception: Value) is
108
super.init()
109
110
self.exception = exception
111
si
112
113
accept(visitor: Visitor) is
114
visitor.visit(self)
115
si
116
117
to_string() -> string => "throw({exception})"
118
si
119
120
// Returns from the enclosing method. A value for the same reason
121
// as DECLARE_LOCAL: silently dropping an instruction produces a
122
// body that fails verification with nothing to point at.
123
class RET: Value is
124
init() is
125
super.init()
126
si
127
128
accept(visitor: Visitor) is
129
visitor.visit(self)
130
si
131
132
to_string() -> string => "ret"
133
si
134
135
// The value a non-void function falls out of the bottom with: a
136
// fresh local of the return type, never assigned, loaded so the
137
// trailing `ret` has something to return. The local is anonymous
138
// and used once, so it is declared and loaded together rather than
139
// through the usual declare-then-load pair.
140
class DEFAULT_RETURN: Value is
141
type: Type
142
143
init(type: Type) is
144
super.init()
145
146
self.type = type
147
si
148
149
accept(visitor: Visitor) is
150
visitor.visit(self)
151
si
152
153
to_string() -> string => "default-return:[{type}]()"
154
si
155
si