Commit a0864f36
a0864f361051Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #265 from mame/pr/op-assign-user-operator
fix(codegen): op-assign on obj slot dispatches user-defined operator
Details
- Commit
- a0864f361051
- Parent
- 6952fe2b21fe
- Merge parent
- da7fb97d0cbf
spinel_codegen.rbmodifiedView file
@@ -17509,7 +17509,15 @@
1750917509
# outer write zeroes the ivar.1751017510
op = @nd_binop[nid]1751117511
val = compile_expr(@nd_expression[nid])17512-
return "(" + fiber_var_ref(@nd_name[nid]) + " " + op + "= " + val + ")"17512+
vref = fiber_var_ref(@nd_name[nid])17513+
vt = find_var_type(@nd_name[nid])17514+
# Obj-typed local: dispatch via user-defined operator instead17515+
# of emitting raw C `OP=` (pointer arithmetic on an obj slot).17516+
disp = obj_op_dispatch_expr(vt, vref, op, val)17517+
if disp != ""17518+
return "(" + vref + " = " + disp + ")"17519+
end17520+
return "(" + vref + " " + op + "= " + val + ")"1751317521
end1751417522
if t == "LocalVariableOrWriteNode"1751517523
# `local ||= expr` in expression context. Lower as@@ -24542,6 +24550,32 @@
2454224550
""2454324551
end2454424552
24553+
# `recv OP rhs` lowering for an obj-typed receiver. When the24554+
# receiver's class (or an ancestor) defines `op` as a user method,24555+
# returns the C expression `sp_<owner>_<op>(<recv_c>, <rhs_c>)`.24556+
# Returns "" when no dispatch applies (caller falls back to its24557+
# inline path, or to `warn_unresolved_call`).24558+
def obj_op_dispatch_expr(recv_t, recv_c, op, rhs_c)24559+
if is_obj_type(recv_t) == 024560+
return ""24561+
end24562+
cname = recv_t[4, recv_t.length - 4]24563+
ci = find_class_idx(cname)24564+
if ci < 024565+
return ""24566+
end24567+
owner = find_method_owner(ci, op)24568+
if owner == ""24569+
warn_unresolved_call(op, "obj_" + cname)24570+
return ""24571+
end24572+
cast = ""24573+
if owner != cname24574+
cast = "(sp_" + owner + " *)"24575+
end24576+
"sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")"24577+
end24578+
2454524579
def compile_if_expr(nid)2454624580
cond = compile_cond_expr(@nd_predicate[nid])2454724581
then_val = "0"@@ -25049,6 +25083,14 @@
2504925083
val = compile_expr(@nd_expression[nid])2505025084
vref = fiber_var_ref(@nd_name[nid])2505125085
vt = find_var_type(@nd_name[nid])25086+
# Same desugaring as the ivar form: `local OP= rhs` is25087+
# `local = local OP rhs`. Dispatch through the user-defined25088+
# operator when the local is obj-typed.25089+
disp = obj_op_dispatch_expr(vt, vref, op, val)25090+
if disp != ""25091+
emit(" " + vref + " = " + disp + ";")25092+
return25093+
end2505225094
if vt == "bigint"2505325095
at = infer_type(@nd_expression[nid])2505425096
barg = at == "bigint" ? val : "sp_bigint_new_int(" + val + ")"@@ -25252,6 +25294,19 @@
2525225294
op = @nd_binop[nid]2525325295
val = compile_expr(@nd_expression[nid])2525425296
lhs = ivar_lhs(@nd_name[nid])25297+
ivar_t = ""25298+
if @current_class_idx >= 025299+
ivar_t = cls_ivar_type(@current_class_idx, @nd_name[nid])25300+
end25301+
# `@x OP= v` desugars to `@x = @x OP v`. When @x is obj-typed25302+
# and the class defines OP, dispatch to the user method25303+
# instead of emitting a raw C `+=` (which on a pointer is25304+
# silently miscompiled as pointer arithmetic).25305+
disp = obj_op_dispatch_expr(ivar_t, lhs, op, val)25306+
if disp != ""25307+
emit(" " + lhs + " = " + disp + ";")25308+
return25309+
end2525525310
if op == "+"2525625311
emit(" " + lhs + " += " + val + ";")2525725312
endtest/op_assign_user_operator.rbaddedView file
Loading diff…