LCOV - code coverage report
Current view: top level - lib/MC - MCExpr.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 390 433 90.1 %
Date: 2018-02-23 15:42:53 Functions: 24 25 96.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/Casting.h"
      21             : #include "llvm/Support/Compiler.h"
      22             : #include "llvm/Support/Debug.h"
      23             : #include "llvm/Support/ErrorHandling.h"
      24             : #include "llvm/Support/raw_ostream.h"
      25             : #include <cassert>
      26             : #include <cstdint>
      27             : 
      28             : using namespace llvm;
      29             : 
      30             : #define DEBUG_TYPE "mcexpr"
      31             : 
      32             : namespace {
      33             : namespace stats {
      34             : 
      35             : STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
      36             : 
      37             : } // end namespace stats
      38             : } // end anonymous namespace
      39             : 
      40     1075804 : void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
      41     1075804 :   switch (getKind()) {
      42             :   case MCExpr::Target:
      43       46794 :     return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
      44             :   case MCExpr::Constant:
      45      428426 :     OS << cast<MCConstantExpr>(*this).getValue();
      46      428426 :     return;
      47             : 
      48             :   case MCExpr::SymbolRef: {
      49             :     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
      50      441361 :     const MCSymbol &Sym = SRE.getSymbol();
      51             :     // Parenthesize names that start with $ so that they don't look like
      52             :     // absolute names.
      53             :     bool UseParens =
      54     1268521 :         !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
      55             :     if (UseParens) {
      56             :       OS << '(';
      57        8932 :       Sym.print(OS, MAI);
      58             :       OS << ')';
      59             :     } else
      60      432429 :       Sym.print(OS, MAI);
      61             : 
      62      441361 :     if (SRE.getKind() != MCSymbolRefExpr::VK_None)
      63       15838 :       SRE.printVariantKind(OS);
      64             : 
      65             :     return;
      66             :   }
      67             : 
      68             :   case MCExpr::Unary: {
      69             :     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
      70           1 :     switch (UE.getOpcode()) {
      71             :     case MCUnaryExpr::LNot:  OS << '!'; break;
      72             :     case MCUnaryExpr::Minus: OS << '-'; break;
      73             :     case MCUnaryExpr::Not:   OS << '~'; break;
      74             :     case MCUnaryExpr::Plus:  OS << '+'; break;
      75             :     }
      76           1 :     UE.getSubExpr()->print(OS, MAI);
      77           1 :     return;
      78             :   }
      79             : 
      80             :   case MCExpr::Binary: {
      81             :     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
      82             : 
      83             :     // Only print parens around the LHS if it is non-trivial.
      84      318444 :     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
      85      158272 :       BE.getLHS()->print(OS, MAI);
      86             :     } else {
      87             :       OS << '(';
      88         950 :       BE.getLHS()->print(OS, MAI);
      89             :       OS << ')';
      90             :     }
      91             : 
      92      159222 :     switch (BE.getOpcode()) {
      93       10711 :     case MCBinaryExpr::Add:
      94             :       // Print "X-42" instead of "X+-42".
      95       10711 :       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
      96       10486 :         if (RHSC->getValue() < 0) {
      97        1226 :           OS << RHSC->getValue();
      98        1226 :           return;
      99             :         }
     100             :       }
     101             : 
     102             :       OS <<  '+';
     103             :       break;
     104           0 :     case MCBinaryExpr::AShr: OS << ">>"; break;
     105             :     case MCBinaryExpr::And:  OS <<  '&'; break;
     106             :     case MCBinaryExpr::Div:  OS <<  '/'; break;
     107           0 :     case MCBinaryExpr::EQ:   OS << "=="; break;
     108             :     case MCBinaryExpr::GT:   OS <<  '>'; break;
     109           0 :     case MCBinaryExpr::GTE:  OS << ">="; break;
     110           0 :     case MCBinaryExpr::LAnd: OS << "&&"; break;
     111           0 :     case MCBinaryExpr::LOr:  OS << "||"; break;
     112           2 :     case MCBinaryExpr::LShr: OS << ">>"; break;
     113             :     case MCBinaryExpr::LT:   OS <<  '<'; break;
     114           0 :     case MCBinaryExpr::LTE:  OS << "<="; break;
     115             :     case MCBinaryExpr::Mod:  OS <<  '%'; break;
     116             :     case MCBinaryExpr::Mul:  OS <<  '*'; break;
     117           0 :     case MCBinaryExpr::NE:   OS << "!="; break;
     118             :     case MCBinaryExpr::Or:   OS <<  '|'; break;
     119           0 :     case MCBinaryExpr::Shl:  OS << "<<"; break;
     120             :     case MCBinaryExpr::Sub:  OS <<  '-'; break;
     121             :     case MCBinaryExpr::Xor:  OS <<  '^'; break;
     122             :     }
     123             : 
     124             :     // Only print parens around the LHS if it is non-trivial.
     125      315992 :     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
     126      155848 :       BE.getRHS()->print(OS, MAI);
     127             :     } else {
     128             :       OS << '(';
     129        2148 :       BE.getRHS()->print(OS, MAI);
     130             :       OS << ')';
     131             :     }
     132             :     return;
     133             :   }
     134             :   }
     135             : 
     136           0 :   llvm_unreachable("Invalid expression kind!");
     137             : }
     138             : 
     139             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     140             : LLVM_DUMP_METHOD void MCExpr::dump() const {
     141             :   dbgs() << *this;
     142             :   dbgs() << '\n';
     143             : }
     144             : #endif
     145             : 
     146             : /* *** */
     147             : 
     148     1676195 : const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
     149             :                                          const MCExpr *RHS, MCContext &Ctx,
     150             :                                          SMLoc Loc) {
     151     3352390 :   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
     152             : }
     153             : 
     154       17944 : const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
     155             :                                        MCContext &Ctx, SMLoc Loc) {
     156       35888 :   return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
     157             : }
     158             : 
     159     2194501 : const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx) {
     160     2194501 :   return new (Ctx) MCConstantExpr(Value);
     161             : }
     162             : 
     163             : /* *** */
     164             : 
     165     3343799 : MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
     166     3343799 :                                  const MCAsmInfo *MAI, SMLoc Loc)
     167             :     : MCExpr(MCExpr::SymbolRef, Loc), Kind(Kind),
     168     3343799 :       UseParensForSymbolVariant(MAI->useParensForSymbolVariant()),
     169     3343799 :       HasSubsectionsViaSymbols(MAI->hasSubsectionsViaSymbols()),
     170    10031397 :       Symbol(Symbol) {
     171             :   assert(Symbol);
     172     3343799 : }
     173             : 
     174     3343799 : const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
     175             :                                                VariantKind Kind,
     176             :                                                MCContext &Ctx, SMLoc Loc) {
     177     6687598 :   return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
     178             : }
     179             : 
     180          22 : const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
     181             :                                                MCContext &Ctx) {
     182          44 :   return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
     183             : }
     184             : 
     185       15842 : StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
     186       15842 :   switch (Kind) {
     187           0 :   case VK_Invalid: return "<<invalid>>";
     188           0 :   case VK_None: return "<<none>>";
     189             : 
     190          28 :   case VK_DTPOFF: return "DTPOFF";
     191          12 :   case VK_DTPREL: return "DTPREL";
     192         134 :   case VK_GOT: return "GOT";
     193         131 :   case VK_GOTOFF: return "GOTOFF";
     194           0 :   case VK_GOTREL: return "GOTREL";
     195        1015 :   case VK_GOTPCREL: return "GOTPCREL";
     196          37 :   case VK_GOTTPOFF: return "GOTTPOFF";
     197          11 :   case VK_INDNTPOFF: return "INDNTPOFF";
     198          28 :   case VK_NTPOFF: return "NTPOFF";
     199           5 :   case VK_GOTNTPOFF: return "GOTNTPOFF";
     200        2470 :   case VK_PLT: return "PLT";
     201          47 :   case VK_TLSGD: return "TLSGD";
     202           7 :   case VK_TLSLD: return "TLSLD";
     203          15 :   case VK_TLSLDM: return "TLSLDM";
     204          47 :   case VK_TPOFF: return "TPOFF";
     205          14 :   case VK_TPREL: return "TPREL";
     206           0 :   case VK_TLSCALL: return "tlscall";
     207           1 :   case VK_TLSDESC: return "tlsdesc";
     208          48 :   case VK_TLVP: return "TLVP";
     209          23 :   case VK_TLVPPAGE: return "TLVPPAGE";
     210          24 :   case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
     211         205 :   case VK_PAGE: return "PAGE";
     212         345 :   case VK_PAGEOFF: return "PAGEOFF";
     213         177 :   case VK_GOTPAGE: return "GOTPAGE";
     214         177 :   case VK_GOTPAGEOFF: return "GOTPAGEOFF";
     215          74 :   case VK_SECREL: return "SECREL32";
     216           0 :   case VK_SIZE: return "SIZE";
     217           0 :   case VK_WEAKREF: return "WEAKREF";
     218           4 :   case VK_X86_ABS8: return "ABS8";
     219           0 :   case VK_ARM_NONE: return "none";
     220          48 :   case VK_ARM_GOT_PREL: return "GOT_PREL";
     221           8 :   case VK_ARM_TARGET1: return "target1";
     222           4 :   case VK_ARM_TARGET2: return "target2";
     223           2 :   case VK_ARM_PREL31: return "prel31";
     224          54 :   case VK_ARM_SBREL: return "sbrel";
     225           1 :   case VK_ARM_TLSLDO: return "tlsldo";
     226           0 :   case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
     227           0 :   case VK_AVR_NONE: return "none";
     228           0 :   case VK_AVR_LO8: return "lo8";
     229           0 :   case VK_AVR_HI8: return "hi8";
     230           0 :   case VK_AVR_HLO8: return "hlo8";
     231           0 :   case VK_AVR_DIFF8: return "diff8";
     232           0 :   case VK_AVR_DIFF16: return "diff16";
     233           0 :   case VK_AVR_DIFF32: return "diff32";
     234           1 :   case VK_PPC_LO: return "l";
     235           0 :   case VK_PPC_HI: return "h";
     236           1 :   case VK_PPC_HA: return "ha";
     237           0 :   case VK_PPC_HIGHER: return "higher";
     238           0 :   case VK_PPC_HIGHERA: return "highera";
     239           0 :   case VK_PPC_HIGHEST: return "highest";
     240           0 :   case VK_PPC_HIGHESTA: return "highesta";
     241          24 :   case VK_PPC_GOT_LO: return "got@l";
     242           8 :   case VK_PPC_GOT_HI: return "got@h";
     243           8 :   case VK_PPC_GOT_HA: return "got@ha";
     244        4253 :   case VK_PPC_TOCBASE: return "tocbase";
     245          16 :   case VK_PPC_TOC: return "toc";
     246        2298 :   case VK_PPC_TOC_LO: return "toc@l";
     247           4 :   case VK_PPC_TOC_HI: return "toc@h";
     248        2203 :   case VK_PPC_TOC_HA: return "toc@ha";
     249           2 :   case VK_PPC_DTPMOD: return "dtpmod";
     250          18 :   case VK_PPC_TPREL_LO: return "tprel@l";
     251           4 :   case VK_PPC_TPREL_HI: return "tprel@h";
     252          13 :   case VK_PPC_TPREL_HA: return "tprel@ha";
     253           4 :   case VK_PPC_TPREL_HIGHER: return "tprel@higher";
     254           4 :   case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
     255           4 :   case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
     256           4 :   case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
     257          17 :   case VK_PPC_DTPREL_LO: return "dtprel@l";
     258           4 :   case VK_PPC_DTPREL_HI: return "dtprel@h";
     259          11 :   case VK_PPC_DTPREL_HA: return "dtprel@ha";
     260           4 :   case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
     261           4 :   case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
     262           4 :   case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
     263           4 :   case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
     264           8 :   case VK_PPC_GOT_TPREL: return "got@tprel";
     265          11 :   case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
     266           4 :   case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
     267           6 :   case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
     268           8 :   case VK_PPC_GOT_DTPREL: return "got@dtprel";
     269           8 :   case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
     270           4 :   case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
     271           4 :   case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
     272           7 :   case VK_PPC_TLS: return "tls";
     273           6 :   case VK_PPC_GOT_TLSGD: return "got@tlsgd";
     274          15 :   case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
     275           4 :   case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
     276          15 :   case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
     277          17 :   case VK_PPC_TLSGD: return "tlsgd";
     278           6 :   case VK_PPC_GOT_TLSLD: return "got@tlsld";
     279           9 :   case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
     280           4 :   case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
     281           9 :   case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
     282          11 :   case VK_PPC_TLSLD: return "tlsld";
     283           1 :   case VK_PPC_LOCAL: return "local";
     284         559 :   case VK_COFF_IMGREL32: return "IMGREL";
     285          30 :   case VK_Hexagon_PCREL: return "PCREL";
     286           0 :   case VK_Hexagon_LO16: return "LO16";
     287           0 :   case VK_Hexagon_HI16: return "HI16";
     288           0 :   case VK_Hexagon_GPREL: return "GPREL";
     289          12 :   case VK_Hexagon_GD_GOT: return "GDGOT";
     290           0 :   case VK_Hexagon_LD_GOT: return "LDGOT";
     291          11 :   case VK_Hexagon_GD_PLT: return "GDPLT";
     292           0 :   case VK_Hexagon_LD_PLT: return "LDPLT";
     293           4 :   case VK_Hexagon_IE: return "IE";
     294           4 :   case VK_Hexagon_IE_GOT: return "IEGOT";
     295           0 :   case VK_WebAssembly_FUNCTION: return "FUNCTION";
     296           0 :   case VK_WebAssembly_TYPEINDEX: return "TYPEINDEX";
     297          19 :   case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo";
     298          19 :   case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi";
     299         457 :   case VK_AMDGPU_REL32_LO: return "rel32@lo";
     300         457 :   case VK_AMDGPU_REL32_HI: return "rel32@hi";
     301             :   }
     302           0 :   llvm_unreachable("Invalid variant kind");
     303             : }
     304             : 
     305             : MCSymbolRefExpr::VariantKind
     306        1515 : MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
     307        3030 :   return StringSwitch<VariantKind>(Name.lower())
     308        3030 :     .Case("dtprel", VK_DTPREL)
     309        3030 :     .Case("dtpoff", VK_DTPOFF)
     310        3030 :     .Case("got", VK_GOT)
     311        3030 :     .Case("gotoff", VK_GOTOFF)
     312        3030 :     .Case("gotrel", VK_GOTREL)
     313        3030 :     .Case("gotpcrel", VK_GOTPCREL)
     314        3030 :     .Case("gottpoff", VK_GOTTPOFF)
     315        3030 :     .Case("indntpoff", VK_INDNTPOFF)
     316        3030 :     .Case("ntpoff", VK_NTPOFF)
     317        3030 :     .Case("gotntpoff", VK_GOTNTPOFF)
     318        3030 :     .Case("plt", VK_PLT)
     319        3030 :     .Case("tlscall", VK_TLSCALL)
     320        3030 :     .Case("tlsdesc", VK_TLSDESC)
     321        3030 :     .Case("tlsgd", VK_TLSGD)
     322        3030 :     .Case("tlsld", VK_TLSLD)
     323        3030 :     .Case("tlsldm", VK_TLSLDM)
     324        3030 :     .Case("tpoff", VK_TPOFF)
     325        3030 :     .Case("tprel", VK_TPREL)
     326        3030 :     .Case("tlvp", VK_TLVP)
     327        3030 :     .Case("tlvppage", VK_TLVPPAGE)
     328        3030 :     .Case("tlvppageoff", VK_TLVPPAGEOFF)
     329        3030 :     .Case("page", VK_PAGE)
     330        3030 :     .Case("pageoff", VK_PAGEOFF)
     331        3030 :     .Case("gotpage", VK_GOTPAGE)
     332        3030 :     .Case("gotpageoff", VK_GOTPAGEOFF)
     333        3030 :     .Case("imgrel", VK_COFF_IMGREL32)
     334        3030 :     .Case("secrel32", VK_SECREL)
     335        3030 :     .Case("size", VK_SIZE)
     336        3030 :     .Case("abs8", VK_X86_ABS8)
     337        3030 :     .Case("l", VK_PPC_LO)
     338        3030 :     .Case("h", VK_PPC_HI)
     339        3030 :     .Case("ha", VK_PPC_HA)
     340        3030 :     .Case("higher", VK_PPC_HIGHER)
     341        3030 :     .Case("highera", VK_PPC_HIGHERA)
     342        3030 :     .Case("highest", VK_PPC_HIGHEST)
     343        3030 :     .Case("highesta", VK_PPC_HIGHESTA)
     344        3030 :     .Case("got@l", VK_PPC_GOT_LO)
     345        3030 :     .Case("got@h", VK_PPC_GOT_HI)
     346        3030 :     .Case("got@ha", VK_PPC_GOT_HA)
     347        3030 :     .Case("local", VK_PPC_LOCAL)
     348        3030 :     .Case("tocbase", VK_PPC_TOCBASE)
     349        3030 :     .Case("toc", VK_PPC_TOC)
     350        3030 :     .Case("toc@l", VK_PPC_TOC_LO)
     351        3030 :     .Case("toc@h", VK_PPC_TOC_HI)
     352        3030 :     .Case("toc@ha", VK_PPC_TOC_HA)
     353        3030 :     .Case("tls", VK_PPC_TLS)
     354        3030 :     .Case("dtpmod", VK_PPC_DTPMOD)
     355        3030 :     .Case("tprel@l", VK_PPC_TPREL_LO)
     356        3030 :     .Case("tprel@h", VK_PPC_TPREL_HI)
     357        3030 :     .Case("tprel@ha", VK_PPC_TPREL_HA)
     358        3030 :     .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
     359        3030 :     .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
     360        3030 :     .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
     361        3030 :     .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
     362        3030 :     .Case("dtprel@l", VK_PPC_DTPREL_LO)
     363        3030 :     .Case("dtprel@h", VK_PPC_DTPREL_HI)
     364        3030 :     .Case("dtprel@ha", VK_PPC_DTPREL_HA)
     365        3030 :     .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
     366        3030 :     .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
     367        3030 :     .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
     368        3030 :     .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
     369        3030 :     .Case("got@tprel", VK_PPC_GOT_TPREL)
     370        3030 :     .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
     371        3030 :     .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
     372        3030 :     .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
     373        3030 :     .Case("got@dtprel", VK_PPC_GOT_DTPREL)
     374        3030 :     .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
     375        3030 :     .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
     376        3030 :     .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
     377        3030 :     .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
     378        3030 :     .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
     379        3030 :     .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
     380        3030 :     .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
     381        3030 :     .Case("got@tlsld", VK_PPC_GOT_TLSLD)
     382        3030 :     .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
     383        3030 :     .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
     384        3030 :     .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
     385        3030 :     .Case("gdgot", VK_Hexagon_GD_GOT)
     386        3030 :     .Case("gdplt", VK_Hexagon_GD_PLT)
     387        3030 :     .Case("iegot", VK_Hexagon_IE_GOT)
     388        3030 :     .Case("ie", VK_Hexagon_IE)
     389        3030 :     .Case("ldgot", VK_Hexagon_LD_GOT)
     390        3030 :     .Case("ldplt", VK_Hexagon_LD_PLT)
     391        3030 :     .Case("pcrel", VK_Hexagon_PCREL)
     392        3030 :     .Case("none", VK_ARM_NONE)
     393        3030 :     .Case("got_prel", VK_ARM_GOT_PREL)
     394        3030 :     .Case("target1", VK_ARM_TARGET1)
     395        3030 :     .Case("target2", VK_ARM_TARGET2)
     396        3030 :     .Case("prel31", VK_ARM_PREL31)
     397        3030 :     .Case("sbrel", VK_ARM_SBREL)
     398        3030 :     .Case("tlsldo", VK_ARM_TLSLDO)
     399        3030 :     .Case("lo8", VK_AVR_LO8)
     400        3030 :     .Case("hi8", VK_AVR_HI8)
     401        3030 :     .Case("hlo8", VK_AVR_HLO8)
     402        3030 :     .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO)
     403        3030 :     .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI)
     404        3030 :     .Case("rel32@lo", VK_AMDGPU_REL32_LO)
     405        3030 :     .Case("rel32@hi", VK_AMDGPU_REL32_HI)
     406        1515 :     .Default(VK_Invalid);
     407             : }
     408             : 
     409       15838 : void MCSymbolRefExpr::printVariantKind(raw_ostream &OS) const {
     410       15838 :   if (UseParensForSymbolVariant)
     411         176 :     OS << '(' << MCSymbolRefExpr::getVariantKindName(getKind()) << ')';
     412             :   else
     413       15662 :     OS << '@' << MCSymbolRefExpr::getVariantKindName(getKind());
     414       15838 : }
     415             : 
     416             : /* *** */
     417             : 
     418           0 : void MCTargetExpr::anchor() {}
     419             : 
     420             : /* *** */
     421             : 
     422      832618 : bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
     423      832618 :   return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr);
     424             : }
     425             : 
     426       55656 : bool MCExpr::evaluateAsAbsolute(int64_t &Res,
     427             :                                 const MCAsmLayout &Layout) const {
     428       55656 :   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr);
     429             : }
     430             : 
     431         300 : bool MCExpr::evaluateAsAbsolute(int64_t &Res,
     432             :                                 const MCAsmLayout &Layout,
     433             :                                 const SectionAddrMap &Addrs) const {
     434         300 :   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
     435             : }
     436             : 
     437     1377854 : bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
     438     1377854 :   return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr);
     439             : }
     440             : 
     441      513314 : bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
     442             :                                    const MCAsmLayout &Layout) const {
     443      513314 :   return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
     444      513314 :                             true);
     445             : }
     446             : 
     447     2266428 : bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
     448             :                                 const MCAsmLayout *Layout,
     449             :                                 const SectionAddrMap *Addrs) const {
     450             :   // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
     451             :   // absolutize differences across sections and that is what the MachO writer
     452             :   // uses Addrs for.
     453     2266428 :   return evaluateAsAbsolute(Res, Asm, Layout, Addrs, Addrs);
     454             : }
     455             : 
     456     2779742 : bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
     457             :                                 const MCAsmLayout *Layout,
     458             :                                 const SectionAddrMap *Addrs, bool InSet) const {
     459     2779742 :   MCValue Value;
     460             : 
     461             :   // Fast path constants.
     462             :   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
     463      806082 :     Res = CE->getValue();
     464      806082 :     return true;
     465             :   }
     466             : 
     467             :   bool IsRelocatable =
     468     1973660 :       evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
     469             : 
     470             :   // Record the current value.
     471     1973660 :   Res = Value.getConstant();
     472             : 
     473     1973660 :   return IsRelocatable && Value.isAbsolute();
     474             : }
     475             : 
     476             : /// \brief Helper method for \see EvaluateSymbolAdd().
     477     8728876 : static void AttemptToFoldSymbolOffsetDifference(
     478             :     const MCAssembler *Asm, const MCAsmLayout *Layout,
     479             :     const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
     480             :     const MCSymbolRefExpr *&B, int64_t &Addend) {
     481     8728876 :   if (!A || !B)
     482             :     return;
     483             : 
     484     1261813 :   const MCSymbol &SA = A->getSymbol();
     485     1261813 :   const MCSymbol &SB = B->getSymbol();
     486             : 
     487     2495935 :   if (SA.isUndefined() || SB.isUndefined())
     488             :     return;
     489             : 
     490     1234094 :   if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
     491             :     return;
     492             : 
     493     2393412 :   if (SA.getFragment() == SB.getFragment() && !SA.isVariable() &&
     494             :       !SB.isVariable()) {
     495      589358 :     Addend += (SA.getOffset() - SB.getOffset());
     496             : 
     497             :     // Pointers to Thumb symbols need to have their low-bit set to allow
     498             :     // for interworking.
     499      589358 :     if (Asm->isThumbFunc(&SA))
     500           0 :       Addend |= 1;
     501             : 
     502             :     // Clear the symbol expr pointers to indicate we have folded these
     503             :     // operands.
     504      589358 :     A = B = nullptr;
     505      589358 :     return;
     506             :   }
     507             : 
     508      625246 :   if (!Layout)
     509             :     return;
     510             : 
     511      483534 :   const MCSection &SecA = *SA.getFragment()->getParent();
     512      483534 :   const MCSection &SecB = *SB.getFragment()->getParent();
     513             : 
     514      483534 :   if ((&SecA != &SecB) && !Addrs)
     515             :     return;
     516             : 
     517             :   // Eagerly evaluate.
     518      965888 :   Addend += Layout->getSymbolOffset(A->getSymbol()) -
     519      482944 :             Layout->getSymbolOffset(B->getSymbol());
     520      482944 :   if (Addrs && (&SecA != &SecB))
     521         295 :     Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
     522             : 
     523             :   // Pointers to Thumb symbols need to have their low-bit set to allow
     524             :   // for interworking.
     525      482944 :   if (Asm->isThumbFunc(&SA))
     526           0 :     Addend |= 1;
     527             : 
     528             :   // Clear the symbol expr pointers to indicate we have folded these
     529             :   // operands.
     530      482944 :   A = B = nullptr;
     531             : }
     532             : 
     533             : /// \brief Evaluate the result of an add between (conceptually) two MCValues.
     534             : ///
     535             : /// This routine conceptually attempts to construct an MCValue:
     536             : ///   Result = (Result_A - Result_B + Result_Cst)
     537             : /// from two MCValue's LHS and RHS where
     538             : ///   Result = LHS + RHS
     539             : /// and
     540             : ///   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
     541             : ///
     542             : /// This routine attempts to aggresively fold the operands such that the result
     543             : /// is representable in an MCValue, but may not always succeed.
     544             : ///
     545             : /// \returns True on success, false if the result is not representable in an
     546             : /// MCValue.
     547             : 
     548             : /// NOTE: It is really important to have both the Asm and Layout arguments.
     549             : /// They might look redundant, but this function can be used before layout
     550             : /// is done (see the object streamer for example) and having the Asm argument
     551             : /// lets us avoid relaxations early.
     552             : static bool
     553     2193947 : EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
     554             :                     const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
     555             :                     const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
     556             :                     int64_t RHS_Cst, MCValue &Res) {
     557             :   // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
     558             :   // about dealing with modifiers. This will ultimately bite us, one day.
     559     2193947 :   const MCSymbolRefExpr *LHS_A = LHS.getSymA();
     560     2193947 :   const MCSymbolRefExpr *LHS_B = LHS.getSymB();
     561     2193947 :   int64_t LHS_Cst = LHS.getConstant();
     562             : 
     563             :   // Fold the result constant immediately.
     564     2193947 :   int64_t Result_Cst = LHS_Cst + RHS_Cst;
     565             : 
     566             :   assert((!Layout || Asm) &&
     567             :          "Must have an assembler object if layout is given!");
     568             : 
     569             :   // If we have a layout, we can fold resolved differences.
     570     2193947 :   if (Asm) {
     571             :     // First, fold out any differences which are fully resolved. By
     572             :     // reassociating terms in
     573             :     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
     574             :     // we have the four possible differences:
     575             :     //   (LHS_A - LHS_B),
     576             :     //   (LHS_A - RHS_B),
     577             :     //   (RHS_A - LHS_B),
     578             :     //   (RHS_A - RHS_B).
     579             :     // Since we are attempting to be as aggressive as possible about folding, we
     580             :     // attempt to evaluate each possible alternative.
     581     2182219 :     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
     582             :                                         Result_Cst);
     583     2182219 :     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
     584             :                                         Result_Cst);
     585     2182219 :     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
     586             :                                         Result_Cst);
     587     2182219 :     AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
     588             :                                         Result_Cst);
     589             :   }
     590             : 
     591             :   // We can't represent the addition or subtraction of two symbols.
     592     2193947 :   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
     593             :     return false;
     594             : 
     595             :   // At this point, we have at most one additive symbol and one subtractive
     596             :   // symbol -- find them.
     597     2193859 :   const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
     598     2193859 :   const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
     599             : 
     600     2193859 :   Res = MCValue::get(A, B, Result_Cst);
     601     2193859 :   return true;
     602             : }
     603             : 
     604     1697253 : bool MCExpr::evaluateAsRelocatable(MCValue &Res,
     605             :                                    const MCAsmLayout *Layout,
     606             :                                    const MCFixup *Fixup) const {
     607     1697253 :   MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
     608             :   return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
     609     1697253 :                                    false);
     610             : }
     611             : 
     612        1616 : bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
     613        1616 :   MCAssembler *Assembler = &Layout.getAssembler();
     614             :   return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
     615        1616 :                                    true);
     616             : }
     617             : 
     618        8867 : static bool canExpand(const MCSymbol &Sym, bool InSet) {
     619             :   const MCExpr *Expr = Sym.getVariableValue();
     620             :   const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
     621             :   if (Inner) {
     622         357 :     if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
     623             :       return false;
     624             :   }
     625             : 
     626        8838 :   if (InSet)
     627             :     return true;
     628        8808 :   return !Sym.isInSection();
     629             : }
     630             : 
     631     8140529 : bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
     632             :                                        const MCAsmLayout *Layout,
     633             :                                        const MCFixup *Fixup,
     634             :                                        const SectionAddrMap *Addrs,
     635             :                                        bool InSet) const {
     636             :   ++stats::MCExprEvaluate;
     637             : 
     638     8140529 :   switch (getKind()) {
     639             :   case Target:
     640             :     return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
     641       83949 :                                                                Fixup);
     642             : 
     643             :   case Constant:
     644     1028144 :     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
     645     1028144 :     return true;
     646             : 
     647             :   case SymbolRef: {
     648             :     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
     649     4789679 :     const MCSymbol &Sym = SRE->getSymbol();
     650             : 
     651             :     // Evaluate recursively if this is a variable.
     652     4798546 :     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None &&
     653        8867 :         canExpand(Sym, InSet)) {
     654             :       bool IsMachO = SRE->hasSubsectionsViaSymbols();
     655        8538 :       if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
     656        8538 :               Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
     657        8538 :         if (!IsMachO)
     658             :           return true;
     659             : 
     660        8267 :         const MCSymbolRefExpr *A = Res.getSymA();
     661        8267 :         const MCSymbolRefExpr *B = Res.getSymB();
     662             :         // FIXME: This is small hack. Given
     663             :         // a = b + 4
     664             :         // .long a
     665             :         // the OS X assembler will completely drop the 4. We should probably
     666             :         // include it in the relocation or produce an error if that is not
     667             :         // possible.
     668             :         // Allow constant expressions.
     669        8267 :         if (!A && !B)
     670             :           return true;
     671             :         // Allows aliases with zero offset.
     672        4288 :         if (Res.getConstant() == 0 && (!A || !B))
     673             :           return true;
     674             :       }
     675             :     }
     676             : 
     677     4785416 :     Res = MCValue::get(SRE, nullptr, 0);
     678     4785416 :     return true;
     679             :   }
     680             : 
     681             :   case Unary: {
     682             :     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
     683       17971 :     MCValue Value;
     684             : 
     685       17971 :     if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
     686             :                                                       Addrs, InSet))
     687             :       return false;
     688             : 
     689       17971 :     switch (AUE->getOpcode()) {
     690           4 :     case MCUnaryExpr::LNot:
     691           4 :       if (!Value.isAbsolute())
     692             :         return false;
     693           4 :       Res = MCValue::get(!Value.getConstant());
     694           4 :       break;
     695       17820 :     case MCUnaryExpr::Minus:
     696             :       /// -(a - b + const) ==> (b - a - const)
     697       17820 :       if (Value.getSymA() && !Value.getSymB())
     698             :         return false;
     699             : 
     700             :       // The cast avoids undefined behavior if the constant is INT64_MIN.
     701       17690 :       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
     702       17690 :                          -(uint64_t)Value.getConstant());
     703       17690 :       break;
     704         134 :     case MCUnaryExpr::Not:
     705         134 :       if (!Value.isAbsolute())
     706             :         return false;
     707         133 :       Res = MCValue::get(~Value.getConstant());
     708         133 :       break;
     709          13 :     case MCUnaryExpr::Plus:
     710          13 :       Res = Value;
     711          13 :       break;
     712             :     }
     713             : 
     714             :     return true;
     715             :   }
     716             : 
     717             :   case Binary: {
     718             :     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
     719     2220786 :     MCValue LHSValue, RHSValue;
     720             : 
     721     2220786 :     if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
     722     4441491 :                                                   Addrs, InSet) ||
     723     2220705 :         !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
     724             :                                                   Addrs, InSet))
     725             :       return false;
     726             : 
     727             :     // We only support a few operations on non-constant expressions, handle
     728             :     // those first.
     729     2247433 :     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
     730     2194125 :       switch (ABE->getOpcode()) {
     731             :       default:
     732             :         return false;
     733     1271699 :       case MCBinaryExpr::Sub:
     734             :         // Negate RHS and add.
     735             :         // The cast avoids undefined behavior if the constant is INT64_MIN.
     736     1271699 :         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
     737             :                                    RHSValue.getSymB(), RHSValue.getSymA(),
     738     2543398 :                                    -(uint64_t)RHSValue.getConstant(), Res);
     739             : 
     740      922248 :       case MCBinaryExpr::Add:
     741      922248 :         return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
     742             :                                    RHSValue.getSymA(), RHSValue.getSymB(),
     743      922248 :                                    RHSValue.getConstant(), Res);
     744             :       }
     745             :     }
     746             : 
     747             :     // FIXME: We need target hooks for the evaluation. It may be limited in
     748             :     // width, and gas defines the result of comparisons differently from
     749             :     // Apple as.
     750       26574 :     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
     751             :     int64_t Result = 0;
     752       26574 :     switch (ABE->getOpcode()) {
     753           1 :     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
     754         430 :     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
     755          37 :     case MCBinaryExpr::And:  Result = LHS & RHS; break;
     756         147 :     case MCBinaryExpr::Div:
     757             :     case MCBinaryExpr::Mod:
     758             :       // Handle division by zero. gas just emits a warning and keeps going,
     759             :       // we try to be stricter.
     760             :       // FIXME: Currently the caller of this function has no way to understand
     761             :       // we're bailing out because of 'division by zero'. Therefore, it will
     762             :       // emit a 'expected relocatable expression' error. It would be nice to
     763             :       // change this code to emit a better diagnostic.
     764         147 :       if (RHS == 0)
     765             :         return false;
     766         141 :       if (ABE->getOpcode() == MCBinaryExpr::Div)
     767         136 :         Result = LHS / RHS;
     768             :       else
     769           5 :         Result = LHS % RHS;
     770             :       break;
     771          18 :     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
     772           5 :     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
     773           1 :     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
     774           3 :     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
     775           4 :     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
     776          27 :     case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
     777           4 :     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
     778           1 :     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
     779         221 :     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
     780          42 :     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
     781          80 :     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
     782         263 :     case MCBinaryExpr::Shl:  Result = uint64_t(LHS) << uint64_t(RHS); break;
     783       25288 :     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
     784           2 :     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
     785             :     }
     786             : 
     787       26568 :     Res = MCValue::get(Result);
     788       26568 :     return true;
     789             :   }
     790             :   }
     791             : 
     792           0 :   llvm_unreachable("Invalid assembly expression kind!");
     793             : }
     794             : 
     795       17162 : MCFragment *MCExpr::findAssociatedFragment() const {
     796       17164 :   switch (getKind()) {
     797             :   case Target:
     798             :     // We never look through target specific expressions.
     799           0 :     return cast<MCTargetExpr>(this)->findAssociatedFragment();
     800             : 
     801        2030 :   case Constant:
     802        2030 :     return MCSymbol::AbsolutePseudoFragment;
     803             : 
     804             :   case SymbolRef: {
     805             :     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
     806        9066 :     const MCSymbol &Sym = SRE->getSymbol();
     807        9066 :     return Sym.getFragment();
     808             :   }
     809             : 
     810             :   case Unary:
     811           2 :     return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
     812             : 
     813             :   case Binary: {
     814             :     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
     815        6066 :     MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
     816        6066 :     MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
     817             : 
     818             :     // If either is absolute, return the other.
     819        6066 :     if (LHS_F == MCSymbol::AbsolutePseudoFragment)
     820             :       return RHS_F;
     821        4390 :     if (RHS_F == MCSymbol::AbsolutePseudoFragment)
     822             :       return LHS_F;
     823             : 
     824             :     // Not always correct, but probably the best we can do without more context.
     825        4301 :     if (BE->getOpcode() == MCBinaryExpr::Sub)
     826             :       return MCSymbol::AbsolutePseudoFragment;
     827             : 
     828             :     // Otherwise, return the first non-null fragment.
     829           1 :     return LHS_F ? LHS_F : RHS_F;
     830             :   }
     831             :   }
     832             : 
     833           0 :   llvm_unreachable("Invalid assembly expression kind!");
     834             : }

Generated by: LCOV version 1.13