Changed frontend-api from FILE to std::istream

This commit is contained in:
Clifford Wolf 2014-08-23 15:03:55 +02:00
parent 5dce303a2a
commit 19cff41eb4
22 changed files with 116 additions and 89 deletions

View File

@ -101,13 +101,13 @@ struct IntersynthBackend : public Backend {
log("Output filename: %s\n", filename.c_str()); log("Output filename: %s\n", filename.c_str());
for (auto filename : libfiles) { for (auto filename : libfiles) {
FILE *f = fopen(filename.c_str(), "rt"); std::ifstream f;
if (f == NULL) f.open(filename.c_str());
if (f.fail())
log_error("Can't open lib file `%s'.\n", filename.c_str()); log_error("Can't open lib file `%s'.\n", filename.c_str());
RTLIL::Design *lib = new RTLIL::Design; RTLIL::Design *lib = new RTLIL::Design;
Frontend::frontend_call(lib, f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog"); Frontend::frontend_call(lib, &f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
libs.push_back(lib); libs.push_back(lib);
fclose(f);
} }
if (libs.size() > 0) if (libs.size() > 0)

View File

@ -45,15 +45,16 @@ struct IlangFrontend : public Frontend {
log("representation of a design in yosys's internal format.)\n"); log("representation of a design in yosys's internal format.)\n");
log("\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"); log_header("Executing ILANG frontend.\n");
extra_args(f, filename, args, 1); extra_args(f, filename, args, 1);
log("Input filename: %s\n", filename.c_str()); log("Input filename: %s\n", filename.c_str());
ILANG_FRONTEND::lexin = f;
ILANG_FRONTEND::current_design = design; ILANG_FRONTEND::current_design = design;
rtlil_frontend_ilang_yydebug = false; rtlil_frontend_ilang_yydebug = false;
rtlil_frontend_ilang_yyrestart(f); rtlil_frontend_ilang_yyrestart(NULL);
rtlil_frontend_ilang_yyparse(); rtlil_frontend_ilang_yyparse();
rtlil_frontend_ilang_yylex_destroy(); rtlil_frontend_ilang_yylex_destroy();
} }

View File

@ -26,12 +26,11 @@
#define ILANG_FRONTEND_H #define ILANG_FRONTEND_H
#include "kernel/yosys.h" #include "kernel/yosys.h"
#include <stdio.h>
YOSYS_NAMESPACE_BEGIN YOSYS_NAMESPACE_BEGIN
namespace ILANG_FRONTEND { namespace ILANG_FRONTEND {
void ilang_frontend(FILE *f, RTLIL::Design *design); extern std::istream *lexin;
extern RTLIL::Design *current_design; 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_yyerror(char const *s);
void rtlil_frontend_ilang_yyrestart(FILE *f); void rtlil_frontend_ilang_yyrestart(FILE *f);
int rtlil_frontend_ilang_yyparse(void); 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); int rtlil_frontend_ilang_yyget_lineno(void);
#endif #endif

View File

@ -29,9 +29,12 @@
#pragma clang diagnostic ignored "-Wdeprecated-register" #pragma clang diagnostic ignored "-Wdeprecated-register"
#endif #endif
#include "kernel/rtlil.h" #include "ilang_frontend.h"
#include "parser.tab.h" #include "parser.tab.h"
#define YY_INPUT(buf,result,max_size) \
result = ILANG_FRONTEND::lexin->readsome(buf, max_size);
%} %}
%option yylineno %option yylineno

View File

@ -27,6 +27,7 @@
#include "ilang_frontend.h" #include "ilang_frontend.h"
YOSYS_NAMESPACE_BEGIN YOSYS_NAMESPACE_BEGIN
namespace ILANG_FRONTEND { namespace ILANG_FRONTEND {
std::istream *lexin;
RTLIL::Design *current_design; RTLIL::Design *current_design;
RTLIL::Module *current_module; RTLIL::Module *current_module;
RTLIL::Wire *current_wire; RTLIL::Wire *current_wire;

View File

@ -430,7 +430,7 @@ struct LibertyFrontend : public Frontend {
log(" set the specified attribute (to the value 1) on all loaded modules\n"); log(" set the specified attribute (to the value 1) on all loaded modules\n");
log("\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_lib = false;
bool flag_ignore_redef = false; bool flag_ignore_redef = false;
@ -467,7 +467,7 @@ struct LibertyFrontend : public Frontend {
} }
extra_args(f, filename, args, argidx); extra_args(f, filename, args, argidx);
LibertyParser parser(f); LibertyParser parser(*f);
int cell_count = 0; int cell_count = 0;
for (auto cell : parser.ast->children) for (auto cell : parser.ast->children)

View File

@ -63,6 +63,9 @@ YOSYS_NAMESPACE_END
frontend_verilog_yylval.string = new std::string(std::string("\\") + yytext); \ frontend_verilog_yylval.string = new std::string(std::string("\\") + yytext); \
return TOK_ID; return TOK_ID;
#define YY_INPUT(buf,result,max_size) \
result = lexin->readsome(buf, max_size);
%} %}
%option yylineno %option yylineno

View File

@ -58,6 +58,7 @@ namespace VERILOG_FRONTEND {
bool do_not_require_port_stubs; bool do_not_require_port_stubs;
bool default_nettype_wire; bool default_nettype_wire;
bool sv_mode; bool sv_mode;
std::istream *lexin;
} }
YOSYS_NAMESPACE_END YOSYS_NAMESPACE_END

View File

@ -193,7 +193,7 @@ static std::string next_token(bool pass_newline = false)
return token; return token;
} }
static void input_file(FILE *f, std::string filename) static void input_file(std::istream &f, std::string filename)
{ {
char buffer[513]; char buffer[513];
int rc; int rc;
@ -202,14 +202,14 @@ static void input_file(FILE *f, std::string filename)
auto it = input_buffer.begin(); auto it = input_buffer.begin();
input_buffer.insert(it, "`file_push " + filename + "\n"); 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; buffer[rc] = 0;
input_buffer.insert(it, buffer); input_buffer.insert(it, buffer);
} }
input_buffer.insert(it, "\n`file_pop\n"); 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::set<std::string> defines_with_args;
std::map<std::string, std::string> defines_map(pre_defines_map); 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 else
fn = fn.substr(0, pos) + fn.substr(pos+1); fn = fn.substr(0, pos) + fn.substr(pos+1);
} }
FILE *fp = fopen(fn.c_str(), "r"); std::ifstream ff;
if (fp == NULL && fn.size() > 0 && fn[0] != '/' && filename.find('/') != std::string::npos) { 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 // 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 // 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; ff.clear();
fp = fopen(fn2.c_str(), "r"); 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 // if the include file was not found and it is not given with an absolute path, then
// search it in the include path // search it in the include path
for (auto incdir : include_dirs) { for (auto incdir : include_dirs) {
std::string fn2 = incdir + '/' + fn; ff.clear();
fp = fopen(fn2.c_str(), "r"); ff.open(incdir + '/' + fn);
if (fp != NULL) break; if (!ff.fail()) break;
} }
} }
if (fp != NULL) { if (ff.fail())
input_file(fp, fn);
fclose(fp);
} else
output_code.push_back("`file_notfound " + fn); output_code.push_back("`file_notfound " + fn);
else
input_file(ff, fn);
continue; continue;
} }

View File

@ -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("the syntax of the code, rather than to rely on read_verilog for that.\n");
log("\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_ast1 = false;
bool flag_dump_ast2 = false; bool flag_dump_ast2 = false;
@ -269,18 +269,18 @@ struct VerilogFrontend : public Frontend {
current_ast = new AST::AstNode(AST::AST_DESIGN); current_ast = new AST::AstNode(AST::AST_DESIGN);
default_nettype_wire = true; default_nettype_wire = true;
FILE *fp = f; lexin = f;
std::string code_after_preproc; std::string code_after_preproc;
if (!flag_nopp) { 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) if (flag_ppdump)
log("-- Verilog code after preprocessor --\n%s-- END OF DUMP --\n", code_after_preproc.c_str()); 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_yyset_lineno(1);
frontend_verilog_yyrestart(fp); frontend_verilog_yyrestart(NULL);
frontend_verilog_yyparse(); frontend_verilog_yyparse();
frontend_verilog_yylex_destroy(); 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); 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) if (!flag_nopp)
fclose(fp); delete lexin;
delete current_ast; delete current_ast;
current_ast = NULL; current_ast = NULL;

View File

@ -50,10 +50,13 @@ namespace VERILOG_FRONTEND
// running in SystemVerilog mode // running in SystemVerilog mode
extern bool sv_mode; extern bool sv_mode;
// lexer input stream
extern std::istream *lexin;
} }
// the pre-processor // 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 YOSYS_NAMESPACE_END

View File

@ -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)); log_error("Execution of command \"%s\" failed: the shell returned %d\n", command.c_str(), WEXITSTATUS(ret));
if (out_file.empty()) { if (out_file.empty()) {
f = fopen(stringf("%s/vhdl2verilog_output.v", tempdir_name).c_str(), "rt"); std::ifstream ff;
if (f == NULL) 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"); 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"); Frontend::frontend_call(design, &ff, stringf("%s/vhdl2verilog_output.v", tempdir_name), "verilog");
fclose(f);
} }
log_header("Removing temp directory `%s':\n", tempdir_name); log_header("Removing temp directory `%s':\n", tempdir_name);

View File

@ -286,20 +286,20 @@ void Frontend::execute(std::vector<std::string> args, RTLIL::Design *design)
{ {
log_assert(next_args.empty()); log_assert(next_args.empty());
do { do {
FILE *f = NULL; std::istream *f = NULL;
next_args.clear(); next_args.clear();
auto state = pre_execute(); auto state = pre_execute();
execute(f, std::string(), args, design); execute(f, std::string(), args, design);
post_execute(state); post_execute(state);
args = next_args; args = next_args;
fclose(f); delete f;
} while (!args.empty()); } while (!args.empty());
} }
FILE *Frontend::current_script_file = NULL; FILE *Frontend::current_script_file = NULL;
std::string Frontend::last_here_document; std::string Frontend::last_here_document;
void Frontend::extra_args(FILE *&f, std::string &filename, std::vector<std::string> args, size_t argidx) void Frontend::extra_args(std::istream *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
{ {
bool called_with_fp = f != NULL; bool called_with_fp = f != NULL;
@ -338,11 +338,16 @@ void Frontend::extra_args(FILE *&f, std::string &filename, std::vector<std::stri
break; break;
last_here_document += buffer; last_here_document += buffer;
} }
f = fmemopen((void*)last_here_document.c_str(), last_here_document.size(), "r"); f = new std::istringstream(last_here_document);
} else { } else {
if (filename.substr(0, 2) == "+/") if (filename.substr(0, 2) == "+/")
filename = proc_share_dirname() + filename.substr(1); filename = proc_share_dirname() + filename.substr(1);
f = fopen(filename.c_str(), "r"); std::ifstream *ff = new std::ifstream;
ff->open(filename.c_str());
if (ff->fail())
delete ff;
else
f = ff;
} }
if (f == NULL) if (f == NULL)
log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno)); log_cmd_error("Can't open input file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
@ -367,7 +372,7 @@ void Frontend::extra_args(FILE *&f, std::string &filename, std::vector<std::stri
// cmd_log_args(args); // cmd_log_args(args);
} }
void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filename, std::string command) void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
{ {
std::vector<std::string> args; std::vector<std::string> args;
char *s = strdup(command.c_str()); char *s = strdup(command.c_str());
@ -377,7 +382,7 @@ void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filenam
frontend_call(design, f, filename, args); frontend_call(design, f, filename, args);
} }
void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filename, std::vector<std::string> args) void Frontend::frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::vector<std::string> args)
{ {
if (args.size() == 0) if (args.size() == 0)
return; return;
@ -389,9 +394,9 @@ void Frontend::frontend_call(RTLIL::Design *design, FILE *f, std::string filenam
frontend_register[args[0]]->execute(f, filename, args, design); frontend_register[args[0]]->execute(f, filename, args, design);
frontend_register[args[0]]->post_execute(state); frontend_register[args[0]]->post_execute(state);
} else if (filename == "-") { } else if (filename == "-") {
FILE *f_stdin = stdin; // workaround for OpenBSD 'stdin' implementation std::istream *f_cin = &std::cin;
auto state = frontend_register[args[0]]->pre_execute(); auto state = frontend_register[args[0]]->pre_execute();
frontend_register[args[0]]->execute(f_stdin, "<stdin>", args, design); frontend_register[args[0]]->execute(f_cin, "<stdin>", args, design);
frontend_register[args[0]]->post_execute(state); frontend_register[args[0]]->post_execute(state);
} else { } else {
if (!filename.empty()) if (!filename.empty())

View File

@ -74,13 +74,13 @@ struct Frontend : Pass
virtual void run_register(); virtual void run_register();
virtual ~Frontend(); virtual ~Frontend();
virtual void execute(std::vector<std::string> args, RTLIL::Design *design) OVERRIDE FINAL; virtual void execute(std::vector<std::string> args, RTLIL::Design *design) OVERRIDE FINAL;
virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) = 0; virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) = 0;
static std::vector<std::string> next_args; static std::vector<std::string> next_args;
void extra_args(FILE *&f, std::string &filename, std::vector<std::string> args, size_t argidx); void extra_args(std::istream *&f, std::string &filename, std::vector<std::string> args, size_t argidx);
static void frontend_call(RTLIL::Design *design, FILE *f, std::string filename, std::string command); static void frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command);
static void frontend_call(RTLIL::Design *design, FILE *f, std::string filename, std::vector<std::string> args); static void frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::vector<std::string> args);
}; };
struct Backend : Pass struct Backend : Pass

View File

@ -47,6 +47,7 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <istream>
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>

View File

@ -720,13 +720,13 @@ struct ShowPass : public Pass {
} }
for (auto filename : libfiles) { for (auto filename : libfiles) {
FILE *f = fopen(filename.c_str(), "rt"); std::ifstream f;
if (f == NULL) f.open(filename.c_str());
if (f.fail())
log_error("Can't open lib file `%s'.\n", filename.c_str()); log_error("Can't open lib file `%s'.\n", filename.c_str());
RTLIL::Design *lib = new RTLIL::Design; RTLIL::Design *lib = new RTLIL::Design;
Frontend::frontend_call(lib, f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog"); Frontend::frontend_call(lib, &f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
libs.push_back(lib); libs.push_back(lib);
fclose(f);
} }
if (libs.size() > 0) if (libs.size() > 0)

View File

@ -41,7 +41,7 @@ struct WriteFileFrontend : public Frontend {
log(" EOT\n"); log(" EOT\n");
log("\n"); log("\n");
} }
virtual void execute(FILE *&f, std::string filename, std::vector<std::string> args, RTLIL::Design*) virtual void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design*)
{ {
bool append_mode = false; bool append_mode = false;
std::string output_filename; std::string output_filename;
@ -67,7 +67,7 @@ struct WriteFileFrontend : public Frontend {
char buffer[64 * 1024]; char buffer[64 * 1024];
size_t bytes; size_t bytes;
while (0 < (bytes = fread(buffer, 1, sizeof(buffer), f))) while (0 < (bytes = f->readsome(buffer, sizeof(buffer))))
fwrite(buffer, bytes, 1, of); fwrite(buffer, bytes, 1, of);
fclose(of); fclose(of);

View File

@ -463,11 +463,12 @@ struct DfflibmapPass : public Pass {
if (liberty_file.empty()) if (liberty_file.empty())
log_cmd_error("Missing `-liberty liberty_file' option!\n"); log_cmd_error("Missing `-liberty liberty_file' option!\n");
FILE *f = fopen(liberty_file.c_str(), "r"); std::ifstream f;
if (f == NULL) f.open(liberty_file.c_str());
if (f.fail())
log_cmd_error("Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno)); log_cmd_error("Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno));
LibertyParser libparser(f); LibertyParser libparser(f);
fclose(f); f.close();
find_cell(libparser.ast, "$_DFF_N_", false, false, false, false); find_cell(libparser.ast, "$_DFF_N_", false, false, false, false);
find_cell(libparser.ast, "$_DFF_P_", true, false, false, false); find_cell(libparser.ast, "$_DFF_P_", true, false, false, false);

View File

@ -609,13 +609,14 @@ struct ExtractPass : public Pass {
} }
else else
{ {
FILE *f = fopen(filename.c_str(), "rt"); std::ifstream f;
if (f == NULL) { f.open(filename.c_str());
if (f.fail()) {
delete map; delete map;
log_cmd_error("Can't open map file `%s'.\n", filename.c_str()); log_cmd_error("Can't open map file `%s'.\n", filename.c_str());
} }
Frontend::frontend_call(map, f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog"); Frontend::frontend_call(map, &f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
fclose(f); f.close();
if (filename.size() <= 3 || filename.substr(filename.size()-3) != ".il") { if (filename.size() <= 3 || filename.substr(filename.size()-3) != ".il") {
Pass::call(map, "proc"); Pass::call(map, "proc");

View File

@ -21,6 +21,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <istream>
#include <fstream>
#include <iostream>
#ifndef FILTERLIB #ifndef FILTERLIB
#include "kernel/log.h" #include "kernel/log.h"
#endif #endif
@ -85,19 +89,19 @@ int LibertyParser::lexer(std::string &str)
int c; int c;
do { do {
c = fgetc(f); c = f.get();
} while (c == ' ' || c == '\t' || c == '\r'); } while (c == ' ' || c == '\t' || c == '\r');
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.') { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.') {
str = c; str = c;
while (1) { while (1) {
c = fgetc(f); c = f.get();
if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.') if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_' || c == '-' || c == '+' || c == '.')
str += c; str += c;
else else
break; break;
} }
ungetc(c, f); f.unget();
// fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str()); // fprintf(stderr, "LEX: identifier >>%s<<\n", str.c_str());
return 'v'; return 'v';
} }
@ -105,7 +109,7 @@ int LibertyParser::lexer(std::string &str)
if (c == '"') { if (c == '"') {
str = c; str = c;
while (1) { while (1) {
c = fgetc(f); c = f.get();
if (c == '\n') if (c == '\n')
line++; line++;
str += c; str += c;
@ -117,34 +121,34 @@ int LibertyParser::lexer(std::string &str)
} }
if (c == '/') { if (c == '/') {
c = fgetc(f); c = f.get();
if (c == '*') { if (c == '*') {
int last_c = 0; int last_c = 0;
while (c > 0 && (last_c != '*' || c != '/')) { while (c > 0 && (last_c != '*' || c != '/')) {
last_c = c; last_c = c;
c = fgetc(f); c = f.get();
if (c == '\n') if (c == '\n')
line++; line++;
} }
return lexer(str); return lexer(str);
} else if (c == '/') { } else if (c == '/') {
while (c > 0 && c != '\n') while (c > 0 && c != '\n')
c = fgetc(f); c = f.get();
line++; line++;
return lexer(str); return lexer(str);
} }
ungetc(c, f); f.unget();
// fprintf(stderr, "LEX: char >>/<<\n"); // fprintf(stderr, "LEX: char >>/<<\n");
return '/'; return '/';
} }
if (c == '\\') { if (c == '\\') {
c = fgetc(f); c = f.get();
if (c == '\r') if (c == '\r')
c = fgetc(f); c = f.get();
if (c == '\n') if (c == '\n')
return lexer(str); return lexer(str);
ungetc(c, f); f.unget();
return '\\'; return '\\';
} }
@ -608,16 +612,20 @@ int main(int argc, char **argv)
} }
} }
FILE *f = stdin; std::istream *f = &std::cin;
if (argc == 3) { if (argc == 3) {
f = fopen(argv[2], "r"); std::ifstream *ff = new std::ifstream;
if (f == NULL) { ff->open(argv[2]);
if (ff->fail()) {
delete ff;
fprintf(stderr, "Can't open liberty file `%s'.\n", argv[2]); fprintf(stderr, "Can't open liberty file `%s'.\n", argv[2]);
usage(); usage();
} }
f = ff;
} }
LibertyParser parser(f); LibertyParser parser(*f);
if (parser.ast) { if (parser.ast) {
if (flag_verilogsim) if (flag_verilogsim)
gen_verilogsim(parser.ast); gen_verilogsim(parser.ast);
@ -626,7 +634,7 @@ int main(int argc, char **argv)
} }
if (argc == 3) if (argc == 3)
fclose(f); delete f;
return 0; return 0;
} }

View File

@ -41,10 +41,10 @@ namespace PASS_DFFLIBMAP
struct LibertyParser struct LibertyParser
{ {
FILE *f; std::istream &f;
int line; int line;
LibertyAst *ast; LibertyAst *ast;
LibertyParser(FILE *f) : f(f), line(1), ast(parse()) {} LibertyParser(std::istream &f) : f(f), line(1), ast(parse()) {}
~LibertyParser() { if (ast) delete ast; } ~LibertyParser() { if (ast) delete ast; }
int lexer(std::string &str); int lexer(std::string &str);
LibertyAst *parse(); LibertyAst *parse();

View File

@ -869,9 +869,8 @@ struct TechmapPass : public Pass {
RTLIL::Design *map = new RTLIL::Design; RTLIL::Design *map = new RTLIL::Design;
if (map_files.empty()) { if (map_files.empty()) {
FILE *f = fmemopen(stdcells_code, strlen(stdcells_code), "rt"); std::istringstream f(stdcells_code);
Frontend::frontend_call(map, f, "<techmap.v>", verilog_frontend); Frontend::frontend_call(map, &f, "<techmap.v>", verilog_frontend);
fclose(f);
} else } else
for (auto &fn : map_files) for (auto &fn : map_files)
if (fn.substr(0, 1) == "%") { if (fn.substr(0, 1) == "%") {
@ -883,11 +882,11 @@ struct TechmapPass : public Pass {
if (!map->has(mod->name)) if (!map->has(mod->name))
map->add(mod->clone()); map->add(mod->clone());
} else { } else {
FILE *f = fopen(fn.c_str(), "rt"); std::ifstream f;
if (f == NULL) f.open(fn.c_str());
if (f.fail())
log_cmd_error("Can't open map file `%s'\n", fn.c_str()); log_cmd_error("Can't open map file `%s'\n", fn.c_str());
Frontend::frontend_call(map, f, fn, (fn.size() > 3 && fn.substr(fn.size()-3) == ".il") ? "ilang" : verilog_frontend); Frontend::frontend_call(map, &f, fn, (fn.size() > 3 && fn.substr(fn.size()-3) == ".il") ? "ilang" : verilog_frontend);
fclose(f);
} }
std::map<RTLIL::IdString, RTLIL::Module*> modules_new; std::map<RTLIL::IdString, RTLIL::Module*> modules_new;