Skip to contentSkip to conversation

C undefined behavior fix

PATCH [email protected] 3 participants 3 messages Jan 10, 2002

lore
Share & tools
lore
#0 lore raw

<<Note that this is not too much of a problem for system programming, as you have a way to be sure they are not combined: just use intermediate variables and set them separately; the nice thing there is that as you use these intermediate variables just once, the compiler will eliminate them. But be
careful: the sequence point MUST BE RETAINED, and then the two loads cannot be combined (in case 1 of course).

Quoted 1 line
Quoted 1 line

Of course we all understand that sequence points myust be retained, but this is a weak condition compared to the rule that all loads and stores for volatile variables must not be resequenced, and in particular, you seem to agree that two loads *can* be combined if they both appear between two sequence points. I think that's unfortunate, and it is why in Ada we adopted a stricter point of view that avoids the notion of sequence points.

It even seems that if you have two stores between two sequence points then the compiler is free to omit one, and again that seems the wrong decision for the case of volatile variables. If it can omit a store in this way, can it omit a load, i.e. if we have:

   x := v - v;

can someone read the sequence point rule to mean that the compiler is free to do only one load here? I hope not, but we have already seen how much confusion there is on this point.

#1 lore raw
Quoted 20 lines · On Thu, Jan 10, 2002 at 07:18:45AM -0500, [email protected] wrote:

<<Note that this is not too much of a problem for system programming, as you have a way to be sure they are not combined: just use intermediate variables and set them separately; the nice thing there is that as you use these intermediate variables just once, the compiler will eliminate them. But be
careful: the sequence point MUST BE RETAINED, and then the two loads cannot be combined (in case 1 of course).

Quoted 1 line
Quoted 1 line

Of course we all understand that sequence points myust be retained, but this is a weak condition compared to the rule that all loads and stores for volatile variables must not be resequenced, and in particular, you seem to agree that two loads *can* be combined if they both appear between two sequence points. I think that's unfortunate, and it is why in Ada we adopted a stricter point of view that avoids the notion of sequence points.

It even seems that if you have two stores between two sequence points then the compiler is free to omit one, and again that seems the wrong decision for the case of volatile variables. If it can omit a store in this way, can it omit a load, i.e. if we have:

Quoted 5 lines
   x := v - v;

can someone read the sequence point rule to mean that the compiler is free to do only one load here? I hope not, but we have already seen how much confusion there is on this point.

The compiler is not free to do only one load there if v is declared volatile.
The relevant part of the C standard would be the following paragraph:

[6.7.3]
[#6] An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.105) What constitutes an access to an object that has volatile-qualified type is implementation- defined.

footnote 105:

105A volatile declaration may be used to describe an object
corresponding to a memory-mapped input/output port or an
object accessed by an asynchronously interrupting
function. Actions on objects so declared shall not be
`optimized out'' by an implementation or reordered
except as permitted by the rules for evaluating
expressions.

[Quotes taken from a draft of the final standard, so it is possible, but unlikely, that this was changed for the final standard.]

Signature · 3 lines
<Insert your favourite quote here.>
Erik Trulsson
[email protected]
#2 lore raw
In reply to [email protected]
Quoted 4 lines · Erik Trulsson

The compiler is not free to do only one load there if v is declared volatile.
The relevant part of the C standard would be the following paragraph:

Quoted 12 lines

[6.7.3]
[#6] An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.105) What constitutes an access to an object that has volatile-qualified type is implementation- defined.

And 5.1.2.3 states:

[#2] Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects,11) which are changes in the state of the execution environment. Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken
place. (A summary of the sequence points is given in annex
C.)

So it seems to be obvious ;-) that the compiler must not remove seemingly unnecessary references to volatile objects.