core: Add ArrayLikeValue

For exposing LLVM values that can be accessed like an array.
This commit is contained in:
David Mak 2024-03-19 00:20:09 +08:00
parent 2c4bf3ce59
commit 393ef6098e
6 changed files with 646 additions and 303 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,14 @@ use std::{collections::HashMap, convert::TryInto, iter::once, iter::zip};
use crate::{ use crate::{
codegen::{ codegen::{
classes::{ListValue, NDArrayValue, RangeValue}, classes::{
ArrayLikeIndexer,
ArrayLikeValue,
ListValue,
NDArrayValue,
RangeValue,
UntypedArrayLikeAccessor,
},
concrete_type::{ConcreteFuncArg, ConcreteTypeEnum, ConcreteTypeStore}, concrete_type::{ConcreteFuncArg, ConcreteTypeEnum, ConcreteTypeStore},
gen_in_range_check, gen_in_range_check,
get_llvm_type, get_llvm_type,
@ -982,7 +989,7 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
list_alloc_size.into_int_value(), list_alloc_size.into_int_value(),
Some("listcomp.addr") Some("listcomp.addr")
); );
list_content = list.data().as_ptr_value(ctx); list_content = list.data().base_ptr(ctx, generator);
let i = generator.gen_store_target(ctx, target, Some("i.addr"))?.unwrap(); let i = generator.gen_store_target(ctx, target, Some("i.addr"))?.unwrap();
ctx.builder ctx.builder
@ -1015,7 +1022,7 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
) )
.into_int_value(); .into_int_value();
list = allocate_list(generator, ctx, elem_ty, length, Some("listcomp")); list = allocate_list(generator, ctx, elem_ty, length, Some("listcomp"));
list_content = list.data().as_ptr_value(ctx); list_content = list.data().base_ptr(ctx, generator);
let counter = generator.gen_var_alloc(ctx, size_t.into(), Some("counter.addr"))?; let counter = generator.gen_var_alloc(ctx, size_t.into(), Some("counter.addr"))?;
// counter = -1 // counter = -1
ctx.builder.build_store(counter, size_t.const_int(u64::MAX, true)).unwrap(); ctx.builder.build_store(counter, size_t.const_int(u64::MAX, true)).unwrap();
@ -1260,7 +1267,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
}; };
Ok(Some(v.data() Ok(Some(v.data()
.get_const( .get(
ctx, ctx,
generator, generator,
ctx.ctx.i32_type().const_array(&[index]), ctx.ctx.i32_type().const_array(&[index]),
@ -1307,13 +1314,14 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
let v_dims_src_ptr = unsafe { let v_dims_src_ptr = unsafe {
v.dim_sizes().ptr_offset_unchecked( v.dim_sizes().ptr_offset_unchecked(
ctx, ctx,
generator,
llvm_usize.const_int(1, false), llvm_usize.const_int(1, false),
None, None,
) )
}; };
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
ndarray.dim_sizes().as_ptr_value(ctx), ndarray.dim_sizes().base_ptr(ctx, generator),
v_dims_src_ptr, v_dims_src_ptr,
ctx.builder ctx.builder
.build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "") .build_int_mul(ndarray_num_dims, llvm_usize.size_of(), "")
@ -1325,12 +1333,11 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
ndarray.load_ndims(ctx), &ndarray.dim_sizes().as_slice_value(ctx, generator),
ndarray.dim_sizes().as_ptr_value(ctx),
); );
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems); ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
let v_data_src_ptr = v.data().ptr_offset_const( let v_data_src_ptr = v.data().ptr_offset(
ctx, ctx,
generator, generator,
ctx.ctx.i32_type().const_array(&[index]), ctx.ctx.i32_type().const_array(&[index]),
@ -1338,7 +1345,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
); );
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
ndarray.data().as_ptr_value(ctx), ndarray.data().base_ptr(ctx, generator),
v_data_src_ptr, v_data_src_ptr,
ctx.builder ctx.builder
.build_int_mul(ndarray_num_elems, llvm_ndarray_data_t.size_of().unwrap(), "") .build_int_mul(ndarray_num_elems, llvm_ndarray_data_t.size_of().unwrap(), "")

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
codegen::{expr::*, stmt::*, bool_to_i1, bool_to_i8, CodeGenContext}, codegen::{classes::ArraySliceValue, expr::*, stmt::*, bool_to_i1, bool_to_i8, CodeGenContext},
symbol_resolver::ValueEnum, symbol_resolver::ValueEnum,
toplevel::{DefinitionId, TopLevelDef}, toplevel::{DefinitionId, TopLevelDef},
typecheck::typedef::{FunSignature, Type}, typecheck::typedef::{FunSignature, Type},
@ -99,8 +99,8 @@ pub trait CodeGenerator {
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
ty: BasicTypeEnum<'ctx>, ty: BasicTypeEnum<'ctx>,
size: IntValue<'ctx>, size: IntValue<'ctx>,
name: Option<&str>, name: Option<&'ctx str>,
) -> Result<PointerValue<'ctx>, String> { ) -> Result<ArraySliceValue<'ctx>, String> {
gen_array_var(ctx, ty, size, name) gen_array_var(ctx, ty, size, name)
} }

View File

@ -1,7 +1,7 @@
use crate::typecheck::typedef::Type; use crate::typecheck::typedef::Type;
use super::{ use super::{
classes::{ListValue, NDArrayValue}, classes::{ArrayLikeIndexer, ArrayLikeValue, ListValue, NDArrayValue, UntypedArrayLikeMutator},
CodeGenContext, CodeGenContext,
CodeGenerator, CodeGenerator,
}; };
@ -338,7 +338,7 @@ pub fn list_slice_assignment<'ctx, G: CodeGenerator + ?Sized>(
let zero = int32.const_zero(); let zero = int32.const_zero();
let one = int32.const_int(1, false); let one = int32.const_int(1, false);
let dest_arr_ptr = dest_arr.data().as_ptr_value(ctx); let dest_arr_ptr = dest_arr.data().base_ptr(ctx, generator);
let dest_arr_ptr = ctx.builder.build_pointer_cast( let dest_arr_ptr = ctx.builder.build_pointer_cast(
dest_arr_ptr, dest_arr_ptr,
elem_ptr_type, elem_ptr_type,
@ -346,7 +346,7 @@ pub fn list_slice_assignment<'ctx, G: CodeGenerator + ?Sized>(
).unwrap(); ).unwrap();
let dest_len = dest_arr.load_size(ctx, Some("dest.len")); let dest_len = dest_arr.load_size(ctx, Some("dest.len"));
let dest_len = ctx.builder.build_int_truncate_or_bit_cast(dest_len, int32, "srclen32").unwrap(); let dest_len = ctx.builder.build_int_truncate_or_bit_cast(dest_len, int32, "srclen32").unwrap();
let src_arr_ptr = src_arr.data().as_ptr_value(ctx); let src_arr_ptr = src_arr.data().base_ptr(ctx, generator);
let src_arr_ptr = ctx.builder.build_pointer_cast( let src_arr_ptr = ctx.builder.build_pointer_cast(
src_arr_ptr, src_arr_ptr,
elem_ptr_type, elem_ptr_type,
@ -574,12 +574,14 @@ 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, G: CodeGenerator + ?Sized>( pub fn call_ndarray_calc_size<'ctx, G, Dims>(
generator: &G, generator: &G,
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
num_dims: IntValue<'ctx>, dims: &Dims,
dims: PointerValue<'ctx>, ) -> IntValue<'ctx>
) -> IntValue<'ctx> { where
G: CodeGenerator + ?Sized,
Dims: ArrayLikeIndexer<'ctx>, {
let llvm_i64 = ctx.ctx.i64_type(); let llvm_i64 = ctx.ctx.i64_type();
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
@ -606,8 +608,8 @@ pub fn call_ndarray_calc_size<'ctx, G: CodeGenerator + ?Sized>(
.build_call( .build_call(
ndarray_calc_size_fn, ndarray_calc_size_fn,
&[ &[
dims.into(), dims.base_ptr(ctx, generator).into(),
num_dims.into(), dims.size(ctx, generator).into(),
], ],
"", "",
) )
@ -666,7 +668,7 @@ pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
ndarray_calc_nd_indices_fn, ndarray_calc_nd_indices_fn,
&[ &[
index.into(), index.into(),
ndarray_dims.as_ptr_value(ctx).into(), ndarray_dims.base_ptr(ctx, generator).into(),
ndarray_num_dims.into(), ndarray_num_dims.into(),
indices.into(), indices.into(),
], ],
@ -677,13 +679,15 @@ pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
indices indices
} }
fn call_ndarray_flatten_index_impl<'ctx, G: CodeGenerator + ?Sized>( fn call_ndarray_flatten_index_impl<'ctx, G, Indices>(
generator: &G, generator: &G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
indices: PointerValue<'ctx>, indices: &Indices,
indices_size: IntValue<'ctx>, ) -> IntValue<'ctx>
) -> IntValue<'ctx> { where
G: CodeGenerator + ?Sized,
Indices: ArrayLikeIndexer<'ctx>, {
let llvm_i32 = ctx.ctx.i32_type(); let llvm_i32 = ctx.ctx.i32_type();
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
@ -691,14 +695,14 @@ fn call_ndarray_flatten_index_impl<'ctx, G: CodeGenerator + ?Sized>(
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default()); let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
debug_assert_eq!( debug_assert_eq!(
IntType::try_from(indices.get_type().get_element_type()) IntType::try_from(indices.element_type(ctx, generator))
.map(IntType::get_bit_width) .map(IntType::get_bit_width)
.unwrap_or_default(), .unwrap_or_default(),
llvm_i32.get_bit_width(), llvm_i32.get_bit_width(),
"Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`" "Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`"
); );
debug_assert_eq!( debug_assert_eq!(
indices_size.get_type().get_bit_width(), indices.size(ctx, generator).get_type().get_bit_width(),
llvm_usize.get_bit_width(), llvm_usize.get_bit_width(),
"Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`" "Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`"
); );
@ -729,10 +733,10 @@ fn call_ndarray_flatten_index_impl<'ctx, G: CodeGenerator + ?Sized>(
.build_call( .build_call(
ndarray_flatten_index_fn, ndarray_flatten_index_fn,
&[ &[
ndarray_dims.as_ptr_value(ctx).into(), ndarray_dims.base_ptr(ctx, generator).into(),
ndarray_num_dims.into(), ndarray_num_dims.into(),
indices.into(), indices.base_ptr(ctx, generator).into(),
indices_size.into(), indices.size(ctx, generator).into(),
], ],
"", "",
) )
@ -750,21 +754,21 @@ fn call_ndarray_flatten_index_impl<'ctx, G: CodeGenerator + ?Sized>(
/// * `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, G: CodeGenerator + ?Sized>( pub fn call_ndarray_flatten_index<'ctx, G, Index>(
generator: &G, generator: &mut G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
ndarray: NDArrayValue<'ctx>, ndarray: NDArrayValue<'ctx>,
indices: ListValue<'ctx>, indices: &Index,
) -> IntValue<'ctx> { ) -> IntValue<'ctx>
let indices_size = indices.load_size(ctx, None); where
let indices_data = indices.data(); G: CodeGenerator + ?Sized,
Index: ArrayLikeIndexer<'ctx>, {
call_ndarray_flatten_index_impl( call_ndarray_flatten_index_impl(
generator, generator,
ctx, ctx,
ndarray, ndarray,
indices_data.as_ptr_value(ctx), indices,
indices_size,
) )
} }
/// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the /// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the
@ -786,27 +790,27 @@ pub fn call_ndarray_flatten_index_const<'ctx, G: CodeGenerator + ?Sized>(
ctx, ctx,
indices.get_type().get_element_type(), indices.get_type().get_element_type(),
llvm_usize.const_int(indices_size as u64, false), llvm_usize.const_int(indices_size as u64, false),
None None,
).unwrap(); ).unwrap();
for i in 0..indices_size { for i in 0..indices_size {
let v = ctx.builder.build_extract_value(indices, i, "") let v = ctx.builder.build_extract_value(indices, i, "")
.unwrap() .unwrap()
.into_int_value(); .into_int_value();
let elem_ptr = unsafe {
ctx.builder.build_in_bounds_gep( unsafe {
indices_alloca, indices_alloca.set_unchecked(
&[ctx.ctx.i32_type().const_int(i as u64, false)], ctx,
"" generator,
) ctx.ctx.i32_type().const_int(i as u64, false),
}.unwrap(); v.into(),
ctx.builder.build_store(elem_ptr, v).unwrap(); );
}
} }
call_ndarray_flatten_index_impl( call_ndarray_flatten_index_impl(
generator, generator,
ctx, ctx,
ndarray, ndarray,
indices_alloca, &indices_alloca,
llvm_usize.const_int(indices_size as u64, false),
) )
} }

View File

@ -6,7 +6,14 @@ use inkwell::{
use nac3parser::ast::StrRef; use nac3parser::ast::StrRef;
use crate::{ use crate::{
codegen::{ codegen::{
classes::{ListValue, NDArrayValue}, classes::{
ArrayLikeIndexer,
ArrayLikeValue,
ListValue,
NDArrayValue,
TypedArrayLikeAccessor,
UntypedArrayLikeAccessor,
},
CodeGenContext, CodeGenContext,
CodeGenerator, CodeGenerator,
irrt::{ irrt::{
@ -109,7 +116,7 @@ fn create_ndarray_dyn_shape<'ctx, 'a, G, V, LenFn, DataFn>(
.unwrap(); .unwrap();
let ndarray_pdim = unsafe { let ndarray_pdim = unsafe {
ndarray.dim_sizes().ptr_offset_unchecked(ctx, i, None) ndarray.dim_sizes().ptr_offset_unchecked(ctx, generator, i, None)
}; };
ctx.builder.build_store(ndarray_pdim, shape_dim).unwrap(); ctx.builder.build_store(ndarray_pdim, shape_dim).unwrap();
@ -122,8 +129,7 @@ fn create_ndarray_dyn_shape<'ctx, 'a, G, V, LenFn, DataFn>(
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
ndarray.load_ndims(ctx), &ndarray.dim_sizes().as_slice_value(ctx, generator),
ndarray.dim_sizes().as_ptr_value(ctx),
); );
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems); ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
@ -193,12 +199,10 @@ fn create_ndarray_const_shape<'ctx, G: CodeGenerator + ?Sized>(
ctx.builder.build_store(ndarray_dim, shape_dim).unwrap(); ctx.builder.build_store(ndarray_dim, shape_dim).unwrap();
} }
let ndarray_dims = ndarray.dim_sizes().as_ptr_value(ctx);
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
ndarray.load_ndims(ctx), &ndarray.dim_sizes().as_slice_value(ctx, generator),
ndarray_dims,
); );
ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems); ndarray.create_data(ctx, llvm_ndarray_data_t, ndarray_num_elems);
@ -288,8 +292,7 @@ fn ndarray_fill_flattened<'ctx, 'a, G, ValueFn>(
let ndarray_num_elems = call_ndarray_calc_size( let ndarray_num_elems = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
ndarray.load_ndims(ctx), &ndarray.dim_sizes().as_slice_value(ctx, generator),
ndarray.dim_sizes().as_ptr_value(ctx),
); );
gen_for_callback_incrementing( gen_for_callback_incrementing(
@ -299,7 +302,7 @@ fn ndarray_fill_flattened<'ctx, 'a, G, ValueFn>(
(ndarray_num_elems, false), (ndarray_num_elems, false),
|generator, ctx, i| { |generator, ctx, i| {
let elem = unsafe { let elem = unsafe {
ndarray.data().ptr_to_data_flattened_unchecked(ctx, i, None) ndarray.data().ptr_offset_unchecked(ctx, generator, i, None)
}; };
let value = value_fn(generator, ctx, i)?; let value = value_fn(generator, ctx, i)?;
@ -548,16 +551,15 @@ fn ndarray_copy_impl<'ctx, G: CodeGenerator + ?Sized>(
|_, ctx, shape| { |_, ctx, shape| {
Ok(shape.load_ndims(ctx)) Ok(shape.load_ndims(ctx))
}, },
|_, ctx, shape, idx| { |generator, ctx, shape, idx| {
unsafe { Ok(shape.dim_sizes().get_unchecked(ctx, idx, None)) } unsafe { Ok(shape.dim_sizes().get_typed_unchecked(ctx, generator, idx, None)) }
}, },
)?; )?;
let len = call_ndarray_calc_size( let len = call_ndarray_calc_size(
generator, generator,
ctx, ctx,
ndarray.load_ndims(ctx), &ndarray.dim_sizes().as_slice_value(ctx, generator),
ndarray.dim_sizes().as_ptr_value(ctx),
); );
let sizeof_ty = ctx.get_llvm_type(generator, elem_ty); let sizeof_ty = ctx.get_llvm_type(generator, elem_ty);
let len_bytes = ctx.builder let len_bytes = ctx.builder
@ -570,8 +572,8 @@ fn ndarray_copy_impl<'ctx, G: CodeGenerator + ?Sized>(
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
ndarray.data().as_ptr_value(ctx), ndarray.data().base_ptr(ctx, generator),
this.data().as_ptr_value(ctx), this.data().base_ptr(ctx, generator),
len_bytes, len_bytes,
llvm_i1.const_zero(), llvm_i1.const_zero(),
); );

View File

@ -6,7 +6,7 @@ use super::{
}; };
use crate::{ use crate::{
codegen::{ codegen::{
classes::{ListValue, RangeValue}, classes::{ArrayLikeIndexer, ArraySliceValue, ListValue, RangeValue},
expr::gen_binop_expr, expr::gen_binop_expr,
gen_in_range_check, gen_in_range_check,
}, },
@ -65,8 +65,8 @@ pub fn gen_array_var<'ctx, 'a, T: BasicType<'ctx>>(
ctx: &mut CodeGenContext<'ctx, 'a>, ctx: &mut CodeGenContext<'ctx, 'a>,
ty: T, ty: T,
size: IntValue<'ctx>, size: IntValue<'ctx>,
name: Option<&str>, name: Option<&'ctx str>,
) -> Result<PointerValue<'ctx>, String> { ) -> Result<ArraySliceValue<'ctx>, String> {
// Restore debug location // Restore debug location
let di_loc = ctx.debug_info.0.create_debug_location( let di_loc = ctx.debug_info.0.create_debug_location(
ctx.ctx, ctx.ctx,
@ -84,6 +84,7 @@ pub fn gen_array_var<'ctx, 'a, T: BasicType<'ctx>>(
ctx.builder.set_current_debug_location(di_loc); ctx.builder.set_current_debug_location(di_loc);
let ptr = ctx.builder.build_array_alloca(ty, size, name.unwrap_or("")).unwrap(); let ptr = ctx.builder.build_array_alloca(ty, size, name.unwrap_or("")).unwrap();
let ptr = ArraySliceValue::from_ptr_val(ptr, size, name);
ctx.builder.position_at_end(current); ctx.builder.position_at_end(current);
ctx.builder.set_current_debug_location(di_loc); ctx.builder.set_current_debug_location(di_loc);