[core] codegen: Add CodeGenContext::get_size_type
Convenience method for getting the `size_t` LLVM type without the use of `CodeGenerator`.
This commit is contained in:
parent
3ebd4ba5d1
commit
f8530e0ef6
@ -19,6 +19,9 @@ pub trait CodeGenerator {
|
|||||||
fn get_name(&self) -> &str;
|
fn get_name(&self) -> &str;
|
||||||
|
|
||||||
/// Return an instance of [`IntType`] corresponding to the type of `size_t` for this instance.
|
/// Return an instance of [`IntType`] corresponding to the type of `size_t` for this instance.
|
||||||
|
///
|
||||||
|
/// Prefer using [`CodeGenContext::get_size_type`] if [`CodeGenContext`] is available, as it is
|
||||||
|
/// equivalent to this function in a more concise syntax.
|
||||||
fn get_size_type<'ctx>(&self, ctx: &'ctx Context) -> IntType<'ctx>;
|
fn get_size_type<'ctx>(&self, ctx: &'ctx Context) -> IntType<'ctx>;
|
||||||
|
|
||||||
/// Generate function call and returns the function return value.
|
/// Generate function call and returns the function return value.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
cell::OnceCell,
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
@ -19,7 +20,7 @@ use inkwell::{
|
|||||||
module::Module,
|
module::Module,
|
||||||
passes::PassBuilderOptions,
|
passes::PassBuilderOptions,
|
||||||
targets::{CodeModel, RelocMode, Target, TargetMachine, TargetTriple},
|
targets::{CodeModel, RelocMode, Target, TargetMachine, TargetTriple},
|
||||||
types::{AnyType, BasicType, BasicTypeEnum},
|
types::{AnyType, BasicType, BasicTypeEnum, IntType},
|
||||||
values::{BasicValueEnum, FunctionValue, IntValue, PhiValue, PointerValue},
|
values::{BasicValueEnum, FunctionValue, IntValue, PhiValue, PointerValue},
|
||||||
AddressSpace, IntPredicate, OptimizationLevel,
|
AddressSpace, IntPredicate, OptimizationLevel,
|
||||||
};
|
};
|
||||||
@ -226,14 +227,33 @@ pub struct CodeGenContext<'ctx, 'a> {
|
|||||||
|
|
||||||
/// The current source location.
|
/// The current source location.
|
||||||
pub current_loc: Location,
|
pub current_loc: Location,
|
||||||
|
|
||||||
|
/// The cached type of `size_t`.
|
||||||
|
llvm_usize: OnceCell<IntType<'ctx>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeGenContext<'_, '_> {
|
impl<'ctx> CodeGenContext<'ctx, '_> {
|
||||||
/// Whether the [current basic block][Builder::get_insert_block] referenced by `builder`
|
/// Whether the [current basic block][Builder::get_insert_block] referenced by `builder`
|
||||||
/// contains a [terminator statement][BasicBlock::get_terminator].
|
/// contains a [terminator statement][BasicBlock::get_terminator].
|
||||||
pub fn is_terminated(&self) -> bool {
|
pub fn is_terminated(&self) -> bool {
|
||||||
self.builder.get_insert_block().and_then(BasicBlock::get_terminator).is_some()
|
self.builder.get_insert_block().and_then(BasicBlock::get_terminator).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a [`IntType`] representing `size_t` for the compilation target as specified by
|
||||||
|
/// [`self.registry`][WorkerRegistry].
|
||||||
|
pub fn get_size_type(&self) -> IntType<'ctx> {
|
||||||
|
*self.llvm_usize.get_or_init(|| {
|
||||||
|
self.ctx.ptr_sized_int_type(
|
||||||
|
&self
|
||||||
|
.registry
|
||||||
|
.llvm_options
|
||||||
|
.create_target_machine()
|
||||||
|
.map(|tm| tm.get_target_data())
|
||||||
|
.unwrap(),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Fp = Box<dyn Fn(&Module) + Send + Sync>;
|
type Fp = Box<dyn Fn(&Module) + Send + Sync>;
|
||||||
@ -987,6 +1007,7 @@ pub fn gen_func_impl<
|
|||||||
need_sret: has_sret,
|
need_sret: has_sret,
|
||||||
current_loc: Location::default(),
|
current_loc: Location::default(),
|
||||||
debug_info: (dibuilder, compile_unit, func_scope.as_debug_info_scope()),
|
debug_info: (dibuilder, compile_unit, func_scope.as_debug_info_scope()),
|
||||||
|
llvm_usize: OnceCell::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let target_llvm_usize = context.ptr_sized_int_type(
|
let target_llvm_usize = context.ptr_sized_int_type(
|
||||||
|
Loading…
Reference in New Issue
Block a user