a0ec28b416c70cfd064e12b268cacdabee1685a2
[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 ;;;; LIST and LIST*
15
16 (define-vop (list-or-list*)
17   (:args (things :more t))
18   (:temporary (:sc unsigned-reg) ptr temp)
19   (:temporary (:sc unsigned-reg :to (:result 0) :target result) res)
20   (:info num)
21   (:results (result :scs (descriptor-reg)))
22   (:variant-vars star)
23   (:policy :safe)
24   (:node-var node)
25   (:generator 0
26     (cond ((zerop num)
27            ;; (move result nil-value)
28            (inst mov result nil-value))
29           ((and star (= num 1))
30            (move result (tn-ref-tn things)))
31           (t
32            (macrolet
33                ((store-car (tn list &optional (slot cons-car-slot))
34                   `(let ((reg
35                           (sc-case ,tn
36                             ((any-reg descriptor-reg) ,tn)
37                             ((control-stack)
38                              (move temp ,tn)
39                              temp))))
40                      (storew reg ,list ,slot list-pointer-lowtag))))
41              (let ((cons-cells (if star (1- num) num)))
42                (pseudo-atomic
43                 (allocation res (* (pad-data-block cons-size) cons-cells) node)
44                 (inst lea res
45                       (make-ea :byte :base res :disp 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 (define-vop (allocate-code-object)
72   (:args (boxed-arg :scs (any-reg) :target boxed)
73          (unboxed-arg :scs (any-reg) :target unboxed))
74   (:results (result :scs (descriptor-reg) :from :eval))
75   (:temporary (:sc unsigned-reg :from (:argument 0)) boxed)
76   (:temporary (:sc unsigned-reg :from (:argument 1)) unboxed)
77   (:node-var node)
78   (:generator 100
79     (move boxed boxed-arg)
80     (inst add boxed (fixnumize (1+ code-trace-table-offset-slot)))
81     (inst and boxed (lognot lowtag-mask))
82     (move unboxed unboxed-arg)
83     (inst shr unboxed word-shift)
84     (inst add unboxed lowtag-mask)
85     (inst and unboxed (lognot lowtag-mask))
86     (inst mov result boxed)
87     (inst add result unboxed)
88     (pseudo-atomic
89      (allocation result result node)
90      (inst lea result (make-ea :byte :base result :disp other-pointer-lowtag))
91      (inst shl boxed (- n-widetag-bits word-shift))
92      (inst or boxed code-header-widetag)
93      (storew boxed result 0 other-pointer-lowtag)
94      (storew unboxed result code-code-size-slot other-pointer-lowtag)
95      (storew nil-value result code-entry-points-slot other-pointer-lowtag))
96     (storew nil-value result code-debug-info-slot other-pointer-lowtag)))
97 \f
98 (define-vop (make-fdefn)
99   (:policy :fast-safe)
100   (:translate make-fdefn)
101   (:args (name :scs (descriptor-reg) :to :eval))
102   (:results (result :scs (descriptor-reg) :from :argument))
103   (:node-var node)
104   (:generator 37
105     (with-fixed-allocation (result fdefn-widetag fdefn-size node)
106       (storew name result fdefn-name-slot other-pointer-lowtag)
107       (storew nil-value result fdefn-fun-slot other-pointer-lowtag)
108       (storew (make-fixup (extern-alien-name "undefined_tramp") :foreign)
109               result fdefn-raw-addr-slot other-pointer-lowtag))))
110
111 (define-vop (make-closure)
112   (:args (function :to :save :scs (descriptor-reg)))
113   (:info length)
114   (:temporary (:sc any-reg) temp)
115   (:results (result :scs (descriptor-reg)))
116   (:node-var node)
117   (:generator 10
118    (pseudo-atomic
119     (let ((size (+ length closure-info-offset)))
120       (allocation result (pad-data-block size) node)
121       (inst lea result
122             (make-ea :byte :base result :disp fun-pointer-lowtag))
123       (storew (logior (ash (1- size) n-widetag-bits) closure-header-widetag)
124               result 0 fun-pointer-lowtag))
125     (loadw temp function closure-fun-slot fun-pointer-lowtag)
126     (storew temp result closure-fun-slot fun-pointer-lowtag))))
127
128 ;;; The compiler likes to be able to directly make value cells.
129 (define-vop (make-value-cell)
130   (:args (value :scs (descriptor-reg any-reg) :to :result))
131   (:results (result :scs (descriptor-reg) :from :eval))
132   (:node-var node)
133   (:generator 10
134     (with-fixed-allocation
135         (result value-cell-header-widetag value-cell-size node))
136     (storew value result value-cell-value-slot other-pointer-lowtag)))
137 \f
138 ;;;; automatic allocators for primitive objects
139
140 (define-vop (make-unbound-marker)
141   (:args)
142   (:results (result :scs (any-reg)))
143   (:generator 1
144     (inst mov result unbound-marker-widetag)))
145
146 (define-vop (fixed-alloc)
147   (:args)
148   (:info name words type lowtag)
149   (:ignore name)
150   (:results (result :scs (descriptor-reg)))
151   (:node-var node)
152   (:generator 50
153     (pseudo-atomic
154      (allocation result (pad-data-block words) node)
155      (inst lea result (make-ea :byte :base result :disp lowtag))
156      (when type
157        (storew (logior (ash (1- words) n-widetag-bits) type)
158                result
159                0
160                lowtag)))))
161
162 (define-vop (var-alloc)
163   (:args (extra :scs (any-reg)))
164   (:arg-types positive-fixnum)
165   (:info name words type lowtag)
166   (:ignore name)
167   (:results (result :scs (descriptor-reg) :from (:eval 1)))
168   (:temporary (:sc any-reg :from :eval :to (:eval 1)) bytes)
169   (:temporary (:sc any-reg :from :eval :to :result) header)
170   (:node-var node)
171   (:generator 50
172     (inst lea bytes
173           (make-ea :dword :base extra :disp (* (1+ words) n-word-bytes)))
174     (inst mov header bytes)
175     (inst shl header (- n-widetag-bits 2)) ; w+1 to length field
176     (inst lea header                    ; (w-1 << 8) | type
177           (make-ea :dword :base header :disp (+ (ash -2 n-widetag-bits) type)))
178     (inst and bytes (lognot lowtag-mask))
179     (pseudo-atomic
180      (allocation result bytes node)
181      (inst lea result (make-ea :byte :base result :disp lowtag))
182      (storew header result 0 lowtag))))
183
184