verilog: add sv_maps iterators
[ghdl-vlg.git] / ghw / libghw.c
blob116f1df1a4fc3eed172f456c4f7c4ee5e6fd2091
1 /* GHDL Wavefile reader library.
2 Copyright (C) 2005 Tristan Gingold
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <gnu.org/licenses>.
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include "libghw.h"
26 /* Reopen H through decompressor DECOMP. */
28 static int
29 ghw_openz (struct ghw_handler *h, const char *decomp, const char *filename)
31 int plen = strlen (decomp) + 1 + strlen (filename) + 1;
32 char *p = malloc (plen);
34 snprintf (p, plen, "%s %s", decomp, filename);
35 fclose (h->stream);
36 h->stream = popen (p, "r");
37 free (p);
39 if (h->stream == NULL)
40 return -1;
42 h->stream_ispipe = 1;
44 return 0;
47 int
48 ghw_open (struct ghw_handler *h, const char *filename)
50 char hdr[16];
52 h->stream = fopen (filename, "rb");
53 if (h->stream == NULL)
54 return -1;
56 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
57 return -1;
59 /* Check compression layer. */
60 if (!memcmp (hdr, "\x1f\x8b", 2))
62 if (ghw_openz (h, "gzip -cd", filename) < 0)
63 return -1;
64 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
65 return -1;
67 else if (!memcmp (hdr, "BZ", 2))
69 if (ghw_openz (h, "bzip2 -cd", filename) < 0)
70 return -1;
71 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
72 return -1;
74 else
76 h->stream_ispipe = 0;
79 /* Check magic. */
80 if (memcmp (hdr, "GHDLwave\n", 9) != 0)
81 return -2;
82 /* Check version. */
83 if (hdr[9] != 16 || hdr[10] != 0)
84 return -2;
85 h->version = hdr[11];
86 if (h->version > 1)
87 return -3;
88 if (hdr[12] == 1)
89 h->word_be = 0;
90 else if (hdr[12] == 2)
91 h->word_be = 1;
92 else
93 return -4;
94 #if 0
95 /* Endianness. */
97 int endian;
98 union
100 unsigned char b[4];
101 uint32_t i;
102 } v;
103 v.i = 0x11223344;
104 if (v.b[0] == 0x11)
105 endian = 2;
106 else if (v.b[0] == 0x44)
107 endian = 1;
108 else
109 return -3;
111 if (hdr[12] != 1 && hdr[12] != 2)
112 return -3;
113 if (hdr[12] != endian)
114 h->swap_word = 1;
115 else
116 h->swap_word = 0;
118 #endif
119 h->word_len = hdr[13];
120 h->off_len = hdr[14];
122 if (hdr[15] != 0)
123 return -5;
125 h->hie = NULL;
126 return 0;
129 int32_t
130 ghw_get_i32 (struct ghw_handler *h, unsigned char *b)
132 if (h->word_be)
133 return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3] << 0);
134 else
135 return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0] << 0);
138 int64_t
139 ghw_get_i64 (struct ghw_handler *ghw_h, unsigned char *b)
141 int l, h;
143 if (ghw_h->word_be)
145 h = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3] << 0);
146 l = (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7] << 0);
148 else
150 l = (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0] << 0);
151 h = (b[7] << 24) | (b[6] << 16) | (b[5] << 8) | (b[4] << 0);
153 return (((int64_t) h) << 32) | l;
157 ghw_read_byte (struct ghw_handler *h, unsigned char *res)
159 int v;
161 v = fgetc (h->stream);
162 if (v == EOF)
163 return -1;
164 *res = v;
165 return 0;
169 ghw_read_uleb128 (struct ghw_handler *h, uint32_t * res)
171 uint32_t r = 0;
172 unsigned int off = 0;
174 while (1)
176 int v = fgetc (h->stream);
177 if (v == EOF)
178 return -1;
179 r |= (v & 0x7f) << off;
180 if ((v & 0x80) == 0)
181 break;
182 off += 7;
184 *res = r;
185 return 0;
189 ghw_read_sleb128 (struct ghw_handler *h, int32_t * res)
191 int32_t r = 0;
192 unsigned int off = 0;
194 while (1)
196 int v = fgetc (h->stream);
197 if (v == EOF)
198 return -1;
199 r |= ((int32_t) (v & 0x7f)) << off;
200 off += 7;
201 if ((v & 0x80) == 0)
203 if ((v & 0x40) && off < 32)
204 r |= ~0U << off;
205 break;
208 *res = r;
209 return 0;
213 ghw_read_lsleb128 (struct ghw_handler *h, int64_t * res)
215 static const int64_t r_mask = -1;
216 int64_t r = 0;
217 unsigned int off = 0;
219 while (1)
221 int v = fgetc (h->stream);
222 if (v == EOF)
223 return -1;
224 r |= ((int64_t) (v & 0x7f)) << off;
225 off += 7;
226 if ((v & 0x80) == 0)
228 if ((v & 0x40) && off < 64)
229 r |= r_mask << off;
230 break;
233 *res = r;
234 return 0;
238 ghw_read_f64 (struct ghw_handler *h, double *res)
240 /* FIXME: handle byte order. */
241 if (fread (res, sizeof (*res), 1, h->stream) != 1)
242 return -1;
243 return 0;
246 const char *
247 ghw_read_strid (struct ghw_handler *h)
249 uint32_t id;
251 if (ghw_read_uleb128 (h, &id) != 0)
252 return NULL;
253 return h->str_table[id];
256 union ghw_type *
257 ghw_read_typeid (struct ghw_handler *h)
259 uint32_t id;
261 if (ghw_read_uleb128 (h, &id) != 0)
262 return NULL;
263 return h->types[id - 1];
266 union ghw_range *
267 ghw_read_range (struct ghw_handler *h)
269 int t = fgetc (h->stream);
270 if (t == EOF)
271 return NULL;
272 switch (t & 0x7f)
274 case ghdl_rtik_type_b2:
276 struct ghw_range_b2 *r;
277 r = malloc (sizeof (struct ghw_range_b2));
278 r->kind = t & 0x7f;
279 r->dir = (t & 0x80) != 0;
280 if (ghw_read_byte (h, &r->left) != 0)
281 goto err_b2;
282 if (ghw_read_byte (h, &r->right) != 0)
283 goto err_b2;
284 return (union ghw_range *) r;
285 err_b2:
286 free (r);
287 return NULL;
289 case ghdl_rtik_type_e8:
291 struct ghw_range_e8 *r;
292 r = malloc (sizeof (struct ghw_range_e8));
293 r->kind = t & 0x7f;
294 r->dir = (t & 0x80) != 0;
295 if (ghw_read_byte (h, &r->left) != 0)
296 goto err_e8;
297 if (ghw_read_byte (h, &r->right) != 0)
298 goto err_e8;
299 return (union ghw_range *) r;
300 err_e8:
301 free (r);
302 return NULL;
304 case ghdl_rtik_type_i32:
305 case ghdl_rtik_type_p32:
307 struct ghw_range_i32 *r;
308 r = malloc (sizeof (struct ghw_range_i32));
309 r->kind = t & 0x7f;
310 r->dir = (t & 0x80) != 0;
311 if (ghw_read_sleb128 (h, &r->left) != 0)
312 goto err_i32;
313 if (ghw_read_sleb128 (h, &r->right) != 0)
314 goto err_i32;
315 return (union ghw_range *) r;
316 err_i32:
317 free (r);
318 return NULL;
320 case ghdl_rtik_type_i64:
321 case ghdl_rtik_type_p64:
323 struct ghw_range_i64 *r;
324 r = malloc (sizeof (struct ghw_range_i64));
325 r->kind = t & 0x7f;
326 r->dir = (t & 0x80) != 0;
327 if (ghw_read_lsleb128 (h, &r->left) != 0)
328 goto err_i64;
329 if (ghw_read_lsleb128 (h, &r->right) != 0)
330 goto err_i64;
331 return (union ghw_range *) r;
332 err_i64:
333 free (r);
334 return NULL;
336 case ghdl_rtik_type_f64:
338 struct ghw_range_f64 *r;
339 r = malloc (sizeof (struct ghw_range_f64));
340 r->kind = t & 0x7f;
341 r->dir = (t & 0x80) != 0;
342 if (ghw_read_f64 (h, &r->left) != 0)
343 goto err_f64;
344 if (ghw_read_f64 (h, &r->right) != 0)
345 goto err_f64;
346 return (union ghw_range *) r;
347 err_f64:
348 free (r);
349 return NULL;
351 default:
352 fprintf (stderr, "ghw_read_range: type %d unhandled\n", t & 0x7f);
353 return NULL;
358 ghw_read_str (struct ghw_handler *h)
360 unsigned char hdr[12];
361 unsigned i;
362 char *p;
363 int prev_len;
365 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
366 return -1;
368 if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
369 return -1;
370 h->nbr_str = ghw_get_i32 (h, &hdr[4]);
371 h->nbr_str++;
372 h->str_size = ghw_get_i32 (h, &hdr[8]);
373 h->str_table = (char **) malloc ((h->nbr_str + 1) * sizeof (char *));
374 h->str_content = (char *) malloc (h->str_size + h->nbr_str + 1);
376 if (h->flag_verbose)
378 printf ("Number of strings: %u\n", h->nbr_str - 1);
379 printf ("String table size: %u\n", h->str_size);
382 h->str_table[0] = "<anon>";
383 p = h->str_content;
384 prev_len = 0;
385 for (i = 1; i < h->nbr_str; i++)
387 int j;
388 int c;
389 char *prev;
390 int sh;
392 h->str_table[i] = p;
393 prev = h->str_table[i - 1];
394 for (j = 0; j < prev_len; j++)
395 *p++ = prev[j];
397 while (1)
399 c = fgetc (h->stream);
400 if (c == EOF)
401 return -1;
402 if ((c >= 0 && c <= 31) || (c >= 128 && c <= 159))
403 break;
404 *p++ = c;
406 *p++ = 0;
408 if (h->flag_verbose > 1)
409 printf (" string %u (pl=%d): %s\n", i, prev_len, h->str_table[i]);
411 prev_len = c & 0x1f;
412 sh = 5;
413 while (c >= 128)
415 c = fgetc (h->stream);
416 if (c == EOF)
417 return -1;
418 prev_len |= (c & 0x1f) << sh;
419 sh += 5;
422 if (fread (hdr, 4, 1, h->stream) != 1)
423 return -1;
424 if (memcmp (hdr, "EOS", 4) != 0)
425 return -1;
426 return 0;
429 union ghw_type *
430 ghw_get_base_type (union ghw_type *t)
432 switch (t->kind)
434 case ghdl_rtik_type_b2:
435 case ghdl_rtik_type_e8:
436 case ghdl_rtik_type_e32:
437 case ghdl_rtik_type_i32:
438 case ghdl_rtik_type_i64:
439 case ghdl_rtik_type_f64:
440 case ghdl_rtik_type_p32:
441 case ghdl_rtik_type_p64:
442 case ghdl_rtik_type_array:
443 return t;
444 case ghdl_rtik_subtype_scalar:
445 return t->ss.base;
446 case ghdl_rtik_subtype_array:
447 return t->sa.base;
448 case ghdl_rtik_subtype_unbounded_array:
449 return t->sua.base;
450 default:
451 fprintf (stderr, "ghw_get_base_type: cannot handle type %d\n", t->kind);
452 abort ();
456 /* Return -1 for unbounded types. */
457 static int
458 get_nbr_elements (union ghw_type *t)
460 switch (t->kind)
462 case ghdl_rtik_type_b2:
463 case ghdl_rtik_type_e8:
464 case ghdl_rtik_type_e32:
465 case ghdl_rtik_type_i32:
466 case ghdl_rtik_type_i64:
467 case ghdl_rtik_type_f64:
468 case ghdl_rtik_type_p32:
469 case ghdl_rtik_type_p64:
470 case ghdl_rtik_subtype_scalar:
471 return 1;
472 case ghdl_rtik_type_array:
473 return -1;
474 case ghdl_rtik_subtype_array:
475 return t->sa.nbr_scalars;
476 case ghdl_rtik_type_record:
477 return t->rec.nbr_scalars;
478 case ghdl_rtik_subtype_record:
479 return t->sr.nbr_scalars;
480 case ghdl_rtik_subtype_unbounded_record:
481 case ghdl_rtik_subtype_unbounded_array:
482 return -1;
483 default:
484 fprintf (stderr, "get_nbr_elements: unhandled type %d\n", t->kind);
485 abort ();
490 ghw_get_range_length (union ghw_range *rng)
492 int res;
494 assert (rng != NULL);
496 switch (rng->kind)
498 case ghdl_rtik_type_i32:
499 if (rng->i32.dir)
500 res = rng->i32.left - rng->i32.right + 1;
501 else
502 res = rng->i32.right - rng->i32.left + 1;
503 break;
504 case ghdl_rtik_type_b2:
505 if (rng->b2.dir)
506 res = rng->b2.left - rng->b2.right + 1;
507 else
508 res = rng->b2.right - rng->b2.left + 1;
509 break;
510 case ghdl_rtik_type_e8:
511 if (rng->e8.dir)
512 res = rng->e8.left - rng->e8.right + 1;
513 else
514 res = rng->e8.right - rng->e8.left + 1;
515 break;
516 default:
517 fprintf (stderr, "get_range_length: unhandled kind %d\n", rng->kind);
518 abort ();
520 /* The length of a null range is 0. */
521 return (res <= 0) ? 0 : res;
524 static union ghw_type *ghw_read_type_bounds (struct ghw_handler *h,
525 union ghw_type *base);
527 /* Create an array subtype using BASE and ranges read from H. */
529 struct ghw_subtype_array *
530 ghw_read_array_subtype (struct ghw_handler *h, union ghw_type *base)
532 struct ghw_type_array *arr =
533 (struct ghw_type_array *) ghw_get_base_type (base);
534 struct ghw_subtype_array *sa;
535 unsigned j;
536 int nbr_scalars;
537 int nbr_els;
539 sa = malloc (sizeof (struct ghw_subtype_array));
540 sa->kind = ghdl_rtik_subtype_array;
541 sa->name = NULL;
542 sa->base = base;
543 nbr_els = get_nbr_elements (arr->el);
544 nbr_scalars = 1;
545 sa->rngs = malloc (arr->nbr_dim * sizeof (union ghw_range *));
546 for (j = 0; j < arr->nbr_dim; j++)
548 sa->rngs[j] = ghw_read_range (h);
549 nbr_scalars *= ghw_get_range_length (sa->rngs[j]);
551 if (nbr_els >= 0)
553 /* Element type is bounded. */
554 sa->el = arr->el;
556 else
558 /* Read bounds for the elements. */
559 sa->el = ghw_read_type_bounds (h, arr->el);
560 nbr_els = get_nbr_elements (sa->el);
562 sa->nbr_scalars = nbr_scalars * nbr_els;
563 return sa;
566 struct ghw_subtype_record *
567 ghw_read_record_subtype (struct ghw_handler *h, struct ghw_type_record *base)
569 struct ghw_subtype_record *sr;
571 sr = malloc (sizeof (struct ghw_subtype_record));
572 sr->kind = ghdl_rtik_subtype_record;
573 sr->name = NULL;
574 sr->base = base;
575 if (base->nbr_scalars >= 0)
577 /* Record base type is bounded. */
578 sr->nbr_scalars = base->nbr_scalars;
579 sr->els = base->els;
581 else
583 /* Read subtypes. */
584 unsigned j;
585 int nbr_scalars;
587 sr->els =
588 malloc (base->nbr_fields * sizeof (struct ghw_record_element));
589 nbr_scalars = 0;
590 for (j = 0; j < base->nbr_fields; j++)
592 union ghw_type *btype = base->els[j].type;
593 int el_nbr_scalars = get_nbr_elements (btype);
595 sr->els[j].name = base->els[j].name;
596 if (el_nbr_scalars >= 0)
598 /* Element is constrained. */
599 sr->els[j].type = btype;
601 else
603 sr->els[j].type = ghw_read_type_bounds (h, btype);
604 el_nbr_scalars = get_nbr_elements (sr->els[j].type);
606 nbr_scalars += el_nbr_scalars;
608 sr->nbr_scalars = nbr_scalars;
610 return sr;
613 /* Read bounds for BASE and create a subtype. */
615 static union ghw_type *
616 ghw_read_type_bounds (struct ghw_handler *h, union ghw_type *base)
618 switch (base->kind)
620 case ghdl_rtik_type_array:
621 case ghdl_rtik_subtype_unbounded_array:
622 return (union ghw_type *) ghw_read_array_subtype (h, base);
623 break;
624 case ghdl_rtik_type_record:
625 case ghdl_rtik_subtype_unbounded_record:
626 return (union ghw_type *) ghw_read_record_subtype (h, &base->rec);
627 break;
628 default:
629 fprintf (stderr, "ghw_read_type_bounds: unhandled kind %d\n",
630 base->kind);
631 return NULL;
636 ghw_read_type (struct ghw_handler *h)
638 unsigned char hdr[8];
639 unsigned i;
641 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
642 return -1;
644 if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
645 return -1;
646 h->nbr_types = ghw_get_i32 (h, &hdr[4]);
647 h->types =
648 (union ghw_type **) malloc (h->nbr_types * sizeof (union ghw_type *));
650 for (i = 0; i < h->nbr_types; i++)
652 int t;
654 t = fgetc (h->stream);
655 if (t == EOF)
656 return -1;
657 if (h->flag_verbose > 1)
658 printf ("type[%u]= %d\n", i, t);
659 switch (t)
661 case ghdl_rtik_type_b2:
662 case ghdl_rtik_type_e8:
664 struct ghw_type_enum *e;
665 unsigned j;
667 e = malloc (sizeof (struct ghw_type_enum));
668 e->kind = t;
669 e->wkt = ghw_wkt_unknown;
670 e->name = ghw_read_strid (h);
671 if (ghw_read_uleb128 (h, &e->nbr) != 0)
672 goto err_b2;
673 e->lits = (const char **) malloc (e->nbr * sizeof (char *));
674 if (h->flag_verbose > 1)
675 printf ("enum %s:", e->name);
676 for (j = 0; j < e->nbr; j++)
678 e->lits[j] = ghw_read_strid (h);
679 if (h->flag_verbose > 1)
680 printf (" %s", e->lits[j]);
682 if (h->flag_verbose > 1)
683 printf ("\n");
684 h->types[i] = (union ghw_type *) e;
685 break;
686 err_b2:
687 free (e->lits);
688 free (e);
689 return -1;
691 break;
692 case ghdl_rtik_type_i32:
693 case ghdl_rtik_type_i64:
694 case ghdl_rtik_type_f64:
696 struct ghw_type_scalar *sc;
698 sc = malloc (sizeof (struct ghw_type_scalar));
699 sc->kind = t;
700 sc->name = ghw_read_strid (h);
701 if (h->flag_verbose > 1)
702 printf ("scalar: %s\n", sc->name);
703 h->types[i] = (union ghw_type *) sc;
704 } break;
705 case ghdl_rtik_type_p32:
706 case ghdl_rtik_type_p64:
708 struct ghw_type_physical *ph;
710 ph = malloc (sizeof (struct ghw_type_physical));
711 ph->kind = t;
712 ph->name = ghw_read_strid (h);
713 ph->units = NULL;
714 if (h->version == 0)
715 ph->nbr_units = 0;
716 else
718 unsigned j;
720 if (ghw_read_uleb128 (h, &ph->nbr_units) != 0)
721 goto err_p32;
722 ph->units = malloc (ph->nbr_units * sizeof (struct ghw_unit));
723 for (j = 0; j < ph->nbr_units; j++)
725 ph->units[j].name = ghw_read_strid (h);
726 if (ghw_read_lsleb128 (h, &ph->units[j].val) < 0)
727 goto err_p32;
730 if (h->flag_verbose > 1)
731 printf ("physical: %s\n", ph->name);
732 h->types[i] = (union ghw_type *) ph;
733 break;
734 err_p32:
735 free (ph->units);
736 free (ph);
737 return -1;
739 break;
740 case ghdl_rtik_subtype_scalar:
742 struct ghw_subtype_scalar *ss;
744 ss = malloc (sizeof (struct ghw_subtype_scalar));
745 ss->kind = t;
746 ss->name = ghw_read_strid (h);
747 ss->base = ghw_read_typeid (h);
748 ss->rng = ghw_read_range (h);
749 if (h->flag_verbose > 1)
750 printf ("subtype scalar: %s\n", ss->name);
751 h->types[i] = (union ghw_type *) ss;
752 } break;
753 case ghdl_rtik_type_array:
755 struct ghw_type_array *arr;
756 unsigned j;
758 arr = malloc (sizeof (struct ghw_type_array));
759 arr->kind = t;
760 arr->name = ghw_read_strid (h);
761 arr->el = ghw_read_typeid (h);
762 if (ghw_read_uleb128 (h, &arr->nbr_dim) != 0)
763 goto err_array;
764 arr->dims =
765 (union ghw_type **) malloc (arr->nbr_dim *
766 sizeof (union ghw_type *));
767 for (j = 0; j < arr->nbr_dim; j++)
768 arr->dims[j] = ghw_read_typeid (h);
769 if (h->flag_verbose > 1)
770 printf ("array: %s (ndim=%u) of %s\n", arr->name, arr->nbr_dim,
771 arr->el->common.name);
772 h->types[i] = (union ghw_type *) arr;
773 break;
774 err_array:
775 free (arr->dims);
776 free (arr);
777 return -1;
779 break;
780 case ghdl_rtik_subtype_array:
782 struct ghw_subtype_array *sa;
783 const char *name;
784 union ghw_type *base;
786 name = ghw_read_strid (h);
787 base = ghw_read_typeid (h);
789 sa = ghw_read_array_subtype (h, base);
790 sa->name = name;
791 h->types[i] = (union ghw_type *) sa;
792 if (h->flag_verbose > 1)
793 printf ("subtype array: %s (nbr_scalars=%d)\n", sa->name,
794 sa->nbr_scalars);
796 break;
797 case ghdl_rtik_subtype_unbounded_array:
799 struct ghw_subtype_unbounded_array *sua;
801 sua = malloc (sizeof (struct ghw_subtype_unbounded_array));
802 sua->kind = t;
803 sua->name = ghw_read_strid (h);
804 sua->base = ghw_read_typeid (h);
805 h->types[i] = (union ghw_type *) sua;
806 if (h->flag_verbose > 1)
807 printf ("subtype unbounded array: %s\n", sua->name);
809 break;
810 case ghdl_rtik_type_record:
812 struct ghw_type_record *rec;
813 unsigned j;
814 int nbr_scalars;
816 rec = malloc (sizeof (struct ghw_type_record));
817 rec->kind = t;
818 rec->name = ghw_read_strid (h);
819 rec->els = NULL;
820 if (ghw_read_uleb128 (h, &rec->nbr_fields) != 0)
821 goto err_record;
822 rec->els =
823 malloc (rec->nbr_fields * sizeof (struct ghw_record_element));
824 nbr_scalars = 0;
825 for (j = 0; j < rec->nbr_fields; j++)
827 rec->els[j].name = ghw_read_strid (h);
828 rec->els[j].type = ghw_read_typeid (h);
829 if (nbr_scalars != -1)
831 int field_nbr_scalars =
832 get_nbr_elements (rec->els[j].type);
833 if (field_nbr_scalars == -1)
834 nbr_scalars = -1;
835 else
836 nbr_scalars += field_nbr_scalars;
839 rec->nbr_scalars = nbr_scalars;
840 if (h->flag_verbose > 1)
841 printf ("record type: %s (nbr_scalars=%d)\n", rec->name,
842 rec->nbr_scalars);
843 h->types[i] = (union ghw_type *) rec;
844 break;
845 err_record:
846 free (rec->els);
847 free (rec);
848 return -1;
850 break;
851 case ghdl_rtik_subtype_record:
853 struct ghw_subtype_record *sr;
854 const char *name;
855 struct ghw_type_record *base;
857 name = ghw_read_strid (h);
858 base = (struct ghw_type_record *) ghw_read_typeid (h);
860 sr = ghw_read_record_subtype (h, base);
861 sr->name = name;
862 h->types[i] = (union ghw_type *) sr;
863 if (h->flag_verbose > 1)
864 printf ("subtype record: %s (nbr_scalars=%d)\n", sr->name,
865 sr->nbr_scalars);
867 break;
868 case ghdl_rtik_subtype_unbounded_record:
870 struct ghw_subtype_unbounded_record *sur;
872 sur = malloc (sizeof (struct ghw_subtype_unbounded_record));
873 sur->kind = t;
874 sur->name = ghw_read_strid (h);
875 sur->base = (struct ghw_type_record *) ghw_read_typeid (h);
876 h->types[i] = (union ghw_type *) sur;
877 if (h->flag_verbose > 1)
878 printf ("subtype unbounded record: %s\n", sur->name);
880 break;
881 default:
882 fprintf (stderr, "ghw_read_type: unknown type %d\n", t);
883 return -1;
886 if (fgetc (h->stream) != 0)
887 return -1;
888 return 0;
892 ghw_read_wk_types (struct ghw_handler *h)
894 char hdr[4];
896 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
897 return -1;
899 if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
900 return -1;
902 while (1)
904 int t;
905 union ghw_type *tid;
907 t = fgetc (h->stream);
908 if (t == EOF)
909 return -1;
910 else if (t == 0)
911 break;
913 tid = ghw_read_typeid (h);
914 if (tid->kind == ghdl_rtik_type_b2 || tid->kind == ghdl_rtik_type_e8)
916 if (h->flag_verbose > 0)
917 printf ("%s: wkt=%d\n", tid->en.name, t);
918 tid->en.wkt = t;
921 return 0;
924 void
925 ghw_disp_typename (struct ghw_handler *h, union ghw_type *t)
927 (void) h;
928 printf ("%s", t->common.name);
931 /* Read a signal composed of severals elements.
932 Return 0 for success. */
934 ghw_read_signal (struct ghw_handler *h, unsigned int *sigs, union ghw_type *t)
936 switch (t->kind)
938 case ghdl_rtik_type_b2:
939 case ghdl_rtik_type_e8:
940 case ghdl_rtik_type_e32:
941 case ghdl_rtik_subtype_scalar:
943 unsigned int sig_el;
945 if (ghw_read_uleb128 (h, &sig_el) < 0)
946 return -1;
947 *sigs = sig_el;
948 if (sig_el == 0 || sig_el >= h->nbr_sigs)
949 return -1;
950 if (h->sigs[sig_el].type == NULL)
951 h->sigs[sig_el].type = ghw_get_base_type (t);
953 return 0;
954 case ghdl_rtik_subtype_array:
956 int i;
957 int stride;
958 int len;
960 len = t->sa.nbr_scalars;
961 stride = get_nbr_elements (t->sa.el);
963 for (i = 0; i < len; i += stride)
964 if (ghw_read_signal (h, &sigs[i], t->sa.el) < 0)
965 return -1;
967 return 0;
968 case ghdl_rtik_type_record:
970 struct ghw_type_record *r = &t->rec;
971 int nbr_fields = r->nbr_fields;
972 int i;
973 int off;
975 off = 0;
976 for (i = 0; i < nbr_fields; i++)
978 if (ghw_read_signal (h, &sigs[off], r->els[i].type) < 0)
979 return -1;
980 off += get_nbr_elements (r->els[i].type);
983 return 0;
984 case ghdl_rtik_subtype_record:
986 struct ghw_subtype_record *sr = &t->sr;
987 int nbr_fields = sr->base->nbr_fields;
988 int i;
989 int off;
991 off = 0;
992 for (i = 0; i < nbr_fields; i++)
994 if (ghw_read_signal (h, &sigs[off], sr->els[i].type) < 0)
995 return -1;
996 off += get_nbr_elements (sr->els[i].type);
999 return 0;
1000 default:
1001 fprintf (stderr, "ghw_read_signal: type kind %d unhandled\n", t->kind);
1002 abort ();
1007 ghw_read_value (struct ghw_handler *h,
1008 union ghw_val *val, union ghw_type *type)
1010 switch (ghw_get_base_type (type)->kind)
1012 case ghdl_rtik_type_b2:
1014 int v;
1015 v = fgetc (h->stream);
1016 if (v == EOF)
1017 return -1;
1018 val->b2 = v;
1020 break;
1021 case ghdl_rtik_type_e8:
1023 int v;
1024 v = fgetc (h->stream);
1025 if (v == EOF)
1026 return -1;
1027 val->e8 = v;
1029 break;
1030 case ghdl_rtik_type_i32:
1031 case ghdl_rtik_type_p32:
1033 int32_t v;
1034 if (ghw_read_sleb128 (h, &v) < 0)
1035 return -1;
1036 val->i32 = v;
1038 break;
1039 case ghdl_rtik_type_f64:
1041 double v;
1042 if (ghw_read_f64 (h, &v) < 0)
1043 return -1;
1044 val->f64 = v;
1046 break;
1047 case ghdl_rtik_type_p64:
1049 int64_t v;
1050 if (ghw_read_lsleb128 (h, &v) < 0)
1051 return -1;
1052 val->i64 = v;
1054 break;
1055 default:
1056 fprintf (stderr, "read_value: cannot handle format %d\n", type->kind);
1057 abort ();
1059 return 0;
1063 ghw_read_hie (struct ghw_handler *h)
1065 unsigned char hdr[16];
1066 int nbr_scopes;
1067 int nbr_sigs;
1068 unsigned i;
1069 struct ghw_hie *blk;
1070 struct ghw_hie **last;
1072 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1073 return -1;
1075 if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
1076 return -1;
1077 nbr_scopes = ghw_get_i32 (h, &hdr[4]);
1078 /* Number of declared signals (which may be composite). */
1079 nbr_sigs = ghw_get_i32 (h, &hdr[8]);
1080 /* Number of basic signals. */
1081 h->nbr_sigs = ghw_get_i32 (h, &hdr[12]);
1083 if (h->flag_verbose)
1084 printf ("%d scopes, %d signals, %u signal elements\n", nbr_scopes,
1085 nbr_sigs, h->nbr_sigs);
1087 blk = (struct ghw_hie *) malloc (sizeof (struct ghw_hie));
1088 blk->kind = ghw_hie_design;
1089 blk->name = NULL;
1090 blk->parent = NULL;
1091 blk->brother = NULL;
1092 blk->u.blk.child = NULL;
1094 last = &blk->u.blk.child;
1095 h->hie = blk;
1097 h->nbr_sigs++;
1098 h->skip_sigs = NULL;
1099 h->flag_full_names = 0;
1100 h->sigs = (struct ghw_sig *) malloc (h->nbr_sigs * sizeof (struct ghw_sig));
1101 memset (h->sigs, 0, h->nbr_sigs * sizeof (struct ghw_sig));
1103 while (1)
1105 int t;
1106 struct ghw_hie *el;
1107 unsigned int str;
1109 t = fgetc (h->stream);
1110 if (t == EOF)
1111 return -1;
1112 if (t == 0)
1113 break;
1115 if (t == ghw_hie_eos)
1117 blk = blk->parent;
1118 if (blk->u.blk.child == NULL)
1119 last = &blk->u.blk.child;
1120 else
1122 struct ghw_hie *l = blk->u.blk.child;
1123 while (l->brother != NULL)
1124 l = l->brother;
1125 last = &l->brother;
1128 continue;
1131 el = (struct ghw_hie *) malloc (sizeof (struct ghw_hie));
1132 el->kind = t;
1133 el->parent = blk;
1134 el->brother = NULL;
1136 /* Link. */
1137 *last = el;
1138 last = &el->brother;
1140 /* Read name. */
1141 if (ghw_read_uleb128 (h, &str) != 0)
1142 return -1;
1143 el->name = h->str_table[str];
1145 switch (t)
1147 case ghw_hie_eoh:
1148 case ghw_hie_design:
1149 case ghw_hie_eos:
1150 /* Should not be here. */
1151 abort ();
1152 case ghw_hie_process:
1153 el->u.blk.child = NULL;
1154 break;
1155 case ghw_hie_block:
1156 case ghw_hie_generate_if:
1157 case ghw_hie_generate_for:
1158 case ghw_hie_instance:
1159 case ghw_hie_generic:
1160 case ghw_hie_package:
1161 /* Create a block. */
1162 el->u.blk.child = NULL;
1164 if (t == ghw_hie_generate_for)
1166 el->u.blk.iter_type = ghw_read_typeid (h);
1167 el->u.blk.iter_value = malloc (sizeof (union ghw_val));
1168 if (ghw_read_value
1169 (h, el->u.blk.iter_value, el->u.blk.iter_type) < 0)
1170 return -1;
1172 blk = el;
1173 last = &el->u.blk.child;
1174 break;
1175 case ghw_hie_signal:
1176 case ghw_hie_port_in:
1177 case ghw_hie_port_out:
1178 case ghw_hie_port_inout:
1179 case ghw_hie_port_buffer:
1180 case ghw_hie_port_linkage:
1181 /* For a signal, read type. */
1183 int nbr_el;
1184 unsigned int *sigs;
1186 el->u.sig.type = ghw_read_typeid (h);
1187 nbr_el = get_nbr_elements (el->u.sig.type);
1188 if (nbr_el < 0)
1189 return -1;
1190 sigs =
1191 (unsigned int *) malloc ((nbr_el + 1) * sizeof (unsigned int));
1192 el->u.sig.sigs = sigs;
1193 /* Last element is NULL. */
1194 sigs[nbr_el] = 0;
1196 if (h->flag_verbose > 1)
1197 printf ("signal %s: %d el [", el->name, nbr_el);
1198 if (ghw_read_signal (h, sigs, el->u.sig.type) < 0)
1199 return -1;
1200 if (h->flag_verbose > 1)
1202 int j;
1203 for (j = 0; j < nbr_el; j++)
1204 printf (" #%u", sigs[j]);
1205 printf ("]\n");
1208 break;
1209 default:
1210 fprintf (stderr, "ghw_read_hie: unhandled kind %d\n", t);
1211 abort ();
1215 /* Allocate values. */
1216 for (i = 0; i < h->nbr_sigs; i++)
1217 if (h->sigs[i].type != NULL)
1218 h->sigs[i].val = (union ghw_val *) malloc (sizeof (union ghw_val));
1219 return 0;
1222 const char *
1223 ghw_get_hie_name (struct ghw_hie *h)
1225 switch (h->kind)
1227 case ghw_hie_eoh:
1228 return "eoh";
1229 case ghw_hie_design:
1230 return "design";
1231 case ghw_hie_block:
1232 return "block";
1233 case ghw_hie_generate_if:
1234 return "generate-if";
1235 case ghw_hie_generate_for:
1236 return "generate-for";
1237 case ghw_hie_instance:
1238 return "instance";
1239 case ghw_hie_package:
1240 return "package";
1241 case ghw_hie_process:
1242 return "process";
1243 case ghw_hie_generic:
1244 return "generic";
1245 case ghw_hie_eos:
1246 return "eos";
1247 case ghw_hie_signal:
1248 return "signal";
1249 case ghw_hie_port_in:
1250 return "port-in";
1251 case ghw_hie_port_out:
1252 return "port-out";
1253 case ghw_hie_port_inout:
1254 return "port-inout";
1255 case ghw_hie_port_buffer:
1256 return "port-buffer";
1257 case ghw_hie_port_linkage:
1258 return "port-linkage";
1259 default:
1260 return "??";
1264 void ghw_disp_value (union ghw_val *val, union ghw_type *type);
1266 static void
1267 print_name (struct ghw_hie *hie, int full_names)
1269 int i;
1270 int depth;
1271 struct ghw_hie *p;
1272 struct ghw_hie **buf;
1273 struct ghw_hie **end;
1275 /* HIE must be valid. */
1276 assert (hie->name != NULL);
1278 if (0 == full_names)
1280 printf (" %s: ", hie->name);
1281 return;
1284 p = hie;
1285 depth = 0;
1286 while (p && p->name)
1288 p = p->parent;
1289 ++depth;
1291 buf = (struct ghw_hie **) malloc (depth * sizeof (struct ghw_hie *));
1293 p = hie;
1294 end = depth + buf;
1295 while (p && p->name)
1297 *(--end) = p;
1298 p = p->parent;
1301 putchar (' ');
1302 putchar ('/');
1303 for (i = 0; i < depth; ++i)
1305 printf ("%s%s", i ? "/" : "", buf[i]->name);
1306 if (ghw_hie_generate_for == buf[i]->kind)
1308 putchar ('(');
1309 ghw_disp_value (buf[i]->u.blk.iter_value, buf[i]->u.blk.iter_type);
1310 putchar (')');
1313 putchar (':');
1314 putchar (' ');
1315 free (buf);
1318 void
1319 ghw_disp_hie (struct ghw_handler *h, struct ghw_hie *top)
1321 int i;
1322 int indent;
1323 struct ghw_hie *hie;
1324 struct ghw_hie *n;
1326 hie = top;
1327 indent = 0;
1329 while (1)
1331 if (0 == h->flag_full_names)
1332 for (i = 0; i < indent; i++)
1333 fputc (' ', stdout);
1334 printf ("%s", ghw_get_hie_name (hie));
1336 switch (hie->kind)
1338 case ghw_hie_design:
1339 case ghw_hie_block:
1340 case ghw_hie_generate_if:
1341 case ghw_hie_generate_for:
1342 case ghw_hie_instance:
1343 case ghw_hie_process:
1344 case ghw_hie_package:
1345 if (hie->name)
1346 print_name (hie, h->flag_full_names);
1347 if (hie->kind == ghw_hie_generate_for)
1349 printf ("(");
1350 ghw_disp_value (hie->u.blk.iter_value, hie->u.blk.iter_type);
1351 printf (")");
1353 n = hie->u.blk.child;
1354 if (n == NULL)
1355 n = hie->brother;
1356 else
1357 indent++;
1358 break;
1359 case ghw_hie_generic:
1360 case ghw_hie_eos:
1361 abort ();
1362 case ghw_hie_signal:
1363 case ghw_hie_port_in:
1364 case ghw_hie_port_out:
1365 case ghw_hie_port_inout:
1366 case ghw_hie_port_buffer:
1367 case ghw_hie_port_linkage:
1369 unsigned int *sigs = hie->u.sig.sigs;
1370 unsigned int k, num;
1372 print_name (hie, h->flag_full_names);
1373 ghw_disp_subtype_indication (h, hie->u.sig.type);
1374 printf (":");
1375 k = 0;
1376 /* There can be 0-length signals. */
1377 while (sigs[k] != GHW_NO_SIG)
1379 /* First signal of the range. */
1380 printf (" #%u", sigs[k]);
1381 for (num = 1; sigs[k + num] != GHW_NO_SIG; num++)
1382 if (sigs[k + num] != sigs[k + num - 1] + 1)
1383 break;
1384 if (num > 1)
1385 printf ("-#%u", sigs[k + num - 1]);
1386 k += num;
1388 n = hie->brother;
1390 break;
1391 default:
1392 abort ();
1394 printf ("\n");
1396 while (n == NULL)
1398 if (hie->parent == NULL)
1399 return;
1400 hie = hie->parent;
1401 indent--;
1402 n = hie->brother;
1404 hie = n;
1409 ghw_read_eoh (struct ghw_handler *h)
1411 (void) h;
1412 return 0;
1416 ghw_read_base (struct ghw_handler *h)
1418 unsigned char hdr[4];
1419 int res;
1421 while (1)
1423 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1424 return -1;
1425 if (memcmp (hdr, "STR", 4) == 0)
1426 res = ghw_read_str (h);
1427 else if (memcmp (hdr, "HIE", 4) == 0)
1428 res = ghw_read_hie (h);
1429 else if (memcmp (hdr, "TYP", 4) == 0)
1430 res = ghw_read_type (h);
1431 else if (memcmp (hdr, "WKT", 4) == 0)
1432 res = ghw_read_wk_types (h);
1433 else if (memcmp (hdr, "EOH", 4) == 0)
1434 return 0;
1435 else
1437 fprintf (stderr, "ghw_read_base: unknown GHW section %c%c%c%c\n",
1438 hdr[0], hdr[1], hdr[2], hdr[3]);
1439 return -1;
1441 if (res != 0)
1443 fprintf (stderr, "ghw_read_base: error in section %s\n", hdr);
1444 return res;
1450 ghw_read_signal_value (struct ghw_handler *h, struct ghw_sig *s)
1452 return ghw_read_value (h, s->val, s->type);
1456 ghw_read_snapshot (struct ghw_handler *h)
1458 unsigned char hdr[12];
1459 unsigned i;
1460 struct ghw_sig *s;
1462 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1463 return -1;
1465 if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
1466 return -1;
1467 h->snap_time = ghw_get_i64 (h, &hdr[4]);
1468 if (h->flag_verbose > 1)
1469 printf ("Time is " GHWPRI64 " fs\n", h->snap_time);
1471 for (i = 0; i < h->nbr_sigs; i++)
1473 s = &h->sigs[i];
1474 if (s->type != NULL)
1476 if (h->flag_verbose > 1)
1477 printf ("read type %d for sig %u\n", s->type->kind, i);
1478 if (ghw_read_signal_value (h, s) < 0)
1479 return -1;
1482 if (fread (hdr, 4, 1, h->stream) != 1)
1483 return -1;
1485 if (memcmp (hdr, "ESN", 4))
1486 return -1;
1488 return 0;
1491 void ghw_disp_values (struct ghw_handler *h);
1494 ghw_read_cycle_start (struct ghw_handler *h)
1496 unsigned char hdr[8];
1498 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1499 return -1;
1501 h->snap_time = ghw_get_i64 (h, hdr);
1502 return 0;
1506 ghw_read_cycle_cont (struct ghw_handler *h, int *list)
1508 int i;
1509 int *list_p;
1511 i = 0;
1512 list_p = list;
1513 while (1)
1515 uint32_t d;
1517 /* Read delta to next signal. */
1518 if (ghw_read_uleb128 (h, &d) < 0)
1519 return -1;
1520 if (d == 0)
1522 /* Last signal reached. */
1523 break;
1526 /* Find next signal. */
1527 while (d > 0)
1529 i++;
1530 if (h->sigs[i].type != NULL)
1531 d--;
1534 if (ghw_read_signal_value (h, &h->sigs[i]) < 0)
1535 return -1;
1536 if (list_p)
1537 *list_p++ = i;
1540 if (list_p)
1541 *list_p = 0;
1542 return 0;
1546 ghw_read_cycle_next (struct ghw_handler *h)
1548 int64_t d_time;
1550 if (ghw_read_lsleb128 (h, &d_time) < 0)
1551 return -1;
1552 if (d_time == -1)
1553 return 0;
1554 h->snap_time += d_time;
1555 return 1;
1559 ghw_read_cycle_end (struct ghw_handler *h)
1561 char hdr[4];
1563 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1564 return -1;
1565 if (memcmp (hdr, "ECY", 4))
1566 return -1;
1568 return 0;
1571 static const char *
1572 ghw_get_lit (union ghw_type *type, unsigned e)
1574 if (e >= type->en.nbr)
1575 return "??";
1576 else
1577 return type->en.lits[e];
1580 static void
1581 ghw_disp_lit (union ghw_type *type, unsigned e)
1583 printf ("%s (%u)", ghw_get_lit (type, e), e);
1586 void
1587 ghw_disp_value (union ghw_val *val, union ghw_type *type)
1589 switch (ghw_get_base_type (type)->kind)
1591 case ghdl_rtik_type_b2:
1592 ghw_disp_lit (type, val->b2);
1593 break;
1594 case ghdl_rtik_type_e8:
1595 ghw_disp_lit (type, val->e8);
1596 break;
1597 case ghdl_rtik_type_i32:
1598 printf (GHWPRI32, val->i32);
1599 break;
1600 case ghdl_rtik_type_p64:
1601 printf (GHWPRI64, val->i64);
1602 break;
1603 case ghdl_rtik_type_f64:
1604 printf ("%g", val->f64);
1605 break;
1606 default:
1607 fprintf (stderr, "ghw_disp_value: cannot handle type %d\n", type->kind);
1608 abort ();
1612 /* Put the ASCII representation of VAL into BUF, whose size if LEN.
1613 A NUL is always written to BUF.
1615 void
1616 ghw_get_value (char *buf, int len, union ghw_val *val, union ghw_type *type)
1618 union ghw_type *base = ghw_get_base_type (type);
1620 switch (base->kind)
1622 case ghdl_rtik_type_b2:
1623 if (val->b2 <= 1)
1625 strncpy (buf, base->en.lits[val->b2], len - 1);
1626 buf[len - 1] = 0;
1628 else
1630 snprintf (buf, len, "?%d", val->b2);
1632 break;
1633 case ghdl_rtik_type_e8:
1634 if (val->b2 <= base->en.nbr)
1636 strncpy (buf, base->en.lits[val->e8], len - 1);
1637 buf[len - 1] = 0;
1639 else
1641 snprintf (buf, len, "?%d", val->e8);
1643 break;
1644 case ghdl_rtik_type_i32:
1645 snprintf (buf, len, GHWPRI32, val->i32);
1646 break;
1647 case ghdl_rtik_type_p64:
1648 snprintf (buf, len, GHWPRI64, val->i64);
1649 break;
1650 case ghdl_rtik_type_f64:
1651 snprintf (buf, len, "%g", val->f64);
1652 break;
1653 default:
1654 snprintf (buf, len, "?bad type %d?", type->kind);
1658 static char
1659 is_skip_signal (int *signals_to_keep, int nb_signals_to_keep, int signal)
1661 int i;
1662 for (i = 0; i < nb_signals_to_keep; ++i)
1664 if (signal == signals_to_keep[i])
1666 return 0;
1669 return 1;
1672 void
1673 ghw_filter_signals (struct ghw_handler *h, int *signals_to_keep,
1674 int nb_signals_to_keep)
1676 unsigned i;
1678 if (0 < nb_signals_to_keep && 0 != signals_to_keep)
1680 if (0 == h->skip_sigs)
1682 h->skip_sigs = (char *) malloc (sizeof (char) * h->nbr_sigs);
1684 for (i = 0; i < h->nbr_sigs; ++i)
1686 h->skip_sigs[i] =
1687 is_skip_signal (signals_to_keep, nb_signals_to_keep, i);
1690 else
1692 if (0 != h->skip_sigs)
1694 free (h->skip_sigs);
1695 h->skip_sigs = 0;
1700 void
1701 ghw_disp_values (struct ghw_handler *h)
1703 unsigned i;
1704 for (i = 0; i < h->nbr_sigs; i++)
1706 struct ghw_sig *s = &h->sigs[i];
1707 int skip = (0 != h->skip_sigs && (0 != h->skip_sigs[i]));
1708 if (s->type != NULL && !skip)
1710 printf ("#%u: ", i);
1711 ghw_disp_value (s->val, s->type);
1712 printf ("\n");
1718 ghw_read_directory (struct ghw_handler *h)
1720 unsigned char hdr[8];
1721 int nbr_entries;
1722 int i;
1724 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1725 return -1;
1727 nbr_entries = ghw_get_i32 (h, &hdr[4]);
1729 if (h->flag_verbose)
1730 printf ("Directory (%d entries):\n", nbr_entries);
1732 for (i = 0; i < nbr_entries; i++)
1734 unsigned char ent[8];
1735 int pos;
1737 if (fread (ent, sizeof (ent), 1, h->stream) != 1)
1738 return -1;
1740 pos = ghw_get_i32 (h, &ent[4]);
1741 if (h->flag_verbose)
1742 printf (" %s at %d\n", ent, pos);
1745 if (fread (hdr, 4, 1, h->stream) != 1)
1746 return -1;
1747 if (memcmp (hdr, "EOD", 4))
1748 return -1;
1749 return 0;
1753 ghw_read_tailer (struct ghw_handler *h)
1755 unsigned char hdr[8];
1756 int pos;
1758 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1759 return -1;
1761 pos = ghw_get_i32 (h, &hdr[4]);
1763 if (h->flag_verbose)
1764 printf ("Tailer: directory at %d\n", pos);
1765 return 0;
1768 enum ghw_res
1769 ghw_read_sm_hdr (struct ghw_handler *h, int *list)
1771 unsigned char hdr[4];
1772 int res;
1774 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1776 if (feof (h->stream))
1777 return ghw_res_eof;
1778 else
1779 return ghw_res_error;
1781 if (memcmp (hdr, "SNP", 4) == 0)
1783 res = ghw_read_snapshot (h);
1784 if (res < 0)
1785 return res;
1786 return ghw_res_snapshot;
1788 else if (memcmp (hdr, "CYC", 4) == 0)
1790 res = ghw_read_cycle_start (h);
1791 if (res < 0)
1792 return res;
1793 res = ghw_read_cycle_cont (h, list);
1794 if (res < 0)
1795 return res;
1797 return ghw_res_cycle;
1799 else if (memcmp (hdr, "DIR", 4) == 0)
1801 res = ghw_read_directory (h);
1803 else if (memcmp (hdr, "TAI", 4) == 0)
1805 res = ghw_read_tailer (h);
1807 else
1809 fprintf (stderr, "unknown GHW section %c%c%c%c\n", hdr[0], hdr[1],
1810 hdr[2], hdr[3]);
1811 return -1;
1813 if (res != 0)
1814 return res;
1815 return ghw_res_other;
1819 ghw_read_sm (struct ghw_handler *h, enum ghw_sm_type *sm)
1821 int res;
1823 while (1)
1825 /* printf ("sm: state = %d\n", *sm); */
1826 switch (*sm)
1828 case ghw_sm_init:
1829 case ghw_sm_sect:
1830 res = ghw_read_sm_hdr (h, NULL);
1831 switch (res)
1833 case ghw_res_other:
1834 break;
1835 case ghw_res_snapshot:
1836 *sm = ghw_sm_sect;
1837 return res;
1838 case ghw_res_cycle:
1839 *sm = ghw_sm_cycle;
1840 return res;
1841 default:
1842 return res;
1844 break;
1845 case ghw_sm_cycle:
1846 if (0)
1847 printf ("Time is " GHWPRI64 " fs\n", h->snap_time);
1848 if (0)
1849 ghw_disp_values (h);
1851 res = ghw_read_cycle_next (h);
1852 if (res < 0)
1853 return res;
1854 if (res == 1)
1856 res = ghw_read_cycle_cont (h, NULL);
1857 if (res < 0)
1858 return res;
1859 return ghw_res_cycle;
1861 res = ghw_read_cycle_end (h);
1862 if (res < 0)
1863 return res;
1864 *sm = ghw_sm_sect;
1865 break;
1871 ghw_read_cycle (struct ghw_handler *h)
1873 int res;
1875 res = ghw_read_cycle_start (h);
1876 if (res < 0)
1877 return res;
1878 while (1)
1880 res = ghw_read_cycle_cont (h, NULL);
1881 if (res < 0)
1882 return res;
1884 if (0)
1885 printf ("Time is " GHWPRI64 " fs\n", h->snap_time);
1886 if (0)
1887 ghw_disp_values (h);
1889 res = ghw_read_cycle_next (h);
1890 if (res < 0)
1891 return res;
1892 if (res == 0)
1893 break;
1895 res = ghw_read_cycle_end (h);
1896 return res;
1900 ghw_read_dump (struct ghw_handler *h)
1902 unsigned char hdr[4];
1903 int res;
1905 while (1)
1907 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1909 if (feof (h->stream))
1910 return 0;
1911 else
1912 return -1;
1914 if (memcmp (hdr, "SNP", 4) == 0)
1916 res = ghw_read_snapshot (h);
1917 if (0 && res >= 0)
1918 ghw_disp_values (h);
1920 else if (memcmp (hdr, "CYC", 4) == 0)
1922 res = ghw_read_cycle (h);
1924 else if (memcmp (hdr, "DIR", 4) == 0)
1926 res = ghw_read_directory (h);
1928 else if (memcmp (hdr, "TAI", 4) == 0)
1930 res = ghw_read_tailer (h);
1932 else
1934 fprintf (stderr, "unknown GHW section %c%c%c%c\n", hdr[0], hdr[1],
1935 hdr[2], hdr[3]);
1936 return -1;
1938 if (res != 0)
1939 return res;
1943 struct ghw_section ghw_sections[] = { {"\0\0\0", NULL},
1944 {"STR", ghw_read_str},
1945 {"HIE", ghw_read_hie},
1946 {"TYP", ghw_read_type},
1947 {"WKT", ghw_read_wk_types},
1948 {"EOH", ghw_read_eoh},
1949 {"SNP", ghw_read_snapshot},
1950 {"CYC", ghw_read_cycle},
1951 {"DIR", ghw_read_directory},
1952 {"TAI", ghw_read_tailer}
1956 ghw_read_section (struct ghw_handler *h)
1958 unsigned char hdr[4];
1959 unsigned i;
1961 if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1963 if (feof (h->stream))
1964 return -2;
1965 else
1966 return -1;
1969 for (i = 1; i < sizeof (ghw_sections) / sizeof (*ghw_sections); i++)
1970 if (memcmp (hdr, ghw_sections[i].name, 4) == 0)
1971 return i;
1973 fprintf (stderr, "ghw_read_section: unknown GHW section %c%c%c%c\n", hdr[0],
1974 hdr[1], hdr[2], hdr[3]);
1975 return 0;
1978 void
1979 ghw_close (struct ghw_handler *h)
1981 if (h->stream)
1983 if (h->stream_ispipe)
1984 pclose (h->stream);
1985 else
1986 fclose (h->stream);
1988 h->stream = NULL;
1992 const char *
1993 ghw_get_dir (int is_downto)
1995 return is_downto ? "downto" : "to";
1998 void
1999 ghw_disp_range (union ghw_type *type, union ghw_range *rng)
2001 switch (rng->kind)
2003 case ghdl_rtik_type_b2:
2004 printf ("%s %s %s", ghw_get_lit (type, rng->b2.left),
2005 ghw_get_dir (rng->b2.dir), ghw_get_lit (type, rng->b2.right));
2006 break;
2007 case ghdl_rtik_type_e8:
2008 printf ("%s %s %s", ghw_get_lit (type, rng->e8.left),
2009 ghw_get_dir (rng->e8.dir), ghw_get_lit (type, rng->e8.right));
2010 break;
2011 case ghdl_rtik_type_i32:
2012 case ghdl_rtik_type_p32:
2013 printf (GHWPRI32 " %s " GHWPRI32, rng->i32.left,
2014 ghw_get_dir (rng->i32.dir), rng->i32.right);
2015 break;
2016 case ghdl_rtik_type_i64:
2017 case ghdl_rtik_type_p64:
2018 printf (GHWPRI64 " %s " GHWPRI64, rng->i64.left,
2019 ghw_get_dir (rng->i64.dir), rng->i64.right);
2020 break;
2021 case ghdl_rtik_type_f64:
2022 printf ("%g %s %g", rng->f64.left, ghw_get_dir (rng->f64.dir),
2023 rng->f64.right);
2024 break;
2025 default:
2026 printf ("?(%d)", rng->kind);
2030 static void
2031 ghw_disp_array_subtype_bounds (struct ghw_subtype_array *a)
2033 unsigned i;
2034 struct ghw_type_array *base =
2035 (struct ghw_type_array *) ghw_get_base_type (a->base);
2037 printf (" (");
2038 for (i = 0; i < base->nbr_dim; i++)
2040 if (i != 0)
2041 printf (", ");
2042 ghw_disp_range (base->dims[i], a->rngs[i]);
2044 printf (")");
2047 static void
2048 ghw_disp_record_subtype_bounds (struct ghw_subtype_record *sr)
2050 struct ghw_type_record *base = sr->base;
2051 int is_first = 1;
2052 unsigned i;
2054 for (i = 0; i < base->nbr_fields; i++)
2056 if (sr->els[i].type != base->els[i].type)
2058 if (is_first)
2060 printf ("(");
2061 is_first = 0;
2063 else
2064 printf (", ");
2065 printf ("%s", base->els[i].name);
2066 switch (sr->els[i].type->kind)
2068 case ghdl_rtik_subtype_array:
2069 ghw_disp_array_subtype_bounds (&sr->els[i].type->sa);
2070 break;
2071 case ghdl_rtik_subtype_record:
2072 ghw_disp_record_subtype_bounds (&sr->els[i].type->sr);
2073 break;
2074 default:
2075 printf ("??? (%d)", sr->els[i].type->kind);
2079 if (!is_first)
2080 printf (")");
2083 static void
2084 ghw_disp_subtype_definition (struct ghw_handler *h, union ghw_type *t)
2086 switch (t->kind)
2088 case ghdl_rtik_subtype_scalar:
2090 struct ghw_subtype_scalar *s = &t->ss;
2091 ghw_disp_typename (h, s->base);
2092 printf (" range ");
2093 ghw_disp_range (s->base, s->rng);
2094 } break;
2095 case ghdl_rtik_subtype_array:
2097 struct ghw_subtype_array *a = &t->sa;
2099 ghw_disp_typename (h, (union ghw_type *) a->base);
2100 ghw_disp_array_subtype_bounds (a);
2101 } break;
2102 case ghdl_rtik_subtype_record:
2104 struct ghw_subtype_record *sr = &t->sr;
2106 ghw_disp_typename (h, (union ghw_type *) sr->base);
2107 ghw_disp_record_subtype_bounds (sr);
2108 } break;
2109 case ghdl_rtik_subtype_unbounded_array:
2110 case ghdl_rtik_subtype_unbounded_record:
2112 struct ghw_subtype_unbounded_record *sur = &t->sur;
2114 ghw_disp_typename (h, (union ghw_type *) sur->base);
2115 } break;
2116 default:
2117 printf ("ghw_disp_subtype_definition: unhandled type kind %d\n",
2118 t->kind);
2122 static int
2123 ghw_is_anonymous_type (struct ghw_handler *h, union ghw_type *t)
2125 return t->common.name == h->str_table[0];
2128 void
2129 ghw_disp_subtype_indication (struct ghw_handler *h, union ghw_type *t)
2131 if (ghw_is_anonymous_type (h, t))
2133 /* Anonymous subtype. */
2134 ghw_disp_subtype_definition (h, t);
2136 else
2137 ghw_disp_typename (h, t);
2140 void
2141 ghw_disp_type (struct ghw_handler *h, union ghw_type *t)
2143 switch (t->kind)
2145 case ghdl_rtik_type_b2:
2146 case ghdl_rtik_type_e8:
2148 struct ghw_type_enum *e = &t->en;
2149 unsigned i;
2151 printf ("type %s is (", e->name);
2152 for (i = 0; i < e->nbr; i++)
2154 if (i != 0)
2155 printf (", ");
2156 printf ("%s", e->lits[i]);
2158 printf (");");
2159 if (e->wkt != ghw_wkt_unknown)
2160 printf (" -- WKT:%d", e->wkt);
2161 printf ("\n");
2163 break;
2164 case ghdl_rtik_type_i32:
2165 case ghdl_rtik_type_f64:
2167 struct ghw_type_scalar *s = &t->sc;
2168 printf ("type %s is range <>;\n", s->name);
2169 } break;
2170 case ghdl_rtik_type_p32:
2171 case ghdl_rtik_type_p64:
2173 unsigned i;
2175 struct ghw_type_physical *p = &t->ph;
2176 printf ("type %s is range <> units\n", p->name);
2177 for (i = 0; i < p->nbr_units; i++)
2179 struct ghw_unit *u = &p->units[i];
2180 printf (" %s = " GHWPRI64 " %s;\n", u->name, u->val,
2181 p->units[0].name);
2183 printf ("end units;\n");
2184 } break;
2185 case ghdl_rtik_type_array:
2187 struct ghw_type_array *a = &t->ar;
2188 unsigned i;
2190 printf ("type %s is array (", a->name);
2191 for (i = 0; i < a->nbr_dim; i++)
2193 if (i != 0)
2194 printf (", ");
2195 ghw_disp_typename (h, a->dims[i]);
2196 printf (" range <>");
2198 printf (") of ");
2199 ghw_disp_subtype_indication (h, a->el);
2200 printf (";\n");
2202 break;
2203 case ghdl_rtik_type_record:
2205 struct ghw_type_record *r = &t->rec;
2206 unsigned i;
2208 printf ("type %s is record\n", r->name);
2209 for (i = 0; i < r->nbr_fields; i++)
2211 printf (" %s: ", r->els[i].name);
2212 ghw_disp_subtype_indication (h, r->els[i].type);
2213 printf (";\n");
2215 printf ("end record;\n");
2217 break;
2218 case ghdl_rtik_subtype_array:
2219 case ghdl_rtik_subtype_scalar:
2220 case ghdl_rtik_subtype_record:
2221 case ghdl_rtik_subtype_unbounded_array:
2222 case ghdl_rtik_subtype_unbounded_record:
2224 struct ghw_type_common *c = &t->common;
2225 printf ("subtype %s is ", c->name);
2226 ghw_disp_subtype_definition (h, t);
2227 printf (";\n");
2228 } break;
2229 default:
2230 printf ("ghw_disp_type: unhandled type kind %d\n", t->kind);
2234 void
2235 ghw_disp_types (struct ghw_handler *h)
2237 unsigned i;
2239 for (i = 0; i < h->nbr_types; i++)
2240 if (h->flag_verbose || !ghw_is_anonymous_type (h, h->types[i]))
2241 ghw_disp_type (h, h->types[i]);