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
))
48 (stack-allocate-p (awhen (sb!c
::node-lvar node
)
49 (sb!c
::lvar-dynamic-extent it
))))
50 (maybe-pseudo-atomic stack-allocate-p
51 (allocation res
(* (pad-data-block cons-size
) cons-cells
) node
52 stack-allocate-p list-pointer-lowtag
)
54 (dotimes (i (1- cons-cells
))
55 (store-car (tn-ref-tn things
) ptr
)
56 (setf things
(tn-ref-across things
))
57 (inst add ptr
(pad-data-block cons-size
))
58 (storew ptr ptr
(- cons-cdr-slot cons-size
)
60 (store-car (tn-ref-tn things
) ptr
)
62 (setf things
(tn-ref-across things
))
63 (store-car (tn-ref-tn things
) ptr cons-cdr-slot
))
65 (storew nil-value ptr cons-cdr-slot
66 list-pointer-lowtag
)))
67 (aver (null (tn-ref-across things
)))))
68 (move result res
))))))
70 (define-vop (list list-or-list
*)
73 (define-vop (list* list-or-list
*)
76 ;;;; special-purpose inline allocators
79 (define-vop (allocate-vector-on-heap)
80 (:args
(type :scs
(unsigned-reg))
81 (length :scs
(any-reg))
82 (words :scs
(any-reg)))
83 (:results
(result :scs
(descriptor-reg) :from
:load
))
84 (:arg-types positive-fixnum
89 (inst lea result
(make-ea :byte
:base words
:disp
90 (+ (1- (ash 1 n-lowtag-bits
))
91 (* vector-data-offset n-word-bytes
))))
92 (inst and result
(lognot lowtag-mask
))
94 (allocation result result
)
95 (inst lea result
(make-ea :byte
:base result
:disp other-pointer-lowtag
))
96 (storew type result
0 other-pointer-lowtag
)
97 (storew length result vector-length-slot other-pointer-lowtag
))))
99 (define-vop (allocate-vector-on-stack)
100 (:args
(type :scs
(unsigned-reg))
101 (length :scs
(any-reg))
102 (words :scs
(any-reg) :target ecx
))
103 (:temporary
(:sc any-reg
:offset ecx-offset
:from
(:argument
2)) ecx
)
104 (:temporary
(:sc any-reg
:offset eax-offset
:from
(:argument
2)) zero
)
105 (:temporary
(:sc any-reg
:offset edi-offset
:from
(:argument
0)) res
)
106 (:results
(result :scs
(descriptor-reg) :from
:load
))
107 (:arg-types positive-fixnum
110 (:translate allocate-vector
)
114 (inst lea result
(make-ea :byte
:base words
:disp
115 (+ (1- (ash 1 n-lowtag-bits
))
116 (* vector-data-offset n-word-bytes
))))
117 (inst and result
(lognot lowtag-mask
))
118 ;; FIXME: It would be good to check for stack overflow here.
120 (inst shr ecx n-fixnum-tag-bits
)
121 (allocation result result node t other-pointer-lowtag
)
124 (make-ea :byte
:base result
:disp
(- (* vector-data-offset n-word-bytes
)
125 other-pointer-lowtag
)))
126 (storew type result
0 other-pointer-lowtag
)
127 (storew length result vector-length-slot other-pointer-lowtag
)
134 (defoptimizer (allocate-vector stack-allocate-result
)
135 ((type length words
) node
)
136 (ecase (policy node stack-allocate-vector
)
139 ;; a vector object should fit in one page
140 (values-subtypep (lvar-derived-type words
)
142 (specifier-type `(integer 0 ,(- (/ sb
!vm
::*backend-page-size
*
144 sb
!vm
:vector-data-offset
))))))
147 (defoptimizer (allocate-vector ltn-annotate
) ((type length words
) call ltn-policy
)
148 (let ((args (basic-combination-args call
))
149 (template (template-or-lose (if (awhen (node-lvar call
)
150 (lvar-dynamic-extent it
))
151 'sb
!vm
::allocate-vector-on-stack
152 'sb
!vm
::allocate-vector-on-heap
))))
154 (setf (lvar-info arg
)
155 (make-ir2-lvar (primitive-type (lvar-type arg
)))))
156 (unless (is-ok-template-use template call
(ltn-policy-safe-p ltn-policy
))
157 (ltn-default-call call
)
158 (return-from allocate-vector-ltn-annotate-optimizer
(values)))
159 (setf (basic-combination-info call
) template
)
160 (setf (node-tail-p call
) nil
)
163 (annotate-1-value-lvar arg
))))
168 (define-vop (allocate-code-object)
169 (:args
(boxed-arg :scs
(any-reg) :target boxed
)
170 (unboxed-arg :scs
(any-reg) :target unboxed
))
171 (:results
(result :scs
(descriptor-reg) :from
:eval
))
172 (:temporary
(:sc unsigned-reg
:from
(:argument
0)) boxed
)
173 (:temporary
(:sc unsigned-reg
:from
(:argument
1)) unboxed
)
176 (move boxed boxed-arg
)
177 (inst add boxed
(fixnumize (1+ code-trace-table-offset-slot
)))
178 (inst and boxed
(lognot lowtag-mask
))
179 (move unboxed unboxed-arg
)
180 (inst shr unboxed word-shift
)
181 (inst add unboxed lowtag-mask
)
182 (inst and unboxed
(lognot lowtag-mask
))
183 (inst mov result boxed
)
184 (inst add result unboxed
)
186 (allocation result result node
)
187 (inst lea result
(make-ea :byte
:base result
:disp other-pointer-lowtag
))
188 (inst shl boxed
(- n-widetag-bits word-shift
))
189 (inst or boxed code-header-widetag
)
190 (storew boxed result
0 other-pointer-lowtag
)
191 (storew unboxed result code-code-size-slot other-pointer-lowtag
)
192 (storew nil-value result code-entry-points-slot other-pointer-lowtag
))
193 (storew nil-value result code-debug-info-slot other-pointer-lowtag
)))
195 (define-vop (make-fdefn)
197 (:translate make-fdefn
)
198 (:args
(name :scs
(descriptor-reg) :to
:eval
))
199 (:results
(result :scs
(descriptor-reg) :from
:argument
))
202 (with-fixed-allocation (result fdefn-widetag fdefn-size node
)
203 (storew name result fdefn-name-slot other-pointer-lowtag
)
204 (storew nil-value result fdefn-fun-slot other-pointer-lowtag
)
205 (storew (make-fixup "undefined_tramp" :foreign
)
206 result fdefn-raw-addr-slot other-pointer-lowtag
))))
208 (define-vop (make-closure)
209 (:args
(function :to
:save
:scs
(descriptor-reg)))
210 (:info length stack-allocate-p
)
211 (:temporary
(:sc any-reg
) temp
)
212 (:results
(result :scs
(descriptor-reg)))
215 (maybe-pseudo-atomic stack-allocate-p
216 (let ((size (+ length closure-info-offset
)))
217 (allocation result
(pad-data-block size
) node stack-allocate-p
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
))
228 (:info stack-allocate-p
)
231 (with-fixed-allocation
232 (result value-cell-header-widetag value-cell-size node stack-allocate-p
)
233 (storew value result value-cell-value-slot other-pointer-lowtag
))))
235 ;;;; automatic allocators for primitive objects
237 (define-vop (make-unbound-marker)
239 (:results
(result :scs
(any-reg)))
241 (inst mov result unbound-marker-widetag
)))
243 (define-vop (make-funcallable-instance-tramp)
245 (:results
(result :scs
(any-reg)))
247 (inst lea result
(make-fixup "funcallable_instance_tramp" :foreign
))))
249 (define-vop (fixed-alloc)
251 (:info name words type lowtag stack-allocate-p
)
253 (:results
(result :scs
(descriptor-reg)))
256 (maybe-pseudo-atomic stack-allocate-p
257 (allocation result
(pad-data-block words
) node stack-allocate-p 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
))))