On 20-Mar-12 05:00, James Kuyper wrote:
> On 03/20/2012 04:52 AM, Stephen Sprunk wrote:
>> On 19-Mar-12 13:30, James Kuyper wrote:
> ...
>>> Sort-of. Because of the as-if principle, &variable doesn't have to
>>> actually identify the location (if any) where the variable is stored
>>> unless some other part of the program would make the failure of the
>>> program to store it there detectable.
>>>
>>> For instance, if two non-overlapping objects have overlapping lifetimes,
>>> and the program prints out the values of pointers to those two objects,
>>> the print outs must identify different memory locations. If those
>>> addresses are compared for equality, they must compare unequal. If such
>>> a pointer is dereferenced, the behavior must be the same as if it
>>> actually pointed at the object it's supposed to refer to. However, if
>>> none of those things occur, printf("%p\n", (void*)&object) can print
>>> just about any arbitrary memory location that is not, detectably,
>>> currently in use for some other purpose, regardless of how (or whether)
>>> the variable is actually stored.
>>
>> All of the above sounds reasonable, but when/why would any reasonably
>> sane implementation (i.e. not the DS9k) take advantage of that dubious
>> flexibility rather than use the "obvious" approach of storing the
>> variable in memory?
>
> Storing the variable in memory takes memory space and time; why bother
> storing it there if you don't have to? A plausible implementation might
> reserve a piece of memory to store the value in, if it actually needs to
> be stored, and then find that it never actually needs to store the value
> there. &variable would return that address, but the object never actual
> resides in that memory. This doesn't sound like DS9K optimization to me.
Hmm. My mental model of how a compiler works is slightly different: the
compiler initially locates all variables in memory and then (provided
various conditions are met) migrates some of them to registers or
eliminates them entirely. Taking the address of the variable would,
logically, prohibit such migration or elimination. Allocating addresses
for memory variables would be done (probably by the assembler) long
after that took place. Allocating addresses for variables that were
migrated or eliminated seems illogical.
(Perhaps that model is too simplistic, and I welcome correction, but so
far it has served me well enough to understand most optimizations.)
S
--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
|