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

nickg / nvc / 14317268613

07 Apr 2025 06:56PM UTC coverage: 92.316% (-0.01%) from 92.326%
14317268613

push

github

nickg
Rewrite all concatenations as aggregates

44 of 48 new or added lines in 3 files covered. (91.67%)

221 existing lines in 9 files now uncovered.

68756 of 74479 relevant lines covered (92.32%)

425237.07 hits per line

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

59.49
/src/dump.c
1
//
2
//  Copyright (C) 2011-2024  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-node.h"
27
#include "vlog/vlog-phase.h"
28

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

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

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

51
typedef tree_t (*get_fn_t)(tree_t, unsigned);
52

53
static void tab(int indent)
236✔
54
{
55
   print_syntax("%*s", indent, "");
236✔
56
}
236✔
57

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

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

78
static void dump_params(tree_t t, get_fn_t get, int n, const char *prefix)
18✔
79
{
80
   if (n > 0) {
18✔
81
      if (prefix != NULL) {
17✔
82
         print_syntax(prefix, "");
4✔
83
         print_syntax(" ");
4✔
84
      }
85
      print_syntax("(");
17✔
86
      for (int i = 0; i < n; i++) {
49✔
87
         if (i > 0)
32✔
88
            print_syntax(", ");
15✔
89
         dump_param((*get)(t, i));
32✔
90
      }
91
      print_syntax(")");
17✔
92
   }
93
}
18✔
94

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

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

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

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

143
   const int color = 16 + 36*r + 6*g + b;
144

145
   char *LOCAL fmt = xasprintf("$!#%d${%%p:%s}$$", color,
146
                               tree_kind_str(tree_kind(t)));
147
   color_printf(fmt, t);
148
#endif
149
}
150

151
static void dump_waveform(tree_t w)
4✔
152
{
153
   if (tree_has_value(w))
4✔
154
      dump_expr(tree_value(w));
4✔
155
   else
156
      print_syntax("#null");
×
157

158
   if (tree_has_delay(w)) {
4✔
159
      print_syntax(" #after ");
3✔
160
      dump_expr(tree_delay(w));
3✔
161
   }
162
}
4✔
163

164
static void dump_external_name(tree_t t)
2✔
165
{
166
   print_syntax("<< #%s ", class_str(tree_class(t)));
2✔
167
   const int nparts = tree_parts(t);
2✔
168
   for (int i = 0; i < nparts; i++) {
8✔
169
      tree_t part = tree_part(t, i);
6✔
170
      switch (tree_subkind(part)) {
6✔
171
      case PE_ABSOLUTE:
1✔
172
         print_syntax(".");
1✔
173
         break;
1✔
174
      case PE_SIMPLE:
3✔
175
         print_syntax("%s%s", istr(tree_ident(part)),
3✔
176
                      i + 1 < nparts ? "." : "");
3✔
177
         break;
3✔
178
      case PE_CARET:
1✔
179
         print_syntax("^.");
1✔
180
         break;
1✔
181
      case PE_GENERATE:
×
182
         print_syntax("%s(", istr(tree_ident(part)));
×
183
         dump_expr(tree_value(part));
×
184
         print_syntax(").");
×
185
         break;
×
186
      }
187
   }
188
   print_syntax(" : ");
2✔
189
   dump_type(tree_type(t));
2✔
190
   print_syntax(" >>");
2✔
191
}
2✔
192

193
static void dump_expr(tree_t t)
201✔
194
{
195
   switch (tree_kind(t)) {
202✔
196
   case T_PROT_FCALL:
1✔
197
      dump_expr(tree_name(t));
1✔
198
      print_syntax(".");
1✔
199
      // Fall-through
200
   case T_FCALL:
13✔
201
      if (tree_has_ref(t)) {
13✔
202
         tree_t decl = tree_ref(t);
13✔
203
         dump_address(decl);
13✔
204
         print_syntax("%s", istr(tree_ident(decl)));
13✔
205
      }
206
      else
207
         print_syntax("%s", istr(tree_ident(t)));
×
208
      dump_params(t, tree_param, tree_params(t), NULL);
13✔
209
#if DUMP_STATICNESS
210
      if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
211
         color_printf("$red$/* locally static */$$");
212
      else if (tree_flags(t) & TREE_F_GLOBALLY_STATIC)
213
         color_printf("$red$/* globally static */$$");
214
#endif
215
      break;
13✔
216

217
   case T_CONV_FUNC:
×
218
      if (tree_has_ref(t)) {
×
219
         tree_t decl = tree_ref(t);
×
220
         dump_address(decl);
×
221
         print_syntax("%s", istr(tree_ident(decl)));
×
222
      }
223
      else
224
         print_syntax("%s", istr(tree_ident(t)));
×
225
      print_syntax("(");
×
226
      dump_expr(tree_value(t));
×
227
      print_syntax(")");
×
228
      break;
×
229

230
   case T_LITERAL:
34✔
231
      switch (tree_subkind(t)) {
34✔
232
      case L_INT:
28✔
233
         print_syntax("%"PRIi64, tree_ival(t));
28✔
234
         break;
28✔
235
      case L_PHYSICAL:
4✔
236
         if (tree_has_ident(t))
4✔
237
            print_syntax("%"PRIi64" %s", tree_ival(t), istr(tree_ident(t)));
4✔
238
         else
239
            print_syntax("%"PRIi64, tree_ival(t));
×
240
         break;
241
      case L_REAL:
2✔
242
         print_syntax("%lf", tree_dval(t));
2✔
243
         break;
2✔
244
      case L_NULL:
×
245
         print_syntax("#null");
×
246
         break;
×
247
      default:
×
248
         should_not_reach_here();
249
      }
250
      break;
251

252
   case T_STRING:
6✔
253
      {
254
         print_syntax("\"");
6✔
255
         const int nchars = tree_chars(t);
6✔
256
         for (int i = 0; i < nchars; i++) {
30✔
257
            ident_t rune = tree_ident(tree_char(t, i));
24✔
258
            if (ident_char(rune, 0) == '\'')
24✔
259
               print_syntax("%c", ident_char(rune, 1));
24✔
260
            else
261
               print_syntax("\" & %s & \"", istr(rune));
×
262
         }
263
         print_syntax("\"");
6✔
264
      }
265
      break;
6✔
266

267
   case T_NEW:
×
268
      print_syntax("#new ");
×
269
      dump_expr(tree_value(t));
×
270
      break;
×
271

272
   case T_ALL:
×
273
      if (tree_has_value(t)) {
×
274
         dump_expr(tree_value(t));
×
275
         print_syntax(".#all");
×
276
      }
277
      else
278
         print_syntax("#all");
×
279
      break;
280

281
   case T_TYPE_REF:
1✔
282
      dump_type(tree_type(t));
1✔
283
      break;
1✔
284

285
   case T_AGGREGATE:
3✔
286
      print_syntax("(");
3✔
287
      for (unsigned i = 0; i < tree_assocs(t); i++) {
7✔
288
         if (i > 0)
4✔
289
            print_syntax(", ");
1✔
290
         tree_t a = tree_assoc(t, i);
4✔
291
         tree_t value = tree_value(a);
4✔
292
         switch (tree_subkind(a)) {
4✔
293
         case A_CONCAT:
×
NEW
294
            print_syntax("/* concat */");
×
295
            // Fall-through
NEW
296
         case A_POS:
×
297
            dump_expr(value);
×
298
            break;
×
299
         case A_NAMED:
1✔
300
            dump_expr(tree_name(a));
1✔
301
            print_syntax(" => ");
1✔
302
            dump_expr(value);
1✔
303
            break;
1✔
304
         case A_OTHERS:
3✔
305
            print_syntax("#others => ");
3✔
306
            dump_expr(value);
3✔
307
            break;
3✔
308
         case A_SLICE:
×
NEW
309
            print_syntax("/* slice */");
×
310
            // Fall-through
NEW
311
         case A_RANGE:
×
312
            dump_range(tree_range(a, 0));
×
313
            print_syntax(" => ");
×
314
            dump_expr(value);
×
315
            break;
×
316
         default:
×
317
            should_not_reach_here();
318
         }
319
      }
320
      print_syntax(")");
3✔
321
      break;
3✔
322

323
   case T_REF:
54✔
324
      if (tree_has_ref(t)) {
54✔
325
         tree_t decl = tree_ref(t);
54✔
326
         dump_address(decl);
54✔
327
         print_syntax("%s", istr(tree_ident(decl)));
54✔
328
      }
329
      else
330
         print_syntax("%s", istr(tree_ident(t)));
×
331
      break;
332

333
   case T_ATTR_REF:
2✔
334
      dump_expr(tree_name(t));
2✔
335
      print_syntax("'%s", istr(tree_ident(t)));
2✔
336
      if (tree_params(t) > 0)
2✔
337
         dump_params(t, tree_param, tree_params(t), NULL);
×
338
      break;
339

340
   case T_EXTERNAL_NAME:
2✔
341
      dump_external_name(t);
2✔
342
      break;
2✔
343

344
   case T_ARRAY_REF:
1✔
345
      dump_expr(tree_value(t));
1✔
346
      dump_params(t, tree_param, tree_params(t), NULL);
1✔
347
      break;
1✔
348

349
   case T_ARRAY_SLICE:
×
350
      dump_expr(tree_value(t));
×
351
      print_syntax("(");
×
352
      dump_range(tree_range(t, 0));
×
353
      print_syntax(")");
×
354
      break;
×
355

356
   case T_RECORD_REF:
×
357
      dump_expr(tree_value(t));
×
358
      print_syntax(".%s", istr(tree_ident(t)));
×
359
      break;
×
360

361
   case T_TYPE_CONV:
4✔
362
      dump_type(tree_type(t));
4✔
363
      print_syntax("(");
4✔
364
      dump_expr(tree_value(t));
4✔
365
      print_syntax(")");
4✔
366
      break;
4✔
367

368
   case T_QUALIFIED:
×
369
      if (tree_has_value(t)) {
×
370
         print_syntax("%s'(", istr(type_ident(tree_type(t))));
×
371
         dump_expr(tree_value(t));
×
372
         print_syntax(")");
×
373
      }
374
      else
375
         dump_type(tree_type(t));
×
376
      break;
377

378
   case T_OPEN:
1✔
379
      print_syntax("#open");
1✔
380
      break;
1✔
381

382
   case T_BOX:
80✔
383
      print_syntax("<>");
80✔
384
      break;
80✔
385

386
   case T_WAVEFORM:
×
387
      dump_waveform(t);
×
388
      break;
×
389

390
   case T_PACKAGE_MAP:
×
391
      print_syntax("%s ", istr(tree_ident(t)));
×
392
      switch (tree_subkind(t)) {
×
393
      case PACKAGE_MAP_BOX:
×
394
         print_syntax("#generic #map (<>)");
×
395
         break;
×
396
      case PACKAGE_MAP_DEFAULT:
×
397
         print_syntax("#generic map (#default)");
×
398
         break;
×
399
      case PACKAGE_MAP_MATCHING:
×
400
         dump_params(t, tree_genmap, tree_genmaps(t), "#generic #map");
×
401
         break;
×
402
      }
403
      return;
404

405
   case T_COND_VALUE:
406
      for (int i = 0; i < tree_conds(t); i++) {
×
407
         tree_t c = tree_cond(t, i);
×
408
         if (i > 0)
×
409
            print_syntax(" #else ");
×
410
         if (tree_has_result(c))
×
411
            dump_expr(tree_result(c));
×
412
         else
413
            print_syntax("#unaffected");
×
414
         if (tree_has_value(c)) {
×
415
            print_syntax(" #when ");
×
416
            dump_expr(tree_value(c));
×
417
         }
418
      }
419
      break;
420

421
   case T_INERTIAL:
1✔
422
      print_syntax("#inertial ");
1✔
423
      dump_expr(tree_value(t));
1✔
424
      break;
1✔
425

426
   default:
×
427
      cannot_dump(t, "expr");
×
428
   }
429

430
   dump_type_hint(t);
201✔
431
}
432

433
static void dump_record_elem_constraint(tree_t t)
×
434
{
435
   print_syntax("%s", istr(tree_ident(t)));
×
436

437
   type_t ftype = tree_type(t);
×
438
   if (type_has_constraint(ftype))
×
439
      dump_constraint(type_constraint(ftype));
×
440

441
   dump_elem_constraints(ftype);
×
442
}
×
443

444
static void dump_constraint(tree_t t)
5✔
445
{
446
   const int nranges = tree_ranges(t);
5✔
447

448
   switch (tree_subkind(t)) {
5✔
449
   case C_RANGE:
1✔
450
      print_syntax(" #range ");
1✔
451
      dump_range(tree_range(t, 0));
1✔
452
      break;
1✔
453
   case C_INDEX:
4✔
454
      print_syntax("(");
4✔
455
      for (int i = 0; i < nranges; i++) {
8✔
456
         if (i > 0) print_syntax(", ");
4✔
457
         dump_range(tree_range(t, i));
4✔
458
      }
459
      print_syntax(")");
4✔
460
      break;
4✔
461
   case C_RECORD:
×
462
      print_syntax("(");
×
463
      for (int i = 0; i < nranges; i++) {
×
464
         if (i > 0) print_syntax(", ");
×
465
         dump_record_elem_constraint(tree_range(t, i));
×
466
      }
467
      print_syntax(")");
×
468
      break;
×
469
   }
470
}
5✔
471

472
static void dump_elem_constraints(type_t type)
5✔
473
{
474
   if (type_is_array(type) && type_has_elem(type)) {
5✔
475
      type_t elem = type_elem(type);
×
476
      if (is_anonymous_subtype(elem)) {
×
477
         // Anonymous subtype created for element constraints
478
         if (type_has_constraint(elem))
×
479
            dump_constraint(type_constraint(elem));
×
480
         else
481
            print_syntax("(#open)");
×
482
         dump_elem_constraints(elem);
×
483
      }
484
   }
485
}
5✔
486

487
static void dump_type(type_t type)
123✔
488
{
489
   if (is_anonymous_subtype(type)) {
123✔
490
      // Anonymous subtype
491
      print_syntax("%s", type_pp(type));
4✔
492
      if (type_ident(type) == type_ident(type_base(type))) {
4✔
493
         if (type_has_constraint(type))
4✔
494
            dump_constraint(type_constraint(type));
4✔
495
      }
496
      dump_elem_constraints(type);
4✔
497
   }
498
   else if (type_is_none(type))
119✔
499
      print_syntax("/* error */");
×
500
   else
501
      print_syntax("%s", type_pp(type));
119✔
502
}
123✔
503

504
static void dump_arguments(tree_t t, int indent, const char *trailer)
7✔
505
{
506
   const int nports = tree_ports(t);
7✔
507
   if (nports > 0) {
7✔
508
      print_syntax(" (");
3✔
509
      if (nports > 1) {
3✔
510
         print_syntax("\n");
1✔
511
         for (int i = 0; i < nports; i++) {
3✔
512
            if (i > 0) print_syntax(";\n");
2✔
513
            dump_port(tree_port(t, i), indent + 4);
2✔
514
         }
515
      }
516
      else
517
         dump_port(tree_port(t, 0), 1);
2✔
518
      print_syntax(" )%s", trailer);
3✔
519
   }
520
}
7✔
521

522
static void dump_ports(tree_t t, int indent)
5✔
523
{
524
   const int nports = tree_ports(t);
5✔
525
   if (nports > 0) {
5✔
526
      tab(indent);
4✔
527
      print_syntax("#port (");
4✔
528
      if (nports > 1) {
4✔
529
         print_syntax("\n");
3✔
530
         for (unsigned i = 0; i < nports; i++) {
9✔
531
            if (i > 0) print_syntax(";\n");
6✔
532
            dump_port(tree_port(t, i), indent + 2);
6✔
533
         }
534
      }
535
      else
536
         dump_port(tree_port(t, 0), 1);
1✔
537
      print_syntax(" );\n");
4✔
538
   }
539
}
5✔
540

541
static void dump_generics(tree_t t, int indent, const char *trailer)
13✔
542
{
543
   const int ngenerics = tree_generics(t);
13✔
544
   if (ngenerics > 0) {
13✔
545
      tab(indent);
3✔
546
      print_syntax("#generic (");
3✔
547
      if (ngenerics > 1) {
3✔
548
         print_syntax("\n");
2✔
549
         for (int i = 0; i < ngenerics; i++) {
93✔
550
            tree_t g = tree_generic(t, i);
91✔
551
            dump_port(g, indent + 2);
91✔
552
            if (i + 1 == ngenerics && (tree_flags(g) & TREE_F_PREDEFINED)) {
91✔
553
               print_syntax(";\n");
2✔
554
               tab(indent - 1);
2✔
555
            }
556
            else if (i + 1 < ngenerics)
89✔
557
               print_syntax(";\n");
89✔
558
         }
559
      }
560
      else
561
         dump_port(tree_generic(t, 0), 1);
1✔
562
      print_syntax(" )%s", trailer);
3✔
563
   }
564
}
13✔
565

566
static void dump_port_map(tree_t t, int indent, const char *trailer)
3✔
567
{
568
   const int nparams = tree_params(t);
3✔
569
   if (nparams > 0) {
3✔
570
      tab(indent);
3✔
571
      dump_params(t, tree_param, nparams, "#port #map");
3✔
572
      print_syntax("%s", trailer);
3✔
573
   }
574
}
3✔
575

576
static void dump_generic_map(tree_t t, int indent, const char *trailer)
4✔
577
{
578
   const int ngenmaps = tree_genmaps(t);
4✔
579
   if (ngenmaps > 0) {
4✔
580
      tab(indent);
1✔
581
      dump_params(t, tree_genmap, ngenmaps, "#generic #map");
1✔
582
      print_syntax("%s", trailer);
1✔
583
   }
584
}
4✔
585

586
static void dump_binding(tree_t t, int indent)
×
587
{
588
   dump_address(t);
×
589
   print_syntax("#use %s", istr(tree_ident(t)));
×
590
   if (tree_has_ident2(t))
×
591
      print_syntax("(%s)", istr(tree_ident2(t)));
×
592
   if (tree_genmaps(t) > 0 || tree_params(t) > 0)
×
593
      print_syntax("\n");
×
594
   dump_generic_map(t, indent + 2, tree_params(t) > 0 ? "\n" : "");
×
595
   dump_port_map(t, indent + 2, "");
×
596
   print_syntax(";\n");
×
597
}
×
598

599
static void dump_stmts(tree_t t, int indent)
13✔
600
{
601
   bool last_was_newline = false;
13✔
602
   const int nstmts = tree_stmts(t);
13✔
603
   for (int i = 0; i < nstmts; i++) {
25✔
604
      tree_t s = tree_stmt(t, i);
12✔
605
      const tree_kind_t kind = tree_kind(s);
12✔
606

607
      bool needs_newline;
12✔
608
      if (kind == T_VERILOG) {
12✔
609
         const vlog_kind_t vkind = vlog_kind(tree_vlog(s));
×
610
         needs_newline = (vkind == V_ALWAYS || vkind == V_INITIAL);
×
611
      }
612
      else
613
         needs_newline = (kind == T_BLOCK || kind == T_PROCESS
12✔
614
                          || kind == T_INSTANCE);
615

616
      if (needs_newline && i > 0 && !last_was_newline)
12✔
617
         print_syntax("\n");
×
618

619
      dump_stmt(s, indent);
12✔
620

621
      if (needs_newline && i + 1 < nstmts) {
12✔
622
         print_syntax("\n");
×
623
         last_was_newline = true;
×
624
      }
625
      else
626
         last_was_newline = false;
627
   }
628
}
13✔
629

630
static void dump_block(tree_t t, int indent)
7✔
631
{
632
   dump_decls(t, indent + 2);
7✔
633
   tab(indent);
7✔
634
   print_syntax("#begin\n");
7✔
635
   dump_stmts(t, indent + 2);
7✔
636
}
7✔
637

638
static void dump_wait_level(tree_t t)
3✔
639
{
640
   const tree_flags_t flags = tree_flags(t);
3✔
641
   if (flags & TREE_F_NEVER_WAITS)
3✔
642
      print_syntax("   -- Never waits");
3✔
643
   else if (flags & TREE_F_HAS_WAIT)
×
644
      print_syntax("   -- Contains wait statement");
×
645
}
3✔
646

647
static void dump_type_decl(tree_t t, int indent)
2✔
648
{
649
   type_t type = tree_type(t);
2✔
650
   const type_kind_t kind = type_kind(type);
2✔
651

652
   print_syntax("#type %s", istr(tree_ident(t)));
2✔
653

654
   if (kind == T_INCOMPLETE) {
2✔
655
      print_syntax(";\n");
×
656
      return;
×
657
   }
658

659
   print_syntax(" #is ");
2✔
660

661
   if (type_is_integer(type) || type_is_real(type)) {
2✔
662
      print_syntax("#range ");
×
663
      dump_range(type_dim(type, 0));
×
664
   }
665
   else if (type_is_physical(type)) {
2✔
666
      print_syntax("#range ");
×
667
      dump_range(type_dim(type, 0));
×
668
      print_syntax("\n");
×
669
      tab(indent + 2);
×
670
      print_syntax("#units\n");
×
671
      {
672
         const int nunits = type_units(type);
×
673
         for (int i = 0; i < nunits; i++) {
×
674
            tree_t u = type_unit(type, i);
×
675
            tab(indent + 4);
×
676
            print_syntax("%s = ", istr(tree_ident(u)));
×
677
            dump_expr(tree_value(u));
×
678
            print_syntax(";\n");
×
679
         }
680
      }
681
      tab(indent + 2);
×
682
      print_syntax("#end #units");
×
683
   }
684
   else if (type_is_array(type)) {
2✔
685
      print_syntax("#array ");
1✔
686
      if (kind == T_ARRAY) {
1✔
687
         print_syntax("(");
1✔
688
         const int nindex = type_indexes(type);
1✔
689
         for (int i = 0; i < nindex; i++) {
2✔
690
            if (i > 0) print_syntax(", ");
1✔
691
            dump_type(type_index(type, i));
1✔
692
            print_syntax(" #range <>");
1✔
693
         }
694
         print_syntax(")");
1✔
695
      }
696
      else if (kind == T_SUBTYPE) {
×
697
         if (type_has_constraint(type))
×
698
            dump_constraint(type_constraint(type));
×
699
      }
700
      else {
701
         print_syntax("(");
×
702
         const int ndims = type_dims(type);
×
703
         for (int i = 0; i < ndims; i++) {
×
704
            if (i > 0) print_syntax(", ");
×
705
            dump_range(type_dim(type, i));
×
706
         }
707
         print_syntax(")");
×
708
      }
709
      print_syntax(" #of ");
1✔
710
      dump_type(type_elem(type));
1✔
711
   }
712
   else if (type_is_record(type)) {
1✔
713
      print_syntax("#record\n");
1✔
714
      const int nfields = type_fields(type);
1✔
715
      for (int i = 0; i < nfields; i++)
3✔
716
         dump_decl(type_field(type, i), indent + 2);
2✔
717
      tab(indent);
1✔
718
      print_syntax("#end #record");
1✔
719
   }
720
   else if (kind == T_ENUM) {
×
721
      print_syntax("(");
×
722
      for (unsigned i = 0; i < type_enum_literals(type); i++) {
×
723
         if (i > 0) print_syntax(", ");
×
724
         print_syntax("%s", istr(tree_ident(type_enum_literal(type, i))));
×
725
      }
726
      print_syntax(")");
×
727
   }
728
   else if (kind == T_INCOMPLETE)
×
729
      ;
730
   else
731
      dump_type(type);
×
732

733
   print_syntax(";\n");
2✔
734
}
735

736
static void dump_subtype_decl(tree_t t, int indent)
1✔
737
{
738
   type_t type = tree_type(t);
1✔
739

740
   print_syntax("#subtype %s #is ", istr(tree_ident(t)));
1✔
741
   if (type_has_resolution(type)) {
1✔
742
      dump_expr(type_resolution(type));
×
743
      print_syntax(" ");
×
744
   }
745
   print_syntax("%s", type_pp(type_base(type)));
1✔
746

747
   if (type_has_constraint(type))
1✔
748
      dump_constraint(type_constraint(type));
1✔
749

750
   dump_elem_constraints(type);
1✔
751

752
   print_syntax(";\n");
1✔
753
}
1✔
754

755
static void dump_component(tree_t t, int indent)
1✔
756
{
757
   print_syntax("#component %s #is\n", istr(tree_ident(t)));
1✔
758
   dump_generics(t, indent + 2, ";\n");
1✔
759
   dump_ports(t, indent + 2);
1✔
760
   print_syntax("#end #component;\n");
1✔
761
}
1✔
762

763
static void dump_use(tree_t t)
7✔
764
{
765
   print_syntax("#use %s", istr(tree_ident(t)));
7✔
766
   if (tree_has_ident2(t))
7✔
767
      print_syntax(".%s", istr(tree_ident2(t)));
7✔
768
   print_syntax(";\n");
7✔
769
}
7✔
770

771
static void dump_attr_spec(tree_t t)
4✔
772
{
773
   print_syntax("#attribute %s #of ", istr(tree_ident(t)));
4✔
774

775
   switch (tree_subkind(t)) {
4✔
776
   case SPEC_EXACT:
2✔
777
      print_syntax("%s", istr(tree_ident2(t)));
2✔
778
      break;
2✔
779
   case SPEC_ALL:
1✔
780
      print_syntax("#all");
1✔
781
      break;
1✔
782
   case SPEC_OTHERS:
1✔
783
      print_syntax("#others");
1✔
784
      break;
1✔
785
   }
786

787
   print_syntax(" : #%s #is ", class_str(tree_class(t)));
4✔
788
   dump_expr(tree_value(t));
4✔
789
   print_syntax(";\n");
4✔
790
}
4✔
791

792
static void dump_view_decl(tree_t t, int indent)
1✔
793
{
794
   type_t type = tree_type(t);
1✔
795
   print_syntax("#view %s #of ", istr(tree_ident(t)));
1✔
796
   dump_type(type_designated(type));
1✔
797
   print_syntax(" #is\n");
1✔
798

799
   const int nfields = type_fields(type);
1✔
800
   for (int i = 0; i < nfields; i++) {
3✔
801
      tree_t elem = type_field(type, i);
2✔
802
      tab(indent + 2);
2✔
803
      print_syntax("%s : #%s;\n", istr(tree_ident(elem)),
2✔
804
                   port_mode_str(tree_subkind(elem)));
2✔
805
   }
806

807
   tab(indent);
1✔
808
   print_syntax("#end #view;\n");
1✔
809
}
1✔
810

811
static void dump_decl(tree_t t, int indent)
35✔
812
{
813
   tab(indent);
35✔
814
   if (tree_kind(t) != T_HIER) dump_address(t);
35✔
815

816
   switch (tree_kind(t)) {
35✔
817
   case T_IMPLICIT_SIGNAL:
×
818
      print_syntax("/* implicit */ ");
×
819
      // Fall-through
820
   case T_SIGNAL_DECL:
5✔
821
      print_syntax("#signal %s : ", istr(tree_ident(t)));
5✔
822
      break;
5✔
823

824
   case T_VAR_DECL:
3✔
825
      print_syntax("#variable %s : ", istr(tree_ident(t)));
3✔
826
      break;
3✔
827

828
   case T_CONST_DECL:
3✔
829
      print_syntax("#constant %s : ", istr(tree_ident(t)));
3✔
830
      break;
3✔
831

832
   case T_GENERIC_DECL:
1✔
833
      // Loop variable in for-generate statement
834
      print_syntax("/* loop variable */ %s : ", istr(tree_ident(t)));
1✔
835
      break;
1✔
836

837
   case T_FIELD_DECL:
2✔
838
      print_syntax("%s : ", istr(tree_ident(t)));
2✔
839
      break;
2✔
840

841
   case T_TYPE_DECL:
2✔
842
      dump_type_decl(t, indent);
2✔
843
      return;
2✔
844

845
   case T_SUBTYPE_DECL:
1✔
846
      dump_subtype_decl(t, indent);
1✔
847
      return;
1✔
848

849
   case T_SPEC:
×
850
      if (tree_has_ident(t))
×
851
         print_syntax("#for %s", istr(tree_ident(t)));
×
852
      else
853
         print_syntax("#for #others");
×
854
      if (tree_has_ref(t))
×
855
         print_syntax(" : %s", istr(tree_ident(tree_ref(t))));
×
856
      print_syntax("\n");
×
857
      tab(indent + 2);
×
858
      dump_binding(tree_value(t), indent + 2);
×
859
      dump_decls(t, indent + 2);
×
860
      tab(indent);
×
861
      print_syntax("#end #for;\n");
×
862
      return;
×
863

864
   case T_BLOCK_CONFIG:
×
865
      print_syntax("#for %s\n", istr(tree_ident(t)));
×
866
      dump_decls(t, indent + 2);
×
867
      tab(indent);
×
868
      print_syntax("#end #for;\n");
×
869
      return;
×
870

871
   case T_ENUM_LIT:
×
872
      print_syntax("%s", istr(tree_ident(t)));
×
873
      return;
×
874

875
   case T_ALIAS:
×
876
      if (tree_flags(t) & TREE_F_PREDEFINED)
×
877
         print_syntax("-- predefined ");
×
878
      print_syntax("#alias %s", istr(tree_ident(t)));
×
879
      if (tree_has_type(t)) {
×
880
         print_syntax(" : ");
×
881
         dump_type(tree_type(t));
×
882
      }
883
      print_syntax(" #is ");
×
884
      dump_expr(tree_value(t));
×
885
      print_syntax(";\n");
×
886
      return;
×
887

888
   case T_ATTR_SPEC:
4✔
889
      dump_attr_spec(t);
4✔
890
      return;
4✔
891

892
   case T_ATTR_DECL:
2✔
893
      print_syntax("#attribute %s : ", istr(tree_ident(t)));
2✔
894
      dump_type(tree_type(t));
2✔
895
      print_syntax(";\n");
2✔
896
      return;
2✔
897

898
   case T_FUNC_DECL:
2✔
899
      if (tree_flags(t) & TREE_F_PREDEFINED)
2✔
900
         print_syntax("-- predefined %s\n", type_pp(tree_type(t)));
×
901
      else {
902
         print_syntax("#function %s", istr(tree_ident(t)));
2✔
903
         dump_generics(t, indent + 2, "");
2✔
904
         dump_arguments(t, indent, "");
2✔
905
         print_syntax(" #return ");
2✔
906
         dump_type(type_result(tree_type(t)));
2✔
907
         print_syntax(";\n");
2✔
908
         if (tree_has_ident2(t)) {
2✔
909
            tab(indent + 2);
2✔
910
            print_syntax("-- %s\n", istr(tree_ident2(t)));
2✔
911
         }
912
      }
913
      return;
914

915
   case T_FUNC_INST:
2✔
916
   case T_FUNC_BODY:
917
      print_syntax("#function %s", istr(tree_ident(t)));
2✔
918
      dump_type_hint(t);
2✔
919
      dump_generics(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
3✔
920
      if (tree_kind(t) == T_FUNC_INST)
2✔
921
         dump_generic_map(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
×
922
      dump_arguments(t, indent, "");
2✔
923
      print_syntax(" #return ");
2✔
924
      dump_type(type_result(tree_type(t)));
2✔
925
      print_syntax(" #is\n");
2✔
926
      if (tree_has_ident2(t)) {
2✔
927
         tab(indent + 2);
2✔
928
         print_syntax("-- %s\n", istr(tree_ident2(t)));
2✔
929
      }
930
      dump_block(t, indent);
2✔
931
      tab(indent);
2✔
932
      print_syntax("#end #function;\n");
2✔
933
      return;
2✔
934

935
   case T_PROC_DECL:
×
936
      if (tree_flags(t) & TREE_F_PREDEFINED)
×
937
         print_syntax("-- predefined %s\n", type_pp(tree_type(t)));
×
938
      else {
939
         print_syntax("#procedure %s", istr(tree_ident(t)));
×
940
         dump_generics(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
×
941
         dump_arguments(t, indent, "");
×
942
         print_syntax(";");
×
943
         dump_wait_level(t);
×
944
         print_syntax("\n");
×
945
         if (tree_has_ident2(t)) {
×
946
            tab(indent + 2);
×
947
            print_syntax("-- %s\n", istr(tree_ident2(t)));
×
948
         }
949
      }
950
      return;
951

952
   case T_PROC_INST:
3✔
953
   case T_PROC_BODY:
954
      print_syntax("#procedure %s", istr(tree_ident(t)));
3✔
955
      dump_type_hint(t);
3✔
956
      dump_generics(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
5✔
957
      if (tree_kind(t) == T_PROC_INST)
3✔
958
         dump_generic_map(t, indent + 2, tree_ports(t) > 0 ? "\n" : "");
×
959
      dump_arguments(t, indent, "");
3✔
960
      print_syntax(" #is");
3✔
961
      dump_wait_level(t);
3✔
962
      print_syntax("\n");
3✔
963
      if (tree_has_ident2(t)) {
3✔
964
         tab(indent + 2);
3✔
965
         print_syntax("-- %s\n", istr(tree_ident2(t)));
3✔
966
      }
967
      dump_block(t, indent);
3✔
968
      tab(indent);
3✔
969
      print_syntax("#end #procedure;\n");
3✔
970
      return;
3✔
971

972
   case T_HIER:
×
973
      {
974
         const char *kind = "Scope";
×
975
         switch (tree_subkind(t)) {
×
976
         case T_ARCH: kind = "Instance"; break;
×
977
         case T_IF_GENERATE: kind = "If generate"; break;
×
978
         case T_FOR_GENERATE: kind = "For generate"; break;
×
979
         case T_BLOCK: kind = "Block"; break;
×
980
         }
981
         print_syntax("-- %s %s\n", kind, istr(tree_ident2(t)));
×
982
      }
983
      return;
×
984

985
   case T_COMPONENT:
1✔
986
      dump_component(t, indent);
1✔
987
      return;
1✔
988

989
   case T_PROT_DECL:
1✔
990
      print_syntax("#type %s #is #protected\n", istr(tree_ident(t)));
1✔
991
      dump_decls(t, indent + 2);
1✔
992
      tab(indent);
1✔
993
      print_syntax("#end #protected;\n");
1✔
994
      return;
1✔
995

996
   case T_PROT_BODY:
1✔
997
      print_syntax("#type %s #is #protected #body\n", istr(tree_ident(t)));
1✔
998
      dump_decls(t, indent + 2);
1✔
999
      tab(indent);
1✔
1000
      print_syntax("#end #protected #body;\n");
1✔
1001
      return;
1✔
1002

1003
   case T_FILE_DECL:
×
1004
      print_syntax("#file %s : ", istr(tree_ident(t)));
×
1005
      dump_type(tree_type(t));
×
1006
      if (tree_has_value(t)) {
×
1007
         print_syntax(" #open ");
×
1008
         dump_expr(tree_file_mode(t));
×
1009
         print_syntax(" #is ");
×
1010
         dump_expr(tree_value(t));
×
1011
      }
1012
      print_syntax(";\n");
×
1013
      return;
×
1014

1015
   case T_USE:
×
1016
      dump_use(t);
×
1017
      return;
×
1018

1019
   case T_PACKAGE:
×
1020
   case T_PACK_INST:
1021
      dump_package(t, indent);
×
1022
      return;
×
1023

1024
   case T_PACK_BODY:
×
1025
      dump_package_body(t, indent);
×
1026
      return;
×
1027

1028
   case T_PSL_DECL:
1✔
1029
   case T_PSL_DIRECT:
1030
      dump_psl(t, 0);
1✔
1031
      print_syntax(";\n");
1✔
1032
      return;
1✔
1033

1034
   case T_VIEW_DECL:
1✔
1035
      dump_view_decl(t, 0);
1✔
1036
      return;
1✔
1037

1038
   default:
×
1039
      cannot_dump(t, "decl");
×
1040
   }
1041

1042
   dump_type(tree_type(t));
14✔
1043

1044
   if (tree_kind(t) != T_FIELD_DECL && tree_has_value(t)) {
14✔
1045
      print_syntax(" := ");
6✔
1046
      dump_expr(tree_value(t));
6✔
1047
   }
1048
   print_syntax(";\n");
14✔
1049
}
1050

1051
static void dump_waveforms(tree_t t)
3✔
1052
{
1053
   const int nwaves = tree_waveforms(t);
3✔
1054
   for (int i = 0; i < nwaves; i++) {
7✔
1055
      if (i > 0) print_syntax(", ");
4✔
1056
      dump_waveform(tree_waveform(t, i));
4✔
1057
   }
1058
}
3✔
1059

1060
static void dump_alternative(tree_t t, int indent)
×
1061
{
1062
   tab(indent);
×
1063
   print_syntax("#when ");
×
1064
   if (tree_has_ident(t))
×
1065
      print_syntax("%s: ", istr(tree_ident(t)));
×
1066
   for (unsigned i = 0; i < tree_assocs(t); i++) {
×
1067
      if (i > 0) print_syntax("| ");
×
1068
      tree_t a = tree_assoc(t, i);
×
1069
      switch (tree_subkind(a)) {
×
1070
      case A_NAMED:
×
1071
         dump_expr(tree_name(a));
×
1072
         break;
×
1073
      case A_OTHERS:
×
1074
         print_syntax("#others");
×
1075
         break;
×
1076
      case A_RANGE:
×
1077
         dump_range(tree_range(a, 0));
×
1078
         break;
×
1079
      }
1080
   }
1081
   print_syntax(" =>\n");
×
1082
   if (tree_decls(t) > 0) {
×
1083
      dump_decls(t, indent + 4);
×
1084
      tab(indent + 2);
×
1085
      print_syntax("#begin\n");
×
1086
      dump_stmts(t, indent + 4);
×
1087
      tab(indent + 2);
×
1088
      print_syntax("#end;\n");
×
1089
   }
1090
   else
1091
      dump_stmts(t, indent + 2);
×
1092
}
×
1093

1094
static void dump_psl(tree_t t, int indent)
2✔
1095
{
1096
   if (standard() < STD_08)
2✔
1097
      print_syntax("-- psl ");
×
1098
   psl_dump(tree_psl(t));
2✔
1099
}
2✔
1100

1101
static void dump_instance(tree_t t, int indent)
1✔
1102
{
1103
   switch (tree_class(t)) {
1✔
1104
   case C_ENTITY:        print_syntax("#entity "); break;
×
1105
   case C_COMPONENT:     print_syntax("#component "); break;
1✔
1106
   case C_CONFIGURATION: print_syntax("#configuration "); break;
×
1107
   default: break;
1108
   }
1109

1110
   print_syntax("%s", istr(tree_ident2(t)));
1✔
1111

1112
   const int nparams = tree_params(t);
1✔
1113
   const int ngenmaps = tree_genmaps(t);
1✔
1114

1115
   if (tree_has_spec(t)) {
1✔
1116
      tree_t spec = tree_spec(t);
×
1117
      if (tree_has_value(spec)) {
×
1118
         tree_t bind = tree_value(spec);
×
1119
         LOCAL_TEXT_BUF tb = tb_new();
×
1120
         tb_cat(tb, istr(tree_ident(bind)));
×
1121
         if (tree_has_ident2(bind))
×
1122
            tb_printf(tb, "(%s)", istr(tree_ident2(bind)));
×
1123
         print_syntax("  -- bound to %s\n", tb_get(tb));
×
1124
      }
1125
   }
1126
   else if (nparams > 0 || ngenmaps > 0)
1✔
1127
      print_syntax("\n");
1✔
1128

1129
   dump_generic_map(t, indent + 2, nparams > 0 ? "\n" : "");
1✔
1130
   dump_port_map(t, indent + 2, "");
1✔
1131
   print_syntax(";\n");
1✔
1132
}
1✔
1133

1134
static void dump_stmt(tree_t t, int indent)
18✔
1135
{
1136
   const tree_kind_t kind = tree_kind(t);
21✔
1137
   if (kind == T_VERILOG) {
21✔
1138
      vlog_dump(tree_vlog(t), indent);
×
1139
      return;
×
1140
   }
1141

1142
   tab(indent);
21✔
1143

1144
   if (tree_has_ident(t)) {
21✔
1145
      const char *label = istr(tree_ident(t));
9✔
1146
#if !DUMP_GEN_NAMES
1147
      if (label[0] != '_')   // Skip generated labels
9✔
1148
#endif
1149
         print_syntax("%s: ", label);
5✔
1150
   }
1151

1152
   switch (tree_kind(t)) {
21✔
1153
   case T_PROCESS:
1✔
1154
      dump_address(t);
1✔
1155
      if (tree_flags(t) & TREE_F_POSTPONED)
1✔
1156
         print_syntax("#postponed ");
×
1157
      print_syntax("#process ");
1✔
1158
      if (tree_triggers(t) > 0) {
1✔
1159
         print_syntax("(");
1✔
1160
         for (unsigned i = 0; i < tree_triggers(t); i++) {
2✔
1161
            if (i > 0)
1✔
1162
               print_syntax(", ");
×
1163
            dump_expr(tree_trigger(t, i));
1✔
1164
         }
1165
         print_syntax(") ");
1✔
1166
      }
1167
      print_syntax("#is\n");
1✔
1168
      dump_decls(t, indent + 2);
1✔
1169
      tab(indent);
1✔
1170
      print_syntax("#begin\n");
1✔
1171
      dump_stmts(t, indent + 2);
1✔
1172
      tab(indent);
1✔
1173
      print_syntax("#end #process;\n");
1✔
1174
      return;
1✔
1175

1176
   case T_SIGNAL_ASSIGN:
2✔
1177
      dump_expr(tree_target(t));
2✔
1178
      print_syntax(" <= #reject ");
2✔
1179
      if (tree_has_reject(t))
2✔
1180
         dump_expr(tree_reject(t));
1✔
1181
      else
1182
         print_syntax("0 ps");
1✔
1183
      print_syntax(" #inertial ");
2✔
1184
      dump_waveforms(t);
2✔
1185
      break;
2✔
1186

1187
   case T_FORCE:
×
1188
      dump_expr(tree_target(t));
×
1189
      print_syntax(" <= #force ");
×
1190
      dump_expr(tree_value(t));
×
1191
      break;
×
1192

1193
   case T_RELEASE:
×
1194
      dump_expr(tree_target(t));
×
1195
      print_syntax(" <= #release");
×
1196
      break;
×
1197

1198
   case T_VAR_ASSIGN:
2✔
1199
      dump_expr(tree_target(t));
2✔
1200
      print_syntax(" := ");
2✔
1201
      dump_expr(tree_value(t));
2✔
1202
      break;
2✔
1203

1204
   case T_WAIT:
×
1205
      print_syntax("#wait");
×
1206
      if (tree_triggers(t) > 0) {
×
1207
         print_syntax(" #on ");
×
1208
         for (unsigned i = 0; i < tree_triggers(t); i++) {
×
1209
            if (i > 0)
×
1210
               print_syntax(", ");
×
1211
            dump_expr(tree_trigger(t, i));
×
1212
         }
1213
      }
1214
      if (tree_has_value(t)) {
×
1215
         print_syntax(" #until ");
×
1216
         dump_expr(tree_value(t));
×
1217
      }
1218
      if (tree_has_delay(t)) {
×
1219
         print_syntax(" #for ");
×
1220
         dump_expr(tree_delay(t));
×
1221
      }
1222
      print_syntax(";");
×
1223
      if (tree_flags(t) & TREE_F_STATIC_WAIT)
×
1224
         print_syntax("   -- static");
×
1225
      print_syntax("\n");
×
1226
      return;
×
1227

1228
   case T_BLOCK:
2✔
1229
      dump_address(t);
2✔
1230
      print_syntax("#block #is\n");
2✔
1231
      dump_generics(t, indent + 2, ";\n");
2✔
1232
      dump_generic_map(t, indent + 2, ";\n");
2✔
1233
      dump_ports(t, indent + 2);
2✔
1234
      dump_port_map(t, indent + 2, ";\n");
2✔
1235
      dump_block(t, indent);
2✔
1236
      tab(indent);
2✔
1237
      print_syntax("#end #block;\n");
2✔
1238
      return;
2✔
1239

1240
   case T_SEQUENCE:
×
1241
      print_syntax("#block #is\n");
×
1242
      dump_block(t, indent);
×
1243
      tab(indent);
×
1244
      print_syntax("#end #block;\n");
×
1245
      return;
×
1246

1247
   case T_ASSERT:
2✔
1248
      print_syntax("#assert ");
2✔
1249
      dump_expr(tree_value(t));
2✔
1250
      if (tree_has_message(t)) {
2✔
1251
         print_syntax(" #report ");
×
1252
         dump_expr(tree_message(t));
×
1253
      }
1254
      if (tree_has_severity(t)) {
2✔
1255
         print_syntax(" #severity ");
×
1256
         dump_expr(tree_severity(t));
×
1257
      }
1258
      break;
1259

1260
   case T_REPORT:
1✔
1261
      print_syntax("#report ");
1✔
1262
      dump_expr(tree_message(t));
1✔
1263
      if (tree_has_severity(t)) {
1✔
1264
         print_syntax(" #severity ");
×
1265
         dump_expr(tree_severity(t));
×
1266
      }
1267
      break;
1268

1269
   case T_WHILE:
×
1270
      print_syntax("#while ");
×
1271
      dump_expr(tree_value(t));
×
1272
      print_syntax(" ");
×
1273
      // Fall-through
1274
   case T_LOOP:
×
1275
      print_syntax("#loop\n");
×
1276
      dump_stmts(t, indent + 2);
×
1277
      tab(indent);
×
1278
      print_syntax("#end #loop");
×
1279
      break;
×
1280

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

1301
   case T_EXIT:
×
1302
      print_syntax("#exit %s", istr(tree_ident2(t)));
×
1303
      if (tree_has_value(t)) {
×
1304
         print_syntax(" #when ");
×
1305
         dump_expr(tree_value(t));
×
1306
      }
1307
      break;
1308

1309
   case T_CASE:
×
1310
   case T_MATCH_CASE:
1311
      {
1312
         const char *suffix = tree_kind(t) == T_MATCH_CASE ? "?" : "";
×
1313
         print_syntax("#case%s ", suffix);
×
1314
         dump_expr(tree_value(t));
×
1315
         print_syntax(" #is\n");
×
1316
         const int nstmts = tree_stmts(t);
×
1317
         for (int i = 0; i < nstmts; i++)
×
1318
            dump_alternative(tree_stmt(t, i), indent + 2);
×
1319
         tab(indent);
×
1320
         print_syntax("#end #case%s", suffix);
×
1321
      }
1322
      break;
×
1323

1324
   case T_RETURN:
3✔
1325
      print_syntax("#return");
3✔
1326
      if (tree_has_value(t)) {
3✔
1327
         print_syntax(" ");
3✔
1328
         dump_expr(tree_value(t));
3✔
1329
      }
1330
      break;
1331

1332
   case T_COND_RETURN:
×
1333
      print_syntax("#return #when ");
×
1334
      dump_expr(tree_value(t));
×
1335
      break;
×
1336

1337
   case T_FOR:
×
1338
      print_syntax("#for %s #in ", istr(tree_ident(tree_decl(t, 0))));
×
1339
      dump_range(tree_range(t, 0));
×
1340
      print_syntax(" #loop\n");
×
1341
      dump_stmts(t, indent + 2);
×
1342
      tab(indent);
×
1343
      print_syntax("#end #for");
×
1344
      break;
×
1345

1346
   case T_PROT_PCALL:
×
1347
      dump_expr(tree_name(t));
×
1348
      print_syntax(".");
×
1349
      // Fall-through
1350
   case T_PCALL:
×
1351
      if (tree_has_ref(t)) {
×
1352
         tree_t decl = tree_ref(t);
×
1353
         dump_address(decl);
×
1354
         print_syntax("%s", istr(tree_ident(decl)));
×
1355
      }
1356
      else
1357
         print_syntax("%s", istr(tree_ident2(t)));
×
1358
      dump_params(t, tree_param, tree_params(t), NULL);
×
1359
      break;
×
1360

1361
   case T_FOR_GENERATE:
1✔
1362
      print_syntax("#for %s #in ", istr(tree_ident(tree_decl(t, 0))));
1✔
1363
      dump_range(tree_range(t, 0));
1✔
1364
      print_syntax(" #generate\n");
1✔
1365
      dump_decls(t, indent + 2);
1✔
1366
      tab(indent);
1✔
1367
      print_syntax("#begin\n");
1✔
1368
      dump_stmts(t, indent + 2);
1✔
1369
      tab(indent);
1✔
1370
      print_syntax("#end #generate");
1✔
1371
      break;
1✔
1372

1373
   case T_CASE_GENERATE:
×
1374
      {
1375
         print_syntax("#case ");
×
1376
         dump_expr(tree_value(t));
×
1377
         print_syntax(" #generate\n");
×
1378

1379
         const int nstmts = tree_stmts(t);
×
1380
         for (int i = 0; i < nstmts; i++)
×
1381
            dump_alternative(tree_stmt(t, i), indent + 2);
×
1382

1383
         print_syntax("#end #generate");
×
1384
         tab(indent);
×
1385
      }
1386
      break;
×
1387

1388
   case T_IF_GENERATE:
1389
      for (unsigned i = 0; i < tree_conds(t); i++) {
×
1390
         tree_t c = tree_cond(t, i);
×
1391
         if (tree_has_value(c)) {
×
1392
            if (i > 0)
×
1393
               tab(indent);
×
1394
            print_syntax(i > 0 ? "#elsif " : "#if ");
×
1395
            dump_expr(tree_value(c));
×
1396
            print_syntax(" #generate\n");
×
1397
         }
1398
         else {
1399
            tab(indent);
×
1400
            print_syntax("#else\n");
×
1401
         }
1402
         for (unsigned i = 0; i < tree_stmts(c); i++)
×
1403
            dump_stmt(tree_stmt(c, i), indent + 2);
×
1404
      }
1405
      tab(indent);
×
1406
      print_syntax("#end #generate");
×
1407
      break;
×
1408

1409
   case T_INSTANCE:
1✔
1410
      dump_instance(t, indent);
1✔
1411
      return;
1✔
1412

1413
   case T_NEXT:
×
1414
      print_syntax("#next");
×
1415
      if (tree_has_value(t)) {
×
1416
         print_syntax(" #when ");
×
1417
         dump_expr(tree_value(t));
×
1418
      }
1419
      break;
1420

1421
   case T_NULL:
×
1422
      print_syntax("#null");
×
1423
      break;
×
1424

1425
   case T_COND_ASSIGN:
1✔
1426
      dump_expr(tree_target(t));
1✔
1427
      print_syntax(" <= ");
1✔
1428
      if (tree_has_guard(t))
1✔
1429
         print_syntax("#guarded ");
×
1430
      for (int i = 0; i < tree_conds(t); i++) {
2✔
1431
         tree_t c = tree_cond(t, i);
1✔
1432
         dump_waveforms(tree_stmt(c, 0));
1✔
1433
         if (tree_has_value(c)) {
1✔
1434
            print_syntax(" #when ");
×
1435
            dump_expr(tree_value(c));
×
1436
         }
1437
      }
1438
      break;
1439

1440
   case T_SELECT:
×
1441
      print_syntax(" <= ");
×
1442
      if (tree_has_guard(t)) print_syntax("#guarded ");
×
1443
      color_printf("$red$/* TODO: T_SELECT */$$");
×
1444
      break;
×
1445

1446
   case T_CONCURRENT:
3✔
1447
      if (tree_flags(t) & TREE_F_POSTPONED)
3✔
1448
         print_syntax("#postponed ");
×
1449
      dump_stmt(tree_stmt(t, 0), 0);
3✔
1450
      return;
3✔
1451

1452
   case T_DUMMY_DRIVER:
×
1453
      print_syntax("-- dummy driver for ");
×
1454
      dump_expr(tree_target(t));
×
1455
      print_syntax("\n");
×
1456
      return;
×
1457

1458
   case T_PSL_DIRECT:
1✔
1459
      dump_psl(t, indent);
1✔
1460
      break;
1✔
1461

1462
   default:
×
1463
      cannot_dump(t, "stmt");
×
1464
   }
1465

1466
   print_syntax(";\n");
14✔
1467
}
1468

1469
static void dump_port(tree_t t, int indent)
103✔
1470
{
1471
   tab(indent);
103✔
1472
   dump_address(t);
103✔
1473

1474
   if (tree_flags(t) & TREE_F_PREDEFINED)
103✔
1475
      print_syntax("-- predefined ");
80✔
1476

1477
   const class_t class = tree_class(t);
103✔
1478
   print_syntax("#%s %s", class_str(class), istr(tree_ident(t)));
103✔
1479

1480
   const port_mode_t mode = tree_subkind(t);
103✔
1481

1482
   type_t type = get_type_or_null(t);
103✔
1483
   if (class == C_PACKAGE) {
103✔
1484
      print_syntax(" #is #new ");
×
1485
      dump_expr(tree_value(t));
×
1486
   }
1487
   else if (class == C_TYPE && type_kind(type) == T_GENERIC) {
103✔
1488
      print_syntax(" #is ");
10✔
1489

1490
      switch (type_subkind(type)) {
10✔
1491
      case GTYPE_PRIVATE:
2✔
1492
         print_syntax("#private");
2✔
1493
         break;
2✔
1494
      case GTYPE_SCALAR:
1✔
1495
         print_syntax("<>");
1✔
1496
         break;
1✔
1497
      case GTYPE_DISCRETE:
1✔
1498
         print_syntax("(<>)");
1✔
1499
         break;
1✔
1500
      case GTYPE_INTEGER:
1✔
1501
         print_syntax("#range <>");
1✔
1502
         break;
1✔
1503
      case GTYPE_PHYSICAL:
1✔
1504
         print_syntax("#units <>");
1✔
1505
         break;
1✔
1506
      case GTYPE_FLOATING:
1✔
1507
         print_syntax("#range <> . <>");
1✔
1508
         break;
1✔
1509
      case GTYPE_ARRAY:
1✔
1510
         {
1511
            print_syntax("#array (");
1✔
1512
            const int nindex = type_indexes(type);
1✔
1513
            for (int i = 0; i < nindex; i++) {
2✔
1514
               if (i > 0) print_syntax(", ");
1✔
1515
               dump_type(type_index(type, i));
1✔
1516
               print_syntax(" #range <>");
1✔
1517
            }
1518
            print_syntax(") #of ");
1✔
1519
            dump_type(type_elem(type));
1✔
1520
         }
1521
         break;
1✔
1522
      case GTYPE_ACCESS:
1✔
1523
         print_syntax("#access ..");
1✔
1524
         break;
1✔
1525
      case GTYPE_FILE:
1✔
1526
         print_syntax("#file #of ..");
1✔
1527
         break;
1✔
1528
      }
1529
   }
1530
   else if (mode == PORT_ARRAY_VIEW || mode == PORT_RECORD_VIEW) {
93✔
1531
      print_syntax(" : #view ");
2✔
1532
      dump_expr(tree_value(t));
2✔
1533
   }
1534
   else {
1535
      static const char *map[] = {
91✔
1536
         "??", "in", "out", "inout", "buffer", "linkage"
1537
      };
1538
      print_syntax(" : #%s ", map[mode]);
91✔
1539
      dump_type(type);
91✔
1540

1541
      if (tree_has_value(t)) {
91✔
1542
         print_syntax(" := ");
81✔
1543
         dump_expr(tree_value(t));
81✔
1544
      }
1545
   }
1546
}
103✔
1547

1548
static void dump_context(tree_t t, int indent)
7✔
1549
{
1550
   const int nctx = tree_contexts(t);
7✔
1551
   for (int i = 0; i < nctx; i++) {
28✔
1552
      tree_t c = tree_context(t, i);
21✔
1553
      switch (tree_kind(c)) {
21✔
1554
      case T_LIBRARY:
14✔
1555
         switch (is_well_known(tree_ident(c))) {
14✔
1556
         case W_STD:
1557
         case W_WORK:
1558
            break;
1559
         default:
×
1560
            print_syntax("#library %s;\n", istr(tree_ident(c)));
×
1561
         }
1562
         break;
1563

1564
      case T_USE:
7✔
1565
         dump_use(c);
7✔
1566
         break;
7✔
1567

1568
      case T_CONTEXT_REF:
×
1569
         print_syntax("#context %s;\n", istr(tree_ident(t)));
×
1570
         break;
×
1571

1572
      default:
1573
         break;
1574
      }
1575

1576
      tab(indent);
21✔
1577
   }
1578

1579
   if (nctx > 0) {
7✔
1580
      print_syntax("\n");
7✔
1581
      tab(indent);
7✔
1582
   }
1583
}
7✔
1584

1585
static void dump_elab(tree_t t)
×
1586
{
1587
   dump_context(t, 0);
×
1588
   dump_address(t);
×
1589
   print_syntax("#entity %s #is\n#end #entity;\n\n", istr(tree_ident(t)));
×
1590
   print_syntax("#architecture #elab #of %s #is\n", istr(tree_ident(t)));
×
1591
   dump_decls(t, 2);
×
1592
   print_syntax("#begin\n");
×
1593
   for (unsigned i = 0; i < tree_stmts(t); i++)
×
1594
      dump_stmt(tree_stmt(t, i), 2);
×
1595
   print_syntax("#end #architecture;\n\n");
×
1596
}
×
1597

1598
static void dump_entity(tree_t t)
2✔
1599
{
1600
   dump_context(t, 0);
2✔
1601
   dump_address(t);
2✔
1602
   print_syntax("#entity %s #is\n", istr(tree_ident(t)));
2✔
1603
   dump_generics(t, 2, ";\n");
2✔
1604
   dump_ports(t, 2);
2✔
1605
   dump_decls(t, 2);
2✔
1606
   if (tree_stmts(t) > 0) {
2✔
1607
      print_syntax("#begin\n");
×
1608
      for (unsigned i = 0; i < tree_stmts(t); i++) {
×
1609
         dump_stmt(tree_stmt(t, i), 2);
×
1610
      }
1611
   }
1612
   print_syntax("#end #entity;\n\n");
2✔
1613
}
2✔
1614

1615
static void dump_decls(tree_t t, int indent)
18✔
1616
{
1617
   const int ndecls = tree_decls(t);
18✔
1618
   bool was_body = false;
18✔
1619
   for (unsigned i = 0; i < ndecls; i++) {
41✔
1620
      tree_t d = tree_decl(t, i);
23✔
1621
      tree_kind_t dkind = tree_kind(d);
23✔
1622
      const bool is_body = dkind == T_FUNC_BODY || dkind == T_PROT_BODY
46✔
1623
         || dkind == T_PROC_BODY;
23✔
1624
      if ((was_body && !is_body) || (is_body && i > 0))
23✔
1625
         print_syntax("\n");
5✔
1626
      was_body = is_body;
23✔
1627
      dump_decl(d, indent);
23✔
1628
   }
1629
}
18✔
1630

1631
static void dump_arch(tree_t t)
2✔
1632
{
1633
   dump_context(t, 0);
2✔
1634
   dump_address(t);
2✔
1635
   print_syntax("#architecture %s #of %s #is\n",
2✔
1636
                istr(tree_ident(t)), istr(tree_ident2(t)));
1637
   dump_decls(t, 2);
2✔
1638
   print_syntax("#begin\n");
2✔
1639
   dump_stmts(t, 2);
2✔
1640
   print_syntax("#end #architecture;\n\n");
2✔
1641
}
2✔
1642

1643
static void dump_package(tree_t t, int indent)
1✔
1644
{
1645
   dump_context(t, indent);
1✔
1646
   dump_address(t);
1✔
1647
   print_syntax("#package %s #is\n", istr(tree_ident(t)));
1✔
1648
   if (tree_kind(t) == T_PACK_INST && tree_has_ref(t)) {
1✔
1649
      tab(indent);
×
1650
      print_syntax("  -- Instantiated from %s\n", istr(tree_ident(tree_ref(t))));
×
1651
   }
1652
   dump_generics(t, indent + 2, ";\n");
1✔
1653
   dump_generic_map(t, indent + 2, ";\n");
1✔
1654
   dump_decls(t, indent + 2);
1✔
1655
   tab(indent);
1✔
1656
   print_syntax("#end #package;\n\n");
1✔
1657
}
1✔
1658

1659
static void dump_package_body(tree_t t, int indent)
2✔
1660
{
1661
   dump_context(t, indent);
2✔
1662
   dump_address(t);
2✔
1663
   print_syntax("#package #body %s #is\n", istr(tree_ident(t)));
2✔
1664
   dump_decls(t, indent + 2);
2✔
1665
   tab(indent);
2✔
1666
   print_syntax("#end #package #body;\n\n");
2✔
1667
}
2✔
1668

1669
static void dump_configuration(tree_t t)
×
1670
{
1671
   dump_address(t);
×
1672
   print_syntax("#configuration %s #of %s #is\n",
×
1673
          istr(tree_ident(t)), istr(tree_ident2(t)));
1674
   dump_decls(t, 2);
×
1675
   print_syntax("#end #configuration\n");
×
1676
}
×
1677

1678
void vhdl_dump(tree_t t, int indent)
48✔
1679
{
1680
   switch (tree_kind(t)) {
48✔
1681
   case T_ELAB:
×
1682
      dump_elab(t);
×
1683
      break;
×
1684
   case T_ENTITY:
2✔
1685
      dump_entity(t);
2✔
1686
      break;
2✔
1687
   case T_ARCH:
2✔
1688
      dump_arch(t);
2✔
1689
      break;
2✔
1690
   case T_PACKAGE:
1✔
1691
   case T_PACK_INST:
1692
      dump_package(t, indent);
1✔
1693
      break;
1✔
1694
   case T_PACK_BODY:
2✔
1695
      dump_package_body(t, indent);
2✔
1696
      break;
2✔
1697
   case T_CONFIGURATION:
×
1698
      dump_configuration(t);
×
1699
      break;
×
1700
   case T_REF:
25✔
1701
   case T_FCALL:
1702
   case T_PROT_FCALL:
1703
   case T_LITERAL:
1704
   case T_AGGREGATE:
1705
   case T_ARRAY_REF:
1706
   case T_ARRAY_SLICE:
1707
   case T_TYPE_CONV:
1708
   case T_RECORD_REF:
1709
   case T_ATTR_REF:
1710
   case T_CONV_FUNC:
1711
   case T_QUALIFIED:
1712
   case T_EXTERNAL_NAME:
1713
   case T_STRING:
1714
   case T_PACKAGE_MAP:
1715
   case T_TYPE_REF:
1716
      dump_expr(t);
25✔
1717
      break;
25✔
1718
   case T_INSTANCE:
6✔
1719
   case T_FOR_GENERATE:
1720
   case T_CASE_GENERATE:
1721
   case T_BLOCK:
1722
   case T_PROCESS:
1723
   case T_CASE:
1724
   case T_FOR:
1725
   case T_SIGNAL_ASSIGN:
1726
   case T_IF:
1727
   case T_WAIT:
1728
   case T_PSL_DIRECT:
1729
   case T_VAR_ASSIGN:
1730
   case T_RETURN:
1731
   case T_ASSERT:
1732
   case T_WHILE:
1733
   case T_LOOP:
1734
   case T_SEQUENCE:
1735
   case T_PCALL:
1736
      dump_stmt(t, indent);
6✔
1737
      break;
6✔
1738
   case T_CONST_DECL:
10✔
1739
   case T_VAR_DECL:
1740
   case T_SIGNAL_DECL:
1741
   case T_TYPE_DECL:
1742
   case T_FIELD_DECL:
1743
   case T_FUNC_DECL:
1744
   case T_PROC_BODY:
1745
   case T_FUNC_BODY:
1746
   case T_PROC_DECL:
1747
   case T_ATTR_DECL:
1748
   case T_ATTR_SPEC:
1749
   case T_ENUM_LIT:
1750
   case T_COMPONENT:
1751
   case T_BLOCK_CONFIG:
1752
   case T_SPEC:
1753
   case T_ALIAS:
1754
   case T_FUNC_INST:
1755
   case T_PROC_INST:
1756
   case T_SUBTYPE_DECL:
1757
   case T_VIEW_DECL:
1758
      dump_decl(t, indent);
10✔
1759
      break;
10✔
1760
   case T_PORT_DECL:
×
1761
   case T_GENERIC_DECL:
1762
   case T_PARAM_DECL:
1763
      dump_port(t, indent);
×
1764
      break;
×
1765
   case T_RANGE:
×
1766
      dump_range(t);
×
1767
      break;
×
1768
   case T_BINDING:
×
1769
      dump_binding(t, indent);
×
1770
      break;
×
1771
   case T_PARAM:
×
1772
      dump_param(t);
×
1773
      break;
×
1774
   case T_CONSTRAINT:
×
1775
      dump_constraint(t);
×
1776
      break;
×
1777
   case T_ELEM_CONSTRAINT:
×
1778
      dump_record_elem_constraint(t);
×
1779
      break;
×
1780
   case T_ALTERNATIVE:
×
1781
      dump_alternative(t, indent);
×
1782
      break;
×
1783
   default:
×
1784
      cannot_dump(t, "tree");
×
1785
   }
1786
}
48✔
1787

1788
void dump(tree_t t)
21✔
1789
{
1790
   vhdl_dump(t, 0);
21✔
1791
   print_syntax("\r");
21✔
1792
}
21✔
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