1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===// 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 #ifndef LLVM_MC_MCEXPR_H 11 #define LLVM_MC_MCEXPR_H 12 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/Support/Casting.h" 15 #include "llvm/Support/DataTypes.h" 16 17 namespace llvm { 18 class MCAsmInfo; 19 class MCAsmLayout; 20 class MCAssembler; 21 class MCContext; 22 class MCFixup; 23 class MCFragment; 24 class MCSection; 25 class MCStreamer; 26 class MCSymbol; 27 class MCValue; 28 class raw_ostream; 29 class StringRef; 30 typedef DenseMap<const MCSection *, uint64_t> SectionAddrMap; 31 32 /// \brief Base class for the full range of assembler expressions which are 33 /// needed for parsing. 34 class MCExpr { 35 public: 36 enum ExprKind { 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 ExprKind Kind; 46 47 MCExpr(const MCExpr&) = delete; 48 void operator=(const MCExpr&) = delete; 49 50 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, 51 const MCAsmLayout *Layout, 52 const SectionAddrMap *Addrs) const; 53 54 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, 55 const MCAsmLayout *Layout, 56 const SectionAddrMap *Addrs, bool InSet) const; 57 58 protected: MCExpr(ExprKind Kind)59 explicit MCExpr(ExprKind Kind) : Kind(Kind) {} 60 61 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, 62 const MCAsmLayout *Layout, 63 const MCFixup *Fixup, 64 const SectionAddrMap *Addrs, bool InSet) const; 65 66 public: 67 /// \name Accessors 68 /// @{ 69 getKind()70 ExprKind getKind() const { return Kind; } 71 72 /// @} 73 /// \name Utility Methods 74 /// @{ 75 76 void print(raw_ostream &OS, const MCAsmInfo *MAI, 77 bool InParens = false) const; 78 void dump() const; 79 80 /// @} 81 /// \name Expression Evaluation 82 /// @{ 83 84 /// \brief Try to evaluate the expression to an absolute value. 85 /// 86 /// \param Res - The absolute value, if evaluation succeeds. 87 /// \param Layout - The assembler layout object to use for evaluating symbol 88 /// values. If not given, then only non-symbolic expressions will be 89 /// evaluated. 90 /// \return - True on success. 91 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout, 92 const SectionAddrMap &Addrs) const; 93 bool evaluateAsAbsolute(int64_t &Res) const; 94 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const; 95 bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const; 96 97 bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const; 98 99 /// \brief Try to evaluate the expression to a relocatable value, i.e. an 100 /// expression of the fixed form (a - b + constant). 101 /// 102 /// \param Res - The relocatable value, if evaluation succeeds. 103 /// \param Layout - The assembler layout object to use for evaluating values. 104 /// \param Fixup - The Fixup object if available. 105 /// \return - True on success. 106 bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, 107 const MCFixup *Fixup) const; 108 109 /// \brief Try to evaluate the expression to the form (a - b + constant) where 110 /// neither a nor b are variables. 111 /// 112 /// This is a more aggressive variant of evaluateAsRelocatable. The intended 113 /// use is for when relocations are not available, like the .size directive. 114 bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const; 115 116 /// \brief Find the "associated section" for this expression, which is 117 /// currently defined as the absolute section for constants, or 118 /// otherwise the section associated with the first defined symbol in the 119 /// expression. 120 MCFragment *findAssociatedFragment() const; 121 122 /// @} 123 }; 124 125 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) { 126 E.print(OS, nullptr); 127 return OS; 128 } 129 130 //// \brief Represent a constant integer expression. 131 class MCConstantExpr : public MCExpr { 132 int64_t Value; 133 MCConstantExpr(int64_t Value)134 explicit MCConstantExpr(int64_t Value) 135 : MCExpr(MCExpr::Constant), Value(Value) {} 136 137 public: 138 /// \name Construction 139 /// @{ 140 141 static const MCConstantExpr *create(int64_t Value, MCContext &Ctx); 142 143 /// @} 144 /// \name Accessors 145 /// @{ 146 getValue()147 int64_t getValue() const { return Value; } 148 149 /// @} 150 classof(const MCExpr * E)151 static bool classof(const MCExpr *E) { 152 return E->getKind() == MCExpr::Constant; 153 } 154 }; 155 156 /// \brief Represent a reference to a symbol from inside an expression. 157 /// 158 /// A symbol reference in an expression may be a use of a label, a use of an 159 /// assembler variable (defined constant), or constitute an implicit definition 160 /// of the symbol as external. 161 class MCSymbolRefExpr : public MCExpr { 162 public: 163 enum VariantKind : uint16_t { 164 VK_None, 165 VK_Invalid, 166 167 VK_GOT, 168 VK_GOTOFF, 169 VK_GOTREL, 170 VK_GOTPCREL, 171 VK_GOTTPOFF, 172 VK_INDNTPOFF, 173 VK_NTPOFF, 174 VK_GOTNTPOFF, 175 VK_PLT, 176 VK_TLSGD, 177 VK_TLSLD, 178 VK_TLSLDM, 179 VK_TPOFF, 180 VK_DTPOFF, 181 VK_TLSCALL, // symbol(tlscall) 182 VK_TLSDESC, // symbol(tlsdesc) 183 VK_TLVP, // Mach-O thread local variable relocations 184 VK_TLVPPAGE, 185 VK_TLVPPAGEOFF, 186 VK_PAGE, 187 VK_PAGEOFF, 188 VK_GOTPAGE, 189 VK_GOTPAGEOFF, 190 VK_SECREL, 191 VK_SIZE, // symbol@SIZE 192 VK_WEAKREF, // The link between the symbols in .weakref foo, bar 193 194 VK_ARM_NONE, 195 VK_ARM_GOT_PREL, 196 VK_ARM_TARGET1, 197 VK_ARM_TARGET2, 198 VK_ARM_PREL31, 199 VK_ARM_SBREL, // symbol(sbrel) 200 VK_ARM_TLSLDO, // symbol(tlsldo) 201 VK_ARM_TLSDESCSEQ, 202 203 VK_PPC_LO, // symbol@l 204 VK_PPC_HI, // symbol@h 205 VK_PPC_HA, // symbol@ha 206 VK_PPC_HIGHER, // symbol@higher 207 VK_PPC_HIGHERA, // symbol@highera 208 VK_PPC_HIGHEST, // symbol@highest 209 VK_PPC_HIGHESTA, // symbol@highesta 210 VK_PPC_GOT_LO, // symbol@got@l 211 VK_PPC_GOT_HI, // symbol@got@h 212 VK_PPC_GOT_HA, // symbol@got@ha 213 VK_PPC_TOCBASE, // symbol@tocbase 214 VK_PPC_TOC, // symbol@toc 215 VK_PPC_TOC_LO, // symbol@toc@l 216 VK_PPC_TOC_HI, // symbol@toc@h 217 VK_PPC_TOC_HA, // symbol@toc@ha 218 VK_PPC_DTPMOD, // symbol@dtpmod 219 VK_PPC_TPREL_LO, // symbol@tprel@l 220 VK_PPC_TPREL_HI, // symbol@tprel@h 221 VK_PPC_TPREL_HA, // symbol@tprel@ha 222 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher 223 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera 224 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest 225 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta 226 VK_PPC_DTPREL_LO, // symbol@dtprel@l 227 VK_PPC_DTPREL_HI, // symbol@dtprel@h 228 VK_PPC_DTPREL_HA, // symbol@dtprel@ha 229 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher 230 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera 231 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest 232 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta 233 VK_PPC_GOT_TPREL, // symbol@got@tprel 234 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l 235 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h 236 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha 237 VK_PPC_GOT_DTPREL, // symbol@got@dtprel 238 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l 239 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h 240 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha 241 VK_PPC_TLS, // symbol@tls 242 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd 243 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l 244 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h 245 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha 246 VK_PPC_TLSGD, // symbol@tlsgd 247 VK_PPC_GOT_TLSLD, // symbol@got@tlsld 248 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l 249 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h 250 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha 251 VK_PPC_TLSLD, // symbol@tlsld 252 VK_PPC_LOCAL, // symbol@local 253 254 VK_COFF_IMGREL32, // symbol@imgrel (image-relative) 255 256 VK_Hexagon_PCREL, 257 VK_Hexagon_LO16, 258 VK_Hexagon_HI16, 259 VK_Hexagon_GPREL, 260 VK_Hexagon_GD_GOT, 261 VK_Hexagon_LD_GOT, 262 VK_Hexagon_GD_PLT, 263 VK_Hexagon_LD_PLT, 264 VK_Hexagon_IE, 265 VK_Hexagon_IE_GOT, 266 267 VK_WebAssembly_FUNCTION, // Function table index, rather than virtual addr 268 269 VK_TPREL, 270 VK_DTPREL 271 }; 272 273 private: 274 /// The symbol reference modifier. 275 const VariantKind Kind; 276 277 /// Specifies how the variant kind should be printed. 278 const unsigned UseParensForSymbolVariant : 1; 279 280 // FIXME: Remove this bit. 281 const unsigned HasSubsectionsViaSymbols : 1; 282 283 /// The symbol being referenced. 284 const MCSymbol *Symbol; 285 286 explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind, 287 const MCAsmInfo *MAI); 288 289 public: 290 /// \name Construction 291 /// @{ 292 create(const MCSymbol * Symbol,MCContext & Ctx)293 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) { 294 return MCSymbolRefExpr::create(Symbol, VK_None, Ctx); 295 } 296 297 static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind, 298 MCContext &Ctx); 299 static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind, 300 MCContext &Ctx); 301 302 /// @} 303 /// \name Accessors 304 /// @{ 305 getSymbol()306 const MCSymbol &getSymbol() const { return *Symbol; } 307 getKind()308 VariantKind getKind() const { return Kind; } 309 310 void printVariantKind(raw_ostream &OS) const; 311 hasSubsectionsViaSymbols()312 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; } 313 314 /// @} 315 /// \name Static Utility Functions 316 /// @{ 317 318 static StringRef getVariantKindName(VariantKind Kind); 319 320 static VariantKind getVariantKindForName(StringRef Name); 321 322 /// @} 323 classof(const MCExpr * E)324 static bool classof(const MCExpr *E) { 325 return E->getKind() == MCExpr::SymbolRef; 326 } 327 }; 328 329 /// \brief Unary assembler expressions. 330 class MCUnaryExpr : public MCExpr { 331 public: 332 enum Opcode { 333 LNot, ///< Logical negation. 334 Minus, ///< Unary minus. 335 Not, ///< Bitwise negation. 336 Plus ///< Unary plus. 337 }; 338 339 private: 340 Opcode Op; 341 const MCExpr *Expr; 342 MCUnaryExpr(Opcode Op,const MCExpr * Expr)343 MCUnaryExpr(Opcode Op, const MCExpr *Expr) 344 : MCExpr(MCExpr::Unary), Op(Op), Expr(Expr) {} 345 346 public: 347 /// \name Construction 348 /// @{ 349 350 static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr, 351 MCContext &Ctx); createLNot(const MCExpr * Expr,MCContext & Ctx)352 static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx) { 353 return create(LNot, Expr, Ctx); 354 } createMinus(const MCExpr * Expr,MCContext & Ctx)355 static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx) { 356 return create(Minus, Expr, Ctx); 357 } createNot(const MCExpr * Expr,MCContext & Ctx)358 static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx) { 359 return create(Not, Expr, Ctx); 360 } createPlus(const MCExpr * Expr,MCContext & Ctx)361 static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx) { 362 return create(Plus, Expr, Ctx); 363 } 364 365 /// @} 366 /// \name Accessors 367 /// @{ 368 369 /// \brief Get the kind of this unary expression. getOpcode()370 Opcode getOpcode() const { return Op; } 371 372 /// \brief Get the child of this unary expression. getSubExpr()373 const MCExpr *getSubExpr() const { return Expr; } 374 375 /// @} 376 classof(const MCExpr * E)377 static bool classof(const MCExpr *E) { 378 return E->getKind() == MCExpr::Unary; 379 } 380 }; 381 382 /// \brief Binary assembler expressions. 383 class MCBinaryExpr : public MCExpr { 384 public: 385 enum Opcode { 386 Add, ///< Addition. 387 And, ///< Bitwise and. 388 Div, ///< Signed division. 389 EQ, ///< Equality comparison. 390 GT, ///< Signed greater than comparison (result is either 0 or some 391 ///< target-specific non-zero value) 392 GTE, ///< Signed greater than or equal comparison (result is either 0 or 393 ///< some target-specific non-zero value). 394 LAnd, ///< Logical and. 395 LOr, ///< Logical or. 396 LT, ///< Signed less than comparison (result is either 0 or 397 ///< some target-specific non-zero value). 398 LTE, ///< Signed less than or equal comparison (result is either 0 or 399 ///< some target-specific non-zero value). 400 Mod, ///< Signed remainder. 401 Mul, ///< Multiplication. 402 NE, ///< Inequality comparison. 403 Or, ///< Bitwise or. 404 Shl, ///< Shift left. 405 AShr, ///< Arithmetic shift right. 406 LShr, ///< Logical shift right. 407 Sub, ///< Subtraction. 408 Xor ///< Bitwise exclusive or. 409 }; 410 411 private: 412 Opcode Op; 413 const MCExpr *LHS, *RHS; 414 MCBinaryExpr(Opcode Op,const MCExpr * LHS,const MCExpr * RHS)415 MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS) 416 : MCExpr(MCExpr::Binary), Op(Op), LHS(LHS), RHS(RHS) {} 417 418 public: 419 /// \name Construction 420 /// @{ 421 422 static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS, 423 const MCExpr *RHS, MCContext &Ctx); createAdd(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)424 static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS, 425 MCContext &Ctx) { 426 return create(Add, LHS, RHS, Ctx); 427 } createAnd(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)428 static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS, 429 MCContext &Ctx) { 430 return create(And, LHS, RHS, Ctx); 431 } createDiv(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)432 static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS, 433 MCContext &Ctx) { 434 return create(Div, LHS, RHS, Ctx); 435 } createEQ(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)436 static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS, 437 MCContext &Ctx) { 438 return create(EQ, LHS, RHS, Ctx); 439 } createGT(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)440 static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS, 441 MCContext &Ctx) { 442 return create(GT, LHS, RHS, Ctx); 443 } createGTE(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)444 static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS, 445 MCContext &Ctx) { 446 return create(GTE, LHS, RHS, Ctx); 447 } createLAnd(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)448 static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS, 449 MCContext &Ctx) { 450 return create(LAnd, LHS, RHS, Ctx); 451 } createLOr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)452 static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS, 453 MCContext &Ctx) { 454 return create(LOr, LHS, RHS, Ctx); 455 } createLT(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)456 static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS, 457 MCContext &Ctx) { 458 return create(LT, LHS, RHS, Ctx); 459 } createLTE(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)460 static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS, 461 MCContext &Ctx) { 462 return create(LTE, LHS, RHS, Ctx); 463 } createMod(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)464 static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS, 465 MCContext &Ctx) { 466 return create(Mod, LHS, RHS, Ctx); 467 } createMul(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)468 static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS, 469 MCContext &Ctx) { 470 return create(Mul, LHS, RHS, Ctx); 471 } createNE(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)472 static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS, 473 MCContext &Ctx) { 474 return create(NE, LHS, RHS, Ctx); 475 } createOr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)476 static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS, 477 MCContext &Ctx) { 478 return create(Or, LHS, RHS, Ctx); 479 } createShl(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)480 static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS, 481 MCContext &Ctx) { 482 return create(Shl, LHS, RHS, Ctx); 483 } createAShr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)484 static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS, 485 MCContext &Ctx) { 486 return create(AShr, LHS, RHS, Ctx); 487 } createLShr(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)488 static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS, 489 MCContext &Ctx) { 490 return create(LShr, LHS, RHS, Ctx); 491 } createSub(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)492 static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS, 493 MCContext &Ctx) { 494 return create(Sub, LHS, RHS, Ctx); 495 } createXor(const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx)496 static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS, 497 MCContext &Ctx) { 498 return create(Xor, LHS, RHS, Ctx); 499 } 500 501 /// @} 502 /// \name Accessors 503 /// @{ 504 505 /// \brief Get the kind of this binary expression. getOpcode()506 Opcode getOpcode() const { return Op; } 507 508 /// \brief Get the left-hand side expression of the binary operator. getLHS()509 const MCExpr *getLHS() const { return LHS; } 510 511 /// \brief Get the right-hand side expression of the binary operator. getRHS()512 const MCExpr *getRHS() const { return RHS; } 513 514 /// @} 515 classof(const MCExpr * E)516 static bool classof(const MCExpr *E) { 517 return E->getKind() == MCExpr::Binary; 518 } 519 }; 520 521 /// \brief This is an extension point for target-specific MCExpr subclasses to 522 /// implement. 523 /// 524 /// NOTE: All subclasses are required to have trivial destructors because 525 /// MCExprs are bump pointer allocated and not destructed. 526 class MCTargetExpr : public MCExpr { 527 virtual void anchor(); 528 protected: MCTargetExpr()529 MCTargetExpr() : MCExpr(Target) {} ~MCTargetExpr()530 virtual ~MCTargetExpr() {} 531 public: 532 virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0; 533 virtual bool evaluateAsRelocatableImpl(MCValue &Res, 534 const MCAsmLayout *Layout, 535 const MCFixup *Fixup) const = 0; 536 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0; 537 virtual MCFragment *findAssociatedFragment() const = 0; 538 539 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0; 540 classof(const MCExpr * E)541 static bool classof(const MCExpr *E) { 542 return E->getKind() == MCExpr::Target; 543 } 544 }; 545 546 } // end namespace llvm 547 548 #endif 549