Skip to content

Commit

Permalink
Partial implementation of vhpiCaseNameP for declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jun 24, 2023
1 parent 4a1acac commit d48237b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ char *xstrdup(const char *str)
return copy;
}

char *xstrndup(const char *str, size_t n)
{
char *copy = strndup(str, n);
if (copy == NULL)
fatal("memory exhausted (strndup)");
return copy;
}

char *xvasprintf(const char *fmt, va_list ap)
{
char *strp = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ void *xrealloc_array(void *ptr, size_t nelems, size_t size) RETURNS_NONNULL;
void *xrealloc_flex(void *ptr, size_t fixed, size_t nelems, size_t size)
RETURNS_NONNULL;
char *xstrdup(const char *str) RETURNS_NONNULL;
char *xstrndup(const char *str, size_t n) RETURNS_NONNULL;

char *xvasprintf(const char *fmt, va_list ap) RETURNS_NONNULL;
char *xasprintf(const char *fmt, ...)
Expand Down
60 changes: 56 additions & 4 deletions src/vhpi/vhpi-model.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ static vhpiCharT *new_string(const char *s)
return p;
}

static vhpiCharT *new_string_n(const char *s, size_t n)
{
vhpiCharT *p = shash_get(strtab, s);
if (p == NULL)
shash_put(strtab, s, (p = (vhpiCharT *)xstrndup(s, n)));
return p;
}

static void init_abstractRegion(c_abstractRegion *r, tree_t t)
{
const loc_t *loc = tree_loc(t);
Expand Down Expand Up @@ -633,10 +641,10 @@ static void init_abstractDecl(c_abstractDecl *d, tree_t t, c_abstractRegion *r)
d->LineNo = loc->first_line;
d->LineOffset = loc->line_delta;

d->Name = d->CaseName = new_string(istr(tree_ident(t)));
d->Name = new_string(istr(tree_ident(t)));
if (r) {
char *full LOCAL = xasprintf("%s:%s", r->FullName, d->Name);
d->FullName = d->FullCaseName = new_string(full);
d->FullName = new_string(full);
}

d->ImmRegion = r;
Expand Down Expand Up @@ -1867,6 +1875,50 @@ vhpiIntT vhpi_get(vhpiIntPropertyT property, vhpiHandleT handle)
}
}

static vhpiCharT *cached_caseName(c_abstractDecl *decl)
{
if (decl->CaseName != NULL)
return decl->CaseName;

// We do not save the orignal case of identifiers as it does not seem
// to be required except for this property. Instead try to recover
// the original identifier text using the source location.

const loc_t *loc = tree_loc(decl->tree);
if (loc->line_delta != 0)
goto failed;

const char *src = loc_get_source(loc);
if (src == NULL)
goto failed;

// Do some basic sanity checks to make sure we found the right
// identifier

const char *start = src + loc->first_column;
if (decl->Name[0] != toupper_iso88591(*start))
goto failed;

return (decl->CaseName = new_string_n(start, loc->column_delta + 1));

failed:
return (decl->CaseName = decl->Name);
}

static vhpiCharT *cached_fullCaseName(c_abstractDecl *decl)
{
if (decl->FullCaseName != NULL)
return decl->FullCaseName;

vhpiCharT *cn = cached_caseName(decl);

if (decl->ImmRegion == NULL)
return (decl->FullCaseName = cn);

char *full LOCAL = xasprintf("%s:%s", decl->ImmRegion->FullCaseName, cn);
return (decl->FullCaseName = new_string(full));
}

DLLEXPORT
const vhpiCharT *vhpi_get_str(vhpiStrPropertyT property, vhpiHandleT handle)
{
Expand Down Expand Up @@ -1906,10 +1958,10 @@ const vhpiCharT *vhpi_get_str(vhpiStrPropertyT property, vhpiHandleT handle)
if (d != NULL) {
switch (property) {
case vhpiNameP: return d->Name;
case vhpiCaseNameP: return d->CaseName;
case vhpiCaseNameP: return cached_caseName(d);
case vhpiFileNameP: return (vhpiCharT *)loc_file_str(&(d->object.loc));
case vhpiFullNameP: return d->FullName;
case vhpiFullCaseNameP: return d->FullCaseName;
case vhpiFullCaseNameP: return cached_fullCaseName(d);
default: goto unsupported;
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/regress/gold/vhpi1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ VHPI printf v indexed name is V(3)
VHPI printf v indexed name is V(2)
VHPI printf v indexed name is V(1)
VHPI printf v indexed name is V(0)
VHPI printf name is A_NAME_WITH_MIXED_CASE
VHPI printf case name is A_name_with_MIXED_case
VHPI printf full case name is :VHPI1:A_name_with_MIXED_case
VHPI printf start of sim callback! user data is 'some user data'
0ms+0: Report Note: x=5
VHPI printf after_5ns callback!
Expand Down
1 change: 1 addition & 0 deletions test/regress/vhpi1.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ architecture test of vhpi1 is
signal v : bit_vector(3 downto 0) := "0011";
signal b : bit;
signal r : real;
signal A_name_with_MIXED_case : bit;
begin

p1: process (x) is
Expand Down
8 changes: 8 additions & 0 deletions test/vhpi/vhpi1.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,5 +483,13 @@ void vhpi1_startup(void)
for (vhpiHandleT name = vhpi_scan(v_names); name != NULL; name = vhpi_scan(v_names))
vhpi_printf("v indexed name is %s", vhpi_get_str(vhpiNameP, name));

vhpiHandleT handle_case = vhpi_handle_by_name("a_name_with_mixed_case", root);
check_error();
fail_if(handle_case == NULL);
vhpi_printf("handle %p", handle_case);
vhpi_printf("name is %s", vhpi_get_str(vhpiNameP, handle_case));
vhpi_printf("case name is %s", vhpi_get_str(vhpiCaseNameP, handle_case));
vhpi_printf("full case name is %s", vhpi_get_str(vhpiFullCaseNameP, handle_case));

vhpi_release_handle(root);
}

0 comments on commit d48237b

Please sign in to comment.