Merge branch 'btor-ng'
[yosys.git] / backends / btor / btor.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
20 #include "kernel/rtlil.h"
21 #include "kernel/register.h"
22 #include "kernel/sigtools.h"
23 #include "kernel/celltypes.h"
24 #include "kernel/log.h"
25 #include <string>
26
27 USING_YOSYS_NAMESPACE
28 PRIVATE_NAMESPACE_BEGIN
29
30 struct BtorWorker
31 {
32 std::ostream &f;
33 SigMap sigmap;
34 RTLIL::Module *module;
35 bool verbose;
36 bool single_bad;
37
38 int next_nid = 1;
39 int initstate_nid = -1;
40
41 // <width> => <sid>
42 dict<int, int> sorts_bv;
43
44 // (<address-width>, <data-width>) => <sid>
45 dict<pair<int, int>, int> sorts_mem;
46
47 // SigBit => (<nid>, <bitidx>)
48 dict<SigBit, pair<int, int>> bit_nid;
49
50 // <nid> => <bvwidth>
51 dict<int, int> nid_width;
52
53 // SigSpec => <nid>
54 dict<SigSpec, int> sig_nid;
55
56 // bit to driving cell
57 dict<SigBit, Cell*> bit_cell;
58
59 // nids for constants
60 dict<Const, int> consts;
61
62 // ff inputs that need to be evaluated (<nid>, <ff_cell>)
63 vector<pair<int, Cell*>> ff_todo;
64
65 pool<Cell*> cell_recursion_guard;
66 vector<int> bad_properties;
67 dict<SigBit, bool> initbits;
68 pool<Wire*> statewires;
69 string indent;
70
71 void btorf(const char *fmt, ...)
72 {
73 va_list ap;
74 va_start(ap, fmt);
75 f << indent << vstringf(fmt, ap);
76 va_end(ap);
77 }
78
79 void btorf_push(const string &id)
80 {
81 if (verbose) {
82 f << indent << stringf(" ; begin %s\n", id.c_str());
83 indent += " ";
84 }
85 }
86
87 void btorf_pop(const string &id)
88 {
89 if (verbose) {
90 indent = indent.substr(4);
91 f << indent << stringf(" ; end %s\n", id.c_str());
92 }
93 }
94
95 int get_bv_sid(int width)
96 {
97 if (sorts_bv.count(width) == 0) {
98 int nid = next_nid++;
99 btorf("%d sort bitvec %d\n", nid, width);
100 sorts_bv[width] = nid;
101 }
102 return sorts_bv.at(width);
103 }
104
105 int get_mem_sid(int abits, int dbits)
106 {
107 pair<int, int> key(abits, dbits);
108 if (sorts_mem.count(key) == 0) {
109 int addr_sid = get_bv_sid(abits);
110 int data_sid = get_bv_sid(dbits);
111 int nid = next_nid++;
112 btorf("%d sort array %d %d\n", nid, addr_sid, data_sid);
113 sorts_mem[key] = nid;
114 }
115 return sorts_mem.at(key);
116 }
117
118 void add_nid_sig(int nid, const SigSpec &sig)
119 {
120 if (verbose)
121 f << indent << stringf("; %d %s\n", nid, log_signal(sig));
122
123 for (int i = 0; i < GetSize(sig); i++)
124 bit_nid[sig[i]] = make_pair(nid, i);
125
126 sig_nid[sig] = nid;
127 nid_width[nid] = GetSize(sig);
128 }
129
130 void export_cell(Cell *cell)
131 {
132 log_assert(cell_recursion_guard.count(cell) == 0);
133 cell_recursion_guard.insert(cell);
134 btorf_push(log_id(cell));
135
136 if (cell->type.in("$add", "$sub", "$and", "$or", "$xor", "$xnor", "$shl", "$sshl", "$shr", "$sshr", "$shift", "$shiftx",
137 "$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
138 {
139 string btor_op;
140 if (cell->type == "$add") btor_op = "add";
141 if (cell->type == "$sub") btor_op = "sub";
142 if (cell->type.in("$shl", "$sshl")) btor_op = "sll";
143 if (cell->type == "$shr") btor_op = "srl";
144 if (cell->type == "$sshr") btor_op = "sra";
145 if (cell->type.in("$shift", "$shiftx")) btor_op = "shift";
146 if (cell->type.in("$and", "$_AND_")) btor_op = "and";
147 if (cell->type.in("$or", "$_OR_")) btor_op = "or";
148 if (cell->type.in("$xor", "$_XOR_")) btor_op = "xor";
149 if (cell->type == "$_NAND_") btor_op = "nand";
150 if (cell->type == "$_NOR_") btor_op = "nor";
151 if (cell->type.in("$xnor", "$_XNOR_")) btor_op = "xnor";
152 log_assert(!btor_op.empty());
153
154 int width = GetSize(cell->getPort("\\Y"));
155 width = std::max(width, GetSize(cell->getPort("\\A")));
156 width = std::max(width, GetSize(cell->getPort("\\B")));
157
158 bool a_signed = cell->hasParam("\\A_SIGNED") ? cell->getParam("\\A_SIGNED").as_bool() : false;
159 bool b_signed = cell->hasParam("\\B_SIGNED") ? cell->getParam("\\B_SIGNED").as_bool() : false;
160
161 if (btor_op == "shift" && !b_signed)
162 btor_op = "srl";
163
164 if (cell->type.in("$shl", "$sshl", "$shr", "$sshr"))
165 b_signed = false;
166
167 if (cell->type == "$sshr" && !a_signed)
168 btor_op = "srl";
169
170 int sid = get_bv_sid(width);
171 int nid;
172
173 if (btor_op == "shift")
174 {
175 int nid_a = get_sig_nid(cell->getPort("\\A"), width, false);
176 int nid_b = get_sig_nid(cell->getPort("\\B"), width, b_signed);
177
178 int nid_r = next_nid++;
179 btorf("%d srl %d %d %d\n", nid_r, sid, nid_a, nid_b);
180
181 int nid_b_neg = next_nid++;
182 btorf("%d neg %d %d\n", nid_b_neg, sid, nid_b);
183
184 int nid_l = next_nid++;
185 btorf("%d sll %d %d %d\n", nid_l, sid, nid_a, nid_b_neg);
186
187 int sid_bit = get_bv_sid(1);
188 int nid_zero = get_sig_nid(Const(0, width));
189 int nid_b_ltz = next_nid++;
190 btorf("%d slt %d %d %d\n", nid_b_ltz, sid_bit, nid_b, nid_zero);
191
192 nid = next_nid++;
193 btorf("%d ite %d %d %d %d\n", nid, sid, nid_b_ltz, nid_l, nid_r);
194 }
195 else
196 {
197 int nid_a = get_sig_nid(cell->getPort("\\A"), width, a_signed);
198 int nid_b = get_sig_nid(cell->getPort("\\B"), width, b_signed);
199
200 nid = next_nid++;
201 btorf("%d %s %d %d %d\n", nid, btor_op.c_str(), sid, nid_a, nid_b);
202 }
203
204 SigSpec sig = sigmap(cell->getPort("\\Y"));
205
206 if (GetSize(sig) < width) {
207 int sid = get_bv_sid(GetSize(sig));
208 int nid2 = next_nid++;
209 btorf("%d slice %d %d %d 0\n", nid2, sid, nid, GetSize(sig)-1);
210 nid = nid2;
211 }
212
213 add_nid_sig(nid, sig);
214 goto okay;
215 }
216
217 if (cell->type.in("$_ANDNOT_", "$_ORNOT_"))
218 {
219 int sid = get_bv_sid(1);
220 int nid_a = get_sig_nid(cell->getPort("\\A"));
221 int nid_b = get_sig_nid(cell->getPort("\\B"));
222
223 int nid1 = next_nid++;
224 int nid2 = next_nid++;
225
226 if (cell->type == "$_ANDNOT_") {
227 btorf("%d not %d %d\n", nid1, sid, nid_b);
228 btorf("%d and %d %d %d\n", nid2, sid, nid_a, nid1);
229 }
230
231 if (cell->type == "$_ORNOT_") {
232 btorf("%d not %d %d\n", nid1, sid, nid_b);
233 btorf("%d or %d %d %d\n", nid2, sid, nid_a, nid1);
234 }
235
236 SigSpec sig = sigmap(cell->getPort("\\Y"));
237 add_nid_sig(nid2, sig);
238 goto okay;
239 }
240
241 if (cell->type.in("$_OAI3_", "$_AOI3_"))
242 {
243 int sid = get_bv_sid(1);
244 int nid_a = get_sig_nid(cell->getPort("\\A"));
245 int nid_b = get_sig_nid(cell->getPort("\\B"));
246 int nid_c = get_sig_nid(cell->getPort("\\C"));
247
248 int nid1 = next_nid++;
249 int nid2 = next_nid++;
250 int nid3 = next_nid++;
251
252 if (cell->type == "$_OAI3_") {
253 btorf("%d or %d %d %d\n", nid1, sid, nid_a, nid_b);
254 btorf("%d and %d %d %d\n", nid2, sid, nid1, nid_c);
255 btorf("%d not %d %d\n", nid3, sid, nid2);
256 }
257
258 if (cell->type == "$_AOI3_") {
259 btorf("%d and %d %d %d\n", nid1, sid, nid_a, nid_b);
260 btorf("%d or %d %d %d\n", nid2, sid, nid1, nid_c);
261 btorf("%d not %d %d\n", nid3, sid, nid2);
262 }
263
264 SigSpec sig = sigmap(cell->getPort("\\Y"));
265 add_nid_sig(nid3, sig);
266 goto okay;
267 }
268
269 if (cell->type.in("$_OAI4_", "$_AOI4_"))
270 {
271 int sid = get_bv_sid(1);
272 int nid_a = get_sig_nid(cell->getPort("\\A"));
273 int nid_b = get_sig_nid(cell->getPort("\\B"));
274 int nid_c = get_sig_nid(cell->getPort("\\C"));
275 int nid_d = get_sig_nid(cell->getPort("\\D"));
276
277 int nid1 = next_nid++;
278 int nid2 = next_nid++;
279 int nid3 = next_nid++;
280 int nid4 = next_nid++;
281
282 if (cell->type == "$_OAI4_") {
283 btorf("%d or %d %d %d\n", nid1, sid, nid_a, nid_b);
284 btorf("%d or %d %d %d\n", nid2, sid, nid_c, nid_d);
285 btorf("%d and %d %d %d\n", nid3, sid, nid1, nid2);
286 btorf("%d not %d %d\n", nid4, sid, nid3);
287 }
288
289 if (cell->type == "$_AOI4_") {
290 btorf("%d and %d %d %d\n", nid1, sid, nid_a, nid_b);
291 btorf("%d and %d %d %d\n", nid2, sid, nid_c, nid_d);
292 btorf("%d or %d %d %d\n", nid3, sid, nid1, nid2);
293 btorf("%d not %d %d\n", nid4, sid, nid3);
294 }
295
296 SigSpec sig = sigmap(cell->getPort("\\Y"));
297 add_nid_sig(nid4, sig);
298 goto okay;
299 }
300
301 if (cell->type.in("$lt", "$le", "$eq", "$eqx", "$ne", "$nex", "$ge", "$gt"))
302 {
303 string btor_op;
304 if (cell->type == "$lt") btor_op = "lt";
305 if (cell->type == "$le") btor_op = "lte";
306 if (cell->type.in("$eq", "$eqx")) btor_op = "eq";
307 if (cell->type.in("$ne", "$nex")) btor_op = "ne";
308 if (cell->type == "$ge") btor_op = "gte";
309 if (cell->type == "$gt") btor_op = "gt";
310 log_assert(!btor_op.empty());
311
312 int width = 1;
313 width = std::max(width, GetSize(cell->getPort("\\A")));
314 width = std::max(width, GetSize(cell->getPort("\\B")));
315
316 bool a_signed = cell->hasParam("\\A_SIGNED") ? cell->getParam("\\A_SIGNED").as_bool() : false;
317 bool b_signed = cell->hasParam("\\B_SIGNED") ? cell->getParam("\\B_SIGNED").as_bool() : false;
318
319 int sid = get_bv_sid(1);
320 int nid_a = get_sig_nid(cell->getPort("\\A"), width, a_signed);
321 int nid_b = get_sig_nid(cell->getPort("\\B"), width, b_signed);
322
323 int nid = next_nid++;
324 if (cell->type.in("$lt", "$le", "$ge", "$gt")) {
325 btorf("%d %c%s %d %d %d\n", nid, a_signed || b_signed ? 's' : 'u', btor_op.c_str(), sid, nid_a, nid_b);
326 } else {
327 btorf("%d %s %d %d %d\n", nid, btor_op.c_str(), sid, nid_a, nid_b);
328 }
329
330 SigSpec sig = sigmap(cell->getPort("\\Y"));
331
332 if (GetSize(sig) > 1) {
333 int sid = get_bv_sid(GetSize(sig));
334 int nid2 = next_nid++;
335 btorf("%d uext %d %d %d\n", nid2, sid, nid, GetSize(sig) - 1);
336 nid = nid2;
337 }
338
339 add_nid_sig(nid, sig);
340 goto okay;
341 }
342
343 if (cell->type.in("$not", "$neg", "$_NOT_"))
344 {
345 string btor_op;
346 if (cell->type.in("$not", "$_NOT_")) btor_op = "not";
347 if (cell->type == "$neg") btor_op = "neg";
348 log_assert(!btor_op.empty());
349
350 int width = GetSize(cell->getPort("\\Y"));
351 width = std::max(width, GetSize(cell->getPort("\\A")));
352
353 bool a_signed = cell->hasParam("\\A_SIGNED") ? cell->getParam("\\A_SIGNED").as_bool() : false;
354
355 int sid = get_bv_sid(width);
356 int nid_a = get_sig_nid(cell->getPort("\\A"), width, a_signed);
357
358 int nid = next_nid++;
359 btorf("%d %s %d %d\n", nid, btor_op.c_str(), sid, nid_a);
360
361 SigSpec sig = sigmap(cell->getPort("\\Y"));
362
363 if (GetSize(sig) < width) {
364 int sid = get_bv_sid(GetSize(sig));
365 int nid2 = next_nid++;
366 btorf("%d slice %d %d %d 0\n", nid2, sid, nid, GetSize(sig)-1);
367 nid = nid2;
368 }
369
370 add_nid_sig(nid, sig);
371 goto okay;
372 }
373
374 if (cell->type.in("$logic_and", "$logic_or", "$logic_not"))
375 {
376 string btor_op;
377 if (cell->type == "$logic_and") btor_op = "and";
378 if (cell->type == "$logic_or") btor_op = "or";
379 if (cell->type == "$logic_not") btor_op = "not";
380 log_assert(!btor_op.empty());
381
382 int sid = get_bv_sid(1);
383 int nid_a = get_sig_nid(cell->getPort("\\A"));
384 int nid_b = btor_op != "not" ? get_sig_nid(cell->getPort("\\B")) : 0;
385
386 if (GetSize(cell->getPort("\\A")) > 1) {
387 int nid_red_a = next_nid++;
388 btorf("%d redor %d %d\n", nid_red_a, sid, nid_a);
389 nid_a = nid_red_a;
390 }
391
392 if (btor_op != "not" && GetSize(cell->getPort("\\B")) > 1) {
393 int nid_red_b = next_nid++;
394 btorf("%d redor %d %d\n", nid_red_b, sid, nid_b);
395 nid_b = nid_red_b;
396 }
397
398 int nid = next_nid++;
399 if (btor_op != "not")
400 btorf("%d %s %d %d %d\n", nid, btor_op.c_str(), sid, nid_a, nid_b);
401 else
402 btorf("%d %s %d %d\n", nid, btor_op.c_str(), sid, nid_a);
403
404 SigSpec sig = sigmap(cell->getPort("\\Y"));
405
406 if (GetSize(sig) > 1) {
407 int sid = get_bv_sid(GetSize(sig));
408 int zeros_nid = get_sig_nid(Const(0, GetSize(sig)-1));
409 int nid2 = next_nid++;
410 btorf("%d concat %d %d %d\n", nid2, sid, zeros_nid, nid);
411 nid = nid2;
412 }
413
414 add_nid_sig(nid, sig);
415 goto okay;
416 }
417
418 if (cell->type.in("$reduce_and", "$reduce_or", "$reduce_bool", "$reduce_xor", "$reduce_xnor"))
419 {
420 string btor_op;
421 if (cell->type == "$reduce_and") btor_op = "redand";
422 if (cell->type.in("$reduce_or", "$reduce_bool")) btor_op = "redor";
423 if (cell->type.in("$reduce_xor", "$reduce_xnor")) btor_op = "redxor";
424 log_assert(!btor_op.empty());
425
426 int sid = get_bv_sid(1);
427 int nid_a = get_sig_nid(cell->getPort("\\A"));
428
429 int nid = next_nid++;
430 btorf("%d %s %d %d\n", nid, btor_op.c_str(), sid, nid_a);
431
432 if (cell->type == "$reduce_xnor") {
433 int nid2 = next_nid++;
434 btorf("%d not %d %d %d\n", nid2, sid, nid);
435 nid = nid2;
436 }
437
438 SigSpec sig = sigmap(cell->getPort("\\Y"));
439
440 if (GetSize(sig) > 1) {
441 int sid = get_bv_sid(GetSize(sig));
442 int zeros_nid = get_sig_nid(Const(0, GetSize(sig)-1));
443 int nid2 = next_nid++;
444 btorf("%d concat %d %d %d\n", nid2, sid, zeros_nid, nid);
445 nid = nid2;
446 }
447
448 add_nid_sig(nid, sig);
449 goto okay;
450 }
451
452 if (cell->type.in("$mux", "$_MUX_"))
453 {
454 SigSpec sig_a = sigmap(cell->getPort("\\A"));
455 SigSpec sig_b = sigmap(cell->getPort("\\B"));
456 SigSpec sig_s = sigmap(cell->getPort("\\S"));
457 SigSpec sig_y = sigmap(cell->getPort("\\Y"));
458
459 int nid_a = get_sig_nid(sig_a);
460 int nid_b = get_sig_nid(sig_b);
461 int nid_s = get_sig_nid(sig_s);
462
463 int sid = get_bv_sid(GetSize(sig_y));
464 int nid = next_nid++;
465 btorf("%d ite %d %d %d %d\n", nid, sid, nid_s, nid_b, nid_a);
466
467 add_nid_sig(nid, sig_y);
468 goto okay;
469 }
470
471 if (cell->type == "$pmux")
472 {
473 SigSpec sig_a = sigmap(cell->getPort("\\A"));
474 SigSpec sig_b = sigmap(cell->getPort("\\B"));
475 SigSpec sig_s = sigmap(cell->getPort("\\S"));
476 SigSpec sig_y = sigmap(cell->getPort("\\Y"));
477
478 int width = GetSize(sig_a);
479 int sid = get_bv_sid(width);
480 int nid = get_sig_nid(sig_a);
481
482 for (int i = 0; i < GetSize(sig_s); i++) {
483 int nid_b = get_sig_nid(sig_b.extract(i*width, width));
484 int nid_s = get_sig_nid(sig_s.extract(i));
485 int nid2 = next_nid++;
486 btorf("%d ite %d %d %d %d\n", nid2, sid, nid_s, nid_b, nid);
487 nid = nid2;
488 }
489
490 add_nid_sig(nid, sig_y);
491 goto okay;
492 }
493
494 if (cell->type.in("$dff", "$ff", "$_DFF_P_", "$_DFF_N", "$_FF_"))
495 {
496 SigSpec sig_d = sigmap(cell->getPort("\\D"));
497 SigSpec sig_q = sigmap(cell->getPort("\\Q"));
498
499 IdString symbol;
500
501 if (sig_q.is_wire()) {
502 Wire *w = sig_q.as_wire();
503 if (w->port_id == 0) {
504 statewires.insert(w);
505 symbol = w->name;
506 }
507 }
508
509 int sid = get_bv_sid(GetSize(sig_q));
510 int nid = next_nid++;
511
512 if (symbol.empty())
513 btorf("%d state %d\n", nid, sid);
514 else
515 btorf("%d state %d %s\n", nid, sid, log_id(symbol));
516
517 Const initval;
518 for (int i = 0; i < GetSize(sig_q); i++)
519 if (initbits.count(sig_q[i]))
520 initval.bits.push_back(initbits.at(sig_q[i]) ? State::S1 : State::S0);
521 else
522 initval.bits.push_back(State::Sx);
523
524 if (!initval.is_fully_undef()) {
525 int nid_init_val = get_sig_nid(initval);
526 int nid_init = next_nid++;
527 if (verbose)
528 btorf("; initval = %s\n", log_signal(initval));
529 btorf("%d init %d %d %d\n", nid_init, sid, nid, nid_init_val);
530 }
531
532 ff_todo.push_back(make_pair(nid, cell));
533 add_nid_sig(nid, sig_q);
534 goto okay;
535 }
536
537 if (cell->type.in("$anyconst", "$anyseq"))
538 {
539 SigSpec sig_y = sigmap(cell->getPort("\\Y"));
540
541 int sid = get_bv_sid(GetSize(sig_y));
542 int nid = next_nid++;
543
544 btorf("%d state %d\n", nid, sid);
545
546 if (cell->type == "$anyconst") {
547 int nid2 = next_nid++;
548 btorf("%d next %d %d %d\n", nid2, sid, nid, nid);
549 }
550
551 add_nid_sig(nid, sig_y);
552 goto okay;
553 }
554
555 if (cell->type == "$initstate")
556 {
557 SigSpec sig_y = sigmap(cell->getPort("\\Y"));
558
559 if (initstate_nid < 0)
560 {
561 int sid = get_bv_sid(1);
562 int one_nid = get_sig_nid(Const(1, 1));
563 int zero_nid = get_sig_nid(Const(0, 1));
564 initstate_nid = next_nid++;
565 btorf("%d state %d\n", initstate_nid, sid);
566 btorf("%d init %d %d %d\n", next_nid++, sid, initstate_nid, one_nid);
567 btorf("%d next %d %d %d\n", next_nid++, sid, initstate_nid, zero_nid);
568 }
569
570 add_nid_sig(initstate_nid, sig_y);
571 goto okay;
572 }
573
574 if (cell->type == "$mem")
575 {
576 int abits = cell->getParam("\\ABITS").as_int();
577 int width = cell->getParam("\\WIDTH").as_int();
578 int rdports = cell->getParam("\\RD_PORTS").as_int();
579 int wrports = cell->getParam("\\WR_PORTS").as_int();
580
581 Const wr_clk_en = cell->getParam("\\WR_CLK_ENABLE");
582 Const rd_clk_en = cell->getParam("\\RD_CLK_ENABLE");
583
584 bool asyncwr = wr_clk_en.is_fully_zero();
585
586 if (!asyncwr && !wr_clk_en.is_fully_ones())
587 log_error("Memory %s.%s has mixed async/sync write ports.\n",
588 log_id(module), log_id(cell));
589
590 if (!rd_clk_en.is_fully_zero())
591 log_error("Memory %s.%s has sync read ports.\n",
592 log_id(module), log_id(cell));
593
594 SigSpec sig_rd_addr = sigmap(cell->getPort("\\RD_ADDR"));
595 SigSpec sig_rd_data = sigmap(cell->getPort("\\RD_DATA"));
596
597 SigSpec sig_wr_addr = sigmap(cell->getPort("\\WR_ADDR"));
598 SigSpec sig_wr_data = sigmap(cell->getPort("\\WR_DATA"));
599 SigSpec sig_wr_en = sigmap(cell->getPort("\\WR_EN"));
600
601 int data_sid = get_bv_sid(width);
602 int sid = get_mem_sid(abits, width);
603 int nid = next_nid++;
604 int nid_head = nid;
605
606 if (cell->name[0] == '$')
607 btorf("%d state %d\n", nid, sid);
608 else
609 btorf("%d state %d %s\n", nid, sid, log_id(cell));
610
611 if (asyncwr)
612 {
613 for (int port = 0; port < wrports; port++)
614 {
615 SigSpec wa = sig_wr_addr.extract(port*abits, abits);
616 SigSpec wd = sig_wr_data.extract(port*width, width);
617 SigSpec we = sig_wr_en.extract(port*width, width);
618
619 int wa_nid = get_sig_nid(wa);
620 int wd_nid = get_sig_nid(wd);
621 int we_nid = get_sig_nid(we);
622
623 int nid2 = next_nid++;
624 btorf("%d read %d %d %d\n", nid2, data_sid, nid_head, wa_nid);
625
626 int nid3 = next_nid++;
627 btorf("%d not %d %d %d\n", nid3, data_sid, we_nid);
628
629 int nid4 = next_nid++;
630 btorf("%d and %d %d %d\n", nid4, data_sid, nid2, nid3);
631
632 int nid5 = next_nid++;
633 btorf("%d and %d %d %d\n", nid5, data_sid, wd_nid, we_nid);
634
635 int nid6 = next_nid++;
636 btorf("%d or %d %d %d\n", nid6, data_sid, nid5, nid4);
637
638 int nid7 = next_nid++;
639 btorf("%d write %d %d %d %d\n", nid7, sid, nid_head, wa_nid, nid6);
640
641 nid_head = nid7;
642 }
643 }
644
645 for (int port = 0; port < rdports; port++)
646 {
647 SigSpec ra = sig_rd_addr.extract(port*abits, abits);
648 SigSpec rd = sig_rd_data.extract(port*width, width);
649
650 int ra_nid = get_sig_nid(ra);
651 int rd_nid = next_nid++;
652
653 btorf("%d read %d %d %d\n", rd_nid, data_sid, nid_head, ra_nid);
654
655 add_nid_sig(rd_nid, rd);
656 }
657
658 if (!asyncwr)
659 {
660 ff_todo.push_back(make_pair(nid, cell));
661 }
662 else
663 {
664 int nid2 = next_nid++;
665 btorf("%d next %d %d %d\n", nid2, sid, nid, nid_head);
666 }
667
668 goto okay;
669 }
670
671 log_error("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
672
673 okay:
674 btorf_pop(log_id(cell));
675 cell_recursion_guard.erase(cell);
676 }
677
678 int get_sig_nid(SigSpec sig, int to_width = -1, bool is_signed = false)
679 {
680 int nid = -1;
681 sigmap.apply(sig);
682
683 for (auto bit : sig)
684 if (bit == State::Sx)
685 goto has_undef_bits;
686
687 if (0)
688 {
689 has_undef_bits:
690 SigSpec sig_mask_undef, sig_noundef;
691 int first_undef = -1;
692
693 for (int i = 0; i < GetSize(sig); i++)
694 if (sig[i] == State::Sx) {
695 if (first_undef < 0)
696 first_undef = i;
697 sig_mask_undef.append(State::S1);
698 sig_noundef.append(State::S0);
699 } else {
700 sig_mask_undef.append(State::S0);
701 sig_noundef.append(sig[i]);
702 }
703
704 if (to_width < 0 || first_undef < to_width)
705 {
706 int sid = get_bv_sid(GetSize(sig));
707
708 int nid_input = next_nid++;
709 btorf("%d input %d\n", nid_input, sid);
710
711 int nid_masked_input;
712 if (sig_mask_undef.is_fully_ones()) {
713 nid_masked_input = nid_input;
714 } else {
715 int nid_mask_undef = get_sig_nid(sig_mask_undef);
716 nid_masked_input = next_nid++;
717 btorf("%d and %d %d %d\n", nid_masked_input, sid, nid_input, nid_mask_undef);
718 }
719
720 if (sig_noundef.is_fully_zero()) {
721 nid = nid_masked_input;
722 } else {
723 int nid_noundef = get_sig_nid(sig_noundef);
724 nid = next_nid++;
725 btorf("%d or %d %d %d\n", nid, sid, nid_masked_input, nid_noundef);
726 }
727
728 goto extend_or_trim;
729 }
730
731 sig = sig_noundef;
732 }
733
734 if (sig_nid.count(sig) == 0)
735 {
736 // <nid>, <bitidx>
737 vector<pair<int, int>> nidbits;
738
739 // collect all bits
740 for (int i = 0; i < GetSize(sig); i++)
741 {
742 SigBit bit = sig[i];
743
744 if (bit_nid.count(bit) == 0)
745 {
746 if (bit.wire == nullptr)
747 {
748 Const c(bit.data);
749
750 while (i+GetSize(c) < GetSize(sig) && sig[i+GetSize(c)].wire == nullptr)
751 c.bits.push_back(sig[i+GetSize(c)].data);
752
753 if (consts.count(c) == 0) {
754 int sid = get_bv_sid(GetSize(c));
755 int nid = next_nid++;
756 btorf("%d const %d %s\n", nid, sid, c.as_string().c_str());
757 consts[c] = nid;
758 nid_width[nid] = GetSize(c);
759 }
760
761 int nid = consts.at(c);
762
763 for (int j = 0; j < GetSize(c); j++)
764 nidbits.push_back(make_pair(nid, j));
765
766 i += GetSize(c)-1;
767 continue;
768 }
769 else
770 {
771 export_cell(bit_cell.at(bit));
772 log_assert(bit_nid.count(bit));
773 }
774 }
775
776 nidbits.push_back(bit_nid.at(bit));
777 }
778
779 int width = 0;
780 int nid = -1;
781
782 // group bits and emit slice-concat chain
783 for (int i = 0; i < GetSize(nidbits); i++)
784 {
785 int nid2 = nidbits[i].first;
786 int lower = nidbits[i].second;
787 int upper = lower;
788
789 while (i+1 < GetSize(nidbits) && nidbits[i+1].first == nidbits[i].first &&
790 nidbits[i+1].second == nidbits[i].second+1)
791 upper++, i++;
792
793 int nid3 = nid2;
794
795 if (lower != 0 || upper+1 != nid_width.at(nid2)) {
796 int sid = get_bv_sid(upper-lower+1);
797 nid3 = next_nid++;
798 btorf("%d slice %d %d %d %d\n", nid3, sid, nid2, upper, lower);
799 }
800
801 int nid4 = nid3;
802
803 if (nid >= 0) {
804 int sid = get_bv_sid(width+upper-lower+1);
805 nid4 = next_nid++;
806 btorf("%d concat %d %d %d\n", nid4, sid, nid3, nid);
807 }
808
809 width += upper-lower+1;
810 nid = nid4;
811 }
812
813 sig_nid[sig] = nid;
814 nid_width[nid] = width;
815 }
816
817 nid = sig_nid.at(sig);
818
819 extend_or_trim:
820 if (to_width >= 0 && to_width != GetSize(sig))
821 {
822 if (to_width < GetSize(sig))
823 {
824 int sid = get_bv_sid(to_width);
825 int nid2 = next_nid++;
826 btorf("%d slice %d %d %d 0\n", nid2, sid, nid, to_width-1);
827 nid = nid2;
828 }
829 else
830 {
831 int sid = get_bv_sid(to_width);
832 int nid2 = next_nid++;
833 btorf("%d %s %d %d %d\n", nid2, is_signed ? "sext" : "uext",
834 sid, nid, to_width - GetSize(sig));
835 nid = nid2;
836 }
837 }
838
839 return nid;
840 }
841
842 BtorWorker(std::ostream &f, RTLIL::Module *module, bool verbose, bool single_bad) :
843 f(f), sigmap(module), module(module), verbose(verbose), single_bad(single_bad)
844 {
845 btorf_push("inputs");
846
847 for (auto wire : module->wires())
848 {
849 if (wire->attributes.count("\\init")) {
850 Const attrval = wire->attributes.at("\\init");
851 for (int i = 0; i < GetSize(wire) && i < GetSize(attrval); i++)
852 if (attrval[i] == State::S0 || attrval[i] == State::S1)
853 initbits[sigmap(SigBit(wire, i))] = (attrval[i] == State::S1);
854 }
855
856 if (!wire->port_id || !wire->port_input)
857 continue;
858
859 SigSpec sig = sigmap(wire);
860 int sid = get_bv_sid(GetSize(sig));
861 int nid = next_nid++;
862
863 btorf("%d input %d %s\n", nid, sid, log_id(wire));
864 add_nid_sig(nid, sig);
865 }
866
867 btorf_pop("inputs");
868
869 for (auto cell : module->cells())
870 for (auto &conn : cell->connections())
871 {
872 if (!cell->output(conn.first))
873 continue;
874
875 for (auto bit : sigmap(conn.second))
876 bit_cell[bit] = cell;
877 }
878
879 for (auto wire : module->wires())
880 {
881 if (!wire->port_id || !wire->port_output)
882 continue;
883
884 btorf_push(stringf("output %s", log_id(wire)));
885
886 int sid = get_bv_sid(GetSize(wire));
887 int nid = get_sig_nid(wire);
888 btorf("%d output %d %d %s\n", next_nid++, sid, nid, log_id(wire));
889
890 btorf_pop(stringf("output %s", log_id(wire)));
891 }
892
893 for (auto cell : module->cells())
894 {
895 if (cell->type == "$assume")
896 {
897 btorf_push(log_id(cell));
898
899 int sid = get_bv_sid(1);
900 int nid_a = get_sig_nid(cell->getPort("\\A"));
901 int nid_en = get_sig_nid(cell->getPort("\\EN"));
902 int nid_not_en = next_nid++;
903 int nid_a_or_not_en = next_nid++;
904 int nid = next_nid++;
905
906 btorf("%d not %d %d\n", nid_not_en, sid, nid_en);
907 btorf("%d or %d %d %d\n", nid_a_or_not_en, sid, nid_a, nid_not_en);
908 btorf("%d constraint %d\n", nid, nid_a_or_not_en);
909
910 btorf_pop(log_id(cell));
911 }
912
913 if (cell->type == "$assert")
914 {
915 btorf_push(log_id(cell));
916
917 int sid = get_bv_sid(1);
918 int nid_a = get_sig_nid(cell->getPort("\\A"));
919 int nid_en = get_sig_nid(cell->getPort("\\EN"));
920 int nid_not_a = next_nid++;
921 int nid_en_and_not_a = next_nid++;
922
923 btorf("%d not %d %d\n", nid_not_a, sid, nid_a);
924 btorf("%d and %d %d %d\n", nid_en_and_not_a, sid, nid_en, nid_not_a);
925
926 if (single_bad) {
927 bad_properties.push_back(nid_en_and_not_a);
928 } else {
929 int nid = next_nid++;
930 btorf("%d bad %d\n", nid, nid_en_and_not_a);
931 }
932
933 btorf_pop(log_id(cell));
934 }
935 }
936
937 for (auto wire : module->wires())
938 {
939 if (wire->port_id || wire->name[0] == '$')
940 continue;
941
942 btorf_push(stringf("wire %s", log_id(wire)));
943
944 int sid = get_bv_sid(GetSize(wire));
945 int nid = get_sig_nid(sigmap(wire));
946
947 if (statewires.count(wire))
948 continue;
949
950 int this_nid = next_nid++;
951 btorf("%d uext %d %d %d %s\n", this_nid, sid, nid, 0, log_id(wire));
952
953 btorf_pop(stringf("wire %s", log_id(wire)));
954 continue;
955 }
956
957 while (!ff_todo.empty())
958 {
959 vector<pair<int, Cell*>> todo;
960 todo.swap(ff_todo);
961
962 for (auto &it : todo)
963 {
964 int nid = it.first;
965 Cell *cell = it.second;
966
967 btorf_push(stringf("next %s", log_id(cell)));
968
969 if (cell->type == "$mem")
970 {
971 int abits = cell->getParam("\\ABITS").as_int();
972 int width = cell->getParam("\\WIDTH").as_int();
973 int wrports = cell->getParam("\\WR_PORTS").as_int();
974
975 SigSpec sig_wr_addr = sigmap(cell->getPort("\\WR_ADDR"));
976 SigSpec sig_wr_data = sigmap(cell->getPort("\\WR_DATA"));
977 SigSpec sig_wr_en = sigmap(cell->getPort("\\WR_EN"));
978
979 int data_sid = get_bv_sid(width);
980 int sid = get_mem_sid(abits, width);
981 int nid_head = nid;
982
983 for (int port = 0; port < wrports; port++)
984 {
985 SigSpec wa = sig_wr_addr.extract(port*abits, abits);
986 SigSpec wd = sig_wr_data.extract(port*width, width);
987 SigSpec we = sig_wr_en.extract(port*width, width);
988
989 int wa_nid = get_sig_nid(wa);
990 int wd_nid = get_sig_nid(wd);
991 int we_nid = get_sig_nid(we);
992
993 int nid2 = next_nid++;
994 btorf("%d read %d %d %d\n", nid2, data_sid, nid_head, wa_nid);
995
996 int nid3 = next_nid++;
997 btorf("%d not %d %d %d\n", nid3, data_sid, we_nid);
998
999 int nid4 = next_nid++;
1000 btorf("%d and %d %d %d\n", nid4, data_sid, nid2, nid3);
1001
1002 int nid5 = next_nid++;
1003 btorf("%d and %d %d %d\n", nid5, data_sid, wd_nid, we_nid);
1004
1005 int nid6 = next_nid++;
1006 btorf("%d or %d %d %d\n", nid6, data_sid, nid5, nid4);
1007
1008 int nid7 = next_nid++;
1009 btorf("%d write %d %d %d %d\n", nid7, sid, nid_head, wa_nid, nid6);
1010
1011 nid_head = nid7;
1012 }
1013
1014 int nid2 = next_nid++;
1015 btorf("%d next %d %d %d\n", nid2, sid, nid, nid_head);
1016 }
1017 else
1018 {
1019 SigSpec sig = sigmap(cell->getPort("\\D"));
1020 int nid_q = get_sig_nid(sig);
1021 int sid = get_bv_sid(GetSize(sig));
1022 btorf("%d next %d %d %d\n", next_nid++, sid, nid, nid_q);
1023 }
1024
1025 btorf_pop(stringf("next %s", log_id(cell)));
1026 }
1027 }
1028
1029 while (!bad_properties.empty())
1030 {
1031 vector<int> todo;
1032 bad_properties.swap(todo);
1033
1034 int sid = get_bv_sid(1);
1035 int cursor = 0;
1036
1037 while (cursor+1 < GetSize(todo))
1038 {
1039 int nid_a = todo[cursor++];
1040 int nid_b = todo[cursor++];
1041 int nid = next_nid++;
1042
1043 bad_properties.push_back(nid);
1044 btorf("%d or %d %d %d\n", nid, sid, nid_a, nid_b);
1045 }
1046
1047 if (!bad_properties.empty()) {
1048 if (cursor < GetSize(todo))
1049 bad_properties.push_back(todo[cursor++]);
1050 log_assert(cursor == GetSize(todo));
1051 } else {
1052 int nid = next_nid++;
1053 log_assert(cursor == 0);
1054 log_assert(GetSize(todo) == 1);
1055 btorf("%d bad %d\n", nid, todo[cursor]);
1056 }
1057 }
1058 }
1059 };
1060
1061 struct BtorBackend : public Backend {
1062 BtorBackend() : Backend("btor", "write design to BTOR file") { }
1063 virtual void help()
1064 {
1065 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1066 log("\n");
1067 log(" write_btor [options] [filename]\n");
1068 log("\n");
1069 log("Write a BTOR description of the current design.\n");
1070 log("\n");
1071 log(" -v\n");
1072 log(" Add comments and indentation to BTOR output file\n");
1073 log("\n");
1074 log(" -s\n");
1075 log(" Output only a single bad property for all asserts\n");
1076 log("\n");
1077 }
1078 virtual void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
1079 {
1080 bool verbose = false, single_bad = false;
1081
1082 log_header(design, "Executing BTOR backend.\n");
1083
1084 size_t argidx;
1085 for (argidx = 1; argidx < args.size(); argidx++)
1086 {
1087 if (args[argidx] == "-v") {
1088 verbose = true;
1089 continue;
1090 }
1091 if (args[argidx] == "-s") {
1092 single_bad = true;
1093 continue;
1094 }
1095 break;
1096 }
1097 extra_args(f, filename, args, argidx);
1098
1099 RTLIL::Module *topmod = design->top_module();
1100
1101 if (topmod == nullptr)
1102 log_cmd_error("No top module found.\n");
1103
1104 *f << stringf("; BTOR description generated by %s for module %s.\n",
1105 yosys_version_str, log_id(topmod));
1106
1107 BtorWorker(*f, topmod, verbose, single_bad);
1108
1109 *f << stringf("; end of yosys output\n");
1110 }
1111 } BtorBackend;
1112
1113 PRIVATE_NAMESPACE_END