Ken Brody said:
On 7/8/2013 9:14 PM, Ian Collins wrote:
(snip)
It doesn't matter. Simply loading an invalid address
(which is likely to happen when it calculates the value of "p-1")
into the register can cause a fault.
Well it can, but for systems where that is true, and where the
computation can (or must) be done in other registers, it is usual
to do it such that it won't cause a fault.
Some time ago I was considering the problem of a C compiler
generating JVM code. The JVM 'reference' is opaque. There is no
operation that will increment or decrement such an object.
The offset is supplied at dereference time.
JVM code to represent a C pointer would have an origin
(reference object) and offset.
Note also the C restriction on comparing pointers to different
objects. In large model x86, the compiler only compares the offset.
If you use a relational operator on different object, the result
may be surprising.
Even if the implementation does fault on generating an invalid
address, all the C compiler has to do is be sure not to generate
such an address until it is actually time to dereference it.
That is, keep the origin and offset separate. There is a good
chance that the system will require that, anyway.
-- glen