Libc Migration Plan: .in → Inauguration Multi-Language

Current state

Space kernel includes kernel/libc.in (47 functions, 925 lines) that reimplements C standard library functions in .in. The functions cover:

CategoryFunctionsCoverage
string.hstrlen, strlcpy, strncpy, strcmp, strncmp, strncat, strchr, strrchr, strstr, strdup, strrevFull
memory.hmemset, memcpy, memmove, memcmpFull
ctype.hisalpha, isdigit, isalnum, isspace, isupper, islower, isprint, toupper, tolowerFull
stdlib.hatoi, atol, itoa, htoa, utoa, fmt_unsigned_base, printf_padded, printf, malloc, calloc, realloc, free, abs, max, minPartial (no qsort, bsearch, rand)
math.hpow, log10, min, maxMinimal (trig, exp, floor, ceil missing)
stdio.hprintf (synthetic)Very partial (no sprintf, snprintf, sscanf, puts, gets)
time.htime, gmtime, localtime, mktimeMinimal

Problem: This is a maintenance tax. Every bugfix and optimization must be done twice — once in .in and once in the actual C/C++/Rust standard library for the compilers. The .in implementations are also smaller: no locale, no thread safety, no errno, no signal handling.

Target state

Compile C/C++/Rust standard library implementations through Inauguration's multi-language frontend pipeline. The .in libc becomes a thin wrapper — or disappears entirely, with kernel components linking against freestanding C stdlib functions compiled to Core IR and emitted as SCI components.

Inauguration's existing multi-language pipeline

C source           C++ source         Rust source
  │                   │                   │
  ▼                   ▼                   ▼
Tree-sitter parse   Tree-sitter parse   Tree-sitter parse
  (.c / .h)          (.cpp / .hpp)      (.rs)
  │                   │                   │
  ▼                   ▼                   ▼
extract_c_family    extract_cpp         extract_rust
  │                   │                   │
  └───────────────────┴───────────────────┘
                    ▼
              Core IR (UnifiedModule)
              Decl::Function, Decl::Class, Decl::Component
                    │
                    ▼
          ┌─────────────────┬─────────────────┐
          ▼                 ▼                  ▼
   x86_64 lowering    SCI binary emit    AArch64 lowering
   (x86_64_lower.rs)  (sci.rs)           (aarch64.rs)
          │                 │                  │
          ▼                 ▼                  ▼
   ELF relocatable    Raw SCI binary     Mach-O executable
   object (.o)        (bootable image)   (macOS JIT)

Current C frontend capability:

Current C++ frontend capability:

Migration phases

Phase 1: Minimal freestanding C string.h

Compile a minimal freestanding C source file through Inauguration's C frontend to produce a SCI component that Space can load.

What:

Status: Pipeline exists. The C frontend can parse and lower to Core IR. The SCI emitter can produce raw binaries. The gap is:

  1. C pointer types need proper lowering to Core IR pointer types
  2. The C frontend currently has parse + lower + typecheck but no boundary capability (C ABI boundary support is not wired)
  3. Need to ensure freestanding target linkage doesn't require a host libc

Evidence pipeline works:

in-cli/src/compiler/tree_front/c_family.rs     → C parser
in-cli/src/compiler/tree_front/extract.rs       → shared extraction
in-cli/src/native_emit/sci.rs                  → SCI binary emitter
in-cli/src/native_emit/x86_64_lower.rs         → x86_64 codegen
in-cli/src/native_emit/x86_64.rs               → x86_64 instruction encoding
in-cli/src/native_emit/elf.rs                  → ELF object (for non-SCI path)

Tests in sci.rs show the pipeline works for Core IR → SCI binary.

Phase 2: Freestanding stdlib.h subset

Compile atoi, atol, abs, malloc/free (bump allocator), bsearch/qsort.

What:

Gaps:

Phase 3: Full C stdlib on freestanding target

Port musl or a minimal freestanding C library subset through the pipeline.

Why musl (or similar):

What's needed from Inauguration:

FeatureStatusFor
C pointer type loweringparse onlymemcpy, memmove need pointers
C ABI boundary (boundary)not wiredCross-language calls
extern C linkagenot wiredLinking C stdlib with .in kernel
Freestanding __builtin stubsnot wiredmemset/memcpy IA-32 string ops
SCI data section globals✅ worksString literals, global data
x86_64 function body lowering✅ worksAll C function bodies

Phase 4: Rust and C++ stdlib

Once the C pipeline is proven, Rust and C++ standard library implementations follow the same path. Inauguration already has Rust tree-sitter parsing and class extraction for C++. The Core IR lowers all three languages to the same format, so the native emit pipeline needs no changes per-language.

Key files to change

FileChange
in-cli/src/compiler/tree_front/c_family.rsAdd pointer type lowering, extern linkage, boundary support
in-cli/src/compiler/tree_front/extract.rsMay need pointer declarator support
in-cli/src/native_emit/sci.rsAlready works; may need data section alignment fixes
in-cli/src/native_emit/lower/x86_64.rsMay need C ABI calling convention adjustments
in-cli/src/core_ir.rsMay need pointer type representation
(in Space) kernel/libc.inRemove functions as C replacements are verified
(in Space) kernel/kernel-root.inAdd SCI libc component loading at boot

Verification

Each phase must pass:

  1. in build --target x86_64-unknown-none --emit sci libc-phaseN.c → produces SCI
  2. Load SCI in Space boot → passes determinism test (identical PRNG output)
  3. bash scripts/check-qemu-boot.sh → full boot verification passes
  4. Existing shell commands (ls, cat, ps, etc.) continue working
  5. Existing Linux personality shell demo continues working

Risk assessment

RiskLikelihoodMitigation
C pointer lowering is incompleteMediumPhase 1 uses Int for pointers (current .in convention). Add real pointer types in Phase 3.
C ABI boundary (boundary capability)MediumStick to single-language (C→Core IR→SCI) first. Boundary needed when .in calls C functions.
Freestanding linker issuesLowx86_64-unknown-none ELF object emission already tested with Space kernel.
musl has architecture-specific codeLowUse only scalar subset (no atomics before arch support).
SCI loader rejects external components with undeclared capsLowDeclare cap_memory() in component manifest.