I have an old C ++ project that I did some time ago. Well, this is a processor emulator. Whenever a CPU error occurs (for example, dividing by zero or debugging an interrupt interrupt, etc.), in my code it only has throw , and in my main loop I have something like this:
try{ *(uint32_t*)&op_cache=ReadDword(cCS,eip); (this->*Opcodes[op_cache[0]])(); eip=(uint16_t)eip+1; } catch(CpuInt_excp err){ err.code&=0x00FF; switch(err.code){ case 0: case 1:
And a simple code example (drawn out of thin air)
if(**regs16[AX]==0){ throw CpuInt_excp(0); //throw divide by zero error }
What this code basically does is simply read the operation code and, if an exception occurs, then call the appropriate interrupt (on the CPU, which just changes the EIP)
Well, this is in the main loop, the try{}catch{} premium really adds. This is not a premature optimization, I profiled it and the auxiliary gcc exception functions (without any failures and, therefore, without throws), and the auxiliary functions took more than 10% of the total execution time of a long emulated program.
So! What would be the best way to replace exceptions in this case? I would prefer not to keep track of the return values, because I already have a ton of code, and because tracking them really when the functions get very deep.
source share