Forum : ST7/STM8
Post Information | Post |
---|---|
May 20, 2011 - 12:53pm
|
I have a few lines of code that crashes. When I start to analyze the cause of the crash it seems like the compiler generates code that actually crashes. Please take a look at the example below and see what you think. I have a function called func2, the first thing that happens when we enter the function is that a number of bytes is reserved on the stack for local variables. In this case, it looks like this ; FUNCTION ?func2 (BEGIN) Here we can see that three bytes are reserved on the stack. This is fine, however , but if we look at these lines futher down in the function. 001C A602 LD A,#002H we can see that the value in register A is inserted into the stack variable (speed) at offset 7 !! This is outside the reserved range for local variables. So what happens is that the return address is overwritten and when we return from func2() ( executing RET) we jump into void! This is bad. This error only happen when I use the maximum level of optimization(7) and optimize for size. The complete source code that can be used to reproduce the error. #include typedef unsigned char uint8_t; typedef enum typedef enum char buf[200]; void error(char *p1, uint32_t p2) int func1(EnumType p1, EnumType p2, EnumType p3, uint8_t s) r1 = p1; int func2(Speed s) EnumType p1; p1 = ITEM4; switch (s) void main(void) // The next line will never be executed because the program crashes when returning from func2() This is the output for function func2() the the main.lst file ; FUNCTION ?func2 (BEGIN) Then I compiled the file I used the Ride7 IDE and the compilation line looks like this "C:\Program Files (x86)\Raisonance\Ride\Bin\RCSTM8.EXE" "C:\src\testr\main.c" QUIET GENERATEDEPFILE CODE DB OJ("C:\src\testr\main.obj") PR("C:\src\testr\main.lst") PIN("C:\Program Files (x86)\Raisonance\Ride\Inc;C:\Program Files (x86)\Raisonance\Ride\Inc\ST7") STM8(SMALL) DGC(DATA) O(3,SIZE) INITSTATICVAR ET(INT) LISTINCLUDE Best Regards |
Hi Andreas,
Thanks for your report.
We have been able to reproduce the bug in our labs and fixed it.
The fix will be available in the next RKit-STM8 release, which is nearly out (well... I guess so :/)
Best Regards