Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- BPFInstPrinter.cpp - Convert BPF MCInst to asm syntax -------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This class prints an BPF MCInst to a .s file.
10
//
11
//===----------------------------------------------------------------------===//
12
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"
20
using namespace llvm;
21
22
#define DEBUG_TYPE "asm-printer"
23
24
// Include the auto-generated portion of the assembly writer.
25
#include "BPFGenAsmWriter.inc"
26
27
void BPFInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
28
2.29k
                               StringRef Annot, const MCSubtargetInfo &STI) {
29
2.29k
  printInstruction(MI, O);
30
2.29k
  printAnnotation(O, Annot);
31
2.29k
}
32
33
228
static void printExpr(const MCExpr *Expr, raw_ostream &O) {
34
#ifndef NDEBUG
35
  const MCSymbolRefExpr *SRE;
36
37
  if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr))
38
    SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
39
  else
40
    SRE = dyn_cast<MCSymbolRefExpr>(Expr);
41
  assert(SRE && "Unexpected MCExpr type.");
42
43
  MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
44
45
  assert(Kind == MCSymbolRefExpr::VK_None);
46
#endif
47
  O << *Expr;
48
228
}
49
50
void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
51
3.31k
                                  raw_ostream &O, const char *Modifier) {
52
3.31k
  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
53
3.31k
  const MCOperand &Op = MI->getOperand(OpNo);
54
3.31k
  if (Op.isReg()) {
55
2.52k
    O << getRegisterName(Op.getReg());
56
2.52k
  } else 
if (795
Op.isImm()795
) {
57
760
    O << formatImm((int32_t)Op.getImm());
58
760
  } else {
59
35
    assert(Op.isExpr() && "Expected an expression");
60
35
    printExpr(Op.getExpr(), O);
61
35
  }
62
3.31k
}
63
64
void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
65
426
                                     const char *Modifier) {
66
426
  const MCOperand &RegOp = MI->getOperand(OpNo);
67
426
  const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
68
426
69
426
  // register
70
426
  assert(RegOp.isReg() && "Register operand not a register");
71
426
  O << getRegisterName(RegOp.getReg());
72
426
73
426
  // offset
74
426
  if (OffsetOp.isImm()) {
75
426
    auto Imm = OffsetOp.getImm();
76
426
    if (Imm >= 0)
77
338
      O << " + " << formatImm(Imm);
78
88
    else
79
88
      O << " - " << formatImm(-Imm);
80
426
  } else {
81
0
    assert(0 && "Expected an immediate");
82
0
  }
83
426
}
84
85
void BPFInstPrinter::printImm64Operand(const MCInst *MI, unsigned OpNo,
86
123
                                       raw_ostream &O) {
87
123
  const MCOperand &Op = MI->getOperand(OpNo);
88
123
  if (Op.isImm())
89
44
    O << formatImm(Op.getImm());
90
79
  else if (Op.isExpr())
91
79
    printExpr(Op.getExpr(), O);
92
0
  else
93
0
    O << Op;
94
123
}
95
96
void BPFInstPrinter::printBrTargetOperand(const MCInst *MI, unsigned OpNo,
97
208
                                       raw_ostream &O) {
98
208
  const MCOperand &Op = MI->getOperand(OpNo);
99
208
  if (Op.isImm()) {
100
94
    int16_t Imm = Op.getImm();
101
94
    O << ((Imm >= 0) ? 
"+"77
:
""17
) << formatImm(Imm);
102
114
  } else if (Op.isExpr()) {
103
114
    printExpr(Op.getExpr(), O);
104
114
  } else {
105
0
    O << Op;
106
0
  }
107
208
}