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)))
154 (emit (list 'movwf adr)))
156 (emit (list 'movfw adr)))
157 (define (movff src dst)
158 (emit (list 'movff src dst)))
161 (emit (list 'clrf adr)))
163 (emit (list 'setf adr)))
166 (emit (list 'incf adr)))
168 (emit (list 'decf adr)))
171 (emit (list 'addwf adr)))
173 (emit (list 'addwfc adr)))
176 (emit (list 'subwf adr)))
178 (emit (list 'subwfb adr)))
181 (emit (list 'mullw adr)))
183 (emit (list 'mulwf adr)))
186 (emit (list 'andwf adr)))
188 (emit (list 'iorwf adr)))
190 (emit (list 'xorwf adr)))
193 (emit (list 'rlcf adr)))
195 (emit (list 'rrcf adr)))
197 (define (bcf adr bit)
198 (emit (list 'bcf adr bit)))
199 (define (bsf adr bit)
200 (emit (list 'bsf adr bit)))
201 (define (btg adr bit)
202 (emit (list 'btg adr bit)))
205 (emit (list 'comf adr)))
208 (emit (list 'cpfseq adr)))
210 (emit (list 'cpfslt adr)))
212 (emit (list 'cpfsgt adr)))
215 (emit (list 'bra label)))
217 (define (rcall label)
218 (emit (list 'rcall label)))
221 (if (and #f (and (not (null? rev-code))
222 (eq? (caar rev-code) 'rcall)))
223 (let ((label (cadar rev-code)))
224 (set! rev-code (cdr rev-code))
226 (emit (list 'return))))
229 (if (and #f (and (not (null? rev-code))
230 (eq? (caar rev-code) 'bra)
231 (eq? (cadar rev-code) lab)))
233 (set! rev-code (cdr rev-code))
235 (emit (list 'label lab))))
238 (emit (list 'sleep)))
240 (define (move-reg src dst)
249 ;; takes 2 cycles (as much as movfw src ; movwf dst), but takes
250 ;; only 1 instruction
253 (define (bb-linearize bb)
254 (let ((label-num (bb-label-num bb)))
255 (let ((bb (vector-ref bbs-vector label-num)))
257 (define (move-lit n adr)
266 (define (dump-instr instr)
267 (cond ((call-instr? instr)
268 (let* ((def-proc (call-instr-def-proc instr))
269 (entry (def-procedure-entry def-proc)))
273 (let ((label (bb-label entry)))
276 ((return-instr? instr)
279 (let ((src1 (instr-src1 instr))
280 (src2 (instr-src2 instr))
281 (dst (instr-dst instr)))
282 (if (and (or (not (byte-cell? dst))
284 (or (not (byte-cell? src1))
285 (byte-cell-adr src1))
286 (or (not (byte-cell? src2))
287 (byte-cell-adr src2)))
289 (case (instr-id instr)
293 (let ((n (byte-lit-val src1))
294 (z (byte-cell-adr dst)))
296 (let ((x (byte-cell-adr src1))
297 (z (byte-cell-adr dst)))
302 (let ((n (byte-lit-val src2))
303 (z (byte-cell-adr dst)))
305 (move-lit (byte-lit-val src1) z)
306 (move-reg (byte-cell-adr src1) z))
307 (case (instr-id instr)
308 ((add) (cond ((= n 1) (incf z))
309 ((= n #xff) (decf z))
312 ((addc) (movlw n) (addwfc z))
313 ((sub) (cond ((= n 1) (decf z))
314 ((= n #xff) (incf z))
317 ((subb) (movlw n) (subwfb z))))
318 (let ((x (byte-cell-adr src1))
319 (y (byte-cell-adr src2))
320 (z (byte-cell-adr dst)))
321 (cond ((and (not (= x y))
323 (memq (instr-id instr)
325 ;; since this basically swaps the
326 ;; arguments, it can't be used for
331 ;; for subtraction, preserves argument
334 ;; this NEEDS to be done with movff, or
335 ;; else wreg will get clobbered and this
338 (else ;; TODO check if it could be merged with the previous case
341 (case (instr-id instr)
346 (else (error "..."))))))
348 ((mul) ; 8 by 8 multiplication
350 ;; since multiplication is commutative, the
351 ;; arguments are set up so the second one will
352 ;; be a literal if the operator is applied on a
353 ;; literal and a variable
354 (let ((n (byte-lit-val src2)))
356 (movlw (byte-lit-val src1))
357 (move-reg (byte-cell-adr src1) WREG))
358 ;; literal multiplication
360 (let ((x (byte-cell-adr src1))
361 (y (byte-cell-adr src2)))
366 ;; no instructions for bitwise operations involving
367 ;; literals exist on the PIC18
368 (let ((x (if (byte-lit? src1)
370 (byte-cell-adr src1)))
371 (y (if (byte-lit? src2)
373 (byte-cell-adr src2)))
374 (z (byte-cell-adr dst)))
375 (cond ((byte-lit? src1)
380 ((and (not (= x y)) (= y z))
385 (case (instr-id instr)
389 (else (error "...")))))
392 (let ((x (if (byte-lit? src1)
394 (byte-cell-adr src1)))
395 (z (byte-cell-adr dst)))
396 (cond ((byte-lit? src1) (move-lit x z))
397 ((not (= x z)) (move-reg x z)))
398 (case (instr-id instr)
404 (if (not (byte-lit? src2))
405 (error "bit offset must be a literal"))
406 (let ((x (byte-cell-adr src1))
407 (y (byte-lit-val src2)))
408 (case (instr-id instr)
411 ((toggle) (btg x y)))))
414 (let ((z (byte-cell-adr dst)))
416 (move-lit (byte-lit-val src1) z)
417 (move-reg (byte-cell-adr src1) z))
421 (if (null? (bb-succs bb))
422 (error "I think you might have given me an empty source file."))
423 (let* ((succs (bb-succs bb))
425 (bra (bb-label dest))
428 (let* ((succs (bb-succs bb))
429 (dest-true (car succs))
430 (dest-false (cadr succs)))
432 (define (compare flip adr)
433 (case (instr-id instr)
434 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
435 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
437 (bra (bb-label dest-false))
438 (bra (bb-label dest-true))
439 (add-todo dest-false)
440 (add-todo dest-true))
442 (cond ((byte-lit? src1)
443 (let ((n (byte-lit-val src1))
444 (y (byte-cell-adr src2)))
445 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
446 (eq? (instr-id instr) 'x==y))
447 (special-compare-eq-lit n x)
452 (let ((x (byte-cell-adr src1))
453 (n (byte-lit-val src2)))
454 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
455 (eq? (instr-id instr) 'x==y))
456 (special-compare-eq-lit n x)
461 (let ((x (byte-cell-adr src1))
462 (y (byte-cell-adr src2)))
467 (emit (list (instr-id instr))))))))))
471 (vector-set! bbs-vector label-num #f)
472 (label (bb-label bb))
473 (for-each dump-instr (reverse (bb-rev-instrs bb)))
474 (for-each add-todo (bb-succs bb)))))))
476 (let ((prog-label (asm-make-label 'PROG)))
481 (add-todo (vector-ref bbs-vector 0))
486 (let ((bb (car todo)))
487 (set! todo (cdr todo))
492 (define (assembler-gen filename cfg)
497 (movlw (cadr instr)))
499 (movwf (cadr instr)))
501 (movf (cadr instr) 'w))
503 (movff (cadr instr) (caddr instr)))
513 (addwf (cadr instr)))
515 (addwfc (cadr instr)))
517 (subwf (cadr instr)))
519 (subwfb (cadr instr)))
521 (mullw (cadr instr)))
523 (mulwf (cadr instr)))
525 (andwf (cadr instr)))
527 (iorwf (cadr instr)))
529 (xorwf (cadr instr)))
535 (bcf (cadr instr) (caddr instr)))
537 (bsf (cadr instr) (caddr instr)))
539 (btg (cadr instr) (caddr instr)))
543 (cpfseq (cadr instr)))
545 (cpfslt (cadr instr)))
547 (cpfsgt (cadr instr)))
551 (rcall (cadr instr)))
556 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
557 (asm-label (cadr instr)))
561 (error "unknown instruction" instr))))
567 (let ((code (linearize-and-cleanup cfg)))
568 ; (pretty-print code)
569 (for-each gen code)))
571 (define (code-gen filename cfg)
572 (allocate-registers cfg)
573 (assembler-gen filename cfg))