blob: b49dd010479310b55710f73f6df9e2cf1490f9a1 [file] [log] [blame]
Chris Lattnerc8dfbcb2009-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 Lattner5b0e01c2010-01-22 01:58:08 +000014#include "llvm/MC/MCParser/AsmLexer.h"
Kevin Enderbyf92f9902009-09-04 21:45:34 +000015#include "llvm/MC/MCAsmInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/SMLoc.h"
Nick Lewycky0de20af2010-12-19 20:43:38 +000018#include <cctype>
Chris Lattnerd0765612009-06-21 19:21:25 +000019#include <cerrno>
Duncan Sands376c6f12009-06-22 06:59:32 +000020#include <cstdio>
Chris Lattner99f0b602009-06-21 19:43:50 +000021#include <cstdlib>
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000022using namespace llvm;
23
Sean Callanan7a77eae2010-01-21 00:19:58 +000024AsmLexer::AsmLexer(const MCAsmInfo &_MAI) : MAI(_MAI) {
25 CurBuf = NULL;
26 CurPtr = NULL;
Jim Grosbach1daf0732011-09-14 16:37:04 +000027 isAtStartOfLine = true;
Chris Lattner4c501fc2009-06-24 00:33:19 +000028}
29
30AsmLexer::~AsmLexer() {
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000031}
32
Sean Callanan7a77eae2010-01-21 00:19:58 +000033void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
34 CurBuf = buf;
Jim Grosbach01af6c42011-02-11 19:05:56 +000035
Sean Callanan7a77eae2010-01-21 00:19:58 +000036 if (ptr)
37 CurPtr = ptr;
38 else
39 CurPtr = CurBuf->getBufferStart();
Jim Grosbach01af6c42011-02-11 19:05:56 +000040
Sean Callanan7a77eae2010-01-21 00:19:58 +000041 TokStart = 0;
42}
43
Chris Lattnerd0765612009-06-21 19:21:25 +000044/// ReturnError - Set the error to the specified string at the specified
Daniel Dunbarf2dcd772009-07-28 16:08:33 +000045/// location. This is defined to always return AsmToken::Error.
Daniel Dunbar8368f4e2009-07-28 03:00:54 +000046AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
Sean Callanan70855e42010-01-20 22:18:24 +000047 SetError(SMLoc::getFromPointer(Loc), Msg);
Jim Grosbach01af6c42011-02-11 19:05:56 +000048
Daniel Dunbarf2dcd772009-07-28 16:08:33 +000049 return AsmToken(AsmToken::Error, StringRef(Loc, 0));
Chris Lattnerd0765612009-06-21 19:21:25 +000050}
51
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000052int AsmLexer::getNextChar() {
53 char CurChar = *CurPtr++;
54 switch (CurChar) {
55 default:
56 return (unsigned char)CurChar;
Sean Callanan7a77eae2010-01-21 00:19:58 +000057 case 0:
Chris Lattnerc8dfbcb2009-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 Grosbach01af6c42011-02-11 19:05:56 +000062
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000063 // Otherwise, return end of file.
Jim Grosbach01af6c42011-02-11 19:05:56 +000064 --CurPtr; // Another call to lex will return EOF again.
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000065 return EOF;
66 }
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000067}
68
Daniel Dunbard116d8a2010-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
Tim Northover1f256232013-08-14 14:23:31 +000094/// LexHexFloatLiteral matches essentially (.[0-9a-fA-F]*)?[pP][+-]?[0-9a-fA-F]+
95/// while making sure there are enough actual digits around for the constant to
96/// be valid.
97///
98/// The leading "0x[0-9a-fA-F]*" (i.e. integer part) has already been consumed
99/// before we get here.
100AsmToken AsmLexer::LexHexFloatLiteral(bool NoIntDigits) {
101 assert((*CurPtr == 'p' || *CurPtr == 'P' || *CurPtr == '.') &&
102 "unexpected parse state in floating hex");
103 bool NoFracDigits = true;
104
105 // Skip the fractional part if there is one
106 if (*CurPtr == '.') {
107 ++CurPtr;
108
109 const char *FracStart = CurPtr;
110 while (isxdigit(*CurPtr))
111 ++CurPtr;
112
113 NoFracDigits = CurPtr == FracStart;
114 }
115
116 if (NoIntDigits && NoFracDigits)
117 return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
118 "expected at least one significand digit");
119
120 // Make sure we do have some kind of proper exponent part
121 if (*CurPtr != 'p' && *CurPtr != 'P')
122 return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
123 "expected exponent part 'p'");
124 ++CurPtr;
125
126 if (*CurPtr == '+' || *CurPtr == '-')
127 ++CurPtr;
128
129 // N.b. exponent digits are *not* hex
130 const char *ExpStart = CurPtr;
131 while (isdigit(*CurPtr))
132 ++CurPtr;
133
134 if (CurPtr == ExpStart)
135 return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
136 "expected at least one exponent digit");
137
138 return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
139}
140
Hans Wennborgce69d772013-10-18 20:46:28 +0000141/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@?]*
Daniel Dunbar3068a932010-09-24 01:59:31 +0000142static bool IsIdentifierChar(char c) {
Hans Wennborgce69d772013-10-18 20:46:28 +0000143 return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@' || c == '?';
Daniel Dunbar3068a932010-09-24 01:59:31 +0000144}
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000145AsmToken AsmLexer::LexIdentifier() {
Daniel Dunbar3068a932010-09-24 01:59:31 +0000146 // Check for floating point literals.
147 if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000148 // Disambiguate a .1243foo identifier from a floating literal.
Daniel Dunbar3068a932010-09-24 01:59:31 +0000149 while (isdigit(*CurPtr))
150 ++CurPtr;
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000151 if (*CurPtr == 'e' || *CurPtr == 'E' || !IsIdentifierChar(*CurPtr))
152 return LexFloatLiteral();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000153 }
154
155 while (IsIdentifierChar(*CurPtr))
Chris Lattnerd0765612009-06-21 19:21:25 +0000156 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000157
Chris Lattner6b55cb92010-04-14 04:40:28 +0000158 // Handle . as a special case.
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000159 if (CurPtr == TokStart+1 && TokStart[0] == '.')
160 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000161
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000162 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
Chris Lattnerd0765612009-06-21 19:21:25 +0000163}
164
Chris Lattnerd0765612009-06-21 19:21:25 +0000165/// LexSlash: Slash: /
166/// C-Style Comment: /* ... */
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000167AsmToken AsmLexer::LexSlash() {
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000168 switch (*CurPtr) {
169 case '*': break; // C style comment.
170 case '/': return ++CurPtr, LexLineComment();
Daniel Dunbar7484d2c2010-10-25 20:18:53 +0000171 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr-1, 1));
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000172 }
Chris Lattnerd0765612009-06-21 19:21:25 +0000173
174 // C Style comment.
175 ++CurPtr; // skip the star.
176 while (1) {
177 int CurChar = getNextChar();
178 switch (CurChar) {
179 case EOF:
Chris Lattnerb0133452009-06-21 20:16:42 +0000180 return ReturnError(TokStart, "unterminated comment");
Chris Lattnerd0765612009-06-21 19:21:25 +0000181 case '*':
182 // End of the comment?
183 if (CurPtr[0] != '/') break;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000184
Chris Lattnerd0765612009-06-21 19:21:25 +0000185 ++CurPtr; // End the */.
186 return LexToken();
187 }
188 }
189}
190
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000191/// LexLineComment: Comment: #[^\n]*
192/// : //[^\n]*
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000193AsmToken AsmLexer::LexLineComment() {
194 // FIXME: This is broken if we happen to a comment at the end of a file, which
195 // was .included, and which doesn't end with a newline.
Chris Lattnerd0765612009-06-21 19:21:25 +0000196 int CurChar = getNextChar();
Chris Lattnere8baa382011-08-04 19:31:26 +0000197 while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
Chris Lattnerd0765612009-06-21 19:21:25 +0000198 CurChar = getNextChar();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000199
Chris Lattnerd0765612009-06-21 19:21:25 +0000200 if (CurChar == EOF)
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000201 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
202 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
Chris Lattnerd0765612009-06-21 19:21:25 +0000203}
204
Chris Lattner02db8f62010-08-24 00:43:25 +0000205static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
Jim Grosbach94a22602013-02-26 20:17:10 +0000206 // Skip ULL, UL, U, L and LL suffices.
207 if (CurPtr[0] == 'U')
208 ++CurPtr;
209 if (CurPtr[0] == 'L')
210 ++CurPtr;
211 if (CurPtr[0] == 'L')
212 ++CurPtr;
Chris Lattner02db8f62010-08-24 00:43:25 +0000213}
214
Chad Rosier8bc65562013-02-12 01:00:01 +0000215// Look ahead to search for first non-hex digit, if it's [hH], then we treat the
216// integer as a hexadecimal, possibly with leading zeroes.
217static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix) {
218 const char *FirstHex = 0;
219 const char *LookAhead = CurPtr;
220 while (1) {
221 if (isdigit(*LookAhead)) {
222 ++LookAhead;
223 } else if (isxdigit(*LookAhead)) {
224 if (!FirstHex)
225 FirstHex = LookAhead;
226 ++LookAhead;
227 } else {
228 break;
229 }
230 }
231 bool isHex = *LookAhead == 'h' || *LookAhead == 'H';
Rafael Espindola86d53452013-02-14 16:23:08 +0000232 CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
Chad Rosier8bc65562013-02-12 01:00:01 +0000233 if (isHex)
234 return 16;
235 return DefaultRadix;
236}
237
Chris Lattnerd0765612009-06-21 19:21:25 +0000238/// LexDigit: First character is [0-9].
239/// Local Label: [0-9][:]
Rafael Espindola86d53452013-02-14 16:23:08 +0000240/// Forward/Backward Label: [0-9][fb]
241/// Binary integer: 0b[01]+
Chris Lattnerd0765612009-06-21 19:21:25 +0000242/// Octal integer: 0[0-7]+
Chad Rosier8bc65562013-02-12 01:00:01 +0000243/// Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
Chris Lattnerd0765612009-06-21 19:21:25 +0000244/// Decimal integer: [1-9][0-9]*
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000245AsmToken AsmLexer::LexDigit() {
Chris Lattnerd0765612009-06-21 19:21:25 +0000246 // Decimal integer: [1-9][0-9]*
Daniel Dunbarce17f722010-09-24 17:10:26 +0000247 if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
Chad Rosier8bc65562013-02-12 01:00:01 +0000248 unsigned Radix = doLookAhead(CurPtr, 10);
Rafael Espindola86d53452013-02-14 16:23:08 +0000249 bool isHex = Radix == 16;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000250 // Check for floating point literals.
Rafael Espindola86d53452013-02-14 16:23:08 +0000251 if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
Daniel Dunbar3068a932010-09-24 01:59:31 +0000252 ++CurPtr;
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000253 return LexFloatLiteral();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000254 }
255
Chris Lattnere6494012010-01-22 07:34:12 +0000256 StringRef Result(TokStart, CurPtr - TokStart);
Chris Lattner02db8f62010-08-24 00:43:25 +0000257
Chris Lattnere6494012010-01-22 07:34:12 +0000258 long long Value;
Chad Rosier8bc65562013-02-12 01:00:01 +0000259 if (Result.getAsInteger(Radix, Value)) {
Chris Lattner03a102b2010-12-25 21:36:35 +0000260 // Allow positive values that are too large to fit into a signed 64-bit
261 // integer, but that do fit in an unsigned one, we just convert them over.
262 unsigned long long UValue;
Chad Rosier8bc65562013-02-12 01:00:01 +0000263 if (Result.getAsInteger(Radix, UValue))
Rafael Espindola86d53452013-02-14 16:23:08 +0000264 return ReturnError(TokStart, !isHex ? "invalid decimal number" :
Chad Rosier8bc65562013-02-12 01:00:01 +0000265 "invalid hexdecimal number");
Chris Lattner03a102b2010-12-25 21:36:35 +0000266 Value = (long long)UValue;
Chris Lattner341b1d22010-03-13 19:25:13 +0000267 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000268
Chad Rosier8bc65562013-02-12 01:00:01 +0000269 // Consume the [bB][hH].
270 if (Radix == 2 || Radix == 16)
271 ++CurPtr;
272
Jim Grosbach94a22602013-02-26 20:17:10 +0000273 // The darwin/x86 (and x86-64) assembler accepts and ignores type
274 // suffices on integer literals.
Chris Lattner02db8f62010-08-24 00:43:25 +0000275 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000276
Chris Lattnere6494012010-01-22 07:34:12 +0000277 return AsmToken(AsmToken::Integer, Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000278 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000279
Chris Lattnerd0765612009-06-21 19:21:25 +0000280 if (*CurPtr == 'b') {
Rafael Espindola86d53452013-02-14 16:23:08 +0000281 ++CurPtr;
282 // See if we actually have "0b" as part of something like "jmp 0b\n"
283 if (!isdigit(CurPtr[0])) {
284 --CurPtr;
285 StringRef Result(TokStart, CurPtr - TokStart);
286 return AsmToken(AsmToken::Integer, Result, 0);
287 }
288 const char *NumStart = CurPtr;
Chris Lattnerd0765612009-06-21 19:21:25 +0000289 while (CurPtr[0] == '0' || CurPtr[0] == '1')
290 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000291
Chris Lattnerd0765612009-06-21 19:21:25 +0000292 // Requires at least one binary digit.
293 if (CurPtr == NumStart)
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000294 return ReturnError(TokStart, "invalid binary number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000295
Chris Lattnere6494012010-01-22 07:34:12 +0000296 StringRef Result(TokStart, CurPtr - TokStart);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000297
Chris Lattnere6494012010-01-22 07:34:12 +0000298 long long Value;
Chris Lattner02db8f62010-08-24 00:43:25 +0000299 if (Result.substr(2).getAsInteger(2, Value))
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000300 return ReturnError(TokStart, "invalid binary number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000301
Chris Lattner02db8f62010-08-24 00:43:25 +0000302 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
303 // suffixes on integer literals.
304 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000305
Chris Lattnere6494012010-01-22 07:34:12 +0000306 return AsmToken(AsmToken::Integer, Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000307 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000308
Chris Lattnerd0765612009-06-21 19:21:25 +0000309 if (*CurPtr == 'x') {
310 ++CurPtr;
311 const char *NumStart = CurPtr;
312 while (isxdigit(CurPtr[0]))
313 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000314
Tim Northover1f256232013-08-14 14:23:31 +0000315 // "0x.0p0" is valid, and "0x0p0" (but not "0xp0" for example, which will be
316 // diagnosed by LexHexFloatLiteral).
317 if (CurPtr[0] == '.' || CurPtr[0] == 'p' || CurPtr[0] == 'P')
318 return LexHexFloatLiteral(NumStart == CurPtr);
319
320 // Otherwise requires at least one hex digit.
Chris Lattnerd0765612009-06-21 19:21:25 +0000321 if (CurPtr == NumStart)
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000322 return ReturnError(CurPtr-2, "invalid hexadecimal number");
Chris Lattner6401c882010-01-22 01:17:12 +0000323
324 unsigned long long Result;
325 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000326 return ReturnError(TokStart, "invalid hexadecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000327
Chad Rosier8bc65562013-02-12 01:00:01 +0000328 // Consume the optional [hH].
329 if (*CurPtr == 'h' || *CurPtr == 'H')
330 ++CurPtr;
331
Chris Lattner02db8f62010-08-24 00:43:25 +0000332 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
333 // suffixes on integer literals.
334 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000335
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000336 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
Chris Lattner6401c882010-01-22 01:17:12 +0000337 (int64_t)Result);
Chris Lattnerd0765612009-06-21 19:21:25 +0000338 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000339
Matt Beaumont-Gay0e760da2013-02-25 18:11:18 +0000340 // Either octal or hexadecimal.
Chris Lattnere6494012010-01-22 07:34:12 +0000341 long long Value;
Chad Rosier8bc65562013-02-12 01:00:01 +0000342 unsigned Radix = doLookAhead(CurPtr, 8);
Rafael Espindola86d53452013-02-14 16:23:08 +0000343 bool isHex = Radix == 16;
Chad Rosier8bc65562013-02-12 01:00:01 +0000344 StringRef Result(TokStart, CurPtr - TokStart);
345 if (Result.getAsInteger(Radix, Value))
Rafael Espindola86d53452013-02-14 16:23:08 +0000346 return ReturnError(TokStart, !isHex ? "invalid octal number" :
Chad Rosier559cea42013-02-12 01:12:24 +0000347 "invalid hexdecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000348
Rafael Espindola86d53452013-02-14 16:23:08 +0000349 // Consume the [hH].
350 if (Radix == 16)
Chad Rosier8bc65562013-02-12 01:00:01 +0000351 ++CurPtr;
352
Chris Lattner02db8f62010-08-24 00:43:25 +0000353 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
354 // suffixes on integer literals.
355 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000356
Chris Lattnere6494012010-01-22 07:34:12 +0000357 return AsmToken(AsmToken::Integer, Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000358}
359
Roman Divacky71d29162010-12-18 08:56:37 +0000360/// LexSingleQuote: Integer: 'b'
361AsmToken AsmLexer::LexSingleQuote() {
362 int CurChar = getNextChar();
363
364 if (CurChar == '\\')
365 CurChar = getNextChar();
366
367 if (CurChar == EOF)
368 return ReturnError(TokStart, "unterminated single quote");
369
370 CurChar = getNextChar();
371
372 if (CurChar != '\'')
373 return ReturnError(TokStart, "single quote way too long");
374
375 // The idea here being that 'c' is basically just an integral
376 // constant.
377 StringRef Res = StringRef(TokStart,CurPtr - TokStart);
378 long long Value;
379
380 if (Res.startswith("\'\\")) {
381 char theChar = Res[2];
382 switch (theChar) {
383 default: Value = theChar; break;
384 case '\'': Value = '\''; break;
385 case 't': Value = '\t'; break;
386 case 'n': Value = '\n'; break;
387 case 'b': Value = '\b'; break;
388 }
389 } else
390 Value = TokStart[1];
391
Jim Grosbach01af6c42011-02-11 19:05:56 +0000392 return AsmToken(AsmToken::Integer, Res, Value);
Roman Divacky71d29162010-12-18 08:56:37 +0000393}
394
395
Chris Lattner419a9742009-06-21 19:56:35 +0000396/// LexQuote: String: "..."
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000397AsmToken AsmLexer::LexQuote() {
Chris Lattner419a9742009-06-21 19:56:35 +0000398 int CurChar = getNextChar();
399 // TODO: does gas allow multiline string constants?
400 while (CurChar != '"') {
401 if (CurChar == '\\') {
402 // Allow \", etc.
403 CurChar = getNextChar();
404 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000405
Chris Lattner2adc9e72009-06-21 21:22:11 +0000406 if (CurChar == EOF)
407 return ReturnError(TokStart, "unterminated string constant");
Chris Lattner419a9742009-06-21 19:56:35 +0000408
409 CurChar = getNextChar();
410 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000411
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000412 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner419a9742009-06-21 19:56:35 +0000413}
414
Chris Lattnercb307a272009-08-10 01:39:42 +0000415StringRef AsmLexer::LexUntilEndOfStatement() {
416 TokStart = CurPtr;
417
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000418 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
419 !isAtStatementSeparator(CurPtr) && // End of statement marker.
Chris Lattnercb307a272009-08-10 01:39:42 +0000420 *CurPtr != '\n' &&
421 *CurPtr != '\r' &&
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000422 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
Chris Lattnercb307a272009-08-10 01:39:42 +0000423 ++CurPtr;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000424 }
Chris Lattnercb307a272009-08-10 01:39:42 +0000425 return StringRef(TokStart, CurPtr-TokStart);
426}
Chris Lattnerd0765612009-06-21 19:21:25 +0000427
Kevin Enderby72553612011-09-13 23:45:18 +0000428StringRef AsmLexer::LexUntilEndOfLine() {
429 TokStart = CurPtr;
430
431 while (*CurPtr != '\n' &&
432 *CurPtr != '\r' &&
433 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
434 ++CurPtr;
435 }
436 return StringRef(TokStart, CurPtr-TokStart);
437}
438
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000439bool AsmLexer::isAtStartOfComment(char Char) {
Chris Lattner10a1cfd2009-09-27 19:38:39 +0000440 // FIXME: This won't work for multi-character comment indicators like "//".
441 return Char == *MAI.getCommentString();
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000442}
443
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000444bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
445 return strncmp(Ptr, MAI.getSeparatorString(),
446 strlen(MAI.getSeparatorString())) == 0;
447}
448
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000449AsmToken AsmLexer::LexToken() {
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000450 TokStart = CurPtr;
451 // This always consumes at least one character.
452 int CurChar = getNextChar();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000453
Kevin Enderby72553612011-09-13 23:45:18 +0000454 if (isAtStartOfComment(CurChar)) {
455 // If this comment starts with a '#', then return the Hash token and let
456 // the assembler parser see if it can be parsed as a cpp line filename
457 // comment. We do this only if we are at the start of a line.
458 if (CurChar == '#' && isAtStartOfLine)
459 return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
460 isAtStartOfLine = true;
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000461 return LexLineComment();
Kevin Enderby72553612011-09-13 23:45:18 +0000462 }
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000463 if (isAtStatementSeparator(TokStart)) {
464 CurPtr += strlen(MAI.getSeparatorString()) - 1;
465 return AsmToken(AsmToken::EndOfStatement,
466 StringRef(TokStart, strlen(MAI.getSeparatorString())));
467 }
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000468
Jim Grosbacha9aa3c12011-09-15 16:52:06 +0000469 // If we're missing a newline at EOF, make sure we still get an
470 // EndOfStatement token before the Eof token.
471 if (CurChar == EOF && !isAtStartOfLine) {
472 isAtStartOfLine = true;
473 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
474 }
475
476 isAtStartOfLine = false;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000477 switch (CurChar) {
478 default:
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000479 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
480 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
Chris Lattnerd0765612009-06-21 19:21:25 +0000481 return LexIdentifier();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000482
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000483 // Unknown character, emit an error.
Chris Lattnerb0133452009-06-21 20:16:42 +0000484 return ReturnError(TokStart, "invalid character in input");
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000485 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000486 case 0:
487 case ' ':
488 case '\t':
Preston Gurd05500642012-09-19 20:36:12 +0000489 if (SkipSpace) {
490 // Ignore whitespace.
491 return LexToken();
492 } else {
493 int len = 1;
494 while (*CurPtr==' ' || *CurPtr=='\t') {
495 CurPtr++;
496 len++;
497 }
498 return AsmToken(AsmToken::Space, StringRef(TokStart, len));
499 }
Chris Lattnerd0765612009-06-21 19:21:25 +0000500 case '\n': // FALL THROUGH.
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000501 case '\r':
Kevin Enderby72553612011-09-13 23:45:18 +0000502 isAtStartOfLine = true;
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000503 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000504 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
505 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
506 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
507 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
508 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
509 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
Kevin Enderby9c0f7fc2009-09-04 22:40:31 +0000510 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
511 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
512 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
513 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000514 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
515 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000516 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
Matt Flemingec9d6fa2010-05-21 11:36:59 +0000517 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +0000518 case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000519 case '=':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000520 if (*CurPtr == '=')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000521 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
522 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000523 case '|':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000524 if (*CurPtr == '|')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000525 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
526 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
527 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000528 case '&':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000529 if (*CurPtr == '&')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000530 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
531 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000532 case '!':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000533 if (*CurPtr == '=')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000534 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
535 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
Kevin Enderby7d912182009-09-03 17:15:07 +0000536 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
Chris Lattnerd0765612009-06-21 19:21:25 +0000537 case '/': return LexSlash();
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000538 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
Roman Divacky71d29162010-12-18 08:56:37 +0000539 case '\'': return LexSingleQuote();
Chris Lattner419a9742009-06-21 19:56:35 +0000540 case '"': return LexQuote();
Chris Lattnerd0765612009-06-21 19:21:25 +0000541 case '0': case '1': case '2': case '3': case '4':
542 case '5': case '6': case '7': case '8': case '9':
543 return LexDigit();
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000544 case '<':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000545 switch (*CurPtr) {
Jim Grosbach01af6c42011-02-11 19:05:56 +0000546 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000547 StringRef(TokStart, 2));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000548 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000549 StringRef(TokStart, 2));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000550 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000551 StringRef(TokStart, 2));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000552 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000553 }
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000554 case '>':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000555 switch (*CurPtr) {
Jim Grosbach01af6c42011-02-11 19:05:56 +0000556 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000557 StringRef(TokStart, 2));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000558 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000559 StringRef(TokStart, 2));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000560 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000561 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000562
Chris Lattnerd0765612009-06-21 19:21:25 +0000563 // TODO: Quoted identifiers (objc methods etc)
564 // local labels: [0-9][:]
565 // Forward/backward labels: [0-9][fb]
566 // Integers, fp constants, character constants.
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000567 }
Duncan Sands376c6f12009-06-22 06:59:32 +0000568}