blob: 424c8ae66a0482ea3976bfb17e2871026b523b2c [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
David Blaikie9f380a32015-03-16 18:06:57 +000024AsmLexer::AsmLexer(const MCAsmInfo &MAI) : MAI(MAI) {
Craig Topper353eda42014-04-24 06:44:33 +000025 CurPtr = nullptr;
Jim Grosbach1daf0732011-09-14 16:37:04 +000026 isAtStartOfLine = true;
David Peixotto27aa0012013-12-06 23:05:33 +000027 AllowAtInIdentifier = !StringRef(MAI.getCommentString()).startswith("@");
Chris Lattner4c501fc2009-06-24 00:33:19 +000028}
29
30AsmLexer::~AsmLexer() {
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000031}
32
Rafael Espindola8026bd02014-07-06 14:17:29 +000033void AsmLexer::setBuffer(StringRef 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
Rafael Espindola8026bd02014-07-06 14:17:29 +000039 CurPtr = CurBuf.begin();
Jim Grosbach01af6c42011-02-11 19:05:56 +000040
Craig Topper353eda42014-04-24 06:44:33 +000041 TokStart = nullptr;
Sean Callanan7a77eae2010-01-21 00:19:58 +000042}
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.
Rafael Espindola8026bd02014-07-06 14:17:29 +000060 if (CurPtr - 1 != CurBuf.end())
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +000061 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_$.@?]*
David Peixotto2cdc56d2013-12-06 20:35:58 +0000142static bool IsIdentifierChar(char c, bool AllowAt) {
143 return isalnum(c) || c == '_' || c == '$' || c == '.' ||
144 (c == '@' && AllowAt) || c == '?';
Daniel Dunbar3068a932010-09-24 01:59:31 +0000145}
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000146AsmToken AsmLexer::LexIdentifier() {
Daniel Dunbar3068a932010-09-24 01:59:31 +0000147 // Check for floating point literals.
148 if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000149 // Disambiguate a .1243foo identifier from a floating literal.
Daniel Dunbar3068a932010-09-24 01:59:31 +0000150 while (isdigit(*CurPtr))
151 ++CurPtr;
David Peixotto2cdc56d2013-12-06 20:35:58 +0000152 if (*CurPtr == 'e' || *CurPtr == 'E' ||
153 !IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000154 return LexFloatLiteral();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000155 }
156
David Peixotto2cdc56d2013-12-06 20:35:58 +0000157 while (IsIdentifierChar(*CurPtr, AllowAtInIdentifier))
Chris Lattnerd0765612009-06-21 19:21:25 +0000158 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000159
Chris Lattner6b55cb92010-04-14 04:40:28 +0000160 // Handle . as a special case.
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000161 if (CurPtr == TokStart+1 && TokStart[0] == '.')
162 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000163
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000164 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
Chris Lattnerd0765612009-06-21 19:21:25 +0000165}
166
Chris Lattnerd0765612009-06-21 19:21:25 +0000167/// LexSlash: Slash: /
168/// C-Style Comment: /* ... */
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000169AsmToken AsmLexer::LexSlash() {
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000170 switch (*CurPtr) {
Richard Trieu7a083812016-02-18 22:09:30 +0000171 case '*':
172 break; // C style comment.
173 case '/':
174 ++CurPtr;
175 return LexLineComment();
176 default:
177 return AsmToken(AsmToken::Slash, StringRef(CurPtr - 1, 1));
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000178 }
Chris Lattnerd0765612009-06-21 19:21:25 +0000179
180 // C Style comment.
181 ++CurPtr; // skip the star.
182 while (1) {
183 int CurChar = getNextChar();
184 switch (CurChar) {
185 case EOF:
Chris Lattnerb0133452009-06-21 20:16:42 +0000186 return ReturnError(TokStart, "unterminated comment");
Chris Lattnerd0765612009-06-21 19:21:25 +0000187 case '*':
188 // End of the comment?
189 if (CurPtr[0] != '/') break;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000190
Chris Lattnerd0765612009-06-21 19:21:25 +0000191 ++CurPtr; // End the */.
192 return LexToken();
193 }
194 }
195}
196
Daniel Dunbar6b22f9c2009-06-29 21:58:22 +0000197/// LexLineComment: Comment: #[^\n]*
198/// : //[^\n]*
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000199AsmToken AsmLexer::LexLineComment() {
200 // FIXME: This is broken if we happen to a comment at the end of a file, which
201 // was .included, and which doesn't end with a newline.
Chris Lattnerd0765612009-06-21 19:21:25 +0000202 int CurChar = getNextChar();
Chris Lattnere8baa382011-08-04 19:31:26 +0000203 while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
Chris Lattnerd0765612009-06-21 19:21:25 +0000204 CurChar = getNextChar();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000205
Chris Lattnerd0765612009-06-21 19:21:25 +0000206 if (CurChar == EOF)
Saleem Abdulrasool71ede292014-06-18 20:57:32 +0000207 return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
208 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 0));
Chris Lattnerd0765612009-06-21 19:21:25 +0000209}
210
Chris Lattner02db8f62010-08-24 00:43:25 +0000211static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
Jim Grosbach94a22602013-02-26 20:17:10 +0000212 // Skip ULL, UL, U, L and LL suffices.
213 if (CurPtr[0] == 'U')
214 ++CurPtr;
215 if (CurPtr[0] == 'L')
216 ++CurPtr;
217 if (CurPtr[0] == 'L')
218 ++CurPtr;
Chris Lattner02db8f62010-08-24 00:43:25 +0000219}
220
Chad Rosier8bc65562013-02-12 01:00:01 +0000221// Look ahead to search for first non-hex digit, if it's [hH], then we treat the
222// integer as a hexadecimal, possibly with leading zeroes.
223static unsigned doLookAhead(const char *&CurPtr, unsigned DefaultRadix) {
Craig Topper353eda42014-04-24 06:44:33 +0000224 const char *FirstHex = nullptr;
Chad Rosier8bc65562013-02-12 01:00:01 +0000225 const char *LookAhead = CurPtr;
226 while (1) {
227 if (isdigit(*LookAhead)) {
228 ++LookAhead;
229 } else if (isxdigit(*LookAhead)) {
230 if (!FirstHex)
231 FirstHex = LookAhead;
232 ++LookAhead;
233 } else {
234 break;
235 }
236 }
237 bool isHex = *LookAhead == 'h' || *LookAhead == 'H';
Rafael Espindola86d53452013-02-14 16:23:08 +0000238 CurPtr = isHex || !FirstHex ? LookAhead : FirstHex;
Chad Rosier8bc65562013-02-12 01:00:01 +0000239 if (isHex)
240 return 16;
241 return DefaultRadix;
242}
243
David Woodhousef42a6662014-02-01 16:20:54 +0000244static AsmToken intToken(StringRef Ref, APInt &Value)
245{
246 if (Value.isIntN(64))
247 return AsmToken(AsmToken::Integer, Ref, Value);
248 return AsmToken(AsmToken::BigNum, Ref, Value);
249}
250
Chris Lattnerd0765612009-06-21 19:21:25 +0000251/// LexDigit: First character is [0-9].
252/// Local Label: [0-9][:]
Rafael Espindola86d53452013-02-14 16:23:08 +0000253/// Forward/Backward Label: [0-9][fb]
254/// Binary integer: 0b[01]+
Chris Lattnerd0765612009-06-21 19:21:25 +0000255/// Octal integer: 0[0-7]+
Chad Rosier8bc65562013-02-12 01:00:01 +0000256/// Hex integer: 0x[0-9a-fA-F]+ or [0x]?[0-9][0-9a-fA-F]*[hH]
Chris Lattnerd0765612009-06-21 19:21:25 +0000257/// Decimal integer: [1-9][0-9]*
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000258AsmToken AsmLexer::LexDigit() {
Chris Lattnerd0765612009-06-21 19:21:25 +0000259 // Decimal integer: [1-9][0-9]*
Daniel Dunbarce17f722010-09-24 17:10:26 +0000260 if (CurPtr[-1] != '0' || CurPtr[0] == '.') {
Chad Rosier8bc65562013-02-12 01:00:01 +0000261 unsigned Radix = doLookAhead(CurPtr, 10);
Rafael Espindola86d53452013-02-14 16:23:08 +0000262 bool isHex = Radix == 16;
Daniel Dunbar3068a932010-09-24 01:59:31 +0000263 // Check for floating point literals.
Rafael Espindola86d53452013-02-14 16:23:08 +0000264 if (!isHex && (*CurPtr == '.' || *CurPtr == 'e')) {
Daniel Dunbar3068a932010-09-24 01:59:31 +0000265 ++CurPtr;
Daniel Dunbard116d8a2010-09-27 20:12:52 +0000266 return LexFloatLiteral();
Daniel Dunbar3068a932010-09-24 01:59:31 +0000267 }
268
Chris Lattnere6494012010-01-22 07:34:12 +0000269 StringRef Result(TokStart, CurPtr - TokStart);
Chris Lattner02db8f62010-08-24 00:43:25 +0000270
David Woodhousef42a6662014-02-01 16:20:54 +0000271 APInt Value(128, 0, true);
272 if (Result.getAsInteger(Radix, Value))
273 return ReturnError(TokStart, !isHex ? "invalid decimal number" :
Chad Rosier8bc65562013-02-12 01:00:01 +0000274 "invalid hexdecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000275
Chad Rosier8bc65562013-02-12 01:00:01 +0000276 // Consume the [bB][hH].
277 if (Radix == 2 || Radix == 16)
278 ++CurPtr;
279
Jim Grosbach94a22602013-02-26 20:17:10 +0000280 // The darwin/x86 (and x86-64) assembler accepts and ignores type
281 // suffices on integer literals.
Chris Lattner02db8f62010-08-24 00:43:25 +0000282 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000283
David Woodhousef42a6662014-02-01 16:20:54 +0000284 return intToken(Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000285 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000286
Chris Lattnerd0765612009-06-21 19:21:25 +0000287 if (*CurPtr == 'b') {
Rafael Espindola86d53452013-02-14 16:23:08 +0000288 ++CurPtr;
289 // See if we actually have "0b" as part of something like "jmp 0b\n"
290 if (!isdigit(CurPtr[0])) {
291 --CurPtr;
292 StringRef Result(TokStart, CurPtr - TokStart);
293 return AsmToken(AsmToken::Integer, Result, 0);
294 }
295 const char *NumStart = CurPtr;
Chris Lattnerd0765612009-06-21 19:21:25 +0000296 while (CurPtr[0] == '0' || CurPtr[0] == '1')
297 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000298
Chris Lattnerd0765612009-06-21 19:21:25 +0000299 // Requires at least one binary digit.
300 if (CurPtr == NumStart)
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000301 return ReturnError(TokStart, "invalid binary number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000302
Chris Lattnere6494012010-01-22 07:34:12 +0000303 StringRef Result(TokStart, CurPtr - TokStart);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000304
David Woodhousef42a6662014-02-01 16:20:54 +0000305 APInt Value(128, 0, true);
Chris Lattner02db8f62010-08-24 00:43:25 +0000306 if (Result.substr(2).getAsInteger(2, Value))
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000307 return ReturnError(TokStart, "invalid binary number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000308
Chris Lattner02db8f62010-08-24 00:43:25 +0000309 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
310 // suffixes on integer literals.
311 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000312
David Woodhousef42a6662014-02-01 16:20:54 +0000313 return intToken(Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000314 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000315
Chris Lattnerd0765612009-06-21 19:21:25 +0000316 if (*CurPtr == 'x') {
317 ++CurPtr;
318 const char *NumStart = CurPtr;
319 while (isxdigit(CurPtr[0]))
320 ++CurPtr;
Jim Grosbach01af6c42011-02-11 19:05:56 +0000321
Tim Northover1f256232013-08-14 14:23:31 +0000322 // "0x.0p0" is valid, and "0x0p0" (but not "0xp0" for example, which will be
323 // diagnosed by LexHexFloatLiteral).
324 if (CurPtr[0] == '.' || CurPtr[0] == 'p' || CurPtr[0] == 'P')
325 return LexHexFloatLiteral(NumStart == CurPtr);
326
327 // Otherwise requires at least one hex digit.
Chris Lattnerd0765612009-06-21 19:21:25 +0000328 if (CurPtr == NumStart)
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000329 return ReturnError(CurPtr-2, "invalid hexadecimal number");
Chris Lattner6401c882010-01-22 01:17:12 +0000330
David Woodhousef42a6662014-02-01 16:20:54 +0000331 APInt Result(128, 0);
Chris Lattner6401c882010-01-22 01:17:12 +0000332 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
Eric Christopherffc0e1f2011-04-12 00:18:03 +0000333 return ReturnError(TokStart, "invalid hexadecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000334
Chad Rosier8bc65562013-02-12 01:00:01 +0000335 // Consume the optional [hH].
336 if (*CurPtr == 'h' || *CurPtr == 'H')
337 ++CurPtr;
338
Chris Lattner02db8f62010-08-24 00:43:25 +0000339 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
340 // suffixes on integer literals.
341 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000342
David Woodhousef42a6662014-02-01 16:20:54 +0000343 return intToken(StringRef(TokStart, CurPtr - TokStart), Result);
Chris Lattnerd0765612009-06-21 19:21:25 +0000344 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000345
Matt Beaumont-Gay0e760da2013-02-25 18:11:18 +0000346 // Either octal or hexadecimal.
David Woodhousef42a6662014-02-01 16:20:54 +0000347 APInt Value(128, 0, true);
Chad Rosier8bc65562013-02-12 01:00:01 +0000348 unsigned Radix = doLookAhead(CurPtr, 8);
Rafael Espindola86d53452013-02-14 16:23:08 +0000349 bool isHex = Radix == 16;
Chad Rosier8bc65562013-02-12 01:00:01 +0000350 StringRef Result(TokStart, CurPtr - TokStart);
351 if (Result.getAsInteger(Radix, Value))
Rafael Espindola86d53452013-02-14 16:23:08 +0000352 return ReturnError(TokStart, !isHex ? "invalid octal number" :
Chad Rosier559cea42013-02-12 01:12:24 +0000353 "invalid hexdecimal number");
Jim Grosbach01af6c42011-02-11 19:05:56 +0000354
Rafael Espindola86d53452013-02-14 16:23:08 +0000355 // Consume the [hH].
356 if (Radix == 16)
Chad Rosier8bc65562013-02-12 01:00:01 +0000357 ++CurPtr;
358
Chris Lattner02db8f62010-08-24 00:43:25 +0000359 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
360 // suffixes on integer literals.
361 SkipIgnoredIntegerSuffix(CurPtr);
Jim Grosbach01af6c42011-02-11 19:05:56 +0000362
David Woodhousef42a6662014-02-01 16:20:54 +0000363 return intToken(Result, Value);
Chris Lattnerd0765612009-06-21 19:21:25 +0000364}
365
Roman Divacky71d29162010-12-18 08:56:37 +0000366/// LexSingleQuote: Integer: 'b'
367AsmToken AsmLexer::LexSingleQuote() {
368 int CurChar = getNextChar();
369
370 if (CurChar == '\\')
371 CurChar = getNextChar();
372
373 if (CurChar == EOF)
374 return ReturnError(TokStart, "unterminated single quote");
375
376 CurChar = getNextChar();
377
378 if (CurChar != '\'')
379 return ReturnError(TokStart, "single quote way too long");
380
381 // The idea here being that 'c' is basically just an integral
382 // constant.
383 StringRef Res = StringRef(TokStart,CurPtr - TokStart);
384 long long Value;
385
386 if (Res.startswith("\'\\")) {
387 char theChar = Res[2];
388 switch (theChar) {
389 default: Value = theChar; break;
390 case '\'': Value = '\''; break;
391 case 't': Value = '\t'; break;
392 case 'n': Value = '\n'; break;
393 case 'b': Value = '\b'; break;
394 }
395 } else
396 Value = TokStart[1];
397
Jim Grosbach01af6c42011-02-11 19:05:56 +0000398 return AsmToken(AsmToken::Integer, Res, Value);
Roman Divacky71d29162010-12-18 08:56:37 +0000399}
400
401
Chris Lattner419a9742009-06-21 19:56:35 +0000402/// LexQuote: String: "..."
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000403AsmToken AsmLexer::LexQuote() {
Chris Lattner419a9742009-06-21 19:56:35 +0000404 int CurChar = getNextChar();
405 // TODO: does gas allow multiline string constants?
406 while (CurChar != '"') {
407 if (CurChar == '\\') {
408 // Allow \", etc.
409 CurChar = getNextChar();
410 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000411
Chris Lattner2adc9e72009-06-21 21:22:11 +0000412 if (CurChar == EOF)
413 return ReturnError(TokStart, "unterminated string constant");
Chris Lattner419a9742009-06-21 19:56:35 +0000414
415 CurChar = getNextChar();
416 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000417
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000418 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner419a9742009-06-21 19:56:35 +0000419}
420
Chris Lattnercb307a272009-08-10 01:39:42 +0000421StringRef AsmLexer::LexUntilEndOfStatement() {
422 TokStart = CurPtr;
423
Saleem Abdulrasoolbb67af42014-08-14 02:51:43 +0000424 while (!isAtStartOfComment(CurPtr) && // Start of line comment.
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000425 !isAtStatementSeparator(CurPtr) && // End of statement marker.
Rafael Espindola8026bd02014-07-06 14:17:29 +0000426 *CurPtr != '\n' && *CurPtr != '\r' &&
427 (*CurPtr != 0 || CurPtr != CurBuf.end())) {
Chris Lattnercb307a272009-08-10 01:39:42 +0000428 ++CurPtr;
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000429 }
Chris Lattnercb307a272009-08-10 01:39:42 +0000430 return StringRef(TokStart, CurPtr-TokStart);
431}
Chris Lattnerd0765612009-06-21 19:21:25 +0000432
Kevin Enderby72553612011-09-13 23:45:18 +0000433StringRef AsmLexer::LexUntilEndOfLine() {
434 TokStart = CurPtr;
435
Rafael Espindola8026bd02014-07-06 14:17:29 +0000436 while (*CurPtr != '\n' && *CurPtr != '\r' &&
437 (*CurPtr != 0 || CurPtr != CurBuf.end())) {
Kevin Enderby72553612011-09-13 23:45:18 +0000438 ++CurPtr;
439 }
440 return StringRef(TokStart, CurPtr-TokStart);
441}
442
Benjamin Kramer1ee99a82015-08-17 14:35:25 +0000443size_t AsmLexer::peekTokens(MutableArrayRef<AsmToken> Buf,
444 bool ShouldSkipSpace) {
Saleem Abdulrasoola879fab2014-02-09 23:29:24 +0000445 const char *SavedTokStart = TokStart;
446 const char *SavedCurPtr = CurPtr;
447 bool SavedAtStartOfLine = isAtStartOfLine;
448 bool SavedSkipSpace = SkipSpace;
449
450 std::string SavedErr = getErr();
451 SMLoc SavedErrLoc = getErrLoc();
452
453 SkipSpace = ShouldSkipSpace;
Benjamin Kramer1ee99a82015-08-17 14:35:25 +0000454
455 size_t ReadCount;
456 for (ReadCount = 0; ReadCount < Buf.size(); ++ReadCount) {
457 AsmToken Token = LexToken();
458
459 Buf[ReadCount] = Token;
460
461 if (Token.is(AsmToken::Eof))
462 break;
463 }
Saleem Abdulrasoola879fab2014-02-09 23:29:24 +0000464
465 SetError(SavedErrLoc, SavedErr);
466
467 SkipSpace = SavedSkipSpace;
468 isAtStartOfLine = SavedAtStartOfLine;
469 CurPtr = SavedCurPtr;
470 TokStart = SavedTokStart;
471
Benjamin Kramer1ee99a82015-08-17 14:35:25 +0000472 return ReadCount;
Saleem Abdulrasoola879fab2014-02-09 23:29:24 +0000473}
474
Saleem Abdulrasoolbb67af42014-08-14 02:51:43 +0000475bool AsmLexer::isAtStartOfComment(const char *Ptr) {
476 const char *CommentString = MAI.getCommentString();
477
478 if (CommentString[1] == '\0')
479 return CommentString[0] == Ptr[0];
480
481 // FIXME: special case for the bogus "##" comment string in X86MCAsmInfoDarwin
482 if (CommentString[1] == '#')
483 return CommentString[0] == Ptr[0];
484
485 return strncmp(Ptr, CommentString, strlen(CommentString)) == 0;
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000486}
487
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000488bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
489 return strncmp(Ptr, MAI.getSeparatorString(),
490 strlen(MAI.getSeparatorString())) == 0;
491}
492
Daniel Dunbar8368f4e2009-07-28 03:00:54 +0000493AsmToken AsmLexer::LexToken() {
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000494 TokStart = CurPtr;
495 // This always consumes at least one character.
496 int CurChar = getNextChar();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000497
Saleem Abdulrasoolbb67af42014-08-14 02:51:43 +0000498 if (isAtStartOfComment(TokStart)) {
Kevin Enderby72553612011-09-13 23:45:18 +0000499 // If this comment starts with a '#', then return the Hash token and let
500 // the assembler parser see if it can be parsed as a cpp line filename
501 // comment. We do this only if we are at the start of a line.
502 if (CurChar == '#' && isAtStartOfLine)
503 return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
504 isAtStartOfLine = true;
Kevin Enderbyecd879a2009-09-16 18:08:00 +0000505 return LexLineComment();
Kevin Enderby72553612011-09-13 23:45:18 +0000506 }
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000507 if (isAtStatementSeparator(TokStart)) {
508 CurPtr += strlen(MAI.getSeparatorString()) - 1;
509 return AsmToken(AsmToken::EndOfStatement,
510 StringRef(TokStart, strlen(MAI.getSeparatorString())));
511 }
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000512
Jim Grosbacha9aa3c12011-09-15 16:52:06 +0000513 // If we're missing a newline at EOF, make sure we still get an
514 // EndOfStatement token before the Eof token.
515 if (CurChar == EOF && !isAtStartOfLine) {
516 isAtStartOfLine = true;
517 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
518 }
519
520 isAtStartOfLine = false;
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000521 switch (CurChar) {
522 default:
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000523 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
524 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
Chris Lattnerd0765612009-06-21 19:21:25 +0000525 return LexIdentifier();
Jim Grosbach01af6c42011-02-11 19:05:56 +0000526
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000527 // Unknown character, emit an error.
Chris Lattnerb0133452009-06-21 20:16:42 +0000528 return ReturnError(TokStart, "invalid character in input");
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000529 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000530 case 0:
531 case ' ':
532 case '\t':
Preston Gurd05500642012-09-19 20:36:12 +0000533 if (SkipSpace) {
534 // Ignore whitespace.
535 return LexToken();
536 } else {
537 int len = 1;
538 while (*CurPtr==' ' || *CurPtr=='\t') {
539 CurPtr++;
540 len++;
541 }
542 return AsmToken(AsmToken::Space, StringRef(TokStart, len));
543 }
Chris Lattnerd0765612009-06-21 19:21:25 +0000544 case '\n': // FALL THROUGH.
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000545 case '\r':
Kevin Enderby72553612011-09-13 23:45:18 +0000546 isAtStartOfLine = true;
Jim Grosbacha3df87f2011-03-24 18:46:34 +0000547 return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000548 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
549 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
550 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
551 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
552 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
553 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
Kevin Enderby9c0f7fc2009-09-04 22:40:31 +0000554 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
555 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
556 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
557 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000558 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
559 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
Daniel Dunbarb0ceb762010-05-06 14:46:38 +0000560 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
Matt Flemingec9d6fa2010-05-21 11:36:59 +0000561 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
Rafael Espindola1134ab232011-06-05 02:43:45 +0000562 case '\\': return AsmToken(AsmToken::BackSlash, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000563 case '=':
Richard Trieu7a083812016-02-18 22:09:30 +0000564 if (*CurPtr == '=') {
565 ++CurPtr;
566 return AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
567 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000568 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000569 case '|':
Richard Trieu7a083812016-02-18 22:09:30 +0000570 if (*CurPtr == '|') {
571 ++CurPtr;
572 return AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
573 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000574 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
575 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000576 case '&':
Richard Trieu7a083812016-02-18 22:09:30 +0000577 if (*CurPtr == '&') {
578 ++CurPtr;
579 return AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
580 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000581 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
Jim Grosbach01af6c42011-02-11 19:05:56 +0000582 case '!':
Richard Trieu7a083812016-02-18 22:09:30 +0000583 if (*CurPtr == '=') {
584 ++CurPtr;
585 return AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
586 }
Daniel Dunbarf2dcd772009-07-28 16:08:33 +0000587 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
Kevin Enderby7d912182009-09-03 17:15:07 +0000588 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
Chris Lattnerd0765612009-06-21 19:21:25 +0000589 case '/': return LexSlash();
Kevin Enderbyf92f9902009-09-04 21:45:34 +0000590 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
Roman Divacky71d29162010-12-18 08:56:37 +0000591 case '\'': return LexSingleQuote();
Chris Lattner419a9742009-06-21 19:56:35 +0000592 case '"': return LexQuote();
Chris Lattnerd0765612009-06-21 19:21:25 +0000593 case '0': case '1': case '2': case '3': case '4':
594 case '5': case '6': case '7': case '8': case '9':
595 return LexDigit();
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000596 case '<':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000597 switch (*CurPtr) {
Richard Trieu7a083812016-02-18 22:09:30 +0000598 case '<':
599 ++CurPtr;
600 return AsmToken(AsmToken::LessLess, StringRef(TokStart, 2));
601 case '=':
602 ++CurPtr;
603 return AsmToken(AsmToken::LessEqual, StringRef(TokStart, 2));
604 case '>':
605 ++CurPtr;
606 return AsmToken(AsmToken::LessGreater, StringRef(TokStart, 2));
607 default:
608 return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000609 }
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000610 case '>':
Daniel Dunbar7e8d6c72009-06-29 20:37:27 +0000611 switch (*CurPtr) {
Richard Trieu7a083812016-02-18 22:09:30 +0000612 case '>':
613 ++CurPtr;
614 return AsmToken(AsmToken::GreaterGreater, StringRef(TokStart, 2));
615 case '=':
616 ++CurPtr;
617 return AsmToken(AsmToken::GreaterEqual, StringRef(TokStart, 2));
618 default:
619 return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
Chris Lattnerf97d8bb2009-06-23 05:57:07 +0000620 }
Jim Grosbach01af6c42011-02-11 19:05:56 +0000621
Chris Lattnerd0765612009-06-21 19:21:25 +0000622 // TODO: Quoted identifiers (objc methods etc)
623 // local labels: [0-9][:]
624 // Forward/backward labels: [0-9][fb]
625 // Integers, fp constants, character constants.
Chris Lattnerc8dfbcb2009-06-21 07:19:10 +0000626 }
Duncan Sands376c6f12009-06-22 06:59:32 +0000627}