Commit 69ff099a
69ff099a10cfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: value type support for small stack-allocated classes
Classes with <=4 ivars (int/float/bool/string/value-type objects) are automatically stack-allocated as value types when safe:
- No self-mutating methods (excluding initialize and attr_writers)
- No instances mutated through method parameters
- No inheritance (no parent or subclasses)
Value type objects are passed by value, avoiding GC allocation and rooting. Programs using only value types emit no GC runtime at all.
For ao_render: Vec, Sphere, Plane, Ray, Scene become value types. Rand (self-mutating), Basis (self-mutating), Isect (param-mutated) remain heap-allocated.
Also includes:
- auto_register_attr_readers/writers for manual def x; @x; end patterns
- param-mutation detection via AST scanning
- self-mutation detection via AST scanning
- GC runtime elimination when no heap classes exist (recalc_needs_gc)
- attr_writer expression fix (comma-0 return for type safety)
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 69ff099a10cf
- Parent
- 290b4625ec41
spinel_codegen.rbmodifiedView file
@@ -93,6 +93,7 @@
9393
@cls_cmeth_ptypes = "".split(",")9494
@cls_cmeth_returns = "".split(",")9595
@cls_cmeth_bodies = "".split(",")96+
@cls_is_value_type = []9697
9798
# ---- Constants (parallel arrays) ----9899
@const_names = "".split(",")@@ -515,8 +516,6 @@
515516
end516517
517518
def set_array_field(nid, field, ids_str)518-
$stderr.print " "519-
$stderr.print " "520519
if field == "body"521520
@nd_stmts[nid] = ids_str522521
end@@ -2034,11 +2033,27 @@
20342033
return 120352034
end20362035
if is_obj_type(t) == 12036+
cname = t[4, t.length - 4]2037+
ci = find_class_idx(cname)2038+
if ci >= 0 && @cls_is_value_type[ci] == 12039+
return 02040+
end20372041
return 120382042
end20392043
020402044
end20412045
2046+
def is_value_type_obj(t)2047+
if is_obj_type(t) == 12048+
cname = t[4, t.length - 4]2049+
ci = find_class_idx(cname)2050+
if ci >= 0 && @cls_is_value_type[ci] == 12051+
return 12052+
end2053+
end2054+
02055+
end2056+
20422057
# ---- C type mapping ----20432058
def c_type(t)20442059
if t == "range"@@ -2097,6 +2112,10 @@
20972112
end20982113
if is_obj_type(t) == 120992114
cname = t[4, t.length - 4]2115+
ci = find_class_idx(cname)2116+
if ci >= 0 && @cls_is_value_type[ci] == 12117+
return "sp_" + cname2118+
end21002119
return "sp_" + cname + " *"21012120
end21022121
"mrb_int"@@ -2139,6 +2158,9 @@
21392158
if type_is_pointer(t) == 121402159
return "NULL"21412160
end2161+
if is_value_type_obj(t) == 12162+
return "{0}"2163+
end21422164
"0"21432165
end21442166
@@ -2352,6 +2374,7 @@
23522374
23532375
ci = @cls_names.length23542376
@cls_names.push(cname)2377+
@cls_is_value_type.push(0)23552378
@cls_parents.push(parent)23562379
# Initialize struct fields as ivars23572380
ivar_names = ""@@ -2413,7 +2436,6 @@
24132436
@cls_cmeth_returns.push("")24142437
@cls_cmeth_bodies.push("")24152438
@cls_meth_has_yield.push("")2416-
@needs_gc = 124172439
24182440
# Collect class body24192441
body = @nd_body[nid]@@ -3237,6 +3259,7 @@
32373259
# Generate a synthetic class from Struct.new(:field1, :field2, ...)32383260
ci = @cls_names.length32393261
@cls_names.push(cname)3262+
@cls_is_value_type.push(0)32403263
@cls_parents.push("")32413264
@cls_ivar_names.push("")32423265
@cls_ivar_types.push("")@@ -3254,7 +3277,6 @@
32543277
@cls_cmeth_returns.push("")32553278
@cls_cmeth_bodies.push("")32563279
@cls_meth_has_yield.push("")3257-
@needs_gc = 132583280
32593281
# Get field names from symbol args (skip keyword_init hash)32603282
args_id = @nd_arguments[call_nid]@@ -5906,6 +5928,8 @@
59065928
def generate_code59075929
stmts = get_body_stmts(@root_id)59085930
5931+
detect_value_types5932+
recalc_needs_gc59095933
emit_header59105934
if @needs_gc == 159115935
emit_gc_runtime@@ -6487,163 +6511,672 @@
64876511
end64886512
end64896513
6490-
def emit_class_structs6491-
# Forward declare typedefs6492-
i = 06493-
while i < @cls_names.length6494-
emit_raw("typedef struct sp_" + @cls_names[i] + "_s sp_" + @cls_names[i] + ";")6495-
i = i + 16514+
def is_value_type_ivar(t)6515+
if t == "int" || t == "float" || t == "bool" || t == "string"6516+
return 164966517
end6497-
if @cls_names.length > 06498-
emit_raw("")6499-
end6500-
# Struct definitions6501-
i = 06502-
while i < @cls_names.length6503-
emit_raw("struct sp_" + @cls_names[i] + "_s {")6504-
emit_class_fields(i)6505-
emit_raw("};")6506-
emit_raw("")6507-
i = i + 16508-
end6509-
end6510-
6511-
def emit_class_fields(ci)6512-
# Parent fields first6513-
if @cls_parents[ci] != ""6514-
pi = find_class_idx(@cls_parents[ci])6515-
if pi >= 06516-
emit_parent_fields(pi)6517-
end6518-
end6519-
# Own fields (skip those inherited from parent)6520-
names = @cls_ivar_names[ci].split(";")6521-
types = @cls_ivar_types[ci].split(";")6522-
j = 06523-
while j < names.length6524-
iname = names[j]6525-
itype = "int"6526-
if j < types.length6527-
itype = types[j]6528-
end6529-
# Skip if in parent chain6530-
if @cls_parents[ci] != ""6531-
pi = find_class_idx(@cls_parents[ci])6532-
if pi >= 06533-
if ivar_in_chain(pi, iname) == 16534-
j = j + 16535-
next6536-
end6518+
if is_obj_type(t) == 16519+
cname = t[4, t.length - 4]6520+
ci2 = find_class_idx(cname)6521+
if ci2 >= 06522+
if @cls_is_value_type[ci2] == 16523+
return 165376524
end65386525
end6539-
fname = sanitize_ivar(iname)6540-
emit_raw(" " + c_type(itype) + " " + fname + ";")6541-
j = j + 165426526
end6527+
065436528
end65446529
6545-
def emit_parent_fields(ci)6546-
if @cls_parents[ci] != ""6547-
pi = find_class_idx(@cls_parents[ci])6548-
if pi >= 06549-
emit_parent_fields(pi)6530+
def self_arrow6531+
if @current_class_idx >= 06532+
if @cls_is_value_type[@current_class_idx] == 16533+
return "self."65506534
end65516535
end6552-
names = @cls_ivar_names[ci].split(";")6553-
types = @cls_ivar_types[ci].split(";")6554-
j = 06555-
while j < names.length6556-
iname = names[j]6557-
itype = "int"6558-
if j < types.length6559-
itype = types[j]6560-
end6561-
if @cls_parents[ci] != ""6562-
pi = find_class_idx(@cls_parents[ci])6563-
if pi >= 06564-
if ivar_in_chain(pi, iname) == 16565-
j = j + 16566-
next6567-
end6568-
end6569-
end6570-
fname = sanitize_ivar(iname)6571-
emit_raw(" " + c_type(itype) + " " + fname + ";")6572-
j = j + 16573-
end6536+
"self->"65746537
end65756538
6576-
def ivar_in_chain(ci, iname)6577-
names = @cls_ivar_names[ci].split(";")6578-
k = 06579-
while k < names.length6580-
if names[k] == iname6539+
def check_ivar_write_child(child_nid)6540+
if child_nid >= 06541+
if subtree_has_ivar_write(child_nid) == 165816542
return 165826543
end6583-
k = k + 165846544
end6585-
if @cls_parents[ci] != ""6586-
pi = find_class_idx(@cls_parents[ci])6587-
if pi >= 06588-
return ivar_in_chain(pi, iname)6545+
06546+
end6547+
6548+
def check_ivar_write_list(list_str)6549+
if list_str != ""6550+
parts = list_str.split(",")6551+
pi = 06552+
while pi < parts.length6553+
id = parts[pi].to_i6554+
if id > 0 && subtree_has_ivar_write(id) == 16555+
return 16556+
end6557+
pi = pi + 165896558
end65906559
end65916560
065926561
end65936562
6594-
def ivar_is_gc_ptr(t)6595-
if is_obj_type(t) == 16596-
return 16563+
def subtree_has_ivar_write(nid)6564+
if nid < 0 || nid >= @nd_count6565+
return 065976566
end6598-
if type_is_pointer(t) == 16567+
t = @nd_type[nid]6568+
if t == "InstanceVariableWriteNode" || t == "InstanceVariableOperatorWriteNode" || t == "InstanceVariableTargetNode"65996569
return 166006570
end6571+
# Check integer child references6572+
if check_ivar_write_child(@nd_body[nid]) == 1; return 1; end6573+
if check_ivar_write_child(@nd_expression[nid]) == 1; return 1; end6574+
if check_ivar_write_child(@nd_predicate[nid]) == 1; return 1; end6575+
if check_ivar_write_child(@nd_subsequent[nid]) == 1; return 1; end6576+
if check_ivar_write_child(@nd_else_clause[nid]) == 1; return 1; end6577+
if check_ivar_write_child(@nd_left[nid]) == 1; return 1; end6578+
if check_ivar_write_child(@nd_right[nid]) == 1; return 1; end6579+
if check_ivar_write_child(@nd_target[nid]) == 1; return 1; end6580+
if check_ivar_write_child(@nd_rescue_clause[nid]) == 1; return 1; end6581+
if check_ivar_write_child(@nd_ensure_clause[nid]) == 1; return 1; end6582+
if check_ivar_write_child(@nd_collection[nid]) == 1; return 1; end6583+
if check_ivar_write_child(@nd_rest[nid]) == 1; return 1; end6584+
if check_ivar_write_child(@nd_arguments[nid]) == 1; return 1; end6585+
if check_ivar_write_child(@nd_block[nid]) == 1; return 1; end6586+
# Check string-list children (comma-separated node IDs)6587+
if check_ivar_write_list(@nd_stmts[nid]) == 1; return 1; end6588+
if check_ivar_write_list(@nd_elements[nid]) == 1; return 1; end6589+
if check_ivar_write_list(@nd_targets[nid]) == 1; return 1; end66016590
066026591
end66036592
6604-
def class_has_ptr_ivars(ci)6605-
names = @cls_ivar_names[ci].split(";")6606-
types = @cls_ivar_types[ci].split(";")6607-
j = 06608-
while j < names.length6609-
if j < types.length6610-
if ivar_is_gc_ptr(types[j]) == 16611-
return 16593+
def is_simple_writer_method(mn, bid)6594+
# Check if method is a simple attr_writer pattern: def x=(v); @x = v; end6595+
if mn.length <= 1 || mn[mn.length - 1] != "="6596+
return 06597+
end6598+
if bid < 0 || bid >= @nd_count6599+
return 06600+
end6601+
# Body should be a StatementsNode with a single InstanceVariableWriteNode6602+
t = @nd_type[bid]6603+
if t == "StatementsNode"6604+
stmts = @nd_stmts[bid]6605+
if stmts != ""6606+
parts = stmts.split(",")6607+
if parts.length == 16608+
sid = parts[0].to_i6609+
if sid >= 0 && sid < @nd_count6610+
if @nd_type[sid] == "InstanceVariableWriteNode"6611+
return 16612+
end6613+
end66126614
end66136615
end6614-
j = j + 166156616
end6616-
if @cls_parents[ci] != ""6617-
pi = find_class_idx(@cls_parents[ci])6618-
if pi >= 06619-
return class_has_ptr_ivars(pi)6620-
end6617+
# Body might be a single InstanceVariableWriteNode directly6618+
if t == "InstanceVariableWriteNode"6619+
return 166216620
end66226621
066236622
end66246623
6625-
def emit_gc_scan_functions6626-
i = 06627-
while i < @cls_names.length6628-
if class_has_ptr_ivars(i) == 16629-
cname = @cls_names[i]6630-
emit_raw("static void sp_" + cname + "_gc_scan(void *p) {")6631-
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)p;")6632-
names = @cls_ivar_names[i].split(";")6633-
types = @cls_ivar_types[i].split(";")6634-
j = 06635-
while j < names.length6636-
if j < types.length6637-
if ivar_is_gc_ptr(types[j]) == 16638-
emit_raw(" if (self->" + sanitize_ivar(names[j]) + ") sp_gc_mark(self->" + sanitize_ivar(names[j]) + ");")6624+
def cls_has_self_mutating_methods(ci)6625+
mnames_str = @cls_meth_names[ci]6626+
if mnames_str == ""6627+
return 06628+
end6629+
mnames = mnames_str.split(";")6630+
bodies = @cls_meth_bodies[ci].split(";")6631+
writers = @cls_attr_writers[ci].split(";")6632+
mi = 06633+
while mi < mnames.length6634+
mn = mnames[mi]6635+
if mn != "initialize"6636+
# Skip registered attr_writers6637+
is_writer = 06638+
bname = ""6639+
if mn.length > 1 && mn[mn.length - 1] == "="6640+
bname = mn[0, mn.length - 1]6641+
wi = 06642+
while wi < writers.length6643+
if writers[wi] == bname6644+
is_writer = 166396645
end6646+
wi = wi + 166406647
end6641-
j = j + 166426648
end6643-
# Also scan parent fields6644-
if @cls_parents[i] != ""6645-
pi = find_class_idx(@cls_parents[i])6646-
if pi >= 06649+
# Also skip simple writer methods: def x=(v); @x = v; end6650+
if is_writer == 0 && mi < bodies.length6651+
bid = bodies[mi].to_i6652+
if is_simple_writer_method(mn, bid) == 16653+
is_writer = 16654+
end6655+
end6656+
if is_writer == 0 && mi < bodies.length6657+
bid = bodies[mi].to_i6658+
if bid >= 0 && subtree_has_ivar_write(bid) == 16659+
return 16660+
end6661+
end6662+
end6663+
mi = mi + 16664+
end6665+
06666+
end6667+
6668+
def auto_register_attr_writers6669+
# Detect manual attr_writer patterns: def x=(v); @x = v; end6670+
# and register them as attr_writers for direct field access6671+
i = 06672+
while i < @cls_names.length6673+
mnames_str = @cls_meth_names[i]6674+
if mnames_str != ""6675+
mnames = mnames_str.split(";")6676+
bodies = @cls_meth_bodies[i].split(";")6677+
writers = @cls_attr_writers[i].split(";")6678+
mi = 06679+
while mi < mnames.length6680+
mn = mnames[mi]6681+
bname = ""6682+
if mn.length > 1 && mn[mn.length - 1] == "="6683+
bname = mn[0, mn.length - 1]6684+
# Check if already registered6685+
already = 06686+
wi = 06687+
while wi < writers.length6688+
if writers[wi] == bname6689+
already = 16690+
end6691+
wi = wi + 16692+
end6693+
if already == 0 && mi < bodies.length6694+
bid = bodies[mi].to_i6695+
if is_simple_writer_method(mn, bid) == 16696+
append_attr_writer(i, bname)6697+
# Also ensure the corresponding reader exists6698+
readers = @cls_attr_readers[i].split(";")6699+
has_reader = 06700+
ri = 06701+
while ri < readers.length6702+
if readers[ri] == bname6703+
has_reader = 16704+
end6705+
ri = ri + 16706+
end6707+
end6708+
end6709+
end6710+
mi = mi + 16711+
end6712+
end6713+
i = i + 16714+
end6715+
end6716+
6717+
def is_simple_reader_method(mn, bid)6718+
# Check if method is a simple attr_reader pattern: def x; @x; end6719+
if bid < 0 || bid >= @nd_count6720+
return 06721+
end6722+
t = @nd_type[bid]6723+
if t == "StatementsNode"6724+
stmts = @nd_stmts[bid]6725+
if stmts != ""6726+
parts = stmts.split(",")6727+
if parts.length == 16728+
sid = parts[0].to_i6729+
if sid >= 0 && sid < @nd_count6730+
if @nd_type[sid] == "InstanceVariableReadNode"6731+
iname = @nd_name[sid]6732+
if iname == "@" + mn6733+
return 16734+
end6735+
end6736+
end6737+
end6738+
end6739+
end6740+
if t == "InstanceVariableReadNode"6741+
iname = @nd_name[bid]6742+
if iname == "@" + mn6743+
return 16744+
end6745+
end6746+
06747+
end6748+
6749+
def auto_register_attr_readers6750+
i = 06751+
while i < @cls_names.length6752+
mnames_str = @cls_meth_names[i]6753+
if mnames_str != ""6754+
mnames = mnames_str.split(";")6755+
bodies = @cls_meth_bodies[i].split(";")6756+
readers = @cls_attr_readers[i].split(";")6757+
mi = 06758+
while mi < mnames.length6759+
mn = mnames[mi]6760+
if mn != "initialize" && mn.length > 0 && mn[mn.length - 1] != "="6761+
already = 06762+
ri = 06763+
while ri < readers.length6764+
if readers[ri] == mn6765+
already = 16766+
end6767+
ri = ri + 16768+
end6769+
if already == 0 && mi < bodies.length6770+
bid = bodies[mi].to_i6771+
if is_simple_reader_method(mn, bid) == 16772+
append_attr_reader(i, mn)6773+
end6774+
end6775+
end6776+
mi = mi + 16777+
end6778+
end6779+
i = i + 16780+
end6781+
end6782+
6783+
def check_setter_on_params_child(child_nid, param_names)6784+
if child_nid >= 06785+
r = subtree_has_setter_on_params(child_nid, param_names)6786+
if r != ""6787+
return r6788+
end6789+
end6790+
""6791+
end6792+
6793+
def check_setter_on_params_list(list_str, param_names)6794+
if list_str != ""6795+
parts = list_str.split(",")6796+
pi2 = 06797+
while pi2 < parts.length6798+
id = parts[pi2].to_i6799+
if id > 06800+
r = subtree_has_setter_on_params(id, param_names)6801+
if r != ""6802+
return r6803+
end6804+
end6805+
pi2 = pi2 + 16806+
end6807+
end6808+
""6809+
end6810+
6811+
def subtree_has_setter_on_params(nid, param_names)6812+
if nid < 0 || nid >= @nd_count6813+
return ""6814+
end6815+
t = @nd_type[nid]6816+
# Check: CallNode with setter name, receiver is a param6817+
if t == "CallNode"6818+
mn = @nd_name[nid]6819+
if mn != "" && mn.length > 1 && mn[mn.length - 1] == "="6820+
recv = @nd_receiver[nid]6821+
if recv >= 0 && @nd_type[recv] == "LocalVariableReadNode"6822+
vname = @nd_name[recv]6823+
pi2 = 06824+
while pi2 < param_names.length6825+
if param_names[pi2] == vname6826+
return vname6827+
end6828+
pi2 = pi2 + 16829+
end6830+
end6831+
end6832+
end6833+
# Recurse into integer children6834+
r = check_setter_on_params_child(@nd_body[nid], param_names)6835+
if r != ""; return r; end6836+
r = check_setter_on_params_child(@nd_expression[nid], param_names)6837+
if r != ""; return r; end6838+
r = check_setter_on_params_child(@nd_predicate[nid], param_names)6839+
if r != ""; return r; end6840+
r = check_setter_on_params_child(@nd_subsequent[nid], param_names)6841+
if r != ""; return r; end6842+
r = check_setter_on_params_child(@nd_else_clause[nid], param_names)6843+
if r != ""; return r; end6844+
r = check_setter_on_params_child(@nd_left[nid], param_names)6845+
if r != ""; return r; end6846+
r = check_setter_on_params_child(@nd_right[nid], param_names)6847+
if r != ""; return r; end6848+
r = check_setter_on_params_child(@nd_target[nid], param_names)6849+
if r != ""; return r; end6850+
r = check_setter_on_params_child(@nd_rescue_clause[nid], param_names)6851+
if r != ""; return r; end6852+
r = check_setter_on_params_child(@nd_ensure_clause[nid], param_names)6853+
if r != ""; return r; end6854+
r = check_setter_on_params_child(@nd_collection[nid], param_names)6855+
if r != ""; return r; end6856+
r = check_setter_on_params_child(@nd_rest[nid], param_names)6857+
if r != ""; return r; end6858+
r = check_setter_on_params_child(@nd_arguments[nid], param_names)6859+
if r != ""; return r; end6860+
r = check_setter_on_params_child(@nd_block[nid], param_names)6861+
if r != ""; return r; end6862+
# Recurse into string-list children6863+
r = check_setter_on_params_list(@nd_stmts[nid], param_names)6864+
if r != ""; return r; end6865+
r = check_setter_on_params_list(@nd_elements[nid], param_names)6866+
if r != ""; return r; end6867+
r = check_setter_on_params_list(@nd_targets[nid], param_names)6868+
if r != ""; return r; end6869+
""6870+
end6871+
6872+
def detect_param_mutated_types6873+
# Find classes whose instances are mutated when passed as method parameters6874+
@param_mutated_types = "".split(",")6875+
i = 06876+
while i < @cls_names.length6877+
mnames_str = @cls_meth_names[i]6878+
if mnames_str != ""6879+
mnames = mnames_str.split(";")6880+
all_params = @cls_meth_params[i].split("|")6881+
all_ptypes = @cls_meth_ptypes[i].split("|")6882+
bodies = @cls_meth_bodies[i].split(";")6883+
mi = 06884+
while mi < mnames.length6885+
if mi < bodies.length && mi < all_params.length6886+
bid = bodies[mi].to_i6887+
pnames = all_params[mi].split(",")6888+
ptypes = "".split(",")6889+
if mi < all_ptypes.length6890+
ptypes = all_ptypes[mi].split(",")6891+
end6892+
# Collect object-type param names6893+
obj_param_names = "".split(",")6894+
obj_param_types = "".split(",")6895+
pj = 06896+
while pj < pnames.length6897+
pt = "int"6898+
if pj < ptypes.length6899+
pt = ptypes[pj]6900+
end6901+
if is_obj_type(pt) == 16902+
obj_param_names.push(pnames[pj])6903+
obj_param_types.push(pt)6904+
end6905+
pj = pj + 16906+
end6907+
if obj_param_names.length > 0 && bid >= 06908+
mutated_name = subtree_has_setter_on_params(bid, obj_param_names)6909+
if mutated_name != ""6910+
# Find the type of the mutated param6911+
pj = 06912+
while pj < obj_param_names.length6913+
if obj_param_names[pj] == mutated_name6914+
@param_mutated_types.push(obj_param_types[pj])6915+
end6916+
pj = pj + 16917+
end6918+
end6919+
end6920+
end6921+
mi = mi + 16922+
end6923+
end6924+
i = i + 16925+
end6926+
end6927+
6928+
def recalc_needs_gc6929+
# Recalculate @needs_gc: only needed if non-value-type classes are used6930+
# Save and reset, then re-enable if needed6931+
saved = @needs_gc6932+
@needs_gc = 06933+
# Non-value-type class exists → GC needed6934+
i = 06935+
while i < @cls_names.length6936+
if @cls_is_value_type[i] == 06937+
@needs_gc = 16938+
end6939+
i = i + 16940+
end6941+
# If there were other GC triggers (arrays, hashes, etc.) but no heap classes,6942+
# those built-in types handle their own memory (malloc/free), not GC.6943+
# However, IntArray/StrArray etc. ARE GC-allocated, so we need to check.6944+
if @needs_gc == 06945+
if @needs_int_array == 1 || @needs_float_array == 1 || @needs_str_array == 16946+
@needs_gc = 16947+
end6948+
if @needs_str_int_hash == 1 || @needs_str_str_hash == 16949+
@needs_gc = 16950+
end6951+
if @needs_mutable_str == 1 || @needs_stringio == 16952+
@needs_gc = 16953+
end6954+
if @needs_rb_value == 1 || @needs_lambda == 16955+
@needs_gc = 16956+
end6957+
end6958+
end6959+
6960+
def detect_value_types6961+
auto_register_attr_readers6962+
auto_register_attr_writers6963+
detect_param_mutated_types6964+
# Multiple passes: value type detection depends on other classes6965+
2.times do6966+
i = 06967+
while i < @cls_names.length6968+
names = @cls_ivar_names[i].split(";")6969+
types = @cls_ivar_types[i].split(";")6970+
if names.length > 0 && names.length <= 46971+
all_val = 16972+
j = 06973+
while j < types.length6974+
if is_value_type_ivar(types[j]) == 06975+
all_val = 06976+
end6977+
j = j + 16978+
end6979+
# Exclude classes with self-mutating methods6980+
if all_val == 16981+
if cls_has_self_mutating_methods(i) == 16982+
all_val = 06983+
end6984+
end6985+
# Exclude classes involved in inheritance6986+
if all_val == 16987+
# Has a parent class6988+
if @cls_parents[i] != ""6989+
all_val = 06990+
end6991+
# Has subclasses6992+
si = 06993+
while si < @cls_names.length6994+
if @cls_parents[si] == @cls_names[i]6995+
all_val = 06996+
end6997+
si = si + 16998+
end6999+
end7000+
# Exclude classes whose instances are param-mutated7001+
if all_val == 17002+
type_str = "obj_" + @cls_names[i]7003+
pmi = 07004+
while pmi < @param_mutated_types.length7005+
if @param_mutated_types[pmi] == type_str7006+
all_val = 07007+
end7008+
pmi = pmi + 17009+
end7010+
end7011+
if all_val == 17012+
@cls_is_value_type[i] = 17013+
end7014+
end7015+
i = i + 17016+
end7017+
end7018+
end7019+
7020+
def emit_class_structs7021+
# Forward declare typedefs7022+
i = 07023+
while i < @cls_names.length7024+
emit_raw("typedef struct sp_" + @cls_names[i] + "_s sp_" + @cls_names[i] + ";")7025+
i = i + 17026+
end7027+
if @cls_names.length > 07028+
emit_raw("")7029+
end7030+
# Struct definitions7031+
i = 07032+
while i < @cls_names.length7033+
emit_raw("struct sp_" + @cls_names[i] + "_s {")7034+
emit_class_fields(i)7035+
emit_raw("};")7036+
emit_raw("")7037+
i = i + 17038+
end7039+
end7040+
7041+
def emit_class_fields(ci)7042+
# Parent fields first7043+
if @cls_parents[ci] != ""7044+
pi = find_class_idx(@cls_parents[ci])7045+
if pi >= 07046+
emit_parent_fields(pi)7047+
end7048+
end7049+
# Own fields (skip those inherited from parent)7050+
names = @cls_ivar_names[ci].split(";")7051+
types = @cls_ivar_types[ci].split(";")7052+
j = 07053+
while j < names.length7054+
iname = names[j]7055+
itype = "int"7056+
if j < types.length7057+
itype = types[j]7058+
end7059+
# Skip if in parent chain7060+
if @cls_parents[ci] != ""7061+
pi = find_class_idx(@cls_parents[ci])7062+
if pi >= 07063+
if ivar_in_chain(pi, iname) == 17064+
j = j + 17065+
next7066+
end7067+
end7068+
end7069+
fname = sanitize_ivar(iname)7070+
emit_raw(" " + c_type(itype) + " " + fname + ";")7071+
j = j + 17072+
end7073+
end7074+
7075+
def emit_parent_fields(ci)7076+
if @cls_parents[ci] != ""7077+
pi = find_class_idx(@cls_parents[ci])7078+
if pi >= 07079+
emit_parent_fields(pi)7080+
end7081+
end7082+
names = @cls_ivar_names[ci].split(";")7083+
types = @cls_ivar_types[ci].split(";")7084+
j = 07085+
while j < names.length7086+
iname = names[j]7087+
itype = "int"7088+
if j < types.length7089+
itype = types[j]7090+
end7091+
if @cls_parents[ci] != ""7092+
pi = find_class_idx(@cls_parents[ci])7093+
if pi >= 07094+
if ivar_in_chain(pi, iname) == 17095+
j = j + 17096+
next7097+
end7098+
end7099+
end7100+
fname = sanitize_ivar(iname)7101+
emit_raw(" " + c_type(itype) + " " + fname + ";")7102+
j = j + 17103+
end7104+
end7105+
7106+
def ivar_in_chain(ci, iname)7107+
names = @cls_ivar_names[ci].split(";")7108+
k = 07109+
while k < names.length7110+
if names[k] == iname7111+
return 17112+
end7113+
k = k + 17114+
end7115+
if @cls_parents[ci] != ""7116+
pi = find_class_idx(@cls_parents[ci])7117+
if pi >= 07118+
return ivar_in_chain(pi, iname)7119+
end7120+
end7121+
07122+
end7123+
7124+
def ivar_is_gc_ptr(t)7125+
if is_obj_type(t) == 17126+
if is_value_type_obj(t) == 17127+
return 07128+
end7129+
return 17130+
end7131+
if type_is_pointer(t) == 17132+
return 17133+
end7134+
07135+
end7136+
7137+
def class_has_ptr_ivars(ci)7138+
names = @cls_ivar_names[ci].split(";")7139+
types = @cls_ivar_types[ci].split(";")7140+
j = 07141+
while j < names.length7142+
if j < types.length7143+
if ivar_is_gc_ptr(types[j]) == 17144+
return 17145+
end7146+
end7147+
j = j + 17148+
end7149+
if @cls_parents[ci] != ""7150+
pi = find_class_idx(@cls_parents[ci])7151+
if pi >= 07152+
return class_has_ptr_ivars(pi)7153+
end7154+
end7155+
07156+
end7157+
7158+
def emit_gc_scan_functions7159+
i = 07160+
while i < @cls_names.length7161+
if class_has_ptr_ivars(i) == 17162+
cname = @cls_names[i]7163+
emit_raw("static void sp_" + cname + "_gc_scan(void *p) {")7164+
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)p;")7165+
names = @cls_ivar_names[i].split(";")7166+
types = @cls_ivar_types[i].split(";")7167+
j = 07168+
while j < names.length7169+
if j < types.length7170+
if ivar_is_gc_ptr(types[j]) == 17171+
emit_raw(" if (self->" + sanitize_ivar(names[j]) + ") sp_gc_mark(self->" + sanitize_ivar(names[j]) + ");")7172+
end7173+
end7174+
j = j + 17175+
end7176+
# Also scan parent fields7177+
if @cls_parents[i] != ""7178+
pi = find_class_idx(@cls_parents[i])7179+
if pi >= 066477180
pnames = @cls_ivar_names[pi].split(";")66487181
ptypes = @cls_ivar_types[pi].split(";")66497182
pj = 0@@ -6686,7 +7219,11 @@
66867219
cname = @cls_names[i]66877220
# Constructor66887221
init_idx = cls_find_method_direct(i, "initialize")6689-
emit_raw("static sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(i) + ");")7222+
if @cls_is_value_type[i] == 17223+
emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(i) + ");")7224+
else7225+
emit_raw("static sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(i) + ");")7226+
end66907227
if init_idx >= 066917228
emit_raw("static void sp_" + cname + "_initialize(sp_" + cname + " *self" + init_params_decl(i) + ");")66927229
end@@ -6706,7 +7243,11 @@
67067243
if cls_method_has_yield(i, j) == 167077244
yp = yield_params_suffix_cls(i, j)67087245
end6709-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + " *self" + method_with_self_params(j, all_params, all_ptypes) + yp + ");")7246+
sp = " *self"7247+
if @cls_is_value_type[i] == 17248+
sp = " self"7249+
end7250+
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mnames[j]) + "(sp_" + cname + sp + method_with_self_params(j, all_params, all_ptypes) + yp + ");")67107251
end67117252
j = j + 167127253
end@@ -7022,14 +7563,19 @@
70227563
def emit_constructor(ci)70237564
cname = @cls_names[ci]70247565
init_idx = cls_find_method_direct(ci, "initialize")7025-
emit_raw("static sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")7026-
emit_raw(" SP_GC_SAVE();")7027-
scan_fn = "NULL"7028-
if class_has_ptr_ivars(ci) == 17029-
scan_fn = "sp_" + cname + "_gc_scan"7566+
if @cls_is_value_type[ci] == 17567+
emit_raw("static sp_" + cname + " sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")7568+
emit_raw(" sp_" + cname + " self = {0};")7569+
else7570+
emit_raw("static sp_" + cname + " *sp_" + cname + "_new(" + constructor_params_decl(ci) + ") {")7571+
emit_raw(" SP_GC_SAVE();")7572+
scan_fn = "NULL"7573+
if class_has_ptr_ivars(ci) == 17574+
scan_fn = "sp_" + cname + "_gc_scan"7575+
end7576+
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)sp_gc_alloc(sizeof(sp_" + cname + "), NULL, " + scan_fn + ");")7577+
emit_raw(" SP_GC_ROOT(self);")70307578
end7031-
emit_raw(" sp_" + cname + " *self = (sp_" + cname + " *)sp_gc_alloc(sizeof(sp_" + cname + "), NULL, " + scan_fn + ");")7032-
emit_raw(" SP_GC_ROOT(self);")70337579
70347580
# Root pointer-type constructor parameters70357581
if init_idx >= 0@@ -7074,7 +7620,11 @@
70747620
end70757621
sk = 070767622
while sk < pnames2.length7077-
emit_raw(" self->" + pnames2[sk] + " = lv_" + pnames2[sk] + ";")7623+
sa = "->"7624+
if @cls_is_value_type[ci] == 17625+
sa = "."7626+
end7627+
emit_raw(" self" + sa + pnames2[sk] + " = lv_" + pnames2[sk] + ";")70787628
sk = sk + 170797629
end70807630
end@@ -7127,7 +7677,7 @@
71277677
if @nd_type[sid] == "InstanceVariableWriteNode"71287678
ivar = sanitize_ivar(@nd_name[sid])71297679
val = compile_expr(@nd_expression[sid])7130-
emit_raw(" self->" + ivar + " = " + val + ";")7680+
emit_raw(" " + self_arrow + ivar + " = " + val + ";")71317681
else71327682
if @nd_type[sid] != "SuperNode"71337683
# Compile other statements (e.g., method calls like @arr[0] = val)@@ -7166,13 +7716,17 @@
71667716
end71677717
end71687718
7169-
emit_raw(" SP_GC_RESTORE();")7719+
if @cls_is_value_type[ci] == 07720+
emit_raw(" SP_GC_RESTORE();")7721+
end71707722
emit_raw(" return self;")71717723
emit_raw("}")71727724
emit_raw("")71737725
7174-
# Initialize function (for super calls)7726+
# Initialize function (for super calls) - always uses *self (pointer)71757727
if init_idx >= 07728+
saved_vt = @cls_is_value_type[ci]7729+
@cls_is_value_type[ci] = 071767730
emit_raw("static void sp_" + cname + "_initialize(sp_" + cname + " *self" + init_params_decl(ci) + ") {")71777731
bodies = @cls_meth_bodies[ci].split(";")71787732
bid = -1@@ -7207,7 +7761,7 @@
72077761
if @nd_type[sid] == "InstanceVariableWriteNode"72087762
ivar = sanitize_ivar(@nd_name[sid])72097763
val = compile_expr(@nd_expression[sid])7210-
emit_raw(" self->" + ivar + " = " + val + ";")7764+
emit_raw(" " + self_arrow + ivar + " = " + val + ";")72117765
end72127766
}72137767
pop_scope@@ -7215,6 +7769,7 @@
72157769
end72167770
emit_raw("}")72177771
emit_raw("")7772+
@cls_is_value_type[ci] = saved_vt72187773
end72197774
end72207775
@@ -7239,7 +7794,11 @@
72397794
if @in_yield_method == 172407795
yp = yield_params_suffix_cls(ci, midx)72417796
end7242-
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + yp + ") {")7797+
if @cls_is_value_type[ci] == 17798+
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " self" + build_params_str(pnames, ptypes) + yp + ") {")7799+
else7800+
emit_raw("static " + c_type(rt) + " sp_" + cname + "_" + sanitize_name(mname) + "(sp_" + cname + " *self" + build_params_str(pnames, ptypes) + yp + ") {")7801+
end72437802
72447803
push_scope72457804
j = 0@@ -7261,7 +7820,9 @@
72617820
end72627821
end72637822
if @in_gc_scope == 17264-
emit(" SP_GC_ROOT(self);")7823+
if @cls_is_value_type[ci] == 07824+
emit(" SP_GC_ROOT(self);")7825+
end72657826
j = 072667827
while j < pnames.length72677828
if j < ptypes.length@@ -8063,7 +8624,7 @@
80638624
end80648625
mi3 = mi3 + 180658626
end8066-
return "self->" + sanitize_ivar(@nd_name[nid])8627+
return self_arrow + sanitize_ivar(@nd_name[nid])80678628
end80688629
if t == "InstanceVariableWriteNode"80698630
val = compile_expr(@nd_expression[nid])@@ -8083,7 +8644,7 @@
80838644
end80848645
mi3 = mi3 + 180858646
end8086-
return "(self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ")"8647+
return "(" + self_arrow + sanitize_ivar(@nd_name[nid]) + " = " + val + ")"80878648
end80888649
if t == "ConstantReadNode"80898650
if @nd_name[nid] == "ARGV"@@ -8938,7 +9499,7 @@
89389499
rk = 089399500
while rk < readers.length89409501
if readers[rk] == mname8941-
return "self->" + sanitize_ivar(mname)9502+
return self_arrow + sanitize_ivar(mname)89429503
end89439504
rk = rk + 189449505
end@@ -10360,12 +10921,16 @@
1036010921
cname = recv_type[4, recv_type.length - 4]1036110922
ci = find_class_idx(cname)1036210923
if ci >= 010924+
arrow = "->"10925+
if @cls_is_value_type[ci] == 110926+
arrow = "."10927+
end1036310928
# attr_reader1036410929
readers = @cls_attr_readers[ci].split(";")1036510930
j = 01036610931
while j < readers.length1036710932
if readers[j] == mname10368-
return rc + "->" + mname10933+
return rc + arrow + mname1036910934
end1037010935
j = j + 11037110936
end@@ -10377,7 +10942,7 @@
1037710942
j = 01037810943
while j < writers.length1037910944
if writers[j] == bname10380-
return "(" + rc + "->" + bname + " = " + compile_arg0(nid) + ")"10945+
return "(" + rc + arrow + bname + " = " + compile_arg0(nid) + ", 0)"1038110946
end1038210947
j = j + 11038310948
end@@ -11123,7 +11688,7 @@
1112311688
mi3 = mi3 + 11112411689
end1112511690
if mod_ivar == 011126-
emit(" self->" + sanitize_ivar(@nd_name[nid]) + " = " + val + ";")11691+
emit(" " + self_arrow + sanitize_ivar(@nd_name[nid]) + " = " + val + ";")1112711692
end1112811693
return1112911694
end@@ -11132,10 +11697,10 @@
1113211697
val = compile_expr(@nd_expression[nid])1113311698
ivar = sanitize_ivar(@nd_name[nid])1113411699
if op == "+"11135-
emit(" self->" + ivar + " += " + val + ";")11700+
emit(" " + self_arrow + ivar + " += " + val + ";")1113611701
end1113711702
if op == "-"11138-
emit(" self->" + ivar + " -= " + val + ";")11703+
emit(" " + self_arrow + ivar + " -= " + val + ";")1113911704
end1114011705
return1114111706
end@@ -11258,7 +11823,7 @@
1125811823
mi3 = mi3 + 11125911824
end1126011825
if mod_ivar == 011261-
emit(" self->" + sanitize_ivar(iname) + " = " + tmps[k] + ";")11826+
emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmps[k] + ";")1126211827
end1126311828
end1126411829
end@@ -11810,7 +12375,7 @@
1181012375
return 11181112376
end1181212377
if @nd_type[recv] == "InstanceVariableReadNode"11813-
emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = sp_str_concat(self->" + sanitize_ivar(@nd_name[recv]) + ", " + val + ");")12378+
emit(" " + self_arrow + sanitize_ivar(@nd_name[recv]) + " = sp_str_concat(self->" + sanitize_ivar(@nd_name[recv]) + ", " + val + ");")1181412379
return 11181512380
end1181612381
end@@ -11834,7 +12399,7 @@
1183412399
return 11183512400
end1183612401
if @nd_type[recv] == "InstanceVariableReadNode"11837-
emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = " + val + ";")12402+
emit(" " + self_arrow + sanitize_ivar(@nd_name[recv]) + " = " + val + ";")1183812403
return 11183912404
end1184012405
end@@ -11856,7 +12421,7 @@
1185612421
return 11185712422
end1185812423
if @nd_type[recv] == "InstanceVariableReadNode"11859-
emit(" self->" + sanitize_ivar(@nd_name[recv]) + " = \"\";")12424+
emit(" " + self_arrow + sanitize_ivar(@nd_name[recv]) + " = \"\";")1186012425
return 11186112426
end1186212427
end@@ -12152,7 +12717,11 @@
1215212717
rt = infer_type(recv)1215312718
if is_obj_type(rt) == 11215412719
rc = compile_expr(recv)12155-
emit(" " + rc + "->" + bname + " = " + compile_arg0(nid) + ";")12720+
arrow2 = "->"12721+
if is_value_type_obj(rt) == 112722+
arrow2 = "."12723+
end12724+
emit(" " + rc + arrow2 + bname + " = " + compile_arg0(nid) + ";")1215612725
return 11215712726
end1215812727
end