From 0db19dcfafe5b2b3cbebaec9098ec50b9d6b1102 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sat, 22 Mar 2025 16:55:57 +0200 Subject: use new hashmap --- deps/conts | 2 +- include/ek/scope.h | 24 ++++++++++++++++-------- scripts/makefile | 2 +- src/actualize.c | 6 +++--- src/compiler.c | 3 ++- src/scope.c | 22 +++++++++++----------- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/deps/conts b/deps/conts index 4f647dc..be71a36 160000 --- a/deps/conts +++ b/deps/conts @@ -1 +1 @@ -Subproject commit 4f647dc8520a9186b367400c5a337b681aec5565 +Subproject commit be71a36fd88941a5bc3f25d2e563c8aa0481327c diff --git a/include/ek/scope.h b/include/ek/scope.h index 8c56640..d09c086 100644 --- a/include/ek/scope.h +++ b/include/ek/scope.h @@ -11,6 +11,7 @@ */ #include +#include #include "ast.h" #include "debug.h" @@ -25,13 +26,10 @@ enum scope_flags { SCOPE_ROOT = (1 << 2) }; -struct visible_tuple { - struct ast *def; -}; - #define MAP_KEY char * #define MAP_TYPE struct ast * #define MAP_CMP(a, b) strcmp((a), (b)) +#define MAP_HASH(a) CONTS_MAP_STR_HASH(a) #define MAP_NAME visible #include @@ -50,16 +48,26 @@ static inline int expanded_key_cmp(struct expanded_key a, struct expanded_key b) return !type_lists_match(a.types, b.types); } +static inline size_t expanded_key_hash(struct expanded_key a) +{ + return (uintptr_t)a.def; +} + #define MAP_KEY struct expanded_key #define MAP_TYPE struct ast * #define MAP_CMP(a, b) expanded_key_cmp((a), (b)) +#define MAP_HASH(a) expanded_key_hash(a) #define MAP_NAME expanded #include -#define SPTREE_TYPE struct ast * -#define SPTREE_CMP(a, b) ((uintptr_t)(a) - (uintptr_t)(b)) -#define SPTREE_NAME exported -#include +/* use map like set, could maybe be added to conts directly with some + * convenience wrappers? */ +#define MAP_KEY struct ast * +#define MAP_TYPE struct ast * +#define MAP_CMP(a, b) ((uintptr_t)(a) - (uintptr_t)(b)) +#define MAP_HASH(a) ((uintptr_t)(a)) +#define MAP_NAME exported +#include /** * Scope. diff --git a/scripts/makefile b/scripts/makefile index 7d86089..27e5e22 100644 --- a/scripts/makefile +++ b/scripts/makefile @@ -42,7 +42,7 @@ COMPILER != [ -n "$(CROSS_COMPILE)" ] \ || echo $(CC) -OBFLAGS := -g -std=gnu23 +OBFLAGS := -g WARNFLAGS := -Wall -Wextra COMPILE_FLAGS := $(CFLAGS) $(WARNFLAGS) $(OPTFLAGS) $(OBFLAGS) $(ASSERTFLAGS) \ diff --git a/src/actualize.c b/src/actualize.c index a846d10..dae9cd9 100644 --- a/src/actualize.c +++ b/src/actualize.c @@ -390,7 +390,7 @@ static int copy_var(bool public, struct scope *to, struct ast *def) static int copy_symbols(bool public, struct scope *to, struct scope *from) { foreach(exported, n, &from->exported_symbols) { - struct ast *def = *n; + struct ast *def = n->data; switch (def->k) { case AST_PROC_DEF: if (copy_proc(public, to, def)) @@ -499,7 +499,7 @@ static int copy_chain(bool public, struct scope *to, struct ast *def) static int copy_types(bool public, struct scope *to, struct scope *from) { foreach(exported, n, &from->exported_types) { - struct ast *def = *n; + struct ast *def = n->data; switch (def->k) { case AST_STRUCT_DEF: if (copy_type(public, to, def, struct_id(def))) @@ -538,7 +538,7 @@ static int copy_types(bool public, struct scope *to, struct scope *from) static int copy_macros(bool public, struct scope *to, struct scope *from) { foreach(exported, n, &from->exported_macros) { - struct ast *def = *n; + struct ast *def = n->data; struct ast *exists = file_scope_find_macro(to, macro_def_id(def)); if (!exists) { diff --git a/src/compiler.c b/src/compiler.c index 6f24ee6..90f5fd0 100644 --- a/src/compiler.c +++ b/src/compiler.c @@ -112,6 +112,7 @@ static int process(struct scope *scope, const char *file) #define MAP_KEY char * #define MAP_TYPE struct scope * #define MAP_CMP(a, b) strcmp((a), (b)) +#define MAP_HASH(a) CONTS_MAP_STR_HASH(a) #define MAP_NAME scopes #include @@ -195,7 +196,7 @@ out: } int compile(const char *input) { - scopes = scopes_create(); + scopes = scopes_create(1); int ret = -1; struct scope *root = process_file(input); diff --git a/src/scope.c b/src/scope.c index 4c9097f..c838fe9 100644 --- a/src/scope.c +++ b/src/scope.c @@ -28,14 +28,14 @@ struct scope *create_scope() return NULL; } - scope->expanded = expanded_create(); - scope->symbols = visible_create(); - scope->macros = visible_create(); - scope->types = visible_create(); + scope->expanded = expanded_create(4); + scope->symbols = visible_create(4); + scope->macros = visible_create(4); + scope->types = visible_create(4); - scope->exported_symbols = exported_create(); - scope->exported_macros = exported_create(); - scope->exported_types = exported_create(); + scope->exported_symbols = exported_create(4); + scope->exported_macros = exported_create(4); + scope->exported_types = exported_create(4); scope->number = counter++; return scope; @@ -495,7 +495,7 @@ bool is_exported_macro(struct scope *scope, struct ast *def) int scope_add_exported_symbol(struct scope *scope, struct ast *def) { - struct ast **inserted = exported_insert(&scope->exported_symbols, def); + struct ast **inserted = exported_insert(&scope->exported_symbols, def, def); if (!inserted) { internal_error("failed inserting exported symbol"); return -1; @@ -507,7 +507,7 @@ int scope_add_exported_symbol(struct scope *scope, struct ast *def) int scope_add_exported_type(struct scope *scope, struct ast *def) { - struct ast **inserted = exported_insert(&scope->exported_types, def); + struct ast **inserted = exported_insert(&scope->exported_types, def, def); if (!inserted) { internal_error("failed inserting exported type"); return -1; @@ -533,7 +533,7 @@ int scope_add_exported_chain(struct scope *scope, struct ast *def) assert(exported_find(&scope->exported_types, def) == NULL); remove_exported_chain(scope, def->chain); - struct ast **inserted = exported_insert(&scope->exported_types, def); + struct ast **inserted = exported_insert(&scope->exported_types, def, def); if (!inserted) { internal_error("failed inserting exported chain"); return -1; @@ -545,7 +545,7 @@ int scope_add_exported_chain(struct scope *scope, struct ast *def) int scope_add_exported_macro(struct scope *scope, struct ast *def) { - struct ast **inserted = exported_insert(&scope->exported_macros, def); + struct ast **inserted = exported_insert(&scope->exported_macros, def, def); if (!inserted) { internal_error("failed inserting exported macro"); return -1; -- cgit v1.3