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

nickg / nvc / 14432819507

13 Apr 2025 07:41PM UTC coverage: 92.319% (+0.03%) from 92.294%
14432819507

push

github

nickg
Use a function to evaluate complex default values

60 of 62 new or added lines in 4 files covered. (96.77%)

245 existing lines in 5 files now uncovered.

68994 of 74734 relevant lines covered (92.32%)

420155.84 hits per line

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

87.2
/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.h"
25
#include "jit/jit-ffi.h"
26
#include "lib.h"
27
#include "lower.h"
28
#include "option.h"
29
#include "phase.h"
30
#include "tree.h"
31
#include "type.h"
32
#include "vcode.h"
33

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

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

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

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

61
      tree_t lit = type_enum_literal(base, value.integer);
2,130✔
62

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

86
   tree_set_type(tree, type);
10,694✔
87
   tree_set_loc(tree, loc);
10,694✔
88
   return tree;
10,694✔
89
}
90

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

97
   if (type_is_array(type)) {
10,876✔
98
      assert(dimension_of(type) == 1);
194✔
99

100
      type_t elem = type_elem(type);
194✔
101
      assert(type_is_scalar(elem));
194✔
102

103
      type_t base = type_base_recur(elem);
194✔
104

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

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

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

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

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

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

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

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

157
      type_t sub = type_new(T_SUBTYPE);
194✔
158
      type_set_base(sub, type);
194✔
159

160
      type_t index_type = index_type_of(type, 0);
194✔
161

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

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

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

184
      type_set_constraint(sub, c);
194✔
185

186
      if (all_chars) {
194✔
187
         tree_t tree = tree_new(T_STRING);
188✔
188

189
         for (int i = 0; i < length; i++)
2,331✔
190
            tree_add_char(tree, elts[i]);
2,143✔
191

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

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

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

211
         return tree;
212
      }
213
   }
214
   else
215
      return eval_value_to_tree(args[0], tree_type(expr), tree_loc(expr));
10,682✔
216
}
217

218
static tree_t eval_do_fold(jit_t *jit, tree_t expr, lower_unit_t *parent,
12,394✔
219
                           unit_registry_t *registry, void *context)
220
{
221
   vcode_unit_t thunk = lower_thunk(registry, expr, parent);
12,394✔
222
   if (thunk == NULL)
12,394✔
223
      return expr;
224

225
   const bool verbose = opt_get_verbose(OPT_EVAL_VERBOSE, NULL);
11,300✔
226

227
   tree_t result = jit_call_thunk(jit, thunk, context, thunk_result_cb, expr);
11,300✔
228

229
   vcode_unit_unref(thunk);
11,296✔
230
   thunk = NULL;
11,296✔
231

232
   if (result != NULL) {
11,296✔
233
      if (verbose) {
10,876✔
234
         LOCAL_TEXT_BUF tb = tb_new();
×
235
         capture_syntax(tb);
×
236
         dump(result);
×
237
         capture_syntax(NULL);
×
238
         tb_strip(tb);
×
239

240
         debugf("evaluating %s returned %s", eval_expr_name(expr), tb_get(tb));
×
241
      }
242

243
      return result;
10,876✔
244
   }
245
   else if (verbose) {
420✔
246
      diag_t *d = diag_new(DIAG_DEBUG, tree_loc(expr));
×
247
      diag_printf(d, "failed to evaluate %s", eval_expr_name(expr));
×
248
      diag_emit(d);
×
249
   }
250

251
   return expr;
252
}
253

254
tree_t eval_try_fold(jit_t *jit, tree_t expr, unit_registry_t *registry,
12,269✔
255
                     lower_unit_t *parent, void *context)
256
{
257
   const bool verbose = opt_get_verbose(OPT_EVAL_VERBOSE, NULL);
12,269✔
258
   jit_set_silent(jit, !verbose);
12,269✔
259

260
   tree_t result = eval_do_fold(jit, expr, parent, registry, context);
12,269✔
261

262
   jit_set_silent(jit, false);
12,265✔
263
   jit_reset_exit_status(jit);
12,265✔
264

265
   return result;
12,265✔
266
}
267

268
tree_t eval_must_fold(jit_t *jit, tree_t expr, unit_registry_t *registry,
125✔
269
                      lower_unit_t *parent, void *context)
270
{
271
   return eval_do_fold(jit, expr, parent, registry, context);
125✔
272
}
273

274
static bool eval_not_possible(tree_t t, const char *why)
10,657✔
275
{
276
   if (opt_get_verbose(OPT_EVAL_VERBOSE, NULL))
10,657✔
277
      warn_at(tree_loc(t), "%s prevents constant folding", why);
×
278

279
   return false;
10,657✔
280
}
281

282
bool eval_possible(tree_t t, unit_registry_t *ur, mir_context_t *mc)
89,802✔
283
{
284
   switch (tree_kind(t)) {
100,207✔
285
   case T_FCALL:
47,439✔
286
      {
287
         const tree_flags_t flags = tree_flags(t);
47,439✔
288
         if (!(flags & (TREE_F_LOCALLY_STATIC | TREE_F_GLOBALLY_STATIC)))
47,439✔
289
            return eval_not_possible(t, "non-static expression");
62✔
290

291
         tree_t decl = tree_ref(t);
47,377✔
292
         const subprogram_kind_t kind = tree_subkind(decl);
47,377✔
293
         if (tree_flags(decl) & TREE_F_IMPURE)
47,377✔
294
            return eval_not_possible(t, "call to impure function");
×
295
         else if (kind != S_USER && !is_open_coded_builtin(kind)
47,377✔
296
                  && unit_registry_get(ur, tree_ident2(decl)) == NULL
4,166✔
297
                  && mir_get_unit(mc, tree_ident2(decl)) == NULL)
207✔
298
            return eval_not_possible(t, "not yet lowered predef");
162✔
299
         else if (kind == S_USER && !is_package(tree_container(decl)))
47,215✔
300
            return eval_not_possible(t, "subprogram not in package");
835✔
301

302
         const int nparams = tree_params(t);
46,380✔
303
         for (int i = 0; i < nparams; i++) {
63,235✔
304
            tree_t p = tree_value(tree_param(t, i));
52,074✔
305
            if (!eval_possible(p, ur, mc))
52,074✔
306
               return false;
307
            else if (tree_kind(p) == T_FCALL && type_is_scalar(tree_type(p)))
17,527✔
308
               return false;  // Would have been folded already if possible
309
         }
310

311
         return true;
312
      }
313

314
   case T_LITERAL:
315
   case T_STRING:
316
   case T_OPEN:
317
      return true;
318

319
   case T_TYPE_CONV:
7,045✔
320
   case T_QUALIFIED:
321
      {
322
         tree_t value = tree_value(t);
7,045✔
323
         if (tree_kind(value) == T_FCALL)
7,045✔
324
            return false;   // Would have been folded already if possible
325

326
         return eval_possible(value, ur, mc);
327
      }
328

329
   case T_REF:
20,379✔
330
      {
331
         tree_t decl = tree_ref(t);
20,379✔
332
         switch (tree_kind(decl)) {
20,379✔
333
         case T_UNIT_DECL:
334
         case T_ENUM_LIT:
335
            return true;
336

337
         case T_CONST_DECL:
2,554✔
338
            if (tree_has_value(decl))
2,554✔
339
               return eval_possible(tree_value(decl), ur, mc);
2,533✔
340
            else
341
               return false;
342

343
         default:
9,376✔
344
            return eval_not_possible(t, "reference");
9,376✔
345
         }
346
      }
347

348
   case T_RECORD_REF:
406✔
349
      return eval_possible(tree_value(t), ur, mc);
406✔
350

351
   case T_ARRAY_REF:
1,505✔
352
      {
353
         const int nparams = tree_params(t);
1,505✔
354
         for (int i = 0; i < nparams; i++) {
2,970✔
355
            if (!eval_possible(tree_value(tree_param(t, i)), ur, mc))
1,540✔
356
               return false;
357
         }
358

359
         return eval_possible(tree_value(t), ur, mc);
1,430✔
360
      }
361

362
   case T_AGGREGATE:
1,710✔
363
      {
364
         const int nassocs = tree_assocs(t);
1,710✔
365
         for (int i = 0; i < nassocs; i++) {
10,658✔
366
            if (!eval_possible(tree_value(tree_assoc(t, i)), ur, mc))
8,981✔
367
               return false;
368
         }
369

370
         // Check for missing choices in constrained array aggregates
371
         type_t composite_type = tree_type(t);
1,677✔
372
         if (type_is_array(composite_type)
1,677✔
373
             && !type_is_unconstrained(composite_type)) {
1,569✔
374
            int64_t count = 0, elem_count = 0;
1,547✔
375
            bool known_elem_count = false;
1,547✔
376
            bool has_others = false;
1,547✔
377
            bool has_range = false;
1,547✔
378
            for (int i = 0; i < nassocs; i++) {
10,155✔
379
               tree_t a = tree_assoc(t, i);
8,608✔
380
               const assoc_kind_t akind = tree_subkind(a);
8,608✔
381

382
               switch (akind) {
8,608✔
383

384
               case A_NAMED:
8,512✔
385
               case A_POS:
386
                  known_elem_count = true;
8,512✔
387
                  elem_count = 1;
8,512✔
388
                  break;
8,512✔
389

390
               case A_RANGE:
53✔
391
                  known_elem_count = false;
53✔
392
                  has_range = true;
53✔
393
                  break;
53✔
394

395
               case A_OTHERS:
40✔
396
                  known_elem_count = false;
40✔
397
                  has_others = true;
40✔
398
                  break;
40✔
399

400
               case A_SLICE:
3✔
401
               case A_CONCAT:
402
                  {
403
                     type_t v_type = tree_type(tree_value(a));
3✔
404
                     known_elem_count = true;
3✔
405
                     if (type_is_unconstrained(v_type))
3✔
406
                        known_elem_count = false;
407
                     else if (!folded_length(range_of(v_type, 0), &elem_count))
3✔
UNCOV
408
                        known_elem_count = false;
×
409
                     break;
410
                  }
411
               }
412

413
               if (known_elem_count)
8,608✔
414
                  count += elem_count;
8,515✔
415
            }
416

417
            if (has_range)
1,547✔
418
               // Range could overlap, defer to bounds check
419
               return eval_not_possible(t, "range as choice");
54✔
420

421
            if (!has_others) {
1,494✔
422
               int64_t type_count;
1,454✔
423
               if (folded_length(range_of(composite_type, 0), &type_count))
1,454✔
424
                  if (count != type_count)
1,454✔
425
                     return eval_not_possible(t, "missing choice");
1✔
426
            }
427
         }
428

429
         return true;
430
      }
431

432
   case T_ATTR_REF:
3,364✔
433
      {
434
         if (tree_subkind(t) == ATTR_USER)
3,364✔
UNCOV
435
            return eval_not_possible(t, "user defined attribute");
×
436

437
         if (!eval_possible(tree_name(t), ur, mc))
3,364✔
438
            return false;
439

440
         const int nparams = tree_params(t);
×
441
         for (int i = 0; i < nparams; i++) {
×
UNCOV
442
            if (!eval_possible(tree_value(tree_param(t, i)), ur, mc))
×
443
               return false;
444
         }
445

446
         return true;
447
      }
448

449
   default:
168✔
450
      return eval_not_possible(t, tree_kind_str(tree_kind(t)));
168✔
451
   }
452
}
453

454
static void *case_result_cb(jit_scalar_t *args, void *user)
9✔
455
{
456
   jit_scalar_t *result = user;
9✔
457
   result->integer = args[0].integer;
9✔
458
   return result;
9✔
459
}
460

461
tree_t eval_case(jit_t *jit, tree_t stmt, lower_unit_t *parent, void *context)
9✔
462
{
463
   assert(tree_kind(stmt) == T_CASE_GENERATE);
9✔
464

465
   vcode_unit_t thunk = lower_case_generate_thunk(parent, stmt);
9✔
466

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

471
   vcode_unit_unref(thunk);
9✔
472

473
   if (result.integer == -1)
9✔
474
      return NULL;
475
   else
476
      return tree_stmt(stmt, result.integer);
9✔
477
}
478

UNCOV
479
void *eval_instance(jit_t *jit, ident_t name, void *context)
×
480
{
481
   jit_handle_t h = jit_lazy_compile(jit, name);
×
UNCOV
482
   if (h == JIT_HANDLE_INVALID)
×
483
      fatal_trace("failed to compile instance %s", istr(name));
484

485
   jit_scalar_t result;
×
UNCOV
486
   if (!jit_try_call(jit, h, &result, context, context))
×
487
      return NULL;
488

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