Inside the Lume C11 Compiler: From a Tree-Walking Interpreter to Dual LLVM Backends

🇨🇳 中文版

Inside the Lume C11 Compiler: From a Tree-Walking Interpreter to Dual LLVM Backends

Lume-core is the host-independent upstream tree of the Lume language: it factors the language itself out into its own tree — the frontend (lexer / parser / typecheck), a tree-walking interpreter, and two native code emitters (hand-written LLVM IR text + the libLLVM C API). It links only libc (and additionally libLLVM at build time if llvm-config is found), with no HTTP service, no agent runtime, and no Docker image. The distinction to keep in mind: "links only libc" refers to the compiler's own link-time dependencies (plus the optional libLLVM); turning a Lume script into a native executable additionally relies on the system toolchain (cc / clang and the linker) — that is a requirement of the generation step, not of the compiler binary itself. It stands in a "same language, different deployment shape" relationship with the host tree lume (the distribution that embeds the language into agent-httpd): language changes land here first, and the host tree then syncs from here.

Below, we walk through the real source code (src/) layer by layer along the data flow.

TL;DR

  • One frontend + three non-serial execution / code-generation paths: after the frontend (lexer / parser / typecheck) produces the typed AST, it forks in parallel into a tree-walking interpreter (interp.c), a hand-written LLVM IR text backend (codegen*.c), and a libLLVM C API backend (llvm_codegen.c) — the interpreter and the two backends are divergent execution branches over the same AST, not a serial "frontend → interpreter → backend" pipeline.
  • Two backends deliberately coexist and cross-validate: --compile goes through libLLVM (LLVMVerifyModule checks the IR shape at build time), --compile-text goes through hand-written IR text handed to clang; make native-bench compares the two with the same script.
  • **--no-pass** skips the LLVM optimization pipeline, making it easy to observe the lowering produced by the emitter.
  • Frontend files shared with the host tree (tool-assisted controlled sync — make sync-lang to trigger, make check-sync to audit drift, lang/PIN pins the upstream commit, not fully automatic tracking): lexer.c / token.c / parser_stmt.c / typecheck_stmt.c are still byte-identical (only vdom.c differs); both trees split the parser as parser.c + parser_expr.c + parser_stmt.c + parser_internal.h.
  • **int is a real i64** (two's complement); the lexer routes integers through strtoll, and only literals with . / e / E go through strtod to become float — a deliberate design to avoid silent rounding above 2^53 and divergence between the interpreter and the two backends.

Overall Architecture

Lume's source is organized as one shared frontend + three non-serial execution / code-generation paths, not a serial "frontend → interpreter → backend" pipeline. The README states clearly that this tree is host-independent, linking only libc (with libLLVM added when llvm-config is found), carrying no HTTP service or agent runtime. The typed AST is the fork point: from there the tree-walking interpreter, the hand-written IR text backend, and the libLLVM backend each consume that same AST independently. Compared with the host tree (lume), the frontend files here are controlled-sync copies — the host pulls from upstream via make sync-lang and pins the specific core commit with lang/PIN, so they are not automatically tracked forks; they have already drifted: lexer.c / token.c / parser_stmt.c / typecheck_stmt.c remain byte-identical (only vdom.c differs), while interp.c / value.c / typecheck.c / loader.c and others have diverged. Today the host already achieves controlled sync via make sync-lang + lang/PIN (explicit trigger, human audit of drift); the ideal is to push further toward automation — a single authoritative core implementation the host reuses through a dependency / submodule / explicit sync mechanism, keeping HTTP, Agent, and deployment capabilities at the host layer; the point is not to split it into a standalone dynamic library right now, but to avoid a situation where a language-semantics fix lands in only one tree while the other keeps running the old implementation. This is the architectural debt most worth watching in this whole story.

The frontend's abstract data structures are all declared in lume.h, including the core types VM, Node, and Type, which are shared by the lexer, the recursive-descent parser, the type checker, the interpreter, and both backends, forming a unified cross-layer intermediate representation. The symbol table is maintained in loader.c and value.c: the type-checking phase fills in binding information, and the interpreter queries that same symbol table at runtime.

The diagram below is the key to the whole architecture — the interpreter and the two backends share the AST and are not serial:

         Lume source code
                 │
                 ▼
             Lexer (lexical)
                 │
                 ▼
           Parser (parsing)
                 │
                 ▼
        Type Checker (type checking)
                 │
                 ▼
      Typed AST  (lume.h: Node / Type)
                 │
   ┌─────────────┼─────────────┐
   ▼             ▼             ▼
Tree-walking   Hand-written    libLLVM C API
interpreter    LLVM IR text     backend
(interp.c)     backend          (llvm_codegen.c)
              (codegen*.c)
   │             │              │
   ▼             ▼              ▼
 direct run    .ll → Clang/     LLVM IR module
               LLVM toolchain   (built in memory)
               → native exe      │
                                 ▼
                               verify(LLVMVerifyModule)
                               + optimize(-O2)
                               → object file (.o)
                                 │
                                 ▼
                               cc / clang link
                                 │
                                 ▼
                               native executable

Note: LLVMVerifyModule is only a structural-legality check of the IR module — it is not a binary-generation step. The libLLVM backend still has to lower the module to an object file via the target code generator and link it with the system cc into an executable (depending on the build config it may instead use libLLVM's JIT to run directly, rather than emitting a standalone binary).

Frontend Tour

**Lexer (lexer.c) The lexical phase converts the source program into a token stream. One noteworthy real design is the split between integers and floats** — int is the language's true i64 and must never pass through double (strtod silently rounds above 2^53, which once caused the native backends and the interpreter to disagree on the same literal). Only literals with . / e / E go through strtod to become float:

bool is_float = false;
for (int i = 0; i < len; i++)
    if (buf[i] == '.' || buf[i] == 'e' || buf[i] == 'E') { is_float = true; break; }

Token *t = &o->toks[o->count - 1];
t->is_int = is_float ? 0 : 1;

Integers outside the i64 range error out directly rather than silently wrapping — because a value a source program cannot represent is a source bug, and silent mutation was precisely the root cause of the two backends diverging in the first place.

**Recursive-descent parser (parser.c, parser_expr.c, parser_stmt.c)** This tree splits parsing into parser.c + parser_expr.c + parser_stmt.c + parser_internal.h, internally handling expressions and statements through recursive descent, building the Node AST defined in lume.h. The host tree uses the same split (parser.c + parser_expr.c + parser_stmt.c + parser_internal.h), which is exactly why the "hand sync" of the two trees' frontends drifts most easily across these files.

**Type checker (typecheck.c, typecheck_expr.c, typecheck_stmt.c)** After parsing, the type checker walks the AST, performs static inference and consistency checks against the Type system in lume.h, and attaches each node's inferred type to the AST, providing type information for backend code generation.

Middle-end Tour: The Tree-Walking Interpreter (interp.c)

The interpreter directly executes the AST using the classic tree-walking approach. eval_expr() dispatches by node type with a large switch — the very skeleton of "tree walking", where each case corresponds to a syntactic node:

static void eval_expr(VM *vm, Node *n, Env *env) {
    if (vm->error) return;
    switch (n->type) {
        case N_LITERAL:
            eval_expr_literal(vm, n);
            return;
        case N_VAR: {
            int found = 0;
            Value v = env_get(env, n->as.var.name, &found);
            if (!found) {
                vm_set_error(vm, "line %zu: undefined variable '%s'", n->line, n->as.var.name);
                return;
            }
            vm_push(vm, v);
            return;
        }
        case N_ASSIGN: {
            eval_expr(vm, n->as.assign.value, env);
            if (vm->error) return;
            Value v = vm_peek(vm, 0); /* keep rooted on the stack */
            env_set(vm, env, n->as.assign.name, v);
            return; /* result: assigned value, already on stack */
        }
        /* …N_ASSIGN_MEMBER / N_BINARY / N_CALL / N_IF / N_WHILE / N_FOR … */
    }
}

Execution depends only on the interpreter's own runtime stack (vm_push / vm_peek), generating no intermediate code; on return it pops the stack frame and passes the return value to the caller. This stack-based execution is also the semantic baseline the native backends must align with.

Backend One: Hand-Written LLVM IR Text Emission (codegen.c and the codegen_*.c family)

One backend directly concatenates the text form of LLVM IR to produce the intermediate representation. codegen.c is just the entry point — the real emitters are split by responsibility into adjacent files, sharing the same CG context through codegen_internal.h:

/* codegen.c — the text backend's entry point.
 *   codegen_types.c   Type -> LLVM spelling
 *   codegen_expr.c    every expression form
 *   codegen_scan.c    static types of expressions, block scanning
 *   codegen_sig.c     signature inference
 *   codegen_stmt.c    statements, loops, function bodies
 */

It walks the typed AST and emits the corresponding LLVM instruction sequence for each expression or statement, writing it into a .ll file, which is then handed to clang -S / llvm-as for assembly and linking. Because it is pure text concatenation, this path is easy to inspect by hand to verify the generated IR.

Backend Two: libLLVM C API Emission (llvm_codegen.c, backend_llvm.c)

The other backend uses LLVM's C interface directly to build IR in memory. The context struct in llvm_codegen.c holds the module, builder, and declared types:

typedef struct {
    LLVMContextRef    ctx;
    LLVMModuleRef     mod;
    LLVMBuilderRef    ab;          /* the moving builder                     */
    LLVMValueRef      fn;          /* function whose body is being emitted   */
    LLVMBasicBlockRef cur;         /* block the builder points into          */
    LLVMTypeRef       i1, i64, dbl, i8ptr, void_ty, i32;
    /* …locals / gvars / sigs … */
} Cg;

Each AST node calls the corresponding LLVMBuild* API to construct values (LLVMValueRef) and basic blocks. For example, emitting binary addition is a single, plain builder call:

case OP_ADD: r = LLVMBuildAdd(g->ab, a.v, b.v, "a"); break;

The IR built this way lives in LLVM's in-memory data structures, skipping the text-parsing step. backend_llvm.c handles backend initialization, target-machine selection, and hands the generated module to LLVMVerifyModule for on-the-spot validation at the point of production — a malformed IR shape is rejected as soon as it is produced, rather than surfacing only at the clang step. Note: LLVMVerifyModule validates the structural legality of the LLVM IR module itself (that types, basic blocks, and call signatures are self-consistent) — it does not prove the generated program matches Lume's language semantics. Semantic correctness (integer overflow, scoping, control flow) is still backstopped by the interpreter-vs-native-backend consistency tests.

Dual-Backend Coordination and Cross-Validation

Both backends share the same typed AST produced by the frontend, but they are not required to generate byte-identical IR — as long as each conforms to Lume's language semantics, their instruction layout, temporary-variable naming, and optimization results may all differ. As docs/NATIVE.md explains, validation works at two layers: first, IR diff comparison (diagnostic) — enable both backends on the same script, generate IR, and diff it, which is good for spotting deviations in emitted instructions, basic blocks, and lowering strategy; second, differential run-result testing (semantic evidence) — tests/native_backends.sh runs the interpreter / text / libLLVM three-way comparison and checks the output of the same program, which is the primary evidence of semantic consistency (scripts/check-backend-parity.sh only checks whether the two emitters' AST-label coverage is consistent, a separate structural check).

Keeping both backends is a deliberate trade-off, not over-engineering. Three reasons justify it: portable builds — without an LLVM development package, the text backend still produces native binaries using only the system clang / system toolchain, lowering the deployment bar; observability — hand-written IR text is human-readable, so generation-stage problems (a mistyped instruction, an unclosed basic block) are visible with a plain cat, whereas libLLVM's in-memory IR needs an extra dump to inspect; consistency validation — since both emitters share the same frontend, any divergence in the execution result / test coverage of the same program is immediately flagged by diff and native_backends.sh. The cost is equally real: maintaining two code-generation paths means double the emitter bug surface and two lowering implementations to keep aligned, so development and testing cost is materially higher than a single backend — a liability the project currently carries on purpose, not for free.

An updated conclusion (2026-10-03): the libLLVM path now runs the optimization pipeline — the entry point for LLVMRunPasses is in llvm-c/Transforms/PassBuilder.h, and backend_llvm.c already includes it, so it is no longer a matter of "calling into a crash inside a pure-C translation unit". Empirically, the text path and the libLLVM path land in the same tier on the run side (default<O2> / clang -O2), and the libLLVM path actually compiles faster; make native-bench gives the following measurements on the same nested loop (1e8 inner iterations):

backend   compile-ms   ir-KiB  obj-KiB  bin-KiB    run-ms  nopass-ms
text            446        3        1       35         9          -
llvm            184        2        1       35         9        215

Once the run side is even, the real sentinel is the last column nopass-ms: after --no-pass (equivalently the env var LUME_NO_PASS=1) skips the pipeline, the libLLVM leg jumps from 9ms to 215ms — if the pipeline silently fails one day, this column will jump first. Do not quote the old numbers (e.g. 220ms / 7ms) directly: those were the table before the pipeline was connected, and the docs are explicitly marked as outdated. You can still switch to --compile-text when throughput is sensitive, but the reason now is no longer "libLLVM is slow because it doesn't run a pass".

Test environment: measured on the author's development machine — Apple M3, macOS 26.6.2 (Apple clang 21.0.0, LLVM 23.1), all at the unified -O2 optimization level. Statistic method from make native-bench: compile-ms is a single compile (one shot, no averaging), while run-ms and nopass-ms are the median of 3 runs (jitter-suppressed, not a mean). In the table, compile-ms is the single-measurement value (compile time is not sampled multiple times) and run-ms / nopass-ms are medians of 3 runs. The only defensible conclusion is "on this specific workload — the same nested loop (1e8 inner iterations) — the libLLVM backend compiles faster while its run time is on par with the text backend"; it must not be inferred that all programs show the same performance relationship.

Design takeaway: why keep both an interpreter and native compilation?

Layering the value of the three paths makes the design through-line of this article clear:

Execution mode Core value
Tree-walking interpreter Fast feedback, debugging, and a semantic reference — get language behavior working first on a relatively direct execution path
Hand-written LLVM IR text backend Easy to observe, easy to diagnose, toolchain-compatible (only needs system clang)
libLLVM C API backend Build IR directly in memory and integrate LLVM's verification (LLVMVerifyModule) and optimization capabilities

One clarification: the interpreter is not inherently easier to debug than native compilation, nor does it automatically become the correct semantic standard. Its real value is that you can get the language behavior working first on a relatively direct path as a reference, and then use the differential test across the interpreter and the two native backends to surface deviations in the native implementations. Once all three paths are stable, this structure itself provides a ready-made regression-validation baseline for Lume's language evolution.

A complete end-to-end compilation example

A minimal script ties every section of this article together, so readers unfamiliar with LLVM can follow one concrete path.

**① Source file fib.lume** (recursively compute the n-th Fibonacci number):

fn fib(n) {
  if n < 2 { return n }
  return fib(n - 1) + fib(n - 2)
}
print(fib(10))

② Frontend produces the typed AST: lume --check fib.lume runs lexing / parsing / type checking and infers the signature (int) -> int for fib, producing no IR.

③ Text-backend IR (representative .ll excerpt, visible via lume --compile-text --dump fib.lume):

define i64 @fib(i64 %n) {
entry:
  %lt = icmp slt i64 %n, 2
  br i1 %lt, label %then, label %else
; …then/else branches, the recursive call to fib, phi to merge the result…
}

④ Optimization: both --compile (libLLVM, default) and --compile-text run the optimization pipeline at -O2; --no-pass disables the pipeline to observe the raw lowering.

⑤ Native binary and execution: lume --compile fib.lume invokes the system cc to link the IR into an executable and runs it; print(fib(10)) outputs 55.

Compilation Flow in Reverse (main.c)

From the CLI entry main.c, when the user runs lume --compile script.lume, the program first reads the file, runs lexing / parsing / type checking to obtain the typed AST, then enters the corresponding code generation based on the selected backend. main.c explicitly models "which native backend" with an enum — the default is libLLVM, falling back to text when built without libLLVM:

/* Native backends the CLI can ask for. --compile picks the default, which is
 * the libLLVM one when the binary was built with libLLVM (LLVM then verifies
 * the IR shape while it is built); --compile-text is the opt-out. */
enum { NAT_DEFAULT = 0, NAT_LLVM = 1, NAT_TEXT = 2 };

The flags exposed by the CLI are consistent with the README: --check (type check only), --dump (print the IR produced by the text backend), --compile / --compile-llvm / --compile-text, --no-pass (skip the optimization pipeline), --no-fs / --no-net (narrow the script's capability boundary). main.c then temporarily invokes the system's cc (or libLLVM's JIT) to turn the IR into an executable binary and return it to the user.

Build, Test, and Platform Support

Dependencies are minimal: a C11 compiler (cc), and optionally llvm-config (determining whether --compile-llvm is available). Common targets:

make             # build bin/lume-core (needs cc; links libLLVM only if llvm-config present)
make check       # type-check the built-in examples, produces no artifacts
make test        # parity + unit tests + crypto + both native backends + consistency
make asan        # rebuild with ASan/UBSan and run unit tests
make native-bench # compare the two backends on the same script

Windows (MSYS2 / mingw-w64) is fully supported: the interpreter, type checker, both native backends, and module import all work under mingw. File/directory/lock/time builtins (mkdir / file read-write / files / lock_file / strftime) are also available, but they go through shims in os_win32.c (lume_mkdir / lume_flock) rather than builtins_fs.c writing the Win API directly. The only unsupported pieces are --watch (which depends on fork/exec/kqueue) and outbound HTTP: specifically http_get / http_post / … — src/builtins_http.c is excluded at build time and compiled with LUME_HAS_HTTP=0, so these builtins pass type checking in such a build but report a clear error when called (rather than failing to link outright).

Source Navigation

Layer Files
Core types lume.h
Frontend lexer.c · token.c · parser.c · parser_expr.c · parser_stmt.c · parser_internal.h · typecheck.c · typecheck_expr.c · typecheck_stmt.c · vdom.c
Interpreter interp.c · value.c · loader.c · rt.c
Text backend codegen.c · codegen_types.c · codegen_expr.c · codegen_stmt.c · codegen_scan.c · codegen_sig.c · irbuf.c · backend.c
libLLVM backend llvm_codegen.c · backend_llvm.c · backend_llvm.h
Builtins builtins.c · builtins_str.c · builtins_math.c · builtins_fs.c · builtins_http.c · builtins_crypt.c · builtins_hof.c · builtins_catalog.c
Bridge / entry bridge_stub.c · bridge_native.c · bridge_mcp.c · bridge_lsp.c · bridge_serve.c · main.c
Docs docs/LUME.md (user guide) · docs/DEVELOPMENT.md · docs/ARCHITECTURE.md · docs/NATIVE.md · docs/SPEC.md · docs/STYLE.md · docs/PITFALLS.md

FAQ

Does this Lume tree depend on any library other than libc? How should I read "links only libc"?

Keep four layers of dependency separate: ① the compiler's own base link-time dependencies — only libc, plus libLLVM when llvm-config is detected; ② the optional LLVM development dependency — libLLVM, which falls back to the text backend when absent; ③ the system toolchain needed to generate a native program — cc / clang and the system linker (the text backend goes .ll → clang, and the libLLVM backend, after target code generation, is likewise linked by cc); ④ the OS capabilities used at program runtime (file / network / process syscalls, etc.). "Links only libc" refers to layer ①, not to "the entire develop–compile–deploy chain needs no other tools". No Node, no server runtime, no SQLite.

What is the main difference between this tree's parser and the host tree's parser?

Both trees split the parser as parser.c + parser_expr.c + parser_stmt.c + parser_internal.h; the two trees' frontends are hand-synced copies, not automatically tracked, so files like parser_stmt.c must be merged manually as core changes.

How does the interpreter handle function calls and returns?

The interpreter uses the runtime stack (vm_push / vm_peek) to hold return addresses and local variable frames, and on return it pops the stack frame and passes the return value to the caller.

What is the main difference between the two backends?

The text IR backend generates a .ll file by directly concatenating LLVM IR strings and then hands it to clang; the libLLVM backend calls the LLVM C API to build LLVMValueRef and basic blocks in memory, validating on the spot with LLVMVerifyModule at the point of production.

How can I verify that the two backends and the interpreter are semantically consistent?

See the "Dual-Backend Coordination and Cross-Validation" section. IR text diff is only a diagnostic (spotting instruction / basic-block / lowering deviations); the real primary evidence is the interpreter / text / libLLVM three-way differential run-result test run by tests/native_backends.sh — as long as all three produce the same output for the same program, the semantics are considered consistent; the two backends are not required to generate byte-identical IR.

What does the --no-pass option do?

It skips the LLVM optimization pipeline and outputs the raw IR for easier inspection of the lowering; it is also the project's own diagnostic sentinel (see the performance-sentinel note in "Dual-Backend Coordination and Cross-Validation").

Why does the program built by the libLLVM backend run slower?

That was the old conclusion (before 2026-10-03). The libLLVM path now runs the optimization pipeline — the entry point for LLVMRunPasses is in llvm-c/Transforms/PassBuilder.h, and backend_llvm.c already includes it, so it is no longer a matter of "calling into a crash inside a pure-C translation unit"; on the run side the measurements are now even (default<O2> / clang -O2 in the same tier). The real sentinel today is the column when --no-pass (or LUME_NO_PASS=1) turns the pipeline off: the libLLVM leg jumps from roughly 9ms to roughly 215ms, and it will jump first if the pipeline silently fails one day. You can still switch to --compile-text when throughput is sensitive, but the reason is no longer "libLLVM is slow because it doesn't run a pass" — it is the fallback path when the LLVM development package cannot be installed.

Project Links and Further Reading

Lume-core is the host-independent upstream tree of the Lume language, adjacent to the host distribution lume. The source is public; the GitHub repository is directly accessible: github.com/erishen/lume-core. Language changes land here first and are then synced by the host tree. The authoritative externally accessible entry point is the Lume series on erishen.cn (e.g. Lume: An Agent DSL Server in a Single C11 Binary).

Comments (0)

    Comments require approval before appearing. Markdown supported.

    ← Back to list
    erishen

    Written by

    erishen

    Senior full-stack engineer focused on AI application engineering & delivery. Author of lume, lume-core and open-source AI engineering tooling.

    ⭐

    Enjoy this article?

    erishen/lume-core

    Star on GitHub