Confirmed in Linaro GCC 4.5-2011.02-0. I assume that GCC is looking at the type, checking the expected alignment, and then inferring what the value of the LSB should be. This code:
doesn't get optimised away as foo could be at an odd address.
I can't remember if casting a function pointer to an int is undefined behaviour. I think it is, so this code is invalid. However it is very useful so I think we should support it.
Confirmed in Linaro GCC 4.5-2011.02-0. I assume that GCC is looking at the type, checking the expected alignment, and then inferring what the value of the LSB should be. This code:
extern int foo;
void main()
{
void *p = &foo;
if ((int)p & 1) printf ("HIT!\n");
}
gets optimised away into a bx lr as an int should be word aligned, so the lower two bits should be zero. This code:
extern char foo;
void main()
{
void *p = &foo;
if ((int)p & 1) printf ("HIT!\n");
}
doesn't get optimised away as foo could be at an odd address.
I can't remember if casting a function pointer to an int is undefined behaviour. I think it is, so this code is invalid. However it is very useful so I think we should support it.