1.0.27.46: Fix build on systems with "src" in the path.
[sbcl/tcr.git] / src / code / host-alieneval.lisp
blobdb3baad686d129829ee80ee4c49cbec0cb71fed3
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 (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))
49 ;; Cast?
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))))
64 (if old
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
69 :include include)))))
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)
83 ;; cast?
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))))
91 ) ; EVAL-WHEN
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)
96 &rest slots)
97 (let ((defstruct-name (symbolicate "ALIEN-" name "-TYPE")))
98 (multiple-value-bind (include include-defstruct overrides)
99 (etypecase include
100 (null
101 (values nil 'alien-type nil))
102 (symbol
103 (values
104 include
105 (alien-type-class-defstruct-name
106 (alien-type-class-or-lose include))
107 nil))
108 (list
109 (values
110 (car include)
111 (alien-type-class-defstruct-name
112 (alien-type-class-or-lose (car include)))
113 (cdr include))))
114 `(progn
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
120 (class ',name)
121 ,@overrides)
122 (:constructor
123 ,(symbolicate "MAKE-" defstruct-name)
124 (&key class bits alignment
125 ,@(mapcar (lambda (x)
126 (if (atom x) x (car x)))
127 slots)
128 ,@include-args
129 ;; KLUDGE
130 &aux (alignment (or alignment (guess-alignment bits))))))
131 ,@slots)))))
133 (def!macro define-alien-type-method ((class method) lambda-list &rest body)
134 (let ((defun-name (symbolicate class "-" method "-METHOD")))
135 `(progn
136 (defun ,defun-name ,lambda-list
137 ,@body)
138 (setf (,(method-slot method) (alien-type-class-or-lose ',class))
139 #',defun-name))))
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)))
146 ((null class)
147 (error "method ~S not defined for ~S"
148 ',method (alien-type-class ,type)))
149 (let ((fn (,slot class)))
150 (when fn
151 (return fn))))
152 ,type ,@args))))
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 (sb!xc:macroexpand '&auxiliary-type-definitions& env)
164 (if expanded-p
165 result
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.
169 nil))))
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))
177 ,@body)))
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))
183 (if (consp type)
184 (let ((translator (info :alien-type :translator (car type))))
185 (unless translator
186 (error "unknown alien type: ~S" type))
187 (funcall translator type env))
188 (ecase (info :alien-type :kind type)
189 (:primitive
190 (let ((translator (info :alien-type :translator type)))
191 (unless translator
192 (error "no translator for primitive alien type ~S" type))
193 (funcall translator (list type) env)))
194 (:defined
195 (or (info :alien-type :definition type)
196 (error "no definition for alien type ~S" type)))
197 (:unknown
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)))))
207 (if in-auxiliaries
208 (values (third in-auxiliaries) t)
209 (ecase kind
210 (:struct
211 (info :alien-type :struct name))
212 (:union
213 (info :alien-type :union name))
214 (:enum
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*)
226 new-value)
228 (defun verify-local-auxiliaries-okay ()
229 (dolist (info *new-auxiliary-types*)
230 (destructuring-bind (kind name defn) info
231 (declare (ignore defn))
232 (when (ecase kind
233 (:struct
234 (info :alien-type :struct name))
235 (:union
236 (info :alien-type :union name))
237 (:enum
238 (info :alien-type :enum name)))
239 (error "attempt to shadow definition of ~A ~S" kind name)))))
241 (defun unparse-alien-type (type)
242 #!+sb-doc
243 "Convert the alien-type structure TYPE back into a list specification of
244 the type."
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
263 :environment env)
264 `(eval-when (:compile-toplevel :load-toplevel :execute)
265 (defun ,defun-name (,whole ,env)
266 (declare (ignorable ,env))
267 ,@decls
268 (block ,name
269 ,body))
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)
278 #+nil
279 (setf (fdocumentation name 'alien-type) docs)
280 name))
282 (def!macro define-alien-type (name type &environment env)
283 #!+sb-doc
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
286 specifier."
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*)))
292 ,@(when name
293 `((%define-alien-type ',name ',alien-type)))))))
295 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
296 (defun %def-auxiliary-alien-types (types)
297 (dolist (info 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*
304 :test (lambda (a b)
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))
311 (warn
312 "redefining ~A ~S to be:~% ~S,~%was:~% ~S"
313 kind name defn old))
314 (setf (info :alien-type ,kind name) defn))))
315 (ecase kind
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)
321 (:primitive
322 (error "~S is a built-in alien type." name))
323 (:defined
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"
327 name
328 (unparse-alien-type new)
329 (unparse-alien-type old)))))
330 (:unknown))
331 (setf (info :alien-type :definition name) new)
332 (setf (info :alien-type :kind name) :defined)
333 name))
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)))
351 ;;;; the SAP type
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)
368 (declare (ignore type))
369 'system-area-pointer)
371 (define-alien-type-method (system-area-pointer :naturalize-gen) (type alien)
372 (declare (ignore type))
373 alien)
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)
378 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))
390 nil)
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)
398 `(alien-sap ,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
408 ;; it is.
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")
413 stream
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)
420 #!+sb-doc
421 "Return T iff TYPE1 and TYPE2 describe equivalent alien types."
422 (or (eq type1 type2)
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)
428 #!+sb-doc
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
433 ALIEN-TYPE-=."
434 (or (eq type1 type2)
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)
450 `(alien ,type))
451 value)
452 (ignore ignore))
453 ,form)))
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)
464 (ignore ignore))
465 (naturalize ,(invoke-alien-type-method :extract-gen type 'sap 'offset)
466 ',type)))
468 (def!macro maybe-with-pinned-objects (variables types &body body)
469 (declare (ignorable variables types))
470 (let ((pin-variables
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.
474 #!+(or x86 x86-64)
475 (loop for variable in variables
476 for type in types
477 when (invoke-alien-type-method :deport-pin-p type)
478 collect variable)))
479 (if pin-variables
480 `(with-pinned-objects ,pin-variables
481 ,@body)
482 `(progn
483 ,@body))))
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)
490 (ignore ignore))
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 (defun compute-alien-rep-type (type)
506 (invoke-alien-type-method :alien-rep type))
508 ;;;; default methods
510 (define-alien-type-method (root :unparse) (type)
511 `(<unknown-alien-type> ,(type-of type)))
513 (define-alien-type-method (root :type=) (type1 type2)
514 (declare (ignore type1 type2))
517 (define-alien-type-method (root :subtypep) (type1 type2)
518 (alien-type-= type1 type2))
520 (define-alien-type-method (root :lisp-rep) (type)
521 (declare (ignore type))
522 nil)
524 (define-alien-type-method (root :alien-rep) (type)
525 (declare (ignore type))
528 (define-alien-type-method (root :naturalize-gen) (type alien)
529 (declare (ignore alien))
530 (error "cannot represent ~S typed aliens" type))
532 (define-alien-type-method (root :deport-gen) (type object)
533 (declare (ignore object))
534 (error "cannot represent ~S typed aliens" type))
536 (define-alien-type-method (root :deport-alloc-gen) (type object)
537 (declare (ignore type))
538 object)
540 (define-alien-type-method (root :deport-pin-p) (type)
541 (declare (ignore type))
542 ;; Override this method to return T for classes which take a SAP to a
543 ;; GCable lisp object when deporting.
544 nil)
546 (define-alien-type-method (root :extract-gen) (type sap offset)
547 (declare (ignore sap offset))
548 (error "cannot represent ~S typed aliens" type))
550 (define-alien-type-method (root :deposit-gen) (type sap offset value)
551 `(setf ,(invoke-alien-type-method :extract-gen type sap offset) ,value))
553 (define-alien-type-method (root :arg-tn) (type state)
554 (declare (ignore state))
555 (error "Aliens of type ~S cannot be passed as arguments to CALL-OUT."
556 (unparse-alien-type type)))
558 (define-alien-type-method (root :result-tn) (type state)
559 (declare (ignore state))
560 (error "Aliens of type ~S cannot be returned from CALL-OUT."
561 (unparse-alien-type type)))
563 ;;;; the INTEGER type
565 (define-alien-type-class (integer)
566 (signed t :type (member t nil)))
568 (define-alien-type-translator signed (&optional (bits sb!vm:n-word-bits))
569 (make-alien-integer-type :bits bits))
571 (define-alien-type-translator integer (&optional (bits sb!vm:n-word-bits))
572 (make-alien-integer-type :bits bits))
574 (define-alien-type-translator unsigned (&optional (bits sb!vm:n-word-bits))
575 (make-alien-integer-type :bits bits :signed nil))
577 (define-alien-type-method (integer :unparse) (type)
578 (list (if (alien-integer-type-signed type) 'signed 'unsigned)
579 (alien-integer-type-bits type)))
581 (define-alien-type-method (integer :type=) (type1 type2)
582 (and (eq (alien-integer-type-signed type1)
583 (alien-integer-type-signed type2))
584 (= (alien-integer-type-bits type1)
585 (alien-integer-type-bits type2))))
587 (define-alien-type-method (integer :lisp-rep) (type)
588 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
589 (alien-integer-type-bits type)))
591 (define-alien-type-method (integer :alien-rep) (type)
592 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
593 (alien-integer-type-bits type)))
595 (define-alien-type-method (integer :naturalize-gen) (type alien)
596 (declare (ignore type))
597 alien)
599 (define-alien-type-method (integer :deport-gen) (type value)
600 (declare (ignore type))
601 value)
603 (define-alien-type-method (integer :extract-gen) (type sap offset)
604 (declare (type alien-integer-type type))
605 (let ((ref-fun
606 (if (alien-integer-type-signed type)
607 (case (alien-integer-type-bits type)
608 (8 'signed-sap-ref-8)
609 (16 'signed-sap-ref-16)
610 (32 'signed-sap-ref-32)
611 (64 'signed-sap-ref-64))
612 (case (alien-integer-type-bits type)
613 (8 'sap-ref-8)
614 (16 'sap-ref-16)
615 (32 'sap-ref-32)
616 (64 'sap-ref-64)))))
617 (if ref-fun
618 `(,ref-fun ,sap (/ ,offset sb!vm:n-byte-bits))
619 (error "cannot extract ~W-bit integers"
620 (alien-integer-type-bits type)))))
622 ;;;; the BOOLEAN type
624 (define-alien-type-class (boolean :include integer :include-args (signed)))
626 ;;; FIXME: Check to make sure that we aren't attaching user-readable
627 ;;; stuff to CL:BOOLEAN in any way which impairs ANSI compliance.
628 (define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
629 (make-alien-boolean-type :bits bits :signed nil))
631 (define-alien-type-method (boolean :unparse) (type)
632 `(boolean ,(alien-boolean-type-bits type)))
634 (define-alien-type-method (boolean :lisp-rep) (type)
635 (declare (ignore type))
636 `(member t nil))
638 (define-alien-type-method (boolean :naturalize-gen) (type alien)
639 (declare (ignore type))
640 `(not (zerop ,alien)))
642 (define-alien-type-method (boolean :deport-gen) (type value)
643 (declare (ignore type))
644 `(if ,value 1 0))
646 ;;;; the ENUM type
648 (define-alien-type-class (enum :include (integer (bits 32))
649 :include-args (signed))
650 name ; name of this enum (if any)
651 from ; alist from symbols to integers
652 to ; alist or vector from integers to symbols
653 kind ; kind of from mapping, :VECTOR or :ALIST
654 offset) ; offset to add to value for :VECTOR from mapping
656 (define-alien-type-translator enum (&whole
657 type name
658 &rest mappings
659 &environment env)
660 (cond (mappings
661 (let ((result (parse-enum name mappings)))
662 (when name
663 (multiple-value-bind (old old-p)
664 (auxiliary-alien-type :enum name env)
665 (when old-p
666 (unless (alien-type-= result old)
667 (cerror "Continue, clobbering the old definition"
668 "Incompatible alien enum type definition: ~S" name)
669 (setf (alien-type-from old) (alien-type-from result)
670 (alien-type-to old) (alien-type-to result)
671 (alien-type-kind old) (alien-type-kind result)
672 (alien-type-offset old) (alien-type-offset result)
673 (alien-type-signed old) (alien-type-signed result)))
674 (setf result old))
675 (unless old-p
676 (setf (auxiliary-alien-type :enum name env) result))))
677 result))
678 (name
679 (multiple-value-bind (result found)
680 (auxiliary-alien-type :enum name env)
681 (unless found
682 (error "unknown enum type: ~S" name))
683 result))
685 (error "empty enum type: ~S" type))))
687 (defun parse-enum (name elements)
688 (when (null elements)
689 (error "An enumeration must contain at least one element."))
690 (let ((min nil)
691 (max nil)
692 (from-alist ())
693 (prev -1))
694 (declare (list from-alist))
695 (dolist (el elements)
696 (multiple-value-bind (sym val)
697 (if (listp el)
698 (values (first el) (second el))
699 (values el (1+ prev)))
700 (setf prev val)
701 (unless (symbolp sym)
702 (error "The enumeration element ~S is not a symbol." sym))
703 (unless (integerp val)
704 (error "The element value ~S is not an integer." val))
705 (unless (and max (> max val)) (setq max val))
706 (unless (and min (< min val)) (setq min val))
707 (when (rassoc val from-alist)
708 (style-warn "The element value ~S is used more than once." val))
709 (when (assoc sym from-alist :test #'eq)
710 (error "The enumeration element ~S is used more than once." sym))
711 (push (cons sym val) from-alist)))
712 (let* ((signed (minusp min))
713 (min-bits (if signed
714 (1+ (max (integer-length min)
715 (integer-length max)))
716 (integer-length max))))
717 (when (> min-bits 32)
718 (error "can't represent enums needing more than 32 bits"))
719 (setf from-alist (sort from-alist #'< :key #'cdr))
720 (cond
721 ;; If range is at least 20% dense, use vector mapping. Crossover
722 ;; point solely on basis of space would be 25%. Vector mapping
723 ;; is always faster, so give the benefit of the doubt.
724 ((< 0.2 (/ (float (length from-alist)) (float (1+ (- max min)))))
725 ;; If offset is small and ignorable, ignore it to save time.
726 (when (< 0 min 10) (setq min 0))
727 (let ((to (make-array (1+ (- max min)))))
728 (dolist (el from-alist)
729 (setf (svref to (- (cdr el) min)) (car el)))
730 (make-alien-enum-type :name name :signed signed
731 :from from-alist :to to :kind
732 :vector :offset (- min))))
734 (make-alien-enum-type :name name :signed signed
735 :from from-alist
736 :to (mapcar (lambda (x) (cons (cdr x) (car x)))
737 from-alist)
738 :kind :alist))))))
740 (define-alien-type-method (enum :unparse) (type)
741 `(enum ,(alien-enum-type-name type)
742 ,@(let ((prev -1))
743 (mapcar (lambda (mapping)
744 (let ((sym (car mapping))
745 (value (cdr mapping)))
746 (prog1
747 (if (= (1+ prev) value)
749 `(,sym ,value))
750 (setf prev value))))
751 (alien-enum-type-from type)))))
753 (define-alien-type-method (enum :type=) (type1 type2)
754 (and (eq (alien-enum-type-name type1)
755 (alien-enum-type-name type2))
756 (equal (alien-enum-type-from type1)
757 (alien-enum-type-from type2))))
759 (define-alien-type-method (enum :lisp-rep) (type)
760 `(member ,@(mapcar #'car (alien-enum-type-from type))))
762 (define-alien-type-method (enum :naturalize-gen) (type alien)
763 (ecase (alien-enum-type-kind type)
764 (:vector
765 `(svref ',(alien-enum-type-to type)
766 (+ ,alien ,(alien-enum-type-offset type))))
767 (:alist
768 `(ecase ,alien
769 ,@(mapcar (lambda (mapping)
770 `(,(car mapping) ',(cdr mapping)))
771 (alien-enum-type-to type))))))
773 (define-alien-type-method (enum :deport-gen) (type value)
774 `(ecase ,value
775 ,@(mapcar (lambda (mapping)
776 `(,(car mapping) ,(cdr mapping)))
777 (alien-enum-type-from type))))
779 ;;;; the FLOAT types
781 (define-alien-type-class (float)
782 (type (missing-arg) :type symbol))
784 (define-alien-type-method (float :unparse) (type)
785 (alien-float-type-type type))
787 (define-alien-type-method (float :lisp-rep) (type)
788 (alien-float-type-type type))
790 (define-alien-type-method (float :alien-rep) (type)
791 (alien-float-type-type type))
793 (define-alien-type-method (float :naturalize-gen) (type alien)
794 (declare (ignore type))
795 alien)
797 (define-alien-type-method (float :deport-gen) (type value)
798 (declare (ignore type))
799 value)
801 (define-alien-type-class (single-float :include (float (bits 32))
802 :include-args (type)))
804 (define-alien-type-translator single-float ()
805 (make-alien-single-float-type :type 'single-float))
807 (define-alien-type-method (single-float :extract-gen) (type sap offset)
808 (declare (ignore type))
809 `(sap-ref-single ,sap (/ ,offset sb!vm:n-byte-bits)))
811 (define-alien-type-class (double-float :include (float (bits 64))
812 :include-args (type)))
814 (define-alien-type-translator double-float ()
815 (make-alien-double-float-type :type 'double-float))
817 (define-alien-type-method (double-float :extract-gen) (type sap offset)
818 (declare (ignore type))
819 `(sap-ref-double ,sap (/ ,offset sb!vm:n-byte-bits)))
822 ;;;; the POINTER type
824 (define-alien-type-class (pointer :include (alien-value (bits
825 #!-alpha
826 sb!vm:n-word-bits
827 #!+alpha 64)))
828 (to nil :type (or alien-type null)))
830 (define-alien-type-translator * (to &environment env)
831 (make-alien-pointer-type :to (if (eq to t) nil (parse-alien-type to env))))
833 (define-alien-type-method (pointer :unparse) (type)
834 (let ((to (alien-pointer-type-to type)))
835 `(* ,(if to
836 (%unparse-alien-type to)
837 t))))
839 (define-alien-type-method (pointer :type=) (type1 type2)
840 (let ((to1 (alien-pointer-type-to type1))
841 (to2 (alien-pointer-type-to type2)))
842 (if to1
843 (if to2
844 (alien-type-= to1 to2)
845 nil)
846 (null to2))))
848 (define-alien-type-method (pointer :subtypep) (type1 type2)
849 (and (alien-pointer-type-p type2)
850 (let ((to1 (alien-pointer-type-to type1))
851 (to2 (alien-pointer-type-to type2)))
852 (if to1
853 (if to2
854 (alien-subtype-p to1 to2)
856 (null to2)))))
858 (define-alien-type-method (pointer :deport-gen) (type value)
859 (/noshow "doing alien type method POINTER :DEPORT-GEN" type value)
860 (values
861 ;; FIXME: old version, highlighted a bug in xc optimization
862 `(etypecase ,value
863 (null
864 (int-sap 0))
865 (system-area-pointer
866 ,value)
867 ((alien ,type)
868 (alien-sap ,value)))
869 ;; new version, works around bug in xc optimization
870 #+nil
871 `(etypecase ,value
872 (system-area-pointer
873 ,value)
874 ((alien ,type)
875 (alien-sap ,value))
876 (null
877 (int-sap 0)))
878 `(or null system-area-pointer (alien ,type))))
880 ;;;; the MEM-BLOCK type
882 (define-alien-type-class (mem-block :include alien-value))
884 (define-alien-type-method (mem-block :extract-gen) (type sap offset)
885 (declare (ignore type))
886 `(sap+ ,sap (truncate ,offset sb!vm:n-byte-bits)))
888 (define-alien-type-method (mem-block :deposit-gen) (type sap offset value)
889 (let ((bits (alien-mem-block-type-bits type)))
890 (unless bits
891 (error "can't deposit aliens of type ~S (unknown size)" type))
892 `(sb!kernel:system-area-ub8-copy ,value 0 ,sap
893 (truncate ,offset sb!vm:n-byte-bits)
894 ',(truncate bits sb!vm:n-byte-bits))))
896 ;;;; the ARRAY type
898 (define-alien-type-class (array :include mem-block)
899 (element-type (missing-arg) :type alien-type)
900 (dimensions (missing-arg) :type list))
902 (define-alien-type-translator array (ele-type &rest dims &environment env)
904 (when dims
905 (unless (typep (first dims) '(or index null))
906 (error "The first dimension is not a non-negative fixnum or NIL: ~S"
907 (first dims)))
908 (let ((loser (find-if-not (lambda (x) (typep x 'index))
909 (rest dims))))
910 (when loser
911 (error "A dimension is not a non-negative fixnum: ~S" loser))))
913 (let ((parsed-ele-type (parse-alien-type ele-type env)))
914 (make-alien-array-type
915 :element-type parsed-ele-type
916 :dimensions dims
917 :alignment (alien-type-alignment parsed-ele-type)
918 :bits (if (and (alien-type-bits parsed-ele-type)
919 (every #'integerp dims))
920 (* (align-offset (alien-type-bits parsed-ele-type)
921 (alien-type-alignment parsed-ele-type))
922 (reduce #'* dims))))))
924 (define-alien-type-method (array :unparse) (type)
925 `(array ,(%unparse-alien-type (alien-array-type-element-type type))
926 ,@(alien-array-type-dimensions type)))
928 (define-alien-type-method (array :type=) (type1 type2)
929 (and (equal (alien-array-type-dimensions type1)
930 (alien-array-type-dimensions type2))
931 (alien-type-= (alien-array-type-element-type type1)
932 (alien-array-type-element-type type2))))
934 (define-alien-type-method (array :subtypep) (type1 type2)
935 (and (alien-array-type-p type2)
936 (let ((dim1 (alien-array-type-dimensions type1))
937 (dim2 (alien-array-type-dimensions type2)))
938 (and (= (length dim1) (length dim2))
939 (or (and dim2
940 (null (car dim2))
941 (equal (cdr dim1) (cdr dim2)))
942 (equal dim1 dim2))
943 (alien-subtype-p (alien-array-type-element-type type1)
944 (alien-array-type-element-type type2))))))
946 ;;;; the RECORD type
948 (def!struct (alien-record-field
949 (:make-load-form-fun sb!kernel:just-dump-it-normally))
950 (name (missing-arg) :type symbol)
951 (type (missing-arg) :type alien-type)
952 (bits nil :type (or unsigned-byte null))
953 (offset 0 :type unsigned-byte))
954 (def!method print-object ((field alien-record-field) stream)
955 (print-unreadable-object (field stream :type t)
956 (format stream
957 "~S ~S~@[:~D~]"
958 (alien-record-field-type field)
959 (alien-record-field-name field)
960 (alien-record-field-bits field))))
962 (define-alien-type-class (record :include mem-block)
963 (kind :struct :type (member :struct :union))
964 (name nil :type (or symbol null))
965 (fields nil :type list))
967 (define-alien-type-translator struct (name &rest fields &environment env)
968 (parse-alien-record-type :struct name fields env))
970 (define-alien-type-translator union (name &rest fields &environment env)
971 (parse-alien-record-type :union name fields env))
973 ;;; FIXME: This is really pretty horrible: we avoid creating new
974 ;;; ALIEN-RECORD-TYPE objects when a live one is flitting around the
975 ;;; system already. This way forwrd-references sans fields get get
976 ;;; "updated" for free to contain the field info. Maybe rename
977 ;;; MAKE-ALIEN-RECORD-TYPE to %MAKE-ALIEN-RECORD-TYPE and use
978 ;;; ENSURE-ALIEN-RECORD-TYPE instead. --NS 20040729
979 (defun parse-alien-record-type (kind name fields env)
980 (declare (type (or sb!kernel:lexenv null) env))
981 (flet ((frob-type (type new-fields alignment bits)
982 (setf (alien-record-type-fields type) new-fields
983 (alien-record-type-alignment type) alignment
984 (alien-record-type-bits type) bits)))
985 (cond (fields
986 (multiple-value-bind (new-fields alignment bits)
987 (parse-alien-record-fields kind fields env)
988 (let* ((old (and name (auxiliary-alien-type kind name env)))
989 (old-fields (and old (alien-record-type-fields old))))
990 (when (and old-fields
991 (notevery #'record-fields-match-p old-fields new-fields))
992 (cerror "Continue, clobbering the old definition."
993 "Incompatible alien record type definition~%Old: ~S~%New: ~S"
994 (unparse-alien-type old)
995 `(,(unparse-alien-record-kind kind)
996 ,name
997 ,@(mapcar #'unparse-alien-record-field new-fields)))
998 (frob-type old new-fields alignment bits))
999 (if old-fields
1001 (let ((type (or old (make-alien-record-type :name name :kind kind))))
1002 (when (and name (not old))
1003 (setf (auxiliary-alien-type kind name env) type))
1004 (frob-type type new-fields alignment bits)
1005 type)))))
1006 (name
1007 (or (auxiliary-alien-type kind name env)
1008 (setf (auxiliary-alien-type kind name env)
1009 (make-alien-record-type :name name :kind kind))))
1011 (make-alien-record-type :kind kind)))))
1013 ;;; This is used by PARSE-ALIEN-TYPE to parse the fields of struct and union
1014 ;;; types. KIND is the kind we are paring the fields of, and FIELDS is the
1015 ;;; list of field specifications.
1017 ;;; Result is a list of field objects, overall alignment, and number of bits
1018 (defun parse-alien-record-fields (kind fields env)
1019 (declare (type list fields))
1020 (let ((total-bits 0)
1021 (overall-alignment 1)
1022 (parsed-fields nil))
1023 (dolist (field fields)
1024 (destructuring-bind (var type &key alignment) field
1025 (let* ((field-type (parse-alien-type type env))
1026 (bits (alien-type-bits field-type))
1027 (parsed-field
1028 (make-alien-record-field :type field-type
1029 :name var)))
1030 (unless alignment
1031 (setf alignment (alien-type-alignment field-type)))
1032 (push parsed-field parsed-fields)
1033 (when (null bits)
1034 (error "unknown size: ~S" (unparse-alien-type field-type)))
1035 (when (null alignment)
1036 (error "unknown alignment: ~S" (unparse-alien-type field-type)))
1037 (setf overall-alignment (max overall-alignment alignment))
1038 (ecase kind
1039 (:struct
1040 (let ((offset (align-offset total-bits alignment)))
1041 (setf (alien-record-field-offset parsed-field) offset)
1042 (setf total-bits (+ offset bits))))
1043 (:union
1044 (setf total-bits (max total-bits bits)))))))
1045 (values (nreverse parsed-fields)
1046 overall-alignment
1047 (align-offset total-bits overall-alignment))))
1049 (define-alien-type-method (record :unparse) (type)
1050 `(,(unparse-alien-record-kind (alien-record-type-kind type))
1051 ,(alien-record-type-name type)
1052 ,@(unless (member type *record-types-already-unparsed* :test #'eq)
1053 (push type *record-types-already-unparsed*)
1054 (mapcar #'unparse-alien-record-field
1055 (alien-record-type-fields type)))))
1057 (defun unparse-alien-record-kind (kind)
1058 (case kind
1059 (:struct 'struct)
1060 (:union 'union)
1061 (t '???)))
1063 (defun unparse-alien-record-field (field)
1064 `(,(alien-record-field-name field)
1065 ,(%unparse-alien-type (alien-record-field-type field))
1066 ,@(when (alien-record-field-bits field)
1067 (list (alien-record-field-bits field)))))
1069 ;;; Test the record fields. Keep a hashtable table of already compared
1070 ;;; types to detect cycles.
1071 (defun record-fields-match-p (field1 field2)
1072 (and (eq (alien-record-field-name field1)
1073 (alien-record-field-name field2))
1074 (eql (alien-record-field-bits field1)
1075 (alien-record-field-bits field2))
1076 (eql (alien-record-field-offset field1)
1077 (alien-record-field-offset field2))
1078 (alien-type-= (alien-record-field-type field1)
1079 (alien-record-field-type field2))))
1081 (defvar *alien-type-matches* nil
1082 "A hashtable used to detect cycles while comparing record types.")
1084 (define-alien-type-method (record :type=) (type1 type2)
1085 (and (eq (alien-record-type-name type1)
1086 (alien-record-type-name type2))
1087 (eq (alien-record-type-kind type1)
1088 (alien-record-type-kind type2))
1089 (eql (alien-type-bits type1)
1090 (alien-type-bits type2))
1091 (eql (alien-type-alignment type1)
1092 (alien-type-alignment type2))
1093 (flet ((match-fields (&optional old)
1094 (setf (gethash type1 *alien-type-matches*) (cons type2 old))
1095 (every #'record-fields-match-p
1096 (alien-record-type-fields type1)
1097 (alien-record-type-fields type2))))
1098 (if *alien-type-matches*
1099 (let ((types (gethash type1 *alien-type-matches*)))
1100 (or (memq type2 types) (match-fields types)))
1101 (let ((*alien-type-matches* (make-hash-table :test #'eq)))
1102 (match-fields))))))
1104 ;;;; the FUNCTION and VALUES alien types
1106 (define-alien-type-class (fun :include mem-block)
1107 (result-type (missing-arg) :type alien-type)
1108 (arg-types (missing-arg) :type list)
1109 (stub nil :type (or null function)))
1111 (define-alien-type-translator function (result-type &rest arg-types
1112 &environment env)
1113 (make-alien-fun-type
1114 :result-type (let ((*values-type-okay* t))
1115 (parse-alien-type result-type env))
1116 :arg-types (mapcar (lambda (arg-type) (parse-alien-type arg-type env))
1117 arg-types)))
1119 (define-alien-type-method (fun :unparse) (type)
1120 `(function ,(%unparse-alien-type (alien-fun-type-result-type type))
1121 ,@(mapcar #'%unparse-alien-type
1122 (alien-fun-type-arg-types type))))
1124 (define-alien-type-method (fun :type=) (type1 type2)
1125 (and (alien-type-= (alien-fun-type-result-type type1)
1126 (alien-fun-type-result-type type2))
1127 (= (length (alien-fun-type-arg-types type1))
1128 (length (alien-fun-type-arg-types type2)))
1129 (every #'alien-type-=
1130 (alien-fun-type-arg-types type1)
1131 (alien-fun-type-arg-types type2))))
1133 (define-alien-type-class (values)
1134 (values (missing-arg) :type list))
1136 (define-alien-type-translator values (&rest values &environment env)
1137 (unless *values-type-okay*
1138 (error "cannot use values types here"))
1139 (let ((*values-type-okay* nil))
1140 (make-alien-values-type
1141 :values (mapcar (lambda (alien-type) (parse-alien-type alien-type env))
1142 values))))
1144 (define-alien-type-method (values :unparse) (type)
1145 `(values ,@(mapcar #'%unparse-alien-type
1146 (alien-values-type-values type))))
1148 (define-alien-type-method (values :type=) (type1 type2)
1149 (and (= (length (alien-values-type-values type1))
1150 (length (alien-values-type-values type2)))
1151 (every #'alien-type-=
1152 (alien-values-type-values type1)
1153 (alien-values-type-values type2))))
1155 ;;;; a structure definition needed both in the target and in the
1156 ;;;; cross-compilation host
1158 ;;; information about local aliens. The WITH-ALIEN macro builds one of
1159 ;;; these structures and LOCAL-ALIEN and friends communicate
1160 ;;; information about how that local alien is represented.
1161 (def!struct (local-alien-info
1162 (:make-load-form-fun sb!kernel:just-dump-it-normally)
1163 (:constructor make-local-alien-info
1164 (&key type force-to-memory-p
1165 &aux (force-to-memory-p (or force-to-memory-p
1166 (alien-array-type-p type)
1167 (alien-record-type-p type))))))
1168 ;; the type of the local alien
1169 (type (missing-arg) :type alien-type)
1170 ;; Must this local alien be forced into memory? Using the ADDR macro
1171 ;; on a local alien will set this.
1172 (force-to-memory-p nil :type (member t nil)))
1173 (def!method print-object ((info local-alien-info) stream)
1174 (print-unreadable-object (info stream :type t)
1175 (format stream
1176 "~:[~;(forced to stack) ~]~S"
1177 (local-alien-info-force-to-memory-p info)
1178 (unparse-alien-type (local-alien-info-type info)))))
1180 ;;;; the ADDR macro
1182 (defmacro-mundanely addr (expr &environment env)
1183 #!+sb-doc
1184 "Return an Alien pointer to the data addressed by Expr, which must be a call
1185 to SLOT or DEREF, or a reference to an Alien variable."
1186 (let ((form (sb!xc:macroexpand expr env)))
1187 (or (typecase form
1188 (cons
1189 (case (car form)
1190 (slot
1191 (cons '%slot-addr (cdr form)))
1192 (deref
1193 (cons '%deref-addr (cdr form)))
1194 (%heap-alien
1195 (cons '%heap-alien-addr (cdr form)))
1196 (local-alien
1197 (let ((info (let ((info-arg (second form)))
1198 (and (consp info-arg)
1199 (eq (car info-arg) 'quote)
1200 (second info-arg)))))
1201 (unless (local-alien-info-p info)
1202 (error "Something is wrong, LOCAL-ALIEN-INFO not found: ~S"
1203 form))
1204 (setf (local-alien-info-force-to-memory-p info) t))
1205 (cons '%local-alien-addr (cdr form)))))
1206 (symbol
1207 (let ((kind (info :variable :kind form)))
1208 (when (eq kind :alien)
1209 `(%heap-alien-addr ',(info :variable :alien-info form))))))
1210 (error "~S is not a valid L-value." form))))
1212 (/show0 "host-alieneval.lisp end of file")