MCAsmLexer.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface ------*- 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_MCPARSER_MCASMLEXER_H
  14. #define LLVM_MC_MCPARSER_MCASMLEXER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/MC/MCAsmMacro.h"
  18. #include <algorithm>
  19. #include <cassert>
  20. #include <cstddef>
  21. #include <cstdint>
  22. #include <string>
  23. namespace llvm {
  24. /// A callback class which is notified of each comment in an assembly file as
  25. /// it is lexed.
  26. class AsmCommentConsumer {
  27. public:
  28. virtual ~AsmCommentConsumer() = default;
  29. /// Callback function for when a comment is lexed. Loc is the start of the
  30. /// comment text (excluding the comment-start marker). CommentText is the text
  31. /// of the comment, excluding the comment start and end markers, and the
  32. /// newline for single-line comments.
  33. virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
  34. };
  35. /// Generic assembler lexer interface, for use by target specific assembly
  36. /// lexers.
  37. class MCAsmLexer {
  38. /// The current token, stored in the base class for faster access.
  39. SmallVector<AsmToken, 1> CurTok;
  40. /// The location and description of the current error
  41. SMLoc ErrLoc;
  42. std::string Err;
  43. protected: // Can only create subclasses.
  44. const char *TokStart = nullptr;
  45. bool SkipSpace = true;
  46. bool AllowAtInIdentifier;
  47. bool AllowHashInIdentifier = false;
  48. bool IsAtStartOfStatement = true;
  49. bool LexMasmHexFloats = false;
  50. bool LexMasmIntegers = false;
  51. bool LexMasmStrings = false;
  52. bool LexMotorolaIntegers = false;
  53. bool UseMasmDefaultRadix = false;
  54. unsigned DefaultRadix = 10;
  55. bool LexHLASMIntegers = false;
  56. bool LexHLASMStrings = false;
  57. AsmCommentConsumer *CommentConsumer = nullptr;
  58. MCAsmLexer();
  59. virtual AsmToken LexToken() = 0;
  60. void SetError(SMLoc errLoc, const std::string &err) {
  61. ErrLoc = errLoc;
  62. Err = err;
  63. }
  64. public:
  65. MCAsmLexer(const MCAsmLexer &) = delete;
  66. MCAsmLexer &operator=(const MCAsmLexer &) = delete;
  67. virtual ~MCAsmLexer();
  68. /// Consume the next token from the input stream and return it.
  69. ///
  70. /// The lexer will continuously return the end-of-file token once the end of
  71. /// the main input file has been reached.
  72. const AsmToken &Lex() {
  73. assert(!CurTok.empty());
  74. // Mark if we parsing out a EndOfStatement.
  75. IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
  76. CurTok.erase(CurTok.begin());
  77. // LexToken may generate multiple tokens via UnLex but will always return
  78. // the first one. Place returned value at head of CurTok vector.
  79. if (CurTok.empty()) {
  80. AsmToken T = LexToken();
  81. CurTok.insert(CurTok.begin(), T);
  82. }
  83. return CurTok.front();
  84. }
  85. void UnLex(AsmToken const &Token) {
  86. IsAtStartOfStatement = false;
  87. CurTok.insert(CurTok.begin(), Token);
  88. }
  89. bool isAtStartOfStatement() { return IsAtStartOfStatement; }
  90. virtual StringRef LexUntilEndOfStatement() = 0;
  91. /// Get the current source location.
  92. SMLoc getLoc() const;
  93. /// Get the current (last) lexed token.
  94. const AsmToken &getTok() const {
  95. return CurTok[0];
  96. }
  97. /// Look ahead at the next token to be lexed.
  98. const AsmToken peekTok(bool ShouldSkipSpace = true) {
  99. AsmToken Tok;
  100. MutableArrayRef<AsmToken> Buf(Tok);
  101. size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
  102. assert(ReadCount == 1);
  103. (void)ReadCount;
  104. return Tok;
  105. }
  106. /// Look ahead an arbitrary number of tokens.
  107. virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
  108. bool ShouldSkipSpace = true) = 0;
  109. /// Get the current error location
  110. SMLoc getErrLoc() {
  111. return ErrLoc;
  112. }
  113. /// Get the current error string
  114. const std::string &getErr() {
  115. return Err;
  116. }
  117. /// Get the kind of current token.
  118. AsmToken::TokenKind getKind() const { return getTok().getKind(); }
  119. /// Check if the current token has kind \p K.
  120. bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
  121. /// Check if the current token has kind \p K.
  122. bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
  123. /// Set whether spaces should be ignored by the lexer
  124. void setSkipSpace(bool val) { SkipSpace = val; }
  125. bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
  126. void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
  127. void setAllowHashInIdentifier(bool V) { AllowHashInIdentifier = V; }
  128. void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
  129. this->CommentConsumer = CommentConsumer;
  130. }
  131. /// Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified
  132. /// literals (e.g., 0ABCh [hex], 576t [decimal], 77o [octal], 1101y [binary]).
  133. void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
  134. /// Set whether to use masm-style default-radix integer literals. If disabled,
  135. /// assume decimal unless prefixed (e.g., 0x2c [hex], 077 [octal]).
  136. void useMasmDefaultRadix(bool V) { UseMasmDefaultRadix = V; }
  137. unsigned getMasmDefaultRadix() const { return DefaultRadix; }
  138. void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; }
  139. /// Set whether to lex masm-style hex float literals, such as 3f800000r.
  140. void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
  141. /// Set whether to lex masm-style string literals, such as 'Can''t find file'
  142. /// and "This ""value"" not found".
  143. void setLexMasmStrings(bool V) { LexMasmStrings = V; }
  144. /// Set whether to lex Motorola-style integer literals, such as $deadbeef or
  145. /// %01010110.
  146. void setLexMotorolaIntegers(bool V) { LexMotorolaIntegers = V; }
  147. /// Set whether to lex HLASM-flavour integers. For now this is only [0-9]*
  148. void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; }
  149. /// Set whether to "lex" HLASM-flavour character and string literals. For now,
  150. /// setting this option to true, will disable lexing for character and string
  151. /// literals.
  152. void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
  153. };
  154. } // end namespace llvm
  155. #endif // LLVM_MC_MCPARSER_MCASMLEXER_H
  156. #ifdef __GNUC__
  157. #pragma GCC diagnostic pop
  158. #endif