1.0.18.25: tweak stack allocation on x86 and x86-64
[sbcl/pkhuong.git] / src / compiler / x86-64 / alloc.lisp
blobe0c521170b8f3d8c026b563b0f2056ecdd2168d1
1 ;;;; allocation VOPs for the x86-64
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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.
12 (in-package "SB!VM")
14 ;;;; CONS, LIST and LIST*
15 (defoptimizer (cons stack-allocate-result) ((&rest args))
17 (defoptimizer (list stack-allocate-result) ((&rest args))
18 (not (null 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)
26 (:info num)
27 (:results (result :scs (descriptor-reg)))
28 (:variant-vars star)
29 (:policy :safe)
30 (:node-var node)
31 (:generator 0
32 (cond ((zerop num)
33 ;; (move result nil-value)
34 (inst mov result nil-value))
35 ((and star (= num 1))
36 (move result (tn-ref-tn things)))
38 (macrolet
39 ((store-car (tn list &optional (slot cons-car-slot))
40 `(let ((reg
41 (sc-case ,tn
42 ((any-reg descriptor-reg) ,tn)
43 ((control-stack)
44 (move temp ,tn)
45 temp))))
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)
53 (move ptr res)
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)
59 list-pointer-lowtag))
60 (store-car (tn-ref-tn things) ptr)
61 (cond (star
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*)
71 (:variant nil))
73 (define-vop (list* list-or-list*)
74 (:variant t))
76 ;;;; special-purpose inline allocators
78 ;;; ALLOCATE-VECTOR
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
85 positive-fixnum
86 positive-fixnum)
87 (:policy :fast-safe)
88 (:generator 100
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))
93 (pseudo-atomic
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
108 positive-fixnum
109 positive-fixnum)
110 (:translate allocate-vector)
111 (:policy :fast-safe)
112 (:node-var node)
113 (:generator 100
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.
119 (move ecx words)
120 (inst shr ecx n-fixnum-tag-bits)
121 (allocation result result node t other-pointer-lowtag)
122 (inst cld)
123 (inst lea res
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)
128 (zeroize zero)
129 (inst rep)
130 (inst stos zero)))
132 (in-package "SB!C")
134 (defoptimizer (allocate-vector stack-allocate-result)
135 ((type length words) node)
136 (ecase (policy node stack-allocate-vector)
137 (0 nil)
138 ((1 2)
139 ;; a vector object should fit in one page
140 (values-subtypep (lvar-derived-type words)
141 (load-time-value
142 (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-size*
143 sb!vm:n-word-bytes)
144 sb!vm:vector-data-offset))))))
145 (3 t)))
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))))
153 (dolist (arg args)
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)
162 (dolist (arg args)
163 (annotate-1-value-lvar arg))))
165 (in-package "SB!VM")
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)
174 (:node-var node)
175 (:generator 100
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)
185 (pseudo-atomic
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)
196 (:policy :fast-safe)
197 (:translate make-fdefn)
198 (:args (name :scs (descriptor-reg) :to :eval))
199 (:results (result :scs (descriptor-reg) :from :argument))
200 (:node-var node)
201 (:generator 37
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)))
213 (:node-var node)
214 (:generator 10
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
218 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))
228 (:info stack-allocate-p)
229 (:node-var node)
230 (:generator 10
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)
238 (:args)
239 (:results (result :scs (any-reg)))
240 (:generator 1
241 (inst mov result unbound-marker-widetag)))
243 (define-vop (make-funcallable-instance-tramp)
244 (:args)
245 (:results (result :scs (any-reg)))
246 (:generator 1
247 (inst lea result (make-fixup "funcallable_instance_tramp" :foreign))))
249 (define-vop (fixed-alloc)
250 (:args)
251 (:info name words type lowtag stack-allocate-p)
252 (:ignore name)
253 (:results (result :scs (descriptor-reg)))
254 (:node-var node)
255 (:generator 50
256 (maybe-pseudo-atomic stack-allocate-p
257 (allocation result (pad-data-block words) node stack-allocate-p lowtag)
258 (when type
259 (storew (logior (ash (1- words) n-widetag-bits) type)
260 result
262 lowtag)))))
264 (define-vop (var-alloc)
265 (:args (extra :scs (any-reg)))
266 (:arg-types positive-fixnum)
267 (:info name words type lowtag)
268 (:ignore name)
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)
272 (:node-var node)
273 (:generator 50
274 (inst lea bytes
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))
281 (pseudo-atomic
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)
287 (:policy :fast-safe)
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))
292 (:node-var node)
293 (:generator 37
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
297 result
298 symbol-value-slot
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.)
305 (inst imul temp
306 (make-fixup "fast_random_state" :foreign)
307 1103515245)
308 (inst add temp 12345)
309 (inst mov (make-fixup "fast_random_state" :foreign)
310 temp)
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?
317 (inst shr temp 1)
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))))