Remove some test noise. A drop in the ocean unfortunately.
[sbcl.git] / src / code / host-alieneval.lisp
blob606c500949324656b3411c3c43d8fe7c6e4971d4
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 (%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 sb!kernel:lexenv-designator 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 sb!kernel:lexenv-designator 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 sb!kernel:lexenv-designator 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 (let ((defun-name (symbolicate "ALIEN-" name "-TYPE-TRANSLATOR")))
259 `(eval-when (:compile-toplevel :load-toplevel :execute)
260 (setf (symbol-function ',defun-name)
261 ,(make-macro-lambda defun-name lambda-list body
262 'define-alien-type-translator name))
263 (%define-alien-type-translator ',name #',defun-name))))
265 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
266 (defun %define-alien-type-translator (name translator)
267 (setf (info :alien-type :kind name) :primitive)
268 (setf (info :alien-type :translator name) translator)
269 (clear-info :alien-type :definition name)
270 name))
272 (def!macro define-alien-type (name type &environment env)
273 #!+sb-doc
274 "Define the alien type NAME to be equivalent to TYPE. Name may be NIL for
275 STRUCT and UNION types, in which case the name is taken from the type
276 specifier."
277 (with-auxiliary-alien-types env
278 (let ((alien-type (parse-alien-type type env)))
279 `(eval-when (:compile-toplevel :load-toplevel :execute)
280 ,@(when *new-auxiliary-types*
281 `((%def-auxiliary-alien-types ',*new-auxiliary-types*
282 (sb!c:source-location))))
283 ,@(when name
284 `((%define-alien-type ',name ',alien-type)
285 (setf (info :source-location :alien-type ',name)
286 (sb!c:source-location))))))))
288 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
289 (defun %def-auxiliary-alien-types (types source-location)
290 (dolist (info types)
291 ;; Clear up the type we're about to define from the toplevel
292 ;; *new-auxiliary-types* (local scopes take care of themselves).
293 ;; Unless this is done we never actually get back the full type
294 ;; from INFO, since the *new-auxiliary-types* have precendence.
295 (setf *new-auxiliary-types*
296 (remove info *new-auxiliary-types*
297 :test (lambda (a b)
298 (and (eq (first a) (first b))
299 (eq (second a) (second b))))))
300 (destructuring-bind (kind name defn) info
301 (macrolet ((frob (kind)
302 `(let ((old (info :alien-type ,kind name)))
303 (unless (or (null old) (alien-type-= old defn))
304 (warn
305 "redefining ~A ~S to be:~% ~S,~%was:~% ~S"
306 kind name defn old))
307 (setf (info :alien-type ,kind name) defn
308 (info :source-location :alien-type name)
309 source-location))))
310 (ecase kind
311 (:struct (frob :struct))
312 (:union (frob :union))
313 (:enum (frob :enum)))))))
315 (defun %define-alien-type (name new)
316 (ecase (info :alien-type :kind name)
317 (:primitive
318 (error "~S is a built-in alien type." name))
319 (:defined
320 (let ((old (info :alien-type :definition name)))
321 (unless (or (null old) (alien-type-= new old))
322 (warn "redefining ~S to be:~% ~S,~%was~% ~S"
323 name
324 (unparse-alien-type new)
325 (unparse-alien-type old)))))
326 (:unknown))
327 (setf (info :alien-type :definition name) new)
328 (setf (info :alien-type :kind name) :defined)
329 name))
331 ;;;; the root alien type
333 (eval-when (:compile-toplevel :load-toplevel :execute)
334 (create-alien-type-class-if-necessary 'root 'alien-type nil))
336 (def!struct (alien-type
337 (:make-load-form-fun sb!kernel:just-dump-it-normally)
338 (:constructor make-alien-type (&key class bits alignment
339 &aux (alignment (or alignment (guess-alignment bits))))))
340 (class 'root :type symbol)
341 (bits nil :type (or null unsigned-byte))
342 (alignment nil :type (or null unsigned-byte)))
343 (def!method print-object ((type alien-type) stream)
344 (print-unreadable-object (type stream :type t)
345 ;; Kludge to avoid printing #'(SIGNED 64) instead of (FUNCTION (SIGNED 64))
346 ;; for a 0-argument function. This is only a problem with alien types
347 ;; because ordinary FUNCTION type specifiers are 3-lists.
348 (let ((sb!pretty:*pprint-quote-with-syntactic-sugar* nil))
349 ;; forward-reference of this special variable unfortunately
350 (declare (special sb!pretty:*pprint-quote-with-syntactic-sugar*))
351 (prin1 (unparse-alien-type type) stream))))
353 ;;;; the SAP type
355 (define-alien-type-class (system-area-pointer))
357 (define-alien-type-translator system-area-pointer ()
358 (make-alien-system-area-pointer-type
359 :bits #!-alpha sb!vm:n-word-bits #!+alpha 64))
361 (define-alien-type-method (system-area-pointer :unparse) (type)
362 (declare (ignore type))
363 'system-area-pointer)
365 (define-alien-type-method (system-area-pointer :lisp-rep) (type)
366 (declare (ignore type))
367 'system-area-pointer)
369 (define-alien-type-method (system-area-pointer :alien-rep) (type context)
370 (declare (ignore type context))
371 'system-area-pointer)
373 (define-alien-type-method (system-area-pointer :naturalize-gen) (type alien)
374 (declare (ignore type))
375 alien)
377 (define-alien-type-method (system-area-pointer :deport-gen) (type object)
378 (declare (ignore type))
379 (/noshow "doing alien type method SYSTEM-AREA-POINTER :DEPORT-GEN" object)
380 object)
382 (define-alien-type-method (system-area-pointer :extract-gen) (type sap offset)
383 (declare (ignore type))
384 `(sap-ref-sap ,sap (/ ,offset sb!vm:n-byte-bits)))
386 ;;;; the ALIEN-VALUE type
388 (define-alien-type-class (alien-value :include system-area-pointer))
390 (define-alien-type-method (alien-value :lisp-rep) (type)
391 (declare (ignore type))
392 nil)
394 (define-alien-type-method (alien-value :naturalize-gen) (type alien)
395 `(%sap-alien ,alien ',type))
397 (define-alien-type-method (alien-value :deport-gen) (type value)
398 (declare (ignore type))
399 (/noshow "doing alien type method ALIEN-VALUE :DEPORT-GEN" value)
400 `(alien-sap ,value))
402 ;;; HEAP-ALIEN-INFO -- defstruct.
404 ;;; Information describing a heap-allocated alien.
405 (def!struct (heap-alien-info
406 (:make-load-form-fun sb!kernel:just-dump-it-normally))
407 ;; The type of this alien.
408 (type (missing-arg) :type alien-type)
409 ;; Its name.
410 (alien-name (missing-arg) :type simple-string)
411 ;; Data or code?
412 (datap (missing-arg) :type boolean))
413 (def!method print-object ((info heap-alien-info) stream)
414 (print-unreadable-object (info stream :type t)
415 (funcall (formatter "~S ~S~@[ (data)~]")
416 stream
417 (heap-alien-info-alien-name info)
418 (unparse-alien-type (heap-alien-info-type info))
419 (heap-alien-info-datap info))))
421 ;;; The form to evaluate to produce the SAP pointing to where in the heap
422 ;;; it is.
423 (defun heap-alien-info-sap-form (info)
424 `(foreign-symbol-sap ,(heap-alien-info-alien-name info)
425 ,(heap-alien-info-datap info)))
427 (defun heap-alien-info-sap (info)
428 (foreign-symbol-sap (heap-alien-info-alien-name info)
429 (heap-alien-info-datap info)))
431 ;;;; Interfaces to the different methods
433 (defun alien-type-= (type1 type2)
434 #!+sb-doc
435 "Return T iff TYPE1 and TYPE2 describe equivalent alien types."
436 (or (eq type1 type2)
437 (and (eq (alien-type-class type1)
438 (alien-type-class type2))
439 (invoke-alien-type-method :type= type1 type2))))
441 (defun alien-subtype-p (type1 type2)
442 #!+sb-doc
443 "Return T iff the alien type TYPE1 is a subtype of TYPE2. Currently, the
444 only supported subtype relationships are is that any pointer type is a
445 subtype of (* t), and any array type first dimension will match
446 (array <eltype> nil ...). Otherwise, the two types have to be
447 ALIEN-TYPE-=."
448 (or (eq type1 type2)
449 (invoke-alien-type-method :subtypep type1 type2)))
451 (defun compute-naturalize-lambda (type)
452 `(lambda (alien ignore)
453 (declare (ignore ignore))
454 ,(invoke-alien-type-method :naturalize-gen type 'alien)))
456 (defun compute-deport-lambda (type)
457 (declare (type alien-type type))
458 (/noshow "entering COMPUTE-DEPORT-LAMBDA" type)
459 (multiple-value-bind (form value-type)
460 (invoke-alien-type-method :deport-gen type 'value)
461 `(lambda (value ignore)
462 (declare (type ,(or value-type
463 (compute-lisp-rep-type type)
464 `(alien ,type))
465 value)
466 (ignore ignore))
467 ,form)))
469 (defun compute-deport-alloc-lambda (type)
470 `(lambda (value ignore)
471 (declare (ignore ignore))
472 ,(invoke-alien-type-method :deport-alloc-gen type 'value)))
474 (defun compute-extract-lambda (type)
475 `(lambda (sap offset ignore)
476 (declare (type system-area-pointer sap)
477 (type unsigned-byte offset)
478 (ignore ignore))
479 (naturalize ,(invoke-alien-type-method :extract-gen type 'sap 'offset)
480 ',type)))
482 (def!macro maybe-with-pinned-objects (variables types &body body)
483 (declare (ignorable variables types))
484 (let ((pin-variables
485 ;; Only pin things on GENCGC, since on CHENEYGC it'd imply
486 ;; disabling the GC. Which is something we don't want to do
487 ;; every time we're calling to C.
488 #!+gencgc
489 (loop for variable in variables
490 for type in types
491 when (invoke-alien-type-method :deport-pin-p type)
492 collect variable)))
493 (if pin-variables
494 `(with-pinned-objects ,pin-variables
495 ,@body)
496 `(progn
497 ,@body))))
499 (defun compute-deposit-lambda (type)
500 (declare (type alien-type type))
501 `(lambda (value sap offset ignore)
502 (declare (type system-area-pointer sap)
503 (type unsigned-byte offset)
504 (ignore ignore))
505 (let ((alloc-tmp (deport-alloc value ',type)))
506 (maybe-with-pinned-objects (alloc-tmp) (,type)
507 (let ((value (deport alloc-tmp ',type)))
508 ,(invoke-alien-type-method :deposit-gen type 'sap 'offset 'value)
509 ;; Note: the reason we don't just return the pre-deported value
510 ;; is because that would inhibit any (deport (naturalize ...))
511 ;; optimizations that might have otherwise happen. Re-naturalizing
512 ;; the value might cause extra consing, but is flushable, so probably
513 ;; results in better code.
514 (naturalize value ',type))))))
516 (defun compute-lisp-rep-type (type)
517 (invoke-alien-type-method :lisp-rep type))
519 ;;; CONTEXT is either :NORMAL (the default) or :RESULT (alien function
520 ;;; return values). See the :ALIEN-REP method for INTEGER for
521 ;;; details.
522 (defun compute-alien-rep-type (type &optional (context :normal))
523 (invoke-alien-type-method :alien-rep type context))
525 ;;;; default methods
527 (define-alien-type-method (root :unparse) (type)
528 `(<unknown-alien-type> ,(type-of type)))
530 (define-alien-type-method (root :type=) (type1 type2)
531 (declare (ignore type1 type2))
534 (define-alien-type-method (root :subtypep) (type1 type2)
535 (alien-type-= type1 type2))
537 (define-alien-type-method (root :lisp-rep) (type)
538 (declare (ignore type))
539 nil)
541 (define-alien-type-method (root :alien-rep) (type context)
542 (declare (ignore type context))
545 (define-alien-type-method (root :naturalize-gen) (type alien)
546 (declare (ignore alien))
547 (error "cannot represent ~S typed aliens" type))
549 (define-alien-type-method (root :deport-gen) (type object)
550 (declare (ignore object))
551 (error "cannot represent ~S typed aliens" type))
553 (define-alien-type-method (root :deport-alloc-gen) (type object)
554 (declare (ignore type))
555 object)
557 (define-alien-type-method (root :deport-pin-p) (type)
558 (declare (ignore type))
559 ;; Override this method to return T for classes which take a SAP to a
560 ;; GCable lisp object when deporting.
561 nil)
563 (define-alien-type-method (root :extract-gen) (type sap offset)
564 (declare (ignore sap offset))
565 (error "cannot represent ~S typed aliens" type))
567 (define-alien-type-method (root :deposit-gen) (type sap offset value)
568 `(setf ,(invoke-alien-type-method :extract-gen type sap offset) ,value))
570 (define-alien-type-method (root :arg-tn) (type state)
571 (declare (ignore state))
572 (error "Aliens of type ~S cannot be passed as arguments to CALL-OUT."
573 (unparse-alien-type type)))
575 (define-alien-type-method (root :result-tn) (type state)
576 (declare (ignore state))
577 (error "Aliens of type ~S cannot be returned from CALL-OUT."
578 (unparse-alien-type type)))
580 ;;;; the INTEGER type
582 (define-alien-type-class (integer)
583 (signed t :type (member t nil)))
585 (define-alien-type-translator signed (&optional (bits sb!vm:n-word-bits))
586 (make-alien-integer-type :bits bits))
588 (define-alien-type-translator integer (&optional (bits sb!vm:n-word-bits))
589 (make-alien-integer-type :bits bits))
591 (define-alien-type-translator unsigned (&optional (bits sb!vm:n-word-bits))
592 (make-alien-integer-type :bits bits :signed nil))
594 (define-alien-type-method (integer :unparse) (type)
595 (list (if (alien-integer-type-signed type) 'signed 'unsigned)
596 (alien-integer-type-bits type)))
598 (define-alien-type-method (integer :type=) (type1 type2)
599 (and (eq (alien-integer-type-signed type1)
600 (alien-integer-type-signed type2))
601 (= (alien-integer-type-bits type1)
602 (alien-integer-type-bits type2))))
604 (define-alien-type-method (integer :lisp-rep) (type)
605 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
606 (alien-integer-type-bits type)))
608 (define-alien-type-method (integer :alien-rep) (type context)
609 ;; When returning integer values that are narrower than a machine
610 ;; register from a function, some platforms leave the higher bits of
611 ;; the register uninitialized. On those platforms, we use an
612 ;; alien-rep of the full register width when checking for purposes
613 ;; of return values and override the naturalize method to perform
614 ;; the sign extension (in compiler/target/c-call.lisp).
615 (ecase context
616 ((:normal #!-(or x86 x86-64) :result)
617 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
618 (alien-integer-type-bits type)))
619 #!+(or x86 x86-64)
620 (:result
621 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
622 (max (alien-integer-type-bits type)
623 sb!vm:n-word-bits)))))
625 ;;; As per the comment in the :ALIEN-REP method above, this is defined
626 ;;; elsewhere for x86oids.
627 #!-(or x86 x86-64)
628 (define-alien-type-method (integer :naturalize-gen) (type alien)
629 (declare (ignore type))
630 alien)
632 (define-alien-type-method (integer :deport-gen) (type value)
633 (declare (ignore type))
634 value)
636 (define-alien-type-method (integer :extract-gen) (type sap offset)
637 (declare (type alien-integer-type type))
638 (let ((ref-fun
639 (if (alien-integer-type-signed type)
640 (case (alien-integer-type-bits type)
641 (8 'signed-sap-ref-8)
642 (16 'signed-sap-ref-16)
643 (32 'signed-sap-ref-32)
644 (64 'signed-sap-ref-64))
645 (case (alien-integer-type-bits type)
646 (8 'sap-ref-8)
647 (16 'sap-ref-16)
648 (32 'sap-ref-32)
649 (64 'sap-ref-64)))))
650 (if ref-fun
651 `(,ref-fun ,sap (/ ,offset sb!vm:n-byte-bits))
652 (error "cannot extract ~W-bit integers"
653 (alien-integer-type-bits type)))))
655 ;;;; the BOOLEAN type
657 (define-alien-type-class (boolean :include integer :include-args (signed)))
659 ;;; FIXME: Check to make sure that we aren't attaching user-readable
660 ;;; stuff to CL:BOOLEAN in any way which impairs ANSI compliance.
661 (define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
662 (make-alien-boolean-type :bits bits :signed nil))
664 (define-alien-type-method (boolean :unparse) (type)
665 `(boolean ,(alien-boolean-type-bits type)))
667 (define-alien-type-method (boolean :lisp-rep) (type)
668 (declare (ignore type))
669 `(member t nil))
671 (define-alien-type-method (boolean :naturalize-gen) (type alien)
672 (let ((bits (alien-boolean-type-bits type)))
673 (if (= bits sb!vm:n-word-bits)
674 `(not (zerop ,alien))
675 `(logtest ,alien ,(ldb (byte bits 0) -1)))))
677 (define-alien-type-method (boolean :deport-gen) (type value)
678 (declare (ignore type))
679 `(if ,value 1 0))
681 ;;;; the ENUM type
683 (define-alien-type-class (enum :include (integer (bits 32))
684 :include-args (signed))
685 name ; name of this enum (if any)
686 from ; alist from symbols to integers
687 to ; alist or vector from integers to symbols
688 kind ; kind of from mapping, :VECTOR or :ALIST
689 offset) ; offset to add to value for :VECTOR from mapping
691 (define-alien-type-translator enum (&whole
692 type name
693 &rest mappings
694 &environment env)
695 (cond (mappings
696 (let ((result (parse-enum name mappings)))
697 (when name
698 (multiple-value-bind (old old-p)
699 (auxiliary-alien-type :enum name env)
700 (when old-p
701 (unless (alien-type-= result old)
702 (cerror "Continue, clobbering the old definition"
703 "Incompatible alien enum type definition: ~S" name)
704 (setf (alien-type-from old) (alien-type-from result)
705 (alien-type-to old) (alien-type-to result)
706 (alien-type-kind old) (alien-type-kind result)
707 (alien-type-offset old) (alien-type-offset result)
708 (alien-type-signed old) (alien-type-signed result)))
709 (setf result old))
710 (unless old-p
711 (setf (auxiliary-alien-type :enum name env) result))))
712 result))
713 (name
714 (multiple-value-bind (result found)
715 (auxiliary-alien-type :enum name env)
716 (unless found
717 (error "unknown enum type: ~S" name))
718 result))
720 (error "empty enum type: ~S" type))))
722 (defun parse-enum (name elements)
723 (when (null elements)
724 (error "An enumeration must contain at least one element."))
725 (let ((min nil)
726 (max nil)
727 (from-alist ())
728 (prev -1))
729 (declare (list from-alist))
730 (dolist (el elements)
731 (multiple-value-bind (sym val)
732 (if (listp el)
733 (values (first el) (second el))
734 (values el (1+ prev)))
735 (setf prev val)
736 (unless (symbolp sym)
737 (error "The enumeration element ~S is not a symbol." sym))
738 (unless (integerp val)
739 (error "The element value ~S is not an integer." val))
740 (unless (and max (> max val)) (setq max val))
741 (unless (and min (< min val)) (setq min val))
742 (when (rassoc val from-alist)
743 (style-warn "The element value ~S is used more than once." val))
744 (when (assoc sym from-alist :test #'eq)
745 (error "The enumeration element ~S is used more than once." sym))
746 (push (cons sym val) from-alist)))
747 (let* ((signed (minusp min))
748 (min-bits (if signed
749 (1+ (max (integer-length min)
750 (integer-length max)))
751 (integer-length max))))
752 (when (> min-bits 32)
753 (error "can't represent enums needing more than 32 bits"))
754 (setf from-alist (sort from-alist #'< :key #'cdr))
755 (cond
756 ;; If range is at least 20% dense, use vector mapping. Crossover
757 ;; point solely on basis of space would be 25%. Vector mapping
758 ;; is always faster, so give the benefit of the doubt.
759 ((< 0.2 (/ (float (length from-alist)) (float (1+ (- max min)))))
760 ;; If offset is small and ignorable, ignore it to save time.
761 (when (< 0 min 10) (setq min 0))
762 (let ((to (make-array (1+ (- max min)))))
763 (dolist (el from-alist)
764 (setf (svref to (- (cdr el) min)) (car el)))
765 (make-alien-enum-type :name name :signed signed
766 :from from-alist :to to :kind
767 :vector :offset (- min))))
769 (make-alien-enum-type :name name :signed signed
770 :from from-alist
771 :to (mapcar (lambda (x) (cons (cdr x) (car x)))
772 from-alist)
773 :kind :alist))))))
775 (define-alien-type-method (enum :unparse) (type)
776 `(enum ,(alien-enum-type-name type)
777 ,@(let ((prev -1))
778 (mapcar (lambda (mapping)
779 (let ((sym (car mapping))
780 (value (cdr mapping)))
781 (prog1
782 (if (= (1+ prev) value)
784 `(,sym ,value))
785 (setf prev value))))
786 (alien-enum-type-from type)))))
788 (define-alien-type-method (enum :type=) (type1 type2)
789 (and (eq (alien-enum-type-name type1)
790 (alien-enum-type-name type2))
791 (equal (alien-enum-type-from type1)
792 (alien-enum-type-from type2))))
794 (define-alien-type-method (enum :lisp-rep) (type)
795 `(member ,@(mapcar #'car (alien-enum-type-from type))))
797 (define-alien-type-method (enum :naturalize-gen) (type alien)
798 (ecase (alien-enum-type-kind type)
799 (:vector
800 `(svref ',(alien-enum-type-to type)
801 (+ ,alien ,(alien-enum-type-offset type))))
802 (:alist
803 `(ecase ,alien
804 ,@(mapcar (lambda (mapping)
805 `(,(car mapping) ',(cdr mapping)))
806 (alien-enum-type-to type))))))
808 (define-alien-type-method (enum :deport-gen) (type value)
809 `(ecase ,value
810 ,@(mapcar (lambda (mapping)
811 `(,(car mapping) ,(cdr mapping)))
812 (alien-enum-type-from type))))
814 ;;;; the FLOAT types
816 (define-alien-type-class (float)
817 (type (missing-arg) :type symbol))
819 (define-alien-type-method (float :unparse) (type)
820 (alien-float-type-type type))
822 (define-alien-type-method (float :lisp-rep) (type)
823 (alien-float-type-type type))
825 (define-alien-type-method (float :alien-rep) (type context)
826 (declare (ignore context))
827 (alien-float-type-type type))
829 (define-alien-type-method (float :naturalize-gen) (type alien)
830 (declare (ignore type))
831 alien)
833 (define-alien-type-method (float :deport-gen) (type value)
834 (declare (ignore type))
835 value)
837 (define-alien-type-class (single-float :include (float (bits 32))
838 :include-args (type)))
840 (define-alien-type-translator single-float ()
841 (make-alien-single-float-type :type 'single-float))
843 (define-alien-type-method (single-float :extract-gen) (type sap offset)
844 (declare (ignore type))
845 `(sap-ref-single ,sap (/ ,offset sb!vm:n-byte-bits)))
847 (define-alien-type-class (double-float :include (float (bits 64))
848 :include-args (type)))
850 (define-alien-type-translator double-float ()
851 (make-alien-double-float-type :type 'double-float))
853 (define-alien-type-method (double-float :extract-gen) (type sap offset)
854 (declare (ignore type))
855 `(sap-ref-double ,sap (/ ,offset sb!vm:n-byte-bits)))
858 ;;;; the POINTER type
860 (define-alien-type-class (pointer :include (alien-value (bits
861 #!-alpha
862 sb!vm:n-word-bits
863 #!+alpha 64)))
864 (to nil :type (or alien-type null)))
866 (define-alien-type-translator * (to &environment env)
867 (make-alien-pointer-type :to (if (eq to t) nil (parse-alien-type to env))))
869 (define-alien-type-method (pointer :unparse) (type)
870 (let ((to (alien-pointer-type-to type)))
871 `(* ,(if to
872 (%unparse-alien-type to)
873 t))))
875 (define-alien-type-method (pointer :type=) (type1 type2)
876 (let ((to1 (alien-pointer-type-to type1))
877 (to2 (alien-pointer-type-to type2)))
878 (if to1
879 (if to2
880 (alien-type-= to1 to2)
881 nil)
882 (null to2))))
884 (define-alien-type-method (pointer :subtypep) (type1 type2)
885 (and (alien-pointer-type-p type2)
886 (let ((to1 (alien-pointer-type-to type1))
887 (to2 (alien-pointer-type-to type2)))
888 (if to1
889 (if to2
890 (alien-subtype-p to1 to2)
892 (null to2)))))
894 (define-alien-type-method (pointer :deport-gen) (type value)
895 (/noshow "doing alien type method POINTER :DEPORT-GEN" type value)
896 (values
897 ;; FIXME: old version, highlighted a bug in xc optimization
898 `(etypecase ,value
899 (null
900 (int-sap 0))
901 (system-area-pointer
902 ,value)
903 ((alien ,type)
904 (alien-sap ,value)))
905 ;; new version, works around bug in xc optimization
906 #+nil
907 `(etypecase ,value
908 (system-area-pointer
909 ,value)
910 ((alien ,type)
911 (alien-sap ,value))
912 (null
913 (int-sap 0)))
914 `(or null system-area-pointer (alien ,type))))
916 ;;;; the MEM-BLOCK type
918 (define-alien-type-class (mem-block :include alien-value))
920 (define-alien-type-method (mem-block :extract-gen) (type sap offset)
921 (declare (ignore type))
922 `(sap+ ,sap (truncate ,offset sb!vm:n-byte-bits)))
924 (define-alien-type-method (mem-block :deposit-gen) (type sap offset value)
925 (let ((bits (alien-mem-block-type-bits type)))
926 (unless bits
927 (error "can't deposit aliens of type ~S (unknown size)" type))
928 `(sb!kernel:system-area-ub8-copy ,value 0 ,sap
929 (truncate ,offset sb!vm:n-byte-bits)
930 ',(truncate bits sb!vm:n-byte-bits))))
932 ;;;; the ARRAY type
934 (define-alien-type-class (array :include mem-block)
935 (element-type (missing-arg) :type alien-type)
936 (dimensions (missing-arg) :type list))
938 (define-alien-type-translator array (ele-type &rest dims &environment env)
940 (when dims
941 (unless (typep (first dims) '(or index null))
942 (error "The first dimension is not a non-negative fixnum or NIL: ~S"
943 (first dims)))
944 (let ((loser (find-if-not (lambda (x) (typep x 'index))
945 (rest dims))))
946 (when loser
947 (error "A dimension is not a non-negative fixnum: ~S" loser))))
949 (let ((parsed-ele-type (parse-alien-type ele-type env)))
950 (make-alien-array-type
951 :element-type parsed-ele-type
952 :dimensions dims
953 :alignment (alien-type-alignment parsed-ele-type)
954 :bits (if (and (alien-type-bits parsed-ele-type)
955 (every #'integerp dims))
956 (* (align-offset (alien-type-bits parsed-ele-type)
957 (alien-type-alignment parsed-ele-type))
958 (reduce #'* dims))))))
960 (define-alien-type-method (array :unparse) (type)
961 `(array ,(%unparse-alien-type (alien-array-type-element-type type))
962 ,@(alien-array-type-dimensions type)))
964 (define-alien-type-method (array :type=) (type1 type2)
965 (and (equal (alien-array-type-dimensions type1)
966 (alien-array-type-dimensions type2))
967 (alien-type-= (alien-array-type-element-type type1)
968 (alien-array-type-element-type type2))))
970 (define-alien-type-method (array :subtypep) (type1 type2)
971 (and (alien-array-type-p type2)
972 (let ((dim1 (alien-array-type-dimensions type1))
973 (dim2 (alien-array-type-dimensions type2)))
974 (and (= (length dim1) (length dim2))
975 (or (and dim2
976 (null (car dim2))
977 (equal (cdr dim1) (cdr dim2)))
978 (equal dim1 dim2))
979 (alien-subtype-p (alien-array-type-element-type type1)
980 (alien-array-type-element-type type2))))))
982 ;;;; the RECORD type
984 (def!struct (alien-record-field
985 (:make-load-form-fun sb!kernel:just-dump-it-normally))
986 (name (missing-arg) :type symbol)
987 (type (missing-arg) :type alien-type)
988 (bits nil :type (or unsigned-byte null))
989 (offset 0 :type unsigned-byte))
990 (def!method print-object ((field alien-record-field) stream)
991 (print-unreadable-object (field stream :type t)
992 (format stream
993 "~S ~S~@[:~D~]"
994 (alien-record-field-type field)
995 (alien-record-field-name field)
996 (alien-record-field-bits field))))
998 (define-alien-type-class (record :include mem-block)
999 (kind :struct :type (member :struct :union))
1000 (name nil :type (or symbol null))
1001 (fields nil :type list))
1003 (define-alien-type-translator struct (name &rest fields &environment env)
1004 (parse-alien-record-type :struct name fields env))
1006 (define-alien-type-translator union (name &rest fields &environment env)
1007 (parse-alien-record-type :union name fields env))
1009 ;;; FIXME: This is really pretty horrible: we avoid creating new
1010 ;;; ALIEN-RECORD-TYPE objects when a live one is flitting around the
1011 ;;; system already. This way forward-references sans fields get
1012 ;;; "updated" for free to contain the field info. Maybe rename
1013 ;;; MAKE-ALIEN-RECORD-TYPE to %MAKE-ALIEN-RECORD-TYPE and use
1014 ;;; ENSURE-ALIEN-RECORD-TYPE instead. --NS 20040729
1015 (defun parse-alien-record-type (kind name fields env)
1016 (declare (type sb!kernel:lexenv-designator env))
1017 (flet ((frob-type (type new-fields alignment bits)
1018 (setf (alien-record-type-fields type) new-fields
1019 (alien-record-type-alignment type) alignment
1020 (alien-record-type-bits type) bits)))
1021 (cond (fields
1022 (multiple-value-bind (new-fields alignment bits)
1023 (parse-alien-record-fields kind fields env)
1024 (let* ((old (and name (auxiliary-alien-type kind name env)))
1025 (old-fields (and old (alien-record-type-fields old))))
1026 (when (and old-fields
1027 (notevery #'record-fields-match-p old-fields new-fields))
1028 (cerror "Continue, clobbering the old definition."
1029 "Incompatible alien record type definition~%Old: ~S~%New: ~S"
1030 (unparse-alien-type old)
1031 `(,(unparse-alien-record-kind kind)
1032 ,name
1033 ,@(let ((*record-types-already-unparsed* '()))
1034 (mapcar #'unparse-alien-record-field new-fields))))
1035 (frob-type old new-fields alignment bits))
1036 (if old-fields
1038 (let ((type (or old (make-alien-record-type :name name :kind kind))))
1039 (when (and name (not old))
1040 (setf (auxiliary-alien-type kind name env) type))
1041 (frob-type type new-fields alignment bits)
1042 type)))))
1043 (name
1044 (or (auxiliary-alien-type kind name env)
1045 (setf (auxiliary-alien-type kind name env)
1046 (make-alien-record-type :name name :kind kind))))
1048 (make-alien-record-type :kind kind)))))
1050 ;;; This is used by PARSE-ALIEN-TYPE to parse the fields of struct and union
1051 ;;; types. KIND is the kind we are paring the fields of, and FIELDS is the
1052 ;;; list of field specifications.
1054 ;;; Result is a list of field objects, overall alignment, and number of bits
1055 (defun parse-alien-record-fields (kind fields env)
1056 (declare (type list fields))
1057 (let ((total-bits 0)
1058 (overall-alignment 1)
1059 (parsed-fields nil))
1060 (dolist (field fields)
1061 (destructuring-bind (var type &key alignment bits offset) field
1062 (declare (ignore bits))
1063 (let* ((field-type (parse-alien-type type env))
1064 (bits (alien-type-bits field-type))
1065 (parsed-field
1066 (make-alien-record-field :type field-type
1067 :name var)))
1068 (unless alignment
1069 (setf alignment (alien-type-alignment field-type)))
1070 (push parsed-field parsed-fields)
1071 (when (null bits)
1072 (error "unknown size: ~S" (unparse-alien-type field-type)))
1073 (when (null alignment)
1074 (error "unknown alignment: ~S" (unparse-alien-type field-type)))
1075 (setf overall-alignment (max overall-alignment alignment))
1076 (ecase kind
1077 (:struct
1078 (let ((offset (or offset (align-offset total-bits alignment))))
1079 (setf (alien-record-field-offset parsed-field) offset)
1080 (setf total-bits (+ offset bits))))
1081 (:union
1082 (setf total-bits (max total-bits bits)))))))
1083 (values (nreverse parsed-fields)
1084 overall-alignment
1085 (align-offset total-bits overall-alignment))))
1087 (define-alien-type-method (record :unparse) (type)
1088 `(,(unparse-alien-record-kind (alien-record-type-kind type))
1089 ,(alien-record-type-name type)
1090 ,@(unless (member type *record-types-already-unparsed* :test #'eq)
1091 (push type *record-types-already-unparsed*)
1092 (mapcar #'unparse-alien-record-field
1093 (alien-record-type-fields type)))))
1095 (defun unparse-alien-record-kind (kind)
1096 (case kind
1097 (:struct 'struct)
1098 (:union 'union)
1099 (t '???)))
1101 (defun unparse-alien-record-field (field)
1102 `(,(alien-record-field-name field)
1103 ,(%unparse-alien-type (alien-record-field-type field))
1104 ,@(when (alien-record-field-bits field)
1105 (list :bits (alien-record-field-bits field)))
1106 ,@(when (alien-record-field-offset field)
1107 (list :offset (alien-record-field-offset field)))))
1109 ;;; Test the record fields. Keep a hashtable table of already compared
1110 ;;; types to detect cycles.
1111 (defun record-fields-match-p (field1 field2)
1112 (and (eq (alien-record-field-name field1)
1113 (alien-record-field-name field2))
1114 (eql (alien-record-field-bits field1)
1115 (alien-record-field-bits field2))
1116 (eql (alien-record-field-offset field1)
1117 (alien-record-field-offset field2))
1118 (alien-type-= (alien-record-field-type field1)
1119 (alien-record-field-type field2))))
1121 (defvar *alien-type-matches* nil
1122 #!+sb-doc
1123 "A hashtable used to detect cycles while comparing record types.")
1125 (define-alien-type-method (record :type=) (type1 type2)
1126 (and (eq (alien-record-type-name type1)
1127 (alien-record-type-name type2))
1128 (eq (alien-record-type-kind type1)
1129 (alien-record-type-kind type2))
1130 (eql (alien-type-bits type1)
1131 (alien-type-bits type2))
1132 (eql (alien-type-alignment type1)
1133 (alien-type-alignment type2))
1134 (flet ((match-fields (&optional old)
1135 (setf (gethash type1 *alien-type-matches*) (cons type2 old))
1136 (every #'record-fields-match-p
1137 (alien-record-type-fields type1)
1138 (alien-record-type-fields type2))))
1139 (if *alien-type-matches*
1140 (let ((types (gethash type1 *alien-type-matches*)))
1141 (or (memq type2 types) (match-fields types)))
1142 (let ((*alien-type-matches* (make-hash-table :test #'eq)))
1143 (match-fields))))))
1145 ;;;; the FUNCTION and VALUES alien types
1147 ;;; Calling-convention spec, typically one of predefined keywords.
1148 ;;; Add or remove as needed for target platform. It makes sense to
1149 ;;; support :cdecl everywhere.
1151 ;;; Null convention is supposed to be platform-specific most-universal
1152 ;;; callout convention. For x86, SBCL calls foreign functions in a way
1153 ;;; allowing them to be either stdcall or cdecl; null convention is
1154 ;;; appropriate here, as it is for specifying callbacks that could be
1155 ;;; accepted by foreign code both in cdecl and stdcall form.
1156 (def!type calling-convention () `(or null (member :stdcall :cdecl)))
1158 ;;; Convention could be a values type class, stored at result-type.
1159 ;;; However, it seems appropriate only for epilogue-related
1160 ;;; conventions, those not influencing incoming arg passing.
1162 ;;; As of x86's :stdcall and :cdecl, supported by now, both are
1163 ;;; epilogue-related, but future extensions (like :fastcall and
1164 ;;; miscellaneous non-x86 stuff) might affect incoming argument
1165 ;;; translation as well.
1167 (define-alien-type-class (fun :include mem-block)
1168 (result-type (missing-arg) :type alien-type)
1169 (arg-types (missing-arg) :type list)
1170 (stub nil :type (or null function))
1171 (convention nil :type calling-convention))
1173 ;;; KLUDGE: non-intrusive, backward-compatible way to allow calling
1174 ;;; convention specification for function types is unobvious.
1176 ;;; By now, `RESULT-TYPE' is allowed, but not required, to be a list
1177 ;;; starting with a convention keyword; its second item is a real
1178 ;;; result-type in this case. If convention is ever to become a part
1179 ;;; of result-type, such a syntax can be retained.
1181 (define-alien-type-translator function (result-type &rest arg-types
1182 &environment env)
1183 (multiple-value-bind (bare-result-type calling-convention)
1184 (typecase result-type
1185 ((cons calling-convention *)
1186 (values (second result-type) (first result-type)))
1187 (t result-type))
1188 (make-alien-fun-type
1189 :convention calling-convention
1190 :result-type (let ((*values-type-okay* t))
1191 (parse-alien-type bare-result-type env))
1192 :arg-types (mapcar (lambda (arg-type) (parse-alien-type arg-type env))
1193 arg-types))))
1195 (define-alien-type-method (fun :unparse) (type)
1196 `(function ,(let ((result-type
1197 (%unparse-alien-type (alien-fun-type-result-type type)))
1198 (convention (alien-fun-type-convention type)))
1199 (if convention (list convention result-type)
1200 result-type))
1201 ,@(mapcar #'%unparse-alien-type
1202 (alien-fun-type-arg-types type))))
1204 (define-alien-type-method (fun :type=) (type1 type2)
1205 (and (alien-type-= (alien-fun-type-result-type type1)
1206 (alien-fun-type-result-type type2))
1207 (eq (alien-fun-type-convention type1)
1208 (alien-fun-type-convention type2))
1209 (= (length (alien-fun-type-arg-types type1))
1210 (length (alien-fun-type-arg-types type2)))
1211 (every #'alien-type-=
1212 (alien-fun-type-arg-types type1)
1213 (alien-fun-type-arg-types type2))))
1215 (define-alien-type-class (values)
1216 (values (missing-arg) :type list))
1218 (define-alien-type-translator values (&rest values &environment env)
1219 (unless *values-type-okay*
1220 (error "cannot use values types here"))
1221 (let ((*values-type-okay* nil))
1222 (make-alien-values-type
1223 :values (mapcar (lambda (alien-type) (parse-alien-type alien-type env))
1224 values))))
1226 (define-alien-type-method (values :unparse) (type)
1227 `(values ,@(mapcar #'%unparse-alien-type
1228 (alien-values-type-values type))))
1230 (define-alien-type-method (values :type=) (type1 type2)
1231 (and (= (length (alien-values-type-values type1))
1232 (length (alien-values-type-values type2)))
1233 (every #'alien-type-=
1234 (alien-values-type-values type1)
1235 (alien-values-type-values type2))))
1237 ;;;; a structure definition needed both in the target and in the
1238 ;;;; cross-compilation host
1240 ;;; information about local aliens. The WITH-ALIEN macro builds one of
1241 ;;; these structures and LOCAL-ALIEN and friends communicate
1242 ;;; information about how that local alien is represented.
1243 (def!struct (local-alien-info
1244 (:make-load-form-fun sb!kernel:just-dump-it-normally)
1245 (:constructor make-local-alien-info
1246 (&key type force-to-memory-p
1247 &aux (force-to-memory-p (or force-to-memory-p
1248 (alien-array-type-p type)
1249 (alien-record-type-p type))))))
1250 ;; the type of the local alien
1251 (type (missing-arg) :type alien-type)
1252 ;; Must this local alien be forced into memory? Using the ADDR macro
1253 ;; on a local alien will set this.
1254 (force-to-memory-p nil :type (member t nil)))
1255 (def!method print-object ((info local-alien-info) stream)
1256 (print-unreadable-object (info stream :type t)
1257 (format stream
1258 "~:[~;(forced to stack) ~]~S"
1259 (local-alien-info-force-to-memory-p info)
1260 (unparse-alien-type (local-alien-info-type info)))))
1262 ;;;; the ADDR macro
1264 (defmacro-mundanely addr (expr &environment env)
1265 #!+sb-doc
1266 "Return an Alien pointer to the data addressed by Expr, which must be a call
1267 to SLOT or DEREF, or a reference to an Alien variable."
1268 (let ((form (%macroexpand expr env)))
1269 (or (typecase form
1270 (cons
1271 (case (car form)
1272 (slot
1273 (cons '%slot-addr (cdr form)))
1274 (deref
1275 (cons '%deref-addr (cdr form)))
1276 (%heap-alien
1277 (cons '%heap-alien-addr (cdr form)))
1278 (local-alien
1279 (let ((info (let ((info-arg (second form)))
1280 (and (consp info-arg)
1281 (eq (car info-arg) 'quote)
1282 (second info-arg)))))
1283 (unless (local-alien-info-p info)
1284 (error "Something is wrong, LOCAL-ALIEN-INFO not found: ~S"
1285 form))
1286 (setf (local-alien-info-force-to-memory-p info) t))
1287 (cons '%local-alien-addr (cdr form)))))
1288 (symbol
1289 (let ((kind (info :variable :kind form)))
1290 (when (eq kind :alien)
1291 `(%heap-alien-addr ',(info :variable :alien-info form))))))
1292 (error "~S is not a valid L-value." form))))
1294 (/show0 "host-alieneval.lisp end of file")