1 //===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
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-exception
7 //===----------------------------------------------------------------------===//
9 // This class prints an BPF MCInst to a .s file.
11 //===----------------------------------------------------------------------===//
13 #include "MCTargetDesc/BPFInstPrinter.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/FormattedStream.h"
22 #define DEBUG_TYPE "asm-printer"
24 // Include the auto-generated portion of the assembly writer.
25 #include "BPFGenAsmWriter.inc"
27 void BPFInstPrinter::printInst(const MCInst
*MI
, raw_ostream
&O
,
28 StringRef Annot
, const MCSubtargetInfo
&STI
) {
29 printInstruction(MI
, O
);
30 printAnnotation(O
, Annot
);
33 static void printExpr(const MCExpr
*Expr
, raw_ostream
&O
) {
35 const MCSymbolRefExpr
*SRE
;
37 if (const MCBinaryExpr
*BE
= dyn_cast
<MCBinaryExpr
>(Expr
))
38 SRE
= dyn_cast
<MCSymbolRefExpr
>(BE
->getLHS());
40 SRE
= dyn_cast
<MCSymbolRefExpr
>(Expr
);
41 assert(SRE
&& "Unexpected MCExpr type.");
43 MCSymbolRefExpr::VariantKind Kind
= SRE
->getKind();
45 assert(Kind
== MCSymbolRefExpr::VK_None
);
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
);
55 O
<< getRegisterName(Op
.getReg());
56 } else if (Op
.isImm()) {
57 O
<< formatImm((int32_t)Op
.getImm());
59 assert(Op
.isExpr() && "Expected an expression");
60 printExpr(Op
.getExpr(), O
);
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);
70 assert(RegOp
.isReg() && "Register operand not a register");
71 O
<< getRegisterName(RegOp
.getReg());
74 if (OffsetOp
.isImm()) {
75 auto Imm
= OffsetOp
.getImm();
77 O
<< " + " << formatImm(Imm
);
79 O
<< " - " << formatImm(-Imm
);
81 assert(0 && "Expected an immediate");
85 void BPFInstPrinter::printImm64Operand(const MCInst
*MI
, unsigned OpNo
,
87 const MCOperand
&Op
= MI
->getOperand(OpNo
);
89 O
<< formatImm(Op
.getImm());
91 printExpr(Op
.getExpr(), O
);
96 void BPFInstPrinter::printBrTargetOperand(const MCInst
*MI
, unsigned OpNo
,
98 const MCOperand
&Op
= MI
->getOperand(OpNo
);
100 int16_t Imm
= Op
.getImm();
101 O
<< ((Imm
>= 0) ? "+" : "") << formatImm(Imm
);
102 } else if (Op
.isExpr()) {
103 printExpr(Op
.getExpr(), O
);