Implement till 5.18

main
Felix Martin 2021-03-30 19:49:09 -04:00
parent e22a52b7f3
commit bb870cbdf1
3 changed files with 69 additions and 7 deletions

View File

@ -34,7 +34,6 @@
((factorial-machine 'stack) 'initialize) ((factorial-machine 'stack) 'initialize)
(set-register-contents! factorial-machine 'n n) (set-register-contents! factorial-machine 'n n)
(start factorial-machine) (start factorial-machine)
; (assert (get-register-contents factorial-machine 'val) 720)
((factorial-machine 'stack) 'print-statistics) ((factorial-machine 'stack) 'print-statistics)
(newline)) (newline))
@ -44,6 +43,35 @@
; total-pushes(n) = (n - 1) * 2 ; total-pushes(n) = (n - 1) * 2
; max-depth: (n - 1) * 2 ; max-depth: (n - 1) * 2
(display "\nex-5.15 - instruction-counting\n") (display "\nex-5.15 - instruction-count\n")
(set-register-contents! factorial-machine 'n 1)
(factorial-machine 'reset-instruction-acount)
(start factorial-machine)
(assert (factorial-machine 'instruction-count) 5)
(display "\nex-5.16 - instruction-tracing\n")
(display "[ok]\n")
(display "\nex-5.17 - instruction-tracing-labels\n")
(factorial-machine 'trace-on)
(start factorial-machine)
(factorial-machine 'trace-off)
(start factorial-machine)
(display "\nex-5.18 - register-tracing\n")
((factorial-machine 'trace-on-reg) 'val)
(set-register-contents! factorial-machine 'n 5)
(start factorial-machine)
(assert (get-register-contents factorial-machine 'val) 120)
((factorial-machine 'trace-off-reg) 'val)
(set-register-contents! factorial-machine 'n 5)
(start factorial-machine)
(display "\nex-5.19 - breakpoint\n")
(display "\nex-5.16\n")

4
ex-5_20-xx.scm Normal file
View File

@ -0,0 +1,4 @@
(load "util.scm")
(load "misc/sicp-regsim.scm")
(display "\nex-5.20\n")

View File

@ -24,12 +24,21 @@
machine)) machine))
(define (make-register name) (define (make-register name)
(let ((contents '*unassigned*)) (let ((contents '*unassigned*)
(trace #f))
(define (set value)
(if trace
(begin
(display "set! ") (display name)
(display " from ") (display contents)
(display " to ") (display value) (newline)))
(set! contents value))
(define (dispatch message) (define (dispatch message)
(cond ((eq? message 'get) contents) (cond ((eq? message 'get) contents)
((eq? message 'set) ((eq? message 'set) set)
(lambda (value) (set! contents value)))
((eq? message 'name?) name) ((eq? message 'name?) name)
((eq? message 'trace-on) (set! trace #t))
((eq? message 'trace-off) (set! trace #f))
(else (else
(error "Unknown request -- REGISTER" message)))) (error "Unknown request -- REGISTER" message))))
dispatch)) dispatch))
@ -110,6 +119,8 @@
(let ((pc (make-register 'pc)) (let ((pc (make-register 'pc))
(flag (make-register 'flag)) (flag (make-register 'flag))
(stack (make-stack)) (stack (make-stack))
(instruction-count 0)
(instruction-trace #f)
(the-instruction-sequence '())) (the-instruction-sequence '()))
(let ((the-ops (let ((the-ops
(list (list 'initialize-stack (list (list 'initialize-stack
@ -138,6 +149,12 @@
'done 'done
(begin (begin
((instruction-execution-proc (car insts))) ((instruction-execution-proc (car insts)))
(if instruction-trace
(begin
(display (instruction-text (car insts)))
(newline)))
(if (not (eq? 'label (car (instruction-text (car insts)))))
(set! instruction-count (+ instruction-count 1)))
(execute))))) (execute)))))
(define (dispatch message) (define (dispatch message)
(cond ((eq? message 'start) (cond ((eq? message 'start)
@ -145,6 +162,15 @@
(execute)) (execute))
((eq? message 'install-instruction-sequence) ((eq? message 'install-instruction-sequence)
(lambda (seq) (set! the-instruction-sequence seq))) (lambda (seq) (set! the-instruction-sequence seq)))
((eq? message 'instruction-count) instruction-count)
((eq? message 'reset-instruction-acount)
(set! instruction-count 0) 'ok)
((eq? message 'trace-on) (set! instruction-trace #t) 'ok)
((eq? message 'trace-off) (set! instruction-trace #f) 'ok)
((eq? message 'trace-on-reg)
(lambda (reg-name) ((lookup-register reg-name) 'trace-on)))
((eq? message 'trace-off-reg)
(lambda (reg-name) ((lookup-register reg-name) 'trace-off)))
((eq? message 'allocate-register) allocate-register) ((eq? message 'allocate-register) allocate-register)
((eq? message 'get-register) lookup-register) ((eq? message 'get-register) lookup-register)
((eq? message 'install-operations) ((eq? message 'install-operations)
@ -181,7 +207,7 @@
(lambda (insts labels) (lambda (insts labels)
(let ((next-inst (car text))) (let ((next-inst (car text)))
(if (symbol? next-inst) (if (symbol? next-inst)
(receive insts (receive (cons (make-instruction (list 'label next-inst)) insts)
(cons (make-label-entry next-inst (cons (make-label-entry next-inst
insts) insts)
labels)) labels))
@ -239,6 +265,8 @@
(make-save inst machine stack pc)) (make-save inst machine stack pc))
((eq? (car inst) 'restore) ((eq? (car inst) 'restore)
(make-restore inst machine stack pc)) (make-restore inst machine stack pc))
((eq? (car inst) 'label)
(make-label pc))
((eq? (car inst) 'perform) ((eq? (car inst) 'perform)
(make-perform inst machine labels ops pc)) (make-perform inst machine labels ops pc))
((eq? (car inst) 'inc) ((eq? (car inst) 'inc)
@ -246,6 +274,8 @@
(else (error "Unknown instruction type -- ASSEMBLE" (else (error "Unknown instruction type -- ASSEMBLE"
inst)))) inst))))
(define (make-label pc)
(lambda () (advance-pc pc)))
(define (make-assign inst machine labels operations pc) (define (make-assign inst machine labels operations pc)
(let ((target (let ((target