Subject: [PATCH] WIP - core: Output passed vararg arguments --- Index: nac3core/src/codegen/mod.rs IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs --- a/nac3core/src/codegen/mod.rs (revision 51cb28dd24d40565938294a347fb2b0509c2daab) +++ b/nac3core/src/codegen/mod.rs (date 1722846979271) @@ -1,5 +1,6 @@ use crate::{ codegen::classes::{ListType, NDArrayType, ProxyType, RangeType}, + codegen::stmt::gen_for_callback_incrementing, codegen::tracert::{TraceRuntimeConfig, TraceRuntimeState}, symbol_resolver::{StaticValue, SymbolResolver}, toplevel::{helper::PrimDef, numpy::unpack_ndarray_var_tys, TopLevelContext, TopLevelDef}, @@ -1005,6 +1006,66 @@ ); code_gen_context.builder.set_current_debug_location(loc); + if let Some(vararg_arg) = vararg_arg { + let ctx = &mut code_gen_context; + + let llvm_usize = generator.get_size_type(ctx.ctx); + let llvm_pi8 = ctx.ctx.i8_type().ptr_type(AddressSpace::default()); + let llvm_i32 = ctx.ctx.i32_type(); + + let num_vararg = fn_val.get_last_param().unwrap().into_int_value(); + extern_fns::call_printf(ctx, "%d\n", &[num_vararg.into()]); + + let target_machine = registry.llvm_options.create_target_machine().unwrap(); + + let llvm_va_args = get_llvm_valist_type( + ctx.ctx, + &target_machine.get_triple(), + generator.get_size_type(context), + ); + let va_args = + generator.gen_var_alloc(ctx, llvm_va_args, Some(&vararg_arg.name.to_string())).unwrap(); + + let va_args_pi8 = ctx + .builder + .build_bitcast(va_args, llvm_pi8, "") + .map(BasicValueEnum::into_pointer_value) + .unwrap(); + llvm_intrinsics::call_va_start(ctx, va_args_pi8); + + gen_for_callback_incrementing( + generator, + ctx, + None, + llvm_usize.const_zero(), + (num_vararg, false), + |_, ctx, _, _| { + let va_args_pi8 = ctx + .builder + .build_bitcast(va_args, llvm_pi8, "") + .map(BasicValueEnum::into_pointer_value) + .unwrap(); + let varg = ctx + .builder + .build_va_arg(va_args_pi8, llvm_i32, "") + .map(BasicValueEnum::into_int_value) + .unwrap(); + extern_fns::call_printf(ctx, "%d\n", &[varg.into()]); + + Ok(()) + }, + llvm_usize.const_int(1, false), + ) + .unwrap(); + + let va_args_pi8 = ctx + .builder + .build_bitcast(va_args, llvm_pi8, "") + .map(BasicValueEnum::into_pointer_value) + .unwrap(); + llvm_intrinsics::call_va_end(ctx, va_args_pi8); + } + let result = codegen_function(generator, &mut code_gen_context); // after static analysis, only void functions can have no return at the end.