# valdanylchuk/xcc700 architecture

> Architecture diagram of the GitHub repository valdanylchuk/xcc700, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/valdanylchuk/xcc700
- Repository: https://github.com/valdanylchuk/xcc700

## Overview

#### 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`](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c) (Xtensa code generator), [`release/xcc700.elf`](https://github.com/valdanylchuk/xcc700/blob/main/release/xcc700.elf) (Released ESP32 compiler)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_compiler["xcc700 compiler"]
  node_xcc["xcc700.c<br/>compiler implementation<br/>[xcc700.c]"]
  node_cli_ingest["CLI and source ingestion<br/>compiler stage<br/>[xcc700.c]"]
  node_frontend["Tokenizer and parser<br/>compiler stage<br/>[xcc700.c]"]
  node_symbols["Symbols and storage<br/>compiler stage<br/>[xcc700.c]"]
  node_xtensa_codegen["Xtensa code generator<br/>compiler stage<br/>[xcc700.c]"]
  node_relocations["Patch and relocation records<br/>compiler stage<br/>[xcc700.c]"]
  node_elf_writer["ELF writer<br/>compiler stage<br/>[xcc700.c]"]
end

subgraph group_elf["Module output"]
  node_rel_elf["Relocatable ELF module<br/>REL ELF"]
  node_release_elf["Released ESP32 compiler<br/>prebuilt REL ELF<br/>[xcc700.elf]"]
end

subgraph group_runtime["Deployment and loading"]
  node_host_gcc["Host or Xtensa GCC<br/>bootstrap toolchain"]
  node_compiler_runtime["xcc700 execution<br/>execution context"]
  node_elf_loader["ESP-IDF elf_loader<br/>runtime loader"]
  node_firmware_apis["Firmware-visible APIs<br/>symbol provider"]
end

node_input_c["C source input"]

node_host_gcc -->|"builds"| node_xcc
node_xcc -->|"produces executable"| node_compiler_runtime
node_release_elf -->|"runs on ESP32"| node_compiler_runtime
node_input_c -->|"source file"| node_cli_ingest
node_compiler_runtime -->|"invokes"| node_cli_ingest
node_cli_ingest -->|"source text"| node_frontend
node_frontend -->|"declarations and storage"| node_symbols
node_frontend -->|"parsed constructs"| node_xtensa_codegen
node_symbols -->|"symbol bindings"| node_xtensa_codegen
node_xtensa_codegen -->|"address patches"| node_relocations
node_symbols -->|"section metadata"| node_elf_writer
node_xtensa_codegen -->|"text and literals"| node_elf_writer
node_relocations -->|"relocation entries"| node_elf_writer
node_elf_writer -->|"serializes"| node_rel_elf
node_rel_elf -->|"loaded as module"| node_elf_loader
node_elf_loader -->|"resolves symbols against"| node_firmware_apis
node_rel_elf -.->|"self-hosted example"| node_release_elf

click node_xcc "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_cli_ingest "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_frontend "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_symbols "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_xtensa_codegen "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_relocations "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_elf_writer "https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c"
click node_release_elf "https://github.com/valdanylchuk/xcc700/blob/main/release/xcc700.elf"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_xcc,node_cli_ingest,node_frontend,node_symbols,node_xtensa_codegen,node_relocations,node_elf_writer toneBlue
class node_rel_elf,node_release_elf toneAmber
class node_host_gcc,node_compiler_runtime,node_elf_loader,node_firmware_apis toneMint
class node_input_c toneNeutral
```

## Components

### xcc700 compiler

Single translation unit implementing the full compiler.

- [xcc700.c](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler implementation · Monolithic self-hosting compiler source. · `xcc700.c`
- [CLI and source ingestion](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler stage · `xcc700.c`
- [Tokenizer and parser](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler stage · Restricted C parsing drives code generation directly. · `xcc700.c`
- [Symbols and storage](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler stage · Tracks functions, globals, literals, and BSS. · `xcc700.c`
- [Xtensa code generator](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler stage · Direct bytecode emission using a stack-oriented strategy. · `xcc700.c`
- [Patch and relocation records](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler stage · Defers internal and external address resolution. · `xcc700.c`
- [ELF writer](https://github.com/valdanylchuk/xcc700/blob/main/xcc700.c): compiler stage · Serializes sections, symbols, and relocations as REL ELF. · `xcc700.c`

### Module output

Relocatable ELF produced for ESP32.

- **Relocatable ELF module**: REL ELF · Contains text, rodata, BSS metadata, symbols, and relocations.
- [Released ESP32 compiler](https://github.com/valdanylchuk/xcc700/blob/main/release/xcc700.elf): prebuilt REL ELF · 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**: bootstrap toolchain · Builds xcc700 for host or ESP32 execution.
- **xcc700 execution**: execution context · Runs on POSIX/macOS, ESP32, or embedded firmware.
- **ESP-IDF elf_loader**: runtime loader · Loads modules and resolves their relocations.
- **Firmware-visible APIs**: symbol provider · Exports libc, LVGL, or application functions to loaded modules.

### Other components

- **C source input**: source · One restricted-C source file per compilation.

## Connections

- 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
