blob: 0550bb1883e540760ea42d69fd4def28506f1d3b [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
23AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
24 CurBuffer = 0;
25 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
26 CurPtr = CurBuf->getBufferStart();
27 TokStart = 0;
Chris Lattnerfaf32c12009-06-24 00:33:19 +000028}
29
30AsmLexer::~AsmLexer() {
Chris Lattnera59e8772009-06-21 07:19:10 +000031}
32
Chris Lattner4651bca2009-06-21 19:21:25 +000033SMLoc AsmLexer::getLoc() const {
34 return SMLoc::getFromPointer(TokStart);
35}
36
Daniel Dunbar3fb76832009-06-30 00:49:23 +000037void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg,
38 const char *Type) const {
39 SrcMgr.PrintMessage(Loc, Msg, Type);
Chris Lattnera59e8772009-06-21 07:19:10 +000040}
41
Chris Lattner4651bca2009-06-21 19:21:25 +000042/// ReturnError - Set the error to the specified string at the specified
43/// location. This is defined to always return asmtok::Error.
44asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
Daniel Dunbar3fb76832009-06-30 00:49:23 +000045 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
Chris Lattner4651bca2009-06-21 19:21:25 +000046 return asmtok::Error;
47}
48
Chris Lattner8e25e2d2009-07-16 06:14:39 +000049/// EnterIncludeFile - Enter the specified file. This prints an error and
50/// returns true on failure.
51bool AsmLexer::EnterIncludeFile(const std::string &Filename) {
52 int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
53 if (NewBuf == -1)
54 return true;
55
56 // Save the line number and lex buffer of the includer.
57 CurBuffer = NewBuf;
58 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
59 CurPtr = CurBuf->getBufferStart();
60 return false;
61}
62
63
Chris Lattnera59e8772009-06-21 07:19:10 +000064int AsmLexer::getNextChar() {
65 char CurChar = *CurPtr++;
66 switch (CurChar) {
67 default:
68 return (unsigned char)CurChar;
69 case 0: {
70 // A nul character in the stream is either the end of the current buffer or
71 // a random nul in the file. Disambiguate that here.
72 if (CurPtr-1 != CurBuf->getBufferEnd())
73 return 0; // Just whitespace.
74
75 // If this is the end of an included file, pop the parent file off the
76 // include stack.
77 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
78 if (ParentIncludeLoc != SMLoc()) {
79 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
80 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
81 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattner8e25e2d2009-07-16 06:14:39 +000082
83 // Reset the token start pointer to the start of the new file.
84 TokStart = CurPtr;
85
Chris Lattnera59e8772009-06-21 07:19:10 +000086 return getNextChar();
87 }
88
89 // Otherwise, return end of file.
90 --CurPtr; // Another call to lex will return EOF again.
91 return EOF;
92 }
93 }
94}
95
Chris Lattner4651bca2009-06-21 19:21:25 +000096/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
97asmtok::TokKind AsmLexer::LexIdentifier() {
98 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
99 *CurPtr == '.' || *CurPtr == '@')
100 ++CurPtr;
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000101 // Unique string.
Daniel Dunbar825e3852009-07-28 00:58:50 +0000102 CurStrVal = StringRef(TokStart, CurPtr - TokStart);
Chris Lattner4651bca2009-06-21 19:21:25 +0000103 return asmtok::Identifier;
104}
105
106/// LexPercent: Register: %[a-zA-Z0-9]+
107asmtok::TokKind AsmLexer::LexPercent() {
108 if (!isalnum(*CurPtr))
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000109 return asmtok::Percent; // Single %.
110
Chris Lattner4651bca2009-06-21 19:21:25 +0000111 while (isalnum(*CurPtr))
112 ++CurPtr;
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000113
114 // Unique string.
Daniel Dunbar825e3852009-07-28 00:58:50 +0000115 CurStrVal = StringRef(TokStart, CurPtr - TokStart);
Chris Lattner4651bca2009-06-21 19:21:25 +0000116 return asmtok::Register;
117}
118
119/// LexSlash: Slash: /
120/// C-Style Comment: /* ... */
121asmtok::TokKind AsmLexer::LexSlash() {
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000122 switch (*CurPtr) {
123 case '*': break; // C style comment.
124 case '/': return ++CurPtr, LexLineComment();
125 default: return asmtok::Slash;
126 }
Chris Lattner4651bca2009-06-21 19:21:25 +0000127
128 // C Style comment.
129 ++CurPtr; // skip the star.
130 while (1) {
131 int CurChar = getNextChar();
132 switch (CurChar) {
133 case EOF:
Chris Lattner27aa7d22009-06-21 20:16:42 +0000134 return ReturnError(TokStart, "unterminated comment");
Chris Lattner4651bca2009-06-21 19:21:25 +0000135 case '*':
136 // End of the comment?
137 if (CurPtr[0] != '/') break;
138
139 ++CurPtr; // End the */.
140 return LexToken();
141 }
142 }
143}
144
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000145/// LexLineComment: Comment: #[^\n]*
146/// : //[^\n]*
147asmtok::TokKind AsmLexer::LexLineComment() {
Chris Lattner4651bca2009-06-21 19:21:25 +0000148 int CurChar = getNextChar();
149 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
150 CurChar = getNextChar();
151
152 if (CurChar == EOF)
153 return asmtok::Eof;
154 return asmtok::EndOfStatement;
155}
156
157
158/// LexDigit: First character is [0-9].
159/// Local Label: [0-9][:]
160/// Forward/Backward Label: [0-9][fb]
161/// Binary integer: 0b[01]+
162/// Octal integer: 0[0-7]+
163/// Hex integer: 0x[0-9a-fA-F]+
164/// Decimal integer: [1-9][0-9]*
165/// TODO: FP literal.
166asmtok::TokKind AsmLexer::LexDigit() {
167 if (*CurPtr == ':')
Chris Lattner27aa7d22009-06-21 20:16:42 +0000168 return ReturnError(TokStart, "FIXME: local label not implemented");
Chris Lattner4651bca2009-06-21 19:21:25 +0000169 if (*CurPtr == 'f' || *CurPtr == 'b')
Chris Lattner27aa7d22009-06-21 20:16:42 +0000170 return ReturnError(TokStart, "FIXME: directional label not implemented");
Chris Lattner4651bca2009-06-21 19:21:25 +0000171
172 // Decimal integer: [1-9][0-9]*
173 if (CurPtr[-1] != '0') {
174 while (isdigit(*CurPtr))
175 ++CurPtr;
176 CurIntVal = strtoll(TokStart, 0, 10);
177 return asmtok::IntVal;
178 }
179
180 if (*CurPtr == 'b') {
181 ++CurPtr;
182 const char *NumStart = CurPtr;
183 while (CurPtr[0] == '0' || CurPtr[0] == '1')
184 ++CurPtr;
185
186 // Requires at least one binary digit.
187 if (CurPtr == NumStart)
188 return ReturnError(CurPtr-2, "Invalid binary number");
189 CurIntVal = strtoll(NumStart, 0, 2);
190 return asmtok::IntVal;
191 }
192
193 if (*CurPtr == 'x') {
194 ++CurPtr;
195 const char *NumStart = CurPtr;
196 while (isxdigit(CurPtr[0]))
197 ++CurPtr;
198
199 // Requires at least one hex digit.
200 if (CurPtr == NumStart)
201 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
202
203 errno = 0;
204 CurIntVal = strtoll(NumStart, 0, 16);
205 if (errno == EINVAL)
206 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
207 if (errno == ERANGE) {
208 errno = 0;
209 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
210 if (errno == EINVAL)
211 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
212 if (errno == ERANGE)
213 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
214 }
215 return asmtok::IntVal;
216 }
217
218 // Must be an octal number, it starts with 0.
219 while (*CurPtr >= '0' && *CurPtr <= '7')
220 ++CurPtr;
221 CurIntVal = strtoll(TokStart, 0, 8);
222 return asmtok::IntVal;
223}
224
Chris Lattner10a907d2009-06-21 19:56:35 +0000225/// LexQuote: String: "..."
226asmtok::TokKind AsmLexer::LexQuote() {
227 int CurChar = getNextChar();
228 // TODO: does gas allow multiline string constants?
229 while (CurChar != '"') {
230 if (CurChar == '\\') {
231 // Allow \", etc.
232 CurChar = getNextChar();
233 }
234
Chris Lattner14ee48a2009-06-21 21:22:11 +0000235 if (CurChar == EOF)
236 return ReturnError(TokStart, "unterminated string constant");
Chris Lattner10a907d2009-06-21 19:56:35 +0000237
238 CurChar = getNextChar();
239 }
240
Chris Lattnerfaf32c12009-06-24 00:33:19 +0000241 // Unique string, include quotes for now.
Daniel Dunbar825e3852009-07-28 00:58:50 +0000242 CurStrVal = StringRef(TokStart, CurPtr - TokStart);
Chris Lattner10a907d2009-06-21 19:56:35 +0000243 return asmtok::String;
244}
245
Chris Lattner4651bca2009-06-21 19:21:25 +0000246
Chris Lattnera59e8772009-06-21 07:19:10 +0000247asmtok::TokKind AsmLexer::LexToken() {
248 TokStart = CurPtr;
249 // This always consumes at least one character.
250 int CurChar = getNextChar();
251
252 switch (CurChar) {
253 default:
Chris Lattner4651bca2009-06-21 19:21:25 +0000254 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
255 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
256 return LexIdentifier();
Chris Lattnera59e8772009-06-21 07:19:10 +0000257
258 // Unknown character, emit an error.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000259 return ReturnError(TokStart, "invalid character in input");
Chris Lattnera59e8772009-06-21 07:19:10 +0000260 case EOF: return asmtok::Eof;
261 case 0:
262 case ' ':
263 case '\t':
Chris Lattnera59e8772009-06-21 07:19:10 +0000264 // Ignore whitespace.
265 return LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +0000266 case '\n': // FALL THROUGH.
267 case '\r': // FALL THROUGH.
268 case ';': return asmtok::EndOfStatement;
Chris Lattnera59e8772009-06-21 07:19:10 +0000269 case ':': return asmtok::Colon;
270 case '+': return asmtok::Plus;
271 case '-': return asmtok::Minus;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000272 case '~': return asmtok::Tilde;
Chris Lattner4651bca2009-06-21 19:21:25 +0000273 case '(': return asmtok::LParen;
274 case ')': return asmtok::RParen;
275 case '*': return asmtok::Star;
276 case ',': return asmtok::Comma;
277 case '$': return asmtok::Dollar;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000278 case '=':
279 if (*CurPtr == '=')
280 return ++CurPtr, asmtok::EqualEqual;
281 return asmtok::Equal;
282 case '|':
283 if (*CurPtr == '|')
284 return ++CurPtr, asmtok::PipePipe;
285 return asmtok::Pipe;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000286 case '^': return asmtok::Caret;
Daniel Dunbar475839e2009-06-29 20:37:27 +0000287 case '&':
288 if (*CurPtr == '&')
289 return ++CurPtr, asmtok::AmpAmp;
290 return asmtok::Amp;
291 case '!':
292 if (*CurPtr == '=')
293 return ++CurPtr, asmtok::ExclaimEqual;
294 return asmtok::Exclaim;
Chris Lattner4651bca2009-06-21 19:21:25 +0000295 case '%': return LexPercent();
296 case '/': return LexSlash();
Daniel Dunbar383a4a82009-06-29 21:58:22 +0000297 case '#': return LexLineComment();
Chris Lattner10a907d2009-06-21 19:56:35 +0000298 case '"': return LexQuote();
Chris Lattner4651bca2009-06-21 19:21:25 +0000299 case '0': case '1': case '2': case '3': case '4':
300 case '5': case '6': case '7': case '8': case '9':
301 return LexDigit();
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000302 case '<':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000303 switch (*CurPtr) {
304 case '<': return ++CurPtr, asmtok::LessLess;
305 case '=': return ++CurPtr, asmtok::LessEqual;
306 case '>': return ++CurPtr, asmtok::LessGreater;
307 default: return asmtok::Less;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000308 }
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000309 case '>':
Daniel Dunbar475839e2009-06-29 20:37:27 +0000310 switch (*CurPtr) {
311 case '>': return ++CurPtr, asmtok::GreaterGreater;
312 case '=': return ++CurPtr, asmtok::GreaterEqual;
313 default: return asmtok::Greater;
Chris Lattner8dfbe6c2009-06-23 05:57:07 +0000314 }
Chris Lattner4651bca2009-06-21 19:21:25 +0000315
316 // TODO: Quoted identifiers (objc methods etc)
317 // local labels: [0-9][:]
318 // Forward/backward labels: [0-9][fb]
319 // Integers, fp constants, character constants.
Chris Lattnera59e8772009-06-21 07:19:10 +0000320 }
Duncan Sands66b9f292009-06-22 06:59:32 +0000321}