### Get Function Example Source: https://thedan64.github.io/inkwell/inkwell/execution_engine/struct.ExecutionEngine.html Demonstrates retrieving a JIT-compiled function by its name and executing it. Ensure the function signature and calling convention are correctly specified. The retrieved function must be called unsafely. ```rust let context = Context::create(); let module = context.create_module("test"); let builder = context.create_builder(); // Set up the function signature let double = context.f64_type(); let sig = double.fn_type(&[], false); // Add the function to our module let f = module.add_function("test_fn", sig, None); let b = context.append_basic_block(f, "entry"); builder.position_at_end(b); // Insert a return statement let ret = double.const_float(64.0); builder.build_return(Some(&ret)).unwrap(); // create the JIT engine let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); // fetch our JIT'd function and execute it unsafe { let test_fn = ee.get_function:: f64>("test_fn").unwrap(); let return_value = test_fn.call(); assert_eq!(return_value, 64.0); } ``` -------------------------------- ### Example Search: u32 -> bool Source: https://thedan64.github.io/inkwell/inkwell/debug_info/fn.debug_metadata_version.html?search= Demonstrates an example search query for a type transformation from 'u32' to 'bool'. ```text u32 -> bool ``` -------------------------------- ### Example Search: std::vec Source: https://thedan64.github.io/inkwell/inkwell/debug_info/fn.debug_metadata_version.html?search= Demonstrates an example search query for 'std::vec'. ```text std::vec ``` -------------------------------- ### Build a Function that Returns a Pointer's Value Source: https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E This example demonstrates how to build an LLVM function that accepts a pointer to an i32 and returns the i32 value it points to. It covers context creation, module and builder setup, type definitions, function creation, basic block appending, and instruction building. ```rust use inkwell::context::Context; use inkwell::AddressSpace; // Builds a function which takes an i32 pointer and returns the pointed at i32. let context = Context::create(); let module = context.create_module("ret"); let builder = context.create_builder(); let i32_type = context.i32_type(); #[cfg(feature = "typed-pointers")] let i32_ptr_type = i32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let i32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = i32_type.fn_type(&[i32_ptr_type.into()], false); let fn_value = module.add_function("ret", fn_type, None); let entry = context.append_basic_block(fn_value, "entry"); let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value(); builder.position_at_end(entry); let pointee = builder.build_load(i32_type, i32_ptr_param, "load2").unwrap(); builder.build_return(Some(&pointee)).unwrap(); ``` -------------------------------- ### Example Search: Option, (T -> U) -> Option Source: https://thedan64.github.io/inkwell/inkwell/debug_info/fn.debug_metadata_version.html?search= Demonstrates an example search query for a generic type transformation involving Option. ```text Option, (T -> U) -> Option ``` -------------------------------- ### Get First Instruction in BasicBlock Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the first instruction within a BasicBlock. This example positions a return instruction and then asserts its retrieval. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::builder::Builder; use inkwell::values::InstructionOpcode; let context = Context::create(); let builder = context.create_builder(); let module = context.create_module("my_module"); let void_type = context.void_type(); let fn_type = void_type.fn_type(&[], false); let function = module.add_function("do_nothing", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); builder.build_return(None); assert_eq!(basic_block.get_first_instruction().unwrap().get_opcode(), InstructionOpcode::Return); ``` -------------------------------- ### Example: Iterating Through Uses of a Pointer Value Source: https://thedan64.github.io/inkwell/inkwell/values/struct.BasicValueUse.html?search= Demonstrates how to create a function, store a value to a pointer, free the pointer, and then retrieve and assert the uses of the pointer argument. This example highlights the usage of `get_first_use` and `get_next_use`. ```rust use inkwell::AddressSpace; use inkwell::context::Context; use inkwell::values::BasicValue; let context = Context::create(); let module = context.create_module("ivs"); let builder = context.create_builder(); let void_type = context.void_type(); let f32_type = context.f32_type(); #[cfg(feature = "typed-pointers")] let f32_ptr_type = f32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let f32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false); let function = module.add_function("take_f32_ptr", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); let arg1 = function.get_first_param().unwrap().into_pointer_value(); let f32_val = f32_type.const_float(std::f64::consts::PI); let store_instruction = builder.build_store(arg1, f32_val).unwrap(); let free_instruction = builder.build_free(arg1).unwrap(); let return_instruction = builder.build_return(None).unwrap(); let arg1_first_use = arg1.get_first_use().unwrap(); assert!(arg1_first_use.get_next_use().is_some()); ``` -------------------------------- ### Get Instruction by Name in BasicBlock Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Finds an instruction within a BasicBlock by its name. This example creates a store instruction with a name and then retrieves it. ```rust use inkwell::context::Context; use inkwell::AddressSpace; let context = Context::create(); let module = context.create_module("ret"); let builder = context.create_builder(); let void_type = context.void_type(); let i32_type = context.i32_type(); #[cfg(feature = "typed-pointers")] let i32_ptr_type = i32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let i32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false); let fn_value = module.add_function("ret", fn_type, None); let entry = context.append_basic_block(fn_value, "entry"); builder.position_at_end(entry); let var = builder.build_alloca(i32_type, "some_number").unwrap(); builder.build_store(var, i32_type.const_int(1 as u64, false)).unwrap(); builder.build_return(None).unwrap(); let block = fn_value.get_first_basic_block().unwrap(); let some_number = block.get_instruction_with_name("some_number"); assert!(some_number.is_some()); assert_eq!(some_number.unwrap().get_name().unwrap().to_str(), Ok("some_number")) ``` -------------------------------- ### get_instructions Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=u32+-%3E+bool Get an instruction iterator. ```APIDOC ## get_instructions ### Description Get an instruction iterator. ### Method `BasicBlock::get_instructions` ### Parameters None ### Returns `InstructionIter<'ctx>` - An iterator over the instructions in the basic block. ``` -------------------------------- ### get_instructions Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Get an instruction iterator for the BasicBlock. ```APIDOC ## get_instructions ### Description Get an instruction iterator. ### Method `BasicBlock.get_instructions()` ### Parameters None ### Response - `InstructionIter<'ctx>`: An iterator over the instructions in the BasicBlock. ``` -------------------------------- ### Get Size of ScalableVectorType Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html Retrieves the size (number of elements) of a ScalableVectorType. This example also demonstrates how to get the element type. ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vector_type = f32_type.scalable_vec_type(3); assert_eq!(f32_scalable_vector_type.get_size(), 3); assert_eq!(f32_scalable_vector_type.get_element_type().into_float_type(), f32_type); ``` -------------------------------- ### Get Size of ScalableVectorType (Example) Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html Demonstrates how to obtain the size of a ScalableVectorType. This example uses a float type and a vector size of 3. ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vec_type = f32_type.scalable_vec_type(3); let f32_scalable_vec_type_size = f32_scalable_vec_type.size_of(); ``` -------------------------------- ### ScalableVectorType::get_size Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html Gets the size of this ScalableVectorType. An example shows how to retrieve the size and verify the element type. ```APIDOC ## ScalableVectorType::get_size ### Description Gets the size of this `ScalableVectorType`. ### Example ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vector_type = f32_type.scalable_vec_type(3); assert_eq!(f32_scalable_vector_type.get_size(), 3); assert_eq!(f32_scalable_vector_type.get_element_type().into_float_type(), f32_type); ``` ``` -------------------------------- ### Create LLVM IR Module with Builder Source: https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html Demonstrates how to create a new LLVM module, define types, add a function, and use the builder to insert instructions like bit casting and return. This is a foundational example for generating basic LLVM IR. ```rust use inkwell::AddressSpace; use inkwell::context::Context; let context = Context::create(); let module = context.create_module("bc"); let void_type = context.void_type(); let f32_type = context.f32_type(); let i32_type = context.i32_type(); let arg_types = [i32_type.into()]; let fn_type = void_type.fn_type(&arg_types, false); let fn_value = module.add_function("bc", fn_type, None); let builder = context.create_builder(); let entry = context.append_basic_block(fn_value, "entry"); let i32_arg = fn_value.get_first_param().unwrap(); builder.position_at_end(entry); builder.build_bit_cast(i32_arg, f32_type, "i32tof32").unwrap(); builder.build_return(None).unwrap(); assert!(module.verify().is_ok()); ``` -------------------------------- ### Get Next BasicBlock Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets the BasicBlock succeeding the current one within its scope. Returns None if it's the last BasicBlock or has no parent. The example shows this for a single function and then for a function with multiple basic blocks. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::builder::Builder; let context = Context::create(); let module = context.create_module("my_module"); let void_type = context.void_type(); let fn_type = void_type.fn_type(&[], false); let function1 = module.add_function("do_nothing", fn_type, None); let basic_block1 = context.append_basic_block(function1, "entry"); assert!(basic_block1.get_next_basic_block().is_none()); let function2 = module.add_function("do_nothing", fn_type, None); let basic_block2 = context.append_basic_block(function2, "entry"); let basic_block3 = context.append_basic_block(function2, "next"); assert!(basic_block1.get_next_basic_block().is_none()); assert_eq!(basic_block2.get_next_basic_block().unwrap(), basic_block3); assert!(basic_block3.get_next_basic_block().is_none()); ``` -------------------------------- ### Get Previous BasicBlock Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Gets the BasicBlock preceding the current one within its scope. Returns None if it's the first BasicBlock or has no parent. The example demonstrates this for a single function and then for a function with multiple basic blocks. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::builder::Builder; let context = Context::create(); let module = context.create_module("my_module"); let void_type = context.void_type(); let fn_type = void_type.fn_type(&[], false); let function1 = module.add_function("do_nothing", fn_type, None); let basic_block1 = context.append_basic_block(function1, "entry"); assert!(basic_block1.get_previous_basic_block().is_none()); let function2 = module.add_function("do_nothing", fn_type, None); let basic_block2 = context.append_basic_block(function2, "entry"); let basic_block3 = context.append_basic_block(function2, "next"); assert!(basic_block2.get_previous_basic_block().is_none()); assert_eq!(basic_block3.get_previous_basic_block().unwrap(), basic_block2); ``` -------------------------------- ### Example Search Queries Source: https://thedan64.github.io/inkwell/inkwell/comdat/enum.ComdatSelectionKind.html?search= Illustrative search queries for exploring type conversions and standard library functionalities. ```text std::vec ``` ```text u32 -> bool ``` ```text Option, (T -> U) -> Option ``` -------------------------------- ### Get Terminator Instruction in BasicBlock Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the terminating instruction of a BasicBlock. This example adds a return instruction and confirms it is the terminator. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::builder::Builder; use inkwell::values::InstructionOpcode; let context = Context::create(); let builder = context.create_builder(); let module = context.create_module("my_module"); let void_type = context.void_type(); let fn_type = void_type.fn_type(&[], false); let function = module.add_function("do_nothing", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); builder.build_return(None); assert_eq!(basic_block.get_terminator().unwrap().get_opcode(), InstructionOpcode::Return); ``` -------------------------------- ### LLVM Version Range Examples Source: https://thedan64.github.io/inkwell/inkwell_internals/attr.llvm_versions.html?search=u32+-%3E+bool Demonstrates different ways to specify LLVM version ranges using the `llvm_versions` attribute. Use these to control compatibility with specific LLVM releases. ```rust // Inclusive range from 15 up to and including 18. #[llvm_versions(15..=18)] ``` ```rust // Exclusive range from 15 up to but not including 18. #[llvm_versions(15..18)] ``` ```rust // Inclusive range from 15.1 up to and including the latest release. #[llvm_versions(15.1..)] ``` -------------------------------- ### ScalableVectorType::get_element_type Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html Gets the element type of this ScalableVectorType. An example demonstrates retrieving the element type and verifying it against the original float type. ```APIDOC ## ScalableVectorType::get_element_type ### Description Gets the element type of this `ScalableVectorType`. ### Example ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vector_type = f32_type.scalable_vec_type(3); assert_eq!(f32_scalable_vector_type.get_size(), 3); assert_eq!(f32_scalable_vector_type.get_element_type().into_float_type(), f32_type); ``` ``` -------------------------------- ### Create JIT Execution Engine Source: https://thedan64.github.io/inkwell/inkwell/module/struct.Module.html Illustrates how to create a Just-In-Time (JIT) compiled execution engine with a specified optimization level. This is ideal for performance-critical applications. ```rust use inkwell::OptimizationLevel; use inkwell::context::Context; use inkwell::module::Module; use inkwell::targets::{InitializationConfig, Target}; Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target"); let context = Context::create(); let module = context.create_module("my_module"); let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); assert_eq!(module.get_context(), context); ``` -------------------------------- ### Setting up Debug Info Module Source: https://thedan64.github.io/inkwell/inkwell/debug_info/index.html Initializes the context, module, and DebugInfoBuilder for generating debug information. This includes setting module flags and creating the builder with essential compilation unit details. ```rust let context = Context::create(); let module = context.create_module("bin"); let debug_metadata_version = context.i32_type().const_int(3, false); module.add_basic_value_flag( "Debug Info Version", inkwell::module::FlagBehavior::Warning, debug_metadata_version, ); let builder = context.create_builder(); let (dibuilder, compile_unit) = module.create_debug_info_builder( true, /* language */ inkwell::debug_info::DWARFSourceLanguage::C, /* filename */ "source_file", /* directory */ ".", /* producer */ "my llvm compiler frontend", /* is_optimized */ false, /* compiler command line flags */ "", /* runtime_ver */ 0, /* split_name */ "", /* kind */ inkwell::debug_info::DWARFEmissionKind::Full, /* dwo_id */ 0, /* split_debug_inling */ false, /* debug_info_for_profiling */ false, ); ``` -------------------------------- ### Add Module Example Source: https://thedan64.github.io/inkwell/inkwell/execution_engine/struct.ExecutionEngine.html Shows how to add a module to an existing JIT execution engine. The method returns an error if the module is already associated with an execution engine. ```rust use inkwell::targets::{InitializationConfig, Target}; use inkwell::context::Context; use inkwell::OptimizationLevel; Target::initialize_native(&InitializationConfig::default()).unwrap(); let context = Context::create(); let module = context.create_module("test"); let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); assert!(ee.add_module(&module).is_err()); ``` -------------------------------- ### ScalableVectorType::size_of Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html Gets the size of this ScalableVectorType. The returned value may vary depending on the target architecture. An example demonstrates its usage with f32 type. ```APIDOC ## ScalableVectorType::size_of ### Description Gets the size of this `ScalableVectorType`. Value may vary depending on the target architecture. ### Example ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vec_type = f32_type.scalable_vec_type(3); let f32_scalable_vec_type_size = f32_scalable_vec_type.size_of(); ``` ``` -------------------------------- ### Create a new Builder Source: https://thedan64.github.io/inkwell/inkwell/context/struct.ContextRef.html Demonstrates how to create a new Builder associated with a given Context. This is a fundamental step for constructing LLVM IR. ```rust use inkwell::context::Context; let context = Context::create(); let builder = context.create_builder(); ``` -------------------------------- ### Get Attribute Type Value Source: https://thedan64.github.io/inkwell/inkwell/attributes/struct.Attribute.html Retrieves the `AnyTypeEnum` associated with a type attribute. This example demonstrates creating a type attribute and then asserting its type value. ```rust use inkwell::context::Context; use inkwell::attributes::Attribute; use inkwell::types::AnyType; let context = Context::create(); let kind_id = Attribute::get_named_enum_kind_id("sret"); let any_type = context.i32_type().as_any_type_enum(); let type_attribute = context.create_type_attribute( kind_id, any_type, ); assert!(type_attribute.is_type()); assert_eq!(type_attribute.get_type_value(), any_type); assert_ne!(type_attribute.get_type_value(), context.i64_type().as_any_type_enum()); ``` -------------------------------- ### Get Field at Index Source: https://thedan64.github.io/inkwell/inkwell/values/struct.StructValue.html?search= Retrieves a field from a StructValue by its index. Returns None if the index is out of bounds. This example demonstrates creating a struct, accessing its fields, and verifying their types. ```rust use inkwell::context::Context; let context = Context::create(); let i32_type = context.i32_type(); let i8_type = context.i8_type(); let i8_val = i8_type.const_all_ones(); let i32_val = i32_type.const_all_ones(); let struct_type = context.struct_type(&[i8_type.into(), i32_type.into()], false); let struct_val = struct_type.const_named_struct(&[i8_val.into(), i32_val.into()]); assert!(struct_val.get_field_at_index(0).is_some()); assert!(struct_val.get_field_at_index(1).is_some()); assert!(struct_val.get_field_at_index(3).is_none()); assert!(struct_val.get_field_at_index(0).unwrap().is_int_value()); ``` -------------------------------- ### build_and Source: https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html?search=u32+-%3E+bool Builds a bitwise AND instruction. ```APIDOC ## build_and ### Description Builds a bitwise AND instruction. ### Method `build_and>(&self, lhs: T, rhs: T, name: &str) -> Result` ### Parameters - `lhs` (T): The left-hand side integer value. - `rhs` (T): The right-hand side integer value. - `name` (str): The name for the resulting instruction. ``` -------------------------------- ### Get Last Instruction in BasicBlock Source: https://thedan64.github.io/inkwell/inkwell/basic_block/struct.BasicBlock.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Retrieves the last instruction within a BasicBlock. This example adds a return instruction and verifies it's correctly identified as the last one. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::builder::Builder; use inkwell::values::InstructionOpcode; let context = Context::create(); let builder = context.create_builder(); let module = context.create_module("my_module"); let void_type = context.void_type(); let fn_type = void_type.fn_type(&[], false); let function = module.add_function("do_nothing", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); builder.build_return(None); assert_eq!(basic_block.get_last_instruction().unwrap().get_opcode(), InstructionOpcode::Return); ``` -------------------------------- ### Build Resume Instruction Source: https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html Demonstrates how to build a resume instruction for exception handling. This is used to resume unwinding an interrupted exception. ```rust use inkwell::context::Context; use inkwell::AddressSpace; use inkwell::module::Linkage; let context = Context::create(); let module = context.create_module("sum"); let builder = context.create_builder(); let f32_type = context.f32_type(); let fn_type = f32_type.fn_type(&[], false); // we will pretend this function can throw an exception let function = module.add_function("bomb", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); let pi = f32_type.const_float(std::f64::consts::PI); builder.build_return(Some(&pi)).unwrap(); let function2 = module.add_function("wrapper", fn_type, None); let basic_block2 = context.append_basic_block(function2, "entry"); builder.position_at_end(basic_block2); let then_block = context.append_basic_block(function2, "then_block"); let catch_block = context.append_basic_block(function2, "catch_block"); let call_site = builder.build_invoke(function, &[], then_block, catch_block, "get_pi").unwrap(); { builder.position_at_end(then_block); // in the then_block, the `call_site` value is defined and can be used let result = call_site.try_as_basic_value().unwrap_basic(); builder.build_return(Some(&result)).unwrap(); } { builder.position_at_end(catch_block); // the personality function used by C++ let personality_function = { let name = "__gxx_personality_v0"; let linkage = Some(Linkage::External); module.add_function(name, context.i64_type().fn_type(&[], false), linkage) }; // type of an exception in C++ #[cfg(feature = "typed-pointers")] let i8_ptr_type = context.i8_type().ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let i8_ptr_type = context.ptr_type(AddressSpace::default()); let i32_type = context.i32_type(); let exception_type = context.struct_type(&[i8_ptr_type.into(), i32_type.into()], false); // make the landing pad; must give a concrete type to the slice let res = builder.build_landing_pad( exception_type, personality_function, &[], true, "res").unwrap(); // do cleanup ... builder.build_resume(res).unwrap(); } ``` -------------------------------- ### ScalableVectorType::ptr_type Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html Creates a PointerType with this ScalableVectorType for its element type. This method is deprecated starting from version 15.0, and Context::ptr_type should be used instead. An example shows its usage with AddressSpace. ```APIDOC ## ScalableVectorType::ptr_type ### Description 👎Deprecated: Starting from version 15.0, LLVM doesn’t differentiate between pointer types. Use Context::ptr_type instead. Creates a `PointerType` with this `ScalableVectorType` for its element type. ### Example ```rust use inkwell::context::Context; use inkwell::AddressSpace; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vec_type = f32_type.scalable_vec_type(3); let f32_scalable_vec_ptr_type = f32_scalable_vec_type.ptr_type(AddressSpace::default()); #[cfg(feature = "typed-pointers")] assert_eq!(f32_scalable_vec_ptr_type.get_element_type().into_scalable_vector_type(), f32_scalable_vec_type); ``` ``` -------------------------------- ### Create Target Machine with Options Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.Target.html Use `create_target_machine_from_options` to configure a target machine with specific CPU, features, ABI, and optimization level. This is useful for fine-tuning code generation for a particular architecture. ```rust use inkwell::targets::{InitializationConfig, Target, TargetTriple, TargetMachineOptions}; use inkwell::OptimizationLevel; Target::initialize_x86(&InitializationConfig::default()); let triple = TargetTriple::create("x86_64-pc-linux-gnu"); let target = Target::from_triple(&triple).unwrap(); let options = TargetMachineOptions::default() .set_cpu("x86-64") .set_features("+avx2") .set_abi("sysv") .set_level(OptimizationLevel::Aggressive); let target_machine = target.create_target_machine_from_options(&triple, options).unwrap(); assert_eq!(target_machine.get_cpu().to_str(), Ok("x86-64")); assert_eq!(target_machine.get_feature_string().to_str(), Ok("+avx2")); ``` -------------------------------- ### Get Next Use of a BasicValue Source: https://thedan64.github.io/inkwell/inkwell/values/struct.BasicValueUse.html Demonstrates how to retrieve the next use of a BasicValue, InstructionValue, or BasicBlock. This is useful for iterating through all places a value is referenced. Requires setup of a context, module, builder, and function. ```rust use inkwell::AddressSpace; use inkwell::context::Context; use inkwell::values::BasicValue; let context = Context::create(); let module = context.create_module("ivs"); let builder = context.create_builder(); let void_type = context.void_type(); let f32_type = context.f32_type(); #[cfg(feature = "typed-pointers")] let f32_ptr_type = f32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let f32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false); let function = module.add_function("take_f32_ptr", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); let arg1 = function.get_first_param().unwrap().into_pointer_value(); let f32_val = f32_type.const_float(std::f64::consts::PI); let store_instruction = builder.build_store(arg1, f32_val).unwrap(); let free_instruction = builder.build_free(arg1).unwrap(); let return_instruction = builder.build_return(None).unwrap(); let arg1_first_use = arg1.get_first_use().unwrap(); assert!(arg1_first_use.get_next_use().is_some()); ``` -------------------------------- ### Example: Identifying Users of Store Instruction Operands Source: https://thedan64.github.io/inkwell/inkwell/values/struct.BasicValueUse.html?search= Demonstrates how to create a store instruction and then retrieve the `BasicValueUse` for each of its operands using `get_operand_use`. It then asserts that the user of these uses is indeed the store instruction itself. ```rust use inkwell::AddressSpace; use inkwell::context::Context; use inkwell::values::BasicValue; let context = Context::create(); let module = context.create_module("ivs"); let builder = context.create_builder(); let void_type = context.void_type(); let f32_type = context.f32_type(); #[cfg(feature = "typed-pointers")] let f32_ptr_type = f32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let f32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false); let function = module.add_function("take_f32_ptr", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); let arg1 = function.get_first_param().unwrap().into_pointer_value(); let f32_val = f32_type.const_float(std::f64::consts::PI); let store_instruction = builder.build_store(arg1, f32_val).unwrap(); let free_instruction = builder.build_free(arg1).unwrap(); let return_instruction = builder.build_return(None).unwrap(); let store_operand_use0 = store_instruction.get_operand_use(0).unwrap(); let store_operand_use1 = store_instruction.get_operand_use(1).unwrap(); assert_eq!(store_operand_use0.get_user(), store_instruction); assert_eq!(store_operand_use1.get_user(), store_instruction); ``` -------------------------------- ### Get User of a Store Instruction Operand Source: https://thedan64.github.io/inkwell/inkwell/values/struct.BasicValueUse.html Retrieves the user of a specific operand within a store instruction. This helps identify which instruction is using a particular value. Requires the setup of a context, module, builder, and function. ```rust use inkwell::AddressSpace; use inkwell::context::Context; use inkwell::values::BasicValue; let context = Context::create(); let module = context.create_module("ivs"); let builder = context.create_builder(); let void_type = context.void_type(); let f32_type = context.f32_type(); #[cfg(feature = "typed-pointers")] let f32_ptr_type = f32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let f32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false); let function = module.add_function("take_f32_ptr", fn_type, None); let basic_block = context.append_basic_block(function, "entry"); builder.position_at_end(basic_block); let arg1 = function.get_first_param().unwrap().into_pointer_value(); let f32_val = f32_type.const_float(std::f64::consts::PI); let store_instruction = builder.build_store(arg1, f32_val).unwrap(); let free_instruction = builder.build_free(arg1).unwrap(); let return_instruction = builder.build_return(None).unwrap(); let store_operand_use0 = store_instruction.get_operand_use(0).unwrap(); let store_operand_use1 = store_instruction.get_operand_use(1).unwrap(); assert_eq!(store_operand_use0.get_user(), store_instruction); assert_eq!(store_operand_use1.get_user(), store_instruction); ``` -------------------------------- ### Create a new Context Source: https://thedan64.github.io/inkwell/inkwell/context/struct.ContextRef.html?search= Demonstrates how to create a new LLVM Context. This is a fundamental step before creating other LLVM structures like modules or builders. ```rust use inkwell::context::Context; let context = Context::create(); ``` -------------------------------- ### Set and Get Module Target Triple Source: https://thedan64.github.io/inkwell/inkwell/module/struct.Module.html Demonstrates how to initialize the target, create a module, set its target triple, and then retrieve it to verify the setting. Useful for ensuring your module is configured for the correct architecture. ```rust use inkwell::context::Context; use inkwell::targets::{Target, TargetTriple}; Target::initialize_x86(&Default::default()); let context = Context::create(); let module = context.create_module("mod"); let triple = TargetTriple::create("x86_64-pc-linux-gnu"); assert_eq!(module.get_triple(), TargetTriple::create("")); module.set_triple(&triple); assert_eq!(module.get_triple(), triple); ``` -------------------------------- ### Get Comdat Source: https://thedan64.github.io/inkwell/inkwell/values/struct.GlobalValue.html Gets the Comdat assigned to this GlobalValue, if any. ```rust pub fn get_comdat(self) -> Option ``` -------------------------------- ### TargetMachineOptions::new Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetMachineOptions.html?search=std%3A%3Avec Creates a new instance of TargetMachineOptions with default values. ```APIDOC ## TargetMachineOptions::new ### Description Creates a new instance of `TargetMachineOptions` with default values. ### Method `new()` ### Returns - `Self`: A new `TargetMachineOptions` instance. ``` -------------------------------- ### Get Context of VoidType Source: https://thedan64.github.io/inkwell/inkwell/types/struct.VoidType.html?search=u32+-%3E+bool Gets a reference to the Context this VoidType was created in. ```rust pub fn get_context(self) -> ContextRef<'ctx> ``` ```rust use inkwell::context::Context; let context = Context::create(); let void_type = context.void_type(); assert_eq!(void_type.get_context(), context); ``` -------------------------------- ### Get Comdat Selection Kind Source: https://thedan64.github.io/inkwell/inkwell/comdat/struct.Comdat.html?search=u32+-%3E+bool Gets the current selection kind of the Comdat. ```rust pub fn get_selection_kind(self) -> ComdatSelectionKind ``` -------------------------------- ### Create Metadata Type Source: https://thedan64.github.io/inkwell/inkwell/context/struct.ContextRef.html Demonstrates creating a metadata type and verifying its context. ```rust use inkwell::context::Context; use inkwell::values::IntValue; let context = Context::create(); let md_type = context.metadata_type(); assert_eq!(md_type.get_context(), context); ``` -------------------------------- ### Create Interpreter Execution Engine Source: https://thedan64.github.io/inkwell/inkwell/module/struct.Module.html Demonstrates creating an interpreter-based execution engine. This is useful for debugging or when a JIT is not available or desired. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::targets::{InitializationConfig, Target}; Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target"); let context = Context::create(); let module = context.create_module("my_module"); let execution_engine = module.create_interpreter_execution_engine().unwrap(); assert_eq!(module.get_context(), context); ``` -------------------------------- ### Get Bit Width of IntType Source: https://thedan64.github.io/inkwell/inkwell/types/struct.IntType.html Gets the bit width of a boolean type, which is 1. ```rust use inkwell::context::Context; let context = Context::create(); let bool_type = context.bool_type(); assert_eq!(bool_type.get_bit_width(), 1); ``` -------------------------------- ### Target::initialize_system_z Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.Target.html?search=u32+-%3E+bool Initializes the SystemZ target. This function is only available when the `target-systemz` crate feature is enabled. ```APIDOC ## Target::initialize_system_z ### Description Initializes the SystemZ target. This function is only available when the `target-systemz` crate feature is enabled. ### Parameters * `config` (*InitializationConfig*) - Configuration for initialization. ### Method `pub fn initialize_system_z(config: &InitializationConfig)` ``` -------------------------------- ### Get Element Type of ScalableVectorType Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html?search=u32+-%3E+bool Gets the element type of this ScalableVectorType. This is useful for understanding the composition of the vector. ```rust pub fn get_element_type(self) -> BasicTypeEnum<'ctx> ``` ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vector_type = f32_type.scalable_vec_type(3); assert_eq!(f32_scalable_vector_type.get_size(), 3); assert_eq!(f32_scalable_vector_type.get_element_type().into_float_type(), f32_type); ``` -------------------------------- ### Creating a New TargetMachineOptions Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetMachineOptions.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Instantiates a new TargetMachineOptions struct with default settings. This is the starting point for configuring target machine options. ```rust pub fn new() -> Self ``` -------------------------------- ### Get Alignment of BasicTypeEnum Source: https://thedan64.github.io/inkwell/inkwell/types/enum.BasicTypeEnum.html?search= Gets the alignment of this BasicTypeEnum. The value may vary depending on the target architecture. ```rust fn get_alignment(&self) -> IntValue<'ctx> ``` -------------------------------- ### ExecutionEngine::new Source: https://thedan64.github.io/inkwell/inkwell/execution_engine/struct.ExecutionEngine.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a new ExecutionEngine. This is an unsafe function as it requires a raw LLVMExecutionEngineRef. ```APIDOC ## pub unsafe fn new Creates a new `ExecutionEngine`. ### Parameters * `execution_engine`: A reference-counted pointer to the LLVM execution engine. * `jit_mode`: A boolean indicating whether the engine is in JIT mode. ``` -------------------------------- ### ExecutionEngine::new Source: https://thedan64.github.io/inkwell/inkwell/execution_engine/struct.ExecutionEngine.html Creates a new ExecutionEngine. This is an unsafe function as it requires a raw LLVMExecutionEngineRef. ```APIDOC ## pub unsafe fn new ### Description Creates a new `ExecutionEngine`. ### Parameters * `execution_engine` (Rc) - A reference-counted pointer to the LLVM execution engine. * `jit_mode` (bool) - A boolean indicating whether the engine is running in JIT mode. ``` -------------------------------- ### Get Size of BasicTypeEnum Source: https://thedan64.github.io/inkwell/inkwell/types/enum.BasicTypeEnum.html?search= Gets the size of this BasicTypeEnum. The value may vary depending on the target architecture. ```rust fn size_of(&self) -> Option> ``` -------------------------------- ### Get Size of ScalableVectorType (u32) Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ScalableVectorType.html?search=u32+-%3E+bool Gets the exact size of this ScalableVectorType as a u32. Also demonstrates retrieving the element type. ```rust pub fn get_size(self) -> u32 ``` ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_scalable_vector_type = f32_type.scalable_vec_type(3); assert_eq!(f32_scalable_vector_type.get_size(), 3); assert_eq!(f32_scalable_vector_type.get_element_type().into_float_type(), f32_type); ``` -------------------------------- ### Add Global Mapping Example Source: https://thedan64.github.io/inkwell/inkwell/execution_engine/struct.ExecutionEngine.html Demonstrates how to map a function to a specific address within the execution engine. This is useful for providing external functions to the JIT-compiled code. Ensure native targets are initialized before use. ```rust use inkwell::targets::{InitializationConfig, Target}; use inkwell::context::Context; use inkwell::OptimizationLevel; Target::initialize_native(&InitializationConfig::default()).unwrap(); extern fn sumf(a: f64, b: f64) -> f64 { a + b } let context = Context::create(); let module = context.create_module("test"); let builder = context.create_builder(); let ft = context.f64_type(); let fnt = ft.fn_type(&[], false); let f = module.add_function("test_fn", fnt, None); let b = context.append_basic_block(f, "entry"); builder.position_at_end(b); let extf = module.add_function("sumf", ft.fn_type(&[ft.into(), ft.into()], false), None); let argf = ft.const_float(64.); let call_site_value = builder.build_call(extf, &[argf.into(), argf.into()], "retv").unwrap(); let retv = call_site_value.try_as_basic_value().unwrap_basic().into_float_value(); builder.build_return(Some(&retv)).unwrap(); let mut ee = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); ee.add_global_mapping(&extf, sumf as usize); let result = unsafe { ee.run_function(f, &[]) }.as_float(&ft); assert_eq!(result, 128.); ``` -------------------------------- ### Get Basic Type Size (BasicType Trait) Source: https://thedan64.github.io/inkwell/inkwell/types/struct.FloatType.html Gets the size of this BasicType. The size may vary depending on the target architecture. ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let size = f32_type.size_of(); ``` -------------------------------- ### Create Default Execution Engine Source: https://thedan64.github.io/inkwell/inkwell/module/struct.Module.html Shows how to create a default execution engine for a module. This is useful for running compiled code directly. ```rust use inkwell::context::Context; use inkwell::module::Module; use inkwell::targets::{InitializationConfig, Target}; Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target"); let context = Context::create(); let module = context.create_module("my_module"); let execution_engine = module.create_execution_engine().unwrap(); assert_eq!(module.get_context(), context); ``` -------------------------------- ### Target::initialize_all Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.Target.html Initializes all available targets. This function does not return a value. ```APIDOC ## Target::initialize_all ### Description Initializes all available targets. This function does not return a value. ### Method `pub fn initialize_all(config: &InitializationConfig)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```rust use inkwell::targets::InitializationConfig; inkwell::targets::Target::initialize_all(&InitializationConfig::default()); ``` ### Response This method does not return a value. ``` -------------------------------- ### Get VectorType Size and Element Type Source: https://thedan64.github.io/inkwell/inkwell/types/struct.VectorType.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Demonstrates how to get the exact size and element type of a VectorType. Asserts that the size is as expected and the element type is a float type. ```rust use inkwell::context::Context; let context = Context::create(); let f32_type = context.f32_type(); let f32_vector_type = f32_type.vec_type(3); assert_eq!(f32_vector_type.get_size(), 3); assert_eq!(f32_vector_type.get_element_type().into_float_type(), f32_type); ``` -------------------------------- ### Create a new Module Source: https://thedan64.github.io/inkwell/inkwell/context/struct.ContextRef.html Shows how to create a new LLVM Module within a specific Context. Modules are containers for functions and global variables. ```rust use inkwell::context::Context; let context = Context::create(); let module = context.create_module("my_module"); ``` -------------------------------- ### Get Named Enum Kind ID Source: https://thedan64.github.io/inkwell/inkwell/attributes/struct.Attribute.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Shows how to get the enum kind ID for builtin attribute names using `Attribute::get_named_enum_kind_id`. Returns 0 for non-existent names. ```rust use inkwell::attributes::Attribute; // This kind id doesn't exist: assert_eq!(Attribute::get_named_enum_kind_id("foobar"), 0); // These are real kind ids: assert_eq!(Attribute::get_named_enum_kind_id("align"), 1); assert_eq!(Attribute::get_named_enum_kind_id("builtin"), 5); ``` -------------------------------- ### create_jit_execution_engine Source: https://thedan64.github.io/inkwell/inkwell/module/struct.Module.html Creates a JIT ExecutionEngine from this Module. ```APIDOC ## pub fn create_jit_execution_engine(&self, opt_level: OptimizationLevel) -> Result, LLVMString> Creates a JIT `ExecutionEngine` from this `Module`. ##### §Example ``` use inkwell::OptimizationLevel; use inkwell::context::Context; use inkwell::module::Module; use inkwell::targets::{InitializationConfig, Target}; Target::initialize_native(&InitializationConfig::default()).expect("Failed to initialize native target"); let context = Context::create(); let module = context.create_module("my_module"); let execution_engine = module.create_jit_execution_engine(OptimizationLevel::None).unwrap(); assert_eq!(module.get_context(), context); ``` ``` -------------------------------- ### Build Function to Return Pointed-at Value Source: https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html?search= Demonstrates building a function that takes a pointer to an i32 and returns the i32 value it points to. This involves creating context, module, builder, types, and function signature, then emitting load and return instructions. ```rust use inkwell::context::Context; use inkwell::AddressSpace; // Builds a function which takes an i32 pointer and returns the pointed at i32. let context = Context::create(); let module = context.create_module("ret"); let builder = context.create_builder(); let i32_type = context.i32_type(); #[cfg(feature = "typed-pointers")] let i32_ptr_type = i32_type.ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let i32_ptr_type = context.ptr_type(AddressSpace::default()); let fn_type = i32_type.fn_type(&[i32_ptr_type.into()], false); let fn_value = module.add_function("ret", fn_type, None); let entry = context.append_basic_block(fn_value, "entry"); let i32_ptr_param = fn_value.get_first_param().unwrap().into_pointer_value(); builder.position_at_end(entry); let pointee = builder.build_load(i32_type, i32_ptr_param, "load2").unwrap(); builder.build_return(Some(&pointee)).unwrap(); ``` -------------------------------- ### Get Pointer-Sized Integer Type Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetData.html?search= Gets the `IntType` representing a bit width of a pointer. It will be assigned the referenced context. This method is deprecated and will be removed in the future. Use `Context::ptr_sized_int_type` instead. ```rust pub fn ptr_sized_int_type_in_context<'ctx>( &self, context: impl AsContextRef<'ctx>, address_space: Option, ) -> IntType<'ctx> ``` -------------------------------- ### Build Catch-All Landing Pad Source: https://thedan64.github.io/inkwell/inkwell/builder/struct.Builder.html Creates a catch-all landing pad that matches any exception, similar to C++'s `catch(...)`. A null pointer clause achieves this. ```rust use inkwell::context::Context; use inkwell::AddressSpace; use inkwell::module::Linkage; let context = Context::create(); let module = context.create_module("sum"); let builder = context.create_builder(); // type of an exception in C++ #[cfg(feature = "typed-pointers")] let i8_ptr_type = context.i8_type().ptr_type(AddressSpace::default()); #[cfg(not(feature = "typed-pointers"))] let i8_ptr_type = context.ptr_type(AddressSpace::default()); let i32_type = context.i32_type(); let exception_type = context.struct_type(&[i8_ptr_type.into(), i32_type.into()], false); // the personality function used by C++ let personality_function = { let name = "__gxx_personality_v0"; let linkage = Some(Linkage::External); module.add_function(name, context.i64_type().fn_type(&[], false), linkage) }; // make a null pointer of type i8 let null = i8_ptr_type.const_zero(); // make the catch all landing pad let res = builder.build_landing_pad(exception_type, personality_function, &[null.into()], false, "res").unwrap(); ``` -------------------------------- ### Context::create Source: https://thedan64.github.io/inkwell/inkwell/context/struct.Context.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a new, independent LLVM Context. This is the recommended way to create a context for general use. ```APIDOC ## fn create() -> Self ### Description Creates a new `Context`. ### Example ```rust use inkwell::context::Context; let context = Context::create(); ``` ``` -------------------------------- ### type_id Source: https://thedan64.github.io/inkwell/inkwell/values/struct.PointerValue.html Gets the TypeId of the PointerValue. ```APIDOC ## fn type_id(&self) -> TypeId ### Description Gets the `TypeId` of `self`. ### Method GET (conceptual) ### Endpoint N/A (method call) ### Parameters None ### Response #### Success Response - **TypeId**: The type identifier of the value. ``` -------------------------------- ### Create New TargetMachineOptions Source: https://thedan64.github.io/inkwell/inkwell/targets/struct.TargetMachineOptions.html Creates a new instance of TargetMachineOptions with default settings. This is the starting point for configuring target machine options. ```rust pub fn new() -> Self; ``` -------------------------------- ### get_alignment Source: https://thedan64.github.io/inkwell/inkwell/types/struct.ArrayType.html?search=std%3A%3Avec Gets the alignment of this `BasicType`. ```APIDOC ## get_alignment ### Description Gets the alignment of this `BasicType`. Value may vary depending on the target architecture. ### Method `self.get_alignment()` ### Parameters None ### Response - Returns an `IntValue<'ctx>` representing the alignment of the type. ### Example ```rust // Example usage would go here, assuming ArrayType is already defined ``` ```