Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/MCExpr.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
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 "llvm/MC/MCExpr.h"
10
#include "llvm/ADT/Statistic.h"
11
#include "llvm/ADT/StringExtras.h"
12
#include "llvm/ADT/StringSwitch.h"
13
#include "llvm/Config/llvm-config.h"
14
#include "llvm/MC/MCAsmBackend.h"
15
#include "llvm/MC/MCAsmInfo.h"
16
#include "llvm/MC/MCAsmLayout.h"
17
#include "llvm/MC/MCAssembler.h"
18
#include "llvm/MC/MCContext.h"
19
#include "llvm/MC/MCObjectWriter.h"
20
#include "llvm/MC/MCSymbol.h"
21
#include "llvm/MC/MCValue.h"
22
#include "llvm/Support/Casting.h"
23
#include "llvm/Support/Compiler.h"
24
#include "llvm/Support/Debug.h"
25
#include "llvm/Support/ErrorHandling.h"
26
#include "llvm/Support/raw_ostream.h"
27
#include <cassert>
28
#include <cstdint>
29
30
using namespace llvm;
31
32
#define DEBUG_TYPE "mcexpr"
33
34
namespace {
35
namespace stats {
36
37
STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
38
39
} // end namespace stats
40
} // end anonymous namespace
41
42
1.55M
void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
43
1.55M
  switch (getKind()) {
44
1.55M
  case MCExpr::Target:
45
59.6k
    return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
46
1.55M
  case MCExpr::Constant: {
47
644k
    auto Value = cast<MCConstantExpr>(*this).getValue();
48
644k
    auto PrintInHex = cast<MCConstantExpr>(*this).useHexFormat();
49
644k
    if (PrintInHex)
50
26.3k
      OS << "0x" << Twine::utohexstr(Value);
51
618k
    else
52
618k
      OS << Value;
53
644k
    return;
54
1.55M
  }
55
1.55M
  case MCExpr::SymbolRef: {
56
627k
    const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
57
627k
    const MCSymbol &Sym = SRE.getSymbol();
58
627k
    // Parenthesize names that start with $ so that they don't look like
59
627k
    // absolute names.
60
627k
    bool UseParens =
61
627k
        !InParens && 
!Sym.getName().empty()597k
&&
Sym.getName()[0] == '$'597k
;
62
627k
    if (UseParens) {
63
10.3k
      OS << '(';
64
10.3k
      Sym.print(OS, MAI);
65
10.3k
      OS << ')';
66
10.3k
    } else
67
617k
      Sym.print(OS, MAI);
68
627k
69
627k
    if (SRE.getKind() != MCSymbolRefExpr::VK_None)
70
21.8k
      SRE.printVariantKind(OS);
71
627k
72
627k
    return;
73
1.55M
  }
74
1.55M
75
1.55M
  case MCExpr::Unary: {
76
6
    const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
77
6
    switch (UE.getOpcode()) {
78
6
    
case MCUnaryExpr::LNot: OS << '!'; break0
;
79
6
    
case MCUnaryExpr::Minus: OS << '-'; break5
;
80
6
    
case MCUnaryExpr::Not: OS << '~'; break1
;
81
6
    
case MCUnaryExpr::Plus: OS << '+'; break0
;
82
6
    }
83
6
    bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
84
6
    if (Binary) 
OS << "("1
;
85
6
    UE.getSubExpr()->print(OS, MAI);
86
6
    if (Binary) 
OS << ")"1
;
87
6
    return;
88
6
  }
89
6
90
226k
  case MCExpr::Binary: {
91
226k
    const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
92
226k
93
226k
    // Only print parens around the LHS if it is non-trivial.
94
226k
    if (isa<MCConstantExpr>(BE.getLHS()) || 
isa<MCSymbolRefExpr>(BE.getLHS())226k
) {
95
224k
      BE.getLHS()->print(OS, MAI);
96
224k
    } else {
97
2.29k
      OS << '(';
98
2.29k
      BE.getLHS()->print(OS, MAI);
99
2.29k
      OS << ')';
100
2.29k
    }
101
226k
102
226k
    switch (BE.getOpcode()) {
103
226k
    case MCBinaryExpr::Add:
104
10.5k
      // Print "X-42" instead of "X+-42".
105
10.5k
      if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
106
10.0k
        if (RHSC->getValue() < 0) {
107
1.37k
          OS << RHSC->getValue();
108
1.37k
          return;
109
1.37k
        }
110
9.15k
      }
111
9.15k
112
9.15k
      OS <<  '+';
113
9.15k
      break;
114
9.15k
    
case MCBinaryExpr::AShr: OS << ">>"; break0
;
115
9.15k
    
case MCBinaryExpr::And: OS << '&'; break2
;
116
9.15k
    
case MCBinaryExpr::Div: OS << '/'; break1.22k
;
117
9.15k
    
case MCBinaryExpr::EQ: OS << "=="; break0
;
118
9.15k
    
case MCBinaryExpr::GT: OS << '>'; break1
;
119
9.15k
    
case MCBinaryExpr::GTE: OS << ">="; break0
;
120
9.15k
    
case MCBinaryExpr::LAnd: OS << "&&"; break0
;
121
9.15k
    
case MCBinaryExpr::LOr: OS << "||"; break0
;
122
9.15k
    
case MCBinaryExpr::LShr: OS << ">>"; break410
;
123
9.15k
    
case MCBinaryExpr::LT: OS << '<'; break0
;
124
9.15k
    
case MCBinaryExpr::LTE: OS << "<="; break0
;
125
9.15k
    
case MCBinaryExpr::Mod: OS << '%'; break0
;
126
9.15k
    
case MCBinaryExpr::Mul: OS << '*'; break5
;
127
9.15k
    
case MCBinaryExpr::NE: OS << "!="; break0
;
128
9.15k
    
case MCBinaryExpr::Or: OS << '|'; break0
;
129
9.15k
    
case MCBinaryExpr::Shl: OS << "<<"; break0
;
130
214k
    case MCBinaryExpr::Sub:  OS <<  '-'; break;
131
9.15k
    
case MCBinaryExpr::Xor: OS << '^'; break0
;
132
225k
    }
133
225k
134
225k
    // Only print parens around the LHS if it is non-trivial.
135
225k
    if (isa<MCConstantExpr>(BE.getRHS()) || 
isa<MCSymbolRefExpr>(BE.getRHS())214k
) {
136
221k
      BE.getRHS()->print(OS, MAI);
137
221k
    } else {
138
3.35k
      OS << '(';
139
3.35k
      BE.getRHS()->print(OS, MAI);
140
3.35k
      OS << ')';
141
3.35k
    }
142
225k
    return;
143
225k
  }
144
0
  }
145
0
146
0
  llvm_unreachable("Invalid expression kind!");
147
0
}
148
149
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
150
LLVM_DUMP_METHOD void MCExpr::dump() const {
151
  dbgs() << *this;
152
  dbgs() << '\n';
153
}
154
#endif
155
156
/* *** */
157
158
const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
159
                                         const MCExpr *RHS, MCContext &Ctx,
160
2.69M
                                         SMLoc Loc) {
161
2.69M
  return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
162
2.69M
}
163
164
const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
165
37.5k
                                       MCContext &Ctx, SMLoc Loc) {
166
37.5k
  return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
167
37.5k
}
168
169
const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx,
170
3.68M
                                             bool PrintInHex) {
171
3.68M
  return new (Ctx) MCConstantExpr(Value, PrintInHex);
172
3.68M
}
173
174
/* *** */
175
176
MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
177
                                 const MCAsmInfo *MAI, SMLoc Loc)
178
    : MCExpr(MCExpr::SymbolRef, Loc), Kind(Kind),
179
      UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
180
      HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
181
9.83M
      Symbol(Symbol) {
182
9.83M
  assert(Symbol);
183
9.83M
}
184
185
const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
186
                                               VariantKind Kind,
187
9.83M
                                               MCContext &Ctx, SMLoc Loc) {
188
9.83M
  return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
189
9.83M
}
190
191
const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
192
22
                                               MCContext &Ctx) {
193
22
  return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
194
22
}
195
196
21.8k
StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
197
21.8k
  switch (Kind) {
198
21.8k
  
case VK_Invalid: return "<<invalid>>"0
;
199
21.8k
  
case VK_None: return "<<none>>"0
;
200
21.8k
201
21.8k
  
case VK_DTPOFF: return "DTPOFF"37
;
202
21.8k
  
case VK_DTPREL: return "DTPREL"12
;
203
21.8k
  
case VK_GOT: return "GOT"563
;
204
21.8k
  
case VK_GOTOFF: return "GOTOFF"217
;
205
21.8k
  
case VK_GOTREL: return "GOTREL"0
;
206
21.8k
  
case VK_GOTPCREL: return "GOTPCREL"2.38k
;
207
21.8k
  
case VK_GOTTPOFF: return "GOTTPOFF"51
;
208
21.8k
  
case VK_INDNTPOFF: return "INDNTPOFF"17
;
209
21.8k
  
case VK_NTPOFF: return "NTPOFF"58
;
210
21.8k
  
case VK_GOTNTPOFF: return "GOTNTPOFF"7
;
211
21.8k
  
case VK_PLT: return "PLT"2.18k
;
212
21.8k
  
case VK_TLSGD: return "TLSGD"63
;
213
21.8k
  
case VK_TLSLD: return "TLSLD"10
;
214
21.8k
  
case VK_TLSLDM: return "TLSLDM"19
;
215
21.8k
  
case VK_TPOFF: return "TPOFF"77
;
216
21.8k
  
case VK_TPREL: return "TPREL"18
;
217
21.8k
  
case VK_TLSCALL: return "tlscall"2
;
218
21.8k
  
case VK_TLSDESC: return "tlsdesc"3
;
219
21.8k
  
case VK_TLVP: return "TLVP"48
;
220
21.8k
  
case VK_TLVPPAGE: return "TLVPPAGE"24
;
221
21.8k
  
case VK_TLVPPAGEOFF: return "TLVPPAGEOFF"25
;
222
21.8k
  
case VK_PAGE: return "PAGE"206
;
223
21.8k
  
case VK_PAGEOFF: return "PAGEOFF"346
;
224
21.8k
  
case VK_GOTPAGE: return "GOTPAGE"182
;
225
21.8k
  
case VK_GOTPAGEOFF: return "GOTPAGEOFF"182
;
226
21.8k
  
case VK_SECREL: return "SECREL32"82
;
227
21.8k
  
case VK_SIZE: return "SIZE"0
;
228
21.8k
  
case VK_WEAKREF: return "WEAKREF"0
;
229
21.8k
  
case VK_X86_ABS8: return "ABS8"6
;
230
21.8k
  
case VK_ARM_NONE: return "none"0
;
231
21.8k
  
case VK_ARM_GOT_PREL: return "GOT_PREL"70
;
232
21.8k
  
case VK_ARM_TARGET1: return "target1"8
;
233
21.8k
  
case VK_ARM_TARGET2: return "target2"4
;
234
21.8k
  
case VK_ARM_PREL31: return "prel31"2
;
235
21.8k
  
case VK_ARM_SBREL: return "sbrel"66
;
236
21.8k
  
case VK_ARM_TLSLDO: return "tlsldo"1
;
237
21.8k
  
case VK_ARM_TLSDESCSEQ: return "tlsdescseq"0
;
238
21.8k
  
case VK_AVR_NONE: return "none"0
;
239
21.8k
  
case VK_AVR_LO8: return "lo8"0
;
240
21.8k
  
case VK_AVR_HI8: return "hi8"0
;
241
21.8k
  
case VK_AVR_HLO8: return "hlo8"0
;
242
21.8k
  
case VK_AVR_DIFF8: return "diff8"0
;
243
21.8k
  
case VK_AVR_DIFF16: return "diff16"0
;
244
21.8k
  
case VK_AVR_DIFF32: return "diff32"0
;
245
21.8k
  
case VK_PPC_LO: return "l"1
;
246
21.8k
  
case VK_PPC_HI: return "h"0
;
247
21.8k
  
case VK_PPC_HA: return "ha"1
;
248
21.8k
  
case VK_PPC_HIGH: return "high"0
;
249
21.8k
  
case VK_PPC_HIGHA: return "higha"0
;
250
21.8k
  
case VK_PPC_HIGHER: return "higher"0
;
251
21.8k
  
case VK_PPC_HIGHERA: return "highera"0
;
252
21.8k
  
case VK_PPC_HIGHEST: return "highest"0
;
253
21.8k
  
case VK_PPC_HIGHESTA: return "highesta"0
;
254
21.8k
  
case VK_PPC_GOT_LO: return "got@l"24
;
255
21.8k
  
case VK_PPC_GOT_HI: return "got@h"8
;
256
21.8k
  
case VK_PPC_GOT_HA: return "got@ha"8
;
257
21.8k
  
case VK_PPC_TOCBASE: return "tocbase"4.88k
;
258
21.8k
  
case VK_PPC_TOC: return "toc"16
;
259
21.8k
  
case VK_PPC_TOC_LO: return "toc@l"3.56k
;
260
21.8k
  
case VK_PPC_TOC_HI: return "toc@h"4
;
261
21.8k
  
case VK_PPC_TOC_HA: return "toc@ha"3.44k
;
262
21.8k
  
case VK_PPC_DTPMOD: return "dtpmod"2
;
263
21.8k
  
case VK_PPC_TPREL_LO: return "tprel@l"18
;
264
21.8k
  
case VK_PPC_TPREL_HI: return "tprel@h"4
;
265
21.8k
  
case VK_PPC_TPREL_HA: return "tprel@ha"13
;
266
21.8k
  
case VK_PPC_TPREL_HIGH: return "tprel@high"4
;
267
21.8k
  
case VK_PPC_TPREL_HIGHA: return "tprel@higha"4
;
268
21.8k
  
case VK_PPC_TPREL_HIGHER: return "tprel@higher"4
;
269
21.8k
  
case VK_PPC_TPREL_HIGHERA: return "tprel@highera"4
;
270
21.8k
  
case VK_PPC_TPREL_HIGHEST: return "tprel@highest"4
;
271
21.8k
  
case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta"4
;
272
21.8k
  
case VK_PPC_DTPREL_LO: return "dtprel@l"19
;
273
21.8k
  
case VK_PPC_DTPREL_HI: return "dtprel@h"4
;
274
21.8k
  
case VK_PPC_DTPREL_HA: return "dtprel@ha"13
;
275
21.8k
  
case VK_PPC_DTPREL_HIGH: return "dtprel@high"4
;
276
21.8k
  
case VK_PPC_DTPREL_HIGHA: return "dtprel@higha"4
;
277
21.8k
  
case VK_PPC_DTPREL_HIGHER: return "dtprel@higher"4
;
278
21.8k
  
case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera"4
;
279
21.8k
  
case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest"4
;
280
21.8k
  
case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta"4
;
281
21.8k
  
case VK_PPC_GOT_TPREL: return "got@tprel"8
;
282
21.8k
  
case VK_PPC_GOT_TPREL_LO: return "got@tprel@l"25
;
283
21.8k
  
case VK_PPC_GOT_TPREL_HI: return "got@tprel@h"4
;
284
21.8k
  
case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha"19
;
285
21.8k
  
case VK_PPC_GOT_DTPREL: return "got@dtprel"8
;
286
21.8k
  
case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l"8
;
287
21.8k
  
case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h"4
;
288
21.8k
  
case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha"4
;
289
21.8k
  
case VK_PPC_TLS: return "tls"26
;
290
21.8k
  
case VK_PPC_GOT_TLSGD: return "got@tlsgd"11
;
291
21.8k
  
case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l"16
;
292
21.8k
  
case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h"4
;
293
21.8k
  
case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha"16
;
294
21.8k
  
case VK_PPC_TLSGD: return "tlsgd"23
;
295
21.8k
  
case VK_PPC_GOT_TLSLD: return "got@tlsld"7
;
296
21.8k
  
case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l"10
;
297
21.8k
  
case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h"4
;
298
21.8k
  
case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha"10
;
299
21.8k
  
case VK_PPC_TLSLD: return "tlsld"13
;
300
21.8k
  
case VK_PPC_LOCAL: return "local"1
;
301
21.8k
  
case VK_COFF_IMGREL32: return "IMGREL"731
;
302
21.8k
  
case VK_Hexagon_PCREL: return "PCREL"72
;
303
21.8k
  
case VK_Hexagon_LO16: return "LO16"0
;
304
21.8k
  
case VK_Hexagon_HI16: return "HI16"0
;
305
21.8k
  
case VK_Hexagon_GPREL: return "GPREL"0
;
306
21.8k
  
case VK_Hexagon_GD_GOT: return "GDGOT"24
;
307
21.8k
  
case VK_Hexagon_LD_GOT: return "LDGOT"0
;
308
21.8k
  
case VK_Hexagon_GD_PLT: return "GDPLT"22
;
309
21.8k
  
case VK_Hexagon_LD_PLT: return "LDPLT"0
;
310
21.8k
  
case VK_Hexagon_IE: return "IE"8
;
311
21.8k
  
case VK_Hexagon_IE_GOT: return "IEGOT"4
;
312
21.8k
  
case VK_WASM_TYPEINDEX: return "TYPEINDEX"1
;
313
21.8k
  
case VK_WASM_MBREL: return "MBREL"10
;
314
21.8k
  
case VK_WASM_TBREL: return "TBREL"2
;
315
21.8k
  
case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo"316
;
316
21.8k
  
case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi"316
;
317
21.8k
  
case VK_AMDGPU_REL32_LO: return "rel32@lo"408
;
318
21.8k
  
case VK_AMDGPU_REL32_HI: return "rel32@hi"385
;
319
21.8k
  
case VK_AMDGPU_REL64: return "rel64"0
;
320
21.8k
  
case VK_AMDGPU_ABS32_LO: return "abs32@lo"258
;
321
21.8k
  
case VK_AMDGPU_ABS32_HI: return "abs32@hi"0
;
322
0
  }
323
0
  llvm_unreachable("Invalid variant kind");
324
0
}
325
326
MCSymbolRefExpr::VariantKind
327
2.42k
MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
328
2.42k
  return StringSwitch<VariantKind>(Name.lower())
329
2.42k
    .Case("dtprel", VK_DTPREL)
330
2.42k
    .Case("dtpoff", VK_DTPOFF)
331
2.42k
    .Case("got", VK_GOT)
332
2.42k
    .Case("gotoff", VK_GOTOFF)
333
2.42k
    .Case("gotrel", VK_GOTREL)
334
2.42k
    .Case("gotpcrel", VK_GOTPCREL)
335
2.42k
    .Case("gottpoff", VK_GOTTPOFF)
336
2.42k
    .Case("indntpoff", VK_INDNTPOFF)
337
2.42k
    .Case("ntpoff", VK_NTPOFF)
338
2.42k
    .Case("gotntpoff", VK_GOTNTPOFF)
339
2.42k
    .Case("plt", VK_PLT)
340
2.42k
    .Case("tlscall", VK_TLSCALL)
341
2.42k
    .Case("tlsdesc", VK_TLSDESC)
342
2.42k
    .Case("tlsgd", VK_TLSGD)
343
2.42k
    .Case("tlsld", VK_TLSLD)
344
2.42k
    .Case("tlsldm", VK_TLSLDM)
345
2.42k
    .Case("tpoff", VK_TPOFF)
346
2.42k
    .Case("tprel", VK_TPREL)
347
2.42k
    .Case("tlvp", VK_TLVP)
348
2.42k
    .Case("tlvppage", VK_TLVPPAGE)
349
2.42k
    .Case("tlvppageoff", VK_TLVPPAGEOFF)
350
2.42k
    .Case("page", VK_PAGE)
351
2.42k
    .Case("pageoff", VK_PAGEOFF)
352
2.42k
    .Case("gotpage", VK_GOTPAGE)
353
2.42k
    .Case("gotpageoff", VK_GOTPAGEOFF)
354
2.42k
    .Case("imgrel", VK_COFF_IMGREL32)
355
2.42k
    .Case("secrel32", VK_SECREL)
356
2.42k
    .Case("size", VK_SIZE)
357
2.42k
    .Case("abs8", VK_X86_ABS8)
358
2.42k
    .Case("l", VK_PPC_LO)
359
2.42k
    .Case("h", VK_PPC_HI)
360
2.42k
    .Case("ha", VK_PPC_HA)
361
2.42k
    .Case("high", VK_PPC_HIGH)
362
2.42k
    .Case("higha", VK_PPC_HIGHA)
363
2.42k
    .Case("higher", VK_PPC_HIGHER)
364
2.42k
    .Case("highera", VK_PPC_HIGHERA)
365
2.42k
    .Case("highest", VK_PPC_HIGHEST)
366
2.42k
    .Case("highesta", VK_PPC_HIGHESTA)
367
2.42k
    .Case("got@l", VK_PPC_GOT_LO)
368
2.42k
    .Case("got@h", VK_PPC_GOT_HI)
369
2.42k
    .Case("got@ha", VK_PPC_GOT_HA)
370
2.42k
    .Case("local", VK_PPC_LOCAL)
371
2.42k
    .Case("tocbase", VK_PPC_TOCBASE)
372
2.42k
    .Case("toc", VK_PPC_TOC)
373
2.42k
    .Case("toc@l", VK_PPC_TOC_LO)
374
2.42k
    .Case("toc@h", VK_PPC_TOC_HI)
375
2.42k
    .Case("toc@ha", VK_PPC_TOC_HA)
376
2.42k
    .Case("tls", VK_PPC_TLS)
377
2.42k
    .Case("dtpmod", VK_PPC_DTPMOD)
378
2.42k
    .Case("tprel@l", VK_PPC_TPREL_LO)
379
2.42k
    .Case("tprel@h", VK_PPC_TPREL_HI)
380
2.42k
    .Case("tprel@ha", VK_PPC_TPREL_HA)
381
2.42k
    .Case("tprel@high", VK_PPC_TPREL_HIGH)
382
2.42k
    .Case("tprel@higha", VK_PPC_TPREL_HIGHA)
383
2.42k
    .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
384
2.42k
    .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
385
2.42k
    .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
386
2.42k
    .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
387
2.42k
    .Case("dtprel@l", VK_PPC_DTPREL_LO)
388
2.42k
    .Case("dtprel@h", VK_PPC_DTPREL_HI)
389
2.42k
    .Case("dtprel@ha", VK_PPC_DTPREL_HA)
390
2.42k
    .Case("dtprel@high", VK_PPC_DTPREL_HIGH)
391
2.42k
    .Case("dtprel@higha", VK_PPC_DTPREL_HIGHA)
392
2.42k
    .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
393
2.42k
    .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
394
2.42k
    .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
395
2.42k
    .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
396
2.42k
    .Case("got@tprel", VK_PPC_GOT_TPREL)
397
2.42k
    .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
398
2.42k
    .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
399
2.42k
    .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
400
2.42k
    .Case("got@dtprel", VK_PPC_GOT_DTPREL)
401
2.42k
    .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
402
2.42k
    .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
403
2.42k
    .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
404
2.42k
    .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
405
2.42k
    .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
406
2.42k
    .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
407
2.42k
    .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
408
2.42k
    .Case("got@tlsld", VK_PPC_GOT_TLSLD)
409
2.42k
    .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
410
2.42k
    .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
411
2.42k
    .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
412
2.42k
    .Case("gdgot", VK_Hexagon_GD_GOT)
413
2.42k
    .Case("gdplt", VK_Hexagon_GD_PLT)
414
2.42k
    .Case("iegot", VK_Hexagon_IE_GOT)
415
2.42k
    .Case("ie", VK_Hexagon_IE)
416
2.42k
    .Case("ldgot", VK_Hexagon_LD_GOT)
417
2.42k
    .Case("ldplt", VK_Hexagon_LD_PLT)
418
2.42k
    .Case("pcrel", VK_Hexagon_PCREL)
419
2.42k
    .Case("none", VK_ARM_NONE)
420
2.42k
    .Case("got_prel", VK_ARM_GOT_PREL)
421
2.42k
    .Case("target1", VK_ARM_TARGET1)
422
2.42k
    .Case("target2", VK_ARM_TARGET2)
423
2.42k
    .Case("prel31", VK_ARM_PREL31)
424
2.42k
    .Case("sbrel", VK_ARM_SBREL)
425
2.42k
    .Case("tlsldo", VK_ARM_TLSLDO)
426
2.42k
    .Case("lo8", VK_AVR_LO8)
427
2.42k
    .Case("hi8", VK_AVR_HI8)
428
2.42k
    .Case("hlo8", VK_AVR_HLO8)
429
2.42k
    .Case("typeindex", VK_WASM_TYPEINDEX)
430
2.42k
    .Case("tbrel", VK_WASM_TBREL)
431
2.42k
    .Case("mbrel", VK_WASM_MBREL)
432
2.42k
    .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO)
433
2.42k
    .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI)
434
2.42k
    .Case("rel32@lo", VK_AMDGPU_REL32_LO)
435
2.42k
    .Case("rel32@hi", VK_AMDGPU_REL32_HI)
436
2.42k
    .Case("rel64", VK_AMDGPU_REL64)
437
2.42k
    .Case("abs32@lo", VK_AMDGPU_ABS32_LO)
438
2.42k
    .Case("abs32@hi", VK_AMDGPU_ABS32_HI)
439
2.42k
    .Default(VK_Invalid);
440
2.42k
}
441
442
21.8k
void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
443
21.8k
  if (UseParensForSymbolVariant)
444
210
    OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
445
21.6k
  else
446
21.6k
    OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
447
21.8k
}
448
449
/* *** */
450
451
0
void MCTargetExpr::anchor() {}
452
453
/* *** */
454
455
1.40M
bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
456
1.40M
  return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
457
1.40M
}
458
459
bool MCExpr::evaluateAsAbsolute(int64_t &Res,
460
1.26M
                                const MCAsmLayout &Layout) const {
461
1.26M
  return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
462
1.26M
}
463
464
bool MCExpr::evaluateAsAbsolute(int64_t &Res,
465
                                const MCAsmLayout &Layout,
466
22.6k
                                const SectionAddrMap &Addrs) const {
467
22.6k
  return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
468
22.6k
}
469
470
152
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
471
152
  return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
472
152
}
473
474
3.00M
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
475
3.00M
  return evaluateAsAbsolute(Res, Asm, nullptr, nullptr);
476
3.00M
}
477
478
bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
479
3.30M
                                   const MCAsmLayout &Layout) const {
480
3.30M
  return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
481
3.30M
                            true);
482
3.30M
}
483
484
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
485
                                const MCAsmLayout *Layout,
486
5.70M
                                const SectionAddrMap *Addrs) const {
487
5.70M
  // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
488
5.70M
  // absolutize differences across sections and that is what the MachO writer
489
5.70M
  // uses Addrs for.
490
5.70M
  return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
491
5.70M
}
492
493
bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
494
                                const MCAsmLayout *Layout,
495
9.00M
                                const SectionAddrMap *Addrs, bool InSet) const {
496
9.00M
  MCValue Value;
497
9.00M
498
9.00M
  // Fast path constants.
499
9.00M
  if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
500
2.70M
    Res = CE->getValue();
501
2.70M
    return true;
502
2.70M
  }
503
6.30M
504
6.30M
  bool IsRelocatable =
505
6.30M
      evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
506
6.30M
507
6.30M
  // Record the current value.
508
6.30M
  Res = Value.getConstant();
509
6.30M
510
6.30M
  return IsRelocatable && 
Value.isAbsolute()5.95M
;
511
6.30M
}
512
513
/// Helper method for \see EvaluateSymbolAdd().
514
static void AttemptToFoldSymbolOffsetDifference(
515
    const MCAssembler *Asm, const MCAsmLayout *Layout,
516
    const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
517
20.0M
    const MCSymbolRefExpr *&B, int64_t &Addend) {
518
20.0M
  if (!A || 
!B10.0M
)
519
15.9M
    return;
520
4.01M
521
4.01M
  const MCSymbol &SA = A->getSymbol();
522
4.01M
  const MCSymbol &SB = B->getSymbol();
523
4.01M
524
4.01M
  if (SA.isUndefined() || 
SB.isUndefined()4.01M
)
525
4.87k
    return;
526
4.01M
527
4.01M
  if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
528
70.4k
    return;
529
3.94M
530
3.94M
  if (SA.getFragment() == SB.getFragment() && 
!SA.isVariable()2.92M
&&
531
3.94M
      
!SA.isUnset()2.92M
&&
!SB.isVariable()2.92M
&&
!SB.isUnset()2.92M
) {
532
2.92M
    Addend += (SA.getOffset() - SB.getOffset());
533
2.92M
534
2.92M
    // Pointers to Thumb symbols need to have their low-bit set to allow
535
2.92M
    // for interworking.
536
2.92M
    if (Asm->isThumbFunc(&SA))
537
0
      Addend |= 1;
538
2.92M
539
2.92M
    // If symbol is labeled as micromips, we set low-bit to ensure
540
2.92M
    // correct offset in .gcc_except_table
541
2.92M
    if (Asm->getBackend().isMicroMips(&SA))
542
34
      Addend |= 1;
543
2.92M
544
2.92M
    // Clear the symbol expr pointers to indicate we have folded these
545
2.92M
    // operands.
546
2.92M
    A = B = nullptr;
547
2.92M
    return;
548
2.92M
  }
549
1.02M
550
1.02M
  if (!Layout)
551
1.46k
    return;
552
1.02M
553
1.02M
  const MCSection &SecA = *SA.getFragment()->getParent();
554
1.02M
  const MCSection &SecB = *SB.getFragment()->getParent();
555
1.02M
556
1.02M
  if ((&SecA != &SecB) && 
!Addrs67.9k
)
557
45.3k
    return;
558
975k
559
975k
  // Eagerly evaluate.
560
975k
  Addend += Layout->getSymbolOffset(A->getSymbol()) -
561
975k
            Layout->getSymbolOffset(B->getSymbol());
562
975k
  if (Addrs && 
(&SecA != &SecB)22.6k
)
563
22.6k
    Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
564
975k
565
975k
  // Pointers to Thumb symbols need to have their low-bit set to allow
566
975k
  // for interworking.
567
975k
  if (Asm->isThumbFunc(&SA))
568
0
    Addend |= 1;
569
975k
570
975k
  // If symbol is labeled as micromips, we set low-bit to ensure
571
975k
  // correct offset in .gcc_except_table
572
975k
  if (Asm->getBackend().isMicroMips(&SA))
573
30
    Addend |= 1;
574
975k
575
975k
  // Clear the symbol expr pointers to indicate we have folded these
576
975k
  // operands.
577
975k
  A = B = nullptr;
578
975k
}
579
580
static bool canFold(const MCAssembler *Asm, const MCSymbolRefExpr *A,
581
5.00M
                    const MCSymbolRefExpr *B, bool InSet) {
582
5.00M
  if (InSet)
583
3.88M
    return true;
584
1.11M
585
1.11M
  if (!Asm->getBackend().requiresDiffExpressionRelocations())
586
1.11M
    return true;
587
207
588
207
  const MCSymbol &CheckSym = A ? 
A->getSymbol()206
:
B->getSymbol()1
;
589
207
  if (!CheckSym.isInSection())
590
4
    return true;
591
203
592
203
  if (!CheckSym.getSection().hasInstructions())
593
48
    return true;
594
155
595
155
  return false;
596
155
}
597
598
/// Evaluate the result of an add between (conceptually) two MCValues.
599
///
600
/// This routine conceptually attempts to construct an MCValue:
601
///   Result = (Result_A - Result_B + Result_Cst)
602
/// from two MCValue's LHS and RHS where
603
///   Result = LHS + RHS
604
/// and
605
///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
606
///
607
/// This routine attempts to aggresively fold the operands such that the result
608
/// is representable in an MCValue, but may not always succeed.
609
///
610
/// \returns True on success, false if the result is not representable in an
611
/// MCValue.
612
613
/// NOTE: It is really important to have both the Asm and Layout arguments.
614
/// They might look redundant, but this function can be used before layout
615
/// is done (see the object streamer for example) and having the Asm argument
616
/// lets us avoid relaxations early.
617
static bool
618
EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
619
                    const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
620
                    const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
621
7.53M
                    int64_t RHS_Cst, MCValue &Res) {
622
7.53M
  // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
623
7.53M
  // about dealing with modifiers. This will ultimately bite us, one day.
624
7.53M
  const MCSymbolRefExpr *LHS_A = LHS.getSymA();
625
7.53M
  const MCSymbolRefExpr *LHS_B = LHS.getSymB();
626
7.53M
  int64_t LHS_Cst = LHS.getConstant();
627
7.53M
628
7.53M
  // Fold the result constant immediately.
629
7.53M
  int64_t Result_Cst = LHS_Cst + RHS_Cst;
630
7.53M
631
7.53M
  assert((!Layout || Asm) &&
632
7.53M
         "Must have an assembler object if layout is given!");
633
7.53M
634
7.53M
  // If we have a layout, we can fold resolved differences. Do not do this if
635
7.53M
  // the backend requires this to be emitted as individual relocations, unless
636
7.53M
  // the InSet flag is set to get the current difference anyway (used for
637
7.53M
  // example to calculate symbol sizes).
638
7.53M
  if (Asm && 
canFold(Asm, LHS_A, LHS_B, InSet)5.00M
) {
639
5.00M
    // First, fold out any differences which are fully resolved. By
640
5.00M
    // reassociating terms in
641
5.00M
    //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
642
5.00M
    // we have the four possible differences:
643
5.00M
    //   (LHS_A - LHS_B),
644
5.00M
    //   (LHS_A - RHS_B),
645
5.00M
    //   (RHS_A - LHS_B),
646
5.00M
    //   (RHS_A - RHS_B).
647
5.00M
    // Since we are attempting to be as aggressive as possible about folding, we
648
5.00M
    // attempt to evaluate each possible alternative.
649
5.00M
    AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
650
5.00M
                                        Result_Cst);
651
5.00M
    AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
652
5.00M
                                        Result_Cst);
653
5.00M
    AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
654
5.00M
                                        Result_Cst);
655
5.00M
    AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
656
5.00M
                                        Result_Cst);
657
5.00M
  }
658
7.53M
659
7.53M
  // We can't represent the addition or subtraction of two symbols.
660
7.53M
  if ((LHS_A && 
RHS_A3.64M
) ||
(7.53M
LHS_B7.53M
&&
RHS_B291k
))
661
330
    return false;
662
7.53M
663
7.53M
  // At this point, we have at most one additive symbol and one subtractive
664
7.53M
  // symbol -- find them.
665
7.53M
  const MCSymbolRefExpr *A = LHS_A ? 
LHS_A3.64M
:
RHS_A3.89M
;
666
7.53M
  const MCSymbolRefExpr *B = LHS_B ? 
LHS_B291k
:
RHS_B7.24M
;
667
7.53M
668
7.53M
  Res = MCValue::get(A, B, Result_Cst);
669
7.53M
  return true;
670
7.53M
}
671
672
bool MCExpr::evaluateAsRelocatable(MCValue &Res,
673
                                   const MCAsmLayout *Layout,
674
7.18M
                                   const MCFixup *Fixup) const {
675
7.18M
  MCAssembler *Assembler = Layout ? 
&Layout->getAssembler()6.42M
:
nullptr758k
;
676
7.18M
  return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
677
7.18M
                                   false);
678
7.18M
}
679
680
24.6k
bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
681
24.6k
  MCAssembler *Assembler = &Layout.getAssembler();
682
24.6k
  return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
683
24.6k
                                   true);
684
24.6k
}
685
686
1.07M
static bool canExpand(const MCSymbol &Sym, bool InSet) {
687
1.07M
  const MCExpr *Expr = Sym.getVariableValue();
688
1.07M
  const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
689
1.07M
  if (Inner) {
690
452
    if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
691
29
      return false;
692
1.07M
  }
693
1.07M
694
1.07M
  if (InSet)
695
91
    return true;
696
1.07M
  return !Sym.isInSection();
697
1.07M
}
698
699
bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
700
                                       const MCAsmLayout *Layout,
701
                                       const MCFixup *Fixup,
702
                                       const SectionAddrMap *Addrs,
703
30.1M
                                       bool InSet) const {
704
30.1M
  ++stats::MCExprEvaluate;
705
30.1M
706
30.1M
  switch (getKind()) {
707
30.1M
  case Target:
708
400k
    return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
709
400k
                                                               Fixup);
710
30.1M
711
30.1M
  case Constant:
712
1.61M
    Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
713
1.61M
    return true;
714
30.1M
715
30.1M
  case SymbolRef: {
716
20.3M
    const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
717
20.3M
    const MCSymbol &Sym = SRE->getSymbol();
718
20.3M
719
20.3M
    // Evaluate recursively if this is a variable.
720
20.3M
    if (Sym.isVariable() && 
SRE->getKind() == MCSymbolRefExpr::VK_None1.07M
&&
721
20.3M
        
canExpand(Sym, InSet)1.07M
) {
722
1.07M
      bool IsMachO = SRE->hasSubsectionsViaSymbols();
723
1.07M
      if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
724
1.07M
              Res, Asm, Layout, Fixup, Addrs, 
InSet1.07M
||
IsMachO1.07M
)) {
725
1.07M
        if (!IsMachO)
726
553
          return true;
727
1.07M
728
1.07M
        const MCSymbolRefExpr *A = Res.getSymA();
729
1.07M
        const MCSymbolRefExpr *B = Res.getSymB();
730
1.07M
        // FIXME: This is small hack. Given
731
1.07M
        // a = b + 4
732
1.07M
        // .long a
733
1.07M
        // the OS X assembler will completely drop the 4. We should probably
734
1.07M
        // include it in the relocation or produce an error if that is not
735
1.07M
        // possible.
736
1.07M
        // Allow constant expressions.
737
1.07M
        if (!A && 
!B514k
)
738
514k
          return true;
739
559k
        // Allows aliases with zero offset.
740
559k
        if (Res.getConstant() == 0 && 
(553k
!A553k
||
!B553k
))
741
13
          return true;
742
19.8M
      }
743
1.07M
    }
744
19.8M
745
19.8M
    Res = MCValue::get(SRE, nullptr, 0);
746
19.8M
    return true;
747
19.8M
  }
748
19.8M
749
19.8M
  case Unary: {
750
37.5k
    const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
751
37.5k
    MCValue Value;
752
37.5k
753
37.5k
    if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
754
37.5k
                                                      Addrs, InSet))
755
2
      return false;
756
37.5k
757
37.5k
    switch (AUE->getOpcode()) {
758
37.5k
    case MCUnaryExpr::LNot:
759
20
      if (!Value.isAbsolute())
760
0
        return false;
761
20
      Res = MCValue::get(!Value.getConstant());
762
20
      break;
763
37.3k
    case MCUnaryExpr::Minus:
764
37.3k
      /// -(a - b + const) ==> (b - a - const)
765
37.3k
      if (Value.getSymA() && 
!Value.getSymB()52
)
766
52
        return false;
767
37.2k
768
37.2k
      // The cast avoids undefined behavior if the constant is INT64_MIN.
769
37.2k
      Res = MCValue::get(Value.getSymB(), Value.getSymA(),
770
37.2k
                         -(uint64_t)Value.getConstant());
771
37.2k
      break;
772
37.2k
    case MCUnaryExpr::Not:
773
164
      if (!Value.isAbsolute())
774
1
        return false;
775
163
      Res = MCValue::get(~Value.getConstant());
776
163
      break;
777
163
    case MCUnaryExpr::Plus:
778
44
      Res = Value;
779
44
      break;
780
37.4k
    }
781
37.4k
782
37.4k
    return true;
783
37.4k
  }
784
37.4k
785
7.76M
  case Binary: {
786
7.76M
    const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
787
7.76M
    MCValue LHSValue, RHSValue;
788
7.76M
789
7.76M
    if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
790
7.76M
                                                  Addrs, InSet) ||
791
7.76M
        !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
792
7.76M
                                                  Addrs, InSet)) {
793
89
      // Check if both are Target Expressions, see if we can compare them.
794
89
      if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS()))
795
2
        if (const MCTargetExpr *R = cast<MCTargetExpr>(ABE->getRHS())) {
796
2
          switch (ABE->getOpcode()) {
797
2
          case MCBinaryExpr::EQ:
798
2
            Res = MCValue::get((L->isEqualTo(R)) ? 
-11
:
01
);
799
2
            return true;
800
2
          case MCBinaryExpr::NE:
801
0
            Res = MCValue::get((R->isEqualTo(R)) ? 0 : -1);
802
0
            return true;
803
2
          
default: break0
;
804
87
          }
805
87
        }
806
87
      return false;
807
87
    }
808
7.76M
809
7.76M
    // We only support a few operations on non-constant expressions, handle
810
7.76M
    // those first.
811
7.76M
    if (!LHSValue.isAbsolute() || 
!RHSValue.isAbsolute()190k
) {
812
7.57M
      switch (ABE->getOpcode()) {
813
7.57M
      default:
814
40.2k
        return false;
815
7.57M
      case MCBinaryExpr::Sub:
816
6.53M
        // Negate RHS and add.
817
6.53M
        // The cast avoids undefined behavior if the constant is INT64_MIN.
818
6.53M
        return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
819
6.53M
                                   RHSValue.getSymB(), RHSValue.getSymA(),
820
6.53M
                                   -(uint64_t)RHSValue.getConstant(), Res);
821
7.57M
822
7.57M
      case MCBinaryExpr::Add:
823
1.00M
        return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
824
1.00M
                                   RHSValue.getSymA(), RHSValue.getSymB(),
825
1.00M
                                   RHSValue.getConstant(), Res);
826
190k
      }
827
190k
    }
828
190k
829
190k
    // FIXME: We need target hooks for the evaluation. It may be limited in
830
190k
    // width, and gas defines the result of comparisons differently from
831
190k
    // Apple as.
832
190k
    int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
833
190k
    int64_t Result = 0;
834
190k
    auto Op = ABE->getOpcode();
835
190k
    switch (Op) {
836
190k
    
case MCBinaryExpr::AShr: Result = LHS >> RHS; break1
;
837
190k
    
case MCBinaryExpr::Add: Result = LHS + RHS; break747
;
838
190k
    
case MCBinaryExpr::And: Result = LHS & RHS; break37
;
839
190k
    case MCBinaryExpr::Div:
840
4.55k
    case MCBinaryExpr::Mod:
841
4.55k
      // Handle division by zero. gas just emits a warning and keeps going,
842
4.55k
      // we try to be stricter.
843
4.55k
      // FIXME: Currently the caller of this function has no way to understand
844
4.55k
      // we're bailing out because of 'division by zero'. Therefore, it will
845
4.55k
      // emit a 'expected relocatable expression' error. It would be nice to
846
4.55k
      // change this code to emit a better diagnostic.
847
4.55k
      if (RHS == 0)
848
6
        return false;
849
4.54k
      if (ABE->getOpcode() == MCBinaryExpr::Div)
850
4.54k
        Result = LHS / RHS;
851
5
      else
852
5
        Result = LHS % RHS;
853
4.54k
      break;
854
4.54k
    
case MCBinaryExpr::EQ: Result = LHS == RHS; break27
;
855
4.54k
    
case MCBinaryExpr::GT: Result = LHS > RHS; break6
;
856
4.54k
    
case MCBinaryExpr::GTE: Result = LHS >= RHS; break1
;
857
4.54k
    
case MCBinaryExpr::LAnd: Result = 3
LHS3
&&
RHS2
; break;
858
4.54k
    
case MCBinaryExpr::LOr: Result = 4
LHS4
||
RHS2
; break;
859
35.6k
    case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
860
4.54k
    
case MCBinaryExpr::LT: Result = LHS < RHS; break4
;
861
4.54k
    
case MCBinaryExpr::LTE: Result = LHS <= RHS; break1
;
862
4.54k
    
case MCBinaryExpr::Mul: Result = LHS * RHS; break283
;
863
4.54k
    
case MCBinaryExpr::NE: Result = LHS != RHS; break42
;
864
4.54k
    
case MCBinaryExpr::Or: Result = LHS | RHS; break112
;
865
4.54k
    
case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break604
;
866
147k
    case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
867
4.54k
    
case MCBinaryExpr::Xor: Result = LHS ^ RHS; break2
;
868
190k
    }
869
190k
870
190k
    switch (Op) {
871
190k
    default:
872
189k
      Res = MCValue::get(Result);
873
189k
      break;
874
190k
    case MCBinaryExpr::EQ:
875
81
    case MCBinaryExpr::GT:
876
81
    case MCBinaryExpr::GTE:
877
81
    case MCBinaryExpr::LT:
878
81
    case MCBinaryExpr::LTE:
879
81
    case MCBinaryExpr::NE:
880
81
      // A comparison operator returns a -1 if true and 0 if false.
881
81
      Res = MCValue::get(Result ? 
-128
:
053
);
882
81
      break;
883
190k
    }
884
190k
885
190k
    return true;
886
190k
  }
887
0
  }
888
0
889
0
  llvm_unreachable("Invalid assembly expression kind!");
890
0
}
891
892
1.89M
MCFragment *MCExpr::findAssociatedFragment() const {
893
1.89M
  switch (getKind()) {
894
1.89M
  case Target:
895
92
    // We never look through target specific expressions.
896
92
    return cast<MCTargetExpr>(this)->findAssociatedFragment();
897
1.89M
898
1.89M
  case Constant:
899
143k
    return MCSymbol::AbsolutePseudoFragment;
900
1.89M
901
1.89M
  case SymbolRef: {
902
1.06M
    const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
903
1.06M
    const MCSymbol &Sym = SRE->getSymbol();
904
1.06M
    return Sym.getFragment();
905
1.89M
  }
906
1.89M
907
1.89M
  case Unary:
908
2
    return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
909
1.89M
910
1.89M
  case Binary: {
911
677k
    const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
912
677k
    MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
913
677k
    MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
914
677k
915
677k
    // If either is absolute, return the other.
916
677k
    if (LHS_F == MCSymbol::AbsolutePseudoFragment)
917
142k
      return RHS_F;
918
534k
    if (RHS_F == MCSymbol::AbsolutePseudoFragment)
919
126
      return LHS_F;
920
534k
921
534k
    // Not always correct, but probably the best we can do without more context.
922
534k
    if (BE->getOpcode() == MCBinaryExpr::Sub)
923
534k
      return MCSymbol::AbsolutePseudoFragment;
924
3
925
3
    // Otherwise, return the first non-null fragment.
926
3
    return LHS_F ? 
LHS_F0
: RHS_F;
927
3
  }
928
0
  }
929
0
930
0
  llvm_unreachable("Invalid assembly expression kind!");
931
0
}