]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/MC/MCExpr.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / lib / MC / MCExpr.cpp
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #define DEBUG_TYPE "mcexpr"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 namespace {
26 namespace stats {
27 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
28 }
29 }
30
31 void MCExpr::print(raw_ostream &OS) const {
32   switch (getKind()) {
33   case MCExpr::Target:
34     return cast<MCTargetExpr>(this)->PrintImpl(OS);
35   case MCExpr::Constant:
36     OS << cast<MCConstantExpr>(*this).getValue();
37     return;
38
39   case MCExpr::SymbolRef: {
40     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
41     const MCSymbol &Sym = SRE.getSymbol();
42     // Parenthesize names that start with $ so that they don't look like
43     // absolute names.
44     bool UseParens = Sym.getName()[0] == '$';
45
46     if (SRE.getKind() == MCSymbolRefExpr::VK_PPC_DARWIN_HA16 ||
47         SRE.getKind() == MCSymbolRefExpr::VK_PPC_DARWIN_LO16) {
48       OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
49       UseParens = true;
50     }
51
52     if (UseParens)
53       OS << '(' << Sym << ')';
54     else
55       OS << Sym;
56
57     if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT ||
58         SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD ||
59         SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT ||
60         SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF ||
61         SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF ||
62         SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF ||
63         SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET1 ||
64         SRE.getKind() == MCSymbolRefExpr::VK_ARM_TARGET2)
65       OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
66     else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
67              SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_HA16 &&
68              SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_LO16)
69       OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
70
71     return;
72   }
73
74   case MCExpr::Unary: {
75     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
76     switch (UE.getOpcode()) {
77     case MCUnaryExpr::LNot:  OS << '!'; break;
78     case MCUnaryExpr::Minus: OS << '-'; break;
79     case MCUnaryExpr::Not:   OS << '~'; break;
80     case MCUnaryExpr::Plus:  OS << '+'; break;
81     }
82     OS << *UE.getSubExpr();
83     return;
84   }
85
86   case MCExpr::Binary: {
87     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
88
89     // Only print parens around the LHS if it is non-trivial.
90     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
91       OS << *BE.getLHS();
92     } else {
93       OS << '(' << *BE.getLHS() << ')';
94     }
95
96     switch (BE.getOpcode()) {
97     case MCBinaryExpr::Add:
98       // Print "X-42" instead of "X+-42".
99       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
100         if (RHSC->getValue() < 0) {
101           OS << RHSC->getValue();
102           return;
103         }
104       }
105
106       OS <<  '+';
107       break;
108     case MCBinaryExpr::And:  OS <<  '&'; break;
109     case MCBinaryExpr::Div:  OS <<  '/'; break;
110     case MCBinaryExpr::EQ:   OS << "=="; break;
111     case MCBinaryExpr::GT:   OS <<  '>'; break;
112     case MCBinaryExpr::GTE:  OS << ">="; break;
113     case MCBinaryExpr::LAnd: OS << "&&"; break;
114     case MCBinaryExpr::LOr:  OS << "||"; break;
115     case MCBinaryExpr::LT:   OS <<  '<'; break;
116     case MCBinaryExpr::LTE:  OS << "<="; break;
117     case MCBinaryExpr::Mod:  OS <<  '%'; break;
118     case MCBinaryExpr::Mul:  OS <<  '*'; break;
119     case MCBinaryExpr::NE:   OS << "!="; break;
120     case MCBinaryExpr::Or:   OS <<  '|'; break;
121     case MCBinaryExpr::Shl:  OS << "<<"; break;
122     case MCBinaryExpr::Shr:  OS << ">>"; break;
123     case MCBinaryExpr::Sub:  OS <<  '-'; break;
124     case MCBinaryExpr::Xor:  OS <<  '^'; break;
125     }
126
127     // Only print parens around the LHS if it is non-trivial.
128     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
129       OS << *BE.getRHS();
130     } else {
131       OS << '(' << *BE.getRHS() << ')';
132     }
133     return;
134   }
135   }
136
137   llvm_unreachable("Invalid expression kind!");
138 }
139
140 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
141 void MCExpr::dump() const {
142   print(dbgs());
143   dbgs() << '\n';
144 }
145 #endif
146
147 /* *** */
148
149 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
150                                          const MCExpr *RHS, MCContext &Ctx) {
151   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
152 }
153
154 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
155                                        MCContext &Ctx) {
156   return new (Ctx) MCUnaryExpr(Opc, Expr);
157 }
158
159 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
160   return new (Ctx) MCConstantExpr(Value);
161 }
162
163 /* *** */
164
165 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
166                                                VariantKind Kind,
167                                                MCContext &Ctx) {
168   return new (Ctx) MCSymbolRefExpr(Sym, Kind);
169 }
170
171 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
172                                                MCContext &Ctx) {
173   return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
174 }
175
176 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
177   switch (Kind) {
178   case VK_Invalid: return "<<invalid>>";
179   case VK_None: return "<<none>>";
180
181   case VK_GOT: return "GOT";
182   case VK_GOTOFF: return "GOTOFF";
183   case VK_GOTPCREL: return "GOTPCREL";
184   case VK_GOTTPOFF: return "GOTTPOFF";
185   case VK_INDNTPOFF: return "INDNTPOFF";
186   case VK_NTPOFF: return "NTPOFF";
187   case VK_GOTNTPOFF: return "GOTNTPOFF";
188   case VK_PLT: return "PLT";
189   case VK_TLSGD: return "TLSGD";
190   case VK_TLSLD: return "TLSLD";
191   case VK_TLSLDM: return "TLSLDM";
192   case VK_TPOFF: return "TPOFF";
193   case VK_DTPOFF: return "DTPOFF";
194   case VK_TLVP: return "TLVP";
195   case VK_SECREL: return "SECREL";
196   case VK_ARM_PLT: return "(PLT)";
197   case VK_ARM_GOT: return "(GOT)";
198   case VK_ARM_GOTOFF: return "(GOTOFF)";
199   case VK_ARM_TPOFF: return "(tpoff)";
200   case VK_ARM_GOTTPOFF: return "(gottpoff)";
201   case VK_ARM_TLSGD: return "(tlsgd)";
202   case VK_ARM_TARGET1: return "(target1)";
203   case VK_ARM_TARGET2: return "(target2)";
204   case VK_PPC_TOC: return "tocbase";
205   case VK_PPC_TOC_ENTRY: return "toc";
206   case VK_PPC_DARWIN_HA16: return "ha16";
207   case VK_PPC_DARWIN_LO16: return "lo16";
208   case VK_PPC_GAS_HA16: return "ha";
209   case VK_PPC_GAS_LO16: return "l";
210   case VK_PPC_TPREL16_HA: return "tprel@ha";
211   case VK_PPC_TPREL16_LO: return "tprel@l";
212   case VK_Mips_GPREL: return "GPREL";
213   case VK_Mips_GOT_CALL: return "GOT_CALL";
214   case VK_Mips_GOT16: return "GOT16";
215   case VK_Mips_GOT: return "GOT";
216   case VK_Mips_ABS_HI: return "ABS_HI";
217   case VK_Mips_ABS_LO: return "ABS_LO";
218   case VK_Mips_TLSGD: return "TLSGD";
219   case VK_Mips_TLSLDM: return "TLSLDM";
220   case VK_Mips_DTPREL_HI: return "DTPREL_HI";
221   case VK_Mips_DTPREL_LO: return "DTPREL_LO";
222   case VK_Mips_GOTTPREL: return "GOTTPREL";
223   case VK_Mips_TPREL_HI: return "TPREL_HI";
224   case VK_Mips_TPREL_LO: return "TPREL_LO";
225   case VK_Mips_GPOFF_HI: return "GPOFF_HI";
226   case VK_Mips_GPOFF_LO: return "GPOFF_LO";
227   case VK_Mips_GOT_DISP: return "GOT_DISP";
228   case VK_Mips_GOT_PAGE: return "GOT_PAGE";
229   case VK_Mips_GOT_OFST: return "GOT_OFST";
230   case VK_Mips_HIGHER:   return "HIGHER";
231   case VK_Mips_HIGHEST:  return "HIGHEST";
232   case VK_Mips_GOT_HI16: return "GOT_HI16";
233   case VK_Mips_GOT_LO16: return "GOT_LO16";
234   case VK_Mips_CALL_HI16: return "CALL_HI16";
235   case VK_Mips_CALL_LO16: return "CALL_LO16";
236   }
237   llvm_unreachable("Invalid variant kind");
238 }
239
240 MCSymbolRefExpr::VariantKind
241 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
242   return StringSwitch<VariantKind>(Name)
243     .Case("GOT", VK_GOT)
244     .Case("got", VK_GOT)
245     .Case("GOTOFF", VK_GOTOFF)
246     .Case("gotoff", VK_GOTOFF)
247     .Case("GOTPCREL", VK_GOTPCREL)
248     .Case("gotpcrel", VK_GOTPCREL)
249     .Case("GOTTPOFF", VK_GOTTPOFF)
250     .Case("gottpoff", VK_GOTTPOFF)
251     .Case("INDNTPOFF", VK_INDNTPOFF)
252     .Case("indntpoff", VK_INDNTPOFF)
253     .Case("NTPOFF", VK_NTPOFF)
254     .Case("ntpoff", VK_NTPOFF)
255     .Case("GOTNTPOFF", VK_GOTNTPOFF)
256     .Case("gotntpoff", VK_GOTNTPOFF)
257     .Case("PLT", VK_PLT)
258     .Case("plt", VK_PLT)
259     .Case("TLSGD", VK_TLSGD)
260     .Case("tlsgd", VK_TLSGD)
261     .Case("TLSLD", VK_TLSLD)
262     .Case("tlsld", VK_TLSLD)
263     .Case("TLSLDM", VK_TLSLDM)
264     .Case("tlsldm", VK_TLSLDM)
265     .Case("TPOFF", VK_TPOFF)
266     .Case("tpoff", VK_TPOFF)
267     .Case("DTPOFF", VK_DTPOFF)
268     .Case("dtpoff", VK_DTPOFF)
269     .Case("TLVP", VK_TLVP)
270     .Case("tlvp", VK_TLVP)
271     .Default(VK_Invalid);
272 }
273
274 /* *** */
275
276 void MCTargetExpr::anchor() {}
277
278 /* *** */
279
280 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
281   return EvaluateAsAbsolute(Res, 0, 0, 0);
282 }
283
284 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
285                                 const MCAsmLayout &Layout) const {
286   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
287 }
288
289 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
290                                 const MCAsmLayout &Layout,
291                                 const SectionAddrMap &Addrs) const {
292   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
293 }
294
295 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
296   return EvaluateAsAbsolute(Res, &Asm, 0, 0);
297 }
298
299 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
300                                 const MCAsmLayout *Layout,
301                                 const SectionAddrMap *Addrs) const {
302   MCValue Value;
303
304   // Fast path constants.
305   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
306     Res = CE->getValue();
307     return true;
308   }
309
310   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
311   // absolutize differences across sections and that is what the MachO writer
312   // uses Addrs for.
313   bool IsRelocatable =
314     EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
315
316   // Record the current value.
317   Res = Value.getConstant();
318
319   return IsRelocatable && Value.isAbsolute();
320 }
321
322 /// \brief Helper method for \see EvaluateSymbolAdd().
323 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
324                                                 const MCAsmLayout *Layout,
325                                                 const SectionAddrMap *Addrs,
326                                                 bool InSet,
327                                                 const MCSymbolRefExpr *&A,
328                                                 const MCSymbolRefExpr *&B,
329                                                 int64_t &Addend) {
330   if (!A || !B)
331     return;
332
333   const MCSymbol &SA = A->getSymbol();
334   const MCSymbol &SB = B->getSymbol();
335
336   if (SA.isUndefined() || SB.isUndefined())
337     return;
338
339   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
340     return;
341
342   MCSymbolData &AD = Asm->getSymbolData(SA);
343   MCSymbolData &BD = Asm->getSymbolData(SB);
344
345   if (AD.getFragment() == BD.getFragment()) {
346     Addend += (AD.getOffset() - BD.getOffset());
347
348     // Pointers to Thumb symbols need to have their low-bit set to allow
349     // for interworking.
350     if (Asm->isThumbFunc(&SA))
351       Addend |= 1;
352
353     // Clear the symbol expr pointers to indicate we have folded these
354     // operands.
355     A = B = 0;
356     return;
357   }
358
359   if (!Layout)
360     return;
361
362   const MCSectionData &SecA = *AD.getFragment()->getParent();
363   const MCSectionData &SecB = *BD.getFragment()->getParent();
364
365   if ((&SecA != &SecB) && !Addrs)
366     return;
367
368   // Eagerly evaluate.
369   Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
370              Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
371   if (Addrs && (&SecA != &SecB))
372     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
373
374   // Pointers to Thumb symbols need to have their low-bit set to allow
375   // for interworking.
376   if (Asm->isThumbFunc(&SA))
377     Addend |= 1;
378
379   // Clear the symbol expr pointers to indicate we have folded these
380   // operands.
381   A = B = 0;
382 }
383
384 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
385 ///
386 /// This routine conceptually attempts to construct an MCValue:
387 ///   Result = (Result_A - Result_B + Result_Cst)
388 /// from two MCValue's LHS and RHS where
389 ///   Result = LHS + RHS
390 /// and
391 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
392 ///
393 /// This routine attempts to aggresively fold the operands such that the result
394 /// is representable in an MCValue, but may not always succeed.
395 ///
396 /// \returns True on success, false if the result is not representable in an
397 /// MCValue.
398
399 /// NOTE: It is really important to have both the Asm and Layout arguments.
400 /// They might look redundant, but this function can be used before layout
401 /// is done (see the object streamer for example) and having the Asm argument
402 /// lets us avoid relaxations early.
403 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
404                                 const MCAsmLayout *Layout,
405                                 const SectionAddrMap *Addrs,
406                                 bool InSet,
407                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
408                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
409                                 MCValue &Res) {
410   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
411   // about dealing with modifiers. This will ultimately bite us, one day.
412   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
413   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
414   int64_t LHS_Cst = LHS.getConstant();
415
416   // Fold the result constant immediately.
417   int64_t Result_Cst = LHS_Cst + RHS_Cst;
418
419   assert((!Layout || Asm) &&
420          "Must have an assembler object if layout is given!");
421
422   // If we have a layout, we can fold resolved differences.
423   if (Asm) {
424     // First, fold out any differences which are fully resolved. By
425     // reassociating terms in
426     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
427     // we have the four possible differences:
428     //   (LHS_A - LHS_B),
429     //   (LHS_A - RHS_B),
430     //   (RHS_A - LHS_B),
431     //   (RHS_A - RHS_B).
432     // Since we are attempting to be as aggressive as possible about folding, we
433     // attempt to evaluate each possible alternative.
434     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
435                                         Result_Cst);
436     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
437                                         Result_Cst);
438     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
439                                         Result_Cst);
440     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
441                                         Result_Cst);
442   }
443
444   // We can't represent the addition or subtraction of two symbols.
445   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
446     return false;
447
448   // At this point, we have at most one additive symbol and one subtractive
449   // symbol -- find them.
450   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
451   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
452
453   // If we have a negated symbol, then we must have also have a non-negated
454   // symbol in order to encode the expression.
455   if (B && !A)
456     return false;
457
458   Res = MCValue::get(A, B, Result_Cst);
459   return true;
460 }
461
462 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
463                                    const MCAsmLayout &Layout) const {
464   return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout,
465                                    0, false);
466 }
467
468 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
469                                        const MCAssembler *Asm,
470                                        const MCAsmLayout *Layout,
471                                        const SectionAddrMap *Addrs,
472                                        bool InSet) const {
473   ++stats::MCExprEvaluate;
474
475   switch (getKind()) {
476   case Target:
477     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
478
479   case Constant:
480     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
481     return true;
482
483   case SymbolRef: {
484     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
485     const MCSymbol &Sym = SRE->getSymbol();
486
487     // Evaluate recursively if this is a variable.
488     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
489       bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm,
490                                                                    Layout,
491                                                                    Addrs,
492                                                                    true);
493       // If we failed to simplify this to a constant, let the target
494       // handle it.
495       if (Ret && !Res.getSymA() && !Res.getSymB())
496         return true;
497     }
498
499     Res = MCValue::get(SRE, 0, 0);
500     return true;
501   }
502
503   case Unary: {
504     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
505     MCValue Value;
506
507     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
508                                                       Addrs, InSet))
509       return false;
510
511     switch (AUE->getOpcode()) {
512     case MCUnaryExpr::LNot:
513       if (!Value.isAbsolute())
514         return false;
515       Res = MCValue::get(!Value.getConstant());
516       break;
517     case MCUnaryExpr::Minus:
518       /// -(a - b + const) ==> (b - a - const)
519       if (Value.getSymA() && !Value.getSymB())
520         return false;
521       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
522                          -Value.getConstant());
523       break;
524     case MCUnaryExpr::Not:
525       if (!Value.isAbsolute())
526         return false;
527       Res = MCValue::get(~Value.getConstant());
528       break;
529     case MCUnaryExpr::Plus:
530       Res = Value;
531       break;
532     }
533
534     return true;
535   }
536
537   case Binary: {
538     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
539     MCValue LHSValue, RHSValue;
540
541     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
542                                                   Addrs, InSet) ||
543         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
544                                                   Addrs, InSet))
545       return false;
546
547     // We only support a few operations on non-constant expressions, handle
548     // those first.
549     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
550       switch (ABE->getOpcode()) {
551       default:
552         return false;
553       case MCBinaryExpr::Sub:
554         // Negate RHS and add.
555         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
556                                    RHSValue.getSymB(), RHSValue.getSymA(),
557                                    -RHSValue.getConstant(),
558                                    Res);
559
560       case MCBinaryExpr::Add:
561         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
562                                    RHSValue.getSymA(), RHSValue.getSymB(),
563                                    RHSValue.getConstant(),
564                                    Res);
565       }
566     }
567
568     // FIXME: We need target hooks for the evaluation. It may be limited in
569     // width, and gas defines the result of comparisons and right shifts
570     // differently from Apple as.
571     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
572     int64_t Result = 0;
573     switch (ABE->getOpcode()) {
574     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
575     case MCBinaryExpr::And:  Result = LHS & RHS; break;
576     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
577     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
578     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
579     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
580     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
581     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
582     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
583     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
584     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
585     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
586     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
587     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
588     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
589     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
590     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
591     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
592     }
593
594     Res = MCValue::get(Result);
595     return true;
596   }
597   }
598
599   llvm_unreachable("Invalid assembly expression kind!");
600 }
601
602 const MCSection *MCExpr::FindAssociatedSection() const {
603   switch (getKind()) {
604   case Target:
605     // We never look through target specific expressions.
606     return cast<MCTargetExpr>(this)->FindAssociatedSection();
607
608   case Constant:
609     return MCSymbol::AbsolutePseudoSection;
610
611   case SymbolRef: {
612     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
613     const MCSymbol &Sym = SRE->getSymbol();
614
615     if (Sym.isDefined())
616       return &Sym.getSection();
617
618     return 0;
619   }
620
621   case Unary:
622     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
623
624   case Binary: {
625     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
626     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
627     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
628
629     // If either section is absolute, return the other.
630     if (LHS_S == MCSymbol::AbsolutePseudoSection)
631       return RHS_S;
632     if (RHS_S == MCSymbol::AbsolutePseudoSection)
633       return LHS_S;
634
635     // Otherwise, return the first non-null section.
636     return LHS_S ? LHS_S : RHS_S;
637   }
638   }
639
640   llvm_unreachable("Invalid assembly expression kind!");
641 }