4 ms·
Checking error codes adds overhead only in the way it is implemented in modern programming languages, by returning an integer error code that must be tested for
by adrian_b 2y ago
Checking error codes adds overhead only in the way it is implemented in modern programming languages, by returning an integer error code that must be tested for a conditional jump after returning to the invoking procedure.
Many early programming languages, including various dialects of Algol and Fortran, included a better implementation method.
The invoking procedure passed to the invoked procedure not a single return address, but multiple return addresses. One return address was for the normal return and the other addresses were for alternate returns when errors were detected.
On a modern CPU with many registers, all these return addresses would be passed in registers.
The invoking procedure had at its end one or more handlers for error conditions, corresponding to the alternate return addresses. When a procedure was invoked, no tests were done at the invocation place, because any error would cause a jump to the error handler. In the invoked procedure, a test and a conditional jump must always exist in order to detect an error, and there the alternate return was executed only when an error was detected, so this implementation could omit one test and one conditional jump in comparison with the modern implementations.
Moreover, the text of the procedure was more clear in this way, with all the error handlers separated from the normal execution case, but nonetheless close enough to examine them when necessary.
- protomolecule 2y agoThanks, that's an interesting idea, even though I'm not sure that extra instructions to put these return addresses into registers and reduced number of registers available for the codegen is better than cmp and jmp. Also I'm not sure if that will play well with cpu's branch predictor.