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 AsmToken AsmLexer::LexIdentifier() {
69 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
70 *CurPtr == '.' || *CurPtr == '@')
73 // Handle . as a special case.
74 if (CurPtr == TokStart+1 && TokStart[0] == '.')
75 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
77 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
80 /// LexSlash: Slash: /
81 /// C-Style Comment: /* ... */
82 AsmToken AsmLexer::LexSlash() {
84 case '*': break; // C style comment.
85 case '/': return ++CurPtr, LexLineComment();
86 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
90 ++CurPtr; // skip the star.
92 int CurChar = getNextChar();
95 return ReturnError(TokStart, "unterminated comment");
97 // End of the comment?
98 if (CurPtr[0] != '/') break;
100 ++CurPtr; // End the */.
106 /// LexLineComment: Comment: #[^\n]*
108 AsmToken AsmLexer::LexLineComment() {
109 // FIXME: This is broken if we happen to a comment at the end of a file, which
110 // was .included, and which doesn't end with a newline.
111 int CurChar = getNextChar();
112 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
113 CurChar = getNextChar();
116 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
117 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
120 static void SkipIgnoredIntegerSuffix(const char *&CurPtr) {
121 if (CurPtr[0] == 'L' && CurPtr[1] == 'L')
123 if (CurPtr[0] == 'U' && CurPtr[1] == 'L' && CurPtr[2] == 'L')
128 /// LexDigit: First character is [0-9].
129 /// Local Label: [0-9][:]
130 /// Forward/Backward Label: [0-9][fb]
131 /// Binary integer: 0b[01]+
132 /// Octal integer: 0[0-7]+
133 /// Hex integer: 0x[0-9a-fA-F]+
134 /// Decimal integer: [1-9][0-9]*
135 /// TODO: FP literal.
136 AsmToken AsmLexer::LexDigit() {
137 // Decimal integer: [1-9][0-9]*
138 if (CurPtr[-1] != '0') {
139 while (isdigit(*CurPtr))
142 StringRef Result(TokStart, CurPtr - TokStart);
145 if (Result.getAsInteger(10, Value)) {
146 // We have to handle minint_as_a_positive_value specially, because
147 // - minint_as_a_positive_value = minint and it is valid.
148 if (Result == "9223372036854775808")
149 Value = -9223372036854775808ULL;
151 return ReturnError(TokStart, "Invalid decimal number");
154 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
155 // suffixes on integer literals.
156 SkipIgnoredIntegerSuffix(CurPtr);
158 return AsmToken(AsmToken::Integer, Result, Value);
161 if (*CurPtr == 'b') {
163 // See if we actually have "0b" as part of something like "jmp 0b\n"
164 if (!isdigit(CurPtr[0])) {
166 StringRef Result(TokStart, CurPtr - TokStart);
167 return AsmToken(AsmToken::Integer, Result, 0);
169 const char *NumStart = CurPtr;
170 while (CurPtr[0] == '0' || CurPtr[0] == '1')
173 // Requires at least one binary digit.
174 if (CurPtr == NumStart)
175 return ReturnError(TokStart, "Invalid binary number");
177 StringRef Result(TokStart, CurPtr - TokStart);
180 if (Result.substr(2).getAsInteger(2, Value))
181 return ReturnError(TokStart, "Invalid binary number");
183 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
184 // suffixes on integer literals.
185 SkipIgnoredIntegerSuffix(CurPtr);
187 return AsmToken(AsmToken::Integer, Result, Value);
190 if (*CurPtr == 'x') {
192 const char *NumStart = CurPtr;
193 while (isxdigit(CurPtr[0]))
196 // Requires at least one hex digit.
197 if (CurPtr == NumStart)
198 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
200 unsigned long long Result;
201 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
202 return ReturnError(TokStart, "Invalid hexadecimal number");
204 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
205 // suffixes on integer literals.
206 SkipIgnoredIntegerSuffix(CurPtr);
208 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
212 // Must be an octal number, it starts with 0.
213 while (*CurPtr >= '0' && *CurPtr <= '7')
216 StringRef Result(TokStart, CurPtr - TokStart);
218 if (Result.getAsInteger(8, Value))
219 return ReturnError(TokStart, "Invalid octal number");
221 // The darwin/x86 (and x86-64) assembler accepts and ignores ULL and LL
222 // suffixes on integer literals.
223 SkipIgnoredIntegerSuffix(CurPtr);
225 return AsmToken(AsmToken::Integer, Result, Value);
228 /// LexQuote: String: "..."
229 AsmToken AsmLexer::LexQuote() {
230 int CurChar = getNextChar();
231 // TODO: does gas allow multiline string constants?
232 while (CurChar != '"') {
233 if (CurChar == '\\') {
235 CurChar = getNextChar();
239 return ReturnError(TokStart, "unterminated string constant");
241 CurChar = getNextChar();
244 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
247 StringRef AsmLexer::LexUntilEndOfStatement() {
250 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
251 *CurPtr != ';' && // End of statement marker.
254 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
257 return StringRef(TokStart, CurPtr-TokStart);
260 bool AsmLexer::isAtStartOfComment(char Char) {
261 // FIXME: This won't work for multi-character comment indicators like "//".
262 return Char == *MAI.getCommentString();
265 AsmToken AsmLexer::LexToken() {
267 // This always consumes at least one character.
268 int CurChar = getNextChar();
270 if (isAtStartOfComment(CurChar))
271 return LexLineComment();
275 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
276 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
277 return LexIdentifier();
279 // Unknown character, emit an error.
280 return ReturnError(TokStart, "invalid character in input");
281 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
285 // Ignore whitespace.
287 case '\n': // FALL THROUGH.
288 case '\r': // FALL THROUGH.
289 case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
290 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
291 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
292 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
293 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
294 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
295 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
296 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
297 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
298 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
299 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
300 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
301 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
302 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
303 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
306 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
307 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
310 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
311 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
312 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
315 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
316 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
319 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
320 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
321 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
322 case '/': return LexSlash();
323 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
324 case '"': return LexQuote();
325 case '0': case '1': case '2': case '3': case '4':
326 case '5': case '6': case '7': case '8': case '9':
330 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
331 StringRef(TokStart, 2));
332 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
333 StringRef(TokStart, 2));
334 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
335 StringRef(TokStart, 2));
336 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
340 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
341 StringRef(TokStart, 2));
342 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
343 StringRef(TokStart, 2));
344 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
347 // TODO: Quoted identifiers (objc methods etc)
348 // local labels: [0-9][:]
349 // Forward/backward labels: [0-9][fb]
350 // Integers, fp constants, character constants.