3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-04-23 17:15:33 +00:00

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

@ -720,13 +720,13 @@ struct ShowPass : public Pass {
}
for (auto filename : libfiles) {
FILE *f = fopen(filename.c_str(), "rt");
if (f == NULL)
std::ifstream f;
f.open(filename.c_str());
if (f.fail())
log_error("Can't open lib file `%s'.\n", filename.c_str());
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);
fclose(f);
}
if (libs.size() > 0)

View file

@ -41,7 +41,7 @@ struct WriteFileFrontend : public Frontend {
log(" EOT\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;
std::string output_filename;
@ -67,7 +67,7 @@ struct WriteFileFrontend : public Frontend {
char buffer[64 * 1024];
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);
fclose(of);

View file

@ -463,11 +463,12 @@ struct DfflibmapPass : public Pass {
if (liberty_file.empty())
log_cmd_error("Missing `-liberty liberty_file' option!\n");
FILE *f = fopen(liberty_file.c_str(), "r");
if (f == NULL)
std::ifstream f;
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));
LibertyParser libparser(f);
fclose(f);
f.close();
find_cell(libparser.ast, "$_DFF_N_", false, false, false, false);
find_cell(libparser.ast, "$_DFF_P_", true, false, false, false);

View file

@ -609,13 +609,14 @@ struct ExtractPass : public Pass {
}
else
{
FILE *f = fopen(filename.c_str(), "rt");
if (f == NULL) {
std::ifstream f;
f.open(filename.c_str());
if (f.fail()) {
delete map;
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");
fclose(f);
Frontend::frontend_call(map, &f, filename, (filename.size() > 3 && filename.substr(filename.size()-3) == ".il") ? "ilang" : "verilog");
f.close();
if (filename.size() <= 3 || filename.substr(filename.size()-3) != ".il") {
Pass::call(map, "proc");

View file

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

View file

@ -41,10 +41,10 @@ namespace PASS_DFFLIBMAP
struct LibertyParser
{
FILE *f;
std::istream &f;
int line;
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; }
int lexer(std::string &str);
LibertyAst *parse();

View file

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