# tinygo-org/tinygo architecture

> Architecture diagram of the GitHub repository tinygo-org/tinygo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tinygo-org/tinygo
- Repository: https://github.com/tinygo-org/tinygo

## Overview

##### Main Components of the System

a. Core Compiler Components:
- Compiler Frontend (compiler/)
- LLVM Backend Integration (compiler/llvm.go)
- Builder System (builder/)
- Transformation Pipeline (transform/)
- Loader (loader/)

b. Runtime Components:
- Runtime System (src/runtime/)
- Garbage Collectors (runtime/gc_*.go)
- Scheduler Implementations (runtime/scheduler_*.go)

c. Target Support:
- Microcontroller Targets (targets/*.json)
- WebAssembly Targets (targets/wasm*.json)
- Operating System Targets

d. Standard Library Implementation:
- Machine Package (src/machine/)
- Runtime Package (src/runtime/)
- Other Standard Packages (src/*)

**┌─────────────────────────────────────────────────────────┐**
- TinyGo Compiler Frontend               │
- ┬─────────────────────┬────────────────┤
- Go Parser     │    Type Checker     │  IR Generator  │
- ┬────────┴──────────┬──────────┴───────┬────────┘

**┌────────▼────────┐  ┌───────▼───────┐  ┌──────▼────────┐**
- Transformation │  │  Optimization  │  │  LLVM Backend │
- Pipeline    │──▶│     Passes    │──▶│  Integration  │
- ┬────────┘  └───────────────┘  └──────┬────────┘

**┌────────▼──────────────────────────────┐      │**
- Target Backends            │      │
- ┬─────────────┬──────────┤      │
- Embedded    │    WASM     │   OS     │      │
- (MCU)       │  (Browser/  │ (Linux/  │      │
- Server)   │  macOS)  │      │
- ┴─────────────┴──────────┘      │
- ▲                                     │
- ▼
**┌────────┴─────────────────────────────────────────────┐**
- Runtime System                       │
- ┬────────────────┬────────────────────┤
- GC Systems    │  Schedulers    │  Platform Support  │
- ┴────────────────┴────────────────────┘

##### Key Elements to Highlight

a. Compilation Pipeline:
- Flow from Go source code through various compilation stages
- Transformation passes
- Integration with LLVM backend

b. Target Support:
- Different target platforms
- Shared components vs. platform-specific components
- Build configuration flow

c. Runtime Features:
- Different garbage collector implementations
- Scheduler variants (cooperative, none, tasks)
- Platform-specific runtime support

##### Additional Details

Callouts for:
- Configuration system (compileopts/)
- Build system integration (builder/)
- Testing infrastructure (tests/)
- Standard library implementation details

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Frontend["TinyGo Compiler Frontend"]
        Parser["Go Parser"]:::compiler
        TypeCheck["Type Checker"]:::compiler
        IRGen["IR Generator"]:::compiler
    end

    subgraph Pipeline["Compilation Pipeline"]
        Transform["Transformation Pipeline"]:::transform
        Optimize["Optimization Passes"]:::transform
        LLVM["LLVM Backend Integration"]:::compiler
    end

    subgraph Targets["Target Backends"]
        MCU["Embedded (MCU)"]:::target
        WASM["WASM (Browser/Server)"]:::target
        OS["OS (Linux/macOS)"]:::target
    end

    subgraph Runtime["Runtime System"]
        GC["GC Systems"]:::runtime
        Scheduler["Schedulers"]:::runtime
        Platform["Platform Support"]:::runtime
    end

    %% Connections
    Parser --> TypeCheck
    TypeCheck --> IRGen
    IRGen --> Transform
    Transform --> Optimize
    Optimize --> LLVM
    LLVM --> MCU
    LLVM --> WASM
    LLVM --> OS
    MCU --> Platform
    WASM --> Platform
    OS --> Platform
    Platform --> GC
    Platform --> Scheduler

    %% Click Events
    click Parser "https://github.com/tinygo-org/tinygo/blob/release/compiler/compiler.go"
    click TypeCheck "https://github.com/tinygo-org/tinygo/blob/release/compiler/interface.go"
    click LLVM "https://github.com/tinygo-org/tinygo/blob/release/compiler/llvm.go"
    click Transform "https://github.com/tinygo-org/tinygo/blob/release/transform/transform.go"
    click GC "https://github.com/tinygo-org/tinygo/blob/release/src/runtime/gc_conservative.go"
    click Scheduler "https://github.com/tinygo-org/tinygo/blob/release/src/runtime/scheduler_cooperative.go"
    click Platform "https://github.com/tinygo-org/tinygo/blob/release/src/runtime/runtime.go"
    click MCU "https://github.com/tinygo-org/tinygo/blob/release/targets/cortex-m.json"
    click WASM "https://github.com/tinygo-org/tinygo/blob/release/targets/wasm.json"
    click OS "https://github.com/tinygo-org/tinygo/blob/release/targets/linux.json"

    %% Styles
    classDef compiler fill:#3498db,stroke:#2980b9,color:white
    classDef transform fill:#e74c3c,stroke:#c0392b,color:white
    classDef target fill:#f39c12,stroke:#d35400,color:white
    classDef runtime fill:#2ecc71,stroke:#27ae60,color:white

    %% Legend
    subgraph Legend
        L1["Compiler Components"]:::compiler
        L2["Transform Components"]:::transform
        L3["Target Components"]:::target
        L4["Runtime Components"]:::runtime
    end
```
