Skip to content

MC Layer 02 data structures

Mugundan edited this page Nov 27, 2026 · 1 revision

Core Data Structures

Relevant source files

The following files were used as context for generating this wiki page:

Six types carry almost everything through the MC layer: `MCInst` (an instruction), `MCOperand` (one of its operands), `MCExpr` (a value that may not be known yet), `MCSymbol` (a name), `MCSection` (an output section) and `MCContext` (the owner of all of them).

Purpose and Scope

What each type holds and how they relate. Fragments and fixups are on Assembler and Layout and Fixups and Relocations.

MCInst and MCOperand

An MCInst is an opcode number plus a list of MCOperands. It carries no basic block, no liveness, no flags like killed: those belong to MachineInstr. The opcode is the target enum generated by TableGen (AArch64::ADRP, AArch64::ORRWrs, ...).

An MCOperand is one of:

Kind Holds Example
Register An MCRegister Reg:X8
Immediate int64_t Imm:-2
Floating-point immediate uint64_t bit pattern FP constants
Expression const MCExpr * Expr::got:gv (a symbol reference)
Instruction const MCInst * Bundles

llc -asm-show-inst prints the MCInst behind each line. Real output for the example on Overview:

// <MCInst #7672 STPXpre
//  <MCOperand Reg:SP>
//  <MCOperand Reg:LR>
//  <MCOperand Reg:X19>
//  <MCOperand Reg:SP>
//  <MCOperand Imm:-2>>
	adrp	x8, :got:gv                     // <MCInst #1865 ADRP
                                        //  <MCOperand Reg:X8>
                                        //  <MCOperand Expr::got:gv>>
	mov	w19, w0                         // <MCInst #5666 ORRWrs
                                        //  <MCOperand Reg:W19>
                                        //  <MCOperand Reg:WZR>

Three things to notice:

  • Operands follow the TableGen operand list, not the assembly text. stp x30, x19, [sp, #-16]! is STPXpre with five operands, including SP twice (the written-back base is a result, then the base is an input). The immediate is -2, not -16: it is stored scaled by 8, as the encoding needs.
  • Aliases are a printer concern. mov w19, w0 is really ORRWrs w19, wzr, w0, 0. The printer chooses the alias; the MCInst is the canonical instruction.
  • Unknown values are expressions. The adrp operand is an MCExpr (:got:gv), not a number. The code emitter turns it into a fixup.

Sources: llvm/lib/MC/MCInst.cpp

MCExpr

MCExpr represents a value computed at assembly or link time:

Class Meaning
MCConstantExpr A number
MCSymbolRefExpr A symbol (g, .LBB0_2)
MCBinaryExpr A + B, A - B, ...
MCUnaryExpr -A, ~A
MCSpecifierExpr / target subclasses A target modifier around an expression, e.g. AArch64 :got:, :lo12:, :gottprel:

When the assembler needs the value, it evaluates the expression to an MCValue = SymA - SymB + Constant plus a specifier (the target modifier). If SymA is defined in the same section and the fixup is resolvable, the value is computed and patched in. Otherwise a relocation is emitted. The specifier is what makes :got:gv become an R_AARCH64_ADR_GOT_PAGE relocation rather than a plain address (see Fixups and Relocations).

Sources: llvm/lib/MC/MCExpr.cpp, llvm/lib/MC/MCValue.cpp

MCSymbol

A symbol is a name with, eventually, a location (a fragment and an offset). Kinds you will meet:

Kind Example Notes
Function or global h, gv Goes into the object's symbol table
Temporary .Lfunc_end0, .LBB0_2 Private prefix (.L on ELF, MCAsmInfo::PrivateGlobalPrefix); usually not in the symbol table
Undefined g Referenced but defined elsewhere; always needs a relocation

On ELF, MCSymbolELF also stores binding (local, global, weak), type (function, object, TLS) and visibility. AArch64ELFObjectWriter::getRelocType even sets STT_TLS on symbols used through TLS specifiers.

MCSection

An MCSection (on ELF, MCSectionELF: name, type, flags) is a list of fragments. Fragment kinds are declared in MCSection.h:

    FT_Data,
    FT_Relaxable,
    FT_Align,
    FT_PrefAlign,
    FT_Fill,
    FT_LEB,
    FT_Nops,
    FT_Org,
    FT_Dwarf,
    FT_DwarfFrame,
    ...

Most code lives in FT_Data fragments (bytes plus fixups). An instruction that may need a longer encoding later gets its own FT_Relaxable fragment. Alignment, fills and variable-length DWARF data have their own kinds because their size depends on layout. See Assembler and Layout.

Sources: llvm/include/llvm/MC/MCSection.h:53-67

MCContext

MCContext owns every symbol, section and expression of one compilation and hands out unique temporaries (createTempSymbol). It also holds pointers to the target's MCAsmInfo, MCRegisterInfo and MCSubtargetInfo, and collects errors (reportError). Everything in MC is allocated in it, so objects are never freed one by one.

Sources: llvm/lib/MC/MCContext.cpp

Target Description Objects

Object Generated from Gives
MCInstrInfo <T>InstrInfo.td Per-opcode MCInstrDesc: size, operand list, flags (branch, call, may load)
MCRegisterInfo <T>RegisterInfo.td Register names, sub-registers, register units, DWARF numbers
MCSubtargetInfo <T>.td processors and features CPU and feature bits (which instructions are legal)
MCAsmInfo Hand-written Syntax and object-format facts: comment string, directives, alignment rules

The first three are mostly TableGen output; MCAsmInfo is written by hand. All are created through TargetRegistry (see Custom backend).

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally