Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyAsmBackend.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- WebAssemblyAsmBackend.cpp - WebAssembly 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
/// \file
10
/// This file implements the WebAssemblyAsmBackend class.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "MCTargetDesc/WebAssemblyFixupKinds.h"
15
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
16
#include "llvm/MC/MCAsmBackend.h"
17
#include "llvm/MC/MCAssembler.h"
18
#include "llvm/MC/MCDirectives.h"
19
#include "llvm/MC/MCExpr.h"
20
#include "llvm/MC/MCFixupKindInfo.h"
21
#include "llvm/MC/MCObjectWriter.h"
22
#include "llvm/MC/MCSubtargetInfo.h"
23
#include "llvm/MC/MCSymbol.h"
24
#include "llvm/MC/MCWasmObjectWriter.h"
25
#include "llvm/Support/ErrorHandling.h"
26
#include "llvm/Support/raw_ostream.h"
27
28
using namespace llvm;
29
30
namespace {
31
32
class WebAssemblyAsmBackend final : public MCAsmBackend {
33
  bool Is64Bit;
34
35
public:
36
  explicit WebAssemblyAsmBackend(bool Is64Bit)
37
443
      : MCAsmBackend(support::little), Is64Bit(Is64Bit) {}
38
39
0
  unsigned getNumFixupKinds() const override {
40
0
    return WebAssembly::NumTargetFixupKinds;
41
0
  }
42
43
  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
44
45
  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
46
                  const MCValue &Target, MutableArrayRef<char> Data,
47
                  uint64_t Value, bool IsPCRel,
48
                  const MCSubtargetInfo *STI) const override;
49
50
  std::unique_ptr<MCObjectTargetWriter>
51
  createObjectTargetWriter() const override;
52
53
  // No instruction requires relaxation
54
  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
55
                            const MCRelaxableFragment *DF,
56
0
                            const MCAsmLayout &Layout) const override {
57
0
    return false;
58
0
  }
59
60
  bool mayNeedRelaxation(const MCInst &Inst,
61
1.40k
                         const MCSubtargetInfo &STI) const override {
62
1.40k
    return false;
63
1.40k
  }
64
65
  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
66
0
                        MCInst &Res) const override {}
67
68
  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
69
};
70
71
const MCFixupKindInfo &
72
2.29k
WebAssemblyAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
73
2.29k
  const static MCFixupKindInfo Infos[WebAssembly::NumTargetFixupKinds] = {
74
2.29k
      // This table *must* be in the order that the fixup_* kinds are defined in
75
2.29k
      // WebAssemblyFixupKinds.h.
76
2.29k
      //
77
2.29k
      // Name                     Offset (bits) Size (bits)     Flags
78
2.29k
      {"fixup_sleb128_i32", 0, 5 * 8, 0},
79
2.29k
      {"fixup_sleb128_i64", 0, 10 * 8, 0},
80
2.29k
      {"fixup_uleb128_i32", 0, 5 * 8, 0},
81
2.29k
  };
82
2.29k
83
2.29k
  if (Kind < FirstTargetFixupKind)
84
690
    return MCAsmBackend::getFixupKindInfo(Kind);
85
1.60k
86
1.60k
  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
87
1.60k
         "Invalid kind!");
88
1.60k
  return Infos[Kind - FirstTargetFixupKind];
89
1.60k
}
90
91
bool WebAssemblyAsmBackend::writeNopData(raw_ostream &OS,
92
0
                                         uint64_t Count) const {
93
0
  for (uint64_t I = 0; I < Count; ++I)
94
0
    OS << char(WebAssembly::Nop);
95
0
96
0
  return true;
97
0
}
98
99
void WebAssemblyAsmBackend::applyFixup(const MCAssembler &Asm,
100
                                       const MCFixup &Fixup,
101
                                       const MCValue &Target,
102
                                       MutableArrayRef<char> Data,
103
                                       uint64_t Value, bool IsPCRel,
104
578
                                       const MCSubtargetInfo *STI) const {
105
578
  const MCFixupKindInfo &Info = getFixupKindInfo(Fixup.getKind());
106
578
  assert(Info.Flags == 0 && "WebAssembly does not use MCFixupKindInfo flags");
107
578
108
578
  unsigned NumBytes = alignTo(Info.TargetSize, 8) / 8;
109
578
  if (Value == 0)
110
553
    return; // Doesn't change encoding.
111
25
112
25
  // Shift the value into position.
113
25
  Value <<= Info.TargetOffset;
114
25
115
25
  unsigned Offset = Fixup.getOffset();
116
25
  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
117
25
118
25
  // For each byte of the fragment that the fixup touches, mask in the
119
25
  // bits from the fixup value.
120
125
  for (unsigned I = 0; I != NumBytes; 
++I100
)
121
100
    Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
122
25
}
123
124
std::unique_ptr<MCObjectTargetWriter>
125
443
WebAssemblyAsmBackend::createObjectTargetWriter() const {
126
443
  return createWebAssemblyWasmObjectWriter(Is64Bit);
127
443
}
128
129
} // end anonymous namespace
130
131
443
MCAsmBackend *llvm::createWebAssemblyAsmBackend(const Triple &TT) {
132
443
  return new WebAssemblyAsmBackend(TT.isArch64Bit());
133
443
}