Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/MCWasmStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/MC/MCWasmStreamer.cpp - Wasm Object Output ---------------------===//
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 assembles .s files and emits Wasm .o object files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/MC/MCWasmStreamer.h"
14
#include "llvm/ADT/STLExtras.h"
15
#include "llvm/ADT/SmallPtrSet.h"
16
#include "llvm/MC/MCAsmBackend.h"
17
#include "llvm/MC/MCAsmLayout.h"
18
#include "llvm/MC/MCAssembler.h"
19
#include "llvm/MC/MCCodeEmitter.h"
20
#include "llvm/MC/MCContext.h"
21
#include "llvm/MC/MCExpr.h"
22
#include "llvm/MC/MCInst.h"
23
#include "llvm/MC/MCObjectStreamer.h"
24
#include "llvm/MC/MCSection.h"
25
#include "llvm/MC/MCSectionWasm.h"
26
#include "llvm/MC/MCSymbol.h"
27
#include "llvm/MC/MCSymbolWasm.h"
28
#include "llvm/MC/MCValue.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/Debug.h"
31
#include "llvm/Support/ErrorHandling.h"
32
#include "llvm/Support/TargetRegistry.h"
33
#include "llvm/Support/raw_ostream.h"
34
35
using namespace llvm;
36
37
208
MCWasmStreamer::~MCWasmStreamer() = default; // anchor.
38
39
0
void MCWasmStreamer::mergeFragment(MCDataFragment *DF, MCDataFragment *EF) {
40
0
  flushPendingLabels(DF, DF->getContents().size());
41
0
42
0
  for (unsigned I = 0, E = EF->getFixups().size(); I != E; ++I) {
43
0
    EF->getFixups()[I].setOffset(EF->getFixups()[I].getOffset() +
44
0
                                 DF->getContents().size());
45
0
    DF->getFixups().push_back(EF->getFixups()[I]);
46
0
  }
47
0
  if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
48
0
    DF->setHasInstructions(*EF->getSubtargetInfo());
49
0
  DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
50
0
}
51
52
0
void MCWasmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
53
0
  // Let the target do whatever target specific stuff it needs to do.
54
0
  getAssembler().getBackend().handleAssemblerFlag(Flag);
55
0
56
0
  // Do any generic stuff we need to do.
57
0
  llvm_unreachable("invalid assembler flag!");
58
0
}
59
60
void MCWasmStreamer::ChangeSection(MCSection *Section,
61
895
                                   const MCExpr *Subsection) {
62
895
  MCAssembler &Asm = getAssembler();
63
895
  auto *SectionWasm = cast<MCSectionWasm>(Section);
64
895
  const MCSymbol *Grp = SectionWasm->getGroup();
65
895
  if (Grp)
66
13
    Asm.registerSymbol(*Grp);
67
895
68
895
  this->MCObjectStreamer::ChangeSection(Section, Subsection);
69
895
  Asm.registerSymbol(*Section->getBeginSymbol());
70
895
}
71
72
void MCWasmStreamer::EmitWeakReference(MCSymbol *Alias,
73
0
                                       const MCSymbol *Symbol) {
74
0
  getAssembler().registerSymbol(*Symbol);
75
0
  const MCExpr *Value = MCSymbolRefExpr::create(
76
0
      Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
77
0
  Alias->setVariableValue(Value);
78
0
}
79
80
1.21k
bool MCWasmStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
81
1.21k
  assert(Attribute != MCSA_IndirectSymbol && "indirect symbols not supported");
82
1.21k
83
1.21k
  auto *Symbol = cast<MCSymbolWasm>(S);
84
1.21k
85
1.21k
  // Adding a symbol attribute always introduces the symbol; note that an
86
1.21k
  // important side effect of calling registerSymbol here is to register the
87
1.21k
  // symbol with the assembler.
88
1.21k
  getAssembler().registerSymbol(*Symbol);
89
1.21k
90
1.21k
  switch (Attribute) {
91
1.21k
  case MCSA_LazyReference:
92
0
  case MCSA_Reference:
93
0
  case MCSA_SymbolResolver:
94
0
  case MCSA_PrivateExtern:
95
0
  case MCSA_WeakDefinition:
96
0
  case MCSA_WeakDefAutoPrivate:
97
0
  case MCSA_Invalid:
98
0
  case MCSA_IndirectSymbol:
99
0
  case MCSA_Protected:
100
0
    return false;
101
0
102
137
  case MCSA_Hidden:
103
137
    Symbol->setHidden(true);
104
137
    break;
105
0
106
33
  case MCSA_Weak:
107
33
  case MCSA_WeakReference:
108
33
    Symbol->setWeak(true);
109
33
    Symbol->setExternal(true);
110
33
    break;
111
33
112
489
  case MCSA_Global:
113
489
    Symbol->setExternal(true);
114
489
    break;
115
33
116
426
  case MCSA_ELF_TypeFunction:
117
426
    Symbol->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION);
118
426
    break;
119
33
120
130
  case MCSA_ELF_TypeObject:
121
130
  case MCSA_Cold:
122
130
    break;
123
130
124
130
  case MCSA_NoDeadStrip:
125
4
    Symbol->setExported();
126
4
    break;
127
130
128
130
  default:
129
0
    // unrecognized directive
130
0
    llvm_unreachable("unexpected MCSymbolAttr");
131
130
    
return false0
;
132
1.21k
  }
133
1.21k
134
1.21k
  return true;
135
1.21k
}
136
137
void MCWasmStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
138
0
                                      unsigned ByteAlignment) {
139
0
  llvm_unreachable("Common symbols are not yet implemented for Wasm");
140
0
}
141
142
584
void MCWasmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
143
584
  cast<MCSymbolWasm>(Symbol)->setSize(Value);
144
584
}
145
146
void MCWasmStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
147
0
                                           unsigned ByteAlignment) {
148
0
  llvm_unreachable("Local common symbols are not yet implemented for Wasm");
149
0
}
150
151
void MCWasmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
152
188
                                   SMLoc Loc) {
153
188
  MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
154
188
}
155
156
void MCWasmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
157
                                          unsigned ValueSize,
158
114
                                          unsigned MaxBytesToEmit) {
159
114
  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, ValueSize,
160
114
                                         MaxBytesToEmit);
161
114
}
162
163
7
void MCWasmStreamer::EmitIdent(StringRef IdentString) {
164
7
  // TODO(sbc): Add the ident section once we support mergable strings
165
7
  // sections in the object format
166
7
}
167
168
void MCWasmStreamer::EmitInstToFragment(const MCInst &Inst,
169
0
                                        const MCSubtargetInfo &STI) {
170
0
  this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
171
0
}
172
173
void MCWasmStreamer::EmitInstToData(const MCInst &Inst,
174
1.40k
                                    const MCSubtargetInfo &STI) {
175
1.40k
  MCAssembler &Assembler = getAssembler();
176
1.40k
  SmallVector<MCFixup, 4> Fixups;
177
1.40k
  SmallString<256> Code;
178
1.40k
  raw_svector_ostream VecOS(Code);
179
1.40k
  Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
180
1.40k
181
1.40k
  // Append the encoded instruction to the current data fragment (or create a
182
1.40k
  // new such fragment if the current fragment is not a data fragment).
183
1.40k
  MCDataFragment *DF = getOrCreateDataFragment();
184
1.40k
185
1.40k
  // Add the fixups and data.
186
1.80k
  for (unsigned I = 0, E = Fixups.size(); I != E; 
++I400
) {
187
400
    Fixups[I].setOffset(Fixups[I].getOffset() + DF->getContents().size());
188
400
    DF->getFixups().push_back(Fixups[I]);
189
400
  }
190
1.40k
  DF->setHasInstructions(STI);
191
1.40k
  DF->getContents().append(Code.begin(), Code.end());
192
1.40k
}
193
194
209
void MCWasmStreamer::FinishImpl() {
195
209
  EmitFrames(nullptr);
196
209
197
209
  this->MCObjectStreamer::FinishImpl();
198
209
}
199
200
MCStreamer *llvm::createWasmStreamer(MCContext &Context,
201
                                     std::unique_ptr<MCAsmBackend> &&MAB,
202
                                     std::unique_ptr<MCObjectWriter> &&OW,
203
                                     std::unique_ptr<MCCodeEmitter> &&CE,
204
209
                                     bool RelaxAll) {
205
209
  MCWasmStreamer *S =
206
209
      new MCWasmStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
207
209
  if (RelaxAll)
208
0
    S->getAssembler().setRelaxAll(true);
209
209
  return S;
210
209
}
211
212
0
void MCWasmStreamer::EmitThumbFunc(MCSymbol *Func) {
213
0
  llvm_unreachable("Generic Wasm doesn't support this directive");
214
0
}
215
216
0
void MCWasmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
217
0
  llvm_unreachable("Wasm doesn't support this directive");
218
0
}
219
220
void MCWasmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
221
                                  uint64_t Size, unsigned ByteAlignment,
222
0
                                  SMLoc Loc) {
223
0
  llvm_unreachable("Wasm doesn't support this directive");
224
0
}
225
226
void MCWasmStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
227
0
                                    uint64_t Size, unsigned ByteAlignment) {
228
0
  llvm_unreachable("Wasm doesn't support this directive");
229
0
}