Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED!nerds-end From: George Neuner Newsgroups: comp.compilers Subject: Re: Is multi-level function return possible? Date: Tue, 11 Mar 2014 13:56:01 -0400 Organization: A noiseless patient Spider Lines: 46 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <14-03-023@comp.compilers> References: <14-03-020@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: miucha.iecc.com 1394571686 65602 64.57.183.58 (11 Mar 2014 21:01:26 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Tue, 11 Mar 2014 21:01:26 +0000 (UTC) Keywords: code, history Posted-Date: 11 Mar 2014 17:01:26 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:1145 On Mon, 10 Mar 2014 15:46:31 -0400, noitalmost wrote: >Is [multi-level return] a doable thing for a Pascal-like language that is >meant to be compiled? I have a multi-level break which works within a >procedure, but the return across procedure boundaries seems to add a lot >of complications. > >I don't recall this being discussed in the dragon book, perhaps because it's >just a silly idea. But implementing the multi-level break got me curious >whether it's possible. The modern term is "exception". See also "non-local" control transfer, exit and/or return, and "escape" or "backtracking" continuation. I don't have my book handy to check but I *think* the purple dragon has a discussion of exceptions. I'm pretty sure the older books don't explicitly cover non-local control transfer, but careful reading of the sections on control flow should allow inferring an implementation. >[Sure, you can do it. PL/I let you jump out of a routine and I think Algol60 >may have, too. You just need to keep your stack frames sufficiently linked >so that you can find the one you want and unwind everything below it. Look >at the way Algol and Pascal reference variables in blocks outside the current >routine and you'll get most of what you need. -John] The real issue is whether you simply need to abort the computation (perhaps with a return value) or whether you need to clean up side effects: e.g., free heap allocations, restore variable values, etc. The OP describes the language as "Pascal-like", but that doesn't necessarily mean "simple" ... there certainly are modern OO and GC'd Pascal derivatives for which the notion of "cleaning up" after an exception can be quite involved. The most primitive mechanism - performing only control transfer - is something like C's setjmp/longjmp: you save the return point register state (as if you are calling a function [even if you aren't]) and you arrange that the state is [at least name] visible to the lower level functions. In a Pascal-like language the simplest thing is just to keep the saved state on the stack as a local variable of the upper-most function and let lower functions refer to it as a non-local variable. George