d1551dcd1125180c50855bfb9fcce0e6ad331c54
[sbcl.git] / src / compiler / ppc / alloc.lisp
1 ;;;; allocation VOPs
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 (defoptimizer (list stack-allocate-result) ((&rest args))
16   (not (null args)))
17 (defoptimizer (list* stack-allocate-result) ((&rest args))
18   (not (null (rest args))))
19
20 (define-vop (list-or-list*)
21   (:args (things :more t))
22   (:temporary (:scs (descriptor-reg) :type list) ptr)
23   (:temporary (:scs (descriptor-reg)) temp)
24   (:temporary (:scs (descriptor-reg) :type list :to (:result 0) :target result)
25               res)
26   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
27   (:info num)
28   (:results (result :scs (descriptor-reg)))
29   (:variant-vars star)
30   (:policy :safe)
31   (:node-var node)
32   (:generator 0
33     (cond ((zerop num)
34            (move result null-tn))
35           ((and star (= num 1))
36            (move result (tn-ref-tn things)))
37           (t
38            (macrolet
39                ((maybe-load (tn)
40                   (once-only ((tn tn))
41                     `(sc-case ,tn
42                        ((any-reg descriptor-reg zero null)
43                         ,tn)
44                        (control-stack
45                         (load-stack-tn temp ,tn)
46                         temp)))))
47              (let* ((dx-p (node-stack-allocate-p node))
48                     (cons-cells (if star (1- num) num))
49                     (alloc (* (pad-data-block cons-size) cons-cells)))
50                (pseudo-atomic (pa-flag :extra (if dx-p 0 alloc))
51                  (let ((allocation-area-tn (if dx-p csp-tn alloc-tn)))
52                    (when dx-p
53                      (align-csp res))
54                    (inst clrrwi res allocation-area-tn n-lowtag-bits)
55                    (inst ori res res list-pointer-lowtag)
56                    (when dx-p
57                      (inst addi csp-tn csp-tn alloc)))
58                  (move ptr res)
59                  (dotimes (i (1- cons-cells))
60                    (storew (maybe-load (tn-ref-tn things)) ptr
61                            cons-car-slot list-pointer-lowtag)
62                    (setf things (tn-ref-across things))
63                    (inst addi ptr ptr (pad-data-block cons-size))
64                    (storew ptr ptr
65                            (- cons-cdr-slot cons-size)
66                            list-pointer-lowtag))
67                  (storew (maybe-load (tn-ref-tn things)) ptr
68                          cons-car-slot list-pointer-lowtag)
69                  (storew (if star
70                              (maybe-load (tn-ref-tn (tn-ref-across things)))
71                              null-tn)
72                          ptr cons-cdr-slot list-pointer-lowtag))
73                (move result res)))))))
74
75 (define-vop (list list-or-list*)
76   (:variant nil))
77
78 (define-vop (list* list-or-list*)
79   (:variant t))
80
81 \f
82 ;;;; Special purpose inline allocators.
83
84 (define-vop (allocate-code-object)
85   (:args (boxed-arg :scs (any-reg))
86          (unboxed-arg :scs (any-reg)))
87   (:results (result :scs (descriptor-reg)))
88   (:temporary (:scs (non-descriptor-reg)) ndescr)
89   (:temporary (:scs (any-reg) :from (:argument 0)) boxed)
90   (:temporary (:scs (non-descriptor-reg) :from (:argument 1)) unboxed)
91   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
92   (:generator 100
93     (inst addi boxed boxed-arg (fixnumize (1+ code-trace-table-offset-slot)))
94     (inst clrrwi boxed boxed n-lowtag-bits)
95     (inst srwi unboxed unboxed-arg word-shift)
96     (inst addi unboxed unboxed lowtag-mask)
97     (inst clrrwi unboxed unboxed n-lowtag-bits)
98     (pseudo-atomic (pa-flag)
99       ;; Note: we don't have to subtract off the 4 that was added by
100       ;; pseudo-atomic, because oring in other-pointer-lowtag just adds
101       ;; it right back.
102       (inst ori result alloc-tn other-pointer-lowtag)
103       (inst add alloc-tn alloc-tn boxed)
104       (inst add alloc-tn alloc-tn unboxed)
105       (inst slwi ndescr boxed (- n-widetag-bits word-shift))
106       (inst ori ndescr ndescr code-header-widetag)
107       (storew ndescr result 0 other-pointer-lowtag)
108       (storew unboxed result code-code-size-slot other-pointer-lowtag)
109       (storew null-tn result code-entry-points-slot other-pointer-lowtag)
110       (storew null-tn result code-debug-info-slot other-pointer-lowtag))))
111
112 (define-vop (make-fdefn)
113   (:args (name :scs (descriptor-reg) :to :eval))
114   (:temporary (:scs (non-descriptor-reg)) temp)
115   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
116   (:results (result :scs (descriptor-reg) :from :argument))
117   (:policy :fast-safe)
118   (:translate make-fdefn)
119   (:generator 37
120     (with-fixed-allocation (result pa-flag temp fdefn-widetag fdefn-size)
121       (inst lr temp  (make-fixup "undefined_tramp" :foreign))
122       (storew name result fdefn-name-slot other-pointer-lowtag)
123       (storew null-tn result fdefn-fun-slot other-pointer-lowtag)
124       (storew temp result fdefn-raw-addr-slot other-pointer-lowtag))))
125
126 (define-vop (make-closure)
127   (:args (function :to :save :scs (descriptor-reg)))
128   (:info length stack-allocate-p)
129   (:ignore stack-allocate-p)
130   (:temporary (:scs (non-descriptor-reg)) temp)
131   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
132   (:results (result :scs (descriptor-reg)))
133   (:node-var node)
134   (:generator 10
135     (let* ((size (+ length closure-info-offset))
136            (alloc-size (pad-data-block size))
137            (dx-p (node-stack-allocate-p node))
138            (allocation-area-tn (if dx-p csp-tn alloc-tn)))
139       (pseudo-atomic (pa-flag :extra (if dx-p 0 alloc-size))
140         ;; no need to align CSP for DX: FUN-POINTER-LOWTAG already has
141         ;; the corresponding bit set
142         (inst clrrwi. result allocation-area-tn n-lowtag-bits)
143         (when dx-p
144           (inst addi csp-tn alloc-size))
145         (inst ori result result fun-pointer-lowtag)
146         (inst lr temp (logior (ash (1- size) n-widetag-bits) closure-header-widetag))
147         (storew temp result 0 fun-pointer-lowtag)))
148     ;(inst lis temp (ash 18 10))
149     ;(storew temp result closure-jump-insn-slot function-pointer-type)
150     (storew function result closure-fun-slot fun-pointer-lowtag)))
151
152 ;;; The compiler likes to be able to directly make value cells.
153 ;;; 
154 (define-vop (make-value-cell)
155   (:args (value :to :save :scs (descriptor-reg any-reg)))
156   (:temporary (:scs (non-descriptor-reg)) temp)
157   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
158   (:results (result :scs (descriptor-reg)))
159   (:generator 10
160     (with-fixed-allocation (result pa-flag temp value-cell-header-widetag value-cell-size)
161       (storew value result value-cell-value-slot other-pointer-lowtag))))
162
163
164 \f
165 ;;;; Automatic allocators for primitive objects.
166
167 (define-vop (make-unbound-marker)
168   (:args)
169   (:results (result :scs (any-reg)))
170   (:generator 1
171     (inst li result unbound-marker-widetag)))
172
173 (define-vop (fixed-alloc)
174   (:args)
175   (:info name words type lowtag)
176   (:ignore name)
177   (:results (result :scs (descriptor-reg)))
178   (:temporary (:scs (non-descriptor-reg)) temp)
179   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
180   (:generator 4
181     (pseudo-atomic (pa-flag :extra (pad-data-block words))
182       (cond ((logbitp 2 lowtag)
183              (inst ori result alloc-tn lowtag))
184             (t
185              (inst clrrwi result alloc-tn n-lowtag-bits)
186              (inst ori result  result lowtag)))
187       (when type
188         (inst lr temp (logior (ash (1- words) n-widetag-bits) type))
189         (storew temp result 0 lowtag)))))
190
191 (define-vop (var-alloc)
192   (:args (extra :scs (any-reg)))
193   (:arg-types positive-fixnum)
194   (:info name words type lowtag)
195   (:ignore name)
196   (:results (result :scs (descriptor-reg)))
197   (:temporary (:scs (any-reg)) bytes)
198   (:temporary (:scs (non-descriptor-reg)) header)
199   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
200   (:generator 6
201     (inst addi bytes extra (* (1+ words) n-word-bytes))
202     (inst slwi header bytes (- n-widetag-bits 2))
203     (inst addi header header (+ (ash -2 n-widetag-bits) type))
204     (inst clrrwi bytes bytes n-lowtag-bits)
205     (pseudo-atomic (pa-flag)
206       (cond ((logbitp 2 lowtag)
207              (inst ori result alloc-tn lowtag))
208             (t
209              (inst clrrwi result alloc-tn n-lowtag-bits)
210              (inst ori result result lowtag)))
211       (storew header result 0 lowtag)
212       (inst add alloc-tn alloc-tn bytes))))