LLVM  10.0.0svn
BPFInstPrinter.cpp
Go to the documentation of this file.
1 //===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
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 class prints an BPF MCInst to a .s file.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSymbol.h"
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "asm-printer"
23 
24 // Include the auto-generated portion of the assembly writer.
25 #include "BPFGenAsmWriter.inc"
26 
28  StringRef Annot, const MCSubtargetInfo &STI) {
29  printInstruction(MI, O);
30  printAnnotation(O, Annot);
31 }
32 
33 static void printExpr(const MCExpr *Expr, raw_ostream &O) {
34 #ifndef NDEBUG
35  const MCSymbolRefExpr *SRE;
36 
37  if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
38  SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
39  else
40  SRE = dyn_cast<MCSymbolRefExpr>(Expr);
41  assert(SRE && "Unexpected MCExpr type.");
42 
43  MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
44 
46 #endif
47  O << *Expr;
48 }
49 
50 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
51  raw_ostream &O, const char *Modifier) {
52  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
53  const MCOperand &Op = MI->getOperand(OpNo);
54  if (Op.isReg()) {
55  O << getRegisterName(Op.getReg());
56  } else if (Op.isImm()) {
57  O << formatImm((int32_t)Op.getImm());
58  } else {
59  assert(Op.isExpr() && "Expected an expression");
60  printExpr(Op.getExpr(), O);
61  }
62 }
63 
65  const char *Modifier) {
66  const MCOperand &RegOp = MI->getOperand(OpNo);
67  const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
68 
69  // register
70  assert(RegOp.isReg() && "Register operand not a register");
71  O << getRegisterName(RegOp.getReg());
72 
73  // offset
74  if (OffsetOp.isImm()) {
75  auto Imm = OffsetOp.getImm();
76  if (Imm >= 0)
77  O << " + " << formatImm(Imm);
78  else
79  O << " - " << formatImm(-Imm);
80  } else {
81  assert(0 && "Expected an immediate");
82  }
83 }
84 
85 void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
86  raw_ostream &O) {
87  const MCOperand &Op = MI->getOperand(OpNo);
88  if (Op.isImm())
89  O << formatImm(Op.getImm());
90  else if (Op.isExpr())
91  printExpr(Op.getExpr(), O);
92  else
93  O << Op;
94 }
95 
96 void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
97  raw_ostream &O) {
98  const MCOperand &Op = MI->getOperand(OpNo);
99  if (Op.isImm()) {
100  int16_t Imm = Op.getImm();
101  O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
102  } else if (Op.isExpr()) {
103  printExpr(Op.getExpr(), O);
104  } else {
105  O << Op;
106  }
107 }
bool isImm() const
Definition: MCInst.h:58
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isReg() const
Definition: MCInst.h:57
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:35
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:64
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:97
const MCExpr * getExpr() const
Definition: MCInst.h:95
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
int64_t getImm() const
Definition: MCInst.h:75
void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O, const char *Modifier=nullptr)
bool isExpr() const
Definition: MCInst.h:60
Binary assembler expressions.
Definition: MCExpr.h:423
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:179
static void printExpr(const MCExpr *Expr, raw_ostream &O)
static const char * getRegisterName(unsigned RegNo)
Generic base class for all target subtargets.
LLVM_NODISCARD 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:332
void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:45
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34