Merge branch 'master' into verilog-ams
[sverilog.git] / vpi / vcd_priv.c
blob2eef7903f4d4fc9b04b02e0ab5ed79b5c7da3202
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;
191 /* This is used by the compiletf routines to check if an argument
192 * is numeric. */
193 static void check_numeric_arg(vpiHandle arg, char *msg, PLI_BYTE8 *name)
195 assert(arg);
197 switch (vpi_get(vpiType, arg)) {
198 case vpiConstant:
199 case vpiParameter:
200 /* String constants are invalid numeric values. */
201 if (vpi_get(vpiConstType, arg) == vpiStringConst) {
202 vpi_mcd_printf(1, msg, name);
203 vpi_control(vpiFinish, 1);
205 break;
207 /* These have valid numeric values. */
208 case vpiIntegerVar:
209 case vpiMemoryWord:
210 case vpiNet:
211 case vpiRealVar:
212 case vpiReg:
213 case vpiTimeVar:
214 break;
216 default:
217 /* Anything else is not a numeric value. */
218 vpi_mcd_printf(1, msg, name);
219 vpi_control(vpiFinish, 1);
220 break;
224 /* This is used by the compiletf routines to check if an argument
225 * is a string value. */
226 static void check_string_arg(vpiHandle arg, char *msg, PLI_BYTE8 *name)
228 assert(arg);
229 PLI_INT32 ctype = 0;
231 switch (vpi_get(vpiType, arg)) {
232 case vpiConstant:
233 case vpiParameter:
234 /* These must be a string or binary constant. */
235 ctype = vpi_get(vpiConstType, arg);
236 if (ctype != vpiStringConst && ctype != vpiBinaryConst) {
237 vpi_mcd_printf(1, msg, name);
238 vpi_control(vpiFinish, 1);
240 break;
242 /* These have valid string values. */
243 case vpiIntegerVar:
244 case vpiMemoryWord:
245 case vpiNet:
246 case vpiReg:
247 case vpiTimeVar:
248 break;
250 default:
251 /* Anything else is not a string. */
252 vpi_mcd_printf(1, msg, name);
253 vpi_control(vpiFinish, 1);
254 break;
259 * Since the compiletf routines are all the same they are located here,
260 * so we only need a single copy.
263 /* $dumpall does not take an argument. */
264 PLI_INT32 sys_dumpall_compiletf(PLI_BYTE8 *name)
266 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
267 vpiHandle argv = vpi_iterate(vpiArgument, callh);
269 if (argv != 0) {
270 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name);
271 vpi_control(vpiFinish, 1);
274 return 0;
277 /* $dumpfile takes a single string argument. */
278 PLI_INT32 sys_dumpfile_compiletf(PLI_BYTE8 *name)
280 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
281 vpiHandle argv = vpi_iterate(vpiArgument, callh);
283 /* Check that there is an argument and that it is a string. */
284 if (argv == 0) {
285 vpi_mcd_printf(1, "ERROR: %s requires an argument.\n", name);
286 vpi_control(vpiFinish, 1);
287 return 0;
289 check_string_arg(vpi_scan(argv), "ERROR: %s's argument must be a"
290 " string.\n", name);
292 /* Check that there is only a single argument. */
293 if (vpi_scan(argv) != 0) {
294 vpi_mcd_printf(1, "ERROR: %s takes a single argument.\n", name);
295 vpi_control(vpiFinish, 1);
296 return 0;
299 return 0;
302 /* $dumpflush does not take an argument. */
303 PLI_INT32 sys_dumpflush_compiletf(PLI_BYTE8 *name)
305 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
306 vpiHandle argv = vpi_iterate(vpiArgument, callh);
308 if (argv != 0) {
309 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name);
310 vpi_control(vpiFinish, 1);
313 return 0;
316 /* $dumplimit takes a single numeric argument. */
317 PLI_INT32 sys_dumplimit_compiletf(PLI_BYTE8 *name)
319 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
320 vpiHandle argv = vpi_iterate(vpiArgument, callh);
322 /* Check that there is an argument and that it is numeric. */
323 if (argv == 0) {
324 vpi_mcd_printf(1, "ERROR: %s requires an argument.\n", name);
325 vpi_control(vpiFinish, 1);
326 return 0;
328 check_numeric_arg(vpi_scan(argv), "ERROR: %s's argument must be"
329 " numeric.\n", name);
331 /* Check that there is only a single argument. */
332 if (vpi_scan(argv) != 0) {
333 vpi_mcd_printf(1, "ERROR: %s takes a single argument.\n", name);
334 vpi_control(vpiFinish, 1);
335 return 0;
338 return 0;
341 /* $dumpoff does not take an argument. */
342 PLI_INT32 sys_dumpoff_compiletf(PLI_BYTE8 *name)
344 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
345 vpiHandle argv = vpi_iterate(vpiArgument, callh);
347 if (argv != 0) {
348 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name);
349 vpi_control(vpiFinish, 1);
352 return 0;
355 /* $dumpon does not take an argument. */
356 PLI_INT32 sys_dumpon_compiletf(PLI_BYTE8 *name)
358 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
359 vpiHandle argv = vpi_iterate(vpiArgument, callh);
361 if (argv != 0) {
362 vpi_mcd_printf(1, "ERROR: %s does not take an argument.\n", name);
363 vpi_control(vpiFinish, 1);
366 return 0;
369 /* $dumpvars takes a variety of arguments. */
370 PLI_INT32 sys_dumpvars_compiletf(PLI_BYTE8 *name)
372 vpiHandle callh = vpi_handle(vpiSysTfCall, 0);
373 vpiHandle argv = vpi_iterate(vpiArgument, callh);
374 vpiHandle arg;
376 /* No arguments is OK, dump everything. */
377 if (argv == 0)
378 return 0;
380 /* The first argument is the numeric level. */
381 check_numeric_arg(vpi_scan(argv), "ERROR: %s's first argument must be"
382 " numeric.\n", name);
384 /* The rest of the arguments are either a module or a variable. */
385 while ((arg=vpi_scan(argv)) != NULL) {
386 switch(vpi_get(vpiType, arg)) {
387 /* The module types. */
388 case vpiModule:
389 case vpiTask:
390 case vpiFunction:
391 case vpiNamedBegin:
392 case vpiNamedFork:
393 /* The variable types. */
394 case vpiNet:
395 case vpiReg:
396 case vpiMemoryWord:
397 case vpiIntegerVar:
398 case vpiTimeVar:
399 case vpiRealVar:
400 break;
401 default:
402 vpi_mcd_printf(1, "ERROR: %s cannot dump a %s.\n",
403 name, vpi_get_str(vpiType, arg));
404 vpi_control(vpiFinish, 1);
408 return 0;