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