LLVM API Documentation

MCExpr.cpp
Go to the documentation of this file.
00001 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/MC/MCExpr.h"
00011 #include "llvm/ADT/Statistic.h"
00012 #include "llvm/ADT/StringSwitch.h"
00013 #include "llvm/MC/MCAsmInfo.h"
00014 #include "llvm/MC/MCAsmLayout.h"
00015 #include "llvm/MC/MCAssembler.h"
00016 #include "llvm/MC/MCContext.h"
00017 #include "llvm/MC/MCObjectWriter.h"
00018 #include "llvm/MC/MCSymbol.h"
00019 #include "llvm/MC/MCValue.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Support/ErrorHandling.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 using namespace llvm;
00024 
00025 #define DEBUG_TYPE "mcexpr"
00026 
00027 namespace {
00028 namespace stats {
00029 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
00030 }
00031 }
00032 
00033 void MCExpr::print(raw_ostream &OS) const {
00034   switch (getKind()) {
00035   case MCExpr::Target:
00036     return cast<MCTargetExpr>(this)->PrintImpl(OS);
00037   case MCExpr::Constant:
00038     OS << cast<MCConstantExpr>(*this).getValue();
00039     return;
00040 
00041   case MCExpr::SymbolRef: {
00042     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
00043     const MCSymbol &Sym = SRE.getSymbol();
00044     // Parenthesize names that start with $ so that they don't look like
00045     // absolute names.
00046     bool UseParens = Sym.getName()[0] == '$';
00047     if (UseParens)
00048       OS << '(' << Sym << ')';
00049     else
00050       OS << Sym;
00051 
00052     if (SRE.getKind() != MCSymbolRefExpr::VK_None) {
00053       if (SRE.getMCAsmInfo().useParensForSymbolVariant())
00054         OS << '(' << MCSymbolRefExpr::getVariantKindName(SRE.getKind()) << ')';
00055       else
00056         OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
00057     }
00058 
00059     return;
00060   }
00061 
00062   case MCExpr::Unary: {
00063     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
00064     switch (UE.getOpcode()) {
00065     case MCUnaryExpr::LNot:  OS << '!'; break;
00066     case MCUnaryExpr::Minus: OS << '-'; break;
00067     case MCUnaryExpr::Not:   OS << '~'; break;
00068     case MCUnaryExpr::Plus:  OS << '+'; break;
00069     }
00070     OS << *UE.getSubExpr();
00071     return;
00072   }
00073 
00074   case MCExpr::Binary: {
00075     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
00076 
00077     // Only print parens around the LHS if it is non-trivial.
00078     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
00079       OS << *BE.getLHS();
00080     } else {
00081       OS << '(' << *BE.getLHS() << ')';
00082     }
00083 
00084     switch (BE.getOpcode()) {
00085     case MCBinaryExpr::Add:
00086       // Print "X-42" instead of "X+-42".
00087       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
00088         if (RHSC->getValue() < 0) {
00089           OS << RHSC->getValue();
00090           return;
00091         }
00092       }
00093 
00094       OS <<  '+';
00095       break;
00096     case MCBinaryExpr::And:  OS <<  '&'; break;
00097     case MCBinaryExpr::Div:  OS <<  '/'; break;
00098     case MCBinaryExpr::EQ:   OS << "=="; break;
00099     case MCBinaryExpr::GT:   OS <<  '>'; break;
00100     case MCBinaryExpr::GTE:  OS << ">="; break;
00101     case MCBinaryExpr::LAnd: OS << "&&"; break;
00102     case MCBinaryExpr::LOr:  OS << "||"; break;
00103     case MCBinaryExpr::LT:   OS <<  '<'; break;
00104     case MCBinaryExpr::LTE:  OS << "<="; break;
00105     case MCBinaryExpr::Mod:  OS <<  '%'; break;
00106     case MCBinaryExpr::Mul:  OS <<  '*'; break;
00107     case MCBinaryExpr::NE:   OS << "!="; break;
00108     case MCBinaryExpr::Or:   OS <<  '|'; break;
00109     case MCBinaryExpr::Shl:  OS << "<<"; break;
00110     case MCBinaryExpr::Shr:  OS << ">>"; break;
00111     case MCBinaryExpr::Sub:  OS <<  '-'; break;
00112     case MCBinaryExpr::Xor:  OS <<  '^'; break;
00113     }
00114 
00115     // Only print parens around the LHS if it is non-trivial.
00116     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
00117       OS << *BE.getRHS();
00118     } else {
00119       OS << '(' << *BE.getRHS() << ')';
00120     }
00121     return;
00122   }
00123   }
00124 
00125   llvm_unreachable("Invalid expression kind!");
00126 }
00127 
00128 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00129 void MCExpr::dump() const {
00130   print(dbgs());
00131   dbgs() << '\n';
00132 }
00133 #endif
00134 
00135 /* *** */
00136 
00137 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
00138                                          const MCExpr *RHS, MCContext &Ctx) {
00139   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
00140 }
00141 
00142 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
00143                                        MCContext &Ctx) {
00144   return new (Ctx) MCUnaryExpr(Opc, Expr);
00145 }
00146 
00147 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
00148   return new (Ctx) MCConstantExpr(Value);
00149 }
00150 
00151 /* *** */
00152 
00153 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
00154                                                VariantKind Kind,
00155                                                MCContext &Ctx) {
00156   return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo());
00157 }
00158 
00159 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
00160                                                MCContext &Ctx) {
00161   return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
00162 }
00163 
00164 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
00165   switch (Kind) {
00166   case VK_Invalid: return "<<invalid>>";
00167   case VK_None: return "<<none>>";
00168 
00169   case VK_GOT: return "GOT";
00170   case VK_GOTOFF: return "GOTOFF";
00171   case VK_GOTPCREL: return "GOTPCREL";
00172   case VK_GOTTPOFF: return "GOTTPOFF";
00173   case VK_INDNTPOFF: return "INDNTPOFF";
00174   case VK_NTPOFF: return "NTPOFF";
00175   case VK_GOTNTPOFF: return "GOTNTPOFF";
00176   case VK_PLT: return "PLT";
00177   case VK_TLSGD: return "TLSGD";
00178   case VK_TLSLD: return "TLSLD";
00179   case VK_TLSLDM: return "TLSLDM";
00180   case VK_TPOFF: return "TPOFF";
00181   case VK_DTPOFF: return "DTPOFF";
00182   case VK_TLVP: return "TLVP";
00183   case VK_TLVPPAGE: return "TLVPPAGE";
00184   case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
00185   case VK_PAGE: return "PAGE";
00186   case VK_PAGEOFF: return "PAGEOFF";
00187   case VK_GOTPAGE: return "GOTPAGE";
00188   case VK_GOTPAGEOFF: return "GOTPAGEOFF";
00189   case VK_SECREL: return "SECREL32";
00190   case VK_WEAKREF: return "WEAKREF";
00191   case VK_ARM_NONE: return "none";
00192   case VK_ARM_TARGET1: return "target1";
00193   case VK_ARM_TARGET2: return "target2";
00194   case VK_ARM_PREL31: return "prel31";
00195   case VK_ARM_TLSLDO: return "tlsldo";
00196   case VK_ARM_TLSCALL: return "tlscall";
00197   case VK_ARM_TLSDESC: return "tlsdesc";
00198   case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
00199   case VK_PPC_LO: return "l";
00200   case VK_PPC_HI: return "h";
00201   case VK_PPC_HA: return "ha";
00202   case VK_PPC_HIGHER: return "higher";
00203   case VK_PPC_HIGHERA: return "highera";
00204   case VK_PPC_HIGHEST: return "highest";
00205   case VK_PPC_HIGHESTA: return "highesta";
00206   case VK_PPC_GOT_LO: return "got@l";
00207   case VK_PPC_GOT_HI: return "got@h";
00208   case VK_PPC_GOT_HA: return "got@ha";
00209   case VK_PPC_TOCBASE: return "tocbase";
00210   case VK_PPC_TOC: return "toc";
00211   case VK_PPC_TOC_LO: return "toc@l";
00212   case VK_PPC_TOC_HI: return "toc@h";
00213   case VK_PPC_TOC_HA: return "toc@ha";
00214   case VK_PPC_DTPMOD: return "dtpmod";
00215   case VK_PPC_TPREL: return "tprel";
00216   case VK_PPC_TPREL_LO: return "tprel@l";
00217   case VK_PPC_TPREL_HI: return "tprel@h";
00218   case VK_PPC_TPREL_HA: return "tprel@ha";
00219   case VK_PPC_TPREL_HIGHER: return "tprel@higher";
00220   case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
00221   case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
00222   case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
00223   case VK_PPC_DTPREL: return "dtprel";
00224   case VK_PPC_DTPREL_LO: return "dtprel@l";
00225   case VK_PPC_DTPREL_HI: return "dtprel@h";
00226   case VK_PPC_DTPREL_HA: return "dtprel@ha";
00227   case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
00228   case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
00229   case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
00230   case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
00231   case VK_PPC_GOT_TPREL: return "got@tprel";
00232   case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
00233   case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
00234   case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
00235   case VK_PPC_GOT_DTPREL: return "got@dtprel";
00236   case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
00237   case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
00238   case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
00239   case VK_PPC_TLS: return "tls";
00240   case VK_PPC_GOT_TLSGD: return "got@tlsgd";
00241   case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
00242   case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
00243   case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
00244   case VK_PPC_TLSGD: return "tlsgd";
00245   case VK_PPC_GOT_TLSLD: return "got@tlsld";
00246   case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
00247   case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
00248   case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
00249   case VK_PPC_TLSLD: return "tlsld";
00250   case VK_Mips_GPREL: return "GPREL";
00251   case VK_Mips_GOT_CALL: return "GOT_CALL";
00252   case VK_Mips_GOT16: return "GOT16";
00253   case VK_Mips_GOT: return "GOT";
00254   case VK_Mips_ABS_HI: return "ABS_HI";
00255   case VK_Mips_ABS_LO: return "ABS_LO";
00256   case VK_Mips_TLSGD: return "TLSGD";
00257   case VK_Mips_TLSLDM: return "TLSLDM";
00258   case VK_Mips_DTPREL_HI: return "DTPREL_HI";
00259   case VK_Mips_DTPREL_LO: return "DTPREL_LO";
00260   case VK_Mips_GOTTPREL: return "GOTTPREL";
00261   case VK_Mips_TPREL_HI: return "TPREL_HI";
00262   case VK_Mips_TPREL_LO: return "TPREL_LO";
00263   case VK_Mips_GPOFF_HI: return "GPOFF_HI";
00264   case VK_Mips_GPOFF_LO: return "GPOFF_LO";
00265   case VK_Mips_GOT_DISP: return "GOT_DISP";
00266   case VK_Mips_GOT_PAGE: return "GOT_PAGE";
00267   case VK_Mips_GOT_OFST: return "GOT_OFST";
00268   case VK_Mips_HIGHER:   return "HIGHER";
00269   case VK_Mips_HIGHEST:  return "HIGHEST";
00270   case VK_Mips_GOT_HI16: return "GOT_HI16";
00271   case VK_Mips_GOT_LO16: return "GOT_LO16";
00272   case VK_Mips_CALL_HI16: return "CALL_HI16";
00273   case VK_Mips_CALL_LO16: return "CALL_LO16";
00274   case VK_Mips_PCREL_HI16: return "PCREL_HI16";
00275   case VK_Mips_PCREL_LO16: return "PCREL_LO16";
00276   case VK_COFF_IMGREL32: return "IMGREL32";
00277   }
00278   llvm_unreachable("Invalid variant kind");
00279 }
00280 
00281 MCSymbolRefExpr::VariantKind
00282 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
00283   return StringSwitch<VariantKind>(Name)
00284     .Case("GOT", VK_GOT)
00285     .Case("got", VK_GOT)
00286     .Case("GOTOFF", VK_GOTOFF)
00287     .Case("gotoff", VK_GOTOFF)
00288     .Case("GOTPCREL", VK_GOTPCREL)
00289     .Case("gotpcrel", VK_GOTPCREL)
00290     .Case("GOT_PREL", VK_GOTPCREL)
00291     .Case("got_prel", VK_GOTPCREL)
00292     .Case("GOTTPOFF", VK_GOTTPOFF)
00293     .Case("gottpoff", VK_GOTTPOFF)
00294     .Case("INDNTPOFF", VK_INDNTPOFF)
00295     .Case("indntpoff", VK_INDNTPOFF)
00296     .Case("NTPOFF", VK_NTPOFF)
00297     .Case("ntpoff", VK_NTPOFF)
00298     .Case("GOTNTPOFF", VK_GOTNTPOFF)
00299     .Case("gotntpoff", VK_GOTNTPOFF)
00300     .Case("PLT", VK_PLT)
00301     .Case("plt", VK_PLT)
00302     .Case("TLSGD", VK_TLSGD)
00303     .Case("tlsgd", VK_TLSGD)
00304     .Case("TLSLD", VK_TLSLD)
00305     .Case("tlsld", VK_TLSLD)
00306     .Case("TLSLDM", VK_TLSLDM)
00307     .Case("tlsldm", VK_TLSLDM)
00308     .Case("TPOFF", VK_TPOFF)
00309     .Case("tpoff", VK_TPOFF)
00310     .Case("DTPOFF", VK_DTPOFF)
00311     .Case("dtpoff", VK_DTPOFF)
00312     .Case("TLVP", VK_TLVP)
00313     .Case("tlvp", VK_TLVP)
00314     .Case("TLVPPAGE", VK_TLVPPAGE)
00315     .Case("tlvppage", VK_TLVPPAGE)
00316     .Case("TLVPPAGEOFF", VK_TLVPPAGEOFF)
00317     .Case("tlvppageoff", VK_TLVPPAGEOFF)
00318     .Case("PAGE", VK_PAGE)
00319     .Case("page", VK_PAGE)
00320     .Case("PAGEOFF", VK_PAGEOFF)
00321     .Case("pageoff", VK_PAGEOFF)
00322     .Case("GOTPAGE", VK_GOTPAGE)
00323     .Case("gotpage", VK_GOTPAGE)
00324     .Case("GOTPAGEOFF", VK_GOTPAGEOFF)
00325     .Case("gotpageoff", VK_GOTPAGEOFF)
00326     .Case("IMGREL", VK_COFF_IMGREL32)
00327     .Case("imgrel", VK_COFF_IMGREL32)
00328     .Case("SECREL32", VK_SECREL)
00329     .Case("secrel32", VK_SECREL)
00330     .Case("L", VK_PPC_LO)
00331     .Case("l", VK_PPC_LO)
00332     .Case("H", VK_PPC_HI)
00333     .Case("h", VK_PPC_HI)
00334     .Case("HA", VK_PPC_HA)
00335     .Case("ha", VK_PPC_HA)
00336     .Case("HIGHER", VK_PPC_HIGHER)
00337     .Case("higher", VK_PPC_HIGHER)
00338     .Case("HIGHERA", VK_PPC_HIGHERA)
00339     .Case("highera", VK_PPC_HIGHERA)
00340     .Case("HIGHEST", VK_PPC_HIGHEST)
00341     .Case("highest", VK_PPC_HIGHEST)
00342     .Case("HIGHESTA", VK_PPC_HIGHESTA)
00343     .Case("highesta", VK_PPC_HIGHESTA)
00344     .Case("GOT@L", VK_PPC_GOT_LO)
00345     .Case("got@l", VK_PPC_GOT_LO)
00346     .Case("GOT@H", VK_PPC_GOT_HI)
00347     .Case("got@h", VK_PPC_GOT_HI)
00348     .Case("GOT@HA", VK_PPC_GOT_HA)
00349     .Case("got@ha", VK_PPC_GOT_HA)
00350     .Case("TOCBASE", VK_PPC_TOCBASE)
00351     .Case("tocbase", VK_PPC_TOCBASE)
00352     .Case("TOC", VK_PPC_TOC)
00353     .Case("toc", VK_PPC_TOC)
00354     .Case("TOC@L", VK_PPC_TOC_LO)
00355     .Case("toc@l", VK_PPC_TOC_LO)
00356     .Case("TOC@H", VK_PPC_TOC_HI)
00357     .Case("toc@h", VK_PPC_TOC_HI)
00358     .Case("TOC@HA", VK_PPC_TOC_HA)
00359     .Case("toc@ha", VK_PPC_TOC_HA)
00360     .Case("TLS", VK_PPC_TLS)
00361     .Case("tls", VK_PPC_TLS)
00362     .Case("DTPMOD", VK_PPC_DTPMOD)
00363     .Case("dtpmod", VK_PPC_DTPMOD)
00364     .Case("TPREL", VK_PPC_TPREL)
00365     .Case("tprel", VK_PPC_TPREL)
00366     .Case("TPREL@L", VK_PPC_TPREL_LO)
00367     .Case("tprel@l", VK_PPC_TPREL_LO)
00368     .Case("TPREL@H", VK_PPC_TPREL_HI)
00369     .Case("tprel@h", VK_PPC_TPREL_HI)
00370     .Case("TPREL@HA", VK_PPC_TPREL_HA)
00371     .Case("tprel@ha", VK_PPC_TPREL_HA)
00372     .Case("TPREL@HIGHER", VK_PPC_TPREL_HIGHER)
00373     .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
00374     .Case("TPREL@HIGHERA", VK_PPC_TPREL_HIGHERA)
00375     .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
00376     .Case("TPREL@HIGHEST", VK_PPC_TPREL_HIGHEST)
00377     .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
00378     .Case("TPREL@HIGHESTA", VK_PPC_TPREL_HIGHESTA)
00379     .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
00380     .Case("DTPREL", VK_PPC_DTPREL)
00381     .Case("dtprel", VK_PPC_DTPREL)
00382     .Case("DTPREL@L", VK_PPC_DTPREL_LO)
00383     .Case("dtprel@l", VK_PPC_DTPREL_LO)
00384     .Case("DTPREL@H", VK_PPC_DTPREL_HI)
00385     .Case("dtprel@h", VK_PPC_DTPREL_HI)
00386     .Case("DTPREL@HA", VK_PPC_DTPREL_HA)
00387     .Case("dtprel@ha", VK_PPC_DTPREL_HA)
00388     .Case("DTPREL@HIGHER", VK_PPC_DTPREL_HIGHER)
00389     .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
00390     .Case("DTPREL@HIGHERA", VK_PPC_DTPREL_HIGHERA)
00391     .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
00392     .Case("DTPREL@HIGHEST", VK_PPC_DTPREL_HIGHEST)
00393     .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
00394     .Case("DTPREL@HIGHESTA", VK_PPC_DTPREL_HIGHESTA)
00395     .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
00396     .Case("GOT@TPREL", VK_PPC_GOT_TPREL)
00397     .Case("got@tprel", VK_PPC_GOT_TPREL)
00398     .Case("GOT@TPREL@L", VK_PPC_GOT_TPREL_LO)
00399     .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
00400     .Case("GOT@TPREL@H", VK_PPC_GOT_TPREL_HI)
00401     .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
00402     .Case("GOT@TPREL@HA", VK_PPC_GOT_TPREL_HA)
00403     .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
00404     .Case("GOT@DTPREL", VK_PPC_GOT_DTPREL)
00405     .Case("got@dtprel", VK_PPC_GOT_DTPREL)
00406     .Case("GOT@DTPREL@L", VK_PPC_GOT_DTPREL_LO)
00407     .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
00408     .Case("GOT@DTPREL@H", VK_PPC_GOT_DTPREL_HI)
00409     .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
00410     .Case("GOT@DTPREL@HA", VK_PPC_GOT_DTPREL_HA)
00411     .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
00412     .Case("GOT@TLSGD", VK_PPC_GOT_TLSGD)
00413     .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
00414     .Case("GOT@TLSGD@L", VK_PPC_GOT_TLSGD_LO)
00415     .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
00416     .Case("GOT@TLSGD@H", VK_PPC_GOT_TLSGD_HI)
00417     .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
00418     .Case("GOT@TLSGD@HA", VK_PPC_GOT_TLSGD_HA)
00419     .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
00420     .Case("GOT@TLSLD", VK_PPC_GOT_TLSLD)
00421     .Case("got@tlsld", VK_PPC_GOT_TLSLD)
00422     .Case("GOT@TLSLD@L", VK_PPC_GOT_TLSLD_LO)
00423     .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
00424     .Case("GOT@TLSLD@H", VK_PPC_GOT_TLSLD_HI)
00425     .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
00426     .Case("GOT@TLSLD@HA", VK_PPC_GOT_TLSLD_HA)
00427     .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
00428     .Case("NONE", VK_ARM_NONE)
00429     .Case("none", VK_ARM_NONE)
00430     .Case("TARGET1", VK_ARM_TARGET1)
00431     .Case("target1", VK_ARM_TARGET1)
00432     .Case("TARGET2", VK_ARM_TARGET2)
00433     .Case("target2", VK_ARM_TARGET2)
00434     .Case("PREL31", VK_ARM_PREL31)
00435     .Case("prel31", VK_ARM_PREL31)
00436     .Case("TLSLDO", VK_ARM_TLSLDO)
00437     .Case("tlsldo", VK_ARM_TLSLDO)
00438     .Case("TLSCALL", VK_ARM_TLSCALL)
00439     .Case("tlscall", VK_ARM_TLSCALL)
00440     .Case("TLSDESC", VK_ARM_TLSDESC)
00441     .Case("tlsdesc", VK_ARM_TLSDESC)
00442     .Default(VK_Invalid);
00443 }
00444 
00445 /* *** */
00446 
00447 void MCTargetExpr::anchor() {}
00448 
00449 /* *** */
00450 
00451 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
00452   return EvaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
00453 }
00454 
00455 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
00456                                 const MCAsmLayout &Layout) const {
00457   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
00458 }
00459 
00460 bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
00461                                 const MCAsmLayout &Layout,
00462                                 const SectionAddrMap &Addrs) const {
00463   return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
00464 }
00465 
00466 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
00467   return EvaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
00468 }
00469 
00470 int64_t MCExpr::evaluateKnownAbsolute(const MCAsmLayout &Layout) const {
00471   int64_t Res;
00472   bool Abs =
00473       evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, true);
00474   (void)Abs;
00475   assert(Abs && "Not actually absolute");
00476   return Res;
00477 }
00478 
00479 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
00480                                 const MCAsmLayout *Layout,
00481                                 const SectionAddrMap *Addrs) const {
00482   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
00483   // absolutize differences across sections and that is what the MachO writer
00484   // uses Addrs for.
00485   return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
00486 }
00487 
00488 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
00489                                 const MCAsmLayout *Layout,
00490                                 const SectionAddrMap *Addrs, bool InSet) const {
00491   MCValue Value;
00492 
00493   // Fast path constants.
00494   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
00495     Res = CE->getValue();
00496     return true;
00497   }
00498 
00499   bool IsRelocatable = EvaluateAsRelocatableImpl(
00500       Value, Asm, Layout, nullptr, Addrs, InSet, /*ForceVarExpansion*/ false);
00501 
00502   // Record the current value.
00503   Res = Value.getConstant();
00504 
00505   return IsRelocatable && Value.isAbsolute();
00506 }
00507 
00508 /// \brief Helper method for \see EvaluateSymbolAdd().
00509 static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
00510                                                 const MCAsmLayout *Layout,
00511                                                 const SectionAddrMap *Addrs,
00512                                                 bool InSet,
00513                                                 const MCSymbolRefExpr *&A,
00514                                                 const MCSymbolRefExpr *&B,
00515                                                 int64_t &Addend) {
00516   if (!A || !B)
00517     return;
00518 
00519   const MCSymbol &SA = A->getSymbol();
00520   const MCSymbol &SB = B->getSymbol();
00521 
00522   if (SA.isUndefined() || SB.isUndefined())
00523     return;
00524 
00525   if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
00526     return;
00527 
00528   const MCSymbolData &AD = Asm->getSymbolData(SA);
00529   const MCSymbolData &BD = Asm->getSymbolData(SB);
00530 
00531   if (AD.getFragment() == BD.getFragment()) {
00532     Addend += (AD.getOffset() - BD.getOffset());
00533 
00534     // Pointers to Thumb symbols need to have their low-bit set to allow
00535     // for interworking.
00536     if (Asm->isThumbFunc(&SA))
00537       Addend |= 1;
00538 
00539     // Clear the symbol expr pointers to indicate we have folded these
00540     // operands.
00541     A = B = nullptr;
00542     return;
00543   }
00544 
00545   if (!Layout)
00546     return;
00547 
00548   const MCSectionData &SecA = *AD.getFragment()->getParent();
00549   const MCSectionData &SecB = *BD.getFragment()->getParent();
00550 
00551   if ((&SecA != &SecB) && !Addrs)
00552     return;
00553 
00554   // Eagerly evaluate.
00555   Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
00556              Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
00557   if (Addrs && (&SecA != &SecB))
00558     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
00559 
00560   // Pointers to Thumb symbols need to have their low-bit set to allow
00561   // for interworking.
00562   if (Asm->isThumbFunc(&SA))
00563     Addend |= 1;
00564 
00565   // Clear the symbol expr pointers to indicate we have folded these
00566   // operands.
00567   A = B = nullptr;
00568 }
00569 
00570 /// \brief Evaluate the result of an add between (conceptually) two MCValues.
00571 ///
00572 /// This routine conceptually attempts to construct an MCValue:
00573 ///   Result = (Result_A - Result_B + Result_Cst)
00574 /// from two MCValue's LHS and RHS where
00575 ///   Result = LHS + RHS
00576 /// and
00577 ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
00578 ///
00579 /// This routine attempts to aggresively fold the operands such that the result
00580 /// is representable in an MCValue, but may not always succeed.
00581 ///
00582 /// \returns True on success, false if the result is not representable in an
00583 /// MCValue.
00584 
00585 /// NOTE: It is really important to have both the Asm and Layout arguments.
00586 /// They might look redundant, but this function can be used before layout
00587 /// is done (see the object streamer for example) and having the Asm argument
00588 /// lets us avoid relaxations early.
00589 static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
00590                                 const MCAsmLayout *Layout,
00591                                 const SectionAddrMap *Addrs,
00592                                 bool InSet,
00593                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
00594                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
00595                                 MCValue &Res) {
00596   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
00597   // about dealing with modifiers. This will ultimately bite us, one day.
00598   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
00599   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
00600   int64_t LHS_Cst = LHS.getConstant();
00601 
00602   // Fold the result constant immediately.
00603   int64_t Result_Cst = LHS_Cst + RHS_Cst;
00604 
00605   assert((!Layout || Asm) &&
00606          "Must have an assembler object if layout is given!");
00607 
00608   // If we have a layout, we can fold resolved differences.
00609   if (Asm) {
00610     // First, fold out any differences which are fully resolved. By
00611     // reassociating terms in
00612     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
00613     // we have the four possible differences:
00614     //   (LHS_A - LHS_B),
00615     //   (LHS_A - RHS_B),
00616     //   (RHS_A - LHS_B),
00617     //   (RHS_A - RHS_B).
00618     // Since we are attempting to be as aggressive as possible about folding, we
00619     // attempt to evaluate each possible alternative.
00620     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
00621                                         Result_Cst);
00622     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
00623                                         Result_Cst);
00624     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
00625                                         Result_Cst);
00626     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
00627                                         Result_Cst);
00628   }
00629 
00630   // We can't represent the addition or subtraction of two symbols.
00631   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
00632     return false;
00633 
00634   // At this point, we have at most one additive symbol and one subtractive
00635   // symbol -- find them.
00636   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
00637   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
00638 
00639   // If we have a negated symbol, then we must have also have a non-negated
00640   // symbol in order to encode the expression.
00641   if (B && !A)
00642     return false;
00643 
00644   Res = MCValue::get(A, B, Result_Cst);
00645   return true;
00646 }
00647 
00648 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
00649                                    const MCAsmLayout *Layout,
00650                                    const MCFixup *Fixup) const {
00651   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
00652   return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
00653                                    false, /*ForceVarExpansion*/ false);
00654 }
00655 
00656 bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
00657                              const MCFixup *Fixup) const {
00658   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
00659   return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
00660                                    false, /*ForceVarExpansion*/ true);
00661 }
00662 
00663 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
00664                                        const MCAsmLayout *Layout,
00665                                        const MCFixup *Fixup,
00666                                        const SectionAddrMap *Addrs, bool InSet,
00667                                        bool ForceVarExpansion) const {
00668   ++stats::MCExprEvaluate;
00669 
00670   switch (getKind()) {
00671   case Target:
00672     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
00673                                                                Fixup);
00674 
00675   case Constant:
00676     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
00677     return true;
00678 
00679   case SymbolRef: {
00680     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
00681     const MCSymbol &Sym = SRE->getSymbol();
00682     const MCAsmInfo &MCAsmInfo = SRE->getMCAsmInfo();
00683 
00684     // Evaluate recursively if this is a variable.
00685     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
00686       if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
00687               Res, Asm, Layout, Fixup, Addrs, true, ForceVarExpansion)) {
00688         const MCSymbolRefExpr *A = Res.getSymA();
00689         const MCSymbolRefExpr *B = Res.getSymB();
00690 
00691         if (MCAsmInfo.hasSubsectionsViaSymbols()) {
00692           // FIXME: This is small hack. Given
00693           // a = b + 4
00694           // .long a
00695           // the OS X assembler will completely drop the 4. We should probably
00696           // include it in the relocation or produce an error if that is not
00697           // possible.
00698           if (!A && !B)
00699             return true;
00700         } else {
00701           if (ForceVarExpansion)
00702             return true;
00703           bool IsSymbol = A && A->getSymbol().isDefined();
00704           if (!IsSymbol)
00705             return true;
00706         }
00707       }
00708     }
00709 
00710     Res = MCValue::get(SRE, nullptr, 0);
00711     return true;
00712   }
00713 
00714   case Unary: {
00715     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
00716     MCValue Value;
00717 
00718     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
00719                                                       Fixup, Addrs, InSet,
00720                                                       ForceVarExpansion))
00721       return false;
00722 
00723     switch (AUE->getOpcode()) {
00724     case MCUnaryExpr::LNot:
00725       if (!Value.isAbsolute())
00726         return false;
00727       Res = MCValue::get(!Value.getConstant());
00728       break;
00729     case MCUnaryExpr::Minus:
00730       /// -(a - b + const) ==> (b - a - const)
00731       if (Value.getSymA() && !Value.getSymB())
00732         return false;
00733       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
00734                          -Value.getConstant());
00735       break;
00736     case MCUnaryExpr::Not:
00737       if (!Value.isAbsolute())
00738         return false;
00739       Res = MCValue::get(~Value.getConstant());
00740       break;
00741     case MCUnaryExpr::Plus:
00742       Res = Value;
00743       break;
00744     }
00745 
00746     return true;
00747   }
00748 
00749   case Binary: {
00750     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
00751     MCValue LHSValue, RHSValue;
00752 
00753     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
00754                                                   Fixup, Addrs, InSet,
00755                                                   ForceVarExpansion) ||
00756         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
00757                                                   Fixup, Addrs, InSet,
00758                                                   ForceVarExpansion))
00759       return false;
00760 
00761     // We only support a few operations on non-constant expressions, handle
00762     // those first.
00763     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
00764       switch (ABE->getOpcode()) {
00765       default:
00766         return false;
00767       case MCBinaryExpr::Sub:
00768         // Negate RHS and add.
00769         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
00770                                    RHSValue.getSymB(), RHSValue.getSymA(),
00771                                    -RHSValue.getConstant(),
00772                                    Res);
00773 
00774       case MCBinaryExpr::Add:
00775         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
00776                                    RHSValue.getSymA(), RHSValue.getSymB(),
00777                                    RHSValue.getConstant(),
00778                                    Res);
00779       }
00780     }
00781 
00782     // FIXME: We need target hooks for the evaluation. It may be limited in
00783     // width, and gas defines the result of comparisons and right shifts
00784     // differently from Apple as.
00785     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
00786     int64_t Result = 0;
00787     switch (ABE->getOpcode()) {
00788     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
00789     case MCBinaryExpr::And:  Result = LHS & RHS; break;
00790     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
00791     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
00792     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
00793     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
00794     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
00795     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
00796     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
00797     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
00798     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
00799     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
00800     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
00801     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
00802     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
00803     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
00804     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
00805     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
00806     }
00807 
00808     Res = MCValue::get(Result);
00809     return true;
00810   }
00811   }
00812 
00813   llvm_unreachable("Invalid assembly expression kind!");
00814 }
00815 
00816 const MCSection *MCExpr::FindAssociatedSection() const {
00817   switch (getKind()) {
00818   case Target:
00819     // We never look through target specific expressions.
00820     return cast<MCTargetExpr>(this)->FindAssociatedSection();
00821 
00822   case Constant:
00823     return MCSymbol::AbsolutePseudoSection;
00824 
00825   case SymbolRef: {
00826     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
00827     const MCSymbol &Sym = SRE->getSymbol();
00828 
00829     if (Sym.isDefined())
00830       return &Sym.getSection();
00831 
00832     return nullptr;
00833   }
00834 
00835   case Unary:
00836     return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
00837 
00838   case Binary: {
00839     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
00840     const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
00841     const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
00842 
00843     // If either section is absolute, return the other.
00844     if (LHS_S == MCSymbol::AbsolutePseudoSection)
00845       return RHS_S;
00846     if (RHS_S == MCSymbol::AbsolutePseudoSection)
00847       return LHS_S;
00848 
00849     // Otherwise, return the first non-null section.
00850     return LHS_S ? LHS_S : RHS_S;
00851   }
00852   }
00853 
00854   llvm_unreachable("Invalid assembly expression kind!");
00855 }