vm: add OP_RETNIL for returning nil directly

Add a new opcode that returns nil without requiring LOADNIL + RETURN.
This avoids loading nil into a register by setting the return value (v)
directly. The implementation uses a separate label (L_RETURN_NIL) to
bypass v = regs[a], preserving self in regs[0] for ensure blocks.

Codegen applies peephole optimization to fuse LOADNIL + RETURN -> RETNIL.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-01-23 00:33:25 +09:00
parent a6bf08847a
commit 0b1af858e2
5 changed files with 19 additions and 1 deletions
+3
View File
@@ -399,6 +399,9 @@ codedump(mrb_state *mrb, const mrb_irep *irep, FILE *out)
CASE(OP_RETSELF, Z):
fprintf(out, "RETSELF\n");
break;
CASE(OP_RETNIL, Z):
fprintf(out, "RETNIL\n");
break;
CASE(OP_BREAK, B):
fprintf(out, "BREAK\t\tR%d\t", a);
print_lv_a(mrb, irep, a, out);
+8
View File
@@ -2744,6 +2744,10 @@ RETRY_TRY_BLOCK:
a = 0;
goto NORMAL_RETURN;
}
CASE(OP_RETNIL, Z) {
a = 0;
goto L_RETURN_NIL;
}
CASE(OP_RETURN, B) {
mrb_int acc;
mrb_value v;
@@ -2751,6 +2755,10 @@ RETRY_TRY_BLOCK:
NORMAL_RETURN:
v = regs[a];
goto L_RETURN;
L_RETURN_NIL:
v = mrb_nil_value();
L_RETURN:
mrb_gc_protect(mrb, v);
return_ci = ci;
CHECKPOINT_RESTORE(RBREAK_TAG_BREAK) {