LLVM 20.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
13
14#include "BPF.h"
16#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCRegister.h"
20#include "llvm/MC/MCSymbol.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "asm-printer"
27
28// Include the auto-generated portion of the assembly writer.
29#include "BPFGenAsmWriter.inc"
30
32 StringRef Annot, const MCSubtargetInfo &STI,
33 raw_ostream &O) {
35 printAnnotation(O, Annot);
36}
37
38static void printExpr(const MCExpr *Expr, raw_ostream &O) {
39 const MCSymbolRefExpr *SRE;
40
41 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
42 SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
43 else
44 SRE = dyn_cast<MCSymbolRefExpr>(Expr);
45 if (!SRE)
46 report_fatal_error("Unexpected MCExpr type.");
47
48#ifndef NDEBUG
50
52#endif
53 O << *Expr;
54}
55
56void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
57 raw_ostream &O, const char *Modifier) {
58 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
59 const MCOperand &Op = MI->getOperand(OpNo);
60 if (Op.isReg()) {
61 O << getRegisterName(Op.getReg());
62 } else if (Op.isImm()) {
63 O << formatImm((int32_t)Op.getImm());
64 } else {
65 assert(Op.isExpr() && "Expected an expression");
66 printExpr(Op.getExpr(), O);
67 }
68}
69
71 const char *Modifier) {
72 const MCOperand &RegOp = MI->getOperand(OpNo);
73 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
74
75 // register
76 assert(RegOp.isReg() && "Register operand not a register");
77 O << getRegisterName(RegOp.getReg());
78
79 // offset
80 if (OffsetOp.isImm()) {
81 auto Imm = OffsetOp.getImm();
82 if (Imm >= 0)
83 O << " + " << formatImm(Imm);
84 else
85 O << " - " << formatImm(-Imm);
86 } else {
87 assert(0 && "Expected an immediate");
88 }
89}
90
91void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
92 raw_ostream &O) {
93 const MCOperand &Op = MI->getOperand(OpNo);
94 if (Op.isImm())
95 O << formatImm(Op.getImm());
96 else if (Op.isExpr())
97 printExpr(Op.getExpr(), O);
98 else
99 O << Op;
100}
101
103 raw_ostream &O) {
104 const MCOperand &Op = MI->getOperand(OpNo);
105 if (Op.isImm()) {
106 if (MI->getOpcode() == BPF::JMPL) {
107 int32_t Imm = Op.getImm();
108 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
109 } else {
110 int16_t Imm = Op.getImm();
111 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
112 }
113 } else if (Op.isExpr()) {
114 printExpr(Op.getExpr(), O);
115 } else {
116 O << Op;
117 }
118}
static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI, raw_ostream &OS)
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
static const char * getRegisterName(MCRegister Reg)
void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
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.
void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O, const char *Modifier=nullptr)
void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O, const char *Modifier=nullptr)
This class represents an Operation in the Expression.
Binary assembler expressions.
Definition: MCExpr.h:488
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
int64_t getImm() const
Definition: MCInst.h:80
bool isImm() const
Definition: MCInst.h:62
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69
bool isReg() const
Definition: MCInst.h:61
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
VariantKind getKind() const
Definition: MCExpr.h:408
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
DWARFExpression::Operation Op