1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use crate::*;
#[derive(Debug)]
#[cfg_attr(feature = "io", derive(Serialize, Deserialize))]
pub(crate) enum ValuePayload {
Undef(Type),
Argument(Argument),
Instruction(Instruction),
Constant(Constant),
Global(Global),
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "io", derive(Serialize, Deserialize))]
pub struct Value(pub(crate) generational_arena::Index);
pub struct ValueDisplayer {
pub(crate) value: Value,
}
impl<'a> std::fmt::Display for ValueDisplayer {
fn fmt(
&self,
writer: &mut std::fmt::Formatter<'_>,
) -> std::result::Result<(), std::fmt::Error> {
write!(writer, "v{}", self.value.get_unique_index())
}
}
impl UniqueIndex for Value {
fn get_unique_index(&self) -> usize {
self.0.into_raw_parts().0
}
}
impl Value {
/// Return true if a value is an undef.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let u32_ty = library.get_uint_type(32);
/// # let value = library.get_undef(u32_ty);
/// let is_undef = value.is_undef(&library);
/// # assert!(is_undef);
/// ```
pub fn is_undef(&self, library: &Library) -> bool {
matches!(library.values[self.0], ValuePayload::Undef(_))
}
/// Return true if a value is a constant.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let value = library.get_bool_constant(true);
/// let is_constant = value.is_constant(&library);
/// # assert!(is_constant);
/// ```
pub fn is_constant(&self, library: &Library) -> bool {
matches!(library.values[self.0], ValuePayload::Constant(_))
}
/// If the value is a constant, get the constant, otherwise panic.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let value = library.get_bool_constant(true);
/// let constant = value.get_constant(&library);
/// # match constant {
/// # Constant::Bool(c, _) => assert!(c),
/// # _ => panic!("Bad constant"),
/// # }
/// ```
pub fn get_constant<'a>(&self, library: &'a Library) -> &'a Constant {
match &library.values[self.0] {
ValuePayload::Constant(c) => &c,
_ => panic!("Cannot get the constant from a non-constant value"),
}
}
/// Return true if a value is an instruction.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let u32_ty = library.get_uint_type(32);
/// # let function = module.create_function(&mut library).with_name("func").with_return_type(u32_ty).build();
/// # let _ = function.create_block(&mut library).build();
/// # let block = function.create_block(&mut library).build();
/// # let constant = library.get_uint_constant(32, 42);
/// # let mut instruction_builder = block.create_instructions(&mut library);
/// # let instruction = instruction_builder.ret_val(constant, None);
/// let is_inst = instruction.is_inst(&library);
/// # assert!(is_inst);
/// ```
pub fn is_inst(&self, library: &Library) -> bool {
matches!(library.values[self.0], ValuePayload::Instruction(_))
}
/// If the value is an instruction, get the instruction, otherwise panic.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let u32_ty = library.get_uint_type(32);
/// # let function = module.create_function(&mut library).with_name("func").with_return_type(u32_ty).build();
/// # let _ = function.create_block(&mut library).build();
/// # let block = function.create_block(&mut library).build();
/// # let constant = library.get_uint_constant(32, 42);
/// # let mut instruction_builder = block.create_instructions(&mut library);
/// # let value = instruction_builder.ret_val(constant, None);
/// let instruction = value.get_inst(&library);
/// ```
pub fn get_inst<'a>(&self, library: &'a Library) -> &'a Instruction {
match &library.values[self.0] {
ValuePayload::Instruction(i) => &i,
_ => panic!("Cannot get the instruction from a non-instruction value"),
}
}
/// Return true if the value is a global export.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().with_name("module").build();
/// # let u32_ty = library.get_uint_type(32);
/// # let global = module.create_global(&mut library).with_attributes(GlobalAttributes::only(GlobalAttribute::Export)).with_name("global").with_type(u32_ty).build();
/// let is_export = global.is_export(&library);
/// # assert!(is_export);
/// ```
pub fn is_export(&self, library: &Library) -> bool {
match &library.values[self.0] {
ValuePayload::Global(g) => g.attributes.contains(GlobalAttribute::Export),
_ => false,
}
}
/// Get the domain of the global value.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().with_name("module").build();
/// # let u32_ty = library.get_uint_type(32);
/// # let global = module.create_global(&mut library).with_domain(Domain::Cpu).with_name("global").with_type(u32_ty).build();
/// let domain = global.get_global_domain(&library);
/// # assert_eq!(domain, Domain::Cpu);
/// ```
pub fn get_global_domain(&self, library: &Library) -> Domain {
match &library.values[self.0] {
ValuePayload::Global(g) => g.ptr_ty.get_domain(library),
_ => std::unreachable!(),
}
}
/// Get the type that backs the global value.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().with_name("module").build();
/// # let u32_ty = library.get_uint_type(32);
/// # let global = module.create_global(&mut library).with_domain(Domain::Cpu).with_name("global").with_type(u32_ty).build();
/// let ty = global.get_global_backing_type(&library);
/// # assert_eq!(ty, u32_ty);
/// ```
pub fn get_global_backing_type(&self, library: &Library) -> Type {
match &library.values[self.0] {
ValuePayload::Global(g) => g.ty,
_ => std::unreachable!(),
}
}
/// Get the location of a value.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let global = module.create_global(&mut library).with_name("var").build();
/// let location = global.get_location(&library);
/// # assert_eq!(None, location);
/// # let location = library.get_location("foo.ya", 0, 13);
/// # let global = module.create_global(&mut library).with_name("var").with_location(location).build();
/// # let location = global.get_location(&library);
/// # assert!(location.is_some());
/// ```
pub fn get_location(&self, library: &Library) -> Option<Location> {
match &library.values[self.0] {
ValuePayload::Instruction(i) => match i {
Instruction::Return(location) => *location,
Instruction::ReturnValue(_, _, location) => *location,
Instruction::Cmp(_, _, _, _, location) => *location,
Instruction::Unary(_, _, _, location) => *location,
Instruction::Binary(_, _, _, _, location) => *location,
Instruction::Cast(_, _, location) => *location,
Instruction::BitCast(_, _, location) => *location,
Instruction::Load(_, _, location) => *location,
Instruction::Store(_, _, _, location) => *location,
Instruction::Extract(_, _, location) => *location,
Instruction::Insert(_, _, _, location) => *location,
Instruction::StackAlloc(_, _, _, location) => *location,
Instruction::Call(_, _, location) => *location,
Instruction::Branch(_, _, location) => *location,
Instruction::ConditionalBranch(_, _, _, _, _, location) => *location,
Instruction::Select(_, _, _, _, location) => *location,
Instruction::IndexInto(_, _, _, location) => *location,
},
ValuePayload::Global(g) => g.location,
_ => None,
}
}
pub fn get_displayer(&self, _: &Library) -> ValueDisplayer {
ValueDisplayer { value: *self }
}
pub fn get_inst_displayer<'a>(&self, library: &'a Library) -> InstructionDisplayer<'a> {
InstructionDisplayer {
value: *self,
library,
}
}
}
impl Named for Value {
/// Get the name of a value.
fn get_name(&self, library: &Library) -> Name {
match &library.values[self.0] {
ValuePayload::Undef(_) => panic!("Undef values cannot have names"),
ValuePayload::Argument(arg) => arg.get_name(library),
ValuePayload::Instruction(inst) => inst.get_name(library),
ValuePayload::Constant(_) => panic!("Constants cannot have names"),
ValuePayload::Global(glbl) => glbl.get_name(library),
}
}
}
impl Typed for Value {
/// Get the type of a value.
fn get_type(&self, library: &Library) -> Type {
match &library.values[self.0] {
ValuePayload::Undef(ty) => *ty,
ValuePayload::Argument(arg) => arg.get_type(library),
ValuePayload::Instruction(inst) => inst.get_type(library),
ValuePayload::Constant(cnst) => cnst.get_type(library),
ValuePayload::Global(glbl) => glbl.get_type(library),
}
}
}
pub struct ValueIterator {
vec: Vec<Value>,
next: usize,
}
impl ValueIterator {
pub(crate) fn new(iter: &[Value]) -> ValueIterator {
ValueIterator {
vec: iter.to_vec(),
next: 0,
}
}
}
impl Iterator for ValueIterator {
type Item = Value;
fn next(&mut self) -> Option<Self::Item> {
if self.next < self.vec.len() {
let next = self.next;
self.next += 1;
Some(self.vec[next])
} else {
None
}
}
}