Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I was confused as to why a compiler might do that.

Surely a jump to register would always be faster than writing it out to memory.

Then I remembered we are probably talking about 16bit code (for Win16), and that far pointers were a major thing. And turns out there is no "far jump to register" instruction in x86. The far jump instruction requires the 32bit pointer to be in memory. I guess some clever compiler engineer came up with a trick to use two pushes and a far return instruction to avoid needing to allocate a temporary 32bit variable on the stack.

But that raises a deeper question: Why is this compiler using this clever "far jump to value in register" trick for virtual method dispatch? The full 32bit far pointer should already be sitting in the vtable, presumably it just loaded it from there? It would be a lot faster to just use the proper far jump instruction directly on the value in the vtable rather than loading it and writing it back out to the stack.

I suspect this compiler was deficient, and missing the optimisation that would avoid the need for this "clever trick" most of the time.



Main reason is probably just that x86s have so very few registers.

Most C++s have a pointer to the current object (bp), virtual objects then contain a pointer to a vtable (to save space) so it's a double indirect load - also I suspect (it was a long time ago) that parameters were already being passed in registers, they would have had to leave a free reg to use as a temp




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: