diff --git a/vvp/array.cc b/vvp/array.cc index 1405dfb98..b984b238a 100644 --- a/vvp/array.cc +++ b/vvp/array.cc @@ -145,26 +145,7 @@ static char*vpi_array_get_str(int code, vpiHandle ref) { struct __vpiArray*obj = ARRAY_HANDLE(ref); - char*bn; - char*rbuf; - size_t len; - - switch (code) { - case vpiFullName: - bn = strdup(vpi_get_str(vpiFullName, &obj->scope->base)); - len = strlen(bn)+strlen(obj->name)+2; - rbuf = need_result_buf(len, RBUF_STR); - snprintf(rbuf, len, "%s.%s", bn, obj->name); - free(bn); - return rbuf; - - case vpiName: - rbuf = need_result_buf(strlen(obj->name)+1, RBUF_STR); - strcpy(rbuf, obj->name); - return rbuf; - } - - return 0; + return generic_get_str(code, &obj->scope->base, obj->name, NULL); } static vpiHandle vpi_array_get_handle(int code, vpiHandle ref) diff --git a/vvp/vpi_const.cc b/vvp/vpi_const.cc index bbc40aa3a..df1a8d975 100644 --- a/vvp/vpi_const.cc +++ b/vvp/vpi_const.cc @@ -251,27 +251,10 @@ struct __vpiStringParam : public __vpiStringConst { static char* string_param_get_str(int code, vpiHandle obj) { struct __vpiStringParam*rfp = (struct __vpiStringParam*)obj; - char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); - char *rbuf = need_result_buf(strlen(bn)+strlen(rfp->basename) + 2, - RBUF_STR); assert(obj->vpi_type->type_code == vpiParameter); - switch (code) { - case vpiFullName: - sprintf(rbuf, "%s.%s", bn, rfp->basename); - free(bn); - return rbuf; - - case vpiName: - strcpy(rbuf, rfp->basename); - free(bn); - return rbuf; - - default: - free(bn); - return 0; - } + return generic_get_str(code, &rfp->scope->base, rfp->basename, NULL); } static vpiHandle string_param_handle(int code, vpiHandle obj) @@ -441,26 +424,10 @@ struct __vpiBinaryParam : public __vpiBinaryConst { static char* binary_param_get_str(int code, vpiHandle obj) { struct __vpiBinaryParam*rfp = (struct __vpiBinaryParam*)obj; - char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); - char *rbuf = need_result_buf(strlen(bn)+strlen(rfp->basename) + 2, - RBUF_STR); assert(obj->vpi_type->type_code == vpiParameter); - switch (code) { - case vpiFullName: - sprintf(rbuf, "%s.%s", bn, rfp->basename); - free(bn); - return rbuf; - case vpiName: - strcpy(rbuf, rfp->basename); - free(bn); - return rbuf; - - default: - free(bn); - return 0; - } + return generic_get_str(code, &rfp->scope->base, rfp->basename, NULL); } static vpiHandle binary_param_handle(int code, vpiHandle obj) diff --git a/vvp/vpi_event.cc b/vvp/vpi_event.cc index 1beaff433..bd9b43564 100644 --- a/vvp/vpi_event.cc +++ b/vvp/vpi_event.cc @@ -29,30 +29,13 @@ # include # include -static char* named_event_str(int code, vpiHandle ref) +static char* named_event_get_str(int code, vpiHandle ref) { assert((ref->vpi_type->type_code==vpiNamedEvent)); struct __vpiNamedEvent*obj = (struct __vpiNamedEvent*)ref; - char *bn = vpi_get_str(vpiFullName, &obj->scope->base); - const char *nm = obj->name; - - char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 1, RBUF_STR); - - switch (code) { - - case vpiFullName: - sprintf(rbuf, "%s.%s", bn, nm); - return rbuf; - - case vpiName: - strcpy(rbuf, nm); - return rbuf; - - } - - return 0; + return generic_get_str(code, &obj->scope->base, obj->name, NULL); } static vpiHandle named_event_get_handle(int code, vpiHandle ref) @@ -74,7 +57,7 @@ static const struct __vpirt vpip_named_event_rt = { vpiNamedEvent, 0, - named_event_str, + named_event_get_str, 0, 0, diff --git a/vvp/vpi_memory.cc b/vvp/vpi_memory.cc index c582c1618..5f5eb281d 100644 --- a/vvp/vpi_memory.cc +++ b/vvp/vpi_memory.cc @@ -103,23 +103,7 @@ static char* memory_get_str(int code, vpiHandle ref) struct __vpiMemory*rfp = (struct __vpiMemory*)ref; - char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); - - char *rbuf = need_result_buf(strlen(bn)+strlen(rfp->name)+2, RBUF_STR); - - switch (code) { - case vpiFullName: - sprintf(rbuf, "%s.%s", bn, rfp->name); - free(bn); - return rbuf; - case vpiName: - strcpy(rbuf, rfp->name); - free(bn); - return rbuf; - } - - free(bn); - return 0; + return generic_get_str(code, &rfp->scope->base, rfp->name, NULL); } static vpiHandle memory_scan(vpiHandle ref, int) @@ -340,27 +324,10 @@ static char* memory_word_get_str(int code, vpiHandle ref) struct __vpiMemoryWord*rfp = (struct __vpiMemoryWord*)ref; - char *bn = strdup(vpi_get_str(vpiFullName, &rfp->mem->scope->base)); - const char *nm = rfp->mem->name; + char number[32]; + sprintf(number, "%d", rfp->index.value); - char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 10 + 4, RBUF_STR); - - switch (code) { - case vpiFullName: - sprintf(rbuf, "%s.%s[%d]", bn, nm, rfp->index.value); - free(bn); - return rbuf; - break; - case vpiName: { - sprintf(rbuf, "%s[%d]", nm, rfp->index.value); - free(bn); - return rbuf; - break; - } - } - - free(bn); - return 0; + return generic_get_str(code, &rfp->mem->scope->base, rfp->mem->name, number); } static void memory_word_get_value(vpiHandle ref, s_vpi_value*vp) diff --git a/vvp/vpi_priv.h b/vvp/vpi_priv.h index 864ec243c..879c04a91 100644 --- a/vvp/vpi_priv.h +++ b/vvp/vpi_priv.h @@ -539,5 +539,8 @@ enum vpi_rbuf_t { /* Storage for *_get_str() */ }; extern char *need_result_buf(unsigned cnt, vpi_rbuf_t type); +/* following two routines use need_result_buf(, RBUF_STR) */ +extern char *simple_set_rbuf_str(const char *s1); +extern char *generic_get_str(int code, vpiHandle ref, const char *name, const char *index); #endif diff --git a/vvp/vpi_real.cc b/vvp/vpi_real.cc index 8a3938f77..0e9feabe5 100644 --- a/vvp/vpi_real.cc +++ b/vvp/vpi_real.cc @@ -48,39 +48,22 @@ static char* real_var_get_str(int code, vpiHandle ref) assert(ref->vpi_type->type_code == vpiRealVar); struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref; - char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); - char *nm; + + char *nm, *ixs; if (rfp->parent) { - s_vpi_value vp; nm = strdup(vpi_get_str(vpiName, rfp->parent)); - strcat(nm, "["); + s_vpi_value vp; vp.format = vpiDecStrVal; vpi_get_value(rfp->id.index, &vp); - strcat(nm, vp.value.str); - strcat(nm, "]"); + ixs = vp.value.str; /* do I need to strdup() this? */ } else { nm = strdup(rfp->id.name); - } - char *rbuf = need_result_buf(strlen(bn)+strlen(nm) + 2, RBUF_STR); - - switch (code) { - - case vpiFullName: - sprintf(rbuf, "%s.%s", bn, nm); - free(bn); - free(nm); - return rbuf; - - case vpiName: - strcpy(rbuf, nm); - free(bn); - free(nm); - return rbuf; + ixs = NULL; } - free(bn); + char *rbuf = generic_get_str(code, &rfp->scope->base, nm, ixs); free(nm); - return 0; + return rbuf; } static vpiHandle real_var_get_handle(int code, vpiHandle ref) diff --git a/vvp/vpi_scope.cc b/vvp/vpi_scope.cc index c0548fede..81d441053 100644 --- a/vvp/vpi_scope.cc +++ b/vvp/vpi_scope.cc @@ -109,40 +109,36 @@ static const char* scope_get_type(int code) static char* scope_get_str(int code, vpiHandle obj) { struct __vpiScope*ref = (struct __vpiScope*)obj; - char *rbuf; assert(handle_is_scope(obj)); + char buf[4096]; // XXX is a fixed buffer size really reliable? + const char *p=0; switch (code) { - case vpiFullName: { - char buf[4096]; - buf[0] = 0; - construct_scope_fullname(ref, buf); - rbuf = need_result_buf(strlen(buf) + 1, RBUF_STR); - strcpy(rbuf, buf); - return rbuf; - } + case vpiFullName: + buf[0] = 0; + construct_scope_fullname(ref, buf); + p = buf; + break; case vpiName: - rbuf = need_result_buf(strlen(ref->name) + 1, RBUF_STR); - strcpy(rbuf, ref->name); - return rbuf; + p = ref->name; + break; case vpiDefName: - rbuf = need_result_buf(strlen(ref->tname) + 1, RBUF_STR); - strcpy(rbuf, ref->tname); - return rbuf; + p = ref->tname; + break; case vpiType: - rbuf = need_result_buf(strlen(scope_get_type(code)) + 1, RBUF_STR); - strcpy(rbuf, scope_get_type(code)); - return rbuf; + p = scope_get_type(code); + break; default: fprintf(stderr, "ERROR: invalid code %d.", code); assert(0); return 0; } + return simple_set_rbuf_str(p); } static vpiHandle scope_get_handle(int code, vpiHandle obj) diff --git a/vvp/vpi_signal.cc b/vvp/vpi_signal.cc index aac8af24e..f3974f458 100644 --- a/vvp/vpi_signal.cc +++ b/vvp/vpi_signal.cc @@ -46,7 +46,7 @@ * just perform the lookup in a table. This only takes 256 bytes, * which is not many executable instructions:-) * - * The table is calculated as compile time, therefore, by the + * The table is calculated at compile time, therefore, by the * draw_tt.c program. */ extern const char hex_digits[256]; @@ -75,6 +75,42 @@ char *need_result_buf(unsigned cnt, vpi_rbuf_t type) return result_buf[type]; } +char *simple_set_rbuf_str(const char *s1) +{ + char *res = need_result_buf(strlen(s1)+1, RBUF_STR); + if (res) strcpy(res,s1); + return res; +} + +char *generic_get_str(int code, vpiHandle ref, const char *name, const char *index) +{ + size_t len = strlen(name) + 1; /* include space for null termination */ + char *bn = NULL; + if (code == vpiFullName) { + bn = strdup(vpi_get_str(code,ref)); + len += strlen(bn) + 1; /* include space for "." separator */ + } + if (index != NULL) len += strlen(index) + 2; /* include space for brackets */ + + char *res = need_result_buf(len, RBUF_STR); + if (!res) return NULL; + *res=0; /* start with nothing */ + + /* if this works, I can make it more efficient later */ + if (bn != NULL) { + strcat(res, bn); + strcat(res, "."); + free(bn); + } + strcat(res, name); + if (index != NULL) { + strcat(res, "["); + strcat(res, index); + strcat(res, "]"); + } + return res; +} + struct __vpiSignal* vpip_signal_from_handle(vpiHandle ref) { if ((ref->vpi_type->type_code != vpiNet) @@ -134,39 +170,21 @@ static char* signal_get_str(int code, vpiHandle ref) struct __vpiSignal*rfp = (struct __vpiSignal*)ref; - char *bn = strdup(vpi_get_str(vpiFullName, &rfp->scope->base)); - char *nm; + char *nm, *ixs; if (rfp->parent) { + nm = vpi_get_str(vpiName, rfp->parent); s_vpi_value vp; - nm = strdup(vpi_get_str(vpiName, rfp->parent)); - strcat(nm, "["); vp.format = vpiDecStrVal; vpi_get_value(rfp->id.index, &vp); - strcat(nm, vp.value.str); - strcat(nm, "]"); + ixs = vp.value.str; /* do I need to strdup() this? */ } else { nm = strdup(rfp->id.name); - } - char *rbuf = need_result_buf(strlen(bn) + strlen(nm) + 2, RBUF_STR); - - switch (code) { - - case vpiFullName: - sprintf(rbuf, "%s.%s", bn, nm); - free(bn); - free(nm); - return rbuf; - - case vpiName: - strcpy(rbuf, nm); - free(bn); - free(nm); - return rbuf; + ixs = NULL; } - free(bn); + char *rbuf = generic_get_str(code, &rfp->scope->base, nm, ixs); free(nm); - return 0; + return rbuf; } static vpiHandle signal_get_handle(int code, vpiHandle ref) diff --git a/vvp/vpi_tasks.cc b/vvp/vpi_tasks.cc index 643f61126..3c74aca49 100644 --- a/vvp/vpi_tasks.cc +++ b/vvp/vpi_tasks.cc @@ -111,14 +111,10 @@ static char *systask_get_str(int type, vpiHandle ref) assert((ref->vpi_type->type_code == vpiSysTaskCall) || (ref->vpi_type->type_code == vpiSysFuncCall)); - const char *bn = rfp->defn->info.tfname; - char *rbuf = need_result_buf(strlen(bn) + 1, RBUF_STR); - switch (type) { case vpiName: - strcpy(rbuf,bn); - return rbuf; + return simple_set_rbuf_str(rfp->defn->info.tfname); } return 0; diff --git a/vvp/vpi_time.cc b/vvp/vpi_time.cc index 9da7bb8e4..621d52e15 100644 --- a/vvp/vpi_time.cc +++ b/vvp/vpi_time.cc @@ -107,7 +107,7 @@ static char* timevar_time_get_str(int code, vpiHandle ref) { switch (code) { case vpiName: - return "$time"; + return simple_set_rbuf_str("$time"); default: fprintf(stderr, "Code: %d\n", code); assert(0); @@ -119,7 +119,7 @@ static char* timevar_simtime_get_str(int code, vpiHandle ref) { switch (code) { case vpiName: - return "$simtime"; + return simple_set_rbuf_str("$simtime"); default: fprintf(stderr, "Code: %d\n", code); assert(0); @@ -131,7 +131,7 @@ static char* timevar_realtime_get_str(int code, vpiHandle ref) { switch (code) { case vpiName: - return "$realtime"; + return simple_set_rbuf_str("$realtime"); default: fprintf(stderr, "Code: %d\n", code); assert(0); diff --git a/vvp/vpi_vthr_vector.cc b/vvp/vpi_vthr_vector.cc index 72f984678..e8af1d9a2 100644 --- a/vvp/vpi_vthr_vector.cc +++ b/vvp/vpi_vthr_vector.cc @@ -105,8 +105,8 @@ static char* vthr_vec_get_str(int code, vpiHandle ref) switch (code) { - case vpiFullName: - return (char*)rfp->name; + case vpiFullName: /* should this be vpiName? */ + return simple_set_rbuf_str(rfp->name); } return 0;