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()))
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 assert s
in state_types
[current_pattern
]
309 block
["states"].add(s
)
318 if len(a
) == 0: continue
319 if a
[0] == "endcode": break
321 if a
[0] == "finally":
325 block
[codetype
].append(rewrite_cpp(l
.rstrip()))
333 with
open(fn
, "r") as f
:
334 process_pmgfile(f
, fn
)
336 if current_pattern
is not None:
338 block
["type"] = "final"
339 block
["pattern"] = (current_pattern
, current_subpattern
)
342 current_pattern
= None
343 current_subpattern
= None
348 with
open(outfile
, "w") as f
:
350 print("// Generated by pmgen.py from {}".format(fn
), file=f
)
354 print("#include \"kernel/yosys.h\"", file=f
)
355 print("#include \"kernel/sigtools.h\"", file=f
)
357 print("YOSYS_NAMESPACE_BEGIN", file=f
)
360 print("struct {}_pm {{".format(prefix
), file=f
)
361 print(" Module *module;", file=f
)
362 print(" SigMap sigmap;", file=f
)
363 print(" std::function<void()> on_accept;", file=f
)
364 print(" bool generate_mode;", file=f
)
365 print(" int accept_cnt;", file=f
)
368 print(" uint32_t rngseed;", file=f
)
369 print(" int rng(unsigned int n) {", file=f
)
370 print(" rngseed ^= rngseed << 13;", file=f
)
371 print(" rngseed ^= rngseed >> 17;", file=f
)
372 print(" rngseed ^= rngseed << 5;", file=f
)
373 print(" return rngseed % n;", file=f
)
377 for index
in range(len(blocks
)):
378 block
= blocks
[index
]
379 if block
["type"] == "match":
381 for entry
in block
["index"]:
382 index_types
.append(entry
[0])
383 value_types
= ["Cell*"]
384 for entry
in block
["setup"]:
385 if entry
[0] == "slice":
386 value_types
.append("int")
387 if entry
[0] == "choice":
388 value_types
.append(entry
[1])
389 if entry
[0] == "define":
390 value_types
.append(entry
[1])
391 print(" typedef std::tuple<{}> index_{}_key_type;".format(", ".join(index_types
), index
), file=f
)
392 print(" typedef std::tuple<{}> index_{}_value_type;".format(", ".join(value_types
), index
), file=f
)
393 print(" dict<index_{}_key_type, vector<index_{}_value_type>> index_{};".format(index
, index
, index
), file=f
)
394 print(" dict<SigBit, pool<Cell*>> sigusers;", file=f
)
395 print(" pool<Cell*> blacklist_cells;", file=f
)
396 print(" pool<Cell*> autoremove_cells;", file=f
)
397 print(" dict<Cell*,int> rollback_cache;", file=f
)
398 print(" int rollback;", file=f
)
401 for current_pattern
in sorted(patterns
.keys()):
402 print(" struct state_{}_t {{".format(current_pattern
), file=f
)
403 for s
, t
in sorted(state_types
[current_pattern
].items()):
404 print(" {} {};".format(t
, s
), file=f
)
405 print(" }} st_{};".format(current_pattern
), file=f
)
408 print(" struct udata_{}_t {{".format(current_pattern
), file=f
)
409 for s
, t
in sorted(udata_types
[current_pattern
].items()):
410 print(" {} {};".format(t
, s
), file=f
)
411 print(" }} ud_{};".format(current_pattern
), file=f
)
413 current_pattern
= None
415 for v
, n
in sorted(ids
.items()):
417 print(" IdString {}{{\"\\{}\"}};".format(v
, n
), file=f
)
419 print(" IdString {}{{\"{}\"}};".format(v
, n
), file=f
)
422 print(" void add_siguser(const SigSpec &sig, Cell *cell) {", file=f
)
423 print(" for (auto bit : sigmap(sig)) {", file=f
)
424 print(" if (bit.wire == nullptr) continue;", file=f
)
425 print(" sigusers[bit].insert(cell);", file=f
)
430 print(" void blacklist(Cell *cell) {", file=f
)
431 print(" if (cell != nullptr && blacklist_cells.insert(cell).second) {", file=f
)
432 print(" auto ptr = rollback_cache.find(cell);", file=f
)
433 print(" if (ptr == rollback_cache.end()) return;", file=f
)
434 print(" int rb = ptr->second;", file=f
)
435 print(" if (rollback == 0 || rollback > rb)", file=f
)
436 print(" rollback = rb;", file=f
)
441 print(" void autoremove(Cell *cell) {", file=f
)
442 print(" if (cell != nullptr) {", file=f
)
443 print(" autoremove_cells.insert(cell);", file=f
)
444 print(" blacklist(cell);", file=f
)
449 current_pattern
= None
451 print(" SigSpec port(Cell *cell, IdString portname) {", file=f
)
452 print(" return sigmap(cell->getPort(portname));", file=f
)
456 print(" Const param(Cell *cell, IdString paramname) {", file=f
)
457 print(" return cell->getParam(paramname);", file=f
)
461 print(" int nusers(const SigSpec &sig) {", file=f
)
462 print(" pool<Cell*> users;", file=f
)
463 print(" for (auto bit : sigmap(sig))", file=f
)
464 print(" for (auto user : sigusers[bit])", file=f
)
465 print(" users.insert(user);", file=f
)
466 print(" return GetSize(users);", file=f
)
470 print(" {}_pm(Module *module, const vector<Cell*> &cells) :".format(prefix
), file=f
)
471 print(" module(module), sigmap(module), generate_mode(false), rngseed(12345678) {", file=f
)
472 for current_pattern
in sorted(patterns
.keys()):
473 for s
, t
in sorted(udata_types
[current_pattern
].items()):
475 print(" ud_{}.{} = nullptr;".format(current_pattern
,s
), file=f
)
477 print(" ud_{}.{} = {}();".format(current_pattern
, s
, t
), file=f
)
478 current_pattern
= None
479 print(" for (auto port : module->ports)", file=f
)
480 print(" add_siguser(module->wire(port), nullptr);", file=f
)
481 print(" for (auto cell : module->cells())", file=f
)
482 print(" for (auto &conn : cell->connections())", file=f
)
483 print(" add_siguser(conn.second, cell);", file=f
)
484 print(" for (auto cell : cells) {", file=f
)
486 for index
in range(len(blocks
)):
487 block
= blocks
[index
]
488 if block
["type"] == "match":
489 print(" do {", file=f
)
490 print(" Cell *{} = cell;".format(block
["cell"]), file=f
)
491 print(" index_{}_value_type value;".format(index
), file=f
)
492 print(" std::get<0>(value) = cell;", file=f
)
495 for item
in block
["setup"]:
496 if item
[0] == "select":
497 print(" if (!({})) continue;".format(item
[1]), file=f
)
498 if item
[0] == "slice":
499 print(" int &{} = std::get<{}>(value);".format(item
[1], valueidx
), file=f
)
500 print(" for ({} = 0; {} < {}; {}++) {{".format(item
[1], item
[1], item
[2], item
[1]), file=f
)
503 if item
[0] == "choice":
504 print(" vector<{}> _pmg_choices_{} = {};".format(item
[1], item
[2], item
[3]), file=f
)
505 print(" for (const {} &{} : _pmg_choices_{}) {{".format(item
[1], item
[2], item
[2]), file=f
)
506 print(" std::get<{}>(value) = {};".format(valueidx
, item
[2]), file=f
)
509 if item
[0] == "define":
510 print(" {} &{} = std::get<{}>(value);".format(item
[1], item
[2], valueidx
), file=f
)
511 print(" {} = {};".format(item
[2], item
[3]), file=f
)
513 print(" index_{}_key_type key;".format(index
), file=f
)
514 for field
, entry
in enumerate(block
["index"]):
515 print(" std::get<{}>(key) = {};".format(field
, entry
[1]), file=f
)
516 print(" index_{}[key].push_back(value);".format(index
), file=f
)
517 for i
in range(loopcnt
):
519 print(" } while (0);", file=f
)
525 print(" ~{}_pm() {{".format(prefix
), file=f
)
526 print(" for (auto cell : autoremove_cells)", file=f
)
527 print(" module->remove(cell);", file=f
)
531 for current_pattern
in sorted(patterns
.keys()):
532 print(" int run_{}(std::function<void()> on_accept_f) {{".format(current_pattern
), file=f
)
533 print(" accept_cnt = 0;", file=f
)
534 print(" on_accept = on_accept_f;", file=f
)
535 print(" rollback = 0;", file=f
)
536 for s
, t
in sorted(state_types
[current_pattern
].items()):
538 print(" st_{}.{} = nullptr;".format(current_pattern
, s
), file=f
)
540 print(" st_{}.{} = {}();".format(current_pattern
, s
, t
), file=f
)
541 print(" block_{}(1);".format(patterns
[current_pattern
]), file=f
)
542 print(" log_assert(rollback_cache.empty());", file=f
)
543 print(" return accept_cnt;", file=f
)
546 print(" int run_{}(std::function<void({}_pm&)> on_accept_f) {{".format(current_pattern
, prefix
), file=f
)
547 print(" return run_{}([&](){{on_accept_f(*this);}});".format(current_pattern
), file=f
)
550 print(" int run_{}() {{".format(current_pattern
), file=f
)
551 print(" return run_{}([](){{}});".format(current_pattern
, current_pattern
), file=f
)
556 for p
, s
in sorted(subpatterns
.keys()):
557 print(" void block_subpattern_{}_{}(int recursion) {{ block_{}(recursion); }}".format(p
, s
, subpatterns
[(p
, s
)]), file=f
)
560 current_pattern
= None
561 current_subpattern
= None
563 for index
in range(len(blocks
)):
564 block
= blocks
[index
]
566 if block
["type"] in ("match", "code"):
567 print(" // {}".format(block
["src"]), file=f
)
569 print(" void block_{}(int recursion YS_ATTRIBUTE(unused)) {{".format(index
), file=f
)
570 current_pattern
, current_subpattern
= block
["pattern"]
572 if block
["type"] == "final":
574 if index
+1 != len(blocks
):
582 for s
in subpattern_args
[(current_pattern
, current_subpattern
)]:
585 for i
in range(subpatterns
[(current_pattern
, current_subpattern
)], index
):
586 if blocks
[i
]["type"] == "code":
587 for s
in blocks
[i
]["states"]:
589 elif blocks
[i
]["type"] == "match":
590 const_st
.add(blocks
[i
]["cell"])
591 for item
in blocks
[i
]["sets"]:
592 const_st
.add(item
[0])
596 if block
["type"] == "code":
597 for s
in block
["states"]:
602 elif block
["type"] == "match":
604 assert s
not in const_st
606 for item
in block
["sets"]:
607 if item
[0] in const_st
:
608 const_st
.remove(item
[0])
609 nonconst_st
.add(item
[0])
613 for s
in sorted(const_st
):
614 t
= state_types
[current_pattern
][s
]
616 print(" {} const &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
618 print(" const {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
620 for s
in sorted(nonconst_st
):
621 t
= state_types
[current_pattern
][s
]
622 print(" {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
624 for u
in sorted(udata_types
[current_pattern
].keys()):
625 t
= udata_types
[current_pattern
][u
]
626 print(" {} &{} YS_ATTRIBUTE(unused) = ud_{}.{};".format(t
, u
, current_pattern
, u
), file=f
)
630 for s
in sorted(restore_st
):
631 t
= state_types
[current_pattern
][s
]
632 print(" {} _pmg_backup_{} = {};".format(t
, s
, s
), file=f
)
634 if block
["type"] == "code":
636 print("#define reject do { goto rollback_label; } while(0)", file=f
)
637 print("#define accept do { accept_cnt++; on_accept(); if (rollback) goto rollback_label; } while(0)", file=f
)
638 print("#define finish do { rollback = -1; goto rollback_label; } while(0)", file=f
)
639 print("#define branch do {{ block_{}(recursion+1); if (rollback) goto rollback_label; }} while(0)".format(index
+1), file=f
)
640 print("#define subpattern(pattern_name) do {{ block_subpattern_{}_ ## pattern_name (recursion+1); if (rollback) goto rollback_label; }} while(0)".format(current_pattern
), file=f
)
642 for line
in block
["code"]:
643 print(" " + line
, file=f
)
646 print(" block_{}(recursion+1);".format(index
+1), file=f
)
648 print("#undef reject", file=f
)
649 print("#undef accept", file=f
)
650 print("#undef finish", file=f
)
651 print("#undef branch", file=f
)
652 print("#undef subpattern", file=f
)
655 print("rollback_label:", file=f
)
656 print(" YS_ATTRIBUTE(unused);", file=f
)
658 if len(block
["fcode"]):
659 print("#define accept do { accept_cnt++; on_accept(); } while(0)", file=f
)
660 print("#define finish do { rollback = -1; goto finish_label; } while(0)", file=f
)
661 for line
in block
["fcode"]:
662 print(" " + line
, file=f
)
663 print("finish_label:", file=f
)
664 print(" YS_ATTRIBUTE(unused);", file=f
)
665 print("#undef accept", file=f
)
666 print("#undef finish", file=f
)
668 if len(restore_st
) or len(nonconst_st
):
670 for s
in sorted(restore_st
):
671 t
= state_types
[current_pattern
][s
]
672 print(" {} = _pmg_backup_{};".format(s
, s
), file=f
)
673 for s
in sorted(nonconst_st
):
674 if s
not in restore_st
:
675 t
= state_types
[current_pattern
][s
]
677 print(" {} = nullptr;".format(s
), file=f
)
679 print(" {} = {}();".format(s
, t
), file=f
)
681 elif block
["type"] == "match":
682 assert len(restore_st
) == 0
684 print(" Cell* _pmg_backup_{} = {};".format(block
["cell"], block
["cell"]), file=f
)
687 for expr
in block
["if"]:
689 print(" if (!({})) {{".format(expr
), file=f
)
690 print(" {} = nullptr;".format(block
["cell"]), file=f
)
691 print(" block_{}(recursion+1);".format(index
+1), file=f
)
692 print(" {} = _pmg_backup_{};".format(block
["cell"], block
["cell"]), file=f
)
693 print(" return;", file=f
)
697 print(" index_{}_key_type key;".format(index
), file=f
)
698 for field
, entry
in enumerate(block
["index"]):
699 print(" std::get<{}>(key) = {};".format(field
, entry
[2]), file=f
)
700 print(" auto cells_ptr = index_{}.find(key);".format(index
), file=f
)
702 if block
["semioptional"] or block
["genargs"] is not None:
703 print(" bool found_any_match = false;", file=f
)
706 print(" if (cells_ptr != index_{}.end()) {{".format(index
), file=f
)
707 print(" const vector<index_{}_value_type> &cells = cells_ptr->second;".format(index
), file=f
)
708 print(" for (int _pmg_idx = 0; _pmg_idx < GetSize(cells); _pmg_idx++) {", file=f
)
709 print(" {} = std::get<0>(cells[_pmg_idx]);".format(block
["cell"]), file=f
)
711 for item
in block
["setup"]:
712 if item
[0] == "slice":
713 print(" const int &{} YS_ATTRIBUTE(unused) = std::get<{}>(cells[_pmg_idx]);".format(item
[1], valueidx
), file=f
)
715 if item
[0] == "choice":
716 print(" const {} &{} YS_ATTRIBUTE(unused) = std::get<{}>(cells[_pmg_idx]);".format(item
[1], item
[2], valueidx
), file=f
)
718 if item
[0] == "define":
719 print(" const {} &{} YS_ATTRIBUTE(unused) = std::get<{}>(cells[_pmg_idx]);".format(item
[1], item
[2], valueidx
), file=f
)
721 print(" if (blacklist_cells.count({})) continue;".format(block
["cell"]), file=f
)
722 for expr
in block
["filter"]:
723 print(" if (!({})) continue;".format(expr
), file=f
)
724 if block
["semioptional"] or block
["genargs"] is not None:
725 print(" found_any_match = true;", file=f
)
726 for item
in block
["sets"]:
727 print(" auto _pmg_backup_{} = {};".format(item
[0], item
[0]), file=f
)
728 print(" {} = {};".format(item
[0], item
[1]), file=f
)
729 print(" auto rollback_ptr = rollback_cache.insert(make_pair(std::get<0>(cells[_pmg_idx]), recursion));", file=f
)
730 print(" block_{}(recursion+1);".format(index
+1), file=f
)
731 for item
in block
["sets"]:
732 print(" {} = _pmg_backup_{};".format(item
[0], item
[0]), file=f
)
733 print(" if (rollback_ptr.second)", file=f
)
734 print(" rollback_cache.erase(rollback_ptr.first);", file=f
)
735 print(" if (rollback) {", file=f
)
736 print(" if (rollback != recursion) {{".format(index
+1), file=f
)
737 print(" {} = _pmg_backup_{};".format(block
["cell"], block
["cell"]), file=f
)
738 print(" return;", file=f
)
740 print(" rollback = 0;", file=f
)
746 print(" {} = nullptr;".format(block
["cell"]), file=f
)
748 if block
["optional"]:
749 print(" block_{}(recursion+1);".format(index
+1), file=f
)
751 if block
["semioptional"]:
752 print(" if (!found_any_match) block_{}(recursion+1);".format(index
+1), file=f
)
754 print(" {} = _pmg_backup_{};".format(block
["cell"], block
["cell"]), file=f
)
756 if block
["genargs"] is not None:
757 print("#define finish do { rollback = -1; return; } while(0)", file=f
)
758 print(" if (generate_mode && rng(100) < (found_any_match ? {} : {})) {{".format(block
["genargs"][1], block
["genargs"][0]), file=f
)
759 for line
in block
["gencode"]:
760 print(" " + line
, file=f
)
762 print("#undef finish", file=f
)
766 current_pattern
= None
774 print("YOSYS_NAMESPACE_END", file=f
)