Re: [PATCH] C undefined behavior fix

Bernd Petrovitsch
2002-01-09T09:25:42Z

In message <17B78BDF120BD411B70100500422FC6309E40E@IIS000>, Bernard Dautrevaux
wrote:
>I agree that in some cases reading a 32-bit word when needing a 16-bit
>volatile short may be allowed by the standard. HOWEVER that suppose that gcc
>makes a careful examination of all the memory layout for the program so that
>to be sure that the 16 unneeded bits it reads for efficiency do NOT come
>from some volatile object(s), or gcc will then BREAK the volatile semantics
>for these objects.
>
>So in any case this is not allowed in a lot of cases such as accessing
>accessing an external "volatile short" (only the linker knwos for sure what
>is near this short) or reading memory through a "volatile short*" (only GOD
>knows if you can). And in fact it's WRONG to access in such a way if you
>know that near this object you have other objects (such as is the case in a
>volatile struct...). So even if it *may* be legal in some cases, such an
>optimization that *may* be more efficient is not at all very interesting.

Especially if there are cases were this optimization yields a slower
access (or even worse indirect bugs).
E.g. if the referenced "volatile short" is a hardware register and the
access is multiplexed over a slow 8 bit bus. There are embedded systems
around where this is the case and the (cross-)compiler has no way to
know this (except it can be told by the programmer).

Bernd
--
Bernd Petrovitsch Email : bernd@gams.at
g.a.m.s gmbh Fax : +43 1 205255-900
Prinz-Eugen-Straße 8 A-1040 Vienna/Austria/Europe
LUGA : http://www.luga.at