utils: add parse-db

Signed-off-by: Alessandro Comodi <acomodi@antmicro.com>
diff --git a/utils/parsedb.py b/utils/parsedb.py
new file mode 100755
index 0000000..4907ca6
--- /dev/null
+++ b/utils/parsedb.py
@@ -0,0 +1,62 @@
+#!/usr/bin/env python3
+
+import sys
+import re
+import util
+
+
+def run(fnin, fnout=None, strict=False, verbose=False):
+    lines = open(fnin, 'r').read().split('\n')
+    tags = dict()
+    bitss = dict()
+    for line in lines:
+        line = line.strip()
+        if line == '':
+            continue
+        # TODO: figure out what to do with masks
+        if line.startswith("bit "):
+            continue
+        tag, bits, mode, _ = util.parse_db_line(line)
+        if strict:
+            if mode != "always":
+                assert not mode, "strict: got ill defined line: %s" % (line, )
+            if tag in tags:
+                print("Original line: %s" % tags[tag], file=sys.stderr)
+                print("New line: %s" % line, file=sys.stderr)
+                assert 0, "strict: got duplicate tag %s" % (tag, )
+            assert bits not in bitss, "strict: got duplicate bits %s: %s %s" % (
+                bits, tag, bitss[bits])
+        tags[tag] = line
+        if bits != None:
+            bitss[bits] = tag
+
+    if fnout:
+        with open(fnout, "w") as fout:
+            for line in sorted(lines):
+                line = line.strip()
+                if line == '':
+                    continue
+                fout.write(line + '\n')
+
+
+def main():
+    import argparse
+
+    parser = argparse.ArgumentParser(
+        description="Parse a db file, checking for consistency")
+
+    util.db_root_arg(parser)
+    parser.add_argument('--verbose', action='store_true', help='')
+    parser.add_argument(
+        '--strict',
+        action='store_true',
+        help='Complain on unresolved entries (ex: <0 candidates>, <const0>)')
+    parser.add_argument('fin', help='')
+    parser.add_argument('fout', nargs='?', help='')
+    args = parser.parse_args()
+
+    run(args.fin, args.fout, strict=args.strict, verbose=args.verbose)
+
+
+if __name__ == '__main__':
+    main()
diff --git a/utils/util.py b/utils/util.py
index 5fec063..50d4086 100644
--- a/utils/util.py
+++ b/utils/util.py
@@ -2,7 +2,7 @@
 import os
 import random
 import re
-from .roi import Roi
+from utils.roi import Roi
 from jinja2 import Environment, FileSystemLoader
 
 def get_jinja_template(tpl_file, directory = "./"):