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 == '@')
78 // Handle . as a special case.
79 if (CurPtr == TokStart+1 && TokStart[0] == '.')
80 return AsmToken(AsmToken::Dot, StringRef(TokStart, 1));
82 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
85 /// LexSlash: Slash: /
86 /// C-Style Comment: /* ... */
87 AsmToken AsmLexer::LexSlash() {
89 case '*': break; // C style comment.
90 case '/': return ++CurPtr, LexLineComment();
91 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
95 ++CurPtr; // skip the star.
97 int CurChar = getNextChar();
100 return ReturnError(TokStart, "unterminated comment");
102 // End of the comment?
103 if (CurPtr[0] != '/') break;
105 ++CurPtr; // End the */.
111 /// LexLineComment: Comment: #[^\n]*
113 AsmToken AsmLexer::LexLineComment() {
114 // FIXME: This is broken if we happen to a comment at the end of a file, which
115 // was .included, and which doesn't end with a newline.
116 int CurChar = getNextChar();
117 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
118 CurChar = getNextChar();
121 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
122 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
126 /// LexDigit: First character is [0-9].
127 /// Local Label: [0-9][:]
128 /// Forward/Backward Label: [0-9][fb]
129 /// Binary integer: 0b[01]+
130 /// Octal integer: 0[0-7]+
131 /// Hex integer: 0x[0-9a-fA-F]+
132 /// Decimal integer: [1-9][0-9]*
133 /// TODO: FP literal.
134 AsmToken AsmLexer::LexDigit() {
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 // See if we actually have "0b" as part of something like "jmp 0b\n"
157 if (CurPtr[0] == '\n') {
159 StringRef Result(TokStart, CurPtr - TokStart);
161 return AsmToken(AsmToken::Integer, Result, 0);
163 const char *NumStart = CurPtr;
164 while (CurPtr[0] == '0' || CurPtr[0] == '1')
167 // Requires at least one binary digit.
168 if (CurPtr == NumStart)
169 return ReturnError(TokStart, "Invalid binary number");
171 StringRef Result(TokStart, CurPtr - TokStart);
174 if (Result.getAsInteger(2, Value))
175 return ReturnError(TokStart, "Invalid binary number");
177 return AsmToken(AsmToken::Integer, Result, Value);
180 if (*CurPtr == 'x') {
182 const char *NumStart = CurPtr;
183 while (isxdigit(CurPtr[0]))
186 // Requires at least one hex digit.
187 if (CurPtr == NumStart)
188 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
190 unsigned long long Result;
191 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
192 return ReturnError(TokStart, "Invalid hexadecimal number");
194 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
198 // Must be an octal number, it starts with 0.
199 while (*CurPtr >= '0' && *CurPtr <= '7')
202 StringRef Result(TokStart, CurPtr - TokStart);
204 if (Result.getAsInteger(8, Value))
205 return ReturnError(TokStart, "Invalid octal number");
207 return AsmToken(AsmToken::Integer, Result, Value);
210 /// LexQuote: String: "..."
211 AsmToken AsmLexer::LexQuote() {
212 int CurChar = getNextChar();
213 // TODO: does gas allow multiline string constants?
214 while (CurChar != '"') {
215 if (CurChar == '\\') {
217 CurChar = getNextChar();
221 return ReturnError(TokStart, "unterminated string constant");
223 CurChar = getNextChar();
226 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
229 StringRef AsmLexer::LexUntilEndOfStatement() {
232 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
233 *CurPtr != ';' && // End of statement marker.
236 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
239 return StringRef(TokStart, CurPtr-TokStart);
242 bool AsmLexer::isAtStartOfComment(char Char) {
243 // FIXME: This won't work for multi-character comment indicators like "//".
244 return Char == *MAI.getCommentString();
247 AsmToken AsmLexer::LexToken() {
249 // This always consumes at least one character.
250 int CurChar = getNextChar();
252 if (isAtStartOfComment(CurChar))
253 return LexLineComment();
257 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
258 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
259 return LexIdentifier();
261 // Unknown character, emit an error.
262 return ReturnError(TokStart, "invalid character in input");
263 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
267 // Ignore whitespace.
269 case '\n': // FALL THROUGH.
270 case '\r': // FALL THROUGH.
271 case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
272 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
273 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
274 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
275 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
276 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
277 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
278 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
279 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
280 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
281 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
282 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
283 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
284 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
287 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
288 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
291 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
292 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
293 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
296 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
297 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
300 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
301 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
302 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
303 case '/': return LexSlash();
304 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
305 case '"': return LexQuote();
306 case '0': case '1': case '2': case '3': case '4':
307 case '5': case '6': case '7': case '8': case '9':
311 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
312 StringRef(TokStart, 2));
313 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
314 StringRef(TokStart, 2));
315 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
316 StringRef(TokStart, 2));
317 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
321 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
322 StringRef(TokStart, 2));
323 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
324 StringRef(TokStart, 2));
325 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
328 // TODO: Quoted identifiers (objc methods etc)
329 // local labels: [0-9][:]
330 // Forward/backward labels: [0-9][fb]
331 // Integers, fp constants, character constants.