2 * Copyright (c) 2003 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: vcd_priv.c,v 1.6 2004/10/04 01:10:58 steve Exp $"
23 # include "vpi_config.h"
24 # include "vcd_priv.h"
33 # include "stringheap.h"
35 int is_escaped_id(const char *name
)
40 /* The first digit must be alpha or '_' to be a normal id. */
41 if (isalpha(name
[0]) || name
[0] == '_') {
42 for (lp
=1; name
[lp
] != '\0'; lp
++) {
43 /* If this digit is not alpha-numeric or '_' we have
44 * an escaped identifier. */
45 if (!(isalnum(name
[lp
]) || name
[lp
] == '_')) {
49 /* We looked at all the digits, so this is a normal id. */
55 struct stringheap_s name_heap
= {0, 0};
59 struct vcd_names_s
*next
;
62 void vcd_names_add(struct vcd_names_list_s
*tab
, const char *name
)
64 struct vcd_names_s
*nl
= (struct vcd_names_s
*)
65 malloc(sizeof(struct vcd_names_s
));
67 nl
->name
= strdup_sh(&name_heap
, name
);
68 nl
->next
= tab
->vcd_names_list
;
69 tab
->vcd_names_list
= nl
;
73 static int vcd_names_compare(const void *s1
, const void *s2
)
75 const char *v1
= *(const char **) s1
;
76 const char *v2
= *(const char **) s2
;
78 return strcmp(v1
, v2
);
81 const char *vcd_names_search(struct vcd_names_list_s
*tab
, const char *key
)
85 if (tab
->vcd_names_sorted
== 0)
88 v
= (const char **) bsearch(&key
,
89 tab
->vcd_names_sorted
, tab
->sorted_names
,
90 sizeof(const char *), vcd_names_compare
);
92 return(v
? *v
: NULL
);
95 void vcd_names_sort(struct vcd_names_list_s
*tab
)
97 if (tab
->listed_names
) {
98 struct vcd_names_s
*r
;
101 tab
->sorted_names
+= tab
->listed_names
;
102 tab
->vcd_names_sorted
= (const char **)
103 realloc(tab
->vcd_names_sorted
,
104 tab
->sorted_names
*(sizeof(const char *)));
105 assert(tab
->vcd_names_sorted
);
107 l
= tab
->vcd_names_sorted
+ tab
->sorted_names
- tab
->listed_names
;
108 tab
->listed_names
= 0;
110 r
= tab
->vcd_names_list
;
111 tab
->vcd_names_list
= 0x0;
114 struct vcd_names_s
*rr
= r
;
120 qsort(tab
->vcd_names_sorted
, tab
->sorted_names
,
121 sizeof(const char **), vcd_names_compare
);
129 In structural models, many signals refer to the same nexus.
130 Some structural models also have very many signals. This cache
131 saves nexus_id - vcd_id pairs, and reuses the vcd_id when a signal
132 refers to a nexus that is already dumped.
134 The new signal will be listed as a $var, but no callback
135 will be installed. This saves considerable CPU time and leads
138 The _vpiNexusId is a private (int) property of IVL simulators.
144 struct vcd_id_s
*next
;
148 static inline unsigned ihash(int nex
)
156 static struct vcd_id_s
**vcd_ids
= 0;
158 const char *find_nexus_ident(int nex
)
160 struct vcd_id_s
*bucket
;
163 vcd_ids
= (struct vcd_id_s
**)
164 calloc(256, sizeof(struct vcd_id_s
*));
168 bucket
= vcd_ids
[ihash(nex
)];
170 if (nex
== bucket
->nex
)
172 bucket
= bucket
->next
;
178 void set_nexus_ident(int nex
, const char *id
)
180 struct vcd_id_s
*bucket
;
184 bucket
= (struct vcd_id_s
*) malloc(sizeof(struct vcd_id_s
));
185 bucket
->next
= vcd_ids
[ihash(nex
)];
188 vcd_ids
[ihash(nex
)] = bucket
;