Commit 44d9090a

44d9090a484dMerged

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

Description

fix: benchmark improvements - global constants, yield params, clamp conflict

  • Make constants file-scope globals (accessible from all functions)
  • Fix yield_params_suffix for zero-param methods (was generating "voidvoid")
  • Rename sp_clamp to sp_int_clamp to avoid conflicts with user methods
  • Fix sp_str_split to return empty array for empty strings
  • Fix << operator to always enable string helpers
  • Fix ARGV[] to infer "string" return type

Benchmarks: 26/39 OK (up from 24), 1 CC error (down from 5) Tests: 58/60 pass (unchanged)

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

Details

Commit
44d9090a484d
Parent
040080709c79

spinel_codegen.rbmodifiedView file

@@ -3986,6 +3986,7 @@
39863986 @needs_string_helpers = 1
39873987 end
39883988 if mname == "<<"
3989+ @needs_string_helpers = 1
39893990 if @nd_receiver[nid] >= 0
39903991 rt = infer_type(@nd_receiver[nid])
39913992 if rt == "string"
@@ -4828,6 +4829,7 @@
48284829 end
48294830 emit_class_structs
48304831 emit_forward_decls
4832+ emit_global_constants
48314833 emit_class_methods
48324834 emit_toplevel_methods
48334835 emit_main
@@ -4867,7 +4869,7 @@
48674869 emit_raw("}")
48684870 emit_raw("")
48694871 emit_raw("static mrb_int sp_gcd(mrb_int a,mrb_int b){if(a<0)a=-a;if(b<0)b=-b;while(b){mrb_int t=b;b=a%b;a=t;}return a;}")
4870- emit_raw("static mrb_int sp_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}")
4872+ emit_raw("static mrb_int sp_int_clamp(mrb_int v,mrb_int lo,mrb_int hi){return v<lo?lo:v>hi?hi:v;}")
48714873 emit_raw("static const char*sp_int_chr(mrb_int n){char*s=(char*)malloc(2);s[0]=(char)n;s[1]=0;return s;}")
48724874 emit_raw("typedef struct{mrb_int first;mrb_int last;}sp_Range;")
48734875 emit_raw("static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}")
@@ -5109,6 +5111,19 @@
51095111 end
51105112
51115113 # ---- Struct emission ----
5114+ def emit_global_constants
5115+ # Emit file-scope constant declarations (initialized in main)
5116+ i = 0
5117+ while i < @const_names.length
5118+ ctp = c_type(@const_types[i])
5119+ emit_raw("static " + ctp + " cst_" + @const_names[i] + " = " + c_default_val(@const_types[i]) + ";")
5120+ i = i + 1
5121+ end
5122+ if @const_names.length > 0
5123+ emit_raw("")
5124+ end
5125+ end
5126+
51125127 def emit_class_structs
51135128 # Forward declare typedefs
51145129 i = 0
@@ -5279,8 +5294,8 @@
52795294 end
52805295
52815296 def yield_params_suffix(mi)
5282- pnames = @meth_param_names[mi].split(",")
5283- if pnames.length == 0
5297+ pd = method_params_decl(mi)
5298+ if pd == ""
52845299 return "void (*_block)(mrb_int, void*), void *_benv"
52855300 end
52865301 return ", void (*_block)(mrb_int, void*), void *_benv"
@@ -5346,6 +5361,9 @@
53465361 return result
53475362 end
53485363 if pnames.length == 0
5364+ if @meth_has_yield[mi] == 1
5365+ return ""
5366+ end
53495367 return "void"
53505368 end
53515369 result = ""
@@ -6387,12 +6405,11 @@
63876405 j = j + 1
63886406 end
63896407
6390- # Constants
6408+ # Constants (initialize global declarations)
63916409 i = 0
63926410 while i < @const_names.length
6393- ctp = c_type(@const_types[i])
63946411 val = compile_expr(@const_expr_ids[i])
6395- emit(" " + ctp + " cst_" + @const_names[i] + " = " + val + ";")
6412+ emit(" cst_" + @const_names[i] + " = " + val + ";")
63966413 i = i + 1
63976414 end
63986415
@@ -7676,7 +7693,7 @@
76767693 if args_id >= 0
76777694 a = get_args(args_id)
76787695 if a.length >= 2
7679- return "sp_clamp(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
7696+ return "sp_int_clamp(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
76807697 end
76817698 end
76827699 end