r/Compilers • u/mwksoul • 22d ago
An alternative, allocation-free exception handling model designed for low-latency systems and kernels
https://jamsilva.github.io/posts/exciting-exception-excursions/Hi everyone! In the post, I talk a bit about current implementations of exception handling and then I present a new one that I designed for my own programming language and could be used in places where exceptions usually have to be disabled, like kernels. Let me know your thoughts :)
24
Upvotes
6
u/SwedishFindecanor 22d ago edited 22d ago
In the earliest systems programming languages with Exception Handling, it was a way to model hardware exceptions, such as e.g. division by zero, interrupts etc. That is what the name "Exception" comes from. On many processors, exception handler routines shares the stack with user-mode so when one is started it creates a new stack frame on the top of the stack. Each hardware exception type has a "vector", i.e. a pointer to its current handler function At the start of each "try block", you would save the old function and overwrite the vector. Then restore the old pointer value afterwards. Later, people came up with the idea of having software exception as well.
The model is as if you'd pass a handler forward down the stack instead of unwinding to the handler. In some languages the handler is a closure (with access to local variables in its context) which can resume execution to after where the exception was raised (
raisecan be an expression with a return value!), or it could choose to do a non-local return (unwinding) back to where the closure had been created.Exception handling could still be zero-cost unless raised by having the runtime walking the stack and looking up metadata for each call site -- just like what most C++ runtimes do.
Herb Sutter once proposed this method for C++'s runtime, but was voted down because it would break existing ABIs. The method is sometimes referred to as "herbceptions" :). It is also a very old assembly programming idiom.
Among rejected proposals for CLU's runtime (many years earlier) had been to have either an exception return address or a table with exception return addresses at a fixed offset from the normal return address.
2
u/nculwell 22d ago
Seems to me that if you have structured payloads then you can still get dynamically formatted error messages if you want to. You could include a static format string with the exception. Then just do the actual formatting when it's time to print the message.
2
u/tending 21d ago
This isn't zero cost in the happy path. Traditional exceptions are zero cost in the happy path precisely because they don't need branching to check for flags or sentinel values. Branch prediction reduces the cost, but it's not free.
1
u/padfoot9446 22d ago
i don’t see how this is substantially different to rust. A single ? per call isn’t much of a hassle; marking the function throw is no different to -> Result<T, int> (or a massive enum table, or anyhow instead of int).
1
u/EggplantExtra4946 22d ago
You're talking about exceptions as if they were interrupts. They are not the same thing.
1
u/severelywrong 19d ago
Hey, you mention that the Library ID is assigned at load time via relocation. I have a rough understanding of how relocation works, and would be very interested how one can derive a unique 16 bit value with it!