HAL
netlist_factory.cpp
Go to the documentation of this file.
2 
11 
12 #include <fstream>
13 #include <unistd.h>
14 
15 namespace hal
16 {
17  namespace netlist_factory
18  {
19  std::unique_ptr<Netlist> create_netlist(const GateLibrary* gate_library)
20  {
21  if (gate_library == nullptr)
22  {
23  log_critical("netlist", "nullptr given as gate library.");
24  return nullptr;
25  }
26 
27  return std::make_unique<Netlist>(gate_library);
28  }
29 
30  std::unique_ptr<Netlist> load_netlist_from_string(const std::string& netlist_string, const std::filesystem::path& gate_library_file)
31  {
32  GateLibrary* lib = nullptr;
33 
34  if (!gate_library_file.empty())
35  {
36  lib = gate_library_manager::load(gate_library_file);
37  if (!lib)
38  {
39  log_critical("netlist", "could not parse gate library '{}', will not read netlist.", gate_library_file.string());
40  return nullptr;
41  }
42  }
43 
44  return netlist_serializer::deserialize_from_string(netlist_string, lib);
45  }
46 
47  std::unique_ptr<Netlist> load_netlist(const std::filesystem::path& netlist_file, const std::filesystem::path& gate_library_file)
48  {
49  if (access(netlist_file.c_str(), F_OK | R_OK) == -1)
50  {
51  log_critical("netlist", "could not access file '{}'.", netlist_file.string());
52  return nullptr;
53  }
54 
55  GateLibrary* lib = nullptr;
56 
57  if (!gate_library_file.empty())
58  {
59  lib = gate_library_manager::load(gate_library_file);
60  if (!lib)
61  {
62  log_critical("netlist", "could not parse gate library '{}', will not load netlist.", gate_library_file.string());
63  return nullptr;
64  }
65  }
66 
67  if (netlist_file.extension() == ".hal")
68  {
69  return netlist_serializer::deserialize_from_file(netlist_file, lib);
70  }
71  else
72  {
73  if (!lib)
74  {
75  log_critical("netlist", "could not load netlist '{}' without gate library.", netlist_file.string());
76  return nullptr;
77  }
78  return netlist_parser_manager::parse(netlist_file, lib);
79  }
80  }
81 
82  std::unique_ptr<Netlist> load_netlist(const std::filesystem::path& netlist_file, GateLibrary* gate_library)
83  {
84  if (access(netlist_file.c_str(), F_OK | R_OK) == -1)
85  {
86  log_critical("netlist", "could not access file '{}'.", netlist_file.string());
87  return nullptr;
88  }
89 
90  if (!gate_library)
91  {
92  log_critical("netlist", "gate library is a nullptr, will not load netlist.");
93  return nullptr;
94  }
95 
96  if (netlist_file.extension() == ".hal")
97  {
98  return netlist_serializer::deserialize_from_file(netlist_file, gate_library);
99  }
100  else
101  {
102  return netlist_parser_manager::parse(netlist_file, gate_library);
103  }
104  }
105 
106  std::unique_ptr<Netlist> load_hal_project(const std::filesystem::path& project_dir)
107  {
108  if (!std::filesystem::is_directory(project_dir))
109  {
110  log_critical("netlist", "could not access hal project '{}'.", project_dir.string());
111  return nullptr;
112  }
113 
115  if (!pm->open_project(project_dir.string()))
116  {
117  log_critical("netlist", "could not open hal project '{}'.", project_dir.string());
118  return nullptr;
119  }
120 
121  std::unique_ptr<Netlist> retval = std::move(pm->get_netlist());
122  return retval;
123  }
124 
125  std::unique_ptr<Netlist> load_netlist(const ProjectDirectory& pdir, const ProgramArguments& args)
126  {
127  std::filesystem::path netlist_file = args.is_option_set("--import-netlist") ? std::filesystem::path(args.get_parameter("--import-netlist")) : pdir.get_default_filename();
128 
129  if (access(netlist_file.c_str(), F_OK | R_OK) == -1)
130  {
131  log_critical("netlist", "cannot access file '{}'.", netlist_file.string());
132  return nullptr;
133  }
134 
135  auto extension = netlist_file.extension();
136 
137  if (extension == ".hal")
138  {
139  return netlist_serializer::deserialize_from_file(netlist_file);
140  }
141 
142  return netlist_parser_manager::parse(netlist_file, args);
143  }
144 
145  std::vector<std::unique_ptr<Netlist>> load_netlists(const std::filesystem::path& netlist_file)
146  {
147  if (access(netlist_file.c_str(), F_OK | R_OK) == -1)
148  {
149  log_critical("netlist", "could not access file '{}'.", netlist_file.string());
150  return {};
151  }
152 
153  return netlist_parser_manager::parse_all(netlist_file);
154  }
155  } // namespace netlist_factory
156 } // namespace hal
std::filesystem::path get_default_filename(const std::string &extension=std::string()) const
static ProjectManager * instance()
std::unique_ptr< Netlist > & get_netlist()
bool open_project(const std::string &path="")
#define log_critical(channel,...)
Definition: log.h:80
GateLibrary * load(std::filesystem::path file_path, bool reload)
std::unique_ptr< Netlist > create_netlist(const GateLibrary *gate_library)
Create a new empty netlist using the specified gate library.
std::unique_ptr< Netlist > load_hal_project(const std::filesystem::path &project_dir)
Create a netlist from the given hal project.
std::unique_ptr< Netlist > load_netlist_from_string(const std::string &netlist_string, const std::filesystem::path &gate_library_file)
Create a netlist from the given string.
std::unique_ptr< Netlist > load_netlist(const std::filesystem::path &netlist_file, const std::filesystem::path &gate_library_file)
Create a netlist from the given file.
std::vector< std::unique_ptr< Netlist > > load_netlists(const std::filesystem::path &netlist_file)
Create a netlist from a given file for each matching pre-loaded gate library.
std::unique_ptr< Netlist > parse(const std::filesystem::path &file_name, const ProgramArguments &args)
std::vector< std::unique_ptr< Netlist > > parse_all(const std::filesystem::path &file_name)
std::unique_ptr< Netlist > deserialize_from_string(const std::string &hal_string, GateLibrary *gatelib)
std::unique_ptr< Netlist > deserialize_from_file(const std::filesystem::path &hal_file, GateLibrary *gatelib)
This file contains various functions to create and load netlists.