xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This class prints an BPF MCInst to a .s file.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*5f757f3fSDimitry Andric 
14*5f757f3fSDimitry Andric #include "BPF.h"
150b57cec5SDimitry Andric #include "MCTargetDesc/BPFInstPrinter.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
170b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
19bdd1243dSDimitry Andric #include "llvm/MC/MCRegister.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
2181ad6265SDimitry Andric #include "llvm/Support/Casting.h"
220b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
230b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric #define DEBUG_TYPE "asm-printer"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric // Include the auto-generated portion of the assembly writer.
290b57cec5SDimitry Andric #include "BPFGenAsmWriter.inc"
300b57cec5SDimitry Andric 
printInst(const MCInst * MI,uint64_t Address,StringRef Annot,const MCSubtargetInfo & STI,raw_ostream & O)31480093f4SDimitry Andric void BPFInstPrinter::printInst(const MCInst *MI, uint64_t Address,
32480093f4SDimitry Andric                                StringRef Annot, const MCSubtargetInfo &STI,
33480093f4SDimitry Andric                                raw_ostream &O) {
34480093f4SDimitry Andric   printInstruction(MI, Address, O);
350b57cec5SDimitry Andric   printAnnotation(O, Annot);
360b57cec5SDimitry Andric }
370b57cec5SDimitry Andric 
printExpr(const MCExpr * Expr,raw_ostream & O)380b57cec5SDimitry Andric static void printExpr(const MCExpr *Expr, raw_ostream &O) {
390b57cec5SDimitry Andric   const MCSymbolRefExpr *SRE;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
420b57cec5SDimitry Andric     SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
430b57cec5SDimitry Andric   else
440b57cec5SDimitry Andric     SRE = dyn_cast<MCSymbolRefExpr>(Expr);
45*5f757f3fSDimitry Andric   if (!SRE)
46*5f757f3fSDimitry Andric     report_fatal_error("Unexpected MCExpr type.");
470b57cec5SDimitry Andric 
48*5f757f3fSDimitry Andric #ifndef NDEBUG
490b57cec5SDimitry Andric   MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   assert(Kind == MCSymbolRefExpr::VK_None);
520b57cec5SDimitry Andric #endif
530b57cec5SDimitry Andric   O << *Expr;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
printOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O,const char * Modifier)560b57cec5SDimitry Andric void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
570b57cec5SDimitry Andric                                   raw_ostream &O, const char *Modifier) {
5804eeddc0SDimitry Andric   assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
590b57cec5SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
600b57cec5SDimitry Andric   if (Op.isReg()) {
610b57cec5SDimitry Andric     O << getRegisterName(Op.getReg());
620b57cec5SDimitry Andric   } else if (Op.isImm()) {
630b57cec5SDimitry Andric     O << formatImm((int32_t)Op.getImm());
640b57cec5SDimitry Andric   } else {
650b57cec5SDimitry Andric     assert(Op.isExpr() && "Expected an expression");
660b57cec5SDimitry Andric     printExpr(Op.getExpr(), O);
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
printMemOperand(const MCInst * MI,int OpNo,raw_ostream & O,const char * Modifier)700b57cec5SDimitry Andric void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
710b57cec5SDimitry Andric                                      const char *Modifier) {
720b57cec5SDimitry Andric   const MCOperand &RegOp = MI->getOperand(OpNo);
730b57cec5SDimitry Andric   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // register
760b57cec5SDimitry Andric   assert(RegOp.isReg() && "Register operand not a register");
770b57cec5SDimitry Andric   O << getRegisterName(RegOp.getReg());
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   // offset
800b57cec5SDimitry Andric   if (OffsetOp.isImm()) {
810b57cec5SDimitry Andric     auto Imm = OffsetOp.getImm();
820b57cec5SDimitry Andric     if (Imm >= 0)
830b57cec5SDimitry Andric       O << " + " << formatImm(Imm);
840b57cec5SDimitry Andric     else
850b57cec5SDimitry Andric       O << " - " << formatImm(-Imm);
860b57cec5SDimitry Andric   } else {
870b57cec5SDimitry Andric     assert(0 && "Expected an immediate");
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
printImm64Operand(const MCInst * MI,unsigned OpNo,raw_ostream & O)910b57cec5SDimitry Andric void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
920b57cec5SDimitry Andric                                        raw_ostream &O) {
930b57cec5SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
940b57cec5SDimitry Andric   if (Op.isImm())
950b57cec5SDimitry Andric     O << formatImm(Op.getImm());
960b57cec5SDimitry Andric   else if (Op.isExpr())
970b57cec5SDimitry Andric     printExpr(Op.getExpr(), O);
980b57cec5SDimitry Andric   else
990b57cec5SDimitry Andric     O << Op;
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
printBrTargetOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)1020b57cec5SDimitry Andric void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
1030b57cec5SDimitry Andric                                        raw_ostream &O) {
1040b57cec5SDimitry Andric   const MCOperand &Op = MI->getOperand(OpNo);
1050b57cec5SDimitry Andric   if (Op.isImm()) {
106*5f757f3fSDimitry Andric     if (MI->getOpcode() == BPF::JMPL) {
107*5f757f3fSDimitry Andric       int32_t Imm = Op.getImm();
108*5f757f3fSDimitry Andric       O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
109*5f757f3fSDimitry Andric     } else {
1100b57cec5SDimitry Andric       int16_t Imm = Op.getImm();
1110b57cec5SDimitry Andric       O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
112*5f757f3fSDimitry Andric     }
1130b57cec5SDimitry Andric   } else if (Op.isExpr()) {
1140b57cec5SDimitry Andric     printExpr(Op.getExpr(), O);
1150b57cec5SDimitry Andric   } else {
1160b57cec5SDimitry Andric     O << Op;
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric }
119