Commit 87918e3f

87918e3ff610Merged

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

Description

fix: pass bm_misc2, bm_stdlib and more - add format, sprintf, join, uniq

  • Add format/sprintf as sp_sprintf calls with string return type
  • Add inline rescue (RescueModifierNode) expression handling
  • Add raise handling in compile_call_expr (not just compile_call_stmt)
  • Add Array#join with IntArray join helper
  • Add Array#uniq with IntArray uniq helper
  • Add srand/rand support
  • Add ARGV.length and ARGV[] support
  • Add putc support
  • Fix two-pass local variable scanning in emit_main for dependent types
  • Add p() as alias for puts

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

Details

Commit
87918e3ff610
Parent
e49f6dd58354

spinel_codegen.rbmodifiedView file

@@ -772,6 +772,9 @@
772772 return "int"
773773 end
774774 if t == "ConstantReadNode"
775+ if @nd_name[nid] == "ARGV"
776+ return "argv"
777+ end
775778 ci = find_const_idx(@nd_name[nid])
776779 if ci >= 0
777780 return @const_types[ci]
@@ -1042,6 +1045,21 @@
10421045 if mname == "__method__"
10431046 return "string"
10441047 end
1048+ if mname == "join"
1049+ return "string"
1050+ end
1051+ if mname == "uniq"
1052+ if recv >= 0
1053+ return infer_type(recv)
1054+ end
1055+ return "int_array"
1056+ end
1057+ if mname == "format"
1058+ return "string"
1059+ end
1060+ if mname == "sprintf"
1061+ return "string"
1062+ end
10451063 if mname == "positive?"
10461064 return "bool"
10471065 end
@@ -4010,6 +4028,8 @@
40104028 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;}")
40114029 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;}")
40124030 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;}")
4031+ 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;}")
4032+ 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;}")
40134033 emit_raw("")
40144034 end
40154035
@@ -5244,13 +5264,53 @@
52445264 i = i + 1
52455265 end
52465266
5267+ # Declare vars for second pass to resolve dependent types
5268+ j = 0
5269+ while j < lnames.length
5270+ declare_var(lnames[j], ltypes[j])
5271+ j = j + 1
5272+ end
5273+ # Second pass with vars in scope
5274+ lnames2 = []
5275+ ltypes2 = []
5276+ i = 0
5277+ while i < stmts.length
5278+ sid = stmts[i]
5279+ if @nd_type[sid] != "DefNode"
5280+ if @nd_type[sid] != "ClassNode"
5281+ if @nd_type[sid] != "ConstantWriteNode"
5282+ if @nd_type[sid] != "ModuleNode"
5283+ scan_locals(sid, lnames2, ltypes2, empty_params)
5284+ end
5285+ end
5286+ end
5287+ end
5288+ i = i + 1
5289+ end
5290+ # Update types that improved in second pass
5291+ j = 0
5292+ while j < lnames2.length
5293+ k = 0
5294+ while k < lnames.length
5295+ if lnames[k] == lnames2[j]
5296+ if ltypes[k] == "int"
5297+ if ltypes2[j] != "int"
5298+ ltypes[k] = ltypes2[j]
5299+ set_var_type(lnames[k], ltypes2[j])
5300+ end
5301+ end
5302+ end
5303+ k = k + 1
5304+ end
5305+ j = j + 1
5306+ end
5307+
52475308 vol = ""
52485309 if @needs_setjmp == 1
52495310 vol = "volatile "
52505311 end
52515312 j = 0
52525313 while j < lnames.length
5253- declare_var(lnames[j], ltypes[j])
52545314 ctp = c_type(ltypes[j])
52555315 if type_is_pointer(ltypes[j]) == 1
52565316 emit(" " + vol + ctp + "lv_" + lnames[j] + " = NULL;")
@@ -5333,6 +5393,9 @@
53335393 return "self->" + sanitize_ivar(@nd_name[nid])
53345394 end
53355395 if t == "ConstantReadNode"
5396+ if @nd_name[nid] == "ARGV"
5397+ return "sp_argv"
5398+ end
53365399 ci = find_const_idx(@nd_name[nid])
53375400 if ci >= 0
53385401 return "cst_" + @nd_name[nid]
@@ -5403,6 +5466,21 @@
54035466 if t == "DefinedNode"
54045467 return "\"expression\""
54055468 end
5469+ if t == "RescueModifierNode"
5470+ @needs_setjmp = 1
5471+ tmp = new_temp
5472+ rt = infer_type(@nd_else_clause[nid])
5473+ emit(" " + c_type(rt) + " " + tmp + " = " + c_default_val(rt) + ";")
5474+ emit(" sp_exc_top++;")
5475+ emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")
5476+ emit(" " + tmp + " = " + compile_expr(@nd_expression[nid]) + ";")
5477+ emit(" sp_exc_top--;")
5478+ emit(" } else {")
5479+ emit(" sp_exc_top--;")
5480+ emit(" " + tmp + " = " + compile_expr(@nd_else_clause[nid]) + ";")
5481+ emit(" }")
5482+ return tmp
5483+ end
54065484 if t == "XStringNode"
54075485 @needs_file_io = 1
54085486 return "sp_backtick(" + c_string_literal(@nd_content[nid]) + ")"
@@ -5646,6 +5724,58 @@
56465724 if mname == "__method__"
56475725 return "\"" + @current_method_name + "\""
56485726 end
5727+ if mname == "p"
5728+ # p(val) -> puts(val.inspect) - for simplicity, same as puts
5729+ compile_puts(nid)
5730+ return "0"
5731+ end
5732+ if mname == "srand"
5733+ emit(" srand((unsigned)" + compile_arg0(nid) + ");")
5734+ return "0"
5735+ end
5736+ if mname == "rand"
5737+ args_id = @nd_arguments[nid]
5738+ if args_id >= 0
5739+ return "((mrb_int)(rand() % (int)" + compile_arg0(nid) + "))"
5740+ end
5741+ return "((mrb_int)rand())"
5742+ end
5743+ if mname == "raise"
5744+ @needs_setjmp = 1
5745+ args_id = @nd_arguments[nid]
5746+ if args_id >= 0
5747+ arg_ids = get_args(args_id)
5748+ if arg_ids.length >= 1
5749+ emit(" sp_raise(" + compile_expr(arg_ids[0]) + ");")
5750+ end
5751+ else
5752+ emit(" sp_raise(\"RuntimeError\");")
5753+ end
5754+ return "0"
5755+ end
5756+ if mname == "format"
5757+ @needs_string_helpers = 1
5758+ return compile_sprintf_call(nid)
5759+ end
5760+ if mname == "sprintf"
5761+ @needs_string_helpers = 1
5762+ return compile_sprintf_call(nid)
5763+ end
5764+ if mname == "putc"
5765+ args_id = @nd_arguments[nid]
5766+ if args_id >= 0
5767+ arg_ids = get_args(args_id)
5768+ if arg_ids.length > 0
5769+ at = infer_type(arg_ids[0])
5770+ if at == "int"
5771+ return "(putchar((char)" + compile_expr(arg_ids[0]) + "), 0)"
5772+ else
5773+ return "(putchar(" + compile_expr(arg_ids[0]) + "[0]), 0)"
5774+ end
5775+ end
5776+ end
5777+ return "0"
5778+ end
56495779 mi = find_method_idx(mname)
56505780 if mi >= 0
56515781 yargs = ""
@@ -6116,6 +6246,16 @@
61166246 if mname == "to_a"
61176247 return "sp_IntArray_dup(" + rc + ")"
61186248 end
6249+ if mname == "uniq"
6250+ return "sp_IntArray_uniq(" + rc + ")"
6251+ end
6252+ if mname == "join"
6253+ @needs_string_helpers = 1
6254+ return "sp_IntArray_join(" + rc + ", " + compile_arg0(nid) + ")"
6255+ end
6256+ if mname == "reverse"
6257+ return "({ sp_IntArray *_r = sp_IntArray_dup(" + rc + "); sp_IntArray_reverse_bang(_r); _r; })"
6258+ end
61196259 end
61206260 if recv_type == "str_array"
61216261 if mname == "length"
@@ -6195,6 +6335,18 @@
61956335 end
61966336 end
61976337
6338+ # ARGV methods
6339+ if @nd_type[recv] == "ConstantReadNode"
6340+ if @nd_name[recv] == "ARGV"
6341+ if mname == "length"
6342+ return "sp_argv.len"
6343+ end
6344+ if mname == "[]"
6345+ return "sp_argv.data[(int)" + compile_arg0(nid) + "]"
6346+ end
6347+ end
6348+ end
6349+
61986350 # Math
61996351 if @nd_type[recv] == "ConstantReadNode"
62006352 rcname = @nd_name[recv]
@@ -8091,6 +8243,32 @@
80918243 end
80928244 end
80938245
8246+ def compile_sprintf_call(nid)
8247+ @needs_string_helpers = 1
8248+ args_id = @nd_arguments[nid]
8249+ if args_id < 0
8250+ return "\"\""
8251+ end
8252+ arg_ids = get_args(args_id)
8253+ if arg_ids.length == 0
8254+ return "\"\""
8255+ end
8256+ # First arg is format string, rest are values
8257+ fmt = compile_expr(arg_ids[0])
8258+ result = "sp_sprintf(" + fmt
8259+ k = 1
8260+ while k < arg_ids.length
8261+ at = infer_type(arg_ids[k])
8262+ if at == "float"
8263+ result = result + ", " + compile_expr(arg_ids[k])
8264+ else
8265+ result = result + ", " + compile_expr(arg_ids[k])
8266+ end
8267+ k = k + 1
8268+ end
8269+ result + ")"
8270+ end
8271+
80948272 def compile_catch_expr(nid)
80958273 @needs_setjmp = 1
80968274 tag = compile_arg0(nid)