LLVM  3.7.0
BPFInstPrinter.cpp
Go to the documentation of this file.
1 //===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
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 class prints an BPF MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BPF.h"
15 #include "BPFInstPrinter.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCSymbol.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "asm-printer"
25 
26 // Include the auto-generated portion of the assembly writer.
27 #include "BPFGenAsmWriter.inc"
28 
30  StringRef Annot, const MCSubtargetInfo &STI) {
31  printInstruction(MI, O);
32  printAnnotation(O, Annot);
33 }
34 
35 static void printExpr(const MCExpr *Expr, raw_ostream &O) {
36 #ifndef NDEBUG
37  const MCSymbolRefExpr *SRE;
38 
39  if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
40  SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
41  else
42  SRE = dyn_cast<MCSymbolRefExpr>(Expr);
43  assert(SRE && "Unexpected MCExpr type.");
44 
45  MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
46 
47  assert(Kind == MCSymbolRefExpr::VK_None);
48 #endif
49  O << *Expr;
50 }
51 
52 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
53  raw_ostream &O, const char *Modifier) {
54  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
55  const MCOperand &Op = MI->getOperand(OpNo);
56  if (Op.isReg()) {
57  O << getRegisterName(Op.getReg());
58  } else if (Op.isImm()) {
59  O << (int32_t)Op.getImm();
60  } else {
61  assert(Op.isExpr() && "Expected an expression");
62  printExpr(Op.getExpr(), O);
63  }
64 }
65 
67  const char *Modifier) {
68  const MCOperand &RegOp = MI->getOperand(OpNo);
69  const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
70  // offset
71  if (OffsetOp.isImm())
72  O << formatDec(OffsetOp.getImm());
73  else
74  assert(0 && "Expected an immediate");
75 
76  // register
77  assert(RegOp.isReg() && "Register operand not a register");
78  O << '(' << getRegisterName(RegOp.getReg()) << ')';
79 }
80 
81 void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
82  raw_ostream &O) {
83  const MCOperand &Op = MI->getOperand(OpNo);
84  if (Op.isImm())
85  O << (uint64_t)Op.getImm();
86  else
87  O << Op;
88 }
bool isReg() const
Definition: MCInst.h:56
void printInstruction(const MCInst *MI, raw_ostream &O)
void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:159
void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:63
format_object< int64_t > formatDec(int64_t Value) const
Utility functions to print decimal/hexadecimal values.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
bool isImm() const
Definition: MCInst.h:57
const MCExpr * getExpr() const
Definition: MCInst.h:93
bool isExpr() const
Definition: MCInst.h:59
void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O, const char *Modifier=nullptr)
Binary assembler expressions.
Definition: MCExpr.h:405
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
int64_t getImm() const
Definition: MCInst.h:74
static void printExpr(const MCExpr *Expr, raw_ostream &O)
static const char * getRegisterName(unsigned RegNo)
MCSubtargetInfo - Generic base class for all target subtargets.
const ARM::ArchExtKind Kind
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164