Commit dae58287

dae582872888Merged

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

Description

Merge pull request #110 from rramsden/cleanup-dead-string-helpers

Cleanup up dead code

Details

Commit
dae582872888
Parent
e68173cb9f8a
Merge parent
8706c94b4d9c

spinel_codegen.rbmodifiedView file

@@ -168,7 +168,6 @@
168168 @needs_system = 0
169169 @needs_int_array = 0
170170 @needs_float_array = 0
171- @needs_ptr_array = 0
172171 @tuple_types = "".split(",")
173172 @needs_str_array = 0
174173 @needs_str_int_hash = 0
@@ -176,12 +175,8 @@
176175 @needs_int_str_hash = 0
177176 @needs_sym_int_hash = 0
178177 @needs_sym_str_hash = 0
179- @needs_str_poly_hash = 0
180- @needs_sym_poly_hash = 0
181178 @needs_sym_intern = 0
182- @needs_string_helpers = 0
183179 @needs_setjmp = 0
184- @needs_file_io = 0
185180 @needs_mutable_str = 0
186181 @needs_rb_value = 0
187182 @needs_regexp = 0
@@ -205,7 +200,6 @@
205200 @parse_id_pool = [[0]]
206201
207202 @needs_stringio = 0
208- @needs_proc = 0
209203 @proc_counter = 0
210204 @proc_funcs = ""
211205
@@ -1425,7 +1419,6 @@
14251419 k = k + 1
14261420 end
14271421 if all_same == 1
1428- @needs_ptr_array = 1
14291422 @needs_gc = 1
14301423 return et + "_ptr_array"
14311424 end
@@ -1442,7 +1435,6 @@
14421435 k = k + 1
14431436 end
14441437 if all_same == 1
1445- @needs_ptr_array = 1
14461438 @needs_gc = 1
14471439 return et + "_ptr_array"
14481440 end
@@ -2719,7 +2711,6 @@
27192711 return "poly_array"
27202712 end
27212713 if type_is_pointer(vt) == 1
2722- @needs_ptr_array = 1
27232714 @needs_gc = 1
27242715 return vt + "_ptr_array"
27252716 end
@@ -4651,7 +4642,6 @@
46514642 return "poly_array"
46524643 end
46534644 if type_is_pointer(vt) == 1
4654- @needs_ptr_array = 1
46554645 @needs_gc = 1
46564646 return vt + "_ptr_array"
46574647 end
@@ -7093,10 +7083,6 @@
70937083 @needs_setjmp = 1
70947084 end
70957085 end
7096- if t == "InterpolatedXStringNode"
7097- @needs_file_io = 1
7098- @needs_string_helpers = 1
7099- end
71007086 if t == "RegularExpressionNode"
71017087 @needs_regexp = 1
71027088 # Collect pattern and flags
@@ -7176,9 +7162,6 @@
71767162 @local_regex_idx.push(this_idx)
71777163 end
71787164 end
7179- if t == "XStringNode"
7180- @needs_file_io = 1
7181- end
71827165 if t == "ArrayNode"
71837166 et = infer_array_elem_type(nid)
71847167 if et == "str_array"
@@ -7213,12 +7196,7 @@
72137196 @needs_gc = 1
72147197 @needs_str_array = 1
72157198 end
7216- if t == "InterpolatedStringNode"
7217- @needs_string_helpers = 1
7218- end
7219- if t == "SymbolNode"
7220- @needs_string_helpers = 1
7221- end
7199+
72227200 if t == "GlobalVariableWriteNode"
72237201 gname = @nd_name[nid]
72247202 if gname != "$stderr" && gname != "$stdout" && gname != "$?"
@@ -7259,14 +7237,12 @@
72597237 mname == "gsub" || mname == "index" || mname == "sub" || mname == "tr" ||
72607238 mname == "ljust" || mname == "rjust" || mname == "capitalize" ||
72617239 mname == "count" || mname == "<<"
7262- @needs_string_helpers = 1
72637240 end
72647241 if mname == "rand" || mname == "srand" || mname == "sample" ||
72657242 mname == "shuffle" || mname == "shuffle!"
72667243 @needs_rand = 1
72677244 end
72687245 if mname == "split"
7269- @needs_string_helpers = 1
72707246 @needs_str_array = 1
72717247 @needs_gc = 1
72727248 end
@@ -7283,7 +7259,6 @@
72837259 if @nd_receiver[nid] >= 0
72847260 rt = infer_type(@nd_receiver[nid])
72857261 if rt == "string"
7286- @needs_string_helpers = 1
72877262 # Long string concat chains emit SP_GC_ROOT temps, so the
72887263 # enclosing function needs SP_GC_SAVE() in its header.
72897264 if mname == "+"
@@ -7292,9 +7267,7 @@
72927267 end
72937268 end
72947269 end
7295- if mname == "[]"
7296- @needs_string_helpers = 1
7297- end
7270+
72987271 if mname == "new"
72997272 if @nd_receiver[nid] >= 0
73007273 if @nd_type[@nd_receiver[nid]] == "ConstantReadNode"
@@ -7324,18 +7297,12 @@
73247297 if rn == "Hash"
73257298 @needs_str_int_hash = 1
73267299 end
7327- if rn == "Proc"
7328- @needs_proc = 1
7329- end
73307300 if rn == "StringIO"
73317301 @needs_stringio = 1
73327302 end
73337303 end
73347304 end
73357305 end
7336- if mname == "proc"
7337- @needs_proc = 1
7338- end
73397306 if mname == "to_a"
73407307 if @nd_receiver[nid] >= 0
73417308 rt = infer_type(@nd_receiver[nid])
@@ -7398,23 +7365,8 @@
73987365 @needs_setjmp = 1
73997366 end
74007367 if mname == "system"
7401- @needs_file_io = 1
74027368 @needs_system = 1
74037369 end
7404- if @nd_receiver[nid] >= 0
7405- if @nd_type[@nd_receiver[nid]] == "ConstantReadNode"
7406- rn = @nd_name[@nd_receiver[nid]]
7407- if rn == "File"
7408- @needs_file_io = 1
7409- if mname == "join"
7410- @needs_string_helpers = 1
7411- end
7412- if mname == "basename"
7413- @needs_string_helpers = 1
7414- end
7415- end
7416- end
7417- end
74187370 if mname == "keys"
74197371 @needs_str_array = 1
74207372 @needs_gc = 1
@@ -7446,9 +7398,6 @@
74467398 end
74477399 end
74487400 end
7449- if t == "BlockParameterNode"
7450- @needs_proc = 1
7451- end
74527401 # Recurse
74537402 scan_features_children(nid)
74547403 end
@@ -8739,132 +8688,6 @@
87398688 changed
87408689 end
87418690
8742- def emit_gc_runtime
8743- emit_raw("typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); size_t size; unsigned marked : 1; } sp_gc_hdr;")
8744- emit_raw("static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 256*1024;")
8745- emit_raw("#define SP_GC_STACK_MAX 65536")
8746- emit_raw("static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;")
8747- emit_raw("#define SP_GC_SAVE() int __attribute__((cleanup(sp_gc_cleanup))) _gc_saved = sp_gc_nroots")
8748- emit_raw("#define SP_GC_ROOT(v) do{if(sp_gc_nroots<SP_GC_STACK_MAX)sp_gc_roots[sp_gc_nroots++]=(void**)&(v);}while(0)")
8749- emit_raw("#define SP_GC_RESTORE() sp_gc_nroots = _gc_saved")
8750- # stack_bottom removed (no conservative scan)
8751- emit_raw("#define SP_GC_MARK_STACK_MAX (1024*64)")
8752- emit_raw("static void**sp_gc_mark_stack=NULL;static int sp_gc_mark_top=0;")
8753- emit_raw("static void sp_gc_mark(void*obj){if(!obj)return;sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->marked)return;h->marked=1;if(h->scan){if(sp_gc_mark_stack&&sp_gc_mark_top<SP_GC_MARK_STACK_MAX){sp_gc_mark_stack[sp_gc_mark_top++]=obj;}else{h->scan(obj);}}}")
8754- emit_raw("static void sp_gc_mark_all(void){if(!sp_gc_mark_stack)sp_gc_mark_stack=(void**)malloc(sizeof(void*)*SP_GC_MARK_STACK_MAX);sp_gc_mark_top=0;for(int i=0;i<sp_gc_nroots;i++){void*obj=*sp_gc_roots[i];if(obj)sp_gc_mark(obj);}while(sp_gc_mark_top>0){void*obj=sp_gc_mark_stack[--sp_gc_mark_top];sp_gc_hdr*h=(sp_gc_hdr*)((char*)obj-sizeof(sp_gc_hdr));if(h->scan)h->scan(obj);}}")
8755- emit_raw("static void sp_gc_cleanup(int*p){sp_gc_nroots=*p;}")
8756- # Size-segregated free lists (no arena overhead during sweep)
8757- emit_raw("#define SP_GC_NBUCKETS 32")
8758- emit_raw("static sp_gc_hdr*sp_gc_buckets[SP_GC_NBUCKETS];")
8759- emit_raw("static inline int sp_gc_bucket(size_t sz){int b=(int)(sz/16);return b<SP_GC_NBUCKETS?b:SP_GC_NBUCKETS-1;}")
8760- emit_raw("static int sp_gc_cycle=0;")
8761- emit_raw("static sp_gc_hdr*sp_gc_old_heap=NULL;static size_t sp_gc_old_bytes=0;")
8762- emit_raw("#define SP_GC_FULL_INTERVAL 8")
8763- emit_raw("static void sp_gc_collect(void){int full=(sp_gc_cycle%SP_GC_FULL_INTERVAL==0);sp_gc_cycle++;sp_gc_hdr*hh=sp_gc_old_heap;while(hh){hh->marked=0;hh=hh->next;}sp_gc_mark_all();if(full){sp_gc_hdr**pp=&sp_gc_old_heap;sp_gc_old_bytes=0;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));int b=sp_gc_bucket(h->size);h->next=sp_gc_buckets[b];sp_gc_buckets[b]=h;}else{h->marked=1;sp_gc_old_bytes+=h->size;pp=&h->next;}}}else{hh=sp_gc_old_heap;while(hh){hh->marked=1;hh=hh->next;}}sp_gc_hdr**pp=&sp_gc_heap;sp_gc_bytes=sp_gc_old_bytes;while(*pp){sp_gc_hdr*h=*pp;if(!h->marked){*pp=h->next;if(h->finalize)h->finalize((char*)h+sizeof(sp_gc_hdr));int b=sp_gc_bucket(h->size);h->next=sp_gc_buckets[b];sp_gc_buckets[b]=h;}else{h->marked=1;*pp=h->next;h->next=sp_gc_old_heap;sp_gc_old_heap=h;sp_gc_old_bytes+=h->size;sp_gc_bytes+=h->size;}}}")
8764- emit_raw("static size_t sp_gc_threshold_init=256*1024;")
8765- gc_linkage = @needs_bigint == 1 ? "" : "static "
8766- emit_raw(gc_linkage + "void*sp_gc_alloc(size_t sz,void(*fin)(void*),void(*scn)(void*)){if(sp_gc_bytes>sp_gc_threshold){size_t before=sp_gc_bytes;sp_gc_collect();size_t freed=before-sp_gc_bytes;if(freed<before/4){sp_gc_threshold=before*2;}else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else{sp_gc_threshold=sp_gc_threshold_init;}}size_t need=sizeof(sp_gc_hdr)+sz;int b=sp_gc_bucket(need);sp_gc_hdr*h=NULL;if(sp_gc_buckets[b]&&sp_gc_buckets[b]->size==need){h=sp_gc_buckets[b];sp_gc_buckets[b]=h->next;}if(!h){h=(sp_gc_hdr*)calloc(1,need);}h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}")
8767- if @needs_bigint == 1
8768- emit_raw("void*sp_gc_alloc_nogc(size_t sz,void(*fin)(void*),void(*scn)(void*)){size_t need=sizeof(sp_gc_hdr)+sz;sp_gc_hdr*h=(sp_gc_hdr*)calloc(1,need);h->finalize=fin;h->scan=scn;h->size=need;h->marked=0;h->next=sp_gc_heap;sp_gc_heap=h;sp_gc_bytes+=need;return(char*)h+sizeof(sp_gc_hdr);}")
8769- end
8770- emit_raw("")
8771- end
8772-
8773- def emit_int_array_runtime
8774- emit_raw("typedef struct{mrb_int*data;mrb_int start;mrb_int len;mrb_int cap;}sp_IntArray;")
8775- emit_raw("static void sp_IntArray_fin(void*p){free(((sp_IntArray*)p)->data);}")
8776- emit_raw("static sp_IntArray*sp_IntArray_new(void){sp_IntArray*a=(sp_IntArray*)sp_gc_alloc(sizeof(sp_IntArray),sp_IntArray_fin,NULL);a->cap=16;a->data=(mrb_int*)malloc(sizeof(mrb_int)*a->cap);a->start=0;a->len=0;{sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}return a;}")
8777- emit_raw("static sp_IntArray*sp_IntArray_from_range(mrb_int s,mrb_int e){sp_IntArray*a=sp_IntArray_new();mrb_int n=e-s+1;if(n<0)n=0;if(n>a->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)a-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*a->cap;h->size-=sizeof(mrb_int)*a->cap;a->cap=n;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);h->size+=sizeof(mrb_int)*a->cap;sp_gc_bytes+=sizeof(mrb_int)*a->cap;}for(mrb_int i=0;i<n;i++)a->data[i]=s+i;a->len=n;return a;}")
8778- emit_raw("static sp_IntArray*sp_IntArray_dup(sp_IntArray*a){sp_IntArray*b=sp_IntArray_new();if(a->len>b->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)b-sizeof(sp_gc_hdr));sp_gc_bytes-=sizeof(mrb_int)*b->cap;h->size-=sizeof(mrb_int)*b->cap;b->cap=a->len;b->data=(mrb_int*)realloc(b->data,sizeof(mrb_int)*b->cap);h->size+=sizeof(mrb_int)*b->cap;sp_gc_bytes+=sizeof(mrb_int)*b->cap;}memcpy(b->data,a->data+a->start,sizeof(mrb_int)*a->len);b->len=a->len;return b;}")
8779- emit_raw("static void __attribute__((noinline)) sp_IntArray_push_grow(sp_IntArray*a){if(a->start>0){memmove(a->data,a->data+a->start,sizeof(mrb_int)*a->len);a->start=0;if(a->len<a->cap)return;}a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);}")
8780- emit_raw("static inline void sp_IntArray_push(sp_IntArray*a,mrb_int v){if(a->start+a->len>=a->cap)sp_IntArray_push_grow(a);a->data[a->start+a->len]=v;a->len++;}")
8781- emit_raw("static inline mrb_int sp_IntArray_pop(sp_IntArray*a){return a->data[a->start+--a->len];}")
8782- emit_raw("static inline mrb_int sp_IntArray_shift(sp_IntArray*a){mrb_int v=a->data[a->start];a->start++;a->len--;return v;}")
8783- emit_raw("static inline mrb_int sp_IntArray_length(sp_IntArray*a){return a->len;}")
8784- emit_raw("static inline mrb_bool sp_IntArray_empty(sp_IntArray*a){return a->len==0;}")
8785- emit_raw("static inline mrb_int sp_IntArray_get(sp_IntArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[a->start+i];}")
8786- emit_raw("static void sp_IntArray_set_slow(sp_IntArray*a,mrb_int i,mrb_int v){while(a->start+i>=a->cap){a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);}while(i>=a->len){a->data[a->start+a->len]=0;a->len++;}a->data[a->start+i]=v;}")
8787- emit_raw("static inline void sp_IntArray_set(sp_IntArray*a,mrb_int i,mrb_int v){if(i<0)i+=a->len;if(i>=0&&i<a->len){a->data[a->start+i]=v;return;}sp_IntArray_set_slow(a,i,v);}")
8788- emit_raw("static void sp_IntArray_reverse_bang(sp_IntArray*a){for(mrb_int i=0,j=a->len-1;i<j;i++,j--){mrb_int t=a->data[a->start+i];a->data[a->start+i]=a->data[a->start+j];a->data[a->start+j]=t;}}")
8789- emit_raw("static int _sp_int_cmp(const void*a,const void*b){mrb_int va=*(const mrb_int*)a,vb=*(const mrb_int*)b;return(va>vb)-(va<vb);}")
8790- emit_raw("static sp_IntArray*sp_IntArray_sort(sp_IntArray*a){sp_IntArray*b=sp_IntArray_dup(a);qsort(b->data+b->start,b->len,sizeof(mrb_int),_sp_int_cmp);return b;}")
8791- emit_raw("static void sp_IntArray_sort_bang(sp_IntArray*a){qsort(a->data+a->start,a->len,sizeof(mrb_int),_sp_int_cmp);}")
8792- emit_raw("static mrb_int sp_IntArray_min(sp_IntArray*a){mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]<m)m=a->data[a->start+i];return m;}")
8793- emit_raw("static mrb_int sp_IntArray_max(sp_IntArray*a){mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]>m)m=a->data[a->start+i];return m;}")
8794- emit_raw("static mrb_int sp_IntArray_sum(sp_IntArray*a){mrb_int s=0;for(mrb_int i=0;i<a->len;i++)s+=a->data[a->start+i];return s;}")
8795- emit_raw("static mrb_bool sp_IntArray_include(sp_IntArray*a,mrb_int v){for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]==v)return TRUE;return FALSE;}")
8796- emit_raw("static sp_IntArray*sp_IntArray_uniq(sp_IntArray*a){sp_IntArray*b=sp_IntArray_new();for(mrb_int i=0;i<a->len;i++){int found=0;for(mrb_int j=0;j<b->len;j++){if(b->data[b->start+j]==a->data[a->start+i]){found=1;break;}}if(!found)sp_IntArray_push(b,a->data[a->start+i]);}return b;}")
8797- emit_raw("static void sp_IntArray_unshift(sp_IntArray*a,mrb_int v){if(a->start>0){a->start--;a->data[a->start]=v;a->len++;}else{mrb_int e=a->len+1;if(e>a->cap){a->cap=a->cap*2+1;a->data=(mrb_int*)realloc(a->data,sizeof(mrb_int)*a->cap);}memmove(a->data+1,a->data,sizeof(mrb_int)*a->len);a->data[0]=v;a->len++;}}")
8798- emit_raw("static const char*sp_IntArray_join(sp_IntArray*a,const char*sep){size_t sl=strlen(sep),cap=256;char*buf=(char*)malloc(cap);size_t len=0;for(mrb_int i=0;i<a->len;i++){if(i>0){if(len+sl>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,sep,sl);len+=sl;}char tmp[32];int n=snprintf(tmp,32,\"%lld\",(long long)a->data[a->start+i]);if(len+n>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,tmp,n);len+=n;}buf[len]=0;return buf;}")
8799- emit_raw("static mrb_bool sp_IntArray_eq(sp_IntArray*a,sp_IntArray*b){if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]!=b->data[b->start+i])return FALSE;return TRUE;}")
8800- emit_raw("")
8801- end
8802-
8803- def emit_ptr_array_runtime
8804- emit_raw("/* ---- PtrArray: array of void* pointers ---- */")
8805- emit_raw("typedef struct{void**data;mrb_int len;mrb_int cap;void(*scan_elem)(void*);}sp_PtrArray;")
8806- emit_raw("static void sp_PtrArray_fin(void*p){free(((sp_PtrArray*)p)->data);}")
8807- emit_raw("static void sp_PtrArray_gc_scan(void*p){sp_PtrArray*a=(sp_PtrArray*)p;if(!a->scan_elem)return;for(mrb_int i=0;i<a->len;i++){if(a->data[i])a->scan_elem(a->data[i]);}}")
8808- emit_raw("static sp_PtrArray*sp_PtrArray_new_scan(void(*scan_elem)(void*)){sp_PtrArray*a=(sp_PtrArray*)sp_gc_alloc(sizeof(sp_PtrArray),sp_PtrArray_fin,scan_elem?sp_PtrArray_gc_scan:NULL);a->cap=16;a->data=(void**)malloc(sizeof(void*)*a->cap);a->len=0;a->scan_elem=scan_elem;return a;}")
8809- emit_raw("static sp_PtrArray*sp_PtrArray_new(void){return sp_PtrArray_new_scan(sp_gc_mark);}")
8810- emit_raw("static inline void sp_PtrArray_push(sp_PtrArray*a,void*v){if(a->len>=a->cap){a->cap=a->cap*2+1;a->data=(void**)realloc(a->data,sizeof(void*)*a->cap);}a->data[a->len++]=v;}")
8811- emit_raw("static inline void*sp_PtrArray_get(sp_PtrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}")
8812- emit_raw("static inline void sp_PtrArray_set(sp_PtrArray*a,mrb_int i,void*v){if(i<0)i+=a->len;a->data[i]=v;}")
8813- emit_raw("static inline mrb_int sp_PtrArray_length(sp_PtrArray*a){return a->len;}")
8814- emit_raw("static inline mrb_bool sp_PtrArray_empty(sp_PtrArray*a){return a->len==0;}")
8815- emit_raw("static sp_PtrArray*sp_PtrArray_dup(sp_PtrArray*a){sp_PtrArray*b=sp_PtrArray_new_scan(a->scan_elem);for(mrb_int i=0;i<a->len;i++)sp_PtrArray_push(b,a->data[i]);return b;}")
8816- emit_raw("static void sp_PtrArray_shuffle_bang(sp_PtrArray*a){for(mrb_int i=a->len-1;i>0;i--){mrb_int j=(mrb_int)(rand()%(i+1));void*t=a->data[i];a->data[i]=a->data[j];a->data[j]=t;}}")
8817- emit_raw("static sp_PtrArray*sp_PtrArray_shuffle(sp_PtrArray*a){sp_PtrArray*b=sp_PtrArray_dup(a);sp_PtrArray_shuffle_bang(b);return b;}")
8818- emit_raw("")
8819- end
8820-
8821- def emit_float_array_runtime
8822- emit_raw("typedef struct{mrb_float*data;mrb_int len;mrb_int cap;}sp_FloatArray;")
8823- emit_raw("static void sp_FloatArray_fin(void*p){free(((sp_FloatArray*)p)->data);}")
8824- emit_raw("static sp_FloatArray*sp_FloatArray_new(void){sp_FloatArray*a=(sp_FloatArray*)sp_gc_alloc(sizeof(sp_FloatArray),sp_FloatArray_fin,NULL);a->cap=16;a->data=(mrb_float*)malloc(sizeof(mrb_float)*a->cap);a->len=0;return a;}")
8825- emit_raw("static inline void sp_FloatArray_push(sp_FloatArray*a,mrb_float v){if(a->len>=a->cap){a->cap=a->cap*2+1;a->data=(mrb_float*)realloc(a->data,sizeof(mrb_float)*a->cap);}a->data[a->len++]=v;}")
8826- emit_raw("static inline mrb_float sp_FloatArray_pop(sp_FloatArray*a){return a->data[--a->len];}")
8827- emit_raw("static inline mrb_int sp_FloatArray_length(sp_FloatArray*a){return a->len;}")
8828- emit_raw("static inline mrb_bool sp_FloatArray_empty(sp_FloatArray*a){return a->len==0;}")
8829- emit_raw("static inline mrb_float sp_FloatArray_get(sp_FloatArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}")
8830- emit_raw("static inline void sp_FloatArray_set(sp_FloatArray*a,mrb_int i,mrb_float v){if(i<0)i+=a->len;while(i>=a->cap){a->cap=a->cap*2+1;a->data=(mrb_float*)realloc(a->data,sizeof(mrb_float)*a->cap);}while(i>=a->len){a->data[a->len]=0.0;a->len++;}a->data[i]=v;}")
8831- emit_raw("")
8832- end
8833-
8834- def emit_str_array_runtime
8835- emit_raw("typedef struct{const char**data;mrb_int len;mrb_int cap;}sp_StrArray;")
8836- emit_raw("static void sp_StrArray_fin(void*p){free(((sp_StrArray*)p)->data);}")
8837- emit_raw("static sp_StrArray*sp_StrArray_new(void){sp_StrArray*a=(sp_StrArray*)sp_gc_alloc(sizeof(sp_StrArray),sp_StrArray_fin,NULL);a->cap=16;a->data=(const char**)malloc(sizeof(const char*)*a->cap);a->len=0;return a;}")
8838- emit_raw("static inline void sp_StrArray_push(sp_StrArray*a,const char*v){if(a->len>=a->cap){a->cap=a->cap*2+1;a->data=(const char**)realloc(a->data,sizeof(const char*)*a->cap);}a->data[a->len++]=v;}")
8839- emit_raw("static const char*sp_StrArray_pop(sp_StrArray*a){return a->data[--a->len];}")
8840- emit_raw("static inline mrb_int sp_StrArray_length(sp_StrArray*a){return a->len;}")
8841- emit_raw("static inline mrb_bool sp_StrArray_empty(sp_StrArray*a){return a->len==0;}")
8842- emit_raw("static inline const char*sp_StrArray_get(sp_StrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}")
8843- emit_raw("static inline void sp_StrArray_set(sp_StrArray*a,mrb_int i,const char*v){if(i<0)i+=a->len;while(i>=a->len)sp_StrArray_push(a,\"\");a->data[i]=v;}")
8844- emit_raw("static const char*sp_StrArray_join(sp_StrArray*a,const char*sep){size_t sl=strlen(sep),cap=256;char*buf=(char*)malloc(cap);size_t len=0;for(mrb_int i=0;i<a->len;i++){if(i>0){if(len+sl>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,sep,sl);len+=sl;}size_t el=strlen(a->data[i]);if(len+el>=cap){cap=(len+el)*2+1;buf=(char*)realloc(buf,cap);}memcpy(buf+len,a->data[i],el);len+=el;}buf[len]=0;return buf;}")
8845- emit_raw("static mrb_bool sp_StrArray_include(sp_StrArray*a,const char*v){for(mrb_int i=0;i<a->len;i++)if(strcmp(a->data[i],v)==0)return TRUE;return FALSE;}")
8846- emit_raw("")
8847- end
8848-
8849- def emit_str_hash_func
8850- emit_raw("static inline uint64_t sp_str_hash(const char*s){uint64_t h=14695981039346656037ULL;while(*s){h^=(unsigned char)*s++;h*=1099511628211ULL;}return h;}")
8851- end
8852-
8853- def emit_str_int_hash_runtime
8854- emit_raw("typedef struct{const char**keys;mrb_int*vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;}sp_StrIntHash;")
8855- emit_raw("static void sp_StrIntHash_fin(void*p){sp_StrIntHash*h=(sp_StrIntHash*)p;free(h->keys);free(h->vals);free(h->order);}")
8856- emit_raw("static sp_StrIntHash*sp_StrIntHash_new(void){sp_StrIntHash*h=(sp_StrIntHash*)sp_gc_alloc(sizeof(sp_StrIntHash),sp_StrIntHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;return h;}")
8857- emit_raw("static void sp_StrIntHash_grow(sp_StrIntHash*h){mrb_int oc=h->cap;const char**ok=h->keys;mrb_int*ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->order=(const char**)realloc(h->order,sizeof(const char*)*h->cap);mrb_int ol=h->len;h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]){mrb_int idx=(mrb_int)(sp_str_hash(ok[i])&h->mask);while(h->keys[idx])idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}")
8858- emit_raw("static mrb_int sp_StrIntHash_get(sp_StrIntHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}return 0;}")
8859- emit_raw("static void sp_StrIntHash_set(sp_StrIntHash*h,const char*k,mrb_int v){if(h->len*2>=h->cap)sp_StrIntHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){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++;}")
8860- emit_raw("static mrb_bool sp_StrIntHash_has_key(sp_StrIntHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")
8861- emit_raw("static mrb_int sp_StrIntHash_length(sp_StrIntHash*h){return h->len;}")
8862- emit_raw("static void sp_StrIntHash_delete(sp_StrIntHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->keys[idx]=NULL;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]){mrb_int nj=(mrb_int)(sp_str_hash(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]=NULL;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}")
8863- emit_raw("static sp_StrArray*sp_StrIntHash_keys(sp_StrIntHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}")
8864- emit_raw("static sp_StrIntHash*sp_StrIntHash_merge(sp_StrIntHash*a,sp_StrIntHash*b){sp_StrIntHash*r=sp_StrIntHash_new();for(mrb_int i=0;i<a->len;i++)sp_StrIntHash_set(r,a->order[i],sp_StrIntHash_get(a,a->order[i]));for(mrb_int i=0;i<b->len;i++)sp_StrIntHash_set(r,b->order[i],sp_StrIntHash_get(b,b->order[i]));return r;}")
8865- emit_raw("")
8866- end
8867-
88688691 # Symbol-keyed hash with integer values. Keys are sp_sym (mrb_int);
88698692 # the empty-slot sentinel is -1 (= invalid sp_sym, same as default).
88708693 def emit_sym_int_hash_runtime
@@ -8880,23 +8703,6 @@
88808703 emit_raw("")
88818704 end
88828705
8883- # Integer-keyed hash with string values.
8884- def emit_int_str_hash_runtime
8885- emit_raw("typedef struct{mrb_int*keys;const char**vals;mrb_int*order;mrb_bool*used;mrb_int len;mrb_int cap;mrb_int mask;}sp_IntStrHash;")
8886- emit_raw("static void sp_IntStrHash_fin(void*p){sp_IntStrHash*h=(sp_IntStrHash*)p;free(h->keys);free(h->vals);free(h->order);free(h->used);}")
8887- emit_raw("static void sp_IntStrHash_scan(void*p){sp_IntStrHash*h=(sp_IntStrHash*)p;for(mrb_int i=0;i<h->cap;i++)if(h->used[i])sp_mark_string(h->vals[i]);}")
8888- emit_raw("static sp_IntStrHash*sp_IntStrHash_new(void){sp_IntStrHash*h=(sp_IntStrHash*)sp_gc_alloc(sizeof(sp_IntStrHash),sp_IntStrHash_fin,sp_IntStrHash_scan);h->cap=16;h->mask=15;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;return h;}")
8889- emit_raw("static inline mrb_int _sp_istr_idx(mrb_int mask,mrb_int k){return(mrb_int)(((uint64_t)(unsigned long long)k*11400714819323198485ULL)&(uint64_t)mask);}")
8890- emit_raw("static void sp_IntStrHash_grow(sp_IntStrHash*h){mrb_int oc=h->cap,ol=h->len;mrb_int*ok=h->keys;const char**ov=h->vals;mrb_bool*ou=h->used;mrb_int*oo=h->order;h->cap*=2;h->mask=h->cap-1;h->keys=(mrb_int*)calloc(h->cap,sizeof(mrb_int));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(mrb_int*)malloc(sizeof(mrb_int)*h->cap);h->used=(mrb_bool*)calloc(h->cap,sizeof(mrb_bool));h->len=0;for(mrb_int i=0;i<ol;i++){mrb_int k=oo[i];mrb_int si=_sp_istr_idx(oc-1,k);while(!ou[si]||ok[si]!=k)si=(si+1)&(oc-1);const char*v=ov[si];mrb_int di=_sp_istr_idx(h->mask,k);while(h->used[di])di=(di+1)&h->mask;h->used[di]=TRUE;h->keys[di]=k;h->vals[di]=v;h->order[h->len++]=k;}free(ok);free(ov);free(ou);free(oo);}")
8891- emit_raw("static void sp_IntStrHash_set(sp_IntStrHash*h,mrb_int k,const char*v){if(h->len*2>=h->cap)sp_IntStrHash_grow(h);mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k){h->vals[idx]=v;return;}idx=(idx+1)&h->mask;}h->used[idx]=TRUE;h->keys[idx]=k;h->vals[idx]=v;h->order[h->len++]=k;}")
8892- emit_raw("static const char*sp_IntStrHash_get(sp_IntStrHash*h,mrb_int k){mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return h->vals[idx];idx=(idx+1)&h->mask;}return\"\";}")
8893- emit_raw("static mrb_bool sp_IntStrHash_has_key(sp_IntStrHash*h,mrb_int k){mrb_int idx=_sp_istr_idx(h->mask,k);while(h->used[idx]){if(h->keys[idx]==k)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")
8894- emit_raw("static mrb_int sp_IntStrHash_length(sp_IntStrHash*h){return h->len;}")
8895- emit_raw("static sp_IntArray*sp_IntStrHash_keys(sp_IntStrHash*h){sp_IntArray*a=sp_IntArray_new();for(mrb_int i=0;i<h->len;i++)sp_IntArray_push(a,h->order[i]);return a;}")
8896- emit_raw("static sp_StrArray*sp_IntStrHash_values(sp_IntStrHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,sp_IntStrHash_get(h,h->order[i]));return a;}")
8897- emit_raw("")
8898- end
8899-
89008706 # Symbol-keyed hash with string values.
89018707 def emit_sym_str_hash_runtime
89028708 emit_raw("typedef struct{sp_sym*keys;const char**vals;sp_sym*order;mrb_int len;mrb_int cap;mrb_int mask;}sp_SymStrHash;")
@@ -8911,20 +8717,6 @@
89118717 emit_raw("")
89128718 end
89138719
8914- def emit_str_str_hash_runtime
8915- emit_raw("typedef struct{const char**keys;const char**vals;const char**order;mrb_int len;mrb_int cap;mrb_int mask;}sp_StrStrHash;")
8916- emit_raw("static void sp_StrStrHash_fin(void*p){sp_StrStrHash*h=(sp_StrStrHash*)p;free(h->keys);free(h->vals);free(h->order);}")
8917- emit_raw("static sp_StrStrHash*sp_StrStrHash_new(void){sp_StrStrHash*h=(sp_StrStrHash*)sp_gc_alloc(sizeof(sp_StrStrHash),sp_StrStrHash_fin,NULL);h->cap=16;h->mask=15;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(const char**)malloc(sizeof(const char*)*h->cap);h->len=0;return h;}")
8918- emit_raw("static void sp_StrStrHash_grow(sp_StrStrHash*h){mrb_int oc=h->cap;const char**ok=h->keys;const char**ov=h->vals;h->cap*=2;h->mask=h->cap-1;h->keys=(const char**)calloc(h->cap,sizeof(const char*));h->vals=(const char**)calloc(h->cap,sizeof(const char*));h->order=(const char**)realloc(h->order,sizeof(const char*)*h->cap);h->len=0;for(mrb_int i=0;i<oc;i++){if(ok[i]){mrb_int idx=(mrb_int)(sp_str_hash(ok[i])&h->mask);while(h->keys[idx])idx=(idx+1)&h->mask;h->keys[idx]=ok[i];h->vals[idx]=ov[i];h->len++;}}free(ok);free(ov);}")
8919- emit_raw("static const char*sp_StrStrHash_get(sp_StrStrHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return h->vals[idx];idx=(idx+1)&h->mask;}return\"\";}")
8920- emit_raw("static void sp_StrStrHash_set(sp_StrStrHash*h,const char*k,const char*v){if(h->len*2>=h->cap)sp_StrStrHash_grow(h);mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){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++;}")
8921- emit_raw("static mrb_bool sp_StrStrHash_has_key(sp_StrStrHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}")
8922- emit_raw("static mrb_int sp_StrStrHash_length(sp_StrStrHash*h){return h->len;}")
8923- emit_raw("static void sp_StrStrHash_delete(sp_StrStrHash*h,const char*k){mrb_int idx=(mrb_int)(sp_str_hash(k)&h->mask);while(h->keys[idx]){if(strcmp(h->keys[idx],k)==0){h->keys[idx]=NULL;h->vals[idx]=NULL;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]){mrb_int nj=(mrb_int)(sp_str_hash(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]=NULL;h->vals[j]=NULL;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}")
8924- emit_raw("static sp_StrArray*sp_StrStrHash_keys(sp_StrStrHash*h){sp_StrArray*a=sp_StrArray_new();for(mrb_int i=0;i<h->len;i++)sp_StrArray_push(a,h->order[i]);return a;}")
8925- emit_raw("")
8926- end
8927-
89288720 # Symbol type Phase 2, Step 1: collect all SymbolNode content strings
89298721 # into @sym_names as a separate pass (dedup, stable order).
89308722 def collect_sym_names
@@ -9083,17 +8875,6 @@
90838875 i = i + 1
90848876 end
90858877 1
9086- end
9087-
9088- def emit_mutable_str_runtime
9089- emit_raw("typedef struct{char*data;int64_t len;int64_t cap;}sp_String;")
9090- emit_raw("static void sp_String_fin(void*p){free(((sp_String*)p)->data);}")
9091- emit_raw("static sp_String*sp_String_new(const char*s){sp_String*r=(sp_String*)sp_gc_alloc(sizeof(sp_String),sp_String_fin,NULL);r->len=(int64_t)strlen(s);r->cap=r->len*2+16;r->data=(char*)malloc(r->cap+1);memcpy(r->data,s,r->len+1);{sp_gc_hdr*h=(sp_gc_hdr*)((char*)r-sizeof(sp_gc_hdr));h->size+=r->cap+1;sp_gc_bytes+=r->cap+1;}return r;}")
9092- emit_raw("static inline void sp_String_append(sp_String*s,const char*t){int64_t tl=(int64_t)strlen(t);if(s->len+tl>=s->cap){sp_gc_hdr*h=(sp_gc_hdr*)((char*)s-sizeof(sp_gc_hdr));sp_gc_bytes-=s->cap+1;h->size-=s->cap+1;s->cap=(s->len+tl)*2+16;s->data=(char*)realloc(s->data,s->cap+1);h->size+=s->cap+1;sp_gc_bytes+=s->cap+1;}memcpy(s->data+s->len,t,tl+1);s->len+=tl;}")
9093- emit_raw("static inline const char*sp_String_cstr(sp_String*s){return s->data;}")
9094- emit_raw("static inline int64_t sp_String_length(sp_String*s){return s->len;}")
9095- emit_raw("static sp_String*sp_String_dup(sp_String*s){return sp_String_new(s->data);}")
9096- emit_raw("")
90978878 end
90988879
90998880 def emit_regexp_runtime
@@ -9148,162 +8929,6 @@
91488929 emit_raw("")
91498930 end
91508931
9151- def emit_rb_value_runtime
9152- emit_raw("/* NaN-boxed polymorphic value */")
9153- emit_raw("typedef uint64_t sp_RbValue;")
9154- emit_raw("#define SP_TAG_INT 0")
9155- emit_raw("#define SP_TAG_STR 1")
9156- emit_raw("#define SP_TAG_FLT 2")
9157- emit_raw("#define SP_TAG_BOOL 3")
9158- emit_raw("#define SP_TAG_NIL 4")
9159- emit_raw("#define SP_TAG_OBJ 5")
9160- emit_raw("typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;")
9161- emit_raw("static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }")
9162- emit_raw("static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }")
9163- emit_raw("static sp_RbVal sp_box_float(mrb_float v) { sp_RbVal r; r.tag = SP_TAG_FLT; r.cls_id = 0; r.v.f = v; return r; }")
9164- emit_raw("static sp_RbVal sp_box_bool(mrb_bool v) { sp_RbVal r; r.tag = SP_TAG_BOOL; r.cls_id = 0; r.v.b = v; return r; }")
9165- emit_raw("static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.cls_id = 0; r.v.i = 0; return r; }")
9166- emit_raw("static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.cls_id = cls_id; r.v.p = p; return r; }")
9167- emit_raw("static void sp_poly_puts(sp_RbVal v) {")
9168- emit_raw(" switch (v.tag) {")
9169- emit_raw(" case SP_TAG_INT: printf(\"%lld" + bsl_n + "\", (long long)v.v.i); break;")
9170- emit_raw(" case SP_TAG_STR: if (v.v.s) { fputs(v.v.s, stdout); if (!*v.v.s || v.v.s[strlen(v.v.s)-1] != '" + bsl_n + "') putchar('" + bsl_n + "'); } else putchar('" + bsl_n + "'); break;")
9171- emit_raw(" case SP_TAG_FLT: { char _fb[64]; snprintf(_fb,64,\"%g\",v.v.f); if(!strchr(_fb,'.')&&!strchr(_fb,'e')&&!strchr(_fb,'i')&&!strchr(_fb,'n')){strcat(_fb,\".0\");} printf(\"%s" + bsl_n + "\",_fb); break; }")
9172- emit_raw(" case SP_TAG_BOOL: puts(v.v.b ? \"true\" : \"false\"); break;")
9173- emit_raw(" case SP_TAG_NIL: putchar('" + bsl_n + "'); break;")
9174- emit_raw(" default: printf(\"%lld" + bsl_n + "\", (long long)v.v.i); break;")
9175- emit_raw(" }")
9176- emit_raw("}")
9177- emit_raw("static mrb_bool sp_poly_nil_p(sp_RbVal v) { return v.tag == SP_TAG_NIL; }")
9178- emit_raw("static const char *sp_poly_to_s(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: return sp_int_to_s(v.v.i); case SP_TAG_STR: return v.v.s ? v.v.s : \"\"; case SP_TAG_FLT: return sp_float_to_s(v.v.f); case SP_TAG_BOOL: return v.v.b ? \"true\" : \"false\"; case SP_TAG_NIL: return \"\"; default: return \"\"; } }")
9179- emit_raw("static sp_RbVal sp_poly_add(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i + b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f + b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i + b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f + (mrb_float)b.v.i); if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR) return sp_box_str(sp_str_concat(a.v.s, b.v.s)); return sp_box_int(0); }")
9180- emit_raw("static sp_RbVal sp_poly_sub(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i - b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f - b.v.f); return sp_box_int(0); }")
9181- emit_raw("static sp_RbVal sp_poly_mul(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i * b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f * b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i * b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f * (mrb_float)b.v.i); return sp_box_int(0); }")
9182- emit_raw("static mrb_bool sp_poly_gt(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return a.v.i > b.v.i; if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return a.v.f > b.v.f; return FALSE; }")
9183- emit_raw("")
9184- emit_raw("/* PolyArray: array of sp_RbVal */")
9185- emit_raw("typedef struct { sp_RbVal *data; mrb_int len; mrb_int cap; } sp_PolyArray;")
9186- emit_raw("static sp_PolyArray *sp_PolyArray_new(void) { sp_PolyArray *a = (sp_PolyArray *)sp_gc_alloc(sizeof(sp_PolyArray), NULL, NULL); a->cap = 16; a->data = (sp_RbVal *)malloc(sizeof(sp_RbVal) * a->cap); a->len = 0; return a; }")
9187- emit_raw("static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (a->len >= a->cap) { a->cap = a->cap * 2 + 1; a->data = (sp_RbVal *)realloc(a->data, sizeof(sp_RbVal) * a->cap); } a->data[a->len++] = v; }")
9188- emit_raw("static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }")
9189- emit_raw("static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }")
9190- emit_raw("static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }")
9191- emit_raw("static void sp_PolyArray_shuffle_bang(sp_PolyArray *a) { for (mrb_int i = a->len - 1; i > 0; i--) { mrb_int j = (mrb_int)(rand() % (i + 1)); sp_RbVal t = a->data[i]; a->data[i] = a->data[j]; a->data[j] = t; } }")
9192- emit_raw("static sp_PolyArray *sp_PolyArray_shuffle(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_dup(a); sp_PolyArray_shuffle_bang(b); return b; }")
9193- emit_raw("")
9194- end
9195-
9196- def emit_setjmp_runtime
9197- emit_raw("#include <setjmp.h>")
9198- emit_raw("#define SP_EXC_STACK_MAX 64")
9199- emit_raw("static jmp_buf sp_exc_stack[SP_EXC_STACK_MAX];")
9200- emit_raw("static const char *sp_exc_msg[SP_EXC_STACK_MAX];")
9201- emit_raw("static volatile int sp_exc_top = 0;")
9202- emit_raw("static const char *sp_exc_cls[SP_EXC_STACK_MAX];")
9203- emit_raw("static volatile const char *sp_last_exc_cls = \"\";")
9204- emit_raw("static void sp_raise_cls(const char *cls, const char *msg) { if (sp_exc_top > 0) { sp_exc_msg[sp_exc_top-1] = msg; sp_exc_cls[sp_exc_top-1] = cls; sp_last_exc_cls = cls; longjmp(sp_exc_stack[sp_exc_top-1], 1); } fprintf(stderr, \"unhandled exception: %s" + bsl_n + "\", msg); exit(1); }")
9205- emit_raw("static void sp_raise(const char *msg) { sp_raise_cls(\"RuntimeError\", msg); }")
9206- emit_raw("static mrb_bool sp_exc_is_a(const char *cls, const char *target) { return strcmp(cls, target) == 0; }")
9207- emit_raw("")
9208- # catch/throw support
9209- emit_raw("#define SP_CATCH_STACK_MAX 64")
9210- emit_raw("static jmp_buf sp_catch_stack[SP_CATCH_STACK_MAX];")
9211- emit_raw("static const char *sp_catch_tag[SP_CATCH_STACK_MAX];")
9212- emit_raw("static mrb_int sp_catch_val[SP_CATCH_STACK_MAX];")
9213- emit_raw("static volatile int sp_catch_top = 0;")
9214- emit_raw("static void sp_throw(const char *tag, mrb_int val) { int i = sp_catch_top - 1; while (i >= 0) { if (strcmp(sp_catch_tag[i], tag) == 0) { sp_catch_val[i] = val; sp_catch_top = i + 1; longjmp(sp_catch_stack[i], 1); } i--; } fprintf(stderr, \"uncaught throw: %s" + bsl_n + "\", tag); exit(1); }")
9215- emit_raw("")
9216- end
9217-
9218- def emit_file_io_runtime
9219- emit_raw("static const char *sp_file_read(const char *path) { FILE *f = fopen(path, \"rb\"); if (!f) return \"\"; fseek(f, 0, SEEK_END); long sz = ftell(f); fseek(f, 0, SEEK_SET); char *buf = (char *)malloc(sz + 1); if (sz > 0) { size_t r = fread(buf, 1, sz, f); (void)r; } buf[sz] = 0; fclose(f); return buf; }")
9220- emit_raw("static void sp_file_write(const char *path, const char *data) { FILE *f = fopen(path, \"w\"); if (f) { fputs(data, f); fclose(f); } }")
9221- emit_raw("static mrb_bool sp_file_exist(const char *path) { FILE *f = fopen(path, \"r\"); if (f) { fclose(f); return TRUE; } return FALSE; }")
9222- emit_raw("static void sp_file_delete(const char *path) { remove(path); }")
9223- emit_raw("static const char *sp_backtick(const char *cmd) { FILE *p = popen(cmd, \"r\"); if (!p) return \"\"; char *buf = (char *)malloc(4096); size_t n = fread(buf, 1, 4095, p); buf[n] = 0; pclose(p); return buf; }")
9224- emit_raw("static const char *sp_file_basename(const char *path) { const char *s = strrchr(path, '/'); if (s) return s + 1; return path; }")
9225- emit_raw("")
9226- end
9227-
9228- def emit_proc_runtime
9229- emit_raw("typedef mrb_int (*sp_proc_fn_t)(void *, mrb_int);")
9230- emit_raw("typedef struct sp_Proc { sp_proc_fn_t fn; void *cap; void (*cap_scan)(void *); } sp_Proc;")
9231- emit_raw("static void sp_Proc_scan(void *p) { sp_Proc *pr = (sp_Proc *)p; if (pr->cap && pr->cap_scan) pr->cap_scan(pr->cap); }")
9232- emit_raw("static sp_Proc *sp_proc_new(sp_proc_fn_t fn, void *cap, void (*cap_scan)(void *)) { sp_Proc *p = (sp_Proc *)sp_gc_alloc(sizeof(sp_Proc), NULL, sp_Proc_scan); p->fn = fn; p->cap = cap; p->cap_scan = cap_scan; return p; }")
9233- emit_raw("static mrb_int sp_proc_call(sp_Proc *p, mrb_int arg) { return (p && p->fn) ? p->fn(p->cap, arg) : 0; }")
9234- emit_raw("")
9235- end
9236-
9237- def emit_lambda_runtime
9238- emit_raw("/* ---- Lambda/closure runtime (sp_Val) ---- */")
9239- emit_raw("#include <sys/mman.h>")
9240- emit_raw("typedef struct sp_Val sp_Val;")
9241- emit_raw("typedef sp_Val *(*sp_fn_t)(sp_Val *self, sp_Val *arg);")
9242- emit_raw("struct sp_Val { enum { SP_PROC2, SP_INT2, SP_BOOL2, SP_NIL2 } tag; union { struct { sp_fn_t fn; int ncaptures; } proc; mrb_int ival; mrb_bool bval; } u; sp_Val *captures[]; };")
9243- emit_raw("#define SP_ARENA_SIZE ((size_t)16ULL * 1024 * 1024 * 1024)")
9244- emit_raw("static char *sp_arena = NULL; static size_t sp_arena_pos = 0;")
9245- emit_raw("static void *sp_lam_alloc(size_t sz) { sz = (sz + 7) & ~(size_t)7; if (!sp_arena) { sp_arena = (char *)mmap(NULL, SP_ARENA_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE, -1, 0); if (sp_arena == MAP_FAILED) { perror(\"mmap\"); exit(1); } sp_arena_pos = 0; } if (sp_arena_pos + sz > SP_ARENA_SIZE) { fprintf(stderr, \"arena exhausted" + bsl_n + "\"); exit(1); } void *p = sp_arena + sp_arena_pos; sp_arena_pos += sz; return p; }")
9246- emit_raw("static sp_Val *sp_lam_proc(sp_fn_t fn, int ncap) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val) + sizeof(sp_Val *) * ncap); v->tag = SP_PROC2; v->u.proc.fn = fn; v->u.proc.ncaptures = ncap; return v; }")
9247- emit_raw("static sp_Val *sp_lam_int(mrb_int n) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val)); v->tag = SP_INT2; v->u.ival = n; return v; }")
9248- emit_raw("static sp_Val *sp_lam_bool(mrb_bool b) { sp_Val *v = (sp_Val *)sp_lam_alloc(sizeof(sp_Val)); v->tag = SP_BOOL2; v->u.bval = b; return v; }")
9249- emit_raw("static sp_Val sp_lam_nil_val = { .tag = SP_NIL2 };")
9250- emit_raw("static sp_Val *sp_lam_call(sp_Val *f, sp_Val *arg) { return f->u.proc.fn(f, arg); }")
9251- emit_raw("static mrb_int sp_lam_to_int(sp_Val *v) { return v->u.ival; }")
9252- emit_raw("")
9253- end
9254-
9255- def emit_fiber_runtime
9256- emit_raw("/* ---- Fiber runtime (ucontext) ---- */")
9257- emit_raw("#include <ucontext.h>")
9258- emit_raw("#include <sys/mman.h>")
9259- emit_raw("#define SP_FIBER_STACK_SIZE (64*1024)")
9260- emit_raw("typedef struct sp_Fiber{ucontext_t ctx;ucontext_t caller_ctx;char*stack;int state;int transferred;sp_RbVal yielded_value;sp_RbVal resumed_value;void(*body)(struct sp_Fiber*);void*user_data;int saved_exc_top;int saved_catch_top;}sp_Fiber;")
9261- emit_raw("static sp_Fiber sp_fiber_root;")
9262- emit_raw("static sp_Fiber*sp_fiber_current=&sp_fiber_root;")
9263- emit_raw("static void sp_Fiber_fin(void*p){sp_Fiber*f=(sp_Fiber*)p;if(f->stack)munmap(f->stack,SP_FIBER_STACK_SIZE);}")
9264- emit_raw("static void sp_Fiber_scan(void*p){sp_Fiber*f=(sp_Fiber*)p;if(f->user_data)sp_gc_mark(f->user_data);}")
9265- emit_raw("static sp_Fiber*sp_Fiber_new(void(*body)(sp_Fiber*)){sp_Fiber*f=(sp_Fiber*)sp_gc_alloc(sizeof(sp_Fiber),sp_Fiber_fin,sp_Fiber_scan);f->stack=(char*)mmap(NULL,SP_FIBER_STACK_SIZE,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);f->state=0;f->transferred=0;f->body=body;f->yielded_value=sp_box_nil();f->resumed_value=sp_box_nil();f->user_data=NULL;f->saved_exc_top=0;f->saved_catch_top=0;return f;}")
9266- emit_raw("static void sp_fiber_trampoline(void){sp_Fiber*f=sp_fiber_current;f->body(f);f->state=3;if(f->transferred){sp_fiber_current=&sp_fiber_root;setcontext(&sp_fiber_root.ctx);}else{swapcontext(&f->ctx,&f->caller_ctx);}}")
9267- emit_raw("static sp_RbVal sp_Fiber_resume(sp_Fiber*f,sp_RbVal val){if(f->state==3){sp_raise_cls(\"FiberError\",\"attempt to resume a terminated fiber\");}f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_current=f;if(f->state==0){f->state=1;getcontext(&f->ctx);f->ctx.uc_stack.ss_sp=f->stack;f->ctx.uc_stack.ss_size=SP_FIBER_STACK_SIZE;f->ctx.uc_link=&f->caller_ctx;makecontext(&f->ctx,(void(*)(void))sp_fiber_trampoline,0);swapcontext(&f->caller_ctx,&f->ctx);}else{f->state=1;swapcontext(&f->caller_ctx,&f->ctx);}sp_fiber_current=prev;return f->yielded_value;}")
9268- emit_raw("static sp_RbVal sp_Fiber_yield(sp_RbVal val){sp_Fiber*f=sp_fiber_current;f->yielded_value=val;f->state=2;swapcontext(&f->ctx,&f->caller_ctx);return f->resumed_value;}")
9269- emit_raw("static mrb_bool sp_Fiber_alive(sp_Fiber*f){return f->state!=3;}")
9270- emit_raw("static sp_RbVal sp_Fiber_transfer(sp_Fiber*f,sp_RbVal val){f->resumed_value=val;sp_Fiber*prev=sp_fiber_current;sp_fiber_current=f;if(f->state==0){f->state=1;f->transferred=1;getcontext(&f->ctx);f->ctx.uc_stack.ss_sp=f->stack;f->ctx.uc_stack.ss_size=SP_FIBER_STACK_SIZE;f->ctx.uc_link=&prev->ctx;makecontext(&f->ctx,(void(*)(void))sp_fiber_trampoline,0);swapcontext(&prev->ctx,&f->ctx);}else{f->state=1;swapcontext(&prev->ctx,&f->ctx);}sp_fiber_current=prev;return prev->resumed_value;}")
9271- emit_raw("")
9272- end
9273-
9274- def emit_stringio_runtime
9275- emit_raw("/* ---- StringIO runtime ---- */")
9276- emit_raw("typedef struct { char *buf; int64_t len; int64_t cap; int64_t pos; int64_t lineno; int closed; } sp_StringIO;")
9277- emit_raw("static void sio_grow(sp_StringIO *sio, int64_t need) { int64_t req = sio->pos + need; if (req <= sio->cap) return; int64_t nc = sio->cap ? sio->cap : 64; while (nc < req) nc *= 2; sio->buf = (char *)realloc(sio->buf, nc + 1); sio->cap = nc; }")
9278- emit_raw("static int64_t sio_write(sp_StringIO *sio, const char *d, int64_t dl) { sio_grow(sio, dl); if (sio->pos > sio->len) memset(sio->buf + sio->len, 0, sio->pos - sio->len); memcpy(sio->buf + sio->pos, d, dl); sio->pos += dl; if (sio->pos > sio->len) sio->len = sio->pos; sio->buf[sio->len] = '" + bsl + "0'; return dl; }")
9279- emit_raw("static sp_StringIO *sp_StringIO_new(void) { sp_StringIO *s = (sp_StringIO *)calloc(1, sizeof(sp_StringIO)); s->buf = (char *)calloc(1, 64); s->cap = 63; return s; }")
9280- emit_raw("static sp_StringIO *sp_StringIO_new_s(const char *init) { sp_StringIO *s = (sp_StringIO *)calloc(1, sizeof(sp_StringIO)); int64_t l = (int64_t)strlen(init); int64_t c = l < 63 ? 63 : l; s->buf = (char *)malloc(c+1); memcpy(s->buf, init, l); s->buf[l]='" + bsl + "0'; s->len = l; s->cap = c; return s; }")
9281- emit_raw("static const char *sp_StringIO_string(sp_StringIO *s) { return s->buf ? s->buf : \"\"; }")
9282- emit_raw("static int64_t sp_StringIO_pos(sp_StringIO *s) { return s->pos; }")
9283- emit_raw("static int64_t sp_StringIO_size(sp_StringIO *s) { return s->len; }")
9284- emit_raw("static int64_t sp_StringIO_write(sp_StringIO *s, const char *str) { return sio_write(s, str, (int64_t)strlen(str)); }")
9285- emit_raw("static int64_t sp_StringIO_puts(sp_StringIO *s, const char *str) { int64_t l = (int64_t)strlen(str); sio_write(s, str, l); if (l == 0 || str[l-1] != '" + bsl_n + "') sio_write(s, \"" + bsl_n + "\", 1); return 0; }")
9286- emit_raw("static int64_t sp_StringIO_puts_empty(sp_StringIO *s) { sio_write(s, \"" + bsl_n + "\", 1); return 0; }")
9287- emit_raw("static int64_t sp_StringIO_print(sp_StringIO *s, const char *str) { return sio_write(s, str, (int64_t)strlen(str)); }")
9288- emit_raw("static int64_t sp_StringIO_putc(sp_StringIO *s, int64_t ch) { char c = (char)(ch & 0xFF); sio_write(s, &c, 1); return ch; }")
9289- emit_raw("static const char *sp_StringIO_read(sp_StringIO *s) { if (s->pos >= s->len) return \"\"; const char *r = s->buf + s->pos; s->pos = s->len; return r; }")
9290- emit_raw("static const char *sp_StringIO_read_n(sp_StringIO *s, int64_t n) { if (s->pos >= s->len) return \"\"; int64_t rem = s->len - s->pos; if (n > rem) n = rem; char *r = (char *)malloc(n+1); memcpy(r, s->buf + s->pos, n); r[n] = '" + bsl + "0'; s->pos += n; return r; }")
9291- emit_raw("static const char *sp_StringIO_gets(sp_StringIO *s) { if (s->pos >= s->len) return NULL; const char *st = s->buf + s->pos; const char *nl = memchr(st, '" + bsl_n + "', s->len - s->pos); int64_t ll = nl ? (nl - st) + 1 : s->len - s->pos; char *r = (char *)malloc(ll+1); memcpy(r, st, ll); r[ll] = '" + bsl + "0'; s->pos += ll; s->lineno++; return r; }")
9292- emit_raw("static const char *sp_StringIO_getc(sp_StringIO *s) { if (s->pos >= s->len) return NULL; char *gc = (char *)malloc(2); gc[0] = s->buf[s->pos++]; gc[1] = '" + bsl + "0'; return gc; }")
9293- emit_raw("static int64_t sp_StringIO_getbyte(sp_StringIO *s) { if (s->pos >= s->len) return -1; return (int64_t)(unsigned char)s->buf[s->pos++]; }")
9294- emit_raw("static int64_t sp_StringIO_rewind(sp_StringIO *s) { s->pos = 0; s->lineno = 0; return 0; }")
9295- emit_raw("static int64_t sp_StringIO_seek(sp_StringIO *s, int64_t off) { if (off < 0) off = 0; s->pos = off; return 0; }")
9296- emit_raw("static int64_t sp_StringIO_tell(sp_StringIO *s) { return s->pos; }")
9297- emit_raw("static mrb_bool sp_StringIO_eof_p(sp_StringIO *s) { return s->pos >= s->len; }")
9298- emit_raw("static int64_t sp_StringIO_truncate(sp_StringIO *s, int64_t l) { if (l < 0) l = 0; if (l < s->len) { s->len = l; s->buf[l] = '" + bsl + "0'; } return 0; }")
9299- emit_raw("static int64_t sp_StringIO_close(sp_StringIO *s) { s->closed = 1; return 0; }")
9300- emit_raw("static mrb_bool sp_StringIO_closed_p(sp_StringIO *s) { return s->closed; }")
9301- emit_raw("static sp_StringIO *sp_StringIO_flush(sp_StringIO *s) { return s; }")
9302- emit_raw("static mrb_bool sp_StringIO_sync(sp_StringIO *s) { (void)s; return 1; }")
9303- emit_raw("static mrb_bool sp_StringIO_isatty(sp_StringIO *s) { (void)s; return 0; }")
9304- emit_raw("")
9305- end
9306-
93078932 # ---- Struct emission ----
93088933 def emit_global_constants
93098934 # Emit file-scope constant declarations (initialized in main)
@@ -11426,7 +11051,6 @@
1142611051 end
1142711052 if is_obj_type(arg_type) == 1
1142811053 target_type = arg_type + "_ptr_array"
11429- @needs_ptr_array = 1
1143011054 @needs_gc = 1
1143111055 ki = 0
1143211056 while ki < names.length
@@ -12542,12 +12166,9 @@
1254212166 return tmp
1254312167 end
1254412168 if t == "XStringNode"
12545- @needs_file_io = 1
1254612169 return "sp_backtick(" + c_string_literal(@nd_content[nid]) + ")"
1254712170 end
1254812171 if t == "InterpolatedXStringNode"
12549- @needs_file_io = 1
12550- @needs_string_helpers = 1
1255112172 interp = compile_interpolated(nid)
1255212173 return "sp_backtick(" + interp + ")"
1255312174 end
@@ -12760,7 +12381,6 @@
1276012381 end
1276112382
1276212383 def compile_interpolated(nid)
12763- @needs_string_helpers = 1
1276412384 parts = parse_id_list(@nd_parts[nid])
1276512385 if parts.length == 0
1276612386 return "(&(\"\\xff\")[1])"
@@ -13719,7 +13339,6 @@
1371913339 return "0"
1372013340 end
1372113341 if mname == "system"
13722- @needs_file_io = 1
1372313342 @needs_system = 1
1372413343 return "({ fflush(stdout); sp_last_status = system(" + compile_arg0(nid) + "); sp_last_status == 0; })"
1372513344 end
@@ -13766,7 +13385,6 @@
1376613385 end
1376713386 if mname == "proc"
1376813387 if @nd_block[nid] >= 0
13769- @needs_proc = 1
1377013388 return compile_proc_literal(nid)
1377113389 end
1377213390 end
@@ -13836,11 +13454,9 @@
1383613454 return "0"
1383713455 end
1383813456 if mname == "format"
13839- @needs_string_helpers = 1
1384013457 return compile_sprintf_call(nid)
1384113458 end
1384213459 if mname == "sprintf"
13843- @needs_string_helpers = 1
1384413460 return compile_sprintf_call(nid)
1384513461 end
1384613462 if mname == "putc"
@@ -13876,7 +13492,6 @@
1387613492 end
1387713493 if has_block_param == 1
1387813494 if @nd_block[nid] >= 0
13879- @needs_proc = 1
1388013495 block_proc = compile_proc_literal(nid)
1388113496 return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ", " + block_proc + ")"
1388213497 end
@@ -13888,7 +13503,6 @@
1388813503 if st != ""
1388913504 # Redirect as self.mname - string methods
1389013505 if st == "string"
13891- @needs_string_helpers = 1
1389213506 if mname == "upcase"
1389313507 return "sp_str_upcase(self)"
1389413508 end
@@ -13929,7 +13543,6 @@
1392913543 # int methods
1393013544 if st == "int"
1393113545 if mname == "to_s"
13932- @needs_string_helpers = 1
1393313546 return "sp_int_to_s(self)"
1393413547 end
1393513548 if mname == "to_f"
@@ -13945,7 +13558,6 @@
1394513558 return "(mrb_int)(self)"
1394613559 end
1394713560 if mname == "to_s"
13948- @needs_string_helpers = 1
1394913561 return "sp_float_to_s(self)"
1395013562 end
1395113563 end
@@ -14143,10 +13755,8 @@
1414313755 if at == "string"
1414413756 parts.push(compile_expr(aargs[0]))
1414513757 elsif at == "int"
14146- @needs_string_helpers = 1
1414713758 parts.push("sp_int_to_s(" + compile_expr(aargs[0]) + ")")
1414813759 elsif at == "float"
14149- @needs_string_helpers = 1
1415013760 parts.push("sp_float_to_s(" + compile_expr(aargs[0]) + ")")
1415113761 else
1415213762 parts.push(compile_expr(aargs[0]))
@@ -14224,11 +13834,9 @@
1422413834 if mname == "+"
1422513835 lt = infer_type(recv)
1422613836 if lt == "mutable_str"
14227- @needs_string_helpers = 1
1422813837 return "sp_str_concat(" + compile_expr(recv) + "->data, " + compile_arg0(nid) + ")"
1422913838 end
1423013839 if lt == "string"
14231- @needs_string_helpers = 1
1423213840 # Flatten chained string concat: a + b + c → sp_str_concat3(a,b,c)
1423313841 parts = collect_concat_chain(nid)
1423413842 if parts.length == 3
@@ -14270,7 +13878,6 @@
1427013878 end
1427113879 if lt == "poly"
1427213880 @needs_rb_value = 1
14273- @needs_string_helpers = 1
1427413881 return "sp_poly_add(" + compile_expr(recv) + ", " + box_expr_to_poly(@nd_arguments[nid] >= 0 ? get_args(@nd_arguments[nid])[0] : -1) + ")"
1427513882 end
1427613883 if is_array_type(lt) == 1
@@ -14301,7 +13908,6 @@
1430113908 if mname == "*"
1430213909 lt = infer_type(recv)
1430313910 if lt == "string"
14304- @needs_string_helpers = 1
1430513911 return "sp_str_repeat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
1430613912 end
1430713913 if lt == "poly"
@@ -14517,7 +14123,6 @@
1451714123 return "(sp_String_append(" + rc + ", " + val + "), " + rc + ")"
1451814124 end
1451914125 if lt == "string"
14520- @needs_string_helpers = 1
1452114126 return "sp_str_concat(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
1452214127 end
1452314128 return "(" + compile_expr(recv) + " << " + compile_arg0(nid) + ")"
@@ -14552,7 +14157,6 @@
1455214157 if cname != ""
1455314158 if cname == "Proc"
1455414159 if @nd_block[nid] >= 0
14555- @needs_proc = 1
1455614160 return compile_proc_literal(nid)
1455714161 end
1455814162 end
@@ -14624,7 +14228,6 @@
1462414228 # so the GC scans the elements. Without this they'd be pushed
1462514229 # into an int_array and silently swept.
1462614230 if type_is_pointer(vt) == 1
14627- @needs_ptr_array = 1
1462814231 @needs_gc = 1
1462914232 tmp = new_temp
1463014233 emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
@@ -14766,7 +14369,6 @@
1476614369 end
1476714370
1476814371 def compile_string_method_expr(nid, mname, rc)
14769- @needs_string_helpers = 1
1477014372 if mname == "length"
1477114373 # Only use hoisted length if the receiver matches (otherwise we'd
1477214374 # return the wrong string's length).
@@ -15028,7 +14630,6 @@
1502814630 return fn + "(" + lprefix + ", " + compile_arg0(nid) + ", 1)"
1502914631 end
1503014632 if mname == "reverse"
15031- @needs_string_helpers = 1
1503214633 return "sp_str_reverse(" + rc + ")"
1503314634 end
1503414635 if mname == "freeze"
@@ -15216,7 +14817,6 @@
1521614817 return rc
1521714818 end
1521814819 if mname == "inspect"
15219- @needs_string_helpers = 1
1522014820 return "sp_str_concat(\":\", sp_sym_to_s(" + rc + "))"
1522114821 end
1522214822 if mname == "length" || mname == "size"
@@ -15276,7 +14876,6 @@
1527614876
1527714877 def compile_int_method_expr(nid, mname, rc)
1527814878 if mname == "to_s"
15279- @needs_string_helpers = 1
1528014879 if @nd_arguments[nid] >= 0
1528114880 aargs = get_args(@nd_arguments[nid])
1528214881 if aargs.length > 0
@@ -15286,7 +14885,6 @@
1528614885 return "sp_int_to_s(" + rc + ")"
1528714886 end
1528814887 if mname == "inspect"
15289- @needs_string_helpers = 1
1529014888 return "sp_int_to_s(" + rc + ")"
1529114889 end
1529214890 if mname == "digits"
@@ -15354,7 +14952,6 @@
1535414952 return "TRUE"
1535514953 end
1535614954 if mname == "chr"
15357- @needs_string_helpers = 1
1535814955 return "sp_int_chr(" + rc + ")"
1535914956 end
1536014957 if mname == "succ" || mname == "next"
@@ -15371,11 +14968,9 @@
1537114968 return rc
1537214969 end
1537314970 if mname == "to_s"
15374- @needs_string_helpers = 1
1537514971 return "sp_float_to_s(" + rc + ")"
1537614972 end
1537714973 if mname == "inspect"
15378- @needs_string_helpers = 1
1537914974 return "sp_float_inspect(" + rc + ")"
1538014975 end
1538114976 if mname == "to_i"
@@ -15447,7 +15042,6 @@
1544715042 end
1544815043 # zip without block: return array of pairs/tuples
1544915044 if mname == "zip" && @nd_block[nid] < 0
15450- @needs_ptr_array = 1
1545115045 @needs_gc = 1
1545215046 pfx_recv = array_c_prefix(recv_type)
1545315047 args_id = @nd_arguments[nid]
@@ -15839,7 +15433,6 @@
1583915433 return "sp_IntArray_uniq(" + rc + ")"
1584015434 end
1584115435 if mname == "join"
15842- @needs_string_helpers = 1
1584315436 jarg = compile_arg0(nid)
1584415437 if jarg == "0"
1584515438 jarg = "\"\""
@@ -16050,7 +15643,6 @@
1605015643 return "sp_StrArray_get(" + rc + ", sp_StrArray_length(" + rc + ") - 1)"
1605115644 end
1605215645 if mname == "join"
16053- @needs_string_helpers = 1
1605415646 jarg = compile_arg0(nid)
1605515647 if jarg == "0"
1605615648 jarg = "\"\""
@@ -16298,7 +15890,6 @@
1629815890 if mname == "to_a"
1629915891 tt = "tuple:string,int"
1630015892 register_tuple_type(tt)
16301- @needs_ptr_array = 1
1630215893 @needs_gc = 1
1630315894 tname = tuple_c_name(tt)
1630415895 tmp = new_temp
@@ -16404,7 +15995,6 @@
1640415995 if mname == "to_a"
1640515996 tt = "tuple:string,string"
1640615997 register_tuple_type(tt)
16407- @needs_ptr_array = 1
1640815998 @needs_gc = 1
1640915999 tname = tuple_c_name(tt)
1641016000 tmp = new_temp
@@ -16574,19 +16164,15 @@
1657416164 # File operations
1657516165 if rcname == "File"
1657616166 if mname == "read"
16577- @needs_file_io = 1
1657816167 return "sp_file_read(" + compile_arg0(nid) + ")"
1657916168 end
1658016169 if mname == "exist?"
16581- @needs_file_io = 1
1658216170 return "sp_file_exist(" + compile_arg0(nid) + ")"
1658316171 end
1658416172 if mname == "delete"
16585- @needs_file_io = 1
1658616173 return "(sp_file_delete(" + compile_arg0(nid) + "), 0)"
1658716174 end
1658816175 if mname == "join"
16589- @needs_string_helpers = 1
1659016176 args_id = @nd_arguments[nid]
1659116177 if args_id >= 0
1659216178 arg_ids = get_args(args_id)
@@ -16597,7 +16183,6 @@
1659716183 return "\"\""
1659816184 end
1659916185 if mname == "basename"
16600- @needs_string_helpers = 1
1660116186 return "sp_file_basename(" + compile_arg0(nid) + ")"
1660216187 end
1660316188 end
@@ -17815,7 +17400,6 @@
1781517400 return tmp
1781617401 end
1781717402 if is_ptr_array_type(arr_type) == 1
17818- @needs_ptr_array = 1
1781917403 tmp = new_temp
1782017404 emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
1782117405 k = 0
@@ -17857,7 +17441,6 @@
1785717441 end
1785817442 if ht == "str_str_hash"
1785917443 @needs_str_str_hash = 1
17860- @needs_string_helpers = 1
1786117444 tmp = new_temp
1786217445 emit(" sp_StrStrHash *" + tmp + " = sp_StrStrHash_new();")
1786317446 elems.each { |el|
@@ -17903,7 +17486,6 @@
1790317486 return tmp
1790417487 end
1790517488 if ht == "sym_poly_hash"
17906- @needs_sym_poly_hash = 1
1790717489 @needs_rb_value = 1
1790817490 tmp = new_temp
1790917491 emit(" sp_SymPolyHash *" + tmp + " = sp_SymPolyHash_new();")
@@ -17915,7 +17497,6 @@
1791517497 return tmp
1791617498 end
1791717499 if ht == "str_poly_hash"
17918- @needs_str_poly_hash = 1
1791917500 @needs_rb_value = 1
1792017501 tmp = new_temp
1792117502 emit(" sp_StrPolyHash *" + tmp + " = sp_StrPolyHash_new();")
@@ -18005,7 +17586,6 @@
1800517586 @needs_gc = 1
1800617587 emit(" " + vref + " = sp_IntArray_new();")
1800717588 else
18008- @needs_ptr_array = 1
1800917589 @needs_gc = 1
1801017590 emit(" " + vref + " = sp_PtrArray_new();")
1801117591 end
@@ -18104,7 +17684,6 @@
1810417684 end
1810517685 if op == "+"
1810617686 if vt == "string" && infer_type(@nd_expression[nid]) == "string"
18107- @needs_string_helpers = 1
1810817687 emit(" " + vref + " = sp_str_concat(" + vref + ", " + val + ");")
1810917688 else
1811017689 emit(" " + vref + " += " + val + ";")
@@ -19061,7 +18640,6 @@
1906118640 if @nd_type[recv] == "ConstantReadNode"
1906218641 if @nd_name[recv] == "File"
1906318642 if @nd_block[nid] >= 0
19064- @needs_file_io = 1
1906518643 args_id = @nd_arguments[nid]
1906618644 path_expr = "\"\""
1906718645 mode_expr = "\"r\""
@@ -19204,7 +18782,6 @@
1920418782 return 1
1920518783 end
1920618784 if rt == "string"
19207- @needs_string_helpers = 1
1920818785 rc = compile_expr_gc_rooted(recv)
1920918786 val = compile_arg0(nid)
1921018787 # If receiver is a local variable, reassign
@@ -19527,7 +19104,6 @@
1952719104 bp = "_c"
1952819105 end
1952919106 declare_var(bp, "string")
19530- @needs_string_helpers = 1
1953119107 tmp = new_temp
1953219108 src = rc
1953319109 if rt == "mutable_str"
@@ -19856,7 +19432,6 @@
1985619432 # system
1985719433 if mname == "system"
1985819434 if recv < 0
19859- @needs_file_io = 1
1986019435 emit(" fflush(stdout);")
1986119436 emit(" sp_last_status = system(" + compile_arg0(nid) + ");")
1986219437 return 1
@@ -19892,7 +19467,6 @@
1989219467 rcname = @nd_name[recv]
1989319468 if rcname == "File"
1989419469 if mname == "write"
19895- @needs_file_io = 1
1989619470 args_id = @nd_arguments[nid]
1989719471 arg_ids = []
1989819472 if args_id >= 0
@@ -19910,7 +19484,6 @@
1991019484 return 1
1991119485 end
1991219486 if mname == "delete"
19913- @needs_file_io = 1
1991419487 emit(" sp_file_delete(" + compile_arg0(nid) + ");")
1991519488 return 1
1991619489 end
@@ -21237,7 +20810,6 @@
2123720810 if op == "+"
2123820811 tt = new_temp
2123920812 ti = new_temp
21240- @needs_string_helpers = 1
2124120813 emit(" { sp_IntStrHash *" + tt + " = " + rc + "; mrb_int " + ti + " = " + idx +
2124220814 "; sp_IntStrHash_set(" + tt + ", " + ti +
2124320815 ", sp_str_concat(sp_IntStrHash_get(" + tt + ", " + ti + "), " + val + ")); }")
@@ -21260,23 +20832,18 @@
2126020832 # callers can fall back to their previous behaviour.
2126120833 def compile_inspect_for(at, val)
2126220834 if at == "int"
21263- @needs_string_helpers = 1
2126420835 return "sp_int_to_s(" + val + ")"
2126520836 end
2126620837 if at == "float"
21267- @needs_string_helpers = 1
2126820838 return "sp_float_inspect(" + val + ")"
2126920839 end
2127020840 if at == "string" || at == "string?"
21271- @needs_string_helpers = 1
2127220841 return "sp_str_inspect(" + val + ")"
2127320842 end
2127420843 if at == "mutable_str"
21275- @needs_string_helpers = 1
2127620844 return "sp_str_inspect(" + val + "->data)"
2127720845 end
2127820846 if at == "symbol"
21279- @needs_string_helpers = 1
2128020847 return "sp_str_concat(\":\", sp_sym_to_s(" + val + "))"
2128120848 end
2128220849 if at == "bool"
@@ -21287,32 +20854,26 @@
2128720854 end
2128820855 if at == "int_array"
2128920856 @needs_int_array = 1
21290- @needs_string_helpers = 1
2129120857 return "sp_IntArray_inspect(" + val + ")"
2129220858 end
2129320859 if at == "float_array"
2129420860 @needs_float_array = 1
21295- @needs_string_helpers = 1
2129620861 return "sp_FloatArray_inspect(" + val + ")"
2129720862 end
2129820863 if at == "str_array"
2129920864 @needs_str_array = 1
21300- @needs_string_helpers = 1
2130120865 return "sp_StrArray_inspect(" + val + ")"
2130220866 end
2130320867 if at == "sym_array"
2130420868 @needs_int_array = 1
21305- @needs_string_helpers = 1
2130620869 return "sp_SymArray_inspect(" + val + ")"
2130720870 end
2130820871 if at == "poly_array"
2130920872 @needs_rb_value = 1
21310- @needs_string_helpers = 1
2131120873 return "sp_PolyArray_inspect(" + val + ")"
2131220874 end
2131320875 if at == "poly"
2131420876 @needs_rb_value = 1
21315- @needs_string_helpers = 1
2131620877 return "sp_poly_inspect(" + val + ")"
2131720878 end
2131820879 ""
@@ -22843,7 +22404,6 @@
2284322404 end
2284422405
2284522406 def compile_sprintf_call(nid)
22846- @needs_string_helpers = 1
2284722407 args_id = @nd_arguments[nid]
2284822408 if args_id < 0
2284922409 return "\"\""
@@ -23876,7 +23436,6 @@
2387623436 end
2387723437 if lmname == "proc"
2387823438 if return_type != "void"
23879- @needs_proc = 1
2388023439 val = compile_proc_literal(last)
2388123440 emit(" return " + val + ";")
2388223441 return