From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!weretis.net!feeder8.news.weretis.net!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Ada and "early return" - opinion/practice question Date: Tue, 16 Mar 2021 13:04:59 +0200 Organization: Tidorum Ltd Message-ID: References: <38356aa9-b8b0-4e0b-a490-99e7b239d0b1n@googlegroups.com> <87im5sutdt.fsf@nightsong.com> <861rcfe9w7.fsf@stephe-leake.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 9Oo35AePkiPsFj1Ltcc47Q0VpCygS3vUEuWj5D8eblCgcgWhXM Cancel-Lock: sha1:cZvNTzH4UFRuIjFYtUe893KI1ps= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 In-Reply-To: <861rcfe9w7.fsf@stephe-leake.org> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:61560 List-Id: On 2021-03-16 11:16, Stephen Leake wrote: > Paul Rubin writes: > >> Shark8 writes: >>> Exceptions, typically. Sometimes a return itself, typically in a >>> procedure though. >> >> This is interesting: I thought in the Java and C++ worlds, using >> exceptions to manage normal control flow was frowned on, in part because >> the exception mechanism is quite heavyweight. And in Haskell, >> exceptions are considered i/o effects and therefore nasty in pure code >> (code with side effects is "impure" and has a special type signature). >> >> Is there wisdom about this in Ada? > > GNAT has zero-cost exceptions; raising an exception is the same as goto. Are you sure about that? As I understand it, for the full-runtime GNAT systems, the "zero cost exception handling" means that there is no run-time cost to placing an exception handler in a subprogram or block, because there is no run-time code to set up such a handler when entering the subprogram or block, nor to remove it when leaving the subprogram or block. But there is cost, partly at compile/link time, when the tools set up a static mapping from code sections (address ranges) to the exception handlers that handle exceptions raised in those parts of the code, and partly at run-time when an exception is raised. AIUI raising an exception is not a mere goto: at run-time, the code has to use the static mapping to locate the applicable handler, while also unwinding the call-stack to the corresponding point, and then enter the handler code. So the GNAT "zero cost" exception system is optimized for having plenty of exception handlers in the code, but rarely having to handle exceptions during execution, which is as it should be, IMO. A different zero-cost system is used when GNAT compiles for embedded targets with run-times that do not support exception propagation. In those cases, one can write only "local" exception handlers. For example, the exception handlers in a subprogram can handle only exceptions raised in that same subprogram, but not exceptions raised in any callee subprograms. Then raising an exception is indeed the same as a goto, because the compiler statically knows if there is a local handler, and where it is, and no stack unwinding is needed. If there is no local exception handler, raising an exception is a "goto" the GNAT last-chance handler followed by program termination. But this applies only in these restricted run-time environments.