2010-11-18 05:00:23 +01:00
|
|
|
/*
|
2012-01-20 00:04:51 +01:00
|
|
|
* Copyright (c) 2010-2012 Stephen Williams (steve@icarus.com)
|
2010-11-18 05:00:23 +01:00
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form 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
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2010-11-18 05:00:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
# include "compile.h"
|
|
|
|
|
# include "enum_type.h"
|
2011-10-10 07:20:40 +02:00
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
|
|
|
# include "vvp_cleanup.h"
|
|
|
|
|
#endif
|
2010-11-18 05:00:23 +01:00
|
|
|
# include <iostream>
|
|
|
|
|
# include <cassert>
|
|
|
|
|
|
2012-01-19 19:16:39 +01:00
|
|
|
struct enumconst_s : public __vpiHandle {
|
|
|
|
|
enumconst_s();
|
2012-01-20 00:04:51 +01:00
|
|
|
int get_type_code(void) const;
|
2012-01-20 20:39:48 +01:00
|
|
|
int vpi_get(int code);
|
|
|
|
|
char* vpi_get_str(int code);
|
|
|
|
|
void vpi_get_value(p_vpi_value val);
|
2012-01-19 19:16:39 +01:00
|
|
|
|
2010-11-18 05:00:23 +01:00
|
|
|
const char*name;
|
|
|
|
|
vvp_vector2_t val2;
|
2010-11-22 02:24:46 +01:00
|
|
|
vvp_vector4_t val4;
|
2010-11-18 05:00:23 +01:00
|
|
|
};
|
|
|
|
|
|
2012-01-19 19:16:39 +01:00
|
|
|
struct __vpiEnumTypespec : public __vpiHandle {
|
|
|
|
|
__vpiEnumTypespec();
|
2012-01-20 00:04:51 +01:00
|
|
|
int get_type_code(void) const;
|
2012-01-20 20:39:48 +01:00
|
|
|
int vpi_get(int code);
|
|
|
|
|
vpiHandle vpi_iterate(int code);
|
2010-11-18 05:00:23 +01:00
|
|
|
|
|
|
|
|
std::vector<enumconst_s> names;
|
2011-10-12 03:27:45 +02:00
|
|
|
int base_type_code;
|
2011-09-20 03:50:06 +02:00
|
|
|
bool is_signed;
|
2010-11-18 05:00:23 +01:00
|
|
|
};
|
|
|
|
|
|
2010-11-22 02:24:46 +01:00
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
inline __vpiEnumTypespec::__vpiEnumTypespec()
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
int __vpiEnumTypespec::get_type_code(void) const
|
|
|
|
|
{ return vpiEnumTypespec; }
|
|
|
|
|
|
|
|
|
|
int __vpiEnumTypespec::vpi_get(int code)
|
|
|
|
|
{
|
2010-11-22 02:24:46 +01:00
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
2012-01-21 02:16:42 +01:00
|
|
|
return names.size();
|
2011-09-20 03:50:06 +02:00
|
|
|
|
2011-10-12 03:27:45 +02:00
|
|
|
/* This is not currently set correctly. We always use vpiReg for
|
|
|
|
|
* four state variables and vpiBitVar for two state variables.
|
|
|
|
|
* This minimal functionality is needed to get the next() and
|
|
|
|
|
* prev() methods to work correctly with invalid values. */
|
|
|
|
|
case vpiBaseTypespec:
|
2012-01-21 02:16:42 +01:00
|
|
|
return base_type_code;
|
2011-10-12 03:27:45 +02:00
|
|
|
|
2011-09-20 03:50:06 +02:00
|
|
|
case vpiSigned:
|
2012-01-21 02:16:42 +01:00
|
|
|
return is_signed;
|
2011-09-20 03:50:06 +02:00
|
|
|
|
2010-11-22 02:24:46 +01:00
|
|
|
default:
|
|
|
|
|
fprintf(stderr, "vvp error: get %d not supported "
|
|
|
|
|
"by __vpiEnumTypespec\n", code);
|
|
|
|
|
assert(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-18 05:00:23 +01:00
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
vpiHandle __vpiEnumTypespec::vpi_iterate(int code)
|
|
|
|
|
{
|
2011-10-12 18:29:55 +02:00
|
|
|
if (code == vpiEnumConst) {
|
2010-11-18 05:00:23 +01:00
|
|
|
vpiHandle*args = (vpiHandle*)
|
2012-01-21 02:16:42 +01:00
|
|
|
calloc(names.size(), sizeof(vpiHandle*));
|
|
|
|
|
for (size_t idx = 0 ; idx < names.size() ; idx += 1)
|
|
|
|
|
args[idx] = &names[idx];
|
2010-11-18 05:00:23 +01:00
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
return vpip_make_iterator(names.size(), args, true);
|
2010-11-18 05:00:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
inline enumconst_s::enumconst_s()
|
2012-01-20 23:15:26 +01:00
|
|
|
{ }
|
2012-01-19 19:16:39 +01:00
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
int enumconst_s::get_type_code(void) const
|
|
|
|
|
{ return vpiEnumConst; }
|
2012-01-20 20:39:48 +01:00
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
int enumconst_s::vpi_get(int code)
|
2010-11-22 02:24:46 +01:00
|
|
|
{
|
|
|
|
|
switch (code) {
|
|
|
|
|
case vpiSize:
|
2012-01-21 02:16:42 +01:00
|
|
|
return val4.size()? val4.size() : val2.size();
|
2010-11-22 02:24:46 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-18 05:00:23 +01:00
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
char* enumconst_s::vpi_get_str(int code)
|
|
|
|
|
{
|
2010-11-18 05:00:23 +01:00
|
|
|
switch (code) {
|
|
|
|
|
case vpiName:
|
2012-01-21 02:16:42 +01:00
|
|
|
return const_cast<char*> (name);
|
2010-11-18 05:00:23 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-01-21 02:16:42 +01:00
|
|
|
void enumconst_s::vpi_get_value(p_vpi_value val)
|
|
|
|
|
{
|
|
|
|
|
if (val4.size() > 0)
|
|
|
|
|
vpip_vec4_get_value(val4, val4.size(), false, val);
|
2010-11-22 02:24:46 +01:00
|
|
|
else
|
2012-01-21 02:16:42 +01:00
|
|
|
vpip_vec2_get_value(val2, val2.size(), false, val);
|
2010-11-18 05:00:23 +01:00
|
|
|
}
|
|
|
|
|
|
2012-01-20 20:39:48 +01:00
|
|
|
|
2011-09-20 03:50:06 +02:00
|
|
|
void compile_enum2_type(char*label, long width, bool signed_flag,
|
|
|
|
|
std::list<struct enum_name_s>*names)
|
2010-11-22 02:24:46 +01:00
|
|
|
{
|
|
|
|
|
struct __vpiEnumTypespec*spec = new struct __vpiEnumTypespec;
|
|
|
|
|
spec->names = std::vector<enumconst_s> (names->size());
|
2011-09-20 03:50:06 +02:00
|
|
|
spec->is_signed = signed_flag;
|
2011-10-12 03:27:45 +02:00
|
|
|
spec->base_type_code = vpiBitVar;
|
2010-11-22 02:24:46 +01:00
|
|
|
|
|
|
|
|
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].name = cur->text;
|
|
|
|
|
spec->names[idx].val2 = vvp_vector2_t(cur->val2, width);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(idx == spec->names.size());
|
2012-01-19 19:16:39 +01:00
|
|
|
compile_vpi_symbol(label, spec);
|
|
|
|
|
vpip_attach_to_current_scope(spec);
|
2010-11-22 02:24:46 +01:00
|
|
|
|
|
|
|
|
free(label);
|
|
|
|
|
delete names;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 03:50:06 +02:00
|
|
|
void compile_enum4_type(char*label, long width, bool signed_flag,
|
|
|
|
|
std::list<struct enum_name_s>*names)
|
2010-11-18 05:00:23 +01:00
|
|
|
{
|
|
|
|
|
struct __vpiEnumTypespec*spec = new struct __vpiEnumTypespec;
|
|
|
|
|
spec->names = std::vector<enumconst_s> (names->size());
|
2011-09-20 03:50:06 +02:00
|
|
|
spec->is_signed = signed_flag;
|
2011-10-12 03:27:45 +02:00
|
|
|
spec->base_type_code = vpiReg;
|
2010-11-18 05:00:23 +01:00
|
|
|
|
|
|
|
|
size_t idx = 0;
|
|
|
|
|
for (list<struct enum_name_s>::iterator cur = names->begin()
|
|
|
|
|
; cur != names->end() ; ++cur, ++idx) {
|
|
|
|
|
spec->names[idx].name = cur->text;
|
2010-11-22 02:24:46 +01:00
|
|
|
assert(cur->val4);
|
|
|
|
|
spec->names[idx].val4 = vector4_from_text(cur->val4, width);
|
|
|
|
|
free(cur->val4);
|
|
|
|
|
cur->val4 = 0;
|
2010-11-18 05:00:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(idx == spec->names.size());
|
2012-01-19 19:16:39 +01:00
|
|
|
compile_vpi_symbol(label, spec);
|
|
|
|
|
vpip_attach_to_current_scope(spec);
|
2010-11-18 05:00:23 +01:00
|
|
|
|
|
|
|
|
free(label);
|
|
|
|
|
delete names;
|
|
|
|
|
}
|
2011-10-10 07:20:40 +02:00
|
|
|
|
|
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
|
|
|
void enum_delete(vpiHandle item)
|
|
|
|
|
{
|
2012-01-19 19:16:39 +01:00
|
|
|
struct __vpiEnumTypespec*obj = dynamic_cast<__vpiEnumTypespec*>(item);
|
2011-10-10 07:20:40 +02:00
|
|
|
|
|
|
|
|
for (vector<enumconst_s>::iterator iter = obj->names.begin();
|
|
|
|
|
iter != obj->names.end(); ++ iter ) {
|
|
|
|
|
delete [] iter->name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete obj;
|
|
|
|
|
}
|
|
|
|
|
#endif
|