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