Repository navigation
MC Layer 01 overview
Relevant source files
The following files were used as context for generating this wiki page:
This wiki explains llvm/lib/MC/ and the per-target MCTargetDesc, AsmParser and Disassembler directories, using AArch64 as the worked example. It runs from the basic data structures to everything a new backend has to write: see Writing an MC Layer for a Custom Backend.
It does not cover instruction selection or register allocation (see the other two wikis), or the linker.
All tool output on these pages was produced by the release llc built from the pinned commit. That build has no llvm-mc, llvm-objdump or llvm-readelf, so assembling .s files, disassembly and reading relocation tables back from objects are explained from the source code only, without listings.
flowchart LR
MI["MachineInstr<br/>after register allocation"] --> AP["AsmPrinter<br/>lowers each MI to an MCInst"]
AP --> S{"MCStreamer"}
S -->|"-filetype=asm"| AS["MCAsmStreamer<br/>MCInstPrinter prints text"]
S -->|"-filetype=obj"| OS["MCELFStreamer / MCObjectStreamer<br/>MCCodeEmitter encodes bytes"]
OS --> ASM["MCAssembler<br/>layout, relaxation, fixups"]
ASM --> OW["ELFObjectWriter<br/>sections, symbols, relocations"]
P[".s file"] --> PARSER["AsmParser<br/>MCTargetAsmParser"] --> S
BYTES["bytes"] --> DIS["MCDisassembler"] --> MCI["MCInst"] --> PR["MCInstPrinter"]
The MCStreamer is the hub. Everything that produces code (the compiler's AsmPrinter, the assembly parser) talks to a streamer, and the kind of streamer decides whether text or an object file comes out. The same MCInst can be printed or encoded, which is why llc can emit both from one pipeline.
CodeGenTargetMachineImpl::addAsmPrinter creates the streamer, then the AsmPrinter pass. createMCStreamer chooses by file type: getTarget().createAsmStreamer(...) for text, getTarget().createMCObjectStreamer(...) for objects.
AsmPrinter lowers each MachineInstr to an MCInst (AArch64: AArch64AsmPrinter::emitInstruction, using AArch64MCInstLower::lowerOperand) and hands it to the streamer through EmitToStreamer.
Sources: llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp:145-215, llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:505, llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp:3258-3265, llvm/lib/Target/AArch64/AArch64MCInstLower.cpp:332
@gv = global i32 0
declare i32 @g(i32)
define i32 @h(i32 %a) {
%v = load i32, ptr @gv
%x = call i32 @g(i32 %v)
%y = add i32 %x, %a
ret i32 %y
}llc -mtriple=aarch64-linux-gnu -O2 -show-mc-encoding prints the assembly, the bytes the code emitter produced, and any fixups (excerpt):
adrp x8, :got:gv // encoding: [0x08'A',A,A,0x90'A']
// fixup A - offset: 0, value: :got:gv, kind: fixup_aarch64_pcrel_adrp_imm21
mov w19, w0 // encoding: [0xf3,0x03,0x00,0x2a]
bl g // encoding: [A,A,A,0b100101AA]
// fixup A - offset: 0, value: g, kind: fixup_aarch64_pcrel_call26
add w0, w0, w19 // encoding: [0x00,0x00,0x13,0x0b]
ret // encoding: [0xc0,0x03,0x5f,0xd6]
mov w19, w0 is fully known: four bytes. bl g is not: the address of g is unknown when the instruction is encoded. The emitter writes the opcode bits it knows and records a fixup (bits marked A), which is resolved later by the assembler or turned into a relocation for the linker. That distinction runs through the whole MC layer; see Fixups and Relocations.
| Component | Class | Role | Page |
|---|---|---|---|
| Instruction |
MCInst, MCOperand
|
Opcode + operands, target-independent container | Data Structures |
| Expressions, symbols, sections |
MCExpr, MCSymbol, MCSection, MCContext
|
Values not known yet, names, output sections | Data Structures |
| Streamer |
MCStreamer, MCAsmStreamer, MCObjectStreamer, MCELFStreamer
|
The interface code producers call | Streamers |
| Assembler |
MCAssembler, fragments |
Layout, relaxation, fixup evaluation | Assembler and Layout |
| Object writer | ELFObjectWriter |
Writes the ELF file and relocations | Fixups and Relocations |
| Target: description |
MCInstrInfo, MCRegisterInfo, MCSubtargetInfo, MCAsmInfo
|
Tables and syntax facts | Custom backend |
| Target: printer | <T>InstPrinter |
MCInst to text |
MCAsmInfo and Printer |
| Target: encoder | <T>MCCodeEmitter |
MCInst to bytes + fixups |
Code Emitter |
| Target: backend |
<T>AsmBackend, <T>ELFObjectWriter
|
Apply fixups, relax, choose relocation types | AsmBackend and Object Writer |
| Target: parser, decoder |
<T>AsmParser, <T>Disassembler
|
Text to MCInst, bytes to MCInst
|
Parser and Disassembler |
- New to MC: Data Structures, Streamers, Assembler and Layout, Fixups and Relocations.
- Writing a backend: Custom backend and its checklist.
- Debugging output: Tooling and Glossary.
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look