Merge pull request #2188 from antmicro/missing-operators
[yosys.git] / passes / pmgen / pmgen.py
1 #!/usr/bin/env python3
2
3 import re
4 import sys
5 import pprint
6 import getopt
7
8 pp = pprint.PrettyPrinter(indent=4)
9
10 prefix = None
11 pmgfiles = list()
12 outfile = None
13 debug = False
14 genhdr = False
15
16 opts, args = getopt.getopt(sys.argv[1:], "p:o:dg")
17
18 for o, a in opts:
19 if o == "-p":
20 prefix = a
21 elif o == "-o":
22 outfile = a
23 elif o == "-d":
24 debug = True
25 elif o == "-g":
26 genhdr = True
27
28 if outfile is None:
29 outfile = "/dev/stdout"
30
31 for a in args:
32 assert a.endswith(".pmg")
33 if prefix is None and len(args) == 1:
34 prefix = a[0:-4]
35 prefix = prefix.split('/')[-1]
36 pmgfiles.append(a)
37
38 assert prefix is not None
39
40 current_pattern = None
41 current_subpattern = None
42 patterns = dict()
43 subpatterns = dict()
44 subpattern_args = dict()
45 state_types = dict()
46 udata_types = dict()
47 blocks = list()
48 ids = dict()
49
50 def rewrite_cpp(s):
51 t = list()
52 i = 0
53 while i < len(s):
54 if s[i] in ("'", '"') and i + 1 < len(s):
55 j = i + 1
56 while j + 1 < len(s) and s[j] != s[i]:
57 if s[j] == '\\' and j + 1 < len(s):
58 j += 1
59 j += 1
60 t.append(s[i:j+1])
61 i = j + 1
62 continue
63
64 if s[i] in ('$', '\\') and i + 1 < len(s):
65 j = i + 1
66 while True:
67 if j == len(s):
68 j -= 1
69 break
70 if ord('a') <= ord(s[j]) <= ord('z'):
71 j += 1
72 continue
73 if ord('A') <= ord(s[j]) <= ord('Z'):
74 j += 1
75 continue
76 if ord('0') <= ord(s[j]) <= ord('9'):
77 j += 1
78 continue
79 if s[j] == '_':
80 j += 1
81 continue
82 j -= 1
83 break
84
85 n = s[i:j+1]
86 i = j + 1
87
88 if n[0] == '$':
89 v = "id_d_" + n[1:]
90 else:
91 v = "id_b_" + n[1:]
92
93 if v not in ids:
94 ids[v] = n
95 else:
96 assert ids[v] == n
97
98 t.append(v)
99 continue
100
101 if s[i] == "\t":
102 t.append(" ")
103 else:
104 t.append(s[i])
105
106 i += 1
107
108 return "".join(t)
109
110 def process_pmgfile(f, filename):
111 linenr = 0
112 global current_pattern
113 global current_subpattern
114 while True:
115 linenr += 1
116 line = f.readline()
117 if line == "": break
118 line = line.strip()
119
120 cmd = line.split()
121 if len(cmd) == 0 or cmd[0].startswith("//"): continue
122 cmd = cmd[0]
123
124 if cmd == "pattern":
125 if current_pattern is not None:
126 block = dict()
127 block["type"] = "final"
128 block["pattern"] = (current_pattern, current_subpattern)
129 blocks.append(block)
130 line = line.split()
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()
140 continue
141
142 assert current_pattern is not None
143
144 if cmd == "fallthrough":
145 block = dict()
146 block["type"] = "fallthrough"
147 blocks.append(block)
148 line = line.split()
149 assert len(line) == 1
150 continue
151
152 if cmd == "subpattern":
153 if len(blocks) == 0 or blocks[-1]["type"] != "fallthrough":
154 block = dict()
155 block["type"] = "final"
156 block["pattern"] = (current_pattern, current_subpattern)
157 blocks.append(block)
158 elif len(blocks) and blocks[-1]["type"] == "fallthrough":
159 del blocks[-1]
160 line = line.split()
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)
166 continue
167
168 if cmd == "arg":
169 line = line.split()
170 assert len(line) > 1
171 subpattern_args[(current_pattern, current_subpattern)] += line[1:]
172 continue
173
174 if cmd == "state":
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)
176 assert m
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
182 continue
183
184 if cmd == "udata":
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)
186 assert m
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
192 continue
193
194 if cmd == "match":
195 block = dict()
196 block["type"] = "match"
197 block["src"] = "%s:%d" % (filename, linenr)
198 block["pattern"] = (current_pattern, current_subpattern)
199
200 block["genargs"] = None
201 block["gencode"] = None
202
203 line = line.split()
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*";
208
209 block["if"] = list()
210 block["setup"] = list()
211 block["index"] = list()
212 block["filter"] = list()
213 block["sets"] = list()
214 block["optional"] = False
215 block["semioptional"] = False
216
217 while True:
218 linenr += 1
219 l = f.readline()
220 assert l != ""
221 a = l.split()
222 if len(a) == 0 or a[0].startswith("//"): continue
223 if a[0] == "endmatch": break
224
225 if a[0] == "if":
226 b = l.lstrip()[2:]
227 block["if"].append(rewrite_cpp(b.strip()))
228 continue
229
230 if a[0] == "select":
231 b = l.lstrip()[6:]
232 block["setup"].append(("select", rewrite_cpp(b.strip())))
233 continue
234
235 if a[0] == "slice":
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))))
238 continue
239
240 if a[0] == "choice":
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))))
243 continue
244
245 if a[0] == "define":
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))))
248 continue
249
250 if a[0] == "index":
251 m = re.match(r"^\s*index\s+<(.*?)>\s+(.*?)\s*===\s*(.*?)\s*$", l)
252 assert m
253 block["index"].append((m.group(1), rewrite_cpp(m.group(2)), rewrite_cpp(m.group(3))))
254 continue
255
256 if a[0] == "filter":
257 b = l.lstrip()[6:]
258 block["filter"].append(rewrite_cpp(b.strip()))
259 continue
260
261 if a[0] == "set":
262 m = re.match(r"^\s*set\s+(\S+)\s+(.*?)\s*$", l)
263 block["sets"].append((m.group(1), rewrite_cpp(m.group(2))))
264 continue
265
266 if a[0] == "optional":
267 block["optional"] = True
268 continue
269
270 if a[0] == "semioptional":
271 block["semioptional"] = True
272 continue
273
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()
280 while True:
281 linenr += 1
282 l = f.readline()
283 assert l != ""
284 a = l.split()
285 if len(a) == 1 and a[0] == "endmatch": break
286 block["gencode"].append(rewrite_cpp(l.rstrip()))
287 break
288
289 raise RuntimeError("'%s' statement not recognised on line %d" % (a[0], linenr))
290
291 if block["optional"]:
292 assert not block["semioptional"]
293
294 blocks.append(block)
295 continue
296
297 if cmd == "code":
298 block = dict()
299 block["type"] = "code"
300 block["src"] = "%s:%d" % (filename, linenr)
301 block["pattern"] = (current_pattern, current_subpattern)
302
303 block["code"] = list()
304 block["fcode"] = list()
305 block["states"] = set()
306
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)
311
312 codetype = "code"
313
314 while True:
315 linenr += 1
316 l = f.readline()
317 assert l != ""
318 a = l.split()
319 if len(a) == 0: continue
320 if a[0] == "endcode": break
321
322 if a[0] == "finally":
323 codetype = "fcode"
324 continue
325
326 block[codetype].append(rewrite_cpp(l.rstrip()))
327
328 blocks.append(block)
329 continue
330
331 raise RuntimeError("'%s' command not recognised" % cmd)
332
333 for fn in pmgfiles:
334 with open(fn, "r") as f:
335 process_pmgfile(f, fn)
336
337 if current_pattern is not None:
338 block = dict()
339 block["type"] = "final"
340 block["pattern"] = (current_pattern, current_subpattern)
341 blocks.append(block)
342
343 current_pattern = None
344 current_subpattern = None
345
346 if debug:
347 pp.pprint(blocks)
348
349 with open(outfile, "w") as f:
350 for fn in pmgfiles:
351 print("// Generated by pmgen.py from {}".format(fn), file=f)
352 print("", file=f)
353
354 if genhdr:
355 print("#include \"kernel/yosys.h\"", file=f)
356 print("#include \"kernel/sigtools.h\"", file=f)
357 print("", file=f)
358 print("YOSYS_NAMESPACE_BEGIN", file=f)
359 print("", file=f)
360
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)
368 print("", file=f)
369
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)
376 print(" }", file=f)
377 print("", file=f)
378
379 for index in range(len(blocks)):
380 block = blocks[index]
381 if block["type"] == "match":
382 index_types = list()
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)
401 print("", file=f)
402
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)
408 print("", file=f)
409
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)
414 print("", file=f)
415 current_pattern = None
416
417 for v, n in sorted(ids.items()):
418 if n[0] == "\\":
419 print(" IdString {}{{\"\\{}\"}};".format(v, n), file=f)
420 else:
421 print(" IdString {}{{\"{}\"}};".format(v, n), file=f)
422 print("", file=f)
423
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)
428 print(" }", file=f)
429 print(" }", file=f)
430 print("", file=f)
431
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)
439 print(" }", file=f)
440 print(" }", file=f)
441 print("", file=f)
442
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)
447 print(" }", file=f)
448 print(" }", file=f)
449 print("", file=f)
450
451 current_pattern = None
452
453 print(" SigSpec port(Cell *cell, IdString portname) {", file=f)
454 print(" return sigmap(cell->getPort(portname));", file=f)
455 print(" }", file=f)
456 print("", 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)
459 print(" }", file=f)
460 print("", file=f)
461
462 print(" Const param(Cell *cell, IdString paramname) {", file=f)
463 print(" return cell->getParam(paramname);", file=f)
464 print(" }", file=f)
465 print("", 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)
468 print(" }", file=f)
469 print("", file=f)
470
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)
477 print(" }", file=f)
478 print("", file=f)
479
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)
483 print(" }", file=f)
484 print("", file=f)
485
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)
488 print(" }", file=f)
489 print("", file=f)
490
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()):
494 if t.endswith("*"):
495 print(" ud_{}.{} = nullptr;".format(current_pattern,s), file=f)
496 else:
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)
507
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)
515 loopcnt = 0
516 valueidx = 1
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)
523 valueidx += 1
524 loopcnt += 1
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)
529 valueidx += 1
530 loopcnt += 1
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)
534 valueidx += 1
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):
540 print(" }", file=f)
541 print(" } while (0);", file=f)
542
543 print(" }", file=f)
544 print(" }", file=f)
545 print("", file=f)
546
547 print(" ~{}_pm() {{".format(prefix), file=f)
548 print(" for (auto cell : autoremove_cells)", file=f)
549 print(" module->remove(cell);", file=f)
550 print(" }", file=f)
551 print("", file=f)
552
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()):
560 if t.endswith("*"):
561 print(" st_{}.{} = nullptr;".format(current_pattern, s), file=f)
562 else:
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)
567 print(" }", file=f)
568 print("", 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)
571 print(" }", file=f)
572 print("", file=f)
573 print(" int run_{}() {{".format(current_pattern), file=f)
574 print(" return run_{}([](){{}});".format(current_pattern, current_pattern), file=f)
575 print(" }", file=f)
576 print("", file=f)
577
578 if len(subpatterns):
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)
581 print("", file=f)
582
583 current_pattern = None
584 current_subpattern = None
585
586 for index in range(len(blocks)):
587 block = blocks[index]
588
589 if block["type"] in ("match", "code"):
590 print(" // {}".format(block["src"]), file=f)
591
592 print(" void block_{}(int recursion YS_MAYBE_UNUSED) {{".format(index), file=f)
593 current_pattern, current_subpattern = block["pattern"]
594
595 if block["type"] == "final":
596 print(" }", file=f)
597 if index+1 != len(blocks):
598 print("", file=f)
599 continue
600
601 const_st = set()
602 nonconst_st = set()
603 restore_st = set()
604
605 for s in subpattern_args[(current_pattern, current_subpattern)]:
606 const_st.add(s)
607
608 for i in range(subpatterns[(current_pattern, current_subpattern)], index):
609 if blocks[i]["type"] == "code":
610 for s in blocks[i]["states"]:
611 const_st.add(s)
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])
616 else:
617 assert False
618
619 if block["type"] == "code":
620 for s in block["states"]:
621 if s in const_st:
622 const_st.remove(s)
623 restore_st.add(s)
624 nonconst_st.add(s)
625 elif block["type"] == "match":
626 s = block["cell"]
627 assert s not in const_st
628 nonconst_st.add(s)
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])
633 else:
634 assert False
635
636 for s in sorted(const_st):
637 t = state_types[current_pattern][s]
638 if t.endswith("*"):
639 print(" {} const &{} YS_MAYBE_UNUSED = st_{}.{};".format(t, s, current_pattern, s), file=f)
640 else:
641 print(" const {} &{} YS_MAYBE_UNUSED = st_{}.{};".format(t, s, current_pattern, s), file=f)
642
643 for s in sorted(nonconst_st):
644 t = state_types[current_pattern][s]
645 print(" {} &{} YS_MAYBE_UNUSED = st_{}.{};".format(t, s, current_pattern, s), file=f)
646
647 for u in sorted(udata_types[current_pattern].keys()):
648 t = udata_types[current_pattern][u]
649 print(" {} &{} YS_MAYBE_UNUSED = ud_{}.{};".format(t, u, current_pattern, u), file=f)
650
651 if len(restore_st):
652 print("", 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)
656
657 if block["type"] == "code":
658 print("", file=f)
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)
664
665 for line in block["code"]:
666 print(" " + line, file=f)
667
668 print("", file=f)
669 print(" block_{}(recursion+1);".format(index+1), file=f)
670
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)
676
677 print("", file=f)
678 print("rollback_label:", file=f)
679 print(" YS_MAYBE_UNUSED;", file=f)
680
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_MAYBE_UNUSED;", file=f)
688 print("#undef accept", file=f)
689 print("#undef finish", file=f)
690
691 if len(restore_st) or len(nonconst_st):
692 print("", file=f)
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]
699 if t.endswith("*"):
700 print(" {} = nullptr;".format(s), file=f)
701 else:
702 print(" {} = {}();".format(s, t), file=f)
703
704 elif block["type"] == "match":
705 assert len(restore_st) == 0
706
707 print(" Cell* _pmg_backup_{} = {};".format(block["cell"], block["cell"]), file=f)
708
709 if len(block["if"]):
710 for expr in block["if"]:
711 print("", file=f)
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)
717 print(" }", file=f)
718
719 print("", 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)
724
725 if block["semioptional"] or block["genargs"] is not None:
726 print(" bool found_any_match = false;", file=f)
727
728 print("", 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)
733 valueidx = 1
734 for item in block["setup"]:
735 if item[0] == "slice":
736 print(" const int &{} YS_MAYBE_UNUSED = std::get<{}>(cells[_pmg_idx]);".format(item[1], valueidx), file=f)
737 valueidx += 1
738 if item[0] == "choice":
739 print(" const {} &{} YS_MAYBE_UNUSED = std::get<{}>(cells[_pmg_idx]);".format(item[1], item[2], valueidx), file=f)
740 valueidx += 1
741 if item[0] == "define":
742 print(" const {} &{} YS_MAYBE_UNUSED = std::get<{}>(cells[_pmg_idx]);".format(item[1], item[2], valueidx), file=f)
743 valueidx += 1
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)
762 print(" }", file=f)
763 print(" rollback = 0;", file=f)
764 print(" }", file=f)
765 print(" }", file=f)
766 print(" }", file=f)
767
768 print("", file=f)
769 print(" {} = nullptr;".format(block["cell"]), file=f)
770
771 if block["optional"]:
772 print(" block_{}(recursion+1);".format(index+1), file=f)
773
774 if block["semioptional"]:
775 print(" if (!found_any_match) block_{}(recursion+1);".format(index+1), file=f)
776
777 print(" {} = _pmg_backup_{};".format(block["cell"], block["cell"]), file=f)
778
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)
784 print(" }", file=f)
785 print("#undef finish", file=f)
786 else:
787 assert False
788
789 current_pattern = None
790 print(" }", file=f)
791 print("", file=f)
792
793 print("};", file=f)
794
795 if genhdr:
796 print("", file=f)
797 print("YOSYS_NAMESPACE_END", file=f)