Skip to content

Commit

Permalink
Add TypeWriter stream to allow for in-place hashing and string gene…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
gingerBill committed Feb 18, 2025
1 parent 19b5946 commit 23efd1b
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 271 deletions.
56 changes: 1 addition & 55 deletions src/checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,61 +167,7 @@ typedef DECL_ATTRIBUTE_PROC(DeclAttributeProc);

gb_internal void check_decl_attributes(CheckerContext *c, Array<Ast *> const &attributes, DeclAttributeProc *proc, AttributeContext *ac);

struct TypeInfoPair {
Type *type;
u64 hash; // see: type_hash_canonical_type
};

struct TypeSet {
TypeInfoPair *keys;
usize count;
usize capacity;
};

static constexpr u64 TYPE_SET_TOMBSTONE = ~(u64)(0ull);

struct TypeSetIterator {
TypeSet *set;
usize index;

TypeSetIterator &operator++() noexcept {
for (;;) {
++index;
if (set->capacity == index) {
return *this;
}
TypeInfoPair key = set->keys[index];
if (key.hash != 0 && key.hash != TYPE_SET_TOMBSTONE) {
return *this;
}
}
}

bool operator==(TypeSetIterator const &other) const noexcept {
return this->set == other.set && this->index == other.index;
}


operator TypeInfoPair *() const {
return &set->keys[index];
}
};


gb_internal void type_set_init (TypeSet *s, isize capacity = 16);
gb_internal void type_set_destroy(TypeSet *s);
gb_internal Type *type_set_add (TypeSet *s, Type *ptr);
gb_internal Type *type_set_add (TypeSet *s, TypeInfoPair pair);
gb_internal bool type_set_update (TypeSet *s, Type *ptr); // returns true if it previously existed
gb_internal bool type_set_update (TypeSet *s, TypeInfoPair pair); // returns true if it previously existed
gb_internal bool type_set_exists (TypeSet *s, Type *ptr);
gb_internal void type_set_remove (TypeSet *s, Type *ptr);
gb_internal void type_set_clear (TypeSet *s);
gb_internal TypeInfoPair *type_set_retrieve(TypeSet *s, Type *ptr);

gb_internal TypeSetIterator begin(TypeSet &set) noexcept;
gb_internal TypeSetIterator end(TypeSet &set) noexcept;

#include "name_canonicalization.hpp"

enum ProcCheckedState : u8 {
ProcCheckedState_Unchecked,
Expand Down
4 changes: 2 additions & 2 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ gb_internal u32 fnv32a(void const *data, isize len) {
return h;
}

gb_internal u64 fnv64a(void const *data, isize len) {
gb_internal u64 fnv64a(void const *data, isize len, u64 seed=0xcbf29ce484222325ull) {
u8 const *bytes = cast(u8 const *)data;
u64 h = 0xcbf29ce484222325ull;
u64 h = seed;

for (; len >= 8; len -= 8, bytes += 8) {
h = (h ^ bytes[0]) * 0x100000001b3ull;
Expand Down
4 changes: 2 additions & 2 deletions src/llvm_backend_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1456,11 +1456,11 @@ gb_internal String lb_get_entity_name(lbModule *m, Entity *e) {
return e->token.string;
}

gbString w = gb_string_make(heap_allocator(), "");
w = write_canonical_entity_name(w, e);
gbString w = string_canonical_entity_name(heap_allocator(), e);
defer (gb_string_free(w));

String name = copy_string(permanent_allocator(), make_string(cast(u8 const *)w, gb_string_length(w)));
gb_printf_err("%.*s\n", LIT(name));

if (e->kind == Entity_TypeName) {
e->TypeName.ir_mangled_name = name;
Expand Down
Loading

0 comments on commit 23efd1b

Please sign in to comment.