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)
8 (define all-live (new-empty-set))
10 (define (interfere x y)
11 (if (not (set-member? (byte-cell-interferes-with y) x))
13 (byte-cell-interferes-with-set!
15 (union (new-set y) (byte-cell-interferes-with x)))
16 (byte-cell-interferes-with-set!
18 (union (new-set x) (byte-cell-interferes-with y))))))
20 (define (interfere-pairwise live)
21 (set! all-live (union all-live live))
22 (table-for-each (lambda (x val1)
23 (table-for-each (lambda (y val2)
24 (if (and val1 val2 (not (eq? x y)))
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
71 (set-filter byte-cell-adr
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 (set->list coalesce-candidates))))) ;; TODO have as a set all along
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 (set->list neighbours))) ;; TODO FOO cop out...
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 (table-for-each (lambda (byte-cell2 val)
100 (let ((lst (byte-cell-interferes-with byte-cell2)))
101 (byte-cell-interferes-with-set!
103 (diff lst (new-set byte-cell1))))))
106 (define (undelete byte-cell1 neighbours)
107 (table-for-each (lambda (byte-cell2 val)
109 (let ((lst (byte-cell-interferes-with byte-cell2)))
110 (byte-cell-interferes-with-set!
112 (union (new-set byte-cell1) lst)))))
115 (define (find-min-neighbours graph)
116 (let loop ((lst graph) (m #f) (byte-cell #f))
120 (n (table-length (byte-cell-interferes-with x))))
121 (if (or (not m) (< n m))
123 (loop (cdr lst) m byte-cell))))))
125 (define (alloc-reg graph)
126 (if (not (null? graph))
127 (let* ((byte-cell (find-min-neighbours graph))
128 (neighbours (byte-cell-interferes-with byte-cell)))
129 (let ((new-graph (remove byte-cell graph)))
130 (delete byte-cell neighbours)
131 (alloc-reg new-graph)
132 (undelete byte-cell neighbours))
133 (if (not (byte-cell-adr byte-cell))
134 (color byte-cell)))))
136 (alloc-reg (set->list all-live)))) ;; TODO FOO BAD cop out, convert find-min-neighbors et alloc-reg to used tables
139 (define (linearize-and-cleanup cfg)
141 (define bbs-vector (cfg->vector cfg))
145 (define (add-todo bb)
146 (set! todo (cons bb todo)))
148 (define rev-code '())
151 (set! rev-code (cons instr rev-code)))
154 (emit (list 'movlw val)))
156 (emit (list 'movwf adr)))
158 (emit (list 'movfw adr)))
159 (define (movff src dst)
160 (emit (list 'movff src dst)))
163 (emit (list 'clrf adr)))
165 (emit (list 'setf adr)))
168 (emit (list 'incf adr)))
170 (emit (list 'decf adr)))
173 (emit (list 'addwf adr)))
175 (emit (list 'addwfc adr)))
178 (emit (list 'subwf adr)))
180 (emit (list 'subwfb adr)))
183 (emit (list 'mullw adr)))
185 (emit (list 'mulwf adr)))
188 (emit (list 'andwf adr)))
190 (emit (list 'iorwf adr)))
192 (emit (list 'xorwf adr)))
195 (emit (list 'rlcf adr)))
197 (emit (list 'rrcf adr)))
199 (define (bcf adr bit)
200 (emit (list 'bcf adr bit)))
201 (define (bsf adr bit)
202 (emit (list 'bsf adr bit)))
203 (define (btg adr bit)
204 (emit (list 'btg adr bit)))
207 (emit (list 'comf adr)))
210 (emit (list 'cpfseq adr)))
212 (emit (list 'cpfslt adr)))
214 (emit (list 'cpfsgt adr)))
217 (emit (list 'bra label)))
219 (define (rcall label)
220 (emit (list 'rcall label)))
223 (if (and #f (and (not (null? rev-code))
224 (eq? (caar rev-code) 'rcall)))
225 (let ((label (cadar rev-code)))
226 (set! rev-code (cdr rev-code))
228 (emit (list 'return))))
231 (if (and #f (and (not (null? rev-code))
232 (eq? (caar rev-code) 'bra)
233 (eq? (cadar rev-code) lab)))
235 (set! rev-code (cdr rev-code))
237 (emit (list 'label lab))))
240 (emit (list 'sleep)))
242 (define (move-reg src dst)
251 ;; takes 2 cycles (as much as movfw src ; movwf dst), but takes
252 ;; only 1 instruction
255 (define (bb-linearize bb)
256 (let ((label-num (bb-label-num bb)))
257 (let ((bb (vector-ref bbs-vector label-num)))
259 (define (move-lit n adr)
268 (define (dump-instr instr)
269 (cond ((call-instr? instr)
270 (let* ((def-proc (call-instr-def-proc instr))
271 (entry (def-procedure-entry def-proc)))
275 (let ((label (bb-label entry)))
278 ((return-instr? instr)
281 (let ((src1 (instr-src1 instr))
282 (src2 (instr-src2 instr))
283 (dst (instr-dst instr)))
284 (if (and (or (not (byte-cell? dst))
286 (or (not (byte-cell? src1))
287 (byte-cell-adr src1))
288 (or (not (byte-cell? src2))
289 (byte-cell-adr src2)))
291 (case (instr-id instr)
295 (let ((n (byte-lit-val src1))
296 (z (byte-cell-adr dst)))
298 (let ((x (byte-cell-adr src1))
299 (z (byte-cell-adr dst)))
304 (let ((n (byte-lit-val src2))
305 (z (byte-cell-adr dst)))
307 (move-lit (byte-lit-val src1) z)
308 (move-reg (byte-cell-adr src1) z))
309 (case (instr-id instr)
310 ((add) (cond ((= n 1) (incf z))
311 ((= n #xff) (decf z))
314 ((addc) (movlw n) (addwfc z))
315 ((sub) (cond ((= n 1) (decf z))
316 ((= n #xff) (incf z))
319 ((subb) (movlw n) (subwfb z))))
320 (let ((x (byte-cell-adr src1))
321 (y (byte-cell-adr src2))
322 (z (byte-cell-adr dst)))
323 (cond ((and (not (= x y))
325 (memq (instr-id instr)
327 ;; since this basically swaps the
328 ;; arguments, it can't be used for
333 ;; for subtraction, preserves argument
336 ;; this NEEDS to be done with movff, or
337 ;; else wreg will get clobbered and this
340 (else ;; TODO check if it could be merged with the previous case
343 (case (instr-id instr)
348 (else (error "..."))))))
350 ((mul) ; 8 by 8 multiplication
352 ;; since multiplication is commutative, the
353 ;; arguments are set up so the second one will
354 ;; be a literal if the operator is applied on a
355 ;; literal and a variable
356 (let ((n (byte-lit-val src2)))
358 (movlw (byte-lit-val src1))
359 (move-reg (byte-cell-adr src1) WREG))
360 ;; literal multiplication
362 (let ((x (byte-cell-adr src1))
363 (y (byte-cell-adr src2)))
368 ;; no instructions for bitwise operations involving
369 ;; literals exist on the PIC18
370 (let ((x (if (byte-lit? src1)
372 (byte-cell-adr src1)))
373 (y (if (byte-lit? src2)
375 (byte-cell-adr src2)))
376 (z (byte-cell-adr dst)))
377 (cond ((byte-lit? src1)
382 ((and (not (= x y)) (= y z))
387 (case (instr-id instr)
391 (else (error "...")))))
394 (let ((x (if (byte-lit? src1)
396 (byte-cell-adr src1)))
397 (z (byte-cell-adr dst)))
398 (cond ((byte-lit? src1) (move-lit x z))
399 ((not (= x z)) (move-reg x z)))
400 (case (instr-id instr)
406 (if (not (byte-lit? src2))
407 (error "bit offset must be a literal"))
408 (let ((x (byte-cell-adr src1))
409 (y (byte-lit-val src2)))
410 (case (instr-id instr)
413 ((toggle) (btg x y)))))
416 (let ((z (byte-cell-adr dst)))
418 (move-lit (byte-lit-val src1) z)
419 (move-reg (byte-cell-adr src1) z))
423 (if (null? (bb-succs bb))
424 (error "I think you might have given me an empty source file."))
425 (let* ((succs (bb-succs bb))
427 (bra (bb-label dest))
430 (let* ((succs (bb-succs bb))
431 (dest-true (car succs))
432 (dest-false (cadr succs)))
434 (define (compare flip adr)
435 (case (instr-id instr)
436 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
437 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
439 (bra (bb-label dest-false))
440 (bra (bb-label dest-true))
441 (add-todo dest-false)
442 (add-todo dest-true))
444 (cond ((byte-lit? src1)
445 (let ((n (byte-lit-val src1))
446 (y (byte-cell-adr src2)))
447 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
448 (eq? (instr-id instr) 'x==y))
449 (special-compare-eq-lit n x)
454 (let ((x (byte-cell-adr src1))
455 (n (byte-lit-val src2)))
456 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
457 (eq? (instr-id instr) 'x==y))
458 (special-compare-eq-lit n x)
463 (let ((x (byte-cell-adr src1))
464 (y (byte-cell-adr src2)))
469 (emit (list (instr-id instr))))))))))
473 (vector-set! bbs-vector label-num #f)
474 (label (bb-label bb))
475 (for-each dump-instr (reverse (bb-rev-instrs bb)))
476 (for-each add-todo (bb-succs bb)))))))
478 (let ((prog-label (asm-make-label 'PROG)))
483 (add-todo (vector-ref bbs-vector 0))
488 (let ((bb (car todo)))
489 (set! todo (cdr todo))
494 (define (assembler-gen filename cfg)
499 (movlw (cadr instr)))
501 (movwf (cadr instr)))
503 (movf (cadr instr) 'w))
505 (movff (cadr instr) (caddr instr)))
515 (addwf (cadr instr)))
517 (addwfc (cadr instr)))
519 (subwf (cadr instr)))
521 (subwfb (cadr instr)))
523 (mullw (cadr instr)))
525 (mulwf (cadr instr)))
527 (andwf (cadr instr)))
529 (iorwf (cadr instr)))
531 (xorwf (cadr instr)))
537 (bcf (cadr instr) (caddr instr)))
539 (bsf (cadr instr) (caddr instr)))
541 (btg (cadr instr) (caddr instr)))
545 (cpfseq (cadr instr)))
547 (cpfslt (cadr instr)))
549 (cpfsgt (cadr instr)))
553 (rcall (cadr instr)))
558 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
559 (asm-label (cadr instr)))
563 (error "unknown instruction" instr))))
569 (let ((code (linearize-and-cleanup cfg)))
570 ; (pretty-print code)
571 (for-each gen code)))
573 (define (code-gen filename cfg)
574 (pp register-allocation:)
575 (allocate-registers cfg)
576 (pp code-generation:)
577 (assembler-gen filename cfg))