Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- AsmPrinterDwarf.cpp - AsmPrinter Dwarf Support --------------------===//
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 implements the Dwarf emissions parts of AsmPrinter.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "ByteStreamer.h"
14
#include "llvm/ADT/Twine.h"
15
#include "llvm/BinaryFormat/Dwarf.h"
16
#include "llvm/CodeGen/AsmPrinter.h"
17
#include "llvm/CodeGen/DIE.h"
18
#include "llvm/CodeGen/MachineFunction.h"
19
#include "llvm/IR/DataLayout.h"
20
#include "llvm/MC/MCAsmInfo.h"
21
#include "llvm/MC/MCDwarf.h"
22
#include "llvm/MC/MCRegisterInfo.h"
23
#include "llvm/MC/MCSection.h"
24
#include "llvm/MC/MCStreamer.h"
25
#include "llvm/MC/MCSymbol.h"
26
#include "llvm/MC/MachineLocation.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Target/TargetLoweringObjectFile.h"
29
#include "llvm/Target/TargetMachine.h"
30
using namespace llvm;
31
32
#define DEBUG_TYPE "asm-printer"
33
34
//===----------------------------------------------------------------------===//
35
// Dwarf Emission Helper Routines
36
//===----------------------------------------------------------------------===//
37
38
/// EmitSLEB128 - emit the specified signed leb128 value.
39
5.03k
void AsmPrinter::EmitSLEB128(int64_t Value, const char *Desc) const {
40
5.03k
  if (isVerbose() && 
Desc746
)
41
0
    OutStreamer->AddComment(Desc);
42
5.03k
43
5.03k
  OutStreamer->EmitSLEB128IntValue(Value);
44
5.03k
}
45
46
737k
void AsmPrinter::EmitULEB128(uint64_t Value, const char *Desc, unsigned PadTo) const {
47
737k
  if (isVerbose() && 
Desc44.5k
)
48
37.9k
    OutStreamer->AddComment(Desc);
49
737k
50
737k
  OutStreamer->EmitULEB128IntValue(Value, PadTo);
51
737k
}
52
53
/// Emit something like ".uleb128 Hi-Lo".
54
void AsmPrinter::EmitLabelDifferenceAsULEB128(const MCSymbol *Hi,
55
77.9k
                                              const MCSymbol *Lo) const {
56
77.9k
  OutStreamer->emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
57
77.9k
}
58
59
945
static const char *DecodeDWARFEncoding(unsigned Encoding) {
60
945
  switch (Encoding) {
61
945
  case dwarf::DW_EH_PE_absptr:
62
109
    return "absptr";
63
945
  case dwarf::DW_EH_PE_omit:
64
410
    return "omit";
65
945
  case dwarf::DW_EH_PE_pcrel:
66
0
    return "pcrel";
67
945
  case dwarf::DW_EH_PE_uleb128:
68
271
    return "uleb128";
69
945
  case dwarf::DW_EH_PE_sleb128:
70
0
    return "sleb128";
71
945
  case dwarf::DW_EH_PE_udata4:
72
82
    return "udata4";
73
945
  case dwarf::DW_EH_PE_udata8:
74
0
    return "udata8";
75
945
  case dwarf::DW_EH_PE_sdata4:
76
0
    return "sdata4";
77
945
  case dwarf::DW_EH_PE_sdata8:
78
0
    return "sdata8";
79
945
  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
80
0
    return "pcrel udata4";
81
945
  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
82
0
    return "pcrel sdata4";
83
945
  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
84
0
    return "pcrel udata8";
85
945
  case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
86
0
    return "pcrel sdata8";
87
945
  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4
88
0
      :
89
0
    return "indirect pcrel udata4";
90
945
  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4
91
62
      :
92
62
    return "indirect pcrel sdata4";
93
945
  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8
94
5
      :
95
5
    return "indirect pcrel udata8";
96
945
  case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8
97
6
      :
98
6
    return "indirect pcrel sdata8";
99
0
  }
100
0
101
0
  return "<unknown encoding>";
102
0
}
103
104
/// EmitEncodingByte - Emit a .byte 42 directive that corresponds to an
105
/// encoding.  If verbose assembly output is enabled, we output comments
106
/// describing the encoding.  Desc is an optional string saying what the
107
/// encoding is specifying (e.g. "LSDA").
108
10.3k
void AsmPrinter::EmitEncodingByte(unsigned Val, const char *Desc) const {
109
10.3k
  if (isVerbose()) {
110
945
    if (Desc)
111
945
      OutStreamer->AddComment(Twine(Desc) + " Encoding = " +
112
945
                              Twine(DecodeDWARFEncoding(Val)));
113
0
    else
114
0
      OutStreamer->AddComment(Twine("Encoding = ") + DecodeDWARFEncoding(Val));
115
945
  }
116
10.3k
117
10.3k
  OutStreamer->EmitIntValue(Val, 1);
118
10.3k
}
119
120
/// GetSizeOfEncodedValue - Return the size of the encoding in bytes.
121
2.03k
unsigned AsmPrinter::GetSizeOfEncodedValue(unsigned Encoding) const {
122
2.03k
  if (Encoding == dwarf::DW_EH_PE_omit)
123
0
    return 0;
124
2.03k
125
2.03k
  switch (Encoding & 0x07) {
126
2.03k
  default:
127
0
    llvm_unreachable("Invalid encoded value.");
128
2.03k
  case dwarf::DW_EH_PE_absptr:
129
129
    return MF->getDataLayout().getPointerSize();
130
2.03k
  case dwarf::DW_EH_PE_udata2:
131
0
    return 2;
132
2.03k
  case dwarf::DW_EH_PE_udata4:
133
1.89k
    return 4;
134
2.03k
  case dwarf::DW_EH_PE_udata8:
135
8
    return 8;
136
2.03k
  }
137
2.03k
}
138
139
void AsmPrinter::EmitTTypeReference(const GlobalValue *GV,
140
1.96k
                                    unsigned Encoding) const {
141
1.96k
  if (GV) {
142
258
    const TargetLoweringObjectFile &TLOF = getObjFileLowering();
143
258
144
258
    const MCExpr *Exp =
145
258
        TLOF.getTTypeGlobalReference(GV, Encoding, TM, MMI, *OutStreamer);
146
258
    OutStreamer->EmitValue(Exp, GetSizeOfEncodedValue(Encoding));
147
258
  } else
148
1.70k
    OutStreamer->EmitIntValue(0, GetSizeOfEncodedValue(Encoding));
149
1.96k
}
150
151
void AsmPrinter::emitDwarfSymbolReference(const MCSymbol *Label,
152
7.40k
                                          bool ForceOffset) const {
153
7.40k
  if (!ForceOffset) {
154
7.39k
    // On COFF targets, we have to emit the special .secrel32 directive.
155
7.39k
    if (MAI->needsDwarfSectionOffsetDirective()) {
156
124
      OutStreamer->EmitCOFFSecRel32(Label, /*Offset=*/0);
157
124
      return;
158
124
    }
159
7.26k
160
7.26k
    // If the format uses relocations with dwarf, refer to the symbol directly.
161
7.26k
    if (MAI->doesDwarfUseRelocationsAcrossSections()) {
162
1.93k
      OutStreamer->EmitSymbolValue(Label, 4);
163
1.93k
      return;
164
1.93k
    }
165
5.34k
  }
166
5.34k
167
5.34k
  // Otherwise, emit it as a label difference from the start of the section.
168
5.34k
  EmitLabelDifference(Label, Label->getSection().getBeginSymbol(), 4);
169
5.34k
}
170
171
72.4k
void AsmPrinter::emitDwarfStringOffset(DwarfStringPoolEntry S) const {
172
72.4k
  if (MAI->doesDwarfUseRelocationsAcrossSections()) {
173
468
    assert(S.Symbol && "No symbol available");
174
468
    emitDwarfSymbolReference(S.Symbol);
175
468
    return;
176
468
  }
177
72.0k
178
72.0k
  // Just emit the offset directly; no need for symbol math.
179
72.0k
  emitInt32(S.Offset);
180
72.0k
}
181
182
2
void AsmPrinter::EmitDwarfOffset(const MCSymbol *Label, uint64_t Offset) const {
183
2
  EmitLabelPlusOffset(Label, Offset, MAI->getCodePointerSize());
184
2
}
185
186
void AsmPrinter::EmitCallSiteOffset(const MCSymbol *Hi,
187
                                    const MCSymbol *Lo,
188
72.6k
                                    unsigned Encoding) const {
189
72.6k
  // The least significant 3 bits specify the width of the encoding
190
72.6k
  if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
191
72.5k
    EmitLabelDifferenceAsULEB128(Hi, Lo);
192
60
  else
193
60
    EmitLabelDifference(Hi, Lo, GetSizeOfEncodedValue(Encoding));
194
72.6k
}
195
196
void AsmPrinter::EmitCallSiteValue(uint64_t Value,
197
8.81k
                                   unsigned Encoding) const {
198
8.81k
  // The least significant 3 bits specify the width of the encoding
199
8.81k
  if ((Encoding & 0x7) == dwarf::DW_EH_PE_uleb128)
200
8.80k
    EmitULEB128(Value);
201
12
  else
202
12
    OutStreamer->EmitIntValue(Value, GetSizeOfEncodedValue(Encoding));
203
8.81k
}
204
205
//===----------------------------------------------------------------------===//
206
// Dwarf Lowering Routines
207
//===----------------------------------------------------------------------===//
208
209
426k
void AsmPrinter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
210
426k
  switch (Inst.getOperation()) {
211
426k
  default:
212
0
    llvm_unreachable("Unexpected instruction");
213
426k
  case MCCFIInstruction::OpDefCfaOffset:
214
42.5k
    OutStreamer->EmitCFIDefCfaOffset(Inst.getOffset());
215
42.5k
    break;
216
426k
  case MCCFIInstruction::OpAdjustCfaOffset:
217
1.66k
    OutStreamer->EmitCFIAdjustCfaOffset(Inst.getOffset());
218
1.66k
    break;
219
426k
  case MCCFIInstruction::OpDefCfa:
220
34.4k
    OutStreamer->EmitCFIDefCfa(Inst.getRegister(), Inst.getOffset());
221
34.4k
    break;
222
426k
  case MCCFIInstruction::OpDefCfaRegister:
223
23.5k
    OutStreamer->EmitCFIDefCfaRegister(Inst.getRegister());
224
23.5k
    break;
225
426k
  case MCCFIInstruction::OpOffset:
226
323k
    OutStreamer->EmitCFIOffset(Inst.getRegister(), Inst.getOffset());
227
323k
    break;
228
426k
  case MCCFIInstruction::OpRegister:
229
374
    OutStreamer->EmitCFIRegister(Inst.getRegister(), Inst.getRegister2());
230
374
    break;
231
426k
  case MCCFIInstruction::OpWindowSave:
232
374
    OutStreamer->EmitCFIWindowSave();
233
374
    break;
234
426k
  case MCCFIInstruction::OpNegateRAState:
235
11
    OutStreamer->EmitCFINegateRAState();
236
11
    break;
237
426k
  case MCCFIInstruction::OpSameValue:
238
4
    OutStreamer->EmitCFISameValue(Inst.getRegister());
239
4
    break;
240
426k
  case MCCFIInstruction::OpGnuArgsSize:
241
32
    OutStreamer->EmitCFIGnuArgsSize(Inst.getOffset());
242
32
    break;
243
426k
  case MCCFIInstruction::OpEscape:
244
2
    OutStreamer->EmitCFIEscape(Inst.getValues());
245
2
    break;
246
426k
  case MCCFIInstruction::OpRestore:
247
72
    OutStreamer->EmitCFIRestore(Inst.getRegister());
248
72
    break;
249
426k
  }
250
426k
}
251
252
385k
void AsmPrinter::emitDwarfDIE(const DIE &Die) const {
253
385k
  // Emit the code (index) for the abbreviation.
254
385k
  if (isVerbose())
255
4.41k
    OutStreamer->AddComment("Abbrev [" + Twine(Die.getAbbrevNumber()) + "] 0x" +
256
4.41k
                            Twine::utohexstr(Die.getOffset()) + ":0x" +
257
4.41k
                            Twine::utohexstr(Die.getSize()) + " " +
258
4.41k
                            dwarf::TagString(Die.getTag()));
259
385k
  EmitULEB128(Die.getAbbrevNumber());
260
385k
261
385k
  // Emit the DIE attribute values.
262
1.84M
  for (const auto &V : Die.values()) {
263
1.84M
    dwarf::Attribute Attr = V.getAttribute();
264
1.84M
    assert(V.getForm() && "Too many attributes for DIE (check abbreviation)");
265
1.84M
266
1.84M
    if (isVerbose()) {
267
18.8k
      OutStreamer->AddComment(dwarf::AttributeString(Attr));
268
18.8k
      if (Attr == dwarf::DW_AT_accessibility)
269
39
        OutStreamer->AddComment(
270
39
            dwarf::AccessibilityString(V.getDIEInteger().getValue()));
271
18.8k
    }
272
1.84M
273
1.84M
    // Emit an attribute using the defined form.
274
1.84M
    V.EmitValue(this);
275
1.84M
  }
276
385k
277
385k
  // Emit the DIE children if any.
278
385k
  if (Die.hasChildren()) {
279
167k
    for (auto &Child : Die.children())
280
382k
      emitDwarfDIE(Child);
281
167k
282
167k
    OutStreamer->AddComment("End Of Children Mark");
283
167k
    emitInt8(0);
284
167k
  }
285
385k
}
286
287
21.1k
void AsmPrinter::emitDwarfAbbrev(const DIEAbbrev &Abbrev) const {
288
21.1k
  // Emit the abbreviations code (base 1 index.)
289
21.1k
  EmitULEB128(Abbrev.getNumber(), "Abbreviation Code");
290
21.1k
291
21.1k
  // Emit the abbreviations data.
292
21.1k
  Abbrev.Emit(this);
293
21.1k
}