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 (analyze-liveness cfg)
10 (define (instr-analyze-liveness instr live-after)
15 (let ((def-proc (call-instr-def-proc instr)))
16 (if (and (not (set-empty? live-after))
19 (def-procedure-live-after-calls def-proc)
24 (set-union! (def-procedure-live-after-calls def-proc)
29 (map (lambda (def-var)
31 (value-bytes (def-variable-value def-var))))
32 (def-procedure-params def-proc)))
36 (value-bytes (def-procedure-value def-proc)))))))
37 (if (bb? (def-procedure-entry def-proc))
38 (set-intersection ;; TODO disabling this branch saves around 12 bytes
39 (bb-live-before (def-procedure-entry def-proc))
43 ((return-instr? instr)
44 (let ((def-proc (return-instr-def-proc instr)))
46 (if (def-procedure? def-proc)
47 (def-procedure-live-after-calls def-proc)
48 (list->set (value-bytes def-proc)))))
49 (set! live-after live)
53 (let* ((src1 (instr-src1 instr))
54 (src2 (instr-src2 instr))
55 (dst (instr-dst instr))
56 (use (if (byte-cell? src1)
58 (set-add (new-set src1) src2)
63 (def (if (byte-cell? dst)
67 ;; (and (byte-cell? dst) ; dead instruction?
68 ;; (not (set-member? live-after dst))
69 ;; (not (and (byte-cell? dst) (byte-cell-adr dst))))
72 (set-diff live-after def))))))))
74 (instr-live-before-set! instr live-before)
75 (instr-live-after-set! instr live-after)
77 (define (bb-analyze-liveness bb)
78 (let loop ((rev-instrs (bb-rev-instrs bb))
79 (live-after (set-union-multi
80 (map bb-live-before (bb-succs bb)))))
81 (if (null? rev-instrs)
82 (if (not (set-equal? live-after (bb-live-before bb)))
83 (begin (set! changed? #t)
84 (bb-live-before-set! bb live-after)))
85 (let* ((instr (car rev-instrs))
86 (live-before (instr-analyze-liveness instr live-after)))
87 (loop (cdr rev-instrs)
91 (begin (set! changed? #f)
92 (for-each bb-analyze-liveness (cfg-bbs cfg))
95 ;;-----------------------------------------------------------------------------
97 (define all-live (new-empty-set))
99 (define (bb-interference-graph bb)
101 (define (interfere x y)
102 (if (not (set-member? (byte-cell-interferes-with y) x)) ;; TODO is this check worth it ?
103 (begin (set-add! (byte-cell-interferes-with x) y)
104 (set-add! (byte-cell-interferes-with y) x))))
106 (define (interfere-pairwise live)
107 (set-union! all-live live)
108 (set-for-each (lambda (x)
109 (set-for-each (lambda (y)
110 (if (not (eq? x y)) (interfere x y)))
114 (define (instr-interference-graph instr)
115 (let ((dst (instr-dst instr)))
117 (let ((src1 (instr-src1 instr))
118 (src2 (instr-src2 instr)))
119 (if (and (byte-cell? src1) (not (eq? dst src1)))
120 (begin (set-add! (byte-cell-coalesceable-with dst) src1)
121 (set-add! (byte-cell-coalesceable-with src1) dst)))
122 (if (and (byte-cell? src2) (not (eq? dst src2)))
123 (begin (set-add! (byte-cell-coalesceable-with dst) src2)
124 (set-add! (byte-cell-coalesceable-with src2) dst)))
125 (interfere-pairwise (set-add (instr-live-after instr) dst)))))
126 (interfere-pairwise (instr-live-before instr)))
127 (for-each instr-interference-graph (bb-rev-instrs bb)))
129 (pp analyse-liveness:)
130 (time (analyze-liveness cfg))
132 (pp interference-graph:)
133 (time (for-each bb-interference-graph (cfg-bbs cfg)))
137 ;;-----------------------------------------------------------------------------
139 (define (delete byte-cell1 neighbours)
140 (set-for-each (lambda (byte-cell2)
141 (set-remove! (byte-cell-interferes-with byte-cell2)
144 (define (undelete byte-cell1 neighbours)
145 (set-for-each (lambda (byte-cell2)
146 (set-add! (byte-cell-interferes-with byte-cell2)
150 (define (coalesce graph)
154 (let* ((coalesceable-with (byte-cell-coalesceable-with byte-cell))
155 (old-neighbours (byte-cell-interferes-with byte-cell))
156 (coalesce-candidates (set-diff coalesceable-with
158 (if (or (byte-cell-adr byte-cell) ; in a special register
159 (set-empty? coalesce-candidates))
161 ;; coalesce byte-cell with another cell
162 (let loop ((l (set->list coalesce-candidates)))
164 #t ; can't coalesce, keep
166 ;; don't coalesce with a special register
167 (if (byte-cell-adr c)
169 (let ((c-neighbours (byte-cell-interferes-with c))
170 (c-coalesced (byte-cell-coalesced-with c)))
171 ;; remove all references to byte-cell and replace
172 ;; them with references to c
173 (set-union! c-neighbours old-neighbours)
174 (undelete c old-neighbours)
175 (delete byte-cell old-neighbours)
176 (set-union! c-coalesced
177 (byte-cell-coalesced-with byte-cell))
180 (let ((s (byte-cell-coalesceable-with cell)))
181 (set-remove! s byte-cell)))
183 (byte-cell-coalesceable-with-set! byte-cell
185 (byte-cell-interferes-with-set! byte-cell
187 (set-add! c-coalesced byte-cell)
192 ;;-----------------------------------------------------------------------------
194 (define register-table (make-table))
195 (define reverse-register-table (make-table))
196 (define (allocate-registers cfg)
197 (let ((all-live (coalesce (set->list (interference-graph cfg))))
198 (max-adr 0)) ; to know how much ram we need
200 (define (color byte-cell)
201 (define (set-register-table cell adr)
202 (if #f (not (string=? (byte-cell-name cell) "__tmp"))
203 (let ((adr (if (and (> adr #x5F) (< adr #xF60)) ; not in bank 0 ;; TODO have a function for that
206 (table-set! register-table
208 (cons (cons (byte-cell-bb cell)
209 (byte-cell-name cell))
210 (table-ref register-table adr '())))
211 (table-set! reverse-register-table
212 (byte-cell-name cell) ;; TODO add the bb ?
214 (let ((neighbours (byte-cell-interferes-with byte-cell)))
216 (if (and memory-divide ; the user wants his own zone
217 (>= adr memory-divide)) ; and we'd use it
218 (error "register allocation would cross the memory divide") ;; TODO fallback ?
219 (let loop2 ((lst (set->list neighbours))) ;; TODO keep using sets, but not urgent, it's not a bottleneck
221 (begin (byte-cell-adr-set! byte-cell adr)
222 (set-register-table byte-cell adr)
225 (byte-cell-adr-set! cell adr)
226 (set-register-table cell adr))
227 (byte-cell-coalesced-with byte-cell)))
228 (if (= adr (byte-cell-adr (car lst)))
230 (loop2 (cdr lst))))))
231 (set! max-adr (max max-adr adr)))))
233 (define (find-min-neighbours graph)
234 (let loop ((lst graph) (m #f) (byte-cell #f))
238 (n (set-length (byte-cell-interferes-with x))))
239 (if (or (not m) (< n m))
241 (loop (cdr lst) m byte-cell))))))
243 (define (alloc-reg graph)
244 (if (not (null? graph))
245 (let* ((byte-cell (find-min-neighbours graph))
246 (neighbours (byte-cell-interferes-with byte-cell)))
247 (let ((new-graph (remove byte-cell graph)))
248 (delete byte-cell neighbours)
249 (alloc-reg new-graph)
250 (undelete byte-cell neighbours))
251 (if (not (byte-cell-adr byte-cell))
252 (color byte-cell)))))
254 (pp register-allocation:)
255 (time (alloc-reg all-live)) ;; TODO convert find-min-neighbours and alloc-reg to use tables, not urgent since it's not a bottleneck
256 (display (string-append (number->string (+ max-adr 1)) " RAM bytes\n"))))