1 ;; address after which memory is allocated by the user, therefore not used for
3 ;; in programs, located in the SIXPIC_MEMORY_DIVIDE variable
4 (define memory-divide #f)
6 (define (interference-graph cfg)
10 (define (interfere x y)
11 (if (not (memq x (byte-cell-interferes-with y)))
13 (byte-cell-interferes-with-set!
15 (cons y (byte-cell-interferes-with x)))
16 (byte-cell-interferes-with-set!
18 (cons x (byte-cell-interferes-with y))))))
20 (define (interfere-pairwise live)
21 (set! all-live (union all-live live))
29 (define (instr-interference-graph instr)
30 (let ((dst (instr-dst instr)))
32 (let ((src1 (instr-src1 instr))
33 (src2 (instr-src2 instr)))
36 (byte-cell-coalesceable-with-set!
38 (union (byte-cell-coalesceable-with dst)
40 (byte-cell-coalesceable-with-set!
42 (union (byte-cell-coalesceable-with src1)
46 (byte-cell-coalesceable-with-set!
48 (union (byte-cell-coalesceable-with dst)
50 (byte-cell-coalesceable-with-set!
52 (union (byte-cell-coalesceable-with src2)
54 (let ((live-before (instr-live-before instr)))
55 (interfere-pairwise live-before)))
57 (define (bb-interference-graph bb)
58 (for-each instr-interference-graph (bb-rev-instrs bb)))
60 (analyze-liveness cfg)
62 (for-each bb-interference-graph (cfg-bbs cfg))
66 (define (allocate-registers cfg)
67 (let ((all-live (interference-graph cfg)))
69 (define (color byte-cell)
70 (let ((coalesce-candidates ; TODO right now, no coalescing is done
72 (diff (byte-cell-coalesceable-with byte-cell)
73 (byte-cell-interferes-with byte-cell)))))
75 (pp (list byte-cell: byte-cell;;;;;;;;;;;;;;;
77 ; interferes-with: (byte-cell-interferes-with byte-cell)
78 ; coalesceable-with: (byte-cell-coalesceable-with byte-cell)
81 (if #f #;(not (null? coalesce-candidates))
82 (let ((adr (byte-cell-adr (car coalesce-candidates))))
83 (byte-cell-adr-set! byte-cell adr))
84 (let ((neighbours (byte-cell-interferes-with byte-cell)))
86 (if (and memory-divide ; the user wants his own zone
87 (>= adr memory-divide)) ; and we'd use it
88 (error "register allocation would cross the memory divide") ;; TODO fallback ?
89 (let loop2 ((lst neighbours))
91 (byte-cell-adr-set! byte-cell adr)
93 (if (= adr (byte-cell-adr x))
95 (loop2 (cdr lst))))))))))))
97 (define (delete byte-cell1 neighbours)
98 (for-each (lambda (byte-cell2)
99 (let ((lst (byte-cell-interferes-with byte-cell2)))
100 (byte-cell-interferes-with-set!
102 (remove byte-cell1 lst))))
105 (define (undelete byte-cell1 neighbours)
106 (for-each (lambda (byte-cell2)
107 (let ((lst (byte-cell-interferes-with byte-cell2)))
108 (byte-cell-interferes-with-set!
110 (cons byte-cell1 lst))))
113 (define (find-min-neighbours graph)
114 (let loop ((lst graph) (m #f) (byte-cell #f))
118 (n (length (byte-cell-interferes-with x))))
119 (if (or (not m) (< n m))
121 (loop (cdr lst) m byte-cell))))))
123 (define (alloc-reg graph)
124 (if (not (null? graph))
125 (let* ((byte-cell (find-min-neighbours graph))
126 (neighbours (byte-cell-interferes-with byte-cell)))
127 (let ((new-graph (remove byte-cell graph)))
128 (delete byte-cell neighbours)
129 (alloc-reg new-graph)
130 (undelete byte-cell neighbours))
131 (if (not (byte-cell-adr byte-cell))
132 (color byte-cell)))))
134 (alloc-reg all-live)))
137 (define (linearize-and-cleanup cfg)
139 (define bbs-vector (cfg->vector cfg))
143 (define (add-todo bb)
144 (set! todo (cons bb todo)))
146 (define rev-code '())
149 (set! rev-code (cons instr rev-code)))
152 (emit (list 'movlw val)))
155 (emit (list 'movwf adr)))
158 (emit (list 'movfw adr)))
160 (define (movff src dst)
161 (emit (list 'movff src dst)))
164 (emit (list 'clrf adr)))
167 (emit (list 'setf adr)))
170 (emit (list 'incf adr)))
173 (emit (list 'decf adr)))
176 (emit (list 'addwf adr)))
179 (emit (list 'addwfc adr)))
182 (emit (list 'subwf adr)))
185 (emit (list 'subwfb adr)))
188 (emit (list 'cpfseq adr)))
191 (emit (list 'cpfslt adr)))
194 (emit (list 'cpfsgt adr)))
197 (emit (list 'bra label)))
199 (define (rcall label)
200 (emit (list 'rcall label)))
203 (if (and #f (and (not (null? rev-code)) ; TODO probably here for eventual inlining
204 (eq? (caar rev-code) 'rcall)))
205 (let ((label (cadar rev-code)))
206 (set! rev-code (cdr rev-code))
208 (emit (list 'return))))
211 (if (and #f (and (not (null? rev-code)) ; TODO would probably be useful to eliminate things like : bra $2, $2:
212 (eq? (caar rev-code) 'bra)
213 (eq? (cadar rev-code) lab)))
215 (set! rev-code (cdr rev-code))
217 (emit (list 'label lab))))
220 (emit (list 'sleep)))
222 (define (move-reg src dst)
231 ;(movff src dst) ; takes 2 cycles (as much as movfw src ; movwf dst), but takes only 1 instruction TODO not implemented in the simulator
234 (define (bb-linearize bb)
235 (let ((label-num (bb-label-num bb)))
236 (let ((bb (vector-ref bbs-vector label-num)))
238 (define (move-lit n adr)
247 (define (dump-instr instr)
248 (cond ((call-instr? instr)
249 (let* ((def-proc (call-instr-def-proc instr))
250 (entry (def-procedure-entry def-proc)))
254 (let ((label (bb-label entry)))
257 ((return-instr? instr)
260 (let ((src1 (instr-src1 instr))
261 (src2 (instr-src2 instr))
262 (dst (instr-dst instr)))
263 (if (and (or (not (byte-cell? dst))
265 (or (not (byte-cell? src1))
266 (byte-cell-adr src1))
267 (or (not (byte-cell? src2))
268 (byte-cell-adr src2)))
269 (case (instr-id instr)
272 (let ((n (byte-lit-val src1))
273 (z (byte-cell-adr dst)))
275 (let ((x (byte-cell-adr src1))
276 (z (byte-cell-adr dst)))
280 (let ((n (byte-lit-val src2))
281 (z (byte-cell-adr dst)))
283 (move-lit (byte-lit-val src1) z)
284 (move-reg (byte-cell-adr src1) z))
285 (case (instr-id instr)
308 (let ((x (byte-cell-adr src1))
309 (y (byte-cell-adr src2))
310 (z (byte-cell-adr dst)))
311 (cond ((and (not (= x y)) (= y z))
313 (case (instr-id instr)
322 (else (error "..."))))
326 (case (instr-id instr)
335 (else (error "..."))))))))
337 (let* ((succs (bb-succs bb))
339 (bra (bb-label dest))
342 (let* ((succs (bb-succs bb))
343 (dest-true (car succs))
344 (dest-false (cadr succs)))
346 (define (compare flip adr)
347 (case (instr-id instr)
348 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
349 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
351 (bra (bb-label dest-false))
352 (bra (bb-label dest-true))
353 (add-todo dest-false)
354 (add-todo dest-true))
356 (cond ((byte-lit? src1)
357 (let ((n (byte-lit-val src1))
358 (y (byte-cell-adr src2)))
359 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
360 (eq? (instr-id instr) 'x==y))
361 (special-compare-eq-lit n x)
366 (let ((x (byte-cell-adr src1))
367 (n (byte-lit-val src2)))
368 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
369 (eq? (instr-id instr) 'x==y))
370 (special-compare-eq-lit n x) ;; TODO does not exist. the only way apart from cpfseq I see would be to load w, do a subtraction, then conditional branch, but would be larger and would take 1-2 cycles more
375 (let ((x (byte-cell-adr src1))
376 (y (byte-cell-adr src2)))
381 (emit (list (instr-id instr))))))))))
385 (vector-set! bbs-vector label-num #f)
386 (label (bb-label bb))
387 (for-each dump-instr (reverse (bb-rev-instrs bb)))
388 (for-each add-todo (bb-succs bb)))))))
390 (let ((prog-label (asm-make-label 'PROG)))
395 (add-todo (vector-ref bbs-vector 0))
400 (let ((bb (car todo)))
401 (set! todo (cdr todo))
406 (define (assembler-gen filename cfg)
411 (movlw (cadr instr)))
413 (movwf (cadr instr)))
415 (movf (cadr instr) 'w))
417 (movff (cadr instr) (caddr instr)))
427 (addwf (cadr instr)))
429 (addwfc (cadr instr)))
431 (subwf (cadr instr)))
433 (subwfb (cadr instr)))
435 (cpfseq (cadr instr)))
437 (cpfslt (cadr instr)))
439 (cpfsgt (cadr instr)))
443 (rcall (cadr instr)))
448 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
449 (asm-label (cadr instr)))
453 (error "unknown instruction" instr))))
459 (let ((code (linearize-and-cleanup cfg)))
460 ; (pretty-print code)
465 '(display "------------------ GENERATED CODE\n")
467 (asm-display-listing (current-output-port))
469 (asm-write-hex-file (string-append filename ".hex")) ;; TODO move to main ?
473 (define (code-gen filename cfg)
474 (allocate-registers cfg)
475 (assembler-gen filename cfg)
477 ; (pretty-print (reverse (bb-rev-instrs bb))) ;; TODO what ? there are no bbs here...