[comp.std.c] assignment to/from volatile execution sequence

lai@mips.COM (David Lai) (03/05/90)

In a simple expression:

	a = b = c;

where:
	int a,c;
	volatile int b;

In determining the value to be assigned to a, the standard says in
section 3.3.16, "An assignment expression has the value of the left
operand after the assignment".  Suppose 'b' is volatile, then we can't
expect the value of b after the assignment to have any relation to the
value stored into it (namely c).  So the question is, can a=b=c be
implemented as b=c, a=c.  Or does the standard force it to be
implemented as: b=c, a=b.

I give an example, suppose 'b' is an I/O port, writing to b outputs a
character, and reading b reads an input character.  This is a case
where reading the value of b may not necessarily be the value written
to it.  So the programmer writes:
	a=b=c;
expecting to output the value c, and inputting an unknown value
into a.  The value of the left hand side (of b=c), after the
assignment, may not be c.  Can an implementation "expect" the
value of "b=c" to be "c"?

Another related question, does the left hand side have to be fully
evaluated to determine the value of an assignment expression:

	a = *(complicated_expression_with_side_effects) = c;

In other words, will the complicated_expression_with_side_effects be
evaluated once or twice when the above statement is executed.
Example:

	int a,*b,c;

	a = *(b++) = c; /* will b be incremented twice? */

-- 
        "What is a DJ if he can't scratch?"  - Uncle Jamms Army
     David Lai (lai@mips.com || {ames,prls,pyramid,decwrl}!mips!lai)

karl@haddock.ima.isc.com (Karl Heuer) (03/06/90)

Taking the easy question first...

In article <36669@mips.mips.COM> lai@mips.COM (David Lai) writes:
>	a = *(b++) = c; /* will b be incremented twice? */

No.  Even if the abstract machine semantics require you to fetch back the
value from memory, the location of the object denoted by *b++ has already been
determined and should not be re-evaluated.  This is the same reason that
	*p++ += 1;
increments p only once.

>[What happens if the middle lvalue is volatile?]

Good question.  Is it intended to be the value that was stored there, or the
value that is read back?  These are equivalent for nonvolatiles, and the
Committee may have overlooked this situation.  I suggest you write the X3
Secretariat for an official interpretation.

Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint
(I also strongly disrecommend writing any code that depends on such nuances!)