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
end774774
if t == "ConstantReadNode"775+
if @nd_name[nid] == "ARGV"776+
return "argv"777+
end775778
ci = find_const_idx(@nd_name[nid])776779
if ci >= 0777780
return @const_types[ci]@@ -1042,6 +1045,21 @@
10421045
if mname == "__method__"10431046
return "string"10441047
end1048+
if mname == "join"1049+
return "string"1050+
end1051+
if mname == "uniq"1052+
if recv >= 01053+
return infer_type(recv)1054+
end1055+
return "int_array"1056+
end1057+
if mname == "format"1058+
return "string"1059+
end1060+
if mname == "sprintf"1061+
return "string"1062+
end10451063
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
end40154035
@@ -5244,13 +5264,53 @@
52445264
i = i + 152455265
end52465266
5267+
# Declare vars for second pass to resolve dependent types5268+
j = 05269+
while j < lnames.length5270+
declare_var(lnames[j], ltypes[j])5271+
j = j + 15272+
end5273+
# Second pass with vars in scope5274+
lnames2 = []5275+
ltypes2 = []5276+
i = 05277+
while i < stmts.length5278+
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+
end5285+
end5286+
end5287+
end5288+
i = i + 15289+
end5290+
# Update types that improved in second pass5291+
j = 05292+
while j < lnames2.length5293+
k = 05294+
while k < lnames.length5295+
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+
end5301+
end5302+
end5303+
k = k + 15304+
end5305+
j = j + 15306+
end5307+
52475308
vol = ""52485309
if @needs_setjmp == 152495310
vol = "volatile "52505311
end52515312
j = 052525313
while j < lnames.length5253-
declare_var(lnames[j], ltypes[j])52545314
ctp = c_type(ltypes[j])52555315
if type_is_pointer(ltypes[j]) == 152565316
emit(" " + vol + ctp + "lv_" + lnames[j] + " = NULL;")@@ -5333,6 +5393,9 @@
53335393
return "self->" + sanitize_ivar(@nd_name[nid])53345394
end53355395
if t == "ConstantReadNode"5396+
if @nd_name[nid] == "ARGV"5397+
return "sp_argv"5398+
end53365399
ci = find_const_idx(@nd_name[nid])53375400
if ci >= 053385401
return "cst_" + @nd_name[nid]@@ -5403,6 +5466,21 @@
54035466
if t == "DefinedNode"54045467
return "\"expression\""54055468
end5469+
if t == "RescueModifierNode"5470+
@needs_setjmp = 15471+
tmp = new_temp5472+
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 tmp5483+
end54065484
if t == "XStringNode"54075485
@needs_file_io = 154085486
return "sp_backtick(" + c_string_literal(@nd_content[nid]) + ")"@@ -5646,6 +5724,58 @@
56465724
if mname == "__method__"56475725
return "\"" + @current_method_name + "\""56485726
end5727+
if mname == "p"5728+
# p(val) -> puts(val.inspect) - for simplicity, same as puts5729+
compile_puts(nid)5730+
return "0"5731+
end5732+
if mname == "srand"5733+
emit(" srand((unsigned)" + compile_arg0(nid) + ");")5734+
return "0"5735+
end5736+
if mname == "rand"5737+
args_id = @nd_arguments[nid]5738+
if args_id >= 05739+
return "((mrb_int)(rand() % (int)" + compile_arg0(nid) + "))"5740+
end5741+
return "((mrb_int)rand())"5742+
end5743+
if mname == "raise"5744+
@needs_setjmp = 15745+
args_id = @nd_arguments[nid]5746+
if args_id >= 05747+
arg_ids = get_args(args_id)5748+
if arg_ids.length >= 15749+
emit(" sp_raise(" + compile_expr(arg_ids[0]) + ");")5750+
end5751+
else5752+
emit(" sp_raise(\"RuntimeError\");")5753+
end5754+
return "0"5755+
end5756+
if mname == "format"5757+
@needs_string_helpers = 15758+
return compile_sprintf_call(nid)5759+
end5760+
if mname == "sprintf"5761+
@needs_string_helpers = 15762+
return compile_sprintf_call(nid)5763+
end5764+
if mname == "putc"5765+
args_id = @nd_arguments[nid]5766+
if args_id >= 05767+
arg_ids = get_args(args_id)5768+
if arg_ids.length > 05769+
at = infer_type(arg_ids[0])5770+
if at == "int"5771+
return "(putchar((char)" + compile_expr(arg_ids[0]) + "), 0)"5772+
else5773+
return "(putchar(" + compile_expr(arg_ids[0]) + "[0]), 0)"5774+
end5775+
end5776+
end5777+
return "0"5778+
end56495779
mi = find_method_idx(mname)56505780
if mi >= 056515781
yargs = ""@@ -6116,6 +6246,16 @@
61166246
if mname == "to_a"61176247
return "sp_IntArray_dup(" + rc + ")"61186248
end6249+
if mname == "uniq"6250+
return "sp_IntArray_uniq(" + rc + ")"6251+
end6252+
if mname == "join"6253+
@needs_string_helpers = 16254+
return "sp_IntArray_join(" + rc + ", " + compile_arg0(nid) + ")"6255+
end6256+
if mname == "reverse"6257+
return "({ sp_IntArray *_r = sp_IntArray_dup(" + rc + "); sp_IntArray_reverse_bang(_r); _r; })"6258+
end61196259
end61206260
if recv_type == "str_array"61216261
if mname == "length"@@ -6195,6 +6335,18 @@
61956335
end61966336
end61976337
6338+
# ARGV methods6339+
if @nd_type[recv] == "ConstantReadNode"6340+
if @nd_name[recv] == "ARGV"6341+
if mname == "length"6342+
return "sp_argv.len"6343+
end6344+
if mname == "[]"6345+
return "sp_argv.data[(int)" + compile_arg0(nid) + "]"6346+
end6347+
end6348+
end6349+
61986350
# Math61996351
if @nd_type[recv] == "ConstantReadNode"62006352
rcname = @nd_name[recv]@@ -8091,6 +8243,32 @@
80918243
end80928244
end80938245
8246+
def compile_sprintf_call(nid)8247+
@needs_string_helpers = 18248+
args_id = @nd_arguments[nid]8249+
if args_id < 08250+
return "\"\""8251+
end8252+
arg_ids = get_args(args_id)8253+
if arg_ids.length == 08254+
return "\"\""8255+
end8256+
# First arg is format string, rest are values8257+
fmt = compile_expr(arg_ids[0])8258+
result = "sp_sprintf(" + fmt8259+
k = 18260+
while k < arg_ids.length8261+
at = infer_type(arg_ids[k])8262+
if at == "float"8263+
result = result + ", " + compile_expr(arg_ids[k])8264+
else8265+
result = result + ", " + compile_expr(arg_ids[k])8266+
end8267+
k = k + 18268+
end8269+
result + ")"8270+
end8271+
80948272
def compile_catch_expr(nid)80958273
@needs_setjmp = 180968274
tag = compile_arg0(nid)