Skip to content

Commit 3ca44a2

Browse files
committed
Added VecType docs
1 parent 82c5cb1 commit 3ca44a2

File tree

5 files changed

+222
-9
lines changed

5 files changed

+222
-9
lines changed

src/types/float_type.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! This module contains functions for working with `FloatType`s and generating types and values from them.
2-
31
use llvm_sys::core::{LLVMConstReal, LLVMConstNull, LLVMHalfType, LLVMFloatType, LLVMDoubleType, LLVMFP128Type, LLVMPPCFP128Type, LLVMConstRealOfStringAndSize, LLVMX86FP80Type, LLVMConstArray};
42
use llvm_sys::execution_engine::LLVMCreateGenericValueOfFloat;
53
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};
@@ -399,7 +397,7 @@ impl FloatType {
399397
self.float_type.print_to_stderr()
400398
}
401399

402-
/// Creates an undefined instance of an `FloatType`.
400+
/// Creates an undefined instance of a `FloatType`.
403401
///
404402
/// # Example
405403
/// ```no_run

src/types/int_type.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! This module contains functions for working with `IntType`s and generating values from them.
2-
31
use llvm_sys::core::{LLVMInt1Type, LLVMInt8Type, LLVMInt16Type, LLVMInt32Type, LLVMInt64Type, LLVMConstInt, LLVMConstNull, LLVMConstAllOnes, LLVMIntType, LLVMGetIntTypeWidth, LLVMConstIntOfStringAndSize, LLVMConstIntOfArbitraryPrecision, LLVMConstArray};
42
use llvm_sys::execution_engine::LLVMCreateGenericValueOfInt;
53
use llvm_sys::prelude::{LLVMTypeRef, LLVMValueRef};

src/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod int_type;
88
mod ptr_type;
99
mod struct_type;
1010
mod traits;
11+
#[deny(missing_docs)]
1112
mod vec_type;
1213
mod void_type;
1314

src/types/vec_type.rs

Lines changed: 219 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use support::LLVMString;
77
use types::{ArrayType, BasicTypeEnum, Type, traits::AsTypeRef, FunctionType, PointerType};
88
use values::{AsValueRef, ArrayValue, BasicValue, PointerValue, VectorValue, IntValue};
99

10-
// REVIEW: vec_type() is impl for IntType & FloatType. Need to
11-
// find out if it is valid for other types too. Maybe PointerType?
10+
/// A `VectorType` is the type of a multiple value SIMD constant or variable.
1211
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1312
pub struct VectorType {
1413
vec_type: Type,
@@ -24,13 +23,38 @@ impl VectorType {
2423
}
2524

2625
// REVIEW: Can be unsized if inner type is opaque struct
26+
/// Gets whether or not this `VectorType` is sized or not.
27+
///
28+
/// # Example
29+
///
30+
/// ```no_run
31+
/// use inkwell::context::Context;
32+
///
33+
/// let context = Context::create();
34+
/// let f32_type = context.f32_type();
35+
/// let f32_vec_type = f32_type.vec_type(40);
36+
///
37+
/// assert!(f32_vec_type.is_sized());
38+
/// ```
2739
pub fn is_sized(&self) -> bool {
2840
self.vec_type.is_sized()
2941
}
3042

3143
// TODO: impl only for VectorType<!StructType<Opaque>>
3244
// REVIEW: What about Opaque struct hiding in deeper levels?
3345
// like VectorType<ArrayType<StructType<Opaque>>>?
46+
/// Gets the size of this `VectorType`. Value may vary depending on the target architecture.
47+
///
48+
/// # Example
49+
///
50+
/// ```no_run
51+
/// use inkwell::context::Context;
52+
///
53+
/// let context = Context::create();
54+
/// let f32_type = context.f32_type();
55+
/// let f32_vec_type = f32_type.vec_type(3);
56+
/// let f32_vec_type_size = f32_vec_type.size_of();
57+
/// ```
3458
pub fn size_of(&self) -> Option<IntValue> {
3559
if self.is_sized() {
3660
return Some(self.vec_type.size_of())
@@ -39,10 +63,36 @@ impl VectorType {
3963
None
4064
}
4165

66+
/// Gets the aligment of this `VectorType`. Value may vary depending on the target architecture.
67+
///
68+
/// # Example
69+
///
70+
/// ```no_run
71+
/// use inkwell::context::Context;
72+
///
73+
/// let context = Context::create();
74+
/// let f32_type = context.f32_type();
75+
/// let f32_vec_type = f32_type.vec_type(7);
76+
/// let f32_type_alignment = f32_vec_type.get_alignment();
77+
/// ```
4278
pub fn get_alignment(&self) -> IntValue {
4379
self.vec_type.get_alignment()
4480
}
4581

82+
/// Gets the size of this `VectorType`.
83+
///
84+
/// # Example
85+
///
86+
/// ```no_run
87+
/// use inkwell::context::Context;
88+
///
89+
/// let context = Context::create();
90+
/// let f32_type = context.f32_type();
91+
/// let f32_vector_type = f32_type.vec_type(3);
92+
///
93+
/// assert_eq!(f32_vector_type.get_size(), 3);
94+
/// assert_eq!(f32_vector_type.get_element_type().into_float_type(), f32_type);
95+
/// ```
4696
pub fn get_size(&self) -> u32 {
4797
unsafe {
4898
LLVMGetVectorSize(self.as_type_ref())
@@ -57,6 +107,21 @@ impl VectorType {
57107
// REVIEW: Maybe we could make this use &self if the vector size
58108
// is stored as a const and the input values took a const size?
59109
// Something like: values: &[&V; self.size]. Doesn't sound possible though
110+
/// Creates a constant `VectorValue`.
111+
///
112+
/// # Example
113+
/// ```no_run
114+
/// use inkwell::context::Context;
115+
/// use inkwell::types::VectorType;
116+
///
117+
/// let context = Context::create();
118+
/// let f32_type = context.f32_type();
119+
/// let f32_val = f32_type.const_float(0.);
120+
/// let f32_val2 = f32_type.const_float(2.);
121+
/// let f32_vec_val = VectorType::const_vector(&[f32_val, f32_val2]);
122+
///
123+
/// assert!(f32_vec_val.is_constant_vector());
124+
/// ```
60125
pub fn const_vector<V: BasicValue>(values: &[V]) -> VectorValue {
61126
let mut values: Vec<LLVMValueRef> = values.iter()
62127
.map(|val| val.as_value_ref())
@@ -68,10 +133,44 @@ impl VectorType {
68133
VectorValue::new(vec_value)
69134
}
70135

136+
/// Creates a `PointerValue` representing a constant value of zero (null pointer) pointing to this `VectorType`.
137+
/// It will be automatically assigned this `FloatType`'s `Context`.
138+
///
139+
/// # Example
140+
/// ```
141+
/// use inkwell::context::Context;
142+
/// use inkwell::types::FloatType;
143+
///
144+
/// // Global Context
145+
/// let f32_type = FloatType::f32_type();
146+
/// let f32_vec_type = f32_type.vec_type(7);
147+
/// let f32_value = f32_vec_type.const_null_ptr();
148+
///
149+
/// // Custom Context
150+
/// let context = Context::create();
151+
/// let f32_type = context.f32_type();
152+
/// let f32_vec_type = f32_type.vec_type(7);
153+
/// let f32_value = f32_vec_type.const_null_ptr();
154+
/// ```
71155
pub fn const_null_ptr(&self) -> PointerValue {
72156
self.vec_type.const_null_ptr()
73157
}
74158

159+
/// Creates a constant null (zero) value of this `FloatType`.
160+
///
161+
/// # Example
162+
///
163+
/// ```no_run
164+
/// use inkwell::context::Context;
165+
///
166+
/// let context = Context::create();
167+
/// let f32_type = context.f32_type();
168+
/// let f32_vec_type = f32_type.vec_type(7);
169+
/// let f32_vec_zero = f32_vec_type.const_null();
170+
///
171+
/// assert!(f32_vec_zero.is_null());
172+
/// assert_eq!(f32_vec_zero.print_to_string().to_string(), "f32 0");
173+
/// ```
75174
pub fn const_null(&self) -> VectorValue {
76175
let null = unsafe {
77176
LLVMConstNull(self.as_type_ref())
@@ -80,41 +179,145 @@ impl VectorType {
80179
VectorValue::new(null)
81180
}
82181

182+
/// Prints the definition of a `VectorType` to a `LLVMString`.
83183
pub fn print_to_string(&self) -> LLVMString {
84184
self.vec_type.print_to_string()
85185
}
86186

87187
// See Type::print_to_stderr note on 5.0+ status
88-
#[cfg(not(any(feature = "llvm3-6", feature = "llvm5-0")))]
188+
/// Prints the definition of an `IntType` to stderr. Not available in newer LLVM versions.
189+
#[cfg(any(feature = "llvm3-7", feature = "llvm3-8", feature = "llvm3-9", feature = "llvm4-0"))]
89190
pub fn print_to_stderr(&self) {
90191
self.vec_type.print_to_stderr()
91192
}
92193

194+
/// Creates an undefined instance of a `VectorType`.
195+
///
196+
/// # Example
197+
/// ```no_run
198+
/// use inkwell::context::Context;
199+
/// use inkwell::AddressSpace;
200+
///
201+
/// let context = Context::create();
202+
/// let f32_type = context.f32_type();
203+
/// let f32_vec_type = f32_type.vec_type(3);
204+
/// let f32_vec_undef = f32_vec_type.get_undef();
205+
///
206+
/// assert!(f32_vec_undef.is_undef());
207+
/// ```
93208
pub fn get_undef(&self) -> VectorValue {
94209
VectorValue::new(self.vec_type.get_undef())
95210
}
96211

97212
// SubType: VectorType<BT> -> BT?
213+
/// Gets the element type of this `VectorType`.
214+
///
215+
/// # Example
216+
///
217+
/// ```no_run
218+
/// use inkwell::context::Context;
219+
///
220+
/// let context = Context::create();
221+
/// let f32_type = context.f32_type();
222+
/// let f32_vector_type = f32_type.vec_type(3);
223+
///
224+
/// assert_eq!(f32_vector_type.get_size(), 3);
225+
/// assert_eq!(f32_vector_type.get_element_type().into_float_type(), f32_type);
226+
/// ```
98227
pub fn get_element_type(&self) -> BasicTypeEnum {
99228
self.vec_type.get_element_type()
100229
}
101230

231+
/// Creates a `VectorType` with this `VectorType` for its element type.
232+
///
233+
/// # Example
234+
///
235+
/// ```no_run
236+
/// use inkwell::context::Context;
237+
///
238+
/// let context = Context::create();
239+
/// let f32_type = context.f32_type();
240+
/// let f32_vector_type = f32_type.vec_type(3);
241+
/// let f32_vector_vector_type = f32_vector_type.vec_type(3);
242+
///
243+
/// assert_eq!(f32_vector_vector_type.get_size(), 3);
244+
/// assert_eq!(f32_vector_vector_type.get_element_type().into_vector_type(), f32_vector_type);
245+
/// ```
102246
pub fn vec_type(&self, size: u32) -> VectorType {
103247
self.vec_type.vec_type(size)
104248
}
105249

250+
/// Creates a `PointerType` with this `VectorType` for its element type.
251+
///
252+
/// # Example
253+
///
254+
/// ```no_run
255+
/// use inkwell::context::Context;
256+
/// use inkwell::AddressSpace;
257+
///
258+
/// let context = Context::create();
259+
/// let f32_type = context.f32_type();
260+
/// let f32_vec_type = f32_type.vec_type(3);
261+
/// let f32_vec_ptr_type = f32_vec_type.ptr_type(AddressSpace::Generic);
262+
///
263+
/// assert_eq!(f32_vec_ptr_type.get_element_type().into_vector_type(), f32_vec_type);
264+
/// ```
106265
pub fn ptr_type(&self, address_space: AddressSpace) -> PointerType {
107266
self.vec_type.ptr_type(address_space)
108267
}
109268

269+
/// Creates a `FunctionType` with this `VectorType` for its return type.
270+
///
271+
/// # Example
272+
///
273+
/// ```no_run
274+
/// use inkwell::context::Context;
275+
///
276+
/// let context = Context::create();
277+
/// let f32_type = context.f32_type();
278+
/// let f32_vec_type = f32_type.vec_type(3);
279+
/// let fn_type = f32_vec_type.fn_type(&[], false);
280+
/// ```
110281
pub fn fn_type(&self, param_types: &[BasicTypeEnum], is_var_args: bool) -> FunctionType {
111282
self.vec_type.fn_type(param_types, is_var_args)
112283
}
113284

285+
/// Creates an `ArrayType` with this `VectorType` for its element type.
286+
///
287+
/// # Example
288+
///
289+
/// ```no_run
290+
/// use inkwell::context::Context;
291+
///
292+
/// let context = Context::create();
293+
/// let f32_type = context.f32_type();
294+
/// let f32_vec_type = f32_type.vec_type(3);
295+
/// let f32_vec_array_type = f32_vec_type.array_type(3);
296+
///
297+
/// assert_eq!(f32_vec_array_type.len(), 3);
298+
/// assert_eq!(f32_vec_array_type.get_element_type().into_vector_type(), f32_vec_type);
299+
/// ```
114300
pub fn array_type(&self, size: u32) -> ArrayType {
115301
self.vec_type.array_type(size)
116302
}
117303

304+
/// Creates a constant `ArrayValue`.
305+
///
306+
/// # Example
307+
/// ```no_run
308+
/// use inkwell::context::Context;
309+
/// use inkwell::types::VectorType;
310+
///
311+
/// let context = Context::create();
312+
/// let f32_type = context.f32_type();
313+
/// let f32_val = f32_type.const_float(0.);
314+
/// let f32_val2 = f32_type.const_float(2.);
315+
/// let f32_vec_type = f32_type.vec_type(2);
316+
/// let f32_vec_val = VectorType::const_vector(&[f32_val, f32_val2]);
317+
/// let f32_array = f32_vec_type.const_array(&[f32_vec_val, f32_vec_val]);
318+
///
319+
/// assert!(f32_array.is_const());
320+
/// ```
118321
pub fn const_array(&self, values: &[VectorValue]) -> ArrayValue {
119322
let mut values: Vec<LLVMValueRef> = values.iter()
120323
.map(|val| val.as_value_ref())
@@ -126,6 +329,19 @@ impl VectorType {
126329
ArrayValue::new(value)
127330
}
128331

332+
/// Gets a reference to the `Context` this `VectorType` was created in.
333+
///
334+
/// # Example
335+
///
336+
/// ```no_run
337+
/// use inkwell::context::Context;
338+
///
339+
/// let context = Context::create();
340+
/// let f32_type = context.f32_type();
341+
/// let f32_vec_type = f32_type.vec_type(7);
342+
///
343+
/// assert_eq!(*f32_vec_type.get_context(), context);
344+
/// ```
129345
pub fn get_context(&self) -> ContextRef {
130346
self.vec_type.get_context()
131347
}

src/values/array_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use llvm_sys::core::{LLVMIsConstant, LLVMIsAConstantArray, LLVMIsAConstantDataArray};
1+
use llvm_sys::core::{LLVMIsAConstantArray, LLVMIsAConstantDataArray};
22
use llvm_sys::prelude::LLVMValueRef;
33

44
use std::ffi::CStr;

0 commit comments

Comments
 (0)