1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
12 (def-alloc %make-structure-instance 1 :structure-alloc
13 sb!vm:instance-header-widetag sb!vm:instance-pointer-lowtag
16 #!+stack-allocatable-fixed-objects
17 (defoptimizer (%make-structure-instance stack-allocate-result) ((defstruct-description &rest args) node dx)
18 (aver (constant-lvar-p defstruct-description))
19 ;; A structure instance can be stack-allocated if it has no raw
20 ;; slots, or if we're on a target with a conservatively-scavenged
21 ;; stack. We have no reader conditional for stack conservation, but
22 ;; it turns out that the only time stack conservation is in play is
23 ;; when we're on GENCGC (since CHENEYGC doesn't have conservation)
24 ;; and C-STACK-IS-CONTROL-STACK (otherwise, the C stack is the
25 ;; number stack, and we precisely-scavenge the control stack).
26 #!-(and :gencgc :c-stack-is-control-stack)
27 (zerop (sb!kernel::dd-raw-length (lvar-value defstruct-description)))
28 #!+(and :gencgc :c-stack-is-control-stack)
31 (defoptimizer ir2-convert-reffer ((object) node block name offset lowtag)
32 (let* ((lvar (node-lvar node))
33 (locs (lvar-result-tns lvar
34 (list *backend-t-primitive-type*)))
36 (vop slot node block (lvar-tn node block object)
37 name offset lowtag res)
38 (move-lvar-result node block locs lvar)))
40 (defoptimizer ir2-convert-setter ((object value) node block name offset lowtag)
41 (let ((value-tn (lvar-tn node block value)))
42 (vop set-slot node block (lvar-tn node block object) value-tn
44 (move-lvar-result node block (list value-tn) (node-lvar node))))
46 ;;; FIXME: Isn't there a name for this which looks less like a typo?
47 ;;; (The name IR2-CONVERT-SETTER is used for something else, just above.)
48 (defoptimizer ir2-convert-setfer ((value object) node block name offset lowtag)
49 (let ((value-tn (lvar-tn node block value)))
50 (vop set-slot node block (lvar-tn node block object) value-tn
52 (move-lvar-result node block (list value-tn) (node-lvar node))))
54 #!+compare-and-swap-vops
55 (defoptimizer ir2-convert-casser
56 ((object old new) node block name offset lowtag)
57 (let* ((lvar (node-lvar node))
58 (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
60 (vop compare-and-swap-slot node block
61 (lvar-tn node block object)
62 (lvar-tn node block old)
63 (lvar-tn node block new)
66 (move-lvar-result node block locs lvar)))
68 (defun emit-inits (node block name object lowtag instance-length inits args)
69 #!-raw-instance-init-vops
70 (declare (ignore instance-length))
71 (let ((unbound-marker-tn nil)
72 (funcallable-instance-tramp-tn nil))
74 (let ((kind (car init))
78 (let ((raw-type (pop slot))
79 (arg-tn (lvar-tn node block (pop args))))
80 (macrolet ((make-case ()
83 (vop init-slot node block object arg-tn
84 name (+ sb!vm:instance-slots-offset slot) lowtag))
85 ,@(mapcar (lambda (rsd)
86 `(,(sb!kernel::raw-slot-data-raw-type rsd)
87 (vop ,(sb!kernel::raw-slot-data-init-vop rsd)
89 object arg-tn instance-length slot)))
90 #!+raw-instance-init-vops
91 sb!kernel::*raw-slot-data-list*
92 #!-raw-instance-init-vops
96 (vop init-slot node block object
97 (emit-constant (sb!kernel::dd-layout-or-lose slot))
98 name sb!vm:instance-slots-offset lowtag))
100 (vop init-slot node block object
104 (lvar-tn node block (pop args)))
106 (or unbound-marker-tn
107 (setf unbound-marker-tn
108 (let ((tn (make-restricted-tn
110 (sc-number-or-lose 'sb!vm::any-reg))))
111 (vop make-unbound-marker node block tn)
115 (:funcallable-instance-tramp
116 (or funcallable-instance-tramp-tn
117 (setf funcallable-instance-tramp-tn
118 (let ((tn (make-restricted-tn
120 (sc-number-or-lose 'sb!vm::any-reg))))
121 (vop make-funcallable-instance-tramp node block tn)
123 name slot lowtag))))))
125 (bug "Leftover args: ~S" args)))
127 (defun emit-fixed-alloc (node block name words type lowtag result lvar)
128 (let ((stack-allocate-p (and lvar (lvar-dynamic-extent lvar))))
129 (when stack-allocate-p
130 (vop current-stack-pointer node block
131 (ir2-lvar-stack-pointer (lvar-info lvar))))
132 (vop fixed-alloc node block name words type lowtag stack-allocate-p result)))
134 (defoptimizer ir2-convert-fixed-allocation
135 ((&rest args) node block name words type lowtag inits)
136 (let* ((lvar (node-lvar node))
137 (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
138 (result (first locs)))
139 (emit-fixed-alloc node block name words type lowtag result lvar)
140 (emit-inits node block name result lowtag words inits args)
141 (move-lvar-result node block locs lvar)))
143 (defoptimizer ir2-convert-variable-allocation
144 ((extra &rest args) node block name words type lowtag inits)
145 (let* ((lvar (node-lvar node))
146 (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
147 (result (first locs)))
148 (if (constant-lvar-p extra)
149 (let ((words (+ (lvar-value extra) words)))
150 (emit-fixed-alloc node block name words type lowtag result lvar))
151 (vop var-alloc node block (lvar-tn node block extra) name words
153 (emit-inits node block name result lowtag nil inits args)
154 (move-lvar-result node block locs lvar)))
156 (defoptimizer ir2-convert-structure-allocation
157 ((dd slot-specs &rest args) node block name words type lowtag inits)
158 (let* ((lvar (node-lvar node))
159 (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
160 (result (first locs)))
161 (aver (constant-lvar-p dd))
162 (aver (constant-lvar-p slot-specs))
163 (let* ((c-dd (lvar-value dd))
164 (c-slot-specs (lvar-value slot-specs))
165 (words (+ (sb!kernel::dd-instance-length c-dd) words)))
166 (emit-fixed-alloc node block name words type lowtag result lvar)
167 (emit-inits node block name result lowtag words `((:dd . ,c-dd) ,@c-slot-specs) args)
168 (move-lvar-result node block locs lvar))))
170 (defoptimizer (initialize-vector ir2-convert)
171 ((vector &rest initial-contents) node block)
172 (let* ((vector-ctype (lvar-type vector))
173 (elt-ctype (if (array-type-p vector-ctype)
174 (array-type-specialized-element-type vector-ctype)
175 (bug "Unknow vector type in IR2 conversion for ~S."
176 'initialize-vector)))
177 (saetp (find-saetp-by-ctype elt-ctype))
178 (lvar (node-lvar node))
179 (locs (lvar-result-tns lvar (list (primitive-type vector-ctype))))
180 (result (first locs))
181 (elt-ptype (primitive-type elt-ctype))
182 (tmp (make-normal-tn elt-ptype)))
183 (emit-move node block (lvar-tn node block vector) result)
184 (flet ((compute-setter ()
187 (let ((*package* (find-package :sb!vm))
190 (when (sb!vm:saetp-specifier s)
192 `(,(sb!vm:saetp-typecode s)
195 (vop ,(symbolicate "DATA-VECTOR-SET-WITH-OFFSET/"
196 (sb!vm:saetp-primitive-type-name s))
197 node block result index tn 0 tn)
199 (vop ,(symbolicate "DATA-VECTOR-SET/"
200 (sb!vm:saetp-primitive-type-name s))
201 node block result index tn tn)))
203 sb!vm:*specialized-array-element-type-properties*)
204 `(ecase (sb!vm:saetp-typecode saetp)
205 ,@(nreverse clauses)))))
208 (emit-constant index)))
209 (let ((setter (compute-setter))
210 (length (length initial-contents)))
212 (emit-move node block (lvar-tn node block (pop initial-contents)) tmp)
213 (funcall setter (tnify i) tmp))))
214 (move-lvar-result node block locs lvar)))
216 ;;; :SET-TRANS (in objdef.lisp DEFINE-PRIMITIVE-OBJECT) doesn't quite
217 ;;; cut it for symbols, where under certain compilation options
218 ;;; (e.g. #!+SB-THREAD) we have to do something complicated, rather
219 ;;; than simply set the slot. So we build the IR2 converting function
220 ;;; by hand. -- CSR, 2003-05-08
221 (let ((fun-info (fun-info-or-lose '%set-symbol-value)))
222 (setf (fun-info-ir2-convert fun-info)
224 (let ((args (basic-combination-args node)))
225 (destructuring-bind (symbol value) args
226 (let ((value-tn (lvar-tn node block value)))
228 (lvar-tn node block symbol) value-tn)
230 node block (list value-tn) (node-lvar node))))))))
232 ;;; Stack allocation optimizers per platform support
233 #!+stack-allocatable-vectors
235 (defoptimizer (allocate-vector stack-allocate-result)
236 ((type length words) node dx)
238 ;; Can't put unboxed data on the stack unless we scavenge it
240 #!-c-stack-is-control-stack
241 (constant-lvar-p type)
242 #!-c-stack-is-control-stack
243 (member (lvar-value type)
244 '#.(list (sb!vm:saetp-typecode (find-saetp 't))
245 (sb!vm:saetp-typecode (find-saetp 'fixnum))))
246 (or (eq dx :always-dynamic)
247 (zerop (policy node safety))
248 ;; a vector object should fit in one page -- otherwise it might go past
249 ;; stack guard pages.
250 (values-subtypep (lvar-derived-type words)
252 (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-bytes*
254 sb!vm:vector-data-offset))))))))
256 (defoptimizer (allocate-vector ltn-annotate) ((type length words) call ltn-policy)
257 (let ((args (basic-combination-args call))
258 (template (template-or-lose (if (awhen (node-lvar call)
259 (lvar-dynamic-extent it))
260 'sb!vm::allocate-vector-on-stack
261 'sb!vm::allocate-vector-on-heap))))
263 (setf (lvar-info arg)
264 (make-ir2-lvar (primitive-type (lvar-type arg)))))
265 (unless (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
266 (ltn-default-call call)
267 (return-from allocate-vector-ltn-annotate-optimizer (values)))
268 (setf (basic-combination-info call) template)
269 (setf (node-tail-p call) nil)
272 (annotate-1-value-lvar arg)))))
275 #!+stack-allocatable-lists
277 (defoptimizer (list stack-allocate-result) ((&rest args) node dx)
278 (declare (ignore node dx))
280 (defoptimizer (list* stack-allocate-result) ((&rest args) node dx)
281 (declare (ignore node dx))
282 (not (null (rest args))))
283 (defoptimizer (%listify-rest-args stack-allocate-result) ((&rest args) node dx)
284 (declare (ignore node dx))
288 #!+stack-allocatable-fixed-objects
290 (defoptimizer (cons stack-allocate-result) ((&rest args) node dx)
292 (defoptimizer (%make-complex stack-allocate-result) ((&rest args) node dx)