1 //===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the BPF assembly language.
13 //===----------------------------------------------------------------------===//
16 #include "BPFInstrInfo.h"
17 #include "BPFMCInstLower.h"
18 #include "BPFTargetMachine.h"
20 #include "InstPrinter/BPFInstPrinter.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "asm-printer"
37 class BPFAsmPrinter : public AsmPrinter {
39 explicit BPFAsmPrinter(TargetMachine &TM,
40 std::unique_ptr<MCStreamer> Streamer)
41 : AsmPrinter(TM, std::move(Streamer)) {}
43 StringRef getPassName() const override { return "BPF Assembly Printer"; }
44 bool doInitialization(Module &M) override;
45 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
46 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
47 unsigned AsmVariant, const char *ExtraCode,
48 raw_ostream &O) override;
49 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
50 unsigned AsmVariant, const char *ExtraCode,
51 raw_ostream &O) override;
53 void EmitInstruction(const MachineInstr *MI) override;
57 bool BPFAsmPrinter::doInitialization(Module &M) {
58 AsmPrinter::doInitialization(M);
60 if (MAI->doesSupportDebugInformation()) {
61 Handlers.push_back(HandlerInfo(new BTFDebug(this), "emit",
62 "Debug Info Emission", "BTF",
69 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
71 const MachineOperand &MO = MI->getOperand(OpNum);
73 switch (MO.getType()) {
74 case MachineOperand::MO_Register:
75 O << BPFInstPrinter::getRegisterName(MO.getReg());
78 case MachineOperand::MO_Immediate:
82 case MachineOperand::MO_MachineBasicBlock:
83 O << *MO.getMBB()->getSymbol();
86 case MachineOperand::MO_GlobalAddress:
87 O << *getSymbol(MO.getGlobal());
90 case MachineOperand::MO_BlockAddress: {
91 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
96 case MachineOperand::MO_ExternalSymbol:
97 O << *GetExternalSymbolSymbol(MO.getSymbolName());
100 case MachineOperand::MO_JumpTableIndex:
101 case MachineOperand::MO_ConstantPoolIndex:
103 llvm_unreachable("<unknown operand type>");
107 bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
108 unsigned /*AsmVariant*/,
109 const char *ExtraCode, raw_ostream &O) {
110 if (ExtraCode && ExtraCode[0])
111 return true; // BPF does not have special modifiers
113 printOperand(MI, OpNo, O);
117 bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
118 unsigned OpNum, unsigned AsmVariant,
119 const char *ExtraCode,
121 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
122 const MachineOperand &BaseMO = MI->getOperand(OpNum);
123 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
124 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
125 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
126 int Offset = OffsetMO.getImm();
129 return true; // Unknown modifier.
132 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
134 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
139 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
141 BPFMCInstLower MCInstLowering(OutContext, *this);
144 MCInstLowering.Lower(MI, TmpInst);
145 EmitToStreamer(*OutStreamer, TmpInst);
148 // Force static initialization.
149 extern "C" void LLVMInitializeBPFAsmPrinter() {
150 RegisterAsmPrinter<BPFAsmPrinter> X(getTheBPFleTarget());
151 RegisterAsmPrinter<BPFAsmPrinter> Y(getTheBPFbeTarget());
152 RegisterAsmPrinter<BPFAsmPrinter> Z(getTheBPFTarget());