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