MCAsmMacro.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //===- MCAsmMacro.h - Assembly Macros ---------------------------*- 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. #ifndef LLVM_MC_MCASMMACRO_H
  9. #define LLVM_MC_MCASMMACRO_H
  10. #include "llvm/ADT/APInt.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/Debug.h"
  13. #include "llvm/Support/SMLoc.h"
  14. #include <vector>
  15. namespace llvm {
  16. /// Target independent representation for an assembler token.
  17. class AsmToken {
  18. public:
  19. enum TokenKind {
  20. // Markers
  21. Eof, Error,
  22. // String values.
  23. Identifier,
  24. String,
  25. // Integer values.
  26. Integer,
  27. BigNum, // larger than 64 bits
  28. // Real values.
  29. Real,
  30. // Comments
  31. Comment,
  32. HashDirective,
  33. // No-value.
  34. EndOfStatement,
  35. Colon,
  36. Space,
  37. Plus, Minus, Tilde,
  38. Slash, // '/'
  39. BackSlash, // '\'
  40. LParen, RParen, LBrac, RBrac, LCurly, RCurly,
  41. Star, Dot, Comma, Dollar, Equal, EqualEqual,
  42. Pipe, PipePipe, Caret,
  43. Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
  44. Less, LessEqual, LessLess, LessGreater,
  45. Greater, GreaterEqual, GreaterGreater, At, MinusGreater,
  46. // MIPS unary expression operators such as %neg.
  47. PercentCall16, PercentCall_Hi, PercentCall_Lo, PercentDtprel_Hi,
  48. PercentDtprel_Lo, PercentGot, PercentGot_Disp, PercentGot_Hi, PercentGot_Lo,
  49. PercentGot_Ofst, PercentGot_Page, PercentGottprel, PercentGp_Rel, PercentHi,
  50. PercentHigher, PercentHighest, PercentLo, PercentNeg, PercentPcrel_Hi,
  51. PercentPcrel_Lo, PercentTlsgd, PercentTlsldm, PercentTprel_Hi,
  52. PercentTprel_Lo
  53. };
  54. private:
  55. TokenKind Kind;
  56. /// A reference to the entire token contents; this is always a pointer into
  57. /// a memory buffer owned by the source manager.
  58. StringRef Str;
  59. APInt IntVal;
  60. public:
  61. AsmToken() = default;
  62. AsmToken(TokenKind Kind, StringRef Str, APInt IntVal)
  63. : Kind(Kind), Str(Str), IntVal(std::move(IntVal)) {}
  64. AsmToken(TokenKind Kind, StringRef Str, int64_t IntVal = 0)
  65. : Kind(Kind), Str(Str), IntVal(64, IntVal, true) {}
  66. TokenKind getKind() const { return Kind; }
  67. bool is(TokenKind K) const { return Kind == K; }
  68. bool isNot(TokenKind K) const { return Kind != K; }
  69. SMLoc getLoc() const;
  70. SMLoc getEndLoc() const;
  71. SMRange getLocRange() const;
  72. /// Get the contents of a string token (without quotes).
  73. StringRef getStringContents() const {
  74. assert(Kind == String && "This token isn't a string!");
  75. return Str.slice(1, Str.size() - 1);
  76. }
  77. /// Get the identifier string for the current token, which should be an
  78. /// identifier or a string. This gets the portion of the string which should
  79. /// be used as the identifier, e.g., it does not include the quotes on
  80. /// strings.
  81. StringRef getIdentifier() const {
  82. if (Kind == Identifier)
  83. return getString();
  84. return getStringContents();
  85. }
  86. /// Get the string for the current token, this includes all characters (for
  87. /// example, the quotes on strings) in the token.
  88. ///
  89. /// The returned StringRef points into the source manager's memory buffer, and
  90. /// is safe to store across calls to Lex().
  91. StringRef getString() const { return Str; }
  92. // FIXME: Don't compute this in advance, it makes every token larger, and is
  93. // also not generally what we want (it is nicer for recovery etc. to lex 123br
  94. // as a single token, then diagnose as an invalid number).
  95. int64_t getIntVal() const {
  96. assert(Kind == Integer && "This token isn't an integer!");
  97. return IntVal.getZExtValue();
  98. }
  99. APInt getAPIntVal() const {
  100. assert((Kind == Integer || Kind == BigNum) &&
  101. "This token isn't an integer!");
  102. return IntVal;
  103. }
  104. void dump(raw_ostream &OS) const;
  105. };
  106. struct MCAsmMacroParameter {
  107. StringRef Name;
  108. std::vector<AsmToken> Value;
  109. bool Required = false;
  110. bool Vararg = false;
  111. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  112. void dump() const { dump(dbgs()); }
  113. LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
  114. #endif
  115. };
  116. typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
  117. struct MCAsmMacro {
  118. StringRef Name;
  119. StringRef Body;
  120. MCAsmMacroParameters Parameters;
  121. std::vector<std::string> Locals;
  122. bool IsFunction = false;
  123. public:
  124. MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
  125. : Name(N), Body(B), Parameters(std::move(P)) {}
  126. MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P,
  127. std::vector<std::string> L, bool F)
  128. : Name(N), Body(B), Parameters(std::move(P)), Locals(std::move(L)),
  129. IsFunction(F) {}
  130. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  131. void dump() const { dump(dbgs()); }
  132. LLVM_DUMP_METHOD void dump(raw_ostream &OS) const;
  133. #endif
  134. };
  135. } // namespace llvm
  136. #endif