Domanda

In C++03 Standard 1.9/6 there's this definition of observable behavior

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.

and The Standard goes to length explaining that the compiler must preserve observable behavior while doing optimizations.

However there's no such or similar definition in C99 draft I'm looking at. The only time observable behavior is mentioned is 6.7.3/7

The intended use of the restrict qualifier (like the register storage class) is to promote optimization, and deleting all instances of the qualifier from a conforming program does not change its meaning (i.e., observable behavior)

Is there a definition of what exactly the compiler must preserve when optimizing a C99 program?

È stato utile?

Soluzione

In my draft, §3.4, defines behavior as "external appearance or action". "Observable behavior" seems to be a pleonasm that occurs exactly once.

§5.1.2.3, Program execution, further defines the behavior of C programs:

The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant.

It then goes on to define side-effects as "changes in the state of the execution environment" caused by "[a]ccessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations". Side-effects are sequenced at sequence points.

This seems to be stricter than C++ in that "modifying an object", i.e. writing to memory, is (observable) behavior in C.

As for allowed optimization:

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

"Needed side-effects" are then listed in the following point:

  • At sequence points, volatile objects are stable in the sense that previous accesses are complete and subsequent accesses have not yet occurred.

  • At program termination, all data written into files shall be identical to the result that execution of the program according to the abstract semantics would have produced.

  • The input and output dynamics of interactive devices shall take place as specified in 7.19.3.

The paragraph concludes with a list of examples; §7.19.3 describes files in the context of stdio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top