[comp.std.c] volatile registers, etc

chris@mimsy.UUCP (Chris Torek) (01/05/89)

In article <9316@ihlpb.ATT.COM> nevin1@ihlpb.ATT.COM (Liber) writes:
>I can ask the same question about volatile const.  All the volatile
>register means is that the alias to the variable cannot occur within
>the conforming C program.

`volatile const' is also peculiar, but if I write

	extern volatile const int clock;

I can (by jimmying the link) get the compiled code to refer to a
hardware clock.  Similary, I can take the address of a non-register
variable (global or local) and tell the hardware to map some peculiar
hardware object into that loction:

	f()
	{
		volatile const int clock;

		set_hardware_clock_addr(&clock);
		(void) printf("%d\n", clock);
		(void) printf("%d\n", clock);
	}

could again access a hardware clock.  But if I write

	f()
	{
		register volatile const int clock;

I have no way to find out where the compiler actually put that
variable.  It might be on the stack; it might be in a machine
register; it might be in some other machine-specific location.
But no matter where it is, if I leave off the word `register',
I can find it; if I put in the word `register', I cannot.

>I agree that I don't see [volatile registers?] as being particularly
>useful; that doesn't make it illegal, however.

True enough.  The point (back when there was a point) was that, being
useless, it would be possible to outlaw it.  Since the volatility
concept is still necessary, and since we all believe that compilers are
still too stupid to get the idea themselves, we would still want the
`volatile' keyword.  Given the keyword, I think it would be stupid to
outlaw `volatile register' just because it cannot be used: it does no
harm to leave it, and obviates the need to check for yet another
compile time error.

>>I did not say that.  I said `register implies not-volatile, not-
>>aliased': that `volatile register' and `aliased register' are
>>nonsensical combinations.

>Register only implies non-aliased when not qualified by volatile, in
>much the same way that const implies non-modifiable when not qualified
>by volatile.  Register is allowed to be aliased by hardware or by a
>different thread of execution, but a conforming C program cannot alias
>it.

Not only a conformant C program, but indeed any C program, unless it is
by taking advantage of known properties of the implementation (as
current Unix kernels do with PCC and register allocation).  Obviously
people do it; it remains a bad idea.  If and when we start using GCC
to compile 4BSD, we will have to rip out all those implementation
assumptions.

[If we used the `people do it now' argument, we would have to consider
legitimising (yeck, what a word) loops of the form
	for (p = &arr[N]; --p >= arr;)
which now exist and work by taking advantage of known properties of
the implementation---in this case, that &arr[-1] < &arr[0].]

>[Another side note:  is it even possible to have a strictly conforming
>C program whose behavior would change by declaring any of its
>variables volatile?  I cannot think of any examples without having to
>escape to having some library routines around that 'cheat'.]

I would venture to suggest that it is indeed impossible.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

jss@hector.UUCP (Jerry Schwarz) (01/07/89)

"volatile register" has a use.  Consider

void f() {
	jmp_buf env ;
	volatile register int x = 1 ; 
	if ( setjmp(b) ) { 
		printf("%d",x) ; 
			/* Guaranteed to print 1 
			 * because x is volatile */
		} 
	...
	}

Jerry Schwarz
AT&T Bell Labs, Murray Hill