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