mirror of
https://github.com/YosysHQ/yosys
synced 2025-04-28 03:15:50 +00:00
Changed frontend-api from FILE to std::istream
This commit is contained in:
parent
5dce303a2a
commit
19cff41eb4
22 changed files with 116 additions and 89 deletions
|
@ -45,15 +45,16 @@ struct IlangFrontend : public Frontend {
|
|||
log("representation of a design in yosys's internal format.)\n");
|
||||
log("\n");
|
||||
}
|
||||
virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
||||
virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
||||
{
|
||||
log_header("Executing ILANG frontend.\n");
|
||||
extra_args(f, filename, args, 1);
|
||||
log("Input filename: %s\n", filename.c_str());
|
||||
|
||||
ILANG_FRONTEND::lexin = f;
|
||||
ILANG_FRONTEND::current_design = design;
|
||||
rtlil_frontend_ilang_yydebug = false;
|
||||
rtlil_frontend_ilang_yyrestart(f);
|
||||
rtlil_frontend_ilang_yyrestart(NULL);
|
||||
rtlil_frontend_ilang_yyparse();
|
||||
rtlil_frontend_ilang_yylex_destroy();
|
||||
}
|
||||
|
|
|
@ -26,12 +26,11 @@
|
|||
#define ILANG_FRONTEND_H
|
||||
|
||||
#include "kernel/yosys.h"
|
||||
#include <stdio.h>
|
||||
|
||||
YOSYS_NAMESPACE_BEGIN
|
||||
|
||||
namespace ILANG_FRONTEND {
|
||||
void ilang_frontend(FILE *f, RTLIL::Design *design);
|
||||
extern std::istream *lexin;
|
||||
extern RTLIL::Design *current_design;
|
||||
}
|
||||
|
||||
|
@ -42,7 +41,7 @@ int rtlil_frontend_ilang_yylex(void);
|
|||
void rtlil_frontend_ilang_yyerror(char const *s);
|
||||
void rtlil_frontend_ilang_yyrestart(FILE *f);
|
||||
int rtlil_frontend_ilang_yyparse(void);
|
||||
void rtlil_frontend_ilang_yylex_destroy(void);
|
||||
int rtlil_frontend_ilang_yylex_destroy(void);
|
||||
int rtlil_frontend_ilang_yyget_lineno(void);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -29,9 +29,12 @@
|
|||
#pragma clang diagnostic ignored "-Wdeprecated-register"
|
||||
#endif
|
||||
|
||||
#include "kernel/rtlil.h"
|
||||
#include "ilang_frontend.h"
|
||||
#include "parser.tab.h"
|
||||
|
||||
#define YY_INPUT(buf,result,max_size) \
|
||||
result = ILANG_FRONTEND::lexin->readsome(buf, max_size);
|
||||
|
||||
%}
|
||||
|
||||
%option yylineno
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#include "ilang_frontend.h"
|
||||
YOSYS_NAMESPACE_BEGIN
|
||||
namespace ILANG_FRONTEND {
|
||||
std::istream *lexin;
|
||||
RTLIL::Design *current_design;
|
||||
RTLIL::Module *current_module;
|
||||
RTLIL::Wire *current_wire;
|
||||
|
|
|
@ -430,7 +430,7 @@ struct LibertyFrontend : public Frontend {
|
|||
log(" set the specified attribute (to the value 1) on all loaded modules\n");
|
||||
log("\n");
|
||||
}
|
||||
virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
||||
virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
||||
{
|
||||
bool flag_lib = false;
|
||||
bool flag_ignore_redef = false;
|
||||
|
@ -467,7 +467,7 @@ struct LibertyFrontend : public Frontend {
|
|||
}
|
||||
extra_args(f, filename, args, argidx);
|
||||
|
||||
LibertyParser parser(f);
|
||||
LibertyParser parser(*f);
|
||||
int cell_count = 0;
|
||||
|
||||
for (auto cell : parser.ast->children)
|
||||
|
|
|
@ -63,6 +63,9 @@ YOSYS_NAMESPACE_END
|
|||
frontend_verilog_yylval.string = new std::string(std::string("\\") + yytext); \
|
||||
return TOK_ID;
|
||||
|
||||
#define YY_INPUT(buf,result,max_size) \
|
||||
result = lexin->readsome(buf, max_size);
|
||||
|
||||
%}
|
||||
|
||||
%option yylineno
|
||||
|
|
|
@ -58,6 +58,7 @@ namespace VERILOG_FRONTEND {
|
|||
bool do_not_require_port_stubs;
|
||||
bool default_nettype_wire;
|
||||
bool sv_mode;
|
||||
std::istream *lexin;
|
||||
}
|
||||
YOSYS_NAMESPACE_END
|
||||
|
||||
|
|
|
@ -193,7 +193,7 @@ static std::string next_token(bool pass_newline = false)
|
|||
return token;
|
||||
}
|
||||
|
||||
static void input_file(FILE *f, std::string filename)
|
||||
static void input_file(std::istream &f, std::string filename)
|
||||
{
|
||||
char buffer[513];
|
||||
int rc;
|
||||
|
@ -202,14 +202,14 @@ static void input_file(FILE *f, std::string filename)
|
|||
auto it = input_buffer.begin();
|
||||
|
||||
input_buffer.insert(it, "`file_push " + filename + "\n");
|
||||
while ((rc = fread(buffer, 1, sizeof(buffer)-1, f)) > 0) {
|
||||
while ((rc = f.readsome(buffer, sizeof(buffer)-1)) > 0) {
|
||||
buffer[rc] = 0;
|
||||
input_buffer.insert(it, buffer);
|
||||
}
|
||||
input_buffer.insert(it, "\n`file_pop\n");
|
||||
}
|
||||
|
||||
std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::map<std::string, std::string> pre_defines_map, const std::list<std::string> include_dirs)
|
||||
std::string frontend_verilog_preproc(std::istream &f, std::string filename, const std::map<std::string, std::string> pre_defines_map, const std::list<std::string> include_dirs)
|
||||
{
|
||||
std::set<std::string> defines_with_args;
|
||||
std::map<std::string, std::string> defines_map(pre_defines_map);
|
||||
|
@ -288,27 +288,28 @@ std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::m
|
|||
else
|
||||
fn = fn.substr(0, pos) + fn.substr(pos+1);
|
||||
}
|
||||
FILE *fp = fopen(fn.c_str(), "r");
|
||||
if (fp == NULL && fn.size() > 0 && fn[0] != '/' && filename.find('/') != std::string::npos) {
|
||||
std::ifstream ff;
|
||||
ff.clear();
|
||||
ff.open(fn.c_str());
|
||||
if (ff.fail() && fn.size() > 0 && fn[0] != '/' && filename.find('/') != std::string::npos) {
|
||||
// if the include file was not found, it is not given with an absolute path, and the
|
||||
// currently read file is given with a path, then try again relative to its directory
|
||||
std::string fn2 = filename.substr(0, filename.rfind('/')+1) + fn;
|
||||
fp = fopen(fn2.c_str(), "r");
|
||||
ff.clear();
|
||||
ff.open(filename.substr(0, filename.rfind('/')+1) + fn);
|
||||
}
|
||||
if (fp == NULL && fn.size() > 0 && fn[0] != '/') {
|
||||
if (ff.fail() && fn.size() > 0 && fn[0] != '/') {
|
||||
// if the include file was not found and it is not given with an absolute path, then
|
||||
// search it in the include path
|
||||
for (auto incdir : include_dirs) {
|
||||
std::string fn2 = incdir + '/' + fn;
|
||||
fp = fopen(fn2.c_str(), "r");
|
||||
if (fp != NULL) break;
|
||||
ff.clear();
|
||||
ff.open(incdir + '/' + fn);
|
||||
if (!ff.fail()) break;
|
||||
}
|
||||
}
|
||||
if (fp != NULL) {
|
||||
input_file(fp, fn);
|
||||
fclose(fp);
|
||||
} else
|
||||
if (ff.fail())
|
||||
output_code.push_back("`file_notfound " + fn);
|
||||
else
|
||||
input_file(ff, fn);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -136,7 +136,7 @@ struct VerilogFrontend : public Frontend {
|
|||
log("the syntax of the code, rather than to rely on read_verilog for that.\n");
|
||||
log("\n");
|
||||
}
|
||||
virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
||||
virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
||||
{
|
||||
bool flag_dump_ast1 = false;
|
||||
bool flag_dump_ast2 = false;
|
||||
|
@ -269,18 +269,18 @@ struct VerilogFrontend : public Frontend {
|
|||
current_ast = new AST::AstNode(AST::AST_DESIGN);
|
||||
default_nettype_wire = true;
|
||||
|
||||
FILE *fp = f;
|
||||
lexin = f;
|
||||
std::string code_after_preproc;
|
||||
|
||||
if (!flag_nopp) {
|
||||
code_after_preproc = frontend_verilog_preproc(f, filename, defines_map, include_dirs);
|
||||
code_after_preproc = frontend_verilog_preproc(*f, filename, defines_map, include_dirs);
|
||||
if (flag_ppdump)
|
||||
log("-- Verilog code after preprocessor --\n%s-- END OF DUMP --\n", code_after_preproc.c_str());
|
||||
fp = fmemopen((void*)code_after_preproc.c_str(), code_after_preproc.size(), "r");
|
||||
lexin = new std::istringstream(code_after_preproc);
|
||||
}
|
||||
|
||||
frontend_verilog_yyset_lineno(1);
|
||||
frontend_verilog_yyrestart(fp);
|
||||
frontend_verilog_yyrestart(NULL);
|
||||
frontend_verilog_yyparse();
|
||||
frontend_verilog_yylex_destroy();
|
||||
|
||||
|
@ -294,7 +294,7 @@ struct VerilogFrontend : public Frontend {
|
|||
AST::process(design, current_ast, flag_dump_ast1, flag_dump_ast2, flag_dump_vlog, flag_nolatches, flag_nomem2reg, flag_mem2reg, flag_lib, flag_noopt, flag_icells, flag_ignore_redef, flag_defer, default_nettype_wire);
|
||||
|
||||
if (!flag_nopp)
|
||||
fclose(fp);
|
||||
delete lexin;
|
||||
|
||||
delete current_ast;
|
||||
current_ast = NULL;
|
||||
|
|
|
@ -50,10 +50,13 @@ namespace VERILOG_FRONTEND
|
|||
|
||||
// running in SystemVerilog mode
|
||||
extern bool sv_mode;
|
||||
|
||||
// lexer input stream
|
||||
extern std::istream *lexin;
|
||||
}
|
||||
|
||||
// the pre-processor
|
||||
std::string frontend_verilog_preproc(FILE *f, std::string filename, const std::map<std::string, std::string> pre_defines_map, const std::list<std::string> include_dirs);
|
||||
std::string frontend_verilog_preproc(std::istream &f, std::string filename, const std::map<std::string, std::string> pre_defines_map, const std::list<std::string> include_dirs);
|
||||
|
||||
YOSYS_NAMESPACE_END
|
||||
|
||||
|
|
|
@ -177,11 +177,11 @@ struct Vhdl2verilogPass : public Pass {
|
|||
log_error("Execution of command \"%s\" failed: the shell returned %d\n", command.c_str(), WEXITSTATUS(ret));
|
||||
|
||||
if (out_file.empty()) {
|
||||
f = fopen(stringf("%s/vhdl2verilog_output.v", tempdir_name).c_str(), "rt");
|
||||
if (f == NULL)
|
||||
std::ifstream ff;
|
||||
ff.open(stringf("%s/vhdl2verilog_output.v", tempdir_name).c_str());
|
||||
if (ff.fail())
|
||||
log_error("Can't open vhdl2verilog output file `vhdl2verilog_output.v'.\n");
|
||||
Frontend::frontend_call(design, f, stringf("%s/vhdl2verilog_output.v", tempdir_name), "verilog");
|
||||
fclose(f);
|
||||
Frontend::frontend_call(design, &ff, stringf("%s/vhdl2verilog_output.v", tempdir_name), "verilog");
|
||||
}
|
||||
|
||||
log_header("Removing temp directory `%s':\n", tempdir_name);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue