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

nickg / nvc / 4773040954

pending completion
4773040954

push

github

Nick Gasson
Stack corruption with large number of arguments. Fixes #665

8 of 8 new or added lines in 1 file covered. (100.0%)

40777 of 45100 relevant lines covered (90.41%)

917340.07 hits per line

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

91.2
/src/common.c
1
//
2
//  Copyright (C) 2013-2023  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 "option.h"
25
#include "type.h"
26

27
#include <assert.h>
28
#include <ctype.h>
29
#include <string.h>
30
#include <stdlib.h>
31
#include <inttypes.h>
32

33
typedef struct {
34
   ptr_list_t copied_subs;
35
   ptr_list_t copied_types;
36
} copy_ctx_t;
37

38
static vhdl_standard_t  current_std  = STD_02;
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)
108,080✔
44
{
45
   switch (tree_kind(t)) {
108,080✔
46
   case T_LITERAL:
95,184✔
47
      switch (tree_subkind(t)) {
95,184✔
48
      case L_INT:
95,184✔
49
      case L_PHYSICAL:
50
         return tree_ival(t);
95,184✔
51
      }
52
      break;
53

54
   case T_REF:
12,896✔
55
      {
56
         tree_t decl = tree_ref(t);
12,896✔
57
         if (tree_kind(decl) == T_CONST_DECL) {
12,896✔
58
            if (tree_has_value(decl))
×
59
               return assume_int(tree_value(decl));
×
60
            else {
61
               // Deferred constant
62
               tree_t pack = tree_container(decl);
×
63
               assert(tree_kind(pack) == T_PACKAGE);
×
64

65
               tree_t body = body_of(pack);
×
66
               ident_t id = tree_ident(decl);
×
67

68
               if (body != NULL && (decl = search_decls(body, id, 0))) {
×
69
                  assert(tree_kind(decl) == T_CONST_DECL);
×
70
                  assert(tree_has_value(decl));
×
71
                  return assume_int(tree_value(decl));
×
72
               }
73
            }
74
         }
75
         else {
76
            assert(tree_kind(decl) == T_ENUM_LIT);
12,896✔
77
            return tree_pos(decl);
12,896✔
78
         }
79
      }
80

81
   default:
82
      break;
83
   }
84

85
   fatal_at(tree_loc(t), "expression cannot be folded to "
×
86
            "an integer constant");
87
}
88

89
void range_bounds(tree_t r, int64_t *low, int64_t *high)
38,858✔
90
{
91
   assert(tree_kind(r) == T_RANGE);
38,858✔
92

93
   const int64_t left = assume_int(tree_left(r));
38,858✔
94
   const int64_t right = assume_int(tree_right(r));
38,858✔
95

96
   *low  = tree_subkind(r) == RANGE_TO ? left : right;
38,858✔
97
   *high = tree_subkind(r) == RANGE_TO ? right : left;
38,858✔
98
}
38,858✔
99

100
bool folded_int(tree_t t, int64_t *l)
1,742,930✔
101
{
102
   switch (tree_kind(t)) {
1,742,980✔
103
   case T_LITERAL:
1,487,300✔
104
      switch (tree_subkind(t)) {
1,487,300✔
105
      case L_PHYSICAL:
46,629✔
106
         if (tree_has_ref(t))
46,629✔
107
            return false;
108
         // Fall-through
109
      case L_INT:
110
         *l = tree_ival(t);
1,477,940✔
111
         return true;
1,477,940✔
112
      default:
113
         return false;
114
      }
115
   case T_QUALIFIED:
52✔
116
      return folded_int(tree_value(t), l);
52✔
117
   default:
118
      return false;
119
   }
120
}
121

122
bool folded_real(tree_t t, double *l)
85,223✔
123
{
124
   switch (tree_kind(t)) {
85,244✔
125
   case T_LITERAL:
78,855✔
126
      if (tree_subkind(t) == L_REAL) {
78,855✔
127
         *l = tree_dval(t);
78,853✔
128
         return true;
78,853✔
129
      }
130
      else
131
         return false;
132
   case T_QUALIFIED:
21✔
133
      return folded_real(tree_value(t), l);
21✔
134
   default:
135
      return false;
136
   }
137
}
138

139
bool folded_length(tree_t r, int64_t *l)
29,555✔
140
{
141
   int64_t low, high;
29,555✔
142
   if (folded_bounds(r, &low, &high)) {
29,555✔
143
      *l = MAX(high - low + 1, 0);
27,256✔
144
      return true;
27,256✔
145
   }
146
   else
147
      return false;
148
}
149

150
bool folded_bounds(tree_t r, int64_t *low, int64_t *high)
854,442✔
151
{
152
   assert(tree_kind(r) == T_RANGE);
854,442✔
153

154
   const range_kind_t rkind = tree_subkind(r);
854,442✔
155

156
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
854,442✔
157
      return false;
158

159
   int64_t left, right;
849,613✔
160
   unsigned leftu, rightu;
849,613✔
161
   if (folded_int(tree_left(r), &left) && folded_int(tree_right(r), &right))
849,613✔
162
       ;
163
   else if (folded_enum(tree_left(r), &leftu)
138,044✔
164
            && folded_enum(tree_right(r), &rightu)) {
97,577✔
165
      left  = leftu;
97,577✔
166
      right = rightu;
97,577✔
167
   }
168
   else
169
      return false;
40,467✔
170

171
   switch (rkind) {
809,146✔
172
   case RANGE_TO:
771,076✔
173
      *low  = left;
771,076✔
174
      *high = right;
771,076✔
175
      return true;
771,076✔
176
   case RANGE_DOWNTO:
38,070✔
177
      *low  = right;
38,070✔
178
      *high = left;
38,070✔
179
      return true;
38,070✔
180
   default:
181
      return false;
182
   }
183
}
184

185
bool folded_bounds_real(tree_t r, double *low, double *high)
37,917✔
186
{
187
   assert(tree_kind(r) == T_RANGE);
37,917✔
188

189
   const range_kind_t rkind = tree_subkind(r);
37,917✔
190

191
   if (rkind != RANGE_TO && rkind != RANGE_DOWNTO)
37,917✔
192
      return false;
193

194
   double left, right;
37,914✔
195
   if (folded_real(tree_left(r), &left) && folded_real(tree_right(r), &right)) {
37,914✔
196
      switch (rkind) {
37,909✔
197
      case RANGE_TO:
37,909✔
198
         *low  = left;
37,909✔
199
         *high = right;
37,909✔
200
         return true;
37,909✔
201
      case RANGE_DOWNTO:
×
202
         *low  = right;
×
203
         *high = left;
×
204
         return true;
×
205
      default:
206
         return false;
207
      }
208
   }
209
   else
210
      return false;
5✔
211
}
212

213
bool folded_enum(tree_t t, unsigned *pos)
354,740✔
214
{
215
   if (tree_kind(t) == T_REF) {
354,740✔
216
      tree_t decl = tree_ref(t);
268,940✔
217
      if (tree_kind(decl) == T_ENUM_LIT) {
268,940✔
218
         *pos = tree_pos(decl);
218,988✔
219
         return true;
218,988✔
220
      }
221
   }
222

223
   return false;
224
}
225

226
bool folded_bool(tree_t t, bool *b)
27,739✔
227
{
228
   if (tree_kind(t) == T_REF) {
27,739✔
229
      tree_t decl = tree_ref(t);
5,692✔
230
      if (tree_kind(decl) == T_ENUM_LIT
5,692✔
231
          && type_ident(tree_type(decl)) == well_known(W_STD_BOOL)) {
3,816✔
232
         *b = (tree_pos(decl) == 1);
3,816✔
233
         return true;
3,816✔
234
      }
235
   }
236

237
   return false;
238
}
239

240
tree_t get_enum_lit(tree_t t, type_t type, int pos)
20✔
241
{
242
   type_t enum_type = type_base_recur(type ?: tree_type(t));
20✔
243
   tree_t lit = type_enum_literal(enum_type, pos);
20✔
244

245
   tree_t b = tree_new(T_REF);
20✔
246
   tree_set_loc(b, tree_loc(t));
20✔
247
   tree_set_ref(b, lit);
20✔
248
   tree_set_type(b, enum_type);
20✔
249
   tree_set_ident(b, tree_ident(lit));
20✔
250

251
   return b;
20✔
252
}
253

254
tree_t get_int_lit(tree_t t, type_t type, int64_t i)
12,782✔
255
{
256
   tree_t f = tree_new(T_LITERAL);
12,782✔
257
   tree_set_subkind(f, L_INT);
12,782✔
258
   tree_set_ival(f, i);
12,782✔
259
   tree_set_loc(f, tree_loc(t));
12,782✔
260
   tree_set_type(f, type ?: tree_type(t));
12,782✔
261

262
   return f;
12,782✔
263
}
264

265
tree_t get_real_lit(tree_t t, type_t type, double r)
×
266
{
267
   tree_t f = tree_new(T_LITERAL);
×
268
   tree_set_loc(f, tree_loc(t));
×
269
   tree_set_subkind(f, L_REAL);
×
270
   tree_set_dval(f, r);
×
271
   tree_set_type(f, type ?: tree_type(t));
×
272

273
   return f;
×
274
}
275

276
bool parse_value(type_t type, const char *str, scalar_value_t *value)
59✔
277
{
278
   while (isspace_iso88591(*str))
68✔
279
      ++str;
9✔
280

281
   type_t base = type_base_recur(type);
59✔
282
   const type_kind_t basek = type_kind(base);
59✔
283

284
   switch (basek) {
59✔
285
   case T_INTEGER:
37✔
286
      {
287
         bool is_negative = *str == '-';
37✔
288
         int num_digits = 0;
37✔
289

290
         if (is_negative) {
37✔
291
            ++str;
2✔
292
         }
293
         int64_t sum = 0;
294
         while (isdigit((int)*str) || (*str == '_')) {
114✔
295
            if (*str != '_') {
77✔
296
               sum *= 10;
75✔
297
               sum += (*str - '0');
75✔
298
               num_digits++;
75✔
299
            }
300
            ++str;
77✔
301
         }
302

303
         value->integer = is_negative ? -sum : sum;
37✔
304

305
         if (num_digits == 0)
37✔
306
            return false;
307
      }
308
      break;
309

310
   case T_ENUM:
12✔
311
      {
312
         bool upcase = true;
12✔
313
         char *copy LOCAL = xstrdup(str), *p;
24✔
314
         for (p = copy; (*p != '\0') && !isspace_iso88591(*p); p++, str++) {
48✔
315
            if (*p == '\'')
36✔
316
               upcase = false;
317
            if (upcase)
20✔
318
               *p = toupper_iso88591(*p);
12✔
319
         }
320
         *p = '\0';
12✔
321

322
         ident_t id = ident_new(copy);
12✔
323

324
         value->integer = -1;
12✔
325

326
         const int nlits = type_enum_literals(base);
12✔
327
         for (int i = 0; i < nlits; i++) {
33✔
328
            if (tree_ident(type_enum_literal(base, i)) == id) {
33✔
329
               value->integer = i;
12✔
330
               break;
12✔
331
            }
332
         }
333

334
         if (value->integer == -1)
12✔
335
            return false;
×
336
      }
337
      break;
338

339
   case T_REAL:
6✔
340
      {
341
         char *eptr = NULL;
6✔
342
         value->real = strtod(str, &eptr);
6✔
343
         str = eptr;
6✔
344
      }
345
      break;
6✔
346

347
   case T_PHYSICAL:
4✔
348
      {
349
         char *eptr = NULL;
4✔
350
         double scale = strtod(str, &eptr);
4✔
351
         str = eptr;
4✔
352

353
         while (isspace_iso88591(*str)) ++str;
9✔
354

355
         char *copy LOCAL = xstrdup(str), *p;
8✔
356
         for (p = copy; *p && !isspace_iso88591(*p); p++, str++)
12✔
357
            *p = toupper_iso88591(*p);
8✔
358
         *p = '\0';
4✔
359

360
         ident_t id = ident_new(copy);
4✔
361

362
         value->integer = -1;
4✔
363

364
         const int nunits = type_units(base);
4✔
365
         for (int i = 0; i < nunits; i++) {
11✔
366
            tree_t u = type_unit(base, i);
11✔
367
            if (tree_ident(u) == id) {
11✔
368
               value->integer = scale * assume_int(tree_value(u));
4✔
369
               break;
4✔
370
            }
371
         }
372

373
         if (value->integer == -1)
4✔
374
            return false;
×
375
      }
376

377
   default:
378
      break;
379
   }
380

381
   for (; *str; str++) {
70✔
382
      if (!isspace_iso88591(*str))
11✔
383
         return false;
384
   }
385

386
   return true;
387
}
388

389
tree_t make_ref(tree_t to)
11,752✔
390
{
391
   tree_t t = tree_new(T_REF);
11,752✔
392
   tree_set_ident(t, tree_ident(to));
11,752✔
393
   tree_set_ref(t, to);
11,752✔
394
   tree_set_type(t, tree_type(to));
11,752✔
395
   return t;
11,752✔
396
}
397

398
vhdl_standard_t standard(void)
3,887,820✔
399
{
400
   return current_std;
3,887,820✔
401
}
402

403
void set_standard(vhdl_standard_t s)
3,293✔
404
{
405
   current_std = s;
3,293✔
406
   have_set_std = true;
3,293✔
407
}
3,293✔
408

409
void set_default_standard(vhdl_standard_t s)
234✔
410
{
411
   if (!have_set_std)
234✔
412
      set_standard(s);
30✔
413
}
234✔
414

415
const char *standard_text(vhdl_standard_t s)
3,060✔
416
{
417
   static const char *text[] = {
3,060✔
418
      "1987", "1993", "2000", "2002", "2008", "2019"
419
   };
420

421
   if ((unsigned)s < ARRAY_LEN(text))
3,060✔
422
      return text[s];
3,060✔
423
   else
424
      return "????";
425
}
426

427
int record_field_to_net(type_t type, unsigned pos)
×
428
{
429
   int offset = 0;
×
430
   for (int i = 0; i < pos; i++)
×
431
      offset += type_width(tree_type(type_field(type, i)));
×
432

433
   return offset;
×
434
}
435

436
tree_t find_record_field(tree_t rref)
8,043✔
437
{
438
   ident_t fname = tree_ident(rref);
8,043✔
439
   type_t value_type = tree_type(tree_value(rref));
8,043✔
440

441
   if (!type_is_record(value_type))
8,043✔
442
      return NULL;
443

444
   const int nfields = type_fields(value_type);
8,043✔
445
   for (int i = 0; i < nfields; i++) {
27,035✔
446
      tree_t field = type_field(value_type, i);
27,030✔
447
      if (tree_ident(field) == fname)
27,030✔
448
         return field;
8,038✔
449
   }
450

451
   return NULL;
452
}
453

454
class_t class_of(tree_t t)
1,105,150✔
455
{
456
   switch (tree_kind(t)) {
1,157,920✔
457
   case T_VAR_DECL:
458
      return C_VARIABLE;
459
   case T_SIGNAL_DECL:
44,708✔
460
   case T_IMPLICIT_SIGNAL:
461
      return C_SIGNAL;
44,708✔
462
   case T_CONST_DECL:
39,483✔
463
      return C_CONSTANT;
39,483✔
464
   case T_PORT_DECL:
175,375✔
465
   case T_GENERIC_DECL:
466
   case T_PARAM_DECL:
467
   case T_EXTERNAL_NAME:
468
      return tree_class(t);
175,375✔
469
   case T_ENUM_LIT:
602,572✔
470
   case T_LITERAL:
471
   case T_STRING:
472
      return C_LITERAL;
602,572✔
473
   case T_FIELD_DECL:
1,594✔
474
   case T_ATTR_DECL:
475
      return C_DEFAULT;
1,594✔
476
   case T_UNIT_DECL:
218✔
477
      return C_UNITS;
218✔
478
   case T_ARCH:
42✔
479
      return C_ARCHITECTURE;
42✔
480
   case T_FUNC_DECL:
1,879✔
481
   case T_FUNC_BODY:
482
   case T_FUNC_INST:
483
   case T_FCALL:
484
      return C_FUNCTION;
1,879✔
485
   case T_PROC_DECL:
307✔
486
   case T_PROC_BODY:
487
   case T_PROC_INST:
488
   case T_PCALL:
489
      return C_PROCEDURE;
307✔
490
   case T_ENTITY:
104✔
491
      return C_ENTITY;
104✔
492
   case T_SUBTYPE_DECL:
24,668✔
493
      return C_SUBTYPE;
24,668✔
494
   case T_TYPE_DECL:
121,274✔
495
      return C_TYPE;
121,274✔
496
   case T_FILE_DECL:
1,535✔
497
      return C_FILE;
1,535✔
498
   case T_PROCESS:
39✔
499
   case T_BLOCK:
500
   case T_FOR:
501
   case T_INSTANCE:
502
   case T_CONCURRENT:
503
   case T_ELAB:
504
      return C_LABEL;
39✔
505
   case T_COMPONENT:
1✔
506
      return C_COMPONENT;
1✔
507
   case T_REF:
46,317✔
508
      return tree_has_ref(t) ? class_of(tree_ref(t)) : C_DEFAULT;
46,317✔
509
   case T_ARRAY_REF:
6,455✔
510
   case T_ARRAY_SLICE:
511
   case T_RECORD_REF:
512
   case T_ALL:
513
   case T_ALIAS:
514
      return class_of(tree_value(t));
6,455✔
515
   case T_PACKAGE:
1,202✔
516
   case T_PACK_BODY:
517
   case T_PACK_INST:
518
      return C_PACKAGE;
1,202✔
519
   case T_LIBRARY:
×
520
      return C_LIBRARY;
×
521
   case T_ATTR_REF:
14✔
522
      switch (tree_subkind(t)) {
14✔
523
      case ATTR_DELAYED:
524
      case ATTR_STABLE:
525
      case ATTR_QUIET:
526
      case ATTR_TRANSACTION:
527
         return C_SIGNAL;
528
      default:
11✔
529
         return C_DEFAULT;
11✔
530
      }
531
   default:
×
532
      fatal_trace("missing class_of for %s", tree_kind_str(tree_kind(t)));
×
533
   }
534
}
535

536
bool class_has_type(class_t c)
581,850✔
537
{
538
   switch (c) {
581,850✔
539
   case C_LABEL:
540
   case C_ENTITY:
541
   case C_ARCHITECTURE:
542
   case C_COMPONENT:
543
   case C_CONFIGURATION:
544
   case C_PACKAGE:
545
   case C_LIBRARY:
546
      return false;
547
   default:
580,721✔
548
      return true;
580,721✔
549
   }
550
}
551

552
const char *class_str(class_t c)
234✔
553
{
554
   static const char *strs[] = {
234✔
555
      "default", "signal", "variable", "constant", "file", "entity",
556
      "component", "configuration", "architecture", "function", "package",
557
      "type", "subtype", "label", "procedure", "literal", "units", "library"
558
   };
559
   assert(c < ARRAY_LEN(strs));
234✔
560
   return strs[c];
234✔
561
}
562

563
const char *assoc_kind_str(assoc_kind_t akind)
9✔
564
{
565
   switch (akind) {
9✔
566
   case A_NAMED:  return "named";
567
   case A_POS:    return "positional";
2✔
568
   case A_OTHERS: return "others";
3✔
569
   case A_RANGE:  return "range";
1✔
570
   default:       return "??";
×
571
   }
572
}
573

574
bool is_subprogram(tree_t t)
3,703,000✔
575
{
576
   switch (tree_kind(t)) {
3,703,000✔
577
   case T_FUNC_DECL:
578
   case T_FUNC_BODY:
579
   case T_FUNC_INST:
580
   case T_PROC_DECL:
581
   case T_PROC_BODY:
582
   case T_PROC_INST:
583
      return true;
584
   case T_GENERIC_DECL:
1,211✔
585
      {
586
         const class_t class = tree_class(t);
1,211✔
587
         return class == C_FUNCTION || class == C_PROCEDURE;
1,211✔
588
      }
589
   default:
978,736✔
590
      return false;
978,736✔
591
   }
592
}
593

594
bool is_loop_stmt(tree_t t)
363✔
595
{
596
   const tree_kind_t kind = tree_kind(t);
363✔
597
   return kind == T_WHILE || kind == T_FOR;
363✔
598
}
599

600
bool is_container(tree_t t)
52,753✔
601
{
602
   switch (tree_kind(t)) {
52,753✔
603
   case T_FUNC_BODY:
604
   case T_PROC_BODY:
605
   case T_ENTITY:
606
   case T_ARCH:
607
   case T_PACKAGE:
608
   case T_PACK_BODY:
609
   case T_CONFIGURATION:
610
   case T_BLOCK:
611
   case T_PROT_BODY:
612
   case T_ELAB:
613
   case T_FOR:
614
   case T_PROCESS:
615
   case T_PACK_INST:
616
      return true;
617
   default:
6,773✔
618
      return false;
6,773✔
619
   }
620
}
621

622
bool is_concurrent_block(tree_t t)
193✔
623
{
624
   switch (tree_kind(t)) {
193✔
625
   case T_ARCH:
626
   case T_ENTITY:
627
   case T_BLOCK:
628
   case T_IF_GENERATE:
629
   case T_FOR_GENERATE:
630
      return true;
631
   default:
95✔
632
      return false;
95✔
633
   }
634
}
635

636
bool is_package(tree_t t)
22,086✔
637
{
638
   switch (tree_kind(t)) {
22,086✔
639
   case T_PACKAGE:
640
   case T_PACK_BODY:
641
   case T_PACK_INST:
642
      return true;
643
   default:
596✔
644
      return false;
596✔
645
   }
646
}
647

648
bool is_design_unit(tree_t t)
8,983✔
649
{
650
   switch (tree_kind(t)) {
8,983✔
651
   case T_ENTITY:
652
   case T_ARCH:
653
   case T_PACKAGE:
654
   case T_PACK_BODY:
655
   case T_CONFIGURATION:
656
   case T_CONTEXT:
657
   case T_PACK_INST:
658
      return true;
659
   default:
828✔
660
      return false;
828✔
661
   }
662
}
663

664
bool is_guarded_signal(tree_t decl)
3,980✔
665
{
666
   return !!(tree_flags(decl) & (TREE_F_BUS | TREE_F_REGISTER));
3,980✔
667
}
668

669
bool is_type_decl(tree_t t)
17,682✔
670
{
671
   switch (tree_kind(t)) {
17,682✔
672
   case T_TYPE_DECL:
673
   case T_SUBTYPE_DECL:
674
      return true;
675
   default:
16,494✔
676
      return false;
16,494✔
677
   }
678
}
679

680
tree_t aliased_type_decl(tree_t decl)
1,902,650✔
681
{
682
   switch (tree_kind(decl)) {
1,913,120✔
683
   case T_ALIAS:
10,556✔
684
      {
685
         tree_t value = tree_value(decl);
10,556✔
686
         if (tree_kind(value) == T_REF && tree_has_ref(value))
10,556✔
687
            return aliased_type_decl(tree_ref(value));
10,474✔
688
         else
689
            return NULL;
82✔
690
      }
691
   case T_TYPE_DECL:
692
   case T_SUBTYPE_DECL:
693
      return decl;
694
   case T_GENERIC_DECL:
5,881✔
695
      if (tree_class(decl) == C_TYPE)
5,881✔
696
         return decl;
697
      else
698
         return NULL;
5,362✔
699
   default:
1,757,320✔
700
      return NULL;
1,757,320✔
701
   }
702
}
703

704
tree_t add_param(tree_t call, tree_t value, param_kind_t kind, tree_t name)
119,735✔
705
{
706
   tree_t p = tree_new(T_PARAM);
119,735✔
707
   tree_set_loc(p, tree_loc(value));
119,735✔
708
   tree_set_subkind(p, kind);
119,735✔
709
   tree_set_value(p, value);
119,735✔
710

711
   switch (kind) {
119,735✔
712
   case P_NAMED:
×
713
      assert(name != NULL);
×
714
      tree_set_name(p, name);
×
715
      break;
×
716
   case P_POS:
119,735✔
717
      tree_set_pos(p, tree_params(call));
119,735✔
718
      break;
119,735✔
719
   }
720

721
   tree_add_param(call, p);
119,735✔
722
   return p;
119,735✔
723
}
724

725
type_t array_aggregate_type(type_t array, int from_dim)
246✔
726
{
727
   if (type_is_none(array))
246✔
728
      return type_new(T_NONE);
25✔
729

730
   if (type_is_unconstrained(array)) {
221✔
731
      const int nindex = type_index_constrs(array);
33✔
732
      assert(from_dim < nindex);
33✔
733

734
      type_t type = type_new(T_ARRAY);
33✔
735
      type_set_ident(type, type_ident(array));
33✔
736
      type_set_elem(type, type_elem(array));
33✔
737

738
      for (int i = from_dim; i < nindex; i++)
66✔
739
         type_add_index_constr(type, type_index_constr(array, i));
33✔
740

741
      return type;
742
   }
743
   else {
744
      const int ndims = dimension_of(array);
188✔
745
      assert(from_dim < ndims);
188✔
746

747
      type_t base = type_new(T_ARRAY);
188✔
748
      type_set_ident(base, type_ident(array));
188✔
749
      type_set_elem(base, type_elem(array));
188✔
750

751
      tree_t constraint = tree_new(T_CONSTRAINT);
188✔
752
      tree_set_subkind(constraint, C_INDEX);
188✔
753

754
      type_t sub = type_new(T_SUBTYPE);
188✔
755
      type_set_base(sub, base);
188✔
756
      type_add_constraint(sub, constraint);
188✔
757

758
      for (int i = from_dim; i < ndims; i++) {
391✔
759
         tree_t r = range_of(array, i);
203✔
760
         type_add_index_constr(base, tree_type(r));
203✔
761
         tree_add_range(constraint, r);
203✔
762
      }
763

764
      return sub;
765
   }
766
}
767

768
unsigned bits_for_range(int64_t low, int64_t high)
1,989,720✔
769
{
770
   assert(low <= high);
1,989,720✔
771

772
   if (low < 0) {
1,989,720✔
773
      // Signed integers
774
      if (low >= INT8_MIN && high <= INT8_MAX)
581,490✔
775
         return 8;
776
      else if (low >= INT16_MIN && high <= INT16_MAX)
572,058✔
777
         return 16;
778
      else if (low >= INT32_MIN && high <= INT32_MAX)
571,655✔
779
         return 32;
780
      else
781
         return 64;
88,178✔
782
   }
783
   else {
784
      // Unsigned integers
785
      if (high <= 1)
1,408,230✔
786
         return 1;
787
      else if (high <= UINT8_MAX)
557,269✔
788
         return 8;
789
      else if (high <= UINT16_MAX)
75,495✔
790
         return 16;
791
      else if (high <= UINT32_MAX)
73,264✔
792
         return 32;
793
      else
794
         return 64;
2,103✔
795
   }
796
}
797

798
unsigned dimension_of(type_t type)
1,070,200✔
799
{
800
   switch (type_kind(type)) {
1,944,320✔
801
   case T_SUBTYPE:
874,119✔
802
      return dimension_of(type_base(type));
874,119✔
803
   case T_ARRAY:
1,067,560✔
804
      return type_index_constrs(type);
1,067,560✔
805
   case T_NONE:
806
   case T_ACCESS:
807
   case T_RECORD:
808
      return 0;
809
   case T_INTEGER:
2,548✔
810
   case T_REAL:
811
   case T_PHYSICAL:
812
   case T_ENUM:
813
      return type_dims(type);
2,548✔
814
   default:
×
815
      fatal_trace("invalid type kind %s in dimension_of",
×
816
                  type_kind_str(type_kind(type)));
817
   }
818
}
819

820
tree_t range_of(type_t type, unsigned dim)
1,266,010✔
821
{
822
   switch (type_kind(type)) {
1,294,670✔
823
   case T_SUBTYPE:
1,050,790✔
824
      if (type_constraints(type) > 0) {
1,050,790✔
825
         tree_t c0 = type_constraint(type, 0);
1,022,130✔
826
         switch (tree_subkind(c0)) {
1,022,130✔
827
         case C_OPEN:
1✔
828
            return range_of(type_base(type), dim);
1✔
829
         case C_INDEX:
1,022,130✔
830
         case C_RANGE:
831
            return tree_range(type_constraint(type, 0), dim);
1,022,130✔
832
         default:
×
833
            fatal_trace("invalid constraint in range_of");
×
834
         }
835
      }
836
      else
837
         return range_of(type_base(type), dim);
28,658✔
838
   case T_INTEGER:
243,884✔
839
   case T_REAL:
840
   case T_PHYSICAL:
841
   case T_ENUM:
842
      return type_dim(type, dim);
243,884✔
843
   default:
×
844
      fatal_trace("invalid type kind %s in range_of",
×
845
                  type_kind_str(type_kind(type)));
846
   }
847
}
848

849
range_kind_t direction_of(type_t type, unsigned dim)
11,995✔
850
{
851
   switch (type_kind(type)) {
12,039✔
852
   case T_ENUM:
853
      return RANGE_TO;
854
   case T_NONE:
×
855
      return RANGE_ERROR;
×
856
   case T_INTEGER:
11,970✔
857
   case T_REAL:
858
   case T_PHYSICAL:
859
   case T_SUBTYPE:
860
      {
861
         tree_t r = range_of(type, dim);
11,970✔
862
         const range_kind_t rkind = tree_subkind(r);
11,970✔
863
         if (rkind == RANGE_EXPR) {
11,970✔
864
            // Return a fixed direction if possible
865
            tree_t value = tree_value(r);
106✔
866
            assert(tree_kind(value) == T_ATTR_REF);
106✔
867

868
            DEBUG_ONLY({
106✔
869
                  const attr_kind_t attr = tree_subkind(value);
870
                  assert(attr == ATTR_RANGE || attr == ATTR_REVERSE_RANGE);
871
               });
106✔
872

873
            tree_t name = tree_name(value);
106✔
874
            if (tree_kind(name) == T_REF && tree_has_ref(name)) {
106✔
875
               tree_t decl = tree_ref(name);
105✔
876
               if (is_type_decl(decl))
105✔
877
                  return direction_of(tree_type(decl), 0);
44✔
878
            }
879
         }
880

881
         return rkind;
882
      }
883
   default:
×
884
      fatal_trace("invalid type kind %s in direction_of",
×
885
                  type_kind_str(type_kind(type)));
886
   }
887
}
888

889
type_t index_type_of(type_t type, unsigned dim)
194,427✔
890
{
891
   if (dim >= dimension_of(type))
194,427✔
892
      return NULL;
893

894
   type_t base = type_base_recur(type);
194,389✔
895
   type_kind_t base_kind = type_kind(base);
194,389✔
896
   if (base_kind == T_ARRAY)
194,389✔
897
      return type_index_constr(base, dim);
192,147✔
898
   else if (base_kind == T_ENUM || base_kind == T_NONE)
2,242✔
899
      return type;
900
   else if (base_kind == T_RECORD)
2,100✔
901
      return NULL;
902
   else
903
      return tree_type(range_of(base, dim));
2,100✔
904
}
905

906
int64_t rebase_index(type_t array_type, int dim, int64_t value)
240✔
907
{
908
   // Convert value which is in the range of array_type to a zero-based index
909
   tree_t r = range_of(array_type, dim);
240✔
910
   const int64_t left = assume_int(tree_left(r));
240✔
911
   return (tree_subkind(r) == RANGE_TO) ? value - left : left - value;
240✔
912
}
913

914
ident_t well_known(well_known_t id)
271,486✔
915
{
916
   assert(id < NUM_WELL_KNOWN);
271,486✔
917
   return id_cache[id];
271,486✔
918
}
919

920
well_known_t is_well_known(ident_t ident)
211,239✔
921
{
922
   well_known_t pos = 0;
211,239✔
923
   for (; pos < NUM_WELL_KNOWN; pos++) {
6,647,900✔
924
      if (id_cache[pos] == ident)
6,513,960✔
925
         break;
926
   }
927

928
   return pos;
211,239✔
929
}
930

931
void intern_strings(void)
3,450✔
932
{
933
   id_cache[W_STD_STANDARD]   = ident_new("STD.STANDARD");
3,450✔
934
   id_cache[W_ALL]            = ident_new("all");
3,450✔
935
   id_cache[W_STD_BIT]        = ident_new("STD.STANDARD.BIT");
3,450✔
936
   id_cache[W_STD_BOOL]       = ident_new("STD.STANDARD.BOOLEAN");
3,450✔
937
   id_cache[W_STD_CHAR]       = ident_new("STD.STANDARD.CHARACTER");
3,450✔
938
   id_cache[W_STD_NATURAL]    = ident_new("STD.STANDARD.NATURAL");
3,450✔
939
   id_cache[W_STD_POSITIVE]   = ident_new("STD.STANDARD.POSITIVE");
3,450✔
940
   id_cache[W_STD_INTEGER]    = ident_new("STD.STANDARD.INTEGER");
3,450✔
941
   id_cache[W_STD_STRING]     = ident_new("STD.STANDARD.STRING");
3,450✔
942
   id_cache[W_STD_REAL]       = ident_new("STD.STANDARD.REAL");
3,450✔
943
   id_cache[W_STD_TIME]       = ident_new("STD.STANDARD.TIME");
3,450✔
944
   id_cache[W_STD_BIT_VECTOR] = ident_new("STD.STANDARD.BIT_VECTOR");
3,450✔
945
   id_cache[W_IEEE_SIGNED]    = ident_new("IEEE.NUMERIC_STD.SIGNED");
3,450✔
946
   id_cache[W_IEEE_UNSIGNED]  = ident_new("IEEE.NUMERIC_STD.UNSIGNED");
3,450✔
947
   id_cache[W_IEEE_LOGIC]     = ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC");
3,450✔
948
   id_cache[W_IEEE_ULOGIC]    = ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC");
3,450✔
949
   id_cache[W_IEEE_1164_AND]  = ident_new("IEEE.STD_LOGIC_1164.\"and\"");
3,450✔
950
   id_cache[W_IEEE_1164_NAND] = ident_new("IEEE.STD_LOGIC_1164.\"nand\"");
3,450✔
951
   id_cache[W_IEEE_1164_OR]   = ident_new("IEEE.STD_LOGIC_1164.\"or\"");
3,450✔
952
   id_cache[W_IEEE_1164_NOR]  = ident_new("IEEE.STD_LOGIC_1164.\"nor\"");
3,450✔
953
   id_cache[W_IEEE_1164_XOR]  = ident_new("IEEE.STD_LOGIC_1164.\"xor\"");
3,450✔
954
   id_cache[W_IEEE_1164_XNOR] = ident_new("IEEE.STD_LOGIC_1164.\"xnor\"");
3,450✔
955
   id_cache[W_FOREIGN]        = ident_new("FOREIGN");
3,450✔
956
   id_cache[W_WORK]           = ident_new("WORK");
3,450✔
957
   id_cache[W_STD]            = ident_new("STD");
3,450✔
958
   id_cache[W_THUNK]          = ident_new("thunk");
3,450✔
959
   id_cache[W_BODY]           = ident_new("body");
3,450✔
960
   id_cache[W_CARET]          = ident_new("^");
3,450✔
961
   id_cache[W_IEEE]           = ident_new("IEEE");
3,450✔
962
   id_cache[W_IEEE_1164]      = ident_new("IEEE.STD_LOGIC_1164");
3,450✔
963
   id_cache[W_ERROR]          = ident_new("error");
3,450✔
964
   id_cache[W_CCONV]          = ident_new("\"??\"");
3,450✔
965
   id_cache[W_ELAB]           = ident_new("elab");
3,450✔
966
   id_cache[W_NUMERIC_STD]    = ident_new("IEEE.NUMERIC_STD");
3,450✔
967
   id_cache[W_NUMERIC_BIT]    = ident_new("IEEE.NUMERIC_BIT");
3,450✔
968
   id_cache[W_NVC]            = ident_new("NVC");
3,450✔
969
   id_cache[W_DEFAULT_CLOCK]  = ident_new("default clock");
3,450✔
970
   id_cache[W_DOLLAR_DISPLAY] = ident_new("$display");
3,450✔
971
   id_cache[W_DOLLAR_FINISH]  = ident_new("$finish");
3,450✔
972

973
   id_cache[W_IEEE_LOGIC_VECTOR] =
6,900✔
974
      ident_new("IEEE.STD_LOGIC_1164.STD_LOGIC_VECTOR");
3,450✔
975
   id_cache[W_IEEE_ULOGIC_VECTOR] =
6,900✔
976
      ident_new("IEEE.STD_LOGIC_1164.STD_ULOGIC_VECTOR");
3,450✔
977

978
   id_cache[W_NUMERIC_STD_UNSIGNED] = ident_new("IEEE.NUMERIC_STD_UNSIGNED");
3,450✔
979
   id_cache[W_NUMERIC_BIT_UNSIGNED] = ident_new("IEEE.NUMERIC_BIT_UNSIGNED");
3,450✔
980
}
3,450✔
981

982
bool is_uninstantiated_package(tree_t pack)
49,307✔
983
{
984
   return tree_kind(pack) == T_PACKAGE
49,307✔
985
      && tree_generics(pack) > 0
44,194✔
986
      && tree_genmaps(pack) == 0;
51,532✔
987
}
988

989
bool is_uninstantiated_subprogram(tree_t decl)
74,120✔
990
{
991
   switch (tree_kind(decl)) {
74,120✔
992
   case T_FUNC_DECL:
73,979✔
993
   case T_FUNC_BODY:
994
   case T_PROC_DECL:
995
   case T_PROC_BODY:
996
      return tree_generics(decl) > 0;
73,979✔
997
   default:
998
      return false;
999
   }
1000
}
1001

1002
bool unit_needs_cgen(tree_t unit)
7,858✔
1003
{
1004
   switch (tree_kind(unit)) {
7,858✔
1005
   case T_PACK_INST:
1006
      return true;
1007
   case T_PACK_BODY:
508✔
1008
      {
1009
         tree_t pack = tree_primary(unit);
508✔
1010
         return package_needs_body(pack) && !is_uninstantiated_package(pack);
578✔
1011
      }
1012
   case T_PACKAGE:
799✔
1013
      return !package_needs_body(unit) && !is_uninstantiated_package(unit);
816✔
1014
   default:
6,422✔
1015
      return false;
6,422✔
1016
   }
1017
}
1018

1019
bool package_needs_body(tree_t pack)
1,721✔
1020
{
1021
   assert(tree_kind(pack) == T_PACKAGE);
1,721✔
1022

1023
   const int ndecls = tree_decls(pack);
1,721✔
1024
   for (int i = 0; i < ndecls; i++) {
33,748✔
1025
      tree_t d = tree_decl(pack, i);
33,142✔
1026

1027
      switch (tree_kind(d)) {
33,142✔
1028
      case T_FUNC_DECL:
26,535✔
1029
      case T_PROC_DECL:
1030
         if (tree_flags(d) & TREE_F_PREDEFINED)
26,535✔
1031
            continue;
25,612✔
1032
         else if (tree_subkind(d) == S_FOREIGN)
923✔
1033
            continue;
27✔
1034
         return true;
1035

1036
      case T_CONST_DECL:
1,506✔
1037
         if (!tree_has_value(d))
1,506✔
1038
            return true;
1039
         continue;
1,400✔
1040

1041
      case T_TYPE_DECL:
3,093✔
1042
         if (type_is_protected(tree_type(d)))
3,093✔
1043
            return true;
1044
         continue;
2,980✔
1045

1046
      default:
2,008✔
1047
         continue;
2,008✔
1048
      }
1049
   }
1050

1051
   return false;
1052
}
1053

1054
tree_t search_decls(tree_t container, ident_t name, int nth)
28,154✔
1055
{
1056
   type_t type;
28,154✔
1057
   tree_kind_t kind = tree_kind(container);
28,154✔
1058
   if (kind == T_LIBRARY) {
28,154✔
1059
      if (nth == 0) {
×
1060
         lib_t lib = lib_require(tree_ident(container));
×
1061
         return lib_get(lib, name);
×
1062
      }
1063
      else
1064
         return NULL;
1065
   }
1066
   else if ((kind == T_VAR_DECL || kind == T_PARAM_DECL)
28,154✔
1067
            && type_is_protected((type = tree_type(container)))) {
×
1068
      const int ndecls = type_decls(type);
×
1069
      for (int i = 0; i < ndecls; i++) {
×
1070
         tree_t d = type_decl(type, i);
×
1071
         if (tree_ident(d) == name && nth-- == 0)
×
1072
            return d;
×
1073
      }
1074
      return NULL;
1075
   }
1076
   else if (kind == T_ENTITY || kind == T_BLOCK) {
28,154✔
1077
      const int nports = tree_ports(container);
254✔
1078
      for (int i = 0; i < nports; i++) {
279✔
1079
         tree_t p = tree_port(container, i);
25✔
1080
         if (tree_ident(p) == name && nth-- == 0)
25✔
1081
            return p;
×
1082
      }
1083
   }
1084
   else if (!is_container(container))
27,900✔
1085
      return NULL;
1086

1087
   // TODO: how to improve this?
1088
   const int ndecls = tree_decls(container);
28,154✔
1089
   tree_t best = NULL;
28,154✔
1090

1091
   for (int i = 0; i < ndecls; i++) {
1,536,220✔
1092
      tree_t d = tree_decl(container, i);
1,536,060✔
1093
      if (!tree_has_ident(d))
1,536,060✔
1094
         continue;
×
1095
      else if (tree_ident(d) == name) {
1,536,060✔
1096
         if (tree_kind(d) == T_TYPE_DECL
18,609✔
1097
             && type_kind(tree_type(d)) == T_INCOMPLETE)
17,440✔
1098
               best = d;
1099
         else if (nth-- == 0)
18,609✔
1100
            return d;
18,434✔
1101
      }
1102
      else if (tree_kind(d) == T_TYPE_DECL) {
1,517,450✔
1103
         type_t type = tree_type(d);
120,847✔
1104
         switch (type_kind(type)) {
120,847✔
1105
         case T_ENUM:
91,537✔
1106
            {
1107
               const int nlits = type_enum_literals(type);
91,537✔
1108
               for (int j = 0; j < nlits; j++) {
6,157,530✔
1109
                  tree_t lit = type_enum_literal(type, j);
6,075,550✔
1110
                  if (tree_ident(lit) == name && nth-- == 0)
6,075,550✔
1111
                     return lit;
9,556✔
1112
               }
1113
            }
1114
            break;
1115
         default:
1116
            break;
1117
         }
1118
      }
1,396,600✔
1119
   }
1120

1121
   return best;
1122
}
1123

1124
static tree_t cached_unit(tree_t hint, tree_t *cache, well_known_t lib_name,
17,426✔
1125
                          well_known_t unit_name)
1126
{
1127
   const vhdl_standard_t curr = standard();
17,426✔
1128

1129
   if (cache[curr] == NULL) {
17,426✔
1130
      if (hint != NULL)
2,931✔
1131
         cache[curr] = hint;
854✔
1132
      else {
1133
         lib_t std = lib_require(well_known(lib_name));
2,077✔
1134
         cache[curr] = lib_get(std, well_known(unit_name));
2,077✔
1135
         assert(cache[curr] != NULL);
2,077✔
1136
      }
1137
   }
1138

1139
   assert(hint == NULL || hint == cache[curr]);
17,426✔
1140
   return cache[curr];
17,426✔
1141
}
1142

1143
static tree_t cached_std(tree_t hint)
17,404✔
1144
{
1145
   static tree_t standard_cache[STD_19 + 1] = {};
17,404✔
1146
   return cached_unit(hint, standard_cache, W_STD, W_STD_STANDARD);
17,404✔
1147
}
1148

1149
type_t std_type(tree_t std, std_type_t which)
1,799,370✔
1150
{
1151
   static type_t cache[STD_FILE_OPEN_STATE + 1] = {};
1,799,370✔
1152
   assert(which < ARRAY_LEN(cache));
1,799,370✔
1153

1154
   if (cache[which] == NULL) {
1,799,370✔
1155
      const char *names[] = {
17,404✔
1156
         "universal_integer",
1157
         "universal_real",
1158
         "INTEGER",
1159
         "REAL",
1160
         "BOOLEAN",
1161
         "STRING",
1162
         "TIME",
1163
         "BIT",
1164
         "FILE_OPEN_KIND",
1165
         "FILE_OPEN_STATUS",
1166
         "NATURAL",
1167
         "BIT_VECTOR",
1168
         "SEVERITY_LEVEL",
1169
         "FILE_ORIGIN_KIND",
1170
         "FILE_OPEN_STATE",
1171
      };
1172

1173
      tree_t d = search_decls(cached_std(std), ident_new(names[which]), 0);
17,404✔
1174
      if (d == NULL)
17,404✔
1175
         fatal_trace("cannot find standard type %s", names[which]);
×
1176

1177
      // Do not cache standard types while bootstrapping as the GC will
1178
      // move the objects after parsing
1179
      static int can_cache = -1;
17,404✔
1180
      if (can_cache == -1) can_cache = !opt_get_int(OPT_BOOTSTRAP);
17,404✔
1181

1182
      if (can_cache)
17,404✔
1183
         return (cache[which] = tree_type(d));
16,543✔
1184
      else
1185
         return tree_type(d);
861✔
1186
   }
1187
   else
1188
      return cache[which];
1189
}
1190

1191
static tree_t cached_ieee(tree_t hint)
22✔
1192
{
1193
   static tree_t ieee_cache[STD_19 + 1] = {};
22✔
1194
   return cached_unit(hint, ieee_cache, W_IEEE, W_IEEE_1164);
22✔
1195
}
1196

1197
type_t ieee_type(ieee_type_t which)
166✔
1198
{
1199
   static type_t cache[IEEE_STD_LOGIC + 1] = {};
166✔
1200
   assert(which < ARRAY_LEN(cache));
166✔
1201

1202
   if (cache[which] == NULL) {
166✔
1203
      const char *names[] = {
22✔
1204
         "STD_ULOGIC",
1205
         "STD_LOGIC",
1206
      };
1207

1208
      tree_t d = search_decls(cached_ieee(NULL), ident_new(names[which]), 0);
22✔
1209
      if (d == NULL)
22✔
1210
         fatal_trace("cannot find IEEE type %s", names[which]);
×
1211

1212
      // STD.STANDARD cannot depend on IEEE
1213
      assert(!opt_get_int(OPT_BOOTSTRAP));
22✔
1214

1215
      return (cache[which] = tree_type(d));
22✔
1216
   }
1217
   else
1218
      return cache[which];
1219
}
1220

1221
bool is_builtin(subprogram_kind_t kind)
12,810,000✔
1222
{
1223
   return kind != S_USER && kind != S_FOREIGN;
12,810,000✔
1224
}
1225

1226
bool is_open_coded_builtin(subprogram_kind_t kind)
351,150✔
1227
{
1228
   switch (kind) {
351,150✔
1229
   case S_USER:
1230
   case S_FOREIGN:
1231
   case S_ARRAY_EQ:
1232
   case S_ARRAY_NEQ:
1233
   case S_ARRAY_LT:
1234
   case S_ARRAY_LE:
1235
   case S_ARRAY_GT:
1236
   case S_ARRAY_GE:
1237
   case S_RECORD_EQ:
1238
   case S_RECORD_NEQ:
1239
   case S_TO_STRING:
1240
   case S_SLL:
1241
   case S_SRL:
1242
   case S_SLA:
1243
   case S_SRA:
1244
   case S_ROL:
1245
   case S_ROR:
1246
   case S_ARRAY_NOT:
1247
   case S_ARRAY_AND:
1248
   case S_ARRAY_OR:
1249
   case S_ARRAY_XOR:
1250
   case S_ARRAY_XNOR:
1251
   case S_ARRAY_NAND:
1252
   case S_ARRAY_NOR:
1253
   case S_MIXED_AND:
1254
   case S_MIXED_OR:
1255
   case S_MIXED_XOR:
1256
   case S_MIXED_XNOR:
1257
   case S_MIXED_NAND:
1258
   case S_MIXED_NOR:
1259
   case S_REDUCE_OR:
1260
   case S_REDUCE_AND:
1261
   case S_REDUCE_NAND:
1262
   case S_REDUCE_NOR:
1263
   case S_REDUCE_XOR:
1264
   case S_REDUCE_XNOR:
1265
   case S_MATCH_EQ:
1266
   case S_MATCH_NEQ:
1267
   case S_MATCH_LT:
1268
   case S_MATCH_LE:
1269
   case S_MATCH_GT:
1270
   case S_MATCH_GE:
1271
   case S_MINIMUM:
1272
   case S_MAXIMUM:
1273
      return false;
1274
   default:
240,719✔
1275
      return true;
240,719✔
1276
   }
1277
}
1278

1279
tree_t std_func(ident_t mangled)
×
1280
{
1281
   tree_t std = cached_std(NULL);
×
1282

1283
   const int ndecls = tree_decls(std);
×
1284
   for (int i = 0; i < ndecls; i++) {
×
1285
      tree_t d = tree_decl(std, i);
×
1286
      if (is_subprogram(d) && tree_has_ident2(d) && tree_ident2(d) == mangled)
×
1287
         return d;
×
1288
   }
1289

1290
   return NULL;
1291
}
1292

1293
tree_t name_to_ref(tree_t name)
66,984✔
1294
{
1295
   tree_kind_t kind;
66,984✔
1296
   while ((kind = tree_kind(name)) != T_REF) {
73,347✔
1297
      switch (kind) {
6,456✔
1298
      case T_ARRAY_REF:
6,363✔
1299
      case T_ARRAY_SLICE:
1300
      case T_RECORD_REF:
1301
      case T_ALL:
1302
         name = tree_value(name);
6,363✔
1303
         break;
6,363✔
1304
      case T_ATTR_REF:
1305

1306
      default:
1307
         return NULL;
1308
      }
1309
   }
1310

1311
   return name;
1312
}
1313

1314
const char *port_mode_str(port_mode_t mode)
14✔
1315
{
1316
   const char *mode_str[] = {
14✔
1317
      "INVALID", "IN", "OUT", "INOUT", "BUFFER", "LINKAGE"
1318
   };
1319
   assert(mode < ARRAY_LEN(mode_str));
14✔
1320
   return mode_str[mode];
14✔
1321
}
1322

1323
void mangle_one_type(text_buf_t *buf, type_t type)
172,845✔
1324
{
1325
   ident_t ident = type_ident(type);
172,845✔
1326

1327
   char code = 0;
172,845✔
1328
   switch (is_well_known(ident)) {
172,845✔
1329
   case W_STD_INTEGER:        code = 'I'; break;
1330
   case W_STD_STRING:         code = 'S'; break;
2,888✔
1331
   case W_STD_REAL:           code = 'R'; break;
2,952✔
1332
   case W_STD_BOOL:           code = 'B'; break;
29,425✔
1333
   case W_STD_CHAR:           code = 'C'; break;
593✔
1334
   case W_STD_TIME:           code = 'T'; break;
873✔
1335
   case W_STD_NATURAL:        code = 'N'; break;
×
1336
   case W_STD_POSITIVE:       code = 'P'; break;
×
1337
   case W_STD_BIT:            code = 'J'; break;
2,275✔
1338
   case W_STD_BIT_VECTOR:     code = 'Q'; break;
2,131✔
1339
   case W_IEEE_LOGIC:         code = 'L'; break;
×
1340
   case W_IEEE_ULOGIC:        code = 'U'; break;
4,416✔
1341
   case W_IEEE_LOGIC_VECTOR:  code = 'V'; break;
841✔
1342
   case W_IEEE_ULOGIC_VECTOR: code = 'Y'; break;
1,345✔
1343
   default: break;
1344
   }
1345

1346
   if (code)
47,739✔
1347
      tb_append(buf, code);
58,715✔
1348
   else {
1349
      tb_printf(buf, "%zu", ident_len(ident));
114,130✔
1350
      tb_istr(buf, ident);
114,130✔
1351
   }
1352
}
172,845✔
1353

1354
tree_t primary_unit_of(tree_t unit)
4,317✔
1355
{
1356
   switch (tree_kind(unit)) {
4,317✔
1357
   case T_ENTITY:
1358
   case T_COMPONENT:
1359
   case T_PACKAGE:
1360
      return unit;
1361
   case T_ARCH:
477✔
1362
   case T_CONFIGURATION:
1363
   case T_PACK_BODY:
1364
      return tree_primary(unit);
477✔
1365
   default:
×
1366
      fatal_trace("invalid kind %s in primary_unit_of",
×
1367
                  tree_kind_str(tree_kind(unit)));
1368
   }
1369
}
1370

1371
unsigned get_case_choice_char(tree_t value, int depth)
7,668✔
1372
{
1373
   switch (tree_kind(value)) {
7,668✔
1374
   case T_STRING:
6,918✔
1375
      if (depth < tree_chars(value)) {
6,918✔
1376
         tree_t ch = tree_char(value, depth);
6,917✔
1377
         return tree_pos(tree_ref(ch));
6,917✔
1378
      }
1379
      else
1380
         return ~0;   // Out of bounds
1381

1382
   case T_AGGREGATE:
14✔
1383
      {
1384
         const int nassocs = tree_assocs(value);
14✔
1385
         type_t type = tree_type(value);
14✔
1386

1387
         for (int i = 0; i < nassocs; i++) {
24✔
1388
            tree_t a = tree_assoc(value, i);
23✔
1389
            switch (tree_subkind(a)) {
23✔
1390
            case A_NAMED:
×
1391
               if (rebase_index(type, 0, assume_int(tree_name(a))) == depth)
×
1392
                  return assume_int(tree_value(a));
×
1393
               break;
1394

1395
            case A_POS:
23✔
1396
               if (tree_pos(a) == (unsigned)depth)
23✔
1397
                  return assume_int(tree_value(a));
13✔
1398
               break;
1399

1400
            case A_OTHERS:
×
1401
               return assume_int(tree_value(a));
×
1402
            }
1403
         }
10✔
1404

1405
         // This will produce an error during bounds checking
1406
         return ~0;
1407
      }
1408

1409
   case T_REF:
392✔
1410
      {
1411
         tree_t decl = tree_ref(value);
392✔
1412
         assert(tree_kind(decl) == T_CONST_DECL || tree_kind(decl) == T_ALIAS);
392✔
1413
         assert(tree_has_value(decl));
392✔
1414
         return get_case_choice_char(tree_value(decl), depth);
392✔
1415
      }
1416

1417
   case T_ARRAY_SLICE:
240✔
1418
      {
1419
         tree_t base = tree_value(value);
240✔
1420
         tree_t r = tree_range(value, 0);
240✔
1421
         const int64_t rleft = assume_int(tree_left(r));
240✔
1422
         const int64_t offset = rebase_index(tree_type(base), 0, rleft);
240✔
1423
         return get_case_choice_char(base, depth + offset);
240✔
1424
      }
1425

1426
   case T_FCALL:
104✔
1427
      if (tree_subkind(tree_ref(value)) == S_CONCAT) {
104✔
1428
         const int nparams = tree_params(value);
104✔
1429
         for (int i = 0; i < nparams; i++) {
156✔
1430
            tree_t left = tree_value(tree_param(value, i));
156✔
1431

1432
            tree_t lr = range_of(tree_type(left), 0);
156✔
1433
            int64_t left_len;
156✔
1434
            if (!folded_length(lr, &left_len))
156✔
1435
               fatal_at(tree_loc(left), "cannot determine length of left hand "
×
1436
                        "side of concatenation");
1437

1438
            if (depth < left_len || i + 1 == nparams)
156✔
1439
               return get_case_choice_char(left, depth);
104✔
1440

1441
            depth -= left_len;
52✔
1442
         }
1443
      }
1444
      // Fall-through
1445

1446
   default:
1447
      fatal_at(tree_loc(value), "unsupported tree type %s in case choice",
×
1448
               tree_kind_str(tree_kind(value)));
1449
   }
1450
}
1451

1452
int64_t encode_case_choice(tree_t value, int length, int bits)
1,047✔
1453
{
1454
   uint64_t enc = 0;
1,047✔
1455
   for (int i = 0; i < length; i++) {
7,973✔
1456
      if (bits > 0) {
6,926✔
1457
         enc <<= bits;
1,765✔
1458
         enc |= get_case_choice_char(value, i);
1,765✔
1459
      }
1460
      else {
1461
         enc *= 0x27d4eb2d;
5,161✔
1462
         enc += get_case_choice_char(value, i);
5,161✔
1463
      }
1464
   }
1465

1466
   return enc;
1,047✔
1467
}
1468

1469
void to_string(text_buf_t *tb, type_t type, int64_t value)
481✔
1470
{
1471
   if (type_is_integer(type))
481✔
1472
      tb_printf(tb, "%"PRIi64, value);
347✔
1473
   else if (type_is_enum(type)) {
134✔
1474
      type_t base = type_base_recur(type);
62✔
1475
      if (value < 0 || value >= type_enum_literals(base))
62✔
1476
         tb_printf(tb, "%"PRIi64, value);
3✔
1477
      else
1478
         tb_cat(tb, istr(tree_ident(type_enum_literal(base, value))));
59✔
1479
   }
1480
   else if (type_is_physical(type)) {
72✔
1481
      type_t base = type_base_recur(type);
24✔
1482
      const unsigned nunits = type_units(base);
24✔
1483
      tree_t max_unit = NULL;
24✔
1484
      int64_t max_unit_value = 0;
24✔
1485

1486
      // Find the largest unit that evenly divides the given value
1487
      for (unsigned u = 0; u < nunits; u++) {
216✔
1488
         tree_t unit = type_unit(base, u);
192✔
1489
         const int64_t unit_value = assume_int(tree_value(unit));
192✔
1490
         if ((unit_value > max_unit_value) && (value % unit_value == 0)) {
192✔
1491
            max_unit = unit;
90✔
1492
            max_unit_value = unit_value;
90✔
1493
         }
1494
      }
1495
      assert(max_unit);
24✔
1496

1497
      tb_printf(tb, "%"PRIi64" %s", value / max_unit_value,
24✔
1498
                istr(tree_ident(max_unit)));
1499
   }
1500
   else if (type_is_real(type)) {
48✔
1501
      union { int64_t i; double r; } u = { .i = value };
42✔
1502
      tb_printf(tb, "%.17g", u.r);
42✔
1503
   }
1504
   else if (type_is_access(type)) {
6✔
1505
      if (value == 0)
6✔
1506
         tb_cat(tb, "NULL");
3✔
1507
      else
1508
         tb_printf(tb, "%p", (void *)value);
3✔
1509
   }
1510
}
481✔
1511

1512
static bool is_static(tree_t expr)
1,911✔
1513
{
1514
   switch (tree_kind(expr)) {
1,969✔
1515
   case T_REF:
197✔
1516
      {
1517
         tree_t decl = tree_ref(expr);
197✔
1518
         switch (tree_kind(decl)) {
197✔
1519
         case T_CONST_DECL:
1520
         case T_UNIT_DECL:
1521
         case T_ENUM_LIT:
1522
         case T_GENERIC_DECL:
1523
            return true;
1524
         case T_ALIAS:
×
1525
            return is_static(tree_value(decl));
×
1526
         default:
95✔
1527
            return false;
95✔
1528
         }
1529
      }
1530

1531
   case T_LITERAL:
1532
   case T_STRING:
1533
      return true;
1534

1535
   case T_FCALL:
57✔
1536
      return !!(tree_flags(expr) & (TREE_F_LOCALLY_STATIC
57✔
1537
                                    | TREE_F_GLOBALLY_STATIC));
1538

1539
   case T_RECORD_REF:
58✔
1540
      return is_static(tree_value(expr));
58✔
1541

1542
   default:
×
1543
      return false;
×
1544
   }
1545
}
1546

1547
tree_t longest_static_prefix(tree_t expr)
9,568✔
1548
{
1549
   switch (tree_kind(expr)) {
9,568✔
1550
   case T_ARRAY_REF:
1,440✔
1551
      {
1552
         tree_t value = tree_value(expr);
1,440✔
1553
         tree_t prefix = longest_static_prefix(value);
1,440✔
1554

1555
         if (prefix != value)
1,440✔
1556
            return prefix;
1557

1558
         const int nparams = tree_params(expr);
1,430✔
1559
         for (int i = 0; i < nparams; i++) {
2,954✔
1560
            if (!is_static(tree_value(tree_param(expr, i))))
1,637✔
1561
               return prefix;
1562
         }
1563

1564
         return expr;
1565
      }
1566

1567
   case T_ARRAY_SLICE:
153✔
1568
      {
1569
         tree_t value = tree_value(expr);
153✔
1570
         tree_t prefix = longest_static_prefix(value);
153✔
1571

1572
         if (prefix != value)
153✔
1573
            return prefix;
1574

1575
         const int nranges = tree_ranges(expr);
146✔
1576
         for (int i = 0; i < nranges; i++) {
276✔
1577
            tree_t r = tree_range(expr, i);
146✔
1578
            if (tree_subkind(r) == RANGE_EXPR)
146✔
1579
               return prefix;
1580
            else if (!is_static(tree_left(r)) || !is_static(tree_right(r)))
143✔
1581
               return prefix;
13✔
1582
         }
1583

1584
         return expr;
1585
      }
1586

1587
   case T_RECORD_REF:
418✔
1588
      {
1589
         tree_t value = tree_value(expr);
418✔
1590
         tree_t prefix = longest_static_prefix(value);
418✔
1591

1592
         if (prefix != value)
418✔
1593
            return prefix;
×
1594

1595
         return expr;
1596
      }
1597

1598
   default:
1599
      return expr;
1600
   }
1601
}
1602

1603
tree_t body_of(tree_t pack)
8,506✔
1604
{
1605
   const tree_kind_t kind = tree_kind(pack);
8,506✔
1606
   if (kind == T_PACK_INST)
8,506✔
1607
      return NULL;
1608

1609
   assert(tree_kind(pack) == T_PACKAGE);
8,436✔
1610

1611
   ident_t body_i = well_known(W_BODY);
8,436✔
1612
   ident_t body_name = ident_prefix(tree_ident(pack), body_i, '-');
8,436✔
1613
   return lib_get_qualified(body_name);
8,436✔
1614
}
1615

1616
tree_t find_generic_map(tree_t unit, int pos, tree_t g)
859✔
1617
{
1618
   const int ngenmaps = tree_genmaps(unit);
859✔
1619

1620
   if (pos < ngenmaps) {
859✔
1621
      tree_t m = tree_genmap(unit, pos);
859✔
1622
      if (tree_subkind(m) == P_POS && tree_pos(m) == pos)
859✔
1623
         return tree_value(m);
424✔
1624
   }
1625

1626
   for (int j = 0; j < ngenmaps; j++) {
1,127✔
1627
      tree_t m = tree_genmap(unit, j);
1,127✔
1628
      switch (tree_subkind(m)) {
1,127✔
1629
      case P_NAMED:
832✔
1630
         {
1631
            tree_t name = tree_name(m);
832✔
1632
            assert(tree_kind(name) == T_REF);
832✔
1633

1634
            if (tree_ref(name) == g)
832✔
1635
               return tree_value(m);
283✔
1636
         }
1637
         break;
1638

1639
      case P_POS:
295✔
1640
         if (tree_pos(m) == pos)
295✔
1641
            return tree_value(m);
152✔
1642
         break;
1643

1644
      default:
1645
         break;
1646
      }
1647
   }
1648

1649
   return NULL;
1650
}
1651

1652
int pack_constraints(type_t type, tree_t out[MAX_CONSTRAINTS])
65,192✔
1653
{
1654
   if (type_kind(type) != T_SUBTYPE)
65,192✔
1655
      return 0;
1656

1657
   const int ncon = type_constraints(type);
58,461✔
1658

1659
   int ptr = 0;
58,461✔
1660
   type_t base = type_base(type);
58,461✔
1661
   if (type_kind(base) == T_SUBTYPE) {
58,461✔
1662
      ptr = pack_constraints(base, out);
4,221✔
1663

1664
      for (int i = 0, pos = 0; i < ptr && pos < ncon; i++) {
8,413✔
1665
         if (tree_subkind(out[i]) == C_OPEN)
4,192✔
1666
            out[i] = type_constraint(type, pos++);
4,192✔
1667
      }
1668
   }
1669
   else {
1670
      for (int i = 0; i < ncon; i++) {
108,215✔
1671
         tree_t c = type_constraint(type, i);
53,975✔
1672
         switch (tree_subkind(c)) {
53,975✔
1673
         case C_INDEX:
53,353✔
1674
         case C_RECORD:
1675
         case C_OPEN:
1676
            if (ptr == MAX_CONSTRAINTS)
53,353✔
1677
               fatal_at(tree_loc(c), "sorry, a maximum of %d nested "
×
1678
                        "constraints are supported", MAX_CONSTRAINTS);
1679
            out[ptr++] = c;
53,353✔
1680
            break;
53,353✔
1681
         }
1682
      }
53,975✔
1683
   }
1684

1685
   return ptr;
1686
}
1687

1688
bool relaxed_rules(void)
777,139✔
1689
{
1690
   return opt_get_int(OPT_RELAXED);
777,139✔
1691
}
1692

1693
bool is_type_attribute(attr_kind_t kind)
37,438✔
1694
{
1695
   switch (kind) {
37,438✔
1696
   case ATTR_SUBTYPE:
1697
   case ATTR_BASE:
1698
   case ATTR_ELEMENT:
1699
      return true;
1700
   default:
37,256✔
1701
      return false;
37,256✔
1702
   }
1703
}
1704

1705
type_t get_type_or_null(tree_t t)
1,330,300✔
1706
{
1707
   switch (tree_kind(t)) {
1,330,300✔
1708
   case T_LIBRARY:
1709
   case T_ATTR_SPEC:
1710
   case T_PACKAGE:
1711
   case T_PACK_INST:
1712
   case T_PACK_BODY:
1713
   case T_ENTITY:
1714
   case T_ARCH:
1715
   case T_PROCESS:
1716
   case T_COMPONENT:
1717
   case T_INSTANCE:
1718
   case T_CONCURRENT:
1719
   case T_BLOCK:
1720
   case T_WHILE:
1721
   case T_FOR:
1722
   case T_GROUP_TEMPLATE:
1723
   case T_CONFIGURATION:
1724
   case T_GROUP:
1725
   case T_FOR_GENERATE:
1726
   case T_IF_GENERATE:
1727
   case T_USE:
1728
   case T_CONTEXT:
1729
   case T_PSL:
1730
      return NULL;
1731
   default:
1,277,180✔
1732
      if (tree_has_type(t))
1,277,180✔
1733
         return tree_type(t);
1,276,760✔
1734
      else
1735
         return NULL;
1736
   }
1737
}
1738

1739
type_t subtype_for_string(tree_t str, type_t base)
12,629✔
1740
{
1741
   if (!type_is_unconstrained(base))
12,629✔
1742
      return base;
1743

1744
   // Construct a new constrained array subtype: the direction and
1745
   // bounds are the same as those for a positional array aggregate
1746
   type_t sub = type_new(T_SUBTYPE);
10,215✔
1747
   type_set_base(sub, base);
10,215✔
1748

1749
   type_t index_type = index_type_of(base, 0);
10,215✔
1750
   const bool is_enum = type_is_enum(index_type);
10,215✔
1751

1752
   // The direction is determined by the index type
1753
   range_kind_t dir = direction_of(index_type, 0);
10,215✔
1754

1755
   // The left bound is the left of the index type and the right bound
1756
   // is determined by the number of elements
1757

1758
   tree_t left = NULL, right = NULL;
10,215✔
1759

1760
   if (is_enum)
10,215✔
1761
      left = make_ref(type_enum_literal(type_base_recur(index_type), 0));
1✔
1762
   else
1763
      left = tree_left(range_of(index_type, 0));
10,214✔
1764

1765
   const int nchars = tree_chars(str);
10,215✔
1766
   int64_t iright;
10,215✔
1767
   if (dir == RANGE_DOWNTO)
10,215✔
1768
      iright = assume_int(left) - nchars + 1;
×
1769
   else
1770
      iright = assume_int(left) + nchars - 1;
10,215✔
1771

1772
   if (is_enum)
10,215✔
1773
      right = get_enum_lit(str, index_type, iright);
1✔
1774
   else
1775
      right = get_int_lit(str, index_type, iright);
10,214✔
1776

1777
   tree_t r = tree_new(T_RANGE);
10,215✔
1778
   tree_set_subkind(r, dir);
10,215✔
1779
   tree_set_left(r, left);
10,215✔
1780
   tree_set_right(r, right);
10,215✔
1781
   tree_set_loc(r, tree_loc(str));
10,215✔
1782

1783
   tree_t c = tree_new(T_CONSTRAINT);
10,215✔
1784
   tree_set_subkind(c, C_INDEX);
10,215✔
1785
   tree_add_range(c, r);
10,215✔
1786
   tree_set_loc(c, tree_loc(str));
10,215✔
1787

1788
   type_add_constraint(sub, c);
10,215✔
1789

1790
   return sub;
10,215✔
1791
}
1792

1793
tree_t change_ref(tree_t name, tree_t new)
1,632✔
1794
{
1795
   switch (tree_kind(name)) {
1,632✔
1796
   case T_REF:
1,115✔
1797
      return make_ref(new);
1,115✔
1798

1799
   case T_ARRAY_REF:
71✔
1800
      {
1801
         tree_t t = tree_new(T_ARRAY_REF);
71✔
1802
         tree_set_loc(t, tree_loc(name));
71✔
1803
         tree_set_value(t, change_ref(tree_value(name), new));
71✔
1804
         tree_set_type(t, tree_type(name));
71✔
1805

1806
         const int nparams = tree_params(name);
71✔
1807
         for (int i = 0; i < nparams; i++)
142✔
1808
            tree_add_param(t, tree_param(name, i));
71✔
1809

1810
         return t;
1811
      }
1812

1813
   case T_ARRAY_SLICE:
38✔
1814
      {
1815
         tree_t t = tree_new(T_ARRAY_SLICE);
38✔
1816
         tree_set_loc(t, tree_loc(name));
38✔
1817
         tree_set_value(t, change_ref(tree_value(name), new));
38✔
1818
         tree_set_type(t, tree_type(name));
38✔
1819
         tree_add_range(t, tree_range(name, 0));
38✔
1820

1821
         return t;
38✔
1822
      }
1823

1824
   case T_RECORD_REF:
351✔
1825
      {
1826
         tree_t t = tree_new(T_RECORD_REF);
351✔
1827
         tree_set_loc(t, tree_loc(name));
351✔
1828
         tree_set_value(t, change_ref(tree_value(name), new));
351✔
1829
         tree_set_type(t, tree_type(name));
351✔
1830
         tree_set_ident(t, tree_ident(name));
351✔
1831
         tree_set_ref(t, tree_ref(name));
351✔
1832

1833
         return t;
351✔
1834
      }
1835

1836
   case T_CONV_FUNC:
54✔
1837
      {
1838
         tree_t t = tree_new(T_CONV_FUNC);
54✔
1839
         tree_set_loc(t, tree_loc(name));
54✔
1840
         tree_set_value(t, change_ref(tree_value(name), new));
54✔
1841
         tree_set_ident(t, tree_ident(name));
54✔
1842
         tree_set_type(t, tree_type(name));
54✔
1843
         tree_set_ref(t, tree_ref(name));
54✔
1844

1845
         return t;
54✔
1846
      }
1847

1848
   case T_TYPE_CONV:
3✔
1849
      {
1850
         tree_t t = tree_new(T_TYPE_CONV);
3✔
1851
         tree_set_loc(t, tree_loc(name));
3✔
1852
         tree_set_type(t, tree_type(name));
3✔
1853
         tree_set_value(t, change_ref(tree_value(name), new));
3✔
1854

1855
         return t;
3✔
1856
      }
1857

1858
   default:
×
1859
      fatal_trace("cannot handle tree kind %s in elab_change_ref",
×
1860
                  tree_kind_str(tree_kind(name)));
1861
   }
1862
}
1863

1864
static void build_wait_for_target(tree_t expr, build_wait_fn_t fn, void *ctx)
1,661✔
1865
{
1866
   switch (tree_kind(expr)) {
1,661✔
1867
   case T_ARRAY_SLICE:
43✔
1868
      build_wait(tree_range(expr, 0), fn, ctx);
43✔
1869
      break;
43✔
1870

1871
   case T_ARRAY_REF:
209✔
1872
      {
1873
         const int nparams = tree_params(expr);
209✔
1874
         for (int i = 0; i < nparams; i++)
418✔
1875
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
209✔
1876
      }
1877
      break;
1878

1879
   default:
1880
      break;
1881
   }
1882
}
1,661✔
1883

1884
void build_wait(tree_t expr, build_wait_fn_t fn, void *ctx)
7,970✔
1885
{
1886
   // LRM 08 section 10.2 has rules for building a wait statement from a
1887
   // sensitivity list. LRM 08 section 11.3 extends these rules to
1888
   // all-sensitised processes.
1889

1890
   switch (tree_kind(expr)) {
10,252✔
1891
   case T_REF:
2,599✔
1892
      if (class_of(tree_ref(expr)) == C_SIGNAL)
2,599✔
1893
         (*fn)(expr, ctx);
1,371✔
1894
      break;
1895

1896
   case T_EXTERNAL_NAME:
3✔
1897
      if (tree_class(expr) == C_SIGNAL)
3✔
1898
         (*fn)(expr, ctx);
3✔
1899
      break;
1900

1901
   case T_WAVEFORM:
2,137✔
1902
   case T_QUALIFIED:
1903
   case T_TYPE_CONV:
1904
   case T_ASSERT:
1905
      if (tree_has_value(expr))
2,137✔
1906
         build_wait(tree_value(expr), fn, ctx);
2,137✔
1907
      break;
1908

1909
   case T_ARRAY_REF:
473✔
1910
   case T_ARRAY_SLICE:
1911
   case T_RECORD_REF:
1912
      {
1913
         tree_t ref = name_to_ref(expr);
473✔
1914
         if (ref != NULL && class_of(ref) == C_SIGNAL) {
473✔
1915
            if (longest_static_prefix(expr) == expr)
373✔
1916
               (*fn)(expr, ctx);
342✔
1917
            else {
1918
               build_wait(tree_value(expr), fn, ctx);
31✔
1919
               build_wait_for_target(expr, fn, ctx);
31✔
1920
            }
1921
         }
1922
         else
1923
            build_wait(tree_value(expr), fn, ctx);
100✔
1924
      }
1925
      break;
1926

1927
   case T_FCALL:
1,484✔
1928
   case T_PCALL:
1929
   case T_PROT_FCALL:
1930
   case T_PROT_PCALL:
1931
      {
1932
         tree_t decl = tree_ref(expr);
1,484✔
1933
         const int nparams = tree_params(expr);
1,484✔
1934
         for (int i = 0; i < nparams; i++) {
4,212✔
1935
            tree_t p = tree_param(expr, i);
2,728✔
1936
            assert(tree_subkind(p) == P_POS);
2,728✔
1937
            const port_mode_t mode = tree_subkind(tree_port(decl, tree_pos(p)));
2,728✔
1938
            if (mode == PORT_IN || mode == PORT_INOUT)
2,728✔
1939
               build_wait(tree_value(p), fn, ctx);
2,721✔
1940
         }
1941
      }
1942
      break;
1943

1944
   case T_AGGREGATE:
153✔
1945
      {
1946
         const int nassocs = tree_assocs(expr);
153✔
1947
         for (int i = 0; i < nassocs; i++)
571✔
1948
            build_wait(tree_value(tree_assoc(expr, i)), fn, ctx);
418✔
1949
      }
1950
      break;
1951

1952
   case T_ATTR_REF:
54✔
1953
      {
1954
         const attr_kind_t predef = tree_subkind(expr);
54✔
1955
         if (predef == ATTR_EVENT || predef == ATTR_ACTIVE)
54✔
1956
            build_wait(tree_name(expr), fn, ctx);
9✔
1957

1958
         const int nparams = tree_params(expr);
54✔
1959
         for (int i = 0; i < nparams; i++)
55✔
1960
            build_wait(tree_value(tree_param(expr, i)), fn, ctx);
1✔
1961
      }
1962
      break;
1963

1964
   case T_LITERAL:
1965
   case T_STRING:
1966
      break;
1967

1968
   case T_IF:
123✔
1969
      {
1970
         const int nconds = tree_conds(expr);
123✔
1971
         for (int i = 0; i < nconds; i++)
381✔
1972
            build_wait(tree_cond(expr, i), fn, ctx);
258✔
1973
      }
1974
      break;
1975

1976
   case T_COND:
258✔
1977
      {
1978
         if (tree_has_value(expr))
258✔
1979
            build_wait(tree_value(expr), fn, ctx);
145✔
1980

1981
         const int nstmts = tree_stmts(expr);
258✔
1982
         for (int i = 0; i < nstmts; i++)
516✔
1983
            build_wait(tree_stmt(expr, i), fn, ctx);
258✔
1984
      }
1985
      break;
1986

1987
   case T_PROCESS:
15✔
1988
   case T_SEQUENCE:
1989
   case T_PROC_BODY:
1990
      {
1991
         const int ndecls = tree_decls(expr);
15✔
1992
         for (int i = 0; i < ndecls; i++) {
17✔
1993
            tree_t d = tree_decl(expr, i);
2✔
1994
            if (tree_kind(d) == T_PROC_BODY)
2✔
1995
               build_wait(d, fn, ctx);
1✔
1996
         }
1997

1998
         const int nstmts = tree_stmts(expr);
15✔
1999
         for (int i = 0; i < nstmts; i++)
31✔
2000
            build_wait(tree_stmt(expr, i), fn, ctx);
16✔
2001
      }
2002
      break;
2003

2004
   case T_SIGNAL_ASSIGN:
1,629✔
2005
      {
2006
         build_wait_for_target(tree_target(expr), fn, ctx);
1,629✔
2007

2008
         const int nwaves = tree_waveforms(expr);
1,629✔
2009
         for (int i = 0; i < nwaves; i++)
3,322✔
2010
            build_wait(tree_waveform(expr, i), fn, ctx);
1,693✔
2011
      }
2012
      break;
2013

2014
   case T_VAR_ASSIGN:
1✔
2015
      build_wait_for_target(tree_target(expr), fn, ctx);
1✔
2016
      build_wait(tree_value(expr), fn, ctx);
1✔
2017
      break;
1✔
2018

2019
   case T_CASE:
24✔
2020
   case T_MATCH_CASE:
2021
      {
2022
         build_wait(tree_value(expr), fn, ctx);
24✔
2023

2024
         const int nstmts = tree_stmts(expr);
24✔
2025
         for (int i = 0; i < nstmts; i++) {
195✔
2026
            tree_t alt = tree_stmt(expr, i);
171✔
2027

2028
            const int nstmts = tree_stmts(alt);
171✔
2029
            for (int j = 0; j < nstmts; j++)
342✔
2030
               build_wait(tree_stmt(alt, j), fn, ctx);
171✔
2031
         }
2032
      }
2033
      break;
2034

2035
   case T_FOR:
1✔
2036
      {
2037
         build_wait(tree_range(expr, 0), fn, ctx);
1✔
2038

2039
         const int nstmts = tree_stmts(expr);
1✔
2040
         for (int i = 0; i < nstmts; i++)
2✔
2041
            build_wait(tree_stmt(expr, i), fn, ctx);
1✔
2042
      }
2043
      break;
2044

2045
   case T_WHILE:
1✔
2046
      {
2047
         build_wait(tree_value(expr), fn, ctx);
1✔
2048

2049
         const int nstmts = tree_stmts(expr);
1✔
2050
         for (int i = 0; i < nstmts; i++)
2✔
2051
            build_wait(tree_stmt(expr, i), fn, ctx);
1✔
2052
      }
2053
      break;
2054

2055
   case T_RANGE:
44✔
2056
      if (tree_subkind(expr) == RANGE_EXPR)
44✔
2057
         build_wait(tree_value(expr), fn, ctx);
×
2058
      else {
2059
         build_wait(tree_left(expr), fn, ctx);
44✔
2060
         build_wait(tree_right(expr), fn, ctx);
44✔
2061
      }
2062
      break;
2063

2064
   default:
×
2065
      fatal_trace("Cannot handle tree kind %s in wait expression",
×
2066
                  tree_kind_str(tree_kind(expr)));
2067
   }
2068
}
7,970✔
2069

2070
void print_syntax(const char *fmt, ...)
62✔
2071
{
2072
   LOCAL_TEXT_BUF tb = tb_new();
62✔
2073
   bool highlighting = false;
62✔
2074
   static bool comment = false, last_was_newline = false;
62✔
2075
   for (const char *p = fmt; *p != '\0'; p++) {
292✔
2076
      if (comment) {
230✔
2077
         if (*p == '\n' || *p == '\r') {
5✔
2078
            comment = false;
1✔
2079
            last_was_newline = true;
1✔
2080
            tb_printf(tb, "$$\n");
1✔
2081
         }
2082
         else if (*p != '~' && *p != '#') {
4✔
2083
            tb_append(tb, *p);
4✔
2084
            last_was_newline = false;
4✔
2085
         }
2086
         if (p > fmt && *p == '/' && *(p - 1) == '*') {
5✔
2087
            tb_printf(tb, "$$");
×
2088
            comment = false;
×
2089
            last_was_newline = false;
×
2090
         }
2091
      }
2092
      else if (*p == '\r') {
225✔
2093
         if (!last_was_newline) {
×
2094
            tb_append(tb, '\n');
×
2095
            last_was_newline = true;
×
2096
         }
2097
      }
2098
      else if (*p == '#') {
225✔
2099
         tb_printf(tb, "$bold$$cyan$");
15✔
2100
         last_was_newline = false;
15✔
2101
         highlighting = true;
15✔
2102
      }
2103
      else if (*p == '~') {
210✔
2104
         tb_printf(tb, "$yellow$");
×
2105
         last_was_newline = false;
×
2106
         highlighting = true;
×
2107
      }
2108
      else if ((*p == '-' && *(p + 1) == '-')
210✔
2109
               || (*p == '/' && *(p + 1) == '/')
210✔
2110
               || (*p == '/' && *(p + 1) == '*')) {
209✔
2111
         tb_printf(tb, "$red$%c", *p);
1✔
2112
         last_was_newline = false;
1✔
2113
         comment = true;
1✔
2114
      }
2115
      else if (!isalnum((int)*p) && *p != '_' && *p != '%' && highlighting) {
209✔
2116
         tb_printf(tb, "$$%c", *p);
11✔
2117
         last_was_newline = false;
11✔
2118
         highlighting = false;
11✔
2119
      }
2120
      else {
2121
         tb_append(tb, *p);
198✔
2122
         last_was_newline = (*p == '\n');
198✔
2123
      }
2124
   }
2125

2126
   if (highlighting)
62✔
2127
      tb_cat(tb, "$$");
4✔
2128

2129
   va_list ap;
62✔
2130
   va_start(ap, fmt);
62✔
2131

2132
   if (syntax_buf != NULL) {
62✔
2133
      char *stripped LOCAL = strip_color(tb_get(tb), ap);
124✔
2134
      tb_cat(syntax_buf, stripped);
62✔
2135
   }
2136
   else
2137
      color_vprintf(tb_get(tb), ap);
×
2138

2139
   va_end(ap);
62✔
2140
}
62✔
2141

2142
void capture_syntax(text_buf_t *tb)
2✔
2143
{
2144
   assert(tb == NULL || syntax_buf == NULL);
2✔
2145
   syntax_buf = tb;
2✔
2146
}
2✔
2147

2148
void copy_constraints(type_t sub, int index, type_t from)
10,138✔
2149
{
2150
   switch (type_kind(from)) {
13,389✔
2151
   case T_SUBTYPE:
1,261✔
2152
      {
2153
         const int ncon = type_constraints(from);
1,261✔
2154
         for (int i = 0, have = type_constraints(sub); i < ncon; i++) {
2,478✔
2155
            tree_t c = type_constraint(from, i);
1,217✔
2156
            if (tree_subkind(c) == C_OPEN && --have < index)
1,217✔
2157
               type_add_constraint(sub, c);
4✔
2158
         }
2159
      }
2160
      break;
2161

2162
   case T_ARRAY:
8,355✔
2163
      {
2164
         if (index >= type_constraints(sub)) {
8,355✔
2165
            tree_t c = tree_new(T_CONSTRAINT);
91✔
2166
            tree_set_subkind(c, C_OPEN);
91✔
2167

2168
            type_add_constraint(sub, c);
91✔
2169
         }
2170

2171
         if (standard() >= STD_08)
8,355✔
2172
            copy_constraints(sub, index + 1, type_elem(from));
3,251✔
2173
      }
2174
      break;
2175

2176
   case T_RECORD:
361✔
2177
      {
2178
         tree_t cons = NULL;
361✔
2179
         if (index < type_constraints(sub)) {
361✔
2180
            cons = type_constraint(sub, index);
192✔
2181
            if (tree_subkind(cons) != C_RECORD)
192✔
2182
               return;  // Will produce error later
2183
         }
2184

2185
         const int nfields = type_fields(from);
360✔
2186
         for (int i = 0; i < nfields; i++) {
1,767✔
2187
            tree_t f = type_field(from, i), exist;
1,407✔
2188
            type_t ft = tree_type(f);
1,407✔
2189
            if (type_is_unconstrained(ft)) {
1,407✔
2190
               if (cons == NULL) {
369✔
2191
                  cons = tree_new(T_CONSTRAINT);
6✔
2192
                  tree_set_subkind(cons, C_RECORD);
6✔
2193

2194
                  type_add_constraint(sub, cons);
6✔
2195
               }
2196
               else if ((exist = type_constraint_for_field(sub, f)) != NULL) {
363✔
2197
                  copy_constraints(tree_type(exist), 0, ft);
350✔
2198
                  continue;
350✔
2199
               }
2200

2201
               type_t fsub = type_new(T_SUBTYPE);
19✔
2202
               type_set_base(fsub, ft);
19✔
2203

2204
               copy_constraints(fsub, 0, ft);
19✔
2205

2206
               tree_t elem = tree_new(T_ELEM_CONSTRAINT);
19✔
2207
               tree_set_ident(elem, tree_ident(f));
19✔
2208
               tree_set_ref(elem, f);
19✔
2209
               tree_set_type(elem, fsub);
19✔
2210

2211
               tree_add_range(cons, elem);
19✔
2212
            }
2213
         }
2214
      }
2215
      break;
2216

2217
   default:
2218
      break;
2219
   }
2220
}
2221

2222
static void tree_copy_cb(tree_t t, void *__ctx)
240,027✔
2223
{
2224
   copy_ctx_t *ctx = __ctx;
240,027✔
2225

2226
   if (is_subprogram(t))
240,027✔
2227
      list_add(&ctx->copied_subs, t);
10,282✔
2228
}
240,027✔
2229

2230
static void type_copy_cb(type_t type, void *__ctx)
15,225✔
2231
{
2232
   copy_ctx_t *ctx = __ctx;
15,225✔
2233

2234
   if (type_has_ident(type))
15,225✔
2235
      list_add(&ctx->copied_types, type);
9,721✔
2236
}
15,225✔
2237

2238
void copy_with_renaming(tree_t *roots, int nroots, tree_copy_pred_t tree_pred,
5,743✔
2239
                        type_copy_pred_t type_pred, void *context,
2240
                        ident_t dotted, ident_t *prefixes, int nprefix)
2241
{
2242
   copy_ctx_t copy_ctx = {};
5,743✔
2243

2244
   tree_copy(roots, nroots, tree_pred, type_pred, context,
5,743✔
2245
             tree_copy_cb, type_copy_cb, &copy_ctx);
2246

2247
   // Change the name of any copied types to reflect the new hiearchy
2248
   list_foreach(type_t, type, copy_ctx.copied_types) {
15,464✔
2249
      ident_t orig = type_ident(type);
9,721✔
2250
      for (int j = 0; j < nprefix; j++) {
29,980✔
2251
         if (ident_starts_with(orig, prefixes[j])) {
10,904✔
2252
            LOCAL_TEXT_BUF tb = tb_new();
366✔
2253
            tb_cat(tb, istr(dotted));
366✔
2254
            tb_cat(tb, istr(orig) + ident_len(prefixes[j]));
366✔
2255

2256
            type_set_ident(type, ident_new(tb_get(tb)));
366✔
2257
            break;
366✔
2258
         }
2259
      }
2260
   }
2261
   list_clear(&copy_ctx.copied_types);
5,743✔
2262

2263
   // Change the mangled name of copied subprograms so that copies in
2264
   // different instances do not collide
2265
   list_foreach(tree_t, decl, copy_ctx.copied_subs) {
16,025✔
2266
      if (tree_kind(decl) == T_GENERIC_DECL)
10,282✔
2267
         continue;   // Does not yet have mangled name
296✔
2268

2269
      ident_t orig = tree_ident2(decl);
9,986✔
2270
      for (int j = 0; j < nprefix; j++) {
11,255✔
2271
         if (ident_starts_with(orig, prefixes[j])) {
10,060✔
2272
            ident_t prefix = ident_runtil(orig, '(');
8,791✔
2273

2274
            LOCAL_TEXT_BUF tb = tb_new();
8,791✔
2275
            tb_cat(tb, istr(dotted));
8,791✔
2276
            tb_cat(tb, istr(prefix) + ident_len(prefixes[j]));
8,791✔
2277

2278
            const tree_kind_t kind = tree_kind(decl);
8,791✔
2279
            const bool is_func = kind == T_FUNC_BODY || kind == T_FUNC_DECL;
8,791✔
2280
            const int nports = tree_ports(decl);
8,791✔
2281
            if (nports > 0 || is_func)
8,791✔
2282
               tb_append(tb, '(');
8,671✔
2283

2284
            for (int k = 0; k < nports; k++) {
26,021✔
2285
               tree_t p = tree_port(decl, k);
17,230✔
2286
               if (tree_class(p) == C_SIGNAL)
17,230✔
2287
                  tb_printf(tb, "s");
180✔
2288
               mangle_one_type(tb, tree_type(p));
17,230✔
2289
            }
2290

2291
            if (nports > 0 || is_func)
8,791✔
2292
               tb_printf(tb, ")");
8,671✔
2293

2294
            if (is_func)
8,791✔
2295
               mangle_one_type(tb, type_result(tree_type(decl)));
7,709✔
2296

2297
            ident_t mangled = ident_new(tb_get(tb));
8,791✔
2298
            tree_set_ident2(decl, mangled);
8,791✔
2299

2300
            break;
8,791✔
2301
         }
2302
      }
2303
   }
2304
   list_clear(&copy_ctx.copied_subs);
5,743✔
2305
}
5,743✔
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