2011-01-10 18:30:27 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 Cary R. (cygcary@yahoo.com)
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-01-18 22:46:18 +01:00
|
|
|
# include <inttypes.h>
|
2011-02-05 03:20:47 +01:00
|
|
|
# include <string.h>
|
2011-01-10 18:30:27 +01:00
|
|
|
# include "config.h"
|
|
|
|
|
# include "vlog95_priv.h"
|
|
|
|
|
|
2011-01-18 22:46:18 +01:00
|
|
|
// HERE: Do we need to use wid in these routines? We should probably use
|
|
|
|
|
// it to verify that the expressions have the expected width.
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We can convert any 2^n ** <unsigned variable> expression to
|
|
|
|
|
* 1 << (n * <unsigned variable>). If the variable is signed then we need
|
|
|
|
|
* to add a check for less than zero and for that case return 0.
|
|
|
|
|
*/
|
|
|
|
|
static unsigned emit_power_as_shift(ivl_scope_t scope, ivl_expr_t expr,
|
|
|
|
|
unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
int rtype;
|
|
|
|
|
int64_t value, scale;
|
|
|
|
|
unsigned is_signed_rval = 0;
|
|
|
|
|
unsigned expr_wid;
|
|
|
|
|
ivl_expr_t lval = ivl_expr_oper1(expr);
|
|
|
|
|
ivl_expr_t rval = ivl_expr_oper2(expr);
|
|
|
|
|
/* The L-value must be a number. */
|
|
|
|
|
if (ivl_expr_type(lval) != IVL_EX_NUMBER) return 0;
|
|
|
|
|
/* The L-value must of the form 2^n. */
|
|
|
|
|
value = get_int64_from_number(lval, &rtype);
|
|
|
|
|
if (rtype) return 0;
|
|
|
|
|
expr_wid = ivl_expr_width(lval);
|
|
|
|
|
if (value < 2) return 0;
|
|
|
|
|
if (value % 2) return 0;
|
|
|
|
|
/* Generate the appropriate conversion. */
|
|
|
|
|
if (ivl_expr_signed(rval)) {
|
|
|
|
|
emit_expr(scope, rval, 0);
|
|
|
|
|
fprintf(vlog_out, " < 0 ? %u'h0 : (", expr_wid);
|
|
|
|
|
is_signed_rval = 1;
|
|
|
|
|
}
|
|
|
|
|
scale = value / 2;
|
|
|
|
|
fprintf(vlog_out, "%u'h1 << ", expr_wid);
|
|
|
|
|
if (scale != 1) {
|
|
|
|
|
if (is_signed_rval) {
|
|
|
|
|
fprintf(vlog_out, "(%"PRId64, scale);
|
|
|
|
|
} else {
|
|
|
|
|
fprintf(vlog_out, "(%u'h%"PRIx64,
|
|
|
|
|
ivl_expr_width(rval), scale);
|
|
|
|
|
}
|
|
|
|
|
fprintf(vlog_out, " * ");
|
|
|
|
|
}
|
|
|
|
|
emit_expr(scope, rval, 0);
|
|
|
|
|
if (scale != 1) fprintf(vlog_out, ")");
|
|
|
|
|
if (is_signed_rval) fprintf(vlog_out, ")");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2011-01-10 18:30:27 +01:00
|
|
|
|
2011-01-28 23:52:32 +01:00
|
|
|
static void emit_expr_array(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
ivl_signal_t sig = ivl_expr_signal(expr);
|
|
|
|
|
emit_scope_module_path(scope, ivl_signal_scope(sig));
|
|
|
|
|
fprintf(vlog_out, "%s", ivl_signal_basename(sig));
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-10 18:30:27 +01:00
|
|
|
static void emit_expr_binary(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
char *oper = "<invalid>";
|
|
|
|
|
switch(ivl_expr_opcode(expr)) {
|
|
|
|
|
case '+': oper = "+"; break;
|
|
|
|
|
case '-': oper = "-"; break;
|
|
|
|
|
case '*': oper = "*"; break;
|
|
|
|
|
case '/': oper = "/"; break;
|
|
|
|
|
case '%': oper = "%"; break;
|
|
|
|
|
case 'p': oper = "**"; break;
|
|
|
|
|
case 'E': oper = "==="; break;
|
|
|
|
|
case 'e': oper = "=="; break;
|
|
|
|
|
case 'N': oper = "!=="; break;
|
|
|
|
|
case 'n': oper = "!="; break;
|
|
|
|
|
case '<': oper = "<"; break;
|
|
|
|
|
case 'L': oper = "<="; break;
|
|
|
|
|
case '>': oper = ">"; break;
|
|
|
|
|
case 'G': oper = ">="; break;
|
|
|
|
|
case '&': oper = "&"; break;
|
|
|
|
|
case '|': oper = "|"; break;
|
|
|
|
|
case '^': oper = "^"; break;
|
|
|
|
|
case 'A': oper = "&"; break;
|
|
|
|
|
case 'O': oper = "|"; break;
|
|
|
|
|
case 'X': oper = "~^"; break;
|
|
|
|
|
case 'a': oper = "&&"; break;
|
|
|
|
|
case 'o': oper = "||"; break;
|
|
|
|
|
case 'l': oper = "<<"; break;
|
|
|
|
|
case 'r': oper = ">>"; break;
|
|
|
|
|
case 'R': oper = ">>>"; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(vlog_out, "(");
|
|
|
|
|
switch(ivl_expr_opcode(expr)) {
|
2011-01-22 00:33:25 +01:00
|
|
|
case '%':
|
|
|
|
|
if (ivl_expr_value(expr) == IVL_VT_REAL) {
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Real modulus operator "
|
|
|
|
|
"is not supported.\n",
|
|
|
|
|
ivl_expr_file(expr), ivl_expr_lineno(expr));
|
|
|
|
|
vlog_errors += 1;
|
|
|
|
|
}
|
2011-01-10 18:30:27 +01:00
|
|
|
case '+':
|
|
|
|
|
case '-':
|
|
|
|
|
case '*':
|
|
|
|
|
case '/':
|
|
|
|
|
case 'E':
|
|
|
|
|
case 'e':
|
|
|
|
|
case 'N':
|
|
|
|
|
case 'n':
|
|
|
|
|
case '<':
|
|
|
|
|
case 'L':
|
|
|
|
|
case '>':
|
|
|
|
|
case 'G':
|
|
|
|
|
case '&':
|
|
|
|
|
case '|':
|
|
|
|
|
case '^':
|
|
|
|
|
case 'X':
|
2011-01-11 18:22:49 +01:00
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
|
|
|
|
fprintf(vlog_out, " %s ", oper);
|
|
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), wid);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case 'a':
|
|
|
|
|
case 'o':
|
2011-01-11 18:22:49 +01:00
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), 0);
|
|
|
|
|
fprintf(vlog_out, " %s ", oper);
|
|
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), 0);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case 'l':
|
|
|
|
|
case 'r':
|
|
|
|
|
case 'R':
|
|
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
|
|
|
|
fprintf(vlog_out, " %s ", oper);
|
2011-01-11 18:22:49 +01:00
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), 0);
|
2011-01-10 18:30:27 +01:00
|
|
|
break;
|
|
|
|
|
case 'A':
|
|
|
|
|
case 'O':
|
|
|
|
|
fprintf(vlog_out, "~(");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
|
|
|
|
fprintf(vlog_out, " %s ", oper);
|
|
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), wid);
|
|
|
|
|
fprintf(vlog_out, ")");
|
|
|
|
|
break;
|
|
|
|
|
case 'p':
|
2011-01-18 22:46:18 +01:00
|
|
|
if (! emit_power_as_shift(scope, expr, wid)) {
|
|
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
|
|
|
|
fprintf(vlog_out, " ** ");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), 0);
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Power operator is not "
|
|
|
|
|
"supported.\n",
|
|
|
|
|
ivl_expr_file(expr), ivl_expr_lineno(expr));
|
|
|
|
|
vlog_errors += 1;
|
|
|
|
|
}
|
2011-01-10 18:30:27 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
|
|
|
|
fprintf(vlog_out, "<unknown>");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), wid);
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Unknown expression "
|
|
|
|
|
"operator (%c).\n",
|
|
|
|
|
ivl_expr_file(expr),
|
|
|
|
|
ivl_expr_lineno(expr),
|
|
|
|
|
ivl_expr_opcode(expr));
|
|
|
|
|
vlog_errors += 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
fprintf(vlog_out, ")");
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-11 18:22:49 +01:00
|
|
|
static void emit_expr_concat(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
unsigned repeat = ivl_expr_repeat(expr);
|
|
|
|
|
unsigned idx, count = ivl_expr_parms(expr);
|
|
|
|
|
|
|
|
|
|
if (repeat != 1) fprintf(vlog_out, "{%u", repeat);
|
|
|
|
|
fprintf(vlog_out, "{");
|
2011-01-22 00:33:25 +01:00
|
|
|
count -= 1;
|
2011-01-11 18:22:49 +01:00
|
|
|
for (idx = 0; idx < count; idx += 1) {
|
|
|
|
|
emit_expr(scope, ivl_expr_parm(expr, idx), 0);
|
2011-01-22 00:33:25 +01:00
|
|
|
fprintf(vlog_out, ", ");
|
2011-01-11 18:22:49 +01:00
|
|
|
}
|
2011-01-22 00:33:25 +01:00
|
|
|
emit_expr(scope, ivl_expr_parm(expr, count), 0);
|
2011-01-11 18:22:49 +01:00
|
|
|
fprintf(vlog_out, "}");
|
|
|
|
|
if (repeat != 1) fprintf(vlog_out, "}");
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-13 23:11:41 +01:00
|
|
|
static void emit_expr_delay(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
emit_scaled_delay(scope, ivl_expr_delay_val(expr));
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-31 08:53:53 +01:00
|
|
|
/*
|
|
|
|
|
* An event in an expression context must be a named event.
|
|
|
|
|
*/
|
|
|
|
|
static void emit_expr_event(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
2011-01-28 23:52:32 +01:00
|
|
|
{
|
2011-01-31 08:53:53 +01:00
|
|
|
ivl_event_t event = ivl_expr_event(expr);
|
|
|
|
|
ivl_scope_t ev_scope = ivl_event_scope(event);
|
|
|
|
|
assert(! ivl_event_nany(event));
|
|
|
|
|
assert(! ivl_event_npos(event));
|
|
|
|
|
assert(! ivl_event_nneg(event));
|
|
|
|
|
emit_scope_module_path(scope, ev_scope);
|
|
|
|
|
fprintf(vlog_out, "%s", ivl_event_basename(event));
|
2011-01-28 23:52:32 +01:00
|
|
|
}
|
|
|
|
|
|
2011-01-14 06:01:04 +01:00
|
|
|
static void emit_expr_scope(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
fprintf(vlog_out, "%s", ivl_scope_name(ivl_expr_scope(expr)));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-05 03:20:47 +01:00
|
|
|
static unsigned emit_param_name_in_scope(ivl_scope_t scope, ivl_expr_t expr)
|
|
|
|
|
{
|
|
|
|
|
unsigned idx, count, lineno;
|
|
|
|
|
const char* file;
|
|
|
|
|
count = ivl_scope_params(scope);
|
|
|
|
|
file = ivl_expr_file(expr);
|
|
|
|
|
lineno = ivl_expr_lineno(expr);
|
|
|
|
|
for (idx = 0; idx < count; idx += 1) {
|
|
|
|
|
ivl_parameter_t par = ivl_scope_param(scope, idx);
|
|
|
|
|
if (lineno != ivl_parameter_lineno(par)) continue;
|
|
|
|
|
if (strcmp(file, ivl_parameter_file(par)) == 0) {
|
|
|
|
|
/* Check that the appropriate expression bits match the
|
|
|
|
|
* the original parameter bits. */
|
|
|
|
|
ivl_expr_t pex = ivl_parameter_expr(par);
|
|
|
|
|
unsigned wid = ivl_expr_width(expr);
|
|
|
|
|
unsigned param_wid = ivl_expr_width(pex);
|
|
|
|
|
const char *my_bits = ivl_expr_bits(expr);
|
|
|
|
|
const char *param_bits = ivl_expr_bits(pex);
|
|
|
|
|
unsigned bit;
|
|
|
|
|
if (param_wid < wid) wid = param_wid;
|
|
|
|
|
for (bit = 0; bit < wid; bit += 1) {
|
|
|
|
|
if (my_bits[bit] != param_bits[bit]) {
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Constant "
|
|
|
|
|
"expression bits do not match "
|
|
|
|
|
"parameter bits.\n",
|
|
|
|
|
ivl_expr_file(expr),
|
|
|
|
|
ivl_expr_lineno(expr));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fprintf(vlog_out, "%s", ivl_parameter_basename(par));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void emit_select_name(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
2011-02-18 20:45:06 +01:00
|
|
|
/* A select of a number is really a parameter select. */
|
2011-02-05 03:20:47 +01:00
|
|
|
if (ivl_expr_type(expr) == IVL_EX_NUMBER) {
|
|
|
|
|
/* Look in the current scope. */
|
|
|
|
|
if (emit_param_name_in_scope(scope, expr)) return;
|
|
|
|
|
/* Now look in the enclosing module scope if this is not a
|
|
|
|
|
* module scope. */
|
|
|
|
|
if (ivl_scope_type(scope) != IVL_SCT_MODULE) {
|
|
|
|
|
if (emit_param_name_in_scope(get_module_scope(scope),
|
|
|
|
|
expr)) return;
|
|
|
|
|
}
|
|
|
|
|
// HERE: For now we only look in the current and module scope for the
|
|
|
|
|
// parameter that is being selected. We need to also look in other
|
|
|
|
|
// scopes, but that involves a big search.
|
|
|
|
|
fprintf(vlog_out, "<missing>");
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Unable to find parameter "
|
|
|
|
|
"for select expression \n",
|
|
|
|
|
ivl_expr_file(expr), ivl_expr_lineno(expr));
|
|
|
|
|
} else {
|
|
|
|
|
emit_expr(scope, expr, wid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 20:45:06 +01:00
|
|
|
/*
|
|
|
|
|
* Emit an indexed part select as a concatenation of bit selects.
|
|
|
|
|
*/
|
|
|
|
|
static void emit_expr_ips(ivl_scope_t scope, ivl_expr_t sig_expr,
|
|
|
|
|
ivl_expr_t sel_expr, unsigned wid,
|
|
|
|
|
unsigned msb, unsigned lsb)
|
|
|
|
|
{
|
|
|
|
|
unsigned idx;
|
|
|
|
|
if ((msb >= lsb) && (lsb != 0)) {
|
|
|
|
|
fprintf(vlog_out, "<unsupported>");
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 sorry: Variable indexed part "
|
|
|
|
|
"select lsb must be zero.\n",
|
|
|
|
|
ivl_expr_file(sel_expr),
|
|
|
|
|
ivl_expr_lineno(sel_expr));
|
|
|
|
|
vlog_errors += 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (msb < lsb) {
|
|
|
|
|
fprintf(vlog_out, "<unsupported>");
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 sorry: Variable indexed part "
|
|
|
|
|
"selects must be little endian.\n",
|
|
|
|
|
ivl_expr_file(sel_expr),
|
|
|
|
|
ivl_expr_lineno(sel_expr));
|
|
|
|
|
vlog_errors += 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fprintf(vlog_out, "{");
|
|
|
|
|
for (idx = wid - 1; idx > 0; idx -= 1) {
|
|
|
|
|
emit_select_name(scope, sig_expr, wid);
|
|
|
|
|
fprintf(vlog_out, "[");
|
|
|
|
|
// HERE: Ideally we should simplify the scaled expression and the addition
|
|
|
|
|
// of the idx value. Also we should simplify any offset with the base
|
|
|
|
|
// expression as a compiler enhancement. All this will require a
|
|
|
|
|
// modified/updated version of emit_scaled_expr(). We can remove the
|
|
|
|
|
// above fails when this is finished.
|
|
|
|
|
emit_scaled_expr(scope, sel_expr, msb, lsb);
|
|
|
|
|
fprintf(vlog_out, " + %u], ", idx);
|
|
|
|
|
}
|
|
|
|
|
emit_select_name(scope, sig_expr, wid);
|
|
|
|
|
fprintf(vlog_out, "[");
|
|
|
|
|
emit_scaled_expr(scope, sel_expr, msb, lsb);
|
|
|
|
|
fprintf(vlog_out, "]}");
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-10 18:30:27 +01:00
|
|
|
static void emit_expr_select(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
2011-01-17 19:51:05 +01:00
|
|
|
ivl_expr_t sel_expr = ivl_expr_oper2(expr);
|
2011-01-22 00:33:25 +01:00
|
|
|
ivl_expr_t sig_expr = ivl_expr_oper1(expr);
|
2011-01-17 19:51:05 +01:00
|
|
|
if (sel_expr) {
|
|
|
|
|
int msb = 1;
|
|
|
|
|
int lsb = 0;
|
|
|
|
|
unsigned width = ivl_expr_width(expr);
|
2011-02-05 03:20:47 +01:00
|
|
|
ivl_expr_type_t type = ivl_expr_type(sig_expr);
|
2011-01-17 19:51:05 +01:00
|
|
|
assert(width > 0);
|
2011-02-05 03:20:47 +01:00
|
|
|
if (type == IVL_EX_SIGNAL) {
|
2011-01-31 08:53:53 +01:00
|
|
|
ivl_signal_t sig = ivl_expr_signal(sig_expr);
|
2011-01-17 19:51:05 +01:00
|
|
|
msb = ivl_signal_msb(sig);
|
|
|
|
|
lsb = ivl_signal_lsb(sig);
|
|
|
|
|
}
|
2011-02-05 03:20:47 +01:00
|
|
|
/* The compiler uses selects for some shifts. */
|
|
|
|
|
if (type != IVL_EX_NUMBER && type != IVL_EX_SIGNAL) {
|
|
|
|
|
fprintf(vlog_out, "(" );
|
|
|
|
|
emit_select_name(scope, sig_expr, wid);
|
|
|
|
|
fprintf(vlog_out, " >> " );
|
2011-01-17 19:51:05 +01:00
|
|
|
emit_scaled_expr(scope, sel_expr, msb, lsb);
|
2011-02-05 03:20:47 +01:00
|
|
|
fprintf(vlog_out, ")" );
|
2011-01-17 19:51:05 +01:00
|
|
|
} else {
|
2011-02-18 20:45:06 +01:00
|
|
|
/* A bit select. */
|
2011-02-05 03:20:47 +01:00
|
|
|
if (width == 1) {
|
2011-02-18 20:45:06 +01:00
|
|
|
emit_select_name(scope, sig_expr, wid);
|
2011-02-05 03:20:47 +01:00
|
|
|
fprintf(vlog_out, "[");
|
|
|
|
|
emit_scaled_expr(scope, sel_expr, msb, lsb);
|
|
|
|
|
fprintf(vlog_out, "]");
|
|
|
|
|
} else {
|
2011-02-18 20:45:06 +01:00
|
|
|
if (ivl_expr_type(sel_expr) == IVL_EX_NUMBER) {
|
|
|
|
|
/* A constant part select. */
|
|
|
|
|
emit_select_name(scope, sig_expr, wid);
|
|
|
|
|
emit_scaled_range(scope, sel_expr, width,
|
|
|
|
|
msb, lsb);
|
|
|
|
|
} else {
|
|
|
|
|
/* An indexed part select. */
|
|
|
|
|
emit_expr_ips(scope, sig_expr, sel_expr, width,
|
|
|
|
|
msb, lsb);
|
|
|
|
|
}
|
2011-02-05 03:20:47 +01:00
|
|
|
}
|
2011-01-17 19:51:05 +01:00
|
|
|
}
|
2011-01-10 18:30:27 +01:00
|
|
|
} else {
|
2011-01-11 18:22:49 +01:00
|
|
|
// HERE: Should this sign extend if the expression is signed?
|
2011-01-22 00:33:25 +01:00
|
|
|
emit_expr(scope, sig_expr, wid);
|
2011-01-10 18:30:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-14 06:01:04 +01:00
|
|
|
/*
|
|
|
|
|
* This routine is used to emit both system and user functions.
|
|
|
|
|
*/
|
|
|
|
|
static void emit_expr_func(ivl_scope_t scope, ivl_expr_t expr, const char* name)
|
2011-01-11 18:22:49 +01:00
|
|
|
{
|
2011-01-18 02:42:45 +01:00
|
|
|
unsigned count = ivl_expr_parms(expr);
|
2011-01-14 06:01:04 +01:00
|
|
|
fprintf(vlog_out, "%s", name);
|
2011-01-11 18:22:49 +01:00
|
|
|
if (count != 0) {
|
2011-01-18 02:42:45 +01:00
|
|
|
unsigned idx;
|
2011-01-11 18:22:49 +01:00
|
|
|
fprintf(vlog_out, "(");
|
2011-01-14 06:01:04 +01:00
|
|
|
count -= 1;
|
2011-01-11 18:22:49 +01:00
|
|
|
for (idx = 0; idx < count; idx += 1) {
|
|
|
|
|
emit_expr(scope, ivl_expr_parm(expr, idx), 0);
|
2011-01-14 06:01:04 +01:00
|
|
|
fprintf(vlog_out, ", ");
|
2011-01-11 18:22:49 +01:00
|
|
|
}
|
2011-01-14 06:01:04 +01:00
|
|
|
emit_expr(scope, ivl_expr_parm(expr, count), 0);
|
2011-01-11 18:22:49 +01:00
|
|
|
fprintf(vlog_out, ")");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-14 06:01:04 +01:00
|
|
|
static void emit_expr_sfunc(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
emit_expr_func(scope, expr, ivl_expr_name(expr));
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-10 18:30:27 +01:00
|
|
|
static void emit_expr_signal(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
ivl_signal_t sig = ivl_expr_signal(expr);
|
2011-02-11 18:30:42 +01:00
|
|
|
emit_scope_module_path(scope, ivl_signal_scope(sig));
|
|
|
|
|
fprintf(vlog_out, "%s", ivl_signal_basename(sig));
|
|
|
|
|
if (ivl_signal_dimensions(sig)) {
|
|
|
|
|
int lsb = ivl_signal_array_base(sig);
|
|
|
|
|
int msb = lsb + ivl_signal_array_count(sig);
|
|
|
|
|
fprintf(vlog_out, "[");
|
|
|
|
|
emit_scaled_expr(scope, ivl_expr_oper1(expr), msb, lsb);
|
|
|
|
|
fprintf(vlog_out, "]");
|
2011-01-17 19:51:05 +01:00
|
|
|
}
|
2011-01-10 18:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
2011-01-11 18:22:49 +01:00
|
|
|
static void emit_expr_ternary(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
fprintf(vlog_out, "(");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), 0);
|
|
|
|
|
fprintf(vlog_out, " ? ");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper2(expr), wid);
|
|
|
|
|
fprintf(vlog_out, " : ");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper3(expr), wid);
|
|
|
|
|
fprintf(vlog_out, ")");
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-14 06:01:04 +01:00
|
|
|
static void emit_expr_ufunc(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
ivl_scope_t ufunc_def = ivl_expr_def(expr);
|
2011-01-31 08:53:53 +01:00
|
|
|
emit_scope_module_path(scope, ufunc_def);
|
2011-01-14 06:01:04 +01:00
|
|
|
emit_expr_func(scope, expr, ivl_scope_tname(ufunc_def));
|
|
|
|
|
}
|
|
|
|
|
|
2011-01-10 18:30:27 +01:00
|
|
|
static void emit_expr_unary(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
char *oper = "invalid";
|
|
|
|
|
switch (ivl_expr_opcode(expr)) {
|
|
|
|
|
case '-': oper = "-"; break;
|
|
|
|
|
case '~': oper = "~"; break;
|
|
|
|
|
case '&': oper = "&"; break;
|
|
|
|
|
case '|': oper = "|"; break;
|
|
|
|
|
case '^': oper = "^"; break;
|
|
|
|
|
case 'A': oper = "~&"; break;
|
|
|
|
|
case 'N': oper = "~|"; break;
|
|
|
|
|
case 'X': oper = "~^"; break;
|
|
|
|
|
case '!': oper = "!"; break;
|
|
|
|
|
}
|
|
|
|
|
switch (ivl_expr_opcode(expr)) {
|
|
|
|
|
case '-':
|
|
|
|
|
case '~':
|
|
|
|
|
case '&':
|
|
|
|
|
case '|':
|
|
|
|
|
case '^':
|
|
|
|
|
case 'A':
|
|
|
|
|
case 'N':
|
|
|
|
|
case 'X':
|
2011-01-22 00:33:25 +01:00
|
|
|
case '!':
|
2011-02-12 03:14:16 +01:00
|
|
|
fprintf(vlog_out, "(%s", oper);
|
2011-01-10 18:30:27 +01:00
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
2011-02-12 03:14:16 +01:00
|
|
|
fprintf(vlog_out, ")");
|
2011-01-10 18:30:27 +01:00
|
|
|
break;
|
|
|
|
|
case '2':
|
|
|
|
|
case 'i':
|
|
|
|
|
case 'r':
|
|
|
|
|
/* A cast is a noop. */
|
2011-02-11 05:42:51 +01:00
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
2011-01-10 18:30:27 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fprintf(vlog_out, "<unknown>");
|
|
|
|
|
emit_expr(scope, ivl_expr_oper1(expr), wid);
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Unknown unary "
|
|
|
|
|
"operator (%c).\n",
|
|
|
|
|
ivl_expr_file(expr),
|
|
|
|
|
ivl_expr_lineno(expr),
|
|
|
|
|
ivl_expr_opcode(expr));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void emit_expr(ivl_scope_t scope, ivl_expr_t expr, unsigned wid)
|
|
|
|
|
{
|
|
|
|
|
switch(ivl_expr_type(expr)) {
|
2011-01-28 23:52:32 +01:00
|
|
|
case IVL_EX_ARRAY:
|
|
|
|
|
emit_expr_array(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case IVL_EX_BINARY:
|
|
|
|
|
emit_expr_binary(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-11 18:22:49 +01:00
|
|
|
case IVL_EX_CONCAT:
|
|
|
|
|
emit_expr_concat(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-13 23:11:41 +01:00
|
|
|
case IVL_EX_DELAY:
|
|
|
|
|
emit_expr_delay(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-31 08:53:53 +01:00
|
|
|
case IVL_EX_EVENT:
|
|
|
|
|
emit_expr_event(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case IVL_EX_NUMBER:
|
2011-01-31 08:53:53 +01:00
|
|
|
emit_number(ivl_expr_bits(expr), ivl_expr_width(expr),
|
|
|
|
|
ivl_expr_signed(expr), ivl_expr_file(expr),
|
|
|
|
|
ivl_expr_lineno(expr));
|
2011-01-10 18:30:27 +01:00
|
|
|
break;
|
|
|
|
|
case IVL_EX_REALNUM:
|
2011-01-31 08:53:53 +01:00
|
|
|
emit_real_number(ivl_expr_dvalue(expr));
|
2011-01-10 18:30:27 +01:00
|
|
|
break;
|
2011-01-14 06:01:04 +01:00
|
|
|
case IVL_EX_SCOPE:
|
|
|
|
|
emit_expr_scope(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case IVL_EX_SELECT:
|
|
|
|
|
emit_expr_select(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-11 18:22:49 +01:00
|
|
|
case IVL_EX_SFUNC:
|
|
|
|
|
emit_expr_sfunc(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case IVL_EX_SIGNAL:
|
|
|
|
|
emit_expr_signal(scope, expr, wid);
|
|
|
|
|
break;
|
|
|
|
|
case IVL_EX_STRING:
|
|
|
|
|
fprintf(vlog_out, "\"%s\"", ivl_expr_string(expr));
|
|
|
|
|
break;
|
2011-01-11 18:22:49 +01:00
|
|
|
case IVL_EX_TERNARY:
|
|
|
|
|
emit_expr_ternary(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-14 06:01:04 +01:00
|
|
|
case IVL_EX_UFUNC:
|
|
|
|
|
emit_expr_ufunc(scope, expr, wid);
|
|
|
|
|
break;
|
2011-01-10 18:30:27 +01:00
|
|
|
case IVL_EX_UNARY:
|
|
|
|
|
emit_expr_unary(scope, expr, wid);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
fprintf(vlog_out, "<unknown>");
|
|
|
|
|
fprintf(stderr, "%s:%u: vlog95 error: Unknown expression "
|
|
|
|
|
"type (%d).\n",
|
|
|
|
|
ivl_expr_file(expr),
|
|
|
|
|
ivl_expr_lineno(expr),
|
|
|
|
|
(int)ivl_expr_type(expr));
|
|
|
|
|
vlog_errors += 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|