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.
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"
28 PRIVATE_NAMESPACE_BEGIN
34 RTLIL::Module
*module
;
35 bool bvmode
, memmode
, wiresmode
, verbose
;
38 std::vector
<std::string
> decls
, trans
, hier
;
39 std::map
<RTLIL::SigBit
, RTLIL::Cell
*> bit_driver
;
40 std::set
<RTLIL::Cell
*> exported_cells
, hiercells
;
41 pool
<Cell
*> recursive_cells
, registers
;
43 std::map
<RTLIL::SigBit
, std::pair
<int, int>> fcache
;
44 std::map
<Cell
*, int> memarrays
;
45 std::map
<int, int> bvsizes
;
46 dict
<IdString
, char*> ids
;
48 const char *get_id(IdString n
)
50 if (ids
.count(n
) == 0) {
51 std::string str
= log_id(n
);
52 for (int i
= 0; i
< GetSize(str
); i
++) {
56 ids
[n
] = strdup(str
.c_str());
62 const char *get_id(T
*obj
) {
63 return get_id(obj
->name
);
66 Smt2Worker(RTLIL::Module
*module
, bool bvmode
, bool memmode
, bool wiresmode
, bool verbose
) :
67 ct(module
->design
), sigmap(module
), module(module
), bvmode(bvmode
), memmode(memmode
),
68 wiresmode(wiresmode
), verbose(verbose
), idcounter(0)
70 decls
.push_back(stringf("(declare-sort |%s_s| 0)\n", get_id(module
)));
71 decls
.push_back(stringf("(declare-fun |%s_is| (|%s_s|) Bool)\n", get_id(module
), get_id(module
)));
73 for (auto cell
: module
->cells())
74 for (auto &conn
: cell
->connections()) {
75 bool is_input
= ct
.cell_input(cell
->type
, conn
.first
);
76 bool is_output
= ct
.cell_output(cell
->type
, conn
.first
);
77 if (is_output
&& !is_input
)
78 for (auto bit
: sigmap(conn
.second
)) {
79 if (bit_driver
.count(bit
))
80 log_error("Found multiple drivers for %s.\n", log_signal(bit
));
81 bit_driver
[bit
] = cell
;
83 else if (is_output
|| !is_input
)
84 log_error("Unsupported or unknown directionality on port %s of cell %s.%s (%s).\n",
85 log_id(conn
.first
), log_id(module
), log_id(cell
), log_id(cell
->type
));
96 const char *get_id(Module
*m
)
98 return get_id(m
->name
);
101 const char *get_id(Cell
*c
)
103 return get_id(c
->name
);
106 const char *get_id(Wire
*w
)
108 return get_id(w
->name
);
111 void register_bool(RTLIL::SigBit bit
, int id
)
113 if (verbose
) log("%*s-> register_bool: %s %d\n", 2+2*GetSize(recursive_cells
), "",
114 log_signal(bit
), id
);
117 log_assert(fcache
.count(bit
) == 0);
118 fcache
[bit
] = std::pair
<int, int>(id
, -1);
121 void register_bv(RTLIL::SigSpec sig
, int id
)
123 if (verbose
) log("%*s-> register_bv: %s %d\n", 2+2*GetSize(recursive_cells
), "",
124 log_signal(sig
), id
);
129 log_assert(bvsizes
.count(id
) == 0);
130 bvsizes
[id
] = GetSize(sig
);
132 for (int i
= 0; i
< GetSize(sig
); i
++) {
133 log_assert(fcache
.count(sig
[i
]) == 0);
134 fcache
[sig
[i
]] = std::pair
<int, int>(id
, i
);
138 void register_boolvec(RTLIL::SigSpec sig
, int id
)
140 if (verbose
) log("%*s-> register_boolvec: %s %d\n", 2+2*GetSize(recursive_cells
), "",
141 log_signal(sig
), id
);
145 register_bool(sig
[0], id
);
147 for (int i
= 1; i
< GetSize(sig
); i
++)
148 sigmap
.add(sig
[i
], RTLIL::State::S0
);
151 std::string
get_bool(RTLIL::SigBit bit
, const char *state_name
= "state")
155 if (bit
.wire
== nullptr)
156 return bit
== RTLIL::State::S1
? "true" : "false";
158 if (bit_driver
.count(bit
))
159 export_cell(bit_driver
.at(bit
));
162 if (fcache
.count(bit
) == 0) {
163 if (verbose
) log("%*s-> external bool: %s\n", 2+2*GetSize(recursive_cells
), "",
165 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) Bool) ; %s\n",
166 get_id(module
), idcounter
, get_id(module
), log_signal(bit
)));
167 register_bool(bit
, idcounter
++);
170 auto f
= fcache
.at(bit
);
172 return stringf("(= ((_ extract %d %d) (|%s#%d| %s)) #b1)", f
.second
, f
.second
, get_id(module
), f
.first
, state_name
);
173 return stringf("(|%s#%d| %s)", get_id(module
), f
.first
, state_name
);
176 std::string
get_bool(RTLIL::SigSpec sig
, const char *state_name
= "state")
178 return get_bool(sig
.as_bit(), state_name
);
181 std::string
get_bv(RTLIL::SigSpec sig
, const char *state_name
= "state")
186 std::vector
<std::string
> subexpr
;
189 while (orig_sig
!= sig
) {
191 if (bit_driver
.count(bit
))
192 export_cell(bit_driver
.at(bit
));
197 for (int i
= 0, j
= 1; i
< GetSize(sig
); i
+= j
, j
= 1)
199 if (sig
[i
].wire
== nullptr) {
200 while (i
+j
< GetSize(sig
) && sig
[i
+j
].wire
== nullptr) j
++;
201 subexpr
.push_back("#b");
202 for (int k
= i
+j
-1; k
>= i
; k
--)
203 subexpr
.back() += sig
[k
] == RTLIL::State::S1
? "1" : "0";
207 if (fcache
.count(sig
[i
]) && fcache
.at(sig
[i
]).second
== -1) {
208 subexpr
.push_back(stringf("(ite %s #b1 #b0)", get_bool(sig
[i
], state_name
).c_str()));
212 if (fcache
.count(sig
[i
])) {
213 auto t1
= fcache
.at(sig
[i
]);
214 while (i
+j
< GetSize(sig
)) {
215 if (fcache
.count(sig
[i
+j
]) == 0)
217 auto t2
= fcache
.at(sig
[i
+j
]);
218 if (t1
.first
!= t2
.first
)
220 if (t1
.second
+j
!= t2
.second
)
224 if (t1
.second
== 0 && j
== bvsizes
.at(t1
.first
))
225 subexpr
.push_back(stringf("(|%s#%d| %s)", get_id(module
), t1
.first
, state_name
));
227 subexpr
.push_back(stringf("((_ extract %d %d) (|%s#%d| %s))",
228 t1
.second
+ j
- 1, t1
.second
, get_id(module
), t1
.first
, state_name
));
232 std::set
<RTLIL::SigBit
> seen_bits
= { sig
[i
] };
233 while (i
+j
< GetSize(sig
) && sig
[i
+j
].wire
&& !fcache
.count(sig
[i
+j
]) && !seen_bits
.count(sig
[i
+j
]))
234 seen_bits
.insert(sig
[i
+j
]), j
++;
236 if (verbose
) log("%*s-> external bv: %s\n", 2+2*GetSize(recursive_cells
), "",
237 log_signal(sig
.extract(i
, j
)));
238 for (auto bit
: sig
.extract(i
, j
))
239 log_assert(bit_driver
.count(bit
) == 0);
240 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) (_ BitVec %d)) ; %s\n",
241 get_id(module
), idcounter
, get_id(module
), j
, log_signal(sig
.extract(i
, j
))));
242 subexpr
.push_back(stringf("(|%s#%d| %s)", get_id(module
), idcounter
, state_name
));
243 register_bv(sig
.extract(i
, j
), idcounter
++);
246 if (GetSize(subexpr
) > 1) {
247 std::string expr
= "", end_str
= "";
248 for (int i
= GetSize(subexpr
)-1; i
>= 0; i
--) {
249 if (i
> 0) expr
+= " (concat", end_str
+= ")";
250 expr
+= " " + subexpr
[i
];
252 return expr
.substr(1) + end_str
;
254 log_assert(GetSize(subexpr
) == 1);
259 void export_gate(RTLIL::Cell
*cell
, std::string expr
)
261 RTLIL::SigBit bit
= sigmap(cell
->getPort("\\Y").as_bit());
262 std::string processed_expr
;
264 for (char ch
: expr
) {
265 if (ch
== 'A') processed_expr
+= get_bool(cell
->getPort("\\A"));
266 else if (ch
== 'B') processed_expr
+= get_bool(cell
->getPort("\\B"));
267 else if (ch
== 'C') processed_expr
+= get_bool(cell
->getPort("\\C"));
268 else if (ch
== 'D') processed_expr
+= get_bool(cell
->getPort("\\D"));
269 else if (ch
== 'S') processed_expr
+= get_bool(cell
->getPort("\\S"));
270 else processed_expr
+= ch
;
274 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
276 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
277 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(bit
)));
278 register_bool(bit
, idcounter
++);
279 recursive_cells
.erase(cell
);
282 void export_bvop(RTLIL::Cell
*cell
, std::string expr
, char type
= 0)
284 RTLIL::SigSpec sig_a
, sig_b
;
285 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
286 bool is_signed
= cell
->getParam("\\A_SIGNED").as_bool();
287 int width
= GetSize(sig_y
);
289 if (type
== 's' || type
== 'd' || type
== 'b') {
290 width
= max(width
, GetSize(cell
->getPort("\\A")));
291 width
= max(width
, GetSize(cell
->getPort("\\B")));
294 if (cell
->hasPort("\\A")) {
295 sig_a
= cell
->getPort("\\A");
296 sig_a
.extend_u0(width
, is_signed
);
299 if (cell
->hasPort("\\B")) {
300 sig_b
= cell
->getPort("\\B");
301 sig_b
.extend_u0(width
, is_signed
&& !(type
== 's'));
304 std::string processed_expr
;
306 for (char ch
: expr
) {
307 if (ch
== 'A') processed_expr
+= get_bv(sig_a
);
308 else if (ch
== 'B') processed_expr
+= get_bv(sig_b
);
309 else if (ch
== 'L') processed_expr
+= is_signed
? "a" : "l";
310 else if (ch
== 'U') processed_expr
+= is_signed
? "s" : "u";
311 else processed_expr
+= ch
;
314 if (width
!= GetSize(sig_y
) && type
!= 'b')
315 processed_expr
= stringf("((_ extract %d 0) %s)", GetSize(sig_y
)-1, processed_expr
.c_str());
318 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
321 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
322 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(sig_y
)));
323 register_boolvec(sig_y
, idcounter
++);
325 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
326 get_id(module
), idcounter
, get_id(module
), GetSize(sig_y
), processed_expr
.c_str(), log_signal(sig_y
)));
327 register_bv(sig_y
, idcounter
++);
330 recursive_cells
.erase(cell
);
333 void export_reduce(RTLIL::Cell
*cell
, std::string expr
, bool identity_val
)
335 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
336 std::string processed_expr
;
339 if (ch
== 'A' || ch
== 'B') {
340 RTLIL::SigSpec sig
= sigmap(cell
->getPort(stringf("\\%c", ch
)));
342 processed_expr
+= " " + get_bool(bit
);
343 if (GetSize(sig
) == 1)
344 processed_expr
+= identity_val
? " true" : " false";
346 processed_expr
+= ch
;
349 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
351 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
352 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(sig_y
)));
353 register_boolvec(sig_y
, idcounter
++);
354 recursive_cells
.erase(cell
);
357 void export_cell(RTLIL::Cell
*cell
)
360 log("%*s=> export_cell %s (%s) [%s]\n", 2+2*GetSize(recursive_cells
), "",
361 log_id(cell
), log_id(cell
->type
), exported_cells
.count(cell
) ? "old" : "new");
363 if (recursive_cells
.count(cell
))
364 log_error("Found logic loop in module %s! See cell %s.\n", get_id(module
), get_id(cell
));
366 if (exported_cells
.count(cell
))
369 exported_cells
.insert(cell
);
370 recursive_cells
.insert(cell
);
372 if (cell
->type
== "$initstate")
374 SigBit bit
= sigmap(cell
->getPort("\\Y").as_bit());
375 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool (|%s_is| state)) ; %s\n",
376 get_id(module
), idcounter
, get_id(module
), get_id(module
), log_signal(bit
)));
377 register_bool(bit
, idcounter
++);
378 recursive_cells
.erase(cell
);
382 if (cell
->type
== "$_DFF_P_" || cell
->type
== "$_DFF_N_")
384 registers
.insert(cell
);
385 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) Bool) ; %s\n",
386 get_id(module
), idcounter
, get_id(module
), log_signal(cell
->getPort("\\Q"))));
387 register_bool(cell
->getPort("\\Q"), idcounter
++);
388 recursive_cells
.erase(cell
);
392 if (cell
->type
== "$_BUF_") return export_gate(cell
, "A");
393 if (cell
->type
== "$_NOT_") return export_gate(cell
, "(not A)");
394 if (cell
->type
== "$_AND_") return export_gate(cell
, "(and A B)");
395 if (cell
->type
== "$_NAND_") return export_gate(cell
, "(not (and A B))");
396 if (cell
->type
== "$_OR_") return export_gate(cell
, "(or A B)");
397 if (cell
->type
== "$_NOR_") return export_gate(cell
, "(not (or A B))");
398 if (cell
->type
== "$_XOR_") return export_gate(cell
, "(xor A B)");
399 if (cell
->type
== "$_XNOR_") return export_gate(cell
, "(not (xor A B))");
400 if (cell
->type
== "$_MUX_") return export_gate(cell
, "(ite S B A)");
401 if (cell
->type
== "$_AOI3_") return export_gate(cell
, "(not (or (and A B) C))");
402 if (cell
->type
== "$_OAI3_") return export_gate(cell
, "(not (and (or A B) C))");
403 if (cell
->type
== "$_AOI4_") return export_gate(cell
, "(not (or (and A B) (and C D)))");
404 if (cell
->type
== "$_OAI4_") return export_gate(cell
, "(not (and (or A B) (or C D)))");
410 if (cell
->type
== "$dff")
412 registers
.insert(cell
);
413 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) (_ BitVec %d)) ; %s\n",
414 get_id(module
), idcounter
, get_id(module
), GetSize(cell
->getPort("\\Q")), log_signal(cell
->getPort("\\Q"))));
415 register_bv(cell
->getPort("\\Q"), idcounter
++);
416 recursive_cells
.erase(cell
);
420 if (cell
->type
== "$anyconst")
422 registers
.insert(cell
);
423 decls
.push_back(stringf("; yosys-smt2-%s %s#%d %s\n", cell
->type
.c_str() + 1, get_id(module
), idcounter
,
424 cell
->attributes
.count("\\src") ? cell
->attributes
.at("\\src").decode_string().c_str() : get_id(cell
)));
425 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) (_ BitVec %d)) ; %s\n",
426 get_id(module
), idcounter
, get_id(module
), GetSize(cell
->getPort("\\Y")), log_signal(cell
->getPort("\\Y"))));
427 register_bv(cell
->getPort("\\Y"), idcounter
++);
428 recursive_cells
.erase(cell
);
432 if (cell
->type
== "$and") return export_bvop(cell
, "(bvand A B)");
433 if (cell
->type
== "$or") return export_bvop(cell
, "(bvor A B)");
434 if (cell
->type
== "$xor") return export_bvop(cell
, "(bvxor A B)");
435 if (cell
->type
== "$xnor") return export_bvop(cell
, "(bvxnor A B)");
437 if (cell
->type
== "$shl") return export_bvop(cell
, "(bvshl A B)", 's');
438 if (cell
->type
== "$shr") return export_bvop(cell
, "(bvlshr A B)", 's');
439 if (cell
->type
== "$sshl") return export_bvop(cell
, "(bvshl A B)", 's');
440 if (cell
->type
== "$sshr") return export_bvop(cell
, "(bvLshr A B)", 's');
442 if (cell
->type
.in("$shift", "$shiftx")) {
443 if (cell
->getParam("\\B_SIGNED").as_bool()) {
446 return export_bvop(cell
, "(bvlshr A B)", 's');
450 if (cell
->type
== "$lt") return export_bvop(cell
, "(bvUlt A B)", 'b');
451 if (cell
->type
== "$le") return export_bvop(cell
, "(bvUle A B)", 'b');
452 if (cell
->type
== "$ge") return export_bvop(cell
, "(bvUge A B)", 'b');
453 if (cell
->type
== "$gt") return export_bvop(cell
, "(bvUgt A B)", 'b');
455 if (cell
->type
== "$ne") return export_bvop(cell
, "(distinct A B)", 'b');
456 if (cell
->type
== "$nex") return export_bvop(cell
, "(distinct A B)", 'b');
457 if (cell
->type
== "$eq") return export_bvop(cell
, "(= A B)", 'b');
458 if (cell
->type
== "$eqx") return export_bvop(cell
, "(= A B)", 'b');
460 if (cell
->type
== "$not") return export_bvop(cell
, "(bvnot A)");
461 if (cell
->type
== "$pos") return export_bvop(cell
, "A");
462 if (cell
->type
== "$neg") return export_bvop(cell
, "(bvneg A)");
464 if (cell
->type
== "$add") return export_bvop(cell
, "(bvadd A B)");
465 if (cell
->type
== "$sub") return export_bvop(cell
, "(bvsub A B)");
466 if (cell
->type
== "$mul") return export_bvop(cell
, "(bvmul A B)");
467 if (cell
->type
== "$div") return export_bvop(cell
, "(bvUdiv A B)", 'd');
468 if (cell
->type
== "$mod") return export_bvop(cell
, "(bvUrem A B)", 'd');
470 if (cell
->type
== "$reduce_and") return export_reduce(cell
, "(and A)", true);
471 if (cell
->type
== "$reduce_or") return export_reduce(cell
, "(or A)", false);
472 if (cell
->type
== "$reduce_xor") return export_reduce(cell
, "(xor A)", false);
473 if (cell
->type
== "$reduce_xnor") return export_reduce(cell
, "(not (xor A))", false);
474 if (cell
->type
== "$reduce_bool") return export_reduce(cell
, "(or A)", false);
476 if (cell
->type
== "$logic_not") return export_reduce(cell
, "(not (or A))", false);
477 if (cell
->type
== "$logic_and") return export_reduce(cell
, "(and (or A) (or B))", false);
478 if (cell
->type
== "$logic_or") return export_reduce(cell
, "(or A B)", false);
480 if (cell
->type
== "$mux" || cell
->type
== "$pmux")
482 int width
= GetSize(cell
->getPort("\\Y"));
483 std::string processed_expr
= get_bv(cell
->getPort("\\A"));
485 RTLIL::SigSpec sig_b
= cell
->getPort("\\B");
486 RTLIL::SigSpec sig_s
= cell
->getPort("\\S");
490 for (int i
= 0; i
< GetSize(sig_s
); i
++)
491 processed_expr
= stringf("(ite %s %s %s)", get_bool(sig_s
[i
]).c_str(),
492 get_bv(sig_b
.extract(i
*width
, width
)).c_str(), processed_expr
.c_str());
495 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
497 RTLIL::SigSpec sig
= sigmap(cell
->getPort("\\Y"));
498 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
499 get_id(module
), idcounter
, get_id(module
), width
, processed_expr
.c_str(), log_signal(sig
)));
500 register_bv(sig
, idcounter
++);
501 recursive_cells
.erase(cell
);
505 // FIXME: $slice $concat
508 if (memmode
&& cell
->type
== "$mem")
510 int arrayid
= idcounter
++;
511 memarrays
[cell
] = arrayid
;
513 int abits
= cell
->getParam("\\ABITS").as_int();
514 int width
= cell
->getParam("\\WIDTH").as_int();
515 int rd_ports
= cell
->getParam("\\RD_PORTS").as_int();
517 decls
.push_back(stringf("(declare-fun |%s#%d#0| (|%s_s|) (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
518 get_id(module
), arrayid
, get_id(module
), abits
, width
, get_id(cell
)));
520 decls
.push_back(stringf("; yosys-smt2-memory %s %d %d %d\n", get_id(cell
), abits
, width
, rd_ports
));
521 decls
.push_back(stringf("(define-fun |%s_m %s| ((state |%s_s|)) (Array (_ BitVec %d) (_ BitVec %d)) (|%s#%d#0| state))\n",
522 get_id(module
), get_id(cell
), get_id(module
), abits
, width
, get_id(module
), arrayid
));
524 for (int i
= 0; i
< rd_ports
; i
++)
526 SigSpec addr_sig
= cell
->getPort("\\RD_ADDR").extract(abits
*i
, abits
);
527 SigSpec data_sig
= cell
->getPort("\\RD_DATA").extract(width
*i
, width
);
528 std::string addr
= get_bv(addr_sig
);
530 if (cell
->getParam("\\RD_CLK_ENABLE").extract(i
).as_bool())
531 log_error("Read port %d (%s) of memory %s.%s is clocked. This is not supported by \"write_smt2\"! "
532 "Call \"memory\" with -nordff to avoid this error.\n", i
, log_signal(data_sig
), log_id(cell
), log_id(module
));
534 decls
.push_back(stringf("(define-fun |%s_m:%d %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
535 get_id(module
), i
, get_id(cell
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
537 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) (select (|%s#%d#0| state) %s)) ; %s\n",
538 get_id(module
), idcounter
, get_id(module
), width
, get_id(module
), arrayid
, addr
.c_str(), log_signal(data_sig
)));
539 register_bv(data_sig
, idcounter
++);
542 registers
.insert(cell
);
543 recursive_cells
.erase(cell
);
547 Module
*m
= module
->design
->module(cell
->type
);
551 decls
.push_back(stringf("; yosys-smt2-cell %s %s\n", get_id(cell
->type
), get_id(cell
->name
)));
552 string cell_state
= stringf("(|%s_h %s| state)", get_id(module
), get_id(cell
->name
));
554 for (auto &conn
: cell
->connections())
556 Wire
*w
= m
->wire(conn
.first
);
557 SigSpec sig
= sigmap(conn
.second
);
559 if (w
->port_output
&& !w
->port_input
) {
560 if (GetSize(w
) > 1) {
562 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) (_ BitVec %d)) ; %s\n",
563 get_id(module
), idcounter
, get_id(module
), GetSize(w
), log_signal(sig
)));
564 register_bv(sig
, idcounter
++);
566 for (int i
= 0; i
< GetSize(w
); i
++) {
567 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) Bool) ; %s\n",
568 get_id(module
), idcounter
, get_id(module
), log_signal(sig
[i
])));
569 register_bool(sig
[i
], idcounter
++);
573 decls
.push_back(stringf("(declare-fun |%s#%d| (|%s_s|) Bool) ; %s\n",
574 get_id(module
), idcounter
, get_id(module
), log_signal(sig
)));
575 register_bool(sig
, idcounter
++);
580 decls
.push_back(stringf("(declare-fun |%s_h %s| (|%s_s|) |%s_s|)\n",
581 get_id(module
), get_id(cell
->name
), get_id(module
), get_id(cell
->type
)));
583 hiercells
.insert(cell
);
584 recursive_cells
.erase(cell
);
586 for (auto &conn
: cell
->connections())
588 Wire
*w
= m
->wire(conn
.first
);
589 SigSpec sig
= sigmap(conn
.second
);
591 if (bvmode
|| GetSize(w
) == 1) {
592 hier
.push_back(stringf(" (= %s (|%s_n %s| %s)) ; %s.%s\n", (GetSize(w
) > 1 ? get_bv(sig
) : get_bool(sig
)).c_str(),
593 get_id(cell
->type
), get_id(w
), cell_state
.c_str(), get_id(cell
->type
), get_id(w
)));
595 for (int i
= 0; i
< GetSize(w
); i
++)
596 hier
.push_back(stringf(" (= %s (|%s_n %s %d| %s)) ; %s.%s[%d]\n", get_bool(sig
[i
]).c_str(),
597 get_id(cell
->type
), get_id(w
), i
, cell_state
.c_str(), get_id(cell
->type
), get_id(w
), i
));
604 log_error("Unsupported cell type %s for cell %s.%s. (Maybe this cell type would be supported in -bv or -mem mode?)\n",
605 log_id(cell
->type
), log_id(module
), log_id(cell
));
610 if (verbose
) log("=> export logic driving outputs\n");
612 pool
<SigBit
> reg_bits
;
613 for (auto cell
: module
->cells())
614 if (cell
->type
.in("$_DFF_P_", "$_DFF_N_", "$dff")) {
615 // not using sigmap -- we want the net directly at the dff output
616 for (auto bit
: cell
->getPort("\\Q"))
617 reg_bits
.insert(bit
);
620 for (auto wire
: module
->wires()) {
621 bool is_register
= false;
622 for (auto bit
: SigSpec(wire
))
623 if (reg_bits
.count(bit
))
625 if (wire
->port_id
|| is_register
|| wire
->get_bool_attribute("\\keep") || (wiresmode
&& wire
->name
[0] == '\\')) {
626 RTLIL::SigSpec sig
= sigmap(wire
);
627 if (wire
->port_input
)
628 decls
.push_back(stringf("; yosys-smt2-input %s %d\n", get_id(wire
), wire
->width
));
629 if (wire
->port_output
)
630 decls
.push_back(stringf("; yosys-smt2-output %s %d\n", get_id(wire
), wire
->width
));
632 decls
.push_back(stringf("; yosys-smt2-register %s %d\n", get_id(wire
), wire
->width
));
633 if (wire
->get_bool_attribute("\\keep") || (wiresmode
&& wire
->name
[0] == '\\'))
634 decls
.push_back(stringf("; yosys-smt2-wire %s %d\n", get_id(wire
), wire
->width
));
635 if (bvmode
&& GetSize(sig
) > 1) {
636 decls
.push_back(stringf("(define-fun |%s_n %s| ((state |%s_s|)) (_ BitVec %d) %s)\n",
637 get_id(module
), get_id(wire
), get_id(module
), GetSize(sig
), get_bv(sig
).c_str()));
639 for (int i
= 0; i
< GetSize(sig
); i
++)
640 if (GetSize(sig
) > 1)
641 decls
.push_back(stringf("(define-fun |%s_n %s %d| ((state |%s_s|)) Bool %s)\n",
642 get_id(module
), get_id(wire
), i
, get_id(module
), get_bool(sig
[i
]).c_str()));
644 decls
.push_back(stringf("(define-fun |%s_n %s| ((state |%s_s|)) Bool %s)\n",
645 get_id(module
), get_id(wire
), get_id(module
), get_bool(sig
[i
]).c_str()));
650 if (verbose
) log("=> export logic associated with the initial state\n");
652 vector
<string
> init_list
;
653 for (auto wire
: module
->wires())
654 if (wire
->attributes
.count("\\init")) {
655 RTLIL::SigSpec sig
= sigmap(wire
);
656 Const val
= wire
->attributes
.at("\\init");
657 val
.bits
.resize(GetSize(sig
));
658 if (bvmode
&& GetSize(sig
) > 1) {
659 init_list
.push_back(stringf("(= %s #b%s) ; %s", get_bv(sig
).c_str(), val
.as_string().c_str(), get_id(wire
)));
661 for (int i
= 0; i
< GetSize(sig
); i
++)
662 init_list
.push_back(stringf("(= %s %s) ; %s", get_bool(sig
[i
]).c_str(), val
.bits
[i
] == State::S1
? "true" : "false", get_id(wire
)));
666 if (verbose
) log("=> export logic driving asserts\n");
668 vector
<string
> assert_list
, assume_list
;
669 for (auto cell
: module
->cells())
670 if (cell
->type
.in("$assert", "$assume")) {
671 string name_a
= get_bool(cell
->getPort("\\A"));
672 string name_en
= get_bool(cell
->getPort("\\EN"));
673 decls
.push_back(stringf("; yosys-smt2-%s %s#%d %s\n", cell
->type
.c_str() + 1, get_id(module
), idcounter
,
674 cell
->attributes
.count("\\src") ? cell
->attributes
.at("\\src").decode_string().c_str() : get_id(cell
)));
675 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool (or %s (not %s))) ; %s\n",
676 get_id(module
), idcounter
, get_id(module
), name_a
.c_str(), name_en
.c_str(), get_id(cell
)));
677 if (cell
->type
== "$assert")
678 assert_list
.push_back(stringf("(|%s#%d| state)", get_id(module
), idcounter
++));
680 assume_list
.push_back(stringf("(|%s#%d| state)", get_id(module
), idcounter
++));
683 for (int iter
= 1; !registers
.empty(); iter
++)
685 pool
<Cell
*> this_regs
;
686 this_regs
.swap(registers
);
688 if (verbose
) log("=> export logic driving registers [iteration %d]\n", iter
);
690 for (auto cell
: this_regs
)
692 if (cell
->type
== "$_DFF_P_" || cell
->type
== "$_DFF_N_")
694 std::string expr_d
= get_bool(cell
->getPort("\\D"));
695 std::string expr_q
= get_bool(cell
->getPort("\\Q"), "next_state");
696 trans
.push_back(stringf(" (= %s %s) ; %s %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
), log_signal(cell
->getPort("\\Q"))));
699 if (cell
->type
== "$dff")
701 std::string expr_d
= get_bv(cell
->getPort("\\D"));
702 std::string expr_q
= get_bv(cell
->getPort("\\Q"), "next_state");
703 trans
.push_back(stringf(" (= %s %s) ; %s %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
), log_signal(cell
->getPort("\\Q"))));
706 if (cell
->type
== "$anyconst")
708 std::string expr_d
= get_bv(cell
->getPort("\\Y"));
709 std::string expr_q
= get_bv(cell
->getPort("\\Y"), "next_state");
710 trans
.push_back(stringf(" (= %s %s) ; %s %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
), log_signal(cell
->getPort("\\Y"))));
713 if (cell
->type
== "$mem")
715 int arrayid
= memarrays
.at(cell
);
717 int abits
= cell
->getParam("\\ABITS").as_int();
718 int width
= cell
->getParam("\\WIDTH").as_int();
719 int wr_ports
= cell
->getParam("\\WR_PORTS").as_int();
721 for (int i
= 0; i
< wr_ports
; i
++)
723 std::string addr
= get_bv(cell
->getPort("\\WR_ADDR").extract(abits
*i
, abits
));
724 std::string data
= get_bv(cell
->getPort("\\WR_DATA").extract(width
*i
, width
));
725 std::string mask
= get_bv(cell
->getPort("\\WR_EN").extract(width
*i
, width
));
727 data
= stringf("(bvor (bvand %s %s) (bvand (select (|%s#%d#%d| state) %s) (bvnot %s)))",
728 data
.c_str(), mask
.c_str(), get_id(module
), arrayid
, i
, addr
.c_str(), mask
.c_str());
730 decls
.push_back(stringf("(define-fun |%s#%d#%d| ((state |%s_s|)) (Array (_ BitVec %d) (_ BitVec %d)) "
731 "(store (|%s#%d#%d| state) %s %s)) ; %s\n",
732 get_id(module
), arrayid
, i
+1, get_id(module
), abits
, width
,
733 get_id(module
), arrayid
, i
, addr
.c_str(), data
.c_str(), get_id(cell
)));
736 std::string expr_d
= stringf("(|%s#%d#%d| state)", get_id(module
), arrayid
, wr_ports
);
737 std::string expr_q
= stringf("(|%s#%d#0| next_state)", get_id(module
), arrayid
);
738 trans
.push_back(stringf(" (= %s %s) ; %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
)));
740 Const init_data
= cell
->getParam("\\INIT");
741 int memsize
= cell
->getParam("\\SIZE").as_int();
743 for (int i
= 0; i
< memsize
; i
++)
745 if (GetSize(init_data
) < i
*width
)
748 Const initword
= init_data
.extract(i
*width
, width
, State::Sx
);
749 bool gen_init_constr
= false;
751 for (auto bit
: initword
.bits
)
752 if (bit
== State::S0
|| bit
== State::S1
)
753 gen_init_constr
= true;
755 init_list
.push_back(stringf("(= (select (|%s#%d#0| state) #b%s) #b%s) ; %s[%d]",
756 get_id(module
), arrayid
, Const(i
, abits
).as_string().c_str(),
757 initword
.as_string().c_str(), get_id(cell
), i
));
763 for (auto c
: hiercells
) {
764 assert_list
.push_back(stringf("(|%s_a| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
765 assume_list
.push_back(stringf("(|%s_u| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
766 init_list
.push_back(stringf("(|%s_i| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
767 hier
.push_back(stringf(" (|%s_h| (|%s_h %s| state))\n", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
768 trans
.push_back(stringf(" (|%s_t| (|%s_h %s| state) (|%s_h %s| next_state))\n",
769 get_id(c
->type
), get_id(module
), get_id(c
->name
), get_id(module
), get_id(c
->name
)));
772 string assert_expr
= assert_list
.empty() ? "true" : "(and";
773 if (!assert_list
.empty()) {
774 if (GetSize(assert_list
) == 1) {
775 assert_expr
= "\n " + assert_list
.front() + "\n";
777 for (auto &str
: assert_list
)
778 assert_expr
+= stringf("\n %s", str
.c_str());
779 assert_expr
+= "\n)";
782 decls
.push_back(stringf("(define-fun |%s_a| ((state |%s_s|)) Bool %s)\n",
783 get_id(module
), get_id(module
), assert_expr
.c_str()));
785 string assume_expr
= assume_list
.empty() ? "true" : "(and";
786 if (!assume_list
.empty()) {
787 if (GetSize(assume_list
) == 1) {
788 assume_expr
= "\n " + assume_list
.front() + "\n";
790 for (auto &str
: assume_list
)
791 assume_expr
+= stringf("\n %s", str
.c_str());
792 assume_expr
+= "\n)";
795 decls
.push_back(stringf("(define-fun |%s_u| ((state |%s_s|)) Bool %s)\n",
796 get_id(module
), get_id(module
), assume_expr
.c_str()));
798 string init_expr
= init_list
.empty() ? "true" : "(and";
799 if (!init_list
.empty()) {
800 if (GetSize(init_list
) == 1) {
801 init_expr
= "\n " + init_list
.front() + "\n";
803 for (auto &str
: init_list
)
804 init_expr
+= stringf("\n %s", str
.c_str());
808 decls
.push_back(stringf("(define-fun |%s_i| ((state |%s_s|)) Bool %s)\n",
809 get_id(module
), get_id(module
), init_expr
.c_str()));
812 void write(std::ostream
&f
)
814 f
<< stringf("; yosys-smt2-module %s\n", get_id(module
));
816 for (auto it
: decls
)
819 f
<< stringf("(define-fun |%s_h| ((state |%s_s|)) Bool ", get_id(module
), get_id(module
));
820 if (GetSize(hier
) > 1) {
826 if (GetSize(hier
) == 1)
827 f
<< "\n" + hier
.front() + ")\n";
831 f
<< stringf("(define-fun |%s_t| ((state |%s_s|) (next_state |%s_s|)) Bool ", get_id(module
), get_id(module
), get_id(module
));
832 if (GetSize(trans
) > 1) {
834 for (auto it
: trans
)
838 if (GetSize(trans
) == 1)
839 f
<< "\n" + trans
.front() + ")";
842 f
<< stringf(" ; end of module %s\n", get_id(module
));
846 struct Smt2Backend
: public Backend
{
847 Smt2Backend() : Backend("smt2", "write design to SMT-LIBv2 file") { }
850 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
852 log(" write_smt2 [options] [filename]\n");
854 log("Write a SMT-LIBv2 [1] description of the current design. For a module with name\n");
855 log("'<mod>' this will declare the sort '<mod>_s' (state of the module) and the\n");
856 log("functions operating on that state.\n");
858 log("The '<mod>_s' sort represents a module state. Additional '<mod>_n' functions\n");
859 log("are provided that can be used to access the values of the signals in the module.\n");
860 log("By default only ports, registers, and wires with the 'keep' attribute set are\n");
861 log("made available via such functions. With the -nobv option, multi-bit wires are\n");
862 log("exported as separate functions of type Bool for the individual bits. Without\n");
863 log("-nobv multi-bit wires are exported as single functions of type BitVec.\n");
865 log("The '<mod>_t' function evaluates to 'true' when the given pair of states\n");
866 log("describes a valid state transition.\n");
868 log("The '<mod>_a' function evaluates to 'true' when the given state satisfies\n");
869 log("the asserts in the module.\n");
871 log("The '<mod>_u' function evaluates to 'true' when the given state satisfies\n");
872 log("the assumptions in the module.\n");
874 log("The '<mod>_i' function evaluates to 'true' when the given state conforms\n");
875 log("to the initial state. Furthermore the '<mod>_is' function should be asserted\n");
876 log("to be true for initial states in addition to '<mod>_i', and should be\n");
877 log("asserted to be false for non-initial states.\n");
879 log("For hierarchical designs, the '<mod>_h' function must be asserted for each\n");
880 log("state to establish the design hierarchy. The '<mod>_h <cellname>' function\n");
881 log("evaluates to the state corresponding to the given cell within <mod>.\n");
884 log(" this will print the recursive walk used to export the modules.\n");
887 log(" disable support for BitVec (FixedSizeBitVectors theory). without this\n");
888 log(" option multi-bit wires are represented using the BitVec sort and\n");
889 log(" support for coarse grain cells (incl. arithmetic) is enabled.\n");
892 log(" disable support for memories (via ArraysEx theory). this option is\n");
893 log(" implied by -nobv. only $mem cells without merged registers in\n");
894 log(" read ports are supported. call \"memory\" with -nordff to make sure\n");
895 log(" that no registers are merged into $mem read ports. '<mod>_m' functions\n");
896 log(" will be generated for accessing the arrays that are used to represent\n");
900 log(" create '<mod>_n' functions for all public wires. by default only ports,\n");
901 log(" registers, and wires with the 'keep' attribute are exported.\n");
903 log(" -tpl <template_file>\n");
904 log(" use the given template file. the line containing only the token '%%%%'\n");
905 log(" is replaced with the regular output of this command.\n");
907 log("[1] For more information on SMT-LIBv2 visit http://smt-lib.org/ or read David\n");
908 log("R. Cok's tutorial: http://www.grammatech.com/resources/smt/SMTLIBTutorial.pdf\n");
910 log("---------------------------------------------------------------------------\n");
914 log("Consider the following module (test.v). We want to prove that the output can\n");
915 log("never transition from a non-zero value to a zero value.\n");
917 log(" module test(input clk, output reg [3:0] y);\n");
918 log(" always @(posedge clk)\n");
919 log(" y <= (y << 1) | ^y;\n");
922 log("For this proof we create the following template (test.tpl).\n");
924 log(" ; we need QF_UFBV for this poof\n");
925 log(" (set-logic QF_UFBV)\n");
927 log(" ; insert the auto-generated code here\n");
930 log(" ; declare two state variables s1 and s2\n");
931 log(" (declare-fun s1 () test_s)\n");
932 log(" (declare-fun s2 () test_s)\n");
934 log(" ; state s2 is the successor of state s1\n");
935 log(" (assert (test_t s1 s2))\n");
937 log(" ; we are looking for a model with y non-zero in s1\n");
938 log(" (assert (distinct (|test_n y| s1) #b0000))\n");
940 log(" ; we are looking for a model with y zero in s2\n");
941 log(" (assert (= (|test_n y| s2) #b0000))\n");
943 log(" ; is there such a model?\n");
944 log(" (check-sat)\n");
946 log("The following yosys script will create a 'test.smt2' file for our proof:\n");
948 log(" read_verilog test.v\n");
949 log(" hierarchy -check; proc; opt; check -assert\n");
950 log(" write_smt2 -bv -tpl test.tpl test.smt2\n");
952 log("Running 'cvc4 test.smt2' will print 'unsat' because y can never transition\n");
953 log("from non-zero to zero in the test design.\n");
956 virtual void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
)
958 std::ifstream template_f
;
959 bool bvmode
= true, memmode
= true, wiresmode
= false, verbose
= false;
961 log_header(design
, "Executing SMT2 backend.\n");
964 for (argidx
= 1; argidx
< args
.size(); argidx
++)
966 if (args
[argidx
] == "-tpl" && argidx
+1 < args
.size()) {
967 template_f
.open(args
[++argidx
]);
968 if (template_f
.fail())
969 log_error("Can't open template file `%s'.\n", args
[argidx
].c_str());
972 if (args
[argidx
] == "-bv" || args
[argidx
] == "-mem") {
973 log_warning("Options -bv and -mem are now the default. Support for -bv and -mem will be removed in the future.\n");
976 if (args
[argidx
] == "-nobv") {
981 if (args
[argidx
] == "-nomem") {
985 if (args
[argidx
] == "-wires") {
989 if (args
[argidx
] == "-verbose") {
995 extra_args(f
, filename
, args
, argidx
);
997 if (template_f
.is_open()) {
999 while (std::getline(template_f
, line
)) {
1001 while (indent
< GetSize(line
) && (line
[indent
] == ' ' || line
[indent
] == '\t'))
1003 if (line
.substr(indent
, 2) == "%%")
1005 *f
<< line
<< std::endl
;
1009 *f
<< stringf("; SMT-LIBv2 description generated by %s\n", yosys_version_str
);
1011 std::vector
<RTLIL::Module
*> sorted_modules
;
1013 // extract module dependencies
1014 std::map
<RTLIL::Module
*, std::set
<RTLIL::Module
*>> module_deps
;
1015 for (auto &mod_it
: design
->modules_
) {
1016 module_deps
[mod_it
.second
] = std::set
<RTLIL::Module
*>();
1017 for (auto &cell_it
: mod_it
.second
->cells_
)
1018 if (design
->modules_
.count(cell_it
.second
->type
) > 0)
1019 module_deps
[mod_it
.second
].insert(design
->modules_
.at(cell_it
.second
->type
));
1022 // simple good-enough topological sort
1023 // (O(n*m) on n elements and depth m)
1024 while (module_deps
.size() > 0) {
1025 size_t sorted_modules_idx
= sorted_modules
.size();
1026 for (auto &it
: module_deps
) {
1027 for (auto &dep
: it
.second
)
1028 if (module_deps
.count(dep
) > 0)
1030 // log("Next in topological sort: %s\n", RTLIL::id2cstr(it.first->name));
1031 sorted_modules
.push_back(it
.first
);
1034 if (sorted_modules_idx
== sorted_modules
.size())
1035 log_error("Cyclic dependency between modules found! Cycle includes module %s.\n", RTLIL::id2cstr(module_deps
.begin()->first
->name
));
1036 while (sorted_modules_idx
< sorted_modules
.size())
1037 module_deps
.erase(sorted_modules
.at(sorted_modules_idx
++));
1040 Module
*topmod
= design
->top_module();
1041 std::string topmod_id
;
1043 for (auto module
: sorted_modules
)
1045 if (module
->get_bool_attribute("\\blackbox") || module
->has_memories_warn() || module
->has_processes_warn())
1048 log("Creating SMT-LIBv2 representation of module %s.\n", log_id(module
));
1050 Smt2Worker
worker(module
, bvmode
, memmode
, wiresmode
, verbose
);
1054 if (module
== topmod
)
1055 topmod_id
= worker
.get_id(module
);
1059 *f
<< stringf("; yosys-smt2-topmod %s\n", topmod_id
.c_str());
1061 *f
<< stringf("; end of yosys output\n");
1063 if (template_f
.is_open()) {
1065 while (std::getline(template_f
, line
))
1066 *f
<< line
<< std::endl
;
1071 PRIVATE_NAMESPACE_END