home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.arch:10427 comp.lang.misc:3505
- Newsgroups: comp.arch,comp.lang.misc
- Path: sparky!uunet!ukma!darwin.sura.net!zaphod.mps.ohio-state.edu!cs.utexas.edu!bcm!rice!sabry
- From: sabry@rice.edu (Amr Sabry)
- Subject: Re: A challenge to the anti-goto
- Message-ID: <Bx9G9F.4E2@rice.edu>
- Originator: sabry@datura.cs.rice.edu
- Sender: news@rice.edu (News)
- Reply-To: sabry@rice.edu (Amr Sabry)
- Organization: Rice University
- References: <Bwznzx.Dr1@mentor.cc.purdue.edu> <Bx0BuG.s4@mentor.cc.purdue.edu> <1992Nov5.111943.21554@harlqn.co.uk> <Bx8x1y.C2@mentor.cc.purdue.edu>
- Date: Thu, 5 Nov 1992 20:44:02 GMT
- Lines: 45
-
-
- In article <Bx8x1y.C2@mentor.cc.purdue.edu>, hrubin@pop.stat.purdue.edu (Herman Rubin) writes:
- |> In article <1992Nov5.111943.21554@harlqn.co.uk> daveb@harlqn.co.uk (Dave Berry) writes:
- |> >It's easy to get rid of these gotos. Every goto can be replaced by a
- |> >tail-recursive function call. This can be compiled into the same machine
- |> >code.
- |>
- |> This is only part of the challenge. The other part of the challenge is
- |> to do this in such a way that the resulting code will be at least nearly
- |> as efficient. In order to accomplish this, one thing necessary is that
- |> every one of the function calls be a totally-non-context switching function,
- |> with nothing added to the stack. Now it matters not to me what you call
- |> such a beast, BUT IT IS A GOTO.
-
- Yes, it is a goto, and this is exactly what Berry said. A function
- call in a "tail" position is a goto. Some languages require the
- compiler to recognize this and some don't. However, most descent
- compilers will do it anyway.
-
- |> I have never seen ML code before. If the above features could be added,
- |> I would have no problem with it. But, as I have pointed out, these
- |> features are equivalent to reordering code, gotos, and dropthroughs,
- |> and they do not seem to be provided in the language. Would the compiler
- |> do it? If so, it meets the challenge for this code.
-
- An example:
-
- factorial(n, result)
- if n=0 then result
- else fact (n-1, result*n)
-
- is compiled as if it was written like:
-
- factorial(n,result)
- label 100: if n=0 then result
- else begin n = n-1
- result = result * n
- goto 100
-
- No context-switching, no stack, nothing, just a goto.
- --
- Amr Sabry sabry@cs.rice.edu
- ---------------------------------------------------------------------
- Man will occasionally stumble over the truth, but most of the time he
- will pick himself up and continue on.-- Churchill's Commentary on Man
-