2 # yosys -- Yosys Open SYnthesis Suite
4 # Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 # Permission to use, copy, modify, and/or distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 import sys
, re
, os
, signal
21 if os
.name
== "posix":
23 from copy
import deepcopy
24 from select
import select
26 from queue
import Queue
, Empty
27 from threading
import Thread
30 # This is needed so that the recursive SMT2 S-expression parser
31 # does not run out of stack frames when parsing large expressions
32 if os
.name
== "posix":
33 smtio_reclimit
= 64 * 1024
34 if sys
.getrecursionlimit() < smtio_reclimit
:
35 sys
.setrecursionlimit(smtio_reclimit
)
37 current_rlimit_stack
= resource
.getrlimit(resource
.RLIMIT_STACK
)
38 if current_rlimit_stack
[0] != resource
.RLIM_INFINITY
:
39 smtio_stacksize
= 128 * 1024 * 1024
40 if os
.uname().sysname
== "Darwin":
41 # MacOS has rather conservative stack limits
42 smtio_stacksize
= 16 * 1024 * 1024
43 if current_rlimit_stack
[1] != resource
.RLIM_INFINITY
:
44 smtio_stacksize
= min(smtio_stacksize
, current_rlimit_stack
[1])
45 if current_rlimit_stack
[0] < smtio_stacksize
:
47 resource
.setrlimit(resource
.RLIMIT_STACK
, (smtio_stacksize
, current_rlimit_stack
[1]))
49 # couldn't get more stack, just run with what we have
53 # currently running solvers (so we can kill them)
54 running_solvers
= dict()
55 forced_shutdown
= False
58 def force_shutdown(signum
, frame
):
59 global forced_shutdown
60 if not forced_shutdown
:
61 forced_shutdown
= True
62 if signum
is not None:
63 print("<%s>" % signal
.Signals(signum
).name
)
64 for p
in running_solvers
.values():
65 # os.killpg(os.getpgid(p.pid), signal.SIGTERM)
66 os
.kill(p
.pid
, signal
.SIGTERM
)
69 if os
.name
== "posix":
70 signal
.signal(signal
.SIGHUP
, force_shutdown
)
71 signal
.signal(signal
.SIGINT
, force_shutdown
)
72 signal
.signal(signal
.SIGTERM
, force_shutdown
)
74 def except_hook(exctype
, value
, traceback
):
75 if not forced_shutdown
:
76 sys
.__excepthook
__(exctype
, value
, traceback
)
77 force_shutdown(None, None)
79 sys
.excepthook
= except_hook
83 "0": "0000", "1": "0001", "2": "0010", "3": "0011",
84 "4": "0100", "5": "0101", "6": "0110", "7": "0111",
85 "8": "1000", "9": "1001", "A": "1010", "B": "1011",
86 "C": "1100", "D": "1101", "E": "1110", "F": "1111",
87 "a": "1010", "b": "1011", "c": "1100", "d": "1101",
88 "e": "1110", "f": "1111"
96 self
.registers
= set()
97 self
.memories
= dict()
102 self
.asserts
= dict()
104 self
.anyconsts
= dict()
105 self
.anyseqs
= dict()
106 self
.allconsts
= dict()
107 self
.allseqs
= dict()
112 def __init__(self
, opts
=None):
120 self
.logic_dt
= False
122 self
.produce_models
= True
123 self
.smt2cache
= [list()]
125 self
.p_index
= solvers_index
129 self
.logic
= opts
.logic
130 self
.solver
= opts
.solver
131 self
.solver_opts
= opts
.solver_opts
132 self
.debug_print
= opts
.debug_print
133 self
.debug_file
= opts
.debug_file
134 self
.dummy_file
= opts
.dummy_file
135 self
.timeinfo
= opts
.timeinfo
136 self
.unroll
= opts
.unroll
137 self
.noincr
= opts
.noincr
138 self
.info_stmts
= opts
.info_stmts
139 self
.nocomments
= opts
.nocomments
142 self
.solver
= "yices"
143 self
.solver_opts
= list()
144 self
.debug_print
= False
145 self
.debug_file
= None
146 self
.dummy_file
= None
147 self
.timeinfo
= os
.name
!= "nt"
150 self
.info_stmts
= list()
151 self
.nocomments
= False
153 self
.start_time
= time()
155 self
.modinfo
= dict()
158 self
.setup_done
= False
161 if self
.p
is not None and not forced_shutdown
:
162 os
.killpg(os
.getpgid(self
.p
.pid
), signal
.SIGTERM
)
163 if running_solvers
is not None:
164 del running_solvers
[self
.p_index
]
167 assert not self
.setup_done
172 if self
.solver
== "yices":
174 self
.popen_vargs
= ['yices-smt2'] + self
.solver_opts
176 self
.popen_vargs
= ['yices-smt2', '--incremental'] + self
.solver_opts
178 if self
.solver
== "z3":
179 self
.popen_vargs
= ['z3', '-smt2', '-in'] + self
.solver_opts
181 if self
.solver
== "cvc4":
183 self
.popen_vargs
= ['cvc4', '--lang', 'smt2.6' if self
.logic_dt
else 'smt2'] + self
.solver_opts
185 self
.popen_vargs
= ['cvc4', '--incremental', '--lang', 'smt2.6' if self
.logic_dt
else 'smt2'] + self
.solver_opts
187 if self
.solver
== "mathsat":
188 self
.popen_vargs
= ['mathsat'] + self
.solver_opts
190 if self
.solver
== "boolector":
192 self
.popen_vargs
= ['boolector', '--smt2'] + self
.solver_opts
194 self
.popen_vargs
= ['boolector', '--smt2', '-i'] + self
.solver_opts
197 if self
.solver
== "abc":
198 if len(self
.solver_opts
) > 0:
199 self
.popen_vargs
= ['yosys-abc', '-S', '; '.join(self
.solver_opts
)]
201 self
.popen_vargs
= ['yosys-abc', '-S', '%blast; &sweep -C 5000; &syn4; &cec -s -m -C 2000']
202 self
.logic_ax
= False
206 if self
.solver
== "dummy":
207 assert self
.dummy_file
is not None
208 self
.dummy_fd
= open(self
.dummy_file
, "r")
210 if self
.dummy_file
is not None:
211 self
.dummy_fd
= open(self
.dummy_file
, "w")
216 assert not self
.forall
217 self
.logic_uf
= False
218 self
.unroll_idcnt
= 0
219 self
.unroll_buffer
= ""
220 self
.unroll_sorts
= set()
221 self
.unroll_objs
= set()
222 self
.unroll_decls
= dict()
223 self
.unroll_cache
= dict()
224 self
.unroll_stack
= list()
226 if self
.logic
is None:
228 if self
.logic_qf
: self
.logic
+= "QF_"
229 if self
.logic_ax
: self
.logic
+= "A"
230 if self
.logic_uf
: self
.logic
+= "UF"
231 if self
.logic_bv
: self
.logic
+= "BV"
232 if self
.logic_dt
: self
.logic
= "ALL"
234 self
.setup_done
= True
236 for stmt
in self
.info_stmts
:
239 if self
.produce_models
:
240 self
.write("(set-option :produce-models true)")
242 self
.write("(set-logic %s)" % self
.logic
)
245 secs
= int(time() - self
.start_time
)
246 return "## %3d:%02d:%02d " % (secs
// (60*60), (secs
// 60) % 60, secs
% 60)
248 def replace_in_stmt(self
, stmt
, pat
, repl
):
252 if isinstance(stmt
, list):
253 return [self
.replace_in_stmt(s
, pat
, repl
) for s
in stmt
]
257 def unroll_stmt(self
, stmt
):
258 if not isinstance(stmt
, list):
261 stmt
= [self
.unroll_stmt(s
) for s
in stmt
]
263 if len(stmt
) >= 2 and not isinstance(stmt
[0], list) and stmt
[0] in self
.unroll_decls
:
264 assert stmt
[1] in self
.unroll_objs
267 if key
not in self
.unroll_cache
:
268 decl
= deepcopy(self
.unroll_decls
[key
[0]])
270 self
.unroll_cache
[key
] = "|UNROLL#%d|" % self
.unroll_idcnt
271 decl
[1] = self
.unroll_cache
[key
]
272 self
.unroll_idcnt
+= 1
274 if decl
[0] == "declare-fun":
275 if isinstance(decl
[3], list) or decl
[3] not in self
.unroll_sorts
:
276 self
.unroll_objs
.add(decl
[1])
279 self
.unroll_objs
.add(decl
[1])
282 elif decl
[0] == "define-fun":
284 for arg_name
, arg_sort
in decl
[2]:
285 decl
[4] = self
.replace_in_stmt(decl
[4], arg_name
, key
[arg_index
])
290 decl
= self
.unroll_stmt(decl
)
291 self
.write(self
.unparse(decl
), unroll
=False)
293 return self
.unroll_cache
[key
]
297 def p_thread_main(self
):
299 data
= self
.p
.stdout
.readline().decode("ascii")
301 self
.p_queue
.put(data
)
303 self
.p_running
= False
306 assert self
.p
is None
308 self
.p
= subprocess
.Popen(self
.popen_vargs
, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
309 except FileNotFoundError
:
310 print("%s SMT Solver '%s' not found in path." % (self
.timestamp(), self
.popen_vargs
[0]), flush
=True)
312 running_solvers
[self
.p_index
] = self
.p
313 self
.p_running
= True
315 self
.p_queue
= Queue()
316 self
.p_thread
= Thread(target
=self
.p_thread_main
)
317 self
.p_thread
.start()
319 def p_write(self
, data
, flush
):
320 assert self
.p
is not None
321 self
.p
.stdin
.write(bytes(data
, "ascii"))
322 if flush
: self
.p
.stdin
.flush()
325 assert self
.p
is not None
326 if self
.p_next
is not None:
330 if not self
.p_running
:
332 return self
.p_queue
.get()
334 def p_poll(self
, timeout
=0.1):
335 assert self
.p
is not None
336 assert self
.p_running
337 if self
.p_next
is not None:
340 self
.p_next
= self
.p_queue
.get(True, timeout
)
346 assert self
.p
is not None
349 assert not self
.p_running
350 del running_solvers
[self
.p_index
]
356 def write(self
, stmt
, unroll
=True):
357 if stmt
.startswith(";"):
359 if not self
.setup_done
:
360 self
.info_stmts
.append(stmt
)
362 elif not self
.setup_done
:
367 if self
.nocomments
or self
.unroll
:
368 stmt
= re
.sub(r
" *;.*", "", stmt
)
369 if stmt
== "": return
371 if unroll
and self
.unroll
:
372 stmt
= self
.unroll_buffer
+ stmt
373 self
.unroll_buffer
= ""
375 s
= re
.sub(r
"\|[^|]*\|", "", stmt
)
376 if s
.count("(") != s
.count(")"):
377 self
.unroll_buffer
= stmt
+ " "
385 if len(s
) == 3 and s
[0] == "declare-sort" and s
[2] == "0":
386 self
.unroll_sorts
.add(s
[1])
389 elif len(s
) == 4 and s
[0] == "declare-fun" and s
[2] == [] and s
[3] in self
.unroll_sorts
:
390 self
.unroll_objs
.add(s
[1])
393 elif len(s
) >= 4 and s
[0] == "declare-fun":
394 for arg_sort
in s
[2]:
395 if arg_sort
in self
.unroll_sorts
:
396 self
.unroll_decls
[s
[1]] = s
399 elif len(s
) >= 4 and s
[0] == "define-fun":
400 for arg_name
, arg_sort
in s
[2]:
401 if arg_sort
in self
.unroll_sorts
:
402 self
.unroll_decls
[s
[1]] = s
405 stmt
= self
.unparse(self
.unroll_stmt(s
))
407 if stmt
== "(push 1)":
408 self
.unroll_stack
.append((
409 deepcopy(self
.unroll_sorts
),
410 deepcopy(self
.unroll_objs
),
411 deepcopy(self
.unroll_decls
),
412 deepcopy(self
.unroll_cache
),
415 if stmt
== "(pop 1)":
416 self
.unroll_sorts
, self
.unroll_objs
, self
.unroll_decls
, self
.unroll_cache
= self
.unroll_stack
.pop()
422 print(stmt
, file=self
.debug_file
)
423 self
.debug_file
.flush()
425 if self
.solver
!= "dummy":
427 if self
.p
is not None and not stmt
.startswith("(get-"):
429 if stmt
== "(push 1)":
430 self
.smt2cache
.append(list())
431 elif stmt
== "(pop 1)":
434 if self
.p
is not None:
435 self
.p_write(stmt
+ "\n", True)
436 self
.smt2cache
[-1].append(stmt
)
438 self
.p_write(stmt
+ "\n", True)
440 def info(self
, stmt
):
441 if not stmt
.startswith("; yosys-smt2-"):
444 fields
= stmt
.split()
446 if fields
[1] == "yosys-smt2-nomem":
447 if self
.logic
is None:
448 self
.logic_ax
= False
450 if fields
[1] == "yosys-smt2-nobv":
451 if self
.logic
is None:
452 self
.logic_bv
= False
454 if fields
[1] == "yosys-smt2-stdt":
455 if self
.logic
is None:
458 if fields
[1] == "yosys-smt2-forall":
459 if self
.logic
is None:
460 self
.logic_qf
= False
463 if fields
[1] == "yosys-smt2-module":
464 self
.curmod
= fields
[2]
465 self
.modinfo
[self
.curmod
] = SmtModInfo()
467 if fields
[1] == "yosys-smt2-cell":
468 self
.modinfo
[self
.curmod
].cells
[fields
[3]] = fields
[2]
470 if fields
[1] == "yosys-smt2-topmod":
471 self
.topmod
= fields
[2]
473 if fields
[1] == "yosys-smt2-input":
474 self
.modinfo
[self
.curmod
].inputs
.add(fields
[2])
475 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
477 if fields
[1] == "yosys-smt2-output":
478 self
.modinfo
[self
.curmod
].outputs
.add(fields
[2])
479 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
481 if fields
[1] == "yosys-smt2-register":
482 self
.modinfo
[self
.curmod
].registers
.add(fields
[2])
483 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
485 if fields
[1] == "yosys-smt2-memory":
486 self
.modinfo
[self
.curmod
].memories
[fields
[2]] = (int(fields
[3]), int(fields
[4]), int(fields
[5]), int(fields
[6]), fields
[7] == "async")
488 if fields
[1] == "yosys-smt2-wire":
489 self
.modinfo
[self
.curmod
].wires
.add(fields
[2])
490 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
492 if fields
[1] == "yosys-smt2-clock":
493 for edge
in fields
[3:]:
494 if fields
[2] not in self
.modinfo
[self
.curmod
].clocks
:
495 self
.modinfo
[self
.curmod
].clocks
[fields
[2]] = edge
496 elif self
.modinfo
[self
.curmod
].clocks
[fields
[2]] != edge
:
497 self
.modinfo
[self
.curmod
].clocks
[fields
[2]] = "event"
499 if fields
[1] == "yosys-smt2-assert":
500 self
.modinfo
[self
.curmod
].asserts
["%s_a %s" % (self
.curmod
, fields
[2])] = fields
[3]
502 if fields
[1] == "yosys-smt2-cover":
503 self
.modinfo
[self
.curmod
].covers
["%s_c %s" % (self
.curmod
, fields
[2])] = fields
[3]
505 if fields
[1] == "yosys-smt2-anyconst":
506 self
.modinfo
[self
.curmod
].anyconsts
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
507 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
509 if fields
[1] == "yosys-smt2-anyseq":
510 self
.modinfo
[self
.curmod
].anyseqs
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
511 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
513 if fields
[1] == "yosys-smt2-allconst":
514 self
.modinfo
[self
.curmod
].allconsts
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
515 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
517 if fields
[1] == "yosys-smt2-allseq":
518 self
.modinfo
[self
.curmod
].allseqs
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
519 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
521 def hiernets(self
, top
, regs_only
=False):
522 def hiernets_worker(nets
, mod
, cursor
):
523 for netname
in sorted(self
.modinfo
[mod
].wsize
.keys()):
524 if not regs_only
or netname
in self
.modinfo
[mod
].registers
:
525 nets
.append(cursor
+ [netname
])
526 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
527 hiernets_worker(nets
, celltype
, cursor
+ [cellname
])
530 hiernets_worker(nets
, top
, [])
533 def hieranyconsts(self
, top
):
534 def worker(results
, mod
, cursor
):
535 for name
, value
in sorted(self
.modinfo
[mod
].anyconsts
.items()):
536 width
= self
.modinfo
[mod
].asize
[name
]
537 results
.append((cursor
, name
, value
[0], value
[1], width
))
538 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
539 worker(results
, celltype
, cursor
+ [cellname
])
542 worker(results
, top
, [])
545 def hieranyseqs(self
, top
):
546 def worker(results
, mod
, cursor
):
547 for name
, value
in sorted(self
.modinfo
[mod
].anyseqs
.items()):
548 width
= self
.modinfo
[mod
].asize
[name
]
549 results
.append((cursor
, name
, value
[0], value
[1], width
))
550 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
551 worker(results
, celltype
, cursor
+ [cellname
])
554 worker(results
, top
, [])
557 def hierallconsts(self
, top
):
558 def worker(results
, mod
, cursor
):
559 for name
, value
in sorted(self
.modinfo
[mod
].allconsts
.items()):
560 width
= self
.modinfo
[mod
].asize
[name
]
561 results
.append((cursor
, name
, value
[0], value
[1], width
))
562 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
563 worker(results
, celltype
, cursor
+ [cellname
])
566 worker(results
, top
, [])
569 def hierallseqs(self
, top
):
570 def worker(results
, mod
, cursor
):
571 for name
, value
in sorted(self
.modinfo
[mod
].allseqs
.items()):
572 width
= self
.modinfo
[mod
].asize
[name
]
573 results
.append((cursor
, name
, value
[0], value
[1], width
))
574 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
575 worker(results
, celltype
, cursor
+ [cellname
])
578 worker(results
, top
, [])
581 def hiermems(self
, top
):
582 def hiermems_worker(mems
, mod
, cursor
):
583 for memname
in sorted(self
.modinfo
[mod
].memories
.keys()):
584 mems
.append(cursor
+ [memname
])
585 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
586 hiermems_worker(mems
, celltype
, cursor
+ [cellname
])
589 hiermems_worker(mems
, top
, [])
597 if self
.solver
== "dummy":
598 line
= self
.dummy_fd
.readline().strip()
600 line
= self
.p_read().strip()
601 if self
.dummy_file
is not None:
602 self
.dummy_fd
.write(line
+ "\n")
604 count_brackets
+= line
.count("(")
605 count_brackets
-= line
.count(")")
610 if count_brackets
== 0:
612 if self
.solver
!= "dummy" and self
.p
.poll():
613 print("%s Solver terminated unexpectedly: %s" % (self
.timestamp(), "".join(stmt
)), flush
=True)
617 if stmt
.startswith("(error"):
618 print("%s Solver Error: %s" % (self
.timestamp(), stmt
), flush
=True)
619 if self
.solver
!= "dummy":
627 print("> (check-sat)")
628 if self
.debug_file
and not self
.nocomments
:
629 print("; running check-sat..", file=self
.debug_file
)
630 self
.debug_file
.flush()
632 if self
.solver
!= "dummy":
634 if self
.p
is not None:
637 for cache_ctx
in self
.smt2cache
:
638 for cache_stmt
in cache_ctx
:
639 self
.p_write(cache_stmt
+ "\n", False)
641 self
.p_write("(check-sat)\n", True)
655 if count
% 10 == 0 or count
== 25:
659 m
= "(%d seconds)" % secs
661 m
= "(%d seconds -- %d:%02d)" % (secs
, secs
// 60, secs
% 60)
663 m
= "(%d seconds -- %d:%02d:%02d)" % (secs
, secs
// (60*60), (secs
// 60) % 60, secs
% 60)
665 print("%s %s %c" % ("\b \b" * num_bs
, m
, s
[i
]), end
="", file=sys
.stderr
)
669 print("\b" + s
[i
], end
="", file=sys
.stderr
)
675 print("\b \b" * num_bs
, end
="", file=sys
.stderr
)
680 while self
.p_poll(60):
687 elif count
in [5, 10, 15, 30]:
688 msg
= "%d minutes" % count
693 elif count
% 60 == 0:
694 msg
= "%d hours" % (count
// 60)
697 print("%s waiting for solver (%s)" % (self
.timestamp(), msg
), flush
=True)
702 print("(set-info :status %s)" % result
, file=self
.debug_file
)
703 print("(check-sat)", file=self
.debug_file
)
704 self
.debug_file
.flush()
706 if result
not in ["sat", "unsat"]:
708 print("%s Unexpected EOF response from solver." % (self
.timestamp()), flush
=True)
710 print("%s Unexpected response from solver: %s" % (self
.timestamp(), result
), flush
=True)
711 if self
.solver
!= "dummy":
717 def parse(self
, stmt
):
722 while stmt
[cursor
] != ')':
723 el
, le
= worker(stmt
[cursor
:])
726 return expr
, cursor
+1
731 while stmt
[cursor
] != '|':
735 return expr
, cursor
+1
737 if stmt
[0] in [" ", "\t", "\r", "\n"]:
738 el
, le
= worker(stmt
[1:])
743 while stmt
[cursor
] not in ["(", ")", "|", " ", "\t", "\r", "\n"]:
747 return worker(stmt
)[0]
749 def unparse(self
, stmt
):
750 if isinstance(stmt
, list):
751 return "(" + " ".join([self
.unparse(s
) for s
in stmt
]) + ")"
759 if len(v
) > 0 and v
[-1] == "1": d
+= 1
760 if len(v
) > 1 and v
[-2] == "1": d
+= 2
761 if len(v
) > 2 and v
[-3] == "1": d
+= 4
762 if len(v
) > 3 and v
[-4] == "1": d
+= 8
769 if type(v
) is list and len(v
) == 3 and v
[0] == "_" and v
[1].startswith("bv"):
770 x
, n
= int(v
[1][2:]), int(v
[2])
771 return "".join("1" if (x
& (1 << i
)) else "0" for i
in range(n
-1, -1, -1))
772 if v
== "true": return "1"
773 if v
== "false": return "0"
774 if v
.startswith("#b"):
776 if v
.startswith("#x"):
777 return "".join(hex_dict
.get(x
) for x
in v
[2:])
781 return int(self
.bv2bin(v
), 2)
784 self
.write("(get-value (%s))" % (expr
))
785 return self
.parse(self
.read())[0][1]
787 def get_list(self
, expr_list
):
788 if len(expr_list
) == 0:
790 self
.write("(get-value (%s))" % " ".join(expr_list
))
791 return [n
[1] for n
in self
.parse(self
.read())]
793 def get_path(self
, mod
, path
):
794 assert mod
in self
.modinfo
795 path
= path
.replace("\\", "/").split(".")
797 for i
in range(len(path
)-1):
798 first
= ".".join(path
[0:i
+1])
799 second
= ".".join(path
[i
+1:])
801 if first
in self
.modinfo
[mod
].cells
:
802 nextmod
= self
.modinfo
[mod
].cells
[first
]
803 return [first
] + self
.get_path(nextmod
, second
)
805 return [".".join(path
)]
807 def net_expr(self
, mod
, base
, path
):
812 assert mod
in self
.modinfo
815 if path
[0] in self
.modinfo
[mod
].cells
:
816 return "(|%s_h %s| %s)" % (mod
, path
[0], base
)
817 if path
[0] in self
.modinfo
[mod
].wsize
:
818 return "(|%s_n %s| %s)" % (mod
, path
[0], base
)
819 if path
[0] in self
.modinfo
[mod
].memories
:
820 return "(|%s_m %s| %s)" % (mod
, path
[0], base
)
823 assert mod
in self
.modinfo
824 assert path
[0] in self
.modinfo
[mod
].cells
826 nextmod
= self
.modinfo
[mod
].cells
[path
[0]]
827 nextbase
= "(|%s_h %s| %s)" % (mod
, path
[0], base
)
828 return self
.net_expr(nextmod
, nextbase
, path
[1:])
830 def net_width(self
, mod
, net_path
):
831 for i
in range(len(net_path
)-1):
832 assert mod
in self
.modinfo
833 assert net_path
[i
] in self
.modinfo
[mod
].cells
834 mod
= self
.modinfo
[mod
].cells
[net_path
[i
]]
836 assert mod
in self
.modinfo
837 assert net_path
[-1] in self
.modinfo
[mod
].wsize
838 return self
.modinfo
[mod
].wsize
[net_path
[-1]]
840 def net_clock(self
, mod
, net_path
):
841 for i
in range(len(net_path
)-1):
842 assert mod
in self
.modinfo
843 assert net_path
[i
] in self
.modinfo
[mod
].cells
844 mod
= self
.modinfo
[mod
].cells
[net_path
[i
]]
846 assert mod
in self
.modinfo
847 if net_path
[-1] not in self
.modinfo
[mod
].clocks
:
849 return self
.modinfo
[mod
].clocks
[net_path
[-1]]
851 def net_exists(self
, mod
, net_path
):
852 for i
in range(len(net_path
)-1):
853 if mod
not in self
.modinfo
: return False
854 if net_path
[i
] not in self
.modinfo
[mod
].cells
: return False
855 mod
= self
.modinfo
[mod
].cells
[net_path
[i
]]
857 if mod
not in self
.modinfo
: return False
858 if net_path
[-1] not in self
.modinfo
[mod
].wsize
: return False
861 def mem_exists(self
, mod
, mem_path
):
862 for i
in range(len(mem_path
)-1):
863 if mod
not in self
.modinfo
: return False
864 if mem_path
[i
] not in self
.modinfo
[mod
].cells
: return False
865 mod
= self
.modinfo
[mod
].cells
[mem_path
[i
]]
867 if mod
not in self
.modinfo
: return False
868 if mem_path
[-1] not in self
.modinfo
[mod
].memories
: return False
871 def mem_expr(self
, mod
, base
, path
, port
=None, infomode
=False):
873 assert mod
in self
.modinfo
874 assert path
[0] in self
.modinfo
[mod
].memories
876 return self
.modinfo
[mod
].memories
[path
[0]]
877 return "(|%s_m%s %s| %s)" % (mod
, "" if port
is None else ":%s" % port
, path
[0], base
)
879 assert mod
in self
.modinfo
880 assert path
[0] in self
.modinfo
[mod
].cells
882 nextmod
= self
.modinfo
[mod
].cells
[path
[0]]
883 nextbase
= "(|%s_h %s| %s)" % (mod
, path
[0], base
)
884 return self
.mem_expr(nextmod
, nextbase
, path
[1:], port
=port
, infomode
=infomode
)
886 def mem_info(self
, mod
, path
):
887 return self
.mem_expr(mod
, "", path
, infomode
=True)
889 def get_net(self
, mod_name
, net_path
, state_name
):
890 return self
.get(self
.net_expr(mod_name
, state_name
, net_path
))
892 def get_net_list(self
, mod_name
, net_path_list
, state_name
):
893 return self
.get_list([self
.net_expr(mod_name
, state_name
, n
) for n
in net_path_list
])
895 def get_net_hex(self
, mod_name
, net_path
, state_name
):
896 return self
.bv2hex(self
.get_net(mod_name
, net_path
, state_name
))
898 def get_net_hex_list(self
, mod_name
, net_path_list
, state_name
):
899 return [self
.bv2hex(v
) for v
in self
.get_net_list(mod_name
, net_path_list
, state_name
)]
901 def get_net_bin(self
, mod_name
, net_path
, state_name
):
902 return self
.bv2bin(self
.get_net(mod_name
, net_path
, state_name
))
904 def get_net_bin_list(self
, mod_name
, net_path_list
, state_name
):
905 return [self
.bv2bin(v
) for v
in self
.get_net_list(mod_name
, net_path_list
, state_name
)]
908 if self
.p
is not None:
915 self
.shortopts
= "s:S:v"
916 self
.longopts
= ["unroll", "noincr", "noprogress", "dump-smt2=", "logic=", "dummy=", "info=", "nocomments"]
917 self
.solver
= "yices"
918 self
.solver_opts
= list()
919 self
.debug_print
= False
920 self
.debug_file
= None
921 self
.dummy_file
= None
924 self
.timeinfo
= os
.name
!= "nt"
926 self
.info_stmts
= list()
927 self
.nocomments
= False
929 def handle(self
, o
, a
):
933 self
.solver_opts
.append(a
)
935 self
.debug_print
= True
936 elif o
== "--unroll":
938 elif o
== "--noincr":
940 elif o
== "--noprogress":
941 self
.timeinfo
= False
942 elif o
== "--dump-smt2":
943 self
.debug_file
= open(a
, "w")
949 self
.info_stmts
.append(a
)
950 elif o
== "--nocomments":
951 self
.nocomments
= True
959 set SMT solver: z3, yices, boolector, cvc4, mathsat, dummy
963 pass <opt> as command line argument to the solver
966 use the specified SMT2 logic (e.g. QF_AUFBV)
969 if solver is "dummy", read solver output from that file
970 otherwise: write solver output to that file
976 unroll uninterpreted functions
979 don't use incremental solving, instead restart solver for
980 each (check-sat). This also avoids (push) and (pop).
983 disable timer display during solving
984 (this option is set implicitly on Windows)
986 --dump-smt2 <filename>
987 write smt2 statements to file
989 --info <smt2-info-stmt>
990 include the specified smt2 info statement in the smt2 output
993 strip all comments from the generated smt2 code
998 def __init__(self
, f
):
1002 self
.clocks
= dict()
1004 def add_net(self
, path
, width
):
1007 key
= "n%d" % len(self
.nets
)
1008 self
.nets
[path
] = (key
, width
)
1010 def add_clock(self
, path
, edge
):
1013 key
= "n%d" % len(self
.nets
)
1014 self
.nets
[path
] = (key
, 1)
1015 self
.clocks
[path
] = (key
, edge
)
1017 def set_net(self
, path
, bits
):
1020 assert path
in self
.nets
1021 if path
not in self
.clocks
:
1022 print("b%s %s" % (bits
, self
.nets
[path
][0]), file=self
.f
)
1024 def escape_name(self
, name
):
1025 name
= re
.sub(r
"\[([0-9a-zA-Z_]*[a-zA-Z_][0-9a-zA-Z_]*)\]", r
"<\1>", name
)
1026 if re
.match("[\[\]]", name
) and name
[0] != "\\":
1030 def set_time(self
, t
):
1034 print("$version Generated by Yosys-SMTBMC $end", file=self
.f
)
1035 print("$timescale 1ns $end", file=self
.f
)
1036 print("$var integer 32 t smt_step $end", file=self
.f
)
1037 print("$var event 1 ! smt_clock $end", file=self
.f
)
1040 if n
.startswith("$") or ":" in n
:
1045 for path
in sorted(self
.nets
):
1046 key
, width
= self
.nets
[path
]
1049 if "." in uipath
[-1] and not uipath
[-1].startswith("$"):
1050 uipath
= uipath
[0:-1] + uipath
[-1].split(".")
1051 for i
in range(len(uipath
)):
1052 uipath
[i
] = re
.sub(r
"\[([^\]]*)\]", r
"<\1>", uipath
[i
])
1054 while uipath
[:len(scope
)] != scope
:
1055 print("$upscope $end", file=self
.f
)
1058 while uipath
[:-1] != scope
:
1059 scopename
= uipath
[len(scope
)]
1060 print("$scope module %s $end" % vcdescape(scopename
), file=self
.f
)
1061 scope
.append(uipath
[len(scope
)])
1063 if path
in self
.clocks
and self
.clocks
[path
][1] == "event":
1064 print("$var event 1 %s %s $end" % (key
, vcdescape(uipath
[-1])), file=self
.f
)
1066 print("$var wire %d %s %s $end" % (width
, key
, vcdescape(uipath
[-1])), file=self
.f
)
1068 for i
in range(len(scope
)):
1069 print("$upscope $end", file=self
.f
)
1071 print("$enddefinitions $end", file=self
.f
)
1077 print("#%d" % (10 * self
.t
- 5), file=self
.f
)
1078 for path
in sorted(self
.clocks
.keys()):
1079 if self
.clocks
[path
][1] == "posedge":
1080 print("b0 %s" % self
.nets
[path
][0], file=self
.f
)
1081 elif self
.clocks
[path
][1] == "negedge":
1082 print("b1 %s" % self
.nets
[path
][0], file=self
.f
)
1084 print("#%d" % (10 * self
.t
), file=self
.f
)
1085 print("1!", file=self
.f
)
1086 print("b%s t" % format(self
.t
, "032b"), file=self
.f
)
1088 for path
in sorted(self
.clocks
.keys()):
1089 if self
.clocks
[path
][1] == "negedge":
1090 print("b0 %s" % self
.nets
[path
][0], file=self
.f
)
1092 print("b1 %s" % self
.nets
[path
][0], file=self
.f
)