2017-11-22 23:38:57 -06:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/sigtools.h"
|
|
|
|
#include "kernel/celltypes.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
struct BtorWorker
|
|
|
|
{
|
|
|
|
std::ostream &f;
|
|
|
|
SigMap sigmap;
|
|
|
|
RTLIL::Module *module;
|
|
|
|
bool verbose;
|
2017-11-23 11:50:10 -06:00
|
|
|
|
|
|
|
int next_nid = 1;
|
|
|
|
int initstate_nid = -1;
|
2017-11-22 23:38:57 -06:00
|
|
|
|
|
|
|
// <width> => <sid>
|
|
|
|
dict<int, int> sorts_bv;
|
|
|
|
|
|
|
|
// (<address-width>, <data-width>) => <sid>
|
|
|
|
dict<pair<int, int>, int> sorts_mem;
|
|
|
|
|
|
|
|
// SigBit => (<nid>, <bitidx>)
|
|
|
|
dict<SigBit, pair<int, int>> bit_nid;
|
|
|
|
|
|
|
|
// <nid> => <bvwidth>
|
|
|
|
dict<int, int> nid_width;
|
|
|
|
|
|
|
|
// SigSpec => <nid>
|
|
|
|
dict<SigSpec, int> sig_nid;
|
|
|
|
|
|
|
|
// bit to driving cell
|
|
|
|
dict<SigBit, Cell*> bit_cell;
|
|
|
|
|
2017-11-23 01:28:29 -06:00
|
|
|
// nids for constants
|
|
|
|
dict<Const, int> consts;
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
// ff inputs that need to be evaluated (<nid>, <ff_cell>)
|
|
|
|
vector<pair<int, Cell*>> ff_todo;
|
2017-11-23 11:14:53 -06:00
|
|
|
|
2017-11-23 01:28:29 -06:00
|
|
|
pool<Cell*> cell_recursion_guard;
|
2017-11-23 16:44:39 -06:00
|
|
|
pool<string> output_symbols;
|
|
|
|
string indent;
|
|
|
|
|
|
|
|
void btorf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
f << indent << vstringf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void btorf_push(const string &id)
|
|
|
|
{
|
|
|
|
if (verbose) {
|
|
|
|
f << indent << stringf(" ; begin %s\n", id.c_str());
|
|
|
|
indent += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void btorf_pop(const string &id)
|
|
|
|
{
|
|
|
|
if (verbose) {
|
|
|
|
indent = indent.substr(4);
|
|
|
|
f << indent << stringf(" ; end %s\n", id.c_str());
|
|
|
|
}
|
|
|
|
}
|
2017-11-23 01:28:29 -06:00
|
|
|
|
2017-11-22 23:38:57 -06:00
|
|
|
int get_bv_sid(int width)
|
|
|
|
{
|
|
|
|
if (sorts_bv.count(width) == 0) {
|
|
|
|
int nid = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d sort bitvec %d\n", nid, width);
|
2017-11-22 23:38:57 -06:00
|
|
|
sorts_bv[width] = nid;
|
|
|
|
}
|
|
|
|
return sorts_bv.at(width);
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
void add_nid_sig(int nid, const SigSpec &sig)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < GetSize(sig); i++)
|
|
|
|
bit_nid[sig[i]] = make_pair(nid, i);
|
|
|
|
|
|
|
|
sig_nid[sig] = nid;
|
|
|
|
nid_width[nid] = GetSize(sig);
|
|
|
|
}
|
|
|
|
|
2017-11-23 01:28:29 -06:00
|
|
|
void export_cell(Cell *cell)
|
|
|
|
{
|
|
|
|
log_assert(cell_recursion_guard.count(cell) == 0);
|
|
|
|
cell_recursion_guard.insert(cell);
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_push(log_id(cell));
|
2017-11-23 01:28:29 -06:00
|
|
|
|
2017-12-10 01:40:11 -06:00
|
|
|
if (cell->type.in("$add", "$sub", "$and", "$or", "$xor", "$xnor", "$shl", "$sshl", "$shr", "$sshr", "$shift", "$shiftx",
|
2017-12-10 00:16:47 -06:00
|
|
|
"$_AND_", "$_NAND_", "$_OR_", "$_NOR_", "$_XOR_", "$_XNOR_"))
|
2017-11-23 01:28:29 -06:00
|
|
|
{
|
|
|
|
string btor_op;
|
|
|
|
if (cell->type == "$add") btor_op = "add";
|
|
|
|
if (cell->type == "$sub") btor_op = "sub";
|
2017-12-10 00:16:47 -06:00
|
|
|
if (cell->type.in("$shl", "$sshl")) btor_op = "sll";
|
|
|
|
if (cell->type == "$shr") btor_op = "srl";
|
|
|
|
if (cell->type == "$sshr") btor_op = "sra";
|
2017-12-11 07:24:19 -06:00
|
|
|
if (cell->type.in("$shift", "$shiftx")) btor_op = "shift";
|
2017-12-10 00:16:47 -06:00
|
|
|
if (cell->type.in("$and", "$_AND_")) btor_op = "and";
|
|
|
|
if (cell->type.in("$or", "$_OR_")) btor_op = "or";
|
|
|
|
if (cell->type.in("$xor", "$_XOR_")) btor_op = "xor";
|
|
|
|
if (cell->type == "$_NAND_") btor_op = "nand";
|
|
|
|
if (cell->type == "$_NOR_") btor_op = "nor";
|
|
|
|
if (cell->type.in("$xnor", "$_XNOR_")) btor_op = "xnor";
|
2017-11-23 01:28:29 -06:00
|
|
|
log_assert(!btor_op.empty());
|
|
|
|
|
|
|
|
int width = GetSize(cell->getPort("\\Y"));
|
|
|
|
width = std::max(width, GetSize(cell->getPort("\\A")));
|
|
|
|
width = std::max(width, GetSize(cell->getPort("\\B")));
|
|
|
|
|
|
|
|
bool a_signed = cell->hasParam("\\A_SIGNED") ? cell->getParam("\\A_SIGNED").as_bool() : false;
|
|
|
|
bool b_signed = cell->hasParam("\\B_SIGNED") ? cell->getParam("\\B_SIGNED").as_bool() : false;
|
|
|
|
|
2017-12-11 07:24:19 -06:00
|
|
|
if (btor_op == "shift" && !b_signed)
|
|
|
|
btor_op = "srl";
|
|
|
|
|
2017-12-10 01:40:11 -06:00
|
|
|
if (cell->type.in("$shl", "$sshl", "$shr", "$sshr"))
|
2017-12-10 00:16:47 -06:00
|
|
|
b_signed = false;
|
2017-12-10 01:40:11 -06:00
|
|
|
|
|
|
|
if (cell->type == "$sshr" && !a_signed)
|
|
|
|
btor_op = "srl";
|
2017-12-10 00:16:47 -06:00
|
|
|
|
2017-11-23 01:28:29 -06:00
|
|
|
int sid = get_bv_sid(width);
|
2017-12-11 07:24:19 -06:00
|
|
|
int nid;
|
2017-11-23 01:28:29 -06:00
|
|
|
|
2017-12-11 07:24:19 -06:00
|
|
|
if (btor_op == "shift")
|
|
|
|
{
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"), width, false);
|
|
|
|
int nid_b = get_sig_nid(cell->getPort("\\B"), width, b_signed);
|
|
|
|
|
|
|
|
int nid_r = next_nid++;
|
|
|
|
btorf("%d srl %d %d %d\n", nid_r, sid, nid_a, nid_b);
|
|
|
|
|
|
|
|
int nid_b_neg = next_nid++;
|
|
|
|
btorf("%d neg %d %d\n", nid_b_neg, sid, nid_b);
|
|
|
|
|
|
|
|
int nid_l = next_nid++;
|
|
|
|
btorf("%d sll %d %d %d\n", nid_l, sid, nid_a, nid_b_neg);
|
|
|
|
|
|
|
|
int sid_bit = get_bv_sid(1);
|
|
|
|
int nid_zero = get_sig_nid(Const(0, width));
|
|
|
|
int nid_b_ltz = next_nid++;
|
|
|
|
btorf("%d slt %d %d %d\n", nid_b_ltz, sid_bit, nid_b, nid_zero);
|
|
|
|
|
|
|
|
nid = next_nid++;
|
|
|
|
btorf("%d ite %d %d %d %d\n", nid, sid, nid_b_ltz, nid_l, nid_r);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"), width, a_signed);
|
|
|
|
int nid_b = get_sig_nid(cell->getPort("\\B"), width, b_signed);
|
|
|
|
|
|
|
|
nid = next_nid++;
|
|
|
|
btorf("%d %s %d %d %d\n", nid, btor_op.c_str(), sid, nid_a, nid_b);
|
|
|
|
}
|
2017-11-23 01:28:29 -06:00
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
if (GetSize(sig) < width) {
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
int nid2 = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d slice %d %d %d 0\n", nid2, sid, nid, GetSize(sig)-1);
|
2017-11-23 01:28:29 -06:00
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
add_nid_sig(nid, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
2017-11-23 01:28:29 -06:00
|
|
|
|
2017-12-10 00:16:47 -06:00
|
|
|
if (cell->type.in("$_ANDNOT_", "$_ORNOT_"))
|
|
|
|
{
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
|
|
|
int nid_b = get_sig_nid(cell->getPort("\\B"));
|
|
|
|
|
|
|
|
int nid1 = next_nid++;
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
|
|
|
|
if (cell->type == "$_ANDNOT_") {
|
|
|
|
btorf("%d not %d %d\n", nid1, sid, nid_b);
|
|
|
|
btorf("%d and %d %d %d\n", nid2, sid, nid_a, nid1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type == "$_ORNOT_") {
|
|
|
|
btorf("%d not %d %d\n", nid1, sid, nid_b);
|
|
|
|
btorf("%d or %d %d %d\n", nid2, sid, nid_a, nid1);
|
|
|
|
}
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
add_nid_sig(nid2, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in("$_OAI3_", "$_AOI3_"))
|
|
|
|
{
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
|
|
|
int nid_b = get_sig_nid(cell->getPort("\\B"));
|
|
|
|
int nid_c = get_sig_nid(cell->getPort("\\C"));
|
|
|
|
|
|
|
|
int nid1 = next_nid++;
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
int nid3 = next_nid++;
|
|
|
|
|
|
|
|
if (cell->type == "$_OAI3_") {
|
|
|
|
btorf("%d or %d %d %d\n", nid1, sid, nid_a, nid_b);
|
|
|
|
btorf("%d and %d %d %d\n", nid2, sid, nid1, nid_c);
|
|
|
|
btorf("%d not %d %d\n", nid3, sid, nid2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type == "$_AOI3_") {
|
|
|
|
btorf("%d and %d %d %d\n", nid1, sid, nid_a, nid_b);
|
|
|
|
btorf("%d or %d %d %d\n", nid2, sid, nid1, nid_c);
|
|
|
|
btorf("%d not %d %d\n", nid3, sid, nid2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
add_nid_sig(nid3, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in("$_OAI4_", "$_AOI4_"))
|
|
|
|
{
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
|
|
|
int nid_b = get_sig_nid(cell->getPort("\\B"));
|
|
|
|
int nid_c = get_sig_nid(cell->getPort("\\C"));
|
|
|
|
int nid_d = get_sig_nid(cell->getPort("\\D"));
|
|
|
|
|
|
|
|
int nid1 = next_nid++;
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
int nid3 = next_nid++;
|
|
|
|
int nid4 = next_nid++;
|
|
|
|
|
|
|
|
if (cell->type == "$_OAI4_") {
|
|
|
|
btorf("%d or %d %d %d\n", nid1, sid, nid_a, nid_b);
|
|
|
|
btorf("%d or %d %d %d\n", nid2, sid, nid_c, nid_d);
|
|
|
|
btorf("%d and %d %d %d\n", nid3, sid, nid1, nid2);
|
|
|
|
btorf("%d not %d %d\n", nid4, sid, nid3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type == "$_AOI4_") {
|
|
|
|
btorf("%d and %d %d %d\n", nid1, sid, nid_a, nid_b);
|
|
|
|
btorf("%d and %d %d %d\n", nid2, sid, nid_c, nid_d);
|
|
|
|
btorf("%d or %d %d %d\n", nid3, sid, nid1, nid2);
|
|
|
|
btorf("%d not %d %d\n", nid4, sid, nid3);
|
|
|
|
}
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
add_nid_sig(nid4, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in("$lt", "$le", "$eq", "$eqx", "$ne", "$nex", "$ge", "$gt"))
|
|
|
|
{
|
|
|
|
string btor_op;
|
|
|
|
if (cell->type == "$lt") btor_op = "lt";
|
|
|
|
if (cell->type == "$le") btor_op = "lte";
|
|
|
|
if (cell->type.in("$eq", "$eqx")) btor_op = "eq";
|
|
|
|
if (cell->type.in("$ne", "$nex")) btor_op = "ne";
|
|
|
|
if (cell->type == "$ge") btor_op = "gte";
|
|
|
|
if (cell->type == "$gt") btor_op = "gt";
|
|
|
|
log_assert(!btor_op.empty());
|
|
|
|
|
|
|
|
int width = 1;
|
|
|
|
width = std::max(width, GetSize(cell->getPort("\\A")));
|
|
|
|
width = std::max(width, GetSize(cell->getPort("\\B")));
|
|
|
|
|
|
|
|
bool a_signed = cell->hasParam("\\A_SIGNED") ? cell->getParam("\\A_SIGNED").as_bool() : false;
|
|
|
|
bool b_signed = cell->hasParam("\\B_SIGNED") ? cell->getParam("\\B_SIGNED").as_bool() : false;
|
|
|
|
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"), width, a_signed);
|
|
|
|
int nid_b = get_sig_nid(cell->getPort("\\B"), width, b_signed);
|
|
|
|
|
|
|
|
int nid = next_nid++;
|
|
|
|
if (cell->type.in("$lt", "$le", "$ge", "$gt")) {
|
|
|
|
btorf("%d %c%s %d %d %d\n", nid, a_signed || b_signed ? 's' : 'u', btor_op.c_str(), sid, nid_a, nid_b);
|
|
|
|
} else {
|
|
|
|
btorf("%d %s %d %d %d\n", nid, btor_op.c_str(), sid, nid_a, nid_b);
|
|
|
|
}
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
if (GetSize(sig) > 1) {
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
btorf("%d uext %d %d %d\n", nid2, sid, nid, GetSize(sig) - 1);
|
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_nid_sig(nid, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in("$not", "$neg", "$_NOT_"))
|
|
|
|
{
|
|
|
|
string btor_op;
|
|
|
|
if (cell->type.in("$not", "$_NOT_")) btor_op = "not";
|
|
|
|
if (cell->type == "$neg") btor_op = "neg";
|
|
|
|
log_assert(!btor_op.empty());
|
|
|
|
|
|
|
|
int width = GetSize(cell->getPort("\\Y"));
|
|
|
|
width = std::max(width, GetSize(cell->getPort("\\A")));
|
|
|
|
|
|
|
|
bool a_signed = cell->hasParam("\\A_SIGNED") ? cell->getParam("\\A_SIGNED").as_bool() : false;
|
|
|
|
|
|
|
|
int sid = get_bv_sid(width);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"), width, a_signed);
|
|
|
|
|
|
|
|
int nid = next_nid++;
|
|
|
|
btorf("%d %s %d %d\n", nid, btor_op.c_str(), sid, nid_a);
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
if (GetSize(sig) < width) {
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
btorf("%d slice %d %d %d 0\n", nid2, sid, nid, GetSize(sig)-1);
|
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_nid_sig(nid, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in("$logic_and", "$logic_or", "$logic_not"))
|
2017-11-23 11:50:10 -06:00
|
|
|
{
|
|
|
|
string btor_op;
|
|
|
|
if (cell->type == "$logic_and") btor_op = "and";
|
|
|
|
if (cell->type == "$logic_or") btor_op = "or";
|
2017-12-10 00:16:47 -06:00
|
|
|
if (cell->type == "$logic_not") btor_op = "not";
|
2017-11-23 11:50:10 -06:00
|
|
|
log_assert(!btor_op.empty());
|
|
|
|
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
2017-12-10 00:16:47 -06:00
|
|
|
int nid_b = btor_op != "not" ? get_sig_nid(cell->getPort("\\B")) : 0;
|
2017-11-23 11:50:10 -06:00
|
|
|
|
|
|
|
if (GetSize(cell->getPort("\\A")) > 1) {
|
|
|
|
int nid_red_a = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d redor %d %d\n", nid_red_a, sid, nid_a);
|
2017-11-23 11:50:10 -06:00
|
|
|
nid_a = nid_red_a;
|
|
|
|
}
|
|
|
|
|
2017-12-10 00:16:47 -06:00
|
|
|
if (btor_op != "not" && GetSize(cell->getPort("\\B")) > 1) {
|
2017-11-23 11:50:10 -06:00
|
|
|
int nid_red_b = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d redor %d %d\n", nid_red_b, sid, nid_b);
|
2017-11-23 11:50:10 -06:00
|
|
|
nid_b = nid_red_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nid = next_nid++;
|
2017-12-10 00:16:47 -06:00
|
|
|
if (btor_op != "not")
|
|
|
|
btorf("%d %s %d %d %d\n", nid, btor_op.c_str(), sid, nid_a, nid_b);
|
|
|
|
else
|
|
|
|
btorf("%d %s %d %d\n", nid, btor_op.c_str(), sid, nid_a);
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
if (GetSize(sig) > 1) {
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
int zeros_nid = get_sig_nid(Const(0, GetSize(sig)-1));
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
btorf("%d concat %d %d %d\n", nid2, sid, zeros_nid, nid);
|
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_nid_sig(nid, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type.in("$reduce_and", "$reduce_or", "$reduce_bool", "$reduce_xor", "$reduce_xnor"))
|
|
|
|
{
|
|
|
|
string btor_op;
|
|
|
|
if (cell->type == "$reduce_and") btor_op = "redand";
|
|
|
|
if (cell->type.in("$reduce_or", "$reduce_bool")) btor_op = "redor";
|
|
|
|
if (cell->type.in("$reduce_xor", "$reduce_xnor")) btor_op = "redxor";
|
|
|
|
log_assert(!btor_op.empty());
|
|
|
|
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
|
|
|
|
|
|
|
int nid = next_nid++;
|
|
|
|
btorf("%d %s %d %d\n", nid, btor_op.c_str(), sid, nid_a);
|
|
|
|
|
|
|
|
if (cell->type == "$reduce_xnor") {
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
btorf("%d not %d %d %d\n", nid2, sid, nid);
|
|
|
|
nid = nid2;
|
|
|
|
}
|
2017-11-23 11:50:10 -06:00
|
|
|
|
|
|
|
SigSpec sig = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
if (GetSize(sig) > 1) {
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
int zeros_nid = get_sig_nid(Const(0, GetSize(sig)-1));
|
|
|
|
int nid2 = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d concat %d %d %d\n", nid2, sid, zeros_nid, nid);
|
2017-11-23 11:50:10 -06:00
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_nid_sig(nid, sig);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
if (cell->type.in("$mux", "$_MUX_"))
|
|
|
|
{
|
|
|
|
SigSpec sig_a = sigmap(cell->getPort("\\A"));
|
|
|
|
SigSpec sig_b = sigmap(cell->getPort("\\B"));
|
|
|
|
SigSpec sig_s = sigmap(cell->getPort("\\S"));
|
|
|
|
SigSpec sig_y = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
int nid_a = get_sig_nid(sig_a);
|
|
|
|
int nid_b = get_sig_nid(sig_b);
|
|
|
|
int nid_s = get_sig_nid(sig_s);
|
|
|
|
|
|
|
|
int sid = get_bv_sid(GetSize(sig_y));
|
|
|
|
int nid = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d ite %d %d %d %d\n", nid, sid, nid_s, nid_b, nid_a);
|
2017-11-23 01:28:29 -06:00
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
add_nid_sig(nid, sig_y);
|
2017-11-23 01:28:29 -06:00
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
2017-12-10 01:11:08 -06:00
|
|
|
if (cell->type == "$pmux")
|
|
|
|
{
|
|
|
|
SigSpec sig_a = sigmap(cell->getPort("\\A"));
|
|
|
|
SigSpec sig_b = sigmap(cell->getPort("\\B"));
|
|
|
|
SigSpec sig_s = sigmap(cell->getPort("\\S"));
|
|
|
|
SigSpec sig_y = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
int width = GetSize(sig_a);
|
|
|
|
int sid = get_bv_sid(width);
|
|
|
|
int nid = get_sig_nid(sig_a);
|
|
|
|
|
|
|
|
for (int i = 0; i < GetSize(sig_s); i++) {
|
|
|
|
int nid_b = get_sig_nid(sig_b.extract(i*width, width));
|
|
|
|
int nid_s = get_sig_nid(sig_s.extract(i));
|
|
|
|
int nid2 = next_nid++;
|
|
|
|
btorf("%d ite %d %d %d %d\n", nid2, sid, nid_s, nid_b, nid);
|
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_nid_sig(nid, sig_y);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
if (cell->type.in("$dff", "$ff", "$_DFF_P_", "$_DFF_N", "$_FF_"))
|
|
|
|
{
|
|
|
|
SigSpec sig_d = sigmap(cell->getPort("\\D"));
|
|
|
|
SigSpec sig_q = sigmap(cell->getPort("\\Q"));
|
|
|
|
|
|
|
|
string symbol = log_signal(sig_q);
|
|
|
|
if (symbol.find(' ') != string::npos)
|
|
|
|
symbol = log_id(cell);
|
|
|
|
|
|
|
|
if (symbol[0] == '\\')
|
|
|
|
symbol = symbol.substr(1);
|
|
|
|
|
|
|
|
int sid = get_bv_sid(GetSize(sig_q));
|
|
|
|
int nid = next_nid++;
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
if (output_symbols.count(symbol))
|
|
|
|
btorf("%d state %d\n", nid, sid);
|
|
|
|
else
|
|
|
|
btorf("%d state %d %s\n", nid, sid, symbol.c_str());
|
|
|
|
|
|
|
|
ff_todo.push_back(make_pair(nid, cell));
|
2017-11-23 11:14:53 -06:00
|
|
|
add_nid_sig(nid, sig_q);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:50:10 -06:00
|
|
|
if (cell->type == "$initstate")
|
|
|
|
{
|
|
|
|
SigSpec sig_y = sigmap(cell->getPort("\\Y"));
|
|
|
|
|
|
|
|
if (initstate_nid < 0)
|
|
|
|
{
|
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int one_nid = get_sig_nid(Const(1, 1));
|
|
|
|
int zero_nid = get_sig_nid(Const(0, 1));
|
|
|
|
initstate_nid = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d state %d\n", initstate_nid, sid);
|
|
|
|
btorf("%d init %d %d %d\n", next_nid++, sid, initstate_nid, one_nid);
|
|
|
|
btorf("%d next %d %d %d\n", next_nid++, sid, initstate_nid, zero_nid);
|
2017-11-23 11:50:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
add_nid_sig(initstate_nid, sig_y);
|
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
log_error("Unsupported cell type: %s (%s)\n", log_id(cell->type), log_id(cell));
|
2017-11-23 01:28:29 -06:00
|
|
|
|
|
|
|
okay:
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_pop(log_id(cell));
|
2017-11-23 01:28:29 -06:00
|
|
|
cell_recursion_guard.erase(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_sig_nid(SigSpec sig, int to_width = -1, bool is_signed = false)
|
2017-11-22 23:38:57 -06:00
|
|
|
{
|
|
|
|
sigmap.apply(sig);
|
|
|
|
|
|
|
|
if (sig_nid.count(sig) == 0)
|
|
|
|
{
|
|
|
|
// <nid>, <bitidx>
|
|
|
|
vector<pair<int, int>> nidbits;
|
|
|
|
|
|
|
|
// collect all bits
|
|
|
|
for (int i = 0; i < GetSize(sig); i++)
|
|
|
|
{
|
|
|
|
SigBit bit = sig[i];
|
|
|
|
|
|
|
|
if (bit_nid.count(bit) == 0)
|
|
|
|
{
|
2017-11-23 01:28:29 -06:00
|
|
|
if (bit.wire == nullptr)
|
|
|
|
{
|
|
|
|
Const c(bit.data);
|
|
|
|
|
|
|
|
while (i+GetSize(c) < GetSize(sig) && sig[i+GetSize(c)].wire == nullptr)
|
|
|
|
c.bits.push_back(sig[i+GetSize(c)].data);
|
|
|
|
|
|
|
|
if (consts.count(c) == 0) {
|
|
|
|
int sid = get_bv_sid(GetSize(c));
|
|
|
|
int nid = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d const %d %s\n", nid, sid, c.as_string().c_str());
|
2017-11-23 01:28:29 -06:00
|
|
|
consts[c] = nid;
|
2017-11-23 11:14:53 -06:00
|
|
|
nid_width[nid] = GetSize(c);
|
2017-11-23 01:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int nid = consts.at(c);
|
|
|
|
|
|
|
|
for (int j = 0; j < GetSize(c); j++)
|
|
|
|
nidbits.push_back(make_pair(nid, j));
|
|
|
|
|
|
|
|
i += GetSize(c)-1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
export_cell(bit_cell.at(bit));
|
|
|
|
log_assert(bit_nid.count(bit));
|
|
|
|
}
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
nidbits.push_back(bit_nid.at(bit));
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
int nid = -1;
|
|
|
|
|
|
|
|
// group bits and emit slice-concat chain
|
|
|
|
for (int i = 0; i < GetSize(nidbits); i++)
|
|
|
|
{
|
|
|
|
int nid2 = nidbits[i].first;
|
|
|
|
int lower = nidbits[i].second;
|
|
|
|
int upper = lower;
|
|
|
|
|
|
|
|
while (i+1 < GetSize(nidbits) && nidbits[i+1].first == nidbits[i].first &&
|
|
|
|
nidbits[i+1].second == nidbits[i].second+1)
|
|
|
|
upper++, i++;
|
|
|
|
|
|
|
|
int nid3 = nid2;
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
if (lower != 0 || upper+1 != nid_width.at(nid2)) {
|
2017-11-22 23:38:57 -06:00
|
|
|
int sid = get_bv_sid(upper-lower+1);
|
|
|
|
nid3 = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d slice %d %d %d %d\n", nid3, sid, nid2, upper, lower);
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
int nid4 = nid3;
|
|
|
|
|
|
|
|
if (nid >= 0) {
|
|
|
|
int sid = get_bv_sid(width+upper-lower+1);
|
2017-11-24 11:13:41 -06:00
|
|
|
nid4 = next_nid++;
|
2017-12-08 22:58:14 -06:00
|
|
|
btorf("%d concat %d %d %d\n", nid4, sid, nid3, nid);
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
width += upper-lower+1;
|
|
|
|
nid = nid4;
|
|
|
|
}
|
|
|
|
|
|
|
|
sig_nid[sig] = nid;
|
|
|
|
nid_width[nid] = width;
|
|
|
|
}
|
|
|
|
|
2017-11-23 01:28:29 -06:00
|
|
|
int nid = sig_nid.at(sig);
|
|
|
|
|
|
|
|
if (to_width >= 0 && to_width != GetSize(sig))
|
|
|
|
{
|
|
|
|
if (to_width < GetSize(sig))
|
|
|
|
{
|
|
|
|
int sid = get_bv_sid(to_width);
|
|
|
|
int nid2 = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d slice %d %d %d 0\n", nid2, sid, nid, to_width-1);
|
2017-11-23 01:28:29 -06:00
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int sid = get_bv_sid(to_width);
|
|
|
|
int nid2 = next_nid++;
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d %s %d %d %d\n", nid2, is_signed ? "sext" : "uext",
|
2017-11-23 01:28:29 -06:00
|
|
|
sid, nid, to_width - GetSize(sig));
|
|
|
|
nid = nid2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nid;
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
BtorWorker(std::ostream &f, RTLIL::Module *module, bool verbose) :
|
2017-11-23 11:50:10 -06:00
|
|
|
f(f), sigmap(module), module(module), verbose(verbose)
|
2017-11-22 23:38:57 -06:00
|
|
|
{
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_push("inputs");
|
|
|
|
|
2017-11-22 23:38:57 -06:00
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
|
|
|
if (!wire->port_id || !wire->port_input)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(wire);
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
int nid = next_nid++;
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d input %d %s\n", nid, sid, log_id(wire));
|
2017-11-23 11:14:53 -06:00
|
|
|
add_nid_sig(nid, sig);
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_pop("inputs");
|
|
|
|
|
2017-11-22 23:38:57 -06:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
for (auto &conn : cell->connections())
|
|
|
|
{
|
|
|
|
if (!cell->output(conn.first))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto bit : sigmap(conn.second))
|
|
|
|
bit_cell[bit] = cell;
|
|
|
|
}
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
for (auto wire : module->wires())
|
|
|
|
if (wire->port_output)
|
|
|
|
output_symbols.insert(log_id(wire));
|
|
|
|
|
2017-11-22 23:38:57 -06:00
|
|
|
for (auto wire : module->wires())
|
|
|
|
{
|
|
|
|
if (!wire->port_id || !wire->port_output)
|
|
|
|
continue;
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_push(stringf("output %s", log_id(wire)));
|
|
|
|
|
2017-11-24 11:13:41 -06:00
|
|
|
int sid = get_bv_sid(GetSize(wire));
|
2017-11-22 23:38:57 -06:00
|
|
|
int nid = get_sig_nid(wire);
|
2017-11-24 11:13:41 -06:00
|
|
|
btorf("%d output %d %d %s\n", next_nid++, sid, nid, log_id(wire));
|
2017-11-23 16:44:39 -06:00
|
|
|
|
|
|
|
btorf_pop(stringf("output %s", log_id(wire)));
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
2017-11-23 11:14:53 -06:00
|
|
|
|
2017-11-23 11:50:10 -06:00
|
|
|
for (auto cell : module->cells())
|
|
|
|
{
|
|
|
|
if (cell->type == "$assume")
|
|
|
|
{
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_push(log_id(cell));
|
|
|
|
|
2017-11-23 11:50:10 -06:00
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
|
|
|
int nid_en = get_sig_nid(cell->getPort("\\EN"));
|
|
|
|
int nid_not_en = next_nid++;
|
|
|
|
int nid_a_or_not_en = next_nid++;
|
|
|
|
int nid = next_nid++;
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d not %d %d\n", nid_not_en, sid, nid_en);
|
|
|
|
btorf("%d or %d %d %d\n", nid_a_or_not_en, sid, nid_a, nid_not_en);
|
|
|
|
btorf("%d constraint %d\n", nid, nid_a_or_not_en);
|
|
|
|
|
|
|
|
btorf_pop(log_id(cell));
|
2017-11-23 11:50:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cell->type == "$assert")
|
|
|
|
{
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_push(log_id(cell));
|
|
|
|
|
2017-11-23 11:50:10 -06:00
|
|
|
int sid = get_bv_sid(1);
|
|
|
|
int nid_a = get_sig_nid(cell->getPort("\\A"));
|
|
|
|
int nid_en = get_sig_nid(cell->getPort("\\EN"));
|
|
|
|
int nid_not_a = next_nid++;
|
|
|
|
int nid_en_and_not_a = next_nid++;
|
|
|
|
int nid = next_nid++;
|
|
|
|
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf("%d not %d %d\n", nid_not_a, sid, nid_a);
|
|
|
|
btorf("%d and %d %d %d\n", nid_en_and_not_a, sid, nid_en, nid_not_a);
|
|
|
|
btorf("%d bad %d\n", nid, nid_en_and_not_a);
|
|
|
|
|
|
|
|
btorf_pop(log_id(cell));
|
2017-11-23 11:50:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 11:14:53 -06:00
|
|
|
while (!ff_todo.empty())
|
|
|
|
{
|
2017-11-23 16:44:39 -06:00
|
|
|
vector<pair<int, Cell*>> todo;
|
2017-11-23 11:14:53 -06:00
|
|
|
todo.swap(ff_todo);
|
|
|
|
|
|
|
|
for (auto &it : todo)
|
|
|
|
{
|
2017-11-23 16:44:39 -06:00
|
|
|
btorf_push(stringf("next %s", log_id(it.second)));
|
|
|
|
|
|
|
|
SigSpec sig = sigmap(it.second->getPort("\\D"));
|
|
|
|
|
|
|
|
int nid = get_sig_nid(sig);
|
|
|
|
int sid = get_bv_sid(GetSize(sig));
|
|
|
|
btorf("%d next %d %d %d\n", next_nid++, sid, it.first, nid);
|
|
|
|
|
|
|
|
btorf_pop(stringf("next %s", log_id(it.second)));
|
2017-11-23 11:14:53 -06:00
|
|
|
}
|
|
|
|
}
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BtorBackend : public Backend {
|
|
|
|
BtorBackend() : Backend("btor", "write design to BTOR file") { }
|
|
|
|
virtual void help()
|
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" write_btor [options] [filename]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Write a BTOR description of the current design.\n");
|
|
|
|
log("\n");
|
2017-11-23 16:44:39 -06:00
|
|
|
log(" -v\n");
|
|
|
|
log(" Add comments and indentation to BTOR output file\n");
|
|
|
|
log("\n");
|
2017-11-22 23:38:57 -06:00
|
|
|
}
|
|
|
|
virtual void execute(std::ostream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design)
|
|
|
|
{
|
|
|
|
bool verbose = false;
|
|
|
|
|
|
|
|
log_header(design, "Executing BTOR backend.\n");
|
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
2017-11-23 16:44:39 -06:00
|
|
|
if (args[argidx] == "-v") {
|
|
|
|
verbose = true;
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-22 23:38:57 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
extra_args(f, filename, args, argidx);
|
|
|
|
|
|
|
|
RTLIL::Module *topmod = design->top_module();
|
|
|
|
|
|
|
|
if (topmod == nullptr)
|
|
|
|
log_cmd_error("No top module found.\n");
|
|
|
|
|
|
|
|
*f << stringf("; BTOR description generated by %s for module %s.\n",
|
|
|
|
yosys_version_str, log_id(topmod));
|
|
|
|
|
|
|
|
BtorWorker(*f, topmod, verbose);
|
|
|
|
|
|
|
|
*f << stringf("; end of yosys output\n");
|
|
|
|
}
|
|
|
|
} BtorBackend;
|
|
|
|
|
|
|
|
PRIVATE_NAMESPACE_END
|