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