34 : MAI(MAI), CurPtr(nullptr), IsAtStartOfLine(
true),
35 IsAtStartOfStatement(
true), IsParsingMSInlineAsm(
false),
49 CurPtr = CurBuf.
begin();
56 AsmToken AsmLexer::ReturnError(
const char *Loc,
const std::string &Msg) {
62 int AsmLexer::getNextChar() {
63 if (CurPtr == CurBuf.
end())
65 return (
unsigned char)*CurPtr++;
73 AsmToken AsmLexer::LexFloatLiteral() {
75 while (isdigit(*CurPtr))
81 if (*CurPtr ==
'e' || *CurPtr ==
'E') {
83 if (*CurPtr ==
'-' || *CurPtr ==
'+')
85 while (isdigit(*CurPtr))
99 AsmToken AsmLexer::LexHexFloatLiteral(
bool NoIntDigits) {
100 assert((*CurPtr ==
'p' || *CurPtr ==
'P' || *CurPtr ==
'.') &&
101 "unexpected parse state in floating hex");
102 bool NoFracDigits =
true;
105 if (*CurPtr ==
'.') {
108 const char *FracStart = CurPtr;
109 while (isxdigit(*CurPtr))
112 NoFracDigits = CurPtr == FracStart;
115 if (NoIntDigits && NoFracDigits)
116 return ReturnError(
TokStart,
"invalid hexadecimal floating-point constant: "
117 "expected at least one significand digit");
120 if (*CurPtr !=
'p' && *CurPtr !=
'P')
121 return ReturnError(
TokStart,
"invalid hexadecimal floating-point constant: "
122 "expected exponent part 'p'");
125 if (*CurPtr ==
'+' || *CurPtr ==
'-')
129 const char *ExpStart = CurPtr;
130 while (isdigit(*CurPtr))
133 if (CurPtr == ExpStart)
134 return ReturnError(
TokStart,
"invalid hexadecimal floating-point constant: "
135 "expected at least one exponent digit");
142 return isalnum(c) || c ==
'_' || c ==
'$' || c ==
'.' ||
143 (c ==
'@' && AllowAt) || c ==
'?';
146 AsmToken AsmLexer::LexIdentifier() {
148 if (CurPtr[-1] ==
'.' && isdigit(*CurPtr)) {
150 while (isdigit(*CurPtr))
152 if (*CurPtr ==
'e' || *CurPtr ==
'E' ||
154 return LexFloatLiteral();
172 IsAtStartOfStatement =
false;
176 return LexLineComment();
178 IsAtStartOfStatement =
false;
184 const char *CommentTextStart = CurPtr;
185 while (CurPtr != CurBuf.
end()) {
195 StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
202 return ReturnError(
TokStart,
"unterminated comment");
207 AsmToken AsmLexer::LexLineComment() {
212 const char *CommentTextStart = CurPtr;
213 int CurChar = getNextChar();
214 while (CurChar !=
'\n' && CurChar !=
'\r' && CurChar != EOF)
215 CurChar = getNextChar();
221 StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
224 IsAtStartOfLine =
true;
226 if (IsAtStartOfStatement)
229 IsAtStartOfStatement =
true;
237 if (CurPtr[0] ==
'U')
239 if (CurPtr[0] ==
'L')
241 if (CurPtr[0] ==
'L')
247 static unsigned doLookAhead(
const char *&CurPtr,
unsigned DefaultRadix) {
248 const char *FirstHex =
nullptr;
249 const char *LookAhead = CurPtr;
251 if (isdigit(*LookAhead)) {
253 }
else if (isxdigit(*LookAhead)) {
255 FirstHex = LookAhead;
261 bool isHex = *LookAhead ==
'h' || *LookAhead ==
'H';
262 CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
285 if (IsParsingMSInlineAsm && isdigit(CurPtr[-1])) {
286 const char *FirstNonBinary = (CurPtr[-1] !=
'0' && CurPtr[-1] !=
'1') ?
287 CurPtr - 1 :
nullptr;
288 const char *OldCurPtr = CurPtr;
289 while (isxdigit(*CurPtr)) {
290 if (*CurPtr !=
'0' && *CurPtr !=
'1' && !FirstNonBinary)
291 FirstNonBinary = CurPtr;
296 if (*CurPtr ==
'h' || *CurPtr ==
'H') {
300 }
else if (FirstNonBinary && FirstNonBinary + 1 == CurPtr &&
301 (*FirstNonBinary ==
'b' || *FirstNonBinary ==
'B'))
304 if (Radix == 2 || Radix == 16) {
308 if (Result.drop_back().getAsInteger(Radix,
Value))
309 return ReturnError(
TokStart, Radix == 2 ?
"invalid binary number" :
310 "invalid hexdecimal number");
323 if (CurPtr[-1] !=
'0' || CurPtr[0] ==
'.') {
325 bool isHex = Radix == 16;
327 if (!isHex && (*CurPtr ==
'.' || *CurPtr ==
'e')) {
329 return LexFloatLiteral();
335 if (Result.getAsInteger(Radix,
Value))
336 return ReturnError(
TokStart, !isHex ?
"invalid decimal number" :
337 "invalid hexdecimal number");
340 if (Radix == 2 || Radix == 16)
350 if (!IsParsingMSInlineAsm && ((*CurPtr ==
'b') || (*CurPtr ==
'B'))) {
353 if (!isdigit(CurPtr[0])) {
358 const char *NumStart = CurPtr;
359 while (CurPtr[0] ==
'0' || CurPtr[0] ==
'1')
363 if (CurPtr == NumStart)
364 return ReturnError(
TokStart,
"invalid binary number");
369 if (Result.substr(2).getAsInteger(2,
Value))
370 return ReturnError(
TokStart,
"invalid binary number");
379 if ((*CurPtr ==
'x') || (*CurPtr ==
'X')) {
381 const char *NumStart = CurPtr;
382 while (isxdigit(CurPtr[0]))
387 if (CurPtr[0] ==
'.' || CurPtr[0] ==
'p' || CurPtr[0] ==
'P')
388 return LexHexFloatLiteral(NumStart == CurPtr);
391 if (CurPtr == NumStart)
392 return ReturnError(CurPtr-2,
"invalid hexadecimal number");
394 APInt Result(128, 0);
396 return ReturnError(
TokStart,
"invalid hexadecimal number");
399 if (!IsParsingMSInlineAsm && (*CurPtr ==
'h' || *CurPtr ==
'H'))
412 bool isHex = Radix == 16;
414 if (Result.getAsInteger(Radix,
Value))
415 return ReturnError(
TokStart, !isHex ?
"invalid octal number" :
416 "invalid hexdecimal number");
430 AsmToken AsmLexer::LexSingleQuote() {
431 int CurChar = getNextChar();
434 CurChar = getNextChar();
437 return ReturnError(
TokStart,
"unterminated single quote");
439 CurChar = getNextChar();
442 return ReturnError(
TokStart,
"single quote way too long");
450 char theChar = Res[2];
452 default: Value = theChar;
break;
453 case '\'': Value =
'\'';
break;
454 case 't': Value =
'\t';
break;
455 case 'n': Value =
'\n';
break;
456 case 'b': Value =
'\b';
break;
466 int CurChar = getNextChar();
468 while (CurChar !=
'"') {
469 if (CurChar ==
'\\') {
471 CurChar = getNextChar();
475 return ReturnError(
TokStart,
"unterminated string constant");
477 CurChar = getNextChar();
486 while (!isAtStartOfComment(CurPtr) &&
487 !isAtStatementSeparator(CurPtr) &&
488 *CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end()) {
494 StringRef AsmLexer::LexUntilEndOfLine() {
497 while (*CurPtr !=
'\n' && *CurPtr !=
'\r' && CurPtr != CurBuf.
end()) {
504 bool ShouldSkipSpace) {
511 std::string SavedErr =
getErr();
515 for (ReadCount = 0; ReadCount < Buf.
size(); ++ReadCount) {
518 Buf[ReadCount] =
Token;
528 bool AsmLexer::isAtStartOfComment(
const char *
Ptr) {
531 if (CommentString.
size() == 1)
532 return CommentString[0] == Ptr[0];
535 if (CommentString[1] ==
'#')
536 return CommentString[0] == Ptr[0];
538 return strncmp(Ptr, CommentString.
data(), CommentString.
size()) == 0;
541 bool AsmLexer::isAtStatementSeparator(
const char *Ptr) {
549 int CurChar = getNextChar();
551 if (!IsPeeking && CurChar ==
'#' && IsAtStartOfStatement) {
566 return LexLineComment();
570 return LexLineComment();
572 if (isAtStatementSeparator(
TokStart)) {
574 IsAtStartOfLine =
true;
575 IsAtStartOfStatement =
true;
582 if (CurChar == EOF && !IsAtStartOfStatement) {
583 IsAtStartOfLine =
true;
584 IsAtStartOfStatement =
true;
587 IsAtStartOfLine =
false;
588 bool OldIsAtStartOfStatement = IsAtStartOfStatement;
589 IsAtStartOfStatement =
false;
593 if (isalpha(CurChar) || CurChar ==
'_' || CurChar ==
'.')
594 return LexIdentifier();
597 return ReturnError(
TokStart,
"invalid character in input");
599 IsAtStartOfLine =
true;
600 IsAtStartOfStatement =
true;
605 IsAtStartOfStatement = OldIsAtStartOfStatement;
606 while (*CurPtr ==
' ' || *CurPtr ==
'\t')
614 IsAtStartOfLine =
true;
615 IsAtStartOfStatement =
true;
633 if (*CurPtr ==
'=') {
639 if (*CurPtr ==
'|') {
646 if (*CurPtr ==
'&') {
652 if (*CurPtr ==
'=') {
660 unsigned OperatorLength;
662 std::tie(Operator, OperatorLength) =
692 CurPtr += OperatorLength - 1;
698 IsAtStartOfStatement = OldIsAtStartOfStatement;
701 case '\'':
return LexSingleQuote();
702 case '"':
return LexQuote();
703 case '0':
case '1':
case '2':
case '3':
case '4':
704 case '5':
case '6':
case '7':
case '8':
case '9':
AsmCommentConsumer * CommentConsumer
StringRef getCommentString() const
void setBuffer(StringRef Buf, const char *ptr=nullptr)
Target independent representation for an assembler token.
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
This file implements a class to represent arbitrary precision integral constant values and operations...
size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true) override
Look ahead an arbitrary number of tokens.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Function Alias Analysis false
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
size_t size() const
size - Get the array size.
This class is intended to be used as a base class for asm properties and features specific to the tar...
A switch()-like statement whose cases are string literals.
void SetError(SMLoc errLoc, const std::string &err)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
static AsmToken intToken(StringRef Ref, APInt &Value)
const std::string & getErr()
Get the current error string.
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
bool hasMipsExpressions() const
void UnLex(AsmToken const &Token)
This is a utility class that provides an abstraction for the common functionality between Instruction...
bool is(TokenKind K) const
StringRef LexUntilEndOfStatement() override
Class for arbitrary precision integers.
A utility class that uses RAII to save and restore the value of a variable.
static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix)
static SMLoc getFromPointer(const char *Ptr)
static bool startswith(StringRef Magic, const char(&S)[N])
const char * getSeparatorString() const
AsmToken LexToken() override
LexToken - Read the next token and return its code.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
SMLoc getErrLoc()
Get the current error location.
This file provides utility classes that use RAII to save and restore values.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
static void SkipIgnoredIntegerSuffix(const char *&CurPtr)
StringRef - Represent a constant reference to a string, i.e.
Represents a location in source code.
static bool IsIdentifierChar(char c, bool AllowAt)
LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@?]*.