MCValue.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //===-- llvm/MC/MCValue.h - MCValue class -----------------------*- 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. // This file contains the declaration of the MCValue class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCVALUE_H
  14. #define LLVM_MC_MCVALUE_H
  15. #include "llvm/MC/MCExpr.h"
  16. #include "llvm/MC/MCSymbol.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include <cassert>
  19. namespace llvm {
  20. class MCAsmInfo;
  21. class raw_ostream;
  22. /// \brief This represents an "assembler immediate".
  23. ///
  24. /// In its most general form, this can hold ":Kind:(SymbolA - SymbolB +
  25. /// imm64)". Not all targets supports relocations of this general form, but we
  26. /// need to represent this anyway.
  27. ///
  28. /// In general both SymbolA and SymbolB will also have a modifier
  29. /// analogous to the top-level Kind. Current targets are not expected
  30. /// to make use of both though. The choice comes down to whether
  31. /// relocation modifiers apply to the closest symbol or the whole
  32. /// expression.
  33. ///
  34. /// In the general form, SymbolB can only be defined if SymbolA is, and both
  35. /// must be in the same (non-external) section. The latter constraint is not
  36. /// enforced, since a symbol's section may not be known at construction.
  37. ///
  38. /// Note that this class must remain a simple POD value class, because we need
  39. /// it to live in unions etc.
  40. class MCValue {
  41. const MCSymbolRefExpr *SymA, *SymB;
  42. int64_t Cst;
  43. uint32_t RefKind;
  44. public:
  45. int64_t getConstant() const { return Cst; }
  46. const MCSymbolRefExpr *getSymA() const { return SymA; }
  47. const MCSymbolRefExpr *getSymB() const { return SymB; }
  48. uint32_t getRefKind() const { return RefKind; }
  49. /// \brief Is this an absolute (as opposed to relocatable) value.
  50. bool isAbsolute() const { return !SymA && !SymB; }
  51. /// \brief Print the value to the stream \p OS.
  52. void print(raw_ostream &OS) const;
  53. /// \brief Print the value to stderr.
  54. void dump() const;
  55. MCSymbolRefExpr::VariantKind getAccessVariant() const;
  56. static MCValue get(const MCSymbolRefExpr *SymA,
  57. const MCSymbolRefExpr *SymB = nullptr,
  58. int64_t Val = 0, uint32_t RefKind = 0) {
  59. MCValue R;
  60. assert((!SymB || SymA) && "Invalid relocatable MCValue!");
  61. R.Cst = Val;
  62. R.SymA = SymA;
  63. R.SymB = SymB;
  64. R.RefKind = RefKind;
  65. return R;
  66. }
  67. static MCValue get(int64_t Val) {
  68. MCValue R;
  69. R.Cst = Val;
  70. R.SymA = nullptr;
  71. R.SymB = nullptr;
  72. R.RefKind = 0;
  73. return R;
  74. }
  75. };
  76. } // end namespace llvm
  77. #endif