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();
30 AsmLexer::~AsmLexer() {
33 SMLoc AsmLexer::getLoc() const {
34 return SMLoc::getFromPointer(TokStart);
37 void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg,
38 const char *Type) const {
39 SrcMgr.PrintMessage(Loc, Msg, Type);
42 /// ReturnError - Set the error to the specified string at the specified
43 /// location. This is defined to always return asmtok::Error.
44 asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
45 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg, "error");
49 /// EnterIncludeFile - Enter the specified file. This prints an error and
50 /// returns true on failure.
51 bool AsmLexer::EnterIncludeFile(const std::string &Filename) {
52 int NewBuf = SrcMgr.AddIncludeFile(Filename, SMLoc::getFromPointer(CurPtr));
56 // Save the line number and lex buffer of the includer.
58 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
59 CurPtr = CurBuf->getBufferStart();
64 int AsmLexer::getNextChar() {
65 char CurChar = *CurPtr++;
68 return (unsigned char)CurChar;
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.
75 // If this is the end of an included file, pop the parent file off the
77 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
78 if (ParentIncludeLoc != SMLoc()) {
79 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
80 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
81 CurPtr = ParentIncludeLoc.getPointer();
83 // Reset the token start pointer to the start of the new file.
89 // Otherwise, return end of file.
90 --CurPtr; // Another call to lex will return EOF again.
96 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
97 asmtok::TokKind AsmLexer::LexIdentifier() {
98 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
99 *CurPtr == '.' || *CurPtr == '@')
102 CurStrVal = StringRef(TokStart, CurPtr - TokStart);
103 return asmtok::Identifier;
106 /// LexPercent: Register: %[a-zA-Z0-9]+
107 asmtok::TokKind AsmLexer::LexPercent() {
108 if (!isalnum(*CurPtr))
109 return asmtok::Percent; // Single %.
111 while (isalnum(*CurPtr))
115 CurStrVal = StringRef(TokStart, CurPtr - TokStart);
116 return asmtok::Register;
119 /// LexSlash: Slash: /
120 /// C-Style Comment: /* ... */
121 asmtok::TokKind AsmLexer::LexSlash() {
123 case '*': break; // C style comment.
124 case '/': return ++CurPtr, LexLineComment();
125 default: return asmtok::Slash;
129 ++CurPtr; // skip the star.
131 int CurChar = getNextChar();
134 return ReturnError(TokStart, "unterminated comment");
136 // End of the comment?
137 if (CurPtr[0] != '/') break;
139 ++CurPtr; // End the */.
145 /// LexLineComment: Comment: #[^\n]*
147 asmtok::TokKind AsmLexer::LexLineComment() {
148 int CurChar = getNextChar();
149 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
150 CurChar = getNextChar();
154 return asmtok::EndOfStatement;
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.
166 asmtok::TokKind AsmLexer::LexDigit() {
168 return ReturnError(TokStart, "FIXME: local label not implemented");
169 if (*CurPtr == 'f' || *CurPtr == 'b')
170 return ReturnError(TokStart, "FIXME: directional label not implemented");
172 // Decimal integer: [1-9][0-9]*
173 if (CurPtr[-1] != '0') {
174 while (isdigit(*CurPtr))
176 CurIntVal = strtoll(TokStart, 0, 10);
177 return asmtok::IntVal;
180 if (*CurPtr == 'b') {
182 const char *NumStart = CurPtr;
183 while (CurPtr[0] == '0' || CurPtr[0] == '1')
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;
193 if (*CurPtr == 'x') {
195 const char *NumStart = CurPtr;
196 while (isxdigit(CurPtr[0]))
199 // Requires at least one hex digit.
200 if (CurPtr == NumStart)
201 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
204 CurIntVal = strtoll(NumStart, 0, 16);
206 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
207 if (errno == ERANGE) {
209 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
211 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
213 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
215 return asmtok::IntVal;
218 // Must be an octal number, it starts with 0.
219 while (*CurPtr >= '0' && *CurPtr <= '7')
221 CurIntVal = strtoll(TokStart, 0, 8);
222 return asmtok::IntVal;
225 /// LexQuote: String: "..."
226 asmtok::TokKind AsmLexer::LexQuote() {
227 int CurChar = getNextChar();
228 // TODO: does gas allow multiline string constants?
229 while (CurChar != '"') {
230 if (CurChar == '\\') {
232 CurChar = getNextChar();
236 return ReturnError(TokStart, "unterminated string constant");
238 CurChar = getNextChar();
241 // Unique string, include quotes for now.
242 CurStrVal = StringRef(TokStart, CurPtr - TokStart);
243 return asmtok::String;
247 asmtok::TokKind AsmLexer::LexToken() {
249 // This always consumes at least one character.
250 int CurChar = getNextChar();
254 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
255 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
256 return LexIdentifier();
258 // Unknown character, emit an error.
259 return ReturnError(TokStart, "invalid character in input");
260 case EOF: return asmtok::Eof;
264 // Ignore whitespace.
266 case '\n': // FALL THROUGH.
267 case '\r': // FALL THROUGH.
268 case ';': return asmtok::EndOfStatement;
269 case ':': return asmtok::Colon;
270 case '+': return asmtok::Plus;
271 case '-': return asmtok::Minus;
272 case '~': return asmtok::Tilde;
273 case '(': return asmtok::LParen;
274 case ')': return asmtok::RParen;
275 case '*': return asmtok::Star;
276 case ',': return asmtok::Comma;
277 case '$': return asmtok::Dollar;
280 return ++CurPtr, asmtok::EqualEqual;
281 return asmtok::Equal;
284 return ++CurPtr, asmtok::PipePipe;
286 case '^': return asmtok::Caret;
289 return ++CurPtr, asmtok::AmpAmp;
293 return ++CurPtr, asmtok::ExclaimEqual;
294 return asmtok::Exclaim;
295 case '%': return LexPercent();
296 case '/': return LexSlash();
297 case '#': return LexLineComment();
298 case '"': return LexQuote();
299 case '0': case '1': case '2': case '3': case '4':
300 case '5': case '6': case '7': case '8': case '9':
304 case '<': return ++CurPtr, asmtok::LessLess;
305 case '=': return ++CurPtr, asmtok::LessEqual;
306 case '>': return ++CurPtr, asmtok::LessGreater;
307 default: return asmtok::Less;
311 case '>': return ++CurPtr, asmtok::GreaterGreater;
312 case '=': return ++CurPtr, asmtok::GreaterEqual;
313 default: return asmtok::Greater;
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.