1 ;;;; implementation-independent facilities used for defining the
2 ;;;; compiler's interface to the VM in a given implementation
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;; Return the template having the specified name, or die trying.
16 (defun template-or-lose (x)
18 (or (gethash x *backend-template-names*)
19 (error "~S is not a defined template." x))))
21 ;;; Return the SC structure, SB structure or SC number corresponding
22 ;;; to a name, or die trying.
25 (or (gethash x *backend-sc-names*)
26 (error "~S is not a defined storage class." x))))
29 (or (gethash x *backend-sb-names*)
30 (error "~S is not a defined storage base." x))))
31 (defun sc-number-or-lose (x)
32 (the sc-number (sc-number (sc-or-lose x))))
34 ;;; Like the non-meta versions, but go for the meta-compile-time info.
35 ;;; These should not be used after load time, since compiling the compiler
36 ;;; changes the definitions.
37 (defun meta-sc-or-lose (x)
39 (or (gethash x *backend-meta-sc-names*)
40 (error "~S is not a defined storage class." x))))
41 (defun meta-sb-or-lose (x)
43 (or (gethash x *backend-meta-sb-names*)
44 (error "~S is not a defined storage base." x))))
45 (defun meta-sc-number-or-lose (x)
46 (the sc-number (sc-number (meta-sc-or-lose x))))
48 ;;;; side-effect classes
50 (def-boolean-attribute vop
53 ;;;; move/coerce definition
55 ;;; Compute at compiler load time the costs for moving between all SCs that
56 ;;; can be loaded from FROM-SC and to TO-SC given a base move cost Cost.
57 (defun compute-move-costs (from-sc to-sc cost)
58 (declare (type sc from-sc to-sc) (type index cost))
59 (let ((to-scn (sc-number to-sc))
60 (from-costs (sc-load-costs from-sc)))
61 (dolist (dest-sc (cons to-sc (sc-alternate-scs to-sc)))
62 (let ((vec (sc-move-costs dest-sc))
63 (dest-costs (sc-load-costs dest-sc)))
64 (setf (svref vec (sc-number from-sc)) cost)
65 (dolist (sc (append (sc-alternate-scs from-sc)
66 (sc-constant-scs from-sc)))
67 (let* ((scn (sc-number sc))
68 (total (+ (svref from-costs scn)
69 (svref dest-costs to-scn)
71 (old (svref vec scn)))
72 (unless (and old (< old total))
73 (setf (svref vec scn) total))))))))
75 ;;;; primitive type definition
77 ;;; Return the primitive type corresponding to the specified name, or
79 (defun primitive-type-or-lose (name)
81 (or (gethash name *backend-primitive-type-names*)
82 (error "~S is not a defined primitive type." name))))
84 ;;; Return true if SC is either one of PTYPE's SC's, or one of those
85 ;;; SC's alternate or constant SCs.
86 (defun sc-allowed-by-primitive-type (sc ptype)
87 (declare (type sc sc) (type primitive-type ptype))
88 (let ((scn (sc-number sc)))
89 (dolist (allowed (primitive-type-scs ptype) nil)
90 (when (eql allowed scn)
92 (let ((allowed-sc (svref *backend-sc-numbers* allowed)))
93 (when (or (member sc (sc-alternate-scs allowed-sc))
94 (member sc (sc-constant-scs allowed-sc)))
97 ;;;; generation of emit functions
99 (eval-when (:compile-toplevel :load-toplevel :execute)
100 (defconstant max-vop-tn-refs 256))
102 (defvar *vop-tn-refs* (make-array max-vop-tn-refs :initial-element nil))
103 (defvar *using-vop-tn-refs* nil)
105 (defun flush-vop-tn-refs ()
106 (unless *using-vop-tn-refs*
107 (fill *vop-tn-refs* nil)))
109 (pushnew 'flush-vop-tn-refs *before-gc-hooks*)
111 (defconstant sc-bits (integer-length (1- sc-number-limit)))
113 (defun emit-generic-vop (node block template args results &optional info)
114 (%emit-generic-vop node block template args results info))
116 (defun %emit-generic-vop (node block template args results info)
117 (let* ((vop (make-vop block node template args results))
118 (num-args (vop-info-num-args template))
119 (last-arg (1- num-args))
120 (num-results (vop-info-num-results template))
121 (num-operands (+ num-args num-results))
122 (last-result (1- num-operands))
123 (ref-ordering (vop-info-ref-ordering template)))
124 (declare (type vop vop)
125 (type (integer 0 #.max-vop-tn-refs)
126 num-args num-results num-operands)
127 (type (integer -1 #.(1- max-vop-tn-refs)) last-arg last-result))
128 (setf (vop-codegen-info vop) info)
129 (let ((refs *vop-tn-refs*)
130 (*using-vop-tn-refs* t))
131 (declare (type (simple-vector #.max-vop-tn-refs) refs))
132 (do ((index 0 (1+ index))
133 (ref args (and ref (tn-ref-across ref))))
135 (setf (svref refs index) ref))
136 (do ((index num-args (1+ index))
137 (ref results (and ref (tn-ref-across ref))))
138 ((= index num-operands))
139 (setf (svref refs index) ref))
140 (let ((temps (vop-info-temps template)))
142 (let ((index num-operands)
144 (dotimes (i (length temps))
145 (let* ((temp (aref temps i))
146 (tn (if (logbitp 0 temp)
148 (ldb (byte sc-bits 1) temp)
149 (ash temp (- (1+ sc-bits))))
150 (make-restricted-tn nil (ash temp -1))))
151 (write-ref (reference-tn tn t)))
152 (setf (aref refs index) (reference-tn tn nil))
153 (setf (aref refs (1+ index)) write-ref)
155 (setf (tn-ref-across prev) write-ref)
156 (setf (vop-temps vop) write-ref))
157 (setf prev write-ref)
160 (flet ((add-ref (ref)
161 (setf (tn-ref-vop ref) vop)
162 (setf (tn-ref-next-ref ref) prev)
164 (declare (inline add-ref))
165 (dotimes (i (length ref-ordering))
166 (let* ((index (aref ref-ordering i))
167 (ref (aref refs index)))
168 (if (or (= index last-arg) (= index last-result))
169 (do ((ref ref (tn-ref-across ref)))
173 (setf (vop-refs vop) prev))
174 (let ((targets (vop-info-targets template)))
176 (dotimes (i (length targets))
177 (let ((target (aref targets i)))
178 (target-if-desirable (aref refs (ldb (byte 8 8) target))
179 (aref refs (ldb (byte 8 0) target))))))))
182 ;;;; function translation stuff
184 ;;; Add Template into List, removing any old template with the same name.
185 ;;; We also maintain the increasing cost ordering.
186 (defun adjoin-template (template list)
187 (declare (type template template) (list list))
189 (remove (template-name template) list
190 :key #'template-name))
192 :key #'template-cost))
194 ;;; Return a function type specifier describing Template's type computed
195 ;;; from the operand type restrictions.
196 (defun template-type-specifier (template)
197 (declare (type template template))
198 (flet ((convert (types more-types)
203 (:or `(or ,@(mapcar #'(lambda (type)
208 (:constant `(constant-argument ,(third x)))))))
209 `(,@(mapcar #'frob types)
211 `(&rest ,(frob more-types)))))))
212 (let* ((args (convert (template-arg-types template)
213 (template-more-args-type template)))
214 (result-restr (template-result-types template))
215 (results (if (eq result-restr :conditional)
217 (convert result-restr
218 (cond ((template-more-results-type template))
219 ((/= (length result-restr) 1) '*)
222 ,(if (= (length results) 1)
224 `(values ,@results))))))