blob: 20627da38817ef91bb963d86d64278caf50f7111 [file] [log] [blame]
Alexei Starovoitove4c8c802015-01-24 17:51:26 +00001//===-- 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
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000014#include "BPFInstPrinter.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"
20#include "llvm/Support/FormattedStream.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "asm-printer"
24
25// Include the auto-generated portion of the assembly writer.
26#include "BPFGenAsmWriter.inc"
27
28void BPFInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
Akira Hatanakab46d0232015-03-27 20:36:02 +000029 StringRef Annot, const MCSubtargetInfo &STI) {
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000030 printInstruction(MI, O);
31 printAnnotation(O, Annot);
32}
33
34static void printExpr(const MCExpr *Expr, raw_ostream &O) {
Alexei Starovoitovf26c7482015-04-26 01:58:08 +000035#ifndef NDEBUG
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000036 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);
Alexei Starovoitovf26c7482015-04-26 01:58:08 +000047#endif
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000048 O << *Expr;
49}
50
51void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
52 raw_ostream &O, const char *Modifier) {
53 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
54 const MCOperand &Op = MI->getOperand(OpNo);
55 if (Op.isReg()) {
56 O << getRegisterName(Op.getReg());
57 } else if (Op.isImm()) {
Yonghong Song25bf8252017-12-20 19:39:58 +000058 O << formatImm((int32_t)Op.getImm());
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000059 } else {
60 assert(Op.isExpr() && "Expected an expression");
61 printExpr(Op.getExpr(), O);
62 }
63}
64
65void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
66 const char *Modifier) {
67 const MCOperand &RegOp = MI->getOperand(OpNo);
68 const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000069
70 // register
71 assert(RegOp.isReg() && "Register operand not a register");
Alexei Starovoitov8f9f8212016-11-18 02:32:35 +000072 O << getRegisterName(RegOp.getReg());
73
74 // offset
75 if (OffsetOp.isImm()) {
76 auto Imm = OffsetOp.getImm();
77 if (Imm >= 0)
Yonghong Song25bf8252017-12-20 19:39:58 +000078 O << " + " << formatImm(Imm);
Alexei Starovoitov8f9f8212016-11-18 02:32:35 +000079 else
Yonghong Song25bf8252017-12-20 19:39:58 +000080 O << " - " << formatImm(-Imm);
Alexei Starovoitov8f9f8212016-11-18 02:32:35 +000081 } else {
82 assert(0 && "Expected an immediate");
83 }
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000084}
85
86void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
87 raw_ostream &O) {
88 const MCOperand &Op = MI->getOperand(OpNo);
89 if (Op.isImm())
Yonghong Song25bf8252017-12-20 19:39:58 +000090 O << formatImm(Op.getImm());
Yonghong Song093420f2017-09-08 23:32:38 +000091 else if (Op.isExpr())
92 printExpr(Op.getExpr(), O);
Alexei Starovoitove4c8c802015-01-24 17:51:26 +000093 else
94 O << Op;
95}
Yonghong Songce967382017-11-16 19:15:36 +000096
97void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
98 raw_ostream &O) {
99 const MCOperand &Op = MI->getOperand(OpNo);
100 if (Op.isImm()) {
101 int16_t Imm = Op.getImm();
Yonghong Song25bf8252017-12-20 19:39:58 +0000102 O << ((Imm >= 0) ? "+" : "") << formatImm(Imm);
Yonghong Songce967382017-11-16 19:15:36 +0000103 } else if (Op.isExpr()) {
104 printExpr(Op.getExpr(), O);
105 } else {
106 O << Op;
107 }
108}