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"
|
2008-07-20 16:10:00 +02:00
|
|
|
#include "support.hh"
|
2009-01-17 14:38:11 +01:00
|
|
|
#include "state.hh"
|
2008-06-04 15:59:04 +02:00
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cassert>
|
2008-07-18 12:50:05 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
|
2008-06-04 15:59:04 +02:00
|
|
|
|
2008-07-08 14:07:11 +02:00
|
|
|
/*
|
2008-07-10 20:27:17 +02:00
|
|
|
* Change the signedness of a vector.
|
2008-07-08 14:07:11 +02:00
|
|
|
*/
|
2008-07-10 20:27:17 +02:00
|
|
|
static vhdl_expr *change_signedness(vhdl_expr *e, bool issigned)
|
2008-07-08 14:07:11 +02:00
|
|
|
{
|
|
|
|
|
int msb = e->get_type()->get_msb();
|
|
|
|
|
int lsb = e->get_type()->get_lsb();
|
|
|
|
|
vhdl_type u(issigned ? VHDL_TYPE_SIGNED : VHDL_TYPE_UNSIGNED, msb, lsb);
|
|
|
|
|
|
|
|
|
|
return e->cast(&u);
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-28 22:53:12 +02:00
|
|
|
/*
|
|
|
|
|
* Generate code to ensure that the VHDL expression vhd_e has the
|
|
|
|
|
* same signedness as the Verilog expression vl_e.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *correct_signedness(vhdl_expr *vhd_e, ivl_expr_t vl_e)
|
|
|
|
|
{
|
|
|
|
|
bool should_be_signed = ivl_expr_signed(vl_e) != 0;
|
|
|
|
|
|
|
|
|
|
if (vhd_e->get_type()->get_name() == VHDL_TYPE_UNSIGNED
|
|
|
|
|
&& should_be_signed) {
|
|
|
|
|
//operand->print();
|
|
|
|
|
//std::cout << "^ should be signed but is not" << std::endl;
|
|
|
|
|
|
|
|
|
|
return change_signedness(vhd_e, true);
|
|
|
|
|
}
|
|
|
|
|
else if (vhd_e->get_type()->get_name() == VHDL_TYPE_SIGNED
|
|
|
|
|
&& !should_be_signed) {
|
|
|
|
|
//operand->print();
|
|
|
|
|
//std::cout << "^ should be unsigned but is not" << std::endl;
|
|
|
|
|
|
|
|
|
|
return change_signedness(vhd_e, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return vhd_e;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2008-06-21 17:17:44 +02:00
|
|
|
static vhdl_var_ref *translate_signal(ivl_expr_t e)
|
2008-06-07 12:24:09 +02:00
|
|
|
{
|
|
|
|
|
ivl_signal_t sig = ivl_expr_signal(e);
|
2008-07-29 14:30:54 +02:00
|
|
|
|
2008-06-25 19:12:57 +02:00
|
|
|
const vhdl_scope *scope = find_scope_for_signal(sig);
|
|
|
|
|
assert(scope);
|
2008-06-14 19:11:10 +02:00
|
|
|
|
|
|
|
|
const char *renamed = get_renamed_signal(sig).c_str();
|
2008-08-20 23:54:53 +02:00
|
|
|
|
2008-07-16 13:00:11 +02:00
|
|
|
vhdl_decl *decl = scope->get_decl(renamed);
|
2008-06-14 19:11:10 +02:00
|
|
|
assert(decl);
|
|
|
|
|
|
2009-01-24 15:16:19 +01:00
|
|
|
// Make sure we can read from this declaration
|
|
|
|
|
// E.g. if this is an `out' port then we need to make it a buffer
|
|
|
|
|
decl->ensure_readable();
|
|
|
|
|
|
2008-07-16 13:00:11 +02:00
|
|
|
// Can't generate a constant initialiser for this signal
|
|
|
|
|
// later as it has already been read
|
|
|
|
|
if (scope->initializing())
|
|
|
|
|
decl->set_initial(NULL);
|
|
|
|
|
|
2008-07-17 15:47:10 +02:00
|
|
|
vhdl_var_ref *ref =
|
|
|
|
|
new vhdl_var_ref(renamed, new vhdl_type(*decl->get_type()));
|
|
|
|
|
|
|
|
|
|
ivl_expr_t off;
|
|
|
|
|
if (ivl_signal_array_count(sig) > 0 && (off = ivl_expr_oper1(e))) {
|
|
|
|
|
// Select from an array
|
|
|
|
|
vhdl_expr *vhd_off = translate_expr(off);
|
|
|
|
|
if (NULL == vhd_off)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
ref->set_slice(vhd_off->cast(&integer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ref;
|
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-07-16 17:52:15 +02:00
|
|
|
if (ivl_expr_width(e) == 1)
|
|
|
|
|
return new vhdl_const_bit(ivl_expr_bits(e)[0]);
|
|
|
|
|
else
|
|
|
|
|
return new vhdl_const_bits(ivl_expr_bits(e), ivl_expr_width(e),
|
|
|
|
|
ivl_expr_signed(e) != 0);
|
2008-06-07 17:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-18 15:47:35 +02:00
|
|
|
static vhdl_expr *translate_ulong(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
return new vhdl_const_int(ivl_expr_uvalue(e));
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-20 17:41:57 +02:00
|
|
|
static vhdl_expr *translate_reduction(support_function_t f, bool neg,
|
|
|
|
|
vhdl_expr *operand)
|
|
|
|
|
{
|
2008-08-01 17:27:55 +02:00
|
|
|
vhdl_expr *result;
|
|
|
|
|
if (operand->get_type()->get_name() == VHDL_TYPE_STD_LOGIC)
|
|
|
|
|
result = operand;
|
|
|
|
|
else {
|
|
|
|
|
require_support_function(f);
|
|
|
|
|
vhdl_fcall *fcall =
|
|
|
|
|
new vhdl_fcall(support_function::function_name(f),
|
|
|
|
|
vhdl_type::std_logic());
|
|
|
|
|
|
|
|
|
|
vhdl_type std_logic_vector(VHDL_TYPE_STD_LOGIC_VECTOR);
|
|
|
|
|
fcall->add_expr(operand->cast(&std_logic_vector));
|
|
|
|
|
|
|
|
|
|
result = fcall;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-20 17:41:57 +02:00
|
|
|
if (neg)
|
2008-08-01 17:27:55 +02:00
|
|
|
return new vhdl_unaryop_expr(VHDL_UNARYOP_NOT, result,
|
2008-07-20 17:41:57 +02:00
|
|
|
vhdl_type::std_logic());
|
|
|
|
|
else
|
2008-08-01 17:27:55 +02:00
|
|
|
return result;
|
2008-07-20 17:41:57 +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;
|
2008-07-07 16:23:57 +02:00
|
|
|
|
2008-08-28 22:53:12 +02:00
|
|
|
operand = correct_signedness(operand, e);
|
2008-07-08 13:58:50 +02:00
|
|
|
|
2008-07-07 16:23:57 +02:00
|
|
|
char opcode = ivl_expr_opcode(e);
|
|
|
|
|
switch (opcode) {
|
2008-06-12 11:56:28 +02:00
|
|
|
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()));
|
2008-07-22 16:44:29 +02:00
|
|
|
case '-':
|
2008-08-01 17:35:47 +02:00
|
|
|
operand = change_signedness(operand, true);
|
2008-07-22 16:44:29 +02:00
|
|
|
return new vhdl_unaryop_expr
|
|
|
|
|
(VHDL_UNARYOP_NEG, operand, new vhdl_type(*operand->get_type()));
|
2008-07-07 16:23:57 +02:00
|
|
|
case 'N': // NOR
|
2008-07-20 17:41:57 +02:00
|
|
|
return translate_reduction(SF_REDUCE_OR, true, operand);
|
2008-07-07 16:23:57 +02:00
|
|
|
case '|':
|
2008-07-20 17:41:57 +02:00
|
|
|
return translate_reduction(SF_REDUCE_OR, false, operand);
|
|
|
|
|
case 'A': // NAND
|
|
|
|
|
return translate_reduction(SF_REDUCE_AND, true, operand);
|
|
|
|
|
case '&':
|
|
|
|
|
return translate_reduction(SF_REDUCE_AND, false, operand);
|
2008-09-13 19:09:11 +02:00
|
|
|
case '^': // XOR
|
|
|
|
|
return translate_reduction(SF_REDUCE_XOR, false, operand);
|
|
|
|
|
case 'X': // XNOR
|
|
|
|
|
return translate_reduction(SF_REDUCE_XNOR, false, operand);
|
2008-06-12 11:56:28 +02:00
|
|
|
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-23 14:04:28 +02:00
|
|
|
{
|
2008-07-07 17:31:27 +02:00
|
|
|
// May need to make either side Boolean for operators
|
|
|
|
|
// to work
|
|
|
|
|
vhdl_type boolean(VHDL_TYPE_BOOLEAN);
|
|
|
|
|
if (lhs->get_type()->get_name() == VHDL_TYPE_BOOLEAN)
|
|
|
|
|
rhs = rhs->cast(&boolean);
|
|
|
|
|
else if (rhs->get_type()->get_name() == VHDL_TYPE_BOOLEAN)
|
|
|
|
|
lhs = lhs->cast(&boolean);
|
|
|
|
|
|
2008-08-20 23:54:53 +02:00
|
|
|
vhdl_type *rtype;
|
|
|
|
|
if (op == VHDL_BINOP_MULT)
|
|
|
|
|
rtype = new vhdl_type(lhs->get_type()->get_name(),
|
|
|
|
|
(lhs->get_type()->get_width()*2) - 1, 0);
|
|
|
|
|
else
|
|
|
|
|
rtype = new vhdl_type(*lhs->get_type());
|
2008-06-23 14:04:28 +02:00
|
|
|
return new vhdl_binop_expr(lhs, op, rhs, rtype);
|
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-07-04 12:23:32 +02:00
|
|
|
/*
|
|
|
|
|
* Like translate_relation but both operands must be Boolean.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *translate_logical(vhdl_expr *lhs, vhdl_expr *rhs,
|
|
|
|
|
vhdl_binop_t op)
|
|
|
|
|
{
|
|
|
|
|
vhdl_type boolean(VHDL_TYPE_BOOLEAN);
|
|
|
|
|
|
|
|
|
|
return translate_relation(lhs->cast(&boolean), rhs->cast(&boolean), op);
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-23 13:14:12 +02:00
|
|
|
static vhdl_expr *translate_shift(vhdl_expr *lhs, vhdl_expr *rhs,
|
|
|
|
|
vhdl_binop_t op)
|
|
|
|
|
{
|
|
|
|
|
// The RHS must be an integer
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
vhdl_expr *r_cast = rhs->cast(&integer);
|
|
|
|
|
|
|
|
|
|
vhdl_type *rtype = new vhdl_type(*lhs->get_type());
|
2008-09-13 16:23:42 +02:00
|
|
|
|
|
|
|
|
// The sra operator is not defined on numeric_std types until
|
|
|
|
|
// VHDL-2006 which is not well supported. Instead we can use
|
|
|
|
|
// the shift_right function which does the same thing and
|
|
|
|
|
// exists in earlier versions of numeric_std.
|
|
|
|
|
if (op == VHDL_BINOP_SRA) {
|
|
|
|
|
vhdl_fcall *sra = new vhdl_fcall("shift_right", rtype);
|
|
|
|
|
sra->add_expr(lhs);
|
|
|
|
|
sra->add_expr(r_cast);
|
|
|
|
|
|
|
|
|
|
return sra;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return new vhdl_binop_expr(lhs, op, r_cast, rtype);
|
2008-06-23 13:14:12 +02:00
|
|
|
}
|
|
|
|
|
|
2008-09-12 21:19:22 +02:00
|
|
|
/*
|
|
|
|
|
* The exponentiation operator in VHDL is not defined for numeric_std
|
|
|
|
|
* types. We can get around this by converting the operands to integers,
|
|
|
|
|
* performing the operation, then converting the result back to the
|
|
|
|
|
* original type. This will work OK in simulation but certainly will not
|
|
|
|
|
* synthesise unless the operands are constant.
|
|
|
|
|
*
|
|
|
|
|
* However, even this does not work quite correctly. The Integer type in
|
|
|
|
|
* VHDL is signed and usually only 32 bits, therefore any result larger
|
|
|
|
|
* than this will overflow and raise an exception. I can't see a way
|
|
|
|
|
* around this at the moment.
|
|
|
|
|
*/
|
|
|
|
|
static vhdl_expr *translate_power(ivl_expr_t e, vhdl_expr *lhs, vhdl_expr *rhs)
|
|
|
|
|
{
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
vhdl_expr *lhs_int = lhs->cast(&integer);
|
|
|
|
|
vhdl_expr *rhs_int = rhs->cast(&integer);
|
|
|
|
|
|
|
|
|
|
vhdl_expr *result = new vhdl_binop_expr(lhs_int, VHDL_BINOP_POWER, rhs_int,
|
|
|
|
|
vhdl_type::integer());
|
|
|
|
|
|
|
|
|
|
int width = ivl_expr_width(e);
|
|
|
|
|
const char *func = ivl_expr_signed(e) ? "To_Signed" : "To_Unsigned";
|
|
|
|
|
vhdl_type *type = ivl_expr_signed(e)
|
|
|
|
|
? vhdl_type::nsigned(width) : vhdl_type::nunsigned(width);
|
|
|
|
|
|
|
|
|
|
vhdl_fcall *conv = new vhdl_fcall(func, type);
|
|
|
|
|
conv->add_expr(result);
|
|
|
|
|
conv->add_expr(new vhdl_const_int(width));
|
|
|
|
|
|
|
|
|
|
return conv;
|
|
|
|
|
}
|
|
|
|
|
|
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-07-07 17:31:27 +02:00
|
|
|
|
2008-06-23 14:04:28 +02:00
|
|
|
int lwidth = lhs->get_type()->get_width();
|
|
|
|
|
int rwidth = rhs->get_type()->get_width();
|
2008-07-08 01:20:31 +02:00
|
|
|
int result_width = ivl_expr_width(e);
|
|
|
|
|
|
2008-06-19 17:08:33 +02:00
|
|
|
// For === and !== we need to compare std_logic_vectors
|
|
|
|
|
// rather than signeds
|
2008-07-08 01:20:31 +02:00
|
|
|
vhdl_type std_logic_vector(VHDL_TYPE_STD_LOGIC_VECTOR, result_width-1, 0);
|
2008-07-08 13:58:50 +02:00
|
|
|
vhdl_type_name_t ltype = lhs->get_type()->get_name();
|
|
|
|
|
vhdl_type_name_t rtype = rhs->get_type()->get_name();
|
2008-06-19 17:08:33 +02:00
|
|
|
bool vectorop =
|
2008-07-08 13:58:50 +02:00
|
|
|
(ltype == VHDL_TYPE_SIGNED || ltype == VHDL_TYPE_UNSIGNED) &&
|
|
|
|
|
(rtype == VHDL_TYPE_SIGNED || rtype == VHDL_TYPE_UNSIGNED);
|
2008-07-08 01:20:31 +02:00
|
|
|
|
2008-08-28 22:53:12 +02:00
|
|
|
// May need to resize the left or right hand side or change the
|
|
|
|
|
// signedness
|
2008-07-08 13:58:50 +02:00
|
|
|
if (vectorop) {
|
|
|
|
|
if (lwidth < rwidth)
|
|
|
|
|
lhs = lhs->resize(rwidth);
|
|
|
|
|
else if (rwidth < lwidth)
|
|
|
|
|
rhs = rhs->resize(lwidth);
|
2008-08-28 22:53:12 +02:00
|
|
|
|
|
|
|
|
lhs = correct_signedness(lhs, ivl_expr_oper1(e));
|
|
|
|
|
rhs = correct_signedness(rhs, ivl_expr_oper2(e));
|
2008-07-08 13:58:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *result;
|
2008-06-14 18:09:31 +02:00
|
|
|
switch (ivl_expr_opcode(e)) {
|
|
|
|
|
case '+':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_ADD);
|
|
|
|
|
break;
|
2008-06-21 17:17:44 +02:00
|
|
|
case '-':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_SUB);
|
|
|
|
|
break;
|
2008-07-07 20:27:52 +02:00
|
|
|
case '*':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_MULT);
|
|
|
|
|
break;
|
2008-08-07 15:18:26 +02:00
|
|
|
case '/':
|
|
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_DIV);
|
|
|
|
|
break;
|
|
|
|
|
case '%':
|
|
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_MOD);
|
|
|
|
|
break;
|
2008-06-16 13:20:28 +02:00
|
|
|
case 'e':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_EQ);
|
|
|
|
|
break;
|
2008-06-19 17:08:33 +02:00
|
|
|
case 'E':
|
|
|
|
|
if (vectorop)
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs->cast(&std_logic_vector),
|
2008-08-20 23:54:53 +02:00
|
|
|
rhs->cast(&std_logic_vector), VHDL_BINOP_EQ);
|
2008-06-19 17:08:33 +02:00
|
|
|
else
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_EQ);
|
|
|
|
|
break;
|
2008-06-19 17:08:33 +02:00
|
|
|
case 'n':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_NEQ);
|
|
|
|
|
break;
|
2008-06-19 17:08:33 +02:00
|
|
|
case 'N':
|
|
|
|
|
if (vectorop)
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs->cast(&std_logic_vector),
|
2008-08-20 23:54:53 +02:00
|
|
|
rhs->cast(&std_logic_vector), VHDL_BINOP_NEQ);
|
2008-06-19 17:08:33 +02:00
|
|
|
else
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_NEQ);
|
|
|
|
|
break;
|
2008-06-21 16:05:48 +02:00
|
|
|
case '&': // Bitwise AND
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_AND);
|
|
|
|
|
break;
|
2008-07-04 12:10:20 +02:00
|
|
|
case 'a': // Logical AND
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_logical(lhs, rhs, VHDL_BINOP_AND);
|
|
|
|
|
break;
|
2008-08-01 18:42:26 +02:00
|
|
|
case 'A': // Bitwise NAND
|
|
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_NAND);
|
|
|
|
|
break;
|
|
|
|
|
case 'O': // Bitwise NOR
|
|
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_NOR);
|
|
|
|
|
break;
|
2008-08-06 12:18:01 +02:00
|
|
|
case 'X': // Bitwise XNOR
|
|
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_XNOR);
|
|
|
|
|
break;
|
2008-07-04 13:05:49 +02:00
|
|
|
case '|': // Bitwise OR
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_OR);
|
|
|
|
|
break;
|
2008-07-04 12:23:32 +02:00
|
|
|
case 'o': // Logical OR
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_logical(lhs, rhs, VHDL_BINOP_OR);
|
|
|
|
|
break;
|
2008-06-21 16:16:22 +02:00
|
|
|
case '<':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_LT);
|
|
|
|
|
break;
|
2008-07-07 22:19:59 +02:00
|
|
|
case 'L':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_LEQ);
|
|
|
|
|
break;
|
2008-06-21 16:16:22 +02:00
|
|
|
case '>':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_GT);
|
|
|
|
|
break;
|
2008-07-07 22:19:59 +02:00
|
|
|
case 'G':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_relation(lhs, rhs, VHDL_BINOP_GEQ);
|
|
|
|
|
break;
|
2008-06-21 16:19:33 +02:00
|
|
|
case 'l':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_shift(lhs, rhs, VHDL_BINOP_SL);
|
|
|
|
|
break;
|
2008-06-21 16:19:33 +02:00
|
|
|
case 'r':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_shift(lhs, rhs, VHDL_BINOP_SR);
|
|
|
|
|
break;
|
2008-09-13 16:23:42 +02:00
|
|
|
case 'R': // Arithmetic right shift
|
|
|
|
|
// Verilog only actually performs a signed shift if the
|
|
|
|
|
// argument being shifted is signed, otherwise it defaults
|
|
|
|
|
// to a normal shift
|
|
|
|
|
if (ivl_expr_signed(ivl_expr_oper1(e)))
|
|
|
|
|
result = translate_shift(lhs, rhs, VHDL_BINOP_SRA);
|
|
|
|
|
else
|
|
|
|
|
result = translate_shift(lhs, rhs, VHDL_BINOP_SR);
|
|
|
|
|
break;
|
2008-06-24 11:55:45 +02:00
|
|
|
case '^':
|
2008-07-08 13:58:50 +02:00
|
|
|
result = translate_numeric(lhs, rhs, VHDL_BINOP_XOR);
|
|
|
|
|
break;
|
2008-09-12 21:19:22 +02:00
|
|
|
case 'p': // Power
|
|
|
|
|
result = translate_power(e, lhs, rhs);
|
|
|
|
|
break;
|
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-07-08 13:58:50 +02:00
|
|
|
|
|
|
|
|
if (NULL == result)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (vectorop) {
|
2008-08-28 22:53:12 +02:00
|
|
|
result = correct_signedness(result, e);
|
2008-07-08 13:58:50 +02:00
|
|
|
|
|
|
|
|
int actual_width = result->get_type()->get_width();
|
|
|
|
|
if (actual_width != result_width) {
|
|
|
|
|
//result->print();
|
|
|
|
|
//std::cout << "^ should be " << result_width << " but is " << actual_width << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2008-06-12 11:47:52 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-04 21:07:38 +02:00
|
|
|
static vhdl_expr *translate_select(ivl_expr_t e)
|
2008-06-21 17:17:44 +02:00
|
|
|
{
|
2008-11-12 23:26:44 +01:00
|
|
|
vhdl_expr *from = translate_expr(ivl_expr_oper1(e));
|
|
|
|
|
if (NULL == from)
|
2008-07-07 22:19:59 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
ivl_expr_t o2 = ivl_expr_oper2(e);
|
2008-11-14 11:07:15 +01:00
|
|
|
if (o2) {
|
2008-07-07 22:19:59 +02:00
|
|
|
vhdl_expr *base = translate_expr(ivl_expr_oper2(e));
|
|
|
|
|
if (NULL == base)
|
2008-07-08 01:20:31 +02:00
|
|
|
return NULL;
|
2008-07-07 22:19:59 +02:00
|
|
|
|
2008-11-14 11:07:15 +01:00
|
|
|
vhdl_var_ref *from_var_ref = dynamic_cast<vhdl_var_ref*>(from);
|
|
|
|
|
if (NULL == from_var_ref) {
|
|
|
|
|
// We can't directly select bits from something that's not
|
|
|
|
|
// a variable reference in VHDL, but we can emulate the
|
|
|
|
|
// effect with a shift and a resize
|
|
|
|
|
return new vhdl_binop_expr(from, VHDL_BINOP_SR, base->to_integer(),
|
|
|
|
|
new vhdl_type(*from->get_type()));
|
|
|
|
|
}
|
2008-12-12 20:10:38 +01:00
|
|
|
else if (from_var_ref->get_type()->get_name() != VHDL_TYPE_STD_LOGIC) {
|
2008-11-14 11:07:15 +01:00
|
|
|
// We can use the more idomatic VHDL slice notation on a
|
|
|
|
|
// single variable reference
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
from_var_ref->set_slice(base->cast(&integer), ivl_expr_width(e) - 1);
|
|
|
|
|
return from_var_ref;
|
|
|
|
|
}
|
2008-12-12 20:10:38 +01:00
|
|
|
else {
|
|
|
|
|
// Make sure we're not trying to select more than one bit
|
|
|
|
|
// from a std_logic (this shouldn't actually happen)
|
|
|
|
|
if (ivl_expr_width(e) > 1) {
|
|
|
|
|
error("%s:%d: trying to select more than one bit from a std_logic",
|
|
|
|
|
ivl_expr_file(e), ivl_expr_lineno(e));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return from_var_ref;
|
|
|
|
|
}
|
2008-07-07 22:19:59 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return from->resize(ivl_expr_width(e));
|
2008-06-21 17:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-06 19:21:34 +02:00
|
|
|
template <class T>
|
|
|
|
|
static T *translate_parms(T *t, ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
int nparams = ivl_expr_parms(e);
|
|
|
|
|
for (int i = 0; i < nparams; i++) {
|
|
|
|
|
vhdl_expr *param = translate_expr(ivl_expr_parm(e, i));
|
|
|
|
|
if (NULL == param)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
t->add_expr(param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-04 21:07:38 +02:00
|
|
|
static vhdl_expr *translate_ufunc(ivl_expr_t e)
|
2008-06-25 18:29:09 +02:00
|
|
|
{
|
|
|
|
|
ivl_scope_t defscope = ivl_expr_def(e);
|
|
|
|
|
ivl_scope_t parentscope = ivl_scope_parent(defscope);
|
|
|
|
|
assert(ivl_scope_type(parentscope) == IVL_SCT_MODULE);
|
|
|
|
|
|
|
|
|
|
// A function is always declared in a module, which should have
|
|
|
|
|
// a corresponding entity by this point: so we can get type
|
|
|
|
|
// information, etc. from the declaration
|
2009-01-17 13:20:55 +01:00
|
|
|
vhdl_entity *parent_ent = find_entity(parentscope);
|
2008-06-25 18:29:09 +02:00
|
|
|
assert(parent_ent);
|
|
|
|
|
|
|
|
|
|
const char *funcname = ivl_scope_tname(defscope);
|
2008-06-30 18:58:15 +02:00
|
|
|
|
2008-08-22 22:27:24 +02:00
|
|
|
vhdl_type *rettype =
|
|
|
|
|
vhdl_type::type_for(ivl_expr_width(e), ivl_expr_signed(e) != 0);
|
2008-06-25 18:29:09 +02:00
|
|
|
vhdl_fcall *fcall = new vhdl_fcall(funcname, rettype);
|
|
|
|
|
|
2008-08-20 23:54:53 +02:00
|
|
|
int nparams = ivl_expr_parms(e);
|
2009-01-22 23:41:46 +01:00
|
|
|
int func_scope_sig = 0;
|
2008-08-20 23:54:53 +02:00
|
|
|
for (int i = 0; i < nparams; i++) {
|
|
|
|
|
vhdl_expr *param = translate_expr(ivl_expr_parm(e, i));
|
|
|
|
|
if (NULL == param)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
// Ensure the parameter has the correct VHDL type
|
2009-01-22 23:41:46 +01:00
|
|
|
ivl_signal_t param_sig;
|
|
|
|
|
do {
|
|
|
|
|
param_sig = ivl_scope_sig(defscope, func_scope_sig++);
|
|
|
|
|
} while (ivl_signal_port(param_sig) != IVL_SIP_INPUT);
|
2008-08-20 23:54:53 +02:00
|
|
|
vhdl_type *param_type =
|
|
|
|
|
vhdl_type::type_for(ivl_signal_width(param_sig),
|
|
|
|
|
ivl_signal_signed(param_sig) != 0);
|
|
|
|
|
|
|
|
|
|
fcall->add_expr(param->cast(param_type));
|
|
|
|
|
delete param_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fcall;
|
2008-06-25 18:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-04 21:07:38 +02:00
|
|
|
static vhdl_expr *translate_ternary(ivl_expr_t e)
|
|
|
|
|
{
|
2008-07-28 13:59:10 +02:00
|
|
|
support_function_t sf;
|
|
|
|
|
int width = ivl_expr_width(e);
|
|
|
|
|
bool issigned = ivl_expr_signed(e) != 0;
|
|
|
|
|
if (width == 1)
|
|
|
|
|
sf = SF_TERNARY_LOGIC;
|
|
|
|
|
else if (issigned)
|
|
|
|
|
sf = SF_TERNARY_SIGNED;
|
|
|
|
|
else
|
|
|
|
|
sf = SF_TERNARY_UNSIGNED;
|
|
|
|
|
|
|
|
|
|
require_support_function(sf);
|
|
|
|
|
|
|
|
|
|
vhdl_expr *test = translate_expr(ivl_expr_oper1(e));
|
|
|
|
|
vhdl_expr *true_part = translate_expr(ivl_expr_oper2(e));
|
|
|
|
|
vhdl_expr *false_part = translate_expr(ivl_expr_oper3(e));
|
|
|
|
|
if (!test || !true_part || !false_part)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
vhdl_type boolean(VHDL_TYPE_BOOLEAN);
|
|
|
|
|
test = test->cast(&boolean);
|
|
|
|
|
|
|
|
|
|
vhdl_fcall *fcall =
|
|
|
|
|
new vhdl_fcall(support_function::function_name(sf),
|
|
|
|
|
vhdl_type::type_for(width, issigned));
|
|
|
|
|
fcall->add_expr(test);
|
|
|
|
|
fcall->add_expr(true_part);
|
|
|
|
|
fcall->add_expr(false_part);
|
2008-07-04 21:07:38 +02:00
|
|
|
|
2008-07-28 13:59:10 +02:00
|
|
|
return fcall;
|
2008-07-04 21:07:38 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-06 19:21:34 +02:00
|
|
|
static vhdl_expr *translate_concat(ivl_expr_t e)
|
|
|
|
|
{
|
2008-08-22 22:27:24 +02:00
|
|
|
vhdl_type *rtype =
|
|
|
|
|
vhdl_type::type_for(ivl_expr_width(e), ivl_expr_signed(e) != 0);
|
2008-07-06 19:21:34 +02:00
|
|
|
vhdl_binop_expr *concat = new vhdl_binop_expr(VHDL_BINOP_CONCAT, rtype);
|
|
|
|
|
|
2008-07-28 22:46:19 +02:00
|
|
|
int nrepeat = ivl_expr_repeat(e);
|
|
|
|
|
while (nrepeat--)
|
|
|
|
|
translate_parms<vhdl_binop_expr>(concat, e);
|
|
|
|
|
|
|
|
|
|
return concat;
|
2008-07-06 19:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
2008-07-18 12:50:05 +02:00
|
|
|
vhdl_expr *translate_sfunc_time(ivl_expr_t e)
|
|
|
|
|
{
|
2008-09-06 13:06:01 +02:00
|
|
|
cerr << "warning: no translation for $time (returning 0)" << endl;
|
|
|
|
|
vhdl_expr *result = new vhdl_const_int(0);
|
|
|
|
|
result->set_comment("$time not supported, returned 0 instead!");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *translate_sfunc_stime(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
cerr << "warning: no translation for $stime (returning 0)" << endl;
|
|
|
|
|
vhdl_expr *result = new vhdl_const_int(0);
|
|
|
|
|
result->set_comment("$stime not supported, returned 0 instead!");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *translate_sfunc_simtime(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
cerr << "warning: no translation for $simtime (returning 0)" << endl;
|
|
|
|
|
vhdl_expr *result = new vhdl_const_int(0);
|
|
|
|
|
result->set_comment("$simtime not supported, returned 0 instead!");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *translate_sfunc_random(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
cerr << "warning: no translation for $random (returning 0)" << endl;
|
|
|
|
|
vhdl_expr *result = new vhdl_const_int(0);
|
|
|
|
|
result->set_comment("$random not supported, returned 0 instead!");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *translate_sfunc_fopen(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
cerr << "warning: no translation for $fopen (returning 0)" << endl;
|
|
|
|
|
vhdl_expr *result = new vhdl_const_int(0);
|
|
|
|
|
result->set_comment("$fopen not supported, returned 0 instead!");
|
|
|
|
|
return result;
|
2008-07-18 12:50:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vhdl_expr *translate_sfunc(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
const char *name = ivl_expr_name(e);
|
|
|
|
|
if (strcmp(name, "$time") == 0)
|
|
|
|
|
return translate_sfunc_time(e);
|
2008-09-06 13:06:01 +02:00
|
|
|
else if (strcmp(name, "$stime") == 0)
|
|
|
|
|
return translate_sfunc_stime(e);
|
|
|
|
|
else if (strcmp(name, "$simtime") == 0)
|
|
|
|
|
return translate_sfunc_simtime(e);
|
|
|
|
|
else if (strcmp(name, "$random") == 0)
|
|
|
|
|
return translate_sfunc_random(e);
|
|
|
|
|
else if (strcmp(name, "$fopen") == 0)
|
|
|
|
|
return translate_sfunc_random(e);
|
2008-07-18 12:50:05 +02:00
|
|
|
else {
|
|
|
|
|
error("No translation for system function %s", name);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2008-11-14 11:07:15 +01:00
|
|
|
|
2008-06-04 16:19:44 +02:00
|
|
|
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-07-18 15:47:35 +02:00
|
|
|
case IVL_EX_ULONG:
|
|
|
|
|
return translate_ulong(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-21 17:17:44 +02:00
|
|
|
case IVL_EX_SELECT:
|
|
|
|
|
return translate_select(e);
|
2008-06-25 18:29:09 +02:00
|
|
|
case IVL_EX_UFUNC:
|
|
|
|
|
return translate_ufunc(e);
|
2008-07-04 21:07:38 +02:00
|
|
|
case IVL_EX_TERNARY:
|
|
|
|
|
return translate_ternary(e);
|
2008-07-06 19:21:34 +02:00
|
|
|
case IVL_EX_CONCAT:
|
|
|
|
|
return translate_concat(e);
|
2008-07-18 12:50:05 +02:00
|
|
|
case IVL_EX_SFUNC:
|
|
|
|
|
return translate_sfunc(e);
|
2008-09-06 13:06:01 +02:00
|
|
|
case IVL_EX_REALNUM:
|
|
|
|
|
error("No VHDL translation for real expression at %s:%d",
|
|
|
|
|
ivl_expr_file(e), ivl_expr_lineno(e));
|
|
|
|
|
return NULL;
|
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
|
|
|
}
|
2008-07-23 17:18:49 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Translate an expression into a time. This is achieved simply
|
|
|
|
|
* by multiplying the expression by 1ns.
|
|
|
|
|
*/
|
|
|
|
|
vhdl_expr *translate_time_expr(ivl_expr_t e)
|
|
|
|
|
{
|
|
|
|
|
vhdl_expr *time = translate_expr(e);
|
|
|
|
|
if (NULL == time)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
vhdl_type integer(VHDL_TYPE_INTEGER);
|
|
|
|
|
time = time->cast(&integer);
|
|
|
|
|
|
|
|
|
|
vhdl_expr *ns1 = new vhdl_const_time(1, TIME_UNIT_NS);
|
|
|
|
|
return new vhdl_binop_expr(time, VHDL_BINOP_MULT, ns1,
|
|
|
|
|
vhdl_type::time());
|
|
|
|
|
}
|