2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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.
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/log.h"
25 #include "kernel/mem.h"
29 PRIVATE_NAMESPACE_BEGIN
35 RTLIL::Module
*module
;
36 bool bvmode
, memmode
, wiresmode
, verbose
, statebv
, statedt
, forallmode
;
37 dict
<IdString
, int> &mod_stbv_width
;
38 int idcounter
= 0, statebv_width
= 0;
40 std::vector
<std::string
> decls
, trans
, hier
, dtmembers
;
41 std::map
<RTLIL::SigBit
, RTLIL::Cell
*> bit_driver
;
42 std::set
<RTLIL::Cell
*> exported_cells
, hiercells
, hiercells_queue
;
43 pool
<Cell
*> recursive_cells
, registers
;
44 std::vector
<Mem
> memories
;
45 dict
<Cell
*, Mem
*> mem_cells
;
46 std::set
<Mem
*> memory_queue
;
48 pool
<SigBit
> clock_posedge
, clock_negedge
;
49 vector
<string
> ex_state_eq
, ex_input_eq
;
51 std::map
<RTLIL::SigBit
, std::pair
<int, int>> fcache
;
52 std::map
<Mem
*, int> memarrays
;
53 std::map
<int, int> bvsizes
;
54 dict
<IdString
, char*> ids
;
56 const char *get_id(IdString n
)
58 if (ids
.count(n
) == 0) {
59 std::string str
= log_id(n
);
60 for (int i
= 0; i
< GetSize(str
); i
++) {
64 ids
[n
] = strdup(str
.c_str());
70 const char *get_id(T
*obj
) {
71 return get_id(obj
->name
);
74 void makebits(std::string name
, int width
= 0, std::string comment
= std::string())
81 decl_str
= stringf("(define-fun |%s| ((state |%s_s|)) Bool (= ((_ extract %d %d) state) #b1))", name
.c_str(), get_id(module
), statebv_width
, statebv_width
);
84 decl_str
= stringf("(define-fun |%s| ((state |%s_s|)) (_ BitVec %d) ((_ extract %d %d) state))", name
.c_str(), get_id(module
), width
, statebv_width
+width
-1, statebv_width
);
85 statebv_width
+= width
;
91 decl_str
= stringf(" (|%s| Bool)", name
.c_str());
93 decl_str
= stringf(" (|%s| (_ BitVec %d))", name
.c_str(), width
);
99 decl_str
= stringf("(declare-fun |%s| (|%s_s|) Bool)", name
.c_str(), get_id(module
));
101 decl_str
= stringf("(declare-fun |%s| (|%s_s|) (_ BitVec %d))", name
.c_str(), get_id(module
), width
);
105 if (!comment
.empty())
106 decl_str
+= " ; " + comment
;
109 dtmembers
.push_back(decl_str
+ "\n");
111 decls
.push_back(decl_str
+ "\n");
114 Smt2Worker(RTLIL::Module
*module
, bool bvmode
, bool memmode
, bool wiresmode
, bool verbose
, bool statebv
, bool statedt
, bool forallmode
,
115 dict
<IdString
, int> &mod_stbv_width
, dict
<IdString
, dict
<IdString
, pair
<bool, bool>>> &mod_clk_cache
) :
116 ct(module
->design
), sigmap(module
), module(module
), bvmode(bvmode
), memmode(memmode
), wiresmode(wiresmode
),
117 verbose(verbose
), statebv(statebv
), statedt(statedt
), forallmode(forallmode
), mod_stbv_width(mod_stbv_width
)
119 pool
<SigBit
> noclock
;
121 makebits(stringf("%s_is", get_id(module
)));
123 dict
<IdString
, Mem
*> mem_dict
;
124 memories
= Mem::get_all_memories(module
);
125 for (auto &mem
: memories
)
128 mem_dict
[mem
.memid
] = &mem
;
129 for (auto &port
: mem
.wr_ports
)
131 if (port
.clk_enable
) {
132 SigSpec clk
= sigmap(port
.clk
);
133 for (int i
= 0; i
< GetSize(clk
); i
++)
135 if (clk
[i
].wire
== nullptr)
137 if (port
.clk_polarity
)
138 clock_posedge
.insert(clk
[i
]);
140 clock_negedge
.insert(clk
[i
]);
143 for (auto bit
: sigmap(port
.en
))
145 for (auto bit
: sigmap(port
.addr
))
147 for (auto bit
: sigmap(port
.data
))
150 for (auto &port
: mem
.rd_ports
)
152 if (port
.clk_enable
) {
153 SigSpec clk
= sigmap(port
.clk
);
154 for (int i
= 0; i
< GetSize(clk
); i
++)
156 if (clk
[i
].wire
== nullptr)
158 if (port
.clk_polarity
)
159 clock_posedge
.insert(clk
[i
]);
161 clock_negedge
.insert(clk
[i
]);
164 for (auto bit
: sigmap(port
.en
))
166 for (auto bit
: sigmap(port
.addr
))
168 for (auto bit
: sigmap(port
.data
))
170 Cell
*driver
= port
.cell
? port
.cell
: mem
.cell
;
171 for (auto bit
: sigmap(port
.data
)) {
172 if (bit_driver
.count(bit
))
173 log_error("Found multiple drivers for %s.\n", log_signal(bit
));
174 bit_driver
[bit
] = driver
;
179 for (auto cell
: module
->cells())
180 for (auto &conn
: cell
->connections())
182 if (GetSize(conn
.second
) == 0)
186 if (cell
->is_mem_cell()) {
187 mem_cells
[cell
] = mem_dict
[cell
->parameters
.at(ID::MEMID
).decode_string()];
191 bool is_input
= ct
.cell_input(cell
->type
, conn
.first
);
192 bool is_output
= ct
.cell_output(cell
->type
, conn
.first
);
194 if (is_output
&& !is_input
)
195 for (auto bit
: sigmap(conn
.second
)) {
196 if (bit_driver
.count(bit
))
197 log_error("Found multiple drivers for %s.\n", log_signal(bit
));
198 bit_driver
[bit
] = cell
;
200 else if (is_output
|| !is_input
)
201 log_error("Unsupported or unknown directionality on port %s of cell %s.%s (%s).\n",
202 log_id(conn
.first
), log_id(module
), log_id(cell
), log_id(cell
->type
));
204 if (cell
->type
.in(ID($dff
), ID($_DFF_P_
), ID($_DFF_N_
)) && conn
.first
.in(ID::CLK
, ID::C
))
206 bool posedge
= (cell
->type
== ID($_DFF_N_
)) || (cell
->type
== ID($dff
) && cell
->getParam(ID::CLK_POLARITY
).as_bool());
207 for (auto bit
: sigmap(conn
.second
)) {
209 clock_posedge
.insert(bit
);
211 clock_negedge
.insert(bit
);
215 if (mod_clk_cache
.count(cell
->type
) && mod_clk_cache
.at(cell
->type
).count(conn
.first
))
217 for (auto bit
: sigmap(conn
.second
)) {
218 if (mod_clk_cache
.at(cell
->type
).at(conn
.first
).first
)
219 clock_posedge
.insert(bit
);
220 if (mod_clk_cache
.at(cell
->type
).at(conn
.first
).second
)
221 clock_negedge
.insert(bit
);
226 for (auto bit
: sigmap(conn
.second
))
231 for (auto bit
: noclock
) {
232 clock_posedge
.erase(bit
);
233 clock_negedge
.erase(bit
);
236 for (auto wire
: module
->wires())
238 if (!wire
->port_input
|| GetSize(wire
) != 1)
240 SigBit bit
= sigmap(wire
);
241 if (clock_posedge
.count(bit
))
242 mod_clk_cache
[module
->name
][wire
->name
].first
= true;
243 if (clock_negedge
.count(bit
))
244 mod_clk_cache
[module
->name
][wire
->name
].second
= true;
255 const char *get_id(Module
*m
)
257 return get_id(m
->name
);
260 const char *get_id(Cell
*c
)
262 return get_id(c
->name
);
265 const char *get_id(Wire
*w
)
267 return get_id(w
->name
);
270 void register_bool(RTLIL::SigBit bit
, int id
)
272 if (verbose
) log("%*s-> register_bool: %s %d\n", 2+2*GetSize(recursive_cells
), "",
273 log_signal(bit
), id
);
276 log_assert(fcache
.count(bit
) == 0);
277 fcache
[bit
] = std::pair
<int, int>(id
, -1);
280 void register_bv(RTLIL::SigSpec sig
, int id
)
282 if (verbose
) log("%*s-> register_bv: %s %d\n", 2+2*GetSize(recursive_cells
), "",
283 log_signal(sig
), id
);
288 log_assert(bvsizes
.count(id
) == 0);
289 bvsizes
[id
] = GetSize(sig
);
291 for (int i
= 0; i
< GetSize(sig
); i
++) {
292 log_assert(fcache
.count(sig
[i
]) == 0);
293 fcache
[sig
[i
]] = std::pair
<int, int>(id
, i
);
297 void register_boolvec(RTLIL::SigSpec sig
, int id
)
299 if (verbose
) log("%*s-> register_boolvec: %s %d\n", 2+2*GetSize(recursive_cells
), "",
300 log_signal(sig
), id
);
304 register_bool(sig
[0], id
);
306 for (int i
= 1; i
< GetSize(sig
); i
++)
307 sigmap
.add(sig
[i
], RTLIL::State::S0
);
310 std::string
get_bool(RTLIL::SigBit bit
, const char *state_name
= "state")
314 if (bit
.wire
== nullptr)
315 return bit
== RTLIL::State::S1
? "true" : "false";
317 if (bit_driver
.count(bit
))
318 export_cell(bit_driver
.at(bit
));
321 if (fcache
.count(bit
) == 0) {
322 if (verbose
) log("%*s-> external bool: %s\n", 2+2*GetSize(recursive_cells
), "",
324 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(bit
));
325 register_bool(bit
, idcounter
++);
328 auto f
= fcache
.at(bit
);
330 return stringf("(= ((_ extract %d %d) (|%s#%d| %s)) #b1)", f
.second
, f
.second
, get_id(module
), f
.first
, state_name
);
331 return stringf("(|%s#%d| %s)", get_id(module
), f
.first
, state_name
);
334 std::string
get_bool(RTLIL::SigSpec sig
, const char *state_name
= "state")
336 return get_bool(sig
.as_bit(), state_name
);
339 std::string
get_bv(RTLIL::SigSpec sig
, const char *state_name
= "state")
344 std::vector
<std::string
> subexpr
;
347 while (orig_sig
!= sig
) {
349 if (bit_driver
.count(bit
))
350 export_cell(bit_driver
.at(bit
));
355 for (int i
= 0, j
= 1; i
< GetSize(sig
); i
+= j
, j
= 1)
357 if (sig
[i
].wire
== nullptr) {
358 while (i
+j
< GetSize(sig
) && sig
[i
+j
].wire
== nullptr) j
++;
359 subexpr
.push_back("#b");
360 for (int k
= i
+j
-1; k
>= i
; k
--)
361 subexpr
.back() += sig
[k
] == RTLIL::State::S1
? "1" : "0";
365 if (fcache
.count(sig
[i
]) && fcache
.at(sig
[i
]).second
== -1) {
366 subexpr
.push_back(stringf("(ite %s #b1 #b0)", get_bool(sig
[i
], state_name
).c_str()));
370 if (fcache
.count(sig
[i
])) {
371 auto t1
= fcache
.at(sig
[i
]);
372 while (i
+j
< GetSize(sig
)) {
373 if (fcache
.count(sig
[i
+j
]) == 0)
375 auto t2
= fcache
.at(sig
[i
+j
]);
376 if (t1
.first
!= t2
.first
)
378 if (t1
.second
+j
!= t2
.second
)
382 if (t1
.second
== 0 && j
== bvsizes
.at(t1
.first
))
383 subexpr
.push_back(stringf("(|%s#%d| %s)", get_id(module
), t1
.first
, state_name
));
385 subexpr
.push_back(stringf("((_ extract %d %d) (|%s#%d| %s))",
386 t1
.second
+ j
- 1, t1
.second
, get_id(module
), t1
.first
, state_name
));
390 std::set
<RTLIL::SigBit
> seen_bits
= { sig
[i
] };
391 while (i
+j
< GetSize(sig
) && sig
[i
+j
].wire
&& !fcache
.count(sig
[i
+j
]) && !seen_bits
.count(sig
[i
+j
]))
392 seen_bits
.insert(sig
[i
+j
]), j
++;
394 if (verbose
) log("%*s-> external bv: %s\n", 2+2*GetSize(recursive_cells
), "",
395 log_signal(sig
.extract(i
, j
)));
396 for (auto bit
: sig
.extract(i
, j
))
397 log_assert(bit_driver
.count(bit
) == 0);
398 makebits(stringf("%s#%d", get_id(module
), idcounter
), j
, log_signal(sig
.extract(i
, j
)));
399 subexpr
.push_back(stringf("(|%s#%d| %s)", get_id(module
), idcounter
, state_name
));
400 register_bv(sig
.extract(i
, j
), idcounter
++);
403 if (GetSize(subexpr
) > 1) {
404 std::string expr
= "", end_str
= "";
405 for (int i
= GetSize(subexpr
)-1; i
>= 0; i
--) {
406 if (i
> 0) expr
+= " (concat", end_str
+= ")";
407 expr
+= " " + subexpr
[i
];
409 return expr
.substr(1) + end_str
;
411 log_assert(GetSize(subexpr
) == 1);
416 void export_gate(RTLIL::Cell
*cell
, std::string expr
)
418 RTLIL::SigBit bit
= sigmap(cell
->getPort(ID::Y
).as_bit());
419 std::string processed_expr
;
421 for (char ch
: expr
) {
422 if (ch
== 'A') processed_expr
+= get_bool(cell
->getPort(ID::A
));
423 else if (ch
== 'B') processed_expr
+= get_bool(cell
->getPort(ID::B
));
424 else if (ch
== 'C') processed_expr
+= get_bool(cell
->getPort(ID::C
));
425 else if (ch
== 'D') processed_expr
+= get_bool(cell
->getPort(ID::D
));
426 else if (ch
== 'S') processed_expr
+= get_bool(cell
->getPort(ID::S
));
427 else processed_expr
+= ch
;
431 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
433 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
434 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(bit
)));
435 register_bool(bit
, idcounter
++);
436 recursive_cells
.erase(cell
);
439 void export_smtlib2_expr(RTLIL::Cell
*cell
)
441 RTLIL::SigSpec sig_a
= cell
->getPort(ID::A
);
442 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort(ID::Y
));
443 string expr
= cell
->getParam(ID::EXPR
).decode_string();
445 string a_bv
= get_bv(sig_a
);
448 log("%*s-> import cell: %s\n", 2 + 2 * GetSize(recursive_cells
), "", log_id(cell
));
450 int expr_idcounter
= idcounter
++;
452 decls
.push_back(stringf("(define-fun |%s#%d| ((A (_ BitVec %d))) (_ BitVec %d) ; %s\n %s\n)\n", get_id(module
), expr_idcounter
,
453 GetSize(sig_a
), GetSize(sig_y
), log_signal(sig_y
), expr
.c_str()));
454 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) (|%s#%d| %s)) ; %s\n", get_id(module
), idcounter
,
455 get_id(module
), GetSize(sig_y
), get_id(module
), expr_idcounter
, a_bv
.c_str(), log_signal(sig_y
)));
456 register_bv(sig_y
, idcounter
++);
458 recursive_cells
.erase(cell
);
461 void export_bvop(RTLIL::Cell
*cell
, std::string expr
, char type
= 0)
463 RTLIL::SigSpec sig_a
, sig_b
;
464 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort(ID::Y
));
465 bool is_signed
= cell
->getParam(ID::A_SIGNED
).as_bool();
466 int width
= GetSize(sig_y
);
468 if (type
== 's' || type
== 'd' || type
== 'b') {
469 width
= max(width
, GetSize(cell
->getPort(ID::A
)));
470 if (cell
->hasPort(ID::B
))
471 width
= max(width
, GetSize(cell
->getPort(ID::B
)));
474 if (cell
->hasPort(ID::A
)) {
475 sig_a
= cell
->getPort(ID::A
);
476 sig_a
.extend_u0(width
, is_signed
);
479 if (cell
->hasPort(ID::B
)) {
480 sig_b
= cell
->getPort(ID::B
);
481 sig_b
.extend_u0(width
, is_signed
&& !(type
== 's'));
484 std::string processed_expr
;
486 for (char ch
: expr
) {
487 if (ch
== 'A') processed_expr
+= get_bv(sig_a
);
488 else if (ch
== 'B') processed_expr
+= get_bv(sig_b
);
489 else if (ch
== 'P') processed_expr
+= get_bv(cell
->getPort(ID::B
));
490 else if (ch
== 'L') processed_expr
+= is_signed
? "a" : "l";
491 else if (ch
== 'U') processed_expr
+= is_signed
? "s" : "u";
492 else processed_expr
+= ch
;
495 if (width
!= GetSize(sig_y
) && type
!= 'b')
496 processed_expr
= stringf("((_ extract %d 0) %s)", GetSize(sig_y
)-1, processed_expr
.c_str());
499 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
502 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
503 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(sig_y
)));
504 register_boolvec(sig_y
, idcounter
++);
506 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
507 get_id(module
), idcounter
, get_id(module
), GetSize(sig_y
), processed_expr
.c_str(), log_signal(sig_y
)));
508 register_bv(sig_y
, idcounter
++);
511 recursive_cells
.erase(cell
);
514 void export_reduce(RTLIL::Cell
*cell
, std::string expr
, bool identity_val
)
516 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort(ID::Y
));
517 std::string processed_expr
;
520 if (ch
== 'A' || ch
== 'B') {
521 RTLIL::SigSpec sig
= sigmap(cell
->getPort(stringf("\\%c", ch
)));
523 processed_expr
+= " " + get_bool(bit
);
524 if (GetSize(sig
) == 1)
525 processed_expr
+= identity_val
? " true" : " false";
527 processed_expr
+= ch
;
530 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
532 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
533 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(sig_y
)));
534 register_boolvec(sig_y
, idcounter
++);
535 recursive_cells
.erase(cell
);
538 void export_cell(RTLIL::Cell
*cell
)
541 log("%*s=> export_cell %s (%s) [%s]\n", 2+2*GetSize(recursive_cells
), "",
542 log_id(cell
), log_id(cell
->type
), exported_cells
.count(cell
) ? "old" : "new");
544 if (recursive_cells
.count(cell
))
545 log_error("Found logic loop in module %s! See cell %s.\n", get_id(module
), get_id(cell
));
547 if (exported_cells
.count(cell
))
550 exported_cells
.insert(cell
);
551 recursive_cells
.insert(cell
);
553 if (cell
->type
== ID($initstate
))
555 SigBit bit
= sigmap(cell
->getPort(ID::Y
).as_bit());
556 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool (|%s_is| state)) ; %s\n",
557 get_id(module
), idcounter
, get_id(module
), get_id(module
), log_signal(bit
)));
558 register_bool(bit
, idcounter
++);
559 recursive_cells
.erase(cell
);
563 if (cell
->type
.in(ID($_FF_
), ID($_DFF_P_
), ID($_DFF_N_
)))
565 registers
.insert(cell
);
566 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(cell
->getPort(ID::Q
)));
567 register_bool(cell
->getPort(ID::Q
), idcounter
++);
568 recursive_cells
.erase(cell
);
572 if (cell
->type
== ID($_BUF_
)) return export_gate(cell
, "A");
573 if (cell
->type
== ID($_NOT_
)) return export_gate(cell
, "(not A)");
574 if (cell
->type
== ID($_AND_
)) return export_gate(cell
, "(and A B)");
575 if (cell
->type
== ID($_NAND_
)) return export_gate(cell
, "(not (and A B))");
576 if (cell
->type
== ID($_OR_
)) return export_gate(cell
, "(or A B)");
577 if (cell
->type
== ID($_NOR_
)) return export_gate(cell
, "(not (or A B))");
578 if (cell
->type
== ID($_XOR_
)) return export_gate(cell
, "(xor A B)");
579 if (cell
->type
== ID($_XNOR_
)) return export_gate(cell
, "(not (xor A B))");
580 if (cell
->type
== ID($_ANDNOT_
)) return export_gate(cell
, "(and A (not B))");
581 if (cell
->type
== ID($_ORNOT_
)) return export_gate(cell
, "(or A (not B))");
582 if (cell
->type
== ID($_MUX_
)) return export_gate(cell
, "(ite S B A)");
583 if (cell
->type
== ID($_NMUX_
)) return export_gate(cell
, "(not (ite S B A))");
584 if (cell
->type
== ID($_AOI3_
)) return export_gate(cell
, "(not (or (and A B) C))");
585 if (cell
->type
== ID($_OAI3_
)) return export_gate(cell
, "(not (and (or A B) C))");
586 if (cell
->type
== ID($_AOI4_
)) return export_gate(cell
, "(not (or (and A B) (and C D)))");
587 if (cell
->type
== ID($_OAI4_
)) return export_gate(cell
, "(not (and (or A B) (or C D)))");
593 if (cell
->type
.in(ID($ff
), ID($dff
)))
595 registers
.insert(cell
);
596 makebits(stringf("%s#%d", get_id(module
), idcounter
), GetSize(cell
->getPort(ID::Q
)), log_signal(cell
->getPort(ID::Q
)));
597 register_bv(cell
->getPort(ID::Q
), idcounter
++);
598 recursive_cells
.erase(cell
);
602 if (cell
->type
.in(ID($anyconst
), ID($anyseq
), ID($allconst
), ID($allseq
)))
604 registers
.insert(cell
);
605 string infostr
= cell
->attributes
.count(ID::src
) ? cell
->attributes
.at(ID::src
).decode_string().c_str() : get_id(cell
);
606 if (cell
->attributes
.count(ID::reg
))
607 infostr
+= " " + cell
->attributes
.at(ID::reg
).decode_string();
608 decls
.push_back(stringf("; yosys-smt2-%s %s#%d %d %s\n", cell
->type
.c_str() + 1, get_id(module
), idcounter
, GetSize(cell
->getPort(ID::Y
)), infostr
.c_str()));
609 if (cell
->getPort(ID::Y
).is_wire() && cell
->getPort(ID::Y
).as_wire()->get_bool_attribute(ID::maximize
)){
610 decls
.push_back(stringf("; yosys-smt2-maximize %s#%d\n", get_id(module
), idcounter
));
611 log("Wire %s is maximized\n", cell
->getPort(ID::Y
).as_wire()->name
.str().c_str());
613 else if (cell
->getPort(ID::Y
).is_wire() && cell
->getPort(ID::Y
).as_wire()->get_bool_attribute(ID::minimize
)){
614 decls
.push_back(stringf("; yosys-smt2-minimize %s#%d\n", get_id(module
), idcounter
));
615 log("Wire %s is minimized\n", cell
->getPort(ID::Y
).as_wire()->name
.str().c_str());
617 makebits(stringf("%s#%d", get_id(module
), idcounter
), GetSize(cell
->getPort(ID::Y
)), log_signal(cell
->getPort(ID::Y
)));
618 if (cell
->type
== ID($anyseq
))
619 ex_input_eq
.push_back(stringf(" (= (|%s#%d| state) (|%s#%d| other_state))", get_id(module
), idcounter
, get_id(module
), idcounter
));
620 register_bv(cell
->getPort(ID::Y
), idcounter
++);
621 recursive_cells
.erase(cell
);
625 if (cell
->type
== ID($
and)) return export_bvop(cell
, "(bvand A B)");
626 if (cell
->type
== ID($
or)) return export_bvop(cell
, "(bvor A B)");
627 if (cell
->type
== ID($
xor)) return export_bvop(cell
, "(bvxor A B)");
628 if (cell
->type
== ID($xnor
)) return export_bvop(cell
, "(bvxnor A B)");
630 if (cell
->type
== ID($shl
)) return export_bvop(cell
, "(bvshl A B)", 's');
631 if (cell
->type
== ID($shr
)) return export_bvop(cell
, "(bvlshr A B)", 's');
632 if (cell
->type
== ID($sshl
)) return export_bvop(cell
, "(bvshl A B)", 's');
633 if (cell
->type
== ID($sshr
)) return export_bvop(cell
, "(bvLshr A B)", 's');
635 if (cell
->type
.in(ID($shift
), ID($shiftx
))) {
636 if (cell
->getParam(ID::B_SIGNED
).as_bool()) {
637 return export_bvop(cell
, stringf("(ite (bvsge P #b%0*d) "
638 "(bvlshr A B) (bvlshr A (bvneg B)))",
639 GetSize(cell
->getPort(ID::B
)), 0), 's');
641 return export_bvop(cell
, "(bvlshr A B)", 's');
645 if (cell
->type
== ID($lt
)) return export_bvop(cell
, "(bvUlt A B)", 'b');
646 if (cell
->type
== ID($le
)) return export_bvop(cell
, "(bvUle A B)", 'b');
647 if (cell
->type
== ID($ge
)) return export_bvop(cell
, "(bvUge A B)", 'b');
648 if (cell
->type
== ID($gt
)) return export_bvop(cell
, "(bvUgt A B)", 'b');
650 if (cell
->type
== ID($ne
)) return export_bvop(cell
, "(distinct A B)", 'b');
651 if (cell
->type
== ID($nex
)) return export_bvop(cell
, "(distinct A B)", 'b');
652 if (cell
->type
== ID($eq
)) return export_bvop(cell
, "(= A B)", 'b');
653 if (cell
->type
== ID($eqx
)) return export_bvop(cell
, "(= A B)", 'b');
655 if (cell
->type
== ID($
not)) return export_bvop(cell
, "(bvnot A)");
656 if (cell
->type
== ID($pos
)) return export_bvop(cell
, "A");
657 if (cell
->type
== ID($neg
)) return export_bvop(cell
, "(bvneg A)");
659 if (cell
->type
== ID($add
)) return export_bvop(cell
, "(bvadd A B)");
660 if (cell
->type
== ID($sub
)) return export_bvop(cell
, "(bvsub A B)");
661 if (cell
->type
== ID($mul
)) return export_bvop(cell
, "(bvmul A B)");
662 if (cell
->type
== ID($div
)) return export_bvop(cell
, "(bvUdiv A B)", 'd');
663 // "rem" = truncating modulo
664 if (cell
->type
== ID($mod
)) return export_bvop(cell
, "(bvUrem A B)", 'd');
665 // "mod" = flooring modulo
666 if (cell
->type
== ID($modfloor
)) {
667 // bvumod doesn't exist because it's the same as bvurem
668 if (cell
->getParam(ID::A_SIGNED
).as_bool()) {
669 return export_bvop(cell
, "(bvsmod A B)", 'd');
671 return export_bvop(cell
, "(bvurem A B)", 'd');
674 // "div" = flooring division
675 if (cell
->type
== ID($divfloor
)) {
676 if (cell
->getParam(ID::A_SIGNED
).as_bool()) {
677 // bvsdiv is truncating division, so we can't use it here.
678 int width
= max(GetSize(cell
->getPort(ID::A
)), GetSize(cell
->getPort(ID::B
)));
679 width
= max(width
, GetSize(cell
->getPort(ID::Y
)));
680 auto expr
= stringf("(let ("
681 "(a_neg (bvslt A #b%0*d)) "
682 "(b_neg (bvslt B #b%0*d))) "
683 "(let ((abs_a (ite a_neg (bvneg A) A)) "
684 "(abs_b (ite b_neg (bvneg B) B))) "
685 "(let ((u (bvudiv abs_a abs_b)) "
686 "(adj (ite (= #b%0*d (bvurem abs_a abs_b)) #b%0*d #b%0*d))) "
687 "(ite (= a_neg b_neg) u "
688 "(bvneg (bvadd u adj))))))",
689 width
, 0, width
, 0, width
, 0, width
, 0, width
, 1);
690 return export_bvop(cell
, expr
, 'd');
692 return export_bvop(cell
, "(bvudiv A B)", 'd');
696 if (cell
->type
.in(ID($reduce_and
), ID($reduce_or
), ID($reduce_bool
)) &&
697 2*GetSize(cell
->getPort(ID::A
).chunks()) < GetSize(cell
->getPort(ID::A
))) {
698 bool is_and
= cell
->type
== ID($reduce_and
);
699 string
bits(GetSize(cell
->getPort(ID::A
)), is_and
? '1' : '0');
700 return export_bvop(cell
, stringf("(%s A #b%s)", is_and
? "=" : "distinct", bits
.c_str()), 'b');
703 if (cell
->type
== ID($reduce_and
)) return export_reduce(cell
, "(and A)", true);
704 if (cell
->type
== ID($reduce_or
)) return export_reduce(cell
, "(or A)", false);
705 if (cell
->type
== ID($reduce_xor
)) return export_reduce(cell
, "(xor A)", false);
706 if (cell
->type
== ID($reduce_xnor
)) return export_reduce(cell
, "(not (xor A))", false);
707 if (cell
->type
== ID($reduce_bool
)) return export_reduce(cell
, "(or A)", false);
709 if (cell
->type
== ID($logic_not
)) return export_reduce(cell
, "(not (or A))", false);
710 if (cell
->type
== ID($logic_and
)) return export_reduce(cell
, "(and (or A) (or B))", false);
711 if (cell
->type
== ID($logic_or
)) return export_reduce(cell
, "(or A B)", false);
713 if (cell
->type
.in(ID($mux
), ID($pmux
)))
715 int width
= GetSize(cell
->getPort(ID::Y
));
716 std::string processed_expr
= get_bv(cell
->getPort(ID::A
));
718 RTLIL::SigSpec sig_b
= cell
->getPort(ID::B
);
719 RTLIL::SigSpec sig_s
= cell
->getPort(ID::S
);
723 for (int i
= 0; i
< GetSize(sig_s
); i
++)
724 processed_expr
= stringf("(ite %s %s %s)", get_bool(sig_s
[i
]).c_str(),
725 get_bv(sig_b
.extract(i
*width
, width
)).c_str(), processed_expr
.c_str());
728 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
730 RTLIL::SigSpec sig
= sigmap(cell
->getPort(ID::Y
));
731 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
732 get_id(module
), idcounter
, get_id(module
), width
, processed_expr
.c_str(), log_signal(sig
)));
733 register_bv(sig
, idcounter
++);
734 recursive_cells
.erase(cell
);
738 if (cell
->type
== ID($smtlib2_expr
)) {
739 return export_smtlib2_expr(cell
);
742 // FIXME: $slice $concat
745 if (memmode
&& cell
->is_mem_cell())
747 Mem
*mem
= mem_cells
[cell
];
749 if (memarrays
.count(mem
)) {
750 recursive_cells
.erase(cell
);
754 int arrayid
= idcounter
++;
755 memarrays
[mem
] = arrayid
;
757 int abits
= ceil_log2(mem
->size
);
759 bool has_sync_wr
= false;
760 bool has_async_wr
= false;
761 for (auto &port
: mem
->wr_ports
) {
767 if (has_async_wr
&& has_sync_wr
)
768 log_error("Memory %s.%s has mixed clocked/nonclocked write ports. This is not supported by \"write_smt2\".\n", log_id(cell
), log_id(module
));
770 decls
.push_back(stringf("; yosys-smt2-memory %s %d %d %d %d %s\n", get_id(mem
->memid
), abits
, mem
->width
, GetSize(mem
->rd_ports
), GetSize(mem
->wr_ports
), has_async_wr
? "async" : "sync"));
774 memstate
= stringf("%s#%d#final", get_id(module
), arrayid
);
776 memstate
= stringf("%s#%d#0", get_id(module
), arrayid
);
781 makebits(memstate
, mem
->width
*mem
->size
, get_id(mem
->memid
));
782 decls
.push_back(stringf("(define-fun |%s_m %s| ((state |%s_s|)) (_ BitVec %d) (|%s| state))\n",
783 get_id(module
), get_id(mem
->memid
), get_id(module
), mem
->width
*mem
->size
, memstate
.c_str()));
785 for (int i
= 0; i
< GetSize(mem
->rd_ports
); i
++)
787 auto &port
= mem
->rd_ports
[i
];
788 SigSpec addr_sig
= port
.addr
;
789 addr_sig
.extend_u0(abits
);
790 std::string addr
= get_bv(addr_sig
);
793 log_error("Read port %d (%s) of memory %s.%s is clocked. This is not supported by \"write_smt2\"! "
794 "Call \"memory\" with -nordff to avoid this error.\n", i
, log_signal(port
.data
), log_id(mem
->memid
), log_id(module
));
796 decls
.push_back(stringf("(define-fun |%s_m:R%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
797 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
799 std::string read_expr
= "#b";
800 for (int k
= 0; k
< mem
->width
; k
++)
803 for (int k
= 0; k
< mem
->size
; k
++)
804 read_expr
= stringf("(ite (= (|%s_m:R%dA %s| state) #b%s) ((_ extract %d %d) (|%s| state))\n %s)",
805 get_id(module
), i
, get_id(mem
->memid
), Const(k
+mem
->start_offset
, abits
).as_string().c_str(),
806 mem
->width
*(k
+1)-1, mem
->width
*k
, memstate
.c_str(), read_expr
.c_str());
808 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d)\n %s) ; %s\n",
809 get_id(module
), idcounter
, get_id(module
), mem
->width
, read_expr
.c_str(), log_signal(port
.data
)));
811 decls
.push_back(stringf("(define-fun |%s_m:R%dD %s| ((state |%s_s|)) (_ BitVec %d) (|%s#%d| state))\n",
812 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), mem
->width
, get_id(module
), idcounter
));
814 register_bv(port
.data
, idcounter
++);
820 dtmembers
.push_back(stringf(" (|%s| (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
821 memstate
.c_str(), abits
, mem
->width
, get_id(mem
->memid
)));
823 decls
.push_back(stringf("(declare-fun |%s| (|%s_s|) (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
824 memstate
.c_str(), get_id(module
), abits
, mem
->width
, get_id(mem
->memid
)));
826 decls
.push_back(stringf("(define-fun |%s_m %s| ((state |%s_s|)) (Array (_ BitVec %d) (_ BitVec %d)) (|%s| state))\n",
827 get_id(module
), get_id(mem
->memid
), get_id(module
), abits
, mem
->width
, memstate
.c_str()));
829 for (int i
= 0; i
< GetSize(mem
->rd_ports
); i
++)
831 auto &port
= mem
->rd_ports
[i
];
832 SigSpec addr_sig
= port
.addr
;
833 addr_sig
.extend_u0(abits
);
834 std::string addr
= get_bv(addr_sig
);
837 log_error("Read port %d (%s) of memory %s.%s is clocked. This is not supported by \"write_smt2\"! "
838 "Call \"memory\" with -nordff to avoid this error.\n", i
, log_signal(port
.data
), log_id(mem
->memid
), log_id(module
));
840 decls
.push_back(stringf("(define-fun |%s_m:R%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
841 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
843 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) (select (|%s| state) (|%s_m:R%dA %s| state))) ; %s\n",
844 get_id(module
), idcounter
, get_id(module
), mem
->width
, memstate
.c_str(), get_id(module
), i
, get_id(mem
->memid
), log_signal(port
.data
)));
846 decls
.push_back(stringf("(define-fun |%s_m:R%dD %s| ((state |%s_s|)) (_ BitVec %d) (|%s#%d| state))\n",
847 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), mem
->width
, get_id(module
), idcounter
));
849 register_bv(port
.data
, idcounter
++);
853 memory_queue
.insert(mem
);
854 recursive_cells
.erase(cell
);
858 Module
*m
= module
->design
->module(cell
->type
);
862 decls
.push_back(stringf("; yosys-smt2-cell %s %s\n", get_id(cell
->type
), get_id(cell
->name
)));
863 string cell_state
= stringf("(|%s_h %s| state)", get_id(module
), get_id(cell
->name
));
865 for (auto &conn
: cell
->connections())
867 if (GetSize(conn
.second
) == 0)
870 Wire
*w
= m
->wire(conn
.first
);
871 SigSpec sig
= sigmap(conn
.second
);
873 if (w
->port_output
&& !w
->port_input
) {
874 if (GetSize(w
) > 1) {
876 makebits(stringf("%s#%d", get_id(module
), idcounter
), GetSize(w
), log_signal(sig
));
877 register_bv(sig
, idcounter
++);
879 for (int i
= 0; i
< GetSize(w
); i
++) {
880 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(sig
[i
]));
881 register_bool(sig
[i
], idcounter
++);
885 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(sig
));
886 register_bool(sig
, idcounter
++);
892 makebits(stringf("%s_h %s", get_id(module
), get_id(cell
->name
)), mod_stbv_width
.at(cell
->type
));
894 dtmembers
.push_back(stringf(" (|%s_h %s| |%s_s|)\n",
895 get_id(module
), get_id(cell
->name
), get_id(cell
->type
)));
897 decls
.push_back(stringf("(declare-fun |%s_h %s| (|%s_s|) |%s_s|)\n",
898 get_id(module
), get_id(cell
->name
), get_id(module
), get_id(cell
->type
)));
900 hiercells
.insert(cell
);
901 hiercells_queue
.insert(cell
);
902 recursive_cells
.erase(cell
);
906 if (cell
->type
.in(ID($dffe
), ID($sdff
), ID($sdffe
), ID($sdffce
)) || cell
->type
.str().substr(0, 6) == "$_SDFF" || (cell
->type
.str().substr(0, 6) == "$_DFFE" && cell
->type
.str().size() == 10)) {
907 log_error("Unsupported cell type %s for cell %s.%s -- please run `dffunmap` before `write_smt2`.\n",
908 log_id(cell
->type
), log_id(module
), log_id(cell
));
910 if (cell
->type
.in(ID($adff
), ID($adffe
), ID($aldff
), ID($aldffe
), ID($dffsr
), ID($dffsre
)) || cell
->type
.str().substr(0, 5) == "$_DFF" || cell
->type
.str().substr(0, 7) == "$_ALDFF") {
911 log_error("Unsupported cell type %s for cell %s.%s -- please run `async2sync; dffunmap` or `clk2fflogic` before `write_smt2`.\n",
912 log_id(cell
->type
), log_id(module
), log_id(cell
));
914 if (cell
->type
.in(ID($sr
), ID($dlatch
), ID($adlatch
), ID($dlatchsr
)) || cell
->type
.str().substr(0, 8) == "$_DLATCH" || cell
->type
.str().substr(0, 5) == "$_SR_") {
915 log_error("Unsupported cell type %s for cell %s.%s -- please run `clk2fflogic` before `write_smt2`.\n",
916 log_id(cell
->type
), log_id(module
), log_id(cell
));
918 log_error("Unsupported cell type %s for cell %s.%s.\n",
919 log_id(cell
->type
), log_id(module
), log_id(cell
));
924 if (verbose
) log("=> export logic driving outputs\n");
926 pool
<SigBit
> reg_bits
;
927 for (auto cell
: module
->cells())
928 if (cell
->type
.in(ID($ff
), ID($dff
), ID($_FF_
), ID($_DFF_P_
), ID($_DFF_N_
))) {
929 // not using sigmap -- we want the net directly at the dff output
930 for (auto bit
: cell
->getPort(ID::Q
))
931 reg_bits
.insert(bit
);
934 for (auto wire
: module
->wires()) {
935 bool is_register
= false;
936 for (auto bit
: SigSpec(wire
))
937 if (reg_bits
.count(bit
))
939 if (wire
->port_id
|| is_register
|| wire
->get_bool_attribute(ID::keep
) || (wiresmode
&& wire
->name
.isPublic())) {
940 RTLIL::SigSpec sig
= sigmap(wire
);
941 std::vector
<std::string
> comments
;
942 if (wire
->port_input
)
943 comments
.push_back(stringf("; yosys-smt2-input %s %d\n", get_id(wire
), wire
->width
));
944 if (wire
->port_output
)
945 comments
.push_back(stringf("; yosys-smt2-output %s %d\n", get_id(wire
), wire
->width
));
947 comments
.push_back(stringf("; yosys-smt2-register %s %d\n", get_id(wire
), wire
->width
));
948 if (wire
->get_bool_attribute(ID::keep
) || (wiresmode
&& wire
->name
.isPublic()))
949 comments
.push_back(stringf("; yosys-smt2-wire %s %d\n", get_id(wire
), wire
->width
));
950 if (GetSize(wire
) == 1 && (clock_posedge
.count(sig
) || clock_negedge
.count(sig
)))
951 comments
.push_back(stringf("; yosys-smt2-clock %s%s%s\n", get_id(wire
),
952 clock_posedge
.count(sig
) ? " posedge" : "", clock_negedge
.count(sig
) ? " negedge" : ""));
953 if (bvmode
&& GetSize(sig
) > 1) {
954 std::string sig_bv
= get_bv(sig
);
955 if (!comments
.empty())
956 decls
.insert(decls
.end(), comments
.begin(), comments
.end());
957 decls
.push_back(stringf("(define-fun |%s_n %s| ((state |%s_s|)) (_ BitVec %d) %s)\n",
958 get_id(module
), get_id(wire
), get_id(module
), GetSize(sig
), sig_bv
.c_str()));
959 if (wire
->port_input
)
960 ex_input_eq
.push_back(stringf(" (= (|%s_n %s| state) (|%s_n %s| other_state))",
961 get_id(module
), get_id(wire
), get_id(module
), get_id(wire
)));
963 std::vector
<std::string
> sig_bool
;
964 for (int i
= 0; i
< GetSize(sig
); i
++) {
965 sig_bool
.push_back(get_bool(sig
[i
]));
967 if (!comments
.empty())
968 decls
.insert(decls
.end(), comments
.begin(), comments
.end());
969 for (int i
= 0; i
< GetSize(sig
); i
++) {
970 if (GetSize(sig
) > 1) {
971 decls
.push_back(stringf("(define-fun |%s_n %s %d| ((state |%s_s|)) Bool %s)\n",
972 get_id(module
), get_id(wire
), i
, get_id(module
), sig_bool
[i
].c_str()));
973 if (wire
->port_input
)
974 ex_input_eq
.push_back(stringf(" (= (|%s_n %s %d| state) (|%s_n %s %d| other_state))",
975 get_id(module
), get_id(wire
), i
, get_id(module
), get_id(wire
), i
));
977 decls
.push_back(stringf("(define-fun |%s_n %s| ((state |%s_s|)) Bool %s)\n",
978 get_id(module
), get_id(wire
), get_id(module
), sig_bool
[i
].c_str()));
979 if (wire
->port_input
)
980 ex_input_eq
.push_back(stringf(" (= (|%s_n %s| state) (|%s_n %s| other_state))",
981 get_id(module
), get_id(wire
), get_id(module
), get_id(wire
)));
988 if (verbose
) log("=> export logic associated with the initial state\n");
990 vector
<string
> init_list
;
991 for (auto wire
: module
->wires())
992 if (wire
->attributes
.count(ID::init
)) {
993 RTLIL::SigSpec sig
= sigmap(wire
);
994 Const val
= wire
->attributes
.at(ID::init
);
995 val
.bits
.resize(GetSize(sig
), State::Sx
);
996 if (bvmode
&& GetSize(sig
) > 1) {
997 Const
mask(State::S1
, GetSize(sig
));
998 bool use_mask
= false;
999 for (int i
= 0; i
< GetSize(sig
); i
++)
1000 if (val
[i
] != State::S0
&& val
[i
] != State::S1
) {
1002 mask
[i
] = State::S0
;
1006 init_list
.push_back(stringf("(= (bvand %s #b%s) #b%s) ; %s", get_bv(sig
).c_str(), mask
.as_string().c_str(), val
.as_string().c_str(), get_id(wire
)));
1008 init_list
.push_back(stringf("(= %s #b%s) ; %s", get_bv(sig
).c_str(), val
.as_string().c_str(), get_id(wire
)));
1010 for (int i
= 0; i
< GetSize(sig
); i
++)
1011 if (val
[i
] == State::S0
|| val
[i
] == State::S1
)
1012 init_list
.push_back(stringf("(= %s %s) ; %s", get_bool(sig
[i
]).c_str(), val
[i
] == State::S1
? "true" : "false", get_id(wire
)));
1016 if (verbose
) log("=> export logic driving asserts\n");
1018 int assert_id
= 0, assume_id
= 0, cover_id
= 0;
1019 vector
<string
> assert_list
, assume_list
, cover_list
;
1021 for (auto cell
: module
->cells())
1023 if (cell
->type
.in(ID($
assert), ID($assume
), ID($cover
)))
1025 int &id
= cell
->type
== ID($
assert) ? assert_id
:
1026 cell
->type
== ID($assume
) ? assume_id
:
1027 cell
->type
== ID($cover
) ? cover_id
: *(int*)nullptr;
1029 char postfix
= cell
->type
== ID($
assert) ? 'a' :
1030 cell
->type
== ID($assume
) ? 'u' :
1031 cell
->type
== ID($cover
) ? 'c' : 0;
1033 string name_a
= get_bool(cell
->getPort(ID::A
));
1034 string name_en
= get_bool(cell
->getPort(ID::EN
));
1035 if (cell
->name
[0] == '$' && cell
->attributes
.count(ID::src
))
1036 decls
.push_back(stringf("; yosys-smt2-%s %d %s %s\n", cell
->type
.c_str() + 1, id
, get_id(cell
), cell
->attributes
.at(ID::src
).decode_string().c_str()));
1038 decls
.push_back(stringf("; yosys-smt2-%s %d %s\n", cell
->type
.c_str() + 1, id
, get_id(cell
)));
1040 if (cell
->type
== ID($cover
))
1041 decls
.push_back(stringf("(define-fun |%s_%c %d| ((state |%s_s|)) Bool (and %s %s)) ; %s\n",
1042 get_id(module
), postfix
, id
, get_id(module
), name_a
.c_str(), name_en
.c_str(), get_id(cell
)));
1044 decls
.push_back(stringf("(define-fun |%s_%c %d| ((state |%s_s|)) Bool (or %s (not %s))) ; %s\n",
1045 get_id(module
), postfix
, id
, get_id(module
), name_a
.c_str(), name_en
.c_str(), get_id(cell
)));
1047 if (cell
->type
== ID($
assert))
1048 assert_list
.push_back(stringf("(|%s_a %d| state)", get_id(module
), id
));
1049 else if (cell
->type
== ID($assume
))
1050 assume_list
.push_back(stringf("(|%s_u %d| state)", get_id(module
), id
));
1056 if (verbose
) log("=> export logic driving hierarchical cells\n");
1058 for (auto cell
: module
->cells())
1059 if (module
->design
->module(cell
->type
) != nullptr)
1062 while (!hiercells_queue
.empty())
1064 std::set
<RTLIL::Cell
*> queue
;
1065 queue
.swap(hiercells_queue
);
1067 for (auto cell
: queue
)
1069 string cell_state
= stringf("(|%s_h %s| state)", get_id(module
), get_id(cell
->name
));
1070 Module
*m
= module
->design
->module(cell
->type
);
1071 log_assert(m
!= nullptr);
1073 hier
.push_back(stringf(" (= (|%s_is| state) (|%s_is| %s))\n",
1074 get_id(module
), get_id(cell
->type
), cell_state
.c_str()));
1076 for (auto &conn
: cell
->connections())
1078 if (GetSize(conn
.second
) == 0)
1081 Wire
*w
= m
->wire(conn
.first
);
1082 SigSpec sig
= sigmap(conn
.second
);
1084 if (bvmode
|| GetSize(w
) == 1) {
1085 hier
.push_back(stringf(" (= %s (|%s_n %s| %s)) ; %s.%s\n", (GetSize(w
) > 1 ? get_bv(sig
) : get_bool(sig
)).c_str(),
1086 get_id(cell
->type
), get_id(w
), cell_state
.c_str(), get_id(cell
->type
), get_id(w
)));
1088 for (int i
= 0; i
< GetSize(w
); i
++)
1089 hier
.push_back(stringf(" (= %s (|%s_n %s %d| %s)) ; %s.%s[%d]\n", get_bool(sig
[i
]).c_str(),
1090 get_id(cell
->type
), get_id(w
), i
, cell_state
.c_str(), get_id(cell
->type
), get_id(w
), i
));
1096 for (int iter
= 1; !registers
.empty() || !memory_queue
.empty(); iter
++)
1098 pool
<Cell
*> this_regs
;
1099 this_regs
.swap(registers
);
1101 if (verbose
) log("=> export logic driving registers [iteration %d]\n", iter
);
1103 for (auto cell
: this_regs
)
1105 if (cell
->type
.in(ID($_FF_
), ID($_DFF_P_
), ID($_DFF_N_
)))
1107 std::string expr_d
= get_bool(cell
->getPort(ID::D
));
1108 std::string expr_q
= get_bool(cell
->getPort(ID::Q
), "next_state");
1109 trans
.push_back(stringf(" (= %s %s) ; %s %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
), log_signal(cell
->getPort(ID::Q
))));
1110 ex_state_eq
.push_back(stringf("(= %s %s)", get_bool(cell
->getPort(ID::Q
)).c_str(), get_bool(cell
->getPort(ID::Q
), "other_state").c_str()));
1113 if (cell
->type
.in(ID($ff
), ID($dff
)))
1115 std::string expr_d
= get_bv(cell
->getPort(ID::D
));
1116 std::string expr_q
= get_bv(cell
->getPort(ID::Q
), "next_state");
1117 trans
.push_back(stringf(" (= %s %s) ; %s %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
), log_signal(cell
->getPort(ID::Q
))));
1118 ex_state_eq
.push_back(stringf("(= %s %s)", get_bv(cell
->getPort(ID::Q
)).c_str(), get_bv(cell
->getPort(ID::Q
), "other_state").c_str()));
1121 if (cell
->type
.in(ID($anyconst
), ID($allconst
)))
1123 std::string expr_d
= get_bv(cell
->getPort(ID::Y
));
1124 std::string expr_q
= get_bv(cell
->getPort(ID::Y
), "next_state");
1125 trans
.push_back(stringf(" (= %s %s) ; %s %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
), log_signal(cell
->getPort(ID::Y
))));
1126 if (cell
->type
== ID($anyconst
))
1127 ex_state_eq
.push_back(stringf("(= %s %s)", get_bv(cell
->getPort(ID::Y
)).c_str(), get_bv(cell
->getPort(ID::Y
), "other_state").c_str()));
1131 std::set
<Mem
*> this_mems
;
1132 this_mems
.swap(memory_queue
);
1134 for (auto mem
: this_mems
)
1136 int arrayid
= memarrays
.at(mem
);
1138 int abits
= ceil_log2(mem
->size
);;
1140 bool has_sync_wr
= false;
1141 bool has_async_wr
= false;
1142 for (auto &port
: mem
->wr_ports
) {
1143 if (port
.clk_enable
)
1146 has_async_wr
= true;
1149 string initial_memstate
, final_memstate
;
1152 log_assert(!has_sync_wr
);
1153 initial_memstate
= stringf("%s#%d#0", get_id(module
), arrayid
);
1154 final_memstate
= stringf("%s#%d#final", get_id(module
), arrayid
);
1160 makebits(final_memstate
, mem
->width
*mem
->size
, get_id(mem
->memid
));
1163 for (int i
= 0; i
< GetSize(mem
->wr_ports
); i
++)
1165 auto &port
= mem
->wr_ports
[i
];
1166 SigSpec addr_sig
= port
.addr
;
1167 addr_sig
.extend_u0(abits
);
1169 std::string addr
= get_bv(addr_sig
);
1170 std::string data
= get_bv(port
.data
);
1171 std::string mask
= get_bv(port
.en
);
1173 decls
.push_back(stringf("(define-fun |%s_m:W%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1174 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
1175 addr
= stringf("(|%s_m:W%dA %s| state)", get_id(module
), i
, get_id(mem
->memid
));
1177 decls
.push_back(stringf("(define-fun |%s_m:W%dD %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1178 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), mem
->width
, data
.c_str(), log_signal(port
.data
)));
1179 data
= stringf("(|%s_m:W%dD %s| state)", get_id(module
), i
, get_id(mem
->memid
));
1181 decls
.push_back(stringf("(define-fun |%s_m:W%dM %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1182 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), mem
->width
, mask
.c_str(), log_signal(port
.en
)));
1183 mask
= stringf("(|%s_m:W%dM %s| state)", get_id(module
), i
, get_id(mem
->memid
));
1185 std::string data_expr
;
1187 for (int k
= mem
->size
-1; k
>= 0; k
--) {
1188 std::string new_data
= stringf("(bvor (bvand %s %s) (bvand ((_ extract %d %d) (|%s#%d#%d| state)) (bvnot %s)))",
1189 data
.c_str(), mask
.c_str(), mem
->width
*(k
+1)-1, mem
->width
*k
, get_id(module
), arrayid
, i
, mask
.c_str());
1190 data_expr
+= stringf("\n (ite (= %s #b%s) %s ((_ extract %d %d) (|%s#%d#%d| state)))",
1191 addr
.c_str(), Const(k
+mem
->start_offset
, abits
).as_string().c_str(), new_data
.c_str(),
1192 mem
->width
*(k
+1)-1, mem
->width
*k
, get_id(module
), arrayid
, i
);
1195 decls
.push_back(stringf("(define-fun |%s#%d#%d| ((state |%s_s|)) (_ BitVec %d) (concat%s)) ; %s\n",
1196 get_id(module
), arrayid
, i
+1, get_id(module
), mem
->width
*mem
->size
, data_expr
.c_str(), get_id(mem
->memid
)));
1203 dtmembers
.push_back(stringf(" (|%s| (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
1204 initial_memstate
.c_str(), abits
, mem
->width
, get_id(mem
->memid
)));
1206 decls
.push_back(stringf("(declare-fun |%s| (|%s_s|) (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
1207 initial_memstate
.c_str(), get_id(module
), abits
, mem
->width
, get_id(mem
->memid
)));
1210 for (int i
= 0; i
< GetSize(mem
->wr_ports
); i
++)
1212 auto &port
= mem
->wr_ports
[i
];
1213 SigSpec addr_sig
= port
.addr
;
1214 addr_sig
.extend_u0(abits
);
1216 std::string addr
= get_bv(addr_sig
);
1217 std::string data
= get_bv(port
.data
);
1218 std::string mask
= get_bv(port
.en
);
1220 decls
.push_back(stringf("(define-fun |%s_m:W%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1221 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
1222 addr
= stringf("(|%s_m:W%dA %s| state)", get_id(module
), i
, get_id(mem
->memid
));
1224 decls
.push_back(stringf("(define-fun |%s_m:W%dD %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1225 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), mem
->width
, data
.c_str(), log_signal(port
.data
)));
1226 data
= stringf("(|%s_m:W%dD %s| state)", get_id(module
), i
, get_id(mem
->memid
));
1228 decls
.push_back(stringf("(define-fun |%s_m:W%dM %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1229 get_id(module
), i
, get_id(mem
->memid
), get_id(module
), mem
->width
, mask
.c_str(), log_signal(port
.en
)));
1230 mask
= stringf("(|%s_m:W%dM %s| state)", get_id(module
), i
, get_id(mem
->memid
));
1232 data
= stringf("(bvor (bvand %s %s) (bvand (select (|%s#%d#%d| state) %s) (bvnot %s)))",
1233 data
.c_str(), mask
.c_str(), get_id(module
), arrayid
, i
, addr
.c_str(), mask
.c_str());
1235 string
empty_mask(mem
->width
, '0');
1237 decls
.push_back(stringf("(define-fun |%s#%d#%d| ((state |%s_s|)) (Array (_ BitVec %d) (_ BitVec %d)) "
1238 "(ite (= %s #b%s) (|%s#%d#%d| state) (store (|%s#%d#%d| state) %s %s))) ; %s\n",
1239 get_id(module
), arrayid
, i
+1, get_id(module
), abits
, mem
->width
,
1240 mask
.c_str(), empty_mask
.c_str(), get_id(module
), arrayid
, i
, get_id(module
), arrayid
, i
, addr
.c_str(), data
.c_str(), get_id(mem
->memid
)));
1244 std::string expr_d
= stringf("(|%s#%d#%d| state)", get_id(module
), arrayid
, GetSize(mem
->wr_ports
));
1245 std::string expr_q
= stringf("(|%s#%d#0| next_state)", get_id(module
), arrayid
);
1246 trans
.push_back(stringf(" (= %s %s) ; %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(mem
->memid
)));
1247 ex_state_eq
.push_back(stringf("(= (|%s#%d#0| state) (|%s#%d#0| other_state))", get_id(module
), arrayid
, get_id(module
), arrayid
));
1250 hier
.push_back(stringf(" (= %s (|%s| state)) ; %s\n", expr_d
.c_str(), final_memstate
.c_str(), get_id(mem
->memid
)));
1252 Const init_data
= mem
->get_init_data();
1254 for (int i
= 0; i
< mem
->size
; i
++)
1256 if (i
*mem
->width
>= GetSize(init_data
))
1259 Const initword
= init_data
.extract(i
*mem
->width
, mem
->width
, State::Sx
);
1260 Const initmask
= initword
;
1261 bool gen_init_constr
= false;
1263 for (int k
= 0; k
< GetSize(initword
); k
++) {
1264 if (initword
[k
] == State::S0
|| initword
[k
] == State::S1
) {
1265 gen_init_constr
= true;
1266 initmask
[k
] = State::S1
;
1268 initmask
[k
] = State::S0
;
1269 initword
[k
] = State::S0
;
1273 if (gen_init_constr
)
1278 init_list
.push_back(stringf("(= (bvand (select (|%s#%d#0| state) #b%s) #b%s) #b%s) ; %s[%d]",
1279 get_id(module
), arrayid
, Const(i
, abits
).as_string().c_str(),
1280 initmask
.as_string().c_str(), initword
.as_string().c_str(), get_id(mem
->memid
), i
));
1286 if (verbose
) log("=> finalizing SMT2 representation of %s.\n", log_id(module
));
1288 for (auto c
: hiercells
) {
1289 assert_list
.push_back(stringf("(|%s_a| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1290 assume_list
.push_back(stringf("(|%s_u| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1291 init_list
.push_back(stringf("(|%s_i| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1292 hier
.push_back(stringf(" (|%s_h| (|%s_h %s| state))\n", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1293 trans
.push_back(stringf(" (|%s_t| (|%s_h %s| state) (|%s_h %s| next_state))\n",
1294 get_id(c
->type
), get_id(module
), get_id(c
->name
), get_id(module
), get_id(c
->name
)));
1295 ex_state_eq
.push_back(stringf("(|%s_ex_state_eq| (|%s_h %s| state) (|%s_h %s| other_state))\n",
1296 get_id(c
->type
), get_id(module
), get_id(c
->name
), get_id(module
), get_id(c
->name
)));
1301 string expr
= ex_state_eq
.empty() ? "true" : "(and";
1302 if (!ex_state_eq
.empty()) {
1303 if (GetSize(ex_state_eq
) == 1) {
1304 expr
= "\n " + ex_state_eq
.front() + "\n";
1306 for (auto &str
: ex_state_eq
)
1307 expr
+= stringf("\n %s", str
.c_str());
1311 decls
.push_back(stringf("(define-fun |%s_ex_state_eq| ((state |%s_s|) (other_state |%s_s|)) Bool %s)\n",
1312 get_id(module
), get_id(module
), get_id(module
), expr
.c_str()));
1314 expr
= ex_input_eq
.empty() ? "true" : "(and";
1315 if (!ex_input_eq
.empty()) {
1316 if (GetSize(ex_input_eq
) == 1) {
1317 expr
= "\n " + ex_input_eq
.front() + "\n";
1319 for (auto &str
: ex_input_eq
)
1320 expr
+= stringf("\n %s", str
.c_str());
1324 decls
.push_back(stringf("(define-fun |%s_ex_input_eq| ((state |%s_s|) (other_state |%s_s|)) Bool %s)\n",
1325 get_id(module
), get_id(module
), get_id(module
), expr
.c_str()));
1328 string assert_expr
= assert_list
.empty() ? "true" : "(and";
1329 if (!assert_list
.empty()) {
1330 if (GetSize(assert_list
) == 1) {
1331 assert_expr
= "\n " + assert_list
.front() + "\n";
1333 for (auto &str
: assert_list
)
1334 assert_expr
+= stringf("\n %s", str
.c_str());
1335 assert_expr
+= "\n)";
1338 decls
.push_back(stringf("(define-fun |%s_a| ((state |%s_s|)) Bool %s)\n",
1339 get_id(module
), get_id(module
), assert_expr
.c_str()));
1341 string assume_expr
= assume_list
.empty() ? "true" : "(and";
1342 if (!assume_list
.empty()) {
1343 if (GetSize(assume_list
) == 1) {
1344 assume_expr
= "\n " + assume_list
.front() + "\n";
1346 for (auto &str
: assume_list
)
1347 assume_expr
+= stringf("\n %s", str
.c_str());
1348 assume_expr
+= "\n)";
1351 decls
.push_back(stringf("(define-fun |%s_u| ((state |%s_s|)) Bool %s)\n",
1352 get_id(module
), get_id(module
), assume_expr
.c_str()));
1354 string init_expr
= init_list
.empty() ? "true" : "(and";
1355 if (!init_list
.empty()) {
1356 if (GetSize(init_list
) == 1) {
1357 init_expr
= "\n " + init_list
.front() + "\n";
1359 for (auto &str
: init_list
)
1360 init_expr
+= stringf("\n %s", str
.c_str());
1364 decls
.push_back(stringf("(define-fun |%s_i| ((state |%s_s|)) Bool %s)\n",
1365 get_id(module
), get_id(module
), init_expr
.c_str()));
1368 void write(std::ostream
&f
)
1370 f
<< stringf("; yosys-smt2-module %s\n", get_id(module
));
1373 f
<< stringf("(define-sort |%s_s| () (_ BitVec %d))\n", get_id(module
), statebv_width
);
1374 mod_stbv_width
[module
->name
] = statebv_width
;
1377 f
<< stringf("(declare-datatype |%s_s| ((|%s_mk|\n", get_id(module
), get_id(module
));
1378 for (auto it
: dtmembers
)
1380 f
<< stringf(")))\n");
1382 f
<< stringf("(declare-sort |%s_s| 0)\n", get_id(module
));
1384 for (auto it
: decls
)
1387 f
<< stringf("(define-fun |%s_h| ((state |%s_s|)) Bool ", get_id(module
), get_id(module
));
1388 if (GetSize(hier
) > 1) {
1390 for (auto it
: hier
)
1394 if (GetSize(hier
) == 1)
1395 f
<< "\n" + hier
.front() + ")\n";
1399 f
<< stringf("(define-fun |%s_t| ((state |%s_s|) (next_state |%s_s|)) Bool ", get_id(module
), get_id(module
), get_id(module
));
1400 if (GetSize(trans
) > 1) {
1402 for (auto it
: trans
)
1406 if (GetSize(trans
) == 1)
1407 f
<< "\n" + trans
.front() + ")";
1410 f
<< stringf(" ; end of module %s\n", get_id(module
));
1414 struct Smt2Backend
: public Backend
{
1415 Smt2Backend() : Backend("smt2", "write design to SMT-LIBv2 file") { }
1416 void help() override
1418 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1420 log(" write_smt2 [options] [filename]\n");
1422 log("Write a SMT-LIBv2 [1] description of the current design. For a module with name\n");
1423 log("'<mod>' this will declare the sort '<mod>_s' (state of the module) and will\n");
1424 log("define and declare functions operating on that state.\n");
1426 log("The following SMT2 functions are generated for a module with name '<mod>'.\n");
1427 log("Some declarations/definitions are printed with a special comment. A prover\n");
1428 log("using the SMT2 files can use those comments to collect all relevant metadata\n");
1429 log("about the design.\n");
1431 log(" ; yosys-smt2-module <mod>\n");
1432 log(" (declare-sort |<mod>_s| 0)\n");
1433 log(" The sort representing a state of module <mod>.\n");
1435 log(" (define-fun |<mod>_h| ((state |<mod>_s|)) Bool (...))\n");
1436 log(" This function must be asserted for each state to establish the\n");
1437 log(" design hierarchy.\n");
1439 log(" ; yosys-smt2-input <wirename> <width>\n");
1440 log(" ; yosys-smt2-output <wirename> <width>\n");
1441 log(" ; yosys-smt2-register <wirename> <width>\n");
1442 log(" ; yosys-smt2-wire <wirename> <width>\n");
1443 log(" (define-fun |<mod>_n <wirename>| (|<mod>_s|) (_ BitVec <width>))\n");
1444 log(" (define-fun |<mod>_n <wirename>| (|<mod>_s|) Bool)\n");
1445 log(" For each port, register, and wire with the 'keep' attribute set an\n");
1446 log(" accessor function is generated. Single-bit wires are returned as Bool,\n");
1447 log(" multi-bit wires as BitVec.\n");
1449 log(" ; yosys-smt2-cell <submod> <instancename>\n");
1450 log(" (declare-fun |<mod>_h <instancename>| (|<mod>_s|) |<submod>_s|)\n");
1451 log(" There is a function like that for each hierarchical instance. It\n");
1452 log(" returns the sort that represents the state of the sub-module that\n");
1453 log(" implements the instance.\n");
1455 log(" (declare-fun |<mod>_is| (|<mod>_s|) Bool)\n");
1456 log(" This function must be asserted 'true' for initial states, and 'false'\n");
1457 log(" otherwise.\n");
1459 log(" (define-fun |<mod>_i| ((state |<mod>_s|)) Bool (...))\n");
1460 log(" This function must be asserted 'true' for initial states. For\n");
1461 log(" non-initial states it must be left unconstrained.\n");
1463 log(" (define-fun |<mod>_t| ((state |<mod>_s|) (next_state |<mod>_s|)) Bool (...))\n");
1464 log(" This function evaluates to 'true' if the states 'state' and\n");
1465 log(" 'next_state' form a valid state transition.\n");
1467 log(" (define-fun |<mod>_a| ((state |<mod>_s|)) Bool (...))\n");
1468 log(" This function evaluates to 'true' if all assertions hold in the state.\n");
1470 log(" (define-fun |<mod>_u| ((state |<mod>_s|)) Bool (...))\n");
1471 log(" This function evaluates to 'true' if all assumptions hold in the state.\n");
1473 log(" ; yosys-smt2-assert <id> <filename:linenum>\n");
1474 log(" (define-fun |<mod>_a <id>| ((state |<mod>_s|)) Bool (...))\n");
1475 log(" Each $assert cell is converted into one of this functions. The function\n");
1476 log(" evaluates to 'true' if the assert statement holds in the state.\n");
1478 log(" ; yosys-smt2-assume <id> <filename:linenum>\n");
1479 log(" (define-fun |<mod>_u <id>| ((state |<mod>_s|)) Bool (...))\n");
1480 log(" Each $assume cell is converted into one of this functions. The function\n");
1481 log(" evaluates to 'true' if the assume statement holds in the state.\n");
1483 log(" ; yosys-smt2-cover <id> <filename:linenum>\n");
1484 log(" (define-fun |<mod>_c <id>| ((state |<mod>_s|)) Bool (...))\n");
1485 log(" Each $cover cell is converted into one of this functions. The function\n");
1486 log(" evaluates to 'true' if the cover statement is activated in the state.\n");
1491 log(" this will print the recursive walk used to export the modules.\n");
1494 log(" Use a BitVec sort to represent a state instead of an uninterpreted\n");
1495 log(" sort. As a side-effect this will prevent use of arrays to model\n");
1496 log(" memories.\n");
1499 log(" Use SMT-LIB 2.6 style datatypes to represent a state instead of an\n");
1500 log(" uninterpreted sort.\n");
1503 log(" disable support for BitVec (FixedSizeBitVectors theory). without this\n");
1504 log(" option multi-bit wires are represented using the BitVec sort and\n");
1505 log(" support for coarse grain cells (incl. arithmetic) is enabled.\n");
1508 log(" disable support for memories (via ArraysEx theory). this option is\n");
1509 log(" implied by -nobv. only $mem cells without merged registers in\n");
1510 log(" read ports are supported. call \"memory\" with -nordff to make sure\n");
1511 log(" that no registers are merged into $mem read ports. '<mod>_m' functions\n");
1512 log(" will be generated for accessing the arrays that are used to represent\n");
1513 log(" memories.\n");
1516 log(" create '<mod>_n' functions for all public wires. by default only ports,\n");
1517 log(" registers, and wires with the 'keep' attribute are exported.\n");
1519 log(" -tpl <template_file>\n");
1520 log(" use the given template file. the line containing only the token '%%%%'\n");
1521 log(" is replaced with the regular output of this command.\n");
1523 log(" -solver-option <option> <value>\n");
1524 log(" emit a `; yosys-smt2-solver-option` directive for yosys-smtbmc to write\n");
1525 log(" the given option as a `(set-option ...)` command in the SMT-LIBv2.\n");
1527 log("[1] For more information on SMT-LIBv2 visit http://smt-lib.org/ or read David\n");
1528 log("R. Cok's tutorial: https://smtlib.github.io/jSMTLIB/SMTLIBTutorial.pdf\n");
1530 log("---------------------------------------------------------------------------\n");
1534 log("Consider the following module (test.v). We want to prove that the output can\n");
1535 log("never transition from a non-zero value to a zero value.\n");
1537 log(" module test(input clk, output reg [3:0] y);\n");
1538 log(" always @(posedge clk)\n");
1539 log(" y <= (y << 1) | ^y;\n");
1540 log(" endmodule\n");
1542 log("For this proof we create the following template (test.tpl).\n");
1544 log(" ; we need QF_UFBV for this proof\n");
1545 log(" (set-logic QF_UFBV)\n");
1547 log(" ; insert the auto-generated code here\n");
1550 log(" ; declare two state variables s1 and s2\n");
1551 log(" (declare-fun s1 () test_s)\n");
1552 log(" (declare-fun s2 () test_s)\n");
1554 log(" ; state s2 is the successor of state s1\n");
1555 log(" (assert (test_t s1 s2))\n");
1557 log(" ; we are looking for a model with y non-zero in s1\n");
1558 log(" (assert (distinct (|test_n y| s1) #b0000))\n");
1560 log(" ; we are looking for a model with y zero in s2\n");
1561 log(" (assert (= (|test_n y| s2) #b0000))\n");
1563 log(" ; is there such a model?\n");
1564 log(" (check-sat)\n");
1566 log("The following yosys script will create a 'test.smt2' file for our proof:\n");
1568 log(" read_verilog test.v\n");
1569 log(" hierarchy -check; proc; opt; check -assert\n");
1570 log(" write_smt2 -bv -tpl test.tpl test.smt2\n");
1572 log("Running 'cvc4 test.smt2' will print 'unsat' because y can never transition\n");
1573 log("from non-zero to zero in the test design.\n");
1576 void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
) override
1578 std::ifstream template_f
;
1579 bool bvmode
= true, memmode
= true, wiresmode
= false, verbose
= false, statebv
= false, statedt
= false;
1580 bool forallmode
= false;
1581 dict
<std::string
, std::string
> solver_options
;
1583 log_header(design
, "Executing SMT2 backend.\n");
1586 Pass::call(design
, "bmuxmap");
1587 Pass::call(design
, "demuxmap");
1591 for (argidx
= 1; argidx
< args
.size(); argidx
++)
1593 if (args
[argidx
] == "-tpl" && argidx
+1 < args
.size()) {
1594 template_f
.open(args
[++argidx
]);
1595 if (template_f
.fail())
1596 log_error("Can't open template file `%s'.\n", args
[argidx
].c_str());
1599 if (args
[argidx
] == "-bv" || args
[argidx
] == "-mem") {
1600 log_warning("Options -bv and -mem are now the default. Support for -bv and -mem will be removed in the future.\n");
1603 if (args
[argidx
] == "-stbv") {
1608 if (args
[argidx
] == "-stdt") {
1613 if (args
[argidx
] == "-nobv") {
1618 if (args
[argidx
] == "-nomem") {
1622 if (args
[argidx
] == "-wires") {
1626 if (args
[argidx
] == "-verbose") {
1630 if (args
[argidx
] == "-solver-option" && argidx
+2 < args
.size()) {
1631 solver_options
.emplace(args
[argidx
+1], args
[argidx
+2]);
1637 extra_args(f
, filename
, args
, argidx
);
1639 if (template_f
.is_open()) {
1641 while (std::getline(template_f
, line
)) {
1643 while (indent
< GetSize(line
) && (line
[indent
] == ' ' || line
[indent
] == '\t'))
1645 if (line
.compare(indent
, 2, "%%") == 0)
1647 *f
<< line
<< std::endl
;
1651 *f
<< stringf("; SMT-LIBv2 description generated by %s\n", yosys_version_str
);
1654 *f
<< stringf("; yosys-smt2-nobv\n");
1657 *f
<< stringf("; yosys-smt2-nomem\n");
1660 *f
<< stringf("; yosys-smt2-stbv\n");
1663 *f
<< stringf("; yosys-smt2-stdt\n");
1665 for (auto &it
: solver_options
)
1666 *f
<< stringf("; yosys-smt2-solver-option %s %s\n", it
.first
.c_str(), it
.second
.c_str());
1668 std::vector
<RTLIL::Module
*> sorted_modules
;
1670 // extract module dependencies
1671 std::map
<RTLIL::Module
*, std::set
<RTLIL::Module
*>> module_deps
;
1672 for (auto mod
: design
->modules()) {
1673 module_deps
[mod
] = std::set
<RTLIL::Module
*>();
1674 for (auto cell
: mod
->cells())
1675 if (design
->has(cell
->type
))
1676 module_deps
[mod
].insert(design
->module(cell
->type
));
1679 // simple good-enough topological sort
1680 // (O(n*m) on n elements and depth m)
1681 while (module_deps
.size() > 0) {
1682 size_t sorted_modules_idx
= sorted_modules
.size();
1683 for (auto &it
: module_deps
) {
1684 for (auto &dep
: it
.second
)
1685 if (module_deps
.count(dep
) > 0)
1687 // log("Next in topological sort: %s\n", log_id(it.first->name));
1688 sorted_modules
.push_back(it
.first
);
1691 if (sorted_modules_idx
== sorted_modules
.size())
1692 log_error("Cyclic dependency between modules found! Cycle includes module %s.\n", log_id(module_deps
.begin()->first
->name
));
1693 while (sorted_modules_idx
< sorted_modules
.size())
1694 module_deps
.erase(sorted_modules
.at(sorted_modules_idx
++));
1697 dict
<IdString
, int> mod_stbv_width
;
1698 dict
<IdString
, dict
<IdString
, pair
<bool, bool>>> mod_clk_cache
;
1699 Module
*topmod
= design
->top_module();
1700 std::string topmod_id
;
1702 for (auto module
: sorted_modules
)
1703 for (auto cell
: module
->cells())
1704 if (cell
->type
.in(ID($allconst
), ID($allseq
)))
1709 *f
<< stringf("; yosys-smt2-forall\n");
1710 if (!statebv
&& !statedt
)
1711 log_error("Forall-exists problems are only supported in -stbv or -stdt mode.\n");
1714 for (auto module
: sorted_modules
)
1716 if (module
->get_blackbox_attribute() || module
->has_processes_warn())
1719 log("Creating SMT-LIBv2 representation of module %s.\n", log_id(module
));
1721 Smt2Worker
worker(module
, bvmode
, memmode
, wiresmode
, verbose
, statebv
, statedt
, forallmode
, mod_stbv_width
, mod_clk_cache
);
1725 if (module
== topmod
)
1726 topmod_id
= worker
.get_id(module
);
1730 *f
<< stringf("; yosys-smt2-topmod %s\n", topmod_id
.c_str());
1732 *f
<< stringf("; end of yosys output\n");
1734 if (template_f
.is_open()) {
1736 while (std::getline(template_f
, line
))
1737 *f
<< line
<< std::endl
;
1742 PRIVATE_NAMESPACE_END