Skip to content

c backend: unbox list elements, map list params, route str() to the string printer (closes #962) - #963

Open
78th8mysf9-a11y wants to merge 2 commits into
bad-antics:mainfrom
78th8mysf9-a11y:fix/c-backend-unbox
Open

78th8mysf9-a11y wants to merge 2 commits into
bad-antics:mainfrom
78th8mysf9-a11y:fix/c-backend-unbox

Conversation

@78th8mysf9-a11y

Copy link
Copy Markdown

lateralus c could not compile a program that touches a list. Three independent defects, all fixed here; none of them were reachable from the existing suite because no test read a list element in a condition.

1. List elements were never unboxed

ltl_list_t stores ltl_value_t, but _expr_IndexExpr emitted xs->items[i] verbatim:

if ((flags->items[k] == 1)) {     // ltl_value_t == int
error: invalid operands to binary expression ('ltl_value_t' and 'int')

The preamble already ships ltl_unbox_int, ltl_unbox_float, ltl_unbox_bool — they were just never applied. The transpiler now records the element type of a boxed list built as [x] * n and unboxes on read. Assignment goes through the same node, so targets emit un-unboxed; ltl_unbox_int(xs->items[i]) = v is not assignable.

2. list was not a known C type

_TYPE_MAP had no list entry and _c_type falls back to returning the name verbatim, so a parameter typed list produced:

static int64_t sum_list(list xs);   // unknown type name 'list'

Mapped to ltl_list_t*. This is the same failure I reported in #866 for read_u64_le(list buf, …) — same root cause, now fixed at the map.

3. println(str(n)) printed nothing

str() lowers to ltl_int_to_str, which returns ltl_string_t*, but _infer_ctype_from_expr answered ltl_value_t for the call, so the println dispatch picked the const char* overload and handed it a struct ltl_string *. gcc warns, links, and the program prints an empty line. str(x) now infers as ltl_string_t* and routes to ltl_io_println.

len(xs) had no bare-builtin mapping at all — it emitted a call to a function that does not exist. Now ((xs)->len).

End to end, not just emitted text

Sieve of Eratosthenes over 2,000,000, gcc -O2:

time result
interpreter 0.52 s 148933
C 0.026 s 148933

That is 20x, and the first time the C backend has been able to compile a sieve in this tree.

Tests

Six new cases in tests/test_c_backend.py: unbox on read, float unbox, assignment target not unboxed, list prototype, len(), str() in println.

$ python -m pytest tests/test_c_backend.py -q
80 passed in 0.17s

Full suite: 519 passed; tests/test_law_apply_harden.py fails on the unmodified tree as well (verified by stashing), unrelated.

One limitation left, deliberately

xs[i] where xs is a list parameter still has no element type — the parameter does not carry one. Those reads unpack to ltl_value_t and will not compile in scalar context. Defaulting them to ltl_unbox_int would compile and then silently return 0 for a float list, which is worse than the error, so it stays as an error until element types can be threaded through parameters.

Closes #962.

… the string printer

Three independent defects kept `lateralus c` from compiling a program that does
anything with a list. None of them were reachable from the existing test suite,
because no test read a list element in a condition.

1. `_expr_IndexExpr` emitted `xs->items[i]` verbatim. That is an `ltl_value_t`,
   so `if flags[k] == 1` became `ltl_value_t == int` and gcc stopped at
   "invalid operands to binary expression". The preamble already ships
   `ltl_unbox_int/float/bool`; they were simply never applied. The transpiler
   now records the element type of a boxed list built as `[x] * n` and unboxes
   reads. Assignment targets go through the same node, so they are emitted
   un-unboxed — `ltl_unbox_int(xs->items[i]) = v` is not assignable.

2. `_TYPE_MAP` had no entry for `list`, and `_c_type` falls back to returning the
   type name verbatim, so `fn f(xs: list)` produced the prototype
   `static int64_t f(list xs);` — "unknown type name 'list'". Mapped to
   `ltl_list_t*`.

3. `println(str(n))` printed nothing. `str()` lowers to `ltl_int_to_str`, which
   returns `ltl_string_t*`, but `_infer_ctype_from_expr` reported `ltl_value_t`
   for the call, so the `println` dispatch picked the `const char*` overload and
   handed it a `struct ltl_string *`. `str(x)` now infers as `ltl_string_t*` and
   the call routes to `ltl_io_println`. `len(xs)` had no bare-builtin mapping at
   all and emitted a call to a function that does not exist; it now emits
   `((xs)->len)`.

Verified end to end, not just as emitted text — a sieve of Eratosthenes over
2,000,000:

    interpreter  0.52 s
    C, gcc -O2   0.026 s     20x
    both print   148933

and a list function returning 14 through a `list` parameter.

tests/test_c_backend.py gains six cases covering the unboxing, the assignment
path, the parameter prototype, `len()` and `str()` in `println`. 80 pass.

Known limitation, left documented rather than guessed at: `xs[i]` where `xs` is
a `list` *parameter* still has no element type, because the parameter does not
carry one. Reads there still unpack to `ltl_value_t` and will not compile in a
scalar context. Defaulting to `ltl_unbox_int` would compile and then silently
return 0 for a float list, which is worse than the error.

Refs bad-antics#962.

Signed-off-by: Ming <78th8mysf9@privaterelay.appleid.com>
The unboxing added in the previous commit only helps if the surrounding
declarations know what type came back. Three inference gaps left the values
boxed or the wiring missing, all found by writing a real program against the
backend (examples/ports/nbody.ltl):

1. `-1.5` inferred as `ltl_value_t`, so `xs[0] = -1.5` emitted a bare double
   into an `ltl_value_t` slot: "assigning to 'ltl_value_t' from incompatible
   type 'double'". Unary minus now infers from its operand.

2. `xs[i]` inferred as `ltl_value_t` even when the element type was known, so

       let dx = xs[i] - xs[j]

   declared `ltl_value_t dx = <double>`. That compiles and is silently wrong:
   later arithmetic on `dx` operates on a tagged union. The index branch now
   consults the recorded element type.

3. `to_int(x)` was not mapped at all — it emitted a call to a function that does
   not exist in C. Mapped to an `int64_t` cast, alongside `int`.

Three tests in tests/test_c_backend.py, which is now at 83 passing. Full suite
matches the pre-existing baseline exactly (the law/mutation/repl failures are
unrelated and present without these changes — verified by stashing).

Signed-off-by: Ming <78th8mysf9@privaterelay.appleid.com>
78th8mysf9-a11y added a commit to 78th8mysf9-a11y/lateralus-lang that referenced this pull request Nov 24, 2026
…faster than CPython

Ports examples/ports/nbody.py (Sun plus four planets, symplectic integrator,
eight parallel arrays) to examples/ports/nbody.ltl, same algorithm and
constants.

    python3 examples/ports/nbody.py    -4286150 / -4285578   4.25 s
    lateralus c + gcc -O2              -4286150 / -4285578   0.05 s

Energies are printed as integers scaled by 1e9: the language has no printf, and
scaling makes the comparison exact rather than approximate.

The C backend could not compile this when the port started; bad-antics#963 fixes the
three defects it hit (list elements never unboxed, `list` not a known C type,
`str()` routed to the const char* printer) plus three inference gaps found by
writing this file. The README records the two shapes that remain workarounds —
loops in main() because a `list` parameter has no element type, and inlined
constants because a top-level `let` becomes a C global that cannot initialise
from another.

Claims the $25 port micro-bounty in BOUNTIES.md.

Signed-off-by: Ming <78th8mysf9@privaterelay.appleid.com>
@78th8mysf9-a11y

Copy link
Copy Markdown
Author

Rebased onto main — 2 files, no benchmark history. 83 tests pass on the rebased head.

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.

c backend: list element access is never unboxed, so lateralus c output does not compile

1 participant