2024-01-22 16:51:35 +08:00
|
|
|
use crate::codegen::{
|
2024-03-22 16:57:06 +08:00
|
|
|
irrt::{call_ndarray_calc_size, call_ndarray_flatten_index},
|
2024-02-22 01:47:26 +08:00
|
|
|
llvm_intrinsics::call_int_umin,
|
2024-03-08 13:13:18 +08:00
|
|
|
stmt::gen_for_callback_incrementing,
|
2024-06-12 14:45:03 +08:00
|
|
|
CodeGenContext, CodeGenerator,
|
|
|
|
};
|
|
|
|
use inkwell::context::Context;
|
|
|
|
use inkwell::types::{ArrayType, BasicType, StructType};
|
|
|
|
use inkwell::values::{ArrayValue, BasicValue, StructValue};
|
|
|
|
use inkwell::{
|
|
|
|
types::{AnyTypeEnum, BasicTypeEnum, IntType, PointerType},
|
|
|
|
values::{BasicValueEnum, IntValue, PointerValue},
|
|
|
|
AddressSpace, IntPredicate,
|
2024-01-22 16:51:35 +08:00
|
|
|
};
|
2024-01-23 17:21:24 +08:00
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// A LLVM type that is used to represent a non-primitive type in NAC3.
|
2024-06-07 13:00:42 +08:00
|
|
|
pub trait ProxyType<'ctx>: Into<Self::Base> {
|
2024-06-12 14:45:03 +08:00
|
|
|
/// The LLVM type of which values of this type possess. This is usually a
|
2024-06-07 13:00:42 +08:00
|
|
|
/// [LLVM pointer type][PointerType].
|
2024-06-06 12:16:09 +08:00
|
|
|
type Base: BasicType<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
/// The underlying LLVM type used to represent values. This is usually the element type of
|
2024-06-07 13:00:42 +08:00
|
|
|
/// [`Base`] if it is a pointer, otherwise this is the same type as `Base`.
|
|
|
|
type Underlying: BasicType<'ctx>;
|
2024-06-12 14:45:03 +08:00
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// The type of values represented by this type.
|
|
|
|
type Value: ProxyValue<'ctx>;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// Creates a new value of this type.
|
|
|
|
fn new_value<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
generator: &mut G,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value;
|
|
|
|
|
|
|
|
/// Creates a new array value of this type.
|
|
|
|
fn new_array_value<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
generator: &mut G,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
size: IntValue<'ctx>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> ArraySliceValue<'ctx> {
|
|
|
|
generator
|
|
|
|
.gen_array_var_alloc(ctx, self.as_underlying_type().as_basic_type_enum(), size, name)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Creates a [`value`][ProxyValue] with this as its type.
|
|
|
|
fn create_value(
|
|
|
|
&self,
|
|
|
|
value: <Self::Value as ProxyValue<'ctx>>::Base,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// Returns the [base type][Self::Base] of this proxy.
|
2024-06-06 12:16:09 +08:00
|
|
|
fn as_base_type(&self) -> Self::Base;
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
/// Returns the [underlying type][Self::Underlying] of this proxy.
|
|
|
|
fn as_underlying_type(&self) -> Self::Underlying;
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A LLVM type that is used to represent a non-primitive value in NAC3.
|
2024-06-07 13:00:42 +08:00
|
|
|
pub trait ProxyValue<'ctx>: Into<Self::Base> {
|
2024-06-12 14:45:03 +08:00
|
|
|
/// The type of LLVM values represented by this instance. This is usually the
|
2024-06-07 13:00:42 +08:00
|
|
|
/// [LLVM pointer type][PointerValue].
|
2024-06-06 12:16:09 +08:00
|
|
|
type Base: BasicValue<'ctx>;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// The underlying type of LLVM values represented by this instance. This is usually the element
|
|
|
|
/// type of [`Base`] if it is a pointer, otherwise this is the same type as `Base`.
|
|
|
|
type Underlying: BasicValue<'ctx>;
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// The type of this value.
|
|
|
|
type Type: ProxyType<'ctx>;
|
|
|
|
|
|
|
|
/// Returns the [type][ProxyType] of this value.
|
|
|
|
fn get_type(&self) -> Self::Type;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// Returns the [base value][Self::Base] of this proxy.
|
2024-06-06 12:16:09 +08:00
|
|
|
fn as_base_value(&self) -> Self::Base;
|
2024-06-07 13:00:42 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
/// Loads this value into its [underlying representation][Self::Underlying]. Usually involves a
|
2024-06-07 13:00:42 +08:00
|
|
|
/// `getelementptr` if [`Self::Base`] is a [pointer value][PointerValue].
|
|
|
|
fn as_underlying_value(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Underlying;
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
2024-03-13 11:16:23 +08:00
|
|
|
/// An LLVM value that is array-like, i.e. it contains a contiguous, sequenced collection of
|
2024-03-19 00:20:09 +08:00
|
|
|
/// 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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An array-like value that can have its array elements accessed as a [`BasicValueEnum`].
|
2024-06-12 14:45:03 +08:00
|
|
|
pub trait UntypedArrayLikeAccessor<'ctx, Index = IntValue<'ctx>>:
|
|
|
|
ArrayLikeIndexer<'ctx, Index>
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
/// # 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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> BasicValueEnum<'ctx> {
|
2024-06-12 15:13:09 +08:00
|
|
|
let ptr = unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) };
|
2024-03-19 00:20:09 +08:00
|
|
|
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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
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`].
|
2024-06-12 14:45:03 +08:00
|
|
|
pub trait UntypedArrayLikeMutator<'ctx, Index = IntValue<'ctx>>:
|
|
|
|
ArrayLikeIndexer<'ctx, Index>
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
/// # 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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
value: BasicValueEnum<'ctx>,
|
|
|
|
) {
|
2024-06-12 15:13:09 +08:00
|
|
|
let ptr = unsafe { self.ptr_offset_unchecked(ctx, generator, idx, None) };
|
2024-03-19 00:20:09 +08:00
|
|
|
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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
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`.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub trait TypedArrayLikeAccessor<'ctx, T, Index = IntValue<'ctx>>:
|
|
|
|
UntypedArrayLikeAccessor<'ctx, Index>
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
/// Casts an element from [`BasicValueEnum`] into `T`.
|
2024-06-12 14:45:03 +08:00
|
|
|
fn downcast_to_type(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
value: BasicValueEnum<'ctx>,
|
|
|
|
) -> T;
|
2024-03-19 00:20:09 +08:00
|
|
|
|
|
|
|
/// # 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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> T {
|
2024-06-12 15:13:09 +08:00
|
|
|
let value = unsafe { self.get_unchecked(ctx, generator, idx, name) };
|
2024-03-19 00:20:09 +08:00
|
|
|
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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
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`.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub trait TypedArrayLikeMutator<'ctx, T, Index = IntValue<'ctx>>:
|
|
|
|
UntypedArrayLikeMutator<'ctx, Index>
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
/// Casts an element from T into [`BasicValueEnum`].
|
2024-06-12 14:45:03 +08:00
|
|
|
fn upcast_from_type(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
value: T,
|
|
|
|
) -> BasicValueEnum<'ctx>;
|
2024-03-19 00:20:09 +08:00
|
|
|
|
|
|
|
/// # 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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
value: T,
|
|
|
|
) {
|
|
|
|
let value = self.upcast_from_type(ctx, value);
|
2024-06-12 15:13:09 +08:00
|
|
|
unsafe { self.set_unchecked(ctx, generator, idx, value) }
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the data at the `idx`-th index.
|
|
|
|
fn set_typed<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
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`.
|
2024-06-12 14:45:03 +08:00
|
|
|
type ValueDowncastFn<'ctx, T> =
|
|
|
|
Box<dyn Fn(&mut CodeGenContext<'ctx, '_>, BasicValueEnum<'ctx>) -> T>;
|
2024-03-19 00:20:09 +08:00
|
|
|
/// 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>
|
2024-06-12 14:45:03 +08:00
|
|
|
where
|
|
|
|
Adapted: ArrayLikeValue<'ctx>,
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
/// 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>
|
2024-06-12 14:45:03 +08:00
|
|
|
where
|
|
|
|
Adapted: ArrayLikeValue<'ctx>,
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
impl<'ctx, T, Index, Adapted> ArrayLikeIndexer<'ctx, Index>
|
|
|
|
for TypedArrayLikeAdapter<'ctx, T, Adapted>
|
|
|
|
where
|
|
|
|
Adapted: ArrayLikeIndexer<'ctx, Index>,
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-12 15:13:09 +08:00
|
|
|
unsafe { self.adapted.ptr_offset_unchecked(ctx, generator, idx, name) }
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
|
|
|
self.adapted.ptr_offset(ctx, generator, idx, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
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>,
|
|
|
|
{
|
|
|
|
}
|
2024-03-19 00:20:09 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
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 {
|
2024-03-19 00:20:09 +08:00
|
|
|
(self.downcast_fn)(ctx, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
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> {
|
2024-03-19 00:20:09 +08:00
|
|
|
(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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-12 14:45:03 +08:00
|
|
|
let var_name = name.map(|v| format!("{v}.addr")).unwrap_or_default();
|
2024-03-19 00:20:09 +08:00
|
|
|
|
2024-06-12 15:13:09 +08:00
|
|
|
unsafe {
|
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
|
|
|
debug_assert_eq!(idx.get_type(), generator.get_size_type(ctx.ctx));
|
|
|
|
|
|
|
|
let size = self.size(ctx, generator);
|
2024-04-16 17:20:24 +08:00
|
|
|
let in_range = ctx.builder.build_int_compare(IntPredicate::ULT, *idx, size, "").unwrap();
|
2024-03-19 00:20:09 +08:00
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
in_range,
|
|
|
|
"0:IndexError",
|
|
|
|
"list index out of range",
|
|
|
|
[None, None, None],
|
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) }
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx> for ArraySliceValue<'ctx> {}
|
|
|
|
impl<'ctx> UntypedArrayLikeMutator<'ctx> for ArraySliceValue<'ctx> {}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Proxy type for a `list` type in LLVM.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct ListType<'ctx> {
|
|
|
|
ty: PointerType<'ctx>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
impl<'ctx> ListType<'ctx> {
|
|
|
|
/// Checks whether `llvm_ty` represents a `list` type, returning [Err] if it does not.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn is_type(llvm_ty: PointerType<'ctx>, llvm_usize: IntType<'ctx>) -> Result<(), String> {
|
2024-06-06 12:16:09 +08:00
|
|
|
let llvm_list_ty = llvm_ty.get_element_type();
|
2024-01-23 17:21:24 +08:00
|
|
|
let AnyTypeEnum::StructType(llvm_list_ty) = llvm_list_ty else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected struct type for `list` type, got {llvm_list_ty}"));
|
2024-01-23 17:21:24 +08:00
|
|
|
};
|
|
|
|
if llvm_list_ty.count_fields() != 2 {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected 2 fields in `list`, got {}",
|
|
|
|
llvm_list_ty.count_fields()
|
|
|
|
));
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let list_size_ty = llvm_list_ty.get_field_type_at_index(0).unwrap();
|
|
|
|
let Ok(_) = PointerType::try_from(list_size_ty) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected pointer type for `list.0`, got {list_size_ty}"));
|
2024-01-23 17:21:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
let list_data_ty = llvm_list_ty.get_field_type_at_index(1).unwrap();
|
|
|
|
let Ok(list_data_ty) = IntType::try_from(list_data_ty) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected int type for `list.1`, got {list_data_ty}"));
|
2024-01-23 17:21:24 +08:00
|
|
|
};
|
|
|
|
if list_data_ty.get_bit_width() != llvm_usize.get_bit_width() {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected {}-bit int type for `list.1`, got {}-bit int",
|
|
|
|
llvm_usize.get_bit_width(),
|
|
|
|
list_data_ty.get_bit_width()
|
|
|
|
));
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// Creates an instance of [`ListType`].
|
|
|
|
#[must_use]
|
|
|
|
pub fn new<G: CodeGenerator + ?Sized>(
|
|
|
|
generator: &G,
|
|
|
|
ctx: &'ctx Context,
|
|
|
|
element_type: BasicTypeEnum<'ctx>,
|
|
|
|
) -> Self {
|
|
|
|
let llvm_usize = generator.get_size_type(ctx);
|
|
|
|
let llvm_list = ctx
|
|
|
|
.struct_type(
|
2024-06-12 14:45:03 +08:00
|
|
|
&[element_type.ptr_type(AddressSpace::default()).into(), llvm_usize.into()],
|
2024-06-07 13:00:42 +08:00
|
|
|
false,
|
|
|
|
)
|
|
|
|
.ptr_type(AddressSpace::default());
|
|
|
|
|
|
|
|
ListType::from_type(llvm_list, llvm_usize)
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Creates an [`ListType`] from a [`PointerType`].
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-06-06 12:16:09 +08:00
|
|
|
pub fn from_type(ptr_ty: PointerType<'ctx>, llvm_usize: IntType<'ctx>) -> Self {
|
|
|
|
debug_assert!(Self::is_type(ptr_ty, llvm_usize).is_ok());
|
|
|
|
|
|
|
|
ListType { ty: ptr_ty, llvm_usize }
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Returns the type of the `size` field of this `list` type.
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-06-06 12:16:09 +08:00
|
|
|
pub fn size_type(&self) -> IntType<'ctx> {
|
|
|
|
self.as_base_type()
|
|
|
|
.get_element_type()
|
|
|
|
.into_struct_type()
|
2024-06-11 15:17:02 +08:00
|
|
|
.get_field_type_at_index(1)
|
2024-06-06 12:16:09 +08:00
|
|
|
.map(BasicTypeEnum::into_int_type)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the element type of this `list` type.
|
|
|
|
#[must_use]
|
2024-06-11 15:17:02 +08:00
|
|
|
pub fn element_type(&self) -> AnyTypeEnum<'ctx> {
|
2024-06-06 12:16:09 +08:00
|
|
|
self.as_base_type()
|
|
|
|
.get_element_type()
|
|
|
|
.into_struct_type()
|
2024-06-11 15:17:02 +08:00
|
|
|
.get_field_type_at_index(0)
|
|
|
|
.map(BasicTypeEnum::into_pointer_type)
|
|
|
|
.map(PointerType::get_element_type)
|
2024-06-12 14:45:03 +08:00
|
|
|
.unwrap()
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> ProxyType<'ctx> for ListType<'ctx> {
|
|
|
|
type Base = PointerType<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
type Underlying = StructType<'ctx>;
|
2024-06-06 12:16:09 +08:00
|
|
|
type Value = ListValue<'ctx>;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
fn new_value<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
generator: &mut G,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value {
|
|
|
|
self.create_value(
|
|
|
|
generator.gen_var_alloc(ctx, self.as_underlying_type().into(), name).unwrap(),
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
fn create_value(
|
|
|
|
&self,
|
|
|
|
value: <Self::Value as ProxyValue<'ctx>>::Base,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value {
|
|
|
|
debug_assert_eq!(value.get_type(), self.as_base_type());
|
|
|
|
|
|
|
|
ListValue { value, llvm_usize: self.llvm_usize, name }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_base_type(&self) -> Self::Base {
|
|
|
|
self.ty
|
|
|
|
}
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
fn as_underlying_type(&self) -> Self::Underlying {
|
|
|
|
self.as_base_type().get_element_type().into_struct_type()
|
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<ListType<'ctx>> for PointerType<'ctx> {
|
|
|
|
fn from(value: ListType<'ctx>) -> Self {
|
|
|
|
value.as_base_type()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Proxy type for accessing a `list` value in LLVM.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct ListValue<'ctx> {
|
|
|
|
value: PointerValue<'ctx>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> ListValue<'ctx> {
|
|
|
|
/// Checks whether `value` is an instance of `list`, returning [Err] if `value` is not an
|
|
|
|
/// instance.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn is_instance(value: PointerValue<'ctx>, llvm_usize: IntType<'ctx>) -> Result<(), String> {
|
2024-06-06 12:16:09 +08:00
|
|
|
ListType::is_type(value.get_type(), llvm_usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an [`ListValue`] from a [`PointerValue`].
|
|
|
|
#[must_use]
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn from_ptr_val(
|
|
|
|
ptr: PointerValue<'ctx>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self {
|
2024-06-06 12:16:09 +08:00
|
|
|
debug_assert!(Self::is_instance(ptr, llvm_usize).is_ok());
|
|
|
|
|
|
|
|
<Self as ProxyValue<'ctx>>::Type::from_type(ptr.get_type(), llvm_usize)
|
|
|
|
.create_value(ptr, name)
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
|
|
|
/// on the field.
|
2024-03-13 11:41:25 +08:00
|
|
|
fn pptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-23 17:21:24 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.data.addr")).unwrap_or_default();
|
2024-01-23 17:21:24 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_zero()],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the pointer to the field storing the size of this `list`.
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_size(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-23 17:21:24 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.size.addr")).unwrap_or_default();
|
2024-01-23 17:21:24 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_int(1, true)],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the array of data elements `data` into this instance.
|
|
|
|
fn store_data(&self, ctx: &CodeGenContext<'ctx, '_>, data: PointerValue<'ctx>) {
|
2024-03-13 11:41:25 +08:00
|
|
|
ctx.builder.build_store(self.pptr_to_data(ctx), data).unwrap();
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience method for creating a new array storing data elements with the given element
|
|
|
|
/// type `elem_ty` and `size`.
|
|
|
|
///
|
|
|
|
/// If `size` is [None], the size stored in the field of this instance is used instead.
|
|
|
|
pub fn create_data(
|
|
|
|
&self,
|
2024-07-03 17:13:59 +08:00
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2024-01-23 17:21:24 +08:00
|
|
|
elem_ty: BasicTypeEnum<'ctx>,
|
|
|
|
size: Option<IntValue<'ctx>>,
|
|
|
|
) {
|
|
|
|
let size = size.unwrap_or_else(|| self.load_size(ctx, None));
|
2024-07-03 17:13:59 +08:00
|
|
|
|
|
|
|
let data = ctx
|
|
|
|
.builder
|
|
|
|
.build_select(
|
|
|
|
ctx.builder
|
|
|
|
.build_int_compare(IntPredicate::NE, size, self.llvm_usize.const_zero(), "")
|
|
|
|
.unwrap(),
|
|
|
|
ctx.builder.build_array_alloca(elem_ty, size, "").unwrap(),
|
|
|
|
elem_ty.ptr_type(AddressSpace::default()).const_zero(),
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap();
|
|
|
|
self.store_data(ctx, data);
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
|
|
|
/// on the field.
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-03-19 00:20:09 +08:00
|
|
|
pub fn data(&self) -> ListDataProxy<'ctx, '_> {
|
|
|
|
ListDataProxy(self)
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the `size` of this `list` into this instance.
|
2024-03-19 18:24:30 +08:00
|
|
|
pub fn store_size<G: CodeGenerator + ?Sized>(
|
2024-01-23 17:21:24 +08:00
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
2024-03-19 18:24:30 +08:00
|
|
|
generator: &G,
|
2024-01-23 17:21:24 +08:00
|
|
|
size: IntValue<'ctx>,
|
|
|
|
) {
|
|
|
|
debug_assert_eq!(size.get_type(), generator.get_size_type(ctx.ctx));
|
|
|
|
|
2024-03-13 11:41:25 +08:00
|
|
|
let psize = self.ptr_to_size(ctx);
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_store(psize, size).unwrap();
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the size of this `list` as a value.
|
|
|
|
pub fn load_size(&self, ctx: &CodeGenContext<'ctx, '_>, name: Option<&str>) -> IntValue<'ctx> {
|
2024-03-13 11:41:25 +08:00
|
|
|
let psize = self.ptr_to_size(ctx);
|
2024-01-23 17:21:24 +08:00
|
|
|
let var_name = name
|
2024-02-20 18:07:55 +08:00
|
|
|
.map(ToString::to_string)
|
2024-06-06 12:16:09 +08:00
|
|
|
.or_else(|| self.name.map(|v| format!("{v}.size")))
|
2024-01-23 17:21:24 +08:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_load(psize, var_name.as_str())
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap()
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
impl<'ctx> ProxyValue<'ctx> for ListValue<'ctx> {
|
|
|
|
type Base = PointerValue<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
type Underlying = StructValue<'ctx>;
|
2024-06-06 12:16:09 +08:00
|
|
|
type Type = ListType<'ctx>;
|
|
|
|
|
|
|
|
fn get_type(&self) -> Self::Type {
|
|
|
|
ListType::from_type(self.as_base_value().get_type(), self.llvm_usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_base_value(&self) -> Self::Base {
|
|
|
|
self.value
|
|
|
|
}
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
fn as_underlying_value(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Underlying {
|
|
|
|
ctx.builder
|
|
|
|
.build_load(self.as_base_value(), name.unwrap_or_default())
|
|
|
|
.map(BasicValueEnum::into_struct_value)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
2024-02-20 11:57:20 +08:00
|
|
|
impl<'ctx> From<ListValue<'ctx>> for PointerValue<'ctx> {
|
|
|
|
fn from(value: ListValue<'ctx>) -> Self {
|
2024-06-06 12:16:09 +08:00
|
|
|
value.as_base_value()
|
2024-02-20 11:57:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 17:21:24 +08:00
|
|
|
/// Proxy type for accessing the `data` array of an `list` instance in LLVM.
|
|
|
|
#[derive(Copy, Clone)]
|
2024-03-19 00:20:09 +08:00
|
|
|
pub struct ListDataProxy<'ctx, 'a>(&'a ListValue<'ctx>);
|
|
|
|
|
|
|
|
impl<'ctx> ArrayLikeValue<'ctx> for ListDataProxy<'ctx, '_> {
|
|
|
|
fn element_type<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
_: &CodeGenContext<'ctx, '_>,
|
|
|
|
_: &G,
|
|
|
|
) -> AnyTypeEnum<'ctx> {
|
2024-06-06 12:16:09 +08:00
|
|
|
self.0.value.get_type().get_element_type()
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
2024-01-23 17:21:24 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
|
|
|
_: &G,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.0.name.map(|v| format!("{v}.data")).unwrap_or_default();
|
2024-01-23 17:21:24 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_load(self.0.pptr_to_data(ctx), var_name.as_str())
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap()
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn size<G: CodeGenerator + ?Sized>(
|
2024-01-23 17:21:24 +08:00
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
2024-03-19 00:20:09 +08:00
|
|
|
_: &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,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-01-23 17:21:24 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-12 14:45:03 +08:00
|
|
|
let var_name = name.map(|v| format!("{v}.addr")).unwrap_or_default();
|
2024-01-23 17:21:24 +08:00
|
|
|
|
2024-06-12 15:13:09 +08:00
|
|
|
unsafe {
|
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
2024-01-23 17:21:24 +08:00
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2024-03-19 18:24:30 +08:00
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-01-23 17:21:24 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
|
|
|
debug_assert_eq!(idx.get_type(), generator.get_size_type(ctx.ctx));
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
let size = self.size(ctx, generator);
|
2024-04-16 17:20:24 +08:00
|
|
|
let in_range = ctx.builder.build_int_compare(IntPredicate::ULT, *idx, size, "").unwrap();
|
2024-01-23 17:21:24 +08:00
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
in_range,
|
|
|
|
"0:IndexError",
|
|
|
|
"list index out of range",
|
|
|
|
[None, None, None],
|
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) }
|
2024-01-23 17:21:24 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-23 18:27:00 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx> for ListDataProxy<'ctx, '_> {}
|
|
|
|
impl<'ctx> UntypedArrayLikeMutator<'ctx> for ListDataProxy<'ctx, '_> {}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Proxy type for a `range` type in LLVM.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct RangeType<'ctx> {
|
|
|
|
ty: PointerType<'ctx>,
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
impl<'ctx> RangeType<'ctx> {
|
|
|
|
/// Checks whether `llvm_ty` represents a `range` type, returning [Err] if it does not.
|
|
|
|
pub fn is_type(llvm_ty: PointerType<'ctx>) -> Result<(), String> {
|
|
|
|
let llvm_range_ty = llvm_ty.get_element_type();
|
2024-01-23 18:27:00 +08:00
|
|
|
let AnyTypeEnum::ArrayType(llvm_range_ty) = llvm_range_ty else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected array type for `range` type, got {llvm_range_ty}"));
|
2024-01-23 18:27:00 +08:00
|
|
|
};
|
|
|
|
if llvm_range_ty.len() != 3 {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected 3 elements for `range` type, got {}",
|
|
|
|
llvm_range_ty.len()
|
|
|
|
));
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let llvm_range_elem_ty = llvm_range_ty.get_element_type();
|
|
|
|
let Ok(llvm_range_elem_ty) = IntType::try_from(llvm_range_elem_ty) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected int type for `range` element type, got {llvm_range_elem_ty}"
|
|
|
|
));
|
2024-01-23 18:27:00 +08:00
|
|
|
};
|
|
|
|
if llvm_range_elem_ty.get_bit_width() != 32 {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected 32-bit int type for `range` element type, got {}",
|
|
|
|
llvm_range_elem_ty.get_bit_width()
|
|
|
|
));
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// Creates an instance of [`RangeType`].
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(ctx: &'ctx Context) -> Self {
|
|
|
|
let llvm_i32 = ctx.i32_type();
|
|
|
|
let llvm_range = llvm_i32.array_type(3).ptr_type(AddressSpace::default());
|
|
|
|
|
|
|
|
RangeType::from_type(llvm_range)
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Creates an [`RangeType`] from a [`PointerType`].
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-06-06 12:16:09 +08:00
|
|
|
pub fn from_type(ptr_ty: PointerType<'ctx>) -> Self {
|
|
|
|
debug_assert!(Self::is_type(ptr_ty).is_ok());
|
|
|
|
|
|
|
|
RangeType { ty: ptr_ty }
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Returns the type of all fields of this `range` type.
|
2024-05-27 15:11:14 +08:00
|
|
|
#[must_use]
|
2024-06-06 12:16:09 +08:00
|
|
|
pub fn value_type(&self) -> IntType<'ctx> {
|
2024-06-12 14:45:03 +08:00
|
|
|
self.as_base_type().get_element_type().into_array_type().get_element_type().into_int_type()
|
2024-05-27 15:11:14 +08:00
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> ProxyType<'ctx> for RangeType<'ctx> {
|
|
|
|
type Base = PointerType<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
type Underlying = ArrayType<'ctx>;
|
2024-06-06 12:16:09 +08:00
|
|
|
type Value = RangeValue<'ctx>;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
fn new_value<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
generator: &mut G,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value {
|
|
|
|
self.create_value(
|
|
|
|
generator.gen_var_alloc(ctx, self.as_underlying_type().into(), name).unwrap(),
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
fn create_value(
|
|
|
|
&self,
|
|
|
|
value: <Self::Value as ProxyValue<'ctx>>::Base,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value {
|
2024-06-06 12:16:09 +08:00
|
|
|
debug_assert_eq!(value.get_type(), self.as_base_type());
|
|
|
|
|
|
|
|
RangeValue { value, name }
|
|
|
|
}
|
2024-05-27 15:11:14 +08:00
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
fn as_base_type(&self) -> Self::Base {
|
|
|
|
self.ty
|
|
|
|
}
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
fn as_underlying_type(&self) -> Self::Underlying {
|
|
|
|
self.as_base_type().get_element_type().into_array_type()
|
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<RangeType<'ctx>> for PointerType<'ctx> {
|
|
|
|
fn from(value: RangeType<'ctx>) -> Self {
|
|
|
|
value.as_base_type()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Proxy type for accessing a `range` value in LLVM.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct RangeValue<'ctx> {
|
|
|
|
value: PointerValue<'ctx>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> RangeValue<'ctx> {
|
|
|
|
/// Checks whether `value` is an instance of `range`, returning [Err] if `value` is not an instance.
|
|
|
|
pub fn is_instance(value: PointerValue<'ctx>) -> Result<(), String> {
|
|
|
|
RangeType::is_type(value.get_type())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an [`RangeValue`] from a [`PointerValue`].
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-06-06 12:16:09 +08:00
|
|
|
pub fn from_ptr_val(ptr: PointerValue<'ctx>, name: Option<&'ctx str>) -> Self {
|
|
|
|
debug_assert!(Self::is_instance(ptr).is_ok());
|
|
|
|
|
|
|
|
<Self as ProxyValue<'ctx>>::Type::from_type(ptr.get_type()).create_value(ptr, name)
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_start(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-23 18:27:00 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.start.addr")).unwrap_or_default();
|
2024-01-23 18:27:00 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_int(0, false)],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_end(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-23 18:27:00 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.end.addr")).unwrap_or_default();
|
2024-01-23 18:27:00 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_int(1, false)],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_step(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-23 18:27:00 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.step.addr")).unwrap_or_default();
|
2024-01-23 18:27:00 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_int(2, false)],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the `start` value into this instance.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn store_start(&self, ctx: &CodeGenContext<'ctx, '_>, start: IntValue<'ctx>) {
|
2024-01-23 18:27:00 +08:00
|
|
|
debug_assert_eq!(start.get_type().get_bit_width(), 32);
|
|
|
|
|
2024-03-13 11:41:25 +08:00
|
|
|
let pstart = self.ptr_to_start(ctx);
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_store(pstart, start).unwrap();
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `start` value of this `range`.
|
|
|
|
pub fn load_start(&self, ctx: &CodeGenContext<'ctx, '_>, name: Option<&str>) -> IntValue<'ctx> {
|
2024-03-13 11:41:25 +08:00
|
|
|
let pstart = self.ptr_to_start(ctx);
|
2024-01-23 18:27:00 +08:00
|
|
|
let var_name = name
|
2024-02-20 18:07:55 +08:00
|
|
|
.map(ToString::to_string)
|
2024-06-06 12:16:09 +08:00
|
|
|
.or_else(|| self.name.map(|v| format!("{v}.start")))
|
2024-01-23 18:27:00 +08:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_load(pstart, var_name.as_str())
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap()
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the `end` value into this instance.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn store_end(&self, ctx: &CodeGenContext<'ctx, '_>, end: IntValue<'ctx>) {
|
2024-01-23 18:27:00 +08:00
|
|
|
debug_assert_eq!(end.get_type().get_bit_width(), 32);
|
|
|
|
|
2024-06-11 15:12:32 +08:00
|
|
|
let pend = self.ptr_to_end(ctx);
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_store(pend, end).unwrap();
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `end` value of this `range`.
|
|
|
|
pub fn load_end(&self, ctx: &CodeGenContext<'ctx, '_>, name: Option<&str>) -> IntValue<'ctx> {
|
2024-03-13 11:41:25 +08:00
|
|
|
let pend = self.ptr_to_end(ctx);
|
2024-01-23 18:27:00 +08:00
|
|
|
let var_name = name
|
2024-02-20 18:07:55 +08:00
|
|
|
.map(ToString::to_string)
|
2024-06-06 12:16:09 +08:00
|
|
|
.or_else(|| self.name.map(|v| format!("{v}.end")))
|
2024-01-23 18:27:00 +08:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder.build_load(pend, var_name.as_str()).map(BasicValueEnum::into_int_value).unwrap()
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the `step` value into this instance.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn store_step(&self, ctx: &CodeGenContext<'ctx, '_>, step: IntValue<'ctx>) {
|
2024-01-23 18:27:00 +08:00
|
|
|
debug_assert_eq!(step.get_type().get_bit_width(), 32);
|
|
|
|
|
2024-06-11 15:12:32 +08:00
|
|
|
let pstep = self.ptr_to_step(ctx);
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_store(pstep, step).unwrap();
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `step` value of this `range`.
|
|
|
|
pub fn load_step(&self, ctx: &CodeGenContext<'ctx, '_>, name: Option<&str>) -> IntValue<'ctx> {
|
2024-03-13 11:41:25 +08:00
|
|
|
let pstep = self.ptr_to_step(ctx);
|
2024-01-23 18:27:00 +08:00
|
|
|
let var_name = name
|
2024-02-20 18:07:55 +08:00
|
|
|
.map(ToString::to_string)
|
2024-06-06 12:16:09 +08:00
|
|
|
.or_else(|| self.name.map(|v| format!("{v}.step")))
|
2024-01-23 18:27:00 +08:00
|
|
|
.unwrap_or_default();
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_load(pstep, var_name.as_str())
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap()
|
2024-01-23 18:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
impl<'ctx> ProxyValue<'ctx> for RangeValue<'ctx> {
|
|
|
|
type Base = PointerValue<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
type Underlying = ArrayValue<'ctx>;
|
2024-06-06 12:16:09 +08:00
|
|
|
type Type = RangeType<'ctx>;
|
|
|
|
|
|
|
|
fn get_type(&self) -> Self::Type {
|
|
|
|
RangeType::from_type(self.value.get_type())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_base_value(&self) -> Self::Base {
|
|
|
|
self.value
|
|
|
|
}
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
fn as_underlying_value(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Underlying {
|
|
|
|
ctx.builder
|
|
|
|
.build_load(self.as_base_value(), name.unwrap_or_default())
|
|
|
|
.map(BasicValueEnum::into_array_value)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
2024-02-20 11:57:20 +08:00
|
|
|
impl<'ctx> From<RangeValue<'ctx>> for PointerValue<'ctx> {
|
|
|
|
fn from(value: RangeValue<'ctx>) -> Self {
|
2024-06-06 12:16:09 +08:00
|
|
|
value.as_base_value()
|
2024-02-20 11:57:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Proxy type for a `ndarray` type in LLVM.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct NDArrayType<'ctx> {
|
|
|
|
ty: PointerType<'ctx>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
impl<'ctx> NDArrayType<'ctx> {
|
|
|
|
/// Checks whether `llvm_ty` represents a `ndarray` type, returning [Err] if it does not.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn is_type(llvm_ty: PointerType<'ctx>, llvm_usize: IntType<'ctx>) -> Result<(), String> {
|
2024-06-06 12:16:09 +08:00
|
|
|
let llvm_ndarray_ty = llvm_ty.get_element_type();
|
2024-01-22 16:51:35 +08:00
|
|
|
let AnyTypeEnum::StructType(llvm_ndarray_ty) = llvm_ndarray_ty else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected struct type for `NDArray` type, got {llvm_ndarray_ty}"));
|
2024-01-22 16:51:35 +08:00
|
|
|
};
|
|
|
|
if llvm_ndarray_ty.count_fields() != 3 {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected 3 fields in `NDArray`, got {}",
|
|
|
|
llvm_ndarray_ty.count_fields()
|
|
|
|
));
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let ndarray_ndims_ty = llvm_ndarray_ty.get_field_type_at_index(0).unwrap();
|
|
|
|
let Ok(ndarray_ndims_ty) = IntType::try_from(ndarray_ndims_ty) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected int type for `ndarray.0`, got {ndarray_ndims_ty}"));
|
2024-01-22 16:51:35 +08:00
|
|
|
};
|
|
|
|
if ndarray_ndims_ty.get_bit_width() != llvm_usize.get_bit_width() {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected {}-bit int type for `ndarray.0`, got {}-bit int",
|
|
|
|
llvm_usize.get_bit_width(),
|
|
|
|
ndarray_ndims_ty.get_bit_width()
|
|
|
|
));
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let ndarray_dims_ty = llvm_ndarray_ty.get_field_type_at_index(1).unwrap();
|
|
|
|
let Ok(ndarray_pdims) = PointerType::try_from(ndarray_dims_ty) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected pointer type for `ndarray.1`, got {ndarray_dims_ty}"));
|
2024-01-22 16:51:35 +08:00
|
|
|
};
|
|
|
|
let ndarray_dims = ndarray_pdims.get_element_type();
|
|
|
|
let Ok(ndarray_dims) = IntType::try_from(ndarray_dims) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected pointer-to-int type for `ndarray.1`, got pointer-to-{ndarray_dims}"
|
|
|
|
));
|
2024-01-22 16:51:35 +08:00
|
|
|
};
|
|
|
|
if ndarray_dims.get_bit_width() != llvm_usize.get_bit_width() {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!(
|
|
|
|
"Expected pointer-to-{}-bit int type for `ndarray.1`, got pointer-to-{}-bit int",
|
|
|
|
llvm_usize.get_bit_width(),
|
|
|
|
ndarray_dims.get_bit_width()
|
|
|
|
));
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let ndarray_data_ty = llvm_ndarray_ty.get_field_type_at_index(2).unwrap();
|
|
|
|
let Ok(_) = PointerType::try_from(ndarray_data_ty) else {
|
2024-06-12 14:45:03 +08:00
|
|
|
return Err(format!("Expected pointer type for `ndarray.2`, got {ndarray_data_ty}"));
|
2024-01-22 16:51:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
/// Creates an instance of [`ListType`].
|
|
|
|
#[must_use]
|
|
|
|
pub fn new<G: CodeGenerator + ?Sized>(
|
|
|
|
generator: &G,
|
|
|
|
ctx: &'ctx Context,
|
|
|
|
dtype: BasicTypeEnum<'ctx>,
|
|
|
|
) -> Self {
|
|
|
|
let llvm_usize = generator.get_size_type(ctx);
|
|
|
|
|
|
|
|
// struct NDArray { num_dims: size_t, dims: size_t*, data: T* }
|
|
|
|
//
|
|
|
|
// * num_dims: Number of dimensions in the array
|
|
|
|
// * dims: Pointer to an array containing the size of each dimension
|
2024-06-12 14:45:03 +08:00
|
|
|
// * data: Pointer to an array containing the array data
|
2024-06-07 13:00:42 +08:00
|
|
|
let llvm_ndarray = ctx
|
|
|
|
.struct_type(
|
|
|
|
&[
|
|
|
|
llvm_usize.into(),
|
|
|
|
llvm_usize.ptr_type(AddressSpace::default()).into(),
|
2024-06-12 14:45:03 +08:00
|
|
|
dtype.ptr_type(AddressSpace::default()).into(),
|
2024-06-07 13:00:42 +08:00
|
|
|
],
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.ptr_type(AddressSpace::default());
|
|
|
|
|
|
|
|
NDArrayType::from_type(llvm_ndarray, llvm_usize)
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Creates an [`NDArrayType`] from a [`PointerType`].
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-06-06 12:16:09 +08:00
|
|
|
pub fn from_type(ptr_ty: PointerType<'ctx>, llvm_usize: IntType<'ctx>) -> Self {
|
|
|
|
debug_assert!(Self::is_type(ptr_ty, llvm_usize).is_ok());
|
|
|
|
|
|
|
|
NDArrayType { ty: ptr_ty, llvm_usize }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the type of the `size` field of this `ndarray` type.
|
|
|
|
#[must_use]
|
|
|
|
pub fn size_type(&self) -> IntType<'ctx> {
|
|
|
|
self.as_base_type()
|
|
|
|
.get_element_type()
|
|
|
|
.into_struct_type()
|
|
|
|
.get_field_type_at_index(0)
|
|
|
|
.map(BasicTypeEnum::into_int_type)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the element type of this `ndarray` type.
|
|
|
|
#[must_use]
|
|
|
|
pub fn element_type(&self) -> BasicTypeEnum<'ctx> {
|
|
|
|
self.as_base_type()
|
|
|
|
.get_element_type()
|
|
|
|
.into_struct_type()
|
|
|
|
.get_field_type_at_index(2)
|
|
|
|
.unwrap()
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> ProxyType<'ctx> for NDArrayType<'ctx> {
|
|
|
|
type Base = PointerType<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
type Underlying = StructType<'ctx>;
|
2024-06-06 12:16:09 +08:00
|
|
|
type Value = NDArrayValue<'ctx>;
|
|
|
|
|
2024-06-07 13:00:42 +08:00
|
|
|
fn new_value<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
generator: &mut G,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Value {
|
|
|
|
self.create_value(
|
2024-06-12 14:45:03 +08:00
|
|
|
generator.gen_var_alloc(ctx, self.as_underlying_type().into(), name).unwrap(),
|
2024-06-07 13:00:42 +08:00
|
|
|
name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
fn create_value(
|
|
|
|
&self,
|
|
|
|
value: <Self::Value as ProxyValue<'ctx>>::Base,
|
2024-01-22 16:51:35 +08:00
|
|
|
name: Option<&'ctx str>,
|
2024-06-06 12:16:09 +08:00
|
|
|
) -> Self::Value {
|
|
|
|
debug_assert_eq!(value.get_type(), self.as_base_type());
|
|
|
|
|
|
|
|
NDArrayValue { value, llvm_usize: self.llvm_usize, name }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_base_type(&self) -> Self::Base {
|
|
|
|
self.ty
|
|
|
|
}
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
fn as_underlying_type(&self) -> Self::Underlying {
|
|
|
|
self.as_base_type().get_element_type().into_struct_type()
|
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> From<NDArrayType<'ctx>> for PointerType<'ctx> {
|
|
|
|
fn from(value: NDArrayType<'ctx>) -> Self {
|
|
|
|
value.as_base_type()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
/// Proxy type for accessing an `NDArray` value in LLVM.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct NDArrayValue<'ctx> {
|
|
|
|
value: PointerValue<'ctx>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> NDArrayValue<'ctx> {
|
|
|
|
/// Checks whether `value` is an instance of `NDArray`, returning [Err] if `value` is not an
|
|
|
|
/// instance.
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn is_instance(value: PointerValue<'ctx>, llvm_usize: IntType<'ctx>) -> Result<(), String> {
|
2024-06-06 12:16:09 +08:00
|
|
|
NDArrayType::is_type(value.get_type(), llvm_usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an [`NDArrayValue`] from a [`PointerValue`].
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-06-12 14:45:03 +08:00
|
|
|
pub fn from_ptr_val(
|
|
|
|
ptr: PointerValue<'ctx>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self {
|
2024-06-06 12:16:09 +08:00
|
|
|
debug_assert!(Self::is_instance(ptr, llvm_usize).is_ok());
|
|
|
|
|
|
|
|
<Self as ProxyValue<'ctx>>::Type::from_type(ptr.get_type(), llvm_usize)
|
|
|
|
.create_value(ptr, name)
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the pointer to the field storing the number of dimensions of this `NDArray`.
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_ndims(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-22 16:51:35 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.ndims.addr")).unwrap_or_default();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_zero()],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the number of dimensions `ndims` into this instance.
|
2024-03-19 18:24:30 +08:00
|
|
|
pub fn store_ndims<G: CodeGenerator + ?Sized>(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
2024-03-19 18:24:30 +08:00
|
|
|
generator: &G,
|
2024-01-22 16:51:35 +08:00
|
|
|
ndims: IntValue<'ctx>,
|
|
|
|
) {
|
|
|
|
debug_assert_eq!(ndims.get_type(), generator.get_size_type(ctx.ctx));
|
|
|
|
|
2024-03-13 11:41:25 +08:00
|
|
|
let pndims = self.ptr_to_ndims(ctx);
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_store(pndims, ndims).unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of dimensions of this `NDArray` as a value.
|
|
|
|
pub fn load_ndims(&self, ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx> {
|
2024-03-13 11:41:25 +08:00
|
|
|
let pndims = self.ptr_to_ndims(ctx);
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder.build_load(pndims, "").map(BasicValueEnum::into_int_value).unwrap()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the double-indirection pointer to the `dims` array, as if by calling `getelementptr`
|
|
|
|
/// on the field.
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_dims(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-22 16:51:35 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.dims.addr")).unwrap_or_default();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_int(1, true)],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the array of dimension sizes `dims` into this instance.
|
2024-03-13 11:41:25 +08:00
|
|
|
fn store_dim_sizes(&self, ctx: &CodeGenContext<'ctx, '_>, dims: PointerValue<'ctx>) {
|
|
|
|
ctx.builder.build_store(self.ptr_to_dims(ctx), dims).unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience method for creating a new array storing dimension sizes with the given `size`.
|
2024-03-13 11:41:25 +08:00
|
|
|
pub fn create_dim_sizes(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
|
|
|
llvm_usize: IntType<'ctx>,
|
|
|
|
size: IntValue<'ctx>,
|
|
|
|
) {
|
2024-03-13 11:41:25 +08:00
|
|
|
self.store_dim_sizes(ctx, ctx.builder.build_array_alloca(llvm_usize, size, "").unwrap());
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a proxy object to the field storing the size of each dimension of this `NDArray`.
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-03-19 00:20:09 +08:00
|
|
|
pub fn dim_sizes(&self) -> NDArrayDimsProxy<'ctx, '_> {
|
|
|
|
NDArrayDimsProxy(self)
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
|
|
|
/// on the field.
|
2024-03-13 11:41:25 +08:00
|
|
|
fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
2024-01-22 16:51:35 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.name.map(|v| format!("{v}.data.addr")).unwrap_or_default();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.as_base_value(),
|
|
|
|
&[llvm_i32.const_zero(), llvm_i32.const_int(2, true)],
|
|
|
|
var_name.as_str(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stores the array of data elements `data` into this instance.
|
|
|
|
fn store_data(&self, ctx: &CodeGenContext<'ctx, '_>, data: PointerValue<'ctx>) {
|
2024-03-13 11:41:25 +08:00
|
|
|
ctx.builder.build_store(self.ptr_to_data(ctx), data).unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenience method for creating a new array storing data elements with the given element
|
2024-02-19 19:30:25 +08:00
|
|
|
/// type `elem_ty` and `size`.
|
2024-01-22 16:51:35 +08:00
|
|
|
pub fn create_data(
|
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
|
|
|
elem_ty: BasicTypeEnum<'ctx>,
|
|
|
|
size: IntValue<'ctx>,
|
|
|
|
) {
|
2024-02-19 19:30:25 +08:00
|
|
|
self.store_data(ctx, ctx.builder.build_array_alloca(elem_ty, size, "").unwrap());
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a proxy object to the field storing the data of this `NDArray`.
|
2024-02-20 18:07:55 +08:00
|
|
|
#[must_use]
|
2024-03-19 00:20:09 +08:00
|
|
|
pub fn data(&self) -> NDArrayDataProxy<'ctx, '_> {
|
|
|
|
NDArrayDataProxy(self)
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:16:09 +08:00
|
|
|
impl<'ctx> ProxyValue<'ctx> for NDArrayValue<'ctx> {
|
|
|
|
type Base = PointerValue<'ctx>;
|
2024-06-07 13:00:42 +08:00
|
|
|
type Underlying = StructValue<'ctx>;
|
2024-06-06 12:16:09 +08:00
|
|
|
type Type = NDArrayType<'ctx>;
|
|
|
|
|
|
|
|
fn get_type(&self) -> Self::Type {
|
|
|
|
NDArrayType::from_type(self.as_base_value().get_type(), self.llvm_usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_base_value(&self) -> Self::Base {
|
|
|
|
self.value
|
|
|
|
}
|
2024-06-07 13:00:42 +08:00
|
|
|
|
|
|
|
fn as_underlying_value(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
name: Option<&'ctx str>,
|
|
|
|
) -> Self::Underlying {
|
|
|
|
ctx.builder
|
|
|
|
.build_load(self.as_base_value(), name.unwrap_or_default())
|
|
|
|
.map(BasicValueEnum::into_struct_value)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-06-06 12:16:09 +08:00
|
|
|
}
|
|
|
|
|
2024-02-20 11:57:20 +08:00
|
|
|
impl<'ctx> From<NDArrayValue<'ctx>> for PointerValue<'ctx> {
|
|
|
|
fn from(value: NDArrayValue<'ctx>) -> Self {
|
2024-06-06 12:16:09 +08:00
|
|
|
value.as_base_value()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Proxy type for accessing the `dims` array of an `NDArray` instance in LLVM.
|
|
|
|
#[derive(Copy, Clone)]
|
2024-03-19 00:20:09 +08:00
|
|
|
pub struct NDArrayDimsProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>);
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDimsProxy<'ctx, '_> {
|
|
|
|
fn element_type<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
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> {
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.0.name.map(|v| format!("{v}.data")).unwrap_or_default();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_load(self.0.ptr_to_dims(ctx), var_name.as_str())
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn size<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
|
|
|
_: &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>(
|
2024-03-18 15:51:01 +08:00
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2024-03-19 00:20:09 +08:00
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-03-18 15:51:01 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-12 14:45:03 +08:00
|
|
|
let var_name = name.map(|v| format!("{v}.addr")).unwrap_or_default();
|
2024-03-18 15:51:01 +08:00
|
|
|
|
2024-06-12 15:13:09 +08:00
|
|
|
unsafe {
|
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(self.base_ptr(ctx, generator), &[*idx], var_name.as_str())
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-03-18 15:51:01 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2024-03-19 18:24:30 +08:00
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-01-22 16:51:35 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-03-19 00:20:09 +08:00
|
|
|
let size = self.size(ctx, generator);
|
2024-06-12 14:45:03 +08:00
|
|
|
let in_range = ctx.builder.build_int_compare(IntPredicate::ULT, *idx, size, "").unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
in_range,
|
|
|
|
"0:IndexError",
|
|
|
|
"index {0} is out of bounds for axis 0 with size {1}",
|
2024-04-16 17:20:24 +08:00
|
|
|
[Some(*idx), Some(self.0.load_ndims(ctx)), None],
|
2024-01-22 16:51:35 +08:00
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) }
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {}
|
|
|
|
impl<'ctx> UntypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {}
|
|
|
|
|
|
|
|
impl<'ctx> TypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {
|
|
|
|
fn downcast_to_type(
|
2024-03-18 15:51:01 +08:00
|
|
|
&self,
|
2024-03-19 00:20:09 +08:00
|
|
|
_: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
value: BasicValueEnum<'ctx>,
|
2024-03-18 15:51:01 +08:00
|
|
|
) -> IntValue<'ctx> {
|
2024-03-19 00:20:09 +08:00
|
|
|
value.into_int_value()
|
2024-03-18 15:51:01 +08:00
|
|
|
}
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
2024-03-18 15:51:01 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
impl<'ctx> TypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDimsProxy<'ctx, '_> {
|
|
|
|
fn upcast_from_type(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
2024-03-19 00:20:09 +08:00
|
|
|
_: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
value: IntValue<'ctx>,
|
|
|
|
) -> BasicValueEnum<'ctx> {
|
|
|
|
value.into()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Proxy type for accessing the `data` array of an `NDArray` instance in LLVM.
|
|
|
|
#[derive(Copy, Clone)]
|
2024-03-19 00:20:09 +08:00
|
|
|
pub struct NDArrayDataProxy<'ctx, 'a>(&'a NDArrayValue<'ctx>);
|
|
|
|
|
|
|
|
impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> {
|
|
|
|
fn element_type<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &G,
|
|
|
|
) -> AnyTypeEnum<'ctx> {
|
|
|
|
self.0.data().base_ptr(ctx, generator).get_type().get_element_type()
|
|
|
|
}
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn base_ptr<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
|
|
|
_: &G,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-06 12:16:09 +08:00
|
|
|
let var_name = self.0.name.map(|v| format!("{v}.data")).unwrap_or_default();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_load(self.0.ptr_to_data(ctx), var_name.as_str())
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap()
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn size<G: CodeGenerator + ?Sized>(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
|
|
|
ctx: &CodeGenContext<'ctx, '_>,
|
2024-03-19 00:20:09 +08:00
|
|
|
generator: &G,
|
|
|
|
) -> IntValue<'ctx> {
|
2024-05-27 15:58:06 +08:00
|
|
|
call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None))
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'ctx> ArrayLikeIndexer<'ctx> for NDArrayDataProxy<'ctx, '_> {
|
|
|
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-01-22 16:51:35 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-06-12 15:13:09 +08:00
|
|
|
unsafe {
|
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.base_ptr(ctx, generator),
|
|
|
|
&[*idx],
|
|
|
|
name.unwrap_or_default(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2024-03-19 18:24:30 +08:00
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
idx: &IntValue<'ctx>,
|
2024-01-22 16:51:35 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
2024-03-19 00:20:09 +08:00
|
|
|
let data_sz = self.size(ctx, generator);
|
2024-06-12 14:45:03 +08:00
|
|
|
let in_range = ctx.builder.build_int_compare(IntPredicate::ULT, *idx, data_sz, "").unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
in_range,
|
|
|
|
"0:IndexError",
|
|
|
|
"index {0} is out of bounds with size {1}",
|
2024-04-16 17:20:24 +08:00
|
|
|
[Some(*idx), Some(self.0.load_ndims(ctx)), None],
|
2024-01-22 16:51:35 +08:00
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
unsafe { self.ptr_offset_unchecked(ctx, generator, idx, name) }
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
2024-03-19 00:20:09 +08:00
|
|
|
}
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
impl<'ctx> UntypedArrayLikeAccessor<'ctx, IntValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {}
|
|
|
|
impl<'ctx> UntypedArrayLikeMutator<'ctx, IntValue<'ctx>> for NDArrayDataProxy<'ctx, '_> {}
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
|
|
|
|
for NDArrayDataProxy<'ctx, '_>
|
|
|
|
{
|
2024-03-19 00:20:09 +08:00
|
|
|
unsafe fn ptr_offset_unchecked<G: CodeGenerator + ?Sized>(
|
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
indices: &Index,
|
2024-03-19 00:20:09 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
|
|
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
|
|
|
2024-04-16 17:20:24 +08:00
|
|
|
let indices_elem_ty = indices
|
|
|
|
.ptr_offset(ctx, generator, &llvm_usize.const_zero(), None)
|
|
|
|
.get_type()
|
|
|
|
.get_element_type();
|
2024-03-19 00:20:09 +08:00
|
|
|
let Ok(indices_elem_ty) = IntType::try_from(indices_elem_ty) else {
|
|
|
|
panic!("Expected list[int32] but got {indices_elem_ty}")
|
|
|
|
};
|
2024-06-12 14:45:03 +08:00
|
|
|
assert_eq!(
|
|
|
|
indices_elem_ty.get_bit_width(),
|
|
|
|
32,
|
|
|
|
"Expected list[int32] but got list[int{}]",
|
|
|
|
indices_elem_ty.get_bit_width()
|
2024-03-19 00:20:09 +08:00
|
|
|
);
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
unsafe {
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_in_bounds_gep(
|
|
|
|
self.base_ptr(ctx, generator),
|
|
|
|
&[index],
|
|
|
|
name.unwrap_or_default(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
2024-02-15 15:10:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
2024-01-22 16:51:35 +08:00
|
|
|
&self,
|
|
|
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
2024-03-19 18:24:30 +08:00
|
|
|
generator: &mut G,
|
2024-04-16 17:20:24 +08:00
|
|
|
indices: &Index,
|
2024-01-22 16:51:35 +08:00
|
|
|
name: Option<&str>,
|
|
|
|
) -> PointerValue<'ctx> {
|
|
|
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
let indices_size = indices.size(ctx, generator);
|
2024-06-12 14:45:03 +08:00
|
|
|
let nidx_leq_ndims = ctx
|
|
|
|
.builder
|
|
|
|
.build_int_compare(IntPredicate::SLE, indices_size, self.0.load_ndims(ctx), "")
|
|
|
|
.unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
nidx_leq_ndims,
|
|
|
|
"0:IndexError",
|
|
|
|
"invalid index to scalar variable",
|
|
|
|
[None, None, None],
|
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
2024-03-19 00:20:09 +08:00
|
|
|
let indices_len = indices.size(ctx, generator);
|
2024-03-08 13:13:18 +08:00
|
|
|
let ndarray_len = self.0.load_ndims(ctx);
|
|
|
|
let len = call_int_umin(ctx, indices_len, ndarray_len, None);
|
|
|
|
gen_for_callback_incrementing(
|
2024-01-22 16:51:35 +08:00
|
|
|
generator,
|
|
|
|
ctx,
|
2024-07-25 15:54:39 +08:00
|
|
|
None,
|
2024-03-08 13:13:18 +08:00
|
|
|
llvm_usize.const_zero(),
|
|
|
|
(len, false),
|
2024-07-02 19:05:00 +08:00
|
|
|
|generator, ctx, _, i| {
|
2024-01-22 16:51:35 +08:00
|
|
|
let (dim_idx, dim_sz) = unsafe {
|
|
|
|
(
|
2024-04-16 17:20:24 +08:00
|
|
|
indices.get_unchecked(ctx, generator, &i, None).into_int_value(),
|
|
|
|
self.0.dim_sizes().get_typed_unchecked(ctx, generator, &i, None),
|
2024-01-22 16:51:35 +08:00
|
|
|
)
|
|
|
|
};
|
2024-06-12 14:45:03 +08:00
|
|
|
let dim_idx = ctx
|
|
|
|
.builder
|
2024-03-22 16:10:42 +08:00
|
|
|
.build_int_z_extend_or_bit_cast(dim_idx, dim_sz.get_type(), "")
|
|
|
|
.unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
let dim_lt =
|
|
|
|
ctx.builder.build_int_compare(IntPredicate::SLT, dim_idx, dim_sz, "").unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
dim_lt,
|
|
|
|
"0:IndexError",
|
|
|
|
"index {0} is out of bounds for axis 0 with size {1}",
|
|
|
|
[Some(dim_idx), Some(dim_sz), None],
|
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
},
|
2024-03-08 13:13:18 +08:00
|
|
|
llvm_usize.const_int(1, false),
|
2024-06-12 14:45:03 +08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2024-01-22 16:51:35 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
unsafe { self.ptr_offset_unchecked(ctx, generator, indices, name) }
|
2024-01-22 16:51:35 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-19 00:20:09 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> UntypedArrayLikeAccessor<'ctx, Index>
|
|
|
|
for NDArrayDataProxy<'ctx, '_>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> UntypedArrayLikeMutator<'ctx, Index>
|
|
|
|
for NDArrayDataProxy<'ctx, '_>
|
|
|
|
{
|
|
|
|
}
|