From 3ff6db1a29ade9e0f0af828ee3c07ae64d41c64d Mon Sep 17 00:00:00 2001 From: David Mak Date: Wed, 31 Jul 2024 15:32:12 +0800 Subject: [PATCH] core/codegen: Add va_start and va_end intrinsics --- nac3core/src/codegen/llvm_intrinsics.rs | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/nac3core/src/codegen/llvm_intrinsics.rs b/nac3core/src/codegen/llvm_intrinsics.rs index 58dc3487..6e878715 100644 --- a/nac3core/src/codegen/llvm_intrinsics.rs +++ b/nac3core/src/codegen/llvm_intrinsics.rs @@ -35,6 +35,40 @@ fn get_float_intrinsic_repr(ctx: &Context, ft: FloatType) -> &'static str { unreachable!() } +/// Invokes the [`llvm.va_start`](https://llvm.org/docs/LangRef.html#llvm-va-start-intrinsic) +/// intrinsic. +pub fn call_va_start<'ctx>(ctx: &CodeGenContext<'ctx, '_>, arglist: PointerValue<'ctx>) { + const FN_NAME: &str = "llvm.va_start"; + + let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| { + let llvm_void = ctx.ctx.void_type(); + let llvm_i8 = ctx.ctx.i8_type(); + let llvm_p0i8 = llvm_i8.ptr_type(AddressSpace::default()); + let fn_type = llvm_void.fn_type(&[llvm_p0i8.into()], false); + + ctx.module.add_function(FN_NAME, fn_type, None) + }); + + ctx.builder.build_call(intrinsic_fn, &[arglist.into()], "").unwrap(); +} + +/// Invokes the [`llvm.va_start`](https://llvm.org/docs/LangRef.html#llvm-va-start-intrinsic) +/// intrinsic. +pub fn call_va_end<'ctx>(ctx: &CodeGenContext<'ctx, '_>, arglist: PointerValue<'ctx>) { + const FN_NAME: &str = "llvm.va_end"; + + let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| { + let llvm_void = ctx.ctx.void_type(); + let llvm_i8 = ctx.ctx.i8_type(); + let llvm_p0i8 = llvm_i8.ptr_type(AddressSpace::default()); + let fn_type = llvm_void.fn_type(&[llvm_p0i8.into()], false); + + ctx.module.add_function(FN_NAME, fn_type, None) + }); + + ctx.builder.build_call(intrinsic_fn, &[arglist.into()], "").unwrap(); +} + /// Invokes the [`llvm.stacksave`](https://llvm.org/docs/LangRef.html#llvm-stacksave-intrinsic) /// intrinsic. pub fn call_stacksave<'ctx>(