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

nickg / nvc / 13472068665

22 Feb 2025 10:27AM UTC coverage: 92.151% (+0.01%) from 92.14%
13472068665

push

github

nickg
Also show bug report message from fatal_trace

0 of 7 new or added lines in 1 file covered. (0.0%)

571 existing lines in 6 files now uncovered.

64327 of 69806 relevant lines covered (92.15%)

501790.75 hits per line

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

94.81
/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)
379,049✔
46
{
47
   int64_t value;
379,049✔
48
   if (folded_int(t, &value))
379,049✔
49
      return value;
379,049✔
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)
28,929✔
56
{
57
   assert(tree_kind(r) == T_RANGE);
28,929✔
58

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

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

66
bool folded_int(tree_t t, int64_t *l)
3,637,345✔
67
{
68
   switch (tree_kind(t)) {
3,670,883✔
69
   case T_LITERAL:
2,651,489✔
70
      switch (tree_subkind(t)) {
2,651,489✔
71
      case L_PHYSICAL:
16,630✔
72
         if (tree_has_ref(t))
16,630✔
73
            return false;
74
         // Fall-through
75
      case L_INT:
76
         *l = tree_ival(t);
2,604,877✔
77
         return true;
2,604,877✔
78
      default:
79
         return false;
80
      }
81
   case T_QUALIFIED:
362✔
82
      return folded_int(tree_value(t), l);
362✔
83
   case T_REF:
804,295✔
84
      if (tree_has_ref(t)) {
804,295✔
85
         tree_t decl = tree_ref(t);
804,294✔
86
         switch (tree_kind(decl)) {
804,294✔
87
         case T_CONST_DECL:
40,838✔
88
            if (tree_has_value(decl))
40,838✔
89
               return folded_int(tree_value(decl), l);
32,837✔
90
            else
91
               return false;
92
         case T_ENUM_LIT:
692,815✔
93
            *l = tree_pos(decl);
692,815✔
94
            return true;
692,815✔
95
         case T_ALIAS:
339✔
96
            return folded_int(tree_value(decl), l);
339✔
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)
195,646✔
108
{
109
   switch (tree_kind(t)) {
195,649✔
110
   case T_LITERAL:
187,378✔
111
      if (tree_subkind(t) == L_REAL) {
187,378✔
112
         *l = tree_dval(t);
187,307✔
113
         return true;
187,307✔
114
      }
115
      else
116
         return false;
117
   case T_QUALIFIED:
3✔
118
      return folded_real(tree_value(t), l);
3✔
119
   default:
120
      return false;
121
   }
122
}
123

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

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

139
   const range_kind_t rkind = tree_subkind(r);
1,501,238✔
140

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

144
   int64_t left, right;
1,488,688✔
145
   if (!folded_int(tree_left(r), &left))
1,488,688✔
146
      return false;
147
   else if (!folded_int(tree_right(r), &right))
1,376,759✔
148
      return false;
149

150
   switch (rkind) {
1,340,482✔
151
   case RANGE_TO:
1,216,759✔
152
      *low  = left;
1,216,759✔
153
      *high = right;
1,216,759✔
154
      return true;
1,216,759✔
155
   case RANGE_DOWNTO:
123,723✔
156
      *low  = right;
123,723✔
157
      *high = left;
123,723✔
158
      return true;
123,723✔
159
   default:
160
      return false;
161
   }
162
}
163

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

168
   const range_kind_t rkind = tree_subkind(r);
78,110✔
169

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

173
   double left, right;
78,107✔
174
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
78,107✔
175
      switch (rkind) {
78,022✔
176
      case RANGE_TO:
78,022✔
177
         *low  = left;
78,022✔
178
         *high = right;
78,022✔
179
         return true;
78,022✔
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;
85✔
190
}
191

192
bool folded_bool(tree_t t, bool *b)
40,976✔
193
{
194
   if (tree_kind(t) == T_REF) {
40,976✔
195
      tree_t decl = tree_ref(t);
10,759✔
196
      if (tree_kind(decl) == T_ENUM_LIT
10,759✔
197
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
8,347✔
198
         *b = (tree_pos(decl) == 1);
8,347✔
199
         return true;
8,347✔
200
      }
201
   }
202

203
   return false;
204
}
205

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

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

217
   return b;
7,475✔
218
}
219

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

228
   return f;
41,701✔
229
}
230

231
tree_t get_discrete_lit(tree_t t, type_t type, int64_t i)
3,458✔
232
{
233
   if (type_is_enum(type)) {
3,458✔
234
      type_t base = type_base_recur(type);
68✔
235
      const int maxlit = type_enum_literals(base);
68✔
236
      if (i >= maxlit)
68✔
237
         return NULL;
238
      else
239
         return get_enum_lit(t, type, i);
68✔
240
   }
241
   else
242
      return get_int_lit(t, type, i);
3,390✔
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)
92✔
257
{
258
   type_t base = type_base_recur(type);
92✔
259
   const type_kind_t basek = type_kind(base);
92✔
260

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

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

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

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

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

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

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

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

290
      assert(n <= max);
17✔
291
      array->count = n;
17✔
292

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

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

305
      value->enums = array;
15✔
306
      return true;
15✔
307
   }
308

309
   while (isspace_iso88591(*str))
85✔
310
      ++str;
10✔
311

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

318
         if (is_negative) ++str;
50✔
319

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

329
         value->integer = is_negative ? -sum : sum;
50✔
330

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

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

348
         ident_t id = ident_new(copy);
14✔
349

350
         value->integer = -1;
14✔
351

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

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

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

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

379
         while (isspace_iso88591(*str)) ++str;
10✔
380

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

386
         if (p == copy)
5✔
387
            return false;
388

389
         ident_t id = ident_new(copy);
4✔
390

391
         value->integer = -1;
4✔
392

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

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

407
   default:
408
      return false;
409
   }
410

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

416
   return true;
417
}
418

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

428
vhdl_standard_t standard(void)
2,690,387✔
429
{
430
   return current_std;
2,690,387✔
431
}
432

433
void set_standard(vhdl_standard_t s)
4,964✔
434
{
435
   current_std = s;
4,964✔
436
   have_set_std = true;
4,964✔
437
}
4,964✔
438

439
void set_default_standard(vhdl_standard_t s)
456✔
440
{
441
   if (!have_set_std)
456✔
442
      set_standard(s);
88✔
443
}
456✔
444

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

451
   if ((unsigned)s < ARRAY_LEN(text))
4,510✔
452
      return text[s];
4,510✔
453
   else
454
      return "????";
455
}
456

457
tree_t find_element_mode_indication(tree_t view, tree_t field, bool *converse)
397✔
458
{
459
   switch (tree_kind(view)) {
1,255✔
460
   case T_REF:
541✔
461
      return find_element_mode_indication(tree_ref(view), field, converse);
541✔
462

463
   case T_ALIAS:
144✔
464
      return find_element_mode_indication(tree_value(view), field, converse);
144✔
465

466
   case T_ATTR_REF:
173✔
467
      assert(tree_subkind(view) == ATTR_CONVERSE);
173✔
468
      *converse = !*converse;
173✔
469
      return find_element_mode_indication(tree_name(view), field, converse);
173✔
470

471
   case T_VIEW_DECL:
397✔
472
      {
473
         type_t view_type = tree_type(view);
397✔
474
         assert(type_kind(view_type) == T_VIEW);
397✔
475

476
         const int nelems = type_fields(view_type);
397✔
477
         for (int i = 0; i < nelems; i++) {
673✔
478
            tree_t e = type_field(view_type, i);
673✔
479
            if (tree_ref(e) == field)
673✔
480
               return e;
397✔
481
         }
482

483
         return NULL;
484
      }
485

UNCOV
486
   default:
×
487
      fatal_trace("unhandled tree kind %s in find_element_mode_indication",
488
                  tree_kind_str(tree_kind(view)));
489
   }
490
}
491

492
port_mode_t converse_mode(tree_t port, bool converse)
265✔
493
{
494
   const port_mode_t mode = tree_subkind(port);
265✔
495
   switch (mode) {
265✔
496
   case PORT_IN: return converse ? PORT_OUT : PORT_IN;
135✔
497
   case PORT_OUT: return converse ? PORT_IN : PORT_OUT;
121✔
498
   default: return mode;
499
   }
500
}
501

502
class_t class_of(tree_t t)
1,018,042✔
503
{
504
   switch (tree_kind(t)) {
1,071,444✔
505
   case T_VAR_DECL:
506
      return C_VARIABLE;
507
   case T_SIGNAL_DECL:
45,695✔
508
   case T_IMPLICIT_SIGNAL:
509
      return C_SIGNAL;
45,695✔
510
   case T_CONST_DECL:
36,589✔
511
      return C_CONSTANT;
36,589✔
512
   case T_PORT_DECL:
152,389✔
513
   case T_GENERIC_DECL:
514
   case T_PARAM_DECL:
515
   case T_EXTERNAL_NAME:
516
      return tree_class(t);
152,389✔
517
   case T_ENUM_LIT:
589,342✔
518
   case T_LITERAL:
519
   case T_STRING:
520
      return C_LITERAL;
589,342✔
521
   case T_FIELD_DECL:
1,055✔
522
   case T_ATTR_DECL:
523
      return C_DEFAULT;
1,055✔
524
   case T_VIEW_DECL:
126✔
525
      return C_VIEW;
126✔
526
   case T_UNIT_DECL:
7,519✔
527
      return C_UNITS;
7,519✔
528
   case T_ARCH:
47✔
529
      return C_ARCHITECTURE;
47✔
530
   case T_FUNC_DECL:
5,401✔
531
   case T_FUNC_BODY:
532
   case T_FUNC_INST:
533
   case T_FCALL:
534
   case T_PROT_FCALL:
535
      return C_FUNCTION;
5,401✔
536
   case T_PROC_DECL:
10,261✔
537
   case T_PROC_BODY:
538
   case T_PROC_INST:
539
   case T_PCALL:
540
   case T_PROT_PCALL:
541
      return C_PROCEDURE;
10,261✔
542
   case T_ENTITY:
136✔
543
      return C_ENTITY;
136✔
544
   case T_SUBTYPE_DECL:
18,751✔
545
      return C_SUBTYPE;
18,751✔
546
   case T_TYPE_DECL:
71,804✔
547
   case T_PROT_DECL:
548
   case T_PROT_BODY:
549
      return C_TYPE;
71,804✔
550
   case T_FILE_DECL:
1,101✔
551
      return C_FILE;
1,101✔
552
   case T_PROCESS:
138✔
553
   case T_BLOCK:
554
   case T_FOR:
555
   case T_INSTANCE:
556
   case T_CONCURRENT:
557
   case T_ELAB:
558
   case T_PSL_DECL:
559
   case T_PSL_DIRECT:
560
   case T_FOR_GENERATE:
561
   case T_IF_GENERATE:
562
   case T_CASE_GENERATE:
563
      return C_LABEL;
138✔
564
   case T_COMPONENT:
1✔
565
      return C_COMPONENT;
1✔
566
   case T_REF:
44,685✔
567
   case T_PROT_REF:
568
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
44,685✔
569
   case T_ARRAY_REF:
8,721✔
570
   case T_ARRAY_SLICE:
571
   case T_RECORD_REF:
572
   case T_ALL:
573
   case T_ALIAS:
574
   case T_QUALIFIED:
575
      return class_of(tree_value(t));
8,721✔
576
   case T_PACKAGE:
1,227✔
577
   case T_PACK_BODY:
578
   case T_PACK_INST:
579
      return C_PACKAGE;
1,227✔
580
   case T_CONFIGURATION:
1✔
581
      return C_CONFIGURATION;
1✔
582
   case T_LIBRARY:
1✔
583
      return C_LIBRARY;
1✔
584
   case T_ATTR_REF:
86✔
585
      switch (tree_subkind(t)) {
86✔
586
      case ATTR_DELAYED:
587
      case ATTR_STABLE:
588
      case ATTR_QUIET:
589
      case ATTR_TRANSACTION:
590
         return C_SIGNAL;
591
      default:
80✔
592
         return C_DEFAULT;
80✔
593
      }
UNCOV
594
   case T_CONTEXT:
×
UNCOV
595
      return C_CONTEXT;
×
UNCOV
596
   default:
×
597
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
598
   }
599
}
600

601
bool class_has_type(class_t c)
862,106✔
602
{
603
   switch (c) {
862,106✔
604
   case C_LABEL:
605
   case C_ENTITY:
606
   case C_ARCHITECTURE:
607
   case C_COMPONENT:
608
   case C_CONFIGURATION:
609
   case C_PACKAGE:
610
   case C_LIBRARY:
611
      return false;
612
   default:
860,828✔
613
      return true;
860,828✔
614
   }
615
}
616

617
const char *class_str(class_t c)
445✔
618
{
619
   static const char *strs[] = {
445✔
620
      "default", "signal", "variable", "constant", "file", "entity",
621
      "component", "configuration", "architecture", "function", "package",
622
      "type", "subtype", "label", "procedure", "literal", "units", "library",
623
      "context", "view",
624
   };
625
   assert(c < ARRAY_LEN(strs));
445✔
626
   return strs[c];
445✔
627
}
628

629
const char *assoc_kind_str(assoc_kind_t akind)
9✔
630
{
631
   switch (akind) {
9✔
632
   case A_NAMED:  return "named";
633
   case A_CONCAT:
2✔
634
   case A_POS:    return "positional";
2✔
635
   case A_OTHERS: return "others";
3✔
636
   case A_SLICE:
1✔
637
   case A_RANGE:  return "range";
1✔
UNCOV
638
   default:       return "??";
×
639
   }
640
}
641

642
bool is_subprogram(tree_t t)
794,296✔
643
{
644
   switch (tree_kind(t)) {
794,296✔
645
   case T_FUNC_DECL:
646
   case T_FUNC_BODY:
647
   case T_FUNC_INST:
648
   case T_PROC_DECL:
649
   case T_PROC_BODY:
650
   case T_PROC_INST:
651
      return true;
652
   case T_GENERIC_DECL:
2,861✔
653
      {
654
         const class_t class = tree_class(t);
2,861✔
655
         return class == C_FUNCTION || class == C_PROCEDURE;
2,861✔
656
      }
657
   default:
624,558✔
658
      return false;
624,558✔
659
   }
660
}
661

662
bool is_loop_stmt(tree_t t)
421✔
663
{
664
   const tree_kind_t kind = tree_kind(t);
421✔
665
   return kind == T_WHILE || kind == T_FOR || kind == T_LOOP;
421✔
666
}
667

668
bool is_container(tree_t t)
10,393✔
669
{
670
   switch (tree_kind(t)) {
10,393✔
671
   case T_FUNC_BODY:
672
   case T_PROC_BODY:
673
   case T_ENTITY:
674
   case T_ARCH:
675
   case T_PACKAGE:
676
   case T_PACK_BODY:
677
   case T_CONFIGURATION:
678
   case T_BLOCK:
679
   case T_PROT_BODY:
680
   case T_ELAB:
681
   case T_FOR:
682
   case T_PROCESS:
683
   case T_PACK_INST:
684
      return true;
685
   default:
8,094✔
686
      return false;
8,094✔
687
   }
688
}
689

690
bool is_concurrent_block(tree_t t)
1,117✔
691
{
692
   switch (tree_kind(t)) {
1,117✔
693
   case T_ARCH:
694
   case T_ENTITY:
695
   case T_BLOCK:
696
   case T_IF_GENERATE:
697
   case T_FOR_GENERATE:
698
      return true;
699
   default:
370✔
700
      return false;
370✔
701
   }
702
}
703

704
bool is_package(tree_t t)
45,506✔
705
{
706
   switch (tree_kind(t)) {
45,506✔
707
   case T_PACKAGE:
708
   case T_PACK_BODY:
709
   case T_PACK_INST:
710
      return true;
711
   default:
929✔
712
      return false;
929✔
713
   }
714
}
715

716
bool is_design_unit(tree_t t)
38,353✔
717
{
718
   switch (tree_kind(t)) {
38,353✔
719
   case T_ENTITY:
720
   case T_ARCH:
721
   case T_PACKAGE:
722
   case T_PACK_BODY:
723
   case T_CONFIGURATION:
724
   case T_CONTEXT:
725
   case T_PACK_INST:
726
      return true;
727
   default:
8,608✔
728
      return false;
8,608✔
729
   }
730
}
731

732
bool is_literal(tree_t t)
31,513✔
733
{
734
   switch (tree_kind(t)) {
31,513✔
735
   case T_REF:
3,104✔
736
      return tree_has_ref(t) && tree_kind(tree_ref(t)) == T_ENUM_LIT;
4,307✔
737
   case T_LITERAL:
738
      return true;
739
   case T_STRING:
22,538✔
740
   default:
741
      return false;
22,538✔
742
   }
743
}
744

745
bool is_body(tree_t t)
6,997✔
746
{
747
   switch (tree_kind(t)) {
6,997✔
748
   case T_FUNC_BODY:
749
   case T_PROC_BODY:
750
   case T_PACK_BODY:
751
   case T_PROT_BODY:
752
      return true;
753
   default:
43✔
754
      return false;
43✔
755
   }
756
}
757

758
bool is_guarded_signal(tree_t decl)
13,820✔
759
{
760
   switch (tree_kind(decl)) {
13,820✔
761
   case T_PORT_DECL:
13,428✔
762
   case T_SIGNAL_DECL:
763
      return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
13,428✔
764
   default:
765
      return false;
766
   }
767
}
768

769
bool is_type_decl(tree_t t)
992,026✔
770
{
771
   switch (tree_kind(t)) {
992,026✔
772
   case T_TYPE_DECL:
773
   case T_SUBTYPE_DECL:
774
   case T_PROT_DECL:
775
   case T_PROT_BODY:
776
      return true;
777
   default:
814,093✔
778
      return false;
814,093✔
779
   }
780
}
781

782
tree_t aliased_type_decl(tree_t decl)
124,214✔
783
{
784
   switch (tree_kind(decl)) {
124,478✔
785
   case T_ALIAS:
266✔
786
      {
787
         tree_t value = tree_value(decl);
266✔
788
         const tree_kind_t kind = tree_kind(value);
266✔
789
         if (kind == T_REF && tree_has_ref(value))
266✔
790
            return aliased_type_decl(tree_ref(value));
264✔
791
         else if (kind == T_ATTR_REF && is_type_attribute(tree_subkind(value)))
2✔
792
             return value;
793
         else
UNCOV
794
            return NULL;
×
795
      }
796
   case T_TYPE_DECL:
797
   case T_SUBTYPE_DECL:
798
   case T_PROT_DECL:
799
   case T_PROT_BODY:
800
      return decl;
801
   case T_GENERIC_DECL:
1,521✔
802
      if (tree_class(decl) == C_TYPE)
1,521✔
803
         return decl;
804
      else
805
         return NULL;
740✔
806
   default:
29,033✔
807
      return NULL;
29,033✔
808
   }
809
}
810

811
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
158,513✔
812
{
813
   tree_t p = tree_new(T_PARAM);
158,513✔
814
   tree_set_loc(p, tree_loc(value));
158,513✔
815
   tree_set_subkind(p, kind);
158,513✔
816
   tree_set_value(p, value);
158,513✔
817

818
   switch (kind) {
158,513✔
819
   case P_NAMED:
74✔
820
      assert(name != NULL);
74✔
821
      tree_set_name(p, name);
74✔
822
      break;
74✔
823
   case P_POS:
158,439✔
824
      tree_set_pos(p, tree_params(call));
158,439✔
825
      break;
158,439✔
826
   }
827

828
   tree_add_param(call, p);
158,513✔
829
   return p;
158,513✔
830
}
831

832
type_t array_aggregate_type(type_t array, int from_dim)
291✔
833
{
834
   if (type_is_none(array))
291✔
835
      return type_new(T_NONE);
2✔
836

837
   if (type_is_unconstrained(array)) {
289✔
838
      const int nindex = type_indexes(array);
45✔
839
      assert(from_dim < nindex);
45✔
840

841
      type_t type = type_new(T_ARRAY);
45✔
842
      type_set_ident(type, type_ident(array));
45✔
843
      type_set_elem(type, type_elem(array));
45✔
844

845
      for (int i = from_dim; i < nindex; i++)
90✔
846
         type_add_index(type, type_index(array, i));
45✔
847

848
      return type;
849
   }
850
   else {
851
      const int ndims = dimension_of(array);
244✔
852
      assert(from_dim < ndims);
244✔
853

854
      type_t base = type_new(T_ARRAY);
244✔
855
      type_set_ident(base, type_ident(array));
244✔
856
      type_set_elem(base, type_elem(array));
244✔
857

858
      tree_t constraint = tree_new(T_CONSTRAINT);
244✔
859
      tree_set_subkind(constraint, C_INDEX);
244✔
860

861
      type_t sub = type_new(T_SUBTYPE);
244✔
862
      type_set_base(sub, base);
244✔
863
      type_add_constraint(sub, constraint);
244✔
864

865
      for (int i = from_dim; i < ndims; i++) {
510✔
866
         tree_t r = range_of(array, i);
266✔
867
         if (r == NULL) {
266✔
868
            // Not enough constraints were provided for the type
869
            r = tree_new(T_RANGE);
1✔
870
            tree_set_subkind(r, RANGE_ERROR);
1✔
871
            tree_set_type(r, type_new(T_NONE));
1✔
872
         }
873

874
         type_add_index(base, tree_type(r));
266✔
875
         tree_add_range(constraint, r);
266✔
876
      }
877

878
      return sub;
879
   }
880
}
881

882
unsigned bits_for_range(int64_t low, int64_t high)
1,687,219✔
883
{
884
   if (low > high)
1,687,219✔
885
      return 0;   // Null range
886
   else if (low < 0) {
1,687,218✔
887
      // Signed integers
888
      if (low >= INT8_MIN && high <= INT8_MAX)
516,736✔
889
         return 8;
890
      else if (low >= INT16_MIN && high <= INT16_MAX)
509,750✔
891
         return 16;
892
      else if (low >= INT32_MIN && high <= INT32_MAX)
509,519✔
893
         return 32;
894
      else
895
         return 64;
114,174✔
896
   }
897
   else {
898
      // Unsigned integers
899
      if (high <= 1)
1,170,482✔
900
         return 1;
901
      else if (high <= UINT8_MAX)
580,867✔
902
         return 8;
903
      else if (high <= UINT16_MAX)
137,731✔
904
         return 16;
905
      else if (high <= UINT32_MAX)
134,423✔
906
         return 32;
907
      else
908
         return 64;
15,953✔
909
   }
910
}
911

912
unsigned dimension_of(type_t type)
1,197,587✔
913
{
914
   switch (type_kind(type)) {
2,236,259✔
915
   case T_SUBTYPE:
1,038,670✔
916
      return dimension_of(type_base(type));
1,038,670✔
917
   case T_GENERIC:
133✔
918
      switch (type_subkind(type)) {
133✔
919
      case GTYPE_ARRAY:
121✔
920
         return type_indexes(type);
121✔
UNCOV
921
      case GTYPE_ACCESS:
×
UNCOV
922
         return dimension_of(type_designated(type));
×
923
      case GTYPE_FILE:
924
      case GTYPE_PRIVATE:
925
         return 0;
926
      default:
12✔
927
         return 1;
12✔
928
      }
929
   case T_ARRAY:
1,190,454✔
930
      return type_indexes(type);
1,190,454✔
931
   case T_NONE:
932
   case T_RECORD:
933
   case T_INCOMPLETE:
934
   case T_FILE:
935
      return 0;
936
   case T_INTEGER:
6,943✔
937
   case T_REAL:
938
   case T_PHYSICAL:
939
   case T_ENUM:
940
      return type_dims(type);
6,943✔
941
   case T_ACCESS:
2✔
942
      return dimension_of(type_designated(type));
2✔
UNCOV
943
   default:
×
944
      fatal_trace("invalid type kind %s in dimension_of",
945
                  type_kind_str(type_kind(type)));
946
   }
947
}
948

949
tree_t range_of(type_t type, unsigned dim)
1,530,632✔
950
{
951
   switch (type_kind(type)) {
1,562,319✔
952
   case T_SUBTYPE:
1,225,742✔
953
      if (type_constraints(type) > 0) {
1,225,742✔
954
         tree_t c0 = type_constraint(type, 0);
1,194,262✔
955
         switch (tree_subkind(c0)) {
1,194,262✔
956
         case C_OPEN:
207✔
957
            return range_of(type_base(type), dim);
207✔
958
         case C_INDEX:
1,194,055✔
959
         case C_RANGE:
960
            if (dim < tree_ranges(c0))
1,194,055✔
961
               return tree_range(c0, dim);
1,194,054✔
962
            else
963
               return NULL;   // Must be an error
UNCOV
964
         default:
×
965
            fatal_trace("invalid constraint in range_of");
966
         }
967
      }
968
      else
969
         return range_of(type_base(type), dim);
31,480✔
970
   case T_INTEGER:
336,577✔
971
   case T_REAL:
972
   case T_PHYSICAL:
973
   case T_ENUM:
974
      return type_dim(type, dim);
336,577✔
UNCOV
975
   default:
×
976
      fatal_trace("invalid type kind %s for %s in range_of",
977
                  type_kind_str(type_kind(type)), type_pp(type));
978
   }
979
}
980

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

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

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

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

1021
type_t index_type_of(type_t type, unsigned dim)
211,744✔
1022
{
1023
   if (dim >= dimension_of(type))
211,746✔
1024
      return NULL;
1025

1026
   type_t base = type_base_recur(type);
211,719✔
1027
   type_kind_t base_kind = type_kind(base);
211,719✔
1028
   if (base_kind == T_ARRAY)
211,719✔
1029
      return type_index(base, dim);
208,401✔
1030
   else if (base_kind == T_ENUM || base_kind == T_NONE)
3,318✔
1031
      return type;
1032
   else if (base_kind == T_GENERIC && type_subkind(base) == GTYPE_ARRAY)
3,164✔
1033
      return type_index(base, dim);
74✔
1034
   else if (base_kind == T_RECORD || base_kind == T_GENERIC)
3,090✔
1035
      return NULL;
1036
   else if (base_kind == T_ACCESS)
3,084✔
1037
      return index_type_of(type_designated(type), dim);
2✔
1038
   else
1039
      return tree_type(range_of(base, dim));
3,082✔
1040
}
1041

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

1050
ident_t well_known(well_known_t id)
411,357✔
1051
{
1052
   assert(id < NUM_WELL_KNOWN);
411,357✔
1053
   return id_cache[id];
411,357✔
1054
}
1055

1056
well_known_t is_well_known(ident_t ident)
268,526✔
1057
{
1058
   well_known_t pos = 0;
268,526✔
1059
   for (; pos < NUM_WELL_KNOWN; pos++) {
14,263,760✔
1060
      if (id_cache[pos] == ident)
14,125,878✔
1061
         break;
1062
   }
1063

1064
   return pos;
268,526✔
1065
}
1066

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

1114
   id_cache[W_IEEE_LOGIC_VECTOR] =
10,238✔
1115
      ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR");
5,119✔
1116
   id_cache[W_IEEE_ULOGIC_VECTOR] =
10,238✔
1117
      ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR");
5,119✔
1118
   id_cache[W_IEEE_1164_RISING_EDGE] =
10,238✔
1119
      ident_new("IEEE.STD_LOGIC_1164.RISING_EDGE(sU)B");
5,119✔
1120
   id_cache[W_IEEE_1164_FALLING_EDGE] =
10,238✔
1121
      ident_new("IEEE.STD_LOGIC_1164.FALLING_EDGE(sU)B");
5,119✔
1122

1123
   id_cache[W_NUMERIC_STD_UNSIGNED] = ident_new("IEEE.NUMERIC_STD_UNSIGNED");
5,119✔
1124
   id_cache[W_NUMERIC_BIT_UNSIGNED] = ident_new("IEEE.NUMERIC_BIT_UNSIGNED");
5,119✔
1125

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

1163
bool is_uninstantiated_package(tree_t pack)
36,350✔
1164
{
1165
   return tree_kind(pack) == T_PACKAGE
36,350✔
1166
      && tree_generics(pack) > 0
35,221✔
1167
      && tree_genmaps(pack) == 0;
37,637✔
1168
}
1169

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

1183
bool is_anonymous_subtype(type_t type)
161,291✔
1184
{
1185
   return type_kind(type) == T_SUBTYPE && !type_has_ident(type);
161,291✔
1186
}
1187

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

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

1209
   const int ndecls = tree_decls(pack);
1,860✔
1210
   for (int i = 0; i < ndecls; i++) {
85,039✔
1211
      tree_t d = tree_decl(pack, i);
84,545✔
1212
      const tree_kind_t dkind = tree_kind(d);
84,545✔
1213
      if ((dkind == T_FUNC_DECL || dkind == T_PROC_DECL)
84,545✔
1214
          && !(tree_flags(d) & TREE_F_PREDEFINED))
74,254✔
1215
         return true;
1216
      else if (dkind == T_CONST_DECL && !tree_has_value(d))
83,333✔
1217
         return true;
1218
      else if (dkind == T_PROT_DECL)
83,269✔
1219
         return true;
1220
   }
1221

1222
   return false;
1223
}
1224

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

1230
   if (cache[curr] == NULL) {
13,788✔
1231
      if (hint != NULL)
4,314✔
1232
         cache[curr] = hint;
1,389✔
1233
      else {
1234
         lib_t std = lib_require(well_known(lib_name));
2,925✔
1235
         cache[curr] = lib_get(std, well_known(unit_name));
2,925✔
1236
         assert(cache[curr] != NULL);
2,925✔
1237
      }
1238
   }
1239

1240
   assert(hint == NULL || hint == cache[curr]);
13,788✔
1241
   return cache[curr];
13,788✔
1242
}
1243

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

1250
static tree_t search_type_decls(tree_t container, ident_t name)
13,684✔
1251
{
1252
   const int ndecls = tree_decls(container);
13,684✔
1253

1254
   for (int i = 0; i < ndecls; i++) {
884,074✔
1255
      tree_t d = tree_decl(container, i);
884,074✔
1256
      if (is_type_decl(d) && tree_ident(d) == name)
884,074✔
1257
         return d;
13,684✔
1258
   }
1259

1260
   return NULL;
1261
}
1262

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

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

1287
      tree_t d = search_type_decls(cached_std(std), ident_new(names[which]));
13,389✔
1288
      if (d == NULL)
13,389✔
1289
         fatal_trace("cannot find standard type %s", names[which]);
1290

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

1296
      if (can_cache)
13,389✔
1297
         return (cache[which] = tree_type(d));
13,199✔
1298
      else
1299
         return tree_type(d);
190✔
1300
   }
1301
   else
1302
      return cache[which];
1303
}
1304

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

1310
   if (cache[which] == NULL) {
1,233✔
1311
      const char *names[] = {
112✔
1312
         "STD_ULOGIC",
1313
         "STD_LOGIC",
1314
      };
1315

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

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

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

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

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

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

1343
   if (cache[which] == NULL) {
417✔
1344
      const char *names[] = {
151✔
1345
         [VERILOG_LOGIC] = "T_LOGIC",
1346
         [VERILOG_LOGIC_ARRAY] = "T_LOGIC_ARRAY",
1347
         [VERILOG_INT64] = "T_INT64",
1348
         [VERILOG_NET_VALUE] = "T_NET_VALUE",
1349
         [VERILOG_NET_ARRAY] = "T_NET_ARRAY",
1350
         [VERILOG_WIRE] = "T_WIRE",
1351
         [VERILOG_WIRE_ARRAY] = "T_WIRE_ARRAY",
1352
      };
1353

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

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

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

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

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

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

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

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

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

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

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

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

1453
   return NULL;
1454
}
1455

1456
tree_t verilog_func(ident_t mangled)
104✔
1457
{
1458
   tree_t pack = cached_verilog();
104✔
1459

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

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

1470
tree_t name_to_ref(tree_t name)
96,499✔
1471
{
1472
   tree_kind_t kind;
96,499✔
1473
   while ((kind = tree_kind(name)) != T_REF) {
109,923✔
1474
      switch (kind) {
15,095✔
1475
      case T_ARRAY_REF:
13,424✔
1476
      case T_ARRAY_SLICE:
1477
      case T_RECORD_REF:
1478
      case T_ALL:
1479
         name = tree_value(name);
13,424✔
1480
         break;
13,424✔
1481
      default:
1482
         return NULL;
1483
      }
1484
   }
1485

1486
   return name;
1487
}
1488

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

1498
void mangle_one_type(text_buf_t *buf, type_t type)
169,693✔
1499
{
1500
   ident_t ident = type_ident(type);
169,693✔
1501

1502
   char code = 0;
169,693✔
1503
   switch (is_well_known(ident)) {
169,693✔
1504
   case W_STD_INTEGER:        code = 'I'; break;
1505
   case W_STD_STRING:         code = 'S'; break;
3,391✔
1506
   case W_STD_REAL:           code = 'R'; break;
3,555✔
1507
   case W_STD_BOOL:           code = 'B'; break;
22,898✔
1508
   case W_STD_CHAR:           code = 'C'; break;
623✔
1509
   case W_STD_TIME:           code = 'T'; break;
916✔
1510
   case W_STD_NATURAL:        code = 'N'; break;
4,231✔
1511
   case W_STD_POSITIVE:       code = 'P'; break;
365✔
1512
   case W_STD_BIT:            code = 'J'; break;
2,465✔
1513
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,659✔
1514
   case W_IEEE_LOGIC:         code = 'L'; break;
292✔
1515
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,673✔
1516
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
1,658✔
1517
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,397✔
1518
   default: break;
1519
   }
1520

1521
   if (code)
49,123✔
1522
      tb_append(buf, code);
61,681✔
1523
   else {
1524
      tb_printf(buf, "%zu", ident_len(ident));
108,012✔
1525
      tb_istr(buf, ident);
108,012✔
1526
   }
1527
}
169,693✔
1528

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

1549
unsigned get_case_choice_char(tree_t value, int depth)
8,297✔
1550
{
1551
   switch (tree_kind(value)) {
8,956✔
1552
   case T_STRING:
8,179✔
1553
      if (depth < tree_chars(value))
8,179✔
1554
         return assume_int(tree_char(value, depth));
8,178✔
1555
      else
1556
         return ~0;   // Out of bounds
1557

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

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

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

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

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

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

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

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

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

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

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

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

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

1633
int64_t encode_case_choice(tree_t value, int length, int bits)
1,155✔
1634
{
1635
   uint64_t enc = 0;
1,155✔
1636
   for (int i = 0; i < length; i++) {
9,326✔
1637
      if (bits > 0) {
8,171✔
1638
         enc <<= bits;
1,841✔
1639
         enc |= get_case_choice_char(value, i);
1,841✔
1640
      }
1641
      else {
1642
         enc *= 0x27d4eb2d;
6,330✔
1643
         enc += get_case_choice_char(value, i);
6,330✔
1644
      }
1645
   }
1646

1647
   return enc;
1,155✔
1648
}
1649

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

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

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

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

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

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

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

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

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

1735
         return true;
1736
      }
1737

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

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

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

1749
         return true;
1750
      }
1751

1752
   case T_ATTR_REF:
9✔
1753
      {
1754
         switch (tree_subkind(expr)) {
9✔
1755
         case ATTR_EVENT:
1756
         case ATTR_ACTIVE:
1757
         case ATTR_LAST_EVENT:
1758
         case ATTR_LAST_ACTIVE:
1759
         case ATTR_LAST_VALUE:
1760
         case ATTR_DRIVING:
1761
         case ATTR_DRIVING_VALUE:
1762
         case ATTR_STABLE:
1763
         case ATTR_QUIET:
1764
            return false;
1765
         case ATTR_POS:
3✔
1766
         case ATTR_VAL:
1767
         case ATTR_LEFTOF:
1768
         case ATTR_RIGHTOF:
1769
         case ATTR_SUCC:
1770
         case ATTR_PRED:
1771
         case ATTR_VALUE:
1772
         case ATTR_IMAGE:
1773
            assert(tree_params(expr) == 1);
3✔
1774
            return is_static(tree_value(tree_param(expr, 0)));
3✔
1775
         case ATTR_LENGTH:
6✔
1776
         case ATTR_LEFT:
1777
         case ATTR_RIGHT:
1778
         case ATTR_LOW:
1779
         case ATTR_HIGH:
1780
            {
1781
               type_t type = tree_type(tree_name(expr));
6✔
1782
               if (type_is_unconstrained(type))
6✔
1783
                  return false;
1784

1785
               int64_t dim = 1;
3✔
1786
               if (tree_params(expr) == 1)
3✔
UNCOV
1787
                  dim = assume_int(tree_value(tree_param(expr, 0)));
×
1788

1789
               tree_t r = range_of(type, dim - 1);
3✔
1790
               if (tree_subkind(r) == RANGE_EXPR)
3✔
1791
                  return false;
1792

1793
               return is_static(tree_left(r)) && is_static(tree_right(r));
3✔
1794
            }
UNCOV
1795
         default:
×
UNCOV
1796
            return true;
×
1797
         }
1798
      }
1799

UNCOV
1800
   default:
×
UNCOV
1801
      return false;
×
1802
   }
1803
}
1804

1805
tree_t longest_static_prefix(tree_t expr)
22,480✔
1806
{
1807
   switch (tree_kind(expr)) {
22,480✔
1808
   case T_ARRAY_REF:
4,033✔
1809
      {
1810
         tree_t value = tree_value(expr);
4,033✔
1811
         tree_t prefix = longest_static_prefix(value);
4,033✔
1812

1813
         if (prefix != value)
4,033✔
1814
            return prefix;
1815

1816
         const int nparams = tree_params(expr);
4,005✔
1817
         for (int i = 0; i < nparams; i++) {
8,194✔
1818
            if (!is_static(tree_value(tree_param(expr, i))))
4,431✔
1819
               return prefix;
1820
         }
1821

1822
         return expr;
1823
      }
1824

1825
   case T_ARRAY_SLICE:
648✔
1826
      {
1827
         tree_t value = tree_value(expr);
648✔
1828
         tree_t prefix = longest_static_prefix(value);
648✔
1829

1830
         if (prefix != value)
648✔
1831
            return prefix;
1832

1833
         assert(tree_ranges(expr) == 1);
641✔
1834

1835
         tree_t r = tree_range(expr, 0);
641✔
1836
         if (tree_subkind(r) == RANGE_EXPR)
641✔
1837
            return prefix;
1838
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
635✔
1839
            return prefix;
16✔
1840

1841
         return expr;
1842
      }
1843

1844
   case T_RECORD_REF:
1,095✔
1845
      {
1846
         tree_t value = tree_value(expr);
1,095✔
1847
         tree_t prefix = longest_static_prefix(value);
1,095✔
1848

1849
         if (prefix != value)
1,095✔
1850
            return prefix;
21✔
1851

1852
         return expr;
1853
      }
1854

1855
   default:
1856
      return expr;
1857
   }
1858
}
1859

1860
tree_t body_of(tree_t pack)
1,402✔
1861
{
1862
   const tree_kind_t kind = tree_kind(pack);
1,402✔
1863
   if (kind == T_PACK_INST)
1,402✔
1864
      return NULL;
1865

1866
   assert(tree_kind(pack) == T_PACKAGE);
1,402✔
1867

1868
   ident_t body_i = well_known(W_BODY);
1,402✔
1869
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
1,402✔
1870
   return lib_get_qualified(body_name);
1,402✔
1871
}
1872

1873
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
587✔
1874
{
1875
   const int ngenmaps = tree_genmaps(unit);
587✔
1876

1877
   if (pos < ngenmaps) {
587✔
1878
      tree_t m = tree_genmap(unit, pos);
449✔
1879
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
449✔
1880
         return tree_value(m);
303✔
1881
   }
1882

1883
   for (int j = 0; j < ngenmaps; j++) {
998✔
1884
      tree_t m = tree_genmap(unit, j);
860✔
1885
      switch (tree_subkind(m)) {
860✔
1886
      case P_NAMED:
440✔
1887
         {
1888
            tree_t name = tree_name(m);
440✔
1889
            assert(tree_kind(name) == T_REF);
440✔
1890

1891
            if (tree_has_ref(name) && tree_ref(name) == g)
440✔
1892
               return tree_value(m);
15✔
1893
         }
1894
         break;
1895

1896
      case P_POS:
420✔
1897
         if (tree_pos(m) == pos)
420✔
1898
            return tree_value(m);
131✔
1899
         break;
1900

1901
      default:
1902
         break;
1903
      }
1904
   }
1905

1906
   return NULL;
1907
}
1908

1909
bool relaxed_rules(void)
542,347✔
1910
{
1911
   return opt_get_int(OPT_RELAXED);
542,347✔
1912
}
1913

1914
bool is_type_attribute(attr_kind_t kind)
68,149✔
1915
{
1916
   switch (kind) {
68,149✔
1917
   case ATTR_SUBTYPE:
1918
   case ATTR_BASE:
1919
   case ATTR_ELEMENT:
1920
   case ATTR_DESIGNATED_SUBTYPE:
1921
   case ATTR_INDEX:
1922
      return true;
1923
   default:
67,505✔
1924
      return false;
67,505✔
1925
   }
1926
}
1927

1928
bool attribute_has_param(attr_kind_t attr)
22,681✔
1929
{
1930
   switch (attr) {
22,681✔
1931
   case ATTR_IMAGE:
1932
   case ATTR_SUCC:
1933
   case ATTR_PRED:
1934
   case ATTR_DELAYED:
1935
   case ATTR_LEFTOF:
1936
   case ATTR_RIGHTOF:
1937
   case ATTR_VALUE:
1938
   case ATTR_POS:
1939
   case ATTR_LOW:
1940
   case ATTR_HIGH:
1941
   case ATTR_LEFT:
1942
   case ATTR_RIGHT:
1943
   case ATTR_LENGTH:
1944
   case ATTR_RANGE:
1945
   case ATTR_REVERSE_RANGE:
1946
   case ATTR_VAL:
1947
   case ATTR_QUIET:
1948
   case ATTR_STABLE:
1949
   case ATTR_INDEX:
1950
   case ATTR_ASCENDING:
1951
      return true;
1952
   default:
2,145✔
1953
      return false;
2,145✔
1954
   }
1955
}
1956

1957
type_t get_type_or_null(tree_t t)
2,141,532✔
1958
{
1959
   switch (tree_kind(t)) {
2,141,532✔
1960
   case T_LIBRARY:
1961
   case T_ATTR_SPEC:
1962
   case T_PACKAGE:
1963
   case T_PACK_INST:
1964
   case T_PACK_BODY:
1965
   case T_ENTITY:
1966
   case T_ARCH:
1967
   case T_PROCESS:
1968
   case T_COMPONENT:
1969
   case T_INSTANCE:
1970
   case T_CONCURRENT:
1971
   case T_BLOCK:
1972
   case T_WHILE:
1973
   case T_FOR:
1974
   case T_LOOP:
1975
   case T_GROUP_TEMPLATE:
1976
   case T_CONFIGURATION:
1977
   case T_GROUP:
1978
   case T_FOR_GENERATE:
1979
   case T_IF_GENERATE:
1980
   case T_CASE_GENERATE:
1981
   case T_USE:
1982
   case T_CONTEXT:
1983
   case T_PSL_DECL:
1984
   case T_PSL_DIRECT:
1985
   case T_WAVEFORM:
1986
      return NULL;
1987
   default:
2,053,740✔
1988
      if (tree_has_type(t))
2,053,740✔
1989
         return tree_type(t);
2,052,372✔
1990
      else
1991
         return NULL;
1992
   }
1993
}
1994

1995
type_t subtype_for_string(tree_t str, type_t base)
25,572✔
1996
{
1997
   if (type_const_bounds(base))
25,572✔
1998
      return base;    // Can be checked statically
1999
   else if (!type_is_unconstrained(base))
22,482✔
2000
      base = type_base_recur(base);
149✔
2001

2002
   // Construct a new constrained array subtype: the direction and
2003
   // bounds are the same as those for a positional array aggregate
2004
   type_t sub = type_new(T_SUBTYPE);
22,482✔
2005
   type_set_base(sub, base);
22,482✔
2006

2007
   type_t index_type = index_type_of(base, 0);
22,482✔
2008
   const bool is_enum = type_is_enum(index_type);
22,482✔
2009

2010
   // The direction is determined by the index type
2011
   range_kind_t dir = direction_of(index_type, 0);
22,482✔
2012
   tree_t index_r = range_of(index_type, 0);
22,482✔
2013

2014
   // The left bound is the left of the index type and the right bound
2015
   // is determined by the number of elements
2016

2017
   tree_t left = NULL, right = NULL;
22,482✔
2018
   const int nchars = tree_chars(str);
22,482✔
2019

2020
   if (is_enum) {
22,482✔
2021
      const int nlits = type_enum_literals(type_base_recur(index_type));
14✔
2022
      int64_t index_left = assume_int(tree_left(index_r));
14✔
2023

2024
      int64_t iright, ileft;
14✔
2025
      if (nchars == 0) {
14✔
2026
         iright = index_left;
1✔
2027
         ileft = assume_int(tree_right(index_r));
1✔
2028
      }
2029
      else if (dir == RANGE_DOWNTO) {
13✔
UNCOV
2030
         ileft = index_left;
×
UNCOV
2031
         iright = MIN(nlits - 1, MAX(0, index_left - nchars + 1));
×
2032
      }
2033
      else {
2034
         ileft = index_left;
13✔
2035
         iright = MIN(nlits - 1, MAX(0, index_left + nchars - 1));
13✔
2036
      }
2037

2038
      left = get_enum_lit(str, index_type, ileft);
14✔
2039
      right = get_enum_lit(str, index_type, iright);
14✔
2040
   }
2041
   else {
2042
      left = tree_left(index_r);
22,468✔
2043

2044
      int64_t iright;
22,468✔
2045
      if (dir == RANGE_DOWNTO)
22,468✔
UNCOV
2046
         iright = assume_int(left) - nchars + 1;
×
2047
      else
2048
         iright = assume_int(left) + nchars - 1;
22,468✔
2049

2050
      right = get_int_lit(str, index_type, iright);
22,468✔
2051
   }
2052

2053
   tree_t r = tree_new(T_RANGE);
22,482✔
2054
   tree_set_subkind(r, dir);
22,482✔
2055
   tree_set_left(r, left);
22,482✔
2056
   tree_set_right(r, right);
22,482✔
2057
   tree_set_loc(r, tree_loc(str));
22,482✔
2058
   tree_set_type(r, index_type);
22,482✔
2059

2060
   tree_t c = tree_new(T_CONSTRAINT);
22,482✔
2061
   tree_set_subkind(c, C_INDEX);
22,482✔
2062
   tree_add_range(c, r);
22,482✔
2063
   tree_set_loc(c, tree_loc(str));
22,482✔
2064

2065
   type_add_constraint(sub, c);
22,482✔
2066

2067
   return sub;
22,482✔
2068
}
2069

2070
tree_t change_ref(tree_t name, tree_t new)
2,148✔
2071
{
2072
   switch (tree_kind(name)) {
2,148✔
2073
   case T_REF:
1,506✔
2074
      return make_ref(new);
1,506✔
2075

2076
   case T_ARRAY_REF:
108✔
2077
      {
2078
         tree_t t = tree_new(T_ARRAY_REF);
108✔
2079
         tree_set_loc(t, tree_loc(name));
108✔
2080
         tree_set_value(t, change_ref(tree_value(name), new));
108✔
2081
         tree_set_type(t, tree_type(name));
108✔
2082

2083
         const int nparams = tree_params(name);
108✔
2084
         for (int i = 0; i < nparams; i++)
216✔
2085
            tree_add_param(t, tree_param(name, i));
108✔
2086

2087
         return t;
2088
      }
2089

2090
   case T_ARRAY_SLICE:
46✔
2091
      {
2092
         tree_t t = tree_new(T_ARRAY_SLICE);
46✔
2093
         tree_set_loc(t, tree_loc(name));
46✔
2094
         tree_set_value(t, change_ref(tree_value(name), new));
46✔
2095
         tree_set_type(t, tree_type(name));
46✔
2096
         tree_add_range(t, tree_range(name, 0));
46✔
2097

2098
         return t;
46✔
2099
      }
2100

2101
   case T_RECORD_REF:
380✔
2102
      {
2103
         tree_t t = tree_new(T_RECORD_REF);
380✔
2104
         tree_set_loc(t, tree_loc(name));
380✔
2105
         tree_set_value(t, change_ref(tree_value(name), new));
380✔
2106
         tree_set_type(t, tree_type(name));
380✔
2107
         tree_set_ident(t, tree_ident(name));
380✔
2108
         tree_set_ref(t, tree_ref(name));
380✔
2109

2110
         return t;
380✔
2111
      }
2112

2113
   case T_CONV_FUNC:
99✔
2114
      {
2115
         tree_t t = tree_new(T_CONV_FUNC);
99✔
2116
         tree_set_loc(t, tree_loc(name));
99✔
2117
         tree_set_value(t, change_ref(tree_value(name), new));
99✔
2118
         tree_set_ident(t, tree_ident(name));
99✔
2119
         tree_set_type(t, tree_type(name));
99✔
2120
         tree_set_ref(t, tree_ref(name));
99✔
2121

2122
         return t;
99✔
2123
      }
2124

2125
   case T_TYPE_CONV:
9✔
2126
      {
2127
         tree_t t = tree_new(T_TYPE_CONV);
9✔
2128
         tree_set_loc(t, tree_loc(name));
9✔
2129
         tree_set_type(t, tree_type(name));
9✔
2130
         tree_set_value(t, change_ref(tree_value(name), new));
9✔
2131

2132
         return t;
9✔
2133
      }
2134

UNCOV
2135
   default:
×
2136
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
2137
                  tree_kind_str(tree_kind(name)));
2138
   }
2139
}
2140

2141
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
2,870✔
2142
{
2143
   switch (tree_kind(expr)) {
2,870✔
2144
   case T_ARRAY_SLICE:
77✔
2145
      build_wait(tree_range(expr, 0), fn, ctx);
77✔
2146
      break;
77✔
2147

2148
   case T_ARRAY_REF:
546✔
2149
      {
2150
         const int nparams = tree_params(expr);
546✔
2151
         for (int i = 0; i < nparams; i++)
1,092✔
2152
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
546✔
2153
      }
2154
      break;
2155

2156
   default:
2157
      break;
2158
   }
2159
}
2,870✔
2160

2161
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
13,461✔
2162
{
2163
   // LRM 08 section 10.2 has rules for building a wait statement from a
2164
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2165
   // all-sensitised processes.
2166

2167
   switch (tree_kind(expr)) {
16,687✔
2168
   case T_REF:
4,194✔
2169
      if (class_of(tree_ref(expr)) == C_SIGNAL)
4,194✔
2170
         (*fn)(expr, ctx);
2,426✔
2171
      break;
2172

2173
   case T_EXTERNAL_NAME:
8✔
2174
      if (tree_class(expr) == C_SIGNAL)
8✔
2175
         (*fn)(expr, ctx);
8✔
2176
      break;
2177

2178
   case T_WAVEFORM:
2,798✔
2179
   case T_QUALIFIED:
2180
   case T_TYPE_CONV:
2181
   case T_INERTIAL:
2182
      if (tree_has_value(expr))
2,798✔
2183
         build_wait(tree_value(expr), fn, ctx);
2,789✔
2184
      break;
2185

2186
   case T_ASSERT:
398✔
2187
      build_wait(tree_value(expr), fn, ctx);
398✔
2188
      // Fall-through
2189
   case T_REPORT:
399✔
2190
      if (tree_has_message(expr))
399✔
2191
         build_wait(tree_message(expr), fn, ctx);
272✔
2192
      break;
2193

2194
   case T_ARRAY_REF:
800✔
2195
   case T_ARRAY_SLICE:
2196
   case T_RECORD_REF:
2197
      {
2198
         tree_t ref = name_to_ref(expr);
800✔
2199
         if (ref != NULL && class_of(ref) == C_SIGNAL
800✔
2200
             && longest_static_prefix(expr) == expr)
504✔
2201
            (*fn)(expr, ctx);
427✔
2202
         else {
2203
            build_wait(tree_value(expr), fn, ctx);
373✔
2204
            build_wait_for_target(expr, fn, ctx);
373✔
2205
         }
2206
      }
2207
      break;
2208

2209
   case T_FCALL:
2,151✔
2210
   case T_PCALL:
2211
   case T_PROT_FCALL:
2212
   case T_PROT_PCALL:
2213
      {
2214
         tree_t decl = tree_ref(expr);
2,151✔
2215
         const int nparams = tree_params(expr);
2,151✔
2216
         for (int i = 0; i < nparams; i++) {
5,926✔
2217
            tree_t p = tree_param(expr, i);
3,775✔
2218
            assert(tree_subkind(p) == P_POS);
3,775✔
2219

2220
            switch (tree_subkind(tree_port(decl, tree_pos(p)))) {
3,775✔
2221
            case PORT_IN:
3,768✔
2222
            case PORT_INOUT:
2223
            case PORT_ARRAY_VIEW:
2224
            case PORT_RECORD_VIEW:
2225
               build_wait(tree_value(p), fn, ctx);
3,768✔
2226
               break;
3,768✔
2227
            default:
2228
               break;
2229
            }
2230
         }
2231
      }
2232
      break;
2233

2234
   case T_AGGREGATE:
285✔
2235
      {
2236
         const int nassocs = tree_assocs(expr);
285✔
2237
         for (int i = 0; i < nassocs; i++) {
953✔
2238
            tree_t a = tree_assoc(expr, i);
668✔
2239
            build_wait(tree_value(a), fn, ctx);
668✔
2240

2241
            switch (tree_subkind(a)) {
668✔
2242
            case A_RANGE:
53✔
2243
            case A_SLICE:
2244
               build_wait(tree_range(a, 0), fn, ctx);
53✔
2245
               break;
53✔
2246
            case A_NAMED:
66✔
2247
               build_wait(tree_name(a), fn, ctx);
66✔
2248
               break;
66✔
2249
            }
2250
         }
2251
      }
2252
      break;
2253

2254
   case T_ATTR_REF:
322✔
2255
      {
2256
         const attr_kind_t predef = tree_subkind(expr);
322✔
2257
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
322✔
2258
            build_wait(tree_name(expr), fn, ctx);
139✔
2259

2260
         const int nparams = tree_params(expr);
322✔
2261
         for (int i = 0; i < nparams; i++)
333✔
2262
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
11✔
2263
      }
2264
      break;
2265

2266
   case T_LITERAL:
2267
   case T_STRING:
2268
   case T_DUMMY_DRIVER:
2269
      break;
2270

2271
   case T_IF:
217✔
2272
      {
2273
         const int nconds = tree_conds(expr);
217✔
2274
         for (int i = 0; i < nconds; i++)
587✔
2275
            build_wait(tree_cond(expr, i), fn, ctx);
370✔
2276
      }
2277
      break;
2278

2279
   case T_COND_STMT:
374✔
2280
      {
2281
         if (tree_has_value(expr))
374✔
2282
            build_wait(tree_value(expr), fn, ctx);
245✔
2283

2284
         const int nstmts = tree_stmts(expr);
374✔
2285
         for (int i = 0; i < nstmts; i++)
750✔
2286
            build_wait(tree_stmt(expr, i), fn, ctx);
376✔
2287
      }
2288
      break;
2289

2290
   case T_PROCESS:
50✔
2291
   case T_SEQUENCE:
2292
   case T_PROC_BODY:
2293
      {
2294
         const int ndecls = tree_decls(expr);
50✔
2295
         for (int i = 0; i < ndecls; i++) {
68✔
2296
            tree_t d = tree_decl(expr, i);
18✔
2297
            if (tree_kind(d) == T_PROC_BODY)
18✔
2298
               build_wait(d, fn, ctx);
2✔
2299
         }
2300

2301
         const int nstmts = tree_stmts(expr);
50✔
2302
         for (int i = 0; i < nstmts; i++)
112✔
2303
            build_wait(tree_stmt(expr, i), fn, ctx);
62✔
2304
      }
2305
      break;
2306

2307
   case T_SIGNAL_ASSIGN:
2,481✔
2308
      {
2309
         build_wait_for_target(tree_target(expr), fn, ctx);
2,481✔
2310

2311
         const int nwaves = tree_waveforms(expr);
2,481✔
2312
         for (int i = 0; i < nwaves; i++)
5,125✔
2313
            build_wait(tree_waveform(expr, i), fn, ctx);
2,644✔
2314
      }
2315
      break;
2316

2317
   case T_VAR_ASSIGN:
13✔
2318
   case T_FORCE:
2319
      build_wait_for_target(tree_target(expr), fn, ctx);
13✔
2320
      build_wait(tree_value(expr), fn, ctx);
13✔
2321
      break;
13✔
2322

2323
   case T_RELEASE:
3✔
2324
      build_wait_for_target(tree_target(expr), fn, ctx);
3✔
2325
      break;
3✔
2326

2327
   case T_CASE:
45✔
2328
   case T_MATCH_CASE:
2329
      {
2330
         build_wait(tree_value(expr), fn, ctx);
45✔
2331

2332
         const int nstmts = tree_stmts(expr);
45✔
2333
         for (int i = 0; i < nstmts; i++) {
276✔
2334
            tree_t alt = tree_stmt(expr, i);
231✔
2335

2336
            const int nstmts = tree_stmts(alt);
231✔
2337
            for (int j = 0; j < nstmts; j++)
462✔
2338
               build_wait(tree_stmt(alt, j), fn, ctx);
231✔
2339
         }
2340
      }
2341
      break;
2342

2343
   case T_FOR:
16✔
2344
      {
2345
         build_wait(tree_range(expr, 0), fn, ctx);
16✔
2346

2347
         const int nstmts = tree_stmts(expr);
16✔
2348
         for (int i = 0; i < nstmts; i++)
35✔
2349
            build_wait(tree_stmt(expr, i), fn, ctx);
19✔
2350
      }
2351
      break;
2352

2353
   case T_WHILE:
1✔
2354
      build_wait(tree_value(expr), fn, ctx);
1✔
2355
      // Fall-through
2356
   case T_LOOP:
4✔
2357
      {
2358
         const int nstmts = tree_stmts(expr);
4✔
2359
         for (int i = 0; i < nstmts; i++)
20✔
2360
            build_wait(tree_stmt(expr, i), fn, ctx);
16✔
2361
      }
2362
      break;
2363

2364
   case T_NEXT:
9✔
2365
   case T_EXIT:
2366
      if (tree_has_value(expr))
9✔
2367
         build_wait(tree_value(expr), fn, ctx);
6✔
2368
      break;
2369

2370
   case T_RANGE:
146✔
2371
      if (tree_subkind(expr) == RANGE_EXPR)
146✔
2372
         build_wait(tree_value(expr), fn, ctx);
11✔
2373
      else {
2374
         build_wait(tree_left(expr), fn, ctx);
135✔
2375
         build_wait(tree_right(expr), fn, ctx);
135✔
2376
      }
2377
      break;
2378

UNCOV
2379
   default:
×
2380
      fatal_trace("Cannot handle tree kind %s in wait expression",
2381
                  tree_kind_str(tree_kind(expr)));
2382
   }
2383
}
13,461✔
2384

2385
void print_syntax(const char *fmt, ...)
1,854✔
2386
{
2387
   LOCAL_TEXT_BUF tb = tb_new();
1,854✔
2388
   bool highlighting = false;
1,854✔
2389
   static bool comment = false, last_was_newline = false;
1,854✔
2390
   for (const char *p = fmt; *p != '\0'; p++) {
10,098✔
2391
      if (comment) {
8,244✔
2392
         if (*p == '\n' || *p == '\r') {
3,166✔
2393
            comment = false;
94✔
2394
            last_was_newline = true;
94✔
2395
            tb_printf(tb, "$$\n");
94✔
2396
         }
2397
         else if (*p != '~' && *p != '#') {
3,072✔
2398
            tb_append(tb, *p);
2,912✔
2399
            last_was_newline = false;
2,912✔
2400
         }
2401
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
3,166✔
2402
            tb_printf(tb, "$$");
10✔
2403
            comment = false;
10✔
2404
            last_was_newline = false;
10✔
2405
         }
2406
      }
2407
      else if (*p == '\r') {
5,078✔
2408
         if (!last_was_newline) {
21✔
UNCOV
2409
            tb_append(tb, '\n');
×
UNCOV
2410
            last_was_newline = true;
×
2411
         }
2412
      }
2413
      else if (*p == '#' && *(p + 1) != '#') {
5,057✔
2414
         tb_printf(tb, "$bold$$cyan$");
347✔
2415
         last_was_newline = false;
347✔
2416
         highlighting = true;
347✔
2417
      }
2418
      else if (*p == '~' && *(p + 1) != '~') {
4,710✔
2419
         tb_printf(tb, "$yellow$");
1✔
2420
         last_was_newline = false;
1✔
2421
         highlighting = true;
1✔
2422
      }
2423
      else if ((*p == '-' && *(p + 1) == '-')
4,709✔
2424
               || (*p == '/' && *(p + 1) == '/')
4,619✔
2425
               || (*p == '/' && *(p + 1) == '*')) {
4,615✔
2426
         tb_printf(tb, "$red$%c", *p);
104✔
2427
         last_was_newline = false;
104✔
2428
         comment = true;
104✔
2429
      }
2430
      else if (!isalnum_iso88591(*p) && *p != '_'
4,605✔
2431
               && *p != '%' && highlighting) {
2,335✔
2432
         tb_printf(tb, "$$%c", *p);
304✔
2433
         last_was_newline = false;
304✔
2434
         highlighting = false;
304✔
2435
      }
2436
      else {
2437
         tb_append(tb, *p);
4,301✔
2438
         last_was_newline = (*p == '\n');
4,301✔
2439
      }
2440
   }
2441

2442
   if (highlighting)
1,854✔
2443
      tb_cat(tb, "$$");
44✔
2444

2445
   va_list ap;
1,854✔
2446
   va_start(ap, fmt);
1,854✔
2447

2448
   if (syntax_buf != NULL) {
1,854✔
2449
      char *stripped LOCAL = strip_color(tb_get(tb), ap);
3,708✔
2450
      tb_cat(syntax_buf, stripped);
1,854✔
2451
   }
2452
   else
UNCOV
2453
      color_vprintf(tb_get(tb), ap);
×
2454

2455
   va_end(ap);
1,854✔
2456
}
1,854✔
2457

2458
void capture_syntax(text_buf_t *tb)
9✔
2459
{
2460
   assert(tb == NULL || syntax_buf == NULL);
9✔
2461
   syntax_buf = tb;
9✔
2462
}
9✔
2463

2464
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur)
3,561✔
2465
{
2466
   input_from_file(file);
3,561✔
2467

2468
   switch (source_kind()) {
3,561✔
2469
   case SOURCE_VHDL:
3,483✔
2470
      {
2471
         lib_t work = lib_work();
3,483✔
2472
         int base_errors = 0;
3,483✔
2473
         tree_t unit;
3,483✔
2474
         while (base_errors = error_count(), (unit = parse())) {
13,479✔
2475
            if (error_count() == base_errors) {
9,996✔
2476
               lib_put(work, unit);
9,995✔
2477
               unit_registry_purge(ur, tree_ident(unit));
9,995✔
2478

2479
               simplify_local(unit, jit, ur);
9,995✔
2480
               bounds_check(unit);
9,995✔
2481
            }
2482
            else
2483
               lib_put_error(work, unit);
1✔
2484
         }
2485
      }
2486
      break;
2487

2488
   case SOURCE_VERILOG:
78✔
2489
      {
2490
         LOCAL_TEXT_BUF tb = tb_new();
156✔
2491
         vlog_preprocess(tb, true);
78✔
2492

2493
         input_from_buffer(tb_get(tb), tb_len(tb), SOURCE_VERILOG);
78✔
2494

2495
         lib_t work = lib_work();
78✔
2496
         vlog_node_t module;
78✔
2497
         while ((module = vlog_parse())) {
248✔
2498
            if (error_count() == 0) {
92✔
2499
               vlog_check(module);
92✔
2500

2501
               if (error_count() == 0) {
92✔
2502
                  vlog_simp(module);
92✔
2503
                  lib_put_vlog(work, module);
92✔
2504
               }
2505
            }
2506
         }
2507
      }
2508
      break;
78✔
2509

UNCOV
2510
   case SOURCE_SDF:
×
2511
      {
UNCOV
2512
         sdf_file_t *sdf_file = sdf_parse(file, 0);
×
UNCOV
2513
         progress("analysed SDF file: %s", file);
×
2514

UNCOV
2515
         if (sdf_file != NULL) {
×
UNCOV
2516
            warnf("SDF is not yet supported");
×
UNCOV
2517
            sdf_file_free(sdf_file);
×
2518
         }
2519
      }
2520
      break;
2521
   }
2522
}
3,558✔
2523

2524
bool all_character_literals(type_t type)
1,940✔
2525
{
2526
   assert(type_is_enum(type));
1,940✔
2527

2528
   type_t base = type_base_recur(type);
1,940✔
2529
   const int nlits = type_enum_literals(base);
1,940✔
2530
   for (int i = 0; i < nlits; i++) {
7,802✔
2531
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
6,722✔
2532
         return false;
2533
   }
2534

2535
   return true;
2536
}
2537

2538
bool is_operator_symbol(ident_t ident)
27,633✔
2539
{
2540
   const int len = ident_len(ident);
27,633✔
2541
   if (len < 3)
27,633✔
2542
      return false;
2543
   else if (ident_char(ident, 0) != '"')
27,383✔
2544
      return false;
2545
   else if (ident_char(ident, len - 1) != '"')
16,002✔
2546
      return false;
2547

2548
   const well_known_t wk = is_well_known(ident);
16,002✔
2549

2550
   if (standard() < STD_08)
16,002✔
2551
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,345✔
2552
   else
2553
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
12,657✔
2554
}
2555

2556
bool same_tree(tree_t a, tree_t b)
7,157✔
2557
{
2558
   const tree_kind_t akind = tree_kind(a);
7,157✔
2559
   if (akind != tree_kind(b))
7,157✔
2560
      return false;
2561

2562
   switch (akind) {
7,038✔
2563
   case T_REF:
3,376✔
2564
      return tree_ref(a) == tree_ref(b);
3,376✔
2565
   case T_ARRAY_REF:
1,069✔
2566
      {
2567
         if (!same_tree(tree_value(a), tree_value(b)))
1,069✔
2568
            return false;
2569

2570
         const int nparams = tree_params(a);
1,047✔
2571
         assert(nparams == tree_params(b));
1,047✔
2572

2573
         for (int i = 0; i < nparams; i++) {
1,926✔
2574
            tree_t pa = tree_value(tree_param(a, i));
1,722✔
2575
            tree_t pb = tree_value(tree_param(b, i));
1,722✔
2576
            if (!same_tree(pa, pb))
1,722✔
2577
               return false;
2578
         }
2579

2580
         return true;
2581
      }
2582
   case T_ARRAY_SLICE:
38✔
2583
      {
2584
         if (!same_tree(tree_value(a), tree_value(b)))
38✔
2585
            return false;
2586

2587
         tree_t ra = tree_range(a, 0);
38✔
2588
         tree_t rb = tree_range(b, 0);
38✔
2589

2590
         const range_kind_t rakind = tree_subkind(ra);
38✔
2591
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
38✔
2592
            return false;
2593

2594
         return same_tree(tree_left(ra), tree_left(rb))
38✔
2595
            && same_tree(tree_right(ra), tree_right(rb));
38✔
2596
      }
2597

2598
   case T_RECORD_REF:
747✔
2599
      return tree_ident(a) == tree_ident(b)
747✔
2600
         && same_tree(tree_value(a), tree_value(b));
750✔
2601

2602
   case T_LITERAL:
1,808✔
2603
      {
2604
         const literal_kind_t alkind = tree_subkind(a);
1,808✔
2605
         if (alkind != tree_subkind(b) || alkind != L_INT)
1,808✔
2606
            return false;
2607
         else
2608
            return tree_ival(a) == tree_ival(b);
1,769✔
2609
      }
2610
   default:
2611
      return false;
2612
   }
2613
}
2614

2615
void instance_name_to_path(text_buf_t *tb, const char *str)
9,478✔
2616
{
2617
   bool delete = false;
9,478✔
2618
   for (const char *p = str; *p; p++) {
628,910✔
2619
      if (*p == '@' || (*p == '(' && !isdigit_iso88591(*(p + 1))))
619,432✔
2620
         delete = true;
2621
      else if (*p == ')' && delete)
587,904✔
2622
         delete = false;
2623
      else if (*p == ':') {
567,434✔
2624
         delete = false;
36,474✔
2625
         tb_append(tb, ':');
36,474✔
2626
      }
2627
      else if (!delete)
530,960✔
2628
         tb_append(tb, *p);
324,527✔
2629
   }
2630
}
9,478✔
2631

2632
bool calculate_aggregate_bounds(tree_t expr, range_kind_t *kind,
2,621✔
2633
                                int64_t *left, int64_t *right)
2634
{
2635
   // Calculate the direction and bounds of an array aggregate using the
2636
   // rules in LRM 93 7.3.2.2
2637

2638
   type_t type = tree_type(expr);
2,621✔
2639
   type_t index_type = index_type_of(type, 0);
2,621✔
2640
   if (index_type == NULL || type_is_none(index_type))
2,621✔
2641
      return false;
1✔
2642

2643
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
2,620✔
2644

2645
   int64_t low, high;
2,620✔
2646
   if (!folded_bounds(index_r, &low, &high))
2,620✔
2647
      return false;
2648

2649
   int64_t clow = INT64_MAX, chigh = INT64_MIN;  // Actual bounds computed below
2,605✔
2650

2651
   range_kind_t dir;
2,605✔
2652
   if (type_is_unconstrained(type))
2,605✔
2653
      dir = tree_subkind(index_r);
2,363✔
2654
   else {
2655
      base_r = range_of(type, 0);
242✔
2656
      dir = tree_subkind(base_r);
242✔
2657
   }
2658

2659
   const int nassocs = tree_assocs(expr);
2,605✔
2660

2661
   if (standard() >= STD_08) {
2,605✔
2662
      // VHDL-2008 range association determines index direction for
2663
      // unconstrained aggregate when the expression type matches the
2664
      // array type
2665
      for (int i = 0; i < nassocs; i++) {
3,278✔
2666
         tree_t a = tree_assoc(expr, i);
2,101✔
2667
         if (tree_subkind(a) == A_SLICE)
2,101✔
2668
            dir = tree_subkind(tree_range(a, 0));
47✔
2669
      }
2670
   }
2671

2672
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
2,605✔
2673
      return false;
2674

2675
   int64_t pos = 0;
2676
   for (int i = 0; i < nassocs; i++) {
11,997✔
2677
      tree_t a = tree_assoc(expr, i);
10,245✔
2678
      int64_t ilow = 0, ihigh = 0;
10,245✔
2679
      const assoc_kind_t akind = tree_subkind(a);
10,245✔
2680

2681
      switch (akind) {
10,245✔
2682
      case A_NAMED:
543✔
2683
         {
2684
            tree_t name = tree_name(a);
543✔
2685
            if (folded_int(name, &ilow))
543✔
2686
               ihigh = ilow;
533✔
2687
            else
2688
               return false;
816✔
2689
         }
2690
         break;
533✔
2691

2692
      case A_RANGE:
1,174✔
2693
      case A_SLICE:
2694
         {
2695
            tree_t r = tree_range(a, 0);
1,174✔
2696
            const range_kind_t rkind = tree_subkind(r);
1,174✔
2697
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,174✔
2698
               tree_t left = tree_left(r), right = tree_right(r);
832✔
2699

2700
               int64_t ileft, iright;
832✔
2701
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
832✔
2702
                  ilow = (rkind == RANGE_TO ? ileft : iright);
402✔
2703
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
402✔
2704
               }
2705
               else
2706
                  return false;
430✔
2707
            }
2708
            else
2709
               return false;
2710
         }
2711
         break;
2712

2713
      case A_OTHERS:
2714
         return false;
2715

2716
      case A_POS:
8,485✔
2717
         if (i == 0) {
8,485✔
2718
            int64_t ileft;
1,138✔
2719
            if (folded_int(tree_left(base_r), &ileft))
1,138✔
2720
               ilow = ihigh = ileft;
1,138✔
2721
            else
UNCOV
2722
               return false;
×
2723
         }
2724
         else if (dir == RANGE_TO)
7,347✔
2725
            ilow = ihigh = clow + pos;
7,345✔
2726
         else
2727
            ilow = ihigh = chigh - pos;
2✔
2728
         pos++;
8,485✔
2729
         break;
8,485✔
2730

2731
      case A_CONCAT:
41✔
2732
         {
2733
            type_t value_type = tree_type(tree_value(a));
41✔
2734

2735
            int64_t length;
41✔
2736
            if (type_is_unconstrained(value_type))
41✔
2737
               return false;
32✔
2738
            else if (folded_length(range_of(value_type, 0), &length)) {
16✔
2739
               if (i == 0) {
9✔
2740
                  int64_t ileft;
2✔
2741
                  if (folded_int(tree_left(base_r), &ileft))
2✔
2742
                     ilow = ihigh = ileft;
2✔
2743
                  else
UNCOV
2744
                     return false;
×
2745
               }
2746
               else if (dir == RANGE_TO) {
7✔
2747
                  ilow = clow + pos;
7✔
2748
                  ihigh = ilow + length - 1;
7✔
2749
               }
2750
               else {
UNCOV
2751
                  ihigh = chigh - pos;
×
UNCOV
2752
                  ilow = ihigh - length + 1;
×
2753
               }
2754
               pos += length;
9✔
2755
            }
2756
            else
2757
               return false;
2758
         }
2759
         break;
2760
      }
2761

2762
      clow = MIN(clow, ilow);
9,429✔
2763
      chigh = MAX(chigh, ihigh);
9,429✔
2764
   }
2765

2766
   if (clow < low || chigh > high)
1,752✔
2767
      return false;   // Will raise a bounds check error later
2768

2769
   *kind = dir;
1,747✔
2770
   *left = dir == RANGE_TO ? clow : chigh;
1,747✔
2771
   *right = dir == RANGE_TO ? chigh : clow;
1,747✔
2772

2773
   return true;
1,747✔
2774
}
2775

2776
type_t calculate_aggregate_subtype(tree_t expr)
2,395✔
2777
{
2778
   range_kind_t dir;
2,395✔
2779
   int64_t ileft, iright;
2,395✔
2780
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
2,395✔
2781
      return NULL;
2782

2783
   type_t type = tree_type(expr);
1,729✔
2784

2785
   const int ndims = dimension_of(type);
1,729✔
2786
   type_t a0_type = NULL;
1,729✔
2787
   if (ndims > 1) {
1,729✔
2788
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
66✔
2789
      if (type_is_unconstrained(a0_type))
66✔
2790
         return NULL;
2791

2792
      assert(dimension_of(a0_type) == ndims - 1);
66✔
2793
   }
2794

2795
   type_t index_type = index_type_of(type, 0);
1,729✔
2796

2797
   tree_t left = get_discrete_lit(expr, index_type, ileft);
1,729✔
2798
   tree_t right = get_discrete_lit(expr, index_type, iright);
1,729✔
2799
   assert(left != NULL && right != NULL);
1,729✔
2800

2801
   type_t sub = type_new(T_SUBTYPE);
1,729✔
2802
   type_set_base(sub, type_base_recur(type));
1,729✔
2803

2804
   type_t elem = type_elem(type);
1,729✔
2805
   if (type_is_unconstrained(elem)) {
1,729✔
2806
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
91✔
2807
      if (!type_is_unconstrained(a0_type))
91✔
2808
         elem = a0_type;
47✔
2809
   }
2810

2811
   type_set_elem(sub, elem);
1,729✔
2812

2813
   tree_t cons = tree_new(T_CONSTRAINT);
1,729✔
2814
   tree_set_subkind(cons, C_INDEX);
1,729✔
2815

2816
   tree_t r = tree_new(T_RANGE);
1,729✔
2817
   tree_set_subkind(r, dir);
1,729✔
2818
   tree_set_type(r, index_type);
1,729✔
2819
   tree_set_left(r, left);
1,729✔
2820
   tree_set_right(r, right);
1,729✔
2821

2822
   tree_add_range(cons, r);
1,729✔
2823

2824
   for (int i = 1; i < ndims; i++)
1,795✔
2825
      tree_add_range(cons, range_of(a0_type, i - 1));
66✔
2826

2827
   type_add_constraint(sub, cons);
1,729✔
2828

2829
   return sub;
1,729✔
2830
}
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