From 7436513b6449e8bce78e2c3d5b90c1bc30d2f97e Mon Sep 17 00:00:00 2001 From: lyken Date: Tue, 30 Jul 2024 15:19:45 +0800 Subject: [PATCH] core/model: add util.rs & gen_model_memcpy --- nac3core/src/codegen/model/mod.rs | 1 + nac3core/src/codegen/model/util.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 nac3core/src/codegen/model/util.rs diff --git a/nac3core/src/codegen/model/mod.rs b/nac3core/src/codegen/model/mod.rs index 78da8048..e7b5d9fe 100644 --- a/nac3core/src/codegen/model/mod.rs +++ b/nac3core/src/codegen/model/mod.rs @@ -3,6 +3,7 @@ mod int; mod ptr; mod slice; mod structure; +pub mod util; pub use core::*; pub use int::*; diff --git a/nac3core/src/codegen/model/util.rs b/nac3core/src/codegen/model/util.rs new file mode 100644 index 00000000..5306c0fd --- /dev/null +++ b/nac3core/src/codegen/model/util.rs @@ -0,0 +1,23 @@ +use inkwell::{types::BasicType, values::IntValue}; + +use crate::codegen::{llvm_intrinsics::call_memcpy_generic, CodeGenContext}; + +use super::*; + +pub fn gen_model_memcpy<'ctx, M: Model>( + tyctx: TypeContext<'ctx>, + ctx: &CodeGenContext<'ctx, '_>, + dst: Ptr<'ctx, M>, + src: Ptr<'ctx, M>, + num_elements: IntValue<'ctx>, + volatile: bool, +) { + let bool_model = IntModel(Bool); + + let itemsize = M::default().get_type(tyctx, ctx.ctx).size_of().unwrap(); + let totalsize = + ctx.builder.build_int_mul(itemsize, num_elements, "model_memcpy_totalsize").unwrap(); + let is_volatile = bool_model.constant(tyctx, ctx.ctx, u64::from(volatile)); + + call_memcpy_generic(ctx, dst.value, src.value, totalsize, is_volatile.value); +}