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 >= 0
814814 return "class_" + @nd_name[nid]
815815 end
816+ # Check module-prefixed constants
817+ mi3 = 0
818+ while mi3 < @module_names.length
819+ mmod = @module_names[mi3]
820+ if mmod != ""
821+ cpname = mmod + "_" + @nd_name[nid]
822+ ci4 = find_const_idx(cpname)
823+ if ci4 >= 0
824+ return @const_types[ci4]
825+ end
826+ end
827+ mi3 = mi3 + 1
828+ end
816829 return "int"
817830 end
818831 if t == "ConstantPathNode"
@@ -827,6 +840,9 @@
827840 if rname == "Float"
828841 return "float"
829842 end
843+ if rname == "Math"
844+ return "float"
845+ end
830846 end
831847 return "int"
832848 end
@@ -1558,6 +1574,21 @@
15581574 if mname == "sqrt"
15591575 return "float"
15601576 end
1577+ if mname == "cos"
1578+ return "float"
1579+ end
1580+ if mname == "sin"
1581+ return "float"
1582+ end
1583+ if mname == "log"
1584+ return "float"
1585+ end
1586+ if mname == "exp"
1587+ return "float"
1588+ end
1589+ if mname == "atan2"
1590+ return "float"
1591+ end
15611592 if mname == "freeze"
15621593 if recv >= 0
15631594 return infer_type(recv)
@@ -1595,6 +1626,53 @@
15951626 end
15961627 return "int"
15971628 end
1629+ # Method call on int (possible IntArray element storing object pointers)
1630+ if rt == "int"
1631+ ci = 0
1632+ while ci < @cls_names.length
1633+ # Check zero-arg methods (getters)
1634+ ci2_mnames = @cls_meth_names[ci].split(";")
1635+ ci2_mparams = @cls_meth_params[ci].split("|")
1636+ mi2 = 0
1637+ while mi2 < ci2_mnames.length
1638+ if ci2_mnames[mi2] == mname
1639+ mp2 = ""
1640+ if mi2 < ci2_mparams.length
1641+ mp2 = ci2_mparams[mi2]
1642+ end
1643+ if mp2 == ""
1644+ # Found zero-arg method match
1645+ mr = cls_method_return(ci, mname)
1646+ if mr != "int"
1647+ return mr
1648+ end
1649+ end
1650+ end
1651+ mi2 = mi2 + 1
1652+ end
1653+ # Check attr_readers
1654+ readers2 = @cls_attr_readers[ci].split(";")
1655+ j2 = 0
1656+ while j2 < readers2.length
1657+ if readers2[j2] == mname
1658+ ivt = cls_ivar_type(ci, "@" + mname)
1659+ if ivt != "int"
1660+ return ivt
1661+ end
1662+ end
1663+ j2 = j2 + 1
1664+ end
1665+ # Check methods with args
1666+ midx = cls_find_method_direct(ci, mname)
1667+ if midx >= 0
1668+ mr = cls_method_return(ci, mname)
1669+ if mr != "int"
1670+ return mr
1671+ end
1672+ end
1673+ ci = ci + 1
1674+ end
1675+ end
15981676 if is_obj_type(rt) == 1
15991677 cname = rt[4, rt.length - 4]
16001678 ci = find_class_idx(cname)
@@ -2732,7 +2810,14 @@
27322810 r = @nd_receiver[nid]
27332811 if r >= 0
27342812 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+ end
2817+ if rname == "Hash"
2818+ return "str_int_hash"
2819+ end
2820+ return "obj_" + rname
27362821 end
27372822 end
27382823 end
@@ -3483,51 +3568,149 @@
34833568
34843569 def infer_cls_meth_param_from_body
34853570 # For each class method, if a param is used as param.attr_reader where attr_reader
3486- # belongs to this class, infer param type as this class.
3487- ci = 0
3488- while ci < @cls_names.length
3489- readers = @cls_attr_readers[ci].split(";")
3490- if readers.length > 0
3491- 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 = 0
3496- while j < mnames.length
3497- 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 = 0
3574+ while oci < @cls_names.length
3575+ 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 = 0
3580+ j = 0
3581+ while j < mnames.length
3582+ if mnames[j] != "initialize"
3583+ pnames = "".split(",")
3584+ ptypes = "".split(",")
35003585
3501- if j < all_params.length
3502- pnames = all_params[j].split(",")
3503- end
3504- if j < all_ptypes.length
3505- ptypes = all_ptypes[j].split(",")
3506- end
3507- bid = -1
3508- if j < bodies.length
3509- bid = bodies[j].to_i
3586+ if j < all_params.length
3587+ pnames = all_params[j].split(",")
3588+ end
3589+ if j < all_ptypes.length
3590+ ptypes = all_ptypes[j].split(",")
3591+ end
3592+ bid = -1
3593+ if j < bodies.length
3594+ bid = bodies[j].to_i
3595+ end
3596+ if bid >= 0
3597+ pk = 0
3598+ while pk < pnames.length
3599+ if pk < ptypes.length
3600+ if ptypes[pk] == "int"
3601+ # Check against ALL classes' readers and zero-arg methods
3602+ ci2 = 0
3603+ found_class = 0
3604+ while ci2 < @cls_names.length
3605+ if found_class == 0
3606+ readers = @cls_attr_readers[ci2].split(";")
3607+ if readers.length > 0
3608+ if param_calls_reader(bid, pnames[pk], readers) == 1
3609+ found_class = 1
3610+ end
3611+ end
3612+ # Also check zero-arg class methods as readers
3613+ if found_class == 0
3614+ ci2_mnames = @cls_meth_names[ci2].split(";")
3615+ ci2_mparams = @cls_meth_params[ci2].split("|")
3616+ zero_arg_meths = "".split(",")
3617+ mi2 = 0
3618+ while mi2 < ci2_mnames.length
3619+ mn2 = ci2_mnames[mi2]
3620+ if mn2 != "initialize"
3621+ mp2 = ""
3622+ if mi2 < ci2_mparams.length
3623+ mp2 = ci2_mparams[mi2]
3624+ end
3625+ if mp2 == ""
3626+ zero_arg_meths.push(mn2)
3627+ end
3628+ end
3629+ mi2 = mi2 + 1
3630+ end
3631+ if zero_arg_meths.length > 0
3632+ if param_calls_reader(bid, pnames[pk], zero_arg_meths) == 1
3633+ found_class = 1
3634+ end
3635+ end
3636+ end
3637+ if found_class == 1
3638+ ptypes[pk] = "obj_" + @cls_names[ci2]
3639+ all_ptypes[j] = join_sep(ptypes, ",")
3640+ @cls_meth_ptypes[oci] = join_sep(all_ptypes, "|")
3641+ changed = 1
3642+ end
3643+ end
3644+ ci2 = ci2 + 1
3645+ end
3646+ end
3647+ end
3648+ pk = pk + 1
35103649 end
3511- if bid >= 0
3512- pk = 0
3513- while pk < pnames.length
3514- if pk < ptypes.length
3515- if ptypes[pk] == "int"
3650+ end
3651+ end
3652+ j = j + 1
3653+ end
3654+ oci = oci + 1
3655+ end
3656+ # Also infer top-level method param types from body usage
3657+ mi = 0
3658+ while mi < @meth_names.length
3659+ bid = @meth_body_ids[mi]
3660+ if bid >= 0
3661+ pnames = @meth_param_names[mi].split(",")
3662+ ptypes = @meth_param_types[mi].split(",")
3663+ pk = 0
3664+ while pk < pnames.length
3665+ if pk < ptypes.length
3666+ if ptypes[pk] == "int"
3667+ ci2 = 0
3668+ found_class = 0
3669+ while ci2 < @cls_names.length
3670+ if found_class == 0
3671+ readers = @cls_attr_readers[ci2].split(";")
3672+ if readers.length > 0
35163673 if param_calls_reader(bid, pnames[pk], readers) == 1
3517- 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 = 1
3675+ end
3676+ end
3677+ if found_class == 0
3678+ ci2_mnames = @cls_meth_names[ci2].split(";")
3679+ ci2_mparams = @cls_meth_params[ci2].split("|")
3680+ zero_arg_meths = "".split(",")
3681+ mi2 = 0
3682+ while mi2 < ci2_mnames.length
3683+ mn2 = ci2_mnames[mi2]
3684+ if mn2 != "initialize"
3685+ mp2 = ""
3686+ if mi2 < ci2_mparams.length
3687+ mp2 = ci2_mparams[mi2]
3688+ end
3689+ if mp2 == ""
3690+ zero_arg_meths.push(mn2)
3691+ end
3692+ end
3693+ mi2 = mi2 + 1
3694+ end
3695+ if zero_arg_meths.length > 0
3696+ if param_calls_reader(bid, pnames[pk], zero_arg_meths) == 1
3697+ found_class = 1
3698+ end
35203699 end
35213700 end
3701+ if found_class == 1
3702+ ptypes[pk] = "obj_" + @cls_names[ci2]
3703+ @meth_param_types[mi] = join_sep(ptypes, ",")
3704+ end
35223705 end
3523- pk = pk + 1
3706+ ci2 = ci2 + 1
35243707 end
35253708 end
35263709 end
3527- j = j + 1
3710+ pk = pk + 1
35283711 end
35293712 end
3530- ci = ci + 1
3713+ mi = mi + 1
35313714 end
35323715 end
35333716
@@ -3787,6 +3970,51 @@
37873970 end
37883971 end
37893972
3973+ def infer_writer_param_types
3974+ # For setter methods (def x=(v); @x = v; end), infer param type from ivar type
3975+ ci = 0
3976+ while ci < @cls_names.length
3977+ 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 = 0
3982+ j = 0
3983+ while j < mnames.length
3984+ mn = mnames[j]
3985+ if mn.length > 1
3986+ if mn[mn.length - 1] == "="
3987+ bname = mn[0, mn.length - 1]
3988+ iname = "@" + bname
3989+ # Find ivar type
3990+ ik = 0
3991+ while ik < ivar_names.length
3992+ if ivar_names[ik] == iname
3993+ ivt = ivar_types[ik]
3994+ if ivt != "int"
3995+ if ivt != "nil"
3996+ if j < all_ptypes.length
3997+ if all_ptypes[j] == "int"
3998+ all_ptypes[j] = ivt
3999+ changed = 1
4000+ end
4001+ end
4002+ end
4003+ end
4004+ end
4005+ ik = ik + 1
4006+ end
4007+ end
4008+ end
4009+ j = j + 1
4010+ end
4011+ if changed == 1
4012+ @cls_meth_ptypes[ci] = join_sep(all_ptypes, "|")
4013+ end
4014+ ci = ci + 1
4015+ end
4016+ end
4017+
37904018 def infer_all_returns
37914019 # Pre-pass: infer class method param types from body usage
37924020 infer_cls_meth_param_from_body
@@ -3794,6 +4022,8 @@
37944022 infer_constructor_types
37954023 # Update ivar types from constructor params
37964024 update_ivar_types_from_params
4025+ # Infer setter param types from ivar types
4026+ infer_writer_param_types
37974027
37984028 # Top-level methods
37994029 i = 0
@@ -4887,6 +5117,8 @@
48875117 end
48885118 # Scan for calls to other methods in same class
48895119 scan_cls_method_calls(ci, bid)
5120+ # Also scan for constructor calls to infer param types
5121+ scan_new_calls(bid)
48905122 pop_scope
48915123 @current_class_idx = -1
48925124 end
@@ -5133,7 +5365,7 @@
51335365
51345366 def emit_gc_runtime
51355367 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+ else
6241+ if @nd_type[sid] != "SuperNode"
6242+ # Compile other statements (e.g., method calls like @arr[0] = val)
6243+ compile_stmt(sid)
6244+ end
60086245 end
60096246 k = k + 1
60106247 end
@@ -6665,7 +6902,7 @@
66656902 emit_raw("")
66666903 emit_raw("int main(int argc,char**argv){")
66676904 if @needs_gc == 1
6668- emit_raw(" { volatile char _sb; sp_gc_stack_bottom = (char*)&_sb; }")
6905+ emit_raw(" volatile char _sb; sp_gc_stack_bottom = (char*)&_sb;")
66696906 end
66706907 emit_raw(" sp_argv.data=(const char**)(argv+1);sp_argv.len=argc-1;")
66716908
@@ -6835,6 +7072,26 @@
68357072 end
68367073 return "self->" + sanitize_ivar(@nd_name[nid])
68377074 end
7075+ if t == "InstanceVariableWriteNode"
7076+ val = compile_expr(@nd_expression[nid])
7077+ # Check if in module method
7078+ mi3 = 0
7079+ while mi3 < @module_names.length
7080+ 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 >= 0
7087+ return "(cst_" + cname3 + " = " + val + ")"
7088+ end
7089+ end
7090+ end
7091+ mi3 = mi3 + 1
7092+ end
7093+ return "(self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ")"
7094+ end
68387095 if t == "ConstantReadNode"
68397096 if @nd_name[nid] == "ARGV"
68407097 return "sp_argv"
@@ -6843,6 +7100,27 @@
68437100 if ci >= 0
68447101 return "cst_" + @nd_name[nid]
68457102 end
7103+ # Check if inside a module method and constant belongs to that module
7104+ mi3 = 0
7105+ while mi3 < @module_names.length
7106+ 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 >= 0
7112+ return "cst_" + cpname
7113+ end
7114+ end
7115+ # 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 >= 0
7119+ return "cst_" + cpname
7120+ end
7121+ end
7122+ mi3 = mi3 + 1
7123+ end
68467124 return @nd_name[nid]
68477125 end
68487126 if t == "ConstantPathNode"
@@ -6868,6 +7146,14 @@
68687146 return "INT64_MAX"
68697147 end
68707148 end
7149+ if rname == "Math"
7150+ if nname == "PI"
7151+ return "3.14159265358979323846"
7152+ end
7153+ if nname == "E"
7154+ return "2.71828182845904523536"
7155+ end
7156+ end
68717157 return cpname
68727158 end
68737159 return @nd_name[nid]
@@ -7263,6 +7549,41 @@
72637549 if mname == "__method__"
72647550 return "\"" + @current_method_name + "\""
72657551 end
7552+ if mname == "Integer"
7553+ args_id = @nd_arguments[nid]
7554+ if args_id >= 0
7555+ arg_ids = get_args(args_id)
7556+ if arg_ids.length > 0
7557+ 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 || default
7564+ 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+ else
7569+ return "((" + lc + ") ? (mrb_int)strtoll(" + lc + ", NULL, 10) : (mrb_int)strtoll(" + rc2 + ", NULL, 10))"
7570+ end
7571+ end
7572+ end
7573+ at = infer_type(a0)
7574+ if at == "string"
7575+ return "(mrb_int)strtoll(" + compile_expr(a0) + ", NULL, 10)"
7576+ end
7577+ if at == "argv"
7578+ return "(mrb_int)strtoll(" + compile_expr(a0) + ", NULL, 10)"
7579+ end
7580+ end
7581+ end
7582+ return "(mrb_int)(" + compile_arg0(nid) + ")"
7583+ end
7584+ if mname == "Float"
7585+ return "(mrb_float)(" + compile_arg0(nid) + ")"
7586+ end
72667587 if mname == "proc"
72677588 if @nd_block[nid] >= 0
72687589 @needs_proc = 1
@@ -7584,6 +7905,17 @@
75847905 if lt == "float"
75857906 return "(" + compile_expr(recv) + " / " + compile_arg0(nid) + ")"
75867907 end
7908+ # Check RHS for float
7909+ args_id = @nd_arguments[nid]
7910+ if args_id >= 0
7911+ aargs = get_args(args_id)
7912+ if aargs.length > 0
7913+ rt = infer_type(aargs[0])
7914+ if rt == "float"
7915+ return "((mrb_float)" + compile_expr(recv) + " / " + compile_arg0(nid) + ")"
7916+ end
7917+ end
7918+ end
75877919 return "sp_idiv(" + compile_expr(recv) + ", " + compile_arg0(nid) + ")"
75887920 end
75897921 if mname == "%"
@@ -8484,7 +8816,8 @@
84848816 return "sp_argv.len"
84858817 end
84868818 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 end
84898822 end
84908823 end
@@ -8496,6 +8829,30 @@
84968829 if mname == "sqrt"
84978830 return "sqrt(" + compile_arg0(nid) + ")"
84988831 end
8832+ if mname == "cos"
8833+ return "cos(" + compile_arg0(nid) + ")"
8834+ end
8835+ if mname == "sin"
8836+ return "sin(" + compile_arg0(nid) + ")"
8837+ end
8838+ if mname == "log"
8839+ return "log(" + compile_arg0(nid) + ")"
8840+ end
8841+ if mname == "log2"
8842+ return "log2(" + compile_arg0(nid) + ")"
8843+ end
8844+ if mname == "exp"
8845+ return "exp(" + compile_arg0(nid) + ")"
8846+ end
8847+ if mname == "atan2"
8848+ args_id = @nd_arguments[nid]
8849+ if args_id >= 0
8850+ arg_ids = get_args(args_id)
8851+ if arg_ids.length >= 2
8852+ return "atan2(" + compile_expr(arg_ids[0]) + ", " + compile_expr(arg_ids[1]) + ")"
8853+ end
8854+ end
8855+ end
84998856 end
85008857 # File operations
85018858 if rcname == "File"
@@ -8747,7 +9104,17 @@
87479104 # Method call
87489105 owner = find_method_owner(ci, mname)
87499106 if owner != ""
8750- ca = compile_call_args(nid)
9107+ oci2 = find_class_idx(owner)
9108+ midx2 = -1
9109+ if oci2 >= 0
9110+ midx2 = cls_find_method_direct(oci2, mname)
9111+ end
9112+ ca = ""
9113+ if midx2 >= 0
9114+ ca = compile_typed_call_args(nid, oci2, midx2)
9115+ else
9116+ ca = compile_call_args(nid)
9117+ end
87519118 if owner == cname
87529119 if ca != ""
87539120 return "sp_" + owner + "_" + sanitize_name(mname) + "(" + rc + ", " + ca + ")"
@@ -8765,6 +9132,62 @@
87659132 end
87669133 end
87679134
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 dispatch
9137+ if recv_type == "int"
9138+ ci2 = 0
9139+ while ci2 < @cls_names.length
9140+ cname2 = @cls_names[ci2]
9141+ readers2 = @cls_attr_readers[ci2].split(";")
9142+ found_reader = 0
9143+ j2 = 0
9144+ while j2 < readers2.length
9145+ if readers2[j2] == mname
9146+ found_reader = 1
9147+ end
9148+ j2 = j2 + 1
9149+ end
9150+ if found_reader == 1
9151+ return "((sp_" + cname2 + " *)" + rc + ")->" + mname
9152+ end
9153+ # Check writers
9154+ if mname.length > 1
9155+ if mname[mname.length - 1] == "="
9156+ bname2 = mname[0, mname.length - 1]
9157+ writers2 = @cls_attr_writers[ci2].split(";")
9158+ j2 = 0
9159+ while j2 < writers2.length
9160+ if writers2[j2] == bname2
9161+ return "(((sp_" + cname2 + " *)" + rc + ")->" + bname2 + " = " + compile_arg0(nid) + ")"
9162+ end
9163+ j2 = j2 + 1
9164+ end
9165+ end
9166+ end
9167+ # Check methods
9168+ owner2 = find_method_owner(ci2, mname)
9169+ if owner2 != ""
9170+ oci3 = find_class_idx(owner2)
9171+ midx3 = -1
9172+ if oci3 >= 0
9173+ midx3 = cls_find_method_direct(oci3, mname)
9174+ end
9175+ ca2 = ""
9176+ if midx3 >= 0
9177+ ca2 = compile_typed_call_args(nid, oci3, midx3)
9178+ else
9179+ ca2 = compile_call_args(nid)
9180+ end
9181+ if ca2 != ""
9182+ return "sp_" + owner2 + "_" + sanitize_name(mname) + "((sp_" + owner2 + " *)" + rc + ", " + ca2 + ")"
9183+ else
9184+ return "sp_" + owner2 + "_" + sanitize_name(mname) + "((sp_" + owner2 + " *)" + rc + ")"
9185+ end
9186+ end
9187+ ci2 = ci2 + 1
9188+ end
9189+ end
9190+
87689191 "0"
87699192 end
87709193
@@ -9087,6 +9510,48 @@
90879510 result
90889511 end
90899512
9513+ def compile_typed_call_args(nid, target_ci, target_midx)
9514+ # Like compile_call_args but casts arguments to match target method param types
9515+ args_id = @nd_arguments[nid]
9516+ if args_id < 0
9517+ return ""
9518+ end
9519+ arg_ids = get_args(args_id)
9520+ all_ptypes = @cls_meth_ptypes[target_ci].split("|")
9521+ ptypes = "".split(",")
9522+ if target_midx < all_ptypes.length
9523+ ptypes = all_ptypes[target_midx].split(",")
9524+ end
9525+ result = ""
9526+ k = 0
9527+ while k < arg_ids.length
9528+ if k > 0
9529+ result = result + ", "
9530+ end
9531+ aexpr = compile_expr(arg_ids[k])
9532+ at = infer_type(arg_ids[k])
9533+ if k < ptypes.length
9534+ pt = ptypes[k]
9535+ if at == "int"
9536+ if is_obj_type(pt) == 1
9537+ # Cast int to object pointer
9538+ pcname = pt[4, pt.length - 4]
9539+ aexpr = "(sp_" + pcname + " *)" + aexpr
9540+ end
9541+ end
9542+ if is_obj_type(at) == 1
9543+ if pt == "int"
9544+ # Cast object pointer to int
9545+ aexpr = "(mrb_int)" + aexpr
9546+ end
9547+ end
9548+ end
9549+ result = result + aexpr
9550+ k = k + 1
9551+ end
9552+ result
9553+ end
9554+
90909555 def find_method_owner(ci, mname)
90919556 if ci < 0
90929557 return ""
@@ -9340,7 +9805,27 @@
93409805 end
93419806 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 method
9809+ mod_ivar = 0
9810+ mi3 = 0
9811+ while mi3 < @module_names.length
9812+ 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 >= 0
9819+ emit(" cst_" + cname3 + " = " + val + ";")
9820+ mod_ivar = 1
9821+ end
9822+ end
9823+ end
9824+ mi3 = mi3 + 1
9825+ end
9826+ if mod_ivar == 0
9827+ emit(" self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ";")
9828+ end
93449829 return
93459830 end
93469831 if t == "InstanceVariableOperatorWriteNode"
@@ -9442,7 +9927,8 @@
94429927 while k < elems.length
94439928 tmp = new_temp
94449929 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 + 1
94479933 end
94489934 # Now assign
@@ -9453,6 +9939,29 @@
94539939 if @nd_type[tid] == "LocalVariableTargetNode"
94549940 emit(" lv_" + @nd_name[tid] + " = " + tmps[k] + ";")
94559941 end
9942+ if @nd_type[tid] == "InstanceVariableTargetNode"
9943+ iname = @nd_name[tid]
9944+ # Check if in module method
9945+ mod_ivar = 0
9946+ mi3 = 0
9947+ while mi3 < @module_names.length
9948+ 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 >= 0
9954+ emit(" cst_" + cname3 + " = " + tmps[k] + ";")
9955+ mod_ivar = 1
9956+ end
9957+ end
9958+ end
9959+ mi3 = mi3 + 1
9960+ end
9961+ if mod_ivar == 0
9962+ emit(" self->" + sanitize_ivar(iname) + " = " + tmps[k] + ";")
9963+ end
9964+ end
94569965 end
94579966 k = k + 1
94589967 end
@@ -9828,6 +10337,31 @@
982810337 return
982910338 end
983010339 end
10340+ if mname == "printf"
10341+ if recv < 0
10342+ args_id = @nd_arguments[nid]
10343+ if args_id >= 0
10344+ arg_ids = get_args(args_id)
10345+ if arg_ids.length >= 1
10346+ # First arg is format string
10347+ fmt_expr = compile_expr(arg_ids[0])
10348+ rest_args = ""
10349+ k = 1
10350+ while k < arg_ids.length
10351+ at = infer_type(arg_ids[k])
10352+ if at == "int"
10353+ rest_args = rest_args + ", (int)" + compile_expr(arg_ids[k])
10354+ else
10355+ rest_args = rest_args + ", " + compile_expr(arg_ids[k])
10356+ end
10357+ k = k + 1
10358+ end
10359+ emit(" printf(" + fmt_expr + rest_args + ");")
10360+ return
10361+ end
10362+ end
10363+ end
10364+ end
983110365
983210366 # File.open with block
983310367 if mname == "open"
@@ -10430,7 +10964,16 @@
1043010964 val = compile_expr(arg_ids[1])
1043110965 end
1043210966 if rt == "int_array"
10433- emit(" sp_IntArray_set(" + rc + ", " + idx + ", " + val + ");")
10967+ # Check if value is an object pointer - needs cast
10968+ vt = "int"
10969+ if arg_ids.length >= 2
10970+ vt = infer_type(arg_ids[1])
10971+ end
10972+ if is_obj_type(vt) == 1
10973+ emit(" sp_IntArray_set(" + rc + ", " + idx + ", (mrb_int)" + val + ");")
10974+ else
10975+ emit(" sp_IntArray_set(" + rc + ", " + idx + ", " + val + ");")
10976+ end
1043410977 return
1043510978 end
1043610979 if rt == "float_array"