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
use crate::*;
#[cfg_attr(feature = "io", derive(Serialize, Deserialize))]
pub struct BlockPayload {
pub(crate) arguments: Vec<Value>,
pub(crate) instructions: Vec<Value>,
}
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "io", derive(Serialize, Deserialize))]
pub struct Block(pub(crate) generational_arena::Index);
pub struct BlockDisplayer<'a> {
pub(crate) block: Block,
pub(crate) library: &'a Library,
}
impl<'a> std::fmt::Display for BlockDisplayer<'a> {
fn fmt(
&self,
writer: &mut std::fmt::Formatter<'_>,
) -> std::result::Result<(), std::fmt::Error> {
write!(writer, "b{}(", self.block.get_unique_index())?;
for i in 0..self.block.get_num_args(self.library) {
if i > 0 {
write!(writer, ", ")?;
}
let arg = self.block.get_arg(self.library, i);
write!(
writer,
"{} : {}",
arg.get_displayer(self.library),
arg.get_type(self.library).get_displayer(self.library)
)?;
}
write!(writer, ")")
}
}
impl UniqueIndex for Block {
fn get_unique_index(&self) -> usize {
self.0.into_raw_parts().0
}
}
impl Block {
/// Get an argument from a block.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let function = module.create_function(&mut library).with_name("func").build();
/// # let ty = library.get_uint_type(32);
/// # let _ = function.create_block(&mut library).build();
/// let block = function.create_block(&mut library).with_arg(ty).build();
/// let arg = block.get_arg(&library, 0);
/// assert_eq!(arg.get_type(&library), ty);
/// ```
pub fn get_arg(&self, library: &Library, index: usize) -> Value {
let block = &library.blocks[self.0];
assert!(
index < block.arguments.len(),
"Argument index {} is invalid {}",
index,
block.arguments.len()
);
block.arguments[index]
}
/// Get an argument from a block.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let function = module.create_function(&mut library).with_name("func").build();
/// # let ty = library.get_uint_type(32);
/// # let _ = function.create_block(&mut library).build();
/// let block = function.create_block(&mut library).with_arg(ty).build();
/// let num_args = block.get_num_args(&library);
/// assert_eq!(num_args, 1);
/// ```
pub fn get_num_args(&self, library: &Library) -> usize {
let block = &library.blocks[self.0];
block.arguments.len()
}
/// Add instructions to the block.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let function = module.create_function(&mut library).with_name("func").build();
/// # let block = function.create_block(&mut library).build();
/// let instruction_builder = block.create_instructions(&mut library);
/// ```
pub fn create_instructions<'a>(&self, library: &'a mut Library) -> InstructionBuilder<'a> {
InstructionBuilder::with_library_and_block(library, *self)
}
/// Get all the instructions in a block.
///
/// # 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").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.stack_alloc("😀", u32_ty, None);
/// instruction_builder.ret(None);
/// let mut instructions = block.get_insts(&library);
/// assert_eq!(instructions.nth(0).unwrap(), instruction);
/// ```
pub fn get_insts(&self, library: &Library) -> ValueIterator {
let block = &library.blocks[self.0];
ValueIterator::new(&block.instructions)
}
/// Get all the arguments in a block.
///
/// # 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_arg("a", u32_ty).build();
/// # let block = function.create_block(&mut library).with_arg(u32_ty).build();
/// let mut args = block.get_args(&library);
/// assert_eq!(args.nth(0).unwrap().get_type(&library), u32_ty);
/// ```
pub fn get_args(&self, library: &Library) -> ValueIterator {
let block = &library.blocks[self.0];
ValueIterator::new(&block.arguments)
}
/// Get a mutable reference to all the arguments in a block.
///
/// # 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_arg("a", u32_ty).build();
/// # let block = function.create_block(&mut library).with_arg(u32_ty).build();
/// let mut args = block.get_args_mut(&mut library);
/// # args.push(u32_ty);
/// ```
pub fn get_args_mut<'a>(&self, library: &'a mut Library) -> BlockArguments<'a> {
BlockArguments::new(library, *self)
}
pub fn get_displayer<'a>(&self, library: &'a Library) -> BlockDisplayer<'a> {
BlockDisplayer {
block: *self,
library,
}
}
}
pub struct BlockBuilder<'a> {
library: &'a mut Library,
function: Function,
argument_types: Vec<Type>,
}
impl<'a> BlockBuilder<'a> {
pub(crate) fn with_library_and_function(library: &'a mut Library, function: Function) -> Self {
BlockBuilder {
library,
function,
argument_types: Default::default(),
}
}
/// Add argument types for the block.
///
/// The default is no argument types.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let function = module.create_function(&mut library).with_name("func").build();
/// # let i8_ty = library.get_int_type(8);
/// # let u32_ty = library.get_uint_type(32);
/// # let block_builder = function.create_block(&mut library);
/// block_builder.with_arg(i8_ty).with_arg(u32_ty);
/// ```
pub fn with_arg(mut self, ty: Type) -> Self {
self.argument_types.push(ty);
self
}
/// Finalize and build the block.
///
/// # Examples
///
/// ```
/// # use yair::*;
/// # let mut library = Library::new();
/// # let module = library.create_module().build();
/// # let function = module.create_function(&mut library).with_name("func").build();
/// # let block_builder = function.create_block(&mut library);
/// let block = block_builder.build();
/// ```
pub fn build(self) -> Block {
let mut block = BlockPayload {
arguments: Vec::new(),
instructions: Vec::new(),
};
let name = self.library.get_name("");
let function = &mut self.library.functions[self.function.0];
for argument_type in self.argument_types {
let argument = self.library.values.insert(ValuePayload::Argument(Argument {
name,
ty: argument_type,
}));
block.arguments.push(Value(argument));
}
let block = Block(self.library.blocks.insert(block));
function.blocks.push(block);
block
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic]
fn bad_arg_index() {
let mut library = Library::new();
let module = library.create_module().build();
let function = module
.create_function(&mut library)
.with_name("func")
.build();
let block = function.create_block(&mut library).build();
let _ = block.get_arg(&library, 0);
}
#[test]
fn first_had_args() {
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_arg("a", u32_ty)
.with_name("func")
.build();
let _ = function.create_block(&mut library).with_arg(u32_ty).build();
}
}