On Sat, Jun 6, 2009 at 1:01 PM, Neil Bradley <nb@...> wrote:
> Graham, as usual, you are THE MAN. This is really cool! For extra bonus
> points, you should work in register calling conventions. ;-)
When I do a native code generator, I'll do that and throw all the
optimising tricks at it, but as long as I'm treating the x86 as a
simple stack machine, the rest of the generated code is so inefficient
that it is not worth the effort.
But for teaching/demo purposes, I can do quite a few optimisations at
the virtual stack machine level, such as CSE etc.
Here's a trick I learned from the KDF9 guys:
to increment to top of stack: NOT; NEG
to decrement it: NEG; NOT
:-)
relies on 2's complement arithmetic of course, but that's pretty much
a given nowadays.
It's interesting to look at the generated code and see just what most
of the instructions are... pushes and pulls, obviously, but the
remainder being a few adds, and comparisons/branches. You can see how
the RISC guys came to the conclusions they did. Compare this to the
KDF9's instruction set which is CISC in the extreme and you ask
yourself why did they bother?
It'll be interesting to do an encoding of the stack machine as a byte
code and see how small I can make the binaries. (Of course the real
test is combined size of byte code plus interpreter)
G