From 88f0059c7e8e7680e39611e865b0356bc510adc8 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Tue, 21 Jul 2026 19:40:45 -0400 Subject: pit: Hash table annotations, better dumper --- pit/src/runtime.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) (limited to 'pit/src/runtime.c') diff --git a/pit/src/runtime.c b/pit/src/runtime.c index f13adb3..1e4309e 100644 --- a/pit/src/runtime.c +++ b/pit/src/runtime.c @@ -22,6 +22,8 @@ u64 pit_value_data(pit_value v) { return v & 0x1ffffffffffff; } +static u64 ref_hash(pit_ref x) { return (u64) x; } +static bool ref_equal(pit_ref x, pit_ref y) { return x == y; } pit_runtime *pit_runtime_new(u8 *buf, i64 len) { pit_arena *a = pit_arena_new(buf, len, sizeof(u8)); pit_runtime *ret = pit_arena_alloc_back(a, sizeof(*ret)); @@ -29,10 +31,13 @@ pit_runtime *pit_runtime_new(u8 *buf, i64 len) { i64 annotations_size = len / 32; i64 symtab_size = len / 16; i64 stack_size = len / 32; + pit_hashtable_key_vtable(pit_ref) vt; + vt.hash = ref_hash; + vt.equal = ref_equal; ret->heap = pit_arena_new(pit_arena_alloc_back(a, heap_size), heap_size, sizeof(pit_value_heavy)); ret->backbuffer = pit_arena_new(pit_arena_alloc_back(a, heap_size), heap_size, sizeof(pit_value_heavy)); - ret->annotations = pit_vec_new(pit_annotated_ref)(pit_arena_alloc_back(a, annotations_size), annotations_size); - ret->backtrace = pit_vec_new(pit_annotated_ref)(pit_arena_alloc_back(a, annotations_size), annotations_size); + ret->annotations = pit_hashtable_new(pit_ref, pit_annotation)(pit_arena_alloc_back(a, annotations_size), annotations_size, vt); + ret->annotations_backbuffer = pit_hashtable_new(pit_ref, pit_annotation)(pit_arena_alloc_back(a, annotations_size), annotations_size, vt); ret->symtab = pit_vec_new(pit_symtab_entry)(pit_arena_alloc_back(a, symtab_size), symtab_size); ret->expr_stack = pit_vec_new(pit_value)(pit_arena_alloc_back(a, stack_size), stack_size); ret->result_stack = pit_vec_new(pit_value)(pit_arena_alloc_back(a, stack_size), stack_size); @@ -82,21 +87,11 @@ void pit_error(pit_runtime *rt, char *format, ...) { } void pit_annotation_set(struct pit_runtime *rt, pit_ref ref, pit_annotation annotation) { - pit_annotated_ref a; - a.ref = ref; - a.annotation = annotation; - if (pit_vec_push(pit_annotated_ref)(rt->annotations, a) < 0) + if (pit_hashtable_insert(pit_ref, pit_annotation)(rt->annotations, ref, annotation) < 0) pit_error(rt, "annotation overflow"); } -pit_annotated_ref *pit_annotation_get(struct pit_runtime *rt, pit_ref ref) { - for (i64 i = 0; i < rt->annotations->next; ++i) { - pit_annotated_ref *a = pit_vec_get(pit_annotated_ref)(rt->annotations, i); - if (a == NULL) pit_error(rt, "failed to get annotation"); - else if (a->ref == ref) { - return a; - } - } - return NULL; +pit_annotation *pit_annotation_get(struct pit_runtime *rt, pit_ref ref) { + return pit_hashtable_lookup(pit_ref, pit_annotation)(rt->annotations, ref); } void pit_traversal_push_value(struct pit_runtime *rt, pit_vec(pit_traversal_entry) *s, pit_value x) { @@ -113,7 +108,7 @@ void pit_traversal_push_dump_string(struct pit_runtime *rt, pit_vec(pit_traversa if (pit_vec_push(pit_traversal_entry)(s, ent) < 0) pit_error(rt, "traversal overflow"); } -void pit_traversal_push_application(struct pit_runtime *rt, pit_vec(pit_traversal_entry) *s, i64 arity, pit_annotated_ref *annotation) { +void pit_traversal_push_application(struct pit_runtime *rt, pit_vec(pit_traversal_entry) *s, i64 arity, pit_annotation *annotation) { pit_traversal_entry ent; ent.sort = PIT_TRAVERSAL_ENTRY_APPLICATION; ent.in.application.arity = arity; -- cgit v1.3.1