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

nickg / nvc / 14477455473

15 Apr 2025 07:08PM UTC coverage: 92.302% (-0.02%) from 92.319%
14477455473

push

github

nickg
Locally static evaluation should not initialise user packages

Issue #1182

10 of 10 new or added lines in 3 files covered. (100.0%)

185 existing lines in 5 files now uncovered.

69009 of 74764 relevant lines covered (92.3%)

419861.32 hits per line

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

87.4
/src/eval.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 "eval.h"
22
#include "hash.h"
23
#include "ident.h"
24
#include "jit/jit-ffi.h"
25
#include "jit/jit.h"
26
#include "lib.h"
27
#include "lower.h"
28
#include "option.h"
29
#include "phase.h"
30
#include "rt/assert.h"
31
#include "tree.h"
32
#include "type.h"
33
#include "vcode.h"
34

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

42
static const char *eval_expr_name(tree_t expr)
×
43
{
44
   const tree_kind_t kind = tree_kind(expr);
×
45
   if (kind == T_FCALL)
×
46
      return istr(tree_ident(expr));
×
47
   else
48
      return tree_kind_str(kind);
×
49
}
50

51
static tree_t eval_value_to_tree(jit_scalar_t value, type_t type,
11,467✔
52
                                 const loc_t *loc)
53
{
54
   tree_t tree = NULL;
11,467✔
55

56
   if (type_is_enum(type)) {
11,467✔
57
      type_t base = type_base_recur(type);
2,878✔
58
      if ((unsigned)value.integer >= type_enum_literals(base))
2,878✔
59
         fatal_at(loc, "enum position %"PRIi64" out of range for type %s",
×
60
                  value.integer, type_pp(base));
61

62
      tree_t lit = type_enum_literal(base, value.integer);
2,878✔
63

64
      tree = tree_new(T_REF);
2,878✔
65
      tree_set_ref(tree, lit);
2,878✔
66
      tree_set_ident(tree, tree_ident(lit));
2,878✔
67
   }
68
   else if (type_is_integer(type)) {
8,589✔
69
      tree = tree_new(T_LITERAL);
2,347✔
70
      tree_set_subkind(tree, L_INT);
2,347✔
71
      tree_set_ival(tree, value.integer);
2,347✔
72
   }
73
   else if (type_is_real(type)) {
6,242✔
74
      tree = tree_new(T_LITERAL);
6,123✔
75
      tree_set_subkind(tree, L_REAL);
6,123✔
76
      tree_set_dval(tree, value.real);
6,123✔
77
   }
78
   else if (type_is_physical(type)) {
119✔
79
      tree = tree_new(T_LITERAL);
119✔
80
      tree_set_subkind(tree, L_PHYSICAL);
119✔
81
      tree_set_ival(tree, value.integer);
119✔
82
   }
83
   else
84
      fatal_trace("cannot convert 0x%"PRIx64" to %s", value.integer,
85
                  type_pp(type));
86

87
   tree_set_type(tree, type);
11,467✔
88
   tree_set_loc(tree, loc);
11,467✔
89
   return tree;
11,467✔
90
}
91

92
static void *thunk_result_cb(jit_scalar_t *args, void *user)
11,653✔
93
{
94
   tree_t expr = user;
11,653✔
95
   type_t type = tree_type(expr);
11,653✔
96
   const loc_t *loc = tree_loc(expr);
11,653✔
97

98
   if (type_is_array(type)) {
11,653✔
99
      assert(dimension_of(type) == 1);
198✔
100

101
      type_t elem = type_elem(type);
198✔
102
      assert(type_is_scalar(elem));
198✔
103

104
      type_t base = type_base_recur(elem);
198✔
105

106
      bool all_chars = true;
198✔
107
      tree_t *lits LOCAL = NULL;
396✔
108
      if (type_is_enum(elem)) {
198✔
109
         const int nlits = type_enum_literals(base);
193✔
110
         lits = xcalloc_array(nlits, sizeof(tree_t));
193✔
111
      }
112
      else
113
         all_chars = false;
114

115
      range_kind_t dir;
198✔
116
      int64_t length, ileft, iright;
198✔
117
      if (!type_const_bounds(type)) {
198✔
118
         length = ffi_array_length(args[2].integer);
197✔
119
         dir = ffi_array_dir(args[2].integer);
197✔
120
         ileft = args[1].integer;
197✔
121
         iright = ffi_array_right(args[1].integer, args[2].integer);
296✔
122
      }
123
      else {
124
         tree_t r = range_of(type, 0);
1✔
125
         if (!folded_length(r, &length))
1✔
126
            fatal_at(loc, "cannot determine static length of array");
×
127

128
         dir = tree_subkind(r);
1✔
129
         ileft = assume_int(tree_left(r));
1✔
130
         iright = assume_int(tree_right(r));
1✔
131
      }
132

133
      const int bytes = (type_bit_width(elem) + 7) / 8;
198✔
134
      tree_t *elts LOCAL = xmalloc_array(length, sizeof(tree_t));
396✔
135
      for (int i = 0; i < length; i++) {
2,384✔
136
#define UNPACK_VALUE(type) do {                                    \
137
            value.integer = ((type *)args[0].pointer)[i];          \
138
         } while (0);
139

140
         jit_scalar_t value = { .integer = 0 };
2,186✔
141
         FOR_ALL_SIZES(bytes, UNPACK_VALUE);
2,186✔
142

143
         if (lits != NULL) {
2,186✔
144
            assert(value.integer >= 0);
2,174✔
145

146
            if (lits[value.integer] == NULL) {
2,174✔
147
               tree_t li = type_enum_literal(base, value.integer);
365✔
148
               lits[value.integer] = make_ref(li);
365✔
149
               all_chars &= ident_char(tree_ident(li), 0) == '\'';
365✔
150
            }
151

152
            elts[i] = lits[value.integer];
2,174✔
153
         }
154
         else
155
            elts[i] = eval_value_to_tree(value, elem, loc);
12✔
156
      }
157

158
      type_t sub = type_new(T_SUBTYPE);
198✔
159
      type_set_base(sub, type);
198✔
160

161
      type_t index_type = index_type_of(type, 0);
198✔
162

163
      tree_t left = NULL, right = NULL;
198✔
164
      if (type_is_enum(index_type)) {
198✔
165
         left = get_enum_lit(expr, index_type, ileft);
×
166
         right = get_enum_lit(expr, index_type, iright);
×
167
      }
168
      else {
169
         left = get_int_lit(expr, index_type, ileft);
198✔
170
         right = get_int_lit(expr, index_type, iright);
198✔
171
      }
172

173
      tree_t r = tree_new(T_RANGE);
198✔
174
      tree_set_subkind(r, dir);
198✔
175
      tree_set_left(r, left);
198✔
176
      tree_set_right(r, right);
198✔
177
      tree_set_loc(r, loc);
198✔
178
      tree_set_type(r, index_type);
198✔
179

180
      tree_t c = tree_new(T_CONSTRAINT);
198✔
181
      tree_set_subkind(c, C_INDEX);
198✔
182
      tree_add_range(c, r);
198✔
183
      tree_set_loc(c, loc);
198✔
184

185
      type_set_constraint(sub, c);
198✔
186

187
      if (all_chars) {
198✔
188
         tree_t tree = tree_new(T_STRING);
192✔
189

190
         for (int i = 0; i < length; i++)
2,364✔
191
            tree_add_char(tree, elts[i]);
2,172✔
192

193
         tree_set_loc(tree, loc);
192✔
194
         tree_set_type(tree, sub);
192✔
195
         return tree;
192✔
196
      }
197
      else {
198
         tree_t tree = tree_new(T_AGGREGATE);
6✔
199
         tree_set_type(tree, sub);
6✔
200
         tree_set_loc(tree, loc);
6✔
201

202
         for (int i = 0; i < length; i++) {
20✔
203
            tree_t a = tree_new(T_ASSOC);
14✔
204
            tree_set_loc(a, loc);
14✔
205
            tree_set_subkind(a, A_POS);
14✔
206
            tree_set_pos(a, i);
14✔
207
            tree_set_value(a, elts[i]);
14✔
208

209
            tree_add_assoc(tree, a);
14✔
210
         }
211

212
         return tree;
213
      }
214
   }
215
   else
216
      return eval_value_to_tree(args[0], tree_type(expr), tree_loc(expr));
11,455✔
217
}
218

219
static tree_t eval_do_fold(jit_t *jit, tree_t expr, lower_unit_t *parent,
12,382✔
220
                           unit_registry_t *registry, void *context)
221
{
222
   vcode_unit_t thunk;
12,382✔
223
   if (parent != NULL)
12,382✔
224
      thunk = lower_thunk_in_context(registry, expr, parent);
907✔
225
   else
226
      thunk = lower_global_thunk(registry, expr);
11,475✔
227

228
   if (thunk == NULL)
12,382✔
229
      return expr;
230

231
   const bool verbose = opt_get_verbose(OPT_EVAL_VERBOSE, NULL);
12,080✔
232

233
   tree_t result = jit_call_thunk(jit, thunk, context, thunk_result_cb, expr);
12,080✔
234

235
   vcode_unit_unref(thunk);
12,076✔
236
   thunk = NULL;
12,076✔
237

238
   if (result != NULL) {
12,076✔
239
      if (verbose) {
11,653✔
UNCOV
240
         LOCAL_TEXT_BUF tb = tb_new();
×
241
         capture_syntax(tb);
×
UNCOV
242
         dump(result);
×
UNCOV
243
         capture_syntax(NULL);
×
UNCOV
244
         tb_strip(tb);
×
245

UNCOV
246
         debugf("evaluating %s returned %s", eval_expr_name(expr), tb_get(tb));
×
247
      }
248

249
      return result;
11,653✔
250
   }
251
   else if (verbose) {
423✔
UNCOV
252
      diag_t *d = diag_new(DIAG_DEBUG, tree_loc(expr));
×
UNCOV
253
      diag_printf(d, "failed to evaluate %s", eval_expr_name(expr));
×
UNCOV
254
      diag_emit(d);
×
255
   }
256

257
   return expr;
258
}
259

260
tree_t eval_try_fold(jit_t *jit, tree_t expr, unit_registry_t *registry,
12,257✔
261
                     lower_unit_t *parent, void *context)
262
{
263
   const bool verbose = opt_get_verbose(OPT_EVAL_VERBOSE, NULL);
12,257✔
264
   jit_set_silent(jit, !verbose);
12,257✔
265

266
   tree_t result = eval_do_fold(jit, expr, parent, registry, context);
12,257✔
267

268
   jit_set_silent(jit, false);
12,253✔
269
   jit_reset_exit_status(jit);
12,253✔
270

271
   clear_vhdl_assert();
12,253✔
272

273
   return result;
12,253✔
274
}
275

276
tree_t eval_must_fold(jit_t *jit, tree_t expr, unit_registry_t *registry,
125✔
277
                      lower_unit_t *parent, void *context)
278
{
279
   return eval_do_fold(jit, expr, parent, registry, context);
125✔
280
}
281

282
static bool eval_not_possible(tree_t t, const char *why)
10,659✔
283
{
284
   if (opt_get_verbose(OPT_EVAL_VERBOSE, NULL))
10,659✔
UNCOV
285
      warn_at(tree_loc(t), "%s prevents constant folding", why);
×
286

287
   return false;
10,659✔
288
}
289

290
bool eval_possible(tree_t t, unit_registry_t *ur, mir_context_t *mc)
64,337✔
291
{
292
   switch (tree_kind(t)) {
73,666✔
293
   case T_FCALL:
23,656✔
294
      {
295
         const tree_flags_t flags = tree_flags(t);
23,656✔
296
         if (!(flags & (TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC)))
23,656✔
297
            return eval_not_possible(t, "non-static expression");
64✔
298

299
         tree_t decl = tree_ref(t);
23,592✔
300
         const subprogram_kind_t kind = tree_subkind(decl);
23,592✔
301
         if (tree_flags(decl) & TREE_F_IMPURE)
23,592✔
UNCOV
302
            return eval_not_possible(t, "call to impure function");
×
303
         else if (kind != S_USER && !is_open_coded_builtin(kind)
23,592✔
304
                  && unit_registry_get(ur, tree_ident2(decl)) == NULL
4,158✔
305
                  && mir_get_unit(mc, tree_ident2(decl)) == NULL)
207✔
306
            return eval_not_possible(t, "not yet lowered predef");
162✔
307
         else if (kind == S_USER && !is_package(tree_container(decl)))
23,430✔
308
            return eval_not_possible(t, "subprogram not in package");
835✔
309

310
         const int nparams = tree_params(t);
22,595✔
311
         for (int i = 0; i < nparams; i++) {
38,349✔
312
            tree_t p = tree_value(tree_param(t, i));
27,732✔
313
            if (!eval_possible(p, ur, mc))
27,732✔
314
               return false;
315
            else if (tree_kind(p) == T_FCALL && type_is_scalar(tree_type(p)))
15,890✔
316
               return false;  // Would have been folded already if possible
317
         }
318

319
         return true;
320
      }
321

322
   case T_LITERAL:
323
   case T_STRING:
324
   case T_OPEN:
325
      return true;
326

327
   case T_TYPE_CONV:
7,060✔
328
   case T_QUALIFIED:
329
      {
330
         tree_t value = tree_value(t);
7,060✔
331
         if (tree_kind(value) == T_FCALL)
7,060✔
332
            return false;   // Would have been folded already if possible
333

334
         return eval_possible(value, ur, mc);
335
      }
336

337
   case T_REF:
19,331✔
338
      {
339
         tree_t decl = tree_ref(t);
19,331✔
340
         switch (tree_kind(decl)) {
19,331✔
341
         case T_UNIT_DECL:
342
         case T_ENUM_LIT:
343
            return true;
344

345
         case T_CONST_DECL:
2,008✔
346
            if (tree_has_value(decl))
2,008✔
347
               return eval_possible(tree_value(decl), ur, mc);
1,987✔
348
            else
349
               return false;
350

351
         default:
9,376✔
352
            return eval_not_possible(t, "reference");
9,376✔
353
         }
354
      }
355

356
   case T_RECORD_REF:
397✔
357
      return eval_possible(tree_value(t), ur, mc);
397✔
358

359
   case T_ARRAY_REF:
969✔
360
      {
361
         const int nparams = tree_params(t);
969✔
362
         for (int i = 0; i < nparams; i++) {
1,898✔
363
            if (!eval_possible(tree_value(tree_param(t, i)), ur, mc))
1,004✔
364
               return false;
365
         }
366

367
         return eval_possible(tree_value(t), ur, mc);
894✔
368
      }
369

370
   case T_AGGREGATE:
1,694✔
371
      {
372
         const int nassocs = tree_assocs(t);
1,694✔
373
         for (int i = 0; i < nassocs; i++) {
10,601✔
374
            if (!eval_possible(tree_value(tree_assoc(t, i)), ur, mc))
8,940✔
375
               return false;
376
         }
377

378
         // Check for missing choices in constrained array aggregates
379
         type_t composite_type = tree_type(t);
1,661✔
380
         if (type_is_array(composite_type)
1,661✔
381
             && !type_is_unconstrained(composite_type)) {
1,563✔
382
            int64_t count = 0, elem_count = 0;
1,541✔
383
            bool known_elem_count = false;
1,541✔
384
            bool has_others = false;
1,541✔
385
            bool has_range = false;
1,541✔
386
            for (int i = 0; i < nassocs; i++) {
10,128✔
387
               tree_t a = tree_assoc(t, i);
8,587✔
388
               const assoc_kind_t akind = tree_subkind(a);
8,587✔
389

390
               switch (akind) {
8,587✔
391

392
               case A_NAMED:
8,486✔
393
               case A_POS:
394
                  known_elem_count = true;
8,486✔
395
                  elem_count = 1;
8,486✔
396
                  break;
8,486✔
397

398
               case A_RANGE:
53✔
399
                  known_elem_count = false;
53✔
400
                  has_range = true;
53✔
401
                  break;
53✔
402

403
               case A_OTHERS:
42✔
404
                  known_elem_count = false;
42✔
405
                  has_others = true;
42✔
406
                  break;
42✔
407

408
               case A_SLICE:
6✔
409
               case A_CONCAT:
410
                  {
411
                     type_t v_type = tree_type(tree_value(a));
6✔
412
                     known_elem_count = true;
6✔
413
                     if (type_is_unconstrained(v_type))
6✔
414
                        known_elem_count = false;
415
                     else if (!folded_length(range_of(v_type, 0), &elem_count))
6✔
UNCOV
416
                        known_elem_count = false;
×
417
                     break;
418
                  }
419
               }
420

421
               if (known_elem_count)
8,587✔
422
                  count += elem_count;
8,492✔
423
            }
424

425
            if (has_range)
1,541✔
426
               // Range could overlap, defer to bounds check
427
               return eval_not_possible(t, "range as choice");
54✔
428

429
            if (!has_others) {
1,488✔
430
               int64_t type_count;
1,446✔
431
               if (folded_length(range_of(composite_type, 0), &type_count))
1,446✔
432
                  if (count != type_count)
1,446✔
433
                     return eval_not_possible(t, "missing choice");
1✔
434
            }
435
         }
436

437
         return true;
438
      }
439

440
   case T_ATTR_REF:
3,364✔
441
      {
442
         if (tree_subkind(t) == ATTR_USER)
3,364✔
443
            return eval_not_possible(t, "user defined attribute");
×
444

445
         if (!eval_possible(tree_name(t), ur, mc))
3,364✔
446
            return false;
447

UNCOV
448
         const int nparams = tree_params(t);
×
UNCOV
449
         for (int i = 0; i < nparams; i++) {
×
UNCOV
450
            if (!eval_possible(tree_value(tree_param(t, i)), ur, mc))
×
451
               return false;
452
         }
453

454
         return true;
455
      }
456

457
   default:
168✔
458
      return eval_not_possible(t, tree_kind_str(tree_kind(t)));
168✔
459
   }
460
}
461

462
static void *case_result_cb(jit_scalar_t *args, void *user)
9✔
463
{
464
   jit_scalar_t *result = user;
9✔
465
   result->integer = args[0].integer;
9✔
466
   return result;
9✔
467
}
468

469
tree_t eval_case(jit_t *jit, tree_t stmt, lower_unit_t *parent, void *context)
9✔
470
{
471
   assert(tree_kind(stmt) == T_CASE_GENERATE);
9✔
472

473
   vcode_unit_t thunk = lower_case_generate_thunk(parent, stmt);
9✔
474

475
   jit_scalar_t result = { .integer = -1 };
9✔
476
   if (jit_call_thunk(jit, thunk, context, case_result_cb, &result) == NULL)
9✔
UNCOV
477
      error_at(tree_loc(tree_value(stmt)), "generate expression is not static");
×
478

479
   vcode_unit_unref(thunk);
9✔
480

481
   if (result.integer == -1)
9✔
482
      return NULL;
483
   else
484
      return tree_stmt(stmt, result.integer);
9✔
485
}
486

UNCOV
487
void *eval_instance(jit_t *jit, ident_t name, void *context)
×
488
{
489
   jit_handle_t h = jit_lazy_compile(jit, name);
×
UNCOV
490
   if (h == JIT_HANDLE_INVALID)
×
491
      fatal_trace("failed to compile instance %s", istr(name));
492

UNCOV
493
   jit_scalar_t result;
×
UNCOV
494
   if (!jit_try_call(jit, h, &result, context, context))
×
495
      return NULL;
496

UNCOV
497
   return result.pointer;
×
498
}
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