Thou shalt not MAKE-OTHER-IMMEDIATE-TYPE.
[sbcl.git] / src / compiler / x86-64 / alloc.lisp
1 ;;;; allocation VOPs for the x86-64
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))
74          (length :scs (any-reg))
75          (words :scs (any-reg)))
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     (inst lea result (make-ea :byte :index words
83                               :scale (ash 1 (- word-shift n-fixnum-tag-bits))
84                               :disp (+ lowtag-mask
85                                        (* vector-data-offset n-word-bytes))))
86     (inst and result (lognot lowtag-mask))
87     (pseudo-atomic
88       (allocation result result)
89       (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
90       (storew type result 0 other-pointer-lowtag)
91       (storew length result vector-length-slot other-pointer-lowtag))))
92
93 (define-vop (allocate-vector-on-stack)
94   (:args (type :scs (unsigned-reg) :to :save)
95          (length :scs (any-reg) :to :eval :target zero)
96          (words :scs (any-reg) :target ecx))
97   (:temporary (:sc any-reg :offset ecx-offset :from (:argument 2)) ecx)
98   (:temporary (:sc any-reg :offset eax-offset :from :eval) zero)
99   (:temporary (:sc any-reg :offset edi-offset) res)
100   (:results (result :scs (descriptor-reg) :from :load))
101   (:arg-types positive-fixnum
102               positive-fixnum
103               positive-fixnum)
104   (:translate allocate-vector)
105   (:policy :fast-safe)
106   (:node-var node)
107   (:generator 100
108     (inst lea result (make-ea :byte :index words
109                               :scale (ash 1 (- word-shift n-fixnum-tag-bits))
110                               :disp (+ lowtag-mask
111                                        (* vector-data-offset n-word-bytes))))
112     (inst and result (lognot lowtag-mask))
113     ;; FIXME: It would be good to check for stack overflow here.
114     (move ecx words)
115     (inst shr ecx n-fixnum-tag-bits)
116     (allocation result result node t other-pointer-lowtag)
117     (inst cld)
118     (inst lea res
119           (make-ea :byte :base result :disp (- (* vector-data-offset n-word-bytes)
120                                                other-pointer-lowtag)))
121     (storew type result 0 other-pointer-lowtag)
122     (storew length result vector-length-slot other-pointer-lowtag)
123     (zeroize zero)
124     (inst rep)
125     (inst stos zero)))
126
127 \f
128 (define-vop (make-fdefn)
129   (:policy :fast-safe)
130   (:translate make-fdefn)
131   (:args (name :scs (descriptor-reg) :to :eval))
132   (:results (result :scs (descriptor-reg) :from :argument))
133   (:node-var node)
134   (:generator 37
135     (with-fixed-allocation (result fdefn-widetag fdefn-size node)
136       (storew name result fdefn-name-slot other-pointer-lowtag)
137       (storew nil-value result fdefn-fun-slot other-pointer-lowtag)
138       (storew (make-fixup "undefined_tramp" :foreign)
139               result fdefn-raw-addr-slot other-pointer-lowtag))))
140
141 (define-vop (make-closure)
142   (:args (function :to :save :scs (descriptor-reg)))
143   (:info length stack-allocate-p)
144   (:temporary (:sc any-reg) temp)
145   (:results (result :scs (descriptor-reg)))
146   (:node-var node)
147   (:generator 10
148    (maybe-pseudo-atomic stack-allocate-p
149     (let ((size (+ length closure-info-offset)))
150       (allocation result (pad-data-block size) node stack-allocate-p
151                   fun-pointer-lowtag)
152       (storew (logior (ash (1- size) n-widetag-bits) closure-header-widetag)
153               result 0 fun-pointer-lowtag))
154     (loadw temp function closure-fun-slot fun-pointer-lowtag)
155     (storew temp result closure-fun-slot fun-pointer-lowtag))))
156
157 ;;; The compiler likes to be able to directly make value cells.
158 (define-vop (make-value-cell)
159   (:args (value :scs (descriptor-reg any-reg) :to :result))
160   (:results (result :scs (descriptor-reg) :from :eval))
161   (:info stack-allocate-p)
162   (:node-var node)
163   (:generator 10
164     (with-fixed-allocation
165         (result value-cell-header-widetag value-cell-size node stack-allocate-p)
166       (storew value result value-cell-value-slot other-pointer-lowtag))))
167 \f
168 ;;;; automatic allocators for primitive objects
169
170 (define-vop (make-unbound-marker)
171   (:args)
172   (:results (result :scs (descriptor-reg any-reg)))
173   (:generator 1
174     (inst mov result unbound-marker-widetag)))
175
176 (define-vop (make-funcallable-instance-tramp)
177   (:args)
178   (:results (result :scs (any-reg)))
179   (:generator 1
180     (inst lea result (make-fixup "funcallable_instance_tramp" :foreign))))
181
182 (define-vop (fixed-alloc)
183   (:args)
184   (:info name words type lowtag stack-allocate-p)
185   (:ignore name)
186   (:results (result :scs (descriptor-reg)))
187   (:node-var node)
188   (:generator 50
189     (maybe-pseudo-atomic stack-allocate-p
190      (allocation result (pad-data-block words) node stack-allocate-p lowtag)
191      (when type
192        (storew (logior (ash (1- words) n-widetag-bits) type)
193                result
194                0
195                lowtag)))))
196
197 (define-vop (var-alloc)
198   (:args (extra :scs (any-reg)))
199   (:arg-types positive-fixnum)
200   (:info name words type lowtag)
201   (:ignore name)
202   (:results (result :scs (descriptor-reg) :from (:eval 1)))
203   (:temporary (:sc any-reg :from :eval :to (:eval 1)) bytes)
204   (:temporary (:sc any-reg :from :eval :to :result) header)
205   (:node-var node)
206   (:generator 50
207     (inst lea bytes
208           (make-ea :qword :disp (* (1+ words) n-word-bytes) :index extra
209                    :scale (ash 1 (- word-shift n-fixnum-tag-bits))))
210     (inst mov header bytes)
211     (inst shl header (- n-widetag-bits word-shift)) ; w+1 to length field
212     (inst lea header                    ; (w-1 << 8) | type
213           (make-ea :qword :base header
214                    :disp (+ (ash -2 n-widetag-bits) type)))
215     (inst and bytes (lognot lowtag-mask))
216     (pseudo-atomic
217      (allocation result bytes node)
218      (inst lea result (make-ea :byte :base result :disp lowtag))
219      (storew header result 0 lowtag))))
220
221 (define-vop (%make-symbol)
222   (:policy :fast-safe)
223   (:translate %make-symbol)
224   (:args (name :scs (descriptor-reg) :to :eval))
225   (:temporary (:sc unsigned-reg :from :eval) temp)
226   (:results (result :scs (descriptor-reg) :from :argument))
227   (:node-var node)
228   (:generator 37
229     (with-fixed-allocation (result symbol-header-widetag symbol-size node)
230       (storew name result symbol-name-slot other-pointer-lowtag)
231       (storew unbound-marker-widetag
232               result
233               symbol-value-slot
234               other-pointer-lowtag)
235       ;; Set up a random hash value for the symbol. Perhaps the object
236       ;; address could be used for even faster and smaller code!
237       ;; FIXME: We don't mind the symbol hash not being repeatable, so
238       ;; we might as well add in the object address here, too. (Adding entropy
239       ;; is good, even if ANSI doesn't understand that.)
240       (inst imul temp
241             (make-fixup "fast_random_state" :foreign)
242             1103515245)
243       (inst add temp 12345)
244       (inst mov (make-fixup "fast_random_state" :foreign)
245             temp)
246       ;; We want a positive fixnum for the hash value, so discard the LS bits.
247       ;;
248       ;; FIXME: OK, who wants to tell me (CSR) why these two
249       ;; instructions aren't replaced by (INST AND TEMP #x8FFFFFFC)?
250       ;; Are the following two instructions actually faster?  Does the
251       ;; difference in behaviour really matter?
252       (inst shr temp 1)
253       (inst and temp #xfffffffc)
254       (storew temp result symbol-hash-slot other-pointer-lowtag)
255       (storew nil-value result symbol-plist-slot other-pointer-lowtag)
256       (storew nil-value result symbol-package-slot other-pointer-lowtag))))