96 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This class prints an BPF MCInst to a .s file.10//11//===----------------------------------------------------------------------===//12 13#include "MCTargetDesc/BPFInstPrinter.h"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"19#include "llvm/Support/ErrorHandling.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 27void BPFInstPrinter::printInst(const MCInst *MI, uint64_t Address,28 StringRef Annot, const MCSubtargetInfo &STI,29 raw_ostream &O) {30 printInstruction(MI, Address, 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 47void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo,48 raw_ostream &O) {49 const MCOperand &RegOp = MI->getOperand(OpNo);50 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);51 52 // register53 assert(RegOp.isReg() && "Register operand not a register");54 O << getRegisterName(RegOp.getReg());55 56 // offset57 if (OffsetOp.isImm()) {58 auto Imm = OffsetOp.getImm();59 if (Imm >= 0)60 O << " + " << formatImm(Imm);61 else62 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 else76 O << Op;77}78 79void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,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}96