Skip to content

MC Layer 08 code emitter

Mugundan edited this page Nov 27, 2026 · 1 revision

Code Emitter

Relevant source files

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

The code emitter turns one `MCInst` into bytes. For every operand whose value is not known yet, it also produces a fixup. It never looks at addresses and never decides on relocations: it only says "these bits depend on this expression".

Purpose and Scope

What encodeInstruction has to do, what TableGen generates for you, and where fixups come from.

The Interface

virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl<char> &CB,
                               SmallVectorImpl<MCFixup> &Fixups,
                               const MCSubtargetInfo &STI) const = 0;

Append the bytes to CB and the fixups to Fixups. Fixup offsets are relative to the start of this instruction; the streamer shifts them (Streamers).

Sources: llvm/include/llvm/MC/MCCodeEmitter.h:35

AArch64's Implementation

void AArch64MCCodeEmitter::encodeInstruction(const MCInst &MI,
                                             SmallVectorImpl<char> &CB,

                                             SmallVectorImpl<MCFixup> &Fixups,
                                             const MCSubtargetInfo &STI) const {
  if (MI.getOpcode() == AArch64::TLSDESCCALL) {
    ...
    addFixup(Fixups, 0, MI.getOperand(0).getExpr(), Reloc);
    return;
  }

  if (MI.getOpcode() == AArch64::SPACE) {
    // SPACE just increases basic block size, in both cases no actual code.
    return;
  }

  uint64_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
  support::endian::write<uint32_t>(CB, Binary, llvm::endianness::little);
  ++MCNumEmitted; // Keep track of the # of mi's emitted.
}

There are three parts:

  1. Special opcodes. TLSDESCCALL emits no bytes, only a relocation on the next instruction. SPACE emits nothing.
  2. getBinaryCodeForInstr is generated by -gen-emitter (AArch64GenMCCodeEmitter.inc) from each instruction's bits<32> Inst field. It starts from the fixed opcode bits and ORs in each operand field.
  3. Writing the word little-endian. An AArch64 instruction is always 4 bytes.

Sources: llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCCodeEmitter.cpp:725-779, llvm/lib/Target/AArch64/CMakeLists.txt:22

Operand Encoders and Fixups

For each operand field, the generated code calls an encoder function: getMachineOpValue by default, or the function named in the operand's TableGen EncoderMethod. An encoder returns the field value if the operand is a register or immediate. If the operand is an expression, it adds a fixup and returns 0. For example, the branch-target encoder picks fixup_aarch64_pcrel_call26 for BL:

                                   ? AArch64::fixup_aarch64_pcrel_call26

That is the origin of the [A,A,A,0b100101AA] encoding of bl g: opcode bits known, 26 bits left at 0 with a fixup.

Hand-written post-encoders adjust the generated value where the ISA has quirks. Examples are fixMulHigh, which fills the unused Ra field of SMULH/UMULH with ones, and fixOneOperandFPComparison. They are named with PostEncoderMethod in TableGen.

Sources: llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCCodeEmitter.cpp:455, llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCCodeEmitter.cpp:751-777

Checking Your Emitter

llc -show-mc-encoding runs the emitter even for text output. Real AArch64 output with no fixups:

	mov	w19, w0                         // encoding: [0xf3,0x03,0x00,0x2a]
	add	w0, w0, w19                     // encoding: [0x00,0x00,0x13,0x0b]
	ret                                     // encoding: [0xc0,0x03,0x5f,0xd6]

Compare these bytes against the ISA manual for a handful of instructions per format. That catches most mistakes in Inst field definitions.

A Minimal Emitter (Illustrative)

// ToyMCCodeEmitter.cpp (illustrative, not LLVM source)
void ToyMCCodeEmitter::encodeInstruction(const MCInst &MI,
    SmallVectorImpl<char> &CB, SmallVectorImpl<MCFixup> &Fixups,
    const MCSubtargetInfo &STI) const {
  uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);   // generated
  support::endian::write<uint32_t>(CB, Bits, llvm::endianness::little);
}

unsigned ToyMCCodeEmitter::getBranchTargetOpValue(const MCInst &MI,
    unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
    const MCSubtargetInfo &STI) const {
  const MCOperand &MO = MI.getOperand(OpNo);
  if (MO.isImm())
    return MO.getImm();
  addFixup(Fixups, 0, MO.getExpr(), Toy::fixup_toy_branch24);
  return 0;
}

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