blob: 9e777b6e6e21b1d332febee4163a2f8c95395393 [file] [log] [blame]
Chris Lattnera59e8772009-06-21 07:19:10 +00001//===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the lexer for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AsmLexer.h"
15#include "llvm/Support/SourceMgr.h"
16#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerbcd0b8d2009-06-23 00:24:36 +000017#include "llvm/Config/config.h" // for strtoull.
Chris Lattner4651bca2009-06-21 19:21:25 +000018#include <cerrno>
Duncan Sands66b9f292009-06-22 06:59:32 +000019#include <cstdio>
Chris Lattner4506bd22009-06-21 19:43:50 +000020#include <cstdlib>
Chris Lattnera59e8772009-06-21 07:19:10 +000021using namespace llvm;
22
Daniel Dunbarcb358b62009-07-28 03:00:54 +000023AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
Chris Lattnera59e8772009-06-21 07:19:10 +000024 CurBuffer = 0;
25 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
26 CurPtr = CurBuf->getBufferStart();
Daniel Dunbarcb358b62009-07-28 03:00:54 +000027 CurTok = AsmToken(asmtok::Error, StringRef(CurPtr, 0));
Chris Lattnera59e8772009-06-21 07:19:10 +000028 TokStart = 0;
Chris Lattnerfaf32c12009-06-24 00:33:19 +000029}
30
31AsmLexer::~AsmLexer() {
Chris Lattnera59e8772009-06-21 07:19:10 +000032}
33
Chris Lattner4651bca2009-06-21 19:21:25 +000034SMLoc AsmLexer::getLoc() const {
35 return SMLoc::getFromPointer(TokStart);
36}
37
Daniel Dunbar3fb76832009-06-30 00:49:23 +000038void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg,
39 const char *Type) const {
40 SrcMgr.PrintMessage(Loc, Msg, Type);
Chris Lattnera59e8772009-06-21 07:19:10 +000041}
42
Chris Lattner4651bca2009-06-21 19:21:25 +000043/// ReturnError - Set the error to the specified string at the specified
44/// location. This is defined to always return asmtok::Error.
Daniel Dunbarcb358b62009-07-28 03:00:54 +000045AsmToken AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000046 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
Daniel Dunbarcb358b62009-07-28 03:00:54 +000047 return AsmToken(asmtok::Error, StringRef(Loc, 0));
Chris Lattner4651bca2009-06-21 19:21:25 +000048}
49
Chris Lattner8e25e2d2009-07-16 06:14:39 +000050/// EnterIncludeFile - Enter the specified file. This prints an error and
51/// returns true on failure.
52bool AsmLexer::EnterIncludeFile(const std::string &Filename) {
53 int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
54 if (NewBuf == -1)
55 return true;
56
57 // Save the line number and lex buffer of the includer.
58 CurBuffer = NewBuf;
59 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
60 CurPtr = CurBuf->getBufferStart();
61 return false;
62}
63
64
Chris Lattnera59e8772009-06-21 07:19:10 +000065int AsmLexer::getNextChar() {
66 char CurChar = *CurPtr++;
67 switch (CurChar) {
68 default:
69 return (unsigned char)CurChar;
70 case 0: {
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.
75
76 // If this is the end of an included file, pop the parent file off the
77 // include stack.
78 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
79 if (ParentIncludeLoc != SMLoc()) {
80 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
81 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
82 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattner8e25e2d2009-07-16 06:14:39 +000083
84 // Reset the token start pointer to the start of the new file.
85 TokStart = CurPtr;
86
Chris Lattnera59e8772009-06-21 07:19:10 +000087 return getNextChar();
88 }
89
90 // Otherwise, return end of file.
91 --CurPtr; // Another call to lex will return EOF again.
92 return EOF;
93 }
94 }
95}
96
Chris Lattner4651bca2009-06-21 19:21:25 +000097/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
Daniel Dunbarcb358b62009-07-28 03:00:54 +000098AsmToken AsmLexer::LexIdentifier() {
Chris Lattner4651bca2009-06-21 19:21:25 +000099 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
100 *CurPtr == '.' || *CurPtr == '@')
101 ++CurPtr;
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000102 return AsmToken(asmtok::Identifier, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner4651bca2009-06-21 19:21:25 +0000103}
104
105/// LexPercent: Register: %[a-zA-Z0-9]+
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000106AsmToken AsmLexer::LexPercent() {
Chris Lattner4651bca2009-06-21 19:21:25 +0000107 if (!isalnum(*CurPtr))
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000108 return AsmToken(asmtok::Percent, StringRef(CurPtr, 1)); // Single %.
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000109
Chris Lattner4651bca2009-06-21 19:21:25 +0000110 while (isalnum(*CurPtr))
111 ++CurPtr;
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000112
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000113 return AsmToken(asmtok::Register, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner4651bca2009-06-21 19:21:25 +0000114}
115
116/// LexSlash: Slash: /
117/// C-Style Comment: /* ... */
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000118AsmToken AsmLexer::LexSlash() {
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000119 switch (*CurPtr) {
120 case '*': break; // C style comment.
121 case '/': return ++CurPtr, LexLineComment();
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000122 default: return AsmToken(asmtok::Slash, StringRef(CurPtr, 1));
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000123 }
Chris Lattner4651bca2009-06-21 19:21:25 +0000124
125 // C Style comment.
126 ++CurPtr; // skip the star.
127 while (1) {
128 int CurChar = getNextChar();
129 switch (CurChar) {
130 case EOF:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000131 return ReturnError(TokStart, "unterminated comment");
Chris Lattner4651bca2009-06-21 19:21:25 +0000132 case '*':
133 // End of the comment?
134 if (CurPtr[0] != '/') break;
135
136 ++CurPtr; // End the */.
137 return LexToken();
138 }
139 }
140}
141
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000142/// LexLineComment: Comment: #[^\n]*
143/// : //[^\n]*
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000144AsmToken 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.
Chris Lattner4651bca2009-06-21 19:21:25 +0000147 int CurChar = getNextChar();
148 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
149 CurChar = getNextChar();
150
151 if (CurChar == EOF)
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000152 return AsmToken(asmtok::Eof, StringRef(CurPtr, 0));
153 return AsmToken(asmtok::EndOfStatement, StringRef(CurPtr, 0));
Chris Lattner4651bca2009-06-21 19:21:25 +0000154}
155
156
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.
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000165AsmToken AsmLexer::LexDigit() {
Chris Lattner4651bca2009-06-21 19:21:25 +0000166 if (*CurPtr == ':')
Chris Lattner27aa7d22009-06-21 20:16:42 +0000167 return ReturnError(TokStart, "FIXME: local label not implemented");
Chris Lattner4651bca2009-06-21 19:21:25 +0000168 if (*CurPtr == 'f' || *CurPtr == 'b')
Chris Lattner27aa7d22009-06-21 20:16:42 +0000169 return ReturnError(TokStart, "FIXME: directional label not implemented");
Chris Lattner4651bca2009-06-21 19:21:25 +0000170
171 // Decimal integer: [1-9][0-9]*
172 if (CurPtr[-1] != '0') {
173 while (isdigit(*CurPtr))
174 ++CurPtr;
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000175 return AsmToken(asmtok::IntVal, StringRef(TokStart, CurPtr - TokStart),
176 strtoll(TokStart, 0, 10));
Chris Lattner4651bca2009-06-21 19:21:25 +0000177 }
178
179 if (*CurPtr == 'b') {
180 ++CurPtr;
181 const char *NumStart = CurPtr;
182 while (CurPtr[0] == '0' || CurPtr[0] == '1')
183 ++CurPtr;
184
185 // Requires at least one binary digit.
186 if (CurPtr == NumStart)
187 return ReturnError(CurPtr-2, "Invalid binary number");
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000188 return AsmToken(asmtok::IntVal, StringRef(TokStart, CurPtr - TokStart),
189 strtoll(NumStart, 0, 2));
Chris Lattner4651bca2009-06-21 19:21:25 +0000190 }
191
192 if (*CurPtr == 'x') {
193 ++CurPtr;
194 const char *NumStart = CurPtr;
195 while (isxdigit(CurPtr[0]))
196 ++CurPtr;
197
198 // Requires at least one hex digit.
199 if (CurPtr == NumStart)
200 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
201
202 errno = 0;
Chris Lattner4651bca2009-06-21 19:21:25 +0000203 if (errno == EINVAL)
204 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
205 if (errno == ERANGE) {
206 errno = 0;
Chris Lattner4651bca2009-06-21 19:21:25 +0000207 if (errno == EINVAL)
208 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
209 if (errno == ERANGE)
210 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
211 }
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000212 return AsmToken(asmtok::IntVal, StringRef(TokStart, CurPtr - TokStart),
213 (int64_t) strtoull(NumStart, 0, 16));
Chris Lattner4651bca2009-06-21 19:21:25 +0000214 }
215
216 // Must be an octal number, it starts with 0.
217 while (*CurPtr >= '0' && *CurPtr <= '7')
218 ++CurPtr;
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000219 return AsmToken(asmtok::IntVal, StringRef(TokStart, CurPtr - TokStart),
220 strtoll(TokStart, 0, 8));
Chris Lattner4651bca2009-06-21 19:21:25 +0000221}
222
Chris Lattner10a907d2009-06-21 19:56:35 +0000223/// LexQuote: String: "..."
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000224AsmToken AsmLexer::LexQuote() {
Chris Lattner10a907d2009-06-21 19:56:35 +0000225 int CurChar = getNextChar();
226 // TODO: does gas allow multiline string constants?
227 while (CurChar != '"') {
228 if (CurChar == '\\') {
229 // Allow \", etc.
230 CurChar = getNextChar();
231 }
232
Chris Lattner14ee48a2009-06-21 21:22:11 +0000233 if (CurChar == EOF)
234 return ReturnError(TokStart, "unterminated string constant");
Chris Lattner10a907d2009-06-21 19:56:35 +0000235
236 CurChar = getNextChar();
237 }
238
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000239 return AsmToken(asmtok::String, StringRef(TokStart, CurPtr - TokStart));
Chris Lattner10a907d2009-06-21 19:56:35 +0000240}
241
Chris Lattner4651bca2009-06-21 19:21:25 +0000242
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000243AsmToken AsmLexer::LexToken() {
Chris Lattnera59e8772009-06-21 07:19:10 +0000244 TokStart = CurPtr;
245 // This always consumes at least one character.
246 int CurChar = getNextChar();
247
248 switch (CurChar) {
249 default:
Chris Lattner4651bca2009-06-21 19:21:25 +0000250 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
251 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
252 return LexIdentifier();
Chris Lattnera59e8772009-06-21 07:19:10 +0000253
254 // Unknown character, emit an error.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000255 return ReturnError(TokStart, "invalid character in input");
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000256 case EOF: return AsmToken(asmtok::Eof, StringRef(TokStart, 0));
Chris Lattnera59e8772009-06-21 07:19:10 +0000257 case 0:
258 case ' ':
259 case '\t':
Chris Lattnera59e8772009-06-21 07:19:10 +0000260 // Ignore whitespace.
261 return LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +0000262 case '\n': // FALL THROUGH.
263 case '\r': // FALL THROUGH.
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000264 case ';': return AsmToken(asmtok::EndOfStatement, StringRef(TokStart, 1));
265 case ':': return AsmToken(asmtok::Colon, StringRef(TokStart, 1));
266 case '+': return AsmToken(asmtok::Plus, StringRef(TokStart, 1));
267 case '-': return AsmToken(asmtok::Minus, StringRef(TokStart, 1));
268 case '~': return AsmToken(asmtok::Tilde, StringRef(TokStart, 1));
269 case '(': return AsmToken(asmtok::LParen, StringRef(TokStart, 1));
270 case ')': return AsmToken(asmtok::RParen, StringRef(TokStart, 1));
271 case '*': return AsmToken(asmtok::Star, StringRef(TokStart, 1));
272 case ',': return AsmToken(asmtok::Comma, StringRef(TokStart, 1));
273 case '$': return AsmToken(asmtok::Dollar, StringRef(TokStart, 1));
Daniel Dunbar475839e2009-06-29 20:37:27 +0000274 case '=':
275 if (*CurPtr == '=')
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000276 return ++CurPtr, AsmToken(asmtok::EqualEqual, StringRef(TokStart, 2));
277 return AsmToken(asmtok::Equal, StringRef(TokStart, 1));
Daniel Dunbar475839e2009-06-29 20:37:27 +0000278 case '|':
279 if (*CurPtr == '|')
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000280 return ++CurPtr, AsmToken(asmtok::PipePipe, StringRef(TokStart, 2));
281 return AsmToken(asmtok::Pipe, StringRef(TokStart, 1));
282 case '^': return AsmToken(asmtok::Caret, StringRef(TokStart, 1));
Daniel Dunbar475839e2009-06-29 20:37:27 +0000283 case '&':
284 if (*CurPtr == '&')
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000285 return ++CurPtr, AsmToken(asmtok::AmpAmp, StringRef(TokStart, 2));
286 return AsmToken(asmtok::Amp, StringRef(TokStart, 1));
Daniel Dunbar475839e2009-06-29 20:37:27 +0000287 case '!':
288 if (*CurPtr == '=')
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000289 return ++CurPtr, AsmToken(asmtok::ExclaimEqual, StringRef(TokStart, 2));
290 return AsmToken(asmtok::Exclaim, StringRef(TokStart, 1));
Chris Lattner4651bca2009-06-21 19:21:25 +0000291 case '%': return LexPercent();
292 case '/': return LexSlash();
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000293 case '#': return LexLineComment();
Chris Lattner10a907d2009-06-21 19:56:35 +0000294 case '"': return LexQuote();
Chris Lattner4651bca2009-06-21 19:21:25 +0000295 case '0': case '1': case '2': case '3': case '4':
296 case '5': case '6': case '7': case '8': case '9':
297 return LexDigit();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000298 case '<':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000299 switch (*CurPtr) {
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000300 case '<': return ++CurPtr, AsmToken(asmtok::LessLess,
301 StringRef(TokStart, 2));
302 case '=': return ++CurPtr, AsmToken(asmtok::LessEqual,
303 StringRef(TokStart, 2));
304 case '>': return ++CurPtr, AsmToken(asmtok::LessGreater,
305 StringRef(TokStart, 2));
306 default: return AsmToken(asmtok::Less, StringRef(TokStart, 1));
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000307 }
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000308 case '>':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000309 switch (*CurPtr) {
Daniel Dunbarcb358b62009-07-28 03:00:54 +0000310 case '>': return ++CurPtr, AsmToken(asmtok::GreaterGreater,
311 StringRef(TokStart, 2));
312 case '=': return ++CurPtr, AsmToken(asmtok::GreaterEqual,
313 StringRef(TokStart, 2));
314 default: return AsmToken(asmtok::Greater, StringRef(TokStart, 1));
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000315 }
Chris Lattner4651bca2009-06-21 19:21:25 +0000316
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.
Chris Lattnera59e8772009-06-21 07:19:10 +0000321 }
Duncan Sands66b9f292009-06-22 06:59:32 +0000322}