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"
21 AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
23 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
24 CurPtr = CurBuf->getBufferStart();
28 SMLoc AsmLexer::getLoc() const {
29 return SMLoc::getFromPointer(TokStart);
32 void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
33 SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
36 void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
37 SrcMgr.PrintError(Loc, Msg);
40 /// ReturnError - Set the error to the specified string at the specified
41 /// location. This is defined to always return asmtok::Error.
42 asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
47 int AsmLexer::getNextChar() {
48 char CurChar = *CurPtr++;
51 return (unsigned char)CurChar;
53 // A nul character in the stream is either the end of the current buffer or
54 // a random nul in the file. Disambiguate that here.
55 if (CurPtr-1 != CurBuf->getBufferEnd())
56 return 0; // Just whitespace.
58 // If this is the end of an included file, pop the parent file off the
60 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
61 if (ParentIncludeLoc != SMLoc()) {
62 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
63 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
64 CurPtr = ParentIncludeLoc.getPointer();
68 // Otherwise, return end of file.
69 --CurPtr; // Another call to lex will return EOF again.
75 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
76 asmtok::TokKind AsmLexer::LexIdentifier() {
77 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
78 *CurPtr == '.' || *CurPtr == '@')
80 CurStrVal.assign(TokStart, CurPtr); // Include %
81 return asmtok::Identifier;
84 /// LexPercent: Register: %[a-zA-Z0-9]+
85 asmtok::TokKind AsmLexer::LexPercent() {
86 if (!isalnum(*CurPtr))
87 return ReturnError(TokStart, "invalid register name");
88 while (isalnum(*CurPtr))
90 CurStrVal.assign(TokStart, CurPtr); // Skip %
91 return asmtok::Register;
94 /// LexSlash: Slash: /
95 /// C-Style Comment: /* ... */
96 asmtok::TokKind AsmLexer::LexSlash() {
101 ++CurPtr; // skip the star.
103 int CurChar = getNextChar();
106 return ReturnError(TokStart, "unterminated comment");
108 // End of the comment?
109 if (CurPtr[0] != '/') break;
111 ++CurPtr; // End the */.
117 /// LexHash: Comment: #[^\n]*
118 asmtok::TokKind AsmLexer::LexHash() {
119 int CurChar = getNextChar();
120 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
121 CurChar = getNextChar();
125 return asmtok::EndOfStatement;
129 /// LexDigit: First character is [0-9].
130 /// Local Label: [0-9][:]
131 /// Forward/Backward Label: [0-9][fb]
132 /// Binary integer: 0b[01]+
133 /// Octal integer: 0[0-7]+
134 /// Hex integer: 0x[0-9a-fA-F]+
135 /// Decimal integer: [1-9][0-9]*
136 /// TODO: FP literal.
137 asmtok::TokKind AsmLexer::LexDigit() {
139 return ReturnError(TokStart, "FIXME: local label not implemented");
140 if (*CurPtr == 'f' || *CurPtr == 'b')
141 return ReturnError(TokStart, "FIXME: directional label not implemented");
143 // Decimal integer: [1-9][0-9]*
144 if (CurPtr[-1] != '0') {
145 while (isdigit(*CurPtr))
147 CurIntVal = strtoll(TokStart, 0, 10);
148 return asmtok::IntVal;
151 if (*CurPtr == 'b') {
153 const char *NumStart = CurPtr;
154 while (CurPtr[0] == '0' || CurPtr[0] == '1')
157 // Requires at least one binary digit.
158 if (CurPtr == NumStart)
159 return ReturnError(CurPtr-2, "Invalid binary number");
160 CurIntVal = strtoll(NumStart, 0, 2);
161 return asmtok::IntVal;
164 if (*CurPtr == 'x') {
166 const char *NumStart = CurPtr;
167 while (isxdigit(CurPtr[0]))
170 // Requires at least one hex digit.
171 if (CurPtr == NumStart)
172 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
175 CurIntVal = strtoll(NumStart, 0, 16);
177 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
178 if (errno == ERANGE) {
180 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
182 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
184 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
186 return asmtok::IntVal;
189 // Must be an octal number, it starts with 0.
190 while (*CurPtr >= '0' && *CurPtr <= '7')
192 CurIntVal = strtoll(TokStart, 0, 8);
193 return asmtok::IntVal;
196 /// LexQuote: String: "..."
197 asmtok::TokKind AsmLexer::LexQuote() {
198 int CurChar = getNextChar();
199 // TODO: does gas allow multiline string constants?
200 while (CurChar != '"') {
201 if (CurChar == '\\') {
203 CurChar = getNextChar();
206 if (CurChar == EOF) {
207 PrintError(TokStart, "unterminated string constant");
211 CurChar = getNextChar();
214 CurStrVal.assign(TokStart, CurPtr); // include quotes.
215 return asmtok::String;
219 asmtok::TokKind AsmLexer::LexToken() {
221 // This always consumes at least one character.
222 int CurChar = getNextChar();
226 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
227 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
228 return LexIdentifier();
230 // Unknown character, emit an error.
231 return ReturnError(TokStart, "invalid character in input");
232 case EOF: return asmtok::Eof;
236 // Ignore whitespace.
238 case '\n': // FALL THROUGH.
239 case '\r': // FALL THROUGH.
240 case ';': return asmtok::EndOfStatement;
241 case ':': return asmtok::Colon;
242 case '+': return asmtok::Plus;
243 case '-': return asmtok::Minus;
244 case '(': return asmtok::LParen;
245 case ')': return asmtok::RParen;
246 case '*': return asmtok::Star;
247 case ',': return asmtok::Comma;
248 case '$': return asmtok::Dollar;
249 case '%': return LexPercent();
250 case '/': return LexSlash();
251 case '#': return LexHash();
252 case '"': return LexQuote();
253 case '0': case '1': case '2': case '3': case '4':
254 case '5': case '6': case '7': case '8': case '9':
257 // TODO: Quoted identifiers (objc methods etc)
258 // local labels: [0-9][:]
259 // Forward/backward labels: [0-9][fb]
260 // Integers, fp constants, character constants.