LLVM  8.0.1
BPFAsmPrinter.cpp
Go to the documentation of this file.
1 //===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the BPF assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "BPF.h"
16 #include "BPFInstrInfo.h"
17 #include "BPFMCInstLower.h"
18 #include "BPFTargetMachine.h"
19 #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  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;
52 
53  void EmitInstruction(const MachineInstr *MI) override;
54 };
55 } // namespace
56 
57 bool BPFAsmPrinter::doInitialization(Module &M) {
59 
60  if (MAI->doesSupportDebugInformation()) {
61  Handlers.push_back(HandlerInfo(new BTFDebug(this), "emit",
62  "Debug Info Emission", "BTF",
63  "BTF Emission"));
64  }
65 
66  return false;
67 }
68 
69 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
70  raw_ostream &O) {
71  const MachineOperand &MO = MI->getOperand(OpNum);
72 
73  switch (MO.getType()) {
76  break;
77 
79  O << MO.getImm();
80  break;
81 
83  O << *MO.getMBB()->getSymbol();
84  break;
85 
87  O << *getSymbol(MO.getGlobal());
88  break;
89 
91  MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
92  O << BA->getName();
93  break;
94  }
95 
97  O << *GetExternalSymbolSymbol(MO.getSymbolName());
98  break;
99 
102  default:
103  llvm_unreachable("<unknown operand type>");
104  }
105 }
106 
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
112 
113  printOperand(MI, OpNo, O);
114  return false;
115 }
116 
117 bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
118  unsigned OpNum, unsigned AsmVariant,
119  const char *ExtraCode,
120  raw_ostream &O) {
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();
127 
128  if (ExtraCode)
129  return true; // Unknown modifier.
130 
131  if (Offset < 0)
132  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
133  else
134  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
135 
136  return false;
137 }
138 
139 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
140 
141  BPFMCInstLower MCInstLowering(OutContext, *this);
142 
143  MCInst TmpInst;
144  MCInstLowering.Lower(MI, TmpInst);
145  EmitToStreamer(*OutStreamer, TmpInst);
146 }
147 
148 // Force static initialization.
149 extern "C" void LLVMInitializeBPFAsmPrinter() {
153 }
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
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:412
Name of external global symbol.
const char * getSymbolName() const
zlib-gnu style compression
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:161
Address of a global value.
const GlobalValue * getGlobal() const
Collect and emit BTF information.
Definition: BTFDebug.h:193
MCSymbol * getSymbol(const GlobalValue *GV) const
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
Address of a basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void Lower(const MachineInstr *MI, MCInst &OutMI) const
MachineOperand class - Representation of each machine instruction operand.
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:64
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:248
const BlockAddress * getBlockAddress() const
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
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:46
void LLVMInitializeBPFAsmPrinter()
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Address of indexed Constant in Constant Pool.
Target & getTheBPFbeTarget()
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:414
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Target & getTheBPFTarget()