MCAsmLexer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 IsAtStartOfStatement = true;
  48. bool LexMasmHexFloats = false;
  49. bool LexMasmIntegers = false;
  50. bool LexMasmStrings = false;
  51. bool UseMasmDefaultRadix = false;
  52. unsigned DefaultRadix = 10;
  53. AsmCommentConsumer *CommentConsumer = nullptr;
  54. MCAsmLexer();
  55. virtual AsmToken LexToken() = 0;
  56. void SetError(SMLoc errLoc, const std::string &err) {
  57. ErrLoc = errLoc;
  58. Err = err;
  59. }
  60. public:
  61. MCAsmLexer(const MCAsmLexer &) = delete;
  62. MCAsmLexer &operator=(const MCAsmLexer &) = delete;
  63. virtual ~MCAsmLexer();
  64. /// Consume the next token from the input stream and return it.
  65. ///
  66. /// The lexer will continuously return the end-of-file token once the end of
  67. /// the main input file has been reached.
  68. const AsmToken &Lex() {
  69. assert(!CurTok.empty());
  70. // Mark if we parsing out a EndOfStatement.
  71. IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
  72. CurTok.erase(CurTok.begin());
  73. // LexToken may generate multiple tokens via UnLex but will always return
  74. // the first one. Place returned value at head of CurTok vector.
  75. if (CurTok.empty()) {
  76. AsmToken T = LexToken();
  77. CurTok.insert(CurTok.begin(), T);
  78. }
  79. return CurTok.front();
  80. }
  81. void UnLex(AsmToken const &Token) {
  82. IsAtStartOfStatement = false;
  83. CurTok.insert(CurTok.begin(), Token);
  84. }
  85. bool isAtStartOfStatement() { return IsAtStartOfStatement; }
  86. virtual StringRef LexUntilEndOfStatement() = 0;
  87. /// Get the current source location.
  88. SMLoc getLoc() const;
  89. /// Get the current (last) lexed token.
  90. const AsmToken &getTok() const {
  91. return CurTok[0];
  92. }
  93. /// Look ahead at the next token to be lexed.
  94. const AsmToken peekTok(bool ShouldSkipSpace = true) {
  95. AsmToken Tok;
  96. MutableArrayRef<AsmToken> Buf(Tok);
  97. size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
  98. assert(ReadCount == 1);
  99. (void)ReadCount;
  100. return Tok;
  101. }
  102. /// Look ahead an arbitrary number of tokens.
  103. virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
  104. bool ShouldSkipSpace = true) = 0;
  105. /// Get the current error location
  106. SMLoc getErrLoc() {
  107. return ErrLoc;
  108. }
  109. /// Get the current error string
  110. const std::string &getErr() {
  111. return Err;
  112. }
  113. /// Get the kind of current token.
  114. AsmToken::TokenKind getKind() const { return getTok().getKind(); }
  115. /// Check if the current token has kind \p K.
  116. bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
  117. /// Check if the current token has kind \p K.
  118. bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
  119. /// Set whether spaces should be ignored by the lexer
  120. void setSkipSpace(bool val) { SkipSpace = val; }
  121. bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
  122. void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
  123. void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
  124. this->CommentConsumer = CommentConsumer;
  125. }
  126. /// Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified
  127. /// literals (e.g., 0ABCh [hex], 576t [decimal], 77o [octal], 1101y [binary]).
  128. void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
  129. /// Set whether to use masm-style default-radix integer literals. If disabled,
  130. /// assume decimal unless prefixed (e.g., 0x2c [hex], 077 [octal]).
  131. void useMasmDefaultRadix(bool V) { UseMasmDefaultRadix = V; }
  132. unsigned getMasmDefaultRadix() const { return DefaultRadix; }
  133. void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; }
  134. /// Set whether to lex masm-style hex float literals, such as 3f800000r.
  135. void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
  136. /// Set whether to lex masm-style string literals, such as 'Can''t find file'
  137. /// and "This ""value"" not found".
  138. void setLexMasmStrings(bool V) { LexMasmStrings = V; }
  139. };
  140. } // end namespace llvm
  141. #endif // LLVM_MC_MCPARSER_MCASMLEXER_H
  142. #ifdef __GNUC__
  143. #pragma GCC diagnostic pop
  144. #endif