LLVM  15.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"
18 #include "llvm/Support/Casting.h"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "asm-printer"
24 
25 // Include the auto-generated portion of the assembly writer.
26 #include "BPFGenAsmWriter.inc"
27 
29  StringRef Annot, const MCSubtargetInfo &STI,
30  raw_ostream &O) {
31  printInstruction(MI, Address, 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 
46 
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 == nullptr || 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 << formatImm((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 
71  // register
72  assert(RegOp.isReg() && "Register operand not a register");
73  O << getRegisterName(RegOp.getReg());
74 
75  // offset
76  if (OffsetOp.isImm()) {
77  auto Imm = OffsetOp.getImm();
78  if (Imm >= 0)
79  O << " + " << formatImm(Imm);
80  else
81  O << " - " << formatImm(-Imm);
82  } else {
83  assert(0 && "Expected an immediate");
84  }
85 }
86 
87 void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
88  raw_ostream &O) {
89  const MCOperand &Op = MI->getOperand(OpNo);
90  if (Op.isImm())
91  O << formatImm(Op.getImm());
92  else if (Op.isExpr())
93  printExpr(Op.getExpr(), O);
94  else
95  O << Op;
96 }
97 
98 void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
99  raw_ostream &O) {
100  const MCOperand &Op = MI->getOperand(OpNo);
101  if (Op.isImm()) {
102  int16_t Imm = Op.getImm();
103  O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
104  } else if (Op.isExpr()) {
105  printExpr(Op.getExpr(), O);
106  } else {
107  O << Op;
108  }
109 }
llvm::MCSymbolRefExpr::getKind
VariantKind getKind() const
Definition: MCExpr.h:401
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:481
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:54
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:50
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:239
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:66
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
uint64_t
printExpr
static void printExpr(const MCExpr *Expr, raw_ostream &O)
Definition: BPFInstPrinter.cpp:35
llvm::BPFInstPrinter::printBrTargetOperand
void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
Definition: BPFInstPrinter.cpp:98
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:52
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:341
Casting.h
BPFInstPrinter.h
llvm::BPFInstPrinter::printImm64Operand
void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
Definition: BPFInstPrinter.cpp:87
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:28
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:76
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