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
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)
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 (defstruct-name nil :type symbol)
38 (include nil :type (or null alien-type-class))
39 (unparse nil :type (or null function))
40 (type= nil :type (or null function))
41 (lisp-rep nil :type (or null function))
42 (alien-rep nil :type (or null function))
43 (extract-gen nil :type (or null function))
44 (deposit-gen nil :type (or null function))
45 (naturalize-gen nil :type (or null function))
46 (deport-gen nil :type (or null function))
47 (deport-alloc-gen nil :type (or null function))
48 (deport-pin-p nil :type (or null function))
50 (arg-tn nil :type (or null function))
51 (result-tn nil :type (or null function))
52 (subtypep nil :type (or null function)))
53 (def!method print-object ((type-class alien-type-class) stream)
54 (print-unreadable-object (type-class stream :type t)
55 (prin1 (alien-type-class-name type-class) stream)))
57 (defun alien-type-class-or-lose (name)
58 (or (gethash name *alien-type-classes*)
59 (error "no alien type class ~S" name)))
61 (defun create-alien-type-class-if-necessary (name defstruct-name include)
62 (let ((old (gethash name *alien-type-classes*))
63 (include (and include (alien-type-class-or-lose include))))
65 (setf (alien-type-class-include old) include)
66 (setf (gethash name *alien-type-classes*)
67 (make-alien-type-class :name name
68 :defstruct-name defstruct-name
71 (defparameter *method-slot-alist*
72 '((:unparse . alien-type-class-unparse)
73 (:type= . alien-type-class-type=)
74 (:subtypep . alien-type-class-subtypep)
75 (:lisp-rep . alien-type-class-lisp-rep)
76 (:alien-rep . alien-type-class-alien-rep)
77 (:extract-gen . alien-type-class-extract-gen)
78 (:deposit-gen . alien-type-class-deposit-gen)
79 (:naturalize-gen . alien-type-class-naturalize-gen)
80 (:deport-gen . alien-type-class-deport-gen)
81 (:deport-alloc-gen . alien-type-class-deport-alloc-gen)
82 (:deport-pin-p . alien-type-class-deport-pin-p)
84 (:arg-tn . alien-type-class-arg-tn)
85 (:result-tn . alien-type-class-result-tn)))
87 (defun method-slot (method)
88 (cdr (or (assoc method *method-slot-alist*)
89 (error "no method ~S" method))))
93 ;;; We define a keyword "BOA" constructor so that we can reference the
94 ;;; slot names in init forms.
95 (def!macro define-alien-type-class ((name &key include include-args)
97 (let ((defstruct-name (symbolicate "ALIEN-" name "-TYPE")))
98 (multiple-value-bind (include include-defstruct overrides)
101 (values nil 'alien-type nil))
105 (alien-type-class-defstruct-name
106 (alien-type-class-or-lose include))
111 (alien-type-class-defstruct-name
112 (alien-type-class-or-lose (car include)))
115 (eval-when (:compile-toplevel :load-toplevel :execute)
116 (create-alien-type-class-if-necessary ',name ',defstruct-name
117 ',(or include 'root)))
118 (def!struct (,defstruct-name
119 (:include ,include-defstruct
123 ,(symbolicate "MAKE-" defstruct-name)
124 (&key class bits alignment
125 ,@(mapcar (lambda (x)
126 (if (atom x) x (car x)))
130 &aux (alignment (or alignment (guess-alignment bits))))))
133 (def!macro define-alien-type-method ((class method) lambda-list &rest body)
134 (let ((defun-name (symbolicate class "-" method "-METHOD")))
136 (defun ,defun-name ,lambda-list
138 (setf (,(method-slot method) (alien-type-class-or-lose ',class))
141 (def!macro invoke-alien-type-method (method type &rest args)
142 (let ((slot (method-slot method)))
143 (once-only ((type type))
144 `(funcall (do ((class (alien-type-class-or-lose (alien-type-class ,type))
145 (alien-type-class-include class)))
147 (error "method ~S not defined for ~S"
148 ',method (alien-type-class ,type)))
149 (let ((fn (,slot class)))
154 ;;;; type parsing and unparsing
156 ;;; CMU CL used COMPILER-LET to bind *AUXILIARY-TYPE-DEFINITIONS*, and
157 ;;; COMPILER-LET is no longer supported by ANSI or SBCL. Instead, we
158 ;;; follow the suggestion in CLTL2 of using SYMBOL-MACROLET to achieve
159 ;;; a similar effect.
160 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
161 (defun auxiliary-type-definitions (env)
162 (multiple-value-bind (result expanded-p)
163 (%macroexpand '&auxiliary-type-definitions& env)
166 ;; This is like having the global symbol-macro definition be
167 ;; NIL, but global symbol-macros make me vaguely queasy, so
168 ;; I do it this way instead.
171 ;;; Process stuff in a new scope.
172 (def!macro with-auxiliary-alien-types (env &body body)
173 ``(symbol-macrolet ((&auxiliary-type-definitions&
174 ,(append *new-auxiliary-types*
175 (auxiliary-type-definitions ,env))))
176 ,(let ((*new-auxiliary-types* nil))
179 ;;; Parse TYPE as an alien type specifier and return the resultant
180 ;;; ALIEN-TYPE structure.
181 (defun parse-alien-type (type env)
182 (declare (type (or sb!kernel:lexenv null) env))
184 (let ((translator (info :alien-type :translator (car type))))
186 (error "unknown alien type: ~S" type))
187 (funcall translator type env))
188 (ecase (info :alien-type :kind type)
190 (let ((translator (info :alien-type :translator type)))
192 (error "no translator for primitive alien type ~S" type))
193 (funcall translator (list type) env)))
195 (or (info :alien-type :definition type)
196 (error "no definition for alien type ~S" type)))
198 (error "unknown alien type: ~S" type)))))
200 (defun auxiliary-alien-type (kind name env)
201 (declare (type (or sb!kernel:lexenv null) env))
202 (flet ((aux-defn-matches (x)
203 (and (eq (first x) kind) (eq (second x) name))))
204 (let ((in-auxiliaries
205 (or (find-if #'aux-defn-matches *new-auxiliary-types*)
206 (find-if #'aux-defn-matches (auxiliary-type-definitions env)))))
208 (values (third in-auxiliaries) t)
211 (info :alien-type :struct name))
213 (info :alien-type :union name))
215 (info :alien-type :enum name)))))))
217 (defun (setf auxiliary-alien-type) (new-value kind name env)
218 (declare (type (or sb!kernel:lexenv null) env))
219 (flet ((aux-defn-matches (x)
220 (and (eq (first x) kind) (eq (second x) name))))
221 (when (find-if #'aux-defn-matches *new-auxiliary-types*)
222 (error "attempt to multiply define ~A ~S" kind name))
223 (when (find-if #'aux-defn-matches (auxiliary-type-definitions env))
224 (error "attempt to shadow definition of ~A ~S" kind name)))
225 (push (list kind name new-value) *new-auxiliary-types*)
228 (defun verify-local-auxiliaries-okay ()
229 (dolist (info *new-auxiliary-types*)
230 (destructuring-bind (kind name defn) info
231 (declare (ignore defn))
234 (info :alien-type :struct name))
236 (info :alien-type :union name))
238 (info :alien-type :enum name)))
239 (error "attempt to shadow definition of ~A ~S" kind name)))))
241 (defun unparse-alien-type (type)
243 "Convert the alien-type structure TYPE back into a list specification of
245 (declare (type alien-type type))
246 (let ((*record-types-already-unparsed* nil))
247 (%unparse-alien-type type)))
249 ;;; Does all the work of UNPARSE-ALIEN-TYPE. It's separate because we
250 ;;; need to recurse inside the binding of
251 ;;; *RECORD-TYPES-ALREADY-UNPARSED*.
252 (defun %unparse-alien-type (type)
253 (invoke-alien-type-method :unparse type))
255 ;;;; alien type defining stuff
257 (def!macro define-alien-type-translator (name lambda-list &body body)
258 (with-unique-names (whole env)
259 (let ((defun-name (symbolicate "ALIEN-" name "-TYPE-TRANSLATOR")))
260 (multiple-value-bind (body decls docs)
261 (sb!kernel:parse-defmacro lambda-list whole body name
262 'define-alien-type-translator
264 `(eval-when (:compile-toplevel :load-toplevel :execute)
265 (defun ,defun-name (,whole ,env)
266 (declare (ignorable ,env))
270 (%define-alien-type-translator ',name #',defun-name ,docs))))))
272 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
273 (defun %define-alien-type-translator (name translator docs)
274 (declare (ignore docs))
275 (setf (info :alien-type :kind name) :primitive)
276 (setf (info :alien-type :translator name) translator)
277 (clear-info :alien-type :definition name)
279 (setf (fdocumentation name 'alien-type) docs)
282 (def!macro define-alien-type (name type &environment env)
284 "Define the alien type NAME to be equivalent to TYPE. Name may be NIL for
285 STRUCT and UNION types, in which case the name is taken from the type
287 (with-auxiliary-alien-types env
288 (let ((alien-type (parse-alien-type type env)))
289 `(eval-when (:compile-toplevel :load-toplevel :execute)
290 ,@(when *new-auxiliary-types*
291 `((%def-auxiliary-alien-types ',*new-auxiliary-types*)))
293 `((%define-alien-type ',name ',alien-type)))))))
295 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
296 (defun %def-auxiliary-alien-types (types)
298 ;; Clear up the type we're about to define from the toplevel
299 ;; *new-auxiliary-types* (local scopes take care of themselves).
300 ;; Unless this is done we never actually get back the full type
301 ;; from INFO, since the *new-auxiliary-types* have precendence.
302 (setf *new-auxiliary-types*
303 (remove info *new-auxiliary-types*
305 (and (eq (first a) (first b))
306 (eq (second a) (second b))))))
307 (destructuring-bind (kind name defn) info
308 (macrolet ((frob (kind)
309 `(let ((old (info :alien-type ,kind name)))
310 (unless (or (null old) (alien-type-= old defn))
312 "redefining ~A ~S to be:~% ~S,~%was:~% ~S"
314 (setf (info :alien-type ,kind name) defn))))
316 (:struct (frob :struct))
317 (:union (frob :union))
318 (:enum (frob :enum)))))))
319 (defun %define-alien-type (name new)
320 (ecase (info :alien-type :kind name)
322 (error "~S is a built-in alien type." name))
324 (let ((old (info :alien-type :definition name)))
325 (unless (or (null old) (alien-type-= new old))
326 (warn "redefining ~S to be:~% ~S,~%was~% ~S"
328 (unparse-alien-type new)
329 (unparse-alien-type old)))))
331 (setf (info :alien-type :definition name) new)
332 (setf (info :alien-type :kind name) :defined)
335 ;;;; the root alien type
337 (eval-when (:compile-toplevel :load-toplevel :execute)
338 (create-alien-type-class-if-necessary 'root 'alien-type nil))
340 (def!struct (alien-type
341 (:make-load-form-fun sb!kernel:just-dump-it-normally)
342 (:constructor make-alien-type (&key class bits alignment
343 &aux (alignment (or alignment (guess-alignment bits))))))
344 (class 'root :type symbol)
345 (bits nil :type (or null unsigned-byte))
346 (alignment nil :type (or null unsigned-byte)))
347 (def!method print-object ((type alien-type) stream)
348 (print-unreadable-object (type stream :type t)
349 (prin1 (unparse-alien-type type) stream)))
353 (define-alien-type-class (system-area-pointer))
355 (define-alien-type-translator system-area-pointer ()
356 (make-alien-system-area-pointer-type
357 :bits #!-alpha sb!vm:n-word-bits #!+alpha 64))
359 (define-alien-type-method (system-area-pointer :unparse) (type)
360 (declare (ignore type))
361 'system-area-pointer)
363 (define-alien-type-method (system-area-pointer :lisp-rep) (type)
364 (declare (ignore type))
365 'system-area-pointer)
367 (define-alien-type-method (system-area-pointer :alien-rep) (type context)
368 (declare (ignore type context))
369 'system-area-pointer)
371 (define-alien-type-method (system-area-pointer :naturalize-gen) (type alien)
372 (declare (ignore type))
375 (define-alien-type-method (system-area-pointer :deport-gen) (type object)
376 (declare (ignore type))
377 (/noshow "doing alien type method SYSTEM-AREA-POINTER :DEPORT-GEN" object)
380 (define-alien-type-method (system-area-pointer :extract-gen) (type sap offset)
381 (declare (ignore type))
382 `(sap-ref-sap ,sap (/ ,offset sb!vm:n-byte-bits)))
384 ;;;; the ALIEN-VALUE type
386 (define-alien-type-class (alien-value :include system-area-pointer))
388 (define-alien-type-method (alien-value :lisp-rep) (type)
389 (declare (ignore type))
392 (define-alien-type-method (alien-value :naturalize-gen) (type alien)
393 `(%sap-alien ,alien ',type))
395 (define-alien-type-method (alien-value :deport-gen) (type value)
396 (declare (ignore type))
397 (/noshow "doing alien type method ALIEN-VALUE :DEPORT-GEN" value)
400 ;;; HEAP-ALIEN-INFO -- defstruct.
402 ;;; Information describing a heap-allocated alien.
403 (def!struct (heap-alien-info
404 (:make-load-form-fun sb!kernel:just-dump-it-normally))
405 ;; The type of this alien.
406 (type (missing-arg) :type alien-type)
407 ;; The form to evaluate to produce the SAP pointing to where in the heap
409 (sap-form (missing-arg)))
410 (def!method print-object ((info heap-alien-info) stream)
411 (print-unreadable-object (info stream :type t)
412 (funcall (formatter "~S ~S")
414 (heap-alien-info-sap-form info)
415 (unparse-alien-type (heap-alien-info-type info)))))
417 ;;;; Interfaces to the different methods
419 (defun alien-type-= (type1 type2)
421 "Return T iff TYPE1 and TYPE2 describe equivalent alien types."
423 (and (eq (alien-type-class type1)
424 (alien-type-class type2))
425 (invoke-alien-type-method :type= type1 type2))))
427 (defun alien-subtype-p (type1 type2)
429 "Return T iff the alien type TYPE1 is a subtype of TYPE2. Currently, the
430 only supported subtype relationships are is that any pointer type is a
431 subtype of (* t), and any array type first dimension will match
432 (array <eltype> nil ...). Otherwise, the two types have to be
435 (invoke-alien-type-method :subtypep type1 type2)))
437 (defun compute-naturalize-lambda (type)
438 `(lambda (alien ignore)
439 (declare (ignore ignore))
440 ,(invoke-alien-type-method :naturalize-gen type 'alien)))
442 (defun compute-deport-lambda (type)
443 (declare (type alien-type type))
444 (/noshow "entering COMPUTE-DEPORT-LAMBDA" type)
445 (multiple-value-bind (form value-type)
446 (invoke-alien-type-method :deport-gen type 'value)
447 `(lambda (value ignore)
448 (declare (type ,(or value-type
449 (compute-lisp-rep-type type)
455 (defun compute-deport-alloc-lambda (type)
456 `(lambda (value ignore)
457 (declare (ignore ignore))
458 ,(invoke-alien-type-method :deport-alloc-gen type 'value)))
460 (defun compute-extract-lambda (type)
461 `(lambda (sap offset ignore)
462 (declare (type system-area-pointer sap)
463 (type unsigned-byte offset)
465 (naturalize ,(invoke-alien-type-method :extract-gen type 'sap 'offset)
468 (def!macro maybe-with-pinned-objects (variables types &body body)
469 (declare (ignorable variables types))
471 ;; Only pin things on x86/x86-64, since on non-conservative
472 ;; gcs it'd imply disabling the GC. Which is something we
473 ;; don't want to do every time we're calling to C.
475 (loop for variable in variables
477 when (invoke-alien-type-method :deport-pin-p type)
480 `(with-pinned-objects ,pin-variables
485 (defun compute-deposit-lambda (type)
486 (declare (type alien-type type))
487 `(lambda (sap offset ignore value)
488 (declare (type system-area-pointer sap)
489 (type unsigned-byte offset)
491 (let ((alloc-tmp (deport-alloc value ',type)))
492 (maybe-with-pinned-objects (alloc-tmp) (,type)
493 (let ((value (deport alloc-tmp ',type)))
494 ,(invoke-alien-type-method :deposit-gen type 'sap 'offset 'value)
495 ;; Note: the reason we don't just return the pre-deported value
496 ;; is because that would inhibit any (deport (naturalize ...))
497 ;; optimizations that might have otherwise happen. Re-naturalizing
498 ;; the value might cause extra consing, but is flushable, so probably
499 ;; results in better code.
500 (naturalize value ',type))))))
502 (defun compute-lisp-rep-type (type)
503 (invoke-alien-type-method :lisp-rep type))
505 ;;; CONTEXT is either :NORMAL (the default) or :RESULT (alien function
506 ;;; return values). See the :ALIEN-REP method for INTEGER for
508 (defun compute-alien-rep-type (type &optional (context :normal))
509 (invoke-alien-type-method :alien-rep type context))
513 (define-alien-type-method (root :unparse) (type)
514 `(<unknown-alien-type> ,(type-of type)))
516 (define-alien-type-method (root :type=) (type1 type2)
517 (declare (ignore type1 type2))
520 (define-alien-type-method (root :subtypep) (type1 type2)
521 (alien-type-= type1 type2))
523 (define-alien-type-method (root :lisp-rep) (type)
524 (declare (ignore type))
527 (define-alien-type-method (root :alien-rep) (type context)
528 (declare (ignore type context))
531 (define-alien-type-method (root :naturalize-gen) (type alien)
532 (declare (ignore alien))
533 (error "cannot represent ~S typed aliens" type))
535 (define-alien-type-method (root :deport-gen) (type object)
536 (declare (ignore object))
537 (error "cannot represent ~S typed aliens" type))
539 (define-alien-type-method (root :deport-alloc-gen) (type object)
540 (declare (ignore type))
543 (define-alien-type-method (root :deport-pin-p) (type)
544 (declare (ignore type))
545 ;; Override this method to return T for classes which take a SAP to a
546 ;; GCable lisp object when deporting.
549 (define-alien-type-method (root :extract-gen) (type sap offset)
550 (declare (ignore sap offset))
551 (error "cannot represent ~S typed aliens" type))
553 (define-alien-type-method (root :deposit-gen) (type sap offset value)
554 `(setf ,(invoke-alien-type-method :extract-gen type sap offset) ,value))
556 (define-alien-type-method (root :arg-tn) (type state)
557 (declare (ignore state))
558 (error "Aliens of type ~S cannot be passed as arguments to CALL-OUT."
559 (unparse-alien-type type)))
561 (define-alien-type-method (root :result-tn) (type state)
562 (declare (ignore state))
563 (error "Aliens of type ~S cannot be returned from CALL-OUT."
564 (unparse-alien-type type)))
566 ;;;; the INTEGER type
568 (define-alien-type-class (integer)
569 (signed t :type (member t nil)))
571 (define-alien-type-translator signed (&optional (bits sb!vm:n-word-bits))
572 (make-alien-integer-type :bits bits))
574 (define-alien-type-translator integer (&optional (bits sb!vm:n-word-bits))
575 (make-alien-integer-type :bits bits))
577 (define-alien-type-translator unsigned (&optional (bits sb!vm:n-word-bits))
578 (make-alien-integer-type :bits bits :signed nil))
580 (define-alien-type-method (integer :unparse) (type)
581 (list (if (alien-integer-type-signed type) 'signed 'unsigned)
582 (alien-integer-type-bits type)))
584 (define-alien-type-method (integer :type=) (type1 type2)
585 (and (eq (alien-integer-type-signed type1)
586 (alien-integer-type-signed type2))
587 (= (alien-integer-type-bits type1)
588 (alien-integer-type-bits type2))))
590 (define-alien-type-method (integer :lisp-rep) (type)
591 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
592 (alien-integer-type-bits type)))
594 (define-alien-type-method (integer :alien-rep) (type context)
595 ;; When returning integer values that are narrower than a machine
596 ;; register from a function, some platforms leave the higher bits of
597 ;; the register uninitialized. On those platforms, we use an
598 ;; alien-rep of the full register width when checking for purposes
599 ;; of return values and override the naturalize method to perform
600 ;; the sign extension (in compiler/target/c-call.lisp).
602 ((:normal #!-(or x86 x86-64) :result)
603 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
604 (alien-integer-type-bits type)))
607 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
608 sb!vm:n-word-bits))))
610 ;;; As per the comment in the :ALIEN-REP method above, this is defined
611 ;;; elsewhere for x86oids.
613 (define-alien-type-method (integer :naturalize-gen) (type alien)
614 (declare (ignore type))
617 (define-alien-type-method (integer :deport-gen) (type value)
618 (declare (ignore type))
621 (define-alien-type-method (integer :extract-gen) (type sap offset)
622 (declare (type alien-integer-type type))
624 (if (alien-integer-type-signed type)
625 (case (alien-integer-type-bits type)
626 (8 'signed-sap-ref-8)
627 (16 'signed-sap-ref-16)
628 (32 'signed-sap-ref-32)
629 (64 'signed-sap-ref-64))
630 (case (alien-integer-type-bits type)
636 `(,ref-fun ,sap (/ ,offset sb!vm:n-byte-bits))
637 (error "cannot extract ~W-bit integers"
638 (alien-integer-type-bits type)))))
640 ;;;; the BOOLEAN type
642 (define-alien-type-class (boolean :include integer :include-args (signed)))
644 ;;; FIXME: Check to make sure that we aren't attaching user-readable
645 ;;; stuff to CL:BOOLEAN in any way which impairs ANSI compliance.
646 (define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
647 (make-alien-boolean-type :bits bits :signed nil))
649 (define-alien-type-method (boolean :unparse) (type)
650 `(boolean ,(alien-boolean-type-bits type)))
652 (define-alien-type-method (boolean :lisp-rep) (type)
653 (declare (ignore type))
656 (define-alien-type-method (boolean :naturalize-gen) (type alien)
657 (declare (ignore type))
658 `(not (zerop ,alien)))
660 (define-alien-type-method (boolean :deport-gen) (type value)
661 (declare (ignore type))
666 (define-alien-type-class (enum :include (integer (bits 32))
667 :include-args (signed))
668 name ; name of this enum (if any)
669 from ; alist from symbols to integers
670 to ; alist or vector from integers to symbols
671 kind ; kind of from mapping, :VECTOR or :ALIST
672 offset) ; offset to add to value for :VECTOR from mapping
674 (define-alien-type-translator enum (&whole
679 (let ((result (parse-enum name mappings)))
681 (multiple-value-bind (old old-p)
682 (auxiliary-alien-type :enum name env)
684 (unless (alien-type-= result old)
685 (cerror "Continue, clobbering the old definition"
686 "Incompatible alien enum type definition: ~S" name)
687 (setf (alien-type-from old) (alien-type-from result)
688 (alien-type-to old) (alien-type-to result)
689 (alien-type-kind old) (alien-type-kind result)
690 (alien-type-offset old) (alien-type-offset result)
691 (alien-type-signed old) (alien-type-signed result)))
694 (setf (auxiliary-alien-type :enum name env) result))))
697 (multiple-value-bind (result found)
698 (auxiliary-alien-type :enum name env)
700 (error "unknown enum type: ~S" name))
703 (error "empty enum type: ~S" type))))
705 (defun parse-enum (name elements)
706 (when (null elements)
707 (error "An enumeration must contain at least one element."))
712 (declare (list from-alist))
713 (dolist (el elements)
714 (multiple-value-bind (sym val)
716 (values (first el) (second el))
717 (values el (1+ prev)))
719 (unless (symbolp sym)
720 (error "The enumeration element ~S is not a symbol." sym))
721 (unless (integerp val)
722 (error "The element value ~S is not an integer." val))
723 (unless (and max (> max val)) (setq max val))
724 (unless (and min (< min val)) (setq min val))
725 (when (rassoc val from-alist)
726 (style-warn "The element value ~S is used more than once." val))
727 (when (assoc sym from-alist :test #'eq)
728 (error "The enumeration element ~S is used more than once." sym))
729 (push (cons sym val) from-alist)))
730 (let* ((signed (minusp min))
732 (1+ (max (integer-length min)
733 (integer-length max)))
734 (integer-length max))))
735 (when (> min-bits 32)
736 (error "can't represent enums needing more than 32 bits"))
737 (setf from-alist (sort from-alist #'< :key #'cdr))
739 ;; If range is at least 20% dense, use vector mapping. Crossover
740 ;; point solely on basis of space would be 25%. Vector mapping
741 ;; is always faster, so give the benefit of the doubt.
742 ((< 0.2 (/ (float (length from-alist)) (float (1+ (- max min)))))
743 ;; If offset is small and ignorable, ignore it to save time.
744 (when (< 0 min 10) (setq min 0))
745 (let ((to (make-array (1+ (- max min)))))
746 (dolist (el from-alist)
747 (setf (svref to (- (cdr el) min)) (car el)))
748 (make-alien-enum-type :name name :signed signed
749 :from from-alist :to to :kind
750 :vector :offset (- min))))
752 (make-alien-enum-type :name name :signed signed
754 :to (mapcar (lambda (x) (cons (cdr x) (car x)))
758 (define-alien-type-method (enum :unparse) (type)
759 `(enum ,(alien-enum-type-name type)
761 (mapcar (lambda (mapping)
762 (let ((sym (car mapping))
763 (value (cdr mapping)))
765 (if (= (1+ prev) value)
769 (alien-enum-type-from type)))))
771 (define-alien-type-method (enum :type=) (type1 type2)
772 (and (eq (alien-enum-type-name type1)
773 (alien-enum-type-name type2))
774 (equal (alien-enum-type-from type1)
775 (alien-enum-type-from type2))))
777 (define-alien-type-method (enum :lisp-rep) (type)
778 `(member ,@(mapcar #'car (alien-enum-type-from type))))
780 (define-alien-type-method (enum :naturalize-gen) (type alien)
781 (ecase (alien-enum-type-kind type)
783 `(svref ',(alien-enum-type-to type)
784 (+ ,alien ,(alien-enum-type-offset type))))
787 ,@(mapcar (lambda (mapping)
788 `(,(car mapping) ',(cdr mapping)))
789 (alien-enum-type-to type))))))
791 (define-alien-type-method (enum :deport-gen) (type value)
793 ,@(mapcar (lambda (mapping)
794 `(,(car mapping) ,(cdr mapping)))
795 (alien-enum-type-from type))))
799 (define-alien-type-class (float)
800 (type (missing-arg) :type symbol))
802 (define-alien-type-method (float :unparse) (type)
803 (alien-float-type-type type))
805 (define-alien-type-method (float :lisp-rep) (type)
806 (alien-float-type-type type))
808 (define-alien-type-method (float :alien-rep) (type context)
809 (declare (ignore context))
810 (alien-float-type-type type))
812 (define-alien-type-method (float :naturalize-gen) (type alien)
813 (declare (ignore type))
816 (define-alien-type-method (float :deport-gen) (type value)
817 (declare (ignore type))
820 (define-alien-type-class (single-float :include (float (bits 32))
821 :include-args (type)))
823 (define-alien-type-translator single-float ()
824 (make-alien-single-float-type :type 'single-float))
826 (define-alien-type-method (single-float :extract-gen) (type sap offset)
827 (declare (ignore type))
828 `(sap-ref-single ,sap (/ ,offset sb!vm:n-byte-bits)))
830 (define-alien-type-class (double-float :include (float (bits 64))
831 :include-args (type)))
833 (define-alien-type-translator double-float ()
834 (make-alien-double-float-type :type 'double-float))
836 (define-alien-type-method (double-float :extract-gen) (type sap offset)
837 (declare (ignore type))
838 `(sap-ref-double ,sap (/ ,offset sb!vm:n-byte-bits)))
841 ;;;; the POINTER type
843 (define-alien-type-class (pointer :include (alien-value (bits
847 (to nil :type (or alien-type null)))
849 (define-alien-type-translator * (to &environment env)
850 (make-alien-pointer-type :to (if (eq to t) nil (parse-alien-type to env))))
852 (define-alien-type-method (pointer :unparse) (type)
853 (let ((to (alien-pointer-type-to type)))
855 (%unparse-alien-type to)
858 (define-alien-type-method (pointer :type=) (type1 type2)
859 (let ((to1 (alien-pointer-type-to type1))
860 (to2 (alien-pointer-type-to type2)))
863 (alien-type-= to1 to2)
867 (define-alien-type-method (pointer :subtypep) (type1 type2)
868 (and (alien-pointer-type-p type2)
869 (let ((to1 (alien-pointer-type-to type1))
870 (to2 (alien-pointer-type-to type2)))
873 (alien-subtype-p to1 to2)
877 (define-alien-type-method (pointer :deport-gen) (type value)
878 (/noshow "doing alien type method POINTER :DEPORT-GEN" type value)
880 ;; FIXME: old version, highlighted a bug in xc optimization
888 ;; new version, works around bug in xc optimization
897 `(or null system-area-pointer (alien ,type))))
899 ;;;; the MEM-BLOCK type
901 (define-alien-type-class (mem-block :include alien-value))
903 (define-alien-type-method (mem-block :extract-gen) (type sap offset)
904 (declare (ignore type))
905 `(sap+ ,sap (truncate ,offset sb!vm:n-byte-bits)))
907 (define-alien-type-method (mem-block :deposit-gen) (type sap offset value)
908 (let ((bits (alien-mem-block-type-bits type)))
910 (error "can't deposit aliens of type ~S (unknown size)" type))
911 `(sb!kernel:system-area-ub8-copy ,value 0 ,sap
912 (truncate ,offset sb!vm:n-byte-bits)
913 ',(truncate bits sb!vm:n-byte-bits))))
917 (define-alien-type-class (array :include mem-block)
918 (element-type (missing-arg) :type alien-type)
919 (dimensions (missing-arg) :type list))
921 (define-alien-type-translator array (ele-type &rest dims &environment env)
924 (unless (typep (first dims) '(or index null))
925 (error "The first dimension is not a non-negative fixnum or NIL: ~S"
927 (let ((loser (find-if-not (lambda (x) (typep x 'index))
930 (error "A dimension is not a non-negative fixnum: ~S" loser))))
932 (let ((parsed-ele-type (parse-alien-type ele-type env)))
933 (make-alien-array-type
934 :element-type parsed-ele-type
936 :alignment (alien-type-alignment parsed-ele-type)
937 :bits (if (and (alien-type-bits parsed-ele-type)
938 (every #'integerp dims))
939 (* (align-offset (alien-type-bits parsed-ele-type)
940 (alien-type-alignment parsed-ele-type))
941 (reduce #'* dims))))))
943 (define-alien-type-method (array :unparse) (type)
944 `(array ,(%unparse-alien-type (alien-array-type-element-type type))
945 ,@(alien-array-type-dimensions type)))
947 (define-alien-type-method (array :type=) (type1 type2)
948 (and (equal (alien-array-type-dimensions type1)
949 (alien-array-type-dimensions type2))
950 (alien-type-= (alien-array-type-element-type type1)
951 (alien-array-type-element-type type2))))
953 (define-alien-type-method (array :subtypep) (type1 type2)
954 (and (alien-array-type-p type2)
955 (let ((dim1 (alien-array-type-dimensions type1))
956 (dim2 (alien-array-type-dimensions type2)))
957 (and (= (length dim1) (length dim2))
960 (equal (cdr dim1) (cdr dim2)))
962 (alien-subtype-p (alien-array-type-element-type type1)
963 (alien-array-type-element-type type2))))))
967 (def!struct (alien-record-field
968 (:make-load-form-fun sb!kernel:just-dump-it-normally))
969 (name (missing-arg) :type symbol)
970 (type (missing-arg) :type alien-type)
971 (bits nil :type (or unsigned-byte null))
972 (offset 0 :type unsigned-byte))
973 (def!method print-object ((field alien-record-field) stream)
974 (print-unreadable-object (field stream :type t)
977 (alien-record-field-type field)
978 (alien-record-field-name field)
979 (alien-record-field-bits field))))
981 (define-alien-type-class (record :include mem-block)
982 (kind :struct :type (member :struct :union))
983 (name nil :type (or symbol null))
984 (fields nil :type list))
986 (define-alien-type-translator struct (name &rest fields &environment env)
987 (parse-alien-record-type :struct name fields env))
989 (define-alien-type-translator union (name &rest fields &environment env)
990 (parse-alien-record-type :union name fields env))
992 ;;; FIXME: This is really pretty horrible: we avoid creating new
993 ;;; ALIEN-RECORD-TYPE objects when a live one is flitting around the
994 ;;; system already. This way forwrd-references sans fields get get
995 ;;; "updated" for free to contain the field info. Maybe rename
996 ;;; MAKE-ALIEN-RECORD-TYPE to %MAKE-ALIEN-RECORD-TYPE and use
997 ;;; ENSURE-ALIEN-RECORD-TYPE instead. --NS 20040729
998 (defun parse-alien-record-type (kind name fields env)
999 (declare (type (or sb!kernel:lexenv null) env))
1000 (flet ((frob-type (type new-fields alignment bits)
1001 (setf (alien-record-type-fields type) new-fields
1002 (alien-record-type-alignment type) alignment
1003 (alien-record-type-bits type) bits)))
1005 (multiple-value-bind (new-fields alignment bits)
1006 (parse-alien-record-fields kind fields env)
1007 (let* ((old (and name (auxiliary-alien-type kind name env)))
1008 (old-fields (and old (alien-record-type-fields old))))
1009 (when (and old-fields
1010 (notevery #'record-fields-match-p old-fields new-fields))
1011 (cerror "Continue, clobbering the old definition."
1012 "Incompatible alien record type definition~%Old: ~S~%New: ~S"
1013 (unparse-alien-type old)
1014 `(,(unparse-alien-record-kind kind)
1016 ,@(mapcar #'unparse-alien-record-field new-fields)))
1017 (frob-type old new-fields alignment bits))
1020 (let ((type (or old (make-alien-record-type :name name :kind kind))))
1021 (when (and name (not old))
1022 (setf (auxiliary-alien-type kind name env) type))
1023 (frob-type type new-fields alignment bits)
1026 (or (auxiliary-alien-type kind name env)
1027 (setf (auxiliary-alien-type kind name env)
1028 (make-alien-record-type :name name :kind kind))))
1030 (make-alien-record-type :kind kind)))))
1032 ;;; This is used by PARSE-ALIEN-TYPE to parse the fields of struct and union
1033 ;;; types. KIND is the kind we are paring the fields of, and FIELDS is the
1034 ;;; list of field specifications.
1036 ;;; Result is a list of field objects, overall alignment, and number of bits
1037 (defun parse-alien-record-fields (kind fields env)
1038 (declare (type list fields))
1039 (let ((total-bits 0)
1040 (overall-alignment 1)
1041 (parsed-fields nil))
1042 (dolist (field fields)
1043 (destructuring-bind (var type &key alignment) field
1044 (let* ((field-type (parse-alien-type type env))
1045 (bits (alien-type-bits field-type))
1047 (make-alien-record-field :type field-type
1050 (setf alignment (alien-type-alignment field-type)))
1051 (push parsed-field parsed-fields)
1053 (error "unknown size: ~S" (unparse-alien-type field-type)))
1054 (when (null alignment)
1055 (error "unknown alignment: ~S" (unparse-alien-type field-type)))
1056 (setf overall-alignment (max overall-alignment alignment))
1059 (let ((offset (align-offset total-bits alignment)))
1060 (setf (alien-record-field-offset parsed-field) offset)
1061 (setf total-bits (+ offset bits))))
1063 (setf total-bits (max total-bits bits)))))))
1064 (values (nreverse parsed-fields)
1066 (align-offset total-bits overall-alignment))))
1068 (define-alien-type-method (record :unparse) (type)
1069 `(,(unparse-alien-record-kind (alien-record-type-kind type))
1070 ,(alien-record-type-name type)
1071 ,@(unless (member type *record-types-already-unparsed* :test #'eq)
1072 (push type *record-types-already-unparsed*)
1073 (mapcar #'unparse-alien-record-field
1074 (alien-record-type-fields type)))))
1076 (defun unparse-alien-record-kind (kind)
1082 (defun unparse-alien-record-field (field)
1083 `(,(alien-record-field-name field)
1084 ,(%unparse-alien-type (alien-record-field-type field))
1085 ,@(when (alien-record-field-bits field)
1086 (list (alien-record-field-bits field)))))
1088 ;;; Test the record fields. Keep a hashtable table of already compared
1089 ;;; types to detect cycles.
1090 (defun record-fields-match-p (field1 field2)
1091 (and (eq (alien-record-field-name field1)
1092 (alien-record-field-name field2))
1093 (eql (alien-record-field-bits field1)
1094 (alien-record-field-bits field2))
1095 (eql (alien-record-field-offset field1)
1096 (alien-record-field-offset field2))
1097 (alien-type-= (alien-record-field-type field1)
1098 (alien-record-field-type field2))))
1100 (defvar *alien-type-matches* nil
1101 "A hashtable used to detect cycles while comparing record types.")
1103 (define-alien-type-method (record :type=) (type1 type2)
1104 (and (eq (alien-record-type-name type1)
1105 (alien-record-type-name type2))
1106 (eq (alien-record-type-kind type1)
1107 (alien-record-type-kind type2))
1108 (eql (alien-type-bits type1)
1109 (alien-type-bits type2))
1110 (eql (alien-type-alignment type1)
1111 (alien-type-alignment type2))
1112 (flet ((match-fields (&optional old)
1113 (setf (gethash type1 *alien-type-matches*) (cons type2 old))
1114 (every #'record-fields-match-p
1115 (alien-record-type-fields type1)
1116 (alien-record-type-fields type2))))
1117 (if *alien-type-matches*
1118 (let ((types (gethash type1 *alien-type-matches*)))
1119 (or (memq type2 types) (match-fields types)))
1120 (let ((*alien-type-matches* (make-hash-table :test #'eq)))
1123 ;;;; the FUNCTION and VALUES alien types
1125 (define-alien-type-class (fun :include mem-block)
1126 (result-type (missing-arg) :type alien-type)
1127 (arg-types (missing-arg) :type list)
1128 (stub nil :type (or null function)))
1130 (define-alien-type-translator function (result-type &rest arg-types
1132 (make-alien-fun-type
1133 :result-type (let ((*values-type-okay* t))
1134 (parse-alien-type result-type env))
1135 :arg-types (mapcar (lambda (arg-type) (parse-alien-type arg-type env))
1138 (define-alien-type-method (fun :unparse) (type)
1139 `(function ,(%unparse-alien-type (alien-fun-type-result-type type))
1140 ,@(mapcar #'%unparse-alien-type
1141 (alien-fun-type-arg-types type))))
1143 (define-alien-type-method (fun :type=) (type1 type2)
1144 (and (alien-type-= (alien-fun-type-result-type type1)
1145 (alien-fun-type-result-type type2))
1146 (= (length (alien-fun-type-arg-types type1))
1147 (length (alien-fun-type-arg-types type2)))
1148 (every #'alien-type-=
1149 (alien-fun-type-arg-types type1)
1150 (alien-fun-type-arg-types type2))))
1152 (define-alien-type-class (values)
1153 (values (missing-arg) :type list))
1155 (define-alien-type-translator values (&rest values &environment env)
1156 (unless *values-type-okay*
1157 (error "cannot use values types here"))
1158 (let ((*values-type-okay* nil))
1159 (make-alien-values-type
1160 :values (mapcar (lambda (alien-type) (parse-alien-type alien-type env))
1163 (define-alien-type-method (values :unparse) (type)
1164 `(values ,@(mapcar #'%unparse-alien-type
1165 (alien-values-type-values type))))
1167 (define-alien-type-method (values :type=) (type1 type2)
1168 (and (= (length (alien-values-type-values type1))
1169 (length (alien-values-type-values type2)))
1170 (every #'alien-type-=
1171 (alien-values-type-values type1)
1172 (alien-values-type-values type2))))
1174 ;;;; a structure definition needed both in the target and in the
1175 ;;;; cross-compilation host
1177 ;;; information about local aliens. The WITH-ALIEN macro builds one of
1178 ;;; these structures and LOCAL-ALIEN and friends communicate
1179 ;;; information about how that local alien is represented.
1180 (def!struct (local-alien-info
1181 (:make-load-form-fun sb!kernel:just-dump-it-normally)
1182 (:constructor make-local-alien-info
1183 (&key type force-to-memory-p
1184 &aux (force-to-memory-p (or force-to-memory-p
1185 (alien-array-type-p type)
1186 (alien-record-type-p type))))))
1187 ;; the type of the local alien
1188 (type (missing-arg) :type alien-type)
1189 ;; Must this local alien be forced into memory? Using the ADDR macro
1190 ;; on a local alien will set this.
1191 (force-to-memory-p nil :type (member t nil)))
1192 (def!method print-object ((info local-alien-info) stream)
1193 (print-unreadable-object (info stream :type t)
1195 "~:[~;(forced to stack) ~]~S"
1196 (local-alien-info-force-to-memory-p info)
1197 (unparse-alien-type (local-alien-info-type info)))))
1201 (defmacro-mundanely addr (expr &environment env)
1203 "Return an Alien pointer to the data addressed by Expr, which must be a call
1204 to SLOT or DEREF, or a reference to an Alien variable."
1205 (let ((form (%macroexpand expr env)))
1210 (cons '%slot-addr (cdr form)))
1212 (cons '%deref-addr (cdr form)))
1214 (cons '%heap-alien-addr (cdr form)))
1216 (let ((info (let ((info-arg (second form)))
1217 (and (consp info-arg)
1218 (eq (car info-arg) 'quote)
1219 (second info-arg)))))
1220 (unless (local-alien-info-p info)
1221 (error "Something is wrong, LOCAL-ALIEN-INFO not found: ~S"
1223 (setf (local-alien-info-force-to-memory-p info) t))
1224 (cons '%local-alien-addr (cdr form)))))
1226 (let ((kind (info :variable :kind form)))
1227 (when (eq kind :alien)
1228 `(%heap-alien-addr ',(info :variable :alien-info form))))))
1229 (error "~S is not a valid L-value." form))))
1231 (/show0 "host-alieneval.lisp end of file")