Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

486 lines
15 KiB

  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. #ifndef LLVM_MC_MCEXPR_H
  10. #define LLVM_MC_MCEXPR_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/Support/Casting.h"
  13. #include "llvm/Support/DataTypes.h"
  14. namespace llvm {
  15. class MCAsmLayout;
  16. class MCAssembler;
  17. class MCContext;
  18. class MCSection;
  19. class MCSectionData;
  20. class MCSymbol;
  21. class MCValue;
  22. class raw_ostream;
  23. class StringRef;
  24. typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap;
  25. /// MCExpr - Base class for the full range of assembler expressions which are
  26. /// needed for parsing.
  27. class MCExpr {
  28. public:
  29. enum ExprKind {
  30. Binary, ///< Binary expressions.
  31. Constant, ///< Constant expressions.
  32. SymbolRef, ///< References to labels and assigned expressions.
  33. Unary, ///< Unary expressions.
  34. Target ///< Target specific expression.
  35. };
  36. private:
  37. ExprKind Kind;
  38. MCExpr(const MCExpr&) LLVM_DELETED_FUNCTION;
  39. void operator=(const MCExpr&) LLVM_DELETED_FUNCTION;
  40. bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
  41. const MCAsmLayout *Layout,
  42. const SectionAddrMap *Addrs) const;
  43. protected:
  44. explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
  45. bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
  46. const MCAsmLayout *Layout,
  47. const SectionAddrMap *Addrs,
  48. bool InSet) const;
  49. public:
  50. /// @name Accessors
  51. /// @{
  52. ExprKind getKind() const { return Kind; }
  53. /// @}
  54. /// @name Utility Methods
  55. /// @{
  56. void print(raw_ostream &OS) const;
  57. void dump() const;
  58. /// @}
  59. /// @name Expression Evaluation
  60. /// @{
  61. /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
  62. ///
  63. /// @param Res - The absolute value, if evaluation succeeds.
  64. /// @param Layout - The assembler layout object to use for evaluating symbol
  65. /// values. If not given, then only non-symbolic expressions will be
  66. /// evaluated.
  67. /// @result - True on success.
  68. bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
  69. const SectionAddrMap &Addrs) const;
  70. bool EvaluateAsAbsolute(int64_t &Res) const;
  71. bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
  72. bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
  73. /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
  74. /// value, i.e. an expression of the fixed form (a - b + constant).
  75. ///
  76. /// @param Res - The relocatable value, if evaluation succeeds.
  77. /// @param Layout - The assembler layout object to use for evaluating values.
  78. /// @result - True on success.
  79. bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout &Layout) const;
  80. /// FindAssociatedSection - Find the "associated section" for this expression,
  81. /// which is currently defined as the absolute section for constants, or
  82. /// otherwise the section associated with the first defined symbol in the
  83. /// expression.
  84. const MCSection *FindAssociatedSection() const;
  85. /// @}
  86. };
  87. inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
  88. E.print(OS);
  89. return OS;
  90. }
  91. //// MCConstantExpr - Represent a constant integer expression.
  92. class MCConstantExpr : public MCExpr {
  93. int64_t Value;
  94. explicit MCConstantExpr(int64_t _Value)
  95. : MCExpr(MCExpr::Constant), Value(_Value) {}
  96. public:
  97. /// @name Construction
  98. /// @{
  99. static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
  100. /// @}
  101. /// @name Accessors
  102. /// @{
  103. int64_t getValue() const { return Value; }
  104. /// @}
  105. static bool classof(const MCExpr *E) {
  106. return E->getKind() == MCExpr::Constant;
  107. }
  108. };
  109. /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
  110. /// expression.
  111. ///
  112. /// A symbol reference in an expression may be a use of a label, a use of an
  113. /// assembler variable (defined constant), or constitute an implicit definition
  114. /// of the symbol as external.
  115. class MCSymbolRefExpr : public MCExpr {
  116. public:
  117. enum VariantKind {
  118. VK_None,
  119. VK_Invalid,
  120. VK_GOT,
  121. VK_GOTOFF,
  122. VK_GOTPCREL,
  123. VK_GOTTPOFF,
  124. VK_INDNTPOFF,
  125. VK_NTPOFF,
  126. VK_GOTNTPOFF,
  127. VK_PLT,
  128. VK_TLSGD,
  129. VK_TLSLD,
  130. VK_TLSLDM,
  131. VK_TPOFF,
  132. VK_DTPOFF,
  133. VK_TLVP, // Mach-O thread local variable relocation
  134. VK_SECREL,
  135. // FIXME: We'd really like to use the generic Kinds listed above for these.
  136. VK_ARM_NONE,
  137. VK_ARM_PLT, // ARM-style PLT references. i.e., (PLT) instead of @PLT
  138. VK_ARM_TLSGD, // ditto for TLSGD, GOT, GOTOFF, TPOFF and GOTTPOFF
  139. VK_ARM_GOT,
  140. VK_ARM_GOTOFF,
  141. VK_ARM_TPOFF,
  142. VK_ARM_GOTTPOFF,
  143. VK_ARM_TARGET1,
  144. VK_ARM_TARGET2,
  145. VK_ARM_PREL31,
  146. VK_PPC_TOC, // TOC base
  147. VK_PPC_TOC_ENTRY, // TOC entry
  148. VK_PPC_DARWIN_HA16, // ha16(symbol)
  149. VK_PPC_DARWIN_LO16, // lo16(symbol)
  150. VK_PPC_GAS_HA16, // symbol@ha
  151. VK_PPC_GAS_LO16, // symbol@l
  152. VK_PPC_TPREL16_HA, // symbol@tprel@ha
  153. VK_PPC_TPREL16_LO, // symbol@tprel@l
  154. VK_PPC_DTPREL16_HA, // symbol@dtprel@ha
  155. VK_PPC_DTPREL16_LO, // symbol@dtprel@l
  156. VK_PPC_TOC16_HA, // symbol@toc@ha
  157. VK_PPC_TOC16_LO, // symbol@toc@l
  158. VK_PPC_GOT_TPREL16_HA, // symbol@got@tprel@ha
  159. VK_PPC_GOT_TPREL16_LO, // symbol@got@tprel@l
  160. VK_PPC_TLS, // symbol@tls
  161. VK_PPC_GOT_TLSGD16_HA, // symbol@got@tlsgd@ha
  162. VK_PPC_GOT_TLSGD16_LO, // symbol@got@tlsgd@l
  163. VK_PPC_TLSGD, // symbol@tlsgd
  164. VK_PPC_GOT_TLSLD16_HA, // symbol@got@tlsld@ha
  165. VK_PPC_GOT_TLSLD16_LO, // symbol@got@tlsld@l
  166. VK_PPC_TLSLD, // symbol@tlsld
  167. VK_Mips_GPREL,
  168. VK_Mips_GOT_CALL,
  169. VK_Mips_GOT16,
  170. VK_Mips_GOT,
  171. VK_Mips_ABS_HI,
  172. VK_Mips_ABS_LO,
  173. VK_Mips_TLSGD,
  174. VK_Mips_TLSLDM,
  175. VK_Mips_DTPREL_HI,
  176. VK_Mips_DTPREL_LO,
  177. VK_Mips_GOTTPREL,
  178. VK_Mips_TPREL_HI,
  179. VK_Mips_TPREL_LO,
  180. VK_Mips_GPOFF_HI,
  181. VK_Mips_GPOFF_LO,
  182. VK_Mips_GOT_DISP,
  183. VK_Mips_GOT_PAGE,
  184. VK_Mips_GOT_OFST,
  185. VK_Mips_HIGHER,
  186. VK_Mips_HIGHEST,
  187. VK_Mips_GOT_HI16,
  188. VK_Mips_GOT_LO16,
  189. VK_Mips_CALL_HI16,
  190. VK_Mips_CALL_LO16,
  191. VK_COFF_IMGREL32 // symbol@imgrel (image-relative)
  192. };
  193. private:
  194. /// The symbol being referenced.
  195. const MCSymbol *Symbol;
  196. /// The symbol reference modifier.
  197. const VariantKind Kind;
  198. explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind)
  199. : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind) {
  200. assert(Symbol);
  201. }
  202. public:
  203. /// @name Construction
  204. /// @{
  205. static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
  206. return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
  207. }
  208. static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
  209. MCContext &Ctx);
  210. static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
  211. MCContext &Ctx);
  212. /// @}
  213. /// @name Accessors
  214. /// @{
  215. const MCSymbol &getSymbol() const { return *Symbol; }
  216. VariantKind getKind() const { return Kind; }
  217. /// @}
  218. /// @name Static Utility Functions
  219. /// @{
  220. static StringRef getVariantKindName(VariantKind Kind);
  221. static VariantKind getVariantKindForName(StringRef Name);
  222. /// @}
  223. static bool classof(const MCExpr *E) {
  224. return E->getKind() == MCExpr::SymbolRef;
  225. }
  226. };
  227. /// MCUnaryExpr - Unary assembler expressions.
  228. class MCUnaryExpr : public MCExpr {
  229. public:
  230. enum Opcode {
  231. LNot, ///< Logical negation.
  232. Minus, ///< Unary minus.
  233. Not, ///< Bitwise negation.
  234. Plus ///< Unary plus.
  235. };
  236. private:
  237. Opcode Op;
  238. const MCExpr *Expr;
  239. MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
  240. : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
  241. public:
  242. /// @name Construction
  243. /// @{
  244. static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
  245. MCContext &Ctx);
  246. static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
  247. return Create(LNot, Expr, Ctx);
  248. }
  249. static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
  250. return Create(Minus, Expr, Ctx);
  251. }
  252. static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
  253. return Create(Not, Expr, Ctx);
  254. }
  255. static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
  256. return Create(Plus, Expr, Ctx);
  257. }
  258. /// @}
  259. /// @name Accessors
  260. /// @{
  261. /// getOpcode - Get the kind of this unary expression.
  262. Opcode getOpcode() const { return Op; }
  263. /// getSubExpr - Get the child of this unary expression.
  264. const MCExpr *getSubExpr() const { return Expr; }
  265. /// @}
  266. static bool classof(const MCExpr *E) {
  267. return E->getKind() == MCExpr::Unary;
  268. }
  269. };
  270. /// MCBinaryExpr - Binary assembler expressions.
  271. class MCBinaryExpr : public MCExpr {
  272. public:
  273. enum Opcode {
  274. Add, ///< Addition.
  275. And, ///< Bitwise and.
  276. Div, ///< Signed division.
  277. EQ, ///< Equality comparison.
  278. GT, ///< Signed greater than comparison (result is either 0 or some
  279. ///< target-specific non-zero value)
  280. GTE, ///< Signed greater than or equal comparison (result is either 0 or
  281. ///< some target-specific non-zero value).
  282. LAnd, ///< Logical and.
  283. LOr, ///< Logical or.
  284. LT, ///< Signed less than comparison (result is either 0 or
  285. ///< some target-specific non-zero value).
  286. LTE, ///< Signed less than or equal comparison (result is either 0 or
  287. ///< some target-specific non-zero value).
  288. Mod, ///< Signed remainder.
  289. Mul, ///< Multiplication.
  290. NE, ///< Inequality comparison.
  291. Or, ///< Bitwise or.
  292. Shl, ///< Shift left.
  293. Shr, ///< Shift right (arithmetic or logical, depending on target)
  294. Sub, ///< Subtraction.
  295. Xor ///< Bitwise exclusive or.
  296. };
  297. private:
  298. Opcode Op;
  299. const MCExpr *LHS, *RHS;
  300. MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
  301. : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
  302. public:
  303. /// @name Construction
  304. /// @{
  305. static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
  306. const MCExpr *RHS, MCContext &Ctx);
  307. static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
  308. MCContext &Ctx) {
  309. return Create(Add, LHS, RHS, Ctx);
  310. }
  311. static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
  312. MCContext &Ctx) {
  313. return Create(And, LHS, RHS, Ctx);
  314. }
  315. static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
  316. MCContext &Ctx) {
  317. return Create(Div, LHS, RHS, Ctx);
  318. }
  319. static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
  320. MCContext &Ctx) {
  321. return Create(EQ, LHS, RHS, Ctx);
  322. }
  323. static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
  324. MCContext &Ctx) {
  325. return Create(GT, LHS, RHS, Ctx);
  326. }
  327. static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
  328. MCContext &Ctx) {
  329. return Create(GTE, LHS, RHS, Ctx);
  330. }
  331. static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
  332. MCContext &Ctx) {
  333. return Create(LAnd, LHS, RHS, Ctx);
  334. }
  335. static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
  336. MCContext &Ctx) {
  337. return Create(LOr, LHS, RHS, Ctx);
  338. }
  339. static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
  340. MCContext &Ctx) {
  341. return Create(LT, LHS, RHS, Ctx);
  342. }
  343. static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
  344. MCContext &Ctx) {
  345. return Create(LTE, LHS, RHS, Ctx);
  346. }
  347. static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
  348. MCContext &Ctx) {
  349. return Create(Mod, LHS, RHS, Ctx);
  350. }
  351. static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
  352. MCContext &Ctx) {
  353. return Create(Mul, LHS, RHS, Ctx);
  354. }
  355. static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
  356. MCContext &Ctx) {
  357. return Create(NE, LHS, RHS, Ctx);
  358. }
  359. static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
  360. MCContext &Ctx) {
  361. return Create(Or, LHS, RHS, Ctx);
  362. }
  363. static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
  364. MCContext &Ctx) {
  365. return Create(Shl, LHS, RHS, Ctx);
  366. }
  367. static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
  368. MCContext &Ctx) {
  369. return Create(Shr, LHS, RHS, Ctx);
  370. }
  371. static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
  372. MCContext &Ctx) {
  373. return Create(Sub, LHS, RHS, Ctx);
  374. }
  375. static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
  376. MCContext &Ctx) {
  377. return Create(Xor, LHS, RHS, Ctx);
  378. }
  379. /// @}
  380. /// @name Accessors
  381. /// @{
  382. /// getOpcode - Get the kind of this binary expression.
  383. Opcode getOpcode() const { return Op; }
  384. /// getLHS - Get the left-hand side expression of the binary operator.
  385. const MCExpr *getLHS() const { return LHS; }
  386. /// getRHS - Get the right-hand side expression of the binary operator.
  387. const MCExpr *getRHS() const { return RHS; }
  388. /// @}
  389. static bool classof(const MCExpr *E) {
  390. return E->getKind() == MCExpr::Binary;
  391. }
  392. };
  393. /// MCTargetExpr - This is an extension point for target-specific MCExpr
  394. /// subclasses to implement.
  395. ///
  396. /// NOTE: All subclasses are required to have trivial destructors because
  397. /// MCExprs are bump pointer allocated and not destructed.
  398. class MCTargetExpr : public MCExpr {
  399. virtual void anchor();
  400. protected:
  401. MCTargetExpr() : MCExpr(Target) {}
  402. virtual ~MCTargetExpr() {}
  403. public:
  404. virtual void PrintImpl(raw_ostream &OS) const = 0;
  405. virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
  406. const MCAsmLayout *Layout) const = 0;
  407. virtual void AddValueSymbols(MCAssembler *) const = 0;
  408. virtual const MCSection *FindAssociatedSection() const = 0;
  409. virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
  410. static bool classof(const MCExpr *E) {
  411. return E->getKind() == MCExpr::Target;
  412. }
  413. };
  414. } // end namespace llvm
  415. #endif