How valdanylchuk/xcc700 works
Purpose and Entry Points
xcc700 is a compact, self-hosting mini C compiler targeting ESP32 Xtensa. It accepts one C source file and emits one relocatable ELF (REL) executable/module consumable by ESP-IDF’s elf_loader. The compiler is implemented as a single translation unit: xcc700.c.
Primary invocation is a host-built cross-compiler (gcc xcc700.c) or an ESP32-resident compiler. A prebuilt ESP32 artifact is provided at release/xcc700.elf.
Monolithic Compiler Pipeline
xcc700.c contains the architecture’s full pipeline: command-line handling, source ingestion, lexical scanning, parsing, symbol management, code generation, relocation tracking, and ELF serialization. This single-file design is intentional: the compiler can compile itself and is practical to fork or embed directly in firmware.
• Source/compiler implementation: xcc700.c
• Self-hosted output example: release/xcc700.elf
Front End: Restricted C Parsing
The front end parses a deliberately small C subset sufficient to express the compiler itself: integer/character/pointer/array forms, functions, calls, conditionals, loops, and basic arithmetic/bitwise operators. Parsing directly drives generation rather than building a large standalone AST subsystem.
Important language boundaries are architectural: no preprocessing/includes, structs, typedefs, floating point, initialized globals, .data, or broad type checking. Invalid input is not expected to receive robust diagnostics.
• Parser and tokenizer implementation: xcc700.c
Symbol, Storage, and Relocation Model
Compilation tracks function/global symbols and separates generated content into ELF-relevant regions: executable code, read-only literal data, and zero-initialized global storage (.bss). External and internal address references are recorded as patches/relocations rather than fully resolved at compile time.
This boundary allows generated modules to call functions supplied by the host firmware, provided those symbols are exposed to the loader.
• Symbol/relocation and section emission logic: xcc700.c
Xtensa Back End
The back end emits basic Xtensa instruction bytecodes directly. It uses a stack-machine-oriented compilation strategy, avoiding register allocation and Xtensa register-window optimization in favor of small, understandable implementation. The target is Xtensa ESP32 variants, rather than native host execution.
• Xtensa encoder/code generator: xcc700.c
ELF Output and Runtime Integration
The output stage packages text, read-only data, BSS metadata, symbols, and relocation records into a relocatable ELF. This ELF is designed for ESP-IDF’s external elf_loader, which resolves relocations against firmware-visible APIs such as newlib libc, LVGL, or application functions at load time.
The runtime/linking boundary is therefore external: xcc700 produces a relocatable module; ESP-IDF firmware plus elf_loader loads and links it.
• ELF writer: xcc700.c
• Deployable module: release/xcc700.elf
Deployment Modes
The same compiler source supports three material execution contexts: a POSIX/macOS host cross-compiler, an ESP32 Xtensa binary, or embedded use as code invoked from firmware. Host GCC or Xtensa GCC can bootstrap the compiler; xcc700 can subsequently compile itself.
• Source for host, cross, and embedded builds: xcc700.c
• Released ESP32 binary: release/xcc700.elf
Main entry points: xcc700.c Xtensa code generator · xcc700.elf Released ESP32 compiler
Components 14
xcc700 compiler
Single translation unit implementing the full compiler.
- xcc700.c · Monolithic self-hosting compiler source.
xcc700.c - CLI and source ingestion
xcc700.c - Tokenizer and parser · Restricted C parsing drives code generation directly.
xcc700.c - Symbols and storage · Tracks functions, globals, literals, and BSS.
xcc700.c - Xtensa code generator · Direct bytecode emission using a stack-oriented strategy.
xcc700.c - Patch and relocation records · Defers internal and external address resolution.
xcc700.c - ELF writer · Serializes sections, symbols, and relocations as REL ELF.
xcc700.c
Module output
Relocatable ELF produced for ESP32.
- Relocatable ELF module · Contains text, rodata, BSS metadata, symbols, and relocations.
- Released ESP32 compiler · Deployable self-hosted xcc700 binary.
release/xcc700.elf
Deployment and loading
External environments that build or load the compiler and its modules.
- Host or Xtensa GCC · Builds xcc700 for host or ESP32 execution.
- xcc700 execution · Runs on POSIX/macOS, ESP32, or embedded firmware.
- ESP-IDF elf_loader · Loads modules and resolves their relocations.
- Firmware-visible APIs · Exports libc, LVGL, or application functions to loaded modules.
Other components
- C source input · One restricted-C source file per compilation.
Connections 17
- Host or Xtensa GCC → xcc700.c: builds
- xcc700.c → xcc700 execution: produces executable
- Released ESP32 compiler → xcc700 execution: runs on ESP32
- C source input → CLI and source ingestion: source file
- xcc700 execution → CLI and source ingestion: invokes
- CLI and source ingestion → Tokenizer and parser: source text
- Tokenizer and parser → Symbols and storage: declarations and storage
- Tokenizer and parser → Xtensa code generator: parsed constructs
- Symbols and storage → Xtensa code generator: symbol bindings
- Xtensa code generator → Patch and relocation records: address patches
- Symbols and storage → ELF writer: section metadata
- Xtensa code generator → ELF writer: text and literals
- Patch and relocation records → ELF writer: relocation entries
- ELF writer → Relocatable ELF module: serializes
- Relocatable ELF module → ESP-IDF elf_loader: loaded as module
- ESP-IDF elf_loader → Firmware-visible APIs: resolves symbols against
- Relocatable ELF module → Released ESP32 compiler: self-hosted example