7a0a1ee3369c02c9518ec3aa0ebdbd11249388a6
[sbcl.git] / src / compiler / generic / vm-ir2tran.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!C")
11
12 (def-alloc %make-structure-instance 1 :structure-alloc
13            sb!vm:instance-header-widetag sb!vm:instance-pointer-lowtag
14            nil)
15
16 #!+stack-allocatable-fixed-objects
17 (defoptimizer (%make-structure-instance stack-allocate-result) ((&rest args) node dx)
18   t)
19
20 (defoptimizer ir2-convert-reffer ((object) node block name offset lowtag)
21   (let* ((lvar (node-lvar node))
22          (locs (lvar-result-tns lvar
23                                         (list *backend-t-primitive-type*)))
24          (res (first locs)))
25     (vop slot node block (lvar-tn node block object)
26          name offset lowtag res)
27     (move-lvar-result node block locs lvar)))
28
29 (defoptimizer ir2-convert-setter ((object value) node block name offset lowtag)
30   (let ((value-tn (lvar-tn node block value)))
31     (vop set-slot node block (lvar-tn node block object) value-tn
32          name offset lowtag)
33     (move-lvar-result node block (list value-tn) (node-lvar node))))
34
35 ;;; FIXME: Isn't there a name for this which looks less like a typo?
36 ;;; (The name IR2-CONVERT-SETTER is used for something else, just above.)
37 (defoptimizer ir2-convert-setfer ((value object) node block name offset lowtag)
38   (let ((value-tn (lvar-tn node block value)))
39     (vop set-slot node block (lvar-tn node block object) value-tn
40          name offset lowtag)
41     (move-lvar-result node block (list value-tn) (node-lvar node))))
42
43 #!+compare-and-swap-vops
44 (defoptimizer ir2-convert-casser
45     ((object old new) node block name offset lowtag)
46   (let* ((lvar (node-lvar node))
47          (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
48          (res (first locs)))
49     (vop compare-and-swap-slot node block
50          (lvar-tn node block object)
51          (lvar-tn node block old)
52          (lvar-tn node block new)
53          name offset lowtag
54          res)
55     (move-lvar-result node block locs lvar)))
56
57 (defun emit-inits (node block name object lowtag instance-length inits args)
58   #!-raw-instance-init-vops
59   (declare (ignore instance-length))
60   (let ((unbound-marker-tn nil)
61         (funcallable-instance-tramp-tn nil))
62     (dolist (init inits)
63       (let ((kind (car init))
64             (slot (cdr init)))
65         (case kind
66           (:slot
67            (let ((raw-type (pop slot))
68                  (arg-tn (lvar-tn node block (pop args))))
69              (macrolet ((make-case ()
70                           `(ecase raw-type
71                              ((t)
72                               (vop init-slot node block object arg-tn
73                                    name (+ sb!vm:instance-slots-offset slot) lowtag))
74                              ,@(mapcar (lambda (rsd)
75                                          `(,(sb!kernel::raw-slot-data-raw-type rsd)
76                                             (vop ,(sb!kernel::raw-slot-data-init-vop rsd)
77                                                  node block
78                                                  object arg-tn instance-length slot)))
79                                        #!+raw-instance-init-vops
80                                        sb!kernel::*raw-slot-data-list*
81                                        #!-raw-instance-init-vops
82                                        nil))))
83                (make-case))))
84           (:dd
85            (vop init-slot node block object
86                 (emit-constant (sb!kernel::dd-layout-or-lose slot))
87                 name sb!vm:instance-slots-offset lowtag))
88           (otherwise
89            (vop init-slot node block object
90                 (ecase kind
91                   (:arg
92                    (aver args)
93                    (lvar-tn node block (pop args)))
94                   (:unbound
95                    (or unbound-marker-tn
96                        (setf unbound-marker-tn
97                              (let ((tn (make-restricted-tn
98                                         nil
99                                         (sc-number-or-lose 'sb!vm::any-reg))))
100                                (vop make-unbound-marker node block tn)
101                                tn))))
102                   (:null
103                    (emit-constant nil))
104                   (:funcallable-instance-tramp
105                    (or funcallable-instance-tramp-tn
106                        (setf funcallable-instance-tramp-tn
107                              (let ((tn (make-restricted-tn
108                                         nil
109                                         (sc-number-or-lose 'sb!vm::any-reg))))
110                                (vop make-funcallable-instance-tramp node block tn)
111                                tn)))))
112                 name slot lowtag))))))
113   (unless (null args)
114     (bug "Leftover args: ~S" args)))
115
116 (defun emit-fixed-alloc (node block name words type lowtag result lvar)
117   (let ((stack-allocate-p (and lvar (lvar-dynamic-extent lvar))))
118     (when stack-allocate-p
119       (vop current-stack-pointer node block
120            (ir2-lvar-stack-pointer (lvar-info lvar))))
121     (vop fixed-alloc node block name words type lowtag stack-allocate-p result)))
122
123 (defoptimizer ir2-convert-fixed-allocation
124               ((&rest args) node block name words type lowtag inits)
125   (let* ((lvar (node-lvar node))
126          (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
127          (result (first locs)))
128     (emit-fixed-alloc node block name words type lowtag result lvar)
129     (emit-inits node block name result lowtag words inits args)
130     (move-lvar-result node block locs lvar)))
131
132 (defoptimizer ir2-convert-variable-allocation
133               ((extra &rest args) node block name words type lowtag inits)
134   (let* ((lvar (node-lvar node))
135          (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
136          (result (first locs)))
137     (if (constant-lvar-p extra)
138         (let ((words (+ (lvar-value extra) words)))
139           (emit-fixed-alloc node block name words type lowtag result lvar))
140         (vop var-alloc node block (lvar-tn node block extra) name words
141              type lowtag result))
142     (emit-inits node block name result lowtag nil inits args)
143     (move-lvar-result node block locs lvar)))
144
145 (defoptimizer ir2-convert-structure-allocation
146     ((dd slot-specs &rest args) node block name words type lowtag inits)
147   (let* ((lvar (node-lvar node))
148          (locs (lvar-result-tns lvar (list *backend-t-primitive-type*)))
149          (result (first locs)))
150     (aver (constant-lvar-p dd))
151     (aver (constant-lvar-p slot-specs))
152     (let* ((c-dd (lvar-value dd))
153            (c-slot-specs (lvar-value slot-specs))
154            (words (+ (sb!kernel::dd-instance-length c-dd) words)))
155       (emit-fixed-alloc node block name words type lowtag result lvar)
156       (emit-inits node block name result lowtag words `((:dd . ,c-dd) ,@c-slot-specs) args)
157       (move-lvar-result node block locs lvar))))
158
159 (defoptimizer (initialize-vector ir2-convert)
160     ((vector &rest initial-contents) node block)
161   (let* ((vector-ctype (lvar-type vector))
162          (elt-ctype (if (array-type-p vector-ctype)
163                         (array-type-specialized-element-type vector-ctype)
164                         (bug "Unknow vector type in IR2 conversion for ~S."
165                              'initialize-vector)))
166          (saetp (find-saetp-by-ctype elt-ctype))
167          (lvar (node-lvar node))
168          (locs (lvar-result-tns lvar (list (primitive-type vector-ctype))))
169          (result (first locs))
170          (elt-ptype (primitive-type elt-ctype))
171          (tmp (make-normal-tn elt-ptype)))
172     (emit-move node block (lvar-tn node block vector) result)
173     (flet ((compute-setter ()
174              (macrolet
175                  ((frob ()
176                     (let ((*package* (find-package :sb!vm))
177                           (clauses nil))
178                       (map nil (lambda (s)
179                                  (when (sb!vm:saetp-specifier s)
180                                    (push
181                                     `(,(sb!vm:saetp-typecode s)
182                                        (lambda (index tn)
183                                          #!+(or x86 x86-64)
184                                          (vop ,(symbolicate "DATA-VECTOR-SET-WITH-OFFSET/"
185                                                             (sb!vm:saetp-primitive-type-name s))
186                                               node block result index tn 0 tn)
187                                          #!-(or x86 x86-64)
188                                          (vop ,(symbolicate "DATA-VECTOR-SET/"
189                                                             (sb!vm:saetp-primitive-type-name s))
190                                               node block result index tn tn)))
191                                     clauses)))
192                            sb!vm:*specialized-array-element-type-properties*)
193                       `(ecase (sb!vm:saetp-typecode saetp)
194                          ,@(nreverse clauses)))))
195                (frob)))
196            (tnify (index)
197              (emit-constant index)))
198       (let ((setter (compute-setter))
199             (length (length initial-contents)))
200         (dotimes (i length)
201           (emit-move node block (lvar-tn node block (pop initial-contents)) tmp)
202           (funcall setter (tnify i) tmp))))
203     (move-lvar-result node block locs lvar)))
204
205 ;;; :SET-TRANS (in objdef.lisp DEFINE-PRIMITIVE-OBJECT) doesn't quite
206 ;;; cut it for symbols, where under certain compilation options
207 ;;; (e.g. #!+SB-THREAD) we have to do something complicated, rather
208 ;;; than simply set the slot.  So we build the IR2 converting function
209 ;;; by hand.  -- CSR, 2003-05-08
210 (let ((fun-info (fun-info-or-lose '%set-symbol-value)))
211   (setf (fun-info-ir2-convert fun-info)
212         (lambda (node block)
213           (let ((args (basic-combination-args node)))
214             (destructuring-bind (symbol value) args
215               (let ((value-tn (lvar-tn node block value)))
216                 (vop set node block
217                      (lvar-tn node block symbol) value-tn)
218                 (move-lvar-result
219                  node block (list value-tn) (node-lvar node))))))))
220
221 ;;; Stack allocation optimizers per platform support
222 #!+stack-allocatable-vectors
223 (progn
224   (defoptimizer (allocate-vector stack-allocate-result)
225       ((type length words) node dx)
226     (and
227      ;; Can't put unboxed data on the stack unless we scavenge it
228      ;; conservatively.
229      #!-c-stack-is-control-stack
230      (constant-lvar-p type)
231      #!-c-stack-is-control-stack
232      (member (lvar-value type)
233              '#.(list (sb!vm:saetp-typecode (find-saetp 't))
234                       (sb!vm:saetp-typecode (find-saetp 'fixnum))))
235      (or (eq dx :always-dynamic)
236          (zerop (policy node safety))
237          ;; a vector object should fit in one page -- otherwise it might go past
238          ;; stack guard pages.
239          (values-subtypep (lvar-derived-type words)
240                           (load-time-value
241                            (specifier-type `(integer 0 ,(- (/ sb!vm::*backend-page-bytes*
242                                                               sb!vm:n-word-bytes)
243                                                            sb!vm:vector-data-offset))))))))
244
245   (defoptimizer (allocate-vector ltn-annotate) ((type length words) call ltn-policy)
246     (let ((args (basic-combination-args call))
247           (template (template-or-lose (if (awhen (node-lvar call)
248                                             (lvar-dynamic-extent it))
249                                           'sb!vm::allocate-vector-on-stack
250                                           'sb!vm::allocate-vector-on-heap))))
251       (dolist (arg args)
252         (setf (lvar-info arg)
253               (make-ir2-lvar (primitive-type (lvar-type arg)))))
254       (unless (is-ok-template-use template call (ltn-policy-safe-p ltn-policy))
255         (ltn-default-call call)
256         (return-from allocate-vector-ltn-annotate-optimizer (values)))
257       (setf (basic-combination-info call) template)
258       (setf (node-tail-p call) nil)
259
260       (dolist (arg args)
261         (annotate-1-value-lvar arg)))))
262
263 ;;; ...lists
264 #!+stack-allocatable-lists
265 (progn
266   (defoptimizer (list stack-allocate-result) ((&rest args) node dx)
267     (declare (ignore node dx))
268     (not (null args)))
269   (defoptimizer (list* stack-allocate-result) ((&rest args) node dx)
270     (declare (ignore node dx))
271     (not (null (rest args))))
272   (defoptimizer (%listify-rest-args stack-allocate-result) ((&rest args) node dx)
273     (declare (ignore node dx))
274     t))
275
276 ;;; ...conses
277 #!+stack-allocatable-fixed-objects
278 (progn
279   (defoptimizer (cons stack-allocate-result) ((&rest args) node dx)
280     t)
281   (defoptimizer (%make-complex stack-allocate-result) ((&rest args) node dx)
282     t))