MCAsmMacro.h 5.0 KB

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