LLVM  3.7.0
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, std::unique_ptr<MCStreamer> Streamer)
39  : AsmPrinter(TM, std::move(Streamer)) {}
40 
41  const char *getPassName() const override { return "BPF Assembly Printer"; }
42 
43  void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
44  const char *Modifier = nullptr);
45  void EmitInstruction(const MachineInstr *MI) override;
46 };
47 }
48 
49 void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
50  raw_ostream &O, const char *Modifier) {
51  const MachineOperand &MO = MI->getOperand(OpNum);
52 
53  switch (MO.getType()) {
56  break;
57 
59  O << MO.getImm();
60  break;
61 
63  O << *MO.getMBB()->getSymbol();
64  break;
65 
67  O << *getSymbol(MO.getGlobal());
68  break;
69 
70  default:
71  llvm_unreachable("<unknown operand type>");
72  }
73 }
74 
75 void BPFAsmPrinter::EmitInstruction(const MachineInstr *MI) {
76 
77  BPFMCInstLower MCInstLowering(OutContext, *this);
78 
79  MCInst TmpInst;
80  MCInstLowering.Lower(MI, TmpInst);
81  EmitToStreamer(*OutStreamer, TmpInst);
82 }
83 
84 // Force static initialization.
85 extern "C" void LLVMInitializeBPFAsmPrinter() {
89 }
const GlobalValue * getGlobal() const
MachineBasicBlock * getMBB() const
MachineBasicBlock reference.
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
Target TheBPFTarget
static void printOperand(raw_ostream &OS, uint8_t Opcode, unsigned OperandIdx, uint64_t Operand, uint64_t CodeAlignmentFactor, int64_t DataAlignmentFactor)
Print Opcode's operand number OperandIdx which has value Operand.
int64_t getImm() const
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
Address of a global value.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
MCSymbol * getSymbol() const
getSymbol - Return the MCSymbol for this basic block.
MachineOperand class - Representation of each machine instruction operand.
Target TheBPFleTarget
static const char * getRegisterName(unsigned RegNo)
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Target TheBPFbeTarget
unsigned getReg() const
getReg - Returns the register number.
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:38
void LLVMInitializeBPFAsmPrinter()
Primary interface to the complete machine description for the target machine.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")