Compare commits

..

11 Commits

Author SHA1 Message Date
dfbbe66154
core/model: renaming and add notes on upgrading Ptr to LLVM 15 2024-08-27 10:42:57 +08:00
696575aa77
core/model: introduce models 2024-08-27 10:42:57 +08:00
048950b7f0
standalone: reformat demo.c 2024-08-27 10:37:46 +08:00
38be74d5fe
core/irrt: add exceptions and debug utils 2024-08-27 10:36:51 +08:00
fb41645863
core/irrt: split into headers 2024-08-27 10:31:49 +08:00
fce5ba3dbf
core/irrt: build.rs capture IR defined constants 2024-08-27 10:28:39 +08:00
4c0f976ef1
core/irrt: build.rs capture IR defined types 2024-08-27 10:28:33 +08:00
96073fe6bc
core/irrt: use +std=c++20 to compile
To explicitly set the C++ variant and avoid inconsistencies.
2024-08-27 10:28:17 +08:00
aabd682664
core/irrt: reformat 2024-08-27 10:28:01 +08:00
4065f98447
add .clang-format 2024-08-27 10:27:31 +08:00
e00ff7f4e6
core/irrt: comment build.rs & move irrt to nac3core/irrt 2024-08-27 10:26:20 +08:00
34 changed files with 399 additions and 1000 deletions

62
Cargo.lock generated
View File

@ -117,9 +117,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.1.15" version = "1.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@ -161,7 +161,7 @@ dependencies = [
"heck 0.5.0", "heck 0.5.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
@ -310,9 +310,9 @@ dependencies = [
[[package]] [[package]]
name = "fastrand" name = "fastrand"
version = "2.1.1" version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
[[package]] [[package]]
name = "fixedbitset" name = "fixedbitset"
@ -424,7 +424,7 @@ checksum = "4fa4d8d74483041a882adaa9a29f633253a66dde85055f0495c121620ac484b2"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
@ -510,9 +510,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.158" version = "0.2.157"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" checksum = "374af5f94e54fa97cf75e945cce8a6b201e88a1a07e688b47dfd2a59c66dbd86"
[[package]] [[package]]
name = "libloading" name = "libloading"
@ -752,7 +752,7 @@ dependencies = [
"phf_shared 0.11.2", "phf_shared 0.11.2",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
@ -856,7 +856,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"pyo3-macros-backend", "pyo3-macros-backend",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
@ -869,14 +869,14 @@ dependencies = [
"proc-macro2", "proc-macro2",
"pyo3-build-config", "pyo3-build-config",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.37" version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -942,9 +942,9 @@ dependencies = [
[[package]] [[package]]
name = "redox_users" name = "redox_users"
version = "0.4.6" version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891"
dependencies = [ dependencies = [
"getrandom", "getrandom",
"libredox", "libredox",
@ -989,9 +989,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.35" version = "0.38.34"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@ -1035,29 +1035,29 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.209" version = "1.0.208"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.209" version = "1.0.208"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.127" version = "1.0.125"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad" checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
@ -1147,7 +1147,7 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"rustversion", "rustversion",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
@ -1163,9 +1163,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.76" version = "2.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1232,7 +1232,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]
[[package]] [[package]]
@ -1310,9 +1310,9 @@ checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.5" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "229730647fbc343e3a80e463c1db7f78f3855d3f3739bee0dda773c9a037c90a" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]] [[package]]
name = "unicode_names2" name = "unicode_names2"
@ -1510,5 +1510,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.76", "syn 2.0.75",
] ]

View File

@ -2,7 +2,7 @@ use nac3core::{
codegen::{ codegen::{
classes::{ classes::{
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayType, ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayType,
NDArrayValue, ProxyType, ProxyValue, RangeValue, UntypedArrayLikeAccessor, NDArrayValue, RangeValue, UntypedArrayLikeAccessor,
}, },
expr::{destructure_range, gen_call}, expr::{destructure_range, gen_call},
irrt::call_ndarray_calc_size, irrt::call_ndarray_calc_size,
@ -22,7 +22,7 @@ use inkwell::{
module::Linkage, module::Linkage,
types::{BasicType, IntType}, types::{BasicType, IntType},
values::{BasicValueEnum, PointerValue, StructValue}, values::{BasicValueEnum, PointerValue, StructValue},
AddressSpace, IntPredicate, OptimizationLevel, AddressSpace, IntPredicate,
}; };
use pyo3::{ use pyo3::{
@ -32,7 +32,6 @@ use pyo3::{
use crate::{symbol_resolver::InnerResolver, timeline::TimeFns}; use crate::{symbol_resolver::InnerResolver, timeline::TimeFns};
use inkwell::values::IntValue;
use itertools::Itertools; use itertools::Itertools;
use std::{ use std::{
collections::{hash_map::DefaultHasher, HashMap}, collections::{hash_map::DefaultHasher, HashMap},
@ -487,10 +486,13 @@ fn format_rpc_arg<'ctx>(
let buffer = ctx.builder.build_array_alloca(llvm_i8, buffer_size, "rpc.arg").unwrap(); let buffer = ctx.builder.build_array_alloca(llvm_i8, buffer_size, "rpc.arg").unwrap();
let buffer = ArraySliceValue::from_ptr_val(buffer, buffer_size, Some("rpc.arg")); let buffer = ArraySliceValue::from_ptr_val(buffer, buffer_size, Some("rpc.arg"));
let ppdata = generator.gen_var_alloc(ctx, llvm_arg_ty.element_type(), None).unwrap();
ctx.builder.build_store(ppdata, llvm_arg.data().base_ptr(ctx, generator)).unwrap();
call_memcpy_generic( call_memcpy_generic(
ctx, ctx,
buffer.base_ptr(ctx, generator), buffer.base_ptr(ctx, generator),
llvm_arg.ptr_to_data(ctx), ppdata,
llvm_pdata_sizeof, llvm_pdata_sizeof,
llvm_i1.const_zero(), llvm_i1.const_zero(),
); );
@ -526,298 +528,6 @@ fn format_rpc_arg<'ctx>(
arg_slot arg_slot
} }
/// Formats an RPC return value to conform to the expected format required by NAC3.
fn format_rpc_ret<'ctx>(
generator: &mut dyn CodeGenerator,
ctx: &mut CodeGenContext<'ctx, '_>,
ret_ty: Type,
) -> Option<BasicValueEnum<'ctx>> {
// -- receive value:
// T result = {
// void *ret_ptr = alloca(sizeof(T));
// void *ptr = ret_ptr;
// loop: int size = rpc_recv(ptr);
// // Non-zero: Provide `size` bytes of extra storage for variable-length data.
// if(size) { ptr = alloca(size); goto loop; }
// else *(T*)ret_ptr
// }
let llvm_i8 = ctx.ctx.i8_type();
let llvm_i32 = ctx.ctx.i32_type();
let llvm_i8_8 = ctx.ctx.struct_type(&[llvm_i8.array_type(8).into()], false);
let llvm_pi8 = llvm_i8.ptr_type(AddressSpace::default());
let rpc_recv = ctx.module.get_function("rpc_recv").unwrap_or_else(|| {
ctx.module.add_function("rpc_recv", llvm_i32.fn_type(&[llvm_pi8.into()], false), None)
});
if ctx.unifier.unioned(ret_ty, ctx.primitives.none) {
ctx.build_call_or_invoke(rpc_recv, &[llvm_pi8.const_null().into()], "rpc_recv");
return None;
}
let prehead_bb = ctx.builder.get_insert_block().unwrap();
let current_function = prehead_bb.get_parent().unwrap();
let head_bb = ctx.ctx.append_basic_block(current_function, "rpc.head");
let alloc_bb = ctx.ctx.append_basic_block(current_function, "rpc.continue");
let tail_bb = ctx.ctx.append_basic_block(current_function, "rpc.tail");
let llvm_ret_ty = ctx.get_llvm_abi_type(generator, ret_ty);
let result = match &*ctx.unifier.get_ty_immutable(ret_ty) {
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
let llvm_i1 = ctx.ctx.bool_type();
let llvm_usize = generator.get_size_type(ctx.ctx);
// Round `val` up to its modulo `power_of_two`
let round_up = |ctx: &mut CodeGenContext<'ctx, '_>,
val: IntValue<'ctx>,
power_of_two: IntValue<'ctx>| {
debug_assert_eq!(
val.get_type().get_bit_width(),
power_of_two.get_type().get_bit_width()
);
let llvm_val_t = val.get_type();
let max_rem = ctx
.builder
.build_int_sub(power_of_two, llvm_val_t.const_int(1, false), "")
.unwrap();
ctx.builder
.build_and(
ctx.builder.build_int_add(val, max_rem, "").unwrap(),
ctx.builder.build_not(max_rem, "").unwrap(),
"",
)
.unwrap()
};
// Setup types
let (elem_ty, ndims) = unpack_ndarray_var_tys(&mut ctx.unifier, ret_ty);
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
let llvm_ret_ty = NDArrayType::new(generator, ctx.ctx, llvm_elem_ty);
// Allocate the resulting ndarray
// A condition after format_rpc_ret ensures this will not be popped this off.
let ndarray = llvm_ret_ty.new_value(generator, ctx, Some("rpc.result"));
// Setup ndims
let ndims =
if let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) {
assert_eq!(values.len(), 1);
u64::try_from(values[0].clone()).unwrap()
} else {
unreachable!();
};
// Set `ndarray.ndims`
ndarray.store_ndims(ctx, generator, llvm_usize.const_int(ndims, false));
// Allocate `ndarray.shape` [size_t; ndims]
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray.load_ndims(ctx));
/*
ndarray now:
- .ndims: initialized
- .shape: allocated but uninitialized .shape
- .data: uninitialized
*/
let llvm_usize_sizeof = ctx
.builder
.build_int_truncate_or_bit_cast(llvm_usize.size_of(), llvm_usize, "")
.unwrap();
let llvm_pdata_sizeof = ctx
.builder
.build_int_truncate_or_bit_cast(
llvm_ret_ty.element_type().size_of().unwrap(),
llvm_usize,
"",
)
.unwrap();
let llvm_elem_sizeof = ctx
.builder
.build_int_truncate_or_bit_cast(llvm_elem_ty.size_of().unwrap(), llvm_usize, "")
.unwrap();
// Allocates a buffer for the initial RPC'ed object, which is guaranteed to be
// (4 + 4 * ndims) bytes with 8-byte alignment
let sizeof_dims =
ctx.builder.build_int_mul(ndarray.load_ndims(ctx), llvm_usize_sizeof, "").unwrap();
let unaligned_buffer_size =
ctx.builder.build_int_add(sizeof_dims, llvm_pdata_sizeof, "").unwrap();
let buffer_size = round_up(ctx, unaligned_buffer_size, llvm_usize.const_int(8, false));
let stackptr = call_stacksave(ctx, None);
// Just to be absolutely sure, alloca in [i8 x 8] slices to force 8-byte alignment
let buffer = ctx
.builder
.build_array_alloca(
llvm_i8_8,
ctx.builder
.build_int_unsigned_div(buffer_size, llvm_usize.const_int(8, false), "")
.unwrap(),
"rpc.buffer",
)
.unwrap();
let buffer = ctx
.builder
.build_bitcast(buffer, llvm_pi8, "")
.map(BasicValueEnum::into_pointer_value)
.unwrap();
let buffer = ArraySliceValue::from_ptr_val(buffer, buffer_size, None);
// The first call to `rpc_recv` reads the top-level ndarray object: [pdata, shape]
//
// The returned value is the number of bytes for `ndarray.data`.
let ndarray_nbytes = ctx
.build_call_or_invoke(
rpc_recv,
&[buffer.base_ptr(ctx, generator).into()], // Reads [usize; ndims]. NOTE: We are allocated [size_t; ndims].
"rpc.size.next",
)
.map(BasicValueEnum::into_int_value)
.unwrap();
// debug_assert(ndarray_nbytes > 0)
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
ctx.make_assert(
generator,
ctx.builder
.build_int_compare(
IntPredicate::UGT,
ndarray_nbytes,
ndarray_nbytes.get_type().const_zero(),
"",
)
.unwrap(),
"0:AssertionError",
"Unexpected RPC termination for ndarray - Expected data buffer next",
[None, None, None],
ctx.current_loc,
);
}
// Copy shape from the buffer to `ndarray.shape`.
let pbuffer_dims =
unsafe { buffer.ptr_offset_unchecked(ctx, generator, &llvm_pdata_sizeof, None) };
call_memcpy_generic(
ctx,
ndarray.dim_sizes().base_ptr(ctx, generator),
pbuffer_dims,
sizeof_dims,
llvm_i1.const_zero(),
);
// Restore stack from before allocation of buffer
call_stackrestore(ctx, stackptr);
// Allocate `ndarray.data`.
// `ndarray.shape` must be initialized beforehand in this implementation
// (for ndarray.create_data() to know how many elements to allocate)
let num_elements =
call_ndarray_calc_size(generator, ctx, &ndarray.dim_sizes(), (None, None));
// debug_assert(nelems * sizeof(T) >= ndarray_nbytes)
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
let sizeof_data =
ctx.builder.build_int_mul(num_elements, llvm_elem_sizeof, "").unwrap();
ctx.make_assert(
generator,
ctx.builder.build_int_compare(IntPredicate::UGE,
sizeof_data,
ndarray_nbytes,
"",
).unwrap(),
"0:AssertionError",
"Unexpected allocation size request for ndarray data - Expected up to {0} bytes, got {1} bytes",
[Some(sizeof_data), Some(ndarray_nbytes), None],
ctx.current_loc,
);
}
ndarray.create_data(ctx, llvm_elem_ty, num_elements);
let ndarray_data = ndarray.data().base_ptr(ctx, generator);
let ndarray_data_i8 =
ctx.builder.build_pointer_cast(ndarray_data, llvm_pi8, "").unwrap();
// NOTE: Currently on `prehead_bb`
ctx.builder.build_unconditional_branch(head_bb).unwrap();
// Inserting into `head_bb`. Do `rpc_recv` for `data` recursively.
ctx.builder.position_at_end(head_bb);
let phi = ctx.builder.build_phi(llvm_pi8, "rpc.ptr").unwrap();
phi.add_incoming(&[(&ndarray_data_i8, prehead_bb)]);
let alloc_size = ctx
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
.map(BasicValueEnum::into_int_value)
.unwrap();
let is_done = ctx
.builder
.build_int_compare(IntPredicate::EQ, llvm_i32.const_zero(), alloc_size, "rpc.done")
.unwrap();
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
ctx.builder.position_at_end(alloc_bb);
// Align the allocation to sizeof(T)
let alloc_size = round_up(ctx, alloc_size, llvm_elem_sizeof);
let alloc_ptr = ctx
.builder
.build_array_alloca(
llvm_elem_ty,
ctx.builder.build_int_unsigned_div(alloc_size, llvm_elem_sizeof, "").unwrap(),
"rpc.alloc",
)
.unwrap();
let alloc_ptr =
ctx.builder.build_pointer_cast(alloc_ptr, llvm_pi8, "rpc.alloc.ptr").unwrap();
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(tail_bb);
ndarray.as_base_value().into()
}
_ => {
let slot = ctx.builder.build_alloca(llvm_ret_ty, "rpc.ret.slot").unwrap();
let slotgen = ctx.builder.build_bitcast(slot, llvm_pi8, "rpc.ret.ptr").unwrap();
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(head_bb);
let phi = ctx.builder.build_phi(llvm_pi8, "rpc.ptr").unwrap();
phi.add_incoming(&[(&slotgen, prehead_bb)]);
let alloc_size = ctx
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
.unwrap()
.into_int_value();
let is_done = ctx
.builder
.build_int_compare(IntPredicate::EQ, llvm_i32.const_zero(), alloc_size, "rpc.done")
.unwrap();
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
ctx.builder.position_at_end(alloc_bb);
let alloc_ptr =
ctx.builder.build_array_alloca(llvm_pi8, alloc_size, "rpc.alloc").unwrap();
let alloc_ptr =
ctx.builder.build_bitcast(alloc_ptr, llvm_pi8, "rpc.alloc.ptr").unwrap();
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(tail_bb);
ctx.builder.build_load(slot, "rpc.result").unwrap()
}
};
Some(result)
}
fn rpc_codegen_callback_fn<'ctx>( fn rpc_codegen_callback_fn<'ctx>(
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
obj: Option<(Type, ValueEnum<'ctx>)>, obj: Option<(Type, ValueEnum<'ctx>)>,
@ -953,14 +663,63 @@ fn rpc_codegen_callback_fn<'ctx>(
// reclaim stack space used by arguments // reclaim stack space used by arguments
call_stackrestore(ctx, stackptr); call_stackrestore(ctx, stackptr);
let result = format_rpc_ret(generator, ctx, fun.0.ret); // -- receive value:
// T result = {
// void *ret_ptr = alloca(sizeof(T));
// void *ptr = ret_ptr;
// loop: int size = rpc_recv(ptr);
// // Non-zero: Provide `size` bytes of extra storage for variable-length data.
// if(size) { ptr = alloca(size); goto loop; }
// else *(T*)ret_ptr
// }
let rpc_recv = ctx.module.get_function("rpc_recv").unwrap_or_else(|| {
ctx.module.add_function("rpc_recv", int32.fn_type(&[ptr_type.into()], false), None)
});
if !result.is_some_and(|res| res.get_type().is_pointer_type()) { if ctx.unifier.unioned(fun.0.ret, ctx.primitives.none) {
// An RPC returning an NDArray would not touch here. ctx.build_call_or_invoke(rpc_recv, &[ptr_type.const_null().into()], "rpc_recv");
call_stackrestore(ctx, stackptr); return Ok(None);
} }
Ok(result) let prehead_bb = ctx.builder.get_insert_block().unwrap();
let current_function = prehead_bb.get_parent().unwrap();
let head_bb = ctx.ctx.append_basic_block(current_function, "rpc.head");
let alloc_bb = ctx.ctx.append_basic_block(current_function, "rpc.continue");
let tail_bb = ctx.ctx.append_basic_block(current_function, "rpc.tail");
let ret_ty = ctx.get_llvm_abi_type(generator, fun.0.ret);
let need_load = !ret_ty.is_pointer_type();
let slot = ctx.builder.build_alloca(ret_ty, "rpc.ret.slot").unwrap();
let slotgen = ctx.builder.build_bitcast(slot, ptr_type, "rpc.ret.ptr").unwrap();
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(head_bb);
let phi = ctx.builder.build_phi(ptr_type, "rpc.ptr").unwrap();
phi.add_incoming(&[(&slotgen, prehead_bb)]);
let alloc_size = ctx
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
.unwrap()
.into_int_value();
let is_done = ctx
.builder
.build_int_compare(inkwell::IntPredicate::EQ, int32.const_zero(), alloc_size, "rpc.done")
.unwrap();
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
ctx.builder.position_at_end(alloc_bb);
let alloc_ptr = ctx.builder.build_array_alloca(ptr_type, alloc_size, "rpc.alloc").unwrap();
let alloc_ptr = ctx.builder.build_bitcast(alloc_ptr, ptr_type, "rpc.alloc.ptr").unwrap();
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
ctx.builder.build_unconditional_branch(head_bb).unwrap();
ctx.builder.position_at_end(tail_bb);
let result = ctx.builder.build_load(slot, "rpc.result").unwrap();
if need_load {
call_stackrestore(ctx, stackptr);
}
Ok(Some(result))
} }
pub fn attributes_writeback( pub fn attributes_writeback(

View File

@ -448,6 +448,7 @@ impl Nac3 {
pyid_to_type: pyid_to_type.clone(), pyid_to_type: pyid_to_type.clone(),
primitive_ids: self.primitive_ids.clone(), primitive_ids: self.primitive_ids.clone(),
global_value_ids: global_value_ids.clone(), global_value_ids: global_value_ids.clone(),
class_names: Mutex::default(),
name_to_pyid: name_to_pyid.clone(), name_to_pyid: name_to_pyid.clone(),
module: module.clone(), module: module.clone(),
id_to_pyval: RwLock::default(), id_to_pyval: RwLock::default(),
@ -539,6 +540,7 @@ impl Nac3 {
pyid_to_type: pyid_to_type.clone(), pyid_to_type: pyid_to_type.clone(),
primitive_ids: self.primitive_ids.clone(), primitive_ids: self.primitive_ids.clone(),
global_value_ids: global_value_ids.clone(), global_value_ids: global_value_ids.clone(),
class_names: Mutex::default(),
id_to_pyval: RwLock::default(), id_to_pyval: RwLock::default(),
id_to_primitive: RwLock::default(), id_to_primitive: RwLock::default(),
field_to_val: RwLock::default(), field_to_val: RwLock::default(),

View File

@ -23,7 +23,7 @@ use nac3core::{
}, },
}; };
use nac3parser::ast::{self, StrRef}; use nac3parser::ast::{self, StrRef};
use parking_lot::RwLock; use parking_lot::{Mutex, RwLock};
use pyo3::{ use pyo3::{
types::{PyDict, PyTuple}, types::{PyDict, PyTuple},
PyAny, PyObject, PyResult, Python, PyAny, PyObject, PyResult, Python,
@ -79,6 +79,7 @@ pub struct InnerResolver {
pub id_to_primitive: RwLock<HashMap<u64, PrimitiveValue>>, pub id_to_primitive: RwLock<HashMap<u64, PrimitiveValue>>,
pub field_to_val: RwLock<HashMap<ResolverField, Option<PyFieldHandle>>>, pub field_to_val: RwLock<HashMap<ResolverField, Option<PyFieldHandle>>>,
pub global_value_ids: Arc<RwLock<HashMap<u64, PyObject>>>, pub global_value_ids: Arc<RwLock<HashMap<u64, PyObject>>>,
pub class_names: Mutex<HashMap<StrRef, Type>>,
pub pyid_to_def: Arc<RwLock<HashMap<u64, DefinitionId>>>, pub pyid_to_def: Arc<RwLock<HashMap<u64, DefinitionId>>>,
pub pyid_to_type: Arc<RwLock<HashMap<u64, Type>>>, pub pyid_to_type: Arc<RwLock<HashMap<u64, Type>>>,
pub primitive_ids: PrimitivePythonId, pub primitive_ids: PrimitivePythonId,

View File

@ -1,6 +1,6 @@
#include "irrt/exception.hpp" #include <irrt/exception.hpp>
#include "irrt/int_types.hpp" #include <irrt/int_types.hpp>
#include "irrt/list.hpp" #include <irrt/list.hpp>
#include "irrt/math.hpp" #include <irrt/math.hpp>
#include "irrt/ndarray.hpp" #include <irrt/ndarray.hpp>
#include "irrt/slice.hpp" #include <irrt/slice.hpp>

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "irrt/int_types.hpp" #include <irrt/int_types.hpp>
template<typename SizeT> template<typename SizeT>
struct CSlice { struct CSlice {

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "irrt/cslice.hpp" #include <irrt/cslice.hpp>
#include "irrt/int_types.hpp" #include <irrt/int_types.hpp>
/** /**
* @brief The int type of ARTIQ exception IDs. * @brief The int type of ARTIQ exception IDs.

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "irrt/int_types.hpp" #include <irrt/int_types.hpp>
#include "irrt/math_util.hpp" #include <irrt/math_util.hpp>
extern "C" { extern "C" {
// Handle list assignment and dropping part of the list when // Handle list assignment and dropping part of the list when

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "irrt/int_types.hpp" #include <irrt/int_types.hpp>
namespace { namespace {
template<typename SizeT> template<typename SizeT>

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "irrt/int_types.hpp" #include <irrt/int_types.hpp>
extern "C" { extern "C" {
SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) { SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) {

View File

@ -9,7 +9,6 @@ use crate::codegen::classes::{
}; };
use crate::codegen::expr::destructure_range; use crate::codegen::expr::destructure_range;
use crate::codegen::irrt::calculate_len_for_slice_range; use crate::codegen::irrt::calculate_len_for_slice_range;
use crate::codegen::macros::codegen_unreachable;
use crate::codegen::numpy::ndarray_elementwise_unaryop_impl; use crate::codegen::numpy::ndarray_elementwise_unaryop_impl;
use crate::codegen::stmt::gen_for_callback_incrementing; use crate::codegen::stmt::gen_for_callback_incrementing;
use crate::codegen::{extern_fns, irrt, llvm_intrinsics, numpy, CodeGenContext, CodeGenerator}; use crate::codegen::{extern_fns, irrt, llvm_intrinsics, numpy, CodeGenContext, CodeGenerator};
@ -21,8 +20,7 @@ use crate::typecheck::typedef::{Type, TypeEnum};
/// ///
/// The generated message will contain the function name and the name of the unsupported type. /// The generated message will contain the function name and the name of the unsupported type.
fn unsupported_type(ctx: &CodeGenContext<'_, '_>, fn_name: &str, tys: &[Type]) -> ! { fn unsupported_type(ctx: &CodeGenContext<'_, '_>, fn_name: &str, tys: &[Type]) -> ! {
codegen_unreachable!( unreachable!(
ctx,
"{fn_name}() not supported for '{}'", "{fn_name}() not supported for '{}'",
tys.iter().map(|ty| format!("'{}'", ctx.unifier.stringify(*ty))).join(", "), tys.iter().map(|ty| format!("'{}'", ctx.unifier.stringify(*ty))).join(", "),
) )
@ -84,7 +82,7 @@ pub fn call_len<'ctx, G: CodeGenerator + ?Sized>(
ctx.builder.build_int_truncate_or_bit_cast(len, llvm_i32, "len").unwrap() ctx.builder.build_int_truncate_or_bit_cast(len, llvm_i32, "len").unwrap()
} }
_ => codegen_unreachable!(ctx), _ => unreachable!(),
} }
}) })
} }
@ -786,7 +784,7 @@ pub fn call_numpy_minimum<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -890,7 +888,7 @@ pub fn call_numpy_max_min<'ctx, G: CodeGenerator + ?Sized>(
match fn_name { match fn_name {
"np_argmin" | "np_argmax" => llvm_int64.const_zero().into(), "np_argmin" | "np_argmax" => llvm_int64.const_zero().into(),
"np_max" | "np_min" => a, "np_max" | "np_min" => a,
_ => codegen_unreachable!(ctx), _ => unreachable!(),
} }
} }
BasicValueEnum::PointerValue(n) BasicValueEnum::PointerValue(n)
@ -945,7 +943,7 @@ pub fn call_numpy_max_min<'ctx, G: CodeGenerator + ?Sized>(
"np_argmax" | "np_max" => { "np_argmax" | "np_max" => {
call_max(ctx, (elem_ty, accumulator), (elem_ty, elem)) call_max(ctx, (elem_ty, accumulator), (elem_ty, elem))
} }
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
let updated_idx = match (accumulator, result) { let updated_idx = match (accumulator, result) {
@ -982,7 +980,7 @@ pub fn call_numpy_max_min<'ctx, G: CodeGenerator + ?Sized>(
match fn_name { match fn_name {
"np_argmin" | "np_argmax" => ctx.builder.build_load(res_idx, "").unwrap(), "np_argmin" | "np_argmax" => ctx.builder.build_load(res_idx, "").unwrap(),
"np_max" | "np_min" => ctx.builder.build_load(accumulator_addr, "").unwrap(), "np_max" | "np_min" => ctx.builder.build_load(accumulator_addr, "").unwrap(),
_ => codegen_unreachable!(ctx), _ => unreachable!(),
} }
} }
@ -1048,7 +1046,7 @@ pub fn call_numpy_maximum<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -1488,7 +1486,7 @@ pub fn call_numpy_arctan2<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -1555,7 +1553,7 @@ pub fn call_numpy_copysign<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -1622,7 +1620,7 @@ pub fn call_numpy_fmax<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -1689,7 +1687,7 @@ pub fn call_numpy_fmin<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -1812,7 +1810,7 @@ pub fn call_numpy_hypot<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };
@ -1879,7 +1877,7 @@ pub fn call_numpy_nextafter<'ctx, G: CodeGenerator + ?Sized>(
} else if is_ndarray2 { } else if is_ndarray2 {
unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0 unpack_ndarray_var_tys(&mut ctx.unifier, x2_ty).0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty }; let x1_scalar_ty = if is_ndarray1 { dtype } else { x1_ty };

View File

@ -1404,7 +1404,7 @@ impl<'ctx> NDArrayValue<'ctx> {
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr` /// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
/// on the field. /// on the field.
pub fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> { fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
let llvm_i32 = ctx.ctx.i32_type(); let llvm_i32 = ctx.ctx.i32_type();
let var_name = self.name.map(|v| format!("{v}.data.addr")).unwrap_or_default(); let var_name = self.name.map(|v| format!("{v}.data.addr")).unwrap_or_default();

View File

@ -11,7 +11,6 @@ use crate::{
call_expect, call_float_floor, call_float_pow, call_float_powi, call_int_smax, call_expect, call_float_floor, call_float_pow, call_float_powi, call_int_smax,
call_int_umin, call_memcpy_generic, call_int_umin, call_memcpy_generic,
}, },
macros::codegen_unreachable,
need_sret, numpy, need_sret, numpy,
stmt::{ stmt::{
gen_for_callback_incrementing, gen_if_callback, gen_if_else_expr_callback, gen_raise, gen_for_callback_incrementing, gen_if_callback, gen_if_else_expr_callback, gen_raise,
@ -113,7 +112,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
let obj_id = match &*self.unifier.get_ty(ty) { let obj_id = match &*self.unifier.get_ty(ty) {
TypeEnum::TObj { obj_id, .. } => *obj_id, TypeEnum::TObj { obj_id, .. } => *obj_id,
// we cannot have other types, virtual type should be handled by function calls // we cannot have other types, virtual type should be handled by function calls
_ => codegen_unreachable!(self), _ => unreachable!(),
}; };
let def = &self.top_level.definitions.read()[obj_id.0]; let def = &self.top_level.definitions.read()[obj_id.0];
let (index, value) = if let TopLevelDef::Class { fields, attributes, .. } = &*def.read() { let (index, value) = if let TopLevelDef::Class { fields, attributes, .. } = &*def.read() {
@ -124,7 +123,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
(attribute_index.0, Some(attribute_index.1 .2.clone())) (attribute_index.0, Some(attribute_index.1 .2.clone()))
} }
} else { } else {
codegen_unreachable!(self) unreachable!()
}; };
(index, value) (index, value)
} }
@ -134,7 +133,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
TypeEnum::TObj { fields, .. } => { TypeEnum::TObj { fields, .. } => {
fields.iter().find_position(|x| *x.0 == attr).unwrap().0 fields.iter().find_position(|x| *x.0 == attr).unwrap().0
} }
_ => codegen_unreachable!(self), _ => unreachable!(),
} }
} }
@ -189,7 +188,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} }
_ => codegen_unreachable!(self, "must be option type"), _ => unreachable!("must be option type"),
}; };
let val = self.gen_symbol_val(generator, v, ty); let val = self.gen_symbol_val(generator, v, ty);
let ptr = generator let ptr = generator
@ -205,7 +204,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} }
_ => codegen_unreachable!(self, "must be option type"), _ => unreachable!("must be option type"),
}; };
let actual_ptr_type = let actual_ptr_type =
self.get_llvm_type(generator, ty).ptr_type(AddressSpace::default()); self.get_llvm_type(generator, ty).ptr_type(AddressSpace::default());
@ -272,7 +271,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
{ {
self.ctx.i64_type() self.ctx.i64_type()
} else { } else {
codegen_unreachable!(self) unreachable!()
}; };
Some(ty.const_int(*val as u64, false).into()) Some(ty.const_int(*val as u64, false).into())
} }
@ -286,7 +285,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
let (types, is_vararg_ctx) = if let TypeEnum::TTuple { ty, is_vararg_ctx } = &*ty { let (types, is_vararg_ctx) = if let TypeEnum::TTuple { ty, is_vararg_ctx } = &*ty {
(ty.clone(), *is_vararg_ctx) (ty.clone(), *is_vararg_ctx)
} else { } else {
codegen_unreachable!(self) unreachable!()
}; };
let values = zip(types, v.iter()) let values = zip(types, v.iter())
.map_while(|(ty, v)| self.gen_const(generator, v, ty)) .map_while(|(ty, v)| self.gen_const(generator, v, ty))
@ -331,7 +330,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
None None
} }
_ => codegen_unreachable!(self), _ => unreachable!(),
} }
} }
@ -345,7 +344,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
signed: bool, signed: bool,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
let (BasicValueEnum::IntValue(lhs), BasicValueEnum::IntValue(rhs)) = (lhs, rhs) else { let (BasicValueEnum::IntValue(lhs), BasicValueEnum::IntValue(rhs)) = (lhs, rhs) else {
codegen_unreachable!(self) unreachable!()
}; };
let float = self.ctx.f64_type(); let float = self.ctx.f64_type();
match (op, signed) { match (op, signed) {
@ -420,7 +419,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
.build_right_shift(lhs, rhs, signed, "rshift") .build_right_shift(lhs, rhs, signed, "rshift")
.map(Into::into) .map(Into::into)
.unwrap(), .unwrap(),
_ => codegen_unreachable!(self), _ => unreachable!(),
} }
} }
@ -432,7 +431,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
(Operator::Pow, s) => integer_power(generator, self, lhs, rhs, s).into(), (Operator::Pow, s) => integer_power(generator, self, lhs, rhs, s).into(),
// special implementation? // special implementation?
(Operator::MatMult, _) => codegen_unreachable!(self), (Operator::MatMult, _) => unreachable!(),
} }
} }
@ -444,8 +443,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
rhs: BasicValueEnum<'ctx>, rhs: BasicValueEnum<'ctx>,
) -> BasicValueEnum<'ctx> { ) -> BasicValueEnum<'ctx> {
let (BasicValueEnum::FloatValue(lhs), BasicValueEnum::FloatValue(rhs)) = (lhs, rhs) else { let (BasicValueEnum::FloatValue(lhs), BasicValueEnum::FloatValue(rhs)) = (lhs, rhs) else {
codegen_unreachable!( unreachable!(
self,
"Expected (FloatValue, FloatValue), got ({}, {})", "Expected (FloatValue, FloatValue), got ({}, {})",
lhs.get_type(), lhs.get_type(),
rhs.get_type() rhs.get_type()
@ -689,7 +687,7 @@ pub fn gen_constructor<'ctx, 'a, G: CodeGenerator>(
def: &TopLevelDef, def: &TopLevelDef,
params: Vec<(Option<StrRef>, ValueEnum<'ctx>)>, params: Vec<(Option<StrRef>, ValueEnum<'ctx>)>,
) -> Result<BasicValueEnum<'ctx>, String> { ) -> Result<BasicValueEnum<'ctx>, String> {
let TopLevelDef::Class { methods, .. } = def else { codegen_unreachable!(ctx) }; let TopLevelDef::Class { methods, .. } = def else { unreachable!() };
// TODO: what about other fields that require alloca? // TODO: what about other fields that require alloca?
let fun_id = methods.iter().find(|method| method.0 == "__init__".into()).map(|method| method.2); let fun_id = methods.iter().find(|method| method.0 == "__init__".into()).map(|method| method.2);
@ -721,7 +719,7 @@ pub fn gen_func_instance<'ctx>(
key, key,
) = fun ) = fun
else { else {
codegen_unreachable!(ctx) unreachable!()
}; };
if let Some(sym) = instance_to_symbol.get(&key) { if let Some(sym) = instance_to_symbol.get(&key) {
@ -753,7 +751,7 @@ pub fn gen_func_instance<'ctx>(
.collect(); .collect();
let mut signature = store.from_signature(&mut ctx.unifier, &ctx.primitives, sign, &mut cache); let mut signature = store.from_signature(&mut ctx.unifier, &ctx.primitives, sign, &mut cache);
let ConcreteTypeEnum::TFunc { args, .. } = &mut signature else { codegen_unreachable!(ctx) }; let ConcreteTypeEnum::TFunc { args, .. } = &mut signature else { unreachable!() };
if let Some(obj) = &obj { if let Some(obj) = &obj {
let zelf = store.from_unifier_type(&mut ctx.unifier, &ctx.primitives, obj.0, &mut cache); let zelf = store.from_unifier_type(&mut ctx.unifier, &ctx.primitives, obj.0, &mut cache);
@ -1119,7 +1117,7 @@ pub fn gen_comprehension<'ctx, G: CodeGenerator>(
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
expr: &Expr<Option<Type>>, expr: &Expr<Option<Type>>,
) -> Result<Option<BasicValueEnum<'ctx>>, String> { ) -> Result<Option<BasicValueEnum<'ctx>>, String> {
let ExprKind::ListComp { elt, generators } = &expr.node else { codegen_unreachable!(ctx) }; let ExprKind::ListComp { elt, generators } = &expr.node else { unreachable!() };
let current = ctx.builder.get_insert_block().unwrap().get_parent().unwrap(); let current = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
@ -1378,13 +1376,13 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
if let TypeEnum::TObj { params, .. } = &*ctx.unifier.get_ty_immutable(ty1) { if let TypeEnum::TObj { params, .. } = &*ctx.unifier.get_ty_immutable(ty1) {
ctx.unifier.get_representative(*params.iter().next().unwrap().1) ctx.unifier.get_representative(*params.iter().next().unwrap().1)
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let elem_ty2 = let elem_ty2 =
if let TypeEnum::TObj { params, .. } = &*ctx.unifier.get_ty_immutable(ty2) { if let TypeEnum::TObj { params, .. } = &*ctx.unifier.get_ty_immutable(ty2) {
ctx.unifier.get_representative(*params.iter().next().unwrap().1) ctx.unifier.get_representative(*params.iter().next().unwrap().1)
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
debug_assert!(ctx.unifier.unioned(elem_ty1, elem_ty2)); debug_assert!(ctx.unifier.unioned(elem_ty1, elem_ty2));
@ -1457,7 +1455,7 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
(elem_ty, left_val, right_val) (elem_ty, left_val, right_val)
@ -1467,12 +1465,12 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
(elem_ty, right_val, left_val) (elem_ty, right_val, left_val)
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let list_val = let list_val =
ListValue::from_ptr_val(list_val.into_pointer_value(), llvm_usize, None); ListValue::from_ptr_val(list_val.into_pointer_value(), llvm_usize, None);
@ -1639,7 +1637,7 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
} else { } else {
let left_ty_enum = ctx.unifier.get_ty_immutable(left_ty.unwrap()); let left_ty_enum = ctx.unifier.get_ty_immutable(left_ty.unwrap());
let TypeEnum::TObj { fields, obj_id, .. } = left_ty_enum.as_ref() else { let TypeEnum::TObj { fields, obj_id, .. } = left_ty_enum.as_ref() else {
codegen_unreachable!(ctx, "must be tobj") unreachable!("must be tobj")
}; };
let (op_name, id) = { let (op_name, id) = {
let normal_method_name = Binop::normal(op.base).op_info().method_name; let normal_method_name = Binop::normal(op.base).op_info().method_name;
@ -1660,19 +1658,19 @@ pub fn gen_binop_expr_with_values<'ctx, G: CodeGenerator>(
} else { } else {
let left_enum_ty = ctx.unifier.get_ty_immutable(left_ty.unwrap()); let left_enum_ty = ctx.unifier.get_ty_immutable(left_ty.unwrap());
let TypeEnum::TObj { fields, .. } = left_enum_ty.as_ref() else { let TypeEnum::TObj { fields, .. } = left_enum_ty.as_ref() else {
codegen_unreachable!(ctx, "must be tobj") unreachable!("must be tobj")
}; };
let fn_ty = fields.get(&op_name).unwrap().0; let fn_ty = fields.get(&op_name).unwrap().0;
let fn_ty_enum = ctx.unifier.get_ty_immutable(fn_ty); let fn_ty_enum = ctx.unifier.get_ty_immutable(fn_ty);
let TypeEnum::TFunc(sig) = fn_ty_enum.as_ref() else { codegen_unreachable!(ctx) }; let TypeEnum::TFunc(sig) = fn_ty_enum.as_ref() else { unreachable!() };
sig.clone() sig.clone()
}; };
let fun_id = { let fun_id = {
let defs = ctx.top_level.definitions.read(); let defs = ctx.top_level.definitions.read();
let obj_def = defs.get(id.0).unwrap().read(); let obj_def = defs.get(id.0).unwrap().read();
let TopLevelDef::Class { methods, .. } = &*obj_def else { codegen_unreachable!(ctx) }; let TopLevelDef::Class { methods, .. } = &*obj_def else { unreachable!() };
methods.iter().find(|method| method.0 == op_name).unwrap().2 methods.iter().find(|method| method.0 == op_name).unwrap().2
}; };
@ -1803,8 +1801,7 @@ pub fn gen_unaryop_expr_with_values<'ctx, G: CodeGenerator>(
if op == ast::Unaryop::Invert { if op == ast::Unaryop::Invert {
ast::Unaryop::Not ast::Unaryop::Not
} else { } else {
codegen_unreachable!( unreachable!(
ctx,
"ufunc {} not supported for ndarray[bool, N]", "ufunc {} not supported for ndarray[bool, N]",
op.op_info().method_name, op.op_info().method_name,
) )
@ -1871,8 +1868,8 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
{ {
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let (Some(left_ty), lhs) = left else { codegen_unreachable!(ctx) }; let (Some(left_ty), lhs) = left else { unreachable!() };
let (Some(right_ty), rhs) = comparators[0] else { codegen_unreachable!(ctx) }; let (Some(right_ty), rhs) = comparators[0] else { unreachable!() };
let op = ops[0]; let op = ops[0];
let is_ndarray1 = let is_ndarray1 =
@ -1979,7 +1976,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
let op = match op { let op = match op {
ast::Cmpop::Eq | ast::Cmpop::Is => IntPredicate::EQ, ast::Cmpop::Eq | ast::Cmpop::Is => IntPredicate::EQ,
ast::Cmpop::NotEq => IntPredicate::NE, ast::Cmpop::NotEq => IntPredicate::NE,
_ if left_ty == ctx.primitives.bool => codegen_unreachable!(ctx), _ if left_ty == ctx.primitives.bool => unreachable!(),
ast::Cmpop::Lt => { ast::Cmpop::Lt => {
if use_unsigned_ops { if use_unsigned_ops {
IntPredicate::ULT IntPredicate::ULT
@ -2008,7 +2005,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
IntPredicate::SGE IntPredicate::SGE
} }
} }
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
ctx.builder.build_int_compare(op, lhs, rhs, "cmp").unwrap() ctx.builder.build_int_compare(op, lhs, rhs, "cmp").unwrap()
@ -2025,7 +2022,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
ast::Cmpop::LtE => inkwell::FloatPredicate::OLE, ast::Cmpop::LtE => inkwell::FloatPredicate::OLE,
ast::Cmpop::Gt => inkwell::FloatPredicate::OGT, ast::Cmpop::Gt => inkwell::FloatPredicate::OGT,
ast::Cmpop::GtE => inkwell::FloatPredicate::OGE, ast::Cmpop::GtE => inkwell::FloatPredicate::OGE,
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
ctx.builder.build_float_compare(op, lhs, rhs, "cmp").unwrap() ctx.builder.build_float_compare(op, lhs, rhs, "cmp").unwrap()
} else if left_ty == ctx.primitives.str { } else if left_ty == ctx.primitives.str {
@ -2157,7 +2154,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
match (op, val) { match (op, val) {
(Cmpop::Eq, true) | (Cmpop::NotEq, false) => llvm_i1.const_all_ones(), (Cmpop::Eq, true) | (Cmpop::NotEq, false) => llvm_i1.const_all_ones(),
(Cmpop::Eq, false) | (Cmpop::NotEq, true) => llvm_i1.const_zero(), (Cmpop::Eq, false) | (Cmpop::NotEq, true) => llvm_i1.const_zero(),
(_, _) => codegen_unreachable!(ctx), (_, _) => unreachable!(),
} }
}; };
@ -2170,14 +2167,14 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let right_elem_ty = if let TypeEnum::TObj { params, .. } = let right_elem_ty = if let TypeEnum::TObj { params, .. } =
&*ctx.unifier.get_ty_immutable(right_ty) &*ctx.unifier.get_ty_immutable(right_ty)
{ {
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
if !ctx.unifier.unioned(left_elem_ty, right_elem_ty) { if !ctx.unifier.unioned(left_elem_ty, right_elem_ty) {
@ -2385,10 +2382,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
}) })
.map(BasicValueEnum::into_int_value)?; .map(BasicValueEnum::into_int_value)?;
Ok(ctx.builder.build_not( Ok(ctx.builder.build_not(cmp, "").unwrap())
generator.bool_to_i1(ctx, cmp),
"",
).unwrap())
}, },
|_, ctx| { |_, ctx| {
let bb = ctx.builder.get_insert_block().unwrap(); let bb = ctx.builder.get_insert_block().unwrap();
@ -2517,7 +2511,7 @@ fn gen_ndarray_subscript_expr<'ctx, G: CodeGenerator>(
let llvm_usize = generator.get_size_type(ctx.ctx); let llvm_usize = generator.get_size_type(ctx.ctx);
let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) else { let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) else {
codegen_unreachable!(ctx) unreachable!()
}; };
let ndims = values let ndims = values
@ -2869,7 +2863,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
.const_null() .const_null()
.into() .into()
} }
_ => codegen_unreachable!(ctx, "must be option type"), _ => unreachable!("must be option type"),
} }
} }
ExprKind::Name { id, .. } => match ctx.var_assignment.get(id) { ExprKind::Name { id, .. } => match ctx.var_assignment.get(id) {
@ -2879,7 +2873,29 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()), Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()),
None => { None => {
let resolver = ctx.resolver.clone(); let resolver = ctx.resolver.clone();
resolver.get_symbol_value(*id, ctx).unwrap() if let Some(res) = resolver.get_symbol_value(*id, ctx) {
res
} else {
// Allow "raise Exception" short form
let def_id = resolver.get_identifier_def(*id).map_err(|e| {
format!("{} (at {})", e.iter().next().unwrap(), expr.location)
})?;
let def = ctx.top_level.definitions.read();
if let TopLevelDef::Class { constructor, .. } = *def[def_id.0].read() {
let TypeEnum::TFunc(signature) =
ctx.unifier.get_ty(constructor.unwrap()).as_ref().clone()
else {
return Err(format!(
"Failed to resolve symbol {} (at {})",
id, expr.location
));
};
return Ok(generator
.gen_call(ctx, None, (&signature, def_id), Vec::default())?
.map(Into::into));
}
return Err(format!("Failed to resolve symbol {} (at {})", id, expr.location));
}
} }
}, },
ExprKind::List { elts, .. } => { ExprKind::List { elts, .. } => {
@ -2908,7 +2924,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
*params.iter().next().unwrap().1 *params.iter().next().unwrap().1
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
if let TypeEnum::TVar { .. } = &*ctx.unifier.get_ty_immutable(ty) { if let TypeEnum::TVar { .. } = &*ctx.unifier.get_ty_immutable(ty) {
@ -3002,9 +3018,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
return generator.gen_expr(ctx, &modified_expr); return generator.gen_expr(ctx, &modified_expr);
} }
None => { None => unreachable!("Function Type should not have attributes"),
codegen_unreachable!(ctx, "Function Type should not have attributes")
}
} }
} else if let TypeEnum::TObj { obj_id, fields, params } = &*ctx.unifier.get_ty(c) { } else if let TypeEnum::TObj { obj_id, fields, params } = &*ctx.unifier.get_ty(c) {
if fields.is_empty() && params.is_empty() { if fields.is_empty() && params.is_empty() {
@ -3026,7 +3040,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
return generator.gen_expr(ctx, &modified_expr); return generator.gen_expr(ctx, &modified_expr);
} }
None => codegen_unreachable!(ctx), None => unreachable!(),
} }
} }
} }
@ -3128,7 +3142,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
} }
(Some(a), None) => a.into(), (Some(a), None) => a.into(),
(None, Some(b)) => b.into(), (None, Some(b)) => b.into(),
(None, None) => codegen_unreachable!(ctx), (None, None) => unreachable!(),
} }
} }
ExprKind::BinOp { op, left, right } => { ExprKind::BinOp { op, left, right } => {
@ -3218,9 +3232,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
ctx.unifier.get_call_signature(*call).unwrap() ctx.unifier.get_call_signature(*call).unwrap()
} else { } else {
let ty = func.custom.unwrap(); let ty = func.custom.unwrap();
let TypeEnum::TFunc(sign) = &*ctx.unifier.get_ty(ty) else { let TypeEnum::TFunc(sign) = &*ctx.unifier.get_ty(ty) else { unreachable!() };
codegen_unreachable!(ctx)
};
sign.clone() sign.clone()
}; };
@ -3239,26 +3251,17 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
let Some(val) = generator.gen_expr(ctx, value)? else { return Ok(None) }; let Some(val) = generator.gen_expr(ctx, value)? else { return Ok(None) };
// Handle Class Method calls // Handle Class Method calls
// The attribute will be `DefinitionId` of the method if the call is to one of the parent methods
let func_id = attr.to_string().parse::<usize>();
let id = if let TypeEnum::TObj { obj_id, .. } = let id = if let TypeEnum::TObj { obj_id, .. } =
&*ctx.unifier.get_ty(value.custom.unwrap()) &*ctx.unifier.get_ty(value.custom.unwrap())
{ {
*obj_id *obj_id
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let fun_id = {
// Use the `DefinitionID` from attribute if it is available
let fun_id = if let Ok(func_id) = func_id {
DefinitionId(func_id)
} else {
let defs = ctx.top_level.definitions.read(); let defs = ctx.top_level.definitions.read();
let obj_def = defs.get(id.0).unwrap().read(); let obj_def = defs.get(id.0).unwrap().read();
let TopLevelDef::Class { methods, .. } = &*obj_def else { let TopLevelDef::Class { methods, .. } = &*obj_def else { unreachable!() };
codegen_unreachable!(ctx)
};
methods.iter().find(|method| method.0 == *attr).unwrap().2 methods.iter().find(|method| method.0 == *attr).unwrap().2
}; };
@ -3329,9 +3332,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
.unwrap(), .unwrap(),
)); ));
} }
ValueEnum::Dynamic(_) => { ValueEnum::Dynamic(_) => unreachable!("option must be static or ptr"),
codegen_unreachable!(ctx, "option must be static or ptr")
}
} }
} }
@ -3480,10 +3481,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
if let ExprKind::Constant { value: Constant::Int(v), .. } = &slice.node { if let ExprKind::Constant { value: Constant::Int(v), .. } = &slice.node {
(*v).try_into().unwrap() (*v).try_into().unwrap()
} else { } else {
codegen_unreachable!( unreachable!("tuple subscript must be const int after type check");
ctx,
"tuple subscript must be const int after type check"
);
}; };
match generator.gen_expr(ctx, value)? { match generator.gen_expr(ctx, value)? {
Some(ValueEnum::Dynamic(v)) => { Some(ValueEnum::Dynamic(v)) => {
@ -3506,10 +3504,7 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
None => return Ok(None), None => return Ok(None),
} }
} }
_ => codegen_unreachable!( _ => unreachable!("should not be other subscriptable types after type check"),
ctx,
"should not be other subscriptable types after type check"
),
} }
} }
ExprKind::ListComp { .. } => { ExprKind::ListComp { .. } => {

View File

@ -3,13 +3,12 @@ use crate::{symbol_resolver::SymbolResolver, typecheck::typedef::Type};
use super::{ use super::{
classes::{ classes::{
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue, ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue,
TypedArrayLikeAccessor, TypedArrayLikeAdapter, UntypedArrayLikeAccessor, TypedArrayLikeAdapter, UntypedArrayLikeAccessor,
}, },
llvm_intrinsics, llvm_intrinsics, CodeGenContext, CodeGenerator,
macros::codegen_unreachable,
stmt::gen_for_callback_incrementing,
CodeGenContext, CodeGenerator,
}; };
use crate::codegen::classes::TypedArrayLikeAccessor;
use crate::codegen::stmt::gen_for_callback_incrementing;
use inkwell::{ use inkwell::{
attributes::{Attribute, AttributeLoc}, attributes::{Attribute, AttributeLoc},
context::Context, context::Context,
@ -75,7 +74,7 @@ pub fn integer_power<'ctx, G: CodeGenerator + ?Sized>(
(64, 64, true) => "__nac3_int_exp_int64_t", (64, 64, true) => "__nac3_int_exp_int64_t",
(32, 32, false) => "__nac3_int_exp_uint32_t", (32, 32, false) => "__nac3_int_exp_uint32_t",
(64, 64, false) => "__nac3_int_exp_uint64_t", (64, 64, false) => "__nac3_int_exp_uint64_t",
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
let base_type = base.get_type(); let base_type = base.get_type();
let pow_fun = ctx.module.get_function(symbol).unwrap_or_else(|| { let pow_fun = ctx.module.get_function(symbol).unwrap_or_else(|| {
@ -461,7 +460,7 @@ pub fn list_slice_assignment<'ctx, G: CodeGenerator + ?Sized>(
BasicTypeEnum::IntType(t) => t.size_of(), BasicTypeEnum::IntType(t) => t.size_of(),
BasicTypeEnum::PointerType(t) => t.size_of(), BasicTypeEnum::PointerType(t) => t.size_of(),
BasicTypeEnum::StructType(t) => t.size_of().unwrap(), BasicTypeEnum::StructType(t) => t.size_of().unwrap(),
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
ctx.builder.build_int_truncate_or_bit_cast(s, int32, "size").unwrap() ctx.builder.build_int_truncate_or_bit_cast(s, int32, "size").unwrap()
} }
@ -606,7 +605,7 @@ where
let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() { let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_size", 32 => "__nac3_ndarray_calc_size",
64 => "__nac3_ndarray_calc_size64", 64 => "__nac3_ndarray_calc_size64",
bw => codegen_unreachable!(ctx, "Unsupported size type bit width: {}", bw), bw => unreachable!("Unsupported size type bit width: {}", bw),
}; };
let ndarray_calc_size_fn_t = llvm_usize.fn_type( let ndarray_calc_size_fn_t = llvm_usize.fn_type(
&[llvm_pusize.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()], &[llvm_pusize.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()],
@ -657,7 +656,7 @@ pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
let ndarray_calc_nd_indices_fn_name = match llvm_usize.get_bit_width() { let ndarray_calc_nd_indices_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_nd_indices", 32 => "__nac3_ndarray_calc_nd_indices",
64 => "__nac3_ndarray_calc_nd_indices64", 64 => "__nac3_ndarray_calc_nd_indices64",
bw => codegen_unreachable!(ctx, "Unsupported size type bit width: {}", bw), bw => unreachable!("Unsupported size type bit width: {}", bw),
}; };
let ndarray_calc_nd_indices_fn = let ndarray_calc_nd_indices_fn =
ctx.module.get_function(ndarray_calc_nd_indices_fn_name).unwrap_or_else(|| { ctx.module.get_function(ndarray_calc_nd_indices_fn_name).unwrap_or_else(|| {
@ -726,7 +725,7 @@ where
let ndarray_flatten_index_fn_name = match llvm_usize.get_bit_width() { let ndarray_flatten_index_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_flatten_index", 32 => "__nac3_ndarray_flatten_index",
64 => "__nac3_ndarray_flatten_index64", 64 => "__nac3_ndarray_flatten_index64",
bw => codegen_unreachable!(ctx, "Unsupported size type bit width: {}", bw), bw => unreachable!("Unsupported size type bit width: {}", bw),
}; };
let ndarray_flatten_index_fn = let ndarray_flatten_index_fn =
ctx.module.get_function(ndarray_flatten_index_fn_name).unwrap_or_else(|| { ctx.module.get_function(ndarray_flatten_index_fn_name).unwrap_or_else(|| {
@ -794,7 +793,7 @@ pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() { let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_broadcast", 32 => "__nac3_ndarray_calc_broadcast",
64 => "__nac3_ndarray_calc_broadcast64", 64 => "__nac3_ndarray_calc_broadcast64",
bw => codegen_unreachable!(ctx, "Unsupported size type bit width: {}", bw), bw => unreachable!("Unsupported size type bit width: {}", bw),
}; };
let ndarray_calc_broadcast_fn = let ndarray_calc_broadcast_fn =
ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| { ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
@ -914,7 +913,7 @@ pub fn call_ndarray_calc_broadcast_index<
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() { let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
32 => "__nac3_ndarray_calc_broadcast_idx", 32 => "__nac3_ndarray_calc_broadcast_idx",
64 => "__nac3_ndarray_calc_broadcast_idx64", 64 => "__nac3_ndarray_calc_broadcast_idx64",
bw => codegen_unreachable!(ctx, "Unsupported size type bit width: {}", bw), bw => unreachable!("Unsupported size type bit width: {}", bw),
}; };
let ndarray_calc_broadcast_fn = let ndarray_calc_broadcast_fn =
ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| { ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {

View File

@ -51,22 +51,6 @@ mod test;
use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore}; use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore};
pub use generator::{CodeGenerator, DefaultCodeGenerator}; pub use generator::{CodeGenerator, DefaultCodeGenerator};
mod macros {
/// Codegen-variant of [`std::unreachable`] which accepts an instance of [`CodeGenContext`] as
/// its first argument to provide Python source information to indicate the codegen location
/// causing the assertion.
macro_rules! codegen_unreachable {
($ctx:expr $(,)?) => {
std::unreachable!("unreachable code while processing {}", &$ctx.current_loc)
};
($ctx:expr, $($arg:tt)*) => {
std::unreachable!("unreachable code while processing {}: {}", &$ctx.current_loc, std::format!("{}", std::format_args!($($arg)+)))
};
}
pub(crate) use codegen_unreachable;
}
#[derive(Default)] #[derive(Default)]
pub struct StaticValueStore { pub struct StaticValueStore {
pub lookup: HashMap<Vec<(usize, u64)>, usize>, pub lookup: HashMap<Vec<(usize, u64)>, usize>,

View File

@ -18,7 +18,7 @@ impl<'ctx> Model<'ctx> for Any<'ctx> {
type Value = BasicValueEnum<'ctx>; type Value = BasicValueEnum<'ctx>;
type Type = BasicTypeEnum<'ctx>; type Type = BasicTypeEnum<'ctx>;
fn llvm_type<G: CodeGenerator + ?Sized>( fn get_type<G: CodeGenerator + ?Sized>(
&self, &self,
_generator: &G, _generator: &G,
_ctx: &'ctx Context, _ctx: &'ctx Context,

View File

@ -11,8 +11,8 @@ use crate::codegen::{CodeGenContext, CodeGenerator};
use super::*; use super::*;
/// Trait for Rust structs identifying length values for [`Array`]. /// Trait for Rust structs identifying length values for [`Array`].
pub trait ArrayLen: fmt::Debug + Clone + Copy { pub trait LenKind: fmt::Debug + Clone + Copy {
fn length(&self) -> u32; fn get_length(&self) -> u32;
} }
/// A statically known length. /// A statically known length.
@ -23,14 +23,14 @@ pub struct Len<const N: u32>;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct AnyLen(pub u32); pub struct AnyLen(pub u32);
impl<const N: u32> ArrayLen for Len<N> { impl<const N: u32> LenKind for Len<N> {
fn length(&self) -> u32 { fn get_length(&self) -> u32 {
N N
} }
} }
impl ArrayLen for AnyLen { impl LenKind for AnyLen {
fn length(&self) -> u32 { fn get_length(&self) -> u32 {
self.0 self.0
} }
} }
@ -46,16 +46,12 @@ pub struct Array<Len, Item> {
pub item: Item, pub item: Item,
} }
impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Model<'ctx> for Array<Len, Item> { impl<'ctx, Len: LenKind, Item: Model<'ctx>> Model<'ctx> for Array<Len, Item> {
type Value = ArrayValue<'ctx>; type Value = ArrayValue<'ctx>;
type Type = ArrayType<'ctx>; type Type = ArrayType<'ctx>;
fn llvm_type<G: CodeGenerator + ?Sized>( fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
&self, self.item.get_type(generator, ctx).array_type(self.len.get_length())
generator: &G,
ctx: &'ctx Context,
) -> Self::Type {
self.item.llvm_type(generator, ctx).array_type(self.len.length())
} }
fn check_type<T: BasicType<'ctx>, G: CodeGenerator + ?Sized>( fn check_type<T: BasicType<'ctx>, G: CodeGenerator + ?Sized>(
@ -69,11 +65,11 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Model<'ctx> for Array<Len, Item> {
return Err(ModelError(format!("Expecting ArrayType, but got {ty:?}"))); return Err(ModelError(format!("Expecting ArrayType, but got {ty:?}")));
}; };
if ty.len() != self.len.length() { if ty.len() != self.len.get_length() {
return Err(ModelError(format!( return Err(ModelError(format!(
"Expecting ArrayType with size {}, but got an ArrayType with size {}", "Expecting ArrayType with size {}, but got an ArrayType with size {}",
ty.len(), ty.len(),
self.len.length() self.len.get_length()
))); )));
} }
@ -85,7 +81,7 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Model<'ctx> for Array<Len, Item> {
} }
} }
impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Instance<'ctx, Ptr<Array<Len, Item>>> { impl<'ctx, Len: LenKind, Item: Model<'ctx>> Instance<'ctx, Ptr<Array<Len, Item>>> {
/// Get the pointer to the `i`-th (0-based) array element. /// Get the pointer to the `i`-th (0-based) array element.
pub fn gep( pub fn gep(
&self, &self,
@ -95,15 +91,15 @@ impl<'ctx, Len: ArrayLen, Item: Model<'ctx>> Instance<'ctx, Ptr<Array<Len, Item>
let zero = ctx.ctx.i32_type().const_zero(); let zero = ctx.ctx.i32_type().const_zero();
let ptr = unsafe { ctx.builder.build_in_bounds_gep(self.value, &[zero, i], "").unwrap() }; let ptr = unsafe { ctx.builder.build_in_bounds_gep(self.value, &[zero, i], "").unwrap() };
unsafe { Ptr(self.model.0.item).believe_value(ptr) } Ptr(self.model.0.item).believe_value(ptr)
} }
/// Like `gep` but `i` is a constant. /// Like `gep` but `i` is a constant.
pub fn gep_const(&self, ctx: &CodeGenContext<'ctx, '_>, i: u64) -> Instance<'ctx, Ptr<Item>> { pub fn gep_const(&self, ctx: &CodeGenContext<'ctx, '_>, i: u64) -> Instance<'ctx, Ptr<Item>> {
assert!( assert!(
i < u64::from(self.model.0.len.length()), i < u64::from(self.model.0.len.get_length()),
"Index {i} is out of bounds. Array length = {}", "Index {i} is out of bounds. Array length = {}",
self.model.0.len.length() self.model.0.len.get_length()
); );
let i = ctx.ctx.i32_type().const_int(i, false); let i = ctx.ctx.i32_type().const_int(i, false);

View File

@ -11,7 +11,7 @@ use crate::codegen::{CodeGenContext, CodeGenerator};
pub struct ModelError(pub String); pub struct ModelError(pub String);
impl ModelError { impl ModelError {
/// Append a context message to the error. // Append a context message to the error.
pub(super) fn under_context(mut self, context: &str) -> Self { pub(super) fn under_context(mut self, context: &str) -> Self {
self.0.push_str(" ... in "); self.0.push_str(" ... in ");
self.0.push_str(context); self.0.push_str(context);
@ -47,7 +47,7 @@ impl ModelError {
/// } /// }
/// ``` /// ```
/// ///
/// ### Notes on converting between Inkwell and model/ge. /// ### Notes on converting between Inkwell and model.
/// ///
/// Suppose you have an [`IntValue`], and you want to pass it into a function that takes a [`Instance<'ctx, Int<Int32>>`]. You can do use /// Suppose you have an [`IntValue`], and you want to pass it into a function that takes a [`Instance<'ctx, Int<Int32>>`]. You can do use
/// [`Model::check_value`] or [`Model::believe_value`]. /// [`Model::check_value`] or [`Model::believe_value`].
@ -68,16 +68,15 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
/// Return the [`BasicType`] of this model. /// Return the [`BasicType`] of this model.
#[must_use] #[must_use]
fn llvm_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type;
-> Self::Type;
/// Get the number of bytes of the [`BasicType`] of this model. /// Get the number of bytes of the [`BasicType`] of this model.
fn size_of<G: CodeGenerator + ?Sized>( fn sizeof<G: CodeGenerator + ?Sized>(
&self, &self,
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> IntValue<'ctx> { ) -> IntValue<'ctx> {
self.llvm_type(generator, ctx).size_of().unwrap() self.get_type(generator, ctx).size_of().unwrap()
} }
/// Check if a [`BasicType`] matches the [`BasicType`] of this model. /// Check if a [`BasicType`] matches the [`BasicType`] of this model.
@ -90,11 +89,9 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
/// Create an instance from a value. /// Create an instance from a value.
/// ///
/// # Safety
///
/// Caller must make sure the type of `value` and the type of this `model` are equivalent. /// Caller must make sure the type of `value` and the type of this `model` are equivalent.
#[must_use] #[must_use]
unsafe fn believe_value(&self, value: Self::Value) -> Instance<'ctx, Self> { fn believe_value(&self, value: Self::Value) -> Instance<'ctx, Self> {
Instance { model: *self, value } Instance { model: *self, value }
} }
@ -113,7 +110,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
let Ok(value) = Self::Value::try_from(value) else { let Ok(value) = Self::Value::try_from(value) else {
unreachable!("check_type() has bad implementation") unreachable!("check_type() has bad implementation")
}; };
unsafe { Ok(self.believe_value(value)) } Ok(self.believe_value(value))
} }
// Allocate a value on the stack and return its pointer. // Allocate a value on the stack and return its pointer.
@ -122,8 +119,8 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
generator: &mut G, generator: &mut G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
) -> Instance<'ctx, Ptr<Self>> { ) -> Instance<'ctx, Ptr<Self>> {
let p = ctx.builder.build_alloca(self.llvm_type(generator, ctx.ctx), "").unwrap(); let p = ctx.builder.build_alloca(self.get_type(generator, ctx.ctx), "").unwrap();
unsafe { Ptr(*self).believe_value(p) } Ptr(*self).believe_value(p)
} }
// Allocate an array on the stack and return its pointer. // Allocate an array on the stack and return its pointer.
@ -133,9 +130,8 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
len: IntValue<'ctx>, len: IntValue<'ctx>,
) -> Instance<'ctx, Ptr<Self>> { ) -> Instance<'ctx, Ptr<Self>> {
let p = let p = ctx.builder.build_array_alloca(self.get_type(generator, ctx.ctx), len, "").unwrap();
ctx.builder.build_array_alloca(self.llvm_type(generator, ctx.ctx), len, "").unwrap(); Ptr(*self).believe_value(p)
unsafe { Ptr(*self).believe_value(p) }
} }
fn var_alloca<G: CodeGenerator + ?Sized>( fn var_alloca<G: CodeGenerator + ?Sized>(
@ -144,9 +140,9 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
ctx: &mut CodeGenContext<'ctx, '_>, ctx: &mut CodeGenContext<'ctx, '_>,
name: Option<&str>, name: Option<&str>,
) -> Result<Instance<'ctx, Ptr<Self>>, String> { ) -> Result<Instance<'ctx, Ptr<Self>>, String> {
let ty = self.llvm_type(generator, ctx.ctx).as_basic_type_enum(); let ty = self.get_type(generator, ctx.ctx).as_basic_type_enum();
let p = generator.gen_var_alloc(ctx, ty, name)?; let p = generator.gen_var_alloc(ctx, ty, name)?;
unsafe { Ok(Ptr(*self).believe_value(p)) } Ok(Ptr(*self).believe_value(p))
} }
fn array_var_alloca<G: CodeGenerator + ?Sized>( fn array_var_alloca<G: CodeGenerator + ?Sized>(
@ -157,9 +153,9 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
name: Option<&'ctx str>, name: Option<&'ctx str>,
) -> Result<Instance<'ctx, Ptr<Self>>, String> { ) -> Result<Instance<'ctx, Ptr<Self>>, String> {
// TODO: Remove ArraySliceValue // TODO: Remove ArraySliceValue
let ty = self.llvm_type(generator, ctx.ctx).as_basic_type_enum(); let ty = self.get_type(generator, ctx.ctx).as_basic_type_enum();
let p = generator.gen_array_var_alloc(ctx, ty, len, name)?; let p = generator.gen_array_var_alloc(ctx, ty, len, name)?;
unsafe { Ok(Ptr(*self).believe_value(PointerValue::from(p))) } Ok(Ptr(*self).believe_value(PointerValue::from(p)))
} }
/// Allocate a constant array. /// Allocate a constant array.
@ -180,7 +176,7 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
}; };
} }
let value = match self.llvm_type(generator, ctx).as_basic_type_enum() { let value = match self.get_type(generator, ctx).as_basic_type_enum() {
BasicTypeEnum::ArrayType(t) => make!(t, BasicValueEnum::into_array_value), BasicTypeEnum::ArrayType(t) => make!(t, BasicValueEnum::into_array_value),
BasicTypeEnum::IntType(t) => make!(t, BasicValueEnum::into_int_value), BasicTypeEnum::IntType(t) => make!(t, BasicValueEnum::into_int_value),
BasicTypeEnum::FloatType(t) => make!(t, BasicValueEnum::into_float_value), BasicTypeEnum::FloatType(t) => make!(t, BasicValueEnum::into_float_value),
@ -199,7 +195,6 @@ pub trait Model<'ctx>: fmt::Debug + Clone + Copy {
pub struct Instance<'ctx, M: Model<'ctx>> { pub struct Instance<'ctx, M: Model<'ctx>> {
/// The model of this instance. /// The model of this instance.
pub model: M, pub model: M,
/// The value of this instance. /// The value of this instance.
/// ///
/// It is guaranteed the [`BasicType`] of `value` is consistent with that of `model`. /// It is guaranteed the [`BasicType`] of `value` is consistent with that of `model`.

View File

@ -63,11 +63,7 @@ impl<'ctx, N: FloatKind<'ctx>> Model<'ctx> for Float<N> {
type Value = FloatValue<'ctx>; type Value = FloatValue<'ctx>;
type Type = FloatType<'ctx>; type Type = FloatType<'ctx>;
fn llvm_type<G: CodeGenerator + ?Sized>( fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
&self,
generator: &G,
ctx: &'ctx Context,
) -> Self::Type {
self.0.get_float_type(generator, ctx) self.0.get_float_type(generator, ctx)
} }

View File

@ -35,7 +35,7 @@ struct Arg<'ctx> {
/// If `my_function_name` has not been declared in `ctx.module`, once `.returning()` is called, a function /// If `my_function_name` has not been declared in `ctx.module`, once `.returning()` is called, a function
/// declaration of `my_function_name` is added to `ctx.module`, where the [`FunctionType`] is deduced from /// declaration of `my_function_name` is added to `ctx.module`, where the [`FunctionType`] is deduced from
/// the argument types and returning type. /// the argument types and returning type.
pub struct FnCall<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> { pub struct CallFunction<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> {
generator: &'d mut G, generator: &'d mut G,
ctx: &'b CodeGenContext<'ctx, 'a>, ctx: &'b CodeGenContext<'ctx, 'a>,
/// Function name /// Function name
@ -46,9 +46,9 @@ pub struct FnCall<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> {
attrs: Vec<&'static str>, attrs: Vec<&'static str>,
} }
impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> FnCall<'ctx, 'a, 'b, 'c, 'd, G> { impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> CallFunction<'ctx, 'a, 'b, 'c, 'd, G> {
pub fn builder(generator: &'d mut G, ctx: &'b CodeGenContext<'ctx, 'a>, name: &'c str) -> Self { pub fn begin(generator: &'d mut G, ctx: &'b CodeGenContext<'ctx, 'a>, name: &'c str) -> Self {
FnCall { generator, ctx, name, args: Vec::new(), attrs: Vec::new() } CallFunction { generator, ctx, name, args: Vec::new(), attrs: Vec::new() }
} }
/// Push a list of LLVM function attributes to the function declaration. /// Push a list of LLVM function attributes to the function declaration.
@ -63,7 +63,7 @@ impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> FnCall<'ctx, 'a, 'b, 'c, '
#[must_use] #[must_use]
pub fn arg<M: Model<'ctx>>(mut self, arg: Instance<'ctx, M>) -> Self { pub fn arg<M: Model<'ctx>>(mut self, arg: Instance<'ctx, M>) -> Self {
let arg = Arg { let arg = Arg {
ty: arg.model.llvm_type(self.generator, self.ctx.ctx).as_basic_type_enum().into(), ty: arg.model.get_type(self.generator, self.ctx.ctx).as_basic_type_enum().into(),
val: arg.value.as_basic_value_enum().into(), val: arg.value.as_basic_value_enum().into(),
}; };
self.args.push(arg); self.args.push(arg);
@ -73,7 +73,7 @@ impl<'ctx, 'a, 'b, 'c, 'd, G: CodeGenerator + ?Sized> FnCall<'ctx, 'a, 'b, 'c, '
/// Call the function and expect the function to return a value of type of `return_model`. /// Call the function and expect the function to return a value of type of `return_model`.
#[must_use] #[must_use]
pub fn returning<M: Model<'ctx>>(self, name: &str, return_model: M) -> Instance<'ctx, M> { pub fn returning<M: Model<'ctx>>(self, name: &str, return_model: M) -> Instance<'ctx, M> {
let ret_ty = return_model.llvm_type(self.generator, self.ctx.ctx); let ret_ty = return_model.get_type(self.generator, self.ctx.ctx);
let ret = self.call(|tys| ret_ty.fn_type(tys, false), name); let ret = self.call(|tys| ret_ty.fn_type(tys, false), name);
let ret = BasicValueEnum::try_from(ret.as_any_value_enum()).unwrap(); // Must work let ret = BasicValueEnum::try_from(ret.as_any_value_enum()).unwrap(); // Must work

View File

@ -100,11 +100,7 @@ impl<'ctx, N: IntKind<'ctx>> Model<'ctx> for Int<N> {
type Value = IntValue<'ctx>; type Value = IntValue<'ctx>;
type Type = IntType<'ctx>; type Type = IntType<'ctx>;
fn llvm_type<G: CodeGenerator + ?Sized>( fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
&self,
generator: &G,
ctx: &'ctx Context,
) -> Self::Type {
self.0.get_int_type(generator, ctx) self.0.get_int_type(generator, ctx)
} }
@ -138,10 +134,9 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
value: u64, value: u64,
sign_extend: bool,
) -> Instance<'ctx, Self> { ) -> Instance<'ctx, Self> {
let value = self.llvm_type(generator, ctx).const_int(value, sign_extend); let value = self.get_type(generator, ctx).const_int(value, false);
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn const_0<G: CodeGenerator + ?Sized>( pub fn const_0<G: CodeGenerator + ?Sized>(
@ -149,8 +144,8 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> Instance<'ctx, Self> { ) -> Instance<'ctx, Self> {
let value = self.llvm_type(generator, ctx).const_zero(); let value = self.get_type(generator, ctx).const_zero();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn const_1<G: CodeGenerator + ?Sized>( pub fn const_1<G: CodeGenerator + ?Sized>(
@ -158,7 +153,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> Instance<'ctx, Self> { ) -> Instance<'ctx, Self> {
self.const_int(generator, ctx, 1, false) self.const_int(generator, ctx, 1)
} }
pub fn const_all_ones<G: CodeGenerator + ?Sized>( pub fn const_all_ones<G: CodeGenerator + ?Sized>(
@ -166,8 +161,8 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> Instance<'ctx, Self> { ) -> Instance<'ctx, Self> {
let value = self.llvm_type(generator, ctx).const_all_ones(); let value = self.get_type(generator, ctx).const_all_ones();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn s_extend_or_bit_cast<G: CodeGenerator + ?Sized>( pub fn s_extend_or_bit_cast<G: CodeGenerator + ?Sized>(
@ -182,9 +177,9 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
); );
let value = ctx let value = ctx
.builder .builder
.build_int_s_extend_or_bit_cast(value, self.llvm_type(generator, ctx.ctx), "") .build_int_s_extend_or_bit_cast(value, self.get_type(generator, ctx.ctx), "")
.unwrap(); .unwrap();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn s_extend<G: CodeGenerator + ?Sized>( pub fn s_extend<G: CodeGenerator + ?Sized>(
@ -198,8 +193,8 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
< self.0.get_int_type(generator, ctx.ctx).get_bit_width() < self.0.get_int_type(generator, ctx.ctx).get_bit_width()
); );
let value = let value =
ctx.builder.build_int_s_extend(value, self.llvm_type(generator, ctx.ctx), "").unwrap(); ctx.builder.build_int_s_extend(value, self.get_type(generator, ctx.ctx), "").unwrap();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn z_extend_or_bit_cast<G: CodeGenerator + ?Sized>( pub fn z_extend_or_bit_cast<G: CodeGenerator + ?Sized>(
@ -214,9 +209,9 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
); );
let value = ctx let value = ctx
.builder .builder
.build_int_z_extend_or_bit_cast(value, self.llvm_type(generator, ctx.ctx), "") .build_int_z_extend_or_bit_cast(value, self.get_type(generator, ctx.ctx), "")
.unwrap(); .unwrap();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn z_extend<G: CodeGenerator + ?Sized>( pub fn z_extend<G: CodeGenerator + ?Sized>(
@ -230,8 +225,8 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
< self.0.get_int_type(generator, ctx.ctx).get_bit_width() < self.0.get_int_type(generator, ctx.ctx).get_bit_width()
); );
let value = let value =
ctx.builder.build_int_z_extend(value, self.llvm_type(generator, ctx.ctx), "").unwrap(); ctx.builder.build_int_z_extend(value, self.get_type(generator, ctx.ctx), "").unwrap();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn truncate_or_bit_cast<G: CodeGenerator + ?Sized>( pub fn truncate_or_bit_cast<G: CodeGenerator + ?Sized>(
@ -246,9 +241,9 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
); );
let value = ctx let value = ctx
.builder .builder
.build_int_truncate_or_bit_cast(value, self.llvm_type(generator, ctx.ctx), "") .build_int_truncate_or_bit_cast(value, self.get_type(generator, ctx.ctx), "")
.unwrap(); .unwrap();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
pub fn truncate<G: CodeGenerator + ?Sized>( pub fn truncate<G: CodeGenerator + ?Sized>(
@ -262,8 +257,8 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
> self.0.get_int_type(generator, ctx.ctx).get_bit_width() > self.0.get_int_type(generator, ctx.ctx).get_bit_width()
); );
let value = let value =
ctx.builder.build_int_truncate(value, self.llvm_type(generator, ctx.ctx), "").unwrap(); ctx.builder.build_int_truncate(value, self.get_type(generator, ctx.ctx), "").unwrap();
unsafe { self.believe_value(value) } self.believe_value(value)
} }
/// `sext` or `trunc` an int to this model's int type. Does nothing if equal bit-widths. /// `sext` or `trunc` an int to this model's int type. Does nothing if equal bit-widths.
@ -277,7 +272,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
let our_width = self.0.get_int_type(generator, ctx.ctx).get_bit_width(); let our_width = self.0.get_int_type(generator, ctx.ctx).get_bit_width();
match their_width.cmp(&our_width) { match their_width.cmp(&our_width) {
Ordering::Less => self.s_extend(generator, ctx, value), Ordering::Less => self.s_extend(generator, ctx, value),
Ordering::Equal => unsafe { self.believe_value(value) }, Ordering::Equal => self.believe_value(value),
Ordering::Greater => self.truncate(generator, ctx, value), Ordering::Greater => self.truncate(generator, ctx, value),
} }
} }
@ -293,7 +288,7 @@ impl<'ctx, N: IntKind<'ctx>> Int<N> {
let our_width = self.0.get_int_type(generator, ctx.ctx).get_bit_width(); let our_width = self.0.get_int_type(generator, ctx.ctx).get_bit_width();
match their_width.cmp(&our_width) { match their_width.cmp(&our_width) {
Ordering::Less => self.z_extend(generator, ctx, value), Ordering::Less => self.z_extend(generator, ctx, value),
Ordering::Equal => unsafe { self.believe_value(value) }, Ordering::Equal => self.believe_value(value),
Ordering::Greater => self.truncate(generator, ctx, value), Ordering::Greater => self.truncate(generator, ctx, value),
} }
} }
@ -306,7 +301,7 @@ impl Int<Bool> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> Instance<'ctx, Self> { ) -> Instance<'ctx, Self> {
self.const_int(generator, ctx, 0, false) self.const_int(generator, ctx, 0)
} }
#[must_use] #[must_use]
@ -315,7 +310,7 @@ impl Int<Bool> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> Instance<'ctx, Self> { ) -> Instance<'ctx, Self> {
self.const_int(generator, ctx, 1, false) self.const_int(generator, ctx, 1)
} }
} }
@ -395,19 +390,19 @@ impl<'ctx, N: IntKind<'ctx>> Instance<'ctx, Int<N>> {
#[must_use] #[must_use]
pub fn add(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self { pub fn add(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self {
let value = ctx.builder.build_int_add(self.value, other.value, "").unwrap(); let value = ctx.builder.build_int_add(self.value, other.value, "").unwrap();
unsafe { self.model.believe_value(value) } self.model.believe_value(value)
} }
#[must_use] #[must_use]
pub fn sub(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self { pub fn sub(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self {
let value = ctx.builder.build_int_sub(self.value, other.value, "").unwrap(); let value = ctx.builder.build_int_sub(self.value, other.value, "").unwrap();
unsafe { self.model.believe_value(value) } self.model.believe_value(value)
} }
#[must_use] #[must_use]
pub fn mul(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self { pub fn mul(&self, ctx: &CodeGenContext<'ctx, '_>, other: Self) -> Self {
let value = ctx.builder.build_int_mul(self.value, other.value, "").unwrap(); let value = ctx.builder.build_int_mul(self.value, other.value, "").unwrap();
unsafe { self.model.believe_value(value) } self.model.believe_value(value)
} }
pub fn compare( pub fn compare(
@ -417,6 +412,6 @@ impl<'ctx, N: IntKind<'ctx>> Instance<'ctx, Int<N>> {
other: Self, other: Self,
) -> Instance<'ctx, Int<Bool>> { ) -> Instance<'ctx, Int<Bool>> {
let value = ctx.builder.build_int_compare(op, self.value, other.value, "").unwrap(); let value = ctx.builder.build_int_compare(op, self.value, other.value, "").unwrap();
unsafe { Int(Bool).believe_value(value) } Int(Bool).believe_value(value)
} }
} }

View File

@ -31,13 +31,9 @@ impl<'ctx, Item: Model<'ctx>> Model<'ctx> for Ptr<Item> {
type Value = PointerValue<'ctx>; type Value = PointerValue<'ctx>;
type Type = PointerType<'ctx>; type Type = PointerType<'ctx>;
fn llvm_type<G: CodeGenerator + ?Sized>( fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
&self,
generator: &G,
ctx: &'ctx Context,
) -> Self::Type {
// TODO: LLVM 15: ctx.ptr_type(AddressSpace::default()) // TODO: LLVM 15: ctx.ptr_type(AddressSpace::default())
self.0.llvm_type(generator, ctx).ptr_type(AddressSpace::default()) self.0.get_type(generator, ctx).ptr_type(AddressSpace::default())
} }
fn check_type<T: BasicType<'ctx>, G: CodeGenerator + ?Sized>( fn check_type<T: BasicType<'ctx>, G: CodeGenerator + ?Sized>(
@ -75,8 +71,8 @@ impl<'ctx, Item: Model<'ctx>> Ptr<Item> {
generator: &mut G, generator: &mut G,
ctx: &'ctx Context, ctx: &'ctx Context,
) -> Instance<'ctx, Ptr<Item>> { ) -> Instance<'ctx, Ptr<Item>> {
let ptr = self.llvm_type(generator, ctx).const_null(); let ptr = self.get_type(generator, ctx).const_null();
unsafe { self.believe_value(ptr) } self.believe_value(ptr)
} }
/// Cast a pointer into this model with [`inkwell::builder::Builder::build_pointer_cast`] /// Cast a pointer into this model with [`inkwell::builder::Builder::build_pointer_cast`]
@ -91,9 +87,9 @@ impl<'ctx, Item: Model<'ctx>> Ptr<Item> {
// ``` // ```
// return self.believe_value(ptr); // return self.believe_value(ptr);
// ``` // ```
let t = self.llvm_type(generator, ctx.ctx); let t = self.get_type(generator, ctx.ctx);
let ptr = ctx.builder.build_pointer_cast(ptr, t, "").unwrap(); let ptr = ctx.builder.build_pointer_cast(ptr, t, "").unwrap();
unsafe { self.believe_value(ptr) } self.believe_value(ptr)
} }
} }
@ -106,7 +102,7 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr<Item>> {
offset: IntValue<'ctx>, offset: IntValue<'ctx>,
) -> Instance<'ctx, Ptr<Item>> { ) -> Instance<'ctx, Ptr<Item>> {
let p = unsafe { ctx.builder.build_in_bounds_gep(self.value, &[offset], "").unwrap() }; let p = unsafe { ctx.builder.build_in_bounds_gep(self.value, &[offset], "").unwrap() };
unsafe { self.model.believe_value(p) } self.model.believe_value(p)
} }
/// Offset the pointer by [`inkwell::builder::Builder::build_in_bounds_gep`] by a constant offset. /// Offset the pointer by [`inkwell::builder::Builder::build_in_bounds_gep`] by a constant offset.
@ -114,9 +110,9 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr<Item>> {
pub fn offset_const( pub fn offset_const(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
offset: i64, offset: u64,
) -> Instance<'ctx, Ptr<Item>> { ) -> Instance<'ctx, Ptr<Item>> {
let offset = ctx.ctx.i32_type().const_int(offset as u64, true); let offset = ctx.ctx.i32_type().const_int(offset, false);
self.offset(ctx, offset) self.offset(ctx, offset)
} }
@ -132,7 +128,7 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr<Item>> {
pub fn set_index_const( pub fn set_index_const(
&self, &self,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
index: i64, index: u64,
value: Instance<'ctx, Item>, value: Instance<'ctx, Item>,
) { ) {
self.offset_const(ctx, index).store(ctx, value); self.offset_const(ctx, index).store(ctx, value);
@ -151,7 +147,7 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr<Item>> {
&self, &self,
generator: &mut G, generator: &mut G,
ctx: &CodeGenContext<'ctx, '_>, ctx: &CodeGenContext<'ctx, '_>,
index: i64, index: u64,
) -> Instance<'ctx, Item> { ) -> Instance<'ctx, Item> {
self.offset_const(ctx, index).load(generator, ctx) self.offset_const(ctx, index).load(generator, ctx)
} }
@ -185,13 +181,13 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr<Item>> {
/// Check if the pointer is null with [`inkwell::builder::Builder::build_is_null`]. /// Check if the pointer is null with [`inkwell::builder::Builder::build_is_null`].
pub fn is_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int<Bool>> { pub fn is_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int<Bool>> {
let value = ctx.builder.build_is_null(self.value, "").unwrap(); let value = ctx.builder.build_is_null(self.value, "").unwrap();
unsafe { Int(Bool).believe_value(value) } Int(Bool).believe_value(value)
} }
/// Check if the pointer is not null with [`inkwell::builder::Builder::build_is_not_null`]. /// Check if the pointer is not null with [`inkwell::builder::Builder::build_is_not_null`].
pub fn is_not_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int<Bool>> { pub fn is_not_null(&self, ctx: &CodeGenContext<'ctx, '_>) -> Instance<'ctx, Int<Bool>> {
let value = ctx.builder.build_is_not_null(self.value, "").unwrap(); let value = ctx.builder.build_is_not_null(self.value, "").unwrap();
unsafe { Int(Bool).believe_value(value) } Int(Bool).believe_value(value)
} }
/// `memcpy` from another pointer. /// `memcpy` from another pointer.
@ -203,9 +199,9 @@ impl<'ctx, Item: Model<'ctx>> Instance<'ctx, Ptr<Item>> {
num_items: IntValue<'ctx>, num_items: IntValue<'ctx>,
) { ) {
// Force extend `num_items` and `itemsize` to `i64` so their types would match. // Force extend `num_items` and `itemsize` to `i64` so their types would match.
let itemsize = self.model.size_of(generator, ctx.ctx); let itemsize = self.model.sizeof(generator, ctx.ctx);
let itemsize = Int(SizeT).z_extend_or_truncate(generator, ctx, itemsize); let itemsize = Int(Int64).z_extend_or_truncate(generator, ctx, itemsize);
let num_items = Int(SizeT).z_extend_or_truncate(generator, ctx, num_items); let num_items = Int(Int64).z_extend_or_truncate(generator, ctx, num_items);
let totalsize = itemsize.mul(ctx, num_items); let totalsize = itemsize.mul(ctx, num_items);
let is_volatile = ctx.ctx.bool_type().const_zero(); // is_volatile = false let is_volatile = ctx.ctx.bool_type().const_zero(); // is_volatile = false

View File

@ -13,16 +13,16 @@ use super::*;
/// A traveral that traverses a Rust `struct` that is used to declare an LLVM's struct's field types. /// A traveral that traverses a Rust `struct` that is used to declare an LLVM's struct's field types.
pub trait FieldTraversal<'ctx> { pub trait FieldTraversal<'ctx> {
/// Output type of [`FieldTraversal::add`]. /// Output type of [`FieldTraversal::add`].
type Output<M>; type Out<M>;
/// Traverse through the type of a declared field and do something with it. /// Traverse through the type of a declared field and do something with it.
/// ///
/// * `name` - The cosmetic name of the LLVM field. Used for debugging. /// * `name` - The cosmetic name of the LLVM field. Used for debugging.
/// * `model` - The [`Model`] representing the LLVM type of this field. /// * `model` - The [`Model`] representing the LLVM type of this field.
fn add<M: Model<'ctx>>(&mut self, name: &'static str, model: M) -> Self::Output<M>; fn add<M: Model<'ctx>>(&mut self, name: &'static str, model: M) -> Self::Out<M>;
/// Like [`FieldTraversal::add`] but [`Model`] is automatically inferred from its [`Default`] trait. /// Like [`FieldTraversal::add`] but [`Model`] is automatically inferred from its [`Default`] trait.
fn add_auto<M: Model<'ctx> + Default>(&mut self, name: &'static str) -> Self::Output<M> { fn add_auto<M: Model<'ctx> + Default>(&mut self, name: &'static str) -> Self::Out<M> {
self.add(name, M::default()) self.add(name, M::default())
} }
} }
@ -31,7 +31,7 @@ pub trait FieldTraversal<'ctx> {
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct GepField<M> { pub struct GepField<M> {
/// The GEP index of this field. This is the index to use with `build_gep`. /// The GEP index of this field. This is the index to use with `build_gep`.
pub gep_index: u32, pub gep_index: u64,
/// The cosmetic name of this field. /// The cosmetic name of this field.
pub name: &'static str, pub name: &'static str,
/// The [`Model`] of this field's type. /// The [`Model`] of this field's type.
@ -41,16 +41,16 @@ pub struct GepField<M> {
/// A traversal to calculate the GEP index of fields. /// A traversal to calculate the GEP index of fields.
pub struct GepFieldTraversal { pub struct GepFieldTraversal {
/// The current GEP index. /// The current GEP index.
gep_index_counter: u32, gep_index_counter: u64,
} }
impl<'ctx> FieldTraversal<'ctx> for GepFieldTraversal { impl<'ctx> FieldTraversal<'ctx> for GepFieldTraversal {
type Output<M> = GepField<M>; type Out<M> = GepField<M>;
fn add<M: Model<'ctx>>(&mut self, name: &'static str, model: M) -> Self::Output<M> { fn add<M: Model<'ctx>>(&mut self, name: &'static str, model: M) -> Self::Out<M> {
let gep_index = self.gep_index_counter; let gep_index = self.gep_index_counter;
self.gep_index_counter += 1; self.gep_index_counter += 1;
Self::Output { gep_index, name, model } Self::Out { gep_index, name, model }
} }
} }
@ -65,10 +65,10 @@ struct TypeFieldTraversal<'ctx, 'a, G: CodeGenerator + ?Sized> {
} }
impl<'ctx, 'a, G: CodeGenerator + ?Sized> FieldTraversal<'ctx> for TypeFieldTraversal<'ctx, 'a, G> { impl<'ctx, 'a, G: CodeGenerator + ?Sized> FieldTraversal<'ctx> for TypeFieldTraversal<'ctx, 'a, G> {
type Output<M> = (); // Checking types return nothing. type Out<M> = (); // Checking types return nothing.
fn add<M: Model<'ctx>>(&mut self, _name: &'static str, model: M) -> Self::Output<M> { fn add<M: Model<'ctx>>(&mut self, _name: &'static str, model: M) -> Self::Out<M> {
let t = model.llvm_type(self.generator, self.ctx).as_basic_type_enum(); let t = model.get_type(self.generator, self.ctx).as_basic_type_enum();
self.field_types.push(t); self.field_types.push(t);
} }
} }
@ -89,9 +89,9 @@ struct CheckTypeFieldTraversal<'ctx, 'a, G: CodeGenerator + ?Sized> {
impl<'ctx, 'a, G: CodeGenerator + ?Sized> FieldTraversal<'ctx> impl<'ctx, 'a, G: CodeGenerator + ?Sized> FieldTraversal<'ctx>
for CheckTypeFieldTraversal<'ctx, 'a, G> for CheckTypeFieldTraversal<'ctx, 'a, G>
{ {
type Output<M> = (); // Checking types return nothing. type Out<M> = (); // Checking types return nothing.
fn add<M: Model<'ctx>>(&mut self, name: &'static str, model: M) -> Self::Output<M> { fn add<M: Model<'ctx>>(&mut self, name: &'static str, model: M) -> Self::Out<M> {
let gep_index = self.gep_index_counter; let gep_index = self.gep_index_counter;
self.gep_index_counter += 1; self.gep_index_counter += 1;
@ -100,8 +100,7 @@ impl<'ctx, 'a, G: CodeGenerator + ?Sized> FieldTraversal<'ctx>
self.errors self.errors
.push(err.under_context(format!("field #{gep_index} '{name}'").as_str())); .push(err.under_context(format!("field #{gep_index} '{name}'").as_str()));
} }
} } // Otherwise, it will be caught by Struct's `check_type`.
// Otherwise, it will be caught by Struct's `check_type`.
} }
} }
@ -193,13 +192,13 @@ pub trait StructKind<'ctx>: fmt::Debug + Clone + Copy {
/// Traverse through all fields of this [`StructKind`]. /// Traverse through all fields of this [`StructKind`].
/// ///
/// Only used internally in this module for implementing other components. /// Only used internally in this module for implementing other components.
fn iter_fields<F: FieldTraversal<'ctx>>(&self, traversal: &mut F) -> Self::Fields<F>; fn traverse_fields<F: FieldTraversal<'ctx>>(&self, traversal: &mut F) -> Self::Fields<F>;
/// Get a convenience structure to get a struct field's GEP index through its corresponding Rust field. /// Get a convenience structure to get a struct field's GEP index through its corresponding Rust field.
/// ///
/// Only used internally in this module for implementing other components. /// Only used internally in this module for implementing other components.
fn fields(&self) -> Self::Fields<GepFieldTraversal> { fn fields(&self) -> Self::Fields<GepFieldTraversal> {
self.iter_fields(&mut GepFieldTraversal { gep_index_counter: 0 }) self.traverse_fields(&mut GepFieldTraversal { gep_index_counter: 0 })
} }
/// Get the LLVM [`StructType`] of this [`StructKind`]. /// Get the LLVM [`StructType`] of this [`StructKind`].
@ -209,7 +208,7 @@ pub trait StructKind<'ctx>: fmt::Debug + Clone + Copy {
ctx: &'ctx Context, ctx: &'ctx Context,
) -> StructType<'ctx> { ) -> StructType<'ctx> {
let mut traversal = TypeFieldTraversal { generator, ctx, field_types: Vec::new() }; let mut traversal = TypeFieldTraversal { generator, ctx, field_types: Vec::new() };
self.iter_fields(&mut traversal); self.traverse_fields(&mut traversal);
ctx.struct_type(&traversal.field_types, false) ctx.struct_type(&traversal.field_types, false)
} }
@ -243,11 +242,7 @@ impl<'ctx, S: StructKind<'ctx>> Model<'ctx> for Struct<S> {
type Value = StructValue<'ctx>; type Value = StructValue<'ctx>;
type Type = StructType<'ctx>; type Type = StructType<'ctx>;
fn llvm_type<G: CodeGenerator + ?Sized>( fn get_type<G: CodeGenerator + ?Sized>(&self, generator: &G, ctx: &'ctx Context) -> Self::Type {
&self,
generator: &G,
ctx: &'ctx Context,
) -> Self::Type {
self.0.get_struct_type(generator, ctx) self.0.get_struct_type(generator, ctx)
} }
@ -270,7 +265,7 @@ impl<'ctx, S: StructKind<'ctx>> Model<'ctx> for Struct<S> {
errors: Vec::new(), errors: Vec::new(),
scrutinee: ty, scrutinee: ty,
}; };
self.0.iter_fields(&mut traversal); self.0.traverse_fields(&mut traversal);
// Check the number of fields. // Check the number of fields.
let exp_num_fields = traversal.gep_index_counter; let exp_num_fields = traversal.gep_index_counter;
@ -303,7 +298,7 @@ impl<'ctx, S: StructKind<'ctx>> Instance<'ctx, Struct<S>> {
GetField: FnOnce(S::Fields<GepFieldTraversal>) -> GepField<M>, GetField: FnOnce(S::Fields<GepFieldTraversal>) -> GepField<M>,
{ {
let field = get_field(self.model.0.fields()); let field = get_field(self.model.0.fields());
let val = self.value.get_field_at_index(field.gep_index).unwrap(); let val = self.value.get_field_at_index(field.gep_index as u32).unwrap();
field.model.check_value(generator, ctx, val).unwrap() field.model.check_value(generator, ctx, val).unwrap()
} }
} }
@ -326,13 +321,13 @@ impl<'ctx, S: StructKind<'ctx>> Instance<'ctx, Ptr<Struct<S>>> {
ctx.builder ctx.builder
.build_in_bounds_gep( .build_in_bounds_gep(
self.value, self.value,
&[llvm_i32.const_zero(), llvm_i32.const_int(u64::from(field.gep_index), false)], &[llvm_i32.const_zero(), llvm_i32.const_int(field.gep_index, false)],
field.name, field.name,
) )
.unwrap() .unwrap()
}; };
unsafe { Ptr(field.model).believe_value(ptr) } Ptr(field.model).believe_value(ptr)
} }
/// Convenience function equivalent to `.gep(...).load(...)`. /// Convenience function equivalent to `.gep(...).load(...)`.

View File

@ -12,7 +12,6 @@ use crate::{
call_ndarray_calc_size, call_ndarray_calc_size,
}, },
llvm_intrinsics::{self, call_memcpy_generic}, llvm_intrinsics::{self, call_memcpy_generic},
macros::codegen_unreachable,
stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback}, stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback},
CodeGenContext, CodeGenerator, CodeGenContext, CodeGenerator,
}, },
@ -260,7 +259,7 @@ fn ndarray_zero_value<'ctx, G: CodeGenerator + ?Sized>(
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) { } else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) {
ctx.gen_string(generator, "").into() ctx.gen_string(generator, "").into()
} else { } else {
codegen_unreachable!(ctx) unreachable!()
} }
} }
@ -288,7 +287,7 @@ fn ndarray_one_value<'ctx, G: CodeGenerator + ?Sized>(
} else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) { } else if ctx.unifier.unioned(elem_ty, ctx.primitives.str) {
ctx.gen_string(generator, "1").into() ctx.gen_string(generator, "1").into()
} else { } else {
codegen_unreachable!(ctx) unreachable!()
} }
} }
@ -356,7 +355,7 @@ fn call_ndarray_empty_impl<'ctx, G: CodeGenerator + ?Sized>(
create_ndarray_const_shape(generator, ctx, elem_ty, &[shape_int]) create_ndarray_const_shape(generator, ctx, elem_ty, &[shape_int])
} }
_ => codegen_unreachable!(ctx), _ => unreachable!(),
} }
} }
@ -627,7 +626,7 @@ fn call_ndarray_full_impl<'ctx, G: CodeGenerator + ?Sized>(
} else if fill_value.is_int_value() || fill_value.is_float_value() { } else if fill_value.is_int_value() || fill_value.is_float_value() {
fill_value fill_value
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
Ok(value) Ok(value)
@ -2021,7 +2020,7 @@ pub fn gen_ndarray_fill<'ctx>(
} else if value_arg.is_int_value() || value_arg.is_float_value() { } else if value_arg.is_int_value() || value_arg.is_float_value() {
value_arg value_arg
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
Ok(value) Ok(value)
@ -2130,8 +2129,7 @@ pub fn ndarray_transpose<'ctx, G: CodeGenerator + ?Sized>(
Ok(out.as_base_value().into()) Ok(out.as_base_value().into())
} else { } else {
codegen_unreachable!( unreachable!(
ctx,
"{FN_NAME}() not supported for '{}'", "{FN_NAME}() not supported for '{}'",
format!("'{}'", ctx.unifier.stringify(x1_ty)) format!("'{}'", ctx.unifier.stringify(x1_ty))
) )
@ -2373,7 +2371,7 @@ pub fn ndarray_reshape<'ctx, G: CodeGenerator + ?Sized>(
.into_int_value(); .into_int_value();
create_ndarray_const_shape(generator, ctx, elem_ty, &[shape_int]) create_ndarray_const_shape(generator, ctx, elem_ty, &[shape_int])
} }
_ => codegen_unreachable!(ctx), _ => unreachable!(),
} }
.unwrap(); .unwrap();
@ -2417,8 +2415,7 @@ pub fn ndarray_reshape<'ctx, G: CodeGenerator + ?Sized>(
Ok(out.as_base_value().into()) Ok(out.as_base_value().into())
} else { } else {
codegen_unreachable!( unreachable!(
ctx,
"{FN_NAME}() not supported for '{}'", "{FN_NAME}() not supported for '{}'",
format!("'{}'", ctx.unifier.stringify(x1_ty)) format!("'{}'", ctx.unifier.stringify(x1_ty))
) )
@ -2486,7 +2483,7 @@ pub fn ndarray_dot<'ctx, G: CodeGenerator + ?Sized>(
.build_float_mul(e1, elem2.into_float_value(), "") .build_float_mul(e1, elem2.into_float_value(), "")
.unwrap() .unwrap()
.as_basic_value_enum(), .as_basic_value_enum(),
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
let acc_val = ctx.builder.build_load(acc, "").unwrap(); let acc_val = ctx.builder.build_load(acc, "").unwrap();
let acc_val = match acc_val { let acc_val = match acc_val {
@ -2500,7 +2497,7 @@ pub fn ndarray_dot<'ctx, G: CodeGenerator + ?Sized>(
.build_float_add(e1, product.into_float_value(), "") .build_float_add(e1, product.into_float_value(), "")
.unwrap() .unwrap()
.as_basic_value_enum(), .as_basic_value_enum(),
_ => codegen_unreachable!(ctx), _ => unreachable!(),
}; };
ctx.builder.build_store(acc, acc_val).unwrap(); ctx.builder.build_store(acc, acc_val).unwrap();
@ -2517,8 +2514,7 @@ pub fn ndarray_dot<'ctx, G: CodeGenerator + ?Sized>(
(BasicValueEnum::FloatValue(e1), BasicValueEnum::FloatValue(e2)) => { (BasicValueEnum::FloatValue(e1), BasicValueEnum::FloatValue(e2)) => {
Ok(ctx.builder.build_float_mul(e1, e2, "").unwrap().as_basic_value_enum()) Ok(ctx.builder.build_float_mul(e1, e2, "").unwrap().as_basic_value_enum())
} }
_ => codegen_unreachable!( _ => unreachable!(
ctx,
"{FN_NAME}() not supported for '{}'", "{FN_NAME}() not supported for '{}'",
format!("'{}'", ctx.unifier.stringify(x1_ty)) format!("'{}'", ctx.unifier.stringify(x1_ty))
), ),

View File

@ -1,13 +1,15 @@
use super::{ use super::{
classes::{ArrayLikeIndexer, ArraySliceValue, ListValue, RangeValue}, super::symbol_resolver::ValueEnum,
expr::{destructure_range, gen_binop_expr}, expr::destructure_range,
gen_in_range_check,
irrt::{handle_slice_indices, list_slice_assignment}, irrt::{handle_slice_indices, list_slice_assignment},
macros::codegen_unreachable,
CodeGenContext, CodeGenerator, CodeGenContext, CodeGenerator,
}; };
use crate::{ use crate::{
symbol_resolver::ValueEnum, codegen::{
classes::{ArrayLikeIndexer, ArraySliceValue, ListValue, RangeValue},
expr::gen_binop_expr,
gen_in_range_check,
},
toplevel::{DefinitionId, TopLevelDef}, toplevel::{DefinitionId, TopLevelDef},
typecheck::{ typecheck::{
magic_methods::Binop, magic_methods::Binop,
@ -119,7 +121,7 @@ pub fn gen_store_target<'ctx, G: CodeGenerator>(
return Ok(None); return Ok(None);
}; };
let BasicValueEnum::PointerValue(ptr) = val else { let BasicValueEnum::PointerValue(ptr) = val else {
codegen_unreachable!(ctx); unreachable!();
}; };
unsafe { unsafe {
ctx.builder.build_in_bounds_gep( ctx.builder.build_in_bounds_gep(
@ -133,7 +135,7 @@ pub fn gen_store_target<'ctx, G: CodeGenerator>(
} }
.unwrap() .unwrap()
} }
_ => codegen_unreachable!(ctx), _ => unreachable!(),
})) }))
} }
@ -174,14 +176,6 @@ pub fn gen_assign<'ctx, G: CodeGenerator>(
} }
} }
let val = value.to_basic_value_enum(ctx, generator, target.custom.unwrap())?; let val = value.to_basic_value_enum(ctx, generator, target.custom.unwrap())?;
// Perform i1 <-> i8 conversion as needed
let val = if ctx.unifier.unioned(target.custom.unwrap(), ctx.primitives.bool) {
generator.bool_to_i8(ctx, val.into_int_value()).into()
} else {
val
};
ctx.builder.build_store(ptr, val).unwrap(); ctx.builder.build_store(ptr, val).unwrap();
} }
}; };
@ -199,12 +193,12 @@ pub fn gen_assign_target_list<'ctx, G: CodeGenerator>(
// Deconstruct the tuple `value` // Deconstruct the tuple `value`
let BasicValueEnum::StructValue(tuple) = value.to_basic_value_enum(ctx, generator, value_ty)? let BasicValueEnum::StructValue(tuple) = value.to_basic_value_enum(ctx, generator, value_ty)?
else { else {
codegen_unreachable!(ctx) unreachable!()
}; };
// NOTE: Currently, RHS's type is forced to be a Tuple by the type inferencer. // NOTE: Currently, RHS's type is forced to be a Tuple by the type inferencer.
let TypeEnum::TTuple { ty: tuple_tys, .. } = &*ctx.unifier.get_ty(value_ty) else { let TypeEnum::TTuple { ty: tuple_tys, .. } = &*ctx.unifier.get_ty(value_ty) else {
codegen_unreachable!(ctx); unreachable!();
}; };
assert_eq!(tuple.get_type().count_fields() as usize, tuple_tys.len()); assert_eq!(tuple.get_type().count_fields() as usize, tuple_tys.len());
@ -264,7 +258,7 @@ pub fn gen_assign_target_list<'ctx, G: CodeGenerator>(
// Now assign with that sub-tuple to the starred target. // Now assign with that sub-tuple to the starred target.
generator.gen_assign(ctx, target, ValueEnum::Dynamic(sub_tuple_val), sub_tuple_ty)?; generator.gen_assign(ctx, target, ValueEnum::Dynamic(sub_tuple_val), sub_tuple_ty)?;
} else { } else {
codegen_unreachable!(ctx) // The typechecker ensures this unreachable!() // The typechecker ensures this
} }
// Handle assignment after the starred target // Handle assignment after the starred target
@ -312,9 +306,7 @@ pub fn gen_setitem<'ctx, G: CodeGenerator>(
if let ExprKind::Slice { .. } = &key.node { if let ExprKind::Slice { .. } = &key.node {
// Handle assigning to a slice // Handle assigning to a slice
let ExprKind::Slice { lower, upper, step } = &key.node else { let ExprKind::Slice { lower, upper, step } = &key.node else { unreachable!() };
codegen_unreachable!(ctx)
};
let Some((start, end, step)) = handle_slice_indices( let Some((start, end, step)) = handle_slice_indices(
lower, lower,
upper, upper,
@ -424,9 +416,7 @@ pub fn gen_for<G: CodeGenerator>(
ctx: &mut CodeGenContext<'_, '_>, ctx: &mut CodeGenContext<'_, '_>,
stmt: &Stmt<Option<Type>>, stmt: &Stmt<Option<Type>>,
) -> Result<(), String> { ) -> Result<(), String> {
let StmtKind::For { iter, target, body, orelse, .. } = &stmt.node else { let StmtKind::For { iter, target, body, orelse, .. } = &stmt.node else { unreachable!() };
codegen_unreachable!(ctx)
};
// var_assignment static values may be changed in another branch // var_assignment static values may be changed in another branch
// if so, remove the static value as it may not be correct in this branch // if so, remove the static value as it may not be correct in this branch
@ -468,7 +458,7 @@ pub fn gen_for<G: CodeGenerator>(
let Some(target_i) = let Some(target_i) =
generator.gen_store_target(ctx, target, Some("for.target.addr"))? generator.gen_store_target(ctx, target, Some("for.target.addr"))?
else { else {
codegen_unreachable!(ctx) unreachable!()
}; };
let (start, stop, step) = destructure_range(ctx, iter_val); let (start, stop, step) = destructure_range(ctx, iter_val);
@ -911,7 +901,7 @@ pub fn gen_while<G: CodeGenerator>(
ctx: &mut CodeGenContext<'_, '_>, ctx: &mut CodeGenContext<'_, '_>,
stmt: &Stmt<Option<Type>>, stmt: &Stmt<Option<Type>>,
) -> Result<(), String> { ) -> Result<(), String> {
let StmtKind::While { test, body, orelse, .. } = &stmt.node else { codegen_unreachable!(ctx) }; let StmtKind::While { test, body, orelse, .. } = &stmt.node else { unreachable!() };
// var_assignment static values may be changed in another branch // var_assignment static values may be changed in another branch
// if so, remove the static value as it may not be correct in this branch // if so, remove the static value as it may not be correct in this branch
@ -941,7 +931,7 @@ pub fn gen_while<G: CodeGenerator>(
return Ok(()); return Ok(());
}; };
let BasicValueEnum::IntValue(test) = test else { codegen_unreachable!(ctx) }; let BasicValueEnum::IntValue(test) = test else { unreachable!() };
ctx.builder ctx.builder
.build_conditional_branch(generator.bool_to_i1(ctx, test), body_bb, orelse_bb) .build_conditional_branch(generator.bool_to_i1(ctx, test), body_bb, orelse_bb)
@ -1089,7 +1079,7 @@ pub fn gen_if<G: CodeGenerator>(
ctx: &mut CodeGenContext<'_, '_>, ctx: &mut CodeGenContext<'_, '_>,
stmt: &Stmt<Option<Type>>, stmt: &Stmt<Option<Type>>,
) -> Result<(), String> { ) -> Result<(), String> {
let StmtKind::If { test, body, orelse, .. } = &stmt.node else { codegen_unreachable!(ctx) }; let StmtKind::If { test, body, orelse, .. } = &stmt.node else { unreachable!() };
// var_assignment static values may be changed in another branch // var_assignment static values may be changed in another branch
// if so, remove the static value as it may not be correct in this branch // if so, remove the static value as it may not be correct in this branch
@ -1212,11 +1202,11 @@ pub fn exn_constructor<'ctx>(
let zelf_id = if let TypeEnum::TObj { obj_id, .. } = &*ctx.unifier.get_ty(zelf_ty) { let zelf_id = if let TypeEnum::TObj { obj_id, .. } = &*ctx.unifier.get_ty(zelf_ty) {
obj_id.0 obj_id.0
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let defs = ctx.top_level.definitions.read(); let defs = ctx.top_level.definitions.read();
let def = defs[zelf_id].read(); let def = defs[zelf_id].read();
let TopLevelDef::Class { name: zelf_name, .. } = &*def else { codegen_unreachable!(ctx) }; let TopLevelDef::Class { name: zelf_name, .. } = &*def else { unreachable!() };
let exception_name = format!("{}:{}", ctx.resolver.get_exception_id(zelf_id), zelf_name); let exception_name = format!("{}:{}", ctx.resolver.get_exception_id(zelf_id), zelf_name);
unsafe { unsafe {
let id_ptr = ctx.builder.build_in_bounds_gep(zelf, &[zero, zero], "exn.id").unwrap(); let id_ptr = ctx.builder.build_in_bounds_gep(zelf, &[zero, zero], "exn.id").unwrap();
@ -1324,7 +1314,7 @@ pub fn gen_try<'ctx, 'a, G: CodeGenerator>(
target: &Stmt<Option<Type>>, target: &Stmt<Option<Type>>,
) -> Result<(), String> { ) -> Result<(), String> {
let StmtKind::Try { body, handlers, orelse, finalbody, .. } = &target.node else { let StmtKind::Try { body, handlers, orelse, finalbody, .. } = &target.node else {
codegen_unreachable!(ctx) unreachable!()
}; };
// if we need to generate anything related to exception, we must have personality defined // if we need to generate anything related to exception, we must have personality defined
@ -1401,7 +1391,7 @@ pub fn gen_try<'ctx, 'a, G: CodeGenerator>(
if let TypeEnum::TObj { obj_id, .. } = &*ctx.unifier.get_ty(type_.custom.unwrap()) { if let TypeEnum::TObj { obj_id, .. } = &*ctx.unifier.get_ty(type_.custom.unwrap()) {
*obj_id *obj_id
} else { } else {
codegen_unreachable!(ctx) unreachable!()
}; };
let exception_name = format!("{}:{}", ctx.resolver.get_exception_id(obj_id.0), exn_name); let exception_name = format!("{}:{}", ctx.resolver.get_exception_id(obj_id.0), exn_name);
let exn_id = ctx.resolver.get_string_id(&exception_name); let exn_id = ctx.resolver.get_string_id(&exception_name);
@ -1673,23 +1663,6 @@ pub fn gen_return<G: CodeGenerator>(
} else { } else {
None None
}; };
// Remap boolean return type into i1
let value = value.map(|ret_val| {
// The "return type" of a sret function is in the first parameter
let expected_ty = if ctx.need_sret {
func.get_type().get_param_types()[0]
} else {
func.get_type().get_return_type().unwrap()
};
if matches!(expected_ty, BasicTypeEnum::IntType(ty) if ty.get_bit_width() == 1) {
generator.bool_to_i1(ctx, ret_val.into_int_value()).into()
} else {
ret_val
}
});
if let Some(return_target) = ctx.return_target { if let Some(return_target) = ctx.return_target {
if let Some(value) = value { if let Some(value) = value {
ctx.builder.build_store(ctx.return_buffer.unwrap(), value).unwrap(); ctx.builder.build_store(ctx.return_buffer.unwrap(), value).unwrap();
@ -1700,6 +1673,25 @@ pub fn gen_return<G: CodeGenerator>(
ctx.builder.build_store(ctx.return_buffer.unwrap(), value.unwrap()).unwrap(); ctx.builder.build_store(ctx.return_buffer.unwrap(), value.unwrap()).unwrap();
ctx.builder.build_return(None).unwrap(); ctx.builder.build_return(None).unwrap();
} else { } else {
// Remap boolean return type into i1
let value = value.map(|v| {
let expected_ty = func.get_type().get_return_type().unwrap();
let ret_val = v.as_basic_value_enum();
if expected_ty.is_int_type() && ret_val.is_int_value() {
let ret_type = expected_ty.into_int_type();
let ret_val = ret_val.into_int_value();
if ret_type.get_bit_width() == 1 && ret_val.get_type().get_bit_width() != 1 {
generator.bool_to_i1(ctx, ret_val)
} else {
ret_val
}
.into()
} else {
ret_val
}
});
let value = value.as_ref().map(|v| v as &dyn BasicValue); let value = value.as_ref().map(|v| v as &dyn BasicValue);
ctx.builder.build_return(value).unwrap(); ctx.builder.build_return(value).unwrap();
} }
@ -1768,30 +1760,7 @@ pub fn gen_stmt<G: CodeGenerator>(
StmtKind::Try { .. } => gen_try(generator, ctx, stmt)?, StmtKind::Try { .. } => gen_try(generator, ctx, stmt)?,
StmtKind::Raise { exc, .. } => { StmtKind::Raise { exc, .. } => {
if let Some(exc) = exc { if let Some(exc) = exc {
let exn = if let ExprKind::Name { id, .. } = &exc.node { let exc = if let Some(v) = generator.gen_expr(ctx, exc)? {
// Handle "raise Exception" short form
let def_id = ctx.resolver.get_identifier_def(*id).map_err(|e| {
format!("{} (at {})", e.iter().next().unwrap(), exc.location)
})?;
let def = ctx.top_level.definitions.read();
let TopLevelDef::Class { constructor, .. } = *def[def_id.0].read() else {
return Err(format!("Failed to resolve symbol {id} (at {})", exc.location));
};
let TypeEnum::TFunc(signature) =
ctx.unifier.get_ty(constructor.unwrap()).as_ref().clone()
else {
return Err(format!("Failed to resolve symbol {id} (at {})", exc.location));
};
generator
.gen_call(ctx, None, (&signature, def_id), Vec::default())?
.map(Into::into)
} else {
generator.gen_expr(ctx, exc)?
};
let exc = if let Some(v) = exn {
v.to_basic_value_enum(ctx, generator, exc.custom.unwrap())? v.to_basic_value_enum(ctx, generator, exc.custom.unwrap())?
} else { } else {
return Ok(()); return Ok(());

View File

@ -23,7 +23,7 @@ impl Default for ComposerConfig {
} }
} }
pub type DefAst = (Arc<RwLock<TopLevelDef>>, Option<Stmt<()>>); type DefAst = (Arc<RwLock<TopLevelDef>>, Option<Stmt<()>>);
pub struct TopLevelComposer { pub struct TopLevelComposer {
// list of top level definitions, same as top level context // list of top level definitions, same as top level context
pub definition_ast_list: Vec<DefAst>, pub definition_ast_list: Vec<DefAst>,
@ -1822,12 +1822,7 @@ impl TopLevelComposer {
if *name != init_str_id { if *name != init_str_id {
unreachable!("must be init function here") unreachable!("must be init function here")
} }
let all_inited = Self::get_all_assigned_field(body.as_slice())?;
let all_inited = Self::get_all_assigned_field(
object_id.0,
definition_ast_list,
body.as_slice(),
)?;
for (f, _, _) in fields { for (f, _, _) in fields {
if !all_inited.contains(f) { if !all_inited.contains(f) {
return Err(HashSet::from([ return Err(HashSet::from([

View File

@ -3,7 +3,6 @@ use std::convert::TryInto;
use crate::symbol_resolver::SymbolValue; use crate::symbol_resolver::SymbolValue;
use crate::toplevel::numpy::unpack_ndarray_var_tys; use crate::toplevel::numpy::unpack_ndarray_var_tys;
use crate::typecheck::typedef::{into_var_map, iter_type_vars, Mapping, TypeVarId, VarMap}; use crate::typecheck::typedef::{into_var_map, iter_type_vars, Mapping, TypeVarId, VarMap};
use ast::ExprKind;
use nac3parser::ast::{Constant, Location}; use nac3parser::ast::{Constant, Location};
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use strum_macros::EnumIter; use strum_macros::EnumIter;
@ -734,16 +733,7 @@ impl TopLevelComposer {
) )
} }
/// This function returns the fields that have been initialized in the `__init__` function of a class pub fn get_all_assigned_field(stmts: &[Stmt<()>]) -> Result<HashSet<StrRef>, HashSet<String>> {
/// The function takes as input:
/// * `class_id`: The `object_id` of the class whose function is being evaluated (check `TopLevelDef::Class`)
/// * `definition_ast_list`: A list of ast definitions and statements defined in `TopLevelComposer`
/// * `stmts`: The body of function being parsed. Each statment is analyzed to check varaible initialization statements
pub fn get_all_assigned_field(
class_id: usize,
definition_ast_list: &Vec<DefAst>,
stmts: &[Stmt<()>],
) -> Result<HashSet<StrRef>, HashSet<String>> {
let mut result = HashSet::new(); let mut result = HashSet::new();
for s in stmts { for s in stmts {
match &s.node { match &s.node {
@ -779,138 +769,30 @@ impl TopLevelComposer {
// TODO: do not check for For and While? // TODO: do not check for For and While?
ast::StmtKind::For { body, orelse, .. } ast::StmtKind::For { body, orelse, .. }
| ast::StmtKind::While { body, orelse, .. } => { | ast::StmtKind::While { body, orelse, .. } => {
result.extend(Self::get_all_assigned_field( result.extend(Self::get_all_assigned_field(body.as_slice())?);
class_id, result.extend(Self::get_all_assigned_field(orelse.as_slice())?);
definition_ast_list,
body.as_slice(),
)?);
result.extend(Self::get_all_assigned_field(
class_id,
definition_ast_list,
orelse.as_slice(),
)?);
} }
ast::StmtKind::If { body, orelse, .. } => { ast::StmtKind::If { body, orelse, .. } => {
let inited_for_sure = Self::get_all_assigned_field( let inited_for_sure = Self::get_all_assigned_field(body.as_slice())?
class_id, .intersection(&Self::get_all_assigned_field(orelse.as_slice())?)
definition_ast_list, .copied()
body.as_slice(), .collect::<HashSet<_>>();
)?
.intersection(&Self::get_all_assigned_field(
class_id,
definition_ast_list,
orelse.as_slice(),
)?)
.copied()
.collect::<HashSet<_>>();
result.extend(inited_for_sure); result.extend(inited_for_sure);
} }
ast::StmtKind::Try { body, orelse, finalbody, .. } => { ast::StmtKind::Try { body, orelse, finalbody, .. } => {
let inited_for_sure = Self::get_all_assigned_field( let inited_for_sure = Self::get_all_assigned_field(body.as_slice())?
class_id, .intersection(&Self::get_all_assigned_field(orelse.as_slice())?)
definition_ast_list, .copied()
body.as_slice(), .collect::<HashSet<_>>();
)?
.intersection(&Self::get_all_assigned_field(
class_id,
definition_ast_list,
orelse.as_slice(),
)?)
.copied()
.collect::<HashSet<_>>();
result.extend(inited_for_sure); result.extend(inited_for_sure);
result.extend(Self::get_all_assigned_field( result.extend(Self::get_all_assigned_field(finalbody.as_slice())?);
class_id,
definition_ast_list,
finalbody.as_slice(),
)?);
} }
ast::StmtKind::With { body, .. } => { ast::StmtKind::With { body, .. } => {
result.extend(Self::get_all_assigned_field( result.extend(Self::get_all_assigned_field(body.as_slice())?);
class_id,
definition_ast_list,
body.as_slice(),
)?);
}
// Variables Initialized in function calls
ast::StmtKind::Expr { value, .. } => {
let ExprKind::Call { func, .. } = &value.node else {
continue;
};
let ExprKind::Attribute { value, attr, .. } = &func.node else {
continue;
};
let ExprKind::Name { id, .. } = &value.node else {
continue;
};
// Need to consider the two cases:
// Case 1) Call to class function i.e. id = `self`
// Case 2) Call to class ancestor function i.e. id = ancestor_name
// We leave checking whether function in case 2 belonged to class ancestor or not to type checker
//
// According to current handling of `self`, function definition are fixed and do not change regardless
// of which object is passed as `self` i.e. virtual polymorphism is not supported
// Therefore, we change class id for case 2 to reflect behavior of our compiler
let class_name = if *id == "self".into() {
let ast::StmtKind::ClassDef { name, .. } =
&definition_ast_list[class_id].1.as_ref().unwrap().node
else {
unreachable!()
};
name
} else {
id
};
let parent_method = definition_ast_list.iter().find_map(|def| {
let (
class_def,
Some(ast::Located {
node: ast::StmtKind::ClassDef { name, body, .. },
..
}),
) = &def
else {
return None;
};
let TopLevelDef::Class { object_id: class_id, .. } = &*class_def.read()
else {
unreachable!()
};
if name == class_name {
body.iter().find_map(|m| {
let ast::StmtKind::FunctionDef { name, body, .. } = &m.node else {
return None;
};
if *name == *attr {
return Some((body.clone(), class_id.0));
}
None
})
} else {
None
}
});
// If method body is none then method does not exist
if let Some((method_body, class_id)) = parent_method {
result.extend(Self::get_all_assigned_field(
class_id,
definition_ast_list,
method_body.as_slice(),
)?);
} else {
return Err(HashSet::from([format!(
"{}.{} not found in class {class_name} at {}",
*id, *attr, value.location
)]));
}
} }
ast::StmtKind::Pass { .. } ast::StmtKind::Pass { .. }
| ast::StmtKind::Assert { .. } | ast::StmtKind::Assert { .. }
| ast::StmtKind::AnnAssign { .. } => {} | ast::StmtKind::Expr { .. } => {}
_ => { _ => {
unimplemented!() unimplemented!()

View File

@ -520,23 +520,6 @@ pub fn typeof_binop(
} }
Operator::MatMult => { Operator::MatMult => {
// NOTE: NumPy matmul's LHS and RHS must both be ndarrays. Scalars are not allowed.
match (&*unifier.get_ty(lhs), &*unifier.get_ty(rhs)) {
(
TypeEnum::TObj { obj_id: lhs_obj_id, .. },
TypeEnum::TObj { obj_id: rhs_obj_id, .. },
) if *lhs_obj_id == primitives.ndarray.obj_id(unifier).unwrap()
&& *rhs_obj_id == primitives.ndarray.obj_id(unifier).unwrap() =>
{
// LHS and RHS have valid types
}
_ => {
let lhs_str = unifier.stringify(lhs);
let rhs_str = unifier.stringify(rhs);
return Err(format!("ndarray.__matmul__ only accepts ndarray operands, but left operand has type {lhs_str}, and right operand has type {rhs_str}"));
}
}
let (_, lhs_ndims) = unpack_ndarray_var_tys(unifier, lhs); let (_, lhs_ndims) = unpack_ndarray_var_tys(unifier, lhs);
let lhs_ndims = match &*unifier.get_ty_immutable(lhs_ndims) { let lhs_ndims = match &*unifier.get_ty_immutable(lhs_ndims) {
TypeEnum::TLiteral { values, .. } => { TypeEnum::TLiteral { values, .. } => {

View File

@ -12,7 +12,6 @@ use super::{
RecordField, RecordKey, Type, TypeEnum, TypeVar, Unifier, VarMap, RecordField, RecordKey, Type, TypeEnum, TypeVar, Unifier, VarMap,
}, },
}; };
use crate::toplevel::type_annotation::TypeAnnotation;
use crate::{ use crate::{
symbol_resolver::{SymbolResolver, SymbolValue}, symbol_resolver::{SymbolResolver, SymbolValue},
toplevel::{ toplevel::{
@ -103,7 +102,6 @@ pub struct Inferencer<'a> {
} }
type InferenceError = HashSet<String>; type InferenceError = HashSet<String>;
type OverrideResult = Result<Option<ast::Expr<Option<Type>>>, InferenceError>;
struct NaiveFolder(); struct NaiveFolder();
impl Fold<()> for NaiveFolder { impl Fold<()> for NaiveFolder {
@ -1674,86 +1672,6 @@ impl<'a> Inferencer<'a> {
Ok(None) Ok(None)
} }
/// Checks whether a class method is calling parent function
/// Returns [`None`] if its not a call to parent method, otherwise
/// returns a new `func` with class name replaced by `self` and method resolved to its `DefinitionID`
///
/// e.g. A.f1(self, ...) returns Some(self.{DefintionID(f1)})
fn check_overriding(&mut self, func: &ast::Expr<()>, args: &[ast::Expr<()>]) -> OverrideResult {
// `self` must be first argument for call to parent method
if let Some(Located { node: ExprKind::Name { id, .. }, .. }) = &args.first() {
if *id != "self".into() {
return Ok(None);
}
} else {
return Ok(None);
}
let Located {
node: ExprKind::Attribute { value, attr: method_name, ctx }, location, ..
} = func
else {
return Ok(None);
};
let ExprKind::Name { id: class_name, ctx: class_ctx } = &value.node else {
return Ok(None);
};
let zelf = &self.fold_expr(args[0].clone())?;
// Check whether the method belongs to class ancestors
let def_id = self.unifier.get_ty(zelf.custom.unwrap());
let TypeEnum::TObj { obj_id, .. } = def_id.as_ref() else { unreachable!() };
let defs = self.top_level.definitions.read();
let res = {
if let TopLevelDef::Class { ancestors, .. } = &*defs[obj_id.0].read() {
let res = ancestors.iter().find_map(|f| {
let TypeAnnotation::CustomClass { id, .. } = f else { unreachable!() };
let TopLevelDef::Class { name, methods, .. } = &*defs[id.0].read() else {
unreachable!()
};
// Class names are stored as `__module__.class`
let name = name.to_string();
let (_, name) = name.rsplit_once('.').unwrap();
if name == class_name.to_string() {
return methods.iter().find_map(|f| {
if f.0 == *method_name {
return Some(*f);
}
None
});
}
None
});
res
} else {
None
}
};
match res {
Some(r) => {
let mut new_func = func.clone();
let mut new_value = value.clone();
new_value.node = ExprKind::Name { id: "self".into(), ctx: *class_ctx };
new_func.node =
ExprKind::Attribute { value: new_value.clone(), attr: *method_name, ctx: *ctx };
let mut new_func = self.fold_expr(new_func)?;
let ExprKind::Attribute { value, .. } = new_func.node else { unreachable!() };
new_func.node =
ExprKind::Attribute { value, attr: r.2 .0.to_string().into(), ctx: *ctx };
new_func.custom = Some(r.1);
Ok(Some(new_func))
}
None => report_error(
format!("Ancestor method [{class_name}.{method_name}] should be defined with same decorator as its overridden version").as_str(),
*location,
),
}
}
fn fold_call( fn fold_call(
&mut self, &mut self,
location: Location, location: Location,
@ -1767,20 +1685,8 @@ impl<'a> Inferencer<'a> {
return Ok(spec_call_func); return Ok(spec_call_func);
} }
// Check for call to parent method let func = Box::new(self.fold_expr(func)?);
let override_res = self.check_overriding(&func, &args)?; let args = args.into_iter().map(|v| self.fold_expr(v)).collect::<Result<Vec<_>, _>>()?;
let is_override = override_res.is_some();
let func = if is_override { override_res.unwrap() } else { self.fold_expr(func)? };
let func = Box::new(func);
let mut args =
args.into_iter().map(|v| self.fold_expr(v)).collect::<Result<Vec<_>, _>>()?;
// TODO: Handle passing of self to functions to allow runtime lookup of functions to be called
// Currently removing `self` and using compile time function definitions
if is_override {
args.remove(0);
}
let keywords = keywords let keywords = keywords
.into_iter() .into_iter()
.map(|v| fold::fold_keyword(self, v)) .map(|v| fold::fold_keyword(self, v))

View File

@ -66,7 +66,7 @@ struct cslice {
}; };
void output_int32_list(struct cslice* slice) { void output_int32_list(struct cslice* slice) {
const int32_t* data = (int32_t*)slice->data; const int32_t* data = reinterpret_cast<int32_t*>(slice->data);
putchar('['); putchar('[');
for (size_t i = 0; i < slice->len; ++i) { for (size_t i = 0; i < slice->len; ++i) {
@ -81,7 +81,7 @@ void output_int32_list(struct cslice* slice) {
} }
void output_str(struct cslice* slice) { void output_str(struct cslice* slice) {
const char* data = (const char*)slice->data; const char* data = reinterpret_cast<const char*>(slice->data);
for (size_t i = 0; i < slice->len; ++i) { for (size_t i = 0; i < slice->len; ++i) {
putchar(data[i]); putchar(data[i]);
@ -95,7 +95,7 @@ void output_strln(struct cslice* slice) {
uint64_t dbg_stack_address(__attribute__((unused)) struct cslice* slice) { uint64_t dbg_stack_address(__attribute__((unused)) struct cslice* slice) {
int i; int i;
void* ptr = (void*)&i; void* ptr = static_cast<void*>(&i);
return (uintptr_t)ptr; return (uintptr_t)ptr;
} }
@ -120,10 +120,10 @@ struct Exception {
uint32_t __nac3_raise(struct Exception* e) { uint32_t __nac3_raise(struct Exception* e) {
printf("__nac3_raise called. Exception details:\n"); printf("__nac3_raise called. Exception details:\n");
printf(" ID: %" PRIu32 "\n", e->id); printf(" ID: %" PRIu32 "\n", e->id);
printf(" Location: %*s:%" PRIu32 ":%" PRIu32 "\n", (int)e->file.len, (const char*)e->file.data, e->line, printf(" Location: %*s:%" PRIu32 ":%" PRIu32 "\n", static_cast<int>(e->file.len),
e->column); reinterpret_cast<const char*>(e->file.data), e->line, e->column);
printf(" Function: %*s\n", (int)e->function.len, (const char*)e->function.data); printf(" Function: %*s\n", static_cast<int>(e->function.len), reinterpret_cast<const char*>(e->function.data));
printf(" Message: \"%*s\"\n", (int)e->message.len, (const char*)e->message.data); printf(" Message: \"%*s\"\n", static_cast<int>(e->message.len), reinterpret_cast<const char*>(e->message.data));
printf(" Params: {0}=%" PRId64 ", {1}=%" PRId64 ", {2}=%" PRId64 "\n", e->param[0], e->param[1], e->param[2]); printf(" Params: {0}=%" PRId64 ", {1}=%" PRId64 ", {2}=%" PRId64 "\n", e->param[0], e->param[1], e->param[2]);
exit(101); exit(101);
__builtin_unreachable(); __builtin_unreachable();

View File

@ -9,7 +9,6 @@ def output_bool(x: bool):
def example1(): def example1():
x, *ys, z = (1, 2, 3, 4, 5) x, *ys, z = (1, 2, 3, 4, 5)
output_int32(x) output_int32(x)
output_int32(len(ys))
output_int32(ys[0]) output_int32(ys[0])
output_int32(ys[1]) output_int32(ys[1])
output_int32(ys[2]) output_int32(ys[2])
@ -19,14 +18,12 @@ def example2():
x, y, *zs = (1, 2, 3, 4, 5) x, y, *zs = (1, 2, 3, 4, 5)
output_int32(x) output_int32(x)
output_int32(y) output_int32(y)
output_int32(len(zs))
output_int32(zs[0]) output_int32(zs[0])
output_int32(zs[1]) output_int32(zs[1])
output_int32(zs[2]) output_int32(zs[2])
def example3(): def example3():
*xs, y, z = (1, 2, 3, 4, 5) *xs, y, z = (1, 2, 3, 4, 5)
output_int32(len(xs))
output_int32(xs[0]) output_int32(xs[0])
output_int32(xs[1]) output_int32(xs[1])
output_int32(xs[2]) output_int32(xs[2])
@ -34,12 +31,6 @@ def example3():
output_int32(z) output_int32(z)
def example4(): def example4():
*xs, y, z = (4, 5)
output_int32(len(xs))
output_int32(y)
output_int32(z)
def example5():
# Example from: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements # Example from: https://docs.python.org/3/reference/simple_stmts.html#assignment-statements
x = [0, 1] x = [0, 1]
i = 0 i = 0
@ -53,7 +44,7 @@ class A:
def __init__(self): def __init__(self):
self.value = 1000 self.value = 1000
def example6(): def example5():
ws = [88, 7, 8] ws = [88, 7, 8]
a = A() a = A()
x, [y, *ys, a.value], ws[0], (ws[0],) = 1, (2, False, 4, 5), 99, (6,) x, [y, *ys, a.value], ws[0], (ws[0],) = 1, (2, False, 4, 5), 99, (6,)
@ -72,5 +63,4 @@ def run() -> int32:
example3() example3()
example4() example4()
example5() example5()
example6()
return 0 return 0

View File

@ -10,58 +10,23 @@ class A:
def __init__(self, a: int32): def __init__(self, a: int32):
self.a = a self.a = a
def output_all_fields(self): def f1(self):
output_int32(self.a) self.f2()
def set_a(self, a: int32): def f2(self):
self.a = a output_int32(self.a)
class B(A): class B(A):
b: int32 b: int32
def __init__(self, b: int32): def __init__(self, b: int32):
A.__init__(self, b + 1) self.a = b + 1
self.set_b(b)
def output_parent_fields(self):
A.output_all_fields(self)
def output_all_fields(self):
A.output_all_fields(self)
output_int32(self.b)
def set_b(self, b: int32):
self.b = b self.b = b
class C(B):
c: int32
def __init__(self, c: int32):
B.__init__(self, c + 1)
self.c = c
def output_parent_fields(self):
B.output_all_fields(self)
def output_all_fields(self):
B.output_all_fields(self)
output_int32(self.c)
def set_c(self, c: int32):
self.c = c
def run() -> int32: def run() -> int32:
ccc = C(10) aaa = A(5)
ccc.output_all_fields() bbb = B(2)
ccc.set_a(1) aaa.f1()
ccc.set_b(2) bbb.f1()
ccc.set_c(3)
ccc.output_all_fields()
bbb = B(10)
bbb.set_a(9)
bbb.set_b(8)
bbb.output_all_fields()
ccc.output_all_fields()
return 0 return 0

View File

@ -15,6 +15,7 @@ use std::{collections::HashMap, sync::Arc};
pub struct ResolverInternal { pub struct ResolverInternal {
pub id_to_type: Mutex<HashMap<StrRef, Type>>, pub id_to_type: Mutex<HashMap<StrRef, Type>>,
pub id_to_def: Mutex<HashMap<StrRef, DefinitionId>>, pub id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
pub class_names: Mutex<HashMap<StrRef, Type>>,
pub module_globals: Mutex<HashMap<StrRef, SymbolValue>>, pub module_globals: Mutex<HashMap<StrRef, SymbolValue>>,
pub str_store: Mutex<HashMap<String, i32>>, pub str_store: Mutex<HashMap<String, i32>>,
} }

View File

@ -306,6 +306,7 @@ fn main() {
let internal_resolver: Arc<ResolverInternal> = ResolverInternal { let internal_resolver: Arc<ResolverInternal> = ResolverInternal {
id_to_type: builtins_ty.into(), id_to_type: builtins_ty.into(),
id_to_def: builtins_def.into(), id_to_def: builtins_def.into(),
class_names: Mutex::default(),
module_globals: Mutex::default(), module_globals: Mutex::default(),
str_store: Mutex::default(), str_store: Mutex::default(),
} }