0.6.10.19:
[sbcl.git] / src / pcl / low.lisp
1 ;;;; This file contains portable versions of low-level functions and macros
2 ;;;; which are ripe for implementation specific customization. None of the code
3 ;;;; in this file *has* to be customized for a particular Common Lisp
4 ;;;; implementation. Moreover, in some implementations it may not make any
5 ;;;; sense to customize some of this code.
6 ;;;;
7 ;;;; The original version was intended to support portable customization to
8 ;;;; lotso different Lisp implementations. This functionality is gone in the
9 ;;;; current version, and it now runs only under SBCL. (Now that ANSI Common
10 ;;;; Lisp has mixed CLOS into the insides of the system (e.g. error handling
11 ;;;; and printing) so deeply that it's not very meaningful to bootstrap Common
12 ;;;; Lisp without CLOS, the old functionality is of dubious use. -- WHN
13 ;;;; 19981108)
14
15 ;;;; This software is part of the SBCL system. See the README file for more
16 ;;;; information.
17
18 ;;;; This software is derived from software originally released by Xerox
19 ;;;; Corporation. Copyright and release statements follow. Later modifications
20 ;;;; to the software are in the public domain and are provided with
21 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
22 ;;;; information.
23
24 ;;;; copyright information from original PCL sources:
25 ;;;;
26 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
27 ;;;; All rights reserved.
28 ;;;;
29 ;;;; Use and copying of this software and preparation of derivative works based
30 ;;;; upon this software are permitted. Any distribution of this software or
31 ;;;; derivative works must comply with all applicable United States export
32 ;;;; control laws.
33 ;;;;
34 ;;;; This software is made available AS IS, and Xerox Corporation makes no
35 ;;;; warranty about the software, its performance or its conformity to any
36 ;;;; specification.
37
38 (in-package "SB-PCL")
39 \f
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed* '(optimize (speed 3) (safety 0)))
42 ) ; EVAL-WHEN
43
44 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
45   `(dotimes (,var (the fixnum ,count) ,result)
46      (declare (fixnum ,var))
47      ,@body))
48 \f
49
50 (defmacro instance-ref (slots index)
51   `(svref ,slots ,index))
52
53 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
54 ;;; is only used to discriminate between functions (including FINs)
55 ;;; and normal instances, so we can return true on structures also. A
56 ;;; few uses of (or std-instance-p fsc-instance-p) are changed to
57 ;;; pcl-instance-p.
58 (defmacro std-instance-p (x)
59   `(sb-kernel:%instancep ,x))
60
61 (defmacro get-slots (inst)
62   `(cond ((std-instance-p ,inst) (std-instance-slots ,inst))
63          ((fsc-instance-p ,inst) (fsc-instance-slots ,inst))
64          (t (error "What kind of instance is this?"))))
65
66 ;; a temporary definition used for debugging the bootstrap
67 #+sb-show
68 (defun print-std-instance (instance stream depth)
69   (declare (ignore depth))      
70   (print-unreadable-object (instance stream :type t :identity t)
71     (let ((class (class-of instance)))
72       (when (or (eq class (find-class 'standard-class nil))
73                 (eq class (find-class 'funcallable-standard-class nil))
74                 (eq class (find-class 'built-in-class nil)))
75         (princ (early-class-name instance) stream)))))
76
77 ;;; This is the value that we stick into a slot to tell us that it is unbound.
78 ;;; It may seem gross, but for performance reasons, we make this an interned
79 ;;; symbol. That means that the fast check to see whether a slot is unbound is
80 ;;; to say (EQ <val> '..SLOT-UNBOUND..). That is considerably faster than
81 ;;; looking at the value of a special variable. Be careful, there are places in
82 ;;; the code which actually use ..slot-unbound.. rather than this variable. So
83 ;;; much for modularity..
84 ;;;
85 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use the
86 ;;; SBCL built-in unbound value token instead.
87 (defconstant +slot-unbound+ '..slot-unbound..)
88
89 (defmacro %allocate-static-slot-storage--class (no-of-slots)
90   `(make-array ,no-of-slots :initial-element +slot-unbound+))
91
92 (defmacro std-instance-class (instance)
93   `(wrapper-class* (std-instance-wrapper ,instance)))
94 \f
95
96 ;;; SET-FUNCTION-NAME
97 ;;;
98 ;;; When given a function should give this function the name <new-name>.
99 ;;; Note that <new-name> is sometimes a list. Some lisps get the upset
100 ;;; in the tummy when they start thinking about functions which have
101 ;;; lists as names. To deal with that there is set-function-name-intern
102 ;;; which takes a list spec for a function name and turns it into a symbol
103 ;;; if need be.
104 ;;;
105 ;;; When given a funcallable instance, set-function-name MUST side-effect
106 ;;; that FIN to give it the name. When given any other kind of function
107 ;;; set-function-name is allowed to return new function which is the 'same'
108 ;;; except that it has the name.
109 ;;;
110 ;;; In all cases, set-function-name must return the new (or same) function.
111 ;;; (Unlike other functions to set stuff, it does not return the new value.)
112 (defun set-function-name (fcn new-name)
113   #+sb-doc
114   "Set the name of a compiled function object. Return the function."
115   (declare (special *boot-state* *the-class-standard-generic-function*))
116   (cond ((symbolp fcn)
117          (set-function-name (symbol-function fcn) new-name))
118         ((funcallable-instance-p fcn)
119          (if (if (eq *boot-state* 'complete)
120                  (typep fcn 'generic-function)
121                  (eq (class-of fcn) *the-class-standard-generic-function*))
122              (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
123              (typecase fcn
124                (sb-kernel:byte-closure
125                 (set-function-name (sb-kernel:byte-closure-function fcn)
126                                    new-name))
127                (sb-kernel:byte-function
128                 (setf (sb-kernel:byte-function-name fcn) new-name))
129                (sb-eval:interpreted-function
130                 (setf (sb-eval:interpreted-function-name fcn) new-name))))
131          fcn)
132         (t
133          ;; pw-- This seems wrong and causes trouble. Tests show
134          ;; that loading CL-HTTP resulted in ~5400 closures being
135          ;; passed through this code of which ~4000 of them pointed
136          ;; to but 16 closure-functions, including 1015 each of
137          ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
138          ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
139          ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
140          ;; Since the actual functions have been moved by PURIFY
141          ;; to memory not seen by GC, changing a pointer there
142          ;; not only clobbers the last change but leaves a dangling
143          ;; pointer invalid  after the next GC. Comments in low.lisp
144          ;; indicate this code need do nothing. Setting the
145          ;; function-name to NIL loses some info, and not changing
146          ;; it loses some info of potential hacking value. So,
147          ;; lets not do this...
148          #+nil
149          (let ((header (sb-kernel:%closure-function fcn)))
150            (setf (sb-c::%function-name header) new-name))
151
152          ;; Maybe add better scheme here someday.
153          fcn)))
154
155 (defun intern-function-name (name)
156   (cond ((symbolp name) name)
157         ((listp name)
158          (intern (let ((*package* *pcl-package*)
159                        (*print-case* :upcase)
160                        (*print-pretty* nil)
161                        (*print-gensym* 't))
162                    (format nil "~S" name))
163                  *pcl-package*))))
164 \f
165 ;;; FIXME: probably no longer needed after init
166 (defmacro precompile-random-code-segments (&optional system)
167   `(progn
168      (eval-when (:compile-toplevel)
169        (update-dispatch-dfuns)
170        (compile-iis-functions nil))
171      (precompile-function-generators ,system)
172      (precompile-dfun-constructors ,system)
173      (precompile-iis-functions ,system)
174      (eval-when (:load-toplevel)
175        (compile-iis-functions t))))
176 \f
177 (defun record-definition (type spec &rest args)
178   (declare (ignore type spec args))
179   ())
180
181 (defun doctor-dfun-for-the-debugger (gf dfun) (declare (ignore gf)) dfun)
182 \f
183 ;;; This definition is for interpreted code.
184 (defun pcl-instance-p (x)
185   (typep (sb-kernel:layout-of x) 'wrapper))
186
187 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber the
188 ;;; layout with some standard-instance layout as soon as we make it, and we
189 ;;; want the accessor to still be type-correct.
190 (defstruct (standard-instance
191             (:predicate nil)
192             (:constructor %%allocate-instance--class ())
193             (:copier nil)
194             (:alternate-metaclass sb-kernel:instance cl:standard-class
195                                   sb-kernel:make-standard-class))
196   (slots nil))
197
198 ;;; Both of these operations "work" on structures, which allows the above
199 ;;; weakening of STD-INSTANCE-P.
200 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
201 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
202
203 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
204
205 (defmacro get-wrapper (inst)
206   (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
207     `(progn
208        (assert (typep ,wrapper 'wrapper) () "What kind of instance is this?")
209        ,wrapper)))
210
211 ;;; FIXME: could be an inline function (like many other things around
212 ;;; here)
213 (defmacro get-instance-wrapper-or-nil (inst)
214   (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
215     `(if (typep ,wrapper 'wrapper)
216          ,wrapper
217          nil)))
218
219 (defmacro get-slots-or-nil (inst)
220   (sb-int:once-only ((n-inst inst))
221     `(when (pcl-instance-p ,n-inst)
222        (if (std-instance-p ,n-inst)
223            (std-instance-slots ,n-inst)
224            (fsc-instance-slots ,n-inst)))))
225 \f
226 ;;;; structure-instance stuff
227 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
228
229 (defun get-structure-dd (type)
230   (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
231
232 (defun structure-type-included-type-name (type)
233   (let ((include (sb-kernel::dd-include (get-structure-dd type))))
234     (if (consp include)
235         (car include)
236         include)))
237
238 (defun structure-type-slot-description-list (type)
239   (nthcdr (length (let ((include (structure-type-included-type-name type)))
240                     (and include
241                          (sb-kernel:dd-slots (get-structure-dd include)))))
242           (sb-kernel:dd-slots (get-structure-dd type))))
243
244 (defun structure-slotd-name (slotd)
245   (sb-kernel:dsd-name slotd))
246
247 (defun structure-slotd-accessor-symbol (slotd)
248   (sb-kernel:dsd-accessor slotd))
249
250 (defun structure-slotd-reader-function (slotd)
251   (fdefinition (sb-kernel:dsd-accessor slotd)))
252
253 (defun structure-slotd-writer-function (slotd)
254   (unless (sb-kernel:dsd-read-only slotd)
255     (fdefinition `(setf ,(sb-kernel:dsd-accessor slotd)))))
256
257 (defun structure-slotd-type (slotd)
258   (sb-kernel:dsd-type slotd))
259
260 (defun structure-slotd-init-form (slotd)
261   (sb-kernel::dsd-default slotd))