core: WIP - int64, uint32 and uint64 works now
This commit is contained in:
parent
5ab59147ca
commit
5f7cac1c97
|
@ -100,15 +100,17 @@ pub fn call_int32<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the `int64` builtin function.
|
/// Invokes the `int64` builtin function.
|
||||||
pub fn call_int64<'ctx>(
|
pub fn call_int64<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
n: (Type, BasicValueEnum<'ctx>),
|
n: (Type, BasicValueEnum<'ctx>),
|
||||||
) -> IntValue<'ctx> {
|
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||||
let llvm_i64 = ctx.ctx.i64_type();
|
let llvm_i64 = ctx.ctx.i64_type();
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
let (n_ty, n) = n;
|
let (n_ty, n) = n;
|
||||||
|
|
||||||
match n.get_type() {
|
Ok(match n.get_type() {
|
||||||
BasicTypeEnum::IntType(int_ty) if matches!(int_ty.get_bit_width(), 1 | 8 | 32) => {
|
BasicTypeEnum::IntType(int_ty) if matches!(int_ty.get_bit_width(), 1 | 8 | 32) => {
|
||||||
debug_assert!([
|
debug_assert!([
|
||||||
ctx.primitives.bool,
|
ctx.primitives.bool,
|
||||||
|
@ -119,10 +121,12 @@ pub fn call_int64<'ctx>(
|
||||||
if ctx.unifier.unioned(n_ty, ctx.primitives.int32) {
|
if ctx.unifier.unioned(n_ty, ctx.primitives.int32) {
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_s_extend(n.into_int_value(), llvm_i64, "sext")
|
.build_int_s_extend(n.into_int_value(), llvm_i64, "sext")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
} else {
|
} else {
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_z_extend(n.into_int_value(), llvm_i64, "zext")
|
.build_int_z_extend(n.into_int_value(), llvm_i64, "zext")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,7 +137,7 @@ pub fn call_int64<'ctx>(
|
||||||
ctx.primitives.uint64,
|
ctx.primitives.uint64,
|
||||||
].iter().any(|ty| ctx.unifier.unioned(n_ty, *ty)));
|
].iter().any(|ty| ctx.unifier.unioned(n_ty, *ty)));
|
||||||
|
|
||||||
n.into_int_value()
|
n
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicTypeEnum::FloatType(_) => {
|
BasicTypeEnum::FloatType(_) => {
|
||||||
|
@ -141,28 +145,49 @@ pub fn call_int64<'ctx>(
|
||||||
|
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_float_to_signed_int(n.into_float_value(), ctx.ctx.i64_type(), "fptosi")
|
.build_float_to_signed_int(n.into_float_value(), ctx.ctx.i64_type(), "fptosi")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BasicTypeEnum::PointerType(_) if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PRIMITIVE_DEF_IDS.ndarray) => {
|
||||||
|
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
|
||||||
|
|
||||||
|
let ndarray = ndarray_elementwise_unaryop_impl(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ctx.primitives.int64,
|
||||||
|
None,
|
||||||
|
NDArrayValue::from_ptr_val(n.into_pointer_value(), llvm_usize, None),
|
||||||
|
|generator, ctx, val| {
|
||||||
|
call_int64(generator, ctx, (elem_ty, val))
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
ndarray.as_ptr_value().into()
|
||||||
|
}
|
||||||
|
|
||||||
_ => unsupported_type(ctx, "int64", &[n_ty])
|
_ => unsupported_type(ctx, "int64", &[n_ty])
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the `uint32` builtin function.
|
/// Invokes the `uint32` builtin function.
|
||||||
pub fn call_uint32<'ctx>(
|
pub fn call_uint32<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
n: (Type, BasicValueEnum<'ctx>),
|
n: (Type, BasicValueEnum<'ctx>),
|
||||||
) -> IntValue<'ctx> {
|
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||||
let llvm_i32 = ctx.ctx.i32_type();
|
let llvm_i32 = ctx.ctx.i32_type();
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
let (n_ty, n) = n;
|
let (n_ty, n) = n;
|
||||||
|
|
||||||
match n.get_type() {
|
Ok(match n.get_type() {
|
||||||
BasicTypeEnum::IntType(int_ty) if matches!(int_ty.get_bit_width(), 1 | 8) => {
|
BasicTypeEnum::IntType(int_ty) if matches!(int_ty.get_bit_width(), 1 | 8) => {
|
||||||
debug_assert!(ctx.unifier.unioned(n_ty, ctx.primitives.bool));
|
debug_assert!(ctx.unifier.unioned(n_ty, ctx.primitives.bool));
|
||||||
|
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_z_extend(n.into_int_value(), llvm_i32, "zext")
|
.build_int_z_extend(n.into_int_value(), llvm_i32, "zext")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,7 +197,7 @@ pub fn call_uint32<'ctx>(
|
||||||
ctx.primitives.uint32,
|
ctx.primitives.uint32,
|
||||||
].iter().any(|ty| ctx.unifier.unioned(n_ty, *ty)));
|
].iter().any(|ty| ctx.unifier.unioned(n_ty, *ty)));
|
||||||
|
|
||||||
n.into_int_value()
|
n
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicTypeEnum::IntType(int_ty) if int_ty.get_bit_width() == 64 => {
|
BasicTypeEnum::IntType(int_ty) if int_ty.get_bit_width() == 64 => {
|
||||||
|
@ -183,6 +208,7 @@ pub fn call_uint32<'ctx>(
|
||||||
|
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_truncate(n.into_int_value(), llvm_i32, "trunc")
|
.build_int_truncate(n.into_int_value(), llvm_i32, "trunc")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,24 +234,42 @@ pub fn call_uint32<'ctx>(
|
||||||
to_int32,
|
to_int32,
|
||||||
"conv",
|
"conv",
|
||||||
)
|
)
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BasicTypeEnum::PointerType(_) if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PRIMITIVE_DEF_IDS.ndarray) => {
|
||||||
|
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
|
||||||
|
|
||||||
|
let ndarray = ndarray_elementwise_unaryop_impl(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ctx.primitives.uint32,
|
||||||
|
None,
|
||||||
|
NDArrayValue::from_ptr_val(n.into_pointer_value(), llvm_usize, None),
|
||||||
|
|generator, ctx, val| {
|
||||||
|
call_uint32(generator, ctx, (elem_ty, val))
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
ndarray.as_ptr_value().into()
|
||||||
|
}
|
||||||
|
|
||||||
_ => unsupported_type(ctx, "uint32", &[n_ty])
|
_ => unsupported_type(ctx, "uint32", &[n_ty])
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the `uint64` builtin function.
|
/// Invokes the `uint64` builtin function.
|
||||||
pub fn call_uint64<'ctx>(
|
pub fn call_uint64<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
n: (Type, BasicValueEnum<'ctx>),
|
n: (Type, BasicValueEnum<'ctx>),
|
||||||
) -> IntValue<'ctx> {
|
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||||
let llvm_i64 = ctx.ctx.i64_type();
|
let llvm_i64 = ctx.ctx.i64_type();
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
let (n_ty, n) = n;
|
let (n_ty, n) = n;
|
||||||
|
|
||||||
match n.get_type() {
|
Ok(match n.get_type() {
|
||||||
BasicTypeEnum::IntType(int_ty) if matches!(int_ty.get_bit_width(), 1 | 8 | 32) => {
|
BasicTypeEnum::IntType(int_ty) if matches!(int_ty.get_bit_width(), 1 | 8 | 32) => {
|
||||||
debug_assert!([
|
debug_assert!([
|
||||||
ctx.primitives.bool,
|
ctx.primitives.bool,
|
||||||
|
@ -236,10 +280,12 @@ pub fn call_uint64<'ctx>(
|
||||||
if ctx.unifier.unioned(n_ty, ctx.primitives.int32) {
|
if ctx.unifier.unioned(n_ty, ctx.primitives.int32) {
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_s_extend(n.into_int_value(), llvm_i64, "sext")
|
.build_int_s_extend(n.into_int_value(), llvm_i64, "sext")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
} else {
|
} else {
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_int_z_extend(n.into_int_value(), llvm_i64, "zext")
|
.build_int_z_extend(n.into_int_value(), llvm_i64, "zext")
|
||||||
|
.map(Into::into)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -250,7 +296,7 @@ pub fn call_uint64<'ctx>(
|
||||||
ctx.primitives.uint64,
|
ctx.primitives.uint64,
|
||||||
].iter().any(|ty| ctx.unifier.unioned(n_ty, *ty)));
|
].iter().any(|ty| ctx.unifier.unioned(n_ty, *ty)));
|
||||||
|
|
||||||
n.into_int_value()
|
n
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicTypeEnum::FloatType(_) => {
|
BasicTypeEnum::FloatType(_) => {
|
||||||
|
@ -270,12 +316,28 @@ pub fn call_uint64<'ctx>(
|
||||||
|
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_select(val_gez, to_uint64, to_int64, "conv")
|
.build_select(val_gez, to_uint64, to_int64, "conv")
|
||||||
.map(BasicValueEnum::into_int_value)
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BasicTypeEnum::PointerType(_) if n_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PRIMITIVE_DEF_IDS.ndarray) => {
|
||||||
|
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, n_ty);
|
||||||
|
|
||||||
|
let ndarray = ndarray_elementwise_unaryop_impl(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ctx.primitives.uint64,
|
||||||
|
None,
|
||||||
|
NDArrayValue::from_ptr_val(n.into_pointer_value(), llvm_usize, None),
|
||||||
|
|generator, ctx, val| {
|
||||||
|
call_uint64(generator, ctx, (elem_ty, val))
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
ndarray.as_ptr_value().into()
|
||||||
|
}
|
||||||
|
|
||||||
_ => unsupported_type(ctx, "uint64", &[n_ty])
|
_ => unsupported_type(ctx, "uint64", &[n_ty])
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the `float` builtin function.
|
/// Invokes the `float` builtin function.
|
||||||
|
|
|
@ -596,8 +596,8 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
name: "int64".into(),
|
name: "int64".into(),
|
||||||
simple_name: "int64".into(),
|
simple_name: "int64".into(),
|
||||||
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
args: vec![FuncArg { name: "n".into(), ty: num_ty.0, default_value: None }],
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
||||||
ret: int64,
|
ret: num_or_ndarray_ty.0,
|
||||||
vars: var_map.clone(),
|
vars: var_map.clone(),
|
||||||
})),
|
})),
|
||||||
var_id: Vec::default(),
|
var_id: Vec::default(),
|
||||||
|
@ -609,7 +609,7 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
Ok(Some(builtin_fns::call_int64(ctx, (arg_ty, arg)).into()))
|
Ok(Some(builtin_fns::call_int64(generator, ctx, (arg_ty, arg))?))
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
@ -618,8 +618,8 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
name: "uint32".into(),
|
name: "uint32".into(),
|
||||||
simple_name: "uint32".into(),
|
simple_name: "uint32".into(),
|
||||||
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
args: vec![FuncArg { name: "n".into(), ty: num_ty.0, default_value: None }],
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
||||||
ret: uint32,
|
ret: num_or_ndarray_ty.0,
|
||||||
vars: var_map.clone(),
|
vars: var_map.clone(),
|
||||||
})),
|
})),
|
||||||
var_id: Vec::default(),
|
var_id: Vec::default(),
|
||||||
|
@ -631,7 +631,7 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
Ok(Some(builtin_fns::call_uint32(ctx, (arg_ty, arg)).into()))
|
Ok(Some(builtin_fns::call_uint32(generator, ctx, (arg_ty, arg))?))
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
@ -640,8 +640,8 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
name: "uint64".into(),
|
name: "uint64".into(),
|
||||||
simple_name: "uint64".into(),
|
simple_name: "uint64".into(),
|
||||||
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
args: vec![FuncArg { name: "n".into(), ty: num_ty.0, default_value: None }],
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
||||||
ret: uint64,
|
ret: num_or_ndarray_ty.0,
|
||||||
vars: var_map.clone(),
|
vars: var_map.clone(),
|
||||||
})),
|
})),
|
||||||
var_id: Vec::default(),
|
var_id: Vec::default(),
|
||||||
|
@ -653,7 +653,7 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
Ok(Some(builtin_fns::call_uint64(ctx, (arg_ty, arg)).into()))
|
Ok(Some(builtin_fns::call_uint64(generator, ctx, (arg_ty, arg))?))
|
||||||
},
|
},
|
||||||
)))),
|
)))),
|
||||||
loc: None,
|
loc: None,
|
||||||
|
|
|
@ -14,17 +14,7 @@ use crate::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use itertools::{Itertools, izip};
|
use itertools::{Itertools, izip};
|
||||||
use nac3parser::ast::{
|
use nac3parser::ast::{self, fold::{self, Fold}, Arguments, Comprehension, ExprContext, ExprKind, Located, Location, StrRef};
|
||||||
self,
|
|
||||||
fold::{self, Fold},
|
|
||||||
Arguments,
|
|
||||||
Comprehension,
|
|
||||||
ExprContext,
|
|
||||||
ExprKind,
|
|
||||||
Located,
|
|
||||||
Location,
|
|
||||||
StrRef
|
|
||||||
};
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test;
|
mod test;
|
||||||
|
@ -862,20 +852,17 @@ impl<'a> Inferencer<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if id == &"int32".into() && args.len() == 1 {
|
if id == &"int32".into() && args.len() == 1 {
|
||||||
|
let int32_ty = self.primitives.int32;
|
||||||
|
|
||||||
let arg0 = self.fold_expr(args.remove(0))?;
|
let arg0 = self.fold_expr(args.remove(0))?;
|
||||||
let arg0_ty = arg0.custom.unwrap();
|
let arg0_ty = arg0.custom.unwrap();
|
||||||
|
|
||||||
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PRIMITIVE_DEF_IDS.ndarray) {
|
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PRIMITIVE_DEF_IDS.ndarray) {
|
||||||
let (_, ndarray_ndims) = unpack_ndarray_var_tys(self.unifier, arg0_ty);
|
let (_, ndarray_ndims) = unpack_ndarray_var_tys(self.unifier, arg0_ty);
|
||||||
|
|
||||||
make_ndarray_ty(
|
make_ndarray_ty(self.unifier, self.primitives, Some(int32_ty), Some(ndarray_ndims))
|
||||||
self.unifier,
|
|
||||||
self.primitives,
|
|
||||||
Some(self.primitives.int32),
|
|
||||||
Some(ndarray_ndims),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
self.primitives.int32
|
int32_ty
|
||||||
};
|
};
|
||||||
|
|
||||||
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
|
@ -905,66 +892,83 @@ impl<'a> Inferencer<'a> {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// int64 is special because its argument can be a constant larger than int32
|
// int64, uint32 and uint64 are special because their argument can be a constant outside the
|
||||||
if id == &"int64".into() && args.len() == 1 {
|
// range of int32s
|
||||||
if let ExprKind::Constant { value: ast::Constant::Int(val), kind } =
|
if [
|
||||||
&args[0].node
|
"int64",
|
||||||
{
|
"uint32",
|
||||||
let custom = Some(self.primitives.int64);
|
"uint64",
|
||||||
let v: Result<i64, _> = (*val).try_into();
|
].iter().any(|fun_id| id == &(*fun_id).into()) && args.len() == 1 {
|
||||||
return if v.is_ok() {
|
let target_ty = if id == &"int64".into() {
|
||||||
Ok(Some(Located {
|
self.primitives.int64
|
||||||
location: args[0].location,
|
} else if id == &"uint32".into() {
|
||||||
custom,
|
self.primitives.uint32
|
||||||
node: ExprKind::Constant {
|
} else if id == &"uint64".into() {
|
||||||
value: ast::Constant::Int(*val),
|
self.primitives.uint64
|
||||||
kind: kind.clone(),
|
} else { unreachable!() };
|
||||||
},
|
|
||||||
}))
|
let arg0 = self.fold_expr(args.remove(0))?;
|
||||||
} else {
|
let arg0_ty = arg0.custom.unwrap();
|
||||||
report_error("Integer out of bound", args[0].location)
|
|
||||||
|
let ret = if arg0_ty.obj_id(self.unifier).is_some_and(|id| id == PRIMITIVE_DEF_IDS.ndarray) {
|
||||||
|
let (_, ndarray_ndims) = unpack_ndarray_var_tys(self.unifier, arg0_ty);
|
||||||
|
|
||||||
|
make_ndarray_ty(self.unifier, self.primitives, Some(target_ty), Some(ndarray_ndims))
|
||||||
|
} else {
|
||||||
|
if let ExprKind::Constant {
|
||||||
|
value: ast::Constant::Int(val),
|
||||||
|
kind
|
||||||
|
} = &arg0.node {
|
||||||
|
let conv_is_ok = if self.unifier.unioned(target_ty, self.primitives.int64) {
|
||||||
|
i64::try_from(*val).is_ok()
|
||||||
|
} else if self.unifier.unioned(target_ty, self.primitives.uint32) {
|
||||||
|
u32::try_from(*val).is_ok()
|
||||||
|
} else if self.unifier.unioned(target_ty, self.primitives.uint64) {
|
||||||
|
u64::try_from(*val).is_ok()
|
||||||
|
} else { unreachable!() };
|
||||||
|
|
||||||
|
return if conv_is_ok {
|
||||||
|
Ok(Some(Located {
|
||||||
|
location: arg0.location,
|
||||||
|
custom: Some(target_ty),
|
||||||
|
node: ExprKind::Constant {
|
||||||
|
value: ast::Constant::Int(*val),
|
||||||
|
kind: kind.clone(),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
report_error("Integer out of bound", arg0.location)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
target_ty
|
||||||
if id == &"uint32".into() && args.len() == 1 {
|
};
|
||||||
if let ExprKind::Constant { value: ast::Constant::Int(val), kind } =
|
|
||||||
&args[0].node
|
let custom = self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
||||||
{
|
args: vec![
|
||||||
let custom = Some(self.primitives.uint32);
|
FuncArg {
|
||||||
let v: Result<u32, _> = (*val).try_into();
|
name: "n".into(),
|
||||||
return if v.is_ok() {
|
ty: arg0.custom.unwrap(),
|
||||||
Ok(Some(Located {
|
default_value: None,
|
||||||
location: args[0].location,
|
},
|
||||||
custom,
|
],
|
||||||
node: ExprKind::Constant {
|
ret,
|
||||||
value: ast::Constant::Int(*val),
|
vars: VarMap::new(),
|
||||||
kind: kind.clone(),
|
}));
|
||||||
},
|
|
||||||
}))
|
return Ok(Some(Located {
|
||||||
} else {
|
location,
|
||||||
report_error("Integer out of bound", args[0].location)
|
custom: Some(ret),
|
||||||
}
|
node: ExprKind::Call {
|
||||||
}
|
func: Box::new(Located {
|
||||||
}
|
custom: Some(custom),
|
||||||
if id == &"uint64".into() && args.len() == 1 {
|
location: func.location,
|
||||||
if let ExprKind::Constant { value: ast::Constant::Int(val), kind } =
|
node: ExprKind::Name { id: *id, ctx: ctx.clone() },
|
||||||
&args[0].node
|
}),
|
||||||
{
|
args: vec![arg0],
|
||||||
let custom = Some(self.primitives.uint64);
|
keywords: vec![],
|
||||||
let v: Result<u64, _> = (*val).try_into();
|
},
|
||||||
return if v.is_ok() {
|
}))
|
||||||
Ok(Some(Located {
|
|
||||||
location: args[0].location,
|
|
||||||
custom,
|
|
||||||
node: ExprKind::Constant {
|
|
||||||
value: ast::Constant::Int(*val),
|
|
||||||
kind: kind.clone(),
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
} else {
|
|
||||||
report_error("Integer out of bound", args[0].location)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1-argument ndarray n-dimensional creation functions
|
// 1-argument ndarray n-dimensional creation functions
|
||||||
|
|
|
@ -6,6 +6,18 @@ def output_bool(x: bool):
|
||||||
def output_int32(x: int32):
|
def output_int32(x: int32):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_int64(x: int64):
|
||||||
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_uint32(x: uint32):
|
||||||
|
...
|
||||||
|
|
||||||
|
@extern
|
||||||
|
def output_uint64(x: uint64):
|
||||||
|
...
|
||||||
|
|
||||||
@extern
|
@extern
|
||||||
def output_float64(x: float):
|
def output_float64(x: float):
|
||||||
...
|
...
|
||||||
|
@ -24,6 +36,21 @@ def output_ndarray_int32_2(n: ndarray[int32, Literal[2]]):
|
||||||
for c in range(len(n[r])):
|
for c in range(len(n[r])):
|
||||||
output_int32(n[r][c])
|
output_int32(n[r][c])
|
||||||
|
|
||||||
|
def output_ndarray_int64_2(n: ndarray[int64, Literal[2]]):
|
||||||
|
for r in range(len(n)):
|
||||||
|
for c in range(len(n[r])):
|
||||||
|
output_int64(n[r][c])
|
||||||
|
|
||||||
|
def output_ndarray_uint32_2(n: ndarray[uint32, Literal[2]]):
|
||||||
|
for r in range(len(n)):
|
||||||
|
for c in range(len(n[r])):
|
||||||
|
output_uint32(n[r][c])
|
||||||
|
|
||||||
|
def output_ndarray_uint64_2(n: ndarray[uint64, Literal[2]]):
|
||||||
|
for r in range(len(n)):
|
||||||
|
for c in range(len(n[r])):
|
||||||
|
output_uint64(n[r][c])
|
||||||
|
|
||||||
def output_ndarray_float_1(n: ndarray[float, Literal[1]]):
|
def output_ndarray_float_1(n: ndarray[float, Literal[1]]):
|
||||||
for i in range(len(n)):
|
for i in range(len(n)):
|
||||||
output_float64(n[i])
|
output_float64(n[i])
|
||||||
|
@ -656,6 +683,27 @@ def test_ndarray_int32():
|
||||||
output_ndarray_float_2(x)
|
output_ndarray_float_2(x)
|
||||||
output_ndarray_int32_2(y)
|
output_ndarray_int32_2(y)
|
||||||
|
|
||||||
|
def test_ndarray_int64():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = int64(x)
|
||||||
|
|
||||||
|
output_ndarray_float_2(x)
|
||||||
|
output_ndarray_int64_2(y)
|
||||||
|
|
||||||
|
def test_ndarray_uint32():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = uint32(x)
|
||||||
|
|
||||||
|
output_ndarray_float_2(x)
|
||||||
|
output_ndarray_uint32_2(y)
|
||||||
|
|
||||||
|
def test_ndarray_uint64():
|
||||||
|
x = np_identity(2)
|
||||||
|
y = uint64(x)
|
||||||
|
|
||||||
|
output_ndarray_float_2(x)
|
||||||
|
output_ndarray_uint64_2(y)
|
||||||
|
|
||||||
def run() -> int32:
|
def run() -> int32:
|
||||||
test_ndarray_ctor()
|
test_ndarray_ctor()
|
||||||
test_ndarray_empty()
|
test_ndarray_empty()
|
||||||
|
@ -747,5 +795,8 @@ def run() -> int32:
|
||||||
test_ndarray_ge_broadcast_rhs_scalar()
|
test_ndarray_ge_broadcast_rhs_scalar()
|
||||||
|
|
||||||
test_ndarray_int32()
|
test_ndarray_int32()
|
||||||
|
test_ndarray_int64()
|
||||||
|
test_ndarray_uint32()
|
||||||
|
test_ndarray_uint64()
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in New Issue