Commit 29b26ea8

29b26ea83ad5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

prepare: sym_int_hash type infra (Phase 2 Step 3a)

Add sp_SymIntHash struct + functions emitter (gated on @needs_sym_int_hash) and "sym_int_hash" type mapping in c_type / type_is_pointer / is_nullable_pointer_type. Not yet referenced by codegen — wiring follows in Step 3b/3c.

Keys are sp_sym (mrb_int) compared with int equality, no strcmp needed. Empty-slot sentinel is -1 (= invalid sp_sym).

67/67 tests pass, bootstrap stable.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
29b26ea83ad5
Parent
068be916339c

spinel_codegen.rbmodifiedView file

@@ -156,6 +156,7 @@
156156 @needs_str_array = 0
157157 @needs_str_int_hash = 0
158158 @needs_str_str_hash = 0
159+ @needs_sym_int_hash = 0
159160 @needs_string_helpers = 0
160161 @needs_setjmp = 0
161162 @needs_file_io = 0
@@ -2688,6 +2689,9 @@
26882689 if t == "str_str_hash"
26892690 return 1
26902691 end
2692+ if t == "sym_int_hash"
2693+ return 1
2694+ end
26912695 if t == "lambda"
26922696 return 1
26932697 end
@@ -2761,6 +2765,9 @@
27612765 if bt == "str_int_hash" || bt == "str_str_hash"
27622766 return 1
27632767 end
2768+ if bt == "sym_int_hash"
2769+ return 1
2770+ end
27642771 if bt == "stringio" || bt == "lambda" || bt == "poly_array"
27652772 return 1
27662773 end
@@ -2843,6 +2850,9 @@
28432850 if t == "str_str_hash"
28442851 return "sp_StrStrHash *"
28452852 end
2853+ if t == "sym_int_hash"
2854+ return "sp_SymIntHash *"
2855+ end
28462856 if is_tuple_type(t) == 1
28472857 return tuple_c_name(t) + " *"
28482858 end
@@ -7515,6 +7525,21 @@
75157525 emit_raw("")
75167526 end
75177527
7528+ # Symbol-keyed hash with integer values. Keys are sp_sym (mrb_int);
7529+ # the empty-slot sentinel is -1 (= invalid sp_sym, same as default).
7530+ def emit_sym_int_hash_runtime
7531+ emit_raw("typedef struct{sp_sym*keys;mrb_int*vals;sp_sym*order;mrb_int len;mrb_int cap;mrb_int mask;}sp_SymIntHash;")
7532+ emit_raw("static void sp_SymIntHash_fin(void*p){sp_SymIntHash*h=(sp_SymIntHash*)p;free(h->keys);free(h->vals);free(h->order);}")
7533+ emit_raw("static sp_SymIntHash*sp_SymIntHash_new(void){sp_SymIntHash*h=(sp_SymIntHash*)sp_gc_alloc(sizeof(sp_SymIntHash),sp_SymIntHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(sp_sym*)malloc(sizeof(sp_sym)*h->cap);for(mrb_int i=0;i<h->cap;i++)h->keys[i]=-1;h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(sp_sym*)malloc(sizeof(sp_sym)*h->cap);h->len=0;return h;}")
7534+ emit_raw("static void sp_SymIntHash_grow(sp_SymIntHash*h){mrb_int oc=h->cap;sp_sym*ok=h->keys;mrb_int*ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(sp_sym*)malloc(sizeof(sp_sym)*h->cap);for(mrb_int i=0;i<h->cap;i++)h->keys[i]=-1;h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(sp_sym*)realloc(h->order,sizeof(sp_sym)*h->cap);h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]>=0){mrb_int idx=(mrb_int)(((mrb_int)ok[i])&h->mask);while(h->keys[idx]>=0)idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}")
7535+ emit_raw("static mrb_int sp_SymIntHash_get(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k)return h->vals[idx];idx=(idx+1)&h->mask;}return 0;}")
7536+ emit_raw("static void sp_SymIntHash_set(sp_SymIntHash*h,sp_sym k,mrb_int v){if(h->len*2>=h->cap)sp_SymIntHash_grow(h);mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->keys[idx]=k;h->vals[idx]=v;h->order[h->len]=k;h->len++;}")
7537+ emit_raw("static mrb_bool sp_SymIntHash_has_key(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")
7538+ emit_raw("static mrb_int sp_SymIntHash_length(sp_SymIntHash*h){return h->len;}")
7539+ emit_raw("static void sp_SymIntHash_delete(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->keys[idx]=-1;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]>=0){mrb_int nj=(mrb_int)(((mrb_int)h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=-1;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}")
7540+ emit_raw("")
7541+ end
7542+
75187543 def emit_str_str_hash_runtime
75197544 emit_raw("typedef struct{const char**keys;const char**vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;}sp_StrStrHash;")
75207545 emit_raw("static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}")
@@ -7584,6 +7609,9 @@
75847609 i = i + 1
75857610 end
75867611 emit_raw("")
7612+ if @needs_sym_int_hash == 1
7613+ emit_sym_int_hash_runtime
7614+ end
75877615 end
75887616
75897617 # Index of symbol name in @sym_names, or -1 if not found.
@@ -8368,6 +8396,9 @@
83688396 if @needs_str_int_hash == 1 || @needs_str_str_hash == 1
83698397 @needs_gc = 1
83708398 end
8399+ if @needs_sym_int_hash == 1
8400+ @needs_gc = 1
8401+ end
83718402 if @needs_mutable_str == 1 || @needs_stringio == 1
83728403 @needs_gc = 1
83738404 end