t-crest/patmos-simulator

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

Closed this issue · 4 comments

Emoun commented

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.

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

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).

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

Emoun commented

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)