1 ;;; generation of control flow graph
3 ;; special variables whose contents are located in the FSR registers
4 (define fsr-variables '(SIXPIC_FSR0 SIXPIC_FSR1 SIXPIC_FSR2))
15 label-name ; if the block had a label
21 live-before) ; stored as a set
24 (asm-label-id (bb-label bb)))
27 extender: define-type-of-instr
28 (live-before unprintable:) ; these 2 are stored as sets
29 (live-after unprintable:)
36 (define-type-of-instr call-instr
40 (define-type-of-instr return-instr
44 (define (new-instr id src1 src2 dst)
45 (make-instr (new-empty-set) (new-empty-set) #f id src1 src2 dst))
47 ;; list of all conditional branching generic instructions
48 (define conditional-instrs ;; TODO add as we add specialized instructions
49 '(x==y x!=y x<y x>y x<=y x>=y))
51 (define (new-call-instr def-proc)
52 (make-call-instr '() '() #f 'call #f #f #f def-proc))
54 (define (new-return-instr def-proc)
55 (make-return-instr '() '() #f 'return #f #f #f def-proc))
57 (define (add-bb cfg proc id) ;; TODO maybe have the name in the label for named-bbs ? would help debugging
58 (let* ((label-num (cfg-next-label-num cfg))
59 (bb (make-bb label-num #f #f '() '() '() (new-empty-set))))
65 (number->string label-num)
67 (if proc (symbol->string proc) "")
69 (if proc (number->string id) "")))))
70 (cfg-bbs-set! cfg (cons bb (cfg-bbs cfg)))
71 (cfg-next-label-num-set! cfg (+ 1 (cfg-next-label-num cfg)))
74 (define (add-instr bb instr)
75 (let ((rev-instrs (bb-rev-instrs bb)))
76 (bb-rev-instrs-set! bb (cons instr rev-instrs))))
78 (define (add-succ bb succ)
79 (bb-succs-set! bb (cons succ (bb-succs bb)))
80 (bb-preds-set! succ (cons bb (bb-preds succ))))
82 (define (generate-cfg ast)
84 (define cfg (new-cfg))
86 (define bb #f) ; current bb
88 (define (in x) (set! bb x))
92 (if current-def-proc (def-id current-def-proc) #f)
93 current-def-proc-bb-id)))
94 (set! current-def-proc-bb-id (+ current-def-proc-bb-id 1))
97 (define (emit instr) (add-instr bb instr))
99 (define current-def-proc #f)
100 (define (current-def-proc-id)
102 (def-id current-def-proc)
104 (define current-def-proc-bb-id 0)
105 (define break-stack '())
106 (define continue-stack '())
107 (define delayed-post-incdec '())
109 (define (push-break x) (set! break-stack (cons x break-stack)))
110 (define (pop-break) (set! break-stack (cdr break-stack)))
112 (define (push-continue x) (set! continue-stack (cons x continue-stack)))
113 (define (pop-continue) (set! continue-stack (cdr continue-stack)))
115 (define (push-delayed-post-incdec ast)
116 (set! delayed-post-incdec (cons ast delayed-post-incdec))
117 ;; moves the original value to a new location (so it won't be modified)
118 ;; and returns that location to the original expression
119 (let ((x (subast1 ast)))
121 (error "assignment target must be a variable")
122 (let* ((def-var (ref-def-var x))
123 (result (alloc-value (def-variable-type def-var) #f (bb-name bb))))
124 (move-value (def-variable-value def-var) result)
127 ;; TODO instead of carrying types around, use the length instead, or even better, just pass the value-bytes, and calculate the length as needed
128 (define (extend value type)
129 ;; literals must be extended with literal 0s, while variables must be
130 ;; extended with byte cells
131 (let* ((bytes (value-bytes value))
132 (lit? (byte-lit? (car bytes))))
133 (let loop ((rev-bytes (reverse bytes))
134 (n (max 0 (- (type->bytes type) (length bytes)))))
136 (new-value (reverse rev-bytes))
137 (loop (cons (new-byte-lit 0) ;; TODO used to extend with empty byte cells when it expanded a variable. caused weird bugs.
141 (define (program ast)
142 (let loop ((asts (ast-subasts ast)))
143 (if (not (null? asts))
144 (let ((ast (car asts)))
145 (if (null? (cdr asts))
146 (let ((value (expression ast)))
147 (return-with-no-new-bb value))
150 (loop (cdr asts))))))))
152 (define (toplevel ast)
153 (cond ((def-variable? ast)
155 ((def-procedure? ast)
160 (define (def-variable ast) ;; FOO set the fun for each byte-cell
161 (let ((subasts (ast-subasts ast)))
162 (if (not (null? subasts)) ; if needed, set the variable
163 (let ((value (expression (subast1 ast))))
164 (let ((ext-value (extend value (def-variable-type ast))))
165 (move-value value (def-variable-value ast)))))))
167 ;; resolve the C gotos by setting the appropriate successor to their bb
168 (define (resolve-all-gotos start table)
169 (let loop ((start start)
170 (visited (new-empty-set)))
171 (if (not (set-member? visited start)) ; not visited
174 (if (and (eq? (instr-id x) 'goto)
175 (instr-dst x)) ; unresolved label
176 (let ((target (assoc (instr-dst x) table))) ;; TODO use a set, but not urgent, not a bottleneck
178 (begin (add-succ start (cdr target))
179 (instr-dst-set! x #f))
180 (error "invalid goto target" (instr-dst x))))))
181 (bb-rev-instrs start))
182 (for-each (lambda (x)
183 (set-add! visited start)
185 (bb-succs start))))))
187 (define (def-procedure ast) ;; FOO set the fun for the parameters, and maybe also return value
188 (set! current-def-proc-bb-id 0)
189 (set! current-def-proc ast)
192 (def-procedure-entry-set! ast entry)
194 (for-each statement (ast-subasts ast))
195 (return-with-no-new-bb ast)
196 (set! current-def-proc #f)
197 (resolve-all-gotos entry (list-named-bbs entry))
200 ;; returns a list of all named bbs in the successor-tree of a given bb
201 (define (list-named-bbs start)
202 (let ((visited (new-empty-set)))
203 (let loop ((start start) ;; TODO not really a loop, it's tree recursion
205 (if (set-member? visited start)
210 (set-add! visited start)
213 (if (bb-label-name start)
214 (cons (cons (bb-label-name start) start) succs)
217 (define (statement ast)
218 (cond ((def-variable? ast) (def-variable ast))
219 ((block? ast) (block ast))
220 ((return? ast) (return ast))
221 ((if? ast) (if (null? (cddr (ast-subasts ast)))
224 ((while? ast) (while ast))
225 ((do-while? ast) (do-while ast))
226 ((for? ast) (for ast))
227 ((switch? ast) (switch ast))
228 ((break? ast) (break ast))
229 ((continue? ast) (continue ast))
230 ((goto? ast) (goto ast))
231 (else (expression ast))))
234 (if (block-name ast) ; named block ?
235 (begin (let ((new (new-bb)))
238 (bb-label-name-set! bb (block-name ast))))
239 (for-each statement (ast-subasts ast)))
241 (define (move from to)
242 (emit (new-instr 'move from #f to)))
244 (define (move-value from to)
245 (let loop ((from (value-bytes from))
246 (to (value-bytes to)))
247 (cond ((null? to)) ; done, we truncate the rest
248 ((null? from) ; promote the value by padding
249 (move (new-byte-lit 0) (car to))
250 (loop from (cdr to)))
252 (move (car from) (car to))
253 (loop (cdr from) (cdr to))))))
255 (define (return-with-no-new-bb def-proc)
256 (emit (new-return-instr def-proc)))
259 (if (null? (ast-subasts ast))
260 (return-with-no-new-bb current-def-proc)
261 (let ((value (expression (subast1 ast))))
262 (let ((ext-value (extend value (def-procedure-type current-def-proc))))
263 (move-value value (def-procedure-value current-def-proc))
264 (return-with-no-new-bb current-def-proc))))
268 (let* ((bb-join (new-bb))
270 (test-expression (subast1 ast) bb-then bb-join)
272 (statement (subast2 ast))
277 (let* ((bb-join (new-bb))
280 (test-expression (subast1 ast) bb-then bb-else)
282 (statement (subast2 ast))
285 (statement (subast3 ast))
290 (let* ((bb-cont (new-bb))
293 (push-continue bb-cont)
297 (test-expression (subast1 ast) bb-body bb-exit)
299 (statement (subast2 ast))
305 (define (do-while ast)
306 (let* ((bb-body (new-bb))
309 (push-continue bb-cont)
313 (statement (subast1 ast))
316 (test-expression (subast2 ast) bb-body bb-exit)
322 (let* ((bb-loop (new-bb))
326 (statement (subast1 ast))
328 (push-continue bb-cont)
331 (test-expression (subast2 ast) bb-body bb-exit)
333 (statement (subast4 ast))
336 (statement (subast3 ast))
342 ;; switchs with branch tables
343 ;; since offsets are calculated using one byte, switches are limited to
344 ;; 60 cases or so (slightly below 64)
346 (let* ((var (subast1 ast))
350 (let loop ((asts (cdr (ast-subasts ast))) ; car is the tested variable
351 (bbs '()) ; first bb of each case
352 (end-bbs '()) ; last bb of each case
353 (cases '())) ; case labels
354 (if (not (null? asts))
359 (if (or (null? (bb-succs case-bb))
360 (not (bb-label-name (car (bb-succs case-bb)))))
361 ;; the first block inside the body of a switch might not
362 ;; have a label, in which case it ill be skipped
363 ;; to have a valid CFG, it must still contain an instruction
364 (begin (gen-goto exit-bb)
365 (loop (cdr asts) bbs end-bbs cases))
369 ;; blocks create their own bb, which contains the case label
370 (cons (bb-label-name (car (bb-succs case-bb)))
372 (let ((bbs (reverse bbs))
373 (end-bbs (reverse end-bbs))
374 (cases (reverse cases))
376 ;; add the case names to the bb names
377 (for-each ;; TODO do it for all named bbs, not just switch (but, since the name is on the successor, might be lost)
380 (bb-label (car (bb-succs bb))) 2
382 (string-append (symbol->string (bb-name bb))
386 (symbol->string case)
389 (symbol->string (car case))
390 (number->string (cadr case))))))))
393 ;; handle fall-throughs
396 (let ((case-bb (list-ref end-bbs i)))
397 (if (null? (bb-succs case-bb))
400 (gen-goto (if (= i (- l 1)) ; last bb
402 (list-ref bbs (+ i 1))))))))
404 (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
405 ;; cases are lists (case n) and we want the numbers
406 (cases (map cadr (keep list? cases)))
407 (case-max (foldl max 0 cases))
408 (case-min (foldl min case-max cases))
409 (n-entries (+ (- case-max case-min) 1)))
410 (if default (error "default is not supported with switch"))
414 (cond ((pos-in-list (+ i case-min) cases)
415 => (lambda (j) (list-ref bbs j)))
416 ;; no label, jump to the exit TODO would jump to default, eventually
419 ;; the branch-table virtual instruction takes the byte to check
420 ;; to choose the branch
424 ;; the cases now start from 0, so we might have to
425 ;; adjust the checked variable
430 (let* ((op (operation? '(six.x-y)))
432 (list var (new-literal 'int8
436 (expr-type-set! ast ((op-type-rule op) ast))
438 ;; working space to calculate addresses
439 (new-byte-cell #f (bb-name bb))
444 ;; ;; naive switch with if cascade ;; TODO revert to that if there's a case we can't handle with branch tables
445 ;; (define (switch ast)
446 ;; (let* ((var (subast1 ast))
450 ;; (exit-bb (new-bb))
451 ;; (prev-bb decision-bb))
452 ;; (push-break exit-bb)
453 ;; (for-each (lambda (x) ; generate each case
454 ;; (in (new-bb)) ; this bb will be given the name of the case
455 ;; (add-succ decision-bb bb)
456 ;; ;; if the previous case didn't end in a break, fall through
457 ;; (if (null? (bb-succs prev-bb))
463 ;; (set! prev-bb bb))
464 ;; (cdr (ast-subasts ast)))
465 ;; (if (null? (bb-succs prev-bb)) ; if the last case didn't end in a break, fall through to the exit
466 ;; (gen-goto exit-bb))
467 ;; (bb-succs-set! decision-bb (reverse (bb-succs decision-bb))) ; preserving the order is important in the absence of break
468 ;; (set! case-list (list-named-bbs decision-bb))
469 ;; (set! default (keep (lambda (x) (eq? (car x) 'default))
470 ;; (list-named-bbs decision-bb)))
471 ;; (set! case-list (keep (lambda (x) (and (list? (car x))
472 ;; (eq? (caar x) 'case)))
474 ;; (bb-succs-set! decision-bb '()) ; now that we have the list of cases we don't need the successors anymore
475 ;; (let loop ((case-list case-list)
476 ;; (decision-bb decision-bb))
478 ;; (if (not (null? case-list))
479 ;; (let* ((next-bb (new-bb))
480 ;; (curr-case (car case-list))
481 ;; (curr-case-id (cadar curr-case))
482 ;; (curr-case-bb (cdr curr-case)))
483 ;; (emit (new-instr 'x==y
484 ;; (car (value-bytes (expression var)))
485 ;; (new-byte-lit curr-case-id) #f))
486 ;; (add-succ bb next-bb) ; if false, keep looking
487 ;; (add-succ bb curr-case-bb) ; if true, go to the case
488 ;; (loop (cdr case-list)
490 ;; (gen-goto (if (not (null? default))
497 (gen-goto (car break-stack)))
499 (define (continue ast)
500 (gen-goto (car continue-stack)))
502 ;; generates a goto with a target label. once the current function definition
503 ;; is over, all these labels are resolved. therefore, we don't have any gotos
504 ;; that jump from a function to another
506 (emit (new-instr 'goto #f #f (subast1 ast))))
508 (define (gen-goto dest)
509 (if (null? (bb-succs bb))
510 ;; since this is an unconditional goto, we want only one
511 (begin (add-succ bb dest)
512 (emit (new-instr 'goto #f #f #f)))))
514 (define (test-expression ast bb-true bb-false)
516 (define (test-byte id byte1 byte2 bb-true bb-false)
517 (define (test-lit id x y)
522 (else (error "invalid test")))
525 (cond ((and (byte-lit? byte1) (byte-lit? byte2))
526 (if (test-lit id (byte-lit-val byte1) (byte-lit-val byte2))
528 (gen-goto bb-false)))
530 ;; since we cons each new successor at the front, true has to be
532 (add-succ bb bb-false)
533 (add-succ bb bb-true)
534 (emit (new-instr id byte1 byte2 #f)))
541 (else (error "invalid test")))))
542 (add-succ bb bb-false)
543 (add-succ bb bb-true)
544 (emit (new-instr id byte2 byte1 #f))))
546 (add-succ bb bb-false)
547 (add-succ bb bb-true)
548 (emit (new-instr id byte1 byte2 #f)))))
550 (define (test-value id value1 value2 bb-true bb-false)
551 (let loop ((bytes1 (value-bytes value1)) ; lsb first
552 (bytes2 (value-bytes value2))
555 (if (not (and (null? bytes1) (null? bytes2)))
556 ;; note: won't work with signed types, as the padding is done
558 (loop (if (null? bytes1) bytes1 (cdr bytes1))
559 (if (null? bytes2) bytes2 (cdr bytes2))
560 (cons (if (null? bytes1) (new-byte-lit 0) (car bytes1))
562 (cons (if (null? bytes2) (new-byte-lit 0) (car bytes2))
564 ;; now so the test itself, using the padded values
565 ;; the comparisons are done msb-first, for < and >
566 (let ((padded1 (reverse padded1))
567 (padded2 (reverse padded2)))
569 ((x==y) ; unlike < and >, must check all bytes, so is simpler
570 (let loop2 ((bytes1 padded1) ;; TODO ior the xors
572 (let ((byte1 (car bytes1))
573 (byte2 (car bytes2)))
574 (if (null? (cdr bytes1))
575 (test-byte 'x==y byte1 byte2 bb-true bb-false)
576 (let ((bb-true2 (new-bb)))
577 (test-byte 'x==y byte1 byte2 bb-true2 bb-false)
579 (loop2 (cdr bytes1) (cdr bytes2)))))))
582 (if (= (length padded1) 1)
583 (test-byte id (car padded1) (car padded2)
585 ;; more than one byte, we subtract, then see if we had
587 (let ((scratch (new-byte-cell #f (bb-name bb))))
589 ;; our values might contain literal bytes and sub and
590 ;; subb can't have literals in their first argument,
591 ;; allocate it somewhere if needed
592 (if (and (foldl (lambda (acc new)
593 (or acc (byte-lit? new)))
596 (let ((tmp (alloc-value
597 (bytes->type (length padded2))
599 (move-value (new-value padded2) tmp)
600 (set! padded2 (value-bytes tmp))))
601 (if (and (foldl (lambda (acc new) ;; TODO abstract both cases
602 (or acc (byte-lit? new)))
605 (let ((tmp (alloc-value
606 (bytes->type (length padded1))
608 (move-value (new-value padded1) tmp)
609 (set! padded1 (value-bytes tmp))))
611 (let loop ((bytes1 padded1)
614 (if (not (null? bytes1))
615 (begin ; subtract y from x
618 ((x<y) (new-instr (if borrow? 'subb 'sub)
619 (car bytes1) (car bytes2)
621 ((x>y) (new-instr (if borrow? 'subb 'sub)
622 (car bytes2) (car bytes1)
624 (loop (cdr bytes1) (cdr bytes2) #t))))
626 (add-succ bb bb-false)
627 (add-succ bb bb-true)
628 (emit (new-instr 'branch-if-carry scratch #f #f))))))))))
630 (define (test-relation id x y bb-true bb-false)
631 (cond ((and (literal? x) (not (literal? y)))
632 ;; literals must be in the last argument for code generation
633 ;; flip the relation if needed
634 (test-relation (case id
635 ((x==y x!=y) id) ; commutative, no change
640 (else (error "relation error")))
645 ((assq id '((x!=y . x==y) (x<=y . x>y) (x>=y . x<y)))
646 ;; flip the destination blocks to have a simpler comparison
648 (lambda (z) (test-relation (cdr z) x y bb-false bb-true)))
651 ;; ' ;; TODO use these special cases, but fall back on the current implementation for default
654 ;; (cond ((and (literal? y) (= (literal-val y) 0))
655 ;; (test-zero x bb-true bb-false))
657 ;; (test-eq-lit x (literal-val y) bb-true bb-false))
659 ;; (error "unhandled case"))))
661 ;; (cond ((and (literal? y) (= (literal-val y) 0))
662 ;; (test-negative x bb-true bb-false))
664 ;; (error "unhandled case"))))
666 ;; (cond ((and (literal? y) (= (literal-val y) 0))
667 ;; (test-positive x bb-true bb-false))
669 ;; (error "unhandled case"))))
671 ;; (error "unexpected operator")))
673 (let* ((value1 (expression x))
674 (value2 (expression y)))
675 (test-value id value1 value2 bb-true bb-false))
678 (define (test-zero ast bb-true bb-false)
681 (let ((type (expr-type ast))
682 (value (expression ast)))
683 ;; since nonzero is true, we must swap the destinations to use ==
684 (test-value 'x==y value (int->value 0 type) bb-false bb-true)))
687 (let* ((op (oper-op ast))
691 (test-zero (subast1 ast) bb-false bb-true))
693 (let ((bb-true2 (new-bb)))
694 (test-zero (subast1 ast) bb-true2 bb-false)
696 (test-zero (subast2 ast) bb-true bb-false)))
698 (let ((bb-false2 (new-bb)))
699 (test-zero (subast1 ast) bb-true bb-false2)
701 (test-zero (subast2 ast) bb-true bb-false)))
702 ((x==y x!=y x<y x>y x<=y x>=y)
711 (test-zero ast bb-true bb-false))
713 (define (expression ast)
715 (cond ((literal? ast) (literal ast))
716 ((ref? ast) (ref ast))
717 ((oper? ast) (oper ast))
718 ((call? ast) (call ast))
719 (else (error "unexpected ast" ast)))))
720 (do-delayed-post-incdec)
723 (define (literal ast)
724 (let ((val (literal-val ast)))
725 (int->value val (expr-type ast))))
728 (let* ((def-var (ref-def-var ast))
729 (value (def-variable-value def-var)))
732 (define (add-sub id value1 value2 result)
733 (let loop ((bytes1 (value-bytes value1)) ; car is lsb
734 (bytes2 (value-bytes value2))
735 (bytes3 (value-bytes result))
736 (ignore-carry-borrow? #t))
737 (if (not (null? bytes3))
738 ;; if we would add or subtract 0 and not use the carry, just move
740 (let ((b1 (car bytes1)) (b2 (car bytes2)) (b3 (car bytes3)))
741 (if (and (byte-lit? b2)
742 (= (byte-lit-val b2) 0)
743 (or (eq? id 'add) (eq? id 'sub)))
745 (emit (new-instr (if ignore-carry-borrow?
746 (case id ((x+y) 'add) ((x-y) 'sub))
747 (case id ((x+y) 'addc) ((x-y) 'subb)))
749 (loop (cdr bytes1) (cdr bytes2) (cdr bytes3) #f))
752 (define (mul x y type result)
753 (let* ((value-x (expression x))
754 (value-y (expression y))
755 (bytes-x (value-bytes value-x))
756 (bytes-y (value-bytes value-y))
757 (lx (length bytes-x))
758 (ly (length bytes-y)))
759 ;; if this a multiplication by 2 or 4, we use additions instead
760 ;; at this point, only y (or both x and y) can contain a literal
762 (byte-lit? (car bytes-y))
763 (let ((v (byte-lit-val (car bytes-y))))
764 (or (= v 2) (= v 4))))
765 (case (byte-lit-val (car bytes-y))
766 ((2) (add-sub 'x+y value-x value-x result)) ; simple addition
767 ((4) (let ((tmp (alloc-value (bytes->type
768 (length (value-bytes result)))
770 (add-sub 'x+y value-x value-x tmp)
771 (add-sub 'x+y tmp tmp result))))
772 ;; if not, we have to do it the long way
774 ;; finds the appropriate multiplication routine (depending on the
775 ;; length of each argument) and turns the multiplication into a
776 ;; call to the routine
777 ;; the arguments must be the asts of the 2 arguments (x and y) and
778 ;; the type of the returned value, since these are what are
779 ;; expected by the call function
781 ;; to avoid code duplication (i.e. habing a routine for 8 by 16
782 ;; multplication and one for 16 by 8), the longest operand goes first
791 (string->symbol ; mul8_8, mul8_16, etc
792 ;; for now, only unsigned multiplications are supported
793 (string-append "__mul"
794 (number->string (* lx 8)) "_"
795 (number->string (* ly 8))))
799 (define (mod x y result)
800 (let* ((bytes1 (value-bytes x))
801 (bytes2 (value-bytes y))
802 (bytes3 (value-bytes result))
804 ;; if y is a literal and a power of 2, we can do a bitwise and
805 (if (and (byte-lit? y0)
806 (let ((x (/ (log (value->int y)) (log 2))))
808 ;; bitwise and with y - 1
809 (begin (let* ((l (bytes->type (length bytes2)))
810 (tmp (alloc-value l #f (bb-name bb))))
811 (move-value (int->value (- (value->int y) 1)
812 (bytes->type (length bytes2)))
814 (bitwise 'x&y x tmp result)))
815 ;; TODO for the general case, try to optimise the case where division and modulo are used together, since they are calculated together
816 (error "modulo is only supported for powers of 2"))
819 (define (shift id x y type result)
820 (let ((bytes1 (value-bytes (extend (expression x) type)))
821 (bytes2 (value-bytes (extend (expression y) type)))
822 (bytes3 (value-bytes result)))
823 ;; if the second argument is a literal and a multiple of 8, we can simply
824 ;; move the bytes around
825 (let ((y0 (car bytes2)))
826 (if (and (byte-lit? y0) (= (modulo (byte-lit-val y0) 8) 0))
827 ;; uses only the first byte, but shifting by 255 should be enough
828 (let ((n (/ (byte-lit-val y0) 8))
829 (l (length bytes1))) ; same length for x and result
836 (new-byte-lit 0) ; padding
839 (loop (+ i 1) (if (< i n) x (cdr x))))
841 (move (if (<= l (+ i n))
843 (list-ref x (+ i n)))
849 (string-append "__sh"
850 (case id ((x<<y) "l") ((x>>y) "r"))
851 (number->string (* 8 (length bytes1)))))
855 ;; bitwise and, or, xor
856 ;; TODO similar to add-sub and probably others, abstract multi-byte ops
857 ;; TODO use bit set, clear and toggle for some shortcuts
858 (define (bitwise id value1 value2 result)
859 (let loop ((bytes1 (value-bytes value1))
860 (bytes2 (value-bytes value2))
861 (bytes3 (value-bytes result)))
862 (if (not (null? bytes3)) ;; TODO check for cases like or 0, or ff, and 0, and ff, ...
864 (emit (new-instr (case id ((x&y) 'and) ((|x\|y|) 'ior) ((x^y) 'xor))
865 (car bytes1) (car bytes2) (car bytes3)))
866 (loop (cdr bytes1) (cdr bytes2) (cdr bytes3)))
869 (define (bitwise-negation x result)
870 (let loop ((bytes1 (value-bytes x))
871 (bytes2 (value-bytes result)))
872 (if (not (null? bytes2))
873 (begin (emit (new-instr 'not (car bytes1) #f (car bytes2)))
874 (loop (cdr bytes1) (cdr bytes2))))))
876 (define (do-delayed-post-incdec)
877 (if (not (null? delayed-post-incdec))
878 (let* ((ast (car delayed-post-incdec))
879 (type (expr-type ast))
882 (set! delayed-post-incdec (cdr delayed-post-incdec))
883 (let ((x (subast1 ast)))
885 (error "assignment target must be a variable"))
886 (let ((result (def-variable-value (ref-def-var x))))
887 ;; clobbers the original value, which is fine, since it
888 ;; was moved somewhere else for the expression
889 (add-sub (if (eq? id 'x++) 'x+y 'x-y)
893 (do-delayed-post-incdec))))
895 ;; calculates an address in an array by adding the base pointer and the offset
896 ;; and puts the answer in FSR0 so that changes to INDF0 change the array
898 (define (calculate-address ast)
899 ;; if we have a special FSR variable, no need to calculate the address as
900 ;; it is already in the register
901 (let ((base-name (array-base-name ast))
902 (index? (eq? (op-id (oper-op ast)) 'index)))
903 (if (not (and base-name
904 (memq base-name fsr-variables)))
905 (let ((base (expression (subast1 ast)))
906 ;; NB: actual addresses are 12 bits, not 16
907 (address (new-value (list (get-register FSR0L)
908 (get-register FSR0H)))))
910 ;; we pad up to int16, since it is the size of the addresses
911 (let ((value1 (extend base 'int16))
912 (value2 (extend (expression (subast2 ast)) 'int16)))
913 (add-sub 'x+y value1 value2 address))
914 ;; no offset with simple dereference
915 (move-value base address)))
916 (error "You used the array index syntax with a FSR variable, didn't you? I told you not to."))))
918 (define (array-base-name ast)
919 ;; returns #f if the lhs is not a direct variable reference
920 ;; eg : *x++ ; (x+y)* ; ...
921 (let ((lhs (subast1 ast)))
923 (def-id (ref-def-var lhs)))))
925 (define (get-indf base-name)
926 ;; INDF0 is not here, since it's already used for regular array accesses
927 (if (eq? base-name 'SIXPIC_FSR1)
928 (new-value (list (get-register INDF1)))
929 (new-value (list (get-register INDF2)))))
932 (let* ((type (expr-type ast))
936 (define (arith-op id x y value-x value-y)
937 ;; since code generation does not accept literals as first
938 ;; arguments unless both arguments are, if this is the
939 ;; case, we either have to swap the arguments (if
940 ;; possible) or allocate the argument somewhere
941 (if (and (literal? x) (not (literal? y)))
942 (if (memq id '(x+y x*y x&y |x\|y| x^y))
943 ;; the operator is commutative, we can swap the args
945 (set! value-x value-y)
947 ;; the operator is not commutative, we have to
948 ;; allocate the first argument somewhere
949 (let ((dest (alloc-value (expr-type x) #f (bb-name bb))))
950 (move-value value-x dest)
951 (set! value-x dest))))
952 (let ((result (alloc-value type #f (bb-name bb))))
954 ((x+y x-y) (add-sub id value-x value-y result))
955 ((x*y) (mul x y type result))
956 ((x/y) (error "division not implemented yet")) ;; TODO optimize for powers of 2
957 ((x%y) (mod value-x value-y result))
958 ((x&y |x\|y| x^y) (bitwise id value-x value-y result))
959 ((x>>y x<<y) (shift id x y type result)))))
965 (let ((x (extend (expression (subast1 ast))
967 (result (alloc-value type #f (bb-name bb))))
973 ((~x) (bitwise-negation x result)))
976 (let ((x (subast1 ast)))
978 (error "assignment target must be a variable"))
979 (let ((result (def-variable-value (ref-def-var x))))
980 (add-sub (if (eq? id '++x) 'x+y 'x-y)
986 (let ((x (subast1 ast)))
988 (error "assignment target must be a variable"))
989 ;; push-delayed-post-incdec moves the original value
990 ;; somewhere else, and returns that location
991 (push-delayed-post-incdec ast)))
993 ;; if it's a FSR variable, no adress to set
994 (let ((base-name (array-base-name ast)))
995 (if (and (ref? (subast1 ast)) ; do we have a FSR variable ?
997 (memq base-name fsr-variables))
999 (begin (calculate-address ast)
1000 (new-value (list (get-register INDF0)))))))
1002 (error "unary operation error" id))))
1006 ((x+y x-y x*y x/y x%y x&y |x\|y| x^y x>>y x<<y)
1007 (let* ((x (subast1 ast))
1009 (let* ((value-x (extend (expression x) type))
1010 (value-y (extend (expression y) type)))
1011 (arith-op id x y value-x value-y))))
1013 (let* ((x (subast1 ast))
1015 (value-y (expression y)))
1017 ;; lhs is a variable
1019 (let ((ext-value-y (extend value-y type)))
1020 (let ((result (def-variable-value (ref-def-var x))))
1021 (move-value value-y result)
1023 ;; lhs is a pointer dereference
1024 ((and (oper? x) (eq? (op-id (oper-op x)) '*x))
1025 (let ((base-name (array-base-name x))
1026 (val (car (value-bytes value-y))))
1027 (if (and (ref? (subast1 x))
1029 (memq base-name fsr-variables))
1030 (move val (car (value-bytes (get-indf base-name))))
1031 (begin (calculate-address x)
1032 (move val (get-register INDF0))))))
1033 ;; lhs is an indexed array access
1034 ((and (oper? x) (eq? (op-id (oper-op x)) 'index))
1035 ;; note: this will throw an error if SIXPIC_FSR{1,2} is
1036 ;; used. this is by design, as it would clobber the value
1037 ;; in the FSR registers, which goes against their purpose
1038 ;; of storing a user-chosen value
1039 (calculate-address x)
1040 ;; this section of memory is a byte array, only the lsb
1042 (move (car (value-bytes value-y)) (get-register INDF0)))
1043 (else (error "assignment target must be a variable or an array slot")))))
1045 ;; note: throws an error if given SIXPIC_FSR{1,2}, see above
1046 (calculate-address ast)
1047 (new-value (list (get-register INDF0))))
1048 ((x+=y x-=y x*=y x/=y x%=y x&=y |x\|=y| x^=y x>>=y x<<=y)
1049 (let* ((x (subast1 ast))
1051 (value-x (extend (expression x) type))
1052 (value-y (extend (expression y) type)))
1053 (move-value (arith-op (case id
1064 x y value-x value-y)
1067 ((x==y x!=y x>y x>=y x<y x<=y x&&y |x\|\|y|)
1072 (result (alloc-value type #f (bb-name bb))))
1074 (move-value (int->value 1 type) result)
1077 (move-value (int->value 0 type) result)
1080 (test-expression ast bb-true bb-false)
1084 (error "binary operation error" id))))
1091 (result (alloc-value type #f (bb-name bb))))
1093 (move-value (expression (subast2 ast)) result)
1096 (move-value (expression (subast3 ast)) result)
1099 (test-expression (subast1 ast) bb-true bb-false)
1103 ;; generates the cfg for a predefined routine and adds it to the current cfg
1104 (define (include-predefined-routine proc)
1105 (define (get-bytes var)
1106 (value-bytes (def-variable-value var)))
1107 (let ((old-proc current-def-proc) ; if we were already defining a procedure, save it
1108 (old-bb-no current-def-proc-bb-id)
1110 (params (def-procedure-params proc))
1111 (value (def-procedure-value proc))
1113 (entry (begin (set! current-def-proc proc)
1114 (set! current-def-proc-bb-id 0)
1115 (new-bb)))) ;; TODO insipired from def-procedure, abstract
1116 (def-procedure-entry-set! proc entry)
1121 (let* ((x (get-bytes (car params)))
1124 (z0 (car (value-bytes value))))
1125 ;; TODO use postinc/dec and co
1126 (emit (new-instr 'tblrd x0 x1 #f))
1127 (move (get-register TABLAT) z0)))
1130 (let ((x (car params))
1132 (z (value-bytes value)))
1133 ;; TODO implement literal multiplication in the simulator
1134 (emit (new-instr 'mul (car (get-bytes x)) (car (get-bytes y)) #f))
1135 (move (get-register PRODL) (car z)))) ; lsb
1138 (let* ((x (get-bytes (car params)))
1141 (y (get-bytes (cadr params)))
1143 (z (value-bytes value))
1146 (emit (new-instr 'mul y0 x1 #f))
1147 (move (get-register PRODL) z1)
1149 (emit (new-instr 'mul y0 x0 #f))
1150 (move (get-register PRODL) z0)
1151 (emit (new-instr 'add (get-register PRODH) z1 z1))))
1154 (let* ((x (get-bytes (car params)))
1157 (y (get-bytes (cadr params)))
1160 (z (value-bytes value))
1164 (emit (new-instr 'mul x0 y0 #f))
1165 (move (get-register PRODH) z1)
1166 (move (get-register PRODL) z0)
1168 (emit (new-instr 'mul x0 y1 #f))
1169 (emit (new-instr 'add (get-register PRODL) z1 z1))
1171 (emit (new-instr 'mul x1 y0 #f))
1172 (emit (new-instr 'add (get-register PRODL) z1 z1))))
1175 (let* ((x (get-bytes (car params)))
1180 (y (get-bytes (cadr params)))
1183 (z (value-bytes value))
1189 (emit (new-instr 'mul x0 y0 #f))
1190 (move (get-register PRODH) z1)
1191 (move (get-register PRODL) z0)
1193 (emit (new-instr 'mul x1 y1 #f))
1194 (move (get-register PRODH) z3)
1195 (move (get-register PRODL) z2)
1197 (emit (new-instr 'mul x1 y0 #f))
1198 (emit (new-instr 'add (get-register PRODL) z1 z1))
1199 (emit (new-instr 'addc (get-register PRODH) z2 z2))
1200 (emit (new-instr 'addc z3 (new-byte-lit 0) z3))
1202 (emit (new-instr 'mul x0 y1 #f))
1203 (emit (new-instr 'add (get-register PRODL) z1 z1))
1204 (emit (new-instr 'addc (get-register PRODH) z2 z2))
1205 (emit (new-instr 'addc z3 (new-byte-lit 0) z3))
1207 (emit (new-instr 'mul x2 y0 #f))
1208 (emit (new-instr 'add (get-register PRODL) z2 z2))
1209 (emit (new-instr 'addc (get-register PRODH) z3 z3))
1211 (emit (new-instr 'mul x2 y1 #f))
1212 (emit (new-instr 'add (get-register PRODL) z3 z3))
1214 (emit (new-instr 'mul x3 y0 #f))
1215 (emit (new-instr 'add (get-register PRODL) z3 z3))))
1217 ((__shl8 __shr8 __shl16 __shr16 __shl32 __shr32)
1218 (let* ((id (symbol->string id))
1219 (left-shift? (eq? (string-ref id 4) #\l))
1220 (x (def-variable-value (car params)))
1221 (y (def-variable-value (cadr params)))
1222 (y0 (car (value-bytes y))) ; shift by 255 is enough
1223 (bytes-z (value-bytes value))
1226 (after-bb (new-bb)))
1227 (move-value x value)
1228 (gen-goto start-bb) ; fall through to the loop
1230 ;; if we'd shift of 0, we're done
1231 (add-succ bb loop-bb) ; false
1232 (add-succ bb after-bb) ; true
1233 (emit (new-instr 'x==y y0 (new-byte-lit 0) #f))
1235 ;; clear the carry, to avoid reinserting it in the register
1236 (emit (new-instr 'clear
1237 (get-register STATUS)
1240 ;; shift for each byte, since it's a rotation using the carry,
1241 ;; what goes out from the low bytes gets into the high bytes
1242 (for-each (lambda (b)
1243 (emit (new-instr (if left-shift? 'shl 'shr)
1245 (if left-shift? bytes-z (reverse bytes-z)))
1246 (emit (new-instr 'sub y0 (new-byte-lit 1) y0))
1249 (return-with-no-new-bb proc)
1250 (set! current-def-proc old-proc)
1251 (set! current-def-proc-bb-id old-bb-no)
1252 (resolve-all-gotos entry (list-named-bbs entry))
1256 (let* ((def-proc (call-def-proc ast))
1257 (arguments (ast-subasts ast))
1258 (parameters (def-procedure-params def-proc)))
1259 (if (and (memq (def-id def-proc) predefined-routines)
1260 (not (def-procedure-entry def-proc)))
1261 ;; it's the first time we encounter this predefined routine, generate
1262 ;; the corresponding cfg
1263 (include-predefined-routine def-proc))
1264 ;; argument number check
1265 (if (not (= (length arguments) (length parameters))) ;; TODO check at parse time ?
1266 (error (string-append "wrong number of arguments given to function "
1267 (symbol->string (def-id def-proc)) ": "
1268 (number->string (length arguments)) " given, "
1269 (number->string (length parameters))
1271 (for-each (lambda (ast def-var)
1272 (let ((value (expression ast)))
1273 (let ((ext-value (extend value (def-variable-type def-var))))
1274 (move-value value (def-variable-value def-var)))))
1277 (emit (new-call-instr def-proc))
1278 (let ((value (def-procedure-value def-proc)))
1280 (alloc-value (def-procedure-type def-proc) #f (bb-name bb))))
1281 (move-value value result)
1284 ;; call to a predefined routine, a simple wrapper to an ordinary call
1285 ;; name is a symbol, args is a list of the arguments
1286 (define (routine-call name args type)
1287 (cond ((memp (lambda (x) (eq? (def-id x) name))
1289 => (lambda (x) (call (new-call args type (car x)))))
1290 (else (error "unknown routine: " name))))
1296 (define (print-cfg-bbs cfg)
1297 (for-each (lambda (bb)
1298 (pp (list "BB:" (bb-name bb)
1299 "SUCCS" (map bb-name (bb-succs bb))
1300 "PREDS" (map bb-name (bb-preds bb))
1301 (cond ((null? (bb-rev-instrs bb)) "EMPTY")
1302 ((and (null? (cdr (bb-rev-instrs bb)))
1303 (eq? (instr-id (car (bb-rev-instrs bb))) 'goto)) "SINGLE GOTO")