forked from M-Labs/nac3
core: Add ArrayLikeValue
For exposing LLVM values that can be accessed like an array.
This commit is contained in:
parent
2c4bf3ce59
commit
f0da9c0283
|
@ -11,6 +11,383 @@ use crate::codegen::{
|
||||||
stmt::gen_for_callback_incrementing,
|
stmt::gen_for_callback_incrementing,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// An LLVM value that is array-like, i.e. it contains a contiguous, sequenced collection of
|
||||||
|
/// elements.
|
||||||
|
pub trait ArrayLikeValue<'ctx> {
|
||||||
|
/// Returns the element type of this array-like value.
|
||||||
|
fn element_type<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> AnyTypeEnum<'ctx>;
|
||||||
|
|
||||||
|
/// Returns the base pointer to the array.
|
||||||
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> PointerValue<'ctx>;
|
||||||
|
|
||||||
|
/// Returns the size of this array-like value.
|
||||||
|
fn size<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> IntValue<'ctx>;
|
||||||
|
|
||||||
|
/// Returns a [`ArraySliceValue`] representing this value.
|
||||||
|
fn as_slice_value<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> ArraySliceValue<'ctx> {
|
||||||
|
ArraySliceValue::from_ptr_val(
|
||||||
|
self.base_ptr(ctx, generator),
|
||||||
|
self.size(ctx, generator),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An array-like value that can be indexed by memory offset.
|
||||||
|
pub trait ArrayLikeIndexer<'ctx, Index = IntValue<'ctx>>: ArrayLikeValue<'ctx> {
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function should be called with a valid index.
|
||||||
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx>;
|
||||||
|
|
||||||
|
/// Returns the pointer to the data at the `idx`-th index.
|
||||||
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An array-like value that can have its array elements accessed as a [`BasicValueEnum`].
|
||||||
|
pub trait UntypedArrayLikeAccessor<'ctx, Index = IntValue<'ctx>>: ArrayLikeIndexer<'ctx, Index> {
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function should be called with a valid index.
|
||||||
|
unsafe fn get_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
let ptr = self.ptr_offset_unchecked(ctx, generator, idx, name);
|
||||||
|
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the data at the `idx`-th index.
|
||||||
|
fn get<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
let ptr = self.ptr_offset(ctx, generator, idx, name);
|
||||||
|
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An array-like value that can have its array elements mutated as a [`BasicValueEnum`].
|
||||||
|
pub trait UntypedArrayLikeMutator<'ctx, Index = IntValue<'ctx>>: ArrayLikeIndexer<'ctx, Index> {
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function should be called with a valid index.
|
||||||
|
unsafe fn set_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
value: BasicValueEnum<'ctx>,
|
||||||
|
) {
|
||||||
|
let ptr = self.ptr_offset_unchecked(ctx, generator, idx, None);
|
||||||
|
ctx.builder.build_store(ptr, value).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the data at the `idx`-th index.
|
||||||
|
fn set<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
value: BasicValueEnum<'ctx>,
|
||||||
|
) {
|
||||||
|
let ptr = self.ptr_offset(ctx, generator, idx, None);
|
||||||
|
ctx.builder.build_store(ptr, value).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An array-like value that can have its array elements accessed as an arbitrary type `T`.
|
||||||
|
pub trait TypedArrayLikeAccessor<'ctx, T, Index = IntValue<'ctx>>: UntypedArrayLikeAccessor<'ctx, Index> {
|
||||||
|
/// Casts an element from [`BasicValueEnum`] into `T`.
|
||||||
|
fn downcast_to_type(&self, ctx: &mut CodeGenContext<'ctx, '_>, value: BasicValueEnum<'ctx>) -> T;
|
||||||
|
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function should be called with a valid index.
|
||||||
|
unsafe fn get_typed_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> T {
|
||||||
|
let value = self.get_unchecked(ctx, generator, idx, name);
|
||||||
|
self.downcast_to_type(ctx, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the data at the `idx`-th index.
|
||||||
|
fn get_typed<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> T {
|
||||||
|
let value = self.get(ctx, generator, idx, name);
|
||||||
|
self.downcast_to_type(ctx, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An array-like value that can have its array elements mutated as an arbitrary type `T`.
|
||||||
|
pub trait TypedArrayLikeMutator<'ctx, T, Index = IntValue<'ctx>>: UntypedArrayLikeMutator<'ctx, Index> {
|
||||||
|
/// Casts an element from T into [`BasicValueEnum`].
|
||||||
|
fn upcast_from_type(&self, ctx: &mut CodeGenContext<'ctx, '_>, value: T) -> BasicValueEnum<'ctx>;
|
||||||
|
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function should be called with a valid index.
|
||||||
|
unsafe fn set_typed_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
value: T,
|
||||||
|
) {
|
||||||
|
let value = self.upcast_from_type(ctx, value);
|
||||||
|
self.set_unchecked(ctx, generator, idx, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the data at the `idx`-th index.
|
||||||
|
fn set_typed<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
value: T,
|
||||||
|
) {
|
||||||
|
let value = self.upcast_from_type(ctx, value);
|
||||||
|
self.set(ctx, generator, idx, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Type alias for a function that casts a [`BasicValueEnum`] into a `T`.
|
||||||
|
type ValueDowncastFn<'ctx, T> = Box<dyn Fn(&mut CodeGenContext<'ctx, '_>, BasicValueEnum<'ctx>) -> T>;
|
||||||
|
/// Type alias for a function that casts a `T` into a [`BasicValueEnum`].
|
||||||
|
type ValueUpcastFn<'ctx, T> = Box<dyn Fn(&mut CodeGenContext<'ctx, '_>, T) -> BasicValueEnum<'ctx>>;
|
||||||
|
|
||||||
|
/// An adapter for constraining untyped array values as typed values.
|
||||||
|
pub struct TypedArrayLikeAdapter<'ctx, T, Adapted: ArrayLikeValue<'ctx> = ArraySliceValue<'ctx>> {
|
||||||
|
adapted: Adapted,
|
||||||
|
downcast_fn: ValueDowncastFn<'ctx, T>,
|
||||||
|
upcast_fn: ValueUpcastFn<'ctx, T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T, Adapted> TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: ArrayLikeValue<'ctx> {
|
||||||
|
/// Creates a [`TypedArrayLikeAdapter`].
|
||||||
|
///
|
||||||
|
/// * `adapted` - The value to be adapted.
|
||||||
|
/// * `downcast_fn` - The function converting a [`BasicValueEnum`] into a `T`.
|
||||||
|
/// * `upcast_fn` - The function converting a T into a [`BasicValueEnum`].
|
||||||
|
pub fn from(
|
||||||
|
adapted: Adapted,
|
||||||
|
downcast_fn: ValueDowncastFn<'ctx, T>,
|
||||||
|
upcast_fn: ValueUpcastFn<'ctx, T>,
|
||||||
|
) -> Self {
|
||||||
|
TypedArrayLikeAdapter { adapted, downcast_fn, upcast_fn }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T, Adapted> ArrayLikeValue<'ctx> for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: ArrayLikeValue<'ctx> {
|
||||||
|
fn element_type<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> AnyTypeEnum<'ctx> {
|
||||||
|
self.adapted.element_type(ctx, generator)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
self.adapted.base_ptr(ctx, generator)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
self.adapted.size(ctx, generator)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T, Index, Adapted> ArrayLikeIndexer<'ctx, Index> for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: ArrayLikeIndexer<'ctx, Index> {
|
||||||
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
self.adapted.ptr_offset_unchecked(ctx, generator, idx, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
self.adapted.ptr_offset(ctx, generator, idx, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T, Index, Adapted> UntypedArrayLikeAccessor<'ctx, Index> for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: UntypedArrayLikeAccessor<'ctx, Index> {}
|
||||||
|
impl<'ctx, T, Index, Adapted> UntypedArrayLikeMutator<'ctx, Index> for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: UntypedArrayLikeMutator<'ctx, Index> {}
|
||||||
|
|
||||||
|
impl<'ctx, T, Index, Adapted> TypedArrayLikeAccessor<'ctx, T, Index> for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: UntypedArrayLikeAccessor<'ctx, Index> {
|
||||||
|
fn downcast_to_type(&self, ctx: &mut CodeGenContext<'ctx, '_>, value: BasicValueEnum<'ctx>) -> T {
|
||||||
|
(self.downcast_fn)(ctx, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T, Index, Adapted> TypedArrayLikeMutator<'ctx, T, Index> for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
||||||
|
where Adapted: UntypedArrayLikeMutator<'ctx, Index> {
|
||||||
|
fn upcast_from_type(&self, ctx: &mut CodeGenContext<'ctx, '_>, value: T) -> BasicValueEnum<'ctx> {
|
||||||
|
(self.upcast_fn)(ctx, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An LLVM value representing an array slice, consisting of a pointer to the data and the size of
|
||||||
|
/// the slice.
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct ArraySliceValue<'ctx>(PointerValue<'ctx>, IntValue<'ctx>, Option<&'ctx str>);
|
||||||
|
|
||||||
|
impl<'ctx> ArraySliceValue<'ctx> {
|
||||||
|
/// Creates an [`ArraySliceValue`] from a [`PointerValue`] and its size.
|
||||||
|
#[must_use]
|
||||||
|
pub fn from_ptr_val(
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
size: IntValue<'ctx>,
|
||||||
|
name: Option<&'ctx str>,
|
||||||
|
) -> Self {
|
||||||
|
ArraySliceValue(ptr, size, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> From<ArraySliceValue<'ctx>> for PointerValue<'ctx> {
|
||||||
|
fn from(value: ArraySliceValue<'ctx>) -> Self {
|
||||||
|
value.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> ArrayLikeValue<'ctx> for ArraySliceValue<'ctx> {
|
||||||
|
fn element_type<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
_: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> AnyTypeEnum<'ctx> {
|
||||||
|
self.0.get_type().get_element_type()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
_: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn size<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
_: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
self.1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> ArrayLikeIndexer<'ctx> for ArraySliceValue<'ctx> {
|
||||||
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: IntValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let var_name = name
|
||||||
|
.map(|v| format!("{v}.addr"))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
ctx.builder.build_in_bounds_gep(
|
||||||
|
self.base_ptr(ctx, generator),
|
||||||
|
&[idx],
|
||||||
|
var_name.as_str(),
|
||||||
|
).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: IntValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
debug_assert_eq!(idx.get_type(), generator.get_size_type(ctx.ctx));
|
||||||
|
|
||||||
|
let size = self.size(ctx, generator);
|
||||||
|
let in_range = ctx.builder.build_int_compare(IntPredicate::ULT, idx, size, "").unwrap();
|
||||||
|
ctx.make_assert(
|
||||||
|
generator,
|
||||||
|
in_range,
|
||||||
|
"0:IndexError",
|
||||||
|
"list index out of range",
|
||||||
|
[None, None, None],
|
||||||
|
ctx.current_loc,
|
||||||
|
);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
self.ptr_offset_unchecked(ctx, generator, idx, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx> for ArraySliceValue<'ctx> {}
|
||||||
|
impl<'ctx> UntypedArrayLikeMutator<'ctx> for ArraySliceValue<'ctx> {}
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
pub fn assert_is_list<'ctx>(_value: PointerValue<'ctx>, _llvm_usize: IntType<'ctx>) {}
|
pub fn assert_is_list<'ctx>(_value: PointerValue<'ctx>, _llvm_usize: IntType<'ctx>) {}
|
||||||
|
|
||||||
|
@ -120,8 +497,8 @@ impl<'ctx> ListValue<'ctx> {
|
||||||
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
||||||
/// on the field.
|
/// on the field.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn data(&self) -> ListDataProxy<'ctx> {
|
pub fn data(&self) -> ListDataProxy<'ctx, '_> {
|
||||||
ListDataProxy(*self)
|
ListDataProxy(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stores the `size` of this `list` into this instance.
|
/// Stores the `size` of this `list` into this instance.
|
||||||
|
@ -159,11 +536,22 @@ impl<'ctx> From<ListValue<'ctx>> for PointerValue<'ctx> {
|
||||||
|
|
||||||
/// Proxy type for accessing the `data` array of an `list` instance in LLVM.
|
/// Proxy type for accessing the `data` array of an `list` instance in LLVM.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct ListDataProxy<'ctx>(ListValue<'ctx>);
|
pub struct ListDataProxy<'ctx, 'a>(&'a ListValue<'ctx>);
|
||||||
|
|
||||||
impl<'ctx> ListDataProxy<'ctx> {
|
impl<'ctx> ArrayLikeValue<'ctx> for ListDataProxy<'ctx, '_> {
|
||||||
/// Returns the single-indirection pointer to the array.
|
fn element_type<G: CodeGenerator + ?Sized>(
|
||||||
pub fn as_ptr_value(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
&self,
|
||||||
|
_: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> AnyTypeEnum<'ctx> {
|
||||||
|
self.0.0.get_type().get_element_type()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
let var_name = self.0.1.map(|v| format!("{v}.data")).unwrap_or_default();
|
let var_name = self.0.1.map(|v| format!("{v}.data")).unwrap_or_default();
|
||||||
|
|
||||||
ctx.builder.build_load(self.0.pptr_to_data(ctx), var_name.as_str())
|
ctx.builder.build_load(self.0.pptr_to_data(ctx), var_name.as_str())
|
||||||
|
@ -171,12 +559,20 @@ impl<'ctx> ListDataProxy<'ctx> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
fn size<G: CodeGenerator + ?Sized>(
|
||||||
///
|
|
||||||
/// This function should be called with a valid index.
|
|
||||||
pub unsafe fn ptr_offset_unchecked(
|
|
||||||
&self,
|
&self,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
self.0.load_size(ctx, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> ArrayLikeIndexer<'ctx> for ListDataProxy<'ctx, '_> {
|
||||||
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
idx: IntValue<'ctx>,
|
idx: IntValue<'ctx>,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
) -> PointerValue<'ctx> {
|
) -> PointerValue<'ctx> {
|
||||||
|
@ -185,14 +581,13 @@ impl<'ctx> ListDataProxy<'ctx> {
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
ctx.builder.build_in_bounds_gep(
|
ctx.builder.build_in_bounds_gep(
|
||||||
self.as_ptr_value(ctx),
|
self.base_ptr(ctx, generator),
|
||||||
&[idx],
|
&[idx],
|
||||||
var_name.as_str(),
|
var_name.as_str(),
|
||||||
).unwrap()
|
).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the pointer to the data at the `idx`-th index.
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
pub fn ptr_offset<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
|
@ -201,12 +596,8 @@ impl<'ctx> ListDataProxy<'ctx> {
|
||||||
) -> PointerValue<'ctx> {
|
) -> PointerValue<'ctx> {
|
||||||
debug_assert_eq!(idx.get_type(), generator.get_size_type(ctx.ctx));
|
debug_assert_eq!(idx.get_type(), generator.get_size_type(ctx.ctx));
|
||||||
|
|
||||||
let in_range = ctx.builder.build_int_compare(
|
let size = self.size(ctx, generator);
|
||||||
IntPredicate::ULT,
|
let in_range = ctx.builder.build_int_compare(IntPredicate::ULT, idx, size, "").unwrap();
|
||||||
idx,
|
|
||||||
self.0.load_size(ctx, None),
|
|
||||||
""
|
|
||||||
).unwrap();
|
|
||||||
ctx.make_assert(
|
ctx.make_assert(
|
||||||
generator,
|
generator,
|
||||||
in_range,
|
in_range,
|
||||||
|
@ -217,35 +608,13 @@ impl<'ctx> ListDataProxy<'ctx> {
|
||||||
);
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.ptr_offset_unchecked(ctx, idx, name)
|
self.ptr_offset_unchecked(ctx, generator, idx, name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx> for ListDataProxy<'ctx, '_> {}
|
||||||
///
|
impl<'ctx> UntypedArrayLikeMutator<'ctx> for ListDataProxy<'ctx, '_> {}
|
||||||
/// This function should be called with a valid index.
|
|
||||||
pub unsafe fn get_unchecked(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
idx: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_offset_unchecked(ctx, idx, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the data at the `idx`-th flattened index.
|
|
||||||
pub fn get<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &mut G,
|
|
||||||
idx: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_offset(ctx, generator, idx, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
pub fn assert_is_range(_value: PointerValue) {}
|
pub fn assert_is_range(_value: PointerValue) {}
|
||||||
|
@ -559,8 +928,8 @@ impl<'ctx> NDArrayValue<'ctx> {
|
||||||
|
|
||||||
/// Returns a proxy object to the field storing the size of each dimension of this `NDArray`.
|
/// Returns a proxy object to the field storing the size of each dimension of this `NDArray`.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn dim_sizes(&self) -> NDArrayDimsProxy<'ctx> {
|
pub fn dim_sizes(&self) -> NDArrayDimsProxy<'ctx, '_> {
|
||||||
NDArrayDimsProxy(*self)
|
NDArrayDimsProxy(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
||||||
|
@ -596,8 +965,8 @@ impl<'ctx> NDArrayValue<'ctx> {
|
||||||
|
|
||||||
/// Returns a proxy object to the field storing the data of this `NDArray`.
|
/// Returns a proxy object to the field storing the data of this `NDArray`.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn data(&self) -> NDArrayDataProxy<'ctx> {
|
pub fn data(&self) -> NDArrayDataProxy<'ctx, '_> {
|
||||||
NDArrayDataProxy(*self)
|
NDArrayDataProxy(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -609,24 +978,43 @@ impl<'ctx> From<NDArrayValue<'ctx>> for PointerValue<'ctx> {
|
||||||
|
|
||||||
/// Proxy type for accessing the `dims` array of an `NDArray` instance in LLVM.
|
/// Proxy type for accessing the `dims` array of an `NDArray` instance in LLVM.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct NDArrayDimsProxy<'ctx>(NDArrayValue<'ctx>);
|
pub struct NDArrayDimsProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>);
|
||||||
|
|
||||||
impl<'ctx> NDArrayDimsProxy<'ctx> {
|
impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDimsProxy<'ctx, '_> {
|
||||||
/// Returns the single-indirection pointer to the array.
|
fn element_type<G: CodeGenerator + ?Sized>(
|
||||||
pub fn as_ptr_value(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
&self,
|
||||||
let var_name = self.0.1.map(|v| format!("{v}.dims")).unwrap_or_default();
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> AnyTypeEnum<'ctx> {
|
||||||
|
self.0.dim_sizes().base_ptr(ctx, generator).get_type().get_element_type()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let var_name = self.0.1.map(|v| format!("{v}.data")).unwrap_or_default();
|
||||||
|
|
||||||
ctx.builder.build_load(self.0.ptr_to_dims(ctx), var_name.as_str())
|
ctx.builder.build_load(self.0.ptr_to_dims(ctx), var_name.as_str())
|
||||||
.map(BasicValueEnum::into_pointer_value)
|
.map(BasicValueEnum::into_pointer_value)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
fn size<G: CodeGenerator + ?Sized>(
|
||||||
///
|
&self,
|
||||||
/// This function should be called with a valid index.
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
pub unsafe fn ptr_offset_unchecked(
|
_: &G,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
self.0.load_ndims(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> ArrayLikeIndexer<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {
|
||||||
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
idx: IntValue<'ctx>,
|
idx: IntValue<'ctx>,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
) -> PointerValue<'ctx> {
|
) -> PointerValue<'ctx> {
|
||||||
|
@ -635,24 +1023,24 @@ impl<'ctx> NDArrayDimsProxy<'ctx> {
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
ctx.builder.build_in_bounds_gep(
|
ctx.builder.build_in_bounds_gep(
|
||||||
self.as_ptr_value(ctx),
|
self.base_ptr(ctx, generator),
|
||||||
&[idx],
|
&[idx],
|
||||||
var_name.as_str(),
|
var_name.as_str(),
|
||||||
).unwrap()
|
).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the pointer to the size of the `idx`-th dimension.
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
pub fn ptr_offset<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
idx: IntValue<'ctx>,
|
idx: IntValue<'ctx>,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
) -> PointerValue<'ctx> {
|
) -> PointerValue<'ctx> {
|
||||||
|
let size = self.size(ctx, generator);
|
||||||
let in_range = ctx.builder.build_int_compare(
|
let in_range = ctx.builder.build_int_compare(
|
||||||
IntPredicate::ULT,
|
IntPredicate::ULT,
|
||||||
idx,
|
idx,
|
||||||
self.0.load_ndims(ctx),
|
size,
|
||||||
""
|
""
|
||||||
).unwrap();
|
).unwrap();
|
||||||
ctx.make_assert(
|
ctx.make_assert(
|
||||||
|
@ -665,47 +1053,52 @@ impl<'ctx> NDArrayDimsProxy<'ctx> {
|
||||||
);
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.ptr_offset_unchecked(ctx, idx, name)
|
self.ptr_offset_unchecked(ctx, generator, idx, name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {}
|
||||||
///
|
impl<'ctx> UntypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {}
|
||||||
/// This function should be called with a valid index.
|
|
||||||
pub unsafe fn get_unchecked(
|
impl<'ctx> TypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {
|
||||||
|
fn downcast_to_type(
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
_: &mut CodeGenContext<'ctx, '_>,
|
||||||
idx: IntValue<'ctx>,
|
value: BasicValueEnum<'ctx>,
|
||||||
name: Option<&str>,
|
|
||||||
) -> IntValue<'ctx> {
|
) -> IntValue<'ctx> {
|
||||||
let ptr = self.ptr_offset_unchecked(ctx, idx, name);
|
value.into_int_value()
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default())
|
}
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the size of the `idx`-th dimension.
|
impl<'ctx> TypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {
|
||||||
pub fn get<G: CodeGenerator + ?Sized>(
|
fn upcast_from_type(
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
_: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
value: IntValue<'ctx>,
|
||||||
idx: IntValue<'ctx>,
|
) -> BasicValueEnum<'ctx> {
|
||||||
name: Option<&str>,
|
value.into()
|
||||||
) -> IntValue<'ctx> {
|
|
||||||
let ptr = self.ptr_offset(ctx, generator, idx, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default())
|
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Proxy type for accessing the `data` array of an `NDArray` instance in LLVM.
|
/// Proxy type for accessing the `data` array of an `NDArray` instance in LLVM.
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct NDArrayDataProxy<'ctx>(NDArrayValue<'ctx>);
|
pub struct NDArrayDataProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>);
|
||||||
|
|
||||||
impl<'ctx> NDArrayDataProxy<'ctx> {
|
impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> {
|
||||||
/// Returns the single-indirection pointer to the array.
|
fn element_type<G: CodeGenerator + ?Sized>(
|
||||||
pub fn as_ptr_value(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &G,
|
||||||
|
) -> AnyTypeEnum<'ctx> {
|
||||||
|
self.0.data().base_ptr(ctx, generator).get_type().get_element_type()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
_: &G,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
let var_name = self.0.1.map(|v| format!("{v}.data")).unwrap_or_default();
|
let var_name = self.0.1.map(|v| format!("{v}.data")).unwrap_or_default();
|
||||||
|
|
||||||
ctx.builder.build_load(self.0.ptr_to_data(ctx), var_name.as_str())
|
ctx.builder.build_load(self.0.ptr_to_data(ctx), var_name.as_str())
|
||||||
|
@ -713,34 +1106,38 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
fn size<G: CodeGenerator + ?Sized>(
|
||||||
///
|
|
||||||
/// This function should be called with a valid index.
|
|
||||||
pub unsafe fn ptr_to_data_flattened_unchecked(
|
|
||||||
&self,
|
&self,
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
idx: IntValue<'ctx>,
|
generator: &G,
|
||||||
name: Option<&str>,
|
) -> IntValue<'ctx> {
|
||||||
) -> PointerValue<'ctx> {
|
call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator))
|
||||||
ctx.builder.build_in_bounds_gep(
|
}
|
||||||
self.as_ptr_value(ctx),
|
|
||||||
&[idx],
|
|
||||||
name.unwrap_or_default(),
|
|
||||||
).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the pointer to the data at the `idx`-th flattened index.
|
impl<'ctx> ArrayLikeIndexer<'ctx> for NDArrayDataProxy<'ctx, '_> {
|
||||||
pub fn ptr_to_data_flattened<G: CodeGenerator + ?Sized>(
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
idx: IntValue<'ctx>,
|
idx: IntValue<'ctx>,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
) -> PointerValue<'ctx> {
|
) -> PointerValue<'ctx> {
|
||||||
let ndims = self.0.load_ndims(ctx);
|
ctx.builder.build_in_bounds_gep(
|
||||||
let dims = self.0.dim_sizes().as_ptr_value(ctx);
|
self.base_ptr(ctx, generator),
|
||||||
let data_sz = call_ndarray_calc_size(generator, ctx, ndims, dims);
|
&[idx],
|
||||||
|
name.unwrap_or_default(),
|
||||||
|
).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
idx: IntValue<'ctx>,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let data_sz = self.size(ctx, generator);
|
||||||
let in_range = ctx.builder.build_int_compare(
|
let in_range = ctx.builder.build_int_compare(
|
||||||
IntPredicate::ULT,
|
IntPredicate::ULT,
|
||||||
idx,
|
idx,
|
||||||
|
@ -757,71 +1154,16 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
);
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.ptr_to_data_flattened_unchecked(ctx, idx, name)
|
self.ptr_offset_unchecked(ctx, generator, idx, name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {}
|
||||||
///
|
impl<'ctx> UntypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {}
|
||||||
/// This function should be called with a valid index.
|
|
||||||
pub unsafe fn get_flattened_unchecked(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
idx: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_to_data_flattened_unchecked(ctx, idx, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the data at the `idx`-th flattened index.
|
impl<'ctx> ArrayLikeIndexer<'ctx, ArrayValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {
|
||||||
pub fn get_flattened<G: CodeGenerator + ?Sized>(
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &mut G,
|
|
||||||
idx: IntValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_to_data_flattened(ctx, generator, idx, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function should be called with valid indices.
|
|
||||||
pub unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &G,
|
|
||||||
indices: ListValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> PointerValue<'ctx> {
|
|
||||||
let indices_elem_ty = indices.data().as_ptr_value(ctx).get_type().get_element_type();
|
|
||||||
let Ok(indices_elem_ty) = IntType::try_from(indices_elem_ty) else {
|
|
||||||
panic!("Expected list[int32] but got {indices_elem_ty}")
|
|
||||||
};
|
|
||||||
assert_eq!(indices_elem_ty.get_bit_width(), 32, "Expected list[int32] but got {indices_elem_ty}");
|
|
||||||
|
|
||||||
let index = call_ndarray_flatten_index(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
self.0,
|
|
||||||
indices,
|
|
||||||
);
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
ctx.builder.build_in_bounds_gep(
|
|
||||||
self.as_ptr_value(ctx),
|
|
||||||
&[index],
|
|
||||||
name.unwrap_or_default(),
|
|
||||||
).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function should be called with valid indices.
|
|
||||||
pub unsafe fn ptr_offset_unchecked_const<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
|
@ -831,21 +1173,20 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
let index = call_ndarray_flatten_index_const(
|
let index = call_ndarray_flatten_index_const(
|
||||||
generator,
|
generator,
|
||||||
ctx,
|
ctx,
|
||||||
self.0,
|
*self.0,
|
||||||
indices,
|
indices,
|
||||||
);
|
);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
ctx.builder.build_in_bounds_gep(
|
ctx.builder.build_in_bounds_gep(
|
||||||
self.as_ptr_value(ctx),
|
self.base_ptr(ctx, generator),
|
||||||
&[index],
|
&[index],
|
||||||
name.unwrap_or_default(),
|
name.unwrap_or_default(),
|
||||||
)
|
)
|
||||||
}.unwrap()
|
}.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the pointer to the data at the index specified by `indices`.
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
pub fn ptr_offset_const<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
|
@ -884,7 +1225,7 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
.map(|v| ctx.builder.build_int_z_extend_or_bit_cast(v, llvm_usize, "").unwrap())
|
.map(|v| ctx.builder.build_int_z_extend_or_bit_cast(v, llvm_usize, "").unwrap())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let dim_sz = unsafe {
|
let dim_sz = unsafe {
|
||||||
self.0.dim_sizes().get_unchecked(ctx, i, None)
|
self.0.dim_sizes().get_typed_unchecked(ctx, generator, i, None)
|
||||||
};
|
};
|
||||||
|
|
||||||
let dim_lt = ctx.builder.build_int_compare(
|
let dim_lt = ctx.builder.build_int_compare(
|
||||||
|
@ -905,23 +1246,59 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
self.ptr_offset_unchecked_const(ctx, generator, indices, name)
|
self.ptr_offset_unchecked(ctx, generator, indices, name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the pointer to the data at the index specified by `indices`.
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx, ArrayValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {}
|
||||||
pub fn ptr_offset<G: CodeGenerator + ?Sized>(
|
impl<'ctx> UntypedArrayLikeMutator<'ctx, ArrayValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {}
|
||||||
|
|
||||||
|
impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index> for NDArrayDataProxy<'ctx, '_> {
|
||||||
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
||||||
&self,
|
&self,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
indices: ListValue<'ctx>,
|
indices: Index,
|
||||||
name: Option<&str>,
|
name: Option<&str>,
|
||||||
) -> PointerValue<'ctx> {
|
) -> PointerValue<'ctx> {
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let indices_elem_ty = indices.ptr_offset(ctx, generator, llvm_usize.const_zero(), None).get_type().get_element_type();
|
||||||
|
let Ok(indices_elem_ty) = IntType::try_from(indices_elem_ty) else {
|
||||||
|
panic!("Expected list[int32] but got {indices_elem_ty}")
|
||||||
|
};
|
||||||
|
assert_eq!(indices_elem_ty.get_bit_width(), 32, "Expected list[int32] but got list[int{}]", indices_elem_ty.get_bit_width());
|
||||||
|
|
||||||
|
let index = call_ndarray_flatten_index(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
*self.0,
|
||||||
|
&indices,
|
||||||
|
);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
ctx.builder.build_in_bounds_gep(
|
||||||
|
self.base_ptr(ctx, generator),
|
||||||
|
&[index],
|
||||||
|
name.unwrap_or_default(),
|
||||||
|
).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
generator: &mut G,
|
||||||
|
indices: Index,
|
||||||
|
name: Option<&str>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let indices_size = indices.size(ctx, generator);
|
||||||
let nidx_leq_ndims = ctx.builder.build_int_compare(
|
let nidx_leq_ndims = ctx.builder.build_int_compare(
|
||||||
IntPredicate::SLE,
|
IntPredicate::SLE,
|
||||||
indices.load_size(ctx, None),
|
indices_size,
|
||||||
self.0.load_ndims(ctx),
|
self.0.load_ndims(ctx),
|
||||||
""
|
""
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
@ -934,7 +1311,7 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
ctx.current_loc,
|
ctx.current_loc,
|
||||||
);
|
);
|
||||||
|
|
||||||
let indices_len = indices.load_size(ctx, None);
|
let indices_len = indices.size(ctx, generator);
|
||||||
let ndarray_len = self.0.load_ndims(ctx);
|
let ndarray_len = self.0.load_ndims(ctx);
|
||||||
let len = call_int_umin(ctx, indices_len, ndarray_len, None);
|
let len = call_int_umin(ctx, indices_len, ndarray_len, None);
|
||||||
gen_for_callback_incrementing(
|
gen_for_callback_incrementing(
|
||||||
|
@ -945,8 +1322,8 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
|generator, ctx, i| {
|
|generator, ctx, i| {
|
||||||
let (dim_idx, dim_sz) = unsafe {
|
let (dim_idx, dim_sz) = unsafe {
|
||||||
(
|
(
|
||||||
indices.data().get_unchecked(ctx, i, None).into_int_value(),
|
indices.get_unchecked(ctx, generator, i, None).into_int_value(),
|
||||||
self.0.dim_sizes().get_unchecked(ctx, i, None),
|
self.0.dim_sizes().get_typed_unchecked(ctx, generator, i, None),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -975,56 +1352,7 @@ impl<'ctx> NDArrayDataProxy<'ctx> {
|
||||||
self.ptr_offset_unchecked(ctx, generator, indices, name)
|
self.ptr_offset_unchecked(ctx, generator, indices, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
|
||||||
///
|
|
||||||
/// This function should be called with valid indices.
|
|
||||||
pub unsafe fn get_unchecked_const<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &mut G,
|
|
||||||
indices: ArrayValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_offset_unchecked_const(ctx, generator, indices, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Safety
|
impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> UntypedArrayLikeAccessor<'ctx, Index> for NDArrayDataProxy<'ctx, '_> {}
|
||||||
///
|
impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> UntypedArrayLikeMutator<'ctx, Index> for NDArrayDataProxy<'ctx, '_> {}
|
||||||
/// This function should be called with valid indices.
|
|
||||||
pub unsafe fn get_unchecked<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &G,
|
|
||||||
indices: ListValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_offset_unchecked(ctx, generator, indices, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the data at the index specified by `indices`.
|
|
||||||
pub fn get_const<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &mut G,
|
|
||||||
indices: ArrayValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_offset_const(ctx, generator, indices, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the data at the index specified by `indices`.
|
|
||||||
pub fn get<G: CodeGenerator + ?Sized>(
|
|
||||||
&self,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
generator: &mut G,
|
|
||||||
indices: ListValue<'ctx>,
|
|
||||||
name: Option<&str>,
|
|
||||||
) -> BasicValueEnum<'ctx> {
|
|
||||||
let ptr = self.ptr_offset(ctx, generator, indices, name);
|
|
||||||
ctx.builder.build_load(ptr, name.unwrap_or_default()).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -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(), "")
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
|
@ -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(),
|
||||||
);
|
);
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue