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