LLVM 18.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#ifndef NDEBUG
40 const MCSymbolRefExpr *SRE;
41
42 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
43 SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
44 else
45 SRE = dyn_cast<MCSymbolRefExpr>(Expr);
46 assert(SRE && "Unexpected MCExpr type.");
47
49
51#endif
52 O << *Expr;
53}
54
55void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
56 raw_ostream &O, const char *Modifier) {
57 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
58 const MCOperand &Op = MI->getOperand(OpNo);
59 if (Op.isReg()) {
60 O << getRegisterName(Op.getReg());
61 } else if (Op.isImm()) {
62 O << formatImm((int32_t)Op.getImm());
63 } else {
64 assert(Op.isExpr() && "Expected an expression");
65 printExpr(Op.getExpr(), O);
66 }
67}
68
70 const char *Modifier) {
71 const MCOperand &RegOp = MI->getOperand(OpNo);
72 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
73
74 // register
75 assert(RegOp.isReg() && "Register operand not a register");
76 O << getRegisterName(RegOp.getReg());
77
78 // offset
79 if (OffsetOp.isImm()) {
80 auto Imm = OffsetOp.getImm();
81 if (Imm >= 0)
82 O << " + " << formatImm(Imm);
83 else
84 O << " - " << formatImm(-Imm);
85 } else {
86 assert(0 && "Expected an immediate");
87 }
88}
89
90void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
91 raw_ostream &O) {
92 const MCOperand &Op = MI->getOperand(OpNo);
93 if (Op.isImm())
94 O << formatImm(Op.getImm());
95 else if (Op.isExpr())
96 printExpr(Op.getExpr(), O);
97 else
98 O << Op;
99}
100
102 raw_ostream &O) {
103 const MCOperand &Op = MI->getOperand(OpNo);
104 if (Op.isImm()) {
105 if (MI->getOpcode() == BPF::JMPL) {
106 int32_t Imm = Op.getImm();
107 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
108 } else {
109 int16_t Imm = Op.getImm();
110 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
111 }
112 } else if (Op.isExpr()) {
113 printExpr(Op.getExpr(), O);
114 } else {
115 O << Op;
116 }
117}
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:484
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
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:192
VariantKind getKind() const
Definition: MCExpr.h:404
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
DWARFExpression::Operation Op