1
1
extern crate inkwell;
2
2
3
3
use self :: inkwell:: context:: Context ;
4
+ use self :: inkwell:: module:: Module ;
4
5
use self :: inkwell:: targets:: {
5
6
CodeModel , FileType , InitializationConfig , RelocMode , Target , TargetMachine ,
6
7
} ;
8
+ use self :: inkwell:: types:: IntType ;
7
9
use self :: inkwell:: values:: BasicValue ;
8
10
use self :: inkwell:: OptimizationLevel ;
9
11
12
+ #[ llvm_versions( 4.0 ..=latest) ]
13
+ fn get_host_cpu_name ( ) -> String {
14
+ TargetMachine :: get_host_cpu_name ( ) . to_string ( )
15
+ }
16
+ #[ llvm_versions( 4.0 ..=latest) ]
17
+ fn get_host_cpu_features ( ) -> String {
18
+ TargetMachine :: get_host_cpu_features ( ) . to_string ( )
19
+ }
20
+ #[ llvm_versions( 4.0 ..=latest) ]
21
+ fn ptr_sized_int_type < ' ctx > (
22
+ target_machine : & TargetMachine ,
23
+ context : & ' ctx Context ,
24
+ ) -> IntType < ' ctx > {
25
+ let target_data = target_machine. get_target_data ( ) ;
26
+ target_data. ptr_sized_int_type_in_context ( & context, None )
27
+ }
28
+ #[ llvm_versions( 4.0 ..=latest) ]
29
+ fn apply_target_to_module < ' ctx > ( target_machine : & TargetMachine , module : & Module ) {
30
+ module. set_triple ( & target_machine. get_triple ( ) ) ;
31
+ module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
32
+ }
33
+
34
+ #[ llvm_versions( 3.6 ..4.0 ) ]
35
+ fn get_host_cpu_name ( ) -> String {
36
+ ""
37
+ }
38
+ #[ llvm_versions( 3.6 ..4.0 ) ]
39
+ fn get_host_cpu_features ( ) -> String {
40
+ ""
41
+ }
42
+ #[ llvm_versions( 3.6 ..4.0 ) ]
43
+ fn ptr_sized_int_type < ' ctx > (
44
+ _target_machine : & TargetMachine ,
45
+ context : & ' ctx Context ,
46
+ ) -> IntType < ' ctx > {
47
+ context. i64_type ( )
48
+ }
49
+ #[ llvm_versions( 3.6 ..4.0 ) ]
50
+ fn apply_target_to_module ( target_machine : & TargetMachine , module : & Module ) {
51
+ module. set_triple ( & target_machine. get_triple ( ) ) ;
52
+ }
53
+
10
54
fn get_native_target_machine ( ) -> TargetMachine {
11
55
Target :: initialize_native ( & InitializationConfig :: default ( ) )
12
56
. expect ( "Failed to initialize native target" ) ;
@@ -15,11 +59,11 @@ fn get_native_target_machine() -> TargetMachine {
15
59
target
16
60
. create_target_machine (
17
61
& target_triple,
18
- & TargetMachine :: get_host_cpu_name ( ) . to_string ( ) ,
19
- & TargetMachine :: get_host_cpu_features ( ) . to_string ( ) ,
62
+ & get_host_cpu_name ( ) ,
63
+ & get_host_cpu_features ( ) ,
20
64
OptimizationLevel :: None ,
21
- RelocMode :: Static ,
22
- CodeModel :: Small ,
65
+ RelocMode :: Default ,
66
+ CodeModel :: Default ,
23
67
)
24
68
. unwrap ( )
25
69
}
@@ -43,8 +87,7 @@ fn test_section_iterator() {
43
87
gv_c. set_initializer ( & context. i32_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
44
88
gv_c. set_section ( "C" ) ;
45
89
46
- module. set_triple ( & target_machine. get_triple ( ) ) ;
47
- module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
90
+ apply_target_to_module ( & target_machine, & module) ;
48
91
49
92
let memory_buffer = target_machine
50
93
. write_to_memory_buffer ( & mut module, FileType :: Object )
@@ -96,8 +139,7 @@ fn test_symbol_iterator() {
96
139
module
97
140
. add_global ( context. i32_type ( ) , None , "c" )
98
141
. set_initializer ( & context. i32_type ( ) . const_zero ( ) . as_basic_value_enum ( ) ) ;
99
- module. set_triple ( & target_machine. get_triple ( ) ) ;
100
- module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
142
+ apply_target_to_module ( & target_machine, & module) ;
101
143
102
144
let memory_buffer = target_machine
103
145
. write_to_memory_buffer ( & mut module, FileType :: Object )
@@ -139,8 +181,7 @@ fn test_reloc_iterator() {
139
181
let target_machine = get_native_target_machine ( ) ;
140
182
141
183
let context = Context :: create ( ) ;
142
- let target_data = target_machine. get_target_data ( ) ;
143
- let intptr_t = target_data. ptr_sized_int_type_in_context ( & context, None ) ;
184
+ let intptr_t = ptr_sized_int_type ( & target_machine, & context) ;
144
185
145
186
let mut module = context. create_module ( "test_reloc_iterator" ) ;
146
187
let x_ptr = module
@@ -153,8 +194,7 @@ fn test_reloc_iterator() {
153
194
. add_global ( intptr_t, None , "a" )
154
195
. set_initializer ( & x_plus_4) ;
155
196
156
- module. set_triple ( & target_machine. get_triple ( ) ) ;
157
- module. set_data_layout ( & target_machine. get_target_data ( ) . get_data_layout ( ) ) ;
197
+ apply_target_to_module ( & target_machine, & module) ;
158
198
159
199
let memory_buffer = target_machine
160
200
. write_to_memory_buffer ( & mut module, FileType :: Object )
0 commit comments