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"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "asm-printer"
34 
35 namespace {
36 class BPFAsmPrinter : public AsmPrinter {
37 public:
38  explicit BPFAsmPrinter(TargetMachine &TM,
39  std::unique_ptr<MCStreamer> Streamer)
40  : AsmPrinter(TM, std::move(Streamer)) {}
41 
42  StringRef getPassName() const override { return "BPF Assembly Printer"; }
43  bool doInitialization(Module &M) override;
44  void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
45  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
46  const char *ExtraCode, raw_ostream &O) override;
47  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
48  const char *ExtraCode, raw_ostream &O) override;
49 
50  void EmitInstruction(const MachineInstr *MI) override;
51 };
52 } // namespace
53 
54 bool BPFAsmPrinter::doInitialization(Module &M) {
56 
57  if (MAI->doesSupportDebugInformation()) {
58  Handlers.push_back(HandlerInfo(new BTFDebug(this), "emit",
59  "Debug Info Emission", "BTF",
60  "BTF Emission"));
61  }
62 
63  return false;
64 }
65 
66 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
67  raw_ostream &O) {
68  const MachineOperand &MO = MI->getOperand(OpNum);
69 
70  switch (MO.getType()) {
73  break;
74 
76  O << MO.getImm();
77  break;
78 
80  O << *MO.getMBB()->getSymbol();
81  break;
82 
84  O << *getSymbol(MO.getGlobal());
85  break;
86 
88  MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
89  O << BA->getName();
90  break;
91  }
92 
94  O << *GetExternalSymbolSymbol(MO.getSymbolName());
95  break;
96 
99  default:
100  llvm_unreachable("<unknown operand type>");
101  }
102 }
103 
104 bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
105  const char *ExtraCode, raw_ostream &O) {
106  if (ExtraCode && ExtraCode[0])
107  return true; // BPF does not have special modifiers
108 
109  printOperand(MI, OpNo, O);
110  return false;
111 }
112 
113 bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
114  unsigned OpNum, const char *ExtraCode,
115  raw_ostream &O) {
116  assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
117  const MachineOperand &BaseMO = MI->getOperand(OpNum);
118  const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
119  assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
120  assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
121  int Offset = OffsetMO.getImm();
122 
123  if (ExtraCode)
124  return true; // Unknown modifier.
125 
126  if (Offset < 0)
127  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
128  else
129  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
130 
131  return false;
132 }
133 
134 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
135 
136  BPFMCInstLower MCInstLowering(OutContext, *this);
137 
138  MCInst TmpInst;
139  MCInstLowering.Lower(MI, TmpInst);
140  EmitToStreamer(*OutStreamer, TmpInst);
141 }
142 
143 // Force static initialization.
144 extern "C" void LLVMInitializeBPFAsmPrinter() {
148 }
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:411
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
Collect and emit BTF information.
Definition: BTFDebug.h:222
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.
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: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.
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
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:413
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Target & getTheBPFTarget()