2008-06-04 15:59:04 +02:00
|
|
|
/*
|
|
|
|
|
* VHDL code generation for expressions.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2008 Nick Gasson (nick@nickg.me.uk)
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vhdl_target.h"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2008-06-04 16:19:44 +02:00
|
|
|
/*
|
|
|
|
|
* Convert a constant Verilog string to a constant VHDL string.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *translate_string(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
// TODO: May need to inspect or escape parts of this
|
|
|
|
|
const char *str = ivl_expr_string(e);
|
|
|
|
|
return new vhdl_const_string(str);
|
|
|
|
|
}
|
2008-06-04 15:59:04 +02:00
|
|
|
|
2008-06-07 12:24:09 +02:00
|
|
|
/*
|
|
|
|
|
* A reference to a signal in an expression. It's assumed that the
|
|
|
|
|
* signal has already been defined elsewhere.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *translate_signal(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
ivl_signal_t sig = ivl_expr_signal(e);
|
2008-06-07 14:23:21 +02:00
|
|
|
|
2008-06-14 19:11:10 +02:00
|
|
|
const vhdl_entity *ent = find_entity_for_signal(sig);
|
|
|
|
|
assert(ent);
|
|
|
|
|
|
|
|
|
|
const char *renamed = get_renamed_signal(sig).c_str();
|
|
|
|
|
|
2008-06-18 14:30:19 +02:00
|
|
|
const vhdl_decl *decl = ent->get_arch()->get_decl(strip_var(renamed));
|
2008-06-14 19:11:10 +02:00
|
|
|
assert(decl);
|
|
|
|
|
|
|
|
|
|
vhdl_type *type = new vhdl_type(*decl->get_type());
|
2008-06-07 14:23:21 +02:00
|
|
|
|
2008-06-14 19:11:10 +02:00
|
|
|
return new vhdl_var_ref(renamed, type);
|
2008-06-07 12:24:09 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-07 17:19:10 +02:00
|
|
|
/*
|
|
|
|
|
* A numeric literal ends up as std_logic bit string.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *translate_number(ivl_expr_t e)
|
|
|
|
|
{
|
2008-06-16 15:26:38 +02:00
|
|
|
return new vhdl_const_bits(ivl_expr_bits(e), ivl_expr_width(e));
|
2008-06-07 17:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-12 11:47:52 +02:00
|
|
|
static vhdl_expr *translate_unary(ivl_expr_t e)
|
|
|
|
|
{
|
2008-06-12 11:56:28 +02:00
|
|
|
vhdl_expr *operand = translate_expr(ivl_expr_oper1(e));
|
|
|
|
|
if (NULL == operand)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
switch (ivl_expr_opcode(e)) {
|
|
|
|
|
case '!':
|
2008-06-19 17:08:33 +02:00
|
|
|
case '~':
|
2008-06-12 11:56:28 +02:00
|
|
|
return new vhdl_unaryop_expr
|
|
|
|
|
(VHDL_UNARYOP_NOT, operand, new vhdl_type(*operand->get_type()));
|
|
|
|
|
default:
|
|
|
|
|
error("No translation for unary opcode '%c'\n",
|
|
|
|
|
ivl_expr_opcode(e));
|
2008-06-14 18:09:31 +02:00
|
|
|
delete operand;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Translate a numeric binary operator (+, -, etc.) to
|
|
|
|
|
* a VHDL equivalent using the numeric_std package.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *translate_numeric(vhdl_expr *lhs, vhdl_expr *rhs,
|
|
|
|
|
vhdl_binop_t op)
|
2008-06-14 19:03:25 +02:00
|
|
|
{
|
2008-06-14 18:09:31 +02:00
|
|
|
int lwidth = lhs->get_type()->get_width();
|
|
|
|
|
int rwidth = rhs->get_type()->get_width();
|
|
|
|
|
|
|
|
|
|
vhdl_type ltype(VHDL_TYPE_UNSIGNED, lhs->get_type()->get_msb(),
|
|
|
|
|
lhs->get_type()->get_lsb());
|
|
|
|
|
vhdl_type rtype(VHDL_TYPE_UNSIGNED, rhs->get_type()->get_msb(),
|
|
|
|
|
rhs->get_type()->get_lsb());
|
|
|
|
|
|
|
|
|
|
// May need to resize the left or right hand side
|
|
|
|
|
if (lwidth < rwidth) {
|
|
|
|
|
vhdl_fcall *resize =
|
2008-06-14 19:03:25 +02:00
|
|
|
new vhdl_fcall("resize", vhdl_type::nsigned(rwidth));
|
|
|
|
|
resize->add_expr(lhs);
|
2008-06-14 18:09:31 +02:00
|
|
|
resize->add_expr(new vhdl_const_int(rwidth));
|
2008-06-14 19:03:25 +02:00
|
|
|
lhs = resize;
|
|
|
|
|
lwidth = rwidth;
|
2008-06-14 18:09:31 +02:00
|
|
|
}
|
|
|
|
|
else if (rwidth < lwidth) {
|
|
|
|
|
vhdl_fcall *resize =
|
2008-06-14 19:03:25 +02:00
|
|
|
new vhdl_fcall("resize", vhdl_type::nsigned(lwidth));
|
|
|
|
|
resize->add_expr(rhs);
|
2008-06-14 18:09:31 +02:00
|
|
|
resize->add_expr(new vhdl_const_int(lwidth));
|
2008-06-14 19:03:25 +02:00
|
|
|
rhs = resize;
|
|
|
|
|
rwidth = lwidth;
|
2008-06-14 18:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-14 19:03:25 +02:00
|
|
|
return new vhdl_binop_expr(lhs, op, rhs, vhdl_type::nsigned(lwidth));
|
2008-06-14 18:09:31 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-16 13:20:28 +02:00
|
|
|
static vhdl_expr *translate_relation(vhdl_expr *lhs, vhdl_expr *rhs,
|
|
|
|
|
vhdl_binop_t op)
|
|
|
|
|
{
|
2008-06-16 13:47:41 +02:00
|
|
|
// Generate any necessary casts
|
|
|
|
|
// Arbitrarily, the RHS is casted to the type of the LHS
|
|
|
|
|
vhdl_expr *r_cast = rhs->cast(lhs->get_type());
|
|
|
|
|
|
|
|
|
|
return new vhdl_binop_expr(lhs, op, r_cast, vhdl_type::boolean());
|
2008-06-16 13:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-14 18:09:31 +02:00
|
|
|
static vhdl_expr *translate_binary(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
vhdl_expr *lhs = translate_expr(ivl_expr_oper1(e));
|
|
|
|
|
if (NULL == lhs)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
vhdl_expr *rhs = translate_expr(ivl_expr_oper2(e));
|
|
|
|
|
if (NULL == rhs)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2008-06-19 17:08:33 +02:00
|
|
|
// For === and !== we need to compare std_logic_vectors
|
|
|
|
|
// rather than signeds
|
|
|
|
|
vhdl_type std_logic_vector(VHDL_TYPE_STD_LOGIC_VECTOR);
|
|
|
|
|
bool vectorop =
|
|
|
|
|
(lhs->get_type()->get_name() == VHDL_TYPE_SIGNED
|
|
|
|
|
|| lhs->get_type()->get_name() == VHDL_TYPE_UNSIGNED) &&
|
|
|
|
|
(rhs->get_type()->get_name() == VHDL_TYPE_SIGNED
|
|
|
|
|
|| rhs->get_type()->get_name() == VHDL_TYPE_UNSIGNED);
|
|
|
|
|
|
2008-06-14 18:09:31 +02:00
|
|
|
switch (ivl_expr_opcode(e)) {
|
|
|
|
|
case '+':
|
|
|
|
|
return translate_numeric(lhs, rhs, VHDL_BINOP_ADD);
|
2008-06-16 13:20:28 +02:00
|
|
|
case 'e':
|
|
|
|
|
return translate_relation(lhs, rhs, VHDL_BINOP_EQ);
|
2008-06-19 17:08:33 +02:00
|
|
|
case 'E':
|
|
|
|
|
if (vectorop)
|
|
|
|
|
return translate_relation(lhs->cast(&std_logic_vector),
|
|
|
|
|
rhs->cast(&std_logic_vector), VHDL_BINOP_EQ);
|
|
|
|
|
else
|
|
|
|
|
return translate_relation(lhs, rhs, VHDL_BINOP_EQ);
|
|
|
|
|
case 'n':
|
2008-06-16 13:47:41 +02:00
|
|
|
return translate_relation(lhs, rhs, VHDL_BINOP_NEQ);
|
2008-06-19 17:08:33 +02:00
|
|
|
case 'N':
|
|
|
|
|
if (vectorop)
|
|
|
|
|
return translate_relation(lhs->cast(&std_logic_vector),
|
|
|
|
|
rhs->cast(&std_logic_vector), VHDL_BINOP_NEQ);
|
|
|
|
|
else
|
|
|
|
|
return translate_relation(lhs, rhs, VHDL_BINOP_NEQ);
|
|
|
|
|
case 'o':
|
|
|
|
|
return translate_relation(lhs, rhs, VHDL_BINOP_OR);
|
2008-06-14 18:09:31 +02:00
|
|
|
default:
|
|
|
|
|
error("No translation for binary opcode '%c'\n",
|
|
|
|
|
ivl_expr_opcode(e));
|
|
|
|
|
delete lhs;
|
|
|
|
|
delete rhs;
|
2008-06-12 11:56:28 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2008-06-12 11:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
2008-06-04 15:59:04 +02:00
|
|
|
/*
|
|
|
|
|
* Generate a VHDL expression from a Verilog expression.
|
|
|
|
|
*/
|
|
|
|
|
vhdl_expr *translate_expr(ivl_expr_t e)
|
|
|
|
|
{
|
2008-06-21 16:03:36 +02:00
|
|
|
assert(e);
|
2008-06-04 16:19:44 +02:00
|
|
|
ivl_expr_type_t type = ivl_expr_type(e);
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case IVL_EX_STRING:
|
|
|
|
|
return translate_string(e);
|
2008-06-07 12:24:09 +02:00
|
|
|
case IVL_EX_SIGNAL:
|
|
|
|
|
return translate_signal(e);
|
2008-06-07 17:19:10 +02:00
|
|
|
case IVL_EX_NUMBER:
|
|
|
|
|
return translate_number(e);
|
2008-06-12 11:47:52 +02:00
|
|
|
case IVL_EX_UNARY:
|
|
|
|
|
return translate_unary(e);
|
2008-06-14 18:09:31 +02:00
|
|
|
case IVL_EX_BINARY:
|
|
|
|
|
return translate_binary(e);
|
2008-06-04 16:19:44 +02:00
|
|
|
default:
|
|
|
|
|
error("No VHDL translation for expression at %s:%d (type = %d)",
|
|
|
|
|
ivl_expr_file(e), ivl_expr_lineno(e), type);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2008-06-04 15:59:04 +02:00
|
|
|
}
|