# 8l/qbe architecture

> Architecture diagram of the GitHub repository 8l/qbe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/8l/qbe
- Repository: https://github.com/8l/qbe

## Overview

##### Main Components Structure

a) Core Compiler Pipeline Components:
- Parser (parse.c)
- SSA Form Handler (ssa.c)
- Live Variable Analysis (live.c)
- Register Allocator (rega.c)
- Code Emitter (emit.c)

b) Target Architecture Components:
- AMD64 Backend
- ARM64 Backend

**Parse → SSA Optimization → Live Analysis → Register Allocation → Code Emission**

##### Additional Elements

Annotations for:
- File formats (.ssa files)
- ABI interfaces
- Testing framework integration points

##### Detailed Component Breakdown

For each major component, show:
- Input/Output formats
- Key algorithms used
- Interface points with other components

##### Implementation Notes

- The modular design between target-independent and target-specific code
- The SSA-based optimization pipeline
- The clean separation between different compilation phases
- The testing infrastructure integration

##### Scale and Detail Level

Maintain these levels of detail:
- L1: High-level component interaction
- L2: Individual phase internals
- L3: Target-specific implementation details

- Frontend interface (SSA input)
- Middle-end optimizations
- Backend code generation
- Target-specific code emission

Remember to include the test framework (test/) and tools (tools/) as supporting components, but keep them visually separate from the main compilation pipeline.

This architecture should reflect QBE's nature as a backend compiler with multiple target support and a focus on SSA-based optimization.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Input
        SSA[SSA IR Files]
    end

    subgraph "Core Pipeline"
        Parser["Parser"]:::core
        SSAHandler["SSA Form Handler"]:::core
        LiveAnalysis["Live Variable Analysis"]:::core
        RegAlloc["Register Allocator"]:::core
        CodeEmit["Code Emitter"]:::core
        
        Parser --> SSAHandler
        SSAHandler --> LiveAnalysis
        LiveAnalysis --> RegAlloc
        RegAlloc --> CodeEmit
    end

    subgraph "Optimizations"
        ConstFold["Constant Folding"]:::opt
        CopyProp["Copy Propagation"]:::opt
        CFG["Control Flow Graph"]:::opt
        
        ConstFold --> SSAHandler
        CopyProp --> SSAHandler
        CFG --> SSAHandler
    end

    subgraph "Target Architectures"
        direction TB
        subgraph "AMD64"
            AMD64Isel["Instruction Selection"]:::amd
            AMD64Emit["Code Generation"]:::amd
            AMD64Sysv["System V ABI"]:::amd
            
            AMD64Isel --> AMD64Emit
            AMD64Sysv --> AMD64Emit
        end
        
        subgraph "ARM64"
            ARM64Isel["Instruction Selection"]:::arm
            ARM64Emit["Code Generation"]:::arm
            ARM64Abi["ABI Implementation"]:::arm
            
            ARM64Isel --> ARM64Emit
            ARM64Abi --> ARM64Emit
        end
    end

    subgraph "Support Systems"
        MemMgmt["Memory Management"]:::support
        TestFw["Testing Framework"]:::support
        Tools["Testing Tools"]:::support
        Docs["Documentation"]:::support
    end

    SSA --> Parser
    CodeEmit --> AMD64Isel
    CodeEmit --> ARM64Isel
    AMD64Emit --> Output
    ARM64Emit --> Output

    subgraph Output
        Output[Assembly Code]
    end

    %% Click events for component mapping
    click Parser "https://github.com/8l/qbe/blob/master/parse.c"
    click SSAHandler "https://github.com/8l/qbe/blob/master/ssa.c"
    click LiveAnalysis "https://github.com/8l/qbe/blob/master/live.c"
    click RegAlloc "https://github.com/8l/qbe/blob/master/rega.c"
    click CodeEmit "https://github.com/8l/qbe/blob/master/emit.c"
    click AMD64Isel "https://github.com/8l/qbe/blob/master/amd64/isel.c"
    click AMD64Emit "https://github.com/8l/qbe/blob/master/amd64/emit.c"
    click AMD64Sysv "https://github.com/8l/qbe/blob/master/amd64/sysv.c"
    click ARM64Isel "https://github.com/8l/qbe/blob/master/arm64/isel.c"
    click ARM64Emit "https://github.com/8l/qbe/blob/master/arm64/emit.c"
    click ARM64Abi "https://github.com/8l/qbe/blob/master/arm64/abi.c"
    click MemMgmt "https://github.com/8l/qbe/blob/master/mem.c"
    click ConstFold "https://github.com/8l/qbe/blob/master/fold.c"
    click CopyProp "https://github.com/8l/qbe/blob/master/copy.c"
    click CFG "https://github.com/8l/qbe/blob/master/cfg.c"
    click TestFw "https://github.com/8l/qbe/tree/master/test/"
    click Tools "https://github.com/8l/qbe/tree/master/tools/"
    click Docs "https://github.com/8l/qbe/tree/master/doc/"

    %% Styling
    classDef core fill:#2374ab
    classDef opt fill:#ff7f50
    classDef amd fill:#57a773
    classDef arm fill:#449dd1
    classDef support fill:#grey

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Optimizations"]:::opt
        L3["AMD64 Components"]:::amd
        L4["ARM64 Components"]:::arm
        L5["Support Systems"]:::support
    end
```
