[coldfire-gnu-discuss] Control deferred writes?

David Brown david at westcontrol.com
Fri Dec 3 15:34:48 UTC 2010


On 03/12/2010 15:38, 42Bastian wrote:
> Am 03.12.2010 15:28, schrieb Oliver Betz:
>> Hallo All,
>>
>> can I tell gcc not to defer writes, possibly only to certain
>> variables?
>
> No, not at all. If you need such, write assembly.
>
> The compiler has no idea of the underlying hardware.
> It might schedule instructions if it knows the CPU core, but not w.r.t.
> bus timing.
>

Writes from the Coldfire V2 core are in-order - there is no re-ordering 
write buffer, and the data cache is write-through.  Other Coldfire cores 
may have hardware that affects the ordering or buffering of writes.

The compiler does not know the timing of writes to various places.  Thus 
when scheduling it can only assume that writes all take a fixed number 
of cycles.

Since you don't have to use any cpu-specific instructions to enforce or 
control the ordering of the writes, the only issue is to control the 
compiler-generated write instructions.

There are three tools for doing that.  One is to write at least some 
parts of your code in assembly, as suggested.

Use of "volatile" is important.  All "volatile" writes will be generated 
in the order expected by the program, and you will get no more nor less 
than you ask for.  But note that non-volatile reads and writes can be 
re-ordered freely around the volatile reads and writes.

Remember also that it is possible to enforce volatile writes to 
non-volatile data by a bit of (slightly messy) casting:

     *(volatile int32_t *)(&foo) = 123;


The final tool is the memory block, usually written as:

     asm volatile ("" ::: "memory")

This tells the compiler that any writes to memory need to be completed 
before "excuting" the line (it generates no code by itself), and no data 
read before the line can be trusted after the line (i.e., any data in 
registers must be re-read).

Use volatile accesses, and memory blocks if needed, to enforce the write 
ordering that you require.  Then let the compiler handle the rest as 
best it can.



More information about the coldfire-gnu-discuss mailing list