LLVM 19.0.0git
BPFAsmPrinter.cpp
Go to the documentation of this file.
1//===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//
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 file contains a printer that converts from our internal representation
10// of machine-dependent LLVM code to the BPF assembly language.
11//
12//===----------------------------------------------------------------------===//
13
14#include "BPF.h"
15#include "BPFInstrInfo.h"
16#include "BPFMCInstLower.h"
17#include "BPFTargetMachine.h"
18#include "BTFDebug.h"
26#include "llvm/MC/MCAsmInfo.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
32using namespace llvm;
33
34#define DEBUG_TYPE "asm-printer"
35
36namespace {
37class BPFAsmPrinter : public AsmPrinter {
38public:
39 explicit BPFAsmPrinter(TargetMachine &TM,
40 std::unique_ptr<MCStreamer> Streamer)
41 : AsmPrinter(TM, std::move(Streamer)), BTF(nullptr) {}
42
43 StringRef getPassName() const override { return "BPF Assembly Printer"; }
44 bool doInitialization(Module &M) override;
45 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
46 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
47 const char *ExtraCode, raw_ostream &O) override;
48 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
49 const char *ExtraCode, raw_ostream &O) override;
50
51 void emitInstruction(const MachineInstr *MI) override;
52
53private:
54 BTFDebug *BTF;
55};
56} // namespace
57
58bool BPFAsmPrinter::doInitialization(Module &M) {
60
61 // Only emit BTF when debuginfo available.
62 if (MAI->doesSupportDebugInformation() && !M.debug_compile_units().empty()) {
63 BTF = new BTFDebug(this);
64 Handlers.push_back(HandlerInfo(std::unique_ptr<BTFDebug>(BTF), "emit",
65 "Debug Info Emission", "BTF",
66 "BTF Emission"));
67 }
68
69 return false;
70}
71
72void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
73 raw_ostream &O) {
74 const MachineOperand &MO = MI->getOperand(OpNum);
75
76 switch (MO.getType()) {
79 break;
80
82 O << MO.getImm();
83 break;
84
86 O << *MO.getMBB()->getSymbol();
87 break;
88
90 O << *getSymbol(MO.getGlobal());
91 break;
92
94 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
95 O << BA->getName();
96 break;
97 }
98
100 O << *GetExternalSymbolSymbol(MO.getSymbolName());
101 break;
102
105 default:
106 llvm_unreachable("<unknown operand type>");
107 }
108}
109
110bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
111 const char *ExtraCode, raw_ostream &O) {
112 if (ExtraCode && ExtraCode[0])
113 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
114
115 printOperand(MI, OpNo, O);
116 return false;
117}
118
119bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
120 unsigned OpNum, const char *ExtraCode,
121 raw_ostream &O) {
122 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
123 const MachineOperand &BaseMO = MI->getOperand(OpNum);
124 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
125 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
126 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
127 int Offset = OffsetMO.getImm();
128
129 if (ExtraCode)
130 return true; // Unknown modifier.
131
132 if (Offset < 0)
133 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
134 else
135 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
136
137 return false;
138}
139
140void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) {
141 BPF_MC::verifyInstructionPredicates(MI->getOpcode(),
142 getSubtargetInfo().getFeatureBits());
143
144 MCInst TmpInst;
145
146 if (!BTF || !BTF->InstLower(MI, TmpInst)) {
147 BPFMCInstLower MCInstLowering(OutContext, *this);
148 MCInstLowering.Lower(MI, TmpInst);
149 }
150 EmitToStreamer(*OutStreamer, TmpInst);
151}
152
153// Force static initialization.
158}
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFAsmPrinter()
This file contains support for writing BTF debug info.
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
IRTranslator LLVM IR MI
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
const char LLVMTargetMachineRef TM
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
virtual void emitInstruction(const MachineInstr *)
Targets should implement this to emit instructions.
Definition: AsmPrinter.h:567
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:449
virtual bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
static const char * getRegisterName(MCRegister Reg)
Collect and emit BTF information.
Definition: BTFDebug.h:289
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
MCSymbol * getSymbol() const
Return the MCSymbol for this basic block.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const BlockAddress * getBlockAddress() const
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_GlobalAddress
Address of a global value.
@ MO_BlockAddress
Address of a basic block.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:76
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
Target & getTheBPFleTarget()
Target & getTheBPFbeTarget()
Target & getTheBPFTarget()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...