Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
15 years ago

Gcc4 Frame Pointer Issue

I've been trying to write some code in an MPU exception handler to trap and dump the call stack when an intermittent exception occurs in our system and am pretty sure I've found a bug in the latest Gcc4 compiler that ships with Quartus 10.0. I haven't tried it on 10.1 as yet.

It seems under gcc4 the frame pointer is not calcuated correctly in all cases.

Here's the generated asm code from gcc3 (10.0)

addi sp,sp,-120

stw ra,116(sp)

stw fp,112(sp)

stw r16,108(sp)

stw r17,104(sp)

stw r18,100(sp)

stw r19,96(sp)

stw r20,92(sp)

addi fp,sp,112

And the corresponding code from Gcc4

addi sp,sp,-116

stw ra,112(sp)

stw fp,108(sp)

stw r19,104(sp)

stw r18,100(sp)

stw r17,96(sp)

stw r16,92(sp)

addi fp,sp,92

Under Gcc4 the frame pointer is not pointing to the correct location as documented in the Nios II Processor Reference Handbook Section II-7 Stacks. You can see the difference in the addi fp,sp,xx instruction. Gcc3 does seem to do it right.

Also I've found that the __builtin_return_address() function does not work for Gcc4. Any value greater than >0 under Gcc4 returns 0 for the address.

Rebuild under Gcc3 and it works fine.

Can any one shed some light on this. If it's not a bug how should the call stack be walked now? The following code works under gcc3, fails under gcc4.

struct stack_frame {

struct stack_frame* next;

void* ret;

};

struct stack_frame* fp;

__asm ("mov %0, fp" : "=r" (fp) );

alt_u32 cnt = 0;

while(fp && (cnt < 10)) {

isr_printf("%x: fp(%x) pfp(%x) ra(%x)\n", cnt, fp, fp->next, fp->ret);

fp = fp->next;

cnt++;

} ;

:cry:

18 Replies