This is the first of several patches for Mips direct object generation.
[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/raw_ostream.h"
21 using namespace llvm;
22
23 namespace {
24 namespace stats {
25 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
26 }
27 }
28
29 static bool printMipsSymbolRef(const MCSymbolRefExpr &SRE,
30                                const MCSymbol &Sym, raw_ostream &OS) {
31   MCSymbolRefExpr::VariantKind Kind= SRE.getKind();
32
33   switch (Kind) {
34   default:
35     return false;
36   case MCSymbolRefExpr::VK_Mips_None:     break;
37   case MCSymbolRefExpr::VK_Mips_GPREL:    OS << "%gp_rel("; break;
38   case MCSymbolRefExpr::VK_Mips_GOT_CALL: OS << "%call16("; break;
39   case MCSymbolRefExpr::VK_Mips_GOT:      OS << "%got(";    break;
40   case MCSymbolRefExpr::VK_Mips_ABS_HI:   OS << "%hi(";     break;
41   case MCSymbolRefExpr::VK_Mips_ABS_LO:   OS << "%lo(";     break;
42   case MCSymbolRefExpr::VK_Mips_TLSGD:    OS << "%tlsgd(";  break;
43   case MCSymbolRefExpr::VK_Mips_GOTTPREL: OS << "%gottprel("; break;
44   case MCSymbolRefExpr::VK_Mips_TPREL_HI: OS << "%tprel_hi("; break;
45   case MCSymbolRefExpr::VK_Mips_TPREL_LO: OS << "%tprel_lo("; break;
46   case MCSymbolRefExpr::VK_Mips_GPOFF_HI: OS << "%hi(%neg(%gp_rel("; break;
47   case MCSymbolRefExpr::VK_Mips_GPOFF_LO: OS << "%lo(%neg(%gp_rel("; break;
48   case MCSymbolRefExpr::VK_Mips_GOT_DISP: OS << "%got_disp("; break;
49   case MCSymbolRefExpr::VK_Mips_GOT_PAGE: OS << "%got_page("; break;
50   case MCSymbolRefExpr::VK_Mips_GOT_OFST: OS << "%got_ofst("; break;
51   }
52
53   OS << Sym;
54
55   if (Kind != MCSymbolRefExpr::VK_Mips_None)
56     OS << ')';
57
58   return true;
59 }
60
61 void MCExpr::print(raw_ostream &OS) const {
62   switch (getKind()) {
63   case MCExpr::Target:
64     return cast<MCTargetExpr>(this)->PrintImpl(OS);
65   case MCExpr::Constant:
66     OS << cast<MCConstantExpr>(*this).getValue();
67     return;
68
69   case MCExpr::SymbolRef: {
70     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
71     const MCSymbol &Sym = SRE.getSymbol();
72     // Parenthesize names that start with $ so that they don't look like
73     // absolute names.
74     bool UseParens = Sym.getName()[0] == '$';
75
76     if (printMipsSymbolRef(SRE, Sym, OS))
77       return;
78
79     if (SRE.getKind() == MCSymbolRefExpr::VK_PPC_DARWIN_HA16 ||
80         SRE.getKind() == MCSymbolRefExpr::VK_PPC_DARWIN_LO16) {
81       OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
82       UseParens = true;
83     }
84
85     if (UseParens)
86       OS << '(' << Sym << ')';
87     else
88       OS << Sym;
89
90     if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT ||
91         SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD ||
92         SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT ||
93         SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF ||
94         SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF ||
95         SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF)
96       OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
97     else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
98              SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_HA16 &&
99              SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_LO16)
100       OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
101
102     return;
103   }
104
105   case MCExpr::Unary: {
106     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
107     switch (UE.getOpcode()) {
108     default: assert(0 && "Invalid opcode!");
109     case MCUnaryExpr::LNot:  OS << '!'; break;
110     case MCUnaryExpr::Minus: OS << '-'; break;
111     case MCUnaryExpr::Not:   OS << '~'; break;
112     case MCUnaryExpr::Plus:  OS << '+'; break;
113     }
114     OS << *UE.getSubExpr();
115     return;
116   }
117
118   case MCExpr::Binary: {
119     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
120
121     // Only print parens around the LHS if it is non-trivial.
122     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
123       OS << *BE.getLHS();
124     } else {
125       OS << '(' << *BE.getLHS() << ')';
126     }
127
128     switch (BE.getOpcode()) {
129     default: assert(0 && "Invalid opcode!");
130     case MCBinaryExpr::Add:
131       // Print "X-42" instead of "X+-42".
132       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
133         if (RHSC->getValue() < 0) {
134           OS << RHSC->getValue();
135           return;
136         }
137       }
138
139       OS <<  '+';
140       break;
141     case MCBinaryExpr::And:  OS <<  '&'; break;
142     case MCBinaryExpr::Div:  OS <<  '/'; break;
143     case MCBinaryExpr::EQ:   OS << "=="; break;
144     case MCBinaryExpr::GT:   OS <<  '>'; break;
145     case MCBinaryExpr::GTE:  OS << ">="; break;
146     case MCBinaryExpr::LAnd: OS << "&&"; break;
147     case MCBinaryExpr::LOr:  OS << "||"; break;
148     case MCBinaryExpr::LT:   OS <<  '<'; break;
149     case MCBinaryExpr::LTE:  OS << "<="; break;
150     case MCBinaryExpr::Mod:  OS <<  '%'; break;
151     case MCBinaryExpr::Mul:  OS <<  '*'; break;
152     case MCBinaryExpr::NE:   OS << "!="; break;
153     case MCBinaryExpr::Or:   OS <<  '|'; break;
154     case MCBinaryExpr::Shl:  OS << "<<"; break;
155     case MCBinaryExpr::Shr:  OS << ">>"; break;
156     case MCBinaryExpr::Sub:  OS <<  '-'; break;
157     case MCBinaryExpr::Xor:  OS <<  '^'; break;
158     }
159
160     // Only print parens around the LHS if it is non-trivial.
161     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
162       OS << *BE.getRHS();
163     } else {
164       OS << '(' << *BE.getRHS() << ')';
165     }
166     return;
167   }
168   }
169
170   assert(0 && "Invalid expression kind!");
171 }
172
173 void MCExpr::dump() const {
174   print(dbgs());
175   dbgs() << '\n';
176 }
177
178 /* *** */
179
180 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
181                                          const MCExpr *RHS, MCContext &Ctx) {
182   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
183 }
184
185 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
186                                        MCContext &Ctx) {
187   return new (Ctx) MCUnaryExpr(Opc, Expr);
188 }
189
190 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
191   return new (Ctx) MCConstantExpr(Value);
192 }
193
194 /* *** */
195
196 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
197                                                VariantKind Kind,
198                                                MCContext &Ctx) {
199   return new (Ctx) MCSymbolRefExpr(Sym, Kind);
200 }
201
202 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
203                                                MCContext &Ctx) {
204   return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
205 }
206
207 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
208   switch (Kind) {
209   default:
210   case VK_Invalid: return "<<invalid>>";
211   case VK_None: return "<<none>>";
212
213   case VK_GOT: return "GOT";
214   case VK_GOTOFF: return "GOTOFF";
215   case VK_GOTPCREL: return "GOTPCREL";
216   case VK_GOTTPOFF: return "GOTTPOFF";
217   case VK_INDNTPOFF: return "INDNTPOFF";
218   case VK_NTPOFF: return "NTPOFF";
219   case VK_GOTNTPOFF: return "GOTNTPOFF";
220   case VK_PLT: return "PLT";
221   case VK_TLSGD: return "TLSGD";
222   case VK_TLSLD: return "TLSLD";
223   case VK_TLSLDM: return "TLSLDM";
224   case VK_TPOFF: return "TPOFF";
225   case VK_DTPOFF: return "DTPOFF";
226   case VK_TLVP: return "TLVP";
227   case VK_ARM_PLT: return "(PLT)";
228   case VK_ARM_GOT: return "(GOT)";
229   case VK_ARM_GOTOFF: return "(GOTOFF)";
230   case VK_ARM_TPOFF: return "(tpoff)";
231   case VK_ARM_GOTTPOFF: return "(gottpoff)";
232   case VK_ARM_TLSGD: return "(tlsgd)";
233   case VK_PPC_TOC: return "toc";
234   case VK_PPC_DARWIN_HA16: return "ha16";
235   case VK_PPC_DARWIN_LO16: return "lo16";
236   case VK_PPC_GAS_HA16: return "ha";
237   case VK_PPC_GAS_LO16: return "l";
238   }
239 }
240
241 MCSymbolRefExpr::VariantKind
242 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
243   return StringSwitch<VariantKind>(Name)
244     .Case("GOT", VK_GOT)
245     .Case("got", VK_GOT)
246     .Case("GOTOFF", VK_GOTOFF)
247     .Case("gotoff", VK_GOTOFF)
248     .Case("GOTPCREL", VK_GOTPCREL)
249     .Case("gotpcrel", VK_GOTPCREL)
250     .Case("GOTTPOFF", VK_GOTTPOFF)
251     .Case("gottpoff", VK_GOTTPOFF)
252     .Case("INDNTPOFF", VK_INDNTPOFF)
253     .Case("indntpoff", VK_INDNTPOFF)
254     .Case("NTPOFF", VK_NTPOFF)
255     .Case("ntpoff", VK_NTPOFF)
256     .Case("GOTNTPOFF", VK_GOTNTPOFF)
257     .Case("gotntpoff", VK_GOTNTPOFF)
258     .Case("PLT", VK_PLT)
259     .Case("plt", VK_PLT)
260     .Case("TLSGD", VK_TLSGD)
261     .Case("tlsgd", VK_TLSGD)
262     .Case("TLSLD", VK_TLSLD)
263     .Case("tlsld", VK_TLSLD)
264     .Case("TLSLDM", VK_TLSLDM)
265     .Case("tlsldm", VK_TLSLDM)
266     .Case("TPOFF", VK_TPOFF)
267     .Case("tpoff", VK_TPOFF)
268     .Case("DTPOFF", VK_DTPOFF)
269     .Case("dtpoff", VK_DTPOFF)
270     .Case("TLVP", VK_TLVP)
271     .Case("tlvp", VK_TLVP)
272     .Default(VK_Invalid);
273 }
274
275 /* *** */
276
277 void MCTargetExpr::Anchor() {}
278
279 /* *** */
280
281 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
282   return EvaluateAsAbsolute(Res, 0, 0, 0);
283 }
284
285 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
286                                 const MCAsmLayout &Layout) const {
287   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
288 }
289
290 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
291                                 const MCAsmLayout &Layout,
292                                 const SectionAddrMap &Addrs) const {
293   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
294 }
295
296 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
297   return EvaluateAsAbsolute(Res, &Asm, 0, 0);
298 }
299
300 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
301                                 const MCAsmLayout *Layout,
302                                 const SectionAddrMap *Addrs) const {
303   MCValue Value;
304
305   // Fast path constants.
306   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
307     Res = CE->getValue();
308     return true;
309   }
310
311   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
312   // absolutize differences across sections and that is what the MachO writer
313   // uses Addrs for.
314   bool IsRelocatable =
315     EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
316
317   // Record the current value.
318   Res = Value.getConstant();
319
320   return IsRelocatable && Value.isAbsolute();
321 }
322
323 /// \brief Helper method for \see EvaluateSymbolAdd().
324 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
325                                                 const MCAsmLayout *Layout,
326                                                 const SectionAddrMap *Addrs,
327                                                 bool InSet,
328                                                 const MCSymbolRefExpr *&A,
329                                                 const MCSymbolRefExpr *&B,
330                                                 int64_t &Addend) {
331   if (!A || !B)
332     return;
333
334   const MCSymbol &SA = A->getSymbol();
335   const MCSymbol &SB = B->getSymbol();
336
337   if (SA.isUndefined() || SB.isUndefined())
338     return;
339
340   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
341     return;
342
343   MCSymbolData &AD = Asm->getSymbolData(SA);
344   MCSymbolData &BD = Asm->getSymbolData(SB);
345
346   if (AD.getFragment() == BD.getFragment()) {
347     Addend += (AD.getOffset() - BD.getOffset());
348
349     // Pointers to Thumb symbols need to have their low-bit set to allow
350     // for interworking.
351     if (Asm->isThumbFunc(&SA))
352       Addend |= 1;
353
354     // Clear the symbol expr pointers to indicate we have folded these
355     // operands.
356     A = B = 0;
357     return;
358   }
359
360   if (!Layout)
361     return;
362
363   const MCSectionData &SecA = *AD.getFragment()->getParent();
364   const MCSectionData &SecB = *BD.getFragment()->getParent();
365
366   if ((&SecA != &SecB) && !Addrs)
367     return;
368
369   // Eagerly evaluate.
370   Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
371              Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
372   if (Addrs && (&SecA != &SecB))
373     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
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   assert(0 && "Invalid assembly expression kind!");
596   return false;
597 }
598
599 const MCSection *MCExpr::FindAssociatedSection() const {
600   switch (getKind()) {
601   case Target:
602     // We never look through target specific expressions.
603     return cast<MCTargetExpr>(this)->FindAssociatedSection();
604
605   case Constant:
606     return MCSymbol::AbsolutePseudoSection;
607
608   case SymbolRef: {
609     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
610     const MCSymbol &Sym = SRE->getSymbol();
611
612     if (Sym.isDefined())
613       return &Sym.getSection();
614
615     return 0;
616   }
617
618   case Unary:
619     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
620
621   case Binary: {
622     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
623     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
624     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
625
626     // If either section is absolute, return the other.
627     if (LHS_S == MCSymbol::AbsolutePseudoSection)
628       return RHS_S;
629     if (RHS_S == MCSymbol::AbsolutePseudoSection)
630       return LHS_S;
631
632     // Otherwise, return the first non-null section.
633     return LHS_S ? LHS_S : RHS_S;
634   }
635   }
636
637   assert(0 && "Invalid assembly expression kind!");
638   return 0;
639 }