Commit 233ff34b

233ff34bd1e9Merged

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

Description

fix: add case-as-expr, Array.new(n,val), sort_by/min_by/max_by/count, float type propagation

Fixes bm_lrama_features (bare case, Array.new(n,val), compact/flatten/unshift), bm_mutable_str2 (replace, clear), bm_array3 (count/min_by/max_by/sort_by with blocks, StrArray count), bm_mandel_term (float promotion through +=, unary -@).

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

Details

Commit
233ff34bd1e9
Parent
a03224dd99b3

spinel_codegen.rbmodifiedView file

@@ -1227,6 +1227,51 @@
12271227 end
12281228 return "int_array"
12291229 end
1230+ if mname == "compact"
1231+ if recv >= 0
1232+ return infer_type(recv)
1233+ end
1234+ return "int_array"
1235+ end
1236+ if mname == "flatten"
1237+ if recv >= 0
1238+ return infer_type(recv)
1239+ end
1240+ return "int_array"
1241+ end
1242+ if mname == "sort_by"
1243+ if recv >= 0
1244+ return infer_type(recv)
1245+ end
1246+ return "int_array"
1247+ end
1248+ if mname == "min_by"
1249+ return "int"
1250+ end
1251+ if mname == "max_by"
1252+ return "int"
1253+ end
1254+ if mname == "unshift"
1255+ if recv >= 0
1256+ return infer_type(recv)
1257+ end
1258+ return "int_array"
1259+ end
1260+ if mname == "merge"
1261+ if recv >= 0
1262+ return infer_type(recv)
1263+ end
1264+ return "str_int_hash"
1265+ end
1266+ if mname == "transform_values"
1267+ if recv >= 0
1268+ return infer_type(recv)
1269+ end
1270+ return "str_int_hash"
1271+ end
1272+ if mname == "zip"
1273+ return "int_array"
1274+ end
12301275 if mname == "reject"
12311276 if recv >= 0
12321277 return infer_type(recv)
@@ -3978,9 +4023,45 @@
39784023 end
39794024 end
39804025
4026+ def infer_function_body_call_types
4027+ # Scan each top-level method body for calls to other functions
4028+ # and infer param types from local variable types in those bodies
4029+ mi = 0
4030+ while mi < @meth_names.length
4031+ bid = @meth_body_ids[mi]
4032+ if bid >= 0
4033+ # Build local scope for this function
4034+ push_scope
4035+ pnames = @meth_param_names[mi].split(",")
4036+ ptypes = @meth_param_types[mi].split(",")
4037+ pk = 0
4038+ while pk < pnames.length
4039+ if pnames[pk] != ""
4040+ declare_var(pnames[pk], ptypes[pk])
4041+ end
4042+ pk = pk + 1
4043+ end
4044+ # Scan locals in the body
4045+ lnames = "".split(",")
4046+ ltypes = "".split(",")
4047+ scan_locals(bid, lnames, ltypes, pnames)
4048+ lk = 0
4049+ while lk < lnames.length
4050+ declare_var(lnames[lk], ltypes[lk])
4051+ lk = lk + 1
4052+ end
4053+ # Now scan for calls within this function body
4054+ scan_new_calls(bid)
4055+ pop_scope
4056+ end
4057+ mi = mi + 1
4058+ end
4059+ end
4060+
39814061 def compile
39824062 collect_all
39834063 infer_main_call_types
4064+ infer_function_body_call_types
39844065 detect_poly_params
39854066 detect_poly_locals
39864067 # Re-infer return types after main call type fixes
@@ -4137,6 +4218,7 @@
41374218 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;}")
41384219 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;}")
41394220 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;}")
4221+ 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++;}}")
41404222 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;}")
41414223 emit_raw("")
41424224 end
@@ -5140,10 +5222,30 @@
51405222 end
51415223 if @nd_type[nid] == "LocalVariableOperatorWriteNode"
51425224 lname = @nd_name[nid]
5225+ rhs_type = infer_type(@nd_expression[nid])
5226+ vtype = "int"
5227+ if rhs_type == "float"
5228+ vtype = "float"
5229+ end
51435230 if not_in(lname, names) == 1
51445231 if not_in(lname, params) == 1
51455232 names.push(lname)
5146- types.push("int")
5233+ types.push(vtype)
5234+ end
5235+ else
5236+ if not_in(lname, params) == 1
5237+ # If RHS is float, promote to float
5238+ if rhs_type == "float"
5239+ ki = 0
5240+ while ki < names.length
5241+ if names[ki] == lname
5242+ if types[ki] == "int"
5243+ types[ki] = "float"
5244+ end
5245+ end
5246+ ki = ki + 1
5247+ end
5248+ end
51475249 end
51485250 end
51495251 end
@@ -5647,6 +5749,102 @@
56475749 end
56485750 return "0"
56495751 end
5752+ if t == "CaseNode"
5753+ # Case as expression: use a temp var and compile each branch as assignment
5754+ rt = infer_type(nid)
5755+ tmp = new_temp
5756+ emit(" " + c_type(rt) + " " + tmp + " = " + c_default_val(rt) + ";")
5757+ pred = @nd_predicate[nid]
5758+ if pred >= 0
5759+ pred_type = infer_type(pred)
5760+ pred_val = compile_expr(pred)
5761+ ptmp = new_temp
5762+ if pred_type == "string"
5763+ emit(" const char *" + ptmp + " = " + pred_val + ";")
5764+ else
5765+ emit(" mrb_int " + ptmp + " = " + pred_val + ";")
5766+ end
5767+ conds = parse_id_list(@nd_conditions[nid])
5768+ k = 0
5769+ while k < conds.length
5770+ wid = conds[k]
5771+ if @nd_type[wid] == "WhenNode"
5772+ kw = "if"
5773+ if k > 0
5774+ kw = "} else if"
5775+ end
5776+ cond_str = compile_when_conds(wid, ptmp, pred_type)
5777+ emit(" " + kw + " (" + cond_str + ") {")
5778+ wbody = @nd_body[wid]
5779+ if wbody >= 0
5780+ ws = get_stmts(wbody)
5781+ if ws.length > 0
5782+ i = 0
5783+ while i < ws.length - 1
5784+ compile_stmt(ws[i])
5785+ i = i + 1
5786+ end
5787+ emit(" " + tmp + " = " + compile_expr(ws[ws.length - 1]) + ";")
5788+ end
5789+ end
5790+ end
5791+ k = k + 1
5792+ end
5793+ else
5794+ # Bare case (no predicate)
5795+ conds = parse_id_list(@nd_conditions[nid])
5796+ k = 0
5797+ while k < conds.length
5798+ wid = conds[k]
5799+ if @nd_type[wid] == "WhenNode"
5800+ kw = "if"
5801+ if k > 0
5802+ kw = "} else if"
5803+ end
5804+ wconds = parse_id_list(@nd_conditions[wid])
5805+ cexpr = "0"
5806+ if wconds.length > 0
5807+ cexpr = compile_expr(wconds[0])
5808+ end
5809+ emit(" " + kw + " (" + cexpr + ") {")
5810+ wbody = @nd_body[wid]
5811+ if wbody >= 0
5812+ ws = get_stmts(wbody)
5813+ if ws.length > 0
5814+ i = 0
5815+ while i < ws.length - 1
5816+ compile_stmt(ws[i])
5817+ i = i + 1
5818+ end
5819+ emit(" " + tmp + " = " + compile_expr(ws[ws.length - 1]) + ";")
5820+ end
5821+ end
5822+ end
5823+ k = k + 1
5824+ end
5825+ end
5826+ ec = @nd_else_clause[nid]
5827+ if ec >= 0
5828+ emit(" } else {")
5829+ ebody = @nd_body[ec]
5830+ if ebody >= 0
5831+ es = get_stmts(ebody)
5832+ if es.length > 0
5833+ i = 0
5834+ while i < es.length - 1
5835+ compile_stmt(es[i])
5836+ i = i + 1
5837+ end
5838+ emit(" " + tmp + " = " + compile_expr(es[es.length - 1]) + ";")
5839+ end
5840+ end
5841+ end
5842+ conds2 = parse_id_list(@nd_conditions[nid])
5843+ if conds2.length > 0
5844+ emit(" }")
5845+ end
5846+ return tmp
5847+ end
56505848 "0"
56515849 end
56525850
@@ -6053,6 +6251,13 @@
60536251 if mname == "~"
60546252 return "(~" + compile_expr(recv) + ")"
60556253 end
6254+ if mname == "-@"
6255+ rt = infer_type(recv)
6256+ if rt == "float"
6257+ return "(-" + compile_expr(recv) + ")"
6258+ end
6259+ return "(-" + compile_expr(recv) + ")"
6260+ end
60566261
60576262 # .new
60586263 if mname == "new"
@@ -6061,11 +6266,38 @@
60616266 if cname == "Array"
60626267 @needs_int_array = 1
60636268 @needs_gc = 1
6269+ args_id = @nd_arguments[nid]
6270+ if args_id >= 0
6271+ aargs = get_args(args_id)
6272+ if aargs.length >= 2
6273+ # Array.new(n, val)
6274+ tmp = new_temp
6275+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
6276+ emit(" { mrb_int _n = " + compile_expr(aargs[0]) + "; mrb_int _v = " + compile_expr(aargs[1]) + "; for (mrb_int _i = 0; _i < _n; _i++) sp_IntArray_push(" + tmp + ", _v); }")
6277+ return tmp
6278+ end
6279+ end
60646280 return "sp_IntArray_new()"
60656281 end
60666282 if cname == "Hash"
60676283 @needs_str_int_hash = 1
60686284 @needs_gc = 1
6285+ args_id = @nd_arguments[nid]
6286+ if args_id >= 0
6287+ aargs = get_args(args_id)
6288+ if aargs.length >= 1
6289+ # Hash.new(default_val) - check type
6290+ defval = compile_expr(aargs[0])
6291+ dt = infer_type(aargs[0])
6292+ if dt == "string"
6293+ @needs_str_str_hash = 1
6294+ return "sp_StrStrHash_new()"
6295+ end
6296+ # Default is int - for now just return normal hash
6297+ # Default value is handled by the get function
6298+ return "sp_StrIntHash_new()"
6299+ end
6300+ end
60696301 return "sp_StrIntHash_new()"
60706302 end
60716303 ci = find_class_idx(cname)
@@ -6420,6 +6652,118 @@
64206652 if mname == "reverse"
64216653 return "({ sp_IntArray *_r = sp_IntArray_dup(" + rc + "); sp_IntArray_reverse_bang(_r); _r; })"
64226654 end
6655+ if mname == "compact"
6656+ return "sp_IntArray_dup(" + rc + ")"
6657+ end
6658+ if mname == "flatten"
6659+ return "sp_IntArray_dup(" + rc + ")"
6660+ end
6661+ if mname == "unshift"
6662+ return "(sp_IntArray_unshift(" + rc + ", " + compile_arg0(nid) + "), 0)"
6663+ end
6664+ if mname == "dup"
6665+ return "sp_IntArray_dup(" + rc + ")"
6666+ end
6667+ if mname == "count"
6668+ if @nd_block[nid] >= 0
6669+ # count with block
6670+ blk = @nd_block[nid]
6671+ bp = get_block_param(nid, 0)
6672+ tmp = new_temp
6673+ itmp = new_temp
6674+ emit(" mrb_int " + tmp + " = 0;")
6675+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {")
6676+ emit(" mrb_int lv_" + bp + " = sp_IntArray_get(" + rc + ", " + itmp + ");")
6677+ bbody = @nd_body[blk]
6678+ bexpr = "0"
6679+ if bbody >= 0
6680+ bs = get_stmts(bbody)
6681+ if bs.length > 0
6682+ bexpr = compile_expr(bs[bs.length - 1])
6683+ end
6684+ end
6685+ emit(" if (" + bexpr + ") " + tmp + "++;")
6686+ emit(" }")
6687+ return tmp
6688+ end
6689+ return "sp_IntArray_length(" + rc + ")"
6690+ end
6691+ if mname == "min_by"
6692+ if @nd_block[nid] >= 0
6693+ blk = @nd_block[nid]
6694+ bp = get_block_param(nid, 0)
6695+ tmp = new_temp
6696+ itmp = new_temp
6697+ emit(" mrb_int " + tmp + " = sp_IntArray_get(" + rc + ", 0);")
6698+ emit(" { mrb_int _best = INT64_MAX;")
6699+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {")
6700+ emit(" mrb_int lv_" + bp + " = sp_IntArray_get(" + rc + ", " + itmp + ");")
6701+ bbody = @nd_body[blk]
6702+ bexpr = "0"
6703+ if bbody >= 0
6704+ bs = get_stmts(bbody)
6705+ if bs.length > 0
6706+ bexpr = compile_expr(bs[bs.length - 1])
6707+ end
6708+ end
6709+ emit(" mrb_int _v = " + bexpr + ";")
6710+ emit(" if (_v < _best) { _best = _v; " + tmp + " = lv_" + bp + "; }")
6711+ emit(" } }")
6712+ return tmp
6713+ end
6714+ end
6715+ if mname == "max_by"
6716+ if @nd_block[nid] >= 0
6717+ blk = @nd_block[nid]
6718+ bp = get_block_param(nid, 0)
6719+ tmp = new_temp
6720+ itmp = new_temp
6721+ emit(" mrb_int " + tmp + " = sp_IntArray_get(" + rc + ", 0);")
6722+ emit(" { mrb_int _best = INT64_MIN;")
6723+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {")
6724+ emit(" mrb_int lv_" + bp + " = sp_IntArray_get(" + rc + ", " + itmp + ");")
6725+ bbody = @nd_body[blk]
6726+ bexpr = "0"
6727+ if bbody >= 0
6728+ bs = get_stmts(bbody)
6729+ if bs.length > 0
6730+ bexpr = compile_expr(bs[bs.length - 1])
6731+ end
6732+ end
6733+ emit(" mrb_int _v = " + bexpr + ";")
6734+ emit(" if (_v > _best) { _best = _v; " + tmp + " = lv_" + bp + "; }")
6735+ emit(" } }")
6736+ return tmp
6737+ end
6738+ end
6739+ if mname == "sort_by"
6740+ if @nd_block[nid] >= 0
6741+ blk = @nd_block[nid]
6742+ bp = get_block_param(nid, 0)
6743+ tmp = new_temp
6744+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_dup(" + rc + ");")
6745+ # Use bubble sort with block as key function
6746+ emit(" { mrb_int _n = " + tmp + "->len;")
6747+ emit(" for (mrb_int _i = 0; _i < _n - 1; _i++)")
6748+ emit(" for (mrb_int _j = 0; _j < _n - 1 - _i; _j++) {")
6749+ emit(" mrb_int lv_" + bp + " = " + tmp + "->data[" + tmp + "->start + _j];")
6750+ bbody = @nd_body[blk]
6751+ bexpr = "0"
6752+ if bbody >= 0
6753+ bs = get_stmts(bbody)
6754+ if bs.length > 0
6755+ bexpr = compile_expr(bs[bs.length - 1])
6756+ end
6757+ end
6758+ emit(" mrb_int _ka = " + bexpr + ";")
6759+ emit(" lv_" + bp + " = " + tmp + "->data[" + tmp + "->start + _j + 1];")
6760+ emit(" mrb_int _kb = " + bexpr + ";")
6761+ emit(" if (_ka > _kb) { mrb_int _t = " + tmp + "->data[" + tmp + "->start + _j]; " + tmp + "->data[" + tmp + "->start + _j] = " + tmp + "->data[" + tmp + "->start + _j + 1]; " + tmp + "->data[" + tmp + "->start + _j + 1] = _t; }")
6762+ emit(" }")
6763+ emit(" }")
6764+ return tmp
6765+ end
6766+ end
64236767 end
64246768 if recv_type == "str_array"
64256769 if mname == "length"
@@ -6450,6 +6794,30 @@
64506794 if mname == "include?"
64516795 return "sp_StrArray_include(" + rc + ", " + compile_arg0(nid) + ")"
64526796 end
6797+ if mname == "count"
6798+ if @nd_block[nid] >= 0
6799+ blk = @nd_block[nid]
6800+ bp = get_block_param(nid, 0)
6801+ declare_var(bp, "string")
6802+ tmp = new_temp
6803+ itmp = new_temp
6804+ emit(" mrb_int " + tmp + " = 0;")
6805+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_StrArray_length(" + rc + "); " + itmp + "++) {")
6806+ emit(" const char *lv_" + bp + " = sp_StrArray_get(" + rc + ", " + itmp + ");")
6807+ bbody = @nd_body[blk]
6808+ bexpr = "0"
6809+ if bbody >= 0
6810+ bs = get_stmts(bbody)
6811+ if bs.length > 0
6812+ bexpr = compile_expr(bs[bs.length - 1])
6813+ end
6814+ end
6815+ emit(" if (" + bexpr + ") " + tmp + "++;")
6816+ emit(" }")
6817+ return tmp
6818+ end
6819+ return "sp_StrArray_length(" + rc + ")"
6820+ end
64536821 end
64546822
64556823 # Hash methods
@@ -7760,6 +8128,41 @@
77608128 end
77618129 end
77628130
8131+ # replace on string (mutating reassign)
8132+ if mname == "replace"
8133+ if recv >= 0
8134+ rt = infer_type(recv)
8135+ if rt == "string"
8136+ val = compile_arg0(nid)
8137+ if @nd_type[recv] == "LocalVariableReadNode"
8138+ emit(" lv_" + @nd_name[recv] + " = " + val + ";")
8139+ return
8140+ end
8141+ if @nd_type[recv] == "InstanceVariableReadNode"
8142+ emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = " + val + ";")
8143+ return
8144+ end
8145+ end
8146+ end
8147+ end
8148+
8149+ # clear on string (set to empty)
8150+ if mname == "clear"
8151+ if recv >= 0
8152+ rt = infer_type(recv)
8153+ if rt == "string"
8154+ if @nd_type[recv] == "LocalVariableReadNode"
8155+ emit(" lv_" + @nd_name[recv] + " = \"\";")
8156+ return
8157+ end
8158+ if @nd_type[recv] == "InstanceVariableReadNode"
8159+ emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = \"\";")
8160+ return
8161+ end
8162+ end
8163+ end
8164+ end
8165+
77638166 # push
77648167 if mname == "push"
77658168 if recv >= 0