Skip to content

Commit d93253d

Browse files
authored
Merge pull request rust-lang#25 from 6A/master
AddressSpace everywhere (fixes rust-lang#24)
2 parents b75c231 + dabea27 commit d93253d

12 files changed

+72
-65
lines changed

src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ pub fn shutdown_llvm() {
6161
}
6262
}
6363

64+
/// Defines the address space in which a global will be inserted.
65+
///
66+
/// # Remarks
67+
/// See also: http://llvm.org/doxygen/NVPTXBaseInfo_8h_source.html
68+
#[repr(u32)]
69+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
70+
pub enum AddressSpace {
71+
Generic = 0,
72+
Global = 1,
73+
Shared = 3,
74+
Const = 4,
75+
Local = 5
76+
}
77+
78+
impl From<u32> for AddressSpace {
79+
fn from(val: u32) -> Self {
80+
match val {
81+
0 => AddressSpace::Generic,
82+
1 => AddressSpace::Global,
83+
2 => AddressSpace::Shared,
84+
3 => AddressSpace::Const,
85+
4 => AddressSpace::Local,
86+
_ => unreachable!("Invalid value for AddressSpace"),
87+
}
88+
}
89+
}
90+
6491
// REVIEW: Maybe this belongs in some sort of prelude?
6592
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6693
pub enum IntPredicate {

src/module.rs

+1-27
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::mem::{forget, uninitialized, zeroed};
1111
use std::path::Path;
1212
use std::slice::from_raw_parts;
1313

14-
use OptimizationLevel;
14+
use {AddressSpace, OptimizationLevel};
1515
use context::{Context, ContextRef};
1616
use data_layout::DataLayout;
1717
use execution_engine::ExecutionEngine;
@@ -41,32 +41,6 @@ pub enum Linkage {
4141
WeakODRLinkage,
4242
}
4343

44-
/// Defines the address space in which a global will be inserted.
45-
///
46-
/// # Remarks
47-
/// See also: http://llvm.org/doxygen/NVPTXBaseInfo_8h_source.html
48-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
49-
pub enum AddressSpace {
50-
Generic = 0,
51-
Global = 1,
52-
Shared = 3,
53-
Const = 4,
54-
Local = 5
55-
}
56-
57-
impl From<u32> for AddressSpace {
58-
fn from(val: u32) -> Self {
59-
match val {
60-
0 => AddressSpace::Generic,
61-
1 => AddressSpace::Global,
62-
2 => AddressSpace::Shared,
63-
3 => AddressSpace::Const,
64-
4 => AddressSpace::Local,
65-
_ => unreachable!("Invalid value for AddressSpace"),
66-
}
67-
}
68-
}
69-
7044
impl Linkage {
7145
pub(crate) fn new(linkage: LLVMLinkage) -> Self {
7246
match linkage {

src/types/array_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
33

44
use std::ffi::CStr;
55

6+
use AddressSpace;
67
use context::ContextRef;
78
use types::traits::AsTypeRef;
89
use types::{Type, BasicType, PointerType, FunctionType};
@@ -36,7 +37,7 @@ impl ArrayType {
3637
None
3738
}
3839

39-
pub fn ptr_type(&self, address_space: u32) -> PointerType {
40+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
4041
self.array_type.ptr_type(address_space)
4142
}
4243

src/types/float_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use llvm_sys::prelude::LLVMTypeRef;
44

55
use std::ffi::CStr;
66

7+
use AddressSpace;
78
use context::ContextRef;
89
use types::traits::AsTypeRef;
910
use types::{Type, PointerType, FunctionType, BasicType, ArrayType, VectorType};
@@ -77,7 +78,7 @@ impl FloatType {
7778
self.float_type.get_context()
7879
}
7980

80-
pub fn ptr_type(&self, address_space: u32) -> PointerType {
81+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
8182
self.float_type.ptr_type(address_space)
8283
}
8384

src/types/int_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use llvm_sys::prelude::LLVMTypeRef;
44

55
use std::ffi::CStr;
66

7+
use AddressSpace;
78
use context::ContextRef;
89
use types::traits::AsTypeRef;
910
use types::{Type, ArrayType, BasicType, VectorType, PointerType, FunctionType};
@@ -296,7 +297,7 @@ impl IntType {
296297
self.int_type.size_of()
297298
}
298299

299-
pub fn ptr_type(&self, address_space: u32) -> PointerType {
300+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
300301
self.int_type.ptr_type(address_space)
301302
}
302303

src/types/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
2828
use std::ffi::CStr;
2929
use std::fmt;
3030

31+
use AddressSpace;
3132
use context::{Context, ContextRef};
3233
use values::{IntValue, PointerValue};
3334

@@ -62,9 +63,9 @@ impl Type {
6263
PointerValue::new(ptr_type)
6364
}
6465

65-
fn ptr_type(&self, address_space: u32) -> PointerType {
66+
fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
6667
let ptr_type = unsafe {
67-
LLVMPointerType(self.type_, address_space)
68+
LLVMPointerType(self.type_, address_space as u32)
6869
};
6970

7071
PointerType::new(ptr_type)

src/types/ptr_type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use llvm_sys::prelude::LLVMTypeRef;
33

44
use std::ffi::CStr;
55

6+
use AddressSpace;
67
use context::ContextRef;
7-
use module::AddressSpace;
88
use types::traits::AsTypeRef;
99
use types::{Type, BasicType, ArrayType, FunctionType, VectorType};
1010
use values::{PointerValue, IntValue};
@@ -31,7 +31,7 @@ impl PointerType {
3131
self.ptr_type.size_of()
3232
}
3333

34-
pub fn ptr_type(&self, address_space: u32) -> PointerType {
34+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
3535
self.ptr_type.ptr_type(address_space)
3636
}
3737

src/types/struct_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
44
use std::ffi::CStr;
55
use std::mem::forget;
66

7+
use AddressSpace;
78
use context::ContextRef;
89
use types::traits::AsTypeRef;
910
use types::{Type, BasicType, BasicTypeEnum, ArrayType, PointerType, FunctionType, VectorType};
@@ -115,7 +116,7 @@ impl StructType {
115116
Some(c_str)
116117
}
117118

118-
pub fn ptr_type(&self, address_space: u32) -> PointerType {
119+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
119120
self.struct_type.ptr_type(address_space)
120121
}
121122

src/types/void_type.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use llvm_sys::core::LLVMVoidType;
22
use llvm_sys::prelude::LLVMTypeRef;
33

4+
use AddressSpace;
45
use context::ContextRef;
56
use types::traits::AsTypeRef;
67
use types::{Type, BasicType, FunctionType, PointerType};
@@ -31,7 +32,7 @@ impl VoidType {
3132
self.void_type.get_context()
3233
}
3334

34-
pub fn ptr_type(&self, address_space: u32) -> PointerType {
35+
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
3536
self.void_type.ptr_type(address_space)
3637
}
3738

tests/test_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate inkwell;
22

3-
use self::inkwell::OptimizationLevel;
3+
use self::inkwell::{AddressSpace, OptimizationLevel};
44
use self::inkwell::context::Context;
55
use self::inkwell::builder::Builder;
66
use self::inkwell::targets::{InitializationConfig, Target};
@@ -56,7 +56,7 @@ fn test_null_checked_ptr_ops() {
5656
// }
5757

5858
let i8_type = context.i8_type();
59-
let i8_ptr_type = i8_type.ptr_type(0);
59+
let i8_ptr_type = i8_type.ptr_type(AddressSpace::Generic);
6060
let i64_type = context.i64_type();
6161
let fn_type = i8_type.fn_type(&[&i8_ptr_type], false);
6262
let neg_one = i8_type.const_all_ones();

tests/test_types.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extern crate inkwell;
22

33
use std::ffi::CString;
44

5+
use self::inkwell::AddressSpace;
56
use self::inkwell::context::Context;
67
use self::inkwell::types::{FloatType, IntType, StructType, VoidType};
78

@@ -156,26 +157,26 @@ fn test_sized_types() {
156157
assert!(!fn_type3.is_sized());
157158
assert!(!fn_type4.is_sized());
158159

159-
assert!(void_type.ptr_type(0).is_sized());
160-
assert!(bool_type.ptr_type(0).is_sized());
161-
assert!(i8_type.ptr_type(0).is_sized());
162-
assert!(i16_type.ptr_type(0).is_sized());
163-
assert!(i32_type.ptr_type(0).is_sized());
164-
assert!(i64_type.ptr_type(0).is_sized());
165-
assert!(i128_type.ptr_type(0).is_sized());
166-
assert!(f16_type.ptr_type(0).is_sized());
167-
assert!(f32_type.ptr_type(0).is_sized());
168-
assert!(f64_type.ptr_type(0).is_sized());
169-
assert!(f128_type.ptr_type(0).is_sized());
170-
assert!(ppc_f128_type.ptr_type(0).is_sized());
171-
assert!(struct_type.ptr_type(0).is_sized());
172-
assert!(struct_type2.ptr_type(0).is_sized());
173-
assert!(struct_type3.ptr_type(0).is_sized());
174-
assert!(struct_type4.ptr_type(0).is_sized());
175-
assert!(opaque_struct_type.ptr_type(0).is_sized());
160+
assert!(void_type.ptr_type(AddressSpace::Generic).is_sized());
161+
assert!(bool_type.ptr_type(AddressSpace::Generic).is_sized());
162+
assert!(i8_type.ptr_type(AddressSpace::Generic).is_sized());
163+
assert!(i16_type.ptr_type(AddressSpace::Generic).is_sized());
164+
assert!(i32_type.ptr_type(AddressSpace::Generic).is_sized());
165+
assert!(i64_type.ptr_type(AddressSpace::Generic).is_sized());
166+
assert!(i128_type.ptr_type(AddressSpace::Generic).is_sized());
167+
assert!(f16_type.ptr_type(AddressSpace::Generic).is_sized());
168+
assert!(f32_type.ptr_type(AddressSpace::Generic).is_sized());
169+
assert!(f64_type.ptr_type(AddressSpace::Generic).is_sized());
170+
assert!(f128_type.ptr_type(AddressSpace::Generic).is_sized());
171+
assert!(ppc_f128_type.ptr_type(AddressSpace::Generic).is_sized());
172+
assert!(struct_type.ptr_type(AddressSpace::Generic).is_sized());
173+
assert!(struct_type2.ptr_type(AddressSpace::Generic).is_sized());
174+
assert!(struct_type3.ptr_type(AddressSpace::Generic).is_sized());
175+
assert!(struct_type4.ptr_type(AddressSpace::Generic).is_sized());
176+
assert!(opaque_struct_type.ptr_type(AddressSpace::Generic).is_sized());
176177

177178
// REVIEW: You can't have array of void right?
178-
assert!(void_type.ptr_type(0).array_type(42).is_sized());
179+
assert!(void_type.ptr_type(AddressSpace::Generic).array_type(42).is_sized());
179180
assert!(bool_type.array_type(42).is_sized());
180181
assert!(i8_type.array_type(42).is_sized());
181182
assert!(i16_type.array_type(42).is_sized());
@@ -194,7 +195,7 @@ fn test_sized_types() {
194195
assert!(!opaque_struct_type.array_type(0).is_sized());
195196

196197
// REVIEW: You can't have vec of void right?
197-
assert!(void_type.ptr_type(0).vec_type(42).is_sized());
198+
assert!(void_type.ptr_type(AddressSpace::Generic).vec_type(42).is_sized());
198199
assert!(bool_type.vec_type(42).is_sized());
199200
assert!(i8_type.vec_type(42).is_sized());
200201
assert!(i16_type.vec_type(42).is_sized());
@@ -228,7 +229,7 @@ fn test_const_null() {
228229
let f64_type = context.f64_type();
229230
let f128_type = context.f128_type();
230231
let struct_type = context.struct_type(&[&i8_type, &f128_type], false);
231-
let ptr_type = f64_type.ptr_type(0);
232+
let ptr_type = f64_type.ptr_type(AddressSpace::Generic);
232233
let vec_type = f64_type.vec_type(42);
233234
let array_type = f64_type.array_type(42);
234235

tests/test_values.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
extern crate inkwell;
22

3-
use self::inkwell::{DLLStorageClass, FloatPredicate, GlobalVisibility, ThreadLocalMode};
3+
use self::inkwell::{DLLStorageClass, FloatPredicate, GlobalVisibility, ThreadLocalMode, AddressSpace};
44
use self::inkwell::context::Context;
5-
use self::inkwell::module::AddressSpace::*;
65
use self::inkwell::module::Linkage::*;
76
use self::inkwell::types::{StructType, VectorType};
87
use self::inkwell::values::InstructionOpcode::*;
@@ -32,7 +31,7 @@ fn test_instructions() {
3231
let void_type = context.void_type();
3332
let i64_type = context.i64_type();
3433
let f32_type = context.f32_type();
35-
let f32_ptr_type = f32_type.ptr_type(0);
34+
let f32_ptr_type = f32_type.ptr_type(AddressSpace::Generic);
3635
let fn_type = void_type.fn_type(&[&f32_ptr_type], false);
3736

3837
let function = module.add_function("free_f32", &fn_type, None);
@@ -170,7 +169,7 @@ fn test_set_get_name() {
170169
assert_eq!(ppc_f128_val.get_name(), &*CString::new("").unwrap());
171170

172171
let void_type = context.void_type();
173-
let ptr_type = bool_type.ptr_type(0);
172+
let ptr_type = bool_type.ptr_type(AddressSpace::Generic);
174173
let struct_type = context.struct_type(&[&bool_type], false);
175174
let vec_type = bool_type.vec_type(1);
176175

@@ -278,7 +277,7 @@ fn test_undef() {
278277
let f32_undef = f32_type.get_undef();
279278
let f64_undef = f64_type.get_undef();
280279
let f128_undef = f128_type.get_undef();
281-
let ptr_undef = bool_type.ptr_type(0).get_undef();
280+
let ptr_undef = bool_type.ptr_type(AddressSpace::Generic).get_undef();
282281
let array_undef = array_type.get_undef();
283282
let struct_undef = StructType::struct_type(&[&bool_type], false).get_undef();
284283
let vec_undef = bool_type.vec_type(1).get_undef();
@@ -448,7 +447,7 @@ fn test_metadata() {
448447
let f64_val = f64_type.const_float(0.0);
449448
let f128_val = f128_type.const_float(0.0);
450449
// let ppc_f128_val = ppc_f128_type.const_float(0.0);
451-
let ptr_val = bool_type.ptr_type(0).const_null();
450+
let ptr_val = bool_type.ptr_type(AddressSpace::Generic).const_null();
452451
let array_val = array_type.const_array(&[&f64_val]);
453452
let struct_val = context.const_struct(&[&i8_val, &f128_val], false);
454453
let vec_val = VectorType::const_vector(&[&i8_val]);
@@ -791,7 +790,7 @@ fn test_globals() {
791790

792791
assert!(global.get_thread_local_mode().is_none());
793792

794-
let global2 = module.add_global(&i8_type, Some(Const), "my_global2");
793+
let global2 = module.add_global(&i8_type, Some(AddressSpace::Const), "my_global2");
795794

796795
assert_eq!(global2.get_previous_global().unwrap(), global);
797796
assert_eq!(global.get_next_global().unwrap(), global2);

0 commit comments

Comments
 (0)