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/IR/Module.h"
27#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
33using namespace llvm;
34
35#define DEBUG_TYPE "asm-printer"
36
37namespace {
38class BPFAsmPrinter : public AsmPrinter {
39public:
40 explicit BPFAsmPrinter(TargetMachine &TM,
41 std::unique_ptr<MCStreamer> Streamer)
42 : AsmPrinter(TM, std::move(Streamer)), BTF(nullptr) {}
43
44 StringRef getPassName() const override { return "BPF Assembly Printer"; }
45 bool doInitialization(Module &M) override;
46 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
47 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
48 const char *ExtraCode, raw_ostream &O) override;
49 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
50 const char *ExtraCode, raw_ostream &O) override;
51
52 void emitInstruction(const MachineInstr *MI) override;
53
54private:
55 BTFDebug *BTF;
56};
57} // namespace
58
59bool BPFAsmPrinter::doInitialization(Module &M) {
61
62 // Only emit BTF when debuginfo available.
63 if (MAI->doesSupportDebugInformation() && !M.debug_compile_units().empty()) {
64 BTF = new BTFDebug(this);
65 DebugHandlers.push_back(std::unique_ptr<BTFDebug>(BTF));
66 }
67
68 return false;
69}
70
71void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
72 raw_ostream &O) {
73 const MachineOperand &MO = MI->getOperand(OpNum);
74
75 switch (MO.getType()) {
78 break;
79
81 O << MO.getImm();
82 break;
83
85 O << *MO.getMBB()->getSymbol();
86 break;
87
89 O << *getSymbol(MO.getGlobal());
90 break;
91
93 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
94 O << BA->getName();
95 break;
96 }
97
99 O << *GetExternalSymbolSymbol(MO.getSymbolName());
100 break;
101
104 default:
105 llvm_unreachable("<unknown operand type>");
106 }
107}
108
109bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
110 const char *ExtraCode, raw_ostream &O) {
111 if (ExtraCode && ExtraCode[0])
112 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
113
114 printOperand(MI, OpNo, O);
115 return false;
116}
117
118bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
119 unsigned OpNum, const char *ExtraCode,
120 raw_ostream &O) {
121 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
122 const MachineOperand &BaseMO = MI->getOperand(OpNum);
123 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
124 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
125 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
126 int Offset = OffsetMO.getImm();
127
128 if (ExtraCode)
129 return true; // Unknown modifier.
130
131 if (Offset < 0)
132 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";
133 else
134 O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";
135
136 return false;
137}
138
139void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) {
140 BPF_MC::verifyInstructionPredicates(MI->getOpcode(),
141 getSubtargetInfo().getFeatureBits());
142
143 MCInst TmpInst;
144
145 if (!BTF || !BTF->InstLower(MI, TmpInst)) {
146 BPFMCInstLower MCInstLowering(OutContext, *this);
147 MCInstLowering.Lower(MI, TmpInst);
148 }
149 EmitToStreamer(*OutStreamer, TmpInst);
150}
151
152// Force static initialization.
157}
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 ...
Module.h This file contains the declarations for the Module class.
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:86
virtual void emitInstruction(const MachineInstr *)
Targets should implement this to emit instructions.
Definition: AsmPrinter.h:555
bool doInitialization(Module &M) override
Set up the AsmPrinter when we are working on a new module.
Definition: AsmPrinter.cpp:434
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:41
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:77
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:480
Target & getTheBPFleTarget()
Target & getTheBPFbeTarget()
Target & getTheBPFTarget()
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...