LLVM  9.0.0svn
BPFAsmPrinter.cpp
Go to the documentation of this file.
1 //===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a printer that converts from our internal representation
10 // of machine-dependent LLVM code to the BPF assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BPF.h"
15 #include "BPFInstrInfo.h"
16 #include "BPFMCInstLower.h"
17 #include "BPFTargetMachine.h"
18 #include "BTFDebug.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"
32 using namespace llvm;
33 
34 #define DEBUG_TYPE "asm-printer"
35 
36 namespace {
37 class BPFAsmPrinter : public AsmPrinter {
38 public:
39  explicit BPFAsmPrinter(TargetMachine &TM,
40  std::unique_ptr<MCStreamer> Streamer)
41  : AsmPrinter(TM, std::move(Streamer)) {}
42 
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  const char *ExtraCode, raw_ostream &O) override;
48  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
49  const char *ExtraCode, raw_ostream &O) override;
50 
51  void EmitInstruction(const MachineInstr *MI) override;
52 };
53 } // namespace
54 
55 bool BPFAsmPrinter::doInitialization(Module &M) {
57 
58  // Only emit BTF when debuginfo available.
59  if (MAI->doesSupportDebugInformation() && !empty(M.debug_compile_units())) {
60  Handlers.emplace_back(llvm::make_unique<BTFDebug>(this), "emit",
61  "Debug Info Emission", "BTF", "BTF Emission");
62  }
63 
64  return false;
65 }
66 
67 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
68  raw_ostream &O) {
69  const MachineOperand &MO = MI->getOperand(OpNum);
70 
71  switch (MO.getType()) {
74  break;
75 
77  O << MO.getImm();
78  break;
79 
81  O << *MO.getMBB()->getSymbol();
82  break;
83 
85  O << *getSymbol(MO.getGlobal());
86  break;
87 
89  MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
90  O << BA->getName();
91  break;
92  }
93 
95  O << *GetExternalSymbolSymbol(MO.getSymbolName());
96  break;
97 
100  default:
101  llvm_unreachable("<unknown operand type>");
102  }
103 }
104 
105 bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
106  const char *ExtraCode, raw_ostream &O) {
107  if (ExtraCode && ExtraCode[0])
108  return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
109 
110  printOperand(MI, OpNo, O);
111  return false;
112 }
113 
114 bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
115  unsigned OpNum, const char *ExtraCode,
116  raw_ostream &O) {
117  assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
118  const MachineOperand &BaseMO = MI->getOperand(OpNum);
119  const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
120  assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
121  assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
122  int Offset = OffsetMO.getImm();
123 
124  if (ExtraCode)
125  return true; // Unknown modifier.
126 
127  if (Offset < 0)
128  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
129  else
130  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
131 
132  return false;
133 }
134 
135 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
136 
137  BPFMCInstLower MCInstLowering(OutContext, *this);
138 
139  MCInst TmpInst;
140  MCInstLowering.Lower(MI, TmpInst);
141  EmitToStreamer(*OutStreamer, TmpInst);
142 }
143 
144 // Force static initialization.
145 extern "C" void LLVMInitializeBPFAsmPrinter() {
149 }
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
unsigned getReg() const
getReg - Returns the register number.
Address of indexed Jump Table for switch.
MachineBasicBlock reference.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned getNumOperands() const
Retuns the total number of operands.
Definition: MachineInstr.h:413
Name of external global symbol.
const char * getSymbolName() const
zlib style complession
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
Address of a global value.
const GlobalValue * getGlobal() const
MCSymbol * getSymbol(const GlobalValue *GV) const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
Address of a basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:209
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MachineOperand class - Representation of each machine instruction operand.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant...
int64_t getImm() const
This file contains support for writing BTF debug info.
static const char * getRegisterName(unsigned RegNo)
Representation of each machine instruction.
Definition: MachineInstr.h:63
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:255
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
iterator_range< debug_compile_units_iterator > debug_compile_units() const
Return an iterator for all DICompileUnits listed in this Module&#39;s llvm.dbg.cu named metadata node and...
Definition: Module.h:781
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Target & getTheBPFleTarget()
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void LLVMInitializeBPFAsmPrinter()
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:65
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Address of indexed Constant in Constant Pool.
Target & getTheBPFbeTarget()
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:415
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Target & getTheBPFTarget()