Fix for assertion error when expanding macro.
[iverilog.git] / vpi / vcd_priv.c
blob1f9f2311258fae9458a4ead76a1ab4de4d084aaf
1 /*
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)
8 * any later version.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: vcd_priv.c,v 1.6 2004/10/04 01:10:58 steve Exp $"
21 #endif
23 # include "vpi_config.h"
24 # include "vcd_priv.h"
25 # include <stdio.h>
26 # include <stdlib.h>
27 # include <string.h>
28 # include <assert.h>
29 #ifdef HAVE_MALLOC_H
30 # include <malloc.h>
31 #endif
32 # include <ctype.h>
33 # include "stringheap.h"
35 int is_escaped_id(const char *name)
37 int lp;
39 assert(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] == '_')) {
46 return 1;
49 /* We looked at all the digits, so this is a normal id. */
50 return 0;
52 return 1;
55 struct stringheap_s name_heap = {0, 0};
57 struct vcd_names_s {
58 const char *name;
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));
66 assert(nl);
67 nl->name = strdup_sh(&name_heap, name);
68 nl->next = tab->vcd_names_list;
69 tab->vcd_names_list = nl;
70 tab->listed_names ++;
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)
83 const char **v;
85 if (tab->vcd_names_sorted == 0)
86 return 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;
99 const char **l;
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;
113 while (r) {
114 struct vcd_names_s *rr = r;
115 r = rr->next;
116 *(l++) = rr->name;
117 free(rr);
120 qsort(tab->vcd_names_sorted, tab->sorted_names,
121 sizeof(const char **), vcd_names_compare);
127 Nexus Id cache
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
136 to smalle VCD files.
138 The _vpiNexusId is a private (int) property of IVL simulators.
141 struct vcd_id_s
143 const char *id;
144 struct vcd_id_s *next;
145 int nex;
148 static inline unsigned ihash(int nex)
150 unsigned a = nex;
151 a ^= a>>16;
152 a ^= a>>8;
153 return a & 0xff;
156 static struct vcd_id_s **vcd_ids = 0;
158 const char *find_nexus_ident(int nex)
160 struct vcd_id_s *bucket;
162 if (!vcd_ids) {
163 vcd_ids = (struct vcd_id_s **)
164 calloc(256, sizeof(struct vcd_id_s*));
165 assert(vcd_ids);
168 bucket = vcd_ids[ihash(nex)];
169 while (bucket) {
170 if (nex == bucket->nex)
171 return bucket->id;
172 bucket = bucket->next;
175 return 0;
178 void set_nexus_ident(int nex, const char *id)
180 struct vcd_id_s *bucket;
182 assert(vcd_ids);
184 bucket = (struct vcd_id_s *) malloc(sizeof(struct vcd_id_s));
185 bucket->next = vcd_ids[ihash(nex)];
186 bucket->id = id;
187 bucket->nex = nex;
188 vcd_ids[ihash(nex)] = bucket;