[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Computed goto's



    Date: 30 Mar 87 16:57:26 EST
    From: primerd!DOUG@ENX.Prime.PDN


    Thinking about it there are times when one would like to do a computed goto.
    I notice that (go tag) in tagbody is explicitly not calculated.  For 
    maximum effect the computation should result directly in a dispatched
    goto.  One can get the effect of this by using lambda's in an array and
    doing an array lookup but this is somewhat more expensive (?).

    Any suggestions about this?  

    Doug (doug@enx.prime.com)

One could hope that a CASE statement in which all the test objects were
small fixnums would be compiled into a jump table.  I just checked, and
Symbolics doesn't do this.

I don't think an array of compiled function objects should be
significantly more expensive than what you want, though.  The only added
expense I can think of is that you have to push a stack frame to
transfer and you have to indirect through the lexical environment to
reference variables that would have been local.

I think it would be pretty easy to write a macro that does this.  In
fact, I think this does a simple subcase:

(defmacro on-goto (key &body clauses)		;remember BASIC?
  (let ((array (map '(array function)
		    #'(lambda (clause)
			(compile nil `(lambda () . ,clause)))
		    clauses)))
    `(funcall (aref ,array ,key))))

Well, it's not as easy as I thought -- the above macro doesn't do the
right thing with lexical variables in the clauses.  But I'm pretty sure
it could be done....
					barmar