4 (define (make-machine reg-names ops controller)
5 (let ((machine (make-new-machine)))
8 ((machine 'allocate-register) name))
10 ((machine 'install-operations) ops)
11 ((machine 'install-instruction-sequence)
12 (assemble controller machine))
15 (define (make-register name)
16 (let ((contents '*unassigned*))
17 (define (dispatch message)
18 (cond ((eq? message 'get) contents)
20 (lambda (val) (set! contents val)))
21 (else (error "Unknown request -- REGISTER"
25 (define (set-contents! reg val)
27 (define (get-contents reg)
36 (error "Empty stack -- POP")
43 (define (dispatch message)
44 (cond ((eq? message 'push) push)
45 ((eq? message 'pop) (pop))
46 ((eq? message 'initialize) (initialize))
47 (else (error "Unknown request -- STACK"
50 (define (push stack value)
51 ((stack 'push) value))
54 (define (make-new-machine)
55 (let* ((pc (make-register 'pc))
56 (flag (make-register 'flag))
58 (the-instruction-sequence '())
63 (list (list 'initialize-stack
65 (stack 'initialize))))))
66 (define (lookup-register name)
67 (let ((val (assoc name register-table)))
70 (error "Unknown register -- LOOKUP" name))))
71 (define (allocate-register name)
72 (let ((val (assoc name register-table)))
74 (error "Multiply defined register: " name)
76 (cons (list name (make-register name))
80 (let ((insts (get-contents pc)))
83 (begin ((instruction-execution-proc (car insts)))
85 (define (dispatch message)
86 (cond ((eq? message 'start)
87 (set-contents! pc the-instruction-sequence)
89 ((eq? message 'lookup-register) lookup-register)
90 ((eq? message 'allocate-register) allocate-register)
91 ((eq? message 'stack) stack)
92 ((eq? message 'install-operations)
94 (set! the-ops (append the-ops ops))))
95 ((eq? message 'operations) the-ops)
96 ((eq? message 'install-instruction-sequence)
98 (set! the-instruction-sequence seq)))))
100 (define (make-instruction text)
102 (define (instruction-text inst)
104 (define (instruction-execution-proc inst)
106 (define (set-instruction-execution-proc! inst proc)
107 (set-cdr! inst proc))
109 (define (get-register machine name)
110 ((machine 'lookup-register) name))
111 (define (get-register-contents machine name)
112 (get-contents (get-register machine name)))
113 (define (set-register-contents! machine name value)
114 (set-contents! (get-register machine name) value))
115 (define (start machine)
118 (define (assemble controller machine)
121 (lambda (insts labels)
122 (update-insts! insts labels machine)
125 ;; (define (extract-labels text receive)
128 ;; (let* ((result (extract-labels (cdr text)))
129 ;; (insts (car result))
130 ;; (labels (cdr result))
131 ;; (next-inst (car text)))
132 ;; (if (symbol? next-inst)
134 ;; (cons (make-label-entry next-inst insts)
136 ;; (cons (cons (make-instruction next-inst)
139 ;; (define (assemble controller machine)
140 ;; (let* ((result (extract-labels controller))
141 ;; (insts (car result))
142 ;; (labels (cdr result)))
143 ;; (update-insts! insts labels machine)
146 (define (extract-labels text receive)
151 (lambda (insts labels)
152 (let ((next-inst (car text)))
153 (if (symbol? next-inst)
156 (cons (make-label-entry next-inst insts)
159 (cons (make-instruction next-inst)
163 (define (update-insts! insts labels machine)
164 (let ((pc (get-register machine 'pc))
165 (flag (get-register machine 'flag))
166 (stack (machine 'stack))
167 (ops (machine 'operations)))
170 (set-instruction-execution-proc!
172 (make-execution-procedure
173 (instruction-text inst) labels machine
177 (define (make-label-entry label-name insts)
178 (cons label-name insts))
179 (define (make-execution-procedure text labels machine