Thou shalt not MAKE-OTHER-IMMEDIATE-TYPE.
[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 (define-vop (list-or-list*)
16   (:args (things :more t))
17   (:temporary (:sc unsigned-reg) ptr temp)
18   (:temporary (:sc unsigned-reg :to (:result 0) :target result) res)
19   (:info num)
20   (:results (result :scs (descriptor-reg)))
21   (:variant-vars star)
22   (:policy :safe)
23   (:node-var node)
24   (:generator 0
25     (cond ((zerop num)
26            ;; (move result nil-value)
27            (inst mov result nil-value))
28           ((and star (= num 1))
29            (move result (tn-ref-tn things)))
30           (t
31            (macrolet
32                ((store-car (tn list &optional (slot cons-car-slot))
33                   `(let ((reg
34                           (sc-case ,tn
35                             ((any-reg descriptor-reg) ,tn)
36                             ((control-stack)
37                              (move temp ,tn)
38                              temp))))
39                      (storew reg ,list ,slot list-pointer-lowtag))))
40              (let ((cons-cells (if star (1- num) num))
41                    (stack-allocate-p (awhen (sb!c::node-lvar node)
42                                        (sb!c::lvar-dynamic-extent it))))
43                (maybe-pseudo-atomic stack-allocate-p
44                 (allocation res (* (pad-data-block cons-size) cons-cells) node
45                             stack-allocate-p list-pointer-lowtag)
46                 (move ptr res)
47                 (dotimes (i (1- cons-cells))
48                   (store-car (tn-ref-tn things) ptr)
49                   (setf things (tn-ref-across things))
50                   (inst add ptr (pad-data-block cons-size))
51                   (storew ptr ptr (- cons-cdr-slot cons-size)
52                           list-pointer-lowtag))
53                 (store-car (tn-ref-tn things) ptr)
54                 (cond (star
55                        (setf things (tn-ref-across things))
56                        (store-car (tn-ref-tn things) ptr cons-cdr-slot))
57                       (t
58                        (storew nil-value ptr cons-cdr-slot
59                                list-pointer-lowtag)))
60                 (aver (null (tn-ref-across things)))))
61              (move result res))))))
62
63 (define-vop (list list-or-list*)
64   (:variant nil))
65
66 (define-vop (list* list-or-list*)
67   (:variant t))
68 \f
69 ;;;; special-purpose inline allocators
70
71 ;;; ALLOCATE-VECTOR
72 (define-vop (allocate-vector-on-heap)
73   (:args (type :scs (unsigned-reg immediate))
74          (length :scs (any-reg immediate))
75          (words :scs (any-reg immediate)))
76   (:results (result :scs (descriptor-reg) :from :load))
77   (:arg-types positive-fixnum
78               positive-fixnum
79               positive-fixnum)
80   (:policy :fast-safe)
81   (:generator 100
82     (let ((size (sc-case words
83                   (immediate
84                    (logandc2 (+ (fixnumize (tn-value words))
85                                 (+ (1- (ash 1 n-lowtag-bits))
86                                    (* vector-data-offset n-word-bytes)))
87                              lowtag-mask))
88                   (t
89                    (inst lea result (make-ea :byte :base words :disp
90                                              (+ (1- (ash 1 n-lowtag-bits))
91                                                 (* vector-data-offset
92                                                    n-word-bytes))))
93                    (inst and result (lognot lowtag-mask))
94                    result))))
95       (pseudo-atomic
96        (allocation result size)
97        (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
98        (sc-case type
99          (immediate
100           (aver (typep (tn-value type) '(unsigned-byte 8)))
101           (storeb (tn-value type) result 0 other-pointer-lowtag))
102          (t
103           (storew type result 0 other-pointer-lowtag)))
104        (sc-case length
105          (immediate
106           (let ((fixnum-length (fixnumize (tn-value length))))
107             (typecase fixnum-length
108               ((unsigned-byte 8)
109                (storeb fixnum-length result
110                        vector-length-slot other-pointer-lowtag))
111               (t
112                (storew fixnum-length result
113                        vector-length-slot other-pointer-lowtag)))))
114          (t
115           (storew length result vector-length-slot other-pointer-lowtag)))))))
116
117 (define-vop (allocate-vector-on-stack)
118   (:args (type :scs (unsigned-reg immediate) :to :save)
119          (length :scs (any-reg) :to :eval :target zero)
120          (words :scs (any-reg) :target ecx))
121   (:temporary (:sc any-reg :offset ecx-offset :from (:argument 2)) ecx)
122   (:temporary (:sc any-reg :offset eax-offset :from :eval) zero)
123   (:temporary (:sc any-reg :offset edi-offset) res)
124   (:results (result :scs (descriptor-reg) :from :load))
125   (:arg-types positive-fixnum
126               positive-fixnum
127               positive-fixnum)
128   (:translate allocate-vector)
129   (:policy :fast-safe)
130   (:node-var node)
131   (:generator 100
132     (inst lea result (make-ea :byte :base words :disp
133                               (+ (1- (ash 1 n-lowtag-bits))
134                                  (* vector-data-offset n-word-bytes))))
135     (inst and result (lognot lowtag-mask))
136     ;; FIXME: It would be good to check for stack overflow here.
137     (move ecx words)
138     (inst shr ecx n-fixnum-tag-bits)
139     (allocation result result node t other-pointer-lowtag)
140     (inst cld)
141     (inst lea res
142           (make-ea :byte :base result :disp (- (* vector-data-offset n-word-bytes)
143                                                other-pointer-lowtag)))
144     (sc-case type
145       (immediate
146        (aver (typep (tn-value type) '(unsigned-byte 8)))
147        (storeb (tn-value type) result 0 other-pointer-lowtag))
148       (t
149        (storew type result 0 other-pointer-lowtag)))
150     (storew length result vector-length-slot other-pointer-lowtag)
151     (inst xor zero zero)
152     (inst rep)
153     (inst stos zero)))
154
155 \f
156 (define-vop (make-fdefn)
157   (:policy :fast-safe)
158   (:translate make-fdefn)
159   (:args (name :scs (descriptor-reg) :to :eval))
160   (:results (result :scs (descriptor-reg) :from :argument))
161   (:node-var node)
162   (:generator 37
163     (with-fixed-allocation (result fdefn-widetag fdefn-size node)
164       (storew name result fdefn-name-slot other-pointer-lowtag)
165       (storew nil-value result fdefn-fun-slot other-pointer-lowtag)
166       (storew (make-fixup "undefined_tramp" :foreign)
167               result fdefn-raw-addr-slot other-pointer-lowtag))))
168
169 (define-vop (make-closure)
170   (:args (function :to :save :scs (descriptor-reg)))
171   (:info length stack-allocate-p)
172   (:temporary (:sc any-reg) temp)
173   (:results (result :scs (descriptor-reg)))
174   (:node-var node)
175   (:generator 10
176    (maybe-pseudo-atomic stack-allocate-p
177      (let ((size (+ length closure-info-offset)))
178        (allocation result (pad-data-block size) node
179                    stack-allocate-p
180                    fun-pointer-lowtag)
181        (storew (logior (ash (1- size) n-widetag-bits) closure-header-widetag)
182                result 0 fun-pointer-lowtag))
183     (loadw temp function closure-fun-slot fun-pointer-lowtag)
184     (storew temp result closure-fun-slot fun-pointer-lowtag))))
185
186 ;;; The compiler likes to be able to directly make value cells.
187 (define-vop (make-value-cell)
188   (:args (value :scs (descriptor-reg any-reg) :to :result))
189   (:results (result :scs (descriptor-reg) :from :eval))
190   (:info stack-allocate-p)
191   (:node-var node)
192   (:generator 10
193     (with-fixed-allocation
194         (result value-cell-header-widetag value-cell-size node stack-allocate-p)
195       (storew value result value-cell-value-slot other-pointer-lowtag))))
196 \f
197 ;;;; automatic allocators for primitive objects
198
199 (define-vop (make-unbound-marker)
200   (:args)
201   (:results (result :scs (descriptor-reg any-reg)))
202   (:generator 1
203     (inst mov result unbound-marker-widetag)))
204
205 (define-vop (make-funcallable-instance-tramp)
206   (:args)
207   (:results (result :scs (any-reg)))
208   (:generator 1
209     (inst lea result (make-fixup "funcallable_instance_tramp" :foreign))))
210
211 (define-vop (fixed-alloc)
212   (:args)
213   (:info name words type lowtag stack-allocate-p)
214   (:ignore name)
215   (:results (result :scs (descriptor-reg)))
216   (:node-var node)
217   (:generator 50
218     ;; We special case the allocation of conses, because they're
219     ;; extremely common and because the pseudo-atomic sequence on x86
220     ;; is relatively heavyweight.  However, if the user asks for top
221     ;; speed, we accomodate him.  The primary reason that we don't
222     ;; also check for (< SPEED SPACE) is because we want the space
223     ;; savings that these out-of-line allocation routines bring whilst
224     ;; compiling SBCL itself.  --njf, 2006-07-08
225     (if (and (not stack-allocate-p)
226              (= lowtag list-pointer-lowtag) (policy node (< speed 3)))
227         (let ((dst
228                ;; FIXME: out-of-line dx-allocation
229                #.(loop for offset in *dword-regs*
230                     collect `(,offset
231                               ',(intern (format nil "ALLOCATE-CONS-TO-~A"
232                                                 (svref *dword-register-names*
233                                                        offset)))) into cases
234                     finally (return `(case (tn-offset result)
235                                        ,@cases)))))
236           (aver (null type))
237           (inst call (make-fixup dst :assembly-routine)))
238         (maybe-pseudo-atomic stack-allocate-p
239          (allocation result (pad-data-block words) node stack-allocate-p lowtag)
240          (when type
241            (storew (logior (ash (1- words) n-widetag-bits) type)
242                    result
243                    0
244                    lowtag))))))
245
246 (define-vop (var-alloc)
247   (:args (extra :scs (any-reg)))
248   (:arg-types positive-fixnum)
249   (:info name words type lowtag)
250   (:ignore name)
251   (:results (result :scs (descriptor-reg) :from (:eval 1)))
252   (:temporary (:sc any-reg :from :eval :to (:eval 1)) bytes)
253   (:temporary (:sc any-reg :from :eval :to :result) header)
254   (:node-var node)
255   (:generator 50
256     (inst lea bytes
257           (make-ea :dword :base extra :disp (* (1+ words) n-word-bytes)))
258     (inst mov header bytes)
259     (inst shl header (- n-widetag-bits 2)) ; w+1 to length field
260     (inst lea header                    ; (w-1 << 8) | type
261           (make-ea :dword :base header :disp (+ (ash -2 n-widetag-bits) type)))
262     (inst and bytes (lognot lowtag-mask))
263     (pseudo-atomic
264      (allocation result bytes node)
265      (inst lea result (make-ea :byte :base result :disp lowtag))
266      (storew header result 0 lowtag))))