1 //===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the lexer for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/MC/MCParser/AsmLexer.h"
15 #include "llvm/Support/SMLoc.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/MC/MCAsmInfo.h"
23 AsmLexer::AsmLexer(const MCAsmInfo
&_MAI
) : MAI(_MAI
) {
28 AsmLexer::~AsmLexer() {
31 void AsmLexer::setBuffer(const MemoryBuffer
*buf
, const char *ptr
) {
37 CurPtr
= CurBuf
->getBufferStart();
42 /// ReturnError - Set the error to the specified string at the specified
43 /// location. This is defined to always return AsmToken::Error.
44 AsmToken
AsmLexer::ReturnError(const char *Loc
, const std::string
&Msg
) {
45 SetError(SMLoc::getFromPointer(Loc
), Msg
);
47 return AsmToken(AsmToken::Error
, StringRef(Loc
, 0));
50 int AsmLexer::getNextChar() {
51 char CurChar
= *CurPtr
++;
54 return (unsigned char)CurChar
;
56 // A nul character in the stream is either the end of the current buffer or
57 // a random nul in the file. Disambiguate that here.
58 if (CurPtr
-1 != CurBuf
->getBufferEnd())
59 return 0; // Just whitespace.
61 // Otherwise, return end of file.
62 --CurPtr
; // Another call to lex will return EOF again.
67 /// LexFloatLiteral: [0-9]*[.][0-9]*([eE][+-]?[0-9]*)?
69 /// The leading integral digit sequence and dot should have already been
70 /// consumed, some or all of the fractional digit sequence *can* have been
72 AsmToken
AsmLexer::LexFloatLiteral() {
73 // Skip the fractional digit sequence.
74 while (isdigit(*CurPtr
))
77 // Check for exponent; we intentionally accept a slighlty wider set of
78 // literals here and rely on the upstream client to reject invalid ones (e.g.,
80 if (*CurPtr
== 'e' || *CurPtr
== 'E') {
82 if (*CurPtr
== '-' || *CurPtr
== '+')
84 while (isdigit(*CurPtr
))
88 return AsmToken(AsmToken::Real
,
89 StringRef(TokStart
, CurPtr
- TokStart
));
92 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
93 static bool IsIdentifierChar(char c
) {
94 return isalnum(c
) || c
== '_' || c
== '$' || c
== '.' || c
== '@';
96 AsmToken
AsmLexer::LexIdentifier() {
97 // Check for floating point literals.
98 if (CurPtr
[-1] == '.' && isdigit(*CurPtr
)) {
99 // Disambiguate a .1243foo identifier from a floating literal.
100 while (isdigit(*CurPtr
))
102 if (*CurPtr
== 'e' || *CurPtr
== 'E' || !IsIdentifierChar(*CurPtr
))
103 return LexFloatLiteral();
106 while (IsIdentifierChar(*CurPtr
))
109 // Handle . as a special case.
110 if (CurPtr
== TokStart
+1 && TokStart
[0] == '.')
111 return AsmToken(AsmToken::Dot
, StringRef(TokStart
, 1));
113 return AsmToken(AsmToken::Identifier
, StringRef(TokStart
, CurPtr
- TokStart
));
116 /// LexSlash: Slash: /
117 /// C-Style Comment: /* ... */
118 AsmToken
AsmLexer::LexSlash() {
120 case '*': break; // C style comment.
121 case '/': return ++CurPtr
, LexLineComment();
122 default: return AsmToken(AsmToken::Slash
, StringRef(CurPtr
-1, 1));
126 ++CurPtr
; // skip the star.
128 int CurChar
= getNextChar();
131 return ReturnError(TokStart
, "unterminated comment");
133 // End of the comment?
134 if (CurPtr
[0] != '/') break;
136 ++CurPtr
; // End the */.
142 /// LexLineComment: Comment: #[^\n]*
144 AsmToken
AsmLexer::LexLineComment() {
145 // FIXME: This is broken if we happen to a comment at the end of a file, which
146 // was .included, and which doesn't end with a newline.
147 int CurChar
= getNextChar();
148 while (CurChar
!= '\n' && CurChar
!= '\n' && CurChar
!= EOF
)
149 CurChar
= getNextChar();
152 return AsmToken(AsmToken::Eof
, StringRef(CurPtr
, 0));
153 return AsmToken(AsmToken::EndOfStatement
, StringRef(CurPtr
, 0));
156 static void SkipIgnoredIntegerSuffix(const char *&CurPtr
) {
157 if (CurPtr
[0] == 'L' && CurPtr
[1] == 'L')
159 if (CurPtr
[0] == 'U' && CurPtr
[1] == 'L' && CurPtr
[2] == 'L')
163 /// LexDigit: First character is [0-9].
164 /// Local Label: [0-9][:]
165 /// Forward/Backward Label: [0-9][fb]
166 /// Binary integer: 0b[01]+
167 /// Octal integer: 0[0-7]+
168 /// Hex integer: 0x[0-9a-fA-F]+
169 /// Decimal integer: [1-9][0-9]*
170 AsmToken
AsmLexer::LexDigit() {
171 // Decimal integer: [1-9][0-9]*
172 if (CurPtr
[-1] != '0' || CurPtr
[0] == '.') {
173 while (isdigit(*CurPtr
))
176 // Check for floating point literals.
177 if (*CurPtr
== '.' || *CurPtr
== 'e') {
179 return LexFloatLiteral();
182 StringRef
Result(TokStart
, CurPtr
- TokStart
);
185 if (Result
.getAsInteger(10, Value
)) {
186 // We have to handle minint_as_a_positive_value specially, because
187 // - minint_as_a_positive_value = minint and it is valid.
188 if (Result
== "9223372036854775808")
189 Value
= -9223372036854775808ULL;
191 return ReturnError(TokStart
, "Invalid decimal number");
194 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
195 // suffixes on integer literals.
196 SkipIgnoredIntegerSuffix(CurPtr
);
198 return AsmToken(AsmToken::Integer
, Result
, Value
);
201 if (*CurPtr
== 'b') {
203 // See if we actually have "0b" as part of something like "jmp 0b\n"
204 if (!isdigit(CurPtr
[0])) {
206 StringRef
Result(TokStart
, CurPtr
- TokStart
);
207 return AsmToken(AsmToken::Integer
, Result
, 0);
209 const char *NumStart
= CurPtr
;
210 while (CurPtr
[0] == '0' || CurPtr
[0] == '1')
213 // Requires at least one binary digit.
214 if (CurPtr
== NumStart
)
215 return ReturnError(TokStart
, "Invalid binary number");
217 StringRef
Result(TokStart
, CurPtr
- TokStart
);
220 if (Result
.substr(2).getAsInteger(2, Value
))
221 return ReturnError(TokStart
, "Invalid binary number");
223 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
224 // suffixes on integer literals.
225 SkipIgnoredIntegerSuffix(CurPtr
);
227 return AsmToken(AsmToken::Integer
, Result
, Value
);
230 if (*CurPtr
== 'x') {
232 const char *NumStart
= CurPtr
;
233 while (isxdigit(CurPtr
[0]))
236 // Requires at least one hex digit.
237 if (CurPtr
== NumStart
)
238 return ReturnError(CurPtr
-2, "Invalid hexadecimal number");
240 unsigned long long Result
;
241 if (StringRef(TokStart
, CurPtr
- TokStart
).getAsInteger(0, Result
))
242 return ReturnError(TokStart
, "Invalid hexadecimal number");
244 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
245 // suffixes on integer literals.
246 SkipIgnoredIntegerSuffix(CurPtr
);
248 return AsmToken(AsmToken::Integer
, StringRef(TokStart
, CurPtr
- TokStart
),
252 // Must be an octal number, it starts with 0.
253 while (*CurPtr
>= '0' && *CurPtr
<= '7')
256 StringRef
Result(TokStart
, CurPtr
- TokStart
);
258 if (Result
.getAsInteger(8, Value
))
259 return ReturnError(TokStart
, "Invalid octal number");
261 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
262 // suffixes on integer literals.
263 SkipIgnoredIntegerSuffix(CurPtr
);
265 return AsmToken(AsmToken::Integer
, Result
, Value
);
268 /// LexQuote: String: "..."
269 AsmToken
AsmLexer::LexQuote() {
270 int CurChar
= getNextChar();
271 // TODO: does gas allow multiline string constants?
272 while (CurChar
!= '"') {
273 if (CurChar
== '\\') {
275 CurChar
= getNextChar();
279 return ReturnError(TokStart
, "unterminated string constant");
281 CurChar
= getNextChar();
284 return AsmToken(AsmToken::String
, StringRef(TokStart
, CurPtr
- TokStart
));
287 StringRef
AsmLexer::LexUntilEndOfStatement() {
290 while (!isAtStartOfComment(*CurPtr
) && // Start of line comment.
291 *CurPtr
!= ';' && // End of statement marker.
294 (*CurPtr
!= 0 || CurPtr
!= CurBuf
->getBufferEnd())) {
297 return StringRef(TokStart
, CurPtr
-TokStart
);
300 bool AsmLexer::isAtStartOfComment(char Char
) {
301 // FIXME: This won't work for multi-character comment indicators like "//".
302 return Char
== *MAI
.getCommentString();
305 AsmToken
AsmLexer::LexToken() {
307 // This always consumes at least one character.
308 int CurChar
= getNextChar();
310 if (isAtStartOfComment(CurChar
))
311 return LexLineComment();
315 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
316 if (isalpha(CurChar
) || CurChar
== '_' || CurChar
== '.')
317 return LexIdentifier();
319 // Unknown character, emit an error.
320 return ReturnError(TokStart
, "invalid character in input");
321 case EOF
: return AsmToken(AsmToken::Eof
, StringRef(TokStart
, 0));
325 // Ignore whitespace.
327 case '\n': // FALL THROUGH.
328 case '\r': // FALL THROUGH.
329 case ';': return AsmToken(AsmToken::EndOfStatement
, StringRef(TokStart
, 1));
330 case ':': return AsmToken(AsmToken::Colon
, StringRef(TokStart
, 1));
331 case '+': return AsmToken(AsmToken::Plus
, StringRef(TokStart
, 1));
332 case '-': return AsmToken(AsmToken::Minus
, StringRef(TokStart
, 1));
333 case '~': return AsmToken(AsmToken::Tilde
, StringRef(TokStart
, 1));
334 case '(': return AsmToken(AsmToken::LParen
, StringRef(TokStart
, 1));
335 case ')': return AsmToken(AsmToken::RParen
, StringRef(TokStart
, 1));
336 case '[': return AsmToken(AsmToken::LBrac
, StringRef(TokStart
, 1));
337 case ']': return AsmToken(AsmToken::RBrac
, StringRef(TokStart
, 1));
338 case '{': return AsmToken(AsmToken::LCurly
, StringRef(TokStart
, 1));
339 case '}': return AsmToken(AsmToken::RCurly
, StringRef(TokStart
, 1));
340 case '*': return AsmToken(AsmToken::Star
, StringRef(TokStart
, 1));
341 case ',': return AsmToken(AsmToken::Comma
, StringRef(TokStart
, 1));
342 case '$': return AsmToken(AsmToken::Dollar
, StringRef(TokStart
, 1));
343 case '@': return AsmToken(AsmToken::At
, StringRef(TokStart
, 1));
346 return ++CurPtr
, AsmToken(AsmToken::EqualEqual
, StringRef(TokStart
, 2));
347 return AsmToken(AsmToken::Equal
, StringRef(TokStart
, 1));
350 return ++CurPtr
, AsmToken(AsmToken::PipePipe
, StringRef(TokStart
, 2));
351 return AsmToken(AsmToken::Pipe
, StringRef(TokStart
, 1));
352 case '^': return AsmToken(AsmToken::Caret
, StringRef(TokStart
, 1));
355 return ++CurPtr
, AsmToken(AsmToken::AmpAmp
, StringRef(TokStart
, 2));
356 return AsmToken(AsmToken::Amp
, StringRef(TokStart
, 1));
359 return ++CurPtr
, AsmToken(AsmToken::ExclaimEqual
, StringRef(TokStart
, 2));
360 return AsmToken(AsmToken::Exclaim
, StringRef(TokStart
, 1));
361 case '%': return AsmToken(AsmToken::Percent
, StringRef(TokStart
, 1));
362 case '/': return LexSlash();
363 case '#': return AsmToken(AsmToken::Hash
, StringRef(TokStart
, 1));
364 case '"': return LexQuote();
365 case '0': case '1': case '2': case '3': case '4':
366 case '5': case '6': case '7': case '8': case '9':
370 case '<': return ++CurPtr
, AsmToken(AsmToken::LessLess
,
371 StringRef(TokStart
, 2));
372 case '=': return ++CurPtr
, AsmToken(AsmToken::LessEqual
,
373 StringRef(TokStart
, 2));
374 case '>': return ++CurPtr
, AsmToken(AsmToken::LessGreater
,
375 StringRef(TokStart
, 2));
376 default: return AsmToken(AsmToken::Less
, StringRef(TokStart
, 1));
380 case '>': return ++CurPtr
, AsmToken(AsmToken::GreaterGreater
,
381 StringRef(TokStart
, 2));
382 case '=': return ++CurPtr
, AsmToken(AsmToken::GreaterEqual
,
383 StringRef(TokStart
, 2));
384 default: return AsmToken(AsmToken::Greater
, StringRef(TokStart
, 1));
387 // TODO: Quoted identifiers (objc methods etc)
388 // local labels: [0-9][:]
389 // Forward/backward labels: [0-9][fb]
390 // Integers, fp constants, character constants.