Skip to content

WITH over packed fields and elements that do not start on a byte - #1329

Open
hosking wants to merge 3 commits into
masterfrom
with-packed-designator
Open

hosking wants to merge 3 commits into
masterfrom
with-packed-designator

Conversation

@hosking

@hosking hosking commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

WITH x = d over a designator that does not start on a byte (a sub-byte field, a field of a bit-packed record field, or a packed array element) fails under the C backend on master:

TYPE In  = BITS 13 FOR RECORD a: BITS 4 FOR [0..15];  b: BITS 9 FOR [-256..255] END;
     Out = RECORD x: BITS 3 FOR [0..7];  f: In;  y: BITS 6 FOR [0..63] END;
VAR outs: ARRAY [0..3] OF Out;  pa: ARRAY [0..7] OF BITS 4 FOR [0..15];  i := 1;
...
WITH w = outs[2].f DO w.b := 77 END;   (* INTERNAL CG ERROR: unaligned addr_align in Check_byte_aligned *)
WITH x = pa[i] DO x := 3 END;          (* runtime error: Attempted to compute the address of an unaligned value *)

The language allows both: a WITH over a writable designator aliases it. The C path binds the alias to the designator's address (Expr.CompileAddress, then Variable.SetLValue), and such a designator has none.

CG already represents bit addresses on its stack, and its packed loads and stores handle them; only storing one as a pointer fails. So the alias now keeps the designator's base, with that base's alignment, plus its bits:

  • CG.Split_bit_address turns an address that may not be byte-aligned into its base, its static bit offset and (pushed) its dynamic bits. A byte-aligned address is handled as Check_byte_aligned handled it, so other WITHs compile as before.
  • Variable.SetBitLValue records the bits on the alias (dynamic bits in a local of their own, up-level if the alias is), and Load/LoadLValue add them back, so packed loads and stores through the alias choose the same containers as through the designator.
  • The alias keeps the designator's packed type (Variable.SetRepType), so a load or store through it covers only the designator's bits, not the stripped type's (a store through WITH x = r.b, b 5 bits, would otherwise write 8).

New test p314 covers sub-byte fields; a bit-packed record field at a dynamic index (evaluated once); packed elements at every dynamic index; a byte-crossing element at a static index; a byte-aligned element whose neighbour must survive; object fields; an alias used from a nested procedure; and that neighbouring bits are preserved throughout. Master cannot compile it; with this change it prints ok. Otherwise the full m3tests suite on ARM64_DARWIN gives exactly master's results.

Its dynamic indexes use element sizes that divide a byte: elements that cross a byte at a dynamic index are wrong on master independently of WITH, fixed in #1328.

The MSIR branch solves the same problem the same way (bitLocMSIR: the base evaluated once, plus a bit location).

🤖 Generated with Claude Code

The C path bound a WITH alias to its designator's address, which a sub-byte
field or packed element does not have: an internal CG error for a static
bit offset, an "unaligned address" runtime error for a dynamic one.  The
alias now keeps the designator's base, with its alignment, and its static
and dynamic bits (CG.Split_bit_address, Variable.SetBitLValue), and keeps
the designator's packed type (Variable.SetRepType) so loads and stores
through it cover only its bits.

New test p314.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts:
#	m3-sys/m3tests/src/m3makefile
@hosking
hosking requested a review from RodneyBates November 23, 2026 20:53
@hosking

hosking commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@RodneyBates, could you review this one too? It's the same packed-address machinery in CG as #1328, applied to WITH.

It overlaps with rodney-new much less than #1328 does. The new CG.Split_bit_address sits next to Check_byte_aligned and uses x.temp_bits, which your branch renames to bits_is_temp. Whichever lands second only needs that rename. WithStmt.m3 and Variable.m3 aren't touched on your branch.

The parts I'd most like a second pair of eyes on:

  • Split_bit_address, which takes the place of Check_byte_aligned for a WITH alias. A byte-aligned address goes through the old path, so other WITHs compile as before.
  • In Variable, the alias keeps the designator's base with that base's alignment, plus its bits, held in a local of their own (up-level if the alias is). Load/LoadLValue add the bits back. The alias also keeps the designator's packed type, so a store through it covers only the designator's bits.

p314 covers sub-byte fields, fields of bit-packed record fields, and packed array elements at constant and run-time subscripts, including aliases used from a nested procedure.

🤖 Generated with Claude Code

Resolve the m3tests list (p314 beside master's p315) and follow #1356's
rename of ValRec.temp_bits to bits_is_temp in Split_bit_address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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