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