Subject: Re: USB stack needs early review (Re: Someone should fix our USB stack...)
To: None <tech-kern@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 03/22/2007 18:11:41
>>> [...volatile...]
>> No, the compiler is not allowed to keep cached in a register a
>> memory access across a function call, precisely because it cannot
>> know if that function call modifies the memory.
> don't callee vs. caller saved regs give the compiler a way around
> this?

Not really.  The point is, if you write, for example,

	x = ...something...;
	somefunction();
	y = x;

then, in terms of the C abstract machine, the compiler must re-fetch x
when assigning to y rather than re-using the value from the former
assignment (which it may well have lying around in a register, whether
caller-saved, callee-saved, hardware-saved, or what, is irrelevant).
This is because, in terms of the C abstract machine, somefunction()
could modify x.

Unless, that is, the compiler can prove somefunction() doesn't have
access to x, or at least doesn't change it.  Examples of such cases are
when x is an automatic variable whose address is never taken (and which
is out of scope for somefunction(), which is always true in C but might
not be true in gcc), or when somefunction()'s code is available and
does not include any stores to anything that could possibly alias x.
volatile is the escape hatch via which the code author can tell the
compiler "you must not assume that all accesses to this datum occur
within the bounds of the C abstract machine", forcing the compiler to
access it exactly as written, rather than counting on the "as if" rule
to permit optimizations such as saving values in registers.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B