diff --git a/releasenotes.md b/releasenotes.md index b1a0f0559..b13d0f18b 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -122,6 +122,7 @@ - Slice overrun detected late hit codegen assert #2822 - Compile time dereference of a constant slice was too generous #2821 - Constant deref of subscript had inserted checks #2818 +- Raw vaargs with optional return not lowered correctly #2819 ### Stdlib changes - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 06d38e1e0..cbd6732ca 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -5817,7 +5817,7 @@ INLINE void llvm_emit_call_invocation(GenContext *c, BEValue *result_value, for (unsigned i = 0; i < vararg_count; i++) { ABIArgInfo *info = abi_varargs[index]; - BEValue value_copy = values[i + param_count]; + BEValue value_copy = values[i + param_count - start]; llvm_emit_parameter(c, arg_values, &arg_count, info, &value_copy); index++; } @@ -5828,7 +5828,7 @@ INLINE void llvm_emit_call_invocation(GenContext *c, BEValue *result_value, for (unsigned i = 0; i < vararg_count; i++) { REMINDER("Varargs should be expanded correctly"); - arg_values[arg_count++] = llvm_load_value_store(c, &values[i + param_count]); + arg_values[arg_count++] = llvm_load_value_store(c, &values[i + param_count - start]); } } } diff --git a/test/test_suite/abi/raw_vaarg_with_optional_return.c3t b/test/test_suite/abi/raw_vaarg_with_optional_return.c3t new file mode 100644 index 000000000..198d7f1f0 --- /dev/null +++ b/test/test_suite/abi/raw_vaarg_with_optional_return.c3t @@ -0,0 +1,24 @@ +// #target: linux-aarch64 +module test; +extern fn float? printf(char*,...); +fn int main() +{ + float a = 12.3; + (void)printf("", a); + return 0; +} + +/* #expect: test.ll + +declare i64 @printf(ptr, ptr, ...) #0 + +define i32 @main() #0 { +entry: + %a = alloca float, align 4 + %retparam = alloca float, align 4 + store float 0x40289999A0000000, ptr %a, align 4 + %0 = load float, ptr %a, align 4 + %fpfpext = fpext float %0 to double + %1 = call i64 (ptr, ptr, ...) @printf(ptr %retparam, ptr @.str, double %fpfpext) + ret i32 0 +}