x86-64: Treat more symbols as having immediate storage class
[sbcl.git] / src / compiler / early-globaldb.lisp
blobece062a267a01a0f6e4fa78fc65b7ad4e0f38051
1 ;;;; This file contains stuff that was split out from 'globaldb.lisp'
2 ;;;; to satisfy build-order constraints.
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!IMPL")
15 ;;; Similar to FUNCTION, but the result type is "exactly" specified:
16 ;;; if it is an object type, then the function returns exactly one
17 ;;; value, if it is a short form of VALUES, then this short form
18 ;;; specifies the exact number of values.
19 (def!type sfunction (args &optional result)
20 (let ((result (cond ((eq result '*) '*)
21 ((or (atom result)
22 (not (eq (car result) 'values)))
23 `(values ,result &optional))
24 ((intersection (cdr result) sb!xc:lambda-list-keywords)
25 result)
26 (t `(values ,@(cdr result) &optional)))))
27 `(function ,args ,result)))
29 (declaim (ftype (sfunction (t t t) (values t t)) info)
30 (ftype (sfunction (t t t) t) clear-info)
31 (ftype (sfunction (t t t t) t) (setf info)))
33 ;;; (:FUNCTION :TYPE) information is extracted through a wrapper.
34 ;;; The globaldb representation is not necessarily literally a CTYPE.
35 #-sb-xc-host
36 (declaim (ftype (sfunction (t) (values ctype boolean)) proclaimed-ftype))
38 ;;; At run time, we represent the type of a piece of INFO in the globaldb
39 ;;; by a small integer between 1 and 63. [0 is reserved for internal use.]
40 ;;; CLISP, and maybe others, need EVAL-WHEN because without it, the constant
41 ;;; is not seen by the "#." expression a few lines down.
42 (eval-when (:compile-toplevel :load-toplevel :execute)
43 (defconstant info-number-bits 6))
44 (def!type info-number () `(unsigned-byte ,info-number-bits))
46 ;;; A map from info-number to its META-INFO object.
47 ;;; The reverse mapping is obtained by reading the META-INFO.
48 (declaim (type (simple-vector #.(ash 1 info-number-bits)) *info-types*))
49 (!defglobal *info-types*
50 ;; Must be dumped as a literal for cold-load.
51 #.(make-array (ash 1 info-number-bits) :initial-element nil))
53 (defstruct (meta-info
54 (:constructor
55 !make-meta-info (number category kind type-spec
56 type-checker validate-function default))
57 (:copier nil))
58 ;; a number that uniquely identifies this object
59 (number nil :type info-number :read-only t)
60 ;; 2-part key to this piece of metainfo
61 (category nil :type keyword :read-only t)
62 (kind nil :type keyword :read-only t)
63 ;; a type specifier which info of this type must satisfy
64 (type-spec nil :type t :read-only t)
65 ;; Two functions called by (SETF INFO) before calling SET-INFO-VALUE.
66 ;; 1. A function that type-checks its argument and returns it,
67 ;; or signals an error.
68 ;; Some Lisps trip over their shoelaces trying to assert that
69 ;; a function is (function (t) t). Our code is fine though.
70 (type-checker nil :type #+sb-xc-host function #-sb-xc-host (sfunction (t) t)
71 :read-only t)
72 ;; 2. a function of two arguments, a name and new-value, which performs
73 ;; any other checks and/or side-effects including signaling an error.
74 (validate-function nil :type (or function null) :read-only t)
75 ;; If FUNCTIONP, then a function called when there is no information of
76 ;; this type. If not FUNCTIONP, then any object serving as a default.
77 (default nil :read-only t))
79 (declaim (freeze-type meta-info))
81 (defconstant +info-metainfo-type-num+ 0)
83 ;; Refer to info-vector.lisp for the meaning of this constant.
84 (defconstant +no-auxilliary-key+ 0)
86 ;; Return the globaldb info for SYMBOL. With respect to the state diagram
87 ;; presented at the definition of SYMBOL-PLIST, if the object in SYMBOL's
88 ;; info slot is LISTP, it is in state 1 or 3. Either way, take the CDR.
89 ;; Otherwise, it is in state 2 so return the value as-is.
90 ;; In terms of this function being named "-vector", implying always a vector,
91 ;; it is understood that NIL is a proxy for +NIL-PACKED-INFOS+, a vector.
93 #-sb-xc-host
94 (progn
95 #!-symbol-info-vops (declaim (inline symbol-info-vector))
96 (defun symbol-info-vector (symbol)
97 (let ((info-holder (symbol-info symbol)))
98 (truly-the (or null simple-vector)
99 (if (listp info-holder) (cdr info-holder) info-holder)))))
101 ;;; SYMBOL-INFO is a primitive object accessor defined in 'objdef.lisp'
102 ;;; But in the host Lisp, there is no such thing as a symbol-info slot.
103 ;;; Instead, symbol-info is kept in the host symbol's plist.
104 #+sb-xc-host
105 (defmacro symbol-info-vector (symbol) `(get ,symbol :sb-xc-globaldb-info))
107 ;; Perform the equivalent of (GET-INFO-VALUE KIND +INFO-METAINFO-TYPE-NUM+)
108 ;; but skipping the defaulting logic.
109 ;; Return zero or more META-INFOs that match on KIND, which is usually
110 ;; - though not always - a unique identifier for the (:TYPE :KIND) pair.
111 ;; Note that bypassing of defaults is critical for bootstrapping,
112 ;; since INFO is used to retrieve its own META-INFO at system-build time.
113 (defmacro !get-meta-infos (kind)
114 `(let* ((info-vector (symbol-info-vector ,kind))
115 (index (if info-vector
116 (packed-info-value-index info-vector +no-auxilliary-key+
117 +info-metainfo-type-num+))))
118 (if index (svref info-vector index))))
120 ;; (UNSIGNED-BYTE 16) is an arbitrarily generous limit on the number of
121 ;; cells in an info-vector. Most vectors have a fewer than a handful of things,
122 ;; and performance would need to be re-thought if more than about a dozen
123 ;; cells were in use. (It would want to become hash-based probably)
124 (declaim (ftype (function (simple-vector (or (eql 0) symbol) info-number)
125 (or null (unsigned-byte 16)))
126 packed-info-value-index))
128 ;; Return the META-INFO object for CATEGORY and KIND, signaling an error
129 ;; if not found and ERRORP is non-nil. Note that the two-level logical hierarchy
130 ;; of CATEGORY + KIND is physically grouped by KIND first, then CATEGORY.
131 ;; e.g. Searching for (:SETF :EXPANDER) searches for :EXPANDER and finds
132 ;; (#<:CAS :EXPANDER, 44> #<:SETF :EXPANDER, 43> #<:TYPE :EXPANDER, 25>)
133 ;; from which one is picked. This is slightly faster than searching first by
134 ;; CATEGORY, because in the case of :FUNCTION there would be ~11 things to sift
135 ;; through, whereas typically no more than 3 or 4 items have the same KIND.
137 (defun meta-info (category kind &optional (errorp t))
138 (or (let ((metadata (!get-meta-infos kind)))
139 (cond ((listp metadata) ; conveniently handles NIL
140 (dolist (info metadata nil) ; FIND is slower :-(
141 (when (eq (meta-info-category (truly-the meta-info info))
142 category)
143 (return info))))
144 ((eq (meta-info-category (truly-the meta-info metadata)) category)
145 metadata)))
146 ;; !GET-META-INFOS enforces that KIND is a symbol, therefore
147 ;; if a metaobject was found, CATEGORY was necessarily a symbol too.
148 ;; Otherwise, if the caller wants no error to be signaled on missing info,
149 ;; we must nevertheless enforce that CATEGORY was actually a symbol.
150 (if errorp
151 (error "(~S ~S) is not a defined info type." category kind)
152 (progn (the symbol category) nil)))) ; THE is for type-safety
154 ;;; Compiler macros for INFO functions.
156 ;;; These are defined ASAP so that when building the cross-compiler, all calls
157 ;;; occurring after compilation of "globaldb" (for known constant meta-info)
158 ;;; are transformed; and when executing the cross-compiler, *all* inlineable
159 ;;; calls for known constant meta-info are transformed;
160 ;;; and when running target code, calls with legal constants for the first two
161 ;;; arguments are transformed.
162 (macrolet ((def (name lambda-list form)
163 (assert (and (member 'category lambda-list)
164 (member 'kind lambda-list)))
165 `(define-compiler-macro ,name ,(append '(&whole .whole.) lambda-list)
166 (if (and (keywordp category) (keywordp kind))
167 ;; In the target Lisp, it's a STYLE-WARNING if this macro
168 ;; defers to a full call to #'INFO.
169 ;; If the cross-compilation host, if any info-type is
170 ;; defined, then it's an error not to find the meta-info.
171 ;; If no info-types are defined, silently defer.
172 (let ((meta-info
173 (and #+sb-xc-host (find-if #'identity *info-types*)
174 (meta-info category kind #-sb-xc-host nil))))
175 (if meta-info
176 ,form
177 (progn
178 #-sb-xc-host
179 (style-warn "(~S ~S) is not a defined info type."
180 category kind)
181 .whole.)))
182 .whole.))))
184 (def info (category kind name)
185 `(truly-the (values ,(meta-info-type-spec meta-info) boolean)
186 (get-info-value ,name ,(meta-info-number meta-info))))
188 (def (setf info) (new-value category kind name)
189 (let* (#+sb-xc-host (sb!xc:*gensym-counter* sb!xc:*gensym-counter*)
190 (tin (meta-info-number meta-info)) ; info-type id number
191 (type-spec (meta-info-type-spec meta-info))
192 (new (make-symbol "NEW"))
193 (check
194 (when (meta-info-validate-function meta-info)
195 ;; is (or ... null), but non-null at macroexpansion time
196 ;; implies non-null at runtime.
197 `(truly-the function
198 (meta-info-validate-function
199 (truly-the meta-info (svref *info-types* ,tin)))))))
200 `(let ((,new ,new-value))
201 ;; enforce type-correctness regardless of enclosing policy
202 (let ((,new (locally (declare (optimize (safety 3)))
203 (the ,type-spec ,new))))
204 ,@(when check
205 `((funcall ,check ,name ,new)))
206 (set-info-value ,name ,tin ,new)))))
208 (def clear-info (category kind name)
209 `(clear-info-values ,name '(,(meta-info-number meta-info)))))
211 ;; interface to %ATOMIC-SET-INFO-VALUE
212 ;; GET-INFO-VALUE-INITIALIZING is a restricted case of this,
213 ;; and perhaps could be implemented as such.
214 ;; Atomic update will be important for making the fasloader threadsafe
215 ;; using a predominantly lock-free design, and other nice things.
216 (defmacro atomic-set-info-value (category kind name lambda)
217 (with-unique-names (info-number proc)
218 `(let ((,info-number
219 ,(if (and (keywordp category) (keywordp kind))
220 (meta-info-number (meta-info category kind))
221 `(meta-info-number (meta-info ,category ,kind)))))
222 ,(if (and (listp lambda) (eq (car lambda) 'lambda))
223 ;; rewrite as FLET because the compiler is unable to dxify
224 ;; (DX-LET ((x (LAMBDA <whatever>))) (F x))
225 (destructuring-bind (lambda-list . body) (cdr lambda)
226 `(dx-flet ((,proc ,lambda-list ,@body))
227 (%atomic-set-info-value ,name ,info-number #',proc)))
228 `(%atomic-set-info-value ,name ,info-number ,lambda)))))
230 ;; Perform the approximate equivalent operations of retrieving
231 ;; (INFO :CATEGORY :KIND NAME), but if no info is found, invoke CREATION-FORM
232 ;; to produce an object that becomes the value for that piece of info, storing
233 ;; and returning it. The entire sequence behaves atomically but with a proviso:
234 ;; the creation form's result may be discarded, and another object returned
235 ;; instead (presumably) from another thread's execution of the creation form.
236 ;; If constructing the object has either non-trivial cost, or deleterious
237 ;; side-effects from making and discarding its result, do NOT use this macro.
238 ;; A mutex-guarded table would probably be more appropriate in such cases.
240 (defmacro get-info-value-initializing (category kind name creation-form)
241 (let ((proc (make-symbol "THUNK")))
242 `(dx-flet ((,proc () ,creation-form))
243 (%get-info-value-initializing
244 ,(if (and (keywordp category) (keywordp kind))
245 (meta-info-number (meta-info category kind))
246 `(meta-info-number (meta-info ,category ,kind)))
247 ,name #',proc))))
249 ;;;; boolean attribute utilities
250 ;;;;
251 ;;;; We need to maintain various sets of boolean attributes for known
252 ;;;; functions and VOPs. To save space and allow for quick set
253 ;;;; operations, we represent the attributes as bits in a fixnum.
255 (in-package "SB!C")
256 (deftype attributes () 'fixnum)
258 ;;; Given a list of attribute names and an alist that translates them
259 ;;; to masks, return the OR of the masks.
260 (defun encode-attribute-mask (names universe)
261 (loop for name in names
262 for pos = (position name universe)
263 sum (if pos (ash 1 pos) (error "unknown attribute name: ~S" name))))
265 (defun decode-attribute-mask (bits universe)
266 (loop for name across universe
267 for mask = 1 then (ash mask 1)
268 when (logtest mask bits) collect name))
270 ;;; Define a new class of boolean attributes, with the attributes
271 ;;; having the specified ATTRIBUTE-NAMES. NAME is the name of the
272 ;;; class, which is used to generate some macros to manipulate sets of
273 ;;; the attributes:
275 ;;; NAME-attributep attributes attribute-name*
276 ;;; Return true if one of the named attributes is present, false
277 ;;; otherwise. When set with SETF, updates the place Attributes
278 ;;; setting or clearing the specified attributes.
280 ;;; NAME-attributes attribute-name*
281 ;;; Return a set of the named attributes.
282 (defmacro !def-boolean-attribute (name &rest attribute-names)
283 (let ((vector (coerce attribute-names 'vector))
284 (constructor (symbolicate name "-ATTRIBUTES"))
285 (test-name (symbolicate name "-ATTRIBUTEP")))
286 `(progn
287 (defmacro ,constructor (&rest attribute-names)
288 "Automagically generated boolean attribute creation function.
289 See !DEF-BOOLEAN-ATTRIBUTE."
290 (encode-attribute-mask attribute-names ,vector))
291 (defun ,(symbolicate "DECODE-" name "-ATTRIBUTES") (attributes)
292 (decode-attribute-mask attributes ,vector))
293 (defmacro ,test-name (attributes &rest attribute-names)
294 "Automagically generated boolean attribute test function.
295 See !DEF-BOOLEAN-ATTRIBUTE."
296 `(logtest (the attributes ,attributes)
297 (,',constructor ,@attribute-names)))
298 (define-setf-expander ,test-name (place &rest attributes
299 &environment env)
300 "Automagically generated boolean attribute setter. See
301 !DEF-BOOLEAN-ATTRIBUTE."
302 (multiple-value-bind (temps values stores setter getter)
303 (#+sb-xc-host get-setf-expansion
304 #-sb-xc-host sb!xc:get-setf-expansion place env)
305 (when (cdr stores)
306 (error "multiple store variables for ~S" place))
307 (let ((newval (sb!xc:gensym))
308 (n-place (sb!xc:gensym))
309 (mask (encode-attribute-mask attributes ,vector)))
310 (values `(,@temps ,n-place)
311 `(,@values ,getter)
312 `(,newval)
313 `(let ((,(first stores)
314 (if ,newval
315 (logior ,n-place ,mask)
316 (logandc2 ,n-place ,mask))))
317 ,setter
318 ,newval)
319 `(,',test-name ,n-place ,@attributes))))))))
321 ;;; And now for some gratuitous pseudo-abstraction...
323 ;;; ATTRIBUTES-UNION
324 ;;; Return the union of all the sets of boolean attributes which are its
325 ;;; arguments.
326 ;;; ATTRIBUTES-INTERSECTION
327 ;;; Return the intersection of all the sets of boolean attributes which
328 ;;; are its arguments.
329 ;;; ATTRIBUTES=
330 ;;; True if the attributes present in ATTR1 are identical to
331 ;;; those in ATTR2.
332 (defmacro attributes-union (&rest attributes)
333 `(the attributes
334 (logior ,@(mapcar (lambda (x) `(the attributes ,x)) attributes))))
335 (defmacro attributes-intersection (&rest attributes)
336 `(the attributes
337 (logand ,@(mapcar (lambda (x) `(the attributes ,x)) attributes))))
338 (declaim (ftype (function (attributes attributes) boolean) attributes=))
339 #!-sb-fluid (declaim (inline attributes=))
340 (defun attributes= (attr1 attr2)
341 (eql attr1 attr2))