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