home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / arch / 10404 < prev    next >
Encoding:
Internet Message Format  |  1992-11-04  |  3.1 KB

  1. Xref: sparky comp.arch:10404 comp.lang.misc:3489
  2. Newsgroups: comp.arch,comp.lang.misc
  3. Path: sparky!uunet!pipex!harlqn.co.uk!harlqn!daveb
  4. From: daveb@harlqn.co.uk (Dave Berry)
  5. Subject: Re: A challenge to the anti-goto
  6. Message-ID: <1992Nov5.111943.21554@harlqn.co.uk>
  7. Sender: news@harlqn.co.uk (Usenet News Account)
  8. Organization: Harlequin Ltd, Cambridge, UK
  9. References: <1css0nINNc51@agate.berkeley.edu> <Bwznzx.Dr1@mentor.cc.purdue.edu> <Bx0BuG.s4@mentor.cc.purdue.edu>
  10. Date: Thu, 5 Nov 1992 11:19:43 GMT
  11. Lines: 136
  12.  
  13. It's easy to get rid of these gotos.  Every goto can be replaced by a
  14. tail-recursive function call.  This can be compiled into the same machine
  15. code.  Here is Herman's example, gratuitously rewritten in ML.
  16.  
  17. (Note: I decided not to make the program functional, for clarity.  Also
  18. for clarity I left out all occurrences of the dereference operator.  I
  19. don't know if an existing ML compiler would produce code as efficient as
  20. the original C - they aren't usually optimised for this sort of work.
  21. But that's not the point - you could write this in C if your compiler
  22. optimises tail-recursion.)
  23.  
  24. (Another note: In both ML and C it would be possible (and possibly
  25. advisable) to use other features of the language to structure some
  26. parts of this program.)
  27.  
  28.  
  29. let
  30.   val n = ref 0
  31.   val m = ref 0
  32.   val b = ref B
  33.  
  34.   fun start() =
  35.   ( g := ...;
  36.     body()
  37.   )
  38.  
  39.   fun body() =
  40.     case g of
  41.       1 => add()
  42.     | 2 => ()
  43.     | 3 => start()
  44.     | 4 => label2()
  45.     | 5 => start()
  46.     | 6 => start()
  47.     | 7 => (g4 := 1; label3())
  48.     | 8 => ( h := ...;
  49.              g4 := h>>1;
  50.              if h&1 then
  51.              ( g4+=2;
  52.            label3()
  53.              )
  54.              else
  55.                label4()
  56.            )
  57.     | _ => if g > 8 andalso g <= 12 then
  58.              start()
  59.            else
  60.            ( g16 := (g-9)>>4;
  61.              g &= 3;
  62.              if g=1 then
  63.            label5()
  64.              else if g=2 orelse g=3 then
  65.            start()
  66.              else
  67.              ( h := ...;
  68.            g4 := (h+1)>>1;
  69.            if h&1 then
  70.            ( j := 6;
  71.              labelh()
  72.            ) 
  73.            else
  74.            ( j := 7;
  75.              g := ...;
  76.              if g mod 3 <> 2 then start() else loop()
  77.            ) 
  78.              ) 
  79.            )
  80.  
  81.   fun add() = (n += 1; start ())
  82.  
  83.   fun loop() =
  84.     if bit then
  85.       labelh ()
  86.     else
  87.     ( j += 1;
  88.       g := ...;
  89.       if (2<<(g-1)/j)&1 = 0 then
  90.     start()
  91.       else
  92.     loop ();
  93.     )
  94.  
  95.   fun labelh() =
  96.   ( i := C(j);
  97.     b >>= 1;
  98.     if i=j orelse i=0 then
  99.       labelh()
  100.     else
  101.     ( m |= b;
  102.       case i of
  103.     1 => ()
  104.       | 2 => label2()
  105.       | 3 => label3()
  106.       | 4 => label4()
  107.       | _ => label5()
  108.     )
  109.   )
  110.  
  111.   fun label5 () =
  112.   ( b >>= g16;
  113.     m |= b;
  114.     g := ...;
  115.     if bit then
  116.       if g&1 then label2() else ()
  117.     else
  118.     ( g4 := (g+1)>>1;
  119.       if g&1 then label3() else label4()
  120.     )
  121.   )
  122.  
  123.   fun label4() =
  124.   ( g := ...;
  125.     if g4 < g then
  126.     ( m |= (b>>g4 | b>>g);
  127.       ()
  128.     ) else
  129.     ( m |= b>>g;
  130.       if bit then () else (b >>= 1; label3())
  131.     )
  132.   )
  133.  
  134.   fun label3() =
  135.   ( b >>= g4;
  136.     m |= b;
  137.     b <<= 1;
  138.     label2()
  139.   )
  140.  
  141.   fun label2() =
  142.   ( g = ...;
  143.     m |= (b>>g);
  144.     ()
  145.   )
  146. in
  147.   start()
  148. end;
  149.