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));
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() {
130 // Decimal integer: [1-9][0-9]*
131 if (CurPtr[-1] != '0') {
132 while (isdigit(*CurPtr))
135 StringRef Result(TokStart, CurPtr - TokStart);
138 if (Result.getAsInteger(10, Value)) {
139 // We have to handle minint_as_a_positive_value specially, because
140 // - minint_as_a_positive_value = minint and it is valid.
141 if (Result == "9223372036854775808")
142 Value = -9223372036854775808ULL;
144 return ReturnError(TokStart, "Invalid decimal number");
146 return AsmToken(AsmToken::Integer, Result, Value);
149 if (*CurPtr == 'b') {
151 // See if we actually have "0b" as part of something like "jmp 0b\n"
152 if (!isdigit(CurPtr[0])) {
154 StringRef Result(TokStart, CurPtr - TokStart);
155 return AsmToken(AsmToken::Integer, Result, 0);
157 const char *NumStart = CurPtr;
158 while (CurPtr[0] == '0' || CurPtr[0] == '1')
161 // Requires at least one binary digit.
162 if (CurPtr == NumStart)
163 return ReturnError(TokStart, "Invalid binary number");
165 StringRef Result(TokStart, CurPtr - TokStart);
168 if (Result.getAsInteger(2, Value))
169 return ReturnError(TokStart, "Invalid binary number");
171 return AsmToken(AsmToken::Integer, Result, Value);
174 if (*CurPtr == 'x') {
176 const char *NumStart = CurPtr;
177 while (isxdigit(CurPtr[0]))
180 // Requires at least one hex digit.
181 if (CurPtr == NumStart)
182 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
184 unsigned long long Result;
185 if (StringRef(TokStart, CurPtr - TokStart).getAsInteger(0, Result))
186 return ReturnError(TokStart, "Invalid hexadecimal number");
188 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
192 // Must be an octal number, it starts with 0.
193 while (*CurPtr >= '0' && *CurPtr <= '7')
196 StringRef Result(TokStart, CurPtr - TokStart);
198 if (Result.getAsInteger(8, Value))
199 return ReturnError(TokStart, "Invalid octal number");
201 return AsmToken(AsmToken::Integer, Result, Value);
204 /// LexQuote: String: "..."
205 AsmToken AsmLexer::LexQuote() {
206 int CurChar = getNextChar();
207 // TODO: does gas allow multiline string constants?
208 while (CurChar != '"') {
209 if (CurChar == '\\') {
211 CurChar = getNextChar();
215 return ReturnError(TokStart, "unterminated string constant");
217 CurChar = getNextChar();
220 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
223 StringRef AsmLexer::LexUntilEndOfStatement() {
226 while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
227 *CurPtr != ';' && // End of statement marker.
230 (*CurPtr != 0 || CurPtr != CurBuf->getBufferEnd())) {
233 return StringRef(TokStart, CurPtr-TokStart);
236 bool AsmLexer::isAtStartOfComment(char Char) {
237 // FIXME: This won't work for multi-character comment indicators like "//".
238 return Char == *MAI.getCommentString();
241 AsmToken AsmLexer::LexToken() {
243 // This always consumes at least one character.
244 int CurChar = getNextChar();
246 if (isAtStartOfComment(CurChar))
247 return LexLineComment();
251 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
252 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
253 return LexIdentifier();
255 // Unknown character, emit an error.
256 return ReturnError(TokStart, "invalid character in input");
257 case EOF: return AsmToken(AsmToken::Eof, StringRef(TokStart, 0));
261 // Ignore whitespace.
263 case '\n': // FALL THROUGH.
264 case '\r': // FALL THROUGH.
265 case ';': return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
266 case ':': return AsmToken(AsmToken::Colon, StringRef(TokStart, 1));
267 case '+': return AsmToken(AsmToken::Plus, StringRef(TokStart, 1));
268 case '-': return AsmToken(AsmToken::Minus, StringRef(TokStart, 1));
269 case '~': return AsmToken(AsmToken::Tilde, StringRef(TokStart, 1));
270 case '(': return AsmToken(AsmToken::LParen, StringRef(TokStart, 1));
271 case ')': return AsmToken(AsmToken::RParen, StringRef(TokStart, 1));
272 case '[': return AsmToken(AsmToken::LBrac, StringRef(TokStart, 1));
273 case ']': return AsmToken(AsmToken::RBrac, StringRef(TokStart, 1));
274 case '{': return AsmToken(AsmToken::LCurly, StringRef(TokStart, 1));
275 case '}': return AsmToken(AsmToken::RCurly, StringRef(TokStart, 1));
276 case '*': return AsmToken(AsmToken::Star, StringRef(TokStart, 1));
277 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
278 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
279 case '@': return AsmToken(AsmToken::At, StringRef(TokStart, 1));
282 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
283 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
286 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
287 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
288 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
291 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
292 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
295 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
296 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
297 case '%': return AsmToken(AsmToken::Percent, StringRef(TokStart, 1));
298 case '/': return LexSlash();
299 case '#': return AsmToken(AsmToken::Hash, StringRef(TokStart, 1));
300 case '"': return LexQuote();
301 case '0': case '1': case '2': case '3': case '4':
302 case '5': case '6': case '7': case '8': case '9':
306 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
307 StringRef(TokStart, 2));
308 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
309 StringRef(TokStart, 2));
310 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
311 StringRef(TokStart, 2));
312 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
316 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
317 StringRef(TokStart, 2));
318 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
319 StringRef(TokStart, 2));
320 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
323 // TODO: Quoted identifiers (objc methods etc)
324 // local labels: [0-9][:]
325 // Forward/backward labels: [0-9][fb]
326 // Integers, fp constants, character constants.