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

nickg / nvc / 6685681445

29 Oct 2023 08:42PM UTC coverage: 91.241% (+0.02%) from 91.224%
6685681445

push

github

nickg
Check relative pathname for enclosing concurrent region

8 of 8 new or added lines in 2 files covered. (100.0%)

49670 of 54438 relevant lines covered (91.24%)

588510.75 hits per line

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

57.22
/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)
202✔
52
{
53
   print_syntax("%*s", indent, "");
202✔
54
}
202✔
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_external_name(tree_t t)
1✔
162
{
163
   print_syntax("<< #%s ", class_str(tree_class(t)));
1✔
164
   const int nparts = tree_parts(t);
1✔
165
   for (int i = 0; i < nparts; i++) {
4✔
166
      tree_t part = tree_part(t, i);
3✔
167
      switch (tree_subkind(part)) {
3✔
168
      case PE_ABSOLUTE:
1✔
169
         print_syntax(".");
1✔
170
         break;
1✔
171
      case PE_SIMPLE:
2✔
172
         print_syntax("%s%s", istr(tree_ident(part)),
2✔
173
                      i + 1 < nparts ? "." : "");
2✔
174
         break;
2✔
175
      case PE_CARET:
×
176
         print_syntax("^.");
×
177
         break;
×
178
      }
179
   }
3✔
180
   print_syntax(" : ");
1✔
181
   dump_type(tree_type(t));
1✔
182
   print_syntax(" >>");
1✔
183
}
1✔
184

185
static void dump_expr(tree_t t)
163✔
186
{
187
   switch (tree_kind(t)) {
163✔
188
   case T_PROT_FCALL:
1✔
189
      dump_expr(tree_name(t));
1✔
190
      print_syntax(".");
1✔
191
      // Fall-through
192
   case T_FCALL:
10✔
193
      if (tree_has_ref(t)) {
10✔
194
         tree_t decl = tree_ref(t);
10✔
195
         dump_address(decl);
10✔
196
         print_syntax("%s", istr(tree_ident(decl)));
10✔
197
      }
198
      else
199
         print_syntax("%s", istr(tree_ident(t)));
×
200
      dump_params(t, tree_param, tree_params(t), NULL);
10✔
201
#if DUMP_STATICNESS
202
      if (tree_flags(t) & TREE_F_LOCALLY_STATIC)
203
         color_printf("$red$/* locally static */$$");
204
      else if (tree_flags(t) & TREE_F_GLOBALLY_STATIC)
205
         color_printf("$red$/* globally static */$$");
206
#endif
207
      break;
10✔
208

209
   case T_CONV_FUNC:
×
210
      if (tree_has_ref(t)) {
×
211
         tree_t decl = tree_ref(t);
×
212
         dump_address(decl);
×
213
         print_syntax("%s", istr(tree_ident(decl)));
×
214
      }
215
      else
216
         print_syntax("%s", istr(tree_ident(t)));
×
217
      print_syntax("(");
×
218
      dump_expr(tree_value(t));
×
219
      print_syntax(")");
×
220
      break;
×
221

222
   case T_LITERAL:
29✔
223
      switch (tree_subkind(t)) {
29✔
224
      case L_INT:
23✔
225
         print_syntax("%"PRIi64, tree_ival(t));
23✔
226
         break;
23✔
227
      case L_PHYSICAL:
4✔
228
         if (tree_has_ident(t))
4✔
229
            print_syntax("%"PRIi64" %s", tree_ival(t), istr(tree_ident(t)));
4✔
230
         else
231
            print_syntax("%"PRIi64, tree_ival(t));
×
232
         break;
233
      case L_REAL:
2✔
234
         print_syntax("%lf", tree_dval(t));
2✔
235
         break;
2✔
236
      case L_NULL:
×
237
         print_syntax("#null");
×
238
         break;
×
239
      default:
240
         assert(false);
×
241
      }
242
      break;
243

244
   case T_STRING:
1✔
245
      {
246
         print_syntax("\"");
1✔
247
         const int nchars = tree_chars(t);
1✔
248
         for (int i = 0; i < nchars; i++) {
7✔
249
            ident_t rune = tree_ident(tree_char(t, i));
6✔
250
            if (ident_char(rune, 0) == '\'')
6✔
251
               print_syntax("%c", ident_char(rune, 1));
6✔
252
            else
253
               print_syntax("\" & %s & \"", istr(rune));
×
254
         }
255
         print_syntax("\"");
1✔
256
      }
257
      break;
1✔
258

259
   case T_NEW:
×
260
      print_syntax("#new ");
×
261
      dump_expr(tree_value(t));
×
262
      break;
×
263

264
   case T_ALL:
×
265
      if (tree_has_value(t)) {
×
266
         dump_expr(tree_value(t));
×
267
         print_syntax(".#all");
×
268
      }
269
      else
270
         print_syntax("#all");
×
271
      break;
272

273
   case T_TYPE_REF:
1✔
274
      dump_type(tree_type(t));
1✔
275
      break;
1✔
276

277
   case T_AGGREGATE:
3✔
278
      print_syntax("(");
3✔
279
      for (unsigned i = 0; i < tree_assocs(t); i++) {
7✔
280
         if (i > 0)
4✔
281
            print_syntax(", ");
1✔
282
         tree_t a = tree_assoc(t, i);
4✔
283
         tree_t value = tree_value(a);
4✔
284
         switch (tree_subkind(a)) {
4✔
285
         case A_POS:
×
286
            dump_expr(value);
×
287
            break;
×
288
         case A_NAMED:
1✔
289
            dump_expr(tree_name(a));
1✔
290
            print_syntax(" => ");
1✔
291
            dump_expr(value);
1✔
292
            break;
1✔
293
         case A_OTHERS:
3✔
294
            print_syntax("#others => ");
3✔
295
            dump_expr(value);
3✔
296
            break;
3✔
297
         case A_RANGE:
×
298
            dump_range(tree_range(a, 0));
×
299
            print_syntax(" => ");
×
300
            dump_expr(value);
×
301
            break;
×
302
         default:
303
            assert(false);
×
304
         }
305
      }
306
      print_syntax(")");
3✔
307
      break;
3✔
308

309
   case T_REF:
37✔
310
      if (tree_has_ref(t)) {
37✔
311
         tree_t decl = tree_ref(t);
37✔
312
         dump_address(decl);
37✔
313
         print_syntax("%s", istr(tree_ident(decl)));
37✔
314
      }
315
      else
316
         print_syntax("%s", istr(tree_ident(t)));
×
317
      break;
318

319
   case T_ATTR_REF:
1✔
320
      dump_expr(tree_name(t));
1✔
321
      print_syntax("'%s", istr(tree_ident(t)));
1✔
322
      if (tree_params(t) > 0)
1✔
323
         dump_params(t, tree_param, tree_params(t), NULL);
×
324
      break;
325

326
   case T_EXTERNAL_NAME:
1✔
327
      dump_external_name(t);
1✔
328
      break;
1✔
329

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

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

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

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

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

363
   case T_OPEN:
1✔
364
      print_syntax("#open");
1✔
365
      break;
1✔
366

367
   case T_BOX:
78✔
368
      print_syntax("<>");
78✔
369
      break;
78✔
370

371
   case T_WAVEFORM:
×
372
      dump_waveform(t);
×
373
      break;
×
374

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

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

406
   default:
×
407
      cannot_dump(t, "expr");
×
408
   }
409

410
   dump_type_hint(t);
163✔
411
}
163✔
412

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

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

422
   dump_elem_constraints(ftype);
×
423
}
×
424

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

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

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

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

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

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

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

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

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

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

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

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

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

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

617
   print_syntax("#type %s", istr(tree_ident(t)));
2✔
618

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

624
   print_syntax(" #is ");
2✔
625

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

699
   print_syntax(";\n");
2✔
700
}
701

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

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

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

717
   dump_elem_constraints(type);
718

719
   print_syntax(";\n");
720
}
721

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

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

738
static void dump_decl(tree_t t, int indent)
26✔
739
{
740
   tab(indent);
26✔
741
   if (tree_kind(t) != T_HIER) dump_address(t);
26✔
742

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

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

755
   case T_CONST_DECL:
3✔
756
      print_syntax("#constant %s : ", istr(tree_ident(t)));
3✔
757
      break;
3✔
758

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

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

768
   case T_TYPE_DECL:
2✔
769
      dump_type_decl(t, indent);
2✔
770
      return;
2✔
771

772
   case T_SUBTYPE_DECL:
1✔
773
      dump_subtype_decl(t, indent);
1✔
774
      return;
1✔
775

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

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

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

799
   case T_ALIAS:
×
800
      if (tree_flags(t) & TREE_F_PREDEFINED)
×
801
         print_syntax("-- predefined ");
×
802
      print_syntax("#alias %s", istr(tree_ident(t)));
×
803
      if (tree_has_type(t)) {
×
804
         print_syntax(" : ");
×
805
         dump_type(tree_type(t));
×
806
      }
807
      print_syntax(" #is ");
×
808
      dump_expr(tree_value(t));
×
809
      print_syntax(";\n");
×
810
      return;
×
811

812
   case T_ATTR_SPEC:
1✔
813
      print_syntax("#attribute %s #of %s : #%s #is ", istr(tree_ident(t)),
1✔
814
             istr(tree_ident2(t)), class_str(tree_class(t)));
815
      dump_expr(tree_value(t));
1✔
816
      print_syntax(";\n");
1✔
817
      return;
1✔
818

819
   case T_ATTR_DECL:
1✔
820
      print_syntax("#attribute %s : ", istr(tree_ident(t)));
1✔
821
      dump_type(tree_type(t));
1✔
822
      print_syntax(";\n");
1✔
823
      return;
1✔
824

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

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

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

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

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

912
   case T_COMPONENT:
1✔
913
      dump_component(t, indent);
1✔
914
      return;
1✔
915

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

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

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

942
   case T_USE:
×
943
      dump_use(t);
×
944
      return;
×
945

946
   case T_PACKAGE:
×
947
   case T_PACK_INST:
948
      dump_package(t, indent);
×
949
      return;
×
950

951
   case T_PACK_BODY:
×
952
      dump_package_body(t, indent);
×
953
      return;
×
954

955
   default:
×
956
      cannot_dump(t, "decl");
×
957
   }
958

959
   dump_type(tree_type(t));
12✔
960

961
   if (tree_kind(t) != T_FIELD_DECL && tree_has_value(t)) {
12✔
962
      print_syntax(" := ");
6✔
963
      dump_expr(tree_value(t));
6✔
964
   }
965
   print_syntax(";\n");
12✔
966
}
967

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

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

1011
static void dump_psl(tree_t t, int indent)
1012
{
1013
   if (standard() < STD_08)
1014
      print_syntax("-- psl ");
1015
   psl_dump(tree_psl(t));
1016
}
1017

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

1027
   print_syntax("%s", istr(tree_ident2(t)));
1✔
1028

1029
   const int nparams = tree_params(t);
1✔
1030
   const int ngenmaps = tree_genmaps(t);
1✔
1031

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

1046
   dump_generic_map(t, indent + 2, nparams > 0 ? "\n" : "");
1✔
1047
   dump_port_map(t, indent + 2, "");
1✔
1048
   print_syntax(";\n");
1✔
1049
}
1✔
1050

1051
static void dump_stmt(tree_t t, int indent)
13✔
1052
{
1053
   tab(indent);
15✔
1054

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

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

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

1098
   case T_FORCE:
×
1099
      dump_expr(tree_target(t));
×
1100
      print_syntax(" <= #force ");
×
1101
      dump_expr(tree_value(t));
×
1102
      break;
×
1103

1104
   case T_RELEASE:
×
1105
      dump_expr(tree_target(t));
×
1106
      print_syntax(" <= #release");
×
1107
      break;
×
1108

1109
   case T_VAR_ASSIGN:
2✔
1110
      dump_expr(tree_target(t));
2✔
1111
      print_syntax(" := ");
2✔
1112
      dump_expr(tree_value(t));
2✔
1113
      break;
2✔
1114

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

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

1151
   case T_SEQUENCE:
×
1152
      print_syntax("#block #is\n");
×
1153
      dump_block(t, indent);
×
1154
      tab(indent);
×
1155
      print_syntax("#end #block;\n");
×
1156
      return;
×
1157

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

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

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

1205
   case T_EXIT:
×
1206
      print_syntax("#exit %s", istr(tree_ident2(t)));
×
1207
      if (tree_has_value(t)) {
×
1208
         print_syntax(" #when ");
×
1209
         dump_expr(tree_value(t));
×
1210
      }
1211
      break;
1212

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

1228
   case T_RETURN:
3✔
1229
      print_syntax("#return");
3✔
1230
      if (tree_has_value(t)) {
3✔
1231
         print_syntax(" ");
3✔
1232
         dump_expr(tree_value(t));
3✔
1233
      }
1234
      break;
1235

1236
   case T_COND_RETURN:
×
1237
      print_syntax("#return #when ");
×
1238
      dump_expr(tree_value(t));
×
1239
      break;
×
1240

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

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

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

1277
   case T_CASE_GENERATE:
×
1278
      {
1279
         print_syntax("#case ");
×
1280
         dump_expr(tree_value(t));
×
1281
         print_syntax(" #generate\n");
×
1282

1283
         const int nstmts = tree_stmts(t);
×
1284
         for (int i = 0; i < nstmts; i++)
×
1285
            dump_alternative(tree_stmt(t, i), indent + 2);
×
1286

1287
         print_syntax("#end #generate");
×
1288
         tab(indent);
×
1289
      }
1290
      break;
×
1291

1292
   case T_IF_GENERATE:
1293
      for (unsigned i = 0; i < tree_conds(t); i++) {
×
1294
         tree_t c = tree_cond(t, i);
×
1295
         if (tree_has_value(c)) {
×
1296
            if (i > 0)
×
1297
               tab(indent);
×
1298
            print_syntax(i > 0 ? "#elsif " : "#if ");
×
1299
            dump_expr(tree_value(c));
×
1300
            print_syntax(" #generate\n");
×
1301
         }
1302
         else {
1303
            tab(indent);
×
1304
            print_syntax("#else\n");
×
1305
         }
1306
         for (unsigned i = 0; i < tree_stmts(c); i++)
×
1307
            dump_stmt(tree_stmt(c, i), indent + 2);
×
1308
      }
1309
      tab(indent);
×
1310
      print_syntax("#end #generate");
×
1311
      break;
×
1312

1313
   case T_INSTANCE:
1✔
1314
      dump_instance(t, indent);
1✔
1315
      return;
1✔
1316

1317
   case T_NEXT:
×
1318
      print_syntax("#next");
×
1319
      if (tree_has_value(t)) {
×
1320
         print_syntax(" #when ");
×
1321
         dump_expr(tree_value(t));
×
1322
      }
1323
      break;
1324

1325
   case T_NULL:
×
1326
      print_syntax("#null");
×
1327
      break;
×
1328

1329
   case T_COND_ASSIGN:
1✔
1330
      dump_expr(tree_target(t));
1✔
1331
      print_syntax(" <= ");
1✔
1332
      for (int i = 0; i < tree_conds(t); i++) {
2✔
1333
         tree_t c = tree_cond(t, i);
1✔
1334
         dump_waveforms(tree_stmt(c, 0));
1✔
1335
         if (tree_has_value(c)) {
1✔
1336
            print_syntax(" #when ");
×
1337
            dump_expr(tree_value(c));
×
1338
         }
1339
      }
1340
      break;
1341

1342
   case T_SELECT:
×
1343
      print_syntax(" <= ");
×
1344
      if (tree_has_guard(t)) print_syntax("#guarded ");
×
1345
      color_printf("$red$/* TODO: T_SELECT */$$");
×
1346
      break;
×
1347

1348
   case T_CONCURRENT:
2✔
1349
      if (tree_flags(t) & TREE_F_POSTPONED)
2✔
1350
         print_syntax("#postponed ");
×
1351
      if (tree_has_guard(t))
2✔
1352
         print_syntax("#guarded ");
×
1353
      dump_stmt(tree_stmt(t, 0), 0);
2✔
1354
      return;
2✔
1355

1356
   case T_PSL:
×
1357
      dump_psl(t, 0);
×
1358
      break;
×
1359

1360
   case T_VERILOG:
×
1361
      print_syntax("#block #is\n");
×
1362
      dump_generic_map(t, indent + 2, ";\n");
×
1363
      dump_port_map(t, indent + 2, ";\n");
×
1364
      tab(indent);
×
1365
      print_syntax("#begin\n");
×
1366
      vlog_dump(tree_vlog(t), indent + 2);
×
1367
      tab(indent);
×
1368
      print_syntax("#end #block");
×
1369
      break;
×
1370

1371
   default:
×
1372
      cannot_dump(t, "stmt");
×
1373
   }
1374

1375
   print_syntax(";\n");
10✔
1376
}
1377

1378
static void dump_port(tree_t t, int indent)
100✔
1379
{
1380
   tab(indent);
100✔
1381
   dump_address(t);
100✔
1382

1383
   if (tree_flags(t) & TREE_F_PREDEFINED)
100✔
1384
      print_syntax("-- predefined ");
80✔
1385

1386
   const class_t class = tree_class(t);
100✔
1387
   print_syntax("#%s %s", class_str(class), istr(tree_ident(t)));
100✔
1388

1389
   type_t type = get_type_or_null(t);
100✔
1390
   if (class == C_PACKAGE) {
100✔
1391
      print_syntax(" #is #new ");
×
1392
      dump_expr(tree_value(t));
×
1393
   }
1394
   else if (class == C_TYPE && type_kind(type) == T_GENERIC) {
100✔
1395
      print_syntax(" #is ");
10✔
1396

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

1449
      if (tree_has_value(t)) {
90✔
1450
         print_syntax(" := ");
79✔
1451
         dump_expr(tree_value(t));
79✔
1452
      }
1453
   }
1454
}
100✔
1455

1456
static void dump_context(tree_t t, int indent)
6✔
1457
{
1458
   const int nctx = tree_contexts(t);
6✔
1459
   for (int i = 0; i < nctx; i++) {
24✔
1460
      tree_t c = tree_context(t, i);
18✔
1461
      switch (tree_kind(c)) {
18✔
1462
      case T_LIBRARY:
12✔
1463
         switch (is_well_known(tree_ident(c))) {
12✔
1464
         case W_STD:
1465
         case W_WORK:
1466
            break;
1467
         default:
×
1468
            print_syntax("#library %s;\n", istr(tree_ident(c)));
×
1469
         }
1470
         break;
1471

1472
      case T_USE:
6✔
1473
         dump_use(c);
6✔
1474
         break;
6✔
1475

1476
      case T_CONTEXT_REF:
×
1477
         print_syntax("#context %s;\n", istr(tree_ident(t)));
×
1478
         break;
×
1479

1480
      default:
1481
         break;
1482
      }
1483

1484
      tab(indent);
18✔
1485
   }
1486

1487
   if (nctx > 0) {
6✔
1488
      print_syntax("\n");
6✔
1489
      tab(indent);
6✔
1490
   }
1491
}
6✔
1492

1493
static void dump_elab(tree_t t)
×
1494
{
1495
   dump_context(t, 0);
×
1496
   dump_address(t);
×
1497
   print_syntax("#entity %s #is\n#end #entity;\n\n", istr(tree_ident(t)));
×
1498
   print_syntax("#architecture #elab #of %s #is\n", istr(tree_ident(t)));
×
1499
   dump_decls(t, 2);
×
1500
   print_syntax("#begin\n");
×
1501
   for (unsigned i = 0; i < tree_stmts(t); i++)
×
1502
      dump_stmt(tree_stmt(t, i), 2);
×
1503
   print_syntax("#end #architecture;\n\n");
×
1504
}
×
1505

1506
static void dump_entity(tree_t t)
2✔
1507
{
1508
   dump_context(t, 0);
2✔
1509
   dump_address(t);
2✔
1510
   print_syntax("#entity %s #is\n", istr(tree_ident(t)));
2✔
1511
   dump_generics(t, 2, ";\n");
2✔
1512
   dump_ports(t, 2);
2✔
1513
   dump_decls(t, 2);
2✔
1514
   if (tree_stmts(t) > 0) {
2✔
1515
      print_syntax("#begin\n");
×
1516
      for (unsigned i = 0; i < tree_stmts(t); i++) {
×
1517
         dump_stmt(tree_stmt(t, i), 2);
×
1518
      }
1519
   }
1520
   print_syntax("#end #entity;\n\n");
2✔
1521
}
2✔
1522

1523
static void dump_decls(tree_t t, int indent)
15✔
1524
{
1525
   const int ndecls = tree_decls(t);
15✔
1526
   bool was_body = false;
15✔
1527
   for (unsigned i = 0; i < ndecls; i++) {
31✔
1528
      tree_t d = tree_decl(t, i);
16✔
1529
      tree_kind_t dkind = tree_kind(d);
16✔
1530
      const bool is_body = dkind == T_FUNC_BODY || dkind == T_PROT_BODY
32✔
1531
         || dkind == T_PROC_BODY;
16✔
1532
      if ((was_body && !is_body) || (is_body && i > 0))
16✔
1533
         print_syntax("\n");
4✔
1534
      was_body = is_body;
16✔
1535
      dump_decl(d, indent);
16✔
1536
   }
1537
}
15✔
1538

1539
static void dump_arch(tree_t t)
1✔
1540
{
1541
   dump_context(t, 0);
1✔
1542
   dump_address(t);
1✔
1543
   print_syntax("#architecture %s #of %s #is\n",
1✔
1544
                istr(tree_ident(t)), istr(tree_ident2(t)));
1545
   dump_decls(t, 2);
1✔
1546
   print_syntax("#begin\n");
1✔
1547
   dump_stmts(t, 2);
1✔
1548
   print_syntax("#end #architecture;\n\n");
1✔
1549
}
1✔
1550

1551
static void dump_package(tree_t t, int indent)
1✔
1552
{
1553
   dump_context(t, indent);
1✔
1554
   dump_address(t);
1✔
1555
   print_syntax("#package %s #is\n", istr(tree_ident(t)));
1✔
1556
   if (tree_kind(t) == T_PACK_INST && tree_has_ref(t)) {
1✔
1557
      tab(indent);
×
1558
      print_syntax("  -- Instantiated from %s\n", istr(tree_ident(tree_ref(t))));
×
1559
   }
1560
   dump_generics(t, indent + 2, ";\n");
1✔
1561
   dump_generic_map(t, indent + 2, ";\n");
1✔
1562
   dump_decls(t, indent + 2);
1✔
1563
   tab(indent);
1✔
1564
   print_syntax("#end #package;\n\n");
1✔
1565
}
1✔
1566

1567
static void dump_package_body(tree_t t, int indent)
2✔
1568
{
1569
   dump_context(t, indent);
2✔
1570
   dump_address(t);
2✔
1571
   print_syntax("#package #body %s #is\n", istr(tree_ident(t)));
2✔
1572
   dump_decls(t, indent + 2);
2✔
1573
   tab(indent);
2✔
1574
   print_syntax("#end #package #body;\n\n");
2✔
1575
}
2✔
1576

1577
static void dump_configuration(tree_t t)
×
1578
{
1579
   dump_address(t);
×
1580
   print_syntax("#configuration %s #of %s #is\n",
×
1581
          istr(tree_ident(t)), istr(tree_ident2(t)));
1582
   dump_decls(t, 2);
×
1583
   print_syntax("#end #configuration\n");
×
1584
}
×
1585

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

1688
void dump(tree_t t)
18✔
1689
{
1690
   vhdl_dump(t, 0);
18✔
1691
   print_syntax("\r");
18✔
1692
}
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