aboutsummaryrefslogtreecommitdiff
path: root/tests/i/extern-c-struct-value/notes.md
blob: c694d08b985babba371be896e3c2fe5a1070889d (plain)
1
2
3
4
5
6
7

# extern-c-struct-value

Human review: pending. Expected output was independently traced by AI, not captured from cpsc.

Basis: compiler extension (EXTERN "C", INCLUDE_OBJ of a user object); a record whose fields are all C types is passed and returned by value like the matching C struct (C11 6.5.2.2p7: arguments are assigned to the parameters; 6.8.6.4p3: the returned value is the function call's value; 6.5.2.2p6-7 and 7.16.1.1p2: default promotions leave structs unchanged and va_arg reads a struct type).

Trace: the harness compiles `lib.c` with the host C compiler. pair_sum receives Pair {3, 4} and returns 3 + 4 = 7. vec2_dot receives {1.5, 2} and {4, 0.25}: 1.5 * 4 + 2 * 0.25 = 6 + 0.5 = 6.5, printed like other C doubles as `6.500000`. mixed_score receives bias -1, Mixed {'M', 2.5, 3} and k 4: -1 + 2.5 * 4 + 3 + 1 (the tag is 'M') = 13, printed `13.000000`. vec3f_sum receives {0.5, 1.25, 2}, all exact in float, and returns 0.5 + 1.25 + 2 = 3.75, printed `3.750000`. wide_after_five receives five ints and Wide {100, 1000}: 1 + 2 + 3 + 4 + 5 + 100 + 1000 = 1115. On x86-64 System V the five ints leave one integer register, so the 16-byte Wide goes on the stack. The make_ functions return the struct built from their arguments: make_pair(3, 4) gives `3 4`, and pair_sum(make_pair(5, 6)) passes the returned Pair straight on, 5 + 6 = 11. make_vec2(1.5, 2) gives `1.500000 2.000000`. make_mixed('Q', 0.25, 9) gives `Q 0.250000 9`; on x86-64 System V this 24-byte struct comes back through memory supplied by the caller. make_vec3f(0.5, 1.25, 2) gives `0.500000 1.250000 2.000000`, the C floats printed with %f. sum_vec2 reads two struct Vec2 values with va_arg: U is still {1.5, 2} from make_vec2 and V is {4, 0.25}, so 1.5 + 2 + 4 + 0.25 = 7.75, printed `7.750000`. mixed_counts reads two struct Mixed values: M holds count 9 from make_mixed and N = make_mixed('R', 1, 5) holds count 5, so 9 + 5 = 14.