Path: blob/main/contrib/llvm-project/llvm/lib/Target/BPF/BPFAsmPrinter.cpp
35268 views
//===-- BPFAsmPrinter.cpp - BPF LLVM assembly writer ----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains a printer that converts from our internal representation9// of machine-dependent LLVM code to the BPF assembly language.10//11//===----------------------------------------------------------------------===//1213#include "BPF.h"14#include "BPFInstrInfo.h"15#include "BPFMCInstLower.h"16#include "BPFTargetMachine.h"17#include "BTFDebug.h"18#include "MCTargetDesc/BPFInstPrinter.h"19#include "TargetInfo/BPFTargetInfo.h"20#include "llvm/CodeGen/AsmPrinter.h"21#include "llvm/CodeGen/MachineConstantPool.h"22#include "llvm/CodeGen/MachineFunctionPass.h"23#include "llvm/CodeGen/MachineInstr.h"24#include "llvm/CodeGen/MachineModuleInfo.h"25#include "llvm/IR/Module.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"30#include "llvm/MC/TargetRegistry.h"31#include "llvm/Support/raw_ostream.h"32using namespace llvm;3334#define DEBUG_TYPE "asm-printer"3536namespace {37class BPFAsmPrinter : public AsmPrinter {38public:39explicit BPFAsmPrinter(TargetMachine &TM,40std::unique_ptr<MCStreamer> Streamer)41: AsmPrinter(TM, std::move(Streamer)), BTF(nullptr) {}4243StringRef getPassName() const override { return "BPF Assembly Printer"; }44bool doInitialization(Module &M) override;45void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);46bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,47const char *ExtraCode, raw_ostream &O) override;48bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,49const char *ExtraCode, raw_ostream &O) override;5051void emitInstruction(const MachineInstr *MI) override;5253private:54BTFDebug *BTF;55};56} // namespace5758bool BPFAsmPrinter::doInitialization(Module &M) {59AsmPrinter::doInitialization(M);6061// Only emit BTF when debuginfo available.62if (MAI->doesSupportDebugInformation() && !M.debug_compile_units().empty()) {63BTF = new BTFDebug(this);64DebugHandlers.push_back(std::unique_ptr<BTFDebug>(BTF));65}6667return false;68}6970void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,71raw_ostream &O) {72const MachineOperand &MO = MI->getOperand(OpNum);7374switch (MO.getType()) {75case MachineOperand::MO_Register:76O << BPFInstPrinter::getRegisterName(MO.getReg());77break;7879case MachineOperand::MO_Immediate:80O << MO.getImm();81break;8283case MachineOperand::MO_MachineBasicBlock:84O << *MO.getMBB()->getSymbol();85break;8687case MachineOperand::MO_GlobalAddress:88O << *getSymbol(MO.getGlobal());89break;9091case MachineOperand::MO_BlockAddress: {92MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());93O << BA->getName();94break;95}9697case MachineOperand::MO_ExternalSymbol:98O << *GetExternalSymbolSymbol(MO.getSymbolName());99break;100101case MachineOperand::MO_JumpTableIndex:102case MachineOperand::MO_ConstantPoolIndex:103default:104llvm_unreachable("<unknown operand type>");105}106}107108bool BPFAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,109const char *ExtraCode, raw_ostream &O) {110if (ExtraCode && ExtraCode[0])111return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);112113printOperand(MI, OpNo, O);114return false;115}116117bool BPFAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,118unsigned OpNum, const char *ExtraCode,119raw_ostream &O) {120assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");121const MachineOperand &BaseMO = MI->getOperand(OpNum);122const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);123assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");124assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");125int Offset = OffsetMO.getImm();126127if (ExtraCode)128return true; // Unknown modifier.129130if (Offset < 0)131O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " - " << -Offset << ")";132else133O << "(" << BPFInstPrinter::getRegisterName(BaseMO.getReg()) << " + " << Offset << ")";134135return false;136}137138void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) {139BPF_MC::verifyInstructionPredicates(MI->getOpcode(),140getSubtargetInfo().getFeatureBits());141142MCInst TmpInst;143144if (!BTF || !BTF->InstLower(MI, TmpInst)) {145BPFMCInstLower MCInstLowering(OutContext, *this);146MCInstLowering.Lower(MI, TmpInst);147}148EmitToStreamer(*OutStreamer, TmpInst);149}150151// Force static initialization.152extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFAsmPrinter() {153RegisterAsmPrinter<BPFAsmPrinter> X(getTheBPFleTarget());154RegisterAsmPrinter<BPFAsmPrinter> Y(getTheBPFbeTarget());155RegisterAsmPrinter<BPFAsmPrinter> Z(getTheBPFTarget());156}157158159