1 //===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class implements the lexer for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/MC/MCParser/AsmLexer.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCParser/MCAsmLexer.h"
22 #include "llvm/Support/SMLoc.h"
23 #include "llvm/Support/SaveAndRestore.h"
24 #include <cassert>
25 #include <cctype>
26 #include <cstdio>
27 #include <cstring>
28 #include <string>
29 #include <tuple>
30 #include <utility>
31 
32 using namespace llvm;
33 
34 AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
35   AllowAtInIdentifier = !StringRef(MAI.getCommentString()).startswith("@");
36 }
37 
38 AsmLexer::~AsmLexer() = default;
39 
40 void AsmLexer::setBuffer(StringRef Buf, const char *ptr) {
41   CurBuf = Buf;
42 
43   if (ptr)
44     CurPtr = ptr;
45   else
46     CurPtr = CurBuf.begin();
47 
48   TokStart = nullptr;
49 }
50 
51 /// ReturnError - Set the error to the specified string at the specified
52 /// location.  This is defined to always return AsmToken::Error.
53 AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
54   SetError(SMLoc::getFromPointer(Loc), Msg);
55 
56   return AsmToken(AsmToken::Error, StringRef(Loc, CurPtr - Loc));
57 }
58 
59 int AsmLexer::getNextChar() {
60   if (CurPtr == CurBuf.end())
61     return EOF;
62   return (unsigned char)*CurPtr++;
63 }
64 
65 /// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
66 ///
67 /// The leading integral digit sequence and dot should have already been
68 /// consumed, some or all of the fractional digit sequence *can* have been
69 /// consumed.
70 AsmToken AsmLexer::LexFloatLiteral() {
71   // Skip the fractional digit sequence.
72   while (isDigit(*CurPtr))
73     ++CurPtr;
74 
75   // Check for exponent; we intentionally accept a slighlty wider set of
76   // literals here and rely on the upstream client to reject invalid ones (e.g.,
77   // "1e+").
78   if (*CurPtr == 'e' || *CurPtr == 'E') {
79     ++CurPtr;
80     if (*CurPtr == '-' || *CurPtr == '+')
81       ++CurPtr;
82     while (isDigit(*CurPtr))
83       ++CurPtr;
84   }
85 
86   return AsmToken(AsmToken::Real,
87                   StringRef(TokStart, CurPtr - TokStart));
88 }
89 
90 /// LexHexFloatLiteral matches essentially (.[0-9a-fA-F]*)?[pP][+-]?[0-9a-fA-F]+
91 /// while making sure there are enough actual digits around for the constant to
92 /// be valid.
93 ///
94 /// The leading "0x[0-9a-fA-F]*" (i.e. integer part) has already been consumed
95 /// before we get here.
96 AsmToken AsmLexer::LexHexFloatLiteral(bool NoIntDigits) {
97   assert((*CurPtr == 'p' || *CurPtr == 'P' || *CurPtr == '.') &&
98          "unexpected parse state in floating hex");
99   bool NoFracDigits = true;
100 
101   // Skip the fractional part if there is one
102   if (*CurPtr == '.') {
103     ++CurPtr;
104 
105     const char *FracStart = CurPtr;
106     while (isHexDigit(*CurPtr))
107       ++CurPtr;
108 
109     NoFracDigits = CurPtr == FracStart;
110   }
111 
112   if (NoIntDigits && NoFracDigits)
113     return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
114                                  "expected at least one significand digit");
115 
116   // Make sure we do have some kind of proper exponent part
117   if (*CurPtr != 'p' && *CurPtr != 'P')
118     return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
119                                  "expected exponent part 'p'");
120   ++CurPtr;
121 
122   if (*CurPtr == '+' || *CurPtr == '-')
123     ++CurPtr;
124 
125   // N.b. exponent digits are *not* hex
126   const char *ExpStart = CurPtr;
127   while (isDigit(*CurPtr))
128     ++CurPtr;
129 
130   if (CurPtr == ExpStart)
131     return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
132                                  "expected at least one exponent digit");
133 
134   return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
135 }
136 
137 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@?]*
138 static bool IsIdentifierChar(char c, bool AllowAt) {
139   return isAlnum(c) || c == '_' || c == '$' || c == '.' ||
140          (c == '@' && AllowAt) || c == '?';
141 }
142 
143 AsmToken AsmLexer::LexIdentifier() {
144   // Check for floating point literals.
145   if (CurPtr[-1] == '.' && isDigit(*CurPtr)) {
146     // Disambiguate a .1243foo identifier from a floating literal.
147     while (isDigit(*CurPtr))
148       ++CurPtr;
149     if (*CurPtr == 'e' || *CurPtr == 'E' ||
150         !IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
151       return LexFloatLiteral();
152   }
153 
154   while (IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
155     ++CurPtr;
156 
157   // Handle . as a special case.
158   if (CurPtr == TokStart+1 && TokStart[0] == '.')
159     return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
160 
161   return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
162 }
163 
164 /// LexSlash: Slash: /
165 ///           C-Style Comment: /* ... */
166 AsmToken AsmLexer::LexSlash() {
167   switch (*CurPtr) {
168   case '*':
169     IsAtStartOfStatement = false;
170     break; // C style comment.
171   case '/':
172     ++CurPtr;
173     return LexLineComment();
174   default:
175     IsAtStartOfStatement = false;
176     return AsmToken(AsmToken::Slash, StringRef(TokStart, 1));
177   }
178 
179   // C Style comment.
180   ++CurPtr;  // skip the star.
181   const char *CommentTextStart = CurPtr;
182   while (CurPtr != CurBuf.end()) {
183     switch (*CurPtr++) {
184     case '*':
185       // End of the comment?
186       if (*CurPtr != '/')
187         break;
188       // If we have a CommentConsumer, notify it about the comment.
189       if (CommentConsumer) {
190         CommentConsumer->HandleComment(
191             SMLoc::getFromPointer(CommentTextStart),
192             StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
193       }
194       ++CurPtr;   // End the */.
195       return AsmToken(AsmToken::Comment,
196                       StringRef(TokStart, CurPtr - TokStart));
197     }
198   }
199   return ReturnError(TokStart, "unterminated comment");
200 }
201 
202 /// LexLineComment: Comment: #[^\n]*
203 ///                        : //[^\n]*
204 AsmToken AsmLexer::LexLineComment() {
205   // Mark This as an end of statement with a body of the
206   // comment. While it would be nicer to leave this two tokens,
207   // backwards compatability with TargetParsers makes keeping this in this form
208   // better.
209   const char *CommentTextStart = CurPtr;
210   int CurChar = getNextChar();
211   while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
212     CurChar = getNextChar();
213 
214   // If we have a CommentConsumer, notify it about the comment.
215   if (CommentConsumer) {
216     CommentConsumer->HandleComment(
217         SMLoc::getFromPointer(CommentTextStart),
218         StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
219   }
220 
221   IsAtStartOfLine = true;
222   // This is a whole line comment. leave newline
223   if (IsAtStartOfStatement)
224     return AsmToken(AsmToken::EndOfStatement,
225                     StringRef(TokStart, CurPtr - TokStart));
226   IsAtStartOfStatement = true;
227 
228   return AsmToken(AsmToken::EndOfStatement,
229                   StringRef(TokStart, CurPtr - 1 - TokStart));
230 }
231 
232 static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
233   // Skip ULL, UL, U, L and LL suffices.
234   if (CurPtr[0] == 'U')
235     ++CurPtr;
236   if (CurPtr[0] == 'L')
237     ++CurPtr;
238   if (CurPtr[0] == 'L')
239     ++CurPtr;
240 }
241 
242 // Look ahead to search for first non-hex digit, if it's [hH], then we treat the
243 // integer as a hexadecimal, possibly with leading zeroes.
244 static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix) {
245   const char *FirstHex = nullptr;
246   const char *LookAhead = CurPtr;
247   while (true) {
248     if (isDigit(*LookAhead)) {
249       ++LookAhead;
250     } else if (isHexDigit(*LookAhead)) {
251       if (!FirstHex)
252         FirstHex = LookAhead;
253       ++LookAhead;
254     } else {
255       break;
256     }
257   }
258   bool isHex = *LookAhead == 'h' || *LookAhead == 'H';
259   CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
260   if (isHex)
261     return 16;
262   return DefaultRadix;
263 }
264 
265 static AsmToken intToken(StringRef Ref, APInt &Value)
266 {
267   if (Value.isIntN(64))
268     return AsmToken(AsmToken::Integer, Ref, Value);
269   return AsmToken(AsmToken::BigNum, Ref, Value);
270 }
271 
272 /// LexDigit: First character is [0-9].
273 ///   Local Label: [0-9][:]
274 ///   Forward/Backward Label: [0-9][fb]
275 ///   Binary integer: 0b[01]+
276 ///   Octal integer: 0[0-7]+
277 ///   Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
278 ///   Decimal integer: [1-9][0-9]*
279 AsmToken AsmLexer::LexDigit() {
280   // MASM-flavor binary integer: [01]+[bB]
281   // MASM-flavor hexadecimal integer: [0-9][0-9a-fA-F]*[hH]
282   if (IsParsingMSInlineAsm && isdigit(CurPtr[-1])) {
283     const char *FirstNonBinary = (CurPtr[-1] != '0' && CurPtr[-1] != '1') ?
284                                    CurPtr - 1 : nullptr;
285     const char *OldCurPtr = CurPtr;
286     while (isHexDigit(*CurPtr)) {
287       if (*CurPtr != '0' && *CurPtr != '1' && !FirstNonBinary)
288         FirstNonBinary = CurPtr;
289       ++CurPtr;
290     }
291 
292     unsigned Radix = 0;
293     if (*CurPtr == 'h' || *CurPtr == 'H') {
294       // hexadecimal number
295       ++CurPtr;
296       Radix = 16;
297     } else if (FirstNonBinary && FirstNonBinary + 1 == CurPtr &&
298                (*FirstNonBinary == 'b' || *FirstNonBinary == 'B'))
299       Radix = 2;
300 
301     if (Radix == 2 || Radix == 16) {
302       StringRef Result(TokStart, CurPtr - TokStart);
303       APInt Value(128, 0, true);
304 
305       if (Result.drop_back().getAsInteger(Radix, Value))
306         return ReturnError(TokStart, Radix == 2 ? "invalid binary number" :
307                              "invalid hexdecimal number");
308 
309       // MSVC accepts and ignores type suffices on integer literals.
310       SkipIgnoredIntegerSuffix(CurPtr);
311 
312       return intToken(Result, Value);
313    }
314 
315     // octal/decimal integers, or floating point numbers, fall through
316     CurPtr = OldCurPtr;
317   }
318 
319   // Decimal integer: [1-9][0-9]*
320   if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
321     unsigned Radix = doLookAhead(CurPtr, 10);
322     bool isHex = Radix == 16;
323     // Check for floating point literals.
324     if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
325       ++CurPtr;
326       return LexFloatLiteral();
327     }
328 
329     StringRef Result(TokStart, CurPtr - TokStart);
330 
331     APInt Value(128, 0, true);
332     if (Result.getAsInteger(Radix, Value))
333       return ReturnError(TokStart, !isHex ? "invalid decimal number" :
334                            "invalid hexdecimal number");
335 
336     // Consume the [bB][hH].
337     if (Radix == 2 || Radix == 16)
338       ++CurPtr;
339 
340     // The darwin/x86 (and x86-64) assembler accepts and ignores type
341     // suffices on integer literals.
342     SkipIgnoredIntegerSuffix(CurPtr);
343 
344     return intToken(Result, Value);
345   }
346 
347   if (!IsParsingMSInlineAsm && ((*CurPtr == 'b') || (*CurPtr == 'B'))) {
348     ++CurPtr;
349     // See if we actually have "0b" as part of something like "jmp 0b\n"
350     if (!isDigit(CurPtr[0])) {
351       --CurPtr;
352       StringRef Result(TokStart, CurPtr - TokStart);
353       return AsmToken(AsmToken::Integer, Result, 0);
354     }
355     const char *NumStart = CurPtr;
356     while (CurPtr[0] == '0' || CurPtr[0] == '1')
357       ++CurPtr;
358 
359     // Requires at least one binary digit.
360     if (CurPtr == NumStart)
361       return ReturnError(TokStart, "invalid binary number");
362 
363     StringRef Result(TokStart, CurPtr - TokStart);
364 
365     APInt Value(128, 0, true);
366     if (Result.substr(2).getAsInteger(2, Value))
367       return ReturnError(TokStart, "invalid binary number");
368 
369     // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
370     // suffixes on integer literals.
371     SkipIgnoredIntegerSuffix(CurPtr);
372 
373     return intToken(Result, Value);
374   }
375 
376   if ((*CurPtr == 'x') || (*CurPtr == 'X')) {
377     ++CurPtr;
378     const char *NumStart = CurPtr;
379     while (isHexDigit(CurPtr[0]))
380       ++CurPtr;
381 
382     // "0x.0p0" is valid, and "0x0p0" (but not "0xp0" for example, which will be
383     // diagnosed by LexHexFloatLiteral).
384     if (CurPtr[0] == '.' || CurPtr[0] == 'p' || CurPtr[0] == 'P')
385       return LexHexFloatLiteral(NumStart == CurPtr);
386 
387     // Otherwise requires at least one hex digit.
388     if (CurPtr == NumStart)
389       return ReturnError(CurPtr-2, "invalid hexadecimal number");
390 
391     APInt Result(128, 0);
392     if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
393       return ReturnError(TokStart, "invalid hexadecimal number");
394 
395     // Consume the optional [hH].
396     if (!IsParsingMSInlineAsm && (*CurPtr == 'h' || *CurPtr == 'H'))
397       ++CurPtr;
398 
399     // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
400     // suffixes on integer literals.
401     SkipIgnoredIntegerSuffix(CurPtr);
402 
403     return intToken(StringRef(TokStart, CurPtr - TokStart), Result);
404   }
405 
406   // Either octal or hexadecimal.
407   APInt Value(128, 0, true);
408   unsigned Radix = doLookAhead(CurPtr, 8);
409   bool isHex = Radix == 16;
410   StringRef Result(TokStart, CurPtr - TokStart);
411   if (Result.getAsInteger(Radix, Value))
412     return ReturnError(TokStart, !isHex ? "invalid octal number" :
413                        "invalid hexdecimal number");
414 
415   // Consume the [hH].
416   if (Radix == 16)
417     ++CurPtr;
418 
419   // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
420   // suffixes on integer literals.
421   SkipIgnoredIntegerSuffix(CurPtr);
422 
423   return intToken(Result, Value);
424 }
425 
426 /// LexSingleQuote: Integer: 'b'
427 AsmToken AsmLexer::LexSingleQuote() {
428   int CurChar = getNextChar();
429 
430   if (CurChar == '\\')
431     CurChar = getNextChar();
432 
433   if (CurChar == EOF)
434     return ReturnError(TokStart, "unterminated single quote");
435 
436   CurChar = getNextChar();
437 
438   if (CurChar != '\'')
439     return ReturnError(TokStart, "single quote way too long");
440 
441   // The idea here being that 'c' is basically just an integral
442   // constant.
443   StringRef Res = StringRef(TokStart,CurPtr - TokStart);
444   long long Value;
445 
446   if (Res.startswith("\'\\")) {
447     char theChar = Res[2];
448     switch (theChar) {
449       default: Value = theChar; break;
450       case '\'': Value = '\''; break;
451       case 't': Value = '\t'; break;
452       case 'n': Value = '\n'; break;
453       case 'b': Value = '\b'; break;
454     }
455   } else
456     Value = TokStart[1];
457 
458   return AsmToken(AsmToken::Integer, Res, Value);
459 }
460 
461 /// LexQuote: String: "..."
462 AsmToken AsmLexer::LexQuote() {
463   int CurChar = getNextChar();
464   // TODO: does gas allow multiline string constants?
465   while (CurChar != '"') {
466     if (CurChar == '\\') {
467       // Allow \", etc.
468       CurChar = getNextChar();
469     }
470 
471     if (CurChar == EOF)
472       return ReturnError(TokStart, "unterminated string constant");
473 
474     CurChar = getNextChar();
475   }
476 
477   return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
478 }
479 
480 StringRef AsmLexer::LexUntilEndOfStatement() {
481   TokStart = CurPtr;
482 
483   while (!isAtStartOfComment(CurPtr) &&     // Start of line comment.
484          !isAtStatementSeparator(CurPtr) && // End of statement marker.
485          *CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end()) {
486     ++CurPtr;
487   }
488   return StringRef(TokStart, CurPtr-TokStart);
489 }
490 
491 StringRef AsmLexer::LexUntilEndOfLine() {
492   TokStart = CurPtr;
493 
494   while (*CurPtr != '\n' && *CurPtr != '\r' && CurPtr != CurBuf.end()) {
495     ++CurPtr;
496   }
497   return StringRef(TokStart, CurPtr-TokStart);
498 }
499 
500 size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf,
501                             bool ShouldSkipSpace) {
502   SaveAndRestore<const char *> SavedTokenStart(TokStart);
503   SaveAndRestore<const char *> SavedCurPtr(CurPtr);
504   SaveAndRestore<bool> SavedAtStartOfLine(IsAtStartOfLine);
505   SaveAndRestore<bool> SavedAtStartOfStatement(IsAtStartOfStatement);
506   SaveAndRestore<bool> SavedSkipSpace(SkipSpace, ShouldSkipSpace);
507   SaveAndRestore<bool> SavedIsPeeking(IsPeeking, true);
508   std::string SavedErr = getErr();
509   SMLoc SavedErrLoc = getErrLoc();
510 
511   size_t ReadCount;
512   for (ReadCount = 0; ReadCount < Buf.size(); ++ReadCount) {
513     AsmToken Token = LexToken();
514 
515     Buf[ReadCount] = Token;
516 
517     if (Token.is(AsmToken::Eof))
518       break;
519   }
520 
521   SetError(SavedErrLoc, SavedErr);
522   return ReadCount;
523 }
524 
525 bool AsmLexer::isAtStartOfComment(const char *Ptr) {
526   StringRef CommentString = MAI.getCommentString();
527 
528   if (CommentString.size() == 1)
529     return CommentString[0] == Ptr[0];
530 
531   // Allow # preprocessor commments also be counted as comments for "##" cases
532   if (CommentString[1] == '#')
533     return CommentString[0] == Ptr[0];
534 
535   return strncmp(Ptr, CommentString.data(), CommentString.size()) == 0;
536 }
537 
538 bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
539   return strncmp(Ptr, MAI.getSeparatorString(),
540                  strlen(MAI.getSeparatorString())) == 0;
541 }
542 
543 AsmToken AsmLexer::LexToken() {
544   TokStart = CurPtr;
545   // This always consumes at least one character.
546   int CurChar = getNextChar();
547 
548   if (!IsPeeking && CurChar == '#' && IsAtStartOfStatement) {
549     // If this starts with a '#', this may be a cpp
550     // hash directive and otherwise a line comment.
551     AsmToken TokenBuf[2];
552     MutableArrayRef<AsmToken> Buf(TokenBuf, 2);
553     size_t num = peekTokens(Buf, true);
554     // There cannot be a space preceeding this
555     if (IsAtStartOfLine && num == 2 && TokenBuf[0].is(AsmToken::Integer) &&
556         TokenBuf[1].is(AsmToken::String)) {
557       CurPtr = TokStart; // reset curPtr;
558       StringRef s = LexUntilEndOfLine();
559       UnLex(TokenBuf[1]);
560       UnLex(TokenBuf[0]);
561       return AsmToken(AsmToken::HashDirective, s);
562     }
563     return LexLineComment();
564   }
565 
566   if (isAtStartOfComment(TokStart))
567     return LexLineComment();
568 
569   if (isAtStatementSeparator(TokStart)) {
570     CurPtr += strlen(MAI.getSeparatorString()) - 1;
571     IsAtStartOfLine = true;
572     IsAtStartOfStatement = true;
573     return AsmToken(AsmToken::EndOfStatement,
574                     StringRef(TokStart, strlen(MAI.getSeparatorString())));
575   }
576 
577   // If we're missing a newline at EOF, make sure we still get an
578   // EndOfStatement token before the Eof token.
579   if (CurChar == EOF && !IsAtStartOfStatement) {
580     IsAtStartOfLine = true;
581     IsAtStartOfStatement = true;
582     return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
583   }
584   IsAtStartOfLine = false;
585   bool OldIsAtStartOfStatement = IsAtStartOfStatement;
586   IsAtStartOfStatement = false;
587   switch (CurChar) {
588   default:
589     // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
590     if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
591       return LexIdentifier();
592 
593     // Unknown character, emit an error.
594     return ReturnError(TokStart, "invalid character in input");
595   case EOF:
596     IsAtStartOfLine = true;
597     IsAtStartOfStatement = true;
598     return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
599   case 0:
600   case ' ':
601   case '\t':
602     IsAtStartOfStatement = OldIsAtStartOfStatement;
603     while (*CurPtr == ' ' || *CurPtr == '\t')
604       CurPtr++;
605     if (SkipSpace)
606       return LexToken(); // Ignore whitespace.
607     else
608       return AsmToken(AsmToken::Space, StringRef(TokStart, CurPtr - TokStart));
609   case '\n':
610   case '\r':
611     IsAtStartOfLine = true;
612     IsAtStartOfStatement = true;
613     return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
614   case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
615   case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
616   case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
617   case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
618   case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
619   case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
620   case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
621   case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
622   case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
623   case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
624   case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
625   case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
626   case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
627   case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
628   case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
629   case '=':
630     if (*CurPtr == '=') {
631       ++CurPtr;
632       return AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
633     }
634     return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
635   case '|':
636     if (*CurPtr == '|') {
637       ++CurPtr;
638       return AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
639     }
640     return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
641   case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
642   case '&':
643     if (*CurPtr == '&') {
644       ++CurPtr;
645       return AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
646     }
647     return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
648   case '!':
649     if (*CurPtr == '=') {
650       ++CurPtr;
651       return AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
652     }
653     return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
654   case '%':
655     if (MAI.hasMipsExpressions()) {
656       AsmToken::TokenKind Operator;
657       unsigned OperatorLength;
658 
659       std::tie(Operator, OperatorLength) =
660           StringSwitch<std::pair<AsmToken::TokenKind, unsigned>>(
661               StringRef(CurPtr))
662               .StartsWith("call16", {AsmToken::PercentCall16, 7})
663               .StartsWith("call_hi", {AsmToken::PercentCall_Hi, 8})
664               .StartsWith("call_lo", {AsmToken::PercentCall_Lo, 8})
665               .StartsWith("dtprel_hi", {AsmToken::PercentDtprel_Hi, 10})
666               .StartsWith("dtprel_lo", {AsmToken::PercentDtprel_Lo, 10})
667               .StartsWith("got_disp", {AsmToken::PercentGot_Disp, 9})
668               .StartsWith("got_hi", {AsmToken::PercentGot_Hi, 7})
669               .StartsWith("got_lo", {AsmToken::PercentGot_Lo, 7})
670               .StartsWith("got_ofst", {AsmToken::PercentGot_Ofst, 9})
671               .StartsWith("got_page", {AsmToken::PercentGot_Page, 9})
672               .StartsWith("gottprel", {AsmToken::PercentGottprel, 9})
673               .StartsWith("got", {AsmToken::PercentGot, 4})
674               .StartsWith("gp_rel", {AsmToken::PercentGp_Rel, 7})
675               .StartsWith("higher", {AsmToken::PercentHigher, 7})
676               .StartsWith("highest", {AsmToken::PercentHighest, 8})
677               .StartsWith("hi", {AsmToken::PercentHi, 3})
678               .StartsWith("lo", {AsmToken::PercentLo, 3})
679               .StartsWith("neg", {AsmToken::PercentNeg, 4})
680               .StartsWith("pcrel_hi", {AsmToken::PercentPcrel_Hi, 9})
681               .StartsWith("pcrel_lo", {AsmToken::PercentPcrel_Lo, 9})
682               .StartsWith("tlsgd", {AsmToken::PercentTlsgd, 6})
683               .StartsWith("tlsldm", {AsmToken::PercentTlsldm, 7})
684               .StartsWith("tprel_hi", {AsmToken::PercentTprel_Hi, 9})
685               .StartsWith("tprel_lo", {AsmToken::PercentTprel_Lo, 9})
686               .Default({AsmToken::Percent, 1});
687 
688       if (Operator != AsmToken::Percent) {
689         CurPtr += OperatorLength - 1;
690         return AsmToken(Operator, StringRef(TokStart, OperatorLength));
691       }
692     }
693     return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
694   case '/':
695     IsAtStartOfStatement = OldIsAtStartOfStatement;
696     return LexSlash();
697   case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
698   case '\'': return LexSingleQuote();
699   case '"': return LexQuote();
700   case '0': case '1': case '2': case '3': case '4':
701   case '5': case '6': case '7': case '8': case '9':
702     return LexDigit();
703   case '<':
704     switch (*CurPtr) {
705     case '<':
706       ++CurPtr;
707       return AsmToken(AsmToken::LessLess, StringRef(TokStart, 2));
708     case '=':
709       ++CurPtr;
710       return AsmToken(AsmToken::LessEqual, StringRef(TokStart, 2));
711     case '>':
712       ++CurPtr;
713       return AsmToken(AsmToken::LessGreater, StringRef(TokStart, 2));
714     default:
715       return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
716     }
717   case '>':
718     switch (*CurPtr) {
719     case '>':
720       ++CurPtr;
721       return AsmToken(AsmToken::GreaterGreater, StringRef(TokStart, 2));
722     case '=':
723       ++CurPtr;
724       return AsmToken(AsmToken::GreaterEqual, StringRef(TokStart, 2));
725     default:
726       return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
727     }
728 
729   // TODO: Quoted identifiers (objc methods etc)
730   // local labels: [0-9][:]
731   // Forward/backward labels: [0-9][fb]
732   // Integers, fp constants, character constants.
733   }
734 }
735