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::PrintMessage(SMLoc Loc, const std::string &Msg) const {
33 SrcMgr.PrintMessage(Loc, Msg);
36 /// ReturnError - Set the error to the specified string at the specified
37 /// location. This is defined to always return asmtok::Error.
38 asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
39 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), Msg);
43 int AsmLexer::getNextChar() {
44 char CurChar = *CurPtr++;
47 return (unsigned char)CurChar;
49 // A nul character in the stream is either the end of the current buffer or
50 // a random nul in the file. Disambiguate that here.
51 if (CurPtr-1 != CurBuf->getBufferEnd())
52 return 0; // Just whitespace.
54 // If this is the end of an included file, pop the parent file off the
56 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
57 if (ParentIncludeLoc != SMLoc()) {
58 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
59 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
60 CurPtr = ParentIncludeLoc.getPointer();
64 // Otherwise, return end of file.
65 --CurPtr; // Another call to lex will return EOF again.
71 /// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
72 asmtok::TokKind AsmLexer::LexIdentifier() {
73 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
74 *CurPtr == '.' || *CurPtr == '@')
76 CurStrVal.assign(TokStart, CurPtr); // Include %
77 return asmtok::Identifier;
80 /// LexPercent: Register: %[a-zA-Z0-9]+
81 asmtok::TokKind AsmLexer::LexPercent() {
82 if (!isalnum(*CurPtr))
83 return ReturnError(TokStart, "invalid register name");
84 while (isalnum(*CurPtr))
86 CurStrVal.assign(TokStart, CurPtr); // Skip %
87 return asmtok::Register;
90 /// LexSlash: Slash: /
91 /// C-Style Comment: /* ... */
92 asmtok::TokKind AsmLexer::LexSlash() {
97 ++CurPtr; // skip the star.
99 int CurChar = getNextChar();
102 return ReturnError(TokStart, "unterminated comment");
104 // End of the comment?
105 if (CurPtr[0] != '/') break;
107 ++CurPtr; // End the */.
113 /// LexHash: Comment: #[^\n]*
114 asmtok::TokKind AsmLexer::LexHash() {
115 int CurChar = getNextChar();
116 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
117 CurChar = getNextChar();
121 return asmtok::EndOfStatement;
125 /// LexDigit: First character is [0-9].
126 /// Local Label: [0-9][:]
127 /// Forward/Backward Label: [0-9][fb]
128 /// Binary integer: 0b[01]+
129 /// Octal integer: 0[0-7]+
130 /// Hex integer: 0x[0-9a-fA-F]+
131 /// Decimal integer: [1-9][0-9]*
132 /// TODO: FP literal.
133 asmtok::TokKind AsmLexer::LexDigit() {
135 return ReturnError(TokStart, "FIXME: local label not implemented");
136 if (*CurPtr == 'f' || *CurPtr == 'b')
137 return ReturnError(TokStart, "FIXME: directional label not implemented");
139 // Decimal integer: [1-9][0-9]*
140 if (CurPtr[-1] != '0') {
141 while (isdigit(*CurPtr))
143 CurIntVal = strtoll(TokStart, 0, 10);
144 return asmtok::IntVal;
147 if (*CurPtr == 'b') {
149 const char *NumStart = CurPtr;
150 while (CurPtr[0] == '0' || CurPtr[0] == '1')
153 // Requires at least one binary digit.
154 if (CurPtr == NumStart)
155 return ReturnError(CurPtr-2, "Invalid binary number");
156 CurIntVal = strtoll(NumStart, 0, 2);
157 return asmtok::IntVal;
160 if (*CurPtr == 'x') {
162 const char *NumStart = CurPtr;
163 while (isxdigit(CurPtr[0]))
166 // Requires at least one hex digit.
167 if (CurPtr == NumStart)
168 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
171 CurIntVal = strtoll(NumStart, 0, 16);
173 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
174 if (errno == ERANGE) {
176 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
178 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
180 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
182 return asmtok::IntVal;
185 // Must be an octal number, it starts with 0.
186 while (*CurPtr >= '0' && *CurPtr <= '7')
188 CurIntVal = strtoll(TokStart, 0, 8);
189 return asmtok::IntVal;
192 /// LexQuote: String: "..."
193 asmtok::TokKind AsmLexer::LexQuote() {
194 int CurChar = getNextChar();
195 // TODO: does gas allow multiline string constants?
196 while (CurChar != '"') {
197 if (CurChar == '\\') {
199 CurChar = getNextChar();
203 return ReturnError(TokStart, "unterminated string constant");
205 CurChar = getNextChar();
208 CurStrVal.assign(TokStart, CurPtr); // include quotes.
209 return asmtok::String;
213 asmtok::TokKind AsmLexer::LexToken() {
215 // This always consumes at least one character.
216 int CurChar = getNextChar();
220 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
221 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
222 return LexIdentifier();
224 // Unknown character, emit an error.
225 return ReturnError(TokStart, "invalid character in input");
226 case EOF: return asmtok::Eof;
230 // Ignore whitespace.
232 case '\n': // FALL THROUGH.
233 case '\r': // FALL THROUGH.
234 case ';': return asmtok::EndOfStatement;
235 case ':': return asmtok::Colon;
236 case '+': return asmtok::Plus;
237 case '-': return asmtok::Minus;
238 case '(': return asmtok::LParen;
239 case ')': return asmtok::RParen;
240 case '*': return asmtok::Star;
241 case ',': return asmtok::Comma;
242 case '$': return asmtok::Dollar;
243 case '%': return LexPercent();
244 case '/': return LexSlash();
245 case '#': return LexHash();
246 case '"': return LexQuote();
247 case '0': case '1': case '2': case '3': case '4':
248 case '5': case '6': case '7': case '8': case '9':
251 // TODO: Quoted identifiers (objc methods etc)
252 // local labels: [0-9][:]
253 // Forward/backward labels: [0-9][fb]
254 // Integers, fp constants, character constants.