Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/Mips/MCTargetDesc/MipsMCExpr.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- MipsMCExpr.cpp - Mips 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
#include "MipsMCExpr.h"
10
#include "llvm/BinaryFormat/ELF.h"
11
#include "llvm/MC/MCAsmInfo.h"
12
#include "llvm/MC/MCAssembler.h"
13
#include "llvm/MC/MCContext.h"
14
#include "llvm/MC/MCStreamer.h"
15
#include "llvm/MC/MCSymbolELF.h"
16
#include "llvm/MC/MCValue.h"
17
#include "llvm/Support/Casting.h"
18
#include "llvm/Support/ErrorHandling.h"
19
#include "llvm/Support/MathExtras.h"
20
#include "llvm/Support/raw_ostream.h"
21
#include <cstdint>
22
23
using namespace llvm;
24
25
#define DEBUG_TYPE "mipsmcexpr"
26
27
const MipsMCExpr *MipsMCExpr::create(MipsMCExpr::MipsExprKind Kind,
28
41.7k
                                     const MCExpr *Expr, MCContext &Ctx) {
29
41.7k
  return new (Ctx) MipsMCExpr(Kind, Expr);
30
41.7k
}
31
32
const MipsMCExpr *MipsMCExpr::createGpOff(MipsMCExpr::MipsExprKind Kind,
33
2.75k
                                          const MCExpr *Expr, MCContext &Ctx) {
34
2.75k
  return create(Kind, create(MEK_NEG, create(MEK_GPREL, Expr, Ctx), Ctx), Ctx);
35
2.75k
}
36
37
32.5k
void MipsMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
38
32.5k
  int64_t AbsVal;
39
32.5k
40
32.5k
  switch (Kind) {
41
32.5k
  case MEK_None:
42
0
  case MEK_Special:
43
0
    llvm_unreachable("MEK_None and MEK_Special are invalid");
44
0
    break;
45
2
  case MEK_DTPREL:
46
2
    // MEK_DTPREL is used for marking TLS DIEExpr only
47
2
    // and contains a regular sub-expression.
48
2
    getSubExpr()->print(OS, MAI, true);
49
2
    return;
50
12
  case MEK_CALL_HI16:
51
12
    OS << "%call_hi";
52
12
    break;
53
12
  case MEK_CALL_LO16:
54
12
    OS << "%call_lo";
55
12
    break;
56
14
  case MEK_DTPREL_HI:
57
14
    OS << "%dtprel_hi";
58
14
    break;
59
18
  case MEK_DTPREL_LO:
60
18
    OS << "%dtprel_lo";
61
18
    break;
62
4.56k
  case MEK_GOT:
63
4.56k
    OS << "%got";
64
4.56k
    break;
65
19
  case MEK_GOTTPREL:
66
19
    OS << "%gottprel";
67
19
    break;
68
1.72k
  case MEK_GOT_CALL:
69
1.72k
    OS << "%call16";
70
1.72k
    break;
71
1.34k
  case MEK_GOT_DISP:
72
1.34k
    OS << "%got_disp";
73
1.34k
    break;
74
8
  case MEK_GOT_HI16:
75
8
    OS << "%got_hi";
76
8
    break;
77
8
  case MEK_GOT_LO16:
78
8
    OS << "%got_lo";
79
8
    break;
80
206
  case MEK_GOT_PAGE:
81
206
    OS << "%got_page";
82
206
    break;
83
208
  case MEK_GOT_OFST:
84
208
    OS << "%got_ofst";
85
208
    break;
86
2.80k
  case MEK_GPREL:
87
2.80k
    OS << "%gp_rel";
88
2.80k
    break;
89
8.03k
  case MEK_HI:
90
8.03k
    OS << "%hi";
91
8.03k
    break;
92
412
  case MEK_HIGHER:
93
412
    OS << "%higher";
94
412
    break;
95
412
  case MEK_HIGHEST:
96
412
    OS << "%highest";
97
412
    break;
98
9.89k
  case MEK_LO:
99
9.89k
    OS << "%lo";
100
9.89k
    break;
101
2.78k
  case MEK_NEG:
102
2.78k
    OS << "%neg";
103
2.78k
    break;
104
8
  case MEK_PCREL_HI16:
105
8
    OS << "%pcrel_hi";
106
8
    break;
107
8
  case MEK_PCREL_LO16:
108
8
    OS << "%pcrel_lo";
109
8
    break;
110
14
  case MEK_TLSGD:
111
14
    OS << "%tlsgd";
112
14
    break;
113
14
  case MEK_TLSLDM:
114
14
    OS << "%tlsldm";
115
14
    break;
116
21
  case MEK_TPREL_HI:
117
21
    OS << "%tprel_hi";
118
21
    break;
119
21
  case MEK_TPREL_LO:
120
21
    OS << "%tprel_lo";
121
21
    break;
122
32.5k
  }
123
32.5k
124
32.5k
  OS << '(';
125
32.5k
  if (Expr->evaluateAsAbsolute(AbsVal))
126
8
    OS << AbsVal;
127
32.5k
  else
128
32.5k
    Expr->print(OS, MAI, true);
129
32.5k
  OS << ')';
130
32.5k
}
131
132
bool
133
MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
134
                                      const MCAsmLayout *Layout,
135
43.9k
                                      const MCFixup *Fixup) const {
136
43.9k
  // Look for the %hi(%neg(%gp_rel(X))) and %lo(%neg(%gp_rel(X))) special cases.
137
43.9k
  if (isGpOff()) {
138
403
    const MCExpr *SubExpr =
139
403
        cast<MipsMCExpr>(cast<MipsMCExpr>(getSubExpr())->getSubExpr())
140
403
            ->getSubExpr();
141
403
    if (!SubExpr->evaluateAsRelocatable(Res, Layout, Fixup))
142
0
      return false;
143
403
144
403
    Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
145
403
                       MEK_Special);
146
403
    return true;
147
403
  }
148
43.5k
149
43.5k
  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
150
0
    return false;
151
43.5k
152
43.5k
  if (Res.getRefKind() != MCSymbolRefExpr::VK_None)
153
2.84k
    return false;
154
40.7k
155
40.7k
  // evaluateAsAbsolute() and evaluateAsValue() require that we evaluate the
156
40.7k
  // %hi/%lo/etc. here. Fixup is a null pointer when either of these is the
157
40.7k
  // caller.
158
40.7k
  if (Res.isAbsolute() && 
Fixup == nullptr55
) {
159
29
    int64_t AbsVal = Res.getConstant();
160
29
    switch (Kind) {
161
29
    case MEK_None:
162
0
    case MEK_Special:
163
0
      llvm_unreachable("MEK_None and MEK_Special are invalid");
164
0
    case MEK_DTPREL:
165
0
      // MEK_DTPREL is used for marking TLS DIEExpr only
166
0
      // and contains a regular sub-expression.
167
0
      return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
168
0
    case MEK_DTPREL_HI:
169
0
    case MEK_DTPREL_LO:
170
0
    case MEK_GOT:
171
0
    case MEK_GOTTPREL:
172
0
    case MEK_GOT_CALL:
173
0
    case MEK_GOT_DISP:
174
0
    case MEK_GOT_HI16:
175
0
    case MEK_GOT_LO16:
176
0
    case MEK_GOT_OFST:
177
0
    case MEK_GOT_PAGE:
178
0
    case MEK_GPREL:
179
0
    case MEK_PCREL_HI16:
180
0
    case MEK_PCREL_LO16:
181
0
    case MEK_TLSGD:
182
0
    case MEK_TLSLDM:
183
0
    case MEK_TPREL_HI:
184
0
    case MEK_TPREL_LO:
185
0
      return false;
186
22
    case MEK_LO:
187
22
    case MEK_CALL_LO16:
188
22
      AbsVal = SignExtend64<16>(AbsVal);
189
22
      break;
190
22
    case MEK_CALL_HI16:
191
3
    case MEK_HI:
192
3
      AbsVal = SignExtend64<16>((AbsVal + 0x8000) >> 16);
193
3
      break;
194
3
    case MEK_HIGHER:
195
2
      AbsVal = SignExtend64<16>((AbsVal + 0x80008000LL) >> 32);
196
2
      break;
197
3
    case MEK_HIGHEST:
198
2
      AbsVal = SignExtend64<16>((AbsVal + 0x800080008000LL) >> 48);
199
2
      break;
200
3
    case MEK_NEG:
201
0
      AbsVal = -AbsVal;
202
0
      break;
203
29
    }
204
29
    Res = MCValue::get(AbsVal);
205
29
    return true;
206
29
  }
207
40.7k
208
40.7k
  // We want to defer it for relocatable expressions since the constant is
209
40.7k
  // applied to the whole symbol value.
210
40.7k
  //
211
40.7k
  // The value of getKind() that is given to MCValue is only intended to aid
212
40.7k
  // debugging when inspecting MCValue objects. It shouldn't be relied upon
213
40.7k
  // for decision making.
214
40.7k
  Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
215
40.7k
216
40.7k
  return true;
217
40.7k
}
218
219
10.8k
void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
220
10.8k
  Streamer.visitUsedExpr(*getSubExpr());
221
10.8k
}
222
223
71
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
224
71
  switch (Expr->getKind()) {
225
71
  case MCExpr::Target:
226
0
    fixELFSymbolsInTLSFixupsImpl(cast<MipsMCExpr>(Expr)->getSubExpr(), Asm);
227
0
    break;
228
71
  case MCExpr::Constant:
229
2
    break;
230
71
  case MCExpr::Binary: {
231
2
    const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
232
2
    fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
233
2
    fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
234
2
    break;
235
71
  }
236
71
  case MCExpr::SymbolRef: {
237
67
    // We're known to be under a TLS fixup, so any symbol should be
238
67
    // modified. There should be only one.
239
67
    const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
240
67
    cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
241
67
    break;
242
71
  }
243
71
  case MCExpr::Unary:
244
0
    fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
245
0
    break;
246
71
  }
247
71
}
248
249
10.8k
void MipsMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
250
10.8k
  switch (getKind()) {
251
10.8k
  case MEK_None:
252
0
  case MEK_Special:
253
0
    llvm_unreachable("MEK_None and MEK_Special are invalid");
254
0
    break;
255
10.7k
  case MEK_CALL_HI16:
256
10.7k
  case MEK_CALL_LO16:
257
10.7k
  case MEK_GOT:
258
10.7k
  case MEK_GOT_CALL:
259
10.7k
  case MEK_GOT_DISP:
260
10.7k
  case MEK_GOT_HI16:
261
10.7k
  case MEK_GOT_LO16:
262
10.7k
  case MEK_GOT_OFST:
263
10.7k
  case MEK_GOT_PAGE:
264
10.7k
  case MEK_GPREL:
265
10.7k
  case MEK_HI:
266
10.7k
  case MEK_HIGHER:
267
10.7k
  case MEK_HIGHEST:
268
10.7k
  case MEK_LO:
269
10.7k
  case MEK_NEG:
270
10.7k
  case MEK_PCREL_HI16:
271
10.7k
  case MEK_PCREL_LO16:
272
10.7k
    // If we do have nested target-specific expressions, they will be in
273
10.7k
    // a consecutive chain.
274
10.7k
    if (const MipsMCExpr *E = dyn_cast<const MipsMCExpr>(getSubExpr()))
275
124
      E->fixELFSymbolsInTLSFixups(Asm);
276
10.7k
    break;
277
10.7k
  case MEK_DTPREL:
278
67
  case MEK_DTPREL_HI:
279
67
  case MEK_DTPREL_LO:
280
67
  case MEK_TLSLDM:
281
67
  case MEK_TLSGD:
282
67
  case MEK_GOTTPREL:
283
67
  case MEK_TPREL_HI:
284
67
  case MEK_TPREL_LO:
285
67
    fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
286
67
    break;
287
10.8k
  }
288
10.8k
}
289
290
45.0k
bool MipsMCExpr::isGpOff(MipsExprKind &Kind) const {
291
45.0k
  if (getKind() == MEK_HI || 
getKind() == MEK_LO43.7k
) {
292
4.13k
    if (const MipsMCExpr *S1 = dyn_cast<const MipsMCExpr>(getSubExpr())) {
293
481
      if (const MipsMCExpr *S2 = dyn_cast<const MipsMCExpr>(S1->getSubExpr())) {
294
481
        if (S1->getKind() == MEK_NEG && S2->getKind() == MEK_GPREL) {
295
481
          Kind = getKind();
296
481
          return true;
297
481
        }
298
44.5k
      }
299
481
    }
300
4.13k
  }
301
44.5k
  return false;
302
44.5k
}