kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

commit 9d905b3c414afa1ee34e1825d62000f9acfda660
parent 30367860c674c288dc894fe10c8595aa40519149
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Fri,  5 Jun 2026 10:06:36 -0700

modularity #9: move wasm_imports to src/obj, drop cg cross-boundary leak

src/cg/session.c (the frontend-agnostic KitCg entry) no longer #includes a
backend-private header (arch/wasm/wasm_imports.h) behind #if KIT_ARCH_WASM_ENABLED.
wasm_imports.{c,h} is pure obj-layer code (only touches the generic
obj_ext_set(OBJ_EXT_WASM_IMPORTS) side-table), so it moves to src/obj/ where it is
always compiled; session.c calls wasm_imports_set unconditionally. Behavior-identical.
Removes the only cross-boundary include and behavior-forking #if KIT_ARCH_* in src/cg.

Diffstat:
Msrc/arch/wasm/emit.c | 2+-
Dsrc/arch/wasm/wasm_imports.c | 103-------------------------------------------------------------------------------
Dsrc/arch/wasm/wasm_imports.h | 23-----------------------
Msrc/cg/session.c | 7+------
Asrc/obj/wasm_imports.c | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/obj/wasm_imports.h | 26++++++++++++++++++++++++++
6 files changed, 131 insertions(+), 133 deletions(-)

diff --git a/src/arch/wasm/emit.c b/src/arch/wasm/emit.c @@ -21,13 +21,13 @@ #include "abi/abi.h" #include "arch/wasm/internal.h" -#include "arch/wasm/wasm_imports.h" #include "cg/type.h" #include "core/arena.h" #include "core/buf.h" #include "core/heap.h" #include "core/pool.h" #include "obj/obj.h" +#include "obj/wasm_imports.h" /* Shared Wasm core: in-memory WasmModule, helpers (wasm_add_func, * wasm_intern_func_type, wasm_func_add_insn, ...), and wasm_encode for diff --git a/src/arch/wasm/wasm_imports.c b/src/arch/wasm/wasm_imports.c @@ -1,103 +0,0 @@ -/* Wasm import-attribute side table. Stored on the ObjBuilder under - * OBJ_EXT_WASM_IMPORTS so the C frontend (lang/c) can populate it from - * `__attribute__((import_module/import_name))` at decl_declare time before - * the wasm backend exists, and so the backend can read it back from the - * single shared ObjBuilder when promoting undefined function symbols to - * `(import ...)` declarations. */ - -#include "arch/wasm/wasm_imports.h" - -#include <string.h> - -#include "core/core.h" -#include "core/heap.h" - -typedef struct WasmImportEntry { - Sym name; - Sym import_module; - Sym import_name; -} WasmImportEntry; - -typedef struct WasmImportTable { - Heap* heap; - WasmImportEntry* entries; - u32 count; - u32 cap; -} WasmImportTable; - -static void wasm_imports_free(Compiler* c, void* p) { - (void)c; - WasmImportTable* t = (WasmImportTable*)p; - if (!t) return; - if (t->entries) - t->heap->free(t->heap, t->entries, sizeof(*t->entries) * t->cap); - t->heap->free(t->heap, t, sizeof(*t)); -} - -static WasmImportTable* table_ensure(ObjBuilder* o) { - WasmImportTable* t = (WasmImportTable*)obj_ext_get(o, OBJ_EXT_WASM_IMPORTS); - if (t) return t; - Compiler* c = obj_compiler(o); - Heap* h = c->ctx->heap; - t = (WasmImportTable*)h->alloc(h, sizeof(*t), _Alignof(WasmImportTable)); - if (!t) return NULL; - memset(t, 0, sizeof *t); - t->heap = h; - obj_ext_set(o, OBJ_EXT_WASM_IMPORTS, t, wasm_imports_free); - return t; -} - -static WasmImportEntry* table_find_mut(WasmImportTable* t, Sym name) { - for (u32 i = 0; i < t->count; ++i) { - if (t->entries[i].name == name) return &t->entries[i]; - } - return NULL; -} - -static const WasmImportEntry* table_find(const WasmImportTable* t, Sym name) { - for (u32 i = 0; i < t->count; ++i) { - if (t->entries[i].name == name) return &t->entries[i]; - } - return NULL; -} - -void wasm_imports_set(ObjBuilder* o, Sym name, Sym import_module, - Sym import_name) { - if (!o || name == 0) return; - if (import_module == 0 && import_name == 0) return; - WasmImportTable* t = table_ensure(o); - if (!t) return; - WasmImportEntry* e = table_find_mut(t, name); - if (e) { - if (import_module) e->import_module = import_module; - if (import_name) e->import_name = import_name; - return; - } - if (t->count == t->cap) { - u32 nc = t->cap ? t->cap * 2u : 8u; - void* p = - t->heap->realloc(t->heap, t->entries, sizeof(*t->entries) * t->cap, - sizeof(*t->entries) * nc, _Alignof(WasmImportEntry)); - if (!p) return; - t->entries = (WasmImportEntry*)p; - t->cap = nc; - } - t->entries[t->count].name = name; - t->entries[t->count].import_module = import_module; - t->entries[t->count].import_name = import_name; - t->count++; -} - -int wasm_imports_get(const ObjBuilder* o, Sym name, Sym* import_module, - Sym* import_name) { - if (import_module) *import_module = 0; - if (import_name) *import_name = 0; - if (!o || name == 0) return 0; - WasmImportTable* t = (WasmImportTable*)obj_ext_get(o, OBJ_EXT_WASM_IMPORTS); - if (!t) return 0; - const WasmImportEntry* e = table_find(t, name); - if (!e) return 0; - if (import_module) *import_module = e->import_module; - if (import_name) *import_name = e->import_name; - return 1; -} diff --git a/src/arch/wasm/wasm_imports.h b/src/arch/wasm/wasm_imports.h @@ -1,23 +0,0 @@ -#ifndef KIT_ARCH_WASM_IMPORTS_H -#define KIT_ARCH_WASM_IMPORTS_H - -/* Side table keyed by C-level symbol name carrying wasm import overrides - * (`__attribute__((import_module(...), import_name(...)))`). Lives on the - * ObjBuilder under OBJ_EXT_WASM_IMPORTS — populated by the C frontend at - * decl_declare time, consumed by the wasm backend when promoting an - * undefined function symbol into a wasm `(import ...)` declaration. - * - * Either Sym may be 0; the backend treats 0 as "use default" (module="env", - * name=<linkage spelling>). */ - -#include "obj/obj.h" - -void wasm_imports_set(ObjBuilder* o, Sym name, Sym import_module, - Sym import_name); - -/* Returns 1 and writes the import module/name out-params (each 0 when unset) - * when an entry exists; returns 0 otherwise. */ -int wasm_imports_get(const ObjBuilder* o, Sym name, Sym* import_module, - Sym* import_name); - -#endif diff --git a/src/cg/session.c b/src/cg/session.c @@ -6,11 +6,8 @@ #include "opt/opt.h" #endif -#if KIT_ARCH_WASM_ENABLED -#include "arch/wasm/wasm_imports.h" -#endif - #include "obj/symresolve.h" +#include "obj/wasm_imports.h" static void cg_free_obj_state(KitCg* g) { Heap* h; @@ -306,11 +303,9 @@ KitCgSym kit_cg_decl(KitCg* g, KitCgDecl decl) { * for other backends is one heap allocation that no one reads. */ if (decl.kind == KIT_CG_DECL_FUNC && (decl.as.func.wasm_import_module || decl.as.func.wasm_import_name)) { -#if KIT_ARCH_WASM_ENABLED wasm_imports_set(ob, (Sym)decl.linkage_name, (Sym)decl.as.func.wasm_import_module, (Sym)decl.as.func.wasm_import_name); -#endif } return (KitCgSym)sym; } diff --git a/src/obj/wasm_imports.c b/src/obj/wasm_imports.c @@ -0,0 +1,103 @@ +/* Wasm import-attribute side table. Stored on the ObjBuilder under + * OBJ_EXT_WASM_IMPORTS so the C frontend (lang/c) can populate it from + * `__attribute__((import_module/import_name))` at decl_declare time before + * the wasm backend exists, and so the backend can read it back from the + * single shared ObjBuilder when promoting undefined function symbols to + * `(import ...)` declarations. */ + +#include "obj/wasm_imports.h" + +#include <string.h> + +#include "core/core.h" +#include "core/heap.h" + +typedef struct WasmImportEntry { + Sym name; + Sym import_module; + Sym import_name; +} WasmImportEntry; + +typedef struct WasmImportTable { + Heap* heap; + WasmImportEntry* entries; + u32 count; + u32 cap; +} WasmImportTable; + +static void wasm_imports_free(Compiler* c, void* p) { + (void)c; + WasmImportTable* t = (WasmImportTable*)p; + if (!t) return; + if (t->entries) + t->heap->free(t->heap, t->entries, sizeof(*t->entries) * t->cap); + t->heap->free(t->heap, t, sizeof(*t)); +} + +static WasmImportTable* table_ensure(ObjBuilder* o) { + WasmImportTable* t = (WasmImportTable*)obj_ext_get(o, OBJ_EXT_WASM_IMPORTS); + if (t) return t; + Compiler* c = obj_compiler(o); + Heap* h = c->ctx->heap; + t = (WasmImportTable*)h->alloc(h, sizeof(*t), _Alignof(WasmImportTable)); + if (!t) return NULL; + memset(t, 0, sizeof *t); + t->heap = h; + obj_ext_set(o, OBJ_EXT_WASM_IMPORTS, t, wasm_imports_free); + return t; +} + +static WasmImportEntry* table_find_mut(WasmImportTable* t, Sym name) { + for (u32 i = 0; i < t->count; ++i) { + if (t->entries[i].name == name) return &t->entries[i]; + } + return NULL; +} + +static const WasmImportEntry* table_find(const WasmImportTable* t, Sym name) { + for (u32 i = 0; i < t->count; ++i) { + if (t->entries[i].name == name) return &t->entries[i]; + } + return NULL; +} + +void wasm_imports_set(ObjBuilder* o, Sym name, Sym import_module, + Sym import_name) { + if (!o || name == 0) return; + if (import_module == 0 && import_name == 0) return; + WasmImportTable* t = table_ensure(o); + if (!t) return; + WasmImportEntry* e = table_find_mut(t, name); + if (e) { + if (import_module) e->import_module = import_module; + if (import_name) e->import_name = import_name; + return; + } + if (t->count == t->cap) { + u32 nc = t->cap ? t->cap * 2u : 8u; + void* p = + t->heap->realloc(t->heap, t->entries, sizeof(*t->entries) * t->cap, + sizeof(*t->entries) * nc, _Alignof(WasmImportEntry)); + if (!p) return; + t->entries = (WasmImportEntry*)p; + t->cap = nc; + } + t->entries[t->count].name = name; + t->entries[t->count].import_module = import_module; + t->entries[t->count].import_name = import_name; + t->count++; +} + +int wasm_imports_get(const ObjBuilder* o, Sym name, Sym* import_module, + Sym* import_name) { + if (import_module) *import_module = 0; + if (import_name) *import_name = 0; + if (!o || name == 0) return 0; + WasmImportTable* t = (WasmImportTable*)obj_ext_get(o, OBJ_EXT_WASM_IMPORTS); + if (!t) return 0; + const WasmImportEntry* e = table_find(t, name); + if (!e) return 0; + if (import_module) *import_module = e->import_module; + if (import_name) *import_name = e->import_name; + return 1; +} diff --git a/src/obj/wasm_imports.h b/src/obj/wasm_imports.h @@ -0,0 +1,26 @@ +#ifndef KIT_OBJ_WASM_IMPORTS_H +#define KIT_OBJ_WASM_IMPORTS_H + +/* Side table keyed by C-level symbol name carrying wasm import overrides + * (`__attribute__((import_module(...), import_name(...)))`). Lives on the + * generic ObjBuilder under OBJ_EXT_WASM_IMPORTS — populated target-neutrally + * (the C frontend / CG session set it at decl time, before any backend + * exists), consumed by the wasm backend when promoting an undefined function + * symbol into a wasm `(import ...)` declaration. The setter is pure obj-layer + * code (it only touches the obj side-table), so it lives here under src/obj + * and is reachable without any dependency on the wasm backend. + * + * Either Sym may be 0; the backend treats 0 as "use default" (module="env", + * name=<linkage spelling>). */ + +#include "obj/obj.h" + +void wasm_imports_set(ObjBuilder* o, Sym name, Sym import_module, + Sym import_name); + +/* Returns 1 and writes the import module/name out-params (each 0 when unset) + * when an entry exists; returns 0 otherwise. */ +int wasm_imports_get(const ObjBuilder* o, Sym name, Sym* import_module, + Sym* import_name); + +#endif