2024-05-08 17:42:19 +08:00
|
|
|
use std::iter::once;
|
|
|
|
|
|
|
|
use indexmap::IndexMap;
|
|
|
|
use inkwell::{
|
|
|
|
attributes::{Attribute, AttributeLoc},
|
|
|
|
IntPredicate,
|
|
|
|
types::{BasicMetadataTypeEnum, BasicType},
|
|
|
|
values::{BasicMetadataValueEnum, BasicValue, CallSiteValue}
|
|
|
|
};
|
|
|
|
use itertools::Either;
|
|
|
|
|
2022-01-09 19:55:17 +08:00
|
|
|
use crate::{
|
2022-02-21 18:27:46 +08:00
|
|
|
codegen::{
|
2024-04-24 17:40:25 +08:00
|
|
|
builtin_fns,
|
2024-06-07 13:00:42 +08:00
|
|
|
classes::{
|
|
|
|
ArrayLikeValue,
|
|
|
|
NDArrayValue,
|
|
|
|
ProxyType,
|
|
|
|
ProxyValue,
|
|
|
|
RangeValue,
|
|
|
|
RangeType,
|
|
|
|
TypedArrayLikeAccessor,
|
|
|
|
},
|
2023-10-10 16:56:38 +08:00
|
|
|
expr::destructure_range,
|
2024-02-22 01:47:26 +08:00
|
|
|
irrt::*,
|
2024-03-11 14:47:01 +08:00
|
|
|
numpy::*,
|
2023-10-10 16:56:38 +08:00
|
|
|
stmt::exn_constructor,
|
2022-02-21 18:27:46 +08:00
|
|
|
},
|
2022-01-09 19:55:17 +08:00
|
|
|
symbol_resolver::SymbolValue,
|
2024-03-11 14:47:01 +08:00
|
|
|
toplevel::{
|
|
|
|
helper::PRIMITIVE_DEF_IDS,
|
|
|
|
numpy::make_ndarray_ty,
|
|
|
|
},
|
2024-03-04 23:38:52 +08:00
|
|
|
typecheck::typedef::VarMap,
|
2022-01-09 19:55:17 +08:00
|
|
|
};
|
2024-05-08 17:42:19 +08:00
|
|
|
|
|
|
|
use super::*;
|
2021-12-01 02:52:00 +08:00
|
|
|
|
2023-11-28 17:37:49 +08:00
|
|
|
type BuiltinInfo = Vec<(Arc<RwLock<TopLevelDef>>, Option<Stmt>)>;
|
2021-12-01 03:23:58 +08:00
|
|
|
|
2022-03-16 23:42:08 +08:00
|
|
|
pub fn get_exn_constructor(
|
|
|
|
name: &str,
|
|
|
|
class_id: usize,
|
|
|
|
cons_id: usize,
|
|
|
|
unifier: &mut Unifier,
|
|
|
|
primitives: &PrimitiveStore
|
|
|
|
)-> (TopLevelDef, TopLevelDef, Type, Type) {
|
|
|
|
let int32 = primitives.int32;
|
|
|
|
let int64 = primitives.int64;
|
|
|
|
let string = primitives.str;
|
|
|
|
let exception_fields = vec![
|
|
|
|
("__name__".into(), int32, true),
|
|
|
|
("__file__".into(), string, true),
|
|
|
|
("__line__".into(), int32, true),
|
|
|
|
("__col__".into(), int32, true),
|
|
|
|
("__func__".into(), string, true),
|
|
|
|
("__message__".into(), string, true),
|
|
|
|
("__param0__".into(), int64, true),
|
|
|
|
("__param1__".into(), int64, true),
|
|
|
|
("__param2__".into(), int64, true),
|
|
|
|
];
|
|
|
|
let exn_cons_args = vec![
|
|
|
|
FuncArg {
|
|
|
|
name: "msg".into(),
|
|
|
|
ty: string,
|
2023-12-08 17:43:32 +08:00
|
|
|
default_value: Some(SymbolValue::Str(String::new())),
|
2022-03-16 23:42:08 +08:00
|
|
|
},
|
|
|
|
FuncArg { name: "param0".into(), ty: int64, default_value: Some(SymbolValue::I64(0)) },
|
|
|
|
FuncArg { name: "param1".into(), ty: int64, default_value: Some(SymbolValue::I64(0)) },
|
|
|
|
FuncArg { name: "param2".into(), ty: int64, default_value: Some(SymbolValue::I64(0)) },
|
|
|
|
];
|
|
|
|
let exn_type = unifier.add_ty(TypeEnum::TObj {
|
|
|
|
obj_id: DefinitionId(class_id),
|
|
|
|
fields: exception_fields.iter().map(|(a, b, c)| (*a, (*b, *c))).collect(),
|
2024-03-04 23:38:52 +08:00
|
|
|
params: VarMap::default(),
|
2022-03-16 23:42:08 +08:00
|
|
|
});
|
|
|
|
let signature = unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: exn_cons_args,
|
|
|
|
ret: exn_type,
|
2024-03-04 23:38:52 +08:00
|
|
|
vars: VarMap::default(),
|
2022-03-16 23:42:08 +08:00
|
|
|
}));
|
|
|
|
let fun_def = TopLevelDef::Function {
|
2023-12-08 17:43:32 +08:00
|
|
|
name: format!("{name}.__init__"),
|
2022-03-16 23:42:08 +08:00
|
|
|
simple_name: "__init__".into(),
|
|
|
|
signature,
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-16 23:42:08 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(exn_constructor)))),
|
|
|
|
loc: None,
|
|
|
|
};
|
|
|
|
let class_def = TopLevelDef::Class {
|
|
|
|
name: name.into(),
|
|
|
|
object_id: DefinitionId(class_id),
|
2023-12-08 17:43:32 +08:00
|
|
|
type_vars: Vec::default(),
|
2022-03-16 23:42:08 +08:00
|
|
|
fields: exception_fields,
|
|
|
|
methods: vec![("__init__".into(), signature, DefinitionId(cons_id))],
|
|
|
|
ancestors: vec![
|
2023-12-08 17:43:32 +08:00
|
|
|
TypeAnnotation::CustomClass { id: DefinitionId(class_id), params: Vec::default() },
|
2024-02-26 15:11:00 +08:00
|
|
|
TypeAnnotation::CustomClass { id: PRIMITIVE_DEF_IDS.exception, params: Vec::default() },
|
2022-03-16 23:42:08 +08:00
|
|
|
],
|
|
|
|
constructor: Some(signature),
|
|
|
|
resolver: None,
|
|
|
|
loc: None,
|
|
|
|
};
|
|
|
|
(fun_def, class_def, signature, exn_type)
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Creates a NumPy [`TopLevelDef`] function by code generation.
|
2023-10-10 14:56:16 +08:00
|
|
|
///
|
|
|
|
/// * `name`: The name of the implemented NumPy function.
|
|
|
|
/// * `ret_ty`: The return type of this function.
|
|
|
|
/// * `param_ty`: The parameters accepted by this function, represented by a tuple of the
|
|
|
|
/// [parameter type][Type] and the parameter symbol name.
|
|
|
|
/// * `codegen_callback`: A lambda generating LLVM IR for the implementation of this function.
|
|
|
|
fn create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier: &mut Unifier,
|
2024-03-04 23:38:52 +08:00
|
|
|
var_map: &VarMap,
|
2023-10-10 14:56:16 +08:00
|
|
|
name: &'static str,
|
|
|
|
ret_ty: Type,
|
|
|
|
param_ty: &[(Type, &'static str)],
|
2024-04-23 14:35:11 +08:00
|
|
|
codegen_callback: Box<GenCallCallback>,
|
2023-10-10 14:56:16 +08:00
|
|
|
) -> Arc<RwLock<TopLevelDef>> {
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: name.into(),
|
|
|
|
simple_name: name.into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2023-10-10 14:56:16 +08:00
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
2023-12-06 11:49:02 +08:00
|
|
|
ret: ret_ty,
|
2023-10-10 14:56:16 +08:00
|
|
|
vars: var_map.clone(),
|
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2023-10-10 14:56:16 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(codegen_callback))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Creates a NumPy [`TopLevelDef`] function using an LLVM intrinsic.
|
2023-10-10 14:56:16 +08:00
|
|
|
///
|
|
|
|
/// * `name`: The name of the implemented NumPy function.
|
|
|
|
/// * `ret_ty`: The return type of this function.
|
|
|
|
/// * `param_ty`: The parameters accepted by this function, represented by a tuple of the
|
|
|
|
/// [parameter type][Type] and the parameter symbol name.
|
|
|
|
/// * `intrinsic_fn`: The fully-qualified name of the LLVM intrinsic function.
|
|
|
|
fn create_fn_by_intrinsic(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier: &mut Unifier,
|
2024-03-04 23:38:52 +08:00
|
|
|
var_map: &VarMap,
|
2023-10-10 14:56:16 +08:00
|
|
|
name: &'static str,
|
|
|
|
ret_ty: Type,
|
|
|
|
params: &[(Type, &'static str)],
|
|
|
|
intrinsic_fn: &'static str,
|
|
|
|
) -> Arc<RwLock<TopLevelDef>> {
|
|
|
|
let param_tys = params.iter()
|
|
|
|
.map(|p| p.0)
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2023-10-10 14:56:16 +08:00
|
|
|
var_map,
|
|
|
|
name,
|
|
|
|
ret_ty,
|
|
|
|
params,
|
|
|
|
Box::new(move |ctx, _, fun, args, generator| {
|
|
|
|
let args_ty = fun.0.args.iter().map(|a| a.ty).collect_vec();
|
|
|
|
|
|
|
|
assert!(param_tys.iter().zip(&args_ty)
|
|
|
|
.all(|(expected, actual)| ctx.unifier.unioned(*expected, *actual)));
|
|
|
|
|
|
|
|
let args_val = args_ty.iter().zip_eq(args.iter())
|
|
|
|
.map(|(ty, arg)| {
|
|
|
|
arg.1.clone()
|
2023-12-06 11:49:02 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, *ty)
|
2023-10-10 14:56:16 +08:00
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.map_into::<BasicMetadataValueEnum>()
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
let intrinsic_fn = ctx.module.get_function(intrinsic_fn).unwrap_or_else(|| {
|
2023-12-06 11:49:02 +08:00
|
|
|
let ret_llvm_ty = ctx.get_llvm_abi_type(generator, ret_ty);
|
2023-10-10 14:56:16 +08:00
|
|
|
let param_llvm_ty = param_tys.iter()
|
|
|
|
.map(|p| ctx.get_llvm_abi_type(generator, *p))
|
|
|
|
.map_into::<BasicMetadataTypeEnum>()
|
|
|
|
.collect_vec();
|
|
|
|
let fn_type = ret_llvm_ty.fn_type(param_llvm_ty.as_slice(), false);
|
|
|
|
|
|
|
|
ctx.module.add_function(intrinsic_fn, fn_type, None)
|
|
|
|
});
|
|
|
|
|
2024-02-19 19:30:25 +08:00
|
|
|
let val = ctx.builder
|
|
|
|
.build_call(intrinsic_fn, args_val.as_slice(), name)
|
|
|
|
.map(CallSiteValue::try_as_basic_value)
|
|
|
|
.map(Either::unwrap_left)
|
2023-10-10 14:56:16 +08:00
|
|
|
.unwrap();
|
|
|
|
Ok(val.into())
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-08 17:43:32 +08:00
|
|
|
/// Creates a unary NumPy [`TopLevelDef`] function using an extern function (e.g. from `libc` or
|
2023-10-10 14:56:16 +08:00
|
|
|
/// `libm`).
|
|
|
|
///
|
|
|
|
/// * `name`: The name of the implemented NumPy function.
|
|
|
|
/// * `ret_ty`: The return type of this function.
|
|
|
|
/// * `param_ty`: The parameters accepted by this function, represented by a tuple of the
|
|
|
|
/// [parameter type][Type] and the parameter symbol name.
|
|
|
|
/// * `extern_fn`: The fully-qualified name of the extern function used as the implementation.
|
|
|
|
/// * `attrs`: The list of attributes to apply to this function declaration. Note that `nounwind` is
|
|
|
|
/// already implied by the C ABI.
|
|
|
|
fn create_fn_by_extern(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier: &mut Unifier,
|
2024-03-04 23:38:52 +08:00
|
|
|
var_map: &VarMap,
|
2023-10-10 14:56:16 +08:00
|
|
|
name: &'static str,
|
|
|
|
ret_ty: Type,
|
|
|
|
params: &[(Type, &'static str)],
|
|
|
|
extern_fn: &'static str,
|
|
|
|
attrs: &'static [&str],
|
|
|
|
) -> Arc<RwLock<TopLevelDef>> {
|
|
|
|
let param_tys = params.iter()
|
|
|
|
.map(|p| p.0)
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2023-10-10 14:56:16 +08:00
|
|
|
var_map,
|
|
|
|
name,
|
|
|
|
ret_ty,
|
|
|
|
params,
|
|
|
|
Box::new(move |ctx, _, fun, args, generator| {
|
|
|
|
let args_ty = fun.0.args.iter().map(|a| a.ty).collect_vec();
|
|
|
|
|
|
|
|
assert!(param_tys.iter().zip(&args_ty)
|
|
|
|
.all(|(expected, actual)| ctx.unifier.unioned(*expected, *actual)));
|
|
|
|
|
|
|
|
let args_val = args_ty.iter().zip_eq(args.iter())
|
|
|
|
.map(|(ty, arg)| {
|
|
|
|
arg.1.clone()
|
2023-12-06 11:49:02 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, *ty)
|
2023-10-10 14:56:16 +08:00
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
.map_into::<BasicMetadataValueEnum>()
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
let intrinsic_fn = ctx.module.get_function(extern_fn).unwrap_or_else(|| {
|
2023-12-06 11:49:02 +08:00
|
|
|
let ret_llvm_ty = ctx.get_llvm_abi_type(generator, ret_ty);
|
2023-10-10 14:56:16 +08:00
|
|
|
let param_llvm_ty = param_tys.iter()
|
|
|
|
.map(|p| ctx.get_llvm_abi_type(generator, *p))
|
|
|
|
.map_into::<BasicMetadataTypeEnum>()
|
|
|
|
.collect_vec();
|
|
|
|
let fn_type = ret_llvm_ty.fn_type(param_llvm_ty.as_slice(), false);
|
|
|
|
let func = ctx.module.add_function(extern_fn, fn_type, None);
|
|
|
|
func.add_attribute(
|
|
|
|
AttributeLoc::Function,
|
|
|
|
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id("nounwind"), 0)
|
|
|
|
);
|
|
|
|
|
|
|
|
for attr in attrs {
|
|
|
|
func.add_attribute(
|
|
|
|
AttributeLoc::Function,
|
|
|
|
ctx.ctx.create_enum_attribute(Attribute::get_named_enum_kind_id(attr), 0)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
func
|
|
|
|
});
|
|
|
|
|
2024-02-19 19:30:25 +08:00
|
|
|
let val = ctx.builder
|
|
|
|
.build_call(intrinsic_fn, &args_val, name)
|
|
|
|
.map(CallSiteValue::try_as_basic_value)
|
|
|
|
.map(Either::unwrap_left)
|
2023-10-10 14:56:16 +08:00
|
|
|
.unwrap();
|
|
|
|
Ok(val.into())
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-04-29 22:03:13 +08:00
|
|
|
pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> BuiltinInfo {
|
|
|
|
let PrimitiveStore {
|
|
|
|
int32,
|
|
|
|
int64,
|
|
|
|
uint32,
|
|
|
|
uint64,
|
|
|
|
float,
|
|
|
|
bool: boolean,
|
|
|
|
range,
|
|
|
|
str: string,
|
|
|
|
ndarray,
|
|
|
|
..
|
|
|
|
} = *primitives;
|
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), None);
|
2023-11-27 13:25:53 +08:00
|
|
|
let ndarray_float_2d = {
|
2024-04-29 22:03:13 +08:00
|
|
|
let value = match primitives.size_t {
|
2023-11-27 13:25:53 +08:00
|
|
|
64 => SymbolValue::U64(2u64),
|
|
|
|
32 => SymbolValue::U32(2u32),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2024-04-29 22:03:13 +08:00
|
|
|
let ndims = unifier.add_ty(TypeEnum::TLiteral {
|
2023-11-27 13:25:53 +08:00
|
|
|
values: vec![value],
|
|
|
|
loc: None,
|
|
|
|
});
|
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
make_ndarray_ty(unifier, primitives, Some(float), Some(ndims))
|
2023-11-27 13:25:53 +08:00
|
|
|
};
|
2024-04-29 22:03:13 +08:00
|
|
|
let list_int32 = unifier.add_ty(TypeEnum::TList { ty: int32 });
|
|
|
|
let num_ty = unifier.get_fresh_var_with_range(
|
2022-03-05 03:45:09 +08:00
|
|
|
&[int32, int64, float, boolean, uint32, uint64],
|
2022-02-21 18:27:46 +08:00
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
2024-04-25 15:47:16 +08:00
|
|
|
let num_var_map: VarMap = vec![
|
|
|
|
(num_ty.1, num_ty.0),
|
|
|
|
].into_iter().collect();
|
|
|
|
|
|
|
|
let new_type_or_ndarray_ty = |unifier: &mut Unifier, primitives: &PrimitiveStore, scalar_ty: Type| {
|
|
|
|
let ndarray = make_ndarray_ty(unifier, primitives, Some(scalar_ty), None);
|
|
|
|
|
|
|
|
unifier.get_fresh_var_with_range(
|
|
|
|
&[scalar_ty, ndarray],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let ndarray_num_ty = make_ndarray_ty(unifier, primitives, Some(num_ty.0), None);
|
|
|
|
let float_or_ndarray_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let float_or_ndarray_var_map: VarMap = vec![
|
|
|
|
(float_or_ndarray_ty.1, float_or_ndarray_ty.0),
|
|
|
|
].into_iter().collect();
|
|
|
|
|
|
|
|
let num_or_ndarray_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[num_ty.0, ndarray_num_ty],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let num_or_ndarray_var_map: VarMap = vec![
|
|
|
|
(num_ty.1, num_ty.0),
|
|
|
|
(num_or_ndarray_ty.1, num_or_ndarray_ty.0),
|
|
|
|
].into_iter().collect();
|
2024-03-13 11:16:23 +08:00
|
|
|
|
2022-02-12 21:09:23 +08:00
|
|
|
let exception_fields = vec![
|
|
|
|
("__name__".into(), int32, true),
|
|
|
|
("__file__".into(), string, true),
|
|
|
|
("__line__".into(), int32, true),
|
|
|
|
("__col__".into(), int32, true),
|
|
|
|
("__func__".into(), string, true),
|
|
|
|
("__message__".into(), string, true),
|
|
|
|
("__param0__".into(), int64, true),
|
|
|
|
("__param1__".into(), int64, true),
|
|
|
|
("__param2__".into(), int64, true),
|
|
|
|
];
|
2022-03-16 23:42:08 +08:00
|
|
|
|
2022-03-26 15:09:15 +08:00
|
|
|
// for Option, is_some and is_none share the same type: () -> bool,
|
|
|
|
// and they are methods under the same class `Option`
|
|
|
|
let (is_some_ty, unwrap_ty, (option_ty_var, option_ty_var_id)) =
|
|
|
|
if let TypeEnum::TObj { fields, params, .. } =
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier.get_ty(primitives.option).as_ref()
|
2022-03-26 15:09:15 +08:00
|
|
|
{
|
|
|
|
(
|
|
|
|
*fields.get(&"is_some".into()).unwrap(),
|
|
|
|
*fields.get(&"unwrap".into()).unwrap(),
|
|
|
|
(*params.iter().next().unwrap().1, *params.iter().next().unwrap().0),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
2024-03-06 16:53:41 +08:00
|
|
|
|
|
|
|
let TypeEnum::TObj {
|
|
|
|
fields: ndarray_fields,
|
|
|
|
params: ndarray_params,
|
|
|
|
..
|
2024-04-29 22:03:13 +08:00
|
|
|
} = &*unifier.get_ty(primitives.ndarray) else {
|
2024-03-04 13:09:21 +08:00
|
|
|
unreachable!()
|
|
|
|
};
|
2024-03-06 16:53:41 +08:00
|
|
|
|
|
|
|
let (ndarray_dtype_ty, ndarray_dtype_var_id) = ndarray_params
|
|
|
|
.iter()
|
|
|
|
.next()
|
|
|
|
.map(|(var_id, ty)| (*ty, *var_id))
|
|
|
|
.unwrap();
|
|
|
|
let (ndarray_ndims_ty, ndarray_ndims_var_id) = ndarray_params
|
|
|
|
.iter()
|
|
|
|
.nth(1)
|
|
|
|
.map(|(var_id, ty)| (*ty, *var_id))
|
|
|
|
.unwrap();
|
2024-03-07 13:02:13 +08:00
|
|
|
let ndarray_copy_ty = *ndarray_fields.get(&"copy".into()).unwrap();
|
2024-03-06 16:53:41 +08:00
|
|
|
let ndarray_fill_ty = *ndarray_fields.get(&"fill".into()).unwrap();
|
|
|
|
|
2022-03-17 21:23:38 +08:00
|
|
|
let top_level_def_list = vec![
|
2021-12-01 02:52:00 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.int32,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"int32".into(),
|
|
|
|
None,
|
|
|
|
None,
|
2021-12-01 02:52:00 +08:00
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.int64,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"int64".into(),
|
|
|
|
None,
|
|
|
|
None,
|
2021-12-01 02:52:00 +08:00
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.float,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"float".into(),
|
|
|
|
None,
|
|
|
|
None,
|
2021-12-01 02:52:00 +08:00
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.bool,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"bool".into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.none,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"none".into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.range,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"range".into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.str,
|
2022-02-21 18:27:46 +08:00
|
|
|
None,
|
|
|
|
"str".into(),
|
|
|
|
None,
|
|
|
|
None,
|
2021-12-01 02:52:00 +08:00
|
|
|
))),
|
2022-02-12 21:09:23 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Class {
|
|
|
|
name: "Exception".into(),
|
2024-02-26 15:11:00 +08:00
|
|
|
object_id: PRIMITIVE_DEF_IDS.exception,
|
2023-12-08 17:43:32 +08:00
|
|
|
type_vars: Vec::default(),
|
2022-03-16 23:42:08 +08:00
|
|
|
fields: exception_fields,
|
2023-12-08 17:43:32 +08:00
|
|
|
methods: Vec::default(),
|
2022-02-12 21:09:23 +08:00
|
|
|
ancestors: vec![],
|
|
|
|
constructor: None,
|
|
|
|
resolver: None,
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2022-02-12 21:09:23 +08:00
|
|
|
})),
|
2022-03-05 03:45:09 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.uint32,
|
2022-03-05 03:45:09 +08:00
|
|
|
None,
|
|
|
|
"uint32".into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
))),
|
|
|
|
Arc::new(RwLock::new(TopLevelComposer::make_top_level_class_def(
|
2024-02-26 15:11:00 +08:00
|
|
|
PRIMITIVE_DEF_IDS.uint64,
|
2022-03-05 03:45:09 +08:00
|
|
|
None,
|
|
|
|
"uint64".into(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
))),
|
2022-03-26 15:09:15 +08:00
|
|
|
Arc::new(RwLock::new({
|
|
|
|
TopLevelDef::Class {
|
|
|
|
name: "Option".into(),
|
2024-02-26 15:11:00 +08:00
|
|
|
object_id: PRIMITIVE_DEF_IDS.option,
|
2022-03-26 15:09:15 +08:00
|
|
|
type_vars: vec![option_ty_var],
|
|
|
|
fields: vec![],
|
|
|
|
methods: vec![
|
2024-03-06 12:24:39 +08:00
|
|
|
("is_some".into(), is_some_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.option.0 + 1)),
|
|
|
|
("is_none".into(), is_some_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.option.0 + 2)),
|
|
|
|
("unwrap".into(), unwrap_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.option.0 + 3)),
|
2022-03-26 15:09:15 +08:00
|
|
|
],
|
|
|
|
ancestors: vec![TypeAnnotation::CustomClass {
|
2024-02-26 15:11:00 +08:00
|
|
|
id: PRIMITIVE_DEF_IDS.option,
|
2023-12-08 17:43:32 +08:00
|
|
|
params: Vec::default(),
|
2022-03-26 15:09:15 +08:00
|
|
|
}],
|
|
|
|
constructor: None,
|
|
|
|
resolver: None,
|
|
|
|
loc: None,
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "Option.is_some".into(),
|
|
|
|
simple_name: "is_some".into(),
|
|
|
|
signature: is_some_ty.0,
|
|
|
|
var_id: vec![option_ty_var_id],
|
2023-12-08 17:43:32 +08:00
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-26 15:09:15 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, obj, _, _, generator| {
|
2022-04-08 03:26:42 +08:00
|
|
|
let expect_ty = obj.clone().unwrap().0;
|
|
|
|
let obj_val = obj.unwrap().1.clone().to_basic_value_enum(
|
|
|
|
ctx,
|
|
|
|
generator,
|
|
|
|
expect_ty,
|
|
|
|
)?;
|
2023-12-12 13:38:27 +08:00
|
|
|
let BasicValueEnum::PointerValue(ptr) = obj_val else {
|
2022-03-26 15:09:15 +08:00
|
|
|
unreachable!("option must be ptr")
|
2023-12-12 13:38:27 +08:00
|
|
|
};
|
|
|
|
|
2024-02-19 19:30:25 +08:00
|
|
|
Ok(Some(ctx.builder
|
|
|
|
.build_is_not_null(ptr, "is_some")
|
|
|
|
.map(Into::into)
|
|
|
|
.unwrap()
|
|
|
|
))
|
2022-03-26 15:09:15 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "Option.is_none".into(),
|
|
|
|
simple_name: "is_none".into(),
|
|
|
|
signature: is_some_ty.0,
|
|
|
|
var_id: vec![option_ty_var_id],
|
2023-12-08 17:43:32 +08:00
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-26 15:09:15 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, obj, _, _, generator| {
|
2022-04-08 03:26:42 +08:00
|
|
|
let expect_ty = obj.clone().unwrap().0;
|
|
|
|
let obj_val = obj.unwrap().1.clone().to_basic_value_enum(
|
|
|
|
ctx,
|
|
|
|
generator,
|
|
|
|
expect_ty,
|
|
|
|
)?;
|
2023-12-12 13:38:27 +08:00
|
|
|
let BasicValueEnum::PointerValue(ptr) = obj_val else {
|
2022-03-26 15:09:15 +08:00
|
|
|
unreachable!("option must be ptr")
|
2023-12-12 13:38:27 +08:00
|
|
|
};
|
|
|
|
|
2024-02-19 19:30:25 +08:00
|
|
|
Ok(Some(ctx.builder
|
|
|
|
.build_is_null(ptr, "is_none")
|
|
|
|
.map(Into::into)
|
|
|
|
.unwrap()
|
|
|
|
))
|
2022-03-26 15:09:15 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "Option.unwrap".into(),
|
|
|
|
simple_name: "unwrap".into(),
|
|
|
|
signature: unwrap_ty.0,
|
|
|
|
var_id: vec![option_ty_var_id],
|
2023-12-08 17:43:32 +08:00
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-26 15:09:15 +08:00
|
|
|
resolver: None,
|
2024-04-23 14:36:29 +08:00
|
|
|
codegen_callback: Some(Arc::new(GenCall::create_dummy(
|
|
|
|
String::from("handled in gen_expr"),
|
|
|
|
))),
|
2022-03-26 15:09:15 +08:00
|
|
|
loc: None,
|
|
|
|
})),
|
2024-03-04 13:09:21 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Class {
|
|
|
|
name: "ndarray".into(),
|
|
|
|
object_id: PRIMITIVE_DEF_IDS.ndarray,
|
|
|
|
type_vars: vec![ndarray_dtype_ty, ndarray_ndims_ty],
|
|
|
|
fields: Vec::default(),
|
2024-03-06 16:53:41 +08:00
|
|
|
methods: vec![
|
2024-03-07 13:02:13 +08:00
|
|
|
("copy".into(), ndarray_copy_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 1)),
|
|
|
|
("fill".into(), ndarray_fill_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 2)),
|
2024-03-06 16:53:41 +08:00
|
|
|
],
|
2024-03-04 13:09:21 +08:00
|
|
|
ancestors: Vec::default(),
|
|
|
|
constructor: None,
|
|
|
|
resolver: None,
|
|
|
|
loc: None,
|
|
|
|
})),
|
2024-03-07 13:02:13 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "ndarray.copy".into(),
|
|
|
|
simple_name: "copy".into(),
|
|
|
|
signature: ndarray_copy_ty.0,
|
|
|
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, obj, fun, args, generator| {
|
|
|
|
gen_ndarray_copy(ctx, &obj, fun, &args, generator)
|
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2024-03-06 16:53:41 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "ndarray.fill".into(),
|
|
|
|
simple_name: "fill".into(),
|
|
|
|
signature: ndarray_fill_ty.0,
|
|
|
|
var_id: vec![ndarray_dtype_var_id, ndarray_ndims_var_id],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, obj, fun, args, generator| {
|
|
|
|
gen_ndarray_fill(ctx, &obj, fun, &args, generator)?;
|
|
|
|
Ok(None)
|
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2021-12-01 02:52:00 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "int32".into(),
|
|
|
|
simple_name: "int32".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-01 02:52:00 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-02-21 18:27:46 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-03 15:05:40 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_int32(generator, ctx, (arg_ty, arg))?))
|
2022-02-21 18:27:46 +08:00
|
|
|
},
|
2021-12-01 02:52:00 +08:00
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-01 02:52:00 +08:00
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "int64".into(),
|
|
|
|
simple_name: "int64".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-01 02:52:00 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-02-21 18:27:46 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_int64(generator, ctx, (arg_ty, arg))?))
|
2022-02-21 18:27:46 +08:00
|
|
|
},
|
2021-12-01 02:52:00 +08:00
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-01 02:52:00 +08:00
|
|
|
})),
|
2022-03-05 03:45:09 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "uint32".into(),
|
|
|
|
simple_name: "uint32".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-03-05 03:45:09 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-05 03:45:09 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, _, fun, args, generator| {
|
2022-03-05 23:23:32 +08:00
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-03 15:34:07 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_uint32(generator, ctx, (arg_ty, arg))?))
|
2022-03-05 03:45:09 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "uint64".into(),
|
|
|
|
simple_name: "uint64".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-03-05 03:45:09 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-05 03:45:09 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, _, fun, args, generator| {
|
2022-03-05 23:23:32 +08:00
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-03 15:34:07 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_uint64(generator, ctx, (arg_ty, arg))?))
|
2022-03-05 03:45:09 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2021-12-01 02:52:00 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "float".into(),
|
|
|
|
simple_name: "float".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-01 02:52:00 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-02-21 18:27:46 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_float(generator, ctx, (arg_ty, arg))?))
|
2022-02-21 18:27:46 +08:00
|
|
|
},
|
2021-12-01 02:52:00 +08:00
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-01 02:52:00 +08:00
|
|
|
})),
|
2023-11-17 17:30:27 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-17 17:30:27 +08:00
|
|
|
"np_ndarray",
|
|
|
|
ndarray_float,
|
|
|
|
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
|
|
|
// type variable
|
|
|
|
&[(list_int32, "shape")],
|
|
|
|
Box::new(|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_empty(ctx, &obj, fun, &args, generator)
|
2023-11-17 17:30:27 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-17 17:30:27 +08:00
|
|
|
"np_empty",
|
|
|
|
ndarray_float,
|
|
|
|
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
|
|
|
// type variable
|
|
|
|
&[(list_int32, "shape")],
|
|
|
|
Box::new(|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_empty(ctx, &obj, fun, &args, generator)
|
2023-11-17 17:30:27 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
}),
|
|
|
|
),
|
2023-11-27 13:25:53 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-27 13:25:53 +08:00
|
|
|
"np_zeros",
|
|
|
|
ndarray_float,
|
|
|
|
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
|
|
|
// type variable
|
|
|
|
&[(list_int32, "shape")],
|
|
|
|
Box::new(|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_zeros(ctx, &obj, fun, &args, generator)
|
2023-11-27 13:25:53 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-27 13:25:53 +08:00
|
|
|
"np_ones",
|
|
|
|
ndarray_float,
|
|
|
|
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
|
|
|
// type variable
|
|
|
|
&[(list_int32, "shape")],
|
|
|
|
Box::new(|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_ones(ctx, &obj, fun, &args, generator)
|
2023-11-27 13:25:53 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
{
|
2024-04-25 15:47:16 +08:00
|
|
|
let tv = unifier.get_fresh_var(Some("T".into()), None);
|
2023-11-27 13:25:53 +08:00
|
|
|
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&[(tv.1, tv.0)].into_iter().collect(),
|
2023-11-27 13:25:53 +08:00
|
|
|
"np_full",
|
|
|
|
ndarray,
|
|
|
|
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
|
|
|
|
// type variable
|
2024-04-25 15:47:16 +08:00
|
|
|
&[(list_int32, "shape"), (tv.0, "fill_value")],
|
2023-11-27 13:25:53 +08:00
|
|
|
Box::new(|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_full(ctx, &obj, fun, &args, generator)
|
2023-11-27 13:25:53 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_eye".into(),
|
|
|
|
simple_name: "np_eye".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2023-11-27 13:25:53 +08:00
|
|
|
args: vec![
|
|
|
|
FuncArg { name: "N".into(), ty: int32, default_value: None },
|
|
|
|
// TODO(Derppening): Default values current do not work?
|
|
|
|
FuncArg {
|
|
|
|
name: "M".into(),
|
|
|
|
ty: int32,
|
|
|
|
default_value: Some(SymbolValue::OptionNone)
|
|
|
|
},
|
|
|
|
FuncArg { name: "k".into(), ty: int32, default_value: Some(SymbolValue::I32(0)) },
|
|
|
|
],
|
|
|
|
ret: ndarray_float_2d,
|
2024-04-25 15:47:16 +08:00
|
|
|
vars: VarMap::default(),
|
2023-11-27 13:25:53 +08:00
|
|
|
})),
|
2024-02-20 18:07:55 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2023-11-27 13:25:53 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_eye(ctx, &obj, fun, &args, generator)
|
2023-11-27 13:25:53 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-27 13:25:53 +08:00
|
|
|
"np_identity",
|
|
|
|
ndarray_float_2d,
|
|
|
|
&[(int32, "n")],
|
|
|
|
Box::new(|ctx, obj, fun, args, generator| {
|
2024-02-20 18:07:55 +08:00
|
|
|
gen_ndarray_identity(ctx, &obj, fun, &args, generator)
|
2023-11-27 13:25:53 +08:00
|
|
|
.map(|val| Some(val.as_basic_value_enum()))
|
|
|
|
}),
|
|
|
|
),
|
2024-04-25 15:47:16 +08:00
|
|
|
{
|
|
|
|
let common_ndim = unifier.get_fresh_const_generic_var(
|
|
|
|
primitives.usize(),
|
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ndarray_int32 = make_ndarray_ty(unifier, primitives, Some(int32), Some(common_ndim.0));
|
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), Some(common_ndim.0));
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let p0_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ret_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[int32, ndarray_int32],
|
|
|
|
Some("R".into()),
|
|
|
|
None,
|
|
|
|
);
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&[
|
|
|
|
(common_ndim.1, common_ndim.0),
|
|
|
|
(p0_ty.1, p0_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
"round",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(p0_ty.0, "n")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ctx.primitives.int32)?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let common_ndim = unifier.get_fresh_const_generic_var(
|
|
|
|
primitives.usize(),
|
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ndarray_int64 = make_ndarray_ty(unifier, primitives, Some(int64), Some(common_ndim.0));
|
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), Some(common_ndim.0));
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let p0_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ret_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[int64, ndarray_int64],
|
|
|
|
Some("R".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&[
|
|
|
|
(common_ndim.1, common_ndim.0),
|
|
|
|
(p0_ty.1, p0_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
"round64",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(p0_ty.0, "n")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ctx.primitives.int64)?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
2023-11-23 13:45:07 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:45:07 +08:00
|
|
|
"np_round",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "n")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2023-11-23 13:45:07 +08:00
|
|
|
let arg = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-23 13:45:07 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_round(generator, ctx, (arg_ty, arg))?))
|
2023-11-23 13:45:07 +08:00
|
|
|
}),
|
|
|
|
),
|
2021-12-01 02:52:00 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "range".into(),
|
|
|
|
simple_name: "range".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2021-12-01 02:52:00 +08:00
|
|
|
args: vec![
|
|
|
|
FuncArg { name: "start".into(), ty: int32, default_value: None },
|
|
|
|
FuncArg {
|
|
|
|
name: "stop".into(),
|
|
|
|
ty: int32,
|
|
|
|
// placeholder
|
|
|
|
default_value: Some(SymbolValue::I32(0)),
|
|
|
|
},
|
|
|
|
FuncArg {
|
|
|
|
name: "step".into(),
|
|
|
|
ty: int32,
|
|
|
|
default_value: Some(SymbolValue::I32(1)),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
ret: range,
|
2024-03-04 23:38:52 +08:00
|
|
|
vars: VarMap::default(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-01 02:52:00 +08:00
|
|
|
resolver: None,
|
2022-02-21 18:27:46 +08:00
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, _, _, args, generator| {
|
|
|
|
let mut start = None;
|
|
|
|
let mut stop = None;
|
|
|
|
let mut step = None;
|
|
|
|
let int32 = ctx.ctx.i32_type();
|
2022-04-08 03:26:42 +08:00
|
|
|
let ty_i32 = ctx.primitives.int32;
|
2022-02-21 18:27:46 +08:00
|
|
|
for (i, arg) in args.iter().enumerate() {
|
|
|
|
if arg.0 == Some("start".into()) {
|
2024-06-07 13:00:42 +08:00
|
|
|
start = Some(arg.1
|
|
|
|
.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
|
|
.into_int_value()
|
|
|
|
);
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if arg.0 == Some("stop".into()) {
|
2024-06-07 13:00:42 +08:00
|
|
|
stop = Some(
|
|
|
|
arg.1
|
|
|
|
.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
|
|
.into_int_value()
|
|
|
|
);
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if arg.0 == Some("step".into()) {
|
2024-06-07 13:00:42 +08:00
|
|
|
step = Some(
|
|
|
|
arg.1
|
|
|
|
.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
|
|
.into_int_value()
|
|
|
|
);
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if i == 0 {
|
2024-06-07 13:00:42 +08:00
|
|
|
start = Some(
|
|
|
|
arg.1
|
|
|
|
.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
|
|
.into_int_value()
|
|
|
|
);
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if i == 1 {
|
2024-06-07 13:00:42 +08:00
|
|
|
stop = Some(
|
|
|
|
arg.1
|
|
|
|
.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
|
|
.into_int_value()
|
|
|
|
);
|
2022-02-21 18:27:46 +08:00
|
|
|
} else if i == 2 {
|
2024-06-07 13:00:42 +08:00
|
|
|
step = Some(
|
|
|
|
arg.1
|
|
|
|
.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, ty_i32)?
|
|
|
|
.into_int_value()
|
|
|
|
);
|
2022-02-21 18:27:46 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-05 14:29:20 +08:00
|
|
|
let step = match step {
|
|
|
|
Some(step) => {
|
|
|
|
// assert step != 0, throw exception if not
|
2024-02-19 19:30:25 +08:00
|
|
|
let not_zero = ctx.builder
|
|
|
|
.build_int_compare(
|
|
|
|
IntPredicate::NE,
|
|
|
|
step,
|
|
|
|
step.get_type().const_zero(),
|
|
|
|
"range_step_ne",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-04-05 14:29:20 +08:00
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
not_zero,
|
|
|
|
"0:ValueError",
|
|
|
|
"range() step must not be zero",
|
|
|
|
[None, None, None],
|
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
step
|
|
|
|
}
|
|
|
|
None => int32.const_int(1, false),
|
|
|
|
};
|
2022-02-21 18:27:46 +08:00
|
|
|
let stop = stop.unwrap_or_else(|| {
|
|
|
|
let v = start.unwrap();
|
|
|
|
start = None;
|
|
|
|
v
|
|
|
|
});
|
2024-06-07 13:00:42 +08:00
|
|
|
let start = start.unwrap_or_else(|| int32.const_zero());
|
|
|
|
|
|
|
|
let ptr = RangeType::new(ctx.ctx).new_value(generator, ctx, Some("range"));
|
|
|
|
ptr.store_start(ctx, start);
|
|
|
|
ptr.store_end(ctx, stop);
|
|
|
|
ptr.store_step(ctx, step);
|
|
|
|
Ok(Some(ptr.as_base_value().into()))
|
2022-02-21 18:27:46 +08:00
|
|
|
},
|
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-01 02:52:00 +08:00
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "str".into(),
|
|
|
|
simple_name: "str".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2022-02-21 17:52:34 +08:00
|
|
|
args: vec![FuncArg { name: "s".into(), ty: string, default_value: None }],
|
2021-12-01 02:52:00 +08:00
|
|
|
ret: string,
|
2024-03-04 23:38:52 +08:00
|
|
|
vars: VarMap::default(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-01 02:52:00 +08:00
|
|
|
resolver: None,
|
2022-02-21 18:27:46 +08:00
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-04-08 03:26:42 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
Ok(Some(args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?))
|
2022-02-21 18:27:46 +08:00
|
|
|
},
|
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-01 02:52:00 +08:00
|
|
|
})),
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "bool".into(),
|
|
|
|
simple_name: "bool".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-01 02:52:00 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-02-12 21:00:12 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
2021-12-01 02:52:00 +08:00
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_bool(generator, ctx, (arg_ty, arg))?))
|
2021-12-01 02:52:00 +08:00
|
|
|
},
|
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-01 02:52:00 +08:00
|
|
|
})),
|
2024-04-25 15:47:16 +08:00
|
|
|
{
|
|
|
|
let common_ndim = unifier.get_fresh_const_generic_var(
|
|
|
|
primitives.usize(),
|
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ndarray_int32 = make_ndarray_ty(unifier, primitives, Some(int32), Some(common_ndim.0));
|
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), Some(common_ndim.0));
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let p0_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ret_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[int32, ndarray_int32],
|
|
|
|
Some("R".into()),
|
|
|
|
None,
|
|
|
|
);
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&[
|
|
|
|
(common_ndim.1, common_ndim.0),
|
|
|
|
(p0_ty.1, p0_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
"floor",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(p0_ty.0, "n")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_floor(generator, ctx, (arg_ty, arg), ctx.primitives.int32)?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let common_ndim = unifier.get_fresh_const_generic_var(
|
|
|
|
primitives.usize(),
|
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ndarray_int64 = make_ndarray_ty(unifier, primitives, Some(int64), Some(common_ndim.0));
|
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), Some(common_ndim.0));
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let p0_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ret_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[int64, ndarray_int64],
|
|
|
|
Some("R".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&[
|
|
|
|
(common_ndim.1, common_ndim.0),
|
|
|
|
(p0_ty.1, p0_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
"floor64",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(p0_ty.0, "n")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_floor(generator, ctx, (arg_ty, arg), ctx.primitives.int64)?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
2023-11-23 13:45:07 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:45:07 +08:00
|
|
|
"np_floor",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "n")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2023-11-23 13:45:07 +08:00
|
|
|
let arg = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-23 13:45:07 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_floor(generator, ctx, (arg_ty, arg), ctx.primitives.float)?))
|
2023-11-23 13:45:07 +08:00
|
|
|
}),
|
|
|
|
),
|
2024-04-25 15:47:16 +08:00
|
|
|
{
|
|
|
|
let common_ndim = unifier.get_fresh_const_generic_var(
|
|
|
|
primitives.usize(),
|
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ndarray_int32 = make_ndarray_ty(unifier, primitives, Some(int32), Some(common_ndim.0));
|
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), Some(common_ndim.0));
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let p0_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ret_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[int32, ndarray_int32],
|
|
|
|
Some("R".into()),
|
|
|
|
None,
|
|
|
|
);
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&[
|
|
|
|
(common_ndim.1, common_ndim.0),
|
|
|
|
(p0_ty.1, p0_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
"ceil",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(p0_ty.0, "n")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_ceil(generator, ctx, (arg_ty, arg), ctx.primitives.int32)?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let common_ndim = unifier.get_fresh_const_generic_var(
|
|
|
|
primitives.usize(),
|
|
|
|
Some("N".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ndarray_int64 = make_ndarray_ty(unifier, primitives, Some(int64), Some(common_ndim.0));
|
|
|
|
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), Some(common_ndim.0));
|
2023-11-02 14:56:35 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
let p0_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[float, ndarray_float],
|
|
|
|
Some("T".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let ret_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[int64, ndarray_int64],
|
|
|
|
Some("R".into()),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&[
|
|
|
|
(common_ndim.1, common_ndim.0),
|
|
|
|
(p0_ty.1, p0_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
"ceil64",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(p0_ty.0, "n")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_ceil(generator, ctx, (arg_ty, arg), ctx.primitives.int64)?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
2023-11-23 13:45:07 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:45:07 +08:00
|
|
|
"np_ceil",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "n")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2023-11-23 13:45:07 +08:00
|
|
|
let arg = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-11-23 13:45:07 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_ceil(generator, ctx, (arg_ty, arg), ctx.primitives.float)?))
|
2023-11-23 13:45:07 +08:00
|
|
|
}),
|
|
|
|
),
|
2021-12-09 01:14:28 +08:00
|
|
|
Arc::new(RwLock::new({
|
2024-04-29 22:03:13 +08:00
|
|
|
let tvar = unifier.get_fresh_var(Some("L".into()), None);
|
|
|
|
let list = unifier.add_ty(TypeEnum::TList { ty: tvar.0 });
|
|
|
|
let ndims = unifier.get_fresh_const_generic_var(primitives.uint64, Some("N".into()), None);
|
2024-02-27 13:39:05 +08:00
|
|
|
let ndarray = make_ndarray_ty(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
|
|
|
primitives,
|
2024-02-27 13:39:05 +08:00
|
|
|
Some(tvar.0),
|
|
|
|
Some(ndims.0),
|
|
|
|
);
|
2023-11-06 18:03:52 +08:00
|
|
|
|
2024-04-29 22:03:13 +08:00
|
|
|
let arg_ty = unifier.get_fresh_var_with_range(
|
|
|
|
&[list, ndarray, primitives.range],
|
2022-02-21 18:27:46 +08:00
|
|
|
Some("I".into()),
|
|
|
|
None,
|
|
|
|
);
|
2021-12-09 01:14:28 +08:00
|
|
|
TopLevelDef::Function {
|
|
|
|
name: "len".into(),
|
|
|
|
simple_name: "len".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2022-02-21 18:27:46 +08:00
|
|
|
args: vec![FuncArg { name: "ls".into(), ty: arg_ty.0, default_value: None }],
|
2021-12-20 17:44:42 +08:00
|
|
|
ret: int32,
|
2023-11-06 18:03:52 +08:00
|
|
|
vars: vec![(tvar.1, tvar.0), (arg_ty.1, arg_ty.0)]
|
2022-02-21 18:27:46 +08:00
|
|
|
.into_iter()
|
|
|
|
.collect(),
|
2022-02-21 17:52:34 +08:00
|
|
|
})),
|
2024-04-25 15:47:16 +08:00
|
|
|
var_id: Vec::default(),
|
2023-12-08 17:43:32 +08:00
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2021-12-09 01:14:28 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-02-12 21:00:12 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
2021-12-09 01:14:28 +08:00
|
|
|
let range_ty = ctx.primitives.range;
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2022-02-21 17:52:34 +08:00
|
|
|
Ok(if ctx.unifier.unioned(arg_ty, range_ty) {
|
2024-01-23 18:27:00 +08:00
|
|
|
let arg = RangeValue::from_ptr_val(arg.into_pointer_value(), Some("range"));
|
2021-12-09 01:14:28 +08:00
|
|
|
let (start, end, step) = destructure_range(ctx, arg);
|
2022-04-05 14:29:20 +08:00
|
|
|
Some(calculate_len_for_slice_range(generator, ctx, start, end, step).into())
|
2021-12-09 01:14:28 +08:00
|
|
|
} else {
|
2023-11-06 18:03:52 +08:00
|
|
|
match &*ctx.unifier.get_ty_immutable(arg_ty) {
|
|
|
|
TypeEnum::TList { .. } => {
|
|
|
|
let int32 = ctx.ctx.i32_type();
|
|
|
|
let zero = int32.const_zero();
|
|
|
|
let len = ctx
|
|
|
|
.build_gep_and_load(
|
|
|
|
arg.into_pointer_value(),
|
|
|
|
&[zero, int32.const_int(1, false)],
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.into_int_value();
|
|
|
|
if len.get_type().get_bit_width() == 32 {
|
|
|
|
Some(len.into())
|
|
|
|
} else {
|
2024-02-19 19:30:25 +08:00
|
|
|
Some(ctx.builder
|
|
|
|
.build_int_truncate(len, int32, "len2i32")
|
|
|
|
.map(Into::into)
|
|
|
|
.unwrap()
|
|
|
|
)
|
2023-11-06 18:03:52 +08:00
|
|
|
}
|
|
|
|
}
|
2024-02-27 13:39:05 +08:00
|
|
|
TypeEnum::TObj { obj_id, .. } if *obj_id == PRIMITIVE_DEF_IDS.ndarray => {
|
2023-11-27 13:25:53 +08:00
|
|
|
let llvm_i32 = ctx.ctx.i32_type();
|
2024-03-26 15:07:26 +08:00
|
|
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
2023-11-27 13:25:53 +08:00
|
|
|
|
2024-03-26 15:07:26 +08:00
|
|
|
let arg = NDArrayValue::from_ptr_val(
|
2023-11-27 13:25:53 +08:00
|
|
|
arg.into_pointer_value(),
|
2024-03-26 15:07:26 +08:00
|
|
|
llvm_usize,
|
|
|
|
None
|
|
|
|
);
|
|
|
|
|
|
|
|
let ndims = arg.dim_sizes().size(ctx, generator);
|
|
|
|
ctx.make_assert(
|
|
|
|
generator,
|
|
|
|
ctx.builder.build_int_compare(
|
|
|
|
IntPredicate::NE,
|
|
|
|
ndims,
|
|
|
|
llvm_usize.const_zero(),
|
|
|
|
"",
|
|
|
|
).unwrap(),
|
|
|
|
"0:TypeError",
|
|
|
|
"len() of unsized object",
|
|
|
|
[None, None, None],
|
|
|
|
ctx.current_loc,
|
|
|
|
);
|
|
|
|
|
|
|
|
let len = unsafe {
|
|
|
|
arg.dim_sizes().get_typed_unchecked(
|
|
|
|
ctx,
|
|
|
|
generator,
|
2024-04-16 17:20:24 +08:00
|
|
|
&llvm_usize.const_zero(),
|
2024-03-26 15:07:26 +08:00
|
|
|
None,
|
|
|
|
)
|
|
|
|
};
|
2023-11-27 13:25:53 +08:00
|
|
|
|
2024-02-20 18:07:55 +08:00
|
|
|
if len.get_type().get_bit_width() == 32 {
|
|
|
|
Some(len.into())
|
|
|
|
} else {
|
2024-02-19 19:30:25 +08:00
|
|
|
Some(ctx.builder
|
|
|
|
.build_int_truncate(len, llvm_i32, "len")
|
|
|
|
.map(Into::into)
|
|
|
|
.unwrap()
|
|
|
|
)
|
2023-11-27 13:25:53 +08:00
|
|
|
}
|
|
|
|
}
|
2023-11-06 18:03:52 +08:00
|
|
|
_ => unreachable!(),
|
2021-12-27 22:50:36 +08:00
|
|
|
}
|
2022-02-21 17:52:34 +08:00
|
|
|
})
|
2021-12-09 01:14:28 +08:00
|
|
|
},
|
|
|
|
)))),
|
2022-02-21 17:52:34 +08:00
|
|
|
loc: None,
|
2021-12-09 01:14:28 +08:00
|
|
|
}
|
2022-02-21 18:27:46 +08:00
|
|
|
})),
|
2022-03-08 21:50:28 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "min".into(),
|
|
|
|
simple_name: "min".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2022-03-08 21:50:28 +08:00
|
|
|
args: vec![
|
|
|
|
FuncArg { name: "m".into(), ty: num_ty.0, default_value: None },
|
|
|
|
FuncArg { name: "n".into(), ty: num_ty.0, default_value: None },
|
|
|
|
],
|
|
|
|
ret: num_ty.0,
|
2024-04-25 15:47:16 +08:00
|
|
|
vars: num_var_map.clone(),
|
2022-03-08 21:50:28 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-08 21:50:28 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let m_ty = fun.0.args[0].ty;
|
|
|
|
let n_ty = fun.0.args[1].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let m_val = args[0].1.clone().to_basic_value_enum(ctx, generator, m_ty)?;
|
|
|
|
let n_val = args[1].1.clone().to_basic_value_enum(ctx, generator, n_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_min(ctx, (m_ty, m_val), (n_ty, n_val))))
|
2022-03-08 21:50:28 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2024-05-08 17:42:19 +08:00
|
|
|
{
|
|
|
|
let ret_ty = unifier.get_fresh_var(Some("R".into()), None);
|
|
|
|
let var_map = num_or_ndarray_var_map.clone()
|
|
|
|
.into_iter()
|
|
|
|
.chain(once((ret_ty.1, ret_ty.0)))
|
|
|
|
.collect::<IndexMap<_, _>>();
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&var_map,
|
|
|
|
"np_min",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "a")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let a_ty = fun.0.args[0].ty;
|
|
|
|
let a = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, a_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_min(generator, ctx, (a_ty, a))?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
2024-05-08 18:29:11 +08:00
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, num_ty.0);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, num_ty.0);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_minimum".into(),
|
|
|
|
simple_name: "np_minimum".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x1_val = args[0].1.clone().to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_val = args[1].1.clone().to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_minimum(generator, ctx, (x1_ty, x1_val), (x2_ty, x2_val))?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
2022-03-08 22:22:00 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "max".into(),
|
|
|
|
simple_name: "max".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2022-03-08 22:22:00 +08:00
|
|
|
args: vec![
|
|
|
|
FuncArg { name: "m".into(), ty: num_ty.0, default_value: None },
|
|
|
|
FuncArg { name: "n".into(), ty: num_ty.0, default_value: None },
|
|
|
|
],
|
|
|
|
ret: num_ty.0,
|
2024-04-25 15:47:16 +08:00
|
|
|
vars: num_var_map.clone(),
|
2022-03-08 22:22:00 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-08 22:22:00 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let m_ty = fun.0.args[0].ty;
|
|
|
|
let n_ty = fun.0.args[1].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let m_val = args[0].1.clone().to_basic_value_enum(ctx, generator, m_ty)?;
|
|
|
|
let n_val = args[1].1.clone().to_basic_value_enum(ctx, generator, n_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_max(ctx, (m_ty, m_val), (n_ty, n_val))))
|
2022-03-08 22:22:00 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2024-05-08 17:42:19 +08:00
|
|
|
{
|
|
|
|
let ret_ty = unifier.get_fresh_var(Some("R".into()), None);
|
|
|
|
let var_map = num_or_ndarray_var_map.clone()
|
|
|
|
.into_iter()
|
|
|
|
.chain(once((ret_ty.1, ret_ty.0)))
|
|
|
|
.collect::<IndexMap<_, _>>();
|
|
|
|
|
|
|
|
create_fn_by_codegen(
|
|
|
|
unifier,
|
|
|
|
&var_map,
|
|
|
|
"np_max",
|
|
|
|
ret_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "a")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let a_ty = fun.0.args[0].ty;
|
|
|
|
let a = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, a_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_max(generator, ctx, (a_ty, a))?))
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
},
|
2024-05-08 18:29:11 +08:00
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, num_ty.0);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, num_ty.0);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_maximum".into(),
|
|
|
|
simple_name: "np_maximum".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x1_val = args[0].1.clone().to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_val = args[1].1.clone().to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_maximum(generator, ctx, (x1_ty, x1_val), (x2_ty, x2_val))?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
2022-03-08 23:02:25 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "abs".into(),
|
|
|
|
simple_name: "abs".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2024-04-25 15:47:16 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: num_or_ndarray_ty.0, default_value: None }],
|
|
|
|
ret: num_or_ndarray_ty.0,
|
|
|
|
vars: num_or_ndarray_var_map.clone(),
|
2022-03-08 23:02:25 +08:00
|
|
|
})),
|
2023-12-08 17:43:32 +08:00
|
|
|
var_id: Vec::default(),
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-08 23:02:25 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let n_ty = fun.0.args[0].ty;
|
2022-04-08 03:26:42 +08:00
|
|
|
let n_val = args[0].1.clone().to_basic_value_enum(ctx, generator, n_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_abs(generator, ctx, (n_ty, n_val))?))
|
2022-03-08 23:02:25 +08:00
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2023-10-10 16:56:38 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_isnan",
|
2023-10-10 16:56:38 +08:00
|
|
|
boolean,
|
|
|
|
&[(float, "x")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2023-10-10 16:56:38 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_isnan(generator, ctx, (x_ty, x_val))?))
|
2023-10-10 16:56:38 +08:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&VarMap::new(),
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_isinf",
|
2023-10-10 16:56:38 +08:00
|
|
|
boolean,
|
|
|
|
&[(float, "x")],
|
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2023-10-10 16:56:38 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_isinf(generator, ctx, (x_ty, x_val))?))
|
2023-10-10 16:56:38 +08:00
|
|
|
}),
|
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_sin",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_sin(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_cos",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_cos(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_exp",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_exp(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_exp2",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_exp2(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_log",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_log(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_log10",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_log10(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_log2",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_log2(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_fabs",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_fabs(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_sqrt",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_sqrt(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_rint",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_rint(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_tan",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_tan(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_arcsin",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_arcsin(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_arccos",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_arccos(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_arctan",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_arctan(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_sinh",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_sinh(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_cosh",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_cosh(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_tanh",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_tanh(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_arcsinh",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_arcsinh(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_arccosh",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_arccosh(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_arctanh",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_arctanh(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_expm1",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_expm1(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"np_cbrt",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_numpy_cbrt(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"sp_spec_erf",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "z")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let z_ty = fun.0.args[0].ty;
|
|
|
|
let z_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, z_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_scipy_special_erf(generator, ctx, (z_ty, z_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"sp_spec_erfc",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let z_ty = fun.0.args[0].ty;
|
|
|
|
let z_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, z_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_scipy_special_erfc(generator, ctx, (z_ty, z_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2023-10-10 18:19:36 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"sp_spec_gamma",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "z")],
|
2023-10-10 18:19:36 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let z_ty = fun.0.args[0].ty;
|
|
|
|
let z_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, z_ty)?;
|
2023-10-10 18:19:36 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_scipy_special_gamma(generator, ctx, (z_ty, z_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
|
|
|
),
|
2023-10-10 18:19:36 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"sp_spec_gammaln",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2023-10-10 18:19:36 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
2024-04-24 17:40:25 +08:00
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2023-10-10 18:19:36 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_scipy_special_gammaln(generator, ctx, (x_ty, x_val))?))
|
2023-10-10 18:19:36 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2023-10-10 18:19:36 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"sp_spec_j0",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2023-10-10 18:19:36 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
2023-11-06 12:57:23 +08:00
|
|
|
let z_ty = fun.0.args[0].ty;
|
|
|
|
let z_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, z_ty)?;
|
2023-10-10 18:19:36 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_scipy_special_j0(generator, ctx, (z_ty, z_val))?))
|
2023-10-10 18:19:36 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
2024-04-24 17:40:25 +08:00
|
|
|
create_fn_by_codegen(
|
2024-04-29 22:03:13 +08:00
|
|
|
unifier,
|
2024-04-25 15:47:16 +08:00
|
|
|
&float_or_ndarray_var_map,
|
2023-11-23 13:32:08 +08:00
|
|
|
"sp_spec_j1",
|
2024-04-25 15:47:16 +08:00
|
|
|
float_or_ndarray_ty.0,
|
|
|
|
&[(float_or_ndarray_ty.0, "x")],
|
2024-04-24 17:40:25 +08:00
|
|
|
Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x_ty = fun.0.args[0].ty;
|
|
|
|
let x_val = args[0].1.clone()
|
2024-04-25 15:47:16 +08:00
|
|
|
.to_basic_value_enum(ctx, generator, x_ty)?;
|
2024-04-24 17:40:25 +08:00
|
|
|
|
2024-04-25 15:47:16 +08:00
|
|
|
Ok(Some(builtin_fns::call_scipy_special_j1(generator, ctx, (x_ty, x_val))?))
|
2024-04-24 17:40:25 +08:00
|
|
|
}),
|
2023-10-06 17:48:31 +08:00
|
|
|
),
|
|
|
|
// Not mapped: jv/yv, libm only supports integer orders.
|
2024-04-25 15:47:16 +08:00
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_arctan2".into(),
|
|
|
|
simple_name: "np_arctan2".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![ret_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_arctan2(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_copysign".into(),
|
|
|
|
simple_name: "np_copysign".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![ret_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_copysign(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_fmax".into(),
|
|
|
|
simple_name: "np_fmax".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_fmax(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_fmin".into(),
|
|
|
|
simple_name: "np_fmin".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_fmin(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, int32);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_ldexp".into(),
|
|
|
|
simple_name: "np_ldexp".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_ldexp(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_hypot".into(),
|
|
|
|
simple_name: "np_hypot".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_hypot(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
|
|
|
{
|
|
|
|
let x1_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let x2_ty = new_type_or_ndarray_ty(unifier, primitives, float);
|
|
|
|
let param_ty = &[(x1_ty.0, "x1"), (x2_ty.0, "x2")];
|
|
|
|
let ret_ty = unifier.get_fresh_var(None, None);
|
|
|
|
|
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "np_nextafter".into(),
|
|
|
|
simple_name: "np_nextafter".into(),
|
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
|
|
|
args: param_ty.iter().map(|p| FuncArg {
|
|
|
|
name: p.1.into(),
|
|
|
|
ty: p.0,
|
|
|
|
default_value: None,
|
|
|
|
}).collect(),
|
|
|
|
ret: ret_ty.0,
|
|
|
|
vars: [
|
|
|
|
(x1_ty.1, x1_ty.0),
|
|
|
|
(x2_ty.1, x2_ty.0),
|
|
|
|
(ret_ty.1, ret_ty.0),
|
|
|
|
].into_iter().collect(),
|
|
|
|
})),
|
|
|
|
var_id: vec![x1_ty.1, x2_ty.1],
|
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(|ctx, _, fun, args, generator| {
|
|
|
|
let x1_ty = fun.0.args[0].ty;
|
|
|
|
let x1_val = args[0].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x1_ty)?;
|
|
|
|
let x2_ty = fun.0.args[1].ty;
|
|
|
|
let x2_val = args[1].1.clone()
|
|
|
|
.to_basic_value_enum(ctx, generator, x2_ty)?;
|
|
|
|
|
|
|
|
Ok(Some(builtin_fns::call_numpy_nextafter(
|
|
|
|
generator,
|
|
|
|
ctx,
|
|
|
|
(x1_ty, x1_val),
|
|
|
|
(x2_ty, x2_val),
|
|
|
|
)?))
|
|
|
|
})))),
|
|
|
|
loc: None,
|
|
|
|
}))
|
|
|
|
},
|
2022-03-26 15:09:15 +08:00
|
|
|
Arc::new(RwLock::new(TopLevelDef::Function {
|
|
|
|
name: "Some".into(),
|
|
|
|
simple_name: "Some".into(),
|
2024-04-29 22:03:13 +08:00
|
|
|
signature: unifier.add_ty(TypeEnum::TFunc(FunSignature {
|
2022-03-26 15:09:15 +08:00
|
|
|
args: vec![FuncArg { name: "n".into(), ty: option_ty_var, default_value: None }],
|
2024-04-29 22:03:13 +08:00
|
|
|
ret: primitives.option,
|
2024-03-04 23:38:52 +08:00
|
|
|
vars: VarMap::from([(option_ty_var_id, option_ty_var)]),
|
2022-03-26 15:09:15 +08:00
|
|
|
})),
|
|
|
|
var_id: vec![option_ty_var_id],
|
2023-12-08 17:43:32 +08:00
|
|
|
instance_to_symbol: HashMap::default(),
|
|
|
|
instance_to_stmt: HashMap::default(),
|
2022-03-26 15:09:15 +08:00
|
|
|
resolver: None,
|
|
|
|
codegen_callback: Some(Arc::new(GenCall::new(Box::new(
|
2022-04-08 03:26:42 +08:00
|
|
|
|ctx, _, fun, args, generator| {
|
|
|
|
let arg_ty = fun.0.args[0].ty;
|
|
|
|
let arg_val = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
2023-10-03 18:02:45 +08:00
|
|
|
let alloca = generator.gen_var_alloc(ctx, arg_val.get_type(), Some("alloca_some")).unwrap();
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_store(alloca, arg_val).unwrap();
|
2022-03-26 15:09:15 +08:00
|
|
|
Ok(Some(alloca.into()))
|
|
|
|
},
|
|
|
|
)))),
|
|
|
|
loc: None,
|
|
|
|
})),
|
2021-12-01 02:52:00 +08:00
|
|
|
];
|
2022-03-17 00:04:49 +08:00
|
|
|
|
2023-10-26 13:52:40 +08:00
|
|
|
let ast_list: Vec<Option<Stmt<()>>> =
|
2021-12-01 02:52:00 +08:00
|
|
|
(0..top_level_def_list.len()).map(|_| None).collect();
|
2023-11-28 17:37:49 +08:00
|
|
|
|
|
|
|
izip!(top_level_def_list, ast_list).collect_vec()
|
2021-12-12 05:52:22 +08:00
|
|
|
}
|