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

nickg / nvc / 22254670368

21 Feb 2026 08:57AM UTC coverage: 92.569% (-0.03%) from 92.601%
22254670368

push

github

nickg
Fix misleading message when protected method call is ambiguous

6 of 9 new or added lines in 1 file covered. (66.67%)

505 existing lines in 7 files now uncovered.

76900 of 83073 relevant lines covered (92.57%)

435372.87 hits per line

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

91.54
/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)
11,102,973✔
107
{
108
   return (void *)object >= arena->base && (void *)object < arena->limit;
11,102,973✔
109
}
110

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

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

126
   return ident_new("???");
10,652✔
127
}
128

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

135
   if (count == 1)
168,081✔
136
      arena->mark_bits[first] = 0;
146,594✔
137
   else
138
      memset(arena->mark_bits + first, '\0', count * sizeof(uint64_t));
21,487✔
139
}
168,081✔
140

141
static bool object_marked_p(object_t *object, generation_t generation)
11,508,790✔
142
{
143
   object_arena_t *arena = __object_arena(object);
11,508,790✔
144

145
   const uintptr_t bit = ((void *)object - arena->base) >> OBJECT_ALIGN_BITS;
11,508,790✔
146
   const uintptr_t word = bit / 64;
11,508,790✔
147

148
   if (unlikely(arena->mark_bits == NULL)) {
11,508,790✔
149
      const size_t nbits = (arena->limit - arena->base) / OBJECT_ALIGN;
17,537✔
150
      arena->mark_sz = ALIGN_UP(nbits, 64) / 8;
17,537✔
151
      arena->mark_bits = xmalloc(arena->mark_sz);
17,537✔
152
      arena->mark_bits[word] = 0;
17,537✔
153
      arena->mark_low = arena->mark_high = word;
17,537✔
154
      arena->generation = generation;
17,537✔
155
   }
156
   else if (arena->generation != generation) {
11,491,253✔
157
      arena->mark_bits[word] = 0;
56,146✔
158
      arena->mark_low = arena->mark_high = word;
56,146✔
159
      arena->generation = generation;
56,146✔
160
   }
161

162
   // Lazy zeroing of mark bits helps performance with large arenas
163
   if (word < arena->mark_low) {
11,508,790✔
164
      zero_mark_bits(arena, word, arena->mark_low - word);
7,128✔
165
      arena->mark_low = word;
7,128✔
166
   }
167
   else if (word > arena->mark_high) {
11,501,662✔
168
      zero_mark_bits(arena, arena->mark_high + 1, word - arena->mark_high);
160,953✔
169
      arena->mark_high = word;
160,953✔
170
   }
171

172
   const uint64_t mask = UINT64_C(1) << (bit & 63);
11,508,790✔
173

174
   const bool marked = !!(arena->mark_bits[word] & mask);
11,508,790✔
175
   arena->mark_bits[word] |= mask;
11,508,790✔
176

177
   return marked;
11,508,790✔
178
}
179

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

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

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

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

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

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

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

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

220
   if (has & I_IDENT) {
166,170✔
221
      const int n = __builtin_popcountll(has & (I_IDENT - 1));
163,768✔
222
      return object->items[n].ident;
163,768✔
223
   }
224

225
   return NULL;
226
}
227

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

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

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

242
   if (lhs_mask == rhs_mask || rhs == NULL)
5,035,602✔
243
      return;
244
   else if (lhs->arena == rhs->arena)
5,035,602✔
245
      return;
246

247
   object_arena_t *larena = __object_arena(lhs);
4,504,256✔
248
   object_arena_t *rarena = __object_arena(rhs);
4,504,256✔
249

250
   assert(!larena->frozen);
4,504,256✔
251
   assert(rarena->frozen);
4,504,256✔
252

253
   for (unsigned i = 0; i < larena->deps.count; i++) {
7,306,113✔
254
      if (larena->deps.items[i] == rarena)
7,269,020✔
255
         return;
256
   }
257

258
   APUSH(larena->deps, rarena);
37,093✔
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

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

279
void obj_array_add(obj_array_t **a, object_t *o)
1,341,604✔
280
{
281
   if (*a == NULL) {
1,341,604✔
282
      const int defsz = 8;
481,509✔
283
      *a = xmalloc_flex(sizeof(obj_array_t), defsz, sizeof(object_t *));
481,509✔
284
      (*a)->count = 0;
481,509✔
285
      (*a)->limit = defsz;
481,509✔
286
   }
287
   else if ((*a)->count == (*a)->limit) {
860,095✔
288
      (*a)->limit *= 2;
32,937✔
289
      *a = xrealloc_flex(*a, sizeof(obj_array_t),
32,937✔
290
                         (*a)->limit, sizeof(object_t *));
291
   }
292

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

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

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

312
   for (int i = 0; i < src->count; i++)
54,793✔
313
      (*dst)->items[(*dst)->count++] = src->items[i];
43,029✔
314
}
315

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

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

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

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

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

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

344
   const int max_items = MAX(old_nitems, new_nitems);
23,252✔
345

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

349
   int op = 0, np = 0;
23,252✔
350
   for (imask_t mask = 1; np < new_nitems; mask <<= 1) {
922,972✔
351
      if ((old_has & mask) && (new_has & mask))
899,720✔
352
         object->items[np++] = tmp[op++];
83,784✔
353
      else if (old_has & mask) {
815,936✔
354
         if (ITEM_OBJ_ARRAY & mask)
116✔
355
            obj_array_free(&(tmp[op].obj_array));
116✔
356
         ++op;
116✔
357
      }
358
      else if (new_has & mask)
815,820✔
359
         memset(&(object->items[np++]), '\0', sizeof(item_t));
899,720✔
360
   }
361

362
   object->kind = kind;
23,252✔
363
}
364

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

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

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

374
#ifdef DEBUG
375
   imask_t all_items = 0;
21,572✔
376
#endif
377

378
   for (int i = 0; i < class->last_kind; i++) {
1,445,324✔
379
      const int nitems = __builtin_popcountll(class->has_map[i]);
1,423,752✔
380
      class->object_size[i] = sizeof(object_t) + (nitems * sizeof(item_t));
1,423,752✔
381
      DEBUG_ONLY(all_items |= class->has_map[i]);
1,423,752✔
382

383
      format_digest += knuth_hash(class->has_map[i] >> 32);
1,423,752✔
384
      format_digest += knuth_hash(class->has_map[i]);
1,423,752✔
385
   }
386

387
   bool changed = false;
32,358✔
388
   do {
32,358✔
389
      changed = false;
32,358✔
390
      for (int i = 0; i < class->last_kind; i++) {
2,750,430✔
391
         size_t max_size = class->object_size[i];
2,718,072✔
392
         for (size_t j = 0; class->change_allowed[j][0] != -1; j++) {
20,191,392✔
393
            if (class->change_allowed[j][0] == i)
17,473,320✔
394
               max_size = MAX(max_size,
145,611✔
395
                              class->object_size[class->change_allowed[j][1]]);
396
            else if (class->change_allowed[j][1] == i)
17,327,709✔
397
               max_size = MAX(max_size,
145,611✔
398
                              class->object_size[class->change_allowed[j][0]]);
399
         }
400

401
         if (max_size != class->object_size[i]) {
2,718,072✔
402
            class->object_size[i] = max_size;
37,751✔
403
            changed = true;
37,751✔
404
         }
405
      }
406
   } while (changed);
32,358✔
407

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

415
   const imask_t known_types =
21,572✔
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;
21,572✔
420
   if (missing != 0) {
21,572✔
421
      int item;
422
      for (item = 0; (missing & (UINT64_C(1) << item)) == 0; item++)
×
423
         ;
424

UNCOV
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
}
21,572✔
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

UNCOV
439
   for (unsigned i = 1; i < all_arenas.count; i++) {
×
UNCOV
440
      object_arena_t *arena = AGET(all_arenas, i);
×
UNCOV
441
      if (!arena->frozen)
×
UNCOV
442
         continue;
×
UNCOV
443
      else if (addr < arena->base)
×
UNCOV
444
         continue;
×
UNCOV
445
      else if (addr >= arena->limit)
×
UNCOV
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)
25,768,423✔
454
{
455
   INIT_ONCE({
25,768,423✔
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
}
25,768,423✔
479

480
static bool is_gc_root(const object_class_t *class, int kind)
2,248,399✔
481
{
482
   for (int j = 0; j < class->gc_num_roots; j++) {
20,875,149✔
483
      if (class->gc_roots[j] == kind)
18,685,639✔
484
         return true;
485
   }
486

487
   return false;
488
}
489

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

496
   object_one_time_init();
25,723,052✔
497

498
   if (arena == NULL)
25,723,052✔
499
      arena = global_arena;
2,017,489✔
500

501
   if (unlikely(arena == NULL))
25,723,052✔
502
      fatal_trace("allocating object without active arena");
503

504
   const size_t size = ALIGN_UP(class->object_size[kind], OBJECT_ALIGN);
25,723,052✔
505

506
   assert(((uintptr_t)arena->alloc & (OBJECT_ALIGN - 1)) == 0);
25,723,052✔
507

508
   if (unlikely(arena->limit - arena->alloc < size)) {
25,723,052✔
UNCOV
509
      diag_t *d = diag_new(DIAG_FATAL, NULL);
×
UNCOV
510
      diag_suppress(d, false);
×
UNCOV
511
      diag_printf(d, "memory exhausted while creating unit %s",
×
512
                  istr(object_arena_name(arena)));
UNCOV
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());
UNCOV
516
      diag_emit(d);
×
UNCOV
517
      fatal_exit(EXIT_FAILURE);
×
518
   }
519

520
   object_t *object = arena->alloc;
25,723,052✔
521
   arena->alloc = (char *)arena->alloc + size;
25,723,052✔
522

523
   if (arena->root == NULL && is_gc_root(class, kind))
25,723,052✔
524
      arena->root = object;
36,768✔
525

526
   memset(object, '\0', size);
25,723,052✔
527

528
   object->kind  = kind;
25,723,052✔
529
   object->tag   = class->tag;
25,723,052✔
530
   object->arena = arena->key;
25,723,052✔
531
   object->loc   = LOC_INVALID;
25,723,052✔
532

533
   return object;
25,723,052✔
534
}
535

536
static void gc_mark_from_root(object_t *object, object_arena_t *arena,
4,976,889✔
537
                              generation_t generation)
538
{
539
   if (object == NULL)
4,976,889✔
540
      return;
541
   else if (!object_in_arena_p(arena, object))
4,149,034✔
542
      return;
543
   else if (object_marked_p(object, generation))
2,514,402✔
544
      return;
545

546
   const object_class_t *class = classes[object->tag];
1,888,054✔
547

548
   imask_t has = class->has_map[object->kind];
1,888,054✔
549
   for (int n = 0; has; has &= has - 1, n++) {
10,090,723✔
550
      const uint64_t mask = has & -has;
8,202,669✔
551
      item_t *item = &(object->items[n]);
8,202,669✔
552
      if (ITEM_OBJECT & mask)
8,202,669✔
553
         gc_mark_from_root(item->object, arena, generation);
3,511,198✔
554
      else if (ITEM_OBJ_ARRAY & mask) {
4,691,471✔
555
         if (item->obj_array != NULL) {
833,744✔
556
            for (unsigned j = 0; j < item->obj_array->count; j++)
1,973,219✔
557
               gc_mark_from_root(item->obj_array->items[j], arena,
1,443,570✔
558
                                 generation);
559
         }
560
      }
561
   }
562
}
563

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

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

572
   for (int n = 0; has; has &= has - 1, n++) {
372,885✔
573
      const uint64_t mask = has & -has;
310,613✔
574
      item_t *item = &(object->items[n]);
310,613✔
575
      if (ITEM_OBJ_ARRAY & mask)
310,613✔
576
         obj_array_free(&(item->obj_array));
99,937✔
577
   }
578
}
579

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

585
   // Mark
586
   for (void *p = arena->base; p != arena->alloc; ) {
2,228,679✔
587
      assert(p < arena->alloc);
2,211,605✔
588
      object_t *object = p;
2,211,605✔
589

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

592
      if (is_gc_root(class, object->kind))
2,211,605✔
593
         gc_mark_from_root(object, arena, generation);
22,121✔
594

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

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

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

606
   // Calculate forwarding addresses
607
   unsigned woffset = 0, live = 0, dead = 0;
17,074✔
608
   for (void *rptr = arena->base; rptr != arena->alloc; ) {
2,228,679✔
609
      assert(rptr < arena->alloc);
2,211,605✔
610
      object_t *object = rptr;
2,211,605✔
611

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

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

617
      ptrdiff_t index = (rptr - arena->base) >> OBJECT_ALIGN_BITS;
2,211,605✔
618
      if (!object_marked_p(object, generation)) {
2,211,605✔
619
         forward[index] = UINT32_MAX;
323,551✔
620
         gc_free_external(object);
323,551✔
621
         dead++;
323,551✔
622
      }
623
      else {
624
         forward[index] = woffset;
1,888,054✔
625
         woffset += size;
1,888,054✔
626
         live++;
1,888,054✔
627
      }
628

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

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

636
   arena->forward = forward;
17,074✔
637
   arena->live_bytes = woffset;
17,074✔
638

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

646
void object_visit(object_t *object, object_visit_ctx_t *ctx)
706,003✔
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)
706,003✔
652
      return;
653
   else if (object_marked_p(object, ctx->generation))
531,536✔
654
      return;
655

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

658
   const bool visit =
1,009,936✔
659
      (object->tag == ctx->tag && object->kind == ctx->kind)
504,968✔
660
      || ctx->kind == class->last_kind;
1,007,698✔
661

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

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

667
   imask_t has = class->has_map[object->kind];
504,968✔
668
   for (int n = 0; has; has &= has - 1, n++) {
2,655,932✔
669
      const uint64_t mask = has & -has;
2,150,964✔
670
      if (mask & deep_mask) {
2,150,964✔
671
         item_t *item = &(object->items[n]);
1,525,254✔
672
         if (ITEM_OBJECT & mask)
1,525,254✔
673
            object_visit(item->object, ctx);
353,655✔
674
         else if (ITEM_OBJ_ARRAY & mask) {
1,171,599✔
675
            if (item->obj_array != NULL) {
155,614✔
676
               for (unsigned j = 0; j < item->obj_array->count; j++)
437,842✔
677
                  object_visit(item->obj_array->items[j], ctx);
342,947✔
678
            }
679
         }
680
      }
681
   }
682

683
   if (visit) {
504,968✔
684
      if (ctx->postorder != NULL)
491,016✔
685
         (*ctx->postorder)(object, ctx->context);
491,016✔
686
      ctx->count++;
491,016✔
687
   }
688
}
689

690
static object_t *object_rewrite_iter(object_t *object,
3,632,956✔
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);
3,632,956✔
697
   if (new == object)
3,632,952✔
698
      return new;
699
   else
700
      return object_rewrite(new, ctx);
57,266✔
701
}
702

703
object_t *object_rewrite(object_t *object, object_rewrite_ctx_t *ctx)
8,336,100✔
704
{
705
   if (object == NULL)
8,336,100✔
706
      return NULL;
707

708
   if (!object_in_arena_p(ctx->arena, object))
6,953,939✔
709
      return object;
710

711
   const ptrdiff_t index =
4,641,296✔
712
      ((void *)object - ctx->arena->base) >> OBJECT_ALIGN_BITS;
4,641,296✔
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)) {
4,641,296✔
717
      ctx->cache_sz = (ctx->arena->alloc - ctx->arena->base) / OBJECT_ALIGN;
70,204✔
718
      ctx->cache = xrealloc_array(ctx->cache, sizeof(object_t *),
70,204✔
719
                                  ctx->cache_sz);
720
   }
721

722
   if (object_marked_p(object, ctx->generation)) {
4,641,296✔
723
      if (ctx->cache[index] == (object_t *)-1) {
780,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,155✔
727
            if (ctx->pre_fn[object->tag] != NULL)
50✔
UNCOV
728
               (*ctx->pre_fn[object->tag])(object, ctx->context);
×
729
            object_t *new = object_rewrite_iter(object, ctx);
50✔
730
            object_write_barrier(object, new);
50✔
731
            return (ctx->cache[index] = new);
50✔
732
         }
733
         else
734
            return (ctx->cache[index] = object);
1,105✔
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
3,860,506✔
743

744
   if (ctx->pre_fn[object->tag] != NULL)
3,860,506✔
UNCOV
745
      (*ctx->pre_fn[object->tag])(object, ctx->context);
×
746

747
   const imask_t skip_mask =
3,860,506✔
748
      I_REF | ITEM_INT64 | ITEM_INT32 | ITEM_DOUBLE | ITEM_NUMBER | ITEM_IDENT;
749

750
   const object_class_t *class = classes[object->tag];
3,860,506✔
751

752
   imask_t has = class->has_map[object->kind];
3,860,506✔
753
   for (int n = 0; has; has &= has - 1, n++) {
20,648,272✔
754
      const uint64_t mask = has & -has;
16,787,774✔
755
      if (mask & ~skip_mask) {
16,787,774✔
756
         if (ITEM_OBJECT & mask) {
7,047,458✔
757
            object_t *o = object_rewrite(object->items[n].object, ctx);
5,524,100✔
758
            object->items[n].object = o;
5,524,096✔
759
            object_write_barrier(object, o);
5,524,096✔
760
         }
761
         else if (ITEM_OBJ_ARRAY & mask) {
1,523,358✔
762
            obj_array_t **a = &(object->items[n].obj_array);
1,523,358✔
763
            if (object->items[n].obj_array != NULL) {
1,523,358✔
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++) {
3,716,325✔
768
                  object_t *o = object->items[n].obj_array->items[i];
2,723,471✔
769
                  if ((o = object_rewrite(o, ctx))) {
2,723,471✔
770
                     object_write_barrier(object, o);
2,719,015✔
771
                     object->items[n].obj_array->items[wptr++] = o;
2,719,015✔
772
                  }
773
               }
774

775
               if (wptr == 0)
992,854✔
776
                  obj_array_free(a);
847✔
777
               else
778
                  (*a)->count = wptr;
992,007✔
779
            }
780
         }
781
         else
782
            should_not_reach_here();
783
      }
784
   }
785

786
   if (ctx->cache[index] != (object_t *)-1) {
3,860,498✔
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) {
3,859,343✔
791
      object_t *new = object_rewrite_iter(object, ctx);
3,632,906✔
792
      object_write_barrier(object, new);
3,632,902✔
793
      return (ctx->cache[index] = new);
3,632,902✔
794
   }
795
   else
796
      return (ctx->cache[index] = object);
226,437✔
797
}
798

799
static void object_write_ref(object_t *object, fbuf_t *f)
4,473,845✔
800
{
801
   if (object == NULL)
4,473,845✔
802
      fbuf_put_uint(f, 0);
765,744✔
803
   else {
804
      object_arena_t *arena = __object_arena(object);
3,708,101✔
805
      assert(arena->key != 0);
3,708,101✔
806
      fbuf_put_uint(f, arena->key);
3,708,101✔
807

808
      ptrdiff_t index = ((void *)object - arena->base) >> OBJECT_ALIGN_BITS;
3,708,101✔
809
      if (arena->forward != NULL)
3,708,101✔
810
         fbuf_put_uint(f, arena->forward[index] >> OBJECT_ALIGN_BITS);
2,338,985✔
811
      else
812
         fbuf_put_uint(f, index);
1,369,116✔
813
   }
814
}
4,473,845✔
815

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

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

838
   arena_key_t max_key = arena->key;
11,574✔
839
   for (unsigned i = 0; i < arena->deps.count; i++)
34,519✔
840
      max_key = MAX(max_key, arena->deps.items[i]->key);
22,945✔
841
   fbuf_put_uint(f, max_key);
11,574✔
842

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

851
   for (void *p = arena->base, *next; p != arena->alloc; p = next) {
2,013,111✔
852
      assert(p < arena->alloc);
2,001,537✔
853

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

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

859
      ptrdiff_t index = (p - arena->base) >> OBJECT_ALIGN_BITS;
2,001,537✔
860
      if (arena->forward[index] == UINT32_MAX)
2,001,537✔
861
         continue;   // Dead object
274,224✔
862

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

866
      if (class->has_loc)
1,727,313✔
867
         loc_write(&object->loc, loc_ctx);
1,621,613✔
868

869
      imask_t has = class->has_map[object->kind];
1,727,313✔
870
      for (int n = 0; has; has &= has - 1, n++) {
9,184,535✔
871
         const uint64_t mask = has & -has;
7,457,222✔
872
         item_t *item = &(object->items[n]);
7,457,222✔
873
         if (ITEM_IDENT & mask)
7,457,222✔
874
            ident_write(item->ident, ident_ctx);
1,390,175✔
875
         else if (ITEM_OBJECT & mask)
6,067,047✔
876
            object_write_ref(item->object, f);
3,270,300✔
877
         else if (ITEM_OBJ_ARRAY & mask) {
2,796,747✔
878
            if (item->obj_array != NULL) {
663,288✔
879
               const unsigned count = item->obj_array->count;
431,043✔
880
               fbuf_put_uint(f, count);
431,043✔
881
               for (unsigned i = 0; i < count; i++)
1,634,588✔
882
                  object_write_ref(item->obj_array->items[i], f);
1,203,545✔
883
            }
884
            else
885
               fbuf_put_uint(f, 0);
232,245✔
886
         }
887
         else if (ITEM_INT64 & mask)
2,133,459✔
888
            fbuf_put_int(f, item->ival);
377,696✔
889
         else if (ITEM_INT32 & mask)
1,755,763✔
890
            fbuf_put_int(f, item->ival);
1,638,241✔
891
         else if (ITEM_DOUBLE & mask)
117,522✔
892
            write_double(item->dval, f);
111,075✔
893
         else if (ITEM_NUMBER & mask)
6,447✔
894
            number_write(item->number, f);
6,447✔
895
         else
896
            should_not_reach_here();
897
      }
898
   }
899

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

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

909
   arena_key_t mapped = key_map[key];
51,491,963✔
910
   ptrdiff_t offset = fbuf_get_uint(f) << OBJECT_ALIGN_BITS;
51,491,963✔
911

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

915
   assert(mapped < all_arenas.count);
51,491,963✔
916
   assert(mapped > 0);
51,491,963✔
917

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

921
   return (object_t *)((char *)arena->base + offset);
51,491,963✔
922
}
923

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

929
   const uint32_t ver = read_u32(f);
19,063✔
930
   if (ver != format_digest)
19,063✔
UNCOV
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);
19,063✔
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)
19,063✔
941
      set_default_standard(std);
481✔
942

943
   if (std > standard())
19,063✔
944
      fatal("%s: design unit was analysed using standard revision %s which "
6✔
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);
19,057✔
949
   if (size & OBJECT_PAGE_MASK)
19,057✔
UNCOV
950
      fatal("%s: arena size %x bad alignment", fbuf_file_name(f), size);
×
951

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

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

959
   arena_key_t max_key = fbuf_get_uint(f);
19,057✔
960

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

964
   const int ndeps = fbuf_get_uint(f);
19,057✔
965
   for (int i = 0; i < ndeps; i++) {
42,406✔
966
      arena_key_t dkey = fbuf_get_uint(f);
23,352✔
967
      vhdl_standard_t dstd = fbuf_get_uint(f);
23,352✔
968
      uint32_t checksum = fbuf_get_uint(f);
23,352✔
969
      ident_t dep = ident_read(ident_ctx);
23,352✔
970

971
      object_arena_t *a = NULL;
23,352✔
972
      for (unsigned j = 1; a == NULL && j < all_arenas.count; j++) {
110,593✔
973
         if (dep == object_arena_name(all_arenas.items[j]))
87,241✔
974
            a = all_arenas.items[j];
19,267✔
975
      }
976

977
      if (a == NULL) {
23,352✔
978
         object_t *droot = NULL;
4,085✔
979
         if (loader_fn) droot = (*loader_fn)(dep);
4,085✔
980

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

985
         a = __object_arena(droot);
4,085✔
986
      }
987

988
      if (a->std != dstd)
23,352✔
UNCOV
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) {
23,352✔
993
         diag_t *d = diag_new(DIAG_FATAL, NULL);
3✔
994
         diag_suppress(d, false);
3✔
995
         diag_printf(d, "%s: design unit depends on %s with checksum %08x "
3✔
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 "
3✔
999
                   "be reanalysed", istr(name));
1000
         diag_emit(d);
3✔
1001
         fatal_exit(EXIT_FAILURE);
3✔
1002
      }
1003

1004
      APUSH(arena->deps, a);
23,349✔
1005

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

1010
   for (;;) {
23,461,288✔
1011
      const uint64_t hdr = fbuf_get_uint(f);
23,461,288✔
1012
      if (hdr == UINT16_MAX) break;
23,461,288✔
1013

1014
      const unsigned tag = hdr & 3;
23,442,234✔
1015
      const unsigned kind = hdr >> 2;
23,442,234✔
1016

1017
      assert(tag < OBJECT_TAG_COUNT);
23,442,234✔
1018

1019
      const object_class_t *class = classes[tag];
23,442,234✔
1020

1021
      object_t *object = object_new(arena, class, kind);
23,442,234✔
1022

1023
      if (class->has_loc)
23,442,234✔
1024
         loc_read(&(object->loc), loc_ctx);
21,168,396✔
1025

1026
      imask_t has = class->has_map[object->kind];
23,442,234✔
1027
      for (int n = 0; has; has &= has - 1, n++) {
128,206,635✔
1028
         const uint64_t mask = has & -has;
104,764,401✔
1029
         item_t *item = &(object->items[n]);
104,764,401✔
1030
         if (ITEM_IDENT & mask)
104,764,401✔
1031
            item->ident = ident_read(ident_ctx);
21,248,638✔
1032
         else if (ITEM_OBJECT & mask)
83,515,763✔
1033
            item->object = object_read_ref(f, key_map);
42,114,066✔
1034
         else if (ITEM_OBJ_ARRAY & mask) {
41,401,697✔
1035
            const unsigned count = fbuf_get_uint(f);
9,765,480✔
1036
            if (count > 0) {
9,765,480✔
1037
               item->obj_array = xmalloc_flex(sizeof(obj_array_t),
6,524,155✔
1038
                                              count, sizeof(object_t *));
1039
               item->obj_array->count =
6,524,155✔
1040
                  item->obj_array->limit = count;
6,524,155✔
1041
               for (unsigned i = 0; i < count; i++) {
24,942,122✔
1042
                  object_t *o = object_read_ref(f, key_map);
18,417,967✔
1043
                  item->obj_array->items[i] = o;
18,417,967✔
1044
               }
1045
            }
1046
         }
1047
         else if ((ITEM_INT64 | ITEM_INT32) & mask)
31,636,217✔
1048
            item->ival = fbuf_get_int(f);
30,828,808✔
1049
         else if (ITEM_DOUBLE & mask)
807,409✔
1050
            item->dval = read_double(f);
807,409✔
UNCOV
1051
         else if (ITEM_NUMBER & mask)
×
UNCOV
1052
            item->number = number_read(f);
×
1053
         else
1054
            should_not_reach_here();
1055
      }
1056
   }
1057

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

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

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

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

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

1078
   bool marked = false;
1,595,802✔
1079
   if (object_marked_p(object, ctx->generation)) {
1,595,802✔
1080
      object_t *map = hash_get(ctx->copy_map, object);
382,213✔
1081
      if (map == object)
382,213✔
1082
         marked = true;   // Marked as root
1083
      else
1084
         return map != NULL;
368,387✔
1085
   }
1086

1087
   if (!marked && ctx->should_copy[object->tag] != NULL)
1,213,589✔
1088
      marked = (*ctx->should_copy[object->tag])(object, ctx->pred_context);
1,144,806✔
1089

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

1092
   object_t *copy = NULL;
1,227,415✔
1093
   if (marked) {
1,227,415✔
1094
      copy = object_new(global_arena, class, object->kind);
33,473✔
1095
      hash_put(ctx->copy_map, object, copy);
33,473✔
1096
   }
1097

1098
   imask_t has = class->has_map[object->kind];
1,227,415✔
1099
   for (int n = 0; has; has &= has - 1, n++) {
6,486,095✔
1100
      const uint64_t mask = has & -has;
5,258,680✔
1101
      item_t *item = &(object->items[n]);
5,258,680✔
1102
      if (ITEM_OBJECT & mask)
5,258,680✔
1103
         marked |= object_copy_mark(item->object, ctx);
2,325,954✔
1104
      else if (ITEM_OBJ_ARRAY & mask) {
2,932,726✔
1105
         if (item->obj_array != NULL) {
442,087✔
1106
            for (unsigned i = 0; i < item->obj_array->count; i++) {
1,107,639✔
1107
               object_t *o = item->obj_array->items[i];
825,779✔
1108
               marked |= object_copy_mark(o, ctx);
825,779✔
1109
            }
1110
         }
1111
      }
1112
   }
1113

1114
   if (marked && copy == NULL) {
1,227,415✔
1115
      copy = object_new(global_arena, class, object->kind);
229,856✔
1116
      hash_put(ctx->copy_map, object, copy);
229,856✔
1117
   }
1118

1119
   return marked;
1120
}
1121

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

1126
   arena->copygen = ctx->generation;
14,149✔
1127

1128
   if (!object_marked_p(object, ctx->generation))
14,149✔
1129
      hash_put(ctx->copy_map, object, object);
13,826✔
1130
}
14,149✔
1131

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

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

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

1145
   for (int i = 0; i < ctx->nroots; i++)
18,568✔
1146
      __object_arena(ctx->roots[i])->copygen = ctx->generation;
12,227✔
1147
}
6,341✔
1148

1149
void object_copy_finish(object_copy_ctx_t *ctx)
6,341✔
1150
{
1151
   for (int i = 0; i < ctx->nroots; i++)
18,568✔
1152
      (void)object_copy_mark(ctx->roots[i], ctx);
12,227✔
1153

1154
   unsigned ncopied = 0;
6,341✔
1155
   const void *key;
6,341✔
1156
   void *value;
6,341✔
1157
   for (hash_iter_t it = HASH_BEGIN;
6,341✔
1158
        hash_iter(ctx->copy_map, &it, &key, &value); ) {
269,670✔
1159
      const object_t *object = key;
263,329✔
1160
      object_t *copy = value;
263,329✔
1161
      assert(copy != object);
263,329✔
1162
      ncopied++;
263,329✔
1163

1164
      copy->loc = object->loc;
263,329✔
1165

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

1168
      imask_t has = class->has_map[object->kind];
263,329✔
1169
      for (int n = 0; has; has &= has - 1, n++) {
1,464,160✔
1170
         const uint64_t mask = has & -has;
1,200,831✔
1171
         const item_t *from = &(object->items[n]);
1,200,831✔
1172
         item_t *to = &(copy->items[n]);
1,200,831✔
1173

1174
         if (ITEM_IDENT & mask)
1,200,831✔
1175
            to->ident = from->ident;
218,898✔
1176
         else if (ITEM_OBJECT & mask) {
981,933✔
1177
            to->object = object_copy_map(from->object, ctx);
499,117✔
1178
            object_write_barrier(copy, to->object);
499,117✔
1179
         }
1180
         else if (ITEM_DOUBLE & mask)
482,816✔
1181
            to->dval = from->dval;
124✔
1182
         else if (ITEM_OBJ_ARRAY & mask) {
482,692✔
1183
            if (from->obj_array != NULL) {
173,609✔
1184
               // TODO: make a resize macro
1185
               to->obj_array = xmalloc_flex(sizeof(obj_array_t),
232,734✔
1186
                                            from->obj_array->count,
116,367✔
1187
                                            sizeof(object_t *));
1188
               to->obj_array->count =
116,367✔
1189
                  to->obj_array->limit = from->obj_array->count;
116,367✔
1190
               for (size_t i = 0; i < from->obj_array->count; i++) {
388,774✔
1191
                  object_t *o =
272,407✔
1192
                     object_copy_map(from->obj_array->items[i], ctx);
272,407✔
1193
                  to->obj_array->items[i] = o;
272,407✔
1194
                  object_write_barrier(copy, o);
272,407✔
1195
               }
1196
            }
1197
         }
1198
         else if ((ITEM_INT64 | ITEM_INT32) & mask)
309,083✔
1199
            to->ival = from->ival;
309,083✔
1200
         else
1201
            should_not_reach_here();
1202
      }
1203
   }
1204

1205
   for (hash_iter_t it = HASH_BEGIN;
6,341✔
1206
        hash_iter(ctx->copy_map, &it, &key, &value); ) {
269,670✔
1207
      object_t *copy = value;
263,329✔
1208
      if (ctx->callback[copy->tag] != NULL)
263,329✔
1209
         (*ctx->callback[copy->tag])(copy, ctx->callback_context);
260,140✔
1210
   }
1211

1212
   if (opt_get_verbose(OPT_OBJECT_VERBOSE, NULL))
6,341✔
UNCOV
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++) {
18,568✔
1217
      object_t *copy = hash_get(ctx->copy_map, ctx->roots[i]);
12,227✔
1218
      if (copy != NULL)
12,227✔
1219
         ctx->roots[i] = copy;
5,263✔
1220
   }
1221

1222
   hash_free(ctx->copy_map);
6,341✔
1223
}
6,341✔
1224

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

1230
object_arena_t *object_arena_new(size_t size, unsigned std)
36,773✔
1231
{
1232
   if (all_arenas.count == 0)
36,773✔
1233
      APUSH(all_arenas, NULL);   // Dummy null arena
5,383✔
1234

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

1243
   APUSH(all_arenas, arena);
36,773✔
1244

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

1248
   return arena;
36,773✔
1249
}
1250

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

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

1258
   if (arena->source == OBJ_FRESH)
36,128✔
1259
      object_arena_gc(arena);
17,074✔
1260

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

1265
   chash_put(arena_lookup, name, arena);
36,128✔
1266

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

1270
   if (next_page < arena->limit) {
36,128✔
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);
17,074✔
1277
      nvc_memprotect(next_page, arena->limit - next_page, MEM_NONE);
17,074✔
1278
#endif
1279
   }
1280

1281
   arena->frozen = true;
36,128✔
1282
}
36,128✔
1283

1284
void arena_walk_deps(object_arena_t *arena, arena_deps_fn_t fn, void *context)
32,893✔
1285
{
1286
   for (unsigned i = 0; i < arena->deps.count; i++)
90,456✔
1287
      (*fn)(arena_root(arena->deps.items[i]), context);
57,563✔
1288
}
32,893✔
1289

1290
void arena_walk_obsolete_deps(object_arena_t *arena, arena_deps_fn_t fn,
11,574✔
1291
                              void *context)
1292
{
1293
   for (unsigned i = 0; i < arena->deps.count; i++) {
34,519✔
1294
      if (arena->deps.items[i]->obsolete)
22,945✔
1295
         (*fn)(arena_root(arena->deps.items[i]), context);
6✔
1296
   }
1297
}
11,574✔
1298

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

1304
   *module = object_arena_name(arena);
12,402✔
1305

1306
   const ptrdiff_t index = ((void *)object - arena->base) >> OBJECT_ALIGN_BITS;
12,402✔
1307
   if (arena->forward != NULL)
12,402✔
1308
      *offset = arena->forward[index] >> OBJECT_ALIGN_BITS;
5,685✔
1309
   else
1310
      *offset = index;
6,717✔
1311
}
12,402✔
1312

1313
static object_arena_t *arena_by_name(ident_t module)
7,292✔
1314
{
1315
   if (global_arena != NULL && object_arena_name(global_arena) == module)
7,292✔
UNCOV
1316
      return global_arena;
×
1317

1318
   object_arena_t *a = chash_get(arena_lookup, module);
7,292✔
1319
   if (a != NULL)
7,292✔
1320
      return a;
1321

1322
#if defined DEBUG && !defined __SANITIZE_THREAD__
1323
   for (int i = 1; i < all_arenas.count; i++)
4,561✔
1324
      assert(module != object_arena_name(all_arenas.items[i]));
3,927✔
1325
#endif
1326

1327
   return NULL;
1328
}
1329

1330
object_t *object_from_locus(ident_t module, ptrdiff_t offset,
7,292✔
1331
                            object_load_fn_t loader)
1332
{
1333
   object_arena_t *arena = arena_by_name(module);
7,292✔
1334

1335
   if (arena == NULL) {
7,292✔
1336
      object_t *droot = NULL;
634✔
1337
      if (loader) droot = (*loader)(module);
634✔
1338

1339
      if (droot == NULL)
634✔
UNCOV
1340
         fatal("cannot find object locus %s%+"PRIiPTR, istr(module), offset);
×
1341

1342
      arena = __object_arena(droot);
634✔
1343
   }
1344

1345
   assert(arena->frozen);
7,292✔
1346

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

1361
   if (ptr > arena->limit)
7,292✔
1362
      fatal_trace("invalid object locus %s%+"PRIiPTR, istr(module), offset);
1363

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

1372
   return obj;
7,292✔
1373
}
1374

1375
void freeze_global_arena(void)
26,307✔
1376
{
1377
   object_one_time_init();
26,307✔
1378

1379
   if (global_arena != NULL) {
26,307✔
1380
      object_arena_freeze(global_arena);
17,074✔
1381
      global_arena = NULL;
17,074✔
1382
   }
1383
}
26,307✔
1384

1385
void make_new_arena(void)
17,716✔
1386
{
1387
   freeze_global_arena();
17,716✔
1388
   global_arena = object_arena_new(object_arena_default_size(), standard());
17,716✔
1389
}
17,716✔
1390

1391
void discard_global_arena(void)
1✔
1392
{
1393
   if (global_arena == NULL)
1✔
1394
      return;
1395

1396
   object_one_time_init();
1✔
1397

1398
   for (void *p = global_arena->base; p != global_arena->alloc; ) {
7✔
1399
      assert(p < global_arena->alloc);
6✔
1400
      object_t *object = p;
6✔
1401

1402
      gc_free_external(object);
6✔
1403

1404
      const object_class_t *class = classes[object->tag];
6✔
1405
      const size_t size =
6✔
1406
         ALIGN_UP(class->object_size[object->kind], OBJECT_ALIGN);
6✔
1407
      p = (char *)p + size;
6✔
1408
   }
1409

1410
   nvc_munmap(global_arena->base, global_arena->limit - global_arena->base);
1✔
1411

1412
   assert(all_arenas.items[all_arenas.count - 1] == global_arena);
1✔
1413
   APOP(all_arenas);
1✔
1414

1415
   free(global_arena);
1✔
1416
   global_arena = NULL;
1✔
1417
}
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