commit f45825f5e6457c58c4e880ed4e53062ea0c2c248
parent 62c8e5a6f8b5e637fa1cd050b0bcacff7dccd7a1
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Mon, 15 Jun 2026 14:25:41 -0700
Fold CgApiType keys into CgType
Diffstat:
2 files changed, 32 insertions(+), 62 deletions(-)
diff --git a/src/cg/type.c b/src/cg/type.c
@@ -13,21 +13,8 @@ typedef struct CgApiType {
* never 0, and segvec slots are zero-initialized). */
KitCgTypeInfo info;
CgType cg;
- KitCgTypeId base;
- KitSym name;
- u32 count;
- u32 flags;
- u32 address_space;
- u64 array_count;
- const KitCgFieldDesc* fields;
- const KitCgEnumValue* values;
- const KitCgFuncParam* params;
- KitCgFuncResult result; /* void builtin means no value */
- KitCgCallConv call_conv;
- u8 abi_variadic;
/* Lazily-filled packed API_TYPE_CLASS_* memo (0 == not yet computed; the
- * VALID bit distinguishes a computed all-zero class from unfilled). Reuses
- * the former pad, so the entry size is unchanged. */
+ * VALID bit distinguishes a computed all-zero class from unfilled). */
u8 cached_class;
/* Lazily-filled abi_cg_type_info memo (see api_type_layout_get/put). */
u8 abi_cached;
@@ -543,8 +530,9 @@ static KitCgTypeId find_array_type_id(Compiler* c, KitCgTypeId elem,
if (!c || !c->cg_api) return KIT_CG_TYPE_NONE;
s = (CgApiState*)c->cg_api;
memset(&probe, 0, sizeof probe);
- probe.base = elem;
- probe.array_count = count;
+ probe.cg.kind = KIT_CG_TYPE_ARRAY;
+ probe.cg.array.elem = elem;
+ probe.cg.array.count = count;
hit = CgArraySet_find(&s->array_index, &probe);
return hit ? hit->self_id : KIT_CG_TYPE_NONE;
}
@@ -565,13 +553,14 @@ static int cg_result_eq(const KitCgFuncResult* a, const KitCgFuncResult* b) {
}
static u32 cg_array_hash(CgApiType* const e) {
- u32 h = hash_u32((u32)e->base);
- h ^= hash_u64(e->array_count) + 0x9e3779b9u + (h << 6) + (h >> 2);
+ u32 h = hash_u32((u32)e->cg.array.elem);
+ h ^= hash_u64(e->cg.array.count) + 0x9e3779b9u + (h << 6) + (h >> 2);
return h;
}
static int cg_array_eq(CgApiType* const a, CgApiType* const b) {
- return a->base == b->base && a->array_count == b->array_count;
+ return a->cg.array.elem == b->cg.array.elem &&
+ a->cg.array.count == b->cg.array.count;
}
/* Structural hash/eq over a function type's identity — the same fields the old
@@ -580,21 +569,24 @@ static int cg_array_eq(CgApiType* const a, CgApiType* const b) {
* fewer hash inputs just means eq resolves the (rare) collision. */
static u32 cg_func_hash(CgApiType* const e) {
u32 h = 2166136261u;
- h = (h ^ (u32)e->count) * 16777619u;
- h = (h ^ (u32)e->abi_variadic) * 16777619u;
- h = (h ^ (u32)e->call_conv) * 16777619u;
- h = (h ^ (u32)e->result.type) * 16777619u;
- for (u32 i = 0; i < e->count; ++i)
- h = (h ^ (u32)e->params[i].type) * 16777619u;
+ h = (h ^ (u32)e->cg.func.nparams) * 16777619u;
+ h = (h ^ (u32)e->cg.func.abi_variadic) * 16777619u;
+ h = (h ^ (u32)e->cg.func.call_conv) * 16777619u;
+ h = (h ^ (u32)e->cg.func.result.type) * 16777619u;
+ for (u32 i = 0; i < e->cg.func.nparams; ++i)
+ h = (h ^ (u32)e->cg.func.params[i].type) * 16777619u;
return h;
}
static int cg_func_eq(CgApiType* const a, CgApiType* const b) {
- if (a->count != b->count) return 0;
- if (a->abi_variadic != b->abi_variadic) return 0;
- if (a->call_conv != b->call_conv) return 0;
- if (!cg_result_eq(&a->result, &b->result)) return 0;
- if (a->count && !cg_params_eq(a->params, b->params, a->count)) return 0;
+ if (a->cg.func.nparams != b->cg.func.nparams) return 0;
+ if (a->cg.func.abi_variadic != b->cg.func.abi_variadic) return 0;
+ if (a->cg.func.call_conv != b->cg.func.call_conv) return 0;
+ if (!cg_result_eq(&a->cg.func.result, &b->cg.func.result)) return 0;
+ if (a->cg.func.nparams &&
+ !cg_params_eq(a->cg.func.params, b->cg.func.params, a->cg.func.nparams)) {
+ return 0;
+ }
return 1;
}
@@ -605,11 +597,12 @@ static KitCgTypeId find_func_type_id(Compiler* c, KitCgFuncSig sig) {
if (!c || !c->cg_api) return KIT_CG_TYPE_NONE;
s = (CgApiState*)c->cg_api;
memset(&probe, 0, sizeof probe);
- probe.count = sig.nparams;
- probe.abi_variadic = (u8)(sig.abi_variadic != 0);
- probe.call_conv = sig.call_conv;
- probe.result = sig.result;
- probe.params = sig.params;
+ probe.cg.kind = KIT_CG_TYPE_FUNC;
+ probe.cg.func.nparams = sig.nparams;
+ probe.cg.func.abi_variadic = sig.abi_variadic != 0;
+ probe.cg.func.call_conv = sig.call_conv;
+ probe.cg.func.result = sig.result;
+ probe.cg.func.params = sig.params;
hit = CgFuncSet_find(&s->func_index, &probe);
return hit ? hit->self_id : KIT_CG_TYPE_NONE;
}
@@ -948,8 +941,6 @@ KitCgTypeId kit_cg_type_ptr(KitCompiler* c, KitCgTypeId pointee,
if (id != KIT_CG_TYPE_NONE) return id;
e = type_alloc(c, &id);
if (!e) return KIT_CG_TYPE_NONE;
- e->base = pointee;
- e->address_space = address_space;
if (!cg_type_set_ptr(c, e, pointee, address_space)) {
return KIT_CG_TYPE_NONE;
}
@@ -967,8 +958,6 @@ KitCgTypeId kit_cg_type_array(KitCompiler* c, KitCgTypeId elem,
if (id != KIT_CG_TYPE_NONE) return id;
e = type_alloc(c, &id);
if (!e) return KIT_CG_TYPE_NONE;
- e->base = elem;
- e->array_count = count;
if (!cg_type_set_array(c, e, elem, count)) {
return KIT_CG_TYPE_NONE;
}
@@ -982,9 +971,6 @@ KitCgTypeId kit_cg_type_record_decl(KitCompiler* c, KitSym tag, int is_union) {
if (!c) return KIT_CG_TYPE_NONE;
e = type_alloc(c, &id);
if (!e) return KIT_CG_TYPE_NONE;
- e->name = tag;
- e->count = 0;
- e->fields = NULL;
memset(&e->cg, 0, sizeof(e->cg));
e->cg.kind = KIT_CG_TYPE_RECORD;
e->cg.record.tag = tag;
@@ -995,7 +981,7 @@ KitCgTypeId kit_cg_type_record_decl(KitCompiler* c, KitSym tag, int is_union) {
KitStatus kit_cg_type_record_complete(KitCompiler* c, KitCgTypeId record,
const KitCgRecordDesc* desc) {
CgApiType* e;
- KitCgFieldDesc* copied = NULL;
+ KitSym tag;
if (!c || !desc || (desc->nfields && !desc->fields) ||
desc->nfields > UINT16_MAX) {
return KIT_INVALID;
@@ -1005,21 +991,14 @@ KitStatus kit_cg_type_record_complete(KitCompiler* c, KitCgTypeId record,
if (e->cg.record.flags & CG_TYPE_RECORD_COMPLETE) return KIT_INVALID;
if (e->cg.record.is_union != (desc->is_union != 0)) return KIT_INVALID;
- if (desc->nfields) {
- copied = arena_array(&c->global->arena, KitCgFieldDesc, desc->nfields);
- if (!copied) return KIT_NOMEM;
- }
for (u32 i = 0; i < desc->nfields; ++i) {
KitCgTypeId fty = desc->fields[i].type;
if (!cg_type_valid_by_value(c, fty)) return KIT_INVALID;
if (cg_type_contains_by_value(c, fty, record)) return KIT_INVALID;
- copied[i] = desc->fields[i];
}
- e->name = desc->tag ? desc->tag : e->name;
- e->count = desc->nfields;
- e->fields = copied;
- if (!cg_type_set_record(c, e, e->name, desc->fields, desc->nfields,
+ tag = desc->tag ? desc->tag : e->cg.record.tag;
+ if (!cg_type_set_record(c, e, tag, desc->fields, desc->nfields,
desc->is_union, desc->align_override,
CG_TYPE_RECORD_COMPLETE)) {
return KIT_INVALID;
@@ -1042,10 +1021,6 @@ KitCgTypeId kit_cg_type_enum(KitCompiler* c, KitSym tag, KitCgTypeId base,
}
e = type_alloc(c, &id);
if (!e) return KIT_CG_TYPE_NONE;
- e->base = base;
- e->name = tag;
- e->count = nvalues;
- e->values = copied;
if (!cg_type_set_enum(c, e, tag, base, copied, nvalues)) {
return KIT_CG_TYPE_NONE;
}
@@ -1075,11 +1050,6 @@ KitCgTypeId kit_cg_type_func(KitCompiler* c, KitCgFuncSig sig) {
}
e = type_alloc(c, &id);
if (!e) return KIT_CG_TYPE_NONE;
- e->count = sig.nparams;
- e->params = copied;
- e->result = sig.result;
- e->call_conv = sig.call_conv;
- e->abi_variadic = sig.abi_variadic != 0;
if (!cg_type_set_func(c, e, sig, copied)) {
return KIT_CG_TYPE_NONE;
}
diff --git a/src/cg/type.h b/src/cg/type.h
@@ -40,7 +40,7 @@ typedef struct CgType {
} array;
struct {
KitCgFuncResult result; /* void builtin means no value */
- KitCgFuncParam* params;
+ const KitCgFuncParam* params;
u32 nparams;
KitCgCallConv call_conv;
int abi_variadic;