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 asmtok::Error; // Must have at least one character.
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 PrintError(TokStart, "Unterminated comment!");
107 return asmtok::Error;
109 // End of the comment?
110 if (CurPtr[0] != '/') break;
112 ++CurPtr; // End the */.
118 /// LexHash: Comment: #[^\n]*
119 asmtok::TokKind AsmLexer::LexHash() {
120 int CurChar = getNextChar();
121 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
122 CurChar = getNextChar();
126 return asmtok::EndOfStatement;
130 /// LexDigit: First character is [0-9].
131 /// Local Label: [0-9][:]
132 /// Forward/Backward Label: [0-9][fb]
133 /// Binary integer: 0b[01]+
134 /// Octal integer: 0[0-7]+
135 /// Hex integer: 0x[0-9a-fA-F]+
136 /// Decimal integer: [1-9][0-9]*
137 /// TODO: FP literal.
138 asmtok::TokKind AsmLexer::LexDigit() {
140 return asmtok::Error; // FIXME LOCAL LABEL.
141 if (*CurPtr == 'f' || *CurPtr == 'b')
142 return asmtok::Error; // FIXME FORWARD/BACKWARD LABEL.
144 // Decimal integer: [1-9][0-9]*
145 if (CurPtr[-1] != '0') {
146 while (isdigit(*CurPtr))
148 CurIntVal = strtoll(TokStart, 0, 10);
149 return asmtok::IntVal;
152 if (*CurPtr == 'b') {
154 const char *NumStart = CurPtr;
155 while (CurPtr[0] == '0' || CurPtr[0] == '1')
158 // Requires at least one binary digit.
159 if (CurPtr == NumStart)
160 return ReturnError(CurPtr-2, "Invalid binary number");
161 CurIntVal = strtoll(NumStart, 0, 2);
162 return asmtok::IntVal;
165 if (*CurPtr == 'x') {
167 const char *NumStart = CurPtr;
168 while (isxdigit(CurPtr[0]))
171 // Requires at least one hex digit.
172 if (CurPtr == NumStart)
173 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
176 CurIntVal = strtoll(NumStart, 0, 16);
178 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
179 if (errno == ERANGE) {
181 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
183 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
185 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
187 return asmtok::IntVal;
190 // Must be an octal number, it starts with 0.
191 while (*CurPtr >= '0' && *CurPtr <= '7')
193 CurIntVal = strtoll(TokStart, 0, 8);
194 return asmtok::IntVal;
197 /// LexQuote: String: "..."
198 asmtok::TokKind AsmLexer::LexQuote() {
199 int CurChar = getNextChar();
200 // TODO: does gas allow multiline string constants?
201 while (CurChar != '"') {
202 if (CurChar == '\\') {
204 CurChar = getNextChar();
207 if (CurChar == EOF) {
208 PrintError(TokStart, "unterminated string constant");
212 CurChar = getNextChar();
215 CurStrVal.assign(TokStart, CurPtr); // include quotes.
216 return asmtok::String;
220 asmtok::TokKind AsmLexer::LexToken() {
222 // This always consumes at least one character.
223 int CurChar = getNextChar();
227 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
228 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
229 return LexIdentifier();
231 // Unknown character, emit an error.
232 return asmtok::Error;
233 case EOF: return asmtok::Eof;
237 // Ignore whitespace.
239 case '\n': // FALL THROUGH.
240 case '\r': // FALL THROUGH.
241 case ';': return asmtok::EndOfStatement;
242 case ':': return asmtok::Colon;
243 case '+': return asmtok::Plus;
244 case '-': return asmtok::Minus;
245 case '(': return asmtok::LParen;
246 case ')': return asmtok::RParen;
247 case '*': return asmtok::Star;
248 case ',': return asmtok::Comma;
249 case '$': return asmtok::Dollar;
250 case '%': return LexPercent();
251 case '/': return LexSlash();
252 case '#': return LexHash();
253 case '"': return LexQuote();
254 case '0': case '1': case '2': case '3': case '4':
255 case '5': case '6': case '7': case '8': case '9':
258 // TODO: Quoted identifiers (objc methods etc)
259 // local labels: [0-9][:]
260 // Forward/backward labels: [0-9][fb]
261 // Integers, fp constants, character constants.