llvm-wrap 0.2.5

A safer wrapper for the LLVM C API bindings in Rust, based on llvm-sys
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! A wrapper around a `LLVMBuilderRef` for a specific context

use super::*;
use super::types::*;
use super::c_api::*;

/// A wrapper around a `LLVMBuilderRef` for a specific context
pub struct Builder {
    pub(crate) builder: Option<LLVMBuilderRef>,
}

impl Builder {
    /// Build a stack allocation
    pub fn build_alloca(&self, ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildAlloca(self.builder.unwrap(), ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a heap allocation
    pub fn build_malloc(&self, ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildMalloc(self.builder.unwrap(), ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a stack allocation for an array
    pub fn build_array_alloca(&self, ty: Type, count: u32) -> Value {
        Value {
            value: unsafe {
                LLVMBuildArrayAlloca(self.builder.unwrap(), ty.ty, ty_i32().const_int(count as u64).value, into_c("").as_ptr())
            }
        }
    }

    /// Build a heap allocation for an array
    pub fn build_array_malloc(&self, ty: Type, count: u32) -> Value {
        Value {
            value: unsafe {
                LLVMBuildArrayMalloc(self.builder.unwrap(), ty.ty, ty_i32().const_int(count as u64).value, into_c("").as_ptr())
            }
        }
    }

    /// Build a heap free
    pub fn build_free(&self, ptr: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFree(self.builder.unwrap(), ptr.value)
            }
        }
    }

    /// Build a call to a function
    pub fn build_call(&self, func: Value, args: Vec<Value>) -> Value {
        Value {
            value: unsafe {
                LLVMBuildCall(self.builder.unwrap(), func.value, val_vec(&args).as_mut_ptr(), args.len() as u32, into_c("").as_ptr())
            }
        }
    }

    /// Build a struct initialization for the given type and elements
    pub fn build_struct_init(&self, ty: Type, elements: Vec<Value>) -> Value {
        let mut init_elems = Vec::new();
        let mut append_elems = Vec::new();
        for (index, element) in elements.into_iter().enumerate() {
            if element.is_constant() {
                init_elems.push(element);
            } else {
                init_elems.push(element.ty().undef());
                append_elems.push((index, element));
            }
        }
        let mut agg = ty.const_struct(init_elems);
        for (index, element) in append_elems.into_iter() {
            agg = self.build_insert_value(agg, element, index as u32).name(format!("insert_{}", index));
        }
        agg
    }

    /// Build a struct initialization that stores it into memory with `alloca`
    pub fn build_struct_alloca_init(&self, ty: Type, elements: Vec<Value>) -> Value {
        let ptr = self.build_alloca(ty);
        for (index, element) in elements.into_iter().enumerate() {
            let elem_ptr = self.build_struct_gep(ptr, index as u32).name(format!("init_{}", index));
            self.build_store(element, elem_ptr);
        }
        ptr
    }

    /// Build a struct initialization that stores it into memory with `malloc`
    pub fn build_struct_malloc_init(&self, ty: Type, elements: Vec<Value>) -> Value {
        let ptr = self.build_malloc(ty);
        for (index, element) in elements.into_iter().enumerate() {
            let elem_ptr = self.build_struct_gep(ptr, index as u32).name(format!("init_{}", index));
            self.build_store(element, elem_ptr);
        }
        ptr
    }

    /// Build an insert value instruction
    pub fn build_insert_value(&self, agg: Value, elt: Value, index: u32) -> Value {
        Value {
            value: unsafe {
                LLVMBuildInsertValue(self.builder.unwrap(), agg.value, elt.value, index, into_c("").as_ptr())
            }
        }
    }

    /// Build an extract value instruction
    pub fn build_extract_value(&self, agg: Value, index: u32) -> Value {
        Value {
            value: unsafe {
                LLVMBuildExtractValue(self.builder.unwrap(), agg.value, index, into_c("").as_ptr())
            }
        }
    }

    /// Build a get element pointer instruction
    pub fn build_gep(&self, ptr: Value, indices: Vec<Value>) -> Value {
        Value {
            value: unsafe {
                LLVMBuildGEP(self.builder.unwrap(), ptr.value,
                             val_vec(&indices).as_mut_ptr(), indices.len() as u32, into_c("").as_ptr())
            }
        }
    }

    /// Build a struct get element pointer instruction
    pub fn build_struct_gep(&self, ptr: Value, index: u32) -> Value {
        Value {
            value: unsafe {
                LLVMBuildStructGEP(self.builder.unwrap(), ptr.value, index, into_c("").as_ptr())
            }
        }
    }

    /// Build an inbounds get element pointer instruction
    pub fn build_inbounds_gep(&self, ptr: Value, indices: Vec<Value>) -> Value {
        Value {
            value: unsafe {
                LLVMBuildGEP(self.builder.unwrap(), ptr.value,
                             val_vec(&indices).as_mut_ptr(), indices.len() as u32, into_c("").as_ptr())
            }
        }
    }

    /// Build a global string with the given value
    pub fn build_global_string<S>(&self, string: S) -> Value where S: AsRef<str> {
        Value {
            value: unsafe {
                LLVMBuildGlobalString(self.builder.unwrap(), into_c(string).as_ptr(), into_c("").as_ptr())
            }
        }
    }

    /// Build a global string pointer with the given value
    pub fn build_global_string_ptr<S>(&self, string: S) -> Value where S: AsRef<str> {
        Value {
            value: unsafe {
                LLVMBuildGlobalStringPtr(self.builder.unwrap(), into_c(string).as_ptr(), into_c("").as_ptr())
            }
        }
    }

    /// Build a store instruction
    pub fn build_store(&self, val: Value, ptr: Value) {
        unsafe {
            LLVMBuildStore(self.builder.unwrap(), val.value, ptr.value);
        }
    }

    /// Build a load instruction
    pub fn build_load(&self, ptr: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildLoad(self.builder.unwrap(), ptr.value, into_c("").as_ptr())
            }
        }
    }

    /// Alloca some memory for an array and then store values in it
    pub fn build_array_alloca_store(&self, ty: Type, elements: Vec<Value>) -> Value {
        let mut agg = ty_array(ty, elements.len() as u32).undef();
        for (i, element) in elements.into_iter().enumerate() {
            agg = self.build_insert_value(agg, element, i as u32).name(format!("insert_elem_{}", i));
        }
        self.build_pointer_cast(self.build_alloca_store(agg), ty.pointer())
    }

    /// Malloc some memory for an array and then store values in it
    pub fn build_array_malloc_store(&self, ty: Type, elements: Vec<Value>) -> Value {
        let mut agg = ty_array(ty, elements.len() as u32).undef();
        for (i, element) in elements.into_iter().enumerate() {
            agg = self.build_insert_value(agg, element, i as u32).name(format!("insert_elem_{}", i));
        }
        self.build_pointer_cast(self.build_malloc_store(agg), ty.pointer())
    }

    /// Alloca some memory and then store a value in it
    pub fn build_alloca_store(&self, val: Value) -> Value {
        let ptr = self.build_alloca(val.ty());
        self.build_store(val, ptr);
        ptr
    }

    /// Malloc some memory and then store a value in it
    pub fn build_malloc_store(&self, val: Value) -> Value {
        let ptr = self.build_malloc(val.ty());
        self.build_store(val, ptr);
        ptr
    }

    /// Load a value and then free the memory
    pub fn build_load_free(&self, ptr: Value) -> Value {
        let val = self.build_load(ptr);
        self.build_free(ptr);
        val
    }

    /// Build a cast from integer to pointer
    pub fn build_int_to_ptr(&self, val: Value, ptr_ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildIntToPtr(self.builder.unwrap(), val.value, ptr_ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a cast from pointer to integer
    pub fn build_ptr_to_int(&self, ptr: Value, val_ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildPtrToInt(self.builder.unwrap(), ptr.value, val_ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a pointer cast
    pub fn build_pointer_cast(&self, ptr: Value, ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildPointerCast(self.builder.unwrap(), ptr.value, ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build an integer cast
    pub fn build_int_cast(&self, val: Value, ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildIntCast(self.builder.unwrap(), val.value, ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a bit cast
    pub fn build_bit_cast(&self, val: Value, ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildBitCast(self.builder.unwrap(), val.value, ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a floating point cast
    pub fn build_float_cast(&self, val: Value, ty: Type) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFPCast(self.builder.unwrap(), val.value, ty.ty, into_c("").as_ptr())
            }
        }
    }

    /// Build a `ret void` statement
    pub fn build_ret_void(&self) -> Value {
        Value {
            value: unsafe {
                LLVMBuildRetVoid(self.builder.unwrap())
            }
        }
    }

    /// Build a `ret` statement
    pub fn build_ret(&self, val: Value) -> Value where {
        Value {
            value: unsafe {
                LLVMBuildRet(self.builder.unwrap(), val.value)
            }
        }
    }

    /// Build an unreachable instruction
    pub fn build_unreachable(&self) {
        unsafe {
            LLVMBuildUnreachable(self.builder.unwrap());
        }
    }

    /// Build a branch instruction to the given block
    pub fn build_br(&self, block: BasicBlock) {
        unsafe {
            LLVMBuildBr(self.builder.unwrap(), block.basic_block);
        }
    }

    /// Build an if statement that branches to the given blocks
    pub fn build_if(&self, condition: Value, then_block: BasicBlock, else_block: BasicBlock) {
        unsafe {
            LLVMBuildCondBr(self.builder.unwrap(), condition.value, then_block.basic_block, else_block.basic_block);
        }
    }

    /// Build a switch statement that branches to the given blocks
    pub fn build_switch(&self, val: Value, cases: Vec<(Value, BasicBlock)>, default: BasicBlock) {
        unsafe {
            let switch = LLVMBuildSwitch(self.builder.unwrap(), val.value, default.basic_block, cases.len() as u32);
            for (val, bb) in cases {
                LLVMAddCase(switch, val.value, bb.basic_block);
            }
        }
    }

    /// Build a phi instruction that takes ceratin values from certain blocks
    pub fn build_phi(&self, incoming: Vec<(Value, BasicBlock)>) -> Value {
        Value {
            value: unsafe {
                if incoming.is_empty() {
                    panic!("phi node must have an incoming block list");
                } else {
                    let phi = LLVMBuildPhi(
                        self.builder.unwrap(),
                        incoming[0].0.ty().ty,
                        into_c("").as_ptr()
                    );
                    let len = incoming.len();
                    let mut values = Vec::new();
                    let mut blocks = Vec::new();
                    for (val, block) in incoming {
                        values.push(val.value);
                        blocks.push(block.basic_block);
                    }
                    LLVMAddIncoming(phi, values.as_mut_ptr(), blocks.as_mut_ptr(), len as u32);
                    phi
                }
            }
        }
    }

    /// Position the builder at a given value in a basic block
    pub fn position_in_block(&self, bb: BasicBlock, val: Value) {
        unsafe {
            LLVMPositionBuilder(self.builder.unwrap(), bb.basic_block, val.value);
        }
    }

    /// Position the builder at the end of the basic block
    pub fn position_at_end(&self, bb: BasicBlock) {
        unsafe {
            LLVMPositionBuilderAtEnd(self.builder.unwrap(), bb.basic_block);
        }
    }

    /// Position the builder before a value
    pub fn position_before(&self, val: Value) {
        unsafe {
            LLVMPositionBuilderBefore(self.builder.unwrap(), val.value);
        }
    }

    /// Returns the internal builder reference
    pub fn inner(&self) -> LLVMBuilderRef {
        self.builder.unwrap()
    }

    /// Destroys the wrapper, returning the internal builder reference
    pub unsafe fn into_inner(mut self) -> LLVMBuilderRef {
        self.builder.take().unwrap()
    }

    /// Builds a null check
    pub fn build_is_null(&self, val: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildIsNull(self.builder.unwrap(), val.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a null check
    pub fn build_is_not_null(&self, val: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildIsNotNull(self.builder.unwrap(), val.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `add` instruction
    pub fn build_int_add(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildAdd(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `sub` instruction
    pub fn build_int_sub(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildSub(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `mul` instruction
    pub fn build_int_mul(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildMul(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `udiv` instruction
    pub fn build_int_udiv(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildUDiv(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `sdiv` instruction
    pub fn build_int_sdiv(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildSDiv(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `urem` instruction
    pub fn build_int_urem(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildSRem(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `srem` instruction
    pub fn build_int_srem(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildSRem(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a `shl` instruction
    pub fn build_shl(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildShl(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a `lshr` instruction
    pub fn build_lshr(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildLShr(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an `ashr` instruction
    pub fn build_ashr(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildAShr(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an `and` instruction
    pub fn build_and(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildAnd(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an `or` instruction
    pub fn build_or(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildOr(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an `xor` instruction
    pub fn build_xor(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildXor(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `eq` check
    pub fn build_int_eq(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntEQ, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `ne` check
    pub fn build_int_ne(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntNE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `ule` check
    pub fn build_int_ule(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntULE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `ult` check
    pub fn build_int_ult(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntULT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `uge` check
    pub fn build_int_uge(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntUGE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `ugt` check
    pub fn build_int_ugt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntUGT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `sle` check
    pub fn build_int_sle(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntSLE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `slt` check
    pub fn build_int_slt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntSLT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `sge` check
    pub fn build_int_sge(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntSGE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an integer `sgt` check
    pub fn build_int_sgt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildICmp(self.builder.unwrap(), LLVMIntPredicate::LLVMIntSGT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `add` instruction
    pub fn build_float_add(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFAdd(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `sub` instruction
    pub fn build_float_sub(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFSub(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `mul` instruction
    pub fn build_float_mul(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFMul(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `div` instruction
    pub fn build_float_div(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFDiv(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `rem` instruction
    pub fn build_float_rem(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFRem(self.builder.unwrap(), a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `eq` check
    pub fn build_float_eq(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealUEQ, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `ne` check
    pub fn build_float_ne(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealUNE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `le` check
    pub fn build_float_le(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealULE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `lt` check
    pub fn build_float_lt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealULT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `ge` check
    pub fn build_float_ge(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealUGE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a float `gt` check
    pub fn build_float_gt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealUGT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an ordered float `eq` check
    pub fn build_float_ord_eq(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealOEQ, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an ordered float `ne` check
    pub fn build_float_ord_ne(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealONE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an ordered float `le` check
    pub fn build_float_ord_le(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealOLE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an ordered float `lt` check
    pub fn build_float_ord_lt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealOLT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an ordered float `ge` check
    pub fn build_float_ord_ge(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealOGE, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds an ordered float `gt` check
    pub fn build_float_ord_gt(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealOGT, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a check for an ordered float
    pub fn build_float_is_ord(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealORD, a.value, b.value, into_c("").as_ptr())
            }
        }
    }

    /// Builds a check for an unordered float
    pub fn build_float_non_ord(&self, a: Value, b: Value) -> Value {
        Value {
            value: unsafe {
                LLVMBuildFCmp(self.builder.unwrap(), LLVMRealPredicate::LLVMRealUNO, a.value, b.value, into_c("").as_ptr())
            }
        }
    }
}

impl Deref for Builder {
    type Target = LLVMBuilderRef;

    fn deref(&self) -> &LLVMBuilderRef {
        self.builder.as_ref().unwrap()
    }
}

impl Drop for Builder {
    fn drop(&mut self) {
        if let Some(builder) = self.builder {
            unsafe {
                LLVMDisposeBuilder(builder);
            }
        }
    }
}

impl Debug for Builder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Builder")
    }
}