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
:
46 resource
.setrlimit(resource
.RLIMIT_STACK
, (smtio_stacksize
, current_rlimit_stack
[1]))
49 # currently running solvers (so we can kill them)
50 running_solvers
= dict()
51 forced_shutdown
= False
54 def force_shutdown(signum
, frame
):
55 global forced_shutdown
56 if not forced_shutdown
:
57 forced_shutdown
= True
58 if signum
is not None:
59 print("<%s>" % signal
.Signals(signum
).name
)
60 for p
in running_solvers
.values():
61 # os.killpg(os.getpgid(p.pid), signal.SIGTERM)
62 os
.kill(p
.pid
, signal
.SIGTERM
)
65 if os
.name
== "posix":
66 signal
.signal(signal
.SIGHUP
, force_shutdown
)
67 signal
.signal(signal
.SIGINT
, force_shutdown
)
68 signal
.signal(signal
.SIGTERM
, force_shutdown
)
70 def except_hook(exctype
, value
, traceback
):
71 if not forced_shutdown
:
72 sys
.__excepthook
__(exctype
, value
, traceback
)
73 force_shutdown(None, None)
75 sys
.excepthook
= except_hook
79 "0": "0000", "1": "0001", "2": "0010", "3": "0011",
80 "4": "0100", "5": "0101", "6": "0110", "7": "0111",
81 "8": "1000", "9": "1001", "A": "1010", "B": "1011",
82 "C": "1100", "D": "1101", "E": "1110", "F": "1111",
83 "a": "1010", "b": "1011", "c": "1100", "d": "1101",
84 "e": "1110", "f": "1111"
92 self
.registers
= set()
93 self
.memories
= dict()
100 self
.anyconsts
= dict()
101 self
.anyseqs
= dict()
102 self
.allconsts
= dict()
103 self
.allseqs
= dict()
108 def __init__(self
, opts
=None):
116 self
.logic_dt
= False
118 self
.produce_models
= True
119 self
.smt2cache
= [list()]
121 self
.p_index
= solvers_index
125 self
.logic
= opts
.logic
126 self
.solver
= opts
.solver
127 self
.solver_opts
= opts
.solver_opts
128 self
.debug_print
= opts
.debug_print
129 self
.debug_file
= opts
.debug_file
130 self
.dummy_file
= opts
.dummy_file
131 self
.timeinfo
= opts
.timeinfo
132 self
.unroll
= opts
.unroll
133 self
.noincr
= opts
.noincr
134 self
.info_stmts
= opts
.info_stmts
135 self
.nocomments
= opts
.nocomments
138 self
.solver
= "yices"
139 self
.solver_opts
= list()
140 self
.debug_print
= False
141 self
.debug_file
= None
142 self
.dummy_file
= None
143 self
.timeinfo
= os
.name
!= "nt"
146 self
.info_stmts
= list()
147 self
.nocomments
= False
149 self
.start_time
= time()
151 self
.modinfo
= dict()
154 self
.setup_done
= False
157 if self
.p
is not None and not forced_shutdown
:
158 os
.killpg(os
.getpgid(self
.p
.pid
), signal
.SIGTERM
)
159 if running_solvers
is not None:
160 del running_solvers
[self
.p_index
]
163 assert not self
.setup_done
168 if self
.solver
== "yices":
170 self
.popen_vargs
= ['yices-smt2'] + self
.solver_opts
172 self
.popen_vargs
= ['yices-smt2', '--incremental'] + self
.solver_opts
174 if self
.solver
== "z3":
175 self
.popen_vargs
= ['z3', '-smt2', '-in'] + self
.solver_opts
177 if self
.solver
== "cvc4":
179 self
.popen_vargs
= ['cvc4', '--lang', 'smt2.6' if self
.logic_dt
else 'smt2'] + self
.solver_opts
181 self
.popen_vargs
= ['cvc4', '--incremental', '--lang', 'smt2.6' if self
.logic_dt
else 'smt2'] + self
.solver_opts
183 if self
.solver
== "mathsat":
184 self
.popen_vargs
= ['mathsat'] + self
.solver_opts
186 if self
.solver
== "boolector":
188 self
.popen_vargs
= ['boolector', '--smt2'] + self
.solver_opts
190 self
.popen_vargs
= ['boolector', '--smt2', '-i'] + self
.solver_opts
193 if self
.solver
== "abc":
194 if len(self
.solver_opts
) > 0:
195 self
.popen_vargs
= ['yosys-abc', '-S', '; '.join(self
.solver_opts
)]
197 self
.popen_vargs
= ['yosys-abc', '-S', '%blast; &sweep -C 5000; &syn4; &cec -s -m -C 2000']
198 self
.logic_ax
= False
202 if self
.solver
== "dummy":
203 assert self
.dummy_file
is not None
204 self
.dummy_fd
= open(self
.dummy_file
, "r")
206 if self
.dummy_file
is not None:
207 self
.dummy_fd
= open(self
.dummy_file
, "w")
212 assert not self
.forall
213 self
.logic_uf
= False
214 self
.unroll_idcnt
= 0
215 self
.unroll_buffer
= ""
216 self
.unroll_sorts
= set()
217 self
.unroll_objs
= set()
218 self
.unroll_decls
= dict()
219 self
.unroll_cache
= dict()
220 self
.unroll_stack
= list()
222 if self
.logic
is None:
224 if self
.logic_qf
: self
.logic
+= "QF_"
225 if self
.logic_ax
: self
.logic
+= "A"
226 if self
.logic_uf
: self
.logic
+= "UF"
227 if self
.logic_bv
: self
.logic
+= "BV"
228 if self
.logic_dt
: self
.logic
= "ALL"
230 self
.setup_done
= True
232 for stmt
in self
.info_stmts
:
235 if self
.produce_models
:
236 self
.write("(set-option :produce-models true)")
238 self
.write("(set-logic %s)" % self
.logic
)
241 secs
= int(time() - self
.start_time
)
242 return "## %3d:%02d:%02d " % (secs
// (60*60), (secs
// 60) % 60, secs
% 60)
244 def replace_in_stmt(self
, stmt
, pat
, repl
):
248 if isinstance(stmt
, list):
249 return [self
.replace_in_stmt(s
, pat
, repl
) for s
in stmt
]
253 def unroll_stmt(self
, stmt
):
254 if not isinstance(stmt
, list):
257 stmt
= [self
.unroll_stmt(s
) for s
in stmt
]
259 if len(stmt
) >= 2 and not isinstance(stmt
[0], list) and stmt
[0] in self
.unroll_decls
:
260 assert stmt
[1] in self
.unroll_objs
263 if key
not in self
.unroll_cache
:
264 decl
= deepcopy(self
.unroll_decls
[key
[0]])
266 self
.unroll_cache
[key
] = "|UNROLL#%d|" % self
.unroll_idcnt
267 decl
[1] = self
.unroll_cache
[key
]
268 self
.unroll_idcnt
+= 1
270 if decl
[0] == "declare-fun":
271 if isinstance(decl
[3], list) or decl
[3] not in self
.unroll_sorts
:
272 self
.unroll_objs
.add(decl
[1])
275 self
.unroll_objs
.add(decl
[1])
278 elif decl
[0] == "define-fun":
280 for arg_name
, arg_sort
in decl
[2]:
281 decl
[4] = self
.replace_in_stmt(decl
[4], arg_name
, key
[arg_index
])
286 decl
= self
.unroll_stmt(decl
)
287 self
.write(self
.unparse(decl
), unroll
=False)
289 return self
.unroll_cache
[key
]
293 def p_thread_main(self
):
295 data
= self
.p
.stdout
.readline().decode("ascii")
297 self
.p_queue
.put(data
)
299 self
.p_running
= False
302 assert self
.p
is None
303 self
.p
= subprocess
.Popen(self
.popen_vargs
, stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
304 running_solvers
[self
.p_index
] = self
.p
305 self
.p_running
= True
307 self
.p_queue
= Queue()
308 self
.p_thread
= Thread(target
=self
.p_thread_main
)
309 self
.p_thread
.start()
311 def p_write(self
, data
, flush
):
312 assert self
.p
is not None
313 self
.p
.stdin
.write(bytes(data
, "ascii"))
314 if flush
: self
.p
.stdin
.flush()
317 assert self
.p
is not None
318 if self
.p_next
is not None:
322 if not self
.p_running
:
324 return self
.p_queue
.get()
326 def p_poll(self
, timeout
=0.1):
327 assert self
.p
is not None
328 assert self
.p_running
329 if self
.p_next
is not None:
332 self
.p_next
= self
.p_queue
.get(True, timeout
)
338 assert self
.p
is not None
341 assert not self
.p_running
342 del running_solvers
[self
.p_index
]
348 def write(self
, stmt
, unroll
=True):
349 if stmt
.startswith(";"):
351 if not self
.setup_done
:
352 self
.info_stmts
.append(stmt
)
354 elif not self
.setup_done
:
359 if self
.nocomments
or self
.unroll
:
360 stmt
= re
.sub(r
" *;.*", "", stmt
)
361 if stmt
== "": return
363 if unroll
and self
.unroll
:
364 stmt
= self
.unroll_buffer
+ stmt
365 self
.unroll_buffer
= ""
367 s
= re
.sub(r
"\|[^|]*\|", "", stmt
)
368 if s
.count("(") != s
.count(")"):
369 self
.unroll_buffer
= stmt
+ " "
377 if len(s
) == 3 and s
[0] == "declare-sort" and s
[2] == "0":
378 self
.unroll_sorts
.add(s
[1])
381 elif len(s
) == 4 and s
[0] == "declare-fun" and s
[2] == [] and s
[3] in self
.unroll_sorts
:
382 self
.unroll_objs
.add(s
[1])
385 elif len(s
) >= 4 and s
[0] == "declare-fun":
386 for arg_sort
in s
[2]:
387 if arg_sort
in self
.unroll_sorts
:
388 self
.unroll_decls
[s
[1]] = s
391 elif len(s
) >= 4 and s
[0] == "define-fun":
392 for arg_name
, arg_sort
in s
[2]:
393 if arg_sort
in self
.unroll_sorts
:
394 self
.unroll_decls
[s
[1]] = s
397 stmt
= self
.unparse(self
.unroll_stmt(s
))
399 if stmt
== "(push 1)":
400 self
.unroll_stack
.append((
401 deepcopy(self
.unroll_sorts
),
402 deepcopy(self
.unroll_objs
),
403 deepcopy(self
.unroll_decls
),
404 deepcopy(self
.unroll_cache
),
407 if stmt
== "(pop 1)":
408 self
.unroll_sorts
, self
.unroll_objs
, self
.unroll_decls
, self
.unroll_cache
= self
.unroll_stack
.pop()
414 print(stmt
, file=self
.debug_file
)
415 self
.debug_file
.flush()
417 if self
.solver
!= "dummy":
419 if self
.p
is not None and not stmt
.startswith("(get-"):
421 if stmt
== "(push 1)":
422 self
.smt2cache
.append(list())
423 elif stmt
== "(pop 1)":
426 if self
.p
is not None:
427 self
.p_write(stmt
+ "\n", True)
428 self
.smt2cache
[-1].append(stmt
)
430 self
.p_write(stmt
+ "\n", True)
432 def info(self
, stmt
):
433 if not stmt
.startswith("; yosys-smt2-"):
436 fields
= stmt
.split()
438 if fields
[1] == "yosys-smt2-nomem":
439 if self
.logic
is None:
440 self
.logic_ax
= False
442 if fields
[1] == "yosys-smt2-nobv":
443 if self
.logic
is None:
444 self
.logic_bv
= False
446 if fields
[1] == "yosys-smt2-stdt":
447 if self
.logic
is None:
450 if fields
[1] == "yosys-smt2-forall":
451 if self
.logic
is None:
452 self
.logic_qf
= False
455 if fields
[1] == "yosys-smt2-module":
456 self
.curmod
= fields
[2]
457 self
.modinfo
[self
.curmod
] = SmtModInfo()
459 if fields
[1] == "yosys-smt2-cell":
460 self
.modinfo
[self
.curmod
].cells
[fields
[3]] = fields
[2]
462 if fields
[1] == "yosys-smt2-topmod":
463 self
.topmod
= fields
[2]
465 if fields
[1] == "yosys-smt2-input":
466 self
.modinfo
[self
.curmod
].inputs
.add(fields
[2])
467 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
469 if fields
[1] == "yosys-smt2-output":
470 self
.modinfo
[self
.curmod
].outputs
.add(fields
[2])
471 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
473 if fields
[1] == "yosys-smt2-register":
474 self
.modinfo
[self
.curmod
].registers
.add(fields
[2])
475 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
477 if fields
[1] == "yosys-smt2-memory":
478 self
.modinfo
[self
.curmod
].memories
[fields
[2]] = (int(fields
[3]), int(fields
[4]), int(fields
[5]), int(fields
[6]), fields
[7] == "async")
480 if fields
[1] == "yosys-smt2-wire":
481 self
.modinfo
[self
.curmod
].wires
.add(fields
[2])
482 self
.modinfo
[self
.curmod
].wsize
[fields
[2]] = int(fields
[3])
484 if fields
[1] == "yosys-smt2-clock":
485 for edge
in fields
[3:]:
486 if fields
[2] not in self
.modinfo
[self
.curmod
].clocks
:
487 self
.modinfo
[self
.curmod
].clocks
[fields
[2]] = edge
488 elif self
.modinfo
[self
.curmod
].clocks
[fields
[2]] != edge
:
489 self
.modinfo
[self
.curmod
].clocks
[fields
[2]] = "event"
491 if fields
[1] == "yosys-smt2-assert":
492 self
.modinfo
[self
.curmod
].asserts
["%s_a %s" % (self
.curmod
, fields
[2])] = fields
[3]
494 if fields
[1] == "yosys-smt2-cover":
495 self
.modinfo
[self
.curmod
].covers
["%s_c %s" % (self
.curmod
, fields
[2])] = fields
[3]
497 if fields
[1] == "yosys-smt2-anyconst":
498 self
.modinfo
[self
.curmod
].anyconsts
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
499 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
501 if fields
[1] == "yosys-smt2-anyseq":
502 self
.modinfo
[self
.curmod
].anyseqs
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
503 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
505 if fields
[1] == "yosys-smt2-allconst":
506 self
.modinfo
[self
.curmod
].allconsts
[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-allseq":
510 self
.modinfo
[self
.curmod
].allseqs
[fields
[2]] = (fields
[4], None if len(fields
) <= 5 else fields
[5])
511 self
.modinfo
[self
.curmod
].asize
[fields
[2]] = int(fields
[3])
513 def hiernets(self
, top
, regs_only
=False):
514 def hiernets_worker(nets
, mod
, cursor
):
515 for netname
in sorted(self
.modinfo
[mod
].wsize
.keys()):
516 if not regs_only
or netname
in self
.modinfo
[mod
].registers
:
517 nets
.append(cursor
+ [netname
])
518 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
519 hiernets_worker(nets
, celltype
, cursor
+ [cellname
])
522 hiernets_worker(nets
, top
, [])
525 def hieranyconsts(self
, top
):
526 def worker(results
, mod
, cursor
):
527 for name
, value
in sorted(self
.modinfo
[mod
].anyconsts
.items()):
528 width
= self
.modinfo
[mod
].asize
[name
]
529 results
.append((cursor
, name
, value
[0], value
[1], width
))
530 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
531 worker(results
, celltype
, cursor
+ [cellname
])
534 worker(results
, top
, [])
537 def hieranyseqs(self
, top
):
538 def worker(results
, mod
, cursor
):
539 for name
, value
in sorted(self
.modinfo
[mod
].anyseqs
.items()):
540 width
= self
.modinfo
[mod
].asize
[name
]
541 results
.append((cursor
, name
, value
[0], value
[1], width
))
542 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
543 worker(results
, celltype
, cursor
+ [cellname
])
546 worker(results
, top
, [])
549 def hierallconsts(self
, top
):
550 def worker(results
, mod
, cursor
):
551 for name
, value
in sorted(self
.modinfo
[mod
].allconsts
.items()):
552 width
= self
.modinfo
[mod
].asize
[name
]
553 results
.append((cursor
, name
, value
[0], value
[1], width
))
554 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
555 worker(results
, celltype
, cursor
+ [cellname
])
558 worker(results
, top
, [])
561 def hierallseqs(self
, top
):
562 def worker(results
, mod
, cursor
):
563 for name
, value
in sorted(self
.modinfo
[mod
].allseqs
.items()):
564 width
= self
.modinfo
[mod
].asize
[name
]
565 results
.append((cursor
, name
, value
[0], value
[1], width
))
566 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
567 worker(results
, celltype
, cursor
+ [cellname
])
570 worker(results
, top
, [])
573 def hiermems(self
, top
):
574 def hiermems_worker(mems
, mod
, cursor
):
575 for memname
in sorted(self
.modinfo
[mod
].memories
.keys()):
576 mems
.append(cursor
+ [memname
])
577 for cellname
, celltype
in sorted(self
.modinfo
[mod
].cells
.items()):
578 hiermems_worker(mems
, celltype
, cursor
+ [cellname
])
581 hiermems_worker(mems
, top
, [])
589 if self
.solver
== "dummy":
590 line
= self
.dummy_fd
.readline().strip()
592 line
= self
.p_read().strip()
593 if self
.dummy_file
is not None:
594 self
.dummy_fd
.write(line
+ "\n")
596 count_brackets
+= line
.count("(")
597 count_brackets
-= line
.count(")")
602 if count_brackets
== 0:
604 if self
.solver
!= "dummy" and self
.p
.poll():
605 print("%s Solver terminated unexpectedly: %s" % (self
.timestamp(), "".join(stmt
)), flush
=True)
609 if stmt
.startswith("(error"):
610 print("%s Solver Error: %s" % (self
.timestamp(), stmt
), flush
=True)
611 if self
.solver
!= "dummy":
619 print("> (check-sat)")
620 if self
.debug_file
and not self
.nocomments
:
621 print("; running check-sat..", file=self
.debug_file
)
622 self
.debug_file
.flush()
624 if self
.solver
!= "dummy":
626 if self
.p
is not None:
629 for cache_ctx
in self
.smt2cache
:
630 for cache_stmt
in cache_ctx
:
631 self
.p_write(cache_stmt
+ "\n", False)
633 self
.p_write("(check-sat)\n", True)
647 if count
% 10 == 0 or count
== 25:
651 m
= "(%d seconds)" % secs
653 m
= "(%d seconds -- %d:%02d)" % (secs
, secs
// 60, secs
% 60)
655 m
= "(%d seconds -- %d:%02d:%02d)" % (secs
, secs
// (60*60), (secs
// 60) % 60, secs
% 60)
657 print("%s %s %c" % ("\b \b" * num_bs
, m
, s
[i
]), end
="", file=sys
.stderr
)
661 print("\b" + s
[i
], end
="", file=sys
.stderr
)
667 print("\b \b" * num_bs
, end
="", file=sys
.stderr
)
672 while self
.p_poll(60):
679 elif count
in [5, 10, 15, 30]:
680 msg
= "%d minutes" % count
685 elif count
% 60 == 0:
686 msg
= "%d hours" % (count
// 60)
689 print("%s waiting for solver (%s)" % (self
.timestamp(), msg
), flush
=True)
694 print("(set-info :status %s)" % result
, file=self
.debug_file
)
695 print("(check-sat)", file=self
.debug_file
)
696 self
.debug_file
.flush()
698 if result
not in ["sat", "unsat"]:
700 print("%s Unexpected EOF response from solver." % (self
.timestamp()), flush
=True)
702 print("%s Unexpected response from solver: %s" % (self
.timestamp(), result
), flush
=True)
703 if self
.solver
!= "dummy":
709 def parse(self
, stmt
):
714 while stmt
[cursor
] != ')':
715 el
, le
= worker(stmt
[cursor
:])
718 return expr
, cursor
+1
723 while stmt
[cursor
] != '|':
727 return expr
, cursor
+1
729 if stmt
[0] in [" ", "\t", "\r", "\n"]:
730 el
, le
= worker(stmt
[1:])
735 while stmt
[cursor
] not in ["(", ")", "|", " ", "\t", "\r", "\n"]:
739 return worker(stmt
)[0]
741 def unparse(self
, stmt
):
742 if isinstance(stmt
, list):
743 return "(" + " ".join([self
.unparse(s
) for s
in stmt
]) + ")"
751 if len(v
) > 0 and v
[-1] == "1": d
+= 1
752 if len(v
) > 1 and v
[-2] == "1": d
+= 2
753 if len(v
) > 2 and v
[-3] == "1": d
+= 4
754 if len(v
) > 3 and v
[-4] == "1": d
+= 8
761 if type(v
) is list and len(v
) == 3 and v
[0] == "_" and v
[1].startswith("bv"):
762 x
, n
= int(v
[1][2:]), int(v
[2])
763 return "".join("1" if (x
& (1 << i
)) else "0" for i
in range(n
-1, -1, -1))
764 if v
== "true": return "1"
765 if v
== "false": return "0"
766 if v
.startswith("#b"):
768 if v
.startswith("#x"):
769 return "".join(hex_dict
.get(x
) for x
in v
[2:])
773 return int(self
.bv2bin(v
), 2)
776 self
.write("(get-value (%s))" % (expr
))
777 return self
.parse(self
.read())[0][1]
779 def get_list(self
, expr_list
):
780 if len(expr_list
) == 0:
782 self
.write("(get-value (%s))" % " ".join(expr_list
))
783 return [n
[1] for n
in self
.parse(self
.read())]
785 def get_path(self
, mod
, path
):
786 assert mod
in self
.modinfo
787 path
= path
.split(".")
789 for i
in range(len(path
)-1):
790 first
= ".".join(path
[0:i
+1])
791 second
= ".".join(path
[i
+1:])
793 if first
in self
.modinfo
[mod
].cells
:
794 nextmod
= self
.modinfo
[mod
].cells
[first
]
795 return [first
] + self
.get_path(nextmod
, second
)
797 return [".".join(path
)]
799 def net_expr(self
, mod
, base
, path
):
804 assert mod
in self
.modinfo
807 if path
[0] in self
.modinfo
[mod
].cells
:
808 return "(|%s_h %s| %s)" % (mod
, path
[0], base
)
809 if path
[0] in self
.modinfo
[mod
].wsize
:
810 return "(|%s_n %s| %s)" % (mod
, path
[0], base
)
811 if path
[0] in self
.modinfo
[mod
].memories
:
812 return "(|%s_m %s| %s)" % (mod
, path
[0], base
)
815 assert mod
in self
.modinfo
816 assert path
[0] in self
.modinfo
[mod
].cells
818 nextmod
= self
.modinfo
[mod
].cells
[path
[0]]
819 nextbase
= "(|%s_h %s| %s)" % (mod
, path
[0], base
)
820 return self
.net_expr(nextmod
, nextbase
, path
[1:])
822 def net_width(self
, mod
, net_path
):
823 for i
in range(len(net_path
)-1):
824 assert mod
in self
.modinfo
825 assert net_path
[i
] in self
.modinfo
[mod
].cells
826 mod
= self
.modinfo
[mod
].cells
[net_path
[i
]]
828 assert mod
in self
.modinfo
829 assert net_path
[-1] in self
.modinfo
[mod
].wsize
830 return self
.modinfo
[mod
].wsize
[net_path
[-1]]
832 def net_clock(self
, mod
, net_path
):
833 for i
in range(len(net_path
)-1):
834 assert mod
in self
.modinfo
835 assert net_path
[i
] in self
.modinfo
[mod
].cells
836 mod
= self
.modinfo
[mod
].cells
[net_path
[i
]]
838 assert mod
in self
.modinfo
839 if net_path
[-1] not in self
.modinfo
[mod
].clocks
:
841 return self
.modinfo
[mod
].clocks
[net_path
[-1]]
843 def net_exists(self
, mod
, net_path
):
844 for i
in range(len(net_path
)-1):
845 if mod
not in self
.modinfo
: return False
846 if net_path
[i
] not in self
.modinfo
[mod
].cells
: return False
847 mod
= self
.modinfo
[mod
].cells
[net_path
[i
]]
849 if mod
not in self
.modinfo
: return False
850 if net_path
[-1] not in self
.modinfo
[mod
].wsize
: return False
853 def mem_exists(self
, mod
, mem_path
):
854 for i
in range(len(mem_path
)-1):
855 if mod
not in self
.modinfo
: return False
856 if mem_path
[i
] not in self
.modinfo
[mod
].cells
: return False
857 mod
= self
.modinfo
[mod
].cells
[mem_path
[i
]]
859 if mod
not in self
.modinfo
: return False
860 if mem_path
[-1] not in self
.modinfo
[mod
].memories
: return False
863 def mem_expr(self
, mod
, base
, path
, port
=None, infomode
=False):
865 assert mod
in self
.modinfo
866 assert path
[0] in self
.modinfo
[mod
].memories
868 return self
.modinfo
[mod
].memories
[path
[0]]
869 return "(|%s_m%s %s| %s)" % (mod
, "" if port
is None else ":%s" % port
, path
[0], base
)
871 assert mod
in self
.modinfo
872 assert path
[0] in self
.modinfo
[mod
].cells
874 nextmod
= self
.modinfo
[mod
].cells
[path
[0]]
875 nextbase
= "(|%s_h %s| %s)" % (mod
, path
[0], base
)
876 return self
.mem_expr(nextmod
, nextbase
, path
[1:], port
=port
, infomode
=infomode
)
878 def mem_info(self
, mod
, path
):
879 return self
.mem_expr(mod
, "", path
, infomode
=True)
881 def get_net(self
, mod_name
, net_path
, state_name
):
882 return self
.get(self
.net_expr(mod_name
, state_name
, net_path
))
884 def get_net_list(self
, mod_name
, net_path_list
, state_name
):
885 return self
.get_list([self
.net_expr(mod_name
, state_name
, n
) for n
in net_path_list
])
887 def get_net_hex(self
, mod_name
, net_path
, state_name
):
888 return self
.bv2hex(self
.get_net(mod_name
, net_path
, state_name
))
890 def get_net_hex_list(self
, mod_name
, net_path_list
, state_name
):
891 return [self
.bv2hex(v
) for v
in self
.get_net_list(mod_name
, net_path_list
, state_name
)]
893 def get_net_bin(self
, mod_name
, net_path
, state_name
):
894 return self
.bv2bin(self
.get_net(mod_name
, net_path
, state_name
))
896 def get_net_bin_list(self
, mod_name
, net_path_list
, state_name
):
897 return [self
.bv2bin(v
) for v
in self
.get_net_list(mod_name
, net_path_list
, state_name
)]
900 if self
.p
is not None:
907 self
.shortopts
= "s:S:v"
908 self
.longopts
= ["unroll", "noincr", "noprogress", "dump-smt2=", "logic=", "dummy=", "info=", "nocomments"]
909 self
.solver
= "yices"
910 self
.solver_opts
= list()
911 self
.debug_print
= False
912 self
.debug_file
= None
913 self
.dummy_file
= None
916 self
.timeinfo
= os
.name
!= "nt"
918 self
.info_stmts
= list()
919 self
.nocomments
= False
921 def handle(self
, o
, a
):
925 self
.solver_opts
.append(a
)
927 self
.debug_print
= True
928 elif o
== "--unroll":
930 elif o
== "--noincr":
932 elif o
== "--noprogress":
933 self
.timeinfo
= False
934 elif o
== "--dump-smt2":
935 self
.debug_file
= open(a
, "w")
941 self
.info_stmts
.append(a
)
942 elif o
== "--nocomments":
943 self
.nocomments
= True
951 set SMT solver: z3, yices, boolector, cvc4, mathsat, dummy
955 pass <opt> as command line argument to the solver
958 use the specified SMT2 logic (e.g. QF_AUFBV)
961 if solver is "dummy", read solver output from that file
962 otherwise: write solver output to that file
968 unroll uninterpreted functions
971 don't use incremental solving, instead restart solver for
972 each (check-sat). This also avoids (push) and (pop).
975 disable timer display during solving
976 (this option is set implicitly on Windows)
978 --dump-smt2 <filename>
979 write smt2 statements to file
981 --info <smt2-info-stmt>
982 include the specified smt2 info statement in the smt2 output
985 strip all comments from the generated smt2 code
990 def __init__(self
, f
):
996 def add_net(self
, path
, width
):
999 key
= "n%d" % len(self
.nets
)
1000 self
.nets
[path
] = (key
, width
)
1002 def add_clock(self
, path
, edge
):
1005 key
= "n%d" % len(self
.nets
)
1006 self
.nets
[path
] = (key
, 1)
1007 self
.clocks
[path
] = (key
, edge
)
1009 def set_net(self
, path
, bits
):
1012 assert path
in self
.nets
1013 if path
not in self
.clocks
:
1014 print("b%s %s" % (bits
, self
.nets
[path
][0]), file=self
.f
)
1016 def escape_name(self
, name
):
1017 name
= re
.sub(r
"\[([0-9a-zA-Z_]*[a-zA-Z_][0-9a-zA-Z_]*)\]", r
"<\1>", name
)
1018 if re
.match("[\[\]]", name
) and name
[0] != "\\":
1022 def set_time(self
, t
):
1026 print("$var integer 32 t smt_step $end", file=self
.f
)
1027 print("$var event 1 ! smt_clock $end", file=self
.f
)
1030 for path
in sorted(self
.nets
):
1031 key
, width
= self
.nets
[path
]
1034 if "." in uipath
[-1]:
1035 uipath
= uipath
[0:-1] + uipath
[-1].split(".")
1036 for i
in range(len(uipath
)):
1037 uipath
[i
] = re
.sub(r
"\[([^\]]*)\]", r
"<\1>", uipath
[i
])
1039 while uipath
[:len(scope
)] != scope
:
1040 print("$upscope $end", file=self
.f
)
1043 while uipath
[:-1] != scope
:
1044 print("$scope module %s $end" % uipath
[len(scope
)], file=self
.f
)
1045 scope
.append(uipath
[len(scope
)])
1047 if path
in self
.clocks
and self
.clocks
[path
][1] == "event":
1048 print("$var event 1 %s %s $end" % (key
, uipath
[-1]), file=self
.f
)
1050 print("$var wire %d %s %s $end" % (width
, key
, uipath
[-1]), file=self
.f
)
1052 for i
in range(len(scope
)):
1053 print("$upscope $end", file=self
.f
)
1055 print("$enddefinitions $end", file=self
.f
)
1061 print("#%d" % (10 * self
.t
- 5), file=self
.f
)
1062 for path
in sorted(self
.clocks
.keys()):
1063 if self
.clocks
[path
][1] == "posedge":
1064 print("b0 %s" % self
.nets
[path
][0], file=self
.f
)
1065 elif self
.clocks
[path
][1] == "negedge":
1066 print("b1 %s" % self
.nets
[path
][0], file=self
.f
)
1068 print("#%d" % (10 * self
.t
), file=self
.f
)
1069 print("1!", file=self
.f
)
1070 print("b%s t" % format(self
.t
, "032b"), file=self
.f
)
1072 for path
in sorted(self
.clocks
.keys()):
1073 if self
.clocks
[path
][1] == "negedge":
1074 print("b0 %s" % self
.nets
[path
][0], file=self
.f
)
1076 print("b1 %s" % self
.nets
[path
][0], file=self
.f
)