1

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:

  1. What do they mean by instruction here? Do I understand correctly that it would be an op

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

  1. 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

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Stepan Parunashvili
  • 2,627
  • 5
  • 30
  • 51
  • "instruction" presumably, judging from the code as quoted in the question, this is something returned by a call to `label`. this is how it is used right now. so now the sequential tests are performed to find out what kind of expression is being evaluated. presumably in a data-driven implementation each expression would be represented as a tagged list, say, with its type denoted by the tag (a quoted symbol, an int, enum, whatever); that tag is looked up in the table and the jump address is retrieved from there. – Will Ness Oct 03 '20 at 13:29

1 Answers1

1

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

Stepan Parunashvili
  • 2,627
  • 5
  • 30
  • 51
  • 2
    "to avoid the need to perform sequential tests" suggests a table where the direct jump instruction addresses are stored, so the table in consulted, and the jump is directly performed to the appropriate instruction, instead of performing the sequential tests as in the snippet you quoted. – Will Ness Oct 03 '20 at 13:22