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