core: Allow unsized CodeGenerator to be passed to some codegen functions

Enables codegen_callback to call these codegen functions as well.
This commit is contained in:
David Mak 2024-03-19 18:24:30 +08:00
parent e980f19c93
commit 2c4bf3ce59
6 changed files with 126 additions and 117 deletions

View File

@ -125,10 +125,10 @@ impl<'ctx> ListValue<'ctx> {
} }
/// Stores the `size` of this `list` into this instance. /// Stores the `size` of this `list` into this instance.
pub fn store_size( pub fn store_size<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
generator: &dyn CodeGenerator, generator: &G,
size: IntValue<'ctx>, size: IntValue<'ctx>,
) { ) {
debug_assert_eq!(size.get_type(), generator.get_size_type(ctx.ctx)); debug_assert_eq!(size.get_type(), generator.get_size_type(ctx.ctx));
@ -192,10 +192,10 @@ impl<'ctx> ListDataProxy<'ctx> {
} }
/// Returns the pointer to the data at the `idx`-th index. /// Returns the pointer to the data at the `idx`-th index.
pub fn ptr_offset( pub fn ptr_offset<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
idx: IntValue<'ctx>, idx: IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -235,10 +235,10 @@ impl<'ctx> ListDataProxy<'ctx> {
} }
/// Returns the data at the `idx`-th flattened index. /// Returns the data at the `idx`-th flattened index.
pub fn get( pub fn get<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
idx: IntValue<'ctx>, idx: IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -507,10 +507,10 @@ impl<'ctx> NDArrayValue<'ctx> {
} }
/// Stores the number of dimensions `ndims` into this instance. /// Stores the number of dimensions `ndims` into this instance.
pub fn store_ndims( pub fn store_ndims<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
generator: &dyn CodeGenerator, generator: &G,
ndims: IntValue<'ctx>, ndims: IntValue<'ctx>,
) { ) {
debug_assert_eq!(ndims.get_type(), generator.get_size_type(ctx.ctx)); debug_assert_eq!(ndims.get_type(), generator.get_size_type(ctx.ctx));
@ -642,10 +642,10 @@ impl<'ctx> NDArrayDimsProxy<'ctx> {
} }
/// Returns the pointer to the size of the `idx`-th dimension. /// Returns the pointer to the size of the `idx`-th dimension.
pub fn ptr_offset( pub fn ptr_offset<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
idx: IntValue<'ctx>, idx: IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -685,10 +685,10 @@ impl<'ctx> NDArrayDimsProxy<'ctx> {
} }
/// Returns the size of the `idx`-th dimension. /// Returns the size of the `idx`-th dimension.
pub fn get( pub fn get<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
idx: IntValue<'ctx>, idx: IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
@ -730,10 +730,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
} }
/// Returns the pointer to the data at the `idx`-th flattened index. /// Returns the pointer to the data at the `idx`-th flattened index.
pub fn ptr_to_data_flattened( pub fn ptr_to_data_flattened<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
idx: IntValue<'ctx>, idx: IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -775,10 +775,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
} }
/// Returns the data at the `idx`-th flattened index. /// Returns the data at the `idx`-th flattened index.
pub fn get_flattened( pub fn get_flattened<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
idx: IntValue<'ctx>, idx: IntValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -789,10 +789,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
/// # Safety /// # Safety
/// ///
/// This function should be called with valid indices. /// This function should be called with valid indices.
pub unsafe fn ptr_offset_unchecked( pub unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
generator: &dyn CodeGenerator, generator: &G,
indices: ListValue<'ctx>, indices: ListValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -821,10 +821,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
/// # Safety /// # Safety
/// ///
/// This function should be called with valid indices. /// This function should be called with valid indices.
pub unsafe fn ptr_offset_unchecked_const( pub unsafe fn ptr_offset_unchecked_const<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
indices: ArrayValue<'ctx>, indices: ArrayValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -845,10 +845,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
} }
/// Returns the pointer to the data at the index specified by `indices`. /// Returns the pointer to the data at the index specified by `indices`.
pub fn ptr_offset_const( pub fn ptr_offset_const<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
indices: ArrayValue<'ctx>, indices: ArrayValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -910,10 +910,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
} }
/// Returns the pointer to the data at the index specified by `indices`. /// Returns the pointer to the data at the index specified by `indices`.
pub fn ptr_offset( pub fn ptr_offset<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
indices: ListValue<'ctx>, indices: ListValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> PointerValue<'ctx> { ) -> PointerValue<'ctx> {
@ -979,10 +979,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
/// # Safety /// # Safety
/// ///
/// This function should be called with valid indices. /// This function should be called with valid indices.
pub unsafe fn get_unchecked_const( pub unsafe fn get_unchecked_const<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
indices: ArrayValue<'ctx>, indices: ArrayValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -993,10 +993,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
/// # Safety /// # Safety
/// ///
/// This function should be called with valid indices. /// This function should be called with valid indices.
pub unsafe fn get_unchecked( pub unsafe fn get_unchecked<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &dyn CodeGenerator, generator: &G,
indices: ListValue<'ctx>, indices: ListValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -1005,10 +1005,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
} }
/// Returns the data at the index specified by `indices`. /// Returns the data at the index specified by `indices`.
pub fn get_const( pub fn get_const<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
indices: ArrayValue<'ctx>, indices: ArrayValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -1017,10 +1017,10 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
} }
/// Returns the data at the index specified by `indices`. /// Returns the data at the index specified by `indices`.
pub fn get( pub fn get<G: CodeGenerator + ?Sized>(
&self, &self,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
generator: &mut dyn CodeGenerator, generator: &mut G,
indices: ListValue<'ctx>, indices: ListValue<'ctx>,
name: Option<&str>, name: Option<&str>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {

View File

@ -103,9 +103,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
index index
} }
pub fn gen_symbol_val( pub fn gen_symbol_val<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
val: &SymbolValue, val: &SymbolValue,
ty: Type, ty: Type,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -174,9 +174,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
/// See [`get_llvm_type`]. /// See [`get_llvm_type`].
pub fn get_llvm_type( pub fn get_llvm_type<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
ty: Type, ty: Type,
) -> BasicTypeEnum<'ctx> { ) -> BasicTypeEnum<'ctx> {
get_llvm_type( get_llvm_type(
@ -191,9 +191,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
/// See [`get_llvm_abi_type`]. /// See [`get_llvm_abi_type`].
pub fn get_llvm_abi_type( pub fn get_llvm_abi_type<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
ty: Type, ty: Type,
) -> BasicTypeEnum<'ctx> { ) -> BasicTypeEnum<'ctx> {
get_llvm_abi_type( get_llvm_abi_type(
@ -209,9 +209,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
/// Generates an LLVM variable for a [constant value][value] with a given [type][ty]. /// Generates an LLVM variable for a [constant value][value] with a given [type][ty].
pub fn gen_const( pub fn gen_const<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
value: &Constant, value: &Constant,
ty: Type, ty: Type,
) -> Option<BasicValueEnum<'ctx>> { ) -> Option<BasicValueEnum<'ctx>> {
@ -291,9 +291,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
/// Generates a binary operation `op` between two integral operands `lhs` and `rhs`. /// Generates a binary operation `op` between two integral operands `lhs` and `rhs`.
pub fn gen_int_ops( pub fn gen_int_ops<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
op: &Operator, op: &Operator,
lhs: BasicValueEnum<'ctx>, lhs: BasicValueEnum<'ctx>,
rhs: BasicValueEnum<'ctx>, rhs: BasicValueEnum<'ctx>,
@ -492,17 +492,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
/// Helper function for generating a LLVM variable storing a [String]. /// Helper function for generating a LLVM variable storing a [String].
pub fn gen_string<S: Into<String>>( pub fn gen_string<G, S>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
s: S, s: S,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx>
where
G: CodeGenerator + ?Sized,
S: Into<String>,
{
self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str).unwrap() self.gen_const(generator, &Constant::Str(s.into()), self.primitives.str).unwrap()
} }
pub fn raise_exn( pub fn raise_exn<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
name: &str, name: &str,
msg: BasicValueEnum<'ctx>, msg: BasicValueEnum<'ctx>,
params: [Option<IntValue<'ctx>>; 3], params: [Option<IntValue<'ctx>>; 3],
@ -546,9 +550,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
gen_raise(generator, self, Some(&zelf.into()), loc); gen_raise(generator, self, Some(&zelf.into()), loc);
} }
pub fn make_assert( pub fn make_assert<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
cond: IntValue<'ctx>, cond: IntValue<'ctx>,
err_name: &str, err_name: &str,
err_msg: &str, err_msg: &str,
@ -559,9 +563,9 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
self.make_assert_impl(generator, cond, err_name, err_msg, params, loc); self.make_assert_impl(generator, cond, err_name, err_msg, params, loc);
} }
pub fn make_assert_impl( pub fn make_assert_impl<G: CodeGenerator + ?Sized>(
&mut self, &mut self,
generator: &mut dyn CodeGenerator, generator: &mut G,
cond: IntValue<'ctx>, cond: IntValue<'ctx>,
err_name: &str, err_name: &str,
err_msg: BasicValueEnum<'ctx>, err_msg: BasicValueEnum<'ctx>,
@ -878,7 +882,7 @@ pub fn destructure_range<'ctx>(
/// Returns an instance of [`PointerValue`] pointing to the List structure. The List structure is /// Returns an instance of [`PointerValue`] pointing to the List structure. The List structure is
/// defined as `type { ty*, size_t }` in LLVM, where the first element stores the pointer to the /// defined as `type { ty*, size_t }` in LLVM, where the first element stores the pointer to the
/// data, and the second element stores the size of the List. /// data, and the second element stores the size of the List.
pub fn allocate_list<'ctx, G: CodeGenerator>( pub fn allocate_list<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
ty: BasicTypeEnum<'ctx>, ty: BasicTypeEnum<'ctx>,

View File

@ -39,8 +39,8 @@ pub fn load_irrt(ctx: &Context) -> Module {
// repeated squaring method adapted from GNU Scientific Library: // repeated squaring method adapted from GNU Scientific Library:
// https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c // https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
pub fn integer_power<'ctx>( pub fn integer_power<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
base: IntValue<'ctx>, base: IntValue<'ctx>,
exp: IntValue<'ctx>, exp: IntValue<'ctx>,
@ -81,8 +81,8 @@ pub fn integer_power<'ctx>(
.unwrap() .unwrap()
} }
pub fn calculate_len_for_slice_range<'ctx>( pub fn calculate_len_for_slice_range<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
start: IntValue<'ctx>, start: IntValue<'ctx>,
end: IntValue<'ctx>, end: IntValue<'ctx>,
@ -303,8 +303,8 @@ pub fn handle_slice_index_bound<'ctx, G: CodeGenerator>(
/// This function handles 'end' **inclusively**. /// This function handles 'end' **inclusively**.
/// Order of tuples `assign_idx` and `value_idx` is ('start', 'end', 'step'). /// Order of tuples `assign_idx` and `value_idx` is ('start', 'end', 'step').
/// Negative index should be handled before entering this function /// Negative index should be handled before entering this function
pub fn list_slice_assignment<'ctx>( pub fn list_slice_assignment<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
ty: BasicTypeEnum<'ctx>, ty: BasicTypeEnum<'ctx>,
dest_arr: ListValue<'ctx>, dest_arr: ListValue<'ctx>,
@ -468,8 +468,8 @@ pub fn list_slice_assignment<'ctx>(
} }
/// Generates a call to `isinf` in IR. Returns an `i1` representing the result. /// Generates a call to `isinf` in IR. Returns an `i1` representing the result.
pub fn call_isinf<'ctx>( pub fn call_isinf<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
v: FloatValue<'ctx>, v: FloatValue<'ctx>,
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
@ -489,8 +489,8 @@ pub fn call_isinf<'ctx>(
} }
/// Generates a call to `isnan` in IR. Returns an `i1` representing the result. /// Generates a call to `isnan` in IR. Returns an `i1` representing the result.
pub fn call_isnan<'ctx>( pub fn call_isnan<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
v: FloatValue<'ctx>, v: FloatValue<'ctx>,
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
@ -574,8 +574,8 @@ pub fn call_j0<'ctx>(
/// ///
/// * `num_dims` - An [`IntValue`] containing the number of dimensions. /// * `num_dims` - An [`IntValue`] containing the number of dimensions.
/// * `dims` - A [`PointerValue`] to an array containing the size of each dimension. /// * `dims` - A [`PointerValue`] to an array containing the size of each dimension.
pub fn call_ndarray_calc_size<'ctx>( pub fn call_ndarray_calc_size<'ctx, G: CodeGenerator + ?Sized>(
generator: &dyn CodeGenerator, generator: &G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
num_dims: IntValue<'ctx>, num_dims: IntValue<'ctx>,
dims: PointerValue<'ctx>, dims: PointerValue<'ctx>,
@ -622,8 +622,8 @@ pub fn call_ndarray_calc_size<'ctx>(
/// * `index` - The index to compute the multidimensional index for. /// * `index` - The index to compute the multidimensional index for.
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
/// `NDArray`. /// `NDArray`.
pub fn call_ndarray_calc_nd_indices<'ctx>( pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
generator: &dyn CodeGenerator, generator: &G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
index: IntValue<'ctx>, index: IntValue<'ctx>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
@ -677,8 +677,8 @@ pub fn call_ndarray_calc_nd_indices<'ctx>(
indices indices
} }
fn call_ndarray_flatten_index_impl<'ctx>( fn call_ndarray_flatten_index_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &dyn CodeGenerator, generator: &G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
indices: PointerValue<'ctx>, indices: PointerValue<'ctx>,
@ -750,8 +750,8 @@ fn call_ndarray_flatten_index_impl<'ctx>(
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
/// `NDArray`. /// `NDArray`.
/// * `indices` - The multidimensional index to compute the flattened index for. /// * `indices` - The multidimensional index to compute the flattened index for.
pub fn call_ndarray_flatten_index<'ctx>( pub fn call_ndarray_flatten_index<'ctx, G: CodeGenerator + ?Sized>(
generator: &dyn CodeGenerator, generator: &G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
indices: ListValue<'ctx>, indices: ListValue<'ctx>,
@ -773,8 +773,8 @@ pub fn call_ndarray_flatten_index<'ctx>(
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an /// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
/// `NDArray`. /// `NDArray`.
/// * `indices` - The multidimensional index to compute the flattened index for. /// * `indices` - The multidimensional index to compute the flattened index for.
pub fn call_ndarray_flatten_index_const<'ctx>( pub fn call_ndarray_flatten_index_const<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
indices: ArrayValue<'ctx>, indices: ArrayValue<'ctx>,

View File

@ -416,10 +416,10 @@ pub struct CodeGenTask {
/// This function is used to obtain the in-memory representation of `ty`, e.g. a `bool` variable /// This function is used to obtain the in-memory representation of `ty`, e.g. a `bool` variable
/// would be represented by an `i8`. /// would be represented by an `i8`.
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn get_llvm_type<'ctx>( fn get_llvm_type<'ctx, G: CodeGenerator + ?Sized>(
ctx: &'ctx Context, ctx: &'ctx Context,
module: &Module<'ctx>, module: &Module<'ctx>,
generator: &mut dyn CodeGenerator, generator: &mut G,
unifier: &mut Unifier, unifier: &mut Unifier,
top_level: &TopLevelContext, top_level: &TopLevelContext,
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>, type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,
@ -554,10 +554,10 @@ fn get_llvm_type<'ctx>(
/// be byte-aligned for the variable to be addressable in memory, whereas there is no such /// be byte-aligned for the variable to be addressable in memory, whereas there is no such
/// restriction for ABI representations. /// restriction for ABI representations.
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn get_llvm_abi_type<'ctx>( fn get_llvm_abi_type<'ctx, G: CodeGenerator + ?Sized>(
ctx: &'ctx Context, ctx: &'ctx Context,
module: &Module<'ctx>, module: &Module<'ctx>,
generator: &mut dyn CodeGenerator, generator: &mut G,
unifier: &mut Unifier, unifier: &mut Unifier,
top_level: &TopLevelContext, top_level: &TopLevelContext,
type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>, type_cache: &mut HashMap<Type, BasicTypeEnum<'ctx>>,

View File

@ -30,8 +30,8 @@ use crate::{
/// * `shape` - The shape of the `NDArray`. /// * `shape` - The shape of the `NDArray`.
/// * `shape_len_fn` - A function that retrieves the number of dimensions from `shape`. /// * `shape_len_fn` - A function that retrieves the number of dimensions from `shape`.
/// * `shape_data_fn` - A function that retrieves the size of a dimension from `shape`. /// * `shape_data_fn` - A function that retrieves the size of a dimension from `shape`.
fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>( fn create_ndarray_dyn_shape<'ctx, 'a, G, V, LenFn, DataFn>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
elem_ty: Type, elem_ty: Type,
shape: &V, shape: &V,
@ -39,8 +39,9 @@ fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>(
shape_data_fn: DataFn, shape_data_fn: DataFn,
) -> Result<NDArrayValue<'ctx>, String> ) -> Result<NDArrayValue<'ctx>, String>
where where
LenFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, &V) -> Result<IntValue<'ctx>, String>, G: CodeGenerator + ?Sized,
DataFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, &V, IntValue<'ctx>) -> Result<IntValue<'ctx>, String>, LenFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, &V) -> Result<IntValue<'ctx>, String>,
DataFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, &V, IntValue<'ctx>) -> Result<IntValue<'ctx>, String>,
{ {
let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None); let ndarray_ty = make_ndarray_ty(&mut ctx.unifier, &ctx.primitives, Some(elem_ty), None);
@ -133,8 +134,8 @@ fn create_ndarray_dyn_shape<'ctx, 'a, V, LenFn, DataFn>(
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
/// * `shape` - The shape of the `NDArray`, represented as an LLVM [`ArrayValue`]. /// * `shape` - The shape of the `NDArray`, represented as an LLVM [`ArrayValue`].
fn create_ndarray_const_shape<'ctx>( fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
shape: ArrayValue<'ctx> shape: ArrayValue<'ctx>
@ -204,8 +205,8 @@ fn create_ndarray_const_shape<'ctx>(
Ok(ndarray) Ok(ndarray)
} }
fn ndarray_zero_value<'ctx>( fn ndarray_zero_value<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -224,8 +225,8 @@ fn ndarray_zero_value<'ctx>(
} }
} }
fn ndarray_one_value<'ctx>( fn ndarray_one_value<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
@ -250,8 +251,8 @@ fn ndarray_one_value<'ctx>(
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
/// * `shape` - The `shape` parameter used to construct the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`.
fn call_ndarray_empty_impl<'ctx>( fn call_ndarray_empty_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
shape: ListValue<'ctx>, shape: ListValue<'ctx>,
@ -272,14 +273,15 @@ fn call_ndarray_empty_impl<'ctx>(
/// Generates LLVM IR for populating the entire `NDArray` using a lambda with its flattened index as /// Generates LLVM IR for populating the entire `NDArray` using a lambda with its flattened index as
/// its input. /// its input.
fn ndarray_fill_flattened<'ctx, 'a, ValueFn>( fn ndarray_fill_flattened<'ctx, 'a, G, ValueFn>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
value_fn: ValueFn, value_fn: ValueFn,
) -> Result<(), String> ) -> Result<(), String>
where where
ValueFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>, G: CodeGenerator + ?Sized,
ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>,
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
@ -311,14 +313,15 @@ fn ndarray_fill_flattened<'ctx, 'a, ValueFn>(
/// Generates LLVM IR for populating the entire `NDArray` using a lambda with the dimension-indices /// Generates LLVM IR for populating the entire `NDArray` using a lambda with the dimension-indices
/// as its input. /// as its input.
fn ndarray_fill_indexed<'ctx, ValueFn>( fn ndarray_fill_indexed<'ctx, G, ValueFn>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
value_fn: ValueFn, value_fn: ValueFn,
) -> Result<(), String> ) -> Result<(), String>
where where
ValueFn: Fn(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, '_>, PointerValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>, G: CodeGenerator + ?Sized,
ValueFn: Fn(&mut G, &mut CodeGenContext<'ctx, '_>, PointerValue<'ctx>) -> Result<BasicValueEnum<'ctx>, String>,
{ {
ndarray_fill_flattened( ndarray_fill_flattened(
generator, generator,
@ -341,8 +344,8 @@ fn ndarray_fill_indexed<'ctx, ValueFn>(
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
/// * `shape` - The `shape` parameter used to construct the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`.
fn call_ndarray_zeros_impl<'ctx>( fn call_ndarray_zeros_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
shape: ListValue<'ctx>, shape: ListValue<'ctx>,
@ -377,8 +380,8 @@ fn call_ndarray_zeros_impl<'ctx>(
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
/// * `shape` - The `shape` parameter used to construct the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`.
fn call_ndarray_ones_impl<'ctx>( fn call_ndarray_ones_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
shape: ListValue<'ctx>, shape: ListValue<'ctx>,
@ -413,8 +416,8 @@ fn call_ndarray_ones_impl<'ctx>(
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
/// * `shape` - The `shape` parameter used to construct the `NDArray`. /// * `shape` - The `shape` parameter used to construct the `NDArray`.
fn call_ndarray_full_impl<'ctx>( fn call_ndarray_full_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
shape: ListValue<'ctx>, shape: ListValue<'ctx>,
@ -456,8 +459,8 @@ fn call_ndarray_full_impl<'ctx>(
/// LLVM-typed implementation for generating the implementation for `ndarray.eye`. /// LLVM-typed implementation for generating the implementation for `ndarray.eye`.
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
fn call_ndarray_eye_impl<'ctx>( fn call_ndarray_eye_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
nrows: IntValue<'ctx>, nrows: IntValue<'ctx>,
@ -529,8 +532,8 @@ fn call_ndarray_eye_impl<'ctx>(
/// LLVM-typed implementation for generating the implementation for `ndarray.copy`. /// LLVM-typed implementation for generating the implementation for `ndarray.copy`.
/// ///
/// * `elem_ty` - The element type of the `NDArray`. /// * `elem_ty` - The element type of the `NDArray`.
fn ndarray_copy_impl<'ctx>( fn ndarray_copy_impl<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
elem_ty: Type, elem_ty: Type,
this: NDArrayValue<'ctx>, this: NDArrayValue<'ctx>,

View File

@ -478,8 +478,8 @@ pub fn gen_for<G: CodeGenerator>(
/// executing. The result value must be an `i1` indicating if the loop should continue. /// executing. The result value must be an `i1` indicating if the loop should continue.
/// * `body` - A lambda containing IR statements within the loop body. /// * `body` - A lambda containing IR statements within the loop body.
/// * `update` - A lambda containing IR statements updating loop variables. /// * `update` - A lambda containing IR statements updating loop variables.
pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>( pub fn gen_for_callback<'ctx, 'a, G, I, InitFn, CondFn, BodyFn, UpdateFn>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
init: InitFn, init: InitFn,
cond: CondFn, cond: CondFn,
@ -487,11 +487,12 @@ pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>(
update: UpdateFn, update: UpdateFn,
) -> Result<(), String> ) -> Result<(), String>
where where
G: CodeGenerator + ?Sized,
I: Clone, I: Clone,
InitFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>) -> Result<I, String>, InitFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>) -> Result<I, String>,
CondFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<IntValue<'ctx>, String>, CondFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<IntValue<'ctx>, String>,
BodyFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>, BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
UpdateFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>, UpdateFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, I) -> Result<(), String>,
{ {
let current = ctx.builder.get_insert_block().and_then(BasicBlock::get_parent).unwrap(); let current = ctx.builder.get_insert_block().and_then(BasicBlock::get_parent).unwrap();
let init_bb = ctx.ctx.append_basic_block(current, "for.init"); let init_bb = ctx.ctx.append_basic_block(current, "for.init");
@ -551,8 +552,8 @@ pub fn gen_for_callback<'ctx, 'a, I, InitFn, CondFn, BodyFn, UpdateFn>(
/// value should be treated as inclusive (as opposed to exclusive). /// value should be treated as inclusive (as opposed to exclusive).
/// * `body` - A lambda containing IR statements within the loop body. /// * `body` - A lambda containing IR statements within the loop body.
/// * `incr_val` - The value to increment the loop variable on each iteration. /// * `incr_val` - The value to increment the loop variable on each iteration.
pub fn gen_for_callback_incrementing<'ctx, 'a, BodyFn>( pub fn gen_for_callback_incrementing<'ctx, 'a, G, BodyFn>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
init_val: IntValue<'ctx>, init_val: IntValue<'ctx>,
max_val: (IntValue<'ctx>, bool), max_val: (IntValue<'ctx>, bool),
@ -560,7 +561,8 @@ pub fn gen_for_callback_incrementing<'ctx, 'a, BodyFn>(
incr_val: IntValue<'ctx>, incr_val: IntValue<'ctx>,
) -> Result<(), String> ) -> Result<(), String>
where where
BodyFn: FnOnce(&mut dyn CodeGenerator, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<(), String>, G: CodeGenerator + ?Sized,
BodyFn: FnOnce(&mut G, &mut CodeGenContext<'ctx, 'a>, IntValue<'ctx>) -> Result<(), String>,
{ {
let init_val_t = init_val.get_type(); let init_val_t = init_val.get_type();
@ -779,8 +781,8 @@ pub fn final_proxy<'ctx>(
/// Inserts the declaration of the builtin function with the specified `symbol` name, and returns /// Inserts the declaration of the builtin function with the specified `symbol` name, and returns
/// the function. /// the function.
pub fn get_builtins<'ctx>( pub fn get_builtins<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
symbol: &str, symbol: &str,
) -> FunctionValue<'ctx> { ) -> FunctionValue<'ctx> {
@ -873,8 +875,8 @@ pub fn exn_constructor<'ctx>(
/// ///
/// * `exception` - The exception thrown by the `raise` statement. /// * `exception` - The exception thrown by the `raise` statement.
/// * `loc` - The location where the exception is raised from. /// * `loc` - The location where the exception is raised from.
pub fn gen_raise<'ctx>( pub fn gen_raise<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut dyn CodeGenerator, generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
exception: Option<&BasicValueEnum<'ctx>>, exception: Option<&BasicValueEnum<'ctx>>,
loc: Location, loc: Location,