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

nickg / nvc / 9799337310

04 Jul 2024 07:57PM UTC coverage: 91.561% (+0.01%) from 91.55%
9799337310

push

github

nickg
Crash after missing file in coverage report. Fixes #909

36 of 37 new or added lines in 2 files covered. (97.3%)

565 existing lines in 8 files now uncovered.

56897 of 62141 relevant lines covered (91.56%)

670833.56 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

142
   int64_t left, right;
1,437,812✔
143
   if (!folded_int(tree_left(r), &left))
1,437,812✔
144
      return false;
145
   else if (!folded_int(tree_right(r), &right))
1,325,868✔
146
      return false;
147

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

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

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

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

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

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

201
   return false;
202
}
203

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

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

215
   return b;
7,315✔
216
}
217

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

226
   return f;
40,698✔
227
}
228

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

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

251
   return f;
×
252
}
253

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

405
   default:
406
      return false;
407
   }
408

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

414
   return true;
415
}
416

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

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

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

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

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

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

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

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

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

470
   return NULL;
471
}
472

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

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

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

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

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

499
         return NULL;
500
      }
501

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

778
bool is_type_decl(tree_t t)
2,534,425✔
779
{
780
   switch (tree_kind(t)) {
2,534,425✔
781
   case T_TYPE_DECL:
782
   case T_SUBTYPE_DECL:
783
   case T_PROT_DECL:
784
   case T_PROT_BODY:
785
      return true;
786
   default:
2,444,009✔
787
      return false;
2,444,009✔
788
   }
789
}
790

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

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

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

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

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

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

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

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

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

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

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

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

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

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

887
      return sub;
888
   }
889
}
890

891
unsigned bits_for_range(int64_t low, int64_t high)
1,615,245✔
892
{
893
   if (low > high)
1,615,245✔
894
      return 0;   // Null range
895
   else if (low < 0) {
1,615,244✔
896
      // Signed integers
897
      if (low >= INT8_MIN && high <= INT8_MAX)
500,318✔
898
         return 8;
899
      else if (low >= INT16_MIN && high <= INT16_MAX)
493,540✔
900
         return 16;
901
      else if (low >= INT32_MIN && high <= INT32_MAX)
493,309✔
902
         return 32;
903
      else
904
         return 64;
108,267✔
905
   }
906
   else {
907
      // Unsigned integers
908
      if (high <= 1)
1,114,926✔
909
         return 1;
910
      else if (high <= UINT8_MAX)
555,005✔
911
         return 8;
912
      else if (high <= UINT16_MAX)
135,121✔
913
         return 16;
914
      else if (high <= UINT32_MAX)
132,087✔
915
         return 32;
916
      else
917
         return 64;
15,313✔
918
   }
919
}
920

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

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

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

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

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

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

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

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

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

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

1055
well_known_t is_well_known(ident_t ident)
261,572✔
1056
{
1057
   well_known_t pos = 0;
261,572✔
1058
   for (; pos < NUM_WELL_KNOWN; pos++) {
14,530,422✔
1059
      if (id_cache[pos] == ident)
14,395,153✔
1060
         break;
1061
   }
1062

1063
   return pos;
261,572✔
1064
}
1065

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

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

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

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

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

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

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

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

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

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

1221
   return false;
1222
}
1223

1224
tree_t search_decls(tree_t container, ident_t name, int nth)
13,081✔
1225
{
1226
   tree_kind_t kind = tree_kind(container);
13,081✔
1227
   if (kind == T_LIBRARY) {
13,081✔
1228
      if (nth == 0) {
×
1229
         lib_t lib = lib_require(tree_ident(container));
×
1230
         return lib_get(lib, name);
×
1231
      }
1232
      else
1233
         return NULL;
1234
   }
1235
   else if (kind == T_ENTITY || kind == T_BLOCK) {
13,081✔
1236
      const int nports = tree_ports(container);
166✔
1237
      for (int i = 0; i < nports; i++) {
176✔
1238
         tree_t p = tree_port(container, i);
10✔
1239
         if (tree_ident(p) == name && nth-- == 0)
10✔
1240
            return p;
×
1241
      }
1242
   }
1243
   else if (!is_container(container))
12,915✔
1244
      return NULL;
1245

1246
   // TODO: how to improve this?
1247
   const int ndecls = tree_decls(container);
13,081✔
1248
   tree_t best = NULL;
13,081✔
1249

1250
   for (int i = 0; i < ndecls; i++) {
840,676✔
1251
      tree_t d = tree_decl(container, i);
840,639✔
1252
      if (!tree_has_ident(d))
840,639✔
1253
         continue;
×
1254
      else if (tree_ident(d) == name) {
840,639✔
1255
         if (tree_kind(d) == T_TYPE_DECL
13,119✔
1256
             && type_kind(tree_type(d)) == T_INCOMPLETE)
12,526✔
1257
            best = d;
1258
         else if (nth-- == 0)
13,119✔
1259
            return d;
12,929✔
1260
      }
1261
      else if (tree_kind(d) == T_TYPE_DECL) {
827,520✔
1262
         type_t type = tree_type(d);
57,695✔
1263
         switch (type_kind(type)) {
57,695✔
1264
         case T_ENUM:
32,696✔
1265
            {
1266
               const int nlits = type_enum_literals(type);
32,696✔
1267
               for (int j = 0; j < nlits; j++) {
2,116,222✔
1268
                  tree_t lit = type_enum_literal(type, j);
2,083,641✔
1269
                  if (tree_ident(lit) == name && nth-- == 0)
2,083,641✔
1270
                     return lit;
115✔
1271
               }
1272
            }
1273
            break;
1274
         default:
1275
            break;
1276
         }
1277
      }
1278
   }
1279

1280
   return best;
1281
}
1282

1283
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
12,424✔
1284
                          well_known_t unit_name)
1285
{
1286
   const vhdl_standard_t curr = standard();
12,424✔
1287

1288
   if (cache[curr] == NULL) {
12,424✔
1289
      if (hint != NULL)
3,844✔
1290
         cache[curr] = hint;
1,350✔
1291
      else {
1292
         lib_t std = lib_require(well_known(lib_name));
2,494✔
1293
         cache[curr] = lib_get(std, well_known(unit_name));
2,494✔
1294
         assert(cache[curr] != NULL);
2,494✔
1295
      }
1296
   }
1297

1298
   assert(hint == NULL || hint == cache[curr]);
12,424✔
1299
   return cache[curr];
12,424✔
1300
}
1301

1302
static tree_t cached_std(tree_t hint)
12,196✔
1303
{
1304
   static tree_t standard_cache[STD_19 + 1] = {};
12,196✔
1305
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
12,196✔
1306
}
1307

1308
type_t std_type(tree_t std, std_type_t which)
923,779✔
1309
{
1310
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
923,779✔
1311
   assert(which < ARRAY_LEN(cache));
923,779✔
1312

1313
   if (cache[which] == NULL) {
923,779✔
1314
      const char *names[] = {
12,196✔
1315
         "universal_integer",
1316
         "universal_real",
1317
         "INTEGER",
1318
         "REAL",
1319
         "BOOLEAN",
1320
         "STRING",
1321
         "TIME",
1322
         "BIT",
1323
         "FILE_OPEN_KIND",
1324
         "FILE_OPEN_STATUS",
1325
         "NATURAL",
1326
         "BIT_VECTOR",
1327
         "SEVERITY_LEVEL",
1328
         "FILE_ORIGIN_KIND",
1329
         "FILE_OPEN_STATE",
1330
      };
1331

1332
      tree_t d = search_decls(cached_std(std), ident_new(names[which]), 0);
12,196✔
1333
      if (d == NULL)
12,196✔
1334
         fatal_trace("cannot find standard type %s", names[which]);
×
1335

1336
      // Do not cache standard types while bootstrapping as the GC will
1337
      // move the objects after parsing
1338
      static int can_cache = -1;
12,196✔
1339
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
12,196✔
1340

1341
      if (can_cache)
12,196✔
1342
         return (cache[which] = tree_type(d));
12,006✔
1343
      else
1344
         return tree_type(d);
190✔
1345
   }
1346
   else
1347
      return cache[which];
1348
}
1349

1350
type_t ieee_type(ieee_type_t which)
441✔
1351
{
1352
   static type_t cache[IEEE_STD_LOGIC + 1] = {};
441✔
1353
   assert(which < ARRAY_LEN(cache));
441✔
1354

1355
   if (cache[which] == NULL) {
441✔
1356
      const char *names[] = {
71✔
1357
         "STD_ULOGIC",
1358
         "STD_LOGIC",
1359
      };
1360

1361
      static tree_t ieee_cache[STD_19 + 1] = {};
71✔
1362
      tree_t unit = cached_unit(NULL, ieee_cache, W_IEEE, W_IEEE_1164);
71✔
1363

1364
      tree_t d = search_decls(unit, ident_new(names[which]), 0);
71✔
1365
      if (d == NULL)
71✔
1366
         fatal_trace("cannot find IEEE type %s", names[which]);
×
1367

1368
      // STD.STANDARD cannot depend on IEEE
1369
      assert(!opt_get_int(OPT_BOOTSTRAP));
71✔
1370

1371
      return (cache[which] = tree_type(d));
71✔
1372
   }
1373
   else
1374
      return cache[which];
1375
}
1376

1377
static tree_t cached_verilog(void)
125✔
1378
{
1379
   static tree_t verilog_cache[STD_19 + 1] = {};
125✔
1380
   return cached_unit(NULL, verilog_cache, W_NVC, W_NVC_VERILOG);
125✔
1381
}
1382

1383
type_t verilog_type(verilog_type_t which)
206✔
1384
{
1385
   static type_t cache[VERILOG_RESOLVED_NET_ARRAY + 1] = {};
206✔
1386
   assert(which < ARRAY_LEN(cache));
206✔
1387

1388
   if (cache[which] == NULL) {
206✔
1389
      const char *names[] = {
79✔
1390
         "T_LOGIC",
1391
         "T_PACKED_LOGIC",
1392
         "T_INT64",
1393
         "T_NET_VALUE",
1394
         "T_NET_ARRAY",
1395
         "T_RESOLVED_NET",
1396
         "T_RESOLVED_NET_ARRAY",
1397
      };
1398

1399
      tree_t d = search_decls(cached_verilog(), ident_new(names[which]), 0);
79✔
1400
      if (d == NULL)
79✔
1401
         fatal_trace("cannot find NVC.VERILOG type %s", names[which]);
×
1402

1403
      // STD.STANDARD cannot depend on NVC.VERILOG
1404
      assert(!opt_get_int(OPT_BOOTSTRAP));
79✔
1405

1406
      return (cache[which] = tree_type(d));
79✔
1407
   }
1408
   else
1409
      return cache[which];
1410
}
1411

1412
type_t reflection_type(reflect_type_t which)
183✔
1413
{
1414
   static type_t cache[REFLECT_SUBTYPE_MIRROR + 1] = {};
183✔
1415
   assert(which < ARRAY_LEN(cache));
183✔
1416

1417
   if (cache[which] == NULL) {
183✔
1418
      const char *names[] = {
32✔
1419
         "VALUE_MIRROR",
1420
         "SUBTYPE_MIRROR",
1421
      };
1422

1423
      static tree_t reflect_cache[STD_19 + 1] = {};
32✔
1424
      tree_t unit = cached_unit(NULL, reflect_cache, W_STD, W_STD_REFLECTION);
32✔
1425

1426
      tree_t d = search_decls(unit, ident_new(names[which]), 0);
32✔
1427
      if (d == NULL)
32✔
1428
         fatal_trace("cannot find REFLECTION type %s", names[which]);
×
1429

1430
      // STD.STANDARD cannot depend on REFLECTION
1431
      assert(!opt_get_int(OPT_BOOTSTRAP));
32✔
1432

1433
      return (cache[which] = tree_type(d));
32✔
1434
   }
1435
   else
1436
      return cache[which];
1437
}
1438

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

1487
tree_t std_func(ident_t mangled)
×
1488
{
1489
   tree_t std = cached_std(NULL);
×
1490

1491
   const int ndecls = tree_decls(std);
×
1492
   for (int i = 0; i < ndecls; i++) {
×
1493
      tree_t d = tree_decl(std, i);
×
1494
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
×
1495
         return d;
×
1496
   }
1497

1498
   return NULL;
1499
}
1500

1501
tree_t verilog_func(ident_t mangled)
46✔
1502
{
1503
   tree_t pack = cached_verilog();
46✔
1504

1505
   const int ndecls = tree_decls(pack);
46✔
1506
   for (int i = 0; i < ndecls; i++) {
3,430✔
1507
      tree_t d = tree_decl(pack, i);
3,430✔
1508
      if (is_subprogram(d) && tree_ident2(d) == mangled)
3,430✔
1509
         return d;
46✔
1510
   }
1511

1512
   fatal_trace("missing Verilog helper function %s", istr(mangled));
×
1513
}
1514

1515
tree_t name_to_ref(tree_t name)
95,419✔
1516
{
1517
   tree_kind_t kind;
95,419✔
1518
   while ((kind = tree_kind(name)) != T_REF) {
109,742✔
1519
      switch (kind) {
15,925✔
1520
      case T_ARRAY_REF:
14,323✔
1521
      case T_ARRAY_SLICE:
1522
      case T_RECORD_REF:
1523
      case T_ALL:
1524
         name = tree_value(name);
14,323✔
1525
         break;
14,323✔
1526
      default:
1527
         return NULL;
1528
      }
1529
   }
1530

1531
   return name;
1532
}
1533

1534
const char *port_mode_str(port_mode_t mode)
40✔
1535
{
1536
   const char *mode_str[] = {
40✔
1537
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE", "VIEW", "VIEW"
1538
   };
1539
   assert(mode < ARRAY_LEN(mode_str));
40✔
1540
   return mode_str[mode];
40✔
1541
}
1542

1543
void mangle_one_type(text_buf_t *buf, type_t type)
166,268✔
1544
{
1545
   ident_t ident = type_ident(type);
166,268✔
1546

1547
   char code = 0;
166,268✔
1548
   switch (is_well_known(ident)) {
166,268✔
1549
   case W_STD_INTEGER:        code = 'I'; break;
1550
   case W_STD_STRING:         code = 'S'; break;
3,260✔
1551
   case W_STD_REAL:           code = 'R'; break;
3,523✔
1552
   case W_STD_BOOL:           code = 'B'; break;
22,249✔
1553
   case W_STD_CHAR:           code = 'C'; break;
651✔
1554
   case W_STD_TIME:           code = 'T'; break;
914✔
1555
   case W_STD_NATURAL:        code = 'N'; break;
4,187✔
1556
   case W_STD_POSITIVE:       code = 'P'; break;
347✔
1557
   case W_STD_BIT:            code = 'J'; break;
2,434✔
1558
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,435✔
1559
   case W_IEEE_LOGIC:         code = 'L'; break;
250✔
1560
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,755✔
1561
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
1,563✔
1562
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,397✔
1563
   default: break;
1564
   }
1565

1566
   if (code)
47,965✔
1567
      tb_append(buf, code);
60,248✔
1568
   else {
1569
      tb_printf(buf, "%zu", ident_len(ident));
106,020✔
1570
      tb_istr(buf, ident);
106,020✔
1571
   }
1572
}
166,268✔
1573

1574
tree_t primary_unit_of(tree_t unit)
24,209✔
1575
{
1576
   switch (tree_kind(unit)) {
24,209✔
1577
   case T_ENTITY:
1578
   case T_COMPONENT:
1579
   case T_PACKAGE:
1580
   case T_BLOCK:
1581
   case T_ELAB:
1582
   case T_PACK_INST:
1583
      return unit;
1584
   case T_ARCH:
14,794✔
1585
   case T_CONFIGURATION:
1586
   case T_PACK_BODY:
1587
      return tree_primary(unit);
14,794✔
1588
   default:
×
1589
      fatal_trace("invalid kind %s in primary_unit_of",
×
1590
                  tree_kind_str(tree_kind(unit)));
1591
   }
1592
}
1593

1594
unsigned get_case_choice_char(tree_t value, int depth)
7,729✔
1595
{
1596
   switch (tree_kind(value)) {
8,361✔
1597
   case T_STRING:
7,611✔
1598
      if (depth < tree_chars(value))
7,611✔
1599
         return assume_int(tree_char(value, depth));
7,610✔
1600
      else
1601
         return ~0;   // Out of bounds
1602

1603
   case T_AGGREGATE:
14✔
1604
      {
1605
         const int nassocs = tree_assocs(value);
14✔
1606
         type_t type = tree_type(value);
14✔
1607

1608
         for (int i = 0; i < nassocs; i++) {
24✔
1609
            tree_t a = tree_assoc(value, i);
23✔
1610
            switch (tree_subkind(a)) {
23✔
1611
            case A_NAMED:
×
1612
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
1613
                  return assume_int(tree_value(a));
×
1614
               break;
1615

1616
            case A_POS:
23✔
1617
               if (tree_pos(a) == (unsigned)depth)
23✔
1618
                  return assume_int(tree_value(a));
13✔
1619
               break;
1620

1621
            case A_OTHERS:
×
1622
               return assume_int(tree_value(a));
×
1623
            }
1624
         }
1625

1626
         // This will produce an error during bounds checking
1627
         return ~0;
1628
      }
1629

1630
   case T_REF:
392✔
1631
      {
1632
         tree_t decl = tree_ref(value);
392✔
1633
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
392✔
1634
         assert(tree_has_value(decl));
392✔
1635
         return get_case_choice_char(tree_value(decl), depth);
392✔
1636
      }
1637

1638
   case T_ARRAY_SLICE:
240✔
1639
      {
1640
         tree_t base = tree_value(value);
240✔
1641
         tree_t r = tree_range(value, 0);
240✔
1642
         const int64_t rleft = assume_int(tree_left(r));
240✔
1643
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
240✔
1644
         return get_case_choice_char(base, depth + offset);
240✔
1645
      }
1646

1647
   case T_FCALL:
104✔
1648
      if (tree_subkind(tree_ref(value)) == S_CONCAT) {
104✔
1649
         const int nparams = tree_params(value);
104✔
1650
         for (int i = 0; i < nparams; i++) {
156✔
1651
            tree_t left = tree_value(tree_param(value, i));
156✔
1652

1653
            type_t left_type = tree_type(left);
156✔
1654
            if (type_is_unconstrained(left_type))
156✔
1655
               fatal_at(tree_loc(left), "sorry, this expression is not "
×
1656
                        "currently supported in a case choice");
1657

1658
            tree_t lr = range_of(tree_type(left), 0);
156✔
1659
            int64_t left_len;
156✔
1660
            if (!folded_length(lr, &left_len))
156✔
1661
               fatal_at(tree_loc(left), "cannot determine length of left hand "
×
1662
                        "side of concatenation");
1663

1664
            if (depth < left_len || i + 1 == nparams)
156✔
1665
               return get_case_choice_char(left, depth);
104✔
1666

1667
            depth -= left_len;
52✔
1668
         }
1669
      }
1670
      // Fall-through
1671

1672
   default:
1673
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1674
               tree_kind_str(tree_kind(value)));
1675
   }
1676
}
1677

1678
int64_t encode_case_choice(tree_t value, int length, int bits)
1,000✔
1679
{
1680
   uint64_t enc = 0;
1,000✔
1681
   for (int i = 0; i < length; i++) {
8,619✔
1682
      if (bits > 0) {
7,619✔
1683
         enc <<= bits;
1,637✔
1684
         enc |= get_case_choice_char(value, i);
1,637✔
1685
      }
1686
      else {
1687
         enc *= 0x27d4eb2d;
5,982✔
1688
         enc += get_case_choice_char(value, i);
5,982✔
1689
      }
1690
   }
1691

1692
   return enc;
1,000✔
1693
}
1694

1695
void to_string(text_buf_t *tb, type_t type, int64_t value)
559✔
1696
{
1697
   if (type_is_integer(type))
559✔
1698
      tb_printf(tb, "%"PRIi64, value);
425✔
1699
   else if (type_is_enum(type)) {
134✔
1700
      type_t base = type_base_recur(type);
62✔
1701
      if (value < 0 || value >= type_enum_literals(base))
62✔
1702
         tb_printf(tb, "%"PRIi64, value);
3✔
1703
      else
1704
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
59✔
1705
   }
1706
   else if (type_is_physical(type)) {
72✔
1707
      type_t base = type_base_recur(type);
24✔
1708
      const unsigned nunits = type_units(base);
24✔
1709
      tree_t max_unit = NULL;
24✔
1710
      int64_t max_unit_value = 0;
24✔
1711

1712
      // Find the largest unit that evenly divides the given value
1713
      for (unsigned u = 0; u < nunits; u++) {
216✔
1714
         tree_t unit = type_unit(base, u);
192✔
1715
         const int64_t unit_value = assume_int(tree_value(unit));
192✔
1716
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
192✔
1717
            max_unit = unit;
90✔
1718
            max_unit_value = unit_value;
90✔
1719
         }
1720
      }
1721
      assert(max_unit);
24✔
1722

1723
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
24✔
1724
                istr(tree_ident(max_unit)));
1725
   }
1726
   else if (type_is_real(type)) {
48✔
1727
      union { int64_t i; double r; } u = { .i = value };
42✔
1728
      tb_printf(tb, "%.17g", u.r);
42✔
1729
   }
1730
   else if (type_is_access(type)) {
6✔
1731
      if (value == 0)
6✔
1732
         tb_cat(tb, "NULL");
3✔
1733
      else
1734
         tb_printf(tb, "%p", (void *)value);
3✔
1735
   }
1736
}
559✔
1737

1738
static bool is_static(tree_t expr)
5,826✔
1739
{
1740
   switch (tree_kind(expr)) {
5,930✔
1741
   case T_REF:
675✔
1742
      {
1743
         tree_t decl = tree_ref(expr);
675✔
1744
         switch (tree_kind(decl)) {
675✔
1745
         case T_CONST_DECL:
179✔
1746
            return !(tree_flags(decl) & TREE_F_SEQ_BLOCK);
179✔
1747
         case T_UNIT_DECL:
1748
         case T_ENUM_LIT:
1749
         case T_GENERIC_DECL:
1750
            return true;
1751
         case T_ALIAS:
×
1752
            return is_static(tree_value(decl));
×
1753
         default:
179✔
1754
            return false;
179✔
1755
         }
1756
      }
1757

1758
   case T_LITERAL:
1759
   case T_STRING:
1760
      return true;
1761

1762
   case T_FCALL:
393✔
1763
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
393✔
1764
                                    | TREE_F_GLOBALLY_STATIC));
1765

1766
   case T_RECORD_REF:
101✔
1767
      return is_static(tree_value(expr));
101✔
1768

1769
   case T_ARRAY_REF:
60✔
1770
      {
1771
         if (!is_static(tree_value(expr)))
60✔
1772
            return false;
1773

1774
         const int nparams = tree_params(expr);
60✔
1775
         for (int i = 0; i < nparams; i++) {
120✔
1776
            if (!is_static(tree_value(tree_param(expr, i))))
60✔
1777
               return false;
1778
         }
1779

1780
         return true;
1781
      }
1782

1783
   case T_ARRAY_SLICE:
30✔
1784
      {
1785
         if (!is_static(tree_value(expr)))
30✔
1786
            return false;
1787

1788
         assert(tree_ranges(expr) == 1);
30✔
1789

1790
         tree_t r = tree_range(expr, 0);
30✔
1791
         if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
30✔
1792
            return false;
×
1793

1794
         return true;
1795
      }
1796

1797
   case T_ATTR_REF:
6✔
1798
      {
1799
         switch (tree_subkind(expr)) {
6✔
1800
         case ATTR_EVENT:
1801
         case ATTR_ACTIVE:
1802
         case ATTR_LAST_EVENT:
1803
         case ATTR_LAST_ACTIVE:
1804
         case ATTR_LAST_VALUE:
1805
         case ATTR_DRIVING:
1806
         case ATTR_DRIVING_VALUE:
1807
         case ATTR_STABLE:
1808
         case ATTR_QUIET:
1809
            return false;
1810
         case ATTR_POS:
3✔
1811
         case ATTR_VAL:
1812
         case ATTR_LEFTOF:
1813
         case ATTR_RIGHTOF:
1814
         case ATTR_SUCC:
1815
         case ATTR_PRED:
1816
         case ATTR_VALUE:
1817
         case ATTR_IMAGE:
1818
            assert(tree_params(expr) == 1);
3✔
1819
            return is_static(tree_value(tree_param(expr, 0)));
3✔
1820
         default:
3✔
1821
            return true;
3✔
1822
         }
1823
      }
1824

UNCOV
1825
   default:
×
UNCOV
1826
      return false;
×
1827
   }
1828
}
1829

1830
tree_t longest_static_prefix(tree_t expr)
21,477✔
1831
{
1832
   switch (tree_kind(expr)) {
21,477✔
1833
   case T_ARRAY_REF:
3,960✔
1834
      {
1835
         tree_t value = tree_value(expr);
3,960✔
1836
         tree_t prefix = longest_static_prefix(value);
3,960✔
1837

1838
         if (prefix != value)
3,960✔
1839
            return prefix;
1840

1841
         const int nparams = tree_params(expr);
3,938✔
1842
         for (int i = 0; i < nparams; i++) {
8,078✔
1843
            if (!is_static(tree_value(tree_param(expr, i))))
4,364✔
1844
               return prefix;
1845
         }
1846

1847
         return expr;
1848
      }
1849

1850
   case T_ARRAY_SLICE:
645✔
1851
      {
1852
         tree_t value = tree_value(expr);
645✔
1853
         tree_t prefix = longest_static_prefix(value);
645✔
1854

1855
         if (prefix != value)
645✔
1856
            return prefix;
1857

1858
         assert(tree_ranges(expr) == 1);
638✔
1859

1860
         tree_t r = tree_range(expr, 0);
638✔
1861
         if (tree_subkind(r) == RANGE_EXPR)
638✔
1862
            return prefix;
1863
         else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
632✔
1864
            return prefix;
13✔
1865

1866
         return expr;
1867
      }
1868

1869
   case T_RECORD_REF:
1,049✔
1870
      {
1871
         tree_t value = tree_value(expr);
1,049✔
1872
         tree_t prefix = longest_static_prefix(value);
1,049✔
1873

1874
         if (prefix != value)
1,049✔
1875
            return prefix;
12✔
1876

1877
         return expr;
1878
      }
1879

1880
   default:
1881
      return expr;
1882
   }
1883
}
1884

1885
tree_t body_of(tree_t pack)
1,298✔
1886
{
1887
   const tree_kind_t kind = tree_kind(pack);
1,298✔
1888
   if (kind == T_PACK_INST)
1,298✔
1889
      return NULL;
1890

1891
   assert(tree_kind(pack) == T_PACKAGE);
1,298✔
1892

1893
   ident_t body_i = well_known(W_BODY);
1,298✔
1894
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
1,298✔
1895
   return lib_get_qualified(body_name);
1,298✔
1896
}
1897

1898
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
2,468✔
1899
{
1900
   const int ngenmaps = tree_genmaps(unit);
2,468✔
1901

1902
   if (pos < ngenmaps) {
2,468✔
1903
      tree_t m = tree_genmap(unit, pos);
2,336✔
1904
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
2,336✔
1905
         return tree_value(m);
1,457✔
1906
   }
1907

1908
   for (int j = 0; j < ngenmaps; j++) {
4,554✔
1909
      tree_t m = tree_genmap(unit, j);
4,422✔
1910
      switch (tree_subkind(m)) {
4,422✔
1911
      case P_NAMED:
2,043✔
1912
         {
1913
            tree_t name = tree_name(m);
2,043✔
1914
            assert(tree_kind(name) == T_REF);
2,043✔
1915

1916
            if (tree_has_ref(name) && tree_ref(name) == g)
2,043✔
1917
               return tree_value(m);
436✔
1918
         }
1919
         break;
1920

1921
      case P_POS:
2,379✔
1922
         if (tree_pos(m) == pos)
2,379✔
1923
            return tree_value(m);
443✔
1924
         break;
1925

1926
      default:
1927
         break;
1928
      }
1929
   }
1930

1931
   return NULL;
1932
}
1933

1934
bool relaxed_rules(void)
826,173✔
1935
{
1936
   return opt_get_int(OPT_RELAXED);
826,173✔
1937
}
1938

1939
bool is_type_attribute(attr_kind_t kind)
66,421✔
1940
{
1941
   switch (kind) {
66,421✔
1942
   case ATTR_SUBTYPE:
1943
   case ATTR_BASE:
1944
   case ATTR_ELEMENT:
1945
   case ATTR_DESIGNATED_SUBTYPE:
1946
   case ATTR_INDEX:
1947
      return true;
1948
   default:
65,958✔
1949
      return false;
65,958✔
1950
   }
1951
}
1952

1953
bool attribute_has_param(attr_kind_t attr)
22,113✔
1954
{
1955
   switch (attr) {
22,113✔
1956
   case ATTR_IMAGE:
1957
   case ATTR_SUCC:
1958
   case ATTR_PRED:
1959
   case ATTR_DELAYED:
1960
   case ATTR_LEFTOF:
1961
   case ATTR_RIGHTOF:
1962
   case ATTR_VALUE:
1963
   case ATTR_POS:
1964
   case ATTR_LOW:
1965
   case ATTR_HIGH:
1966
   case ATTR_LEFT:
1967
   case ATTR_RIGHT:
1968
   case ATTR_LENGTH:
1969
   case ATTR_RANGE:
1970
   case ATTR_REVERSE_RANGE:
1971
   case ATTR_VAL:
1972
   case ATTR_QUIET:
1973
   case ATTR_STABLE:
1974
   case ATTR_INDEX:
1975
   case ATTR_ASCENDING:
1976
      return true;
1977
   default:
1,922✔
1978
      return false;
1,922✔
1979
   }
1980
}
1981

1982
type_t get_type_or_null(tree_t t)
2,023,548✔
1983
{
1984
   switch (tree_kind(t)) {
2,023,548✔
1985
   case T_LIBRARY:
1986
   case T_ATTR_SPEC:
1987
   case T_PACKAGE:
1988
   case T_PACK_INST:
1989
   case T_PACK_BODY:
1990
   case T_ENTITY:
1991
   case T_ARCH:
1992
   case T_PROCESS:
1993
   case T_COMPONENT:
1994
   case T_INSTANCE:
1995
   case T_CONCURRENT:
1996
   case T_BLOCK:
1997
   case T_WHILE:
1998
   case T_FOR:
1999
   case T_LOOP:
2000
   case T_GROUP_TEMPLATE:
2001
   case T_CONFIGURATION:
2002
   case T_GROUP:
2003
   case T_FOR_GENERATE:
2004
   case T_IF_GENERATE:
2005
   case T_USE:
2006
   case T_CONTEXT:
2007
   case T_PSL:
2008
   case T_WAVEFORM:
2009
      return NULL;
2010
   default:
1,945,077✔
2011
      if (tree_has_type(t))
1,945,077✔
2012
         return tree_type(t);
1,943,813✔
2013
      else
2014
         return NULL;
2015
   }
2016
}
2017

2018
type_t subtype_for_string(tree_t str, type_t base)
24,534✔
2019
{
2020
   if (type_const_bounds(base))
24,534✔
2021
      return base;    // Can be checked statically
2022
   else if (!type_is_unconstrained(base))
21,779✔
2023
      base = type_base_recur(base);
138✔
2024

2025
   // Construct a new constrained array subtype: the direction and
2026
   // bounds are the same as those for a positional array aggregate
2027
   type_t sub = type_new(T_SUBTYPE);
21,779✔
2028
   type_set_base(sub, base);
21,779✔
2029

2030
   type_t index_type = index_type_of(base, 0);
21,779✔
2031
   const bool is_enum = type_is_enum(index_type);
21,779✔
2032

2033
   // The direction is determined by the index type
2034
   range_kind_t dir = direction_of(index_type, 0);
21,779✔
2035

2036
   // The left bound is the left of the index type and the right bound
2037
   // is determined by the number of elements
2038

2039
   tree_t left = NULL, right = NULL;
21,779✔
2040

2041
   if (is_enum)
21,779✔
2042
      left = make_ref(type_enum_literal(type_base_recur(index_type), 0));
13✔
2043
   else
2044
      left = tree_left(range_of(index_type, 0));
21,766✔
2045

2046
   const int nchars = tree_chars(str);
21,779✔
2047
   int64_t iright;
21,779✔
2048
   if (dir == RANGE_DOWNTO)
21,779✔
UNCOV
2049
      iright = assume_int(left) - nchars + 1;
×
2050
   else
2051
      iright = assume_int(left) + nchars - 1;
21,779✔
2052

2053
   if (is_enum)
21,779✔
2054
      right = get_enum_lit(str, index_type, iright);
13✔
2055
   else
2056
      right = get_int_lit(str, index_type, iright);
21,766✔
2057

2058
   tree_t r = tree_new(T_RANGE);
21,779✔
2059
   tree_set_subkind(r, dir);
21,779✔
2060
   tree_set_left(r, left);
21,779✔
2061
   tree_set_right(r, right);
21,779✔
2062
   tree_set_loc(r, tree_loc(str));
21,779✔
2063
   tree_set_type(r, index_type);
21,779✔
2064

2065
   tree_t c = tree_new(T_CONSTRAINT);
21,779✔
2066
   tree_set_subkind(c, C_INDEX);
21,779✔
2067
   tree_add_range(c, r);
21,779✔
2068
   tree_set_loc(c, tree_loc(str));
21,779✔
2069

2070
   type_add_constraint(sub, c);
21,779✔
2071

2072
   return sub;
21,779✔
2073
}
2074

2075
tree_t change_ref(tree_t name, tree_t new)
1,999✔
2076
{
2077
   switch (tree_kind(name)) {
1,999✔
2078
   case T_REF:
1,417✔
2079
      return make_ref(new);
1,417✔
2080

2081
   case T_ARRAY_REF:
108✔
2082
      {
2083
         tree_t t = tree_new(T_ARRAY_REF);
108✔
2084
         tree_set_loc(t, tree_loc(name));
108✔
2085
         tree_set_value(t, change_ref(tree_value(name), new));
108✔
2086
         tree_set_type(t, tree_type(name));
108✔
2087

2088
         const int nparams = tree_params(name);
108✔
2089
         for (int i = 0; i < nparams; i++)
216✔
2090
            tree_add_param(t, tree_param(name, i));
108✔
2091

2092
         return t;
2093
      }
2094

2095
   case T_ARRAY_SLICE:
43✔
2096
      {
2097
         tree_t t = tree_new(T_ARRAY_SLICE);
43✔
2098
         tree_set_loc(t, tree_loc(name));
43✔
2099
         tree_set_value(t, change_ref(tree_value(name), new));
43✔
2100
         tree_set_type(t, tree_type(name));
43✔
2101
         tree_add_range(t, tree_range(name, 0));
43✔
2102

2103
         return t;
43✔
2104
      }
2105

2106
   case T_RECORD_REF:
354✔
2107
      {
2108
         tree_t t = tree_new(T_RECORD_REF);
354✔
2109
         tree_set_loc(t, tree_loc(name));
354✔
2110
         tree_set_value(t, change_ref(tree_value(name), new));
354✔
2111
         tree_set_type(t, tree_type(name));
354✔
2112
         tree_set_ident(t, tree_ident(name));
354✔
2113
         tree_set_ref(t, tree_ref(name));
354✔
2114

2115
         return t;
354✔
2116
      }
2117

2118
   case T_CONV_FUNC:
68✔
2119
      {
2120
         tree_t t = tree_new(T_CONV_FUNC);
68✔
2121
         tree_set_loc(t, tree_loc(name));
68✔
2122
         tree_set_value(t, change_ref(tree_value(name), new));
68✔
2123
         tree_set_ident(t, tree_ident(name));
68✔
2124
         tree_set_type(t, tree_type(name));
68✔
2125
         tree_set_ref(t, tree_ref(name));
68✔
2126

2127
         return t;
68✔
2128
      }
2129

2130
   case T_TYPE_CONV:
9✔
2131
      {
2132
         tree_t t = tree_new(T_TYPE_CONV);
9✔
2133
         tree_set_loc(t, tree_loc(name));
9✔
2134
         tree_set_type(t, tree_type(name));
9✔
2135
         tree_set_value(t, change_ref(tree_value(name), new));
9✔
2136

2137
         return t;
9✔
2138
      }
2139

UNCOV
2140
   default:
×
UNCOV
2141
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
×
2142
                  tree_kind_str(tree_kind(name)));
2143
   }
2144
}
2145

2146
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
2,507✔
2147
{
2148
   switch (tree_kind(expr)) {
2,507✔
2149
   case T_ARRAY_SLICE:
78✔
2150
      build_wait(tree_range(expr, 0), fn, ctx);
78✔
2151
      break;
78✔
2152

2153
   case T_ARRAY_REF:
423✔
2154
      {
2155
         const int nparams = tree_params(expr);
423✔
2156
         for (int i = 0; i < nparams; i++)
846✔
2157
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
423✔
2158
      }
2159
      break;
2160

2161
   default:
2162
      break;
2163
   }
2164
}
2,507✔
2165

2166
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
11,828✔
2167
{
2168
   // LRM 08 section 10.2 has rules for building a wait statement from a
2169
   // sensitivity list. LRM 08 section 11.3 extends these rules to
2170
   // all-sensitised processes.
2171

2172
   switch (tree_kind(expr)) {
14,763✔
2173
   case T_REF:
3,588✔
2174
      if (class_of(tree_ref(expr)) == C_SIGNAL)
3,588✔
2175
         (*fn)(expr, ctx);
2,010✔
2176
      break;
2177

2178
   case T_EXTERNAL_NAME:
4✔
2179
      if (tree_class(expr) == C_SIGNAL)
4✔
2180
         (*fn)(expr, ctx);
4✔
2181
      break;
2182

2183
   case T_WAVEFORM:
2,505✔
2184
   case T_QUALIFIED:
2185
   case T_TYPE_CONV:
2186
   case T_INERTIAL:
2187
      if (tree_has_value(expr))
2,505✔
2188
         build_wait(tree_value(expr), fn, ctx);
2,496✔
2189
      break;
2190

2191
   case T_ASSERT:
365✔
2192
      build_wait(tree_value(expr), fn, ctx);
365✔
2193
      // Fall-through
2194
   case T_REPORT:
368✔
2195
      if (tree_has_message(expr))
368✔
2196
         build_wait(tree_message(expr), fn, ctx);
274✔
2197
      break;
2198

2199
   case T_ARRAY_REF:
703✔
2200
   case T_ARRAY_SLICE:
2201
   case T_RECORD_REF:
2202
      {
2203
         tree_t ref = name_to_ref(expr);
703✔
2204
         if (ref != NULL && class_of(ref) == C_SIGNAL
703✔
2205
             && longest_static_prefix(expr) == expr)
515✔
2206
            (*fn)(expr, ctx);
436✔
2207
         else {
2208
            build_wait(tree_value(expr), fn, ctx);
267✔
2209
            build_wait_for_target(expr, fn, ctx);
267✔
2210
         }
2211
      }
2212
      break;
2213

2214
   case T_FCALL:
1,971✔
2215
   case T_PCALL:
2216
   case T_PROT_FCALL:
2217
   case T_PROT_PCALL:
2218
      {
2219
         tree_t decl = tree_ref(expr);
1,971✔
2220
         const int nparams = tree_params(expr);
1,971✔
2221
         for (int i = 0; i < nparams; i++) {
5,474✔
2222
            tree_t p = tree_param(expr, i);
3,503✔
2223
            assert(tree_subkind(p) == P_POS);
3,503✔
2224

2225
            switch (tree_subkind(tree_port(decl, tree_pos(p)))) {
3,503✔
2226
            case PORT_IN:
3,496✔
2227
            case PORT_INOUT:
2228
            case PORT_ARRAY_VIEW:
2229
            case PORT_RECORD_VIEW:
2230
               build_wait(tree_value(p), fn, ctx);
3,496✔
2231
               break;
3,496✔
2232
            default:
2233
               break;
2234
            }
2235
         }
2236
      }
2237
      break;
2238

2239
   case T_AGGREGATE:
214✔
2240
      {
2241
         const int nassocs = tree_assocs(expr);
214✔
2242
         for (int i = 0; i < nassocs; i++) {
750✔
2243
            tree_t a = tree_assoc(expr, i);
536✔
2244
            build_wait(tree_value(a), fn, ctx);
536✔
2245

2246
            switch (tree_subkind(a)) {
536✔
2247
            case A_RANGE:
52✔
2248
            case A_SLICE:
2249
               build_wait(tree_range(a, 0), fn, ctx);
52✔
2250
               break;
52✔
2251
            case A_NAMED:
35✔
2252
               build_wait(tree_name(a), fn, ctx);
35✔
2253
               break;
35✔
2254
            }
2255
         }
2256
      }
2257
      break;
2258

2259
   case T_ATTR_REF:
218✔
2260
      {
2261
         const attr_kind_t predef = tree_subkind(expr);
218✔
2262
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
218✔
2263
            build_wait(tree_name(expr), fn, ctx);
63✔
2264

2265
         const int nparams = tree_params(expr);
218✔
2266
         for (int i = 0; i < nparams; i++)
226✔
2267
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
8✔
2268
      }
2269
      break;
2270

2271
   case T_LITERAL:
2272
   case T_STRING:
2273
   case T_DUMMY_DRIVER:
2274
      break;
2275

2276
   case T_IF:
191✔
2277
      {
2278
         const int nconds = tree_conds(expr);
191✔
2279
         for (int i = 0; i < nconds; i++)
529✔
2280
            build_wait(tree_cond(expr, i), fn, ctx);
338✔
2281
      }
2282
      break;
2283

2284
   case T_COND_STMT:
338✔
2285
      {
2286
         if (tree_has_value(expr))
338✔
2287
            build_wait(tree_value(expr), fn, ctx);
213✔
2288

2289
         const int nstmts = tree_stmts(expr);
338✔
2290
         for (int i = 0; i < nstmts; i++)
682✔
2291
            build_wait(tree_stmt(expr, i), fn, ctx);
344✔
2292
      }
2293
      break;
2294

2295
   case T_PROCESS:
52✔
2296
   case T_SEQUENCE:
2297
   case T_PROC_BODY:
2298
      {
2299
         const int ndecls = tree_decls(expr);
52✔
2300
         for (int i = 0; i < ndecls; i++) {
70✔
2301
            tree_t d = tree_decl(expr, i);
18✔
2302
            if (tree_kind(d) == T_PROC_BODY)
18✔
2303
               build_wait(d, fn, ctx);
2✔
2304
         }
2305

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

2312
   case T_SIGNAL_ASSIGN:
2,224✔
2313
      {
2314
         build_wait_for_target(tree_target(expr), fn, ctx);
2,224✔
2315

2316
         const int nwaves = tree_waveforms(expr);
2,224✔
2317
         for (int i = 0; i < nwaves; i++)
4,574✔
2318
            build_wait(tree_waveform(expr, i), fn, ctx);
2,350✔
2319
      }
2320
      break;
2321

2322
   case T_VAR_ASSIGN:
13✔
2323
   case T_FORCE:
2324
      build_wait_for_target(tree_target(expr), fn, ctx);
13✔
2325
      build_wait(tree_value(expr), fn, ctx);
13✔
2326
      break;
13✔
2327

2328
   case T_RELEASE:
3✔
2329
      build_wait_for_target(tree_target(expr), fn, ctx);
3✔
2330
      break;
3✔
2331

2332
   case T_CASE:
45✔
2333
   case T_MATCH_CASE:
2334
      {
2335
         build_wait(tree_value(expr), fn, ctx);
45✔
2336

2337
         const int nstmts = tree_stmts(expr);
45✔
2338
         for (int i = 0; i < nstmts; i++) {
276✔
2339
            tree_t alt = tree_stmt(expr, i);
231✔
2340

2341
            const int nstmts = tree_stmts(alt);
231✔
2342
            for (int j = 0; j < nstmts; j++)
462✔
2343
               build_wait(tree_stmt(alt, j), fn, ctx);
231✔
2344
         }
2345
      }
2346
      break;
2347

2348
   case T_FOR:
16✔
2349
      {
2350
         build_wait(tree_range(expr, 0), fn, ctx);
16✔
2351

2352
         const int nstmts = tree_stmts(expr);
16✔
2353
         for (int i = 0; i < nstmts; i++)
35✔
2354
            build_wait(tree_stmt(expr, i), fn, ctx);
19✔
2355
      }
2356
      break;
2357

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

2369
   case T_NEXT:
9✔
2370
   case T_EXIT:
2371
      if (tree_has_value(expr))
9✔
2372
         build_wait(tree_value(expr), fn, ctx);
6✔
2373
      break;
2374

2375
   case T_RANGE:
146✔
2376
      if (tree_subkind(expr) == RANGE_EXPR)
146✔
2377
         build_wait(tree_value(expr), fn, ctx);
13✔
2378
      else {
2379
         build_wait(tree_left(expr), fn, ctx);
133✔
2380
         build_wait(tree_right(expr), fn, ctx);
133✔
2381
      }
2382
      break;
2383

UNCOV
2384
   default:
×
UNCOV
2385
      fatal_trace("Cannot handle tree kind %s in wait expression",
×
2386
                  tree_kind_str(tree_kind(expr)));
2387
   }
2388
}
11,828✔
2389

2390
void print_syntax(const char *fmt, ...)
1,529✔
2391
{
2392
   LOCAL_TEXT_BUF tb = tb_new();
1,529✔
2393
   bool highlighting = false;
1,529✔
2394
   static bool comment = false, last_was_newline = false;
1,529✔
2395
   for (const char *p = fmt; *p != '\0'; p++) {
8,442✔
2396
      if (comment) {
6,913✔
2397
         if (*p == '\n' || *p == '\r') {
2,973✔
2398
            comment = false;
92✔
2399
            last_was_newline = true;
92✔
2400
            tb_printf(tb, "$$\n");
92✔
2401
         }
2402
         else if (*p != '~' && *p != '#') {
2,881✔
2403
            tb_append(tb, *p);
2,721✔
2404
            last_was_newline = false;
2,721✔
2405
         }
2406
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
2,973✔
2407
            tb_printf(tb, "$$");
1✔
2408
            comment = false;
1✔
2409
            last_was_newline = false;
1✔
2410
         }
2411
      }
2412
      else if (*p == '\r') {
3,940✔
2413
         if (!last_was_newline) {
18✔
UNCOV
2414
            tb_append(tb, '\n');
×
UNCOV
2415
            last_was_newline = true;
×
2416
         }
2417
      }
2418
      else if (*p == '#' && *(p + 1) != '#') {
3,922✔
2419
         tb_printf(tb, "$bold$$cyan$");
275✔
2420
         last_was_newline = false;
275✔
2421
         highlighting = true;
275✔
2422
      }
2423
      else if (*p == '~' && *(p + 1) != '~') {
3,647✔
2424
         tb_printf(tb, "$yellow$");
1✔
2425
         last_was_newline = false;
1✔
2426
         highlighting = true;
1✔
2427
      }
2428
      else if ((*p == '-' && *(p + 1) == '-')
3,646✔
2429
               || (*p == '/' && *(p + 1) == '/')
3,556✔
2430
               || (*p == '/' && *(p + 1) == '*')) {
3,554✔
2431
         tb_printf(tb, "$red$%c", *p);
93✔
2432
         last_was_newline = false;
93✔
2433
         comment = true;
93✔
2434
      }
2435
      else if (!isalnum_iso88591(*p) && *p != '_'
3,553✔
2436
               && *p != '%' && highlighting) {
1,793✔
2437
         tb_printf(tb, "$$%c", *p);
244✔
2438
         last_was_newline = false;
244✔
2439
         highlighting = false;
244✔
2440
      }
2441
      else {
2442
         tb_append(tb, *p);
3,309✔
2443
         last_was_newline = (*p == '\n');
3,309✔
2444
      }
2445
   }
2446

2447
   if (highlighting)
1,529✔
2448
      tb_cat(tb, "$$");
32✔
2449

2450
   va_list ap;
1,529✔
2451
   va_start(ap, fmt);
1,529✔
2452

2453
   if (syntax_buf != NULL) {
1,529✔
2454
      char *stripped LOCAL = strip_color(tb_get(tb), ap);
3,058✔
2455
      tb_cat(syntax_buf, stripped);
1,529✔
2456
   }
2457
   else
UNCOV
2458
      color_vprintf(tb_get(tb), ap);
×
2459

2460
   va_end(ap);
1,529✔
2461
}
1,529✔
2462

2463
void capture_syntax(text_buf_t *tb)
7✔
2464
{
2465
   assert(tb == NULL || syntax_buf == NULL);
7✔
2466
   syntax_buf = tb;
7✔
2467
}
7✔
2468

2469
void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur)
3,235✔
2470
{
2471
   input_from_file(file);
3,235✔
2472

2473
   switch (source_kind()) {
3,235✔
2474
   case SOURCE_VHDL:
3,191✔
2475
      {
2476
         lib_t work = lib_work();
3,191✔
2477
         int base_errors = 0;
3,191✔
2478
         tree_t unit;
3,191✔
2479
         while (base_errors = error_count(), (unit = parse())) {
12,342✔
2480
            if (error_count() == base_errors) {
9,151✔
2481
               lib_put(work, unit);
9,150✔
2482
               unit_registry_purge(ur, tree_ident(unit));
9,150✔
2483

2484
               simplify_local(unit, jit, ur);
9,150✔
2485
               bounds_check(unit);
9,150✔
2486
            }
2487
            else
2488
               lib_put_error(work, unit);
1✔
2489
         }
2490
      }
2491
      break;
2492

2493
   case SOURCE_VERILOG:
44✔
2494
#ifdef ENABLE_VERILOG
2495
      {
2496
         LOCAL_TEXT_BUF tb = tb_new();
88✔
2497
         vlog_preprocess(tb);
44✔
2498

2499
         input_from_buffer(tb_get(tb), tb_len(tb), SOURCE_VERILOG);
44✔
2500

2501
         lib_t work = lib_work();
44✔
2502
         vlog_node_t module;
44✔
2503
         while ((module = vlog_parse())) {
139✔
2504
            if (error_count() == 0) {
51✔
2505
               vlog_check(module);
51✔
2506

2507
               if (error_count() == 0) {
51✔
2508
                  vlog_simp(module);
51✔
2509
                  lib_put_vlog(work, module);
51✔
2510
               }
2511
            }
2512
         }
2513
      }
2514
#else
2515
      fatal("Verilog is not currently supported");
2516
#endif
2517
      break;
44✔
2518
   }
2519
}
3,232✔
2520

2521
bool all_character_literals(type_t type)
1,796✔
2522
{
2523
   assert(type_is_enum(type));
1,796✔
2524

2525
   type_t base = type_base_recur(type);
1,796✔
2526
   const int nlits = type_enum_literals(base);
1,796✔
2527
   for (int i = 0; i < nlits; i++) {
6,871✔
2528
      if (ident_char(tree_ident(type_enum_literal(base, i)), 0) != '\'')
5,890✔
2529
         return false;
2530
   }
2531

2532
   return true;
2533
}
2534

2535
bool is_operator_symbol(ident_t ident)
30,118✔
2536
{
2537
   const int len = ident_len(ident);
30,118✔
2538
   if (len < 3)
30,118✔
2539
      return false;
2540
   else if (ident_char(ident, 0) != '"')
29,881✔
2541
      return false;
2542
   else if (ident_char(ident, len - 1) != '"')
18,749✔
2543
      return false;
2544

2545
   const well_known_t wk = is_well_known(ident);
18,749✔
2546

2547
   if (standard() < STD_08)
18,749✔
2548
      return wk >= W_OP_AND && wk <= W_OP_NOT;
3,288✔
2549
   else
2550
      return wk >= W_OP_AND && wk <= W_OP_MATCH_GREATER_EQUAL;
15,461✔
2551
}
2552

2553
bool same_tree(tree_t a, tree_t b)
6,706✔
2554
{
2555
   const tree_kind_t akind = tree_kind(a);
6,706✔
2556
   if (akind != tree_kind(b))
6,706✔
2557
      return false;
2558

2559
   switch (akind) {
6,599✔
2560
   case T_REF:
3,077✔
2561
      return tree_ref(a) == tree_ref(b);
3,077✔
2562
   case T_ARRAY_REF:
1,021✔
2563
      {
2564
         if (!same_tree(tree_value(a), tree_value(b)))
1,021✔
2565
            return false;
2566

2567
         const int nparams = tree_params(a);
1,002✔
2568
         assert(nparams == tree_params(b));
1,002✔
2569

2570
         for (int i = 0; i < nparams; i++) {
1,857✔
2571
            tree_t pa = tree_value(tree_param(a, i));
1,677✔
2572
            tree_t pb = tree_value(tree_param(b, i));
1,677✔
2573
            if (!same_tree(pa, pb))
1,677✔
2574
               return false;
2575
         }
2576

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

2584
         tree_t ra = tree_range(a, 0);
38✔
2585
         tree_t rb = tree_range(b, 0);
38✔
2586

2587
         const range_kind_t rakind = tree_subkind(ra);
38✔
2588
         if (rakind != tree_subkind(rb) || rakind == RANGE_EXPR)
38✔
2589
            return false;
2590

2591
         return same_tree(tree_left(ra), tree_left(rb))
38✔
2592
            && same_tree(tree_right(ra), tree_right(rb));
38✔
2593
      }
2594

2595
   case T_RECORD_REF:
736✔
2596
      return tree_ident(a) == tree_ident(b)
736✔
2597
         && same_tree(tree_value(a), tree_value(b));
736✔
2598

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

2612
void instance_name_to_path(text_buf_t *tb, const char *str)
9,100✔
2613
{
2614
   bool delete = false;
9,100✔
2615
   for (const char *p = str; *p; p++) {
591,384✔
2616
      if (*p == '@' || (*p == '(' && !isdigit_iso88591(*(p + 1))))
582,284✔
2617
         delete = true;
2618
      else if (*p == ')' && delete)
552,572✔
2619
         delete = false;
2620
      else if (*p == ':') {
533,187✔
2621
         delete = false;
35,166✔
2622
         tb_append(tb, ':');
35,166✔
2623
      }
2624
      else if (!delete)
498,021✔
2625
         tb_append(tb, *p);
296,991✔
2626
   }
2627
}
9,100✔
2628

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

2635
   type_t type = tree_type(expr);
2,546✔
2636
   type_t index_type = index_type_of(type, 0);
2,546✔
2637
   tree_t index_r = range_of(index_type, 0), base_r = index_r;
2,546✔
2638

2639
   int64_t low, high;
2,546✔
2640
   if (!folded_bounds(index_r, &low, &high))
2,546✔
2641
      return false;
2642

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

2645
   range_kind_t dir;
2,531✔
2646
   if (type_is_unconstrained(type))
2,531✔
2647
      dir = tree_subkind(index_r);
2,303✔
2648
   else {
2649
      base_r = range_of(type, 0);
228✔
2650
      dir = tree_subkind(base_r);
228✔
2651
   }
2652

2653
   const int nassocs = tree_assocs(expr);
2,531✔
2654

2655
   if (standard() >= STD_08) {
2,531✔
2656
      // VHDL-2008 range association determines index direction for
2657
      // unconstrained aggregate when the expression type matches the
2658
      // array type
2659
      for (int i = 0; i < nassocs; i++) {
3,131✔
2660
         tree_t a = tree_assoc(expr, i);
1,993✔
2661
         if (tree_subkind(a) == A_SLICE)
1,993✔
2662
            dir = tree_subkind(tree_range(a, 0));
46✔
2663
      }
2664
   }
2665

2666
   if (dir != RANGE_TO && dir != RANGE_DOWNTO)
2,531✔
2667
      return false;
2668

2669
   int64_t pos = 0;
2670
   for (int i = 0; i < nassocs; i++) {
11,781✔
2671
      tree_t a = tree_assoc(expr, i);
10,102✔
2672
      int64_t ilow = 0, ihigh = 0;
10,102✔
2673
      const assoc_kind_t akind = tree_subkind(a);
10,102✔
2674

2675
      switch (akind) {
10,102✔
2676
      case A_NAMED:
489✔
2677
         {
2678
            tree_t name = tree_name(a);
489✔
2679
            if (folded_int(name, &ilow))
489✔
2680
               ihigh = ilow;
479✔
2681
            else
2682
               return false;
815✔
2683
         }
2684
         break;
479✔
2685

2686
      case A_RANGE:
1,170✔
2687
      case A_SLICE:
2688
         {
2689
            tree_t r = tree_range(a, 0);
1,170✔
2690
            const range_kind_t rkind = tree_subkind(r);
1,170✔
2691
            if (rkind == RANGE_TO || rkind == RANGE_DOWNTO) {
1,170✔
2692
               tree_t left = tree_left(r), right = tree_right(r);
828✔
2693

2694
               int64_t ileft, iright;
828✔
2695
               if (folded_int(left, &ileft) && folded_int(right, &iright)) {
828✔
2696
                  ilow = (rkind == RANGE_TO ? ileft : iright);
398✔
2697
                  ihigh = (rkind == RANGE_TO ? iright : ileft);
398✔
2698
               }
2699
               else
2700
                  return false;
430✔
2701
            }
2702
            else
2703
               return false;
2704
         }
2705
         break;
2706

2707
      case A_OTHERS:
2708
         return false;
2709

2710
      case A_POS:
8,401✔
2711
         if (i == 0) {
8,401✔
2712
            int64_t ileft;
1,112✔
2713
            if (folded_int(tree_left(base_r), &ileft))
1,112✔
2714
               ilow = ihigh = ileft;
1,112✔
2715
            else
UNCOV
2716
               return false;
×
2717
         }
2718
         else if (dir == RANGE_TO)
7,289✔
2719
            ilow = ihigh = clow + pos;
7,287✔
2720
         else
2721
            ilow = ihigh = chigh - pos;
2✔
2722
         pos++;
8,401✔
2723
         break;
8,401✔
2724

2725
      case A_CONCAT:
40✔
2726
         {
2727
            type_t value_type = tree_type(tree_value(a));
40✔
2728

2729
            int64_t length;
40✔
2730
            if (type_is_unconstrained(value_type))
40✔
2731
               return false;
31✔
2732
            else if (folded_length(range_of(value_type, 0), &length)) {
15✔
2733
               if (i == 0) {
9✔
2734
                  int64_t ileft;
2✔
2735
                  if (folded_int(tree_left(base_r), &ileft))
2✔
2736
                     ilow = ihigh = ileft;
2✔
2737
                  else
UNCOV
2738
                     return false;
×
2739
               }
2740
               else if (dir == RANGE_TO) {
7✔
2741
                  ilow = clow + pos;
7✔
2742
                  ihigh = ilow + length - 1;
7✔
2743
               }
2744
               else {
UNCOV
2745
                  ihigh = chigh - pos;
×
UNCOV
2746
                  ilow = ihigh - length + 1;
×
2747
               }
2748
               pos += length;
9✔
2749
            }
2750
            else
2751
               return false;
2752
         }
2753
         break;
2754
      }
2755

2756
      clow = MIN(clow, ilow);
9,287✔
2757
      chigh = MAX(chigh, ihigh);
9,287✔
2758
   }
2759

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

2763
   *kind = dir;
1,674✔
2764
   *left = dir == RANGE_TO ? clow : chigh;
1,674✔
2765
   *right = dir == RANGE_TO ? chigh : clow;
1,674✔
2766

2767
   return true;
1,674✔
2768
}
2769

2770
type_t calculate_aggregate_subtype(tree_t expr)
2,320✔
2771
{
2772
   range_kind_t dir;
2,320✔
2773
   int64_t ileft, iright;
2,320✔
2774
   if (!calculate_aggregate_bounds(expr, &dir, &ileft, &iright))
2,320✔
2775
      return NULL;
2776

2777
   type_t type = tree_type(expr);
1,656✔
2778

2779
   const int ndims = dimension_of(type);
1,656✔
2780
   type_t a0_type = NULL;
1,656✔
2781
   if (ndims > 1) {
1,656✔
2782
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
57✔
2783
      if (type_is_unconstrained(a0_type))
57✔
2784
         return NULL;
2785

2786
      assert(dimension_of(a0_type) == ndims - 1);
57✔
2787
   }
2788

2789
   type_t index_type = index_type_of(type, 0);
1,656✔
2790

2791
   tree_t left = get_discrete_lit(expr, index_type, ileft);
1,656✔
2792
   tree_t right = get_discrete_lit(expr, index_type, iright);
1,656✔
2793
   assert(left != NULL && right != NULL);
1,656✔
2794

2795
   type_t sub = type_new(T_SUBTYPE);
1,656✔
2796
   type_set_base(sub, type_base_recur(type));
1,656✔
2797

2798
   type_t elem = type_elem(type);
1,656✔
2799
   if (type_is_unconstrained(elem)) {
1,656✔
2800
      a0_type = tree_type(tree_value(tree_assoc(expr, 0)));
86✔
2801
      if (!type_is_unconstrained(a0_type))
86✔
2802
         elem = a0_type;
42✔
2803
   }
2804

2805
   type_set_elem(sub, elem);
1,656✔
2806

2807
   tree_t cons = tree_new(T_CONSTRAINT);
1,656✔
2808
   tree_set_subkind(cons, C_INDEX);
1,656✔
2809

2810
   tree_t r = tree_new(T_RANGE);
1,656✔
2811
   tree_set_subkind(r, dir);
1,656✔
2812
   tree_set_type(r, index_type);
1,656✔
2813
   tree_set_left(r, left);
1,656✔
2814
   tree_set_right(r, right);
1,656✔
2815

2816
   tree_add_range(cons, r);
1,656✔
2817

2818
   for (int i = 1; i < ndims; i++)
1,713✔
2819
      tree_add_range(cons, range_of(a0_type, i - 1));
57✔
2820

2821
   type_add_constraint(sub, cons);
1,656✔
2822

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