blob: c1c594a746975d6ba2f432792a29560924ea8292 [file] [log] [blame]
Chris Lattnera59e8772009-06-21 07:19:10 +00001//===- 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
Chris Lattnerbe343b32010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmLexer.h"
Kevin Enderby9823ca92009-09-04 21:45:34 +000015#include "llvm/MC/MCAsmInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/SMLoc.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000018#include <cctype>
Chris Lattner4651bca2009-06-21 19:21:25 +000019#include <cerrno>
Duncan Sands66b9f292009-06-22 06:59:32 +000020#include <cstdio>
Chris Lattner4506bd22009-06-21 19:43:50 +000021#include <cstdlib>
Chris Lattnera59e8772009-06-21 07:19:10 +000022using namespace llvm;
23
Sean Callananfd0b0282010-01-21 00:19:58 +000024AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI) {
25 CurBuf = NULL;
26 CurPtr = NULL;
Jim Grosbach61482252011-09-14 16:37:04 +000027 isAtStartOfLine = true;
Chris Lattnerfaf32c12009-06-24 00:33:19 +000028}
29
30AsmLexer::~AsmLexer() {
Chris Lattnera59e8772009-06-21 07:19:10 +000031}
32
Sean Callananfd0b0282010-01-21 00:19:58 +000033void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
34 CurBuf = buf;
Jim Grosbachde2f5f42011-02-11 19:05:56 +000035
Sean Callananfd0b0282010-01-21 00:19:58 +000036 if (ptr)
37 CurPtr = ptr;
38 else
39 CurPtr = CurBuf->getBufferStart();
Jim Grosbachde2f5f42011-02-11 19:05:56 +000040
Sean Callananfd0b0282010-01-21 00:19:58 +000041 TokStart = 0;
42}
43
Chris Lattner4651bca2009-06-21 19:21:25 +000044/// ReturnError - Set the error to the specified string at the specified
Daniel Dunbar3f872332009-07-28 16:08:33 +000045/// location. This is defined to always return AsmToken::Error.
Daniel Dunbarcb358b62009-07-28 03:00:54 +000046AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
Sean Callanan79036e42010-01-20 22:18:24 +000047 SetError(SMLoc::getFromPointer(Loc), Msg);
Jim Grosbachde2f5f42011-02-11 19:05:56 +000048
Daniel Dunbar3f872332009-07-28 16:08:33 +000049 return AsmToken(AsmToken::Error, StringRef(Loc, 0));
Chris Lattner4651bca2009-06-21 19:21:25 +000050}
51
Chris Lattnera59e8772009-06-21 07:19:10 +000052int AsmLexer::getNextChar() {
53 char CurChar = *CurPtr++;
54 switch (CurChar) {
55 default:
56 return (unsigned char)CurChar;
Sean Callananfd0b0282010-01-21 00:19:58 +000057 case 0:
Chris Lattnera59e8772009-06-21 07:19:10 +000058 // A nul character in the stream is either the end of the current buffer or
59 // a random nul in the file. Disambiguate that here.
60 if (CurPtr-1 != CurBuf->getBufferEnd())
61 return 0; // Just whitespace.
Jim Grosbachde2f5f42011-02-11 19:05:56 +000062
Chris Lattnera59e8772009-06-21 07:19:10 +000063 // Otherwise, return end of file.
Jim Grosbachde2f5f42011-02-11 19:05:56 +000064 --CurPtr; // Another call to lex will return EOF again.
Chris Lattnera59e8772009-06-21 07:19:10 +000065 return EOF;
66 }
Chris Lattnera59e8772009-06-21 07:19:10 +000067}
68
Daniel Dunbar4f2afe32010-09-27 20:12:52 +000069/// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
70///
71/// The leading integral digit sequence and dot should have already been
72/// consumed, some or all of the fractional digit sequence *can* have been
73/// consumed.
74AsmToken AsmLexer::LexFloatLiteral() {
75 // Skip the fractional digit sequence.
76 while (isdigit(*CurPtr))
77 ++CurPtr;
78
79 // Check for exponent; we intentionally accept a slighlty wider set of
80 // literals here and rely on the upstream client to reject invalid ones (e.g.,
81 // "1e+").
82 if (*CurPtr == 'e' || *CurPtr == 'E') {
83 ++CurPtr;
84 if (*CurPtr == '-' || *CurPtr == '+')
85 ++CurPtr;
86 while (isdigit(*CurPtr))
87 ++CurPtr;
88 }
89
90 return AsmToken(AsmToken::Real,
91 StringRef(TokStart, CurPtr - TokStart));
92}
93
Daniel Dunbar5fe03c02010-05-06 14:46:38 +000094/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
Daniel Dunbar54f0a622010-09-24 01:59:31 +000095static bool IsIdentifierChar(char c) {
96 return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@';
97}
Daniel Dunbarcb358b62009-07-28 03:00:54 +000098AsmToken AsmLexer::LexIdentifier() {
Daniel Dunbar54f0a622010-09-24 01:59:31 +000099 // Check for floating point literals.
100 if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
Daniel Dunbar4f2afe32010-09-27 20:12:52 +0000101 // Disambiguate a .1243foo identifier from a floating literal.
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000102 while (isdigit(*CurPtr))
103 ++CurPtr;
Daniel Dunbar4f2afe32010-09-27 20:12:52 +0000104 if (*CurPtr == 'e' || *CurPtr == 'E' || !IsIdentifierChar(*CurPtr))
105 return LexFloatLiteral();
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000106 }
107
108 while (IsIdentifierChar(*CurPtr))
Chris Lattner4651bca2009-06-21 19:21:25 +0000109 ++CurPtr;
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000110
Chris Lattnerd3050352010-04-14 04:40:28 +0000111 // Handle . as a special case.
Daniel Dunbar5fe03c02010-05-06 14:46:38 +0000112 if (CurPtr == TokStart+1 && TokStart[0] == '.')
113 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000114
Daniel Dunbar3f872332009-07-28 16:08:33 +0000115 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner4651bca2009-06-21 19:21:25 +0000116}
117
Chris Lattner4651bca2009-06-21 19:21:25 +0000118/// LexSlash: Slash: /
119/// C-Style Comment: /* ... */
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000120AsmToken AsmLexer::LexSlash() {
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000121 switch (*CurPtr) {
122 case '*': break; // C style comment.
123 case '/': return ++CurPtr, LexLineComment();
Daniel Dunbarbdf90d62010-10-25 20:18:53 +0000124 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr-1, 1));
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000125 }
Chris Lattner4651bca2009-06-21 19:21:25 +0000126
127 // C Style comment.
128 ++CurPtr; // skip the star.
129 while (1) {
130 int CurChar = getNextChar();
131 switch (CurChar) {
132 case EOF:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000133 return ReturnError(TokStart, "unterminated comment");
Chris Lattner4651bca2009-06-21 19:21:25 +0000134 case '*':
135 // End of the comment?
136 if (CurPtr[0] != '/') break;
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000137
Chris Lattner4651bca2009-06-21 19:21:25 +0000138 ++CurPtr; // End the */.
139 return LexToken();
140 }
141 }
142}
143
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000144/// LexLineComment: Comment: #[^\n]*
145/// : //[^\n]*
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000146AsmToken AsmLexer::LexLineComment() {
147 // FIXME: This is broken if we happen to a comment at the end of a file, which
148 // was .included, and which doesn't end with a newline.
Chris Lattner4651bca2009-06-21 19:21:25 +0000149 int CurChar = getNextChar();
Chris Lattner0ecd8252011-08-04 19:31:26 +0000150 while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
Chris Lattner4651bca2009-06-21 19:21:25 +0000151 CurChar = getNextChar();
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000152
Chris Lattner4651bca2009-06-21 19:21:25 +0000153 if (CurChar == EOF)
Daniel Dunbar3f872332009-07-28 16:08:33 +0000154 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
155 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
Chris Lattner4651bca2009-06-21 19:21:25 +0000156}
157
Chris Lattnera78c67e2010-08-24 00:43:25 +0000158static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
Jim Grosbachac67b502013-02-26 20:17:10 +0000159 // Skip ULL, UL, U, L and LL suffices.
160 if (CurPtr[0] == 'U')
161 ++CurPtr;
162 if (CurPtr[0] == 'L')
163 ++CurPtr;
164 if (CurPtr[0] == 'L')
165 ++CurPtr;
Chris Lattnera78c67e2010-08-24 00:43:25 +0000166}
167
Chad Rosierd556fd12013-02-12 01:00:01 +0000168// Look ahead to search for first non-hex digit, if it's [hH], then we treat the
169// integer as a hexadecimal, possibly with leading zeroes.
170static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix) {
171 const char *FirstHex = 0;
172 const char *LookAhead = CurPtr;
173 while (1) {
174 if (isdigit(*LookAhead)) {
175 ++LookAhead;
176 } else if (isxdigit(*LookAhead)) {
177 if (!FirstHex)
178 FirstHex = LookAhead;
179 ++LookAhead;
180 } else {
181 break;
182 }
183 }
184 bool isHex = *LookAhead == 'h' || *LookAhead == 'H';
Rafael Espindolae186d712013-02-14 16:23:08 +0000185 CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
Chad Rosierd556fd12013-02-12 01:00:01 +0000186 if (isHex)
187 return 16;
188 return DefaultRadix;
189}
190
Chris Lattner4651bca2009-06-21 19:21:25 +0000191/// LexDigit: First character is [0-9].
192/// Local Label: [0-9][:]
Rafael Espindolae186d712013-02-14 16:23:08 +0000193/// Forward/Backward Label: [0-9][fb]
194/// Binary integer: 0b[01]+
Chris Lattner4651bca2009-06-21 19:21:25 +0000195/// Octal integer: 0[0-7]+
Chad Rosierd556fd12013-02-12 01:00:01 +0000196/// Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
Chris Lattner4651bca2009-06-21 19:21:25 +0000197/// Decimal integer: [1-9][0-9]*
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000198AsmToken AsmLexer::LexDigit() {
Chris Lattner4651bca2009-06-21 19:21:25 +0000199 // Decimal integer: [1-9][0-9]*
Daniel Dunbarfacb34b2010-09-24 17:10:26 +0000200 if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
Chad Rosierd556fd12013-02-12 01:00:01 +0000201 unsigned Radix = doLookAhead(CurPtr, 10);
Rafael Espindolae186d712013-02-14 16:23:08 +0000202 bool isHex = Radix == 16;
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000203 // Check for floating point literals.
Rafael Espindolae186d712013-02-14 16:23:08 +0000204 if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000205 ++CurPtr;
Daniel Dunbar4f2afe32010-09-27 20:12:52 +0000206 return LexFloatLiteral();
Daniel Dunbar54f0a622010-09-24 01:59:31 +0000207 }
208
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000209 StringRef Result(TokStart, CurPtr - TokStart);
Chris Lattnera78c67e2010-08-24 00:43:25 +0000210
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000211 long long Value;
Chad Rosierd556fd12013-02-12 01:00:01 +0000212 if (Result.getAsInteger(Radix, Value)) {
Chris Lattner7ab3cc32010-12-25 21:36:35 +0000213 // Allow positive values that are too large to fit into a signed 64-bit
214 // integer, but that do fit in an unsigned one, we just convert them over.
215 unsigned long long UValue;
Chad Rosierd556fd12013-02-12 01:00:01 +0000216 if (Result.getAsInteger(Radix, UValue))
Rafael Espindolae186d712013-02-14 16:23:08 +0000217 return ReturnError(TokStart, !isHex ? "invalid decimal number" :
Chad Rosierd556fd12013-02-12 01:00:01 +0000218 "invalid hexdecimal number");
Chris Lattner7ab3cc32010-12-25 21:36:35 +0000219 Value = (long long)UValue;
Chris Lattner3a151be2010-03-13 19:25:13 +0000220 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000221
Chad Rosierd556fd12013-02-12 01:00:01 +0000222 // Consume the [bB][hH].
223 if (Radix == 2 || Radix == 16)
224 ++CurPtr;
225
Jim Grosbachac67b502013-02-26 20:17:10 +0000226 // The darwin/x86 (and x86-64) assembler accepts and ignores type
227 // suffices on integer literals.
Chris Lattnera78c67e2010-08-24 00:43:25 +0000228 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000229
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000230 return AsmToken(AsmToken::Integer, Result, Value);
Chris Lattner4651bca2009-06-21 19:21:25 +0000231 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000232
Chris Lattner4651bca2009-06-21 19:21:25 +0000233 if (*CurPtr == 'b') {
Rafael Espindolae186d712013-02-14 16:23:08 +0000234 ++CurPtr;
235 // See if we actually have "0b" as part of something like "jmp 0b\n"
236 if (!isdigit(CurPtr[0])) {
237 --CurPtr;
238 StringRef Result(TokStart, CurPtr - TokStart);
239 return AsmToken(AsmToken::Integer, Result, 0);
240 }
241 const char *NumStart = CurPtr;
Chris Lattner4651bca2009-06-21 19:21:25 +0000242 while (CurPtr[0] == '0' || CurPtr[0] == '1')
243 ++CurPtr;
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000244
Chris Lattner4651bca2009-06-21 19:21:25 +0000245 // Requires at least one binary digit.
246 if (CurPtr == NumStart)
Eric Christopher05f9e4e2011-04-12 00:18:03 +0000247 return ReturnError(TokStart, "invalid binary number");
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000248
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000249 StringRef Result(TokStart, CurPtr - TokStart);
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000250
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000251 long long Value;
Chris Lattnera78c67e2010-08-24 00:43:25 +0000252 if (Result.substr(2).getAsInteger(2, Value))
Eric Christopher05f9e4e2011-04-12 00:18:03 +0000253 return ReturnError(TokStart, "invalid binary number");
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000254
Chris Lattnera78c67e2010-08-24 00:43:25 +0000255 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
256 // suffixes on integer literals.
257 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000258
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000259 return AsmToken(AsmToken::Integer, Result, Value);
Chris Lattner4651bca2009-06-21 19:21:25 +0000260 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000261
Chris Lattner4651bca2009-06-21 19:21:25 +0000262 if (*CurPtr == 'x') {
263 ++CurPtr;
264 const char *NumStart = CurPtr;
265 while (isxdigit(CurPtr[0]))
266 ++CurPtr;
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000267
Chris Lattner4651bca2009-06-21 19:21:25 +0000268 // Requires at least one hex digit.
269 if (CurPtr == NumStart)
Eric Christopher05f9e4e2011-04-12 00:18:03 +0000270 return ReturnError(CurPtr-2, "invalid hexadecimal number");
Chris Lattner03949c92010-01-22 01:17:12 +0000271
272 unsigned long long Result;
273 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
Eric Christopher05f9e4e2011-04-12 00:18:03 +0000274 return ReturnError(TokStart, "invalid hexadecimal number");
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000275
Chad Rosierd556fd12013-02-12 01:00:01 +0000276 // Consume the optional [hH].
277 if (*CurPtr == 'h' || *CurPtr == 'H')
278 ++CurPtr;
279
Chris Lattnera78c67e2010-08-24 00:43:25 +0000280 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
281 // suffixes on integer literals.
282 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000283
Daniel Dunbar3f872332009-07-28 16:08:33 +0000284 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
Chris Lattner03949c92010-01-22 01:17:12 +0000285 (int64_t)Result);
Chris Lattner4651bca2009-06-21 19:21:25 +0000286 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000287
Matt Beaumont-Gay50e75bf2013-02-25 18:11:18 +0000288 // Either octal or hexadecimal.
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000289 long long Value;
Chad Rosierd556fd12013-02-12 01:00:01 +0000290 unsigned Radix = doLookAhead(CurPtr, 8);
Rafael Espindolae186d712013-02-14 16:23:08 +0000291 bool isHex = Radix == 16;
Chad Rosierd556fd12013-02-12 01:00:01 +0000292 StringRef Result(TokStart, CurPtr - TokStart);
293 if (Result.getAsInteger(Radix, Value))
Rafael Espindolae186d712013-02-14 16:23:08 +0000294 return ReturnError(TokStart, !isHex ? "invalid octal number" :
Chad Rosier53e5bb72013-02-12 01:12:24 +0000295 "invalid hexdecimal number");
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000296
Rafael Espindolae186d712013-02-14 16:23:08 +0000297 // Consume the [hH].
298 if (Radix == 16)
Chad Rosierd556fd12013-02-12 01:00:01 +0000299 ++CurPtr;
300
Chris Lattnera78c67e2010-08-24 00:43:25 +0000301 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
302 // suffixes on integer literals.
303 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000304
Chris Lattnerd5a7e352010-01-22 07:34:12 +0000305 return AsmToken(AsmToken::Integer, Result, Value);
Chris Lattner4651bca2009-06-21 19:21:25 +0000306}
307
Roman Divacky7529b162010-12-18 08:56:37 +0000308/// LexSingleQuote: Integer: 'b'
309AsmToken AsmLexer::LexSingleQuote() {
310 int CurChar = getNextChar();
311
312 if (CurChar == '\\')
313 CurChar = getNextChar();
314
315 if (CurChar == EOF)
316 return ReturnError(TokStart, "unterminated single quote");
317
318 CurChar = getNextChar();
319
320 if (CurChar != '\'')
321 return ReturnError(TokStart, "single quote way too long");
322
323 // The idea here being that 'c' is basically just an integral
324 // constant.
325 StringRef Res = StringRef(TokStart,CurPtr - TokStart);
326 long long Value;
327
328 if (Res.startswith("\'\\")) {
329 char theChar = Res[2];
330 switch (theChar) {
331 default: Value = theChar; break;
332 case '\'': Value = '\''; break;
333 case 't': Value = '\t'; break;
334 case 'n': Value = '\n'; break;
335 case 'b': Value = '\b'; break;
336 }
337 } else
338 Value = TokStart[1];
339
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000340 return AsmToken(AsmToken::Integer, Res, Value);
Roman Divacky7529b162010-12-18 08:56:37 +0000341}
342
343
Chris Lattner10a907d2009-06-21 19:56:35 +0000344/// LexQuote: String: "..."
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000345AsmToken AsmLexer::LexQuote() {
Chris Lattner10a907d2009-06-21 19:56:35 +0000346 int CurChar = getNextChar();
347 // TODO: does gas allow multiline string constants?
348 while (CurChar != '"') {
349 if (CurChar == '\\') {
350 // Allow \", etc.
351 CurChar = getNextChar();
352 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000353
Chris Lattner14ee48a2009-06-21 21:22:11 +0000354 if (CurChar == EOF)
355 return ReturnError(TokStart, "unterminated string constant");
Chris Lattner10a907d2009-06-21 19:56:35 +0000356
357 CurChar = getNextChar();
358 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000359
Daniel Dunbar3f872332009-07-28 16:08:33 +0000360 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner10a907d2009-06-21 19:56:35 +0000361}
362
Chris Lattnerff4bc462009-08-10 01:39:42 +0000363StringRef AsmLexer::LexUntilEndOfStatement() {
364 TokStart = CurPtr;
365
Jim Grosbachd31d3042011-03-24 18:46:34 +0000366 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
367 !isAtStatementSeparator(CurPtr) && // End of statement marker.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000368 *CurPtr != '\n' &&
369 *CurPtr != '\r' &&
Kevin Enderby9823ca92009-09-04 21:45:34 +0000370 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000371 ++CurPtr;
Kevin Enderby9823ca92009-09-04 21:45:34 +0000372 }
Chris Lattnerff4bc462009-08-10 01:39:42 +0000373 return StringRef(TokStart, CurPtr-TokStart);
374}
Chris Lattner4651bca2009-06-21 19:21:25 +0000375
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000376StringRef AsmLexer::LexUntilEndOfLine() {
377 TokStart = CurPtr;
378
379 while (*CurPtr != '\n' &&
380 *CurPtr != '\r' &&
381 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
382 ++CurPtr;
383 }
384 return StringRef(TokStart, CurPtr-TokStart);
385}
386
Kevin Enderbyb5db8302009-09-16 18:08:00 +0000387bool AsmLexer::isAtStartOfComment(char Char) {
Chris Lattnercec54502009-09-27 19:38:39 +0000388 // FIXME: This won't work for multi-character comment indicators like "//".
389 return Char == *MAI.getCommentString();
Kevin Enderbyb5db8302009-09-16 18:08:00 +0000390}
391
Jim Grosbachd31d3042011-03-24 18:46:34 +0000392bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
393 return strncmp(Ptr, MAI.getSeparatorString(),
394 strlen(MAI.getSeparatorString())) == 0;
395}
396
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000397AsmToken AsmLexer::LexToken() {
Chris Lattnera59e8772009-06-21 07:19:10 +0000398 TokStart = CurPtr;
399 // This always consumes at least one character.
400 int CurChar = getNextChar();
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000401
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000402 if (isAtStartOfComment(CurChar)) {
403 // If this comment starts with a '#', then return the Hash token and let
404 // the assembler parser see if it can be parsed as a cpp line filename
405 // comment. We do this only if we are at the start of a line.
406 if (CurChar == '#' && isAtStartOfLine)
407 return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
408 isAtStartOfLine = true;
Kevin Enderbyb5db8302009-09-16 18:08:00 +0000409 return LexLineComment();
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000410 }
Jim Grosbachd31d3042011-03-24 18:46:34 +0000411 if (isAtStatementSeparator(TokStart)) {
412 CurPtr += strlen(MAI.getSeparatorString()) - 1;
413 return AsmToken(AsmToken::EndOfStatement,
414 StringRef(TokStart, strlen(MAI.getSeparatorString())));
415 }
Kevin Enderby9823ca92009-09-04 21:45:34 +0000416
Jim Grosbach70796ca2011-09-15 16:52:06 +0000417 // If we're missing a newline at EOF, make sure we still get an
418 // EndOfStatement token before the Eof token.
419 if (CurChar == EOF && !isAtStartOfLine) {
420 isAtStartOfLine = true;
421 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
422 }
423
424 isAtStartOfLine = false;
Chris Lattnera59e8772009-06-21 07:19:10 +0000425 switch (CurChar) {
426 default:
Daniel Dunbar5fe03c02010-05-06 14:46:38 +0000427 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
428 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
Chris Lattner4651bca2009-06-21 19:21:25 +0000429 return LexIdentifier();
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000430
Chris Lattnera59e8772009-06-21 07:19:10 +0000431 // Unknown character, emit an error.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000432 return ReturnError(TokStart, "invalid character in input");
Daniel Dunbar3f872332009-07-28 16:08:33 +0000433 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
Chris Lattnera59e8772009-06-21 07:19:10 +0000434 case 0:
435 case ' ':
436 case '\t':
Preston Gurd7b6f2032012-09-19 20:36:12 +0000437 if (SkipSpace) {
438 // Ignore whitespace.
439 return LexToken();
440 } else {
441 int len = 1;
442 while (*CurPtr==' ' || *CurPtr=='\t') {
443 CurPtr++;
444 len++;
445 }
446 return AsmToken(AsmToken::Space, StringRef(TokStart, len));
447 }
Chris Lattner4651bca2009-06-21 19:21:25 +0000448 case '\n': // FALL THROUGH.
Jim Grosbachd31d3042011-03-24 18:46:34 +0000449 case '\r':
Kevin Enderbyf1c21a82011-09-13 23:45:18 +0000450 isAtStartOfLine = true;
Jim Grosbachd31d3042011-03-24 18:46:34 +0000451 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
Daniel Dunbar3f872332009-07-28 16:08:33 +0000452 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
453 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
454 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
455 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
456 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
457 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
Kevin Enderbyfb0f0de2009-09-04 22:40:31 +0000458 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
459 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
460 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
461 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
Daniel Dunbar3f872332009-07-28 16:08:33 +0000462 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
463 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
Daniel Dunbar5fe03c02010-05-06 14:46:38 +0000464 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
Matt Fleming924c5e52010-05-21 11:36:59 +0000465 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
Rafael Espindola65366442011-06-05 02:43:45 +0000466 case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000467 case '=':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000468 if (*CurPtr == '=')
Daniel Dunbar3f872332009-07-28 16:08:33 +0000469 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
470 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000471 case '|':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000472 if (*CurPtr == '|')
Daniel Dunbar3f872332009-07-28 16:08:33 +0000473 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
474 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
475 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000476 case '&':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000477 if (*CurPtr == '&')
Daniel Dunbar3f872332009-07-28 16:08:33 +0000478 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
479 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000480 case '!':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000481 if (*CurPtr == '=')
Daniel Dunbar3f872332009-07-28 16:08:33 +0000482 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
483 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000484 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
Chris Lattner4651bca2009-06-21 19:21:25 +0000485 case '/': return LexSlash();
Kevin Enderby9823ca92009-09-04 21:45:34 +0000486 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
Roman Divacky7529b162010-12-18 08:56:37 +0000487 case '\'': return LexSingleQuote();
Chris Lattner10a907d2009-06-21 19:56:35 +0000488 case '"': return LexQuote();
Chris Lattner4651bca2009-06-21 19:21:25 +0000489 case '0': case '1': case '2': case '3': case '4':
490 case '5': case '6': case '7': case '8': case '9':
491 return LexDigit();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000492 case '<':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000493 switch (*CurPtr) {
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000494 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000495 StringRef(TokStart, 2));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000496 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000497 StringRef(TokStart, 2));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000498 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000499 StringRef(TokStart, 2));
Daniel Dunbar3f872332009-07-28 16:08:33 +0000500 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000501 }
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000502 case '>':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000503 switch (*CurPtr) {
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000504 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000505 StringRef(TokStart, 2));
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000506 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000507 StringRef(TokStart, 2));
Daniel Dunbar3f872332009-07-28 16:08:33 +0000508 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000509 }
Jim Grosbachde2f5f42011-02-11 19:05:56 +0000510
Chris Lattner4651bca2009-06-21 19:21:25 +0000511 // TODO: Quoted identifiers (objc methods etc)
512 // local labels: [0-9][:]
513 // Forward/backward labels: [0-9][fb]
514 // Integers, fp constants, character constants.
Chris Lattnera59e8772009-06-21 07:19:10 +0000515 }
Duncan Sands66b9f292009-06-22 06:59:32 +0000516}