Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- SparcMCExpr.cpp - Sparc specific MC expression classes --------===//
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 contains the implementation of the assembly expression modifiers
10
// accepted by the Sparc architecture (e.g. "%hi", "%lo", ...).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "SparcMCExpr.h"
15
#include "llvm/BinaryFormat/ELF.h"
16
#include "llvm/MC/MCAssembler.h"
17
#include "llvm/MC/MCContext.h"
18
#include "llvm/MC/MCObjectStreamer.h"
19
#include "llvm/MC/MCSymbolELF.h"
20
21
using namespace llvm;
22
23
#define DEBUG_TYPE "sparcmcexpr"
24
25
const SparcMCExpr*
26
SparcMCExpr::create(VariantKind Kind, const MCExpr *Expr,
27
1.81k
                      MCContext &Ctx) {
28
1.81k
    return new (Ctx) SparcMCExpr(Kind, Expr);
29
1.81k
}
30
31
2.19k
void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
32
2.19k
33
2.19k
  bool closeParen = printVariantKind(OS, Kind);
34
2.19k
35
2.19k
  const MCExpr *Expr = getSubExpr();
36
2.19k
  Expr->print(OS, MAI);
37
2.19k
38
2.19k
  if (closeParen)
39
666
    OS << ')';
40
2.19k
}
41
42
bool SparcMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind)
43
2.37k
{
44
2.37k
  bool closeParen = true;
45
2.37k
  switch (Kind) {
46
2.37k
  
case VK_Sparc_None: closeParen = false; break703
;
47
2.37k
  
case VK_Sparc_LO: OS << "%lo("; break156
;
48
2.37k
  
case VK_Sparc_HI: OS << "%hi("; break169
;
49
2.37k
  
case VK_Sparc_H44: OS << "%h44("; break35
;
50
2.37k
  
case VK_Sparc_M44: OS << "%m44("; break35
;
51
2.37k
  
case VK_Sparc_L44: OS << "%l44("; break40
;
52
2.37k
  
case VK_Sparc_HH: OS << "%hh("; break9
;
53
2.37k
  
case VK_Sparc_HM: OS << "%hm("; break9
;
54
2.37k
    // FIXME: use %pc22/%pc10, if system assembler supports them.
55
2.37k
  
case VK_Sparc_PC22: OS << "%hi("; break23
;
56
2.37k
  
case VK_Sparc_PC10: OS << "%lo("; break23
;
57
2.37k
    // FIXME: use %got22/%got10, if system assembler supports them.
58
2.37k
  
case VK_Sparc_GOT22: OS << "%hi("; break30
;
59
2.37k
  
case VK_Sparc_GOT10: OS << "%lo("; break30
;
60
2.37k
  
case VK_Sparc_GOT13: closeParen = false; break1
;
61
2.37k
  
case VK_Sparc_13: closeParen = false; break983
;
62
2.37k
  
case VK_Sparc_WPLT30: closeParen = false; break20
;
63
2.37k
  
case VK_Sparc_R_DISP32: OS << "%r_disp32("; break4
;
64
2.37k
  
case VK_Sparc_TLS_GD_HI22: OS << "%tgd_hi22("; break6
;
65
2.37k
  
case VK_Sparc_TLS_GD_LO10: OS << "%tgd_lo10("; break6
;
66
2.37k
  
case VK_Sparc_TLS_GD_ADD: OS << "%tgd_add("; break6
;
67
2.37k
  
case VK_Sparc_TLS_GD_CALL: OS << "%tgd_call("; break6
;
68
2.37k
  
case VK_Sparc_TLS_LDM_HI22: OS << "%tldm_hi22("; break6
;
69
2.37k
  
case VK_Sparc_TLS_LDM_LO10: OS << "%tldm_lo10("; break6
;
70
2.37k
  
case VK_Sparc_TLS_LDM_ADD: OS << "%tldm_add("; break6
;
71
2.37k
  
case VK_Sparc_TLS_LDM_CALL: OS << "%tldm_call("; break6
;
72
2.37k
  
case VK_Sparc_TLS_LDO_HIX22: OS << "%tldo_hix22("; break6
;
73
2.37k
  
case VK_Sparc_TLS_LDO_LOX10: OS << "%tldo_lox10("; break6
;
74
2.37k
  
case VK_Sparc_TLS_LDO_ADD: OS << "%tldo_add("; break6
;
75
2.37k
  
case VK_Sparc_TLS_IE_HI22: OS << "%tie_hi22("; break6
;
76
2.37k
  
case VK_Sparc_TLS_IE_LO10: OS << "%tie_lo10("; break6
;
77
2.37k
  
case VK_Sparc_TLS_IE_LD: OS << "%tie_ld("; break5
;
78
2.37k
  
case VK_Sparc_TLS_IE_LDX: OS << "%tie_ldx("; break5
;
79
2.37k
  
case VK_Sparc_TLS_IE_ADD: OS << "%tie_add("; break6
;
80
2.37k
  
case VK_Sparc_TLS_LE_HIX22: OS << "%tle_hix22("; break6
;
81
2.37k
  
case VK_Sparc_TLS_LE_LOX10: OS << "%tle_lox10("; break6
;
82
2.37k
  }
83
2.37k
  return closeParen;
84
2.37k
}
85
86
SparcMCExpr::VariantKind SparcMCExpr::parseVariantKind(StringRef name)
87
144
{
88
144
  return StringSwitch<SparcMCExpr::VariantKind>(name)
89
144
    .Case("lo",  VK_Sparc_LO)
90
144
    .Case("hi",  VK_Sparc_HI)
91
144
    .Case("h44", VK_Sparc_H44)
92
144
    .Case("m44", VK_Sparc_M44)
93
144
    .Case("l44", VK_Sparc_L44)
94
144
    .Case("hh",  VK_Sparc_HH)
95
144
    .Case("hm",  VK_Sparc_HM)
96
144
    .Case("pc22",  VK_Sparc_PC22)
97
144
    .Case("pc10",  VK_Sparc_PC10)
98
144
    .Case("got22", VK_Sparc_GOT22)
99
144
    .Case("got10", VK_Sparc_GOT10)
100
144
    .Case("got13", VK_Sparc_GOT13)
101
144
    .Case("r_disp32",   VK_Sparc_R_DISP32)
102
144
    .Case("tgd_hi22",   VK_Sparc_TLS_GD_HI22)
103
144
    .Case("tgd_lo10",   VK_Sparc_TLS_GD_LO10)
104
144
    .Case("tgd_add",    VK_Sparc_TLS_GD_ADD)
105
144
    .Case("tgd_call",   VK_Sparc_TLS_GD_CALL)
106
144
    .Case("tldm_hi22",  VK_Sparc_TLS_LDM_HI22)
107
144
    .Case("tldm_lo10",  VK_Sparc_TLS_LDM_LO10)
108
144
    .Case("tldm_add",   VK_Sparc_TLS_LDM_ADD)
109
144
    .Case("tldm_call",  VK_Sparc_TLS_LDM_CALL)
110
144
    .Case("tldo_hix22", VK_Sparc_TLS_LDO_HIX22)
111
144
    .Case("tldo_lox10", VK_Sparc_TLS_LDO_LOX10)
112
144
    .Case("tldo_add",   VK_Sparc_TLS_LDO_ADD)
113
144
    .Case("tie_hi22",   VK_Sparc_TLS_IE_HI22)
114
144
    .Case("tie_lo10",   VK_Sparc_TLS_IE_LO10)
115
144
    .Case("tie_ld",     VK_Sparc_TLS_IE_LD)
116
144
    .Case("tie_ldx",    VK_Sparc_TLS_IE_LDX)
117
144
    .Case("tie_add",    VK_Sparc_TLS_IE_ADD)
118
144
    .Case("tle_hix22",  VK_Sparc_TLS_LE_HIX22)
119
144
    .Case("tle_lox10",  VK_Sparc_TLS_LE_LOX10)
120
144
    .Default(VK_Sparc_None);
121
144
}
122
123
274
Sparc::Fixups SparcMCExpr::getFixupKind(SparcMCExpr::VariantKind Kind) {
124
274
  switch (Kind) {
125
274
  
default: 0
llvm_unreachable0
("Unhandled SparcMCExpr::VariantKind");
126
274
  
case VK_Sparc_LO: return Sparc::fixup_sparc_lo1032
;
127
274
  
case VK_Sparc_HI: return Sparc::fixup_sparc_hi2248
;
128
274
  
case VK_Sparc_H44: return Sparc::fixup_sparc_h446
;
129
274
  
case VK_Sparc_M44: return Sparc::fixup_sparc_m446
;
130
274
  
case VK_Sparc_L44: return Sparc::fixup_sparc_l446
;
131
274
  
case VK_Sparc_HH: return Sparc::fixup_sparc_hh2
;
132
274
  
case VK_Sparc_HM: return Sparc::fixup_sparc_hm2
;
133
274
  
case VK_Sparc_PC22: return Sparc::fixup_sparc_pc228
;
134
274
  
case VK_Sparc_PC10: return Sparc::fixup_sparc_pc108
;
135
274
  
case VK_Sparc_GOT22: return Sparc::fixup_sparc_got225
;
136
274
  
case VK_Sparc_GOT10: return Sparc::fixup_sparc_got105
;
137
274
  
case VK_Sparc_GOT13: return Sparc::fixup_sparc_got131
;
138
274
  
case VK_Sparc_13: return Sparc::fixup_sparc_133
;
139
274
  
case VK_Sparc_WPLT30: return Sparc::fixup_sparc_wplt300
;
140
274
  
case VK_Sparc_TLS_GD_HI22: return Sparc::fixup_sparc_tls_gd_hi228
;
141
274
  
case VK_Sparc_TLS_GD_LO10: return Sparc::fixup_sparc_tls_gd_lo108
;
142
274
  
case VK_Sparc_TLS_GD_ADD: return Sparc::fixup_sparc_tls_gd_add8
;
143
274
  
case VK_Sparc_TLS_GD_CALL: return Sparc::fixup_sparc_tls_gd_call8
;
144
274
  
case VK_Sparc_TLS_LDM_HI22: return Sparc::fixup_sparc_tls_ldm_hi228
;
145
274
  
case VK_Sparc_TLS_LDM_LO10: return Sparc::fixup_sparc_tls_ldm_lo108
;
146
274
  
case VK_Sparc_TLS_LDM_ADD: return Sparc::fixup_sparc_tls_ldm_add8
;
147
274
  
case VK_Sparc_TLS_LDM_CALL: return Sparc::fixup_sparc_tls_ldm_call8
;
148
274
  
case VK_Sparc_TLS_LDO_HIX22: return Sparc::fixup_sparc_tls_ldo_hix228
;
149
274
  
case VK_Sparc_TLS_LDO_LOX10: return Sparc::fixup_sparc_tls_ldo_lox108
;
150
274
  
case VK_Sparc_TLS_LDO_ADD: return Sparc::fixup_sparc_tls_ldo_add8
;
151
274
  
case VK_Sparc_TLS_IE_HI22: return Sparc::fixup_sparc_tls_ie_hi228
;
152
274
  
case VK_Sparc_TLS_IE_LO10: return Sparc::fixup_sparc_tls_ie_lo108
;
153
274
  
case VK_Sparc_TLS_IE_LD: return Sparc::fixup_sparc_tls_ie_ld7
;
154
274
  
case VK_Sparc_TLS_IE_LDX: return Sparc::fixup_sparc_tls_ie_ldx7
;
155
274
  
case VK_Sparc_TLS_IE_ADD: return Sparc::fixup_sparc_tls_ie_add8
;
156
274
  
case VK_Sparc_TLS_LE_HIX22: return Sparc::fixup_sparc_tls_le_hix228
;
157
274
  
case VK_Sparc_TLS_LE_LOX10: return Sparc::fixup_sparc_tls_le_lox108
;
158
274
  }
159
274
}
160
161
bool
162
SparcMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
163
                                       const MCAsmLayout *Layout,
164
224
                                       const MCFixup *Fixup) const {
165
224
  return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
166
224
}
167
168
106
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
169
106
  switch (Expr->getKind()) {
170
106
  case MCExpr::Target:
171
0
    llvm_unreachable("Can't handle nested target expr!");
172
106
    
break0
;
173
106
174
106
  case MCExpr::Constant:
175
0
    break;
176
106
177
106
  case MCExpr::Binary: {
178
0
    const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
179
0
    fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
180
0
    fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
181
0
    break;
182
106
  }
183
106
184
106
  case MCExpr::SymbolRef: {
185
106
    const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
186
106
    cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
187
106
    break;
188
106
  }
189
106
190
106
  case MCExpr::Unary:
191
0
    fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
192
0
    break;
193
106
  }
194
106
195
106
}
196
197
202
void SparcMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
198
202
  switch(getKind()) {
199
202
  
default: return96
;
200
202
  case VK_Sparc_TLS_GD_CALL:
201
12
  case VK_Sparc_TLS_LDM_CALL: {
202
12
    // The corresponding relocations reference __tls_get_addr, as they call it,
203
12
    // but this is only implicit; we must explicitly add it to our symbol table
204
12
    // to bind it for these uses.
205
12
    MCSymbol *Symbol = Asm.getContext().getOrCreateSymbol("__tls_get_addr");
206
12
    Asm.registerSymbol(*Symbol);
207
12
    auto ELFSymbol = cast<MCSymbolELF>(Symbol);
208
12
    if (!ELFSymbol->isBindingSet()) {
209
6
      ELFSymbol->setBinding(ELF::STB_GLOBAL);
210
6
      ELFSymbol->setExternal(true);
211
6
    }
212
12
    LLVM_FALLTHROUGH;
213
12
  }
214
106
  case VK_Sparc_TLS_GD_HI22:
215
106
  case VK_Sparc_TLS_GD_LO10:
216
106
  case VK_Sparc_TLS_GD_ADD:
217
106
  case VK_Sparc_TLS_LDM_HI22:
218
106
  case VK_Sparc_TLS_LDM_LO10:
219
106
  case VK_Sparc_TLS_LDM_ADD:
220
106
  case VK_Sparc_TLS_LDO_HIX22:
221
106
  case VK_Sparc_TLS_LDO_LOX10:
222
106
  case VK_Sparc_TLS_LDO_ADD:
223
106
  case VK_Sparc_TLS_IE_HI22:
224
106
  case VK_Sparc_TLS_IE_LO10:
225
106
  case VK_Sparc_TLS_IE_LD:
226
106
  case VK_Sparc_TLS_IE_LDX:
227
106
  case VK_Sparc_TLS_IE_ADD:
228
106
  case VK_Sparc_TLS_LE_HIX22:
229
106
  case VK_Sparc_TLS_LE_LOX10: break;
230
106
  }
231
106
  fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
232
106
}
233
234
222
void SparcMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
235
222
  Streamer.visitUsedExpr(*getSubExpr());
236
222
}