/* * This file is part of the flashrom project. * * Copyright (C) 2000 Silicon Integrated System Corporation * Copyright (C) 2004 Tyan Corp * Copyright (C) 2005-2008 coresystems GmbH * Copyright (C) 2008,2009,2010 Carl-Daniel Hailfinger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include "flash.h" #include "flashchips.h" void cli_classic_usage(const char *name) { const char *pname; int pnamelen; int remaining = 0; enum programmer p; printf("Usage: %s [-VfLzhR] [-E|-r file|-w file|-v file] [-c chipname]\n" " [-m [vendor:]part] [-l file] [-i image] [-p programmer]\n\n", name); printf("Please note that the command line interface for flashrom will " "change before\nflashrom 1.0. Do not use flashrom in scripts " "or other automated tools without\nchecking that your flashrom" " version won't interpret options in a different way.\n\n"); printf (" -r | --read: read flash and save into file\n" " -w | --write: write file into flash\n" " -v | --verify: verify flash against file\n" " -n | --noverify: don't verify flash against file\n" " -E | --erase: erase flash device\n" " -V | --verbose: more verbose output\n" " -c | --chip : probe only for specified flash chip\n" #if INTERNAL_SUPPORT == 1 " -m | --mainboard <[vendor:]part>: override mainboard settings\n" #endif " -f | --force: force write without checking image\n" " -l | --layout : read ROM layout from file\n" " -i | --image : only flash image name from flash layout\n" " -L | --list-supported: print supported devices\n" #if PRINT_WIKI_SUPPORT == 1 " -z | --list-supported-wiki: print supported devices in wiki syntax\n" #endif " -p | --programmer : specify the programmer device"); for (p = 0; p < PROGRAMMER_INVALID; p++) { pname = programmer_table[p].name; pnamelen = strlen(pname); if (remaining - pnamelen - 2 < 0) { printf("\n "); remaining = 43; } else { printf(" "); remaining--; } if (p == 0) { printf("("); remaining--; } printf("%s", pname); remaining -= pnamelen; if (p < PROGRAMMER_INVALID - 1) { printf(","); remaining--; } else { printf(")\n"); } } printf( " -h | --help: print this help text\n" " -R | --version: print the version (release)\n" "\nYou can specify one of -E, -r, -w, -v or no operation. If no operation is\n" "specified, then all that happens is that flash info is dumped.\n\n"); exit(1); } int cli_classic(int argc, char *argv[]) { unsigned long size; /* Probe for up to three flash chips. */ struct flashchip *flash, *flashes[3]; const char *name; int namelen; int opt; int option_index = 0; int force = 0; int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; int dont_verify_it = 0, list_supported = 0; #if PRINT_WIKI_SUPPORT == 1 int list_supported_wiki = 0; #endif int operation_specified = 0; int i; #if PRINT_WIKI_SUPPORT == 1 const char *optstring = "rRwvnVEfc:m:l:i:p:Lzh"; #else const char *optstring = "rRwvnVEfc:m:l:i:p:Lh"; #endif static struct option long_options[] = { {"read", 0, 0, 'r'}, {"write", 0, 0, 'w'}, {"erase", 0, 0, 'E'}, {"verify", 0, 0, 'v'}, {"noverify", 0, 0, 'n'}, {"chip", 1, 0, 'c'}, {"mainboard", 1, 0, 'm'}, {"verbose", 0, 0, 'V'}, {"force", 0, 0, 'f'}, {"layout", 1, 0, 'l'}, {"image", 1, 0, 'i'}, {"list-supported", 0, 0, 'L'}, #if PRINT_WIKI_SUPPORT == 1 {"list-supported-wiki", 0, 0, 'z'}, #endif {"programmer", 1, 0, 'p'}, {"help", 0, 0, 'h'}, {"version", 0, 0, 'R'}, {0, 0, 0, 0} }; char *filename = NULL; char *tempstr = NULL; print_version(); if (argc > 1) { /* Yes, print them. */ printf_debug("The arguments are:\n"); for (i = 1; i < argc; ++i) printf_debug("%s\n", argv[i]); } if (selfcheck()) exit(1); setbuf(stdout, NULL); while ((opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != EOF) { switch (opt) { case 'r': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); exit(1); } read_it = 1; break; case 'w': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); exit(1); } write_it = 1; break; case 'v': //FIXME: gracefully handle superfluous -v if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); exit(1); } if (dont_verify_it) { fprintf(stderr, "--verify and --noverify are" "mutually exclusive. Aborting.\n"); exit(1); } verify_it = 1; break; case 'n': if (verify_it) { fprintf(stderr, "--verify and --noverify are" "mutually exclusive. Aborting.\n"); exit(1); } dont_verify_it = 1; break; case 'c': chip_to_probe = strdup(optarg); break; case 'V': verbose++; break; case 'E': if (++operation_specified > 1) { fprintf(stderr, "More than one operation " "specified. Aborting.\n"); exit(1); } erase_it = 1; break; #if INTERNAL_SUPPORT == 1 case 'm': tempstr = strdup(optarg); lb_vendor_dev_from_string(tempstr); break; #endif case 'f': force = 1; break; case 'l': tempstr = strdup(optarg); if (read_romlayout(tempstr)) exit(1); break; case 'i': tempstr = strdup(optarg); find_romentry(tempstr); break; case 'L': list_supported = 1; break; #if PRINT_WIKI_SUPPORT == 1 case 'z': list_supported_wiki = 1; break; #endif case 'p': for (programmer = 0; programmer < PROGRAMMER_INVALID; programmer++) { name = programmer_table[programmer].name; namelen = strlen(name); if (strncmp(optarg, name, namelen) == 0) { switch (optarg[namelen]) { case ':': programmer_param = strdup(optarg + namelen + 1); break; case '\0': break; default: /* The continue refers to the * for loop. It is here to be * able to differentiate between * foo and foobar. */ continue; } break; } } if (programmer == PROGRAMMER_INVALID) { printf("Error: Unknown programmer %s.\n", optarg); exit(1); } break; case 'R': /* print_version() is always called during startup. */ exit(0); break; case 'h': default: cli_classic_usage(argv[0]); break; } } if (list_supported) { print_supported(); exit(0); } #if PRINT_WIKI_SUPPORT == 1 if (list_supported_wiki) { print_supported_wiki(); exit(0); } #endif if (read_it && write_it) { printf("Error: -r and -w are mutually exclusive.\n"); cli_classic_usage(argv[0]); } if (optind < argc) filename = argv[optind++]; if (optind < argc) { printf("Error: Extra parameter found.\n"); cli_classic_usage(argv[0]); } if (programmer_init()) { fprintf(stderr, "Error: Programmer initialization failed.\n"); exit(1); } // FIXME: Delay calibration should happen in programmer code. myusec_calibrate_delay(); for (i = 0; i < ARRAY_SIZE(flashes); i++) { flashes[i] = probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0); if (!flashes[i]) for (i++; i < ARRAY_SIZE(flashes); i++) flashes[i] = NULL; } if (flashes[1]) { printf("Multiple flash chips were detected:"); for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++) printf(" %s", flashes[i]->name); printf("\nPlease specify which chip to use with the -c option.\n"); programmer_shutdown(); exit(1); } else if (!flashes[0]) { printf("No EEPROM/flash device found.\n"); if (!force || !chip_to_probe) { printf("If you know which flash chip you have, and if this version of flashrom\n"); printf("supports a similar flash chip, you can try to force read your chip. Run:\n"); printf("flashrom -f -r -c similar_supported_flash_chip filename\n"); printf("\n"); printf("Note: flashrom can never write when the flash chip isn't found automatically.\n"); } if (force && read_it && chip_to_probe) { printf("Force read (-f -r -c) requested, forcing chip probe success:\n"); flashes[0] = probe_flash(flashchips, 1); if (!flashes[0]) { printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe); printf("Run flashrom -L to view the hardware supported in this flashrom version.\n"); exit(1); } printf("Please note that forced reads most likely contain garbage.\n"); return read_flash(flashes[0], filename); } // FIXME: flash writes stay enabled! programmer_shutdown(); exit(1); } flash = flashes[0]; check_chip_supported(flash); size = flash->total_size * 1024; if (check_max_decode((buses_supported & flash->bustype), size) && (!force)) { fprintf(stderr, "Chip is too big for this programmer " "(-V gives details). Use --force to override.\n"); programmer_shutdown(); return 1; } if (!(read_it | write_it | verify_it | erase_it)) { printf("No operations were specified.\n"); // FIXME: flash writes stay enabled! programmer_shutdown(); exit(1); } if (!filename && !erase_it) { printf("Error: No filename specified.\n"); // FIXME: flash writes stay enabled! programmer_shutdown(); exit(1); } /* Always verify write operations unless -n is used. */ if (write_it && !dont_verify_it) verify_it = 1; return doit(flash, force, filename, read_it, write_it, erase_it, verify_it); } ='n234' href='#n234'>234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
#!/usr/bin/env python3

import re
import sys
import pprint
import getopt

pp = pprint.PrettyPrinter(indent=4)

prefix = None
pmgfiles = list()
outfile = None
debug = False
genhdr = False

opts, args = getopt.getopt(sys.argv[1:], "p:o:dg")

for o, a in opts:
    if o == "-p":
        prefix = a
    elif o == "-o":
        outfile = a
    elif o == "-d":
        debug = True
    elif o == "-g":
        genhdr = True

if outfile is None:
    outfile = "/dev/stdout"

for a in args:
    assert a.endswith(".pmg")
    if prefix is None and len(args) == 1:
        prefix = a[0:-4]
        prefix = prefix.split('/')[-1]
    pmgfiles.append(a)

assert prefix is not None

current_pattern = None
current_subpattern = None
patterns = dict()
subpatterns = dict()
subpattern_args = dict()
state_types = dict()
udata_types = dict()
blocks = list()
ids = dict()

def rewrite_cpp(s):
    t = list()
    i = 0
    while i < len(s):
        if s[i] in ("'", '"') and i + 1 < len(s):
            j = i + 1
            while j + 1 < len(s) and s[j] != s[i]:
                if s[j] == '\\' and j + 1 < len(s):
                    j += 1
                j += 1
            t.append(s[i:j+1])
            i = j + 1
            continue

        if s[i] in ('$', '\\') and i + 1 < len(s):
            j = i + 1
            while True:
                if j == len(s):
                    j -= 1
                    break
                if ord('a') <= ord(s[j]) <= ord('z'):
                    j += 1
                    continue
                if ord('A') <= ord(s[j]) <= ord('Z'):
                    j += 1
                    continue
                if ord('0') <= ord(s[j]) <= ord('9'):
                    j += 1
                    continue
                if s[j] == '_':
                    j += 1
                    continue
                j -= 1
                break

            n = s[i:j+1]
            i = j + 1

            if n[0] == '$':
                v = "id_d_" + n[1:]
            else:
                v = "id_b_" + n[1:]

            if v not in ids:
                ids[v] = n
            else:
                assert ids[v] == n

            t.append(v)
            continue

        if s[i] == "\t":
            t.append("  ")
        else:
            t.append(s[i])

        i += 1

    return "".join(t)

def process_pmgfile(f, filename):
    linenr = 0
    global current_pattern
    global current_subpattern
    while True:
        linenr += 1
        line = f.readline()
        if line == "": break
        line = line.strip()

        cmd = line.split()
        if len(cmd) == 0 or cmd[0].startswith("//"): continue
        cmd = cmd[0]

        if cmd == "pattern":
            if current_pattern is not None:
                block = dict()
                block["type"] = "final"
                block["pattern"] = (current_pattern, current_subpattern)
                blocks.append(block)
            line = line.split()
            assert len(line) == 2
            assert line[1] not in patterns
            current_pattern = line[1]
            current_subpattern = ""
            patterns[current_pattern] = len(blocks)
            subpatterns[(current_pattern, current_subpattern)] = len(blocks)
            subpattern_args[(current_pattern, current_subpattern)] = list()
            state_types[current_pattern] = dict()
            udata_types[current_pattern] = dict()
            continue

        assert current_pattern is not None

        if cmd == "fallthrough":
            block = dict()
            block["type"] = "fallthrough"
            blocks.append(block)
            line = line.split()
            assert len(line) == 1
            continue

        if cmd == "subpattern":
            if len(blocks) == 0 or blocks[-1]["type"] != "fallthrough":
                block = dict()
                block["type"] = "final"
                block["pattern"] = (current_pattern, current_subpattern)
                blocks.append(block)
            elif len(blocks) and blocks[-1]["type"] == "fallthrough":
                del blocks[-1]
            line = line.split()
            assert len(line) == 2
            current_subpattern = line[1]
            subpattern_args[(current_pattern, current_subpattern)] = list()
            assert (current_pattern, current_subpattern) not in subpatterns
            subpatterns[(current_pattern, current_subpattern)] = len(blocks)
            continue

        if cmd == "arg":
            line = line.split()
            assert len(line) > 1
            subpattern_args[(current_pattern, current_subpattern)] += line[1:]
            continue

        if cmd == "state":
            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)
            assert m
            type_str = m.group(1)
            states_str = m.group(2)
            for s in re.split(r"\s+", states_str):
                assert s not in state_types[current_pattern]
                state_types[current_pattern][s] = type_str
            continue

        if cmd == "udata":
            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)
            assert m
            type_str = m.group(1)
            udatas_str = m.group(2)
            for s in re.split(r"\s+", udatas_str):
                assert s not in udata_types[current_pattern]
                udata_types[current_pattern][s] = type_str
            continue

        if cmd == "match":
            block = dict()
            block["type"] = "match"
            block["src"] = "%s:%d" % (filename, linenr)
            block["pattern"] = (current_pattern, current_subpattern)

            block["genargs"] = None
            block["gencode"] = None

            line = line.split()
            assert len(line) == 2
            assert (line[1] not in state_types[current_pattern]) or (state_types[current_pattern][line[1]] == "Cell*")
            block["cell"] = line[1]
            state_types[current_pattern][line[1]] = "Cell*";

            block["if"] = list()
            block["setup"] = list()
            block["index"] = list()
            block["filter"] = list()
            block["sets"] = list()
            block["optional"] = False
            block["semioptional"] = False

            while True:
                linenr += 1
                l = f.readline()
                assert l != ""
                a = l.split()
                if len(a) == 0 or a[0].startswith("//"): continue
                if a[0] == "endmatch": break

                if a[0] == "if":
                    b = l.lstrip()[2:]
                    block["if"].append(rewrite_cpp(b.strip()))
                    continue

                if a[0] == "select":
                    b = l.lstrip()[6:]
                    block["setup"].append(("select", rewrite_cpp(b.strip())))
                    continue

                if a[0] == "slice":
                    m = re.match(r"^\s*slice\s+(\S+)\s+(.*?)\s*$", l)
                    block["setup"].append(("slice", m.group(1), rewrite_cpp(m.group(2))))
                    continue

                if a[0] == "choice":
                    m = re.match(r"^\s*choice\s+<(.*?)>\s+(\S+)\s+(.*?)\s*$", l)
                    block["setup"].append(("choice", m.group(1), m.group(2), rewrite_cpp(m.group(3))))
                    continue

                if a[0] == "define":
                    m = re.match(r"^\s*define\s+<(.*?)>\s+(\S+)\s+(.*?)\s*$", l)
                    block["setup"].append(("define", m.group(1), m.group(2), rewrite_cpp(m.group(3))))
                    continue

                if a[0] == "index":
                    m = re.match(r"^\s*index\s+<(.*?)>\s+(.*?)\s*===\s*(.*?)\s*$", l)
                    assert m
                    block["index"].append((m.group(1), rewrite_cpp(m.group(2)), rewrite_cpp(m.group(3))))
                    continue

                if a[0] == "filter":
                    b = l.lstrip()[6:]
                    block["filter"].append(rewrite_cpp(b.strip()))
                    continue

                if a[0] == "set":
                    m = re.match(r"^\s*set\s+(\S+)\s+(.*?)\s*$", l)
                    block["sets"].append((m.group(1), rewrite_cpp(m.group(2))))
                    continue

                if a[0] == "optional":
                    block["optional"] = True
                    continue

                if a[0] == "semioptional":
                    block["semioptional"] = True
                    continue

                if a[0] == "generate":
                    block["genargs"] = list([int(s) for s in a[1:]])
                    if len(block["genargs"]) == 0: block["genargs"].append(100)
                    if len(block["genargs"]) == 1: block["genargs"].append(0)
                    assert len(block["genargs"]) == 2
                    block["gencode"] = list()
                    while True:
                        linenr += 1
                        l = f.readline()
                        assert l != ""
                        a = l.split()
                        if len(a) == 1 and a[0] == "endmatch": break
                        block["gencode"].append(rewrite_cpp(l.rstrip()))
                    break

                raise RuntimeError("'%s' statement not recognised on line %d" % (a[0], linenr))

            if block["optional"]:
                assert not block["semioptional"]

            blocks.append(block)
            continue

        if cmd == "code":
            block = dict()
            block["type"] = "code"
            block["src"] = "%s:%d" % (filename, linenr)
            block["pattern"] = (current_pattern, current_subpattern)

            block["code"] = list()
            block["fcode"] = list()
            block["states"] = set()

            for s in line.split()[1:]:
                if s not in state_types[current_pattern]:
                    raise RuntimeError("'%s' not in state_types" % s)
                block["states"].add(s)

            codetype = "code"

            while True:
                linenr += 1
                l = f.readline()
                assert l != ""
                a = l.split()
                if len(a) == 0: continue
                if a[0] == "endcode": break

                if a[0] == "finally":
                    codetype = "fcode"
                    continue

                block[codetype].append(rewrite_cpp(l.rstrip()))

            blocks.append(block)
            continue

        raise RuntimeError("'%s' command not recognised" % cmd)

for fn in pmgfiles:
    with open(fn, "r") as f:
        process_pmgfile(f, fn)

if current_pattern is not None:
    block = dict()
    block["type"] = "final"
    block["pattern"] = (current_pattern, current_subpattern)
    blocks.append(block)

current_pattern = None
current_subpattern = None

if debug:
    pp.pprint(blocks)

with open(outfile, "w") as f:
    for fn in pmgfiles:
        print("// Generated by pmgen.py from {}".format(fn), file=f)
    print("", file=f)

    if genhdr:
        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)
    print("  std::function<void()> on_accept;", file=f)
    print("  bool setup_done;", file=f)
    print("  bool generate_mode;", file=f)
    print("  int accept_cnt;", file=f)
    print("", file=f)

    print("  uint32_t rngseed;", file=f)
    print("  int rng(unsigned int n) {", file=f)
    print("    rngseed ^= rngseed << 13;", file=f)
    print("    rngseed ^= rngseed >> 17;", file=f)
    print("    rngseed ^= rngseed << 5;", file=f)
    print("    return rngseed % n;", file=f)
    print("  }", file=f)
    print("", file=f)

    for index in range(len(blocks)):
        block = blocks[index]
        if block["type"] == "match":
            index_types = list()
            for entry in block["index"]:
                index_types.append(entry[0])
            value_types = ["Cell*"]
            for entry in block["setup"]:
                if entry[0] == "slice":
                    value_types.append("int")
                if entry[0] == "choice":
                    value_types.append(entry[1])
                if entry[0] == "define":
                    value_types.append(entry[1])
            print("  typedef std::tuple<{}> index_{}_key_type;".format(", ".join(index_types), index), file=f)
            print("  typedef std::tuple<{}> index_{}_value_type;".format(", ".join(value_types), index), file=f)
            print("  dict<index_{}_key_type, vector<index_{}_value_type>> index_{};".format(index, index, index), file=f)
    print("  dict<SigBit, pool<Cell*>> sigusers;", file=f)
    print("  pool<Cell*> blacklist_cells;", file=f)
    print("  pool<Cell*> autoremove_cells;", file=f)
    print("  dict<Cell*,int> rollback_cache;", file=f)
    print("  int rollback;", file=f)
    print("", file=f)

    for current_pattern in sorted(patterns.keys()):
        print("  struct state_{}_t {{".format(current_pattern), file=f)
        for s, t in sorted(state_types[current_pattern].items()):
            print("    {} {};".format(t, s), file=f)
        print("  }} st_{};".format(current_pattern), file=f)
        print("", file=f)

        print("  struct udata_{}_t {{".format(current_pattern), file=f)
        for s, t in sorted(udata_types[current_pattern].items()):
            print("    {} {};".format(t, s), file=f)
        print("  }} ud_{};".format(current_pattern), file=f)
        print("", file=f)
    current_pattern = None

    for v, n in sorted(ids.items()):
        if n[0] == "\\":
            print("  IdString {}{{\"\\{}\"}};".format(v, n), file=f)
        else:
            print("  IdString {}{{\"{}\"}};".format(v, n), file=f)
    print("", 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("      sigusers[bit].insert(cell);", file=f)
    print("    }", file=f)
    print("  }", file=f)
    print("", file=f)

    print("  void blacklist(Cell *cell) {", file=f)
    print("    if (cell != nullptr && blacklist_cells.insert(cell).second) {", file=f)
    print("      auto ptr = rollback_cache.find(cell);", file=f)
    print("      if (ptr == rollback_cache.end()) return;", file=f)
    print("      int rb = ptr->second;", file=f)
    print("      if (rollback == 0 || rollback > rb)", file=f)
    print("        rollback = rb;", file=f)
    print("    }", file=f)
    print("  }", file=f)
    print("", file=f)

    print("  void autoremove(Cell *cell) {", file=f)
    print("    if (cell != nullptr) {", file=f)
    print("      autoremove_cells.insert(cell);", file=f)
    print("      blacklist(cell);", file=f)
    print("    }", file=f)
    print("  }", file=f)
    print("", file=f)

    current_pattern = None

    print("  SigSpec port(Cell *cell, IdString portname) {", file=f)
    print("    return sigmap(cell->getPort(portname));", file=f)
    print("  }", file=f)
    print("", file=f)
    print("  SigSpec port(Cell *cell, IdString portname, const SigSpec& defval) {", file=f)
    print("    return sigmap(cell->connections_.at(portname, defval));", 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("  Const param(Cell *cell, IdString paramname, const Const& defval) {", file=f)
    print("    return cell->parameters.at(paramname, defval);", 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), setup_done(false), generate_mode(false), rngseed(12345678) {", file=f)
    print("    setup(cells);", file=f)
    print("  }", file=f)
    print("", file=f)

    print("  {}_pm(Module *module) :".format(prefix), file=f)
    print("      module(module), sigmap(module), setup_done(false), generate_mode(false), rngseed(12345678) {", file=f)
    print("  }", file=f)
    print("", file=f)

    print("  void setup(const vector<Cell*> &cells) {", file=f)
    for current_pattern in sorted(patterns.keys()):
        for s, t in sorted(udata_types[current_pattern].items()):
            if t.endswith("*"):
                print("    ud_{}.{} = nullptr;".format(current_pattern,s), file=f)
            else:
                print("    ud_{}.{} = {}();".format(current_pattern, s, t), file=f)
    current_pattern = None
    print("    log_assert(!setup_done);", file=f)
    print("    setup_done = true;", file=f)
    print("    for (auto port : module->ports)", file=f)
    print("      add_siguser(module->wire(port), nullptr);", file=f)
    print("    for (auto cell : module->cells())", file=f)
    print("      for (auto &conn : cell->connections())", file=f)
    print("        add_siguser(conn.second, cell);", file=f)
    print("    for (auto cell : cells) {", 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)
            print("        index_{}_value_type value;".format(index), file=f)
            print("        std::get<0>(value) = cell;", file=f)
            loopcnt = 0
            valueidx = 1
            for item in block["setup"]:
                if item[0] == "select":
                    print("        if (!({})) continue;".format(item[1]), file=f)
                if item[0] == "slice":
                    print("        int &{} = std::get<{}>(value);".format(item[1], valueidx), file=f)
                    print("        for ({} = 0; {} < {}; {}++) {{".format(item[1], item[1], item[2], item[1]), file=f)
                    valueidx += 1
                    loopcnt += 1
                if item[0] == "choice":
                    print("        vector<{}> _pmg_choices_{} = {};".format(item[1], item[2], item[3]), file=f)
                    print("        for (const {} &{} : _pmg_choices_{}) {{".format(item[1], item[2], item[2]), file=f)
                    print("        std::get<{}>(value) = {};".format(valueidx, item[2]), file=f)
                    valueidx += 1
                    loopcnt += 1
                if item[0] == "define":
                    print("        {} &{} = std::get<{}>(value);".format(item[1], item[2], valueidx), file=f)
                    print("        {} = {};".format(item[2], item[3]), file=f)
                    valueidx += 1
            print("        index_{}_key_type key;".format(index), file=f)
            for field, entry in enumerate(block["index"]):
                print("        std::get<{}>(key) = {};".format(field, entry[1]), file=f)
            print("        index_{}[key].push_back(value);".format(index), file=f)
            for i in range(loopcnt):
                print("        }", file=f)
            print("      } while (0);", file=f)

    print("    }", file=f)
    print("  }", file=f)
    print("", file=f)

    print("  ~{}_pm() {{".format(prefix), file=f)
    print("    for (auto cell : autoremove_cells)", file=f)
    print("      module->remove(cell);", file=f)
    print("  }", file=f)
    print("", file=f)

    for current_pattern in sorted(patterns.keys()):
        print("  int run_{}(std::function<void()> on_accept_f) {{".format(current_pattern), file=f)
        print("    log_assert(setup_done);", file=f)
        print("    accept_cnt = 0;", file=f)
        print("    on_accept = on_accept_f;", file=f)
        print("    rollback = 0;", file=f)
        for s, t in sorted(state_types[current_pattern].items()):
            if t.endswith("*"):
                print("    st_{}.{} = nullptr;".format(current_pattern, s), file=f)
            else:
                print("    st_{}.{} = {}();".format(current_pattern, s, t), file=f)
        print("    block_{}(1);".format(patterns[current_pattern]), file=f)
        print("    log_assert(rollback_cache.empty());", file=f)
        print("    return accept_cnt;", file=f)
        print("  }", file=f)
        print("", file=f)
        print("  int run_{}(std::function<void({}_pm&)> on_accept_f) {{".format(current_pattern, prefix), file=f)
        print("    return run_{}([&](){{on_accept_f(*this);}});".format(current_pattern), file=f)
        print("  }", file=f)
        print("", file=f)
        print("  int run_{}() {{".format(current_pattern), file=f)
        print("    return run_{}([](){{}});".format(current_pattern, current_pattern), file=f)
        print("  }", file=f)
        print("", file=f)

    if len(subpatterns):
        for p, s in sorted(subpatterns.keys()):
            print("  void block_subpattern_{}_{}(int recursion) {{ block_{}(recursion); }}".format(p, s, subpatterns[(p, s)]), file=f)
        print("", file=f)

    current_pattern = None
    current_subpattern = None

    for index in range(len(blocks)):
        block = blocks[index]

        if block["type"] in ("match", "code"):
            print("  // {}".format(block["src"]), file=f)

        print("  void block_{}(int recursion YS_MAYBE_UNUSED) {{".format(index), file=f)
        current_pattern, current_subpattern = block["pattern"]

        if block["type"] == "final":
            print("  }", file=f)
            if index+1 != len(blocks):
                print("", file=f)
            continue

        const_st = set()
        nonconst_st = set()
        restore_st = set()

        for s in subpattern_args[(current_pattern, current_subpattern)]:
            const_st.add(s)

        for i in range(subpatterns[(current_pattern, current_subpattern)], index):
            if blocks[i]["type"] == "code":
                for s in blocks[i]["states"]:
                    const_st.add(s)
            elif blocks[i]["type"] == "match":
                const_st.add(blocks[i]["cell"])
                for item in blocks[i]["sets"]:
                    const_st.add(item[0])
            else:
                assert False

        if block["type"] == "code":
            for s in block["states"]:
                if s in const_st:
                    const_st.remove(s)
                    restore_st.add(s)
                nonconst_st.add(s)
        elif block["type"] == "match":
            s = block["cell"]
            assert s not in const_st
            nonconst_st.add(s)
            for item in block["sets"]:
                if item[0] in const_st:
                    const_st.remove(item[0])
                nonconst_st.add(item[0])
        else:
            assert False

        for s in sorted(const_st):
            t = state_types[current_pattern][s]
            if t.endswith("*"):
                print("    {} const &{} YS_MAYBE_UNUSED = st_{}.{};".format(t, s, current_pattern, s), file=f)
            else:
                print("    const {} &{} YS_MAYBE_UNUSED = st_{}.{};".format(t, s, current_pattern, s), file=f)

        for s in sorted(nonconst_st):
            t = state_types[current_pattern][s]
            print("    {} &{} YS_MAYBE_UNUSED = st_{}.{};".format(t, s, current_pattern, s), file=f)

        for u in sorted(udata_types[current_pattern].keys()):
            t = udata_types[current_pattern][u]
            print("    {} &{} YS_MAYBE_UNUSED = ud_{}.{};".format(t, u, current_pattern, u), file=f)

        if len(restore_st):
            print("", file=f)
            for s in sorted(restore_st):
                t = state_types[current_pattern][s]
                print("    {} _pmg_backup_{} = {};".format(t, s, s), file=f)

        if block["type"] == "code":
            print("", file=f)
            print("#define reject do { goto rollback_label; } while(0)", file=f)
            print("#define accept do { accept_cnt++; on_accept(); if (rollback) goto rollback_label; } while(0)", file=f)
            print("#define finish do { rollback = -1; goto rollback_label; } while(0)", file=f)
            print("#define branch do {{ block_{}(recursion+1); if (rollback) goto rollback_label; }} while(0)".format(index+1), file=f)
            print("#define subpattern(pattern_name) do {{ block_subpattern_{}_ ## pattern_name (recursion+1); if (rollback) goto rollback_label; }} while(0)".format(current_pattern), file=f)

            for line in block["code"]:
                print("  " + line, file=f)

            print("", file=f)
            print("    block_{}(recursion+1);".format(index+1), file=f)

            print("#undef reject", file=f)
            print("#undef accept", file=f)
            print("#undef finish", file=f)
            print("#undef branch", file=f)
            print("#undef subpattern", file=f)

            print("", file=f)
            print("rollback_label:", file=f)
            print("    YS_MAYBE_UNUSED;", file=f)

            if len(block["fcode"]):
                print("#define accept do { accept_cnt++; on_accept(); } while(0)", file=f)
                print("#define finish do { rollback = -1; goto finish_label; } while(0)", file=f)
                for line in block["fcode"]:
                    print("  " + line, file=f)
                print("finish_label:", file=f)
                print("    YS_MAYBE_UNUSED;", file=f)
                print("#undef accept", file=f)
                print("#undef finish", file=f)

            if len(restore_st) or len(nonconst_st):
                print("", file=f)
                for s in sorted(restore_st):
                    t = state_types[current_pattern][s]
                    print("    {} = _pmg_backup_{};".format(s, s), file=f)
                for s in sorted(nonconst_st):
                    if s not in restore_st:
                        t = state_types[current_pattern][s]
                        if t.endswith("*"):
                            print("    {} = nullptr;".format(s), file=f)
                        else:
                            print("    {} = {}();".format(s, t), file=f)

        elif block["type"] == "match":
            assert len(restore_st) == 0

            print("    Cell* _pmg_backup_{} = {};".format(block["cell"], block["cell"]), file=f)

            if len(block["if"]):
                for expr in block["if"]:
                    print("", file=f)
                    print("    if (!({})) {{".format(expr), file=f)
                    print("      {} = nullptr;".format(block["cell"]), file=f)
                    print("      block_{}(recursion+1);".format(index+1), file=f)
                    print("      {} = _pmg_backup_{};".format(block["cell"], block["cell"]), file=f)
                    print("      return;", file=f)
                    print("    }", file=f)

            print("", file=f)
            print("    index_{}_key_type key;".format(index), file=f)
            for field, entry in enumerate(block["index"]):
                print("    std::get<{}>(key) = {};".format(field, entry[2]), file=f)
            print("    auto cells_ptr = index_{}.find(key);".format(index), file=f)

            if block["semioptional"] or block["genargs"] is not None:
                print("    bool found_any_match = false;", file=f)

            print("", file=f)
            print("    if (cells_ptr != index_{}.end()) {{".format(index), file=f)
            print("      const vector<index_{}_value_type> &cells = cells_ptr->second;".format(index), file=f)
            print("      for (int _pmg_idx = 0; _pmg_idx < GetSize(cells); _pmg_idx++) {", file=f)
            print("        {} = std::get<0>(cells[_pmg_idx]);".format(block["cell"]), file=f)
            valueidx = 1
            for item in block["setup"]:
                if item[0] == "slice":
                    print("        const int &{} YS_MAYBE_UNUSED = std::get<{}>(cells[_pmg_idx]);".format(item[1], valueidx), file=f)
                    valueidx += 1
                if item[0] == "choice":
                    print("        const {} &{} YS_MAYBE_UNUSED = std::get<{}>(cells[_pmg_idx]);".format(item[1], item[2], valueidx), file=f)
                    valueidx += 1
                if item[0] == "define":
                    print("        const {} &{} YS_MAYBE_UNUSED = std::get<{}>(cells[_pmg_idx]);".format(item[1], item[2], valueidx), file=f)
                    valueidx += 1
            print("        if (blacklist_cells.count({})) continue;".format(block["cell"]), file=f)
            for expr in block["filter"]:
                print("        if (!({})) continue;".format(expr), file=f)
            if block["semioptional"] or block["genargs"] is not None:
                print("        found_any_match = true;", file=f)
            for item in block["sets"]:
                print("        auto _pmg_backup_{} = {};".format(item[0], item[0]), file=f)
                print("        {} = {};".format(item[0], item[1]), file=f)
            print("        auto rollback_ptr = rollback_cache.insert(make_pair(std::get<0>(cells[_pmg_idx]), recursion));", file=f)
            print("        block_{}(recursion+1);".format(index+1), file=f)
            for item in block["sets"]:
                print("        {} = _pmg_backup_{};".format(item[0], item[0]), file=f)
            print("        if (rollback_ptr.second)", file=f)
            print("          rollback_cache.erase(rollback_ptr.first);", file=f)
            print("        if (rollback) {", file=f)
            print("          if (rollback != recursion) {{".format(index+1), file=f)
            print("            {} = _pmg_backup_{};".format(block["cell"], 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("", file=f)
            print("    {} = nullptr;".format(block["cell"]), file=f)

            if block["optional"]:
                print("    block_{}(recursion+1);".format(index+1), file=f)

            if block["semioptional"]:
                print("    if (!found_any_match) block_{}(recursion+1);".format(index+1), file=f)

            print("    {} = _pmg_backup_{};".format(block["cell"], block["cell"]), file=f)

            if block["genargs"] is not None:
                print("#define finish do { rollback = -1; return; } while(0)", file=f)
                print("    if (generate_mode && rng(100) < (found_any_match ? {} : {})) {{".format(block["genargs"][1], block["genargs"][0]), file=f)
                for line in block["gencode"]:
                    print("      " + line, file=f)
                print("    }", file=f)
                print("#undef finish", file=f)
        else:
            assert False

        current_pattern = None
        print("  }", file=f)
        print("", file=f)

    print("};", file=f)

    if genhdr:
        print("", file=f)
        print("YOSYS_NAMESPACE_END", file=f)