LLVM  6.0.0svn
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"
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  void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
44  bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
45  unsigned AsmVariant, const char *ExtraCode,
46  raw_ostream &O) override;
47  bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
48  unsigned AsmVariant, const char *ExtraCode,
49  raw_ostream &O) override;
50 
51  void EmitInstruction(const MachineInstr *MI) override;
52 };
53 } // namespace
54 
55 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
56  raw_ostream &O) {
57  const MachineOperand &MO = MI->getOperand(OpNum);
58 
59  switch (MO.getType()) {
62  break;
63 
65  O << MO.getImm();
66  break;
67 
69  O << *MO.getMBB()->getSymbol();
70  break;
71 
73  O << *getSymbol(MO.getGlobal());
74  break;
75 
77  MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
78  O << BA->getName();
79  break;
80  }
81 
83  O << *GetExternalSymbolSymbol(MO.getSymbolName());
84  break;
85 
88  default:
89  llvm_unreachable("<unknown operand type>");
90  }
91 }
92 
93 bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
94  unsigned /*AsmVariant*/,
95  const char *ExtraCode, raw_ostream &O) {
96  if (ExtraCode && ExtraCode[0])
97  return true; // BPF does not have special modifiers
98 
99  printOperand(MI, OpNo, O);
100  return false;
101 }
102 
103 bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
104  unsigned OpNum, unsigned AsmVariant,
105  const char *ExtraCode,
106  raw_ostream &O) {
107  assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
108  const MachineOperand &BaseMO = MI->getOperand(OpNum);
109  const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
110  assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
111  assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
112  int Offset = OffsetMO.getImm();
113 
114  if (ExtraCode)
115  return true; // Unknown modifier.
116 
117  if (Offset < 0)
118  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
119  else
120  O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
121 
122  return false;
123 }
124 
125 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
126 
127  BPFMCInstLower MCInstLowering(OutContext, *this);
128 
129  MCInst TmpInst;
130  MCInstLowering.Lower(MI, TmpInst);
131  EmitToStreamer(*OutStreamer, TmpInst);
132 }
133 
134 // Force static initialization.
135 extern "C" void LLVMInitializeBPFAsmPrinter() {
139 }
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
MachineBasicBlock * getMBB() const
Compute iterated dominance frontiers using a linear time algorithm.
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
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
Access to explicit operands of the instruction.
Definition: MachineInstr.h:293
Name of external global symbol.
const char * getSymbolName() const
zlib-gnu style compression
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
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:76
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
static const char * getRegisterName(unsigned RegNo)
Representation of each machine instruction.
Definition: MachineInstr.h:59
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
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()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer, for use in the target machine initialization function.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void LLVMInitializeBPFAsmPrinter()
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:57
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:295
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Target & getTheBPFTarget()