blob: cf6a4a8c124c234db8a48f73dbd7390fbf1bfc4c [file] [log] [blame]
Chris Lattner22b67fb2009-06-21 07:19:10 +00001//===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class implements the lexer for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AsmLexer.h"
15#include "llvm/Support/SourceMgr.h"
16#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerc688c232009-06-21 19:21:25 +000017#include <cerrno>
Chris Lattnerc3e6b202009-06-21 19:43:50 +000018#include <cstdlib>
Chris Lattner22b67fb2009-06-21 07:19:10 +000019using namespace llvm;
20
21AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
22 CurBuffer = 0;
23 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
24 CurPtr = CurBuf->getBufferStart();
25 TokStart = 0;
26}
27
Chris Lattnerc688c232009-06-21 19:21:25 +000028SMLoc AsmLexer::getLoc() const {
29 return SMLoc::getFromPointer(TokStart);
30}
31
Chris Lattner22b67fb2009-06-21 07:19:10 +000032void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
33 SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
34}
35
36void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
37 SrcMgr.PrintError(Loc, Msg);
38}
39
Chris Lattnerc688c232009-06-21 19:21:25 +000040/// ReturnError - Set the error to the specified string at the specified
41/// location. This is defined to always return asmtok::Error.
42asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
43 PrintError(Loc, Msg);
44 return asmtok::Error;
45}
46
Chris Lattner22b67fb2009-06-21 07:19:10 +000047int AsmLexer::getNextChar() {
48 char CurChar = *CurPtr++;
49 switch (CurChar) {
50 default:
51 return (unsigned char)CurChar;
52 case 0: {
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.
57
58 // If this is the end of an included file, pop the parent file off the
59 // include stack.
60 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
61 if (ParentIncludeLoc != SMLoc()) {
62 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
63 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
64 CurPtr = ParentIncludeLoc.getPointer();
65 return getNextChar();
66 }
67
68 // Otherwise, return end of file.
69 --CurPtr; // Another call to lex will return EOF again.
70 return EOF;
71 }
72 }
73}
74
Chris Lattnerc688c232009-06-21 19:21:25 +000075/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
76asmtok::TokKind AsmLexer::LexIdentifier() {
77 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
78 *CurPtr == '.' || *CurPtr == '@')
79 ++CurPtr;
Chris Lattnerba605b72009-06-21 19:56:35 +000080 CurStrVal.assign(TokStart, CurPtr); // Include %
Chris Lattnerc688c232009-06-21 19:21:25 +000081 return asmtok::Identifier;
82}
83
84/// LexPercent: Register: %[a-zA-Z0-9]+
85asmtok::TokKind AsmLexer::LexPercent() {
86 if (!isalnum(*CurPtr))
Chris Lattner3b4bfbd2009-06-21 20:16:42 +000087 return ReturnError(TokStart, "invalid register name");
Chris Lattnerc688c232009-06-21 19:21:25 +000088 while (isalnum(*CurPtr))
89 ++CurPtr;
90 CurStrVal.assign(TokStart, CurPtr); // Skip %
91 return asmtok::Register;
92}
93
94/// LexSlash: Slash: /
95/// C-Style Comment: /* ... */
96asmtok::TokKind AsmLexer::LexSlash() {
97 if (*CurPtr != '*')
98 return asmtok::Slash;
99
100 // C Style comment.
101 ++CurPtr; // skip the star.
102 while (1) {
103 int CurChar = getNextChar();
104 switch (CurChar) {
105 case EOF:
Chris Lattner3b4bfbd2009-06-21 20:16:42 +0000106 return ReturnError(TokStart, "unterminated comment");
Chris Lattnerc688c232009-06-21 19:21:25 +0000107 case '*':
108 // End of the comment?
109 if (CurPtr[0] != '/') break;
110
111 ++CurPtr; // End the */.
112 return LexToken();
113 }
114 }
115}
116
117/// LexHash: Comment: #[^\n]*
118asmtok::TokKind AsmLexer::LexHash() {
119 int CurChar = getNextChar();
120 while (CurChar != '\n' && CurChar != '\n' && CurChar != EOF)
121 CurChar = getNextChar();
122
123 if (CurChar == EOF)
124 return asmtok::Eof;
125 return asmtok::EndOfStatement;
126}
127
128
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.
137asmtok::TokKind AsmLexer::LexDigit() {
138 if (*CurPtr == ':')
Chris Lattner3b4bfbd2009-06-21 20:16:42 +0000139 return ReturnError(TokStart, "FIXME: local label not implemented");
Chris Lattnerc688c232009-06-21 19:21:25 +0000140 if (*CurPtr == 'f' || *CurPtr == 'b')
Chris Lattner3b4bfbd2009-06-21 20:16:42 +0000141 return ReturnError(TokStart, "FIXME: directional label not implemented");
Chris Lattnerc688c232009-06-21 19:21:25 +0000142
143 // Decimal integer: [1-9][0-9]*
144 if (CurPtr[-1] != '0') {
145 while (isdigit(*CurPtr))
146 ++CurPtr;
147 CurIntVal = strtoll(TokStart, 0, 10);
148 return asmtok::IntVal;
149 }
150
151 if (*CurPtr == 'b') {
152 ++CurPtr;
153 const char *NumStart = CurPtr;
154 while (CurPtr[0] == '0' || CurPtr[0] == '1')
155 ++CurPtr;
156
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;
162 }
163
164 if (*CurPtr == 'x') {
165 ++CurPtr;
166 const char *NumStart = CurPtr;
167 while (isxdigit(CurPtr[0]))
168 ++CurPtr;
169
170 // Requires at least one hex digit.
171 if (CurPtr == NumStart)
172 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
173
174 errno = 0;
175 CurIntVal = strtoll(NumStart, 0, 16);
176 if (errno == EINVAL)
177 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
178 if (errno == ERANGE) {
179 errno = 0;
180 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
181 if (errno == EINVAL)
182 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
183 if (errno == ERANGE)
184 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
185 }
186 return asmtok::IntVal;
187 }
188
189 // Must be an octal number, it starts with 0.
190 while (*CurPtr >= '0' && *CurPtr <= '7')
191 ++CurPtr;
192 CurIntVal = strtoll(TokStart, 0, 8);
193 return asmtok::IntVal;
194}
195
Chris Lattnerba605b72009-06-21 19:56:35 +0000196/// LexQuote: String: "..."
197asmtok::TokKind AsmLexer::LexQuote() {
198 int CurChar = getNextChar();
199 // TODO: does gas allow multiline string constants?
200 while (CurChar != '"') {
201 if (CurChar == '\\') {
202 // Allow \", etc.
203 CurChar = getNextChar();
204 }
205
206 if (CurChar == EOF) {
207 PrintError(TokStart, "unterminated string constant");
208 return asmtok::Eof;
209 }
210
211 CurChar = getNextChar();
212 }
213
214 CurStrVal.assign(TokStart, CurPtr); // include quotes.
215 return asmtok::String;
216}
217
Chris Lattnerc688c232009-06-21 19:21:25 +0000218
Chris Lattner22b67fb2009-06-21 07:19:10 +0000219asmtok::TokKind AsmLexer::LexToken() {
220 TokStart = CurPtr;
221 // This always consumes at least one character.
222 int CurChar = getNextChar();
223
224 switch (CurChar) {
225 default:
Chris Lattnerc688c232009-06-21 19:21:25 +0000226 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
227 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
228 return LexIdentifier();
Chris Lattner22b67fb2009-06-21 07:19:10 +0000229
230 // Unknown character, emit an error.
Chris Lattner3b4bfbd2009-06-21 20:16:42 +0000231 return ReturnError(TokStart, "invalid character in input");
Chris Lattner22b67fb2009-06-21 07:19:10 +0000232 case EOF: return asmtok::Eof;
233 case 0:
234 case ' ':
235 case '\t':
Chris Lattner22b67fb2009-06-21 07:19:10 +0000236 // Ignore whitespace.
237 return LexToken();
Chris Lattnerc688c232009-06-21 19:21:25 +0000238 case '\n': // FALL THROUGH.
239 case '\r': // FALL THROUGH.
240 case ';': return asmtok::EndOfStatement;
Chris Lattner22b67fb2009-06-21 07:19:10 +0000241 case ':': return asmtok::Colon;
242 case '+': return asmtok::Plus;
243 case '-': return asmtok::Minus;
Chris Lattnerc688c232009-06-21 19:21:25 +0000244 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();
Chris Lattnerba605b72009-06-21 19:56:35 +0000252 case '"': return LexQuote();
Chris Lattnerc688c232009-06-21 19:21:25 +0000253 case '0': case '1': case '2': case '3': case '4':
254 case '5': case '6': case '7': case '8': case '9':
255 return LexDigit();
256
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.
Chris Lattner22b67fb2009-06-21 07:19:10 +0000261 }
262}