67ea2a2ca0cd33f7d61c94ac95d3bce033f1ae95
[sbcl.git] / src / compiler / x86 / alloc.lisp
1 ;;;; allocation VOPs for the x86
2
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.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; CONS, LIST and LIST*
15 (defoptimizer (cons stack-allocate-result) ((&rest args))
16   t)
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))))
21
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)))
37           (t
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))
64                       (t
65                        (storew nil-value ptr cons-cdr-slot
66                                list-pointer-lowtag)))
67                 (aver (null (tn-ref-across things)))))
68              (move result res))))))
69
70 (define-vop (list list-or-list*)
71   (:variant nil))
72
73 (define-vop (list* list-or-list*)
74   (:variant t))
75 \f
76 ;;;; special-purpose inline allocators
77
78 ;;; ALLOCATE-VECTOR
79 (define-vop (allocate-vector-on-heap)
80   (:args (type :scs (unsigned-reg immediate))
81          (length :scs (any-reg immediate))
82          (words :scs (any-reg immediate)))
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     (let ((size (sc-case words
90                   (immediate
91                    (logandc2 (+ (fixnumize (tn-value words))
92                                 (+ (1- (ash 1 n-lowtag-bits))
93                                    (* vector-data-offset n-word-bytes)))
94                              lowtag-mask))
95                   (t
96                    (inst lea result (make-ea :byte :base words :disp
97                                              (+ (1- (ash 1 n-lowtag-bits))
98                                                 (* vector-data-offset
99                                                    n-word-bytes))))
100                    (inst and result (lognot lowtag-mask))
101                    result))))
102       (pseudo-atomic
103        (allocation result size)
104        (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
105        (sc-case type
106          (immediate
107           (aver (typep (tn-value type) '(unsigned-byte 8)))
108           (storeb (tn-value type) result 0 other-pointer-lowtag))
109          (t
110           (storew type result 0 other-pointer-lowtag)))
111        (sc-case length
112          (immediate
113           (let ((fixnum-length (fixnumize (tn-value length))))
114             (typecase fixnum-length
115               ((unsigned-byte 8)
116                (storeb fixnum-length result
117                        vector-length-slot other-pointer-lowtag))
118               (t
119                (storew fixnum-length result
120                        vector-length-slot other-pointer-lowtag)))))
121          (t
122           (storew length result vector-length-slot other-pointer-lowtag)))))))
123
124 (define-vop (allocate-vector-on-stack)
125   (:args (type :scs (unsigned-reg immediate))
126          (length :scs (any-reg))
127          (words :scs (any-reg) :target ecx))
128   (:temporary (:sc any-reg :offset ecx-offset :from (:argument 2)) ecx)
129   (:temporary (:sc any-reg :offset eax-offset :from (:argument 2)) zero)
130   (:temporary (:sc any-reg :offset edi-offset :from (:argument 0)) res)
131   (:results (result :scs (descriptor-reg) :from :load))
132   (:arg-types positive-fixnum
133               positive-fixnum
134               positive-fixnum)
135   (:translate allocate-vector)
136   (:policy :fast-safe)
137   (:node-var node)
138   (:generator 100
139     (inst lea result (make-ea :byte :base words :disp
140                               (+ (1- (ash 1 n-lowtag-bits))
141                                  (* vector-data-offset n-word-bytes))))
142     (inst and result (lognot lowtag-mask))
143     ;; FIXME: It would be good to check for stack overflow here.
144     (move ecx words)
145     (inst shr ecx n-fixnum-tag-bits)
146     (allocation result result node t other-pointer-lowtag)
147     (inst cld)
148     (inst lea res
149           (make-ea :byte :base result :disp (- (* vector-data-offset n-word-bytes)
150                                                other-pointer-lowtag)))
151     (sc-case type
152       (immediate
153        (aver (typep (tn-value type) '(unsigned-byte 8)))
154        (storeb (tn-value type) result 0 other-pointer-lowtag))
155       (t
156        (storew type result 0 other-pointer-lowtag)))
157     (storew length result vector-length-slot other-pointer-lowtag)
158     (inst xor zero zero)
159     (inst rep)
160     (inst stos zero)))
161
162 (in-package "SB!C")
163
164 (defoptimizer (allocate-vector stack-allocate-result)
165     ((type length words) node)
166   (ecase (policy node stack-allocate-vector)
167     (0 nil)
168     ((1 2)
169      ;; a vector object should fit in one page
170      (values-subtypep (lvar-derived-type words)
171                       (load-time-value
172                        (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-size*
173                                                           sb!vm:n-word-bytes)
174                                                        sb!vm:vector-data-offset))))))
175     (3 t)))
176
177 (defoptimizer (allocate-vector ltn-annotate) ((type length words) call ltn-policy)
178   (let ((args (basic-combination-args call))
179         (template (template-or-lose (if (awhen (node-lvar call)
180                                           (lvar-dynamic-extent it))
181                                         'sb!vm::allocate-vector-on-stack
182                                         'sb!vm::allocate-vector-on-heap))))
183     (dolist (arg args)
184       (setf (lvar-info arg)
185             (make-ir2-lvar (primitive-type (lvar-type arg)))))
186     (unless (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
187       (ltn-default-call call)
188       (return-from allocate-vector-ltn-annotate-optimizer (values)))
189     (setf (basic-combination-info call) template)
190     (setf (node-tail-p call) nil)
191
192     (dolist (arg args)
193       (annotate-1-value-lvar arg))))
194
195 (in-package "SB!VM")
196
197 ;;;
198 (define-vop (allocate-code-object)
199   (:args (boxed-arg :scs (any-reg) :target boxed)
200          (unboxed-arg :scs (any-reg) :target unboxed))
201   (:results (result :scs (descriptor-reg) :from :eval))
202   (:temporary (:sc unsigned-reg :from (:argument 0)) boxed)
203   (:temporary (:sc unsigned-reg :from (:argument 1)) unboxed)
204   (:node-var node)
205   (:generator 100
206     (move boxed boxed-arg)
207     (inst add boxed (fixnumize (1+ code-trace-table-offset-slot)))
208     (inst and boxed (lognot lowtag-mask))
209     (move unboxed unboxed-arg)
210     (inst shr unboxed word-shift)
211     (inst add unboxed lowtag-mask)
212     (inst and unboxed (lognot lowtag-mask))
213     (inst mov result boxed)
214     (inst add result unboxed)
215     (pseudo-atomic
216      (allocation result result node)
217      (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
218      (inst shl boxed (- n-widetag-bits word-shift))
219      (inst or boxed code-header-widetag)
220      (storew boxed result 0 other-pointer-lowtag)
221      (storew unboxed result code-code-size-slot other-pointer-lowtag)
222      (storew nil-value result code-entry-points-slot other-pointer-lowtag))
223     (storew nil-value result code-debug-info-slot other-pointer-lowtag)))
224 \f
225 (define-vop (make-fdefn)
226   (:policy :fast-safe)
227   (:translate make-fdefn)
228   (:args (name :scs (descriptor-reg) :to :eval))
229   (:results (result :scs (descriptor-reg) :from :argument))
230   (:node-var node)
231   (:generator 37
232     (with-fixed-allocation (result fdefn-widetag fdefn-size node)
233       (storew name result fdefn-name-slot other-pointer-lowtag)
234       (storew nil-value result fdefn-fun-slot other-pointer-lowtag)
235       (storew (make-fixup "undefined_tramp" :foreign)
236               result fdefn-raw-addr-slot other-pointer-lowtag))))
237
238 (define-vop (make-closure)
239   (:args (function :to :save :scs (descriptor-reg)))
240   (:info length stack-allocate-p)
241   (:temporary (:sc any-reg) temp)
242   (:results (result :scs (descriptor-reg)))
243   (:node-var node)
244   (:generator 10
245    (maybe-pseudo-atomic stack-allocate-p
246      (let ((size (+ length closure-info-offset)))
247        (allocation result (pad-data-block size) node
248                    stack-allocate-p
249                    fun-pointer-lowtag)
250        (storew (logior (ash (1- size) n-widetag-bits) closure-header-widetag)
251                result 0 fun-pointer-lowtag))
252     (loadw temp function closure-fun-slot fun-pointer-lowtag)
253     (storew temp result closure-fun-slot fun-pointer-lowtag))))
254
255 ;;; The compiler likes to be able to directly make value cells.
256 (define-vop (make-value-cell)
257   (:args (value :scs (descriptor-reg any-reg) :to :result))
258   (:results (result :scs (descriptor-reg) :from :eval))
259   (:info stack-allocate-p)
260   (:node-var node)
261   (:generator 10
262     (with-fixed-allocation
263         (result value-cell-header-widetag value-cell-size node stack-allocate-p)
264       (storew value result value-cell-value-slot other-pointer-lowtag))))
265 \f
266 ;;;; automatic allocators for primitive objects
267
268 (define-vop (make-unbound-marker)
269   (:args)
270   (:results (result :scs (any-reg)))
271   (:generator 1
272     (inst mov result unbound-marker-widetag)))
273
274 (define-vop (make-funcallable-instance-tramp)
275   (:args)
276   (:results (result :scs (any-reg)))
277   (:generator 1
278     (inst lea result (make-fixup "funcallable_instance_tramp" :foreign))))
279
280 (define-vop (fixed-alloc)
281   (:args)
282   (:info name words type lowtag stack-allocate-p)
283   (:ignore name)
284   (:results (result :scs (descriptor-reg)))
285   (:node-var node)
286   (:generator 50
287     ;; We special case the allocation of conses, because they're
288     ;; extremely common and because the pseudo-atomic sequence on x86
289     ;; is relatively heavyweight.  However, if the user asks for top
290     ;; speed, we accomodate him.  The primary reason that we don't
291     ;; also check for (< SPEED SPACE) is because we want the space
292     ;; savings that these out-of-line allocation routines bring whilst
293     ;; compiling SBCL itself.  --njf, 2006-07-08
294     (if (and (not stack-allocate-p)
295              (= lowtag list-pointer-lowtag) (policy node (< speed 3)))
296         (let ((dst
297                ;; FIXME: out-of-line dx-allocation
298                #.(loop for offset in *dword-regs*
299                     collect `(,offset
300                               ',(intern (format nil "ALLOCATE-CONS-TO-~A"
301                                                 (svref *dword-register-names*
302                                                        offset)))) into cases
303                     finally (return `(case (tn-offset result)
304                                        ,@cases)))))
305           (aver (null type))
306           (inst call (make-fixup dst :assembly-routine)))
307         (maybe-pseudo-atomic stack-allocate-p
308          (allocation result (pad-data-block words) node stack-allocate-p lowtag)
309          (when type
310            (storew (logior (ash (1- words) n-widetag-bits) type)
311                    result
312                    0
313                    lowtag))))))
314
315 (define-vop (var-alloc)
316   (:args (extra :scs (any-reg)))
317   (:arg-types positive-fixnum)
318   (:info name words type lowtag)
319   (:ignore name)
320   (:results (result :scs (descriptor-reg) :from (:eval 1)))
321   (:temporary (:sc any-reg :from :eval :to (:eval 1)) bytes)
322   (:temporary (:sc any-reg :from :eval :to :result) header)
323   (:node-var node)
324   (:generator 50
325     (inst lea bytes
326           (make-ea :dword :base extra :disp (* (1+ words) n-word-bytes)))
327     (inst mov header bytes)
328     (inst shl header (- n-widetag-bits 2)) ; w+1 to length field
329     (inst lea header                    ; (w-1 << 8) | type
330           (make-ea :dword :base header :disp (+ (ash -2 n-widetag-bits) type)))
331     (inst and bytes (lognot lowtag-mask))
332     (pseudo-atomic
333      (allocation result bytes node)
334      (inst lea result (make-ea :byte :base result :disp lowtag))
335      (storew header result 0 lowtag))))