1 /*  GHDL Wavefile reader library.
2     Copyright (C) 2005 Tristan Gingold
3 
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.
8 
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.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <gnu.org/licenses>.
16 */
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <assert.h>
23 
24 #include "ghwlib.h"
25 
26 /* Reopen H through decompressor DECOMP.  */
27 
28 static int
ghw_openz(struct ghw_handler * h,const char * decomp,const char * filename)29 ghw_openz (struct ghw_handler *h, const char *decomp, const char *filename)
30 {
31   int plen = strlen (decomp) + 1 + strlen(filename) + 1;
32   char *p = malloc (plen);
33 
34   snprintf (p, plen, "%s %s", decomp, filename);
35   fclose (h->stream);
36   h->stream = popen(p, "r");
37   free (p);
38 
39   if (h->stream == NULL)
40     return -1;
41 
42   h->stream_ispipe = 1;
43 
44   return 0;
45 }
46 
47 int
ghw_open(struct ghw_handler * h,const char * filename)48 ghw_open (struct ghw_handler *h, const char *filename)
49 {
50   char hdr[16];
51 
52   h->stream = fopen (filename, "rb");
53   if (h->stream == NULL)
54     return -1;
55 
56   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
57     return -1;
58 
59   /* Check compression layer.  */
60   if (!memcmp (hdr, "\x1f\x8b", 2))
61     {
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;
66     }
67   else if (!memcmp (hdr, "BZ", 2))
68     {
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;
73     }
74   else
75     {
76       h->stream_ispipe = 0;
77     }
78 
79   /* Check magic.  */
80   if (memcmp (hdr, "GHDLwave\n", 9) != 0)
81     return -2;
82   /* Check version.  */
83   if (hdr[9] != 16
84       || hdr[10] != 0)
85     return -2;
86   h->version = hdr[11];
87   if (h->version > 1)
88     return -3;
89   if (hdr[12] == 1)
90     h->word_be = 0;
91   else if (hdr[12] == 2)
92     h->word_be = 1;
93   else
94     return -4;
95 #if 0
96   /* Endianness.  */
97   {
98     int endian;
99     union { unsigned char b[4]; uint32_t i;} v;
100     v.i = 0x11223344;
101     if (v.b[0] == 0x11)
102       endian = 2;
103     else if (v.b[0] == 0x44)
104       endian = 1;
105     else
106       return -3;
107 
108     if (hdr[12] != 1 && hdr[12] != 2)
109       return -3;
110     if (hdr[12] != endian)
111       h->swap_word = 1;
112     else
113       h->swap_word = 0;
114   }
115 #endif
116   h->word_len = hdr[13];
117   h->off_len = hdr[14];
118 
119   if (hdr[15] != 0)
120     return -5;
121 
122   h->hie = NULL;
123   return 0;
124 }
125 
126 int32_t
ghw_get_i32(struct ghw_handler * h,unsigned char * b)127 ghw_get_i32 (struct ghw_handler *h, unsigned char *b)
128 {
129   if (h->word_be)
130     return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3] << 0);
131   else
132     return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0] << 0);
133 }
134 
135 int64_t
ghw_get_i64(struct ghw_handler * ghw_h,unsigned char * b)136 ghw_get_i64 (struct ghw_handler *ghw_h, unsigned char *b)
137 {
138   int l, h;
139 
140   if (ghw_h->word_be)
141     {
142       h = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3] << 0);
143       l = (b[4] << 24) | (b[5] << 16) | (b[6] << 8) | (b[7] << 0);
144     }
145   else
146     {
147       l = (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0] << 0);
148       h = (b[7] << 24) | (b[6] << 16) | (b[5] << 8) | (b[4] << 0);
149     }
150   return (((int64_t)h) << 32) | l;
151 }
152 
153 int
ghw_read_byte(struct ghw_handler * h,unsigned char * res)154 ghw_read_byte (struct ghw_handler *h, unsigned char *res)
155 {
156   int v;
157 
158   v = fgetc (h->stream);
159   if (v == EOF)
160     return -1;
161   *res = v;
162   return 0;
163 }
164 
165 int
ghw_read_uleb128(struct ghw_handler * h,uint32_t * res)166 ghw_read_uleb128 (struct ghw_handler *h, uint32_t *res)
167 {
168   uint32_t r = 0;
169   unsigned int off = 0;
170 
171   while (1)
172     {
173       int v = fgetc (h->stream);
174       if (v == EOF)
175 	return -1;
176       r |= (v & 0x7f) << off;
177       if ((v & 0x80) == 0)
178 	break;
179       off += 7;
180     }
181   *res = r;
182   return 0;
183 }
184 
185 int
ghw_read_sleb128(struct ghw_handler * h,int32_t * res)186 ghw_read_sleb128 (struct ghw_handler *h, int32_t *res)
187 {
188   int32_t r = 0;
189   unsigned int off = 0;
190 
191   while (1)
192     {
193       int v = fgetc (h->stream);
194       if (v == EOF)
195 	return -1;
196       r |= ((int32_t)(v & 0x7f)) << off;
197       off += 7;
198       if ((v & 0x80) == 0)
199 	{
200 	  if ((v & 0x40) && off < 32)
201 	    r |= ~0U << off;
202 	  break;
203 	}
204     }
205   *res = r;
206   return 0;
207 }
208 
209 int
ghw_read_lsleb128(struct ghw_handler * h,int64_t * res)210 ghw_read_lsleb128 (struct ghw_handler *h, int64_t *res)
211 {
212   static const int64_t r_mask = -1;
213   int64_t r = 0;
214   unsigned int off = 0;
215 
216   while (1)
217     {
218       int v = fgetc (h->stream);
219       if (v == EOF)
220 	return -1;
221       r |= ((int64_t)(v & 0x7f)) << off;
222       off += 7;
223       if ((v & 0x80) == 0)
224 	{
225 	  if ((v & 0x40) && off < 64)
226 	    r |= r_mask << off;
227 	  break;
228 	}
229     }
230   *res = r;
231   return 0;
232 }
233 
234 int
ghw_read_f64(struct ghw_handler * h,double * res)235 ghw_read_f64 (struct ghw_handler *h, double *res)
236 {
237   /* FIXME: handle byte order.  */
238   if (fread (res, sizeof (*res), 1, h->stream) != 1)
239     return -1;
240   return 0;
241 }
242 
243 const char *
ghw_read_strid(struct ghw_handler * h)244 ghw_read_strid (struct ghw_handler *h)
245 {
246   uint32_t id;
247 
248   if (ghw_read_uleb128 (h, &id) != 0)
249     return NULL;
250   return h->str_table[id];
251 }
252 
253 union ghw_type *
ghw_read_typeid(struct ghw_handler * h)254 ghw_read_typeid (struct ghw_handler *h)
255 {
256   uint32_t id;
257 
258   if (ghw_read_uleb128 (h, &id) != 0)
259     return NULL;
260   return h->types[id - 1];
261 }
262 
263 union ghw_range *
ghw_read_range(struct ghw_handler * h)264 ghw_read_range (struct ghw_handler *h)
265 {
266   int t = fgetc (h->stream);
267   if (t == EOF)
268     return NULL;
269   switch (t & 0x7f)
270     {
271     case ghdl_rtik_type_b2:
272       {
273 	struct ghw_range_b2 *r;
274 	r = malloc (sizeof (struct ghw_range_b2));
275 	r->kind = t & 0x7f;
276 	r->dir = (t & 0x80) != 0;
277 	if (ghw_read_byte (h, &r->left) != 0)
278 	  goto err_b2;
279 	if (ghw_read_byte (h, &r->right) != 0)
280 	  goto err_b2;
281 	return (union ghw_range *)r;
282       err_b2:
283 	free (r);
284 	return NULL;
285       }
286     case ghdl_rtik_type_e8:
287       {
288 	struct ghw_range_e8 *r;
289 	r = malloc (sizeof (struct ghw_range_e8));
290 	r->kind = t & 0x7f;
291 	r->dir = (t & 0x80) != 0;
292 	if (ghw_read_byte (h, &r->left) != 0)
293 	  goto err_e8;
294 	if (ghw_read_byte (h, &r->right) != 0)
295 	  goto err_e8;
296 	return (union ghw_range *)r;
297       err_e8:
298 	free (r);
299 	return NULL;
300       }
301     case ghdl_rtik_type_i32:
302     case ghdl_rtik_type_p32:
303       {
304 	struct ghw_range_i32 *r;
305 	r = malloc (sizeof (struct ghw_range_i32));
306 	r->kind = t & 0x7f;
307 	r->dir = (t & 0x80) != 0;
308 	if (ghw_read_sleb128 (h, &r->left) != 0)
309 	  goto err_i32;
310 	if (ghw_read_sleb128 (h, &r->right) != 0)
311 	  goto err_i32;
312 	return (union ghw_range *)r;
313       err_i32:
314 	free (r);
315 	return NULL;
316       }
317     case ghdl_rtik_type_i64:
318     case ghdl_rtik_type_p64:
319       {
320 	struct ghw_range_i64 *r;
321 	r = malloc (sizeof (struct ghw_range_i64));
322 	r->kind = t & 0x7f;
323 	r->dir = (t & 0x80) != 0;
324 	if (ghw_read_lsleb128 (h, &r->left) != 0)
325 	  goto err_i64;
326 	if (ghw_read_lsleb128 (h, &r->right) != 0)
327 	  goto err_i64;
328 	return (union ghw_range *)r;
329       err_i64:
330 	free (r);
331 	return NULL;
332       }
333     case ghdl_rtik_type_f64:
334       {
335 	struct ghw_range_f64 *r;
336 	r = malloc (sizeof (struct ghw_range_f64));
337 	r->kind = t & 0x7f;
338 	r->dir = (t & 0x80) != 0;
339 	if (ghw_read_f64 (h, &r->left) != 0)
340 	  goto err_f64;
341 	if (ghw_read_f64 (h, &r->right) != 0)
342 	  goto err_f64;
343 	return (union ghw_range *)r;
344       err_f64:
345 	free (r);
346 	return NULL;
347       }
348     default:
349       fprintf (stderr, "ghw_read_range: type %d unhandled\n", t & 0x7f);
350       return NULL;
351     }
352 }
353 
354 int
ghw_read_str(struct ghw_handler * h)355 ghw_read_str (struct ghw_handler *h)
356 {
357   unsigned char hdr[12];
358   unsigned i;
359   char *p;
360   int prev_len;
361 
362   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
363     return -1;
364 
365   if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
366     return -1;
367   h->nbr_str = ghw_get_i32 (h, &hdr[4]);
368   h->nbr_str++;
369   h->str_size = ghw_get_i32 (h, &hdr[8]);
370   h->str_table = (char **)malloc ((h->nbr_str + 1) * sizeof (char *));
371   h->str_content = (char *)malloc (h->str_size + h->nbr_str + 1);
372 
373   if (h->flag_verbose)
374     {
375       printf ("Number of strings: %u\n", h->nbr_str - 1);
376       printf ("String table size: %u\n", h->str_size);
377     }
378 
379   h->str_table[0] = "<anon>";
380   p = h->str_content;
381   prev_len = 0;
382   for (i = 1; i < h->nbr_str; i++)
383     {
384       int j;
385       int c;
386       char *prev;
387       int sh;
388 
389       h->str_table[i] = p;
390       prev = h->str_table[i - 1];
391       for (j = 0; j < prev_len; j++)
392 	*p++ = prev[j];
393 
394       while (1)
395 	{
396 	  c = fgetc (h->stream);
397 	  if (c == EOF)
398 	    return -1;
399 	  if ((c >= 0 && c <= 31)
400 	      || (c >= 128 && c <= 159))
401 	    break;
402 	  *p++ = c;
403 	}
404       *p++ = 0;
405 
406       if (h->flag_verbose > 1)
407 	printf (" string %u (pl=%d): %s\n", i, prev_len, h->str_table[i]);
408 
409       prev_len = c & 0x1f;
410       sh = 5;
411       while (c >= 128)
412 	{
413 	  c = fgetc (h->stream);
414 	  if (c == EOF)
415 	    return -1;
416 	  prev_len |= (c & 0x1f) << sh;
417 	  sh += 5;
418 	}
419     }
420   if (fread (hdr, 4, 1, h->stream) != 1)
421     return -1;
422   if (memcmp (hdr, "EOS", 4) != 0)
423     return -1;
424   return 0;
425 }
426 
427 union ghw_type *
ghw_get_base_type(union ghw_type * t)428 ghw_get_base_type (union ghw_type *t)
429 {
430   switch (t->kind)
431     {
432     case ghdl_rtik_type_b2:
433     case ghdl_rtik_type_e8:
434     case ghdl_rtik_type_e32:
435     case ghdl_rtik_type_i32:
436     case ghdl_rtik_type_i64:
437     case ghdl_rtik_type_f64:
438     case ghdl_rtik_type_p32:
439     case ghdl_rtik_type_p64:
440     case ghdl_rtik_type_array:
441       return t;
442     case ghdl_rtik_subtype_scalar:
443       return t->ss.base;
444     case ghdl_rtik_subtype_array:
445       return t->sa.base;
446     case ghdl_rtik_subtype_unbounded_array:
447       return t->sua.base;
448     default:
449       fprintf (stderr, "ghw_get_base_type: cannot handle type %d\n", t->kind);
450       abort ();
451     }
452 }
453 
454 /* Return -1 for unbounded types.  */
455 static int
get_nbr_elements(union ghw_type * t)456 get_nbr_elements (union ghw_type *t)
457 {
458   switch (t->kind)
459     {
460     case ghdl_rtik_type_b2:
461     case ghdl_rtik_type_e8:
462     case ghdl_rtik_type_e32:
463     case ghdl_rtik_type_i32:
464     case ghdl_rtik_type_i64:
465     case ghdl_rtik_type_f64:
466     case ghdl_rtik_type_p32:
467     case ghdl_rtik_type_p64:
468     case ghdl_rtik_subtype_scalar:
469       return 1;
470     case ghdl_rtik_type_array:
471       return -1;
472     case ghdl_rtik_subtype_array:
473       return t->sa.nbr_scalars;
474     case ghdl_rtik_type_record:
475       return t->rec.nbr_scalars;
476     case ghdl_rtik_subtype_record:
477       return t->sr.nbr_scalars;
478     case ghdl_rtik_subtype_unbounded_record:
479     case ghdl_rtik_subtype_unbounded_array:
480       return -1;
481     default:
482       fprintf (stderr, "get_nbr_elements: unhandled type %d\n", t->kind);
483       abort ();
484     }
485 }
486 
487 int
ghw_get_range_length(union ghw_range * rng)488 ghw_get_range_length (union ghw_range *rng)
489 {
490   int res;
491 
492   assert (rng != NULL);
493 
494   switch (rng->kind)
495     {
496     case ghdl_rtik_type_i32:
497       if (rng->i32.dir)
498 	res = rng->i32.left - rng->i32.right + 1;
499       else
500 	res = rng->i32.right - rng->i32.left + 1;
501       break;
502     case ghdl_rtik_type_b2:
503       if (rng->b2.dir)
504 	res = rng->b2.left - rng->b2.right + 1;
505       else
506 	res = rng->b2.right - rng->b2.left + 1;
507       break;
508     case ghdl_rtik_type_e8:
509       if (rng->e8.dir)
510 	res = rng->e8.left - rng->e8.right + 1;
511       else
512 	res = rng->e8.right - rng->e8.left + 1;
513       break;
514     default:
515       fprintf (stderr, "get_range_length: unhandled kind %d\n", rng->kind);
516       abort ();
517     }
518   /* The length of a null range is 0.  */
519   return (res <= 0) ? 0 : res;
520 }
521 
522 static union ghw_type *
523 ghw_read_type_bounds (struct ghw_handler *h, union ghw_type *base);
524 
525 /* Create an array subtype using BASE and ranges read from H.  */
526 
527 struct ghw_subtype_array *
ghw_read_array_subtype(struct ghw_handler * h,union ghw_type * base)528 ghw_read_array_subtype (struct ghw_handler *h, union ghw_type *base)
529 {
530   struct ghw_type_array *arr =
531     (struct ghw_type_array *)ghw_get_base_type (base);
532   struct ghw_subtype_array *sa;
533   unsigned j;
534   int nbr_scalars;
535   int nbr_els;
536 
537   sa = malloc (sizeof (struct ghw_subtype_array));
538   sa->kind = ghdl_rtik_subtype_array;
539   sa->name = NULL;
540   sa->base = base;
541   nbr_els = get_nbr_elements (arr->el);
542   nbr_scalars = 1;
543   sa->rngs = malloc (arr->nbr_dim * sizeof (union ghw_range *));
544   for (j = 0; j < arr->nbr_dim; j++)
545     {
546       sa->rngs[j] = ghw_read_range (h);
547       nbr_scalars *= ghw_get_range_length (sa->rngs[j]);
548     }
549   if (nbr_els >= 0)
550     {
551       /* Element type is bounded.  */
552       sa->el = arr->el;
553     }
554   else
555     {
556       /* Read bounds for the elements.  */
557       sa->el = ghw_read_type_bounds(h, arr->el);
558       nbr_els = get_nbr_elements (sa->el);
559     }
560     sa->nbr_scalars = nbr_scalars * nbr_els;
561   return sa;
562 }
563 
564 struct ghw_subtype_record *
ghw_read_record_subtype(struct ghw_handler * h,struct ghw_type_record * base)565 ghw_read_record_subtype (struct ghw_handler *h, struct ghw_type_record *base)
566 {
567   struct ghw_subtype_record *sr;
568 
569   sr = malloc (sizeof (struct ghw_subtype_record));
570   sr->kind = ghdl_rtik_subtype_record;
571   sr->name = NULL;
572   sr->base = base;
573   if (base->nbr_scalars >= 0)
574     {
575       /* Record base type is bounded.  */
576       sr->nbr_scalars = base->nbr_scalars;
577       sr->els = base->els;
578     }
579   else
580     {
581       /* Read subtypes.  */
582       unsigned j;
583       int nbr_scalars;
584 
585       sr->els = malloc (base->nbr_fields * sizeof (struct ghw_record_element));
586       nbr_scalars = 0;
587       for (j = 0; j < base->nbr_fields; j++)
588 	{
589 	  union ghw_type *btype = base->els[j].type;
590 	  int el_nbr_scalars = get_nbr_elements (btype);
591 
592 	  sr->els[j].name = base->els[j].name;
593 	  if (el_nbr_scalars >= 0)
594 	    {
595 	      /* Element is constrained.  */
596 	      sr->els[j].type = btype;
597 	    }
598 	  else
599 	    {
600               sr->els[j].type = ghw_read_type_bounds(h, btype);
601 	      el_nbr_scalars = get_nbr_elements (sr->els[j].type);
602 	    }
603 	  nbr_scalars += el_nbr_scalars;
604 	}
605       sr->nbr_scalars = nbr_scalars;
606     }
607   return sr;
608 }
609 
610 /* Read bounds for BASE and create a subtype.  */
611 
612 static union ghw_type *
ghw_read_type_bounds(struct ghw_handler * h,union ghw_type * base)613 ghw_read_type_bounds (struct ghw_handler *h, union ghw_type *base)
614 {
615   switch (base->kind)
616     {
617     case ghdl_rtik_type_array:
618     case ghdl_rtik_subtype_unbounded_array:
619       return (union ghw_type *)ghw_read_array_subtype (h, base);
620       break;
621     case ghdl_rtik_type_record:
622     case ghdl_rtik_subtype_unbounded_record:
623       return (union ghw_type *)ghw_read_record_subtype (h, &base->rec);
624       break;
625     default:
626       fprintf (stderr, "ghw_read_type_bounds: unhandled kind %d\n",
627                base->kind);
628       return NULL;
629     }
630 }
631 
632 int
ghw_read_type(struct ghw_handler * h)633 ghw_read_type (struct ghw_handler *h)
634 {
635   unsigned char hdr[8];
636   unsigned i;
637 
638   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
639     return -1;
640 
641   if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
642     return -1;
643   h->nbr_types = ghw_get_i32 (h, &hdr[4]);
644   h->types = (union ghw_type **)
645     malloc (h->nbr_types * sizeof (union ghw_type *));
646 
647   for (i = 0; i < h->nbr_types; i++)
648     {
649       int t;
650 
651       t = fgetc (h->stream);
652       if (t == EOF)
653 	return -1;
654       if (h->flag_verbose > 1)
655         printf ("type[%d]= %d\n", i, t);
656       switch (t)
657 	{
658 	case ghdl_rtik_type_b2:
659 	case ghdl_rtik_type_e8:
660 	  {
661 	    struct ghw_type_enum *e;
662 	    unsigned j;
663 
664 	    e = malloc (sizeof (struct ghw_type_enum));
665 	    e->kind = t;
666 	    e->wkt = ghw_wkt_unknown;
667 	    e->name = ghw_read_strid (h);
668 	    if (ghw_read_uleb128 (h, &e->nbr) != 0)
669 	      goto err_b2;
670 	    e->lits = (const char **) malloc (e->nbr * sizeof (char *));
671 	    if (h->flag_verbose > 1)
672 	      printf ("enum %s:", e->name);
673 	    for (j = 0; j < e->nbr; j++)
674 	      {
675 		e->lits[j] = ghw_read_strid (h);
676 		if (h->flag_verbose > 1)
677 		  printf (" %s", e->lits[j]);
678 	      }
679 	    if (h->flag_verbose > 1)
680 	      printf ("\n");
681 	    h->types[i] = (union ghw_type *)e;
682 	    break;
683 	  err_b2:
684 	    free (e);
685 	    return -1;
686 	  }
687 	  break;
688 	case ghdl_rtik_type_i32:
689 	case ghdl_rtik_type_i64:
690 	case ghdl_rtik_type_f64:
691 	  {
692 	    struct ghw_type_scalar *sc;
693 
694 	    sc = malloc (sizeof (struct ghw_type_scalar));
695 	    sc->kind = t;
696 	    sc->name = ghw_read_strid (h);
697 	    if (h->flag_verbose > 1)
698 	      printf ("scalar: %s\n", sc->name);
699 	    h->types[i] = (union ghw_type *)sc;
700 	  }
701 	  break;
702 	case ghdl_rtik_type_p32:
703 	case ghdl_rtik_type_p64:
704 	  {
705 	    struct ghw_type_physical *ph;
706 
707 	    ph = malloc (sizeof (struct ghw_type_physical));
708 	    ph->kind = t;
709 	    ph->name = ghw_read_strid (h);
710 	    ph->units = NULL;
711 	    if (h->version == 0)
712 	      ph->nbr_units = 0;
713 	    else
714 	      {
715 		unsigned j;
716 
717 		if (ghw_read_uleb128 (h, &ph->nbr_units) != 0)
718 		  goto err_p32;
719 		ph->units = malloc (ph->nbr_units * sizeof (struct ghw_unit));
720 		for (j = 0; j < ph->nbr_units; j++)
721 		  {
722 		    ph->units[j].name = ghw_read_strid (h);
723 		    if (ghw_read_lsleb128 (h, &ph->units[j].val) < 0)
724 		      goto err_p32;
725 		  }
726 	      }
727 	    if (h->flag_verbose > 1)
728 	      printf ("physical: %s\n", ph->name);
729 	    h->types[i] = (union ghw_type *)ph;
730 	    break;
731 	  err_p32:
732 	    free (ph->units);
733 	    free (ph);
734 	    return -1;
735 	  }
736 	  break;
737 	case ghdl_rtik_subtype_scalar:
738 	  {
739 	    struct ghw_subtype_scalar *ss;
740 
741 	    ss = malloc (sizeof (struct ghw_subtype_scalar));
742 	    ss->kind = t;
743 	    ss->name = ghw_read_strid (h);
744 	    ss->base = ghw_read_typeid (h);
745 	    ss->rng = ghw_read_range (h);
746 	    if (h->flag_verbose > 1)
747 	      printf ("subtype scalar: %s\n", ss->name);
748 	    h->types[i] = (union ghw_type *)ss;
749 	  }
750 	  break;
751 	case ghdl_rtik_type_array:
752 	  {
753 	    struct ghw_type_array *arr;
754 	    unsigned j;
755 
756 	    arr = malloc (sizeof (struct ghw_type_array));
757 	    arr->kind = t;
758 	    arr->name = ghw_read_strid (h);
759 	    arr->el = ghw_read_typeid (h);
760 	    if (ghw_read_uleb128 (h, &arr->nbr_dim) != 0)
761 	      goto err_array;
762 	    arr->dims = (union ghw_type **)
763 	      malloc (arr->nbr_dim * sizeof (union ghw_type *));
764 	    for (j = 0; j < arr->nbr_dim; j++)
765 	      arr->dims[j] = ghw_read_typeid (h);
766 	    if (h->flag_verbose > 1)
767 	      printf ("array: %s (ndim=%u) of %s\n",
768                       arr->name, arr->nbr_dim, arr->el->common.name);
769 	    h->types[i] = (union ghw_type *)arr;
770 	    break;
771 	  err_array:
772 	    free (arr);
773 	    return -1;
774 	  }
775 	  break;
776 	case ghdl_rtik_subtype_array:
777 	  {
778 	    struct ghw_subtype_array *sa;
779 	    const char *name;
780 	    union ghw_type *base;
781 
782 	    name = ghw_read_strid (h);
783 	    base = ghw_read_typeid (h);
784 
785 	    sa = ghw_read_array_subtype (h, base);
786 	    sa->name = name;
787 	    h->types[i] = (union ghw_type *)sa;
788 	    if (h->flag_verbose > 1)
789 	      printf ("subtype array: %s (nbr_scalars=%d)\n",
790 		      sa->name, sa->nbr_scalars);
791 	  }
792 	  break;
793 	case ghdl_rtik_subtype_unbounded_array:
794 	  {
795 	    struct ghw_subtype_unbounded_array *sua;
796 
797             sua = malloc (sizeof (struct ghw_subtype_unbounded_array));
798             sua->kind = t;
799 	    sua->name = ghw_read_strid (h);
800 	    sua->base = ghw_read_typeid (h);
801 	    h->types[i] = (union ghw_type *)sua;
802 	    if (h->flag_verbose > 1)
803 	      printf ("subtype unbounded array: %s\n", sua->name);
804 	  }
805 	  break;
806 	case ghdl_rtik_type_record:
807 	  {
808 	    struct ghw_type_record *rec;
809 	    unsigned j;
810 	    int nbr_scalars;
811 
812 	    rec = malloc (sizeof (struct ghw_type_record));
813 	    rec->kind = t;
814 	    rec->name = ghw_read_strid (h);
815 	    rec->els = NULL;
816 	    if (ghw_read_uleb128 (h, &rec->nbr_fields) != 0)
817 	      goto err_record;
818 	    rec->els = malloc
819 	      (rec->nbr_fields * sizeof (struct ghw_record_element));
820 	    nbr_scalars = 0;
821 	    for (j = 0; j < rec->nbr_fields; j++)
822 	      {
823 		rec->els[j].name = ghw_read_strid (h);
824 		rec->els[j].type = ghw_read_typeid (h);
825 		if (nbr_scalars != -1)
826 		  {
827 		    int field_nbr_scalars = get_nbr_elements (rec->els[j].type);
828 		    if (field_nbr_scalars == -1)
829 		      nbr_scalars = -1;
830 		    else
831 		      nbr_scalars += field_nbr_scalars;
832 		  }
833 	      }
834 	    rec->nbr_scalars = nbr_scalars;
835 	    if (h->flag_verbose > 1)
836 	      printf ("record type: %s (nbr_scalars=%d)\n",
837 		      rec->name, rec->nbr_scalars);
838 	    h->types[i] = (union ghw_type *)rec;
839 	    break;
840 	  err_record:
841 	    free (rec->els);
842 	    free (rec);
843 	    return -1;
844 	  }
845 	  break;
846 	case ghdl_rtik_subtype_record:
847 	  {
848 	    struct ghw_subtype_record *sr;
849 	    const char *name;
850 	    struct ghw_type_record *base;
851 
852 	    name = ghw_read_strid (h);
853 	    base = (struct ghw_type_record *)ghw_read_typeid (h);
854 
855 	    sr = ghw_read_record_subtype (h, base);
856 	    sr->name = name;
857 	    h->types[i] = (union ghw_type *)sr;
858 	    if (h->flag_verbose > 1)
859 	      printf ("subtype record: %s (nbr_scalars=%d)\n",
860 		      sr->name, sr->nbr_scalars);
861 	  }
862 	  break;
863 	default:
864 	  fprintf (stderr, "ghw_read_type: unknown type %d\n", t);
865 	  return -1;
866 	}
867     }
868   if (fgetc (h->stream) != 0)
869     return -1;
870   return 0;
871 }
872 
873 int
ghw_read_wk_types(struct ghw_handler * h)874 ghw_read_wk_types (struct ghw_handler *h)
875 {
876   char hdr[4];
877 
878   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
879     return -1;
880 
881   if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
882     return -1;
883 
884   while (1)
885     {
886       int t;
887       union ghw_type *tid;
888 
889       t = fgetc (h->stream);
890       if (t == EOF)
891 	return -1;
892       else if (t == 0)
893 	break;
894 
895       tid = ghw_read_typeid (h);
896       if (tid->kind == ghdl_rtik_type_b2
897 	  || tid->kind == ghdl_rtik_type_e8)
898 	{
899 	  if (h->flag_verbose > 0)
900 	    printf ("%s: wkt=%d\n", tid->en.name, t);
901 	  tid->en.wkt = t;
902 	}
903     }
904   return 0;
905 }
906 
907 void
ghw_disp_typename(struct ghw_handler * h,union ghw_type * t)908 ghw_disp_typename (struct ghw_handler *h, union ghw_type *t)
909 {
910   (void)h;
911   printf ("%s", t->common.name);
912 }
913 
914 /* Read a signal composed of severals elements.
915    Return 0 for success.  */
916 int
ghw_read_signal(struct ghw_handler * h,unsigned int * sigs,union ghw_type * t)917 ghw_read_signal (struct ghw_handler *h, unsigned int *sigs, union ghw_type *t)
918 {
919   switch (t->kind)
920     {
921     case ghdl_rtik_type_b2:
922     case ghdl_rtik_type_e8:
923     case ghdl_rtik_type_e32:
924     case ghdl_rtik_subtype_scalar:
925       {
926 	unsigned int sig_el;
927 
928 	if (ghw_read_uleb128 (h, &sig_el) < 0)
929 	  return -1;
930 	*sigs = sig_el;
931 	if (sig_el == 0 || sig_el >= h->nbr_sigs)
932 	  return -1;
933 	if (h->sigs[sig_el].type == NULL)
934 	  h->sigs[sig_el].type = ghw_get_base_type (t);
935       }
936       return 0;
937     case ghdl_rtik_subtype_array:
938       {
939 	int i;
940 	int stride;
941 	int len;
942 
943 	len = t->sa.nbr_scalars;
944 	stride = get_nbr_elements (t->sa.el);
945 
946 	for (i = 0; i < len; i += stride)
947 	  if (ghw_read_signal (h, &sigs[i], t->sa.el) < 0)
948 	    return -1;
949       }
950       return 0;
951     case ghdl_rtik_type_record:
952       {
953 	struct ghw_type_record *r = &t->rec;
954 	int nbr_fields = r->nbr_fields;
955 	int i;
956 	int off;
957 
958 	off = 0;
959 	for (i = 0; i < nbr_fields; i++)
960 	  {
961 	    if (ghw_read_signal (h, &sigs[off], r->els[i].type) < 0)
962 	      return -1;
963 	    off += get_nbr_elements (r->els[i].type);
964 	  }
965       }
966       return 0;
967     case ghdl_rtik_subtype_record:
968       {
969 	struct ghw_subtype_record *sr = &t->sr;
970 	int nbr_fields = sr->base->nbr_fields;
971 	int i;
972 	int off;
973 
974 	off = 0;
975 	for (i = 0; i < nbr_fields; i++)
976 	  {
977 	    if (ghw_read_signal (h, &sigs[off], sr->els[i].type) < 0)
978 	      return -1;
979 	    off += get_nbr_elements (sr->els[i].type);
980 	  }
981       }
982       return 0;
983     default:
984       fprintf (stderr, "ghw_read_signal: type kind %d unhandled\n", t->kind);
985       abort ();
986     }
987 }
988 
989 
990 int
ghw_read_value(struct ghw_handler * h,union ghw_val * val,union ghw_type * type)991 ghw_read_value (struct ghw_handler *h,
992 		union ghw_val *val, union ghw_type *type)
993 {
994   switch (ghw_get_base_type (type)->kind)
995     {
996     case ghdl_rtik_type_b2:
997       {
998 	int v;
999 	v = fgetc (h->stream);
1000 	if (v == EOF)
1001 	  return -1;
1002 	val->b2 = v;
1003       }
1004       break;
1005     case ghdl_rtik_type_e8:
1006       {
1007 	int v;
1008 	v = fgetc (h->stream);
1009 	if (v == EOF)
1010 	  return -1;
1011 	val->e8 = v;
1012       }
1013       break;
1014     case ghdl_rtik_type_i32:
1015     case ghdl_rtik_type_p32:
1016       {
1017 	int32_t v;
1018 	if (ghw_read_sleb128 (h, &v) < 0)
1019 	  return -1;
1020 	val->i32 = v;
1021       }
1022       break;
1023     case ghdl_rtik_type_f64:
1024       {
1025 	double v;
1026 	if (ghw_read_f64 (h, &v) < 0)
1027 	  return -1;
1028 	val->f64 = v;
1029       }
1030       break;
1031     case ghdl_rtik_type_p64:
1032       {
1033 	int64_t v;
1034 	if (ghw_read_lsleb128 (h, &v) < 0)
1035 	  return -1;
1036 	val->i64 = v;
1037       }
1038       break;
1039     default:
1040       fprintf (stderr, "read_value: cannot handle format %d\n", type->kind);
1041       abort ();
1042     }
1043   return 0;
1044 }
1045 
1046 int
ghw_read_hie(struct ghw_handler * h)1047 ghw_read_hie (struct ghw_handler *h)
1048 {
1049   unsigned char hdr[16];
1050   int nbr_scopes;
1051   int nbr_sigs;
1052   unsigned i;
1053   struct ghw_hie *blk;
1054   struct ghw_hie **last;
1055 
1056   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1057     return -1;
1058 
1059   if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
1060     return -1;
1061   nbr_scopes = ghw_get_i32 (h, &hdr[4]);
1062   /* Number of declared signals (which may be composite).  */
1063   nbr_sigs = ghw_get_i32 (h, &hdr[8]);
1064   /* Number of basic signals.  */
1065   h->nbr_sigs = ghw_get_i32 (h, &hdr[12]);
1066 
1067   if (h->flag_verbose)
1068     printf ("%u scopes, %u signals, %u signal elements\n",
1069 	    nbr_scopes, nbr_sigs, h->nbr_sigs);
1070 
1071   blk = (struct ghw_hie *)malloc (sizeof (struct ghw_hie));
1072   blk->kind = ghw_hie_design;
1073   blk->name = NULL;
1074   blk->parent = NULL;
1075   blk->brother = NULL;
1076   blk->u.blk.child = NULL;
1077 
1078   last = &blk->u.blk.child;
1079   h->hie = blk;
1080 
1081   h->nbr_sigs++;
1082   h->skip_sigs = NULL;
1083   h->flag_full_names = 0;
1084   h->sigs = (struct ghw_sig *) malloc (h->nbr_sigs * sizeof (struct ghw_sig));
1085   memset (h->sigs, 0, h->nbr_sigs * sizeof (struct ghw_sig));
1086 
1087   while (1)
1088     {
1089       int t;
1090       struct ghw_hie *el;
1091       unsigned int str;
1092 
1093       t = fgetc (h->stream);
1094       if (t == EOF)
1095 	return -1;
1096       if (t == 0)
1097 	break;
1098 
1099       if (t == ghw_hie_eos)
1100 	{
1101 	  blk = blk->parent;
1102 	  if (blk->u.blk.child == NULL)
1103 	    last = &blk->u.blk.child;
1104 	  else
1105 	    {
1106 	      struct ghw_hie *l = blk->u.blk.child;
1107 	      while (l->brother != NULL)
1108 		l = l->brother;
1109 	      last = &l->brother;
1110 	    }
1111 
1112 	  continue;
1113 	}
1114 
1115       el = (struct ghw_hie *) malloc (sizeof (struct ghw_hie));
1116       el->kind = t;
1117       el->parent = blk;
1118       el->brother = NULL;
1119 
1120       /* Link.  */
1121       *last = el;
1122       last = &el->brother;
1123 
1124       /* Read name.  */
1125       if (ghw_read_uleb128 (h, &str) != 0)
1126 	return -1;
1127       el->name = h->str_table[str];
1128 
1129       switch (t)
1130 	{
1131 	case ghw_hie_eoh:
1132 	case ghw_hie_design:
1133 	case ghw_hie_eos:
1134 	  /* Should not be here.  */
1135 	  abort ();
1136 	case ghw_hie_process:
1137 	  el->u.blk.child = NULL;
1138 	  break;
1139 	case ghw_hie_block:
1140 	case ghw_hie_generate_if:
1141 	case ghw_hie_generate_for:
1142 	case ghw_hie_instance:
1143 	case ghw_hie_generic:
1144 	case ghw_hie_package:
1145 	  /* Create a block.  */
1146 	  el->u.blk.child = NULL;
1147 
1148 	  if (t == ghw_hie_generate_for)
1149 	    {
1150 	      el->u.blk.iter_type = ghw_read_typeid (h);
1151 	      el->u.blk.iter_value = malloc (sizeof (union ghw_val));
1152 	      if (ghw_read_value (h, el->u.blk.iter_value,
1153 				  el->u.blk.iter_type) < 0)
1154 		return -1;
1155 	    }
1156 	  blk = el;
1157 	  last = &el->u.blk.child;
1158 	  break;
1159 	case ghw_hie_signal:
1160 	case ghw_hie_port_in:
1161 	case ghw_hie_port_out:
1162 	case ghw_hie_port_inout:
1163 	case ghw_hie_port_buffer:
1164 	case ghw_hie_port_linkage:
1165 	  /* For a signal, read type.  */
1166 	  {
1167 	    int nbr_el;
1168 	    unsigned int *sigs;
1169 
1170 	    el->u.sig.type = ghw_read_typeid (h);
1171 	    nbr_el = get_nbr_elements (el->u.sig.type);
1172 	    if (nbr_el < 0)
1173 	      return -1;
1174 	    sigs = (unsigned int *) malloc
1175 	      ((nbr_el + 1) * sizeof (unsigned int));
1176 	    el->u.sig.sigs = sigs;
1177 	    /* Last element is NULL.  */
1178 	    sigs[nbr_el] = 0;
1179 
1180 	    if (h->flag_verbose > 1)
1181 	      printf ("signal %s: %d el [", el->name, nbr_el);
1182 	    if (ghw_read_signal (h, sigs, el->u.sig.type) < 0)
1183 	      return -1;
1184 	    if (h->flag_verbose > 1)
1185 	      {
1186 		int j;
1187 		for (j = 0; j < nbr_el; j++)
1188 		  printf (" #%u", sigs[j]);
1189 		printf ("]\n");
1190 	      }
1191 	  }
1192 	  break;
1193 	default:
1194 	  fprintf (stderr, "ghw_read_hie: unhandled kind %d\n", t);
1195 	  abort ();
1196 	}
1197     }
1198 
1199   /* Allocate values.  */
1200   for (i = 0; i < h->nbr_sigs; i++)
1201     if (h->sigs[i].type != NULL)
1202       h->sigs[i].val = (union ghw_val *) malloc (sizeof (union ghw_val));
1203   return 0;
1204 }
1205 
1206 const char *
ghw_get_hie_name(struct ghw_hie * h)1207 ghw_get_hie_name (struct ghw_hie *h)
1208 {
1209   switch (h->kind)
1210     {
1211     case ghw_hie_eoh:
1212       return "eoh";
1213     case ghw_hie_design:
1214       return "design";
1215     case ghw_hie_block:
1216       return "block";
1217     case ghw_hie_generate_if:
1218       return "generate-if";
1219     case ghw_hie_generate_for:
1220       return "generate-for";
1221     case ghw_hie_instance:
1222       return "instance";
1223     case ghw_hie_package:
1224       return "package";
1225     case ghw_hie_process:
1226       return "process";
1227     case ghw_hie_generic:
1228       return "generic";
1229     case ghw_hie_eos:
1230       return "eos";
1231     case ghw_hie_signal:
1232       return "signal";
1233     case ghw_hie_port_in:
1234       return "port-in";
1235     case ghw_hie_port_out:
1236       return "port-out";
1237     case ghw_hie_port_inout:
1238       return "port-inout";
1239     case ghw_hie_port_buffer:
1240       return "port-buffer";
1241     case ghw_hie_port_linkage:
1242       return "port-linkage";
1243     default:
1244       return "??";
1245     }
1246 }
1247 
1248 void
1249 ghw_disp_value (union ghw_val *val, union ghw_type *type);
1250 
1251 static void
print_name(struct ghw_hie * hie,int full_names)1252 print_name (struct ghw_hie *hie, int full_names)
1253 {
1254   int i;
1255   int depth;
1256   struct ghw_hie *p;
1257   struct ghw_hie **buf;
1258   struct ghw_hie **end;
1259 
1260   /* HIE must be valid.  */
1261   assert (hie->name != NULL);
1262 
1263   if (0 == full_names)
1264     {
1265       printf (" %s: ", hie->name);
1266       return;
1267     }
1268 
1269   p = hie;
1270   depth = 0;
1271   while (p && p->name)
1272     {
1273       p = p->parent;
1274       ++depth;
1275     }
1276   buf = (struct ghw_hie **) malloc (depth * sizeof (struct ghw_hie *));
1277 
1278   p = hie;
1279   end = depth + buf;
1280   while (p && p->name)
1281     {
1282       *(--end) = p;
1283       p = p->parent;
1284     }
1285 
1286   putchar (' ');
1287   putchar ('/');
1288   for (i = 0; i < depth; ++i)
1289     {
1290       printf ("%s%s", i ? "/" : "", buf[i]->name);
1291       if (ghw_hie_generate_for == buf[i]->kind)
1292 	{
1293 	  putchar ('(');
1294 	  ghw_disp_value (buf[i]->u.blk.iter_value, buf[i]->u.blk.iter_type);
1295 	  putchar (')');
1296 	}
1297     }
1298   putchar (':');
1299   putchar (' ');
1300   free (buf);
1301 }
1302 
1303 void
ghw_disp_hie(struct ghw_handler * h,struct ghw_hie * top)1304 ghw_disp_hie (struct ghw_handler *h, struct ghw_hie *top)
1305 {
1306   int i;
1307   int indent;
1308   struct ghw_hie *hie;
1309   struct ghw_hie *n;
1310 
1311   hie = top;
1312   indent = 0;
1313 
1314   while (1)
1315     {
1316       if (0 == h->flag_full_names)
1317 	for (i = 0; i < indent; i++)
1318 	  fputc (' ', stdout);
1319       printf ("%s", ghw_get_hie_name (hie));
1320 
1321       switch (hie->kind)
1322 	{
1323 	case ghw_hie_design:
1324 	case ghw_hie_block:
1325 	case ghw_hie_generate_if:
1326 	case ghw_hie_generate_for:
1327 	case ghw_hie_instance:
1328 	case ghw_hie_process:
1329 	case ghw_hie_package:
1330 	  if (hie->name)
1331 	    print_name (hie, h->flag_full_names);
1332 	  if (hie->kind == ghw_hie_generate_for)
1333 	    {
1334 	      printf ("(");
1335 	      ghw_disp_value (hie->u.blk.iter_value, hie->u.blk.iter_type);
1336 	      printf (")");
1337 	    }
1338 	  n = hie->u.blk.child;
1339 	  if (n == NULL)
1340 	    n = hie->brother;
1341 	  else
1342 	    indent++;
1343 	  break;
1344 	case ghw_hie_generic:
1345 	case ghw_hie_eos:
1346 	  abort ();
1347 	case ghw_hie_signal:
1348 	case ghw_hie_port_in:
1349 	case ghw_hie_port_out:
1350 	case ghw_hie_port_inout:
1351 	case ghw_hie_port_buffer:
1352 	case ghw_hie_port_linkage:
1353 	  {
1354 	    unsigned int *sigs = hie->u.sig.sigs;
1355 	    unsigned int k, num;
1356 
1357 	    print_name (hie, h->flag_full_names);
1358 	    ghw_disp_subtype_indication (h, hie->u.sig.type);
1359 	    printf (":");
1360 	    k = 0;
1361 	    /* There can be 0-length signals.  */
1362 	    while (sigs[k] != GHW_NO_SIG)
1363 	      {
1364 		/* First signal of the range.  */
1365 		printf (" #%u", sigs[k]);
1366 		for (num = 1; sigs[k + num] != GHW_NO_SIG; num++)
1367 		  if (sigs[k + num] != sigs[k + num - 1] + 1)
1368 		    break;
1369 		if (num > 1)
1370 		  printf ("-#%u", sigs[k + num - 1]);
1371 		k += num;
1372 	      }
1373 	    n = hie->brother;
1374 	  }
1375 	  break;
1376 	default:
1377 	  abort ();
1378 	}
1379       printf ("\n");
1380 
1381       while (n == NULL)
1382 	{
1383 	  if (hie->parent == NULL)
1384 	    return;
1385 	  hie = hie->parent;
1386 	  indent--;
1387 	  n = hie->brother;
1388 	}
1389       hie = n;
1390     }
1391 }
1392 
1393 int
ghw_read_eoh(struct ghw_handler * h)1394 ghw_read_eoh (struct ghw_handler *h)
1395 {
1396   (void)h;
1397   return 0;
1398 }
1399 
1400 int
ghw_read_base(struct ghw_handler * h)1401 ghw_read_base (struct ghw_handler *h)
1402 {
1403   unsigned char hdr[4];
1404   int res;
1405 
1406   while (1)
1407     {
1408       if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1409 	return -1;
1410       if (memcmp (hdr, "STR", 4) == 0)
1411 	res = ghw_read_str (h);
1412       else if (memcmp (hdr, "HIE", 4) == 0)
1413 	res = ghw_read_hie (h);
1414       else if (memcmp (hdr, "TYP", 4) == 0)
1415 	res = ghw_read_type (h);
1416       else if (memcmp (hdr, "WKT", 4) == 0)
1417 	res = ghw_read_wk_types (h);
1418       else if (memcmp (hdr, "EOH", 4) == 0)
1419 	return 0;
1420       else
1421 	{
1422 	  fprintf (stderr, "ghw_read_base: unknown GHW section %c%c%c%c\n",
1423 		   hdr[0], hdr[1], hdr[2], hdr[3]);
1424 	  return -1;
1425 	}
1426       if (res != 0)
1427 	{
1428 	  fprintf (stderr, "ghw_read_base: error in section %s\n", hdr);
1429 	  return res;
1430 	}
1431     }
1432 }
1433 
1434 int
ghw_read_signal_value(struct ghw_handler * h,struct ghw_sig * s)1435 ghw_read_signal_value (struct ghw_handler *h, struct ghw_sig *s)
1436 {
1437   return ghw_read_value (h, s->val, s->type);
1438 }
1439 
1440 int
ghw_read_snapshot(struct ghw_handler * h)1441 ghw_read_snapshot (struct ghw_handler *h)
1442 {
1443   unsigned char hdr[12];
1444   unsigned i;
1445   struct ghw_sig *s;
1446 
1447   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1448     return -1;
1449 
1450   if (hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 0 || hdr[3] != 0)
1451     return -1;
1452   h->snap_time = ghw_get_i64 (h, &hdr[4]);
1453   if (h->flag_verbose > 1)
1454     printf ("Time is " GHWPRI64 " fs\n", h->snap_time);
1455 
1456   for (i = 0; i < h->nbr_sigs; i++)
1457     {
1458       s = &h->sigs[i];
1459       if (s->type != NULL)
1460 	{
1461 	  if (h->flag_verbose > 1)
1462 	    printf ("read type %d for sig %u\n", s->type->kind, i);
1463 	  if (ghw_read_signal_value (h, s) < 0)
1464 	    return -1;
1465 	}
1466     }
1467   if (fread (hdr, 4, 1, h->stream) != 1)
1468     return -1;
1469 
1470   if (memcmp (hdr, "ESN", 4))
1471     return -1;
1472 
1473   return 0;
1474 }
1475 
1476 void ghw_disp_values (struct ghw_handler *h);
1477 
1478 int
ghw_read_cycle_start(struct ghw_handler * h)1479 ghw_read_cycle_start (struct ghw_handler *h)
1480 {
1481   unsigned char hdr[8];
1482 
1483   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1484     return -1;
1485 
1486   h->snap_time = ghw_get_i64 (h, hdr);
1487   return 0;
1488 }
1489 
1490 int
ghw_read_cycle_cont(struct ghw_handler * h,int * list)1491 ghw_read_cycle_cont (struct ghw_handler *h, int *list)
1492 {
1493   int i;
1494   int *list_p;
1495 
1496   i = 0;
1497   list_p = list;
1498   while (1)
1499     {
1500       uint32_t d;
1501 
1502       /* Read delta to next signal.  */
1503       if (ghw_read_uleb128 (h, &d) < 0)
1504 	return -1;
1505       if (d == 0)
1506 	{
1507 	  /* Last signal reached.  */
1508 	  break;
1509 	}
1510 
1511       /* Find next signal.  */
1512       while (d > 0)
1513 	{
1514 	  i++;
1515 	  if (h->sigs[i].type != NULL)
1516 	    d--;
1517 	}
1518 
1519       if (ghw_read_signal_value (h, &h->sigs[i]) < 0)
1520 	return -1;
1521       if (list_p)
1522 	*list_p++ = i;
1523     }
1524 
1525   if (list_p)
1526     *list_p = 0;
1527   return 0;
1528 }
1529 
1530 int
ghw_read_cycle_next(struct ghw_handler * h)1531 ghw_read_cycle_next (struct ghw_handler *h)
1532 {
1533   int64_t d_time;
1534 
1535   if (ghw_read_lsleb128 (h, &d_time) < 0)
1536     return -1;
1537   if (d_time == -1)
1538     return 0;
1539   h->snap_time += d_time;
1540   return 1;
1541 }
1542 
1543 
1544 int
ghw_read_cycle_end(struct ghw_handler * h)1545 ghw_read_cycle_end (struct ghw_handler *h)
1546 {
1547   char hdr[4];
1548 
1549   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1550     return -1;
1551   if (memcmp (hdr, "ECY", 4))
1552     return -1;
1553 
1554   return 0;
1555 }
1556 
1557 static const char *
ghw_get_lit(union ghw_type * type,unsigned e)1558 ghw_get_lit (union ghw_type *type, unsigned e)
1559 {
1560   if (e >= type->en.nbr)
1561     return "??";
1562   else
1563     return type->en.lits[e];
1564 }
1565 
1566 static void
ghw_disp_lit(union ghw_type * type,unsigned e)1567 ghw_disp_lit (union ghw_type *type, unsigned e)
1568 {
1569   printf ("%s (%u)", ghw_get_lit (type, e), e);
1570 }
1571 
1572 void
ghw_disp_value(union ghw_val * val,union ghw_type * type)1573 ghw_disp_value (union ghw_val *val, union ghw_type *type)
1574 {
1575   switch (ghw_get_base_type (type)->kind)
1576     {
1577     case ghdl_rtik_type_b2:
1578       ghw_disp_lit (type, val->b2);
1579       break;
1580     case ghdl_rtik_type_e8:
1581       ghw_disp_lit (type, val->e8);
1582       break;
1583     case ghdl_rtik_type_i32:
1584       printf (GHWPRI32, val->i32);
1585       break;
1586     case ghdl_rtik_type_p64:
1587       printf (GHWPRI64, val->i64);
1588       break;
1589     case ghdl_rtik_type_f64:
1590       printf ("%g", val->f64);
1591       break;
1592     default:
1593       fprintf (stderr, "ghw_disp_value: cannot handle type %d\n",
1594 	       type->kind);
1595       abort ();
1596     }
1597 }
1598 
1599 /* Put the ASCII representation of VAL into BUF, whose size if LEN.
1600    A NUL is always written to BUF.
1601 */
1602 void
ghw_get_value(char * buf,int len,union ghw_val * val,union ghw_type * type)1603 ghw_get_value (char *buf, int len, union ghw_val *val, union ghw_type *type)
1604 {
1605   union ghw_type *base = ghw_get_base_type (type);
1606 
1607   switch (base->kind)
1608     {
1609     case ghdl_rtik_type_b2:
1610       if (val->b2 <= 1)
1611 	{
1612 	  strncpy (buf, base->en.lits[val->b2], len - 1);
1613 	  buf[len - 1] = 0;
1614 	}
1615       else
1616 	{
1617 	  snprintf (buf, len, "?%d", val->b2);
1618 	}
1619       break;
1620     case ghdl_rtik_type_e8:
1621       if (val->b2 <= base->en.nbr)
1622 	{
1623 	  strncpy (buf, base->en.lits[val->e8], len - 1);
1624 	  buf[len - 1] = 0;
1625 	}
1626       else
1627 	{
1628 	  snprintf (buf, len, "?%d", val->e8);
1629 	}
1630       break;
1631     case ghdl_rtik_type_i32:
1632       snprintf (buf, len, GHWPRI32, val->i32);
1633       break;
1634     case ghdl_rtik_type_p64:
1635       snprintf (buf, len, GHWPRI64, val->i64);
1636       break;
1637     case ghdl_rtik_type_f64:
1638       snprintf (buf, len, "%g", val->f64);
1639       break;
1640     default:
1641       snprintf (buf, len, "?bad type %d?", type->kind);
1642     }
1643 }
1644 
1645 static char
is_skip_signal(int * signals_to_keep,int nb_signals_to_keep,int signal)1646 is_skip_signal (int *signals_to_keep, int nb_signals_to_keep, int signal)
1647 {
1648   int i;
1649   for (i = 0; i < nb_signals_to_keep; ++i)
1650     {
1651       if (signal == signals_to_keep[i])
1652 	{
1653 	  return 0;
1654 	}
1655     }
1656   return 1;
1657 }
1658 
1659 void
ghw_filter_signals(struct ghw_handler * h,int * signals_to_keep,int nb_signals_to_keep)1660 ghw_filter_signals (struct ghw_handler *h,
1661 		    int *signals_to_keep, int nb_signals_to_keep)
1662 {
1663   unsigned i;
1664 
1665   if (0 < nb_signals_to_keep && 0 != signals_to_keep)
1666     {
1667       if (0 == h->skip_sigs)
1668 	{
1669 	  h->skip_sigs = (char *) malloc (sizeof (char) * h->nbr_sigs);
1670 	}
1671       for (i = 0; i < h->nbr_sigs; ++i)
1672 	{
1673 	  h->skip_sigs[i] = is_skip_signal (signals_to_keep,
1674 					    nb_signals_to_keep, i);
1675 	}
1676     }
1677   else
1678     {
1679       if (0 != h->skip_sigs)
1680 	{
1681 	  free (h->skip_sigs);
1682 	  h->skip_sigs = 0;
1683 	}
1684     }
1685 }
1686 
1687 void
ghw_disp_values(struct ghw_handler * h)1688 ghw_disp_values (struct ghw_handler *h)
1689 {
1690   unsigned i;
1691   for (i = 0; i < h->nbr_sigs; i++)
1692     {
1693       struct ghw_sig *s = &h->sigs[i];
1694       int skip = (0 != h->skip_sigs && (0 != h->skip_sigs[i]));
1695       if (s->type != NULL && !skip)
1696 	{
1697 	  printf ("#%u: ", i);
1698 	  ghw_disp_value (s->val, s->type);
1699 	  printf ("\n");
1700 	}
1701     }
1702 }
1703 int
ghw_read_directory(struct ghw_handler * h)1704 ghw_read_directory (struct ghw_handler *h)
1705 {
1706   unsigned char hdr[8];
1707   int nbr_entries;
1708   int i;
1709 
1710   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1711     return -1;
1712 
1713   nbr_entries = ghw_get_i32 (h, &hdr[4]);
1714 
1715   if (h->flag_verbose)
1716     printf ("Directory (%d entries):\n", nbr_entries);
1717 
1718   for (i = 0; i < nbr_entries; i++)
1719     {
1720       unsigned char ent[8];
1721       int pos;
1722 
1723       if (fread (ent, sizeof (ent), 1, h->stream) != 1)
1724 	return -1;
1725 
1726       pos = ghw_get_i32 (h, &ent[4]);
1727       if (h->flag_verbose)
1728 	printf (" %s at %d\n", ent, pos);
1729     }
1730 
1731   if (fread (hdr, 4, 1, h->stream) != 1)
1732     return -1;
1733   if (memcmp (hdr, "EOD", 4))
1734     return -1;
1735   return 0;
1736 }
1737 
1738 int
ghw_read_tailer(struct ghw_handler * h)1739 ghw_read_tailer (struct ghw_handler *h)
1740 {
1741   unsigned char hdr[8];
1742   int pos;
1743 
1744   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1745     return -1;
1746 
1747   pos = ghw_get_i32 (h, &hdr[4]);
1748 
1749   if (h->flag_verbose)
1750     printf ("Tailer: directory at %d\n", pos);
1751   return 0;
1752 }
1753 
1754 enum ghw_res
ghw_read_sm_hdr(struct ghw_handler * h,int * list)1755 ghw_read_sm_hdr (struct ghw_handler *h, int *list)
1756 {
1757   unsigned char hdr[4];
1758   int res;
1759 
1760   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1761     {
1762       if (feof (h->stream))
1763 	return ghw_res_eof;
1764       else
1765 	return ghw_res_error;
1766     }
1767   if (memcmp (hdr, "SNP", 4) == 0)
1768     {
1769       res = ghw_read_snapshot (h);
1770       if (res < 0)
1771 	return res;
1772       return ghw_res_snapshot;
1773     }
1774   else if (memcmp (hdr, "CYC", 4) == 0)
1775     {
1776       res = ghw_read_cycle_start (h);
1777       if (res < 0)
1778 	return res;
1779       res = ghw_read_cycle_cont (h, list);
1780       if (res < 0)
1781 	return res;
1782 
1783       return ghw_res_cycle;
1784     }
1785   else if (memcmp (hdr, "DIR", 4) == 0)
1786     {
1787       res = ghw_read_directory (h);
1788     }
1789   else if (memcmp (hdr, "TAI", 4) == 0)
1790     {
1791       res = ghw_read_tailer (h);
1792     }
1793   else
1794     {
1795       fprintf (stderr, "unknown GHW section %c%c%c%c\n",
1796 	       hdr[0], hdr[1], hdr[2], hdr[3]);
1797       return -1;
1798     }
1799   if (res != 0)
1800     return res;
1801   return ghw_res_other;
1802 }
1803 
1804 int
ghw_read_sm(struct ghw_handler * h,enum ghw_sm_type * sm)1805 ghw_read_sm (struct ghw_handler *h, enum ghw_sm_type *sm)
1806 {
1807   int res;
1808 
1809   while (1)
1810     {
1811       /* printf ("sm: state = %d\n", *sm); */
1812       switch (*sm)
1813 	{
1814 	case ghw_sm_init:
1815 	case ghw_sm_sect:
1816 	  res = ghw_read_sm_hdr (h, NULL);
1817 	  switch (res)
1818 	    {
1819 	    case ghw_res_other:
1820 	      break;
1821 	    case ghw_res_snapshot:
1822 	      *sm = ghw_sm_sect;
1823 	      return res;
1824 	    case ghw_res_cycle:
1825 	      *sm = ghw_sm_cycle;
1826 	      return res;
1827 	    default:
1828 	      return res;
1829 	    }
1830 	  break;
1831 	case ghw_sm_cycle:
1832 	  if (0)
1833 	    printf ("Time is " GHWPRI64 " fs\n", h->snap_time);
1834 	  if (0)
1835 	    ghw_disp_values (h);
1836 
1837 	  res = ghw_read_cycle_next (h);
1838 	  if (res < 0)
1839 	    return res;
1840 	  if (res == 1)
1841 	    {
1842 	      res = ghw_read_cycle_cont (h, NULL);
1843 	      if (res < 0)
1844 		return res;
1845 	      return ghw_res_cycle;
1846 	    }
1847 	  res = ghw_read_cycle_end (h);
1848 	  if (res < 0)
1849 	    return res;
1850 	  *sm = ghw_sm_sect;
1851 	  break;
1852 	}
1853     }
1854 }
1855 
1856 int
ghw_read_cycle(struct ghw_handler * h)1857 ghw_read_cycle (struct ghw_handler *h)
1858 {
1859   int res;
1860 
1861   res = ghw_read_cycle_start (h);
1862   if (res < 0)
1863     return res;
1864   while (1)
1865     {
1866       res = ghw_read_cycle_cont (h, NULL);
1867       if (res < 0)
1868 	return res;
1869 
1870       if (0)
1871 	printf ("Time is " GHWPRI64 " fs\n", h->snap_time);
1872       if (0)
1873 	ghw_disp_values (h);
1874 
1875 
1876       res = ghw_read_cycle_next (h);
1877       if (res < 0)
1878 	return res;
1879       if (res == 0)
1880 	break;
1881     }
1882   res = ghw_read_cycle_end (h);
1883   return res;
1884 }
1885 
1886 int
ghw_read_dump(struct ghw_handler * h)1887 ghw_read_dump (struct ghw_handler *h)
1888 {
1889   unsigned char hdr[4];
1890   int res;
1891 
1892   while (1)
1893     {
1894       if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1895 	{
1896 	  if (feof (h->stream))
1897 	    return 0;
1898 	  else
1899 	    return -1;
1900 	}
1901       if (memcmp (hdr, "SNP", 4) == 0)
1902 	{
1903 	  res = ghw_read_snapshot (h);
1904 	  if (0 && res >= 0)
1905 	    ghw_disp_values (h);
1906 	}
1907       else if (memcmp (hdr, "CYC", 4) == 0)
1908 	{
1909 	  res = ghw_read_cycle (h);
1910 	}
1911       else if (memcmp (hdr, "DIR", 4) == 0)
1912 	{
1913 	  res = ghw_read_directory (h);
1914 	}
1915       else if (memcmp (hdr, "TAI", 4) == 0)
1916 	{
1917 	  res = ghw_read_tailer (h);
1918 	}
1919       else
1920 	{
1921 	  fprintf (stderr, "unknown GHW section %c%c%c%c\n",
1922 		   hdr[0], hdr[1], hdr[2], hdr[3]);
1923 	  return -1;
1924 	}
1925       if (res != 0)
1926 	return res;
1927     }
1928 }
1929 
1930 struct ghw_section ghw_sections[] = {
1931   { "\0\0\0", NULL },
1932   { "STR", ghw_read_str },
1933   { "HIE", ghw_read_hie },
1934   { "TYP", ghw_read_type },
1935   { "WKT", ghw_read_wk_types },
1936   { "EOH", ghw_read_eoh },
1937   { "SNP", ghw_read_snapshot },
1938   { "CYC", ghw_read_cycle },
1939   { "DIR", ghw_read_directory },
1940   { "TAI", ghw_read_tailer }
1941 };
1942 
1943 int
ghw_read_section(struct ghw_handler * h)1944 ghw_read_section (struct ghw_handler *h)
1945 {
1946   unsigned char hdr[4];
1947   unsigned i;
1948 
1949   if (fread (hdr, sizeof (hdr), 1, h->stream) != 1)
1950     {
1951       if (feof (h->stream))
1952 	return -2;
1953       else
1954 	return -1;
1955     }
1956 
1957   for (i = 1; i < sizeof (ghw_sections) / sizeof (*ghw_sections); i++)
1958     if (memcmp (hdr, ghw_sections[i].name, 4) == 0)
1959       return i;
1960 
1961   fprintf (stderr, "ghw_read_section: unknown GHW section %c%c%c%c\n",
1962 	   hdr[0], hdr[1], hdr[2], hdr[3]);
1963   return 0;
1964 }
1965 
1966 void
ghw_close(struct ghw_handler * h)1967 ghw_close (struct ghw_handler *h)
1968 {
1969   if (h->stream)
1970     {
1971       if (h->stream_ispipe)
1972 	pclose (h->stream);
1973       else
1974 	fclose (h->stream);
1975 
1976       h->stream = NULL;
1977     }
1978 }
1979 
1980 const char *
ghw_get_dir(int is_downto)1981 ghw_get_dir (int is_downto)
1982 {
1983   return is_downto ? "downto" : "to";
1984 }
1985 
1986 void
ghw_disp_range(union ghw_type * type,union ghw_range * rng)1987 ghw_disp_range (union ghw_type *type, union ghw_range *rng)
1988 {
1989   switch (rng->kind)
1990     {
1991     case ghdl_rtik_type_b2:
1992       printf ("%s %s %s", ghw_get_lit (type, rng->b2.left),
1993 	      ghw_get_dir (rng->b2.dir), ghw_get_lit (type, rng->b2.right));
1994       break;
1995     case ghdl_rtik_type_e8:
1996       printf ("%s %s %s", ghw_get_lit (type, rng->e8.left),
1997 	      ghw_get_dir (rng->e8.dir), ghw_get_lit (type, rng->e8.right));
1998       break;
1999     case ghdl_rtik_type_i32:
2000     case ghdl_rtik_type_p32:
2001       printf (GHWPRI32 " %s " GHWPRI32,
2002 	      rng->i32.left, ghw_get_dir (rng->i32.dir), rng->i32.right);
2003       break;
2004     case ghdl_rtik_type_i64:
2005     case ghdl_rtik_type_p64:
2006       printf (GHWPRI64 " %s " GHWPRI64,
2007 	      rng->i64.left, ghw_get_dir (rng->i64.dir), rng->i64.right);
2008       break;
2009     case ghdl_rtik_type_f64:
2010       printf ("%g %s %g",
2011 	      rng->f64.left, ghw_get_dir (rng->f64.dir), rng->f64.right);
2012       break;
2013     default:
2014       printf ("?(%d)", rng->kind);
2015     }
2016 }
2017 
2018 static void
ghw_disp_array_subtype_bounds(struct ghw_subtype_array * a)2019 ghw_disp_array_subtype_bounds (struct ghw_subtype_array *a)
2020 {
2021   unsigned i;
2022   struct ghw_type_array *base =
2023     (struct ghw_type_array *)ghw_get_base_type (a->base);
2024 
2025   printf (" (");
2026   for (i = 0; i < base->nbr_dim; i++)
2027     {
2028       if (i != 0)
2029 	printf (", ");
2030       ghw_disp_range (base->dims[i], a->rngs[i]);
2031     }
2032   printf (")");
2033 }
2034 
2035 static void
ghw_disp_record_subtype_bounds(struct ghw_subtype_record * sr)2036 ghw_disp_record_subtype_bounds (struct ghw_subtype_record *sr)
2037 {
2038   struct ghw_type_record *base = sr->base;
2039   int is_first = 1;
2040   unsigned i;
2041 
2042   for (i = 0; i < base->nbr_fields; i++)
2043     {
2044       if (sr->els[i].type != base->els[i].type)
2045 	{
2046 	  if (is_first)
2047 	    {
2048 	      printf ("(");
2049 	      is_first = 0;
2050 	    }
2051 	  else
2052 	    printf (", ");
2053 	  printf ("%s", base->els[i].name);
2054 	  switch (sr->els[i].type->kind)
2055 	    {
2056 	    case ghdl_rtik_subtype_array:
2057 	      ghw_disp_array_subtype_bounds (&sr->els[i].type->sa);
2058 	      break;
2059 	    case ghdl_rtik_subtype_record:
2060 	      ghw_disp_record_subtype_bounds (&sr->els[i].type->sr);
2061 	      break;
2062 	    default:
2063 	      printf ("??? (%d)", sr->els[i].type->kind);
2064 	    }
2065 	}
2066     }
2067   if (!is_first)
2068     printf (")");
2069 }
2070 
2071 static void
ghw_disp_subtype_definition(struct ghw_handler * h,union ghw_type * t)2072 ghw_disp_subtype_definition (struct ghw_handler *h, union ghw_type *t)
2073 {
2074   switch (t->kind)
2075     {
2076     case ghdl_rtik_subtype_scalar:
2077       {
2078 	struct ghw_subtype_scalar *s = &t->ss;
2079 	ghw_disp_typename (h, s->base);
2080 	printf (" range ");
2081 	ghw_disp_range (s->base, s->rng);
2082       }
2083       break;
2084     case ghdl_rtik_subtype_array:
2085       {
2086 	struct ghw_subtype_array *a = &t->sa;
2087 
2088 	ghw_disp_typename (h, (union ghw_type *)a->base);
2089 	ghw_disp_array_subtype_bounds (a);
2090       }
2091       break;
2092     case ghdl_rtik_subtype_record:
2093       {
2094 	struct ghw_subtype_record *sr = &t->sr;
2095 
2096 	ghw_disp_typename (h, (union ghw_type *)sr->base);
2097 	ghw_disp_record_subtype_bounds (sr);
2098       }
2099       break;
2100     case ghdl_rtik_subtype_unbounded_array:
2101       {
2102 	struct ghw_subtype_unbounded_record *sur = &t->sur;
2103 
2104 	ghw_disp_typename (h, (union ghw_type *)sur->base);
2105       }
2106       break;
2107     default:
2108       printf ("ghw_disp_subtype_definition: unhandled type kind %d\n",
2109 	      t->kind);
2110     }
2111 }
2112 
2113 static int
ghw_is_anonymous_type(struct ghw_handler * h,union ghw_type * t)2114 ghw_is_anonymous_type (struct ghw_handler *h, union ghw_type *t)
2115 {
2116   return t->common.name == h->str_table[0];
2117 }
2118 
2119 void
ghw_disp_subtype_indication(struct ghw_handler * h,union ghw_type * t)2120 ghw_disp_subtype_indication (struct ghw_handler *h, union ghw_type *t)
2121 {
2122   if (ghw_is_anonymous_type (h, t))
2123     {
2124       /* Anonymous subtype.  */
2125       ghw_disp_subtype_definition (h, t);
2126     }
2127   else
2128     ghw_disp_typename (h, t);
2129 }
2130 
2131 void
ghw_disp_type(struct ghw_handler * h,union ghw_type * t)2132 ghw_disp_type (struct ghw_handler *h, union ghw_type *t)
2133 {
2134   switch (t->kind)
2135     {
2136     case ghdl_rtik_type_b2:
2137     case ghdl_rtik_type_e8:
2138       {
2139 	struct ghw_type_enum *e = &t->en;
2140 	unsigned i;
2141 
2142 	printf ("type %s is (", e->name);
2143 	for (i = 0; i < e->nbr; i++)
2144 	  {
2145 	    if (i != 0)
2146 	      printf (", ");
2147 	    printf ("%s", e->lits[i]);
2148 	  }
2149 	printf (");");
2150 	if (e->wkt != ghw_wkt_unknown)
2151 	  printf ("  -- WKT:%d", e->wkt);
2152 	printf ("\n");
2153       }
2154       break;
2155     case ghdl_rtik_type_i32:
2156     case ghdl_rtik_type_f64:
2157       {
2158 	struct ghw_type_scalar *s = &t->sc;
2159 	printf ("type %s is range <>;\n", s->name);
2160       }
2161       break;
2162     case ghdl_rtik_type_p32:
2163     case ghdl_rtik_type_p64:
2164       {
2165 	unsigned i;
2166 
2167 	struct ghw_type_physical *p = &t->ph;
2168 	printf ("type %s is range <> units\n", p->name);
2169 	for (i = 0; i < p->nbr_units; i++)
2170 	  {
2171 	    struct ghw_unit *u = &p->units[i];
2172 	    printf ("  %s = " GHWPRI64 " %s;\n",
2173 		    u->name, u->val, p->units[0].name);
2174 	  }
2175 	printf ("end units\n");
2176       }
2177       break;
2178     case ghdl_rtik_type_array:
2179       {
2180 	struct ghw_type_array *a = &t->ar;
2181 	unsigned i;
2182 
2183 	printf ("type %s is array (", a->name);
2184 	for (i = 0; i < a->nbr_dim; i++)
2185 	  {
2186 	    if (i != 0)
2187 	      printf (", ");
2188 	    ghw_disp_typename (h, a->dims[i]);
2189 	    printf (" range <>");
2190 	  }
2191 	printf (") of ");
2192 	ghw_disp_subtype_indication (h, a->el);
2193 	printf (";\n");
2194       }
2195       break;
2196     case ghdl_rtik_type_record:
2197       {
2198 	struct ghw_type_record *r = &t->rec;
2199 	unsigned i;
2200 
2201 	printf ("type %s is record\n", r->name);
2202 	for (i = 0; i < r->nbr_fields; i++)
2203 	  {
2204 	    printf ("  %s: ", r->els[i].name);
2205 	    ghw_disp_subtype_indication (h, r->els[i].type);
2206 	    printf (";\n");
2207 	  }
2208 	printf ("end record;\n");
2209       }
2210       break;
2211     case ghdl_rtik_subtype_array:
2212     case ghdl_rtik_subtype_scalar:
2213     case ghdl_rtik_subtype_record:
2214     case ghdl_rtik_subtype_unbounded_array:
2215       {
2216 	struct ghw_type_common *c = &t->common;
2217 	printf ("subtype %s is ", c->name);
2218 	ghw_disp_subtype_definition (h, t);
2219 	printf (";\n");
2220       }
2221       break;
2222     default:
2223       printf ("ghw_disp_type: unhandled type kind %d\n", t->kind);
2224     }
2225 }
2226 
2227 void
ghw_disp_types(struct ghw_handler * h)2228 ghw_disp_types (struct ghw_handler *h)
2229 {
2230   unsigned i;
2231 
2232   for (i = 0; i < h->nbr_types; i++)
2233     if (h->flag_verbose || !ghw_is_anonymous_type (h, h->types[i]))
2234       ghw_disp_type (h, h->types[i]);
2235 }
2236