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 libc::{c_char, c_uint};
use ffi::prelude::{LLVMBuilderRef, LLVMValueRef};
use ffi::{core, LLVMBuilder, LLVMIntPredicate, LLVMRealPredicate};
use cbox::CSemiBox;
use std::marker::PhantomData;
use block::BasicBlock;
use context::Context;
use types::Type;
use value::{Function, Predicate, Value};

static NULL_NAME: [c_char; 1] = [0];

/// This provides a uniform API for creating instructions and inserting them into a basic block.
pub struct Builder(PhantomData<[u8]>);
native_ref!(&Builder = LLVMBuilderRef);
dispose!{Builder, LLVMBuilder, core::LLVMDisposeBuilder}
macro_rules! bin_op(
    ($name:ident, $func:ident) => (
        pub fn $name(&self, left: &Value, right: &Value) -> &Value {
            unsafe { core::$func(self.into(), left.into(), right.into(), NULL_NAME.as_ptr()) }.into()
        }
    );
    ($name:ident, $ifunc:ident, $ffunc:ident) => (
        pub fn $name(&self, left: &Value, right: &Value) -> &Value {
            let ty = left.get_type();
            unsafe {
                (if ty.is_integer() {
                    core::$ifunc
                } else {
                    core::$ffunc
                })(self.into(), left.into(), right.into(), NULL_NAME.as_ptr()).into()
            }
        }
    );
);
macro_rules! un_op(
    ($name:ident, $func:ident) => (
        pub fn $name(&self, value: &Value) -> &Value {
            unsafe { core::$func(self.into(), value.into(), NULL_NAME.as_ptr() as *const c_char) }.into()
        }
    );
);
impl Builder {
    /// Create a new builder in the context given.
    pub fn new(context: &Context) -> CSemiBox<Builder> {
        CSemiBox::new(unsafe { core::LLVMCreateBuilderInContext(context.into()) }.into())
    }
    /// Position the builder at the end of `block`.
    pub fn position_at_end(&self, block: &BasicBlock) {
        unsafe { core::LLVMPositionBuilderAtEnd(self.into(), block.into()) }
    }
    /// Build an instruction that returns from the function with void.
    pub fn build_ret_void(&self) -> &Value {
        unsafe { core::LLVMBuildRetVoid(self.into()) }.into()
    }
    /// Build an instruction that returns from the function with `value`.
    pub fn build_ret(&self, value: &Value) -> &Value {
        unsafe { core::LLVMBuildRet(self.into(), value.into()) }.into()
    }
    /// Build an instruction that allocates an array with the element type `elem` and the size `size`.
    ///
    /// The size of this array will be the size of `elem` times `size`.
    pub fn build_array_alloca(&self, elem: &Type, size: &Value) -> &Value {
        unsafe {
            core::LLVMBuildArrayAlloca(
                self.into(),
                elem.into(),
                size.into(),
                NULL_NAME.as_ptr() as *const c_char,
            )
        }.into()
    }
    /// Build an instruction that allocates a pointer to fit the size of `ty` then returns this pointer.
    ///
    /// Make sure to call `build_free` with the pointer value when you're done with it, or you're
    /// gonna have a bad time.
    pub fn build_alloca(&self, ty: &Type) -> &Value {
        unsafe {
            core::LLVMBuildAlloca(self.into(), ty.into(), NULL_NAME.as_ptr() as *const c_char)
        }.into()
    }
    /// Build an instruction that frees the `val`, which _MUST_ be a pointer that was returned
    /// from `build_alloca`.
    pub fn build_free(&self, val: &Value) -> &Value {
        unsafe { core::LLVMBuildFree(self.into(), val.into()) }.into()
    }
    /// Build an instruction that store the value `val` in the pointer `ptr`.
    pub fn build_store(&self, val: &Value, ptr: &Value) -> &Value {
        unsafe { core::LLVMBuildStore(self.into(), val.into(), ptr.into()) }.into()
    }
    /// Build an instruction that branches to the block `dest`.
    pub fn build_br(&self, dest: &BasicBlock) -> &Value {
        unsafe { core::LLVMBuildBr(self.into(), dest.into()).into() }
    }

    pub fn get_insert_block<'a>(&self) -> &'a BasicBlock {
        unsafe { core::LLVMGetInsertBlock(self.into()) }.into()
    }
    /// Build an instruction that branches to `if_block` if `cond` evaluates to true, and `else_block` otherwise.
    pub fn build_cond_br(
        &self,
        cond: &Value,
        if_block: &BasicBlock,
        else_block: &BasicBlock,
    ) -> &Value {
        unsafe {
            core::LLVMBuildCondBr(self.into(), cond.into(), if_block.into(), else_block.into())
                .into()
        }
    }
    /// Build an instruction that calls the function `func` with the arguments `args`.
    ///
    /// This will return the return value of the function.
    pub fn build_call(&self, func: &Function, args: &[&Value]) -> &Value {
        unsafe {
            let call = core::LLVMBuildCall(
                self.into(),
                func.into(),
                args.as_ptr() as *mut LLVMValueRef,
                args.len() as c_uint,
                NULL_NAME.as_ptr(),
            );
            core::LLVMSetTailCall(call, 0);
            call.into()
        }
    }
    /// Build an instruction that calls the function `func` with the arguments `args`.
    ///
    /// This will return the return value of the function.
    pub fn build_tail_call(&self, func: &Function, args: &[&Value]) -> &Value {
        unsafe {
            let call = core::LLVMBuildCall(
                self.into(),
                func.into(),
                args.as_ptr() as *mut LLVMValueRef,
                args.len() as c_uint,
                NULL_NAME.as_ptr(),
            );
            core::LLVMSetTailCall(call, 1);
            call.into()
        }
    }
    /// Build an instruction that converts `val` to a floating point `dest`.
    pub fn build_fp_to_si(&self, val: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildFPToSI(self.into(), val.into(), dest.into(), NULL_NAME.as_ptr()).into()
        }
    }
    /// Build an instruction that converts `val` to an integer `dest`.
    pub fn build_si_to_fp(&self, val: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildSIToFP(self.into(), val.into(), dest.into(), NULL_NAME.as_ptr()).into()
        }
    }
    /// Build an instruction that yields to `true_val` if `cond` is equal to `1`, and `false_val` otherwise.
    pub fn build_select(&self, cond: &Value, true_val: &Value, false_val: &Value) -> &Value {
        unsafe {
            core::LLVMBuildSelect(
                self.into(),
                cond.into(),
                true_val.into(),
                false_val.into(),
                NULL_NAME.as_ptr(),
            ).into()
        }
    }
    /// Build an instruction that casts a value into a certain type.
    pub fn build_bit_cast(&self, value: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildBitCast(self.into(), value.into(), dest.into(), NULL_NAME.as_ptr())
                .into()
        }
    }
    /// Build an instruction that casts an integer to a pointer.
    pub fn build_int_to_ptr(&self, val: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildIntToPtr(self.into(), val.into(), dest.into(), NULL_NAME.as_ptr()).into()
        }
    }
    /// Build an instruction that casts a pointer to an integer.
    pub fn build_ptr_to_int(&self, val: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildPtrToInt(self.into(), val.into(), dest.into(), NULL_NAME.as_ptr()).into()
        }
    }
    /// Build an instruction that zero extends its operand to the type `dest`.
    pub fn build_zext(&self, value: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildZExtOrBitCast(self.into(), value.into(), dest.into(), NULL_NAME.as_ptr())
                .into()
        }
    }
    /// Build an instruction that truncates the high-order bits of value to fit into a certain type.
    pub fn build_trunc(&self, value: &Value, dest: &Type) -> &Value {
        unsafe {
            core::LLVMBuildTrunc(self.into(), value.into(), dest.into(), NULL_NAME.as_ptr()).into()
        }
    }
    /// Build an instruction that inserts a value into an aggregate data value.
    pub fn build_insert_value(&self, agg: &Value, elem: &Value, index: usize) -> &Value {
        unsafe {
            core::LLVMBuildInsertValue(
                self.into(),
                agg.into(),
                elem.into(),
                index as c_uint,
                NULL_NAME.as_ptr(),
            ).into()
        }
    }
    /// Build an instruction that computes the address of a subelement of an aggregate data structure.
    ///
    /// Basically type-safe pointer arithmetic.
    pub fn build_gep(&self, pointer: &Value, indices: &[&Value]) -> &Value {
        unsafe {
            core::LLVMBuildInBoundsGEP(
                self.into(),
                pointer.into(),
                indices.as_ptr() as *mut LLVMValueRef,
                indices.len() as c_uint,
                NULL_NAME.as_ptr(),
            ).into()
        }
    }
    /// Build an instruction that runs whichever block matches the value, or `default` if none of them matched it.
    pub fn build_switch(
        &self,
        value: &Value,
        default: &BasicBlock,
        cases: &[(&Value, &BasicBlock)],
    ) -> &Value {
        unsafe {
            let switch = core::LLVMBuildSwitch(
                self.into(),
                value.into(),
                default.into(),
                cases.len() as c_uint,
            );
            for case in cases {
                core::LLVMAddCase(switch, case.0.into(), case.1.into());
            }
            switch.into()
        }
    }
    /// Build a phi node which is used together with branching to select a value depending on the predecessor of the current block
    pub fn build_phi<'ctx>(
        &self,
        ty: &'ctx Type,
        entries: &[(&'ctx Value, &'ctx BasicBlock)],
    ) -> &'ctx Value {
        let phi_node = unsafe { core::LLVMBuildPhi(self.into(), ty.into(), NULL_NAME.as_ptr()) };

        for &(val, preds) in entries {
            unsafe { core::LLVMAddIncoming(phi_node, &mut val.into(), &mut preds.into(), 1) }
        }
        phi_node.into()
    }

    un_op!{build_load, LLVMBuildLoad}
    un_op!{build_neg, LLVMBuildNeg}
    un_op!{build_not, LLVMBuildNot}
    bin_op!{build_add, LLVMBuildAdd, LLVMBuildFAdd}
    bin_op!{build_sub, LLVMBuildSub, LLVMBuildFSub}
    bin_op!{build_mul, LLVMBuildMul, LLVMBuildFMul}
    bin_op!{build_div, LLVMBuildSDiv, LLVMBuildFDiv}
    bin_op!{build_shl, LLVMBuildShl}
    bin_op!{build_ashr, LLVMBuildAShr}
    bin_op!{build_and, LLVMBuildAnd}
    bin_op!{build_or, LLVMBuildOr}
    bin_op!{build_xor, LLVMBuildXor}
    bin_op!{build_rem, LLVMBuildSRem,LLVMBuildFRem}
    /// Build an instruction to compare two values with the predicate given.
    pub fn build_cmp(&self, a: &Value, b: &Value, pred: Predicate) -> &Value {
        let (at, bt) = (a.get_type(), b.get_type());
        assert_eq!(at, bt);
        if at.is_integer() {
            let pred = match pred {
                Predicate::Equal => LLVMIntPredicate::LLVMIntEQ,
                Predicate::NotEqual => LLVMIntPredicate::LLVMIntNE,
                Predicate::GreaterThan => LLVMIntPredicate::LLVMIntSGT,
                Predicate::GreaterThanOrEqual => LLVMIntPredicate::LLVMIntSGE,
                Predicate::LessThan => LLVMIntPredicate::LLVMIntSLT,
                Predicate::LessThanOrEqual => LLVMIntPredicate::LLVMIntSLE,
            };
            unsafe {
                core::LLVMBuildICmp(self.into(), pred, a.into(), b.into(), NULL_NAME.as_ptr())
            }.into()
        } else if at.is_float() {
            let pred = match pred {
                Predicate::Equal => LLVMRealPredicate::LLVMRealOEQ,
                Predicate::NotEqual => LLVMRealPredicate::LLVMRealONE,
                Predicate::GreaterThan => LLVMRealPredicate::LLVMRealOGT,
                Predicate::GreaterThanOrEqual => LLVMRealPredicate::LLVMRealOGE,
                Predicate::LessThan => LLVMRealPredicate::LLVMRealOLT,
                Predicate::LessThanOrEqual => LLVMRealPredicate::LLVMRealOLE,
            };
            unsafe {
                core::LLVMBuildFCmp(self.into(), pred, a.into(), b.into(), NULL_NAME.as_ptr())
            }.into()
        } else {
            panic!("expected numbers, got {:?}", at)
        }
    }
}