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

nickg / nvc / 9846914072

08 Jul 2024 09:05PM UTC coverage: 91.578% (+0.02%) from 91.561%
9846914072

push

github

nickg
Dump package signals to FST file. Fixes #901

49 of 50 new or added lines in 2 files covered. (98.0%)

578 existing lines in 10 files now uncovered.

56932 of 62168 relevant lines covered (91.58%)

669358.57 hits per line

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

94.55
/src/common.c
1
//
2
//  Copyright (C) 2013-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 "diag.h"
21
#include "hash.h"
22
#include "ident.h"
23
#include "lib.h"
24
#include "lower.h"
25
#include "option.h"
26
#include "phase.h"
27
#include "scan.h"
28
#include "thread.h"
29
#include "type.h"
30
#include "vlog/vlog-phase.h"
31

32
#include <assert.h>
33
#include <ctype.h>
34
#include <string.h>
35
#include <stdlib.h>
36
#include <inttypes.h>
37

38
static vhdl_standard_t  current_std  = STD_08;
39
static bool             have_set_std = false;
40
static ident_t          id_cache[NUM_WELL_KNOWN];
41
static text_buf_t      *syntax_buf = NULL;
42

43
int64_t assume_int(tree_t t)
358,073✔
44
{
45
   int64_t value;
358,073✔
46
   if (folded_int(t, &value))
358,073✔
47
      return value;
358,073✔
48

49
   fatal_at(tree_loc(t), "expression cannot be folded to "
×
50
            "an integer constant");
51
}
52

53
void range_bounds(tree_t r, int64_t *low, int64_t *high)
26,903✔
54
{
55
   assert(tree_kind(r) == T_RANGE);
26,903✔
56

57
   const int64_t left = assume_int(tree_left(r));
26,903✔
58
   const int64_t right = assume_int(tree_right(r));
26,903✔
59

60
   *low  = tree_subkind(r) == RANGE_TO ? left : right;
26,903✔
61
   *high = tree_subkind(r) == RANGE_TO ? right : left;
26,903✔
62
}
26,903✔
63

64
bool folded_int(tree_t t, int64_t *l)
3,503,973✔
65
{
66
   switch (tree_kind(t)) {
3,538,213✔
67
   case T_LITERAL:
2,557,582✔
68
      switch (tree_subkind(t)) {
2,557,582✔
69
      case L_PHYSICAL:
15,211✔
70
         if (tree_has_ref(t))
15,211✔
71
            return false;
72
         // Fall-through
73
      case L_INT:
74
         *l = tree_ival(t);
2,511,603✔
75
         return true;
2,511,603✔
76
      default:
77
         return false;
78
      }
79
   case T_QUALIFIED:
332✔
80
      return folded_int(tree_value(t), l);
332✔
81
   case T_REF:
765,566✔
82
      if (tree_has_ref(t)) {
765,566✔
83
         tree_t decl = tree_ref(t);
765,565✔
84
         switch (tree_kind(decl)) {
765,565✔
85
         case T_CONST_DECL:
41,322✔
86
            if (tree_has_value(decl))
41,322✔
87
               return folded_int(tree_value(decl), l);
33,584✔
88
            else
89
               return false;
90
         case T_ENUM_LIT:
655,864✔
91
            *l = tree_pos(decl);
655,864✔
92
            return true;
655,864✔
93
         case T_ALIAS:
324✔
94
            return folded_int(tree_value(decl), l);
324✔
95
         default:
96
            return false;
97
         }
98
      }
99
      // Fall-through
100
   default:
101
      return false;
102
   }
103
}
104

105
bool folded_real(tree_t t, double *l)
193,100✔
106
{
107
   switch (tree_kind(t)) {
193,103✔
108
   case T_LITERAL:
185,021✔
109
      if (tree_subkind(t) == L_REAL) {
185,021✔
110
         *l = tree_dval(t);
184,952✔
111
         return true;
184,952✔
112
      }
113
      else
114
         return false;
115
   case T_QUALIFIED:
3✔
116
      return folded_real(tree_value(t), l);
3✔
117
   default:
118
      return false;
119
   }
120
}
121

122
bool folded_length(tree_t r, int64_t *l)
52,412✔
123
{
124
   int64_t low, high;
52,412✔
125
   if (folded_bounds(r, &low, &high)) {
52,412✔
126
      *l = MAX(high - low + 1, 0);
49,570✔
127
      return true;
49,570✔
128
   }
129
   else
130
      return false;
131
}
132

133
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
1,450,232✔
134
{
135
   assert(tree_kind(r) == T_RANGE);
1,450,232✔
136

137
   const range_kind_t rkind = tree_subkind(r);
1,450,232✔
138

139
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
1,450,232✔
140
      return false;
141

142
   int64_t left, right;
1,438,007✔
143
   if (!folded_int(tree_left(r), &left))
1,438,007✔
144
      return false;
145
   else if (!folded_int(tree_right(r), &right))
1,326,063✔
146
      return false;
147

148
   switch (rkind) {
1,288,562✔
149
   case RANGE_TO:
1,174,374✔
150
      *low  = left;
1,174,374✔
151
      *high = right;
1,174,374✔
152
      return true;
1,174,374✔
153
   case RANGE_DOWNTO:
114,188✔
154
      *low  = right;
114,188✔
155
      *high = left;
114,188✔
156
      return true;
114,188✔
157
   default:
158
      return false;
159
   }
160
}
161

162
bool folded_bounds_real(tree_t r, double *low, double *high)
77,336✔
163
{
164
   assert(tree_kind(r) == T_RANGE);
77,336✔
165

166
   const range_kind_t rkind = tree_subkind(r);
77,336✔
167

168
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
77,336✔
169
      return false;
170

171
   double left, right;
77,333✔
172
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
77,333✔
173
      switch (rkind) {
77,248✔
174
      case RANGE_TO:
77,248✔
175
         *low  = left;
77,248✔
176
         *high = right;
77,248✔
177
         return true;
77,248✔
178
      case RANGE_DOWNTO:
×
179
         *low  = right;
×
180
         *high = left;
×
181
         return true;
×
182
      default:
183
         return false;
184
      }
185
   }
186
   else
187
      return false;
85✔
188
}
189

190
bool folded_bool(tree_t t, bool *b)
39,569✔
191
{
192
   if (tree_kind(t) == T_REF) {
39,569✔
193
      tree_t decl = tree_ref(t);
10,406✔
194
      if (tree_kind(decl) == T_ENUM_LIT
10,406✔
195
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
8,084✔
196
         *b = (tree_pos(decl) == 1);
8,084✔
197
         return true;
8,084✔
198
      }
199
   }
200

201
   return false;
202
}
203

204
tree_t get_enum_lit(tree_t t, type_t type, int pos)
7,315✔
205
{
206
   type_t enum_type = type_base_recur(type ?: tree_type(t));
7,315✔
207
   tree_t lit = type_enum_literal(enum_type, pos);
7,315✔
208

209
   tree_t b = tree_new(T_REF);
7,315✔
210
   tree_set_loc(b, tree_loc(t));
7,315✔
211
   tree_set_ref(b, lit);
7,315✔
212
   tree_set_type(b, enum_type);
7,315✔
213
   tree_set_ident(b, tree_ident(lit));
7,315✔
214

215
   return b;
7,315✔
216
}
217

218
tree_t get_int_lit(tree_t t, type_t type, int64_t i)
40,698✔
219
{
220
   tree_t f = tree_new(T_LITERAL);
40,698✔
221
   tree_set_subkind(f, L_INT);
40,698✔
222
   tree_set_ival(f, i);
40,698✔
223
   tree_set_loc(f, tree_loc(t));
40,698✔
224
   tree_set_type(f, type ?: tree_type(t));
40,698✔
225

226
   return f;
40,698✔
227
}
228

229
tree_t get_discrete_lit(tree_t t, type_t type, int64_t i)
3,312✔
230
{
231
   if (type_is_enum(type)) {
3,312✔
232
      type_t base = type_base_recur(type);
68✔
233
      const int maxlit = type_enum_literals(base);
68✔
234
      if (i >= maxlit)
68✔
235
         return NULL;
236
      else
237
         return get_enum_lit(t, type, i);
68✔
238
   }
239
   else
240
      return get_int_lit(t, type, i);
3,244✔
241
}
242

243
tree_t get_real_lit(tree_t t, type_t type, double r)
×
244
{
245
   tree_t f = tree_new(T_LITERAL);
×
246
   tree_set_loc(f, tree_loc(t));
×
247
   tree_set_subkind(f, L_REAL);
×
248
   tree_set_dval(f, r);
×
249
   tree_set_type(f, type ?: tree_type(t));
×
250

251
   return f;
×
252
}
253

254
bool parse_value(type_t type, const char *str, parsed_value_t *value)
92✔
255
{
256
   type_t base = type_base_recur(type);
92✔
257
   const type_kind_t basek = type_kind(base);
92✔
258

259
   if (basek == T_ARRAY && type_is_character_array(base)) {
92✔
260
      value->enums = NULL;
17✔
261

262
      int map[256];
17✔
263
      for (int i = 0; i < ARRAY_LEN(map); i++)
4,369✔
264
         map[i] = INT_MAX;
4,352✔
265

266
      type_t elem = type_elem(base);
17✔
267

268
      const int nlits = type_enum_literals(elem);
17✔
269
      for (int i = 0; i < nlits; i++) {
2,591✔
270
         ident_t id = tree_ident(type_enum_literal(elem, i));
2,574✔
271
         if (ident_char(id, 0) == '\'')
2,574✔
272
            map[(uint8_t)ident_char(id, 1)] = i;
1,924✔
273
      }
274

275
      while (map[(uint8_t)*str] == INT_MAX && isspace_iso88591(*str))
20✔
276
         ++str;
3✔
277

278
      const bool quoted = map['\"'] == INT_MAX && *str == '\"';
17✔
279
      if (quoted) str++;
17✔
280

281
      const size_t max = strlen(str);
17✔
282
      enum_array_t *array = xmalloc_flex(sizeof(enum_array_t), max, 1);
17✔
283

284
      int n = 0, m;
17✔
285
      for (; *str != '\0' && (m = map[(uint8_t)*str]) != INT_MAX; str++, n++)
99✔
286
         array->values[n] = m;
65✔
287

288
      assert(n <= max);
17✔
289
      array->count = n;
17✔
290

291
      if (quoted && *str++ != '\"') {
17✔
292
         free(array);
1✔
293
         return false;
1✔
294
      }
295

296
      for (; *str; str++) {
20✔
297
         if (!isspace_iso88591(*str)) {
5✔
298
            free(array);
1✔
299
            return false;
1✔
300
         }
301
      }
302

303
      value->enums = array;
15✔
304
      return true;
15✔
305
   }
306

307
   while (isspace_iso88591(*str))
85✔
308
      ++str;
10✔
309

310
   switch (basek) {
75✔
311
   case T_INTEGER:
50✔
312
      {
313
         const bool is_negative = *str == '-';
50✔
314
         int num_digits = 0;
50✔
315

316
         if (is_negative) ++str;
50✔
317

318
         int64_t sum = 0;
319
         for (; isdigit_iso88591(*str) || (*str == '_'); str++) {
144✔
320
            if (*str != '_') {
94✔
321
               sum *= 10;
92✔
322
               sum += (*str - '0');
92✔
323
               num_digits++;
92✔
324
            }
325
         }
326

327
         value->integer = is_negative ? -sum : sum;
50✔
328

329
         if (num_digits == 0)
50✔
330
            return false;
331
      }
332
      break;
333

334
   case T_ENUM:
14✔
335
      {
336
         bool upcase = true;
14✔
337
         char *copy LOCAL = xstrdup(str), *p;
27✔
338
         for (p = copy; (*p != '\0') && !isspace_iso88591(*p); p++, str++) {
55✔
339
            if (*p == '\'')
41✔
340
               upcase = false;
341
            if (upcase)
23✔
342
               *p = toupper_iso88591(*p);
14✔
343
         }
344
         *p = '\0';
14✔
345

346
         ident_t id = ident_new(copy);
14✔
347

348
         value->integer = -1;
14✔
349

350
         const int nlits = type_enum_literals(base);
14✔
351
         for (int i = 0; i < nlits; i++) {
38✔
352
            if (tree_ident(type_enum_literal(base, i)) == id) {
37✔
353
               value->integer = i;
13✔
354
               break;
13✔
355
            }
356
         }
357

358
         if (value->integer == -1)
14✔
359
            return false;
1✔
360
      }
361
      break;
362

363
   case T_REAL:
6✔
364
      {
365
         char *eptr = NULL;
6✔
366
         value->real = strtod(str, &eptr);
6✔
367
         str = eptr;
6✔
368
      }
369
      break;
6✔
370

371
   case T_PHYSICAL:
5✔
372
      {
373
         char *eptr = NULL;
5✔
374
         double scale = strtod(str, &eptr);
5✔
375
         str = eptr;
5✔
376

377
         while (isspace_iso88591(*str)) ++str;
10✔
378

379
         char *copy LOCAL = xstrdup(str), *p;
10✔
380
         for (p = copy; *p && !isspace_iso88591(*p); p++, str++)
13✔
381
            *p = toupper_iso88591(*p);
8✔
382
         *p = '\0';
5✔
383

384
         if (p == copy)
5✔
385
            return false;
386

387
         ident_t id = ident_new(copy);
4✔
388

389
         value->integer = -1;
4✔
390

391
         const int nunits = type_units(base);
4✔
392
         for (int i = 0; i < nunits; i++) {
11✔
393
            tree_t u = type_unit(base, i);
11✔
394
            if (tree_ident(u) == id) {
11✔
395
               value->integer = scale * assume_int(tree_value(u));
4✔
396
               break;
4✔
397
            }
398
         }
399

400
         if (value->integer == -1)
4✔
401
            return false;
402
      }
403
      break;
404

405
   default:
406
      return false;
407
   }
408

409
   for (; *str; str++) {
84✔
410
      if (!isspace_iso88591(*str))
11✔
411
         return false;
412
   }
413

414
   return true;
415
}
416

417
tree_t make_ref(tree_t to)
7,371✔
418
{
419
   tree_t t = tree_new(T_REF);
7,371✔
420
   tree_set_ident(t, tree_ident(to));
7,371✔
421
   tree_set_ref(t, to);
7,371✔
422
   tree_set_type(t, tree_type(to));
7,371✔
423
   return t;
7,371✔
424
}
425

426
vhdl_standard_t standard(void)
2,547,875✔
427
{
428
   return current_std;
2,547,875✔
429
}
430

431
void set_standard(vhdl_standard_t s)
4,456✔
432
{
433
   current_std = s;
4,456✔
434
   have_set_std = true;
4,456✔
435
}
4,456✔
436

437
void set_default_standard(vhdl_standard_t s)
381✔
438
{
439
   if (!have_set_std)
381✔
440
      set_standard(s);
70✔
441
}
381✔
442

443
const char *standard_text(vhdl_standard_t s)
4,089✔
444
{
445
   static const char *text[] = {
4,089✔
446
      "1987", "1993", "2000", "2002", "2008", "2019"
447
   };
448

449
   if ((unsigned)s < ARRAY_LEN(text))
4,089✔
450
      return text[s];
4,089✔
451
   else
452
      return "????";
453
}
454

455
tree_t find_record_field(tree_t rref)
8,903✔
456
{
457
   ident_t fname = tree_ident(rref);
8,903✔
458
   type_t value_type = tree_type(tree_value(rref));
8,903✔
459

460
   if (!type_is_record(value_type))
8,903✔
461
      return NULL;
462

463
   const int nfields = type_fields(value_type);
8,903✔
464
   for (int i = 0; i < nfields; i++) {
28,532✔
465
      tree_t field = type_field(value_type, i);
28,527✔
466
      if (tree_ident(field) == fname)
28,527✔
467
         return field;
8,898✔
468
   }
469

470
   return NULL;
471
}
472

473
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
373✔
474
{
475
   switch (tree_kind(view)) {
1,171✔
476
   case T_REF:
505✔
477
      return find_element_mode_indication(tree_ref(view), field, converse);
505✔
478

479
   case T_ALIAS:
132✔
480
      return find_element_mode_indication(tree_value(view), field, converse);
132✔
481

482
   case T_ATTR_REF:
161✔
483
      assert(tree_subkind(view) == ATTR_CONVERSE);
161✔
484
      *converse = !*converse;
161✔
485
      return find_element_mode_indication(tree_name(view), field, converse);
161✔
486

487
   case T_VIEW_DECL:
373✔
488
      {
489
         type_t view_type = tree_type(view);
373✔
490
         assert(type_kind(view_type) == T_VIEW);
373✔
491

492
         const int nelems = type_fields(view_type);
373✔
493
         for (int i = 0; i < nelems; i++) {
637✔
494
            tree_t e = type_field(view_type, i);
637✔
495
            if (tree_ref(e) == field)
637✔
496
               return e;
373✔
497
         }
498

499
         return NULL;
500
      }
501

502
   default:
×
503
      fatal_trace("unhandled tree kind %s in find_element_mode_indication",
×
504
                  tree_kind_str(tree_kind(view)));
505
   }
506
}
507

508
port_mode_t converse_mode(tree_t port, bool converse)
253✔
509
{
510
   const port_mode_t mode = tree_subkind(port);
253✔
511
   switch (mode) {
253✔
512
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
135✔
513
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
109✔
514
   default: return mode;
515
   }
516
}
517

518
class_t class_of(tree_t t)
1,600,580✔
519
{
520
   switch (tree_kind(t)) {
1,720,922✔
521
   case T_VAR_DECL:
522
      return C_VARIABLE;
523
   case T_SIGNAL_DECL:
60,986✔
524
   case T_IMPLICIT_SIGNAL:
525
      return C_SIGNAL;
60,986✔
526
   case T_CONST_DECL:
46,379✔
527
      return C_CONSTANT;
46,379✔
528
   case T_PORT_DECL:
202,106✔
529
   case T_GENERIC_DECL:
530
   case T_PARAM_DECL:
531
   case T_EXTERNAL_NAME:
532
      return tree_class(t);
202,106✔
533
   case T_ENUM_LIT:
935,616✔
534
   case T_LITERAL:
535
   case T_STRING:
536
      return C_LITERAL;
935,616✔
537
   case T_FIELD_DECL:
1,927✔
538
   case T_ATTR_DECL:
539
      return C_DEFAULT;
1,927✔
540
   case T_VIEW_DECL:
199✔
541
      return C_VIEW;
199✔
542
   case T_UNIT_DECL:
6,962✔
543
      return C_UNITS;
6,962✔
544
   case T_ARCH:
44✔
545
      return C_ARCHITECTURE;
44✔
546
   case T_FUNC_DECL:
42,051✔
547
   case T_FUNC_BODY:
548
   case T_FUNC_INST:
549
   case T_FCALL:
550
      return C_FUNCTION;
42,051✔
551
   case T_PROC_DECL:
25,758✔
552
   case T_PROC_BODY:
553
   case T_PROC_INST:
554
   case T_PCALL:
555
      return C_PROCEDURE;
25,758✔
556
   case T_ENTITY:
136✔
557
      return C_ENTITY;
136✔
558
   case T_SUBTYPE_DECL:
33,965✔
559
      return C_SUBTYPE;
33,965✔
560
   case T_TYPE_DECL:
138,388✔
561
   case T_PROT_DECL:
562
   case T_PROT_BODY:
563
      return C_TYPE;
138,388✔
564
   case T_FILE_DECL:
1,985✔
565
      return C_FILE;
1,985✔
566
   case T_PROCESS:
80✔
567
   case T_BLOCK:
568
   case T_FOR:
569
   case T_INSTANCE:
570
   case T_CONCURRENT:
571
   case T_ELAB:
572
      return C_LABEL;
80✔
573
   case T_COMPONENT:
1✔
574
      return C_COMPONENT;
1✔
575
   case T_REF:
109,053✔
576
   case T_PROT_REF:
577
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
109,053✔
578
   case T_ARRAY_REF:
11,305✔
579
   case T_ARRAY_SLICE:
580
   case T_RECORD_REF:
581
   case T_ALL:
582
   case T_ALIAS:
583
   case T_QUALIFIED:
584
      return class_of(tree_value(t));
11,305✔
585
   case T_PACKAGE:
1,225✔
586
   case T_PACK_BODY:
587
   case T_PACK_INST:
588
      return C_PACKAGE;
1,225✔
589
   case T_CONFIGURATION:
1✔
590
      return C_CONFIGURATION;
1✔
591
   case T_LIBRARY:
×
592
      return C_LIBRARY;
×
593
   case T_ATTR_REF:
233✔
594
      switch (tree_subkind(t)) {
233✔
595
      case ATTR_DELAYED:
596
      case ATTR_STABLE:
597
      case ATTR_QUIET:
598
      case ATTR_TRANSACTION:
599
         return C_SIGNAL;
600
      default:
227✔
601
         return C_DEFAULT;
227✔
602
      }
603
   case T_CONTEXT:
×
604
      return C_CONTEXT;
×
605
   default:
×
606
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
×
607
   }
608
}
609

610
bool class_has_type(class_t c)
841,414✔
611
{
612
   switch (c) {
841,414✔
613
   case C_LABEL:
614
   case C_ENTITY:
615
   case C_ARCHITECTURE:
616
   case C_COMPONENT:
617
   case C_CONFIGURATION:
618
   case C_PACKAGE:
619
   case C_LIBRARY:
620
      return false;
621
   default:
840,190✔
622
      return true;
840,190✔
623
   }
624
}
625

626
const char *class_str(class_t c)
272✔
627
{
628
   static const char *strs[] = {
272✔
629
      "default", "signal", "variable", "constant", "file", "entity",
630
      "component", "configuration", "architecture", "function", "package",
631
      "type", "subtype", "label", "procedure", "literal", "units", "library",
632
      "context", "view",
633
   };
634
   assert(c < ARRAY_LEN(strs));
272✔
635
   return strs[c];
272✔
636
}
637

638
const char *assoc_kind_str(assoc_kind_t akind)
9✔
639
{
640
   switch (akind) {
9✔
641
   case A_NAMED:  return "named";
642
   case A_CONCAT:
2✔
643
   case A_POS:    return "positional";
2✔
644
   case A_OTHERS: return "others";
3✔
645
   case A_SLICE:
1✔
646
   case A_RANGE:  return "range";
1✔
647
   default:       return "??";
×
648
   }
649
}
650

651
bool is_subprogram(tree_t t)
2,071,886✔
652
{
653
   switch (tree_kind(t)) {
2,071,886✔
654
   case T_FUNC_DECL:
655
   case T_FUNC_BODY:
656
   case T_FUNC_INST:
657
   case T_PROC_DECL:
658
   case T_PROC_BODY:
659
   case T_PROC_INST:
660
      return true;
661
   case T_GENERIC_DECL:
2,330✔
662
      {
663
         const class_t class = tree_class(t);
2,330✔
664
         return class == C_FUNCTION || class == C_PROCEDURE;
2,330✔
665
      }
666
   default:
1,698,644✔
667
      return false;
1,698,644✔
668
   }
669
}
670

671
bool is_loop_stmt(tree_t t)
421✔
672
{
673
   const tree_kind_t kind = tree_kind(t);
421✔
674
   return kind == T_WHILE || kind == T_FOR || kind == T_LOOP;
421✔
675
}
676

677
bool is_container(tree_t t)
10,408✔
678
{
679
   switch (tree_kind(t)) {
10,408✔
680
   case T_FUNC_BODY:
681
   case T_PROC_BODY:
682
   case T_ENTITY:
683
   case T_ARCH:
684
   case T_PACKAGE:
685
   case T_PACK_BODY:
686
   case T_CONFIGURATION:
687
   case T_BLOCK:
688
   case T_PROT_BODY:
689
   case T_ELAB:
690
   case T_FOR:
691
   case T_PROCESS:
692
   case T_PACK_INST:
693
      return true;
694
   default:
7,936✔
695
      return false;
7,936✔
696
   }
697
}
698

699
bool is_concurrent_block(tree_t t)
741✔
700
{
701
   switch (tree_kind(t)) {
741✔
702
   case T_ARCH:
703
   case T_ENTITY:
704
   case T_BLOCK:
705
   case T_IF_GENERATE:
706
   case T_FOR_GENERATE:
707
      return true;
708
   default:
208✔
709
      return false;
208✔
710
   }
711
}
712

713
bool is_package(tree_t t)
46,464✔
714
{
715
   switch (tree_kind(t)) {
46,464✔
716
   case T_PACKAGE:
717
   case T_PACK_BODY:
718
   case T_PACK_INST:
719
      return true;
720
   default:
5,513✔
721
      return false;
5,513✔
722
   }
723
}
724

725
bool is_design_unit(tree_t t)
35,089✔
726
{
727
   switch (tree_kind(t)) {
35,089✔
728
   case T_ENTITY:
729
   case T_ARCH:
730
   case T_PACKAGE:
731
   case T_PACK_BODY:
732
   case T_CONFIGURATION:
733
   case T_CONTEXT:
734
   case T_PACK_INST:
735
      return true;
736
   default:
8,638✔
737
      return false;
8,638✔
738
   }
739
}
740

741
bool is_literal(tree_t t)
30,559✔
742
{
743
   switch (tree_kind(t)) {
30,559✔
744
   case T_REF:
3,372✔
745
      return tree_has_ref(t) && tree_kind(tree_ref(t)) == T_ENUM_LIT;
5,728✔
746
   case T_LITERAL:
747
   case T_STRING:
748
      return true;
749
   default:
20,593✔
750
      return false;
20,593✔
751
   }
752
}
753

754
bool is_body(tree_t t)
6,872✔
755
{
756
   switch (tree_kind(t)) {
6,872✔
757
   case T_FUNC_BODY:
758
   case T_PROC_BODY:
759
   case T_PACK_BODY:
760
   case T_PROT_BODY:
761
      return true;
762
   default:
43✔
763
      return false;
43✔
764
   }
765
}
766

767
bool is_guarded_signal(tree_t decl)
12,364✔
768
{
769
   switch (tree_kind(decl)) {
12,364✔
770
   case T_PORT_DECL:
12,011✔
771
   case T_SIGNAL_DECL:
772
      return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
12,011✔
773
   default:
774
      return false;
775
   }
776
}
777

778
bool is_type_decl(tree_t t)
3,345,515✔
779
{
780
   switch (tree_kind(t)) {
3,345,515✔
781
   case T_TYPE_DECL:
782
   case T_SUBTYPE_DECL:
783
   case T_PROT_DECL:
784
   case T_PROT_BODY:
785
      return true;
786
   default:
3,181,575✔
787
      return false;
3,181,575✔
788
   }
789
}
790

791
tree_t aliased_type_decl(tree_t decl)
121,038✔
792
{
793
   switch (tree_kind(decl)) {
121,302✔
794
   case T_ALIAS:
266✔
795
      {
796
         tree_t value = tree_value(decl);
266✔
797
         const tree_kind_t kind = tree_kind(value);
266✔
798
         if (kind == T_REF && tree_has_ref(value))
266✔
799
            return aliased_type_decl(tree_ref(value));
264✔
800
         else if (kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
2✔
801
             return value;
802
         else
803
            return NULL;
×
804
      }
805
   case T_TYPE_DECL:
806
   case T_SUBTYPE_DECL:
807
   case T_PROT_DECL:
808
   case T_PROT_BODY:
809
      return decl;
810
   case T_GENERIC_DECL:
1,459✔
811
      if (tree_class(decl) == C_TYPE)
1,459✔
812
         return decl;
813
      else
814
         return NULL;
739✔
815
   default:
28,288✔
816
      return NULL;
28,288✔
817
   }
818
}
819

820
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
153,964✔
821
{
822
   tree_t p = tree_new(T_PARAM);
153,964✔
823
   tree_set_loc(p, tree_loc(value));
153,964✔
824
   tree_set_subkind(p, kind);
153,964✔
825
   tree_set_value(p, value);
153,964✔
826

827
   switch (kind) {
153,964✔
828
   case P_NAMED:
29✔
829
      assert(name != NULL);
29✔
830
      tree_set_name(p, name);
29✔
831
      break;
29✔
832
   case P_POS:
153,935✔
833
      tree_set_pos(p, tree_params(call));
153,935✔
834
      break;
153,935✔
835
   }
836

837
   tree_add_param(call, p);
153,964✔
838
   return p;
153,964✔
839
}
840

841
type_t array_aggregate_type(type_t array, int from_dim)
281✔
842
{
843
   if (type_is_none(array))
281✔
844
      return type_new(T_NONE);
1✔
845

846
   if (type_is_unconstrained(array)) {
280✔
847
      const int nindex = type_indexes(array);
39✔
848
      assert(from_dim < nindex);
39✔
849

850
      type_t type = type_new(T_ARRAY);
39✔
851
      type_set_ident(type, type_ident(array));
39✔
852
      type_set_elem(type, type_elem(array));
39✔
853

854
      for (int i = from_dim; i < nindex; i++)
78✔
855
         type_add_index(type, type_index(array, i));
39✔
856

857
      return type;
858
   }
859
   else {
860
      const int ndims = dimension_of(array);
241✔
861
      assert(from_dim < ndims);
241✔
862

863
      type_t base = type_new(T_ARRAY);
241✔
864
      type_set_ident(base, type_ident(array));
241✔
865
      type_set_elem(base, type_elem(array));
241✔
866

867
      tree_t constraint = tree_new(T_CONSTRAINT);
241✔
868
      tree_set_subkind(constraint, C_INDEX);
241✔
869

870
      type_t sub = type_new(T_SUBTYPE);
241✔
871
      type_set_base(sub, base);
241✔
872
      type_add_constraint(sub, constraint);
241✔
873

874
      for (int i = from_dim; i < ndims; i++) {
504✔
875
         tree_t r = range_of(array, i);
263✔
876
         if (r == NULL) {
263✔
877
            // Not enough constraints were provided for the type
878
            r = tree_new(T_RANGE);
1✔
879
            tree_set_subkind(r, RANGE_ERROR);
1✔
880
            tree_set_type(r, type_new(T_NONE));
1✔
881
         }
882

883
         type_add_index(base, tree_type(r));
263✔
884
         tree_add_range(constraint, r);
263✔
885
      }
886

887
      return sub;
888
   }
889
}
890

891
unsigned bits_for_range(int64_t low, int64_t high)
1,615,446✔
892
{
893
   if (low > high)
1,615,446✔
894
      return 0;   // Null range
895
   else if (low < 0) {
1,615,445✔
896
      // Signed integers
897
      if (low >= INT8_MIN && high <= INT8_MAX)
500,378✔
898
         return 8;
899
      else if (low >= INT16_MIN && high <= INT16_MAX)
493,600✔
900
         return 16;
901
      else if (low >= INT32_MIN && high <= INT32_MAX)
493,369✔
902
         return 32;
903
      else
904
         return 64;
108,291✔
905
   }
906
   else {
907
      // Unsigned integers
908
      if (high <= 1)
1,115,067✔
909
         return 1;
910
      else if (high <= UINT8_MAX)
555,044✔
911
         return 8;
912
      else if (high <= UINT16_MAX)
135,142✔
913
         return 16;
914
      else if (high <= UINT32_MAX)
132,108✔
915
         return 32;
916
      else
917
         return 64;
15,313✔
918
   }
919
}
920

921
unsigned dimension_of(type_t type)
1,170,195✔
922
{
923
   switch (type_kind(type)) {
2,186,163✔
924
   case T_SUBTYPE:
1,015,966✔
925
      return dimension_of(type_base(type));
1,015,966✔
926
   case T_GENERIC:
55✔
927
      assert(type_subkind(type) == GTYPE_ARRAY);
55✔
928
      // Fall-through
929
   case T_ARRAY:
930
      return type_indexes(type);
1,163,450✔
931
   case T_NONE:
932
   case T_RECORD:
933
   case T_INCOMPLETE:
934
      return 0;
935
   case T_INTEGER:
6,693✔
936
   case T_REAL:
937
   case T_PHYSICAL:
938
   case T_ENUM:
939
      return type_dims(type);
6,693✔
940
   case T_ACCESS:
2✔
941
      return dimension_of(type_designated(type));
2✔
942
   default:
×
943
      fatal_trace("invalid type kind %s in dimension_of",
×
944
                  type_kind_str(type_kind(type)));
945
   }
946
}
947

948
tree_t range_of(type_t type, unsigned dim)
1,497,444✔
949
{
950
   switch (type_kind(type)) {
1,527,779✔
951
   case T_SUBTYPE:
1,202,749✔
952
      if (type_constraints(type) > 0) {
1,202,749✔
953
         tree_t c0 = type_constraint(type, 0);
1,172,590✔
954
         switch (tree_subkind(c0)) {
1,172,590✔
955
         case C_OPEN:
176✔
956
            return range_of(type_base(type), dim);
176✔
957
         case C_INDEX:
1,172,414✔
958
         case C_RANGE:
959
            if (dim < tree_ranges(c0))
1,172,414✔
960
               return tree_range(c0, dim);
1,172,413✔
961
            else
962
               return NULL;   // Must be an error
963
         default:
×
964
            fatal_trace("invalid constraint in range_of");
×
965
         }
966
      }
967
      else
968
         return range_of(type_base(type), dim);
30,159✔
969
   case T_INTEGER:
325,030✔
970
   case T_REAL:
971
   case T_PHYSICAL:
972
   case T_ENUM:
973
      return type_dim(type, dim);
325,030✔
974
   default:
×
975
      fatal_trace("invalid type kind %s in range_of",
×
976
                  type_kind_str(type_kind(type)));
977
   }
978
}
979

980
range_kind_t direction_of(type_t type, unsigned dim)
23,706✔
981
{
982
   switch (type_kind(type)) {
23,750✔
983
   case T_ENUM:
984
      return RANGE_TO;
985
   case T_NONE:
×
986
      return RANGE_ERROR;
×
987
   case T_INTEGER:
23,649✔
988
   case T_REAL:
989
   case T_PHYSICAL:
990
   case T_SUBTYPE:
991
      {
992
         tree_t r = range_of(type, dim);
23,649✔
993
         const range_kind_t rkind = tree_subkind(r);
23,649✔
994
         if (rkind == RANGE_EXPR) {
23,649✔
995
            // Return a fixed direction if possible
996
            tree_t value = tree_value(r);
113✔
997
            assert(tree_kind(value) == T_ATTR_REF);
113✔
998

999
            DEBUG_ONLY({
113✔
1000
                  const attr_kind_t attr = tree_subkind(value);
1001
                  assert(attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE);
1002
               });
113✔
1003

1004
            tree_t name = tree_name(value);
113✔
1005
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
113✔
1006
               tree_t decl = tree_ref(name);
112✔
1007
               if (is_type_decl(decl))
112✔
1008
                  return direction_of(tree_type(decl), 0);
44✔
1009
            }
1010
         }
1011

1012
         return rkind;
1013
      }
1014
   default:
×
1015
      fatal_trace("invalid type kind %s in direction_of",
×
1016
                  type_kind_str(type_kind(type)));
1017
   }
1018
}
1019

1020
type_t index_type_of(type_t type, unsigned dim)
205,863✔
1021
{
1022
   if (dim >= dimension_of(type))
205,865✔
1023
      return NULL;
1024

1025
   type_t base = type_base_recur(type);
205,842✔
1026
   type_kind_t base_kind = type_kind(base);
205,842✔
1027
   if (base_kind == T_ARRAY)
205,842✔
1028
      return type_index(base, dim);
202,687✔
1029
   else if (base_kind == T_ENUM || base_kind == T_NONE)
3,155✔
1030
      return type;
1031
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
3,002✔
1032
      return type_index(base, dim);
29✔
1033
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
2,973✔
1034
      return NULL;
1035
   else if (base_kind == T_ACCESS)
2,973✔
1036
      return index_type_of(type_designated(type), dim);
2✔
1037
   else
1038
      return tree_type(range_of(base, dim));
2,971✔
1039
}
1040

1041
int64_t rebase_index(type_t array_type, int dim, int64_t value)
240✔
1042
{
1043
   // Convert value which is in the range of array_type to a zero-based index
1044
   tree_t r = range_of(array_type, dim);
240✔
1045
   const int64_t left = assume_int(tree_left(r));
240✔
1046
   return (tree_subkind(r) == RANGE_TO) ? value - left : left - value;
240✔
1047
}
1048

1049
ident_t well_known(well_known_t id)
263,296✔
1050
{
1051
   assert(id < NUM_WELL_KNOWN);
263,296✔
1052
   return id_cache[id];
263,296✔
1053
}
1054

1055
well_known_t is_well_known(ident_t ident)
261,805✔
1056
{
1057
   well_known_t pos = 0;
261,805✔
1058
   for (; pos < NUM_WELL_KNOWN; pos++) {
14,538,437✔
1059
      if (id_cache[pos] == ident)
14,403,080✔
1060
         break;
1061
   }
1062

1063
   return pos;
261,805✔
1064
}
1065

1066
void intern_strings(void)
4,604✔
1067
{
1068
   id_cache[W_STD_STANDARD]    = ident_new("STD.STANDARD");
4,604✔
1069
   id_cache[W_ALL]             = ident_new("all");
4,604✔
1070
   id_cache[W_STD_BIT]         = ident_new("STD.STANDARD.BIT");
4,604✔
1071
   id_cache[W_STD_BOOL]        = ident_new("STD.STANDARD.BOOLEAN");
4,604✔
1072
   id_cache[W_STD_CHAR]        = ident_new("STD.STANDARD.CHARACTER");
4,604✔
1073
   id_cache[W_STD_NATURAL]     = ident_new("STD.STANDARD.NATURAL");
4,604✔
1074
   id_cache[W_STD_POSITIVE]    = ident_new("STD.STANDARD.POSITIVE");
4,604✔
1075
   id_cache[W_STD_INTEGER]     = ident_new("STD.STANDARD.INTEGER");
4,604✔
1076
   id_cache[W_STD_STRING]      = ident_new("STD.STANDARD.STRING");
4,604✔
1077
   id_cache[W_STD_REAL]        = ident_new("STD.STANDARD.REAL");
4,604✔
1078
   id_cache[W_STD_TIME]        = ident_new("STD.STANDARD.TIME");
4,604✔
1079
   id_cache[W_STD_BIT_VECTOR]  = ident_new("STD.STANDARD.BIT_VECTOR");
4,604✔
1080
   id_cache[W_IEEE_SIGNED]     = ident_new("IEEE.NUMERIC_STD.SIGNED");
4,604✔
1081
   id_cache[W_IEEE_UNSIGNED]   = ident_new("IEEE.NUMERIC_STD.UNSIGNED");
4,604✔
1082
   id_cache[W_IEEE_LOGIC]      = ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC");
4,604✔
1083
   id_cache[W_IEEE_ULOGIC]     = ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC");
4,604✔
1084
   id_cache[W_IEEE_1164_AND]   = ident_new("IEEE.STD_LOGIC_1164.\"and\"");
4,604✔
1085
   id_cache[W_IEEE_1164_NAND]  = ident_new("IEEE.STD_LOGIC_1164.\"nand\"");
4,604✔
1086
   id_cache[W_IEEE_1164_OR]    = ident_new("IEEE.STD_LOGIC_1164.\"or\"");
4,604✔
1087
   id_cache[W_IEEE_1164_NOR]   = ident_new("IEEE.STD_LOGIC_1164.\"nor\"");
4,604✔
1088
   id_cache[W_IEEE_1164_XOR]   = ident_new("IEEE.STD_LOGIC_1164.\"xor\"");
4,604✔
1089
   id_cache[W_IEEE_1164_XNOR]  = ident_new("IEEE.STD_LOGIC_1164.\"xnor\"");
4,604✔
1090
   id_cache[W_FOREIGN]         = ident_new("FOREIGN");
4,604✔
1091
   id_cache[W_WORK]            = ident_new("WORK");
4,604✔
1092
   id_cache[W_STD]             = ident_new("STD");
4,604✔
1093
   id_cache[W_THUNK]           = ident_new("thunk");
4,604✔
1094
   id_cache[W_BODY]            = ident_new("body");
4,604✔
1095
   id_cache[W_CARET]           = ident_new("^");
4,604✔
1096
   id_cache[W_IEEE]            = ident_new("IEEE");
4,604✔
1097
   id_cache[W_IEEE_1164]       = ident_new("IEEE.STD_LOGIC_1164");
4,604✔
1098
   id_cache[W_ERROR]           = ident_new("error");
4,604✔
1099
   id_cache[W_ELAB]            = ident_new("elab");
4,604✔
1100
   id_cache[W_NUMERIC_STD]     = ident_new("IEEE.NUMERIC_STD");
4,604✔
1101
   id_cache[W_NUMERIC_BIT]     = ident_new("IEEE.NUMERIC_BIT");
4,604✔
1102
   id_cache[W_NVC]             = ident_new("NVC");
4,604✔
1103
   id_cache[W_DEFAULT_CLOCK]   = ident_new("default clock");
4,604✔
1104
   id_cache[W_DOLLAR_DISPLAY]  = ident_new("$display");
4,604✔
1105
   id_cache[W_DOLLAR_FINISH]   = ident_new("$finish");
4,604✔
1106
   id_cache[W_DOLLAR_WRITE]    = ident_new("$write");
4,604✔
1107
   id_cache[W_STD_REFLECTION]  = ident_new("STD.REFLECTION");
4,604✔
1108
   id_cache[W_NEVER_WAITS]     = ident_new("NEVER_WAITS");
4,604✔
1109
   id_cache[W_NVC_VERILOG]     = ident_new("NVC.VERILOG");
4,604✔
1110
   id_cache[W_NVC_PSL_SUPPORT] = ident_new("NVC.PSL_SUPPORT");
4,604✔
1111
   id_cache[W_SHAPE]           = ident_new("shape");
4,604✔
1112
   id_cache[W_INSTANCE_NAME]   = ident_new("instance_name");
4,604✔
1113
   id_cache[W_PATH_NAME]       = ident_new("path_name");
4,604✔
1114
   id_cache[W_DOLLAR_TIME]     = ident_new("$time");
4,604✔
1115
   id_cache[W_VITAL]           = ident_new("VITAL");
4,604✔
1116

1117
   id_cache[W_IEEE_LOGIC_VECTOR] =
9,208✔
1118
      ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR");
4,604✔
1119
   id_cache[W_IEEE_ULOGIC_VECTOR] =
9,208✔
1120
      ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR");
4,604✔
1121

1122
   id_cache[W_NUMERIC_STD_UNSIGNED] = ident_new("IEEE.NUMERIC_STD_UNSIGNED");
4,604✔
1123
   id_cache[W_NUMERIC_BIT_UNSIGNED] = ident_new("IEEE.NUMERIC_BIT_UNSIGNED");
4,604✔
1124

1125
   id_cache[W_OP_CCONV]               = ident_new("\"??\"");
4,604✔
1126
   id_cache[W_OP_AND]                 = ident_new("\"and\"");
4,604✔
1127
   id_cache[W_OP_OR]                  = ident_new("\"or\"");
4,604✔
1128
   id_cache[W_OP_NAND]                = ident_new("\"nand\"");
4,604✔
1129
   id_cache[W_OP_NOR]                 = ident_new("\"nor\"");
4,604✔
1130
   id_cache[W_OP_XOR]                 = ident_new("\"xor\"");
4,604✔
1131
   id_cache[W_OP_XNOR]                = ident_new("\"xnor\"");
4,604✔
1132
   id_cache[W_OP_EQUAL]               = ident_new("\"=\"");
4,604✔
1133
   id_cache[W_OP_NOT_EQUAL]           = ident_new("\"/=\"");
4,604✔
1134
   id_cache[W_OP_LESS_THAN]           = ident_new("\"<\"");
4,604✔
1135
   id_cache[W_OP_LESS_EQUAL]          = ident_new("\"<=\"");
4,604✔
1136
   id_cache[W_OP_GREATER_THAN]        = ident_new("\">\"");
4,604✔
1137
   id_cache[W_OP_GREATER_EQUAL]       = ident_new("\">=\"");
4,604✔
1138
   id_cache[W_OP_MATCH_EQUAL]         = ident_new("\"?=\"");
4,604✔
1139
   id_cache[W_OP_MATCH_NOT_EQUAL]     = ident_new("\"?/=\"");
4,604✔
1140
   id_cache[W_OP_MATCH_LESS_THAN]     = ident_new("\"?<\"");
4,604✔
1141
   id_cache[W_OP_MATCH_LESS_EQUAL]    = ident_new("\"?<=\"");
4,604✔
1142
   id_cache[W_OP_MATCH_GREATER_THAN]  = ident_new("\"?>\"");
4,604✔
1143
   id_cache[W_OP_MATCH_GREATER_EQUAL] = ident_new("\"?>=\"");
4,604✔
1144
   id_cache[W_OP_SLL]                 = ident_new("\"sll\"");
4,604✔
1145
   id_cache[W_OP_SRL]                 = ident_new("\"srl\"");
4,604✔
1146
   id_cache[W_OP_SLA]                 = ident_new("\"sla\"");
4,604✔
1147
   id_cache[W_OP_SRA]                 = ident_new("\"sra\"");
4,604✔
1148
   id_cache[W_OP_ROL]                 = ident_new("\"rol\"");
4,604✔
1149
   id_cache[W_OP_ROR]                 = ident_new("\"ror\"");
4,604✔
1150
   id_cache[W_OP_ADD]                 = ident_new("\"+\"");
4,604✔
1151
   id_cache[W_OP_MINUS]               = ident_new("\"-\"");
4,604✔
1152
   id_cache[W_OP_CONCAT]              = ident_new("\"&\"");
4,604✔
1153
   id_cache[W_OP_TIMES]               = ident_new("\"*\"");
4,604✔
1154
   id_cache[W_OP_DIVIDE]              = ident_new("\"/\"");
4,604✔
1155
   id_cache[W_OP_MOD]                 = ident_new("\"mod\"");
4,604✔
1156
   id_cache[W_OP_REM]                 = ident_new("\"rem\"");
4,604✔
1157
   id_cache[W_OP_EXPONENT]            = ident_new("\"**\"");
4,604✔
1158
   id_cache[W_OP_ABS]                 = ident_new("\"abs\"");
4,604✔
1159
   id_cache[W_OP_NOT]                 = ident_new("\"not\"");
4,604✔
1160
}
4,604✔
1161

1162
bool is_uninstantiated_package(tree_t pack)
33,153✔
1163
{
1164
   return tree_kind(pack) == T_PACKAGE
33,153✔
1165
      && tree_generics(pack) > 0
32,057✔
1166
      && tree_genmaps(pack) == 0;
34,324✔
1167
}
1168

1169
bool is_uninstantiated_subprogram(tree_t decl)
95,853✔
1170
{
1171
   switch (tree_kind(decl)) {
95,853✔
1172
   case T_FUNC_DECL:
95,406✔
1173
   case T_FUNC_BODY:
1174
   case T_PROC_DECL:
1175
   case T_PROC_BODY:
1176
      return tree_generics(decl) > 0;
95,406✔
1177
   default:
1178
      return false;
1179
   }
1180
}
1181

1182
bool is_anonymous_subtype(type_t type)
139,998✔
1183
{
1184
   return type_kind(type) == T_SUBTYPE && !type_has_ident(type);
139,998✔
1185
}
1186

1187
bool unit_needs_cgen(tree_t unit)
182✔
1188
{
1189
   switch (tree_kind(unit)) {
182✔
1190
   case T_PACK_INST:
1191
      return true;
1192
   case T_PACK_BODY:
×
1193
      {
1194
         tree_t pack = tree_primary(unit);
×
1195
         return package_needs_body(pack) && !is_uninstantiated_package(pack);
×
1196
      }
1197
   case T_PACKAGE:
12✔
1198
      return !package_needs_body(unit) && !is_uninstantiated_package(unit);
24✔
1199
   default:
×
1200
      return false;
×
1201
   }
1202
}
1203

1204
bool package_needs_body(tree_t pack)
1,720✔
1205
{
1206
   assert(tree_kind(pack) == T_PACKAGE);
1,720✔
1207

1208
   const int ndecls = tree_decls(pack);
1,720✔
1209
   for (int i = 0; i < ndecls; i++) {
78,907✔
1210
      tree_t d = tree_decl(pack, i);
78,449✔
1211
      const tree_kind_t dkind = tree_kind(d);
78,449✔
1212
      if ((dkind == T_FUNC_DECL || dkind == T_PROC_DECL)
78,449✔
1213
          && !(tree_flags(d) & TREE_F_PREDEFINED))
68,865✔
1214
         return true;
1215
      else if (dkind == T_CONST_DECL && !tree_has_value(d))
77,335✔
1216
         return true;
1217
      else if (dkind == T_PROT_DECL)
77,271✔
1218
         return true;
1219
   }
1220

1221
   return false;
1222
}
1223

1224
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
12,430✔
1225
                          well_known_t unit_name)
1226
{
1227
   const vhdl_standard_t curr = standard();
12,430✔
1228

1229
   if (cache[curr] == NULL) {
12,430✔
1230
      if (hint != NULL)
3,847✔
1231
         cache[curr] = hint;
1,350✔
1232
      else {
1233
         lib_t std = lib_require(well_known(lib_name));
2,497✔
1234
         cache[curr] = lib_get(std, well_known(unit_name));
2,497✔
1235
         assert(cache[curr] != NULL);
2,497✔
1236
      }
1237
   }
1238

1239
   assert(hint == NULL || hint == cache[curr]);
12,430✔
1240
   return cache[curr];
12,430✔
1241
}
1242

1243
static tree_t cached_std(tree_t hint)
12,202✔
1244
{
1245
   static tree_t standard_cache[STD_19 + 1] = {};
12,202✔
1246
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
12,202✔
1247
}
1248

1249
static tree_t search_type_decls(tree_t container, ident_t name)
12,384✔
1250
{
1251
   const int ndecls = tree_decls(container);
12,384✔
1252

1253
   for (int i = 0; i < ndecls; i++) {
794,354✔
1254
      tree_t d = tree_decl(container, i);
794,354✔
1255
      if (is_type_decl(d) && tree_ident(d) == name)
794,354✔
1256
         return d;
12,384✔
1257
   }
1258

1259
   return NULL;
1260
}
1261

1262
type_t std_type(tree_t std, std_type_t which)
923,821✔
1263
{
1264
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
923,821✔
1265
   assert(which < ARRAY_LEN(cache));
923,821✔
1266

1267
   if (cache[which] == NULL) {
923,821✔
1268
      const char *names[] = {
12,202✔
1269
         "universal_integer",
1270
         "universal_real",
1271
         "INTEGER",
1272
         "REAL",
1273
         "BOOLEAN",
1274
         "STRING",
1275
         "TIME",
1276
         "BIT",
1277
         "FILE_OPEN_KIND",
1278
         "FILE_OPEN_STATUS",
1279
         "NATURAL",
1280
         "BIT_VECTOR",
1281
         "SEVERITY_LEVEL",
1282
         "FILE_ORIGIN_KIND",
1283
         "FILE_OPEN_STATE",
1284
      };
1285

1286
      tree_t d = search_type_decls(cached_std(std), ident_new(names[which]));
12,202✔
1287
      if (d == NULL)
12,202✔
UNCOV
1288
         fatal_trace("cannot find standard type %s", names[which]);
×
1289

1290
      // Do not cache standard types while bootstrapping as the GC will
1291
      // move the objects after parsing
1292
      static int can_cache = -1;
12,202✔
1293
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
12,202✔
1294

1295
      if (can_cache)
12,202✔
1296
         return (cache[which] = tree_type(d));
12,012✔
1297
      else
1298
         return tree_type(d);
190✔
1299
   }
1300
   else
1301
      return cache[which];
1302
}
1303

1304
type_t ieee_type(ieee_type_t which)
441✔
1305
{
1306
   static type_t cache[IEEE_STD_LOGIC + 1] = {};
441✔
1307
   assert(which < ARRAY_LEN(cache));
441✔
1308

1309
   if (cache[which] == NULL) {
441✔
1310
      const char *names[] = {
71✔
1311
         "STD_ULOGIC",
1312
         "STD_LOGIC",
1313
      };
1314

1315
      static tree_t ieee_cache[STD_19 + 1] = {};
71✔
1316
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
71✔
1317

1318
      tree_t d = search_type_decls(unit, ident_new(names[which]));
71✔
1319
      if (d == NULL)
71✔
UNCOV
1320
         fatal_trace("cannot find IEEE type %s", names[which]);
×
1321

1322
      // STD.STANDARD cannot depend on IEEE
1323
      assert(!opt_get_int(OPT_BOOTSTRAP));
71✔
1324

1325
      return (cache[which] = tree_type(d));
71✔
1326
   }
1327
   else
1328
      return cache[which];
1329
}
1330

1331
static tree_t cached_verilog(void)
125✔
1332
{
1333
   static tree_t verilog_cache[STD_19 + 1] = {};
125✔
1334
   return cached_unit(NULL, verilog_cache, W_NVC, W_NVC_VERILOG);
125✔
1335
}
1336

1337
type_t verilog_type(verilog_type_t which)
206✔
1338
{
1339
   static type_t cache[VERILOG_RESOLVED_NET_ARRAY + 1] = {};
206✔
1340
   assert(which < ARRAY_LEN(cache));
206✔
1341

1342
   if (cache[which] == NULL) {
206✔
1343
      const char *names[] = {
79✔
1344
         "T_LOGIC",
1345
         "T_PACKED_LOGIC",
1346
         "T_INT64",
1347
         "T_NET_VALUE",
1348
         "T_NET_ARRAY",
1349
         "T_RESOLVED_NET",
1350
         "T_RESOLVED_NET_ARRAY",
1351
      };
1352

1353
      tree_t d = search_type_decls(cached_verilog(), ident_new(names[which]));
79✔
1354
      if (d == NULL)
79✔
UNCOV
1355
         fatal_trace("cannot find NVC.VERILOG type %s", names[which]);
×
1356

1357
      // STD.STANDARD cannot depend on NVC.VERILOG
1358
      assert(!opt_get_int(OPT_BOOTSTRAP));
79✔
1359

1360
      return (cache[which] = tree_type(d));
79✔
1361
   }
1362
   else
1363
      return cache[which];
1364
}
1365

1366
type_t reflection_type(reflect_type_t which)
183✔
1367
{
1368
   static type_t cache[REFLECT_SUBTYPE_MIRROR + 1] = {};
183✔
1369
   assert(which < ARRAY_LEN(cache));
183✔
1370

1371
   if (cache[which] == NULL) {
183✔
1372
      const char *names[] = {
32✔
1373
         "VALUE_MIRROR",
1374
         "SUBTYPE_MIRROR",
1375
      };
1376

1377
      static tree_t reflect_cache[STD_19 + 1] = {};
32✔
1378
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
32✔
1379

1380
      tree_t d = search_type_decls(unit, ident_new(names[which]));
32✔
1381
      if (d == NULL)
32✔
UNCOV
1382
         fatal_trace("cannot find REFLECTION type %s", names[which]);
×
1383

1384
      // STD.STANDARD cannot depend on REFLECTION
1385
      assert(!opt_get_int(OPT_BOOTSTRAP));
32✔
1386

1387
      return (cache[which] = tree_type(d));
32✔
1388
   }
1389
   else
1390
      return cache[which];
1391
}
1392

1393
bool is_open_coded_builtin(subprogram_kind_t kind)
407,565✔
1394
{
1395
   switch (kind) {
407,565✔
1396
   case S_ADD:
1397
   case S_SUB:
1398
   case S_DIV:
1399
   case S_MUL:
1400
   case S_MUL_PR:
1401
   case S_MUL_RP:
1402
   case S_MUL_PI:
1403
   case S_MUL_IP:
1404
   case S_DIV_PR:
1405
   case S_DIV_PP:
1406
   case S_DIV_PI:
1407
   case S_IDENTITY:
1408
   case S_NEGATE:
1409
   case S_SCALAR_LT:
1410
   case S_SCALAR_LE:
1411
   case S_SCALAR_GT:
1412
   case S_SCALAR_GE:
1413
   case S_SCALAR_EQ:
1414
   case S_SCALAR_NEQ:
1415
   case S_ABS:
1416
   case S_MOD:
1417
   case S_REM:
1418
   case S_EXP:
1419
   case S_MUL_RI:
1420
   case S_MUL_IR:
1421
   case S_DIV_RI:
1422
   case S_CONCAT:
1423
   case S_SCALAR_AND:
1424
   case S_SCALAR_OR:
1425
   case S_SCALAR_NOT:
1426
   case S_SCALAR_NAND:
1427
   case S_SCALAR_NOR:
1428
   case S_SCALAR_XOR:
1429
   case S_SCALAR_XNOR:
1430
   case S_FILE_OPEN1:
1431
   case S_FILE_OPEN2:
1432
   case S_FILE_READ:
1433
   case S_FILE_WRITE:
1434
   case S_DEALLOCATE:
1435
      return true;
1436
   default:
160,122✔
1437
      return false;
160,122✔
1438
   }
1439
}
1440

UNCOV
1441
tree_t std_func(ident_t mangled)
×
1442
{
UNCOV
1443
   tree_t std = cached_std(NULL);
×
1444

UNCOV
1445
   const int ndecls = tree_decls(std);
×
UNCOV
1446
   for (int i = 0; i < ndecls; i++) {
×
UNCOV
1447
      tree_t d = tree_decl(std, i);
×
UNCOV
1448
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
×
UNCOV
1449
         return d;
×
1450
   }
1451

1452
   return NULL;
1453
}
1454

1455
tree_t verilog_func(ident_t mangled)
46✔
1456
{
1457
   tree_t pack = cached_verilog();
46✔
1458

1459
   const int ndecls = tree_decls(pack);
46✔
1460
   for (int i = 0; i < ndecls; i++) {
3,430✔
1461
      tree_t d = tree_decl(pack, i);
3,430✔
1462
      if (is_subprogram(d) && tree_ident2(d) == mangled)
3,430✔
1463
         return d;
46✔
1464
   }
1465

UNCOV
1466
   fatal_trace("missing Verilog helper function %s", istr(mangled));
×
1467
}
1468

1469
tree_t name_to_ref(tree_t name)
95,434✔
1470
{
1471
   tree_kind_t kind;
95,434✔
1472
   while ((kind = tree_kind(name)) != T_REF) {
109,763✔
1473
      switch (kind) {
15,931✔
1474
      case T_ARRAY_REF:
14,329✔
1475
      case T_ARRAY_SLICE:
1476
      case T_RECORD_REF:
1477
      case T_ALL:
1478
         name = tree_value(name);
14,329✔
1479
         break;
14,329✔
1480
      default:
1481
         return NULL;
1482
      }
1483
   }
1484

1485
   return name;
1486
}
1487

1488
const char *port_mode_str(port_mode_t mode)
40✔
1489
{
1490
   const char *mode_str[] = {
40✔
1491
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE", "VIEW", "VIEW"
1492
   };
1493
   assert(mode < ARRAY_LEN(mode_str));
40✔
1494
   return mode_str[mode];
40✔
1495
}
1496

1497
void mangle_one_type(text_buf_t *buf, type_t type)
166,268✔
1498
{
1499
   ident_t ident = type_ident(type);
166,268✔
1500

1501
   char code = 0;
166,268✔
1502
   switch (is_well_known(ident)) {
166,268✔
1503
   case W_STD_INTEGER:        code = 'I'; break;
1504
   case W_STD_STRING:         code = 'S'; break;
3,260✔
1505
   case W_STD_REAL:           code = 'R'; break;
3,523✔
1506
   case W_STD_BOOL:           code = 'B'; break;
22,249✔
1507
   case W_STD_CHAR:           code = 'C'; break;
651✔
1508
   case W_STD_TIME:           code = 'T'; break;
914✔
1509
   case W_STD_NATURAL:        code = 'N'; break;
4,187✔
1510
   case W_STD_POSITIVE:       code = 'P'; break;
347✔
1511
   case W_STD_BIT:            code = 'J'; break;
2,434✔
1512
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,435✔
1513
   case W_IEEE_LOGIC:         code = 'L'; break;
250✔
1514
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,755✔
1515
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
1,563✔
1516
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,397✔
1517
   default: break;
1518
   }
1519

1520
   if (code)
47,965✔
1521
      tb_append(buf, code);
60,248✔
1522
   else {
1523
      tb_printf(buf, "%zu", ident_len(ident));
106,020✔
1524
      tb_istr(buf, ident);
106,020✔
1525
   }
1526
}
166,268✔
1527

1528
tree_t primary_unit_of(tree_t unit)
24,212✔
1529
{
1530
   switch (tree_kind(unit)) {
24,212✔
1531
   case T_ENTITY:
1532
   case T_COMPONENT:
1533
   case T_PACKAGE:
1534
   case T_BLOCK:
1535
   case T_ELAB:
1536
   case T_PACK_INST:
1537
      return unit;
1538
   case T_ARCH:
14,797✔
1539
   case T_CONFIGURATION:
1540
   case T_PACK_BODY:
1541
      return tree_primary(unit);
14,797✔
UNCOV
1542
   default:
×
UNCOV
1543
      fatal_trace("invalid kind %s in primary_unit_of",
×
1544
                  tree_kind_str(tree_kind(unit)));
1545
   }
1546
}
1547

1548
unsigned get_case_choice_char(tree_t value, int depth)
7,729✔
1549
{
1550
   switch (tree_kind(value)) {
8,361✔
1551
   case T_STRING:
7,611✔
1552
      if (depth < tree_chars(value))
7,611✔
1553
         return assume_int(tree_char(value, depth));
7,610✔
1554
      else
1555
         return ~0;   // Out of bounds
1556

1557
   case T_AGGREGATE:
14✔
1558
      {
1559
         const int nassocs = tree_assocs(value);
14✔
1560
         type_t type = tree_type(value);
14✔
1561

1562
         for (int i = 0; i < nassocs; i++) {
24✔
1563
            tree_t a = tree_assoc(value, i);
23✔
1564
            switch (tree_subkind(a)) {
23✔
UNCOV
1565
            case A_NAMED:
×
UNCOV
1566
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
UNCOV
1567
                  return assume_int(tree_value(a));
×
1568
               break;
1569

1570
            case A_POS:
23✔
1571
               if (tree_pos(a) == (unsigned)depth)
23✔
1572
                  return assume_int(tree_value(a));
13✔
1573
               break;
1574

UNCOV
1575
            case A_OTHERS:
×
UNCOV
1576
               return assume_int(tree_value(a));
×
1577
            }
1578
         }
1579

1580
         // This will produce an error during bounds checking
1581
         return ~0;
1582
      }
1583

1584
   case T_REF:
392✔
1585
      {
1586
         tree_t decl = tree_ref(value);
392✔
1587
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
392✔
1588
         assert(tree_has_value(decl));
392✔
1589
         return get_case_choice_char(tree_value(decl), depth);
392✔
1590
      }
1591

1592
   case T_ARRAY_SLICE:
240✔
1593
      {
1594
         tree_t base = tree_value(value);
240✔
1595
         tree_t r = tree_range(value, 0);
240✔
1596
         const int64_t rleft = assume_int(tree_left(r));
240✔
1597
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
240✔
1598
         return get_case_choice_char(base, depth + offset);
240✔
1599
      }
1600

1601
   case T_FCALL:
104✔
1602
      if (tree_subkind(tree_ref(value)) == S_CONCAT) {
104✔
1603
         const int nparams = tree_params(value);
104✔
1604
         for (int i = 0; i < nparams; i++) {
156✔
1605
            tree_t left = tree_value(tree_param(value, i));
156✔
1606

1607
            type_t left_type = tree_type(left);
156✔
1608
            if (type_is_unconstrained(left_type))
156✔
UNCOV
1609
               fatal_at(tree_loc(left), "sorry, this expression is not "
×
1610
                        "currently supported in a case choice");
1611

1612
            tree_t lr = range_of(tree_type(left), 0);
156✔
1613
            int64_t left_len;
156✔
1614
            if (!folded_length(lr, &left_len))
156✔
UNCOV
1615
               fatal_at(tree_loc(left), "cannot determine length of left hand "
×
1616
                        "side of concatenation");
1617

1618
            if (depth < left_len || i + 1 == nparams)
156✔
1619
               return get_case_choice_char(left, depth);
104✔
1620

1621
            depth -= left_len;
52✔
1622
         }
1623
      }
1624
      // Fall-through
1625

1626
   default:
UNCOV
1627
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1628
               tree_kind_str(tree_kind(value)));
1629
   }
1630
}
1631

1632
int64_t encode_case_choice(tree_t value, int length, int bits)
1,000✔
1633
{
1634
   uint64_t enc = 0;
1,000✔
1635
   for (int i = 0; i < length; i++) {
8,619✔
1636
      if (bits > 0) {
7,619✔
1637
         enc <<= bits;
1,637✔
1638
         enc |= get_case_choice_char(value, i);
1,637✔
1639
      }
1640
      else {
1641
         enc *= 0x27d4eb2d;
5,982✔
1642
         enc += get_case_choice_char(value, i);
5,982✔
1643
      }
1644
   }
1645

1646
   return enc;
1,000✔
1647
}
1648

1649
void to_string(text_buf_t *tb, type_t type, int64_t value)
559✔
1650
{
1651
   if (type_is_integer(type))
559✔
1652
      tb_printf(tb, "%"PRIi64, value);
425✔
1653
   else if (type_is_enum(type)) {
134✔
1654
      type_t base = type_base_recur(type);
62✔
1655
      if (value < 0 || value >= type_enum_literals(base))
62✔
1656
         tb_printf(tb, "%"PRIi64, value);
3✔
1657
      else
1658
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
59✔
1659
   }
1660
   else if (type_is_physical(type)) {
72✔
1661
      type_t base = type_base_recur(type);
24✔
1662
      const unsigned nunits = type_units(base);
24✔
1663
      tree_t max_unit = NULL;
24✔
1664
      int64_t max_unit_value = 0;
24✔
1665

1666
      // Find the largest unit that evenly divides the given value
1667
      for (unsigned u = 0; u < nunits; u++) {
216✔
1668
         tree_t unit = type_unit(base, u);
192✔
1669
         const int64_t unit_value = assume_int(tree_value(unit));
192✔
1670
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
192✔
1671
            max_unit = unit;
90✔
1672
            max_unit_value = unit_value;
90✔
1673
         }
1674
      }
1675
      assert(max_unit);
24✔
1676

1677
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
24✔
1678
                istr(tree_ident(max_unit)));
1679
   }
1680
   else if (type_is_real(type)) {
48✔
1681
      union { int64_t i; double r; } u = { .i = value };
42✔
1682
      tb_printf(tb, "%.17g", u.r);
42✔
1683
   }
1684
   else if (type_is_access(type)) {
6✔
1685
      if (value == 0)
6✔
1686
         tb_cat(tb, "NULL");
3✔
1687
      else
1688
         tb_printf(tb, "%p", (void *)value);
3✔
1689
   }
1690
}
559✔
1691

1692
static bool is_static(tree_t expr)
5,832✔
1693
{
1694
   switch (tree_kind(expr)) {
5,936✔
1695
   case T_REF:
675✔
1696
      {
1697
         tree_t decl = tree_ref(expr);
675✔
1698
         switch (tree_kind(decl)) {
675✔
1699
         case T_CONST_DECL:
179✔
1700
            return !(tree_flags(decl) & TREE_F_SEQ_BLOCK);
179✔
1701
         case T_UNIT_DECL:
1702
         case T_ENUM_LIT:
1703
         case T_GENERIC_DECL:
1704
            return true;
UNCOV
1705
         case T_ALIAS:
×
UNCOV
1706
            return is_static(tree_value(decl));
×
1707
         default:
179✔
1708
            return false;
179✔
1709
         }
1710
      }
1711

1712
   case T_LITERAL:
1713
   case T_STRING:
1714
      return true;
1715

1716
   case T_FCALL:
393✔
1717
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
393✔
1718
                                    | TREE_F_GLOBALLY_STATIC));
1719

1720
   case T_RECORD_REF:
101✔
1721
      return is_static(tree_value(expr));
101✔
1722

1723
   case T_ARRAY_REF:
60✔
1724
      {
1725
         if (!is_static(tree_value(expr)))
60✔
1726
            return false;
1727

1728
         const int nparams = tree_params(expr);
60✔
1729
         for (int i = 0; i < nparams; i++) {
120✔
1730
            if (!is_static(tree_value(tree_param(expr, i))))
60✔
1731
               return false;
1732
         }
1733

1734
         return true;
1735
      }
1736

1737
   case T_ARRAY_SLICE:
30✔
1738
      {
1739
         if (!is_static(tree_value(expr)))
30✔
1740
            return false;
1741

1742
         assert(tree_ranges(expr) == 1);
30✔
1743

1744
         tree_t r = tree_range(expr, 0);
30✔
1745
         if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
30✔
UNCOV
1746
            return false;
×
1747

1748
         return true;
1749
      }
1750

1751
   case T_ATTR_REF:
6✔
1752
      {
1753
         switch (tree_subkind(expr)) {
6✔
1754
         case ATTR_EVENT:
1755
         case ATTR_ACTIVE:
1756
         case ATTR_LAST_EVENT:
1757
         case ATTR_LAST_ACTIVE:
1758
         case ATTR_LAST_VALUE:
1759
         case ATTR_DRIVING:
1760
         case ATTR_DRIVING_VALUE:
1761
         case ATTR_STABLE:
1762
         case ATTR_QUIET:
1763
            return false;
1764
         case ATTR_POS:
3✔
1765
         case ATTR_VAL:
1766
         case ATTR_LEFTOF:
1767
         case ATTR_RIGHTOF:
1768
         case ATTR_SUCC:
1769
         case ATTR_PRED:
1770
         case ATTR_VALUE:
1771
         case ATTR_IMAGE:
1772
            assert(tree_params(expr) == 1);
3✔
1773
            return is_static(tree_value(tree_param(expr, 0)));
3✔
1774
         default:
3✔
1775
            return true;
3✔
1776
         }
1777
      }
1778

UNCOV
1779
   default:
×
UNCOV
1780
      return false;
×
1781
   }
1782
}
1783

1784
tree_t longest_static_prefix(tree_t expr)
21,498✔
1785
{
1786
   switch (tree_kind(expr)) {
21,498✔
1787
   case T_ARRAY_REF:
3,966✔
1788
      {
1789
         tree_t value = tree_value(expr);
3,966✔
1790
         tree_t prefix = longest_static_prefix(value);
3,966✔
1791

1792
         if (prefix != value)
3,966✔
1793
            return prefix;
1794

1795
         const int nparams = tree_params(expr);
3,944✔
1796
         for (int i = 0; i < nparams; i++) {
8,090✔
1797
            if (!is_static(tree_value(tree_param(expr, i))))
4,370✔
1798
               return prefix;
1799
         }
1800

1801
         return expr;
1802
      }
1803

1804
   case T_ARRAY_SLICE:
645✔
1805
      {
1806
         tree_t value = tree_value(expr);
645✔
1807
         tree_t prefix = longest_static_prefix(value);
645✔
1808

1809
         if (prefix != value)
645✔
1810
            return prefix;
1811

1812
         assert(tree_ranges(expr) == 1);
638✔
1813

1814
         tree_t r = tree_range(expr, 0);
638✔
1815
         if (tree_subkind(r) == RANGE_EXPR)
638✔
1816
            return prefix;
1817
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
632✔
1818
            return prefix;
13✔
1819

1820
         return expr;
1821
      }
1822

1823
   case T_RECORD_REF:
1,049✔
1824
      {
1825
         tree_t value = tree_value(expr);
1,049✔
1826
         tree_t prefix = longest_static_prefix(value);
1,049✔
1827

1828
         if (prefix != value)
1,049✔
1829
            return prefix;
12✔
1830

1831
         return expr;
1832
      }
1833

1834
   default:
1835
      return expr;
1836
   }
1837
}
1838

1839
tree_t body_of(tree_t pack)
1,298✔
1840
{
1841
   const tree_kind_t kind = tree_kind(pack);
1,298✔
1842
   if (kind == T_PACK_INST)
1,298✔
1843
      return NULL;
1844

1845
   assert(tree_kind(pack) == T_PACKAGE);
1,298✔
1846

1847
   ident_t body_i = well_known(W_BODY);
1,298✔
1848
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
1,298✔
1849
   return lib_get_qualified(body_name);
1,298✔
1850
}
1851

1852
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
2,468✔
1853
{
1854
   const int ngenmaps = tree_genmaps(unit);
2,468✔
1855

1856
   if (pos < ngenmaps) {
2,468✔
1857
      tree_t m = tree_genmap(unit, pos);
2,336✔
1858
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
2,336✔
1859
         return tree_value(m);
1,457✔
1860
   }
1861

1862
   for (int j = 0; j < ngenmaps; j++) {
4,554✔
1863
      tree_t m = tree_genmap(unit, j);
4,422✔
1864
      switch (tree_subkind(m)) {
4,422✔
1865
      case P_NAMED:
2,043✔
1866
         {
1867
            tree_t name = tree_name(m);
2,043✔
1868
            assert(tree_kind(name) == T_REF);
2,043✔
1869

1870
            if (tree_has_ref(name) && tree_ref(name) == g)
2,043✔
1871
               return tree_value(m);
436✔
1872
         }
1873
         break;
1874

1875
      case P_POS:
2,379✔
1876
         if (tree_pos(m) == pos)
2,379✔
1877
            return tree_value(m);
443✔
1878
         break;
1879

1880
      default:
1881
         break;
1882
      }
1883
   }
1884

1885
   return NULL;
1886
}
1887

1888
bool relaxed_rules(void)
826,176✔
1889
{
1890
   return opt_get_int(OPT_RELAXED);
826,176✔
1891
}
1892

1893
bool is_type_attribute(attr_kind_t kind)
66,421✔
1894
{
1895
   switch (kind) {
66,421✔
1896
   case ATTR_SUBTYPE:
1897
   case ATTR_BASE:
1898
   case ATTR_ELEMENT:
1899
   case ATTR_DESIGNATED_SUBTYPE:
1900
   case ATTR_INDEX:
1901
      return true;
1902
   default:
65,958✔
1903
      return false;
65,958✔
1904
   }
1905
}
1906

1907
bool attribute_has_param(attr_kind_t attr)
22,113✔
1908
{
1909
   switch (attr) {
22,113✔
1910
   case ATTR_IMAGE:
1911
   case ATTR_SUCC:
1912
   case ATTR_PRED:
1913
   case ATTR_DELAYED:
1914
   case ATTR_LEFTOF:
1915
   case ATTR_RIGHTOF:
1916
   case ATTR_VALUE:
1917
   case ATTR_POS:
1918
   case ATTR_LOW:
1919
   case ATTR_HIGH:
1920
   case ATTR_LEFT:
1921
   case ATTR_RIGHT:
1922
   case ATTR_LENGTH:
1923
   case ATTR_RANGE:
1924
   case ATTR_REVERSE_RANGE:
1925
   case ATTR_VAL:
1926
   case ATTR_QUIET:
1927
   case ATTR_STABLE:
1928
   case ATTR_INDEX:
1929
   case ATTR_ASCENDING:
1930
      return true;
1931
   default:
1,922✔
1932
      return false;
1,922✔
1933
   }
1934
}
1935

1936
type_t get_type_or_null(tree_t t)
1,998,518✔
1937
{
1938
   switch (tree_kind(t)) {
1,998,518✔
1939
   case T_LIBRARY:
1940
   case T_ATTR_SPEC:
1941
   case T_PACKAGE:
1942
   case T_PACK_INST:
1943
   case T_PACK_BODY:
1944
   case T_ENTITY:
1945
   case T_ARCH:
1946
   case T_PROCESS:
1947
   case T_COMPONENT:
1948
   case T_INSTANCE:
1949
   case T_CONCURRENT:
1950
   case T_BLOCK:
1951
   case T_WHILE:
1952
   case T_FOR:
1953
   case T_LOOP:
1954
   case T_GROUP_TEMPLATE:
1955
   case T_CONFIGURATION:
1956
   case T_GROUP:
1957
   case T_FOR_GENERATE:
1958
   case T_IF_GENERATE:
1959
   case T_USE:
1960
   case T_CONTEXT:
1961
   case T_PSL:
1962
   case T_WAVEFORM:
1963
      return NULL;
1964
   default:
1,919,996✔
1965
      if (tree_has_type(t))
1,919,996✔
1966
         return tree_type(t);
1,918,732✔
1967
      else
1968
         return NULL;
1969
   }
1970
}
1971

1972
type_t subtype_for_string(tree_t str, type_t base)
24,540✔
1973
{
1974
   if (type_const_bounds(base))
24,540✔
1975
      return base;    // Can be checked statically
1976
   else if (!type_is_unconstrained(base))
21,779✔
1977
      base = type_base_recur(base);
138✔
1978

1979
   // Construct a new constrained array subtype: the direction and
1980
   // bounds are the same as those for a positional array aggregate
1981
   type_t sub = type_new(T_SUBTYPE);
21,779✔
1982
   type_set_base(sub, base);
21,779✔
1983

1984
   type_t index_type = index_type_of(base, 0);
21,779✔
1985
   const bool is_enum = type_is_enum(index_type);
21,779✔
1986

1987
   // The direction is determined by the index type
1988
   range_kind_t dir = direction_of(index_type, 0);
21,779✔
1989

1990
   // The left bound is the left of the index type and the right bound
1991
   // is determined by the number of elements
1992

1993
   tree_t left = NULL, right = NULL;
21,779✔
1994

1995
   if (is_enum)
21,779✔
1996
      left = make_ref(type_enum_literal(type_base_recur(index_type), 0));
13✔
1997
   else
1998
      left = tree_left(range_of(index_type, 0));
21,766✔
1999

2000
   const int nchars = tree_chars(str);
21,779✔
2001
   int64_t iright;
21,779✔
2002
   if (dir == RANGE_DOWNTO)
21,779✔
UNCOV
2003
      iright = assume_int(left) - nchars + 1;
×
2004
   else
2005
      iright = assume_int(left) + nchars - 1;
21,779✔
2006

2007
   if (is_enum)
21,779✔
2008
      right = get_enum_lit(str, index_type, iright);
13✔
2009
   else
2010
      right = get_int_lit(str, index_type, iright);
21,766✔
2011

2012
   tree_t r = tree_new(T_RANGE);
21,779✔
2013
   tree_set_subkind(r, dir);
21,779✔
2014
   tree_set_left(r, left);
21,779✔
2015
   tree_set_right(r, right);
21,779✔
2016
   tree_set_loc(r, tree_loc(str));
21,779✔
2017
   tree_set_type(r, index_type);
21,779✔
2018

2019
   tree_t c = tree_new(T_CONSTRAINT);
21,779✔
2020
   tree_set_subkind(c, C_INDEX);
21,779✔
2021
   tree_add_range(c, r);
21,779✔
2022
   tree_set_loc(c, tree_loc(str));
21,779✔
2023

2024
   type_add_constraint(sub, c);
21,779✔
2025

2026
   return sub;
21,779✔
2027
}
2028

2029
tree_t change_ref(tree_t name, tree_t new)
1,999✔
2030
{
2031
   switch (tree_kind(name)) {
1,999✔
2032
   case T_REF:
1,417✔
2033
      return make_ref(new);
1,417✔
2034

2035
   case T_ARRAY_REF:
108✔
2036
      {
2037
         tree_t t = tree_new(T_ARRAY_REF);
108✔
2038
         tree_set_loc(t, tree_loc(name));
108✔
2039
         tree_set_value(t, change_ref(tree_value(name), new));
108✔
2040
         tree_set_type(t, tree_type(name));
108✔
2041

2042
         const int nparams = tree_params(name);
108✔
2043
         for (int i = 0; i < nparams; i++)
216✔
2044
            tree_add_param(t, tree_param(name, i));
108✔
2045

2046
         return t;
2047
      }
2048

2049
   case T_ARRAY_SLICE:
43✔
2050
      {
2051
         tree_t t = tree_new(T_ARRAY_SLICE);
43✔
2052
         tree_set_loc(t, tree_loc(name));
43✔
2053
         tree_set_value(t, change_ref(tree_value(name), new));
43✔
2054
         tree_set_type(t, tree_type(name));
43✔
2055
         tree_add_range(t, tree_range(name, 0));
43✔
2056

2057
         return t;
43✔
2058
      }
2059

2060
   case T_RECORD_REF:
354✔
2061
      {
2062
         tree_t t = tree_new(T_RECORD_REF);
354✔
2063
         tree_set_loc(t, tree_loc(name));
354✔
2064
         tree_set_value(t, change_ref(tree_value(name), new));
354✔
2065
         tree_set_type(t, tree_type(name));
354✔
2066
         tree_set_ident(t, tree_ident(name));
354✔
2067
         tree_set_ref(t, tree_ref(name));
354✔
2068

2069
         return t;
354✔
2070
      }
2071

2072
   case T_CONV_FUNC:
68✔
2073
      {
2074
         tree_t t = tree_new(T_CONV_FUNC);
68✔
2075
         tree_set_loc(t, tree_loc(name));
68✔
2076
         tree_set_value(t, change_ref(tree_value(name), new));
68✔
2077
         tree_set_ident(t, tree_ident(name));
68✔
2078
         tree_set_type(t, tree_type(name));
68✔
2079
         tree_set_ref(t, tree_ref(name));
68✔
2080

2081
         return t;
68✔
2082
      }
2083

2084
   case T_TYPE_CONV:
9✔
2085
      {
2086
         tree_t t = tree_new(T_TYPE_CONV);
9✔
2087
         tree_set_loc(t, tree_loc(name));
9✔
2088
         tree_set_type(t, tree_type(name));
9✔
2089
         tree_set_value(t, change_ref(tree_value(name), new));
9✔
2090

2091
         return t;
9✔
2092
      }
2093

UNCOV
2094
   default:
×
UNCOV
2095
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
×
2096
                  tree_kind_str(tree_kind(name)));
2097
   }
2098
}
2099

2100
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
2,516✔
2101
{
2102
   switch (tree_kind(expr)) {
2,516✔
2103
   case T_ARRAY_SLICE:
78✔
2104
      build_wait(tree_range(expr, 0), fn, ctx);
78✔
2105
      break;
78✔
2106

2107
   case T_ARRAY_REF:
423✔
2108
      {
2109
         const int nparams = tree_params(expr);
423✔
2110
         for (int i = 0; i < nparams; i++)
846✔
2111
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
423✔
2112
      }
2113
      break;
2114

2115
   default:
2116
      break;
2117
   }
2118
}
2,516✔
2119

2120
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
11,852✔
2121
{
2122
   // LRM 08 section 10.2 has rules for building a wait statement from a
2123
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2124
   // all-sensitised processes.
2125

2126
   switch (tree_kind(expr)) {
14,802✔
2127
   case T_REF:
3,588✔
2128
      if (class_of(tree_ref(expr)) == C_SIGNAL)
3,588✔
2129
         (*fn)(expr, ctx);
2,010✔
2130
      break;
2131

2132
   case T_EXTERNAL_NAME:
4✔
2133
      if (tree_class(expr) == C_SIGNAL)
4✔
2134
         (*fn)(expr, ctx);
4✔
2135
      break;
2136

2137
   case T_WAVEFORM:
2,520✔
2138
   case T_QUALIFIED:
2139
   case T_TYPE_CONV:
2140
   case T_INERTIAL:
2141
      if (tree_has_value(expr))
2,520✔
2142
         build_wait(tree_value(expr), fn, ctx);
2,511✔
2143
      break;
2144

2145
   case T_ASSERT:
365✔
2146
      build_wait(tree_value(expr), fn, ctx);
365✔
2147
      // Fall-through
2148
   case T_REPORT:
368✔
2149
      if (tree_has_message(expr))
368✔
2150
         build_wait(tree_message(expr), fn, ctx);
274✔
2151
      break;
2152

2153
   case T_ARRAY_REF:
706✔
2154
   case T_ARRAY_SLICE:
2155
   case T_RECORD_REF:
2156
      {
2157
         tree_t ref = name_to_ref(expr);
706✔
2158
         if (ref != NULL && class_of(ref) == C_SIGNAL
706✔
2159
             && longest_static_prefix(expr) == expr)
518✔
2160
            (*fn)(expr, ctx);
439✔
2161
         else {
2162
            build_wait(tree_value(expr), fn, ctx);
267✔
2163
            build_wait_for_target(expr, fn, ctx);
267✔
2164
         }
2165
      }
2166
      break;
2167

2168
   case T_FCALL:
1,971✔
2169
   case T_PCALL:
2170
   case T_PROT_FCALL:
2171
   case T_PROT_PCALL:
2172
      {
2173
         tree_t decl = tree_ref(expr);
1,971✔
2174
         const int nparams = tree_params(expr);
1,971✔
2175
         for (int i = 0; i < nparams; i++) {
5,474✔
2176
            tree_t p = tree_param(expr, i);
3,503✔
2177
            assert(tree_subkind(p) == P_POS);
3,503✔
2178

2179
            switch (tree_subkind(tree_port(decl, tree_pos(p)))) {
3,503✔
2180
            case PORT_IN:
3,496✔
2181
            case PORT_INOUT:
2182
            case PORT_ARRAY_VIEW:
2183
            case PORT_RECORD_VIEW:
2184
               build_wait(tree_value(p), fn, ctx);
3,496✔
2185
               break;
3,496✔
2186
            default:
2187
               break;
2188
            }
2189
         }
2190
      }
2191
      break;
2192

2193
   case T_AGGREGATE:
214✔
2194
      {
2195
         const int nassocs = tree_assocs(expr);
214✔
2196
         for (int i = 0; i < nassocs; i++) {
750✔
2197
            tree_t a = tree_assoc(expr, i);
536✔
2198
            build_wait(tree_value(a), fn, ctx);
536✔
2199

2200
            switch (tree_subkind(a)) {
536✔
2201
            case A_RANGE:
52✔
2202
            case A_SLICE:
2203
               build_wait(tree_range(a, 0), fn, ctx);
52✔
2204
               break;
52✔
2205
            case A_NAMED:
35✔
2206
               build_wait(tree_name(a), fn, ctx);
35✔
2207
               break;
35✔
2208
            }
2209
         }
2210
      }
2211
      break;
2212

2213
   case T_ATTR_REF:
218✔
2214
      {
2215
         const attr_kind_t predef = tree_subkind(expr);
218✔
2216
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
218✔
2217
            build_wait(tree_name(expr), fn, ctx);
63✔
2218

2219
         const int nparams = tree_params(expr);
218✔
2220
         for (int i = 0; i < nparams; i++)
226✔
2221
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
8✔
2222
      }
2223
      break;
2224

2225
   case T_LITERAL:
2226
   case T_STRING:
2227
   case T_DUMMY_DRIVER:
2228
      break;
2229

2230
   case T_IF:
191✔
2231
      {
2232
         const int nconds = tree_conds(expr);
191✔
2233
         for (int i = 0; i < nconds; i++)
529✔
2234
            build_wait(tree_cond(expr, i), fn, ctx);
338✔
2235
      }
2236
      break;
2237

2238
   case T_COND_STMT:
338✔
2239
      {
2240
         if (tree_has_value(expr))
338✔
2241
            build_wait(tree_value(expr), fn, ctx);
213✔
2242

2243
         const int nstmts = tree_stmts(expr);
338✔
2244
         for (int i = 0; i < nstmts; i++)
682✔
2245
            build_wait(tree_stmt(expr, i), fn, ctx);
344✔
2246
      }
2247
      break;
2248

2249
   case T_PROCESS:
52✔
2250
   case T_SEQUENCE:
2251
   case T_PROC_BODY:
2252
      {
2253
         const int ndecls = tree_decls(expr);
52✔
2254
         for (int i = 0; i < ndecls; i++) {
70✔
2255
            tree_t d = tree_decl(expr, i);
18✔
2256
            if (tree_kind(d) == T_PROC_BODY)
18✔
2257
               build_wait(d, fn, ctx);
2✔
2258
         }
2259

2260
         const int nstmts = tree_stmts(expr);
52✔
2261
         for (int i = 0; i < nstmts; i++)
116✔
2262
            build_wait(tree_stmt(expr, i), fn, ctx);
64✔
2263
      }
2264
      break;
2265

2266
   case T_SIGNAL_ASSIGN:
2,233✔
2267
      {
2268
         build_wait_for_target(tree_target(expr), fn, ctx);
2,233✔
2269

2270
         const int nwaves = tree_waveforms(expr);
2,233✔
2271
         for (int i = 0; i < nwaves; i++)
4,598✔
2272
            build_wait(tree_waveform(expr, i), fn, ctx);
2,365✔
2273
      }
2274
      break;
2275

2276
   case T_VAR_ASSIGN:
13✔
2277
   case T_FORCE:
2278
      build_wait_for_target(tree_target(expr), fn, ctx);
13✔
2279
      build_wait(tree_value(expr), fn, ctx);
13✔
2280
      break;
13✔
2281

2282
   case T_RELEASE:
3✔
2283
      build_wait_for_target(tree_target(expr), fn, ctx);
3✔
2284
      break;
3✔
2285

2286
   case T_CASE:
45✔
2287
   case T_MATCH_CASE:
2288
      {
2289
         build_wait(tree_value(expr), fn, ctx);
45✔
2290

2291
         const int nstmts = tree_stmts(expr);
45✔
2292
         for (int i = 0; i < nstmts; i++) {
276✔
2293
            tree_t alt = tree_stmt(expr, i);
231✔
2294

2295
            const int nstmts = tree_stmts(alt);
231✔
2296
            for (int j = 0; j < nstmts; j++)
462✔
2297
               build_wait(tree_stmt(alt, j), fn, ctx);
231✔
2298
         }
2299
      }
2300
      break;
2301

2302
   case T_FOR:
16✔
2303
      {
2304
         build_wait(tree_range(expr, 0), fn, ctx);
16✔
2305

2306
         const int nstmts = tree_stmts(expr);
16✔
2307
         for (int i = 0; i < nstmts; i++)
35✔
2308
            build_wait(tree_stmt(expr, i), fn, ctx);
19✔
2309
      }
2310
      break;
2311

2312
   case T_WHILE:
1✔
2313
      build_wait(tree_value(expr), fn, ctx);
1✔
2314
      // Fall-through
2315
   case T_LOOP:
4✔
2316
      {
2317
         const int nstmts = tree_stmts(expr);
4✔
2318
         for (int i = 0; i < nstmts; i++)
20✔
2319
            build_wait(tree_stmt(expr, i), fn, ctx);
16✔
2320
      }
2321
      break;
2322

2323
   case T_NEXT:
9✔
2324
   case T_EXIT:
2325
      if (tree_has_value(expr))
9✔
2326
         build_wait(tree_value(expr), fn, ctx);
6✔
2327
      break;
2328

2329
   case T_RANGE:
146✔
2330
      if (tree_subkind(expr) == RANGE_EXPR)
146✔
2331
         build_wait(tree_value(expr), fn, ctx);
13✔
2332
      else {
2333
         build_wait(tree_left(expr), fn, ctx);
133✔
2334
         build_wait(tree_right(expr), fn, ctx);
133✔
2335
      }
2336
      break;
2337

UNCOV
2338
   default:
×
UNCOV
2339
      fatal_trace("Cannot handle tree kind %s in wait expression",
×
2340
                  tree_kind_str(tree_kind(expr)));
2341
   }
2342
}
11,852✔
2343

2344
void print_syntax(const char *fmt, ...)
1,529✔
2345
{
2346
   LOCAL_TEXT_BUF tb = tb_new();
1,529✔
2347
   bool highlighting = false;
1,529✔
2348
   static bool comment = false, last_was_newline = false;
1,529✔
2349
   for (const char *p = fmt; *p != '\0'; p++) {
8,442✔
2350
      if (comment) {
6,913✔
2351
         if (*p == '\n' || *p == '\r') {
2,973✔
2352
            comment = false;
92✔
2353
            last_was_newline = true;
92✔
2354
            tb_printf(tb, "$$\n");
92✔
2355
         }
2356
         else if (*p != '~' && *p != '#') {
2,881✔
2357
            tb_append(tb, *p);
2,721✔
2358
            last_was_newline = false;
2,721✔
2359
         }
2360
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
2,973✔
2361
            tb_printf(tb, "$$");
1✔
2362
            comment = false;
1✔
2363
            last_was_newline = false;
1✔
2364
         }
2365
      }
2366
      else if (*p == '\r') {
3,940✔
2367
         if (!last_was_newline) {
18✔
UNCOV
2368
            tb_append(tb, '\n');
×
UNCOV
2369
            last_was_newline = true;
×
2370
         }
2371
      }
2372
      else if (*p == '#' && *(p + 1) != '#') {
3,922✔
2373
         tb_printf(tb, "$bold$$cyan$");
275✔
2374
         last_was_newline = false;
275✔
2375
         highlighting = true;
275✔
2376
      }
2377
      else if (*p == '~' && *(p + 1) != '~') {
3,647✔
2378
         tb_printf(tb, "$yellow$");
1✔
2379
         last_was_newline = false;
1✔
2380
         highlighting = true;
1✔
2381
      }
2382
      else if ((*p == '-' && *(p + 1) == '-')
3,646✔
2383
               || (*p == '/' && *(p + 1) == '/')
3,556✔
2384
               || (*p == '/' && *(p + 1) == '*')) {
3,554✔
2385
         tb_printf(tb, "$red$%c", *p);
93✔
2386
         last_was_newline = false;
93✔
2387
         comment = true;
93✔
2388
      }
2389
      else if (!isalnum_iso88591(*p) && *p != '_'
3,553✔
2390
               && *p != '%' && highlighting) {
1,793✔
2391
         tb_printf(tb, "$$%c", *p);
244✔
2392
         last_was_newline = false;
244✔
2393
         highlighting = false;
244✔
2394
      }
2395
      else {
2396
         tb_append(tb, *p);
3,309✔
2397
         last_was_newline = (*p == '\n');
3,309✔
2398
      }
2399
   }
2400

2401
   if (highlighting)
1,529✔
2402
      tb_cat(tb, "$$");
32✔
2403

2404
   va_list ap;
1,529✔
2405
   va_start(ap, fmt);
1,529✔
2406

2407
   if (syntax_buf != NULL) {
1,529✔
2408
      char *stripped LOCAL = strip_color(tb_get(tb), ap);
3,058✔
2409
      tb_cat(syntax_buf, stripped);
1,529✔
2410
   }
2411
   else
UNCOV
2412
      color_vprintf(tb_get(tb), ap);
×
2413

2414
   va_end(ap);
1,529✔
2415
}
1,529✔
2416

2417
void capture_syntax(text_buf_t *tb)
7✔
2418
{
2419
   assert(tb == NULL || syntax_buf == NULL);
7✔
2420
   syntax_buf = tb;
7✔
2421
}
7✔
2422

2423
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur)
3,238✔
2424
{
2425
   input_from_file(file);
3,238✔
2426

2427
   switch (source_kind()) {
3,238✔
2428
   case SOURCE_VHDL:
3,194✔
2429
      {
2430
         lib_t work = lib_work();
3,194✔
2431
         int base_errors = 0;
3,194✔
2432
         tree_t unit;
3,194✔
2433
         while (base_errors = error_count(), (unit = parse())) {
12,354✔
2434
            if (error_count() == base_errors) {
9,160✔
2435
               lib_put(work, unit);
9,159✔
2436
               unit_registry_purge(ur, tree_ident(unit));
9,159✔
2437

2438
               simplify_local(unit, jit, ur);
9,159✔
2439
               bounds_check(unit);
9,159✔
2440
            }
2441
            else
2442
               lib_put_error(work, unit);
1✔
2443
         }
2444
      }
2445
      break;
2446

2447
   case SOURCE_VERILOG:
44✔
2448
#ifdef ENABLE_VERILOG
2449
      {
2450
         LOCAL_TEXT_BUF tb = tb_new();
88✔
2451
         vlog_preprocess(tb);
44✔
2452

2453
         input_from_buffer(tb_get(tb), tb_len(tb), SOURCE_VERILOG);
44✔
2454

2455
         lib_t work = lib_work();
44✔
2456
         vlog_node_t module;
44✔
2457
         while ((module = vlog_parse())) {
139✔
2458
            if (error_count() == 0) {
51✔
2459
               vlog_check(module);
51✔
2460

2461
               if (error_count() == 0) {
51✔
2462
                  vlog_simp(module);
51✔
2463
                  lib_put_vlog(work, module);
51✔
2464
               }
2465
            }
2466
         }
2467
      }
2468
#else
2469
      fatal("Verilog is not currently supported");
2470
#endif
2471
      break;
44✔
2472
   }
2473
}
3,235✔
2474

2475
bool all_character_literals(type_t type)
1,796✔
2476
{
2477
   assert(type_is_enum(type));
1,796✔
2478

2479
   type_t base = type_base_recur(type);
1,796✔
2480
   const int nlits = type_enum_literals(base);
1,796✔
2481
   for (int i = 0; i < nlits; i++) {
6,871✔
2482
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
5,890✔
2483
         return false;
2484
   }
2485

2486
   return true;
2487
}
2488

2489
bool is_operator_symbol(ident_t ident)
30,118✔
2490
{
2491
   const int len = ident_len(ident);
30,118✔
2492
   if (len < 3)
30,118✔
2493
      return false;
2494
   else if (ident_char(ident, 0) != '"')
29,881✔
2495
      return false;
2496
   else if (ident_char(ident, len - 1) != '"')
18,749✔
2497
      return false;
2498

2499
   const well_known_t wk = is_well_known(ident);
18,749✔
2500

2501
   if (standard() < STD_08)
18,749✔
2502
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,288✔
2503
   else
2504
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
15,461✔
2505
}
2506

2507
bool same_tree(tree_t a, tree_t b)
6,715✔
2508
{
2509
   const tree_kind_t akind = tree_kind(a);
6,715✔
2510
   if (akind != tree_kind(b))
6,715✔
2511
      return false;
2512

2513
   switch (akind) {
6,608✔
2514
   case T_REF:
3,080✔
2515
      return tree_ref(a) == tree_ref(b);
3,080✔
2516
   case T_ARRAY_REF:
1,024✔
2517
      {
2518
         if (!same_tree(tree_value(a), tree_value(b)))
1,024✔
2519
            return false;
2520

2521
         const int nparams = tree_params(a);
1,005✔
2522
         assert(nparams == tree_params(b));
1,005✔
2523

2524
         for (int i = 0; i < nparams; i++) {
1,863✔
2525
            tree_t pa = tree_value(tree_param(a, i));
1,680✔
2526
            tree_t pb = tree_value(tree_param(b, i));
1,680✔
2527
            if (!same_tree(pa, pb))
1,680✔
2528
               return false;
2529
         }
2530

2531
         return true;
2532
      }
2533
   case T_ARRAY_SLICE:
38✔
2534
      {
2535
         if (!same_tree(tree_value(a), tree_value(b)))
38✔
2536
            return false;
2537

2538
         tree_t ra = tree_range(a, 0);
38✔
2539
         tree_t rb = tree_range(b, 0);
38✔
2540

2541
         const range_kind_t rakind = tree_subkind(ra);
38✔
2542
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
38✔
2543
            return false;
2544

2545
         return same_tree(tree_left(ra), tree_left(rb))
38✔
2546
            && same_tree(tree_right(ra), tree_right(rb));
38✔
2547
      }
2548

2549
   case T_RECORD_REF:
736✔
2550
      return tree_ident(a) == tree_ident(b)
736✔
2551
         && same_tree(tree_value(a), tree_value(b));
736✔
2552

2553
   case T_LITERAL:
1,730✔
2554
      {
2555
         const literal_kind_t alkind = tree_subkind(a);
1,730✔
2556
         if (alkind != tree_subkind(b) || alkind != L_INT)
1,730✔
2557
            return false;
2558
         else
2559
            return tree_ival(a) == tree_ival(b);
1,730✔
2560
      }
2561
   default:
2562
      return false;
2563
   }
2564
}
2565

2566
void instance_name_to_path(text_buf_t *tb, const char *str)
9,106✔
2567
{
2568
   bool delete = false;
9,106✔
2569
   for (const char *p = str; *p; p++) {
591,480✔
2570
      if (*p == '@' || (*p == '(' && !isdigit_iso88591(*(p + 1))))
582,374✔
2571
         delete = true;
2572
      else if (*p == ')' && delete)
552,656✔
2573
         delete = false;
2574
      else if (*p == ':') {
533,265✔
2575
         delete = false;
35,172✔
2576
         tb_append(tb, ':');
35,172✔
2577
      }
2578
      else if (!delete)
498,093✔
2579
         tb_append(tb, *p);
297,039✔
2580
   }
2581
}
9,106✔
2582

2583
bool calculate_aggregate_bounds(tree_t expr, range_kind_t *kind,
2,546✔
2584
                                int64_t *left, int64_t *right)
2585
{
2586
   // Calculate the direction and bounds of an array aggregate using the
2587
   // rules in LRM 93 7.3.2.2
2588

2589
   type_t type = tree_type(expr);
2,546✔
2590
   type_t index_type = index_type_of(type, 0);
2,546✔
2591
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
2,546✔
2592

2593
   int64_t low, high;
2,546✔
2594
   if (!folded_bounds(index_r, &low, &high))
2,546✔
2595
      return false;
2596

2597
   int64_t clow = INT64_MAX, chigh = INT64_MIN;  // Actual bounds computed below
2,531✔
2598

2599
   range_kind_t dir;
2,531✔
2600
   if (type_is_unconstrained(type))
2,531✔
2601
      dir = tree_subkind(index_r);
2,303✔
2602
   else {
2603
      base_r = range_of(type, 0);
228✔
2604
      dir = tree_subkind(base_r);
228✔
2605
   }
2606

2607
   const int nassocs = tree_assocs(expr);
2,531✔
2608

2609
   if (standard() >= STD_08) {
2,531✔
2610
      // VHDL-2008 range association determines index direction for
2611
      // unconstrained aggregate when the expression type matches the
2612
      // array type
2613
      for (int i = 0; i < nassocs; i++) {
3,131✔
2614
         tree_t a = tree_assoc(expr, i);
1,993✔
2615
         if (tree_subkind(a) == A_SLICE)
1,993✔
2616
            dir = tree_subkind(tree_range(a, 0));
46✔
2617
      }
2618
   }
2619

2620
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
2,531✔
2621
      return false;
2622

2623
   int64_t pos = 0;
2624
   for (int i = 0; i < nassocs; i++) {
11,781✔
2625
      tree_t a = tree_assoc(expr, i);
10,102✔
2626
      int64_t ilow = 0, ihigh = 0;
10,102✔
2627
      const assoc_kind_t akind = tree_subkind(a);
10,102✔
2628

2629
      switch (akind) {
10,102✔
2630
      case A_NAMED:
489✔
2631
         {
2632
            tree_t name = tree_name(a);
489✔
2633
            if (folded_int(name, &ilow))
489✔
2634
               ihigh = ilow;
479✔
2635
            else
2636
               return false;
815✔
2637
         }
2638
         break;
479✔
2639

2640
      case A_RANGE:
1,170✔
2641
      case A_SLICE:
2642
         {
2643
            tree_t r = tree_range(a, 0);
1,170✔
2644
            const range_kind_t rkind = tree_subkind(r);
1,170✔
2645
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,170✔
2646
               tree_t left = tree_left(r), right = tree_right(r);
828✔
2647

2648
               int64_t ileft, iright;
828✔
2649
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
828✔
2650
                  ilow = (rkind == RANGE_TO ? ileft : iright);
398✔
2651
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
398✔
2652
               }
2653
               else
2654
                  return false;
430✔
2655
            }
2656
            else
2657
               return false;
2658
         }
2659
         break;
2660

2661
      case A_OTHERS:
2662
         return false;
2663

2664
      case A_POS:
8,401✔
2665
         if (i == 0) {
8,401✔
2666
            int64_t ileft;
1,112✔
2667
            if (folded_int(tree_left(base_r), &ileft))
1,112✔
2668
               ilow = ihigh = ileft;
1,112✔
2669
            else
UNCOV
2670
               return false;
×
2671
         }
2672
         else if (dir == RANGE_TO)
7,289✔
2673
            ilow = ihigh = clow + pos;
7,287✔
2674
         else
2675
            ilow = ihigh = chigh - pos;
2✔
2676
         pos++;
8,401✔
2677
         break;
8,401✔
2678

2679
      case A_CONCAT:
40✔
2680
         {
2681
            type_t value_type = tree_type(tree_value(a));
40✔
2682

2683
            int64_t length;
40✔
2684
            if (type_is_unconstrained(value_type))
40✔
2685
               return false;
31✔
2686
            else if (folded_length(range_of(value_type, 0), &length)) {
15✔
2687
               if (i == 0) {
9✔
2688
                  int64_t ileft;
2✔
2689
                  if (folded_int(tree_left(base_r), &ileft))
2✔
2690
                     ilow = ihigh = ileft;
2✔
2691
                  else
UNCOV
2692
                     return false;
×
2693
               }
2694
               else if (dir == RANGE_TO) {
7✔
2695
                  ilow = clow + pos;
7✔
2696
                  ihigh = ilow + length - 1;
7✔
2697
               }
2698
               else {
UNCOV
2699
                  ihigh = chigh - pos;
×
UNCOV
2700
                  ilow = ihigh - length + 1;
×
2701
               }
2702
               pos += length;
9✔
2703
            }
2704
            else
2705
               return false;
2706
         }
2707
         break;
2708
      }
2709

2710
      clow = MIN(clow, ilow);
9,287✔
2711
      chigh = MAX(chigh, ihigh);
9,287✔
2712
   }
2713

2714
   if (clow < low || chigh > high)
1,679✔
2715
      return false;   // Will raise a bounds check error later
2716

2717
   *kind = dir;
1,674✔
2718
   *left = dir == RANGE_TO ? clow : chigh;
1,674✔
2719
   *right = dir == RANGE_TO ? chigh : clow;
1,674✔
2720

2721
   return true;
1,674✔
2722
}
2723

2724
type_t calculate_aggregate_subtype(tree_t expr)
2,320✔
2725
{
2726
   range_kind_t dir;
2,320✔
2727
   int64_t ileft, iright;
2,320✔
2728
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
2,320✔
2729
      return NULL;
2730

2731
   type_t type = tree_type(expr);
1,656✔
2732

2733
   const int ndims = dimension_of(type);
1,656✔
2734
   type_t a0_type = NULL;
1,656✔
2735
   if (ndims > 1) {
1,656✔
2736
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
57✔
2737
      if (type_is_unconstrained(a0_type))
57✔
2738
         return NULL;
2739

2740
      assert(dimension_of(a0_type) == ndims - 1);
57✔
2741
   }
2742

2743
   type_t index_type = index_type_of(type, 0);
1,656✔
2744

2745
   tree_t left = get_discrete_lit(expr, index_type, ileft);
1,656✔
2746
   tree_t right = get_discrete_lit(expr, index_type, iright);
1,656✔
2747
   assert(left != NULL && right != NULL);
1,656✔
2748

2749
   type_t sub = type_new(T_SUBTYPE);
1,656✔
2750
   type_set_base(sub, type_base_recur(type));
1,656✔
2751

2752
   type_t elem = type_elem(type);
1,656✔
2753
   if (type_is_unconstrained(elem)) {
1,656✔
2754
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
86✔
2755
      if (!type_is_unconstrained(a0_type))
86✔
2756
         elem = a0_type;
42✔
2757
   }
2758

2759
   type_set_elem(sub, elem);
1,656✔
2760

2761
   tree_t cons = tree_new(T_CONSTRAINT);
1,656✔
2762
   tree_set_subkind(cons, C_INDEX);
1,656✔
2763

2764
   tree_t r = tree_new(T_RANGE);
1,656✔
2765
   tree_set_subkind(r, dir);
1,656✔
2766
   tree_set_type(r, index_type);
1,656✔
2767
   tree_set_left(r, left);
1,656✔
2768
   tree_set_right(r, right);
1,656✔
2769

2770
   tree_add_range(cons, r);
1,656✔
2771

2772
   for (int i = 1; i < ndims; i++)
1,713✔
2773
      tree_add_range(cons, range_of(a0_type, i - 1));
57✔
2774

2775
   type_add_constraint(sub, cons);
1,656✔
2776

2777
   return sub;
1,656✔
2778
}
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