Mumei's FFI-first design and Bridge mechanism.
Mumei adopts an FFI-first design philosophy, providing a foreign function interface
for safe interoperation with existing Rust and C ecosystems.
FFI functions are auto-registered as trusted atoms and verified only via contracts
(requires/ensures) — body is externally implemented.
extern "Rust" {
fn sqrt(x: f64) -> f64;
fn abs(x: i64) -> i64;
}
| Element | Description |
|---|---|
extern |
Keyword to start an external function block |
"Rust" / "C" |
Target language name |
fn name(params) -> RetType; |
Function signature declaration |
Extern functions can optionally declare requires and ensures contracts.
These contracts are not body-verified (the body is external), but they are
checked at every call site by Z3 — the caller must satisfy requires, and may
assume ensures holds after the call.
extern "Rust" {
fn sqrt(x: f64) -> f64
requires: x >= 0.0;
ensures: result >= 0.0;
}
If no contracts are specified, they default to true (backward compatible).
pub struct ExternFn {
pub name: String,
pub param_types: Vec<String>,
pub return_type: String,
pub requires: Option<String>, // verified FFI contract
pub ensures: Option<String>, // verified FFI contract
pub span: Span, // source location
}
pub struct ExternBlock {
pub language: String,
pub functions: Vec<ExternFn>,
pub span: Span, // source location
}Included as Item::ExternBlock(ExternBlock) in parse_module results.
Handled in all match blocks in main.rs, resolver.rs, lsp.rs.
| Item | Status |
|---|---|
| extern block syntax parsing | ✅ Implemented |
Verified FFI contracts (requires/ensures on extern fns) |
✅ Implemented |
ExternFn / ExternBlock AST |
✅ Implemented (with Span + contracts) |
Item::ExternBlock variant |
✅ Implemented (all match arms) |
| Parser tests | ✅ Implemented (test_parse_extern_block, test_parse_extern_block_c) |
| trusted atom auto-registration | ✅ Implemented (PR #32: extern → ModuleEnv auto-registration) |
| LLVM codegen | ✅ Implemented (declare_extern_functions() + resolve_return_type()) |
| FFI memory management | ✅ Implemented (json_free, string_free, http_free) |
| Managed string lifetime | ✅ Implemented (mumei_str_alloc, mumei_str_free, mumei_str_get) |
Functions declared in extern blocks are auto-registered as trusted atoms:
- Body verification skip: External implementation, so Z3 body verification is skipped
- Contract verification:
requires/ensurescontracts are verified at call sites - Taint analysis:
trustedfunction return values are tagged with__tainted_markers
NOTE:
ExternFnfields were previously#[allow(dead_code)]but are now used for trusted atom auto-registration inload_and_prepare().
extern "Rust" {
fn sqrt(x: f64) -> f64;
}
atom safe_sqrt(x: f64) -> f64
requires: x >= 0.0;
ensures: result >= 0.0;
body: sqrt(x);
| Language | Status | Description |
|---|---|---|
| Rust | Designed | References extern "C" symbols from Rust crates |
| C | Designed | References function symbols from C libraries |
extern blocks are detected in parse_module() using the following regex:
extern\s+"(\w+)"\s*\{([^}]*)\}
Each function signature is extracted with:
fn\s+(\w+)\s*\(([^)]*)\)\s*->\s*(\w+)
Details:
docs/ROADMAP.mdPhase P1-A
Completing the FFI Bridge is the top priority as a prerequisite for std.http / std.json.
Implementation Plan:
-
ExternBlock → trusted atom auto-conversion ✅ (PR #32)
- Generate
AtomfromExternFnsignature - Set
TrustLevel::Trusted(skip body verification) - Auto-register in
ModuleEnv.atoms
- Generate
-
LLVM declare generation ✅
declare_extern_functions()emits LLVM IRdeclarefor all extern functionsresolve_param_type()/resolve_return_type()map Mumei types → LLVM types
-
Call-site code generation ✅
- Callee return type resolved from
atom.return_typeannotation - ABI: both "C" and "Rust" use C calling convention
- Callee return type resolved from
-
Memory management ✅ (Plan 16)
json_free()/string_free()/http_free()release handles from global storesmumei_str_alloc()/mumei_str_free()/mumei_str_get()for managed string lifetime- Exposed as atoms in
std/json.mmandstd/http.mm
Files modified:
src/main.rs— ExternBlock → atom conversion inload_and_prepare()mumei-core/src/verification.rs— trusted verification for extern atomsmumei-emit-llvm/src/codegen.rs—declare_extern_functions(),resolve_return_type(), LLVMdeclare+callgenerationmumei-core/src/ffi/json.rs— JSON FFI backend + memory management (json_free,string_free,mumei_str_alloc/free/get)mumei-core/src/ffi/http.rs— HTTP FFI backend + memory management (http_free)
- std.http backend (Roadmap P1-C): HTTP client wrapping reqwest via FFI
- std.json backend (Roadmap P1-B): JSON operations wrapping serde_json via FFI
- std hierarchy: Reorganize into
std.core/std.net/std.mathmodule hierarchy - Link directives:
#[link(name = "libm")]equivalent linker directive syntax - Type mapping: Automatic mapping between Mumei types and foreign language types
mumei-core/src/parser/—ExternFn,ExternBlockstruct definitions + parsingmumei-core/src/verification.rs— Verification skip viaTrustLevel::Trustedmumei-emit-llvm/src/codegen.rs— External function call generation in LLVM IR