/[cmucl]/src/pcl/fin.lisp
ViewVC logotype

Contents of /src/pcl/fin.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations)
Thu Nov 28 00:51:35 2002 UTC (11 years, 4 months ago) by pmai
Branch: MAIN
CVS Tags: release-18e-base, release-18e-pre2, cold-pcl-base, release-18e, release-18e-pre1
Branch point for: release-18e-branch, cold-pcl
Changes since 1.18: +9 -4 lines
Entomotomy Bug: sxhash-on-pcl-instances-returns-42

This commit is a slight rewrite of the previously committed fix, which
sxhash-instance work even when handed non-PCL instances and
funcallable-instances, while enabling the use of stored hash-codes for
all PCL funcallable-instances.
1 ;;;-*-Mode:LISP; Package:(PCL Lisp 1000); Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; *************************************************************************
4 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5 ;;; All rights reserved.
6 ;;;
7 ;;; Use and copying of this software and preparation of derivative works
8 ;;; based upon this software are permitted. Any distribution of this
9 ;;; software or derivative works must comply with all applicable United
10 ;;; States export control laws.
11 ;;;
12 ;;; This software is made available AS IS, and Xerox Corporation makes no
13 ;;; warranty about the software, its performance or its conformity to any
14 ;;; specification.
15 ;;;
16 ;;; Any person obtaining a copy of this software is requested to send their
17 ;;; name and post office or electronic mail address to:
18 ;;; CommonLoops Coordinator
19 ;;; Xerox PARC
20 ;;; 3333 Coyote Hill Rd.
21 ;;; Palo Alto, CA 94304
22 ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
23 ;;;
24 ;;; Suggestions, comments and requests for improvements are also welcome.
25 ;;; *************************************************************************
26 ;;;
27
28 (ext:file-comment
29 "$Header: /tiger/var/lib/cvsroots/cmucl/src/pcl/fin.lisp,v 1.19 2002/11/28 00:51:35 pmai Exp $")
30 ;;;
31
32 ;;
33 ;;;;;; FUNCALLABLE INSTANCES
34 ;;
35
36 #|
37
38 Generic functions are instances with meta class funcallable-standard-class.
39 Instances with this meta class are called funcallable-instances (FINs for
40 short). They behave something like lexical closures in that they have data
41 associated with them (which is used to store the slots) and are funcallable.
42 When a funcallable instance is funcalled, the function that is invoked is
43 called the funcallable-instance-function. The funcallable-instance-function
44 of a funcallable instance can be changed.
45
46 This file implements low level code for manipulating funcallable instances.
47
48 It is possible to implement funcallable instances in pure Common Lisp. A
49 simple implementation which uses lexical closures as the instances and a
50 hash table to record that the lexical closures are funcallable instances
51 is easy to write. Unfortunately, this implementation adds significant
52 overhead:
53
54 to generic-function-invocation (1 function call)
55 to slot-access (1 function call or one hash table lookup)
56 to class-of a generic-function (1 hash-table lookup)
57
58 In addition, it would prevent the funcallable instances from being garbage
59 collected. In short, the pure Common Lisp implementation really isn't
60 practical.
61
62 Instead, PCL uses a specially tailored implementation for each Common Lisp and
63 makes no attempt to provide a purely portable implementation. The specially
64 tailored implementations are based on the lexical closure's provided by that
65 implementation and are fairly short and easy to write.
66
67 Some of the implementation dependent code in this file was originally written
68 by someone in the employ of the vendor of that Common Lisp. That code is
69 explicitly marked saying who wrote it.
70
71 |#
72
73 (in-package :pcl)
74
75 ;;;
76 ;;; The first part of the file contains the implementation dependent code to
77 ;;; implement funcallable instances. Each implementation must provide the
78 ;;; following functions and macros:
79 ;;;
80 ;;; ALLOCATE-FUNCALLABLE-INSTANCE-1 ()
81 ;;; should create and return a new funcallable instance. The
82 ;;; funcallable-instance-data slots must be initialized to NIL.
83 ;;; This is called by allocate-funcallable-instance and by the
84 ;;; bootstrapping code.
85 ;;;
86 ;;; FUNCALLABLE-INSTANCE-P (x)
87 ;;; the obvious predicate. This should be an INLINE function.
88 ;;; it must be funcallable, but it would be nice if it compiled
89 ;;; open.
90 ;;;
91 ;;; SET-FUNCALLABLE-INSTANCE-FUNCTION (fin new-value)
92 ;;; change the fin so that when it is funcalled, the new-value
93 ;;; function is called. Note that it is legal for new-value
94 ;;; to be copied before it is installed in the fin, specifically
95 ;;; there is no accessor for a FIN's function so this function
96 ;;; does not have to preserve the actual new value. The new-value
97 ;;; argument can be any funcallable thing, a closure, lambda
98 ;;; compiled code etc. This function must coerce those values
99 ;;; if necessary.
100 ;;; NOTE: new-value is almost always a compiled closure. This
101 ;;; is the important case to optimize.
102 ;;;
103 ;;; FUNCALLABLE-INSTANCE-DATA-1 (fin data-name)
104 ;;; should return the value of the data named data-name in the fin.
105 ;;; data-name is one of the symbols in the list which is the value
106 ;;; of funcallable-instance-data. Since data-name is almost always
107 ;;; a quoted symbol and funcallable-instance-data is a constant, it
108 ;;; is possible (and worthwhile) to optimize the computation of
109 ;;; data-name's offset in the data part of the fin.
110 ;;; This must be SETF'able.
111 ;;;
112
113 (declaim (notinline called-fin-without-function))
114 (defun called-fin-without-function (&rest args)
115 (declare (ignore args))
116 (error "Attempt to funcall a funcallable-instance without first~%~
117 setting its funcallable-instance-function."))
118
119
120 ;;;; Implementation of funcallable instances for CMU Common Lisp:
121 ;;;
122 (defstruct (pcl-funcallable-instance
123 (:alternate-metaclass kernel:funcallable-instance
124 kernel:random-pcl-class
125 kernel:make-random-pcl-class)
126 (:type kernel:funcallable-structure)
127 (:constructor allocate-funcallable-instance-1 ())
128 (:conc-name nil))
129 ;;
130 ;; PCL wrapper is in the layout slot.
131 ;;
132 ;; PCL data vector.
133 (pcl-funcallable-instance-slots nil)
134 ;;
135 ;; The debug-name for this function.
136 (funcallable-instance-name nil)
137 ;;
138 ;; Hash code.
139 (hash-code (get-instance-hash-code) :type fixnum))
140
141 (defmacro fsc-instance-hash-code (fin)
142 `(kernel:%funcallable-instance-info ,fin 2))
143
144 ;;; Note: returns true for non-pcl funcallable structures.
145 (import 'kernel:funcallable-instance-p)
146
147
148 ;;; SET-FUNCALLABLE-INSTANCE-FUNCTION -- Interface
149 ;;;
150 ;;; Set the function that is called when FIN is called.
151 ;;;
152 (defun set-funcallable-instance-function (fin new-value)
153 (declare (type function new-value))
154 (assert (funcallable-instance-p fin))
155 (setf (kernel:funcallable-instance-function fin) new-value))
156
157
158 ;;;; Slightly Higher-Level stuff built on the implementation-dependent stuff.
159 ;;;
160
161 (defmacro fsc-instance-p (fin)
162 `(funcallable-instance-p ,fin))
163
164 (defmacro fsc-instance-wrapper (fin)
165 `(kernel:%funcallable-instance-layout ,fin))
166
167 (defmacro fsc-instance-slots (fin)
168 `(kernel:%funcallable-instance-info ,fin 0))
169
170 ;;; Implement proper sxhashing of standard instances.
171 (defun common-lisp::sxhash-instance (instance)
172 (cond
173 ((and (std-instance-p instance)
174 (typep (std-instance-wrapper instance) 'wrapper))
175 (std-instance-hash-code instance))
176 ((and (fsc-instance-p instance)
177 (typep (fsc-instance-wrapper instance) 'wrapper))
178 (fsc-instance-hash-code instance))
179 (t
180 42)))

  ViewVC Help
Powered by ViewVC 1.1.5