blob: cc6825f5e07e1c29546cb2d5bc2517499656fb11 [file] [log] [blame]
Misha Brukmanf2f5b392004-02-12 00:03:08 +00001/*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
John Criswellaefb6662003-10-21 15:29:18 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +00009//
Chris Lattner13854f22003-08-04 20:08:28 +000010// This file defines a simple flex scanner for TableGen files. This is pretty
11// straight-forward, except for the magic to handle file inclusion.
Chris Lattnere62c1182002-12-02 01:23:04 +000012//
Misha Brukmanf2f5b392004-02-12 00:03:08 +000013//===----------------------------------------------------------------------===*/
Chris Lattnere62c1182002-12-02 01:23:04 +000014
15%option prefix="File"
16%option yylineno
17%option nostdinit
18%option never-interactive
19%option batch
Chris Lattnere62c1182002-12-02 01:23:04 +000020%option nodefault
21%option 8bit
22%option outfile="Lexer.cpp"
23%option ecs
24%option noreject
25%option noyymore
26
Chris Lattnerd33b8db2003-07-30 19:39:36 +000027%x comment
Chris Lattnere62c1182002-12-02 01:23:04 +000028
29%{
Jeff Cohend41b30d2006-11-05 19:31:28 +000030#include "llvm/Config/config.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000031#include "llvm/Support/Streams.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000032#include "Record.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000033typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
Chris Lattnere62c1182002-12-02 01:23:04 +000034#include "FileParser.h"
35
Brian Gaeked0fde302003-11-11 22:41:34 +000036int Fileparse();
37
38namespace llvm {
39
John Criswell96b4bed2003-08-27 13:41:57 +000040// Global variable recording the location of the include directory
Chris Lattnerd9f5d902006-03-03 01:47:14 +000041std::vector<std::string> IncludeDirectories;
John Criswell96b4bed2003-08-27 13:41:57 +000042
Misha Brukman1700f772004-02-12 00:00:46 +000043/// ParseInt - This has to handle the special case of binary numbers 0b0101
44///
Chris Lattnere62c1182002-12-02 01:23:04 +000045static int ParseInt(const char *Str) {
46 if (Str[0] == '0' && Str[1] == 'b')
Chris Lattnera4503de2006-09-18 22:28:27 +000047 return strtoll(Str+2, 0, 2);
48 return strtoll(Str, 0, 0);
Chris Lattnere62c1182002-12-02 01:23:04 +000049}
50
Chris Lattnerd33b8db2003-07-30 19:39:36 +000051static int CommentDepth = 0;
52
Chris Lattner7dff0532003-07-30 20:56:47 +000053struct IncludeRec {
54 std::string Filename;
55 FILE *File;
56 unsigned LineNo;
57 YY_BUFFER_STATE Buffer;
58
59 IncludeRec(const std::string &FN, FILE *F)
60 : Filename(FN), File(F), LineNo(0){
61 }
62};
63
64static std::vector<IncludeRec> IncludeStack;
65
Chris Lattner7dff0532003-07-30 20:56:47 +000066std::ostream &err() {
Bill Wendlingf5da1332006-12-07 22:21:48 +000067 if (IncludeStack.empty()) {
68 cerr << "At end of input: ";
69 return *cerr.stream();
70 }
Chris Lattner7dff0532003-07-30 20:56:47 +000071
72 for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
Bill Wendlingf5da1332006-12-07 22:21:48 +000073 cerr << "Included from " << IncludeStack[i].Filename << ":"
74 << IncludeStack[i].LineNo << ":\n";
75 cerr << "Parsing " << IncludeStack.back().Filename << ":"
76 << Filelineno << ": ";
77 return *cerr.stream();
Chris Lattner7dff0532003-07-30 20:56:47 +000078}
79
Misha Brukman1700f772004-02-12 00:00:46 +000080/// ParseFile - this function begins the parsing of the specified tablegen file.
81///
Chris Lattnerd9f5d902006-03-03 01:47:14 +000082void ParseFile(const std::string &Filename,
83 const std::vector<std::string> &IncludeDirs) {
Chris Lattnere623fe32003-07-30 19:55:10 +000084 FILE *F = stdin;
85 if (Filename != "-") {
86 F = fopen(Filename.c_str(), "r");
87
88 if (F == 0) {
Bill Wendlingf5da1332006-12-07 22:21:48 +000089 cerr << "Could not open input file '" + Filename + "'!\n";
John Criswell2d930032003-09-09 14:37:48 +000090 exit (1);
Chris Lattnere623fe32003-07-30 19:55:10 +000091 }
Chris Lattner7dff0532003-07-30 20:56:47 +000092 IncludeStack.push_back(IncludeRec(Filename, F));
93 } else {
94 IncludeStack.push_back(IncludeRec("<stdin>", stdin));
Chris Lattnere623fe32003-07-30 19:55:10 +000095 }
96
John Criswell96b4bed2003-08-27 13:41:57 +000097 // Record the location of the include directory so that the lexer can find
98 // it later.
Chris Lattnerd9f5d902006-03-03 01:47:14 +000099 IncludeDirectories = IncludeDirs;
John Criswell96b4bed2003-08-27 13:41:57 +0000100
Chris Lattnere623fe32003-07-30 19:55:10 +0000101 Filein = F;
102 Filelineno = 1;
103 Fileparse();
Chris Lattnere623fe32003-07-30 19:55:10 +0000104 Filein = stdin;
105}
106
Misha Brukman1700f772004-02-12 00:00:46 +0000107/// HandleInclude - This function is called when an include directive is
108/// encountered in the input stream...
109///
Chris Lattner7dff0532003-07-30 20:56:47 +0000110static void HandleInclude(const char *Buffer) {
111 unsigned Length = yyleng;
112 assert(Buffer[Length-1] == '"');
113 Buffer += strlen("include ");
114 Length -= strlen("include ");
115 while (*Buffer != '"') {
116 ++Buffer;
117 --Length;
118 }
119 assert(Length >= 2 && "Double quotes not found?");
120 std::string Filename(Buffer+1, Buffer+Length-1);
Bill Wendlingf5da1332006-12-07 22:21:48 +0000121 //cerr << "Filename = '" << Filename << "'\n";
Chris Lattner7dff0532003-07-30 20:56:47 +0000122
123 // Save the line number and lex buffer of the includer...
124 IncludeStack.back().LineNo = Filelineno;
125 IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
126
127 // Open the new input file...
128 yyin = fopen(Filename.c_str(), "r");
129 if (yyin == 0) {
John Criswell96b4bed2003-08-27 13:41:57 +0000130 // If we couldn't find the file in the current directory, look for it in
131 // the include directories.
132 //
Chris Lattnerd9f5d902006-03-03 01:47:14 +0000133 std::string NextFilename;
134 for (unsigned i = 0, e = IncludeDirectories.size(); i != e; ++i) {
135 NextFilename = IncludeDirectories[i] + "/" + Filename;
Chris Lattner56879b22006-03-03 19:34:28 +0000136 if ((yyin = fopen(NextFilename.c_str(), "r")))
Chris Lattnerd9f5d902006-03-03 01:47:14 +0000137 break;
138 }
139
John Criswell96b4bed2003-08-27 13:41:57 +0000140 if (yyin == 0) {
141 err() << "Could not find include file '" << Filename << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000142 exit(1);
John Criswell96b4bed2003-08-27 13:41:57 +0000143 }
Chris Lattner83fe9172004-02-13 16:33:56 +0000144 Filename = NextFilename;
Chris Lattner7dff0532003-07-30 20:56:47 +0000145 }
146
147 // Add the file to our include stack...
148 IncludeStack.push_back(IncludeRec(Filename, yyin));
149 Filelineno = 1; // Reset line numbering...
150 //yyrestart(yyin); // Start lexing the new file...
151
152 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
153}
154
Misha Brukman1700f772004-02-12 00:00:46 +0000155/// yywrap - This is called when the lexer runs out of input in one of the
156/// files. Switch back to an includer if an includee has run out of input.
157///
Chris Lattner7dff0532003-07-30 20:56:47 +0000158extern "C"
Chris Lattner45237092004-10-13 15:25:46 +0000159int yywrap(void) {
Chris Lattner7dff0532003-07-30 20:56:47 +0000160 if (IncludeStack.back().File != stdin)
161 fclose(IncludeStack.back().File);
162 IncludeStack.pop_back();
163 if (IncludeStack.empty()) return 1; // Top-level file is done.
164
165 // Otherwise, we need to switch back to a file which included the current one.
166 Filelineno = IncludeStack.back().LineNo; // Restore current line number
167 yy_switch_to_buffer(IncludeStack.back().Buffer);
168 return 0;
169}
170
Brian Gaeked0fde302003-11-11 22:41:34 +0000171} // End llvm namespace
172
173using namespace llvm;
174
Chris Lattnere62c1182002-12-02 01:23:04 +0000175%}
176
Chris Lattnere3a1d052003-07-30 22:15:58 +0000177Comment \/\/.*
Chris Lattnere62c1182002-12-02 01:23:04 +0000178
Jim Laskey81a21ea2007-02-06 18:19:44 +0000179Identifier [a-zA-Z_][0-9a-zA-Z_]*
Chris Lattnere3a1d052003-07-30 22:15:58 +0000180Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
181CodeFragment \[\{([^}]+|\}[^\]])*\}\]
182StringVal \"[^"]*\"
183IncludeStr include[ \t\n]+\"[^"]*\"
Chris Lattnere62c1182002-12-02 01:23:04 +0000184
185%%
186
187{Comment} { /* Ignore comments */ }
188
Chris Lattner7dff0532003-07-30 20:56:47 +0000189{IncludeStr} { HandleInclude(yytext); }
Chris Lattnere3a1d052003-07-30 22:15:58 +0000190{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
191 return CODEFRAGMENT; }
Chris Lattner7dff0532003-07-30 20:56:47 +0000192
Chris Lattnere62c1182002-12-02 01:23:04 +0000193int { return INT; }
194bit { return BIT; }
195bits { return BITS; }
196string { return STRING; }
197list { return LIST; }
Chris Lattnerf05760d2003-07-30 21:47:42 +0000198code { return CODE; }
Chris Lattner40f71132003-08-04 04:50:57 +0000199dag { return DAG; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000200
201class { return CLASS; }
202def { return DEF; }
Chris Lattner12069862006-09-01 21:13:49 +0000203defm { return DEFM; }
204multiclass { return MULTICLASS; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000205field { return FIELD; }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000206let { return LET; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000207in { return IN; }
208
Chris Lattnerb9266f82005-04-19 01:11:03 +0000209!sra { return SRATOK; }
210!srl { return SRLTOK; }
211!shl { return SHLTOK; }
Chris Lattner711e5d92006-03-31 21:53:49 +0000212!strconcat { return STRCONCATTOK; }
Chris Lattnerb9266f82005-04-19 01:11:03 +0000213
214
Chris Lattnere62c1182002-12-02 01:23:04 +0000215{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
216 return ID; }
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000217${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
218 return VARNAME; }
Chris Lattnere62c1182002-12-02 01:23:04 +0000219
220{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
221 return STRVAL; }
222
223{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
224
Chris Lattner36c57572004-05-27 17:44:18 +0000225[ \t\n\r]+ { /* Ignore whitespace */ }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000226
227
228"/*" { BEGIN(comment); CommentDepth++; }
Chris Lattner99783322005-09-06 21:23:09 +0000229<comment>[^*/]* {} /* eat anything that's not a '*' or '/' */
230<comment>"*"+[^*/]* {} /* eat up '*'s not followed by '/'s */
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000231<comment>"/*" { ++CommentDepth; }
Chris Lattner99783322005-09-06 21:23:09 +0000232<comment>"/"+[^*/]* {} /* eat up /'s not followed by *'s */
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000233<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
Chris Lattnerf5761a52004-02-13 16:37:43 +0000234<comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); }
Chris Lattner7dff0532003-07-30 20:56:47 +0000235
236. { return Filetext[0]; }
Chris Lattnerd33b8db2003-07-30 19:39:36 +0000237
Chris Lattnere62c1182002-12-02 01:23:04 +0000238%%
Chris Lattnere8242b12006-02-14 05:13:13 +0000239