SICP/ex-5_07-xx.scm

142 lines
4.2 KiB
Scheme

(load "util.scm")
(load "misc/sicp-regsim.scm")
(display "\nex-5.7 - test-machines\n")
; We have already tested the machines in the previous exercises.
(display "[answered]\n")
; continue at 5.2.2 - the assembler
(display "\nex-5.8 - double-here\n")
(define (extract-labels text receive)
(define (warn-if-label-exists label labels)
(if (assoc label labels)
(begin
(display "Duplicated labels -- EXTRACT-LABELS ")
(display label)
(newline))))
(if (null? text)
(receive '() '())
(extract-labels (cdr text)
(lambda (insts labels)
(let ((next-inst (car text)))
(if (symbol? next-inst)
(warn-if-label-exists next-inst labels))
(if (symbol? next-inst)
(receive insts
(cons (make-label-entry next-inst
insts)
labels))
(receive (cons (make-instruction next-inst)
insts)
labels)))))))
(define double-here-machine
(make-machine
'(a)
'()
'(start
(goto (label here))
here
(assign a (const 3))
(goto (label there))
here
(assign a (const 4))
(goto (label there))
there)))
(start double-here-machine)
(assert (get-register-contents double-here-machine 'a) 3)
; The register contains 3 because the assembler jumps to the first label in the
; list.
(display "\nex-5.9 - strict-op\n")
(display "[done]\n")
(define (make-operation-exp exp machine labels operations)
(let ((op (lookup-prim (operation-exp-op exp) operations))
(aprocs
(map (lambda (e)
(if (or (register-exp? e) (constant-exp? e))
(make-primitive-exp e machine labels)
(error "Invalid operation argument -- MAKE-OPERATION-EXP" e)))
(operation-exp-operands exp))))
(lambda ()
(apply op (map (lambda (p) (p)) aprocs)))))
; The following instruction creates an error when analyzing the arguments for
; op. Previously it created the error during runtime.
; (define invalid-op-arg-machine
; (make-machine
; '(a)
; (list (list '+ +))
; '((assign a (op +) (label here) (const 1))
; here)))
(display "\nex-5.10 - inc\n")
(define inc-reg-name cadr)
(define (make-inc inst machine labels operations pc)
(let ((target (get-register machine (inc-reg-name inst))))
(lambda ()
(set-contents!
target
(+ (get-contents target) 1))
(advance-pc pc))))
(define triple-inc-machine
(make-machine
'(a)
()
'((inc a)
(inc a)
(inc a))))
(set-register-contents! triple-inc-machine 'a 8)
(start triple-inc-machine)
(assert (get-register-contents triple-inc-machine 'a) 11)
(display "\nex-5.11\n")
;Exercise 5.11. When we introduced save and restore in section 5.1.4, we
;didn't specify what would happen if you tried to restore a register that was
;not the last one saved, as in the sequence
;(save y)
;(save x)
;(restore y)
;There are several reasonable possibilities for the meaning of restore:
;a. (restore y) puts into y the last value saved on the stack, regardless of
;what register that value came from. This is the way our simulator behaves.
;Show how to take advantage of this behavior to eliminate one instruction from
;the Fibonacci machine of section 5.1.4 (figure 5.12).
;b. (restore y) puts into y the last value saved on the stack, but only if
;that value was saved from y; otherwise, it signals an error. Modify the
;simulator to behave this way. You will have to change save to put the register
;name on the stack along with the value.
;c. (restore y) puts into y the last value saved from y regardless of what
;other registers were saved after y and not restored. Modify the simulator to
;behave this way. You will have to associate a separate stack with each
;register. You should make the initialize-stack operation initialize all the
;register stacks.
; Extend the message-passing interface to the machine to provide access to
; this new information. To test your analyzer, define the Fibonacci machine
; from figure 5.12 and examine the lists you constructed.
(display "\nex-5.12\n")
; (display "\nex-5.13\n")