Skip to content

offsetof[T](field): target layout offsets; the migrator stops folding host offsets (D109, #2131) - #2266

Merged
ehartford merged 2 commits into
mainfrom
type-offset
Oct 8, 2026
Merged

ehartford merged 2 commits into
mainfrom
type-offset

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

offsetof[T](field) is a builtin beside sizeof[T](): the byte offset of a field in the compilation target's layout (D109, delegated; spec v7.29 §16.12). The C migrator spells C's offsetof(T, f) with it instead of clang's host-folded number.

Why (#2131 item 4). The migrated pcre2 carried offsetof(heapframe, ovector) as 136, offsetof(heapframe, eptr) as 80, offsetof(pcre2_match_data, ovector) as 120 and HEAPFRAME_ALIGNMENT as 8: 64-bit host layouts. On wasm32 every heap frame was addressed askew and every regex match returned "no match".

What changed

  • Sema: offsetof[T](field) resolves the field by name and records its index; MIR lowers no operand; codegen reads the layout model.
  • Migrator: emits (offsetof[T](f) as usize) in bodies, initializers, object macros and uses of offsetof macros by name; records resolve through typedefs and macro-suffixed names; offsetof(T, a.b) is a sum; an index designator stays loud.
  • Offsetof checks read macros through a cache-free peek, because the lookup cache changed how unrelated macros translated (filed as Migrator: a function-like macro's translation depends on earlier macro lookups (cache side effect) #2265).
  • Corpora regenerated: only offsetof sites change (18 lines in six pcre2 files).

Evidence

  • test/behavior/behav_2131_offsetof.w fails on main, passes here; wasm32 gives 4/8 where native gives 8/16.
  • New migrator fixture in c-migrator-basic-tests (body, initializer, macro, macro-named typedef, nested designator) migrates and runs.
  • wasm32 under node: a on a → rc=1 span 0..1, [0-9]+ on x42 → span 1..3; std.regex probe prints true false true, same as native.
  • Gate green; deep-debug-tool-tests and drop-audit green; audit:resolution ok on the test.

…ells C's offsetof with it (D109, #2131)

The migrated pcre2 carried clang's host-folded offsetof values (136, 80,
120, 8 and the serialize offsets) as literals, so on wasm32 every heap
frame was addressed askew and every match failed. offsetof[T](field) is a
builtin beside sizeof: Sema resolves the field and records its index,
codegen reads the target layout. The migrator emits it in bodies,
initializers, object macros and uses of offsetof macros by name, through
typedefs and macro-suffixed record names, with member chains summed.

Offsetof checks read macros through a cache-free peek: the lookup's
caches feed the literal fallback, and using them changed how unrelated
macros translated (#2265). The four corpora are regenerated; only
offsetof sites change.
…rator names c_import's flexible-array field (D109, #2131)

The battery found three gaps. A module's runtime initializer (pcre2's
HEAPFRAME_ALIGNMENT, compiled from source for Windows) resolves no names
in codegen, so the record type is now Sema's fact per call node
(offsetof_owner_in_body). The C emitter lowers offsetof to TypeLayout's
offset, which its struct static-asserts already check. c_import renames a
trailing [0]/[1] array to _name beside its accessor, so offsetof names
that field.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on b3acda6 (gate, uat, battery-checks: fixpoint, drop and move audits, every test target). The first battery on d158370 was red on three gaps, fixed in b3acda6: offsetof in a module's runtime initializer (Sema now records the measured record per call), emit-c lowering, and c_import's renamed flexible-array field.

@ehartford
ehartford marked this pull request as ready for review October 8, 2026 07:04
@ehartford
ehartford merged commit 3915a70 into main Oct 8, 2026
@ehartford
ehartford deleted the type-offset branch October 8, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant