2024-06-12 14:45:03 +08:00
|
|
|
use inkwell::{
|
|
|
|
values::{BasicValueEnum, CallSiteValue},
|
|
|
|
AddressSpace, AtomicOrdering,
|
|
|
|
};
|
2024-02-19 19:30:25 +08:00
|
|
|
use itertools::Either;
|
2022-02-21 18:27:46 +08:00
|
|
|
use nac3core::codegen::CodeGenContext;
|
2021-10-08 23:21:36 +08:00
|
|
|
|
2023-10-23 13:35:29 +08:00
|
|
|
/// Functions for manipulating the timeline.
|
2021-10-08 23:21:36 +08:00
|
|
|
pub trait TimeFns {
|
2023-10-23 13:35:29 +08:00
|
|
|
/// Emits LLVM IR for `now_mu`.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_now_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>) -> BasicValueEnum<'ctx>;
|
2023-10-23 13:35:29 +08:00
|
|
|
|
|
|
|
/// Emits LLVM IR for `at_mu`.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_at_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, t: BasicValueEnum<'ctx>);
|
2023-10-23 13:35:29 +08:00
|
|
|
|
|
|
|
/// Emits LLVM IR for `delay_mu`.
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_delay_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, dt: BasicValueEnum<'ctx>);
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:10:55 +08:00
|
|
|
pub struct NowPinningTimeFns64 {}
|
|
|
|
|
|
|
|
// For FPGA design reasons, on VexRiscv with 64-bit data bus, the "now" CSR is split into two 32-bit
|
|
|
|
// values that are each padded to 64-bits.
|
|
|
|
impl TimeFns for NowPinningTimeFns64 {
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_now_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>) -> BasicValueEnum<'ctx> {
|
2021-11-16 17:37:40 +08:00
|
|
|
let i64_type = ctx.ctx.i64_type();
|
|
|
|
let i32_type = ctx.ctx.i32_type();
|
|
|
|
let now = ctx
|
|
|
|
.module
|
|
|
|
.get_global("now")
|
|
|
|
.unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now"));
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hiptr = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr")
|
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let now_loptr = unsafe {
|
|
|
|
ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now.lo.addr")
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
|
|
|
.unwrap();
|
2024-02-19 19:30:25 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hi = ctx
|
|
|
|
.builder
|
|
|
|
.build_load(now_hiptr, "now.hi")
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_lo = ctx
|
|
|
|
.builder
|
|
|
|
.build_load(now_loptr, "now.lo")
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let zext_hi = ctx.builder.build_int_z_extend(now_hi, i64_type, "").unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let shifted_hi =
|
|
|
|
ctx.builder.build_left_shift(zext_hi, i64_type.const_int(32, false), "").unwrap();
|
2024-02-19 19:30:25 +08:00
|
|
|
let zext_lo = ctx.builder.build_int_z_extend(now_lo, i64_type, "").unwrap();
|
|
|
|
ctx.builder.build_or(shifted_hi, zext_lo, "now_mu").map(Into::into).unwrap()
|
2021-11-13 12:10:55 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_at_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, t: BasicValueEnum<'ctx>) {
|
2021-11-16 17:37:40 +08:00
|
|
|
let i32_type = ctx.ctx.i32_type();
|
|
|
|
let i64_type = ctx.ctx.i64_type();
|
2023-10-23 13:35:29 +08:00
|
|
|
|
2021-11-16 17:37:40 +08:00
|
|
|
let i64_32 = i64_type.const_int(32, false);
|
2024-02-19 19:30:25 +08:00
|
|
|
let time = t.into_int_value();
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
let time_hi = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_int_truncate(
|
|
|
|
ctx.builder.build_right_shift(time, i64_32, false, "time.hi").unwrap(),
|
|
|
|
i32_type,
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let time_lo = ctx.builder.build_int_truncate(time, i32_type, "time.lo").unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
let now = ctx
|
|
|
|
.module
|
|
|
|
.get_global("now")
|
|
|
|
.unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now"));
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hiptr = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr")
|
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let now_loptr = unsafe {
|
|
|
|
ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now.lo.addr")
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_store(now_hiptr, time_hi)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
|
|
|
ctx.builder
|
|
|
|
.build_store(now_loptr, time_lo)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
2021-11-13 12:10:55 +08:00
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
fn emit_delay_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, dt: BasicValueEnum<'ctx>) {
|
2021-11-16 17:37:40 +08:00
|
|
|
let i64_type = ctx.ctx.i64_type();
|
|
|
|
let i32_type = ctx.ctx.i32_type();
|
|
|
|
let now = ctx
|
|
|
|
.module
|
|
|
|
.get_global("now")
|
|
|
|
.unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now"));
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hiptr = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr")
|
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let now_loptr = unsafe {
|
|
|
|
ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(2, false)], "now.lo.addr")
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
|
|
|
.unwrap();
|
2024-02-19 19:30:25 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hi = ctx
|
|
|
|
.builder
|
|
|
|
.build_load(now_hiptr, "now.hi")
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_lo = ctx
|
|
|
|
.builder
|
|
|
|
.build_load(now_loptr, "now.lo")
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap();
|
|
|
|
let dt = dt.into_int_value();
|
|
|
|
|
|
|
|
let zext_hi = ctx.builder.build_int_z_extend(now_hi, i64_type, "").unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let shifted_hi =
|
|
|
|
ctx.builder.build_left_shift(zext_hi, i64_type.const_int(32, false), "").unwrap();
|
2024-02-19 19:30:25 +08:00
|
|
|
let zext_lo = ctx.builder.build_int_z_extend(now_lo, i64_type, "").unwrap();
|
|
|
|
let now_val = ctx.builder.build_or(shifted_hi, zext_lo, "now").unwrap();
|
|
|
|
|
|
|
|
let time = ctx.builder.build_int_add(now_val, dt, "time").unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let time_hi = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_int_truncate(
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_right_shift(time, i64_type.const_int(32, false), false, "")
|
|
|
|
.unwrap(),
|
2024-02-19 19:30:25 +08:00
|
|
|
i32_type,
|
|
|
|
"time.hi",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let time_lo = ctx.builder.build_int_truncate(time, i32_type, "time.lo").unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
ctx.builder
|
|
|
|
.build_store(now_hiptr, time_hi)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
|
|
|
ctx.builder
|
|
|
|
.build_store(now_loptr, time_lo)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
2021-11-13 12:10:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub static NOW_PINNING_TIME_FNS_64: NowPinningTimeFns64 = NowPinningTimeFns64 {};
|
|
|
|
|
2021-10-08 23:21:36 +08:00
|
|
|
pub struct NowPinningTimeFns {}
|
|
|
|
|
|
|
|
impl TimeFns for NowPinningTimeFns {
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_now_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>) -> BasicValueEnum<'ctx> {
|
2021-10-08 23:21:36 +08:00
|
|
|
let i64_type = ctx.ctx.i64_type();
|
|
|
|
let now = ctx
|
|
|
|
.module
|
|
|
|
.get_global("now")
|
|
|
|
.unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now"));
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_raw = ctx
|
|
|
|
.builder
|
|
|
|
.build_load(now.as_pointer_value(), "now")
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let i64_32 = i64_type.const_int(32, false);
|
2024-02-19 19:30:25 +08:00
|
|
|
let now_lo = ctx.builder.build_left_shift(now_raw, i64_32, "now.lo").unwrap();
|
|
|
|
let now_hi = ctx.builder.build_right_shift(now_raw, i64_32, false, "now.hi").unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder.build_or(now_lo, now_hi, "now_mu").map(Into::into).unwrap()
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_at_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, t: BasicValueEnum<'ctx>) {
|
2021-10-08 23:21:36 +08:00
|
|
|
let i32_type = ctx.ctx.i32_type();
|
|
|
|
let i64_type = ctx.ctx.i64_type();
|
2021-11-06 23:00:18 +08:00
|
|
|
let i64_32 = i64_type.const_int(32, false);
|
2023-10-23 13:35:29 +08:00
|
|
|
|
2024-02-19 19:30:25 +08:00
|
|
|
let time = t.into_int_value();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
let time_hi = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_int_truncate(
|
|
|
|
ctx.builder.build_right_shift(time, i64_32, false, "").unwrap(),
|
|
|
|
i32_type,
|
|
|
|
"time.hi",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let time_lo = ctx.builder.build_int_truncate(time, i32_type, "now_trunc").unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
let now = ctx
|
|
|
|
.module
|
|
|
|
.get_global("now")
|
|
|
|
.unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now"));
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hiptr = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr")
|
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let now_loptr = unsafe {
|
|
|
|
ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(1, false)], "now.lo.addr")
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_store(now_hiptr, time_hi)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
|
|
|
ctx.builder
|
|
|
|
.build_store(now_loptr, time_lo)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
fn emit_delay_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, dt: BasicValueEnum<'ctx>) {
|
2021-10-08 23:21:36 +08:00
|
|
|
let i32_type = ctx.ctx.i32_type();
|
|
|
|
let i64_type = ctx.ctx.i64_type();
|
2021-11-06 23:00:18 +08:00
|
|
|
let i64_32 = i64_type.const_int(32, false);
|
2021-10-08 23:21:36 +08:00
|
|
|
let now = ctx
|
|
|
|
.module
|
|
|
|
.get_global("now")
|
|
|
|
.unwrap_or_else(|| ctx.module.add_global(i64_type, None, "now"));
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_raw = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_load(now.as_pointer_value(), "")
|
|
|
|
.map(BasicValueEnum::into_int_value)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let dt = dt.into_int_value();
|
|
|
|
|
|
|
|
let now_lo = ctx.builder.build_left_shift(now_raw, i64_32, "now.lo").unwrap();
|
|
|
|
let now_hi = ctx.builder.build_right_shift(now_raw, i64_32, false, "now.hi").unwrap();
|
|
|
|
let now_val = ctx.builder.build_or(now_lo, now_hi, "now_val").unwrap();
|
|
|
|
let time = ctx.builder.build_int_add(now_val, dt, "time").unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let time_hi = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_int_truncate(
|
|
|
|
ctx.builder.build_right_shift(time, i64_32, false, "time.hi").unwrap(),
|
|
|
|
i32_type,
|
|
|
|
"now_trunc",
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let time_lo = ctx.builder.build_int_truncate(time, i32_type, "time.lo").unwrap();
|
2024-06-12 14:45:03 +08:00
|
|
|
let now_hiptr = ctx
|
|
|
|
.builder
|
2024-02-19 19:30:25 +08:00
|
|
|
.build_bitcast(now, i32_type.ptr_type(AddressSpace::default()), "now.hi.addr")
|
|
|
|
.map(BasicValueEnum::into_pointer_value)
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
|
|
|
|
let now_loptr = unsafe {
|
|
|
|
ctx.builder.build_gep(now_hiptr, &[i32_type.const_int(1, false)], "now.lo.addr")
|
2024-06-12 14:45:03 +08:00
|
|
|
}
|
|
|
|
.unwrap();
|
2023-12-12 13:38:27 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_store(now_hiptr, time_hi)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
|
|
|
ctx.builder
|
|
|
|
.build_store(now_loptr, time_lo)
|
2024-02-19 19:30:25 +08:00
|
|
|
.unwrap()
|
2023-12-12 13:38:27 +08:00
|
|
|
.set_atomic_ordering(AtomicOrdering::SequentiallyConsistent)
|
|
|
|
.unwrap();
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub static NOW_PINNING_TIME_FNS: NowPinningTimeFns = NowPinningTimeFns {};
|
|
|
|
|
|
|
|
pub struct ExternTimeFns {}
|
|
|
|
|
|
|
|
impl TimeFns for ExternTimeFns {
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_now_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>) -> BasicValueEnum<'ctx> {
|
2022-02-21 18:27:46 +08:00
|
|
|
let now_mu = ctx.module.get_function("now_mu").unwrap_or_else(|| {
|
|
|
|
ctx.module.add_function("now_mu", ctx.ctx.i64_type().fn_type(&[], false), None)
|
|
|
|
});
|
2024-06-12 14:45:03 +08:00
|
|
|
ctx.builder
|
|
|
|
.build_call(now_mu, &[], "now_mu")
|
2024-02-19 19:30:25 +08:00
|
|
|
.map(CallSiteValue::try_as_basic_value)
|
|
|
|
.map(Either::unwrap_left)
|
|
|
|
.unwrap()
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 11:49:02 +08:00
|
|
|
fn emit_at_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, t: BasicValueEnum<'ctx>) {
|
2022-02-21 18:27:46 +08:00
|
|
|
let at_mu = ctx.module.get_function("at_mu").unwrap_or_else(|| {
|
|
|
|
ctx.module.add_function(
|
|
|
|
"at_mu",
|
|
|
|
ctx.ctx.void_type().fn_type(&[ctx.ctx.i64_type().into()], false),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
});
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_call(at_mu, &[t.into()], "at_mu").unwrap();
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
|
2024-06-12 14:45:03 +08:00
|
|
|
fn emit_delay_mu<'ctx>(&self, ctx: &mut CodeGenContext<'ctx, '_>, dt: BasicValueEnum<'ctx>) {
|
2022-02-21 18:27:46 +08:00
|
|
|
let delay_mu = ctx.module.get_function("delay_mu").unwrap_or_else(|| {
|
|
|
|
ctx.module.add_function(
|
|
|
|
"delay_mu",
|
|
|
|
ctx.ctx.void_type().fn_type(&[ctx.ctx.i64_type().into()], false),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
});
|
2024-02-19 19:30:25 +08:00
|
|
|
ctx.builder.build_call(delay_mu, &[dt.into()], "delay_mu").unwrap();
|
2021-10-08 23:21:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub static EXTERN_TIME_FNS: ExternTimeFns = ExternTimeFns {};
|