Path: blob/main/contrib/llvm-project/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
35294 views
//===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//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 class prints an BPF MCInst to a .s file.9//10//===----------------------------------------------------------------------===//111213#include "BPF.h"14#include "MCTargetDesc/BPFInstPrinter.h"15#include "llvm/MC/MCAsmInfo.h"16#include "llvm/MC/MCExpr.h"17#include "llvm/MC/MCInst.h"18#include "llvm/MC/MCRegister.h"19#include "llvm/MC/MCSymbol.h"20#include "llvm/Support/Casting.h"21#include "llvm/Support/ErrorHandling.h"22#include "llvm/Support/FormattedStream.h"23using namespace llvm;2425#define DEBUG_TYPE "asm-printer"2627// Include the auto-generated portion of the assembly writer.28#include "BPFGenAsmWriter.inc"2930void BPFInstPrinter::printInst(const MCInst *MI, uint64_t Address,31StringRef Annot, const MCSubtargetInfo &STI,32raw_ostream &O) {33printInstruction(MI, Address, O);34printAnnotation(O, Annot);35}3637static void printExpr(const MCExpr *Expr, raw_ostream &O) {38const MCSymbolRefExpr *SRE;3940if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))41SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());42else43SRE = dyn_cast<MCSymbolRefExpr>(Expr);44if (!SRE)45report_fatal_error("Unexpected MCExpr type.");4647#ifndef NDEBUG48MCSymbolRefExpr::VariantKind Kind = SRE->getKind();4950assert(Kind == MCSymbolRefExpr::VK_None);51#endif52O << *Expr;53}5455void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,56raw_ostream &O, const char *Modifier) {57assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");58const MCOperand &Op = MI->getOperand(OpNo);59if (Op.isReg()) {60O << getRegisterName(Op.getReg());61} else if (Op.isImm()) {62O << formatImm((int32_t)Op.getImm());63} else {64assert(Op.isExpr() && "Expected an expression");65printExpr(Op.getExpr(), O);66}67}6869void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,70const char *Modifier) {71const MCOperand &RegOp = MI->getOperand(OpNo);72const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);7374// register75assert(RegOp.isReg() && "Register operand not a register");76O << getRegisterName(RegOp.getReg());7778// offset79if (OffsetOp.isImm()) {80auto Imm = OffsetOp.getImm();81if (Imm >= 0)82O << " + " << formatImm(Imm);83else84O << " - " << formatImm(-Imm);85} else {86assert(0 && "Expected an immediate");87}88}8990void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,91raw_ostream &O) {92const MCOperand &Op = MI->getOperand(OpNo);93if (Op.isImm())94O << formatImm(Op.getImm());95else if (Op.isExpr())96printExpr(Op.getExpr(), O);97else98O << Op;99}100101void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,102raw_ostream &O) {103const MCOperand &Op = MI->getOperand(OpNo);104if (Op.isImm()) {105if (MI->getOpcode() == BPF::JMPL) {106int32_t Imm = Op.getImm();107O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);108} else {109int16_t Imm = Op.getImm();110O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);111}112} else if (Op.isExpr()) {113printExpr(Op.getExpr(), O);114} else {115O << Op;116}117}118119120