From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!tethys.csu.net!nntp.csufresno.edu!sn-xit-03!sn-xit-09!sn-xit-08!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: CTips Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: Teaching new tricks to an old dog (C++ -->Ada) Date: Mon, 14 Mar 2005 16:13:58 -0500 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <113bvkqbb8q0038@corp.supernews.com> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.3) Gecko/20040910 X-Accept-Language: en-us, en MIME-Version: 1.0 References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110377260.350158.58730@z14g2000cwz.googlegroups.com> <1136bh3li136dac@corp.supernews.com> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@supernews.com Xref: g2news1.google.com comp.lang.ada:9395 comp.lang.c++:45670 comp.realtime:1476 comp.software-eng:5042 Date: 2005-03-14T16:13:58-05:00 List-Id: Dr. Adrian Wrigley wrote: > On Sat, 12 Mar 2005 12:59:31 -0500, CTips wrote: > > >>Robert A Duff wrote: >> >> >>>Kilgallen@SpamCop.net (Larry Kilgallen) writes: >>> >>> >>> >>>>Even Bliss has nested functions. What Ada has that Pascal has in addition >>>>to nested functions is uplevel addressing, allowing an inner function to >>>>access data declared in outer scopes. >>> >>> >>>Heh? Pascal has that. In fact, practically every programming language >>>outside the C family has this feature. It's quite useful -- almost >>>essential in multi-threaded programs. >>> >>>- Bob >> >>Yeah, and don't ask what it costs you. I'd carefully forgotten about all >>the grungy details about displays and static/dynamic chaining, and you >>had to remind me. I particularily like solutions that reserves a >>register for the top-of-display/top-of-static-chain. Thats right - blow >>away a register for that. And then of course the cost of >>maintaining/walking those structures. >> >>If you need thread-private storage, there are *much* cheaper solutions. > > > isn't uplevel addressing usually zero cost? Are you saying it is > expensive whenever you use it? Or expensive on all programs, whether > or not it is used? Is it absent from C++ because of cost? > (I'm sure Robert understands this far better than I!) My knowledge of this is a little dated and hazy, so I could be wrong. If we're doing something like: foo() { int x, y; bar() { int y; gah() { use(x), use(y); } } } then, at run-time, in use(x), x comes from the stack-frame of the nearest invocation of foo() on the stack, and in use(y), y comes from the stack-frame of the nearest invocation of bar(). There has to be a mechanism to identify the nearest invocation of foo()/bar(). There are, if I remember correctly, 4 mechanisms to find the invocation: - dynamic chaining: you just follow the back-chain pointers, stepping through all stack frames, till you come to the right stack frame. - static chianing: you maintain a separate chain (the static chain) that directly link to the stack frame of the enclosing functions. Thus the static chain for gah() would have the last invocation of bar() followed by the last invocation of foo(). - display: somewhat like the static chain, except its an array instead of a list - currying (?): this one I'm really hazy about, but, roughly, you passed pointers to the correct uplevel variables as extra arguments to the functions. Thus bar would be passed the pointer to x and gah would be passed pointers to x and y, as well as their other arguments, or something like that. There were some additional nastinesses dealing with what happens when a nested function is passed as an argument Depending on the techinques, you either pay whenever you access variables of enclosing functions, or you pay to maintain the data-structures [static-chain,display] which make this access cheaper, or both. On some implementations (on RISC architectures) a register is reserved for the static-chain. This means one less register for *every* function (or maybe only for enclosed functions?) when used with a language that support this kind of nested functions. C++ probably left it out because of its C heritage, while Ada probably dropped it in because of its Pascal heritage. IMHO, its probably not worth the cost.