mirror of
https://github.com/steveicarus/iverilog.git
synced 2026-08-29 01:03:29 +02:00
Properly handle base-type logic enumerations.
The next()/prev() methods need to know of the base type so that the comparisons can go right.
This commit is contained in:
+4
-1
@@ -202,7 +202,10 @@ extern void compile_dff(char*label,
|
||||
struct symb_s arg_e,
|
||||
struct symb_s arg_a);
|
||||
|
||||
extern void compile_enum_type(char*label, std::list<struct enum_name_s>*names);
|
||||
extern void compile_enum2_type(char*label, long width,
|
||||
std::list<struct enum_name_s>*names);
|
||||
extern void compile_enum4_type(char*label, long width,
|
||||
std::list<struct enum_name_s>*names);
|
||||
|
||||
class __vpiModPath;
|
||||
extern __vpiModPath* compile_modpath(char*label,
|
||||
|
||||
+64
-14
@@ -26,6 +26,7 @@ struct enumconst_s {
|
||||
struct __vpiHandle base;
|
||||
const char*name;
|
||||
vvp_vector2_t val2;
|
||||
vvp_vector4_t val4;
|
||||
};
|
||||
|
||||
static struct enumconst_s* enumconst_from_handle(vpiHandle obj)
|
||||
@@ -49,6 +50,22 @@ static struct __vpiEnumTypespec* vpip_enum_typespec_from_handle(vpiHandle obj)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int enum_type_get(int code, vpiHandle obj)
|
||||
{
|
||||
struct __vpiEnumTypespec*ref = vpip_enum_typespec_from_handle(obj);
|
||||
assert(ref);
|
||||
|
||||
switch (code) {
|
||||
case vpiSize:
|
||||
return ref->names.size();
|
||||
default:
|
||||
fprintf(stderr, "vvp error: get %d not supported "
|
||||
"by __vpiEnumTypespec\n", code);
|
||||
assert(0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static vpiHandle enum_type_iterate(int code, vpiHandle obj)
|
||||
{
|
||||
struct __vpiEnumTypespec*ref = vpip_enum_typespec_from_handle(obj);
|
||||
@@ -57,7 +74,7 @@ static vpiHandle enum_type_iterate(int code, vpiHandle obj)
|
||||
if (code == vpiMember) {
|
||||
vpiHandle*args = (vpiHandle*)
|
||||
calloc(ref->names.size(), sizeof(vpiHandle*));
|
||||
for (int idx = 0 ; idx < ref->names.size() ; idx += 1)
|
||||
for (size_t idx = 0 ; idx < ref->names.size() ; idx += 1)
|
||||
args[idx] = vpi_handle(&ref->names[idx]);
|
||||
|
||||
return vpip_make_iterator(ref->names.size(), args, true);
|
||||
@@ -68,7 +85,7 @@ static vpiHandle enum_type_iterate(int code, vpiHandle obj)
|
||||
|
||||
static const struct __vpirt enum_type_rt = {
|
||||
vpiEnumTypespec,
|
||||
0, //enum_type_get,
|
||||
enum_type_get,
|
||||
0, //enum_type_get_str,
|
||||
0, //enum_type_get_value,
|
||||
0, //enum_type_put_value,
|
||||
@@ -80,6 +97,19 @@ static const struct __vpirt enum_type_rt = {
|
||||
0, //enum_type_put_delays
|
||||
};
|
||||
|
||||
static int enum_name_get(int code, vpiHandle obj)
|
||||
{
|
||||
struct enumconst_s*ref = enumconst_from_handle(obj);
|
||||
assert(ref);
|
||||
|
||||
switch (code) {
|
||||
case vpiSize:
|
||||
return ref->val4.size()? ref->val4.size() : ref->val2.size();
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static char* enum_name_get_str(int code, vpiHandle obj)
|
||||
{
|
||||
struct enumconst_s*ref = enumconst_from_handle(obj);
|
||||
@@ -98,20 +128,15 @@ static void enum_name_get_value(vpiHandle obj, p_vpi_value value)
|
||||
struct enumconst_s*ref = enumconst_from_handle(obj);
|
||||
assert(ref);
|
||||
|
||||
switch (value->format) {
|
||||
case vpiObjTypeVal:
|
||||
value->format = vpiIntVal;
|
||||
case vpiIntVal:
|
||||
vector2_to_value(ref->val2, value->value.integer, true);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (ref->val4.size() > 0)
|
||||
vpip_vec4_get_value(ref->val4, ref->val4.size(), false, value);
|
||||
else
|
||||
vpip_vec2_get_value(ref->val2, ref->val2.size(), true, value);
|
||||
}
|
||||
|
||||
static const struct __vpirt enum_name_rt = {
|
||||
vpiEnumConst,
|
||||
0, //enum_name_get,
|
||||
enum_name_get,
|
||||
enum_name_get_str,
|
||||
enum_name_get_value,
|
||||
0, //enum_name_put_value,
|
||||
@@ -123,7 +148,29 @@ static const struct __vpirt enum_name_rt = {
|
||||
0, //enum_name_put_delays
|
||||
};
|
||||
|
||||
void compile_enum_type(char*label, std::list<struct enum_name_s>*names)
|
||||
void compile_enum2_type(char*label, long width, std::list<struct enum_name_s>*names)
|
||||
{
|
||||
struct __vpiEnumTypespec*spec = new struct __vpiEnumTypespec;
|
||||
spec->base.vpi_type = &enum_type_rt;
|
||||
spec->names = std::vector<enumconst_s> (names->size());
|
||||
|
||||
size_t idx = 0;
|
||||
for (list<struct enum_name_s>::iterator cur = names->begin()
|
||||
; cur != names->end() ; ++cur, ++idx) {
|
||||
assert(cur->val4 == 0);
|
||||
spec->names[idx].base.vpi_type = &enum_name_rt;
|
||||
spec->names[idx].name = cur->text;
|
||||
spec->names[idx].val2 = vvp_vector2_t(cur->val2, width);
|
||||
}
|
||||
|
||||
assert(idx == spec->names.size());
|
||||
compile_vpi_symbol(label, vpi_handle(spec));
|
||||
|
||||
free(label);
|
||||
delete names;
|
||||
}
|
||||
|
||||
void compile_enum4_type(char*label, long width, std::list<struct enum_name_s>*names)
|
||||
{
|
||||
struct __vpiEnumTypespec*spec = new struct __vpiEnumTypespec;
|
||||
spec->base.vpi_type = &enum_type_rt;
|
||||
@@ -134,7 +181,10 @@ void compile_enum_type(char*label, std::list<struct enum_name_s>*names)
|
||||
; cur != names->end() ; ++cur, ++idx) {
|
||||
spec->names[idx].base.vpi_type = &enum_name_rt;
|
||||
spec->names[idx].name = cur->text;
|
||||
spec->names[idx].val2 = vvp_vector2_t(cur->val2, 32);
|
||||
assert(cur->val4);
|
||||
spec->names[idx].val4 = vector4_from_text(cur->val4, width);
|
||||
free(cur->val4);
|
||||
cur->val4 = 0;
|
||||
}
|
||||
|
||||
assert(idx == spec->names.size());
|
||||
|
||||
+2
-1
@@ -142,7 +142,8 @@ static char* strdupnew(char const *str)
|
||||
".concat" { return K_CONCAT; }
|
||||
".delay" { return K_DELAY; }
|
||||
".dff" { return K_DFF; }
|
||||
".enum" { return K_ENUM; }
|
||||
".enum2" { return K_ENUM2; }
|
||||
".enum4" { return K_ENUM4; }
|
||||
".event" { return K_EVENT; }
|
||||
".event/or" { return K_EVENT_OR; }
|
||||
".export" { return K_EXPORT; }
|
||||
|
||||
+9
-4
@@ -80,7 +80,8 @@ static struct __vpiModPath*modpath_dst = 0;
|
||||
%token K_CMP_EEQ K_CMP_EQ K_CMP_EQ_R K_CMP_NEE K_CMP_NE K_CMP_NE_R
|
||||
%token K_CMP_GE K_CMP_GE_R K_CMP_GE_S K_CMP_GT K_CMP_GT_R K_CMP_GT_S
|
||||
%token K_CONCAT K_DEBUG K_DELAY K_DFF
|
||||
%token K_ENUM K_EVENT K_EVENT_OR K_EXPORT K_EXTEND_S K_FUNCTOR K_IMPORT K_ISLAND
|
||||
%token K_ENUM2 K_ENUM4 K_EVENT K_EVENT_OR K_EXPORT K_EXTEND_S K_FUNCTOR
|
||||
%token K_IMPORT K_ISLAND
|
||||
%token K_MODPATH
|
||||
%token K_NET K_NET_S K_NET_R K_NET_2S K_NET_2U K_NET8 K_NET8_S
|
||||
%token K_PARAM_STR K_PARAM_L K_PARAM_REAL K_PART K_PART_PV
|
||||
@@ -799,8 +800,10 @@ statement
|
||||
|
||||
/* Enumeration types */
|
||||
enum_type
|
||||
: T_LABEL K_ENUM enum_type_names ';'
|
||||
{ compile_enum_type($1, $3); }
|
||||
: T_LABEL K_ENUM2 '(' T_NUMBER ')' enum_type_names ';'
|
||||
{ compile_enum2_type($1, $4, $6); }
|
||||
| T_LABEL K_ENUM4 '(' T_NUMBER ')' enum_type_names ';'
|
||||
{ compile_enum4_type($1, $4, $6); }
|
||||
;
|
||||
|
||||
enum_type_names
|
||||
@@ -818,7 +821,9 @@ enum_type_names
|
||||
|
||||
enum_type_name
|
||||
: T_STRING T_NUMBER
|
||||
{ $$.text = $1; $$.val2 = $2; }
|
||||
{ $$.text = $1; $$.val2 = $2; $$.val4 = 0; }
|
||||
| T_STRING T_VECTOR
|
||||
{ $$.text = $1; $$.val2 = 0; $$.val4 = $2.text; }
|
||||
;
|
||||
|
||||
local_flag
|
||||
|
||||
@@ -73,6 +73,7 @@ struct numbv_s {
|
||||
struct enum_name_s {
|
||||
char*text;
|
||||
uint64_t val2;
|
||||
char*val4;
|
||||
};
|
||||
|
||||
extern void numbv_init(struct numbv_s*obj);
|
||||
|
||||
+12
-4
@@ -442,7 +442,6 @@ vpiHandle vpip_make_binary_const(unsigned wid, const char*bits)
|
||||
|
||||
obj->signed_flag = 0;
|
||||
obj->sized_flag = 0;
|
||||
obj->bits = vvp_vector4_t(wid);
|
||||
|
||||
const char*bp = bits;
|
||||
if (*bp == 's') {
|
||||
@@ -450,9 +449,18 @@ vpiHandle vpip_make_binary_const(unsigned wid, const char*bits)
|
||||
obj->signed_flag = 1;
|
||||
}
|
||||
|
||||
obj->bits = vector4_from_text(bp, wid);
|
||||
|
||||
return &(obj->base);
|
||||
}
|
||||
|
||||
vvp_vector4_t vector4_from_text(const char*bits, unsigned wid)
|
||||
{
|
||||
vvp_vector4_t res (wid);
|
||||
|
||||
for (unsigned idx = 0 ; idx < wid ; idx += 1) {
|
||||
vvp_bit4_t val = BIT4_0;
|
||||
switch (bp[wid-idx-1]) {
|
||||
switch (bits[wid-idx-1]) {
|
||||
case '0':
|
||||
val = BIT4_0;
|
||||
break;
|
||||
@@ -467,10 +475,10 @@ vpiHandle vpip_make_binary_const(unsigned wid, const char*bits)
|
||||
break;
|
||||
}
|
||||
|
||||
obj->bits.set_bit(idx, val);
|
||||
res.set_bit(idx, val);
|
||||
}
|
||||
|
||||
return &(obj->base);
|
||||
return res;
|
||||
}
|
||||
|
||||
struct __vpiBinaryParam : public __vpiBinaryConst {
|
||||
|
||||
@@ -711,6 +711,53 @@ void vpip_vec4_get_value(const vvp_vector4_t&word_val, unsigned width,
|
||||
}
|
||||
}
|
||||
|
||||
void vpip_vec2_get_value(const vvp_vector2_t&word_val, unsigned width,
|
||||
bool signed_flag, s_vpi_value*vp)
|
||||
{
|
||||
char *rbuf = 0;
|
||||
|
||||
switch (vp->format) {
|
||||
default:
|
||||
fprintf(stderr, "sorry: Format %d not implemented for "
|
||||
"getting vector2 values.\n", (int)vp->format);
|
||||
assert(0);
|
||||
|
||||
case vpiSuppressVal:
|
||||
break;
|
||||
|
||||
case vpiObjTypeVal:
|
||||
vp->format = vpiIntVal;
|
||||
case vpiIntVal:
|
||||
vector2_to_value(word_val, vp->value.integer, true);
|
||||
break;
|
||||
|
||||
case vpiVectorVal: {
|
||||
unsigned hwid = (width - 1)/32 + 1;
|
||||
|
||||
rbuf = need_result_buf(hwid * sizeof(s_vpi_vecval), RBUF_VAL);
|
||||
s_vpi_vecval *op = (p_vpi_vecval)rbuf;
|
||||
vp->value.vector = op;
|
||||
|
||||
op->aval = op->bval = 0;
|
||||
for (unsigned idx = 0 ; idx < width ; idx += 1) {
|
||||
if (word_val.value(idx)) {
|
||||
op->aval |= (1 << idx % 32);
|
||||
op->bval &= ~(1 << idx % 32);
|
||||
} else {
|
||||
op->aval &= ~(1 << idx % 32);
|
||||
op->bval &= ~(1 << idx % 32);
|
||||
}
|
||||
if (!((idx+1) % 32) && (idx+1 < width)) {
|
||||
op++;
|
||||
op->aval = op->bval = 0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert a real value to the appropriate integer.
|
||||
*/
|
||||
|
||||
@@ -611,6 +611,8 @@ extern double real_from_vpi_value(s_vpi_value*vp);
|
||||
|
||||
extern void vpip_vec4_get_value(const vvp_vector4_t&word_val, unsigned width,
|
||||
bool signed_flag, s_vpi_value*vp);
|
||||
extern void vpip_vec2_get_value(const vvp_vector2_t&word_val, unsigned width,
|
||||
bool signed_flag, s_vpi_value*vp);
|
||||
extern void vpip_real_get_value(double real, s_vpi_value*vp);
|
||||
|
||||
/*
|
||||
|
||||
+17
-34
@@ -1527,11 +1527,11 @@ ostream& operator<< (ostream&out, const vvp_vector4_t&that)
|
||||
return out;
|
||||
}
|
||||
|
||||
bool vector4_to_value(const vvp_vector4_t&vec, long&val,
|
||||
bool is_signed, bool is_arithmetic)
|
||||
template <class INT>bool do_vector4_to_value(const vvp_vector4_t&vec, INT&val,
|
||||
bool is_signed, bool is_arithmetic)
|
||||
{
|
||||
long res = 0;
|
||||
long msk = 1;
|
||||
INT msk = 1;
|
||||
bool rc_flag = true;
|
||||
|
||||
unsigned size = vec.size();
|
||||
@@ -1555,13 +1555,25 @@ bool vector4_to_value(const vvp_vector4_t&vec, long&val,
|
||||
|
||||
if (is_signed && vec.value(vec.size()-1) == BIT4_1) {
|
||||
if (vec.size() < 8*sizeof(val))
|
||||
res |= (-1L) << vec.size();
|
||||
res |= (INT)(-1L) << vec.size();
|
||||
}
|
||||
|
||||
val = res;
|
||||
return rc_flag;
|
||||
}
|
||||
|
||||
bool vector4_to_value(const vvp_vector4_t&vec, long&val,
|
||||
bool is_signed, bool is_arithmetic)
|
||||
{
|
||||
return do_vector4_to_value(vec, val, is_signed, is_arithmetic);
|
||||
}
|
||||
|
||||
bool vector4_to_value(const vvp_vector4_t&vec, int32_t&val,
|
||||
bool is_signed, bool is_arithmetic)
|
||||
{
|
||||
return do_vector4_to_value(vec, val, is_signed, is_arithmetic);
|
||||
}
|
||||
|
||||
bool vector4_to_value(const vvp_vector4_t&vec, unsigned long&val)
|
||||
{
|
||||
unsigned long res = 0;
|
||||
@@ -1591,36 +1603,7 @@ bool vector4_to_value(const vvp_vector4_t&vec, unsigned long&val)
|
||||
bool vector4_to_value(const vvp_vector4_t&vec, int64_t&val,
|
||||
bool is_signed, bool is_arithmetic)
|
||||
{
|
||||
long res = 0;
|
||||
long msk = 1;
|
||||
bool rc_flag = true;
|
||||
|
||||
unsigned size = vec.size();
|
||||
if (size > 8*sizeof(val)) size = 8*sizeof(val);
|
||||
for (unsigned idx = 0 ; idx < size ; idx += 1) {
|
||||
switch (vec.value(idx)) {
|
||||
case BIT4_0:
|
||||
break;
|
||||
case BIT4_1:
|
||||
res |= msk;
|
||||
break;
|
||||
default:
|
||||
if (is_arithmetic)
|
||||
return false;
|
||||
else
|
||||
rc_flag = false;
|
||||
}
|
||||
|
||||
msk <<= 1L;
|
||||
}
|
||||
|
||||
if (is_signed && vec.value(vec.size()-1) == BIT4_1) {
|
||||
if (vec.size() < 8*sizeof(val))
|
||||
res |= (-1L) << vec.size();
|
||||
}
|
||||
|
||||
val = res;
|
||||
return rc_flag;
|
||||
return do_vector4_to_value(vec, val, is_signed, is_arithmetic);
|
||||
}
|
||||
|
||||
bool vector4_to_value(const vvp_vector4_t&vec, vvp_time64_t&val)
|
||||
|
||||
@@ -490,6 +490,7 @@ template <class T> extern T coerce_to_width(const T&that, unsigned width);
|
||||
*/
|
||||
extern bool vector4_to_value(const vvp_vector4_t&a, long&val, bool is_signed, bool is_arithmetic =true);
|
||||
extern bool vector4_to_value(const vvp_vector4_t&a, unsigned long&val);
|
||||
extern bool vector4_to_value(const vvp_vector4_t&a, int32_t&val, bool is_signed, bool is_arithmetic =true);
|
||||
#ifndef UL_AND_TIME64_SAME
|
||||
extern bool vector4_to_value(const vvp_vector4_t&a, int64_t&val, bool is_signed, bool is_arithmetic =true);
|
||||
extern bool vector4_to_value(const vvp_vector4_t&a, vvp_time64_t&val);
|
||||
@@ -498,6 +499,8 @@ extern bool vector4_to_value(const vvp_vector4_t&a, double&val, bool is_signed);
|
||||
|
||||
extern bool vector2_to_value(const vvp_vector2_t&a, int32_t&val, bool is_signed);
|
||||
|
||||
extern vvp_vector4_t vector4_from_text(const char*bits, unsigned wid);
|
||||
|
||||
/*
|
||||
* The __vpiArray handle uses instances of this to keep an array of
|
||||
* real valued variables.
|
||||
|
||||
Reference in New Issue
Block a user