t-crest / patmos-simulator

Host the Patmos ISA simulator
2 stars 2 forks source link

pasim: retnd doesn't ignore following instructions if returning from no function #20

Closed Emoun closed 1 year ago

Emoun commented 2 years ago

Take the following assembly program:

                .word    36;
        addi    r1 = r0, 0;
                brcfnd     x;
                nop;
                nop;
                nop;
                halt;
        nop;
        nop;
        nop;
                .word    24;
x:              addi     r1 = r1, 1337;
        retnd;
        addi     r1  = r1, 1;
        addi     r1  = r1, 2;
        addi     r1  = r1, 3;

When it finishes, should have r1=1337 however gets r1=1343 meaning the final adds are being executed even if they shouldn't. If we replace retnd with ret (and remove the adds) we get the expected result.

I know this is a niche situation, as in real code you should never have a return without first a call. However, since it works as expected with ret, and the current behavior is definitely wrong, a fix is justified.

schoeberl commented 2 years ago

I hope that this is the correct execution! This is not a niche thing, as delay slots are part of the Patmos ISA definition. We need to check that we have the same number of delay slots in Patmos, pasim, the compiler, and in the handbook.

Emoun commented 2 years ago

The delay slots for ret seem to work properly. By niche, I meant the fact of executing a retnd without having executed any call.

The issue here is that retnd also has delay slots in this specific circumstance even though it shouldn't. So there is definitely wrong execution here (for retnd).

schoeberl commented 2 years ago

Ooh, this is bad! We need to check if this is pasim only.

Emoun commented 1 year ago

Additional problems:

  1. This is not limited to when returning without having done a call first. I have yet to figure out the cause.
  2. If the bytes following a retnd are invalid as instructions, pasim will throw an error (again, even though it should ignore them)