Let’s Write a Virtual Machine!

2 Responses

  1. A. Bouchez says:

    Your assumption about “case” being slow is plain wrong.
    The compiler (both Delphi and FPC) generates cascaded if .. then … else if … statements, but only for a few items.
    When it reaches a given number, the compiler generates a jump table.

    • Well first off, it’s neither an assumption, nor is it “plain wrong” – I generated and analyzed the assembler to be certain.
      I was unaware however, that the compilers will optimize larger case statements, that’s good to know, thank you.

      None the less, the jump table approach that I’ve taken will work, and it lends its self both to further optimization later, and to more advanced instruction decoding mechanisms. I’ll likely stick with it.
      Furthermore, the fact that I was able to demonstrate the problem, and yet you’ve expressed that the compiler does something different, highlights another reason for my approach of doing it manually. Doing it the way I have, means that I don’t have to guess at which point the case statement will be sufficient for the compiler to warrant optimization on it. This is even more important given that “Delphi” alone is not a single compiler but several, which behave differently under different targets and circumstances. I’d prefer to retain a tighter control on what is produced.

Leave a Reply