matt-kempster / m2c

A MIPS and PowerPC decompiler.
GNU General Public License v3.0
411 stars 49 forks source link

Identify `*a = *b` bulk copy #279

Open joshlory opened 4 months ago

joshlory commented 4 months ago

We've seen this pattern in sotn decomp:

loop_8:
                var_a2->next = var_a3->next;
                var_a2->unk4 = (s32) var_a3->unk4;
                var_a2->unk8 = (s32) var_a3->unk8;
                var_a2->unkC = (s32) var_a3->unkC;
                var_a3 += 0x10;
                var_a2 += 0x10;
                if (var_a3 != temp_t0) {
                    goto loop_8;
                }

...which matches as:

*var_a2 = *var_a3;
simonlindholm commented 4 months ago

We have a few patterns implemented for this already, but it's pretty fragile. Can you post the asm that resulted in the above?

joshlory commented 3 months ago

There are a few in weapon funcs, links below.

https://decomp.me/scratch/udWNi

 2ac:    move    a2,s0
 2b0:    lw      t0,0(s0)
 2b4: ~> lw      v0,0(a3)
 2b8:    lw      v1,4(a3)
 2bc:    lw      a0,8(a3)
 2c0:    lw      a1,0xc(a3)
 2c4:    sw      v0,0(a2)
 2c8:    sw      v1,4(a2)
 2cc:    sw      a0,8(a2)
 2d0:    sw      a1,0xc(a2)
 2d4:    addiu   a3,a3,0x10
 2d8:    bne     a3,t1,2b4 ~>
 2dc:    addiu   a2,a2,0x10
 2e0:    lw      v0,0(a3)
 2e4:    nop     
 2e8:    sw      v0,0(a2)

https://decomp.me/scratch/p4pzA

 2a0:    move    a2,s0
 2a4:    lw      t0,0(s0)
 2a8: ~> lw      v0,0(a3)
 2ac:    lw      v1,4(a3)
 2b0:    lw      a0,8(a3)
 2b4:    lw      a1,0xc(a3)
 2b8:    sw      v0,0(a2)
 2bc:    sw      v1,4(a2)
 2c0:    sw      a0,8(a2)
 2c4:    sw      a1,0xc(a2)
 2c8:    addiu   a3,a3,0x10
 2cc:    bne     a3,t1,2a8 ~>
 2d0:    addiu   a2,a2,0x10
 2d4:    lw      v0,0(a3)
 2d8:    nop     
 2dc:    sw      v0,0(a2)

https://decomp.me/scratch/MnTB0

 2a0:    move    a2,s0
 2a4:    lw      t1,0(s0)
 2a8: ~> lw      v0,0(a3)
 2ac:    lw      v1,4(a3)
 2b0:    lw      a0,8(a3)
 2b4:    lw      a1,0xc(a3)
 2b8:    sw      v0,0(a2)
 2bc:    sw      v1,4(a2)
 2c0:    sw      a0,8(a2)
 2c4:    sw      a1,0xc(a2)
 2c8:    addiu   a3,a3,0x10
 2cc:    bne     a3,t0,2a8 ~>
 2d0:    addiu   a2,a2,0x10
 2d4:    lw      v0,0(a3)
 2d8:    nop     
 2dc:    sw      v0,0(a2)
joshlory commented 3 months ago

Slightly different pattern here: https://decomp.me/scratch/ra0BJ

 c:    addiu   a3,sp,0x10
 10:    lui     a2,%hi(g_Entities)
 14:    addiu   a2,a2,%lo(g_Entities)
 18:    addiu   t0,a2,0xb0
 2c: ~> lw      v0,0(a2)
 30:    lw      v1,4(a2)
 34:    lw      a0,8(a2)
 38:    lw      a1,0xc(a2)
 3c:    sw      v0,0(a3)
 40:    sw      v1,4(a3)
 44:    sw      a0,8(a3)
 48:    sw      a1,0xc(a3)
 4c:    addiu   a2,a2,0x10
 50:    bne     a2,t0,2c ~>
 54:    addiu   a3,a3,0x10
 58:    lw      v0,0(a2)
 5c:    lw      v1,4(a2)
 60:    lw      a0,8(a2)
 64:    sw      v0,0(a3)
 68:    sw      v1,4(a3)
 6c:    sw      a0,8(a3)