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