Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- BPFAsmBackend.cpp - BPF Assembler Backend -------------------------===//
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
#include "MCTargetDesc/BPFMCTargetDesc.h"
10
#include "llvm/ADT/StringRef.h"
11
#include "llvm/MC/MCAsmBackend.h"
12
#include "llvm/MC/MCAssembler.h"
13
#include "llvm/MC/MCContext.h"
14
#include "llvm/MC/MCFixup.h"
15
#include "llvm/MC/MCObjectWriter.h"
16
#include "llvm/Support/EndianStream.h"
17
#include <cassert>
18
#include <cstdint>
19
20
using namespace llvm;
21
22
namespace {
23
24
class BPFAsmBackend : public MCAsmBackend {
25
public:
26
209
  BPFAsmBackend(support::endianness Endian) : MCAsmBackend(Endian) {}
27
204
  ~BPFAsmBackend() override = default;
28
29
  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
30
                  const MCValue &Target, MutableArrayRef<char> Data,
31
                  uint64_t Value, bool IsResolved,
32
                  const MCSubtargetInfo *STI) const override;
33
34
  std::unique_ptr<MCObjectTargetWriter>
35
  createObjectTargetWriter() const override;
36
37
  // No instruction requires relaxation
38
  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
39
                            const MCRelaxableFragment *DF,
40
0
                            const MCAsmLayout &Layout) const override {
41
0
    return false;
42
0
  }
43
44
0
  unsigned getNumFixupKinds() const override { return 1; }
45
46
  bool mayNeedRelaxation(const MCInst &Inst,
47
387
                         const MCSubtargetInfo &STI) const override {
48
387
    return false;
49
387
  }
50
51
  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
52
0
                        MCInst &Res) const override {}
53
54
  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
55
};
56
57
} // end anonymous namespace
58
59
50
bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
60
50
  if ((Count % 8) != 0)
61
0
    return false;
62
50
63
50
  for (uint64_t i = 0; i < Count; 
i += 80
)
64
0
    support::endian::write<uint64_t>(OS, 0x15000000, Endian);
65
50
66
50
  return true;
67
50
}
68
69
void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
70
                               const MCValue &Target,
71
                               MutableArrayRef<char> Data, uint64_t Value,
72
                               bool IsResolved,
73
350
                               const MCSubtargetInfo *STI) const {
74
350
  if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
75
20
    // The Value is 0 for global variables, and the in-section offset
76
20
    // for static variables. Write to the immediate field of the inst.
77
20
    assert(Value <= UINT32_MAX);
78
20
    support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4],
79
20
                                     static_cast<uint32_t>(Value),
80
20
                                     Endian);
81
330
  } else if (Fixup.getKind() == FK_Data_4) {
82
207
    support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
83
207
  } else 
if (123
Fixup.getKind() == FK_Data_8123
) {
84
69
    support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, Endian);
85
69
  } else 
if (54
Fixup.getKind() == FK_PCRel_454
) {
86
0
    Value = (uint32_t)((Value - 8) / 8);
87
0
    if (Endian == support::little) {
88
0
      Data[Fixup.getOffset() + 1] = 0x10;
89
0
      support::endian::write32le(&Data[Fixup.getOffset() + 4], Value);
90
0
    } else {
91
0
      Data[Fixup.getOffset() + 1] = 0x1;
92
0
      support::endian::write32be(&Data[Fixup.getOffset() + 4], Value);
93
0
    }
94
54
  } else {
95
54
    assert(Fixup.getKind() == FK_PCRel_2);
96
54
    Value = (uint16_t)((Value - 8) / 8);
97
54
    support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
98
54
                                     Endian);
99
54
  }
100
350
}
101
102
std::unique_ptr<MCObjectTargetWriter>
103
209
BPFAsmBackend::createObjectTargetWriter() const {
104
209
  return createBPFELFObjectWriter(0);
105
209
}
106
107
MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
108
                                        const MCSubtargetInfo &STI,
109
                                        const MCRegisterInfo &MRI,
110
129
                                        const MCTargetOptions &) {
111
129
  return new BPFAsmBackend(support::little);
112
129
}
113
114
MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
115
                                          const MCSubtargetInfo &STI,
116
                                          const MCRegisterInfo &MRI,
117
80
                                          const MCTargetOptions &) {
118
80
  return new BPFAsmBackend(support::big);
119
80
}