From 87092705d92cb59bf3ff07af138a5a62973af3fe Mon Sep 17 00:00:00 2001 From: "Alice V." Date: Tue, 1 Dec 2026 07:13:47 +0000 Subject: [PATCH 1/5] pipeline: apply the assert-sandbox stubs to the eZ80 assert program #375 gave the forced-z80 assert sandbox appended/inserted zero-return stubs for call targets with no executable body, but the eZ80 runner (EZ80AssertEach) assembled the product body as-is. An addressless @extern keeps a bare label there too, so the sandbox assert of examples/nanz/canvas_house.nanz fell through the canvas_* labels into unrelated code and hit the cycle limit: "no return after 400000000 cycles (PC $040027)". The stub body (XOR A / LD L,A / LD H,A / RET) is valid ADL text, so the same withAssertSandboxStubs pass covers both. Product output is untouched: the pass runs only on the body the assert runner assembles, never on Steps.Assembly or Steps.EZ80Body. Regression test: pkg/pipeline TestEZ80AssertSandboxStubsUndefinedExtern (product keeps the bare label; the eZ80 sandbox assert passes). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS --- minzc/pkg/pipeline/assert_ez80.go | 8 +++++- .../pkg/pipeline/assert_sandbox_stubs_test.go | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/minzc/pkg/pipeline/assert_ez80.go b/minzc/pkg/pipeline/assert_ez80.go index 0e98fd1a4..74d135876 100644 --- a/minzc/pkg/pipeline/assert_ez80.go +++ b/minzc/pkg/pipeline/assert_ez80.go @@ -475,7 +475,13 @@ func EZ80AssertEach(hm *hir.Module, m *mir2.Module, ar *mir2.AllocResult, body s if !wanted { return nil } - prog, err := assembleEZ80Program(body) + // The assert sandbox, like the z80 one, defines call targets that have no + // executable body: addressless @extern functions keep a bare label in + // product output (their bodies live on the host or a later link step), so + // executing them here would fall through the label into unrelated code and + // run to the cycle limit (examples/nanz/canvas_house.nanz sandbox). The + // stub returns zeroes in A and HL, valid ADL text. + prog, err := assembleEZ80Program(withAssertSandboxStubs(body, mir2Funcs)) if err != nil { return fmt.Errorf("eZ80 asserts: assemble program: %w", err) } diff --git a/minzc/pkg/pipeline/assert_sandbox_stubs_test.go b/minzc/pkg/pipeline/assert_sandbox_stubs_test.go index 1fd5162dd..ce5671faa 100644 --- a/minzc/pkg/pipeline/assert_sandbox_stubs_test.go +++ b/minzc/pkg/pipeline/assert_sandbox_stubs_test.go @@ -45,3 +45,28 @@ func TestAssertSandboxStubsUndefinedExtern(t *testing.T) { t.Errorf("z80 sandbox assert: %v", err) } } + +// The eZ80 assert program gets the same accommodation: an addressless @extern +// keeps a bare label in the product body, so an assert that calls through it +// must not fall through the label into unrelated code until the cycle limit +// (examples/nanz/canvas_house.nanz sandbox, "no return after 400000000 cycles"). +func TestEZ80AssertSandboxStubsUndefinedExtern(t *testing.T) { + hm, err := nanz.Parse(assertSandboxStubSrc, "sandbox_stub.nanz") + if err != nil { + t.Fatalf("parse: %v", err) + } + opt := DefaultOptions() + opt.Backend = "ez80" + opt.AssertMode = "none" + steps, err := CompileHIRSteps(hm, opt) + if err != nil { + t.Fatalf("compile: %v", err) + } + // Product output: the extern stays a bare, undefined label. + if strings.Count(steps.EZ80Body(), "host_nop:") != 1 { + t.Fatalf("product eZ80 body must reference host_nop exactly once as a bare label:\n%s", steps.EZ80Body()) + } + if err := RunAssertsEZ80(hm, steps.MIR2Module, steps.Allocation, steps.EZ80Body()); err != nil { + t.Errorf("eZ80 sandbox assert: %v", err) + } +} From ea47a62772bd8a2f9178f5fe344bdcd8eaceba01 Mon Sep 17 00:00:00 2001 From: "Alice V." Date: Tue, 1 Dec 2026 08:18:34 +0000 Subject: [PATCH 2/5] asserts: target-aware "ez80 = N" expectations for the eZ80 corpus fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Five eZ80 assert-corpus fails are target-dependent facts, not compiler bugs (ADR 0054 §2/§3): - sizeof_test.c 22, c99_enum_typedef.c 68, c99_pointers.c 73: sizeof a pointer is 2 on the Z80, 3 under the eZ80 ADL pointer ABI. - 30_stream_test.nanz (first/seq byte): the program stores &buf into a u16 cell, which under the 24-bit ABI truncates the bank byte, so the write lands in bank 0 (unwritable on the Agon) and buf stays 0. The compiled code matches the specification exactly (bufstream_init does two byte stores, LD (IX+0),E / LD (IX+1),D); the Z80 verdicts still pass against the original expectations. Both frontends now accept an optional per-target expectation after the normal one: "assert f() == 2 ez80 = 3" (Nanz) and "// assert f() == 2 ez80 = 3" (C // assert comment). hir.Assert gains ExpectedEZ80 (*int64, nil = use Expected); only the eZ80 verdict paths consult it — checkEZ80AssertResult and the ez80_asserts harness's fab verdict — so Z80, Z80N, mir2 and wasm behaviour is unchanged, and compile_sweep shows Z80 output byte-identical to main (the only exit changes are the four edited corpus sources, which the main binary rejects as malformed directives because it predates the syntax; the candidate compiles their original and edited texts to identical .a80). The 30_stream_test.nanz edit refreshes its pinned record in docs/grace-integration-338/reviewed-updates.json (source_sha256 + boundary note) per the reviewed-catalog workflow. Regression tests: pkg/nanz TestParseAssertEZ80Override (clause, via interaction, nil default, truncated clause is an error), pkg/c89 TestAssertEZ80Override (hex/negative/comment forms, malformed refused), pkg/pipeline TestEZ80AssertTargetExpectation (Z80 runs against Expected == 2 and passes, eZ80 runs against the override == 3). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS --- .../reviewed-updates.json | 4 +- examples/c/c99_enum_typedef.c | 3 +- examples/c/c99_pointers.c | 3 +- examples/c89/sizeof_test.c | 3 +- examples/nanz/30_stream_test.nanz | 9 ++- minzc/pkg/c89/assert_directives_test.go | 40 ++++++++++++ minzc/pkg/c89/c89.go | 27 +++++--- minzc/pkg/hir/hir.go | 1 + minzc/pkg/nanz/assert_ez80_test.go | 48 ++++++++++++++ minzc/pkg/nanz/parse.go | 60 ++++++++++++++---- minzc/pkg/pipeline/assert_ez80.go | 10 ++- .../pipeline/assert_ez80_expectation_test.go | 63 +++++++++++++++++++ scripts/agon/ez80_asserts.go | 10 ++- 13 files changed, 250 insertions(+), 31 deletions(-) create mode 100644 minzc/pkg/nanz/assert_ez80_test.go create mode 100644 minzc/pkg/pipeline/assert_ez80_expectation_test.go diff --git a/docs/grace-integration-338/reviewed-updates.json b/docs/grace-integration-338/reviewed-updates.json index bf7449e6d..b3eb7fd04 100644 --- a/docs/grace-integration-338/reviewed-updates.json +++ b/docs/grace-integration-338/reviewed-updates.json @@ -604,11 +604,11 @@ }, { "path": "examples/nanz/30_stream_test.nanz", - "source_sha256": "c5da58c9e90049f1ac63b9d01dcec856a41bc0a62b111f4a7ffe2c008cb73fc2", + "source_sha256": "e83f40930a5d42b683ba6209c314880870f357a2bbf8a56a071499096f4a33f3", "native_behavior": "executed-independent-vm-z80", "workload": "bufstream_init/write: capacities 0,1,3,63,64; 70 sequential writes each", "independent_model": "All 64 buffer bytes and 6 descriptor bytes; host min(capacity,writes) model with relocated pointer", - "boundary": "HIR driver and explicit initial globals; original source functions unchanged. Does not claim importing stdlib streams or whole source sandbox.", + "boundary": "HIR driver and explicit initial globals; original source functions unchanged. Does not claim importing stdlib streams or whole source sandbox. 2026-12-01: the two first/seq-byte asserts gained target-aware `ez80 = 0` expectations and comments (24-bit ADL pointer stored in a u16 cell truncates to bank 0; ADR 0054); function bodies unchanged.", "evidence": { "minzc/pkg/pipeline/showcase_stream_oracle_test.go": "bc3284dd649caaee3d72b6888ce4b40e315175755f3dd256d52eb89c4ea8bb1e" } diff --git a/examples/c/c99_enum_typedef.c b/examples/c/c99_enum_typedef.c index 85fe58109..370cab82a 100644 --- a/examples/c/c99_enum_typedef.c +++ b/examples/c/c99_enum_typedef.c @@ -64,5 +64,6 @@ uint8_t size_enum(void) { return sizeof(enum Direction); } // assert test_counter() == 3 via mir2 // assert size_u8() == 1 via mir2 // assert size_u16() == 2 via mir2 -// assert size_ptr() == 2 via mir2 +// A pointer is 2 bytes on the Z80, 3 under the eZ80 ADL ABI (ADR 0054 §2). +// assert size_ptr() == 2 via mir2 ez80 = 3 // assert size_enum() == 2 via mir2 diff --git a/examples/c/c99_pointers.c b/examples/c/c99_pointers.c index 1288a33d1..ee8cf21d8 100644 --- a/examples/c/c99_pointers.c +++ b/examples/c/c99_pointers.c @@ -69,6 +69,7 @@ uint8_t test_fptr2(void) { // assert array_via_ptr() == 30 via z80 // assert ptr_index() == 40 via z80 // assert swap_test() == 20 via z80 -// assert ptr_size() == 2 via z80 +// A pointer is 2 bytes on the Z80, 3 under the eZ80 ADL ABI (ADR 0054 §2). +// assert ptr_size() == 2 via z80 ez80 = 3 // assert test_fptr() == 42 via z80 // assert test_fptr2() == 42 via z80 diff --git a/examples/c89/sizeof_test.c b/examples/c89/sizeof_test.c index fa6b13e9b..829ef086d 100644 --- a/examples/c89/sizeof_test.c +++ b/examples/c89/sizeof_test.c @@ -18,7 +18,8 @@ int test_sizeof_long(void) { int test_sizeof_ptr(void) { return sizeof(int *); } -// assert test_sizeof_ptr() == 2 via mir2 +// A pointer is 2 bytes on the Z80, 3 under the eZ80 ADL ABI (ADR 0054 §2). +// assert test_sizeof_ptr() == 2 via mir2 ez80 = 3 int test_sizeof_expr(void) { int x = 42; diff --git a/examples/nanz/30_stream_test.nanz b/examples/nanz/30_stream_test.nanz index cbc29965c..cc95bf3da 100644 --- a/examples/nanz/30_stream_test.nanz +++ b/examples/nanz/30_stream_test.nanz @@ -60,7 +60,11 @@ fun test_first_byte() -> u8 { return buf[0] } -assert test_first_byte() == 42 via mir2 +// On the Z80 the u16 cell holds &buf exactly and the write lands in buf. +// Under the eZ80 ADL ABI (ADR 0054) storing the 24-bit pointer in a u16 cell +// truncates it to bank 0, so the write misses buf (Agon RAM starts at +// $040000) and buf stays zero-initialised. +assert test_first_byte() == 42 via mir2 ez80 = 0 // ── Test: sequential bytes ─────────────────────────────────────────────── @@ -72,4 +76,5 @@ fun test_seq_bytes() -> u8 { return buf[1] } -assert test_seq_bytes() == 20 via mir2 +// Same truncated-pointer store as test_first_byte: buf is never written. +assert test_seq_bytes() == 20 via mir2 ez80 = 0 diff --git a/minzc/pkg/c89/assert_directives_test.go b/minzc/pkg/c89/assert_directives_test.go index 72fd878d0..6c0cd4663 100644 --- a/minzc/pkg/c89/assert_directives_test.go +++ b/minzc/pkg/c89/assert_directives_test.go @@ -22,3 +22,43 @@ func TestAssertTrailingCommentAndMalformed(t *testing.T) { } } } + +// "ez80 = N" gives an assert a target-aware expectation: sizeof(ptr) is 2 on +// the Z80 and 3 under the eZ80 ADL pointer ABI (ADR 0054 §2). Every runner +// but the eZ80 ones keeps using Expected. +func TestAssertEZ80Override(t *testing.T) { + three := int64(3) + zero := int64(0) + for _, tc := range []struct { + directive string + want *int64 + }{ + {"// assert f() == 42", nil}, + {"// assert f() == 42 ez80 = 3", &three}, + {"// assert f() == 42 ez80 = 0x3", &three}, + {"// assert f() == 42 via mir2 ez80 = 3", &three}, + {"// assert f() == 42 ez80 = 0 // explanation", &zero}, + } { + m, err := Compile("unsigned char f(void) { return 42; }\n"+tc.directive, "test.c") + if err != nil { + t.Fatalf("%s: %v", tc.directive, err) + } + a := m.Asserts[0] + if a.Expected != 42 { + t.Fatalf("%s: Expected %+v", tc.directive, a.Expected) + } + switch { + case tc.want == nil && a.ExpectedEZ80 != nil: + t.Fatalf("%s: ExpectedEZ80 %d, want none", tc.directive, *a.ExpectedEZ80) + case tc.want != nil && (a.ExpectedEZ80 == nil || *a.ExpectedEZ80 != *tc.want): + t.Fatalf("%s: ExpectedEZ80 %+v, want %d", tc.directive, a.ExpectedEZ80, *tc.want) + } + } + // A malformed override must not parse as a plain assert. + for _, directive := range []string{"// assert f() == 42 ez80 = typo", "// assert f() == 42 ez80"} { + _, err := Compile("unsigned char f(void) { return 42; }\n"+directive, "test.c") + if err == nil || !strings.Contains(err.Error(), "malformed") { + t.Fatalf("%s: %v, want malformed", directive, err) + } + } +} diff --git a/minzc/pkg/c89/c89.go b/minzc/pkg/c89/c89.go index 574188748..6c58182ee 100644 --- a/minzc/pkg/c89/c89.go +++ b/minzc/pkg/c89/c89.go @@ -257,11 +257,11 @@ func CompileWithOpts(src, name string, opts CompileOpts) (*hir.Module, error) { return l.hm, nil } -// assertRe matches: // assert fn(1, 2, 0xAB) == 42 [via mir2|z80] +// assertRe matches: // assert fn(1, 2, 0xAB) == 42 [via mir2|z80] [ez80 = 43] var assertStartRe = regexp.MustCompile(`^\s*//\s*assert(?:\s|$)`) var assertRe = regexp.MustCompile( - `//\s*assert\s+(\w+)\s*\(([^)]*)\)\s*==\s*(-?(?:0[xX][0-9a-fA-F]+|\d+))(?:\s+via\s+(mir2|z80))?\s*(?://.*)?$`, + `//\s*assert\s+(\w+)\s*\(([^)]*)\)\s*==\s*(-?(?:0[xX][0-9a-fA-F]+|\d+))(?:\s+via\s+(mir2|z80))?(?:\s+ez80\s*=\s*(-?(?:0[xX][0-9a-fA-F]+|\d+)))?\s*(?://.*)?$`, ) // objcAssertRe matches: // assert-objc Counter{count:42}.value(5) == 42 @@ -308,13 +308,24 @@ func parseAssertLine(line string, lineNo int) (hir.Assert, bool) { if err != nil { return hir.Assert{}, false } + // "ez80 = N": expectation on the eZ80 ADL backend (24-bit pointer ABI), + // for target-dependent facts such as sizeof(ptr) == 3 there, 2 on Z80. + var ez80 *int64 + if m[5] != "" { + v, err := strconv.ParseInt(m[5], 0, 64) + if err != nil { + return hir.Assert{}, false + } + ez80 = &v + } return hir.Assert{ - FuncName: funcName, - Args: args, - Expected: expected, - Source: line, - Line: lineNo, - Via: via, + FuncName: funcName, + Args: args, + Expected: expected, + ExpectedEZ80: ez80, + Source: line, + Line: lineNo, + Via: via, }, true } diff --git a/minzc/pkg/hir/hir.go b/minzc/pkg/hir/hir.go index b421447f8..03b5896c7 100644 --- a/minzc/pkg/hir/hir.go +++ b/minzc/pkg/hir/hir.go @@ -48,6 +48,7 @@ type Assert struct { FuncName string // function to call Args []int64 // literal integer arguments Expected int64 // expected return value (single-return) + ExpectedEZ80 *int64 `json:",omitempty"` // expectation on the eZ80 ADL backend (24-bit pointer ABI); nil uses Expected ExpectedMulti []int64 // expected values for multi-return; non-nil overrides Expected Source string // original source text (for error messages) Line int // source line number diff --git a/minzc/pkg/nanz/assert_ez80_test.go b/minzc/pkg/nanz/assert_ez80_test.go new file mode 100644 index 000000000..26aa976ab --- /dev/null +++ b/minzc/pkg/nanz/assert_ez80_test.go @@ -0,0 +1,48 @@ +package nanz + +import ( + "testing" +) + +// "ez80 = N" gives an assert a target-aware expectation (hir.Assert. +// ExpectedEZ80): facts that differ under the eZ80 ADL 24-bit pointer ABI, +// such as sizeof(ptr) == 3 there and 2 on the Z80, or a pointer stored +// through a 16-bit cell truncating. Every runner but the eZ80 ones keeps +// using Expected. +func TestParseAssertEZ80Override(t *testing.T) { + src := ` +fun f() -> u8 { return 2 } +fun g() -> u8 { return 1 } +assert f() == 2 ez80 = 3 +assert g() == 1 via mir2 ez80 = 0 +` + hm, err := Parse(src, "ez80_expect.nanz") + if err != nil { + t.Fatal(err) + } + if len(hm.Asserts) != 2 { + t.Fatalf("asserts: %+v", hm.Asserts) + } + a := hm.Asserts[0] + if a.Expected != 2 || a.ExpectedEZ80 == nil || *a.ExpectedEZ80 != 3 { + t.Fatalf("first assert: expected %d, ez80 %+v", a.Expected, a.ExpectedEZ80) + } + b := hm.Asserts[1] + if b.Via != "mir2" || b.Expected != 1 || b.ExpectedEZ80 == nil || *b.ExpectedEZ80 != 0 { + t.Fatalf("second assert: via %q, expected %d, ez80 %+v", b.Via, b.Expected, b.ExpectedEZ80) + } + + // Without the clause the override is nil and nothing changes. + hm2, err := Parse("fun f() -> u8 { return 2 }\nassert f() == 2 via mir2\n", "plain.nanz") + if err != nil { + t.Fatal(err) + } + if hm2.Asserts[0].ExpectedEZ80 != nil { + t.Fatalf("plain assert grew an override: %+v", hm2.Asserts[0]) + } + + // A truncated clause is an error, not a silently dropped assert. + if _, err := Parse("fun f() -> u8 { return 2 }\nassert f() == 2 ez80\n", "bad.nanz"); err == nil { + t.Fatal("ez80 without '=' must be a parse error") + } +} diff --git a/minzc/pkg/nanz/parse.go b/minzc/pkg/nanz/parse.go index 3f9df82bb..50fa888c5 100644 --- a/minzc/pkg/nanz/parse.go +++ b/minzc/pkg/nanz/parse.go @@ -2324,6 +2324,10 @@ func (p *parser) parseAssert() (hir.Assert, error) { // Negated: `assert not is_digit(65)` implies `== false` (expected=0) if !p.l.is(tokEqEq) { via := p.parseAssertVia() + ez80, err := p.parseAssertEZ80() + if err != nil { + return hir.Assert{}, err + } expected := int64(1) // true if negated { expected = 0 // false @@ -2336,13 +2340,17 @@ func (p *parser) parseAssert() (hir.Assert, error) { if via != "" { src += " via " + via } + if ez80 != nil { + src += fmt.Sprintf(" ez80 = %d", *ez80) + } return hir.Assert{ - FuncName: functionName, - Args: args, - Expected: expected, - Source: src, - Line: line, - Via: via, + FuncName: functionName, + Args: args, + Expected: expected, + ExpectedEZ80: ez80, + Source: src, + Line: line, + Via: via, }, nil } @@ -2392,17 +2400,25 @@ func (p *parser) parseAssert() (hir.Assert, error) { } via := p.parseAssertVia() + ez80, err := p.parseAssertEZ80() + if err != nil { + return hir.Assert{}, err + } src := fmt.Sprintf("assert %s(%s) == %d", assertName, intSliceStr(args), expected) if via != "" { src += " via " + via } + if ez80 != nil { + src += fmt.Sprintf(" ez80 = %d", *ez80) + } return hir.Assert{ - FuncName: functionName, - Args: args, - Expected: expected, - Source: src, - Line: line, - Via: via, + FuncName: functionName, + Args: args, + Expected: expected, + ExpectedEZ80: ez80, + Source: src, + Line: line, + Via: via, }, nil } @@ -2474,6 +2490,26 @@ func (p *parser) parseAssertVia() string { return "" } +// parseAssertEZ80 consumes an optional "ez80 = " suffix: the expectation +// when the assert runs on the eZ80 ADL backend, whose 24-bit pointer ABI makes +// some facts target-dependent (sizeof(ptr) is 3 there, 2 on the Z80; a pointer +// stored through a 16-bit cell truncates). nil = no override, every runner +// uses Expected. Only the eZ80 runners consult it. +func (p *parser) parseAssertEZ80() (*int64, error) { + if !p.l.is(tokIdent) || p.l.peek().val != "ez80" { + return nil, nil + } + p.l.next() // consume "ez80" + if _, err := p.l.eat(tokEq); err != nil { + return nil, fmt.Errorf("line %d: assert: expected '=' after ez80", p.l.peek().line) + } + v, err := p.parseAssertValue() + if err != nil { + return nil, fmt.Errorf("line %d: assert ez80: %v", p.l.peek().line, err) + } + return &v, nil +} + // parseSandbox parses: // // sandbox "name" { diff --git a/minzc/pkg/pipeline/assert_ez80.go b/minzc/pkg/pipeline/assert_ez80.go index 74d135876..a3882b966 100644 --- a/minzc/pkg/pipeline/assert_ez80.go +++ b/minzc/pkg/pipeline/assert_ez80.go @@ -421,9 +421,15 @@ func checkEZ80AssertResult(c *ez80emu.CPU, a hir.Assert, mf *mir2.Func, hirFuncs if err != nil { return fmt.Errorf("line %d: assert %q [ez80]: %w", a.Line, a.Source, err) } - if !hir.AssertValueMatches(got, a.Expected, ret.Ty) { + // The eZ80 expectation overrides the target-independent one when the + // assert declares one (24-bit pointer ABI facts). + want := a.Expected + if a.ExpectedEZ80 != nil { + want = *a.ExpectedEZ80 + } + if !hir.AssertValueMatches(got, want, ret.Ty) { return fmt.Errorf("line %d: assert %q [ez80]: got %s, want %d", - a.Line, a.Source, hir.FormatAssertValue(got, ret.Ty), a.Expected) + a.Line, a.Source, hir.FormatAssertValue(got, ret.Ty), want) } return nil } diff --git a/minzc/pkg/pipeline/assert_ez80_expectation_test.go b/minzc/pkg/pipeline/assert_ez80_expectation_test.go new file mode 100644 index 000000000..496c0f060 --- /dev/null +++ b/minzc/pkg/pipeline/assert_ez80_expectation_test.go @@ -0,0 +1,63 @@ +package pipeline + +import ( + "testing" + + "github.com/minz/minzc/pkg/c89" + "github.com/minz/minzc/pkg/nanz" +) + +// A target-aware expectation runs each backend against its own fact: the Z80 +// runners see Expected, the eZ80 runners see ExpectedEZ80. The corpus case is +// sizeof(ptr) == 2 on the Z80 and 3 under the eZ80 ADL pointer ABI +// (examples/c89/sizeof_test.c, examples/c/c99_pointers.c, +// examples/c/c99_enum_typedef.c). +func TestEZ80AssertTargetExpectation(t *testing.T) { + src := ` +int size_ptr(void) { + return sizeof(int *); +} +// assert size_ptr() == 2 ez80 = 3 +` + // Z80 (16-bit pointers): Expected == 2 must pass. + hmz, err := c89.Compile(src, "size_ptr.c") + if err != nil { + t.Fatal(err) + } + sz, err := CompileHIRSteps(hmz, Options{AssertMode: "none"}) + if err != nil { + t.Fatal(err) + } + if err := RunAssertsZ80(hmz, sz.MIR2Module, sz.Allocation, sz.Assembly); err != nil { + t.Fatalf("z80 assert with Expected == 2: %v", err) + } + + // eZ80 (24-bit pointers): ExpectedEZ80 == 3 must pass — and it is the + // override that runs, not Expected == 2. + hme, err := c89.CompileWithOpts(src, "size_ptr.c", c89.CompileOpts{PointerBits: 24}) + if err != nil { + t.Fatal(err) + } + opt := DefaultOptions() + opt.Backend = "ez80" + opt.AssertMode = "none" + se, err := CompileHIRSteps(hme, opt) + if err != nil { + t.Fatal(err) + } + if hme.Asserts[0].ExpectedEZ80 == nil || *hme.Asserts[0].ExpectedEZ80 != 3 { + t.Fatalf("eZ80 override lost in the C frontend: %+v", hme.Asserts[0]) + } + if err := RunAssertsEZ80(hme, se.MIR2Module, se.Allocation, se.EZ80Body()); err != nil { + t.Fatalf("eZ80 assert with ez80 = 3: %v", err) + } + + // The Nanz parser carries the same clause. + hmn, err := nanz.Parse("fun size() -> u8 { return 2 }\nassert size() == 2 ez80 = 3\n", "size.nanz") + if err != nil { + t.Fatal(err) + } + if hmn.Asserts[0].ExpectedEZ80 == nil || *hmn.Asserts[0].ExpectedEZ80 != 3 { + t.Fatalf("nanz override: %+v", hmn.Asserts[0]) + } +} diff --git a/scripts/agon/ez80_asserts.go b/scripts/agon/ez80_asserts.go index b0fb3dd14..11bca4efd 100644 --- a/scripts/agon/ez80_asserts.go +++ b/scripts/agon/ez80_asserts.go @@ -814,8 +814,14 @@ func verdict(a hir.Assert, mf *mir2.Func, hfuncs map[string]*hir.Func, d []byte) if !ok { return false, "unreadable return" } - if !hir.AssertValueMatches(got, a.Expected, ret.Ty) { - return false, fmt.Sprintf("got %s want %d (upper byte %02X)", hir.FormatAssertValue(got, ret.Ty), a.Expected, up) + // The eZ80 expectation overrides the target-independent one when the + // assert declares one (24-bit pointer ABI facts). + want := a.Expected + if a.ExpectedEZ80 != nil { + want = *a.ExpectedEZ80 + } + if !hir.AssertValueMatches(got, want, ret.Ty) { + return false, fmt.Sprintf("got %s want %d (upper byte %02X)", hir.FormatAssertValue(got, ret.Ty), want, up) } return true, "" } From 25ec090a1d0b5498f5f81f0ed5fd470b8999fb7f Mon Sep 17 00:00:00 2001 From: "Alice V." Date: Tue, 1 Dec 2026 08:24:00 +0000 Subject: [PATCH 3/5] pipeline: withAssertSandboxStubs must not stub labels the text already defines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pass recognised only "name:" lines as label definitions, so two shapes that define a symbol differently gained a duplicate stub and the program stopped assembling: - "tag: DS 2, 0" — a data label whose body is on the same line; - "node_tag EQU peek" — a function alias (examples/nanz/ self_lanz_parser.nanz), assembled as "label 'node_tag' already defined" once the eZ80 assert path (previous commit) ran the pass over that corpus body. The same bug is latent for the forced-z80 sandbox (examples/nanz/self_parser.nanz, self_lanz_parser.nanz). Both shapes now mark the symbol defined: cutAsmLabelDef splits the leading label from a same-line body, and "name EQU expr" lines count as definitions. Bare labels keep the previous next-line lookahead. Regression tests: TestAssertSandboxStubsDataLabelWithSameLineBody, TestAssertSandboxStubsEQUAliasNotStubbed (both expect the text passed through unchanged); self_lanz_parser/self_parser eZ80 asserts verified back to pass. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS --- .../pkg/pipeline/assert_sandbox_stubs_test.go | 24 ++++++++++++ minzc/pkg/pipeline/pipeline.go | 38 ++++++++++++++++--- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/minzc/pkg/pipeline/assert_sandbox_stubs_test.go b/minzc/pkg/pipeline/assert_sandbox_stubs_test.go index ce5671faa..fc8f72760 100644 --- a/minzc/pkg/pipeline/assert_sandbox_stubs_test.go +++ b/minzc/pkg/pipeline/assert_sandbox_stubs_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + "github.com/minz/minzc/pkg/mir2" "github.com/minz/minzc/pkg/nanz" ) @@ -70,3 +71,26 @@ func TestEZ80AssertSandboxStubsUndefinedExtern(t *testing.T) { t.Errorf("eZ80 sandbox assert: %v", err) } } + +// A data label defined with its body on the same line ("tag: DS 2, 0") is an +// existing definition: appending a stub for it made the self-parser corpus +// programs fail to assemble ("label 'node_tag' already defined") once the eZ80 +// assert path started running the stub pass. +func TestAssertSandboxStubsDataLabelWithSameLineBody(t *testing.T) { + asm := "f:\n XOR A\n RET\n\ncount: DS 2, 0\n DJNZ count\n" + out := withAssertSandboxStubs(asm, map[string]*mir2.Func{}) + if out != asm { + t.Fatalf("same-line data label must be left alone, got:\n%s", out) + } +} + +// "name EQU expr" (a function alias, e.g. examples/nanz/self_lanz_parser.nanz +// "node_tag EQU peek") defines the symbol: appending a stub label made the +// program fail to assemble with "label 'node_tag' already defined". +func TestAssertSandboxStubsEQUAliasNotStubbed(t *testing.T) { + asm := "peek:\n XOR A\n RET\n\nnode_tag EQU peek\n CALL node_tag\n" + out := withAssertSandboxStubs(asm, map[string]*mir2.Func{}) + if out != asm { + t.Fatalf("EQU alias must not gain a stub label, got:\n%s", out) + } +} diff --git a/minzc/pkg/pipeline/pipeline.go b/minzc/pkg/pipeline/pipeline.go index 7bdd7b181..46b533522 100644 --- a/minzc/pkg/pipeline/pipeline.go +++ b/minzc/pkg/pipeline/pipeline.go @@ -2174,7 +2174,7 @@ func withAssertSandboxStubs(asmSrc string, mir2Funcs map[string]*mir2.Func) stri externNoBlocks[asmutil.SanitizeZ80Label(f.Name)] = true } } - hasBody := map[string]bool{} // label name → followed by an instruction or data + hasBody := map[string]bool{} // label name → defined with an instruction or data seen := map[string]bool{} var referenced []string for i, line := range lines { @@ -2183,8 +2183,14 @@ func withAssertSandboxStubs(asmSrc string, mir2Funcs map[string]*mir2.Func) stri if code == "" { continue } - if name, ok := strings.CutSuffix(code, ":"); ok && !strings.ContainsAny(name, " \t") { - // A label has a body when the next significant line is an + if name, rest, ok := cutAsmLabelDef(code); ok { + if rest != "" { + // "name: DS 2, 0" — the definition carries its body on the + // same line; appending a stub would define the label twice. + hasBody[name] = true + continue + } + // A bare label has a body when the next significant line is an // instruction or data; consecutive labels (the nanz @extern // section) are empty. body := "" @@ -2200,6 +2206,12 @@ func withAssertSandboxStubs(asmSrc string, mir2Funcs map[string]*mir2.Func) stri hasBody[name] = body != "" && !strings.HasSuffix(body, ":") continue } + // "name EQU expr" defines the symbol too (function aliases): never + // append a stub label for it. + if fields := strings.Fields(code); len(fields) >= 3 && strings.EqualFold(fields[1], "EQU") { + hasBody[fields[0]] = true + continue + } fields := strings.Fields(code) if len(fields) < 2 { continue @@ -2223,8 +2235,8 @@ func withAssertSandboxStubs(asmSrc string, mir2Funcs map[string]*mir2.Func) stri out = append(out, line) code, _, _ := strings.Cut(line, ";") code = strings.TrimSpace(code) - name, ok := strings.CutSuffix(code, ":") - if !ok || strings.ContainsAny(name, " \t") || hasBody[name] || !seen[name] || !externNoBlocks[name] { + name, _, ok := cutAsmLabelDef(code) + if !ok || hasBody[name] || !seen[name] || !externNoBlocks[name] { continue } // Empty label of a referenced extern: insert the stub body in place @@ -2260,6 +2272,22 @@ func withAssertSandboxStubs(asmSrc string, mir2Funcs map[string]*mir2.Func) stri return strings.Join(out, "\n") } +// cutAsmLabelDef splits a leading "name:" label definition from the rest of +// the line ("name: DS 2, 0" → name, "DS 2, 0", true). The name must be one +// whitespace-free token; an instruction line (no colon, or a colon inside an +// operand) is not a definition. +func cutAsmLabelDef(code string) (name, rest string, ok bool) { + i := strings.IndexByte(code, ':') + if i < 0 { + return "", "", false + } + name = code[:i] + if name == "" || strings.ContainsAny(name, " \t") { + return "", "", false + } + return name, strings.TrimSpace(code[i+1:]), true +} + // isAsmSymbolTarget reports whether an operand field is a plausible label // name (not an indirect register or a numeric address). func isAsmSymbolTarget(target string) bool { From 6848a00cf032fce5f0b4f800b7980f0ffdfd4c0d Mon Sep 17 00:00:00 2001 From: "Alice V." Date: Tue, 1 Dec 2026 08:26:33 +0000 Subject: [PATCH 4/5] ci: classify the new assert-sandbox and eZ80-expectation tests in fast The stable pipeline requires every pkg/pipeline test in the profile manifest exactly once; the four new tests (sandbox-stub label/EQU recognition, eZ80 sandbox stubs, eZ80 target-aware expectation) run in seconds and belong in fast. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS --- scripts/ci/pipeline_profiles.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/ci/pipeline_profiles.json b/scripts/ci/pipeline_profiles.json index 0cc17b498..690f39f4b 100644 --- a/scripts/ci/pipeline_profiles.json +++ b/scripts/ci/pipeline_profiles.json @@ -22,6 +22,8 @@ "TestAssertMissingReturnContracts", "TestAssertRejectsMalformedRawResults", "TestAssertSandboxStubsUndefinedExtern", + "TestAssertSandboxStubsDataLabelWithSameLineBody", + "TestAssertSandboxStubsEQUAliasNotStubbed", "TestAssertSignedReturnDomains", "TestBitAccumNewShapesUnderOsize", "TestBitStorePointerHomeClearE2E", @@ -169,6 +171,8 @@ "TestEZ80ADLWordFlagsAndExtensions", "TestEZ80AssertModeRunsInProcess", "TestEZ80AssertPoisonsUpperBytes", + "TestEZ80AssertSandboxStubsUndefinedExtern", + "TestEZ80AssertTargetExpectation", "TestADLNarrowIntegerAsPointerIsZeroExtended", "TestEZ80GlobalAddressInitializer", "TestEmitterKeepsBlockOrderNative", From 0d839beecc4040ebdf78d17d990f57f2b8d18009 Mon Sep 17 00:00:00 2001 From: "Alice V." Date: Tue, 1 Dec 2026 09:12:48 +0000 Subject: [PATCH 5/5] examples: keep 30_stream_test's eZ80 failures visible The example stores a 24-bit pointer in a u16 cell, so on eZ80 it is not portable code; an 'ez80 = 0' expectation would enshrine the broken result. Restore the original source and its reviewed-updates record; the two asserts stay as known eZ80 failures until the example is rewritten portably. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01N957godRAhTkyy1mTB6roS --- docs/grace-integration-338/reviewed-updates.json | 4 ++-- examples/nanz/30_stream_test.nanz | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/docs/grace-integration-338/reviewed-updates.json b/docs/grace-integration-338/reviewed-updates.json index b3eb7fd04..bf7449e6d 100644 --- a/docs/grace-integration-338/reviewed-updates.json +++ b/docs/grace-integration-338/reviewed-updates.json @@ -604,11 +604,11 @@ }, { "path": "examples/nanz/30_stream_test.nanz", - "source_sha256": "e83f40930a5d42b683ba6209c314880870f357a2bbf8a56a071499096f4a33f3", + "source_sha256": "c5da58c9e90049f1ac63b9d01dcec856a41bc0a62b111f4a7ffe2c008cb73fc2", "native_behavior": "executed-independent-vm-z80", "workload": "bufstream_init/write: capacities 0,1,3,63,64; 70 sequential writes each", "independent_model": "All 64 buffer bytes and 6 descriptor bytes; host min(capacity,writes) model with relocated pointer", - "boundary": "HIR driver and explicit initial globals; original source functions unchanged. Does not claim importing stdlib streams or whole source sandbox. 2026-12-01: the two first/seq-byte asserts gained target-aware `ez80 = 0` expectations and comments (24-bit ADL pointer stored in a u16 cell truncates to bank 0; ADR 0054); function bodies unchanged.", + "boundary": "HIR driver and explicit initial globals; original source functions unchanged. Does not claim importing stdlib streams or whole source sandbox.", "evidence": { "minzc/pkg/pipeline/showcase_stream_oracle_test.go": "bc3284dd649caaee3d72b6888ce4b40e315175755f3dd256d52eb89c4ea8bb1e" } diff --git a/examples/nanz/30_stream_test.nanz b/examples/nanz/30_stream_test.nanz index cc95bf3da..cbc29965c 100644 --- a/examples/nanz/30_stream_test.nanz +++ b/examples/nanz/30_stream_test.nanz @@ -60,11 +60,7 @@ fun test_first_byte() -> u8 { return buf[0] } -// On the Z80 the u16 cell holds &buf exactly and the write lands in buf. -// Under the eZ80 ADL ABI (ADR 0054) storing the 24-bit pointer in a u16 cell -// truncates it to bank 0, so the write misses buf (Agon RAM starts at -// $040000) and buf stays zero-initialised. -assert test_first_byte() == 42 via mir2 ez80 = 0 +assert test_first_byte() == 42 via mir2 // ── Test: sequential bytes ─────────────────────────────────────────────── @@ -76,5 +72,4 @@ fun test_seq_bytes() -> u8 { return buf[1] } -// Same truncated-pointer store as test_first_byte: buf is never written. -assert test_seq_bytes() == 20 via mir2 ez80 = 0 +assert test_seq_bytes() == 20 via mir2