sicp-ex-5.32



<< Previous exercise (5.31) | Index | Next exercise (5.33) >>


meteorgan

  
  
 (a) 
 ev-application 
   (save continue) 
   (assign unev (op operands) (reg exp)) 
   (assign exp (op operator) (reg exp)) 
   (test (op symbol?) (reg exp))                    ;; is the operator is symbol? 
   (branch (label ev-appl-operator-symbol)) 
   (save env) 
   (save unev) 
   (assign continue (label ev-appl-did-operator)) 
   (goto (label eval-dispatch)) 
 ev-appl-operator-symbol 
   (assign continue (label ev-appl-did-operator-no-restore)) 
   (goto (label eval-dispatch)) 
 ev-appl-did-operator 
   (restore unev)             
   (restore env) 
 ev-appl-did-operator-no-restore 
   (assign argl (op empty-arglist)) 
   (assign proc (reg val))     ; the operator 
   (test (op no-operands?) (reg unev)) 
   (branch (label apply-dispatch)) 
   (save proc) 
 (b) 
 It won't get all the advantage of compiler, because the interpreter need parse the code every time when it run. and  recognizing the special case will make the code more complicated. 

codybartfast




We can use variable? to identify the optimization and then go directly to 
ev-variable.

  ev-application
    (save continue)
    (assign unev (op operands) (reg exp))
    (assign exp (op operator) (reg exp))
    (test (op variable?) (reg exp))           ; check if a variable
    (branch (label ev-appl-operator-lookup))  ;   --> to lookup
    (save env)                                ; do need eval
    (save unev)                               ; so do need to save
    (assign continue (label ev-appl-did-operator-eval))
    (goto (label eval-dispatch))

  ev-appl-operator-lookup                     ; peform lookup
    (assign continue (label ev-appl-did-operator-lookup))
    (goto (label ev-variable))

  ev-appl-did-operator-eval                   ; return here if we eval'ed
    (restore unev)
    (restore env)
  ev-appl-did-operator-lookup                 ; return here if we looked up
    (assign argl (op empty-arglist))
    (assign proc (reg val))
    ...