blob: e7465de28a4d41059b84168ba94b2f60c794460c [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 Lattnera8058742007-11-18 02:57:27 +000015#include "llvm/Support/Streams.h"
Chris Lattnera8058742007-11-18 02:57:27 +000016#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000017#include <ostream>
Chuck Rose III8b0ec642007-11-21 19:36:25 +000018#include "llvm/Config/config.h"
Chris Lattnera8058742007-11-18 02:57:27 +000019#include <cctype>
Duncan Sands4520dd22008-10-08 07:23:46 +000020#include <cstdio>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000021#include <cstdlib>
22#include <cstring>
Chris Lattnera8058742007-11-18 02:57:27 +000023using namespace llvm;
24
Chris Lattnera8058742007-11-18 02:57:27 +000025TGLexer::TGLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
26 CurPtr = CurBuf->getBufferStart();
Chris Lattner56a9fcf2007-11-19 07:43:52 +000027 TokStart = 0;
Chris Lattnera8058742007-11-18 02:57:27 +000028}
29
30TGLexer::~TGLexer() {
31 while (!IncludeStack.empty()) {
32 delete IncludeStack.back().Buffer;
33 IncludeStack.pop_back();
34 }
35 delete CurBuf;
36}
37
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000038/// ReturnError - Set the error to the specified string at the specified
Chris Lattnerf4601652007-11-22 20:49:04 +000039/// location. This is defined to always return tgtok::Error.
40tgtok::TokKind TGLexer::ReturnError(const char *Loc, const std::string &Msg) {
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000041 PrintError(Loc, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000042 return tgtok::Error;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000043}
Chris Lattnera8058742007-11-18 02:57:27 +000044
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000045void TGLexer::PrintIncludeStack(std::ostream &OS) const {
Chris Lattnera8058742007-11-18 02:57:27 +000046 for (unsigned i = 0, e = IncludeStack.size(); i != e; ++i)
47 OS << "Included from " << IncludeStack[i].Buffer->getBufferIdentifier()
48 << ":" << IncludeStack[i].LineNo << ":\n";
49 OS << "Parsing " << CurBuf->getBufferIdentifier() << ":"
50 << CurLineNo << ": ";
51}
52
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000053/// PrintError - Print the error at the specified location.
54void TGLexer::PrintError(const char *ErrorLoc, const std::string &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000055 PrintIncludeStack(*cerr.stream());
56 cerr << Msg << "\n";
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000057 assert(ErrorLoc && "Location not specified!");
58
59 // Scan backward to find the start of the line.
60 const char *LineStart = ErrorLoc;
61 while (LineStart != CurBuf->getBufferStart() &&
62 LineStart[-1] != '\n' && LineStart[-1] != '\r')
63 --LineStart;
64 // Get the end of the line.
65 const char *LineEnd = ErrorLoc;
66 while (LineEnd != CurBuf->getBufferEnd() &&
67 LineEnd[0] != '\n' && LineEnd[0] != '\r')
68 ++LineEnd;
69 // Print out the line.
70 cerr << std::string(LineStart, LineEnd) << "\n";
71 // Print out spaces before the carat.
Chris Lattner56a9fcf2007-11-19 07:43:52 +000072 for (const char *Pos = LineStart; Pos != ErrorLoc; ++Pos)
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +000073 cerr << (*Pos == '\t' ? '\t' : ' ');
74 cerr << "^\n";
75}
76
Chris Lattnera8058742007-11-18 02:57:27 +000077int TGLexer::getNextChar() {
78 char CurChar = *CurPtr++;
79 switch (CurChar) {
80 default:
Chris Lattnerc1819182007-11-18 05:48:46 +000081 return (unsigned char)CurChar;
Chris Lattnera8058742007-11-18 02:57:27 +000082 case 0:
83 // A nul character in the stream is either the end of the current buffer or
84 // a random nul in the file. Disambiguate that here.
85 if (CurPtr-1 != CurBuf->getBufferEnd())
86 return 0; // Just whitespace.
87
88 // If this is the end of an included file, pop the parent file off the
89 // include stack.
90 if (!IncludeStack.empty()) {
91 delete CurBuf;
92 CurBuf = IncludeStack.back().Buffer;
93 CurLineNo = IncludeStack.back().LineNo;
94 CurPtr = IncludeStack.back().CurPtr;
95 IncludeStack.pop_back();
96 return getNextChar();
97 }
98
99 // Otherwise, return end of file.
100 --CurPtr; // Another call to lex will return EOF again.
101 return EOF;
102 case '\n':
103 case '\r':
104 // Handle the newline character by ignoring it and incrementing the line
105 // count. However, be careful about 'dos style' files with \n\r in them.
106 // Only treat a \n\r or \r\n as a single line.
107 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
108 *CurPtr != CurChar)
Chris Lattnerc1819182007-11-18 05:48:46 +0000109 ++CurPtr; // Eat the two char newline sequence.
Chris Lattnera8058742007-11-18 02:57:27 +0000110
111 ++CurLineNo;
112 return '\n';
113 }
114}
115
Chris Lattnerf4601652007-11-22 20:49:04 +0000116tgtok::TokKind TGLexer::LexToken() {
Chris Lattner56a9fcf2007-11-19 07:43:52 +0000117 TokStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000118 // This always consumes at least one character.
119 int CurChar = getNextChar();
120
121 switch (CurChar) {
122 default:
123 // Handle letters: [a-zA-Z_]
124 if (isalpha(CurChar) || CurChar == '_')
125 return LexIdentifier();
126
Chris Lattnerf4601652007-11-22 20:49:04 +0000127 // Unknown character, emit an error.
128 return ReturnError(TokStart, "Unexpected character");
129 case EOF: return tgtok::Eof;
130 case ':': return tgtok::colon;
131 case ';': return tgtok::semi;
132 case '.': return tgtok::period;
133 case ',': return tgtok::comma;
134 case '<': return tgtok::less;
135 case '>': return tgtok::greater;
136 case ']': return tgtok::r_square;
137 case '{': return tgtok::l_brace;
138 case '}': return tgtok::r_brace;
139 case '(': return tgtok::l_paren;
140 case ')': return tgtok::r_paren;
141 case '=': return tgtok::equal;
142 case '?': return tgtok::question;
143
Chris Lattnera8058742007-11-18 02:57:27 +0000144 case 0:
145 case ' ':
146 case '\t':
147 case '\n':
148 case '\r':
149 // Ignore whitespace.
150 return LexToken();
151 case '/':
152 // If this is the start of a // comment, skip until the end of the line or
153 // the end of the buffer.
154 if (*CurPtr == '/')
155 SkipBCPLComment();
156 else if (*CurPtr == '*') {
157 if (SkipCComment())
Chris Lattnerf4601652007-11-22 20:49:04 +0000158 return tgtok::Error;
159 } else // Otherwise, this is an error.
160 return ReturnError(TokStart, "Unexpected character");
Chris Lattnera8058742007-11-18 02:57:27 +0000161 return LexToken();
162 case '-': case '+':
163 case '0': case '1': case '2': case '3': case '4': case '5': case '6':
164 case '7': case '8': case '9':
165 return LexNumber();
166 case '"': return LexString();
167 case '$': return LexVarName();
168 case '[': return LexBracket();
169 case '!': return LexExclaim();
170 }
171}
172
173/// LexString - Lex "[^"]*"
Chris Lattnerf4601652007-11-22 20:49:04 +0000174tgtok::TokKind TGLexer::LexString() {
Chris Lattnera8058742007-11-18 02:57:27 +0000175 const char *StrStart = CurPtr;
176
177 while (*CurPtr != '"') {
178 // If we hit the end of the buffer, report an error.
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000179 if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
180 return ReturnError(StrStart, "End of file in string literal");
181
182 if (*CurPtr == '\n' || *CurPtr == '\r')
183 return ReturnError(StrStart, "End of line in string literal");
Chris Lattnera8058742007-11-18 02:57:27 +0000184
185 ++CurPtr;
186 }
187
Chris Lattnerf4601652007-11-22 20:49:04 +0000188 CurStrVal.assign(StrStart, CurPtr);
Chris Lattnera8058742007-11-18 02:57:27 +0000189 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000190 return tgtok::StrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000191}
192
Chris Lattnerf4601652007-11-22 20:49:04 +0000193tgtok::TokKind TGLexer::LexVarName() {
Chris Lattnera8058742007-11-18 02:57:27 +0000194 if (!isalpha(CurPtr[0]) && CurPtr[0] != '_')
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 return ReturnError(TokStart, "Invalid variable name");
Chris Lattnera8058742007-11-18 02:57:27 +0000196
197 // Otherwise, we're ok, consume the rest of the characters.
198 const char *VarNameStart = CurPtr++;
199
200 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
201 ++CurPtr;
202
Chris Lattnerf4601652007-11-22 20:49:04 +0000203 CurStrVal.assign(VarNameStart, CurPtr);
204 return tgtok::VarName;
Chris Lattnera8058742007-11-18 02:57:27 +0000205}
206
207
Chris Lattnerf4601652007-11-22 20:49:04 +0000208tgtok::TokKind TGLexer::LexIdentifier() {
Chris Lattnera8058742007-11-18 02:57:27 +0000209 // The first letter is [a-zA-Z_].
Chris Lattnerf4601652007-11-22 20:49:04 +0000210 const char *IdentStart = TokStart;
Chris Lattnera8058742007-11-18 02:57:27 +0000211
212 // Match the rest of the identifier regex: [0-9a-zA-Z_]*
213 while (isalpha(*CurPtr) || isdigit(*CurPtr) || *CurPtr == '_')
214 ++CurPtr;
215
216 // Check to see if this identifier is a keyword.
217 unsigned Len = CurPtr-IdentStart;
218
Chris Lattnerf4601652007-11-22 20:49:04 +0000219 if (Len == 3 && !memcmp(IdentStart, "int", 3)) return tgtok::Int;
220 if (Len == 3 && !memcmp(IdentStart, "bit", 3)) return tgtok::Bit;
221 if (Len == 4 && !memcmp(IdentStart, "bits", 4)) return tgtok::Bits;
222 if (Len == 6 && !memcmp(IdentStart, "string", 6)) return tgtok::String;
223 if (Len == 4 && !memcmp(IdentStart, "list", 4)) return tgtok::List;
224 if (Len == 4 && !memcmp(IdentStart, "code", 4)) return tgtok::Code;
225 if (Len == 3 && !memcmp(IdentStart, "dag", 3)) return tgtok::Dag;
Chris Lattnera8058742007-11-18 02:57:27 +0000226
Chris Lattnerf4601652007-11-22 20:49:04 +0000227 if (Len == 5 && !memcmp(IdentStart, "class", 5)) return tgtok::Class;
228 if (Len == 3 && !memcmp(IdentStart, "def", 3)) return tgtok::Def;
229 if (Len == 4 && !memcmp(IdentStart, "defm", 4)) return tgtok::Defm;
230 if (Len == 10 && !memcmp(IdentStart, "multiclass", 10))
231 return tgtok::MultiClass;
232 if (Len == 5 && !memcmp(IdentStart, "field", 5)) return tgtok::Field;
233 if (Len == 3 && !memcmp(IdentStart, "let", 3)) return tgtok::Let;
234 if (Len == 2 && !memcmp(IdentStart, "in", 2)) return tgtok::In;
Chris Lattnera8058742007-11-18 02:57:27 +0000235
236 if (Len == 7 && !memcmp(IdentStart, "include", 7)) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000237 if (LexInclude()) return tgtok::Error;
238 return Lex();
Chris Lattnera8058742007-11-18 02:57:27 +0000239 }
240
Chris Lattnerf4601652007-11-22 20:49:04 +0000241 CurStrVal.assign(IdentStart, CurPtr);
242 return tgtok::Id;
Chris Lattnera8058742007-11-18 02:57:27 +0000243}
244
245/// LexInclude - We just read the "include" token. Get the string token that
246/// comes next and enter the include.
247bool TGLexer::LexInclude() {
248 // The token after the include must be a string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000249 tgtok::TokKind Tok = LexToken();
250 if (Tok == tgtok::Error) return true;
251 if (Tok != tgtok::StrVal) {
252 PrintError(getLoc(), "Expected filename after include");
Chris Lattnera8058742007-11-18 02:57:27 +0000253 return true;
254 }
255
256 // Get the string.
Chris Lattnerf4601652007-11-22 20:49:04 +0000257 std::string Filename = CurStrVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000258
259 // Try to find the file.
Chris Lattner038112a2008-04-01 18:04:03 +0000260 MemoryBuffer *NewBuf = MemoryBuffer::getFile(Filename.c_str());
Chris Lattnera8058742007-11-18 02:57:27 +0000261
262 // If the file didn't exist directly, see if it's in an include path.
263 for (unsigned i = 0, e = IncludeDirectories.size(); i != e && !NewBuf; ++i) {
264 std::string IncFile = IncludeDirectories[i] + "/" + Filename;
Chris Lattner038112a2008-04-01 18:04:03 +0000265 NewBuf = MemoryBuffer::getFile(IncFile.c_str());
Chris Lattnera8058742007-11-18 02:57:27 +0000266 }
267
268 if (NewBuf == 0) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000269 PrintError(getLoc(), "Could not find include file '" + Filename + "'");
Chris Lattnera8058742007-11-18 02:57:27 +0000270 return true;
271 }
272
273 // Save the line number and lex buffer of the includer.
274 IncludeStack.push_back(IncludeRec(CurBuf, CurPtr, CurLineNo));
275
276 CurLineNo = 1; // Reset line numbering.
277 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
Chris Lattnerf4601652007-11-22 20:49:04 +0000346 CurIntVal = strtoll(NumStart, 0, 16);
347 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000348 } else if (CurPtr[0] == 'b') {
349 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000350 const char *NumStart = CurPtr;
Chris Lattnera8058742007-11-18 02:57:27 +0000351 while (CurPtr[0] == '0' || CurPtr[0] == '1')
352 ++CurPtr;
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000353
354 // Requires at least one binary digit.
355 if (CurPtr == NumStart)
356 return ReturnError(CurPtr-2, "Invalid binary number");
Chris Lattnerf4601652007-11-22 20:49:04 +0000357 CurIntVal = strtoll(NumStart, 0, 2);
358 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000359 }
360 }
361
362 // Check for a sign without a digit.
Chris Lattnerf4601652007-11-22 20:49:04 +0000363 if (!isdigit(CurPtr[0])) {
364 if (CurPtr[-1] == '-')
365 return tgtok::minus;
366 else if (CurPtr[-1] == '+')
367 return tgtok::plus;
Chris Lattnera8058742007-11-18 02:57:27 +0000368 }
369
370 while (isdigit(CurPtr[0]))
371 ++CurPtr;
Chris Lattnerf4601652007-11-22 20:49:04 +0000372 CurIntVal = strtoll(TokStart, 0, 10);
373 return tgtok::IntVal;
Chris Lattnera8058742007-11-18 02:57:27 +0000374}
375
376/// LexBracket - We just read '['. If this is a code block, return it,
377/// otherwise return the bracket. Match: '[' and '[{ ( [^}]+ | }[^]] )* }]'
Chris Lattnerf4601652007-11-22 20:49:04 +0000378tgtok::TokKind TGLexer::LexBracket() {
Chris Lattnera8058742007-11-18 02:57:27 +0000379 if (CurPtr[0] != '{')
Chris Lattnerf4601652007-11-22 20:49:04 +0000380 return tgtok::l_square;
Chris Lattnera8058742007-11-18 02:57:27 +0000381 ++CurPtr;
382 const char *CodeStart = CurPtr;
383 while (1) {
384 int Char = getNextChar();
385 if (Char == EOF) break;
386
387 if (Char != '}') continue;
388
389 Char = getNextChar();
390 if (Char == EOF) break;
391 if (Char == ']') {
Chris Lattnerf4601652007-11-22 20:49:04 +0000392 CurStrVal.assign(CodeStart, CurPtr-2);
393 return tgtok::CodeFragment;
Chris Lattnera8058742007-11-18 02:57:27 +0000394 }
395 }
396
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000397 return ReturnError(CodeStart-2, "Unterminated Code Block");
Chris Lattnera8058742007-11-18 02:57:27 +0000398}
399
400/// LexExclaim - Lex '!' and '![a-zA-Z]+'.
Chris Lattnerf4601652007-11-22 20:49:04 +0000401tgtok::TokKind TGLexer::LexExclaim() {
Chris Lattnera8058742007-11-18 02:57:27 +0000402 if (!isalpha(*CurPtr))
Chris Lattnerf4601652007-11-22 20:49:04 +0000403 return ReturnError(CurPtr-1, "Invalid \"!operator\"");
Chris Lattnera8058742007-11-18 02:57:27 +0000404
405 const char *Start = CurPtr++;
406 while (isalpha(*CurPtr))
407 ++CurPtr;
408
409 // Check to see which operator this is.
410 unsigned Len = CurPtr-Start;
411
Chris Lattnerf4601652007-11-22 20:49:04 +0000412 if (Len == 3 && !memcmp(Start, "con", 3)) return tgtok::XConcat;
413 if (Len == 3 && !memcmp(Start, "sra", 3)) return tgtok::XSRA;
414 if (Len == 3 && !memcmp(Start, "srl", 3)) return tgtok::XSRL;
415 if (Len == 3 && !memcmp(Start, "shl", 3)) return tgtok::XSHL;
416 if (Len == 9 && !memcmp(Start, "strconcat", 9)) return tgtok::XStrConcat;
Chris Lattnera8058742007-11-18 02:57:27 +0000417
Chris Lattnerc8a9bbc2007-11-19 07:38:58 +0000418 return ReturnError(Start-1, "Unknown operator");
Chris Lattnera8058742007-11-18 02:57:27 +0000419}
420