summary_fix-nested-arrays.md 2.2 KB

Fix — nested-array elements (double load → wrong code / signal)

Tag v3-fix-nested-arrays. Main suite 108/108; QbeGen still compiles clean under V3; tools/m2make 18/18.

Symptom

Two failures surfaced by M2make (documented in tools/m2make/README.md), which forced its portable core onto a "flat 1-D arrays only" subset:

  • passing a nested-array element (a row) to an open-array formal, e.g. Sum(m[1]), segfaulted;
  • the a[i][j] / HIGH(a[i]) patterns in M2makeOS could not be used.

Root cause

Fact's Design branch loaded array-typed designators a second time. Design already materialises the array value for array-typed suffixes: the [i] index handler and the .field handler both emit ElemLoad (load the sub-descriptor pointer) when the element type is an array. Fact then did ElemLoad again, so the row pointer was replaced by its first word (the element count), which was passed on as an address:

%t129 =l loadl %t128     ; row descriptor address (correct, from Design)
%t130 =l loadl %t129     ; extra load: dereferences the row -> count
call $Sum_0(l 0, l %t130)

Fix

compiler/src/M2.atg (Fact, Design branch): treat ClArray like ClRecord / ClSet / ClClass — an array designator's value is its address, so CopyOp(qd, q) instead of ElemLoad(qd, dt, q).

Verification

  • Sum(m[1]) went from SIGSEGV (139) to the expected value (15), confirmed by reverting and re-applying the one-line change.
  • New regression test compiler/tests/t_nestidx.mod (exit 42) covers a[i][j] loads and stores, the comma form a[i, j], HIGH(row) via Sum, and passing rows to an open-array formal.
  • The M2makeOS pattern (argTab[i][k], HIGH(argTab[i]), CopyArg(s, argTab[argN])) now compiles and runs.
  • QbeGen.def/.mod still compile clean under V3 (0 errors).

Note on the "233 on a[i][j] loads"

A minimal a[i][j] load/store parsed and ran correctly even with the fix reverted, so no separate rejection was reproducible; the open-array-actual case is the one that definitely miscompiled. The one-line change above is the common fix for the nested-array-element value path.

Files

compiler/src/M2.atg, compiler/tests/t_nestidx.mod, compiler/run_tests.sh.