SICP 読み (275) 5.3 記憶の割り当てとごみ集め

問題 5.21 の b.を検討。

(define (count-leaves tree)
  (define (count-iter tree n)
    (cond ((null? tree) n)
          ((not (pair? tree)) (+ n 1))
          (else (count-iter (cdr tree)
                            (count-iter (car tree) n)))))
  (count-iter tree 0))

これをレジスタ計算機に手翻訳せよ、との事。昨晩の足がかりがあるのでちょい楽。そんなに時間がかかりませんでしたが、だからこその微妙さが残るか。(何
まず、以下のようなソレがでっち上がる。

(controller
 (assign n (const 0))
 (assign cont (label done))
 loop
 (test (op null?) (reg tree))
 (branch (label null))
 (test (op not-pair?) (reg tree))
 (branch (label leaf))
 (save cont)
 (save tree)
 (assign cont (label right))
 (assign tree (op car) (reg tree))
 (goto (label loop))
 right
 (restore tree)
 (assign tree (cdr tree))
 (assign cont (label left))
 (goto (label loop))
 left
;; restore tree ??
 (restore cont)
 (goto (reg cont))
 leaf
 (assign n (op +) (reg n) (const 1))
 (goto (reg cont))
 null
 (goto (reg cont))
 done
 (assign val (reg n)))

left なラベルの処理に微妙なコメントが付いていますが、この処理は不要。一応ちょっとした机上検証はパスしてるんですが ...
上記にちょっとだけ手を入れてみたのが以下です。

(controller
 (assign n (const 0))
 (assign cont (label done))
 loop
 (test (op null?) (reg tree))
 (goto (reg cont))
 (test (op not-pair?) (reg tree))
 (branch (label leaf))
 (save cont)
 (save tree)
 (assign cont (label right))
 (assign tree (op car) (reg tree))
 (goto (label loop))
 right
 (restore tree)
 (assign tree (cdr tree))
 (assign cont (label left))
 (goto (label loop))
 left
 (restore cont)
 (goto (reg cont))
 leaf
 (assign n (op +) (reg n) (const 1))
 (goto (reg cont))
 done
 (assign val (reg n)))

できればもう少しチェックしてみたいんですが、机上ッスか ...