8 pp
= pprint
.PrettyPrinter(indent
=4)
16 opts
, args
= getopt
.getopt(sys
.argv
[1:], "p:o:dg")
29 outfile
= "/dev/stdout"
32 assert a
.endswith(".pmg")
33 if prefix
is None and len(args
) == 1:
35 prefix
= prefix
.split('/')[-1]
38 assert prefix
is not None
40 current_pattern
= None
41 current_subpattern
= None
44 subpattern_args
= dict()
54 if s
[i
] in ("'", '"') and i
+ 1 < len(s
):
56 while j
+ 1 < len(s
) and s
[j
] != s
[i
]:
57 if s
[j
] == '\\' and j
+ 1 < len(s
):
64 if s
[i
] in ('$', '\\') and i
+ 1 < len(s
):
70 if ord('a') <= ord(s
[j
]) <= ord('z'):
73 if ord('A') <= ord(s
[j
]) <= ord('Z'):
76 if ord('0') <= ord(s
[j
]) <= ord('9'):
110 def process_pmgfile(f
, filename
):
112 global current_pattern
113 global current_subpattern
121 if len(cmd
) == 0 or cmd
[0].startswith("//"): continue
125 if current_pattern
is not None:
127 block
["type"] = "final"
128 block
["pattern"] = (current_pattern
, current_subpattern
)
131 assert len(line
) == 2
132 assert line
[1] not in patterns
133 current_pattern
= line
[1]
134 current_subpattern
= ""
135 patterns
[current_pattern
] = len(blocks
)
136 subpatterns
[(current_pattern
, current_subpattern
)] = len(blocks
)
137 subpattern_args
[(current_pattern
, current_subpattern
)] = list()
138 state_types
[current_pattern
] = dict()
139 udata_types
[current_pattern
] = dict()
142 assert current_pattern
is not None
144 if cmd
== "fallthrough":
146 block
["type"] = "fallthrough"
149 assert len(line
) == 1
152 if cmd
== "subpattern":
153 if len(blocks
) == 0 or blocks
[-1]["type"] != "fallthrough":
155 block
["type"] = "final"
156 block
["pattern"] = (current_pattern
, current_subpattern
)
158 elif len(blocks
) and blocks
[-1]["type"] == "fallthrough":
161 assert len(line
) == 2
162 current_subpattern
= line
[1]
163 subpattern_args
[(current_pattern
, current_subpattern
)] = list()
164 assert (current_pattern
, current_subpattern
) not in subpatterns
165 subpatterns
[(current_pattern
, current_subpattern
)] = len(blocks
)
171 subpattern_args
[(current_pattern
, current_subpattern
)] += line
[1:]
175 m
= re
.match(r
"^state\s+<(.*?)>\s+(([A-Za-z_][A-Za-z_0-9]*\s+)*[A-Za-z_][A-Za-z_0-9]*)\s*$", line
)
177 type_str
= m
.group(1)
178 states_str
= m
.group(2)
179 for s
in re
.split(r
"\s+", states_str
):
180 assert s
not in state_types
[current_pattern
]
181 state_types
[current_pattern
][s
] = type_str
185 m
= re
.match(r
"^udata\s+<(.*?)>\s+(([A-Za-z_][A-Za-z_0-9]*\s+)*[A-Za-z_][A-Za-z_0-9]*)\s*$", line
)
187 type_str
= m
.group(1)
188 udatas_str
= m
.group(2)
189 for s
in re
.split(r
"\s+", udatas_str
):
190 assert s
not in udata_types
[current_pattern
]
191 udata_types
[current_pattern
][s
] = type_str
196 block
["type"] = "match"
197 block
["src"] = "%s:%d" % (filename
, linenr
)
198 block
["pattern"] = (current_pattern
, current_subpattern
)
200 block
["genargs"] = None
201 block
["gencode"] = None
204 assert len(line
) == 2
205 assert (line
[1] not in state_types
[current_pattern
]) or (state_types
[current_pattern
][line
[1]] == "Cell*")
206 block
["cell"] = line
[1]
207 state_types
[current_pattern
][line
[1]] = "Cell*";
210 block
["setup"] = list()
211 block
["index"] = list()
212 block
["filter"] = list()
213 block
["sets"] = list()
214 block
["optional"] = False
215 block
["semioptional"] = False
222 if len(a
) == 0 or a
[0].startswith("//"): continue
223 if a
[0] == "endmatch": break
227 block
["if"].append(rewrite_cpp(b
.strip()))
232 block
["setup"].append(("select", rewrite_cpp(b
.strip())))
236 m
= re
.match(r
"^\s*slice\s+(\S+)\s+(.*?)\s*$", l
)
237 block
["setup"].append(("slice", m
.group(1), rewrite_cpp(m
.group(2))))
241 m
= re
.match(r
"^\s*choice\s+<(.*?)>\s+(\S+)\s+(.*?)\s*$", l
)
242 block
["setup"].append(("choice", m
.group(1), m
.group(2), rewrite_cpp(m
.group(3))))
246 m
= re
.match(r
"^\s*define\s+<(.*?)>\s+(\S+)\s+(.*?)\s*$", l
)
247 block
["setup"].append(("define", m
.group(1), m
.group(2), rewrite_cpp(m
.group(3))))
251 m
= re
.match(r
"^\s*index\s+<(.*?)>\s+(.*?)\s*===\s*(.*?)\s*$", l
)
253 block
["index"].append((m
.group(1), rewrite_cpp(m
.group(2)), rewrite_cpp(m
.group(3))))
258 block
["filter"].append(rewrite_cpp(b
.strip()))
262 m
= re
.match(r
"^\s*set\s+(\S+)\s+(.*?)\s*$", l
)
263 block
["sets"].append((m
.group(1), rewrite_cpp(m
.group(2))))
266 if a
[0] == "optional":
267 block
["optional"] = True
270 if a
[0] == "semioptional":
271 block
["semioptional"] = True
274 if a
[0] == "generate":
275 block
["genargs"] = list([int(s
) for s
in a
[1:]])
276 if len(block
["genargs"]) == 0: block
["genargs"].append(100)
277 if len(block
["genargs"]) == 1: block
["genargs"].append(0)
278 assert len(block
["genargs"]) == 2
279 block
["gencode"] = list()
285 if len(a
) == 1 and a
[0] == "endmatch": break
286 block
["gencode"].append(rewrite_cpp(l
.rstrip()))
289 raise RuntimeError("'%s' statement not recognised on line %d" % (a
[0], linenr
))
291 if block
["optional"]:
292 assert not block
["semioptional"]
299 block
["type"] = "code"
300 block
["src"] = "%s:%d" % (filename
, linenr
)
301 block
["pattern"] = (current_pattern
, current_subpattern
)
303 block
["code"] = list()
304 block
["fcode"] = list()
305 block
["states"] = set()
307 for s
in line
.split()[1:]:
308 if s
not in state_types
[current_pattern
]:
309 raise RuntimeError("'%s' not in state_types" % s
)
310 block
["states"].add(s
)
319 if len(a
) == 0: continue
320 if a
[0] == "endcode": break
322 if a
[0] == "finally":
326 block
[codetype
].append(rewrite_cpp(l
.rstrip()))
331 raise RuntimeError("'%s' command not recognised" % cmd
)
334 with
open(fn
, "r") as f
:
335 process_pmgfile(f
, fn
)
337 if current_pattern
is not None:
339 block
["type"] = "final"
340 block
["pattern"] = (current_pattern
, current_subpattern
)
343 current_pattern
= None
344 current_subpattern
= None
349 with
open(outfile
, "w") as f
:
351 print("// Generated by pmgen.py from {}".format(fn
), file=f
)
355 print("#include \"kernel/yosys.h\"", file=f
)
356 print("#include \"kernel/sigtools.h\"", file=f
)
358 print("YOSYS_NAMESPACE_BEGIN", file=f
)
361 print("struct {}_pm {{".format(prefix
), file=f
)
362 print(" Module *module;", file=f
)
363 print(" SigMap sigmap;", file=f
)
364 print(" std::function<void()> on_accept;", file=f
)
365 print(" bool setup_done;", file=f
)
366 print(" bool generate_mode;", file=f
)
367 print(" int accept_cnt;", file=f
)
370 print(" uint32_t rngseed;", file=f
)
371 print(" int rng(unsigned int n) {", file=f
)
372 print(" rngseed ^= rngseed << 13;", file=f
)
373 print(" rngseed ^= rngseed >> 17;", file=f
)
374 print(" rngseed ^= rngseed << 5;", file=f
)
375 print(" return rngseed % n;", file=f
)
379 for index
in range(len(blocks
)):
380 block
= blocks
[index
]
381 if block
["type"] == "match":
383 for entry
in block
["index"]:
384 index_types
.append(entry
[0])
385 value_types
= ["Cell*"]
386 for entry
in block
["setup"]:
387 if entry
[0] == "slice":
388 value_types
.append("int")
389 if entry
[0] == "choice":
390 value_types
.append(entry
[1])
391 if entry
[0] == "define":
392 value_types
.append(entry
[1])
393 print(" typedef std::tuple<{}> index_{}_key_type;".format(", ".join(index_types
), index
), file=f
)
394 print(" typedef std::tuple<{}> index_{}_value_type;".format(", ".join(value_types
), index
), file=f
)
395 print(" dict<index_{}_key_type, vector<index_{}_value_type>> index_{};".format(index
, index
, index
), file=f
)
396 print(" dict<SigBit, pool<Cell*>> sigusers;", file=f
)
397 print(" pool<Cell*> blacklist_cells;", file=f
)
398 print(" pool<Cell*> autoremove_cells;", file=f
)
399 print(" dict<Cell*,int> rollback_cache;", file=f
)
400 print(" int rollback;", file=f
)
403 for current_pattern
in sorted(patterns
.keys()):
404 print(" struct state_{}_t {{".format(current_pattern
), file=f
)
405 for s
, t
in sorted(state_types
[current_pattern
].items()):
406 print(" {} {};".format(t
, s
), file=f
)
407 print(" }} st_{};".format(current_pattern
), file=f
)
410 print(" struct udata_{}_t {{".format(current_pattern
), file=f
)
411 for s
, t
in sorted(udata_types
[current_pattern
].items()):
412 print(" {} {};".format(t
, s
), file=f
)
413 print(" }} ud_{};".format(current_pattern
), file=f
)
415 current_pattern
= None
417 for v
, n
in sorted(ids
.items()):
419 print(" IdString {}{{\"\\{}\"}};".format(v
, n
), file=f
)
421 print(" IdString {}{{\"{}\"}};".format(v
, n
), file=f
)
424 print(" void add_siguser(const SigSpec &sig, Cell *cell) {", file=f
)
425 print(" for (auto bit : sigmap(sig)) {", file=f
)
426 print(" if (bit.wire == nullptr) continue;", file=f
)
427 print(" sigusers[bit].insert(cell);", file=f
)
432 print(" void blacklist(Cell *cell) {", file=f
)
433 print(" if (cell != nullptr && blacklist_cells.insert(cell).second) {", file=f
)
434 print(" auto ptr = rollback_cache.find(cell);", file=f
)
435 print(" if (ptr == rollback_cache.end()) return;", file=f
)
436 print(" int rb = ptr->second;", file=f
)
437 print(" if (rollback == 0 || rollback > rb)", file=f
)
438 print(" rollback = rb;", file=f
)
443 print(" void autoremove(Cell *cell) {", file=f
)
444 print(" if (cell != nullptr) {", file=f
)
445 print(" autoremove_cells.insert(cell);", file=f
)
446 print(" blacklist(cell);", file=f
)
451 current_pattern
= None
453 print(" SigSpec port(Cell *cell, IdString portname) {", file=f
)
454 print(" return sigmap(cell->getPort(portname));", file=f
)
457 print(" SigSpec port(Cell *cell, IdString portname, const SigSpec& defval) {", file=f
)
458 print(" return sigmap(cell->connections_.at(portname, defval));", file=f
)
462 print(" Const param(Cell *cell, IdString paramname) {", file=f
)
463 print(" return cell->getParam(paramname);", file=f
)
466 print(" Const param(Cell *cell, IdString paramname, const Const& defval) {", file=f
)
467 print(" return cell->parameters.at(paramname, defval);", file=f
)
471 print(" int nusers(const SigSpec &sig) {", file=f
)
472 print(" pool<Cell*> users;", file=f
)
473 print(" for (auto bit : sigmap(sig))", file=f
)
474 print(" for (auto user : sigusers[bit])", file=f
)
475 print(" users.insert(user);", file=f
)
476 print(" return GetSize(users);", file=f
)
480 print(" {}_pm(Module *module, const vector<Cell*> &cells) :".format(prefix
), file=f
)
481 print(" module(module), sigmap(module), setup_done(false), generate_mode(false), rngseed(12345678) {", file=f
)
482 print(" setup(cells);", file=f
)
486 print(" {}_pm(Module *module) :".format(prefix
), file=f
)
487 print(" module(module), sigmap(module), setup_done(false), generate_mode(false), rngseed(12345678) {", file=f
)
491 print(" void setup(const vector<Cell*> &cells) {", file=f
)
492 for current_pattern
in sorted(patterns
.keys()):
493 for s
, t
in sorted(udata_types
[current_pattern
].items()):
495 print(" ud_{}.{} = nullptr;".format(current_pattern
,s
), file=f
)
497 print(" ud_{}.{} = {}();".format(current_pattern
, s
, t
), file=f
)
498 current_pattern
= None
499 print(" log_assert(!setup_done);", file=f
)
500 print(" setup_done = true;", file=f
)
501 print(" for (auto port : module->ports)", file=f
)
502 print(" add_siguser(module->wire(port), nullptr);", file=f
)
503 print(" for (auto cell : module->cells())", file=f
)
504 print(" for (auto &conn : cell->connections())", file=f
)
505 print(" add_siguser(conn.second, cell);", file=f
)
506 print(" for (auto cell : cells) {", file=f
)
508 for index
in range(len(blocks
)):
509 block
= blocks
[index
]
510 if block
["type"] == "match":
511 print(" do {", file=f
)
512 print(" Cell *{} = cell;".format(block
["cell"]), file=f
)
513 print(" index_{}_value_type value;".format(index
), file=f
)
514 print(" std::get<0>(value) = cell;", file=f
)
517 for item
in block
["setup"]:
518 if item
[0] == "select":
519 print(" if (!({})) continue;".format(item
[1]), file=f
)
520 if item
[0] == "slice":
521 print(" int &{} = std::get<{}>(value);".format(item
[1], valueidx
), file=f
)
522 print(" for ({} = 0; {} < {}; {}++) {{".format(item
[1], item
[1], item
[2], item
[1]), file=f
)
525 if item
[0] == "choice":
526 print(" vector<{}> _pmg_choices_{} = {};".format(item
[1], item
[2], item
[3]), file=f
)
527 print(" for (const {} &{} : _pmg_choices_{}) {{".format(item
[1], item
[2], item
[2]), file=f
)
528 print(" std::get<{}>(value) = {};".format(valueidx
, item
[2]), file=f
)
531 if item
[0] == "define":
532 print(" {} &{} = std::get<{}>(value);".format(item
[1], item
[2], valueidx
), file=f
)
533 print(" {} = {};".format(item
[2], item
[3]), file=f
)
535 print(" index_{}_key_type key;".format(index
), file=f
)
536 for field
, entry
in enumerate(block
["index"]):
537 print(" std::get<{}>(key) = {};".format(field
, entry
[1]), file=f
)
538 print(" index_{}[key].push_back(value);".format(index
), file=f
)
539 for i
in range(loopcnt
):
541 print(" } while (0);", file=f
)
547 print(" ~{}_pm() {{".format(prefix
), file=f
)
548 print(" for (auto cell : autoremove_cells)", file=f
)
549 print(" module->remove(cell);", file=f
)
553 for current_pattern
in sorted(patterns
.keys()):
554 print(" int run_{}(std::function<void()> on_accept_f) {{".format(current_pattern
), file=f
)
555 print(" log_assert(setup_done);", file=f
)
556 print(" accept_cnt = 0;", file=f
)
557 print(" on_accept = on_accept_f;", file=f
)
558 print(" rollback = 0;", file=f
)
559 for s
, t
in sorted(state_types
[current_pattern
].items()):
561 print(" st_{}.{} = nullptr;".format(current_pattern
, s
), file=f
)
563 print(" st_{}.{} = {}();".format(current_pattern
, s
, t
), file=f
)
564 print(" block_{}(1);".format(patterns
[current_pattern
]), file=f
)
565 print(" log_assert(rollback_cache.empty());", file=f
)
566 print(" return accept_cnt;", file=f
)
569 print(" int run_{}(std::function<void({}_pm&)> on_accept_f) {{".format(current_pattern
, prefix
), file=f
)
570 print(" return run_{}([&](){{on_accept_f(*this);}});".format(current_pattern
), file=f
)
573 print(" int run_{}() {{".format(current_pattern
), file=f
)
574 print(" return run_{}([](){{}});".format(current_pattern
, current_pattern
), file=f
)
579 for p
, s
in sorted(subpatterns
.keys()):
580 print(" void block_subpattern_{}_{}(int recursion) {{ block_{}(recursion); }}".format(p
, s
, subpatterns
[(p
, s
)]), file=f
)
583 current_pattern
= None
584 current_subpattern
= None
586 for index
in range(len(blocks
)):
587 block
= blocks
[index
]
589 if block
["type"] in ("match", "code"):
590 print(" // {}".format(block
["src"]), file=f
)
592 print(" void block_{}(int recursion YS_ATTRIBUTE(unused)) {{".format(index
), file=f
)
593 current_pattern
, current_subpattern
= block
["pattern"]
595 if block
["type"] == "final":
597 if index
+1 != len(blocks
):
605 for s
in subpattern_args
[(current_pattern
, current_subpattern
)]:
608 for i
in range(subpatterns
[(current_pattern
, current_subpattern
)], index
):
609 if blocks
[i
]["type"] == "code":
610 for s
in blocks
[i
]["states"]:
612 elif blocks
[i
]["type"] == "match":
613 const_st
.add(blocks
[i
]["cell"])
614 for item
in blocks
[i
]["sets"]:
615 const_st
.add(item
[0])
619 if block
["type"] == "code":
620 for s
in block
["states"]:
625 elif block
["type"] == "match":
627 assert s
not in const_st
629 for item
in block
["sets"]:
630 if item
[0] in const_st
:
631 const_st
.remove(item
[0])
632 nonconst_st
.add(item
[0])
636 for s
in sorted(const_st
):
637 t
= state_types
[current_pattern
][s
]
639 print(" {} const &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
641 print(" const {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
643 for s
in sorted(nonconst_st
):
644 t
= state_types
[current_pattern
][s
]
645 print(" {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
647 for u
in sorted(udata_types
[current_pattern
].keys()):
648 t
= udata_types
[current_pattern
][u
]
649 print(" {} &{} YS_ATTRIBUTE(unused) = ud_{}.{};".format(t
, u
, current_pattern
, u
), file=f
)
653 for s
in sorted(restore_st
):
654 t
= state_types
[current_pattern
][s
]
655 print(" {} _pmg_backup_{} = {};".format(t
, s
, s
), file=f
)
657 if block
["type"] == "code":
659 print("#define reject do { goto rollback_label; } while(0)", file=f
)
660 print("#define accept do { accept_cnt++; on_accept(); if (rollback) goto rollback_label; } while(0)", file=f
)
661 print("#define finish do { rollback = -1; goto rollback_label; } while(0)", file=f
)
662 print("#define branch do {{ block_{}(recursion+1); if (rollback) goto rollback_label; }} while(0)".format(index
+1), file=f
)
663 print("#define subpattern(pattern_name) do {{ block_subpattern_{}_ ## pattern_name (recursion+1); if (rollback) goto rollback_label; }} while(0)".format(current_pattern
), file=f
)
665 for line
in block
["code"]:
666 print(" " + line
, file=f
)
669 print(" block_{}(recursion+1);".format(index
+1), file=f
)
671 print("#undef reject", file=f
)
672 print("#undef accept", file=f
)
673 print("#undef finish", file=f
)
674 print("#undef branch", file=f
)
675 print("#undef subpattern", file=f
)
678 print("rollback_label:", file=f
)
679 print(" YS_ATTRIBUTE(unused);", file=f
)
681 if len(block
["fcode"]):
682 print("#define accept do { accept_cnt++; on_accept(); } while(0)", file=f
)
683 print("#define finish do { rollback = -1; goto finish_label; } while(0)", file=f
)
684 for line
in block
["fcode"]:
685 print(" " + line
, file=f
)
686 print("finish_label:", file=f
)
687 print(" YS_ATTRIBUTE(unused);", file=f
)
688 print("#undef accept", file=f
)
689 print("#undef finish", file=f
)
691 if len(restore_st
) or len(nonconst_st
):
693 for s
in sorted(restore_st
):
694 t
= state_types
[current_pattern
][s
]
695 print(" {} = _pmg_backup_{};".format(s
, s
), file=f
)
696 for s
in sorted(nonconst_st
):
697 if s
not in restore_st
:
698 t
= state_types
[current_pattern
][s
]
700 print(" {} = nullptr;".format(s
), file=f
)
702 print(" {} = {}();".format(s
, t
), file=f
)
704 elif block
["type"] == "match":
705 assert len(restore_st
) == 0
707 print(" Cell* _pmg_backup_{} = {};".format(block
["cell"], block
["cell"]), file=f
)
710 for expr
in block
["if"]:
712 print(" if (!({})) {{".format(expr
), file=f
)
713 print(" {} = nullptr;".format(block
["cell"]), file=f
)
714 print(" block_{}(recursion+1);".format(index
+1), file=f
)
715 print(" {} = _pmg_backup_{};".format(block
["cell"], block
["cell"]), file=f
)
716 print(" return;", file=f
)
720 print(" index_{}_key_type key;".format(index
), file=f
)
721 for field
, entry
in enumerate(block
["index"]):
722 print(" std::get<{}>(key) = {};".format(field
, entry
[2]), file=f
)
723 print(" auto cells_ptr = index_{}.find(key);".format(index
), file=f
)
725 if block
["semioptional"] or block
["genargs"] is not None:
726 print(" bool found_any_match = false;", file=f
)
729 print(" if (cells_ptr != index_{}.end()) {{".format(index
), file=f
)
730 print(" const vector<index_{}_value_type> &cells = cells_ptr->second;".format(index
), file=f
)
731 print(" for (int _pmg_idx = 0; _pmg_idx < GetSize(cells); _pmg_idx++) {", file=f
)
732 print(" {} = std::get<0>(cells[_pmg_idx]);".format(block
["cell"]), file=f
)
734 for item
in block
["setup"]:
735 if item
[0] == "slice":
736 print(" const int &{} YS_ATTRIBUTE(unused) = std::get<{}>(cells[_pmg_idx]);".format(item
[1], valueidx
), file=f
)
738 if item
[0] == "choice":
739 print(" const {} &{} YS_ATTRIBUTE(unused) = std::get<{}>(cells[_pmg_idx]);".format(item
[1], item
[2], valueidx
), file=f
)
741 if item
[0] == "define":
742 print(" const {} &{} YS_ATTRIBUTE(unused) = std::get<{}>(cells[_pmg_idx]);".format(item
[1], item
[2], valueidx
), file=f
)
744 print(" if (blacklist_cells.count({})) continue;".format(block
["cell"]), file=f
)
745 for expr
in block
["filter"]:
746 print(" if (!({})) continue;".format(expr
), file=f
)
747 if block
["semioptional"] or block
["genargs"] is not None:
748 print(" found_any_match = true;", file=f
)
749 for item
in block
["sets"]:
750 print(" auto _pmg_backup_{} = {};".format(item
[0], item
[0]), file=f
)
751 print(" {} = {};".format(item
[0], item
[1]), file=f
)
752 print(" auto rollback_ptr = rollback_cache.insert(make_pair(std::get<0>(cells[_pmg_idx]), recursion));", file=f
)
753 print(" block_{}(recursion+1);".format(index
+1), file=f
)
754 for item
in block
["sets"]:
755 print(" {} = _pmg_backup_{};".format(item
[0], item
[0]), file=f
)
756 print(" if (rollback_ptr.second)", file=f
)
757 print(" rollback_cache.erase(rollback_ptr.first);", file=f
)
758 print(" if (rollback) {", file=f
)
759 print(" if (rollback != recursion) {{".format(index
+1), file=f
)
760 print(" {} = _pmg_backup_{};".format(block
["cell"], block
["cell"]), file=f
)
761 print(" return;", file=f
)
763 print(" rollback = 0;", file=f
)
769 print(" {} = nullptr;".format(block
["cell"]), file=f
)
771 if block
["optional"]:
772 print(" block_{}(recursion+1);".format(index
+1), file=f
)
774 if block
["semioptional"]:
775 print(" if (!found_any_match) block_{}(recursion+1);".format(index
+1), file=f
)
777 print(" {} = _pmg_backup_{};".format(block
["cell"], block
["cell"]), file=f
)
779 if block
["genargs"] is not None:
780 print("#define finish do { rollback = -1; return; } while(0)", file=f
)
781 print(" if (generate_mode && rng(100) < (found_any_match ? {} : {})) {{".format(block
["genargs"][1], block
["genargs"][0]), file=f
)
782 for line
in block
["gencode"]:
783 print(" " + line
, file=f
)
785 print("#undef finish", file=f
)
789 current_pattern
= None
797 print("YOSYS_NAMESPACE_END", file=f
)