mruby-compiler: remove unnecessary MOVE/APOST in simple masgn

For simple parallel assignment like `a,b = 1,2` without rest or post
variables, skip generating APOST instruction and unnecessary MOVE.
The values are already extracted via AREF.

Before (27 bytes):
    LOADI_1 R3
    LOADI_2 R4
    ARRAY   R3 2
    AREF    R1 R3 0
    AREF    R2 R3 1
    MOVE    R4 R3
    APOST   R4 2 0
    MOVE    R4 R3
    RETURN  R3

After (18 bytes):
    LOADI_1 R3
    LOADI_2 R4
    ARRAY   R3 2
    AREF    R1 R3 0
    AREF    R2 R3 1
    RETURN  R3

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-12-19 22:26:36 +09:00
parent 7e28e68dca
commit e84f7bb3b0
+17 -17
View File
@@ -5086,24 +5086,24 @@ codegen_masgn(codegen_scope *s, node *varnode, node *rhs, int sp, int val)
}
}
gen_move(s, cursp(), rhs_reg, val);
push_n(post+1);
pop_n(post+1);
genop_3(s, OP_APOST, cursp(), n, post);
int nn = 1;
if (masgn_n->rest && (intptr_t)masgn_n->rest != -1) { /* rest */
gen_assignment(s, masgn_n->rest, NULL, cursp(), NOVAL);
}
if (masgn_n->post) {
node *post_part = masgn_n->post;
while (post_part) {
gen_assignment(s, post_part->car, NULL, cursp()+nn, NOVAL);
post_part = post_part->cdr;
nn++;
/* Only generate APOST if there's rest or post variables */
if ((masgn_n->rest && (intptr_t)masgn_n->rest != -1) || masgn_n->post) {
gen_move(s, cursp(), rhs_reg, val);
push_n(post+1);
pop_n(post+1);
genop_3(s, OP_APOST, cursp(), n, post);
int nn = 1;
if (masgn_n->rest && (intptr_t)masgn_n->rest != -1) { /* rest */
gen_assignment(s, masgn_n->rest, NULL, cursp(), NOVAL);
}
if (masgn_n->post) {
node *post_part = masgn_n->post;
while (post_part) {
gen_assignment(s, post_part->car, NULL, cursp()+nn, NOVAL);
post_part = post_part->cdr;
nn++;
}
}
}
if (val) {
gen_move(s, cursp(), rhs_reg, 0);
}
if (!val && t) {