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/ADT/StringSet.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Config/config.h" // for strtoull.
24 static StringSet<> &getSS(void *TheSS) {
25 return *(StringSet<>*)TheSS;
28 AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
30 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
31 CurPtr = CurBuf->getBufferStart();
34 TheStringSet = new StringSet<>();
37 AsmLexer::~AsmLexer() {
38 delete &getSS(TheStringSet);
41 SMLoc AsmLexer::getLoc() const {
42 return SMLoc::getFromPointer(TokStart);
45 void AsmLexer::PrintMessage(SMLoc Loc, const std::string &Msg) const {
46 SrcMgr.PrintMessage(Loc, Msg);
49 /// ReturnError - Set the error to the specified string at the specified
50 /// location. This is defined to always return asmtok::Error.
51 asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
52 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg);
56 int AsmLexer::getNextChar() {
57 char CurChar = *CurPtr++;
60 return (unsigned char)CurChar;
62 // A nul character in the stream is either the end of the current buffer or
63 // a random nul in the file. Disambiguate that here.
64 if (CurPtr-1 != CurBuf->getBufferEnd())
65 return 0; // Just whitespace.
67 // If this is the end of an included file, pop the parent file off the
69 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
70 if (ParentIncludeLoc != SMLoc()) {
71 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
72 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
73 CurPtr = ParentIncludeLoc.getPointer();
77 // Otherwise, return end of file.
78 --CurPtr; // Another call to lex will return EOF again.
84 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
85 asmtok::TokKind AsmLexer::LexIdentifier() {
86 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
87 *CurPtr == '.' || *CurPtr == '@')
91 getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
92 return asmtok::Identifier;
95 /// LexPercent: Register: %[a-zA-Z0-9]+
96 asmtok::TokKind AsmLexer::LexPercent() {
97 if (!isalnum(*CurPtr))
98 return asmtok::Percent; // Single %.
100 while (isalnum(*CurPtr))
105 getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
106 return asmtok::Register;
109 /// LexSlash: Slash: /
110 /// C-Style Comment: /* ... */
111 asmtok::TokKind AsmLexer::LexSlash() {
113 return asmtok::Slash;
116 ++CurPtr; // skip the star.
118 int CurChar = getNextChar();
121 return ReturnError(TokStart, "unterminated comment");
123 // End of the comment?
124 if (CurPtr[0] != '/') break;
126 ++CurPtr; // End the */.
132 /// LexHash: Comment: #[^\n]*
133 asmtok::TokKind AsmLexer::LexHash() {
134 int CurChar = getNextChar();
135 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
136 CurChar = getNextChar();
140 return asmtok::EndOfStatement;
144 /// LexDigit: First character is [0-9].
145 /// Local Label: [0-9][:]
146 /// Forward/Backward Label: [0-9][fb]
147 /// Binary integer: 0b[01]+
148 /// Octal integer: 0[0-7]+
149 /// Hex integer: 0x[0-9a-fA-F]+
150 /// Decimal integer: [1-9][0-9]*
151 /// TODO: FP literal.
152 asmtok::TokKind AsmLexer::LexDigit() {
154 return ReturnError(TokStart, "FIXME: local label not implemented");
155 if (*CurPtr == 'f' || *CurPtr == 'b')
156 return ReturnError(TokStart, "FIXME: directional label not implemented");
158 // Decimal integer: [1-9][0-9]*
159 if (CurPtr[-1] != '0') {
160 while (isdigit(*CurPtr))
162 CurIntVal = strtoll(TokStart, 0, 10);
163 return asmtok::IntVal;
166 if (*CurPtr == 'b') {
168 const char *NumStart = CurPtr;
169 while (CurPtr[0] == '0' || CurPtr[0] == '1')
172 // Requires at least one binary digit.
173 if (CurPtr == NumStart)
174 return ReturnError(CurPtr-2, "Invalid binary number");
175 CurIntVal = strtoll(NumStart, 0, 2);
176 return asmtok::IntVal;
179 if (*CurPtr == 'x') {
181 const char *NumStart = CurPtr;
182 while (isxdigit(CurPtr[0]))
185 // Requires at least one hex digit.
186 if (CurPtr == NumStart)
187 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
190 CurIntVal = strtoll(NumStart, 0, 16);
192 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
193 if (errno == ERANGE) {
195 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
197 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
199 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
201 return asmtok::IntVal;
204 // Must be an octal number, it starts with 0.
205 while (*CurPtr >= '0' && *CurPtr <= '7')
207 CurIntVal = strtoll(TokStart, 0, 8);
208 return asmtok::IntVal;
211 /// LexQuote: String: "..."
212 asmtok::TokKind AsmLexer::LexQuote() {
213 int CurChar = getNextChar();
214 // TODO: does gas allow multiline string constants?
215 while (CurChar != '"') {
216 if (CurChar == '\\') {
218 CurChar = getNextChar();
222 return ReturnError(TokStart, "unterminated string constant");
224 CurChar = getNextChar();
227 // Unique string, include quotes for now.
229 getSS(TheStringSet).GetOrCreateValue(TokStart, CurPtr, 0).getKeyData();
230 return asmtok::String;
234 asmtok::TokKind AsmLexer::LexToken() {
236 // This always consumes at least one character.
237 int CurChar = getNextChar();
241 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
242 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
243 return LexIdentifier();
245 // Unknown character, emit an error.
246 return ReturnError(TokStart, "invalid character in input");
247 case EOF: return asmtok::Eof;
251 // Ignore whitespace.
253 case '\n': // FALL THROUGH.
254 case '\r': // FALL THROUGH.
255 case ';': return asmtok::EndOfStatement;
256 case ':': return asmtok::Colon;
257 case '+': return asmtok::Plus;
258 case '-': return asmtok::Minus;
259 case '~': return asmtok::Tilde;
260 case '(': return asmtok::LParen;
261 case ')': return asmtok::RParen;
262 case '*': return asmtok::Star;
263 case ',': return asmtok::Comma;
264 case '$': return asmtok::Dollar;
265 case '=': return asmtok::Equal;
266 case '|': return asmtok::Pipe;
267 case '^': return asmtok::Caret;
268 case '&': return asmtok::Amp;
269 case '!': return asmtok::Exclaim;
270 case '%': return LexPercent();
271 case '/': return LexSlash();
272 case '#': return LexHash();
273 case '"': return LexQuote();
274 case '0': case '1': case '2': case '3': case '4':
275 case '5': case '6': case '7': case '8': case '9':
278 if (*CurPtr == '<') {
280 return asmtok::LessLess;
282 // Don't have any use for bare '<' yet.
283 return ReturnError(TokStart, "invalid character in input");
285 if (*CurPtr == '>') {
287 return asmtok::GreaterGreater;
289 // Don't have any use for bare '>' yet.
290 return ReturnError(TokStart, "invalid character in input");
292 // TODO: Quoted identifiers (objc methods etc)
293 // local labels: [0-9][:]
294 // Forward/backward labels: [0-9][fb]
295 // Integers, fp constants, character constants.