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 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
68 static bool IsIdentifierChar(char c) {
69 return isalnum(c) || c == '_' || c == '$' || c == '.' || c == '@';
71 AsmToken AsmLexer::LexIdentifier() {
72 // Check for floating point literals.
73 if (CurPtr[-1] == '.' && isdigit(*CurPtr)) {
74 while (isdigit(*CurPtr))
76 if (!IsIdentifierChar(*CurPtr)) {
77 return AsmToken(AsmToken::Real,
78 StringRef(TokStart, CurPtr - TokStart));
82 while (IsIdentifierChar(*CurPtr))
85 // Handle . as a special case.
86 if (CurPtr == TokStart+1 && TokStart[0] == '.')
87 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
89 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
92 /// LexSlash: Slash: /
93 /// C-Style Comment: /* ... */
94 AsmToken AsmLexer::LexSlash() {
96 case '*': break; // C style comment.
97 case '/': return ++CurPtr, LexLineComment();
98 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
102 ++CurPtr; // skip the star.
104 int CurChar = getNextChar();
107 return ReturnError(TokStart, "unterminated comment");
109 // End of the comment?
110 if (CurPtr[0] != '/') break;
112 ++CurPtr; // End the */.
118 /// LexLineComment: Comment: #[^\n]*
120 AsmToken AsmLexer::LexLineComment() {
121 // FIXME: This is broken if we happen to a comment at the end of a file, which
122 // was .included, and which doesn't end with a newline.
123 int CurChar = getNextChar();
124 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
125 CurChar = getNextChar();
128 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
129 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
132 static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
133 if (CurPtr[0] == 'L' && CurPtr[1] == 'L')
135 if (CurPtr[0] == 'U' && CurPtr[1] == 'L' && CurPtr[2] == 'L')
139 /// LexDigit: First character is [0-9].
140 /// Local Label: [0-9][:]
141 /// Forward/Backward Label: [0-9][fb]
142 /// Binary integer: 0b[01]+
143 /// Octal integer: 0[0-7]+
144 /// Hex integer: 0x[0-9a-fA-F]+
145 /// Decimal integer: [1-9][0-9]*
146 AsmToken AsmLexer::LexDigit() {
147 // Decimal integer: [1-9][0-9]*
148 if (CurPtr[-1] != '0') {
149 while (isdigit(*CurPtr))
152 // Check for floating point literals.
153 if (*CurPtr == '.') {
155 while (isdigit(*CurPtr))
158 return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
161 StringRef Result(TokStart, CurPtr - TokStart);
164 if (Result.getAsInteger(10, Value)) {
165 // We have to handle minint_as_a_positive_value specially, because
166 // - minint_as_a_positive_value = minint and it is valid.
167 if (Result == "9223372036854775808")
168 Value = -9223372036854775808ULL;
170 return ReturnError(TokStart, "Invalid decimal number");
173 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
174 // suffixes on integer literals.
175 SkipIgnoredIntegerSuffix(CurPtr);
177 return AsmToken(AsmToken::Integer, Result, Value);
180 if (*CurPtr == 'b') {
182 // See if we actually have "0b" as part of something like "jmp 0b\n"
183 if (!isdigit(CurPtr[0])) {
185 StringRef Result(TokStart, CurPtr - TokStart);
186 return AsmToken(AsmToken::Integer, Result, 0);
188 const char *NumStart = CurPtr;
189 while (CurPtr[0] == '0' || CurPtr[0] == '1')
192 // Requires at least one binary digit.
193 if (CurPtr == NumStart)
194 return ReturnError(TokStart, "Invalid binary number");
196 StringRef Result(TokStart, CurPtr - TokStart);
199 if (Result.substr(2).getAsInteger(2, Value))
200 return ReturnError(TokStart, "Invalid binary number");
202 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
203 // suffixes on integer literals.
204 SkipIgnoredIntegerSuffix(CurPtr);
206 return AsmToken(AsmToken::Integer, Result, Value);
209 if (*CurPtr == 'x') {
211 const char *NumStart = CurPtr;
212 while (isxdigit(CurPtr[0]))
215 // Requires at least one hex digit.
216 if (CurPtr == NumStart)
217 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
219 unsigned long long Result;
220 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
221 return ReturnError(TokStart, "Invalid hexadecimal 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, StringRef(TokStart, CurPtr - TokStart),
231 // Must be an octal number, it starts with 0.
232 while (*CurPtr >= '0' && *CurPtr <= '7')
235 StringRef Result(TokStart, CurPtr - TokStart);
237 if (Result.getAsInteger(8, Value))
238 return ReturnError(TokStart, "Invalid octal number");
240 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
241 // suffixes on integer literals.
242 SkipIgnoredIntegerSuffix(CurPtr);
244 return AsmToken(AsmToken::Integer, Result, Value);
247 /// LexQuote: String: "..."
248 AsmToken AsmLexer::LexQuote() {
249 int CurChar = getNextChar();
250 // TODO: does gas allow multiline string constants?
251 while (CurChar != '"') {
252 if (CurChar == '\\') {
254 CurChar = getNextChar();
258 return ReturnError(TokStart, "unterminated string constant");
260 CurChar = getNextChar();
263 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
266 StringRef AsmLexer::LexUntilEndOfStatement() {
269 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
270 *CurPtr != ';' && // End of statement marker.
273 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
276 return StringRef(TokStart, CurPtr-TokStart);
279 bool AsmLexer::isAtStartOfComment(char Char) {
280 // FIXME: This won't work for multi-character comment indicators like "//".
281 return Char == *MAI.getCommentString();
284 AsmToken AsmLexer::LexToken() {
286 // This always consumes at least one character.
287 int CurChar = getNextChar();
289 if (isAtStartOfComment(CurChar))
290 return LexLineComment();
294 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
295 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
296 return LexIdentifier();
298 // Unknown character, emit an error.
299 return ReturnError(TokStart, "invalid character in input");
300 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
304 // Ignore whitespace.
306 case '\n': // FALL THROUGH.
307 case '\r': // FALL THROUGH.
308 case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
309 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
310 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
311 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
312 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
313 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
314 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
315 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
316 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
317 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
318 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
319 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
320 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
321 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
322 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
325 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
326 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
329 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
330 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
331 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
334 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
335 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
338 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
339 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
340 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
341 case '/': return LexSlash();
342 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
343 case '"': return LexQuote();
344 case '0': case '1': case '2': case '3': case '4':
345 case '5': case '6': case '7': case '8': case '9':
349 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
350 StringRef(TokStart, 2));
351 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
352 StringRef(TokStart, 2));
353 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
354 StringRef(TokStart, 2));
355 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
359 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
360 StringRef(TokStart, 2));
361 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
362 StringRef(TokStart, 2));
363 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
366 // TODO: Quoted identifiers (objc methods etc)
367 // local labels: [0-9][:]
368 // Forward/backward labels: [0-9][fb]
369 // Integers, fp constants, character constants.