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 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Config/config.h" // for strtoull.
23 AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
25 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
26 CurPtr = CurBuf->getBufferStart();
27 CurTok = AsmToken(AsmToken::Error, StringRef(CurPtr, 0));
31 AsmLexer::~AsmLexer() {
34 SMLoc AsmLexer::getLoc() const {
35 return SMLoc::getFromPointer(TokStart);
38 void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg,
39 const char *Type) const {
40 SrcMgr.PrintMessage(Loc, Msg, Type);
43 /// ReturnError - Set the error to the specified string at the specified
44 /// location. This is defined to always return AsmToken::Error.
45 AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
46 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
47 return AsmToken(AsmToken::Error, StringRef(Loc, 0));
50 /// EnterIncludeFile - Enter the specified file. This prints an error and
51 /// returns true on failure.
52 bool AsmLexer::EnterIncludeFile(const std::string &Filename) {
53 int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
57 // Save the line number and lex buffer of the includer.
59 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
60 CurPtr = CurBuf->getBufferStart();
65 int AsmLexer::getNextChar() {
66 char CurChar = *CurPtr++;
69 return (unsigned char)CurChar;
71 // A nul character in the stream is either the end of the current buffer or
72 // a random nul in the file. Disambiguate that here.
73 if (CurPtr-1 != CurBuf->getBufferEnd())
74 return 0; // Just whitespace.
76 // If this is the end of an included file, pop the parent file off the
78 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
79 if (ParentIncludeLoc != SMLoc()) {
80 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
81 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
82 CurPtr = ParentIncludeLoc.getPointer();
84 // Reset the token start pointer to the start of the new file.
90 // Otherwise, return end of file.
91 --CurPtr; // Another call to lex will return EOF again.
97 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
98 AsmToken AsmLexer::LexIdentifier() {
99 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
100 *CurPtr == '.' || *CurPtr == '@')
102 return AsmToken(AsmToken::Identifier, StringRef(TokStart, CurPtr - TokStart));
105 /// LexPercent: Register: %[a-zA-Z0-9]+
106 AsmToken AsmLexer::LexPercent() {
107 if (!isalnum(*CurPtr))
108 return AsmToken(AsmToken::Percent, StringRef(CurPtr, 1)); // Single %.
110 while (isalnum(*CurPtr))
113 return AsmToken(AsmToken::Register, StringRef(TokStart, CurPtr - TokStart));
116 /// LexSlash: Slash: /
117 /// C-Style Comment: /* ... */
118 AsmToken AsmLexer::LexSlash() {
120 case '*': break; // C style comment.
121 case '/': return ++CurPtr, LexLineComment();
122 default: return AsmToken(AsmToken::Slash, StringRef(CurPtr, 1));
126 ++CurPtr; // skip the star.
128 int CurChar = getNextChar();
131 return ReturnError(TokStart, "unterminated comment");
133 // End of the comment?
134 if (CurPtr[0] != '/') break;
136 ++CurPtr; // End the */.
142 /// LexLineComment: Comment: #[^\n]*
144 AsmToken AsmLexer::LexLineComment() {
145 // FIXME: This is broken if we happen to a comment at the end of a file, which
146 // was .included, and which doesn't end with a newline.
147 int CurChar = getNextChar();
148 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
149 CurChar = getNextChar();
152 return AsmToken(AsmToken::Eof, StringRef(CurPtr, 0));
153 return AsmToken(AsmToken::EndOfStatement, StringRef(CurPtr, 0));
157 /// LexDigit: First character is [0-9].
158 /// Local Label: [0-9][:]
159 /// Forward/Backward Label: [0-9][fb]
160 /// Binary integer: 0b[01]+
161 /// Octal integer: 0[0-7]+
162 /// Hex integer: 0x[0-9a-fA-F]+
163 /// Decimal integer: [1-9][0-9]*
164 /// TODO: FP literal.
165 AsmToken AsmLexer::LexDigit() {
167 return ReturnError(TokStart, "FIXME: local label not implemented");
168 if (*CurPtr == 'f' || *CurPtr == 'b')
169 return ReturnError(TokStart, "FIXME: directional label not implemented");
171 // Decimal integer: [1-9][0-9]*
172 if (CurPtr[-1] != '0') {
173 while (isdigit(*CurPtr))
175 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
176 strtoll(TokStart, 0, 10));
179 if (*CurPtr == 'b') {
181 const char *NumStart = CurPtr;
182 while (CurPtr[0] == '0' || CurPtr[0] == '1')
185 // Requires at least one binary digit.
186 if (CurPtr == NumStart)
187 return ReturnError(CurPtr-2, "Invalid binary number");
188 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
189 strtoll(NumStart, 0, 2));
192 if (*CurPtr == 'x') {
194 const char *NumStart = CurPtr;
195 while (isxdigit(CurPtr[0]))
198 // Requires at least one hex digit.
199 if (CurPtr == NumStart)
200 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
204 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
205 if (errno == ERANGE) {
208 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
210 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
212 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
213 (int64_t) strtoull(NumStart, 0, 16));
216 // Must be an octal number, it starts with 0.
217 while (*CurPtr >= '0' && *CurPtr <= '7')
219 return AsmToken(AsmToken::Integer, StringRef(TokStart, CurPtr - TokStart),
220 strtoll(TokStart, 0, 8));
223 /// LexQuote: String: "..."
224 AsmToken AsmLexer::LexQuote() {
225 int CurChar = getNextChar();
226 // TODO: does gas allow multiline string constants?
227 while (CurChar != '"') {
228 if (CurChar == '\\') {
230 CurChar = getNextChar();
234 return ReturnError(TokStart, "unterminated string constant");
236 CurChar = getNextChar();
239 return AsmToken(AsmToken::String, StringRef(TokStart, CurPtr - TokStart));
243 AsmToken AsmLexer::LexToken() {
245 // This always consumes at least one character.
246 int CurChar = getNextChar();
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::Star, StringRef(TokStart, 1));
272 case ',': return AsmToken(AsmToken::Comma, StringRef(TokStart, 1));
273 case '$': return AsmToken(AsmToken::Dollar, StringRef(TokStart, 1));
276 return ++CurPtr, AsmToken(AsmToken::EqualEqual, StringRef(TokStart, 2));
277 return AsmToken(AsmToken::Equal, StringRef(TokStart, 1));
280 return ++CurPtr, AsmToken(AsmToken::PipePipe, StringRef(TokStart, 2));
281 return AsmToken(AsmToken::Pipe, StringRef(TokStart, 1));
282 case '^': return AsmToken(AsmToken::Caret, StringRef(TokStart, 1));
285 return ++CurPtr, AsmToken(AsmToken::AmpAmp, StringRef(TokStart, 2));
286 return AsmToken(AsmToken::Amp, StringRef(TokStart, 1));
289 return ++CurPtr, AsmToken(AsmToken::ExclaimEqual, StringRef(TokStart, 2));
290 return AsmToken(AsmToken::Exclaim, StringRef(TokStart, 1));
291 case '%': return LexPercent();
292 case '/': return LexSlash();
293 case '#': return LexLineComment();
294 case '"': return LexQuote();
295 case '0': case '1': case '2': case '3': case '4':
296 case '5': case '6': case '7': case '8': case '9':
300 case '<': return ++CurPtr, AsmToken(AsmToken::LessLess,
301 StringRef(TokStart, 2));
302 case '=': return ++CurPtr, AsmToken(AsmToken::LessEqual,
303 StringRef(TokStart, 2));
304 case '>': return ++CurPtr, AsmToken(AsmToken::LessGreater,
305 StringRef(TokStart, 2));
306 default: return AsmToken(AsmToken::Less, StringRef(TokStart, 1));
310 case '>': return ++CurPtr, AsmToken(AsmToken::GreaterGreater,
311 StringRef(TokStart, 2));
312 case '=': return ++CurPtr, AsmToken(AsmToken::GreaterEqual,
313 StringRef(TokStart, 2));
314 default: return AsmToken(AsmToken::Greater, StringRef(TokStart, 1));
317 // TODO: Quoted identifiers (objc methods etc)
318 // local labels: [0-9][:]
319 // Forward/backward labels: [0-9][fb]
320 // Integers, fp constants, character constants.