How tpde2/tpde works
Project Type and Purpose
• This is a compiler back-end framework (TPDE) plus two supporting components:
• TPDE-Encodegen: generates instruction encoders from LLVM Machine IR.
• TPDE-LLVM: a drop-in LLVM back-end that compiles LLVM IR much faster than LLVM-O0.
• It targets JIT and unoptimized builds on ELF x86-64 and AArch64.
High-Level File Structure Analysis
• Top-level modules:
• deps/ (third-party libraries)
• docs/ (user and internals documentation)
• tpde/ (core framework: IR adaptor, analysis, assembler, ELF mapper)
• tpde-encodegen/ (template-based code generator scaffold)
• tpde-llvm/ (LLVM adaptor, JIT mapper, tools: tpde-llc, tpde-lli)
• tests/ & tpde/test/ & tpde-llvm/test/ (unit and integration tests)
• tools/ (small helper scripts, coverage, CI)
• CMakeLists.txt orchestrates multi-module build, selects targets for each subproject.
Key Architectural Components
A. Front-end IR Adaptor
• IRAdaptor.hpp / LLVMAdaptor.cpp in tpde and tpde-llvm
• Takes incoming SSA IR or LLVM IR and translates to TPDE’s generic IR model.
B. Analyzer & Register-Allocator
• Analyzer.hpp, util structures (liveness, RPO, etc.)
• Produces basic block order, liveness info, and allocation constraints.
C. Code Generator (Core)
• CompilerBase.hpp, Compiler.hpp and platform-specific CompilerX64/A64
• Uses templates (from tpde-encodegen) or hand-written emitter to select and emit machine instructions.
D. Assembler & ELF Mapper
• AssemblerElf*.hpp/.cpp, ElfMapper.cpp
• Converts instruction streams into binary sections, symbol tables, relocation entries.
E. TPDE-Encodegen Utility
• Reads LLVM Machine IR → emits C++ template code (EncCompilerTemplate.hpp) for each target.
• Supplies platform encoder modules (arm64, x64) to core.
F. TPDE-LLVM Adaptor & Tools
• JITMapper: links with core to JIT-compile at runtime.
• tpde-llc / tpde-lli CLI wrappers around LLVMAdaptor + Compiler – emit object code or execute IR.
Component Interactions and Data Flow
1. Input Stage:
• External SSA IR or LLVM IR
→ IR Adaptor (tpde/include/tpde/IRAdaptor.hpp or tpde-llvm/src/LLVMAdaptor.cpp)
2. Analysis & Allocation:
→ Analyzer (compute CFG, liveness) → Register Allocator → ValueAssignment
3. Code Generation:
→ CompilerBase + platform-specific CompilerX64/A64 (tpde/src/x64 & arm64)
→ Use templates from tpde-encodegen for instruction encoding
4. Assembly & Linking:
→ AssemblerElf (emit .text, .data, relocations)
→ ElfMapper (pack into ELF object or in-memory JIT image)
5. Tooling Integration (TPDE-LLVM):
• For LLVM IR: LLVMAdaptor calls into TPDE core pipeline
• JITMapper for in-process execution
• CLI wrappers: tpde-llc (codegen to file), tpde-lli (interpret with JIT)
Architectural Patterns and Principles
• Layered Architecture: clear separation between IR Adaptor → Analyzer → CodeGen → Assembler → ELF.
• Plug-in / Template-driven codegen: tpde-encodegen decouples instruction definitions from core logic.
• Modular targets: x64 and A64 share common core, override only necessary methods.
• CMake multi-project build: each submodule builds into its own library or tool.