B
Bartc
Willem said:Bartc wrote:
) There's probably a misunderstanding. A typical asm listing of x=x++
might
) be:
)
) (1) mov R,[x]
) (2) inc [x]
) (3) mov [x],R
Another typical asm listing of x=x++ might be:
(1) mov [x],[x]
(2) inc [x]
That one might be tricky..
And yet another, on a CPU with special post-increment instructions:
(1) mov R,[x]
(2) mpi R,R (move, post-increment)
(3) mov [x],R
In this case, the mpi instruction does the move and the increment
*at the same time*!! This is roughly how it works:
mpi is only register to register? C's ++ works on l-values. And if mpi
expects to work on two distinct registers then I'd say this is a code
generation problem. Assuming the increment is on the right-hand register,
this can be coded as:
mov R,[x] R=1
mpi S,R R=2, S=1
mov [x],R x=2
mov [x],S x=1
although I would understand the mov [x],R being optimised out.
At the clock tick, the input of R has *two* outputs connected to it.
Now, as it happens, the circuitry is such that if two ones or two zeroes
are both connected to an input, then it becomes one or zero respectively,
but if a one and a zero are connected, then the zero wins out(*).
(Of course, this works for each bit separately)
(And that sounds like a hardware problem, usually you can't tie two logic
outputs together, unless they're open-collector or something, according to
my 1983-vintage electronics knowledge)
) It's possible that some crazy compiler might reverse the order of lines
(2)
) and (3) for some reasons best known to itself, but it seems obvious to
me
) that the right-hand-side of an assignment should be evaluated before
storing
) it in the left-hand-side, which in this case requires no calculation.
Could you explain why this seems obvious to you ? Obviously it does not
seem so for most people.
Because in x = expr you might expect the expression to be evaluated first
before you assign it's value to x.
Besides I've always considered ++x and x++ as single, indivisible
operations. Now you've demonstrated some architectural scenarios where it
would be tricky (ie. take extra effort) to keep these effectively
indivisible.
All the code generators I've worked on (forgetting the distant past) for x86
and virtual machines, exhibit this indivisible behaviour, but then ++ and --
have always been troublesome to work with anyway.