emit_llvm: bridge struct<->array ABI for 9..16-byte foreign structs

Resolves issue-0036 (LLVM verifier failure on 16-byte integer-only
struct by value through #foreign). The mismatch:

  Call parameter type does not match function signature!
    %load = load { i64, i64 }, ptr %alloca, align 8
  [2 x i64]  %call = call [2 x i64] @fn({ i64, i64 } %load)

`abiCoerceParamType` had already chosen `[2 x i64]` for 9..16-byte
non-HFA structs (the AAPCS64 / SysV AMD64 register-pair ABI slot for
that size class) on the foreign-decl side, but `coerceArg` only knew
how to bridge struct<->integer (the ≤8 B case) — not struct<->array.
LLVM's verifier rejects type-mismatched call args, so the call site
never landed.

Added the symmetric branches in coerceArg:
  - Struct -> Array : alloca <array>; store <struct>; load <array>
  - Array -> Struct : alloca <array>; store <array>;  load <struct>

Both use the LLVM opaque-pointer memory-bitcast pattern already in
place for the integer case. They're paired with the existing
i64 <-> small-struct bridge so all four (≤8 B int, 9..16 B int,
16 B HFA, >16 B byval) ABI slots round-trip cleanly through
emit_llvm now.

File mechanics: promotes the issue-0036 repro to a focused feature
example per CLAUDE.md's issue-resolution workflow:

  examples/issue-0036.sx              -> examples/101-ffi-medium-struct.sx
  tests/expected/issue-0036.{txt,exit} -> tests/expected/101-ffi-medium-struct.{txt,exit}
  vendors/issue_0036/issue_0036.c     -> vendors/ffi_medium_struct/ffi_medium_struct.c

Snapshot updated to the passing output. 89/89 regression tests pass;
chess Android build still clean.
This commit is contained in:
agra
2026-05-19 11:31:04 +03:00
parent 36e929101b
commit 7d2c2fb062
9 changed files with 64 additions and 51 deletions

View File

@@ -2607,6 +2607,21 @@ pub const LLVMEmitter = struct {
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.ret.coerce");
}
// Struct → Array (C ABI coercion for 9..16-byte structs — paired with
// abiCoerceParamType's `[2 x i64]` slot for that size class). Same
// memory-bitcast pattern as the integer case; the array type carries
// 16 bytes of storage so we alloca with param_ty to guarantee size.
if (val_kind == c.LLVMStructTypeKind and param_kind == c.LLVMArrayTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, param_ty, "abi.struct2arr");
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.coerce.arr");
}
// Array → Struct (return-side counterpart for 9..16-byte structs)
if (val_kind == c.LLVMArrayTypeKind and param_kind == c.LLVMStructTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, val_ty, "abi.arr2struct");
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.ret.coerce.arr");
}
// Array → Ptr (array decay: alloca + GEP to first element)
if (val_kind == c.LLVMArrayTypeKind and param_kind == c.LLVMPointerTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, val_ty, "ca.arr");