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

  1. Xref: sparky comp.arch:10543 comp.lang.misc:3550
  2. Path: sparky!uunet!think.com!news!bradley
  3. From: bradley@marley.think.com (Bradley Kuszmaul)
  4. Newsgroups: comp.arch,comp.lang.misc
  5. Subject: Re: CLU break and continue (Was: Re: A challenge to the anti-goto)
  6. Date: 9 Nov 92 10:18:22
  7. Organization: Thinking Machines Corporation, Cambridge MA, USA
  8. Lines: 89
  9. Message-ID: <BRADLEY.92Nov9101822@marley.think.com>
  10. References: <1cvoctINNmhs@agate.berkeley.edu> <PSM.92Nov3165530@soma.sics.se>
  11.     <1992Nov3.165005.18037@midway.uchicago.edu>
  12.     <184123@pyramid.pyramid.com> <721054332.26680@minster.york.ac.uk>
  13.     <1992Nov9.114323.22688@rdg.dec.com>
  14. NNTP-Posting-Host: marley.think.com
  15. In-reply-to: jch@rdg.dec.com's message of Mon, 9 Nov 1992 11:43:23 GMT
  16.  
  17. In article <1992Nov9.114323.22688@rdg.dec.com> jch@rdg.dec.com (John Haxby) writes:
  18.  
  19.    ...
  20.    It's not surprising that the Ada exception mechanism is similar to CLU's
  21.    since CLU is one of Ada's ancestors.
  22.  
  23.    CLU has two exception mechanisms for slightly different purposes.  One
  24.    mechanism is for returning an exception from a procedure call: a procedure
  25.    can signal an exception to be caught by its caller (the exception is not
  26.    propagated up the procedure call stack) or by a generic handler for
  27.    unhandled exceptions.  The other mechanism is a local form that allows a
  28.    block (eg in a loop or if-statement) to exit to an enclosing block.
  29.    Apart from the local/procedure-call difference the two mechanisms are
  30.    identical, although `signal' tends to be expensive and `exit' is simply
  31.    an unconditional branch. ...
  32.  
  33. The signal is not as expensive as you might think.  This is because the
  34. exception is not propagated up the call stack.  I remember descriptions of
  35. two implementations of signal, both of which had the property that
  36.  - if the signal is not raised, there is zero runtime cost (e.g., to set up the
  37.    signal handler.
  38.  - if the signal is raised, only a few instructions need to execute to
  39.    figure out where the signal handler is and to jump to it.
  40.  - if the appropriate handler is not defined, it only takes a few more
  41.    instructions to figure out where the `failure' handler is and to jump to
  42.    it.
  43. Both implementations take advantage of the fact that the return address on
  44. the call stack uniquely identifies the handler for each signal.
  45.  
  46. Basically the idea is that you use the return address to look up the signal
  47. handler in a hash table, if you don't find anything then you have to signal
  48. a `failure', which is handled the same way, but the compiler always sets up a
  49. `failure' handler for every call.  The hash lookup is very fast, typically
  50. only a few instructions.
  51.  
  52. This is in contrast to Ada, in which, for all the implementations I can
  53. think of 
  54.  - you pay something every time you set up a handler.
  55.  - when a signal is raised, you have to do some search up the call stack
  56.    for the right handler, doing some sort of hashtable lookup (or linear
  57.    search or ...) at every activation frame.
  58. I can imagine some hardware support for Ada style exceptions:  I believe
  59. that the Symbolics lisp machines provide some support for this (since these
  60. trace-up-the-call-stack-style-exceptions appear in LISP.)  (For that matter
  61. I can imagine hardware support for CLU style exceptions too:  it would
  62. probably be less hardware and would run faster...)  (There ..., I included
  63. something relevant to comp.arch.)
  64.  
  65. My opinion is that from a software engineering point of view, CLU got it
  66. right and Ada got it wrong.  The standard argument, by example, is this:
  67.   Suppose that you implement a "stack" abstraction, implemented using some
  68.   sort of "array" abstraction.  You might write
  69.      ...
  70.      pop = proc(s : stack[T]) returns(T) signals (empty) 
  71.        index : int
  72.        index := s.index
  73.        if(index=0) signal empty
  74.        else s.index = index-1
  75.             return s.array[index]
  76.        end
  77.      end
  78.          
  79.    Suppose that you make a mistake implementing your stack, and you forgot
  80.    that your "array" abstraction provides FORTRAN arrays (that are indexed
  81.    from 1 instead of from 0), so that the array indexing operation raises
  82.    a `bounds' signal (i.e., the bound was invalid).
  83.  
  84.    The code for `pop' does not handle the `bounds' exception.  What should
  85.    be done?
  86.     In CLU the `pop' does not handle a `bounds' exception, so `pop' raises
  87.     `failure', meaning that the code failed.  That is exactly what
  88.     happened.  If you are writing code that needs to be reliable, you can  
  89.     handle failure in various ways (e.g., by using three implementations
  90.     and voting on the answer:  If one of the implementations fails, then just
  91.     use an answer from one of the other two), so it is much better to know
  92.     right away that your code failed than to have the signal go
  93.     who-knows-where.
  94.  
  95.     In Ada, the `bounds' is then raised by `pop', but the `pop' interface
  96.     for `pop' does not mention `bounds', so the abstraction is broken.
  97.     Higher up the call tree somewhere, someone who was handling a `bounds'
  98.     for some completely other reason (e.g., because in some football-field
  99.     abstraction, the ball might leave the playing field), gets this completely
  100.     bogus `bounds' signal and the referee abstraction gets completely
  101.     confused.... 
  102.  
  103. However, the Ada committee did not buy this argument, so ...
  104. -Bradley
  105.  
  106.