# realnegate/cuik architecture

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

- Interactive diagram: https://gitdiagram.com/realnegate/cuik
- Repository: https://github.com/realnegate/cuik

## Overview

##### Main Components

a) Frontend Layer:
- cuik_c (C11 Frontend)
- cuik_pp (Preprocessor)
- Lexer, parser, and semantic analysis components

b) Middle Layer:
- IR Generation (shown in cuik_c/ir_gen*)
- Optimization Pipeline (tb/opt/*)

c) Backend Layer (tb):
- Multiple target architectures (x64, aarch64, MIPS, WASM)
- Code generation
- Register allocation
- Object file generation

d) Linker Layer:
- Support for different formats (ELF, COFF, PE)
- Archive handling

##### Component Relationships

**Source Code → Preprocessor → Frontend → IR → Optimizer → Backend → Object Files → Linker → Executable**

##### Specific Elements

a) Frontend Components:
- Lexical Analysis
- Parsing
- Semantic Analysis
- Symbol Table Management
- AST Generation

b) Middle-end Components:
- IR Generation
- Optimization Passes (show major ones):
- GVN (Global Value Numbering)
- CFG Optimization
- Memory-to-Register Promotion
- Loop Optimization
- Instruction Scheduling

c) Backend Components:
- Target-specific Code Generation
- Register Allocation
- Instruction Selection
- Object File Generation

##### Important Details to Highlight

- Modular architecture allowing separate compilation of components
- Multiple target architecture support
- Different object file format support
- Optimization pipeline structure
- Cross-platform capabilities

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef frontend fill:#b3d9ff,stroke:#333
    classDef middleend fill:#b3ffb3,stroke:#333
    classDef backend fill:#ffcc99,stroke:#333
    classDef linker fill:#e6b3ff,stroke:#333
    classDef storage fill:#f2f2f2,stroke:#333
    
    %% Source and Output
    SRC[Source Code]:::storage
    EXE[Executable]:::storage
    
    %% Frontend Layer
    subgraph Frontend
        PP[Preprocessor]:::frontend
        LEX[Lexical Analysis]:::frontend
        PAR[Parser]:::frontend
        SEM[Semantic Analysis]:::frontend
        AST[AST Generation]:::frontend
        SYMTAB[Symbol Table]:::frontend
    end
    
    %% Middle Layer
    subgraph MiddleEnd
        IR[IR Generation]:::middleend
        subgraph Optimizations
            GVN[Global Value Numbering]:::middleend
            CFG[CFG Optimization]:::middleend
            M2R[Memory-to-Register]:::middleend
            LOOP[Loop Optimization]:::middleend
            SCHED[Instruction Scheduling]:::middleend
        end
    end
    
    %% Backend Layer
    subgraph Backend
        CODEGEN[Code Generation]:::backend
        REGALLOC[Register Allocation]:::backend
        subgraph Targets
            X64[x64]:::backend
            ARM[AArch64]:::backend
            MIPS[MIPS]:::backend
            WASM[WebAssembly]:::backend
        end
        OBJ[Object Files]:::backend
    end
    
    %% Linker Layer
    subgraph Linker
        ARCH[Archive Handler]:::linker
        subgraph Formats
            ELF[ELF Linker]:::linker
            PE[PE Linker]:::linker
            COFF[COFF Support]:::linker
        end
    end
    
    %% Relationships
    SRC --> PP --> LEX --> PAR --> SEM --> AST
    AST --> IR
    SYMTAB -.-> SEM
    
    IR --> GVN & CFG & M2R & LOOP & SCHED
    GVN & CFG & M2R & LOOP & SCHED --> CODEGEN
    
    CODEGEN --> REGALLOC
    REGALLOC --> X64 & ARM & MIPS & WASM
    X64 & ARM & MIPS & WASM --> OBJ
    
    OBJ --> ARCH
    ARCH --> ELF & PE & COFF
    ELF & PE & COFF --> EXE
    
    %% Click Events
    click PP "https://github.com/RealNeGate/Cuik/tree/master//cuik_pp/"
    click LEX "https://github.com/RealNeGate/Cuik/blob/master//cuik_pp/lexer.c"
    click PAR "https://github.com/RealNeGate/Cuik/blob/master//cuik_c/parser.c"
    click SEM "https://github.com/RealNeGate/Cuik/blob/master//cuik_c/sema.c"
    click IR "https://github.com/RealNeGate/Cuik/blob/master//cuik_c/ir_gen.c"
    click GVN "https://github.com/RealNeGate/Cuik/blob/master//tb/opt/gvn.h"
    click CFG "https://github.com/RealNeGate/Cuik/blob/master//tb/opt/cfg.h"
    click M2R "https://github.com/RealNeGate/Cuik/blob/master//tb/opt/mem2reg.h"
    click LOOP "https://github.com/RealNeGate/Cuik/blob/master//tb/opt/loop.h"
    click SCHED "https://github.com/RealNeGate/Cuik/blob/master//tb/opt/list_sched.h"
    click X64 "https://github.com/RealNeGate/Cuik/tree/master//tb/x64/"
    click ARM "https://github.com/RealNeGate/Cuik/tree/master//tb/aarch64/"
    click MIPS "https://github.com/RealNeGate/Cuik/tree/master//tb/mips/"
    click WASM "https://github.com/RealNeGate/Cuik/tree/master//tb/wasm/"
    click REGALLOC "https://github.com/RealNeGate/Cuik/blob/master//tb/briggs_ra.c"
    click CODEGEN "https://github.com/RealNeGate/Cuik/blob/master//tb/codegen.c"
    click ELF "https://github.com/RealNeGate/Cuik/blob/master//linker/elf_linker.c"
    click PE "https://github.com/RealNeGate/Cuik/blob/master//linker/pe_linker.c"
    click ARCH "https://github.com/RealNeGate/Cuik/blob/master//linker/archives.c"
    click COFF "https://github.com/RealNeGate/Cuik/blob/master//tb/objects/coff.c"
```
