3 ms·
> But it's generations of technical ideas, solutions, implementation details and understanding I have to go through. I would like to understand exception handli
by peterfirefly 3y ago
> But it's generations of technical ideas, solutions, implementation details and understanding I have to go through. I would like to understand exception handling to implement algebraic effects.
and upstream:
> Need to create a closure in assembly.
"I would like to know how nuclear reactors work so I can build my own. I'd like to skip the Schrödinger Equation, differential equations, and linear algebra.
And I want the nuclear reactor to run on thorium."
> I should probably get better at writing assembly
Yes.
Exceptions are not hard to understand once you know assembly language (any one of them). There are lots of blog posts you can look at. Algebraic effects are rather new and haven't been widely implemented yet ("thorium"). You most likely won't be able to find a pre-written document that spoon feeds you the implementation details.
A simple exceptions implementation uses a stack of "handlers". The code pushes and pops as it enters and leaves scopes. When an exception is raised, this stack is searched from top to bottom for a suitable handler (or maybe just the top handler is used). Precisely how depends on the implementation. The problem is that it's kinda slow to do all that work if exceptions are rare. Another implementation strategy is to have a table: the code address where the exception was raised is looked up in the table. That gives you the handler info. A bit more cumbersome if you have to handle linking. More so with dynamic linking. Even more so with run-time generated code.
Closures can be implemented in a billion and a half different ways. A common way is to allocate whatever local information ("captured variables") that the closure needs in a block on the heap. When the closure code is invoked, it gets a pointer to this block as a hidden parameter.
Of course, there are all sorts of code transformation tricks to complicate things...
Some you might run into often are transformations to and from SSA and CPS (+ the optimization transformations you can do on those):
https://en.wikipedia.org/wiki/Continuation-passing_style https://en.wikipedia.org/wiki/Continuation-passing_style
https://en.wikipedia.org/wiki/Static_single-assignment_form https://en.wikipedia.org/wiki/Static_single-assignment_form
If you are interested in these things, you should really know some semantics and type theory:
https://en.wikipedia.org/wiki/Operational_semantics https://en.wikipedia.org/wiki/Operational_semantics
https://en.wikipedia.org/wiki/Denotational_semantics https://en.wikipedia.org/wiki/Denotational_semantics
https://en.wikipedia.org/wiki/Type_theory#Technical_details https://en.wikipedia.org/wiki/Type_theory#Technical_details
You do not have to know all the myriad variants, of course.
Learning a little assembly language is easy compared to semantics and type theory. Just learn your linear algebra and stop looking for shortcuts. It's like wanting to learn calculus while still being uneasy about fractions.
- peterfirefly 3y agoForgot to mention another kind of exception handling that is very common: https://en.wikipedia.org/wiki/Microsoft-specific_exception_handling_mechanisms https://en.wikipedia.org/wiki/Microsoft-specific_exception_h... https://learn.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp?view=msvc-170 https://learn.microsoft.com/en-us/cpp/cpp/structured-excepti... Yes, Windows NT (and up) has a cross-language exception handling mechanism, separate from and in addition to whatever C++, Modula-3, etc have.