yosys/backends/smt2/smtbmc.py

169 lines
4.3 KiB
Python
Raw Normal View History

2015-10-13 10:17:23 -05:00
#!/usr/bin/env python3
import os, sys, getopt, re
from smtio import smtio, smtopts, mkvcd
2015-10-13 18:27:55 -05:00
skip_steps = 0
num_steps = 20
2015-10-13 10:17:23 -05:00
vcdfile = None
tempind = False
2015-10-13 18:27:55 -05:00
assume_skipped = None
2015-10-13 10:17:23 -05:00
topmod = "main"
so = smtopts()
2015-10-13 17:18:38 -05:00
2015-10-13 10:17:23 -05:00
def usage():
print("""
2015-10-13 17:37:41 -05:00
yosys-smtbmc [options] <yosys_smt2_output>
2015-10-13 10:17:23 -05:00
2015-10-13 18:27:55 -05:00
-t <num_steps>, -t <skip_steps>:<num_steps>
default: skip_steps=0, num_steps=20
-u <start_step>
assume asserts in skipped steps in BMC
2015-10-13 10:17:23 -05:00
-c <vcd_filename>
write counter-example to this VCD file
2015-10-13 17:37:41 -05:00
(hint: use 'write_smt2 -wires' for maximum
coverage of signals in generated VCD file)
2015-10-13 10:17:23 -05:00
2015-10-13 18:27:55 -05:00
-i
2015-10-13 10:17:23 -05:00
instead of BMC run temporal induction
-m <module_name>
name of the top module, default: main
""" + so.helpmsg())
sys.exit(1)
2015-10-13 17:18:38 -05:00
2015-10-13 10:17:23 -05:00
try:
2015-10-13 18:27:55 -05:00
opts, args = getopt.getopt(sys.argv[1:], so.optstr + "t:u:c:im:")
2015-10-13 10:17:23 -05:00
except:
usage()
for o, a in opts:
if o == "-t":
2015-10-13 18:27:55 -05:00
match = re.match(r"(\d+):(.*)", a)
if match:
skip_steps = int(match.group(1))
num_steps = int(match.group(2))
else:
num_steps = int(a)
elif o == "-u":
assume_skipped = int(a)
2015-10-13 10:17:23 -05:00
elif o == "-c":
vcdfile = a
elif o == "-i":
tempind = True
2015-10-13 17:37:41 -05:00
elif o == "-m":
topmod = a
2015-10-13 10:17:23 -05:00
elif so.handle(o, a):
pass
else:
usage()
if len(args) != 1:
usage()
2015-10-13 17:18:38 -05:00
2015-10-13 10:17:23 -05:00
smt = smtio(opts=so)
2015-10-13 18:27:55 -05:00
print("%s Solver: %s" % (smt.timestamp(), so.solver))
2015-10-13 10:17:23 -05:00
smt.setup("QF_AUFBV")
debug_nets = set()
debug_nets_re = re.compile(r"^; yosys-smt2-(input|output|register|wire) (\S+) (\d+)")
with open(args[0], "r") as f:
for line in f:
match = debug_nets_re.match(line)
if match:
debug_nets.add(match.group(2))
smt.write(line)
2015-10-13 17:18:38 -05:00
2015-10-13 10:17:23 -05:00
def write_vcd_model():
print("%s Writing model to VCD file." % smt.timestamp())
vcd = mkvcd(open(vcdfile, "w"))
for netname in sorted(debug_nets):
width = len(smt.get_net_bin("main", netname, "s0"))
vcd.add_net(netname, width)
for i in range(step+1):
vcd.set_time(i)
for netname in debug_nets:
vcd.set_net(netname, smt.get_net_bin("main", netname, "s%d" % i))
vcd.set_time(step+1)
2015-10-13 17:18:38 -05:00
if tempind:
2015-10-13 18:27:55 -05:00
for step in range(num_steps, -1, -1):
2015-10-13 17:18:38 -05:00
smt.write("(declare-fun s%d () %s_s)" % (step, topmod))
smt.write("(assert (%s_u s%d))" % (topmod, step))
2015-10-13 10:17:23 -05:00
2015-10-13 18:27:55 -05:00
if step == num_steps:
2015-10-13 17:18:38 -05:00
smt.write("(assert (not (%s_a s%d)))" % (topmod, step))
else:
smt.write("(assert (%s_t s%d s%d))" % (topmod, step, step+1))
smt.write("(assert (%s_a s%d))" % (topmod, step))
2015-10-13 18:27:55 -05:00
if step > num_steps-skip_steps:
2015-10-13 17:18:38 -05:00
print("%s Skipping induction in step %d.." % (smt.timestamp(), step))
continue
print("%s Trying induction in step %d.." % (smt.timestamp(), step))
if smt.check_sat() == "sat":
if step == 0:
print("%s temporal induction failed!" % smt.timestamp())
if vcdfile is not None:
write_vcd_model()
else:
print("%s PASSED." % smt.timestamp())
break
2015-10-13 17:37:41 -05:00
2015-10-13 17:18:38 -05:00
else: # not tempind
2015-10-13 18:27:55 -05:00
for step in range(num_steps+1):
2015-10-13 17:18:38 -05:00
smt.write("(declare-fun s%d () %s_s)" % (step, topmod))
smt.write("(assert (%s_u s%d))" % (topmod, step))
if step == 0:
smt.write("(assert (%s_i s0))" % (topmod))
else:
smt.write("(assert (%s_t s%d s%d))" % (topmod, step-1, step))
2015-10-13 18:27:55 -05:00
if step < skip_steps:
if assume_skipped is not None and step >= assume_skipped:
print("%s Skipping step %d (and assuming pass).." % (smt.timestamp(), step))
smt.write("(assert (%s_a s%d))" % (topmod, step))
else:
print("%s Skipping step %d.." % (smt.timestamp(), step))
continue
2015-10-13 17:18:38 -05:00
print("%s Checking asserts in step %d.." % (smt.timestamp(), step))
smt.write("(push 1)")
2015-10-13 10:17:23 -05:00
2015-10-13 17:18:38 -05:00
smt.write("(assert (not (%s_a s%d)))" % (topmod, step))
2015-10-13 10:17:23 -05:00
2015-10-13 17:18:38 -05:00
if smt.check_sat() == "sat":
print("%s BMC failed!" % smt.timestamp())
if vcdfile is not None:
write_vcd_model()
break
2015-10-13 10:17:23 -05:00
2015-10-13 17:18:38 -05:00
else: # unsat
smt.write("(pop 1)")
smt.write("(assert (%s_a s%d))" % (topmod, step))
2015-10-13 10:17:23 -05:00
print("%s Done." % smt.timestamp())
smt.write("(exit)")
smt.wait()