blob: 8b810918dc676509ab2acd0180e045b1278f51f2 [file] [log] [blame]
Chris Lattnera8058742007-11-18 02:57:27 +00001//===- TGLexer.cpp - Lexer for TableGen -----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera8058742007-11-18 02:57:27 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Lexer for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner6aaca042007-11-18 05:25:45 +000014#include "TGLexer.h"
Chris Lattneraa739d22009-03-13 07:05:43 +000015#include "TGSourceMgr.h"
Chris Lattnera8058742007-11-18 02:57:27 +000016#include "llvm/Support/Streams.h"
Chris Lattnera8058742007-11-18 02:57:27 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000018#include <ostream>
Chuck Rose III8b0ec642007-11-21 19:36:25 +000019#include "llvm/Config/config.h"
Chris Lattnera8058742007-11-18 02:57:27 +000020#include <cctype>
Duncan Sands4520dd22008-10-08 07:23:46 +000021#include <cstdio>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000022#include <cstdlib>
23#include <cstring>
Dan Gohman63f97202008-10-17 01:33:43 +000024#include <cerrno>
Chris Lattnera8058742007-11-18 02:57:27 +000025using namespace llvm;
26
Chris Lattneraa739d22009-03-13 07:05:43 +000027TGLexer::TGLexer(TGSourceMgr &SM) : SrcMgr(SM) {
28 CurBuffer = 0;
29 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattnera8058742007-11-18 02:57:27 +000030 CurPtr = CurBuf->getBufferStart();
Chris Lattner56a9fcf2007-11-19 07:43:52 +000031 TokStart = 0;
Chris Lattnera8058742007-11-18 02:57:27 +000032}
33
Chris Lattner1c8ae592009-03-13 16:01:53 +000034TGLoc TGLexer::getLoc() const {
35 return TGLoc::getFromPointer(TokStart);
36}
37
Chris Lattnera8058742007-11-18 02:57:27 +000038
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000039/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4601652007-11-22 20:49:04 +000040/// location. This is defined to always return tgtok::Error.
41tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000042 PrintError(Loc, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000043 return tgtok::Error;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000044}
Chris Lattnera8058742007-11-18 02:57:27 +000045
Chris Lattnera8058742007-11-18 02:57:27 +000046
Chris Lattner1c8ae592009-03-13 16:01:53 +000047void TGLexer::PrintError(const char *Loc, const std::string &Msg) const {
48 SrcMgr.PrintError(TGLoc::getFromPointer(Loc), Msg);
49}
50
51void TGLexer::PrintError(TGLoc Loc, const std::string &Msg) const {
Chris Lattneraa739d22009-03-13 07:05:43 +000052 SrcMgr.PrintError(Loc, Msg);
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000053}
54
Chris Lattner1c8ae592009-03-13 16:01:53 +000055
Chris Lattnera8058742007-11-18 02:57:27 +000056int TGLexer::getNextChar() {
57 char CurChar = *CurPtr++;
58 switch (CurChar) {
59 default:
Chris Lattnerc1819182007-11-18 05:48:46 +000060 return (unsigned char)CurChar;
Chris Lattneraa739d22009-03-13 07:05:43 +000061 case 0: {
Chris Lattnera8058742007-11-18 02:57:27 +000062 // 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.
66
67 // If this is the end of an included file, pop the parent file off the
68 // include stack.
Chris Lattner1c8ae592009-03-13 16:01:53 +000069 TGLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
70 if (ParentIncludeLoc != TGLoc()) {
Chris Lattneraa739d22009-03-13 07:05:43 +000071 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
72 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
Chris Lattner1c8ae592009-03-13 16:01:53 +000073 CurPtr = ParentIncludeLoc.getPointer();
Chris Lattnera8058742007-11-18 02:57:27 +000074 return getNextChar();
75 }
76
77 // Otherwise, return end of file.
78 --CurPtr; // Another call to lex will return EOF again.
79 return EOF;
Chris Lattneraa739d22009-03-13 07:05:43 +000080 }
Chris Lattnera8058742007-11-18 02:57:27 +000081 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)
Chris Lattnerc1819182007-11-18 05:48:46 +000088 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnera8058742007-11-18 02:57:27 +000089 return '\n';
90 }
91}
92
Chris Lattnerf4601652007-11-22 20:49:04 +000093tgtok::TokKind TGLexer::LexToken() {
Chris Lattner56a9fcf2007-11-19 07:43:52 +000094 TokStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +000095 // This always consumes at least one character.
96 int CurChar = getNextChar();
97
98 switch (CurChar) {
99 default:
100 // Handle letters: [a-zA-Z_]
101 if (isalpha(CurChar) || CurChar == '_')
102 return LexIdentifier();
103
Chris Lattnerf4601652007-11-22 20:49:04 +0000104 // 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;
120
Chris Lattnera8058742007-11-18 02:57:27 +0000121 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())
Chris Lattnerf4601652007-11-22 20:49:04 +0000135 return tgtok::Error;
136 } else // Otherwise, this is an error.
137 return ReturnError(TokStart, "Unexpected character");
Chris Lattnera8058742007-11-18 02:57:27 +0000138 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();
147 }
148}
149
150/// LexString - Lex "[^"]*"
Chris Lattnerf4601652007-11-22 20:49:04 +0000151tgtok::TokKind TGLexer::LexString() {
Chris Lattnera8058742007-11-18 02:57:27 +0000152 const char *StrStart = CurPtr;
153
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000154 CurStrVal = "";
155
Chris Lattnera8058742007-11-18 02:57:27 +0000156 while (*CurPtr != '"') {
157 // If we hit the end of the buffer, report an error.
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000158 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
159 return ReturnError(StrStart, "End of file in string literal");
160
161 if (*CurPtr == '\n' || *CurPtr == '\r')
162 return ReturnError(StrStart, "End of line in string literal");
Chris Lattnera8058742007-11-18 02:57:27 +0000163
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000164 if (*CurPtr != '\\') {
165 CurStrVal += *CurPtr++;
166 continue;
167 }
168
Chris Lattnera8058742007-11-18 02:57:27 +0000169 ++CurPtr;
Chris Lattnerea9f4df2009-03-13 21:03:27 +0000170
171 switch (*CurPtr) {
172 case '\\': case '\'': case '"':
173 // These turn into their literal character.
174 CurStrVal += *CurPtr++;
175 break;
176 case '\n':
177 case '\r':
178 return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
179
180 // If we hit the end of the buffer, report an error.
181 case '\0':
182 if (CurPtr == CurBuf->getBufferEnd())
183 return ReturnError(StrStart, "End of file in string literal");
184 // FALL THROUGH
185 default:
186 return ReturnError(CurPtr, "invalid escape in string literal");
187 }
Chris Lattnera8058742007-11-18 02:57:27 +0000188 }
189
Chris Lattnera8058742007-11-18 02:57:27 +0000190 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000191 return tgtok::StrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000192}
193
Chris Lattnerf4601652007-11-22 20:49:04 +0000194tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnera8058742007-11-18 02:57:27 +0000195 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4601652007-11-22 20:49:04 +0000196 return ReturnError(TokStart, "Invalid variable name");
Chris Lattnera8058742007-11-18 02:57:27 +0000197
198 // Otherwise, we're ok, consume the rest of the characters.
199 const char *VarNameStart = CurPtr++;
200
201 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
202 ++CurPtr;
203
Chris Lattnerf4601652007-11-22 20:49:04 +0000204 CurStrVal.assign(VarNameStart, CurPtr);
205 return tgtok::VarName;
Chris Lattnera8058742007-11-18 02:57:27 +0000206}
207
208
Chris Lattnerf4601652007-11-22 20:49:04 +0000209tgtok::TokKind TGLexer::LexIdentifier() {
Chris Lattnera8058742007-11-18 02:57:27 +0000210 // The first letter is [a-zA-Z_].
Chris Lattnerf4601652007-11-22 20:49:04 +0000211 const char *IdentStart = TokStart;
Chris Lattnera8058742007-11-18 02:57:27 +0000212
213 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
214 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
215 ++CurPtr;
216
217 // Check to see if this identifier is a keyword.
218 unsigned Len = CurPtr-IdentStart;
219
Chris Lattnerf4601652007-11-22 20:49:04 +0000220 if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
221 if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
222 if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
223 if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
224 if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
225 if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
226 if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
Chris Lattnera8058742007-11-18 02:57:27 +0000227
Chris Lattnerf4601652007-11-22 20:49:04 +0000228 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
229 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
230 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
231 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
232 return tgtok::MultiClass;
233 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
234 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
235 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
Chris Lattnera8058742007-11-18 02:57:27 +0000236
237 if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000238 if (LexInclude()) return tgtok::Error;
239 return Lex();
Chris Lattnera8058742007-11-18 02:57:27 +0000240 }
241
Chris Lattnerf4601652007-11-22 20:49:04 +0000242 CurStrVal.assign(IdentStart, CurPtr);
243 return tgtok::Id;
Chris Lattnera8058742007-11-18 02:57:27 +0000244}
245
246/// LexInclude - We just read the "include" token. Get the string token that
247/// comes next and enter the include.
248bool TGLexer::LexInclude() {
249 // The token after the include must be a string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000250 tgtok::TokKind Tok = LexToken();
251 if (Tok == tgtok::Error) return true;
252 if (Tok != tgtok::StrVal) {
253 PrintError(getLoc(), "Expected filename after include");
Chris Lattnera8058742007-11-18 02:57:27 +0000254 return true;
255 }
256
257 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000258 std::string Filename = CurStrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000259
260 // Try to find the file.
Chris Lattner038112a2008-04-01 18:04:03 +0000261 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
Chris Lattnera8058742007-11-18 02:57:27 +0000262
263 // If the file didn't exist directly, see if it's in an include path.
264 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
265 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
Chris Lattner038112a2008-04-01 18:04:03 +0000266 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
Chris Lattnera8058742007-11-18 02:57:27 +0000267 }
268
269 if (NewBuf == 0) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000270 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000271 return true;
272 }
273
274 // Save the line number and lex buffer of the includer.
Chris Lattner1c8ae592009-03-13 16:01:53 +0000275 CurBuffer = SrcMgr.AddNewSourceBuffer(NewBuf, TGLoc::getFromPointer(CurPtr));
Chris Lattnera8058742007-11-18 02:57:27 +0000276
Chris Lattnera8058742007-11-18 02:57:27 +0000277 CurBuf = NewBuf;
278 CurPtr = CurBuf->getBufferStart();
279 return false;
280}
281
282void TGLexer::SkipBCPLComment() {
283 ++CurPtr; // skip the second slash.
284 while (1) {
285 switch (*CurPtr) {
286 case '\n':
287 case '\r':
288 return; // Newline is end of comment.
289 case 0:
290 // If this is the end of the buffer, end the comment.
291 if (CurPtr == CurBuf->getBufferEnd())
292 return;
293 break;
294 }
295 // Otherwise, skip the character.
296 ++CurPtr;
297 }
298}
299
300/// SkipCComment - This skips C-style /**/ comments. The only difference from C
301/// is that we allow nesting.
302bool TGLexer::SkipCComment() {
303 ++CurPtr; // skip the star.
304 unsigned CommentDepth = 1;
305
306 while (1) {
307 int CurChar = getNextChar();
308 switch (CurChar) {
309 case EOF:
Chris Lattnerf4601652007-11-22 20:49:04 +0000310 PrintError(TokStart, "Unterminated comment!");
Chris Lattnera8058742007-11-18 02:57:27 +0000311 return true;
312 case '*':
313 // End of the comment?
314 if (CurPtr[0] != '/') break;
315
316 ++CurPtr; // End the */.
317 if (--CommentDepth == 0)
318 return false;
319 break;
320 case '/':
321 // Start of a nested comment?
322 if (CurPtr[0] != '*') break;
323 ++CurPtr;
324 ++CommentDepth;
325 break;
326 }
327 }
328}
329
330/// LexNumber - Lex:
331/// [-+]?[0-9]+
332/// 0x[0-9a-fA-F]+
333/// 0b[01]+
Chris Lattnerf4601652007-11-22 20:49:04 +0000334tgtok::TokKind TGLexer::LexNumber() {
Chris Lattnera8058742007-11-18 02:57:27 +0000335 if (CurPtr[-1] == '0') {
336 if (CurPtr[0] == 'x') {
337 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000338 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000339 while (isxdigit(CurPtr[0]))
340 ++CurPtr;
341
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000342 // Requires at least one hex digit.
343 if (CurPtr == NumStart)
344 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
345
Dan Gohman63f97202008-10-17 01:33:43 +0000346 errno = 0;
Chris Lattnerf4601652007-11-22 20:49:04 +0000347 CurIntVal = strtoll(NumStart, 0, 16);
Dan Gohman63f97202008-10-17 01:33:43 +0000348 if (errno == EINVAL)
349 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
350 if (errno == ERANGE) {
351 errno = 0;
352 CurIntVal = (int64_t)strtoull(NumStart, 0, 16);
353 if (errno == EINVAL)
354 return ReturnError(CurPtr-2, "Invalid hexadecimal number");
355 if (errno == ERANGE)
356 return ReturnError(CurPtr-2, "Hexadecimal number out of range");
357 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000358 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000359 } else if (CurPtr[0] == 'b') {
360 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000361 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000362 while (CurPtr[0] == '0' || CurPtr[0] == '1')
363 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000364
365 // Requires at least one binary digit.
366 if (CurPtr == NumStart)
367 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000368 CurIntVal = strtoll(NumStart, 0, 2);
369 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000370 }
371 }
372
373 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000374 if (!isdigit(CurPtr[0])) {
375 if (CurPtr[-1] == '-')
376 return tgtok::minus;
377 else if (CurPtr[-1] == '+')
378 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000379 }
380
381 while (isdigit(CurPtr[0]))
382 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000383 CurIntVal = strtoll(TokStart, 0, 10);
384 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000385}
386
387/// LexBracket - We just read '['. If this is a code block, return it,
388/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000389tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000390 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000391 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000392 ++CurPtr;
393 const char *CodeStart = CurPtr;
394 while (1) {
395 int Char = getNextChar();
396 if (Char == EOF) break;
397
398 if (Char != '}') continue;
399
400 Char = getNextChar();
401 if (Char == EOF) break;
402 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000403 CurStrVal.assign(CodeStart, CurPtr-2);
404 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000405 }
406 }
407
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000408 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000409}
410
411/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000412tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000413 if (!isalpha(*CurPtr))
Chris Lattnerf4601652007-11-22 20:49:04 +0000414 return ReturnError(CurPtr-1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000415
416 const char *Start = CurPtr++;
417 while (isalpha(*CurPtr))
418 ++CurPtr;
419
420 // Check to see which operator this is.
421 unsigned Len = CurPtr-Start;
422
Chris Lattnerf4601652007-11-22 20:49:04 +0000423 if (Len == 3 && !memcmp(Start, "con", 3)) return tgtok::XConcat;
424 if (Len == 3 && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
425 if (Len == 3 && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
426 if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
427 if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
Chris Lattnera8058742007-11-18 02:57:27 +0000428
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000429 return ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000430}
431