Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_MC_MCPARSER_MCASMLEXER_H
0010 #define LLVM_MC_MCPARSER_MCASMLEXER_H
0011 
0012 #include "llvm/ADT/ArrayRef.h"
0013 #include "llvm/ADT/SmallVector.h"
0014 #include "llvm/MC/MCAsmMacro.h"
0015 #include <cassert>
0016 #include <cstddef>
0017 #include <string>
0018 
0019 namespace llvm {
0020 
0021 /// A callback class which is notified of each comment in an assembly file as
0022 /// it is lexed.
0023 class AsmCommentConsumer {
0024 public:
0025   virtual ~AsmCommentConsumer() = default;
0026 
0027   /// Callback function for when a comment is lexed. Loc is the start of the
0028   /// comment text (excluding the comment-start marker). CommentText is the text
0029   /// of the comment, excluding the comment start and end markers, and the
0030   /// newline for single-line comments.
0031   virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
0032 };
0033 
0034 
0035 /// Generic assembler lexer interface, for use by target specific assembly
0036 /// lexers.
0037 class MCAsmLexer {
0038   /// The current token, stored in the base class for faster access.
0039   SmallVector<AsmToken, 1> CurTok;
0040 
0041   /// The location and description of the current error
0042   SMLoc ErrLoc;
0043   std::string Err;
0044 
0045 protected: // Can only create subclasses.
0046   const char *TokStart = nullptr;
0047   bool SkipSpace = true;
0048   bool AllowAtInIdentifier = false;
0049   bool AllowHashInIdentifier = false;
0050   bool IsAtStartOfStatement = true;
0051   bool LexMasmHexFloats = false;
0052   bool LexMasmIntegers = false;
0053   bool LexMasmStrings = false;
0054   bool LexMotorolaIntegers = false;
0055   bool UseMasmDefaultRadix = false;
0056   unsigned DefaultRadix = 10;
0057   bool LexHLASMIntegers = false;
0058   bool LexHLASMStrings = false;
0059   AsmCommentConsumer *CommentConsumer = nullptr;
0060 
0061   MCAsmLexer();
0062 
0063   virtual AsmToken LexToken() = 0;
0064 
0065   void SetError(SMLoc errLoc, const std::string &err) {
0066     ErrLoc = errLoc;
0067     Err = err;
0068   }
0069 
0070 public:
0071   MCAsmLexer(const MCAsmLexer &) = delete;
0072   MCAsmLexer &operator=(const MCAsmLexer &) = delete;
0073   virtual ~MCAsmLexer();
0074 
0075   /// Consume the next token from the input stream and return it.
0076   ///
0077   /// The lexer will continuously return the end-of-file token once the end of
0078   /// the main input file has been reached.
0079   const AsmToken &Lex() {
0080     assert(!CurTok.empty());
0081     // Mark if we parsing out a EndOfStatement.
0082     IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement;
0083     CurTok.erase(CurTok.begin());
0084     // LexToken may generate multiple tokens via UnLex but will always return
0085     // the first one. Place returned value at head of CurTok vector.
0086     if (CurTok.empty()) {
0087       AsmToken T = LexToken();
0088       CurTok.insert(CurTok.begin(), T);
0089     }
0090     return CurTok.front();
0091   }
0092 
0093   void UnLex(AsmToken const &Token) {
0094     IsAtStartOfStatement = false;
0095     CurTok.insert(CurTok.begin(), Token);
0096   }
0097 
0098   bool isAtStartOfStatement() { return IsAtStartOfStatement; }
0099 
0100   virtual StringRef LexUntilEndOfStatement() = 0;
0101 
0102   /// Get the current source location.
0103   SMLoc getLoc() const;
0104 
0105   /// Get the current (last) lexed token.
0106   const AsmToken &getTok() const {
0107     return CurTok[0];
0108   }
0109 
0110   /// Look ahead at the next token to be lexed.
0111   const AsmToken peekTok(bool ShouldSkipSpace = true) {
0112     AsmToken Tok;
0113 
0114     MutableArrayRef<AsmToken> Buf(Tok);
0115     size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
0116 
0117     assert(ReadCount == 1);
0118     (void)ReadCount;
0119 
0120     return Tok;
0121   }
0122 
0123   /// Look ahead an arbitrary number of tokens.
0124   virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
0125                             bool ShouldSkipSpace = true) = 0;
0126 
0127   /// Get the current error location
0128   SMLoc getErrLoc() {
0129     return ErrLoc;
0130   }
0131 
0132   /// Get the current error string
0133   const std::string &getErr() {
0134     return Err;
0135   }
0136 
0137   /// Get the kind of current token.
0138   AsmToken::TokenKind getKind() const { return getTok().getKind(); }
0139 
0140   /// Check if the current token has kind \p K.
0141   bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
0142 
0143   /// Check if the current token has kind \p K.
0144   bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
0145 
0146   /// Set whether spaces should be ignored by the lexer
0147   void setSkipSpace(bool val) { SkipSpace = val; }
0148 
0149   bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
0150   void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
0151 
0152   void setAllowHashInIdentifier(bool V) { AllowHashInIdentifier = V; }
0153 
0154   void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
0155     this->CommentConsumer = CommentConsumer;
0156   }
0157 
0158   /// Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified
0159   /// literals (e.g., 0ABCh [hex], 576t [decimal], 77o [octal], 1101y [binary]).
0160   void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
0161 
0162   /// Set whether to use masm-style default-radix integer literals. If disabled,
0163   /// assume decimal unless prefixed (e.g., 0x2c [hex], 077 [octal]).
0164   void useMasmDefaultRadix(bool V) { UseMasmDefaultRadix = V; }
0165 
0166   unsigned getMasmDefaultRadix() const { return DefaultRadix; }
0167   void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; }
0168 
0169   /// Set whether to lex masm-style hex float literals, such as 3f800000r.
0170   void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
0171 
0172   /// Set whether to lex masm-style string literals, such as 'Can''t find file'
0173   /// and "This ""value"" not found".
0174   void setLexMasmStrings(bool V) { LexMasmStrings = V; }
0175 
0176   /// Set whether to lex Motorola-style integer literals, such as $deadbeef or
0177   /// %01010110.
0178   void setLexMotorolaIntegers(bool V) { LexMotorolaIntegers = V; }
0179 
0180   /// Set whether to lex HLASM-flavour integers. For now this is only [0-9]*
0181   void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; }
0182 
0183   /// Set whether to "lex" HLASM-flavour character and string literals. For now,
0184   /// setting this option to true, will disable lexing for character and string
0185   /// literals.
0186   void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
0187 };
0188 
0189 } // end namespace llvm
0190 
0191 #endif // LLVM_MC_MCPARSER_MCASMLEXER_H