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 /// LexSingleQuote: Integer: 'b'
269 AsmToken AsmLexer::LexSingleQuote() {
270 int CurChar = getNextChar();
273 CurChar = getNextChar();
276 return ReturnError(TokStart, "unterminated single quote");
278 CurChar = getNextChar();
281 return ReturnError(TokStart, "single quote way too long");
283 // The idea here being that 'c' is basically just an integral
285 StringRef Res = StringRef(TokStart,CurPtr - TokStart);
288 if (Res.startswith("\'\\")) {
289 char theChar = Res[2];
291 default: Value = theChar; break;
292 case '\'': Value = '\''; break;
293 case 't': Value = '\t'; break;
294 case 'n': Value = '\n'; break;
295 case 'b': Value = '\b'; break;
300 return AsmToken(AsmToken::Integer, Res, Value);
304 /// LexQuote: String: "..."
305 AsmToken AsmLexer::LexQuote() {
306 int CurChar = getNextChar();
307 // TODO: does gas allow multiline string constants?
308 while (CurChar != '"') {
309 if (CurChar == '\\') {
311 CurChar = getNextChar();
315 return ReturnError(TokStart, "unterminated string constant");
317 CurChar = getNextChar();
320 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
323 StringRef AsmLexer::LexUntilEndOfStatement() {
326 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
327 *CurPtr != ';' && // End of statement marker.
330 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
333 return StringRef(TokStart, CurPtr-TokStart);
336 bool AsmLexer::isAtStartOfComment(char Char) {
337 // FIXME: This won't work for multi-character comment indicators like "//".
338 return Char == *MAI.getCommentString();
341 AsmToken AsmLexer::LexToken() {
343 // This always consumes at least one character.
344 int CurChar = getNextChar();
346 if (isAtStartOfComment(CurChar))
347 return LexLineComment();
351 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
352 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
353 return LexIdentifier();
355 // Unknown character, emit an error.
356 return ReturnError(TokStart, "invalid character in input");
357 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
361 // Ignore whitespace.
363 case '\n': // FALL THROUGH.
364 case '\r': // FALL THROUGH.
365 case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
366 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
367 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
368 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
369 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
370 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
371 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
372 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
373 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
374 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
375 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
376 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
377 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
378 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
379 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
382 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
383 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
386 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
387 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
388 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
391 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
392 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
395 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
396 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
397 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
398 case '/': return LexSlash();
399 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
400 case '\'': return LexSingleQuote();
401 case '"': return LexQuote();
402 case '0': case '1': case '2': case '3': case '4':
403 case '5': case '6': case '7': case '8': case '9':
407 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
408 StringRef(TokStart, 2));
409 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
410 StringRef(TokStart, 2));
411 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
412 StringRef(TokStart, 2));
413 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
417 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
418 StringRef(TokStart, 2));
419 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
420 StringRef(TokStart, 2));
421 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
424 // TODO: Quoted identifiers (objc methods etc)
425 // local labels: [0-9][:]
426 // Forward/backward labels: [0-9][fb]
427 // Integers, fp constants, character constants.