# rui314/chibicc architecture

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

- Interactive diagram: https://gitdiagram.com/rui314/chibicc
- Repository: https://github.com/rui314/chibicc

## Overview

chibicc is a small C11 compiler. A developer supplies C source to the driver, which reads and tokenizes it, expands preprocessing directives and headers, parses declarations and expressions into typed ASTs, emits x86-64 assembly, and invokes the system linker for an executable. The sampled code also shows UTF-8 handling, C11 type features, ABI-aware code generation, atomics, variadic arguments, TLS, VLAs, and related extensions. Some driver orchestration is documented rather than present in the excerpts.

Main entry points: [`main.c`](https://github.com/rui314/chibicc/blob/main/main.c) (Compiler Driver), [`parse.c`](https://github.com/rui314/chibicc/blob/main/parse.c) (C Parser), [`codegen.c`](https://github.com/rui314/chibicc/blob/main/codegen.c) (Code Generator), [`tokenize.c`](https://github.com/rui314/chibicc/blob/main/tokenize.c) (Tokenizer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_driver["Compiler Driver"]
  node_driver_main["Compiler Driver<br/>[main.c]"]
end

subgraph group_frontend["Frontend"]
  node_tokenizer["Tokenizer<br/>[tokenize.c]"]
  node_unicode["UTF-8 Support<br/>[unicode.c]"]
  node_preprocessor["Preprocessor<br/>[preprocess.c]"]
  node_c_headers["C11 Headers"]
  node_parser["C Parser<br/>[parse.c]"]
end

subgraph group_semantic["Language Model"]
  node_type_system["Type System<br/>[type.c]"]
  node_typed_ast["Typed AST<br/>[parse.c]"]
  node_c11_features["C11 Features<br/>[parse.c]"]
end

subgraph group_backend["Backend"]
  node_codegen["Code Generator<br/>[codegen.c]"]
  node_abi_lowering["x86-64 ABI<br/>[codegen.c]"]
end

node_developer(("C Developer"))
node_c_source["C Source"]
node_assembly["Assembly Output"]
node_linker["System Linker"]
node_executable["Executable"]

node_developer -->|"writes"| node_c_source
node_c_source -->|"supplies"| node_driver_main
node_driver_main -->|"dispatches"| node_tokenizer
node_tokenizer -->|"decodes UTF-8"| node_unicode
node_tokenizer -->|"emits tokens"| node_preprocessor
node_c_headers -.->|"provides macros"| node_preprocessor
node_preprocessor -->|"expands tokens"| node_parser
node_driver_main -->|"invokes parser"| node_parser
node_parser -->|"adds types"| node_type_system
node_parser -->|"constructs AST"| node_typed_ast
node_type_system -->|"annotates AST"| node_typed_ast
node_parser -->|"handles features"| node_c11_features
node_driver_main -->|"invokes codegen"| node_codegen
node_typed_ast -->|"provides AST"| node_codegen
node_codegen -->|"applies ABI"| node_abi_lowering
node_codegen -->|"emits assembly"| node_assembly
node_driver_main -->|"runs linker"| node_linker
node_assembly -->|"links input"| node_linker
node_linker -->|"produces executable"| node_executable

click node_driver_main "https://github.com/rui314/chibicc/blob/main/main.c"
click node_tokenizer "https://github.com/rui314/chibicc/blob/main/tokenize.c"
click node_unicode "https://github.com/rui314/chibicc/blob/main/unicode.c"
click node_preprocessor "https://github.com/rui314/chibicc/blob/main/preprocess.c"
click node_c_headers "https://github.com/rui314/chibicc/tree/main/include"
click node_parser "https://github.com/rui314/chibicc/blob/main/parse.c"
click node_type_system "https://github.com/rui314/chibicc/blob/main/type.c"
click node_typed_ast "https://github.com/rui314/chibicc/blob/main/parse.c"
click node_c11_features "https://github.com/rui314/chibicc/blob/main/parse.c"
click node_codegen "https://github.com/rui314/chibicc/blob/main/codegen.c"
click node_abi_lowering "https://github.com/rui314/chibicc/blob/main/codegen.c"

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_driver_main toneBlue
class node_tokenizer,node_unicode,node_preprocessor,node_c_headers,node_parser toneAmber
class node_type_system,node_typed_ast,node_c11_features toneMint
class node_codegen,node_abi_lowering toneRose
class node_developer,node_c_source,node_assembly,node_linker,node_executable toneIndigo
```

## Components

### Compiler Driver

- [Compiler Driver](https://github.com/rui314/chibicc/blob/main/main.c): `main.c`

### Frontend

- [Tokenizer](https://github.com/rui314/chibicc/blob/main/tokenize.c): `tokenize.c`
- [UTF-8 Support](https://github.com/rui314/chibicc/blob/main/unicode.c): `unicode.c`
- [Preprocessor](https://github.com/rui314/chibicc/blob/main/preprocess.c): `preprocess.c`
- [C11 Headers](https://github.com/rui314/chibicc/tree/main/include): `include`
- [C Parser](https://github.com/rui314/chibicc/blob/main/parse.c): `parse.c`

### Language Model

- [Type System](https://github.com/rui314/chibicc/blob/main/type.c): `type.c`
- [Typed AST](https://github.com/rui314/chibicc/blob/main/parse.c): `parse.c`
- [C11 Features](https://github.com/rui314/chibicc/blob/main/parse.c): `parse.c`

### Backend

- [Code Generator](https://github.com/rui314/chibicc/blob/main/codegen.c): `codegen.c`
- [x86-64 ABI](https://github.com/rui314/chibicc/blob/main/codegen.c): `codegen.c`

### Other components

- **C Developer**
- **C Source**
- **Assembly Output**
- **System Linker**
- **Executable**

## Connections

- C Developer → C Source: writes
- C Source → Compiler Driver: supplies
- Compiler Driver → Tokenizer: dispatches
- Tokenizer → UTF-8 Support: decodes UTF-8
- Tokenizer → Preprocessor: emits tokens
- C11 Headers → Preprocessor: provides macros
- Preprocessor → C Parser: expands tokens
- Compiler Driver → C Parser: invokes parser
- C Parser → Type System: adds types
- C Parser → Typed AST: constructs AST
- Type System → Typed AST: annotates AST
- C Parser → C11 Features: handles features
- Compiler Driver → Code Generator: invokes codegen
- Typed AST → Code Generator: provides AST
- Code Generator → x86-64 ABI: applies ABI
- Code Generator → Assembly Output: emits assembly
- Compiler Driver → System Linker: runs linker
- Assembly Output → System Linker: links input
- System Linker → Executable: produces executable
