cstruct-layout.md 1.4 KB

C record layout

Records declared in a DEFINITION MODULE FOR "C" use the C record layout:

  • declaration-order fields;
  • natural alignment for each field;
  • trailing padding to the record's maximum alignment;
  • SIZE is the C sizeof value;
  • field offsets match C offsetof.

Example:

DEFINITION MODULE FOR "C" CStructLayout;

FROM SYSTEM IMPORT ADDRESS;

TYPE
  Mixed = RECORD
    b : CBool;       (* 1-byte _Bool *)
    f : SHORTREAL;   (* 4-byte float *)
    d : REAL;        (* 8-byte double *)
    i : INTEGER;     (* 4-byte int *)
    p : ADDRESS;     (* 8-byte pointer *)
  END;
END CStructLayout.

The layout is:

b  @ 0
f  @ 4
d  @ 8
i  @16
p  @24
sizeof(Mixed) = 32

This is covered by compiler/tests/cstruct_layout.def and CStructLayoutProg, with the C side checked using sizeof and offsetof.

Initial boundary policy

Records are currently passed to C by pointer only:

c_function(ADR(record))

By-value C struct parameters and results are not yet part of the ABI; use a C shim for APIs that require them.

C structs containing inline C arrays cannot be represented by V3 array fields, which are descriptors/pointers. Such records need a C shim or an array-free view.

Next

Callback/function-pointer ABI, with top-level trampolines and a user_data + destroy convention such as the existing GtkClosures design.