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
use super::LLVMRef;
use super::context::Context;
use super::value::Value;
use libc::c_char;
use llvm::core::{
LLVMBuildAdd,
LLVMBuildRet,
LLVMBuildRetVoid,
LLVMCreateBuilderInContext,
LLVMDisposeBuilder,
LLVMPositionBuilderAtEnd
};
use llvm::prelude::{
LLVMBasicBlockRef,
LLVMBuilderRef
};
use std::ffi::CString;
#[derive(Debug)]
pub struct Builder {
builder: LLVMBuilderRef,
owned : bool
}
impl Builder {
pub fn new(context: &Context) -> Builder {
Builder {
builder: unsafe {
LLVMCreateBuilderInContext(context.to_ref())
},
owned: true
}
}
pub fn move_to_end(&mut self, basic_block: BasicBlock) {
unsafe {
LLVMPositionBuilderAtEnd(self.to_ref(), basic_block.to_ref());
}
}
pub fn return_void(&mut self) -> Value {
Value::from_ref(
unsafe {
LLVMBuildRetVoid(self.to_ref())
}
)
}
pub fn return_value(&mut self, value: Value) -> Value {
Value::from_ref(
unsafe {
LLVMBuildRet(self.to_ref(), value.to_ref())
}
)
}
pub fn add(&mut self, lhs: Value, rhs: Value, name: &str) -> Value {
let name = CString::new(name).unwrap();
Value::from_ref(
unsafe {
LLVMBuildAdd(
self.to_ref(),
lhs.to_ref(),
rhs.to_ref(),
name.as_ptr() as *const c_char
)
}
)
}
}
impl Drop for Builder {
fn drop(&mut self) {
if self.owned {
unsafe {
LLVMDisposeBuilder(self.builder);
}
}
}
}
impl LLVMRef<LLVMBuilderRef> for Builder {
fn to_ref(&self) -> LLVMBuilderRef {
self.builder
}
}
pub struct BasicBlock {
block: LLVMBasicBlockRef
}
impl BasicBlock {
pub fn from_ref(basic_block_ref: LLVMBasicBlockRef) -> BasicBlock {
BasicBlock {
block: basic_block_ref
}
}
}
impl LLVMRef<LLVMBasicBlockRef> for BasicBlock {
fn to_ref(&self) -> LLVMBasicBlockRef {
self.block
}
}
#[cfg(test)]
mod tests {
use super::Builder;
use super::super::context::Context;
use super::super::function::Function;
use super::super::module::Module;
use super::super::native_type::{
VMRepresentation,
int1_type,
int8_type,
void_type
};
#[test]
fn case_ownership() {
let context = Context::new();
let builder = Builder::new(&context);
assert!(builder.owned);
}
#[test]
fn case_return_void() {
let context = Context::new();
let module = Module::new("foobar", &context);
let mut builder = Builder::new(&context);
let function = Function::new(&module, "f", &mut [], void_type(&context));
let basic_block = function.new_basic_block("entry");
builder.move_to_end(basic_block);
builder.return_void();
assert_eq!(
"; ModuleID = 'foobar'\n".to_string() +
"source_filename = \"foobar\"\n" +
"\n" +
"define void @f() {\n" +
"entry:\n" +
" ret void\n" +
"}\n",
format!("{}", module)
);
}
#[test]
fn case_return_bool() {
let context = Context::new();
let module = Module::new("foobar", &context);
let mut builder = Builder::new(&context);
let function = Function::new(&module, "f", &mut [], int1_type(&context));
let basic_block = function.new_basic_block("entry");
builder.move_to_end(basic_block);
builder.return_value(true.to_vm_representation(&context));
assert_eq!(
"; ModuleID = 'foobar'\n".to_string() +
"source_filename = \"foobar\"\n" +
"\n" +
"define i1 @f() {\n" +
"entry:\n" +
" ret i1 true\n" +
"}\n",
format!("{}", module)
);
}
#[test]
fn case_return_integer() {
let context = Context::new();
let module = Module::new("foobar", &context);
let mut builder = Builder::new(&context);
let function = Function::new(&module, "f", &mut [], int8_type(&context));
let basic_block = function.new_basic_block("entry");
builder.move_to_end(basic_block);
builder.return_value(42u8.to_vm_representation(&context));
assert_eq!(
"; ModuleID = 'foobar'\n".to_string() +
"source_filename = \"foobar\"\n" +
"\n" +
"define i8 @f() {\n" +
"entry:\n" +
" ret i8 42\n" +
"}\n",
format!("{}", module)
);
}
#[test]
fn case_add_constants() {
let context = Context::new();
let module = Module::new("foobar", &context);
let mut builder = Builder::new(&context);
let function = Function::new(&module, "f", &mut [], int8_type(&context));
let basic_block = function.new_basic_block("entry");
builder.move_to_end(basic_block);
let addition = builder.add(
7u8.to_vm_representation(&context),
42u8.to_vm_representation(&context),
"addition"
);
builder.return_value(addition);
assert_eq!(
"; ModuleID = 'foobar'\n".to_string() +
"source_filename = \"foobar\"\n" +
"\n" +
"define i8 @f() {\n" +
"entry:\n" +
" ret i8 49\n" +
"}\n",
format!("{}", module)
);
}
}