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