0.6.11.23:
[sbcl.git] / src / compiler / ltv.lisp
1 ;;;; This file implements LOAD-TIME-VALUE.
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!C")
13
14 (defknown %load-time-value (t) t (flushable movable))
15
16 (def-ir1-translator load-time-value ((form &optional read-only-p) start cont)
17   #!+sb-doc
18   "Arrange for FORM to be evaluated at load-time and use the value produced
19    as if it were a constant. If READ-ONLY-P is non-NIL, then the resultant
20    object is guaranteed to never be modified, so it can be put in read-only
21    storage."
22   (if (producing-fasl-file)
23       (multiple-value-bind (handle type)
24           (compile-load-time-value (if read-only-p
25                                        form
26                                        `(make-value-cell ,form)))
27         (declare (ignore type))
28         (ir1-convert start cont
29                      (if read-only-p
30                          `(%load-time-value ',handle)
31                          `(value-cell-ref (%load-time-value ',handle)))))
32       (let ((value
33              (handler-case (eval form)
34                (error (condition)
35                  (compiler-error "(during EVAL of LOAD-TIME-VALUE)~%~A"
36                                  condition)))))
37         (ir1-convert start cont
38                      (if read-only-p
39                          `',value
40                          `(value-cell-ref ',(make-value-cell value)))))))
41
42 (defoptimizer (%load-time-value ir2-convert) ((handle) node block)
43   (aver (constant-continuation-p handle))
44   (let ((cont (node-cont node))
45         (tn (make-load-time-value-tn (continuation-value handle)
46                                      *universal-type*)))
47     (move-continuation-result node block (list tn) cont)))