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
== "subpattern":
146 block
["type"] = "final"
147 block
["pattern"] = (current_pattern
, current_subpattern
)
150 assert len(line
) == 2
151 current_subpattern
= line
[1]
152 subpattern_args
[(current_pattern
, current_subpattern
)] = list()
153 assert (current_pattern
, current_subpattern
) not in subpatterns
154 subpatterns
[(current_pattern
, current_subpattern
)] = len(blocks
)
160 subpattern_args
[(current_pattern
, current_subpattern
)] += line
[1:]
164 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
)
166 type_str
= m
.group(1)
167 states_str
= m
.group(2)
168 for s
in re
.split(r
"\s+", states_str
):
169 assert s
not in state_types
[current_pattern
]
170 state_types
[current_pattern
][s
] = type_str
174 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
)
176 type_str
= m
.group(1)
177 udatas_str
= m
.group(2)
178 for s
in re
.split(r
"\s+", udatas_str
):
179 assert s
not in udata_types
[current_pattern
]
180 udata_types
[current_pattern
][s
] = type_str
185 block
["type"] = "match"
186 block
["src"] = "%s:%d" % (filename
, linenr
)
187 block
["pattern"] = (current_pattern
, current_subpattern
)
189 block
["genargs"] = None
190 block
["gencode"] = None
193 assert len(line
) == 2
194 assert (line
[1] not in state_types
[current_pattern
]) or (state_types
[current_pattern
][line
[1]] == "Cell*")
195 block
["cell"] = line
[1]
196 state_types
[current_pattern
][line
[1]] = "Cell*";
199 block
["select"] = list()
200 block
["index"] = list()
201 block
["filter"] = list()
202 block
["optional"] = False
203 block
["semioptional"] = False
210 if len(a
) == 0 or a
[0].startswith("//"): continue
211 if a
[0] == "endmatch": break
215 block
["if"].append(rewrite_cpp(b
.strip()))
220 block
["select"].append(rewrite_cpp(b
.strip()))
224 m
= re
.match(r
"^\s*index\s+<(.*?)>\s+(.*?)\s*===\s*(.*?)\s*$", l
)
226 block
["index"].append((m
.group(1), rewrite_cpp(m
.group(2)), rewrite_cpp(m
.group(3))))
231 block
["filter"].append(rewrite_cpp(b
.strip()))
234 if a
[0] == "optional":
235 block
["optional"] = True
238 if a
[0] == "semioptional":
239 block
["semioptional"] = True
242 if a
[0] == "generate":
243 block
["genargs"] = list([int(s
) for s
in a
[1:]])
244 block
["gencode"] = list()
245 assert len(block
["genargs"]) < 2
251 if a
[0] == "endmatch": break
252 block
["gencode"].append(rewrite_cpp(l
.rstrip()))
257 if block
["optional"]:
258 assert not block
["semioptional"]
265 block
["type"] = "code"
266 block
["src"] = "%s:%d" % (filename
, linenr
)
267 block
["pattern"] = (current_pattern
, current_subpattern
)
269 block
["code"] = list()
270 block
["fcode"] = list()
271 block
["states"] = set()
273 for s
in line
.split()[1:]:
274 assert s
in state_types
[current_pattern
]
275 block
["states"].add(s
)
284 if len(a
) == 0: continue
285 if a
[0] == "endcode": break
287 if a
[0] == "finally":
291 block
[codetype
].append(rewrite_cpp(l
.rstrip()))
299 with
open(fn
, "r") as f
:
300 process_pmgfile(f
, fn
)
302 if current_pattern
is not None:
304 block
["type"] = "final"
305 block
["pattern"] = (current_pattern
, current_subpattern
)
308 current_pattern
= None
309 current_subpattern
= None
314 with
open(outfile
, "w") as f
:
316 print("// Generated by pmgen.py from {}".format(fn
), file=f
)
320 print("#include \"kernel/yosys.h\"", file=f
)
321 print("#include \"kernel/sigtools.h\"", file=f
)
323 print("YOSYS_NAMESPACE_BEGIN", file=f
)
326 print("struct {}_pm {{".format(prefix
), file=f
)
327 print(" Module *module;", file=f
)
328 print(" SigMap sigmap;", file=f
)
329 print(" std::function<void()> on_accept;", file=f
)
330 print(" bool generate_mode;", file=f
)
331 print(" int accept_cnt;", file=f
)
334 print(" uint32_t rngseed;", file=f
)
335 print(" int rng(unsigned int n) {", file=f
)
336 print(" rngseed ^= rngseed << 13;", file=f
)
337 print(" rngseed ^= rngseed >> 17;", file=f
)
338 print(" rngseed ^= rngseed << 5;", file=f
)
339 print(" return rngseed % n;", file=f
)
343 for index
in range(len(blocks
)):
344 block
= blocks
[index
]
345 if block
["type"] == "match":
347 for entry
in block
["index"]:
348 index_types
.append(entry
[0])
349 print(" typedef std::tuple<{}> index_{}_key_type;".format(", ".join(index_types
), index
), file=f
)
350 print(" dict<index_{}_key_type, vector<Cell*>> index_{};".format(index
, index
), file=f
)
351 print(" dict<SigBit, pool<Cell*>> sigusers;", file=f
)
352 print(" pool<Cell*> blacklist_cells;", file=f
)
353 print(" pool<Cell*> autoremove_cells;", file=f
)
354 print(" bool blacklist_dirty;", file=f
)
355 print(" vector<pair<Cell*,int>> rollback_stack;", file=f
)
356 print(" int rollback;", file=f
)
359 for current_pattern
in sorted(patterns
.keys()):
360 print(" struct state_{}_t {{".format(current_pattern
), file=f
)
361 for s
, t
in sorted(state_types
[current_pattern
].items()):
362 print(" {} {};".format(t
, s
), file=f
)
363 print(" }} st_{};".format(current_pattern
), file=f
)
366 print(" struct udata_{}_t {{".format(current_pattern
), file=f
)
367 for s
, t
in sorted(udata_types
[current_pattern
].items()):
368 print(" {} {};".format(t
, s
), file=f
)
369 print(" }} ud_{};".format(current_pattern
), file=f
)
371 current_pattern
= None
373 for v
, n
in sorted(ids
.items()):
375 print(" IdString {}{{\"\\{}\"}};".format(v
, n
), file=f
)
377 print(" IdString {}{{\"{}\"}};".format(v
, n
), file=f
)
380 print(" void add_siguser(const SigSpec &sig, Cell *cell) {", file=f
)
381 print(" for (auto bit : sigmap(sig)) {", file=f
)
382 print(" if (bit.wire == nullptr) continue;", file=f
)
383 print(" if (sigusers.count(bit) == 0 && bit.wire->port_id)", file=f
)
384 print(" sigusers[bit].insert(nullptr);", file=f
)
385 print(" sigusers[bit].insert(cell);", file=f
)
390 print(" void blacklist(Cell *cell) {", file=f
)
391 print(" if (cell != nullptr) {", file=f
)
392 print(" if (blacklist_cells.insert(cell).second)", file=f
)
393 print(" blacklist_dirty = true;", file=f
)
398 print(" void check_blacklist() {", file=f
)
399 print(" if (!blacklist_dirty)", file=f
)
400 print(" return;", file=f
)
401 print(" blacklist_dirty = false;", file=f
)
402 print(" for (int i = 0; i < GetSize(rollback_stack); i++)", file=f
)
403 print(" if (blacklist_cells.count(rollback_stack[i].first)) {", file=f
)
404 print(" rollback = rollback_stack[i].second;", file=f
)
405 print(" rollback_stack.resize(i);", file=f
)
406 print(" return;", file=f
)
411 print(" void autoremove(Cell *cell) {", file=f
)
412 print(" if (cell != nullptr) {", file=f
)
413 print(" if (blacklist_cells.insert(cell).second)", file=f
)
414 print(" blacklist_dirty = true;", file=f
)
415 print(" autoremove_cells.insert(cell);", file=f
)
420 current_pattern
= None
422 print(" SigSpec port(Cell *cell, IdString portname) {", file=f
)
423 print(" return sigmap(cell->getPort(portname));", file=f
)
427 print(" Const param(Cell *cell, IdString paramname) {", file=f
)
428 print(" return cell->getParam(paramname);", file=f
)
432 print(" int nusers(const SigSpec &sig) {", file=f
)
433 print(" pool<Cell*> users;", file=f
)
434 print(" for (auto bit : sigmap(sig))", file=f
)
435 print(" for (auto user : sigusers[bit])", file=f
)
436 print(" users.insert(user);", file=f
)
437 print(" return GetSize(users);", file=f
)
441 print(" {}_pm(Module *module, const vector<Cell*> &cells) :".format(prefix
), file=f
)
442 print(" module(module), sigmap(module), generate_mode(false), rngseed(12345678) {", file=f
)
443 for current_pattern
in sorted(patterns
.keys()):
444 for s
, t
in sorted(udata_types
[current_pattern
].items()):
446 print(" ud_{}.{} = nullptr;".format(current_pattern
,s
), file=f
)
448 print(" ud_{}.{} = {}();".format(current_pattern
, s
, t
), file=f
)
449 current_pattern
= None
450 print(" for (auto cell : module->cells()) {", file=f
)
451 print(" for (auto &conn : cell->connections())", file=f
)
452 print(" add_siguser(conn.second, cell);", file=f
)
454 print(" for (auto cell : cells) {", file=f
)
456 for index
in range(len(blocks
)):
457 block
= blocks
[index
]
458 if block
["type"] == "match":
459 print(" do {", file=f
)
460 print(" Cell *{} = cell;".format(block
["cell"]), file=f
)
461 for expr
in block
["select"]:
462 print(" if (!({})) break;".format(expr
), file=f
)
463 print(" index_{}_key_type key;".format(index
), file=f
)
464 for field
, entry
in enumerate(block
["index"]):
465 print(" std::get<{}>(key) = {};".format(field
, entry
[1]), file=f
)
466 print(" index_{}[key].push_back(cell);".format(index
), file=f
)
467 print(" } while (0);", file=f
)
473 print(" ~{}_pm() {{".format(prefix
), file=f
)
474 print(" for (auto cell : autoremove_cells)", file=f
)
475 print(" module->remove(cell);", file=f
)
479 for current_pattern
in sorted(patterns
.keys()):
480 print(" int run_{}(std::function<void()> on_accept_f) {{".format(current_pattern
), file=f
)
481 print(" accept_cnt = 0;", file=f
)
482 print(" on_accept = on_accept_f;", file=f
)
483 print(" rollback = 0;", file=f
)
484 print(" blacklist_dirty = false;", file=f
)
485 for s
, t
in sorted(state_types
[current_pattern
].items()):
487 print(" st_{}.{} = nullptr;".format(current_pattern
, s
), file=f
)
489 print(" st_{}.{} = {}();".format(current_pattern
, s
, t
), file=f
)
490 print(" block_{}(1);".format(patterns
[current_pattern
]), file=f
)
491 print(" log_assert(rollback_stack.empty());", file=f
)
492 print(" return accept_cnt;", file=f
)
495 print(" int run_{}(std::function<void({}_pm&)> on_accept_f) {{".format(current_pattern
, prefix
), file=f
)
496 print(" return run_{}([&](){{on_accept_f(*this);}});".format(current_pattern
), file=f
)
499 print(" int run_{}() {{".format(current_pattern
), file=f
)
500 print(" return run_{}([](){{}});".format(current_pattern
, current_pattern
), file=f
)
505 for p
, s
in sorted(subpatterns
.keys()):
506 print(" void block_subpattern_{}_{}(int recursion) {{ block_{}(recursion); }}".format(p
, s
, subpatterns
[(p
, s
)]), file=f
)
509 current_pattern
= None
510 current_subpattern
= None
512 for index
in range(len(blocks
)):
513 block
= blocks
[index
]
515 if block
["type"] in ("match", "code"):
516 print(" // {}".format(block
["src"]), file=f
)
518 print(" void block_{}(int recursion YS_ATTRIBUTE(unused)) {{".format(index
), file=f
)
519 current_pattern
, current_subpattern
= block
["pattern"]
521 if block
["type"] == "final":
523 if index
+1 != len(blocks
):
531 for s
in subpattern_args
[(current_pattern
, current_subpattern
)]:
534 for i
in range(subpatterns
[(current_pattern
, current_subpattern
)], index
):
535 if blocks
[i
]["type"] == "code":
536 for s
in blocks
[i
]["states"]:
538 elif blocks
[i
]["type"] == "match":
539 const_st
.add(blocks
[i
]["cell"])
543 if block
["type"] == "code":
544 for s
in block
["states"]:
549 elif block
["type"] == "match":
551 assert s
not in const_st
556 for s
in sorted(const_st
):
557 t
= state_types
[current_pattern
][s
]
559 print(" {} const &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
561 print(" const {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
563 for s
in sorted(nonconst_st
):
564 t
= state_types
[current_pattern
][s
]
565 print(" {} &{} YS_ATTRIBUTE(unused) = st_{}.{};".format(t
, s
, current_pattern
, s
), file=f
)
567 for u
in sorted(udata_types
[current_pattern
].keys()):
568 t
= udata_types
[current_pattern
][u
]
569 print(" {} &{} YS_ATTRIBUTE(unused) = ud_{}.{};".format(t
, u
, current_pattern
, u
), file=f
)
573 for s
in sorted(restore_st
):
574 t
= state_types
[current_pattern
][s
]
575 print(" {} backup_{} = {};".format(t
, s
, s
), file=f
)
577 if block
["type"] == "code":
579 print("#define reject do { check_blacklist(); goto rollback_label; } while(0)", file=f
)
580 print("#define accept do { accept_cnt++; on_accept(); check_blacklist(); if (rollback) goto rollback_label; } while(0)", file=f
)
581 print("#define finish do { rollback = -1; rollback_stack.clean(); goto rollback_label; } while(0)", file=f
)
582 print("#define branch do {{ block_{}(recursion+1); if (rollback) goto rollback_label; }} while(0)".format(index
+1), file=f
)
583 print("#define subpattern(pattern_name) do {{ block_subpattern_{}_ ## pattern_name (recursion+1); if (rollback) goto rollback_label; }} while(0)".format(current_pattern
), file=f
)
585 for line
in block
["code"]:
586 print(" " + line
, file=f
)
589 print(" block_{}(recursion+1);".format(index
+1), file=f
)
591 print("#undef reject", file=f
)
592 print("#undef accept", file=f
)
593 print("#undef finish", file=f
)
594 print("#undef branch", file=f
)
595 print("#undef subpattern", file=f
)
598 print("rollback_label:", file=f
)
599 print(" YS_ATTRIBUTE(unused);", file=f
)
601 if len(block
["fcode"]):
602 print("#define accept do { accept_cnt++; on_accept(); check_blacklist(); } while(0)", file=f
)
603 print("#define finish do { rollback = -1; rollback_stack.clean(); return; } while(0)", file=f
)
604 for line
in block
["fcode"]:
605 print(" " + line
, file=f
)
606 print("#undef accept", file=f
)
607 print("#undef finish", file=f
)
609 if len(restore_st
) or len(nonconst_st
):
611 for s
in sorted(restore_st
):
612 t
= state_types
[current_pattern
][s
]
613 print(" {} = backup_{};".format(s
, s
), file=f
)
614 for s
in sorted(nonconst_st
):
615 if s
not in restore_st
:
616 t
= state_types
[current_pattern
][s
]
618 print(" {} = nullptr;".format(s
), file=f
)
620 print(" {} = {}();".format(s
, t
), file=f
)
622 elif block
["type"] == "match":
623 assert len(restore_st
) == 0
625 print(" Cell* backup_{} = {};".format(block
["cell"], block
["cell"]), file=f
)
628 for expr
in block
["if"]:
630 print(" if (!({})) {{".format(expr
), file=f
)
631 print(" {} = nullptr;".format(block
["cell"]), file=f
)
632 print(" block_{}(recursion+1);".format(index
+1), file=f
)
633 print(" {} = backup_{};".format(block
["cell"], block
["cell"]), file=f
)
634 print(" return;", file=f
)
638 print(" index_{}_key_type key;".format(index
), file=f
)
639 for field
, entry
in enumerate(block
["index"]):
640 print(" std::get<{}>(key) = {};".format(field
, entry
[2]), file=f
)
641 print(" auto cells_ptr = index_{}.find(key);".format(index
), file=f
)
643 if block
["semioptional"] or block
["genargs"] is not None:
644 print(" bool found_any_match = false;", file=f
)
647 print(" if (cells_ptr != index_{}.end()) {{".format(index
), file=f
)
648 print(" const vector<Cell*> &cells = cells_ptr->second;".format(index
), file=f
)
649 print(" for (int idx = 0; idx < GetSize(cells); idx++) {", file=f
)
650 print(" {} = cells[idx];".format(block
["cell"]), file=f
)
651 print(" if (blacklist_cells.count({})) continue;".format(block
["cell"]), file=f
)
652 for expr
in block
["filter"]:
653 print(" if (!({})) continue;".format(expr
), file=f
)
654 if block
["semioptional"] or block
["genargs"] is not None:
655 print(" found_any_match = true;", file=f
)
656 print(" rollback_stack.push_back(make_pair(cells[idx], recursion));", file=f
)
657 print(" block_{}(recursion+1);".format(index
+1), file=f
)
658 print(" if (rollback == 0) {", file=f
)
659 print(" rollback_stack.pop_back();", file=f
)
660 print(" } else {", file=f
)
661 print(" if (rollback != recursion) {{".format(index
+1), file=f
)
662 print(" {} = backup_{};".format(block
["cell"], block
["cell"]), file=f
)
663 print(" return;", file=f
)
665 print(" rollback = 0;", file=f
)
671 print(" {} = nullptr;".format(block
["cell"]), file=f
)
673 if block
["optional"]:
674 print(" block_{}(recursion+1);".format(index
+1), file=f
)
676 if block
["semioptional"]:
677 print(" if (!found_any_match) block_{}(recursion+1);".format(index
+1), file=f
)
679 print(" {} = backup_{};".format(block
["cell"], block
["cell"]), file=f
)
681 if block
["genargs"] is not None:
682 print("#define finish do { rollback = -1; rollback_stack.clean(); return; } while(0)", file=f
)
683 print(" if (generate_mode && !found_any_match) {", file=f
)
684 if len(block
["genargs"]) == 1:
685 print(" if (rng(100) >= {}) return;".format(block
["genargs"][0]), file=f
)
686 for line
in block
["gencode"]:
687 print(" " + line
, file=f
)
689 print("#undef finish", file=f
)
693 current_pattern
= None
701 print("YOSYS_NAMESPACE_END", file=f
)