# inside-compiler/inside-llvm-code-gen architecture

> Architecture diagram of the GitHub repository inside-compiler/inside-llvm-code-gen, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/inside-compiler/inside-llvm-code-gen
- Repository: https://github.com/inside-compiler/inside-llvm-code-gen

## Overview

##### Project type and purpose
- This is an educational “compiler code-generation” project accompanying the Chinese book “深入理解LLVM代码生成” (“Deep Understanding of LLVM Code Generation”).
- It contains sample programs (.c), LLVM IR files (.ll), Machine IR (.mir), machine code files (.mc/.s), and disassemblies, organized per chapter (ch1, ch2, …, ch12).
- Its goal is to walk the reader through each stage of LLVM’s codegen pipeline, from C source through to object code.

##### File structure
- Top-level:
- A/ – Misc. examples
- ch1…ch12/ – Chapter-by-chapter code samples
- README.md – Project overview, points to the LLVM fork (inside-compiler/llvm-project, release/15.x)
- Each chapter directory contains:
- .c – C source input to Clang
- .ll – LLVM IR produced by Clang
- .mir – LLVM “Machine IR” after instruction selection
- .mc, .s – Target assembly output
- .disassembly/.log – Disassembler output or logs for verification
- There are no web-frontend or database files; this is a language/compiler project.

##### README for additional insights
- Confirms this is companion code for the LLVM code-generation book.
- References the LLVM project repo and version.

##### Detailed capture of essential elements
- External dependency: the inside-compiler/llvm-project repo version at the top corner.
- Chapter as a sub-module or annotation around the relevant stage (“ch10 examples illustrate MC layer”).
- Don’t overcrowd: group chapters by stage (e.g., chapters 1–3 under “Frontend”, 4–6 under “Mid-end IR”, 7–9 under “Machine IR”, 10–12 under “Assembly/Disassembly”).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Project Metadata
    subgraph "Project Overview"
        MD["README.md"]:::meta
        DR["dialect.drawio"]:::meta
    end

    %% Compiler Pipeline
    subgraph "Frontend Stage"
        FE["Clang Frontend\n(.c → AST → .ll)"]:::frontend
    end

    subgraph "Mid-end Optimizer"
        OPT["LLVM IR Optimizer (opt)\n(.ll → .ll)"]:::midend
    end

    subgraph "Back-end Instruction Selection"
        IS["Instruction Selector (llc)\n(.ll → .mir)"]:::backend
    end

    subgraph "Machine Code Emitter"
        MC["MC Emitter (llc)\n(.mir → .s/.mc)"]:::backend
    end

    subgraph "Assembler & Linker"
        AL["Assembler & Linker\n(.s/.mc → .o → exe)"]:::backend
    end

    subgraph "Disassembly / Verification"
        DIS["Disassembler / objdump\n(.o → .disassembly/.log)"]:::verification
    end

    %% Dataflow
    MD --> DR
    DR --> FE
    FE -->|"clang"| OPT
    OPT -->|"opt passes"| IS
    IS -->|"llc -filetype=asm"| MC
    MC -->|"as / ld"| AL
    AL -->|"llvm-objdump"| DIS

    %% Click Events
    click MD "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/README.md"
    click DR "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/dialect.drawio"
    click FE "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/A/A-1.c"
    click OPT "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/A/A-2.ll"
    click IS "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/ch7/7-11.mir"
    click MC "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/A/A-10.mc"
    click AL "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/ch11/11-2.s"
    click DIS "https://github.com/inside-compiler/inside-llvm-code-gen/blob/main/ch7/7-9.log"

    %% Styles
    classDef meta fill:#f9f,stroke:#333,stroke-width:1px;
    classDef frontend fill:#bbf,stroke:#225,stroke-width:1px;
    classDef midend fill:#bfb,stroke:#252,stroke-width:1px;
    classDef backend fill:#ffb,stroke:#522,stroke-width:1px;
    classDef verification fill:#fbb,stroke:#522,stroke-width:1px;
```
