1 (define (linearize-and-cleanup cfg)
3 (define bbs-vector (cfg->vector cfg))
8 (set! todo (cons bb todo)))
13 (set! rev-code (cons instr rev-code)))
16 (emit (list 'movlw val)))
18 (emit (list 'movwf adr)))
20 (emit (list 'movfw adr)))
21 (define (movff src dst)
22 (emit (list 'movff src dst)))
25 (emit (list 'clrf adr)))
27 (emit (list 'setf adr)))
30 (emit (list 'incf adr)))
32 (emit (list 'decf adr)))
35 (emit (list 'addwf adr)))
37 (emit (list 'addwfc adr)))
40 (emit (list 'subwf adr)))
42 (emit (list 'subwfb adr)))
45 (emit (list 'mullw adr)))
47 (emit (list 'mulwf adr)))
50 (emit (list 'andwf adr)))
52 (emit (list 'iorwf adr)))
54 (emit (list 'xorwf adr)))
57 (emit (list 'rlcf adr)))
59 (emit (list 'rrcf adr)))
62 (emit (list 'bcf adr bit)))
64 (emit (list 'bsf adr bit)))
66 (emit (list 'btg adr bit)))
69 (emit (list 'comf adr)))
72 (emit (list 'cpfseq adr)))
74 (emit (list 'cpfslt adr)))
76 (emit (list 'cpfsgt adr)))
79 (emit (list 'bra label)))
82 (emit (list 'rcall label)))
85 (if (and #f (and (not (null? rev-code))
86 (eq? (caar rev-code) 'rcall)))
87 (let ((label (cadar rev-code)))
88 (set! rev-code (cdr rev-code))
90 (emit (list 'return))))
93 (if (and #f (and (not (null? rev-code))
94 (eq? (caar rev-code) 'bra)
95 (eq? (cadar rev-code) lab)))
97 (set! rev-code (cdr rev-code))
99 (emit (list 'label lab))))
102 (emit (list 'sleep)))
104 (define (move-reg src dst)
113 ;; takes 2 cycles (as much as movfw src ; movwf dst), but takes
114 ;; only 1 instruction
117 (define (bb-linearize bb)
118 (let ((label-num (bb-label-num bb)))
119 (let ((bb (vector-ref bbs-vector label-num)))
121 (define (move-lit n adr)
130 (define (dump-instr instr)
131 (cond ((call-instr? instr)
132 (let* ((def-proc (call-instr-def-proc instr))
133 (entry (def-procedure-entry def-proc)))
137 (let ((label (bb-label entry)))
140 ((return-instr? instr)
143 (let ((src1 (instr-src1 instr))
144 (src2 (instr-src2 instr))
145 (dst (instr-dst instr)))
146 (if (and (or (not (byte-cell? dst))
148 (or (not (byte-cell? src1))
149 (byte-cell-adr src1))
150 (or (not (byte-cell? src2))
151 (byte-cell-adr src2)))
153 (case (instr-id instr)
157 (let ((n (byte-lit-val src1))
158 (z (byte-cell-adr dst)))
160 (let ((x (byte-cell-adr src1))
161 (z (byte-cell-adr dst)))
166 (let ((n (byte-lit-val src2))
167 (z (byte-cell-adr dst)))
169 (move-lit (byte-lit-val src1) z)
170 (move-reg (byte-cell-adr src1) z))
171 (case (instr-id instr)
172 ((add) (cond ((= n 1) (incf z))
173 ((= n #xff) (decf z))
176 ((addc) (movlw n) (addwfc z))
177 ((sub) (cond ((= n 1) (decf z))
178 ((= n #xff) (incf z))
181 ((subb) (movlw n) (subwfb z))))
182 (let ((x (byte-cell-adr src1))
183 (y (byte-cell-adr src2))
184 (z (byte-cell-adr dst)))
185 (cond ((and (not (= x y))
187 (memq (instr-id instr)
189 ;; since this basically swaps the
190 ;; arguments, it can't be used for
195 ;; for subtraction, preserves argument
198 ;; this NEEDS to be done with movff, or
199 ;; else wreg will get clobbered and this
202 (else ;; TODO check if it could be merged with the previous case
205 (case (instr-id instr)
210 (else (error "..."))))))
212 ((mul) ; 8 by 8 multiplication
214 ;; since multiplication is commutative, the
215 ;; arguments are set up so the second one will
216 ;; be a literal if the operator is applied on a
217 ;; literal and a variable
218 (let ((n (byte-lit-val src2)))
220 (movlw (byte-lit-val src1))
221 (move-reg (byte-cell-adr src1) WREG))
222 ;; literal multiplication
224 (let ((x (byte-cell-adr src1))
225 (y (byte-cell-adr src2)))
230 ;; no instructions for bitwise operations involving
231 ;; literals exist on the PIC18
232 (let ((x (if (byte-lit? src1)
234 (byte-cell-adr src1)))
235 (y (if (byte-lit? src2)
237 (byte-cell-adr src2)))
238 (z (byte-cell-adr dst)))
239 (cond ((byte-lit? src1)
244 ((and (not (= x y)) (= y z))
249 (case (instr-id instr)
253 (else (error "...")))))
256 (let ((x (if (byte-lit? src1)
258 (byte-cell-adr src1)))
259 (z (byte-cell-adr dst)))
260 (cond ((byte-lit? src1) (move-lit x z))
261 ((not (= x z)) (move-reg x z)))
262 (case (instr-id instr)
268 (if (not (byte-lit? src2))
269 (error "bit offset must be a literal"))
270 (let ((x (byte-cell-adr src1))
271 (y (byte-lit-val src2)))
272 (case (instr-id instr)
275 ((toggle) (btg x y)))))
278 (let ((z (byte-cell-adr dst)))
280 (move-lit (byte-lit-val src1) z)
281 (move-reg (byte-cell-adr src1) z))
285 (if (null? (bb-succs bb))
286 (error "I think you might have given me an empty source file."))
287 (let* ((succs (bb-succs bb))
289 (bra (bb-label dest))
292 (let* ((succs (bb-succs bb))
293 (dest-true (car succs))
294 (dest-false (cadr succs)))
296 (define (compare flip adr)
297 (case (instr-id instr)
298 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
299 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
301 (bra (bb-label dest-false))
302 (bra (bb-label dest-true))
303 (add-todo dest-false)
304 (add-todo dest-true))
306 (cond ((byte-lit? src1)
307 (let ((n (byte-lit-val src1))
308 (y (byte-cell-adr src2)))
309 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
310 (eq? (instr-id instr) 'x==y))
311 (special-compare-eq-lit n x)
316 (let ((x (byte-cell-adr src1))
317 (n (byte-lit-val src2)))
318 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
319 (eq? (instr-id instr) 'x==y))
320 (special-compare-eq-lit n x)
325 (let ((x (byte-cell-adr src1))
326 (y (byte-cell-adr src2)))
331 (emit (list (instr-id instr))))))))))
335 (vector-set! bbs-vector label-num #f)
336 (label (bb-label bb))
337 (for-each dump-instr (reverse (bb-rev-instrs bb)))
338 (for-each add-todo (bb-succs bb)))))))
340 (let ((prog-label (asm-make-label 'PROG)))
345 (add-todo (vector-ref bbs-vector 0))
350 (let ((bb (car todo)))
351 (set! todo (cdr todo))
356 (define (assembler-gen filename cfg)
361 (movlw (cadr instr)))
363 (movwf (cadr instr)))
365 (movf (cadr instr) 'w))
367 (movff (cadr instr) (caddr instr)))
377 (addwf (cadr instr)))
379 (addwfc (cadr instr)))
381 (subwf (cadr instr)))
383 (subwfb (cadr instr)))
385 (mullw (cadr instr)))
387 (mulwf (cadr instr)))
389 (andwf (cadr instr)))
391 (iorwf (cadr instr)))
393 (xorwf (cadr instr)))
399 (bcf (cadr instr) (caddr instr)))
401 (bsf (cadr instr) (caddr instr)))
403 (btg (cadr instr) (caddr instr)))
407 (cpfseq (cadr instr)))
409 (cpfslt (cadr instr)))
411 (cpfsgt (cadr instr)))
415 (rcall (cadr instr)))
420 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
421 (asm-label (cadr instr)))
425 (error "unknown instruction" instr))))
431 (let ((code (linearize-and-cleanup cfg)))
432 ; (pretty-print code)
433 (for-each gen code)))