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