release, will be tagged as sbcl_1_0_27
[sbcl/tcr.git] / src / code / host-alieneval.lisp
blob267a652d77995841937ce504133ebc7b5386fe90
1 ;;;; the part of the Alien implementation which is needed at
2 ;;;; cross-compilation time
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!ALIEN")
15 (/show0 "host-alieneval.lisp 15")
17 ;;;; utility functions
19 (defun align-offset (offset alignment)
20 (let ((extra (rem offset alignment)))
21 (if (zerop extra) offset (+ offset (- alignment extra)))))
23 (defun guess-alignment (bits)
24 (cond ((null bits) nil)
25 #!-(or (and x86 (not win32)) (and ppc darwin)) ((> bits 32) 64)
26 ((> bits 16) 32)
27 ((> bits 8) 16)
28 ((> bits 1) 8)
29 (t 1)))
31 ;;;; ALIEN-TYPE-INFO stuff
33 (eval-when (#-sb-xc :compile-toplevel :execute :load-toplevel)
35 (defstruct (alien-type-class (:copier nil))
36 (name nil :type symbol)
37 (include nil :type (or null alien-type-class))
38 (unparse nil :type (or null function))
39 (type= nil :type (or null function))
40 (lisp-rep nil :type (or null function))
41 (alien-rep nil :type (or null function))
42 (extract-gen nil :type (or null function))
43 (deposit-gen nil :type (or null function))
44 (naturalize-gen nil :type (or null function))
45 (deport-gen nil :type (or null function))
46 (deport-alloc-gen nil :type (or null function))
47 (deport-pin-p nil :type (or null function))
48 ;; Cast?
49 (arg-tn nil :type (or null function))
50 (result-tn nil :type (or null function))
51 (subtypep nil :type (or null function)))
52 (def!method print-object ((type-class alien-type-class) stream)
53 (print-unreadable-object (type-class stream :type t)
54 (prin1 (alien-type-class-name type-class) stream)))
56 (defun alien-type-class-or-lose (name)
57 (or (gethash name *alien-type-classes*)
58 (error "no alien type class ~S" name)))
60 (defun create-alien-type-class-if-necessary (name include)
61 (let ((old (gethash name *alien-type-classes*))
62 (include (and include (alien-type-class-or-lose include))))
63 (if old
64 (setf (alien-type-class-include old) include)
65 (setf (gethash name *alien-type-classes*)
66 (make-alien-type-class :name name :include include)))))
68 (defparameter *method-slot-alist*
69 '((:unparse . alien-type-class-unparse)
70 (:type= . alien-type-class-type=)
71 (:subtypep . alien-type-class-subtypep)
72 (:lisp-rep . alien-type-class-lisp-rep)
73 (:alien-rep . alien-type-class-alien-rep)
74 (:extract-gen . alien-type-class-extract-gen)
75 (:deposit-gen . alien-type-class-deposit-gen)
76 (:naturalize-gen . alien-type-class-naturalize-gen)
77 (:deport-gen . alien-type-class-deport-gen)
78 (:deport-alloc-gen . alien-type-class-deport-alloc-gen)
79 (:deport-pin-p . alien-type-class-deport-pin-p)
80 ;; cast?
81 (:arg-tn . alien-type-class-arg-tn)
82 (:result-tn . alien-type-class-result-tn)))
84 (defun method-slot (method)
85 (cdr (or (assoc method *method-slot-alist*)
86 (error "no method ~S" method))))
88 ) ; EVAL-WHEN
90 ;;; We define a keyword "BOA" constructor so that we can reference the
91 ;;; slot names in init forms.
92 (def!macro define-alien-type-class ((name &key include include-args)
93 &rest slots)
94 (let ((defstruct-name (symbolicate "ALIEN-" name "-TYPE")))
95 (multiple-value-bind (include include-defstruct overrides)
96 (etypecase include
97 (null
98 (values nil 'alien-type nil))
99 (symbol
100 (values
101 include
102 (symbolicate "ALIEN-" include "-TYPE")
103 nil))
104 (list
105 (values
106 (car include)
107 (symbolicate "ALIEN-" (car include) "-TYPE")
108 (cdr include))))
109 `(progn
110 (eval-when (:compile-toplevel :load-toplevel :execute)
111 (create-alien-type-class-if-necessary ',name ',(or include 'root)))
112 (def!struct (,defstruct-name
113 (:include ,include-defstruct
114 (class ',name)
115 ,@overrides)
116 (:constructor
117 ,(symbolicate "MAKE-" defstruct-name)
118 (&key class bits alignment
119 ,@(mapcar (lambda (x)
120 (if (atom x) x (car x)))
121 slots)
122 ,@include-args
123 ;; KLUDGE
124 &aux (alignment (or alignment (guess-alignment bits))))))
125 ,@slots)))))
127 (def!macro define-alien-type-method ((class method) lambda-list &rest body)
128 (let ((defun-name (symbolicate class "-" method "-METHOD")))
129 `(progn
130 (defun ,defun-name ,lambda-list
131 ,@body)
132 (setf (,(method-slot method) (alien-type-class-or-lose ',class))
133 #',defun-name))))
135 (def!macro invoke-alien-type-method (method type &rest args)
136 (let ((slot (method-slot method)))
137 (once-only ((type type))
138 `(funcall (do ((class (alien-type-class-or-lose (alien-type-class ,type))
139 (alien-type-class-include class)))
140 ((null class)
141 (error "method ~S not defined for ~S"
142 ',method (alien-type-class ,type)))
143 (let ((fn (,slot class)))
144 (when fn
145 (return fn))))
146 ,type ,@args))))
148 ;;;; type parsing and unparsing
150 ;;; CMU CL used COMPILER-LET to bind *AUXILIARY-TYPE-DEFINITIONS*, and
151 ;;; COMPILER-LET is no longer supported by ANSI or SBCL. Instead, we
152 ;;; follow the suggestion in CLTL2 of using SYMBOL-MACROLET to achieve
153 ;;; a similar effect.
154 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
155 (defun auxiliary-type-definitions (env)
156 (multiple-value-bind (result expanded-p)
157 (sb!xc:macroexpand '&auxiliary-type-definitions& env)
158 (if expanded-p
159 result
160 ;; This is like having the global symbol-macro definition be
161 ;; NIL, but global symbol-macros make me vaguely queasy, so
162 ;; I do it this way instead.
163 nil))))
165 ;;; Process stuff in a new scope.
166 (def!macro with-auxiliary-alien-types (env &body body)
167 ``(symbol-macrolet ((&auxiliary-type-definitions&
168 ,(append *new-auxiliary-types*
169 (auxiliary-type-definitions ,env))))
170 ,(let ((*new-auxiliary-types* nil))
171 ,@body)))
173 ;;; Parse TYPE as an alien type specifier and return the resultant
174 ;;; ALIEN-TYPE structure.
175 (defun parse-alien-type (type env)
176 (declare (type (or sb!kernel:lexenv null) env))
177 (if (consp type)
178 (let ((translator (info :alien-type :translator (car type))))
179 (unless translator
180 (error "unknown alien type: ~S" type))
181 (funcall translator type env))
182 (ecase (info :alien-type :kind type)
183 (:primitive
184 (let ((translator (info :alien-type :translator type)))
185 (unless translator
186 (error "no translator for primitive alien type ~S" type))
187 (funcall translator (list type) env)))
188 (:defined
189 (or (info :alien-type :definition type)
190 (error "no definition for alien type ~S" type)))
191 (:unknown
192 (error "unknown alien type: ~S" type)))))
194 (defun auxiliary-alien-type (kind name env)
195 (declare (type (or sb!kernel:lexenv null) env))
196 (flet ((aux-defn-matches (x)
197 (and (eq (first x) kind) (eq (second x) name))))
198 (let ((in-auxiliaries
199 (or (find-if #'aux-defn-matches *new-auxiliary-types*)
200 (find-if #'aux-defn-matches (auxiliary-type-definitions env)))))
201 (if in-auxiliaries
202 (values (third in-auxiliaries) t)
203 (ecase kind
204 (:struct
205 (info :alien-type :struct name))
206 (:union
207 (info :alien-type :union name))
208 (:enum
209 (info :alien-type :enum name)))))))
211 (defun (setf auxiliary-alien-type) (new-value kind name env)
212 (declare (type (or sb!kernel:lexenv null) env))
213 (flet ((aux-defn-matches (x)
214 (and (eq (first x) kind) (eq (second x) name))))
215 (when (find-if #'aux-defn-matches *new-auxiliary-types*)
216 (error "attempt to multiply define ~A ~S" kind name))
217 (when (find-if #'aux-defn-matches (auxiliary-type-definitions env))
218 (error "attempt to shadow definition of ~A ~S" kind name)))
219 (push (list kind name new-value) *new-auxiliary-types*)
220 new-value)
222 (defun verify-local-auxiliaries-okay ()
223 (dolist (info *new-auxiliary-types*)
224 (destructuring-bind (kind name defn) info
225 (declare (ignore defn))
226 (when (ecase kind
227 (:struct
228 (info :alien-type :struct name))
229 (:union
230 (info :alien-type :union name))
231 (:enum
232 (info :alien-type :enum name)))
233 (error "attempt to shadow definition of ~A ~S" kind name)))))
235 (defun unparse-alien-type (type)
236 #!+sb-doc
237 "Convert the alien-type structure TYPE back into a list specification of
238 the type."
239 (declare (type alien-type type))
240 (let ((*record-types-already-unparsed* nil))
241 (%unparse-alien-type type)))
243 ;;; Does all the work of UNPARSE-ALIEN-TYPE. It's separate because we
244 ;;; need to recurse inside the binding of
245 ;;; *RECORD-TYPES-ALREADY-UNPARSED*.
246 (defun %unparse-alien-type (type)
247 (invoke-alien-type-method :unparse type))
249 ;;;; alien type defining stuff
251 (def!macro define-alien-type-translator (name lambda-list &body body)
252 (with-unique-names (whole env)
253 (let ((defun-name (symbolicate "ALIEN-" name "-TYPE-TRANSLATOR")))
254 (multiple-value-bind (body decls docs)
255 (sb!kernel:parse-defmacro lambda-list whole body name
256 'define-alien-type-translator
257 :environment env)
258 `(eval-when (:compile-toplevel :load-toplevel :execute)
259 (defun ,defun-name (,whole ,env)
260 (declare (ignorable ,env))
261 ,@decls
262 (block ,name
263 ,body))
264 (%define-alien-type-translator ',name #',defun-name ,docs))))))
266 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
267 (defun %define-alien-type-translator (name translator docs)
268 (declare (ignore docs))
269 (setf (info :alien-type :kind name) :primitive)
270 (setf (info :alien-type :translator name) translator)
271 (clear-info :alien-type :definition name)
272 #+nil
273 (setf (fdocumentation name 'alien-type) docs)
274 name))
276 (def!macro define-alien-type (name type &environment env)
277 #!+sb-doc
278 "Define the alien type NAME to be equivalent to TYPE. Name may be NIL for
279 STRUCT and UNION types, in which case the name is taken from the type
280 specifier."
281 (with-auxiliary-alien-types env
282 (let ((alien-type (parse-alien-type type env)))
283 `(eval-when (:compile-toplevel :load-toplevel :execute)
284 ,@(when *new-auxiliary-types*
285 `((%def-auxiliary-alien-types ',*new-auxiliary-types*)))
286 ,@(when name
287 `((%define-alien-type ',name ',alien-type)))))))
289 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
290 (defun %def-auxiliary-alien-types (types)
291 (dolist (info types)
292 ;; Clear up the type we're about to define from the toplevel
293 ;; *new-auxiliary-types* (local scopes take care of themselves).
294 ;; Unless this is done we never actually get back the full type
295 ;; from INFO, since the *new-auxiliary-types* have precendence.
296 (setf *new-auxiliary-types*
297 (remove info *new-auxiliary-types*
298 :test (lambda (a b)
299 (and (eq (first a) (first b))
300 (eq (second a) (second b))))))
301 (destructuring-bind (kind name defn) info
302 (macrolet ((frob (kind)
303 `(let ((old (info :alien-type ,kind name)))
304 (unless (or (null old) (alien-type-= old defn))
305 (warn
306 "redefining ~A ~S to be:~% ~S,~%was:~% ~S"
307 kind name defn old))
308 (setf (info :alien-type ,kind name) defn))))
309 (ecase kind
310 (:struct (frob :struct))
311 (:union (frob :union))
312 (:enum (frob :enum)))))))
313 (defun %define-alien-type (name new)
314 (ecase (info :alien-type :kind name)
315 (:primitive
316 (error "~S is a built-in alien type." name))
317 (:defined
318 (let ((old (info :alien-type :definition name)))
319 (unless (or (null old) (alien-type-= new old))
320 (warn "redefining ~S to be:~% ~S,~%was~% ~S"
321 name
322 (unparse-alien-type new)
323 (unparse-alien-type old)))))
324 (:unknown))
325 (setf (info :alien-type :definition name) new)
326 (setf (info :alien-type :kind name) :defined)
327 name))
329 ;;;; the root alien type
331 (eval-when (:compile-toplevel :load-toplevel :execute)
332 (create-alien-type-class-if-necessary 'root nil))
334 (def!struct (alien-type
335 (:make-load-form-fun sb!kernel:just-dump-it-normally)
336 (:constructor make-alien-type (&key class bits alignment
337 &aux (alignment (or alignment (guess-alignment bits))))))
338 (class 'root :type symbol)
339 (bits nil :type (or null unsigned-byte))
340 (alignment nil :type (or null unsigned-byte)))
341 (def!method print-object ((type alien-type) stream)
342 (print-unreadable-object (type stream :type t)
343 (prin1 (unparse-alien-type type) stream)))
345 ;;;; the SAP type
347 (define-alien-type-class (system-area-pointer))
349 (define-alien-type-translator system-area-pointer ()
350 (make-alien-system-area-pointer-type
351 :bits #!-alpha sb!vm:n-word-bits #!+alpha 64))
353 (define-alien-type-method (system-area-pointer :unparse) (type)
354 (declare (ignore type))
355 'system-area-pointer)
357 (define-alien-type-method (system-area-pointer :lisp-rep) (type)
358 (declare (ignore type))
359 'system-area-pointer)
361 (define-alien-type-method (system-area-pointer :alien-rep) (type)
362 (declare (ignore type))
363 'system-area-pointer)
365 (define-alien-type-method (system-area-pointer :naturalize-gen) (type alien)
366 (declare (ignore type))
367 alien)
369 (define-alien-type-method (system-area-pointer :deport-gen) (type object)
370 (declare (ignore type))
371 (/noshow "doing alien type method SYSTEM-AREA-POINTER :DEPORT-GEN" object)
372 object)
374 (define-alien-type-method (system-area-pointer :extract-gen) (type sap offset)
375 (declare (ignore type))
376 `(sap-ref-sap ,sap (/ ,offset sb!vm:n-byte-bits)))
378 ;;;; the ALIEN-VALUE type
380 (define-alien-type-class (alien-value :include system-area-pointer))
382 (define-alien-type-method (alien-value :lisp-rep) (type)
383 (declare (ignore type))
384 nil)
386 (define-alien-type-method (alien-value :naturalize-gen) (type alien)
387 `(%sap-alien ,alien ',type))
389 (define-alien-type-method (alien-value :deport-gen) (type value)
390 (declare (ignore type))
391 (/noshow "doing alien type method ALIEN-VALUE :DEPORT-GEN" value)
392 `(alien-sap ,value))
394 ;;; HEAP-ALIEN-INFO -- defstruct.
396 ;;; Information describing a heap-allocated alien.
397 (def!struct (heap-alien-info
398 (:make-load-form-fun sb!kernel:just-dump-it-normally))
399 ;; The type of this alien.
400 (type (missing-arg) :type alien-type)
401 ;; The form to evaluate to produce the SAP pointing to where in the heap
402 ;; it is.
403 (sap-form (missing-arg)))
404 (def!method print-object ((info heap-alien-info) stream)
405 (print-unreadable-object (info stream :type t)
406 (funcall (formatter "~S ~S")
407 stream
408 (heap-alien-info-sap-form info)
409 (unparse-alien-type (heap-alien-info-type info)))))
411 ;;;; Interfaces to the different methods
413 (defun alien-type-= (type1 type2)
414 #!+sb-doc
415 "Return T iff TYPE1 and TYPE2 describe equivalent alien types."
416 (or (eq type1 type2)
417 (and (eq (alien-type-class type1)
418 (alien-type-class type2))
419 (invoke-alien-type-method :type= type1 type2))))
421 (defun alien-subtype-p (type1 type2)
422 #!+sb-doc
423 "Return T iff the alien type TYPE1 is a subtype of TYPE2. Currently, the
424 only supported subtype relationships are is that any pointer type is a
425 subtype of (* t), and any array type first dimension will match
426 (array <eltype> nil ...). Otherwise, the two types have to be
427 ALIEN-TYPE-=."
428 (or (eq type1 type2)
429 (invoke-alien-type-method :subtypep type1 type2)))
431 (defun compute-naturalize-lambda (type)
432 `(lambda (alien ignore)
433 (declare (ignore ignore))
434 ,(invoke-alien-type-method :naturalize-gen type 'alien)))
436 (defun compute-deport-lambda (type)
437 (declare (type alien-type type))
438 (/noshow "entering COMPUTE-DEPORT-LAMBDA" type)
439 (multiple-value-bind (form value-type)
440 (invoke-alien-type-method :deport-gen type 'value)
441 `(lambda (value ignore)
442 (declare (type ,(or value-type
443 (compute-lisp-rep-type type)
444 `(alien ,type))
445 value)
446 (ignore ignore))
447 ,form)))
449 (defun compute-deport-alloc-lambda (type)
450 `(lambda (value ignore)
451 (declare (ignore ignore))
452 ,(invoke-alien-type-method :deport-alloc-gen type 'value)))
454 (defun compute-extract-lambda (type)
455 `(lambda (sap offset ignore)
456 (declare (type system-area-pointer sap)
457 (type unsigned-byte offset)
458 (ignore ignore))
459 (naturalize ,(invoke-alien-type-method :extract-gen type 'sap 'offset)
460 ',type)))
462 (def!macro maybe-with-pinned-objects (variables types &body body)
463 (declare (ignorable variables types))
464 (let ((pin-variables
465 ;; Only pin things on x86/x86-64, since on non-conservative
466 ;; gcs it'd imply disabling the GC. Which is something we
467 ;; don't want to do every time we're calling to C.
468 #!+(or x86 x86-64)
469 (loop for variable in variables
470 for type in types
471 when (invoke-alien-type-method :deport-pin-p type)
472 collect variable)))
473 (if pin-variables
474 `(with-pinned-objects ,pin-variables
475 ,@body)
476 `(progn
477 ,@body))))
479 (defun compute-deposit-lambda (type)
480 (declare (type alien-type type))
481 `(lambda (sap offset ignore value)
482 (declare (type system-area-pointer sap)
483 (type unsigned-byte offset)
484 (ignore ignore))
485 (let ((alloc-tmp (deport-alloc value ',type)))
486 (maybe-with-pinned-objects (alloc-tmp) (,type)
487 (let ((value (deport alloc-tmp ',type)))
488 ,(invoke-alien-type-method :deposit-gen type 'sap 'offset 'value)
489 ;; Note: the reason we don't just return the pre-deported value
490 ;; is because that would inhibit any (deport (naturalize ...))
491 ;; optimizations that might have otherwise happen. Re-naturalizing
492 ;; the value might cause extra consing, but is flushable, so probably
493 ;; results in better code.
494 (naturalize value ',type))))))
496 (defun compute-lisp-rep-type (type)
497 (invoke-alien-type-method :lisp-rep type))
499 (defun compute-alien-rep-type (type)
500 (invoke-alien-type-method :alien-rep type))
502 ;;;; default methods
504 (define-alien-type-method (root :unparse) (type)
505 `(<unknown-alien-type> ,(type-of type)))
507 (define-alien-type-method (root :type=) (type1 type2)
508 (declare (ignore type1 type2))
511 (define-alien-type-method (root :subtypep) (type1 type2)
512 (alien-type-= type1 type2))
514 (define-alien-type-method (root :lisp-rep) (type)
515 (declare (ignore type))
516 nil)
518 (define-alien-type-method (root :alien-rep) (type)
519 (declare (ignore type))
522 (define-alien-type-method (root :naturalize-gen) (type alien)
523 (declare (ignore alien))
524 (error "cannot represent ~S typed aliens" type))
526 (define-alien-type-method (root :deport-gen) (type object)
527 (declare (ignore object))
528 (error "cannot represent ~S typed aliens" type))
530 (define-alien-type-method (root :deport-alloc-gen) (type object)
531 (declare (ignore type))
532 object)
534 (define-alien-type-method (root :deport-pin-p) (type)
535 (declare (ignore type))
536 ;; Override this method to return T for classes which take a SAP to a
537 ;; GCable lisp object when deporting.
538 nil)
540 (define-alien-type-method (root :extract-gen) (type sap offset)
541 (declare (ignore sap offset))
542 (error "cannot represent ~S typed aliens" type))
544 (define-alien-type-method (root :deposit-gen) (type sap offset value)
545 `(setf ,(invoke-alien-type-method :extract-gen type sap offset) ,value))
547 (define-alien-type-method (root :arg-tn) (type state)
548 (declare (ignore state))
549 (error "Aliens of type ~S cannot be passed as arguments to CALL-OUT."
550 (unparse-alien-type type)))
552 (define-alien-type-method (root :result-tn) (type state)
553 (declare (ignore state))
554 (error "Aliens of type ~S cannot be returned from CALL-OUT."
555 (unparse-alien-type type)))
557 ;;;; the INTEGER type
559 (define-alien-type-class (integer)
560 (signed t :type (member t nil)))
562 (define-alien-type-translator signed (&optional (bits sb!vm:n-word-bits))
563 (make-alien-integer-type :bits bits))
565 (define-alien-type-translator integer (&optional (bits sb!vm:n-word-bits))
566 (make-alien-integer-type :bits bits))
568 (define-alien-type-translator unsigned (&optional (bits sb!vm:n-word-bits))
569 (make-alien-integer-type :bits bits :signed nil))
571 (define-alien-type-method (integer :unparse) (type)
572 (list (if (alien-integer-type-signed type) 'signed 'unsigned)
573 (alien-integer-type-bits type)))
575 (define-alien-type-method (integer :type=) (type1 type2)
576 (and (eq (alien-integer-type-signed type1)
577 (alien-integer-type-signed type2))
578 (= (alien-integer-type-bits type1)
579 (alien-integer-type-bits type2))))
581 (define-alien-type-method (integer :lisp-rep) (type)
582 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
583 (alien-integer-type-bits type)))
585 (define-alien-type-method (integer :alien-rep) (type)
586 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
587 (alien-integer-type-bits type)))
589 (define-alien-type-method (integer :naturalize-gen) (type alien)
590 (declare (ignore type))
591 alien)
593 (define-alien-type-method (integer :deport-gen) (type value)
594 (declare (ignore type))
595 value)
597 (define-alien-type-method (integer :extract-gen) (type sap offset)
598 (declare (type alien-integer-type type))
599 (let ((ref-fun
600 (if (alien-integer-type-signed type)
601 (case (alien-integer-type-bits type)
602 (8 'signed-sap-ref-8)
603 (16 'signed-sap-ref-16)
604 (32 'signed-sap-ref-32)
605 (64 'signed-sap-ref-64))
606 (case (alien-integer-type-bits type)
607 (8 'sap-ref-8)
608 (16 'sap-ref-16)
609 (32 'sap-ref-32)
610 (64 'sap-ref-64)))))
611 (if ref-fun
612 `(,ref-fun ,sap (/ ,offset sb!vm:n-byte-bits))
613 (error "cannot extract ~W-bit integers"
614 (alien-integer-type-bits type)))))
616 ;;;; the BOOLEAN type
618 (define-alien-type-class (boolean :include integer :include-args (signed)))
620 ;;; FIXME: Check to make sure that we aren't attaching user-readable
621 ;;; stuff to CL:BOOLEAN in any way which impairs ANSI compliance.
622 (define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
623 (make-alien-boolean-type :bits bits :signed nil))
625 (define-alien-type-method (boolean :unparse) (type)
626 `(boolean ,(alien-boolean-type-bits type)))
628 (define-alien-type-method (boolean :lisp-rep) (type)
629 (declare (ignore type))
630 `(member t nil))
632 (define-alien-type-method (boolean :naturalize-gen) (type alien)
633 (declare (ignore type))
634 `(not (zerop ,alien)))
636 (define-alien-type-method (boolean :deport-gen) (type value)
637 (declare (ignore type))
638 `(if ,value 1 0))
640 ;;;; the ENUM type
642 (define-alien-type-class (enum :include (integer (bits 32))
643 :include-args (signed))
644 name ; name of this enum (if any)
645 from ; alist from symbols to integers
646 to ; alist or vector from integers to symbols
647 kind ; kind of from mapping, :VECTOR or :ALIST
648 offset) ; offset to add to value for :VECTOR from mapping
650 (define-alien-type-translator enum (&whole
651 type name
652 &rest mappings
653 &environment env)
654 (cond (mappings
655 (let ((result (parse-enum name mappings)))
656 (when name
657 (multiple-value-bind (old old-p)
658 (auxiliary-alien-type :enum name env)
659 (when old-p
660 (unless (alien-type-= result old)
661 (cerror "Continue, clobbering the old definition"
662 "Incompatible alien enum type definition: ~S" name)
663 (setf (alien-type-from old) (alien-type-from result)
664 (alien-type-to old) (alien-type-to result)
665 (alien-type-kind old) (alien-type-kind result)
666 (alien-type-offset old) (alien-type-offset result)
667 (alien-type-signed old) (alien-type-signed result)))
668 (setf result old))
669 (unless old-p
670 (setf (auxiliary-alien-type :enum name env) result))))
671 result))
672 (name
673 (multiple-value-bind (result found)
674 (auxiliary-alien-type :enum name env)
675 (unless found
676 (error "unknown enum type: ~S" name))
677 result))
679 (error "empty enum type: ~S" type))))
681 (defun parse-enum (name elements)
682 (when (null elements)
683 (error "An enumeration must contain at least one element."))
684 (let ((min nil)
685 (max nil)
686 (from-alist ())
687 (prev -1))
688 (declare (list from-alist))
689 (dolist (el elements)
690 (multiple-value-bind (sym val)
691 (if (listp el)
692 (values (first el) (second el))
693 (values el (1+ prev)))
694 (setf prev val)
695 (unless (symbolp sym)
696 (error "The enumeration element ~S is not a symbol." sym))
697 (unless (integerp val)
698 (error "The element value ~S is not an integer." val))
699 (unless (and max (> max val)) (setq max val))
700 (unless (and min (< min val)) (setq min val))
701 (when (rassoc val from-alist)
702 (style-warn "The element value ~S is used more than once." val))
703 (when (assoc sym from-alist :test #'eq)
704 (error "The enumeration element ~S is used more than once." sym))
705 (push (cons sym val) from-alist)))
706 (let* ((signed (minusp min))
707 (min-bits (if signed
708 (1+ (max (integer-length min)
709 (integer-length max)))
710 (integer-length max))))
711 (when (> min-bits 32)
712 (error "can't represent enums needing more than 32 bits"))
713 (setf from-alist (sort from-alist #'< :key #'cdr))
714 (cond
715 ;; If range is at least 20% dense, use vector mapping. Crossover
716 ;; point solely on basis of space would be 25%. Vector mapping
717 ;; is always faster, so give the benefit of the doubt.
718 ((< 0.2 (/ (float (length from-alist)) (float (1+ (- max min)))))
719 ;; If offset is small and ignorable, ignore it to save time.
720 (when (< 0 min 10) (setq min 0))
721 (let ((to (make-array (1+ (- max min)))))
722 (dolist (el from-alist)
723 (setf (svref to (- (cdr el) min)) (car el)))
724 (make-alien-enum-type :name name :signed signed
725 :from from-alist :to to :kind
726 :vector :offset (- min))))
728 (make-alien-enum-type :name name :signed signed
729 :from from-alist
730 :to (mapcar (lambda (x) (cons (cdr x) (car x)))
731 from-alist)
732 :kind :alist))))))
734 (define-alien-type-method (enum :unparse) (type)
735 `(enum ,(alien-enum-type-name type)
736 ,@(let ((prev -1))
737 (mapcar (lambda (mapping)
738 (let ((sym (car mapping))
739 (value (cdr mapping)))
740 (prog1
741 (if (= (1+ prev) value)
743 `(,sym ,value))
744 (setf prev value))))
745 (alien-enum-type-from type)))))
747 (define-alien-type-method (enum :type=) (type1 type2)
748 (and (eq (alien-enum-type-name type1)
749 (alien-enum-type-name type2))
750 (equal (alien-enum-type-from type1)
751 (alien-enum-type-from type2))))
753 (define-alien-type-method (enum :lisp-rep) (type)
754 `(member ,@(mapcar #'car (alien-enum-type-from type))))
756 (define-alien-type-method (enum :naturalize-gen) (type alien)
757 (ecase (alien-enum-type-kind type)
758 (:vector
759 `(svref ',(alien-enum-type-to type)
760 (+ ,alien ,(alien-enum-type-offset type))))
761 (:alist
762 `(ecase ,alien
763 ,@(mapcar (lambda (mapping)
764 `(,(car mapping) ',(cdr mapping)))
765 (alien-enum-type-to type))))))
767 (define-alien-type-method (enum :deport-gen) (type value)
768 `(ecase ,value
769 ,@(mapcar (lambda (mapping)
770 `(,(car mapping) ,(cdr mapping)))
771 (alien-enum-type-from type))))
773 ;;;; the FLOAT types
775 (define-alien-type-class (float)
776 (type (missing-arg) :type symbol))
778 (define-alien-type-method (float :unparse) (type)
779 (alien-float-type-type type))
781 (define-alien-type-method (float :lisp-rep) (type)
782 (alien-float-type-type type))
784 (define-alien-type-method (float :alien-rep) (type)
785 (alien-float-type-type type))
787 (define-alien-type-method (float :naturalize-gen) (type alien)
788 (declare (ignore type))
789 alien)
791 (define-alien-type-method (float :deport-gen) (type value)
792 (declare (ignore type))
793 value)
795 (define-alien-type-class (single-float :include (float (bits 32))
796 :include-args (type)))
798 (define-alien-type-translator single-float ()
799 (make-alien-single-float-type :type 'single-float))
801 (define-alien-type-method (single-float :extract-gen) (type sap offset)
802 (declare (ignore type))
803 `(sap-ref-single ,sap (/ ,offset sb!vm:n-byte-bits)))
805 (define-alien-type-class (double-float :include (float (bits 64))
806 :include-args (type)))
808 (define-alien-type-translator double-float ()
809 (make-alien-double-float-type :type 'double-float))
811 (define-alien-type-method (double-float :extract-gen) (type sap offset)
812 (declare (ignore type))
813 `(sap-ref-double ,sap (/ ,offset sb!vm:n-byte-bits)))
816 ;;;; the POINTER type
818 (define-alien-type-class (pointer :include (alien-value (bits
819 #!-alpha
820 sb!vm:n-word-bits
821 #!+alpha 64)))
822 (to nil :type (or alien-type null)))
824 (define-alien-type-translator * (to &environment env)
825 (make-alien-pointer-type :to (if (eq to t) nil (parse-alien-type to env))))
827 (define-alien-type-method (pointer :unparse) (type)
828 (let ((to (alien-pointer-type-to type)))
829 `(* ,(if to
830 (%unparse-alien-type to)
831 t))))
833 (define-alien-type-method (pointer :type=) (type1 type2)
834 (let ((to1 (alien-pointer-type-to type1))
835 (to2 (alien-pointer-type-to type2)))
836 (if to1
837 (if to2
838 (alien-type-= to1 to2)
839 nil)
840 (null to2))))
842 (define-alien-type-method (pointer :subtypep) (type1 type2)
843 (and (alien-pointer-type-p type2)
844 (let ((to1 (alien-pointer-type-to type1))
845 (to2 (alien-pointer-type-to type2)))
846 (if to1
847 (if to2
848 (alien-subtype-p to1 to2)
850 (null to2)))))
852 (define-alien-type-method (pointer :deport-gen) (type value)
853 (/noshow "doing alien type method POINTER :DEPORT-GEN" type value)
854 (values
855 ;; FIXME: old version, highlighted a bug in xc optimization
856 `(etypecase ,value
857 (null
858 (int-sap 0))
859 (system-area-pointer
860 ,value)
861 ((alien ,type)
862 (alien-sap ,value)))
863 ;; new version, works around bug in xc optimization
864 #+nil
865 `(etypecase ,value
866 (system-area-pointer
867 ,value)
868 ((alien ,type)
869 (alien-sap ,value))
870 (null
871 (int-sap 0)))
872 `(or null system-area-pointer (alien ,type))))
874 ;;;; the MEM-BLOCK type
876 (define-alien-type-class (mem-block :include alien-value))
878 (define-alien-type-method (mem-block :extract-gen) (type sap offset)
879 (declare (ignore type))
880 `(sap+ ,sap (truncate ,offset sb!vm:n-byte-bits)))
882 (define-alien-type-method (mem-block :deposit-gen) (type sap offset value)
883 (let ((bits (alien-mem-block-type-bits type)))
884 (unless bits
885 (error "can't deposit aliens of type ~S (unknown size)" type))
886 `(sb!kernel:system-area-ub8-copy ,value 0 ,sap
887 (truncate ,offset sb!vm:n-byte-bits)
888 ',(truncate bits sb!vm:n-byte-bits))))
890 ;;;; the ARRAY type
892 (define-alien-type-class (array :include mem-block)
893 (element-type (missing-arg) :type alien-type)
894 (dimensions (missing-arg) :type list))
896 (define-alien-type-translator array (ele-type &rest dims &environment env)
898 (when dims
899 (unless (typep (first dims) '(or index null))
900 (error "The first dimension is not a non-negative fixnum or NIL: ~S"
901 (first dims)))
902 (let ((loser (find-if-not (lambda (x) (typep x 'index))
903 (rest dims))))
904 (when loser
905 (error "A dimension is not a non-negative fixnum: ~S" loser))))
907 (let ((parsed-ele-type (parse-alien-type ele-type env)))
908 (make-alien-array-type
909 :element-type parsed-ele-type
910 :dimensions dims
911 :alignment (alien-type-alignment parsed-ele-type)
912 :bits (if (and (alien-type-bits parsed-ele-type)
913 (every #'integerp dims))
914 (* (align-offset (alien-type-bits parsed-ele-type)
915 (alien-type-alignment parsed-ele-type))
916 (reduce #'* dims))))))
918 (define-alien-type-method (array :unparse) (type)
919 `(array ,(%unparse-alien-type (alien-array-type-element-type type))
920 ,@(alien-array-type-dimensions type)))
922 (define-alien-type-method (array :type=) (type1 type2)
923 (and (equal (alien-array-type-dimensions type1)
924 (alien-array-type-dimensions type2))
925 (alien-type-= (alien-array-type-element-type type1)
926 (alien-array-type-element-type type2))))
928 (define-alien-type-method (array :subtypep) (type1 type2)
929 (and (alien-array-type-p type2)
930 (let ((dim1 (alien-array-type-dimensions type1))
931 (dim2 (alien-array-type-dimensions type2)))
932 (and (= (length dim1) (length dim2))
933 (or (and dim2
934 (null (car dim2))
935 (equal (cdr dim1) (cdr dim2)))
936 (equal dim1 dim2))
937 (alien-subtype-p (alien-array-type-element-type type1)
938 (alien-array-type-element-type type2))))))
940 ;;;; the RECORD type
942 (def!struct (alien-record-field
943 (:make-load-form-fun sb!kernel:just-dump-it-normally))
944 (name (missing-arg) :type symbol)
945 (type (missing-arg) :type alien-type)
946 (bits nil :type (or unsigned-byte null))
947 (offset 0 :type unsigned-byte))
948 (def!method print-object ((field alien-record-field) stream)
949 (print-unreadable-object (field stream :type t)
950 (format stream
951 "~S ~S~@[:~D~]"
952 (alien-record-field-type field)
953 (alien-record-field-name field)
954 (alien-record-field-bits field))))
956 (define-alien-type-class (record :include mem-block)
957 (kind :struct :type (member :struct :union))
958 (name nil :type (or symbol null))
959 (fields nil :type list))
961 (define-alien-type-translator struct (name &rest fields &environment env)
962 (parse-alien-record-type :struct name fields env))
964 (define-alien-type-translator union (name &rest fields &environment env)
965 (parse-alien-record-type :union name fields env))
967 ;;; FIXME: This is really pretty horrible: we avoid creating new
968 ;;; ALIEN-RECORD-TYPE objects when a live one is flitting around the
969 ;;; system already. This way forwrd-references sans fields get get
970 ;;; "updated" for free to contain the field info. Maybe rename
971 ;;; MAKE-ALIEN-RECORD-TYPE to %MAKE-ALIEN-RECORD-TYPE and use
972 ;;; ENSURE-ALIEN-RECORD-TYPE instead. --NS 20040729
973 (defun parse-alien-record-type (kind name fields env)
974 (declare (type (or sb!kernel:lexenv null) env))
975 (flet ((frob-type (type new-fields alignment bits)
976 (setf (alien-record-type-fields type) new-fields
977 (alien-record-type-alignment type) alignment
978 (alien-record-type-bits type) bits)))
979 (cond (fields
980 (multiple-value-bind (new-fields alignment bits)
981 (parse-alien-record-fields kind fields env)
982 (let* ((old (and name (auxiliary-alien-type kind name env)))
983 (old-fields (and old (alien-record-type-fields old))))
984 (when (and old-fields
985 (notevery #'record-fields-match-p old-fields new-fields))
986 (cerror "Continue, clobbering the old definition."
987 "Incompatible alien record type definition~%Old: ~S~%New: ~S"
988 (unparse-alien-type old)
989 `(,(unparse-alien-record-kind kind)
990 ,name
991 ,@(mapcar #'unparse-alien-record-field new-fields)))
992 (frob-type old new-fields alignment bits))
993 (if old-fields
995 (let ((type (or old (make-alien-record-type :name name :kind kind))))
996 (when (and name (not old))
997 (setf (auxiliary-alien-type kind name env) type))
998 (frob-type type new-fields alignment bits)
999 type)))))
1000 (name
1001 (or (auxiliary-alien-type kind name env)
1002 (setf (auxiliary-alien-type kind name env)
1003 (make-alien-record-type :name name :kind kind))))
1005 (make-alien-record-type :kind kind)))))
1007 ;;; This is used by PARSE-ALIEN-TYPE to parse the fields of struct and union
1008 ;;; types. KIND is the kind we are paring the fields of, and FIELDS is the
1009 ;;; list of field specifications.
1011 ;;; Result is a list of field objects, overall alignment, and number of bits
1012 (defun parse-alien-record-fields (kind fields env)
1013 (declare (type list fields))
1014 (let ((total-bits 0)
1015 (overall-alignment 1)
1016 (parsed-fields nil))
1017 (dolist (field fields)
1018 (destructuring-bind (var type &key alignment) field
1019 (let* ((field-type (parse-alien-type type env))
1020 (bits (alien-type-bits field-type))
1021 (parsed-field
1022 (make-alien-record-field :type field-type
1023 :name var)))
1024 (unless alignment
1025 (setf alignment (alien-type-alignment field-type)))
1026 (push parsed-field parsed-fields)
1027 (when (null bits)
1028 (error "unknown size: ~S" (unparse-alien-type field-type)))
1029 (when (null alignment)
1030 (error "unknown alignment: ~S" (unparse-alien-type field-type)))
1031 (setf overall-alignment (max overall-alignment alignment))
1032 (ecase kind
1033 (:struct
1034 (let ((offset (align-offset total-bits alignment)))
1035 (setf (alien-record-field-offset parsed-field) offset)
1036 (setf total-bits (+ offset bits))))
1037 (:union
1038 (setf total-bits (max total-bits bits)))))))
1039 (values (nreverse parsed-fields)
1040 overall-alignment
1041 (align-offset total-bits overall-alignment))))
1043 (define-alien-type-method (record :unparse) (type)
1044 `(,(unparse-alien-record-kind (alien-record-type-kind type))
1045 ,(alien-record-type-name type)
1046 ,@(unless (member type *record-types-already-unparsed* :test #'eq)
1047 (push type *record-types-already-unparsed*)
1048 (mapcar #'unparse-alien-record-field
1049 (alien-record-type-fields type)))))
1051 (defun unparse-alien-record-kind (kind)
1052 (case kind
1053 (:struct 'struct)
1054 (:union 'union)
1055 (t '???)))
1057 (defun unparse-alien-record-field (field)
1058 `(,(alien-record-field-name field)
1059 ,(%unparse-alien-type (alien-record-field-type field))
1060 ,@(when (alien-record-field-bits field)
1061 (list (alien-record-field-bits field)))))
1063 ;;; Test the record fields. Keep a hashtable table of already compared
1064 ;;; types to detect cycles.
1065 (defun record-fields-match-p (field1 field2)
1066 (and (eq (alien-record-field-name field1)
1067 (alien-record-field-name field2))
1068 (eql (alien-record-field-bits field1)
1069 (alien-record-field-bits field2))
1070 (eql (alien-record-field-offset field1)
1071 (alien-record-field-offset field2))
1072 (alien-type-= (alien-record-field-type field1)
1073 (alien-record-field-type field2))))
1075 (defvar *alien-type-matches* nil
1076 "A hashtable used to detect cycles while comparing record types.")
1078 (define-alien-type-method (record :type=) (type1 type2)
1079 (and (eq (alien-record-type-name type1)
1080 (alien-record-type-name type2))
1081 (eq (alien-record-type-kind type1)
1082 (alien-record-type-kind type2))
1083 (eql (alien-type-bits type1)
1084 (alien-type-bits type2))
1085 (eql (alien-type-alignment type1)
1086 (alien-type-alignment type2))
1087 (flet ((match-fields (&optional old)
1088 (setf (gethash type1 *alien-type-matches*) (cons type2 old))
1089 (every #'record-fields-match-p
1090 (alien-record-type-fields type1)
1091 (alien-record-type-fields type2))))
1092 (if *alien-type-matches*
1093 (let ((types (gethash type1 *alien-type-matches*)))
1094 (or (memq type2 types) (match-fields types)))
1095 (let ((*alien-type-matches* (make-hash-table :test #'eq)))
1096 (match-fields))))))
1098 ;;;; the FUNCTION and VALUES alien types
1100 (define-alien-type-class (fun :include mem-block)
1101 (result-type (missing-arg) :type alien-type)
1102 (arg-types (missing-arg) :type list)
1103 (stub nil :type (or null function)))
1105 (define-alien-type-translator function (result-type &rest arg-types
1106 &environment env)
1107 (make-alien-fun-type
1108 :result-type (let ((*values-type-okay* t))
1109 (parse-alien-type result-type env))
1110 :arg-types (mapcar (lambda (arg-type) (parse-alien-type arg-type env))
1111 arg-types)))
1113 (define-alien-type-method (fun :unparse) (type)
1114 `(function ,(%unparse-alien-type (alien-fun-type-result-type type))
1115 ,@(mapcar #'%unparse-alien-type
1116 (alien-fun-type-arg-types type))))
1118 (define-alien-type-method (fun :type=) (type1 type2)
1119 (and (alien-type-= (alien-fun-type-result-type type1)
1120 (alien-fun-type-result-type type2))
1121 (= (length (alien-fun-type-arg-types type1))
1122 (length (alien-fun-type-arg-types type2)))
1123 (every #'alien-type-=
1124 (alien-fun-type-arg-types type1)
1125 (alien-fun-type-arg-types type2))))
1127 (define-alien-type-class (values)
1128 (values (missing-arg) :type list))
1130 (define-alien-type-translator values (&rest values &environment env)
1131 (unless *values-type-okay*
1132 (error "cannot use values types here"))
1133 (let ((*values-type-okay* nil))
1134 (make-alien-values-type
1135 :values (mapcar (lambda (alien-type) (parse-alien-type alien-type env))
1136 values))))
1138 (define-alien-type-method (values :unparse) (type)
1139 `(values ,@(mapcar #'%unparse-alien-type
1140 (alien-values-type-values type))))
1142 (define-alien-type-method (values :type=) (type1 type2)
1143 (and (= (length (alien-values-type-values type1))
1144 (length (alien-values-type-values type2)))
1145 (every #'alien-type-=
1146 (alien-values-type-values type1)
1147 (alien-values-type-values type2))))
1149 ;;;; a structure definition needed both in the target and in the
1150 ;;;; cross-compilation host
1152 ;;; information about local aliens. The WITH-ALIEN macro builds one of
1153 ;;; these structures and LOCAL-ALIEN and friends communicate
1154 ;;; information about how that local alien is represented.
1155 (def!struct (local-alien-info
1156 (:make-load-form-fun sb!kernel:just-dump-it-normally)
1157 (:constructor make-local-alien-info
1158 (&key type force-to-memory-p
1159 &aux (force-to-memory-p (or force-to-memory-p
1160 (alien-array-type-p type)
1161 (alien-record-type-p type))))))
1162 ;; the type of the local alien
1163 (type (missing-arg) :type alien-type)
1164 ;; Must this local alien be forced into memory? Using the ADDR macro
1165 ;; on a local alien will set this.
1166 (force-to-memory-p nil :type (member t nil)))
1167 (def!method print-object ((info local-alien-info) stream)
1168 (print-unreadable-object (info stream :type t)
1169 (format stream
1170 "~:[~;(forced to stack) ~]~S"
1171 (local-alien-info-force-to-memory-p info)
1172 (unparse-alien-type (local-alien-info-type info)))))
1174 ;;;; the ADDR macro
1176 (defmacro-mundanely addr (expr &environment env)
1177 #!+sb-doc
1178 "Return an Alien pointer to the data addressed by Expr, which must be a call
1179 to SLOT or DEREF, or a reference to an Alien variable."
1180 (let ((form (sb!xc:macroexpand expr env)))
1181 (or (typecase form
1182 (cons
1183 (case (car form)
1184 (slot
1185 (cons '%slot-addr (cdr form)))
1186 (deref
1187 (cons '%deref-addr (cdr form)))
1188 (%heap-alien
1189 (cons '%heap-alien-addr (cdr form)))
1190 (local-alien
1191 (let ((info (let ((info-arg (second form)))
1192 (and (consp info-arg)
1193 (eq (car info-arg) 'quote)
1194 (second info-arg)))))
1195 (unless (local-alien-info-p info)
1196 (error "Something is wrong, LOCAL-ALIEN-INFO not found: ~S"
1197 form))
1198 (setf (local-alien-info-force-to-memory-p info) t))
1199 (cons '%local-alien-addr (cdr form)))))
1200 (symbol
1201 (let ((kind (info :variable :kind form)))
1202 (when (eq kind :alien)
1203 `(%heap-alien-addr ',(info :variable :alien-info form))))))
1204 (error "~S is not a valid L-value." form))))
1206 (/show0 "host-alieneval.lisp end of file")