21 bool is_double_strict(
const std::string& s)
31 return idx == s.size();
44 m_modules_by_name.clear();
48 ifs.open(file_path.string(), std::ifstream::in);
51 return ERR(
"could not parse Verilog file '" + m_path.string() +
"' : unable to open file");
63 if (
auto res = parse_tokens(); res.is_error())
65 return ERR_APPEND(res.get_error(),
"could not parse Verilog file '" + file_path.string() +
"': unable to parse tokens");
70 if (e.line_number != (
u32)-1)
72 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': " + e.message +
" (line " + std::to_string(e.line_number) +
")");
76 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': " + e.message);
80 if (m_modules.empty())
82 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': does not contain any modules");
86 for (
auto& [module_name, verilog_module] : m_modules_by_name)
89 for (
const auto& port : verilog_module->m_ports)
91 const bool stands_for_one_signal = port->m_expression_parts.size() == 1 && std::holds_alternative<identifier_t>(port->m_expression_parts.front());
92 if (!stands_for_one_signal)
95 const std::vector<std::string> expanded_expression = expand_assignment_expression(verilog_module, port->m_expression_parts);
96 if (expanded_expression.empty())
98 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': unable to expand the expression of port '" + port->m_identifier +
"' of module '" + verilog_module->m_name +
"'");
100 if (expanded_expression.size() == 1)
102 port->m_expanded_identifiers = {port->m_identifier};
106 std::vector<u32> range(expanded_expression.size());
107 std::iota(range.begin(), range.end(), 0);
108 port->m_ranges = {range};
109 port->m_expanded_identifiers = expand_ranges(port->m_identifier, port->m_ranges);
111 for (
u32 i = 0; i < expanded_expression.size(); i++)
113 verilog_module->m_expanded_port_identifiers_to_expressions[port->m_expanded_identifiers.at(i)] = expanded_expression.at(i);
116 else if (
const identifier_t& signal = std::get<identifier_t>(port->m_expression_parts.front()); signal != port->m_identifier)
119 port->m_expanded_identifiers = port->m_ranges.empty() ? std::vector<std::string>{port->m_identifier} : expand_ranges(port->m_identifier, port->m_ranges);
120 const std::vector<std::string> expanded_expression = port->m_ranges.empty() ? std::vector<std::string>{signal} : expand_ranges(signal, port->m_ranges);
121 for (
u32 i = 0; i < expanded_expression.size(); i++)
123 verilog_module->m_expanded_port_identifiers_to_expressions[port->m_expanded_identifiers.at(i)] = expanded_expression.at(i);
128 port->m_expanded_identifiers = port->m_ranges.empty() ? std::vector<std::string>{port->m_identifier} : expand_ranges(port->m_identifier, port->m_ranges);
133 for (
auto& signal : verilog_module->m_signals)
135 if (!signal->m_ranges.empty())
137 signal->m_expanded_names = expand_ranges(signal->m_name, signal->m_ranges);
141 signal->m_expanded_names = std::vector<std::string>({signal->m_name});
146 for (
auto& assignment : verilog_module->m_assignments)
148 const std::vector<std::string> left_signals = expand_assignment_expression(verilog_module, assignment.m_variable);
149 const std::vector<std::string> right_signals = expand_assignment_expression(verilog_module, assignment.m_assignment);
150 if (left_signals.empty() || right_signals.empty())
152 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': unable to expand assignments within module '" + verilog_module->m_name +
"'");
155 u32 left_size = left_signals.size();
156 u32 right_size = right_signals.size();
157 if (left_size <= right_size)
160 for (
u32 i = 0; i < left_size; i++)
162 verilog_module->m_expanded_assignments.push_back(std::make_pair(left_signals.at(i), right_signals.at(i)));
167 for (
u32 i = 0; i < right_size; i++)
169 verilog_module->m_expanded_assignments.push_back(std::make_pair(left_signals.at(i), right_signals.at(i)));
173 for (
u32 i = 0; i < left_size - right_size; i++)
175 verilog_module->m_expanded_assignments.push_back(std::make_pair(left_signals.at(i + right_size),
"'0'"));
182 for (
auto& [module_name, verilog_module] : m_modules_by_name)
184 for (
auto&
instance : verilog_module->m_instances)
186 if (
auto module_it = m_modules_by_name.find(
instance->m_type); module_it != m_modules_by_name.end())
189 if (!
instance->m_port_assignments.empty())
192 if (
instance->m_port_assignments.front().m_port_name.has_value())
194 for (
const auto& port_assignment :
instance->m_port_assignments)
196 const std::vector<std::string> right_port = expand_assignment_expression(verilog_module, port_assignment.m_assignment);
197 if (!right_port.empty())
200 if (
const auto port_it = module_it->second->m_ports_by_identifier.find(port_assignment.m_port_name.value());
201 port_it == module_it->second->m_ports_by_identifier.end())
203 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': unable to assign signal to port '" + port_assignment.m_port_name.value()
204 +
"' as it is not a port of module '" + module_it->first +
"'");
208 port = port_it->second;
210 const std::vector<std::string>& left_port = port->m_expanded_identifiers;
211 if (left_port.empty())
213 return ERR(
"could not parse Verilog file '" + m_path.string() +
"': unable to expand port assignment");
216 u32 max_size = right_port.size() <= left_port.size() ? right_port.size() : left_port.size();
218 for (
u32 i = 0; i < max_size; i++)
220 instance->m_expanded_port_assignments.push_back(std::make_pair(left_port.at(i), right_port.at(i)));
228 std::vector<std::string> ports;
229 for (
const auto& port : m_modules_by_name.at(
instance->m_type)->m_ports)
231 ports.insert(ports.end(), port->m_expanded_identifiers.begin(), port->m_expanded_identifiers.end());
234 auto port_it = ports.begin();
236 for (
const auto& port_assignment :
instance->m_port_assignments)
238 std::vector<std::string> right_port = expand_assignment_expression(verilog_module, port_assignment.m_assignment);
239 if (!right_port.empty())
241 std::vector<std::string> left_port;
243 for (
u32 i = 0; i < right_port.size() && port_it != ports.end(); i++)
245 left_port.push_back(*port_it++);
248 u32 max_size = right_port.size() <= left_port.size() ? right_port.size() : left_port.size();
250 for (
u32 i = 0; i < max_size; i++)
252 instance->m_expanded_port_assignments.push_back(std::make_pair(left_port.at(i), right_port.at(i)));
269 m_netlist = result.get();
270 if (m_netlist ==
nullptr)
272 return ERR(
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name() +
"': failed to create empty netlist");
275 m_gate_types.clear();
276 m_gnd_gate_types.clear();
277 m_vcc_gate_types.clear();
278 m_module_instantiation_count.clear();
279 m_instance_name_occurences.clear();
280 m_net_name_occurences.clear();
281 m_net_by_name.clear();
282 m_nets_to_merge.clear();
283 m_module_ports.clear();
284 m_module_port_by_net.clear();
285 for (
const auto& verilog_module : m_modules)
287 for (
const auto&
instance : verilog_module->m_instances)
291 instance->m_expanded_port_assignments.clear();
303 if (m_zero_net ==
nullptr)
305 return ERR(
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name() +
"': failed to create zero net");
307 m_net_by_name[m_zero_net->
get_name()] = m_zero_net;
310 if (m_one_net ==
nullptr)
312 return ERR(
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name() +
"': failed to create one net");
314 m_net_by_name[m_one_net->
get_name()] = m_one_net;
317 std::vector<std::string> marked_top_modules;
318 for (
const auto& [
name,
module] : m_modules_by_name)
320 for (
const VerilogDataEntry& attribute :
module->m_attributes)
322 if (attribute.m_name ==
"top" &&
utils::trim(attribute.m_value) ==
"1")
324 marked_top_modules.push_back(
name);
329 if (marked_top_modules.size() > 1)
331 return ERR(
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name() +
"': several modules carry the top attribute: "
335 std::map<std::string, u32> module_name_to_refereneces;
336 for (
const auto& [_name,
module] : m_modules_by_name)
340 if (
const auto it = m_modules_by_name.find(
instance->m_type); it != m_modules_by_name.end())
342 module_name_to_refereneces[it->first]++;
347 std::vector<std::string> top_module_candidates = marked_top_modules;
348 if (top_module_candidates.empty())
350 for (
const auto& [
name,
module] : m_modules_by_name)
352 if (module_name_to_refereneces.find(
name) == module_name_to_refereneces.end())
354 top_module_candidates.push_back(
name);
359 if (top_module_candidates.empty())
361 return ERR(
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name() +
"': unable to find any top module candidates");
364 if (top_module_candidates.size() > 1)
366 return ERR(
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name()
367 +
"': found multiple modules as candidates for the top module, none of them marked (* top = 1 *): " +
utils::join(
", ", top_module_candidates));
371 VerilogModule* top_module = m_modules_by_name.at(top_module_candidates.front());
373 if (
const auto res = construct_netlist(top_module); res.is_error())
375 return ERR_APPEND(res.get_error(),
"could not instantiate Verilog netlist '" + m_path.string() +
"' with gate library '" + gate_library->
get_name() +
"': unable to construct netlist");
379 std::queue<Net*> nets_to_be_deleted;
383 const u32 num_of_sources =
net->get_num_of_sources();
384 const u32 num_of_destinations =
net->get_num_of_destinations();
385 const bool no_source = num_of_sources == 0 && !(
net->is_global_input_net() && num_of_destinations != 0);
386 const bool no_destination = num_of_destinations == 0 && !(
net->is_global_output_net() && num_of_sources != 0);
387 if (no_source && no_destination)
389 nets_to_be_deleted.push(
net);
393 while (!nets_to_be_deleted.empty())
395 Net*
net = nets_to_be_deleted.front();
396 nets_to_be_deleted.pop();
402 return OK(std::move(result));
409 void VerilogParser::tokenize()
411 const std::string delimiters =
"`,()[]{}\\#*: ;=./";
412 std::string current_token;
417 bool in_string =
false;
418 bool escaped =
false;
419 bool in_comment =
false;
421 std::vector<Token<std::string>> parsed_tokens;
422 while (std::getline(m_fs, line))
432 if (c ==
'/' && prev_char ==
'*')
442 if (!in_string && c ==
'\\')
447 else if (escaped && std::isspace(c))
452 else if (!escaped && c ==
'"')
454 in_string = !in_string;
457 if (!in_comment && ((!std::isspace(c) && delimiters.find(c) == std::string::npos) || escaped || in_string))
464 if (!current_token.empty())
466 if (parsed_tokens.size() > 1 &&
utils::is_digits(parsed_tokens.at(parsed_tokens.size() - 2).string) && parsed_tokens.at(parsed_tokens.size() - 1) ==
"."
469 parsed_tokens.pop_back();
470 parsed_tokens.back() +=
"." + current_token;
474 parsed_tokens.emplace_back(line_number, current_token);
476 current_token.clear();
479 if (!parsed_tokens.empty())
482 if (c ==
'(' && parsed_tokens.back() ==
"#")
484 parsed_tokens.back() =
"#(";
487 else if (c ==
'*' && parsed_tokens.back() ==
"(")
489 parsed_tokens.back() =
"(*";
492 else if (c ==
')' && parsed_tokens.back() ==
"*")
494 parsed_tokens.back() =
"*)";
498 else if (c ==
'/' && parsed_tokens.back() ==
"/")
500 parsed_tokens.pop_back();
503 else if (c ==
'*' && parsed_tokens.back() ==
"/")
506 parsed_tokens.pop_back();
511 if (!std::isspace(c))
513 parsed_tokens.emplace_back(line_number, std::string(1, c));
517 if (!current_token.empty())
519 parsed_tokens.emplace_back(line_number, current_token);
520 current_token.clear();
524 m_token_stream = TokenStream(parsed_tokens, {
"(",
"["}, {
")",
"]"});
527 Result<std::monostate> VerilogParser::parse_tokens()
529 std::vector<VerilogDataEntry> attributes;
534 if (m_token_stream.
peek() ==
"(*")
536 parse_attribute(attributes);
538 else if (m_token_stream.
peek() ==
"`")
541 log_warning(
"verilog_parser",
"could not parse compiler directives.");
545 line_number = m_token_stream.
peek().number;
546 if (
auto res = parse_module(attributes); res.is_error())
548 return ERR_APPEND(res.get_error(),
"could not parse tokens: unable to parse module (line " + std::to_string(line_number) +
")");
556 Result<std::monostate> VerilogParser::parse_module(std::vector<VerilogDataEntry>& attributes)
558 std::set<std::string> port_names;
559 std::vector<VerilogDataEntry> internal_attributes;
561 m_token_stream.
consume(
"module",
true);
562 const u32 line_number = m_token_stream.
peek().number;
563 const std::string module_name = m_token_stream.
consume();
566 if (
const auto it = m_modules_by_name.find(module_name); it != m_modules_by_name.end())
568 return ERR(
"could not parse module '" + module_name +
"' (line " + std::to_string(line_number) +
"): a module with the same name already exists (line "
569 + std::to_string(it->second->m_line_number) +
")");
572 auto verilog_module = std::make_unique<VerilogModule>();
573 VerilogModule* verilog_module_raw = verilog_module.get();
574 verilog_module_raw->m_line_number = line_number;
575 verilog_module_raw->m_name = module_name;
578 if (m_token_stream.
consume(
"#("))
582 m_token_stream.
consume(
")",
true);
583 log_warning(
"verilog_parser",
"could not parse parameter list provided for module '{}'.", module_name);
587 m_token_stream.
consume(
"(",
true);
588 Token<std::string> next_token = m_token_stream.
peek();
589 if (next_token ==
"input" || next_token ==
"output" || next_token ==
"inout")
591 if (
auto res = parse_port_declaration_list(verilog_module_raw); res.is_error())
593 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"': unable to parse port declaration list (line " + std::to_string(line_number) +
")");
598 if (
auto res = parse_port_list(verilog_module_raw); res.is_error())
600 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"' (line " + std::to_string(line_number) +
"): unable to parse port list");
604 m_token_stream.
consume(
";",
true);
606 next_token = m_token_stream.
peek();
607 while (next_token !=
"endmodule")
609 if (next_token ==
"input" || next_token ==
"output" || next_token ==
"inout")
611 if (
auto res = parse_port_definition(verilog_module_raw, internal_attributes); res.is_error())
613 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"': unable to parse port definition (line " + std::to_string(line_number) +
")");
616 else if (next_token ==
"wire" || next_token ==
"tri")
618 if (
auto res = parse_signal_definition(verilog_module_raw, internal_attributes); res.is_error())
620 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"': unable to parse signal definition (line " + std::to_string(line_number) +
")");
623 else if (next_token ==
"parameter")
627 m_token_stream.
consume(
";",
true);
628 log_warning(
"verilog_parser",
"could not parse parameter provided for module '{}'.", module_name);
630 else if (next_token ==
"assign")
632 if (
auto res = parse_assignment(verilog_module_raw); res.is_error())
634 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"': unable to parse assignment (line " + std::to_string(line_number) +
")");
637 else if (next_token ==
"defparam")
639 if (
auto res = parse_defparam(verilog_module_raw); res.is_error())
641 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"': unable to parse defparam (line " + std::to_string(line_number) +
")");
644 else if (next_token ==
"(*")
646 parse_attribute(internal_attributes);
650 if (
auto res = parse_instance(verilog_module_raw, internal_attributes); res.is_error())
652 return ERR_APPEND(res.get_error(),
"could not parse module '" + module_name +
"': unable to parse instance (line " + std::to_string(line_number) +
")");
656 next_token = m_token_stream.
peek();
659 m_token_stream.
consume(
"endmodule",
true);
662 if (!attributes.empty())
664 verilog_module->m_attributes.insert(verilog_module->m_attributes.end(), attributes.begin(), attributes.end());
669 m_modules.push_back(std::move(verilog_module));
670 m_modules_by_name[module_name] = verilog_module_raw;
671 m_last_module = module_name;
676 Result<std::monostate> VerilogParser::parse_port_list(VerilogModule* verilog_module)
678 TokenStream<std::string> ports_stream = m_token_stream.
extract_until(
")");
679 m_token_stream.
consume(
")",
true);
681 while (ports_stream.remaining() > 0)
683 Token<std::string> next_token = ports_stream.consume();
684 auto port = std::make_unique<VerilogPort>();
686 if (next_token ==
".")
688 port->m_identifier = ports_stream.consume().string;
689 ports_stream.consume(
"(",
true);
690 TokenStream<std::string> expression_stream = ports_stream.extract_until(
")");
691 ports_stream.consume(
")",
true);
695 auto parts_res = parse_assignment_expression(std::move(expression_stream));
696 if (parts_res.is_error())
698 return ERR_APPEND(parts_res.get_error(),
"could not parse port list: invalid expression of port '" + port->m_identifier +
"' (line " + std::to_string(next_token.number) +
")");
700 port->m_expression_parts = parts_res.get();
704 port->m_identifier = next_token.string;
705 port->m_expression_parts = {identifier_t(next_token.string)};
709 for (
const auto& part : port->m_expression_parts)
713 verilog_module->m_ports_by_expression[*
identifier] = port.get();
715 else if (
const ranged_identifier_t* ranged = std::get_if<ranged_identifier_t>(&part); ranged !=
nullptr)
717 verilog_module->m_ports_by_expression[ranged->first] = port.get();
721 verilog_module->m_ports_by_identifier[port->m_identifier] = port.get();
722 verilog_module->m_ports.push_back(std::move(port));
724 ports_stream.consume(
",", ports_stream.remaining() > 0);
730 Result<std::monostate> VerilogParser::parse_port_declaration_list(VerilogModule* verilog_module)
732 TokenStream<std::string> ports_stream = m_token_stream.
extract_until(
")");
733 m_token_stream.
consume(
")",
true);
735 while (ports_stream.remaining() > 0)
738 const Token<std::string> direction_token = ports_stream.consume();
742 return ERR(
"could not parse port declaration list: invalid direction '" + direction_token.string +
"' (line " + std::to_string(direction_token.number) +
")");
746 std::vector<std::vector<u32>> ranges;
747 while (ports_stream.consume(
"["))
749 const std::vector<u32> range = parse_range(ports_stream);
750 ports_stream.consume(
"]",
true);
752 ranges.emplace_back(range);
758 const Token<std::string> next_token = ports_stream.peek();
759 if (next_token ==
"input" || next_token ==
"output" || next_token ==
"inout")
763 ports_stream.consume();
765 auto port = std::make_unique<VerilogPort>();
766 const std::string& port_expression = next_token.string;
767 port->m_identifier = port_expression;
768 port->m_expression_parts = {identifier_t(port_expression)};
772 port->m_ranges = ranges;
774 verilog_module->m_ports_by_identifier[port_expression] = port.get();
775 verilog_module->m_ports_by_expression[port_expression] = port.get();
776 verilog_module->m_ports.push_back(std::move(port));
779 if (verilog_module->m_signals_by_name.find(port_expression) == verilog_module->m_signals_by_name.end())
781 auto signal = std::make_unique<VerilogSignal>();
782 signal->m_name = port_expression;
785 signal->m_ranges = ranges;
787 verilog_module->m_signals_by_name[port_expression] = signal.get();
788 verilog_module->m_signals.push_back(std::move(signal));
790 }
while (ports_stream.consume(
",", ports_stream.remaining() > 0));
796 Result<std::monostate> VerilogParser::parse_port_definition(VerilogModule* verilog_module, std::vector<VerilogDataEntry>& attributes)
799 const Token<std::string> direction_token = m_token_stream.
consume();
803 return ERR(
"could not parse port definition: invalid direction '" + direction_token.string +
"' (line " + std::to_string(direction_token.number) +
")");
807 std::vector<std::vector<u32>> ranges;
808 while (m_token_stream.
consume(
"["))
810 const std::vector<u32> range = parse_range(m_token_stream);
811 m_token_stream.
consume(
"]",
true);
813 ranges.emplace_back(range);
819 Token<std::string> port_expression_token = m_token_stream.
consume();
820 std::string port_expression = port_expression_token.string;
823 if (
const auto it = verilog_module->m_ports_by_expression.find(port_expression); it == verilog_module->m_ports_by_expression.end())
825 return ERR(
"could not parse port definition: a port with name '" + port_expression +
"' does not exist for module '" + verilog_module->m_name +
"' (line "
826 + std::to_string(direction_token.number) +
")");
835 const bool stands_for_one_signal = port->m_expression_parts.size() == 1 && std::holds_alternative<identifier_t>(port->m_expression_parts.front());
836 if (!ranges.empty() && stands_for_one_signal)
838 port->m_ranges = ranges;
842 if (
const auto signal_it = verilog_module->m_signals_by_name.find(port_expression); signal_it == verilog_module->m_signals_by_name.end())
844 auto signal = std::make_unique<VerilogSignal>();
845 signal->m_name = port_expression;
848 signal->m_ranges = ranges;
850 signal->m_attributes.insert(signal->m_attributes.end(), attributes.begin(), attributes.end());
851 verilog_module->m_signals_by_name[port_expression] = signal.get();
852 verilog_module->m_signals.push_back(std::move(signal));
856 auto* signal = signal_it->second;
857 signal->m_attributes.insert(signal->m_attributes.end(), attributes.begin(), attributes.end());
859 }
while (m_token_stream.
consume(
",",
false));
861 m_token_stream.
consume(
";",
true);
867 Result<std::monostate> VerilogParser::parse_signal_definition(VerilogModule* verilog_module, std::vector<VerilogDataEntry>& attributes)
870 u32 line_number = m_token_stream.
consume().number;
872 TokenStream<std::string> signal_stream = m_token_stream.
extract_until(
";");
873 m_token_stream.
consume(
";",
true);
876 std::vector<std::vector<u32>> ranges;
877 while (signal_stream.consume(
"["))
879 const std::vector<u32> range = parse_range(signal_stream);
880 signal_stream.consume(
"]",
true);
882 ranges.emplace_back(range);
888 Token<std::string> signal_name = signal_stream.consume();
889 if (signal_stream.remaining() > 0 && signal_stream.peek() ==
"=")
891 VerilogAssignment assignment;
892 assignment.m_variable.push_back(signal_name);
893 signal_stream.consume(
"=",
true);
894 if (
auto res = parse_assignment_expression(signal_stream.extract_until(
",")); res.is_error())
896 return ERR_APPEND(res.get_error(),
"could not parse signal definition: unable to parse assignment expression (line " + std::to_string(line_number) +
")");
900 assignment.m_assignment = res.get();
902 verilog_module->m_assignments.push_back(std::move(assignment));
906 if (
const auto signal_it = verilog_module->m_signals_by_name.find(signal_name.string); signal_it == verilog_module->m_signals_by_name.end())
908 auto signal = std::make_unique<VerilogSignal>();
909 signal->m_name = signal_name.string;
912 signal->m_ranges = ranges;
914 signal->m_attributes.insert(signal->m_attributes.end(), attributes.begin(), attributes.end());
915 verilog_module->m_signals_by_name[signal_name.string] = signal.get();
916 verilog_module->m_signals.push_back(std::move(signal));
920 auto* signal = signal_it->second;
921 signal->m_attributes.insert(signal->m_attributes.end(), attributes.begin(), attributes.end());
924 }
while (signal_stream.consume(
",",
false));
931 Result<std::monostate> VerilogParser::parse_assignment(VerilogModule* verilog_module)
933 m_token_stream.
consume(
"assign",
true);
934 u32 line_number = m_token_stream.
peek().number;
935 VerilogAssignment assignment;
937 if (
auto res = parse_assignment_expression(m_token_stream.
extract_until(
"=")); res.is_error())
939 return ERR_APPEND(res.get_error(),
"could not parse assignment: unable to parse assignment expression (line " + std::to_string(line_number) +
")");
943 assignment.m_variable = res.get();
945 m_token_stream.
consume(
"=",
true);
947 if (
auto res = parse_assignment_expression(m_token_stream.
extract_until(
";")); res.is_error())
949 return ERR_APPEND(res.get_error(),
"could not parse assignment: unable to parse assignment expression (line " + std::to_string(line_number) +
")");
953 assignment.m_assignment = res.get();
957 for (
const auto& var : assignment.m_variable)
961 if (verilog_module->m_signals_by_name.find(*
identifier) == verilog_module->m_signals_by_name.end())
963 auto signal = std::make_unique<VerilogSignal>();
965 verilog_module->m_signals_by_name[*
identifier] = signal.get();
966 verilog_module->m_signals.push_back(std::move(signal));
969 else if (
const auto* ranged_identifier = std::get_if<ranged_identifier_t>(&var); ranged_identifier !=
nullptr)
971 const auto& signal_name = std::get<0>(*ranged_identifier);
972 if (verilog_module->m_signals_by_name.find(signal_name) == verilog_module->m_signals_by_name.end())
974 auto signal = std::make_unique<VerilogSignal>();
975 signal->m_name = signal_name;
976 signal->m_ranges = std::get<1>(*ranged_identifier);
977 verilog_module->m_signals_by_name[signal_name] = signal.get();
978 verilog_module->m_signals.push_back(std::move(signal));
982 m_token_stream.
consume(
";",
true);
984 verilog_module->m_assignments.push_back(std::move(assignment));
988 Result<std::monostate> VerilogParser::parse_defparam(VerilogModule*
module)
990 m_token_stream.
consume(
"defparam",
true);
991 std::string instance_name = m_token_stream.
consume().string;
992 m_token_stream.
consume(
".",
true);
994 if (
const auto inst_it =
module->m_instances_by_name.find(instance_name); inst_it !=
module->m_instances_by_name.end())
996 VerilogDataEntry param;
997 param.m_name = m_token_stream.
consume().string;
998 m_token_stream.
consume(
"=",
true);
1000 if (
const auto res = parse_parameter_value(param.m_name, m_token_stream.
consume()); res.is_ok())
1002 const auto& [parsed_param, value_str] = res.get();
1003 param.m_value = value_str;
1004 param.m_parameter = parsed_param;
1005 inst_it->second->m_parameters.push_back(std::move(param));
1009 log_warning(
"verilog_parser",
"{}", res.get_error().get());
1014 m_token_stream.
consume(
";",
true);
1015 return ERR(
"could not parse defparam: no instance with name '" + instance_name +
"' exists within module '" +
module->m_name +
"'");
1018 m_token_stream.
consume(
";",
true);
1022 void VerilogParser::parse_attribute(std::vector<VerilogDataEntry>& attributes)
1024 m_token_stream.
consume(
"(*",
true);
1029 VerilogDataEntry attribute;
1030 attribute.m_name = m_token_stream.
consume().string;
1033 if (m_token_stream.
consume(
"="))
1035 attribute.m_value = m_token_stream.
consume();
1038 if (attribute.m_value[0] ==
'\"' && attribute.m_value.back() ==
'\"')
1040 attribute.m_value = attribute.m_value.substr(1, attribute.m_value.size() - 2);
1044 attributes.push_back(std::move(attribute));
1046 }
while (m_token_stream.
consume(
",",
false));
1048 m_token_stream.
consume(
"*)",
true);
1051 Result<std::monostate> VerilogParser::parse_instance(VerilogModule* verilog_module, std::vector<VerilogDataEntry>& attributes)
1053 auto instance = std::make_unique<VerilogInstance>();
1054 u32 line_number = m_token_stream.
peek().number;
1058 if (m_token_stream.
consume(
"#("))
1060 if (
auto res = parse_parameter_assign(); res.is_error())
1062 return ERR_APPEND(res.get_error(),
"could not parse instance of type '" +
instance->m_type +
"': unable to parse parameter assignment (line " + std::to_string(line_number) +
")");
1066 instance->m_parameters = res.get();
1074 if (
auto res = parse_port_assign(
instance.get()); res.is_error())
1077 "could not parse instance '" +
instance->m_name +
"' of type '" +
instance->m_type +
"': unable to parse port assignment (line " + std::to_string(line_number) +
")");
1081 instance->m_attributes = attributes;
1085 verilog_module->m_instances.push_back(std::move(
instance));
1090 Result<std::monostate> VerilogParser::parse_port_assign(VerilogInstance*
instance)
1092 u32 line_number = m_token_stream.
peek().number;
1093 m_token_stream.
consume(
"(",
true);
1095 if (m_token_stream.
peek() ==
".")
1100 VerilogPortAssignment port_assignment;
1101 port_assignment.m_port_name = m_token_stream.
consume().string;
1102 m_token_stream.
consume(
"(",
true);
1103 if (
auto res = parse_assignment_expression(m_token_stream.
extract_until(
")")); res.is_error())
1105 return ERR_APPEND(res.get_error(),
"could not parse port assignment: unable to parse assignment expression (line " + std::to_string(line_number) +
")");
1109 port_assignment.m_assignment = res.get();
1111 m_token_stream.
consume(
")",
true);
1112 if (port_assignment.m_assignment.empty())
1116 instance->m_port_assignments.push_back(std::move(port_assignment));
1117 }
while (m_token_stream.
consume(
",",
false));
1123 VerilogPortAssignment port_assignment;
1124 if (
auto res = parse_assignment_expression(m_token_stream.
extract_until(
",", line_end - 1)); res.is_error())
1126 return ERR_APPEND(res.get_error(),
"could not parse port assignment: unable to parse assignment expression (line " + std::to_string(line_number) +
")");
1130 port_assignment.m_assignment = res.get();
1132 if (port_assignment.m_assignment.empty())
1136 instance->m_port_assignments.push_back(std::move(port_assignment));
1137 }
while (m_token_stream.
consume(
",",
false));
1140 m_token_stream.
consume(
")",
true);
1141 m_token_stream.
consume(
";",
true);
1146 Result<std::vector<VerilogParser::VerilogDataEntry>> VerilogParser::parse_parameter_assign()
1148 std::vector<VerilogDataEntry> generics;
1152 if (m_token_stream.
consume(
".",
false))
1154 const Token<std::string> lhs = m_token_stream.
join_until(
"(",
"");
1155 m_token_stream.
consume(
"(",
true);
1156 const Token<std::string> rhs = m_token_stream.
join_until(
")",
"");
1157 m_token_stream.
consume(
")",
true);
1159 if (
const auto res = parse_parameter_value(lhs.string, rhs); res.is_ok())
1161 const auto& [param, value_str] = res.get();
1162 VerilogDataEntry entry;
1163 entry.m_name = lhs.string;
1164 entry.m_value = value_str;
1165 entry.m_parameter = param;
1166 generics.push_back(std::move(entry));
1170 log_warning(
"verilog_parser",
"{}", res.get_error().get());
1173 }
while (m_token_stream.
consume(
",",
false));
1175 m_token_stream.
consume(
")",
true);
1177 return OK(generics);
1184 Result<std::monostate> VerilogParser::construct_netlist(VerilogModule* top_module)
1190 std::queue<VerilogModule*> q;
1195 VerilogModule*
module = q.front();
1198 m_module_instantiation_count[
module->m_name]++;
1201 for (
const auto& s :
module->m_signals)
1203 std::vector<std::string> expanded_names;
1204 expand_ranges_recursively(expanded_names, s->m_name, s->m_ranges, 0);
1205 for (
const auto& net_name : expanded_names)
1207 m_net_name_occurences[net_name]++;
1213 m_instance_name_occurences[
instance->m_name]++;
1216 if (
const auto it = m_modules_by_name.find(
instance->m_type); it != m_modules_by_name.end())
1223 for (
auto& [module_name, verilog_module] : m_modules_by_name)
1226 if (m_module_instantiation_count[module_name] == 0)
1228 log_warning(
"verilog_parser",
"module '{}' has been defined in the netlist but is not instantiated.", module_name);
1233 for (
const auto&
instance : verilog_module->m_instances)
1235 if (
const auto gate_type_it = m_gate_types.find(
instance->m_type); gate_type_it != m_gate_types.end())
1237 if (!
instance->m_port_assignments.empty())
1240 if (
instance->m_port_assignments.front().m_port_name.has_value())
1243 std::unordered_map<std::string, std::vector<std::string>> pin_groups;
1244 for (
const auto pin_group : gate_type_it->second->get_pin_groups())
1246 const auto pins = pin_group->get_pins();
1247 for (
auto it =
pins.rbegin(); it !=
pins.rend(); it++)
1249 const auto* pin = *it;
1250 pin_groups[pin_group->get_name()].push_back(pin->get_name());
1254 for (
const auto& port_assignment :
instance->m_port_assignments)
1256 std::vector<std::string> right_port = expand_assignment_expression(verilog_module, port_assignment.m_assignment);
1257 if (!right_port.empty())
1259 std::vector<std::string> left_port;
1261 const auto& port_name = port_assignment.m_port_name.value();
1262 if (
const auto group_it = pin_groups.find(port_name); group_it != pin_groups.end())
1264 left_port = group_it->second;
1268 left_port.push_back(port_name);
1271 u32 max_size = right_port.size() <= left_port.size() ? right_port.size() : left_port.size();
1273 for (
u32 i = 0; i < max_size; i++)
1275 instance->m_expanded_port_assignments.push_back(std::make_pair(left_port.at(i), right_port.at(i)));
1284 std::vector<std::string>
pins = gate_type_it->second->get_pin_names();
1285 auto pin_it =
pins.begin();
1287 for (
const auto& port_assignment :
instance->m_port_assignments)
1289 std::vector<std::string> right_port = expand_assignment_expression(verilog_module, port_assignment.m_assignment);
1290 if (!right_port.empty())
1292 std::vector<std::string> left_port;
1294 for (
u32 i = 0; i < right_port.size() && pin_it !=
pins.end(); i++)
1296 left_port.push_back(*pin_it++);
1299 u32 max_size = right_port.size() <= left_port.size() ? right_port.size() : left_port.size();
1301 for (
u32 i = 0; i < max_size; i++)
1303 instance->m_expanded_port_assignments.push_back(std::make_pair(left_port.at(i), right_port.at(i)));
1314 std::unordered_map<std::string, std::string> top_assignments;
1315 for (
const auto& port : top_module->m_ports)
1317 for (
const auto& expanded_port_identifier : port->m_expanded_identifiers)
1319 const auto signal_name = get_unique_alias(
"", expanded_port_identifier +
"__GLOBAL_IO__", m_net_name_occurences);
1320 m_net_name_occurences[signal_name]++;
1323 if (global_port_net ==
nullptr)
1325 return ERR(
"could not construct netlist: failed to create global I/O net '" + signal_name +
"'");
1328 m_net_by_name[signal_name] = global_port_net;
1331 top_assignments[expanded_port_identifier] = signal_name;
1335 if (!global_port_net->mark_global_input_net())
1337 return ERR(
"could not construct netlist: failed to mark global I/O net '" + signal_name +
"' as global input");
1343 if (!global_port_net->mark_global_output_net())
1345 return ERR(
"could not construct netlist: failed to mark global I/O net '" + signal_name +
"' as global output");
1351 if (
auto res = instantiate_module(
"top_module", top_module,
nullptr, top_assignments); res.is_error())
1353 return ERR_APPEND(res.get_error(),
"could not construct netlist: unable to instantiate top module");
1357 std::unordered_map<std::string, std::string> merged_nets;
1358 std::unordered_map<std::string, std::vector<std::string>> master_to_slaves;
1360 for (
auto& [master, slave] : m_nets_to_merge)
1365 if (
const auto master_it = merged_nets.find(master); master_it != merged_nets.end())
1367 master = master_it->second;
1378 if (
const auto slave_it = merged_nets.find(slave); slave_it != merged_nets.end())
1380 slave = slave_it->second;
1388 auto master_net = m_net_by_name.at(master);
1389 auto slave_net = m_net_by_name.at(slave);
1391 if (master_net == slave_net)
1395 else if (slave_net == m_zero_net || slave_net == m_one_net)
1397 auto* tmp_net = master_net;
1398 master_net = slave_net;
1399 slave_net = tmp_net;
1401 auto tmp_name = master;
1407 if (slave_net->is_global_input_net())
1409 master_net->mark_global_input_net();
1412 for (
auto src : slave_net->get_sources())
1414 Gate* src_gate = src->get_gate();
1415 GatePin* src_pin = src->get_pin();
1417 if (!slave_net->remove_source(src))
1419 return ERR(
"could not construct netlist: failed to remove source from net '" + slave_net->get_name() +
"' with ID " + std::to_string(slave_net->get_id()));
1422 if (!master_net->is_a_source(src_gate, src_pin))
1424 if (!master_net->add_source(src_gate, src_pin))
1426 return ERR(
"could not construct netlist: failed to add source to net '" + master_net->get_name() +
"' with ID " + std::to_string(master_net->get_id()));
1432 if (slave_net->is_global_output_net())
1434 master_net->mark_global_output_net();
1437 for (
auto dst : slave_net->get_destinations())
1439 Gate* dst_gate = dst->get_gate();
1440 GatePin* dst_pin = dst->get_pin();
1442 if (!slave_net->remove_destination(dst))
1444 return ERR(
"could not construct netlist: failed to remove destination from net '" + slave_net->get_name() +
"' with ID " + std::to_string(slave_net->get_id()));
1447 if (!master_net->is_a_destination(dst_gate, dst_pin))
1449 if (!master_net->add_destination(dst_gate, dst_pin))
1451 return ERR(
"could not construct netlist: failed to add destination to net '" + master_net->get_name() +
"' with ID " + std::to_string(master_net->get_id()));
1457 for (
const auto& [
identifier, content] : slave_net->get_data_map())
1459 if (!master_net->set_data(std::get<0>(
identifier), std::get<1>(
identifier), std::get<0>(content), std::get<1>(content)))
1462 "unable to transfer data from slave net '{}' with ID {} to master net '{}' with ID {}.",
1463 slave_net->get_name(),
1464 slave_net->get_id(),
1465 master_net->get_name(),
1466 master_net->get_id());
1471 if (
const auto it = m_module_port_by_net.find(slave_net); it != m_module_port_by_net.end())
1475 std::get<1>(m_module_ports.at(
module).at(
index)) = master_net;
1477 m_module_port_by_net[master_net].insert(m_module_port_by_net[master_net].end(), it->second.begin(), it->second.end());
1478 m_module_port_by_net.erase(it);
1482 m_net_by_name.erase(slave);
1483 merged_nets[slave] = master;
1484 master_to_slaves[master].push_back(slave);
1488 for (
auto& master_net : m_netlist->
get_nets())
1490 const auto master_name = master_net->get_name();
1492 if (
const auto m2s_it = master_to_slaves.find(master_name); m2s_it != master_to_slaves.end())
1494 std::vector<std::vector<std::string>> merged_slaves;
1495 auto current_slaves = m2s_it->second;
1497 while (!current_slaves.empty())
1499 std::vector<std::string> next_slaves;
1500 for (
const auto& s : current_slaves)
1502 if (
const auto m2s_inner_it = master_to_slaves.find(s); m2s_inner_it != master_to_slaves.end())
1504 next_slaves.insert(next_slaves.end(), m2s_inner_it->second.begin(), m2s_inner_it->second.end());
1508 merged_slaves.push_back(current_slaves);
1509 current_slaves = next_slaves;
1510 next_slaves.clear();
1515 std::string merged_str =
"";
1516 bool has_merged_nets =
false;
1517 for (
const auto& vec : merged_slaves)
1521 has_merged_nets =
true;
1523 const auto s =
utils::join(
", ", vec, [](
const auto e) {
return '"' + e +
'"'; });
1524 merged_str +=
"[" + s +
"], ";
1526 merged_str = merged_str.substr(0, merged_str.size() - 2);
1528 if (has_merged_nets)
1530 master_net->set_data(
"parser_annotation",
"merged_nets",
"string",
"[" + merged_str +
"]");
1540 GateType* gnd_type = m_gnd_gate_types.begin()->second;
1541 GatePin* output_pin = gnd_type->get_output_pins().front();
1546 return ERR(
"failed to mark GND gate");
1549 if (m_zero_net->
add_source(gnd, output_pin) ==
nullptr)
1551 return ERR(
"failed to add source to GND gate");
1557 m_zero_net =
nullptr;
1566 GateType* vcc_type = m_vcc_gate_types.begin()->second;
1567 GatePin* output_pin = vcc_type->get_output_pins().front();
1572 return ERR(
"failed to mark VCC gate");
1575 if (m_one_net->
add_source(vcc, output_pin) ==
nullptr)
1577 return ERR(
"failed to add source to VCC gate");
1583 m_one_net =
nullptr;
1594 for (
const auto& [
module, ports] : m_module_ports)
1599 for (
const auto& [port_name, port_net] : ports)
1609 "could not construct netlist: failed to create pin '" + port_name +
"' at net '" + port_net->get_name() +
"' with ID " + std::to_string(port_net->get_id())
1619 Result<Module*> VerilogParser::instantiate_module(
const std::string& instance_identifier,
1620 VerilogModule* verilog_module,
1622 const std::unordered_map<std::string, std::string>& parent_module_assignments)
1624 std::unordered_map<std::string, std::string> signal_alias;
1625 std::unordered_map<std::string, std::string> instance_alias;
1629 const std::string parent_name = (parent ==
nullptr) ?
"" : parent->get_name();
1630 instance_alias[instance_identifier] = get_unique_alias(parent_name, instance_identifier, m_instance_name_occurences);
1634 if (parent ==
nullptr)
1644 std::string instance_type = verilog_module->m_name;
1647 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to create module");
1652 for (
const VerilogDataEntry& attribute : verilog_module->m_attributes)
1654 if (!
module->
set_data(
"attribute", attribute.m_name, attribute.m_type, attribute.m_value))
1657 "could not set attribute '{} = {}' of type '{}' for instance '{}' type '{}'.",
1661 instance_identifier,
1667 for (
const auto& port : verilog_module->m_ports)
1669 for (
const auto& expanded_port_identifier : port->m_expanded_identifiers)
1671 if (
const auto it = parent_module_assignments.find(expanded_port_identifier); it != parent_module_assignments.end())
1673 Net* port_net = m_net_by_name.at(it->second);
1674 m_module_ports[
module].push_back(std::make_tuple(expanded_port_identifier, port_net));
1675 m_module_port_by_net[port_net].push_back(std::make_pair(
module, m_module_ports[
module].
size() - 1));
1681 for (
const auto& signal : verilog_module->m_signals)
1683 for (
const auto& expanded_name : signal->m_expanded_names)
1685 std::string unique_net_name = get_unique_alias(
module->
get_name(), expanded_name, m_net_name_occurences);
1686 if (unique_net_name != expanded_name)
1688 m_net_name_occurences[unique_net_name]++;
1690 signal_alias[expanded_name] = unique_net_name;
1693 Net* signal_net = m_netlist->
create_net(signal_alias.at(expanded_name));
1694 if (signal_net ==
nullptr)
1696 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to create net '" + expanded_name +
"'");
1699 m_net_by_name[signal_alias.at(expanded_name)] = signal_net;
1702 for (
const VerilogDataEntry& attribute : signal->m_attributes)
1704 if (!signal_net->set_data(
"attribute", attribute.m_name,
"unknown", attribute.m_value))
1707 "could not set attribute ({} = {}) for net '{}' of instance '{}' of type '{}'.",
1711 instance_identifier,
1719 for (
const auto& [left_expanded_signal, right_expanded_signal] : verilog_module->m_expanded_assignments)
1721 std::string a = left_expanded_signal;
1722 std::string b = right_expanded_signal;
1724 if (
const auto alias_it = signal_alias.find(a); alias_it != signal_alias.end())
1726 a = alias_it->second;
1730 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to find alias for net '" + a +
"'");
1733 if (
const auto alias_it = signal_alias.find(b); alias_it != signal_alias.end())
1735 b = alias_it->second;
1737 else if (b ==
"'Z'" || b ==
"'X'")
1741 else if (b !=
"'0'" && b !=
"'1'")
1743 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to find alias for net '" + b +
"'");
1746 m_nets_to_merge.push_back(std::make_pair(a, b));
1750 for (
const auto& [port_identifier, net_name] : parent_module_assignments)
1752 std::string signal_name;
1753 if (
const auto expr_it = verilog_module->m_expanded_port_identifiers_to_expressions.find(port_identifier); expr_it == verilog_module->m_expanded_port_identifiers_to_expressions.end())
1755 signal_name = port_identifier;
1759 signal_name = expr_it->second;
1763 if (
const auto alias_it = signal_alias.find(signal_name); alias_it != signal_alias.end())
1765 const bool swap = net_name.find(
"__GLOBAL_IO__") == std::string::npos;
1766 m_nets_to_merge.push_back(swap ? std::make_pair(net_name, alias_it->second) : std::make_pair(alias_it->second, net_name));
1770 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to find alias for net '" + signal_name +
"'");
1775 for (
const auto&
instance : verilog_module->m_instances)
1778 DataContainer* container =
nullptr;
1781 std::unordered_map<std::string, std::string> instance_assignments;
1784 if (
auto module_it = m_modules_by_name.find(
instance->m_type); module_it != m_modules_by_name.end())
1787 for (
const auto& [port, assignment] :
instance->m_expanded_port_assignments)
1789 if (
const auto alias_it = signal_alias.find(assignment); alias_it != signal_alias.end())
1791 instance_assignments[port] = alias_it->second;
1793 else if (assignment ==
"'0'" || assignment ==
"'1'")
1795 instance_assignments[port] = assignment;
1797 else if (assignment ==
"'Z'" || assignment ==
"'X'" || assignment.empty())
1803 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': port assignment '" + port +
" = " + assignment +
"' is invalid");
1807 if (
auto res = instantiate_module(
instance->m_name, module_it->second,
module, instance_assignments); res.is_error())
1810 "could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': unable to create instance '" +
instance->m_name +
"' of type '"
1811 + module_it->second->m_name +
"'");
1815 container = res.get();
1819 else if (
const auto gate_type_it = m_gate_types.find(
instance->m_type); gate_type_it != m_gate_types.end())
1824 if (new_gate ==
nullptr)
1826 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to create gate '" +
instance->m_name +
"'");
1834 container = new_gate;
1837 if (m_vcc_gate_types.find(
instance->m_type) != m_vcc_gate_types.end() && !new_gate->mark_vcc_gate())
1839 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to mark '" +
instance->m_name +
"' of type '" +
instance->m_type
1842 if (m_gnd_gate_types.find(
instance->m_type) != m_gnd_gate_types.end() && !new_gate->mark_gnd_gate())
1844 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to mark '" +
instance->m_name +
"' of type '" +
instance->m_type
1849 std::unordered_map<std::string, GatePin*> pin_names_map;
1850 for (
auto* pin : gate_type_it->second->get_pins())
1852 pin_names_map[pin->get_name()] = pin;
1856 for (
const auto& [pin, assignment] :
instance->m_expanded_port_assignments)
1860 if (
const auto alias_it = signal_alias.find(assignment); alias_it != signal_alias.end())
1862 signal = alias_it->second;
1864 else if (assignment ==
"'0'" || assignment ==
"'1'")
1866 signal = assignment;
1868 else if (assignment ==
"'Z'" || assignment ==
"'X'")
1874 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to assign '" + assignment +
"' to pin '" + pin +
"' of gate '"
1875 +
instance->m_name +
"' of type '" +
instance->m_type +
"' as the assignment is invalid");
1879 if (
const auto net_it = m_net_by_name.find(signal); net_it == m_net_by_name.end())
1881 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to assign signal'" + signal +
"' to pin '" + pin
1882 +
"' as the signal has not been declared");
1886 Net* current_net = net_it->second;
1889 bool is_input =
false;
1890 bool is_output =
false;
1892 if (
const auto it = pin_names_map.find(pin); it != pin_names_map.end())
1906 if (!is_input && !is_output)
1908 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to assign net '" + signal +
"' to pin '" + pin
1909 +
"' as it is not a pin of gate '" + new_gate->get_name() +
"' of type '" + new_gate->get_type()->get_name() +
"'");
1912 if (is_output && !current_net->add_source(new_gate, pin))
1914 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to add net '" + signal +
"' as a source to gate '"
1915 + new_gate->get_name() +
"' via pin '" + pin +
"'");
1918 if (is_input && !current_net->add_destination(new_gate, pin))
1920 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to add net '" + signal +
"' as a destination to gate '"
1921 + new_gate->get_name() +
"' via pin '" + pin +
"'");
1928 return ERR(
"could not create instance '" + instance_identifier +
"' of type '" + instance_type +
"': failed to find gate type '" +
instance->m_type +
"' in gate library '"
1933 for (
const auto& attribute :
instance->m_attributes)
1935 if (!container->set_data(
"attribute", attribute.m_name, attribute.m_type, attribute.m_value))
1938 "could not set attribute '{} = {}' of type '{}' for instance '{}' of type '{}' within instance '{}' of type '{}'.",
1944 instance_identifier,
1951 const GateType* gt =
nullptr;
1952 if (
const Gate* g =
dynamic_cast<const Gate*
>(container))
1957 for (
const auto& parameter :
instance->m_parameters)
1959 if (!parameter.m_parameter.has_value())
1962 "verilog_parser",
"could not set generic '{}' for instance '{}' of type '{}': no typed parameter declaration available.", parameter.m_name,
instance->m_name,
instance->m_type);
1965 Parameter effective_param = parameter.m_parameter.value();
1968 if (
auto decl = gt->get_parameter(parameter.m_name); decl.is_ok())
1970 effective_param = decl.get();
1973 if (
auto res = container->set_parameter(effective_param, parameter.m_value); res.is_error())
1976 "could not set parameter '{} = {}' for instance '{}' of type '{}' within instance '{}' of type '{}': {}",
1981 instance_identifier,
1983 res.get_error().get());
1997 static const std::map<char, BooleanFunction::Value> bin_map = {{
'0', BooleanFunction::Value::ZERO},
1998 {
'1', BooleanFunction::Value::ONE},
1999 {
'X', BooleanFunction::Value::X},
2000 {
'Z', BooleanFunction::Value::Z}};
2002 static const std::map<char, std::vector<BooleanFunction::Value>> oct_map = {{
'0', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO}},
2003 {
'1', {BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO}},
2004 {
'2', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO}},
2005 {
'3', {BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO}},
2006 {
'4', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE}},
2007 {
'5', {BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE}},
2008 {
'6', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE}},
2009 {
'7', {BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE}},
2010 {
'X', {BooleanFunction::Value::X, BooleanFunction::Value::X, BooleanFunction::Value::X}},
2011 {
'Z', {BooleanFunction::Value::Z, BooleanFunction::Value::Z, BooleanFunction::Value::Z}}};
2013 static const std::map<char, std::vector<BooleanFunction::Value>> hex_map = {
2014 {
'0', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO}},
2015 {
'1', {BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO}},
2016 {
'2', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO}},
2017 {
'3', {BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO}},
2018 {
'4', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO}},
2019 {
'5', {BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO}},
2020 {
'6', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO}},
2021 {
'7', {BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO}},
2022 {
'8', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE}},
2023 {
'9', {BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE}},
2024 {
'A', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE}},
2025 {
'B', {BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE}},
2026 {
'C', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE}},
2027 {
'D', {BooleanFunction::Value::ONE, BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE}},
2028 {
'E', {BooleanFunction::Value::ZERO, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE}},
2029 {
'F', {BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE, BooleanFunction::Value::ONE}},
2030 {
'X', {BooleanFunction::Value::X, BooleanFunction::Value::X, BooleanFunction::Value::X, BooleanFunction::Value::X}},
2031 {
'Z', {BooleanFunction::Value::Z, BooleanFunction::Value::Z, BooleanFunction::Value::Z, BooleanFunction::Value::Z}}};
2035 std::string VerilogParser::get_unique_alias(
const std::string& parent_name,
const std::string&
name,
const std::unordered_map<std::string, u32>& name_occurences)
const
2037 std::string unique_alias =
name;
2039 if (!parent_name.empty())
2043 auto instance_name_it = name_occurences.find(
name);
2049 while (instance_name_it != name_occurences.end() && (cnt || instance_name_it->second > 1))
2051 std::string extension;
2054 extension =
"_u" + std::to_string(cnt);
2056 unique_alias = parent_name + instance_name_seperator + unique_alias + extension;
2057 instance_name_it = name_occurences.find(unique_alias);
2061 return unique_alias;
2064 std::vector<u32> VerilogParser::parse_range(TokenStream<std::string>& stream)
const
2066 if (stream.remaining() == 1)
2068 return {(
u32)std::stoi(stream.consume().string)};
2072 const int end = std::stoi(stream.consume().string);
2073 stream.consume(
":",
true);
2074 const int start = std::stoi(stream.consume().string);
2076 const int direction = (start <= end) ? 1 : -1;
2078 std::vector<u32> result;
2081 result.push_back((
u32)i);
2086 void VerilogParser::expand_ranges_recursively(std::vector<std::string>& expanded_names,
const std::string& current_name,
const std::vector<std::vector<u32>>& ranges,
u32 dimension)
const
2089 if (ranges.size() > dimension)
2091 for (
const u32 index : ranges[dimension])
2093 expand_ranges_recursively(expanded_names, current_name +
"(" + std::to_string(
index) +
")", ranges, dimension + 1);
2099 expanded_names.push_back(current_name);
2103 std::vector<std::string> VerilogParser::expand_ranges(
const std::string&
name,
const std::vector<std::vector<u32>>& ranges)
const
2105 std::vector<std::string> res;
2107 expand_ranges_recursively(res,
name, ranges, 0);
2112 Result<std::vector<BooleanFunction::Value>> VerilogParser::get_binary_vector(std::string value)
const
2119 std::vector<BooleanFunction::Value> result;
2122 if (value.find(
'\'') == std::string::npos)
2129 if (value.at(0) !=
'\'')
2131 len = std::stoi(value.substr(0, value.find(
'\'')));
2133 prefix = value.substr(value.find(
'\'') + 1, 1);
2134 number = value.substr(value.find(
'\'') + 2);
2138 switch (prefix.at(0))
2141 for (
auto it = number.rbegin(); it != number.rend(); it++)
2144 if (c ==
'0' || c ==
'1' || c ==
'Z' || c ==
'X')
2146 result.push_back(bin_map.at(c));
2150 return ERR(
"could not convert string to binary vector: invalid character within binary number literal '" + value +
"'");
2157 for (
auto it = number.rbegin(); it != number.rend(); it++)
2160 if ((c >=
'0' && c <=
'7') || c ==
'X' || c ==
'Z')
2162 const auto& bits = oct_map.at(c);
2163 result.insert(result.end(), bits.begin(), bits.end());
2167 return ERR(
"could not convert string to binary vector: invalid character within octal number literal '" + value +
"'");
2175 for (
const char c : number)
2177 if ((c >=
'0' && c <=
'9'))
2179 tmp_val = (tmp_val * 10) + (c -
'0');
2183 return ERR(
"could not convert string to binary vector: invalid character within decimal number literal '" + value +
"'");
2189 result.push_back(((tmp_val & 1) == 1) ? BooleanFunction::Value::ONE : BooleanFunction::Value::ZERO);
2191 }
while (tmp_val != 0);
2196 for (
auto it = number.rbegin(); it != number.rend(); it++)
2199 if ((c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'F') || c ==
'X' || c ==
'Z')
2201 const auto& bits = hex_map.at(c);
2202 result.insert(result.end(), bits.begin(), bits.end());
2206 return ERR(
"could not convert string to binary vector: invalid character within hexadecimal number literal '" + value +
"'");
2213 return ERR(
"could not convert string to binary vector: invalid base '" + prefix +
"' within number literal '" + value +
"'");
2219 i32 result_size = result.size();
2221 if (len > result_size)
2224 for (
i32 i = 0; i < (len - result_size); i++)
2226 result.push_back(BooleanFunction::Value::ZERO);
2232 for (
i32 i = 0; i < (result_size - len); i++)
2242 Result<std::string> VerilogParser::get_hex_from_literal(
const Token<std::string>& value_token)
const
2244 const u32 line_number = value_token.number;
2253 if (value.find(
'\'') == std::string::npos)
2260 if (value.at(0) !=
'\'')
2262 len = std::stoi(value.substr(0, value.find(
'\'')));
2264 prefix = value.substr(value.find(
'\'') + 1, 1);
2265 number = value.substr(value.find(
'\'') + 2);
2269 switch (prefix.at(0))
2272 if (!std::all_of(number.begin(), number.end(), [](
const char& c) { return (c >=
'0' && c <=
'1'); }))
2274 return ERR(
"could not convert token to hexadecimal string: invalid character within binary number literal '" + value +
"' (line " + std::to_string(line_number) +
")");
2282 if (!std::all_of(number.begin(), number.end(), [](
const char& c) { return (c >=
'0' && c <=
'7'); }))
2284 return ERR(
"could not convert token to hexadecimal string: invalid character within ocatl number literal '" + value +
"' (line " + std::to_string(line_number) +
")");
2292 if (!std::all_of(number.begin(), number.end(), [](
const char& c) { return (c >=
'0' && c <=
'9'); }))
2294 return ERR(
"could not convert token to hexadecimal string: invalid character within decimal number literal '" + value +
"' (line " + std::to_string(line_number) +
")");
2304 for (
const char c : number)
2306 if ((c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'F'))
2312 return ERR(
"could not convert token to hexadecimal string: invalid character within hexadecimal number literal '" + value +
"' (line " + std::to_string(line_number) +
")");
2320 return ERR(
"could not convert token to hexadecimal string: invalid base '" + prefix +
"' within number literal '" + value +
"' (line " + std::to_string(line_number) +
")");
2324 std::stringstream ss;
2328 ss << std::uppercase << std::setfill(
'0') << std::setw((len + 3) / 4) << std::hex << stoull(number, 0, base);
2332 ss << std::uppercase << std::hex << stoull(number, 0, base);
2334 return OK(ss.str());
2337 Result<std::pair<Parameter, std::string>> VerilogParser::parse_parameter_value(
const std::string&
name,
const Token<std::string>& value_token)
const
2339 const std::string& raw = value_token.string;
2340 const u32 line_number = value_token.number;
2341 const std::string loc_str =
" (line " + std::to_string(line_number) +
")";
2347 if (param_res.is_error())
2349 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2351 return OK(std::make_pair(param_res.get(), raw));
2355 if (is_double_strict(raw))
2358 if (param_res.is_error())
2360 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2362 return OK(std::make_pair(param_res.get(), raw));
2368 if (lower ==
"true" || lower ==
"false")
2371 if (param_res.is_error())
2373 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2375 return OK(std::make_pair(param_res.get(), lower));
2382 static const std::vector<std::string> time_units = {
"min",
"fs",
"ps",
"ns",
"us",
"ms",
"s",
"h"};
2383 for (
const auto& unit : time_units)
2389 const std::string num_part = raw.substr(0, raw.size() - unit.size());
2395 if (param_res.is_error())
2397 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2399 return OK(std::make_pair(param_res.get(), raw));
2404 if (raw.size() >= 2 && raw.front() ==
'"' && raw.back() ==
'"')
2407 if (param_res.is_error())
2409 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2411 return OK(std::make_pair(param_res.get(), raw.substr(1, raw.size() - 2)));
2415 if (!raw.empty() && (isdigit(
static_cast<unsigned char>(raw[0])) || raw[0] ==
'\''))
2417 const size_t tick_pos = raw.find(
'\'');
2418 if (tick_pos == std::string::npos)
2420 return ERR(
"could not parse parameter value: no base prefix in literal '" + raw +
"'" + loc_str);
2424 u16 explicit_width = 0;
2425 bool has_width =
false;
2430 explicit_width =
static_cast<u16>(std::stoul(raw.substr(0, tick_pos)));
2434 return ERR(
"could not parse parameter value: invalid width in literal '" + raw +
"'" + loc_str);
2439 if (tick_pos + 1 >= raw.size())
2441 return ERR(
"could not parse parameter value: missing base char in literal '" + raw +
"'" + loc_str);
2443 const char base_upper =
static_cast<char>(std::toupper(
static_cast<unsigned char>(raw[tick_pos + 1])));
2446 std::string numeric_set;
2447 std::string out_prefix;
2448 int bits_per_digit = 0;
2449 bool allow_state =
true;
2458 numeric_set =
"01234567";
2463 numeric_set =
"0123456789abcdef";
2468 numeric_set =
"0123456789";
2471 allow_state =
false;
2474 return ERR(
"could not parse parameter value: unsupported base '" + std::string(1, base_upper) +
"' in literal '" + raw +
"'" + loc_str);
2479 digits.reserve(raw.size());
2480 bool has_state =
false;
2481 for (
size_t i = tick_pos + 2; i < raw.size(); ++i)
2483 const char c = raw[i];
2488 const char mapped = (c ==
'?') ?
'-' : c;
2489 const char l = (mapped ==
'-') ?
'-' :
static_cast<char>(std::tolower(
static_cast<unsigned char>(mapped)));
2490 const bool is_num = numeric_set.find(l) != std::string::npos;
2491 const bool is_state = (l ==
'x' || l ==
'z' || l ==
'-');
2492 if (!is_num && (!allow_state || !is_state))
2494 return ERR(
"could not parse parameter value: literal '" + raw +
"' contains invalid digit '" + std::string(1, c) +
"'" + loc_str);
2505 return ERR(
"could not parse parameter value: no digits in literal '" + raw +
"'" + loc_str);
2509 const auto make_param = [&](
u16 size,
const std::string& value) -> Result<std::pair<Parameter, std::string>> {
2513 if (param_res.is_error())
2515 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2517 return OK(std::make_pair(param_res.get(), value));
2522 if (param_res.is_error())
2524 return ERR_APPEND(param_res.get_error(),
"could not parse parameter value '" + raw +
"'" + loc_str);
2526 return OK(std::make_pair(param_res.get(), value));
2531 if (bits_per_digit == 0)
2536 num = std::stoull(digits);
2540 return ERR(
"could not parse parameter value: decimal literal '" + raw +
"' overflows" + loc_str);
2542 std::stringstream ss;
2543 ss << std::hex << num;
2544 const std::string hex_digits = ss.str();
2545 const u16 size = has_width ? explicit_width :
static_cast<u16>(hex_digits.size() * 4);
2546 return make_param(
size, out_prefix + hex_digits);
2548 const u16 size = (has_width && !has_state) ? explicit_width :
static_cast<u16>(digits.size() * bits_per_digit);
2549 return make_param(
size, out_prefix + digits);
2552 return ERR(
"could not parse parameter value: failed to identify data type of '" + raw +
"'" + loc_str);
2555 Result<std::vector<VerilogParser::assignment_t>> VerilogParser::parse_assignment_expression(TokenStream<std::string>&& stream)
const
2557 std::vector<TokenStream<std::string>> parts;
2559 if (stream.size() == 0)
2564 if (stream.peek() ==
"{")
2566 stream.consume(
"{",
true);
2568 TokenStream<std::string> assignment_list_str = stream.extract_until(
"}");
2569 stream.consume(
"}",
true);
2573 parts.push_back(assignment_list_str.extract_until(
","));
2574 }
while (assignment_list_str.consume(
",",
false));
2578 parts.push_back(stream);
2581 std::vector<assignment_t> result;
2582 result.reserve(parts.size());
2584 for (
auto it = parts.rbegin(); it != parts.rend(); it++)
2586 TokenStream<std::string>& part_stream = *it;
2588 const Token<std::string> signal_name_token = part_stream.consume();
2589 std::string signal_name = signal_name_token.string;
2592 if (isdigit(signal_name[0]) || signal_name[0] ==
'\'')
2594 if (
auto res = get_binary_vector(signal_name_token); res.is_error())
2596 return ERR_APPEND(res.get_error(),
"could not parse assignment expression: unable to convert token to binary vector");
2600 result.push_back(std::move(res.get()));
2606 if (part_stream.consume(
"["))
2611 std::vector<std::vector<u32>> ranges;
2614 TokenStream<std::string> range_str = part_stream.extract_until(
"]");
2615 ranges.emplace_back(parse_range(range_str));
2616 part_stream.consume(
"]",
true);
2617 }
while (part_stream.consume(
"[",
false));
2619 result.push_back(ranged_identifier_t({std::move(signal_name), std::move(ranges)}));
2625 result.push_back(std::move(signal_name));
2633 std::vector<std::string> VerilogParser::expand_assignment_expression(VerilogModule* verilog_module,
const std::vector<assignment_t>& vars)
const
2635 std::vector<std::string> result;
2636 for (
const auto& var : vars)
2640 std::vector<std::vector<u32>> ranges;
2642 if (
const auto signal_it = verilog_module->m_signals_by_name.find(*
identifier); signal_it != verilog_module->m_signals_by_name.end())
2644 ranges = signal_it->second->m_ranges;
2646 else if (
const auto port_it = verilog_module->m_ports_by_expression.find(*
identifier); port_it != verilog_module->m_ports_by_expression.end())
2648 ranges = port_it->second->m_ranges;
2651 std::vector<std::string> expanded = expand_ranges(*
identifier, ranges);
2652 result.insert(result.end(), expanded.begin(), expanded.end());
2654 else if (
const ranged_identifier_t* ranged_identifier = std::get_if<ranged_identifier_t>(&var); ranged_identifier !=
nullptr)
2656 std::vector<std::string> expanded = expand_ranges(ranged_identifier->first, ranged_identifier->second);
2657 result.insert(result.end(), expanded.begin(), expanded.end());
2659 else if (
const numeral_t* numeral = std::get_if<numeral_t>(&var); numeral !=
nullptr)
2661 for (
auto value : *numeral)
static std::string to_string(Value value)
bool set_data(const std::string &category, const std::string &key, const std::string &data_type, const std::string &value, const bool log_with_info_level=false)
std::unordered_map< std::string, GateType * > get_gate_types(const std::function< bool(const GateType *)> &filter=nullptr) const
std::unordered_map< std::string, GateType * > get_vcc_gate_types() const
std::unordered_map< std::string, GateType * > get_gnd_gate_types() const
std::string get_name() const
bool is_input_net(Net *net) const
void set_name(const std::string &name)
bool is_top_module() const
bool assign_gate(Gate *gate)
bool is_output_net(Net *net) const
std::string get_name() const
const std::unordered_set< Net * > & get_input_nets() const
void set_type(const std::string &type)
Result< ModulePin * > create_pin(const u32 id, const std::string &name, Net *net, PinType type=PinType::none, bool create_group=true, bool force_name=false)
const std::unordered_set< Net * > & get_output_nets() const
Endpoint * add_source(Gate *gate, const std::string &pin_name)
const std::string & get_name() const
u32 get_num_of_destinations(const std::function< bool(Endpoint *ep)> &filter=nullptr) const
Module * get_top_module() const
bool mark_vcc_gate(Gate *gate)
bool mark_gnd_gate(Gate *gate)
bool load_gate_locations_from_data(const std::string &data_category="", const std::pair< std::string, std::string > &data_identifiers=std::pair< std::string, std::string >())
bool delete_net(Net *net)
Net * create_net(const u32 net_id, const std::string &name)
const std::vector< Gate * > & get_gnd_gates() const
void set_design_name(const std::string &name)
void enable_automatic_net_checks(bool enable_checks=true)
const std::vector< Gate * > & get_vcc_gates() const
const std::vector< Module * > & get_modules() const
Gate * create_gate(const u32 gate_id, GateType *gate_type, const std::string &name="", i32 x=-1, i32 y=-1)
const std::vector< Net * > & get_nets() const
const GateLibrary * get_gate_library() const
Module * create_module(const u32 module_id, const std::string &name, Module *parent, const std::vector< Gate * > &gates={})
Token< T > consume_until(const T &expected, u32 end=END_OF_STREAM, bool level_aware=true, bool throw_on_error=false)
Token< T > consume_current_line()
Token< T > & peek(i32 offset=0)
u32 find_next(const T &match, u32 end=END_OF_STREAM, bool level_aware=true) const
Token< T > join_until(const T &match, const T &joiner, u32 end=END_OF_STREAM, bool level_aware=true, bool throw_on_error=false)
TokenStream< T > extract_until(const T &expected, u32 end=END_OF_STREAM, bool level_aware=true, bool throw_on_error=false)
Token< T > consume(u32 num=1)
Result< std::monostate > parse(const std::filesystem::path &file_path) override
Result< std::unique_ptr< Netlist > > instantiate(const GateLibrary *gate_library) override
#define log_warning(channel,...)
#define ERR_APPEND(prev_error, message)
const Module * module(const Gate *g, const NodeBoxes &boxes)
std::unique_ptr< Netlist > create_netlist(const GateLibrary *gate_library)
Create a new empty netlist using the specified gate library.
T replace(const T &str, const T &search, const T &replace)
bool is_digits(const T &s)
std::string join(const std::string &joiner, const Iterator &begin, const Iterator &end, const Transform &transform)
bool ends_with(const T &s, const T &end)
bool is_integer(const T &s)
T trim(const T &s, const char *to_remove=" \t\r\n")
std::vector< PinInformation > pins
This file contains various functions to create and load netlists.
std::unique_ptr< BasePluginInterface > instance
static Result< Parameter > Boolean(const std::string &name, const std::string &default_value)
static Result< Parameter > BitVector(const std::string &name, u16 size, const std::string &default_value)
static Result< Parameter > Integer(const std::string &name, const std::string &default_value)
static Result< Parameter > String(const std::string &name, const std::string &default_value)
static Result< Parameter > LogicVector(const std::string &name, u16 size, const std::string &default_value)
static Result< Parameter > Time(const std::string &name, const std::string &default_value)
static Result< Parameter > Float(const std::string &name, const std::string &default_value)