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: Bakul Shah Newsgroups: comp.compilers Subject: Re: Is multi-level function return possible? Date: Sat, 15 Mar 2014 10:28:08 -0700 Organization: Sonic.Net Lines: 62 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <14-03-042@comp.compilers> References: <14-03-020@comp.compilers> <14-03-022@comp.compilers> <14-03-025@comp.compilers> NNTP-Posting-Host: news.iecc.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: miucha.iecc.com 1394942912 9884 2001:470:1f07:1126:0:676f:7373:6970 (16 Mar 2014 04:08:32 GMT) X-Complaints-To: abuse@iecc.com NNTP-Posting-Date: Sun, 16 Mar 2014 04:08:32 +0000 (UTC) Keywords: Pascal, code Posted-Date: 16 Mar 2014 00:08:30 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:1164 On 3/11/14, 3:15 PM, Kaz Kylheku wrote: > > Recursion means there is possibly more than one activation of the environment > which contains the target label of the goto. Which activation is chosen as the > jump target? Does the goto return to the same recursion level in which > the closure was created or does it just find the innermost activation of > that label? > > If the closure jsut goes to the innermost block which has the label, > then the target resolution is in fact dynamically scoped, even though > the label is lexical. (Because the target block is not lexically captured > along with the closure, but chosen just from whatever is visible at the > time the goto takes place.) No, the label/goto scoping is strictly lexical. What happens is you pop all the frames until you reach the frame of the correct lexically enclosing routine with the defined label and then set PC to the address of the instruction the label points to. Here's a convoluted example using C syntax (but Pascal's goto and nested function semantics): typedef int(*int_f)(int); int fun(int_f g) { return g(1) + g(0); } int top(int v, int_f f) { int sub(int x) { if (x == 0) goto lab1; return x+1; } int HOfun(int_f argfun1) { top(v-1, argfun1); } if (v > 1) return HOfun(sub); return fun(f); lab1: return -1; } When top is called with v=2 and f=some random function, it will call HOfun with argfun1=sub. HOfun calls top again and passing its argfun1. Now top calls fun with the passed in function, which sub (but from the previous call to top). When fun calls g (which is really sub) with x=0, the stack has these activation frames: top // original call HOfun top // call from HOfun fun sub Since sub's static link points to the frame of the original call to top, four frames will be popped & PC set to the instruction named by lab1. BTW, label declarations have to precede function/procedure declarations so that a one pass compiler can identify the correct enclosing scope!