1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===// 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 #ifndef LLVM_MC_MCEXPR_H 10 #define LLVM_MC_MCEXPR_H 11 12 #include "llvm/ADT/DenseMap.h" 13 #include "llvm/Support/SMLoc.h" 14 #include <cstdint> 15 16 namespace llvm { 17 18 class MCAsmInfo; 19 class MCAssembler; 20 class MCContext; 21 class MCFixup; 22 class MCFragment; 23 class MCSection; 24 class MCStreamer; 25 class MCSymbol; 26 class MCValue; 27 class raw_ostream; 28 class StringRef; 29 30 using SectionAddrMap = DenseMap<const MCSection *, uint64_t>; 31 32 /// Base class for the full range of assembler expressions which are 33 /// needed for parsing. 34 class MCExpr { 35 public: 36 enum ExprKind : uint8_t { 37 Binary, ///< Binary expressions. 38 Constant, ///< Constant expressions. 39 SymbolRef, ///< References to labels and assigned expressions. 40 Unary, ///< Unary expressions. 41 Target ///< Target specific expression. 42 }; 43 44 private: 45 static const unsigned NumSubclassDataBits = 24; 46 static_assert( 47 NumSubclassDataBits == CHAR_BIT * (sizeof(unsigned) - sizeof(ExprKind)), 48 "ExprKind and SubclassData together should take up one word"); 49 50 ExprKind Kind; 51 /// Field reserved for use by MCExpr subclasses. 52 unsigned SubclassData : NumSubclassDataBits; 53 SMLoc Loc; 54 55 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, 56 const SectionAddrMap *Addrs, bool InSet) const; 57 58 protected: 59 explicit MCExpr(ExprKind Kind, SMLoc Loc, unsigned SubclassData = 0) 60 : Kind(Kind), SubclassData(SubclassData), Loc(Loc) { 61 assert(SubclassData < (1 << NumSubclassDataBits) && 62 "Subclass data too large"); 63 } 64 65 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, 66 const MCFixup *Fixup, 67 const SectionAddrMap *Addrs, bool InSet) const; 68 69 unsigned getSubclassData() const { return SubclassData; } 70 71 public: 72 MCExpr(const MCExpr &) = delete; 73 MCExpr &operator=(const MCExpr &) = delete; 74 75 /// \name Accessors 76 /// @{ 77 78 ExprKind getKind() const { return Kind; } 79 SMLoc getLoc() const { return Loc; } 80 81 /// @} 82 /// \name Utility Methods 83 /// @{ 84 85 void print(raw_ostream &OS, const MCAsmInfo *MAI, 86 bool InParens = false) const; 87 void dump() const; 88 89 /// Returns whether the given symbol is used anywhere in the expression or 90 /// subexpressions. 91 bool isSymbolUsedInExpression(const MCSymbol *Sym) const; 92 93 /// @} 94 /// \name Expression Evaluation 95 /// @{ 96 97 /// Try to evaluate the expression to an absolute value. 98 /// 99 /// \param Res - The absolute value, if evaluation succeeds. 100 /// \return - True on success. 101 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm, 102 const SectionAddrMap &Addrs) const; 103 bool evaluateAsAbsolute(int64_t &Res) const; 104 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const; 105 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const; 106 107 /// Aggressive variant of evaluateAsRelocatable when relocations are 108 /// unavailable (e.g. .fill). Expects callers to handle errors when true is 109 /// returned. 110 bool evaluateKnownAbsolute(int64_t &Res, const MCAssembler &Asm) const; 111 112 /// Try to evaluate the expression to a relocatable value, i.e. an 113 /// expression of the fixed form (a - b + constant). 114 /// 115 /// \param Res - The relocatable value, if evaluation succeeds. 116 /// \param Asm - The assembler object to use for evaluating values. 117 /// \param Fixup - The Fixup object if available. 118 /// \return - True on success. 119 bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, 120 const MCFixup *Fixup) const; 121 122 /// Try to evaluate the expression to the form (a - b + constant) where 123 /// neither a nor b are variables. 124 /// 125 /// This is a more aggressive variant of evaluateAsRelocatable. The intended 126 /// use is for when relocations are not available, like the .size directive. 127 bool evaluateAsValue(MCValue &Res, const MCAssembler &Asm) const; 128 129 /// Find the "associated section" for this expression, which is 130 /// currently defined as the absolute section for constants, or 131 /// otherwise the section associated with the first defined symbol in the 132 /// expression. 133 MCFragment *findAssociatedFragment() const; 134 135 /// @} 136 }; 137 138 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) { 139 E.print(OS, nullptr); 140 return OS; 141 } 142 143 //// Represent a constant integer expression. 144 class MCConstantExpr : public MCExpr { 145 int64_t Value; 146 147 // Subclass data stores SizeInBytes in bits 0..7 and PrintInHex in bit 8. 148 static const unsigned SizeInBytesBits = 8; 149 static const unsigned SizeInBytesMask = (1 << SizeInBytesBits) - 1; 150 static const unsigned PrintInHexBit = 1 << SizeInBytesBits; 151 152 static unsigned encodeSubclassData(bool PrintInHex, unsigned SizeInBytes) { 153 assert(SizeInBytes <= sizeof(int64_t) && "Excessive size"); 154 return SizeInBytes | (PrintInHex ? PrintInHexBit : 0); 155 } 156 157 MCConstantExpr(int64_t Value, bool PrintInHex, unsigned SizeInBytes) 158 : MCExpr(MCExpr::Constant, SMLoc(), 159 encodeSubclassData(PrintInHex, SizeInBytes)), Value(Value) {} 160 161 public: 162 /// \name Construction 163 /// @{ 164 165 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx, 166 bool PrintInHex = false, 167 unsigned SizeInBytes = 0); 168 169 /// @} 170 /// \name Accessors 171 /// @{ 172 173 int64_t getValue() const { return Value; } 174 unsigned getSizeInBytes() const { 175 return getSubclassData() & SizeInBytesMask; 176 } 177 178 bool useHexFormat() const { return (getSubclassData() & PrintInHexBit) != 0; } 179 180 /// @} 181 182 static bool classof(const MCExpr *E) { 183 return E->getKind() == MCExpr::Constant; 184 } 185 }; 186 187 /// Represent a reference to a symbol from inside an expression. 188 /// 189 /// A symbol reference in an expression may be a use of a label, a use of an 190 /// assembler variable (defined constant), or constitute an implicit definition 191 /// of the symbol as external. 192 class MCSymbolRefExpr : public MCExpr { 193 public: 194 enum VariantKind : uint16_t { 195 VK_None, 196 VK_Invalid, 197 198 VK_GOT, 199 VK_GOTENT, 200 VK_GOTOFF, 201 VK_GOTREL, 202 VK_PCREL, 203 VK_GOTPCREL, 204 VK_GOTPCREL_NORELAX, 205 VK_GOTTPOFF, 206 VK_INDNTPOFF, 207 VK_NTPOFF, 208 VK_GOTNTPOFF, 209 VK_PLT, 210 VK_TLSGD, 211 VK_TLSLD, 212 VK_TLSLDM, 213 VK_TPOFF, 214 VK_DTPOFF, 215 VK_TLSCALL, // symbol(tlscall) 216 VK_TLSDESC, // symbol(tlsdesc) 217 VK_TLVP, // Mach-O thread local variable relocations 218 VK_TLVPPAGE, 219 VK_TLVPPAGEOFF, 220 VK_PAGE, 221 VK_PAGEOFF, 222 VK_GOTPAGE, 223 VK_GOTPAGEOFF, 224 VK_SECREL, 225 VK_SIZE, // symbol@SIZE 226 VK_WEAKREF, // The link between the symbols in .weakref foo, bar 227 VK_FUNCDESC, 228 VK_GOTFUNCDESC, 229 VK_GOTOFFFUNCDESC, 230 VK_TLSGD_FDPIC, 231 VK_TLSLDM_FDPIC, 232 VK_GOTTPOFF_FDPIC, 233 234 VK_X86_ABS8, 235 VK_X86_PLTOFF, 236 237 VK_ARM_NONE, 238 VK_ARM_GOT_PREL, 239 VK_ARM_TARGET1, 240 VK_ARM_TARGET2, 241 VK_ARM_PREL31, 242 VK_ARM_SBREL, // symbol(sbrel) 243 VK_ARM_TLSLDO, // symbol(tlsldo) 244 VK_ARM_TLSDESCSEQ, 245 246 VK_AVR_NONE, 247 VK_AVR_LO8, 248 VK_AVR_HI8, 249 VK_AVR_HLO8, 250 VK_AVR_DIFF8, 251 VK_AVR_DIFF16, 252 VK_AVR_DIFF32, 253 VK_AVR_PM, 254 255 VK_PPC_LO, // symbol@l 256 VK_PPC_HI, // symbol@h 257 VK_PPC_HA, // symbol@ha 258 VK_PPC_HIGH, // symbol@high 259 VK_PPC_HIGHA, // symbol@higha 260 VK_PPC_HIGHER, // symbol@higher 261 VK_PPC_HIGHERA, // symbol@highera 262 VK_PPC_HIGHEST, // symbol@highest 263 VK_PPC_HIGHESTA, // symbol@highesta 264 VK_PPC_GOT_LO, // symbol@got@l 265 VK_PPC_GOT_HI, // symbol@got@h 266 VK_PPC_GOT_HA, // symbol@got@ha 267 VK_PPC_TOCBASE, // symbol@tocbase 268 VK_PPC_TOC, // symbol@toc 269 VK_PPC_TOC_LO, // symbol@toc@l 270 VK_PPC_TOC_HI, // symbol@toc@h 271 VK_PPC_TOC_HA, // symbol@toc@ha 272 VK_PPC_U, // symbol@u 273 VK_PPC_L, // symbol@l 274 VK_PPC_DTPMOD, // symbol@dtpmod 275 VK_PPC_TPREL_LO, // symbol@tprel@l 276 VK_PPC_TPREL_HI, // symbol@tprel@h 277 VK_PPC_TPREL_HA, // symbol@tprel@ha 278 VK_PPC_TPREL_HIGH, // symbol@tprel@high 279 VK_PPC_TPREL_HIGHA, // symbol@tprel@higha 280 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher 281 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera 282 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest 283 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta 284 VK_PPC_DTPREL_LO, // symbol@dtprel@l 285 VK_PPC_DTPREL_HI, // symbol@dtprel@h 286 VK_PPC_DTPREL_HA, // symbol@dtprel@ha 287 VK_PPC_DTPREL_HIGH, // symbol@dtprel@high 288 VK_PPC_DTPREL_HIGHA, // symbol@dtprel@higha 289 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher 290 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera 291 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest 292 VK_PPC_DTPREL_HIGHESTA, // symbol@dtprel@highesta 293 VK_PPC_GOT_TPREL, // symbol@got@tprel 294 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l 295 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h 296 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha 297 VK_PPC_GOT_DTPREL, // symbol@got@dtprel 298 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l 299 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h 300 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha 301 VK_PPC_TLS, // symbol@tls 302 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd 303 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l 304 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h 305 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha 306 VK_PPC_TLSGD, // symbol@tlsgd 307 VK_PPC_AIX_TLSGD, // symbol@gd 308 VK_PPC_AIX_TLSGDM, // symbol@m 309 VK_PPC_AIX_TLSIE, // symbol@ie 310 VK_PPC_AIX_TLSLE, // symbol@le 311 VK_PPC_AIX_TLSLD, // symbol@ld 312 VK_PPC_AIX_TLSML, // symbol@ml 313 VK_PPC_GOT_TLSLD, // symbol@got@tlsld 314 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l 315 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h 316 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha 317 VK_PPC_GOT_PCREL, // symbol@got@pcrel 318 VK_PPC_GOT_TLSGD_PCREL, // symbol@got@tlsgd@pcrel 319 VK_PPC_GOT_TLSLD_PCREL, // symbol@got@tlsld@pcrel 320 VK_PPC_GOT_TPREL_PCREL, // symbol@got@tprel@pcrel 321 VK_PPC_TLS_PCREL, // symbol@tls@pcrel 322 VK_PPC_TLSLD, // symbol@tlsld 323 VK_PPC_LOCAL, // symbol@local 324 VK_PPC_NOTOC, // symbol@notoc 325 VK_PPC_PCREL_OPT, // .reloc expr, R_PPC64_PCREL_OPT, expr 326 327 VK_COFF_IMGREL32, // symbol@imgrel (image-relative) 328 329 VK_Hexagon_LO16, 330 VK_Hexagon_HI16, 331 VK_Hexagon_GPREL, 332 VK_Hexagon_GD_GOT, 333 VK_Hexagon_LD_GOT, 334 VK_Hexagon_GD_PLT, 335 VK_Hexagon_LD_PLT, 336 VK_Hexagon_IE, 337 VK_Hexagon_IE_GOT, 338 339 VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature) 340 VK_WASM_TLSREL, // Memory address relative to __tls_base 341 VK_WASM_MBREL, // Memory address relative to __memory_base 342 VK_WASM_TBREL, // Table index relative to __table_base 343 VK_WASM_GOT_TLS, // Wasm global index of TLS symbol. 344 VK_WASM_FUNCINDEX, // Wasm function index. 345 346 VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo 347 VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi 348 VK_AMDGPU_REL32_LO, // symbol@rel32@lo 349 VK_AMDGPU_REL32_HI, // symbol@rel32@hi 350 VK_AMDGPU_REL64, // symbol@rel64 351 VK_AMDGPU_ABS32_LO, // symbol@abs32@lo 352 VK_AMDGPU_ABS32_HI, // symbol@abs32@hi 353 354 VK_VE_HI32, // symbol@hi 355 VK_VE_LO32, // symbol@lo 356 VK_VE_PC_HI32, // symbol@pc_hi 357 VK_VE_PC_LO32, // symbol@pc_lo 358 VK_VE_GOT_HI32, // symbol@got_hi 359 VK_VE_GOT_LO32, // symbol@got_lo 360 VK_VE_GOTOFF_HI32, // symbol@gotoff_hi 361 VK_VE_GOTOFF_LO32, // symbol@gotoff_lo 362 VK_VE_PLT_HI32, // symbol@plt_hi 363 VK_VE_PLT_LO32, // symbol@plt_lo 364 VK_VE_TLS_GD_HI32, // symbol@tls_gd_hi 365 VK_VE_TLS_GD_LO32, // symbol@tls_gd_lo 366 VK_VE_TPOFF_HI32, // symbol@tpoff_hi 367 VK_VE_TPOFF_LO32, // symbol@tpoff_lo 368 369 VK_TPREL, 370 VK_DTPREL 371 }; 372 373 private: 374 /// The symbol being referenced. 375 const MCSymbol *Symbol; 376 377 // Subclass data stores VariantKind in bits 0..15 and HasSubsectionsViaSymbols 378 // in bit 16. 379 static const unsigned VariantKindBits = 16; 380 static const unsigned VariantKindMask = (1 << VariantKindBits) - 1; 381 382 // FIXME: Remove this bit. 383 static const unsigned HasSubsectionsViaSymbolsBit = 1 << VariantKindBits; 384 385 static unsigned encodeSubclassData(VariantKind Kind, 386 bool HasSubsectionsViaSymbols) { 387 return (unsigned)Kind | 388 (HasSubsectionsViaSymbols ? HasSubsectionsViaSymbolsBit : 0); 389 } 390 391 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind, 392 const MCAsmInfo *MAI, SMLoc Loc = SMLoc()); 393 394 public: 395 /// \name Construction 396 /// @{ 397 398 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) { 399 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx); 400 } 401 402 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind, 403 MCContext &Ctx, SMLoc Loc = SMLoc()); 404 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind, 405 MCContext &Ctx); 406 407 /// @} 408 /// \name Accessors 409 /// @{ 410 411 const MCSymbol &getSymbol() const { return *Symbol; } 412 413 VariantKind getKind() const { 414 return (VariantKind)(getSubclassData() & VariantKindMask); 415 } 416 417 bool hasSubsectionsViaSymbols() const { 418 return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0; 419 } 420 421 /// @} 422 /// \name Static Utility Functions 423 /// @{ 424 425 static StringRef getVariantKindName(VariantKind Kind); 426 427 static VariantKind getVariantKindForName(StringRef Name); 428 429 /// @} 430 431 static bool classof(const MCExpr *E) { 432 return E->getKind() == MCExpr::SymbolRef; 433 } 434 }; 435 436 /// Unary assembler expressions. 437 class MCUnaryExpr : public MCExpr { 438 public: 439 enum Opcode { 440 LNot, ///< Logical negation. 441 Minus, ///< Unary minus. 442 Not, ///< Bitwise negation. 443 Plus ///< Unary plus. 444 }; 445 446 private: 447 const MCExpr *Expr; 448 449 MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc) 450 : MCExpr(MCExpr::Unary, Loc, Op), Expr(Expr) {} 451 452 public: 453 /// \name Construction 454 /// @{ 455 456 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr, 457 MCContext &Ctx, SMLoc Loc = SMLoc()); 458 459 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) { 460 return create(LNot, Expr, Ctx, Loc); 461 } 462 463 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) { 464 return create(Minus, Expr, Ctx, Loc); 465 } 466 467 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) { 468 return create(Not, Expr, Ctx, Loc); 469 } 470 471 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) { 472 return create(Plus, Expr, Ctx, Loc); 473 } 474 475 /// @} 476 /// \name Accessors 477 /// @{ 478 479 /// Get the kind of this unary expression. 480 Opcode getOpcode() const { return (Opcode)getSubclassData(); } 481 482 /// Get the child of this unary expression. 483 const MCExpr *getSubExpr() const { return Expr; } 484 485 /// @} 486 487 static bool classof(const MCExpr *E) { 488 return E->getKind() == MCExpr::Unary; 489 } 490 }; 491 492 /// Binary assembler expressions. 493 class MCBinaryExpr : public MCExpr { 494 public: 495 enum Opcode { 496 Add, ///< Addition. 497 And, ///< Bitwise and. 498 Div, ///< Signed division. 499 EQ, ///< Equality comparison. 500 GT, ///< Signed greater than comparison (result is either 0 or some 501 ///< target-specific non-zero value) 502 GTE, ///< Signed greater than or equal comparison (result is either 0 or 503 ///< some target-specific non-zero value). 504 LAnd, ///< Logical and. 505 LOr, ///< Logical or. 506 LT, ///< Signed less than comparison (result is either 0 or 507 ///< some target-specific non-zero value). 508 LTE, ///< Signed less than or equal comparison (result is either 0 or 509 ///< some target-specific non-zero value). 510 Mod, ///< Signed remainder. 511 Mul, ///< Multiplication. 512 NE, ///< Inequality comparison. 513 Or, ///< Bitwise or. 514 OrNot, ///< Bitwise or not. 515 Shl, ///< Shift left. 516 AShr, ///< Arithmetic shift right. 517 LShr, ///< Logical shift right. 518 Sub, ///< Subtraction. 519 Xor ///< Bitwise exclusive or. 520 }; 521 522 private: 523 const MCExpr *LHS, *RHS; 524 525 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, 526 SMLoc Loc = SMLoc()) 527 : MCExpr(MCExpr::Binary, Loc, Op), LHS(LHS), RHS(RHS) {} 528 529 public: 530 /// \name Construction 531 /// @{ 532 533 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS, 534 const MCExpr *RHS, MCContext &Ctx, 535 SMLoc Loc = SMLoc()); 536 537 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS, 538 MCContext &Ctx) { 539 return create(Add, LHS, RHS, Ctx); 540 } 541 542 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS, 543 MCContext &Ctx) { 544 return create(And, LHS, RHS, Ctx); 545 } 546 547 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS, 548 MCContext &Ctx) { 549 return create(Div, LHS, RHS, Ctx); 550 } 551 552 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS, 553 MCContext &Ctx) { 554 return create(EQ, LHS, RHS, Ctx); 555 } 556 557 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS, 558 MCContext &Ctx) { 559 return create(GT, LHS, RHS, Ctx); 560 } 561 562 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS, 563 MCContext &Ctx) { 564 return create(GTE, LHS, RHS, Ctx); 565 } 566 567 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS, 568 MCContext &Ctx) { 569 return create(LAnd, LHS, RHS, Ctx); 570 } 571 572 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS, 573 MCContext &Ctx) { 574 return create(LOr, LHS, RHS, Ctx); 575 } 576 577 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS, 578 MCContext &Ctx) { 579 return create(LT, LHS, RHS, Ctx); 580 } 581 582 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS, 583 MCContext &Ctx) { 584 return create(LTE, LHS, RHS, Ctx); 585 } 586 587 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS, 588 MCContext &Ctx) { 589 return create(Mod, LHS, RHS, Ctx); 590 } 591 592 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS, 593 MCContext &Ctx) { 594 return create(Mul, LHS, RHS, Ctx); 595 } 596 597 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS, 598 MCContext &Ctx) { 599 return create(NE, LHS, RHS, Ctx); 600 } 601 602 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS, 603 MCContext &Ctx) { 604 return create(Or, LHS, RHS, Ctx); 605 } 606 607 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS, 608 MCContext &Ctx) { 609 return create(Shl, LHS, RHS, Ctx); 610 } 611 612 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS, 613 MCContext &Ctx) { 614 return create(AShr, LHS, RHS, Ctx); 615 } 616 617 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS, 618 MCContext &Ctx) { 619 return create(LShr, LHS, RHS, Ctx); 620 } 621 622 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS, 623 MCContext &Ctx) { 624 return create(Sub, LHS, RHS, Ctx); 625 } 626 627 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS, 628 MCContext &Ctx) { 629 return create(Xor, LHS, RHS, Ctx); 630 } 631 632 /// @} 633 /// \name Accessors 634 /// @{ 635 636 /// Get the kind of this binary expression. 637 Opcode getOpcode() const { return (Opcode)getSubclassData(); } 638 639 /// Get the left-hand side expression of the binary operator. 640 const MCExpr *getLHS() const { return LHS; } 641 642 /// Get the right-hand side expression of the binary operator. 643 const MCExpr *getRHS() const { return RHS; } 644 645 /// @} 646 647 static bool classof(const MCExpr *E) { 648 return E->getKind() == MCExpr::Binary; 649 } 650 }; 651 652 /// This is an extension point for target-specific MCExpr subclasses to 653 /// implement. 654 /// 655 /// NOTE: All subclasses are required to have trivial destructors because 656 /// MCExprs are bump pointer allocated and not destructed. 657 class MCTargetExpr : public MCExpr { 658 virtual void anchor(); 659 660 protected: 661 MCTargetExpr() : MCExpr(Target, SMLoc()) {} 662 virtual ~MCTargetExpr() = default; 663 664 public: 665 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0; 666 virtual bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, 667 const MCFixup *Fixup) const = 0; 668 // allow Target Expressions to be checked for equality 669 virtual bool isEqualTo(const MCExpr *x) const { return false; } 670 virtual bool isSymbolUsedInExpression(const MCSymbol *Sym) const { 671 return false; 672 } 673 // This should be set when assigned expressions are not valid ".set" 674 // expressions, e.g. registers, and must be inlined. 675 virtual bool inlineAssignedExpr() const { return false; } 676 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0; 677 virtual MCFragment *findAssociatedFragment() const = 0; 678 679 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0; 680 681 static bool classof(const MCExpr *E) { 682 return E->getKind() == MCExpr::Target; 683 } 684 }; 685 686 } // end namespace llvm 687 688 #endif // LLVM_MC_MCEXPR_H 689