MCExpr.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCEXPR_H
  14. #define LLVM_MC_MCEXPR_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/Support/SMLoc.h"
  17. #include <cstdint>
  18. namespace llvm {
  19. class MCAsmInfo;
  20. class MCAsmLayout;
  21. class MCAssembler;
  22. class MCContext;
  23. class MCFixup;
  24. class MCFragment;
  25. class MCSection;
  26. class MCStreamer;
  27. class MCSymbol;
  28. class MCValue;
  29. class raw_ostream;
  30. class StringRef;
  31. using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
  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. private:
  44. static const unsigned NumSubclassDataBits = 24;
  45. static_assert(
  46. NumSubclassDataBits == CHAR_BIT * (sizeof(unsigned) - sizeof(ExprKind)),
  47. "ExprKind and SubclassData together should take up one word");
  48. ExprKind Kind;
  49. /// Field reserved for use by MCExpr subclasses.
  50. unsigned SubclassData : NumSubclassDataBits;
  51. SMLoc Loc;
  52. bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
  53. const MCAsmLayout *Layout,
  54. const SectionAddrMap *Addrs, bool InSet) const;
  55. protected:
  56. explicit MCExpr(ExprKind Kind, SMLoc Loc, unsigned SubclassData = 0)
  57. : Kind(Kind), SubclassData(SubclassData), Loc(Loc) {
  58. assert(SubclassData < (1 << NumSubclassDataBits) &&
  59. "Subclass data too large");
  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. unsigned getSubclassData() const { return SubclassData; }
  66. public:
  67. MCExpr(const MCExpr &) = delete;
  68. MCExpr &operator=(const MCExpr &) = delete;
  69. /// \name Accessors
  70. /// @{
  71. ExprKind getKind() const { return Kind; }
  72. SMLoc getLoc() const { return Loc; }
  73. /// @}
  74. /// \name Utility Methods
  75. /// @{
  76. void print(raw_ostream &OS, const MCAsmInfo *MAI,
  77. bool InParens = false) const;
  78. void dump() const;
  79. /// @}
  80. /// \name Expression Evaluation
  81. /// @{
  82. /// Try to evaluate the expression to an absolute value.
  83. ///
  84. /// \param Res - The absolute value, if evaluation succeeds.
  85. /// \param Layout - The assembler layout object to use for evaluating symbol
  86. /// values. If not given, then only non-symbolic expressions will be
  87. /// evaluated.
  88. /// \return - True on success.
  89. bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
  90. const SectionAddrMap &Addrs) const;
  91. bool evaluateAsAbsolute(int64_t &Res) const;
  92. bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
  93. bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const;
  94. bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
  95. bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
  96. /// Try to evaluate the expression to a relocatable value, i.e. an
  97. /// expression of the fixed form (a - b + constant).
  98. ///
  99. /// \param Res - The relocatable value, if evaluation succeeds.
  100. /// \param Layout - The assembler layout object to use for evaluating values.
  101. /// \param Fixup - The Fixup object if available.
  102. /// \return - True on success.
  103. bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
  104. const MCFixup *Fixup) const;
  105. /// Try to evaluate the expression to the form (a - b + constant) where
  106. /// neither a nor b are variables.
  107. ///
  108. /// This is a more aggressive variant of evaluateAsRelocatable. The intended
  109. /// use is for when relocations are not available, like the .size directive.
  110. bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
  111. /// Find the "associated section" for this expression, which is
  112. /// currently defined as the absolute section for constants, or
  113. /// otherwise the section associated with the first defined symbol in the
  114. /// expression.
  115. MCFragment *findAssociatedFragment() const;
  116. /// @}
  117. };
  118. inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
  119. E.print(OS, nullptr);
  120. return OS;
  121. }
  122. //// Represent a constant integer expression.
  123. class MCConstantExpr : public MCExpr {
  124. int64_t Value;
  125. // Subclass data stores SizeInBytes in bits 0..7 and PrintInHex in bit 8.
  126. static const unsigned SizeInBytesBits = 8;
  127. static const unsigned SizeInBytesMask = (1 << SizeInBytesBits) - 1;
  128. static const unsigned PrintInHexBit = 1 << SizeInBytesBits;
  129. static unsigned encodeSubclassData(bool PrintInHex, unsigned SizeInBytes) {
  130. assert(SizeInBytes <= sizeof(int64_t) && "Excessive size");
  131. return SizeInBytes | (PrintInHex ? PrintInHexBit : 0);
  132. }
  133. MCConstantExpr(int64_t Value, bool PrintInHex, unsigned SizeInBytes)
  134. : MCExpr(MCExpr::Constant, SMLoc(),
  135. encodeSubclassData(PrintInHex, SizeInBytes)), Value(Value) {}
  136. public:
  137. /// \name Construction
  138. /// @{
  139. static const MCConstantExpr *create(int64_t Value, MCContext &Ctx,
  140. bool PrintInHex = false,
  141. unsigned SizeInBytes = 0);
  142. /// @}
  143. /// \name Accessors
  144. /// @{
  145. int64_t getValue() const { return Value; }
  146. unsigned getSizeInBytes() const {
  147. return getSubclassData() & SizeInBytesMask;
  148. }
  149. bool useHexFormat() const { return (getSubclassData() & PrintInHexBit) != 0; }
  150. /// @}
  151. static bool classof(const MCExpr *E) {
  152. return E->getKind() == MCExpr::Constant;
  153. }
  154. };
  155. /// Represent a reference to a symbol from inside an expression.
  156. ///
  157. /// A symbol reference in an expression may be a use of a label, a use of an
  158. /// assembler variable (defined constant), or constitute an implicit definition
  159. /// of the symbol as external.
  160. class MCSymbolRefExpr : public MCExpr {
  161. public:
  162. enum VariantKind : uint16_t {
  163. VK_None,
  164. VK_Invalid,
  165. VK_GOT,
  166. VK_GOTOFF,
  167. VK_GOTREL,
  168. VK_PCREL,
  169. VK_GOTPCREL,
  170. VK_GOTPCREL_NORELAX,
  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. VK_X86_ABS8,
  194. VK_X86_PLTOFF,
  195. VK_ARM_NONE,
  196. VK_ARM_GOT_PREL,
  197. VK_ARM_TARGET1,
  198. VK_ARM_TARGET2,
  199. VK_ARM_PREL31,
  200. VK_ARM_SBREL, // symbol(sbrel)
  201. VK_ARM_TLSLDO, // symbol(tlsldo)
  202. VK_ARM_TLSDESCSEQ,
  203. VK_AVR_NONE,
  204. VK_AVR_LO8,
  205. VK_AVR_HI8,
  206. VK_AVR_HLO8,
  207. VK_AVR_DIFF8,
  208. VK_AVR_DIFF16,
  209. VK_AVR_DIFF32,
  210. VK_AVR_PM,
  211. VK_PPC_LO, // symbol@l
  212. VK_PPC_HI, // symbol@h
  213. VK_PPC_HA, // symbol@ha
  214. VK_PPC_HIGH, // symbol@high
  215. VK_PPC_HIGHA, // symbol@higha
  216. VK_PPC_HIGHER, // symbol@higher
  217. VK_PPC_HIGHERA, // symbol@highera
  218. VK_PPC_HIGHEST, // symbol@highest
  219. VK_PPC_HIGHESTA, // symbol@highesta
  220. VK_PPC_GOT_LO, // symbol@got@l
  221. VK_PPC_GOT_HI, // symbol@got@h
  222. VK_PPC_GOT_HA, // symbol@got@ha
  223. VK_PPC_TOCBASE, // symbol@tocbase
  224. VK_PPC_TOC, // symbol@toc
  225. VK_PPC_TOC_LO, // symbol@toc@l
  226. VK_PPC_TOC_HI, // symbol@toc@h
  227. VK_PPC_TOC_HA, // symbol@toc@ha
  228. VK_PPC_U, // symbol@u
  229. VK_PPC_L, // symbol@l
  230. VK_PPC_DTPMOD, // symbol@dtpmod
  231. VK_PPC_TPREL_LO, // symbol@tprel@l
  232. VK_PPC_TPREL_HI, // symbol@tprel@h
  233. VK_PPC_TPREL_HA, // symbol@tprel@ha
  234. VK_PPC_TPREL_HIGH, // symbol@tprel@high
  235. VK_PPC_TPREL_HIGHA, // symbol@tprel@higha
  236. VK_PPC_TPREL_HIGHER, // symbol@tprel@higher
  237. VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera
  238. VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest
  239. VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta
  240. VK_PPC_DTPREL_LO, // symbol@dtprel@l
  241. VK_PPC_DTPREL_HI, // symbol@dtprel@h
  242. VK_PPC_DTPREL_HA, // symbol@dtprel@ha
  243. VK_PPC_DTPREL_HIGH, // symbol@dtprel@high
  244. VK_PPC_DTPREL_HIGHA, // symbol@dtprel@higha
  245. VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher
  246. VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera
  247. VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest
  248. VK_PPC_DTPREL_HIGHESTA, // symbol@dtprel@highesta
  249. VK_PPC_GOT_TPREL, // symbol@got@tprel
  250. VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l
  251. VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h
  252. VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha
  253. VK_PPC_GOT_DTPREL, // symbol@got@dtprel
  254. VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l
  255. VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h
  256. VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha
  257. VK_PPC_TLS, // symbol@tls
  258. VK_PPC_GOT_TLSGD, // symbol@got@tlsgd
  259. VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l
  260. VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h
  261. VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha
  262. VK_PPC_TLSGD, // symbol@tlsgd
  263. VK_PPC_AIX_TLSGD, // symbol@gd
  264. VK_PPC_AIX_TLSGDM, // symbol@m
  265. VK_PPC_GOT_TLSLD, // symbol@got@tlsld
  266. VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l
  267. VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h
  268. VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha
  269. VK_PPC_GOT_PCREL, // symbol@got@pcrel
  270. VK_PPC_GOT_TLSGD_PCREL, // symbol@got@tlsgd@pcrel
  271. VK_PPC_GOT_TLSLD_PCREL, // symbol@got@tlsld@pcrel
  272. VK_PPC_GOT_TPREL_PCREL, // symbol@got@tprel@pcrel
  273. VK_PPC_TLS_PCREL, // symbol@tls@pcrel
  274. VK_PPC_TLSLD, // symbol@tlsld
  275. VK_PPC_LOCAL, // symbol@local
  276. VK_PPC_NOTOC, // symbol@notoc
  277. VK_PPC_PCREL_OPT, // .reloc expr, R_PPC64_PCREL_OPT, expr
  278. VK_COFF_IMGREL32, // symbol@imgrel (image-relative)
  279. VK_Hexagon_LO16,
  280. VK_Hexagon_HI16,
  281. VK_Hexagon_GPREL,
  282. VK_Hexagon_GD_GOT,
  283. VK_Hexagon_LD_GOT,
  284. VK_Hexagon_GD_PLT,
  285. VK_Hexagon_LD_PLT,
  286. VK_Hexagon_IE,
  287. VK_Hexagon_IE_GOT,
  288. VK_WASM_TYPEINDEX, // Reference to a symbol's type (signature)
  289. VK_WASM_TLSREL, // Memory address relative to __tls_base
  290. VK_WASM_MBREL, // Memory address relative to __memory_base
  291. VK_WASM_TBREL, // Table index relative to __table_base
  292. VK_WASM_GOT_TLS, // Wasm global index of TLS symbol.
  293. VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo
  294. VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi
  295. VK_AMDGPU_REL32_LO, // symbol@rel32@lo
  296. VK_AMDGPU_REL32_HI, // symbol@rel32@hi
  297. VK_AMDGPU_REL64, // symbol@rel64
  298. VK_AMDGPU_ABS32_LO, // symbol@abs32@lo
  299. VK_AMDGPU_ABS32_HI, // symbol@abs32@hi
  300. VK_VE_HI32, // symbol@hi
  301. VK_VE_LO32, // symbol@lo
  302. VK_VE_PC_HI32, // symbol@pc_hi
  303. VK_VE_PC_LO32, // symbol@pc_lo
  304. VK_VE_GOT_HI32, // symbol@got_hi
  305. VK_VE_GOT_LO32, // symbol@got_lo
  306. VK_VE_GOTOFF_HI32, // symbol@gotoff_hi
  307. VK_VE_GOTOFF_LO32, // symbol@gotoff_lo
  308. VK_VE_PLT_HI32, // symbol@plt_hi
  309. VK_VE_PLT_LO32, // symbol@plt_lo
  310. VK_VE_TLS_GD_HI32, // symbol@tls_gd_hi
  311. VK_VE_TLS_GD_LO32, // symbol@tls_gd_lo
  312. VK_VE_TPOFF_HI32, // symbol@tpoff_hi
  313. VK_VE_TPOFF_LO32, // symbol@tpoff_lo
  314. VK_TPREL,
  315. VK_DTPREL
  316. };
  317. private:
  318. /// The symbol being referenced.
  319. const MCSymbol *Symbol;
  320. // Subclass data stores VariantKind in bits 0..15 and HasSubsectionsViaSymbols
  321. // in bit 16.
  322. static const unsigned VariantKindBits = 16;
  323. static const unsigned VariantKindMask = (1 << VariantKindBits) - 1;
  324. // FIXME: Remove this bit.
  325. static const unsigned HasSubsectionsViaSymbolsBit = 1 << VariantKindBits;
  326. static unsigned encodeSubclassData(VariantKind Kind,
  327. bool HasSubsectionsViaSymbols) {
  328. return (unsigned)Kind |
  329. (HasSubsectionsViaSymbols ? HasSubsectionsViaSymbolsBit : 0);
  330. }
  331. explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
  332. const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
  333. public:
  334. /// \name Construction
  335. /// @{
  336. static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
  337. return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
  338. }
  339. static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
  340. MCContext &Ctx, SMLoc Loc = SMLoc());
  341. static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
  342. MCContext &Ctx);
  343. /// @}
  344. /// \name Accessors
  345. /// @{
  346. const MCSymbol &getSymbol() const { return *Symbol; }
  347. VariantKind getKind() const {
  348. return (VariantKind)(getSubclassData() & VariantKindMask);
  349. }
  350. bool hasSubsectionsViaSymbols() const {
  351. return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0;
  352. }
  353. /// @}
  354. /// \name Static Utility Functions
  355. /// @{
  356. static StringRef getVariantKindName(VariantKind Kind);
  357. static VariantKind getVariantKindForName(StringRef Name);
  358. /// @}
  359. static bool classof(const MCExpr *E) {
  360. return E->getKind() == MCExpr::SymbolRef;
  361. }
  362. };
  363. /// Unary assembler expressions.
  364. class MCUnaryExpr : public MCExpr {
  365. public:
  366. enum Opcode {
  367. LNot, ///< Logical negation.
  368. Minus, ///< Unary minus.
  369. Not, ///< Bitwise negation.
  370. Plus ///< Unary plus.
  371. };
  372. private:
  373. const MCExpr *Expr;
  374. MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
  375. : MCExpr(MCExpr::Unary, Loc, Op), Expr(Expr) {}
  376. public:
  377. /// \name Construction
  378. /// @{
  379. static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
  380. MCContext &Ctx, SMLoc Loc = SMLoc());
  381. static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
  382. return create(LNot, Expr, Ctx, Loc);
  383. }
  384. static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
  385. return create(Minus, Expr, Ctx, Loc);
  386. }
  387. static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
  388. return create(Not, Expr, Ctx, Loc);
  389. }
  390. static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
  391. return create(Plus, Expr, Ctx, Loc);
  392. }
  393. /// @}
  394. /// \name Accessors
  395. /// @{
  396. /// Get the kind of this unary expression.
  397. Opcode getOpcode() const { return (Opcode)getSubclassData(); }
  398. /// Get the child of this unary expression.
  399. const MCExpr *getSubExpr() const { return Expr; }
  400. /// @}
  401. static bool classof(const MCExpr *E) {
  402. return E->getKind() == MCExpr::Unary;
  403. }
  404. };
  405. /// Binary assembler expressions.
  406. class MCBinaryExpr : public MCExpr {
  407. public:
  408. enum Opcode {
  409. Add, ///< Addition.
  410. And, ///< Bitwise and.
  411. Div, ///< Signed division.
  412. EQ, ///< Equality comparison.
  413. GT, ///< Signed greater than comparison (result is either 0 or some
  414. ///< target-specific non-zero value)
  415. GTE, ///< Signed greater than or equal comparison (result is either 0 or
  416. ///< some target-specific non-zero value).
  417. LAnd, ///< Logical and.
  418. LOr, ///< Logical or.
  419. LT, ///< Signed less than comparison (result is either 0 or
  420. ///< some target-specific non-zero value).
  421. LTE, ///< Signed less than or equal comparison (result is either 0 or
  422. ///< some target-specific non-zero value).
  423. Mod, ///< Signed remainder.
  424. Mul, ///< Multiplication.
  425. NE, ///< Inequality comparison.
  426. Or, ///< Bitwise or.
  427. OrNot, ///< Bitwise or not.
  428. Shl, ///< Shift left.
  429. AShr, ///< Arithmetic shift right.
  430. LShr, ///< Logical shift right.
  431. Sub, ///< Subtraction.
  432. Xor ///< Bitwise exclusive or.
  433. };
  434. private:
  435. const MCExpr *LHS, *RHS;
  436. MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
  437. SMLoc Loc = SMLoc())
  438. : MCExpr(MCExpr::Binary, Loc, Op), LHS(LHS), RHS(RHS) {}
  439. public:
  440. /// \name Construction
  441. /// @{
  442. static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
  443. const MCExpr *RHS, MCContext &Ctx,
  444. SMLoc Loc = SMLoc());
  445. static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
  446. MCContext &Ctx) {
  447. return create(Add, LHS, RHS, Ctx);
  448. }
  449. static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
  450. MCContext &Ctx) {
  451. return create(And, LHS, RHS, Ctx);
  452. }
  453. static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
  454. MCContext &Ctx) {
  455. return create(Div, LHS, RHS, Ctx);
  456. }
  457. static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
  458. MCContext &Ctx) {
  459. return create(EQ, LHS, RHS, Ctx);
  460. }
  461. static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
  462. MCContext &Ctx) {
  463. return create(GT, LHS, RHS, Ctx);
  464. }
  465. static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
  466. MCContext &Ctx) {
  467. return create(GTE, LHS, RHS, Ctx);
  468. }
  469. static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
  470. MCContext &Ctx) {
  471. return create(LAnd, LHS, RHS, Ctx);
  472. }
  473. static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
  474. MCContext &Ctx) {
  475. return create(LOr, LHS, RHS, Ctx);
  476. }
  477. static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
  478. MCContext &Ctx) {
  479. return create(LT, LHS, RHS, Ctx);
  480. }
  481. static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
  482. MCContext &Ctx) {
  483. return create(LTE, LHS, RHS, Ctx);
  484. }
  485. static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
  486. MCContext &Ctx) {
  487. return create(Mod, LHS, RHS, Ctx);
  488. }
  489. static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
  490. MCContext &Ctx) {
  491. return create(Mul, LHS, RHS, Ctx);
  492. }
  493. static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
  494. MCContext &Ctx) {
  495. return create(NE, LHS, RHS, Ctx);
  496. }
  497. static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
  498. MCContext &Ctx) {
  499. return create(Or, LHS, RHS, Ctx);
  500. }
  501. static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
  502. MCContext &Ctx) {
  503. return create(Shl, LHS, RHS, Ctx);
  504. }
  505. static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
  506. MCContext &Ctx) {
  507. return create(AShr, LHS, RHS, Ctx);
  508. }
  509. static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
  510. MCContext &Ctx) {
  511. return create(LShr, LHS, RHS, Ctx);
  512. }
  513. static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
  514. MCContext &Ctx) {
  515. return create(Sub, LHS, RHS, Ctx);
  516. }
  517. static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
  518. MCContext &Ctx) {
  519. return create(Xor, LHS, RHS, Ctx);
  520. }
  521. /// @}
  522. /// \name Accessors
  523. /// @{
  524. /// Get the kind of this binary expression.
  525. Opcode getOpcode() const { return (Opcode)getSubclassData(); }
  526. /// Get the left-hand side expression of the binary operator.
  527. const MCExpr *getLHS() const { return LHS; }
  528. /// Get the right-hand side expression of the binary operator.
  529. const MCExpr *getRHS() const { return RHS; }
  530. /// @}
  531. static bool classof(const MCExpr *E) {
  532. return E->getKind() == MCExpr::Binary;
  533. }
  534. };
  535. /// This is an extension point for target-specific MCExpr subclasses to
  536. /// implement.
  537. ///
  538. /// NOTE: All subclasses are required to have trivial destructors because
  539. /// MCExprs are bump pointer allocated and not destructed.
  540. class MCTargetExpr : public MCExpr {
  541. virtual void anchor();
  542. protected:
  543. MCTargetExpr() : MCExpr(Target, SMLoc()) {}
  544. virtual ~MCTargetExpr() = default;
  545. public:
  546. virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
  547. virtual bool evaluateAsRelocatableImpl(MCValue &Res,
  548. const MCAsmLayout *Layout,
  549. const MCFixup *Fixup) const = 0;
  550. // allow Target Expressions to be checked for equality
  551. virtual bool isEqualTo(const MCExpr *x) const { return false; }
  552. // This should be set when assigned expressions are not valid ".set"
  553. // expressions, e.g. registers, and must be inlined.
  554. virtual bool inlineAssignedExpr() const { return false; }
  555. virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
  556. virtual MCFragment *findAssociatedFragment() const = 0;
  557. virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
  558. static bool classof(const MCExpr *E) {
  559. return E->getKind() == MCExpr::Target;
  560. }
  561. };
  562. } // end namespace llvm
  563. #endif // LLVM_MC_MCEXPR_H
  564. #ifdef __GNUC__
  565. #pragma GCC diagnostic pop
  566. #endif