G
gvarndell
Hi,
In the following code fragment, gcc seems to ignore the initial value
assigned to pData. (compiled -fvolatile -O2 with gcc)
void test(void)
{
void *pData = (void*)0x3400;
pData = (void*)(*((unsigned char**)&pData) + 4);
memcpy(pData,(void*)0x1000,10);
return;
}
The generated code simply reads whatever garbage is at [sp], adds 4 to
that value, stores it back to [sp], and then calls memcpy.
I've found that, if I declare pData as
void * volatile pData = (void*)0x3400;
then the generated code behaves as I would expect -- that is, the value
actually passed to memcpy is 0x3404 because the initializer is not
optimized away.
I also found that, if I write this fragment this way..
void test(void)
{
void *pData = (void*)0x3400;
unsigned char *pChar;
pchar = pData;
pData = pChar + 4;
memcpy(pData,(void*)0x1000,10);
return;
}
then the generated code works as expected, passing 0x3404 to memcpy.
I know that the casts create temporary unnamed values, but I wouldn't
expect that to cause the complier to ignore the initializer.
It seems as though gcc doesn't consider the read access (&pData) when
deciding whether to optimize the initializer out of existence.
Does this make sense? Is a compiler allowed to ignore the code sequence
when read accesses are buried in a cast expression?
TIA,
GV
In the following code fragment, gcc seems to ignore the initial value
assigned to pData. (compiled -fvolatile -O2 with gcc)
void test(void)
{
void *pData = (void*)0x3400;
pData = (void*)(*((unsigned char**)&pData) + 4);
memcpy(pData,(void*)0x1000,10);
return;
}
The generated code simply reads whatever garbage is at [sp], adds 4 to
that value, stores it back to [sp], and then calls memcpy.
I've found that, if I declare pData as
void * volatile pData = (void*)0x3400;
then the generated code behaves as I would expect -- that is, the value
actually passed to memcpy is 0x3404 because the initializer is not
optimized away.
I also found that, if I write this fragment this way..
void test(void)
{
void *pData = (void*)0x3400;
unsigned char *pChar;
pchar = pData;
pData = pChar + 4;
memcpy(pData,(void*)0x1000,10);
return;
}
then the generated code works as expected, passing 0x3404 to memcpy.
I know that the casts create temporary unnamed values, but I wouldn't
expect that to cause the complier to ignore the initializer.
It seems as though gcc doesn't consider the read access (&pData) when
deciding whether to optimize the initializer out of existence.
Does this make sense? Is a compiler allowed to ignore the code sequence
when read accesses are buried in a cast expression?
TIA,
GV