The code which generates a dynamic array on the stack in the middle of a procedure (which I never would have suspected you could do in Pascal) appears to corrupt the stack in a way which makes ReturnAddress(0) either return bogus numbers or crash with a bus error. Or more accurately, assigning to sections of the created array corrupts the stack.
I think the problem is that ReturnAddress expects to find the address at 8(0(r30)) (r30 is the frame pointer I believe). But when the frame expands, r30 remains where it is while r1 (sp I think) drops down, and the return address is preserved at 0(r1), and r1 and r30 are no longer equal.
Procedure entry:
$000029e0 <_p__M0_S0_Test+0>: mflr r0 $000029e4 <_p__M0_S0_Test+4>: stmw r30,-8(r1) $000029e8 <_p__M0_S0_Test+8>: stw r0,8(r1) $000029ec <_p__M0_S0_Test+12>: stwu r1,-4240(r1) $000029f0 <_p__M0_S0_Test+16>: mr r30,r1 $000029f4 <_p__M0_S0_Test+20>: bcl- 20,4*cr7+so,$29f8 <_p__M0_S0_Test+24> $000029f8 <_p__M0_S0_Test+24>: mflr r31
r1 = r30, and 8(0(r30)) = 8(0(r1)) is the return address.
When the space is allocated on the stack for the dynamic array:
$00002a28 <_p__M0_S0_Test+72>: lwz r2,0(r1) $00002a2c <_p__M0_S0_Test+76>: neg r0,r0 $00002a30 <_p__M0_S0_Test+80>: stwux r2,r1,r0
Now r1 <> r30, and 8(0(r1)) is the return address
But ReturnAddress(0) is:
$00002a50 <_p__M0_S0_Test+112>: lwz r2,0(r30) $00002a54 <_p__M0_S0_Test+116>: lwz r0,8(r2)
Which gives a bogus result, and potentially a crash.
I'm pretty sure that's the problem, but hopefully someone with more depth of understanding of the compiler can verify it...?
Thanks, Peter.