Repository navigation
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
Open
c backend: unbox list elements, map list params, route str() to the string printer (closes #962)#96378th8mysf9-a11y wants to merge 2 commits into
list params, route str() to the string printer (closes #962)#96378th8mysf9-a11y wants to merge 2 commits into
Conversation
… 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
force-pushed
the
fix/c-backend-unbox
branch
from
November 24, 2026 13:53
532f8ce to
2df2339
Compare
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>
Author
|
Rebased onto |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
lateralus ccould 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_tstoresltl_value_t, but_expr_IndexExpremittedxs->items[i]verbatim: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] * nand unboxes on read. Assignment goes through the same node, so targets emit un-unboxed;ltl_unbox_int(xs->items[i]) = vis not assignable.2.
listwas not a known C type_TYPE_MAPhad nolistentry and_c_typefalls back to returning the name verbatim, so a parameter typedlistproduced:Mapped to
ltl_list_t*. This is the same failure I reported in #866 forread_u64_le(list buf, …)— same root cause, now fixed at the map.3.
println(str(n))printed nothingstr()lowers toltl_int_to_str, which returnsltl_string_t*, but_infer_ctype_from_expransweredltl_value_tfor the call, so theprintlndispatch picked theconst char*overload and handed it astruct ltl_string *. gcc warns, links, and the program prints an empty line.str(x)now infers asltl_string_t*and routes toltl_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: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,listprototype,len(),str()inprintln.Full suite: 519 passed;
tests/test_law_apply_harden.pyfails on the unmodified tree as well (verified by stashing), unrelated.One limitation left, deliberately
xs[i]wherexsis alistparameter still has no element type — the parameter does not carry one. Those reads unpack toltl_value_tand will not compile in scalar context. Defaulting them toltl_unbox_intwould 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.