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 (set-add! (byte-cell-interferes-with x) y)
14 (set-add! (byte-cell-interferes-with y) x))))
16 (define (interfere-pairwise live)
17 (set! all-live (union all-live live))
18 (table-for-each (lambda (x dummy) ;; TODO do it in utilities ?
19 (table-for-each (lambda (y dummy)
20 (if (not (eq? x y)) (interfere x y)))
24 (define (instr-interference-graph instr)
25 (let ((dst (instr-dst instr)))
27 (let ((src1 (instr-src1 instr))
28 (src2 (instr-src2 instr)))
31 (set-add! (byte-cell-coalesceable-with dst) src1)
32 (set-add! (byte-cell-coalesceable-with src1) dst)))
35 (set-add! (byte-cell-coalesceable-with dst) src2)
36 (set-add! (byte-cell-coalesceable-with src2) dst))))))
37 (let ((live-before (instr-live-before instr)))
38 (interfere-pairwise live-before)))
40 (define (bb-interference-graph bb)
41 (for-each instr-interference-graph (bb-rev-instrs bb)))
43 (analyze-liveness cfg)
45 (for-each bb-interference-graph (cfg-bbs cfg))
49 (define (allocate-registers cfg)
50 (let ((all-live (interference-graph cfg)))
52 (define (color byte-cell)
53 (let ((coalesce-candidates ; TODO right now, no coalescing is done
54 (set-filter byte-cell-adr
55 (diff (byte-cell-coalesceable-with byte-cell)
56 (byte-cell-interferes-with byte-cell)))))
58 (pp (list byte-cell: byte-cell;;;;;;;;;;;;;;;
60 ; interferes-with: (byte-cell-interferes-with byte-cell)
61 ; coalesceable-with: (byte-cell-coalesceable-with byte-cell)
64 (if #f #;(not (null? coalesce-candidates))
65 (let ((adr (byte-cell-adr (car (set->list coalesce-candidates))))) ;; TODO have as a set all along
66 (byte-cell-adr-set! byte-cell adr))
67 (let ((neighbours (byte-cell-interferes-with byte-cell)))
69 (if (and memory-divide ; the user wants his own zone
70 (>= adr memory-divide)) ; and we'd use it
71 (error "register allocation would cross the memory divide") ;; TODO fallback ?
72 (let loop2 ((lst (set->list neighbours))) ;; TODO keep using sets, but not urgent, it's not a bottleneck
74 (byte-cell-adr-set! byte-cell adr)
76 (if (= adr (byte-cell-adr x))
78 (loop2 (cdr lst))))))))))))
80 (define (delete byte-cell1 neighbours)
81 (table-for-each (lambda (byte-cell2 dummy)
82 (set-remove! (byte-cell-interferes-with byte-cell2)
86 (define (undelete byte-cell1 neighbours)
87 (table-for-each (lambda (byte-cell2 val)
88 (set-add! (byte-cell-interferes-with byte-cell2)
92 (define (find-min-neighbours graph)
93 (let loop ((lst graph) (m #f) (byte-cell #f))
97 (n (table-length (byte-cell-interferes-with x))))
98 (if (or (not m) (< n m))
100 (loop (cdr lst) m byte-cell))))))
102 (define (alloc-reg graph)
103 (if (not (null? graph))
104 (let* ((byte-cell (find-min-neighbours graph))
105 (neighbours (byte-cell-interferes-with byte-cell)))
106 (let ((new-graph (remove byte-cell graph)))
107 (delete byte-cell neighbours)
108 (alloc-reg new-graph)
109 (undelete byte-cell neighbours))
110 (if (not (byte-cell-adr byte-cell))
111 (color byte-cell)))))
113 (alloc-reg (set->list all-live)))) ;; TODO convert find-min-neighbors andalloc-reg to use tables, not urgent since it's not a bottleneck
116 (define (linearize-and-cleanup cfg)
118 (define bbs-vector (cfg->vector cfg))
122 (define (add-todo bb)
123 (set! todo (cons bb todo)))
125 (define rev-code '())
128 (set! rev-code (cons instr rev-code)))
131 (emit (list 'movlw val)))
133 (emit (list 'movwf adr)))
135 (emit (list 'movfw adr)))
136 (define (movff src dst)
137 (emit (list 'movff src dst)))
140 (emit (list 'clrf adr)))
142 (emit (list 'setf adr)))
145 (emit (list 'incf adr)))
147 (emit (list 'decf adr)))
150 (emit (list 'addwf adr)))
152 (emit (list 'addwfc adr)))
155 (emit (list 'subwf adr)))
157 (emit (list 'subwfb adr)))
160 (emit (list 'mullw adr)))
162 (emit (list 'mulwf adr)))
165 (emit (list 'andwf adr)))
167 (emit (list 'iorwf adr)))
169 (emit (list 'xorwf adr)))
172 (emit (list 'rlcf adr)))
174 (emit (list 'rrcf adr)))
176 (define (bcf adr bit)
177 (emit (list 'bcf adr bit)))
178 (define (bsf adr bit)
179 (emit (list 'bsf adr bit)))
180 (define (btg adr bit)
181 (emit (list 'btg adr bit)))
184 (emit (list 'comf adr)))
187 (emit (list 'cpfseq adr)))
189 (emit (list 'cpfslt adr)))
191 (emit (list 'cpfsgt adr)))
194 (emit (list 'bra label)))
196 (define (rcall label)
197 (emit (list 'rcall label)))
200 (if (and #f (and (not (null? rev-code))
201 (eq? (caar rev-code) 'rcall)))
202 (let ((label (cadar rev-code)))
203 (set! rev-code (cdr rev-code))
205 (emit (list 'return))))
208 (if (and #f (and (not (null? rev-code))
209 (eq? (caar rev-code) 'bra)
210 (eq? (cadar rev-code) lab)))
212 (set! rev-code (cdr rev-code))
214 (emit (list 'label lab))))
217 (emit (list 'sleep)))
219 (define (move-reg src dst)
228 ;; takes 2 cycles (as much as movfw src ; movwf dst), but takes
229 ;; only 1 instruction
232 (define (bb-linearize bb)
233 (let ((label-num (bb-label-num bb)))
234 (let ((bb (vector-ref bbs-vector label-num)))
236 (define (move-lit n adr)
245 (define (dump-instr instr)
246 (cond ((call-instr? instr)
247 (let* ((def-proc (call-instr-def-proc instr))
248 (entry (def-procedure-entry def-proc)))
252 (let ((label (bb-label entry)))
255 ((return-instr? instr)
258 (let ((src1 (instr-src1 instr))
259 (src2 (instr-src2 instr))
260 (dst (instr-dst instr)))
261 (if (and (or (not (byte-cell? dst))
263 (or (not (byte-cell? src1))
264 (byte-cell-adr src1))
265 (or (not (byte-cell? src2))
266 (byte-cell-adr src2)))
268 (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)))
281 (let ((n (byte-lit-val src2))
282 (z (byte-cell-adr dst)))
284 (move-lit (byte-lit-val src1) z)
285 (move-reg (byte-cell-adr src1) z))
286 (case (instr-id instr)
287 ((add) (cond ((= n 1) (incf z))
288 ((= n #xff) (decf z))
291 ((addc) (movlw n) (addwfc z))
292 ((sub) (cond ((= n 1) (decf z))
293 ((= n #xff) (incf z))
296 ((subb) (movlw n) (subwfb z))))
297 (let ((x (byte-cell-adr src1))
298 (y (byte-cell-adr src2))
299 (z (byte-cell-adr dst)))
300 (cond ((and (not (= x y))
302 (memq (instr-id instr)
304 ;; since this basically swaps the
305 ;; arguments, it can't be used for
310 ;; for subtraction, preserves argument
313 ;; this NEEDS to be done with movff, or
314 ;; else wreg will get clobbered and this
317 (else ;; TODO check if it could be merged with the previous case
320 (case (instr-id instr)
325 (else (error "..."))))))
327 ((mul) ; 8 by 8 multiplication
329 ;; since multiplication is commutative, the
330 ;; arguments are set up so the second one will
331 ;; be a literal if the operator is applied on a
332 ;; literal and a variable
333 (let ((n (byte-lit-val src2)))
335 (movlw (byte-lit-val src1))
336 (move-reg (byte-cell-adr src1) WREG))
337 ;; literal multiplication
339 (let ((x (byte-cell-adr src1))
340 (y (byte-cell-adr src2)))
345 ;; no instructions for bitwise operations involving
346 ;; literals exist on the PIC18
347 (let ((x (if (byte-lit? src1)
349 (byte-cell-adr src1)))
350 (y (if (byte-lit? src2)
352 (byte-cell-adr src2)))
353 (z (byte-cell-adr dst)))
354 (cond ((byte-lit? src1)
359 ((and (not (= x y)) (= y z))
364 (case (instr-id instr)
368 (else (error "...")))))
371 (let ((x (if (byte-lit? src1)
373 (byte-cell-adr src1)))
374 (z (byte-cell-adr dst)))
375 (cond ((byte-lit? src1) (move-lit x z))
376 ((not (= x z)) (move-reg x z)))
377 (case (instr-id instr)
383 (if (not (byte-lit? src2))
384 (error "bit offset must be a literal"))
385 (let ((x (byte-cell-adr src1))
386 (y (byte-lit-val src2)))
387 (case (instr-id instr)
390 ((toggle) (btg x y)))))
393 (let ((z (byte-cell-adr dst)))
395 (move-lit (byte-lit-val src1) z)
396 (move-reg (byte-cell-adr src1) z))
400 (if (null? (bb-succs bb))
401 (error "I think you might have given me an empty source file."))
402 (let* ((succs (bb-succs bb))
404 (bra (bb-label dest))
407 (let* ((succs (bb-succs bb))
408 (dest-true (car succs))
409 (dest-false (cadr succs)))
411 (define (compare flip adr)
412 (case (instr-id instr)
413 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
414 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
416 (bra (bb-label dest-false))
417 (bra (bb-label dest-true))
418 (add-todo dest-false)
419 (add-todo dest-true))
421 (cond ((byte-lit? src1)
422 (let ((n (byte-lit-val src1))
423 (y (byte-cell-adr src2)))
424 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
425 (eq? (instr-id instr) 'x==y))
426 (special-compare-eq-lit n x)
431 (let ((x (byte-cell-adr src1))
432 (n (byte-lit-val src2)))
433 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
434 (eq? (instr-id instr) 'x==y))
435 (special-compare-eq-lit n x)
440 (let ((x (byte-cell-adr src1))
441 (y (byte-cell-adr src2)))
446 (emit (list (instr-id instr))))))))))
450 (vector-set! bbs-vector label-num #f)
451 (label (bb-label bb))
452 (for-each dump-instr (reverse (bb-rev-instrs bb)))
453 (for-each add-todo (bb-succs bb)))))))
455 (let ((prog-label (asm-make-label 'PROG)))
460 (add-todo (vector-ref bbs-vector 0))
465 (let ((bb (car todo)))
466 (set! todo (cdr todo))
471 (define (assembler-gen filename cfg)
476 (movlw (cadr instr)))
478 (movwf (cadr instr)))
480 (movf (cadr instr) 'w))
482 (movff (cadr instr) (caddr instr)))
492 (addwf (cadr instr)))
494 (addwfc (cadr instr)))
496 (subwf (cadr instr)))
498 (subwfb (cadr instr)))
500 (mullw (cadr instr)))
502 (mulwf (cadr instr)))
504 (andwf (cadr instr)))
506 (iorwf (cadr instr)))
508 (xorwf (cadr instr)))
514 (bcf (cadr instr) (caddr instr)))
516 (bsf (cadr instr) (caddr instr)))
518 (btg (cadr instr) (caddr instr)))
522 (cpfseq (cadr instr)))
524 (cpfslt (cadr instr)))
526 (cpfsgt (cadr instr)))
530 (rcall (cadr instr)))
535 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
536 (asm-label (cadr instr)))
540 (error "unknown instruction" instr))))
546 (let ((code (linearize-and-cleanup cfg)))
547 ; (pretty-print code)
548 (for-each gen code)))
550 (define (code-gen filename cfg)
551 (pp register-allocation:)
552 (allocate-registers cfg)
553 (pp code-generation:)
554 (assembler-gen filename cfg))