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

nickg / nvc / 27865744029

20 Jun 2026 08:32AM UTC coverage: 92.264% (-0.005%) from 92.269%
27865744029

push

github

nickg
Bump version to 1.21.1

78741 of 85343 relevant lines covered (92.26%)

642699.33 hits per line

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

94.47
/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 "printf.h"
28
#include "scan.h"
29
#include "thread.h"
30
#include "type.h"
31
#include "vlog/vlog-phase.h"
32
#include "sdf/sdf-phase.h"
33
#include "sdf/sdf-util.h"
34

35
#include <assert.h>
36
#include <ctype.h>
37
#include <string.h>
38
#include <stdlib.h>
39
#include <inttypes.h>
40

41
static vhdl_standard_t  current_std  = STD_08;
42
static bool             have_set_std = false;
43
static ident_t          id_cache[NUM_WELL_KNOWN];
44
static text_buf_t      *syntax_buf = NULL;
45

46
int64_t assume_int(tree_t t)
1,079,410✔
47
{
48
   int64_t value;
1,079,410✔
49
   if (folded_int(t, &value))
1,079,410✔
50
      return value;
1,079,410✔
51

52
   fatal_at(tree_loc(t), "expression cannot be folded to "
×
53
            "an integer constant");
54
}
55

56
void range_bounds(tree_t r, int64_t *low, int64_t *high)
223,955✔
57
{
58
   assert(tree_kind(r) == T_RANGE);
223,955✔
59

60
   const int64_t left = assume_int(tree_left(r));
223,955✔
61
   const int64_t right = assume_int(tree_right(r));
223,955✔
62

63
   *low  = tree_subkind(r) == RANGE_TO ? left : right;
223,955✔
64
   *high = tree_subkind(r) == RANGE_TO ? right : left;
223,955✔
65
}
223,955✔
66

67
bool folded_int(tree_t t, int64_t *l)
7,390,522✔
68
{
69
   switch (tree_kind(t)) {
7,444,465✔
70
   case T_LITERAL:
4,197,890✔
71
      switch (tree_subkind(t)) {
4,197,890✔
72
      case L_PHYSICAL:
24,375✔
73
         if (tree_has_ref(t))
24,375✔
74
            return false;
75
         // Fall-through
76
      case L_INT:
77
         *l = tree_ival(t);
4,103,644✔
78
         return true;
4,103,644✔
79
      default:
80
         return false;
81
      }
82
   case T_QUALIFIED:
474✔
83
      return folded_int(tree_value(t), l);
474✔
84
   case T_REF:
2,961,460✔
85
      if (tree_has_ref(t)) {
2,961,460✔
86
         tree_t decl = tree_ref(t);
2,961,460✔
87
         switch (tree_kind(decl)) {
2,961,460✔
88
         case T_CONST_DECL:
65,212✔
89
            if (tree_has_value(decl))
65,212✔
90
               return folded_int(tree_value(decl), l);
52,835✔
91
            else
92
               return false;
93
         case T_ENUM_LIT:
2,801,589✔
94
            *l = tree_pos(decl);
2,801,589✔
95
            return true;
2,801,589✔
96
         case T_ALIAS:
634✔
97
            return folded_int(tree_value(decl), l);
634✔
98
         default:
99
            return false;
100
         }
101
      }
102
      // Fall-through
103
   default:
104
      return false;
105
   }
106
}
107

108
bool folded_real(tree_t t, double *l)
318,183✔
109
{
110
   switch (tree_kind(t)) {
318,192✔
111
   case T_LITERAL:
308,522✔
112
      if (tree_subkind(t) == L_REAL) {
308,522✔
113
         *l = tree_dval(t);
308,276✔
114
         return true;
308,276✔
115
      }
116
      else
117
         return false;
118
   case T_QUALIFIED:
9✔
119
      return folded_real(tree_value(t), l);
9✔
120
   default:
121
      return false;
122
   }
123
}
124

125
bool folded_length(tree_t r, int64_t *l)
88,550✔
126
{
127
   int64_t low, high;
88,550✔
128
   if (folded_bounds(r, &low, &high)) {
88,550✔
129
      *l = MAX(high - low + 1, 0);
83,909✔
130
      return true;
83,909✔
131
   }
132
   else
133
      return false;
134
}
135

136
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
2,950,148✔
137
{
138
   assert(tree_kind(r) == T_RANGE);
2,950,148✔
139

140
   const range_kind_t rkind = tree_subkind(r);
2,950,148✔
141

142
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
2,950,148✔
143
      return false;
144

145
   int64_t left, right;
2,939,919✔
146
   if (!folded_int(tree_left(r), &left))
2,939,919✔
147
      return false;
148
   else if (!folded_int(tree_right(r), &right))
2,780,924✔
149
      return false;
150

151
   switch (rkind) {
2,722,848✔
152
   case RANGE_TO:
2,519,658✔
153
      *low  = left;
2,519,658✔
154
      *high = right;
2,519,658✔
155
      return true;
2,519,658✔
156
   case RANGE_DOWNTO:
203,190✔
157
      *low  = right;
203,190✔
158
      *high = left;
203,190✔
159
      return true;
203,190✔
160
   default:
161
      return false;
162
   }
163
}
164

165
bool folded_bounds_real(tree_t r, double *low, double *high)
116,377✔
166
{
167
   assert(tree_kind(r) == T_RANGE);
116,377✔
168

169
   const range_kind_t rkind = tree_subkind(r);
116,377✔
170

171
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
116,377✔
172
      return false;
173

174
   double left, right;
116,349✔
175
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
116,349✔
176
      switch (rkind) {
116,252✔
177
      case RANGE_TO:
116,252✔
178
         *low  = left;
116,252✔
179
         *high = right;
116,252✔
180
         return true;
116,252✔
181
      case RANGE_DOWNTO:
×
182
         *low  = right;
×
183
         *high = left;
×
184
         return true;
×
185
      default:
186
         return false;
187
      }
188
   }
189
   else
190
      return false;
97✔
191
}
192

193
bool folded_bool(tree_t t, bool *b)
49,858✔
194
{
195
   if (tree_kind(t) == T_REF) {
49,858✔
196
      tree_t decl = tree_ref(t);
11,632✔
197
      if (tree_kind(decl) == T_ENUM_LIT
11,632✔
198
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
9,126✔
199
         *b = (tree_pos(decl) == 1);
9,126✔
200
         return true;
9,126✔
201
      }
202
   }
203

204
   return false;
205
}
206

207
tree_t get_enum_lit(tree_t t, type_t type, int pos)
9,546✔
208
{
209
   type_t enum_type = type_base_recur(type ?: tree_type(t));
9,546✔
210
   tree_t lit = type_enum_literal(enum_type, pos);
9,546✔
211

212
   tree_t b = tree_new(T_REF);
9,546✔
213
   tree_set_loc(b, tree_loc(t));
9,546✔
214
   tree_set_ref(b, lit);
9,546✔
215
   tree_set_type(b, enum_type);
9,546✔
216
   tree_set_ident(b, tree_ident(lit));
9,546✔
217

218
   return b;
9,546✔
219
}
220

221
tree_t get_int_lit(tree_t t, type_t type, int64_t i)
51,427✔
222
{
223
   tree_t f = tree_new(T_LITERAL);
51,427✔
224
   tree_set_subkind(f, L_INT);
51,427✔
225
   tree_set_ival(f, i);
51,427✔
226
   tree_set_loc(f, tree_loc(t));
51,427✔
227
   tree_set_type(f, type ?: tree_type(t));
51,427✔
228

229
   return f;
51,427✔
230
}
231

232
tree_t get_discrete_lit(tree_t t, type_t type, int64_t i)
5,910✔
233
{
234
   if (type_is_enum(type)) {
5,910✔
235
      type_t base = type_base_recur(type);
90✔
236
      const int maxlit = type_enum_literals(base);
90✔
237
      if (i >= maxlit)
90✔
238
         return NULL;
239
      else
240
         return get_enum_lit(t, type, i);
90✔
241
   }
242
   else
243
      return get_int_lit(t, type, i);
5,820✔
244
}
245

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

254
   return f;
×
255
}
256

257
bool parse_value(type_t type, const char *str, parsed_value_t *value)
127✔
258
{
259
   type_t base = type_base_recur(type);
127✔
260
   const type_kind_t basek = type_kind(base);
127✔
261

262
   if (basek == T_ARRAY && type_is_character_array(base)) {
127✔
263
      value->enums = NULL;
23✔
264

265
      int map[256];
23✔
266
      for (int i = 0; i < ARRAY_LEN(map); i++)
5,911✔
267
         map[i] = INT_MAX;
5,888✔
268

269
      type_t elem = type_elem(base);
23✔
270

271
      const int nlits = type_enum_literals(elem);
23✔
272
      for (int i = 0; i < nlits; i++) {
3,117✔
273
         ident_t id = tree_ident(type_enum_literal(elem, i));
3,094✔
274
         if (ident_char(id, 0) == '\'')
3,094✔
275
            map[(uint8_t)ident_char(id, 1)] = i;
2,314✔
276
      }
277

278
      while (map[(uint8_t)*str] == INT_MAX && isspace_iso88591(*str))
30✔
279
         ++str;
7✔
280

281
      int base = 0, dbits = 1;
23✔
282
      bool quoted = false;
23✔
283
      if (map['\"'] == INT_MAX) {
23✔
284
         if (str[0] == '\"') {
11✔
285
            quoted = true;
2✔
286
            str++;
2✔
287
         }
288
         else if (str[1] == '\"' && toupper_iso88591(str[0]) == 'X') {
9✔
289
            quoted = true;
3✔
290
            base = 16;
3✔
291
            dbits = 4;
3✔
292
            str += 2;
3✔
293
         }
294
      }
295

296
      const size_t max = strlen(str) * dbits;
23✔
297
      enum_array_t *array = xmalloc_flex(sizeof(enum_array_t), max, 1);
23✔
298

299
      int n = 0, m;
23✔
300
      for (; *str != '\0'; str++, n += dbits) {
122✔
301
         if (base > 0) {
84✔
302
            const bool extended = (isdigit_iso88591(*str) && *str < '0' + base)
20✔
303
               || (base > 10 && *str >= 'A' && *str < 'A' + base - 10)
4✔
304
               || (base > 10 && *str >= 'a' && *str < 'a' + base - 10);
15✔
305

306
            if (!extended)
12✔
307
               break;
308

309
            int digit = isdigit_iso88591(*str)
9✔
310
               ? (*str - '0')
8✔
311
               : 10 + (toupper_iso88591(*str) - 'A');
9✔
312

313
            bool valid = true;
9✔
314
            for (int i = dbits - 1; i >= 0; i--) {
45✔
315
               const uint8_t bit = (digit >> i) & 1 ? '1' : '0';
36✔
316
               valid &= (m = map[bit]) != INT_MAX;
36✔
317
               array->values[n + dbits - i - 1] = m;
36✔
318
            }
319

320
            if (!valid)
9✔
321
               break;
322
         }
323
         else if ((m = map[(uint8_t)*str]) == INT_MAX)
72✔
324
            break;
325
         else
326
            array->values[n] = m;
67✔
327
      }
328

329
      assert(n <= max);
23✔
330
      array->count = n;
23✔
331

332
      if (quoted && *str++ != '\"') {
23✔
333
         free(array);
2✔
334
         return false;
2✔
335
      }
336

337
      for (; *str; str++) {
29✔
338
         if (!isspace_iso88591(*str)) {
10✔
339
            free(array);
2✔
340
            return false;
2✔
341
         }
342
      }
343

344
      value->enums = array;
19✔
345
      return true;
19✔
346
   }
347

348
   while (isspace_iso88591(*str))
114✔
349
      ++str;
10✔
350

351
   switch (basek) {
104✔
352
   case T_INTEGER:
77✔
353
      {
354
         const bool is_negative = *str == '-';
77✔
355
         int num_digits = 0;
77✔
356

357
         if (is_negative) ++str;
77✔
358

359
         int64_t sum = 0;
360
         for (; isdigit_iso88591(*str) || (*str == '_'); str++) {
199✔
361
            if (*str != '_') {
122✔
362
               sum *= 10;
120✔
363
               sum += (*str - '0');
120✔
364
               num_digits++;
120✔
365
            }
366
         }
367

368
         value->integer = is_negative ? -sum : sum;
77✔
369

370
         if (num_digits == 0)
77✔
371
            return false;
372
      }
373
      break;
374

375
   case T_ENUM:
16✔
376
      {
377
         bool upcase = true;
16✔
378
         char *copy LOCAL = xstrdup(str), *p;
31✔
379
         for (p = copy; (*p != '\0') && !isspace_iso88591(*p); p++, str++) {
63✔
380
            if (*p == '\'')
47✔
381
               upcase = false;
382
            if (upcase)
25✔
383
               *p = toupper_iso88591(*p);
14✔
384
         }
385
         *p = '\0';
16✔
386

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

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

391
         const int nlits = type_enum_literals(base);
16✔
392
         for (int i = 0; i < nlits; i++) {
45✔
393
            if (tree_ident(type_enum_literal(base, i)) == id) {
44✔
394
               value->integer = i;
15✔
395
               break;
15✔
396
            }
397
         }
398

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

404
   case T_REAL:
6✔
405
      {
406
         char *eptr = NULL;
6✔
407
         value->real = strtod(str, &eptr);
6✔
408
         str = eptr;
6✔
409
      }
410
      break;
6✔
411

412
   case T_PHYSICAL:
5✔
413
      {
414
         char *eptr = NULL;
5✔
415
         double scale = strtod(str, &eptr);
5✔
416
         str = eptr;
5✔
417

418
         while (isspace_iso88591(*str)) ++str;
10✔
419

420
         char *copy LOCAL = xstrdup(str), *p;
10✔
421
         for (p = copy; *p && !isspace_iso88591(*p); p++, str++)
13✔
422
            *p = toupper_iso88591(*p);
8✔
423
         *p = '\0';
5✔
424

425
         if (p == copy)
5✔
426
            return false;
427

428
         ident_t id = ident_new(copy);
4✔
429

430
         value->integer = -1;
4✔
431

432
         const int nunits = type_units(base);
4✔
433
         for (int i = 0; i < nunits; i++) {
11✔
434
            tree_t u = type_unit(base, i);
11✔
435
            if (tree_ident(u) == id) {
11✔
436
               value->integer = scale * assume_int(tree_value(u));
4✔
437
               break;
4✔
438
            }
439
         }
440

441
         if (value->integer == -1)
4✔
442
            return false;
443
      }
444
      break;
445

446
   default:
447
      return false;
448
   }
449

450
   for (; *str; str++) {
113✔
451
      if (!isspace_iso88591(*str))
11✔
452
         return false;
453
   }
454

455
   return true;
456
}
457

458
tree_t make_ref(tree_t to)
8,600✔
459
{
460
   tree_t t = tree_new(T_REF);
8,600✔
461
   tree_set_ident(t, tree_ident(to));
8,600✔
462
   tree_set_ref(t, to);
8,600✔
463
   tree_set_type(t, tree_type(to));
8,600✔
464
   tree_set_loc(t, tree_loc(to));
8,600✔
465
   return t;
8,600✔
466
}
467

468
vhdl_standard_t standard(void)
4,490,407✔
469
{
470
   return current_std;
4,490,407✔
471
}
472

473
void set_standard(vhdl_standard_t s)
8,802✔
474
{
475
   current_std = s;
8,802✔
476
   have_set_std = true;
8,802✔
477
}
8,802✔
478

479
void set_default_standard(vhdl_standard_t s)
1,862✔
480
{
481
   if (!have_set_std)
1,862✔
482
      set_standard(s);
132✔
483
}
1,862✔
484

485
const char *standard_text(vhdl_standard_t s)
6,987✔
486
{
487
   static const char *text[] = {
6,987✔
488
      "1987", "1993", "2000", "2002", "2008", "2019"
489
   };
490

491
   if ((unsigned)s < ARRAY_LEN(text))
6,987✔
492
      return text[s];
6,987✔
493
   else
494
      return "????";
495
}
496

497
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
785✔
498
{
499
   switch (tree_kind(view)) {
2,400✔
500
   case T_REF:
1,045✔
501
      return find_element_mode_indication(tree_ref(view), field, converse);
1,045✔
502

503
   case T_ALIAS:
260✔
504
      return find_element_mode_indication(tree_value(view), field, converse);
260✔
505

506
   case T_ATTR_REF:
310✔
507
      assert(tree_subkind(view) == ATTR_CONVERSE);
310✔
508
      *converse = !*converse;
310✔
509
      return find_element_mode_indication(tree_name(view), field, converse);
310✔
510

511
   case T_VIEW_DECL:
785✔
512
      {
513
         type_t view_type = tree_type(view);
785✔
514
         assert(type_kind(view_type) == T_VIEW);
785✔
515

516
         const int nelems = type_fields(view_type);
785✔
517
         for (int i = 0; i < nelems; i++) {
1,288✔
518
            tree_t e = type_field(view_type, i);
1,288✔
519
            if (tree_ref(e) == field)
1,288✔
520
               return e;
785✔
521
         }
522

523
         return NULL;
524
      }
525

526
   default:
×
527
      fatal_trace("unhandled tree kind %s in find_element_mode_indication",
528
                  tree_kind_str(tree_kind(view)));
529
   }
530
}
531

532
port_mode_t converse_mode(tree_t port, bool converse)
495✔
533
{
534
   const port_mode_t mode = tree_subkind(port);
495✔
535
   switch (mode) {
495✔
536
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
230✔
537
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
224✔
538
   default: return mode;
539
   }
540
}
541

542
class_t class_of(tree_t t)
482,479✔
543
{
544
   switch (tree_kind(t)) {
543,071✔
545
   case T_VAR_DECL:
546
      return C_VARIABLE;
547
   case T_SIGNAL_DECL:
33,362✔
548
   case T_IMPLICIT_SIGNAL:
549
      return C_SIGNAL;
33,362✔
550
   case T_CONST_DECL:
15,362✔
551
      return C_CONSTANT;
15,362✔
552
   case T_PORT_DECL:
93,645✔
553
   case T_GENERIC_DECL:
554
   case T_PARAM_DECL:
555
   case T_EXTERNAL_NAME:
556
      return tree_class(t);
93,645✔
557
   case T_ENUM_LIT:
287,424✔
558
   case T_LITERAL:
559
   case T_STRING:
560
      return C_LITERAL;
287,424✔
561
   case T_FIELD_DECL:
36✔
562
   case T_ATTR_DECL:
563
      return C_DEFAULT;
36✔
564
   case T_VIEW_DECL:
2✔
565
      return C_VIEW;
2✔
566
   case T_UNIT_DECL:
10,900✔
567
      return C_UNITS;
10,900✔
568
   case T_ARCH:
30✔
569
      return C_ARCHITECTURE;
30✔
570
   case T_FUNC_DECL:
882✔
571
   case T_FUNC_BODY:
572
   case T_FUNC_INST:
573
   case T_FCALL:
574
   case T_PROT_FCALL:
575
      return C_FUNCTION;
882✔
576
   case T_PROC_DECL:
11,001✔
577
   case T_PROC_BODY:
578
   case T_PROC_INST:
579
   case T_PCALL:
580
   case T_PROT_PCALL:
581
      return C_PROCEDURE;
11,001✔
582
   case T_ENTITY:
87✔
583
      return C_ENTITY;
87✔
584
   case T_SUBTYPE_DECL:
891✔
585
      return C_SUBTYPE;
891✔
586
   case T_TYPE_DECL:
697✔
587
   case T_PROT_DECL:
588
   case T_PROT_BODY:
589
      return C_TYPE;
697✔
590
   case T_FILE_DECL:
256✔
591
      return C_FILE;
256✔
592
   case T_PROCESS:
561✔
593
   case T_BLOCK:
594
   case T_FOR:
595
   case T_INSTANCE:
596
   case T_CONCURRENT:
597
   case T_ELAB:
598
   case T_PSL_DECL:
599
   case T_PSL_DIRECT:
600
   case T_FOR_GENERATE:
601
   case T_IF_GENERATE:
602
   case T_CASE_GENERATE:
603
      return C_LABEL;
561✔
604
   case T_COMPONENT:
2✔
605
      return C_COMPONENT;
2✔
606
   case T_REF:
51,899✔
607
   case T_PROT_REF:
608
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
51,899✔
609
   case T_ARRAY_REF:
8,695✔
610
   case T_ARRAY_SLICE:
611
   case T_RECORD_REF:
612
   case T_ALL:
613
   case T_ALIAS:
614
   case T_QUALIFIED:
615
      return class_of(tree_value(t));
8,695✔
616
   case T_PACKAGE:
848✔
617
   case T_PACK_BODY:
618
   case T_PACK_INST:
619
      return C_PACKAGE;
848✔
620
   case T_CONFIGURATION:
1✔
621
      return C_CONFIGURATION;
1✔
622
   case T_LIBRARY:
2✔
623
      return C_LIBRARY;
2✔
624
   case T_ATTR_REF:
52✔
625
      switch (tree_subkind(t)) {
52✔
626
      case ATTR_DELAYED:
627
      case ATTR_STABLE:
628
      case ATTR_QUIET:
629
      case ATTR_TRANSACTION:
630
         return C_SIGNAL;
631
      default:
43✔
632
         return C_DEFAULT;
43✔
633
      }
634
   case T_CONTEXT:
×
635
      return C_CONTEXT;
×
636
   default:
×
637
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
638
   }
639
}
640

641
bool class_has_type(class_t c)
277,308✔
642
{
643
   switch (c) {
277,308✔
644
   case C_LABEL:
645
   case C_ENTITY:
646
   case C_ARCHITECTURE:
647
   case C_COMPONENT:
648
   case C_CONFIGURATION:
649
   case C_PACKAGE:
650
   case C_LIBRARY:
651
      return false;
652
   default:
276,602✔
653
      return true;
276,602✔
654
   }
655
}
656

657
const char *class_str(class_t c)
844✔
658
{
659
   static const char *strs[] = {
844✔
660
      "default", "signal", "variable", "constant", "file", "entity",
661
      "component", "configuration", "architecture", "function", "package",
662
      "type", "subtype", "label", "procedure", "literal", "units", "library",
663
      "context", "view",
664
   };
665
   assert(c < ARRAY_LEN(strs));
844✔
666
   return strs[c];
844✔
667
}
668

669
const char *assoc_kind_str(assoc_kind_t akind)
9✔
670
{
671
   switch (akind) {
9✔
672
   case A_NAMED:  return "named";
673
   case A_CONCAT:
2✔
674
   case A_POS:    return "positional";
2✔
675
   case A_OTHERS: return "others";
3✔
676
   case A_SLICE:
1✔
677
   case A_RANGE:  return "range";
1✔
678
   default:       return "??";
×
679
   }
680
}
681

682
bool is_subprogram(tree_t t)
1,017,154✔
683
{
684
   switch (tree_kind(t)) {
1,017,154✔
685
   case T_FUNC_DECL:
686
   case T_FUNC_BODY:
687
   case T_FUNC_INST:
688
   case T_PROC_DECL:
689
   case T_PROC_BODY:
690
   case T_PROC_INST:
691
      return true;
692
   case T_GENERIC_DECL:
6,418✔
693
      {
694
         const class_t class = tree_class(t);
6,418✔
695
         return class == C_FUNCTION || class == C_PROCEDURE;
6,418✔
696
      }
697
   default:
742,643✔
698
      return false;
742,643✔
699
   }
700
}
701

702
bool is_loop_stmt(tree_t t)
588✔
703
{
704
   const tree_kind_t kind = tree_kind(t);
588✔
705
   return kind == T_WHILE || kind == T_FOR || kind == T_LOOP;
588✔
706
}
707

708
bool is_container(tree_t t)
15,688✔
709
{
710
   switch (tree_kind(t)) {
15,688✔
711
   case T_FUNC_BODY:
712
   case T_PROC_BODY:
713
   case T_ENTITY:
714
   case T_ARCH:
715
   case T_PACKAGE:
716
   case T_PACK_BODY:
717
   case T_CONFIGURATION:
718
   case T_BLOCK:
719
   case T_PROT_BODY:
720
   case T_ELAB:
721
   case T_FOR:
722
   case T_PROCESS:
723
   case T_PACK_INST:
724
      return true;
725
   default:
13,664✔
726
      return false;
13,664✔
727
   }
728
}
729

730
bool is_concurrent_block(tree_t t)
1,088✔
731
{
732
   switch (tree_kind(t)) {
1,088✔
733
   case T_ARCH:
734
   case T_ENTITY:
735
   case T_BLOCK:
736
   case T_IF_GENERATE:
737
   case T_FOR_GENERATE:
738
   case T_CASE_GENERATE:
739
      return true;
740
   default:
174✔
741
      return false;
174✔
742
   }
743
}
744

745
bool is_package(tree_t t)
56,913✔
746
{
747
   switch (tree_kind(t)) {
56,913✔
748
   case T_PACKAGE:
749
   case T_PACK_BODY:
750
   case T_PACK_INST:
751
      return true;
752
   default:
1,369✔
753
      return false;
1,369✔
754
   }
755
}
756

757
bool is_design_unit(tree_t t)
69,195✔
758
{
759
   switch (tree_kind(t)) {
69,195✔
760
   case T_ENTITY:
761
   case T_ARCH:
762
   case T_PACKAGE:
763
   case T_PACK_BODY:
764
   case T_CONFIGURATION:
765
   case T_CONTEXT:
766
   case T_PACK_INST:
767
      return true;
768
   default:
20,326✔
769
      return false;
20,326✔
770
   }
771
}
772

773
bool is_literal(tree_t t)
79,990✔
774
{
775
   switch (tree_kind(t)) {
79,990✔
776
   case T_REF:
29,213✔
777
      return tree_has_ref(t) && tree_kind(tree_ref(t)) == T_ENUM_LIT;
31,436✔
778
   case T_LITERAL:
779
      return true;
780
   case T_STRING:
27,787✔
781
   default:
782
      return false;
27,787✔
783
   }
784
}
785

786
bool is_body(tree_t t)
13,499✔
787
{
788
   switch (tree_kind(t)) {
13,499✔
789
   case T_FUNC_BODY:
790
   case T_PROC_BODY:
791
   case T_PACK_BODY:
792
   case T_PROT_BODY:
793
      return true;
794
   default:
4,873✔
795
      return false;
4,873✔
796
   }
797
}
798

799
bool is_guarded_signal(tree_t decl)
19,893✔
800
{
801
   switch (tree_kind(decl)) {
19,893✔
802
   case T_PORT_DECL:
19,342✔
803
   case T_SIGNAL_DECL:
804
      return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
19,342✔
805
   default:
806
      return false;
807
   }
808
}
809

810
bool is_type_decl(tree_t t)
1,200,792✔
811
{
812
   switch (tree_kind(t)) {
1,200,792✔
813
   case T_TYPE_DECL:
814
   case T_SUBTYPE_DECL:
815
   case T_PROT_DECL:
816
   case T_PROT_BODY:
817
      return true;
818
   default:
938,352✔
819
      return false;
938,352✔
820
   }
821
}
822

823
tree_t aliased_type_decl(tree_t decl)
183,183✔
824
{
825
   switch (tree_kind(decl)) {
184,457✔
826
   case T_ALIAS:
1,326✔
827
      {
828
         tree_t value = tree_value(decl);
1,326✔
829
         const tree_kind_t kind = tree_kind(value);
1,326✔
830
         if (kind == T_REF && tree_has_ref(value))
1,326✔
831
            return aliased_type_decl(tree_ref(value));
1,274✔
832
         else if (kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
52✔
833
             return value;
834
         else
835
            return NULL;
46✔
836
      }
837
   case T_TYPE_DECL:
838
   case T_SUBTYPE_DECL:
839
   case T_PROT_DECL:
840
   case T_PROT_BODY:
841
      return decl;
842
   case T_GENERIC_DECL:
2,360✔
843
      if (tree_class(decl) == C_TYPE)
2,360✔
844
         return decl;
845
      else
846
         return NULL;
1,226✔
847
   default:
63,542✔
848
      return NULL;
63,542✔
849
   }
850
}
851

852
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
202,053✔
853
{
854
   tree_t p = tree_new(T_PARAM);
202,053✔
855
   tree_set_loc(p, tree_loc(value));
202,053✔
856
   tree_set_subkind(p, kind);
202,053✔
857
   tree_set_value(p, value);
202,053✔
858

859
   switch (kind) {
202,053✔
860
   case P_NAMED:
204✔
861
      assert(name != NULL);
204✔
862
      tree_set_name(p, name);
204✔
863
      break;
204✔
864
   case P_POS:
201,849✔
865
      tree_set_pos(p, tree_params(call));
201,849✔
866
      break;
201,849✔
867
   }
868

869
   tree_add_param(call, p);
202,053✔
870
   return p;
202,053✔
871
}
872

873
type_t array_aggregate_type(type_t array, int from_dim)
398✔
874
{
875
   if (type_is_none(array))
398✔
876
      return type_new(T_NONE);
2✔
877

878
   if (type_is_unconstrained(array)) {
396✔
879
      const int nindex = type_indexes(array);
57✔
880
      assert(from_dim < nindex);
57✔
881

882
      type_t type = type_new(T_ARRAY);
57✔
883
      type_set_ident(type, type_ident(array));
57✔
884
      type_set_elem(type, type_elem(array));
57✔
885

886
      for (int i = from_dim; i < nindex; i++)
114✔
887
         type_add_index(type, type_index(array, i));
57✔
888

889
      return type;
890
   }
891
   else {
892
      const int ndims = dimension_of(array);
339✔
893
      assert(from_dim < ndims);
339✔
894

895
      type_t base = type_new(T_ARRAY);
339✔
896
      type_set_ident(base, type_ident(array));
339✔
897
      type_set_elem(base, type_elem(array));
339✔
898

899
      tree_t constraint = tree_new(T_CONSTRAINT);
339✔
900
      tree_set_subkind(constraint, C_INDEX);
339✔
901

902
      type_t sub = type_new(T_SUBTYPE);
339✔
903
      type_set_base(sub, base);
339✔
904
      type_set_constraint(sub, constraint);
339✔
905

906
      for (int i = from_dim; i < ndims; i++) {
710✔
907
         tree_t r = range_of(array, i);
371✔
908
         if (r == NULL) {
371✔
909
            // Not enough constraints were provided for the type
910
            r = tree_new(T_RANGE);
1✔
911
            tree_set_subkind(r, RANGE_ERROR);
1✔
912
            tree_set_type(r, type_new(T_NONE));
1✔
913
         }
914

915
         type_add_index(base, tree_type(r));
371✔
916
         tree_add_range(constraint, r);
371✔
917
      }
918

919
      return sub;
920
   }
921
}
922

923
unsigned bits_for_range(int64_t low, int64_t high)
2,757,377✔
924
{
925
   if (low > high)
2,757,377✔
926
      return 0;   // Null range
927
   else if (low < 0) {
2,757,377✔
928
      // Signed integers
929
      if (low >= INT8_MIN && high <= INT8_MAX)
661,197✔
930
         return 8;
931
      else if (low >= INT16_MIN && high <= INT16_MAX)
661,112✔
932
         return 16;
933
      else if (low >= INT32_MIN && high <= INT32_MAX)
660,991✔
934
         return 32;
935
      else
936
         return 64;
97,789✔
937
   }
938
   else {
939
      // Unsigned integers
940
      if (high <= 1)
2,096,180✔
941
         return 1;
942
      else if (high <= UINT8_MAX)
1,371,608✔
943
         return 8;
944
      else if (high <= UINT16_MAX)
3,646✔
945
         return 16;
946
      else if (high <= UINT32_MAX)
3,441✔
947
         return 32;
948
      else
949
         return 64;
164✔
950
   }
951
}
952

953
unsigned dimension_of(type_t type)
2,278,683✔
954
{
955
   switch (type_kind(type)) {
4,302,562✔
956
   case T_SUBTYPE:
2,023,877✔
957
      return dimension_of(type_base(type));
2,023,877✔
958
   case T_GENERIC:
428✔
959
      switch (type_subkind(type)) {
428✔
960
      case GTYPE_ARRAY:
378✔
961
         return type_indexes(type);
378✔
962
      case GTYPE_ACCESS:
×
963
         return dimension_of(type_designated(type));
×
964
      case GTYPE_FILE:
965
      case GTYPE_PRIVATE:
966
         return 0;
967
      default:
50✔
968
         return 1;
50✔
969
      }
970
   case T_ARRAY:
2,269,034✔
971
      return type_indexes(type);
2,269,034✔
972
   case T_NONE:
973
   case T_RECORD:
974
   case T_INCOMPLETE:
975
   case T_FILE:
976
   case T_SIGNATURE:
977
   case T_VIEW:
978
      return 0;
979
   case T_INTEGER:
9,130✔
980
   case T_REAL:
981
   case T_PHYSICAL:
982
   case T_ENUM:
983
      return type_dims(type);
9,130✔
984
   case T_ACCESS:
2✔
985
      return dimension_of(type_designated(type));
2✔
986
   default:
×
987
      fatal_trace("invalid type kind %s in dimension_of",
988
                  type_kind_str(type_kind(type)));
989
   }
990
}
991

992
tree_t range_of(type_t type, unsigned dim)
3,471,035✔
993
{
994
   switch (type_kind(type)) {
3,515,596✔
995
   case T_SUBTYPE:
2,821,802✔
996
      if (type_has_constraint(type)) {
2,821,802✔
997
         tree_t c = type_constraint(type);
2,777,241✔
998
         switch (tree_subkind(c)) {
2,777,241✔
999
         case C_INDEX:
2,777,241✔
1000
         case C_RANGE:
1001
            if (dim < tree_ranges(c))
2,777,241✔
1002
               return tree_range(c, dim);
2,777,240✔
1003
            else
1004
               return NULL;   // Must be an error
1005
         default:
×
1006
            should_not_reach_here();
1007
         }
1008
      }
1009
      else
1010
         return range_of(type_base(type), dim);
44,561✔
1011
   case T_INTEGER:
693,794✔
1012
   case T_REAL:
1013
   case T_PHYSICAL:
1014
   case T_ENUM:
1015
      return type_dim(type, dim);
693,794✔
1016
   default:
×
1017
      fatal_trace("invalid type kind %s for %s in range_of",
1018
                  type_kind_str(type_kind(type)), type_pp(type));
1019
   }
1020
}
1021

1022
range_kind_t direction_of(type_t type, unsigned dim)
31,912✔
1023
{
1024
   switch (type_kind(type)) {
32,016✔
1025
   case T_ENUM:
1026
      return RANGE_TO;
1027
   case T_NONE:
×
1028
      return RANGE_ERROR;
×
1029
   case T_INTEGER:
31,883✔
1030
   case T_REAL:
1031
   case T_PHYSICAL:
1032
   case T_SUBTYPE:
1033
      {
1034
         tree_t r = range_of(type, dim);
31,883✔
1035
         const range_kind_t rkind = tree_subkind(r);
31,883✔
1036
         if (rkind == RANGE_EXPR) {
31,883✔
1037
            // Return a fixed direction if possible
1038
            tree_t value = tree_value(r);
196✔
1039
            assert(tree_kind(value) == T_ATTR_REF);
196✔
1040

1041
            DEBUG_ONLY({
196✔
1042
                  const attr_kind_t attr = tree_subkind(value);
1043
                  assert(attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE);
1044
               });
196✔
1045

1046
            tree_t name = tree_name(value);
196✔
1047
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
196✔
1048
               tree_t decl = tree_ref(name);
195✔
1049
               if (is_type_decl(decl))
195✔
1050
                  return direction_of(tree_type(decl), 0);
104✔
1051
            }
1052
         }
1053

1054
         return rkind;
1055
      }
1056
   default:
×
1057
      fatal_trace("invalid type kind %s in direction_of",
1058
                  type_kind_str(type_kind(type)));
1059
   }
1060
}
1061

1062
type_t index_type_of(type_t type, unsigned dim)
317,565✔
1063
{
1064
   if (dim >= dimension_of(type))
317,567✔
1065
      return NULL;
1066

1067
   type_t base = type_base_recur(type);
317,522✔
1068
   type_kind_t base_kind = type_kind(base);
317,522✔
1069
   if (base_kind == T_ARRAY)
317,522✔
1070
      return type_index(base, dim);
313,058✔
1071
   else if (base_kind == T_ENUM || base_kind == T_NONE)
4,464✔
1072
      return type;
1073
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
4,265✔
1074
      return type_index(base, dim);
228✔
1075
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
4,037✔
1076
      return NULL;
1077
   else if (base_kind == T_ACCESS)
4,012✔
1078
      return index_type_of(type_designated(type), dim);
2✔
1079
   else
1080
      return tree_type(range_of(base, dim));
4,010✔
1081
}
1082

1083
int64_t rebase_index(type_t array_type, int dim, int64_t value)
320✔
1084
{
1085
   // Convert value which is in the range of array_type to a zero-based index
1086
   tree_t r = range_of(array_type, dim);
320✔
1087
   const int64_t left = assume_int(tree_left(r));
320✔
1088
   return (tree_subkind(r) == RANGE_TO) ? value - left : left - value;
320✔
1089
}
1090

1091
ident_t well_known(well_known_t id)
562,331✔
1092
{
1093
   assert(id < NUM_WELL_KNOWN);
562,331✔
1094
   return id_cache[id];
562,331✔
1095
}
1096

1097
well_known_t is_well_known(ident_t ident)
339,678✔
1098
{
1099
   return ident_key(ident);
339,678✔
1100
}
1101

1102
void intern_strings(void)
9,104✔
1103
{
1104
   static const char *tab[] = {
9,104✔
1105
      [W_STD_STANDARD]   = "STD.STANDARD",
1106
      [W_ALL]            = "all",
1107
      [W_STD_BIT]        = "STD.STANDARD.BIT",
1108
      [W_STD_BOOL]       = "STD.STANDARD.BOOLEAN",
1109
      [W_STD_CHAR]       = "STD.STANDARD.CHARACTER",
1110
      [W_STD_NATURAL]    = "STD.STANDARD.NATURAL",
1111
      [W_STD_POSITIVE]   = "STD.STANDARD.POSITIVE",
1112
      [W_STD_INTEGER]    = "STD.STANDARD.INTEGER",
1113
      [W_STD_STRING]     = "STD.STANDARD.STRING",
1114
      [W_STD_REAL]       = "STD.STANDARD.REAL",
1115
      [W_STD_TIME]       = "STD.STANDARD.TIME",
1116
      [W_STD_BIT_VECTOR] = "STD.STANDARD.BIT_VECTOR",
1117

1118
      [W_IEEE_SIGNED]   = "IEEE.NUMERIC_STD.SIGNED",
1119
      [W_IEEE_UNSIGNED] = "IEEE.NUMERIC_STD.UNSIGNED",
1120
      [W_IEEE_LOGIC]    = "IEEE.STD_LOGIC_1164.STD_LOGIC",
1121
      [W_IEEE_ULOGIC]   = "IEEE.STD_LOGIC_1164.STD_ULOGIC",
1122

1123
      [W_IEEE_1164_AND]  = "IEEE.STD_LOGIC_1164.\"and\"",
1124
      [W_IEEE_1164_NAND] = "IEEE.STD_LOGIC_1164.\"nand\"",
1125
      [W_IEEE_1164_OR]   = "IEEE.STD_LOGIC_1164.\"or\"",
1126
      [W_IEEE_1164_NOR]  = "IEEE.STD_LOGIC_1164.\"nor\"",
1127
      [W_IEEE_1164_XOR]  = "IEEE.STD_LOGIC_1164.\"xor\"",
1128
      [W_IEEE_1164_XNOR] = "IEEE.STD_LOGIC_1164.\"xnor\"",
1129

1130
      [W_FOREIGN]         = "FOREIGN",
1131
      [W_WORK]            = "WORK",
1132
      [W_STD]             = "STD",
1133
      [W_THUNK]           = "thunk",
1134
      [W_BODY]            = "body",
1135
      [W_CARET]           = "^",
1136
      [W_IEEE]            = "IEEE",
1137
      [W_IEEE_1164]       = "IEEE.STD_LOGIC_1164",
1138
      [W_ERROR]           = "$error",
1139
      [W_ELAB]            = "elab",
1140
      [W_NUMERIC_STD]     = "IEEE.NUMERIC_STD",
1141
      [W_NUMERIC_BIT]     = "IEEE.NUMERIC_BIT",
1142
      [W_NVC]             = "NVC",
1143
      [W_DEFAULT_CLOCK]   = "default clock",
1144
      [W_STD_REFLECTION]  = "STD.REFLECTION",
1145
      [W_NEVER_WAITS]     = "NEVER_WAITS",
1146
      [W_NVC_VERILOG]     = "NVC.VERILOG",
1147
      [W_NVC_PSL_SUPPORT] = "NVC.PSL_SUPPORT",
1148
      [W_INSTANCE_NAME]   = "instance_name",
1149
      [W_PATH_NAME]       = "path_name",
1150
      [W_VITAL]           = "VITAL",
1151
      [W_RESOLUTION]      = "resolution",
1152
      [W_TEXT_UTIL]       = "NVC.TEXT_UTIL",
1153
      [W_VERILOG_LOGIC]   = "NVC.VERILOG.T_LOGIC",
1154
      [W_DLR_SIGNED]      = "$signed",
1155
      [W_DLR_UNSIGNED]    = "$unsigned",
1156
      [W_DLR_CLOG2]       = "$clog2",
1157
      [W_COUNTERS]        = "#counters",
1158

1159
      [W_IEEE_LOGIC_VECTOR]      = "IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR",
1160
      [W_IEEE_ULOGIC_VECTOR]     = "IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR",
1161
      [W_IEEE_1164_RISING_EDGE]  = "IEEE.STD_LOGIC_1164.RISING_EDGE(sU)B",
1162
      [W_IEEE_1164_FALLING_EDGE] = "IEEE.STD_LOGIC_1164.FALLING_EDGE(sU)B",
1163

1164
      [W_NUMERIC_STD_UNSIGNED] = "IEEE.NUMERIC_STD_UNSIGNED",
1165
      [W_NUMERIC_BIT_UNSIGNED] = "IEEE.NUMERIC_BIT_UNSIGNED",
1166
      [W_VERILOG_NET_VALUE]    = "NVC.VERILOG.T_NET_VALUE",
1167
      [W_VERILOG_WIRE_ARRAY]   = "NVC.VERILOG.T_WIRE_ARRAY",
1168

1169
      [W_OP_CCONV]               = "\"??\"",
1170
      [W_OP_AND]                 = "\"and\"",
1171
      [W_OP_OR]                  = "\"or\"",
1172
      [W_OP_NAND]                = "\"nand\"",
1173
      [W_OP_NOR]                 = "\"nor\"",
1174
      [W_OP_XOR]                 = "\"xor\"",
1175
      [W_OP_XNOR]                = "\"xnor\"",
1176
      [W_OP_EQUAL]               = "\"=\"",
1177
      [W_OP_NOT_EQUAL]           = "\"/=\"",
1178
      [W_OP_LESS_THAN]           = "\"<\"",
1179
      [W_OP_LESS_EQUAL]          = "\"<=\"",
1180
      [W_OP_GREATER_THAN]        = "\">\"",
1181
      [W_OP_GREATER_EQUAL]       = "\">=\"",
1182
      [W_OP_MATCH_EQUAL]         = "\"?=\"",
1183
      [W_OP_MATCH_NOT_EQUAL]     = "\"?/=\"",
1184
      [W_OP_MATCH_LESS_THAN]     = "\"?<\"",
1185
      [W_OP_MATCH_LESS_EQUAL]    = "\"?<=\"",
1186
      [W_OP_MATCH_GREATER_THAN]  = "\"?>\"",
1187
      [W_OP_MATCH_GREATER_EQUAL] = "\"?>=\"",
1188
      [W_OP_SLL]                 = "\"sll\"",
1189
      [W_OP_SRL]                 = "\"srl\"",
1190
      [W_OP_SLA]                 = "\"sla\"",
1191
      [W_OP_SRA]                 = "\"sra\"",
1192
      [W_OP_ROL]                 = "\"rol\"",
1193
      [W_OP_ROR]                 = "\"ror\"",
1194
      [W_OP_ADD]                 = "\"+\"",
1195
      [W_OP_MINUS]               = "\"-\"",
1196
      [W_OP_CONCAT]              = "\"&\"",
1197
      [W_OP_TIMES]               = "\"*\"",
1198
      [W_OP_DIVIDE]              = "\"/\"",
1199
      [W_OP_MOD]                 = "\"mod\"",
1200
      [W_OP_REM]                 = "\"rem\"",
1201
      [W_OP_EXPONENT]            = "\"**\"",
1202
      [W_OP_ABS]                 = "\"abs\"",
1203
      [W_OP_NOT]                 = "\"not\"",
1204
   };
1205

1206
   for (int i = 0; i < ARRAY_LEN(tab); i++)
855,776✔
1207
      id_cache[i] = ident_intern(i, tab[i]);
846,672✔
1208
}
9,104✔
1209

1210
bool is_uninstantiated_package(tree_t pack)
59,727✔
1211
{
1212
   return tree_kind(pack) == T_PACKAGE
59,727✔
1213
      && tree_generics(pack) > 0
57,805✔
1214
      && tree_genmaps(pack) == 0;
62,487✔
1215
}
1216

1217
bool is_uninstantiated_subprogram(tree_t decl)
128,449✔
1218
{
1219
   switch (tree_kind(decl)) {
128,449✔
1220
   case T_FUNC_DECL:
127,651✔
1221
   case T_FUNC_BODY:
1222
   case T_PROC_DECL:
1223
   case T_PROC_BODY:
1224
      return tree_generics(decl) > 0;
127,651✔
1225
   default:
1226
      return false;
1227
   }
1228
}
1229

1230
bool is_anonymous_subtype(type_t type)
282,596✔
1231
{
1232
   return type_kind(type) == T_SUBTYPE && !type_has_ident(type);
282,596✔
1233
}
1234

1235
bool unit_needs_cgen(tree_t unit)
351✔
1236
{
1237
   switch (tree_kind(unit)) {
351✔
1238
   case T_PACK_INST:
1239
      return true;
1240
   case T_PACK_BODY:
×
1241
      {
1242
         tree_t pack = tree_primary(unit);
×
1243
         return package_needs_body(pack) && !is_uninstantiated_package(pack);
×
1244
      }
1245
   case T_PACKAGE:
20✔
1246
      return !package_needs_body(unit) && !is_uninstantiated_package(unit);
40✔
1247
   default:
×
1248
      return false;
×
1249
   }
1250
}
1251

1252
bool package_needs_body(tree_t pack)
19,819✔
1253
{
1254
   assert(tree_kind(pack) == T_PACKAGE);
19,819✔
1255

1256
   const int ndecls = tree_decls(pack);
19,819✔
1257
   for (int i = 0; i < ndecls; i++) {
1,315,897✔
1258
      tree_t d = tree_decl(pack, i);
1,314,076✔
1259
      const tree_kind_t dkind = tree_kind(d);
1,314,076✔
1260
      if ((dkind == T_FUNC_DECL || dkind == T_PROC_DECL)
1,314,076✔
1261
          && !(tree_flags(d) & TREE_F_PREDEFINED))
1,179,676✔
1262
         return true;
1263
      else if (dkind == T_CONST_DECL && !tree_has_value(d))
1,296,885✔
1264
         return true;
1265
      else if (dkind == T_PROT_DECL)
1,296,248✔
1266
         return true;
1267
   }
1268

1269
   return false;
1270
}
1271

1272
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
20,611✔
1273
                          well_known_t unit_name)
1274
{
1275
   const vhdl_standard_t curr = standard();
20,611✔
1276

1277
   if (cache[curr] == NULL) {
20,611✔
1278
      if (hint != NULL)
6,395✔
1279
         cache[curr] = hint;
1,941✔
1280
      else {
1281
         lib_t std = lib_require(well_known(lib_name));
4,454✔
1282
         cache[curr] = lib_get(std, well_known(unit_name));
4,454✔
1283
         assert(cache[curr] != NULL);
4,454✔
1284
      }
1285
   }
1286

1287
   assert(hint == NULL || hint == cache[curr]);
20,611✔
1288
   return cache[curr];
20,611✔
1289
}
1290

1291
static tree_t cached_std(tree_t hint)
18,882✔
1292
{
1293
   static tree_t standard_cache[STD_19 + 1] = {};
18,882✔
1294
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
18,882✔
1295
}
1296

1297
static tree_t search_type_decls(tree_t container, ident_t name)
19,907✔
1298
{
1299
   const int ndecls = tree_decls(container);
19,907✔
1300

1301
   for (int i = 0; i < ndecls; i++) {
1,042,017✔
1302
      tree_t d = tree_decl(container, i);
1,042,017✔
1303
      if (is_type_decl(d) && tree_ident(d) == name)
1,042,017✔
1304
         return d;
19,907✔
1305
   }
1306

1307
   return NULL;
1308
}
1309

1310
type_t std_type(tree_t std, std_type_t which)
1,236,096✔
1311
{
1312
   static type_t cache[STD_RANGE_DIRECTION + 1] = {};
1,236,096✔
1313
   assert(which < ARRAY_LEN(cache));
1,236,096✔
1314

1315
   if (cache[which] == NULL) {
1,236,096✔
1316
      const char *names[] = {
18,882✔
1317
         "universal_integer",
1318
         "universal_real",
1319
         "INTEGER",
1320
         "REAL",
1321
         "BOOLEAN",
1322
         "STRING",
1323
         "TIME",
1324
         "BIT",
1325
         "FILE_OPEN_KIND",
1326
         "FILE_OPEN_STATUS",
1327
         "NATURAL",
1328
         "BIT_VECTOR",
1329
         "SEVERITY_LEVEL",
1330
         "FILE_ORIGIN_KIND",
1331
         "FILE_OPEN_STATE",
1332
         "RANGE_DIRECTION",
1333
      };
1334

1335
      tree_t d = search_type_decls(cached_std(std), ident_new(names[which]));
18,882✔
1336
      if (d == NULL)
18,882✔
1337
         fatal_trace("cannot find standard type %s", names[which]);
1338

1339
      // Do not cache standard types while bootstrapping as the GC will
1340
      // move the objects after parsing
1341
      static int can_cache = -1;
18,882✔
1342
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
18,882✔
1343

1344
      if (can_cache)
18,882✔
1345
         return (cache[which] = tree_type(d));
18,732✔
1346
      else
1347
         return tree_type(d);
150✔
1348
   }
1349
   else
1350
      return cache[which];
1351
}
1352

1353
type_t ieee_type(ieee_type_t which)
2,746✔
1354
{
1355
   static type_t cache[IEEE_STD_LOGIC_VECTOR + 1] = {};
2,746✔
1356
   assert(which < ARRAY_LEN(cache));
2,746✔
1357

1358
   if (cache[which] == NULL) {
2,746✔
1359
      static const char *const names[] = {
433✔
1360
         [IEEE_STD_ULOGIC] = "STD_ULOGIC",
1361
         [IEEE_STD_LOGIC] = "STD_LOGIC",
1362
         [IEEE_STD_ULOGIC_VECTOR] = "STD_ULOGIC_VECTOR",
1363
         [IEEE_STD_LOGIC_VECTOR] = "STD_LOGIC_VECTOR",
1364
      };
1365

1366
      static tree_t ieee_cache[STD_19 + 1] = {};
433✔
1367
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
433✔
1368

1369
      tree_t d = search_type_decls(unit, ident_new(names[which]));
433✔
1370
      if (d == NULL)
433✔
1371
         fatal_trace("cannot find IEEE type %s", names[which]);
1372

1373
      // STD.STANDARD cannot depend on IEEE
1374
      assert(!opt_get_int(OPT_BOOTSTRAP));
433✔
1375

1376
      return (cache[which] = tree_type(d));
433✔
1377
   }
1378
   else
1379
      return cache[which];
1380
}
1381

1382
static tree_t cached_verilog(void)
1,241✔
1383
{
1384
   static tree_t verilog_cache[STD_19 + 1] = {};
1,241✔
1385
   return cached_unit(NULL, verilog_cache, W_NVC, W_NVC_VERILOG);
1,241✔
1386
}
1387

1388
type_t verilog_type(verilog_type_t which)
1,696✔
1389
{
1390
   static type_t cache[VERILOG_WIRE_ARRAY + 1] = {};
1,696✔
1391
   assert(which < ARRAY_LEN(cache));
1,696✔
1392

1393
   if (cache[which] == NULL) {
1,696✔
1394
      static const char *const names[] = {
537✔
1395
         [VERILOG_LOGIC] = "T_LOGIC",
1396
         [VERILOG_LOGIC_ARRAY] = "T_LOGIC_ARRAY",
1397
         [VERILOG_INT64] = "T_INT64",
1398
         [VERILOG_NET_VALUE] = "T_NET_VALUE",
1399
         [VERILOG_NET_ARRAY] = "T_NET_ARRAY",
1400
         [VERILOG_WIRE] = "T_WIRE",
1401
         [VERILOG_WIRE_ARRAY] = "T_WIRE_ARRAY",
1402
      };
1403

1404
      tree_t d = search_type_decls(cached_verilog(), ident_new(names[which]));
537✔
1405
      if (d == NULL)
537✔
1406
         fatal_trace("cannot find NVC.VERILOG type %s", names[which]);
1407

1408
      // STD.STANDARD cannot depend on NVC.VERILOG
1409
      assert(!opt_get_int(OPT_BOOTSTRAP));
537✔
1410

1411
      return (cache[which] = tree_type(d));
537✔
1412
   }
1413
   else
1414
      return cache[which];
1415
}
1416

1417
type_t reflection_type(reflect_type_t which)
281✔
1418
{
1419
   static type_t cache[REFLECT_SUBTYPE_MIRROR + 1] = {};
281✔
1420
   assert(which < ARRAY_LEN(cache));
281✔
1421

1422
   if (cache[which] == NULL) {
281✔
1423
      static const char *const names[] = {
55✔
1424
         [REFLECT_VALUE_MIRROR] = "VALUE_MIRROR",
1425
         [REFLECT_SUBTYPE_MIRROR] = "SUBTYPE_MIRROR",
1426
      };
1427

1428
      static tree_t reflect_cache[STD_19 + 1] = {};
55✔
1429
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
55✔
1430

1431
      tree_t d = search_type_decls(unit, ident_new(names[which]));
55✔
1432
      if (d == NULL)
55✔
1433
         fatal_trace("cannot find REFLECTION type %s", names[which]);
1434

1435
      // STD.STANDARD cannot depend on REFLECTION
1436
      assert(!opt_get_int(OPT_BOOTSTRAP));
55✔
1437

1438
      return (cache[which] = tree_type(d));
55✔
1439
   }
1440
   else
1441
      return cache[which];
1442
}
1443

1444
bool is_open_coded_builtin(subprogram_kind_t kind)
2,410,507✔
1445
{
1446
   switch (kind) {
2,410,507✔
1447
   case S_ADD:
1448
   case S_SUB:
1449
   case S_DIV:
1450
   case S_MUL:
1451
   case S_MUL_PR:
1452
   case S_MUL_RP:
1453
   case S_MUL_PI:
1454
   case S_MUL_IP:
1455
   case S_DIV_PR:
1456
   case S_DIV_PP:
1457
   case S_DIV_PI:
1458
   case S_IDENTITY:
1459
   case S_NEGATE:
1460
   case S_SCALAR_LT:
1461
   case S_SCALAR_LE:
1462
   case S_SCALAR_GT:
1463
   case S_SCALAR_GE:
1464
   case S_SCALAR_EQ:
1465
   case S_SCALAR_NEQ:
1466
   case S_ABS:
1467
   case S_MOD:
1468
   case S_REM:
1469
   case S_EXP:
1470
   case S_MUL_RI:
1471
   case S_MUL_IR:
1472
   case S_DIV_RI:
1473
   case S_CONCAT:
1474
   case S_SCALAR_AND:
1475
   case S_SCALAR_OR:
1476
   case S_SCALAR_NOT:
1477
   case S_SCALAR_NAND:
1478
   case S_SCALAR_NOR:
1479
   case S_SCALAR_XOR:
1480
   case S_SCALAR_XNOR:
1481
   case S_FILE_OPEN1:
1482
   case S_FILE_OPEN2:
1483
   case S_FILE_READ:
1484
   case S_FILE_WRITE:
1485
   case S_DEALLOCATE:
1486
   case S_IEEE_AND:
1487
   case S_IEEE_OR:
1488
   case S_IEEE_XOR:
1489
   case S_IEEE_NAND:
1490
   case S_IEEE_NOR:
1491
   case S_IEEE_XNOR:
1492
   case S_IEEE_NOT:
1493
      return true;
1494
   default:
971,828✔
1495
      return false;
971,828✔
1496
   }
1497
}
1498

1499
tree_t std_func(ident_t mangled)
×
1500
{
1501
   tree_t std = cached_std(NULL);
×
1502

1503
   const int ndecls = tree_decls(std);
×
1504
   for (int i = 0; i < ndecls; i++) {
×
1505
      tree_t d = tree_decl(std, i);
×
1506
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
×
1507
         return d;
×
1508
   }
1509

1510
   return NULL;
1511
}
1512

1513
tree_t verilog_func(ident_t mangled)
704✔
1514
{
1515
   tree_t pack = cached_verilog();
704✔
1516

1517
   const int ndecls = tree_decls(pack);
704✔
1518
   for (int i = 0; i < ndecls; i++) {
75,542✔
1519
      tree_t d = tree_decl(pack, i);
75,542✔
1520
      if (is_subprogram(d) && tree_ident2(d) == mangled)
75,542✔
1521
         return d;
704✔
1522
   }
1523

1524
   fatal_trace("missing Verilog helper function %s", istr(mangled));
1525
}
1526

1527
tree_t name_to_ref(tree_t name)
107,340✔
1528
{
1529
   tree_kind_t kind;
107,340✔
1530
   while ((kind = tree_kind(name)) != T_REF) {
120,790✔
1531
      switch (kind) {
14,977✔
1532
      case T_ARRAY_REF:
13,450✔
1533
      case T_ARRAY_SLICE:
1534
      case T_RECORD_REF:
1535
      case T_ALL:
1536
         name = tree_value(name);
13,450✔
1537
         break;
13,450✔
1538
      default:
1539
         return NULL;
1540
      }
1541
   }
1542

1543
   return name;
1544
}
1545

1546
const char *port_mode_str(port_mode_t mode)
47✔
1547
{
1548
   const char *mode_str[] = {
47✔
1549
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE", "VIEW", "VIEW"
1550
   };
1551
   assert(mode < ARRAY_LEN(mode_str));
47✔
1552
   return mode_str[mode];
47✔
1553
}
1554

1555
void mangle_one_type(text_buf_t *buf, type_t type)
221,645✔
1556
{
1557
   ident_t ident = type_ident(type);
221,645✔
1558

1559
   char code = 0;
221,645✔
1560
   switch (is_well_known(ident)) {
221,645✔
1561
   case W_STD_INTEGER:        code = 'I'; break;
1562
   case W_STD_STRING:         code = 'S'; break;
4,589✔
1563
   case W_STD_REAL:           code = 'R'; break;
4,105✔
1564
   case W_STD_BOOL:           code = 'B'; break;
30,187✔
1565
   case W_STD_CHAR:           code = 'C'; break;
732✔
1566
   case W_STD_TIME:           code = 'T'; break;
1,218✔
1567
   case W_STD_NATURAL:        code = 'N'; break;
4,865✔
1568
   case W_STD_POSITIVE:       code = 'P'; break;
475✔
1569
   case W_STD_BIT:            code = 'J'; break;
3,563✔
1570
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
3,662✔
1571
   case W_IEEE_LOGIC:         code = 'L'; break;
421✔
1572
   case W_IEEE_ULOGIC:        code = 'U'; break;
5,518✔
1573
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
2,383✔
1574
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,497✔
1575
   default: break;
1576
   }
1577

1578
   if (code)
63,215✔
1579
      tb_append(buf, code);
79,204✔
1580
   else {
1581
      tb_printf(buf, "%zu", ident_len(ident));
142,441✔
1582
      tb_istr(buf, ident);
142,441✔
1583
   }
1584
}
221,645✔
1585

1586
ident_t get_call_context(ident_t mangled)
4,591✔
1587
{
1588
   const char *str = istr(mangled), *p = str, *end = NULL;
4,591✔
1589
   for (; *p; p++) {
169,302✔
1590
      if (*p == '(') break;
161,449✔
1591
      if (*p == '.') end = p;
160,120✔
1592
   }
1593
   assert(end != NULL);
4,591✔
1594

1595
   return ident_new_n(str, end - str);
4,591✔
1596
}
1597

1598
tree_t primary_unit_of(tree_t unit)
39,122✔
1599
{
1600
   switch (tree_kind(unit)) {
39,122✔
1601
   case T_ENTITY:
1602
   case T_COMPONENT:
1603
   case T_PACKAGE:
1604
   case T_BLOCK:
1605
   case T_ELAB:
1606
   case T_PACK_INST:
1607
      return unit;
1608
   case T_ARCH:
26,188✔
1609
   case T_CONFIGURATION:
1610
   case T_PACK_BODY:
1611
      return tree_primary(unit);
26,188✔
1612
   default:
×
1613
      fatal_trace("invalid kind %s in primary_unit_of",
1614
                  tree_kind_str(tree_kind(unit)));
1615
   }
1616
}
1617

1618
unsigned get_case_choice_char(tree_t value, int depth)
15,249✔
1619
{
1620
   switch (tree_kind(value)) {
16,069✔
1621
   case T_STRING:
15,227✔
1622
      if (depth < tree_chars(value))
15,227✔
1623
         return assume_int(tree_char(value, depth));
14,666✔
1624
      else
1625
         return ~0;   // Out of bounds
1626

1627
   case T_AGGREGATE:
22✔
1628
      {
1629
         const int nassocs = tree_assocs(value);
22✔
1630
         type_t type = tree_type(value);
22✔
1631

1632
         for (int i = 0, pos = 0; i < nassocs; i++) {
36✔
1633
            tree_t a = tree_assoc(value, i);
35✔
1634
            switch (tree_subkind(a)) {
35✔
1635
            case A_NAMED:
×
1636
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
1637
                  return assume_int(tree_value(a));
×
1638
               break;
1639

1640
            case A_POS:
11✔
1641
               if (pos++ == (unsigned)depth)
11✔
1642
                  return assume_int(tree_value(a));
5✔
1643
               break;
1644

1645
            case A_CONCAT:
24✔
1646
               {
1647
                  tree_t left = tree_value(a);
24✔
1648

1649
                  type_t left_type = tree_type(left);
24✔
1650
                  if (type_is_unconstrained(left_type))
24✔
1651
                     fatal_at(tree_loc(left), "sorry, this expression is not "
×
1652
                              "currently supported in a case choice");
1653

1654
                  tree_t lr = range_of(tree_type(left), 0);
24✔
1655
                  int64_t left_len;
24✔
1656
                  if (!folded_length(lr, &left_len))
24✔
1657
                     fatal_at(tree_loc(left), "cannot determine length of "
×
1658
                              "aggregate element");
1659

1660
                  if (depth < pos + left_len)
24✔
1661
                     return get_case_choice_char(left, depth - pos);
16✔
1662

1663
                  pos += left_len;
8✔
1664
               }
1665
               break;
8✔
1666

1667
            case A_OTHERS:
×
1668
               return assume_int(tree_value(a));
×
1669

1670
            case A_SLICE:
×
1671
               {
1672
                  tree_t base = tree_value(a);
×
1673
                  tree_t r = tree_range(a, 0);
×
1674

1675
                  const int64_t rleft = assume_int(tree_left(r));
×
1676
                  const int64_t rright = assume_int(tree_right(r));
×
1677

1678
                  const int64_t loffset = rebase_index(type, 0, rleft);
×
1679
                  const int64_t roffset = rebase_index(type, 0, rright);
×
1680

1681
                  if (depth >= loffset && depth <= roffset)
×
1682
                     return get_case_choice_char(base, depth - loffset);
×
1683
               }
1684
            }
1685
         }
1686

1687
         // This will produce an error during bounds checking
1688
         return ~0;
1689
      }
1690

1691
   case T_REF:
500✔
1692
      {
1693
         tree_t decl = tree_ref(value);
500✔
1694
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
500✔
1695
         assert(tree_has_value(decl));
500✔
1696
         return get_case_choice_char(tree_value(decl), depth);
500✔
1697
      }
1698

1699
   case T_ARRAY_SLICE:
320✔
1700
      {
1701
         tree_t base = tree_value(value);
320✔
1702
         tree_t r = tree_range(value, 0);
320✔
1703
         const int64_t rleft = assume_int(tree_left(r));
320✔
1704
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
320✔
1705
         return get_case_choice_char(base, depth + offset);
320✔
1706
      }
1707

1708
   default:
×
1709
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1710
               tree_kind_str(tree_kind(value)));
1711
   }
1712
}
1713

1714
int64_t encode_case_choice(tree_t value, int length, int bits)
2,009✔
1715
{
1716
   uint64_t enc = 0;
2,009✔
1717
   for (int i = 0; i < length; i++) {
17,220✔
1718
      if (bits > 0) {
15,211✔
1719
         enc <<= bits;
5,412✔
1720
         enc |= get_case_choice_char(value, i);
5,412✔
1721
      }
1722
      else {
1723
         enc *= 0x27d4eb2d;
9,799✔
1724
         enc += get_case_choice_char(value, i);
9,799✔
1725
      }
1726
   }
1727

1728
   return enc;
2,009✔
1729
}
1730

1731
void to_string(text_buf_t *tb, type_t type, int64_t value)
723✔
1732
{
1733
   if (type_is_integer(type))
723✔
1734
      tb_printf(tb, "%"PRIi64, value);
556✔
1735
   else if (type_is_enum(type)) {
167✔
1736
      type_t base = type_base_recur(type);
70✔
1737
      if (value < 0 || value >= type_enum_literals(base))
70✔
1738
         tb_printf(tb, "%"PRIi64, value);
4✔
1739
      else
1740
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
66✔
1741
   }
1742
   else if (type_is_physical(type)) {
97✔
1743
      type_t base = type_base_recur(type);
27✔
1744
      const unsigned nunits = type_units(base);
27✔
1745
      tree_t max_unit = NULL;
27✔
1746
      int64_t max_unit_value = 0;
27✔
1747

1748
      // Find the largest unit that evenly divides the given value
1749
      for (unsigned u = 0; u < nunits; u++) {
243✔
1750
         tree_t unit = type_unit(base, u);
216✔
1751
         const int64_t unit_value = assume_int(tree_value(unit));
216✔
1752
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
216✔
1753
            max_unit = unit;
100✔
1754
            max_unit_value = unit_value;
100✔
1755
         }
1756
      }
1757
      assert(max_unit);
27✔
1758

1759
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
27✔
1760
                istr(tree_ident(max_unit)));
1761
   }
1762
   else if (type_is_real(type)) {
70✔
1763
      union { int64_t i; double r; } u = { .i = value };
62✔
1764
      tb_printf(tb, "%.17g", u.r);
62✔
1765
   }
1766
   else if (type_is_access(type)) {
8✔
1767
      if (value == 0)
8✔
1768
         tb_cat(tb, "NULL");
4✔
1769
      else
1770
         tb_printf(tb, "%p", (void *)value);
4✔
1771
   }
1772
}
723✔
1773

1774
static bool is_static(tree_t expr)
3,560✔
1775
{
1776
   switch (tree_kind(expr)) {
3,623✔
1777
   case T_REF:
902✔
1778
      {
1779
         tree_t decl = tree_ref(expr);
902✔
1780
         switch (tree_kind(decl)) {
902✔
1781
         case T_CONST_DECL:
186✔
1782
            return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
186✔
1783
         case T_UNIT_DECL:
1784
         case T_ENUM_LIT:
1785
         case T_GENERIC_DECL:
1786
            return true;
1787
         case T_ALIAS:
×
1788
            return is_static(tree_value(decl));
×
1789
         default:
257✔
1790
            return false;
257✔
1791
         }
1792
      }
1793

1794
   case T_LITERAL:
1795
   case T_STRING:
1796
      return true;
1797

1798
   case T_FCALL:
228✔
1799
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
228✔
1800
                                    | TREE_F_GLOBALLY_STATIC));
1801

1802
   case T_RECORD_REF:
59✔
1803
      return is_static(tree_value(expr));
59✔
1804

1805
   case T_ARRAY_REF:
64✔
1806
      {
1807
         if (!is_static(tree_value(expr)))
64✔
1808
            return false;
1809

1810
         const int nparams = tree_params(expr);
64✔
1811
         for (int i = 0; i < nparams; i++) {
128✔
1812
            if (!is_static(tree_value(tree_param(expr, i))))
64✔
1813
               return false;
1814
         }
1815

1816
         return true;
1817
      }
1818

1819
   case T_ARRAY_SLICE:
32✔
1820
      {
1821
         if (!is_static(tree_value(expr)))
32✔
1822
            return false;
1823

1824
         assert(tree_ranges(expr) == 1);
32✔
1825

1826
         tree_t r = tree_range(expr, 0);
32✔
1827
         if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
32✔
1828
            return false;
×
1829

1830
         return true;
1831
      }
1832

1833
   case T_ATTR_REF:
43✔
1834
      {
1835
         switch (tree_subkind(expr)) {
43✔
1836
         case ATTR_EVENT:
1837
         case ATTR_ACTIVE:
1838
         case ATTR_LAST_EVENT:
1839
         case ATTR_LAST_ACTIVE:
1840
         case ATTR_LAST_VALUE:
1841
         case ATTR_DRIVING:
1842
         case ATTR_DRIVING_VALUE:
1843
         case ATTR_STABLE:
1844
         case ATTR_QUIET:
1845
            return false;
1846
         case ATTR_POS:
4✔
1847
         case ATTR_VAL:
1848
         case ATTR_LEFTOF:
1849
         case ATTR_RIGHTOF:
1850
         case ATTR_SUCC:
1851
         case ATTR_PRED:
1852
         case ATTR_VALUE:
1853
         case ATTR_IMAGE:
1854
            assert(tree_params(expr) == 1);
4✔
1855
            return is_static(tree_value(tree_param(expr, 0)));
4✔
1856
         case ATTR_LENGTH:
39✔
1857
         case ATTR_LEFT:
1858
         case ATTR_RIGHT:
1859
         case ATTR_LOW:
1860
         case ATTR_HIGH:
1861
         case ATTR_RANGE:
1862
         case ATTR_REVERSE_RANGE:
1863
            {
1864
               tree_t ref = name_to_ref(tree_name(expr));
39✔
1865
               if (ref == NULL)
39✔
1866
                  return false;
1867

1868
               switch (tree_kind(tree_ref(ref))) {
39✔
1869
               case T_GENERIC_DECL:
1870
               case T_PORT_DECL:
1871
               case T_SIGNAL_DECL:
1872
               case T_SUBTYPE_DECL:
1873
               case T_TYPE_DECL:
1874
                  return true;
1875
               default:
9✔
1876
                  return false;
9✔
1877
               }
1878
            }
1879
         default:
×
1880
            return true;
×
1881
         }
1882
      }
1883

1884
   default:
×
1885
      return false;
×
1886
   }
1887
}
1888

1889
tree_t longest_static_prefix(tree_t expr)
16,614✔
1890
{
1891
   switch (tree_kind(expr)) {
16,614✔
1892
   case T_ARRAY_REF:
2,429✔
1893
      {
1894
         tree_t value = tree_value(expr);
2,429✔
1895
         tree_t prefix = longest_static_prefix(value);
2,429✔
1896

1897
         if (prefix != value)
2,429✔
1898
            return prefix;
1899

1900
         const int nparams = tree_params(expr);
2,392✔
1901
         for (int i = 0; i < nparams; i++) {
4,734✔
1902
            if (!is_static(tree_value(tree_param(expr, i))))
2,668✔
1903
               return prefix;
1904
         }
1905

1906
         return expr;
1907
      }
1908

1909
   case T_ARRAY_SLICE:
361✔
1910
      {
1911
         tree_t value = tree_value(expr);
361✔
1912
         tree_t prefix = longest_static_prefix(value);
361✔
1913

1914
         if (prefix != value)
361✔
1915
            return prefix;
1916

1917
         assert(tree_ranges(expr) == 1);
352✔
1918

1919
         tree_t r = tree_range(expr, 0);
352✔
1920
         if (tree_subkind(r) == RANGE_EXPR) {
352✔
1921
            if (!is_static(tree_value(r)))
19✔
1922
               return prefix;
6✔
1923
         }
1924
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
333✔
1925
            return prefix;
20✔
1926

1927
         return expr;
1928
      }
1929

1930
   case T_RECORD_REF:
1,069✔
1931
      {
1932
         tree_t value = tree_value(expr);
1,069✔
1933
         tree_t prefix = longest_static_prefix(value);
1,069✔
1934

1935
         if (prefix != value)
1,069✔
1936
            return prefix;
28✔
1937

1938
         return expr;
1939
      }
1940

1941
   default:
1942
      return expr;
1943
   }
1944
}
1945

1946
tree_t body_of(tree_t pack)
18,377✔
1947
{
1948
   const tree_kind_t kind = tree_kind(pack);
18,377✔
1949
   if (kind == T_PACK_INST)
18,377✔
1950
      return NULL;
1951

1952
   assert(tree_kind(pack) == T_PACKAGE);
18,377✔
1953

1954
   ident_t body_i = well_known(W_BODY);
18,377✔
1955
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
18,377✔
1956
   return lib_get_qualified(body_name);
18,377✔
1957
}
1958

1959
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
976✔
1960
{
1961
   const int ngenmaps = tree_genmaps(unit);
976✔
1962

1963
   if (pos < ngenmaps) {
976✔
1964
      tree_t m = tree_genmap(unit, pos);
778✔
1965
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
778✔
1966
         return tree_value(m);
579✔
1967
   }
1968

1969
   for (int j = 0; j < ngenmaps; j++) {
1,365✔
1970
      tree_t m = tree_genmap(unit, j);
1,167✔
1971
      switch (tree_subkind(m)) {
1,167✔
1972
      case P_NAMED:
605✔
1973
         {
1974
            tree_t name = tree_name(m);
605✔
1975
            assert(tree_kind(name) == T_REF);
605✔
1976

1977
            if (tree_has_ref(name) && tree_ref(name) == g)
605✔
1978
               return tree_value(m);
23✔
1979
         }
1980
         break;
1981

1982
      case P_POS:
562✔
1983
         if (tree_pos(m) == pos)
562✔
1984
            return tree_value(m);
176✔
1985
         break;
1986

1987
      default:
1988
         break;
1989
      }
1990
   }
1991

1992
   return NULL;
1993
}
1994

1995
bool relaxed_rules(void)
713,340✔
1996
{
1997
   return opt_get_int(OPT_RELAXED);
713,340✔
1998
}
1999

2000
bool is_type_attribute(attr_kind_t kind)
57,381✔
2001
{
2002
   switch (kind) {
57,381✔
2003
   case ATTR_SUBTYPE:
2004
   case ATTR_BASE:
2005
   case ATTR_ELEMENT:
2006
   case ATTR_DESIGNATED_SUBTYPE:
2007
   case ATTR_INDEX:
2008
   case ATTR_RECORD:
2009
      return true;
2010
   default:
56,445✔
2011
      return false;
56,445✔
2012
   }
2013
}
2014

2015
bool attribute_has_param(attr_kind_t attr)
28,538✔
2016
{
2017
   switch (attr) {
28,538✔
2018
   case ATTR_IMAGE:
2019
   case ATTR_SUCC:
2020
   case ATTR_PRED:
2021
   case ATTR_DELAYED:
2022
   case ATTR_LEFTOF:
2023
   case ATTR_RIGHTOF:
2024
   case ATTR_VALUE:
2025
   case ATTR_POS:
2026
   case ATTR_LOW:
2027
   case ATTR_HIGH:
2028
   case ATTR_LEFT:
2029
   case ATTR_RIGHT:
2030
   case ATTR_LENGTH:
2031
   case ATTR_RANGE:
2032
   case ATTR_REVERSE_RANGE:
2033
   case ATTR_VAL:
2034
   case ATTR_QUIET:
2035
   case ATTR_STABLE:
2036
   case ATTR_INDEX:
2037
   case ATTR_ASCENDING:
2038
      return true;
2039
   default:
3,113✔
2040
      return false;
3,113✔
2041
   }
2042
}
2043

2044
type_t get_type_or_null(tree_t t)
4,728,293✔
2045
{
2046
   switch (tree_kind(t)) {
4,728,293✔
2047
   case T_LIBRARY:
2048
   case T_ATTR_SPEC:
2049
   case T_PACKAGE:
2050
   case T_PACK_INST:
2051
   case T_PACK_BODY:
2052
   case T_ENTITY:
2053
   case T_ARCH:
2054
   case T_PROCESS:
2055
   case T_COMPONENT:
2056
   case T_INSTANCE:
2057
   case T_CONCURRENT:
2058
   case T_BLOCK:
2059
   case T_WHILE:
2060
   case T_FOR:
2061
   case T_LOOP:
2062
   case T_GROUP_TEMPLATE:
2063
   case T_CONFIGURATION:
2064
   case T_GROUP:
2065
   case T_FOR_GENERATE:
2066
   case T_IF_GENERATE:
2067
   case T_CASE_GENERATE:
2068
   case T_USE:
2069
   case T_CONTEXT:
2070
   case T_PSL_DECL:
2071
   case T_PSL_DIRECT:
2072
   case T_WAVEFORM:
2073
      return NULL;
2074
   default:
4,603,116✔
2075
      if (tree_has_type(t))
4,603,116✔
2076
         return tree_type(t);
4,594,415✔
2077
      else
2078
         return NULL;
2079
   }
2080
}
2081

2082
type_t subtype_for_string(tree_t str, type_t base)
33,559✔
2083
{
2084
   if (type_const_bounds(base))
33,559✔
2085
      return base;    // Can be checked statically
2086
   else if (!type_is_unconstrained(base))
29,269✔
2087
      base = type_base_recur(base);
371✔
2088

2089
   // Construct a new constrained array subtype: the direction and
2090
   // bounds are the same as those for a positional array aggregate
2091
   type_t sub = type_new(T_SUBTYPE);
29,269✔
2092
   type_set_base(sub, base);
29,269✔
2093

2094
   type_t index_type = index_type_of(base, 0);
29,269✔
2095
   const bool is_enum = type_is_enum(index_type);
29,269✔
2096

2097
   // The direction is determined by the index type
2098
   range_kind_t dir = direction_of(index_type, 0);
29,269✔
2099
   tree_t index_r = range_of(index_type, 0);
29,269✔
2100

2101
   // The left bound is the left of the index type and the right bound
2102
   // is determined by the number of elements
2103

2104
   tree_t left = NULL, right = NULL;
29,269✔
2105
   const int nchars = tree_chars(str);
29,269✔
2106

2107
   if (is_enum) {
29,269✔
2108
      const int nlits = type_enum_literals(type_base_recur(index_type));
16✔
2109
      int64_t index_left = assume_int(tree_left(index_r));
16✔
2110

2111
      int64_t iright, ileft;
16✔
2112
      if (nchars == 0) {
16✔
2113
         iright = index_left;
1✔
2114
         ileft = assume_int(tree_right(index_r));
1✔
2115
      }
2116
      else if (dir == RANGE_DOWNTO) {
15✔
2117
         ileft = index_left;
×
2118
         iright = MIN(nlits - 1, MAX(0, index_left - nchars + 1));
×
2119
      }
2120
      else {
2121
         ileft = index_left;
15✔
2122
         iright = MIN(nlits - 1, MAX(0, index_left + nchars - 1));
15✔
2123
      }
2124

2125
      left = get_enum_lit(str, index_type, ileft);
16✔
2126
      right = get_enum_lit(str, index_type, iright);
16✔
2127
   }
2128
   else {
2129
      left = tree_left(index_r);
29,253✔
2130

2131
      int64_t iright;
29,253✔
2132
      if (dir == RANGE_DOWNTO)
29,253✔
2133
         iright = assume_int(left) - nchars + 1;
×
2134
      else
2135
         iright = assume_int(left) + nchars - 1;
29,253✔
2136

2137
      right = get_int_lit(str, index_type, iright);
29,253✔
2138
   }
2139

2140
   tree_t r = tree_new(T_RANGE);
29,269✔
2141
   tree_set_subkind(r, dir);
29,269✔
2142
   tree_set_left(r, left);
29,269✔
2143
   tree_set_right(r, right);
29,269✔
2144
   tree_set_loc(r, tree_loc(str));
29,269✔
2145
   tree_set_type(r, index_type);
29,269✔
2146

2147
   tree_t c = tree_new(T_CONSTRAINT);
29,269✔
2148
   tree_set_subkind(c, C_INDEX);
29,269✔
2149
   tree_add_range(c, r);
29,269✔
2150
   tree_set_loc(c, tree_loc(str));
29,269✔
2151

2152
   type_set_constraint(sub, c);
29,269✔
2153

2154
   return sub;
29,269✔
2155
}
2156

2157
tree_t change_ref(tree_t name, tree_t new)
2,875✔
2158
{
2159
   switch (tree_kind(name)) {
2,875✔
2160
   case T_REF:
1,896✔
2161
      {
2162
         tree_t ref = make_ref(new);
1,896✔
2163
         tree_set_loc(ref, tree_loc(name));
1,896✔
2164
         return ref;
1,896✔
2165
      }
2166

2167
   case T_ARRAY_REF:
146✔
2168
      {
2169
         tree_t value = change_ref(tree_value(name), new);
146✔
2170

2171
         tree_t t = tree_new(T_ARRAY_REF);
146✔
2172
         tree_set_loc(t, tree_loc(name));
146✔
2173
         tree_set_value(t, value);
146✔
2174
         tree_set_type(t, type_elem(tree_type(value)));
146✔
2175

2176
         const int nparams = tree_params(name);
146✔
2177
         for (int i = 0; i < nparams; i++)
292✔
2178
            tree_add_param(t, tree_param(name, i));
146✔
2179

2180
         return t;
2181
      }
2182

2183
   case T_ARRAY_SLICE:
69✔
2184
      {
2185
         tree_t value = change_ref(tree_value(name), new);
69✔
2186
         tree_t r = tree_range(name, 0);
69✔
2187

2188
         tree_t constraint = tree_new(T_CONSTRAINT);
69✔
2189
         tree_set_subkind(constraint, C_INDEX);
69✔
2190
         tree_add_range(constraint, r);
69✔
2191

2192
         type_t slice_type = type_new(T_SUBTYPE);
69✔
2193
         type_set_constraint(slice_type, constraint);
69✔
2194
         type_set_base(slice_type, tree_type(value));
69✔
2195

2196
         tree_t t = tree_new(T_ARRAY_SLICE);
69✔
2197
         tree_set_loc(t, tree_loc(name));
69✔
2198
         tree_set_value(t, value);
69✔
2199
         tree_set_type(t, slice_type);
69✔
2200
         tree_add_range(t, r);
69✔
2201

2202
         return t;
69✔
2203
      }
2204

2205
   case T_RECORD_REF:
534✔
2206
      {
2207
         tree_t t = tree_new(T_RECORD_REF);
534✔
2208
         tree_set_loc(t, tree_loc(name));
534✔
2209
         tree_set_value(t, change_ref(tree_value(name), new));
534✔
2210
         tree_set_type(t, tree_type(name));
534✔
2211
         tree_set_ident(t, tree_ident(name));
534✔
2212
         tree_set_ref(t, tree_ref(name));
534✔
2213

2214
         return t;
534✔
2215
      }
2216

2217
   case T_CONV_FUNC:
218✔
2218
      {
2219
         tree_t t = tree_new(T_CONV_FUNC);
218✔
2220
         tree_set_loc(t, tree_loc(name));
218✔
2221
         tree_set_value(t, change_ref(tree_value(name), new));
218✔
2222
         tree_set_ident(t, tree_ident(name));
218✔
2223
         tree_set_type(t, tree_type(name));
218✔
2224
         tree_set_ref(t, tree_ref(name));
218✔
2225

2226
         return t;
218✔
2227
      }
2228

2229
   case T_TYPE_CONV:
12✔
2230
      {
2231
         tree_t t = tree_new(T_TYPE_CONV);
12✔
2232
         tree_set_loc(t, tree_loc(name));
12✔
2233
         tree_set_type(t, tree_type(name));
12✔
2234
         tree_set_value(t, change_ref(tree_value(name), new));
12✔
2235

2236
         return t;
12✔
2237
      }
2238

2239
   default:
×
2240
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
2241
                  tree_kind_str(tree_kind(name)));
2242
   }
2243
}
2244

2245
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
4,125✔
2246
{
2247
   switch (tree_kind(expr)) {
4,125✔
2248
   case T_ARRAY_SLICE:
108✔
2249
      build_wait(tree_range(expr, 0), fn, ctx);
108✔
2250
      break;
108✔
2251

2252
   case T_ARRAY_REF:
780✔
2253
      {
2254
         const int nparams = tree_params(expr);
780✔
2255
         for (int i = 0; i < nparams; i++)
1,560✔
2256
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
780✔
2257
      }
2258
      break;
2259

2260
   default:
2261
      break;
2262
   }
2263
}
4,125✔
2264

2265
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
19,742✔
2266
{
2267
   // LRM 08 section 10.2 has rules for building a wait statement from a
2268
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2269
   // all-sensitised processes.
2270

2271
   switch (tree_kind(expr)) {
24,408✔
2272
   case T_REF:
6,196✔
2273
      if (class_of(tree_ref(expr)) == C_SIGNAL)
6,196✔
2274
         (*fn)(expr, ctx);
3,593✔
2275
      break;
2276

2277
   case T_EXTERNAL_NAME:
10✔
2278
      if (tree_class(expr) == C_SIGNAL)
10✔
2279
         (*fn)(expr, ctx);
10✔
2280
      break;
2281

2282
   case T_WAVEFORM:
3,995✔
2283
   case T_QUALIFIED:
2284
   case T_TYPE_CONV:
2285
   case T_INERTIAL:
2286
      if (tree_has_value(expr))
3,995✔
2287
         build_wait(tree_value(expr), fn, ctx);
3,983✔
2288
      break;
2289

2290
   case T_ASSERT:
630✔
2291
      build_wait(tree_value(expr), fn, ctx);
630✔
2292
      // Fall-through
2293
   case T_REPORT:
635✔
2294
      if (tree_has_message(expr))
635✔
2295
         build_wait(tree_message(expr), fn, ctx);
399✔
2296
      break;
2297

2298
   case T_ARRAY_REF:
1,157✔
2299
   case T_ARRAY_SLICE:
2300
   case T_RECORD_REF:
2301
      {
2302
         tree_t ref = name_to_ref(expr);
1,157✔
2303
         if (ref != NULL && class_of(ref) == C_SIGNAL
1,157✔
2304
             && longest_static_prefix(expr) == expr)
761✔
2305
            (*fn)(expr, ctx);
663✔
2306
         else {
2307
            build_wait(tree_value(expr), fn, ctx);
494✔
2308
            build_wait_for_target(expr, fn, ctx);
494✔
2309
         }
2310
      }
2311
      break;
2312

2313
   case T_FCALL:
2,949✔
2314
   case T_PCALL:
2315
   case T_PROT_FCALL:
2316
   case T_PROT_PCALL:
2317
      {
2318
         tree_t decl = tree_ref(expr);
2,949✔
2319
         const int nparams = tree_params(expr);
2,949✔
2320
         for (int i = 0; i < nparams; i++) {
8,014✔
2321
            tree_t p = tree_param(expr, i), port;
5,065✔
2322
            switch (tree_subkind(p)) {
5,065✔
2323
            case P_POS:
5,065✔
2324
               port = tree_port(decl, tree_pos(p));
5,065✔
2325
               break;
5,065✔
2326
            case P_NAMED:
×
2327
               port = tree_ref(name_to_ref(tree_name(p)));
×
2328
               break;
×
2329
            default:
×
2330
               should_not_reach_here();
2331
            }
2332
            assert(tree_kind(port) == T_PARAM_DECL);
5,065✔
2333

2334
            switch (tree_subkind(port)) {
5,065✔
2335
            case PORT_IN:
5,056✔
2336
            case PORT_INOUT:
2337
            case PORT_ARRAY_VIEW:
2338
            case PORT_RECORD_VIEW:
2339
               build_wait(tree_value(p), fn, ctx);
5,056✔
2340
               break;
5,056✔
2341
            default:
2342
               break;
2343
            }
2344
         }
2345
      }
2346
      break;
2347

2348
   case T_AGGREGATE:
540✔
2349
      {
2350
         const int nassocs = tree_assocs(expr);
540✔
2351
         for (int i = 0; i < nassocs; i++) {
1,948✔
2352
            tree_t a = tree_assoc(expr, i);
1,408✔
2353
            build_wait(tree_value(a), fn, ctx);
1,408✔
2354

2355
            switch (tree_subkind(a)) {
1,408✔
2356
            case A_RANGE:
70✔
2357
            case A_SLICE:
2358
               build_wait(tree_range(a, 0), fn, ctx);
70✔
2359
               break;
70✔
2360
            case A_NAMED:
124✔
2361
               build_wait(tree_name(a), fn, ctx);
124✔
2362
               break;
124✔
2363
            }
2364
         }
2365
      }
2366
      break;
2367

2368
   case T_ATTR_REF:
574✔
2369
      {
2370
         const attr_kind_t predef = tree_subkind(expr);
574✔
2371
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
574✔
2372
            build_wait(tree_name(expr), fn, ctx);
234✔
2373

2374
         const int nparams = tree_params(expr);
574✔
2375
         for (int i = 0; i < nparams; i++)
598✔
2376
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
24✔
2377
      }
2378
      break;
2379

2380
   case T_LITERAL:
2381
   case T_STRING:
2382
   case T_DUMMY_DRIVER:
2383
      break;
2384

2385
   case T_IF:
330✔
2386
      {
2387
         const int nconds = tree_conds(expr);
330✔
2388
         for (int i = 0; i < nconds; i++)
894✔
2389
            build_wait(tree_cond(expr, i), fn, ctx);
564✔
2390
      }
2391
      break;
2392

2393
   case T_COND_STMT:
568✔
2394
      {
2395
         if (tree_has_value(expr))
568✔
2396
            build_wait(tree_value(expr), fn, ctx);
382✔
2397

2398
         const int nstmts = tree_stmts(expr);
568✔
2399
         for (int i = 0; i < nstmts; i++)
1,154✔
2400
            build_wait(tree_stmt(expr, i), fn, ctx);
586✔
2401
      }
2402
      break;
2403

2404
   case T_COND_VALUE:
4✔
2405
      {
2406
         const int nconds = tree_conds(expr);
4✔
2407
         for (int i = 0; i < nconds; i++)
16✔
2408
            build_wait(tree_cond(expr, i), fn, ctx);
12✔
2409
         break;
2410
      }
2411

2412
   case T_COND_EXPR:
12✔
2413
      {
2414
         if (tree_has_value(expr))
12✔
2415
            build_wait(tree_value(expr), fn, ctx);
8✔
2416

2417
         build_wait(tree_result(expr), fn, ctx);
12✔
2418
         break;
12✔
2419
      }
2420

2421
   case T_PROCESS:
74✔
2422
   case T_SEQUENCE:
2423
   case T_PROC_BODY:
2424
      {
2425
         const int ndecls = tree_decls(expr);
74✔
2426
         for (int i = 0; i < ndecls; i++) {
105✔
2427
            tree_t d = tree_decl(expr, i);
31✔
2428
            if (tree_kind(d) == T_PROC_BODY)
31✔
2429
               build_wait(d, fn, ctx);
2✔
2430
         }
2431

2432
         const int nstmts = tree_stmts(expr);
74✔
2433
         for (int i = 0; i < nstmts; i++)
187✔
2434
            build_wait(tree_stmt(expr, i), fn, ctx);
113✔
2435
      }
2436
      break;
2437

2438
   case T_SIGNAL_ASSIGN:
3,562✔
2439
      {
2440
         build_wait_for_target(tree_target(expr), fn, ctx);
3,562✔
2441

2442
         const int nwaves = tree_waveforms(expr);
3,562✔
2443
         for (int i = 0; i < nwaves; i++)
7,354✔
2444
            build_wait(tree_waveform(expr, i), fn, ctx);
3,792✔
2445
      }
2446
      break;
2447

2448
   case T_VAR_ASSIGN:
65✔
2449
   case T_FORCE:
2450
      build_wait_for_target(tree_target(expr), fn, ctx);
65✔
2451
      build_wait(tree_value(expr), fn, ctx);
65✔
2452
      break;
65✔
2453

2454
   case T_RELEASE:
4✔
2455
      build_wait_for_target(tree_target(expr), fn, ctx);
4✔
2456
      break;
4✔
2457

2458
   case T_CASE:
61✔
2459
   case T_MATCH_CASE:
2460
      {
2461
         build_wait(tree_value(expr), fn, ctx);
61✔
2462

2463
         const int nstmts = tree_stmts(expr);
61✔
2464
         for (int i = 0; i < nstmts; i++) {
374✔
2465
            tree_t alt = tree_stmt(expr, i);
313✔
2466

2467
            const int nstmts = tree_stmts(alt);
313✔
2468
            for (int j = 0; j < nstmts; j++)
626✔
2469
               build_wait(tree_stmt(alt, j), fn, ctx);
313✔
2470
         }
2471
      }
2472
      break;
2473

2474
   case T_FOR:
21✔
2475
      {
2476
         build_wait(tree_range(expr, 0), fn, ctx);
21✔
2477

2478
         const int nstmts = tree_stmts(expr);
21✔
2479
         for (int i = 0; i < nstmts; i++)
46✔
2480
            build_wait(tree_stmt(expr, i), fn, ctx);
25✔
2481
      }
2482
      break;
2483

2484
   case T_WHILE:
1✔
2485
      build_wait(tree_value(expr), fn, ctx);
1✔
2486
      // Fall-through
2487
   case T_LOOP:
5✔
2488
      {
2489
         const int nstmts = tree_stmts(expr);
5✔
2490
         for (int i = 0; i < nstmts; i++)
26✔
2491
            build_wait(tree_stmt(expr, i), fn, ctx);
21✔
2492
      }
2493
      break;
2494

2495
   case T_NEXT:
12✔
2496
   case T_EXIT:
2497
      if (tree_has_value(expr))
12✔
2498
         build_wait(tree_value(expr), fn, ctx);
8✔
2499
      break;
2500

2501
   case T_RANGE:
199✔
2502
      if (tree_subkind(expr) == RANGE_EXPR)
199✔
2503
         build_wait(tree_value(expr), fn, ctx);
21✔
2504
      else {
2505
         build_wait(tree_left(expr), fn, ctx);
178✔
2506
         build_wait(tree_right(expr), fn, ctx);
178✔
2507
      }
2508
      break;
2509

2510
   case T_OPEN:
2511
      break;
2512

2513
   default:
×
2514
      fatal_trace("Cannot handle tree kind %s in wait expression",
2515
                  tree_kind_str(tree_kind(expr)));
2516
   }
2517
}
19,742✔
2518

2519
void print_syntax(const char *fmt, ...)
2,135✔
2520
{
2521
   LOCAL_TEXT_BUF tb = tb_new();
2,135✔
2522
   bool highlighting = false;
2,135✔
2523
   static bool comment = false, last_was_newline = false;
2,135✔
2524
   for (const char *p = fmt; *p != '\0'; p++) {
11,152✔
2525
      if (comment) {
9,017✔
2526
         if (*p == '\n' || *p == '\r') {
3,244✔
2527
            comment = false;
94✔
2528
            last_was_newline = true;
94✔
2529
            tb_cat(tb, "$$\n");
94✔
2530
         }
2531
         else if (*p != '~' && *p != '#') {
3,150✔
2532
            tb_append(tb, *p);
2,990✔
2533
            last_was_newline = false;
2,990✔
2534
         }
2535
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
3,244✔
2536
            tb_cat(tb, "$$");
16✔
2537
            comment = false;
16✔
2538
            last_was_newline = false;
16✔
2539
         }
2540
      }
2541
      else if (*p == '\r') {
5,773✔
2542
         if (!last_was_newline) {
22✔
2543
            tb_append(tb, '\n');
×
2544
            last_was_newline = true;
×
2545
         }
2546
      }
2547
      else if (*p == '#' && *(p + 1) != '#') {
5,751✔
2548
         tb_cat(tb, "$bold$$cyan$");
381✔
2549
         last_was_newline = false;
381✔
2550
         highlighting = true;
381✔
2551
      }
2552
      else if (*p == '~' && *(p + 1) != '~') {
5,370✔
2553
         tb_cat(tb, "$yellow$");
1✔
2554
         last_was_newline = false;
1✔
2555
         highlighting = true;
1✔
2556
      }
2557
      else if ((*p == '-' && *(p + 1) == '-')
5,369✔
2558
               || (*p == '/' && *(p + 1) == '/')
5,279✔
2559
               || (*p == '/' && *(p + 1) == '*')) {
5,275✔
2560
         tb_cat(tb, "$red$");
110✔
2561
         tb_append(tb, *p);
110✔
2562
         last_was_newline = false;
110✔
2563
         comment = true;
110✔
2564
      }
2565
      else if (!isalnum_iso88591(*p) && *p != '_'
5,259✔
2566
               && *p != '%' && highlighting) {
2,709✔
2567
         tb_cat(tb, "$$");
326✔
2568
         tb_append(tb, *p);
326✔
2569
         last_was_newline = false;
326✔
2570
         highlighting = false;
326✔
2571
      }
2572
      else {
2573
         tb_append(tb, *p);
4,933✔
2574
         last_was_newline = (*p == '\n');
4,933✔
2575
      }
2576
   }
2577

2578
   if (highlighting)
2,135✔
2579
      tb_cat(tb, "$$");
56✔
2580

2581
   va_list ap;
2,135✔
2582
   va_start(ap, fmt);
2,135✔
2583

2584
   if (syntax_buf != NULL) {
2,135✔
2585
      ostream_t os = { tb_ostream_write, syntax_buf, CHARSET_ISO88591 };
2,135✔
2586
      nvc_vfprintf(&os, tb_get(tb), ap);
2,135✔
2587
   }
2588
   else
2589
      nvc_vprintf(tb_get(tb), ap);
×
2590

2591
   va_end(ap);
2,135✔
2592
}
2,135✔
2593

2594
void capture_syntax(text_buf_t *tb)
9✔
2595
{
2596
   assert(tb == NULL || syntax_buf == NULL);
9✔
2597
   syntax_buf = tb;
9✔
2598
}
9✔
2599

2600
bool all_character_literals(type_t type)
34,209✔
2601
{
2602
   assert(type_is_enum(type));
34,209✔
2603

2604
   type_t base = type_base_recur(type);
34,209✔
2605
   const int nlits = type_enum_literals(base);
34,209✔
2606
   for (int i = 0; i < nlits; i++) {
187,845✔
2607
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
164,809✔
2608
         return false;
2609
   }
2610

2611
   return true;
2612
}
2613

2614
bool is_operator_symbol(ident_t ident)
31,962✔
2615
{
2616
   const well_known_t wk = is_well_known(ident);
31,962✔
2617
   if (wk >= NUM_WELL_KNOWN)
31,962✔
2618
      return false;
2619
   else if (standard() < STD_08)
17,259✔
2620
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,305✔
2621
   else
2622
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
13,954✔
2623
}
2624

2625
bool same_tree(tree_t a, tree_t b)
8,472✔
2626
{
2627
   const tree_kind_t akind = tree_kind(a);
8,472✔
2628
   if (akind != tree_kind(b))
8,472✔
2629
      return false;
2630

2631
   switch (akind) {
8,209✔
2632
   case T_REF:
3,717✔
2633
      return tree_has_ref(a) && tree_has_ref(b) && tree_ref(a) == tree_ref(b);
4,873✔
2634
   case T_ARRAY_REF:
1,302✔
2635
      {
2636
         if (!same_tree(tree_value(a), tree_value(b)))
1,302✔
2637
            return false;
2638

2639
         const int nparams = tree_params(a);
1,273✔
2640
         assert(nparams == tree_params(b));
1,273✔
2641

2642
         for (int i = 0; i < nparams; i++) {
2,403✔
2643
            tree_t pa = tree_value(tree_param(a, i));
2,173✔
2644
            tree_t pb = tree_value(tree_param(b, i));
2,173✔
2645
            if (!same_tree(pa, pb))
2,173✔
2646
               return false;
2647
         }
2648

2649
         return true;
2650
      }
2651
   case T_ARRAY_SLICE:
46✔
2652
      {
2653
         if (!same_tree(tree_value(a), tree_value(b)))
46✔
2654
            return false;
2655

2656
         tree_t ra = tree_range(a, 0);
46✔
2657
         tree_t rb = tree_range(b, 0);
46✔
2658

2659
         const range_kind_t rakind = tree_subkind(ra);
46✔
2660
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
46✔
2661
            return false;
2662

2663
         return same_tree(tree_left(ra), tree_left(rb))
46✔
2664
            && same_tree(tree_right(ra), tree_right(rb));
50✔
2665
      }
2666
   case T_RECORD_REF:
346✔
2667
      return ident_casecmp(tree_ident(a), tree_ident(b))
346✔
2668
         && same_tree(tree_value(a), tree_value(b));
354✔
2669
   case T_LITERAL:
2,512✔
2670
      {
2671
         const literal_kind_t lkind = tree_subkind(a);
2,512✔
2672
         if (lkind != tree_subkind(b))
2,512✔
2673
            return false;
2674

2675
         switch (lkind) {
2,512✔
2676
         case L_PHYSICAL:
36✔
2677
            {
2678
               ident_t aid = tree_has_ident(a) ? tree_ident(a) : NULL;
36✔
2679
               ident_t bid = tree_has_ident(b) ? tree_ident(b) : NULL;
36✔
2680
               if (aid == bid)
36✔
2681
                  return tree_ival(a) == tree_ival(b);
36✔
2682
               else
2683
                  return false;
2684
            }
2685
         case L_INT:
2,445✔
2686
            return tree_ival(a) == tree_ival(b);
2,445✔
2687
         case L_REAL:
31✔
2688
            return tree_dval(a) == tree_dval(b);
31✔
2689
         case L_NULL:
2690
            return true;
2691
         default:
×
2692
            return false;
×
2693
         }
2694
      }
2695
   case T_STRING:
254✔
2696
      {
2697
         const int nchars = tree_chars(a);
254✔
2698
         if (tree_chars(b) != nchars)
254✔
2699
            return false;
2700

2701
         for (int i = 0; i < nchars; i++) {
309✔
2702
            if (!same_tree(tree_char(a, i), tree_char(b, i)))
56✔
2703
               return false;
2704
         }
2705

2706
         return true;
2707
      }
2708
   case T_OPEN:
2709
      return true;
2710
   default:
32✔
2711
      return false;
32✔
2712
   }
2713
}
2714

2715
static range_kind_t get_range_direction(tree_t r)
410✔
2716
{
2717
   assert(tree_kind(r) == T_RANGE);
410✔
2718

2719
   const range_kind_t dir = tree_subkind(r);
410✔
2720
   if (dir != RANGE_EXPR)
410✔
2721
      return dir;
2722

2723
   // Handle ranges like X'RANGE where X has known direction
2724

2725
   tree_t aref = tree_value(r);
59✔
2726
   assert(tree_kind(aref) == T_ATTR_REF);
59✔
2727

2728
   const attr_kind_t kind = tree_subkind(aref);
59✔
2729
   assert(kind == ATTR_RANGE || kind == ATTR_REVERSE_RANGE);
59✔
2730

2731
   type_t prefix_type = tree_type(tree_name(aref));
59✔
2732
   if (type_is_none(prefix_type))
59✔
2733
      return RANGE_ERROR;
2734
   if (type_is_unconstrained(prefix_type))
58✔
2735
      return RANGE_EXPR;
2736

2737
   tree_t prefix_r = range_of(prefix_type, 0);
54✔
2738

2739
   const range_kind_t prefix_dir = get_range_direction(prefix_r);
54✔
2740
   if (prefix_dir != RANGE_TO && prefix_dir != RANGE_DOWNTO)
54✔
2741
      return prefix_dir;
2742
   else if (kind == ATTR_REVERSE_RANGE)
54✔
2743
      return prefix_dir == RANGE_TO ? RANGE_DOWNTO : RANGE_TO;
4✔
2744
   else
2745
      return prefix_dir;
2746
}
2747

2748
bool calculate_aggregate_bounds(tree_t expr, range_kind_t *kind,
9,106✔
2749
                                int64_t *left, int64_t *right)
2750
{
2751
   // Calculate the direction and bounds of an array aggregate using the
2752
   // rules in LRM 93 7.3.2.2
2753

2754
   type_t type = tree_type(expr);
9,106✔
2755
   if (type_is_none(type))
9,106✔
2756
      return false;
2757

2758
   type_t index_type = index_type_of(type, 0);
9,106✔
2759
   if (index_type == NULL)
9,106✔
2760
      return false;
2761
   else if (type_is_none(index_type) || type_is_generic(index_type))
9,106✔
2762
      return false;
3✔
2763

2764
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
9,103✔
2765

2766
   int64_t low, high;
9,103✔
2767
   if (!folded_bounds(index_r, &low, &high))
9,103✔
2768
      return false;
2769

2770
   int64_t clow = INT64_MAX, chigh = INT64_MIN;  // Actual bounds computed below
9,083✔
2771

2772
   range_kind_t dir;
9,083✔
2773
   if (type_is_unconstrained(type))
9,083✔
2774
      dir = tree_subkind(index_r);
8,727✔
2775
   else {
2776
      base_r = range_of(type, 0);
356✔
2777
      dir = get_range_direction(base_r);
356✔
2778
   }
2779

2780
   const int nassocs = tree_assocs(expr);
9,083✔
2781

2782
   if (standard() >= STD_08) {
9,083✔
2783
      // VHDL-2008 range association determines index direction for
2784
      // unconstrained aggregate when the expression type matches the
2785
      // array type
2786
      for (int i = 0; i < nassocs; i++) {
19,083✔
2787
         tree_t a = tree_assoc(expr, i);
13,802✔
2788
         if (tree_subkind(a) == A_SLICE)
13,802✔
2789
            dir = tree_subkind(tree_range(a, 0));
71✔
2790
      }
2791
   }
2792

2793
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
9,083✔
2794
      return false;
2795

2796
   int64_t pos = 0;
2797
   for (int i = 0; i < nassocs; i++) {
25,590✔
2798
      tree_t a = tree_assoc(expr, i);
22,608✔
2799
      int64_t ilow = 0, ihigh = 0;
22,608✔
2800
      const assoc_kind_t akind = tree_subkind(a);
22,608✔
2801

2802
      switch (akind) {
22,608✔
2803
      case A_NAMED:
861✔
2804
         {
2805
            tree_t name = tree_name(a);
861✔
2806
            if (folded_int(name, &ilow))
861✔
2807
               ihigh = ilow;
848✔
2808
            else
2809
               return false;
6,084✔
2810
         }
2811
         break;
848✔
2812

2813
      case A_RANGE:
1,481✔
2814
      case A_SLICE:
2815
         {
2816
            tree_t r = tree_range(a, 0);
1,481✔
2817
            const range_kind_t rkind = tree_subkind(r);
1,481✔
2818
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,481✔
2819
               tree_t left = tree_left(r), right = tree_right(r);
1,112✔
2820

2821
               int64_t ileft, iright;
1,112✔
2822
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
1,112✔
2823
                  ilow = (rkind == RANGE_TO ? ileft : iright);
581✔
2824
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
581✔
2825
               }
2826
               else
2827
                  return false;
531✔
2828
            }
2829
            else
2830
               return false;
2831
         }
2832
         break;
2833

2834
      case A_OTHERS:
2835
         return false;
2836

2837
      case A_POS:
12,616✔
2838
         if (i == 0) {
12,616✔
2839
            int64_t ileft;
2,043✔
2840
            if (folded_int(tree_left(base_r), &ileft))
2,043✔
2841
               ilow = ihigh = ileft;
2,043✔
2842
            else
2843
               return false;
×
2844
         }
2845
         else if (dir == RANGE_TO)
10,573✔
2846
            ilow = ihigh = clow + pos;
10,571✔
2847
         else
2848
            ilow = ihigh = chigh - pos;
2✔
2849
         pos++;
12,616✔
2850
         break;
12,616✔
2851

2852
      case A_CONCAT:
7,636✔
2853
         {
2854
            type_t value_type = tree_type(tree_value(a));
7,636✔
2855

2856
            int64_t length;
7,636✔
2857
            if (type_is_unconstrained(value_type))
7,636✔
2858
               return false;
5,157✔
2859
            else if (folded_length(range_of(value_type, 0), &length)) {
2,874✔
2860
               if (i == 0) {
2,479✔
2861
                  int64_t ileft;
2,083✔
2862
                  if (folded_int(tree_left(base_r), &ileft))
2,083✔
2863
                     ilow = ihigh = ileft;
2,083✔
2864
                  else
2865
                     return false;
×
2866
               }
2867
               else if (dir == RANGE_TO) {
396✔
2868
                  ilow = clow + pos;
372✔
2869
                  ihigh = ilow + length - 1;
372✔
2870
               }
2871
               else {
2872
                  ihigh = chigh - pos;
24✔
2873
                  ilow = ihigh - length + 1;
24✔
2874
               }
2875
               pos += length;
2,479✔
2876
            }
2877
            else
2878
               return false;
2879
         }
2880
         break;
2881
      }
2882

2883
      clow = MIN(clow, ilow);
16,524✔
2884
      chigh = MAX(chigh, ihigh);
16,524✔
2885
   }
2886

2887
   if (clow < low || chigh > high)
2,982✔
2888
      return false;   // Will raise a bounds check error later
2889

2890
   *kind = dir;
2,976✔
2891
   *left = dir == RANGE_TO ? clow : chigh;
2,976✔
2892
   *right = dir == RANGE_TO ? chigh : clow;
2,976✔
2893

2894
   return true;
2,976✔
2895
}
2896

2897
type_t calculate_aggregate_subtype(tree_t expr)
7,526✔
2898
{
2899
   range_kind_t dir;
7,526✔
2900
   int64_t ileft, iright;
7,526✔
2901
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
7,526✔
2902
      return NULL;
2903

2904
   type_t type = tree_type(expr);
2,955✔
2905

2906
   const int ndims = dimension_of(type);
2,955✔
2907
   type_t a0_type = NULL;
2,955✔
2908
   if (ndims > 1) {
2,955✔
2909
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
87✔
2910
      if (type_is_unconstrained(a0_type))
87✔
2911
         return NULL;
2912

2913
      assert(dimension_of(a0_type) == ndims - 1);
87✔
2914
   }
2915

2916
   type_t index_type = index_type_of(type, 0);
2,955✔
2917

2918
   tree_t left = get_discrete_lit(expr, index_type, ileft);
2,955✔
2919
   tree_t right = get_discrete_lit(expr, index_type, iright);
2,955✔
2920
   assert(left != NULL && right != NULL);
2,955✔
2921

2922
   type_t sub = type_new(T_SUBTYPE);
2,955✔
2923
   type_set_base(sub, type_base_recur(type));
2,955✔
2924

2925
   type_t elem = type_elem(type);
2,955✔
2926
   if (type_is_unconstrained(elem)) {
2,955✔
2927
      tree_t a0 = tree_assoc(expr, 0);
149✔
2928
      switch (tree_subkind(a0)) {
149✔
2929
      case A_CONCAT:
15✔
2930
      case A_SLICE:
2931
         a0_type = type_elem(tree_type(tree_value(a0)));
15✔
2932
         break;
15✔
2933
      default:
134✔
2934
         a0_type = tree_type(tree_value(a0));
134✔
2935
         break;
134✔
2936
      }
2937

2938
      if (!type_is_unconstrained(a0_type))
149✔
2939
         elem = a0_type;
93✔
2940
   }
2941

2942
   type_set_elem(sub, elem);
2,955✔
2943

2944
   tree_t cons = tree_new(T_CONSTRAINT);
2,955✔
2945
   tree_set_subkind(cons, C_INDEX);
2,955✔
2946

2947
   tree_t r = tree_new(T_RANGE);
2,955✔
2948
   tree_set_subkind(r, dir);
2,955✔
2949
   tree_set_type(r, index_type);
2,955✔
2950
   tree_set_left(r, left);
2,955✔
2951
   tree_set_right(r, right);
2,955✔
2952

2953
   tree_add_range(cons, r);
2,955✔
2954

2955
   for (int i = 1; i < ndims; i++)
3,042✔
2956
      tree_add_range(cons, range_of(a0_type, i - 1));
87✔
2957

2958
   type_set_constraint(sub, cons);
2,955✔
2959

2960
   return sub;
2,955✔
2961
}
2962

2963
bool can_be_signal(type_t type)
55,495✔
2964
{
2965
   switch (type_kind(type)) {
76,286✔
2966
   case T_RECORD:
12,654✔
2967
      {
2968
         const int nfields = type_fields(type);
12,654✔
2969
         for (int i = 0; i < nfields; i++) {
50,104✔
2970
            if (!can_be_signal(tree_type(type_field(type, i))))
38,816✔
2971
               return false;
2972
         }
2973

2974
         return true;
2975
      }
2976
   case T_ARRAY:
8,034✔
2977
      return can_be_signal(type_elem(type));
8,034✔
2978
   case T_SUBTYPE:
12,757✔
2979
      return can_be_signal(type_base(type));
12,757✔
2980
   case T_ACCESS:
2981
   case T_FILE:
2982
   case T_PROTECTED:
2983
   case T_INCOMPLETE:
2984
      return false;
2985
   default:
36,612✔
2986
      return true;
36,612✔
2987
   }
2988
}
2989

2990
type_t merge_constraints(type_t to, type_t from)
1,318✔
2991
{
2992
   assert(type_is_unconstrained(to));
1,318✔
2993
   assert(type_eq(to, from));
1,318✔
2994

2995
   tree_t cto = NULL;
1,318✔
2996
   if (type_kind(to) == T_SUBTYPE && type_has_constraint(to))
1,318✔
2997
      cto = type_constraint(to);
30✔
2998

2999
   tree_t cfrom = NULL;
1,318✔
3000
   if (type_kind(from) == T_SUBTYPE && type_has_constraint(from))
1,318✔
3001
      cfrom = type_constraint(from);
1,036✔
3002

3003
   if (cfrom == NULL)
1,036✔
3004
      return to;
282✔
3005

3006
   type_t sub = type_new(T_SUBTYPE);
1,036✔
3007
   type_set_base(sub, type_base_recur(to));
1,036✔
3008

3009
   if (type_is_array(to)) {
1,036✔
3010
      type_set_constraint(sub, cto ?: cfrom);
2,047✔
3011

3012
      type_t elem = type_elem(to);
1,030✔
3013
      if (type_is_unconstrained(elem))
1,030✔
3014
         type_set_elem(sub, merge_constraints(elem, type_elem(from)));
18✔
3015
      else
3016
         type_set_elem(sub, elem);
1,012✔
3017
   }
3018
   else {
3019
      tree_t cnew = tree_new(T_CONSTRAINT);
6✔
3020
      tree_set_subkind(cnew, C_RECORD);
6✔
3021
      tree_set_loc(cnew, tree_loc(cto ?: cfrom));
7✔
3022

3023
      type_set_constraint(sub, cnew);
6✔
3024

3025
      if (cto != NULL) {
6✔
3026
         const int nto = tree_ranges(cto);
5✔
3027
         for (int i = 0; i < nto; i++)
10✔
3028
            tree_add_range(cnew, tree_range(cto, i));
5✔
3029
      }
3030

3031
      const int nfrom = tree_ranges(cfrom), base = tree_ranges(cnew);
6✔
3032
      for (int i = 0; i < nfrom; i++) {
16✔
3033
         tree_t ec = tree_range(cfrom, i);
10✔
3034
         assert(tree_kind(ec) == T_ELEM_CONSTRAINT);
10✔
3035

3036
         ident_t id = tree_ident(ec);
10✔
3037
         bool found = false;
10✔
3038
         for (int j = 0; j < base && !found; j++)
19✔
3039
            found |= tree_ident(tree_range(cnew, j)) == id;
9✔
3040

3041
         if (!found)
10✔
3042
            tree_add_range(cnew, ec);
5✔
3043
      }
3044
   }
3045

3046
   return sub;
3047
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc