Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- BPFMCTargetDesc.cpp - BPF Target Descriptions ---------------------===//
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 file provides BPF specific target descriptions.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "MCTargetDesc/BPFMCTargetDesc.h"
14
#include "MCTargetDesc/BPFInstPrinter.h"
15
#include "MCTargetDesc/BPFMCAsmInfo.h"
16
#include "TargetInfo/BPFTargetInfo.h"
17
#include "llvm/MC/MCInstrAnalysis.h"
18
#include "llvm/MC/MCInstrInfo.h"
19
#include "llvm/MC/MCRegisterInfo.h"
20
#include "llvm/MC/MCSubtargetInfo.h"
21
#include "llvm/Support/Host.h"
22
#include "llvm/Support/TargetRegistry.h"
23
24
#define GET_INSTRINFO_MC_DESC
25
#include "BPFGenInstrInfo.inc"
26
27
#define GET_SUBTARGETINFO_MC_DESC
28
#include "BPFGenSubtargetInfo.inc"
29
30
#define GET_REGINFO_MC_DESC
31
#include "BPFGenRegisterInfo.inc"
32
33
using namespace llvm;
34
35
231
static MCInstrInfo *createBPFMCInstrInfo() {
36
231
  MCInstrInfo *X = new MCInstrInfo();
37
231
  InitBPFMCInstrInfo(X);
38
231
  return X;
39
231
}
40
41
233
static MCRegisterInfo *createBPFMCRegisterInfo(const Triple &TT) {
42
233
  MCRegisterInfo *X = new MCRegisterInfo();
43
233
  InitBPFMCRegisterInfo(X, BPF::R11 /* RAReg doesn't exist */);
44
233
  return X;
45
233
}
46
47
static MCSubtargetInfo *createBPFMCSubtargetInfo(const Triple &TT,
48
231
                                                 StringRef CPU, StringRef FS) {
49
231
  return createBPFMCSubtargetInfoImpl(TT, CPU, FS);
50
231
}
51
52
static MCStreamer *createBPFMCStreamer(const Triple &T, MCContext &Ctx,
53
                                       std::unique_ptr<MCAsmBackend> &&MAB,
54
                                       std::unique_ptr<MCObjectWriter> &&OW,
55
                                       std::unique_ptr<MCCodeEmitter> &&Emitter,
56
22
                                       bool RelaxAll) {
57
22
  return createELFStreamer(Ctx, std::move(MAB), std::move(OW), std::move(Emitter),
58
22
                           RelaxAll);
59
22
}
60
61
static MCInstPrinter *createBPFMCInstPrinter(const Triple &T,
62
                                             unsigned SyntaxVariant,
63
                                             const MCAsmInfo &MAI,
64
                                             const MCInstrInfo &MII,
65
203
                                             const MCRegisterInfo &MRI) {
66
203
  if (SyntaxVariant == 0)
67
203
    return new BPFInstPrinter(MAI, MII, MRI);
68
0
  return nullptr;
69
0
}
70
71
namespace {
72
73
class BPFMCInstrAnalysis : public MCInstrAnalysis {
74
public:
75
  explicit BPFMCInstrAnalysis(const MCInstrInfo *Info)
76
16
      : MCInstrAnalysis(Info) {}
77
78
  bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
79
100
                      uint64_t &Target) const override {
80
100
    // The target is the 3rd operand of cond inst and the 1st of uncond inst.
81
100
    int16_t Imm;
82
100
    if (isConditionalBranch(Inst)) {
83
89
      Imm = Inst.getOperand(2).getImm();
84
89
    } else 
if (11
isUnconditionalBranch(Inst)11
)
85
5
      Imm = Inst.getOperand(0).getImm();
86
6
    else
87
6
      return false;
88
94
89
94
    Target = Addr + Size + Imm * Size;
90
94
    return true;
91
94
  }
92
};
93
94
} // end anonymous namespace
95
96
16
static MCInstrAnalysis *createBPFInstrAnalysis(const MCInstrInfo *Info) {
97
16
  return new BPFMCInstrAnalysis(Info);
98
16
}
99
100
96.4k
extern "C" void LLVMInitializeBPFTargetMC() {
101
96.4k
  for (Target *T :
102
289k
       {&getTheBPFleTarget(), &getTheBPFbeTarget(), &getTheBPFTarget()}) {
103
289k
    // Register the MC asm info.
104
289k
    RegisterMCAsmInfo<BPFMCAsmInfo> X(*T);
105
289k
106
289k
    // Register the MC instruction info.
107
289k
    TargetRegistry::RegisterMCInstrInfo(*T, createBPFMCInstrInfo);
108
289k
109
289k
    // Register the MC register info.
110
289k
    TargetRegistry::RegisterMCRegInfo(*T, createBPFMCRegisterInfo);
111
289k
112
289k
    // Register the MC subtarget info.
113
289k
    TargetRegistry::RegisterMCSubtargetInfo(*T,
114
289k
                                            createBPFMCSubtargetInfo);
115
289k
116
289k
    // Register the object streamer
117
289k
    TargetRegistry::RegisterELFStreamer(*T, createBPFMCStreamer);
118
289k
119
289k
    // Register the MCInstPrinter.
120
289k
    TargetRegistry::RegisterMCInstPrinter(*T, createBPFMCInstPrinter);
121
289k
122
289k
    // Register the MC instruction analyzer.
123
289k
    TargetRegistry::RegisterMCInstrAnalysis(*T, createBPFInstrAnalysis);
124
289k
  }
125
96.4k
126
96.4k
  // Register the MC code emitter
127
96.4k
  TargetRegistry::RegisterMCCodeEmitter(getTheBPFleTarget(),
128
96.4k
                                        createBPFMCCodeEmitter);
129
96.4k
  TargetRegistry::RegisterMCCodeEmitter(getTheBPFbeTarget(),
130
96.4k
                                        createBPFbeMCCodeEmitter);
131
96.4k
132
96.4k
  // Register the ASM Backend
133
96.4k
  TargetRegistry::RegisterMCAsmBackend(getTheBPFleTarget(),
134
96.4k
                                       createBPFAsmBackend);
135
96.4k
  TargetRegistry::RegisterMCAsmBackend(getTheBPFbeTarget(),
136
96.4k
                                       createBPFbeAsmBackend);
137
96.4k
138
96.4k
  if (sys::IsLittleEndianHost) {
139
96.4k
    TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
140
96.4k
                                          createBPFMCCodeEmitter);
141
96.4k
    TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
142
96.4k
                                         createBPFAsmBackend);
143
96.4k
  } else {
144
0
    TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
145
0
                                          createBPFbeMCCodeEmitter);
146
0
    TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
147
0
                                         createBPFbeAsmBackend);
148
0
  }
149
96.4k
150
96.4k
}