forked from M-Labs/nac3
1
0
Fork 0

Compare commits

...

12 Commits

Author SHA1 Message Date
lyken eac164ce11 WIP 2024-07-18 16:07:47 +08:00
lyken e8aa6129f0 core/irrt: add c-string utils 2024-07-17 17:50:55 +08:00
lyken e1abb819c6 core: refactor to lift `struct SizeVariant` to /util.rs 2024-07-17 17:50:49 +08:00
lyken db9e9586b5 core/model: add var_alloc & array_var_alloc 2024-07-17 17:50:39 +08:00
lyken 2525369760 core/model: add FunctionBuilder 2024-07-17 17:50:31 +08:00
lyken 06b64ea888 core/model: introduce `Model<'ctx>` abstraction 2024-07-17 17:50:23 +08:00
lyken 37a022e156 core/irrt: introduce irrt testing
`cargo test -F test` would compile `nac3core/irrt/irrt_test.cpp`
targetted to the host machine (it gets to use `std`) and run the
test executable.
2024-07-17 12:14:17 +08:00
lyken 17e8eeaa46 core/irrt: split irrt.cpp into headers & reformat
To prepare organizing for future IRRT implementations. There will be a *lot* of C++
code.
2024-07-17 12:14:15 +08:00
lyken eb3aa18ce1 core/irrt: build.rs capture IR defined constants 2024-07-17 12:14:13 +08:00
lyken 8068bd5cb0 core/irrt: build.rs capture IR defined types 2024-07-17 12:14:08 +08:00
lyken 3a8c4009fd core/irrt: comment build.rs & move irrt to its own dir
To prepare for future IRRT implementations, and to also make cargo
only have to watch a single directory.
2024-07-17 12:14:05 +08:00
lyken ca13f56404 core/irrt: build with -W=return-type
To help catch future C++ programming bugs
2024-07-17 12:14:02 +08:00
34 changed files with 1802 additions and 185 deletions

View File

@ -13,6 +13,7 @@
''
mkdir -p $out/bin
ln -s ${pkgs.llvmPackages_14.clang-unwrapped}/bin/clang $out/bin/clang-irrt
ln -s ${pkgs.llvmPackages_14.clang}/bin/clang $out/bin/clang-irrt-test
ln -s ${pkgs.llvmPackages_14.llvm.out}/bin/llvm-as $out/bin/llvm-as-irrt
'';
nac3artiq = pkgs.python3Packages.toPythonModule (
@ -23,6 +24,7 @@
cargoLock = {
lockFile = ./Cargo.lock;
};
cargoTestFlags = [ "--features" "test" ];
passthru.cargoLock = cargoLock;
nativeBuildInputs = [ pkgs.python3 pkgs.llvmPackages_14.clang llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ];
buildInputs = [ pkgs.python3 llvm-nac3 ];

View File

@ -1,3 +1,6 @@
[features]
test = []
[package]
name = "nac3core"
version = "0.1.0"

View File

@ -3,20 +3,34 @@ use std::{
env,
fs::File,
io::Write,
path::Path,
path::{Path, PathBuf},
process::{Command, Stdio},
};
fn main() {
const FILE: &str = "src/codegen/irrt/irrt.cpp";
const CMD_IRRT_CLANG: &str = "clang-irrt";
const CMD_IRRT_CLANG_TEST: &str = "clang-irrt-test";
const CMD_IRRT_LLVM_AS: &str = "llvm-as-irrt";
fn get_out_dir() -> PathBuf {
PathBuf::from(env::var("OUT_DIR").unwrap())
}
fn get_irrt_dir() -> &'static Path {
Path::new("irrt")
}
/// Compile `irrt.cpp` for use in `src/codegen`
fn compile_irrt_cpp() {
let out_dir = get_out_dir();
let irrt_dir = get_irrt_dir();
/*
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
* Compiling for WASM32 and filtering the output with regex is the closest we can get.
*/
let irrt_cpp_path = irrt_dir.join("irrt.cpp");
let flags: &[&str] = &[
"--target=wasm32",
FILE,
"-x",
"c++",
"-fno-discard-value-names",
@ -31,15 +45,19 @@ fn main() {
"-S",
"-Wall",
"-Wextra",
"-Werror=return-type",
"-o",
"-",
"-I",
irrt_dir.to_str().unwrap(),
irrt_cpp_path.to_str().unwrap(),
];
println!("cargo:rerun-if-changed={FILE}");
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);
// Tell Cargo to rerun if any file under `irrt_dir` (recursive) changes
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
let output = Command::new("clang-irrt")
// Compile IRRT and capture the LLVM IR output
let output = Command::new(CMD_IRRT_CLANG)
.args(flags)
.output()
.map(|o| {
@ -52,7 +70,17 @@ fn main() {
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
let mut filtered_output = String::with_capacity(output.len());
let regex_filter = Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)").unwrap();
// Filter out irrelevant IR
//
// Regex:
// - `(?ms:^define.*?\}$)` captures LLVM `define` blocks
// - `(?m:^declare.*?$)` captures LLVM `declare` lines
// - `(?m:^%.+?=\s*type\s*\{.+?\}$)` captures LLVM `type` declarations
// - `(?m:^@.+?=.+$)` captures global constants
let regex_filter = Regex::new(
r"(?ms:^define.*?\}$)|(?m:^declare.*?$)|(?m:^%.+?=\s*type\s*\{.+?\}$)|(?m:^@.+?=.+$)",
)
.unwrap();
for f in regex_filter.captures_iter(&output) {
assert_eq!(f.len(), 1);
filtered_output.push_str(&f[0]);
@ -63,20 +91,71 @@ fn main() {
.unwrap()
.replace_all(&filtered_output, "");
println!("cargo:rerun-if-env-changed=DEBUG_DUMP_IRRT");
if env::var("DEBUG_DUMP_IRRT").is_ok() {
let mut file = File::create(out_path.join("irrt.ll")).unwrap();
// For debugging
// Doing `DEBUG_DUMP_IRRT=1 cargo build -p nac3core` dumps the LLVM IR generated
const DEBUG_DUMP_IRRT: &str = "DEBUG_DUMP_IRRT";
println!("cargo:rerun-if-env-changed={DEBUG_DUMP_IRRT}");
if env::var(DEBUG_DUMP_IRRT).is_ok() {
let mut file = File::create(out_dir.join("irrt.ll")).unwrap();
file.write_all(output.as_bytes()).unwrap();
let mut file = File::create(out_path.join("irrt-filtered.ll")).unwrap();
let mut file = File::create(out_dir.join("irrt-filtered.ll")).unwrap();
file.write_all(filtered_output.as_bytes()).unwrap();
}
let mut llvm_as = Command::new("llvm-as-irrt")
// Assemble the emitted and filtered IR to .bc
// That .bc will be integrated into nac3core's codegen
let mut llvm_as = Command::new(CMD_IRRT_LLVM_AS)
.stdin(Stdio::piped())
.arg("-o")
.arg(out_path.join("irrt.bc"))
.arg(out_dir.join("irrt.bc"))
.spawn()
.unwrap();
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
assert!(llvm_as.wait().unwrap().success());
}
/// Compile `irrt_test.cpp` for testing
fn compile_irrt_test_cpp() {
let out_dir = get_out_dir();
let irrt_dir = get_irrt_dir();
let exe_path = out_dir.join("irrt_test.out"); // Output path of the compiled test executable
let irrt_test_cpp_path = irrt_dir.join("irrt_test.cpp");
let flags: &[&str] = &[
irrt_test_cpp_path.to_str().unwrap(),
"-x",
"c++",
"-I",
irrt_dir.to_str().unwrap(),
"-g",
"-fno-discard-value-names",
"-O0",
"-Wall",
"-Wextra",
"-Werror=return-type",
"-lm", // for `tgamma()`, `lgamma()`
"-o",
exe_path.to_str().unwrap(),
];
Command::new(CMD_IRRT_CLANG_TEST)
.args(flags)
.output()
.map(|o| {
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
o
})
.unwrap();
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
}
fn main() {
compile_irrt_cpp();
// https://github.com/rust-lang/cargo/issues/2549
// `cargo test -F test` to also build `irrt_test.cpp
if cfg!(feature = "test") {
compile_irrt_test_cpp();
}
}

9
nac3core/irrt/irrt.cpp Normal file
View File

@ -0,0 +1,9 @@
#define IRRT_DEFINE_TYPEDEF_INTS
#include <irrt_everything.hpp>
/*
All IRRT implementations.
We don't have any pre-compiled objects, so we are writing all implementations in headers and
concatenate them with `#include` into one massive source file that contains all the IRRT stuff.
*/

View File

@ -1,27 +1,17 @@
using int8_t = _BitInt(8);
using uint8_t = unsigned _BitInt(8);
using int32_t = _BitInt(32);
using uint32_t = unsigned _BitInt(32);
using int64_t = _BitInt(64);
using uint64_t = unsigned _BitInt(64);
#pragma once
#include <irrt/int_defs.hpp>
#include <irrt/utils.hpp>
// NDArray indices are always `uint32_t`.
using NDIndex = uint32_t;
// The type of an index or a value describing the length of a range/slice is always `int32_t`.
// The type of an index or a value describing the length of a
// range/slice is always `int32_t`.
using SliceIndex = int32_t;
namespace {
template <typename T>
const T& max(const T& a, const T& b) {
return a > b ? a : b;
}
template <typename T>
const T& min(const T& a, const T& b) {
return a > b ? b : a;
}
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
// adapted from GNU Scientific Library:
// https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
// need to make sure `exp >= 0` before calling this function
template <typename T>
T __nac3_int_exp_impl(T base, T exp) {
@ -38,12 +28,7 @@ T __nac3_int_exp_impl(T base, T exp) {
}
template <typename SizeT>
SizeT __nac3_ndarray_calc_size_impl(
const SizeT* list_data,
SizeT list_len,
SizeT begin_idx,
SizeT end_idx
) {
SizeT __nac3_ndarray_calc_size_impl(const SizeT* list_data, SizeT list_len, SizeT begin_idx, SizeT end_idx) {
__builtin_assume(end_idx <= list_len);
SizeT num_elems = 1;
@ -56,12 +41,7 @@ SizeT __nac3_ndarray_calc_size_impl(
}
template <typename SizeT>
void __nac3_ndarray_calc_nd_indices_impl(
SizeT index,
const SizeT* dims,
SizeT num_dims,
NDIndex* idxs
) {
void __nac3_ndarray_calc_nd_indices_impl(SizeT index, const SizeT* dims, SizeT num_dims, NDIndex* idxs) {
SizeT stride = 1;
for (SizeT dim = 0; dim < num_dims; dim++) {
SizeT i = num_dims - dim - 1;
@ -72,12 +52,7 @@ void __nac3_ndarray_calc_nd_indices_impl(
}
template <typename SizeT>
SizeT __nac3_ndarray_flatten_index_impl(
const SizeT* dims,
SizeT num_dims,
const NDIndex* indices,
SizeT num_indices
) {
SizeT __nac3_ndarray_flatten_index_impl(const SizeT* dims, SizeT num_dims, const NDIndex* indices, SizeT num_indices) {
SizeT idx = 0;
SizeT stride = 1;
for (SizeT i = 0; i < num_dims; ++i) {
@ -94,17 +69,14 @@ SizeT __nac3_ndarray_flatten_index_impl(
template <typename SizeT>
void __nac3_ndarray_calc_broadcast_impl(
const SizeT* lhs_dims,
SizeT lhs_ndims,
const SizeT* rhs_dims,
SizeT rhs_ndims,
SizeT* out_dims
const SizeT* lhs_dims, SizeT lhs_ndims, const SizeT* rhs_dims, SizeT rhs_ndims, SizeT* out_dims
) {
SizeT max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims;
for (SizeT i = 0; i < max_ndims; ++i) {
const SizeT* lhs_dim_sz = i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : nullptr;
const SizeT* rhs_dim_sz = i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : nullptr;
SizeT* out_dim = &out_dims[max_ndims - i - 1];
if (lhs_dim_sz == nullptr) {
@ -125,10 +97,7 @@ void __nac3_ndarray_calc_broadcast_impl(
template <typename SizeT>
void __nac3_ndarray_calc_broadcast_idx_impl(
const SizeT* src_dims,
SizeT src_ndims,
const NDIndex* in_idx,
NDIndex* out_idx
const SizeT* src_dims, SizeT src_ndims, const NDIndex* in_idx, NDIndex* out_idx
) {
for (SizeT i = 0; i < src_ndims; ++i) {
SizeT src_i = src_ndims - i - 1;
@ -139,14 +108,14 @@ void __nac3_ndarray_calc_broadcast_idx_impl(
extern "C" {
#define DEF_nac3_int_exp_(T) \
T __nac3_int_exp_##T(T base, T exp) {\
return __nac3_int_exp_impl(base, exp);\
T __nac3_int_exp_##T(T base, T exp) { \
return __nac3_int_exp_impl(base, exp); \
}
DEF_nac3_int_exp_(int32_t)
DEF_nac3_int_exp_(int64_t)
DEF_nac3_int_exp_(uint32_t)
DEF_nac3_int_exp_(uint64_t)
DEF_nac3_int_exp_(int32_t);
DEF_nac3_int_exp_(int64_t);
DEF_nac3_int_exp_(uint32_t);
DEF_nac3_int_exp_(uint64_t);
SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) {
if (i < 0) {
@ -160,11 +129,7 @@ SliceIndex __nac3_slice_index_bound(SliceIndex i, const SliceIndex len) {
return i;
}
SliceIndex __nac3_range_slice_len(
const SliceIndex start,
const SliceIndex end,
const SliceIndex step
) {
SliceIndex __nac3_range_slice_len(const SliceIndex start, const SliceIndex end, const SliceIndex step) {
SliceIndex diff = end - start;
if (diff > 0 && step > 0) {
return ((diff - 1) / step) + 1;
@ -180,7 +145,8 @@ SliceIndex __nac3_range_slice_len(
// - All the index must *not* be out-of-bound or negative,
// - The end index is *inclusive*,
// - The length of src and dest slice size should already
// be checked: if dest.step == 1 then len(src) <= len(dest) else len(src) == len(dest)
// be checked: if dest.step == 1 then len(src) <= len(dest) else
// len(src) == len(dest)
SliceIndex __nac3_list_slice_assign_var_size(
SliceIndex dest_start,
SliceIndex dest_end,
@ -194,18 +160,18 @@ SliceIndex __nac3_list_slice_assign_var_size(
SliceIndex src_arr_len,
const SliceIndex size
) {
/* if dest_arr_len == 0, do nothing since we do not support extending list */
if (dest_arr_len == 0) return dest_arr_len;
/* if both step is 1, memmove directly, handle the dropping of the list, and shrink size */
/* if dest_arr_len == 0, do nothing since we do not support
* extending list
*/
if (dest_arr_len == 0)
return dest_arr_len;
/* if both step is 1, memmove directly, handle the dropping of
* the list, and shrink size */
if (src_step == dest_step && dest_step == 1) {
const SliceIndex src_len = (src_end >= src_start) ? (src_end - src_start + 1) : 0;
const SliceIndex dest_len = (dest_end >= dest_start) ? (dest_end - dest_start + 1) : 0;
if (src_len > 0) {
__builtin_memmove(
dest_arr + dest_start * size,
src_arr + src_start * size,
src_len * size
);
__builtin_memmove(dest_arr + dest_start * size, src_arr + src_start * size, src_len * size);
}
if (dest_len > 0) {
/* dropping */
@ -219,23 +185,17 @@ SliceIndex __nac3_list_slice_assign_var_size(
return dest_arr_len - (dest_len - src_len);
}
/* if two range overlaps, need alloca */
uint8_t need_alloca =
(dest_arr == src_arr)
&& !(
max(dest_start, dest_end) < min(src_start, src_end)
|| max(src_start, src_end) < min(dest_start, dest_end)
uint8_t need_alloca = (dest_arr == src_arr)
&& !(max(dest_start, dest_end) < min(src_start, src_end) || max(src_start, src_end) < min(dest_start, dest_end)
);
if (need_alloca) {
uint8_t* tmp = reinterpret_cast<uint8_t *>(__builtin_alloca(src_arr_len * size));
uint8_t* tmp = reinterpret_cast<uint8_t*>(__builtin_alloca(src_arr_len * size));
__builtin_memcpy(tmp, src_arr, src_arr_len * size);
src_arr = tmp;
}
SliceIndex src_ind = src_start;
SliceIndex dest_ind = dest_start;
for (;
(src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end);
src_ind += src_step, dest_ind += dest_step
) {
for (; (src_step > 0) ? (src_ind <= src_end) : (src_ind >= src_end); src_ind += src_step, dest_ind += dest_step) {
/* for constant optimization */
if (size == 1) {
__builtin_memcpy(dest_arr + dest_ind, src_arr + src_ind, 1);
@ -244,7 +204,8 @@ SliceIndex __nac3_list_slice_assign_var_size(
} else if (size == 8) {
__builtin_memcpy(dest_arr + dest_ind * 8, src_arr + src_ind * 8, 8);
} else {
/* memcpy for var size, cannot overlap after previous alloca */
/* memcpy for var size, cannot overlap after previous
* alloca */
__builtin_memcpy(dest_arr + dest_ind * size, src_arr + src_ind * size, size);
}
}
@ -254,7 +215,7 @@ SliceIndex __nac3_list_slice_assign_var_size(
__builtin_memmove(
dest_arr + dest_ind * size,
dest_arr + (dest_end + 1) * size,
(dest_arr_len - dest_end - 1) * size
(dest_arr_len - dest_end - 1) * size + size + size + size
);
return dest_arr_len - (dest_end - dest_ind) - 1;
}
@ -320,94 +281,53 @@ double __nac3_j0(double x) {
return j0(x);
}
uint32_t __nac3_ndarray_calc_size(
const uint32_t* list_data,
uint32_t list_len,
uint32_t begin_idx,
uint32_t end_idx
) {
uint32_t __nac3_ndarray_calc_size(const uint32_t* list_data, uint32_t list_len, uint32_t begin_idx, uint32_t end_idx) {
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
}
uint64_t __nac3_ndarray_calc_size64(
const uint64_t* list_data,
uint64_t list_len,
uint64_t begin_idx,
uint64_t end_idx
) {
uint64_t
__nac3_ndarray_calc_size64(const uint64_t* list_data, uint64_t list_len, uint64_t begin_idx, uint64_t end_idx) {
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
}
void __nac3_ndarray_calc_nd_indices(
uint32_t index,
const uint32_t* dims,
uint32_t num_dims,
NDIndex* idxs
) {
void __nac3_ndarray_calc_nd_indices(uint32_t index, const uint32_t* dims, uint32_t num_dims, NDIndex* idxs) {
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
}
void __nac3_ndarray_calc_nd_indices64(
uint64_t index,
const uint64_t* dims,
uint64_t num_dims,
NDIndex* idxs
) {
void __nac3_ndarray_calc_nd_indices64(uint64_t index, const uint64_t* dims, uint64_t num_dims, NDIndex* idxs) {
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
}
uint32_t __nac3_ndarray_flatten_index(
const uint32_t* dims,
uint32_t num_dims,
const NDIndex* indices,
uint32_t num_indices
) {
uint32_t
__nac3_ndarray_flatten_index(const uint32_t* dims, uint32_t num_dims, const NDIndex* indices, uint32_t num_indices) {
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
}
uint64_t __nac3_ndarray_flatten_index64(
const uint64_t* dims,
uint64_t num_dims,
const NDIndex* indices,
uint64_t num_indices
) {
uint64_t
__nac3_ndarray_flatten_index64(const uint64_t* dims, uint64_t num_dims, const NDIndex* indices, uint64_t num_indices) {
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
}
void __nac3_ndarray_calc_broadcast(
const uint32_t* lhs_dims,
uint32_t lhs_ndims,
const uint32_t* rhs_dims,
uint32_t rhs_ndims,
uint32_t* out_dims
const uint32_t* lhs_dims, uint32_t lhs_ndims, const uint32_t* rhs_dims, uint32_t rhs_ndims, uint32_t* out_dims
) {
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
}
void __nac3_ndarray_calc_broadcast64(
const uint64_t* lhs_dims,
uint64_t lhs_ndims,
const uint64_t* rhs_dims,
uint64_t rhs_ndims,
uint64_t* out_dims
const uint64_t* lhs_dims, uint64_t lhs_ndims, const uint64_t* rhs_dims, uint64_t rhs_ndims, uint64_t* out_dims
) {
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
}
void __nac3_ndarray_calc_broadcast_idx(
const uint32_t* src_dims,
uint32_t src_ndims,
const NDIndex* in_idx,
NDIndex* out_idx
const uint32_t* src_dims, uint32_t src_ndims, const NDIndex* in_idx, NDIndex* out_idx
) {
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
}
void __nac3_ndarray_calc_broadcast_idx64(
const uint64_t* src_dims,
uint64_t src_ndims,
const NDIndex* in_idx,
NDIndex* out_idx
const uint64_t* src_dims, uint64_t src_ndims, const NDIndex* in_idx, NDIndex* out_idx
) {
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
}

View File

@ -0,0 +1,87 @@
#pragma once
#include <irrt/int_defs.hpp>
#include <irrt/utils.hpp>
namespace {
// nac3core's "str" struct type definition
template <typename SizeT>
struct Str {
const char* content;
SizeT length;
};
// A limited set of errors IRRT could use.
typedef uint32_t ErrorId;
struct ErrorIds {
ErrorId index_error;
ErrorId value_error;
ErrorId assertion_error;
ErrorId runtime_error;
ErrorId type_error;
};
struct ErrorContext {
// Context
ErrorIds* error_ids;
// Error thrown by IRRT
ErrorId error_id;
const char* message_template; // MUST BE `&'static`
uint64_t param1;
uint64_t param2;
uint64_t param3;
void initialize(ErrorIds* error_ids) {
this->error_ids = error_ids;
clear_error();
}
void clear_error() {
// Point the message_template to an empty str. Don't set it to nullptr as a sentinel
this->message_template = "";
}
void
set_error(ErrorId error_id, const char* message, uint64_t param1 = 0, uint64_t param2 = 0, uint64_t param3 = 0) {
this->error_id = error_id;
this->message_template = message;
this->param1 = param1;
this->param2 = param2;
this->param3 = param3;
}
bool has_error() { return !cstr_utils::is_empty(message_template); }
/// Get a nac3core-understanding `Str<SizeT>` that containing
/// the error message template
template <typename SizeT>
void get_error_str(Str<SizeT>* dst_str) {
dst_str->content = message_template;
dst_str->length = (SizeT)cstr_utils::length(message_template);
}
};
} // namespace
extern "C" {
void __nac3_error_context_initialize(ErrorContext* errctx, ErrorIds* error_ids) {
errctx->initialize(error_ids);
}
bool __nac3_error_context_has_no_error(ErrorContext* errctx) {
return !errctx->has_error();
}
void __nac3_error_context_get_error_str(ErrorContext* errctx, Str<int32_t>* dst_str) {
errctx->get_error_str<int32_t>(dst_str);
}
void __nac3_error_context_get_error_str64(ErrorContext* errctx, Str<int64_t>* dst_str) {
errctx->get_error_str<int64_t>(dst_str);
}
// Used for testing
void __nac3_error_dummy_raise(ErrorContext* errctx) {
errctx->set_error(errctx->error_ids->runtime_error, "Error thrown from __nac3_error_dummy_raise");
}
}

View File

@ -0,0 +1,12 @@
#pragma once
// This is made toggleable since `irrt_test.cpp` itself would include
// headers that define these typedefs
#ifdef IRRT_DEFINE_TYPEDEF_INTS
using int8_t = _BitInt(8);
using uint8_t = unsigned _BitInt(8);
using int32_t = _BitInt(32);
using uint32_t = unsigned _BitInt(32);
using int64_t = _BitInt(64);
using uint64_t = unsigned _BitInt(64);
#endif

View File

@ -0,0 +1,77 @@
#pragma once
namespace {
template <typename T>
const T& max(const T& a, const T& b) {
return a > b ? a : b;
}
template <typename T>
const T& min(const T& a, const T& b) {
return a > b ? b : a;
}
template <typename T>
bool arrays_match(int len, T* as, T* bs) {
for (int i = 0; i < len; i++) {
if (as[i] != bs[i])
return false;
}
return true;
}
namespace cstr_utils {
bool is_empty(const char* str) {
return str[0] == '\0';
}
int8_t compare(const char* a, const char* b) {
uint32_t i = 0;
while (true) {
if (a[i] < b[i]) {
return -1;
} else if (a[i] > b[i]) {
return 1;
} else { // a[i] == b[i]
if (a[i] == '\0') {
return 0;
} else {
i++;
}
}
}
}
int8_t equal(const char* a, const char* b) {
return compare(a, b) == 0;
}
uint32_t length(const char* str) {
uint32_t length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}
bool copy(const char* src, char* dst, uint32_t dst_max_size) {
for (uint32_t i = 0; i < dst_max_size; i++) {
bool is_last = i + 1 == dst_max_size;
if (is_last && src[i] != '\0') {
dst[i] = '\0';
return false;
}
if (src[i] == '\0') {
dst[i] = '\0';
return true;
}
dst[i] = src[i];
}
__builtin_unreachable();
}
} // namespace cstr_utils
} // namespace

View File

@ -0,0 +1,6 @@
#pragma once
#include <irrt/core.hpp>
#include <irrt/int_defs.hpp>
#include <irrt/utils.hpp>
#include <irrt/error_context.hpp>

View File

@ -0,0 +1,13 @@
// This file will be compiled like a real C++ program,
// and we do have the luxury to use the standard libraries.
// That is if the nix flakes do not have issues... especially on msys2...
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <test/test_core.hpp>
int main() {
test::core::run();
return 0;
}

View File

@ -0,0 +1,12 @@
#pragma once
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <test/util.hpp>
#include <irrt_everything.hpp>
/*
Include this header for every test_*.cpp
*/

View File

@ -0,0 +1,16 @@
#pragma once
#include <test/includes.hpp>
namespace test { namespace core {
void test_int_exp() {
BEGIN_TEST();
assert_values_match(125, __nac3_int_exp_impl<int32_t>(5, 3));
assert_values_match(3125, __nac3_int_exp_impl<int32_t>(5, 5));
}
void run() {
test_int_exp();
}
}} // namespace test::core

114
nac3core/irrt/test/util.hpp Normal file
View File

@ -0,0 +1,114 @@
#pragma once
#include <cstdlib>
#include <cstdio>
template <class T>
void print_value(const T& value) {}
template <>
void print_value(const int8_t& value) {
printf("%d", value);
}
template <>
void print_value(const int32_t& value) {
printf("%d", value);
}
template <>
void print_value(const uint8_t& value) {
printf("%u", value);
}
template <>
void print_value(const uint32_t& value) {
printf("%u", value);
}
template <>
void print_value(const float& value) {
printf("%f", value);
}
template <>
void print_value(const double& value) {
printf("%f", value);
}
void __begin_test(const char* function_name, const char* file, int line) {
printf("######### Running %s @ %s:%d\n", function_name, file, line);
}
#define BEGIN_TEST() __begin_test(__FUNCTION__, __FILE__, __LINE__)
void test_fail() {
printf("[!] Test failed. Exiting with status code 1.\n");
exit(1);
}
template <typename T>
void debug_print_array(int len, const T* as) {
printf("[");
for (int i = 0; i < len; i++) {
if (i != 0)
printf(", ");
print_value(as[i]);
}
printf("]");
}
void print_assertion_passed(const char* file, int line) {
printf("[*] Assertion passed on %s:%d\n", file, line);
}
void print_assertion_failed(const char* file, int line) {
printf("[!] Assertion failed on %s:%d\n", file, line);
}
void __assert_true(const char* file, int line, bool cond) {
if (cond) {
print_assertion_passed(file, line);
} else {
print_assertion_failed(file, line);
test_fail();
}
}
#define assert_true(cond) __assert_true(__FILE__, __LINE__, cond)
template <typename T>
void __assert_arrays_match(const char* file, int line, int len, const T* expected, const T* got) {
if (arrays_match(len, expected, got)) {
print_assertion_passed(file, line);
} else {
print_assertion_failed(file, line);
printf("Expect = ");
debug_print_array(len, expected);
printf("\n");
printf(" Got = ");
debug_print_array(len, got);
printf("\n");
test_fail();
}
}
#define assert_arrays_match(len, expected, got) __assert_arrays_match(__FILE__, __LINE__, len, expected, got)
template <typename T>
void __assert_values_match(const char* file, int line, T expected, T got) {
if (expected == got) {
print_assertion_passed(file, line);
} else {
print_assertion_failed(file, line);
printf("Expect = ");
print_value(expected);
printf("\n");
printf(" Got = ");
print_value(got);
printf("\n");
test_fail();
}
}
#define assert_values_match(expected, got) __assert_values_match(__FILE__, __LINE__, expected, got)

View File

@ -576,6 +576,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
params: [Option<IntValue<'ctx>>; 3],
loc: Location,
) {
let error_id = self.resolver.get_string_id(name);
let error_id = self.ctx.i32_type().const_int(error_id as u64, false);
self.raise_exn_by_id(generator, error_id, msg, params, loc);
}
pub fn raise_exn_by_id<G: CodeGenerator + ?Sized>(
&mut self,
generator: &mut G,
error_id: IntValue<'ctx>,
msg: BasicValueEnum<'ctx>,
params: [Option<IntValue<'ctx>>; 3],
loc: Location,
) {
assert_eq!(error_id.get_type().get_bit_width(), 32);
let zelf = if let Some(exception_val) = self.exception_val {
exception_val
} else {
@ -587,9 +602,6 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
let int32 = self.ctx.i32_type();
let zero = int32.const_zero();
unsafe {
let id_ptr = self.builder.build_in_bounds_gep(zelf, &[zero, zero], "exn.id").unwrap();
let id = self.resolver.get_string_id(name);
self.builder.build_store(id_ptr, int32.const_int(id as u64, false)).unwrap();
let ptr = self
.builder
.build_in_bounds_gep(zelf, &[zero, int32.const_int(5, false)], "exn.msg")
@ -652,6 +664,32 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
self.raise_exn(generator, err_name, err_msg, params, loc);
self.builder.position_at_end(then_block);
}
pub fn make_assert_impl_by_id<G: CodeGenerator + ?Sized>(
&mut self,
generator: &mut G,
cond: IntValue<'ctx>,
err_id: IntValue<'ctx>,
err_msg: BasicValueEnum<'ctx>,
params: [Option<IntValue<'ctx>>; 3],
loc: Location,
) {
let i1 = self.ctx.bool_type();
let i1_true = i1.const_all_ones();
// we assume that the condition is most probably true, so the normal path is the most
// probable path
// even if this assumption is violated, it does not matter as exception unwinding is
// slow anyway...
let cond = call_expect(self, cond, i1_true, Some("expect"));
let current_bb = self.builder.get_insert_block().unwrap();
let current_fun = current_bb.get_parent().unwrap();
let then_block = self.ctx.insert_basic_block_after(current_bb, "succ");
let exn_block = self.ctx.append_basic_block(current_fun, "fail");
self.builder.build_conditional_branch(cond, then_block, exn_block).unwrap();
self.builder.position_at_end(exn_block);
self.raise_exn_by_id(generator, err_id, err_msg, params, loc);
self.builder.position_at_end(then_block);
}
}
/// See [`CodeGenerator::gen_constructor`].

View File

@ -0,0 +1,185 @@
use crate::codegen::{model::*, CodeGenContext, CodeGenerator};
use super::{string::Str, util::get_sized_dependent_function_name};
/// The [`IntModel`] of nac3core's error ID.
///
/// It is always [`Int32`].
type ErrorId = Int32;
pub struct ErrorIdsFields {
pub index_error: Field<FixedIntModel<ErrorId>>,
pub value_error: Field<FixedIntModel<ErrorId>>,
pub assertion_error: Field<FixedIntModel<ErrorId>>,
pub runtime_error: Field<FixedIntModel<ErrorId>>,
pub type_error: Field<FixedIntModel<ErrorId>>,
}
/// Corresponds to IRRT's `struct ErrorIds`
#[derive(Debug, Clone, Copy, Default)]
pub struct ErrorIds;
impl<'ctx> IsStruct<'ctx> for ErrorIds {
type Fields = ErrorIdsFields;
fn struct_name(&self) -> &'static str {
"ErrorIds"
}
fn build_fields(&self, builder: &mut FieldBuilder) -> Self::Fields {
Self::Fields {
index_error: builder.add_field_auto("index_error"),
value_error: builder.add_field_auto("value_error"),
assertion_error: builder.add_field_auto("assertion_error"),
runtime_error: builder.add_field_auto("runtime_error"),
type_error: builder.add_field_auto("type_error"),
}
}
}
pub struct ErrorContextFields {
pub error_ids: Field<PointerModel<StructModel<ErrorIds>>>,
pub error_id: Field<FixedIntModel<ErrorId>>,
pub message_template: Field<PointerModel<FixedIntModel<Byte>>>,
pub param1: Field<FixedIntModel<Int64>>,
pub param2: Field<FixedIntModel<Int64>>,
pub param3: Field<FixedIntModel<Int64>>,
}
/// Corresponds to IRRT's `struct ErrorContext`
#[derive(Debug, Clone, Copy, Default)]
pub struct ErrorContext;
impl<'ctx> IsStruct<'ctx> for ErrorContext {
type Fields = ErrorContextFields;
fn struct_name(&self) -> &'static str {
"ErrorIds"
}
fn build_fields(&self, builder: &mut FieldBuilder) -> Self::Fields {
Self::Fields {
error_ids: builder.add_field_auto("error_ids"),
error_id: builder.add_field_auto("error_id"),
message_template: builder.add_field_auto("message_template"),
param1: builder.add_field_auto("param1"),
param2: builder.add_field_auto("param2"),
param3: builder.add_field_auto("param3"),
}
}
}
// Prepare ErrorIds
fn build_error_ids<'ctx>(ctx: &CodeGenContext<'ctx, '_>) -> Pointer<'ctx, StructModel<ErrorIds>> {
// ErrorIdsLens.get_fields(ctx.ctx).assertion_error.
let error_ids = StructModel(ErrorIds).alloca(ctx, "error_ids");
let i32_model = FixedIntModel(Int32);
// i32_model.make_constant()
let get_string_id =
|string_id| i32_model.constant(ctx.ctx, ctx.resolver.get_string_id(string_id) as u64);
error_ids.gep(ctx, |f| f.index_error).store(ctx, get_string_id("0:IndexError"));
error_ids.gep(ctx, |f| f.value_error).store(ctx, get_string_id("0:ValueError"));
error_ids.gep(ctx, |f| f.assertion_error).store(ctx, get_string_id("0:AssertionError"));
error_ids.gep(ctx, |f| f.runtime_error).store(ctx, get_string_id("0:RuntimeError"));
error_ids.gep(ctx, |f| f.type_error).store(ctx, get_string_id("0:TypeError"));
error_ids
}
pub fn call_nac3_error_context_initialize<'ctx>(
ctx: &CodeGenContext<'ctx, '_>,
perrctx: Pointer<'ctx, StructModel<ErrorContext>>,
perror_ids: Pointer<'ctx, StructModel<ErrorIds>>,
) {
FunctionBuilder::begin(ctx, "__nac3_error_context_initialize")
.arg("errctx", PointerModel(StructModel(ErrorContext)), perrctx)
.arg("error_ids", PointerModel(StructModel(ErrorIds)), perror_ids)
.returning_void();
}
pub fn call_nac3_error_context_has_no_error<'ctx>(
ctx: &CodeGenContext<'ctx, '_>,
errctx: Pointer<'ctx, StructModel<ErrorContext>>,
) -> FixedInt<'ctx, Bool> {
FunctionBuilder::begin(ctx, "__nac3_error_context_has_no_error")
.arg("errctx", PointerModel(StructModel(ErrorContext)), errctx)
.returning("has_error", FixedIntModel(Bool))
}
pub fn call_nac3_error_context_get_error_str<'ctx>(
sizet: IntModel<'ctx>,
ctx: &CodeGenContext<'ctx, '_>,
errctx: Pointer<'ctx, StructModel<ErrorContext>>,
dst_str: Pointer<'ctx, StructModel<Str<'ctx>>>,
) {
FunctionBuilder::begin(
ctx,
&get_sized_dependent_function_name(sizet, "__nac3_error_context_get_error_str"),
)
.arg("errctx", PointerModel(StructModel(ErrorContext)), errctx)
.arg("dst_str", PointerModel(StructModel(Str { sizet })), dst_str)
.returning_void();
}
/// Setup a [`ErrorContext`] that could
/// be passed to IRRT functions taking in a `ErrorContext* errctx`
/// for error reporting purposes.
///
/// Also see: [`check_error_context`]
pub fn setup_error_context<'ctx>(
ctx: &CodeGenContext<'ctx, '_>,
) -> Pointer<'ctx, StructModel<ErrorContext>> {
let error_ids = build_error_ids(ctx);
let errctx_ptr = StructModel(ErrorContext).alloca(ctx, "errctx");
call_nac3_error_context_initialize(ctx, errctx_ptr, error_ids);
errctx_ptr
}
/// Check a [`ErrorContext`] to see
/// if it contains error.
///
/// If there is an error, an LLVM exception will be raised at runtime.
pub fn check_error_context<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
errctx_ptr: Pointer<'ctx, StructModel<ErrorContext>>,
) {
let sizet = IntModel(generator.get_size_type(ctx.ctx));
// Does ErrorContext contain an error?
let has_error = call_nac3_error_context_has_no_error(ctx, errctx_ptr);
// Get the error message (doesn't matter even if there's actually no error)
let pstr = StructModel(Str { sizet }).alloca(ctx, "error_str");
call_nac3_error_context_get_error_str(sizet, ctx, errctx_ptr, pstr);
// Load all the values for `ctx.make_assert_impl_by_id`
let error_id = errctx_ptr.gep(ctx, |f| f.error_id).load(ctx, "error_id");
let error_str = pstr.load(ctx, "error_str");
let param1 = errctx_ptr.gep(ctx, |f| f.param1).load(ctx, "param1");
let param2 = errctx_ptr.gep(ctx, |f| f.param2).load(ctx, "param2");
let param3 = errctx_ptr.gep(ctx, |f| f.param3).load(ctx, "param3");
// Make assert
ctx.make_assert_impl_by_id(
generator,
has_error.value,
error_id.value,
error_str.get_llvm_value(),
[Some(param1.value), Some(param2.value), Some(param3.value)],
ctx.current_loc,
);
}
pub fn call_nac3_dummy_raise<G: CodeGenerator + ?Sized>(
generator: &mut G,
ctx: &mut CodeGenContext,
) {
let errctx = setup_error_context(ctx);
FunctionBuilder::begin(ctx, "__nac3_error_dummy_raise")
.arg("errctx", PointerModel(StructModel(ErrorContext)), errctx)
.returning_void();
check_error_context(generator, ctx, errctx);
}

View File

@ -1,5 +1,10 @@
use crate::typecheck::typedef::Type;
pub mod error_context;
pub mod string;
mod test;
pub mod util;
use super::{
classes::{
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue,

View File

@ -0,0 +1,34 @@
use crate::codegen::model::*;
pub struct StrFields<'ctx> {
/// Pointer to the string. Does not have to be null-terminated.
pub content: Field<PointerModel<FixedIntModel<Byte>>>,
/// Number of bytes this string occupies in space.
///
/// The [`IntModel`] matches [`Str::sizet`].
pub length: Field<IntModel<'ctx>>,
}
/// Corresponds to IRRT's `struct Str`
///
/// nac3core's LLVM representation of a string in memory
#[derive(Debug, Clone, Copy)]
pub struct Str<'ctx> {
/// The `SizeT` type of this string.
pub sizet: IntModel<'ctx>,
}
impl<'ctx> IsStruct<'ctx> for Str<'ctx> {
type Fields = StrFields<'ctx>;
fn struct_name(&self) -> &'static str {
"Str"
}
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields {
Self::Fields {
content: builder.add_field_auto("content"),
length: builder.add_field("length", self.sizet),
}
}
}

View File

@ -0,0 +1,26 @@
#[cfg(test)]
mod tests {
use std::{path::Path, process::Command};
#[test]
fn run_irrt_test() {
assert!(
cfg!(feature = "test"),
"Please do `cargo test -F test` to compile `irrt_test.out` and run test"
);
let irrt_test_out_path = Path::new(concat!(env!("OUT_DIR"), "/irrt_test.out"));
let output = Command::new(irrt_test_out_path.to_str().unwrap()).output().unwrap();
if !output.status.success() {
eprintln!("irrt_test failed with status {}:", output.status);
eprintln!("====== stdout ======");
eprintln!("{}", String::from_utf8(output.stdout).unwrap());
eprintln!("====== stderr ======");
eprintln!("{}", String::from_utf8(output.stderr).unwrap());
eprintln!("====================");
panic!("irrt_test failed");
}
}
}

View File

@ -0,0 +1,79 @@
use inkwell::{
types::{BasicMetadataTypeEnum, BasicType, IntType},
values::{AnyValue, BasicMetadataValueEnum},
};
use crate::{
codegen::{model::*, CodeGenContext},
util::SizeVariant,
};
fn get_size_variant(ty: IntType) -> SizeVariant {
match ty.get_bit_width() {
32 => SizeVariant::Bits32,
64 => SizeVariant::Bits64,
_ => unreachable!("Unsupported int type bit width {}", ty.get_bit_width()),
}
}
#[must_use]
pub fn get_sized_dependent_function_name(ty: IntModel, fn_name: &str) -> String {
let mut fn_name = fn_name.to_owned();
match get_size_variant(ty.0) {
SizeVariant::Bits32 => {
// Do nothing, `fn_name` already has the correct name
}
SizeVariant::Bits64 => {
// Append "64", this is the naming convention
fn_name.push_str("64");
}
}
fn_name
}
// TODO: Variadic argument?
pub struct FunctionBuilder<'ctx, 'a> {
ctx: &'a CodeGenContext<'ctx, 'a>,
fn_name: &'a str,
arguments: Vec<(BasicMetadataTypeEnum<'ctx>, BasicMetadataValueEnum<'ctx>)>,
}
impl<'ctx, 'a> FunctionBuilder<'ctx, 'a> {
pub fn begin(ctx: &'a CodeGenContext<'ctx, 'a>, fn_name: &'a str) -> Self {
FunctionBuilder { ctx, fn_name, arguments: Vec::new() }
}
// The name is for self-documentation
#[must_use]
pub fn arg<M: Model<'ctx>>(mut self, _name: &'static str, model: M, value: M::Value) -> Self {
self.arguments
.push((model.get_llvm_type(self.ctx.ctx).into(), value.get_llvm_value().into()));
self
}
pub fn returning<M: Model<'ctx>>(self, name: &'static str, return_model: M) -> M::Value {
let (param_tys, param_vals): (Vec<_>, Vec<_>) = self.arguments.into_iter().unzip();
let function = self.ctx.module.get_function(self.fn_name).unwrap_or_else(|| {
let return_type = return_model.get_llvm_type(self.ctx.ctx);
let fn_type = return_type.fn_type(&param_tys, false);
self.ctx.module.add_function(self.fn_name, fn_type, None)
});
let ret = self.ctx.builder.build_call(function, &param_vals, name).unwrap();
return_model.review(self.ctx.ctx, ret.as_any_value_enum())
}
// TODO: Code duplication, but otherwise returning<S: Optic<'ctx>> cannot resolve S if return_optic = None
pub fn returning_void(self) {
let (param_tys, param_vals): (Vec<_>, Vec<_>) = self.arguments.into_iter().unzip();
let function = self.ctx.module.get_function(self.fn_name).unwrap_or_else(|| {
let return_type = self.ctx.ctx.void_type();
let fn_type = return_type.fn_type(&param_tys, false);
self.ctx.module.add_function(self.fn_name, fn_type, None)
});
self.ctx.builder.build_call(function, &param_vals, "").unwrap();
}
}

View File

@ -23,7 +23,9 @@ use inkwell::{
values::{BasicValueEnum, FunctionValue, IntValue, PhiValue, PointerValue},
AddressSpace, IntPredicate, OptimizationLevel,
};
use irrt::string::Str;
use itertools::Itertools;
use model::*;
use nac3parser::ast::{Location, Stmt, StrRef};
use parking_lot::{Condvar, Mutex};
use std::collections::{HashMap, HashSet};
@ -41,6 +43,7 @@ pub mod extern_fns;
mod generator;
pub mod irrt;
pub mod llvm_intrinsics;
pub mod model;
pub mod numpy;
pub mod stmt;
@ -654,19 +657,8 @@ pub fn gen_func_impl<
(primitives.float, context.f64_type().into()),
(primitives.bool, context.i8_type().into()),
(primitives.str, {
let name = "str";
match module.get_struct_type(name) {
None => {
let str_type = context.opaque_struct_type("str");
let fields = [
context.i8_type().ptr_type(AddressSpace::default()).into(),
generator.get_size_type(context).into(),
];
str_type.set_body(&fields, false);
str_type.into()
}
Some(t) => t.as_basic_type_enum(),
}
let sizet = IntModel(generator.get_size_type(context));
StructModel(Str { sizet }).get_llvm_type(context)
}),
(primitives.range, RangeType::new(context).as_base_type().into()),
(primitives.exception, {

View File

@ -0,0 +1,118 @@
use inkwell::{
context::Context,
types::{AnyTypeEnum, BasicTypeEnum},
values::{AnyValue, AnyValueEnum, BasicValueEnum, PointerValue},
};
use crate::codegen::{CodeGenContext, CodeGenerator};
use super::{slice::ArraySlice, Int, Pointer};
/// A value that belongs to/produced by a [`Model<'ctx>`]
pub trait ModelValue<'ctx>: Clone + Copy {
fn get_llvm_value(&self) -> BasicValueEnum<'ctx>;
}
// Should have been within [`Model<'ctx>`],
// but rust object safety requirements made it necessary to
// split this interface out
pub trait CanCheckLLVMType<'ctx> {
/// Check if `scrutinee` matches the same LLVM type of this [`Model<'ctx>`].
///
/// If they don't not match, a human-readable error message is returned.
fn check_llvm_type(
&self,
ctx: &'ctx Context,
scrutinee: AnyTypeEnum<'ctx>,
) -> Result<(), String>;
}
/// A [`Model`] is a type-safe concrete representation of a complex LLVM type.
pub trait Model<'ctx>: Clone + Copy + CanCheckLLVMType<'ctx> + Sized {
/// The values that inhabit this [`Model<'ctx>`].
///
/// ...that is the type of wrapper that wraps the LLVM values that inhabit [`Model<'ctx>::get_llvm_type()`].
type Value: ModelValue<'ctx>;
/// Get the [`BasicTypeEnum<'ctx>`] this [`Model<'ctx>`] is representing.
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx>;
/// Cast an [`AnyValueEnum<'ctx>`] into [`Self::Value`].
///
/// Panics if `value` cannot pass [`CanCheckLLVMType::check_llvm_type()`].
fn review(&self, ctx: &'ctx Context, value: AnyValueEnum<'ctx>) -> Self::Value;
/// Check if [`Self::Value`] has the correct type described by this [`Model<'ctx>`]
///
/// For example:
/// ```ignore
/// let ctx: &CodeGenContext<'ctx, '_>;
/// let my_i32 = IntModel(ctx.ctx.i32_type());
/// let my_i64 = IntModel(ctx.ctx.i64_type());
/// let value1 = my_i32.constant(3);
/// let value2 = my_i64.constant(3);
/// // Both value1 and value2 have type `IntModel<'ctx>`!
/// // There is no type constraints to tell which value has what int type.
/// my_i32.check(value1); // ok
/// my_i64.check(value2); // ok
///
/// my_i32.check(value2); // PANIC
/// my_i64.check(value1); // PANIC
/// ```
fn check(&self, ctx: &'ctx Context, value: Self::Value) {
self.review(ctx, value.get_llvm_value().as_any_value_enum());
}
/// Build an instruction to allocate a value of [`Model::get_llvm_type`].
fn alloca(&self, ctx: &CodeGenContext<'ctx, '_>, name: &str) -> Pointer<'ctx, Self> {
Pointer {
element: *self,
value: ctx.builder.build_alloca(self.get_llvm_type(ctx.ctx), name).unwrap(),
}
}
/// Build an instruction to allocate an array of [`Model::get_llvm_type`].
fn array_alloca(
&self,
ctx: &CodeGenContext<'ctx, '_>,
count: Int<'ctx>,
name: &str,
) -> ArraySlice<'ctx, Self> {
ArraySlice {
num_elements: count,
pointer: Pointer {
element: *self,
value: ctx
.builder
.build_array_alloca(self.get_llvm_type(ctx.ctx), count.0, name)
.unwrap(),
},
}
}
/// Do [`CodeGenerator::gen_var_alloc`] with the LLVM type of this [`Model<'ctx>`].
fn var_alloc<G: CodeGenerator + ?Sized>(
&self,
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
name: Option<&str>,
) -> Result<Pointer<'ctx, Self>, String> {
let value = generator.gen_var_alloc(ctx, self.get_llvm_type(ctx.ctx), name)?;
Ok(Pointer { element: *self, value })
}
/// Do [`CodeGenerator::gen_array_var_alloc`] with the LLVM type of this [`Model<'ctx>`].
fn array_var_alloc<G: CodeGenerator + ?Sized>(
&self,
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
size: Int<'ctx>,
name: Option<&'ctx str>,
) -> Result<Pointer<'ctx, Self>, String> {
let slice =
generator.gen_array_var_alloc(ctx, self.get_llvm_type(ctx.ctx), size.0, name)?;
let ptr = PointerValue::from(slice); // TODO: Remove ArraySliceValue
Ok(Pointer { element: *self, value: ptr })
}
}

View File

@ -0,0 +1,159 @@
use inkwell::{
context::Context,
types::{AnyTypeEnum, BasicType, BasicTypeEnum, IntType},
values::{AnyValueEnum, BasicValue, BasicValueEnum, IntValue},
};
use crate::codegen::CodeGenContext;
use super::{
core::*,
int_util::{check_int_llvm_type, review_int_llvm_value},
Int, IntModel,
};
/// A marker trait to mark singleton struct that describes a particular fixed integer type.
/// See [`Bool`], [`Byte`], [`Int32`], etc.
///
/// The [`Default`] trait is to enable auto-derivations for utilities like
/// [`FieldBuilder::add_field_auto`]
pub trait IsFixedInt: Clone + Copy + Default {
fn get_int_type(ctx: &Context) -> IntType<'_>;
fn get_bit_width() -> u32; // This is required, instead of only relying on get_int_type
fn constant<'ctx>(&self, ctx: &'ctx Context, value: u64) -> FixedInt<'ctx, Self> {
FixedInt { int: *self, value: Self::get_int_type(ctx).const_int(value, false) }
}
}
// Some pre-defined fixed integers
#[derive(Debug, Clone, Copy, Default)]
pub struct Bool;
pub type BoolModel = FixedIntModel<Bool>;
impl IsFixedInt for Bool {
fn get_int_type(ctx: &Context) -> IntType<'_> {
ctx.bool_type()
}
fn get_bit_width() -> u32 {
1
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Byte;
pub type ByteModel = FixedIntModel<Byte>;
impl IsFixedInt for Byte {
fn get_int_type(ctx: &Context) -> IntType<'_> {
ctx.i8_type()
}
fn get_bit_width() -> u32 {
8
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Int32;
pub type Int32Model = FixedIntModel<Int32>;
impl IsFixedInt for Int32 {
fn get_int_type(ctx: &Context) -> IntType<'_> {
ctx.i32_type()
}
fn get_bit_width() -> u32 {
32
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Int64;
pub type Int64Model = FixedIntModel<Int64>;
impl IsFixedInt for Int64 {
fn get_int_type(ctx: &Context) -> IntType<'_> {
ctx.i64_type()
}
fn get_bit_width() -> u32 {
64
}
}
/// A model representing a compile-time known [`IntType<'ctx>`].
///
/// Also see [`IntModel`], which is less constrained than [`FixedIntModel`],
/// but enables one to handle [`IntType<'ctx>`] that could be dynamic
#[derive(Debug, Clone, Copy, Default)]
pub struct FixedIntModel<T>(pub T);
// FixedIntModel's implementation
impl<'ctx, T: IsFixedInt> CanCheckLLVMType<'ctx> for FixedIntModel<T> {
fn check_llvm_type(
&self,
ctx: &'ctx Context,
scrutinee: AnyTypeEnum<'ctx>,
) -> Result<(), String> {
check_int_llvm_type(scrutinee, T::get_int_type(ctx))
}
}
impl<'ctx, T: IsFixedInt> Model<'ctx> for FixedIntModel<T> {
type Value = FixedInt<'ctx, T>;
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
T::get_int_type(ctx).as_basic_type_enum()
}
fn review(&self, ctx: &'ctx Context, value: AnyValueEnum<'ctx>) -> Self::Value {
let value = review_int_llvm_value(value, T::get_int_type(ctx)).unwrap();
FixedInt { int: self.0, value }
}
}
impl<T: IsFixedInt> FixedIntModel<T> {
pub fn to_int_model(self, ctx: &Context) -> IntModel<'_> {
IntModel(T::get_int_type(ctx))
}
}
/// An inhabitant of [`FixedIntModel<'ctx>`]
#[derive(Debug, Clone, Copy)]
pub struct FixedInt<'ctx, T: IsFixedInt> {
pub int: T,
pub value: IntValue<'ctx>,
}
// FixedInt's Implementation
impl<'ctx, T: IsFixedInt> ModelValue<'ctx> for FixedInt<'ctx, T> {
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
self.value.as_basic_value_enum()
}
}
impl<'ctx, T: IsFixedInt> FixedInt<'ctx, T> {
pub fn to_int(self) -> Int<'ctx> {
Int(self.value)
}
pub fn signed_cast_to_fixed<R: IsFixedInt>(
self,
ctx: &CodeGenContext<'ctx, '_>,
target_fixed_int: R,
name: &str,
) -> FixedInt<'ctx, R> {
FixedInt {
int: target_fixed_int,
value: ctx
.builder
.build_int_s_extend_or_bit_cast(self.value, R::get_int_type(ctx.ctx), name)
.unwrap(),
}
}
}

View File

@ -0,0 +1,55 @@
use inkwell::{
types::{BasicMetadataTypeEnum, BasicType},
values::{AnyValue, BasicMetadataValueEnum},
};
use crate::codegen::{model::*, CodeGenContext};
// TODO: Variadic argument?
pub struct FunctionBuilder<'ctx, 'a> {
ctx: &'a CodeGenContext<'ctx, 'a>,
fn_name: &'a str,
arguments: Vec<(BasicMetadataTypeEnum<'ctx>, BasicMetadataValueEnum<'ctx>)>,
}
impl<'ctx, 'a> FunctionBuilder<'ctx, 'a> {
pub fn begin(ctx: &'a CodeGenContext<'ctx, 'a>, fn_name: &'a str) -> Self {
FunctionBuilder { ctx, fn_name, arguments: Vec::new() }
}
// NOTE: `_name` is for self-documentation
#[must_use]
pub fn arg<M: Model<'ctx>>(mut self, _name: &'static str, model: M, value: M::Value) -> Self {
model.check(self.ctx.ctx, value); // Panics if the passed `value` has the incorrect type.
self.arguments
.push((model.get_llvm_type(self.ctx.ctx).into(), value.get_llvm_value().into()));
self
}
pub fn returning<M: Model<'ctx>>(self, name: &'static str, return_model: M) -> M::Value {
let (param_tys, param_vals): (Vec<_>, Vec<_>) = self.arguments.into_iter().unzip();
let function = self.ctx.module.get_function(self.fn_name).unwrap_or_else(|| {
let return_type = return_model.get_llvm_type(self.ctx.ctx);
let fn_type = return_type.fn_type(&param_tys, false);
self.ctx.module.add_function(self.fn_name, fn_type, None)
});
let ret = self.ctx.builder.build_call(function, &param_vals, name).unwrap();
return_model.review(self.ctx.ctx, ret.as_any_value_enum())
}
// TODO: Code duplication, but otherwise returning<S: Optic<'ctx>> cannot resolve S if return_optic = None
pub fn returning_void(self) {
let (param_tys, param_vals): (Vec<_>, Vec<_>) = self.arguments.into_iter().unzip();
let function = self.ctx.module.get_function(self.fn_name).unwrap_or_else(|| {
let return_type = self.ctx.ctx.void_type();
let fn_type = return_type.fn_type(&param_tys, false);
self.ctx.module.add_function(self.fn_name, fn_type, None)
});
self.ctx.builder.build_call(function, &param_vals, "").unwrap();
}
}

View File

@ -0,0 +1,97 @@
use inkwell::{
context::Context,
types::{AnyType, AnyTypeEnum, BasicType, BasicTypeEnum, IntType},
values::{AnyValueEnum, BasicValue, BasicValueEnum, IntValue},
};
use crate::codegen::CodeGenContext;
use super::{core::*, int_util::check_int_llvm_type, FixedInt, IsFixedInt};
/// A model representing an [`IntType<'ctx>`].
///
/// Also see [`FixedIntModel`], which is more constrained than [`IntModel`]
/// but provides more type-safe mechanisms and even auto-derivation of [`BasicTypeEnum<'ctx>`]
/// for creating LLVM structures.
#[derive(Debug, Clone, Copy)]
pub struct IntModel<'ctx>(pub IntType<'ctx>);
impl<'ctx> CanCheckLLVMType<'ctx> for IntModel<'ctx> {
fn check_llvm_type(
&self,
_ctx: &'ctx Context,
scrutinee: AnyTypeEnum<'ctx>,
) -> Result<(), String> {
check_int_llvm_type(scrutinee, self.0)
}
}
impl<'ctx> Model<'ctx> for IntModel<'ctx> {
type Value = Int<'ctx>;
fn get_llvm_type(&self, _ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
self.0.as_basic_type_enum()
}
fn review(&self, ctx: &'ctx Context, value: AnyValueEnum<'ctx>) -> Self::Value {
let int = value.into_int_value();
self.check_llvm_type(ctx, int.get_type().as_any_type_enum()).unwrap();
Int(int)
}
}
impl<'ctx> IntModel<'ctx> {
/// Create a constant value that inhabits this [`IntModel<'ctx>`].
#[must_use]
pub fn constant(&self, value: u64) -> Int<'ctx> {
Int(self.0.const_int(value, false))
}
/// Check if `other` is fully compatible with this [`IntModel<'ctx>`].
///
/// This simply checks if the underlying [`IntType<'ctx>`] has
/// the same number of bits.
#[must_use]
pub fn same_as(&self, other: IntModel<'ctx>) -> bool {
// TODO: or `self.0 == other.0` would also work?
self.0.get_bit_width() == other.0.get_bit_width()
}
}
/// An inhabitant of an [`IntModel<'ctx>`]
#[derive(Debug, Clone, Copy)]
pub struct Int<'ctx>(pub IntValue<'ctx>);
impl<'ctx> ModelValue<'ctx> for Int<'ctx> {
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
self.0.as_basic_value_enum()
}
}
impl<'ctx> Int<'ctx> {
#[must_use]
pub fn signed_cast_to_int(
self,
ctx: &CodeGenContext<'ctx, '_>,
target_int: IntModel<'ctx>,
name: &str,
) -> Int<'ctx> {
Int(ctx.builder.build_int_s_extend_or_bit_cast(self.0, target_int.0, name).unwrap())
}
#[must_use]
pub fn signed_cast_to_fixed<T: IsFixedInt>(
self,
ctx: &CodeGenContext<'ctx, '_>,
target_fixed: T,
name: &str,
) -> FixedInt<'ctx, T> {
FixedInt {
int: target_fixed,
value: ctx
.builder
.build_int_s_extend_or_bit_cast(self.0, T::get_int_type(ctx.ctx), name)
.unwrap(),
}
}
}

View File

@ -0,0 +1,39 @@
use inkwell::{
types::{AnyType, AnyTypeEnum, IntType},
values::{AnyValueEnum, IntValue},
};
/// Helper function to check if `scrutinee` is the same as `expected_int_type`
pub fn check_int_llvm_type<'ctx>(
scrutinee: AnyTypeEnum<'ctx>,
expected_int_type: IntType<'ctx>,
) -> Result<(), String> {
// Check if llvm_type is int type
let AnyTypeEnum::IntType(scrutinee) = scrutinee else {
return Err(format!("Expecting an int type but got {scrutinee:?}"));
};
// Check bit width
if scrutinee.get_bit_width() != expected_int_type.get_bit_width() {
return Err(format!(
"Expecting an int type of {}-bit(s) but got int type {}-bit(s)",
expected_int_type.get_bit_width(),
scrutinee.get_bit_width()
));
}
Ok(())
}
/// Helper function to cast `scrutinee` is into an [`IntValue<'ctx>`].
/// The LLVM type of `scrutinee` will be checked with [`check_int_llvm_type`].
pub fn review_int_llvm_value<'ctx>(
value: AnyValueEnum<'ctx>,
expected_int_type: IntType<'ctx>,
) -> Result<IntValue<'ctx>, String> {
// Check if value is of int type, error if that is anything else
check_int_llvm_type(value.get_type().as_any_type_enum(), expected_int_type)?;
// Ok, it is must be an int
Ok(value.into_int_value())
}

View File

@ -0,0 +1,18 @@
pub mod core;
pub mod fixed_int;
pub mod function_builder;
pub mod int;
mod int_util;
pub mod opaque;
pub mod pointer;
pub mod slice;
pub mod structure;
pub use core::*;
pub use fixed_int::*;
pub use function_builder::*;
pub use int::*;
pub use opaque::*;
pub use pointer::*;
pub use slice::*;
pub use structure::*;

View File

@ -0,0 +1,46 @@
use inkwell::{
context::Context,
types::{AnyTypeEnum, BasicTypeEnum},
values::{AnyValueEnum, BasicValueEnum},
};
use super::*;
#[derive(Debug, Clone, Copy)]
pub struct OpaqueModel<'ctx>(pub BasicTypeEnum<'ctx>);
impl<'ctx> CanCheckLLVMType<'ctx> for OpaqueModel<'ctx> {
fn check_llvm_type(
&self,
_ctx: &'ctx Context,
scrutinee: AnyTypeEnum<'ctx>,
) -> Result<(), String> {
match BasicTypeEnum::try_from(scrutinee) {
Ok(_) => Ok(()),
Err(_err) => Err(format!("Expecting a BasicTypeEnum, but got {scrutinee:?}")),
}
}
}
impl<'ctx> Model<'ctx> for OpaqueModel<'ctx> {
type Value = Opaque<'ctx>;
fn get_llvm_type(&self, _ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
self.0
}
fn review(&self, ctx: &'ctx Context, value: AnyValueEnum<'ctx>) -> Self::Value {
self.check_llvm_type(ctx, value.get_type()).unwrap();
let value = BasicValueEnum::try_from(value).unwrap(); // Must work
Opaque(value)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Opaque<'ctx>(pub BasicValueEnum<'ctx>);
impl<'ctx> ModelValue<'ctx> for Opaque<'ctx> {
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
self.0
}
}

View File

@ -0,0 +1,83 @@
use inkwell::{
context::Context,
types::{AnyTypeEnum, BasicType, BasicTypeEnum},
values::{AnyValue, AnyValueEnum, BasicValue, BasicValueEnum, PointerValue},
AddressSpace,
};
use crate::codegen::CodeGenContext;
use super::{core::*, OpaqueModel};
/// A [`Model<'ctx>`] representing an LLVM [`PointerType<'ctx>`]
/// with *full* information on the element u
///
/// [`self.0`] contains [`Model<'ctx>`] that represents the
/// LLVM type of element of the [`PointerType<'ctx>`] is pointing at
/// (like `PointerType<'ctx>::get_element_type()`, but abstracted as a [`Model<'ctx>`]).
#[derive(Debug, Clone, Copy, Default)]
pub struct PointerModel<E>(pub E);
impl<'ctx, E: Model<'ctx>> CanCheckLLVMType<'ctx> for PointerModel<E> {
fn check_llvm_type(
&self,
ctx: &'ctx Context,
scrutinee: AnyTypeEnum<'ctx>,
) -> Result<(), String> {
// Check if scrutinee is even a PointerValue
let AnyTypeEnum::PointerType(scrutinee) = scrutinee else {
return Err(format!("Expecting a pointer value, but got {scrutinee:?}"));
};
// Check the type of what the pointer is pointing at
// TODO: This will be deprecated by inkwell > llvm14 because `get_element_type()` will be gone
self.0.check_llvm_type(ctx, scrutinee.get_element_type())?; // TODO: Include backtrace?
Ok(())
}
}
impl<'ctx, E: Model<'ctx>> Model<'ctx> for PointerModel<E> {
type Value = Pointer<'ctx, E>;
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
self.0.get_llvm_type(ctx).ptr_type(AddressSpace::default()).as_basic_type_enum()
}
fn review(&self, ctx: &'ctx Context, value: AnyValueEnum<'ctx>) -> Self::Value {
self.check_llvm_type(ctx, value.get_type()).unwrap();
// TODO: Check get_element_type(). For inkwell LLVM 14 at least...
Pointer { element: self.0, value: value.into_pointer_value() }
}
}
/// An inhabitant of [`PointerModel<E>`]
#[derive(Debug, Clone, Copy)]
pub struct Pointer<'ctx, E: Model<'ctx>> {
pub element: E,
pub value: PointerValue<'ctx>,
}
impl<'ctx, E: Model<'ctx>> ModelValue<'ctx> for Pointer<'ctx, E> {
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
self.value.as_basic_value_enum()
}
}
impl<'ctx, E: Model<'ctx>> Pointer<'ctx, E> {
/// Build an instruction to store a value into this pointer
pub fn store(&self, ctx: &CodeGenContext<'ctx, '_>, val: E::Value) {
ctx.builder.build_store(self.value, val.get_llvm_value()).unwrap();
}
/// Build an instruction to load a value from this pointer
pub fn load(&self, ctx: &CodeGenContext<'ctx, '_>, name: &str) -> E::Value {
let val = ctx.builder.build_load(self.value, name).unwrap();
self.element.review(ctx.ctx, val.as_any_value_enum())
}
pub fn to_opaque(self, ctx: &'ctx Context) -> Pointer<'ctx, OpaqueModel<'ctx>> {
Pointer { element: OpaqueModel(self.element.get_llvm_type(ctx)), value: self.value }
}
}

View File

@ -0,0 +1,73 @@
use crate::codegen::{CodeGenContext, CodeGenerator};
use super::{Int, Model, Pointer};
pub struct ArraySlice<'ctx, E: Model<'ctx>> {
pub num_elements: Int<'ctx>,
pub pointer: Pointer<'ctx, E>,
}
impl<'ctx, E: Model<'ctx>> ArraySlice<'ctx, E> {
pub fn ix_unchecked(
&self,
ctx: &CodeGenContext<'ctx, '_>,
idx: Int<'ctx>,
name: &str,
) -> Pointer<'ctx, E> {
let element_addr =
unsafe { ctx.builder.build_in_bounds_gep(self.pointer.value, &[idx.0], name).unwrap() };
Pointer { value: element_addr, element: self.pointer.element }
}
pub fn ix<G: CodeGenerator + ?Sized>(
&self,
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
idx: Int<'ctx>,
name: &str,
) -> Pointer<'ctx, E> {
let int_type = self.num_elements.0.get_type(); // NOTE: Weird get_type(), see comment under `trait Ixed`
assert_eq!(int_type.get_bit_width(), idx.0.get_type().get_bit_width()); // Might as well check bit width to catch bugs
// TODO: SGE or UGE? or make it defined by the implementee?
// Check `0 <= index`
let lower_bounded = ctx
.builder
.build_int_compare(
inkwell::IntPredicate::SLE,
int_type.const_zero(),
idx.0,
"lower_bounded",
)
.unwrap();
// Check `index < num_elements`
let upper_bounded = ctx
.builder
.build_int_compare(
inkwell::IntPredicate::SLT,
idx.0,
self.num_elements.0,
"upper_bounded",
)
.unwrap();
// Compute `0 <= index && index < num_elements`
let bounded = ctx.builder.build_and(lower_bounded, upper_bounded, "bounded").unwrap();
// Assert `bounded`
ctx.make_assert(
generator,
bounded,
"0:IndexError",
"nac3core LLVM codegen attempting to access out of bounds array index {0}. Must satisfy 0 <= index < {2}",
[ Some(idx.0), Some(self.num_elements.0), None],
ctx.current_loc
);
// ...and finally do indexing
self.ix_unchecked(ctx, idx, name)
}
}

View File

@ -0,0 +1,219 @@
use inkwell::{
context::Context,
types::{AnyType, AnyTypeEnum, BasicType, BasicTypeEnum, StructType},
values::{AnyValueEnum, BasicValue, BasicValueEnum, StructValue},
};
use itertools::{izip, Itertools};
use crate::codegen::CodeGenContext;
use super::{core::CanCheckLLVMType, Model, ModelValue, Pointer};
#[derive(Debug, Clone, Copy)]
pub struct Field<E> {
pub gep_index: u64,
pub name: &'static str,
pub element: E,
}
struct FieldLLVM<'ctx> {
gep_index: u64,
name: &'ctx str,
llvm_type: BasicTypeEnum<'ctx>,
// Only CanCheckLLVMType is needed, dont use `Model<'ctx>`
llvm_type_model: Box<dyn CanCheckLLVMType<'ctx> + 'ctx>,
}
pub struct FieldBuilder<'ctx> {
pub ctx: &'ctx Context,
gep_index_counter: u64,
struct_name: &'ctx str,
fields: Vec<FieldLLVM<'ctx>>,
}
impl<'ctx> FieldBuilder<'ctx> {
#[must_use]
pub fn new(ctx: &'ctx Context, struct_name: &'ctx str) -> Self {
FieldBuilder { ctx, gep_index_counter: 0, struct_name, fields: Vec::new() }
}
fn next_gep_index(&mut self) -> u64 {
let index = self.gep_index_counter;
self.gep_index_counter += 1;
index
}
pub fn add_field<E: Model<'ctx> + 'ctx>(&mut self, name: &'static str, element: E) -> Field<E> {
let gep_index = self.next_gep_index();
self.fields.push(FieldLLVM {
gep_index,
name,
llvm_type: element.get_llvm_type(self.ctx),
llvm_type_model: Box::new(element),
});
Field { gep_index, name, element }
}
pub fn add_field_auto<E: Model<'ctx> + Default + 'ctx>(
&mut self,
name: &'static str,
) -> Field<E> {
self.add_field(name, E::default())
}
}
/// A marker trait to mark singleton struct that describes a particular LLVM structure.
pub trait IsStruct<'ctx>: Clone + Copy {
/// The type of the Rust `struct` that holds all the fields of this LLVM struct.
type Fields;
/// A cosmetic name for this struct.
/// TODO: Currently unused. To be used in error reporting.
fn struct_name(&self) -> &'static str;
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields;
fn get_fields(&self, ctx: &'ctx Context) -> Self::Fields {
let mut builder = FieldBuilder::new(ctx, self.struct_name());
self.build_fields(&mut builder)
}
/// Get the LLVM struct type this [`IsStruct<'ctx>`] is representing.
fn get_struct_type(&self, ctx: &'ctx Context) -> StructType<'ctx> {
let mut builder = FieldBuilder::new(ctx, self.struct_name());
self.build_fields(&mut builder); // Self::Fields is discarded
let field_types = builder.fields.iter().map(|f| f.llvm_type).collect_vec();
ctx.struct_type(&field_types, false)
}
/// Check if `scrutinee` matches the [`StructType<'ctx>`] this [`IsStruct<'ctx>`] is representing.
fn check_struct_type(
&self,
ctx: &'ctx Context,
scrutinee: StructType<'ctx>,
) -> Result<(), String> {
// Details about scrutinee
let scrutinee_field_types = scrutinee.get_field_types();
// Details about the defined specifications of this struct
// We will access them through builder
let mut builder = FieldBuilder::new(ctx, self.struct_name());
self.build_fields(&mut builder);
// Check # of fields
if builder.fields.len() != scrutinee_field_types.len() {
return Err(format!(
"Expecting struct to have {} field(s), but scrutinee has {} field(s)",
builder.fields.len(),
scrutinee_field_types.len()
));
}
// Check the types of each field
// TODO: Traceback?
for (f, scrutinee_field_type) in izip!(builder.fields, scrutinee_field_types) {
f.llvm_type_model.check_llvm_type(ctx, scrutinee_field_type.as_any_type_enum())?;
}
Ok(())
}
}
/// A [`Model<'ctx>`] that represents an LLVM struct.
///
/// `self.0` contains a [`IsStruct<'ctx>`] that gives the details of the LLVM struct.
#[derive(Debug, Clone, Copy, Default)]
pub struct StructModel<S>(pub S);
impl<'ctx, S: IsStruct<'ctx>> CanCheckLLVMType<'ctx> for StructModel<S> {
fn check_llvm_type(
&self,
ctx: &'ctx Context,
scrutinee: AnyTypeEnum<'ctx>,
) -> Result<(), String> {
// Check if scrutinee is even a struct type
let AnyTypeEnum::StructType(scrutinee) = scrutinee else {
return Err(format!("Expecting a struct type, but got {scrutinee:?}"));
};
// Ok. now check the struct type *thoroughly*
self.0.check_struct_type(ctx, scrutinee)
}
}
impl<'ctx, S: IsStruct<'ctx>> Model<'ctx> for StructModel<S> {
type Value = Struct<'ctx, S>;
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
self.0.get_struct_type(ctx).as_basic_type_enum()
}
fn review(&self, ctx: &'ctx Context, value: AnyValueEnum<'ctx>) -> Self::Value {
// Check that `value` is not some bogus values or an incorrect StructValue
self.check_llvm_type(ctx, value.get_type()).unwrap();
Struct { structure: self.0, value: value.into_struct_value() }
}
}
#[derive(Debug, Clone, Copy)]
pub struct Struct<'ctx, S> {
pub structure: S,
pub value: StructValue<'ctx>,
}
impl<'ctx, S: IsStruct<'ctx>> ModelValue<'ctx> for Struct<'ctx, S> {
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
self.value.as_basic_value_enum()
}
}
impl<'ctx, S: IsStruct<'ctx>> Pointer<'ctx, StructModel<S>> {
/// Build an instruction that does `getelementptr` on an LLVM structure referenced by this pointer.
///
/// This provides a nice syntax to chain up `getelementptr` in an intuitive and type-safe way:
///
/// ```ignore
/// let ctx: &CodeGenContext<'ctx, '_>;
/// let ndarray: Pointer<'ctx, StructModel<NpArray<'ctx>>>;
/// ndarray.gep(ctx, |f| f.ndims).store();
/// ```
///
/// You might even write chains `gep`, i.e.,
/// ```ignore
/// my_struct
/// .gep(ctx, |f| f.thing1)
/// .gep(ctx, |f| f.value)
/// .store(ctx, my_value) // Equivalent to `my_struct.thing1.value = my_value`
/// ```
pub fn gep<E, GetFieldFn>(
&self,
ctx: &CodeGenContext<'ctx, '_>,
get_field: GetFieldFn,
) -> Pointer<'ctx, E>
where
E: Model<'ctx>,
GetFieldFn: FnOnce(S::Fields) -> Field<E>,
{
let fields = self.element.0.get_fields(ctx.ctx);
let field = get_field(fields);
let llvm_i32 = ctx.ctx.i32_type(); // TODO: I think I'm not supposed to *just* use i32 for GEP like that
let ptr = unsafe {
ctx.builder
.build_in_bounds_gep(
self.value,
&[llvm_i32.const_zero(), llvm_i32.const_int(field.gep_index, false)],
field.name,
)
.unwrap()
};
Pointer { element: field.element, value: ptr }
}
}

View File

@ -9,7 +9,7 @@ use crate::{
irrt::{
calculate_len_for_slice_range, call_ndarray_calc_broadcast,
call_ndarray_calc_broadcast_index, call_ndarray_calc_nd_indices,
call_ndarray_calc_size,
call_ndarray_calc_size, error_context::call_nac3_dummy_raise,
},
llvm_intrinsics::{self, call_memcpy_generic},
stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback},
@ -1742,6 +1742,8 @@ pub fn gen_ndarray_zeros<'ctx>(
assert!(obj.is_none());
assert_eq!(args.len(), 1);
call_nac3_dummy_raise(generator, context);
let shape_ty = fun.0.args[0].ty;
let shape_arg = args[0].1.clone().to_basic_value_enum(context, generator, shape_ty)?;

View File

@ -23,3 +23,4 @@ pub mod codegen;
pub mod symbol_resolver;
pub mod toplevel;
pub mod typecheck;
pub(crate) mod util;

View File

@ -23,6 +23,7 @@ use crate::{
symbol_resolver::SymbolValue,
toplevel::{helper::PrimDef, numpy::make_ndarray_ty},
typecheck::typedef::{into_var_map, iter_type_vars, TypeVar, VarMap},
util::SizeVariant,
};
use super::*;
@ -278,20 +279,11 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
.collect()
}
/// A helper enum used by [`BuiltinBuilder`]
#[derive(Clone, Copy)]
enum SizeVariant {
Bits32,
Bits64,
}
impl SizeVariant {
fn of_int(self, primitives: &PrimitiveStore) -> Type {
match self {
fn get_size_variant_of_int(size_variant: SizeVariant, primitives: &PrimitiveStore) -> Type {
match size_variant {
SizeVariant::Bits32 => primitives.int32,
SizeVariant::Bits64 => primitives.int64,
}
}
}
struct BuiltinBuilder<'a> {
@ -1061,7 +1053,7 @@ impl<'a> BuiltinBuilder<'a> {
);
// The size variant of the function determines the size of the returned int.
let int_sized = size_variant.of_int(self.primitives);
let int_sized = get_size_variant_of_int(size_variant, self.primitives);
let ndarray_int_sized =
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
@ -1086,7 +1078,7 @@ impl<'a> BuiltinBuilder<'a> {
let arg_ty = fun.0.args[0].ty;
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
let ret_elem_ty = size_variant.of_int(&ctx.primitives);
let ret_elem_ty = get_size_variant_of_int(size_variant, &ctx.primitives);
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ret_elem_ty)?))
}),
)
@ -1127,7 +1119,7 @@ impl<'a> BuiltinBuilder<'a> {
make_ndarray_ty(self.unifier, self.primitives, Some(float), Some(common_ndim.ty));
// The size variant of the function determines the type of int returned
let int_sized = size_variant.of_int(self.primitives);
let int_sized = get_size_variant_of_int(size_variant, self.primitives);
let ndarray_int_sized =
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
@ -1150,7 +1142,7 @@ impl<'a> BuiltinBuilder<'a> {
let arg_ty = fun.0.args[0].ty;
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
let ret_elem_ty = size_variant.of_int(&ctx.primitives);
let ret_elem_ty = get_size_variant_of_int(size_variant, &ctx.primitives);
let func = match kind {
Kind::Ceil => builtin_fns::call_ceil,
Kind::Floor => builtin_fns::call_floor,

6
nac3core/src/util.rs Normal file
View File

@ -0,0 +1,6 @@
/// A helper enum used by [`BuiltinBuilder`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SizeVariant {
Bits32,
Bits64,
}