@@ -3,6 +3,7 @@ extern crate inkwell;
3
3
use self :: inkwell:: { AddressSpace , OptimizationLevel } ;
4
4
use self :: inkwell:: context:: Context ;
5
5
use self :: inkwell:: builder:: Builder ;
6
+ use self :: inkwell:: values:: BasicValue ;
6
7
7
8
// use std::ffi::CString;
8
9
use std:: ptr:: null;
@@ -652,3 +653,49 @@ fn test_insert_value() {
652
653
653
654
assert ! ( module. verify( ) . is_ok( ) ) ;
654
655
}
656
+
657
+ #[ test]
658
+ fn test_bitcast ( ) {
659
+ let context = Context :: create ( ) ;
660
+ let module = context. create_module ( "bc" ) ;
661
+ let void_type = context. void_type ( ) ;
662
+ let f32_type = context. f32_type ( ) ;
663
+ let i32_type = context. i32_type ( ) ;
664
+ let i64_type = context. i64_type ( ) ;
665
+ let i32_ptr_type = i32_type. ptr_type ( AddressSpace :: Generic ) ;
666
+ let i64_ptr_type = i64_type. ptr_type ( AddressSpace :: Generic ) ;
667
+ let i32_vec_type = i32_type. vec_type ( 2 ) ;
668
+ let arg_types = [
669
+ i32_type. into ( ) ,
670
+ f32_type. into ( ) ,
671
+ i32_vec_type. into ( ) ,
672
+ i32_ptr_type. into ( ) ,
673
+ ] ;
674
+ let fn_type = void_type. fn_type ( & arg_types, false ) ;
675
+ let fn_value = module. add_function ( "bc" , fn_type, None ) ;
676
+ let builder = context. create_builder ( ) ;
677
+ let entry = fn_value. append_basic_block ( "entry" ) ;
678
+ let i32_arg = fn_value. get_first_param ( ) . unwrap ( ) . into_int_value ( ) ;
679
+ let f32_arg = fn_value. get_nth_param ( 1 ) . unwrap ( ) . into_float_value ( ) ;
680
+ let i32_vec_arg = fn_value. get_nth_param ( 2 ) . unwrap ( ) . into_vector_value ( ) ;
681
+ let i32_ptr_arg = fn_value. get_nth_param ( 3 ) . unwrap ( ) . into_pointer_value ( ) ;
682
+
683
+ builder. position_at_end ( & entry) ;
684
+
685
+ let cast = builder. build_bitcast ( i32_arg, f32_type, "i32tof32" ) ;
686
+
687
+ builder. build_bitcast ( f32_arg, f32_type, "f32tof32" ) ;
688
+ builder. build_bitcast ( i32_vec_arg, i64_type, "2xi32toi64" ) ;
689
+ builder. build_bitcast ( i32_ptr_arg, i64_ptr_type, "i32*toi64*" ) ;
690
+
691
+ builder. build_return ( None ) ;
692
+
693
+ assert ! ( module. verify( ) . is_ok( ) , module. print_to_string( ) . to_string( ) ) ;
694
+
695
+ let first_iv = cast. as_instruction_value ( ) . unwrap ( ) ;
696
+
697
+ builder. position_before ( & first_iv) ;
698
+ builder. build_bitcast ( f32_arg, i64_type, "f32toi64" ) ;
699
+
700
+ assert ! ( module. verify( ) . is_err( ) ) ;
701
+ }
0 commit comments