Initial revision
[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
40 (sb-int:file-comment
41   "$Header$")
42 \f
43 (eval-when (:compile-toplevel :load-toplevel :execute)
44 (defvar *optimize-speed* '(optimize (speed 3) (safety 0)))
45 ) ; EVAL-WHEN
46
47 ;;; FIXME: Do these definitions actually increase speed significantly?
48 ;;; Could we just use SVREF instead, possibly with a few extra
49 ;;; OPTIMIZE declarations added here and ther?
50 (defmacro %svref (vector index)
51   `(locally (declare #.*optimize-speed*
52                      (inline svref))
53             (svref (the simple-vector ,vector) (the fixnum ,index))))
54 (defsetf %svref %set-svref)
55 (defmacro %set-svref (vector index new-value)
56   `(locally (declare #.*optimize-speed*
57                      (inline svref))
58      (setf (svref (the simple-vector ,vector) (the fixnum ,index))
59            ,new-value)))
60
61 ;;; I want the body to be evaluated in such a way that no other code that is
62 ;;; running PCL can be run during that evaluation. I agree that the body
63 ;;; won't take *long* to evaluate. That is to say that I will only use
64 ;;; WITHOUT-INTERRUPTS around relatively small computations.
65 ;;;
66 ;;; FIXME: We can get rid of this macro definitionand either USE package %SYS
67 ;;; or add an explicit SB-SYS: prefix to each reference to WITHOUT-INTERRUPTS.
68 (defmacro without-interrupts (&rest stuff)
69   `(sb-sys:without-interrupts ,@stuff))
70
71 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
72   `(dotimes (,var (the fixnum ,count) ,result)
73      (declare (fixnum ,var))
74      ,@body))
75 \f
76 ;;;; very low-level representation of instances with meta-class
77 ;;;; STANDARD-CLASS
78
79 ;;; FIXME: more than one IN-PACKAGE in a source file, ick
80 (in-package "SB-C")
81
82 (defknown sb-pcl::pcl-instance-p (t) boolean
83   (movable foldable flushable explicit-check))
84
85 (deftransform sb-pcl::pcl-instance-p ((object))
86   (let* ((otype (continuation-type object))
87          (std-obj (specifier-type 'sb-pcl::std-object)))
88     (cond
89       ;; Flush tests whose result is known at compile time.
90       ((csubtypep otype std-obj) 't)
91       ((not (types-intersect otype std-obj)) 'nil)
92       (t
93        `(typep (sb-kernel:layout-of object) 'sb-pcl::wrapper)))))
94
95 (in-package "SB-PCL")
96
97 ;;; FIXME: What do these do? Could we use SB-KERNEL:INSTANCE-REF instead?
98 (defmacro %instance-ref (slots index)
99   `(%svref ,slots ,index))
100 (defmacro instance-ref (slots index)
101   `(svref ,slots ,index))
102
103 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P is
104 ;;; only used to discriminate between functions (including FINs) and
105 ;;; normal instances, so we can return true on structures also. A few
106 ;;; uses of (or std-instance-p fsc-instance-p) are changed to
107 ;;; pcl-instance-p.
108 (defmacro std-instance-p (x)
109   `(sb-kernel:%instancep ,x))
110
111 (defmacro get-slots (inst)
112   `(cond ((std-instance-p ,inst) (std-instance-slots ,inst))
113          ((fsc-instance-p ,inst) (fsc-instance-slots ,inst))
114          (t (error "What kind of instance is this?"))))
115
116 ;; a temporary definition used for debugging the bootstrap
117 #+sb-show
118 (defun print-std-instance (instance stream depth)
119   (declare (ignore depth))      
120   (print-unreadable-object (instance stream :type t :identity t)
121     (let ((class (class-of instance)))
122       (when (or (eq class (find-class 'standard-class nil))
123                 (eq class (find-class 'funcallable-standard-class nil))
124                 (eq class (find-class 'built-in-class nil)))
125         (princ (early-class-name instance) stream)))))
126
127 ;;; This is the value that we stick into a slot to tell us that it is unbound.
128 ;;; It may seem gross, but for performance reasons, we make this an interned
129 ;;; symbol. That means that the fast check to see whether a slot is unbound is
130 ;;; to say (EQ <val> '..SLOT-UNBOUND..). That is considerably faster than
131 ;;; looking at the value of a special variable. Be careful, there are places in
132 ;;; the code which actually use ..slot-unbound.. rather than this variable. So
133 ;;; much for modularity..
134 ;;;
135 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use the
136 ;;; SBCL built-in unbound value token instead.
137 (defconstant *slot-unbound* '..slot-unbound..)
138
139 (defmacro %allocate-static-slot-storage--class (no-of-slots)
140   `(make-array ,no-of-slots :initial-element *slot-unbound*))
141
142 (defmacro std-instance-class (instance)
143   `(wrapper-class* (std-instance-wrapper ,instance)))
144 \f
145 ;;;; FUNCTION-ARGLIST
146
147 ;;; FIXME: Does FUNCTION-PRETTY-ARGLIST need to be settable at all?
148 (defsetf function-pretty-arglist set-function-pretty-arglist)
149 (defun set-function-pretty-arglist (function new-value)
150   (declare (ignore function))
151   new-value)
152
153 ;;; SET-FUNCTION-NAME
154 ;;;
155 ;;; When given a function should give this function the name <new-name>.
156 ;;; Note that <new-name> is sometimes a list. Some lisps get the upset
157 ;;; in the tummy when they start thinking about functions which have
158 ;;; lists as names. To deal with that there is set-function-name-intern
159 ;;; which takes a list spec for a function name and turns it into a symbol
160 ;;; if need be.
161 ;;;
162 ;;; When given a funcallable instance, set-function-name MUST side-effect
163 ;;; that FIN to give it the name. When given any other kind of function
164 ;;; set-function-name is allowed to return new function which is the 'same'
165 ;;; except that it has the name.
166 ;;;
167 ;;; In all cases, set-function-name must return the new (or same) function.
168 ;;; (Unlike other functions to set stuff, it does not return the new value.)
169 (defun set-function-name (fcn new-name)
170   #+sb-doc
171   "Set the name of a compiled function object. Return the function."
172   (declare (special *boot-state* *the-class-standard-generic-function*))
173   (cond ((symbolp fcn)
174          (set-function-name (symbol-function fcn) new-name))
175         ((funcallable-instance-p fcn)
176          (if (if (eq *boot-state* 'complete)
177                  (typep fcn 'generic-function)
178                  (eq (class-of fcn) *the-class-standard-generic-function*))
179              (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
180              (typecase fcn
181                (sb-kernel:byte-closure
182                 (set-function-name (sb-kernel:byte-closure-function fcn)
183                                    new-name))
184                (sb-kernel:byte-function
185                 (setf (sb-kernel:byte-function-name fcn) new-name))
186                (sb-eval:interpreted-function
187                 (setf (sb-eval:interpreted-function-name fcn) new-name))))
188          fcn)
189         (t
190          ;; pw-- This seems wrong and causes trouble. Tests show
191          ;; that loading CL-HTTP resulted in ~5400 closures being
192          ;; passed through this code of which ~4000 of them pointed
193          ;; to but 16 closure-functions, including 1015 each of
194          ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
195          ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
196          ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
197          ;; Since the actual functions have been moved by PURIFY
198          ;; to memory not seen by GC, changing a pointer there
199          ;; not only clobbers the last change but leaves a dangling
200          ;; pointer invalid  after the next GC. Comments in low.lisp
201          ;; indicate this code need do nothing. Setting the
202          ;; function-name to NIL loses some info, and not changing
203          ;; it loses some info of potential hacking value. So,
204          ;; lets not do this...
205          #+nil
206          (let ((header (sb-kernel:%closure-function fcn)))
207            (setf (sb-c::%function-name header) new-name))
208
209          ;; Maybe add better scheme here someday.
210          fcn)))
211
212 (defun intern-function-name (name)
213   (cond ((symbolp name) name)
214         ((listp name)
215          (intern (let ((*package* *pcl-package*)
216                        (*print-case* :upcase)
217                        (*print-pretty* nil)
218                        (*print-gensym* 't))
219                    (format nil "~S" name))
220                  *pcl-package*))))
221 \f
222 ;;;; COMPILE-LAMBDA
223
224 ;;; This is like the Common Lisp function COMPILE. In fact, that is what it
225 ;;; ends up calling. The difference is that it deals with things like not
226 ;;; calling the compiler in certain cases.
227 ;;;
228 ;;; FIXME: I suspect that in SBCL, we should always call the compiler. (PCL
229 ;;; was originally designed to run even on systems with dog-slow call-out-to-C
230 ;;; compilers, and I suspect that this code is needed only for that.)
231 (defun compile-lambda (lambda &optional (desirability :fast))
232   (cond ((eq desirability :fast)
233          (compile nil lambda))
234         (t
235          (compile-lambda-uncompiled lambda))))
236
237 (defun compile-lambda-uncompiled (uncompiled)
238   #'(lambda (&rest args) (apply (coerce uncompiled 'function) args)))
239
240 (defun compile-lambda-deferred (uncompiled)
241   (let ((function (coerce uncompiled 'function))
242         (compiled nil))
243     (declare (type (or function null) compiled))
244     #'(lambda (&rest args)
245         (if compiled
246             (apply compiled args)
247             (if (in-the-compiler-p)
248                 (apply function args)
249                 (progn (setq compiled (compile nil uncompiled))
250                        (apply compiled args)))))))
251
252 ;;; FIXME: probably no longer needed after init
253 (defmacro precompile-random-code-segments (&optional system)
254   `(progn
255      (eval-when (:compile-toplevel)
256        (update-dispatch-dfuns)
257        (compile-iis-functions nil))
258      (precompile-function-generators ,system)
259      (precompile-dfun-constructors ,system)
260      (precompile-iis-functions ,system)
261      (eval-when (:load-toplevel)
262        (compile-iis-functions t))))
263 \f
264 (defun record-definition (type spec &rest args)
265   (declare (ignore type spec args))
266   ())
267
268 (defun doctor-dfun-for-the-debugger (gf dfun) (declare (ignore gf)) dfun)
269 \f
270 ;;;; low level functions for structures I: functions on arbitrary objects
271
272 ;;; FIXME: Maybe we don't need this given the SBCL-specific
273 ;;; versions of the functions which would otherwise use it?
274 (defvar *structure-table* (make-hash-table :test 'eq))
275
276 (defun declare-structure (name included-name slot-description-list)
277   (setf (gethash name *structure-table*)
278         (cons included-name slot-description-list)))
279
280 (unless (fboundp 'structure-functions-exist-p)
281   (setf (symbol-function 'structure-functions-exist-p)
282         #'(lambda () nil)))
283
284 ;;; FIXME: should probably be INLINE
285 ;;; FIXME: should probably be moved to package SB-INT along with
286 ;;; other nonstandard type predicates, or removed entirely
287 (defun structurep (x)
288   (typep x 'cl:structure-object))
289 \f
290 ;;; This definition is for interpreted code.
291 (defun pcl-instance-p (x)
292   (typep (sb-kernel:layout-of x) 'wrapper))
293
294 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber the
295 ;;; layout with some standard-instance layout as soon as we make it, and we
296 ;;; want the accessor to still be type-correct.
297 (defstruct (standard-instance
298             (:predicate nil)
299             (:constructor %%allocate-instance--class ())
300             (:copier nil)
301             (:alternate-metaclass sb-kernel:instance cl:standard-class
302                                   sb-kernel:make-standard-class))
303   (slots nil))
304
305 ;;; Both of these operations "work" on structures, which allows the above
306 ;;; weakening of std-instance-p.
307 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
308 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
309
310 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
311
312 (defmacro get-wrapper (inst)
313   (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
314     `(progn
315        (assert (typep ,wrapper 'wrapper) () "What kind of instance is this?")
316        ,wrapper)))
317
318 ;;; FIXME: could be an inline function (like many other things around
319 ;;; here)
320 (defmacro get-instance-wrapper-or-nil (inst)
321   (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
322     `(if (typep ,wrapper 'wrapper)
323          ,wrapper
324          nil)))
325
326 (defmacro get-slots-or-nil (inst)
327   (sb-int:once-only ((n-inst inst))
328     `(when (pcl-instance-p ,n-inst)
329        (if (std-instance-p ,n-inst)
330            (std-instance-slots ,n-inst)
331            (fsc-instance-slots ,n-inst)))))
332 \f
333 ;;;; structure-instance stuff
334
335 ;;; FIXME: This can be removed by hardwiring uses of it to T.
336 (defun structure-functions-exist-p ()
337   t)
338
339 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
340
341 (defun get-structure-dd (type)
342   (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
343
344 (defun structure-type-included-type-name (type)
345   (let ((include (sb-kernel::dd-include (get-structure-dd type))))
346     (if (consp include)
347         (car include)
348         include)))
349
350 (defun structure-type-slot-description-list (type)
351   (nthcdr (length (let ((include (structure-type-included-type-name type)))
352                     (and include
353                          (sb-kernel:dd-slots (get-structure-dd include)))))
354           (sb-kernel:dd-slots (get-structure-dd type))))
355
356 (defun structure-slotd-name (slotd)
357   (sb-kernel:dsd-name slotd))
358
359 (defun structure-slotd-accessor-symbol (slotd)
360   (sb-kernel:dsd-accessor slotd))
361
362 (defun structure-slotd-reader-function (slotd)
363   (fdefinition (sb-kernel:dsd-accessor slotd)))
364
365 (defun structure-slotd-writer-function (slotd)
366   (unless (sb-kernel:dsd-read-only slotd)
367     (fdefinition `(setf ,(sb-kernel:dsd-accessor slotd)))))
368
369 (defun structure-slotd-type (slotd)
370   (sb-kernel:dsd-type slotd))
371
372 (defun structure-slotd-init-form (slotd)
373   (sb-kernel::dsd-default slotd))
374
375 ;;; FIXME: more than one IN-PACKAGE in a source file, ick
376 (in-package "SB-C")
377
378 (def-source-context defmethod (name &rest stuff)
379   (let ((arg-pos (position-if #'listp stuff)))
380     (if arg-pos
381         `(defmethod ,name ,@(subseq stuff 0 arg-pos)
382            ,(nth-value 2 (sb-pcl::parse-specialized-lambda-list
383                           (elt stuff arg-pos))))
384         `(defmethod ,name "<illegal syntax>"))))