This was originally observed after I modified some code of the Contiki OS for cc2430 SoCs. It can be stripped down to a simple example, as the one in the attached foo.c
Assume a function accepting two pointer arguments and returning a pointer, as in:
unsigned char * foo(struct ip6_addr * addr, unsigned char * l);
The call itself works. Problems start if we attempt to pass its return value directly to another function, as in:
memcpy(a, foo(b, c), 8);
The attached file demonstrates some working and some non-working cases
foo.c will compile just fine with:
sdcc -c foo.c
However, if we compile with --stack-auto, we get the following error:
geo@lin-geo:~/workspace/test-proj$ sdcc --stack-auto -c foo.c
foo.c:24: error 78: incompatible types
from type 'unsigned-char generic* auto'
to type 'struct ip6_addr generic* fixed'
As anticipated, if we add __reentrant to foo's prototype and compile without --stack-auto we get the same error.
However, if we make any of the following changes (demonstrated in the attached), --stack-auto works
- If foo returns void *
- If we cast its return value to (void *)
- If it takes only a single argument
- If it takes two arguments but neither of type struct xyz *
The outer function doesn't have to be memcpy. The problem only appears to manifest itself when foo's return value is a pointer of a different type than the one expected by the outer function.
I tried with the following versions:
SDCC : mcs51 3.0.4 #6748 (Aug 17 2011) (Linux)
SDCC : mcs51 3.0.4 #6726 (Aug 8 2011) (Mac OS X x86_64)
SDCC : mcs51 3.0.1 #6188 (Feb 3 2011) (CYGWIN)
--model-xyz does not make any difference that I've noticed (tried small, large, huge). Target MCU doesn't seem to make any difference either (quickly re-built and tried -mhc08 with the same results)
This is a follow-up (with some more test cases) to the conversation that started with this:
Hope this Helps