38 #include <unordered_map>
39 #include <unordered_set>
73 using identifier_t = std::string;
74 using ranged_identifier_t = std::pair<std::string, std::vector<std::vector<u32>>>;
75 using numeral_t = std::vector<BooleanFunction::Value>;
76 using empty_t = std::monostate;
77 using assignment_t = std::variant<identifier_t, ranged_identifier_t, numeral_t, empty_t>;
82 struct VerilogDataEntry
85 std::string m_type =
"unknown";
86 std::string m_value =
"";
95 std::vector<std::vector<u32>> m_ranges;
96 std::vector<VerilogDataEntry> m_attributes;
97 std::vector<std::string> m_expanded_names;
105 std::string m_identifier;
106 std::string m_expression;
108 std::vector<std::vector<u32>> m_ranges;
109 std::vector<std::string> m_expanded_identifiers;
115 struct VerilogPortAssignment
117 std::optional<std::string> m_port_name;
118 std::vector<assignment_t> m_assignment;
124 struct VerilogAssignment
126 std::vector<assignment_t> m_variable;
127 std::vector<assignment_t> m_assignment;
133 struct VerilogInstance
137 bool m_is_module =
false;
138 std::vector<VerilogPortAssignment> m_port_assignments;
139 std::vector<VerilogDataEntry> m_parameters;
140 std::vector<VerilogDataEntry> m_attributes;
141 std::vector<std::pair<std::string, std::string>> m_expanded_port_assignments;
150 VerilogModule() =
default;
151 ~VerilogModule() =
default;
159 bool operator<(
const VerilogModule& other)
const
161 return m_name < other.m_name;
167 std::vector<VerilogDataEntry> m_attributes;
170 std::vector<std::unique_ptr<VerilogPort>> m_ports;
171 std::map<std::string, VerilogPort*> m_ports_by_identifier;
172 std::map<std::string, VerilogPort*> m_ports_by_expression;
173 std::map<std::string, std::string> m_expanded_port_identifiers_to_expressions;
176 std::vector<std::unique_ptr<VerilogSignal>> m_signals;
177 std::map<std::string, VerilogSignal*> m_signals_by_name;
180 std::vector<VerilogAssignment> m_assignments;
181 std::vector<std::pair<std::string, std::string>> m_expanded_assignments;
184 std::vector<std::unique_ptr<VerilogInstance>> m_instances;
185 std::map<std::string, VerilogInstance*> m_instances_by_name;
188 std::stringstream m_fs;
189 std::filesystem::path m_path;
192 Netlist* m_netlist =
nullptr;
195 std::vector<std::unique_ptr<VerilogModule>> m_modules;
196 std::unordered_map<std::string, VerilogModule*> m_modules_by_name;
197 std::string m_last_module;
200 TokenStream<std::string> m_token_stream;
203 std::unordered_map<std::string, GateType*> m_gate_types;
204 std::unordered_map<std::string, GateType*> m_vcc_gate_types;
205 std::unordered_map<std::string, GateType*> m_gnd_gate_types;
206 std::unordered_map<Net*, std::vector<std::pair<Module*, u32>>> m_module_port_by_net;
207 std::unordered_map<Module*, std::vector<std::tuple<std::string, Net*>>> m_module_ports;
210 std::unordered_map<std::string, u32> m_module_instantiation_count;
211 std::unordered_map<std::string, u32> m_instance_name_occurences;
212 std::unordered_map<std::string, u32> m_net_name_occurences;
217 std::unordered_map<std::string, Net*> m_net_by_name;
218 std::vector<std::pair<std::string, std::string>> m_nets_to_merge;
221 const std::string instance_name_seperator =
"/";
225 Result<std::monostate> parse_tokens();
226 Result<std::monostate> parse_module(std::vector<VerilogDataEntry>& attributes);
227 void parse_port_list(VerilogModule*
module);
228 Result<std::monostate> parse_port_declaration_list(VerilogModule*
module);
229 Result<std::monostate> parse_port_definition(VerilogModule*
module, std::vector<VerilogDataEntry>& attributes);
230 Result<std::monostate> parse_signal_definition(VerilogModule*
module, std::vector<VerilogDataEntry>& attributes);
231 Result<std::monostate> parse_assignment(VerilogModule*
module);
232 Result<std::monostate> parse_defparam(VerilogModule*
module);
233 void parse_attribute(std::vector<VerilogDataEntry>& attributes);
234 Result<std::monostate> parse_instance(VerilogModule*
module, std::vector<VerilogDataEntry>& attributes);
235 Result<std::monostate> parse_port_assign(VerilogInstance* instance);
236 Result<std::vector<VerilogDataEntry>> parse_parameter_assign();
239 Result<std::monostate> construct_netlist(VerilogModule* top_module);
241 instantiate_module(
const std::string& instance_name, VerilogModule* verilog_module, Module* parent,
const std::unordered_map<std::string, std::string>& parent_module_assignments);
244 std::string get_unique_alias(
const std::string& parent_name,
const std::string&
name,
const std::unordered_map<std::string, u32>& name_occurences)
const;
245 std::vector<u32> parse_range(TokenStream<std::string>& stream)
const;
246 void expand_ranges_recursively(std::vector<std::string>& expanded_names,
const std::string& current_name,
const std::vector<std::vector<u32>>& ranges,
u32 dimension)
const;
247 std::vector<std::string> expand_ranges(
const std::string&
name,
const std::vector<std::vector<u32>>& ranges)
const;
248 Result<std::vector<BooleanFunction::Value>> get_binary_vector(std::string value)
const;
249 Result<std::string> get_hex_from_literal(
const Token<std::string>& value_token)
const;
250 Result<std::pair<std::string, std::string>> parse_parameter_value(
const Token<std::string>& value_token)
const;
251 Result<std::vector<VerilogParser::assignment_t>> parse_assignment_expression(TokenStream<std::string>&& stream)
const;
252 std::vector<std::string> expand_assignment_expression(VerilogModule* verilog_module,
const std::vector<assignment_t>& vars)
const;
const Module * module(const Gate *g, const NodeBoxes &boxes)
std::unique_ptr< GateLibrary > parse(std::filesystem::path file_path)