Commit 20dcd7fd
20dcd7fdd82bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): LV op-assign on bigint via sp_bigint_ helpers
Both expression-form and stmt-form LocalVariableOperatorWriteNode arms now dispatch through sp_bigint_ for bigint LVs. Adds missing %, &, |, ^, <<, >> operators to the bigint dispatch. Uses expr_emit_is_bigint walker for stale-cache detection.
Promote-mode test: +1. Default 631/0/0.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 20dcd7fdd82b
- Parent
- 57d25a8b3530
spinel_codegen.rbmodifiedView file
@@ -11919,6 +11919,47 @@
1191911919
if disp != ""1192011920
return "(" + vref + " = " + disp + ")"1192111921
end11922+
# promote-mode bigint local: raw C `OP=` on a sp_Bigint *11923+
# pointer would be illegal arithmetic. Route through the11924+
# sp_bigint_<op> helpers. Mirrors the InstanceVariableOperator11925+
# WriteNode arm below.11926+
if base_type(vt) == "bigint"11927+
@needs_bigint = 111928+
rhs_t_lov = infer_type(@nd_expression[nid])11929+
if rhs_t_lov != "bigint" && expr_emit_is_bigint(@nd_expression[nid]) == 111930+
rhs_t_lov = "bigint"11931+
end11932+
rhs_big_lov = rhs_t_lov == "bigint" ? "(sp_Bigint *)(" + val + ")" : "sp_bigint_new_int(" + val + ")"11933+
if op == "+"11934+
return "(" + vref + " = sp_bigint_add((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11935+
end11936+
if op == "-"11937+
return "(" + vref + " = sp_bigint_sub((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11938+
end11939+
if op == "*"11940+
return "(" + vref + " = sp_bigint_mul((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11941+
end11942+
if op == "/"11943+
return "(" + vref + " = sp_bigint_div((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11944+
end11945+
if op == "%"11946+
return "(" + vref + " = sp_bigint_mod((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11947+
end11948+
if op == "&"11949+
return "(" + vref + " = sp_bigint_and((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11950+
end11951+
if op == "|"11952+
return "(" + vref + " = sp_bigint_or((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11953+
end11954+
if op == "^"11955+
return "(" + vref + " = sp_bigint_xor((sp_Bigint *)" + vref + ", " + rhs_big_lov + "))"11956+
end11957+
if op == "<<" || op == ">>"11958+
shamt_lov = rhs_t_lov == "bigint" ? "sp_bigint_to_int(" + rhs_big_lov + ")" : "(" + val + ")"11959+
h_lov = op == "<<" ? "sp_bigint_shl" : "sp_bigint_shr"11960+
return "(" + vref + " = " + h_lov + "((sp_Bigint *)" + vref + ", " + shamt_lov + "))"11961+
end11962+
end1192211963
return "(" + vref + " " + op + "= " + val + ")"1192311964
end1192411965
if t == "InstanceVariableOperatorWriteNode"@@ -26213,32 +26254,18 @@
2621326254
emit(" " + vref + " = " + disp + ";")2621426255
return2621526256
end26216-
if vt == "bigint"26257+
if base_type(vt) == "bigint"2621726258
at = infer_type(@nd_expression[nid])26218-
# The cache may say "int" even though the rhs emits bigint26219-
# (arith CallNode with bigint operand, or any of the rhs26220-
# shapes mirrored by compile_stmt's IVW arm). Peek the26221-
# operator's operands to upgrade the cached type.26222-
if at != "bigint" && @nd_expression[nid] >= 0 && @nd_type[@nd_expression[nid]] == "CallNode"26223-
rhs_mn_lv = @nd_name[@nd_expression[nid]]26224-
if rhs_mn_lv == "+" || rhs_mn_lv == "-" || rhs_mn_lv == "*" || rhs_mn_lv == "/" || rhs_mn_lv == "%" || rhs_mn_lv == "**"26225-
rhs_recv_lv = @nd_receiver[@nd_expression[nid]]26226-
if rhs_recv_lv >= 0 && base_type(infer_type(rhs_recv_lv)) == "bigint"26227-
at = "bigint"26228-
else26229-
rhs_args_lv = @nd_arguments[@nd_expression[nid]]26230-
if rhs_args_lv >= 026231-
ra_lv = get_args(rhs_args_lv)26232-
if ra_lv.length > 0 && base_type(infer_type(ra_lv[0])) == "bigint"26233-
at = "bigint"26234-
end26235-
end26236-
end26237-
end26259+
# The cache may say "int" even though the rhs emits bigint;26260+
# the unified emit walker covers arith, bitop, user methods,26261+
# paren, and unary.26262+
if at != "bigint" && expr_emit_is_bigint(@nd_expression[nid]) == 126263+
at = "bigint"2623826264
end2623926265
# Cast away volatile from bigint locals (see compile_bigint_arg).2624026266
barg = at == "bigint" ? "(sp_Bigint *)" + val : "sp_bigint_new_int(" + val + ")"2624126267
vref_b = "(sp_Bigint *)" + vref26268+
@needs_bigint = 12624226269
if op == "+"2624326270
emit(" " + vref + " = sp_bigint_add(" + vref_b + ", " + barg + ");")2624426271
end@@ -26251,6 +26278,23 @@
2625126278
if op == "/"2625226279
emit(" " + vref + " = sp_bigint_div(" + vref_b + ", " + barg + ");")2625326280
end26281+
if op == "%"26282+
emit(" " + vref + " = sp_bigint_mod(" + vref_b + ", " + barg + ");")26283+
end26284+
if op == "&"26285+
emit(" " + vref + " = sp_bigint_and(" + vref_b + ", " + barg + ");")26286+
end26287+
if op == "|"26288+
emit(" " + vref + " = sp_bigint_or(" + vref_b + ", " + barg + ");")26289+
end26290+
if op == "^"26291+
emit(" " + vref + " = sp_bigint_xor(" + vref_b + ", " + barg + ");")26292+
end26293+
if op == "<<" || op == ">>"26294+
shamt_slv = at == "bigint" ? "sp_bigint_to_int(" + barg + ")" : "(" + val + ")"26295+
h_slv = op == "<<" ? "sp_bigint_shl" : "sp_bigint_shr"26296+
emit(" " + vref + " = " + h_slv + "(" + vref_b + ", " + shamt_slv + ");")26297+
end2625426298
emit(" if(sp_gc_bytes>sp_gc_threshold){size_t _b=sp_gc_bytes;sp_gc_collect();size_t _f=_b-sp_gc_bytes;if(_f<_b/4)sp_gc_threshold=_b*2;else if(sp_gc_bytes>0){sp_gc_threshold=sp_gc_bytes*4;if(sp_gc_threshold<sp_gc_threshold_init)sp_gc_threshold=sp_gc_threshold_init;}else sp_gc_threshold=sp_gc_threshold_init;}")2625526299
return2625626300
end