0

So I made a basic stack-based virtual machine and a compiler that compiles bytecode for it, but I've run into an issue that I have no idea how to solve.

I need to check for things like dividing by zero and stack overflows and throw runtime errors, but the only way I understand to do this is to use exceptions (I'm doing this in C++), or by writing the ifs myself. But putting ifs all over the place will make the VM really slow.

What is the best way to implement runtime errors that ideally uses no CPU until an actual error is thrown?

Seki
  • 11,135
  • 7
  • 46
  • 70
Alex
  • 846
  • 6
  • 16

1 Answers1

1

I don't know about the best, but in a byte-code interpreter that I wrote I chose to have a try/catch around the main switch statement in the interpreter. Yes, that does mean you have an implicit exception frame around every statement, which adds a certain run-time overhead, but this is an interpreter after all, so for me that overhead is bearable.

  • Yeah, that's about as far as I got as well. I'm on Windows, so now I'm trying to figure out a combination of SetUnhandledExceptionFilter() and RaiseException() etc., but so far no success. – Alex Feb 13 '16 at 06:48