LLVM  14.0.0git
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  raw_ostream &O) {
31  printAnnotation(O, Annot);
32 }
33 
34 static void printExpr(const MCExpr *Expr, raw_ostream &O) {
35 #ifndef NDEBUG
36  const MCSymbolRefExpr *SRE;
37 
38  if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
39  SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
40  else
41  SRE = dyn_cast<MCSymbolRefExpr>(Expr);
42  assert(SRE && "Unexpected MCExpr type.");
43 
45 
47 #endif
48  O << *Expr;
49 }
50 
51 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
52  raw_ostream &O, const char *Modifier) {
53  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
54  const MCOperand &Op = MI->getOperand(OpNo);
55  if (Op.isReg()) {
56  O << getRegisterName(Op.getReg());
57  } else if (Op.isImm()) {
58  O << formatImm((int32_t)Op.getImm());
59  } else {
60  assert(Op.isExpr() && "Expected an expression");
61  printExpr(Op.getExpr(), O);
62  }
63 }
64 
66  const char *Modifier) {
67  const MCOperand &RegOp = MI->getOperand(OpNo);
68  const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
69 
70  // register
71  assert(RegOp.isReg() && "Register operand not a register");
72  O << getRegisterName(RegOp.getReg());
73 
74  // offset
75  if (OffsetOp.isImm()) {
76  auto Imm = OffsetOp.getImm();
77  if (Imm >= 0)
78  O << " + " << formatImm(Imm);
79  else
80  O << " - " << formatImm(-Imm);
81  } else {
82  assert(0 && "Expected an immediate");
83  }
84 }
85 
86 void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
87  raw_ostream &O) {
88  const MCOperand &Op = MI->getOperand(OpNo);
89  if (Op.isImm())
90  O << formatImm(Op.getImm());
91  else if (Op.isExpr())
92  printExpr(Op.getExpr(), O);
93  else
94  O << Op;
95 }
96 
97 void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
98  raw_ostream &O) {
99  const MCOperand &Op = MI->getOperand(OpNo);
100  if (Op.isImm()) {
101  int16_t Imm = Op.getImm();
102  O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
103  } else if (Op.isExpr()) {
104  printExpr(Op.getExpr(), O);
105  } else {
106  O << Op;
107  }
108 }
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:400
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCOperand::isReg
bool isReg() const
Definition: MCInst.h:61
ErrorHandling.h
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCBinaryExpr
Binary assembler expressions.
Definition: MCExpr.h:480
llvm::BPFInstPrinter::printInstruction
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
FormattedStream.h
llvm::BPFInstPrinter::getRegisterName
static const char * getRegisterName(unsigned RegNo)
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCSymbol.h
MCInst.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCInstPrinter::printAnnotation
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
Definition: MCInstPrinter.cpp:49
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
llvm::MCOperand::isImm
bool isImm() const
Definition: MCInst.h:62
llvm::BPFInstPrinter::printMemOperand
void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O, const char *Modifier=nullptr)
Definition: BPFInstPrinter.cpp:65
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
uint64_t
printExpr
static void printExpr(const MCExpr *Expr, raw_ostream &O)
Definition: BPFInstPrinter.cpp:34
llvm::BPFInstPrinter::printBrTargetOperand
void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
Definition: BPFInstPrinter.cpp:97
llvm::HighlightColor::Address
@ Address
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::BPFInstPrinter::printOperand
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
Definition: BPFInstPrinter.cpp:51
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::MCInstPrinter::formatImm
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:134
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:321
BPFInstPrinter.h
llvm::BPFInstPrinter::printImm64Operand
void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
Definition: BPFInstPrinter.cpp:86
llvm::BPFInstPrinter::printInst
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &O) override
Print the specified MCInst to the specified raw_ostream.
Definition: BPFInstPrinter.cpp:27
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MCOperand::getReg
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69