blob: 578eec18526a86a6a339dffd18b552508a247e16 [file] [log] [blame]
Chris Lattnera59e8772009-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 Lattner4651bca2009-06-21 19:21:25 +000017#include <cerrno>
Chris Lattnera59e8772009-06-21 07:19:10 +000018using namespace llvm;
19
20AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
21 CurBuffer = 0;
22 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
23 CurPtr = CurBuf->getBufferStart();
24 TokStart = 0;
25}
26
Chris Lattner4651bca2009-06-21 19:21:25 +000027SMLoc AsmLexer::getLoc() const {
28 return SMLoc::getFromPointer(TokStart);
29}
30
Chris Lattnera59e8772009-06-21 07:19:10 +000031void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
32 SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
33}
34
35void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
36 SrcMgr.PrintError(Loc, Msg);
37}
38
Chris Lattner4651bca2009-06-21 19:21:25 +000039/// ReturnError - Set the error to the specified string at the specified
40/// location. This is defined to always return asmtok::Error.
41asmtok::TokKind AsmLexer::ReturnError(const char *Loc, const std::string &Msg) {
42 PrintError(Loc, Msg);
43 return asmtok::Error;
44}
45
Chris Lattnera59e8772009-06-21 07:19:10 +000046int AsmLexer::getNextChar() {
47 char CurChar = *CurPtr++;
48 switch (CurChar) {
49 default:
50 return (unsigned char)CurChar;
51 case 0: {
52 // A nul character in the stream is either the end of the current buffer or
53 // a random nul in the file. Disambiguate that here.
54 if (CurPtr-1 != CurBuf->getBufferEnd())
55 return 0; // Just whitespace.
56
57 // If this is the end of an included file, pop the parent file off the
58 // include stack.
59 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
60 if (ParentIncludeLoc != SMLoc()) {
61 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
62 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
63 CurPtr = ParentIncludeLoc.getPointer();
64 return getNextChar();
65 }
66
67 // Otherwise, return end of file.
68 --CurPtr; // Another call to lex will return EOF again.
69 return EOF;
70 }
71 }
72}
73
Chris Lattner4651bca2009-06-21 19:21:25 +000074/// LexIdentifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
75asmtok::TokKind AsmLexer::LexIdentifier() {
76 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
77 *CurPtr == '.' || *CurPtr == '@')
78 ++CurPtr;
79 CurStrVal.assign(TokStart, CurPtr); // Skip %
80 return asmtok::Identifier;
81}
82
83/// LexPercent: Register: %[a-zA-Z0-9]+
84asmtok::TokKind AsmLexer::LexPercent() {
85 if (!isalnum(*CurPtr))
86 return asmtok::Error; // Must have at least one character.
87 while (isalnum(*CurPtr))
88 ++CurPtr;
89 CurStrVal.assign(TokStart, CurPtr); // Skip %
90 return asmtok::Register;
91}
92
93/// LexSlash: Slash: /
94/// C-Style Comment: /* ... */
95asmtok::TokKind AsmLexer::LexSlash() {
96 if (*CurPtr != '*')
97 return asmtok::Slash;
98
99 // C Style comment.
100 ++CurPtr; // skip the star.
101 while (1) {
102 int CurChar = getNextChar();
103 switch (CurChar) {
104 case EOF:
105 PrintError(TokStart, "Unterminated comment!");
106 return asmtok::Error;
107 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 == ':')
139 return asmtok::Error; // FIXME LOCAL LABEL.
140 if (*CurPtr == 'f' || *CurPtr == 'b')
141 return asmtok::Error; // FIXME FORWARD/BACKWARD LABEL.
142
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
196
Chris Lattnera59e8772009-06-21 07:19:10 +0000197asmtok::TokKind AsmLexer::LexToken() {
198 TokStart = CurPtr;
199 // This always consumes at least one character.
200 int CurChar = getNextChar();
201
202 switch (CurChar) {
203 default:
Chris Lattner4651bca2009-06-21 19:21:25 +0000204 // Handle identifier: [a-zA-Z_.][a-zA-Z0-9_$.@]*
205 if (isalpha(CurChar) || CurChar == '_' || CurChar == '.')
206 return LexIdentifier();
Chris Lattnera59e8772009-06-21 07:19:10 +0000207
208 // Unknown character, emit an error.
209 return asmtok::Error;
210 case EOF: return asmtok::Eof;
211 case 0:
212 case ' ':
213 case '\t':
Chris Lattnera59e8772009-06-21 07:19:10 +0000214 // Ignore whitespace.
215 return LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +0000216 case '\n': // FALL THROUGH.
217 case '\r': // FALL THROUGH.
218 case ';': return asmtok::EndOfStatement;
Chris Lattnera59e8772009-06-21 07:19:10 +0000219 case ':': return asmtok::Colon;
220 case '+': return asmtok::Plus;
221 case '-': return asmtok::Minus;
Chris Lattner4651bca2009-06-21 19:21:25 +0000222 case '(': return asmtok::LParen;
223 case ')': return asmtok::RParen;
224 case '*': return asmtok::Star;
225 case ',': return asmtok::Comma;
226 case '$': return asmtok::Dollar;
227 case '%': return LexPercent();
228 case '/': return LexSlash();
229 case '#': return LexHash();
230 case '0': case '1': case '2': case '3': case '4':
231 case '5': case '6': case '7': case '8': case '9':
232 return LexDigit();
233
234 // TODO: Quoted identifiers (objc methods etc)
235 // local labels: [0-9][:]
236 // Forward/backward labels: [0-9][fb]
237 // Integers, fp constants, character constants.
Chris Lattnera59e8772009-06-21 07:19:10 +0000238 }
239}