Proper name 16 bit libcalls
[llvm/msp430.git] / utils / TableGen / TGLexer.cpp
blobf2ea7a151715186ba6bb0ed282f9f5d79f6b5d35
1 //===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
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 // Implement the Lexer for TableGen.
12 //===----------------------------------------------------------------------===//
14 #include "TGLexer.h"
15 #include "TGSourceMgr.h"
16 #include "llvm/Support/Streams.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include <ostream>
19 #include "llvm/Config/config.h"
20 #include <cctype>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 #include <cerrno>
25 using namespace llvm;
27 TGLexer::TGLexer(TGSourceMgr &SM) : SrcMgr(SM) {
28 CurBuffer = 0;
29 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
30 CurPtr = CurBuf->getBufferStart();
31 TokStart = 0;
34 TGLoc TGLexer::getLoc() const {
35 return TGLoc::getFromPointer(TokStart);
39 /// ReturnError - Set the error to the specified string at the specified
40 /// location. This is defined to always return tgtok::Error.
41 tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
42 PrintError(Loc, Msg);
43 return tgtok::Error;
47 void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
48 SrcMgr.PrintError(TGLoc::getFromPointer(Loc), Msg);
51 void TGLexer::PrintError(TGLoc Loc, const std::string &Msg) const {
52 SrcMgr.PrintError(Loc, Msg);
56 int TGLexer::getNextChar() {
57 char CurChar = *CurPtr++;
58 switch (CurChar) {
59 default:
60 return (unsigned char)CurChar;
61 case 0: {
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
68 // include stack.
69 TGLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
70 if (ParentIncludeLoc != TGLoc()) {
71 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
72 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
73 CurPtr = ParentIncludeLoc.getPointer();
74 return getNextChar();
77 // Otherwise, return end of file.
78 --CurPtr; // Another call to lex will return EOF again.
79 return EOF;
81 case '\n':
82 case '\r':
83 // Handle the newline character by ignoring it and incrementing the line
84 // count. However, be careful about 'dos style' files with \n\r in them.
85 // Only treat a \n\r or \r\n as a single line.
86 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
87 *CurPtr != CurChar)
88 ++CurPtr; // Eat the two char newline sequence.
89 return '\n';
93 tgtok::TokKind TGLexer::LexToken() {
94 TokStart = CurPtr;
95 // This always consumes at least one character.
96 int CurChar = getNextChar();
98 switch (CurChar) {
99 default:
100 // Handle letters: [a-zA-Z_]
101 if (isalpha(CurChar) || CurChar == '_')
102 return LexIdentifier();
104 // Unknown character, emit an error.
105 return ReturnError(TokStart, "Unexpected character");
106 case EOF: return tgtok::Eof;
107 case ':': return tgtok::colon;
108 case ';': return tgtok::semi;
109 case '.': return tgtok::period;
110 case ',': return tgtok::comma;
111 case '<': return tgtok::less;
112 case '>': return tgtok::greater;
113 case ']': return tgtok::r_square;
114 case '{': return tgtok::l_brace;
115 case '}': return tgtok::r_brace;
116 case '(': return tgtok::l_paren;
117 case ')': return tgtok::r_paren;
118 case '=': return tgtok::equal;
119 case '?': return tgtok::question;
121 case 0:
122 case ' ':
123 case '\t':
124 case '\n':
125 case '\r':
126 // Ignore whitespace.
127 return LexToken();
128 case '/':
129 // If this is the start of a // comment, skip until the end of the line or
130 // the end of the buffer.
131 if (*CurPtr == '/')
132 SkipBCPLComment();
133 else if (*CurPtr == '*') {
134 if (SkipCComment())
135 return tgtok::Error;
136 } else // Otherwise, this is an error.
137 return ReturnError(TokStart, "Unexpected character");
138 return LexToken();
139 case '-': case '+':
140 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
141 case '7': case '8': case '9':
142 return LexNumber();
143 case '"': return LexString();
144 case '$': return LexVarName();
145 case '[': return LexBracket();
146 case '!': return LexExclaim();
150 /// LexString - Lex "[^"]*"
151 tgtok::TokKind TGLexer::LexString() {
152 const char *StrStart = CurPtr;
154 CurStrVal = "";
156 while (*CurPtr != '"') {
157 // If we hit the end of the buffer, report an error.
158 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
159 return ReturnError(StrStart, "End of file in string literal");
161 if (*CurPtr == '\n' || *CurPtr == '\r')
162 return ReturnError(StrStart, "End of line in string literal");
164 if (*CurPtr != '\\') {
165 CurStrVal += *CurPtr++;
166 continue;
169 ++CurPtr;
171 switch (*CurPtr) {
172 case '\\': case '\'': case '"':
173 // These turn into their literal character.
174 CurStrVal += *CurPtr++;
175 break;
176 case 't':
177 CurStrVal += '\t';
178 ++CurPtr;
179 break;
180 case 'n':
181 CurStrVal += '\n';
182 ++CurPtr;
183 break;
185 case '\n':
186 case '\r':
187 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
189 // If we hit the end of the buffer, report an error.
190 case '\0':
191 if (CurPtr == CurBuf->getBufferEnd())
192 return ReturnError(StrStart, "End of file in string literal");
193 // FALL THROUGH
194 default:
195 return ReturnError(CurPtr, "invalid escape in string literal");
199 ++CurPtr;
200 return tgtok::StrVal;
203 tgtok::TokKind TGLexer::LexVarName() {
204 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
205 return ReturnError(TokStart, "Invalid variable name");
207 // Otherwise, we're ok, consume the rest of the characters.
208 const char *VarNameStart = CurPtr++;
210 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
211 ++CurPtr;
213 CurStrVal.assign(VarNameStart, CurPtr);
214 return tgtok::VarName;
218 tgtok::TokKind TGLexer::LexIdentifier() {
219 // The first letter is [a-zA-Z_].
220 const char *IdentStart = TokStart;
222 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
223 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
224 ++CurPtr;
226 // Check to see if this identifier is a keyword.
227 unsigned Len = CurPtr-IdentStart;
229 if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
230 if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
231 if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
232 if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
233 if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
234 if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
235 if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
237 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
238 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
239 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
240 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
241 return tgtok::MultiClass;
242 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
243 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
244 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
246 if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
247 if (LexInclude()) return tgtok::Error;
248 return Lex();
251 CurStrVal.assign(IdentStart, CurPtr);
252 return tgtok::Id;
255 /// LexInclude - We just read the "include" token. Get the string token that
256 /// comes next and enter the include.
257 bool TGLexer::LexInclude() {
258 // The token after the include must be a string.
259 tgtok::TokKind Tok = LexToken();
260 if (Tok == tgtok::Error) return true;
261 if (Tok != tgtok::StrVal) {
262 PrintError(getLoc(), "Expected filename after include");
263 return true;
266 // Get the string.
267 std::string Filename = CurStrVal;
269 // Try to find the file.
270 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
272 // If the file didn't exist directly, see if it's in an include path.
273 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
274 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
275 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
278 if (NewBuf == 0) {
279 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
280 return true;
283 // Save the line number and lex buffer of the includer.
284 CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, TGLoc::getFromPointer(CurPtr));
286 CurBuf = NewBuf;
287 CurPtr = CurBuf->getBufferStart();
288 return false;
291 void TGLexer::SkipBCPLComment() {
292 ++CurPtr; // skip the second slash.
293 while (1) {
294 switch (*CurPtr) {
295 case '\n':
296 case '\r':
297 return; // Newline is end of comment.
298 case 0:
299 // If this is the end of the buffer, end the comment.
300 if (CurPtr == CurBuf->getBufferEnd())
301 return;
302 break;
304 // Otherwise, skip the character.
305 ++CurPtr;
309 /// SkipCComment - This skips C-style /**/ comments. The only difference from C
310 /// is that we allow nesting.
311 bool TGLexer::SkipCComment() {
312 ++CurPtr; // skip the star.
313 unsigned CommentDepth = 1;
315 while (1) {
316 int CurChar = getNextChar();
317 switch (CurChar) {
318 case EOF:
319 PrintError(TokStart, "Unterminated comment!");
320 return true;
321 case '*':
322 // End of the comment?
323 if (CurPtr[0] != '/') break;
325 ++CurPtr; // End the */.
326 if (--CommentDepth == 0)
327 return false;
328 break;
329 case '/':
330 // Start of a nested comment?
331 if (CurPtr[0] != '*') break;
332 ++CurPtr;
333 ++CommentDepth;
334 break;
339 /// LexNumber - Lex:
340 /// [-+]?[0-9]+
341 /// 0x[0-9a-fA-F]+
342 /// 0b[01]+
343 tgtok::TokKind TGLexer::LexNumber() {
344 if (CurPtr[-1] == '0') {
345 if (CurPtr[0] == 'x') {
346 ++CurPtr;
347 const char *NumStart = CurPtr;
348 while (isxdigit(CurPtr[0]))
349 ++CurPtr;
351 // Requires at least one hex digit.
352 if (CurPtr == NumStart)
353 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
355 errno = 0;
356 CurIntVal = strtoll(NumStart, 0, 16);
357 if (errno == EINVAL)
358 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
359 if (errno == ERANGE) {
360 errno = 0;
361 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
362 if (errno == EINVAL)
363 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
364 if (errno == ERANGE)
365 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
367 return tgtok::IntVal;
368 } else if (CurPtr[0] == 'b') {
369 ++CurPtr;
370 const char *NumStart = CurPtr;
371 while (CurPtr[0] == '0' || CurPtr[0] == '1')
372 ++CurPtr;
374 // Requires at least one binary digit.
375 if (CurPtr == NumStart)
376 return ReturnError(CurPtr-2, "Invalid binary number");
377 CurIntVal = strtoll(NumStart, 0, 2);
378 return tgtok::IntVal;
382 // Check for a sign without a digit.
383 if (!isdigit(CurPtr[0])) {
384 if (CurPtr[-1] == '-')
385 return tgtok::minus;
386 else if (CurPtr[-1] == '+')
387 return tgtok::plus;
390 while (isdigit(CurPtr[0]))
391 ++CurPtr;
392 CurIntVal = strtoll(TokStart, 0, 10);
393 return tgtok::IntVal;
396 /// LexBracket - We just read '['. If this is a code block, return it,
397 /// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
398 tgtok::TokKind TGLexer::LexBracket() {
399 if (CurPtr[0] != '{')
400 return tgtok::l_square;
401 ++CurPtr;
402 const char *CodeStart = CurPtr;
403 while (1) {
404 int Char = getNextChar();
405 if (Char == EOF) break;
407 if (Char != '}') continue;
409 Char = getNextChar();
410 if (Char == EOF) break;
411 if (Char == ']') {
412 CurStrVal.assign(CodeStart, CurPtr-2);
413 return tgtok::CodeFragment;
417 return ReturnError(CodeStart-2, "Unterminated Code Block");
420 /// LexExclaim - Lex '!' and '![a-zA-Z]+'.
421 tgtok::TokKind TGLexer::LexExclaim() {
422 if (!isalpha(*CurPtr))
423 return ReturnError(CurPtr-1, "Invalid \"!operator\"");
425 const char *Start = CurPtr++;
426 while (isalpha(*CurPtr))
427 ++CurPtr;
429 // Check to see which operator this is.
430 unsigned Len = CurPtr-Start;
432 if (Len == 3 && !memcmp(Start, "con", 3)) return tgtok::XConcat;
433 if (Len == 3 && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
434 if (Len == 3 && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
435 if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
436 if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
437 if (Len == 10 && !memcmp(Start, "nameconcat", 10)) return tgtok::XNameConcat;
439 return ReturnError(Start-1, "Unknown operator");