1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
|
(in-package "XLSCMP")
#|
clean up, reduce use of global/special variables
clean up, separate out initialisation code generation
implement large case with hashtable?
need to test %catch-tagbody with changes
think about implementation of mvcall -- alternative to %mv-collect
simplifyer rules: drop unused variables from continuations?
be carefull about unused vs real multiple values (for dropping no side eff.)
constant folded version of nth-value?
push code generaotr test into simplifier?
need to recheck calling, test calling speed
|#
#|
Value registers of zero are used to communicate that values should
(only) be stored in the multiple value array. This should work OK since
the zero register is reserved for the real final value continuation.
Continuations are represented in two parts. Part lives in the
continuation stack, part on the value stack. The value stack part may
be shared -- this is true for the local calls generated by things like
unwind-protect. These local functions cannot overlay their call frame
on a tail call, since that would clobber the part on the value stack.
The byte code interpreter checks for this possibility by checking
whether the current call stack part of the framt is identical to the
one for the next continuation on the stack.
|#
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;
;;;;; Code Generator
;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *code*)
(defvar *literals*)
(defvar *functions*)
(defvar *labels*)
(defvar *registers*)
(defvar *constants*)
(defvar *protected-continuations*)
(defun generate-code (pieces)
(let ((*code* nil)
(*literals* nil)
(*functions* nil)
(*labels* nil))
(initialize-functions pieces)
(dolist (p pieces)
(let ((label (make-label (first p)))
(code (second p)))
(push-label (first p))
(generate-lambda-code label code)))
(peephole-optimize (list (reverse *code*)
(reverse *literals*)
(get-function-label (first (first pieces)))
nil
(nreverse (mapcar #'second *functions*)))
(get-function-labels))))
(defun generate-lambda-code (label n)
(let ((*registers* nil)
(*constants* nil)
(*protected-continuations* nil))
(assign-registers n)
;;**** clean this up!!
(let* ((consts (nreverse *constants*))
(ainfo (lambda-node-lambda-list n))
(nc (length consts))
(nv (lambda-list-num-variables ainfo))
(nr (count-registers))
(nreq (first ainfo))
(nopt (second ainfo))
(odef (coerce (third ainfo) 'vector))
(allow-keys (fourth ainfo))
(rest (sixth ainfo)))
(cond
((and (= nopt 0) (not allow-keys) (not rest))
(push-instruction `(%initialize-0 ,nreq ,nc ,@consts ,(- nr nv nc))))
((and (not allow-keys) (not rest))
(let ((ol (add-literal (make-constant-node `(quote ,odef)))))
(push-instruction
`(%initialize 1 ,nreq ,nopt ,ol ,nc ,@consts ,(- nr nv nc)))))
((not allow-keys)
(if (< 0 nopt)
(let ((ol (add-literal (make-constant-node `(quote ,odef)))))
(push-instruction
`(%initialize 2 ,nreq ,nopt ,ol ,nc ,@consts ,(- nr nv nc))))
(push-instruction
`(%initialize 2 ,nreq ,nopt ,nc ,@consts ,(- nr nv nc)))))
(t
(let* ((kdefs (fifth ainfo))
(allow-other-keys (seventh ainfo))
(ksyms (eighth ainfo))
(kdl (add-literal (make-constant-node `(quote ,kdefs))))
(ksl (add-literal (make-constant-node `(quote ,ksyms))))
(aok (if allow-other-keys 1 0))
(r (if rest 1 0)))
(if (< 0 nopt)
(let ((ol (add-literal (make-constant-node `(quote ,odef)))))
(push-instruction
`(%initialize 3 ,nreq ,nopt ,ol ,r ,aok ,ksl ,kdl
,nc ,@consts ,(- nr nv nc))))
(push-instruction
`(%initialize 3 ,nreq ,nopt ,r ,aok ,ksl ,kdl
,nc ,@consts ,(- nr nv nc))))))))
(set-function-data-registers (get-function-data label) (register-map))
(generate-body-code (lambda-node-body n))))
(defun generate-body-code (form)
(do ((trees (list form)))
((null trees))
(let ((p (first trees)))
(setf trees
(cond
((leaf-node-p p) (push-label p) (rest trees))
((leaf-node-p (call-node-function p))
(generate-symbol-call-code p (rest trees)))
(t (generate-lambda-call-code p (rest trees))))))))
(defun lifted-lfun-node-p (n) (and (leaf-node-p n) (get-function-label n)))
(defun generate-symbol-call-code (n rest)
(let ((f (call-node-function n)))
(cond
((gfun-node-p f) (generate-gfun-call-code n rest))
((lifted-lfun-node-p f) (generate-lfun-call-code n rest))
(t (generate-continuation-call-code n rest)))))
(defun generate-lambda-call-code (n rest)
(let* ((f (call-node-function n))
(alist (lambda-node-arglist f))
(body (lambda-node-body f))
(args (call-node-args n)))
(mapc #'(lambda (x y)
(unless (leaf-node-p y)
(push (lambda-node-body y) rest)
(push x rest)))
alist
args)
(push body rest)
rest))
(defun generate-gfun-call-code (n rest)
(let* ((f (call-node-function n))
(cg (get-code-generator (gfun-symbol f) n)))
(if cg
(funcall cg n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(aregs (mapcar #'find-register (rest (call-node-args n))))
(na (length aregs))
(s (add-literal f)))
(if (final-value-continuation-p c)
(let ((cr (continuation-register c)))
(push-instruction `(%call ,s ,cr ,na ,@aregs)))
(let ((vr (continuation-value-register c)))
(push-instruction `(%save-call ,s ,vr ,na ,@aregs))))
(cleanup-continuation c rest nil)))))
(defun generate-lfun-call-code (n rest)
(let* ((f (call-node-function n))
(c (get-continuation (call-node-arg n 0)))
(aregs (mapcar #'find-register (rest (call-node-args n))))
(na (length aregs))
(s (get-function-label f)))
(if (final-value-continuation-p c)
(let ((cr (continuation-register c)))
(push-instruction `(%lcall ,s ,cr ,na ,@aregs)))
(let ((vr (continuation-value-register c)))
(push-instruction `(%save-lcall ,s ,vr ,na ,@aregs))))
(cleanup-continuation c rest nil)))
;;**** needs to be thought through relative to %y
(defun generate-continuation-call-code (n rest)
(let ((c (get-continuation (call-node-function n))))
(case (continuation-type c)
(final-value
(let ((cr (continuation-register c))
(vr (find-register (call-node-arg n 0))))
(push-instruction `(%set-one-value ,vr))
(push-instruction `(%return ,cr))))
((protected multiple-value)
(let ((aregs (mapcar #'find-register (call-node-args n))))
(cond
((= (length aregs) 1)
(push-instruction `(%set-one-value ,@aregs)))
(t (warn "multiple value continuation in callposition ~
with more than one argument")
(push-instruction `(%set-values ,(length aregs) ,@aregs))))))
(t
(let* ((fsym (continuation-name c))
(f (continuation-function c))
(b (lambda-node-body f))
(vars (lambda-node-arglist f))
(vals (call-node-args n)))
(mapc #'(lambda (x y)
(when (any-references-p y b)
(let ((xr (find-register x))
(yr (find-register y)))
(push-instruction `(%copy ,xr ,yr)))))
vals
vars)
(push-instruction `(%goto ,(make-label fsym))))))
(cleanup-continuation c rest nil)))
;;;;
;;;; Function Information Representation
;;;;
(defun initialize-functions (pieces)
(dolist (p pieces)
(let* ((label (make-label (first p)))
(d (make-function-data label (leaf-node-value (first p)))))
(push (list label d) *functions*))))
(defun make-function-data (label name) (list label name nil))
(defun get-function-data (label) (second (assoc label *functions*)))
(defun get-function-label (f) (first (assoc (make-label f) *functions*)))
(defun get-function-labels () (mapcar #'first *functions*))
(defun set-function-data-registers (fd r) (setf (third fd) r))
;;;;
;;;; Continuation Handling
;;;;
(defun get-continuation (n)
(if (lambda-node-p n)
(cons nil n)
(let ((cf (find-lambda-binding n)))
(cons n (if cf
cf
(if (member n *protected-continuations*)
'protected
'final))))))
(defun register-protected-continuation (k)
(pushnew k *protected-continuations*))
(defun immediate-continuation-p (c)
(and (lambda-node-p (cdr c)) (null (continuation-name c))))
(defun continuation-value-ignored-p (c)
(and (not (multiple-value-continuation-p c))
(or (null (lambda-node-arglist (cdr c)))
(not (any-references-p (first (lambda-node-arglist (cdr c)))
(cdr c))))))
(defun final-value-continuation-p (c) (eq (cdr c) 'final))
(defun protected-continuation-p (c) (eq (cdr c) 'protected))
(defun continuation-register (c) (if (car c) (find-register (car c))))
(defun multiple-value-continuation-p (c)
(multiple-value-continuation-node-p (cdr c)))
(defun continuation-value-register (c)
(if (and (lambda-node-p (cdr c))
(not (continuation-value-ignored-p c))
(not (multiple-value-continuation-p c)))
(find-register (first (lambda-node-arglist (cdr c))))
0))
(defun continuation-name (c) (car c))
(defun continuation-function (c) (if (lambda-node-p (cdr c)) (cdr c)))
(defun continuation-type (c)
(cond
((final-value-continuation-p c) 'final-value)
((protected-continuation-p c) 'protected)
((multiple-value-continuation-p c) 'multiple-value)
((continuation-value-ignored-p c) 'value-ignored)
(t 'one-value)))
(defun cleanup-continuation (c rest inline)
(if (final-value-continuation-p c)
(if inline
(let ((cr (continuation-register c)))
(push-instruction `(%return ,cr))))
(if (immediate-continuation-p c)
(push (lambda-node-body (cdr c)) rest)
(push-instruction `(%goto ,(make-label (continuation-name c))))))
rest)
;;;;
;;;; Register Handling
;;;;
(defun find-register (v)
(let ((e (assoc v *registers*)))
(if e (cdr e) (error "register not found"))))
(defun assign-registers (n)
(assign-registers-1 (lambda-node-body n)
(add-lambda-frame (lambda-node-arglist n) nil)))
(defun pop-unused-frames (env n)
(let ((frame (first env)))
(if (and frame
(eq (cdr frame) 'continuation)
(every #'(lambda (x) (not (any-references-p x n))) (car frame)))
(pop-unused-frames (cdr env) n)
env)))
(defun setq-continuation-p (n)
(if (continuation-node-p n)
(let* ((b (lambda-node-body n))
(f (call-node-function b)))
(if (and (gfun-eq f '%setq) (= (length (lambda-node-arglist n)) 1))
(let ((v (first (lambda-node-arglist n)))
(c (call-node-arg b 0)))
(and (eq v (call-node-arg b 2))
(assoc (call-node-arg b 1) *registers*)
(not (any-references-p v c))))))))
(defun add-register-frame (vars type env)
(let ((maxreg 0))
(dolist (e env) (incf maxreg (length (car e))))
(dolist (v vars)
(push (cons v maxreg) *registers*)
(incf maxreg))
(cons (cons vars type) env)))
(defun add-lambda-frame (vars env) (add-register-frame vars 'lambda env))
(defun add-continuation-frame (n env)
(if (setq-continuation-p n)
(let ((v (first (lambda-node-arglist n)))
(vv (call-node-arg (lambda-node-body n) 1)))
(push (cons v (cdr (assoc vv *registers*))) *registers*)
(pop-unused-frames env n))
(let ((alist (lambda-node-arglist n))
(b (lambda-node-body n)))
(if (and (not (multiple-value-continuation-node-p n))
(some #'(lambda (x) (any-references-p x b)) alist))
(add-register-frame alist
'continuation
(pop-unused-frames env n))
(pop-unused-frames env n)))))
(defun assign-registers-1 (n env)
(let ((f (call-node-function n))
(args (call-node-args n)))
(setf env (pop-unused-frames env n)) ;;**** is this OK?
(when (lambda-node-p f)
(let ((nfvars nil))
(mapc #'(lambda (x y)
(unless (lambda-node-p y)
(push x nfvars)
(push (add-literal y) *constants*)))
(lambda-node-arglist f)
args)
;;**** think about this -- should only have non-nil nfvars once
(assign-registers-1 (lambda-node-body f)
(add-lambda-frame (nreverse nfvars) env))))
(if (gfun-eq f '%y)
(assign-registers-1 (lambda-node-body (call-node-arg n 0)) env)
(let ((is-y-list (gfun-eq f '%y-list)))
(dolist (a args)
(when (lambda-node-p a)
(let ((new-env (if (and (continuation-node-p a) (not is-y-list))
(add-continuation-frame a env)
(add-lambda-frame (lambda-node-arglist a)
env))))
(assign-registers-1 (lambda-node-body a) new-env))))))))
(defun count-registers ()
(let ((n 0))
(dolist (r *registers* n)
(setf n (max n (+ (cdr r) 1))))))
(defun register-map ()
(flet ((rsym (x) (if (symbolp x) x (leaf-node-value x))))
(let ((regs nil))
(dolist (r *registers*)
(let* ((v (rsym (car r)))
(i (cdr r))
(e (assoc i regs)))
(if e (pushnew v (rest e)) (push (list i v) regs))))
(sort regs #'(lambda (x y) (< (first x) (first y)))))))
;;;;
;;;; Instruction Representation
;;;;
(defun make-label (v)
(let ((e (assoc v *labels*)))
(if e
(cdr e)
(let ((s (gensym (string (leaf-node-value v)))))
(push (cons v s) *labels*)
s))))
(defun make-label-node (&optional (label "LABEL"))
(make-leaf-node (gensym (string label))))
(defun push-label (v) (push (make-label v) *code*))
(defun push-instruction (i) (push i *code*))
;;;;
;;;; Literals Representation
;;;;
(defun next-literal-index () (length *literals*))
(defun literal-value (v)
(let ((c (leaf-node-value v)))
(if (and (consp c) (eq (first c) 'quote)) (second c) c)))
(defun push-literal (v)
(let ((n (next-literal-index)))
(push (literal-value v) *literals*)
n))
(defun add-literal (c) (add-literal-value (literal-value c)))
(defun add-literal-value (v)
(let ((p (position v *literals*)))
(if p
(- (length *literals*) p 1)
(prog1 (length *literals*) (push v *literals*)))))
;;;;
;;;; Code Generator Support for Special Instuctions
;;;;
(defun get-code-generator (s n)
(let* ((e (get s 'cmp-code-generator))
(test (first e))
(f (second e)))
(if test (if (funcall test n) f) f)))
(defun set-code-generator (s g)
(let ((e (get s 'cmp-code-generator)))
(if e
(setf (second e) g)
(setf (get s 'cmp-code-generator) (list nil g)))))
(defun set-code-generator-test (s g)
(let ((e (get s 'cmp-code-generator)))
(if e
(setf (first e) g)
(setf (get s 'cmp-code-generator) (list g nil)))))
(defmacro define-code-generator (sym &rest body)
`(set-code-generator ',sym #'(lambda ,@body)))
(defmacro define-code-generator-test (sym &rest body)
`(set-code-generator-test ',sym #'(lambda ,@body)))
(defun generate-inline-function-code (f n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(vr (continuation-value-register c))
(args (rest (call-node-args n))))
(funcall f args vr)
(cleanup-continuation c rest t)))
(defmacro define-inline-function-generator (sym &rest body)
`(define-code-generator ,sym (n rest)
(generate-inline-function-code #'(lambda ,@body) n rest)))
(defmacro define-standard-inline-generator (sym &optional
(name sym)
(llist nil llist-supplied))
`(progn
,@(if llist-supplied `((define-lambda-list ,sym ,llist)))
(define-inline-function-generator ,sym (args r)
(push-instruction
(cons ',name (append (mapcar #'find-register args) (list r)))))))
(defmacro define-standard-inline-generator-2 (sym &rest args)
`(progn
(define-standard-inline-generator ,sym ,@args)
(define-code-generator-test ,sym (n) (= (call-node-arg-count n) 3))))
(defmacro define-test-code-generator-1 (name opcode)
`(define-code-generator ,name (n rest)
(let* ((cons (call-node-arg n 0))
(alt (call-node-arg n 1))
(vr (find-register (call-node-arg n 2)))
(clab (if (lambda-node-p cons) (make-label-node "THEN") cons))
(alab (if (lambda-node-p alt) (make-label-node "ELSE") alt)))
(push-instruction
(list '%test-1 ,opcode (make-label clab) (make-label alab) vr))
(when (lambda-node-p alt)
(push (lambda-node-body alt) rest)
(push alab rest))
(when (lambda-node-p cons)
(push (lambda-node-body cons) rest)
(push clab rest))
rest)))
(defmacro define-test-code-generator-2 (name opcode)
`(define-code-generator ,name (n rest)
(let* ((cons (call-node-arg n 0))
(alt (call-node-arg n 1))
(vr1 (find-register (call-node-arg n 2)))
(vr2 (find-register (call-node-arg n 3)))
(clab (if (lambda-node-p cons) (make-label-node "THEN") cons))
(alab (if (lambda-node-p alt) (make-label-node "ELSE") alt)))
(push-instruction
(list '%test-2 ,opcode (make-label clab) (make-label alab) vr1 vr2))
(when (lambda-node-p alt)
(push (lambda-node-body alt) rest)
(push alab rest))
(when (lambda-node-p cons)
(push (lambda-node-body cons) rest)
(push clab rest))
rest)))
(defmacro define-test-arith-code-generator-2 (name opcode)
`(define-code-generator ,name (n rest)
(let* ((cons (call-node-arg n 0))
(alt (call-node-arg n 1))
(vr1 (find-register (call-node-arg n 2)))
(vr2 (find-register (call-node-arg n 3)))
(clab (if (lambda-node-p cons) (make-label-node "THEN") cons))
(alab (if (lambda-node-p alt) (make-label-node "ELSE") alt)))
(push-instruction
(list '%test-arith-2
,(char-int opcode)
(make-label clab)
(make-label alab)
vr1
vr2))
(when (lambda-node-p alt)
(push (lambda-node-body alt) rest)
(push alab rest))
(when (lambda-node-p cons)
(push (lambda-node-body cons) rest)
(push clab rest))
rest)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;
;;;;; Specific Code Generators
;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; Test/Case Code Generators
;;;;
(define-test-code-generator-1 %test 0)
(define-test-code-generator-1 %test-consp 1)
(define-test-code-generator-1 %test-supplied-p 2)
(define-test-code-generator-1 %test-endp 3)
(define-test-code-generator-2 %test-eq 0)
(define-test-code-generator-2 %test-eql 1)
(define-test-code-generator-2 %test-equal 2)
(define-test-arith-code-generator-2 %test= #\=)
(define-test-arith-code-generator-2 %test/= #\#)
(define-test-arith-code-generator-2 %test< #\<)
(define-test-arith-code-generator-2 %test> #\>)
(define-test-arith-code-generator-2 %test<= #\L)
(define-test-arith-code-generator-2 %test>= #\G)
(define-code-generator %case (n rest)
(let* ((var (find-register (call-node-arg n 0)))
(choices (find-register (call-node-arg n 1)))
(actions (rest (rest (call-node-args n))))
(labels nil))
(dolist (a actions)
(cond
((lambda-node-p a)
(let ((lab (make-label-node "CASE")))
(push (lambda-node-body a) rest)
(push lab rest)
(push (make-label lab) labels)))
(t (push (make-label a) labels))))
(push-instruction `(%case ,var ,choices ,@(reverse labels)))
rest))
;;;;
;;;; Multiple Value Code Generators
;;;;
(define-code-generator %mvc (n rest)
(let ((c (get-continuation (call-node-arg n 0)))
(f (call-node-arg n 1)))
(if (final-value-continuation-p c)
(let ((cr (continuation-register c)))
(cond
((gfun-node-p f)
(push-instruction `(%mvcall ,(add-literal f) ,cr)))
((lifted-lfun-node-p f)
(push-instruction `(%mvlcall ,(get-function-label f) ,cr)))
(t
(push-instruction `(%mvvcall ,(find-register f) ,cr)))))
(let ((vr (continuation-value-register c)))
(cond
((gfun-node-p f)
(push-instruction `(%save-mvcall ,(add-literal f) ,vr)))
((lifted-lfun-node-p f)
(push-instruction `(%save-mvlcall ,(get-function-label f) ,vr)))
(t
(push-instruction `(%save-mvvcall ,(find-register f) ,vr))))))
(cleanup-continuation c rest nil)))
;;**** is the continuation necessarily both immediate and not multiple value?
(define-code-generator %mvcc (n rest)
(let* ((k (call-node-arg n 0))
(alist (lambda-node-arglist k))
(b (lambda-node-body k)))
(if (some #'(lambda (x) (any-references-p x b)) alist)
(let ((vregs (mapcar #'find-register alist)))
(push-instruction `(%get-values ,(length vregs) ,@vregs))))
(cons b rest)))
(define-code-generator values (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(aregs (mapcar #'find-register (rest (call-node-args n))))
(r (continuation-value-register c)))
;;**** check number of values against limit
(push-instruction `(%set-values ,(length aregs) ,@aregs))
(unless (= r 0)
(warn "VALUES occurred in non-multiple value continuations")
(push-instruction `(%get-one-value ,r)))
(cleanup-continuation c rest t)))
(define-code-generator values-list (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(ar (find-register (call-node-arg n 1)))
(r (continuation-value-register c)))
(push-instruction `(%set-values-list ,ar))
(unless (= r 0)
(warn "VALUES-LIST occurred in non-multiple value continuations")
(push-instruction `(%get-one-value ,r)))
(cleanup-continuation c rest t)))
;;;;
;;;; Catch/Throw/Unwind-Protect Code Generators
;;;;
(defun cleanup-protected-continuation (label c rest)
(push label rest)
(let ((r (continuation-value-register c)))
(unless (= r 0) (push-instruction `(%get-one-value ,r))))
(cleanup-continuation c rest t))
(define-code-generator %catch (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(tr (find-register (call-node-arg n 1)))
(form-fun (call-node-arg n 2))
(fb (lambda-node-body form-fun))
(fv (first (lambda-node-arglist form-fun)))
(fr (find-register fv))
(label (make-label-node "C")))
(push-instruction `(%catch ,tr ,(make-label label) ,fr))
(push fb rest)
(cleanup-protected-continuation label c rest)))
(define-code-generator %throw (n rest)
(let ((tr (find-register (call-node-arg n 0))))
(push-instruction `(%throw ,tr))
rest))
(define-code-generator %catch-block (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(nr (find-register (call-node-arg n 1)))
(form-fun (call-node-arg n 2))
(fb (lambda-node-body form-fun))
(fv (first (lambda-node-arglist form-fun)))
(fr (find-register fv))
(ftr (find-register (second (lambda-node-arglist form-fun))))
(label (make-label-node "C")))
(push-instruction `(%catch-block ,nr ,(make-label label) ,fr ,ftr))
(push fb rest)
(cleanup-protected-continuation label c rest)))
(define-code-generator %throw-return-from (n rest)
(let ((tr (find-register (call-node-arg n 0))))
(push-instruction `(%throw-return-from ,tr))
rest))
(define-code-generator %catch-tagbody (n rest)
(cons (lambda-node-body (call-node-arg n 1)) rest))
(define-code-generator %do-catch-tagbody (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(vr (continuation-value-register c))
(start (call-node-arg n 1))
(slab (if (lambda-node-p start) (make-label-node "TAGBODY") start))
(cr (find-register (call-node-arg n 2)))
(tr (find-register (call-node-arg n 3))))
(push-instruction `(%catch-tagbody ,(make-label slab) ,cr ,tr ,vr))
(when (lambda-node-p start)
(push (lambda-node-body start) rest)
(push slab rest))
(cleanup-continuation c rest t)))
(define-code-generator %throw-go (n rest)
(let* ((tr (find-register (call-node-arg n 0)))
(target (call-node-arg n 1))
(tlab (if (lambda-node-p target) (make-label-node "TARGET") target)))
(push-instruction `(%throw-go ,tr ,(make-label tlab)))
(when (lambda-node-p target)
(push (lambda-node-body target) rest)
(push tlab rest))
rest))
(define-code-generator %errset (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(form-fun (call-node-arg n 1))
(fi (find-register (call-node-arg n 2)))
(fb (lambda-node-body form-fun))
(fv (first (lambda-node-arglist form-fun)))
(fr (find-register fv))
(label (make-label-node "C")))
(push-instruction `(%errset ,(make-label label) ,fr ,fi))
(push fb rest)
(cleanup-protected-continuation label c rest)))
(define-code-generator %unwind-protect (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(prot-form (call-node-arg n 1))
(pfb (lambda-node-body prot-form))
(pfv (first (lambda-node-arglist prot-form)))
(pfr (find-register pfv))
(unwind-form (call-node-arg n 2))
(ufb (lambda-node-body unwind-form))
(ufv (first (lambda-node-arglist unwind-form)))
(ufr (find-register ufv))
(label1 (make-label-node "U"))
(label2 (make-label-node "U")))
(push-instruction
`(%unwind-protect ,(make-label label1) ,(make-label label2) ,pfr ,ufr))
(push pfb rest)
(push label1 rest)
(push ufb rest)
(cleanup-protected-continuation label2 c rest)))
(define-code-generator %dynamic-bind (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(svr (find-register (call-node-arg n 1)))
(vvr (find-register (call-node-arg n 2)))
(body-form (call-node-arg n 3))
(bfb (lambda-node-body body-form))
(bfv (first (lambda-node-arglist body-form)))
(ulabel (make-label bfv))
(label (make-label-node "D")))
(register-protected-continuation bfv)
(push-instruction `(%dynamic-bind ,svr ,vvr))
(push-instruction `(%goto ,(make-label label)))
(push-instruction (make-label bfv))
(push-instruction '(%dynamic-unbind))
(push bfb rest)
(cleanup-protected-continuation label c rest)))
;;;;
;;;; Y Combinator/Closure Code Generators
;;;;
(define-code-generator %y (n rest)
(let* ((f (call-node-arg n 0))
(names (reverse (lambda-node-arglist f)))
(b (lambda-node-body f))
(bf (call-node-function b)))
(when (gfun-eq bf '%make-y-closures)
(let* ((bc (call-node-arg b 0))
(cr (mapcar #'find-register (lambda-node-arglist bc)))
(n (length cr))
(bargs (rest (call-node-args b)))
(fi (mapcar #'(lambda (x y) (get-function-label x)) bargs cr))
(fvr (mapcar #'find-register (nthcdr n bargs)))
(nv (length fvr))
(ficr nil))
(mapc #'(lambda (x y) (push x ficr) (push y ficr)) fi cr)
(setf ficr (nreverse ficr))
(push-instruction `(%make-y-closures ,n ,nv ,@ficr ,@fvr))
(setf b (lambda-node-body bc))))
(let* ((y-list-args (call-node-args b))
(body (first y-list-args))
(conts (reverse (rest y-list-args))))
(mapc #'(lambda (x y)
(push (lambda-node-body y) rest)
(push x rest))
names
conts)
(push (lambda-node-body body) rest)
rest)))
(define-inline-function-generator %make-closure (args r)
(let ((f (get-function-label (first args)))
(n (length (rest args)))
(aregs (mapcar #'find-register (rest args))))
(push-instruction `(%make-closure ,f ,r ,n ,@aregs))))
;;;;
;;;; Funcall/Apply Code Generators
;;;;
(define-code-generator funcall (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(fr (find-register (call-node-arg n 1)))
(aregs (mapcar #'find-register (rest (rest (call-node-args n)))))
(na (length aregs)))
(if (final-value-continuation-p c)
(let ((cr (continuation-register c)))
(push-instruction `(%vcall ,fr ,cr ,na ,@aregs)))
(let ((vr (continuation-value-register c)))
(push-instruction `(%save-vcall ,fr ,vr ,na ,@aregs))))
(cleanup-continuation c rest nil)))
;;;;
;;;; Inlined Internal Codes
;;;;
(define-inline-function-generator %symval (args r)
(push-instruction `(%symval ,(add-literal (first args)) ,r)))
(define-inline-function-generator %symfun (args r)
(push-instruction `(%symfun ,(add-literal (first args)) ,r)))
(define-inline-function-generator %set-symval (args r)
(push-instruction `(%set-symval ,(add-literal (first args))
,(find-register (second args))
,r)))
(define-standard-inline-generator %copy)
(define-code-generator %setq (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(p (find-register (call-node-arg n 1)))
(q (find-register (call-node-arg n 2)))
(r (continuation-value-register c)))
(push-instruction `(%copy ,q ,p))
(cond
((/= r 0) (push-instruction `(%copy ,q ,r)))
((not (eq (continuation-type c) 'value-ignored))
(push-instruction `(%set-one-value ,q))))
(cleanup-continuation c rest t)))
(define-standard-inline-generator %supplied-p)
(define-standard-inline-generator %make-cell)
(define-standard-inline-generator %cell-value)
(define-standard-inline-generator %set-cell-value)
;**** drop once implementation changes
(define-code-generator %mv-collect (n rest)
(cleanup-continuation (get-continuation (call-node-arg n 0)) rest nil))
(define-standard-inline-generator %nth-value %nth-value (x))
(define-code-generator %push-values (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(r (continuation-value-register c)))
(unless (= r 0) (push-instruction `(%push-values ,r)))
(cleanup-continuation c rest nil)))
(define-code-generator %pop-values (n rest)
(let* ((c (get-continuation (call-node-arg n 0)))
(nv (find-register (call-node-arg n 1)))
(r (continuation-value-register c)))
(push-instruction `(%pop-values ,nv))
(unless (= r 0) (push-instruction `(%get-one-value ,r)))
(cleanup-continuation c rest t)))
;;;;
;;;; Inlined Function Code Generators
;;;;
;;**** these could handle 3 args specially but funcall for 4 or more
(define-lambda-list + (&rest a))
(define-inline-function-generator + (args r)
(push-instruction `(%arith2 ,(char-int #\+)
,(find-register (first args))
,(find-register (second args))
,r)))
(define-code-generator-test + (n) (= (call-node-arg-count n) 3))
(define-lambda-list * (&rest a))
(define-inline-function-generator * (args r)
(push-instruction `(%arith2 ,(char-int #\*)
,(find-register (first args))
,(find-register (second args))
,r)))
(define-code-generator-test * (n) (= (call-node-arg-count n) 3))
(define-lambda-list - (x &rest a))
(define-inline-function-generator - (args r)
(case (length args)
(1 (push-instruction `(%arith1 ,(char-int #\-)
,(find-register (first args)) ,r)))
(2 (push-instruction `(%arith2 ,(char-int #\-)
,(find-register (first args))
,(find-register (second args))
,r)))))
(define-code-generator-test - (n) (<= (call-node-arg-count n) 3))
(define-lambda-list / (x &rest a))
(define-inline-function-generator / (args r)
(case (length args)
(1 (push-instruction `(%arith1 ,(char-int #\/)
,(find-register (first args)) ,r)))
(2 (push-instruction `(%arith2 ,(char-int #\/)
,(find-register (first args))
,(find-register (second args))
,r)))))
(define-code-generator-test / (n) (<= (call-node-arg-count n) 3))
(define-lambda-list min (x &rest a))
(define-inline-function-generator min (args r)
(push-instruction `(%arith2 ,(char-int #\m)
,(find-register (first args))
,(find-register (second args))
,r)))
(define-code-generator-test min (n) (= (call-node-arg-count n) 3))
(define-lambda-list max (x &rest a))
(define-inline-function-generator max (args r)
(push-instruction `(%arith2 ,(char-int #\M)
,(find-register (first args))
,(find-register (second args))
,r)))
(define-code-generator-test max (n) (= (call-node-arg-count n) 3))
(defmacro define-arith-pred-generator-2 (sym char)
`(progn
(define-lambda-list ,sym (x &rest a))
(define-inline-function-generator ,sym (args r)
(push-instruction
(list '%arith-pred2
(char-int ,char)
(find-register (first args))
(find-register (second args))
r)))
(define-code-generator-test ,sym (n) (= (call-node-arg-count n) 3))))
(define-arith-pred-generator-2 < #\<)
(define-arith-pred-generator-2 <= #\L)
(define-arith-pred-generator-2 = #\=)
(define-arith-pred-generator-2 /= #\#)
(define-arith-pred-generator-2 >= #\G)
(define-arith-pred-generator-2 > #\>)
(define-standard-inline-generator-2 get %get)
(define-standard-inline-generator-2 %set-get)
(define-standard-inline-generator consp %consp (x))
(define-standard-inline-generator endp %endp (x))
(define-standard-inline-generator eq %eq (x y))
(define-standard-inline-generator eql %eql (x y))
(define-standard-inline-generator equal %equal (x y))
(define-inline-function-generator aref (args r)
(case (length args)
(2 (push-instruction `(%aref1 ,(find-register (first args))
,(find-register (second args))
,r)))
(3 (push-instruction `(%aref2 ,(find-register (first args))
,(find-register (second args))
,(find-register (third args))
,r)))))
(define-code-generator-test aref (n) (<= 3 (call-node-arg-count n) 4))
(define-lambda-list aref (x &rest args))
(define-inline-function-generator %set-aref (args r)
(case (length args)
(3 (push-instruction `(%set-aref1 ,(find-register (first args))
,(find-register (second args))
,(find-register (third args))
,r)))
(4 (push-instruction `(%set-aref2 ,(find-register (first args))
,(find-register (second args))
,(find-register (third args))
,(find-register (fourth args))
,r)))))
(define-code-generator-test %set-aref (n) (<= 4 (call-node-arg-count n) 5))
(define-lambda-list %set-aref (x v &rest args))
;;**** check if others are needed
(define-standard-inline-generator %set-nth %set-nth (i x v))
(define-standard-inline-generator rplaca %rplaca (x v))
(define-standard-inline-generator rplacd %rplacd (x v))
(define-standard-inline-generator %set-svref %set-svref (x i v))
(define-standard-inline-generator %set-elt %set-elt (x i v))
(define-standard-inline-generator nth %nth (i x))
(define-standard-inline-generator svref %svref (x i))
(define-standard-inline-generator elt %elt (x i))
(define-standard-inline-generator cons %cons (x y))
(define-lambda-list 1+ (x))
(define-inline-function-generator 1+ (args r)
(push-instruction `(%arith1 ,(char-int #\p)
,(find-register (first args))
,r)))
(define-lambda-list 1- (x))
(define-inline-function-generator 1- (args r)
(push-instruction `(%arith1 ,(char-int #\m)
,(find-register (first args))
,r)))
;;
;; SLOT-VALUE
;;
(define-lambda-list slot-value (x &optional y))
(define-inline-function-generator slot-value (args r)
(case (length args)
(1 (push-instruction `(%slot-value ,(find-register (first args)) ,r)))
(2 (push-instruction `(%set-slot-value ,(find-register (first args))
,(find-register (second args))
,r)))))
;;
;; C?R, C??R and C???R
;;
(define-lambda-list car (x))
(define-standard-inline-generator car %car)
(define-lambda-list cdr (x))
(define-standard-inline-generator cdr %cdr)
(defmacro define-cxr-generator (name n x)
`(progn
(define-lambda-list ,name (x))
(define-inline-function-generator ,name (args r)
(push-instruction (list '%cxr ,n ,x (find-register (first args)) r)))))
(define-cxr-generator caar 2 #b11)
(define-cxr-generator cadr 2 #b10)
(define-cxr-generator cdar 2 #b01)
(define-cxr-generator cddr 2 #b00)
(define-cxr-generator caaar 3 #b111)
(define-cxr-generator caadr 3 #b110)
(define-cxr-generator cadar 3 #b101)
(define-cxr-generator caddr 3 #b100)
(define-cxr-generator cdaar 3 #b011)
(define-cxr-generator cdadr 3 #b010)
(define-cxr-generator cddar 3 #b001)
(define-cxr-generator cdddr 3 #b000)
;;
;; CAR and CDR setf methods
;;
(define-standard-inline-generator %set-car %set-car (x v))
(define-standard-inline-generator %set-cdr %set-cdr (x v))
|