1ff0cc061SDimitry Andric //===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
2ff0cc061SDimitry Andric //
3ff0cc061SDimitry Andric // The LLVM Compiler Infrastructure
4ff0cc061SDimitry Andric //
5ff0cc061SDimitry Andric // This file is distributed under the University of Illinois Open Source
6ff0cc061SDimitry Andric // License. See LICENSE.TXT for details.
7ff0cc061SDimitry Andric //
8ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
9ff0cc061SDimitry Andric //
10ff0cc061SDimitry Andric // This class prints an BPF MCInst to a .s file.
11ff0cc061SDimitry Andric //
12ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
13ff0cc061SDimitry Andric
14ff0cc061SDimitry Andric #include "BPFInstPrinter.h"
15ff0cc061SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
16ff0cc061SDimitry Andric #include "llvm/MC/MCExpr.h"
17ff0cc061SDimitry Andric #include "llvm/MC/MCInst.h"
18ff0cc061SDimitry Andric #include "llvm/MC/MCSymbol.h"
19ff0cc061SDimitry Andric #include "llvm/Support/ErrorHandling.h"
20ff0cc061SDimitry Andric #include "llvm/Support/FormattedStream.h"
21ff0cc061SDimitry Andric using namespace llvm;
22ff0cc061SDimitry Andric
23ff0cc061SDimitry Andric #define DEBUG_TYPE "asm-printer"
24ff0cc061SDimitry Andric
25ff0cc061SDimitry Andric // Include the auto-generated portion of the assembly writer.
26ff0cc061SDimitry Andric #include "BPFGenAsmWriter.inc"
27ff0cc061SDimitry Andric
printInst(const MCInst * MI,raw_ostream & O,StringRef Annot,const MCSubtargetInfo & STI)28ff0cc061SDimitry Andric void BPFInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
29ff0cc061SDimitry Andric StringRef Annot, const MCSubtargetInfo &STI) {
30ff0cc061SDimitry Andric printInstruction(MI, O);
31ff0cc061SDimitry Andric printAnnotation(O, Annot);
32ff0cc061SDimitry Andric }
33ff0cc061SDimitry Andric
printExpr(const MCExpr * Expr,raw_ostream & O)34ff0cc061SDimitry Andric static void printExpr(const MCExpr *Expr, raw_ostream &O) {
35ff0cc061SDimitry Andric #ifndef NDEBUG
36ff0cc061SDimitry Andric const MCSymbolRefExpr *SRE;
37ff0cc061SDimitry Andric
38ff0cc061SDimitry Andric if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
39ff0cc061SDimitry Andric SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
40ff0cc061SDimitry Andric else
41ff0cc061SDimitry Andric SRE = dyn_cast<MCSymbolRefExpr>(Expr);
42ff0cc061SDimitry Andric assert(SRE && "Unexpected MCExpr type.");
43ff0cc061SDimitry Andric
44ff0cc061SDimitry Andric MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
45ff0cc061SDimitry Andric
46ff0cc061SDimitry Andric assert(Kind == MCSymbolRefExpr::VK_None);
47ff0cc061SDimitry Andric #endif
48ff0cc061SDimitry Andric O << *Expr;
49ff0cc061SDimitry Andric }
50ff0cc061SDimitry Andric
printOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O,const char * Modifier)51ff0cc061SDimitry Andric void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
52ff0cc061SDimitry Andric raw_ostream &O, const char *Modifier) {
53ff0cc061SDimitry Andric assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
54ff0cc061SDimitry Andric const MCOperand &Op = MI->getOperand(OpNo);
55ff0cc061SDimitry Andric if (Op.isReg()) {
56ff0cc061SDimitry Andric O << getRegisterName(Op.getReg());
57ff0cc061SDimitry Andric } else if (Op.isImm()) {
58da09e106SDimitry Andric O << formatImm((int32_t)Op.getImm());
59ff0cc061SDimitry Andric } else {
60ff0cc061SDimitry Andric assert(Op.isExpr() && "Expected an expression");
61ff0cc061SDimitry Andric printExpr(Op.getExpr(), O);
62ff0cc061SDimitry Andric }
63ff0cc061SDimitry Andric }
64ff0cc061SDimitry Andric
printMemOperand(const MCInst * MI,int OpNo,raw_ostream & O,const char * Modifier)65ff0cc061SDimitry Andric void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
66ff0cc061SDimitry Andric const char *Modifier) {
67ff0cc061SDimitry Andric const MCOperand &RegOp = MI->getOperand(OpNo);
68ff0cc061SDimitry Andric const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
69ff0cc061SDimitry Andric
70ff0cc061SDimitry Andric // register
71ff0cc061SDimitry Andric assert(RegOp.isReg() && "Register operand not a register");
72d88c1a5aSDimitry Andric O << getRegisterName(RegOp.getReg());
73d88c1a5aSDimitry Andric
74d88c1a5aSDimitry Andric // offset
75d88c1a5aSDimitry Andric if (OffsetOp.isImm()) {
76d88c1a5aSDimitry Andric auto Imm = OffsetOp.getImm();
77d88c1a5aSDimitry Andric if (Imm >= 0)
78da09e106SDimitry Andric O << " + " << formatImm(Imm);
79d88c1a5aSDimitry Andric else
80da09e106SDimitry Andric O << " - " << formatImm(-Imm);
81d88c1a5aSDimitry Andric } else {
82d88c1a5aSDimitry Andric assert(0 && "Expected an immediate");
83d88c1a5aSDimitry Andric }
84ff0cc061SDimitry Andric }
85ff0cc061SDimitry Andric
printImm64Operand(const MCInst * MI,unsigned OpNo,raw_ostream & O)86ff0cc061SDimitry Andric void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
87ff0cc061SDimitry Andric raw_ostream &O) {
88ff0cc061SDimitry Andric const MCOperand &Op = MI->getOperand(OpNo);
89ff0cc061SDimitry Andric if (Op.isImm())
90da09e106SDimitry Andric O << formatImm(Op.getImm());
912cab237bSDimitry Andric else if (Op.isExpr())
922cab237bSDimitry Andric printExpr(Op.getExpr(), O);
93ff0cc061SDimitry Andric else
94ff0cc061SDimitry Andric O << Op;
95ff0cc061SDimitry Andric }
962cab237bSDimitry Andric
printBrTargetOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)972cab237bSDimitry Andric void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
982cab237bSDimitry Andric raw_ostream &O) {
992cab237bSDimitry Andric const MCOperand &Op = MI->getOperand(OpNo);
1002cab237bSDimitry Andric if (Op.isImm()) {
1012cab237bSDimitry Andric int16_t Imm = Op.getImm();
102da09e106SDimitry Andric O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
1032cab237bSDimitry Andric } else if (Op.isExpr()) {
1042cab237bSDimitry Andric printExpr(Op.getExpr(), O);
1052cab237bSDimitry Andric } else {
1062cab237bSDimitry Andric O << Op;
1072cab237bSDimitry Andric }
1082cab237bSDimitry Andric }
109