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
14#include "BPF.h"
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCSymbol.h"
21using 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) {
32 printAnnotation(O, Annot);
33}
34
35static void printExpr(const MCExpr *Expr, raw_ostream &O) {
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 if (!SRE)
43 report_fatal_error("Unexpected MCExpr type.");
44
45#ifndef NDEBUG
47
49#endif
50 O << *Expr;
51}
52
53void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
54 raw_ostream &O, const char *Modifier) {
55 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
56 const MCOperand &Op = MI->getOperand(OpNo);
57 if (Op.isReg()) {
58 O << getRegisterName(Op.getReg());
59 } else if (Op.isImm()) {
60 O << formatImm((int32_t)Op.getImm());
61 } else {
62 assert(Op.isExpr() && "Expected an expression");
63 printExpr(Op.getExpr(), O);
64 }
65}
66
68 const char *Modifier) {
69 const MCOperand &RegOp = MI->getOperand(OpNo);
70 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
71
72 // register
73 assert(RegOp.isReg() && "Register operand not a register");
74 O << getRegisterName(RegOp.getReg());
75
76 // offset
77 if (OffsetOp.isImm()) {
78 auto Imm = OffsetOp.getImm();
79 if (Imm >= 0)
80 O << " + " << formatImm(Imm);
81 else
82 O << " - " << formatImm(-Imm);
83 } else {
84 assert(0 && "Expected an immediate");
85 }
86}
87
88void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
89 raw_ostream &O) {
90 const MCOperand &Op = MI->getOperand(OpNo);
91 if (Op.isImm())
92 O << formatImm(Op.getImm());
93 else if (Op.isExpr())
94 printExpr(Op.getExpr(), O);
95 else
96 O << Op;
97}
98
100 raw_ostream &O) {
101 const MCOperand &Op = MI->getOperand(OpNo);
102 if (Op.isImm()) {
103 if (MI->getOpcode() == BPF::JMPL) {
104 int32_t Imm = Op.getImm();
105 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
106 } else {
107 int16_t Imm = Op.getImm();
108 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
109 }
110 } else if (Op.isExpr()) {
111 printExpr(Op.getExpr(), O);
112 } else {
113 O << Op;
114 }
115}
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:493
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:185
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
int64_t getImm() const
Definition: MCInst.h:81
bool isImm() const
Definition: MCInst.h:63
bool isReg() const
Definition: MCInst.h:62
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:70
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
VariantKind getKind() const
Definition: MCExpr.h:413
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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