Expand description
Code generation from LOGOS AST to Rust source code.
This module transforms the parsed and type-checked LOGOS program into
idiomatic Rust code. The generated code uses logicaffeine_data types
for runtime values and integrates with the kernel for proof verification.
§Pipeline Position
┌─────────────────────────────────────────────────────────┐
│ LOGOS Source → Lexer → Parser → AST → Analysis → HERE │
└─────────────────────────────────────────────────────────┘
↓
Rust Source§Code Generation Rules
| LOGOS Statement | Rust Output |
|---|---|
Let x be 5. | let x = 5; |
Set x to 10. | x = 10; |
Give x to y. | let y = x; (move) |
Show x to show. | println!("{}", x); |
If x > 0 then... | if x > 0 { ... } |
Repeat while x > 0... | while x > 0 { ... } |
Zone "name"... | { /* zone scope */ } |
Mount x at "path". | x.mount(vfs, "path").await; |
Sync x on "topic". | x.subscribe("topic").await; |
§Key Features
- Refinement Types: Generates
debug_assert!for type predicates - Policy Enforcement: Emits capability checks for access control
- Zone Safety: Translates memory zones to Rust scopes
- CRDT Mutability: Uses
.set()for LWWRegister/MVRegister fields - Async Detection: Adds
#[tokio::main]when async operations are present - VFS Detection: Injects
get_platform_vfs()for file operations (io_uring on Linux, NativeVfs elsewhere) - Mount+Sync Detection: Uses
Distributed<T>for combined persistence/sync
§Refinement Context
The RefinementContext tracks type predicates across scopes:
Let x: { it: Int | it > 0 } be 5. ← Register constraint
↓
debug_assert!(5 > 0); ← Check at definition
↓
Set x to 10. ← Re-check on mutation
↓
debug_assert!(10 > 0); ← Re-emit assertion- When a variable with a refinement type is defined, its constraint is registered
- When that variable is mutated, the assertion is re-emitted
- Variable types are tracked for capability resolution
§Entry Point
The main entry point is codegen_program, which generates a complete Rust
program from a list of statements.
Structs§
- Refinement
Context - Tracks refinement type constraints across scopes for mutation enforcement.
- Variable
Capabilities - Tracks which variables have Mount and/or Sync statements.
Functions§
- codegen_
assertion - Converts a LogicExpr to a Rust boolean expression for debug_assert!(). Uses RustFormatter to unify all logic-to-Rust translation.
- codegen_
expr - codegen_
native_ tier_ export - Emit the AOT-native export shim (HOTSWAP §Axis-3): a thin
#[no_mangle] extern "C"calling-convention wrapper over the inner Rust fn that crosses our ACTUAL values — scalars BY VALUE — with NOCString/handle marshaling (unlikecodegen_c_export_with_marshaling). The interpreter marshals VMValues to these scalar args, calls the loaded symbol, and re-boxes the scalar result. - codegen_
program - codegen_
program_ mapped - Like
codegen_program, but also builds the rustc→LOGOSSourceMap: every generated line maps to the top-level statement that produced it (stmt_spansis the parser’s side-table, 1:1 withstmts; zero-width spans mark prelude statements and are skipped), and variable origins carry ownership roles for the diagnostic bridge. This is the flycheck substrate. - codegen_
program_ with_ proven - Like
codegen_program, but bundles an extracted math/logic module into the output.provenis the body of a Rust module — functions,check_*property fns, aWorld/holdsmodel-checker — produced by the Forge’s extraction with NOfn main. It is emitted aspub mod <module_name> { … }right after the prelude (beforeuser_types), followed byuse <module_name>::*;so a bare call in the imperative program below — e.g.double(21)— resolves into it. Naming the module (rather than dumping items at crate root) keeps multiple proven modules reachable and avoids polluting the imperative namespace. WhenprovenisNone/blank the output is byte-identical tocodegen_program. - codegen_
stmt - codegen_
term - collect_
async_ functions - Phase 54: Collect function names that are async. Used by LaunchTask codegen to determine if .await is needed.
- collect_
pipe_ sender_ params - collect_
pipe_ vars - Phase 54: Collect variables that are pipe declarations (created with CreatePipe). These have _tx/_rx suffixes, while pipe parameters don’t.
- embed_
assets_ enabled - Is the EMBEDDED-ASSETS
Servelowering active for this compile (B4)? When set, the emittedServeclosure consultslogos_assets::lookup(&path)BEFORE calling the handler — an embedded page/static asset shadows a backend route, exactly the dev router’s precedence, so alargo buildweb binary serves the bundled site. Default OFF: a plainServeprogram lowers byte-identically (the asset hook is inert), which the HTTP differential pins. - empty_
var_ caps - Helper to create an empty VariableCapabilities map (for tests).
- force_
disable_ borrow_ hoist_ for_ test - Force borrow hoisting off (or back on) for the current thread only. Used by tests to verify the kill switch without the env-var data race.
- function_
slice - The statements needed to compile
targetstandalone: thetargetfunction plus every function reachable from it through the call graph (transitively).Mainand top-level functions unreachable fromtargetare dropped; definition order is preserved. - generate_
c_ header - Generate the C header (.h) content for all C-exported functions.
- generate_
python_ bindings - Generate Python ctypes bindings for all C-exported functions.
- generate_
typescript_ bindings - Generate TypeScript type declarations (.d.ts) and FFI bindings (.js).
- observed_
codegen_ enabled - Is the OBSERVED codegen profile active (Wave 13)? When set, observation is NOT
elided at lowering and the AOT backend EMITS
logicaffeine_system::obscalls forNote/Warn/Record/During. Default OFF — the release/elided profile, where observation is erased before codegen (byte-identical artifact, §0.7). - release_
panic_ strategy - The panic strategy of a generated program’s release build:
unwindfor one that opens withUNWIND_PROFILE_HEADER,abortfor every other, whose failure hook reports and ends it. Every project writer takes it from here. - with_
deterministic_ replay - Run
fwith the deterministic-replay lowering enabled, restoring the previous mode afterwards (panic-safe). Returnsf’s result. - with_
embed_ assets - Run
fwith the embedded-assetsServelowering enabled (B4), restoring the previous mode afterwards (panic-safe).largo buildwraps the web-project compile in this so the generatedServeconsultslogos_assets. - with_
observed_ codegen - Run
fwith the OBSERVED codegen profile enabled, restoring the previous mode afterwards (panic-safe). Returnsf’s result.