Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!snorkelwacker!mit-eddie!uw-beaver!zephyr.ens.tek.com!tekcrl!tekgvs!toma From: toma@tekgvs.LABS.TEK.COM (Tom Almy) Newsgroups: comp.lang.forth Subject: Re: New Directions: 'I' is broken in Forth Message-ID: <6720@tekgvs.LABS.TEK.COM> Date: 22 Jan 90 16:40:05 GMT References: <285.UUL1.3#5129@willett.UUCP> Reply-To: toma@tekgvs.LABS.TEK.COM (Tom Almy) Organization: Tektronix, Inc., Beaverton, OR. Lines: 64 In article <285.UUL1.3#5129@willett.UUCP> dwp@willett.UUCP (Doug Philips) writes: [Doug proposes an intelligent "IMMEDIATE" loop index function instead of the current stupid one] >There would be, IMHO, two major gains by doing this. First, the >restrictions about using R>, >R before/during/after DO LOOPS would >be lifted. Second, the semantics of I J etc. would be simpler. In my Native Code Compilers (which compile complete colon definitions as CODE words) DO LOOP constructs are compiled in two passes (everything else is single pass BTW). The word DO (or ?DO) causes a scan ahead to the matching LOOP (or +LOOP), looking for any usage of the loop index. Depending on how many arguments of DO are literals, presense of I, and termination by LOOP or +LOOP one of about a half dozen different looping structures are compiled. If I does not appear, the loop index is not even maintained -- a simple count down to zero loop (using a register) is used. Sometimes the index is maintained in a register, sometimes on the return stack. If the limit is a literal, then it need not be saved, otherwise it needs to be saved on the return stack. "I" is smart enough to compile the correct code. If the index is in a register, and the limit is a literal then >R and R> can be used across a loop boundary, but the programmer can never be certain. Then again, the 83 standard prevents making such assumptions -- and it is a good thing because it allows me to do far more optimizations than I would otherwise be able. BTW, both my NCC and your scheme would have a tough time handling this (illegal but common) abommination: : y r> I . >r ; : x ." Prints 1 to 10: " 11 1 do y loop ; >I have seen reference to 'control stacks' (probably in Forth Dimensions, but >I can't recall) for solving this problem. Has anyone been using them or >are they just an acedemic exercise? STOIC had a separate loop stack, and thus got around the problem. >and if 'yes', have control stacks >been found to introduce new problems? (Set aside 'efficiency', for the >moment, I mean semantic problems or difficulties). It didn't seem to cause any problems at all. >I'm beginning to be of the opinion that DO LOOP and its use of the >return stack is not an elegant design, but rather one that was hacked >on top of the existing machinery. Unlike the Fortran arithmetic if statement, the Forth DO LOOP doesn't seem to map onto any hardware. And if anything, having two stack (let alone three) rather than a single stack causes hardware problems. If you avoid the use of the return stack (a recommendation I heartily gave students in my Forth classes) then it works just fine! Just use the return stack to help juggle values on the parameter stack, or better yet use a variable or two. >I am also beginning to be of the opinion that Forth is flexible enough to >allow you do 'do it right' and still be efficient. Well, yes. My NCC generates tighter code than even the best C compiler around. Tom Almy toma@tekgvs.labs.tek.com Standard Disclaimers Apply