Commit a0f9dfa9
a0f9dfa90a84Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add ao_render support with module constants, cross-class type inference, and more
- Module constant resolution: bare BNUM/BNUMF inside module methods resolve to cst_Rand_BNUM etc.
- Cross-class method param type inference: check ALL classes' zero-arg methods and readers
- Math.cos, Math.sin, Math::PI, Math.log, Math.exp, Math.atan2 support
- Integer()/Float() conversion with ARGV||default pattern handling
- Module ivar writes in compile_stmt, compile_expr, and MultiWriteNode
- Writer param types inferred from ivar types
- Array.new returns int_array type (not undefined obj_Array)
- Bracket assign casts object pointers to mrb_int for IntArray storage
- Fallback method dispatch for IntArray elements (cast to class pointer)
- Typed call args with auto-casting between int and object pointer types
- Constructor compiles all statement types (not just ivar writes)
- printf statement handler for formatted output
- Float division when RHS operand is float type
- GC stack bottom scope fix and threshold increase for stability
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- a0f9dfa90a84
- Parent
- bf5a7eea452e
spinel_codegen.rbmodifiedView file
@@ -813,6 +813,19 @@
813813
if cx >= 0814814
return "class_" + @nd_name[nid]815815
end816+
# Check module-prefixed constants817+
mi3 = 0818+
while mi3 < @module_names.length819+
mmod = @module_names[mi3]820+
if mmod != ""821+
cpname = mmod + "_" + @nd_name[nid]822+
ci4 = find_const_idx(cpname)823+
if ci4 >= 0824+
return @const_types[ci4]825+
end826+
end827+
mi3 = mi3 + 1828+
end816829
return "int"817830
end818831
if t == "ConstantPathNode"@@ -827,6 +840,9 @@
827840
if rname == "Float"828841
return "float"829842
end843+
if rname == "Math"844+
return "float"845+
end830846
end831847
return "int"832848
end@@ -1558,6 +1574,21 @@
15581574
if mname == "sqrt"15591575
return "float"15601576
end1577+
if mname == "cos"1578+
return "float"1579+
end1580+
if mname == "sin"1581+
return "float"1582+
end1583+
if mname == "log"1584+
return "float"1585+
end1586+
if mname == "exp"1587+
return "float"1588+
end1589+
if mname == "atan2"1590+
return "float"1591+
end15611592
if mname == "freeze"15621593
if recv >= 015631594
return infer_type(recv)@@ -1595,6 +1626,53 @@
15951626
end15961627
return "int"15971628
end1629+
# Method call on int (possible IntArray element storing object pointers)1630+
if rt == "int"1631+
ci = 01632+
while ci < @cls_names.length1633+
# Check zero-arg methods (getters)1634+
ci2_mnames = @cls_meth_names[ci].split(";")1635+
ci2_mparams = @cls_meth_params[ci].split("|")1636+
mi2 = 01637+
while mi2 < ci2_mnames.length1638+
if ci2_mnames[mi2] == mname1639+
mp2 = ""1640+
if mi2 < ci2_mparams.length1641+
mp2 = ci2_mparams[mi2]1642+
end1643+
if mp2 == ""1644+
# Found zero-arg method match1645+
mr = cls_method_return(ci, mname)1646+
if mr != "int"1647+
return mr1648+
end1649+
end1650+
end1651+
mi2 = mi2 + 11652+
end1653+
# Check attr_readers1654+
readers2 = @cls_attr_readers[ci].split(";")1655+
j2 = 01656+
while j2 < readers2.length1657+
if readers2[j2] == mname1658+
ivt = cls_ivar_type(ci, "@" + mname)1659+
if ivt != "int"1660+
return ivt1661+
end1662+
end1663+
j2 = j2 + 11664+
end1665+
# Check methods with args1666+
midx = cls_find_method_direct(ci, mname)1667+
if midx >= 01668+
mr = cls_method_return(ci, mname)1669+
if mr != "int"1670+
return mr1671+
end1672+
end1673+
ci = ci + 11674+
end1675+
end15981676
if is_obj_type(rt) == 115991677
cname = rt[4, rt.length - 4]16001678
ci = find_class_idx(cname)@@ -2732,7 +2810,14 @@
27322810
r = @nd_receiver[nid]27332811
if r >= 027342812
if @nd_type[r] == "ConstantReadNode"2735-
return "obj_" + @nd_name[r]2813+
rname = @nd_name[r]2814+
if rname == "Array"2815+
return "int_array"2816+
end2817+
if rname == "Hash"2818+
return "str_int_hash"2819+
end2820+
return "obj_" + rname27362821
end27372822
end27382823
end@@ -3483,51 +3568,149 @@
34833568
34843569
def infer_cls_meth_param_from_body34853570
# For each class method, if a param is used as param.attr_reader where attr_reader3486-
# belongs to this class, infer param type as this class.3487-
ci = 03488-
while ci < @cls_names.length3489-
readers = @cls_attr_readers[ci].split(";")3490-
if readers.length > 03491-
mnames = @cls_meth_names[ci].split(";")3492-
all_params = @cls_meth_params[ci].split("|")3493-
all_ptypes = @cls_meth_ptypes[ci].split("|")3494-
bodies = @cls_meth_bodies[ci].split(";")3495-
j = 03496-
while j < mnames.length3497-
if mnames[j] != "initialize"3498-
pnames = "".split(",")3499-
ptypes = "".split(",")3571+
# belongs to ANY class, infer param type as that class.3572+
# Check all classes' methods (not just the class owning the readers).3573+
oci = 03574+
while oci < @cls_names.length3575+
mnames = @cls_meth_names[oci].split(";")3576+
all_params = @cls_meth_params[oci].split("|")3577+
all_ptypes = @cls_meth_ptypes[oci].split("|")3578+
bodies = @cls_meth_bodies[oci].split(";")3579+
changed = 03580+
j = 03581+
while j < mnames.length3582+
if mnames[j] != "initialize"3583+
pnames = "".split(",")3584+
ptypes = "".split(",")35003585
3501-
if j < all_params.length3502-
pnames = all_params[j].split(",")3503-
end3504-
if j < all_ptypes.length3505-
ptypes = all_ptypes[j].split(",")3506-
end3507-
bid = -13508-
if j < bodies.length3509-
bid = bodies[j].to_i3586+
if j < all_params.length3587+
pnames = all_params[j].split(",")3588+
end3589+
if j < all_ptypes.length3590+
ptypes = all_ptypes[j].split(",")3591+
end3592+
bid = -13593+
if j < bodies.length3594+
bid = bodies[j].to_i3595+
end3596+
if bid >= 03597+
pk = 03598+
while pk < pnames.length3599+
if pk < ptypes.length3600+
if ptypes[pk] == "int"3601+
# Check against ALL classes' readers and zero-arg methods3602+
ci2 = 03603+
found_class = 03604+
while ci2 < @cls_names.length3605+
if found_class == 03606+
readers = @cls_attr_readers[ci2].split(";")3607+
if readers.length > 03608+
if param_calls_reader(bid, pnames[pk], readers) == 13609+
found_class = 13610+
end3611+
end3612+
# Also check zero-arg class methods as readers3613+
if found_class == 03614+
ci2_mnames = @cls_meth_names[ci2].split(";")3615+
ci2_mparams = @cls_meth_params[ci2].split("|")3616+
zero_arg_meths = "".split(",")3617+
mi2 = 03618+
while mi2 < ci2_mnames.length3619+
mn2 = ci2_mnames[mi2]3620+
if mn2 != "initialize"3621+
mp2 = ""3622+
if mi2 < ci2_mparams.length3623+
mp2 = ci2_mparams[mi2]3624+
end3625+
if mp2 == ""3626+
zero_arg_meths.push(mn2)3627+
end3628+
end3629+
mi2 = mi2 + 13630+
end3631+
if zero_arg_meths.length > 03632+
if param_calls_reader(bid, pnames[pk], zero_arg_meths) == 13633+
found_class = 13634+
end3635+
end3636+
end3637+
if found_class == 13638+
ptypes[pk] = "obj_" + @cls_names[ci2]3639+
all_ptypes[j] = join_sep(ptypes, ",")3640+
@cls_meth_ptypes[oci] = join_sep(all_ptypes, "|")3641+
changed = 13642+
end3643+
end3644+
ci2 = ci2 + 13645+
end3646+
end3647+
end3648+
pk = pk + 135103649
end3511-
if bid >= 03512-
pk = 03513-
while pk < pnames.length3514-
if pk < ptypes.length3515-
if ptypes[pk] == "int"3650+
end3651+
end3652+
j = j + 13653+
end3654+
oci = oci + 13655+
end3656+
# Also infer top-level method param types from body usage3657+
mi = 03658+
while mi < @meth_names.length3659+
bid = @meth_body_ids[mi]3660+
if bid >= 03661+
pnames = @meth_param_names[mi].split(",")3662+
ptypes = @meth_param_types[mi].split(",")3663+
pk = 03664+
while pk < pnames.length3665+
if pk < ptypes.length3666+
if ptypes[pk] == "int"3667+
ci2 = 03668+
found_class = 03669+
while ci2 < @cls_names.length3670+
if found_class == 03671+
readers = @cls_attr_readers[ci2].split(";")3672+
if readers.length > 035163673
if param_calls_reader(bid, pnames[pk], readers) == 13517-
ptypes[pk] = "obj_" + @cls_names[ci]3518-
all_ptypes[j] = join_sep(ptypes, ",")3519-
@cls_meth_ptypes[ci] = join_sep(all_ptypes, "|")3674+
found_class = 13675+
end3676+
end3677+
if found_class == 03678+
ci2_mnames = @cls_meth_names[ci2].split(";")3679+
ci2_mparams = @cls_meth_params[ci2].split("|")3680+
zero_arg_meths = "".split(",")3681+
mi2 = 03682+
while mi2 < ci2_mnames.length3683+
mn2 = ci2_mnames[mi2]3684+
if mn2 != "initialize"3685+
mp2 = ""3686+
if mi2 < ci2_mparams.length3687+
mp2 = ci2_mparams[mi2]3688+
end3689+
if mp2 == ""3690+
zero_arg_meths.push(mn2)3691+
end3692+
end3693+
mi2 = mi2 + 13694+
end3695+
if zero_arg_meths.length > 03696+
if param_calls_reader(bid, pnames[pk], zero_arg_meths) == 13697+
found_class = 13698+
end35203699
end35213700
end3701+
if found_class == 13702+
ptypes[pk] = "obj_" + @cls_names[ci2]3703+
@meth_param_types[mi] = join_sep(ptypes, ",")3704+
end35223705
end3523-
pk = pk + 13706+
ci2 = ci2 + 135243707
end35253708
end35263709
end3527-
j = j + 13710+
pk = pk + 135283711
end35293712
end3530-
ci = ci + 13713+
mi = mi + 135313714
end35323715
end35333716
@@ -3787,6 +3970,51 @@
37873970
end37883971
end37893972
3973+
def infer_writer_param_types3974+
# For setter methods (def x=(v); @x = v; end), infer param type from ivar type3975+
ci = 03976+
while ci < @cls_names.length3977+
mnames = @cls_meth_names[ci].split(";")3978+
all_ptypes = @cls_meth_ptypes[ci].split("|")3979+
ivar_names = @cls_ivar_names[ci].split(";")3980+
ivar_types = @cls_ivar_types[ci].split(";")3981+
changed = 03982+
j = 03983+
while j < mnames.length3984+
mn = mnames[j]3985+
if mn.length > 13986+
if mn[mn.length - 1] == "="3987+
bname = mn[0, mn.length - 1]3988+
iname = "@" + bname3989+
# Find ivar type3990+
ik = 03991+
while ik < ivar_names.length3992+
if ivar_names[ik] == iname3993+
ivt = ivar_types[ik]3994+
if ivt != "int"3995+
if ivt != "nil"3996+
if j < all_ptypes.length3997+
if all_ptypes[j] == "int"3998+
all_ptypes[j] = ivt3999+
changed = 14000+
end4001+
end4002+
end4003+
end4004+
end4005+
ik = ik + 14006+
end4007+
end4008+
end4009+
j = j + 14010+
end4011+
if changed == 14012+
@cls_meth_ptypes[ci] = join_sep(all_ptypes, "|")4013+
end4014+
ci = ci + 14015+
end4016+
end4017+
37904018
def infer_all_returns37914019
# Pre-pass: infer class method param types from body usage37924020
infer_cls_meth_param_from_body@@ -3794,6 +4022,8 @@
37944022
infer_constructor_types37954023
# Update ivar types from constructor params37964024
update_ivar_types_from_params4025+
# Infer setter param types from ivar types4026+
infer_writer_param_types37974027
37984028
# Top-level methods37994029
i = 0@@ -4887,6 +5117,8 @@
48875117
end48885118
# Scan for calls to other methods in same class48895119
scan_cls_method_calls(ci, bid)5120+
# Also scan for constructor calls to infer param types5121+
scan_new_calls(bid)48905122
pop_scope48915123
@current_class_idx = -148925124
end@@ -5133,7 +5365,7 @@
51335365
51345366
def emit_gc_runtime51355367
emit_raw("typedef struct sp_gc_hdr { struct sp_gc_hdr *next; void (*finalize)(void *); void (*scan)(void *); unsigned marked : 1; } sp_gc_hdr;")5136-
emit_raw("static sp_gc_hdr *sp_gc_heap = NULL; static size_t sp_gc_bytes = 0; static size_t sp_gc_threshold = 4*1024*1024;")5368+
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*1024;")51375369
emit_raw("#define SP_GC_STACK_MAX 65536")51385370
emit_raw("static void **sp_gc_roots[SP_GC_STACK_MAX]; static int sp_gc_nroots = 0;")51395371
emit_raw("#define SP_GC_SAVE() int __attribute__((cleanup(sp_gc_cleanup))) _gc_saved = sp_gc_nroots")@@ -6005,6 +6237,11 @@
60056237
ivar = sanitize_ivar(@nd_name[sid])60066238
val = compile_expr(@nd_expression[sid])60076239
emit_raw(" self->" + ivar + " = " + val + ";")6240+
else6241+
if @nd_type[sid] != "SuperNode"6242+
# Compile other statements (e.g., method calls like @arr[0] = val)6243+
compile_stmt(sid)6244+
end60086245
end60096246
k = k + 160106247
end@@ -6665,7 +6902,7 @@
66656902
emit_raw("")66666903
emit_raw("int main(int argc,char**argv){")66676904
if @needs_gc == 16668-
emit_raw(" { volatile char _sb; sp_gc_stack_bottom = (char*)&_sb; }")6905+
emit_raw(" volatile char _sb; sp_gc_stack_bottom = (char*)&_sb;")66696906
end66706907
emit_raw(" sp_argv.data=(const char**)(argv+1);sp_argv.len=argc-1;")66716908
@@ -6835,6 +7072,26 @@
68357072
end68367073
return "self->" + sanitize_ivar(@nd_name[nid])68377074
end7075+
if t == "InstanceVariableWriteNode"7076+
val = compile_expr(@nd_expression[nid])7077+
# Check if in module method7078+
mi3 = 07079+
while mi3 < @module_names.length7080+
mmod = @module_names[mi3]7081+
if mmod != ""7082+
if @current_method_name.start_with?(mmod + "_cls_")7083+
iname = @nd_name[nid]7084+
cname3 = mmod + "_" + iname[1, iname.length - 1]7085+
ci3 = find_const_idx(cname3)7086+
if ci3 >= 07087+
return "(cst_" + cname3 + " = " + val + ")"7088+
end7089+
end7090+
end7091+
mi3 = mi3 + 17092+
end7093+
return "(self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ")"7094+
end68387095
if t == "ConstantReadNode"68397096
if @nd_name[nid] == "ARGV"68407097
return "sp_argv"@@ -6843,6 +7100,27 @@
68437100
if ci >= 068447101
return "cst_" + @nd_name[nid]68457102
end7103+
# Check if inside a module method and constant belongs to that module7104+
mi3 = 07105+
while mi3 < @module_names.length7106+
mmod = @module_names[mi3]7107+
if mmod != ""7108+
if @current_method_name.start_with?(mmod + "_cls_")7109+
cpname = mmod + "_" + @nd_name[nid]7110+
ci4 = find_const_idx(cpname)7111+
if ci4 >= 07112+
return "cst_" + cpname7113+
end7114+
end7115+
# Also check when in main scope (module constants referenced at top level)7116+
cpname = mmod + "_" + @nd_name[nid]7117+
ci5 = find_const_idx(cpname)7118+
if ci5 >= 07119+
return "cst_" + cpname7120+
end7121+
end7122+
mi3 = mi3 + 17123+
end68467124
return @nd_name[nid]68477125
end68487126
if t == "ConstantPathNode"@@ -6868,6 +7146,14 @@
68687146
return "INT64_MAX"68697147
end68707148
end7149+
if rname == "Math"7150+
if nname == "PI"7151+
return "3.14159265358979323846"7152+
end7153+
if nname == "E"7154+
return "2.71828182845904523536"7155+
end7156+
end68717157
return cpname68727158
end68737159
return @nd_name[nid]@@ -7263,6 +7549,41 @@
72637549
if mname == "__method__"72647550
return "\"" + @current_method_name + "\""72657551
end7552+
if mname == "Integer"7553+
args_id = @nd_arguments[nid]7554+
if args_id >= 07555+
arg_ids = get_args(args_id)7556+
if arg_ids.length > 07557+
a0 = arg_ids[0]7558+
# Handle OrNode: Integer(ARGV[0] || default)7559+
if @nd_type[a0] == "OrNode"7560+
lt = infer_type(@nd_left[a0])7561+
rt = infer_type(@nd_right[a0])7562+
if lt == "string" or lt == "argv"7563+
# ARGV access || default7564+
lc = compile_expr(@nd_left[a0])7565+
rc2 = compile_expr(@nd_right[a0])7566+
if rt == "int"7567+
return "((" + lc + ") ? (mrb_int)strtoll(" + lc + ", NULL, 10) : " + rc2 + ")"7568+
else7569+
return "((" + lc + ") ? (mrb_int)strtoll(" + lc + ", NULL, 10) : (mrb_int)strtoll(" + rc2 + ", NULL, 10))"7570+
end7571+
end7572+
end7573+
at = infer_type(a0)7574+
if at == "string"7575+
return "(mrb_int)strtoll(" + compile_expr(a0) + ", NULL, 10)"7576+
end7577+
if at == "argv"7578+
return "(mrb_int)strtoll(" + compile_expr(a0) + ", NULL, 10)"7579+
end7580+
end7581+
end7582+
return "(mrb_int)(" + compile_arg0(nid) + ")"7583+
end7584+
if mname == "Float"7585+
return "(mrb_float)(" + compile_arg0(nid) + ")"7586+
end72667587
if mname == "proc"72677588
if @nd_block[nid] >= 072687589
@needs_proc = 1@@ -7584,6 +7905,17 @@
75847905
if lt == "float"75857906
return "(" + compile_expr(recv) + " / " + compile_arg0(nid) + ")"75867907
end7908+
# Check RHS for float7909+
args_id = @nd_arguments[nid]7910+
if args_id >= 07911+
aargs = get_args(args_id)7912+
if aargs.length > 07913+
rt = infer_type(aargs[0])7914+
if rt == "float"7915+
return "((mrb_float)" + compile_expr(recv) + " / " + compile_arg0(nid) + ")"7916+
end7917+
end7918+
end75877919
return "sp_idiv(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"75887920
end75897921
if mname == "%"@@ -8484,7 +8816,8 @@
84848816
return "sp_argv.len"84858817
end84868818
if mname == "[]"8487-
return "sp_argv.data[(int)" + compile_arg0(nid) + "]"8819+
idx_expr = compile_arg0(nid)8820+
return "((" + idx_expr + " < sp_argv.len) ? sp_argv.data[(int)" + idx_expr + "] : NULL)"84888821
end84898822
end84908823
end@@ -8496,6 +8829,30 @@
84968829
if mname == "sqrt"84978830
return "sqrt(" + compile_arg0(nid) + ")"84988831
end8832+
if mname == "cos"8833+
return "cos(" + compile_arg0(nid) + ")"8834+
end8835+
if mname == "sin"8836+
return "sin(" + compile_arg0(nid) + ")"8837+
end8838+
if mname == "log"8839+
return "log(" + compile_arg0(nid) + ")"8840+
end8841+
if mname == "log2"8842+
return "log2(" + compile_arg0(nid) + ")"8843+
end8844+
if mname == "exp"8845+
return "exp(" + compile_arg0(nid) + ")"8846+
end8847+
if mname == "atan2"8848+
args_id = @nd_arguments[nid]8849+
if args_id >= 08850+
arg_ids = get_args(args_id)8851+
if arg_ids.length >= 28852+
return "atan2(" + compile_expr(arg_ids[0]) + ", " + compile_expr(arg_ids[1]) + ")"8853+
end8854+
end8855+
end84998856
end85008857
# File operations85018858
if rcname == "File"@@ -8747,7 +9104,17 @@
87479104
# Method call87489105
owner = find_method_owner(ci, mname)87499106
if owner != ""8750-
ca = compile_call_args(nid)9107+
oci2 = find_class_idx(owner)9108+
midx2 = -19109+
if oci2 >= 09110+
midx2 = cls_find_method_direct(oci2, mname)9111+
end9112+
ca = ""9113+
if midx2 >= 09114+
ca = compile_typed_call_args(nid, oci2, midx2)9115+
else9116+
ca = compile_call_args(nid)9117+
end87519118
if owner == cname87529119
if ca != ""87539120
return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"@@ -8765,6 +9132,62 @@
87659132
end87669133
end87679134
9135+
# Fallback: if receiver is int (e.g. from IntArray get) but method belongs to a class,9136+
# cast the int to the appropriate class pointer and dispatch9137+
if recv_type == "int"9138+
ci2 = 09139+
while ci2 < @cls_names.length9140+
cname2 = @cls_names[ci2]9141+
readers2 = @cls_attr_readers[ci2].split(";")9142+
found_reader = 09143+
j2 = 09144+
while j2 < readers2.length9145+
if readers2[j2] == mname9146+
found_reader = 19147+
end9148+
j2 = j2 + 19149+
end9150+
if found_reader == 19151+
return "((sp_" + cname2 + " *)" + rc + ")->" + mname9152+
end9153+
# Check writers9154+
if mname.length > 19155+
if mname[mname.length - 1] == "="9156+
bname2 = mname[0, mname.length - 1]9157+
writers2 = @cls_attr_writers[ci2].split(";")9158+
j2 = 09159+
while j2 < writers2.length9160+
if writers2[j2] == bname29161+
return "(((sp_" + cname2 + " *)" + rc + ")->" + bname2 + " = " + compile_arg0(nid) + ")"9162+
end9163+
j2 = j2 + 19164+
end9165+
end9166+
end9167+
# Check methods9168+
owner2 = find_method_owner(ci2, mname)9169+
if owner2 != ""9170+
oci3 = find_class_idx(owner2)9171+
midx3 = -19172+
if oci3 >= 09173+
midx3 = cls_find_method_direct(oci3, mname)9174+
end9175+
ca2 = ""9176+
if midx3 >= 09177+
ca2 = compile_typed_call_args(nid, oci3, midx3)9178+
else9179+
ca2 = compile_call_args(nid)9180+
end9181+
if ca2 != ""9182+
return "sp_" + owner2 + "_" + sanitize_name(mname) + "((sp_" + owner2 + " *)" + rc + ", " + ca2 + ")"9183+
else9184+
return "sp_" + owner2 + "_" + sanitize_name(mname) + "((sp_" + owner2 + " *)" + rc + ")"9185+
end9186+
end9187+
ci2 = ci2 + 19188+
end9189+
end9190+
87689191
"0"87699192
end87709193
@@ -9087,6 +9510,48 @@
90879510
result90889511
end90899512
9513+
def compile_typed_call_args(nid, target_ci, target_midx)9514+
# Like compile_call_args but casts arguments to match target method param types9515+
args_id = @nd_arguments[nid]9516+
if args_id < 09517+
return ""9518+
end9519+
arg_ids = get_args(args_id)9520+
all_ptypes = @cls_meth_ptypes[target_ci].split("|")9521+
ptypes = "".split(",")9522+
if target_midx < all_ptypes.length9523+
ptypes = all_ptypes[target_midx].split(",")9524+
end9525+
result = ""9526+
k = 09527+
while k < arg_ids.length9528+
if k > 09529+
result = result + ", "9530+
end9531+
aexpr = compile_expr(arg_ids[k])9532+
at = infer_type(arg_ids[k])9533+
if k < ptypes.length9534+
pt = ptypes[k]9535+
if at == "int"9536+
if is_obj_type(pt) == 19537+
# Cast int to object pointer9538+
pcname = pt[4, pt.length - 4]9539+
aexpr = "(sp_" + pcname + " *)" + aexpr9540+
end9541+
end9542+
if is_obj_type(at) == 19543+
if pt == "int"9544+
# Cast object pointer to int9545+
aexpr = "(mrb_int)" + aexpr9546+
end9547+
end9548+
end9549+
result = result + aexpr9550+
k = k + 19551+
end9552+
result9553+
end9554+
90909555
def find_method_owner(ci, mname)90919556
if ci < 090929557
return ""@@ -9340,7 +9805,27 @@
93409805
end93419806
if t == "InstanceVariableWriteNode"93429807
val = compile_expr(@nd_expression[nid])9343-
emit(" self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ";")9808+
# Check if we're in a module class method9809+
mod_ivar = 09810+
mi3 = 09811+
while mi3 < @module_names.length9812+
mmod = @module_names[mi3]9813+
if mmod != ""9814+
if @current_method_name.start_with?(mmod + "_cls_")9815+
iname = @nd_name[nid]9816+
cname3 = mmod + "_" + iname[1, iname.length - 1]9817+
ci3 = find_const_idx(cname3)9818+
if ci3 >= 09819+
emit(" cst_" + cname3 + " = " + val + ";")9820+
mod_ivar = 19821+
end9822+
end9823+
end9824+
mi3 = mi3 + 19825+
end9826+
if mod_ivar == 09827+
emit(" self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ";")9828+
end93449829
return93459830
end93469831
if t == "InstanceVariableOperatorWriteNode"@@ -9442,7 +9927,8 @@
94429927
while k < elems.length94439928
tmp = new_temp94449929
tmps.push(tmp)9445-
emit(" mrb_int " + tmp + " = " + compile_expr(elems[k]) + ";")9930+
et = infer_type(elems[k])9931+
emit(" " + c_type(et) + " " + tmp + " = " + compile_expr(elems[k]) + ";")94469932
k = k + 194479933
end94489934
# Now assign@@ -9453,6 +9939,29 @@
94539939
if @nd_type[tid] == "LocalVariableTargetNode"94549940
emit(" lv_" + @nd_name[tid] + " = " + tmps[k] + ";")94559941
end9942+
if @nd_type[tid] == "InstanceVariableTargetNode"9943+
iname = @nd_name[tid]9944+
# Check if in module method9945+
mod_ivar = 09946+
mi3 = 09947+
while mi3 < @module_names.length9948+
mmod = @module_names[mi3]9949+
if mmod != ""9950+
if @current_method_name.start_with?(mmod + "_cls_")9951+
cname3 = mmod + "_" + iname[1, iname.length - 1]9952+
ci3 = find_const_idx(cname3)9953+
if ci3 >= 09954+
emit(" cst_" + cname3 + " = " + tmps[k] + ";")9955+
mod_ivar = 19956+
end9957+
end9958+
end9959+
mi3 = mi3 + 19960+
end9961+
if mod_ivar == 09962+
emit(" self->" + sanitize_ivar(iname) + " = " + tmps[k] + ";")9963+
end9964+
end94569965
end94579966
k = k + 194589967
end@@ -9828,6 +10337,31 @@
982810337
return982910338
end983010339
end10340+
if mname == "printf"10341+
if recv < 010342+
args_id = @nd_arguments[nid]10343+
if args_id >= 010344+
arg_ids = get_args(args_id)10345+
if arg_ids.length >= 110346+
# First arg is format string10347+
fmt_expr = compile_expr(arg_ids[0])10348+
rest_args = ""10349+
k = 110350+
while k < arg_ids.length10351+
at = infer_type(arg_ids[k])10352+
if at == "int"10353+
rest_args = rest_args + ", (int)" + compile_expr(arg_ids[k])10354+
else10355+
rest_args = rest_args + ", " + compile_expr(arg_ids[k])10356+
end10357+
k = k + 110358+
end10359+
emit(" printf(" + fmt_expr + rest_args + ");")10360+
return10361+
end10362+
end10363+
end10364+
end983110365
983210366
# File.open with block983310367
if mname == "open"@@ -10430,7 +10964,16 @@
1043010964
val = compile_expr(arg_ids[1])1043110965
end1043210966
if rt == "int_array"10433-
emit(" sp_IntArray_set(" + rc + ", " + idx + ", " + val + ");")10967+
# Check if value is an object pointer - needs cast10968+
vt = "int"10969+
if arg_ids.length >= 210970+
vt = infer_type(arg_ids[1])10971+
end10972+
if is_obj_type(vt) == 110973+
emit(" sp_IntArray_set(" + rc + ", " + idx + ", (mrb_int)" + val + ");")10974+
else10975+
emit(" sp_IntArray_set(" + rc + ", " + idx + ", " + val + ");")10976+
end1043410977
return1043510978
end1043610979
if rt == "float_array"