0.6.11.17:
[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 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
50 (defun clos-slots-ref (slots index)
51   (svref slots index))
52 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
53 (defun (setf clos-slots-ref) (new-value slots index)
54   (setf (svref slots index) new-value))
55
56 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
57 ;;; is only used to discriminate between functions (including FINs)
58 ;;; and normal instances, so we can return true on structures also. A
59 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
60 ;;; PCL-INSTANCE-P.
61 (defmacro std-instance-p (x)
62   `(sb-kernel:%instancep ,x))
63
64 (defmacro get-slots (inst)
65   `(cond ((std-instance-p ,inst) (std-instance-slots ,inst))
66          ((fsc-instance-p ,inst) (fsc-instance-slots ,inst))
67          (t (error "What kind of instance is this?"))))
68
69 ;; a temporary definition used for debugging the bootstrap
70 #+sb-show
71 (defun print-std-instance (instance stream depth)
72   (declare (ignore depth))      
73   (print-unreadable-object (instance stream :type t :identity t)
74     (let ((class (class-of instance)))
75       (when (or (eq class (find-class 'standard-class nil))
76                 (eq class (find-class 'funcallable-standard-class nil))
77                 (eq class (find-class 'built-in-class nil)))
78         (princ (early-class-name instance) stream)))))
79
80 ;;; This is the value that we stick into a slot to tell us that it is unbound.
81 ;;; It may seem gross, but for performance reasons, we make this an interned
82 ;;; symbol. That means that the fast check to see whether a slot is unbound is
83 ;;; to say (EQ <val> '..SLOT-UNBOUND..). That is considerably faster than
84 ;;; looking at the value of a special variable. Be careful, there are places in
85 ;;; the code which actually use ..slot-unbound.. rather than this variable. So
86 ;;; much for modularity..
87 ;;;
88 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use the
89 ;;; SBCL built-in unbound value token instead.
90 (defconstant +slot-unbound+ '..slot-unbound..)
91
92 (defmacro %allocate-static-slot-storage--class (no-of-slots)
93   `(make-array ,no-of-slots :initial-element +slot-unbound+))
94
95 (defmacro std-instance-class (instance)
96   `(wrapper-class* (std-instance-wrapper ,instance)))
97 \f
98
99 ;;; When given a function should give this function the name <new-name>.
100 ;;; Note that <new-name> is sometimes a list. Some lisps get the upset
101 ;;; in the tummy when they start thinking about functions which have
102 ;;; lists as names. To deal with that there is set-function-name-intern
103 ;;; which takes a list spec for a function name and turns it into a symbol
104 ;;; if need be.
105 ;;;
106 ;;; When given a funcallable instance, set-function-name MUST side-effect
107 ;;; that FIN to give it the name. When given any other kind of function
108 ;;; set-function-name is allowed to return new function which is the 'same'
109 ;;; except that it has the name.
110 ;;;
111 ;;; In all cases, set-function-name must return the new (or same) function.
112 ;;; (Unlike other functions to set stuff, it does not return the new value.)
113 (defun set-function-name (fcn new-name)
114   #+sb-doc
115   "Set the name of a compiled function object. Return the function."
116   (declare (special *boot-state* *the-class-standard-generic-function*))
117   (cond ((symbolp fcn)
118          (set-function-name (symbol-function fcn) new-name))
119         ((funcallable-instance-p fcn)
120          (if (if (eq *boot-state* 'complete)
121                  (typep fcn 'generic-function)
122                  (eq (class-of fcn) *the-class-standard-generic-function*))
123              (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
124              (typecase fcn
125                (sb-kernel:byte-closure
126                 (set-function-name (sb-kernel:byte-closure-function fcn)
127                                    new-name))
128                (sb-kernel:byte-function
129                 (setf (sb-kernel:byte-function-name fcn) new-name))
130                (sb-eval:interpreted-function
131                 (setf (sb-eval:interpreted-function-name fcn) new-name))))
132          fcn)
133         (t
134          ;; pw-- This seems wrong and causes trouble. Tests show
135          ;; that loading CL-HTTP resulted in ~5400 closures being
136          ;; passed through this code of which ~4000 of them pointed
137          ;; to but 16 closure-functions, including 1015 each of
138          ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
139          ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
140          ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
141          ;; Since the actual functions have been moved by PURIFY
142          ;; to memory not seen by GC, changing a pointer there
143          ;; not only clobbers the last change but leaves a dangling
144          ;; pointer invalid  after the next GC. Comments in low.lisp
145          ;; indicate this code need do nothing. Setting the
146          ;; function-name to NIL loses some info, and not changing
147          ;; it loses some info of potential hacking value. So,
148          ;; lets not do this...
149          #+nil
150          (let ((header (sb-kernel:%closure-function fcn)))
151            (setf (sb-c::%function-name header) new-name))
152
153          ;; Maybe add better scheme here someday.
154          fcn)))
155
156 (defun intern-function-name (name)
157   (cond ((symbolp name) name)
158         ((listp name)
159          (intern (let ((*package* *pcl-package*)
160                        (*print-case* :upcase)
161                        (*print-pretty* nil)
162                        (*print-gensym* t))
163                    (format nil "~S" name))
164                  *pcl-package*))))
165 \f
166 ;;; FIXME: probably no longer needed after init
167 (defmacro precompile-random-code-segments (&optional system)
168   `(progn
169      (eval-when (:compile-toplevel)
170        (update-dispatch-dfuns)
171        (compile-iis-functions nil))
172      (precompile-function-generators ,system)
173      (precompile-dfun-constructors ,system)
174      (precompile-iis-functions ,system)
175      (eval-when (:load-toplevel)
176        (compile-iis-functions t))))
177 \f
178 (defun record-definition (type spec &rest args)
179   (declare (ignore type spec args))
180   ())
181
182 (defun doctor-dfun-for-the-debugger (gf dfun) (declare (ignore gf)) dfun)
183 \f
184 ;;; This definition is for interpreted code.
185 (defun pcl-instance-p (x)
186   (typep (sb-kernel:layout-of x) 'wrapper))
187
188 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber
189 ;;; the layout with some standard-instance layout as soon as we make
190 ;;; it, and we want the accessor to still be type-correct.
191 (defstruct (standard-instance
192             (:predicate nil)
193             (:constructor %%allocate-instance--class ())
194             (:copier nil)
195             (:alternate-metaclass sb-kernel:instance cl:standard-class
196                                   sb-kernel:make-standard-class))
197   (slots nil))
198
199 ;;; Both of these operations "work" on structures, which allows the above
200 ;;; weakening of STD-INSTANCE-P.
201 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
202 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
203
204 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
205
206 (defmacro get-wrapper (inst)
207   (once-only ((wrapper `(wrapper-of ,inst)))
208     `(progn
209        (assert (typep ,wrapper 'wrapper) () "What kind of instance is this?")
210        ,wrapper)))
211
212 ;;; FIXME: could be an inline function (like many other things around
213 ;;; here)
214 (defmacro get-instance-wrapper-or-nil (inst)
215   (once-only ((wrapper `(wrapper-of ,inst)))
216     `(if (typep ,wrapper 'wrapper)
217          ,wrapper
218          nil)))
219
220 (defmacro get-slots-or-nil (inst)
221   (once-only ((n-inst inst))
222     `(when (pcl-instance-p ,n-inst)
223        (if (std-instance-p ,n-inst)
224            (std-instance-slots ,n-inst)
225            (fsc-instance-slots ,n-inst)))))
226 \f
227 ;;;; structure-instance stuff
228 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
229
230 (defun get-structure-dd (type)
231   (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
232
233 (defun structure-type-included-type-name (type)
234   (let ((include (sb-kernel::dd-include (get-structure-dd type))))
235     (if (consp include)
236         (car include)
237         include)))
238
239 (defun structure-type-slot-description-list (type)
240   (nthcdr (length (let ((include (structure-type-included-type-name type)))
241                     (and include
242                          (sb-kernel:dd-slots (get-structure-dd include)))))
243           (sb-kernel:dd-slots (get-structure-dd type))))
244
245 (defun structure-slotd-name (slotd)
246   (sb-kernel:dsd-name slotd))
247
248 (defun structure-slotd-accessor-symbol (slotd)
249   (sb-kernel:dsd-accessor slotd))
250
251 (defun structure-slotd-reader-function (slotd)
252   (fdefinition (sb-kernel:dsd-accessor slotd)))
253
254 (defun structure-slotd-writer-function (slotd)
255   (unless (sb-kernel:dsd-read-only slotd)
256     (fdefinition `(setf ,(sb-kernel:dsd-accessor slotd)))))
257
258 (defun structure-slotd-type (slotd)
259   (sb-kernel:dsd-type slotd))
260
261 (defun structure-slotd-init-form (slotd)
262   (sb-kernel::dsd-default slotd))