ffi 0.2: small struct baseline (Vec2, Vec4f) by-value through #foreign
88/88 regression tests pass (+ffi-02-small-struct).
vendors/ffi_structs/ defines:
Vec2 — 8 B, two f32 — register-pair (float) ABI
Vec4f — 16 B, four f32 — homogeneous float aggregate (HFA) on AAPCS64
Both pass cleanly today: the sx-side struct declarations match the C
ABI for these float-only shapes, and the call-site / foreign-decl
type representations agree.
`#source` only (no `#include`) — c_import's type mapping rewrites
struct-typed params/returns to *void, which would link but pass
through the wrong ABI silently. The hand-written #foreign decls keep
sx's struct types end to end.
16-byte integer-only shapes (`{s64, s64}`, `{s32, s32, s32, s32}`)
discovered to trip the LLVM verifier (`[2 x i64]` vs `{ i64, i64 }`
mismatch between foreign decl and call site). Excluded from this
baseline; filed separately in the next commit as issue-0036.
This commit is contained in:
29
vendors/ffi_structs/ffi_structs.c
vendored
Normal file
29
vendors/ffi_structs/ffi_structs.c
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "ffi_structs.h"
|
||||
|
||||
Vec2 ffi_vec2_make(float x, float y) {
|
||||
Vec2 r = { x, y };
|
||||
return r;
|
||||
}
|
||||
|
||||
Vec2 ffi_vec2_swap(Vec2 v) {
|
||||
Vec2 r = { v.y, v.x };
|
||||
return r;
|
||||
}
|
||||
|
||||
float ffi_vec2_sum(Vec2 v) {
|
||||
return v.x + v.y;
|
||||
}
|
||||
|
||||
Vec4f ffi_vec4f_make(float x, float y, float z, float w) {
|
||||
Vec4f r = { x, y, z, w };
|
||||
return r;
|
||||
}
|
||||
|
||||
Vec4f ffi_vec4f_reverse(Vec4f v) {
|
||||
Vec4f r = { v.w, v.z, v.y, v.x };
|
||||
return r;
|
||||
}
|
||||
|
||||
float ffi_vec4f_sum(Vec4f v) {
|
||||
return v.x + v.y + v.z + v.w;
|
||||
}
|
||||
18
vendors/ffi_structs/ffi_structs.h
vendored
Normal file
18
vendors/ffi_structs/ffi_structs.h
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
// FFI struct-marshalling baselines. Two shapes covered today:
|
||||
// Vec2 — 8 bytes (two f32) — register pair, float path
|
||||
// Vec4f — 16 bytes (four f32) — homogeneous float aggregate (HFA)
|
||||
// Declared here so the .c has a header to include; the sx side
|
||||
// imports via `#source` only and re-declares the structs natively
|
||||
// (c_import currently rewrites struct-typed params/returns to *void,
|
||||
// which loses the by-value ABI).
|
||||
|
||||
typedef struct { float x; float y; } Vec2;
|
||||
typedef struct { float x; float y; float z; float w; } Vec4f;
|
||||
|
||||
Vec2 ffi_vec2_make (float x, float y);
|
||||
Vec2 ffi_vec2_swap (Vec2 v);
|
||||
float ffi_vec2_sum (Vec2 v);
|
||||
|
||||
Vec4f ffi_vec4f_make (float x, float y, float z, float w);
|
||||
Vec4f ffi_vec4f_reverse(Vec4f v);
|
||||
float ffi_vec4f_sum (Vec4f v);
|
||||
Reference in New Issue
Block a user