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) {
29 AsmLexer::~AsmLexer() {
32 void AsmLexer::setBuffer(const MemoryBuffer *buf, const char *ptr) {
38 CurPtr = CurBuf->getBufferStart();
43 SMLoc AsmLexer::getLoc() const {
44 return SMLoc::getFromPointer(TokStart);
47 /// ReturnError - Set the error to the specified string at the specified
48 /// location. This is defined to always return AsmToken::Error.
49 AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
50 SetError(SMLoc::getFromPointer(Loc), Msg);
52 return AsmToken(AsmToken::Error, StringRef(Loc, 0));
55 int AsmLexer::getNextChar() {
56 char CurChar = *CurPtr++;
59 return (unsigned char)CurChar;
61 // A nul character in the stream is either the end of the current buffer or
62 // a random nul in the file. Disambiguate that here.
63 if (CurPtr-1 != CurBuf->getBufferEnd())
64 return 0; // Just whitespace.
66 // Otherwise, return end of file.
67 --CurPtr; // Another call to lex will return EOF again.
72 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
73 AsmToken AsmLexer::LexIdentifier() {
74 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
75 *CurPtr == '.' || *CurPtr == '@')
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));
121 /// LexDigit: First character is [0-9].
122 /// Local Label: [0-9][:]
123 /// Forward/Backward Label: [0-9][fb]
124 /// Binary integer: 0b[01]+
125 /// Octal integer: 0[0-7]+
126 /// Hex integer: 0x[0-9a-fA-F]+
127 /// Decimal integer: [1-9][0-9]*
128 /// TODO: FP literal.
129 AsmToken AsmLexer::LexDigit() {
131 return ReturnError(TokStart, "FIXME: local label not implemented");
132 if (*CurPtr == 'f' || *CurPtr == 'b')
133 return ReturnError(TokStart, "FIXME: directional label not implemented");
135 // Decimal integer: [1-9][0-9]*
136 if (CurPtr[-1] != '0') {
137 while (isdigit(*CurPtr))
140 StringRef Result(TokStart, CurPtr - TokStart);
143 if (Result.getAsInteger(10, Value)) {
144 // We have to handle minint_as_a_positive_value specially, because
145 // - minint_as_a_positive_value = minint and it is valid.
146 if (Result == "9223372036854775808")
147 Value = -9223372036854775808ULL;
149 return ReturnError(TokStart, "Invalid decimal number");
151 return AsmToken(AsmToken::Integer, Result, Value);
154 if (*CurPtr == 'b') {
156 const char *NumStart = CurPtr;
157 while (CurPtr[0] == '0' || CurPtr[0] == '1')
160 // Requires at least one binary digit.
161 if (CurPtr == NumStart)
162 return ReturnError(TokStart, "Invalid binary number");
164 StringRef Result(TokStart, CurPtr - TokStart);
167 if (Result.getAsInteger(2, Value))
168 return ReturnError(TokStart, "Invalid binary number");
170 return AsmToken(AsmToken::Integer, Result, Value);
173 if (*CurPtr == 'x') {
175 const char *NumStart = CurPtr;
176 while (isxdigit(CurPtr[0]))
179 // Requires at least one hex digit.
180 if (CurPtr == NumStart)
181 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
183 unsigned long long Result;
184 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
185 return ReturnError(TokStart, "Invalid hexadecimal number");
187 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
191 // Must be an octal number, it starts with 0.
192 while (*CurPtr >= '0' && *CurPtr <= '7')
195 StringRef Result(TokStart, CurPtr - TokStart);
197 if (Result.getAsInteger(8, Value))
198 return ReturnError(TokStart, "Invalid octal number");
200 return AsmToken(AsmToken::Integer, Result, Value);
203 /// LexQuote: String: "..."
204 AsmToken AsmLexer::LexQuote() {
205 int CurChar = getNextChar();
206 // TODO: does gas allow multiline string constants?
207 while (CurChar != '"') {
208 if (CurChar == '\\') {
210 CurChar = getNextChar();
214 return ReturnError(TokStart, "unterminated string constant");
216 CurChar = getNextChar();
219 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
222 StringRef AsmLexer::LexUntilEndOfStatement() {
225 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
226 *CurPtr != ';' && // End of statement marker.
229 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
232 return StringRef(TokStart, CurPtr-TokStart);
235 bool AsmLexer::isAtStartOfComment(char Char) {
236 // FIXME: This won't work for multi-character comment indicators like "//".
237 return Char == *MAI.getCommentString();
240 AsmToken AsmLexer::LexToken() {
242 // This always consumes at least one character.
243 int CurChar = getNextChar();
245 if (isAtStartOfComment(CurChar))
246 return LexLineComment();
250 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
251 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
252 return LexIdentifier();
254 // Unknown character, emit an error.
255 return ReturnError(TokStart, "invalid character in input");
256 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
260 // Ignore whitespace.
262 case '\n': // FALL THROUGH.
263 case '\r': // FALL THROUGH.
264 case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
265 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
266 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
267 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
268 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
269 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
270 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
271 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
272 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
273 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
274 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
275 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
276 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
277 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
280 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
281 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
284 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
285 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
286 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
289 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
290 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
293 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
294 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
295 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
296 case '/': return LexSlash();
297 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
298 case '"': return LexQuote();
299 case '0': case '1': case '2': case '3': case '4':
300 case '5': case '6': case '7': case '8': case '9':
304 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
305 StringRef(TokStart, 2));
306 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
307 StringRef(TokStart, 2));
308 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
309 StringRef(TokStart, 2));
310 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
314 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
315 StringRef(TokStart, 2));
316 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
317 StringRef(TokStart, 2));
318 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
321 // TODO: Quoted identifiers (objc methods etc)
322 // local labels: [0-9][:]
323 // Forward/backward labels: [0-9][fb]
324 // Integers, fp constants, character constants.