Skip to content

Commit 4ce5375

Browse files
committed
Added docs for build_ptr_diff, build_store, and build_load
1 parent ad1c738 commit 4ce5375

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/builder.rs

+75
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,31 @@ impl Builder {
215215
PointerValue::new(value)
216216
}
217217

218+
/// Builds an instruction which calculates the difference of two pointers.
219+
///
220+
/// # Example
221+
///
222+
/// ```no_run
223+
/// use inkwell::context::Context;
224+
/// use inkwell::AddressSpace;
225+
///
226+
/// // Builds a function which diffs two pointers
227+
/// let context = Context::create();
228+
/// let module = context.create_module("ret");
229+
/// let builder = context.create_builder();
230+
/// let void_type = context.void_type();
231+
/// let i32_type = context.i32_type();
232+
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::Generic);
233+
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into(), i32_ptr_type.into()], false);
234+
/// let fn_value = module.add_function("ret", fn_type, None);
235+
/// let entry = fn_value.append_basic_block("entry");
236+
/// let i32_ptr_param1 = fn_value.get_first_param().unwrap().into_pointer_value();
237+
/// let i32_ptr_param2 = fn_value.get_nth_param(1).unwrap().into_pointer_value();
238+
///
239+
/// builder.position_at_end(&entry);
240+
/// builder.build_ptr_diff(i32_ptr_param1, i32_ptr_param2, "diff");
241+
/// builder.build_return(None);
242+
/// ```
218243
pub fn build_ptr_diff(&self, lhs_ptr: PointerValue, rhs_ptr: PointerValue, name: &str) -> IntValue {
219244
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
220245

@@ -240,6 +265,31 @@ impl Builder {
240265
PhiValue::new(value)
241266
}
242267

268+
/// Builds a store instruction. It allows you to store a value of type `T` in a pointer to a type `T`.
269+
///
270+
/// # Example
271+
///
272+
/// ```no_run
273+
/// use inkwell::context::Context;
274+
/// use inkwell::AddressSpace;
275+
///
276+
/// // Builds a function which takes an i32 pointer and stores a 7 in it.
277+
/// let context = Context::create();
278+
/// let module = context.create_module("ret");
279+
/// let builder = context.create_builder();
280+
/// let void_type = context.void_type();
281+
/// let i32_type = context.i32_type();
282+
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::Generic);
283+
/// let i32_seven = i32_type.const_int(7, false);
284+
/// let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false);
285+
/// let fn_value = module.add_function("ret", fn_type, None);
286+
/// let entry = fn_value.append_basic_block("entry");
287+
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
288+
///
289+
/// builder.position_at_end(&entry);
290+
/// builder.build_store(i32_ptr_param, i32_seven);
291+
/// builder.build_return(None);
292+
/// ```
243293
pub fn build_store<V: BasicValue>(&self, ptr: PointerValue, value: V) -> InstructionValue {
244294
let value = unsafe {
245295
LLVMBuildStore(self.builder, value.as_value_ref(), ptr.as_value_ref())
@@ -248,6 +298,31 @@ impl Builder {
248298
InstructionValue::new(value)
249299
}
250300

301+
/// Builds a load instruction. It allows you to retrieve a value of type `T` from a pointer to a type `T`.
302+
///
303+
/// # Example
304+
///
305+
/// ```no_run
306+
/// use inkwell::context::Context;
307+
/// use inkwell::AddressSpace;
308+
///
309+
/// // Builds a function which takes an i32 pointer and returns the pointed at i32.
310+
/// let context = Context::create();
311+
/// let module = context.create_module("ret");
312+
/// let builder = context.create_builder();
313+
/// let i32_type = context.i32_type();
314+
/// let i32_ptr_type = i32_type.ptr_type(AddressSpace::Generic);
315+
/// let fn_type = i32_type.fn_type(&[i32_ptr_type.into()], false);
316+
/// let fn_value = module.add_function("ret", fn_type, None);
317+
/// let entry = fn_value.append_basic_block("entry");
318+
/// let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value();
319+
///
320+
/// builder.position_at_end(&entry);
321+
///
322+
/// let pointee = builder.build_load(i32_ptr_param, "load");
323+
///
324+
/// builder.build_return(Some(&pointee));
325+
/// ```
251326
pub fn build_load(&self, ptr: PointerValue, name: &str) -> BasicValueEnum {
252327
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
253328

tests/all/test_values.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1227,14 +1227,18 @@ fn test_aggregate_returns() {
12271227
let builder = context.create_builder();
12281228
let module = context.create_module("my_mod");
12291229
let i32_type = context.i32_type();
1230+
let i32_ptr_type = i32_type.ptr_type(AddressSpace::Local);
12301231
let i32_three = i32_type.const_int(3, false);
12311232
let i32_seven = i32_type.const_int(7, false);
12321233
let struct_type = context.struct_type(&[i32_type.into(), i32_type.into()], false);
1233-
let fn_type = struct_type.fn_type(&[], false);
1234+
let fn_type = struct_type.fn_type(&[i32_ptr_type.into(), i32_ptr_type.into()], false);
12341235
let fn_value = module.add_function("my_func", fn_type, None);
12351236
let bb = fn_value.append_basic_block("entry");
1237+
let ptr_param1 = fn_value.get_first_param().unwrap().into_pointer_value();
1238+
let ptr_param2 = fn_value.get_nth_param(1).unwrap().into_pointer_value();
12361239

12371240
builder.position_at_end(&bb);
1241+
builder.build_ptr_diff(ptr_param1, ptr_param2, "diff");
12381242
builder.build_aggregate_return(&[i32_three.into(), i32_seven.into()]);
12391243

12401244
assert!(module.verify().is_ok());

0 commit comments

Comments
 (0)