LLVM 22.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"
20using 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
34void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
35 raw_ostream &O) {
36 const MCOperand &Op = MI->getOperand(OpNo);
37 if (Op.isReg()) {
38 O << getRegisterName(Op.getReg());
39 } else if (Op.isImm()) {
40 O << formatImm((int32_t)Op.getImm());
41 } else {
42 assert(Op.isExpr() && "Expected an expression");
43 MAI.printExpr(O, *Op.getExpr());
44 }
45}
46
48 raw_ostream &O) {
49 const MCOperand &RegOp = MI->getOperand(OpNo);
50 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
51
52 // register
53 assert(RegOp.isReg() && "Register operand not a register");
54 O << getRegisterName(RegOp.getReg());
55
56 // offset
57 if (OffsetOp.isImm()) {
58 auto Imm = OffsetOp.getImm();
59 if (Imm >= 0)
60 O << " + " << formatImm(Imm);
61 else
62 O << " - " << formatImm(-Imm);
63 } else {
64 assert(0 && "Expected an immediate");
65 }
66}
67
68void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
69 raw_ostream &O) {
70 const MCOperand &Op = MI->getOperand(OpNo);
71 if (Op.isImm())
72 O << formatImm(Op.getImm());
73 else if (Op.isExpr())
74 MAI.printExpr(O, *Op.getExpr());
75 else
76 O << Op;
77}
78
80 raw_ostream &O) {
81 const MCOperand &Op = MI->getOperand(OpNo);
82 if (Op.isImm()) {
83 if (MI->getOpcode() == BPF::JMPL) {
84 int32_t Imm = Op.getImm();
85 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
86 } else {
87 int16_t Imm = Op.getImm();
88 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
89 }
90 } else if (Op.isExpr()) {
91 MAI.printExpr(O, *Op.getExpr());
92 } else {
93 O << Op;
94 }
95}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
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 printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O)
void printMemOperand(const MCInst *MI, int 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.
This class represents an Operation in the Expression.
void printExpr(raw_ostream &, const MCExpr &) const
Definition: MCAsmInfo.cpp:153
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:52
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:188
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:40
bool isReg() const
Definition: MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
Generic base class for all target subtargets.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
DWARFExpression::Operation Op