This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Help with SH2E -O2 instruction ordering with volatile
- From: Oleg Endo <oleg dot endo at t-online dot de>
- To: Alex Brown <alex dot g dot brown at gmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Sun, 20 Dec 2015 16:28:45 +0900
- Subject: Re: Help with SH2E -O2 instruction ordering with volatile
- Authentication-results: sourceware.org; auth=none
- References: <CAB-RZVTLmhdvvqXDHxBc_qAz2Nnm7mRrwHJU6Df8S4HOxxqaaw at mail dot gmail dot com> <1450592312 dot 2951 dot 15 dot camel at t-online dot de> <CAB-RZVT7Fc9s2nUjU8kharvqPKyFXR5P2=G8Uy3hU=Pw3DFbdQ at mail dot gmail dot com>
On Sun, 2015-12-20 at 06:41 +0000, Alex Brown wrote:
> I know the code is somewhat weird ;-)
>
> Basically peripheral.e wants the address of a null terminated string
> (not the char value).
>
> My "platform/emulator" will use DMA to extract the string from RAM
> before the next instruction is executed.
>
> The issue is that at the time the address is assigned to peripheral.e
> is seems the values of the text array have not yet been written to
> the stack (or that's what I thing I am seeing).
>
> Making "text" volatile makes it output code that makes sense to me.
Ah, that's the other thing I thought you might mean. It seems that the
same reordering happens on other architectures, too. So this is not
exactly SH specific.
Just because "peripheral" is volatile doesn't mean that all the loads
and stores around it automatically become volatile. The compiler
doesn't know that there is some transitivity going on.
Depending on your DMA circumstances, you might need a bit more than
that. For example the below code seems to do what you'd want in this
case:
#define memory_barrier() do { asm volatile ("" : : : "memory"); } while (0)
void
update(unsigned a, int x, int y, char c, unsigned color, unsigned char size)
{
lock();
char text[2] = { c, 0};
peripheral.a = a;
peripheral.b = x;
peripheral.c = y;
peripheral.d = color;
memory_barrier ();
peripheral.e = (unsigned)text;
unlock();
}
Cheers,
Oleg