1 ;;;; some macros and constants that are object-format-specific or are
2 ;;;; used for defining the object format
4 ;;;; This software is part of the SBCL system. See the README file for
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.
15 ;;;; other miscellaneous stuff
17 ;;; This returns a form that returns a dual-word aligned number of bytes when
18 ;;; given a number of words.
20 ;;; FIXME: should be a function
21 ;;; FIXME: should be called PAD-DATA-BLOCK-SIZE
22 (defmacro pad-data-block (words)
23 `(logandc2 (+ (ash ,words word-shift) lowtag-mask) lowtag-mask))
25 ;;;; primitive object definition stuff
27 (defun remove-keywords (options keywords)
28 (cond ((null options) nil)
29 ((member (car options) keywords)
30 (remove-keywords (cddr options) keywords))
32 (list* (car options) (cadr options)
33 (remove-keywords (cddr options) keywords)))))
35 (def!struct (prim-object-slot
36 (:constructor make-slot (name docs rest-p offset options))
37 (:make-load-form-fun just-dump-it-normally)
39 (name nil :type symbol)
40 (docs nil :type (or null simple-string))
41 (rest-p nil :type (member t nil))
42 (offset 0 :type fixnum)
43 (options nil :type list))
45 (def!struct (primitive-object (:make-load-form-fun just-dump-it-normally))
46 (name nil :type symbol)
47 (widetag nil :type symbol)
48 (lowtag nil :type symbol)
49 (options nil :type list)
50 (slots nil :type list)
52 (variable-length-p nil :type (member t nil)))
54 (defvar *primitive-objects* nil)
56 (defun %define-primitive-object (primobj)
57 (let ((name (primitive-object-name primobj)))
58 (setf *primitive-objects*
60 (remove name *primitive-objects*
61 :key #'primitive-object-name :test #'eq)))
64 (defmacro define-primitive-object
65 ((name &key lowtag widetag alloc-trans (type t))
67 (collect ((slots) (exports) (constants) (forms) (inits))
68 (let ((offset (if widetag 1 0))
69 (variable-length-p nil))
70 (dolist (spec slot-specs)
71 (when variable-length-p
72 (error "No more slots can follow a :rest-p slot."))
74 (slot-name &rest options
75 &key docs rest-p (length (if rest-p 0 1))
76 ((:type slot-type) t) init
77 (ref-known nil ref-known-p) ref-trans
78 (set-known nil set-known-p) set-trans
81 (if (atom spec) (list spec) spec)
82 (slots (make-slot slot-name docs rest-p offset
83 (remove-keywords options
84 '(:docs :rest-p :length))))
85 (let ((offset-sym (symbolicate name "-" slot-name
86 (if rest-p "-OFFSET" "-SLOT"))))
87 (constants `(def!constant ,offset-sym ,offset
88 ,@(when docs (list docs))))
92 (forms `(defknown ,ref-trans (,type) ,slot-type ,ref-known)))
93 (forms `(def-reffer ,ref-trans ,offset ,lowtag)))
96 (forms `(defknown ,set-trans
97 ,(if (listp set-trans)
99 (list type slot-type))
102 (forms `(def-setter ,set-trans ,offset ,lowtag)))
105 (error ":REST-P and :CAS-TRANS incompatible."))
108 (defknown ,cas-trans (,type ,slot-type ,slot-type)
110 #!+compare-and-swap-vops
111 (def-casser ,cas-trans ,offset ,lowtag))))
113 (inits (cons init offset)))
115 (setf variable-length-p t))
116 (incf offset length)))
117 (unless variable-length-p
118 (let ((size (symbolicate name "-SIZE")))
119 (constants `(def!constant ,size ,offset))
122 (forms `(def-alloc ,alloc-trans ,offset ,variable-length-p ,widetag
125 (eval-when (:compile-toplevel :load-toplevel :execute)
126 (%define-primitive-object
127 ',(make-primitive-object :name name
132 :variable-length-p variable-length-p))
136 ;;;; stuff for defining reffers and setters
140 (defmacro def-reffer (name offset lowtag)
141 `(%def-reffer ',name ,offset ,lowtag))
142 (defmacro def-setter (name offset lowtag)
143 `(%def-setter ',name ,offset ,lowtag))
144 (defmacro def-alloc (name words variable-length-p header lowtag inits)
145 `(%def-alloc ',name ,words ,variable-length-p ,header ,lowtag ,inits))
146 #!+compare-and-swap-vops
147 (defmacro def-casser (name offset lowtag)
148 `(%def-casser ',name ,offset ,lowtag))
149 ;;; KLUDGE: The %DEF-FOO functions used to implement the macros here
150 ;;; are defined later in another file, since they use structure slot
151 ;;; setters defined later, and we can't have physical forward
152 ;;; references to structure slot setters because ANSI in its wisdom
153 ;;; allows the xc host CL to implement structure slot setters as SETF
154 ;;; expanders instead of SETF functions. -- WHN 2002-02-09
156 ;;;; some general constant definitions
158 ;;; FIXME: SC-NUMBER-LIMIT should probably be exported from SB!C
159 ;;; or SB!VM so that we don't need to do this extra IN-PACKAGE.
162 ;;; the maximum number of SCs in any implementation
163 (def!constant sc-number-limit 32)
165 ;;; Modular functions
167 ;;; For a documentation, see CUT-TO-WIDTH.
169 (defstruct modular-class
170 ;; hash: name -> { :GOOD | optimizer | ({modular-fun-info}*)}
171 (funs (make-hash-table :test 'eq))
172 ;; hash: modular-variant -> (prototype width)
174 ;; FIXME: Reimplement with generic function names of kind
175 ;; (MODULAR-VERSION prototype width)
176 (versions (make-hash-table :test 'eq))
177 ;; list of increasing widths + signedps
179 (defvar *untagged-unsigned-modular-class* (make-modular-class))
180 (defvar *untagged-signed-modular-class* (make-modular-class))
181 (defvar *tagged-modular-class* (make-modular-class))
182 (defun find-modular-class (kind signedp)
186 ((nil) *untagged-unsigned-modular-class*)
187 (t *untagged-signed-modular-class*)))
190 *tagged-modular-class*)))
192 (defstruct modular-fun-info
193 (name (missing-arg) :type symbol)
194 (width (missing-arg) :type (integer 0))
195 (signedp (missing-arg) :type boolean)
196 (lambda-list (missing-arg) :type list)
197 (prototype (missing-arg) :type symbol))
199 (defun find-modular-version (fun-name kind signedp width)
200 (let ((infos (gethash fun-name (modular-class-funs (find-modular-class kind signedp)))))
202 (find-if (lambda (mfi)
203 (aver (eq (modular-fun-info-signedp mfi) signedp))
204 (>= (modular-fun-info-width mfi) width))
208 ;;; Return (VALUES prototype-name width)
209 (defun modular-version-info (name kind signedp)
210 (values-list (gethash name (modular-class-versions (find-modular-class kind signedp)))))
212 (defun %define-modular-fun (name lambda-list prototype kind signedp width)
213 (let* ((class (find-modular-class kind signedp))
214 (funs (modular-class-funs class))
215 (versions (modular-class-versions class))
216 (infos (the list (gethash prototype funs)))
217 (info (find-if (lambda (mfi)
218 (and (eq (modular-fun-info-signedp mfi) signedp)
219 (= (modular-fun-info-width mfi) width)))
222 (unless (and (eq name (modular-fun-info-name info))
223 (= (length lambda-list)
224 (length (modular-fun-info-lambda-list info))))
225 (setf (modular-fun-info-name info) name)
226 (style-warn "Redefining modular version ~S of ~S for ~
227 ~:[un~;~]signed width ~S."
228 name prototype signedp width))
229 (setf (gethash prototype funs)
231 (list (make-modular-fun-info :name name
234 :lambda-list lambda-list
235 :prototype prototype))
237 #'< :key #'modular-fun-info-width)
238 (gethash name versions)
239 (list prototype width)))
240 (setf (modular-class-widths class)
241 (merge 'list (list (cons width signedp)) (modular-class-widths class)
244 (defmacro define-modular-fun (name lambda-list prototype kind signedp width)
245 (check-type name symbol)
246 (check-type prototype symbol)
247 (check-type kind (member :untagged :tagged))
248 (check-type width unsigned-byte)
249 (dolist (arg lambda-list)
250 (when (member arg lambda-list-keywords)
251 (error "Lambda list keyword ~S is not supported for ~
252 modular function lambda lists." arg)))
254 (%define-modular-fun ',name ',lambda-list ',prototype ',kind ',signedp ,width)
255 (defknown ,name ,(mapcar (constantly 'integer) lambda-list)
257 ((nil) 'unsigned-byte)
260 (foldable flushable movable)
261 :derive-type (make-modular-fun-type-deriver
262 ',prototype ',kind ,width ',signedp))))
264 (defun %define-good-modular-fun (name kind signedp)
265 (setf (gethash name (modular-class-funs (find-modular-class kind signedp))) :good)
268 (defmacro define-good-modular-fun (name kind signedp)
269 (check-type name symbol)
270 (check-type kind (member :untagged :tagged))
271 `(%define-good-modular-fun ',name ',kind ',signedp))
273 (defmacro define-modular-fun-optimizer
274 (name ((&rest lambda-list) kind signedp &key (width (gensym "WIDTH")))
276 (check-type name symbol)
277 (check-type kind (member :untagged :tagged))
278 (dolist (arg lambda-list)
279 (when (member arg lambda-list-keywords)
280 (error "Lambda list keyword ~S is not supported for ~
281 modular function lambda lists." arg)))
282 (with-unique-names (call args)
283 `(setf (gethash ',name (modular-class-funs (find-modular-class ',kind ',signedp)))
284 (lambda (,call ,width)
285 (declare (type basic-combination ,call)
286 (type (integer 0) ,width))
287 (let ((,args (basic-combination-args ,call)))
288 (when (= (length ,args) ,(length lambda-list))
289 (destructuring-bind ,lambda-list ,args
290 (declare (type lvar ,@lambda-list))