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

nickg / nvc / 25935296376

15 May 2026 06:29PM UTC coverage: 92.258% (+0.004%) from 92.254%
25935296376

push

github

nickg
Handle real values with integer formats in vpi_get_value

32 of 43 new or added lines in 2 files covered. (74.42%)

623 existing lines in 14 files now uncovered.

78015 of 84562 relevant lines covered (92.26%)

641925.59 hits per line

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

85.29
/src/object.c
1
//
2
//  Copyright (C) 2014-2025  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 "fbuf.h"
22
#include "hash.h"
23
#include "ident.h"
24
#include "object.h"
25
#include "option.h"
26
#include "thread.h"
27

28
#include <string.h>
29
#include <stdlib.h>
30
#include <inttypes.h>
31
#include <signal.h>
32

33
typedef uint64_t mark_mask_t;
34

35
typedef A(object_arena_t *) arena_array_t;
36
typedef A(object_t **) object_ptr_array_t;
37

38
typedef enum { OBJ_DISK, OBJ_FRESH } obj_src_t;
39

40
typedef struct _object_arena {
41
   void           *base;
42
   void           *alloc;
43
   void           *limit;
44
   uint32_t       *forward;
45
   mark_mask_t    *mark_bits;
46
   size_t          mark_sz;
47
   size_t          mark_low;
48
   size_t          mark_high;
49
   uint32_t        live_bytes;
50
   uint32_t        flags;
51
   generation_t    generation;
52
   arena_key_t     key;
53
   arena_array_t   deps;
54
   object_t       *root;
55
   obj_src_t       source;
56
   vhdl_standard_t std;
57
   uint32_t        checksum;
58
   generation_t    copygen;
59
   bool            frozen;
60
   bool            obsolete;
61
} object_arena_t;
62

63
#if !ASAN_ENABLED
64
#define OBJECT_UNMAP_UNUSED 1
65
#endif
66

67
#define ITEM_IDENT       (I_IDENT | I_IDENT2)
68
#define ITEM_OBJECT      (I_VALUE | I_SEVERITY | I_MESSAGE | I_TARGET   \
69
                          | I_DELAY | I_REJECT | I_REF | I_FILE_MODE    \
70
                          | I_NAME | I_SPEC | I_RESOLUTION              \
71
                          | I_LEFT | I_RIGHT | I_TYPE | I_BASE | I_ELEM \
72
                          | I_DESIGNATED | I_RESULT | I_PRIMARY         \
73
                          | I_GUARD | I_FOREIGN | I_CONSTRAINT)
74
#define ITEM_OBJ_ARRAY   (I_DECLS | I_STMTS | I_PORTS | I_GENERICS      \
75
                          | I_WAVES | I_CONDS | I_TRIGGERS | I_PARAMS   \
76
                          | I_GENMAPS | I_ASSOCS | I_CONTEXT            \
77
                          | I_LITERALS | I_FIELDS | I_UNITS | I_CHARS   \
78
                          | I_DIMS | I_RANGES | I_INDEXES | I_PARTS \
79
                          | I_PRAGMAS | I_CHOICES)
80
#define ITEM_INT64       (I_POS | I_IVAL)
81
#define ITEM_INT32       (I_SUBKIND | I_CLASS | I_FLAGS)
82
#define ITEM_DOUBLE      (I_DVAL)
83
#define ITEM_NUMBER      (I_NUMBER)
84

85
static const char *item_text_map[] = {
86
   "I_IDENT",    "I_VALUE",      "I_PRIMARY",  "I_GENERICS",   "I_PORTS",
87
   "I_DECLS",    "I_STMTS",      "I_TARGET",   "I_IVAL",       "I_IDENT2",
88
   "I_SEVERITY", "I_GENMAPS",    "I_PARAMS",   "I_WAVES",      "I_CONDS",
89
   "I_TYPE",     "I_SUBKIND",    "I_DELAY",    "I_REJECT",     "I_POS",
90
   "I_REF",      "I_FILE_MODE",  "I_ASSOCS",   "I_CONTEXT",    "I_TRIGGERS",
91
   "I_PARTS"  ,  "I_CLASS",      "I_RANGES",   "I_NAME",       "I_PRAGMAS",
92
   "I_DVAL",     "I_SPEC",       "I_FOREIGN",  "I_INDEXES",    "I_BASE",
93
   "I_ELEM",     "I_DESIGNATED", "I_CHOICES",  "I_RESOLUTION", "I_RESULT",
94
   "I_UNITS",    "I_LITERALS",   "I_DIMS",     "I_FIELDS",     "I_CLOCK",
95
   "I_GUARD",    "???",          "I_CHARS",    "I_CONSTRAINT", "I_FLAGS",
96
   "???",        "I_LEFT",       "I_RIGHT",    "I_NUMBER",     "I_MESSAGE",
97
};
98

99
static object_class_t *classes[4];
100
static uint32_t        format_digest;
101
static generation_t    next_generation = 1;
102
static arena_array_t   all_arenas;
103
static object_arena_t *global_arena = NULL;
104
static chash_t        *arena_lookup;
105

106
static inline bool object_in_arena_p(object_arena_t *arena, object_t *object)
14,183,126✔
107
{
108
   return (void *)object >= arena->base && (void *)object < arena->limit;
14,183,126✔
109
}
110

111
static inline object_arena_t *__object_arena(object_t *object)
35,207,592✔
112
{
113
   assert(object->arena < all_arenas.count);
35,207,592✔
114
   assert(object->arena != 0);
35,207,592✔
115
   return all_arenas.items[object->arena];
35,207,592✔
116
}
117

118
static ident_t object_arena_name(object_arena_t *arena)
321,046✔
119
{
120
   if (arena->alloc > arena->base) {
321,046✔
121
      ident_t name = object_ident(arena_root(arena));
290,184✔
122
      if (name != NULL)
290,184✔
123
         return name;
124
   }
125

126
   return ident_new("???");
34,251✔
127
}
128

129
static inline void zero_mark_bits(object_arena_t *arena, unsigned first,
218,700✔
130
                                  size_t count)
131
{
132
   assert(first + count <= arena->mark_sz);
218,700✔
133
   assert(first > arena->mark_high || first + count <= arena->mark_low);
218,700✔
134

135
   if (count == 1)
218,700✔
136
      arena->mark_bits[first] = 0;
190,167✔
137
   else
138
      memset(arena->mark_bits + first, '\0', count * sizeof(uint64_t));
28,533✔
139
}
218,700✔
140

141
static bool object_marked_p(object_t *object, generation_t generation)
15,138,605✔
142
{
143
   object_arena_t *arena = __object_arena(object);
15,138,605✔
144

145
   const uintptr_t bit = ((void *)object - arena->base) >> OBJECT_ALIGN_BITS;
15,138,605✔
146
   const uintptr_t word = bit / 64;
15,138,605✔
147

148
   if (unlikely(arena->mark_bits == NULL)) {
15,138,605✔
149
      const size_t nbits = (arena->limit - arena->base) / OBJECT_ALIGN;
26,057✔
150
      arena->mark_sz = ALIGN_UP(nbits, 64) / 8;
26,057✔
151
      arena->mark_bits = xmalloc(arena->mark_sz);
26,057✔
152
      arena->mark_bits[word] = 0;
26,057✔
153
      arena->mark_low = arena->mark_high = word;
26,057✔
154
      arena->generation = generation;
26,057✔
155
   }
156
   else if (arena->generation != generation) {
15,112,548✔
157
      arena->mark_bits[word] = 0;
74,201✔
158
      arena->mark_low = arena->mark_high = word;
74,201✔
159
      arena->generation = generation;
74,201✔
160
   }
161

162
   // Lazy zeroing of mark bits helps performance with large arenas
163
   if (word < arena->mark_low) {
15,138,605✔
164
      zero_mark_bits(arena, word, arena->mark_low - word);
9,552✔
165
      arena->mark_low = word;
9,552✔
166
   }
167
   else if (word > arena->mark_high) {
15,129,053✔
168
      zero_mark_bits(arena, arena->mark_high + 1, word - arena->mark_high);
209,148✔
169
      arena->mark_high = word;
209,148✔
170
   }
171

172
   const uint64_t mask = UINT64_C(1) << (bit & 63);
15,138,605✔
173

174
   const bool marked = !!(arena->mark_bits[word] & mask);
15,138,605✔
175
   arena->mark_bits[word] |= mask;
15,138,605✔
176

177
   return marked;
15,138,605✔
178
}
179

180
void arena_set_checksum(object_arena_t *arena, uint32_t checksum)
55,489✔
181
{
182
   arena->checksum = checksum;
55,489✔
183
}
55,489✔
184

185
object_t *arena_root(object_arena_t *arena)
435,796✔
186
{
187
   return arena->root ?: (object_t *)arena->base;
435,796✔
188
}
189

190
bool arena_frozen(object_arena_t *arena)
145,913✔
191
{
192
   return arena->frozen;
145,913✔
193
}
194

195
uint32_t arena_flags(object_arena_t *arena)
89,037✔
196
{
197
   return arena->flags;
89,037✔
198
}
199

200
void arena_set_flags(object_arena_t *arena, uint32_t flags)
9,552✔
201
{
202
   arena->flags |= flags;
9,552✔
203
}
9,552✔
204

205
void arena_set_obsolete(object_arena_t *arena, bool obsolete)
30✔
206
{
207
   arena->obsolete = true;
30✔
208
}
30✔
209

210
object_arena_t *object_arena(object_t *object)
403,382✔
211
{
212
   return __object_arena(object);
403,382✔
213
}
214

215
ident_t object_ident(object_t *object)
290,192✔
216
{
217
   const object_class_t *class = classes[object->tag];
290,192✔
218
   const imask_t has = class->has_map[object->kind];
290,192✔
219

220
   if (has & I_IDENT) {
290,192✔
221
      const int n = __builtin_popcountll(has & (I_IDENT - 1));
287,000✔
222
      return object->items[n].ident;
287,000✔
223
   }
224

225
   return NULL;
226
}
227

228
const char *object_kind_str(object_t *object)
×
229
{
230
   if (object == NULL)
×
231
      return NULL;
232

233
   const object_class_t *class = classes[object->tag];
×
234
   return class->kind_text_map[object->kind];
×
235
}
236

237
void __object_write_barrier(object_t *lhs, object_t *rhs)
6,311,510✔
238
{
239
   const uintptr_t lhs_mask = (uintptr_t)lhs & ~OBJECT_PAGE_MASK;
6,311,510✔
240
   const uintptr_t rhs_mask = (uintptr_t)rhs & ~OBJECT_PAGE_MASK;
6,311,510✔
241

242
   if (lhs_mask == rhs_mask || rhs == NULL)
6,311,510✔
243
      return;
244
   else if (lhs->arena == rhs->arena)
6,311,510✔
245
      return;
246

247
   object_arena_t *larena = __object_arena(lhs);
5,619,156✔
248
   object_arena_t *rarena = __object_arena(rhs);
5,619,156✔
249

250
   assert(!larena->frozen);
5,619,156✔
251
   assert(rarena->frozen);
5,619,156✔
252

253
   for (unsigned i = 0; i < larena->deps.count; i++) {
9,063,156✔
254
      if (larena->deps.items[i] == rarena)
9,013,965✔
255
         return;
256
   }
257

258
   APUSH(larena->deps, rarena);
49,191✔
259
}
260

261
void object_lookup_failed(object_class_t *class, object_t *object, imask_t mask)
×
262
{
263
   unsigned int item;
×
264
   for (item = 0; (mask & (UINT64_C(1) << item)) == 0; item++)
×
265
      ;
266

267
   assert(item < ARRAY_LEN(item_text_map));
×
268

269
   diag_t *d = diag_new(DIAG_FATAL, &(object->loc));
×
270
   diag_printf(d, "%s kind %s does not have item %s", class->name,
×
271
               class->kind_text_map[object->kind], item_text_map[item]);
×
272
   diag_set_consumer(NULL, NULL);
×
273
   diag_suppress(d, false);
×
274
   diag_emit(d);
×
275
   show_stacktrace();
×
276
   fatal_exit(EXIT_FAILURE);
×
277
}
278

279
void obj_array_add(obj_array_t **a, object_t *o)
1,719,955✔
280
{
281
   if (*a == NULL) {
1,719,955✔
282
      const int defsz = 8;
612,179✔
283
      *a = xmalloc_flex(sizeof(obj_array_t), defsz, sizeof(object_t *));
612,179✔
284
      (*a)->count = 0;
612,179✔
285
      (*a)->limit = defsz;
612,179✔
286
   }
287
   else if ((*a)->count == (*a)->limit) {
1,107,776✔
288
      (*a)->limit *= 2;
42,367✔
289
      *a = xrealloc_flex(*a, sizeof(obj_array_t),
42,367✔
290
                         (*a)->limit, sizeof(object_t *));
291
   }
292

293
   (*a)->items[(*a)->count++] = o;
1,719,955✔
294
}
1,719,955✔
295

296
void obj_array_copy(obj_array_t **dst, const obj_array_t *src)
15,973✔
297
{
298
   if (src == NULL)
15,973✔
299
      return;
300

301
   if (*dst == NULL) {
15,973✔
302
      *dst = xmalloc_flex(sizeof(obj_array_t), src->count, sizeof(object_t *));
15,853✔
303
      (*dst)->count = 0;
15,853✔
304
      (*dst)->limit = src->count;
15,853✔
305
   }
306
   else if ((*dst)->count + src->count > (*dst)->limit) {
120✔
307
      (*dst)->limit = (*dst)->count + src->count;
120✔
308
      *dst = xrealloc_flex(*dst, sizeof(obj_array_t),
120✔
309
                           (*dst)->limit, sizeof(object_t *));
310
   }
311

312
   for (int i = 0; i < src->count; i++)
75,765✔
313
      (*dst)->items[(*dst)->count++] = src->items[i];
59,792✔
314
}
315

316
void obj_array_free(obj_array_t **a)
136,840✔
317
{
318
   free(*a);
136,840✔
319
   *a = NULL;
136,840✔
320
}
136,840✔
321

322
void object_change_kind(const object_class_t *class, object_t *object, int kind)
28,361✔
323
{
28,361✔
324
   if (kind == object->kind)
28,361✔
325
      return;
×
326

327
   bool allow = false;
328
   for (size_t i = 0; (class->change_allowed[i][0] != -1) && !allow; i++) {
164,822✔
329
      allow = (class->change_allowed[i][0] == object->kind)
136,461✔
330
         && (class->change_allowed[i][1] == kind);
136,461✔
331
   }
332

333
   if (!allow)
28,361✔
334
      fatal_trace("cannot change %s kind %s to %s", class->name,
335
                  class->kind_text_map[object->kind],
×
336
                  class->kind_text_map[kind]);
×
337

338
   const imask_t old_has = class->has_map[object->kind];
28,361✔
339
   const imask_t new_has = class->has_map[kind];
28,361✔
340

341
   const int old_nitems = __builtin_popcountll(old_has);
28,361✔
342
   const int new_nitems = __builtin_popcountll(new_has);
28,361✔
343

344
   const int max_items = MAX(old_nitems, new_nitems);
28,361✔
345

346
   item_t tmp[max_items];
28,361✔
347
   memcpy(tmp, object->items, sizeof(item_t) * max_items);
28,361✔
348

349
   int op = 0, np = 0;
28,361✔
350
   for (imask_t mask = 1; np < new_nitems; mask <<= 1) {
1,105,951✔
351
      if ((old_has & mask) && (new_has & mask))
1,077,590✔
352
         object->items[np++] = tmp[op++];
96,243✔
353
      else if (old_has & mask) {
981,347✔
354
         if (ITEM_OBJ_ARRAY & mask)
146✔
355
            obj_array_free(&(tmp[op].obj_array));
146✔
356
         ++op;
146✔
357
      }
358
      else if (new_has & mask)
981,201✔
359
         memset(&(object->items[np++]), '\0', sizeof(item_t));
1,077,590✔
360
   }
361

362
   object->kind = kind;
28,361✔
363
}
364

365
static void object_init(object_class_t *class)
33,720✔
366
{
367
   class->object_size = xmalloc_array(class->last_kind, sizeof(size_t));
33,720✔
368

369
   assert(class->last_kind < (1 << (sizeof(uint8_t) * 8)));
33,720✔
370

371
   assert(class->tag < ARRAY_LEN(classes));
33,720✔
372
   classes[class->tag] = class;
33,720✔
373

374
#ifdef DEBUG
375
   imask_t all_items = 0;
33,720✔
376
#endif
377

378
   for (int i = 0; i < class->last_kind; i++) {
2,267,670✔
379
      const int nitems = __builtin_popcountll(class->has_map[i]);
2,233,950✔
380
      class->object_size[i] = sizeof(object_t) + (nitems * sizeof(item_t));
2,233,950✔
381
      DEBUG_ONLY(all_items |= class->has_map[i]);
2,233,950✔
382

383
      format_digest += knuth_hash(class->has_map[i] >> 32);
2,233,950✔
384
      format_digest += knuth_hash(class->has_map[i]);
2,233,950✔
385
   }
386

387
   bool changed = false;
50,580✔
388
   do {
50,580✔
389
      changed = false;
50,580✔
390
      for (int i = 0; i < class->last_kind; i++) {
4,307,730✔
391
         size_t max_size = class->object_size[i];
4,257,150✔
392
         for (size_t j = 0; class->change_allowed[j][0] != -1; j++) {
31,570,350✔
393
            if (class->change_allowed[j][0] == i)
27,313,200✔
394
               max_size = MAX(max_size,
227,610✔
395
                              class->object_size[class->change_allowed[j][1]]);
396
            else if (class->change_allowed[j][1] == i)
27,085,590✔
397
               max_size = MAX(max_size,
227,610✔
398
                              class->object_size[class->change_allowed[j][0]]);
399
         }
400

401
         if (max_size != class->object_size[i]) {
4,257,150✔
402
            class->object_size[i] = max_size;
59,010✔
403
            changed = true;
59,010✔
404
         }
405
      }
406
   } while (changed);
50,580✔
407

408
#ifdef DEBUG
409
   if (getenv("NVC_TREE_SIZES") != NULL) {
33,720✔
410
      for (int i = 0; i < class->last_kind; i++)
×
411
         printf("%-15s %d\n", class->kind_text_map[i],
×
412
                (int)class->object_size[i]);
×
413
   }
414

415
   const imask_t known_types =
33,720✔
416
      ITEM_IDENT | ITEM_OBJECT | ITEM_OBJ_ARRAY | ITEM_INT64 | ITEM_INT32
417
      | ITEM_DOUBLE | ITEM_NUMBER;
418

419
   const imask_t missing = all_items & ~known_types;
33,720✔
420
   if (missing != 0) {
33,720✔
421
      int item;
422
      for (item = 0; (missing & (UINT64_C(1) << item)) == 0; item++)
×
423
         ;
424

425
      assert(item < ARRAY_LEN(item_text_map));
×
426
      fatal_trace("item %s does not have a type", item_text_map[item]);
427
   }
428
#endif
429
}
33,720✔
430

431
static void check_frozen_object_fault(int sig, void *addr,
×
432
                                      struct cpu_state *cpu, void *context)
433
{
434
#ifndef __MINGW32__
435
   if (sig != SIGSEGV && sig != SIGBUS)
×
436
      return;
437
#endif
438

439
   for (unsigned i = 1; i < all_arenas.count; i++) {
×
440
      object_arena_t *arena = AGET(all_arenas, i);
×
441
      if (!arena->frozen)
×
442
         continue;
×
443
      else if (addr < arena->base)
×
444
         continue;
×
445
      else if (addr >= arena->limit)
×
446
         continue;
×
447

448
      fatal_trace("Write to object in frozen arena %s [address=%p]",
449
                  istr(object_arena_name(arena)), addr);
450
   }
451
}
452

453
static void object_one_time_init(void)
45,252,146✔
454
{
455
   INIT_ONCE({
45,252,146✔
456
         extern object_class_t tree_object;
457
         object_init(&tree_object);
458

459
         extern object_class_t type_object;
460
         object_init(&type_object);
461

462
         extern object_class_t vlog_object;
463
         object_init(&vlog_object);
464

465
         extern object_class_t psl_object;
466
         object_init(&psl_object);
467

468
         // Increment this each time an incompatible change is made to
469
         // the on-disk format not expressed in the object items table
470
         const uint32_t format_fudge = 46;
471

472
         format_digest += format_fudge * UINT32_C(2654435761);
473

474
         add_fault_handler(check_frozen_object_fault, NULL);
475

476
         arena_lookup = chash_new(64);
477
      });
478
}
45,252,146✔
479

480
static bool is_gc_root(const object_class_t *class, int kind)
2,957,269✔
481
{
482
   for (int j = 0; j < class->gc_num_roots; j++) {
26,803,967✔
483
      if (class->gc_roots[j] == kind)
23,938,553✔
484
         return true;
485
   }
486

487
   return false;
488
}
489

490
object_t *object_new(object_arena_t *arena,
45,176,862✔
491
                     const object_class_t *class, int kind)
492
{
493
   if (unlikely(kind >= class->last_kind))
45,176,862✔
494
      fatal_trace("invalid kind %d for %s object", kind, class->name);
495

496
   object_one_time_init();
45,176,862✔
497

498
   if (arena == NULL)
45,176,862✔
499
      arena = global_arena;
2,638,439✔
500

501
   if (unlikely(arena == NULL))
45,176,862✔
502
      fatal_trace("allocating object without active arena");
503

504
   const size_t size = ALIGN_UP(class->object_size[kind], OBJECT_ALIGN);
45,176,862✔
505

506
   assert(((uintptr_t)arena->alloc & (OBJECT_ALIGN - 1)) == 0);
45,176,862✔
507

508
   if (unlikely(arena->limit - arena->alloc < size)) {
45,176,862✔
509
      diag_t *d = diag_new(DIAG_FATAL, NULL);
×
510
      diag_suppress(d, false);
×
511
      diag_printf(d, "memory exhausted while creating unit %s",
×
512
                  istr(object_arena_name(arena)));
513
      diag_hint(d, NULL, "The current limit is %zu bytes which you can "
×
514
                "increase with the $bold$-M$$ option, for example "
515
                "$bold$-M 32m$$", object_arena_default_size());
516
      diag_emit(d);
×
517
      fatal_exit(EXIT_FAILURE);
×
518
   }
519

520
   object_t *object = arena->alloc;
45,176,862✔
521
   arena->alloc = (char *)arena->alloc + size;
45,176,862✔
522

523
   if (arena->root == NULL && is_gc_root(class, kind))
45,176,862✔
524
      arena->root = object;
62,002✔
525

526
   memset(object, '\0', size);
45,176,862✔
527

528
   object->kind  = kind;
45,176,862✔
529
   object->tag   = class->tag;
45,176,862✔
530
   object->arena = arena->key;
45,176,862✔
531
   object->loc   = LOC_INVALID;
45,176,862✔
532

533
   return object;
45,176,862✔
534
}
535

536
static void gc_mark_from_root(object_t *object, object_arena_t *arena,
6,327,522✔
537
                              generation_t generation)
538
{
539
   if (object == NULL)
6,327,522✔
540
      return;
541
   else if (!object_in_arena_p(arena, object))
5,295,569✔
542
      return;
543
   else if (object_marked_p(object, generation))
3,267,205✔
544
      return;
545

546
   const object_class_t *class = classes[object->tag];
2,469,137✔
547

548
   imask_t has = class->has_map[object->kind];
2,469,137✔
549
   for (int n = 0; has; has &= has - 1, n++) {
12,853,087✔
550
      const uint64_t mask = has & -has;
10,383,950✔
551
      item_t *item = &(object->items[n]);
10,383,950✔
552
      if (ITEM_OBJECT & mask)
10,383,950✔
553
         gc_mark_from_root(item->object, arena, generation);
4,447,362✔
554
      else if (ITEM_OBJ_ARRAY & mask) {
5,936,588✔
555
         if (item->obj_array != NULL) {
1,070,176✔
556
            for (unsigned j = 0; j < item->obj_array->count; j++)
2,524,253✔
557
               gc_mark_from_root(item->obj_array->items[j], arena,
1,850,307✔
558
                                 generation);
559
         }
560
      }
561
   }
562
}
563

564
static void gc_free_external(object_t *object)
426,127✔
565
{
566
   const object_class_t *class = classes[object->tag];
426,127✔
567

568
   imask_t has = class->has_map[object->kind];
426,127✔
569
   if ((has & ITEM_OBJ_ARRAY) == 0)
426,127✔
570
      return;
571

572
   for (int n = 0; has; has &= has - 1, n++) {
494,170✔
573
      const uint64_t mask = has & -has;
411,575✔
574
      item_t *item = &(object->items[n]);
411,575✔
575
      if (ITEM_OBJ_ARRAY & mask)
411,575✔
576
         obj_array_free(&(item->obj_array));
135,623✔
577
   }
578
}
579

580
static void object_arena_gc(object_arena_t *arena)
23,071✔
581
{
582
   const generation_t generation = object_next_generation();
23,071✔
583
   const uint64_t start_ticks = get_timestamp_us();
23,071✔
584

585
   // Mark
586
   for (void *p = arena->base; p != arena->alloc; ) {
2,918,335✔
587
      assert(p < arena->alloc);
2,895,264✔
588
      object_t *object = p;
2,895,264✔
589

590
      const object_class_t *class = classes[object->tag];
2,895,264✔
591

592
      if (is_gc_root(class, object->kind))
2,895,264✔
593
         gc_mark_from_root(object, arena, generation);
29,853✔
594

595
      const size_t size =
2,895,264✔
596
         ALIGN_UP(class->object_size[object->kind], OBJECT_ALIGN);
2,895,264✔
597
      p = (char *)p + size;
2,895,264✔
598
   }
599

600
   const size_t fwdsz = (arena->alloc - arena->base) / OBJECT_ALIGN;
23,071✔
601
   uint32_t *forward = xmalloc_array(fwdsz, sizeof(uint32_t));
23,071✔
602

603
   // Must initialise here for the search in object_from_locus
604
   memset(forward, 0xff, fwdsz * sizeof(uint32_t));
23,071✔
605

606
   // Calculate forwarding addresses
607
   unsigned woffset = 0, live = 0, dead = 0;
23,071✔
608
   for (void *rptr = arena->base; rptr != arena->alloc; ) {
2,918,335✔
609
      assert(rptr < arena->alloc);
2,895,264✔
610
      object_t *object = rptr;
2,895,264✔
611

612
      const object_class_t *class = classes[object->tag];
2,895,264✔
613

614
      const size_t size =
2,895,264✔
615
         ALIGN_UP(class->object_size[object->kind], OBJECT_ALIGN);
2,895,264✔
616

617
      ptrdiff_t index = (rptr - arena->base) >> OBJECT_ALIGN_BITS;
2,895,264✔
618
      if (!object_marked_p(object, generation)) {
2,895,264✔
619
         forward[index] = UINT32_MAX;
426,127✔
620
         gc_free_external(object);
426,127✔
621
         dead++;
426,127✔
622
      }
623
      else {
624
         forward[index] = woffset;
2,469,137✔
625
         woffset += size;
2,469,137✔
626
         live++;
2,469,137✔
627
      }
628

629
      rptr = (char *)rptr + size;
2,895,264✔
630
   }
631

632
   if (woffset == 0)
23,071✔
633
      fatal_trace("GC removed all objects from arena %s",
634
                  istr(object_arena_name(arena)));
635

636
   arena->forward = forward;
23,071✔
637
   arena->live_bytes = woffset;
23,071✔
638

639
   if (opt_get_verbose(OPT_OBJECT_VERBOSE, NULL)) {
23,071✔
640
      const int ticks = get_timestamp_us() - start_ticks;
×
641
      debugf("GC: %s: freed %d objects; %d allocated [%d us]",
×
642
             istr(object_arena_name(arena)), dead, live, ticks);
643
   }
644
}
23,071✔
645

646
void object_visit(object_t *object, object_visit_ctx_t *ctx)
950,471✔
647
{
648
   // If `deep' then will follow links above the tree originally passed
649
   // to tree_visit - e.g. following references back to their declarations
650

651
   if (object == NULL)
950,471✔
652
      return;
653
   else if (object_marked_p(object, ctx->generation))
715,754✔
654
      return;
655

656
   const object_class_t *class = classes[object->tag];
679,997✔
657

658
   const bool visit =
1,359,994✔
659
      (object->tag == ctx->tag && object->kind == ctx->kind)
679,997✔
660
      || ctx->kind == class->last_kind;
1,356,833✔
661

662
   if (visit && ctx->preorder != NULL)
679,997✔
663
      (*ctx->preorder)(object, ctx->context);
×
664

665
   const imask_t deep_mask = ~(ctx->deep ? 0 : I_TYPE | I_REF);
679,997✔
666

667
   imask_t has = class->has_map[object->kind];
679,997✔
668
   for (int n = 0; has; has &= has - 1, n++) {
3,573,435✔
669
      const uint64_t mask = has & -has;
2,893,438✔
670
      if (mask & deep_mask) {
2,893,438✔
671
         item_t *item = &(object->items[n]);
2,050,225✔
672
         if (ITEM_OBJECT & mask)
2,050,225✔
673
            object_visit(item->object, ctx);
475,161✔
674
         else if (ITEM_OBJ_ARRAY & mask) {
1,575,064✔
675
            if (item->obj_array != NULL) {
209,820✔
676
               for (unsigned j = 0; j < item->obj_array->count; j++)
590,832✔
677
                  object_visit(item->obj_array->items[j], ctx);
462,602✔
678
            }
679
         }
680
      }
681
   }
682

683
   if (visit) {
679,997✔
684
      if (ctx->postorder != NULL)
658,694✔
685
         (*ctx->postorder)(object, ctx->context);
658,694✔
686
      ctx->count++;
658,694✔
687
   }
688
}
689

690
static object_t *object_rewrite_iter(object_t *object,
4,701,213✔
691
                                     object_rewrite_ctx_t *ctx)
692
{
693
   // The callback may return a new object or a pointer to an existing
694
   // object in the same arena that that needs to be rewritten so
695
   // iterate rewriting until we reach a fixed point
696
   object_t *new = (*ctx->post_fn[object->tag])(object, ctx->context);
4,701,213✔
697
   if (new == object)
4,701,208✔
698
      return new;
699
   else
700
      return object_rewrite(new, ctx);
80,786✔
701
}
702

703
object_t *object_rewrite(object_t *object, object_rewrite_ctx_t *ctx)
10,629,072✔
704
{
705
   if (object == NULL)
10,629,072✔
706
      return NULL;
707

708
   if (!object_in_arena_p(ctx->arena, object))
8,887,557✔
709
      return object;
710

711
   const ptrdiff_t index =
5,991,003✔
712
      ((void *)object - ctx->arena->base) >> OBJECT_ALIGN_BITS;
5,991,003✔
713

714
   // New objects can be allocated while rewrite is in progress so we
715
   // need to check if the index is greater than the current cache size
716
   if (unlikely(ctx->cache == NULL || index >= ctx->cache_sz)) {
5,991,003✔
717
      ctx->cache_sz = (ctx->arena->alloc - ctx->arena->base) / OBJECT_ALIGN;
100,441✔
718
      ctx->cache = xrealloc_array(ctx->cache, sizeof(object_t *),
100,441✔
719
                                  ctx->cache_sz);
720
   }
721

722
   if (object_marked_p(object, ctx->generation)) {
5,991,003✔
723
      if (ctx->cache[index] == (object_t *)-1) {
1,006,790✔
724
         // Found a circular reference: eagerly rewrite the object now
725
         // and break the cycle
726
         if (ctx->post_fn[object->tag] != NULL) {
1,466✔
727
            if (ctx->pre_fn[object->tag] != NULL)
72✔
728
               (*ctx->pre_fn[object->tag])(object, ctx->context);
×
729
            object_t *new = object_rewrite_iter(object, ctx);
72✔
730
            object_write_barrier(object, new);
72✔
731
            return (ctx->cache[index] = new);
72✔
732
         }
733
         else
734
            return (ctx->cache[index] = object);
1,394✔
735
      }
736
      else {
737
         // Already rewritten this tree so return the cached version
738
         return ctx->cache[index];
739
      }
740
   }
741

742
   ctx->cache[index] = (object_t *)-1;  // Rewrite in progress marker
4,984,213✔
743

744
   if (ctx->pre_fn[object->tag] != NULL)
4,984,213✔
745
      (*ctx->pre_fn[object->tag])(object, ctx->context);
×
746

747
   const imask_t skip_mask =
4,984,213✔
748
      I_REF | ITEM_INT64 | ITEM_INT32 | ITEM_DOUBLE | ITEM_NUMBER | ITEM_IDENT;
749

750
   const object_class_t *class = classes[object->tag];
4,984,213✔
751

752
   imask_t has = class->has_map[object->kind];
4,984,213✔
753
   for (int n = 0; has; has &= has - 1, n++) {
26,283,991✔
754
      const uint64_t mask = has & -has;
21,299,788✔
755
      if (mask & ~skip_mask) {
21,299,788✔
756
         if (ITEM_OBJECT & mask) {
8,975,628✔
757
            object_t *o = object_rewrite(object->items[n].object, ctx);
7,020,558✔
758
            object->items[n].object = o;
7,020,553✔
759
            object_write_barrier(object, o);
7,020,553✔
760
         }
761
         else if (ITEM_OBJ_ARRAY & mask) {
1,955,070✔
762
            obj_array_t **a = &(object->items[n].obj_array);
1,955,070✔
763
            if (object->items[n].obj_array != NULL) {
1,955,070✔
764
               // The callback may add new items to the array so the
765
               // array pointer cannot be cached between iterations
766
               unsigned wptr = 0;
767
               for (size_t i = 0; i < object->items[n].obj_array->count; i++) {
4,752,769✔
768
                  object_t *o = object->items[n].obj_array->items[i];
3,485,059✔
769
                  if ((o = object_rewrite(o, ctx))) {
3,485,059✔
770
                     object_write_barrier(object, o);
3,479,211✔
771
                     object->items[n].obj_array->items[wptr++] = o;
3,479,211✔
772
                  }
773
               }
774

775
               if (wptr == 0)
1,267,710✔
776
                  obj_array_free(a);
1,071✔
777
               else
778
                  (*a)->count = wptr;
1,266,639✔
779
            }
780
         }
781
         else
782
            should_not_reach_here();
783
      }
784
   }
785

786
   if (ctx->cache[index] != (object_t *)-1) {
4,984,203✔
787
      // The cache was already updated due to a circular reference
788
      return ctx->cache[index];
789
   }
790
   else if (ctx->post_fn[object->tag] != NULL) {
4,982,737✔
791
      object_t *new = object_rewrite_iter(object, ctx);
4,701,141✔
792
      object_write_barrier(object, new);
4,701,136✔
793
      return (ctx->cache[index] = new);
4,701,136✔
794
   }
795
   else
796
      return (ctx->cache[index] = object);
281,596✔
797
}
798

799
static void object_write_ref(object_t *object, fbuf_t *f)
5,777,810✔
800
{
801
   if (object == NULL)
5,777,810✔
802
      fbuf_put_uint(f, 0);
968,035✔
803
   else {
804
      object_arena_t *arena = __object_arena(object);
4,809,775✔
805
      assert(arena->key != 0);
4,809,775✔
806
      fbuf_put_uint(f, arena->key);
4,809,775✔
807

808
      ptrdiff_t index = ((void *)object - arena->base) >> OBJECT_ALIGN_BITS;
4,809,775✔
809
      if (arena->forward != NULL)
4,809,775✔
810
         fbuf_put_uint(f, arena->forward[index] >> OBJECT_ALIGN_BITS);
3,137,341✔
811
      else
812
         fbuf_put_uint(f, index);
1,672,434✔
813
   }
814
}
5,777,810✔
815

816
void object_write(object_t *root, fbuf_t *f, ident_wr_ctx_t ident_ctx,
17,224✔
817
                  loc_wr_ctx_t *loc_ctx)
818
{
819
   object_arena_t *arena = __object_arena(root);
17,224✔
820
   if (root != arena_root(arena))
17,224✔
821
      fatal_trace("must write root object first");
822
   else if (arena->source == OBJ_DISK)
17,224✔
823
      fatal_trace("writing arena %s originally read from disk",
824
                  istr(object_arena_name(arena)));
825
   else if (!arena->frozen)
17,224✔
826
      fatal_trace("arena %s must be frozen before writing to disk",
827
                  istr(object_arena_name(arena)));
828
   else if (arena->obsolete)
17,224✔
829
      fatal_trace("writing obsolete arena %s", istr(object_arena_name(arena)));
830

831
   write_u32(format_digest, f);
17,224✔
832
   fbuf_put_uint(f, standard());
17,224✔
833
   fbuf_put_uint(f, ALIGN_UP(arena->live_bytes, OBJECT_PAGE_SZ));
17,224✔
834
   fbuf_put_uint(f, arena->flags);
17,224✔
835
   fbuf_put_uint(f, arena->key);
17,224✔
836
   ident_write(object_arena_name(arena), ident_ctx);
17,224✔
837

838
   arena_key_t max_key = arena->key;
17,224✔
839
   for (unsigned i = 0; i < arena->deps.count; i++)
51,540✔
840
      max_key = MAX(max_key, arena->deps.items[i]->key);
34,316✔
841
   fbuf_put_uint(f, max_key);
17,224✔
842

843
   fbuf_put_uint(f, arena->deps.count);
17,224✔
844
   for (unsigned i = 0; i < arena->deps.count; i++) {
51,540✔
845
      fbuf_put_uint(f, arena->deps.items[i]->key);
34,316✔
846
      fbuf_put_uint(f, arena->deps.items[i]->std);
34,316✔
847
      fbuf_put_uint(f, arena->deps.items[i]->checksum);
34,316✔
848
      ident_write(object_arena_name(arena->deps.items[i]), ident_ctx);
34,316✔
849
   }
850

851
   for (void *p = arena->base, *next; p != arena->alloc; p = next) {
2,680,780✔
852
      assert(p < arena->alloc);
2,663,556✔
853

854
      object_t *object = p;
2,663,556✔
855
      object_class_t *class = classes[object->tag];
2,663,556✔
856

857
      next = p + ALIGN_UP(class->object_size[object->kind], OBJECT_ALIGN);
2,663,556✔
858

859
      ptrdiff_t index = (p - arena->base) >> OBJECT_ALIGN_BITS;
2,663,556✔
860
      if (arena->forward[index] == UINT32_MAX)
2,663,556✔
861
         continue;   // Dead object
371,356✔
862

863
      STATIC_ASSERT(OBJECT_TAG_COUNT <= 4);
2,292,200✔
864
      fbuf_put_uint(f, object->tag | (object->kind << 2));
2,292,200✔
865

866
      if (class->has_loc)
2,292,200✔
867
         loc_write(&object->loc, loc_ctx);
2,159,287✔
868

869
      imask_t has = class->has_map[object->kind];
2,292,200✔
870
      for (int n = 0; has; has &= has - 1, n++) {
11,883,188✔
871
         const uint64_t mask = has & -has;
9,590,988✔
872
         item_t *item = &(object->items[n]);
9,590,988✔
873
         if (ITEM_IDENT & mask)
9,590,988✔
874
            ident_write(item->ident, ident_ctx);
1,791,834✔
875
         else if (ITEM_OBJECT & mask)
7,799,154✔
876
            object_write_ref(item->object, f);
4,187,342✔
877
         else if (ITEM_OBJ_ARRAY & mask) {
3,611,812✔
878
            if (item->obj_array != NULL) {
890,508✔
879
               const unsigned count = item->obj_array->count;
570,089✔
880
               fbuf_put_uint(f, count);
570,089✔
881
               for (unsigned i = 0; i < count; i++)
2,160,557✔
882
                  object_write_ref(item->obj_array->items[i], f);
1,590,468✔
883
            }
884
            else
885
               fbuf_put_uint(f, 0);
320,419✔
886
         }
887
         else if (ITEM_INT64 & mask)
2,721,304✔
888
            fbuf_put_int(f, item->ival);
464,604✔
889
         else if (ITEM_INT32 & mask)
2,256,700✔
890
            fbuf_put_int(f, item->ival);
2,070,339✔
891
         else if (ITEM_DOUBLE & mask)
186,361✔
892
            write_double(item->dval, f);
142,270✔
893
         else if (ITEM_NUMBER & mask)
44,091✔
894
            number_write(item->number, f);
44,091✔
895
         else
896
            should_not_reach_here();
897
      }
898
   }
899

900
   fbuf_put_uint(f, UINT16_MAX);   // End of objects marker
17,224✔
901
}
17,224✔
902

903
static object_t *object_read_ref(fbuf_t *f, const arena_key_t *key_map)
108,984,871✔
904
{
905
   arena_key_t key = fbuf_get_uint(f);
108,984,871✔
906
   if (key == 0)
108,984,871✔
907
      return NULL;
908

909
   arena_key_t mapped = key_map[key];
92,696,237✔
910
   ptrdiff_t offset = fbuf_get_uint(f) << OBJECT_ALIGN_BITS;
92,696,237✔
911

912
   if (unlikely(mapped == 0))
92,696,237✔
913
      fatal_trace("%s missing dependency with key %d", fbuf_file_name(f), key);
914

915
   assert(mapped < all_arenas.count);
92,696,237✔
916
   assert(mapped > 0);
92,696,237✔
917

918
   object_arena_t *arena = all_arenas.items[mapped];
92,696,237✔
919
   assert(!arena->frozen || offset < arena->alloc - arena->base);
92,696,237✔
920

921
   return (object_t *)((char *)arena->base + offset);
92,696,237✔
922
}
923

924
object_t *object_read(fbuf_t *f, object_load_fn_t loader_fn,
38,277✔
925
                      ident_rd_ctx_t ident_ctx, loc_rd_ctx_t *loc_ctx)
926
{
927
   object_one_time_init();
38,277✔
928

929
   const uint32_t ver = read_u32(f);
38,277✔
930
   if (ver != format_digest)
38,277✔
931
      fatal("%s: serialised format digest is %x expected %x. This design "
×
932
            "unit uses a library format from an earlier version of "
933
            PACKAGE_NAME " and should be reanalysed.",
934
            fbuf_file_name(f), ver, format_digest);
935

936
   const vhdl_standard_t std = fbuf_get_uint(f);
38,277✔
937

938
   // If this is the first design unit we've loaded then allow it to set
939
   // the default standard
940
   if (all_arenas.count == 0)
38,277✔
941
      set_default_standard(std);
1,820✔
942

943
   if (std > standard())
38,277✔
944
      fatal("%s: design unit was analysed using standard revision %s which "
8✔
945
            "is more recent that the currently selected standard %s",
946
            fbuf_file_name(f), standard_text(std), standard_text(standard()));
947

948
   const unsigned size = fbuf_get_uint(f);
38,269✔
949
   if (size & OBJECT_PAGE_MASK)
38,269✔
950
      fatal("%s: arena size %x bad alignment", fbuf_file_name(f), size);
×
951

952
   object_arena_t *arena = object_arena_new(size, std);
38,269✔
953
   arena->source = OBJ_DISK;
38,269✔
954
   arena->flags  = fbuf_get_uint(f);
38,269✔
955

956
   arena_key_t key = fbuf_get_uint(f);
38,269✔
957
   ident_t name = ident_read(ident_ctx);
38,269✔
958

959
   arena_key_t max_key = fbuf_get_uint(f);
38,269✔
960

961
   arena_key_t *key_map LOCAL = xcalloc_array(max_key + 1, sizeof(arena_key_t));
38,269✔
962
   key_map[key] = arena->key;
38,269✔
963

964
   const int ndeps = fbuf_get_uint(f);
38,269✔
965
   for (int i = 0; i < ndeps; i++) {
91,399✔
966
      arena_key_t dkey = fbuf_get_uint(f);
53,134✔
967
      vhdl_standard_t dstd = fbuf_get_uint(f);
53,134✔
968
      uint32_t checksum = fbuf_get_uint(f);
53,134✔
969
      ident_t dep = ident_read(ident_ctx);
53,134✔
970

971
      object_arena_t *a = NULL;
53,134✔
972
      for (unsigned j = 1; a == NULL && j < all_arenas.count; j++) {
261,304✔
973
         if (dep == object_arena_name(all_arenas.items[j]))
208,170✔
974
            a = all_arenas.items[j];
40,832✔
975
      }
976

977
      if (a == NULL) {
53,134✔
978
         object_t *droot = NULL;
12,302✔
979
         if (loader_fn) droot = (*loader_fn)(dep);
12,302✔
980

981
         if (droot == NULL)
12,302✔
982
            fatal("%s depends on %s which cannot be found",
×
983
                  fbuf_file_name(f), istr(dep));
984

985
         a = __object_arena(droot);
12,302✔
986
      }
987

988
      if (a->std != dstd)
53,134✔
989
         fatal("%s: design unit depends on %s version of %s but conflicting "
×
990
               "%s version has been loaded", fbuf_file_name(f),
991
               standard_text(dstd), istr(dep), standard_text(a->std));
992
      else if (a->checksum != checksum) {
53,134✔
993
         diag_t *d = diag_new(DIAG_FATAL, NULL);
4✔
994
         diag_suppress(d, false);
4✔
995
         diag_printf(d, "%s: design unit depends on %s with checksum %08x "
4✔
996
                     "but the current version in the library has checksum %08x",
997
                     fbuf_file_name(f), istr(dep), checksum, a->checksum);
998
         diag_hint(d, NULL, "this usually means %s is outdated and needs to "
4✔
999
                   "be reanalysed", istr(name));
1000
         diag_emit(d);
4✔
1001
         fatal_exit(EXIT_FAILURE);
4✔
1002
      }
1003

1004
      APUSH(arena->deps, a);
53,130✔
1005

1006
      assert(dkey <= max_key);
53,130✔
1007
      key_map[dkey] = a->key;
53,130✔
1008
   }
1009

1010
   for (;;) {
42,245,314✔
1011
      const uint64_t hdr = fbuf_get_uint(f);
42,245,314✔
1012
      if (hdr == UINT16_MAX) break;
42,245,314✔
1013

1014
      const unsigned tag = hdr & 3;
42,207,049✔
1015
      const unsigned kind = hdr >> 2;
42,207,049✔
1016

1017
      assert(tag < OBJECT_TAG_COUNT);
42,207,049✔
1018

1019
      const object_class_t *class = classes[tag];
42,207,049✔
1020

1021
      object_t *object = object_new(arena, class, kind);
42,207,049✔
1022

1023
      if (class->has_loc)
42,207,049✔
1024
         loc_read(&(object->loc), loc_ctx);
38,221,494✔
1025

1026
      imask_t has = class->has_map[object->kind];
42,207,049✔
1027
      for (int n = 0; has; has &= has - 1, n++) {
230,253,791✔
1028
         const uint64_t mask = has & -has;
188,046,742✔
1029
         item_t *item = &(object->items[n]);
188,046,742✔
1030
         if (ITEM_IDENT & mask)
188,046,742✔
1031
            item->ident = ident_read(ident_ctx);
37,848,226✔
1032
         else if (ITEM_OBJECT & mask)
150,198,516✔
1033
            item->object = object_read_ref(f, key_map);
76,336,324✔
1034
         else if (ITEM_OBJ_ARRAY & mask) {
73,862,192✔
1035
            const unsigned count = fbuf_get_uint(f);
17,603,765✔
1036
            if (count > 0) {
17,603,765✔
1037
               item->obj_array = xmalloc_flex(sizeof(obj_array_t),
11,621,726✔
1038
                                              count, sizeof(object_t *));
1039
               item->obj_array->count =
11,621,726✔
1040
                  item->obj_array->limit = count;
11,621,726✔
1041
               for (unsigned i = 0; i < count; i++) {
44,270,273✔
1042
                  object_t *o = object_read_ref(f, key_map);
32,648,547✔
1043
                  item->obj_array->items[i] = o;
32,648,547✔
1044
               }
1045
            }
1046
         }
1047
         else if ((ITEM_INT64 | ITEM_INT32) & mask)
56,258,427✔
1048
            item->ival = fbuf_get_int(f);
54,808,806✔
1049
         else if (ITEM_DOUBLE & mask)
1,449,621✔
1050
            item->dval = read_double(f);
1,438,211✔
1051
         else if (ITEM_NUMBER & mask)
11,410✔
1052
            item->number = number_read(f);
11,410✔
1053
         else
1054
            should_not_reach_here();
1055
      }
1056
   }
1057

1058
   assert(ALIGN_UP(arena->alloc - arena->base, OBJECT_PAGE_SZ) == size);
38,265✔
1059

1060
   object_arena_freeze(arena);
38,265✔
1061
   return (object_t *)arena->base;
38,265✔
1062
}
1063

1064
unsigned object_next_generation(void)
87,352✔
1065
{
1066
   return next_generation++;
87,352✔
1067
}
1068

1069
static bool object_copy_mark(object_t *object, object_copy_ctx_t *ctx)
4,309,439✔
1070
{
1071
   if (object == NULL)
4,309,439✔
1072
      return false;
1073

1074
   object_arena_t *arena = __object_arena(object);
3,554,279✔
1075
   if (arena->copygen != ctx->generation)
3,554,279✔
1076
      return false;
1077

1078
   bool marked = false;
2,252,414✔
1079
   if (object_marked_p(object, ctx->generation)) {
2,252,414✔
1080
      object_t *map = hash_get(ctx->copy_map, object);
527,198✔
1081
      if (map == object)
527,198✔
1082
         marked = true;   // Marked as root
1083
      else
1084
         return map != NULL;
510,700✔
1085
   }
1086

1087
   if (!marked && ctx->should_copy[object->tag] != NULL)
1,725,216✔
1088
      marked = (*ctx->should_copy[object->tag])(object, ctx->pred_context);
1,634,490✔
1089

1090
   const object_class_t *class = classes[object->tag];
1,741,714✔
1091

1092
   object_t *copy = NULL;
1,741,714✔
1093
   if (marked) {
1,741,714✔
1094
      copy = object_new(global_arena, class, object->kind);
44,720✔
1095
      hash_put(ctx->copy_map, object, copy);
44,720✔
1096
   }
1097

1098
   imask_t has = class->has_map[object->kind];
1,741,714✔
1099
   for (int n = 0; has; has &= has - 1, n++) {
8,893,269✔
1100
      const uint64_t mask = has & -has;
7,151,555✔
1101
      item_t *item = &(object->items[n]);
7,151,555✔
1102
      if (ITEM_OBJECT & mask)
7,151,555✔
1103
         marked |= object_copy_mark(item->object, ctx);
3,171,250✔
1104
      else if (ITEM_OBJ_ARRAY & mask) {
3,980,305✔
1105
         if (item->obj_array != NULL) {
606,161✔
1106
            for (unsigned i = 0; i < item->obj_array->count; i++) {
1,504,826✔
1107
               object_t *o = item->obj_array->items[i];
1,121,441✔
1108
               marked |= object_copy_mark(o, ctx);
1,121,441✔
1109
            }
1110
         }
1111
      }
1112
   }
1113

1114
   if (marked && copy == NULL) {
1,741,714✔
1115
      copy = object_new(global_arena, class, object->kind);
286,654✔
1116
      hash_put(ctx->copy_map, object, copy);
286,654✔
1117
   }
1118

1119
   return marked;
1120
}
1121

1122
void object_copy_mark_root(object_t *object, object_copy_ctx_t *ctx)
16,965✔
1123
{
1124
   object_arena_t *arena = __object_arena(object);
16,965✔
1125

1126
   arena->copygen = ctx->generation;
16,965✔
1127

1128
   if (!object_marked_p(object, ctx->generation))
16,965✔
1129
      hash_put(ctx->copy_map, object, object);
16,498✔
1130
}
16,965✔
1131

1132
static object_t *object_copy_map(object_t *object, object_copy_ctx_t *ctx)
968,451✔
1133
{
1134
   if (object == NULL)
968,451✔
1135
      return NULL;
1136

1137
   object_t *map = hash_get(ctx->copy_map, object);
828,005✔
1138
   return map ?: object;
828,005✔
1139
}
1140

1141
void object_copy_begin(object_copy_ctx_t *ctx)
8,904✔
1142
{
1143
   ctx->copy_map = hash_new(1024);
8,904✔
1144

1145
   for (int i = 0; i < ctx->nroots; i++)
25,652✔
1146
      __object_arena(ctx->roots[i])->copygen = ctx->generation;
16,748✔
1147
}
8,904✔
1148

1149
void object_copy_finish(object_copy_ctx_t *ctx)
8,904✔
1150
{
1151
   for (int i = 0; i < ctx->nroots; i++)
25,652✔
1152
      (void)object_copy_mark(ctx->roots[i], ctx);
16,748✔
1153

1154
   unsigned ncopied = 0;
8,904✔
1155
   const void *key;
8,904✔
1156
   void *value;
8,904✔
1157
   for (hash_iter_t it = HASH_BEGIN;
8,904✔
1158
        hash_iter(ctx->copy_map, &it, &key, &value); ) {
340,278✔
1159
      const object_t *object = key;
331,374✔
1160
      object_t *copy = value;
331,374✔
1161
      assert(copy != object);
331,374✔
1162
      ncopied++;
331,374✔
1163

1164
      copy->loc = object->loc;
331,374✔
1165

1166
      const object_class_t *class = classes[object->tag];
331,374✔
1167

1168
      imask_t has = class->has_map[object->kind];
331,374✔
1169
      for (int n = 0; has; has &= has - 1, n++) {
1,824,493✔
1170
         const uint64_t mask = has & -has;
1,493,119✔
1171
         const item_t *from = &(object->items[n]);
1,493,119✔
1172
         item_t *to = &(copy->items[n]);
1,493,119✔
1173

1174
         if (ITEM_IDENT & mask)
1,493,119✔
1175
            to->ident = from->ident;
272,820✔
1176
         else if (ITEM_OBJECT & mask) {
1,220,299✔
1177
            to->object = object_copy_map(from->object, ctx);
621,497✔
1178
            object_write_barrier(copy, to->object);
621,497✔
1179
         }
1180
         else if (ITEM_DOUBLE & mask)
598,802✔
1181
            to->dval = from->dval;
182✔
1182
         else if (ITEM_OBJ_ARRAY & mask) {
598,620✔
1183
            if (from->obj_array != NULL) {
219,068✔
1184
               // TODO: make a resize macro
1185
               to->obj_array = xmalloc_flex(sizeof(obj_array_t),
294,198✔
1186
                                            from->obj_array->count,
147,099✔
1187
                                            sizeof(object_t *));
1188
               to->obj_array->count =
147,099✔
1189
                  to->obj_array->limit = from->obj_array->count;
147,099✔
1190
               for (size_t i = 0; i < from->obj_array->count; i++) {
494,053✔
1191
                  object_t *o =
346,954✔
1192
                     object_copy_map(from->obj_array->items[i], ctx);
346,954✔
1193
                  to->obj_array->items[i] = o;
346,954✔
1194
                  object_write_barrier(copy, o);
346,954✔
1195
               }
1196
            }
1197
         }
1198
         else if ((ITEM_INT64 | ITEM_INT32) & mask)
379,552✔
1199
            to->ival = from->ival;
379,552✔
1200
         else
1201
            should_not_reach_here();
1202
      }
1203
   }
1204

1205
   for (hash_iter_t it = HASH_BEGIN;
8,904✔
1206
        hash_iter(ctx->copy_map, &it, &key, &value); ) {
340,278✔
1207
      object_t *copy = value;
331,374✔
1208
      if (ctx->callback[copy->tag] != NULL)
331,374✔
1209
         (*ctx->callback[copy->tag])(copy, ctx->callback_context);
315,463✔
1210
   }
1211

1212
   if (opt_get_verbose(OPT_OBJECT_VERBOSE, NULL))
8,904✔
1213
      debugf("copied %d objects into arena %s", ncopied,
×
1214
             istr(object_arena_name(global_arena)));
1215

1216
   for (unsigned i = 0; i < ctx->nroots; i++) {
25,652✔
1217
      object_t *copy = hash_get(ctx->copy_map, ctx->roots[i]);
16,748✔
1218
      if (copy != NULL)
16,748✔
1219
         ctx->roots[i] = copy;
7,076✔
1220
   }
1221

1222
   hash_free(ctx->copy_map);
8,904✔
1223
}
8,904✔
1224

1225
size_t object_arena_default_size(void)
23,738✔
1226
{
1227
   return ALIGN_UP(opt_get_size(OPT_ARENA_SIZE), OBJECT_PAGE_SZ);
23,738✔
1228
}
1229

1230
object_arena_t *object_arena_new(size_t size, unsigned std)
62,007✔
1231
{
1232
   if (all_arenas.count == 0)
62,007✔
1233
      APUSH(all_arenas, NULL);   // Dummy null arena
8,417✔
1234

1235
   object_arena_t *arena = xcalloc(sizeof(object_arena_t));
62,007✔
1236
   arena->base   = nvc_memalign(OBJECT_PAGE_SZ, size);
62,007✔
1237
   arena->alloc  = arena->base;
62,007✔
1238
   arena->limit  = (char *)arena->base + size;
62,007✔
1239
   arena->key    = all_arenas.count;
62,007✔
1240
   arena->source = OBJ_FRESH;
62,007✔
1241
   arena->std    = std;
62,007✔
1242

1243
   APUSH(all_arenas, arena);
62,007✔
1244

1245
   if (all_arenas.count == UINT16_MAX - 1)
62,007✔
1246
      fatal_trace("too many object arenas");
1247

1248
   return arena;
62,007✔
1249
}
1250

1251
void object_arena_freeze(object_arena_t *arena)
61,336✔
1252
{
1253
   ident_t name = object_arena_name(arena);
61,336✔
1254

1255
   if (arena->frozen)
61,336✔
1256
      fatal_trace("arena %s already frozen", istr(name));
1257

1258
   if (arena->source == OBJ_FRESH)
61,336✔
1259
      object_arena_gc(arena);
23,071✔
1260

1261
   if (opt_get_verbose(OPT_OBJECT_VERBOSE, NULL))
61,336✔
1262
      debugf("arena %s frozen (%d bytes)", istr(name),
×
1263
             (int)(arena->alloc - arena->base));
×
1264

1265
   chash_put(arena_lookup, name, arena);
61,336✔
1266

1267
   void *next_page = ALIGN_UP(arena->alloc, OBJECT_PAGE_SZ);
61,336✔
1268
   nvc_memprotect(arena->base, next_page - arena->base, MEM_RO);
61,336✔
1269

1270
   if (next_page < arena->limit) {
61,336✔
1271
#if OBJECT_UNMAP_UNUSED
1272
      nvc_munmap(next_page, arena->limit - next_page);
1273
      arena->limit = next_page;
1274
#else
1275
      // This can be useful for debugging use-after-free
1276
      nvc_decommit(next_page, arena->limit - next_page);
23,071✔
1277
      nvc_memprotect(next_page, arena->limit - next_page, MEM_NONE);
23,071✔
1278
#endif
1279
   }
1280

1281
   arena->frozen = true;
61,336✔
1282
}
61,336✔
1283

1284
void arena_walk_deps(object_arena_t *arena, arena_deps_fn_t fn, void *context)
59,736✔
1285
{
1286
   for (unsigned i = 0; i < arena->deps.count; i++)
161,715✔
1287
      (*fn)(arena_root(arena->deps.items[i]), context);
101,979✔
1288
}
59,736✔
1289

1290
void arena_walk_obsolete_deps(object_arena_t *arena, arena_deps_fn_t fn,
17,224✔
1291
                              void *context)
1292
{
1293
   for (unsigned i = 0; i < arena->deps.count; i++) {
51,540✔
1294
      if (arena->deps.items[i]->obsolete)
34,316✔
1295
         (*fn)(arena_root(arena->deps.items[i]), context);
8✔
1296
   }
1297
}
17,224✔
1298

1299
void object_locus(object_t *object, ident_t *module, ptrdiff_t *offset)
×
1300
{
1301
   object_arena_t *arena = __object_arena(object);
×
1302
   assert(arena->frozen);
×
1303

1304
   *module = object_arena_name(arena);
×
1305

1306
   const ptrdiff_t index = ((void *)object - arena->base) >> OBJECT_ALIGN_BITS;
×
1307
   if (arena->forward != NULL) {
×
1308
      assert(arena->forward[index] != UINT32_MAX);   // Was GC'd
×
1309
      *offset = arena->forward[index] >> OBJECT_ALIGN_BITS;
×
1310
   }
1311
   else
1312
      *offset = index;
×
1313
}
×
1314

1315
static object_arena_t *arena_by_name(ident_t module)
×
1316
{
1317
   if (global_arena != NULL && object_arena_name(global_arena) == module)
×
1318
      return global_arena;
×
1319

1320
   object_arena_t *a = chash_get(arena_lookup, module);
×
1321
   if (a != NULL)
×
1322
      return a;
1323

1324
#if defined DEBUG && !defined __SANITIZE_THREAD__
1325
   for (int i = 1; i < all_arenas.count; i++)
×
1326
      assert(module != object_arena_name(all_arenas.items[i]));
×
1327
#endif
1328

1329
   return NULL;
1330
}
1331

1332
object_t *object_from_locus(ident_t module, ptrdiff_t offset,
×
1333
                            object_load_fn_t loader)
1334
{
1335
   object_arena_t *arena = arena_by_name(module);
×
1336

1337
   if (arena == NULL) {
×
1338
      object_t *droot = NULL;
×
1339
      if (loader) droot = (*loader)(module);
×
1340

1341
      if (droot == NULL)
×
1342
         fatal("cannot find object locus %s%+"PRIiPTR, istr(module), offset);
×
1343

1344
      arena = __object_arena(droot);
×
1345
   }
1346

1347
   assert(arena->frozen);
×
1348

1349
   void *ptr = NULL;
×
1350
   if (arena->forward != NULL) {
×
1351
      // TODO: could do binary search here
1352
      for (int i = 0; i < (arena->alloc - arena->base) / OBJECT_ALIGN; i++) {
×
1353
         if (arena->forward[i] == offset << OBJECT_ALIGN_BITS) {
×
1354
            ptr = arena->base + (i << OBJECT_ALIGN_BITS);
×
1355
            break;
×
1356
         }
1357
      }
1358
      assert(ptr != NULL);
×
1359
   }
1360
   else
1361
      ptr = arena->base + (offset << OBJECT_ALIGN_BITS);
×
1362

1363
   if (ptr > arena->limit)
×
1364
      fatal_trace("invalid object locus %s%+"PRIiPTR, istr(module), offset);
1365

1366
   object_t *obj = ptr;
×
1367
   if (obj->tag >= OBJECT_TAG_COUNT)
×
1368
      fatal_trace("invalid tag %d for object locus %s%+"PRIiPTR, obj->tag,
1369
                  istr(module), offset);
1370
   else if (obj->arena != arena->key)
×
1371
      fatal_trace("invalid arena key %d != %d for object locus %s%+"PRIiPTR,
1372
                  obj->arena, arena->key, istr(module), offset);
×
1373

1374
   return obj;
×
1375
}
1376

1377
void freeze_global_arena(void)
37,007✔
1378
{
1379
   object_one_time_init();
37,007✔
1380

1381
   if (global_arena != NULL) {
37,007✔
1382
      object_arena_freeze(global_arena);
23,071✔
1383
      global_arena = NULL;
23,071✔
1384
   }
1385
}
37,007✔
1386

1387
void make_new_arena(void)
23,738✔
1388
{
1389
   freeze_global_arena();
23,738✔
1390
   global_arena = object_arena_new(object_arena_default_size(), standard());
23,738✔
1391
}
23,738✔
1392

UNCOV
1393
void discard_global_arena(void)
×
1394
{
UNCOV
1395
   if (global_arena == NULL)
×
1396
      return;
1397

UNCOV
1398
   object_one_time_init();
×
1399

UNCOV
1400
   for (void *p = global_arena->base; p != global_arena->alloc; ) {
×
UNCOV
1401
      assert(p < global_arena->alloc);
×
UNCOV
1402
      object_t *object = p;
×
1403

UNCOV
1404
      gc_free_external(object);
×
1405

UNCOV
1406
      const object_class_t *class = classes[object->tag];
×
UNCOV
1407
      const size_t size =
×
UNCOV
1408
         ALIGN_UP(class->object_size[object->kind], OBJECT_ALIGN);
×
UNCOV
1409
      p = (char *)p + size;
×
1410
   }
1411

UNCOV
1412
   nvc_munmap(global_arena->base, global_arena->limit - global_arena->base);
×
1413

UNCOV
1414
   assert(all_arenas.items[all_arenas.count - 1] == global_arena);
×
UNCOV
1415
   APOP(all_arenas);
×
1416

UNCOV
1417
   free(global_arena);
×
UNCOV
1418
   global_arena = NULL;
×
1419
}
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