I am going through SICP, and am on Ch 5 -- implementing the explicit control evaluator.
They start out by writing the machine language for eval-dispatch
eval-dispatch
(test (op self-evaluating?) (reg exp))
(branch (label ev-self-eval))
(test (op variable?) (reg exp))
(branch (label ev-variable))
(test (op quoted?) (reg exp))
(branch (label ev-quoted))
(test (op assignment?) (reg exp))
(branch (label ev-assignment))
(test (op definition?) (reg exp))
(branch (label ev-definition))
(test (op if?) (reg exp))
(branch (label ev-if))
(test (op lambda?) (reg exp))
(branch (label ev-lambda))
(test (op begin?) (reg exp))
(branch (label ev-begin))
(test (op application?) (reg exp))
(branch (label ev-application))
(goto (label unknown-expression-type))
But, they note here that this is also possible to do in a data-directed style:
In our controller, the dispatch is written as a sequence of test and branch instructions. Alternatively, it could have been written in a data-directed style (and in a real system it probably would have been) to avoid the need to perform sequential tests and to facilitate the definition of new expression types. A machine designed to run Lisp would probably include a dispatch-on-type instruction that would efficiently execute such data-directed dispatches.
I am not quite sure I understand what they are implying here:
- What do they mean by
instruction
here? Do I understand correctly that it would be anop
Something like:
(assign type (op dispatch-on-type) (reg exp))
(goto type)
I am not quite sure why writing dispatch-on-type
this way would necessarily make it any easier to add new expression types
- They note that the lisp machine would implement
dispatch-on-type
efficiently
I am not quite sure I understand that. Is there any way one can make it more efficient than what eval-dispatch
is already doing? I would guess dispatch-on-type
would have to run the tests to determine what type it is
Okay, one difference in efficiency that I noticed:
in this version, the
branch
instruction is still checked by the machine. We could have avoided those checks if we used the data-directed approach