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 #include "llvm/MC/MCExpr.h"
     11 #include "llvm/ADT/Statistic.h"
     12 #include "llvm/ADT/StringSwitch.h"
     13 #include "llvm/MC/MCAsmInfo.h"
     14 #include "llvm/MC/MCAsmLayout.h"
     15 #include "llvm/MC/MCAssembler.h"
     16 #include "llvm/MC/MCContext.h"
     17 #include "llvm/MC/MCObjectWriter.h"
     18 #include "llvm/MC/MCSymbol.h"
     19 #include "llvm/MC/MCValue.h"
     20 #include "llvm/Support/Debug.h"
     21 #include "llvm/Support/ErrorHandling.h"
     22 #include "llvm/Support/raw_ostream.h"
     23 using namespace llvm;
     24 
     25 #define DEBUG_TYPE "mcexpr"
     26 
     27 namespace {
     28 namespace stats {
     29 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
     30 }
     31 }
     32 
     33 void MCExpr::print(raw_ostream &OS) const {
     34   switch (getKind()) {
     35   case MCExpr::Target:
     36     return cast<MCTargetExpr>(this)->PrintImpl(OS);
     37   case MCExpr::Constant:
     38     OS << cast<MCConstantExpr>(*this).getValue();
     39     return;
     40 
     41   case MCExpr::SymbolRef: {
     42     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
     43     const MCSymbol &Sym = SRE.getSymbol();
     44     // Parenthesize names that start with $ so that they don't look like
     45     // absolute names.
     46     bool UseParens = Sym.getName()[0] == '$';
     47     if (UseParens)
     48       OS << '(' << Sym << ')';
     49     else
     50       OS << Sym;
     51 
     52     if (SRE.getKind() != MCSymbolRefExpr::VK_None)
     53       SRE.printVariantKind(OS);
     54 
     55     return;
     56   }
     57 
     58   case MCExpr::Unary: {
     59     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
     60     switch (UE.getOpcode()) {
     61     case MCUnaryExpr::LNot:  OS << '!'; break;
     62     case MCUnaryExpr::Minus: OS << '-'; break;
     63     case MCUnaryExpr::Not:   OS << '~'; break;
     64     case MCUnaryExpr::Plus:  OS << '+'; break;
     65     }
     66     OS << *UE.getSubExpr();
     67     return;
     68   }
     69 
     70   case MCExpr::Binary: {
     71     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
     72 
     73     // Only print parens around the LHS if it is non-trivial.
     74     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
     75       OS << *BE.getLHS();
     76     } else {
     77       OS << '(' << *BE.getLHS() << ')';
     78     }
     79 
     80     switch (BE.getOpcode()) {
     81     case MCBinaryExpr::Add:
     82       // Print "X-42" instead of "X+-42".
     83       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
     84         if (RHSC->getValue() < 0) {
     85           OS << RHSC->getValue();
     86           return;
     87         }
     88       }
     89 
     90       OS <<  '+';
     91       break;
     92     case MCBinaryExpr::And:  OS <<  '&'; break;
     93     case MCBinaryExpr::Div:  OS <<  '/'; break;
     94     case MCBinaryExpr::EQ:   OS << "=="; break;
     95     case MCBinaryExpr::GT:   OS <<  '>'; break;
     96     case MCBinaryExpr::GTE:  OS << ">="; break;
     97     case MCBinaryExpr::LAnd: OS << "&&"; break;
     98     case MCBinaryExpr::LOr:  OS << "||"; break;
     99     case MCBinaryExpr::LT:   OS <<  '<'; break;
    100     case MCBinaryExpr::LTE:  OS << "<="; break;
    101     case MCBinaryExpr::Mod:  OS <<  '%'; break;
    102     case MCBinaryExpr::Mul:  OS <<  '*'; break;
    103     case MCBinaryExpr::NE:   OS << "!="; break;
    104     case MCBinaryExpr::Or:   OS <<  '|'; break;
    105     case MCBinaryExpr::Shl:  OS << "<<"; break;
    106     case MCBinaryExpr::Shr:  OS << ">>"; break;
    107     case MCBinaryExpr::Sub:  OS <<  '-'; break;
    108     case MCBinaryExpr::Xor:  OS <<  '^'; break;
    109     }
    110 
    111     // Only print parens around the LHS if it is non-trivial.
    112     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
    113       OS << *BE.getRHS();
    114     } else {
    115       OS << '(' << *BE.getRHS() << ')';
    116     }
    117     return;
    118   }
    119   }
    120 
    121   llvm_unreachable("Invalid expression kind!");
    122 }
    123 
    124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    125 void MCExpr::dump() const {
    126   print(dbgs());
    127   dbgs() << '\n';
    128 }
    129 #endif
    130 
    131 /* *** */
    132 
    133 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
    134                                          const MCExpr *RHS, MCContext &Ctx) {
    135   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
    136 }
    137 
    138 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
    139                                        MCContext &Ctx) {
    140   return new (Ctx) MCUnaryExpr(Opc, Expr);
    141 }
    142 
    143 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
    144   return new (Ctx) MCConstantExpr(Value);
    145 }
    146 
    147 /* *** */
    148 
    149 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
    150                                  const MCAsmInfo *MAI)
    151     : MCExpr(MCExpr::SymbolRef), Kind(Kind),
    152       UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
    153       HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
    154       Symbol(Symbol) {
    155   assert(Symbol);
    156 }
    157 
    158 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
    159                                                VariantKind Kind,
    160                                                MCContext &Ctx) {
    161   return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
    162 }
    163 
    164 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
    165                                                MCContext &Ctx) {
    166   return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
    167 }
    168 
    169 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
    170   switch (Kind) {
    171   case VK_Invalid: return "<<invalid>>";
    172   case VK_None: return "<<none>>";
    173 
    174   case VK_GOT: return "GOT";
    175   case VK_GOTOFF: return "GOTOFF";
    176   case VK_GOTPCREL: return "GOTPCREL";
    177   case VK_GOTTPOFF: return "GOTTPOFF";
    178   case VK_INDNTPOFF: return "INDNTPOFF";
    179   case VK_NTPOFF: return "NTPOFF";
    180   case VK_GOTNTPOFF: return "GOTNTPOFF";
    181   case VK_PLT: return "PLT";
    182   case VK_TLSGD: return "TLSGD";
    183   case VK_TLSLD: return "TLSLD";
    184   case VK_TLSLDM: return "TLSLDM";
    185   case VK_TPOFF: return "TPOFF";
    186   case VK_DTPOFF: return "DTPOFF";
    187   case VK_TLVP: return "TLVP";
    188   case VK_TLVPPAGE: return "TLVPPAGE";
    189   case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
    190   case VK_PAGE: return "PAGE";
    191   case VK_PAGEOFF: return "PAGEOFF";
    192   case VK_GOTPAGE: return "GOTPAGE";
    193   case VK_GOTPAGEOFF: return "GOTPAGEOFF";
    194   case VK_SECREL: return "SECREL32";
    195   case VK_SIZE: return "SIZE";
    196   case VK_WEAKREF: return "WEAKREF";
    197   case VK_ARM_NONE: return "none";
    198   case VK_ARM_TARGET1: return "target1";
    199   case VK_ARM_TARGET2: return "target2";
    200   case VK_ARM_PREL31: return "prel31";
    201   case VK_ARM_SBREL: return "sbrel";
    202   case VK_ARM_TLSLDO: return "tlsldo";
    203   case VK_ARM_TLSCALL: return "tlscall";
    204   case VK_ARM_TLSDESC: return "tlsdesc";
    205   case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
    206   case VK_PPC_LO: return "l";
    207   case VK_PPC_HI: return "h";
    208   case VK_PPC_HA: return "ha";
    209   case VK_PPC_HIGHER: return "higher";
    210   case VK_PPC_HIGHERA: return "highera";
    211   case VK_PPC_HIGHEST: return "highest";
    212   case VK_PPC_HIGHESTA: return "highesta";
    213   case VK_PPC_GOT_LO: return "got@l";
    214   case VK_PPC_GOT_HI: return "got@h";
    215   case VK_PPC_GOT_HA: return "got@ha";
    216   case VK_PPC_TOCBASE: return "tocbase";
    217   case VK_PPC_TOC: return "toc";
    218   case VK_PPC_TOC_LO: return "toc@l";
    219   case VK_PPC_TOC_HI: return "toc@h";
    220   case VK_PPC_TOC_HA: return "toc@ha";
    221   case VK_PPC_DTPMOD: return "dtpmod";
    222   case VK_PPC_TPREL: return "tprel";
    223   case VK_PPC_TPREL_LO: return "tprel@l";
    224   case VK_PPC_TPREL_HI: return "tprel@h";
    225   case VK_PPC_TPREL_HA: return "tprel@ha";
    226   case VK_PPC_TPREL_HIGHER: return "tprel@higher";
    227   case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
    228   case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
    229   case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
    230   case VK_PPC_DTPREL: return "dtprel";
    231   case VK_PPC_DTPREL_LO: return "dtprel@l";
    232   case VK_PPC_DTPREL_HI: return "dtprel@h";
    233   case VK_PPC_DTPREL_HA: return "dtprel@ha";
    234   case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
    235   case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
    236   case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
    237   case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
    238   case VK_PPC_GOT_TPREL: return "got@tprel";
    239   case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
    240   case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
    241   case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
    242   case VK_PPC_GOT_DTPREL: return "got@dtprel";
    243   case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
    244   case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
    245   case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
    246   case VK_PPC_TLS: return "tls";
    247   case VK_PPC_GOT_TLSGD: return "got@tlsgd";
    248   case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
    249   case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
    250   case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
    251   case VK_PPC_TLSGD: return "tlsgd";
    252   case VK_PPC_GOT_TLSLD: return "got@tlsld";
    253   case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
    254   case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
    255   case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
    256   case VK_PPC_TLSLD: return "tlsld";
    257   case VK_PPC_LOCAL: return "local";
    258   case VK_Mips_GPREL: return "GPREL";
    259   case VK_Mips_GOT_CALL: return "GOT_CALL";
    260   case VK_Mips_GOT16: return "GOT16";
    261   case VK_Mips_GOT: return "GOT";
    262   case VK_Mips_ABS_HI: return "ABS_HI";
    263   case VK_Mips_ABS_LO: return "ABS_LO";
    264   case VK_Mips_TLSGD: return "TLSGD";
    265   case VK_Mips_TLSLDM: return "TLSLDM";
    266   case VK_Mips_DTPREL_HI: return "DTPREL_HI";
    267   case VK_Mips_DTPREL_LO: return "DTPREL_LO";
    268   case VK_Mips_GOTTPREL: return "GOTTPREL";
    269   case VK_Mips_TPREL_HI: return "TPREL_HI";
    270   case VK_Mips_TPREL_LO: return "TPREL_LO";
    271   case VK_Mips_GPOFF_HI: return "GPOFF_HI";
    272   case VK_Mips_GPOFF_LO: return "GPOFF_LO";
    273   case VK_Mips_GOT_DISP: return "GOT_DISP";
    274   case VK_Mips_GOT_PAGE: return "GOT_PAGE";
    275   case VK_Mips_GOT_OFST: return "GOT_OFST";
    276   case VK_Mips_HIGHER:   return "HIGHER";
    277   case VK_Mips_HIGHEST:  return "HIGHEST";
    278   case VK_Mips_GOT_HI16: return "GOT_HI16";
    279   case VK_Mips_GOT_LO16: return "GOT_LO16";
    280   case VK_Mips_CALL_HI16: return "CALL_HI16";
    281   case VK_Mips_CALL_LO16: return "CALL_LO16";
    282   case VK_Mips_PCREL_HI16: return "PCREL_HI16";
    283   case VK_Mips_PCREL_LO16: return "PCREL_LO16";
    284   case VK_COFF_IMGREL32: return "IMGREL";
    285   }
    286   llvm_unreachable("Invalid variant kind");
    287 }
    288 
    289 MCSymbolRefExpr::VariantKind
    290 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
    291   return StringSwitch<VariantKind>(Name.lower())
    292     .Case("got", VK_GOT)
    293     .Case("gotoff", VK_GOTOFF)
    294     .Case("gotpcrel", VK_GOTPCREL)
    295     .Case("got_prel", VK_GOTPCREL)
    296     .Case("gottpoff", VK_GOTTPOFF)
    297     .Case("indntpoff", VK_INDNTPOFF)
    298     .Case("ntpoff", VK_NTPOFF)
    299     .Case("gotntpoff", VK_GOTNTPOFF)
    300     .Case("plt", VK_PLT)
    301     .Case("tlsgd", VK_TLSGD)
    302     .Case("tlsld", VK_TLSLD)
    303     .Case("tlsldm", VK_TLSLDM)
    304     .Case("tpoff", VK_TPOFF)
    305     .Case("dtpoff", VK_DTPOFF)
    306     .Case("tlvp", VK_TLVP)
    307     .Case("tlvppage", VK_TLVPPAGE)
    308     .Case("tlvppageoff", VK_TLVPPAGEOFF)
    309     .Case("page", VK_PAGE)
    310     .Case("pageoff", VK_PAGEOFF)
    311     .Case("gotpage", VK_GOTPAGE)
    312     .Case("gotpageoff", VK_GOTPAGEOFF)
    313     .Case("imgrel", VK_COFF_IMGREL32)
    314     .Case("secrel32", VK_SECREL)
    315     .Case("size", VK_SIZE)
    316     .Case("l", VK_PPC_LO)
    317     .Case("h", VK_PPC_HI)
    318     .Case("ha", VK_PPC_HA)
    319     .Case("higher", VK_PPC_HIGHER)
    320     .Case("highera", VK_PPC_HIGHERA)
    321     .Case("highest", VK_PPC_HIGHEST)
    322     .Case("highesta", VK_PPC_HIGHESTA)
    323     .Case("got@l", VK_PPC_GOT_LO)
    324     .Case("got@h", VK_PPC_GOT_HI)
    325     .Case("got@ha", VK_PPC_GOT_HA)
    326     .Case("local", VK_PPC_LOCAL)
    327     .Case("tocbase", VK_PPC_TOCBASE)
    328     .Case("toc", VK_PPC_TOC)
    329     .Case("toc@l", VK_PPC_TOC_LO)
    330     .Case("toc@h", VK_PPC_TOC_HI)
    331     .Case("toc@ha", VK_PPC_TOC_HA)
    332     .Case("tls", VK_PPC_TLS)
    333     .Case("dtpmod", VK_PPC_DTPMOD)
    334     .Case("tprel", VK_PPC_TPREL)
    335     .Case("tprel@l", VK_PPC_TPREL_LO)
    336     .Case("tprel@h", VK_PPC_TPREL_HI)
    337     .Case("tprel@ha", VK_PPC_TPREL_HA)
    338     .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
    339     .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
    340     .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
    341     .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
    342     .Case("dtprel", VK_PPC_DTPREL)
    343     .Case("dtprel@l", VK_PPC_DTPREL_LO)
    344     .Case("dtprel@h", VK_PPC_DTPREL_HI)
    345     .Case("dtprel@ha", VK_PPC_DTPREL_HA)
    346     .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
    347     .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
    348     .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
    349     .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
    350     .Case("got@tprel", VK_PPC_GOT_TPREL)
    351     .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
    352     .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
    353     .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
    354     .Case("got@dtprel", VK_PPC_GOT_DTPREL)
    355     .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
    356     .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
    357     .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
    358     .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
    359     .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
    360     .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
    361     .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
    362     .Case("got@tlsld", VK_PPC_GOT_TLSLD)
    363     .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
    364     .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
    365     .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
    366     .Case("none", VK_ARM_NONE)
    367     .Case("target1", VK_ARM_TARGET1)
    368     .Case("target2", VK_ARM_TARGET2)
    369     .Case("prel31", VK_ARM_PREL31)
    370     .Case("sbrel", VK_ARM_SBREL)
    371     .Case("tlsldo", VK_ARM_TLSLDO)
    372     .Case("tlscall", VK_ARM_TLSCALL)
    373     .Case("tlsdesc", VK_ARM_TLSDESC)
    374     .Default(VK_Invalid);
    375 }
    376 
    377 void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
    378   if (UseParensForSymbolVariant)
    379     OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
    380   else
    381     OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
    382 }
    383 
    384 /* *** */
    385 
    386 void MCTargetExpr::anchor() {}
    387 
    388 /* *** */
    389 
    390 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
    391   return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
    392 }
    393 
    394 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
    395                                 const MCAsmLayout &Layout) const {
    396   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
    397 }
    398 
    399 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
    400                                 const MCAsmLayout &Layout,
    401                                 const SectionAddrMap &Addrs) const {
    402   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
    403 }
    404 
    405 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
    406   return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
    407 }
    408 
    409 bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
    410                                    const MCAsmLayout &Layout) const {
    411   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
    412                             true);
    413 }
    414 
    415 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
    416                                 const MCAsmLayout *Layout,
    417                                 const SectionAddrMap *Addrs) const {
    418   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
    419   // absolutize differences across sections and that is what the MachO writer
    420   // uses Addrs for.
    421   return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
    422 }
    423 
    424 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
    425                                 const MCAsmLayout *Layout,
    426                                 const SectionAddrMap *Addrs, bool InSet) const {
    427   MCValue Value;
    428 
    429   // Fast path constants.
    430   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
    431     Res = CE->getValue();
    432     return true;
    433   }
    434 
    435   bool IsRelocatable =
    436       EvaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
    437 
    438   // Record the current value.
    439   Res = Value.getConstant();
    440 
    441   return IsRelocatable && Value.isAbsolute();
    442 }
    443 
    444 /// \brief Helper method for \see EvaluateSymbolAdd().
    445 static void AttemptToFoldSymbolOffsetDifference(
    446     const MCAssembler *Asm, const MCAsmLayout *Layout,
    447     const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
    448     const MCSymbolRefExpr *&B, int64_t &Addend) {
    449   if (!A || !B)
    450     return;
    451 
    452   const MCSymbol &SA = A->getSymbol();
    453   const MCSymbol &SB = B->getSymbol();
    454 
    455   if (SA.isUndefined() || SB.isUndefined())
    456     return;
    457 
    458   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
    459     return;
    460 
    461   const MCSymbolData &AD = Asm->getSymbolData(SA);
    462   const MCSymbolData &BD = Asm->getSymbolData(SB);
    463 
    464   if (AD.getFragment() == BD.getFragment()) {
    465     Addend += (AD.getOffset() - BD.getOffset());
    466 
    467     // Pointers to Thumb symbols need to have their low-bit set to allow
    468     // for interworking.
    469     if (Asm->isThumbFunc(&SA))
    470       Addend |= 1;
    471 
    472     // Clear the symbol expr pointers to indicate we have folded these
    473     // operands.
    474     A = B = nullptr;
    475     return;
    476   }
    477 
    478   if (!Layout)
    479     return;
    480 
    481   const MCSectionData &SecA = *AD.getFragment()->getParent();
    482   const MCSectionData &SecB = *BD.getFragment()->getParent();
    483 
    484   if ((&SecA != &SecB) && !Addrs)
    485     return;
    486 
    487   // Eagerly evaluate.
    488   Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
    489              Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
    490   if (Addrs && (&SecA != &SecB))
    491     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
    492 
    493   // Pointers to Thumb symbols need to have their low-bit set to allow
    494   // for interworking.
    495   if (Asm->isThumbFunc(&SA))
    496     Addend |= 1;
    497 
    498   // Clear the symbol expr pointers to indicate we have folded these
    499   // operands.
    500   A = B = nullptr;
    501 }
    502 
    503 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
    504 ///
    505 /// This routine conceptually attempts to construct an MCValue:
    506 ///   Result = (Result_A - Result_B + Result_Cst)
    507 /// from two MCValue's LHS and RHS where
    508 ///   Result = LHS + RHS
    509 /// and
    510 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
    511 ///
    512 /// This routine attempts to aggresively fold the operands such that the result
    513 /// is representable in an MCValue, but may not always succeed.
    514 ///
    515 /// \returns True on success, false if the result is not representable in an
    516 /// MCValue.
    517 
    518 /// NOTE: It is really important to have both the Asm and Layout arguments.
    519 /// They might look redundant, but this function can be used before layout
    520 /// is done (see the object streamer for example) and having the Asm argument
    521 /// lets us avoid relaxations early.
    522 static bool
    523 EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
    524                     const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
    525                     const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
    526                     int64_t RHS_Cst, MCValue &Res) {
    527   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
    528   // about dealing with modifiers. This will ultimately bite us, one day.
    529   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
    530   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
    531   int64_t LHS_Cst = LHS.getConstant();
    532 
    533   // Fold the result constant immediately.
    534   int64_t Result_Cst = LHS_Cst + RHS_Cst;
    535 
    536   assert((!Layout || Asm) &&
    537          "Must have an assembler object if layout is given!");
    538 
    539   // If we have a layout, we can fold resolved differences.
    540   if (Asm) {
    541     // First, fold out any differences which are fully resolved. By
    542     // reassociating terms in
    543     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
    544     // we have the four possible differences:
    545     //   (LHS_A - LHS_B),
    546     //   (LHS_A - RHS_B),
    547     //   (RHS_A - LHS_B),
    548     //   (RHS_A - RHS_B).
    549     // Since we are attempting to be as aggressive as possible about folding, we
    550     // attempt to evaluate each possible alternative.
    551     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
    552                                         Result_Cst);
    553     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
    554                                         Result_Cst);
    555     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
    556                                         Result_Cst);
    557     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
    558                                         Result_Cst);
    559   }
    560 
    561   // We can't represent the addition or subtraction of two symbols.
    562   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
    563     return false;
    564 
    565   // At this point, we have at most one additive symbol and one subtractive
    566   // symbol -- find them.
    567   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
    568   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
    569 
    570   // If we have a negated symbol, then we must have also have a non-negated
    571   // symbol in order to encode the expression.
    572   if (B && !A)
    573     return false;
    574 
    575   Res = MCValue::get(A, B, Result_Cst);
    576   return true;
    577 }
    578 
    579 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
    580                                    const MCAsmLayout *Layout,
    581                                    const MCFixup *Fixup) const {
    582   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
    583   return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
    584                                    false);
    585 }
    586 
    587 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
    588   MCAssembler *Assembler = &Layout.getAssembler();
    589   return EvaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
    590                                    true);
    591 }
    592 
    593 static bool canExpand(const MCSymbol &Sym, const MCAssembler *Asm, bool InSet) {
    594   if (InSet)
    595     return true;
    596   if (!Asm)
    597     return false;
    598   const MCSymbolData &SD = Asm->getSymbolData(Sym);
    599   return !Asm->getWriter().isWeak(SD);
    600 }
    601 
    602 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
    603                                        const MCAsmLayout *Layout,
    604                                        const MCFixup *Fixup,
    605                                        const SectionAddrMap *Addrs,
    606                                        bool InSet) const {
    607   ++stats::MCExprEvaluate;
    608 
    609   switch (getKind()) {
    610   case Target:
    611     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
    612                                                                Fixup);
    613 
    614   case Constant:
    615     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
    616     return true;
    617 
    618   case SymbolRef: {
    619     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
    620     const MCSymbol &Sym = SRE->getSymbol();
    621 
    622     // Evaluate recursively if this is a variable.
    623     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
    624         canExpand(Sym, Asm, InSet)) {
    625       bool IsMachO = SRE->hasSubsectionsViaSymbols();
    626       if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
    627               Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
    628         if (!IsMachO)
    629           return true;
    630 
    631         const MCSymbolRefExpr *A = Res.getSymA();
    632         const MCSymbolRefExpr *B = Res.getSymB();
    633         // FIXME: This is small hack. Given
    634         // a = b + 4
    635         // .long a
    636         // the OS X assembler will completely drop the 4. We should probably
    637         // include it in the relocation or produce an error if that is not
    638         // possible.
    639         if (!A && !B)
    640           return true;
    641       }
    642     }
    643 
    644     Res = MCValue::get(SRE, nullptr, 0);
    645     return true;
    646   }
    647 
    648   case Unary: {
    649     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
    650     MCValue Value;
    651 
    652     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
    653                                                       Addrs, InSet))
    654       return false;
    655 
    656     switch (AUE->getOpcode()) {
    657     case MCUnaryExpr::LNot:
    658       if (!Value.isAbsolute())
    659         return false;
    660       Res = MCValue::get(!Value.getConstant());
    661       break;
    662     case MCUnaryExpr::Minus:
    663       /// -(a - b + const) ==> (b - a - const)
    664       if (Value.getSymA() && !Value.getSymB())
    665         return false;
    666       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
    667                          -Value.getConstant());
    668       break;
    669     case MCUnaryExpr::Not:
    670       if (!Value.isAbsolute())
    671         return false;
    672       Res = MCValue::get(~Value.getConstant());
    673       break;
    674     case MCUnaryExpr::Plus:
    675       Res = Value;
    676       break;
    677     }
    678 
    679     return true;
    680   }
    681 
    682   case Binary: {
    683     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
    684     MCValue LHSValue, RHSValue;
    685 
    686     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
    687                                                   Addrs, InSet) ||
    688         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
    689                                                   Addrs, InSet))
    690       return false;
    691 
    692     // We only support a few operations on non-constant expressions, handle
    693     // those first.
    694     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
    695       switch (ABE->getOpcode()) {
    696       default:
    697         return false;
    698       case MCBinaryExpr::Sub:
    699         // Negate RHS and add.
    700         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
    701                                    RHSValue.getSymB(), RHSValue.getSymA(),
    702                                    -RHSValue.getConstant(), Res);
    703 
    704       case MCBinaryExpr::Add:
    705         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
    706                                    RHSValue.getSymA(), RHSValue.getSymB(),
    707                                    RHSValue.getConstant(), Res);
    708       }
    709     }
    710 
    711     // FIXME: We need target hooks for the evaluation. It may be limited in
    712     // width, and gas defines the result of comparisons and right shifts
    713     // differently from Apple as.
    714     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
    715     int64_t Result = 0;
    716     switch (ABE->getOpcode()) {
    717     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
    718     case MCBinaryExpr::And:  Result = LHS & RHS; break;
    719     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
    720     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
    721     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
    722     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
    723     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
    724     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
    725     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
    726     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
    727     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
    728     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
    729     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
    730     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
    731     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
    732     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
    733     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
    734     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
    735     }
    736 
    737     Res = MCValue::get(Result);
    738     return true;
    739   }
    740   }
    741 
    742   llvm_unreachable("Invalid assembly expression kind!");
    743 }
    744 
    745 const MCSection *MCExpr::FindAssociatedSection() const {
    746   switch (getKind()) {
    747   case Target:
    748     // We never look through target specific expressions.
    749     return cast<MCTargetExpr>(this)->FindAssociatedSection();
    750 
    751   case Constant:
    752     return MCSymbol::AbsolutePseudoSection;
    753 
    754   case SymbolRef: {
    755     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
    756     const MCSymbol &Sym = SRE->getSymbol();
    757 
    758     if (Sym.isDefined())
    759       return &Sym.getSection();
    760 
    761     return nullptr;
    762   }
    763 
    764   case Unary:
    765     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
    766 
    767   case Binary: {
    768     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
    769     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
    770     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
    771 
    772     // If either section is absolute, return the other.
    773     if (LHS_S == MCSymbol::AbsolutePseudoSection)
    774       return RHS_S;
    775     if (RHS_S == MCSymbol::AbsolutePseudoSection)
    776       return LHS_S;
    777 
    778     // Not always correct, but probably the best we can do without more context.
    779     if (BE->getOpcode() == MCBinaryExpr::Sub)
    780       return MCSymbol::AbsolutePseudoSection;
    781 
    782     // Otherwise, return the first non-null section.
    783     return LHS_S ? LHS_S : RHS_S;
    784   }
    785   }
    786 
    787   llvm_unreachable("Invalid assembly expression kind!");
    788 }
    789