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

nickg / nvc / 16099227452

06 Jul 2025 12:45PM UTC coverage: 92.335% (+0.05%) from 92.284%
16099227452

push

github

nickg
Handle underscores in Verilog decimal literals

Fixes #1230

18 of 20 new or added lines in 1 file covered. (90.0%)

598 existing lines in 16 files now uncovered.

71069 of 76969 relevant lines covered (92.33%)

564781.41 hits per line

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

95.2
/src/common.c
1
//
2
//  Copyright (C) 2013-2024  Nick Gasson
3
//
4
//  This program is free software: you can redistribute it and/or modify
5
//  it under the terms of the GNU General Public License as published by
6
//  the Free Software Foundation, either version 3 of the License, or
7
//  (at your option) any later version.
8
//
9
//  This program is distributed in the hope that it will be useful,
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
//  GNU General Public License for more details.
13
//
14
//  You should have received a copy of the GNU General Public License
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

18
#include "util.h"
19
#include "common.h"
20
#include "diag.h"
21
#include "hash.h"
22
#include "ident.h"
23
#include "lib.h"
24
#include "lower.h"
25
#include "option.h"
26
#include "phase.h"
27
#include "scan.h"
28
#include "thread.h"
29
#include "type.h"
30
#include "vlog/vlog-phase.h"
31
#include "sdf/sdf-phase.h"
32
#include "sdf/sdf-util.h"
33

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

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

45
int64_t assume_int(tree_t t)
741,734✔
46
{
47
   int64_t value;
741,734✔
48
   if (folded_int(t, &value))
741,734✔
49
      return value;
741,734✔
50

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

55
void range_bounds(tree_t r, int64_t *low, int64_t *high)
115,450✔
56
{
57
   assert(tree_kind(r) == T_RANGE);
115,450✔
58

59
   const int64_t left = assume_int(tree_left(r));
115,450✔
60
   const int64_t right = assume_int(tree_right(r));
115,450✔
61

62
   *low  = tree_subkind(r) == RANGE_TO ? left : right;
115,450✔
63
   *high = tree_subkind(r) == RANGE_TO ? right : left;
115,450✔
64
}
115,450✔
65

66
bool folded_int(tree_t t, int64_t *l)
6,101,172✔
67
{
68
   switch (tree_kind(t)) {
6,154,994✔
69
   case T_LITERAL:
3,957,410✔
70
      switch (tree_subkind(t)) {
3,957,410✔
71
      case L_PHYSICAL:
22,286✔
72
         if (tree_has_ref(t))
22,286✔
73
            return false;
74
         // Fall-through
75
      case L_INT:
76
         *l = tree_ival(t);
3,861,786✔
77
         return true;
3,861,786✔
78
      default:
79
         return false;
80
      }
81
   case T_QUALIFIED:
434✔
82
      return folded_int(tree_value(t), l);
434✔
83
   case T_REF:
1,914,231✔
84
      if (tree_has_ref(t)) {
1,914,231✔
85
         tree_t decl = tree_ref(t);
1,914,230✔
86
         switch (tree_kind(decl)) {
1,914,230✔
87
         case T_CONST_DECL:
63,574✔
88
            if (tree_has_value(decl))
63,574✔
89
               return folded_int(tree_value(decl), l);
52,934✔
90
            else
91
               return false;
92
         case T_ENUM_LIT:
1,765,417✔
93
            *l = tree_pos(decl);
1,765,417✔
94
            return true;
1,765,417✔
95
         case T_ALIAS:
454✔
96
            return folded_int(tree_value(decl), l);
454✔
97
         default:
98
            return false;
99
         }
100
      }
101
      // Fall-through
102
   default:
103
      return false;
104
   }
105
}
106

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

124
bool folded_length(tree_t r, int64_t *l)
82,156✔
125
{
126
   int64_t low, high;
82,156✔
127
   if (folded_bounds(r, &low, &high)) {
82,156✔
128
      *l = MAX(high - low + 1, 0);
77,841✔
129
      return true;
77,841✔
130
   }
131
   else
132
      return false;
133
}
134

135
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
2,501,607✔
136
{
137
   assert(tree_kind(r) == T_RANGE);
2,501,607✔
138

139
   const range_kind_t rkind = tree_subkind(r);
2,501,607✔
140

141
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
2,501,607✔
142
      return false;
143

144
   int64_t left, right;
2,492,468✔
145
   if (!folded_int(tree_left(r), &left))
2,492,468✔
146
      return false;
147
   else if (!folded_int(tree_right(r), &right))
2,328,850✔
148
      return false;
149

150
   switch (rkind) {
2,273,334✔
151
   case RANGE_TO:
2,099,369✔
152
      *low  = left;
2,099,369✔
153
      *high = right;
2,099,369✔
154
      return true;
2,099,369✔
155
   case RANGE_DOWNTO:
173,965✔
156
      *low  = right;
173,965✔
157
      *high = left;
173,965✔
158
      return true;
173,965✔
159
   default:
160
      return false;
161
   }
162
}
163

164
bool folded_bounds_real(tree_t r, double *low, double *high)
123,087✔
165
{
166
   assert(tree_kind(r) == T_RANGE);
123,087✔
167

168
   const range_kind_t rkind = tree_subkind(r);
123,087✔
169

170
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
123,087✔
171
      return false;
172

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

192
bool folded_bool(tree_t t, bool *b)
51,041✔
193
{
194
   if (tree_kind(t) == T_REF) {
51,041✔
195
      tree_t decl = tree_ref(t);
14,175✔
196
      if (tree_kind(decl) == T_ENUM_LIT
14,175✔
197
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
11,559✔
198
         *b = (tree_pos(decl) == 1);
11,559✔
199
         return true;
11,559✔
200
      }
201
   }
202

203
   return false;
204
}
205

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

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

217
   return b;
10,926✔
218
}
219

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

228
   return f;
53,924✔
229
}
230

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

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

253
   return f;
×
254
}
255

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

347
   while (isspace_iso88591(*str))
108✔
348
      ++str;
10✔
349

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

356
         if (is_negative) ++str;
71✔
357

358
         int64_t sum = 0;
359
         for (; isdigit_iso88591(*str) || (*str == '_'); str++) {
187✔
360
            if (*str != '_') {
116✔
361
               sum *= 10;
114✔
362
               sum += (*str - '0');
114✔
363
               num_digits++;
114✔
364
            }
365
         }
366

367
         value->integer = is_negative ? -sum : sum;
71✔
368

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

445
   default:
446
      return false;
447
   }
448

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

454
   return true;
455
}
456

457
tree_t make_ref(tree_t to)
9,564✔
458
{
459
   tree_t t = tree_new(T_REF);
9,564✔
460
   tree_set_ident(t, tree_ident(to));
9,564✔
461
   tree_set_ref(t, to);
9,564✔
462
   tree_set_type(t, tree_type(to));
9,564✔
463
   return t;
9,564✔
464
}
465

466
vhdl_standard_t standard(void)
3,557,027✔
467
{
468
   return current_std;
3,557,027✔
469
}
470

471
void set_standard(vhdl_standard_t s)
6,392✔
472
{
473
   current_std = s;
6,392✔
474
   have_set_std = true;
6,392✔
475
}
6,392✔
476

477
void set_default_standard(vhdl_standard_t s)
627✔
478
{
479
   if (!have_set_std)
627✔
480
      set_standard(s);
136✔
481
}
627✔
482

483
const char *standard_text(vhdl_standard_t s)
5,816✔
484
{
485
   static const char *text[] = {
5,816✔
486
      "1987", "1993", "2000", "2002", "2008", "2019"
487
   };
488

489
   if ((unsigned)s < ARRAY_LEN(text))
5,816✔
490
      return text[s];
5,816✔
491
   else
492
      return "????";
493
}
494

495
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
592✔
496
{
497
   switch (tree_kind(view)) {
1,831✔
498
   case T_REF:
795✔
499
      return find_element_mode_indication(tree_ref(view), field, converse);
795✔
500

501
   case T_ALIAS:
203✔
502
      return find_element_mode_indication(tree_value(view), field, converse);
203✔
503

504
   case T_ATTR_REF:
241✔
505
      assert(tree_subkind(view) == ATTR_CONVERSE);
241✔
506
      *converse = !*converse;
241✔
507
      return find_element_mode_indication(tree_name(view), field, converse);
241✔
508

509
   case T_VIEW_DECL:
592✔
510
      {
511
         type_t view_type = tree_type(view);
592✔
512
         assert(type_kind(view_type) == T_VIEW);
592✔
513

514
         const int nelems = type_fields(view_type);
592✔
515
         for (int i = 0; i < nelems; i++) {
988✔
516
            tree_t e = type_field(view_type, i);
988✔
517
            if (tree_ref(e) == field)
988✔
518
               return e;
592✔
519
         }
520

521
         return NULL;
522
      }
523

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

530
port_mode_t converse_mode(tree_t port, bool converse)
386✔
531
{
532
   const port_mode_t mode = tree_subkind(port);
386✔
533
   switch (mode) {
386✔
534
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
184✔
535
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
170✔
536
   default: return mode;
537
   }
538
}
539

540
class_t class_of(tree_t t)
1,242,044✔
541
{
542
   switch (tree_kind(t)) {
1,350,481✔
543
   case T_VAR_DECL:
544
      return C_VARIABLE;
545
   case T_SIGNAL_DECL:
60,745✔
546
   case T_IMPLICIT_SIGNAL:
547
      return C_SIGNAL;
60,745✔
548
   case T_CONST_DECL:
42,843✔
549
      return C_CONSTANT;
42,843✔
550
   case T_PORT_DECL:
174,450✔
551
   case T_GENERIC_DECL:
552
   case T_PARAM_DECL:
553
   case T_EXTERNAL_NAME:
554
      return tree_class(t);
174,450✔
555
   case T_ENUM_LIT:
703,057✔
556
   case T_LITERAL:
557
   case T_STRING:
558
      return C_LITERAL;
703,057✔
559
   case T_FIELD_DECL:
1,338✔
560
   case T_ATTR_DECL:
561
      return C_DEFAULT;
1,338✔
562
   case T_VIEW_DECL:
181✔
563
      return C_VIEW;
181✔
564
   case T_UNIT_DECL:
10,016✔
565
      return C_UNITS;
10,016✔
566
   case T_ARCH:
56✔
567
      return C_ARCHITECTURE;
56✔
568
   case T_FUNC_DECL:
26,166✔
569
   case T_FUNC_BODY:
570
   case T_FUNC_INST:
571
   case T_FCALL:
572
   case T_PROT_FCALL:
573
      return C_FUNCTION;
26,166✔
574
   case T_PROC_DECL:
28,791✔
575
   case T_PROC_BODY:
576
   case T_PROC_INST:
577
   case T_PCALL:
578
   case T_PROT_PCALL:
579
      return C_PROCEDURE;
28,791✔
580
   case T_ENTITY:
167✔
581
      return C_ENTITY;
167✔
582
   case T_SUBTYPE_DECL:
23,090✔
583
      return C_SUBTYPE;
23,090✔
584
   case T_TYPE_DECL:
82,270✔
585
   case T_PROT_DECL:
586
   case T_PROT_BODY:
587
      return C_TYPE;
82,270✔
588
   case T_FILE_DECL:
1,443✔
589
      return C_FILE;
1,443✔
590
   case T_PROCESS:
178✔
591
   case T_BLOCK:
592
   case T_FOR:
593
   case T_INSTANCE:
594
   case T_CONCURRENT:
595
   case T_ELAB:
596
   case T_PSL_DECL:
597
   case T_PSL_DIRECT:
598
   case T_FOR_GENERATE:
599
   case T_IF_GENERATE:
600
   case T_CASE_GENERATE:
601
      return C_LABEL;
178✔
602
   case T_COMPONENT:
1✔
603
      return C_COMPONENT;
1✔
604
   case T_REF:
95,779✔
605
   case T_PROT_REF:
606
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
95,779✔
607
   case T_ARRAY_REF:
12,662✔
608
   case T_ARRAY_SLICE:
609
   case T_RECORD_REF:
610
   case T_ALL:
611
   case T_ALIAS:
612
   case T_QUALIFIED:
613
      return class_of(tree_value(t));
12,662✔
614
   case T_PACKAGE:
1,428✔
615
   case T_PACK_BODY:
616
   case T_PACK_INST:
617
      return C_PACKAGE;
1,428✔
618
   case T_CONFIGURATION:
1✔
619
      return C_CONFIGURATION;
1✔
620
   case T_LIBRARY:
1✔
621
      return C_LIBRARY;
1✔
622
   case T_ATTR_REF:
115✔
623
      switch (tree_subkind(t)) {
115✔
624
      case ATTR_DELAYED:
625
      case ATTR_STABLE:
626
      case ATTR_QUIET:
627
      case ATTR_TRANSACTION:
628
         return C_SIGNAL;
629
      default:
107✔
630
         return C_DEFAULT;
107✔
631
      }
632
   case T_CONTEXT:
×
633
      return C_CONTEXT;
×
634
   default:
×
635
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
636
   }
637
}
638

639
bool class_has_type(class_t c)
1,019,434✔
640
{
641
   switch (c) {
1,019,434✔
642
   case C_LABEL:
643
   case C_ENTITY:
644
   case C_ARCHITECTURE:
645
   case C_COMPONENT:
646
   case C_CONFIGURATION:
647
   case C_PACKAGE:
648
   case C_LIBRARY:
649
      return false;
650
   default:
1,017,954✔
651
      return true;
1,017,954✔
652
   }
653
}
654

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

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

680
bool is_subprogram(tree_t t)
974,161✔
681
{
682
   switch (tree_kind(t)) {
974,161✔
683
   case T_FUNC_DECL:
684
   case T_FUNC_BODY:
685
   case T_FUNC_INST:
686
   case T_PROC_DECL:
687
   case T_PROC_BODY:
688
   case T_PROC_INST:
689
      return true;
690
   case T_GENERIC_DECL:
3,696✔
691
      {
692
         const class_t class = tree_class(t);
3,696✔
693
         return class == C_FUNCTION || class == C_PROCEDURE;
3,696✔
694
      }
695
   default:
774,753✔
696
      return false;
774,753✔
697
   }
698
}
699

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

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

728
bool is_concurrent_block(tree_t t)
2,997✔
729
{
730
   switch (tree_kind(t)) {
2,997✔
731
   case T_ARCH:
732
   case T_ENTITY:
733
   case T_BLOCK:
734
   case T_IF_GENERATE:
735
   case T_FOR_GENERATE:
736
   case T_CASE_GENERATE:
737
      return true;
738
   default:
504✔
739
      return false;
504✔
740
   }
741
}
742

743
bool is_package(tree_t t)
46,867✔
744
{
745
   switch (tree_kind(t)) {
46,867✔
746
   case T_PACKAGE:
747
   case T_PACK_BODY:
748
   case T_PACK_INST:
749
      return true;
750
   default:
1,660✔
751
      return false;
1,660✔
752
   }
753
}
754

755
bool is_design_unit(tree_t t)
51,500✔
756
{
757
   switch (tree_kind(t)) {
51,500✔
758
   case T_ENTITY:
759
   case T_ARCH:
760
   case T_PACKAGE:
761
   case T_PACK_BODY:
762
   case T_CONFIGURATION:
763
   case T_CONTEXT:
764
   case T_PACK_INST:
765
      return true;
766
   default:
11,549✔
767
      return false;
11,549✔
768
   }
769
}
770

771
bool is_literal(tree_t t)
74,604✔
772
{
773
   switch (tree_kind(t)) {
74,604✔
774
   case T_REF:
26,057✔
775
      return tree_has_ref(t) && tree_kind(tree_ref(t)) == T_ENUM_LIT;
28,674✔
776
   case T_LITERAL:
777
      return true;
778
   case T_STRING:
26,280✔
779
   default:
780
      return false;
26,280✔
781
   }
782
}
783

784
bool is_body(tree_t t)
12,249✔
785
{
786
   switch (tree_kind(t)) {
12,249✔
787
   case T_FUNC_BODY:
788
   case T_PROC_BODY:
789
   case T_PACK_BODY:
790
   case T_PROT_BODY:
791
      return true;
792
   default:
4,112✔
793
      return false;
4,112✔
794
   }
795
}
796

797
bool is_guarded_signal(tree_t decl)
18,210✔
798
{
799
   switch (tree_kind(decl)) {
18,210✔
800
   case T_PORT_DECL:
17,747✔
801
   case T_SIGNAL_DECL:
802
      return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
17,747✔
803
   default:
804
      return false;
805
   }
806
}
807

808
bool is_type_decl(tree_t t)
1,308,823✔
809
{
810
   switch (tree_kind(t)) {
1,308,823✔
811
   case T_TYPE_DECL:
812
   case T_SUBTYPE_DECL:
813
   case T_PROT_DECL:
814
   case T_PROT_BODY:
815
      return true;
816
   default:
1,077,323✔
817
      return false;
1,077,323✔
818
   }
819
}
820

821
tree_t aliased_type_decl(tree_t decl)
143,220✔
822
{
823
   switch (tree_kind(decl)) {
143,509✔
824
   case T_ALIAS:
291✔
825
      {
826
         tree_t value = tree_value(decl);
291✔
827
         const tree_kind_t kind = tree_kind(value);
291✔
828
         if (kind == T_REF && tree_has_ref(value))
291✔
829
            return aliased_type_decl(tree_ref(value));
289✔
830
         else if (kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
2✔
831
             return value;
832
         else
833
            return NULL;
×
834
      }
835
   case T_TYPE_DECL:
836
   case T_SUBTYPE_DECL:
837
   case T_PROT_DECL:
838
   case T_PROT_BODY:
839
      return decl;
840
   case T_GENERIC_DECL:
1,989✔
841
      if (tree_class(decl) == C_TYPE)
1,989✔
842
         return decl;
843
      else
844
         return NULL;
987✔
845
   default:
33,652✔
846
      return NULL;
33,652✔
847
   }
848
}
849

850
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
188,220✔
851
{
852
   tree_t p = tree_new(T_PARAM);
188,220✔
853
   tree_set_loc(p, tree_loc(value));
188,220✔
854
   tree_set_subkind(p, kind);
188,220✔
855
   tree_set_value(p, value);
188,220✔
856

857
   switch (kind) {
188,220✔
858
   case P_NAMED:
90✔
859
      assert(name != NULL);
90✔
860
      tree_set_name(p, name);
90✔
861
      break;
90✔
862
   case P_POS:
188,130✔
863
      tree_set_pos(p, tree_params(call));
188,130✔
864
      break;
188,130✔
865
   }
866

867
   tree_add_param(call, p);
188,220✔
868
   return p;
188,220✔
869
}
870

871
type_t array_aggregate_type(type_t array, int from_dim)
330✔
872
{
873
   if (type_is_none(array))
330✔
874
      return type_new(T_NONE);
2✔
875

876
   if (type_is_unconstrained(array)) {
328✔
877
      const int nindex = type_indexes(array);
57✔
878
      assert(from_dim < nindex);
57✔
879

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

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

887
      return type;
888
   }
889
   else {
890
      const int ndims = dimension_of(array);
271✔
891
      assert(from_dim < ndims);
271✔
892

893
      type_t base = type_new(T_ARRAY);
271✔
894
      type_set_ident(base, type_ident(array));
271✔
895
      type_set_elem(base, type_elem(array));
271✔
896

897
      tree_t constraint = tree_new(T_CONSTRAINT);
271✔
898
      tree_set_subkind(constraint, C_INDEX);
271✔
899

900
      type_t sub = type_new(T_SUBTYPE);
271✔
901
      type_set_base(sub, base);
271✔
902
      type_set_constraint(sub, constraint);
271✔
903

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

913
         type_add_index(base, tree_type(r));
295✔
914
         tree_add_range(constraint, r);
295✔
915
      }
916

917
      return sub;
918
   }
919
}
920

921
unsigned bits_for_range(int64_t low, int64_t high)
3,209,096✔
922
{
923
   if (low > high)
3,209,096✔
924
      return 0;   // Null range
925
   else if (low < 0) {
3,209,095✔
926
      // Signed integers
927
      if (low >= INT8_MIN && high <= INT8_MAX)
788,873✔
928
         return 8;
929
      else if (low >= INT16_MIN && high <= INT16_MAX)
781,195✔
930
         return 16;
931
      else if (low >= INT32_MIN && high <= INT32_MAX)
780,811✔
932
         return 32;
933
      else
934
         return 64;
128,448✔
935
   }
936
   else {
937
      // Unsigned integers
938
      if (high <= 1)
2,420,222✔
939
         return 1;
940
      else if (high <= UINT8_MAX)
1,471,022✔
941
         return 8;
942
      else if (high <= UINT16_MAX)
309,397✔
943
         return 16;
944
      else if (high <= UINT32_MAX)
303,978✔
945
         return 32;
946
      else
947
         return 64;
64,136✔
948
   }
949
}
950

951
unsigned dimension_of(type_t type)
1,843,953✔
952
{
953
   switch (type_kind(type)) {
3,474,273✔
954
   case T_SUBTYPE:
1,630,318✔
955
      return dimension_of(type_base(type));
1,630,318✔
956
   case T_GENERIC:
183✔
957
      switch (type_subkind(type)) {
183✔
958
      case GTYPE_ARRAY:
167✔
959
         return type_indexes(type);
167✔
960
      case GTYPE_ACCESS:
×
961
         return dimension_of(type_designated(type));
×
962
      case GTYPE_FILE:
963
      case GTYPE_PRIVATE:
964
         return 0;
965
      default:
16✔
966
         return 1;
16✔
967
      }
968
   case T_ARRAY:
1,835,505✔
969
      return type_indexes(type);
1,835,505✔
970
   case T_NONE:
971
   case T_RECORD:
972
   case T_INCOMPLETE:
973
   case T_FILE:
974
      return 0;
975
   case T_INTEGER:
8,195✔
976
   case T_REAL:
977
   case T_PHYSICAL:
978
   case T_ENUM:
979
      return type_dims(type);
8,195✔
980
   case T_ACCESS:
2✔
981
      return dimension_of(type_designated(type));
2✔
982
   default:
×
983
      fatal_trace("invalid type kind %s in dimension_of",
984
                  type_kind_str(type_kind(type)));
985
   }
986
}
987

988
tree_t range_of(type_t type, unsigned dim)
2,708,809✔
989
{
990
   switch (type_kind(type)) {
2,755,995✔
991
   case T_SUBTYPE:
2,149,837✔
992
      if (type_has_constraint(type)) {
2,149,837✔
993
         tree_t c = type_constraint(type);
2,102,651✔
994
         switch (tree_subkind(c)) {
2,102,651✔
995
         case C_INDEX:
2,102,651✔
996
         case C_RANGE:
997
            if (dim < tree_ranges(c))
2,102,651✔
998
               return tree_range(c, dim);
2,102,650✔
999
            else
1000
               return NULL;   // Must be an error
1001
         default:
×
1002
            should_not_reach_here();
1003
         }
1004
      }
1005
      else
1006
         return range_of(type_base(type), dim);
47,186✔
1007
   case T_INTEGER:
606,158✔
1008
   case T_REAL:
1009
   case T_PHYSICAL:
1010
   case T_ENUM:
1011
      return type_dim(type, dim);
606,158✔
1012
   default:
×
1013
      fatal_trace("invalid type kind %s for %s in range_of",
1014
                  type_kind_str(type_kind(type)), type_pp(type));
1015
   }
1016
}
1017

1018
range_kind_t direction_of(type_t type, unsigned dim)
29,807✔
1019
{
1020
   switch (type_kind(type)) {
29,911✔
1021
   case T_ENUM:
1022
      return RANGE_TO;
1023
   case T_NONE:
×
1024
      return RANGE_ERROR;
×
1025
   case T_INTEGER:
29,801✔
1026
   case T_REAL:
1027
   case T_PHYSICAL:
1028
   case T_SUBTYPE:
1029
      {
1030
         tree_t r = range_of(type, dim);
29,801✔
1031
         const range_kind_t rkind = tree_subkind(r);
29,801✔
1032
         if (rkind == RANGE_EXPR) {
29,801✔
1033
            // Return a fixed direction if possible
1034
            tree_t value = tree_value(r);
176✔
1035
            assert(tree_kind(value) == T_ATTR_REF);
176✔
1036

1037
            DEBUG_ONLY({
176✔
1038
                  const attr_kind_t attr = tree_subkind(value);
1039
                  assert(attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE);
1040
               });
176✔
1041

1042
            tree_t name = tree_name(value);
176✔
1043
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
176✔
1044
               tree_t decl = tree_ref(name);
175✔
1045
               if (is_type_decl(decl))
175✔
1046
                  return direction_of(tree_type(decl), 0);
104✔
1047
            }
1048
         }
1049

1050
         return rkind;
1051
      }
1052
   default:
×
1053
      fatal_trace("invalid type kind %s in direction_of",
1054
                  type_kind_str(type_kind(type)));
1055
   }
1056
}
1057

1058
type_t index_type_of(type_t type, unsigned dim)
282,431✔
1059
{
1060
   if (dim >= dimension_of(type))
282,433✔
1061
      return NULL;
1062

1063
   type_t base = type_base_recur(type);
282,404✔
1064
   type_kind_t base_kind = type_kind(base);
282,404✔
1065
   if (base_kind == T_ARRAY)
282,404✔
1066
      return type_index(base, dim);
278,508✔
1067
   else if (base_kind == T_ENUM || base_kind == T_NONE)
3,896✔
1068
      return type;
1069
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
3,710✔
1070
      return type_index(base, dim);
103✔
1071
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
3,607✔
1072
      return NULL;
1073
   else if (base_kind == T_ACCESS)
3,599✔
1074
      return index_type_of(type_designated(type), dim);
2✔
1075
   else
1076
      return tree_type(range_of(base, dim));
3,597✔
1077
}
1078

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

1087
ident_t well_known(well_known_t id)
601,897✔
1088
{
1089
   assert(id < NUM_WELL_KNOWN);
601,897✔
1090
   return id_cache[id];
601,897✔
1091
}
1092

1093
well_known_t is_well_known(ident_t ident)
318,107✔
1094
{
1095
   well_known_t pos = 0;
318,107✔
1096
   for (; pos < NUM_WELL_KNOWN; pos++) {
16,922,263✔
1097
      if (id_cache[pos] == ident)
16,757,595✔
1098
         break;
1099
   }
1100

1101
   return pos;
318,107✔
1102
}
1103

1104
void intern_strings(void)
6,673✔
1105
{
1106
   id_cache[W_STD_STANDARD]    = ident_new("STD.STANDARD");
6,673✔
1107
   id_cache[W_ALL]             = ident_new("all");
6,673✔
1108
   id_cache[W_STD_BIT]         = ident_new("STD.STANDARD.BIT");
6,673✔
1109
   id_cache[W_STD_BOOL]        = ident_new("STD.STANDARD.BOOLEAN");
6,673✔
1110
   id_cache[W_STD_CHAR]        = ident_new("STD.STANDARD.CHARACTER");
6,673✔
1111
   id_cache[W_STD_NATURAL]     = ident_new("STD.STANDARD.NATURAL");
6,673✔
1112
   id_cache[W_STD_POSITIVE]    = ident_new("STD.STANDARD.POSITIVE");
6,673✔
1113
   id_cache[W_STD_INTEGER]     = ident_new("STD.STANDARD.INTEGER");
6,673✔
1114
   id_cache[W_STD_STRING]      = ident_new("STD.STANDARD.STRING");
6,673✔
1115
   id_cache[W_STD_REAL]        = ident_new("STD.STANDARD.REAL");
6,673✔
1116
   id_cache[W_STD_TIME]        = ident_new("STD.STANDARD.TIME");
6,673✔
1117
   id_cache[W_STD_BIT_VECTOR]  = ident_new("STD.STANDARD.BIT_VECTOR");
6,673✔
1118
   id_cache[W_IEEE_SIGNED]     = ident_new("IEEE.NUMERIC_STD.SIGNED");
6,673✔
1119
   id_cache[W_IEEE_UNSIGNED]   = ident_new("IEEE.NUMERIC_STD.UNSIGNED");
6,673✔
1120
   id_cache[W_IEEE_LOGIC]      = ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC");
6,673✔
1121
   id_cache[W_IEEE_ULOGIC]     = ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC");
6,673✔
1122
   id_cache[W_IEEE_1164_AND]   = ident_new("IEEE.STD_LOGIC_1164.\"and\"");
6,673✔
1123
   id_cache[W_IEEE_1164_NAND]  = ident_new("IEEE.STD_LOGIC_1164.\"nand\"");
6,673✔
1124
   id_cache[W_IEEE_1164_OR]    = ident_new("IEEE.STD_LOGIC_1164.\"or\"");
6,673✔
1125
   id_cache[W_IEEE_1164_NOR]   = ident_new("IEEE.STD_LOGIC_1164.\"nor\"");
6,673✔
1126
   id_cache[W_IEEE_1164_XOR]   = ident_new("IEEE.STD_LOGIC_1164.\"xor\"");
6,673✔
1127
   id_cache[W_IEEE_1164_XNOR]  = ident_new("IEEE.STD_LOGIC_1164.\"xnor\"");
6,673✔
1128
   id_cache[W_FOREIGN]         = ident_new("FOREIGN");
6,673✔
1129
   id_cache[W_WORK]            = ident_new("WORK");
6,673✔
1130
   id_cache[W_STD]             = ident_new("STD");
6,673✔
1131
   id_cache[W_THUNK]           = ident_new("thunk");
6,673✔
1132
   id_cache[W_BODY]            = ident_new("body");
6,673✔
1133
   id_cache[W_CARET]           = ident_new("^");
6,673✔
1134
   id_cache[W_IEEE]            = ident_new("IEEE");
6,673✔
1135
   id_cache[W_IEEE_1164]       = ident_new("IEEE.STD_LOGIC_1164");
6,673✔
1136
   id_cache[W_ERROR]           = ident_new("$error");
6,673✔
1137
   id_cache[W_ELAB]            = ident_new("elab");
6,673✔
1138
   id_cache[W_NUMERIC_STD]     = ident_new("IEEE.NUMERIC_STD");
6,673✔
1139
   id_cache[W_NUMERIC_BIT]     = ident_new("IEEE.NUMERIC_BIT");
6,673✔
1140
   id_cache[W_NVC]             = ident_new("NVC");
6,673✔
1141
   id_cache[W_DEFAULT_CLOCK]   = ident_new("default clock");
6,673✔
1142
   id_cache[W_STD_REFLECTION]  = ident_new("STD.REFLECTION");
6,673✔
1143
   id_cache[W_NEVER_WAITS]     = ident_new("NEVER_WAITS");
6,673✔
1144
   id_cache[W_NVC_VERILOG]     = ident_new("NVC.VERILOG");
6,673✔
1145
   id_cache[W_NVC_PSL_SUPPORT] = ident_new("NVC.PSL_SUPPORT");
6,673✔
1146
   id_cache[W_INSTANCE_NAME]   = ident_new("instance_name");
6,673✔
1147
   id_cache[W_PATH_NAME]       = ident_new("path_name");
6,673✔
1148
   id_cache[W_VITAL]           = ident_new("VITAL");
6,673✔
1149
   id_cache[W_RESOLUTION]      = ident_new("resolution");
6,673✔
1150
   id_cache[W_TEXT_UTIL]       = ident_new("NVC.TEXT_UTIL");
6,673✔
1151

1152
   id_cache[W_IEEE_LOGIC_VECTOR] =
13,346✔
1153
      ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR");
6,673✔
1154
   id_cache[W_IEEE_ULOGIC_VECTOR] =
13,346✔
1155
      ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR");
6,673✔
1156
   id_cache[W_IEEE_1164_RISING_EDGE] =
13,346✔
1157
      ident_new("IEEE.STD_LOGIC_1164.RISING_EDGE(sU)B");
6,673✔
1158
   id_cache[W_IEEE_1164_FALLING_EDGE] =
13,346✔
1159
      ident_new("IEEE.STD_LOGIC_1164.FALLING_EDGE(sU)B");
6,673✔
1160

1161
   id_cache[W_NUMERIC_STD_UNSIGNED] = ident_new("IEEE.NUMERIC_STD_UNSIGNED");
6,673✔
1162
   id_cache[W_NUMERIC_BIT_UNSIGNED] = ident_new("IEEE.NUMERIC_BIT_UNSIGNED");
6,673✔
1163

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

1201
bool is_uninstantiated_package(tree_t pack)
46,210✔
1202
{
1203
   return tree_kind(pack) == T_PACKAGE
46,210✔
1204
      && tree_generics(pack) > 0
43,908✔
1205
      && tree_genmaps(pack) == 0;
48,660✔
1206
}
1207

1208
bool is_uninstantiated_subprogram(tree_t decl)
119,413✔
1209
{
1210
   switch (tree_kind(decl)) {
119,413✔
1211
   case T_FUNC_DECL:
118,721✔
1212
   case T_FUNC_BODY:
1213
   case T_PROC_DECL:
1214
   case T_PROC_BODY:
1215
      return tree_generics(decl) > 0;
118,721✔
1216
   default:
1217
      return false;
1218
   }
1219
}
1220

1221
bool is_anonymous_subtype(type_t type)
228,042✔
1222
{
1223
   return type_kind(type) == T_SUBTYPE && !type_has_ident(type);
228,042✔
1224
}
1225

1226
bool unit_needs_cgen(tree_t unit)
259✔
1227
{
1228
   switch (tree_kind(unit)) {
259✔
1229
   case T_PACK_INST:
1230
      return true;
UNCOV
1231
   case T_PACK_BODY:
×
1232
      {
UNCOV
1233
         tree_t pack = tree_primary(unit);
×
1234
         return package_needs_body(pack) && !is_uninstantiated_package(pack);
×
1235
      }
1236
   case T_PACKAGE:
16✔
1237
      return !package_needs_body(unit) && !is_uninstantiated_package(unit);
32✔
UNCOV
1238
   default:
×
1239
      return false;
×
1240
   }
1241
}
1242

1243
bool package_needs_body(tree_t pack)
11,252✔
1244
{
1245
   assert(tree_kind(pack) == T_PACKAGE);
11,252✔
1246

1247
   const int ndecls = tree_decls(pack);
11,252✔
1248
   for (int i = 0; i < ndecls; i++) {
840,800✔
1249
      tree_t d = tree_decl(pack, i);
839,230✔
1250
      const tree_kind_t dkind = tree_kind(d);
839,230✔
1251
      if ((dkind == T_FUNC_DECL || dkind == T_PROC_DECL)
839,230✔
1252
          && !(tree_flags(d) & TREE_F_PREDEFINED))
758,846✔
1253
         return true;
1254
      else if (dkind == T_CONST_DECL && !tree_has_value(d))
830,149✔
1255
         return true;
1256
      else if (dkind == T_PROT_DECL)
829,687✔
1257
         return true;
1258
   }
1259

1260
   return false;
1261
}
1262

1263
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
18,212✔
1264
                          well_known_t unit_name)
1265
{
1266
   const vhdl_standard_t curr = standard();
18,212✔
1267

1268
   if (cache[curr] == NULL) {
18,212✔
1269
      if (hint != NULL)
5,692✔
1270
         cache[curr] = hint;
1,787✔
1271
      else {
1272
         lib_t std = lib_require(well_known(lib_name));
3,905✔
1273
         cache[curr] = lib_get(std, well_known(unit_name));
3,905✔
1274
         assert(cache[curr] != NULL);
3,905✔
1275
      }
1276
   }
1277

1278
   assert(hint == NULL || hint == cache[curr]);
18,212✔
1279
   return cache[curr];
18,212✔
1280
}
1281

1282
static tree_t cached_std(tree_t hint)
17,442✔
1283
{
1284
   static tree_t standard_cache[STD_19 + 1] = {};
17,442✔
1285
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
17,442✔
1286
}
1287

1288
static tree_t search_type_decls(tree_t container, ident_t name)
17,985✔
1289
{
1290
   const int ndecls = tree_decls(container);
17,985✔
1291

1292
   for (int i = 0; i < ndecls; i++) {
1,161,283✔
1293
      tree_t d = tree_decl(container, i);
1,161,283✔
1294
      if (is_type_decl(d) && tree_ident(d) == name)
1,161,283✔
1295
         return d;
17,985✔
1296
   }
1297

1298
   return NULL;
1299
}
1300

1301
type_t std_type(tree_t std, std_type_t which)
1,207,410✔
1302
{
1303
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
1,207,410✔
1304
   assert(which < ARRAY_LEN(cache));
1,207,410✔
1305

1306
   if (cache[which] == NULL) {
1,207,410✔
1307
      const char *names[] = {
17,438✔
1308
         "universal_integer",
1309
         "universal_real",
1310
         "INTEGER",
1311
         "REAL",
1312
         "BOOLEAN",
1313
         "STRING",
1314
         "TIME",
1315
         "BIT",
1316
         "FILE_OPEN_KIND",
1317
         "FILE_OPEN_STATUS",
1318
         "NATURAL",
1319
         "BIT_VECTOR",
1320
         "SEVERITY_LEVEL",
1321
         "FILE_ORIGIN_KIND",
1322
         "FILE_OPEN_STATE",
1323
      };
1324

1325
      tree_t d = search_type_decls(cached_std(std), ident_new(names[which]));
17,438✔
1326
      if (d == NULL)
17,438✔
1327
         fatal_trace("cannot find standard type %s", names[which]);
1328

1329
      // Do not cache standard types while bootstrapping as the GC will
1330
      // move the objects after parsing
1331
      static int can_cache = -1;
17,438✔
1332
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
17,438✔
1333

1334
      if (can_cache)
17,438✔
1335
         return (cache[which] = tree_type(d));
17,248✔
1336
      else
1337
         return tree_type(d);
190✔
1338
   }
1339
   else
1340
      return cache[which];
1341
}
1342

1343
type_t ieee_type(ieee_type_t which)
1,799✔
1344
{
1345
   static type_t cache[IEEE_STD_LOGIC_VECTOR + 1] = {};
1,799✔
1346
   assert(which < ARRAY_LEN(cache));
1,799✔
1347

1348
   if (cache[which] == NULL) {
1,799✔
1349
      static const char *const names[] = {
216✔
1350
         [IEEE_STD_ULOGIC] = "STD_ULOGIC",
1351
         [IEEE_STD_LOGIC] = "STD_LOGIC",
1352
         [IEEE_STD_ULOGIC_VECTOR] = "STD_ULOGIC_VECTOR",
1353
         [IEEE_STD_LOGIC_VECTOR] = "STD_LOGIC_VECTOR",
1354
      };
1355

1356
      static tree_t ieee_cache[STD_19 + 1] = {};
216✔
1357
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
216✔
1358

1359
      tree_t d = search_type_decls(unit, ident_new(names[which]));
216✔
1360
      if (d == NULL)
216✔
1361
         fatal_trace("cannot find IEEE type %s", names[which]);
1362

1363
      // STD.STANDARD cannot depend on IEEE
1364
      assert(!opt_get_int(OPT_BOOTSTRAP));
216✔
1365

1366
      return (cache[which] = tree_type(d));
216✔
1367
   }
1368
   else
1369
      return cache[which];
1370
}
1371

1372
static tree_t cached_verilog(void)
512✔
1373
{
1374
   static tree_t verilog_cache[STD_19 + 1] = {};
512✔
1375
   return cached_unit(NULL, verilog_cache, W_NVC, W_NVC_VERILOG);
512✔
1376
}
1377

1378
type_t verilog_type(verilog_type_t which)
959✔
1379
{
1380
   static type_t cache[VERILOG_WIRE_ARRAY + 1] = {};
959✔
1381
   assert(which < ARRAY_LEN(cache));
959✔
1382

1383
   if (cache[which] == NULL) {
959✔
1384
      static const char *const names[] = {
289✔
1385
         [VERILOG_LOGIC] = "T_LOGIC",
1386
         [VERILOG_LOGIC_ARRAY] = "T_LOGIC_ARRAY",
1387
         [VERILOG_INT64] = "T_INT64",
1388
         [VERILOG_NET_VALUE] = "T_NET_VALUE",
1389
         [VERILOG_NET_ARRAY] = "T_NET_ARRAY",
1390
         [VERILOG_WIRE] = "T_WIRE",
1391
         [VERILOG_WIRE_ARRAY] = "T_WIRE_ARRAY",
1392
      };
1393

1394
      tree_t d = search_type_decls(cached_verilog(), ident_new(names[which]));
289✔
1395
      if (d == NULL)
289✔
1396
         fatal_trace("cannot find NVC.VERILOG type %s", names[which]);
1397

1398
      // STD.STANDARD cannot depend on NVC.VERILOG
1399
      assert(!opt_get_int(OPT_BOOTSTRAP));
289✔
1400

1401
      return (cache[which] = tree_type(d));
289✔
1402
   }
1403
   else
1404
      return cache[which];
1405
}
1406

1407
type_t reflection_type(reflect_type_t which)
249✔
1408
{
1409
   static type_t cache[REFLECT_SUBTYPE_MIRROR + 1] = {};
249✔
1410
   assert(which < ARRAY_LEN(cache));
249✔
1411

1412
   if (cache[which] == NULL) {
249✔
1413
      static const char *const names[] = {
42✔
1414
         [REFLECT_VALUE_MIRROR] = "VALUE_MIRROR",
1415
         [REFLECT_SUBTYPE_MIRROR] = "SUBTYPE_MIRROR",
1416
      };
1417

1418
      static tree_t reflect_cache[STD_19 + 1] = {};
42✔
1419
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
42✔
1420

1421
      tree_t d = search_type_decls(unit, ident_new(names[which]));
42✔
1422
      if (d == NULL)
42✔
1423
         fatal_trace("cannot find REFLECTION type %s", names[which]);
1424

1425
      // STD.STANDARD cannot depend on REFLECTION
1426
      assert(!opt_get_int(OPT_BOOTSTRAP));
42✔
1427

1428
      return (cache[which] = tree_type(d));
42✔
1429
   }
1430
   else
1431
      return cache[which];
1432
}
1433

1434
bool is_open_coded_builtin(subprogram_kind_t kind)
1,435,794✔
1435
{
1436
   switch (kind) {
1,435,794✔
1437
   case S_ADD:
1438
   case S_SUB:
1439
   case S_DIV:
1440
   case S_MUL:
1441
   case S_MUL_PR:
1442
   case S_MUL_RP:
1443
   case S_MUL_PI:
1444
   case S_MUL_IP:
1445
   case S_DIV_PR:
1446
   case S_DIV_PP:
1447
   case S_DIV_PI:
1448
   case S_IDENTITY:
1449
   case S_NEGATE:
1450
   case S_SCALAR_LT:
1451
   case S_SCALAR_LE:
1452
   case S_SCALAR_GT:
1453
   case S_SCALAR_GE:
1454
   case S_SCALAR_EQ:
1455
   case S_SCALAR_NEQ:
1456
   case S_ABS:
1457
   case S_MOD:
1458
   case S_REM:
1459
   case S_EXP:
1460
   case S_MUL_RI:
1461
   case S_MUL_IR:
1462
   case S_DIV_RI:
1463
   case S_CONCAT:
1464
   case S_SCALAR_AND:
1465
   case S_SCALAR_OR:
1466
   case S_SCALAR_NOT:
1467
   case S_SCALAR_NAND:
1468
   case S_SCALAR_NOR:
1469
   case S_SCALAR_XOR:
1470
   case S_SCALAR_XNOR:
1471
   case S_FILE_OPEN1:
1472
   case S_FILE_OPEN2:
1473
   case S_FILE_READ:
1474
   case S_FILE_WRITE:
1475
   case S_DEALLOCATE:
1476
      return true;
1477
   default:
537,063✔
1478
      return false;
537,063✔
1479
   }
1480
}
1481

1482
tree_t std_func(ident_t mangled)
4✔
1483
{
1484
   tree_t std = cached_std(NULL);
4✔
1485

1486
   const int ndecls = tree_decls(std);
4✔
1487
   for (int i = 0; i < ndecls; i++) {
736✔
1488
      tree_t d = tree_decl(std, i);
732✔
1489
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
732✔
UNCOV
1490
         return d;
×
1491
   }
1492

1493
   return NULL;
1494
}
1495

1496
tree_t verilog_func(ident_t mangled)
223✔
1497
{
1498
   tree_t pack = cached_verilog();
223✔
1499

1500
   const int ndecls = tree_decls(pack);
223✔
1501
   for (int i = 0; i < ndecls; i++) {
21,274✔
1502
      tree_t d = tree_decl(pack, i);
21,274✔
1503
      if (is_subprogram(d) && tree_ident2(d) == mangled)
21,274✔
1504
         return d;
223✔
1505
   }
1506

1507
   fatal_trace("missing Verilog helper function %s", istr(mangled));
1508
}
1509

1510
tree_t name_to_ref(tree_t name)
113,816✔
1511
{
1512
   tree_kind_t kind;
113,816✔
1513
   while ((kind = tree_kind(name)) != T_REF) {
131,154✔
1514
      switch (kind) {
19,539✔
1515
      case T_ARRAY_REF:
17,338✔
1516
      case T_ARRAY_SLICE:
1517
      case T_RECORD_REF:
1518
      case T_ALL:
1519
         name = tree_value(name);
17,338✔
1520
         break;
17,338✔
1521
      default:
1522
         return NULL;
1523
      }
1524
   }
1525

1526
   return name;
1527
}
1528

1529
const char *port_mode_str(port_mode_t mode)
44✔
1530
{
1531
   const char *mode_str[] = {
44✔
1532
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE", "VIEW", "VIEW"
1533
   };
1534
   assert(mode < ARRAY_LEN(mode_str));
44✔
1535
   return mode_str[mode];
44✔
1536
}
1537

1538
void mangle_one_type(text_buf_t *buf, type_t type)
200,975✔
1539
{
1540
   ident_t ident = type_ident(type);
200,975✔
1541

1542
   char code = 0;
200,975✔
1543
   switch (is_well_known(ident)) {
200,975✔
1544
   case W_STD_INTEGER:        code = 'I'; break;
1545
   case W_STD_STRING:         code = 'S'; break;
3,915✔
1546
   case W_STD_REAL:           code = 'R'; break;
4,000✔
1547
   case W_STD_BOOL:           code = 'B'; break;
27,018✔
1548
   case W_STD_CHAR:           code = 'C'; break;
677✔
1549
   case W_STD_TIME:           code = 'T'; break;
1,018✔
1550
   case W_STD_NATURAL:        code = 'N'; break;
4,739✔
1551
   case W_STD_POSITIVE:       code = 'P'; break;
463✔
1552
   case W_STD_BIT:            code = 'J'; break;
3,011✔
1553
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
3,193✔
1554
   case W_IEEE_LOGIC:         code = 'L'; break;
330✔
1555
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,858✔
1556
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
1,768✔
1557
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,399✔
1558
   default: break;
1559
   }
1560

1561
   if (code)
56,389✔
1562
      tb_append(buf, code);
71,559✔
1563
   else {
1564
      tb_printf(buf, "%zu", ident_len(ident));
129,416✔
1565
      tb_istr(buf, ident);
129,416✔
1566
   }
1567
}
200,975✔
1568

1569
ident_t get_call_context(ident_t mangled)
28,245✔
1570
{
1571
   const char *str = istr(mangled), *p = str, *end = NULL;
28,245✔
1572
   for (; *p; p++) {
775,562✔
1573
      if (*p == '(') break;
740,518✔
1574
      if (*p == '.') end = p;
719,072✔
1575
   }
1576
   assert(end != NULL);
28,245✔
1577

1578
   return ident_new_n(str, end - str);
28,245✔
1579
}
1580

1581
tree_t primary_unit_of(tree_t unit)
20,723✔
1582
{
1583
   switch (tree_kind(unit)) {
20,723✔
1584
   case T_ENTITY:
1585
   case T_COMPONENT:
1586
   case T_PACKAGE:
1587
   case T_BLOCK:
1588
   case T_ELAB:
1589
   case T_PACK_INST:
1590
      return unit;
1591
   case T_ARCH:
10,387✔
1592
   case T_CONFIGURATION:
1593
   case T_PACK_BODY:
1594
      return tree_primary(unit);
10,387✔
UNCOV
1595
   default:
×
1596
      fatal_trace("invalid kind %s in primary_unit_of",
1597
                  tree_kind_str(tree_kind(unit)));
1598
   }
1599
}
1600

1601
unsigned get_case_choice_char(tree_t value, int depth)
12,569✔
1602
{
1603
   switch (tree_kind(value)) {
13,605✔
1604
   case T_STRING:
12,171✔
1605
      if (depth < tree_chars(value))
12,171✔
1606
         return assume_int(tree_char(value, depth));
12,170✔
1607
      else
1608
         return ~0;   // Out of bounds
1609

1610
   case T_AGGREGATE:
398✔
1611
      {
1612
         const int nassocs = tree_assocs(value);
398✔
1613
         type_t type = tree_type(value);
398✔
1614

1615
         for (int i = 0, pos = 0; i < nassocs; i++) {
600✔
1616
            tree_t a = tree_assoc(value, i);
599✔
1617
            switch (tree_subkind(a)) {
599✔
UNCOV
1618
            case A_NAMED:
×
1619
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
1620
                  return assume_int(tree_value(a));
×
1621
               break;
1622

1623
            case A_POS:
23✔
1624
               if (pos++ == (unsigned)depth)
23✔
1625
                  return assume_int(tree_value(a));
13✔
1626
               break;
1627

1628
            case A_CONCAT:
480✔
1629
               {
1630
                  tree_t left = tree_value(a);
480✔
1631

1632
                  type_t left_type = tree_type(left);
480✔
1633
                  if (type_is_unconstrained(left_type))
480✔
UNCOV
1634
                     fatal_at(tree_loc(left), "sorry, this expression is not "
×
1635
                              "currently supported in a case choice");
1636

1637
                  tree_t lr = range_of(tree_type(left), 0);
480✔
1638
                  int64_t left_len;
480✔
1639
                  if (!folded_length(lr, &left_len))
480✔
UNCOV
1640
                     fatal_at(tree_loc(left), "cannot determine length of "
×
1641
                              "aggregate element");
1642

1643
                  if (depth < pos + left_len)
480✔
1644
                     return get_case_choice_char(left, depth - pos);
320✔
1645

1646
                  pos += left_len;
160✔
1647
               }
1648
               break;
160✔
1649

UNCOV
1650
            case A_OTHERS:
×
1651
               return assume_int(tree_value(a));
×
1652

1653
            case A_SLICE:
96✔
1654
               {
1655
                  tree_t base = tree_value(a);
96✔
1656
                  tree_t r = tree_range(a, 0);
96✔
1657

1658
                  const int64_t rleft = assume_int(tree_left(r));
96✔
1659
                  const int64_t rright = assume_int(tree_right(r));
96✔
1660

1661
                  const int64_t loffset = rebase_index(type, 0, rleft);
96✔
1662
                  const int64_t roffset = rebase_index(type, 0, rright);
96✔
1663

1664
                  if (depth >= loffset && depth <= roffset)
96✔
1665
                     return get_case_choice_char(base, depth - loffset);
64✔
1666
               }
1667
            }
1668
         }
1669

1670
         // This will produce an error during bounds checking
1671
         return ~0;
1672
      }
1673

1674
   case T_REF:
716✔
1675
      {
1676
         tree_t decl = tree_ref(value);
716✔
1677
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
716✔
1678
         assert(tree_has_value(decl));
716✔
1679
         return get_case_choice_char(tree_value(decl), depth);
716✔
1680
      }
1681

1682
   case T_ARRAY_SLICE:
320✔
1683
      {
1684
         tree_t base = tree_value(value);
320✔
1685
         tree_t r = tree_range(value, 0);
320✔
1686
         const int64_t rleft = assume_int(tree_left(r));
320✔
1687
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
320✔
1688
         return get_case_choice_char(base, depth + offset);
320✔
1689
      }
1690

UNCOV
1691
   default:
×
1692
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1693
               tree_kind_str(tree_kind(value)));
1694
   }
1695
}
1696

1697
int64_t encode_case_choice(tree_t value, int length, int bits)
1,800✔
1698
{
1699
   uint64_t enc = 0;
1,800✔
1700
   for (int i = 0; i < length; i++) {
13,963✔
1701
      if (bits > 0) {
12,163✔
1702
         enc <<= bits;
3,816✔
1703
         enc |= get_case_choice_char(value, i);
3,816✔
1704
      }
1705
      else {
1706
         enc *= 0x27d4eb2d;
8,347✔
1707
         enc += get_case_choice_char(value, i);
8,347✔
1708
      }
1709
   }
1710

1711
   return enc;
1,800✔
1712
}
1713

1714
void to_string(text_buf_t *tb, type_t type, int64_t value)
699✔
1715
{
1716
   if (type_is_integer(type))
699✔
1717
      tb_printf(tb, "%"PRIi64, value);
532✔
1718
   else if (type_is_enum(type)) {
167✔
1719
      type_t base = type_base_recur(type);
70✔
1720
      if (value < 0 || value >= type_enum_literals(base))
70✔
1721
         tb_printf(tb, "%"PRIi64, value);
4✔
1722
      else
1723
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
66✔
1724
   }
1725
   else if (type_is_physical(type)) {
97✔
1726
      type_t base = type_base_recur(type);
27✔
1727
      const unsigned nunits = type_units(base);
27✔
1728
      tree_t max_unit = NULL;
27✔
1729
      int64_t max_unit_value = 0;
27✔
1730

1731
      // Find the largest unit that evenly divides the given value
1732
      for (unsigned u = 0; u < nunits; u++) {
243✔
1733
         tree_t unit = type_unit(base, u);
216✔
1734
         const int64_t unit_value = assume_int(tree_value(unit));
216✔
1735
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
216✔
1736
            max_unit = unit;
100✔
1737
            max_unit_value = unit_value;
100✔
1738
         }
1739
      }
1740
      assert(max_unit);
27✔
1741

1742
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
27✔
1743
                istr(tree_ident(max_unit)));
1744
   }
1745
   else if (type_is_real(type)) {
70✔
1746
      union { int64_t i; double r; } u = { .i = value };
62✔
1747
      tb_printf(tb, "%.17g", u.r);
62✔
1748
   }
1749
   else if (type_is_access(type)) {
8✔
1750
      if (value == 0)
8✔
1751
         tb_cat(tb, "NULL");
4✔
1752
      else
1753
         tb_printf(tb, "%p", (void *)value);
4✔
1754
   }
1755
}
699✔
1756

1757
static bool is_static(tree_t expr)
8,044✔
1758
{
1759
   switch (tree_kind(expr)) {
8,157✔
1760
   case T_REF:
926✔
1761
      {
1762
         tree_t decl = tree_ref(expr);
926✔
1763
         switch (tree_kind(decl)) {
926✔
1764
         case T_CONST_DECL:
224✔
1765
            return !!(tree_flags(decl) & TREE_F_GLOBALLY_STATIC);
224✔
1766
         case T_UNIT_DECL:
1767
         case T_ENUM_LIT:
1768
         case T_GENERIC_DECL:
1769
            return true;
UNCOV
1770
         case T_ALIAS:
×
1771
            return is_static(tree_value(decl));
×
1772
         default:
250✔
1773
            return false;
250✔
1774
         }
1775
      }
1776

1777
   case T_LITERAL:
1778
   case T_STRING:
1779
      return true;
1780

1781
   case T_FCALL:
534✔
1782
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
534✔
1783
                                    | TREE_F_GLOBALLY_STATIC));
1784

1785
   case T_RECORD_REF:
109✔
1786
      return is_static(tree_value(expr));
109✔
1787

1788
   case T_ARRAY_REF:
80✔
1789
      {
1790
         if (!is_static(tree_value(expr)))
80✔
1791
            return false;
1792

1793
         const int nparams = tree_params(expr);
80✔
1794
         for (int i = 0; i < nparams; i++) {
160✔
1795
            if (!is_static(tree_value(tree_param(expr, i))))
80✔
1796
               return false;
1797
         }
1798

1799
         return true;
1800
      }
1801

1802
   case T_ARRAY_SLICE:
40✔
1803
      {
1804
         if (!is_static(tree_value(expr)))
40✔
1805
            return false;
1806

1807
         assert(tree_ranges(expr) == 1);
40✔
1808

1809
         tree_t r = tree_range(expr, 0);
40✔
1810
         if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
40✔
UNCOV
1811
            return false;
×
1812

1813
         return true;
1814
      }
1815

1816
   case T_ATTR_REF:
11✔
1817
      {
1818
         switch (tree_subkind(expr)) {
11✔
1819
         case ATTR_EVENT:
1820
         case ATTR_ACTIVE:
1821
         case ATTR_LAST_EVENT:
1822
         case ATTR_LAST_ACTIVE:
1823
         case ATTR_LAST_VALUE:
1824
         case ATTR_DRIVING:
1825
         case ATTR_DRIVING_VALUE:
1826
         case ATTR_STABLE:
1827
         case ATTR_QUIET:
1828
            return false;
1829
         case ATTR_POS:
4✔
1830
         case ATTR_VAL:
1831
         case ATTR_LEFTOF:
1832
         case ATTR_RIGHTOF:
1833
         case ATTR_SUCC:
1834
         case ATTR_PRED:
1835
         case ATTR_VALUE:
1836
         case ATTR_IMAGE:
1837
            assert(tree_params(expr) == 1);
4✔
1838
            return is_static(tree_value(tree_param(expr, 0)));
4✔
1839
         case ATTR_LENGTH:
7✔
1840
         case ATTR_LEFT:
1841
         case ATTR_RIGHT:
1842
         case ATTR_LOW:
1843
         case ATTR_HIGH:
1844
            {
1845
               type_t type = tree_type(tree_name(expr));
7✔
1846
               if (type_is_unconstrained(type))
7✔
1847
                  return false;
1848

1849
               int64_t dim = 1;
4✔
1850
               if (tree_params(expr) == 1)
4✔
UNCOV
1851
                  dim = assume_int(tree_value(tree_param(expr, 0)));
×
1852

1853
               tree_t r = range_of(type, dim - 1);
4✔
1854
               if (tree_subkind(r) == RANGE_EXPR)
4✔
1855
                  return false;
1856

1857
               return is_static(tree_left(r)) && is_static(tree_right(r));
4✔
1858
            }
UNCOV
1859
         default:
×
1860
            return true;
×
1861
         }
1862
      }
1863

UNCOV
1864
   default:
×
1865
      return false;
×
1866
   }
1867
}
1868

1869
tree_t longest_static_prefix(tree_t expr)
30,500✔
1870
{
1871
   switch (tree_kind(expr)) {
30,500✔
1872
   case T_ARRAY_REF:
5,578✔
1873
      {
1874
         tree_t value = tree_value(expr);
5,578✔
1875
         tree_t prefix = longest_static_prefix(value);
5,578✔
1876

1877
         if (prefix != value)
5,578✔
1878
            return prefix;
1879

1880
         const int nparams = tree_params(expr);
5,541✔
1881
         for (int i = 0; i < nparams; i++) {
11,332✔
1882
            if (!is_static(tree_value(tree_param(expr, i))))
6,109✔
1883
               return prefix;
1884
         }
1885

1886
         return expr;
1887
      }
1888

1889
   case T_ARRAY_SLICE:
848✔
1890
      {
1891
         tree_t value = tree_value(expr);
848✔
1892
         tree_t prefix = longest_static_prefix(value);
848✔
1893

1894
         if (prefix != value)
848✔
1895
            return prefix;
1896

1897
         assert(tree_ranges(expr) == 1);
839✔
1898

1899
         tree_t r = tree_range(expr, 0);
839✔
1900
         if (tree_subkind(r) == RANGE_EXPR)
839✔
1901
            return prefix;
1902
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
832✔
1903
            return prefix;
20✔
1904

1905
         return expr;
1906
      }
1907

1908
   case T_RECORD_REF:
1,539✔
1909
      {
1910
         tree_t value = tree_value(expr);
1,539✔
1911
         tree_t prefix = longest_static_prefix(value);
1,539✔
1912

1913
         if (prefix != value)
1,539✔
1914
            return prefix;
28✔
1915

1916
         return expr;
1917
      }
1918

1919
   default:
1920
      return expr;
1921
   }
1922
}
1923

1924
tree_t body_of(tree_t pack)
9,972✔
1925
{
1926
   const tree_kind_t kind = tree_kind(pack);
9,972✔
1927
   if (kind == T_PACK_INST)
9,972✔
1928
      return NULL;
1929

1930
   assert(tree_kind(pack) == T_PACKAGE);
9,972✔
1931

1932
   ident_t body_i = well_known(W_BODY);
9,972✔
1933
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
9,972✔
1934
   return lib_get_qualified(body_name);
9,972✔
1935
}
1936

1937
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
758✔
1938
{
1939
   const int ngenmaps = tree_genmaps(unit);
758✔
1940

1941
   if (pos < ngenmaps) {
758✔
1942
      tree_t m = tree_genmap(unit, pos);
582✔
1943
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
582✔
1944
         return tree_value(m);
395✔
1945
   }
1946

1947
   for (int j = 0; j < ngenmaps; j++) {
1,295✔
1948
      tree_t m = tree_genmap(unit, j);
1,119✔
1949
      switch (tree_subkind(m)) {
1,119✔
1950
      case P_NAMED:
569✔
1951
         {
1952
            tree_t name = tree_name(m);
569✔
1953
            assert(tree_kind(name) == T_REF);
569✔
1954

1955
            if (tree_has_ref(name) && tree_ref(name) == g)
569✔
1956
               return tree_value(m);
19✔
1957
         }
1958
         break;
1959

1960
      case P_POS:
550✔
1961
         if (tree_pos(m) == pos)
550✔
1962
            return tree_value(m);
168✔
1963
         break;
1964

1965
      default:
1966
         break;
1967
      }
1968
   }
1969

1970
   return NULL;
1971
}
1972

1973
bool relaxed_rules(void)
710,381✔
1974
{
1975
   return opt_get_int(OPT_RELAXED);
710,381✔
1976
}
1977

1978
bool is_type_attribute(attr_kind_t kind)
79,644✔
1979
{
1980
   switch (kind) {
79,644✔
1981
   case ATTR_SUBTYPE:
1982
   case ATTR_BASE:
1983
   case ATTR_ELEMENT:
1984
   case ATTR_DESIGNATED_SUBTYPE:
1985
   case ATTR_INDEX:
1986
      return true;
1987
   default:
78,845✔
1988
      return false;
78,845✔
1989
   }
1990
}
1991

1992
bool attribute_has_param(attr_kind_t attr)
26,499✔
1993
{
1994
   switch (attr) {
26,499✔
1995
   case ATTR_IMAGE:
1996
   case ATTR_SUCC:
1997
   case ATTR_PRED:
1998
   case ATTR_DELAYED:
1999
   case ATTR_LEFTOF:
2000
   case ATTR_RIGHTOF:
2001
   case ATTR_VALUE:
2002
   case ATTR_POS:
2003
   case ATTR_LOW:
2004
   case ATTR_HIGH:
2005
   case ATTR_LEFT:
2006
   case ATTR_RIGHT:
2007
   case ATTR_LENGTH:
2008
   case ATTR_RANGE:
2009
   case ATTR_REVERSE_RANGE:
2010
   case ATTR_VAL:
2011
   case ATTR_QUIET:
2012
   case ATTR_STABLE:
2013
   case ATTR_INDEX:
2014
   case ATTR_ASCENDING:
2015
      return true;
2016
   default:
2,703✔
2017
      return false;
2,703✔
2018
   }
2019
}
2020

2021
type_t get_type_or_null(tree_t t)
2,715,548✔
2022
{
2023
   switch (tree_kind(t)) {
2,715,548✔
2024
   case T_LIBRARY:
2025
   case T_ATTR_SPEC:
2026
   case T_PACKAGE:
2027
   case T_PACK_INST:
2028
   case T_PACK_BODY:
2029
   case T_ENTITY:
2030
   case T_ARCH:
2031
   case T_PROCESS:
2032
   case T_COMPONENT:
2033
   case T_INSTANCE:
2034
   case T_CONCURRENT:
2035
   case T_BLOCK:
2036
   case T_WHILE:
2037
   case T_FOR:
2038
   case T_LOOP:
2039
   case T_GROUP_TEMPLATE:
2040
   case T_CONFIGURATION:
2041
   case T_GROUP:
2042
   case T_FOR_GENERATE:
2043
   case T_IF_GENERATE:
2044
   case T_CASE_GENERATE:
2045
   case T_USE:
2046
   case T_CONTEXT:
2047
   case T_PSL_DECL:
2048
   case T_PSL_DIRECT:
2049
   case T_WAVEFORM:
2050
      return NULL;
2051
   default:
2,601,921✔
2052
      if (tree_has_type(t))
2,601,921✔
2053
         return tree_type(t);
2,600,185✔
2054
      else
2055
         return NULL;
2056
   }
2057
}
2058

2059
type_t subtype_for_string(tree_t str, type_t base)
31,357✔
2060
{
2061
   if (type_const_bounds(base))
31,357✔
2062
      return base;    // Can be checked statically
2063
   else if (!type_is_unconstrained(base))
27,402✔
2064
      base = type_base_recur(base);
188✔
2065

2066
   // Construct a new constrained array subtype: the direction and
2067
   // bounds are the same as those for a positional array aggregate
2068
   type_t sub = type_new(T_SUBTYPE);
27,402✔
2069
   type_set_base(sub, base);
27,402✔
2070

2071
   type_t index_type = index_type_of(base, 0);
27,402✔
2072
   const bool is_enum = type_is_enum(index_type);
27,402✔
2073

2074
   // The direction is determined by the index type
2075
   range_kind_t dir = direction_of(index_type, 0);
27,402✔
2076
   tree_t index_r = range_of(index_type, 0);
27,402✔
2077

2078
   // The left bound is the left of the index type and the right bound
2079
   // is determined by the number of elements
2080

2081
   tree_t left = NULL, right = NULL;
27,402✔
2082
   const int nchars = tree_chars(str);
27,402✔
2083

2084
   if (is_enum) {
27,402✔
2085
      const int nlits = type_enum_literals(type_base_recur(index_type));
14✔
2086
      int64_t index_left = assume_int(tree_left(index_r));
14✔
2087

2088
      int64_t iright, ileft;
14✔
2089
      if (nchars == 0) {
14✔
2090
         iright = index_left;
1✔
2091
         ileft = assume_int(tree_right(index_r));
1✔
2092
      }
2093
      else if (dir == RANGE_DOWNTO) {
13✔
UNCOV
2094
         ileft = index_left;
×
2095
         iright = MIN(nlits - 1, MAX(0, index_left - nchars + 1));
×
2096
      }
2097
      else {
2098
         ileft = index_left;
13✔
2099
         iright = MIN(nlits - 1, MAX(0, index_left + nchars - 1));
13✔
2100
      }
2101

2102
      left = get_enum_lit(str, index_type, ileft);
14✔
2103
      right = get_enum_lit(str, index_type, iright);
14✔
2104
   }
2105
   else {
2106
      left = tree_left(index_r);
27,388✔
2107

2108
      int64_t iright;
27,388✔
2109
      if (dir == RANGE_DOWNTO)
27,388✔
UNCOV
2110
         iright = assume_int(left) - nchars + 1;
×
2111
      else
2112
         iright = assume_int(left) + nchars - 1;
27,388✔
2113

2114
      right = get_int_lit(str, index_type, iright);
27,388✔
2115
   }
2116

2117
   tree_t r = tree_new(T_RANGE);
27,402✔
2118
   tree_set_subkind(r, dir);
27,402✔
2119
   tree_set_left(r, left);
27,402✔
2120
   tree_set_right(r, right);
27,402✔
2121
   tree_set_loc(r, tree_loc(str));
27,402✔
2122
   tree_set_type(r, index_type);
27,402✔
2123

2124
   tree_t c = tree_new(T_CONSTRAINT);
27,402✔
2125
   tree_set_subkind(c, C_INDEX);
27,402✔
2126
   tree_add_range(c, r);
27,402✔
2127
   tree_set_loc(c, tree_loc(str));
27,402✔
2128

2129
   type_set_constraint(sub, c);
27,402✔
2130

2131
   return sub;
27,402✔
2132
}
2133

2134
tree_t change_ref(tree_t name, tree_t new)
2,590✔
2135
{
2136
   switch (tree_kind(name)) {
2,590✔
2137
   case T_REF:
1,736✔
2138
      return make_ref(new);
1,736✔
2139

2140
   case T_ARRAY_REF:
130✔
2141
      {
2142
         tree_t value = change_ref(tree_value(name), new);
130✔
2143

2144
         tree_t t = tree_new(T_ARRAY_REF);
130✔
2145
         tree_set_loc(t, tree_loc(name));
130✔
2146
         tree_set_value(t, value);
130✔
2147
         tree_set_type(t, type_elem(tree_type(value)));
130✔
2148

2149
         const int nparams = tree_params(name);
130✔
2150
         for (int i = 0; i < nparams; i++)
260✔
2151
            tree_add_param(t, tree_param(name, i));
130✔
2152

2153
         return t;
2154
      }
2155

2156
   case T_ARRAY_SLICE:
61✔
2157
      {
2158
         tree_t value = change_ref(tree_value(name), new);
61✔
2159
         tree_t r = tree_range(name, 0);
61✔
2160

2161
         tree_t constraint = tree_new(T_CONSTRAINT);
61✔
2162
         tree_set_subkind(constraint, C_INDEX);
61✔
2163
         tree_add_range(constraint, r);
61✔
2164

2165
         type_t slice_type = type_new(T_SUBTYPE);
61✔
2166
         type_set_constraint(slice_type, constraint);
61✔
2167
         type_set_base(slice_type, tree_type(value));
61✔
2168

2169
         tree_t t = tree_new(T_ARRAY_SLICE);
61✔
2170
         tree_set_loc(t, tree_loc(name));
61✔
2171
         tree_set_value(t, value);
61✔
2172
         tree_set_type(t, slice_type);
61✔
2173
         tree_add_range(t, r);
61✔
2174

2175
         return t;
61✔
2176
      }
2177

2178
   case T_RECORD_REF:
518✔
2179
      {
2180
         tree_t t = tree_new(T_RECORD_REF);
518✔
2181
         tree_set_loc(t, tree_loc(name));
518✔
2182
         tree_set_value(t, change_ref(tree_value(name), new));
518✔
2183
         tree_set_type(t, tree_type(name));
518✔
2184
         tree_set_ident(t, tree_ident(name));
518✔
2185
         tree_set_ref(t, tree_ref(name));
518✔
2186

2187
         return t;
518✔
2188
      }
2189

2190
   case T_CONV_FUNC:
133✔
2191
      {
2192
         tree_t t = tree_new(T_CONV_FUNC);
133✔
2193
         tree_set_loc(t, tree_loc(name));
133✔
2194
         tree_set_value(t, change_ref(tree_value(name), new));
133✔
2195
         tree_set_ident(t, tree_ident(name));
133✔
2196
         tree_set_type(t, tree_type(name));
133✔
2197
         tree_set_ref(t, tree_ref(name));
133✔
2198

2199
         return t;
133✔
2200
      }
2201

2202
   case T_TYPE_CONV:
12✔
2203
      {
2204
         tree_t t = tree_new(T_TYPE_CONV);
12✔
2205
         tree_set_loc(t, tree_loc(name));
12✔
2206
         tree_set_type(t, tree_type(name));
12✔
2207
         tree_set_value(t, change_ref(tree_value(name), new));
12✔
2208

2209
         return t;
12✔
2210
      }
2211

UNCOV
2212
   default:
×
2213
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
2214
                  tree_kind_str(tree_kind(name)));
2215
   }
2216
}
2217

2218
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
3,857✔
2219
{
2220
   switch (tree_kind(expr)) {
3,857✔
2221
   case T_ARRAY_SLICE:
96✔
2222
      build_wait(tree_range(expr, 0), fn, ctx);
96✔
2223
      break;
96✔
2224

2225
   case T_ARRAY_REF:
746✔
2226
      {
2227
         const int nparams = tree_params(expr);
746✔
2228
         for (int i = 0; i < nparams; i++)
1,492✔
2229
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
746✔
2230
      }
2231
      break;
2232

2233
   default:
2234
      break;
2235
   }
2236
}
3,857✔
2237

2238
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
17,923✔
2239
{
2240
   // LRM 08 section 10.2 has rules for building a wait statement from a
2241
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2242
   // all-sensitised processes.
2243

2244
   switch (tree_kind(expr)) {
22,282✔
2245
   case T_REF:
5,716✔
2246
      if (class_of(tree_ref(expr)) == C_SIGNAL)
5,716✔
2247
         (*fn)(expr, ctx);
3,286✔
2248
      break;
2249

2250
   case T_EXTERNAL_NAME:
9✔
2251
      if (tree_class(expr) == C_SIGNAL)
9✔
2252
         (*fn)(expr, ctx);
9✔
2253
      break;
2254

2255
   case T_WAVEFORM:
3,772✔
2256
   case T_QUALIFIED:
2257
   case T_TYPE_CONV:
2258
   case T_INERTIAL:
2259
      if (tree_has_value(expr))
3,772✔
2260
         build_wait(tree_value(expr), fn, ctx);
3,760✔
2261
      break;
2262

2263
   case T_ASSERT:
529✔
2264
      build_wait(tree_value(expr), fn, ctx);
529✔
2265
      // Fall-through
2266
   case T_REPORT:
534✔
2267
      if (tree_has_message(expr))
534✔
2268
         build_wait(tree_message(expr), fn, ctx);
371✔
2269
      break;
2270

2271
   case T_ARRAY_REF:
1,060✔
2272
   case T_ARRAY_SLICE:
2273
   case T_RECORD_REF:
2274
      {
2275
         tree_t ref = name_to_ref(expr);
1,060✔
2276
         if (ref != NULL && class_of(ref) == C_SIGNAL
1,060✔
2277
             && longest_static_prefix(expr) == expr)
672✔
2278
            (*fn)(expr, ctx);
573✔
2279
         else {
2280
            build_wait(tree_value(expr), fn, ctx);
487✔
2281
            build_wait_for_target(expr, fn, ctx);
487✔
2282
         }
2283
      }
2284
      break;
2285

2286
   case T_FCALL:
2,575✔
2287
   case T_PCALL:
2288
   case T_PROT_FCALL:
2289
   case T_PROT_PCALL:
2290
      {
2291
         tree_t decl = tree_ref(expr);
2,575✔
2292
         const int nparams = tree_params(expr);
2,575✔
2293
         for (int i = 0; i < nparams; i++) {
7,018✔
2294
            tree_t p = tree_param(expr, i), port;
4,443✔
2295
            switch (tree_subkind(p)) {
4,443✔
2296
            case P_POS:
4,443✔
2297
               port = tree_port(decl, tree_pos(p));
4,443✔
2298
               break;
4,443✔
UNCOV
2299
            case P_NAMED:
×
2300
               port = tree_ref(name_to_ref(tree_name(p)));
×
2301
               break;
×
2302
            default:
×
2303
               should_not_reach_here();
2304
            }
2305
            assert(tree_kind(port) == T_PARAM_DECL);
4,443✔
2306

2307
            switch (tree_subkind(port)) {
4,443✔
2308
            case PORT_IN:
4,434✔
2309
            case PORT_INOUT:
2310
            case PORT_ARRAY_VIEW:
2311
            case PORT_RECORD_VIEW:
2312
               build_wait(tree_value(p), fn, ctx);
4,434✔
2313
               break;
4,434✔
2314
            default:
2315
               break;
2316
            }
2317
         }
2318
      }
2319
      break;
2320

2321
   case T_AGGREGATE:
459✔
2322
      {
2323
         const int nassocs = tree_assocs(expr);
459✔
2324
         for (int i = 0; i < nassocs; i++) {
1,719✔
2325
            tree_t a = tree_assoc(expr, i);
1,260✔
2326
            build_wait(tree_value(a), fn, ctx);
1,260✔
2327

2328
            switch (tree_subkind(a)) {
1,260✔
2329
            case A_RANGE:
70✔
2330
            case A_SLICE:
2331
               build_wait(tree_range(a, 0), fn, ctx);
70✔
2332
               break;
70✔
2333
            case A_NAMED:
88✔
2334
               build_wait(tree_name(a), fn, ctx);
88✔
2335
               break;
88✔
2336
            }
2337
         }
2338
      }
2339
      break;
2340

2341
   case T_ATTR_REF:
448✔
2342
      {
2343
         const attr_kind_t predef = tree_subkind(expr);
448✔
2344
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
448✔
2345
            build_wait(tree_name(expr), fn, ctx);
198✔
2346

2347
         const int nparams = tree_params(expr);
448✔
2348
         for (int i = 0; i < nparams; i++)
470✔
2349
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
22✔
2350
      }
2351
      break;
2352

2353
   case T_LITERAL:
2354
   case T_STRING:
2355
   case T_DUMMY_DRIVER:
2356
      break;
2357

2358
   case T_IF:
298✔
2359
      {
2360
         const int nconds = tree_conds(expr);
298✔
2361
         for (int i = 0; i < nconds; i++)
822✔
2362
            build_wait(tree_cond(expr, i), fn, ctx);
524✔
2363
      }
2364
      break;
2365

2366
   case T_COND_STMT:
528✔
2367
      {
2368
         if (tree_has_value(expr))
528✔
2369
            build_wait(tree_value(expr), fn, ctx);
350✔
2370

2371
         const int nstmts = tree_stmts(expr);
528✔
2372
         for (int i = 0; i < nstmts; i++)
1,058✔
2373
            build_wait(tree_stmt(expr, i), fn, ctx);
530✔
2374
      }
2375
      break;
2376

2377
   case T_COND_VALUE:
4✔
2378
      {
2379
         const int nconds = tree_conds(expr);
4✔
2380
         for (int i = 0; i < nconds; i++)
16✔
2381
            build_wait(tree_cond(expr, i), fn, ctx);
12✔
2382
         break;
2383
      }
2384

2385
   case T_COND_EXPR:
12✔
2386
      {
2387
         if (tree_has_value(expr))
12✔
2388
            build_wait(tree_value(expr), fn, ctx);
8✔
2389

2390
         build_wait(tree_result(expr), fn, ctx);
12✔
2391
         break;
12✔
2392
      }
2393

2394
   case T_PROCESS:
64✔
2395
   case T_SEQUENCE:
2396
   case T_PROC_BODY:
2397
      {
2398
         const int ndecls = tree_decls(expr);
64✔
2399
         for (int i = 0; i < ndecls; i++) {
87✔
2400
            tree_t d = tree_decl(expr, i);
23✔
2401
            if (tree_kind(d) == T_PROC_BODY)
23✔
2402
               build_wait(d, fn, ctx);
2✔
2403
         }
2404

2405
         const int nstmts = tree_stmts(expr);
64✔
2406
         for (int i = 0; i < nstmts; i++)
147✔
2407
            build_wait(tree_stmt(expr, i), fn, ctx);
83✔
2408
      }
2409
      break;
2410

2411
   case T_SIGNAL_ASSIGN:
3,345✔
2412
      {
2413
         build_wait_for_target(tree_target(expr), fn, ctx);
3,345✔
2414

2415
         const int nwaves = tree_waveforms(expr);
3,345✔
2416
         for (int i = 0; i < nwaves; i++)
6,914✔
2417
            build_wait(tree_waveform(expr, i), fn, ctx);
3,569✔
2418
      }
2419
      break;
2420

2421
   case T_VAR_ASSIGN:
21✔
2422
   case T_FORCE:
2423
      build_wait_for_target(tree_target(expr), fn, ctx);
21✔
2424
      build_wait(tree_value(expr), fn, ctx);
21✔
2425
      break;
21✔
2426

2427
   case T_RELEASE:
4✔
2428
      build_wait_for_target(tree_target(expr), fn, ctx);
4✔
2429
      break;
4✔
2430

2431
   case T_CASE:
59✔
2432
   case T_MATCH_CASE:
2433
      {
2434
         build_wait(tree_value(expr), fn, ctx);
59✔
2435

2436
         const int nstmts = tree_stmts(expr);
59✔
2437
         for (int i = 0; i < nstmts; i++) {
365✔
2438
            tree_t alt = tree_stmt(expr, i);
306✔
2439

2440
            const int nstmts = tree_stmts(alt);
306✔
2441
            for (int j = 0; j < nstmts; j++)
612✔
2442
               build_wait(tree_stmt(alt, j), fn, ctx);
306✔
2443
         }
2444
      }
2445
      break;
2446

2447
   case T_FOR:
21✔
2448
      {
2449
         build_wait(tree_range(expr, 0), fn, ctx);
21✔
2450

2451
         const int nstmts = tree_stmts(expr);
21✔
2452
         for (int i = 0; i < nstmts; i++)
46✔
2453
            build_wait(tree_stmt(expr, i), fn, ctx);
25✔
2454
      }
2455
      break;
2456

2457
   case T_WHILE:
1✔
2458
      build_wait(tree_value(expr), fn, ctx);
1✔
2459
      // Fall-through
2460
   case T_LOOP:
5✔
2461
      {
2462
         const int nstmts = tree_stmts(expr);
5✔
2463
         for (int i = 0; i < nstmts; i++)
26✔
2464
            build_wait(tree_stmt(expr, i), fn, ctx);
21✔
2465
      }
2466
      break;
2467

2468
   case T_NEXT:
12✔
2469
   case T_EXIT:
2470
      if (tree_has_value(expr))
12✔
2471
         build_wait(tree_value(expr), fn, ctx);
8✔
2472
      break;
2473

2474
   case T_RANGE:
187✔
2475
      if (tree_subkind(expr) == RANGE_EXPR)
187✔
2476
         build_wait(tree_value(expr), fn, ctx);
14✔
2477
      else {
2478
         build_wait(tree_left(expr), fn, ctx);
173✔
2479
         build_wait(tree_right(expr), fn, ctx);
173✔
2480
      }
2481
      break;
2482

2483
   case T_OPEN:
2484
      break;
2485

UNCOV
2486
   default:
×
2487
      fatal_trace("Cannot handle tree kind %s in wait expression",
2488
                  tree_kind_str(tree_kind(expr)));
2489
   }
2490
}
17,923✔
2491

2492
void print_syntax(const char *fmt, ...)
2,011✔
2493
{
2494
   LOCAL_TEXT_BUF tb = tb_new();
2,011✔
2495
   bool highlighting = false;
2,011✔
2496
   static bool comment = false, last_was_newline = false;
2,011✔
2497
   for (const char *p = fmt; *p != '\0'; p++) {
10,602✔
2498
      if (comment) {
8,591✔
2499
         if (*p == '\n' || *p == '\r') {
3,166✔
2500
            comment = false;
94✔
2501
            last_was_newline = true;
94✔
2502
            tb_printf(tb, "$$\n");
94✔
2503
         }
2504
         else if (*p != '~' && *p != '#') {
3,072✔
2505
            tb_append(tb, *p);
2,912✔
2506
            last_was_newline = false;
2,912✔
2507
         }
2508
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
3,166✔
2509
            tb_printf(tb, "$$");
10✔
2510
            comment = false;
10✔
2511
            last_was_newline = false;
10✔
2512
         }
2513
      }
2514
      else if (*p == '\r') {
5,425✔
2515
         if (!last_was_newline) {
21✔
UNCOV
2516
            tb_append(tb, '\n');
×
2517
            last_was_newline = true;
×
2518
         }
2519
      }
2520
      else if (*p == '#' && *(p + 1) != '#') {
5,404✔
2521
         tb_printf(tb, "$bold$$cyan$");
363✔
2522
         last_was_newline = false;
363✔
2523
         highlighting = true;
363✔
2524
      }
2525
      else if (*p == '~' && *(p + 1) != '~') {
5,041✔
2526
         tb_printf(tb, "$yellow$");
1✔
2527
         last_was_newline = false;
1✔
2528
         highlighting = true;
1✔
2529
      }
2530
      else if ((*p == '-' && *(p + 1) == '-')
5,040✔
2531
               || (*p == '/' && *(p + 1) == '/')
4,950✔
2532
               || (*p == '/' && *(p + 1) == '*')) {
4,946✔
2533
         tb_printf(tb, "$red$%c", *p);
104✔
2534
         last_was_newline = false;
104✔
2535
         comment = true;
104✔
2536
      }
2537
      else if (!isalnum_iso88591(*p) && *p != '_'
4,936✔
2538
               && *p != '%' && highlighting) {
2,518✔
2539
         tb_printf(tb, "$$%c", *p);
312✔
2540
         last_was_newline = false;
312✔
2541
         highlighting = false;
312✔
2542
      }
2543
      else {
2544
         tb_append(tb, *p);
4,624✔
2545
         last_was_newline = (*p == '\n');
4,624✔
2546
      }
2547
   }
2548

2549
   if (highlighting)
2,011✔
2550
      tb_cat(tb, "$$");
52✔
2551

2552
   va_list ap;
2,011✔
2553
   va_start(ap, fmt);
2,011✔
2554

2555
   if (syntax_buf != NULL) {
2,011✔
2556
      char *stripped LOCAL = strip_color(tb_get(tb), ap);
4,022✔
2557
      tb_cat(syntax_buf, stripped);
2,011✔
2558
   }
2559
   else
UNCOV
2560
      color_vprintf(tb_get(tb), ap);
×
2561

2562
   va_end(ap);
2,011✔
2563
}
2,011✔
2564

2565
void capture_syntax(text_buf_t *tb)
9✔
2566
{
2567
   assert(tb == NULL || syntax_buf == NULL);
9✔
2568
   syntax_buf = tb;
9✔
2569
}
9✔
2570

2571
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur,
4,843✔
2572
                  mir_context_t *mc)
2573
{
2574
   input_from_file(file);
4,843✔
2575

2576
   switch (source_kind()) {
4,843✔
2577
   case SOURCE_VHDL:
4,697✔
2578
      {
2579
         lib_t work = lib_work();
4,697✔
2580
         int base_errors = 0;
4,697✔
2581
         tree_t unit;
4,697✔
2582
         while (base_errors = error_count(), (unit = parse())) {
18,230✔
2583
            if (error_count() == base_errors) {
13,533✔
2584
               lib_put(work, unit);
13,533✔
2585
               unit_registry_purge(ur, tree_ident(unit));
13,533✔
2586

2587
               simplify_local(unit, jit, ur, mc);
13,533✔
2588
               bounds_check(unit);
13,533✔
2589
            }
2590
            else
UNCOV
2591
               lib_put_error(work, unit);
×
2592
         }
2593
      }
2594
      break;
2595

2596
   case SOURCE_VERILOG:
146✔
2597
      {
2598
         LOCAL_TEXT_BUF tb = tb_new();
292✔
2599
         vlog_preprocess(tb, true);
146✔
2600

2601
         input_from_buffer(tb_get(tb), tb_len(tb), SOURCE_VERILOG);
146✔
2602

2603
         lib_t work = lib_work();
146✔
2604
         vlog_node_t module;
146✔
2605
         while ((module = vlog_parse())) {
463✔
2606
            if (error_count() == 0) {
171✔
2607
               vlog_check(module);
171✔
2608

2609
               if (error_count() == 0) {
171✔
2610
                  vlog_simp(module);
171✔
2611
                  lib_put_vlog(work, module);
171✔
2612
               }
2613
            }
2614
         }
2615
      }
2616
      break;
146✔
2617

UNCOV
2618
   case SOURCE_SDF:
×
2619
      {
UNCOV
2620
         sdf_file_t *sdf_file = sdf_parse(file, 0);
×
2621
         progress("analysed SDF file: %s", file);
×
2622

UNCOV
2623
         if (sdf_file != NULL) {
×
2624
            warnf("SDF is not yet supported");
×
2625
            sdf_file_free(sdf_file);
×
2626
         }
2627
      }
2628
      break;
2629
   }
2630
}
4,839✔
2631

2632
bool all_character_literals(type_t type)
18,494✔
2633
{
2634
   assert(type_is_enum(type));
18,494✔
2635

2636
   type_t base = type_base_recur(type);
18,494✔
2637
   const int nlits = type_enum_literals(base);
18,494✔
2638
   for (int i = 0; i < nlits; i++) {
88,542✔
2639
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
77,331✔
2640
         return false;
2641
   }
2642

2643
   return true;
2644
}
2645

2646
bool is_operator_symbol(ident_t ident)
30,107✔
2647
{
2648
   const int len = ident_len(ident);
30,107✔
2649
   if (len < 3)
30,107✔
2650
      return false;
2651
   else if (ident_char(ident, 0) != '"')
29,790✔
2652
      return false;
2653
   else if (ident_char(ident, len - 1) != '"')
16,390✔
2654
      return false;
2655

2656
   const well_known_t wk = is_well_known(ident);
16,390✔
2657

2658
   if (standard() < STD_08)
16,390✔
2659
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,304✔
2660
   else
2661
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
13,086✔
2662
}
2663

2664
bool same_tree(tree_t a, tree_t b)
9,909✔
2665
{
2666
   const tree_kind_t akind = tree_kind(a);
9,909✔
2667
   if (akind != tree_kind(b))
9,909✔
2668
      return false;
2669

2670
   switch (akind) {
9,753✔
2671
   case T_REF:
4,678✔
2672
      return tree_ref(a) == tree_ref(b);
4,678✔
2673
   case T_ARRAY_REF:
1,529✔
2674
      {
2675
         if (!same_tree(tree_value(a), tree_value(b)))
1,529✔
2676
            return false;
2677

2678
         const int nparams = tree_params(a);
1,500✔
2679
         assert(nparams == tree_params(b));
1,500✔
2680

2681
         for (int i = 0; i < nparams; i++) {
2,785✔
2682
            tree_t pa = tree_value(tree_param(a, i));
2,400✔
2683
            tree_t pb = tree_value(tree_param(b, i));
2,400✔
2684
            if (!same_tree(pa, pb))
2,400✔
2685
               return false;
2686
         }
2687

2688
         return true;
2689
      }
2690
   case T_ARRAY_SLICE:
47✔
2691
      {
2692
         if (!same_tree(tree_value(a), tree_value(b)))
47✔
2693
            return false;
2694

2695
         tree_t ra = tree_range(a, 0);
47✔
2696
         tree_t rb = tree_range(b, 0);
47✔
2697

2698
         const range_kind_t rakind = tree_subkind(ra);
47✔
2699
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
47✔
2700
            return false;
2701

2702
         return same_tree(tree_left(ra), tree_left(rb))
47✔
2703
            && same_tree(tree_right(ra), tree_right(rb));
47✔
2704
      }
2705

2706
   case T_RECORD_REF:
1,007✔
2707
      return ident_casecmp(tree_ident(a), tree_ident(b))
1,007✔
2708
         && same_tree(tree_value(a), tree_value(b));
1,011✔
2709

2710
   case T_LITERAL:
2,492✔
2711
      {
2712
         const literal_kind_t alkind = tree_subkind(a);
2,492✔
2713
         if (alkind != tree_subkind(b) || alkind != L_INT)
2,492✔
2714
            return false;
2715
         else
2716
            return tree_ival(a) == tree_ival(b);
2,436✔
2717
      }
2718
   default:
2719
      return false;
2720
   }
2721
}
2722

2723
bool calculate_aggregate_bounds(tree_t expr, range_kind_t *kind,
8,396✔
2724
                                int64_t *left, int64_t *right)
2725
{
2726
   // Calculate the direction and bounds of an array aggregate using the
2727
   // rules in LRM 93 7.3.2.2
2728

2729
   type_t type = tree_type(expr);
8,396✔
2730
   type_t index_type = index_type_of(type, 0);
8,396✔
2731
   if (index_type == NULL || type_is_none(index_type))
8,396✔
2732
      return false;
1✔
2733

2734
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
8,395✔
2735

2736
   int64_t low, high;
8,395✔
2737
   if (!folded_bounds(index_r, &low, &high))
8,395✔
2738
      return false;
2739

2740
   int64_t clow = INT64_MAX, chigh = INT64_MIN;  // Actual bounds computed below
8,375✔
2741

2742
   range_kind_t dir;
8,375✔
2743
   if (type_is_unconstrained(type))
8,375✔
2744
      dir = tree_subkind(index_r);
8,062✔
2745
   else {
2746
      base_r = range_of(type, 0);
313✔
2747
      dir = tree_subkind(base_r);
313✔
2748
   }
2749

2750
   const int nassocs = tree_assocs(expr);
8,375✔
2751

2752
   if (standard() >= STD_08) {
8,375✔
2753
      // VHDL-2008 range association determines index direction for
2754
      // unconstrained aggregate when the expression type matches the
2755
      // array type
2756
      for (int i = 0; i < nassocs; i++) {
16,540✔
2757
         tree_t a = tree_assoc(expr, i);
11,905✔
2758
         if (tree_subkind(a) == A_SLICE)
11,905✔
2759
            dir = tree_subkind(tree_range(a, 0));
71✔
2760
      }
2761
   }
2762

2763
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
8,375✔
2764
      return false;
2765

2766
   int64_t pos = 0;
2767
   for (int i = 0; i < nassocs; i++) {
24,011✔
2768
      tree_t a = tree_assoc(expr, i);
21,210✔
2769
      int64_t ilow = 0, ihigh = 0;
21,210✔
2770
      const assoc_kind_t akind = tree_subkind(a);
21,210✔
2771

2772
      switch (akind) {
21,210✔
2773
      case A_NAMED:
708✔
2774
         {
2775
            tree_t name = tree_name(a);
708✔
2776
            if (folded_int(name, &ilow))
708✔
2777
               ihigh = ilow;
695✔
2778
            else
2779
               return false;
5,530✔
2780
         }
2781
         break;
695✔
2782

2783
      case A_RANGE:
1,375✔
2784
      case A_SLICE:
2785
         {
2786
            tree_t r = tree_range(a, 0);
1,375✔
2787
            const range_kind_t rkind = tree_subkind(r);
1,375✔
2788
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,375✔
2789
               tree_t left = tree_left(r), right = tree_right(r);
1,031✔
2790

2791
               int64_t ileft, iright;
1,031✔
2792
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
1,031✔
2793
                  ilow = (rkind == RANGE_TO ? ileft : iright);
539✔
2794
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
539✔
2795
               }
2796
               else
2797
                  return false;
492✔
2798
            }
2799
            else
2800
               return false;
2801
         }
2802
         break;
2803

2804
      case A_OTHERS:
2805
         return false;
2806

2807
      case A_POS:
12,300✔
2808
         if (i == 0) {
12,300✔
2809
            int64_t ileft;
1,920✔
2810
            if (folded_int(tree_left(base_r), &ileft))
1,920✔
2811
               ilow = ihigh = ileft;
1,920✔
2812
            else
UNCOV
2813
               return false;
×
2814
         }
2815
         else if (dir == RANGE_TO)
10,380✔
2816
            ilow = ihigh = clow + pos;
10,378✔
2817
         else
2818
            ilow = ihigh = chigh - pos;
2✔
2819
         pos++;
12,300✔
2820
         break;
12,300✔
2821

2822
      case A_CONCAT:
6,825✔
2823
         {
2824
            type_t value_type = tree_type(tree_value(a));
6,825✔
2825

2826
            int64_t length;
6,825✔
2827
            if (type_is_unconstrained(value_type))
6,825✔
2828
               return false;
4,679✔
2829
            else if (folded_length(range_of(value_type, 0), &length)) {
2,521✔
2830
               if (i == 0) {
2,146✔
2831
                  int64_t ileft;
1,879✔
2832
                  if (folded_int(tree_left(base_r), &ileft))
1,879✔
2833
                     ilow = ihigh = ileft;
1,879✔
2834
                  else
UNCOV
2835
                     return false;
×
2836
               }
2837
               else if (dir == RANGE_TO) {
267✔
2838
                  ilow = clow + pos;
243✔
2839
                  ihigh = ilow + length - 1;
243✔
2840
               }
2841
               else {
2842
                  ihigh = chigh - pos;
24✔
2843
                  ilow = ihigh - length + 1;
24✔
2844
               }
2845
               pos += length;
2,146✔
2846
            }
2847
            else
2848
               return false;
2849
         }
2850
         break;
2851
      }
2852

2853
      clow = MIN(clow, ilow);
15,680✔
2854
      chigh = MAX(chigh, ihigh);
15,680✔
2855
   }
2856

2857
   if (clow < low || chigh > high)
2,801✔
2858
      return false;   // Will raise a bounds check error later
2859

2860
   *kind = dir;
2,796✔
2861
   *left = dir == RANGE_TO ? clow : chigh;
2,796✔
2862
   *right = dir == RANGE_TO ? chigh : clow;
2,796✔
2863

2864
   return true;
2,796✔
2865
}
2866

2867
type_t calculate_aggregate_subtype(tree_t expr)
6,937✔
2868
{
2869
   range_kind_t dir;
6,937✔
2870
   int64_t ileft, iright;
6,937✔
2871
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
6,937✔
2872
      return NULL;
2873

2874
   type_t type = tree_type(expr);
2,773✔
2875

2876
   const int ndims = dimension_of(type);
2,773✔
2877
   type_t a0_type = NULL;
2,773✔
2878
   if (ndims > 1) {
2,773✔
2879
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
81✔
2880
      if (type_is_unconstrained(a0_type))
81✔
2881
         return NULL;
2882

2883
      assert(dimension_of(a0_type) == ndims - 1);
81✔
2884
   }
2885

2886
   type_t index_type = index_type_of(type, 0);
2,773✔
2887

2888
   tree_t left = get_discrete_lit(expr, index_type, ileft);
2,773✔
2889
   tree_t right = get_discrete_lit(expr, index_type, iright);
2,773✔
2890
   assert(left != NULL && right != NULL);
2,773✔
2891

2892
   type_t sub = type_new(T_SUBTYPE);
2,773✔
2893
   type_set_base(sub, type_base_recur(type));
2,773✔
2894

2895
   type_t elem = type_elem(type);
2,773✔
2896
   if (type_is_unconstrained(elem)) {
2,773✔
2897
      tree_t a0 = tree_assoc(expr, 0);
124✔
2898
      switch (tree_subkind(a0)) {
124✔
2899
      case A_CONCAT:
7✔
2900
      case A_SLICE:
2901
         a0_type = type_elem(tree_type(tree_value(a0)));
7✔
2902
         break;
7✔
2903
      default:
117✔
2904
         a0_type = tree_type(tree_value(a0));
117✔
2905
         break;
117✔
2906
      }
2907

2908
      if (!type_is_unconstrained(a0_type))
124✔
2909
         elem = a0_type;
68✔
2910
   }
2911

2912
   type_set_elem(sub, elem);
2,773✔
2913

2914
   tree_t cons = tree_new(T_CONSTRAINT);
2,773✔
2915
   tree_set_subkind(cons, C_INDEX);
2,773✔
2916

2917
   tree_t r = tree_new(T_RANGE);
2,773✔
2918
   tree_set_subkind(r, dir);
2,773✔
2919
   tree_set_type(r, index_type);
2,773✔
2920
   tree_set_left(r, left);
2,773✔
2921
   tree_set_right(r, right);
2,773✔
2922

2923
   tree_add_range(cons, r);
2,773✔
2924

2925
   for (int i = 1; i < ndims; i++)
2,854✔
2926
      tree_add_range(cons, range_of(a0_type, i - 1));
81✔
2927

2928
   type_set_constraint(sub, cons);
2,773✔
2929

2930
   return sub;
2,773✔
2931
}
2932

2933
bool can_be_signal(type_t type)
20,527✔
2934
{
2935
   switch (type_kind(type)) {
36,307✔
2936
   case T_RECORD:
4,189✔
2937
      {
2938
         const int nfields = type_fields(type);
4,189✔
2939
         for (int i = 0; i < nfields; i++) {
16,726✔
2940
            if (!can_be_signal(tree_type(type_field(type, i))))
13,574✔
2941
               return false;
2942
         }
2943

2944
         return true;
2945
      }
2946
   case T_ARRAY:
6,047✔
2947
      return can_be_signal(type_elem(type));
6,047✔
2948
   case T_SUBTYPE:
9,733✔
2949
      return can_be_signal(type_base(type));
9,733✔
2950
   case T_ACCESS:
2951
   case T_FILE:
2952
   case T_PROTECTED:
2953
   case T_INCOMPLETE:
2954
      return false;
2955
   default:
11,862✔
2956
      return true;
11,862✔
2957
   }
2958
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc