1 (define bank-1-used? #f)
3 (define (linearize-and-cleanup cfg)
5 (define bbs-vector (cfg->vector cfg))
10 (set! todo (cons bb todo)))
15 (set! rev-code (cons instr rev-code)))
17 (define (outside-bank-0? adr)
18 (if (and (> adr #x5F) (< adr #xF60)) ; not a special register
19 (begin (set! bank-1-used? #t) #t)
21 (define (emit-byte-oriented op file #!optional (d? #t))
22 ;; we might have to access the second bank
23 (emit (if (outside-bank-0? file)
25 (list op (- file 96) 'f 'b)
26 (list op (- file 96) 'b))
30 (define (emit-bit-oriented op file bit)
31 (emit (if (outside-bank-0? file)
32 (list op (- file 96) bit 'b)
33 (list op file bit 'a))))
36 (emit (list 'movlw val)))
38 (emit-byte-oriented 'movwf adr #f))
40 (emit-byte-oriented 'movf adr)
41 (let ((i (car rev-code)))
42 (list-set! i 2 'w) ; destination is w, not f
44 (define (movff src dst)
45 (emit (list 'movff src dst)))
48 (emit-byte-oriented 'clrf adr #f))
50 (emit-byte-oriented 'setf adr #f))
53 (emit-byte-oriented 'incf adr))
55 (emit-byte-oriented 'decf adr))
58 (emit-byte-oriented 'addwf adr))
60 (emit-byte-oriented 'addwfc adr))
63 (emit-byte-oriented 'subwf adr))
65 (emit-byte-oriented 'subwfb adr))
68 (emit (list 'mullw adr)))
70 (emit-byte-oriented 'mulwf adr #f))
73 (emit-byte-oriented 'andwf adr))
75 (emit-byte-oriented 'iorwf adr))
77 (emit-byte-oriented 'xorwf adr))
80 (emit-byte-oriented 'rlcf adr))
82 (emit-byte-oriented 'rrcf adr))
85 (emit-bit-oriented 'bcf adr bit))
87 (emit-bit-oriented 'bsf adr bit))
89 (emit-bit-oriented 'btg adr bit))
92 (emit-byte-oriented 'comf adr))
94 (define (tblrd) ;; TODO support the different modes
98 (emit-byte-oriented 'cpfseq adr #f))
100 (emit-byte-oriented 'cpfslt adr #f))
102 (emit-byte-oriented 'cpfsgt adr #f))
105 (emit (list 'bra label)))
107 (define (rcall label)
108 (emit (list 'rcall label)))
111 (if (and #f (and (not (null? rev-code))
112 (eq? (caar rev-code) 'rcall)))
113 (let ((label (cadar rev-code)))
114 (set! rev-code (cdr rev-code))
116 (emit (list 'return))))
119 (if (and #f (and (not (null? rev-code))
120 (eq? (caar rev-code) 'bra)
121 (eq? (cadar rev-code) lab)))
123 (set! rev-code (cdr rev-code))
125 (emit (list 'label lab))))
128 (emit (list 'sleep)))
130 (define (move-reg src dst)
139 ;; takes 2 cycles (as much as movfw src ; movwf dst), but takes
140 ;; only 1 instruction
143 (define (bb-linearize bb)
144 (let ((label-num (bb-label-num bb)))
145 (let ((bb (vector-ref bbs-vector label-num)))
147 (define (move-lit n adr)
156 (define (dump-instr instr)
157 (cond ((call-instr? instr)
158 (let* ((def-proc (call-instr-def-proc instr))
159 (entry (def-procedure-entry def-proc)))
163 (let ((label (bb-label entry)))
166 ((return-instr? instr)
169 (let ((src1 (instr-src1 instr))
170 (src2 (instr-src2 instr))
171 (dst (instr-dst instr)))
172 (if (and (or (not (byte-cell? dst))
174 (or (not (byte-cell? src1))
175 (byte-cell-adr src1))
176 (or (not (byte-cell? src2))
177 (byte-cell-adr src2)))
179 (case (instr-id instr)
183 (let ((n (byte-lit-val src1))
184 (z (byte-cell-adr dst)))
186 (let ((x (byte-cell-adr src1))
187 (z (byte-cell-adr dst)))
192 (let ((n (byte-lit-val src2))
193 (z (byte-cell-adr dst)))
195 (move-lit (byte-lit-val src1) z)
196 (move-reg (byte-cell-adr src1) z))
197 (case (instr-id instr)
198 ((add) (cond ((= n 1) (incf z))
199 ((= n #xff) (decf z))
202 ((addc) (movlw n) (addwfc z))
203 ((sub) (cond ((= n 1) (decf z))
204 ((= n #xff) (incf z))
207 ((subb) (movlw n) (subwfb z))))
208 (let ((x (byte-cell-adr src1))
209 (y (byte-cell-adr src2))
210 (z (byte-cell-adr dst)))
211 (cond ((and (not (= x y))
213 (memq (instr-id instr)
215 ;; since this basically swaps the
216 ;; arguments, it can't be used for
221 ;; for subtraction, preserves argument
224 ;; this NEEDS to be done with movff, or
225 ;; else wreg will get clobbered and this
228 (else ;; TODO check if it could be merged with the previous case
231 (case (instr-id instr)
236 (else (error "..."))))))
238 ((mul) ; 8 by 8 multiplication
240 ;; since multiplication is commutative, the
241 ;; arguments are set up so the second one will
242 ;; be a literal if the operator is applied on a
243 ;; literal and a variable
244 (let ((n (byte-lit-val src2)))
246 (movlw (byte-lit-val src1))
247 (move-reg (byte-cell-adr src1) WREG))
248 ;; literal multiplication
250 (let ((x (byte-cell-adr src1))
251 (y (byte-cell-adr src2)))
256 ;; no instructions for bitwise operations involving
257 ;; literals exist on the PIC18
258 (let ((x (if (byte-lit? src1)
260 (byte-cell-adr src1)))
261 (y (if (byte-lit? src2)
263 (byte-cell-adr src2)))
264 (z (byte-cell-adr dst)))
265 (cond ((byte-lit? src1)
270 ((and (not (= x y)) (= y z))
275 (case (instr-id instr)
279 (else (error "...")))))
282 (let ((x (if (byte-lit? src1)
284 (byte-cell-adr src1)))
285 (z (byte-cell-adr dst)))
286 (cond ((byte-lit? src1) (move-lit x z))
287 ((not (= x z)) (move-reg x z)))
288 (case (instr-id instr)
294 (if (not (byte-lit? src2))
295 (error "bit offset must be a literal"))
296 (let ((x (byte-cell-adr src1))
297 (y (byte-lit-val src2)))
298 (case (instr-id instr)
301 ((toggle) (btg x y)))))
304 (let ((z (byte-cell-adr dst)))
306 (move-lit (byte-lit-val src1) z)
307 (move-reg (byte-cell-adr src1) z))
312 (move-lit (byte-lit-val src1) TBLPTRL)
313 (move-reg (byte-cell-adr src1) TBLPTRL))
315 (move-lit (byte-lit-val src2) TBLPTRH)
316 (move-reg (byte-cell-adr src2) TBLPTRH))
317 ;; TODO the 5 high bytes are not used for now
321 (if (null? (bb-succs bb))
322 (error "I think you might have given me an empty source file."))
323 (let* ((succs (bb-succs bb))
325 (bra (bb-label dest))
328 (let* ((succs (bb-succs bb))
329 (dest-true (car succs))
330 (dest-false (cadr succs)))
332 (define (compare flip adr)
333 (case (instr-id instr)
334 ((x<y) (if flip (cpfsgt adr) (cpfslt adr)))
335 ((x>y) (if flip (cpfslt adr) (cpfsgt adr)))
337 (bra (bb-label dest-false))
338 (bra (bb-label dest-true))
339 (add-todo dest-false)
340 (add-todo dest-true))
342 (cond ((byte-lit? src1)
343 (let ((n (byte-lit-val src1))
344 (y (byte-cell-adr src2)))
345 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
346 (eq? (instr-id instr) 'x==y))
347 (special-compare-eq-lit n x)
352 (let ((x (byte-cell-adr src1))
353 (n (byte-lit-val src2)))
354 (if #f #;(and (or (= n 0) (= n 1) (= n #xff))
355 (eq? (instr-id instr) 'x==y))
356 (special-compare-eq-lit n x)
361 (let ((x (byte-cell-adr src1))
362 (y (byte-cell-adr src2)))
367 (emit (list (instr-id instr))))))))))
371 (vector-set! bbs-vector label-num #f)
372 (label (bb-label bb))
373 (for-each dump-instr (reverse (bb-rev-instrs bb)))
374 (for-each add-todo (bb-succs bb)))))))
376 (let ((prog-label (asm-make-label 'PROG)))
381 (add-todo (vector-ref bbs-vector 0))
386 (let ((bb (car todo)))
387 (set! todo (cdr todo))
392 (define (assembler-gen filename cfg)
396 ((eval (car instr)) (cadr instr)))
398 ((eval (car instr)) (cadr instr) (caddr instr)))
400 ((eval (car instr)) (cadr instr) (caddr instr) (cadddr instr)))
404 ((movff movwf clrf setf cpfseq cpfslt cpfsgt mulwf)
406 ((incf decf addwf addwfc subwf subwfb andwf iorwf xorwf rlcf rrcf comf
410 (tblrd*)) ;; TODO support the other modes
412 (bra-or-goto (cadr instr)))
414 (rcall-or-call (cadr instr)))
419 (string-append (symbol->string (asm-label-id (cadr instr))) ":"))
420 (asm-label (cadr instr)))
424 (error "unknown instruction" instr))))
428 ;; (pretty-print cfg)
430 (let ((code (linearize-and-cleanup cfg)))
431 ;; (pretty-print code)
432 ;; if we would need a second bank, load the address for the second bank in BSR
434 (begin (gen (list 'movlw 1))
435 (gen (list 'movwf BSR 'a))))
436 (for-each gen code)))