1 ;;; generation of control flow graph
3 ;; list of all conditional branching generic instructions
4 (define conditional-instrs ;; TODO add as we add specialized instructions
5 '(x==y x!=y x<y x>y branch-if-carry))
7 ;; generic instructions that end up generating machine instructions that affect
9 (define carry-affecting-instrs
10 '(add addc sub subb rlcf rrcf))
12 ;; special variables whose contents are located in the FSR registers
13 (define fsr-variables '(SIXPIC_FSR0 SIXPIC_FSR1 SIXPIC_FSR2))
25 label-name ; if the block had a label
34 (asm-label-id (bb-label bb)))
37 extender: define-type-of-instr
38 (live-before unprintable:) ; bitset
39 (live-after unprintable:) ; bitset
46 (define-type-of-instr call-instr
50 (define-type-of-instr return-instr
54 (define (new-instr id src1 src2 dst)
55 (make-instr #f #f #f id src1 src2 dst))
57 (define (new-call-instr def-proc)
58 (make-call-instr '() '() #f 'call #f #f #f def-proc))
60 (define (new-return-instr def-proc)
61 (make-return-instr '() '() #f 'return #f #f #f def-proc))
63 (define (add-bb cfg proc id) ;; TODO maybe have the name in the label for named-bbs ? would help debugging
64 (let* ((label-num (cfg-next-label-num cfg))
65 (bb (make-bb label-num #f #f '() '() '() #f)))
71 (number->string label-num)
73 (if proc (symbol->string proc) "")
75 (if proc (number->string id) "")))))
76 (cfg-bbs-set! cfg (cons bb (cfg-bbs cfg)))
77 (cfg-next-label-num-set! cfg (+ 1 (cfg-next-label-num cfg)))
80 (define (add-instr bb instr)
81 (let ((rev-instrs (bb-rev-instrs bb)))
82 (bb-rev-instrs-set! bb (cons instr rev-instrs))))
84 (define (add-succ bb succ)
85 (bb-succs-set! bb (cons succ (bb-succs bb)))
86 (bb-preds-set! succ (cons bb (bb-preds succ))))
89 (define virtual-instructions-counts (make-table))
90 (define concrete-instructions-counts (make-table))
91 (define byte-cell-src1-counts (make-table))
92 (define byte-cell-src2-counts (make-table))
93 (define byte-cell-dst-counts (make-table))
94 (define byte-cell-all-counts (make-table))
96 (define (generate-cfg ast)
98 (define cfg (new-cfg))
100 (define bb #f) ; current bb
102 (define (in x) (set! bb x))
104 (define current-def-proc #f)
105 (define (current-def-proc-id)
107 (def-id current-def-proc)
109 (define current-def-proc-bb-id 0)
112 (let ((bb (add-bb cfg (current-def-proc-id) current-def-proc-bb-id)))
113 (set! current-def-proc-bb-id (+ current-def-proc-bb-id 1))
118 (let ((id (instr-id instr)))
119 (table-set! virtual-instructions-counts id
120 (+ (table-ref virtual-instructions-counts id 0) 1)))
121 (let ((src1 (instr-src1 instr))) ;; TODO have a macro for that
122 (if (byte-cell? src1)
123 (let ((src1 (byte-cell-name src1))) ;; TODO also include the id, in the case of multiple vars with the same name, or maybe the bb (though it is not defined everywhere
124 (table-set! byte-cell-src1-counts src1
125 (+ (table-ref byte-cell-src1-counts src1 0) 1))
126 (table-set! byte-cell-all-counts src1
127 (+ (table-ref byte-cell-all-counts src1 0) 1)))))
128 (let ((src2 (instr-src2 instr)))
129 (if (byte-cell? src2)
130 (let ((src2 (byte-cell-name src2)))
131 (table-set! byte-cell-src2-counts src2
132 (+ (table-ref byte-cell-src2-counts src2 0) 1))
133 (table-set! byte-cell-all-counts src2
134 (+ (table-ref byte-cell-all-counts src2 0) 1)))))
135 (let ((dst (instr-dst instr)))
137 (let ((dst (byte-cell-name dst)))
138 (table-set! byte-cell-dst-counts dst
139 (+ (table-ref byte-cell-dst-counts dst 0) 1))
140 (table-set! byte-cell-all-counts dst
141 (+ (table-ref byte-cell-all-counts dst 0) 1)))))
142 (add-instr bb instr))
144 (define break-stack '())
145 (define continue-stack '())
146 (define delayed-post-incdec '())
148 (define (push-break x) (set! break-stack (cons x break-stack)))
149 (define (pop-break) (set! break-stack (cdr break-stack)))
151 (define (push-continue x) (set! continue-stack (cons x continue-stack)))
152 (define (pop-continue) (set! continue-stack (cdr continue-stack)))
154 (define (push-delayed-post-incdec ast)
155 (set! delayed-post-incdec (cons ast delayed-post-incdec))
156 ;; moves the original value to a new location (so it won't be modified)
157 ;; and returns that location to the original expression
158 (let ((x (subast1 ast)))
160 (error "assignment target must be a variable")
161 (let* ((def-var (ref-def-var x))
162 (result (alloc-value (def-variable-type def-var)
163 (current-def-proc-id)
165 (move-value (def-variable-value def-var) result)
168 ;; TODO instead of carrying types around, use the length instead, or even better, just pass the value-bytes, and calculate the length as needed
169 (define (extend value type)
170 ;; literals must be extended with literal 0s, while variables must be
171 ;; extended with byte cells
172 (let* ((bytes (value-bytes value))
173 (lit? (byte-lit? (car bytes))))
174 (let loop ((rev-bytes (reverse bytes))
175 (n (max 0 (- (type->bytes type) (length bytes)))))
177 (new-value (reverse rev-bytes))
178 (loop (cons (new-byte-lit 0) ;; TODO used to extend with empty byte cells when it expanded a variable. caused weird bugs.
182 (define (program ast)
183 (let loop ((asts (ast-subasts ast)))
184 (if (not (null? asts))
185 (let ((ast (car asts)))
186 (if (null? (cdr asts))
187 (let ((value (expression ast)))
188 (return-with-no-new-bb value))
191 (loop (cdr asts))))))))
193 (define (toplevel ast)
194 (cond ((def-variable? ast)
196 ((def-procedure? ast)
201 (define (def-variable ast) ;; TODO set the fun for each byte-cell
202 (let ((subasts (ast-subasts ast)))
203 (if (not (null? subasts)) ; if needed, set the variable
204 (let ((value (expression (subast1 ast))))
205 (let ((ext-value (extend value (def-variable-type ast))))
206 (move-value value (def-variable-value ast)))))))
208 ;; resolve the C gotos by setting the appropriate successor to their bb
209 (define (resolve-all-gotos start table)
210 (let loop ((start start)
211 (visited (new-empty-set)))
212 (if (not (set-member? visited start)) ; not visited
215 (if (and (eq? (instr-id x) 'goto)
216 (instr-dst x)) ; unresolved label
217 (let ((target (assoc (instr-dst x) table))) ;; TODO use a set, but not urgent, not a bottleneck
219 (begin (add-succ start (cdr target))
220 (instr-dst-set! x #f))
221 (error "invalid goto target" (instr-dst x))))))
222 (bb-rev-instrs start))
223 (for-each (lambda (x)
224 (set-add! visited start)
226 (bb-succs start))))))
228 (define (def-procedure ast) ;; TODO set the fun for the parameters, and maybe also return value
229 (set! current-def-proc-bb-id 0)
230 (set! current-def-proc ast)
233 (def-procedure-entry-set! ast entry)
235 (for-each statement (ast-subasts ast))
236 (return-with-no-new-bb ast)
237 (set! current-def-proc #f)
238 (resolve-all-gotos entry (list-named-bbs entry))
241 ;; returns a list of all named bbs in the successor-tree of a given bb
242 (define (list-named-bbs start)
243 (let ((visited (new-empty-set)))
244 (let loop ((start start) ;; TODO not really a loop, it's tree recursion
246 (if (set-member? visited start)
251 (set-add! visited start)
254 (if (bb-label-name start)
255 (cons (cons (bb-label-name start) start) succs)
258 (define (statement ast)
259 (cond ((def-variable? ast) (def-variable ast))
260 ((block? ast) (block ast))
261 ((return? ast) (return ast))
262 ((if? ast) (if (null? (cddr (ast-subasts ast)))
265 ((while? ast) (while ast))
266 ((do-while? ast) (do-while ast))
267 ((for? ast) (for ast))
268 ((switch? ast) (switch ast))
269 ((break? ast) (break ast))
270 ((continue? ast) (continue ast))
271 ((goto? ast) (goto ast))
272 (else (expression ast))))
275 (if (block-name ast) ; named block ?
276 (begin (let ((new (new-bb)))
279 (bb-label-name-set! bb (block-name ast))))
280 (for-each statement (ast-subasts ast)))
282 (define (move from to)
283 (emit (new-instr 'move from #f to)))
285 (define (move-value from to)
286 (let loop ((from (value-bytes from))
287 (to (value-bytes to)))
288 (cond ((null? to)) ; done, we truncate the rest
289 ((null? from) ; promote the value by padding
290 (move (new-byte-lit 0) (car to))
291 (loop from (cdr to)))
293 (move (car from) (car to))
294 (loop (cdr from) (cdr to))))))
296 (define (return-with-no-new-bb def-proc)
297 (emit (new-return-instr def-proc)))
300 (if (null? (ast-subasts ast))
301 (return-with-no-new-bb current-def-proc)
302 (let ((value (expression (subast1 ast))))
303 (let ((ext-value (extend value (def-procedure-type current-def-proc))))
304 (move-value value (def-procedure-value current-def-proc))
305 (return-with-no-new-bb current-def-proc))))
309 (let* ((bb-join (new-bb))
311 (test-expression (subast1 ast) bb-then bb-join)
313 (statement (subast2 ast))
318 (let* ((bb-join (new-bb))
321 (test-expression (subast1 ast) bb-then bb-else)
323 (statement (subast2 ast))
326 (statement (subast3 ast))
331 (let* ((bb-cont (new-bb))
334 (push-continue bb-cont)
338 (test-expression (subast1 ast) bb-body bb-exit)
340 (statement (subast2 ast))
346 (define (do-while ast)
347 (let* ((bb-body (new-bb))
350 (push-continue bb-cont)
354 (statement (subast1 ast))
357 (test-expression (subast2 ast) bb-body bb-exit)
363 (let* ((bb-loop (new-bb))
367 (statement (subast1 ast))
369 (push-continue bb-cont)
372 (test-expression (subast2 ast) bb-body bb-exit)
374 (statement (subast4 ast))
377 (statement (subast3 ast))
383 ;; switchs with branch tables
384 ;; since offsets are calculated using one byte, switches are limited to
385 ;; 60 cases or so (slightly below 64)
387 (let* ((var (subast1 ast))
391 (let loop ((asts (cdr (ast-subasts ast))) ; car is the tested variable
392 (bbs '()) ; first bb of each case
393 (end-bbs '()) ; last bb of each case
394 (cases '())) ; case labels
395 (if (not (null? asts))
400 (if (or (null? (bb-succs case-bb))
401 (not (bb-label-name (car (bb-succs case-bb)))))
402 ;; the first block inside the body of a switch might not
403 ;; have a label, in which case it ill be skipped
404 ;; to have a valid CFG, it must still contain an instruction
405 (begin (gen-goto exit-bb)
406 (loop (cdr asts) bbs end-bbs cases))
410 ;; blocks create their own bb, which contains the case label
411 (cons (bb-label-name (car (bb-succs case-bb)))
413 (let ((bbs (reverse bbs))
414 (end-bbs (reverse end-bbs))
415 (cases (reverse cases))
417 ;; add the case names to the bb names
418 (for-each ;; TODO do it for all named bbs, not just switch (but, since the name is on the successor, might be lost)
421 (bb-label (car (bb-succs bb))) 2
423 (string-append (symbol->string (bb-name bb))
427 (symbol->string case)
430 (symbol->string (car case))
431 (number->string (cadr case))))))))
434 ;; handle fall-throughs
437 (let ((case-bb (list-ref end-bbs i)))
438 (if (null? (bb-succs case-bb))
441 (gen-goto (if (= i (- l 1)) ; last bb
443 (list-ref bbs (+ i 1))))))))
445 (let* ((default (memq 'default cases)) ;; TODO if default, since we can't know the domain of possible values (at least, not with enough precision for it to be interesting), revert to naive switch
446 ;; cases are lists (case n) and we want the numbers
447 (cases (map cadr (filter list? cases)))
448 (case-max (foldl max 0 cases))
449 (case-min (foldl min case-max cases))
450 (n-entries (+ (- case-max case-min) 1)))
451 (if default (error "default is not supported with switch"))
455 (cond ((pos-in-list (+ i case-min) cases)
456 => (lambda (j) (list-ref bbs j)))
457 ;; no label, jump to the exit TODO would jump to default, eventually
460 ;; the branch-table virtual instruction takes the byte to check
461 ;; to choose the branch
465 ;; the cases now start from 0, so we might have to
466 ;; adjust the checked variable
471 (let* ((op (operation? '(six.x-y)))
473 (list var (new-literal 'uint8
477 (expr-type-set! ast ((op-type-rule op) ast))
479 ;; working space to calculate addresses
480 (new-byte-cell (current-def-proc-id) #f (bb-name bb))
485 ;; ;; naive switch with if cascade ;; TODO revert to that if there's a case we can't handle with branch tables
486 ;; (define (switch ast)
487 ;; (let* ((var (subast1 ast))
491 ;; (exit-bb (new-bb))
492 ;; (prev-bb decision-bb))
493 ;; (push-break exit-bb)
494 ;; (for-each (lambda (x) ; generate each case
495 ;; (in (new-bb)) ; this bb will be given the name of the case
496 ;; (add-succ decision-bb bb)
497 ;; ;; if the previous case didn't end in a break, fall through
498 ;; (if (null? (bb-succs prev-bb))
504 ;; (set! prev-bb bb))
505 ;; (cdr (ast-subasts ast)))
506 ;; (if (null? (bb-succs prev-bb)) ; if the last case didn't end in a break, fall through to the exit
507 ;; (gen-goto exit-bb))
508 ;; (bb-succs-set! decision-bb (reverse (bb-succs decision-bb))) ; preserving the order is important in the absence of break
509 ;; (set! case-list (list-named-bbs decision-bb))
510 ;; (set! default (filter (lambda (x) (eq? (car x) 'default))
511 ;; (list-named-bbs decision-bb)))
512 ;; (set! case-list (filter (lambda (x) (and (list? (car x))
513 ;; (eq? (caar x) 'case)))
515 ;; (bb-succs-set! decision-bb '()) ; now that we have the list of cases we don't need the successors anymore
516 ;; (let loop ((case-list case-list)
517 ;; (decision-bb decision-bb))
519 ;; (if (not (null? case-list))
520 ;; (let* ((next-bb (new-bb))
521 ;; (curr-case (car case-list))
522 ;; (curr-case-id (cadar curr-case))
523 ;; (curr-case-bb (cdr curr-case)))
524 ;; (emit (new-instr 'x==y
525 ;; (car (value-bytes (expression var)))
526 ;; (new-byte-lit curr-case-id) #f))
527 ;; (add-succ bb next-bb) ; if false, keep looking
528 ;; (add-succ bb curr-case-bb) ; if true, go to the case
529 ;; (loop (cdr case-list)
531 ;; (gen-goto (if (not (null? default))
538 (gen-goto (car break-stack)))
540 (define (continue ast)
541 (gen-goto (car continue-stack)))
543 ;; generates a goto with a target label. once the current function definition
544 ;; is over, all these labels are resolved. therefore, we don't have any gotos
545 ;; that jump from a function to another
547 (emit (new-instr 'goto #f #f (subast1 ast))))
549 (define (gen-goto dest)
550 (if (null? (bb-succs bb))
551 ;; since this is an unconditional goto, we want only one
552 (begin (add-succ bb dest)
553 (emit (new-instr 'goto #f #f #f)))))
555 (define (test-expression ast bb-true bb-false)
557 (define (test-byte id byte1 byte2 bb-true bb-false)
558 (define (test-lit id x y)
563 (else (error "invalid test")))
566 (cond ((and (byte-lit? byte1) (byte-lit? byte2))
567 (if (test-lit id (byte-lit-val byte1) (byte-lit-val byte2))
569 (gen-goto bb-false)))
571 ;; since we cons each new successor at the front, true has to be
573 (add-succ bb bb-false)
574 (add-succ bb bb-true)
575 (emit (new-instr id byte1 byte2 #f)))
582 (else (error "invalid test")))))
583 (add-succ bb bb-false)
584 (add-succ bb bb-true)
585 (emit (new-instr id byte2 byte1 #f))))
587 (add-succ bb bb-false)
588 (add-succ bb bb-true)
589 (emit (new-instr id byte1 byte2 #f)))))
591 (define (test-value id value1 value2 bb-true bb-false)
592 (let loop ((bytes1 (value-bytes value1)) ; lsb first
593 (bytes2 (value-bytes value2))
596 (if (not (and (null? bytes1) (null? bytes2)))
597 ;; note: won't work with signed types, as the padding is done
599 (loop (if (null? bytes1) bytes1 (cdr bytes1))
600 (if (null? bytes2) bytes2 (cdr bytes2))
601 (cons (if (null? bytes1) (new-byte-lit 0) (car bytes1))
603 (cons (if (null? bytes2) (new-byte-lit 0) (car bytes2))
605 ;; now so the test itself, using the padded values
606 (let ((padded1 (reverse padded1))
607 (padded2 (reverse padded2)))
609 ((x==y) ; unlike < and >, must check all bytes, so is simpler
610 (let loop2 ((bytes1 padded1) ;; TODO ior the xors, but increases PICOBIT's size
612 (let ((byte1 (car bytes1))
613 (byte2 (car bytes2)))
614 (if (null? (cdr bytes1))
615 (test-byte 'x==y byte1 byte2 bb-true bb-false)
616 (let ((bb-true2 (new-bb)))
617 (test-byte 'x==y byte1 byte2 bb-true2 bb-false)
619 (loop2 (cdr bytes1) (cdr bytes2)))))))
622 (if (= (length padded1) 1)
623 (test-byte id (car padded1) (car padded2)
625 ;; more than one byte, we subtract, then see if we had
627 (let ((scratch (new-byte-cell
628 (current-def-proc-id)
629 "cmp-scratch" (bb-name bb))))
631 ;; our values might contain literal bytes and sub and
632 ;; subb can't have literals in their first argument,
633 ;; allocate it somewhere if needed
634 (if (and (foldl (lambda (acc new)
635 (or acc (byte-lit? new)))
638 (let ((tmp (alloc-value
639 (bytes->type (length padded2))
640 (current-def-proc-id)
642 (move-value (new-value padded2) tmp)
643 (set! padded2 (value-bytes tmp))))
644 (if (and (foldl (lambda (acc new) ;; TODO abstract both cases
645 (or acc (byte-lit? new)))
648 (let ((tmp (alloc-value
649 (bytes->type (length padded1))
650 (current-def-proc-id)
652 (move-value (new-value padded1) tmp)
653 (set! padded1 (value-bytes tmp))))
655 (let loop ((bytes1 padded1)
658 (if (not (null? bytes1))
659 (let ((b1 (car bytes1))
664 (if borrow? 'subb 'sub)
668 (if borrow? 'subb 'sub)
670 (loop (cdr bytes1) (cdr bytes2) #t))))
672 (add-succ bb bb-false)
673 (add-succ bb bb-true)
674 (emit (new-instr 'branch-if-carry scratch #f #f))))))))))
676 (define (test-relation id x y bb-true bb-false)
677 (cond ((and (literal? x) (not (literal? y)))
678 ;; literals must be in the last argument for code generation
679 ;; flip the relation if needed
680 (test-relation (case id
681 ((x==y x!=y) id) ; commutative, no change
686 (else (error "relation error")))
691 ((assq id '((x!=y . x==y) (x<=y . x>y) (x>=y . x<y)))
692 ;; flip the destination blocks to have a simpler comparison
694 (lambda (z) (test-relation (cdr z) x y bb-false bb-true)))
697 ;; ' ;; TODO use these special cases, but fall back on the current implementation for default
700 ;; (cond ((and (literal? y) (= (literal-val y) 0))
701 ;; (test-zero x bb-true bb-false))
703 ;; (test-eq-lit x (literal-val y) bb-true bb-false))
705 ;; (error "unhandled case"))))
707 ;; (cond ((and (literal? y) (= (literal-val y) 0))
708 ;; (test-negative x bb-true bb-false))
710 ;; (error "unhandled case"))))
712 ;; (cond ((and (literal? y) (= (literal-val y) 0))
713 ;; (test-positive x bb-true bb-false))
715 ;; (error "unhandled case"))))
717 ;; (error "unexpected operator")))
719 (let* ((value1 (expression x))
720 (value2 (expression y)))
721 (test-value id value1 value2 bb-true bb-false))
724 (define (test-zero ast bb-true bb-false)
727 (let ((type (expr-type ast))
728 (value (expression ast)))
729 ;; since nonzero is true, we must swap the destinations to use ==
730 (test-value 'x==y value (int->value 0 type) bb-false bb-true)))
733 (let* ((op (oper-op ast))
737 (test-zero (subast1 ast) bb-false bb-true))
739 (let ((bb-true2 (new-bb)))
740 (test-zero (subast1 ast) bb-true2 bb-false)
742 (test-zero (subast2 ast) bb-true bb-false)))
744 (let ((bb-false2 (new-bb)))
745 (test-zero (subast1 ast) bb-true bb-false2)
747 (test-zero (subast2 ast) bb-true bb-false)))
748 ((x==y x!=y x<y x>y x<=y x>=y)
757 (test-zero ast bb-true bb-false))
759 ;; checks if an expression has already been computed in the current procedure
760 ;; TODO maybe stock all the available expressions not in the def-procedure, but in each node ? as variables are mutated, or functions called, we remove from it ? calls could really harm us, since to be conservative, all calls can be considered to mutate all globals
761 ;; TODO if we end up doing dataflow analysis for cse, also use it for dead-code elimination, if possible
762 (define (computed-expression? ast) ;; TODO to distinguish between global and local, look at the def-proc-id, if #f, it's global
763 ;; TODO reject if it has side effects (++, --) or calls (or extend the analyis to calls ?), a complete mutability analysis would probably be needed for every variable in the expression... checking only in the procedure won't work, since a called function might change a global that's inside the expression in the time between 2 of its uses. maybe SSA (single static assignment) would help ? ALSO REJECT IF IT READS FROM MEMORY (since we can't really know if what we read has not changed. Or maybe consider the memory as a global for this ?)
764 ;; TODO when we see calls and assignments, remove from the set of expressions what is not valid anymore (lhs for assignments, globals for calls)
765 (and current-def-proc
767 (def-procedure-computed-expressions current-def-proc) ast #f)))
769 (define (expression ast)
772 (computed-expression? ast) ;; TODO breaks something in register allocation, possibly due to the ++ and -- ?
773 (cond ((literal? ast) (literal ast))
774 ((ref? ast) (ref ast))
775 ((oper? ast) (oper ast))
776 ((call? ast) (call ast))
777 (else (error "unexpected ast" ast))))))
779 (table-set! (def-procedure-computed-expressions current-def-proc)
780 ast result)) ; cache the result
781 (do-delayed-post-incdec)
784 (define (literal ast)
785 (let ((val (literal-val ast)))
786 (int->value val (expr-type ast))))
789 (let* ((def-var (ref-def-var ast))
790 (value (def-variable-value def-var)))
793 (define (add-sub id value1 value2 result)
794 (let loop ((bytes1 (value-bytes value1)) ; car is lsb
795 (bytes2 (value-bytes value2))
796 (bytes3 (value-bytes result))
797 (ignore-carry-borrow? #t))
798 (if (not (null? bytes3))
799 ;; if we would add or subtract 0 and not use the carry, just move
801 (let ((b1 (car bytes1)) (b2 (car bytes2)) (b3 (car bytes3)))
803 (if ignore-carry-borrow?
804 (case id ((x+y) 'add) ((x-y) 'sub))
805 (case id ((x+y) 'addc) ((x-y) 'subb)))
807 (loop (cdr bytes1) (cdr bytes2) (cdr bytes3) #f))
810 (define (mul value-x value-y type result)
811 (let* ((bytes-x (value-bytes value-x))
812 (bytes-y (value-bytes value-y))
813 ;; to determine the length of the operands, we ignore the padding
814 (lx (length (filter (lambda (x) (not (and (byte-lit? x)
815 (= (byte-lit-val x) 0))))
817 (ly (length (filter (lambda (x) (not (and (byte-lit? x)
818 (= (byte-lit-val x) 0))))
820 ;; if this a multiplication by 2 or 4, we use additions instead
821 ;; at this point, only y (or both x and y) can contain a literal
823 (byte-lit? (car bytes-y))
824 (let ((v (byte-lit-val (car bytes-y))))
825 (or (= v 2) (= v 4))))
826 (case (byte-lit-val (car bytes-y))
827 ((2) (add-sub 'x+y value-x value-x result)) ; simple addition
828 ((4) (let ((tmp (alloc-value (bytes->type
829 (length (value-bytes result)))
830 (current-def-proc-id)
832 (add-sub 'x+y value-x value-x tmp)
833 (add-sub 'x+y tmp tmp result))))
834 ;; if not, we have to do it the long way
836 ;; finds the appropriate multiplication routine (depending on the
837 ;; length of each argument) and turns the multiplication into a
838 ;; call to the routine
839 ;; the arguments must be the asts of the 2 arguments (x and y) and
840 ;; the type of the returned value, since these are what are
841 ;; expected by the call function
843 ;; to avoid code duplication (i.e. having a routine for 8 by 16
844 ;; multplication and one for 16 by 8), the longest operand goes first
853 (string->symbol ; mul8_8, mul8_16, etc
854 ;; for now, only unsigned multiplications are supported
855 (string-append "__mul"
856 (number->string (* lx 8)) "_"
857 (number->string (* ly 8))))
858 (list value-x value-y)
861 (define (mod x y result)
862 (let* ((bytes1 (value-bytes x))
863 (bytes2 (value-bytes y))
864 (bytes3 (value-bytes result))
866 ;; if y is a literal and a power of 2, we can do a bitwise and
867 (if (and (byte-lit? y0)
868 (let ((x (/ (log (value->int y)) (log 2))))
870 ;; bitwise and with y - 1
871 (let ((tmp (alloc-value (bytes->type (length bytes2))
872 (current-def-proc-id)
874 (move-value (int->value (- (value->int y) 1)
875 (bytes->type (length bytes2)))
877 (bitwise 'x&y x tmp result))
878 ;; TODO for the general case, try to optimise the case where division and modulo are used together, since they are calculated together
879 (error "modulo is only supported for powers of 2"))
882 (define (shift id value-x value-y type result)
883 (let ((bytes1 (value-bytes value-x))
884 (bytes2 (value-bytes value-y))
885 (bytes3 (value-bytes result)))
886 ;; if the second argument is a literal and a multiple of 8, we can simply
887 ;; move the bytes around
888 (let ((y0 (car bytes2)))
889 (if (and (byte-lit? y0) (= (modulo (byte-lit-val y0) 8) 0))
890 ;; uses only the first byte, but shifting by 255 should be enough
891 (let ((n (/ (byte-lit-val y0) 8))
892 (l (length bytes1))) ; same length for x and result
899 (new-byte-lit 0) ; padding
902 (loop (+ i 1) (if (< i n) x (cdr x))))
904 (move (if (<= l (+ i n))
906 (list-ref x (+ i n)))
912 (string-append "__sh"
913 (case id ((x<<y) "l") ((x>>y) "r"))
914 (number->string (* 8 (length bytes1)))))
915 (list value-x value-y)
918 ;; bitwise and, or, xor
919 ;; TODO similar to add-sub and probably others, abstract multi-byte ops
920 ;; TODO use bit set, clear and toggle for some shortcuts
921 (define (bitwise id value1 value2 result)
922 (let loop ((bytes1 (value-bytes value1))
923 (bytes2 (value-bytes value2))
924 (bytes3 (value-bytes result)))
925 (if (not (null? bytes3)) ;; TODO check for cases like or 0, or ff, and 0, and ff, ...
927 (emit (new-instr (case id ((x&y) 'and) ((|x\|y|) 'ior) ((x^y) 'xor))
928 (car bytes1) (car bytes2) (car bytes3)))
929 (loop (cdr bytes1) (cdr bytes2) (cdr bytes3)))
932 (define (bitwise-negation x result)
933 (let loop ((bytes1 (value-bytes x))
934 (bytes2 (value-bytes result)))
935 (if (not (null? bytes2))
936 (begin (emit (new-instr 'not (car bytes1) #f (car bytes2)))
937 (loop (cdr bytes1) (cdr bytes2))))))
939 (define (do-delayed-post-incdec)
940 (if (not (null? delayed-post-incdec))
941 (let* ((ast (car delayed-post-incdec))
942 (type (expr-type ast))
945 (set! delayed-post-incdec (cdr delayed-post-incdec))
946 (let ((x (subast1 ast)))
948 (error "assignment target must be a variable"))
949 (let ((result (def-variable-value (ref-def-var x))))
950 ;; clobbers the original value, which is fine, since it
951 ;; was moved somewhere else for the expression
952 (add-sub (if (eq? id 'x++) 'x+y 'x-y)
956 (do-delayed-post-incdec))))
958 ;; calculates an address in an array by adding the base pointer and the offset
959 ;; and puts the answer in FSR0 so that changes to INDF0 change the array
961 (define (calculate-address ast)
962 ;; if we have a special FSR variable, no need to calculate the address as
963 ;; it is already in the register
964 (let ((base-name (array-base-name ast))
965 (index? (eq? (op-id (oper-op ast)) 'index)))
966 (if (not (and base-name
967 (memq base-name fsr-variables)))
968 (let ((base (expression (subast1 ast)))
969 ;; NB: actual addresses are 12 bits, not 16
970 (address (new-value (list (get-register FSR0L)
971 (get-register FSR0H)))))
973 ;; we pad up to uint16, since it is the size of the addresses
974 (let ((value1 (extend base 'uint16))
975 (value2 (extend (expression (subast2 ast)) 'uint16)))
976 (add-sub 'x+y value1 value2 address))
977 ;; no offset with simple dereference
978 (move-value base address)))
979 (error "You used the array index syntax with a FSR variable, didn't you? I told you not to."))))
981 (define (array-base-name ast)
982 ;; returns #f if the lhs is not a direct variable reference
983 ;; eg : *x++ ; (x+y)* ; ...
984 (let ((lhs (subast1 ast)))
986 (def-id (ref-def-var lhs)))))
988 (define (get-indf base-name)
989 ;; INDF0 is not here, since it's already used for regular array accesses
990 (if (eq? base-name 'SIXPIC_FSR1)
991 (new-value (list (get-register INDF1)))
992 (new-value (list (get-register INDF2)))))
995 (let* ((type (expr-type ast))
999 (define (arith-op id x y value-x value-y)
1000 ;; since code generation does not accept literals as first
1001 ;; arguments unless both arguments are, if this is the
1002 ;; case, we either have to swap the arguments (if
1003 ;; possible) or allocate the argument somewhere
1004 (if (and (literal? x) (not (literal? y)))
1005 (if (memq id '(x+y x*y x&y |x\|y| x^y))
1006 ;; the operator is commutative, we can swap the args
1007 (let ((tmp value-x))
1008 (set! value-x value-y)
1010 ;; the operator is not commutative, we have to
1011 ;; allocate the first argument somewhere
1012 (let ((dest (alloc-value (expr-type x)
1013 (current-def-proc-id)
1015 (move-value value-x dest)
1016 (set! value-x dest))))
1017 (let ((result (alloc-value type
1018 (current-def-proc-id)
1021 ((x+y x-y) (add-sub id value-x value-y result))
1022 ((x*y) (mul value-x value-y type result))
1023 ((x/y) (error "division not implemented yet")) ;; TODO optimize for powers of 2
1024 ((x%y) (mod value-x value-y result))
1025 ((x&y |x\|y| x^y) (bitwise id value-x value-y result))
1026 ((x>>y x<<y) (shift id value-x value-y type result)))))
1032 (let ((x (extend (expression (subast1 ast))
1034 (result (alloc-value type
1035 (current-def-proc-id)
1042 ((~x) (bitwise-negation x result)))
1045 (let ((x (subast1 ast)))
1047 (error "assignment target must be a variable"))
1048 (let ((result (def-variable-value (ref-def-var x))))
1049 (add-sub (if (eq? id '++x) 'x+y 'x-y)
1055 (let ((x (subast1 ast)))
1057 (error "assignment target must be a variable"))
1058 ;; push-delayed-post-incdec moves the original value
1059 ;; somewhere else, and returns that location
1060 (push-delayed-post-incdec ast)))
1062 ;; if it's a FSR variable, no adress to set
1063 (let ((base-name (array-base-name ast)))
1064 (if (and (ref? (subast1 ast)) ; do we have a FSR variable ?
1066 (memq base-name fsr-variables))
1067 (get-indf base-name)
1068 (begin (calculate-address ast)
1069 (new-value (list (get-register INDF0)))))))
1071 (error "unary operation error" id))))
1075 ((x+y x-y x*y x/y x%y x&y |x\|y| x^y x>>y x<<y)
1076 (let* ((x (subast1 ast))
1078 (let* ((value-x (extend (expression x) type))
1079 (value-y (extend (expression y) type)))
1080 (arith-op id x y value-x value-y))))
1082 (let* ((x (subast1 ast))
1084 (value-y (expression y)))
1086 ;; lhs is a variable
1088 (let ((ext-value-y (extend value-y type)))
1089 (let ((result (def-variable-value (ref-def-var x))))
1090 (move-value value-y result)
1092 ;; lhs is a pointer dereference
1093 ((and (oper? x) (eq? (op-id (oper-op x)) '*x))
1094 (let ((base-name (array-base-name x))
1095 (val (car (value-bytes value-y))))
1096 (if (and (ref? (subast1 x))
1098 (memq base-name fsr-variables))
1099 (move val (car (value-bytes (get-indf base-name))))
1100 (begin (calculate-address x)
1101 (move val (get-register INDF0))))))
1102 ;; lhs is an indexed array access
1103 ((and (oper? x) (eq? (op-id (oper-op x)) 'index))
1104 ;; note: this will throw an error if SIXPIC_FSR{1,2} is
1105 ;; used. this is by design, as it would clobber the value
1106 ;; in the FSR registers, which goes against their purpose
1107 ;; of storing a user-chosen value
1108 (calculate-address x)
1109 ;; this section of memory is a byte array, only the lsb
1111 (move (car (value-bytes value-y)) (get-register INDF0)))
1112 (else (error "assignment target must be a variable or an array slot")))))
1114 ;; note: throws an error if given SIXPIC_FSR{1,2}, see above
1115 (calculate-address ast)
1116 (new-value (list (get-register INDF0))))
1117 ((x+=y x-=y x*=y x/=y x%=y x&=y |x\|=y| x^=y x>>=y x<<=y)
1118 (let* ((x (subast1 ast))
1120 (value-x (extend (expression x) type))
1121 (value-y (extend (expression y) type)))
1122 (move-value (arith-op (case id
1133 x y value-x value-y)
1136 ((x==y x!=y x>y x>=y x<y x<=y x&&y |x\|\|y|)
1141 (result (alloc-value type
1142 (current-def-proc-id)
1145 (move-value (int->value 1 type) result)
1148 (move-value (int->value 0 type) result)
1151 (test-expression ast bb-true bb-false)
1155 (error "binary operation error" id))))
1162 (result (alloc-value type
1163 (current-def-proc-id)
1166 (move-value (expression (subast2 ast)) result)
1169 (move-value (expression (subast3 ast)) result)
1172 (test-expression (subast1 ast) bb-true bb-false)
1176 ;; generates the cfg for a predefined routine and adds it to the current cfg
1177 (define (include-predefined-routine proc)
1178 (define (get-bytes var)
1179 (value-bytes (def-variable-value var)))
1180 (let ((old-proc current-def-proc) ; if we were already defining a procedure, save it
1181 (old-bb-no current-def-proc-bb-id)
1183 (params (def-procedure-params proc))
1184 (value (def-procedure-value proc))
1186 (entry (begin (set! current-def-proc proc)
1187 (set! current-def-proc-bb-id 0)
1188 (new-bb)))) ;; TODO insipired from def-procedure, abstract
1189 (def-procedure-entry-set! proc entry)
1194 (let* ((x (get-bytes (car params)))
1197 (z0 (car (value-bytes value))))
1198 ;; TODO use postinc/dec and co
1199 (emit (new-instr 'tblrd x0 x1 #f))
1200 (move (get-register TABLAT) z0)))
1203 (emit (new-instr 'sleep #f #f #f)))
1205 ((uart_write) ; writes a byte ;; TODO should configure the uart
1206 (let* ((x (car (get-bytes (car params)))))
1207 (move x (get-register TXREG)))) ;; TODO this is mostly for debugging purposes, won't work on the chip
1208 ((uart_read) ; reads a byr
1209 (let ((z (car (value-bytes value))))
1210 (move (get-register RCREG) z)))
1213 (let ((x (car params))
1215 (z (value-bytes value)))
1216 ;; TODO implement literal multiplication in the simulator
1217 (emit (new-instr 'mul (car (get-bytes x)) (car (get-bytes y)) #f))
1218 (move (get-register PRODL) (car z)))) ; lsb
1221 (let* ((x (get-bytes (car params)))
1224 (y (get-bytes (cadr params)))
1226 (z (value-bytes value))
1229 (emit (new-instr 'mul y0 x1 #f))
1230 (move (get-register PRODL) z1)
1232 (emit (new-instr 'mul y0 x0 #f))
1233 (move (get-register PRODL) z0)
1234 (emit (new-instr 'add (get-register PRODH) z1 z1))))
1237 (let* ((x (get-bytes (car params)))
1240 (y (get-bytes (cadr params)))
1243 (z (value-bytes value))
1247 (emit (new-instr 'mul x0 y0 #f))
1248 (move (get-register PRODH) z1)
1249 (move (get-register PRODL) z0)
1251 (emit (new-instr 'mul x0 y1 #f))
1252 (emit (new-instr 'add (get-register PRODL) z1 z1))
1254 (emit (new-instr 'mul x1 y0 #f))
1255 (emit (new-instr 'add (get-register PRODL) z1 z1))))
1258 (let* ((x (get-bytes (car params)))
1263 (y (get-bytes (cadr params)))
1266 (z (value-bytes value))
1272 (emit (new-instr 'mul x0 y0 #f))
1273 (move (get-register PRODH) z1)
1274 (move (get-register PRODL) z0)
1276 (emit (new-instr 'mul x1 y1 #f))
1277 (move (get-register PRODH) z3)
1278 (move (get-register PRODL) z2)
1280 (emit (new-instr 'mul x1 y0 #f))
1281 (emit (new-instr 'add (get-register PRODL) z1 z1))
1282 (emit (new-instr 'addc (get-register PRODH) z2 z2))
1283 (emit (new-instr 'addc z3 (new-byte-lit 0) z3))
1285 (emit (new-instr 'mul x0 y1 #f))
1286 (emit (new-instr 'add (get-register PRODL) z1 z1))
1287 (emit (new-instr 'addc (get-register PRODH) z2 z2))
1288 (emit (new-instr 'addc z3 (new-byte-lit 0) z3))
1290 (emit (new-instr 'mul x2 y0 #f))
1291 (emit (new-instr 'add (get-register PRODL) z2 z2))
1292 (emit (new-instr 'addc (get-register PRODH) z3 z3))
1294 (emit (new-instr 'mul x2 y1 #f))
1295 (emit (new-instr 'add (get-register PRODL) z3 z3))
1297 (emit (new-instr 'mul x3 y0 #f))
1298 (emit (new-instr 'add (get-register PRODL) z3 z3))))
1300 ((__shl8 __shr8 __shl16 __shr16 __shl32 __shr32)
1301 (let* ((id (symbol->string id))
1302 (left-shift? (eq? (string-ref id 4) #\l))
1303 (x (def-variable-value (car params)))
1304 (y (def-variable-value (cadr params)))
1305 (y0 (car (value-bytes y))) ; shift by 255 is enough
1306 (bytes-z (value-bytes value))
1309 (after-bb (new-bb)))
1310 (move-value x value)
1311 (gen-goto start-bb) ; fall through to the loop
1313 ;; if we'd shift of 0, we're done
1314 (add-succ bb loop-bb) ; false
1315 (add-succ bb after-bb) ; true
1316 (emit (new-instr 'x==y y0 (new-byte-lit 0) #f))
1318 ;; clear the carry, to avoid reinserting it in the register
1319 (emit (new-instr 'clear
1320 (get-register STATUS)
1323 ;; shift for each byte, since it's a rotation using the carry,
1324 ;; what goes out from the low bytes gets into the high bytes
1325 (for-each (lambda (b)
1326 (emit (new-instr (if left-shift? 'shl 'shr)
1328 (if left-shift? bytes-z (reverse bytes-z)))
1329 (emit (new-instr 'sub y0 (new-byte-lit 1) y0))
1332 (return-with-no-new-bb proc)
1333 (set! current-def-proc old-proc)
1334 (set! current-def-proc-bb-id old-bb-no)
1335 (resolve-all-gotos entry (list-named-bbs entry))
1338 (define (call ast #!optional (evaluated-args #f))
1339 ;; note: we might call this with an ast here the arguments are already
1340 ;; evaluated (when doing a routine call)
1341 (let* ((def-proc (call-def-proc ast))
1342 (arguments (ast-subasts ast))
1343 (parameters (def-procedure-params def-proc)))
1344 (if (and (memq (def-id def-proc) predefined-routines)
1345 (not (def-procedure-entry def-proc)))
1346 ;; it's the first time we encounter this predefined routine, generate
1347 ;; the corresponding cfg
1348 (include-predefined-routine def-proc))
1349 ;; argument number check
1350 (if (not (= (length (if evaluated-args evaluated-args arguments))
1351 (length parameters))) ;; TODO check at parse time ?
1352 (error (string-append "wrong number of arguments given to function "
1353 (symbol->string (def-id def-proc)) ": "
1354 (number->string (length arguments)) " given, "
1355 (number->string (length parameters))
1357 (for-each (lambda (ast def-var)
1358 (let ((value (if evaluated-args ast (expression ast))))
1359 (let ((ext-value (extend value (def-variable-type def-var))))
1360 (move-value value (def-variable-value def-var)))))
1361 (if evaluated-args evaluated-args arguments)
1363 (emit (new-call-instr def-proc))
1364 (let ((value (def-procedure-value def-proc)))
1366 (alloc-value (def-procedure-type def-proc)
1367 (current-def-proc-id)
1369 (move-value value result)
1372 ;; call to a predefined routine, a simple wrapper to an ordinary call
1373 ;; name is a symbol, args is a list of the evaluated arguments
1374 (define (routine-call name args type)
1375 (cond ((memp (lambda (x) (eq? (def-id x) name))
1377 => (lambda (x) (call (new-call '() type (car x)) args)))
1378 (else (error "unknown routine: " name))))
1384 (define (print-cfg-bbs cfg)
1385 (for-each (lambda (bb)
1386 (pp (list "BB:" (bb-name bb)
1387 "SUCCS" (map bb-name (bb-succs bb))
1388 "PREDS" (map bb-name (bb-preds bb))
1389 (cond ((null? (bb-rev-instrs bb)) "EMPTY")
1390 ((and (null? (cdr (bb-rev-instrs bb)))
1391 (eq? (instr-id (car (bb-rev-instrs bb))) 'goto)) "SINGLE GOTO")