1 (define (linearize-and-cleanup cfg)
3 (define bbs-vector (cfg->vector cfg))
8 (set! todo (cons bb todo)))
13 (set! rev-code (cons instr rev-code)))
16 (emit (list 'movlw val)))
19 (emit (list 'movwf adr)))
22 (emit (list 'movfw adr)))
25 (emit (list 'clrf adr)))
28 (emit (list 'setf adr)))
31 (emit (list 'incf adr)))
34 (emit (list 'decf adr)))
37 (emit (list 'addwf adr)))
40 (emit (list 'addwfc adr)))
43 (emit (list 'subwf adr)))
46 (emit (list 'subwfb adr)))
49 (emit (list 'cpfseq adr)))
52 (emit (list 'cpfslt adr)))
55 (emit (list 'cpfsgt adr)))
58 (emit (list 'bra label)))
61 (emit (list 'rcall label)))
64 (if (and #f (and (not (null? rev-code))
65 (eq? (caar rev-code) 'rcall))
67 (let ((label (cadar rev-code)))
68 (set! rev-code (cdr rev-code))
70 (emit (list 'return))))
73 (if (and #f (and (not (null? rev-code))
74 (eq? (caar rev-code) 'bra)
75 (eq? (cadar rev-code) lab))
78 (set! rev-code (cdr rev-code))
80 (emit (list 'label lab))))
85 (define (move-reg src dst)
95 (define (bb-linearize bb)
96 (let ((label-num (bb-label-num bb)))
97 (let ((bb (vector-ref bbs-vector label-num)))
99 (define (move-lit n adr)
108 (define (dump-instr instr)
109 (cond ((call-instr? instr)
110 (let* ((def-proc (call-instr-def-proc instr))
111 (entry (def-procedure-entry def-proc)))
115 (let ((label (bb-label entry)))
118 ((return-instr? instr)
121 (let ((src1 (instr-src1 instr))
122 (src2 (instr-src2 instr))
123 (dst (instr-dst instr)))
124 (if (and (or (not (byte-cell? dst))
126 (or (not (byte-cell? src1))
127 (byte-cell-adr src1))
128 (or (not (byte-cell? src2))
129 (byte-cell-adr src2)))
130 (case (instr-id instr)
133 (let ((n (byte-lit-val src1))
134 (z (byte-cell-adr dst)))
136 (let ((x (byte-cell-adr src1))
137 (z (byte-cell-adr dst)))
141 (let ((n (byte-lit-val src2))
142 (z (byte-cell-adr dst)))
144 (move-lit (byte-lit-val src1) z)
145 (move-reg (byte-cell-adr src1) z))
146 (case (instr-id instr)
169 (let ((x (byte-cell-adr src1))
170 (y (byte-cell-adr src2))
171 (z (byte-cell-adr dst)))
172 (cond ((and (not (= x y)) (= y z))
174 (case (instr-id instr)
183 (else (error "..."))))
187 (case (instr-id instr)
196 (else (error "..."))))))))
198 (let* ((succs (bb-succs bb))
200 (bra (bb-label dest))
203 (let* ((succs (bb-succs bb))
204 (dest-true (car succs))
205 (dest-false (cadr succs)))
207 (define (compare flip adr)
208 (case (instr-id instr)
209 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
210 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
212 (bra (bb-label dest-false))
213 (bra (bb-label dest-true))
214 (add-todo dest-false)
215 (add-todo dest-true))
217 (cond ((byte-lit? src1)
218 (let ((n (byte-lit-val src1))
219 (y (byte-cell-adr src2)))
220 (if (and (or (= n 0) (= n 1) (= n #xff))
221 (eq? (instr-id instr) 'x==y))
222 (special-compare-eq-lit n x)
227 (let ((x (byte-cell-adr src1))
228 (n (byte-lit-val src2)))
229 (if (and (or (= n 0) (= n 1) (= n #xff))
230 (eq? (instr-id instr) 'x==y))
231 (special-compare-eq-lit n x)
236 (let ((x (byte-cell-adr src1))
237 (y (byte-cell-adr src2)))
242 (emit (list (instr-id instr))))))))))
246 (vector-set! bbs-vector label-num #f)
247 (label (bb-label bb))
248 (for-each dump-instr (reverse (bb-rev-instrs bb)))
250 (for-each add-todo (bb-succs bb)))))))
252 (let ((prog-label (asm-make-label 'PROG)))
257 (add-todo (vector-ref bbs-vector 0))
262 (let ((bb (car todo)))
263 (set! todo (cdr todo))
268 (define (assembler-gen filename cfg)
273 (movlw (cadr instr)))
275 (movwf (cadr instr)))
277 (movf (cadr instr) 'w))
287 (addwf (cadr instr)))
289 (addwfc (cadr instr)))
291 (subwf (cadr instr)))
293 (subwfb (cadr instr)))
295 (cpfseq (cadr instr)))
297 (cpfslt (cadr instr)))
299 (cpfsgt (cadr instr)))
303 (rcall (cadr instr)))
308 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
309 (asm-label (cadr instr)))
313 ' (error "unknown instruction" instr))))
319 (let ((code (linearize-and-cleanup cfg)))
320 ; (pretty-print code)
325 '(display "------------------ GENERATED CODE\n")
327 '(asm-display-listing (current-output-port)) ;; TODO debug
329 (asm-write-hex-file (string-append filename ".hex"))
331 '(display "------------------ EXECUTION USING SIMULATOR\n")
335 '(execute-hex-file (string-append filename ".hex"))) ;; TODO debug
337 (define (code-gen filename cfg)
338 (allocate-registers cfg)
339 (assembler-gen filename cfg)
341 ; (pretty-print (reverse (bb-rev-instrs bb)))