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