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