Skip to content

emit-C: multi-field enum payloads, collection literals, declared Result locals; the compiler's C emits again (#2017) - #2033

Merged
ehartford merged 5 commits into
impl-1903from
fix-2017
Oct 3, 2026
Merged

ehartford merged 5 commits into
impl-1903from
fix-2017

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes the emission half of #2017: out/release/bin/with build out/gen/versioned_main.w --emit-c --bundle-corpus std/re now writes the compiler's C. Three C-backend refusals stood in the way, each found by the one before it moving:

  1. Multi-field enum payloads (ba92adcb, 3644629d). A variant with several fields is a struct of them in the payload union (payloadV.fieldI), the layout with-abi.md §2 / TypeLayout's TY_ENUM rule give the payload area. Construction, place text, the three place-type walks and Display handle it; Downcast(v), Field(i) is read as payload field i of variant v (MirCore's projection typing). That also fixes a silent miscompile on main: a one-field variant whose payload is a tuple read its payload as the tuple's first element (V((3, 4)) printed 3 0).
  2. Declared Result locals retyped as Option (db1780e3). After (1), emission stopped at payload downcast for unit enum variant 1 with no function named. CCodegen now names the function a refusal came from (in ZipArchive_entries__53862: ...). lldb on the reduction (out.push(get(i)?)): emit_body_text's local-type pass took body_downcast_option_tids' guess over MIR's declared type: _9 = move _7<as v0>.f0 typed _7 Option[Entry] although MIR declares Result[Entry, str]. The guess now skips a local that MIR declares as a payload enum other than Option.
  3. Collection literals (06492ee2). [a, b] into Vec/HashSet and [k: v] into HashMap (MirLower's COLLECTION_LITERAL / MAP_LITERAL) were LLVM-only; the C backend now builds the collection and pushes or inserts each element in order.

Each has an emit_c fixture that main's release compiler refuses to emit, and whose C output this branch's compiler prints identically to the LLVM backend: test/emit_c/enum_multi_field_payload.w (2- and 3-field positional, named and generic variants), result_try_pushed.w, collection_literals.w.

Bridge prototypes (72799f6c): the next stop was emit-c-test's line scanner (could not parse public ABI signature for wl_is_packed_struct). It now handles bool, defaulted parameters, and &str as the #1810 view with_str (it had been const with_str *). That cuts cc -fsyntax-only errors on main.c from 1027 to 165.

:emit-c-test is still red after emission. The 165 errors are the post-emission backlog #955 tracks:

  • 131 member references through an int32_t-typed local (Analysis, SemaVector, MirVector, ...)
  • 23 &str arguments passed as with_str *
  • 3 void results assigned to bool (CImport)
  • zlib's ct_data_s arrays used before the struct is defined
  • one ComptimeValue view assignment
  • two bridge functions (with_cimport_is_cxx, with_cimport_record_is_class) whose return type is left for the compiler to infer, which the line scanner reads as void

Verification: src/main build :gate green (fixed list); :emit-c-smoke green (26 cases, the 3 new fixtures among them); stage1 --emit-c of versioned_main.w succeeds.

Also filed: #2022 (emit-C ignores @[align(N)]; found while checking payload layout).

Eric Hartford added 5 commits October 3, 2026 05:01
… payload union (#2017)

The C backend refused every enum variant with more than one payload field
("C backend does not support enum variants with 2 payload fields"), so
`:emit-c-test` could not emit the compiler's own C.

- The payload union holds such a variant as a struct of its fields in
  declaration order (`payloadV.fieldI`), the layout with-abi.md §2 and
  TypeLayout's TY_ENUM rule give the payload area.
- Construction builds that struct field by field; place text and the three
  place-type walks read `Downcast(v), Field(i)` as payload field i of
  variant v (MirCore's projection typing), so a one-field variant whose
  payload is a tuple no longer has its Field(0) read as the tuple's first
  element.
- Display formats `Name(a, b)`, as the LLVM backend does.

test/emit_c/enum_multi_field_payload.w: 2- and 3-field positional, named
and generic variants built, matched, passed, borrowed and displayed; main's
release compiler refuses its emission, stage1 with this change emits C that
prints the same lines as the LLVM backend.
… tuple (#2017)

Main's C backend printed `tuple payload 3 0`: after the downcast it took the
payload's type, so the payload field projection Field(0) became the tuple's
.field0. The previous commit reads Field(i) after a downcast as payload field i.
…ption; a refusal names its function (#2017)

With multi-field payloads emitted, the compiler's C stopped at
"payload downcast for unit enum variant 1" with no function named.

- CCodegen.emit_fn_body prefixes a refusal raised inside a body with the
  body's C name: the failure was in ZipArchive_entries.
- Root cause (lldb on the q.w reduction, `out.push(get(i)?)`): the
  body-local type pass (emit_body_text) took body_downcast_option_tids'
  guess over MIR's declared type. `_9 = move _7<as v0>.f0` made it type
  `_7` Option[Entry] (Option of the destination's type) although MIR
  declares it Result[Entry, str]; place_text then read `_7<as v1>` as
  Option's unit None. The guess now skips a local MIR declares as a
  payload enum other than Option (local_declared_non_option_payload_enum),
  in the body pass and in the per-local local_payload_downcast_option_tid.

test/emit_c/result_try_pushed.w: main's release compiler refuses its
emission; this stage1's C prints the LLVM backend's lines.
Next stop in the compiler's C: "in Codegen_mir_emit_owned_map_insert: emit-c
does not support generic intrinsic __collection_literal" (`let get_params:
Vec[i64] = [ptr_ty, ptr_ty, i64_ty]`). MirLower lowers `[a, b]` into a Vec or
HashSet and `[k: v]` into a HashMap as a COLLECTION_LITERAL / MAP_LITERAL
call that only the LLVM backend handled.

emit_collection_literal_term builds the collection empty and pushes or
inserts each element in order, as the LLVM backend's
mir_emit_collection_literal_intrinsic_call and this backend's own
VEC_PUSH / MAP_NEW / MAP_INSERT do; any other target refuses loudly.

test/emit_c/collection_literals.w: Vec of ints, strs and records, a
HashSet, a HashMap and an empty Vec; main's release compiler refuses its
emission, this stage1's C prints the LLVM backend's lines.
…rs as the emitted C declares them (#2017)

With the compiler's C emitted, emit-c-test stopped in the bridge prototype
scan: "could not parse public ABI signature for wl_is_packed_struct"
(`-> bool` had no C spelling; `packed: bool = false` kept its default in the
type). Past that, the -included wl_decls.h declared every `&str` parameter
`const with_str *` (#785) while the emitted definitions take the #1810 view
by value: 762 "passing 'with_str' to parameter of incompatible type" and 104
"conflicting types" of the 1027 errors `cc -fsyntax-only` reported on
main.c.

- emitc_c_type: `bool` is C's bool; `&str` is `with_str`.
- emitc_parse_param: a default value is not part of the prototype.

165 errors remain in main.c (tracked by #955): 131 member references
through an int32_t-typed local, 23 `&str` arguments passed as `with_str *`,
3 void results assigned to bool, zlib's ct_data_s arrays before the
struct's definition, one ComptimeValue view assignment, and the two bridge
functions whose omitted return type the line scanner reads as void.
@ehartford
ehartford marked this pull request as ready for review October 3, 2026 15:13
@ehartford
ehartford merged commit 58273c1 into main Oct 3, 2026
@ehartford
ehartford deleted the fix-2017 branch October 4, 2026 19:55
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