1 ;;;; allocation VOPs for the x86-64
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
14 ;;;; CONS, LIST and LIST*
15 (defoptimizer (cons stack-allocate-result) ((&rest args))
17 (defoptimizer (list stack-allocate-result) ((&rest args))
19 (defoptimizer (list* stack-allocate-result) ((&rest args))
20 (not (null (rest args))))
22 (define-vop (list-or-list*)
23 (:args (things :more t))
24 (:temporary (:sc unsigned-reg) ptr temp)
25 (:temporary (:sc unsigned-reg :to (:result 0) :target result) res)
27 (:results (result :scs (descriptor-reg)))
33 ;; (move result nil-value)
34 (inst mov result nil-value))
36 (move result (tn-ref-tn things)))
39 ((store-car (tn list &optional (slot cons-car-slot))
42 ((any-reg descriptor-reg) ,tn)
46 (storew reg ,list ,slot list-pointer-lowtag))))
47 (let ((cons-cells (if star (1- num) num)))
49 (allocation res (* (pad-data-block cons-size) cons-cells) node
50 (awhen (sb!c::node-lvar node)
51 (sb!c::lvar-dynamic-extent it)))
53 (make-ea :byte :base res :disp list-pointer-lowtag))
55 (dotimes (i (1- cons-cells))
56 (store-car (tn-ref-tn things) ptr)
57 (setf things (tn-ref-across things))
58 (inst add ptr (pad-data-block cons-size))
59 (storew ptr ptr (- cons-cdr-slot cons-size)
61 (store-car (tn-ref-tn things) ptr)
63 (setf things (tn-ref-across things))
64 (store-car (tn-ref-tn things) ptr cons-cdr-slot))
66 (storew nil-value ptr cons-cdr-slot
67 list-pointer-lowtag)))
68 (aver (null (tn-ref-across things)))))
69 (move result res))))))
71 (define-vop (list list-or-list*)
74 (define-vop (list* list-or-list*)
77 ;;;; special-purpose inline allocators
80 (define-vop (allocate-vector-on-heap)
81 (:args (type :scs (unsigned-reg))
82 (length :scs (any-reg))
83 (words :scs (any-reg)))
84 (:results (result :scs (descriptor-reg) :from :load))
85 (:arg-types positive-fixnum
90 (inst lea result (make-ea :byte :base words :disp
91 (+ (1- (ash 1 n-lowtag-bits))
92 (* vector-data-offset n-word-bytes))))
93 (inst and result (lognot lowtag-mask))
95 (allocation result result)
96 (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
97 (storew type result 0 other-pointer-lowtag)
98 (storew length result vector-length-slot other-pointer-lowtag))))
100 (define-vop (allocate-vector-on-stack)
101 (:args (type :scs (unsigned-reg))
102 (length :scs (any-reg))
103 (words :scs (any-reg) :target ecx))
104 (:temporary (:sc any-reg :offset ecx-offset :from (:argument 2)) ecx)
105 (:temporary (:sc any-reg :offset eax-offset :from (:argument 2)) zero)
106 (:temporary (:sc any-reg :offset edi-offset :from (:argument 0)) res)
107 (:results (result :scs (descriptor-reg) :from :load))
108 (:arg-types positive-fixnum
111 (:translate allocate-vector)
115 (inst lea result (make-ea :byte :base words :disp
116 (+ (1- (ash 1 n-lowtag-bits))
117 (* vector-data-offset n-word-bytes))))
118 (inst and result (lognot lowtag-mask))
119 ;; FIXME: It would be good to check for stack overflow here.
121 (inst shr ecx n-fixnum-tag-bits)
122 (allocation result result node t)
125 (make-ea :byte :base result :disp (* vector-data-offset n-word-bytes)))
126 (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
127 (storew type result 0 other-pointer-lowtag)
128 (storew length result vector-length-slot other-pointer-lowtag)
135 (defoptimizer (allocate-vector stack-allocate-result)
136 ((type length words) node)
137 (ecase (policy node stack-allocate-vector)
140 ;; a vector object should fit in one page
141 (values-subtypep (lvar-derived-type words)
143 (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-size*
145 sb!vm:vector-data-offset))))))
148 (defoptimizer (allocate-vector ltn-annotate) ((type length words) call ltn-policy)
149 (let ((args (basic-combination-args call))
150 (template (template-or-lose (if (awhen (node-lvar call)
151 (lvar-dynamic-extent it))
152 'sb!vm::allocate-vector-on-stack
153 'sb!vm::allocate-vector-on-heap))))
155 (setf (lvar-info arg)
156 (make-ir2-lvar (primitive-type (lvar-type arg)))))
157 (unless (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
158 (ltn-default-call call)
159 (return-from allocate-vector-ltn-annotate-optimizer (values)))
160 (setf (basic-combination-info call) template)
161 (setf (node-tail-p call) nil)
164 (annotate-1-value-lvar arg))))
169 (define-vop (allocate-code-object)
170 (:args (boxed-arg :scs (any-reg) :target boxed)
171 (unboxed-arg :scs (any-reg) :target unboxed))
172 (:results (result :scs (descriptor-reg) :from :eval))
173 (:temporary (:sc unsigned-reg :from (:argument 0)) boxed)
174 (:temporary (:sc unsigned-reg :from (:argument 1)) unboxed)
177 (move boxed boxed-arg)
178 (inst add boxed (fixnumize (1+ code-trace-table-offset-slot)))
179 (inst and boxed (lognot lowtag-mask))
180 (move unboxed unboxed-arg)
181 (inst shr unboxed word-shift)
182 (inst add unboxed lowtag-mask)
183 (inst and unboxed (lognot lowtag-mask))
184 (inst mov result boxed)
185 (inst add result unboxed)
187 (allocation result result node)
188 (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
189 (inst shl boxed (- n-widetag-bits word-shift))
190 (inst or boxed code-header-widetag)
191 (storew boxed result 0 other-pointer-lowtag)
192 (storew unboxed result code-code-size-slot other-pointer-lowtag)
193 (storew nil-value result code-entry-points-slot other-pointer-lowtag))
194 (storew nil-value result code-debug-info-slot other-pointer-lowtag)))
196 (define-vop (make-fdefn)
198 (:translate make-fdefn)
199 (:args (name :scs (descriptor-reg) :to :eval))
200 (:results (result :scs (descriptor-reg) :from :argument))
203 (with-fixed-allocation (result fdefn-widetag fdefn-size node)
204 (storew name result fdefn-name-slot other-pointer-lowtag)
205 (storew nil-value result fdefn-fun-slot other-pointer-lowtag)
206 (storew (make-fixup "undefined_tramp" :foreign)
207 result fdefn-raw-addr-slot other-pointer-lowtag))))
209 (define-vop (make-closure)
210 (:args (function :to :save :scs (descriptor-reg)))
211 (:info length stack-allocate-p)
212 (:temporary (:sc any-reg) temp)
213 (:results (result :scs (descriptor-reg)))
216 (maybe-pseudo-atomic stack-allocate-p
217 (let ((size (+ length closure-info-offset)))
218 (allocation result (pad-data-block size) node stack-allocate-p)
220 (make-ea :byte :base result :disp fun-pointer-lowtag))
221 (storew (logior (ash (1- size) n-widetag-bits) closure-header-widetag)
222 result 0 fun-pointer-lowtag))
223 (loadw temp function closure-fun-slot fun-pointer-lowtag)
224 (storew temp result closure-fun-slot fun-pointer-lowtag))))
226 ;;; The compiler likes to be able to directly make value cells.
227 (define-vop (make-value-cell)
228 (:args (value :scs (descriptor-reg any-reg) :to :result))
229 (:results (result :scs (descriptor-reg) :from :eval))
230 (:info stack-allocate-p)
233 (with-fixed-allocation
234 (result value-cell-header-widetag value-cell-size node stack-allocate-p)
235 (storew value result value-cell-value-slot other-pointer-lowtag))))
237 ;;;; automatic allocators for primitive objects
239 (define-vop (make-unbound-marker)
241 (:results (result :scs (any-reg)))
243 (inst mov result unbound-marker-widetag)))
245 (define-vop (make-funcallable-instance-tramp)
247 (:results (result :scs (any-reg)))
249 (inst lea result (make-fixup "funcallable_instance_tramp" :foreign))))
251 (define-vop (fixed-alloc)
253 (:info name words type lowtag stack-allocate-p)
255 (:results (result :scs (descriptor-reg)))
259 (allocation result (pad-data-block words) node stack-allocate-p)
260 (inst lea result (make-ea :byte :base result :disp lowtag))
262 (storew (logior (ash (1- words) n-widetag-bits) type)
267 (define-vop (var-alloc)
268 (:args (extra :scs (any-reg)))
269 (:arg-types positive-fixnum)
270 (:info name words type lowtag)
272 (:results (result :scs (descriptor-reg) :from (:eval 1)))
273 (:temporary (:sc any-reg :from :eval :to (:eval 1)) bytes)
274 (:temporary (:sc any-reg :from :eval :to :result) header)
278 (make-ea :qword :base extra :disp (* (1+ words) n-word-bytes)))
279 (inst mov header bytes)
280 (inst shl header (- n-widetag-bits 3)) ; w+1 to length field
281 (inst lea header ; (w-1 << 8) | type
282 (make-ea :qword :base header :disp (+ (ash -2 n-widetag-bits) type)))
283 (inst and bytes (lognot lowtag-mask))
285 (allocation result bytes node)
286 (inst lea result (make-ea :byte :base result :disp lowtag))
287 (storew header result 0 lowtag))))
289 (define-vop (%make-symbol)
291 (:translate %make-symbol)
292 (:args (name :scs (descriptor-reg) :to :eval))
293 (:temporary (:sc unsigned-reg :from :eval) temp)
294 (:results (result :scs (descriptor-reg) :from :argument))
297 (with-fixed-allocation (result symbol-header-widetag symbol-size node)
298 (storew name result symbol-name-slot other-pointer-lowtag)
299 (storew unbound-marker-widetag
302 other-pointer-lowtag)
303 ;; Set up a random hash value for the symbol. Perhaps the object
304 ;; address could be used for even faster and smaller code!
305 ;; FIXME: We don't mind the symbol hash not being repeatable, so
306 ;; we might as well add in the object address here, too. (Adding entropy
307 ;; is good, even if ANSI doesn't understand that.)
309 (make-fixup "fast_random_state" :foreign)
311 (inst add temp 12345)
312 (inst mov (make-fixup "fast_random_state" :foreign)
314 ;; We want a positive fixnum for the hash value, so discard the LS bits.
316 ;; FIXME: OK, who wants to tell me (CSR) why these two
317 ;; instructions aren't replaced by (INST AND TEMP #x8FFFFFFC)?
318 ;; Are the following two instructions actually faster? Does the
319 ;; difference in behaviour really matter?
321 (inst and temp #xfffffffc)
322 (storew temp result symbol-hash-slot other-pointer-lowtag)
323 (storew nil-value result symbol-plist-slot other-pointer-lowtag)
324 (storew nil-value result symbol-package-slot other-pointer-lowtag))))