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.
15 (defoptimizer (list stack-allocate-result) ((&rest args))
17 (defoptimizer (list* stack-allocate-result) ((&rest args))
18 (not (null (rest args))))
20 (define-vop (list-or-list*)
21 (:args (things :more t))
22 (:temporary (:sc unsigned-reg) ptr temp)
23 (:temporary (:sc unsigned-reg :to (:result 0) :target result) res)
25 (:results (result :scs (descriptor-reg)))
31 ;; (move result nil-value)
32 (inst mov result nil-value))
34 (move result (tn-ref-tn things)))
37 ((store-car (tn list &optional (slot cons-car-slot))
40 ((any-reg descriptor-reg) ,tn)
44 (storew reg ,list ,slot list-pointer-lowtag))))
45 (let ((cons-cells (if star (1- num) num)))
47 (allocation res (* (pad-data-block cons-size) cons-cells) node
48 (awhen (sb!c::node-lvar node)
49 (sb!c::lvar-dynamic-extent it)))
51 (make-ea :byte :base res :disp list-pointer-lowtag))
53 (dotimes (i (1- cons-cells))
54 (store-car (tn-ref-tn things) ptr)
55 (setf things (tn-ref-across things))
56 (inst add ptr (pad-data-block cons-size))
57 (storew ptr ptr (- cons-cdr-slot cons-size)
59 (store-car (tn-ref-tn things) ptr)
61 (setf things (tn-ref-across things))
62 (store-car (tn-ref-tn things) ptr cons-cdr-slot))
64 (storew nil-value ptr cons-cdr-slot
65 list-pointer-lowtag)))
66 (aver (null (tn-ref-across things)))))
67 (move result res))))))
69 (define-vop (list list-or-list*)
72 (define-vop (list* list-or-list*)
75 ;;;; special-purpose inline allocators
78 (define-vop (allocate-vector-on-heap)
79 (:args (type :scs (unsigned-reg))
80 (length :scs (any-reg))
81 (words :scs (any-reg)))
82 (:results (result :scs (descriptor-reg) :from :load))
83 (:arg-types positive-fixnum
88 (inst lea result (make-ea :byte :base words :disp
89 (+ (1- (ash 1 n-lowtag-bits))
90 (* vector-data-offset n-word-bytes))))
91 (inst and result (lognot lowtag-mask))
93 (allocation result result)
94 (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
95 (storew type result 0 other-pointer-lowtag)
96 (storew length result vector-length-slot other-pointer-lowtag))))
98 (define-vop (allocate-vector-on-stack)
99 (:args (type :scs (unsigned-reg))
100 (length :scs (any-reg))
101 (words :scs (any-reg) :target ecx))
102 (:temporary (:sc any-reg :offset ecx-offset :from (:argument 2)) ecx)
103 (:temporary (:sc any-reg :offset eax-offset :from (:argument 2)) zero)
104 (:temporary (:sc any-reg :offset edi-offset :from (:argument 0)) res)
105 (:results (result :scs (descriptor-reg) :from :load))
106 (:arg-types positive-fixnum
109 (:translate allocate-vector)
113 (inst lea result (make-ea :byte :base words :disp
114 (+ (1- (ash 1 n-lowtag-bits))
115 (* vector-data-offset n-word-bytes))))
116 (inst and result (lognot lowtag-mask))
117 ;; FIXME: It would be good to check for stack overflow here.
119 (inst shr ecx n-fixnum-tag-bits)
120 (allocation result result node t)
123 (make-ea :byte :base result :disp (* vector-data-offset n-word-bytes)))
124 (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
125 (storew type result 0 other-pointer-lowtag)
126 (storew length result vector-length-slot other-pointer-lowtag)
133 (defoptimizer (allocate-vector stack-allocate-result)
134 ((type length words) node)
135 (ecase (policy node stack-allocate-vector)
138 ;; a vector object should fit in one page
139 (values-subtypep (lvar-derived-type words)
141 (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-size*
143 sb!vm:vector-data-offset))))))
146 (defoptimizer (allocate-vector ltn-annotate) ((type length words) call ltn-policy)
147 (let ((args (basic-combination-args call))
148 (template (template-or-lose (if (awhen (node-lvar call)
149 (lvar-dynamic-extent it))
150 'sb!vm::allocate-vector-on-stack
151 'sb!vm::allocate-vector-on-heap))))
153 (setf (lvar-info arg)
154 (make-ir2-lvar (primitive-type (lvar-type arg)))))
155 (unless (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
156 (ltn-default-call call)
157 (return-from allocate-vector-ltn-annotate-optimizer (values)))
158 (setf (basic-combination-info call) template)
159 (setf (node-tail-p call) nil)
162 (annotate-1-value-lvar arg))))
167 (define-vop (allocate-code-object)
168 (:args (boxed-arg :scs (any-reg) :target boxed)
169 (unboxed-arg :scs (any-reg) :target unboxed))
170 (:results (result :scs (descriptor-reg) :from :eval))
171 (:temporary (:sc unsigned-reg :from (:argument 0)) boxed)
172 (:temporary (:sc unsigned-reg :from (:argument 1)) unboxed)
175 (move boxed boxed-arg)
176 (inst add boxed (fixnumize (1+ code-trace-table-offset-slot)))
177 (inst and boxed (lognot lowtag-mask))
178 (move unboxed unboxed-arg)
179 (inst shr unboxed word-shift)
180 (inst add unboxed lowtag-mask)
181 (inst and unboxed (lognot lowtag-mask))
182 (inst mov result boxed)
183 (inst add result unboxed)
185 (allocation result result node)
186 (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
187 (inst shl boxed (- n-widetag-bits word-shift))
188 (inst or boxed code-header-widetag)
189 (storew boxed result 0 other-pointer-lowtag)
190 (storew unboxed result code-code-size-slot other-pointer-lowtag)
191 (storew nil-value result code-entry-points-slot other-pointer-lowtag))
192 (storew nil-value result code-debug-info-slot other-pointer-lowtag)))
194 (define-vop (make-fdefn)
196 (:translate make-fdefn)
197 (:args (name :scs (descriptor-reg) :to :eval))
198 (:results (result :scs (descriptor-reg) :from :argument))
201 (with-fixed-allocation (result fdefn-widetag fdefn-size node)
202 (storew name result fdefn-name-slot other-pointer-lowtag)
203 (storew nil-value result fdefn-fun-slot other-pointer-lowtag)
204 (storew (make-fixup "undefined_tramp" :foreign)
205 result fdefn-raw-addr-slot other-pointer-lowtag))))
207 (define-vop (make-closure)
208 (:args (function :to :save :scs (descriptor-reg)))
209 (:info length stack-allocate-p)
210 (:temporary (:sc any-reg) temp)
211 (:results (result :scs (descriptor-reg)))
214 (maybe-pseudo-atomic stack-allocate-p
215 (let ((size (+ length closure-info-offset)))
216 (allocation result (pad-data-block size) node stack-allocate-p)
218 (make-ea :byte :base result :disp fun-pointer-lowtag))
219 (storew (logior (ash (1- size) n-widetag-bits) closure-header-widetag)
220 result 0 fun-pointer-lowtag))
221 (loadw temp function closure-fun-slot fun-pointer-lowtag)
222 (storew temp result closure-fun-slot fun-pointer-lowtag))))
224 ;;; The compiler likes to be able to directly make value cells.
225 (define-vop (make-value-cell)
226 (:args (value :scs (descriptor-reg any-reg) :to :result))
227 (:results (result :scs (descriptor-reg) :from :eval))
230 (with-fixed-allocation
231 (result value-cell-header-widetag value-cell-size node)
232 (storew value result value-cell-value-slot other-pointer-lowtag))))
234 ;;;; automatic allocators for primitive objects
236 (define-vop (make-unbound-marker)
238 (:results (result :scs (any-reg)))
240 (inst mov result unbound-marker-widetag)))
242 (define-vop (make-funcallable-instance-tramp)
244 (:results (result :scs (any-reg)))
246 (inst lea result (make-fixup "funcallable_instance_tramp" :foreign))))
248 (define-vop (fixed-alloc)
250 (:info name words type lowtag)
252 (:results (result :scs (descriptor-reg)))
256 (allocation result (pad-data-block words) node)
257 (inst lea result (make-ea :byte :base result :disp lowtag))
259 (storew (logior (ash (1- words) n-widetag-bits) type)
264 (define-vop (var-alloc)
265 (:args (extra :scs (any-reg)))
266 (:arg-types positive-fixnum)
267 (:info name words type lowtag)
269 (:results (result :scs (descriptor-reg) :from (:eval 1)))
270 (:temporary (:sc any-reg :from :eval :to (:eval 1)) bytes)
271 (:temporary (:sc any-reg :from :eval :to :result) header)
275 (make-ea :qword :base extra :disp (* (1+ words) n-word-bytes)))
276 (inst mov header bytes)
277 (inst shl header (- n-widetag-bits 3)) ; w+1 to length field
278 (inst lea header ; (w-1 << 8) | type
279 (make-ea :qword :base header :disp (+ (ash -2 n-widetag-bits) type)))
280 (inst and bytes (lognot lowtag-mask))
282 (allocation result bytes node)
283 (inst lea result (make-ea :byte :base result :disp lowtag))
284 (storew header result 0 lowtag))))
286 (define-vop (%make-symbol)
288 (:translate %make-symbol)
289 (:args (name :scs (descriptor-reg) :to :eval))
290 (:temporary (:sc unsigned-reg :from :eval) temp)
291 (:results (result :scs (descriptor-reg) :from :argument))
294 (with-fixed-allocation (result symbol-header-widetag symbol-size node)
295 (storew name result symbol-name-slot other-pointer-lowtag)
296 (storew unbound-marker-widetag
299 other-pointer-lowtag)
300 ;; Set up a random hash value for the symbol. Perhaps the object
301 ;; address could be used for even faster and smaller code!
302 ;; FIXME: We don't mind the symbol hash not being repeatable, so
303 ;; we might as well add in the object address here, too. (Adding entropy
304 ;; is good, even if ANSI doesn't understand that.)
306 (make-fixup "fast_random_state" :foreign)
308 (inst add temp 12345)
309 (inst mov (make-fixup "fast_random_state" :foreign)
311 ;; We want a positive fixnum for the hash value, so discard the LS bits.
313 ;; FIXME: OK, who wants to tell me (CSR) why these two
314 ;; instructions aren't replaced by (INST AND TEMP #x8FFFFFFC)?
315 ;; Are the following two instructions actually faster? Does the
316 ;; difference in behaviour really matter?
318 (inst and temp #xfffffffc)
319 (storew temp result symbol-hash-slot other-pointer-lowtag)
320 (storew nil-value result symbol-plist-slot other-pointer-lowtag)
321 (storew nil-value result symbol-package-slot other-pointer-lowtag))))