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
, statebv
, statedt
, forallmode
;
36 dict
<IdString
, int> &mod_stbv_width
;
37 int idcounter
= 0, statebv_width
= 0;
39 std::vector
<std::string
> decls
, trans
, hier
, dtmembers
;
40 std::map
<RTLIL::SigBit
, RTLIL::Cell
*> bit_driver
;
41 std::set
<RTLIL::Cell
*> exported_cells
, hiercells
, hiercells_queue
;
42 pool
<Cell
*> recursive_cells
, registers
;
44 pool
<SigBit
> clock_posedge
, clock_negedge
;
45 vector
<string
> ex_state_eq
, ex_input_eq
;
47 std::map
<RTLIL::SigBit
, std::pair
<int, int>> fcache
;
48 std::map
<Cell
*, int> memarrays
;
49 std::map
<int, int> bvsizes
;
50 dict
<IdString
, char*> ids
;
52 const char *get_id(IdString n
)
54 if (ids
.count(n
) == 0) {
55 std::string str
= log_id(n
);
56 for (int i
= 0; i
< GetSize(str
); i
++) {
60 ids
[n
] = strdup(str
.c_str());
66 const char *get_id(T
*obj
) {
67 return get_id(obj
->name
);
70 void makebits(std::string name
, int width
= 0, std::string comment
= std::string())
77 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
);
80 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
);
81 statebv_width
+= width
;
87 decl_str
= stringf(" (|%s| Bool)", name
.c_str());
89 decl_str
= stringf(" (|%s| (_ BitVec %d))", name
.c_str(), width
);
95 decl_str
= stringf("(declare-fun |%s| (|%s_s|) Bool)", name
.c_str(), get_id(module
));
97 decl_str
= stringf("(declare-fun |%s| (|%s_s|) (_ BitVec %d))", name
.c_str(), get_id(module
), width
);
101 if (!comment
.empty())
102 decl_str
+= " ; " + comment
;
105 dtmembers
.push_back(decl_str
+ "\n");
107 decls
.push_back(decl_str
+ "\n");
110 Smt2Worker(RTLIL::Module
*module
, bool bvmode
, bool memmode
, bool wiresmode
, bool verbose
, bool statebv
, bool statedt
, bool forallmode
,
111 dict
<IdString
, int> &mod_stbv_width
, dict
<IdString
, dict
<IdString
, pair
<bool, bool>>> &mod_clk_cache
) :
112 ct(module
->design
), sigmap(module
), module(module
), bvmode(bvmode
), memmode(memmode
), wiresmode(wiresmode
),
113 verbose(verbose
), statebv(statebv
), statedt(statedt
), forallmode(forallmode
), mod_stbv_width(mod_stbv_width
)
115 pool
<SigBit
> noclock
;
117 makebits(stringf("%s_is", get_id(module
)));
119 for (auto cell
: module
->cells())
120 for (auto &conn
: cell
->connections())
122 if (GetSize(conn
.second
) == 0)
125 bool is_input
= ct
.cell_input(cell
->type
, conn
.first
);
126 bool is_output
= ct
.cell_output(cell
->type
, conn
.first
);
128 if (is_output
&& !is_input
)
129 for (auto bit
: sigmap(conn
.second
)) {
130 if (bit_driver
.count(bit
))
131 log_error("Found multiple drivers for %s.\n", log_signal(bit
));
132 bit_driver
[bit
] = cell
;
134 else if (is_output
|| !is_input
)
135 log_error("Unsupported or unknown directionality on port %s of cell %s.%s (%s).\n",
136 log_id(conn
.first
), log_id(module
), log_id(cell
), log_id(cell
->type
));
138 if (cell
->type
.in("$dff", "$_DFF_P_", "$_DFF_N_") && conn
.first
.in("\\CLK", "\\C"))
140 bool posedge
= (cell
->type
== "$_DFF_N_") || (cell
->type
== "$dff" && cell
->getParam("\\CLK_POLARITY").as_bool());
141 for (auto bit
: sigmap(conn
.second
)) {
143 clock_posedge
.insert(bit
);
145 clock_negedge
.insert(bit
);
149 if (mod_clk_cache
.count(cell
->type
) && mod_clk_cache
.at(cell
->type
).count(conn
.first
))
151 for (auto bit
: sigmap(conn
.second
)) {
152 if (mod_clk_cache
.at(cell
->type
).at(conn
.first
).first
)
153 clock_posedge
.insert(bit
);
154 if (mod_clk_cache
.at(cell
->type
).at(conn
.first
).second
)
155 clock_negedge
.insert(bit
);
160 for (auto bit
: sigmap(conn
.second
))
165 for (auto bit
: noclock
) {
166 clock_posedge
.erase(bit
);
167 clock_negedge
.erase(bit
);
170 for (auto wire
: module
->wires())
172 if (!wire
->port_input
|| GetSize(wire
) != 1)
174 SigBit bit
= sigmap(wire
);
175 if (clock_posedge
.count(bit
))
176 mod_clk_cache
[module
->name
][wire
->name
].first
= true;
177 if (clock_negedge
.count(bit
))
178 mod_clk_cache
[module
->name
][wire
->name
].second
= true;
189 const char *get_id(Module
*m
)
191 return get_id(m
->name
);
194 const char *get_id(Cell
*c
)
196 return get_id(c
->name
);
199 const char *get_id(Wire
*w
)
201 return get_id(w
->name
);
204 void register_bool(RTLIL::SigBit bit
, int id
)
206 if (verbose
) log("%*s-> register_bool: %s %d\n", 2+2*GetSize(recursive_cells
), "",
207 log_signal(bit
), id
);
210 log_assert(fcache
.count(bit
) == 0);
211 fcache
[bit
] = std::pair
<int, int>(id
, -1);
214 void register_bv(RTLIL::SigSpec sig
, int id
)
216 if (verbose
) log("%*s-> register_bv: %s %d\n", 2+2*GetSize(recursive_cells
), "",
217 log_signal(sig
), id
);
222 log_assert(bvsizes
.count(id
) == 0);
223 bvsizes
[id
] = GetSize(sig
);
225 for (int i
= 0; i
< GetSize(sig
); i
++) {
226 log_assert(fcache
.count(sig
[i
]) == 0);
227 fcache
[sig
[i
]] = std::pair
<int, int>(id
, i
);
231 void register_boolvec(RTLIL::SigSpec sig
, int id
)
233 if (verbose
) log("%*s-> register_boolvec: %s %d\n", 2+2*GetSize(recursive_cells
), "",
234 log_signal(sig
), id
);
238 register_bool(sig
[0], id
);
240 for (int i
= 1; i
< GetSize(sig
); i
++)
241 sigmap
.add(sig
[i
], RTLIL::State::S0
);
244 std::string
get_bool(RTLIL::SigBit bit
, const char *state_name
= "state")
248 if (bit
.wire
== nullptr)
249 return bit
== RTLIL::State::S1
? "true" : "false";
251 if (bit_driver
.count(bit
))
252 export_cell(bit_driver
.at(bit
));
255 if (fcache
.count(bit
) == 0) {
256 if (verbose
) log("%*s-> external bool: %s\n", 2+2*GetSize(recursive_cells
), "",
258 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(bit
));
259 register_bool(bit
, idcounter
++);
262 auto f
= fcache
.at(bit
);
264 return stringf("(= ((_ extract %d %d) (|%s#%d| %s)) #b1)", f
.second
, f
.second
, get_id(module
), f
.first
, state_name
);
265 return stringf("(|%s#%d| %s)", get_id(module
), f
.first
, state_name
);
268 std::string
get_bool(RTLIL::SigSpec sig
, const char *state_name
= "state")
270 return get_bool(sig
.as_bit(), state_name
);
273 std::string
get_bv(RTLIL::SigSpec sig
, const char *state_name
= "state")
278 std::vector
<std::string
> subexpr
;
281 while (orig_sig
!= sig
) {
283 if (bit_driver
.count(bit
))
284 export_cell(bit_driver
.at(bit
));
289 for (int i
= 0, j
= 1; i
< GetSize(sig
); i
+= j
, j
= 1)
291 if (sig
[i
].wire
== nullptr) {
292 while (i
+j
< GetSize(sig
) && sig
[i
+j
].wire
== nullptr) j
++;
293 subexpr
.push_back("#b");
294 for (int k
= i
+j
-1; k
>= i
; k
--)
295 subexpr
.back() += sig
[k
] == RTLIL::State::S1
? "1" : "0";
299 if (fcache
.count(sig
[i
]) && fcache
.at(sig
[i
]).second
== -1) {
300 subexpr
.push_back(stringf("(ite %s #b1 #b0)", get_bool(sig
[i
], state_name
).c_str()));
304 if (fcache
.count(sig
[i
])) {
305 auto t1
= fcache
.at(sig
[i
]);
306 while (i
+j
< GetSize(sig
)) {
307 if (fcache
.count(sig
[i
+j
]) == 0)
309 auto t2
= fcache
.at(sig
[i
+j
]);
310 if (t1
.first
!= t2
.first
)
312 if (t1
.second
+j
!= t2
.second
)
316 if (t1
.second
== 0 && j
== bvsizes
.at(t1
.first
))
317 subexpr
.push_back(stringf("(|%s#%d| %s)", get_id(module
), t1
.first
, state_name
));
319 subexpr
.push_back(stringf("((_ extract %d %d) (|%s#%d| %s))",
320 t1
.second
+ j
- 1, t1
.second
, get_id(module
), t1
.first
, state_name
));
324 std::set
<RTLIL::SigBit
> seen_bits
= { sig
[i
] };
325 while (i
+j
< GetSize(sig
) && sig
[i
+j
].wire
&& !fcache
.count(sig
[i
+j
]) && !seen_bits
.count(sig
[i
+j
]))
326 seen_bits
.insert(sig
[i
+j
]), j
++;
328 if (verbose
) log("%*s-> external bv: %s\n", 2+2*GetSize(recursive_cells
), "",
329 log_signal(sig
.extract(i
, j
)));
330 for (auto bit
: sig
.extract(i
, j
))
331 log_assert(bit_driver
.count(bit
) == 0);
332 makebits(stringf("%s#%d", get_id(module
), idcounter
), j
, log_signal(sig
.extract(i
, j
)));
333 subexpr
.push_back(stringf("(|%s#%d| %s)", get_id(module
), idcounter
, state_name
));
334 register_bv(sig
.extract(i
, j
), idcounter
++);
337 if (GetSize(subexpr
) > 1) {
338 std::string expr
= "", end_str
= "";
339 for (int i
= GetSize(subexpr
)-1; i
>= 0; i
--) {
340 if (i
> 0) expr
+= " (concat", end_str
+= ")";
341 expr
+= " " + subexpr
[i
];
343 return expr
.substr(1) + end_str
;
345 log_assert(GetSize(subexpr
) == 1);
350 void export_gate(RTLIL::Cell
*cell
, std::string expr
)
352 RTLIL::SigBit bit
= sigmap(cell
->getPort("\\Y").as_bit());
353 std::string processed_expr
;
355 for (char ch
: expr
) {
356 if (ch
== 'A') processed_expr
+= get_bool(cell
->getPort("\\A"));
357 else if (ch
== 'B') processed_expr
+= get_bool(cell
->getPort("\\B"));
358 else if (ch
== 'C') processed_expr
+= get_bool(cell
->getPort("\\C"));
359 else if (ch
== 'D') processed_expr
+= get_bool(cell
->getPort("\\D"));
360 else if (ch
== 'S') processed_expr
+= get_bool(cell
->getPort("\\S"));
361 else processed_expr
+= ch
;
365 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
367 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
368 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(bit
)));
369 register_bool(bit
, idcounter
++);
370 recursive_cells
.erase(cell
);
373 void export_bvop(RTLIL::Cell
*cell
, std::string expr
, char type
= 0)
375 RTLIL::SigSpec sig_a
, sig_b
;
376 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
377 bool is_signed
= cell
->getParam("\\A_SIGNED").as_bool();
378 int width
= GetSize(sig_y
);
380 if (type
== 's' || type
== 'd' || type
== 'b') {
381 width
= max(width
, GetSize(cell
->getPort("\\A")));
382 if (cell
->hasPort("\\B"))
383 width
= max(width
, GetSize(cell
->getPort("\\B")));
386 if (cell
->hasPort("\\A")) {
387 sig_a
= cell
->getPort("\\A");
388 sig_a
.extend_u0(width
, is_signed
);
391 if (cell
->hasPort("\\B")) {
392 sig_b
= cell
->getPort("\\B");
393 sig_b
.extend_u0(width
, is_signed
&& !(type
== 's'));
396 std::string processed_expr
;
398 for (char ch
: expr
) {
399 if (ch
== 'A') processed_expr
+= get_bv(sig_a
);
400 else if (ch
== 'B') processed_expr
+= get_bv(sig_b
);
401 else if (ch
== 'L') processed_expr
+= is_signed
? "a" : "l";
402 else if (ch
== 'U') processed_expr
+= is_signed
? "s" : "u";
403 else processed_expr
+= ch
;
406 if (width
!= GetSize(sig_y
) && type
!= 'b')
407 processed_expr
= stringf("((_ extract %d 0) %s)", GetSize(sig_y
)-1, processed_expr
.c_str());
410 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
413 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
414 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(sig_y
)));
415 register_boolvec(sig_y
, idcounter
++);
417 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
418 get_id(module
), idcounter
, get_id(module
), GetSize(sig_y
), processed_expr
.c_str(), log_signal(sig_y
)));
419 register_bv(sig_y
, idcounter
++);
422 recursive_cells
.erase(cell
);
425 void export_reduce(RTLIL::Cell
*cell
, std::string expr
, bool identity_val
)
427 RTLIL::SigSpec sig_y
= sigmap(cell
->getPort("\\Y"));
428 std::string processed_expr
;
431 if (ch
== 'A' || ch
== 'B') {
432 RTLIL::SigSpec sig
= sigmap(cell
->getPort(stringf("\\%c", ch
)));
434 processed_expr
+= " " + get_bool(bit
);
435 if (GetSize(sig
) == 1)
436 processed_expr
+= identity_val
? " true" : " false";
438 processed_expr
+= ch
;
441 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
443 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool %s) ; %s\n",
444 get_id(module
), idcounter
, get_id(module
), processed_expr
.c_str(), log_signal(sig_y
)));
445 register_boolvec(sig_y
, idcounter
++);
446 recursive_cells
.erase(cell
);
449 void export_cell(RTLIL::Cell
*cell
)
452 log("%*s=> export_cell %s (%s) [%s]\n", 2+2*GetSize(recursive_cells
), "",
453 log_id(cell
), log_id(cell
->type
), exported_cells
.count(cell
) ? "old" : "new");
455 if (recursive_cells
.count(cell
))
456 log_error("Found logic loop in module %s! See cell %s.\n", get_id(module
), get_id(cell
));
458 if (exported_cells
.count(cell
))
461 exported_cells
.insert(cell
);
462 recursive_cells
.insert(cell
);
464 if (cell
->type
== "$initstate")
466 SigBit bit
= sigmap(cell
->getPort("\\Y").as_bit());
467 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) Bool (|%s_is| state)) ; %s\n",
468 get_id(module
), idcounter
, get_id(module
), get_id(module
), log_signal(bit
)));
469 register_bool(bit
, idcounter
++);
470 recursive_cells
.erase(cell
);
474 if (cell
->type
.in("$_FF_", "$_DFF_P_", "$_DFF_N_"))
476 registers
.insert(cell
);
477 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(cell
->getPort("\\Q")));
478 register_bool(cell
->getPort("\\Q"), idcounter
++);
479 recursive_cells
.erase(cell
);
483 if (cell
->type
== "$_BUF_") return export_gate(cell
, "A");
484 if (cell
->type
== "$_NOT_") return export_gate(cell
, "(not A)");
485 if (cell
->type
== "$_AND_") return export_gate(cell
, "(and A B)");
486 if (cell
->type
== "$_NAND_") return export_gate(cell
, "(not (and A B))");
487 if (cell
->type
== "$_OR_") return export_gate(cell
, "(or A B)");
488 if (cell
->type
== "$_NOR_") return export_gate(cell
, "(not (or A B))");
489 if (cell
->type
== "$_XOR_") return export_gate(cell
, "(xor A B)");
490 if (cell
->type
== "$_XNOR_") return export_gate(cell
, "(not (xor A B))");
491 if (cell
->type
== "$_ANDNOT_") return export_gate(cell
, "(and A (not B))");
492 if (cell
->type
== "$_ORNOT_") return export_gate(cell
, "(or A (not B))");
493 if (cell
->type
== "$_MUX_") return export_gate(cell
, "(ite S B A)");
494 if (cell
->type
== "$_AOI3_") return export_gate(cell
, "(not (or (and A B) C))");
495 if (cell
->type
== "$_OAI3_") return export_gate(cell
, "(not (and (or A B) C))");
496 if (cell
->type
== "$_AOI4_") return export_gate(cell
, "(not (or (and A B) (and C D)))");
497 if (cell
->type
== "$_OAI4_") return export_gate(cell
, "(not (and (or A B) (or C D)))");
503 if (cell
->type
.in("$ff", "$dff"))
505 registers
.insert(cell
);
506 makebits(stringf("%s#%d", get_id(module
), idcounter
), GetSize(cell
->getPort("\\Q")), log_signal(cell
->getPort("\\Q")));
507 register_bv(cell
->getPort("\\Q"), idcounter
++);
508 recursive_cells
.erase(cell
);
512 if (cell
->type
.in("$anyconst", "$anyseq", "$allconst", "$allseq"))
514 registers
.insert(cell
);
515 string infostr
= cell
->attributes
.count("\\src") ? cell
->attributes
.at("\\src").decode_string().c_str() : get_id(cell
);
516 if (cell
->attributes
.count("\\reg"))
517 infostr
+= " " + cell
->attributes
.at("\\reg").decode_string();
518 decls
.push_back(stringf("; yosys-smt2-%s %s#%d %d %s\n", cell
->type
.c_str() + 1, get_id(module
), idcounter
, GetSize(cell
->getPort("\\Y")), infostr
.c_str()));
519 makebits(stringf("%s#%d", get_id(module
), idcounter
), GetSize(cell
->getPort("\\Y")), log_signal(cell
->getPort("\\Y")));
520 if (cell
->type
== "$anyseq")
521 ex_input_eq
.push_back(stringf(" (= (|%s#%d| state) (|%s#%d| other_state))", get_id(module
), idcounter
, get_id(module
), idcounter
));
522 register_bv(cell
->getPort("\\Y"), idcounter
++);
523 recursive_cells
.erase(cell
);
527 if (cell
->type
== "$and") return export_bvop(cell
, "(bvand A B)");
528 if (cell
->type
== "$or") return export_bvop(cell
, "(bvor A B)");
529 if (cell
->type
== "$xor") return export_bvop(cell
, "(bvxor A B)");
530 if (cell
->type
== "$xnor") return export_bvop(cell
, "(bvxnor A B)");
532 if (cell
->type
== "$shl") return export_bvop(cell
, "(bvshl A B)", 's');
533 if (cell
->type
== "$shr") return export_bvop(cell
, "(bvlshr A B)", 's');
534 if (cell
->type
== "$sshl") return export_bvop(cell
, "(bvshl A B)", 's');
535 if (cell
->type
== "$sshr") return export_bvop(cell
, "(bvLshr A B)", 's');
537 if (cell
->type
.in("$shift", "$shiftx")) {
538 if (cell
->getParam("\\B_SIGNED").as_bool()) {
541 return export_bvop(cell
, "(bvlshr A B)", 's');
545 if (cell
->type
== "$lt") return export_bvop(cell
, "(bvUlt A B)", 'b');
546 if (cell
->type
== "$le") return export_bvop(cell
, "(bvUle A B)", 'b');
547 if (cell
->type
== "$ge") return export_bvop(cell
, "(bvUge A B)", 'b');
548 if (cell
->type
== "$gt") return export_bvop(cell
, "(bvUgt A B)", 'b');
550 if (cell
->type
== "$ne") return export_bvop(cell
, "(distinct A B)", 'b');
551 if (cell
->type
== "$nex") return export_bvop(cell
, "(distinct A B)", 'b');
552 if (cell
->type
== "$eq") return export_bvop(cell
, "(= A B)", 'b');
553 if (cell
->type
== "$eqx") return export_bvop(cell
, "(= A B)", 'b');
555 if (cell
->type
== "$not") return export_bvop(cell
, "(bvnot A)");
556 if (cell
->type
== "$pos") return export_bvop(cell
, "A");
557 if (cell
->type
== "$neg") return export_bvop(cell
, "(bvneg A)");
559 if (cell
->type
== "$add") return export_bvop(cell
, "(bvadd A B)");
560 if (cell
->type
== "$sub") return export_bvop(cell
, "(bvsub A B)");
561 if (cell
->type
== "$mul") return export_bvop(cell
, "(bvmul A B)");
562 if (cell
->type
== "$div") return export_bvop(cell
, "(bvUdiv A B)", 'd');
563 if (cell
->type
== "$mod") return export_bvop(cell
, "(bvUrem A B)", 'd');
565 if (cell
->type
.in("$reduce_and", "$reduce_or", "$reduce_bool") &&
566 2*GetSize(cell
->getPort("\\A").chunks()) < GetSize(cell
->getPort("\\A"))) {
567 bool is_and
= cell
->type
== "$reduce_and";
568 string
bits(GetSize(cell
->getPort("\\A")), is_and
? '1' : '0');
569 return export_bvop(cell
, stringf("(%s A #b%s)", is_and
? "=" : "distinct", bits
.c_str()), 'b');
572 if (cell
->type
== "$reduce_and") return export_reduce(cell
, "(and A)", true);
573 if (cell
->type
== "$reduce_or") return export_reduce(cell
, "(or A)", false);
574 if (cell
->type
== "$reduce_xor") return export_reduce(cell
, "(xor A)", false);
575 if (cell
->type
== "$reduce_xnor") return export_reduce(cell
, "(not (xor A))", false);
576 if (cell
->type
== "$reduce_bool") return export_reduce(cell
, "(or A)", false);
578 if (cell
->type
== "$logic_not") return export_reduce(cell
, "(not (or A))", false);
579 if (cell
->type
== "$logic_and") return export_reduce(cell
, "(and (or A) (or B))", false);
580 if (cell
->type
== "$logic_or") return export_reduce(cell
, "(or A B)", false);
582 if (cell
->type
== "$mux" || cell
->type
== "$pmux")
584 int width
= GetSize(cell
->getPort("\\Y"));
585 std::string processed_expr
= get_bv(cell
->getPort("\\A"));
587 RTLIL::SigSpec sig_b
= cell
->getPort("\\B");
588 RTLIL::SigSpec sig_s
= cell
->getPort("\\S");
592 for (int i
= 0; i
< GetSize(sig_s
); i
++)
593 processed_expr
= stringf("(ite %s %s %s)", get_bool(sig_s
[i
]).c_str(),
594 get_bv(sig_b
.extract(i
*width
, width
)).c_str(), processed_expr
.c_str());
597 log("%*s-> import cell: %s\n", 2+2*GetSize(recursive_cells
), "", log_id(cell
));
599 RTLIL::SigSpec sig
= sigmap(cell
->getPort("\\Y"));
600 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
601 get_id(module
), idcounter
, get_id(module
), width
, processed_expr
.c_str(), log_signal(sig
)));
602 register_bv(sig
, idcounter
++);
603 recursive_cells
.erase(cell
);
607 // FIXME: $slice $concat
610 if (memmode
&& cell
->type
== "$mem")
612 int arrayid
= idcounter
++;
613 memarrays
[cell
] = arrayid
;
615 int abits
= cell
->getParam("\\ABITS").as_int();
616 int width
= cell
->getParam("\\WIDTH").as_int();
617 int rd_ports
= cell
->getParam("\\RD_PORTS").as_int();
618 int wr_ports
= cell
->getParam("\\WR_PORTS").as_int();
620 bool async_read
= false;
621 if (!cell
->getParam("\\WR_CLK_ENABLE").is_fully_ones()) {
622 if (!cell
->getParam("\\WR_CLK_ENABLE").is_fully_zero())
623 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
));
627 decls
.push_back(stringf("; yosys-smt2-memory %s %d %d %d %d %s\n", get_id(cell
), abits
, width
, rd_ports
, wr_ports
, async_read
? "async" : "sync"));
631 memstate
= stringf("%s#%d#final", get_id(module
), arrayid
);
633 memstate
= stringf("%s#%d#0", get_id(module
), arrayid
);
638 int mem_size
= cell
->getParam("\\SIZE").as_int();
639 int mem_offset
= cell
->getParam("\\OFFSET").as_int();
641 makebits(memstate
, width
*mem_size
, get_id(cell
));
642 decls
.push_back(stringf("(define-fun |%s_m %s| ((state |%s_s|)) (_ BitVec %d) (|%s| state))\n",
643 get_id(module
), get_id(cell
), get_id(module
), width
*mem_size
, memstate
.c_str()));
645 for (int i
= 0; i
< rd_ports
; i
++)
647 SigSpec addr_sig
= cell
->getPort("\\RD_ADDR").extract(abits
*i
, abits
);
648 SigSpec data_sig
= cell
->getPort("\\RD_DATA").extract(width
*i
, width
);
649 std::string addr
= get_bv(addr_sig
);
651 if (cell
->getParam("\\RD_CLK_ENABLE").extract(i
).as_bool())
652 log_error("Read port %d (%s) of memory %s.%s is clocked. This is not supported by \"write_smt2\"! "
653 "Call \"memory\" with -nordff to avoid this error.\n", i
, log_signal(data_sig
), log_id(cell
), log_id(module
));
655 decls
.push_back(stringf("(define-fun |%s_m:R%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
656 get_id(module
), i
, get_id(cell
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
658 std::string read_expr
= "#b";
659 for (int k
= 0; k
< width
; k
++)
662 for (int k
= 0; k
< mem_size
; k
++)
663 read_expr
= stringf("(ite (= (|%s_m:R%dA %s| state) #b%s) ((_ extract %d %d) (|%s| state))\n %s)",
664 get_id(module
), i
, get_id(cell
), Const(k
+mem_offset
, abits
).as_string().c_str(),
665 width
*(k
+1)-1, width
*k
, memstate
.c_str(), read_expr
.c_str());
667 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d)\n %s) ; %s\n",
668 get_id(module
), idcounter
, get_id(module
), width
, read_expr
.c_str(), log_signal(data_sig
)));
670 decls
.push_back(stringf("(define-fun |%s_m:R%dD %s| ((state |%s_s|)) (_ BitVec %d) (|%s#%d| state))\n",
671 get_id(module
), i
, get_id(cell
), get_id(module
), width
, get_id(module
), idcounter
));
673 register_bv(data_sig
, idcounter
++);
679 dtmembers
.push_back(stringf(" (|%s| (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
680 memstate
.c_str(), abits
, width
, get_id(cell
)));
682 decls
.push_back(stringf("(declare-fun |%s| (|%s_s|) (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
683 memstate
.c_str(), get_id(module
), abits
, width
, get_id(cell
)));
685 decls
.push_back(stringf("(define-fun |%s_m %s| ((state |%s_s|)) (Array (_ BitVec %d) (_ BitVec %d)) (|%s| state))\n",
686 get_id(module
), get_id(cell
), get_id(module
), abits
, width
, memstate
.c_str()));
688 for (int i
= 0; i
< rd_ports
; i
++)
690 SigSpec addr_sig
= cell
->getPort("\\RD_ADDR").extract(abits
*i
, abits
);
691 SigSpec data_sig
= cell
->getPort("\\RD_DATA").extract(width
*i
, width
);
692 std::string addr
= get_bv(addr_sig
);
694 if (cell
->getParam("\\RD_CLK_ENABLE").extract(i
).as_bool())
695 log_error("Read port %d (%s) of memory %s.%s is clocked. This is not supported by \"write_smt2\"! "
696 "Call \"memory\" with -nordff to avoid this error.\n", i
, log_signal(data_sig
), log_id(cell
), log_id(module
));
698 decls
.push_back(stringf("(define-fun |%s_m:R%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
699 get_id(module
), i
, get_id(cell
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
701 decls
.push_back(stringf("(define-fun |%s#%d| ((state |%s_s|)) (_ BitVec %d) (select (|%s| state) (|%s_m:R%dA %s| state))) ; %s\n",
702 get_id(module
), idcounter
, get_id(module
), width
, memstate
.c_str(), get_id(module
), i
, get_id(cell
), log_signal(data_sig
)));
704 decls
.push_back(stringf("(define-fun |%s_m:R%dD %s| ((state |%s_s|)) (_ BitVec %d) (|%s#%d| state))\n",
705 get_id(module
), i
, get_id(cell
), get_id(module
), width
, get_id(module
), idcounter
));
707 register_bv(data_sig
, idcounter
++);
711 registers
.insert(cell
);
712 recursive_cells
.erase(cell
);
716 Module
*m
= module
->design
->module(cell
->type
);
720 decls
.push_back(stringf("; yosys-smt2-cell %s %s\n", get_id(cell
->type
), get_id(cell
->name
)));
721 string cell_state
= stringf("(|%s_h %s| state)", get_id(module
), get_id(cell
->name
));
723 for (auto &conn
: cell
->connections())
725 if (GetSize(conn
.second
) == 0)
728 Wire
*w
= m
->wire(conn
.first
);
729 SigSpec sig
= sigmap(conn
.second
);
731 if (w
->port_output
&& !w
->port_input
) {
732 if (GetSize(w
) > 1) {
734 makebits(stringf("%s#%d", get_id(module
), idcounter
), GetSize(w
), log_signal(sig
));
735 register_bv(sig
, idcounter
++);
737 for (int i
= 0; i
< GetSize(w
); i
++) {
738 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(sig
[i
]));
739 register_bool(sig
[i
], idcounter
++);
743 makebits(stringf("%s#%d", get_id(module
), idcounter
), 0, log_signal(sig
));
744 register_bool(sig
, idcounter
++);
750 makebits(stringf("%s_h %s", get_id(module
), get_id(cell
->name
)), mod_stbv_width
.at(cell
->type
));
752 dtmembers
.push_back(stringf(" (|%s_h %s| |%s_s|)\n",
753 get_id(module
), get_id(cell
->name
), get_id(cell
->type
)));
755 decls
.push_back(stringf("(declare-fun |%s_h %s| (|%s_s|) |%s_s|)\n",
756 get_id(module
), get_id(cell
->name
), get_id(module
), get_id(cell
->type
)));
758 hiercells
.insert(cell
);
759 hiercells_queue
.insert(cell
);
760 recursive_cells
.erase(cell
);
764 log_error("Unsupported cell type %s for cell %s.%s.\n",
765 log_id(cell
->type
), log_id(module
), log_id(cell
));
770 if (verbose
) log("=> export logic driving outputs\n");
772 pool
<SigBit
> reg_bits
;
773 for (auto cell
: module
->cells())
774 if (cell
->type
.in("$ff", "$dff", "$_FF_", "$_DFF_P_", "$_DFF_N_")) {
775 // not using sigmap -- we want the net directly at the dff output
776 for (auto bit
: cell
->getPort("\\Q"))
777 reg_bits
.insert(bit
);
780 for (auto wire
: module
->wires()) {
781 bool is_register
= false;
782 for (auto bit
: SigSpec(wire
))
783 if (reg_bits
.count(bit
))
785 if (wire
->port_id
|| is_register
|| wire
->get_bool_attribute("\\keep") || (wiresmode
&& wire
->name
[0] == '\\')) {
786 RTLIL::SigSpec sig
= sigmap(wire
);
787 if (wire
->port_input
)
788 decls
.push_back(stringf("; yosys-smt2-input %s %d\n", get_id(wire
), wire
->width
));
789 if (wire
->port_output
)
790 decls
.push_back(stringf("; yosys-smt2-output %s %d\n", get_id(wire
), wire
->width
));
792 decls
.push_back(stringf("; yosys-smt2-register %s %d\n", get_id(wire
), wire
->width
));
793 if (wire
->get_bool_attribute("\\keep") || (wiresmode
&& wire
->name
[0] == '\\'))
794 decls
.push_back(stringf("; yosys-smt2-wire %s %d\n", get_id(wire
), wire
->width
));
795 if (GetSize(wire
) == 1 && (clock_posedge
.count(sig
) || clock_negedge
.count(sig
)))
796 decls
.push_back(stringf("; yosys-smt2-clock %s%s%s\n", get_id(wire
),
797 clock_posedge
.count(sig
) ? " posedge" : "", clock_negedge
.count(sig
) ? " negedge" : ""));
798 if (bvmode
&& GetSize(sig
) > 1) {
799 decls
.push_back(stringf("(define-fun |%s_n %s| ((state |%s_s|)) (_ BitVec %d) %s)\n",
800 get_id(module
), get_id(wire
), get_id(module
), GetSize(sig
), get_bv(sig
).c_str()));
801 if (wire
->port_input
)
802 ex_input_eq
.push_back(stringf(" (= (|%s_n %s| state) (|%s_n %s| other_state))",
803 get_id(module
), get_id(wire
), get_id(module
), get_id(wire
)));
805 for (int i
= 0; i
< GetSize(sig
); i
++)
806 if (GetSize(sig
) > 1) {
807 decls
.push_back(stringf("(define-fun |%s_n %s %d| ((state |%s_s|)) Bool %s)\n",
808 get_id(module
), get_id(wire
), i
, get_id(module
), get_bool(sig
[i
]).c_str()));
809 if (wire
->port_input
)
810 ex_input_eq
.push_back(stringf(" (= (|%s_n %s %d| state) (|%s_n %s %d| other_state))",
811 get_id(module
), get_id(wire
), i
, get_id(module
), get_id(wire
), i
));
813 decls
.push_back(stringf("(define-fun |%s_n %s| ((state |%s_s|)) Bool %s)\n",
814 get_id(module
), get_id(wire
), get_id(module
), get_bool(sig
[i
]).c_str()));
815 if (wire
->port_input
)
816 ex_input_eq
.push_back(stringf(" (= (|%s_n %s| state) (|%s_n %s| other_state))",
817 get_id(module
), get_id(wire
), get_id(module
), get_id(wire
)));
823 if (verbose
) log("=> export logic associated with the initial state\n");
825 vector
<string
> init_list
;
826 for (auto wire
: module
->wires())
827 if (wire
->attributes
.count("\\init")) {
828 RTLIL::SigSpec sig
= sigmap(wire
);
829 Const val
= wire
->attributes
.at("\\init");
830 val
.bits
.resize(GetSize(sig
), State::Sx
);
831 if (bvmode
&& GetSize(sig
) > 1) {
832 Const
mask(State::S1
, GetSize(sig
));
833 bool use_mask
= false;
834 for (int i
= 0; i
< GetSize(sig
); i
++)
835 if (val
[i
] != State::S0
&& val
[i
] != State::S1
) {
841 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
)));
843 init_list
.push_back(stringf("(= %s #b%s) ; %s", get_bv(sig
).c_str(), val
.as_string().c_str(), get_id(wire
)));
845 for (int i
= 0; i
< GetSize(sig
); i
++)
846 if (val
[i
] == State::S0
|| val
[i
] == State::S1
)
847 init_list
.push_back(stringf("(= %s %s) ; %s", get_bool(sig
[i
]).c_str(), val
[i
] == State::S1
? "true" : "false", get_id(wire
)));
851 if (verbose
) log("=> export logic driving asserts\n");
853 int assert_id
= 0, assume_id
= 0, cover_id
= 0;
854 vector
<string
> assert_list
, assume_list
, cover_list
;
856 for (auto cell
: module
->cells())
858 if (cell
->type
.in("$assert", "$assume", "$cover"))
860 int &id
= cell
->type
== "$assert" ? assert_id
:
861 cell
->type
== "$assume" ? assume_id
:
862 cell
->type
== "$cover" ? cover_id
: *(int*)nullptr;
864 char postfix
= cell
->type
== "$assert" ? 'a' :
865 cell
->type
== "$assume" ? 'u' :
866 cell
->type
== "$cover" ? 'c' : 0;
868 string name_a
= get_bool(cell
->getPort("\\A"));
869 string name_en
= get_bool(cell
->getPort("\\EN"));
870 decls
.push_back(stringf("; yosys-smt2-%s %d %s\n", cell
->type
.c_str() + 1, id
,
871 cell
->attributes
.count("\\src") ? cell
->attributes
.at("\\src").decode_string().c_str() : get_id(cell
)));
873 if (cell
->type
== "$cover")
874 decls
.push_back(stringf("(define-fun |%s_%c %d| ((state |%s_s|)) Bool (and %s %s)) ; %s\n",
875 get_id(module
), postfix
, id
, get_id(module
), name_a
.c_str(), name_en
.c_str(), get_id(cell
)));
877 decls
.push_back(stringf("(define-fun |%s_%c %d| ((state |%s_s|)) Bool (or %s (not %s))) ; %s\n",
878 get_id(module
), postfix
, id
, get_id(module
), name_a
.c_str(), name_en
.c_str(), get_id(cell
)));
880 if (cell
->type
== "$assert")
881 assert_list
.push_back(stringf("(|%s_a %d| state)", get_id(module
), id
));
882 else if (cell
->type
== "$assume")
883 assume_list
.push_back(stringf("(|%s_u %d| state)", get_id(module
), id
));
889 if (verbose
) log("=> export logic driving hierarchical cells\n");
891 for (auto cell
: module
->cells())
892 if (module
->design
->module(cell
->type
) != nullptr)
895 while (!hiercells_queue
.empty())
897 std::set
<RTLIL::Cell
*> queue
;
898 queue
.swap(hiercells_queue
);
900 for (auto cell
: queue
)
902 string cell_state
= stringf("(|%s_h %s| state)", get_id(module
), get_id(cell
->name
));
903 Module
*m
= module
->design
->module(cell
->type
);
904 log_assert(m
!= nullptr);
906 hier
.push_back(stringf(" (= (|%s_is| state) (|%s_is| %s))\n",
907 get_id(module
), get_id(cell
->type
), cell_state
.c_str()));
909 for (auto &conn
: cell
->connections())
911 if (GetSize(conn
.second
) == 0)
914 Wire
*w
= m
->wire(conn
.first
);
915 SigSpec sig
= sigmap(conn
.second
);
917 if (bvmode
|| GetSize(w
) == 1) {
918 hier
.push_back(stringf(" (= %s (|%s_n %s| %s)) ; %s.%s\n", (GetSize(w
) > 1 ? get_bv(sig
) : get_bool(sig
)).c_str(),
919 get_id(cell
->type
), get_id(w
), cell_state
.c_str(), get_id(cell
->type
), get_id(w
)));
921 for (int i
= 0; i
< GetSize(w
); i
++)
922 hier
.push_back(stringf(" (= %s (|%s_n %s %d| %s)) ; %s.%s[%d]\n", get_bool(sig
[i
]).c_str(),
923 get_id(cell
->type
), get_id(w
), i
, cell_state
.c_str(), get_id(cell
->type
), get_id(w
), i
));
929 for (int iter
= 1; !registers
.empty(); iter
++)
931 pool
<Cell
*> this_regs
;
932 this_regs
.swap(registers
);
934 if (verbose
) log("=> export logic driving registers [iteration %d]\n", iter
);
936 for (auto cell
: this_regs
)
938 if (cell
->type
.in("$_FF_", "$_DFF_P_", "$_DFF_N_"))
940 std::string expr_d
= get_bool(cell
->getPort("\\D"));
941 std::string expr_q
= get_bool(cell
->getPort("\\Q"), "next_state");
942 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"))));
943 ex_state_eq
.push_back(stringf("(= %s %s)", get_bool(cell
->getPort("\\Q")).c_str(), get_bool(cell
->getPort("\\Q"), "other_state").c_str()));
946 if (cell
->type
.in("$ff", "$dff"))
948 std::string expr_d
= get_bv(cell
->getPort("\\D"));
949 std::string expr_q
= get_bv(cell
->getPort("\\Q"), "next_state");
950 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"))));
951 ex_state_eq
.push_back(stringf("(= %s %s)", get_bv(cell
->getPort("\\Q")).c_str(), get_bv(cell
->getPort("\\Q"), "other_state").c_str()));
954 if (cell
->type
.in("$anyconst", "$allconst"))
956 std::string expr_d
= get_bv(cell
->getPort("\\Y"));
957 std::string expr_q
= get_bv(cell
->getPort("\\Y"), "next_state");
958 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"))));
959 if (cell
->type
== "$anyconst")
960 ex_state_eq
.push_back(stringf("(= %s %s)", get_bv(cell
->getPort("\\Y")).c_str(), get_bv(cell
->getPort("\\Y"), "other_state").c_str()));
963 if (cell
->type
== "$mem")
965 int arrayid
= memarrays
.at(cell
);
967 int abits
= cell
->getParam("\\ABITS").as_int();
968 int width
= cell
->getParam("\\WIDTH").as_int();
969 int wr_ports
= cell
->getParam("\\WR_PORTS").as_int();
971 bool async_read
= false;
972 string initial_memstate
, final_memstate
;
974 if (!cell
->getParam("\\WR_CLK_ENABLE").is_fully_ones()) {
975 log_assert(cell
->getParam("\\WR_CLK_ENABLE").is_fully_zero());
977 initial_memstate
= stringf("%s#%d#0", get_id(module
), arrayid
);
978 final_memstate
= stringf("%s#%d#final", get_id(module
), arrayid
);
983 int mem_size
= cell
->getParam("\\SIZE").as_int();
984 int mem_offset
= cell
->getParam("\\OFFSET").as_int();
987 makebits(final_memstate
, width
*mem_size
, get_id(cell
));
990 for (int i
= 0; i
< wr_ports
; i
++)
992 SigSpec addr_sig
= cell
->getPort("\\WR_ADDR").extract(abits
*i
, abits
);
993 SigSpec data_sig
= cell
->getPort("\\WR_DATA").extract(width
*i
, width
);
994 SigSpec mask_sig
= cell
->getPort("\\WR_EN").extract(width
*i
, width
);
996 std::string addr
= get_bv(addr_sig
);
997 std::string data
= get_bv(data_sig
);
998 std::string mask
= get_bv(mask_sig
);
1000 decls
.push_back(stringf("(define-fun |%s_m:W%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1001 get_id(module
), i
, get_id(cell
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
1002 addr
= stringf("(|%s_m:W%dA %s| state)", get_id(module
), i
, get_id(cell
));
1004 decls
.push_back(stringf("(define-fun |%s_m:W%dD %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1005 get_id(module
), i
, get_id(cell
), get_id(module
), width
, data
.c_str(), log_signal(data_sig
)));
1006 data
= stringf("(|%s_m:W%dD %s| state)", get_id(module
), i
, get_id(cell
));
1008 decls
.push_back(stringf("(define-fun |%s_m:W%dM %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1009 get_id(module
), i
, get_id(cell
), get_id(module
), width
, mask
.c_str(), log_signal(mask_sig
)));
1010 mask
= stringf("(|%s_m:W%dM %s| state)", get_id(module
), i
, get_id(cell
));
1012 std::string data_expr
;
1014 for (int k
= mem_size
-1; k
>= 0; k
--) {
1015 std::string new_data
= stringf("(bvor (bvand %s %s) (bvand ((_ extract %d %d) (|%s#%d#%d| state)) (bvnot %s)))",
1016 data
.c_str(), mask
.c_str(), width
*(k
+1)-1, width
*k
, get_id(module
), arrayid
, i
, mask
.c_str());
1017 data_expr
+= stringf("\n (ite (= %s #b%s) %s ((_ extract %d %d) (|%s#%d#%d| state)))",
1018 addr
.c_str(), Const(k
+mem_offset
, abits
).as_string().c_str(), new_data
.c_str(),
1019 width
*(k
+1)-1, width
*k
, get_id(module
), arrayid
, i
);
1022 decls
.push_back(stringf("(define-fun |%s#%d#%d| ((state |%s_s|)) (_ BitVec %d) (concat%s)) ; %s\n",
1023 get_id(module
), arrayid
, i
+1, get_id(module
), width
*mem_size
, data_expr
.c_str(), get_id(cell
)));
1030 dtmembers
.push_back(stringf(" (|%s| (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
1031 initial_memstate
.c_str(), abits
, width
, get_id(cell
)));
1033 decls
.push_back(stringf("(declare-fun |%s| (|%s_s|) (Array (_ BitVec %d) (_ BitVec %d))) ; %s\n",
1034 initial_memstate
.c_str(), get_id(module
), abits
, width
, get_id(cell
)));
1037 for (int i
= 0; i
< wr_ports
; i
++)
1039 SigSpec addr_sig
= cell
->getPort("\\WR_ADDR").extract(abits
*i
, abits
);
1040 SigSpec data_sig
= cell
->getPort("\\WR_DATA").extract(width
*i
, width
);
1041 SigSpec mask_sig
= cell
->getPort("\\WR_EN").extract(width
*i
, width
);
1043 std::string addr
= get_bv(addr_sig
);
1044 std::string data
= get_bv(data_sig
);
1045 std::string mask
= get_bv(mask_sig
);
1047 decls
.push_back(stringf("(define-fun |%s_m:W%dA %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1048 get_id(module
), i
, get_id(cell
), get_id(module
), abits
, addr
.c_str(), log_signal(addr_sig
)));
1049 addr
= stringf("(|%s_m:W%dA %s| state)", get_id(module
), i
, get_id(cell
));
1051 decls
.push_back(stringf("(define-fun |%s_m:W%dD %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1052 get_id(module
), i
, get_id(cell
), get_id(module
), width
, data
.c_str(), log_signal(data_sig
)));
1053 data
= stringf("(|%s_m:W%dD %s| state)", get_id(module
), i
, get_id(cell
));
1055 decls
.push_back(stringf("(define-fun |%s_m:W%dM %s| ((state |%s_s|)) (_ BitVec %d) %s) ; %s\n",
1056 get_id(module
), i
, get_id(cell
), get_id(module
), width
, mask
.c_str(), log_signal(mask_sig
)));
1057 mask
= stringf("(|%s_m:W%dM %s| state)", get_id(module
), i
, get_id(cell
));
1059 data
= stringf("(bvor (bvand %s %s) (bvand (select (|%s#%d#%d| state) %s) (bvnot %s)))",
1060 data
.c_str(), mask
.c_str(), get_id(module
), arrayid
, i
, addr
.c_str(), mask
.c_str());
1062 decls
.push_back(stringf("(define-fun |%s#%d#%d| ((state |%s_s|)) (Array (_ BitVec %d) (_ BitVec %d)) "
1063 "(store (|%s#%d#%d| state) %s %s)) ; %s\n",
1064 get_id(module
), arrayid
, i
+1, get_id(module
), abits
, width
,
1065 get_id(module
), arrayid
, i
, addr
.c_str(), data
.c_str(), get_id(cell
)));
1069 std::string expr_d
= stringf("(|%s#%d#%d| state)", get_id(module
), arrayid
, wr_ports
);
1070 std::string expr_q
= stringf("(|%s#%d#0| next_state)", get_id(module
), arrayid
);
1071 trans
.push_back(stringf(" (= %s %s) ; %s\n", expr_d
.c_str(), expr_q
.c_str(), get_id(cell
)));
1072 ex_state_eq
.push_back(stringf("(= (|%s#%d#0| state) (|%s#%d#0| other_state))", get_id(module
), arrayid
, get_id(module
), arrayid
));
1075 hier
.push_back(stringf(" (= %s (|%s| state)) ; %s\n", expr_d
.c_str(), final_memstate
.c_str(), get_id(cell
)));
1077 Const init_data
= cell
->getParam("\\INIT");
1078 int memsize
= cell
->getParam("\\SIZE").as_int();
1080 for (int i
= 0; i
< memsize
; i
++)
1082 if (i
*width
>= GetSize(init_data
))
1085 Const initword
= init_data
.extract(i
*width
, width
, State::Sx
);
1086 bool gen_init_constr
= false;
1088 for (auto bit
: initword
.bits
)
1089 if (bit
== State::S0
|| bit
== State::S1
)
1090 gen_init_constr
= true;
1092 if (gen_init_constr
)
1097 init_list
.push_back(stringf("(= (select (|%s#%d#0| state) #b%s) #b%s) ; %s[%d]",
1098 get_id(module
), arrayid
, Const(i
, abits
).as_string().c_str(),
1099 initword
.as_string().c_str(), get_id(cell
), i
));
1106 if (verbose
) log("=> finalizing SMT2 representation of %s.\n", log_id(module
));
1108 for (auto c
: hiercells
) {
1109 assert_list
.push_back(stringf("(|%s_a| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1110 assume_list
.push_back(stringf("(|%s_u| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1111 init_list
.push_back(stringf("(|%s_i| (|%s_h %s| state))", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1112 hier
.push_back(stringf(" (|%s_h| (|%s_h %s| state))\n", get_id(c
->type
), get_id(module
), get_id(c
->name
)));
1113 trans
.push_back(stringf(" (|%s_t| (|%s_h %s| state) (|%s_h %s| next_state))\n",
1114 get_id(c
->type
), get_id(module
), get_id(c
->name
), get_id(module
), get_id(c
->name
)));
1115 ex_state_eq
.push_back(stringf("(|%s_ex_state_eq| (|%s_h %s| state) (|%s_h %s| other_state))\n",
1116 get_id(c
->type
), get_id(module
), get_id(c
->name
), get_id(module
), get_id(c
->name
)));
1121 string expr
= ex_state_eq
.empty() ? "true" : "(and";
1122 if (!ex_state_eq
.empty()) {
1123 if (GetSize(ex_state_eq
) == 1) {
1124 expr
= "\n " + ex_state_eq
.front() + "\n";
1126 for (auto &str
: ex_state_eq
)
1127 expr
+= stringf("\n %s", str
.c_str());
1131 decls
.push_back(stringf("(define-fun |%s_ex_state_eq| ((state |%s_s|) (other_state |%s_s|)) Bool %s)\n",
1132 get_id(module
), get_id(module
), get_id(module
), expr
.c_str()));
1134 expr
= ex_input_eq
.empty() ? "true" : "(and";
1135 if (!ex_input_eq
.empty()) {
1136 if (GetSize(ex_input_eq
) == 1) {
1137 expr
= "\n " + ex_input_eq
.front() + "\n";
1139 for (auto &str
: ex_input_eq
)
1140 expr
+= stringf("\n %s", str
.c_str());
1144 decls
.push_back(stringf("(define-fun |%s_ex_input_eq| ((state |%s_s|) (other_state |%s_s|)) Bool %s)\n",
1145 get_id(module
), get_id(module
), get_id(module
), expr
.c_str()));
1148 string assert_expr
= assert_list
.empty() ? "true" : "(and";
1149 if (!assert_list
.empty()) {
1150 if (GetSize(assert_list
) == 1) {
1151 assert_expr
= "\n " + assert_list
.front() + "\n";
1153 for (auto &str
: assert_list
)
1154 assert_expr
+= stringf("\n %s", str
.c_str());
1155 assert_expr
+= "\n)";
1158 decls
.push_back(stringf("(define-fun |%s_a| ((state |%s_s|)) Bool %s)\n",
1159 get_id(module
), get_id(module
), assert_expr
.c_str()));
1161 string assume_expr
= assume_list
.empty() ? "true" : "(and";
1162 if (!assume_list
.empty()) {
1163 if (GetSize(assume_list
) == 1) {
1164 assume_expr
= "\n " + assume_list
.front() + "\n";
1166 for (auto &str
: assume_list
)
1167 assume_expr
+= stringf("\n %s", str
.c_str());
1168 assume_expr
+= "\n)";
1171 decls
.push_back(stringf("(define-fun |%s_u| ((state |%s_s|)) Bool %s)\n",
1172 get_id(module
), get_id(module
), assume_expr
.c_str()));
1174 string init_expr
= init_list
.empty() ? "true" : "(and";
1175 if (!init_list
.empty()) {
1176 if (GetSize(init_list
) == 1) {
1177 init_expr
= "\n " + init_list
.front() + "\n";
1179 for (auto &str
: init_list
)
1180 init_expr
+= stringf("\n %s", str
.c_str());
1184 decls
.push_back(stringf("(define-fun |%s_i| ((state |%s_s|)) Bool %s)\n",
1185 get_id(module
), get_id(module
), init_expr
.c_str()));
1188 void write(std::ostream
&f
)
1190 f
<< stringf("; yosys-smt2-module %s\n", get_id(module
));
1193 f
<< stringf("(define-sort |%s_s| () (_ BitVec %d))\n", get_id(module
), statebv_width
);
1194 mod_stbv_width
[module
->name
] = statebv_width
;
1197 f
<< stringf("(declare-datatype |%s_s| ((|%s_mk|\n", get_id(module
), get_id(module
));
1198 for (auto it
: dtmembers
)
1200 f
<< stringf(")))\n");
1202 f
<< stringf("(declare-sort |%s_s| 0)\n", get_id(module
));
1204 for (auto it
: decls
)
1207 f
<< stringf("(define-fun |%s_h| ((state |%s_s|)) Bool ", get_id(module
), get_id(module
));
1208 if (GetSize(hier
) > 1) {
1210 for (auto it
: hier
)
1214 if (GetSize(hier
) == 1)
1215 f
<< "\n" + hier
.front() + ")\n";
1219 f
<< stringf("(define-fun |%s_t| ((state |%s_s|) (next_state |%s_s|)) Bool ", get_id(module
), get_id(module
), get_id(module
));
1220 if (GetSize(trans
) > 1) {
1222 for (auto it
: trans
)
1226 if (GetSize(trans
) == 1)
1227 f
<< "\n" + trans
.front() + ")";
1230 f
<< stringf(" ; end of module %s\n", get_id(module
));
1234 struct Smt2Backend
: public Backend
{
1235 Smt2Backend() : Backend("smt2", "write design to SMT-LIBv2 file") { }
1238 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1240 log(" write_smt2 [options] [filename]\n");
1242 log("Write a SMT-LIBv2 [1] description of the current design. For a module with name\n");
1243 log("'<mod>' this will declare the sort '<mod>_s' (state of the module) and will\n");
1244 log("define and declare functions operating on that state.\n");
1246 log("The following SMT2 functions are generated for a module with name '<mod>'.\n");
1247 log("Some declarations/definitions are printed with a special comment. A prover\n");
1248 log("using the SMT2 files can use those comments to collect all relevant metadata\n");
1249 log("about the design.\n");
1251 log(" ; yosys-smt2-module <mod>\n");
1252 log(" (declare-sort |<mod>_s| 0)\n");
1253 log(" The sort representing a state of module <mod>.\n");
1255 log(" (define-fun |<mod>_h| ((state |<mod>_s|)) Bool (...))\n");
1256 log(" This function must be asserted for each state to establish the\n");
1257 log(" design hierarchy.\n");
1259 log(" ; yosys-smt2-input <wirename> <width>\n");
1260 log(" ; yosys-smt2-output <wirename> <width>\n");
1261 log(" ; yosys-smt2-register <wirename> <width>\n");
1262 log(" ; yosys-smt2-wire <wirename> <width>\n");
1263 log(" (define-fun |<mod>_n <wirename>| (|<mod>_s|) (_ BitVec <width>))\n");
1264 log(" (define-fun |<mod>_n <wirename>| (|<mod>_s|) Bool)\n");
1265 log(" For each port, register, and wire with the 'keep' attribute set an\n");
1266 log(" accessor function is generated. Single-bit wires are returned as Bool,\n");
1267 log(" multi-bit wires as BitVec.\n");
1269 log(" ; yosys-smt2-cell <submod> <instancename>\n");
1270 log(" (declare-fun |<mod>_h <instancename>| (|<mod>_s|) |<submod>_s|)\n");
1271 log(" There is a function like that for each hierarchical instance. It\n");
1272 log(" returns the sort that represents the state of the sub-module that\n");
1273 log(" implements the instance.\n");
1275 log(" (declare-fun |<mod>_is| (|<mod>_s|) Bool)\n");
1276 log(" This function must be asserted 'true' for initial states, and 'false'\n");
1277 log(" otherwise.\n");
1279 log(" (define-fun |<mod>_i| ((state |<mod>_s|)) Bool (...))\n");
1280 log(" This function must be asserted 'true' for initial states. For\n");
1281 log(" non-initial states it must be left unconstrained.\n");
1283 log(" (define-fun |<mod>_t| ((state |<mod>_s|) (next_state |<mod>_s|)) Bool (...))\n");
1284 log(" This function evaluates to 'true' if the states 'state' and\n");
1285 log(" 'next_state' form a valid state transition.\n");
1287 log(" (define-fun |<mod>_a| ((state |<mod>_s|)) Bool (...))\n");
1288 log(" This function evaluates to 'true' if all assertions hold in the state.\n");
1290 log(" (define-fun |<mod>_u| ((state |<mod>_s|)) Bool (...))\n");
1291 log(" This function evaluates to 'true' if all assumptions hold in the state.\n");
1293 log(" ; yosys-smt2-assert <id> <filename:linenum>\n");
1294 log(" (define-fun |<mod>_a <id>| ((state |<mod>_s|)) Bool (...))\n");
1295 log(" Each $assert cell is converted into one of this functions. The function\n");
1296 log(" evaluates to 'true' if the assert statement holds in the state.\n");
1298 log(" ; yosys-smt2-assume <id> <filename:linenum>\n");
1299 log(" (define-fun |<mod>_u <id>| ((state |<mod>_s|)) Bool (...))\n");
1300 log(" Each $assume cell is converted into one of this functions. The function\n");
1301 log(" evaluates to 'true' if the assume statement holds in the state.\n");
1303 log(" ; yosys-smt2-cover <id> <filename:linenum>\n");
1304 log(" (define-fun |<mod>_c <id>| ((state |<mod>_s|)) Bool (...))\n");
1305 log(" Each $cover cell is converted into one of this functions. The function\n");
1306 log(" evaluates to 'true' if the cover statement is activated in the state.\n");
1311 log(" this will print the recursive walk used to export the modules.\n");
1314 log(" Use a BitVec sort to represent a state instead of an uninterpreted\n");
1315 log(" sort. As a side-effect this will prevent use of arrays to model\n");
1316 log(" memories.\n");
1319 log(" Use SMT-LIB 2.6 style datatypes to represent a state instead of an\n");
1320 log(" uninterpreted sort.\n");
1323 log(" disable support for BitVec (FixedSizeBitVectors theory). without this\n");
1324 log(" option multi-bit wires are represented using the BitVec sort and\n");
1325 log(" support for coarse grain cells (incl. arithmetic) is enabled.\n");
1328 log(" disable support for memories (via ArraysEx theory). this option is\n");
1329 log(" implied by -nobv. only $mem cells without merged registers in\n");
1330 log(" read ports are supported. call \"memory\" with -nordff to make sure\n");
1331 log(" that no registers are merged into $mem read ports. '<mod>_m' functions\n");
1332 log(" will be generated for accessing the arrays that are used to represent\n");
1333 log(" memories.\n");
1336 log(" create '<mod>_n' functions for all public wires. by default only ports,\n");
1337 log(" registers, and wires with the 'keep' attribute are exported.\n");
1339 log(" -tpl <template_file>\n");
1340 log(" use the given template file. the line containing only the token '%%%%'\n");
1341 log(" is replaced with the regular output of this command.\n");
1343 log("[1] For more information on SMT-LIBv2 visit http://smt-lib.org/ or read David\n");
1344 log("R. Cok's tutorial: http://www.grammatech.com/resources/smt/SMTLIBTutorial.pdf\n");
1346 log("---------------------------------------------------------------------------\n");
1350 log("Consider the following module (test.v). We want to prove that the output can\n");
1351 log("never transition from a non-zero value to a zero value.\n");
1353 log(" module test(input clk, output reg [3:0] y);\n");
1354 log(" always @(posedge clk)\n");
1355 log(" y <= (y << 1) | ^y;\n");
1356 log(" endmodule\n");
1358 log("For this proof we create the following template (test.tpl).\n");
1360 log(" ; we need QF_UFBV for this poof\n");
1361 log(" (set-logic QF_UFBV)\n");
1363 log(" ; insert the auto-generated code here\n");
1366 log(" ; declare two state variables s1 and s2\n");
1367 log(" (declare-fun s1 () test_s)\n");
1368 log(" (declare-fun s2 () test_s)\n");
1370 log(" ; state s2 is the successor of state s1\n");
1371 log(" (assert (test_t s1 s2))\n");
1373 log(" ; we are looking for a model with y non-zero in s1\n");
1374 log(" (assert (distinct (|test_n y| s1) #b0000))\n");
1376 log(" ; we are looking for a model with y zero in s2\n");
1377 log(" (assert (= (|test_n y| s2) #b0000))\n");
1379 log(" ; is there such a model?\n");
1380 log(" (check-sat)\n");
1382 log("The following yosys script will create a 'test.smt2' file for our proof:\n");
1384 log(" read_verilog test.v\n");
1385 log(" hierarchy -check; proc; opt; check -assert\n");
1386 log(" write_smt2 -bv -tpl test.tpl test.smt2\n");
1388 log("Running 'cvc4 test.smt2' will print 'unsat' because y can never transition\n");
1389 log("from non-zero to zero in the test design.\n");
1392 virtual void execute(std::ostream
*&f
, std::string filename
, std::vector
<std::string
> args
, RTLIL::Design
*design
)
1394 std::ifstream template_f
;
1395 bool bvmode
= true, memmode
= true, wiresmode
= false, verbose
= false, statebv
= false, statedt
= false;
1396 bool forallmode
= false;
1398 log_header(design
, "Executing SMT2 backend.\n");
1401 for (argidx
= 1; argidx
< args
.size(); argidx
++)
1403 if (args
[argidx
] == "-tpl" && argidx
+1 < args
.size()) {
1404 template_f
.open(args
[++argidx
]);
1405 if (template_f
.fail())
1406 log_error("Can't open template file `%s'.\n", args
[argidx
].c_str());
1409 if (args
[argidx
] == "-bv" || args
[argidx
] == "-mem") {
1410 log_warning("Options -bv and -mem are now the default. Support for -bv and -mem will be removed in the future.\n");
1413 if (args
[argidx
] == "-stbv") {
1418 if (args
[argidx
] == "-stdt") {
1423 if (args
[argidx
] == "-nobv") {
1428 if (args
[argidx
] == "-nomem") {
1432 if (args
[argidx
] == "-wires") {
1436 if (args
[argidx
] == "-verbose") {
1442 extra_args(f
, filename
, args
, argidx
);
1444 if (template_f
.is_open()) {
1446 while (std::getline(template_f
, line
)) {
1448 while (indent
< GetSize(line
) && (line
[indent
] == ' ' || line
[indent
] == '\t'))
1450 if (line
.substr(indent
, 2) == "%%")
1452 *f
<< line
<< std::endl
;
1456 *f
<< stringf("; SMT-LIBv2 description generated by %s\n", yosys_version_str
);
1459 *f
<< stringf("; yosys-smt2-nobv\n");
1462 *f
<< stringf("; yosys-smt2-nomem\n");
1465 *f
<< stringf("; yosys-smt2-stbv\n");
1468 *f
<< stringf("; yosys-smt2-stdt\n");
1470 std::vector
<RTLIL::Module
*> sorted_modules
;
1472 // extract module dependencies
1473 std::map
<RTLIL::Module
*, std::set
<RTLIL::Module
*>> module_deps
;
1474 for (auto &mod_it
: design
->modules_
) {
1475 module_deps
[mod_it
.second
] = std::set
<RTLIL::Module
*>();
1476 for (auto &cell_it
: mod_it
.second
->cells_
)
1477 if (design
->modules_
.count(cell_it
.second
->type
) > 0)
1478 module_deps
[mod_it
.second
].insert(design
->modules_
.at(cell_it
.second
->type
));
1481 // simple good-enough topological sort
1482 // (O(n*m) on n elements and depth m)
1483 while (module_deps
.size() > 0) {
1484 size_t sorted_modules_idx
= sorted_modules
.size();
1485 for (auto &it
: module_deps
) {
1486 for (auto &dep
: it
.second
)
1487 if (module_deps
.count(dep
) > 0)
1489 // log("Next in topological sort: %s\n", RTLIL::id2cstr(it.first->name));
1490 sorted_modules
.push_back(it
.first
);
1493 if (sorted_modules_idx
== sorted_modules
.size())
1494 log_error("Cyclic dependency between modules found! Cycle includes module %s.\n", RTLIL::id2cstr(module_deps
.begin()->first
->name
));
1495 while (sorted_modules_idx
< sorted_modules
.size())
1496 module_deps
.erase(sorted_modules
.at(sorted_modules_idx
++));
1499 dict
<IdString
, int> mod_stbv_width
;
1500 dict
<IdString
, dict
<IdString
, pair
<bool, bool>>> mod_clk_cache
;
1501 Module
*topmod
= design
->top_module();
1502 std::string topmod_id
;
1504 for (auto module
: sorted_modules
)
1505 for (auto cell
: module
->cells())
1506 if (cell
->type
.in("$allconst", "$allseq"))
1511 *f
<< stringf("; yosys-smt2-forall\n");
1512 if (!statebv
&& !statedt
)
1513 log_error("Forall-exists problems are only supported in -stbv or -stdt mode.\n");
1516 for (auto module
: sorted_modules
)
1518 if (module
->get_bool_attribute("\\blackbox") || module
->has_memories_warn() || module
->has_processes_warn())
1521 log("Creating SMT-LIBv2 representation of module %s.\n", log_id(module
));
1523 Smt2Worker
worker(module
, bvmode
, memmode
, wiresmode
, verbose
, statebv
, statedt
, forallmode
, mod_stbv_width
, mod_clk_cache
);
1527 if (module
== topmod
)
1528 topmod_id
= worker
.get_id(module
);
1532 *f
<< stringf("; yosys-smt2-topmod %s\n", topmod_id
.c_str());
1534 *f
<< stringf("; end of yosys output\n");
1536 if (template_f
.is_open()) {
1538 while (std::getline(template_f
, line
))
1539 *f
<< line
<< std::endl
;
1544 PRIVATE_NAMESPACE_END