2004-02-12 01:03:08 +01:00
|
|
|
/*===-- FileLexer.l - Scanner for TableGen Files ----------------*- C++ -*-===//
|
2003-10-21 17:29:18 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-05 21:27:59 +02:00
|
|
|
//
|
|
|
|
// This file defines a simple flex scanner for TableGen files. This is pretty
|
|
|
|
// straight-forward, except for the magic to handle file inclusion.
|
|
|
|
//
|
2004-02-12 01:03:08 +01:00
|
|
|
//===----------------------------------------------------------------------===*/
|
2003-10-05 21:27:59 +02:00
|
|
|
|
|
|
|
%option prefix="File"
|
|
|
|
%option yylineno
|
|
|
|
%option nostdinit
|
|
|
|
%option never-interactive
|
|
|
|
%option batch
|
|
|
|
%option nodefault
|
|
|
|
%option 8bit
|
|
|
|
%option outfile="Lexer.cpp"
|
|
|
|
%option ecs
|
|
|
|
%option noreject
|
|
|
|
%option noyymore
|
|
|
|
|
|
|
|
%x comment
|
|
|
|
|
|
|
|
%{
|
|
|
|
#include "Record.h"
|
2003-11-11 23:41:34 +01:00
|
|
|
typedef std::pair<llvm::Record*, std::vector<llvm::Init*>*> SubClassRefTy;
|
2003-10-05 21:27:59 +02:00
|
|
|
#include "FileParser.h"
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
int Fileparse();
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-05 21:27:59 +02:00
|
|
|
// Global variable recording the location of the include directory
|
|
|
|
std::string IncludeDirectory;
|
|
|
|
|
2004-02-12 01:00:46 +01:00
|
|
|
/// ParseInt - This has to handle the special case of binary numbers 0b0101
|
|
|
|
///
|
2003-10-05 21:27:59 +02:00
|
|
|
static int ParseInt(const char *Str) {
|
|
|
|
if (Str[0] == '0' && Str[1] == 'b')
|
|
|
|
return strtol(Str+2, 0, 2);
|
|
|
|
return strtol(Str, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CommentDepth = 0;
|
|
|
|
|
|
|
|
struct IncludeRec {
|
|
|
|
std::string Filename;
|
|
|
|
FILE *File;
|
|
|
|
unsigned LineNo;
|
|
|
|
YY_BUFFER_STATE Buffer;
|
|
|
|
|
|
|
|
IncludeRec(const std::string &FN, FILE *F)
|
|
|
|
: Filename(FN), File(F), LineNo(0){
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<IncludeRec> IncludeStack;
|
|
|
|
|
|
|
|
std::ostream &err() {
|
|
|
|
if (IncludeStack.empty())
|
|
|
|
return std::cerr << "At end of input: ";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = IncludeStack.size()-1; i != e; ++i)
|
|
|
|
std::cerr << "Included from " << IncludeStack[i].Filename << ":"
|
|
|
|
<< IncludeStack[i].LineNo << ":\n";
|
|
|
|
return std::cerr << "Parsing " << IncludeStack.back().Filename << ":"
|
|
|
|
<< Filelineno << ": ";
|
|
|
|
}
|
|
|
|
|
2004-02-12 01:00:46 +01:00
|
|
|
/// ParseFile - this function begins the parsing of the specified tablegen file.
|
|
|
|
///
|
2003-10-05 21:27:59 +02:00
|
|
|
void ParseFile(const std::string &Filename, const std::string & IncludeDir) {
|
|
|
|
FILE *F = stdin;
|
|
|
|
if (Filename != "-") {
|
|
|
|
F = fopen(Filename.c_str(), "r");
|
|
|
|
|
|
|
|
if (F == 0) {
|
|
|
|
std::cerr << "Could not open input file '" + Filename + "'!\n";
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
IncludeStack.push_back(IncludeRec(Filename, F));
|
|
|
|
} else {
|
|
|
|
IncludeStack.push_back(IncludeRec("<stdin>", stdin));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the location of the include directory so that the lexer can find
|
|
|
|
// it later.
|
|
|
|
IncludeDirectory = IncludeDir;
|
|
|
|
|
|
|
|
Filein = F;
|
|
|
|
Filelineno = 1;
|
|
|
|
Fileparse();
|
|
|
|
Filein = stdin;
|
|
|
|
}
|
|
|
|
|
2004-02-12 01:00:46 +01:00
|
|
|
/// HandleInclude - This function is called when an include directive is
|
|
|
|
/// encountered in the input stream...
|
|
|
|
///
|
2003-10-05 21:27:59 +02:00
|
|
|
static void HandleInclude(const char *Buffer) {
|
|
|
|
unsigned Length = yyleng;
|
|
|
|
assert(Buffer[Length-1] == '"');
|
|
|
|
Buffer += strlen("include ");
|
|
|
|
Length -= strlen("include ");
|
|
|
|
while (*Buffer != '"') {
|
|
|
|
++Buffer;
|
|
|
|
--Length;
|
|
|
|
}
|
|
|
|
assert(Length >= 2 && "Double quotes not found?");
|
|
|
|
std::string Filename(Buffer+1, Buffer+Length-1);
|
|
|
|
//std::cerr << "Filename = '" << Filename << "'\n";
|
|
|
|
|
|
|
|
// Save the line number and lex buffer of the includer...
|
|
|
|
IncludeStack.back().LineNo = Filelineno;
|
|
|
|
IncludeStack.back().Buffer = YY_CURRENT_BUFFER;
|
|
|
|
|
|
|
|
// Open the new input file...
|
|
|
|
yyin = fopen(Filename.c_str(), "r");
|
|
|
|
if (yyin == 0) {
|
|
|
|
// If we couldn't find the file in the current directory, look for it in
|
|
|
|
// the include directories.
|
|
|
|
//
|
2004-02-12 01:00:46 +01:00
|
|
|
// NOTE: Right now, there is only one directory. We need to eventually add
|
|
|
|
// support for more.
|
2004-02-13 17:33:56 +01:00
|
|
|
std::string NextFilename = IncludeDirectory + "/" + Filename;
|
|
|
|
yyin = fopen(NextFilename.c_str(), "r");
|
2003-10-05 21:27:59 +02:00
|
|
|
if (yyin == 0) {
|
|
|
|
err() << "Could not find include file '" << Filename << "'!\n";
|
2004-02-13 17:37:43 +01:00
|
|
|
exit(1);
|
2003-10-05 21:27:59 +02:00
|
|
|
}
|
2004-02-13 17:33:56 +01:00
|
|
|
Filename = NextFilename;
|
2003-10-05 21:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the file to our include stack...
|
|
|
|
IncludeStack.push_back(IncludeRec(Filename, yyin));
|
|
|
|
Filelineno = 1; // Reset line numbering...
|
|
|
|
//yyrestart(yyin); // Start lexing the new file...
|
|
|
|
|
|
|
|
yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
|
|
|
|
}
|
|
|
|
|
2004-02-12 01:00:46 +01:00
|
|
|
/// yywrap - This is called when the lexer runs out of input in one of the
|
|
|
|
/// files. Switch back to an includer if an includee has run out of input.
|
|
|
|
///
|
2003-10-05 21:27:59 +02:00
|
|
|
extern "C"
|
|
|
|
int yywrap() {
|
|
|
|
if (IncludeStack.back().File != stdin)
|
|
|
|
fclose(IncludeStack.back().File);
|
|
|
|
IncludeStack.pop_back();
|
|
|
|
if (IncludeStack.empty()) return 1; // Top-level file is done.
|
|
|
|
|
|
|
|
// Otherwise, we need to switch back to a file which included the current one.
|
|
|
|
Filelineno = IncludeStack.back().LineNo; // Restore current line number
|
|
|
|
yy_switch_to_buffer(IncludeStack.back().Buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2003-10-05 21:27:59 +02:00
|
|
|
%}
|
|
|
|
|
|
|
|
Comment \/\/.*
|
|
|
|
|
|
|
|
Identifier [a-zA-Z_][0-9a-zA-Z_]*
|
|
|
|
Integer [-+]?[0-9]+|0x[0-9a-fA-F]+|0b[01]+
|
|
|
|
CodeFragment \[\{([^}]+|\}[^\]])*\}\]
|
|
|
|
StringVal \"[^"]*\"
|
|
|
|
IncludeStr include[ \t\n]+\"[^"]*\"
|
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
{Comment} { /* Ignore comments */ }
|
|
|
|
|
|
|
|
{IncludeStr} { HandleInclude(yytext); }
|
|
|
|
{CodeFragment} { Filelval.StrVal = new std::string(yytext+2, yytext+yyleng-2);
|
|
|
|
return CODEFRAGMENT; }
|
|
|
|
|
|
|
|
int { return INT; }
|
|
|
|
bit { return BIT; }
|
|
|
|
bits { return BITS; }
|
|
|
|
string { return STRING; }
|
|
|
|
list { return LIST; }
|
|
|
|
code { return CODE; }
|
|
|
|
dag { return DAG; }
|
|
|
|
|
|
|
|
class { return CLASS; }
|
|
|
|
def { return DEF; }
|
|
|
|
field { return FIELD; }
|
|
|
|
let { return LET; }
|
|
|
|
in { return IN; }
|
|
|
|
|
|
|
|
{Identifier} { Filelval.StrVal = new std::string(yytext, yytext+yyleng);
|
|
|
|
return ID; }
|
|
|
|
${Identifier} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng);
|
|
|
|
return VARNAME; }
|
|
|
|
|
|
|
|
{StringVal} { Filelval.StrVal = new std::string(yytext+1, yytext+yyleng-1);
|
|
|
|
return STRVAL; }
|
|
|
|
|
|
|
|
{Integer} { Filelval.IntVal = ParseInt(Filetext); return INTVAL; }
|
|
|
|
|
|
|
|
[ \t\n]+ { /* Ignore whitespace */ }
|
|
|
|
|
|
|
|
|
|
|
|
"/*" { BEGIN(comment); CommentDepth++; }
|
|
|
|
<comment>[^*/]* /* eat anything that's not a '*' or '/' */
|
|
|
|
<comment>"*"+[^*/]* /* eat up '*'s not followed by '/'s */
|
|
|
|
<comment>"/*" { ++CommentDepth; }
|
|
|
|
<comment>"/"+[^*]* /* eat up /'s not followed by *'s */
|
|
|
|
<comment>"*"+"/" { if (!--CommentDepth) { BEGIN(INITIAL); } }
|
2004-02-13 17:37:43 +01:00
|
|
|
<comment><<EOF>> { err() << "Unterminated comment!\n"; exit(1); }
|
2003-10-05 21:27:59 +02:00
|
|
|
|
|
|
|
. { return Filetext[0]; }
|
|
|
|
|
|
|
|
%%
|