• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nickg / nvc / 6604118477

22 Oct 2023 01:25PM UTC coverage: 91.218% (+0.3%) from 90.889%
6604118477

push

github

nickg
Generate unique name mangling for predefined functions

19 of 19 new or added lines in 5 files covered. (100.0%)

49523 of 54291 relevant lines covered (91.22%)

584870.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.73
/src/dump.c
1
//
2
//  Copyright (C) 2011-2023  Nick Gasson
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 3 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 <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "common.h"
20
#include "hash.h"
21
#include "ident.h"
22
#include "phase.h"
23
#include "psl/psl-phase.h"
24
#include "tree.h"
25
#include "type.h"
26
#include "vlog/vlog-phase.h"
27

28
#include <assert.h>
29
#include <string.h>
30
#include <ctype.h>
31
#include <inttypes.h>
32

33
#define DUMP_TYPE_HINT  0
34
#define DUMP_ADDRESS    0
35
#define DUMP_STATICNESS 0
36
#define DUMP_GEN_NAMES  0
37

38
static void dump_expr(tree_t t);
39
static void dump_stmt(tree_t t, int indent);
40
static void dump_port(tree_t t, int indent);
41
static void dump_decl(tree_t t, int indent);
42
static void dump_decls(tree_t t, int indent);
43
static void dump_type(type_t type);
44
static void dump_package(tree_t t, int indent);
45
static void dump_package_body(tree_t t, int indent);
46
static void dump_constraint(tree_t t);
47
static void dump_elem_constraints(type_t type);
48

49
typedef tree_t (*get_fn_t)(tree_t, unsigned);
50

51
static void tab(int indent)
201✔
52
{
53
   print_syntax("%*s", indent, "");
201✔
54
}
201✔
55

56
static void cannot_dump(tree_t t, const char *hint)
×
57
{
58
   print_syntax("\n");
×
59
   fflush(stdout);
×
60
   fatal("cannot dump %s kind %s", hint, tree_kind_str(tree_kind(t)));
×
61
}
62

63
static void dump_param(tree_t p)
23✔
64
{
65
   switch (tree_subkind(p)) {
23✔
66
   case P_POS:
67
      break;
68
   case P_NAMED:
2✔
69
      dump_expr(tree_name(p));
2✔
70
      print_syntax(" => ");
2✔
71
      break;
2✔
72
   }
73
   dump_expr(tree_value(p));
23✔
74
}
23✔
75

76
static void dump_params(tree_t t, get_fn_t get, int n, const char *prefix)
13✔
77
{
78
   if (n > 0) {
13✔
79
      if (prefix != NULL) {
12✔
80
         print_syntax(prefix, "");
2✔
81
         print_syntax(" ");
2✔
82
      }
83
      print_syntax("(");
12✔
84
      for (int i = 0; i < n; i++) {
35✔
85
         if (i > 0)
23✔
86
            print_syntax(", ");
11✔
87
         dump_param((*get)(t, i));
23✔
88
      }
89
      print_syntax(")");
12✔
90
   }
91
}
13✔
92

93
static void dump_range(tree_t r)
7✔
94
{
95
   switch (tree_subkind(r)) {
7✔
96
   case RANGE_TO:
5✔
97
      dump_expr(tree_left(r));
5✔
98
      print_syntax(" #to ");
5✔
99
      dump_expr(tree_right(r));
5✔
100
      break;
5✔
101
   case RANGE_DOWNTO:
2✔
102
      dump_expr(tree_left(r));
2✔
103
      print_syntax(" #downto ");
2✔
104
      dump_expr(tree_right(r));
2✔
105
      break;
2✔
106
   case RANGE_EXPR:
×
107
   case RANGE_ERROR:
108
      if (tree_has_value(r))
×
109
         dump_expr(tree_value(r));
×
110
      return;
111
   }
112
}
7✔
113

114
static void dump_type_hint(tree_t t)
115
{
116
#if DUMP_TYPE_HINT
117
   static int nested = 0;
118
   if (++nested == 1 && tree_has_type(t)) {
119
      color_printf("$red$$<$/*");
120
      dump_type(tree_type(t));
121
      color_printf("$>$$red$*/$$");
122
   }
123
   --nested;
124
#endif
125
}
126

127
static void dump_address(tree_t t)
128
{
129
#if DUMP_ADDRESS
130
   uint32_t a = (uint32_t)((uintptr_t)tree_arena(t) >> 2);
131
   a = (a ^ 61) ^ (a >> 16);
132
   a = a + (a << 3);
133
   a = a ^ (a >> 4);
134
   a = a * UINT32_C(0x27d4eb2d);
135
   a = a ^ (a >> 15);
136

137
   const int r = 1 + a % 5;
138
   const int g = 1 + (a >> 8) % 5;
139
   const int b = 1 + (a >> 16) % 5;
140

141
   const int color = 16 + 36*r + 6*g + b;
142

143
   char *LOCAL fmt = xasprintf("$!#%d${%%p}$$", color);
144
   color_printf(fmt, t);
145
#endif
146
}
147

148
static void dump_waveform(tree_t w)
3✔
149
{
150
   if (tree_has_value(w))
3✔
151
      dump_expr(tree_value(w));
3✔
152
   else
153
      print_syntax("#null");
×
154

155
   if (tree_has_delay(w)) {
3✔
156
      print_syntax(" #after ");
3✔
157
      dump_expr(tree_delay(w));
3✔
158
   }
159
}
3✔
160

161
static void dump_expr(tree_t t)
162✔
162
{
163
   switch (tree_kind(t)) {
162✔
164
   case T_PROT_FCALL:
1✔
165
      dump_expr(tree_name(t));
1✔
166
      print_syntax(".");
1✔
167
      // Fall-through
168
   case T_FCALL:
10✔
169
      if (tree_has_ref(t)) {
10✔
170
         tree_t decl = tree_ref(t);
10✔
171
         dump_address(decl);
10✔
172
         print_syntax("%s", istr(tree_ident(decl)));
10✔
173
      }
174
      else
175
         print_syntax("%s", istr(tree_ident(t)));
×
176
      dump_params(t, tree_param, tree_params(t), NULL);
10✔
177
#if DUMP_STATICNESS
178
      if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
179
         color_printf("$red$/* locally static */$$");
180
      else if (tree_flags(t) & TREE_F_GLOBALLY_STATIC)
181
         color_printf("$red$/* globally static */$$");
182
#endif
183
      break;
10✔
184

185
   case T_CONV_FUNC:
×
186
      if (tree_has_ref(t)) {
×
187
         tree_t decl = tree_ref(t);
×
188
         dump_address(decl);
×
189
         print_syntax("%s", istr(tree_ident(decl)));
×
190
      }
191
      else
192
         print_syntax("%s", istr(tree_ident(t)));
×
193
      print_syntax("(");
×
194
      dump_expr(tree_value(t));
×
195
      print_syntax(")");
×
196
      break;
×
197

198
   case T_LITERAL:
29✔
199
      switch (tree_subkind(t)) {
29✔
200
      case L_INT:
23✔
201
         print_syntax("%"PRIi64, tree_ival(t));
23✔
202
         break;
23✔
203
      case L_PHYSICAL:
4✔
204
         if (tree_has_ident(t))
4✔
205
            print_syntax("%"PRIi64" %s", tree_ival(t), istr(tree_ident(t)));
4✔
206
         else
207
            print_syntax("%"PRIi64, tree_ival(t));
×
208
         break;
209
      case L_REAL:
2✔
210
         print_syntax("%lf", tree_dval(t));
2✔
211
         break;
2✔
212
      case L_NULL:
×
213
         print_syntax("#null");
×
214
         break;
×
215
      default:
216
         assert(false);
×
217
      }
218
      break;
219

220
   case T_STRING:
1✔
221
      {
222
         print_syntax("\"");
1✔
223
         const int nchars = tree_chars(t);
1✔
224
         for (int i = 0; i < nchars; i++) {
7✔
225
            ident_t rune = tree_ident(tree_char(t, i));
6✔
226
            if (ident_char(rune, 0) == '\'')
6✔
227
               print_syntax("%c", ident_char(rune, 1));
6✔
228
            else
229
               print_syntax("\" & %s & \"", istr(rune));
×
230
         }
231
         print_syntax("\"");
1✔
232
      }
233
      break;
1✔
234

235
   case T_NEW:
×
236
      print_syntax("#new ");
×
237
      dump_expr(tree_value(t));
×
238
      break;
×
239

240
   case T_ALL:
×
241
      if (tree_has_value(t)) {
×
242
         dump_expr(tree_value(t));
×
243
         print_syntax(".#all");
×
244
      }
245
      else
246
         print_syntax("#all");
×
247
      break;
248

249
   case T_TYPE_REF:
1✔
250
      dump_type(tree_type(t));
1✔
251
      break;
1✔
252

253
   case T_AGGREGATE:
3✔
254
      print_syntax("(");
3✔
255
      for (unsigned i = 0; i < tree_assocs(t); i++) {
7✔
256
         if (i > 0)
4✔
257
            print_syntax(", ");
1✔
258
         tree_t a = tree_assoc(t, i);
4✔
259
         tree_t value = tree_value(a);
4✔
260
         switch (tree_subkind(a)) {
4✔
261
         case A_POS:
×
262
            dump_expr(value);
×
263
            break;
×
264
         case A_NAMED:
1✔
265
            dump_expr(tree_name(a));
1✔
266
            print_syntax(" => ");
1✔
267
            dump_expr(value);
1✔
268
            break;
1✔
269
         case A_OTHERS:
3✔
270
            print_syntax("#others => ");
3✔
271
            dump_expr(value);
3✔
272
            break;
3✔
273
         case A_RANGE:
×
274
            dump_range(tree_range(a, 0));
×
275
            print_syntax(" => ");
×
276
            dump_expr(value);
×
277
            break;
×
278
         default:
279
            assert(false);
×
280
         }
281
      }
282
      print_syntax(")");
3✔
283
      break;
3✔
284

285
   case T_REF:
37✔
286
      if (tree_has_ref(t)) {
37✔
287
         tree_t decl = tree_ref(t);
37✔
288
         dump_address(decl);
37✔
289
         print_syntax("%s", istr(tree_ident(decl)));
37✔
290
      }
291
      else
292
         print_syntax("%s", istr(tree_ident(t)));
×
293
      break;
294

295
   case T_ATTR_REF:
1✔
296
      dump_expr(tree_name(t));
1✔
297
      print_syntax("'%s", istr(tree_ident(t)));
1✔
298
      if (tree_params(t) > 0)
1✔
299
         dump_params(t, tree_param, tree_params(t), NULL);
×
300
      break;
301

302
   case T_EXTERNAL_NAME:
×
303
      {
304
         print_syntax("<< #%s ", class_str(tree_class(t)));
×
305
         const int nparts = tree_parts(t);
×
306
         for (int i = 0; i < nparts; i++) {
×
307
            tree_t part = tree_part(t, i);
×
308
            if (i > 0 || tree_subkind(part) == PE_SIMPLE)
×
309
               print_syntax(".");
×
310
            switch (tree_subkind(part)) {
×
311
            case PE_SIMPLE:
×
312
               print_syntax("%s", istr(tree_ident(part)));
×
313
               break;
×
314
            case PE_ABSOLUTE:
×
315
               print_syntax(".");
×
316
               break;
×
317
            case PE_CARET:
×
318
               print_syntax("^");
×
319
               break;
×
320
            }
321
         }
×
322
         print_syntax(" : ");
×
323
         dump_type(tree_type(t));
×
324
         print_syntax(" >>");
×
325
      }
326
      break;
×
327

328
   case T_ARRAY_REF:
1✔
329
      dump_expr(tree_value(t));
1✔
330
      dump_params(t, tree_param, tree_params(t), NULL);
1✔
331
      break;
1✔
332

333
   case T_ARRAY_SLICE:
×
334
      dump_expr(tree_value(t));
×
335
      print_syntax("(");
×
336
      dump_range(tree_range(t, 0));
×
337
      print_syntax(")");
×
338
      break;
×
339

340
   case T_RECORD_REF:
×
341
      dump_expr(tree_value(t));
×
342
      print_syntax(".%s", istr(tree_ident(t)));
×
343
      break;
×
344

345
   case T_TYPE_CONV:
×
346
      print_syntax("%s(", istr(type_ident(tree_type(t))));
×
347
      dump_expr(tree_value(t));
×
348
      print_syntax(")");
×
349
      break;
×
350

351
   case T_QUALIFIED:
×
352
      if (tree_has_value(t)) {
×
353
         print_syntax("%s'(", istr(type_ident(tree_type(t))));
×
354
         dump_expr(tree_value(t));
×
355
         print_syntax(")");
×
356
      }
357
      else
358
         dump_type(tree_type(t));
×
359
      break;
360

361
   case T_OPEN:
1✔
362
      print_syntax("#open");
1✔
363
      break;
1✔
364

365
   case T_BOX:
78✔
366
      print_syntax("<>");
78✔
367
      break;
78✔
368

369
   case T_WAVEFORM:
×
370
      dump_waveform(t);
×
371
      break;
×
372

373
   case T_PACKAGE_MAP:
×
374
      print_syntax("%s ", istr(tree_ident(t)));
×
375
      switch (tree_subkind(t)) {
×
376
      case PACKAGE_MAP_BOX:
×
377
         print_syntax("#generic #map (<>)");
×
378
         break;
×
379
      case PACKAGE_MAP_DEFAULT:
×
380
         print_syntax("#generic map (#default)");
×
381
         break;
×
382
      case PACKAGE_MAP_MATCHING:
×
383
         dump_params(t, tree_genmap, tree_genmaps(t), "#generic #map");
×
384
         break;
×
385
      }
386
      break;
387

388
   case T_COND_VALUE:
389
      for (int i = 0; i < tree_conds(t); i++) {
×
390
         tree_t c = tree_cond(t, i);
×
391
         if (i > 0)
×
392
            print_syntax(" #else ");
×
393
         if (tree_has_result(c))
×
394
            dump_expr(tree_result(c));
×
395
         else
396
            print_syntax("#unaffected");
×
397
         if (tree_has_value(c)) {
×
398
            print_syntax(" #when ");
×
399
            dump_expr(tree_value(c));
×
400
         }
401
      }
402
      break;
403

404
   default:
×
405
      cannot_dump(t, "expr");
×
406
   }
407

408
   dump_type_hint(t);
162✔
409
}
162✔
410

411
static void dump_record_elem_constraint(tree_t t)
×
412
{
413
   print_syntax("%s", istr(tree_ident(t)));
×
414

415
   type_t ftype = tree_type(t);
×
416
   const int ncon = type_constraints(ftype);
×
417
   for (int i = 0; i < ncon; i++)
×
418
      dump_constraint(type_constraint(ftype, i));
×
419

420
   dump_elem_constraints(ftype);
×
421
}
×
422

423
static void dump_constraint(tree_t t)
5✔
424
{
425
   const int nranges = tree_ranges(t);
5✔
426

427
   switch (tree_subkind(t)) {
5✔
428
   case C_RANGE:
1✔
429
      print_syntax(" #range ");
1✔
430
      dump_range(tree_range(t, 0));
1✔
431
      break;
1✔
432
   case C_INDEX:
4✔
433
      print_syntax("(");
4✔
434
      for (int i = 0; i < nranges; i++) {
8✔
435
         if (i > 0) print_syntax(", ");
4✔
436
         dump_range(tree_range(t, i));
4✔
437
      }
438
      print_syntax(")");
4✔
439
      break;
4✔
440
   case C_OPEN:
×
441
      print_syntax("(#open)");
×
442
      break;
×
443
   case C_RECORD:
×
444
      print_syntax("(");
×
445
      for (int i = 0; i < nranges; i++) {
×
446
         if (i > 0) print_syntax(", ");
×
447
         dump_record_elem_constraint(tree_range(t, i));
×
448
      }
449
      print_syntax(")");
×
450
      break;
×
451
   }
452
}
5✔
453

454
static void dump_elem_constraints(type_t type)
5✔
455
{
456
   if (type_is_array(type) && type_has_elem(type)) {
5✔
457
      type_t elem = type_elem(type);
×
458
      if (type_kind(elem) == T_SUBTYPE && !type_has_ident(elem)) {
×
459
         // Anonymous subtype created for element constraints
460
         assert(type_constraints(elem) == 1);
×
461
         dump_constraint(type_constraint(elem, 0));
×
462
         dump_elem_constraints(elem);
×
463
      }
464
   }
465
}
5✔
466

467
static void dump_type(type_t type)
111✔
468
{
469
   if (type_kind(type) == T_SUBTYPE && !type_has_ident(type)) {
111✔
470
      // Anonymous subtype
471
      print_syntax("%s", type_pp(type));
4✔
472
      if (type_ident(type) == type_ident(type_base(type))) {
4✔
473
         const int ncon = type_constraints(type);
4✔
474
         for (int i = 0; i < ncon; i++)
8✔
475
            dump_constraint(type_constraint(type, i));
4✔
476
      }
477
      dump_elem_constraints(type);
4✔
478
   }
479
   else if (type_is_none(type))
107✔
480
      print_syntax("/* error */");
×
481
   else
482
      print_syntax("%s", type_pp(type));
107✔
483
}
111✔
484

485
static void dump_arguments(tree_t t, int indent, const char *trailer)
6✔
486
{
487
   const int nports = tree_ports(t);
6✔
488
   if (nports > 0) {
6✔
489
      print_syntax(" (");
3✔
490
      if (nports > 1) {
3✔
491
         print_syntax("\n");
1✔
492
         for (int i = 0; i < nports; i++) {
3✔
493
            if (i > 0) print_syntax(";\n");
2✔
494
            dump_port(tree_port(t, i), indent + 4);
2✔
495
         }
496
      }
497
      else
498
         dump_port(tree_port(t, 0), 1);
2✔
499
      print_syntax(" )%s", trailer);
3✔
500
   }
501
}
6✔
502

503
static void dump_ports(tree_t t, int indent)
4✔
504
{
505
   const int nports = tree_ports(t);
4✔
506
   if (nports > 0) {
4✔
507
      tab(indent);
2✔
508
      print_syntax("#port (");
2✔
509
      if (nports > 1) {
2✔
510
         print_syntax("\n");
2✔
511
         for (unsigned i = 0; i < nports; i++) {
6✔
512
            if (i > 0) print_syntax(";\n");
4✔
513
            dump_port(tree_port(t, i), indent + 2);
4✔
514
         }
515
      }
516
      else
517
         dump_port(tree_port(t, 0), 1);
×
518
      print_syntax(" );\n");
2✔
519
   }
520
}
4✔
521

522
static void dump_generics(tree_t t, int indent, const char *trailer)
11✔
523
{
524
   const int ngenerics = tree_generics(t);
11✔
525
   if (ngenerics > 0) {
11✔
526
      tab(indent);
3✔
527
      print_syntax("#generic (");
3✔
528
      if (ngenerics > 1) {
3✔
529
         print_syntax("\n");
2✔
530
         for (int i = 0; i < ngenerics; i++) {
93✔
531
            tree_t g = tree_generic(t, i);
91✔
532
            dump_port(g, indent + 2);
91✔
533
            if (i + 1 == ngenerics && (tree_flags(g) & TREE_F_PREDEFINED)) {
91✔
534
               print_syntax(";\n");
2✔
535
               tab(indent - 1);
2✔
536
            }
537
            else if (i + 1 < ngenerics)
89✔
538
               print_syntax(";\n");
89✔
539
         }
540
      }
541
      else
542
         dump_port(tree_generic(t, 0), 1);
1✔
543
      print_syntax(" )%s", trailer);
3✔
544
   }
545
}
11✔
546

547
static void dump_port_map(tree_t t, int indent, const char *trailer)
2✔
548
{
549
   const int nparams = tree_params(t);
2✔
550
   if (nparams > 0) {
2✔
551
      tab(indent);
1✔
552
      dump_params(t, tree_param, nparams, "#port #map");
1✔
553
      print_syntax("%s", trailer);
1✔
554
   }
555
}
2✔
556

557
static void dump_generic_map(tree_t t, int indent, const char *trailer)
3✔
558
{
559
   const int ngenmaps = tree_genmaps(t);
3✔
560
   if (ngenmaps > 0) {
3✔
561
      tab(indent);
1✔
562
      dump_params(t, tree_genmap, ngenmaps, "#generic #map");
1✔
563
      print_syntax("%s", trailer);
1✔
564
   }
565
}
3✔
566

567
static void dump_binding(tree_t t, int indent)
×
568
{
569
   print_syntax("#use %s", istr(tree_ident(t)));
×
570
   if (tree_has_ident2(t))
×
571
      print_syntax("(%s)", istr(tree_ident2(t)));
×
572
   if (tree_genmaps(t) > 0 || tree_params(t) > 0)
×
573
      print_syntax("\n");
×
574
   dump_generic_map(t, indent + 2, tree_params(t) > 0 ? "\n" : "");
×
575
   dump_port_map(t, indent + 2, "");
×
576
   print_syntax(";\n");
×
577
}
×
578

579
static void dump_stmts(tree_t t, int indent)
10✔
580
{
581
   const int nstmts = tree_stmts(t);
10✔
582
   for (int i = 0; i < nstmts; i++) {
19✔
583
      tree_t s = tree_stmt(t, i);
9✔
584
      const tree_kind_t kind = tree_kind(s);
9✔
585
      const bool needs_newline =
18✔
586
         kind == T_BLOCK || kind == T_PROCESS || kind == T_INSTANCE;
9✔
587
      if (needs_newline && i > 0)
9✔
588
         print_syntax("\n");
×
589
      dump_stmt(s, indent);
9✔
590
   }
591
}
10✔
592

593
static void dump_block(tree_t t, int indent)
5✔
594
{
595
   dump_decls(t, indent + 2);
5✔
596
   tab(indent);
5✔
597
   print_syntax("#begin\n");
5✔
598
   dump_stmts(t, indent + 2);
5✔
599
}
5✔
600

601
static void dump_wait_level(tree_t t)
2✔
602
{
603
   const tree_flags_t flags = tree_flags(t);
2✔
604
   if (flags & TREE_F_NEVER_WAITS)
2✔
605
      print_syntax("   -- Never waits");
2✔
606
   else if (flags & TREE_F_HAS_WAIT)
×
607
      print_syntax("   -- Contains wait statement");
×
608
}
2✔
609

610
static void dump_type_decl(tree_t t, int indent)
2✔
611
{
612
   type_t type = tree_type(t);
2✔
613
   const type_kind_t kind = type_kind(type);
2✔
614

615
   print_syntax("#type %s", istr(tree_ident(t)));
2✔
616

617
   if (kind == T_INCOMPLETE) {
2✔
618
      print_syntax(";\n");
×
619
      return;
×
620
   }
621

622
   print_syntax(" #is ");
2✔
623

624
   if (type_is_integer(type) || type_is_real(type)) {
2✔
625
      print_syntax("#range ");
×
626
      dump_range(type_dim(type, 0));
×
627
   }
628
   else if (type_is_physical(type)) {
2✔
629
      print_syntax("#range ");
×
630
      dump_range(type_dim(type, 0));
×
631
      print_syntax("\n");
×
632
      tab(indent + 2);
×
633
      print_syntax("#units\n");
×
634
      {
635
         const int nunits = type_units(type);
×
636
         for (int i = 0; i < nunits; i++) {
×
637
            tree_t u = type_unit(type, i);
×
638
            tab(indent + 4);
×
639
            print_syntax("%s = ", istr(tree_ident(u)));
×
640
            dump_expr(tree_value(u));
×
641
            print_syntax(";\n");
×
642
         }
643
      }
644
      tab(indent + 2);
×
645
      print_syntax("#end #units");
×
646
   }
647
   else if (type_is_array(type)) {
2✔
648
      print_syntax("#array ");
1✔
649
      if (kind == T_ARRAY) {
1✔
650
         print_syntax("(");
1✔
651
         const int nindex = type_indexes(type);
1✔
652
         for (int i = 0; i < nindex; i++) {
2✔
653
            if (i > 0) print_syntax(", ");
1✔
654
            dump_type(type_index(type, i));
1✔
655
            print_syntax(" #range <>");
1✔
656
         }
657
         print_syntax(")");
1✔
658
      }
659
      else if (kind == T_SUBTYPE) {
×
660
         const int ncon = type_constraints(type);
×
661
         for (int i = 0; i < ncon; i++)
×
662
            dump_constraint(type_constraint(type, i));
×
663
      }
664
      else {
665
         print_syntax("(");
×
666
         const int ndims = type_dims(type);
×
667
         for (int i = 0; i < ndims; i++) {
×
668
            if (i > 0) print_syntax(", ");
×
669
            dump_range(type_dim(type, i));
×
670
         }
671
         print_syntax(")");
×
672
      }
673
      print_syntax(" #of ");
1✔
674
      dump_type(type_elem(type));
1✔
675
   }
676
   else if (type_is_record(type)) {
1✔
677
      print_syntax("#record\n");
1✔
678
      const int nfields = type_fields(type);
1✔
679
      for (int i = 0; i < nfields; i++)
3✔
680
         dump_decl(type_field(type, i), indent + 2);
2✔
681
      tab(indent);
1✔
682
      print_syntax("#end #record");
1✔
683
   }
684
   else if (kind == T_ENUM) {
×
685
      print_syntax("(");
×
686
      for (unsigned i = 0; i < type_enum_literals(type); i++) {
×
687
         if (i > 0) print_syntax(", ");
×
688
         print_syntax("%s", istr(tree_ident(type_enum_literal(type, i))));
×
689
      }
690
      print_syntax(")");
×
691
   }
692
   else if (kind == T_INCOMPLETE)
×
693
      ;
694
   else
695
      dump_type(type);
×
696

697
   print_syntax(";\n");
2✔
698
}
699

700
static void dump_subtype_decl(tree_t t, int indent)
701
{
702
   type_t type = tree_type(t);
703

704
   print_syntax("#subtype %s #is ", istr(tree_ident(t)));
705
   if (type_has_resolution(type)) {
706
      dump_expr(type_resolution(type));
707
      print_syntax(" ");
708
   }
709
   print_syntax("%s", type_pp(type_base(type)));
710

711
   const int ncon = type_constraints(type);
712
   for (int i = 0; i < ncon; i++)
713
      dump_constraint(type_constraint(type, i));
714

715
   dump_elem_constraints(type);
716

717
   print_syntax(";\n");
718
}
719

720
static void dump_component(tree_t t, int indent)
1✔
721
{
722
   print_syntax("#component %s #is\n", istr(tree_ident(t)));
1✔
723
   dump_generics(t, indent + 2, ";\n");
1✔
724
   dump_ports(t, indent + 2);
1✔
725
   print_syntax("#end #component;\n");
1✔
726
}
1✔
727

728
static void dump_use(tree_t t)
6✔
729
{
730
   print_syntax("#use %s", istr(tree_ident(t)));
6✔
731
   if (tree_has_ident2(t))
6✔
732
      print_syntax(".%s", istr(tree_ident2(t)));
6✔
733
   print_syntax(";\n");
6✔
734
}
6✔
735

736
static void dump_decl(tree_t t, int indent)
25✔
737
{
738
   tab(indent);
25✔
739
   if (tree_kind(t) != T_HIER) dump_address(t);
25✔
740

741
   switch (tree_kind(t)) {
25✔
742
   case T_IMPLICIT_SIGNAL:
×
743
      print_syntax("/* implicit */ ");
×
744
      // Fall-through
745
   case T_SIGNAL_DECL:
3✔
746
      print_syntax("#signal %s : ", istr(tree_ident(t)));
3✔
747
      break;
3✔
748

749
   case T_VAR_DECL:
3✔
750
      print_syntax("#variable %s : ", istr(tree_ident(t)));
3✔
751
      break;
3✔
752

753
   case T_CONST_DECL:
2✔
754
      print_syntax("#constant %s : ", istr(tree_ident(t)));
2✔
755
      break;
2✔
756

757
   case T_GENERIC_DECL:
1✔
758
      // Loop variable in for-generate statement
759
      print_syntax("/* loop variable */ %s : ", istr(tree_ident(t)));
1✔
760
      break;
1✔
761

762
   case T_FIELD_DECL:
2✔
763
      print_syntax("%s : ", istr(tree_ident(t)));
2✔
764
      break;
2✔
765

766
   case T_TYPE_DECL:
2✔
767
      dump_type_decl(t, indent);
2✔
768
      return;
2✔
769

770
   case T_SUBTYPE_DECL:
1✔
771
      dump_subtype_decl(t, indent);
1✔
772
      return;
1✔
773

774
   case T_SPEC:
×
775
      print_syntax("#for %s", istr(tree_ident(t)));
×
776
      if (tree_has_ref(t))
×
777
         print_syntax(" : %s", istr(tree_ident(tree_ref(t))));
×
778
      print_syntax("\n");
×
779
      tab(indent + 2);
×
780
      dump_binding(tree_value(t), indent + 2);
×
781
      dump_decls(t, indent + 2);
×
782
      tab(indent);
×
783
      print_syntax("#end #for;\n");
×
784
      return;
×
785

786
   case T_BLOCK_CONFIG:
×
787
      print_syntax("#for %s\n", istr(tree_ident(t)));
×
788
      dump_decls(t, indent + 2);
×
789
      tab(indent);
×
790
      print_syntax("#end #for;\n");
×
791
      return;
×
792

793
   case T_ENUM_LIT:
×
794
      print_syntax("%s", istr(tree_ident(t)));
×
795
      return;
×
796

797
   case T_ALIAS:
×
798
      print_syntax("#alias %s ", istr(tree_ident(t)));
×
799
      if (tree_has_type(t)) {
×
800
         print_syntax(": ");
×
801
         dump_type(tree_type(t));
×
802
      }
803
      print_syntax(" #is ");
×
804
      dump_expr(tree_value(t));
×
805
      print_syntax(";\n");
×
806
      return;
×
807

808
   case T_ATTR_SPEC:
1✔
809
      print_syntax("#attribute %s #of %s : #%s #is ", istr(tree_ident(t)),
1✔
810
             istr(tree_ident2(t)), class_str(tree_class(t)));
811
      dump_expr(tree_value(t));
1✔
812
      print_syntax(";\n");
1✔
813
      return;
1✔
814

815
   case T_ATTR_DECL:
1✔
816
      print_syntax("#attribute %s : ", istr(tree_ident(t)));
1✔
817
      dump_type(tree_type(t));
1✔
818
      print_syntax(";\n");
1✔
819
      return;
1✔
820

821
   case T_FUNC_DECL:
2✔
822
      if (tree_flags(t) & TREE_F_PREDEFINED)
2✔
823
         print_syntax("-- predefined %s\n", type_pp(tree_type(t)));
×
824
      else {
825
         print_syntax("#function %s", istr(tree_ident(t)));
2✔
826
         dump_generics(t, indent + 2, "");
2✔
827
         dump_arguments(t, indent, "");
2✔
828
         print_syntax(" #return ");
2✔
829
         dump_type(type_result(tree_type(t)));
2✔
830
         print_syntax(";\n");
2✔
831
         if (tree_has_ident2(t)) {
2✔
832
            tab(indent + 2);
2✔
833
            print_syntax("-- %s\n", istr(tree_ident2(t)));
2✔
834
         }
835
      }
836
      return;
837

838
   case T_FUNC_INST:
2✔
839
   case T_FUNC_BODY:
840
      print_syntax("#function %s", istr(tree_ident(t)));
2✔
841
      dump_type_hint(t);
2✔
842
      dump_generics(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
3✔
843
      if (tree_kind(t) == T_FUNC_INST)
2✔
844
         dump_generic_map(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
×
845
      dump_arguments(t, indent, "");
2✔
846
      print_syntax(" #return ");
2✔
847
      dump_type(type_result(tree_type(t)));
2✔
848
      print_syntax(" #is\n");
2✔
849
      if (tree_has_ident2(t)) {
2✔
850
         tab(indent + 2);
2✔
851
         print_syntax("-- %s\n", istr(tree_ident2(t)));
2✔
852
      }
853
      dump_block(t, indent);
2✔
854
      tab(indent);
2✔
855
      print_syntax("#end #function;\n");
2✔
856
      return;
2✔
857

858
   case T_PROC_DECL:
×
859
      if (tree_flags(t) & TREE_F_PREDEFINED)
×
860
         print_syntax("-- predefined %s\n", type_pp(tree_type(t)));
×
861
      else {
862
         print_syntax("#procedure %s", istr(tree_ident(t)));
×
863
         dump_generics(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
×
864
         dump_arguments(t, indent, "");
×
865
         print_syntax(";");
×
866
         dump_wait_level(t);
×
867
         print_syntax("\n");
×
868
         if (tree_has_ident2(t)) {
×
869
            tab(indent + 2);
×
870
            print_syntax("-- %s\n", istr(tree_ident2(t)));
×
871
         }
872
      }
873
      return;
874

875
   case T_PROC_INST:
2✔
876
   case T_PROC_BODY:
877
      print_syntax("#procedure %s", istr(tree_ident(t)));
2✔
878
      dump_type_hint(t);
2✔
879
      dump_generics(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
3✔
880
      if (tree_kind(t) == T_PROC_INST)
2✔
881
         dump_generic_map(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
×
882
      dump_arguments(t, indent, "");
2✔
883
      print_syntax(" #is");
2✔
884
      dump_wait_level(t);
2✔
885
      print_syntax("\n");
2✔
886
      if (tree_has_ident2(t)) {
2✔
887
         tab(indent + 2);
2✔
888
         print_syntax("-- %s\n", istr(tree_ident2(t)));
2✔
889
      }
890
      dump_block(t, indent);
2✔
891
      tab(indent);
2✔
892
      print_syntax("#end #procedure;\n");
2✔
893
      return;
2✔
894

895
   case T_HIER:
×
896
      {
897
         const char *kind = "Scope";
×
898
         switch (tree_subkind(t)) {
×
899
         case T_ARCH: kind = "Instance"; break;
×
900
         case T_IF_GENERATE: kind = "If generate"; break;
×
901
         case T_FOR_GENERATE: kind = "For generate"; break;
×
902
         case T_BLOCK: kind = "Block"; break;
×
903
         }
904
         print_syntax("-- %s %s\n", kind, istr(tree_ident2(t)));
×
905
      }
906
      return;
×
907

908
   case T_COMPONENT:
1✔
909
      dump_component(t, indent);
1✔
910
      return;
1✔
911

912
   case T_PROT_DECL:
1✔
913
      print_syntax("#type %s #is #protected\n", istr(tree_ident(t)));
1✔
914
      dump_decls(t, indent + 2);
1✔
915
      tab(indent);
1✔
916
      print_syntax("#end #protected;\n");
1✔
917
      return;
1✔
918

919
   case T_PROT_BODY:
1✔
920
      print_syntax("#type %s #is #protected #body\n", istr(tree_ident(t)));
1✔
921
      dump_decls(t, indent + 2);
1✔
922
      tab(indent);
1✔
923
      print_syntax("#end #protected #body;\n");
1✔
924
      return;
1✔
925

926
   case T_FILE_DECL:
×
927
      print_syntax("#file %s : ", istr(tree_ident(t)));
×
928
      dump_type(tree_type(t));
×
929
      if (tree_has_value(t)) {
×
930
         print_syntax(" #open ");
×
931
         dump_expr(tree_file_mode(t));
×
932
         print_syntax(" #is ");
×
933
         dump_expr(tree_value(t));
×
934
      }
935
      print_syntax(";\n");
×
936
      return;
×
937

938
   case T_USE:
×
939
      dump_use(t);
×
940
      return;
×
941

942
   case T_PACKAGE:
×
943
   case T_PACK_INST:
944
      dump_package(t, indent);
×
945
      return;
×
946

947
   case T_PACK_BODY:
×
948
      dump_package_body(t, indent);
×
949
      return;
×
950

951
   default:
×
952
      cannot_dump(t, "decl");
×
953
   }
954

955
   dump_type(tree_type(t));
11✔
956

957
   if (tree_kind(t) != T_FIELD_DECL && tree_has_value(t)) {
11✔
958
      print_syntax(" := ");
5✔
959
      dump_expr(tree_value(t));
5✔
960
   }
961
   print_syntax(";\n");
11✔
962
}
963

964
static void dump_waveforms(tree_t t)
2✔
965
{
966
   const int nwaves = tree_waveforms(t);
2✔
967
   for (int i = 0; i < nwaves; i++) {
5✔
968
      if (i > 0) print_syntax(", ");
3✔
969
      dump_waveform(tree_waveform(t, i));
3✔
970
   }
971
}
2✔
972

973
static void dump_alternative(tree_t t, int indent)
×
974
{
975
   tab(indent);
×
976
   print_syntax("#when ");
×
977
   if (tree_has_ident(t))
×
978
      print_syntax("%s: ", istr(tree_ident(t)));
×
979
   for (unsigned i = 0; i < tree_assocs(t); i++) {
×
980
      if (i > 0) print_syntax("| ");
×
981
      tree_t a = tree_assoc(t, i);
×
982
      switch (tree_subkind(a)) {
×
983
      case A_NAMED:
×
984
         dump_expr(tree_name(a));
×
985
         break;
×
986
      case A_OTHERS:
×
987
         print_syntax("#others");
×
988
         break;
×
989
      case A_RANGE:
×
990
         dump_range(tree_range(a, 0));
×
991
         break;
×
992
      }
993
   }
×
994
   print_syntax(" =>\n");
×
995
   if (tree_decls(t) > 0) {
×
996
      dump_decls(t, indent + 4);
×
997
      tab(indent + 2);
×
998
      print_syntax("#begin\n");
×
999
      dump_stmts(t, indent + 4);
×
1000
      tab(indent + 2);
×
1001
      print_syntax("#end;\n");
×
1002
   }
1003
   else
1004
      dump_stmts(t, indent + 2);
×
1005
}
×
1006

1007
static void dump_psl(tree_t t, int indent)
1008
{
1009
   if (standard() < STD_08)
1010
      print_syntax("-- psl ");
1011
   psl_dump(tree_psl(t));
1012
}
1013

1014
static void dump_instance(tree_t t, int indent)
1✔
1015
{
1016
   switch (tree_class(t)) {
1✔
1017
   case C_ENTITY:        print_syntax("#entity "); break;
×
1018
   case C_COMPONENT:     print_syntax("#component "); break;
1✔
1019
   case C_CONFIGURATION: print_syntax("#configuration "); break;
×
1020
   default: break;
1021
   }
1022

1023
   print_syntax("%s", istr(tree_ident2(t)));
1✔
1024

1025
   const int nparams = tree_params(t);
1✔
1026
   const int ngenmaps = tree_genmaps(t);
1✔
1027

1028
   if (tree_has_spec(t)) {
1✔
1029
      tree_t spec = tree_spec(t);
×
1030
      if (tree_has_value(spec)) {
×
1031
         tree_t bind = tree_value(spec);
×
1032
         LOCAL_TEXT_BUF tb = tb_new();
×
1033
         tb_cat(tb, istr(tree_ident(bind)));
×
1034
         if (tree_has_ident2(bind))
×
1035
            tb_printf(tb, "(%s)", istr(tree_ident2(bind)));
×
1036
         print_syntax("  -- bound to %s\n", tb_get(tb));
×
1037
      }
1038
   }
1039
   else if (nparams > 0 || ngenmaps > 0)
1✔
1040
      print_syntax("\n");
1✔
1041

1042
   dump_generic_map(t, indent + 2, nparams > 0 ? "\n" : "");
1✔
1043
   dump_port_map(t, indent + 2, "");
1✔
1044
   print_syntax(";\n");
1✔
1045
}
1✔
1046

1047
static void dump_stmt(tree_t t, int indent)
13✔
1048
{
1049
   tab(indent);
15✔
1050

1051
   if (tree_has_ident(t)) {
15✔
1052
      const char *label = istr(tree_ident(t));
6✔
1053
#if !DUMP_GEN_NAMES
1054
      if (label[0] != '_')   // Skip generated labels
6✔
1055
#endif
1056
         print_syntax("%s: ", label);
4✔
1057
   }
1058

1059
   switch (tree_kind(t)) {
15✔
1060
   case T_PROCESS:
1✔
1061
      dump_address(t);
1✔
1062
      if (tree_flags(t) & TREE_F_POSTPONED)
1✔
1063
         print_syntax("#postponed ");
×
1064
      print_syntax("#process ");
1✔
1065
      if (tree_triggers(t) > 0) {
1✔
1066
         print_syntax("(");
1✔
1067
         for (unsigned i = 0; i < tree_triggers(t); i++) {
2✔
1068
            if (i > 0)
1✔
1069
               print_syntax(", ");
×
1070
            dump_expr(tree_trigger(t, i));
1✔
1071
         }
1072
         print_syntax(") ");
1✔
1073
      }
1074
      print_syntax("#is\n");
1✔
1075
      dump_decls(t, indent + 2);
1✔
1076
      tab(indent);
1✔
1077
      print_syntax("#begin\n");
1✔
1078
      dump_stmts(t, indent + 2);
1✔
1079
      tab(indent);
1✔
1080
      print_syntax("#end #process;\n");
1✔
1081
      return;
1✔
1082

1083
   case T_SIGNAL_ASSIGN:
1✔
1084
      dump_expr(tree_target(t));
1✔
1085
      print_syntax(" <= #reject ");
1✔
1086
      if (tree_has_reject(t))
1✔
1087
         dump_expr(tree_reject(t));
1✔
1088
      else
1089
         print_syntax("0 ps");
×
1090
      print_syntax(" #inertial ");
1✔
1091
      dump_waveforms(t);
1✔
1092
      break;
1✔
1093

1094
   case T_FORCE:
×
1095
      dump_expr(tree_target(t));
×
1096
      print_syntax(" <= #force ");
×
1097
      dump_expr(tree_value(t));
×
1098
      break;
×
1099

1100
   case T_RELEASE:
×
1101
      dump_expr(tree_target(t));
×
1102
      print_syntax(" <= #release");
×
1103
      break;
×
1104

1105
   case T_VAR_ASSIGN:
2✔
1106
      dump_expr(tree_target(t));
2✔
1107
      print_syntax(" := ");
2✔
1108
      dump_expr(tree_value(t));
2✔
1109
      break;
2✔
1110

1111
   case T_WAIT:
×
1112
      print_syntax("#wait");
×
1113
      if (tree_triggers(t) > 0) {
×
1114
         print_syntax(" #on ");
×
1115
         for (unsigned i = 0; i < tree_triggers(t); i++) {
×
1116
            if (i > 0)
×
1117
               print_syntax(", ");
×
1118
            dump_expr(tree_trigger(t, i));
×
1119
         }
1120
      }
1121
      if (tree_has_value(t)) {
×
1122
         print_syntax(" #until ");
×
1123
         dump_expr(tree_value(t));
×
1124
      }
1125
      if (tree_has_delay(t)) {
×
1126
         print_syntax(" #for ");
×
1127
         dump_expr(tree_delay(t));
×
1128
      }
1129
      print_syntax(";");
×
1130
      if (tree_flags(t) & TREE_F_STATIC_WAIT)
×
1131
         print_syntax("   -- static");
×
1132
      print_syntax("\n");
×
1133
      return;
×
1134

1135
   case T_BLOCK:
1✔
1136
      dump_address(t);
1✔
1137
      print_syntax("#block #is\n");
1✔
1138
      dump_generics(t, indent + 2, ";\n");
1✔
1139
      dump_generic_map(t, indent + 2, ";\n");
1✔
1140
      dump_ports(t, indent + 2);
1✔
1141
      dump_port_map(t, indent + 2, ";\n");
1✔
1142
      dump_block(t, indent);
1✔
1143
      tab(indent);
1✔
1144
      print_syntax("#end #block;\n");
1✔
1145
      return;
1✔
1146

1147
   case T_SEQUENCE:
×
1148
      print_syntax("#block #is\n");
×
1149
      dump_block(t, indent);
×
1150
      tab(indent);
×
1151
      print_syntax("#end #block;\n");
×
1152
      return;
×
1153

1154
   case T_ASSERT:
1✔
1155
      if (tree_has_value(t)) {
1✔
1156
         print_syntax("#assert ");
1✔
1157
         dump_expr(tree_value(t));
1✔
1158
         print_syntax(" ");
1✔
1159
      }
1160
      if (tree_has_message(t)) {
1✔
1161
         print_syntax("#report ");
×
1162
         dump_expr(tree_message(t));
×
1163
         print_syntax(" ");
×
1164
      }
1165
      print_syntax("#severity ");
1✔
1166
      dump_expr(tree_severity(t));
1✔
1167
      break;
1✔
1168

1169
   case T_WHILE:
×
1170
      if (tree_has_value(t)) {
×
1171
         print_syntax("#while ");
×
1172
         dump_expr(tree_value(t));
×
1173
         print_syntax(" ");
×
1174
      }
1175
      print_syntax("#loop\n");
×
1176
      dump_stmts(t, indent + 2);
×
1177
      tab(indent);
×
1178
      print_syntax("#end #loop");
×
1179
      break;
×
1180

1181
   case T_IF:
1182
      for (unsigned i = 0; i < tree_conds(t); i++) {
3✔
1183
         tree_t c = tree_cond(t, i);
2✔
1184
         if (tree_has_value(c)) {
2✔
1185
            if (i > 0)
1✔
1186
               tab(indent);
×
1187
            print_syntax(i > 0 ? "#elsif " : "#if ");
2✔
1188
            dump_expr(tree_value(c));
1✔
1189
            print_syntax(" #then\n");
1✔
1190
         }
1191
         else {
1192
            tab(indent);
1✔
1193
            print_syntax("#else\n");
1✔
1194
         }
1195
         dump_stmts(c, indent + 2);
2✔
1196
      }
1197
      tab(indent);
1✔
1198
      print_syntax("#end #if");
1✔
1199
      break;
1✔
1200

1201
   case T_EXIT:
×
1202
      print_syntax("#exit %s", istr(tree_ident2(t)));
×
1203
      if (tree_has_value(t)) {
×
1204
         print_syntax(" #when ");
×
1205
         dump_expr(tree_value(t));
×
1206
      }
1207
      break;
1208

1209
   case T_CASE:
×
1210
   case T_MATCH_CASE:
1211
      {
1212
         const char *suffix = tree_kind(t) == T_MATCH_CASE ? "?" : "";
×
1213
         print_syntax("#case%s ", suffix);
×
1214
         dump_expr(tree_value(t));
×
1215
         print_syntax(" #is\n");
×
1216
         const int nstmts = tree_stmts(t);
×
1217
         for (int i = 0; i < nstmts; i++)
×
1218
            dump_alternative(tree_stmt(t, i), indent + 2);
×
1219
         tab(indent);
×
1220
         print_syntax("#end #case%s", suffix);
×
1221
      }
1222
      break;
×
1223

1224
   case T_RETURN:
3✔
1225
      print_syntax("#return");
3✔
1226
      if (tree_has_value(t)) {
3✔
1227
         print_syntax(" ");
3✔
1228
         dump_expr(tree_value(t));
3✔
1229
      }
1230
      break;
1231

1232
   case T_COND_RETURN:
×
1233
      print_syntax("#return #when ");
×
1234
      dump_expr(tree_value(t));
×
1235
      break;
×
1236

1237
   case T_FOR:
×
1238
      print_syntax("#for %s #in ", istr(tree_ident(tree_decl(t, 0))));
×
1239
      dump_range(tree_range(t, 0));
×
1240
      print_syntax(" #loop\n");
×
1241
      dump_stmts(t, indent + 2);
×
1242
      tab(indent);
×
1243
      print_syntax("#end #for");
×
1244
      break;
×
1245

1246
   case T_PROT_PCALL:
×
1247
      dump_expr(tree_name(t));
×
1248
      print_syntax(".");
×
1249
      // Fall-through
1250
   case T_PCALL:
×
1251
      if (tree_has_ref(t)) {
×
1252
         tree_t decl = tree_ref(t);
×
1253
         dump_address(decl);
×
1254
         print_syntax("%s", istr(tree_ident(decl)));
×
1255
      }
1256
      else
1257
         print_syntax("%s", istr(tree_ident(t)));
×
1258
      dump_params(t, tree_param, tree_params(t), NULL);
×
1259
      break;
×
1260

1261
   case T_FOR_GENERATE:
1✔
1262
      print_syntax("#for %s #in ", istr(tree_ident(tree_decl(t, 0))));
1✔
1263
      dump_range(tree_range(t, 0));
1✔
1264
      print_syntax(" #generate\n");
1✔
1265
      dump_decls(t, indent + 2);
1✔
1266
      tab(indent);
1✔
1267
      print_syntax("#begin\n");
1✔
1268
      dump_stmts(t, indent + 2);
1✔
1269
      tab(indent);
1✔
1270
      print_syntax("#end #generate");
1✔
1271
      break;
1✔
1272

1273
   case T_CASE_GENERATE:
×
1274
      print_syntax("#case ");
×
1275
      dump_expr(tree_value(t));
×
1276
      print_syntax(" #generate\n");
×
1277
      dump_stmts(t, indent + 2);
×
1278
      print_syntax("#end #generate");
×
1279
      break;
×
1280

1281
   case T_IF_GENERATE:
1282
      for (unsigned i = 0; i < tree_conds(t); i++) {
×
1283
         tree_t c = tree_cond(t, i);
×
1284
         if (tree_has_value(c)) {
×
1285
            if (i > 0)
×
1286
               tab(indent);
×
1287
            print_syntax(i > 0 ? "#elsif " : "#if ");
×
1288
            dump_expr(tree_value(c));
×
1289
            print_syntax(" #generate\n");
×
1290
         }
1291
         else {
1292
            tab(indent);
×
1293
            print_syntax("#else\n");
×
1294
         }
1295
         for (unsigned i = 0; i < tree_stmts(c); i++)
×
1296
            dump_stmt(tree_stmt(c, i), indent + 2);
×
1297
      }
1298
      tab(indent);
×
1299
      print_syntax("#end #generate");
×
1300
      break;
×
1301

1302
   case T_INSTANCE:
1✔
1303
      dump_instance(t, indent);
1✔
1304
      return;
1✔
1305

1306
   case T_NEXT:
×
1307
      print_syntax("#next");
×
1308
      if (tree_has_value(t)) {
×
1309
         print_syntax(" #when ");
×
1310
         dump_expr(tree_value(t));
×
1311
      }
1312
      break;
1313

1314
   case T_NULL:
×
1315
      print_syntax("#null");
×
1316
      break;
×
1317

1318
   case T_COND_ASSIGN:
1✔
1319
      dump_expr(tree_target(t));
1✔
1320
      print_syntax(" <= ");
1✔
1321
      for (int i = 0; i < tree_conds(t); i++) {
2✔
1322
         tree_t c = tree_cond(t, i);
1✔
1323
         dump_waveforms(tree_stmt(c, 0));
1✔
1324
         if (tree_has_value(c)) {
1✔
1325
            print_syntax(" #when ");
×
1326
            dump_expr(tree_value(c));
×
1327
         }
1328
      }
1329
      break;
1330

1331
   case T_SELECT:
×
1332
      print_syntax(" <= ");
×
1333
      if (tree_has_guard(t)) print_syntax("#guarded ");
×
1334
      color_printf("$red$/* TODO: T_SELECT */$$");
×
1335
      break;
×
1336

1337
   case T_CONCURRENT:
2✔
1338
      if (tree_flags(t) & TREE_F_POSTPONED)
2✔
1339
         print_syntax("#postponed ");
×
1340
      if (tree_has_guard(t))
2✔
1341
         print_syntax("#guarded ");
×
1342
      dump_stmt(tree_stmt(t, 0), 0);
2✔
1343
      return;
2✔
1344

1345
   case T_PSL:
×
1346
      dump_psl(t, 0);
×
1347
      break;
×
1348

1349
   case T_VERILOG:
×
1350
      print_syntax("#block #is\n");
×
1351
      dump_generic_map(t, indent + 2, ";\n");
×
1352
      dump_port_map(t, indent + 2, ";\n");
×
1353
      tab(indent);
×
1354
      print_syntax("#begin\n");
×
1355
      vlog_dump(tree_vlog(t), indent + 2);
×
1356
      tab(indent);
×
1357
      print_syntax("#end #block");
×
1358
      break;
×
1359

1360
   default:
×
1361
      cannot_dump(t, "stmt");
×
1362
   }
1363

1364
   print_syntax(";\n");
10✔
1365
}
1366

1367
static void dump_port(tree_t t, int indent)
100✔
1368
{
1369
   tab(indent);
100✔
1370
   dump_address(t);
100✔
1371

1372
   if (tree_flags(t) & TREE_F_PREDEFINED)
100✔
1373
      print_syntax("-- predefined ");
80✔
1374

1375
   const class_t class = tree_class(t);
100✔
1376
   print_syntax("#%s %s", class_str(class), istr(tree_ident(t)));
100✔
1377

1378
   type_t type = get_type_or_null(t);
100✔
1379
   if (class == C_PACKAGE) {
100✔
1380
      print_syntax(" #is #new ");
×
1381
      dump_expr(tree_value(t));
×
1382
   }
1383
   else if (class == C_TYPE && type_kind(type) == T_GENERIC) {
100✔
1384
      print_syntax(" #is ");
10✔
1385

1386
      switch (type_subkind(type)) {
10✔
1387
      case GTYPE_PRIVATE:
2✔
1388
         print_syntax("#private");
2✔
1389
         break;
2✔
1390
      case GTYPE_SCALAR:
1✔
1391
         print_syntax("<>");
1✔
1392
         break;
1✔
1393
      case GTYPE_DISCRETE:
1✔
1394
         print_syntax("(<>)");
1✔
1395
         break;
1✔
1396
      case GTYPE_INTEGER:
1✔
1397
         print_syntax("#range <>");
1✔
1398
         break;
1✔
1399
      case GTYPE_PHYSICAL:
1✔
1400
         print_syntax("#units <>");
1✔
1401
         break;
1✔
1402
      case GTYPE_FLOATING:
1✔
1403
         print_syntax("#range <> . <>");
1✔
1404
         break;
1✔
1405
      case GTYPE_ARRAY:
1✔
1406
         {
1407
            print_syntax("#array (");
1✔
1408
            const int nindex = type_indexes(type);
1✔
1409
            for (int i = 0; i < nindex; i++) {
2✔
1410
               if (i > 0) print_syntax(", ");
1✔
1411
               dump_type(type_index(type, i));
1✔
1412
               print_syntax(" #range <>");
1✔
1413
            }
1414
            print_syntax(") #of ");
1✔
1415
            dump_type(type_elem(type));
1✔
1416
         }
1417
         break;
1✔
1418
      case GTYPE_ACCESS:
1✔
1419
         print_syntax("#access ..");
1✔
1420
         break;
1✔
1421
      case GTYPE_FILE:
1✔
1422
         print_syntax("#file #of ..");
1✔
1423
         break;
1✔
1424
      }
1425
   }
1426
   else {
1427
      const char *dir = NULL;
90✔
1428
      switch (tree_subkind(t)) {
90✔
1429
      case PORT_IN:      dir = "in";     break;
86✔
1430
      case PORT_OUT:     dir = "out";    break;
4✔
1431
      case PORT_INOUT:   dir = "inout";  break;
×
1432
      case PORT_BUFFER:  dir = "buffer"; break;
×
1433
      case PORT_INVALID: dir = "??";     break;
×
1434
      }
1435
      print_syntax(" : #%s ", dir);
90✔
1436
      dump_type(type);
90✔
1437

1438
      if (tree_has_value(t)) {
90✔
1439
         print_syntax(" := ");
79✔
1440
         dump_expr(tree_value(t));
79✔
1441
      }
1442
   }
1443
}
100✔
1444

1445
static void dump_context(tree_t t, int indent)
6✔
1446
{
1447
   const int nctx = tree_contexts(t);
6✔
1448
   for (int i = 0; i < nctx; i++) {
24✔
1449
      tree_t c = tree_context(t, i);
18✔
1450
      switch (tree_kind(c)) {
18✔
1451
      case T_LIBRARY:
12✔
1452
         switch (is_well_known(tree_ident(c))) {
12✔
1453
         case W_STD:
1454
         case W_WORK:
1455
            break;
1456
         default:
×
1457
            print_syntax("#library %s;\n", istr(tree_ident(c)));
×
1458
         }
1459
         break;
1460

1461
      case T_USE:
6✔
1462
         dump_use(c);
6✔
1463
         break;
6✔
1464

1465
      case T_CONTEXT_REF:
×
1466
         print_syntax("#context %s;\n", istr(tree_ident(t)));
×
1467
         break;
×
1468

1469
      default:
1470
         break;
1471
      }
1472

1473
      tab(indent);
18✔
1474
   }
1475

1476
   if (nctx > 0) {
6✔
1477
      print_syntax("\n");
6✔
1478
      tab(indent);
6✔
1479
   }
1480
}
6✔
1481

1482
static void dump_elab(tree_t t)
×
1483
{
1484
   dump_context(t, 0);
×
1485
   dump_address(t);
×
1486
   print_syntax("#entity %s #is\n#end #entity;\n\n", istr(tree_ident(t)));
×
1487
   print_syntax("#architecture #elab #of %s #is\n", istr(tree_ident(t)));
×
1488
   dump_decls(t, 2);
×
1489
   print_syntax("#begin\n");
×
1490
   for (unsigned i = 0; i < tree_stmts(t); i++)
×
1491
      dump_stmt(tree_stmt(t, i), 2);
×
1492
   print_syntax("#end #architecture;\n\n");
×
1493
}
×
1494

1495
static void dump_entity(tree_t t)
2✔
1496
{
1497
   dump_context(t, 0);
2✔
1498
   dump_address(t);
2✔
1499
   print_syntax("#entity %s #is\n", istr(tree_ident(t)));
2✔
1500
   dump_generics(t, 2, ";\n");
2✔
1501
   dump_ports(t, 2);
2✔
1502
   dump_decls(t, 2);
2✔
1503
   if (tree_stmts(t) > 0) {
2✔
1504
      print_syntax("#begin\n");
×
1505
      for (unsigned i = 0; i < tree_stmts(t); i++) {
×
1506
         dump_stmt(tree_stmt(t, i), 2);
×
1507
      }
1508
   }
1509
   print_syntax("#end #entity;\n\n");
2✔
1510
}
2✔
1511

1512
static void dump_decls(tree_t t, int indent)
15✔
1513
{
1514
   const int ndecls = tree_decls(t);
15✔
1515
   bool was_body = false;
15✔
1516
   for (unsigned i = 0; i < ndecls; i++) {
30✔
1517
      tree_t d = tree_decl(t, i);
15✔
1518
      tree_kind_t dkind = tree_kind(d);
15✔
1519
      const bool is_body = dkind == T_FUNC_BODY || dkind == T_PROT_BODY
30✔
1520
         || dkind == T_PROC_BODY;
15✔
1521
      if ((was_body && !is_body) || (is_body && i > 0))
15✔
1522
         print_syntax("\n");
4✔
1523
      was_body = is_body;
15✔
1524
      dump_decl(d, indent);
15✔
1525
   }
1526
}
15✔
1527

1528
static void dump_arch(tree_t t)
1✔
1529
{
1530
   dump_context(t, 0);
1✔
1531
   dump_address(t);
1✔
1532
   print_syntax("#architecture %s #of %s #is\n",
1✔
1533
                istr(tree_ident(t)), istr(tree_ident2(t)));
1534
   dump_decls(t, 2);
1✔
1535
   print_syntax("#begin\n");
1✔
1536
   dump_stmts(t, 2);
1✔
1537
   print_syntax("#end #architecture;\n\n");
1✔
1538
}
1✔
1539

1540
static void dump_package(tree_t t, int indent)
1✔
1541
{
1542
   dump_context(t, indent);
1✔
1543
   dump_address(t);
1✔
1544
   print_syntax("#package %s #is\n", istr(tree_ident(t)));
1✔
1545
   if (tree_kind(t) == T_PACK_INST && tree_has_ref(t)) {
1✔
1546
      tab(indent);
×
1547
      print_syntax("  -- Instantiated from %s\n", istr(tree_ident(tree_ref(t))));
×
1548
   }
1549
   dump_generics(t, indent + 2, ";\n");
1✔
1550
   dump_generic_map(t, indent + 2, ";\n");
1✔
1551
   dump_decls(t, indent + 2);
1✔
1552
   tab(indent);
1✔
1553
   print_syntax("#end #package;\n\n");
1✔
1554
}
1✔
1555

1556
static void dump_package_body(tree_t t, int indent)
2✔
1557
{
1558
   dump_context(t, indent);
2✔
1559
   dump_address(t);
2✔
1560
   print_syntax("#package #body %s #is\n", istr(tree_ident(t)));
2✔
1561
   dump_decls(t, indent + 2);
2✔
1562
   tab(indent);
2✔
1563
   print_syntax("#end #package #body;\n\n");
2✔
1564
}
2✔
1565

1566
static void dump_configuration(tree_t t)
×
1567
{
1568
   dump_address(t);
×
1569
   print_syntax("#configuration %s #of %s #is\n",
×
1570
          istr(tree_ident(t)), istr(tree_ident2(t)));
1571
   dump_decls(t, 2);
×
1572
   print_syntax("#end #configuration\n");
×
1573
}
×
1574

1575
void vhdl_dump(tree_t t, int indent)
29✔
1576
{
1577
   switch (tree_kind(t)) {
29✔
1578
   case T_ELAB:
×
1579
      dump_elab(t);
×
1580
      break;
×
1581
   case T_ENTITY:
2✔
1582
      dump_entity(t);
2✔
1583
      break;
2✔
1584
   case T_ARCH:
1✔
1585
      dump_arch(t);
1✔
1586
      break;
1✔
1587
   case T_PACKAGE:
1✔
1588
   case T_PACK_INST:
1589
      dump_package(t, indent);
1✔
1590
      break;
1✔
1591
   case T_PACK_BODY:
2✔
1592
      dump_package_body(t, indent);
2✔
1593
      break;
2✔
1594
   case T_CONFIGURATION:
×
1595
      dump_configuration(t);
×
1596
      break;
×
1597
   case T_REF:
10✔
1598
   case T_FCALL:
1599
   case T_PROT_FCALL:
1600
   case T_LITERAL:
1601
   case T_AGGREGATE:
1602
   case T_ARRAY_REF:
1603
   case T_ARRAY_SLICE:
1604
   case T_TYPE_CONV:
1605
   case T_RECORD_REF:
1606
   case T_ATTR_REF:
1607
   case T_CONV_FUNC:
1608
   case T_QUALIFIED:
1609
   case T_EXTERNAL_NAME:
1610
   case T_STRING:
1611
   case T_PACKAGE_MAP:
1612
      dump_expr(t);
10✔
1613
      break;
10✔
1614
   case T_INSTANCE:
4✔
1615
   case T_FOR_GENERATE:
1616
   case T_CASE_GENERATE:
1617
   case T_BLOCK:
1618
   case T_PROCESS:
1619
   case T_CASE:
1620
   case T_FOR:
1621
   case T_SIGNAL_ASSIGN:
1622
   case T_IF:
1623
   case T_WAIT:
1624
   case T_PSL:
1625
   case T_VAR_ASSIGN:
1626
   case T_RETURN:
1627
      dump_stmt(t, indent);
4✔
1628
      break;
4✔
1629
   case T_CONST_DECL:
8✔
1630
   case T_VAR_DECL:
1631
   case T_SIGNAL_DECL:
1632
   case T_TYPE_DECL:
1633
   case T_FIELD_DECL:
1634
   case T_FUNC_DECL:
1635
   case T_PROC_BODY:
1636
   case T_FUNC_BODY:
1637
   case T_PROC_DECL:
1638
   case T_ATTR_DECL:
1639
   case T_ATTR_SPEC:
1640
   case T_ENUM_LIT:
1641
   case T_COMPONENT:
1642
   case T_BLOCK_CONFIG:
1643
   case T_SPEC:
1644
   case T_ALIAS:
1645
   case T_FUNC_INST:
1646
   case T_PROC_INST:
1647
   case T_SUBTYPE_DECL:
1648
      dump_decl(t, indent);
8✔
1649
      break;
8✔
1650
   case T_PORT_DECL:
×
1651
   case T_GENERIC_DECL:
1652
      dump_port(t, indent);
×
1653
      break;
×
1654
   case T_RANGE:
1✔
1655
      dump_range(t);
1✔
1656
      break;
1✔
1657
   case T_BINDING:
×
1658
      dump_binding(t, indent);
×
1659
      break;
×
1660
   case T_PARAM:
×
1661
      dump_param(t);
×
1662
      break;
×
1663
   case T_CONSTRAINT:
×
1664
      dump_constraint(t);
×
1665
      break;
×
1666
   case T_ELEM_CONSTRAINT:
×
1667
      dump_record_elem_constraint(t);
×
1668
      break;
×
1669
   case T_ALTERNATIVE:
×
1670
      dump_alternative(t, indent);
×
1671
      break;
×
1672
   default:
×
1673
      cannot_dump(t, "tree");
×
1674
   }
1675
}
29✔
1676

1677
void dump(tree_t t)
18✔
1678
{
1679
   vhdl_dump(t, 0);
18✔
1680
   print_syntax("\r");
18✔
1681
}
18✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc