blob: bca516eca027691c5c904724837ed7a3e89e225c [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) {
Craig Topper353eda42014-04-24 06:44:33 +000025 CurBuf = nullptr;
26 CurPtr = nullptr;
Jim Grosbach1daf0732011-09-14 16:37:04 +000027 isAtStartOfLine = true;
David Peixotto27aa0012013-12-06 23:05:33 +000028 AllowAtInIdentifier = !StringRef(MAI.getCommentString()).startswith("@");
Chris Lattner4c501fc2009-06-24 00:33:19 +000029}
30
31AsmLexer::~AsmLexer() {
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000032}
33
Sean Callanan7a77eae2010-01-21 00:19:58 +000034void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
35 CurBuf = buf;
Jim Grosbach01af6c42011-02-11 19:05:56 +000036
Sean Callanan7a77eae2010-01-21 00:19:58 +000037 if (ptr)
38 CurPtr = ptr;
39 else
40 CurPtr = CurBuf->getBufferStart();
Jim Grosbach01af6c42011-02-11 19:05:56 +000041
Craig Topper353eda42014-04-24 06:44:33 +000042 TokStart = nullptr;
Sean Callanan7a77eae2010-01-21 00:19:58 +000043}
44
Chris Lattnerd0765612009-06-21 19:21:25 +000045/// ReturnError - Set the error to the specified string at the specified
Daniel Dunbarf2dcd772009-07-28 16:08:33 +000046/// location. This is defined to always return AsmToken::Error.
Daniel Dunbar8368f4e2009-07-28 03:00:54 +000047AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
Sean Callanan70855e42010-01-20 22:18:24 +000048 SetError(SMLoc::getFromPointer(Loc), Msg);
Jim Grosbach01af6c42011-02-11 19:05:56 +000049
Daniel Dunbarf2dcd772009-07-28 16:08:33 +000050 return AsmToken(AsmToken::Error, StringRef(Loc, 0));
Chris Lattnerd0765612009-06-21 19:21:25 +000051}
52
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000053int AsmLexer::getNextChar() {
54 char CurChar = *CurPtr++;
55 switch (CurChar) {
56 default:
57 return (unsigned char)CurChar;
Sean Callanan7a77eae2010-01-21 00:19:58 +000058 case 0:
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000059 // A nul character in the stream is either the end of the current buffer or
60 // a random nul in the file. Disambiguate that here.
61 if (CurPtr-1 != CurBuf->getBufferEnd())
62 return 0; // Just whitespace.
Jim Grosbach01af6c42011-02-11 19:05:56 +000063
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000064 // Otherwise, return end of file.
Jim Grosbach01af6c42011-02-11 19:05:56 +000065 --CurPtr; // Another call to lex will return EOF again.
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000066 return EOF;
67 }
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000068}
69
Daniel Dunbard116d8a2010-09-27 20:12:52 +000070/// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
71///
72/// The leading integral digit sequence and dot should have already been
73/// consumed, some or all of the fractional digit sequence *can* have been
74/// consumed.
75AsmToken AsmLexer::LexFloatLiteral() {
76 // Skip the fractional digit sequence.
77 while (isdigit(*CurPtr))
78 ++CurPtr;
79
80 // Check for exponent; we intentionally accept a slighlty wider set of
81 // literals here and rely on the upstream client to reject invalid ones (e.g.,
82 // "1e+").
83 if (*CurPtr == 'e' || *CurPtr == 'E') {
84 ++CurPtr;
85 if (*CurPtr == '-' || *CurPtr == '+')
86 ++CurPtr;
87 while (isdigit(*CurPtr))
88 ++CurPtr;
89 }
90
91 return AsmToken(AsmToken::Real,
92 StringRef(TokStart, CurPtr - TokStart));
93}
94
Tim Northover1f256232013-08-14 14:23:31 +000095/// LexHexFloatLiteral matches essentially (.[0-9a-fA-F]*)?[pP][+-]?[0-9a-fA-F]+
96/// while making sure there are enough actual digits around for the constant to
97/// be valid.
98///
99/// The leading "0x[0-9a-fA-F]*" (i.e. integer part) has already been consumed
100/// before we get here.
101AsmToken AsmLexer::LexHexFloatLiteral(bool NoIntDigits) {
102 assert((*CurPtr == 'p' || *CurPtr == 'P' || *CurPtr == '.') &&
103 "unexpected parse state in floating hex");
104 bool NoFracDigits = true;
105
106 // Skip the fractional part if there is one
107 if (*CurPtr == '.') {
108 ++CurPtr;
109
110 const char *FracStart = CurPtr;
111 while (isxdigit(*CurPtr))
112 ++CurPtr;
113
114 NoFracDigits = CurPtr == FracStart;
115 }
116
117 if (NoIntDigits && NoFracDigits)
118 return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
119 "expected at least one significand digit");
120
121 // Make sure we do have some kind of proper exponent part
122 if (*CurPtr != 'p' && *CurPtr != 'P')
123 return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
124 "expected exponent part 'p'");
125 ++CurPtr;
126
127 if (*CurPtr == '+' || *CurPtr == '-')
128 ++CurPtr;
129
130 // N.b. exponent digits are *not* hex
131 const char *ExpStart = CurPtr;
132 while (isdigit(*CurPtr))
133 ++CurPtr;
134
135 if (CurPtr == ExpStart)
136 return ReturnError(TokStart, "invalid hexadecimal floating-point constant: "
137 "expected at least one exponent digit");
138
139 return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
140}
141
Hans Wennborgce69d772013-10-18 20:46:28 +0000142/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@?]*
David Peixotto2cdc56d2013-12-06 20:35:58 +0000143static bool IsIdentifierChar(char c, bool AllowAt) {
144 return isalnum(c) || c == '_' || c == '$' || c == '.' ||
145 (c == '@' && AllowAt) || c == '?';
Daniel Dunbar3068a932010-09-24 01:59:31 +0000146}
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000147AsmToken AsmLexer::LexIdentifier() {
Daniel Dunbar3068a932010-09-24 01:59:31 +0000148 // Check for floating point literals.
149 if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000150 // Disambiguate a .1243foo identifier from a floating literal.
Daniel Dunbar3068a932010-09-24 01:59:31 +0000151 while (isdigit(*CurPtr))
152 ++CurPtr;
David Peixotto2cdc56d2013-12-06 20:35:58 +0000153 if (*CurPtr == 'e' || *CurPtr == 'E' ||
154 !IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000155 return LexFloatLiteral();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000156 }
157
David Peixotto2cdc56d2013-12-06 20:35:58 +0000158 while (IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
Chris Lattnerd0765612009-06-21 19:21:25 +0000159 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000160
Chris Lattner6b55cb92010-04-14 04:40:28 +0000161 // Handle . as a special case.
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000162 if (CurPtr == TokStart+1 && TokStart[0] == '.')
163 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000164
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000165 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
Chris Lattnerd0765612009-06-21 19:21:25 +0000166}
167
Chris Lattnerd0765612009-06-21 19:21:25 +0000168/// LexSlash: Slash: /
169/// C-Style Comment: /* ... */
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000170AsmToken AsmLexer::LexSlash() {
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000171 switch (*CurPtr) {
172 case '*': break; // C style comment.
173 case '/': return ++CurPtr, LexLineComment();
Daniel Dunbar7484d2c2010-10-25 20:18:53 +0000174 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr-1, 1));
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000175 }
Chris Lattnerd0765612009-06-21 19:21:25 +0000176
177 // C Style comment.
178 ++CurPtr; // skip the star.
179 while (1) {
180 int CurChar = getNextChar();
181 switch (CurChar) {
182 case EOF:
Chris Lattnerb0133452009-06-21 20:16:42 +0000183 return ReturnError(TokStart, "unterminated comment");
Chris Lattnerd0765612009-06-21 19:21:25 +0000184 case '*':
185 // End of the comment?
186 if (CurPtr[0] != '/') break;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000187
Chris Lattnerd0765612009-06-21 19:21:25 +0000188 ++CurPtr; // End the */.
189 return LexToken();
190 }
191 }
192}
193
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000194/// LexLineComment: Comment: #[^\n]*
195/// : //[^\n]*
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000196AsmToken AsmLexer::LexLineComment() {
197 // FIXME: This is broken if we happen to a comment at the end of a file, which
198 // was .included, and which doesn't end with a newline.
Chris Lattnerd0765612009-06-21 19:21:25 +0000199 int CurChar = getNextChar();
Chris Lattnere8baa382011-08-04 19:31:26 +0000200 while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
Chris Lattnerd0765612009-06-21 19:21:25 +0000201 CurChar = getNextChar();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000202
Chris Lattnerd0765612009-06-21 19:21:25 +0000203 if (CurChar == EOF)
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000204 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
205 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
Chris Lattnerd0765612009-06-21 19:21:25 +0000206}
207
Chris Lattner02db8f62010-08-24 00:43:25 +0000208static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
Jim Grosbach94a22602013-02-26 20:17:10 +0000209 // Skip ULL, UL, U, L and LL suffices.
210 if (CurPtr[0] == 'U')
211 ++CurPtr;
212 if (CurPtr[0] == 'L')
213 ++CurPtr;
214 if (CurPtr[0] == 'L')
215 ++CurPtr;
Chris Lattner02db8f62010-08-24 00:43:25 +0000216}
217
Chad Rosier8bc65562013-02-12 01:00:01 +0000218// Look ahead to search for first non-hex digit, if it's [hH], then we treat the
219// integer as a hexadecimal, possibly with leading zeroes.
220static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix) {
Craig Topper353eda42014-04-24 06:44:33 +0000221 const char *FirstHex = nullptr;
Chad Rosier8bc65562013-02-12 01:00:01 +0000222 const char *LookAhead = CurPtr;
223 while (1) {
224 if (isdigit(*LookAhead)) {
225 ++LookAhead;
226 } else if (isxdigit(*LookAhead)) {
227 if (!FirstHex)
228 FirstHex = LookAhead;
229 ++LookAhead;
230 } else {
231 break;
232 }
233 }
234 bool isHex = *LookAhead == 'h' || *LookAhead == 'H';
Rafael Espindola86d53452013-02-14 16:23:08 +0000235 CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
Chad Rosier8bc65562013-02-12 01:00:01 +0000236 if (isHex)
237 return 16;
238 return DefaultRadix;
239}
240
David Woodhousef42a6662014-02-01 16:20:54 +0000241static AsmToken intToken(StringRef Ref, APInt &Value)
242{
243 if (Value.isIntN(64))
244 return AsmToken(AsmToken::Integer, Ref, Value);
245 return AsmToken(AsmToken::BigNum, Ref, Value);
246}
247
Chris Lattnerd0765612009-06-21 19:21:25 +0000248/// LexDigit: First character is [0-9].
249/// Local Label: [0-9][:]
Rafael Espindola86d53452013-02-14 16:23:08 +0000250/// Forward/Backward Label: [0-9][fb]
251/// Binary integer: 0b[01]+
Chris Lattnerd0765612009-06-21 19:21:25 +0000252/// Octal integer: 0[0-7]+
Chad Rosier8bc65562013-02-12 01:00:01 +0000253/// Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
Chris Lattnerd0765612009-06-21 19:21:25 +0000254/// Decimal integer: [1-9][0-9]*
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000255AsmToken AsmLexer::LexDigit() {
Chris Lattnerd0765612009-06-21 19:21:25 +0000256 // Decimal integer: [1-9][0-9]*
Daniel Dunbarce17f722010-09-24 17:10:26 +0000257 if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
Chad Rosier8bc65562013-02-12 01:00:01 +0000258 unsigned Radix = doLookAhead(CurPtr, 10);
Rafael Espindola86d53452013-02-14 16:23:08 +0000259 bool isHex = Radix == 16;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000260 // Check for floating point literals.
Rafael Espindola86d53452013-02-14 16:23:08 +0000261 if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
Daniel Dunbar3068a932010-09-24 01:59:31 +0000262 ++CurPtr;
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000263 return LexFloatLiteral();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000264 }
265
Chris Lattnere6494012010-01-22 07:34:12 +0000266 StringRef Result(TokStart, CurPtr - TokStart);
Chris Lattner02db8f62010-08-24 00:43:25 +0000267
David Woodhousef42a6662014-02-01 16:20:54 +0000268 APInt Value(128, 0, true);
269 if (Result.getAsInteger(Radix, Value))
270 return ReturnError(TokStart, !isHex ? "invalid decimal number" :
Chad Rosier8bc65562013-02-12 01:00:01 +0000271 "invalid hexdecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000272
Chad Rosier8bc65562013-02-12 01:00:01 +0000273 // Consume the [bB][hH].
274 if (Radix == 2 || Radix == 16)
275 ++CurPtr;
276
Jim Grosbach94a22602013-02-26 20:17:10 +0000277 // The darwin/x86 (and x86-64) assembler accepts and ignores type
278 // suffices on integer literals.
Chris Lattner02db8f62010-08-24 00:43:25 +0000279 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000280
David Woodhousef42a6662014-02-01 16:20:54 +0000281 return intToken(Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000282 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000283
Chris Lattnerd0765612009-06-21 19:21:25 +0000284 if (*CurPtr == 'b') {
Rafael Espindola86d53452013-02-14 16:23:08 +0000285 ++CurPtr;
286 // See if we actually have "0b" as part of something like "jmp 0b\n"
287 if (!isdigit(CurPtr[0])) {
288 --CurPtr;
289 StringRef Result(TokStart, CurPtr - TokStart);
290 return AsmToken(AsmToken::Integer, Result, 0);
291 }
292 const char *NumStart = CurPtr;
Chris Lattnerd0765612009-06-21 19:21:25 +0000293 while (CurPtr[0] == '0' || CurPtr[0] == '1')
294 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000295
Chris Lattnerd0765612009-06-21 19:21:25 +0000296 // Requires at least one binary digit.
297 if (CurPtr == NumStart)
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000298 return ReturnError(TokStart, "invalid binary number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000299
Chris Lattnere6494012010-01-22 07:34:12 +0000300 StringRef Result(TokStart, CurPtr - TokStart);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000301
David Woodhousef42a6662014-02-01 16:20:54 +0000302 APInt Value(128, 0, true);
Chris Lattner02db8f62010-08-24 00:43:25 +0000303 if (Result.substr(2).getAsInteger(2, Value))
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000304 return ReturnError(TokStart, "invalid binary number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000305
Chris Lattner02db8f62010-08-24 00:43:25 +0000306 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
307 // suffixes on integer literals.
308 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000309
David Woodhousef42a6662014-02-01 16:20:54 +0000310 return intToken(Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000311 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000312
Chris Lattnerd0765612009-06-21 19:21:25 +0000313 if (*CurPtr == 'x') {
314 ++CurPtr;
315 const char *NumStart = CurPtr;
316 while (isxdigit(CurPtr[0]))
317 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000318
Tim Northover1f256232013-08-14 14:23:31 +0000319 // "0x.0p0" is valid, and "0x0p0" (but not "0xp0" for example, which will be
320 // diagnosed by LexHexFloatLiteral).
321 if (CurPtr[0] == '.' || CurPtr[0] == 'p' || CurPtr[0] == 'P')
322 return LexHexFloatLiteral(NumStart == CurPtr);
323
324 // Otherwise requires at least one hex digit.
Chris Lattnerd0765612009-06-21 19:21:25 +0000325 if (CurPtr == NumStart)
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000326 return ReturnError(CurPtr-2, "invalid hexadecimal number");
Chris Lattner6401c882010-01-22 01:17:12 +0000327
David Woodhousef42a6662014-02-01 16:20:54 +0000328 APInt Result(128, 0);
Chris Lattner6401c882010-01-22 01:17:12 +0000329 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000330 return ReturnError(TokStart, "invalid hexadecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000331
Chad Rosier8bc65562013-02-12 01:00:01 +0000332 // Consume the optional [hH].
333 if (*CurPtr == 'h' || *CurPtr == 'H')
334 ++CurPtr;
335
Chris Lattner02db8f62010-08-24 00:43:25 +0000336 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
337 // suffixes on integer literals.
338 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000339
David Woodhousef42a6662014-02-01 16:20:54 +0000340 return intToken(StringRef(TokStart, CurPtr - TokStart), Result);
Chris Lattnerd0765612009-06-21 19:21:25 +0000341 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000342
Matt Beaumont-Gay0e760da2013-02-25 18:11:18 +0000343 // Either octal or hexadecimal.
David Woodhousef42a6662014-02-01 16:20:54 +0000344 APInt Value(128, 0, true);
Chad Rosier8bc65562013-02-12 01:00:01 +0000345 unsigned Radix = doLookAhead(CurPtr, 8);
Rafael Espindola86d53452013-02-14 16:23:08 +0000346 bool isHex = Radix == 16;
Chad Rosier8bc65562013-02-12 01:00:01 +0000347 StringRef Result(TokStart, CurPtr - TokStart);
348 if (Result.getAsInteger(Radix, Value))
Rafael Espindola86d53452013-02-14 16:23:08 +0000349 return ReturnError(TokStart, !isHex ? "invalid octal number" :
Chad Rosier559cea42013-02-12 01:12:24 +0000350 "invalid hexdecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000351
Rafael Espindola86d53452013-02-14 16:23:08 +0000352 // Consume the [hH].
353 if (Radix == 16)
Chad Rosier8bc65562013-02-12 01:00:01 +0000354 ++CurPtr;
355
Chris Lattner02db8f62010-08-24 00:43:25 +0000356 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
357 // suffixes on integer literals.
358 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000359
David Woodhousef42a6662014-02-01 16:20:54 +0000360 return intToken(Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000361}
362
Roman Divacky71d29162010-12-18 08:56:37 +0000363/// LexSingleQuote: Integer: 'b'
364AsmToken AsmLexer::LexSingleQuote() {
365 int CurChar = getNextChar();
366
367 if (CurChar == '\\')
368 CurChar = getNextChar();
369
370 if (CurChar == EOF)
371 return ReturnError(TokStart, "unterminated single quote");
372
373 CurChar = getNextChar();
374
375 if (CurChar != '\'')
376 return ReturnError(TokStart, "single quote way too long");
377
378 // The idea here being that 'c' is basically just an integral
379 // constant.
380 StringRef Res = StringRef(TokStart,CurPtr - TokStart);
381 long long Value;
382
383 if (Res.startswith("\'\\")) {
384 char theChar = Res[2];
385 switch (theChar) {
386 default: Value = theChar; break;
387 case '\'': Value = '\''; break;
388 case 't': Value = '\t'; break;
389 case 'n': Value = '\n'; break;
390 case 'b': Value = '\b'; break;
391 }
392 } else
393 Value = TokStart[1];
394
Jim Grosbach01af6c42011-02-11 19:05:56 +0000395 return AsmToken(AsmToken::Integer, Res, Value);
Roman Divacky71d29162010-12-18 08:56:37 +0000396}
397
398
Chris Lattner419a9742009-06-21 19:56:35 +0000399/// LexQuote: String: "..."
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000400AsmToken AsmLexer::LexQuote() {
Chris Lattner419a9742009-06-21 19:56:35 +0000401 int CurChar = getNextChar();
402 // TODO: does gas allow multiline string constants?
403 while (CurChar != '"') {
404 if (CurChar == '\\') {
405 // Allow \", etc.
406 CurChar = getNextChar();
407 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000408
Chris Lattner2adc9e72009-06-21 21:22:11 +0000409 if (CurChar == EOF)
410 return ReturnError(TokStart, "unterminated string constant");
Chris Lattner419a9742009-06-21 19:56:35 +0000411
412 CurChar = getNextChar();
413 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000414
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000415 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner419a9742009-06-21 19:56:35 +0000416}
417
Chris Lattnercb307a272009-08-10 01:39:42 +0000418StringRef AsmLexer::LexUntilEndOfStatement() {
419 TokStart = CurPtr;
420
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000421 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
422 !isAtStatementSeparator(CurPtr) && // End of statement marker.
Chris Lattnercb307a272009-08-10 01:39:42 +0000423 *CurPtr != '\n' &&
424 *CurPtr != '\r' &&
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000425 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
Chris Lattnercb307a272009-08-10 01:39:42 +0000426 ++CurPtr;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000427 }
Chris Lattnercb307a272009-08-10 01:39:42 +0000428 return StringRef(TokStart, CurPtr-TokStart);
429}
Chris Lattnerd0765612009-06-21 19:21:25 +0000430
Kevin Enderby72553612011-09-13 23:45:18 +0000431StringRef AsmLexer::LexUntilEndOfLine() {
432 TokStart = CurPtr;
433
434 while (*CurPtr != '\n' &&
435 *CurPtr != '\r' &&
436 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
437 ++CurPtr;
438 }
439 return StringRef(TokStart, CurPtr-TokStart);
440}
441
Saleem Abdulrasoola879fab2014-02-09 23:29:24 +0000442const AsmToken AsmLexer::peekTok(bool ShouldSkipSpace) {
443 const char *SavedTokStart = TokStart;
444 const char *SavedCurPtr = CurPtr;
445 bool SavedAtStartOfLine = isAtStartOfLine;
446 bool SavedSkipSpace = SkipSpace;
447
448 std::string SavedErr = getErr();
449 SMLoc SavedErrLoc = getErrLoc();
450
451 SkipSpace = ShouldSkipSpace;
452 AsmToken Token = LexToken();
453
454 SetError(SavedErrLoc, SavedErr);
455
456 SkipSpace = SavedSkipSpace;
457 isAtStartOfLine = SavedAtStartOfLine;
458 CurPtr = SavedCurPtr;
459 TokStart = SavedTokStart;
460
461 return Token;
462}
463
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000464bool AsmLexer::isAtStartOfComment(char Char) {
Chris Lattner10a1cfd2009-09-27 19:38:39 +0000465 // FIXME: This won't work for multi-character comment indicators like "//".
466 return Char == *MAI.getCommentString();
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000467}
468
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000469bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
470 return strncmp(Ptr, MAI.getSeparatorString(),
471 strlen(MAI.getSeparatorString())) == 0;
472}
473
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000474AsmToken AsmLexer::LexToken() {
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000475 TokStart = CurPtr;
476 // This always consumes at least one character.
477 int CurChar = getNextChar();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000478
Kevin Enderby72553612011-09-13 23:45:18 +0000479 if (isAtStartOfComment(CurChar)) {
480 // If this comment starts with a '#', then return the Hash token and let
481 // the assembler parser see if it can be parsed as a cpp line filename
482 // comment. We do this only if we are at the start of a line.
483 if (CurChar == '#' && isAtStartOfLine)
484 return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
485 isAtStartOfLine = true;
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000486 return LexLineComment();
Kevin Enderby72553612011-09-13 23:45:18 +0000487 }
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000488 if (isAtStatementSeparator(TokStart)) {
489 CurPtr += strlen(MAI.getSeparatorString()) - 1;
490 return AsmToken(AsmToken::EndOfStatement,
491 StringRef(TokStart, strlen(MAI.getSeparatorString())));
492 }
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000493
Jim Grosbacha9aa3c12011-09-15 16:52:06 +0000494 // If we're missing a newline at EOF, make sure we still get an
495 // EndOfStatement token before the Eof token.
496 if (CurChar == EOF && !isAtStartOfLine) {
497 isAtStartOfLine = true;
498 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
499 }
500
501 isAtStartOfLine = false;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000502 switch (CurChar) {
503 default:
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000504 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
505 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
Chris Lattnerd0765612009-06-21 19:21:25 +0000506 return LexIdentifier();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000507
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000508 // Unknown character, emit an error.
Chris Lattnerb0133452009-06-21 20:16:42 +0000509 return ReturnError(TokStart, "invalid character in input");
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000510 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000511 case 0:
512 case ' ':
513 case '\t':
Preston Gurd05500642012-09-19 20:36:12 +0000514 if (SkipSpace) {
515 // Ignore whitespace.
516 return LexToken();
517 } else {
518 int len = 1;
519 while (*CurPtr==' ' || *CurPtr=='\t') {
520 CurPtr++;
521 len++;
522 }
523 return AsmToken(AsmToken::Space, StringRef(TokStart, len));
524 }
Chris Lattnerd0765612009-06-21 19:21:25 +0000525 case '\n': // FALL THROUGH.
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000526 case '\r':
Kevin Enderby72553612011-09-13 23:45:18 +0000527 isAtStartOfLine = true;
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000528 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000529 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
530 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
531 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
532 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
533 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
534 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
Kevin Enderby9c0f7fc2009-09-04 22:40:31 +0000535 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
536 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
537 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
538 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000539 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
540 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000541 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
Matt Flemingec9d6fa2010-05-21 11:36:59 +0000542 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +0000543 case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000544 case '=':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000545 if (*CurPtr == '=')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000546 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
547 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000548 case '|':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000549 if (*CurPtr == '|')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000550 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
551 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
552 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000553 case '&':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000554 if (*CurPtr == '&')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000555 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
556 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000557 case '!':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000558 if (*CurPtr == '=')
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000559 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
560 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
Kevin Enderby7d912182009-09-03 17:15:07 +0000561 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
Chris Lattnerd0765612009-06-21 19:21:25 +0000562 case '/': return LexSlash();
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000563 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
Roman Divacky71d29162010-12-18 08:56:37 +0000564 case '\'': return LexSingleQuote();
Chris Lattner419a9742009-06-21 19:56:35 +0000565 case '"': return LexQuote();
Chris Lattnerd0765612009-06-21 19:21:25 +0000566 case '0': case '1': case '2': case '3': case '4':
567 case '5': case '6': case '7': case '8': case '9':
568 return LexDigit();
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000569 case '<':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000570 switch (*CurPtr) {
Jim Grosbach01af6c42011-02-11 19:05:56 +0000571 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000572 StringRef(TokStart, 2));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000573 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000574 StringRef(TokStart, 2));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000575 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000576 StringRef(TokStart, 2));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000577 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000578 }
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000579 case '>':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000580 switch (*CurPtr) {
Jim Grosbach01af6c42011-02-11 19:05:56 +0000581 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000582 StringRef(TokStart, 2));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000583 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000584 StringRef(TokStart, 2));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000585 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000586 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000587
Chris Lattnerd0765612009-06-21 19:21:25 +0000588 // TODO: Quoted identifiers (objc methods etc)
589 // local labels: [0-9][:]
590 // Forward/backward labels: [0-9][fb]
591 // Integers, fp constants, character constants.
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000592 }
Duncan Sands376c6f12009-06-22 06:59:32 +0000593}