Hi there,
I have this odd issue. Seems to be a compiler bug. Create a new C166 ST10F276 project and put in this code and you will see it corrupt the user stack pointer!
I know someone will say "Why are you passing structs by value", but this is a problem that occurs due to EC++ generated code which I have no control over. I just simplified it down to this to demonstrate it.
The memory copy to create the argument to takeAFred(*GetA()) misses setting R1, later R1 is added to the stack pointer (R0) to correct the stack but it corrupts it! Refer hex address 41E.
Using latest compiler version 7.54 etc
typedef struct { long int tom; } fred; fred A; fred * GetA(void){return &A;} void takeAFred(fred l) { } void main(void) { fred b; // OK takeAFred(b); // Not OK takeAFred(*GetA()); }
Disassembly:
9: fred * GetA(void){return &A;} 10: 000003EC E6F40002 MOV R4,#0x0200 000003F0 E6F53C00 MOV R5,#0x003C 000003F4 DB00 RETS 11: void takeAFred(fred l) 12: { 13: } 14: 000003F6 DB00 RETS 15: void main(void) 000003F8 2804 SUB R0,#4 16: { 17: fred b; 18: 19: // OK 20: takeAFred(b); 21: // Not OK 000003FA F040 MOV R4,R0 000003FC E047 MOV R7,#0x04 000003FE 2007 SUB R0,R7 00000400 F060 MOV R6,R0 00000402 E864 MOV [R6],[R4+] 00000404 0862 ADD R6,#2 00000406 2872 SUB R7,#2 00000408 3DFC JMPR CC_NZ,0x000402 0000040A DA00F603 CALLS takeAFred(0x3F6) 0000040E 0804 ADD R0,#4 22: takeAFred(*GetA()); 00000410 DA00EC03 CALLS GetA(0x3EC) 00000414 E047 MOV R7,#0x04 00000416 2007 SUB R0,R7 00000418 F060 MOV R6,R0 0000041A DA007802 CALLS ?C_WCPYSF(0x278) 0000041E 0001 ADD R0,R1 00000420 DA00F603 CALLS takeAFred(0x3F6) 00000424 0804 ADD R0,#4 23: } 00000426 0804 ADD R0,#4 00000428 DB00 RETS