Ben Pfaff wrote:
en******@yahoo. com writes:
Ben Pfaff wrote: en******@yahoo. com writes:
> Any compiler that compiles p = p->next = q as storing into p
> before fetching p->next is simply broken. Operands are
> evaluated before the operator they're operands for, and
> operators are evaluated before they produce a value--that's
> just how C semantics work (in a C abstract machine of course).
Do you have any citations to back up your assertions?
"Evaluation of an expression may _produce_ side effects."
"[At sequence points,] all side effects of _previous_
evaluations shall be complete [...]."
"In the abstract machine, all expressions are evaluated
_as specified by the semantics_."
OK.
"[An operator] may specify an operation to be performed
(which _in turn_ may _yield a value_ [...], _produce_ a
side effect, or some combination thereof) [...]."
I don't see anything about ordering there.
Maybe in some imaginary world where words have no intrinsic
meaning. In _this_ world where the writing is a mixture of
informal and semi-formal English, the words used clearly say
something about ordering because of causality relationships.
"The syntax specifies the precedence of operators _in the
evaluation of_ an expression [...]."
Precedence != ordering.
Again, the word precedence implies something about
ordering because of the meaning of the word. The
root of precedence is precede.
"[Except ...], _the order of evaluation of subexpressions_
and _the order in which side effects take place_ are both
unspecified." (Not "order of A and B" but "order of A" and
"order of B".)
I don't think that typographical distinction is a good one to
hinge an argument on. "the order of" could be repeated just for
clarity of exposition. Even if the typography is significant
here, I don't think it gets you where you want to go.
Some people think that the standard says the ordering of any
events whatsoever between sequence points is completely
unconstrained. But that's not what it says. If you read
the defining paragraph for sequence points, you'll see that
sequence points are mentioned in relation to the relative
order of _side effects_. In the Semantics paragraphs there
never is a statement that side effects are unordered
relative to expression evaluations. On the contrary, it's
always side effects of _previous_ evaluations.
The standard takes pains to point out _in every case_ in the
various Semantics paragraphs each case where sequence points
come into play in determining (or not determing) ordering,
and it's always about side effects, nothing else. Since
trouble is taken to point out _exceptions_ to ordering
relationships, the clear implication is that other parts of
the standard use words with their normal sense of causality
and ordering relationships.
The sentence quoted above is just confirmation that the
standard intends to talk about (non-)ordering of side effects
relative to each other, and not relative to other events.
Keeping these in mind, read through section 6.5, noting
which paragraphs are marked "Semantics" . Expressions
are evaluated "as specified by the semantics".
OK. Let's look at section 6.15.6 "Assignment operators":
3 ... The side effect of updating the stored value of the
left operand shall occur between the previous and the
next sequence point.
4 The order of evaluation of the operands is
unspecified. ...
It seems pretty clear that both the order of evaluation of the
operands and the time at which the result is stored are both
rather unconstrained.
Only when read in isolation. When read in conjunction with
the other statements quoted above, since the evaluation
_produces_ the side effect, obviously the evaluation has to
come first.