Skip to content

emit-C lane cannot build a compiler from C: emission stops at a tuple map key, and the in-unit pcre2 C had 165 errors #955

Open

Description

@ehartford

Restated 2026-10-25. The manual emit-C lane (emit-c-test, emit-c-fixpoint, emit-c-roundtrip, package-bootstrap-c) cannot build a compiler from C. Option (a) below is what the lane does now: build/emit_c.w passes --bundle-corpus std/engines/pcre2 so the corpus is emitted in-unit. Today emission stops before any C is written, because emit-C has no hash and equality functions for a tuple map key. The last measured C (comment of 2026-10, after #2033) had 165 C errors of five classes.


Original report

Found in batch C4 (wo-c4) while retiring rt/regex_runtime.w (D30). Three facts about the emit-C bootstrap lane (build/emit_c.w, build/package.w, emit-c-test/emit-c-roundtrip, package-bootstrap-c):

  1. The bootstrap C package has not been self-contained since C3.4 (81dbec8). pkg_emit_c(... "rt/regex_runtime.w" ...) compiled the shim with a compiler that embeds the pcre2 interface, so regex_runtime.c (74 KB, 2744 lines at 5465144) contains bare calls such as pcre2_compile_8(...) and no definitions — the corpus resolved to the .wi. Nothing in the package defined pcre2. (C4.4 deletes that emission; the compiler's own C now needs pcre2 the same way.)

  2. The C backend has no separate-compilation story for a bundle corpus. With --emit-c --bundle-corpus std/re, lib/std/re/bundle.w emits 632k lines of C in 30 s, but every function is named with a per-unit symbol suffix (int32_t pcre2_get_error_message_8__7587(...)) and every global is static with a mangled name (static pcre2_real_compile_context_8 __with_global__pcre2_default_compile_context_8__2861), while a consumer compiled against the interface emits a bare, undeclared call pcre2_get_error_message_8(1, _1, 256) (no prototype: CCodegen emits prototypes for extern fn, not for interface-bodied functions) and defines its own zeroed copy of the interface-provided global (static ... __with_global__pcre2_default_compile_context_8__7 = {0}) instead of an extern reference. Two C units cannot meet; src/CCodegen.w has no path_uses_module_link_names/InterfaceBody/InterfaceProvided handling (the LLVM backend's D38 rule, src/Codegen.w:4188).

  3. The corpus's C is not valid C even alone. cc -std=gnu11 -O1 -c -I runtime -include runtime/with_runtime.h pcre2.c (macOS 26.5, Apple clang) fails with three classes: redeclaration of '__stdinp' with a different type: 'void *' vs 'FILE *' (also __stdoutp, __stderrp — defs.w's migrated extern let for the stdio globals conflicts with <stdio.h>, which the emitted C includes); conflicting types for 'strchr' (defs.w:36 pub extern fn strchr(s: *const i8, c: i32) -> *mut i8 emitted as int8_t*/const int8_t* against <string.h>'s char*); and six call to undeclared function 'with_libc_{open,read,write,close,lseek,unlink}' (referenced from the std modules the corpus pulls, prototypes never emitted under --no-prelude).

Consequence for C4. With the shim gone, with_compiler.c (prelude on) references pcre2_* bare and undeclared, so the manual emit-c-test lane cannot compile the C compiler until one of these lands:

  • (a) the compiler's emit-C passes --bundle-corpus std/re and the corpus is emitted in-unit into with_compiler.c (the design in docs/wo_bundles.md "Retiring the shim": "in the emit-C lane the corpus compiles in-unit as C, no bundle involved") — needs only (3) fixed; +14 MB of C, +30 s Sema, and the roundtrip lane migrates the corpus back to With; or
  • (b) a C-side module-link-name rule mirroring the LLVM backend (stable unsuffixed names for bundle-owned definitions, extern prototypes and globals for interface declarations) so the corpus is a separate C unit — needs (2) and (3).

emit-c-smoke (test/hello.w, --no-prelude) is unaffected and stays green; emit-c-test, emit-c-fixpoint, emit-c-roundtrip and package-bootstrap-c are the manual lanes (docs/with-release-runbook.md:17).

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    With #2033 (fix-2017) the compiler's C emits again and the bridge prototypes match it; cc -fsyntax-only -ferror-limit=0 -x c out/emit-c-test/main.c -I runtime -include out/gen/wl_decls.h leaves 165 errors: 131 member references through an int32_t-typed local (Analysis, SemaVector, MirVector, ReceiverMigration, ...), 23 &str arguments passed as with_str * (CiMigrate, CiPrint, CImport), 3 void results assigned to bool (CImport:1391/1404/3397), zlib's ct_data_s arrays before its definition, one ComptimeValue.w:516 view assignment, and with_cimport_is_cxx / with_cimport_record_is_class, whose inferred return type emit_c.w's line scanner takes as void.

  2. changed the title [-]emit-C lane after the pcre2 bundle: the corpus's emitted C is not valid C, and the bootstrap C package has carried no pcre2 definitions since C3.4[/-] [+]emit-C lane cannot build a compiler from C: emission stops at a tuple map key, and the in-unit pcre2 C had 165 errors[/+] on Oct 25, 2026
  3. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced. The lane's own emission command fails loudly:

    $ with build src/main.w --emit-c --bundle-corpus std/engines/pcre2 -o main.c
    error: C emission failed: in sema_empty_state__23896: emit-c has no hash and equality functions for a map key of type (i32, i32) (#2180)
    error: build failed
    rc=1
    

    post-release: emit-C is a manual bootstrap lane, not on any user path, and getting it green is a multi-PR campaign (tuple keys first, then the 165 C errors).

  4. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25: carried over from #1043, closed as a duplicate of this issue. When the compiler's C last reached cc (after #1038), it had four errors of two classes: array type 'int16_t[75]' is not assignable twice (a global array's MIR proxy local zeroed as __with_global_escapes__4903 = 0;, pcre2's pub let escapes: [75]i16), and a str field reset through a const receiver after a borrowing concat. Both should be rechecked once emission gets past the tuple map key.

  5. added this to the post-v0.16.0 milestone on Oct 28, 2026
  6. ehartford commented on Nov 23, 2026

    @ehartford
    ContributorAuthor

    State 2026-11-23, after #3565: :emit-c-test and :emit-c-fixpoint are green on main (the compiler emits itself as C, that C compiles with with cc, and the C-built compiler emits itself to the same bytes), and :release-checks now runs the fixpoint. #2409 and #1192 are closed. What this issue still names beyond that (the in-unit pcre2 C, building a compiler from C with no LLVM-built compiler at hand) is what remains to measure.

  7. ehartford commented on Nov 23, 2026

    @ehartford
    ContributorAuthor

    Measured :emit-c-roundtrip on main after #3565 (1181 s; the lane emits the compiler as C, migrates that C back to With, and builds it). It stops in the migrate step, on two gaps in with migrate:

    1. The GCC atomic builtins have no translation (14 functions): __atomic_exchange_n, __atomic_store_n, __atomic_load_n and the rest, which the C backend emits for Atomic[T]. clang presents each as an atomic expression, not a call, and CImport bails on it by design today (ci_bail_unsupported_unexposed). The ordering argument is a constant conditional ((1) == 0 ? __ATOMIC_RELAXED : …) clang can evaluate. A translation needs the operation on raw memory: (*(p as *mut Atomic[T])).swap(v, .Acquire) under unsafe, since Atomic[T] is one T.
    2. unsupported assignment rhs in goto CFG (kind=121) in one function (Compilation.bundle_contract_rows, src/compiler/Compilation.with:1642).

    Both are migrator work in their own right (any C that uses <stdatomic.h> or the builtins hits the first), so they are next on this issue. package-bootstrap-c is not yet measured.

  8. ehartford commented on Nov 23, 2026

    @ehartford
    ContributorAuthor

    Correction and update (2026-11-23).

    Correction. My last comment said a translation of the atomic builtins would serve "any C that uses <stdatomic.h> or the builtins". What is written (branch migrate-atomics, not merged) covers the GCC __atomic_* spellings only. <stdatomic.h> (atomic_load(&g), a macro over __c11_atomic_load) is still refused, loudly: "C expression atomic_load(&g) has no translation".

    A defect found beside it, older than this work. with migrate drops _Atomic silently: static _Atomic int g; g++; g += 2; comes out as g = g + 1 on var g: i32. The clang bridge maps an atomic type to its value type (ClangBridge, CXType_Atomic), so a header import does the same to an _Atomic field. In C those operations are atomic; the output is a plausible wrong program (Law 7). The fix the laws point to: present _Atomic T as Atomic[T] (same layout) and lower reads, writes and compound operations on such a place to load, store and fetch_*; the <stdatomic.h> forms follow from the same expression node.

    Roundtrip after the atomics. Rerun: the 14 atomic stops are gone. One function remains untranslatable, Compilation.bundle_contract_rows: a closure construction the C backend emits as a GNU statement expression used as a value,

    _130 = ({ struct __with_env_0* __with_env = (struct __with_env_0*)__builtin_alloca(sizeof(struct __with_env_0)); __with_env->c0 = …; (with_fn_F1_R0s_rb){ __with_closure_0, (void*)__with_env }; });

    inside a function lowered with gotos. The migrator lowers a statement expression only where its value is discarded (and one comparison form); as a value it needs its statements hoisted before the use and its last expression taken as the value, with the locals it declares kept apart from others of the same name in the function.

  9. ehartford commented on Nov 23, 2026

    @ehartford
    ContributorAuthor

    After #3567 (atomic builtins) and #3569 (statement expressions as values): the roundtrip lane's remaining stop in the migrate step is __builtin_alloca (unsupported compiler builtin '__builtin_alloca': no structural lowering). The C backend emits it on purpose for a closure created in a loop (CCodegen.closure_value_text: one environment per evaluation; a function-level slot otherwise). With has no stack-allocation primitive, so what with migrate should make of alloca is a language decision (refuse as today; a local where provably not in a loop; heap with a free at every return; a stack-allocation primitive). Waiting on that ruling; not worked around.

    Also waiting on a ruling: _Atomic T presented as Atomic[T] (brief given to Eric 2026-11-23). Verified today: a header import presents an _Atomic int field as a plain i32 that safe code writes non-atomically, and translates an inline c->refs++ as a plain add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions