This patch implements the general dynamic TLS model for 64-bit PowerPC.
[oota-llvm.git] / 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_PPC_TOC16_HA: return "toc@ha";
213   case VK_PPC_TOC16_LO: return "toc@l";
214   case VK_PPC_GOT_TPREL16_DS: return "got@tprel";
215   case VK_PPC_TLS: return "tls";
216   case VK_PPC_GOT_TLSGD16_HA: return "got@tlsgd@ha";
217   case VK_PPC_GOT_TLSGD16_LO: return "got@tlsgd@l";
218   case VK_PPC_TLSGD: return "tlsgd";
219   case VK_Mips_GPREL: return "GPREL";
220   case VK_Mips_GOT_CALL: return "GOT_CALL";
221   case VK_Mips_GOT16: return "GOT16";
222   case VK_Mips_GOT: return "GOT";
223   case VK_Mips_ABS_HI: return "ABS_HI";
224   case VK_Mips_ABS_LO: return "ABS_LO";
225   case VK_Mips_TLSGD: return "TLSGD";
226   case VK_Mips_TLSLDM: return "TLSLDM";
227   case VK_Mips_DTPREL_HI: return "DTPREL_HI";
228   case VK_Mips_DTPREL_LO: return "DTPREL_LO";
229   case VK_Mips_GOTTPREL: return "GOTTPREL";
230   case VK_Mips_TPREL_HI: return "TPREL_HI";
231   case VK_Mips_TPREL_LO: return "TPREL_LO";
232   case VK_Mips_GPOFF_HI: return "GPOFF_HI";
233   case VK_Mips_GPOFF_LO: return "GPOFF_LO";
234   case VK_Mips_GOT_DISP: return "GOT_DISP";
235   case VK_Mips_GOT_PAGE: return "GOT_PAGE";
236   case VK_Mips_GOT_OFST: return "GOT_OFST";
237   case VK_Mips_HIGHER:   return "HIGHER";
238   case VK_Mips_HIGHEST:  return "HIGHEST";
239   case VK_Mips_GOT_HI16: return "GOT_HI16";
240   case VK_Mips_GOT_LO16: return "GOT_LO16";
241   case VK_Mips_CALL_HI16: return "CALL_HI16";
242   case VK_Mips_CALL_LO16: return "CALL_LO16";
243   }
244   llvm_unreachable("Invalid variant kind");
245 }
246
247 MCSymbolRefExpr::VariantKind
248 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
249   return StringSwitch<VariantKind>(Name)
250     .Case("GOT", VK_GOT)
251     .Case("got", VK_GOT)
252     .Case("GOTOFF", VK_GOTOFF)
253     .Case("gotoff", VK_GOTOFF)
254     .Case("GOTPCREL", VK_GOTPCREL)
255     .Case("gotpcrel", VK_GOTPCREL)
256     .Case("GOTTPOFF", VK_GOTTPOFF)
257     .Case("gottpoff", VK_GOTTPOFF)
258     .Case("INDNTPOFF", VK_INDNTPOFF)
259     .Case("indntpoff", VK_INDNTPOFF)
260     .Case("NTPOFF", VK_NTPOFF)
261     .Case("ntpoff", VK_NTPOFF)
262     .Case("GOTNTPOFF", VK_GOTNTPOFF)
263     .Case("gotntpoff", VK_GOTNTPOFF)
264     .Case("PLT", VK_PLT)
265     .Case("plt", VK_PLT)
266     .Case("TLSGD", VK_TLSGD)
267     .Case("tlsgd", VK_TLSGD)
268     .Case("TLSLD", VK_TLSLD)
269     .Case("tlsld", VK_TLSLD)
270     .Case("TLSLDM", VK_TLSLDM)
271     .Case("tlsldm", VK_TLSLDM)
272     .Case("TPOFF", VK_TPOFF)
273     .Case("tpoff", VK_TPOFF)
274     .Case("DTPOFF", VK_DTPOFF)
275     .Case("dtpoff", VK_DTPOFF)
276     .Case("TLVP", VK_TLVP)
277     .Case("tlvp", VK_TLVP)
278     .Default(VK_Invalid);
279 }
280
281 /* *** */
282
283 void MCTargetExpr::anchor() {}
284
285 /* *** */
286
287 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
288   return EvaluateAsAbsolute(Res, 0, 0, 0);
289 }
290
291 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
292                                 const MCAsmLayout &Layout) const {
293   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
294 }
295
296 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
297                                 const MCAsmLayout &Layout,
298                                 const SectionAddrMap &Addrs) const {
299   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
300 }
301
302 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
303   return EvaluateAsAbsolute(Res, &Asm, 0, 0);
304 }
305
306 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
307                                 const MCAsmLayout *Layout,
308                                 const SectionAddrMap *Addrs) const {
309   MCValue Value;
310
311   // Fast path constants.
312   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
313     Res = CE->getValue();
314     return true;
315   }
316
317   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
318   // absolutize differences across sections and that is what the MachO writer
319   // uses Addrs for.
320   bool IsRelocatable =
321     EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
322
323   // Record the current value.
324   Res = Value.getConstant();
325
326   return IsRelocatable && Value.isAbsolute();
327 }
328
329 /// \brief Helper method for \see EvaluateSymbolAdd().
330 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
331                                                 const MCAsmLayout *Layout,
332                                                 const SectionAddrMap *Addrs,
333                                                 bool InSet,
334                                                 const MCSymbolRefExpr *&A,
335                                                 const MCSymbolRefExpr *&B,
336                                                 int64_t &Addend) {
337   if (!A || !B)
338     return;
339
340   const MCSymbol &SA = A->getSymbol();
341   const MCSymbol &SB = B->getSymbol();
342
343   if (SA.isUndefined() || SB.isUndefined())
344     return;
345
346   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
347     return;
348
349   MCSymbolData &AD = Asm->getSymbolData(SA);
350   MCSymbolData &BD = Asm->getSymbolData(SB);
351
352   if (AD.getFragment() == BD.getFragment()) {
353     Addend += (AD.getOffset() - BD.getOffset());
354
355     // Pointers to Thumb symbols need to have their low-bit set to allow
356     // for interworking.
357     if (Asm->isThumbFunc(&SA))
358       Addend |= 1;
359
360     // Clear the symbol expr pointers to indicate we have folded these
361     // operands.
362     A = B = 0;
363     return;
364   }
365
366   if (!Layout)
367     return;
368
369   const MCSectionData &SecA = *AD.getFragment()->getParent();
370   const MCSectionData &SecB = *BD.getFragment()->getParent();
371
372   if ((&SecA != &SecB) && !Addrs)
373     return;
374
375   // Eagerly evaluate.
376   Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
377              Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
378   if (Addrs && (&SecA != &SecB))
379     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
380
381   // Pointers to Thumb symbols need to have their low-bit set to allow
382   // for interworking.
383   if (Asm->isThumbFunc(&SA))
384     Addend |= 1;
385
386   // Clear the symbol expr pointers to indicate we have folded these
387   // operands.
388   A = B = 0;
389 }
390
391 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
392 ///
393 /// This routine conceptually attempts to construct an MCValue:
394 ///   Result = (Result_A - Result_B + Result_Cst)
395 /// from two MCValue's LHS and RHS where
396 ///   Result = LHS + RHS
397 /// and
398 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
399 ///
400 /// This routine attempts to aggresively fold the operands such that the result
401 /// is representable in an MCValue, but may not always succeed.
402 ///
403 /// \returns True on success, false if the result is not representable in an
404 /// MCValue.
405
406 /// NOTE: It is really important to have both the Asm and Layout arguments.
407 /// They might look redundant, but this function can be used before layout
408 /// is done (see the object streamer for example) and having the Asm argument
409 /// lets us avoid relaxations early.
410 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
411                                 const MCAsmLayout *Layout,
412                                 const SectionAddrMap *Addrs,
413                                 bool InSet,
414                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
415                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
416                                 MCValue &Res) {
417   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
418   // about dealing with modifiers. This will ultimately bite us, one day.
419   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
420   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
421   int64_t LHS_Cst = LHS.getConstant();
422
423   // Fold the result constant immediately.
424   int64_t Result_Cst = LHS_Cst + RHS_Cst;
425
426   assert((!Layout || Asm) &&
427          "Must have an assembler object if layout is given!");
428
429   // If we have a layout, we can fold resolved differences.
430   if (Asm) {
431     // First, fold out any differences which are fully resolved. By
432     // reassociating terms in
433     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
434     // we have the four possible differences:
435     //   (LHS_A - LHS_B),
436     //   (LHS_A - RHS_B),
437     //   (RHS_A - LHS_B),
438     //   (RHS_A - RHS_B).
439     // Since we are attempting to be as aggressive as possible about folding, we
440     // attempt to evaluate each possible alternative.
441     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
442                                         Result_Cst);
443     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
444                                         Result_Cst);
445     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
446                                         Result_Cst);
447     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
448                                         Result_Cst);
449   }
450
451   // We can't represent the addition or subtraction of two symbols.
452   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
453     return false;
454
455   // At this point, we have at most one additive symbol and one subtractive
456   // symbol -- find them.
457   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
458   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
459
460   // If we have a negated symbol, then we must have also have a non-negated
461   // symbol in order to encode the expression.
462   if (B && !A)
463     return false;
464
465   Res = MCValue::get(A, B, Result_Cst);
466   return true;
467 }
468
469 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
470                                    const MCAsmLayout &Layout) const {
471   return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout,
472                                    0, false);
473 }
474
475 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
476                                        const MCAssembler *Asm,
477                                        const MCAsmLayout *Layout,
478                                        const SectionAddrMap *Addrs,
479                                        bool InSet) const {
480   ++stats::MCExprEvaluate;
481
482   switch (getKind()) {
483   case Target:
484     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
485
486   case Constant:
487     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
488     return true;
489
490   case SymbolRef: {
491     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
492     const MCSymbol &Sym = SRE->getSymbol();
493
494     // Evaluate recursively if this is a variable.
495     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
496       bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm,
497                                                                    Layout,
498                                                                    Addrs,
499                                                                    true);
500       // If we failed to simplify this to a constant, let the target
501       // handle it.
502       if (Ret && !Res.getSymA() && !Res.getSymB())
503         return true;
504     }
505
506     Res = MCValue::get(SRE, 0, 0);
507     return true;
508   }
509
510   case Unary: {
511     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
512     MCValue Value;
513
514     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
515                                                       Addrs, InSet))
516       return false;
517
518     switch (AUE->getOpcode()) {
519     case MCUnaryExpr::LNot:
520       if (!Value.isAbsolute())
521         return false;
522       Res = MCValue::get(!Value.getConstant());
523       break;
524     case MCUnaryExpr::Minus:
525       /// -(a - b + const) ==> (b - a - const)
526       if (Value.getSymA() && !Value.getSymB())
527         return false;
528       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
529                          -Value.getConstant());
530       break;
531     case MCUnaryExpr::Not:
532       if (!Value.isAbsolute())
533         return false;
534       Res = MCValue::get(~Value.getConstant());
535       break;
536     case MCUnaryExpr::Plus:
537       Res = Value;
538       break;
539     }
540
541     return true;
542   }
543
544   case Binary: {
545     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
546     MCValue LHSValue, RHSValue;
547
548     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
549                                                   Addrs, InSet) ||
550         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
551                                                   Addrs, InSet))
552       return false;
553
554     // We only support a few operations on non-constant expressions, handle
555     // those first.
556     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
557       switch (ABE->getOpcode()) {
558       default:
559         return false;
560       case MCBinaryExpr::Sub:
561         // Negate RHS and add.
562         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
563                                    RHSValue.getSymB(), RHSValue.getSymA(),
564                                    -RHSValue.getConstant(),
565                                    Res);
566
567       case MCBinaryExpr::Add:
568         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
569                                    RHSValue.getSymA(), RHSValue.getSymB(),
570                                    RHSValue.getConstant(),
571                                    Res);
572       }
573     }
574
575     // FIXME: We need target hooks for the evaluation. It may be limited in
576     // width, and gas defines the result of comparisons and right shifts
577     // differently from Apple as.
578     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
579     int64_t Result = 0;
580     switch (ABE->getOpcode()) {
581     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
582     case MCBinaryExpr::And:  Result = LHS & RHS; break;
583     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
584     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
585     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
586     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
587     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
588     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
589     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
590     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
591     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
592     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
593     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
594     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
595     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
596     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
597     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
598     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
599     }
600
601     Res = MCValue::get(Result);
602     return true;
603   }
604   }
605
606   llvm_unreachable("Invalid assembly expression kind!");
607 }
608
609 const MCSection *MCExpr::FindAssociatedSection() const {
610   switch (getKind()) {
611   case Target:
612     // We never look through target specific expressions.
613     return cast<MCTargetExpr>(this)->FindAssociatedSection();
614
615   case Constant:
616     return MCSymbol::AbsolutePseudoSection;
617
618   case SymbolRef: {
619     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
620     const MCSymbol &Sym = SRE->getSymbol();
621
622     if (Sym.isDefined())
623       return &Sym.getSection();
624
625     return 0;
626   }
627
628   case Unary:
629     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
630
631   case Binary: {
632     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
633     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
634     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
635
636     // If either section is absolute, return the other.
637     if (LHS_S == MCSymbol::AbsolutePseudoSection)
638       return RHS_S;
639     if (RHS_S == MCSymbol::AbsolutePseudoSection)
640       return LHS_S;
641
642     // Otherwise, return the first non-null section.
643     return LHS_S ? LHS_S : RHS_S;
644   }
645   }
646
647   llvm_unreachable("Invalid assembly expression kind!");
648 }