r/transprogrammer • u/definitelynotagirl99 • Jul 20 '24
Implementing exceptions
Just wanna know if anyone knows a more time-efficient way of implementing exceptions in a language than this or sees any issues with this design :3

handlethrow would exist separately for each try-catch clause
(obv this will only work for x86 but smth similar should work for all major architectures)
15
Upvotes
8
u/ato-de-suteru Jul 20 '24
The level of assembly is way out of my depth, but the trend these days seems to be to not throw exceptions (and interrupt the flow of the program) but to return an error type of some kind, eg. Rust's
Resultenum or Go's practice of returning a 2-tuple from everything and null-checking the element that might be an error.I don't know how that looks at the machine code level, but maybe it's worth considering?
3
u/anydalch Jul 21 '24
I don't know enough Intel ASM to give you good feedback on what you've written, but the typical way to compile exceptions as far as I'm aware is:
struct { unwind_stack_to: *mut StackFrame, handle: fn() -> ! }, i.e. a stack pointer paired with a code address. You may also need a frame pointer if you use such a thing.To install any exception handler, incl. an "unwind-protect"/destructor, push the current stack pointer and a handler onto the handler stack. The current stack pointer is pretty self-explanatory. The handler should be a code address you can branch to which will:
catchcode and then proceeds with normal execution.unwind_stack_toaddress, then branch to thehandleaddress.Compilers for languages where exceptions are rare and "unwind-protect"/destructor style handlers are common will ditch the handler stack and instead walk the main stack frame-by-frame, looking up return addresses in a binary tree somewhere in rodata to determine if each frame has a handler installed. That's beneficial for e.g. Rust and C++, where a majority of stack frames will need to be visited during unwinding to run destructors, but throwing an exception is, well, exceptional, and so is allowed to be slow. It probably doesn't work if you want to use exceptions as common control flow, since it means you pay to unwind stack frames between handlers in addition to the handlers themselves.
2
u/Mai_Lapyst Jul 22 '24 edited Jul 22 '24
It seems fine, I too thought about implementing exceptions somehow via return values. I find it interesting (i.e. a bit genious) that you use the carry flag to indicate an exception. One thing i dont know currently is if
call/retrestores the cpu flags.... Apart from that your only issue could be that other binary languages do not do that, so you'll have to properly document your own calling convention, and maybe think how you wrap external libraries, if you even allow linking against arbitary so files. If not (or anything is static) then there should be no problems with this!And thanks for the idea with the carry flag :3
Edit: only thing that might get a bit tricky if how you pass the exception instance;
raxis free bc its usually the return, but it would maybe slow down exception handling if you put the ptr to the object in it due to indirection in the catch-check(s). You could use two registers but that increases the amount you need to save on the stack in order to not override any meaningfull values in registers..... Tricky.