summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorClifford Wolf <clifford@clifford.at>2014-08-23 13:54:21 +0200
committerClifford Wolf <clifford@clifford.at>2014-08-23 13:54:21 +0200
commit5dce303a2a2c27d50e99856b6f33467798e13020 (patch)
tree4f32f6e720ef5bb063e48c9d5ab6bd8356c6b3d6 /kernel
parentfff12c719fc2d61e36e85f27080a4043078b0929 (diff)
Changed backend-api from FILE to std::ostream
Diffstat (limited to 'kernel')
-rw-r--r--kernel/log.cc25
-rw-r--r--kernel/register.cc28
-rw-r--r--kernel/register.h16
-rw-r--r--kernel/rtlil.cc11
-rw-r--r--kernel/yosys.h5
5 files changed, 36 insertions, 49 deletions
diff --git a/kernel/log.cc b/kernel/log.cc
index b742a549..2b4b5db5 100644
--- a/kernel/log.cc
+++ b/kernel/log.cc
@@ -210,20 +210,14 @@ void log_dump_val_worker(RTLIL::SigSpec v) {
const char *log_signal(const RTLIL::SigSpec &sig, bool autoint)
{
- char *ptr;
- size_t size;
-
- FILE *f = open_memstream(&ptr, &size);
- ILANG_BACKEND::dump_sigspec(f, sig, autoint);
- fputc(0, f);
- fclose(f);
+ std::stringstream buf;
+ ILANG_BACKEND::dump_sigspec(buf, sig, autoint);
if (string_buf_size < 100)
string_buf_size++;
else
string_buf.pop_front();
- string_buf.push_back(ptr);
- free(ptr);
+ string_buf.push_back(buf.str());
return string_buf.back().c_str();
}
@@ -239,16 +233,9 @@ const char *log_id(RTLIL::IdString str)
void log_cell(RTLIL::Cell *cell, std::string indent)
{
- char *ptr;
- size_t size;
-
- FILE *f = open_memstream(&ptr, &size);
- ILANG_BACKEND::dump_cell(f, indent, cell);
- fputc(0, f);
- fclose(f);
-
- log("%s", ptr);
- free(ptr);
+ std::stringstream buf;
+ ILANG_BACKEND::dump_cell(buf, indent, cell);
+ log("%s", buf.str().c_str());
}
// ---------------------------------------------------
diff --git a/kernel/register.cc b/kernel/register.cc
index a9e21e6d..95ed0cbd 100644
--- a/kernel/register.cc
+++ b/kernel/register.cc
@@ -423,15 +423,15 @@ Backend::~Backend()
void Backend::execute(std::vector<std::string> args, RTLIL::Design *design)
{
- FILE *f = NULL;
+ std::ostream *f = NULL;
auto state = pre_execute();
execute(f, std::string(), args, design);
post_execute(state);
- if (f != stdout)
- fclose(f);
+ if (f != &std::cout)
+ delete f;
}
-void Backend::extra_args(FILE *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
+void Backend::extra_args(std::ostream *&f, std::string &filename, std::vector<std::string> args, size_t argidx)
{
bool called_with_fp = f != NULL;
@@ -446,14 +446,18 @@ void Backend::extra_args(FILE *&f, std::string &filename, std::vector<std::strin
if (arg == "-") {
filename = "<stdout>";
- f = stdout;
+ f = &std::cout;
continue;
}
filename = arg;
- f = fopen(filename.c_str(), "w");
- if (f == NULL)
+ std::ofstream *ff = new std::ofstream;
+ ff->open(filename.c_str(), std::ofstream::trunc);
+ if (ff->fail()) {
+ delete ff;
log_cmd_error("Can't open output file `%s' for writing: %s\n", filename.c_str(), strerror(errno));
+ }
+ f = ff;
}
if (called_with_fp)
@@ -463,11 +467,11 @@ void Backend::extra_args(FILE *&f, std::string &filename, std::vector<std::strin
if (f == NULL) {
filename = "<stdout>";
- f = stdout;
+ f = &std::cout;
}
}
-void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename, std::string command)
+void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command)
{
std::vector<std::string> args;
char *s = strdup(command.c_str());
@@ -477,7 +481,7 @@ void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename,
backend_call(design, f, filename, args);
}
-void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename, std::vector<std::string> args)
+void Backend::backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::vector<std::string> args)
{
if (args.size() == 0)
return;
@@ -491,9 +495,9 @@ void Backend::backend_call(RTLIL::Design *design, FILE *f, std::string filename,
backend_register[args[0]]->execute(f, filename, args, design);
backend_register[args[0]]->post_execute(state);
} else if (filename == "-") {
- FILE *f_stdout = stdout; // workaround for OpenBSD 'stdout' implementation
+ std::ostream *f_cout = &std::cout;
auto state = backend_register[args[0]]->pre_execute();
- backend_register[args[0]]->execute(f_stdout, "<stdout>", args, design);
+ backend_register[args[0]]->execute(f_cout, "<stdout>", args, design);
backend_register[args[0]]->post_execute(state);
} else {
if (!filename.empty())
diff --git a/kernel/register.h b/kernel/register.h
index d7e4281c..f2c6ad29 100644
--- a/kernel/register.h
+++ b/kernel/register.h
@@ -17,15 +17,11 @@
*
*/
+#include "kernel/yosys.h"
+
#ifndef REGISTER_H
#define REGISTER_H
-#include "kernel/yosys.h"
-#include <stdio.h>
-#include <string>
-#include <vector>
-#include <map>
-
YOSYS_NAMESPACE_BEGIN
struct Pass
@@ -94,12 +90,12 @@ struct Backend : Pass
virtual void run_register();
virtual ~Backend();
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::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) = 0;
- void extra_args(FILE *&f, std::string &filename, std::vector<std::string> args, size_t argidx);
+ void extra_args(std::ostream *&f, std::string &filename, std::vector<std::string> args, size_t argidx);
- static void backend_call(RTLIL::Design *design, FILE *f, std::string filename, std::string command);
- static void backend_call(RTLIL::Design *design, FILE *f, std::string filename, std::vector<std::string> args);
+ static void backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command);
+ static void backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::vector<std::string> args);
};
// implemented in passes/cmds/select.cc
diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc
index 22bff7bd..28a45134 100644
--- a/kernel/rtlil.cc
+++ b/kernel/rtlil.cc
@@ -412,17 +412,12 @@ namespace {
void error(int linenr)
{
- char *ptr;
- size_t size;
-
- FILE *f = open_memstream(&ptr, &size);
- ILANG_BACKEND::dump_cell(f, " ", cell);
- fputc(0, f);
- fclose(f);
+ std::stringstream buf;
+ ILANG_BACKEND::dump_cell(buf, " ", cell);
log_error("Found error in internal cell %s%s%s (%s) at %s:%d:\n%s",
module ? module->name.c_str() : "", module ? "." : "",
- cell->name.c_str(), cell->type.c_str(), __FILE__, linenr, ptr);
+ cell->name.c_str(), cell->type.c_str(), __FILE__, linenr, buf.str().c_str());
}
int param(const char *name)
diff --git a/kernel/yosys.h b/kernel/yosys.h
index c6cbcabc..bfadb5ff 100644
--- a/kernel/yosys.h
+++ b/kernel/yosys.h
@@ -45,6 +45,11 @@
#include <functional>
#include <initializer_list>
+#include <sstream>
+#include <fstream>
+#include <ostream>
+#include <iostream>
+
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>