MCObjectStreamer

Two implementations of MCStreamer:

  • MCAsmStreamer is a straightforward impl that prints out a directive for each method. (e.g EmitValue -> .byte)
  • MCObjectStreamer implements a full assembler.

    // Inheritance relations
    
    MCStreamer (contain an instance of `MCTargetStreamer` as member `TargetStreamer`)
    -> MCAsmStreamer (final)
    -> MCObjectStreamer
    -> MCELFStreamer
       -> MipsELFStreamer
    -> MCWasmStreamer
    
    // MCTargetStreamer for directives.
    
    // llvm/include/llvm/MCStreamer.h
    MCTargetStreamer (contain an instance `MCStreamer` as member `Streamer`)
    -> ARMTargetStreamer // include/llvm/MCStreamer.h
    -> MipsTargetStreamer // lib/Target/Mips/MipsTargetStreamer.h
    -> MipsTargetAsmStreamer // lib/Target/Mips/MipsTargetStreamer.h
    -> MipsTargetELFStreamer // lib/Target/Mips/MipsTargetStreamer.h
    -> RISCVTargetStreamer // lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
    

MipsAsmPrinter::EmitInstruction() acts as a driver to emit an instruction. It will finally calls the MCObjectStreamer to write object (e.g. .o) files or MCAsmStreamer to write assembly (e.g .s) files.

tracking `MipsAsmPrinter::EmitInstruction()`

MCObjectStreamer::EmitInstruction() will:

  1. call parent method MCStreamer::EmitInstruction(Inst, STI); (to do what??? by visitXXXX)
  2. call a child method MCELFStreamer::EmitInstToData() to emit the instruction as a piece of “data” in the .text section.
  3. MCELFStreamer::EmitInstToData() will call Assembler.getEmitter().encodeInstruction() to encode the instruction and store it in as SmallString<256> Code.
  4. Assembler.getEmitter().encodeInstruction() is implemented by target arch in . For example in MipsMCCodeEmitter::encodeInstruction(). This is where the instruction in binary form got emitted to a streamer: EmitInstruction(Binary, Size, STI, OS);
    • Binary is got via Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI)
    • getBinaryCodeForInstr() is a TableGen’erated function for getting the binary encoding for an instruction.
tracking MCObjectStreamer::EmitInstruction()
Created Jul 19, 2020 // Last Updated Aug 16, 2020

If you could revise
the fundmental principles of
computer system design
to improve security...

... what would you change?