29be6794f4fa9a90f1ddfb1408b4a03435d05c00
[sbcl.git] / src / compiler / ppc / alloc.lisp
1 ;;;; allocation VOPs for the PPC
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   (:temporary (:scs (non-descriptor-reg)) temp)
130   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
131   (:results (result :scs (descriptor-reg)))
132   (:generator 10
133     (let* ((size (+ length closure-info-offset))
134            (alloc-size (pad-data-block size))
135            (allocation-area-tn (if stack-allocate-p csp-tn alloc-tn)))
136       (pseudo-atomic (pa-flag :extra (if stack-allocate-p 0 alloc-size))
137         (when stack-allocate-p
138           (align-csp result))
139         (inst clrrwi. result allocation-area-tn n-lowtag-bits)
140         (when stack-allocate-p
141           (inst addi csp-tn csp-tn alloc-size))
142         (inst ori result result fun-pointer-lowtag)
143         (inst lr temp (logior (ash (1- size) n-widetag-bits) closure-header-widetag))
144         (storew temp result 0 fun-pointer-lowtag)))
145     ;(inst lis temp (ash 18 10))
146     ;(storew temp result closure-jump-insn-slot function-pointer-type)
147     (storew result result closure-self-slot fun-pointer-lowtag)
148     (storew function result closure-fun-slot fun-pointer-lowtag)))
149
150 ;;; The compiler likes to be able to directly make value cells.
151 ;;;
152 (define-vop (make-value-cell)
153   (:args (value :to :save :scs (descriptor-reg any-reg)))
154   (:temporary (:scs (non-descriptor-reg)) temp)
155   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
156   (:results (result :scs (descriptor-reg)))
157   (:generator 10
158     (with-fixed-allocation (result pa-flag temp value-cell-header-widetag value-cell-size)
159       (storew value result value-cell-value-slot other-pointer-lowtag))))
160
161
162 \f
163 ;;;; Automatic allocators for primitive objects.
164
165 (define-vop (make-unbound-marker)
166   (:args)
167   (:results (result :scs (any-reg)))
168   (:generator 1
169     (inst li result unbound-marker-widetag)))
170
171 (define-vop (fixed-alloc)
172   (:args)
173   (:info name words type lowtag)
174   (:ignore name)
175   (:results (result :scs (descriptor-reg)))
176   (:temporary (:scs (non-descriptor-reg)) temp)
177   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
178   (:generator 4
179     (pseudo-atomic (pa-flag :extra (pad-data-block words))
180       (cond ((logbitp 2 lowtag)
181              (inst ori result alloc-tn lowtag))
182             (t
183              (inst clrrwi result alloc-tn n-lowtag-bits)
184              (inst ori result  result lowtag)))
185       (when type
186         (inst lr temp (logior (ash (1- words) n-widetag-bits) type))
187         (storew temp result 0 lowtag)))))
188
189 (define-vop (var-alloc)
190   (:args (extra :scs (any-reg)))
191   (:arg-types positive-fixnum)
192   (:info name words type lowtag)
193   (:ignore name)
194   (:results (result :scs (descriptor-reg)))
195   (:temporary (:scs (any-reg)) bytes)
196   (:temporary (:scs (non-descriptor-reg)) header)
197   (:temporary (:sc non-descriptor-reg :offset nl3-offset) pa-flag)
198   (:generator 6
199     (inst addi bytes extra (* (1+ words) n-word-bytes))
200     (inst slwi header bytes (- n-widetag-bits 2))
201     (inst addi header header (+ (ash -2 n-widetag-bits) type))
202     (inst clrrwi bytes bytes n-lowtag-bits)
203     (pseudo-atomic (pa-flag)
204       (cond ((logbitp 2 lowtag)
205              (inst ori result alloc-tn lowtag))
206             (t
207              (inst clrrwi result alloc-tn n-lowtag-bits)
208              (inst ori result result lowtag)))
209       (storew header result 0 lowtag)
210       (inst add alloc-tn alloc-tn bytes))))