line = line.strip()
cmd = line.split()
- if len(cmd) == 0: continue
+ if len(cmd) == 0 or cmd[0].startswith("//"): continue
cmd = cmd[0]
if cmd == "state":
l = f.readline()
assert l != ""
a = l.split()
- if len(a) == 0: continue
+ if len(a) == 0 or a[0].startswith("//"): continue
if a[0] == "endmatch": break
if a[0] == "select":
blocks.append(block)
-# pp.pprint(blocks)
-
with open("%s_pm.h" % prefix, "w") as f:
print("// Generated by pmgen.py from {}.pgm".format(prefix), file=f)
+ print("", file=f)
+
print("#include \"kernel/yosys.h\"", file=f)
print("#include \"kernel/sigtools.h\"", file=f)
+ print("", file=f)
+
print("YOSYS_NAMESPACE_BEGIN", file=f)
+ print("", file=f)
+
print("struct {}_pm {{".format(prefix), file=f)
print(" Module *module;", file=f)
print(" SigMap sigmap;", file=f)
index_types.append(filt[0])
print(" typedef std::tuple<{}> index_{}_key_type;".format(", ".join(index_types), index), file=f)
print(" dict<index_{}_key_type, vector<Cell*>> index_{};".format(index, index), file=f)
- print(" pool<Cell*> blacklist;", file=f)
+ print(" dict<SigBit, pool<Cell*>> sigusers;", file=f)
+ print(" pool<Cell*> blacklist_cells;", file=f)
+ print(" int rollback;", file=f)
print("", file=f)
print(" struct state_t {", file=f)
print(" IdString {}{{\"{}\"}};".format(v, n), file=f)
print("", file=f)
- print(" {}_pm(Module *module) :".format(prefix), file=f)
- print(" module(module), sigmap(module) {", file=f)
+ print(" void add_siguser(const SigSpec &sig, Cell *cell) {", file=f)
+ print(" for (auto bit : sigmap(sig)) {", file=f)
+ print(" if (bit.wire == nullptr) continue;", file=f)
+ print(" if (sigusers.count(bit) == 0 && bit.wire->port_id)", file=f)
+ print(" sigusers[bit].insert(nullptr);", file=f)
+ print(" sigusers[bit].insert(cell);", file=f)
+ print(" }", file=f)
+ print(" }", file=f)
+ print("", file=f)
+
+ print(" void blacklist(Cell *cell) {", file=f)
+ print(" blacklist_cells.insert(cell);", file=f)
+ print(" }", file=f)
+ print("", file=f)
+
+ print(" void check_blacklist() {", file=f)
+ for index in range(len(blocks)):
+ block = blocks[index]
+ if block["type"] == "match":
+ print(" if (st.{} != nullptr && blacklist_cells.count(st.{})) {{".format(block["cell"], block["cell"]), file=f)
+ print(" rollback = {};".format(index+1), file=f)
+ print(" return;", file=f)
+ print(" }", file=f)
+ print(" rollback = 0;", file=f)
+ print(" }", file=f)
+ print("", file=f)
+
+ print(" SigSpec port(Cell *cell, IdString portname) {", file=f)
+ print(" return sigmap(cell->getPort(portname));", file=f)
print(" }", file=f)
+ print("", file=f)
+
+ print(" Const param(Cell *cell, IdString paramname) {", file=f)
+ print(" return cell->getParam(paramname);", file=f)
+ print(" }", file=f)
+ print("", file=f)
+ print(" int nusers(const SigSpec &sig) {", file=f)
+ print(" pool<Cell*> users;", file=f)
+ print(" for (auto bit : sigmap(sig))", file=f)
+ print(" for (auto user : sigusers[bit])", file=f)
+ print(" users.insert(user);", file=f)
+ print(" return GetSize(users);", file=f)
+ print(" }", file=f)
+ print("", file=f)
+
+ print(" {}_pm(Module *module, const vector<Cell*> &cells) :".format(prefix), file=f)
+ print(" module(module), sigmap(module) {", file=f)
+ print(" for (auto cell : cells) {", file=f)
+ print(" for (auto &conn : cell->connections())", file=f)
+ print(" add_siguser(conn.second, cell);", file=f)
+
+ for index in range(len(blocks)):
+ block = blocks[index]
+ if block["type"] == "match":
+ print(" do {", file=f)
+ print(" Cell *{} = cell;".format(block["cell"]), file=f)
+ for expr in block["select"]:
+ print(" if (!({})) break;".format(expr), file=f)
+ print(" index_{}_key_type key;".format(index), file=f)
+ for field, filt in enumerate(block["filter"]):
+ print(" std::get<{}>(key) = {};".format(field, filt[1]), file=f)
+ print(" index_{}[key].push_back(cell);".format(index), file=f)
+ print(" } while (0);", file=f)
+
+ print(" }", file=f)
+ print(" }", file=f)
print("", file=f)
+
print(" void run(std::function<void(struct {}_pm*)> on_accept_f) {{".format(prefix), file=f)
print(" on_accept = on_accept_f;", file=f)
- if len(blocks):
- print(" block_0();", file=f)
+ print(" rollback = 0;", file=f)
+ print(" block_0();", file=f)
print(" }", file=f)
+ print("", file=f)
+
+ print("#define reject break", file=f)
+ print("#define accept do { on_accept(this); check_blacklist(); if (rollback) goto rollback_label; } while(0)", file=f)
+ print("", file=f)
for index in range(len(blocks)):
block = blocks[index]
- print("", file=f)
print(" void block_{}() {{".format(index), file=f)
const_st = set()
t = state_types[s]
print(" {} backup_{} = st.{};".format(t, s, s), file=f)
- if len(restore_st):
+ if block["type"] == "code":
print("", file=f)
- for s in sorted(restore_st):
- t = state_types[s]
- print(" st.{} = backup_{};".format(s, s), file=f)
+ print(" do {", file=f)
+ for line in block["code"]:
+ print(" " + line, file=f)
+
+ print("", file=f)
+ print(" block_{}();".format(index+1), file=f)
+ print("rollback_label: YS_ATTRIBUTE(unused);", file=f)
+ print(" } while (0);", file=f)
+
+ if len(restore_st):
+ print("", file=f)
+ for s in sorted(restore_st):
+ t = state_types[s]
+ print(" st.{} = backup_{};".format(s, s), file=f)
+
+ elif block["type"] == "match":
+ assert len(restore_st) == 0
+
+ print("", file=f)
+ print(" index_{}_key_type key;".format(index), file=f)
+ for field, filt in enumerate(block["filter"]):
+ print(" std::get<{}>(key) = {};".format(field, filt[2]), file=f)
+ print(" const vector<Cell*> &cells = index_{}[key];".format(index), file=f)
+
+ print("", file=f)
+ print(" for (int idx = 0; idx < GetSize(cells); idx++) {", file=f)
+ print(" {} = cells[idx];".format(block["cell"]), file=f)
+ print(" block_{}();".format(index+1), file=f)
+ print(" if (rollback) {", file=f)
+ print(" if (rollback != {}) {{".format(index+1), file=f)
+ print(" {} = nullptr;".format(block["cell"]), file=f)
+ print(" return;", file=f)
+ print(" }", file=f)
+ print(" rollback = 0;", file=f)
+ print(" }", file=f)
+ print(" }", file=f)
+
+ print("", file=f)
+ print(" {} = nullptr;".format(block["cell"]), file=f)
+
+ if block["optional"]:
+ print(" block_{}();".format(index+1), file=f)
+
+ else:
+ assert False
+
print(" }", file=f)
+ print("", file=f)
+
+ print("#undef reject", file=f)
+ print("#undef accept", file=f)
+ print("", file=f)
+
+ print(" void block_{}() {{".format(len(blocks)), file=f)
+ print(" on_accept(this);", file=f)
+ print(" check_blacklist();", file=f)
+ print(" }", file=f)
+ print("};", file=f)
+
+ print("", file=f)
+ print("YOSYS_NAMESPACE_END", file=f)
- print("};\nYOSYS_NAMESPACE_END", file=f)
+# pp.pprint(blocks)