Remove DEFMACRO-MUNDANELY.
[sbcl.git] / src / code / host-alieneval.lisp
blob24282ddae6d63386eefb0cb27e2d23ea27f1a658
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 (info :alien-type kind name)))))
211 (defun (setf auxiliary-alien-type) (new-value kind name env)
212 (declare (type sb!kernel:lexenv-designator env))
213 (flet ((aux-defn-matches (x)
214 (and (eq (first x) kind) (eq (second x) name))))
215 (when (find-if #'aux-defn-matches *new-auxiliary-types*)
216 (error "attempt to multiply define ~A ~S" kind name))
217 (when (find-if #'aux-defn-matches (auxiliary-type-definitions env))
218 (error "attempt to shadow definition of ~A ~S" kind name)))
219 (push (list kind name new-value) *new-auxiliary-types*)
220 new-value)
222 (defun verify-local-auxiliaries-okay ()
223 (dolist (info *new-auxiliary-types*)
224 (destructuring-bind (kind name defn) info
225 (declare (ignore defn))
226 (when (info :alien-type kind name)
227 (error "attempt to shadow definition of ~A ~S" kind name)))))
229 (defun unparse-alien-type (type)
230 #!+sb-doc
231 "Convert the alien-type structure TYPE back into a list specification of
232 the type."
233 (declare (type alien-type type))
234 (let ((*record-types-already-unparsed* nil))
235 (%unparse-alien-type type)))
237 ;;; Does all the work of UNPARSE-ALIEN-TYPE. It's separate because we
238 ;;; need to recurse inside the binding of
239 ;;; *RECORD-TYPES-ALREADY-UNPARSED*.
240 (defun %unparse-alien-type (type)
241 (invoke-alien-type-method :unparse type))
243 ;;;; alien type defining stuff
245 (def!macro define-alien-type-translator (name lambda-list &body body)
246 (let ((defun-name (symbolicate "ALIEN-" name "-TYPE-TRANSLATOR")))
247 `(eval-when (:compile-toplevel :load-toplevel :execute)
248 (setf (symbol-function ',defun-name)
249 ,(make-macro-lambda defun-name lambda-list body
250 'define-alien-type-translator name))
251 (%define-alien-type-translator ',name #',defun-name))))
253 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
254 (defun %define-alien-type-translator (name translator)
255 (setf (info :alien-type :kind name) :primitive)
256 (setf (info :alien-type :translator name) translator)
257 (clear-info :alien-type :definition name)
258 name))
260 (def!macro define-alien-type (name type &environment env)
261 #!+sb-doc
262 "Define the alien type NAME to be equivalent to TYPE. Name may be NIL for
263 STRUCT and UNION types, in which case the name is taken from the type
264 specifier."
265 (with-auxiliary-alien-types env
266 (let ((alien-type (parse-alien-type type env)))
267 `(eval-when (:compile-toplevel :load-toplevel :execute)
268 ,@(when *new-auxiliary-types*
269 `((%def-auxiliary-alien-types ',*new-auxiliary-types*
270 (sb!c:source-location))))
271 ,@(when name
272 `((%define-alien-type ',name ',alien-type)
273 (setf (info :source-location :alien-type ',name)
274 (sb!c:source-location))))))))
276 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
277 (defun %def-auxiliary-alien-types (types source-location)
278 (dolist (info types)
279 ;; Clear up the type we're about to define from the toplevel
280 ;; *new-auxiliary-types* (local scopes take care of themselves).
281 ;; Unless this is done we never actually get back the full type
282 ;; from INFO, since the *new-auxiliary-types* have precendence.
283 (setf *new-auxiliary-types*
284 (remove info *new-auxiliary-types*
285 :test (lambda (a b)
286 (and (eq (first a) (first b))
287 (eq (second a) (second b))))))
288 (destructuring-bind (kind name defn) info
289 (let ((old (info :alien-type kind name)))
290 (unless (or (null old) (alien-type-= old defn))
291 (warn "redefining ~A ~S to be:~% ~S,~%was:~% ~S"
292 kind name defn old)))
293 (setf (info :alien-type kind name) defn
294 (info :source-location :alien-type name) source-location))))
296 (defun %define-alien-type (name new)
297 (ecase (info :alien-type :kind name)
298 (:primitive
299 (error "~S is a built-in alien type." name))
300 (:defined
301 (let ((old (info :alien-type :definition name)))
302 (unless (or (null old) (alien-type-= new old))
303 (warn "redefining ~S to be:~% ~S,~%was~% ~S"
304 name
305 (unparse-alien-type new)
306 (unparse-alien-type old)))))
307 (:unknown))
308 (setf (info :alien-type :definition name) new)
309 (setf (info :alien-type :kind name) :defined)
310 name))
312 ;;;; the root alien type
314 (eval-when (:compile-toplevel :load-toplevel :execute)
315 (create-alien-type-class-if-necessary 'root 'alien-type nil))
317 (def!method print-object ((type alien-type) stream)
318 (print-unreadable-object (type stream :type t)
319 ;; Kludge to avoid printing #'(SIGNED 64) instead of (FUNCTION (SIGNED 64))
320 ;; for a 0-argument function. This is only a problem with alien types
321 ;; because ordinary FUNCTION type specifiers are 3-lists.
322 (let ((sb!pretty:*pprint-quote-with-syntactic-sugar* nil))
323 ;; forward-reference of this special variable unfortunately
324 (declare (special sb!pretty:*pprint-quote-with-syntactic-sugar*))
325 (prin1 (unparse-alien-type type) stream))))
327 ;;;; the SAP type
329 (define-alien-type-class (system-area-pointer))
331 (define-alien-type-translator system-area-pointer ()
332 (make-alien-system-area-pointer-type
333 :bits sb!vm:n-machine-word-bits))
335 (define-alien-type-method (system-area-pointer :unparse) (type)
336 (declare (ignore type))
337 'system-area-pointer)
339 (define-alien-type-method (system-area-pointer :lisp-rep) (type)
340 (declare (ignore type))
341 'system-area-pointer)
343 (define-alien-type-method (system-area-pointer :alien-rep) (type context)
344 (declare (ignore type context))
345 'system-area-pointer)
347 (define-alien-type-method (system-area-pointer :naturalize-gen) (type alien)
348 (declare (ignore type))
349 alien)
351 (define-alien-type-method (system-area-pointer :deport-gen) (type object)
352 (declare (ignore type))
353 (/noshow "doing alien type method SYSTEM-AREA-POINTER :DEPORT-GEN" object)
354 object)
356 (define-alien-type-method (system-area-pointer :extract-gen) (type sap offset)
357 (declare (ignore type))
358 `(sap-ref-sap ,sap (/ ,offset sb!vm:n-byte-bits)))
360 ;;;; the ALIEN-VALUE type
362 (define-alien-type-class (alien-value :include system-area-pointer))
364 (define-alien-type-method (alien-value :lisp-rep) (type)
365 (declare (ignore type))
366 nil)
368 (define-alien-type-method (alien-value :naturalize-gen) (type alien)
369 `(%sap-alien ,alien ',type))
371 (define-alien-type-method (alien-value :deport-gen) (type value)
372 (declare (ignore type))
373 (/noshow "doing alien type method ALIEN-VALUE :DEPORT-GEN" value)
374 `(alien-sap ,value))
376 ;;; HEAP-ALIEN-INFO -- defstruct.
378 (def!method print-object ((info heap-alien-info) stream)
379 (print-unreadable-object (info stream :type t)
380 (funcall (formatter "~S ~S~@[ (data)~]")
381 stream
382 (heap-alien-info-alien-name info)
383 (unparse-alien-type (heap-alien-info-type info))
384 (heap-alien-info-datap info))))
386 ;;; The form to evaluate to produce the SAP pointing to where in the heap
387 ;;; it is.
388 (defun heap-alien-info-sap-form (info)
389 `(foreign-symbol-sap ,(heap-alien-info-alien-name info)
390 ,(heap-alien-info-datap info)))
392 #-sb-xc-host ; No FOREIGN-SYMBOL-SAP
393 (defun heap-alien-info-sap (info)
394 (foreign-symbol-sap (heap-alien-info-alien-name info)
395 (heap-alien-info-datap info)))
397 ;;;; Interfaces to the different methods
399 (defun alien-type-= (type1 type2)
400 #!+sb-doc
401 "Return T iff TYPE1 and TYPE2 describe equivalent alien types."
402 (or (eq type1 type2)
403 (and (eq (alien-type-class type1)
404 (alien-type-class type2))
405 (invoke-alien-type-method :type= type1 type2))))
407 (defun alien-subtype-p (type1 type2)
408 #!+sb-doc
409 "Return T iff the alien type TYPE1 is a subtype of TYPE2. Currently, the
410 only supported subtype relationships are is that any pointer type is a
411 subtype of (* t), and any array type first dimension will match
412 (array <eltype> nil ...). Otherwise, the two types have to be
413 ALIEN-TYPE-=."
414 (or (eq type1 type2)
415 (invoke-alien-type-method :subtypep type1 type2)))
417 (defun compute-naturalize-lambda (type)
418 `(lambda (alien ignore)
419 (declare (ignore ignore))
420 ,(invoke-alien-type-method :naturalize-gen type 'alien)))
422 (defun compute-deport-lambda (type)
423 (declare (type alien-type type))
424 (/noshow "entering COMPUTE-DEPORT-LAMBDA" type)
425 (multiple-value-bind (form value-type)
426 (invoke-alien-type-method :deport-gen type 'value)
427 `(lambda (value ignore)
428 (declare (type ,(or value-type
429 (compute-lisp-rep-type type)
430 `(alien ,type))
431 value)
432 (ignore ignore))
433 ,form)))
435 (defun compute-deport-alloc-lambda (type)
436 `(lambda (value ignore)
437 (declare (ignore ignore))
438 ,(invoke-alien-type-method :deport-alloc-gen type 'value)))
440 (defun compute-extract-lambda (type)
441 `(lambda (sap offset ignore)
442 (declare (type system-area-pointer sap)
443 (type unsigned-byte offset)
444 (ignore ignore))
445 (naturalize ,(invoke-alien-type-method :extract-gen type 'sap 'offset)
446 ',type)))
448 (def!macro maybe-with-pinned-objects (variables types &body body)
449 (declare (ignorable variables types))
450 (let ((pin-variables
451 ;; Only pin things on GENCGC, since on CHENEYGC it'd imply
452 ;; disabling the GC. Which is something we don't want to do
453 ;; every time we're calling to C.
454 #!+gencgc
455 (loop for variable in variables
456 for type in types
457 when (invoke-alien-type-method :deport-pin-p type)
458 collect variable)))
459 (if pin-variables
460 `(with-pinned-objects ,pin-variables
461 ,@body)
462 `(progn
463 ,@body))))
465 (defun compute-deposit-lambda (type)
466 (declare (type alien-type type))
467 `(lambda (value sap offset ignore)
468 (declare (type system-area-pointer sap)
469 (type unsigned-byte offset)
470 (ignore ignore))
471 (let ((alloc-tmp (deport-alloc value ',type)))
472 (maybe-with-pinned-objects (alloc-tmp) (,type)
473 (let ((value (deport alloc-tmp ',type)))
474 ,(invoke-alien-type-method :deposit-gen type 'sap 'offset 'value)
475 ;; Note: the reason we don't just return the pre-deported value
476 ;; is because that would inhibit any (deport (naturalize ...))
477 ;; optimizations that might have otherwise happen. Re-naturalizing
478 ;; the value might cause extra consing, but is flushable, so probably
479 ;; results in better code.
480 (naturalize value ',type))))))
482 (defun compute-lisp-rep-type (type)
483 (invoke-alien-type-method :lisp-rep type))
485 ;;; CONTEXT is either :NORMAL (the default) or :RESULT (alien function
486 ;;; return values). See the :ALIEN-REP method for INTEGER for
487 ;;; details.
488 (defun compute-alien-rep-type (type &optional (context :normal))
489 (invoke-alien-type-method :alien-rep type context))
491 ;;;; default methods
493 (define-alien-type-method (root :unparse) (type)
494 `(<unknown-alien-type> ,(type-of type)))
496 (define-alien-type-method (root :type=) (type1 type2)
497 (declare (ignore type1 type2))
500 (define-alien-type-method (root :subtypep) (type1 type2)
501 (alien-type-= type1 type2))
503 (define-alien-type-method (root :lisp-rep) (type)
504 (declare (ignore type))
505 nil)
507 (define-alien-type-method (root :alien-rep) (type context)
508 (declare (ignore type context))
511 (define-alien-type-method (root :naturalize-gen) (type alien)
512 (declare (ignore alien))
513 (error "cannot represent ~S typed aliens" type))
515 (define-alien-type-method (root :deport-gen) (type object)
516 (declare (ignore object))
517 (error "cannot represent ~S typed aliens" type))
519 (define-alien-type-method (root :deport-alloc-gen) (type object)
520 (declare (ignore type))
521 object)
523 (define-alien-type-method (root :deport-pin-p) (type)
524 (declare (ignore type))
525 ;; Override this method to return T for classes which take a SAP to a
526 ;; GCable lisp object when deporting.
527 nil)
529 (define-alien-type-method (root :extract-gen) (type sap offset)
530 (declare (ignore sap offset))
531 (error "cannot represent ~S typed aliens" type))
533 (define-alien-type-method (root :deposit-gen) (type sap offset value)
534 `(setf ,(invoke-alien-type-method :extract-gen type sap offset) ,value))
536 (define-alien-type-method (root :arg-tn) (type state)
537 (declare (ignore state))
538 (error "Aliens of type ~S cannot be passed as arguments to CALL-OUT."
539 (unparse-alien-type type)))
541 (define-alien-type-method (root :result-tn) (type state)
542 (declare (ignore state))
543 (error "Aliens of type ~S cannot be returned from CALL-OUT."
544 (unparse-alien-type type)))
546 ;;;; the INTEGER type
548 (define-alien-type-class (integer)
549 (signed t :type (member t nil)))
551 (define-alien-type-translator signed (&optional (bits sb!vm:n-word-bits))
552 (make-alien-integer-type :bits bits))
554 (define-alien-type-translator integer (&optional (bits sb!vm:n-word-bits))
555 (make-alien-integer-type :bits bits))
557 (define-alien-type-translator unsigned (&optional (bits sb!vm:n-word-bits))
558 (make-alien-integer-type :bits bits :signed nil))
560 (define-alien-type-method (integer :unparse) (type)
561 (list (if (alien-integer-type-signed type) 'signed 'unsigned)
562 (alien-integer-type-bits type)))
564 (define-alien-type-method (integer :type=) (type1 type2)
565 (and (eq (alien-integer-type-signed type1)
566 (alien-integer-type-signed type2))
567 (= (alien-integer-type-bits type1)
568 (alien-integer-type-bits type2))))
570 (define-alien-type-method (integer :lisp-rep) (type)
571 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
572 (alien-integer-type-bits type)))
574 (define-alien-type-method (integer :alien-rep) (type context)
575 ;; When returning integer values that are narrower than a machine
576 ;; register from a function, some platforms leave the higher bits of
577 ;; the register uninitialized. On those platforms, we use an
578 ;; alien-rep of the full register width when checking for purposes
579 ;; of return values and override the naturalize method to perform
580 ;; the sign extension (in compiler/target/c-call.lisp).
581 (ecase context
582 ((:normal #!-(or alpha x86 x86-64) :result)
583 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
584 (alien-integer-type-bits type)))
585 #!+(or alpha x86 x86-64)
586 (:result
587 (list (if (alien-integer-type-signed type) 'signed-byte 'unsigned-byte)
588 (max (alien-integer-type-bits type)
589 sb!vm:n-machine-word-bits)))))
591 ;;; As per the comment in the :ALIEN-REP method above, this is defined
592 ;;; elsewhere for alpha and x86oids.
593 #!-(or alpha x86 x86-64)
594 (define-alien-type-method (integer :naturalize-gen) (type alien)
595 (declare (ignore type))
596 alien)
598 (define-alien-type-method (integer :deport-gen) (type value)
599 (declare (ignore type))
600 value)
602 (define-alien-type-method (integer :extract-gen) (type sap offset)
603 (declare (type alien-integer-type type))
604 (let ((ref-fun
605 (if (alien-integer-type-signed type)
606 (case (alien-integer-type-bits type)
607 (8 'signed-sap-ref-8)
608 (16 'signed-sap-ref-16)
609 (32 'signed-sap-ref-32)
610 (64 'signed-sap-ref-64))
611 (case (alien-integer-type-bits type)
612 (8 'sap-ref-8)
613 (16 'sap-ref-16)
614 (32 'sap-ref-32)
615 (64 'sap-ref-64)))))
616 (if ref-fun
617 `(,ref-fun ,sap (/ ,offset sb!vm:n-byte-bits))
618 (error "cannot extract ~W-bit integers"
619 (alien-integer-type-bits type)))))
621 ;;;; the BOOLEAN type
623 (define-alien-type-class (boolean :include integer :include-args (signed)))
625 ;;; FIXME: Check to make sure that we aren't attaching user-readable
626 ;;; stuff to CL:BOOLEAN in any way which impairs ANSI compliance.
627 (define-alien-type-translator boolean (&optional (bits sb!vm:n-word-bits))
628 (make-alien-boolean-type :bits bits :signed nil))
630 (define-alien-type-method (boolean :unparse) (type)
631 `(boolean ,(alien-boolean-type-bits type)))
633 (define-alien-type-method (boolean :lisp-rep) (type)
634 (declare (ignore type))
635 `(member t nil))
637 (define-alien-type-method (boolean :naturalize-gen) (type alien)
638 (let ((bits (alien-boolean-type-bits type)))
639 (if (= bits sb!vm:n-word-bits)
640 `(not (zerop ,alien))
641 `(logtest ,alien ,(ldb (byte bits 0) -1)))))
643 (define-alien-type-method (boolean :deport-gen) (type value)
644 (declare (ignore type))
645 `(if ,value 1 0))
647 ;;;; the ENUM type
649 (define-alien-type-class (enum :include (integer (bits 32))
650 :include-args (signed))
651 name ; name of this enum (if any)
652 from ; alist from symbols to integers
653 to ; alist or vector from integers to symbols
654 kind ; kind of from mapping, :VECTOR or :ALIST
655 offset) ; offset to add to value for :VECTOR from mapping
657 (define-alien-type-translator enum (&whole
658 type name
659 &rest mappings
660 &environment env)
661 (cond (mappings
662 (let ((result (parse-enum name mappings)))
663 (when name
664 (multiple-value-bind (old old-p)
665 (auxiliary-alien-type :enum name env)
666 (when old-p
667 (unless (alien-type-= result old)
668 (cerror "Continue, clobbering the old definition"
669 "Incompatible alien enum type definition: ~S" name)
670 (setf (alien-enum-type-from old) (alien-enum-type-from result)
671 (alien-enum-type-to old) (alien-enum-type-to result)
672 (alien-enum-type-kind old) (alien-enum-type-kind result)
673 (alien-enum-type-offset old) (alien-enum-type-offset result)
674 (alien-enum-type-signed old) (alien-enum-type-signed result)))
675 (setf result old))
676 (unless old-p
677 (setf (auxiliary-alien-type :enum name env) result))))
678 result))
679 (name
680 (multiple-value-bind (result found)
681 (auxiliary-alien-type :enum name env)
682 (unless found
683 (error "unknown enum type: ~S" name))
684 result))
686 (error "empty enum type: ~S" type))))
688 (defun parse-enum (name elements)
689 (when (null elements)
690 (error "An enumeration must contain at least one element."))
691 (let ((min nil)
692 (max nil)
693 (from-alist ())
694 (prev -1))
695 (declare (list from-alist))
696 (dolist (el elements)
697 (multiple-value-bind (sym val)
698 (if (listp el)
699 (values (first el) (second el))
700 (values el (1+ prev)))
701 (setf prev val)
702 (unless (symbolp sym)
703 (error "The enumeration element ~S is not a symbol." sym))
704 (unless (integerp val)
705 (error "The element value ~S is not an integer." val))
706 (unless (and max (> max val)) (setq max val))
707 (unless (and min (< min val)) (setq min val))
708 (when (rassoc val from-alist)
709 (style-warn "The element value ~S is used more than once." val))
710 (when (assoc sym from-alist :test #'eq)
711 (error "The enumeration element ~S is used more than once." sym))
712 (push (cons sym val) from-alist)))
713 (let* ((signed (minusp min))
714 (min-bits (if signed
715 (1+ (max (integer-length min)
716 (integer-length max)))
717 (integer-length max))))
718 (when (> min-bits 32)
719 (error "can't represent enums needing more than 32 bits"))
720 (setf from-alist (sort from-alist #'< :key #'cdr))
721 (cond
722 ;; If range is at least 20% dense, use vector mapping. Crossover
723 ;; point solely on basis of space would be 25%. Vector mapping
724 ;; is always faster, so give the benefit of the doubt.
725 ((< 0.2 (/ (float (length from-alist)) (float (1+ (- max min)))))
726 ;; If offset is small and ignorable, ignore it to save time.
727 (when (< 0 min 10) (setq min 0))
728 (let ((to (make-array (1+ (- max min)))))
729 (dolist (el from-alist)
730 (setf (svref to (- (cdr el) min)) (car el)))
731 (make-alien-enum-type :name name :signed signed
732 :from from-alist :to to :kind
733 :vector :offset (- min))))
735 (make-alien-enum-type :name name :signed signed
736 :from from-alist
737 :to (mapcar (lambda (x) (cons (cdr x) (car x)))
738 from-alist)
739 :kind :alist))))))
741 (define-alien-type-method (enum :unparse) (type)
742 `(enum ,(alien-enum-type-name type)
743 ,@(let ((prev -1))
744 (mapcar (lambda (mapping)
745 (let ((sym (car mapping))
746 (value (cdr mapping)))
747 (prog1
748 (if (= (1+ prev) value)
750 `(,sym ,value))
751 (setf prev value))))
752 (alien-enum-type-from type)))))
754 (define-alien-type-method (enum :type=) (type1 type2)
755 (and (eq (alien-enum-type-name type1)
756 (alien-enum-type-name type2))
757 (equal (alien-enum-type-from type1)
758 (alien-enum-type-from type2))))
760 (define-alien-type-method (enum :lisp-rep) (type)
761 `(member ,@(mapcar #'car (alien-enum-type-from type))))
763 (define-alien-type-method (enum :naturalize-gen) (type alien)
764 (ecase (alien-enum-type-kind type)
765 (:vector
766 `(svref ',(alien-enum-type-to type)
767 (+ ,alien ,(alien-enum-type-offset type))))
768 (:alist
769 `(ecase ,alien
770 ,@(mapcar (lambda (mapping)
771 `(,(car mapping) ',(cdr mapping)))
772 (alien-enum-type-to type))))))
774 (define-alien-type-method (enum :deport-gen) (type value)
775 `(ecase ,value
776 ,@(mapcar (lambda (mapping)
777 `(,(car mapping) ,(cdr mapping)))
778 (alien-enum-type-from type))))
780 ;;;; the FLOAT types
782 (define-alien-type-class (float)
783 (type (missing-arg) :type symbol))
785 (define-alien-type-method (float :unparse) (type)
786 (alien-float-type-type type))
788 (define-alien-type-method (float :lisp-rep) (type)
789 (alien-float-type-type type))
791 (define-alien-type-method (float :alien-rep) (type context)
792 (declare (ignore context))
793 (alien-float-type-type type))
795 (define-alien-type-method (float :naturalize-gen) (type alien)
796 (declare (ignore type))
797 alien)
799 (define-alien-type-method (float :deport-gen) (type value)
800 (declare (ignore type))
801 value)
803 (define-alien-type-class (single-float :include (float (bits 32))
804 :include-args (type)))
806 (define-alien-type-translator single-float ()
807 (make-alien-single-float-type :type 'single-float))
809 (define-alien-type-method (single-float :extract-gen) (type sap offset)
810 (declare (ignore type))
811 `(sap-ref-single ,sap (/ ,offset sb!vm:n-byte-bits)))
813 (define-alien-type-class (double-float :include (float (bits 64))
814 :include-args (type)))
816 (define-alien-type-translator double-float ()
817 (make-alien-double-float-type :type 'double-float))
819 (define-alien-type-method (double-float :extract-gen) (type sap offset)
820 (declare (ignore type))
821 `(sap-ref-double ,sap (/ ,offset sb!vm:n-byte-bits)))
824 ;;;; the POINTER type
826 (define-alien-type-class (pointer :include (alien-value (bits
827 sb!vm:n-machine-word-bits)))
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 forward-references sans fields 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 sb!kernel:lexenv-designator 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 ,@(let ((*record-types-already-unparsed* '()))
998 (mapcar #'unparse-alien-record-field new-fields))))
999 (frob-type old new-fields alignment bits))
1000 (if old-fields
1002 (let ((type (or old (make-alien-record-type :name name :kind kind))))
1003 (when (and name (not old))
1004 (setf (auxiliary-alien-type kind name env) type))
1005 (frob-type type new-fields alignment bits)
1006 type)))))
1007 (name
1008 (or (auxiliary-alien-type kind name env)
1009 (setf (auxiliary-alien-type kind name env)
1010 (make-alien-record-type :name name :kind kind))))
1012 (make-alien-record-type :kind kind)))))
1014 ;;; This is used by PARSE-ALIEN-TYPE to parse the fields of struct and union
1015 ;;; types. KIND is the kind we are paring the fields of, and FIELDS is the
1016 ;;; list of field specifications.
1018 ;;; Result is a list of field objects, overall alignment, and number of bits
1019 (defun parse-alien-record-fields (kind fields env)
1020 (declare (type list fields))
1021 (let ((total-bits 0)
1022 (overall-alignment 1)
1023 (parsed-fields nil))
1024 (dolist (field fields)
1025 (destructuring-bind (var type &key alignment bits offset) field
1026 (declare (ignore bits))
1027 (let* ((field-type (parse-alien-type type env))
1028 (bits (alien-type-bits field-type))
1029 (parsed-field
1030 (make-alien-record-field :type field-type
1031 :name var)))
1032 (unless alignment
1033 (setf alignment (alien-type-alignment field-type)))
1034 (push parsed-field parsed-fields)
1035 (when (null bits)
1036 (error "unknown size: ~S" (unparse-alien-type field-type)))
1037 (when (null alignment)
1038 (error "unknown alignment: ~S" (unparse-alien-type field-type)))
1039 (setf overall-alignment (max overall-alignment alignment))
1040 (ecase kind
1041 (:struct
1042 (let ((offset (or offset (align-offset total-bits alignment))))
1043 (setf (alien-record-field-offset parsed-field) offset)
1044 (setf total-bits (+ offset bits))))
1045 (:union
1046 (setf total-bits (max total-bits bits)))))))
1047 (values (nreverse parsed-fields)
1048 overall-alignment
1049 (align-offset total-bits overall-alignment))))
1051 (define-alien-type-method (record :unparse) (type)
1052 `(,(unparse-alien-record-kind (alien-record-type-kind type))
1053 ,(alien-record-type-name type)
1054 ,@(unless (member type *record-types-already-unparsed* :test #'eq)
1055 (push type *record-types-already-unparsed*)
1056 (mapcar #'unparse-alien-record-field
1057 (alien-record-type-fields type)))))
1059 (defun unparse-alien-record-kind (kind)
1060 (case kind
1061 (:struct 'struct)
1062 (:union 'union)
1063 (t '???)))
1065 (defun unparse-alien-record-field (field)
1066 `(,(alien-record-field-name field)
1067 ,(%unparse-alien-type (alien-record-field-type field))
1068 ,@(when (alien-record-field-bits field)
1069 (list :bits (alien-record-field-bits field)))
1070 ,@(when (alien-record-field-offset field)
1071 (list :offset (alien-record-field-offset field)))))
1073 ;;; Test the record fields. Keep a hashtable table of already compared
1074 ;;; types to detect cycles.
1075 (defun record-fields-match-p (field1 field2)
1076 (and (eq (alien-record-field-name field1)
1077 (alien-record-field-name field2))
1078 (eql (alien-record-field-bits field1)
1079 (alien-record-field-bits field2))
1080 (eql (alien-record-field-offset field1)
1081 (alien-record-field-offset field2))
1082 (alien-type-= (alien-record-field-type field1)
1083 (alien-record-field-type field2))))
1085 (defvar *alien-type-matches* nil
1086 #!+sb-doc
1087 "A hashtable used to detect cycles while comparing record types.")
1089 (define-alien-type-method (record :type=) (type1 type2)
1090 (and (eq (alien-record-type-name type1)
1091 (alien-record-type-name type2))
1092 (eq (alien-record-type-kind type1)
1093 (alien-record-type-kind type2))
1094 (eql (alien-type-bits type1)
1095 (alien-type-bits type2))
1096 (eql (alien-type-alignment type1)
1097 (alien-type-alignment type2))
1098 (flet ((match-fields (&optional old)
1099 (setf (gethash type1 *alien-type-matches*) (cons type2 old))
1100 (every #'record-fields-match-p
1101 (alien-record-type-fields type1)
1102 (alien-record-type-fields type2))))
1103 (if *alien-type-matches*
1104 (let ((types (gethash type1 *alien-type-matches*)))
1105 (or (memq type2 types) (match-fields types)))
1106 (let ((*alien-type-matches* (make-hash-table :test #'eq)))
1107 (match-fields))))))
1109 ;;;; the FUNCTION and VALUES alien types
1111 ;;; Calling-convention spec, typically one of predefined keywords.
1112 ;;; Add or remove as needed for target platform. It makes sense to
1113 ;;; support :cdecl everywhere.
1115 ;;; Null convention is supposed to be platform-specific most-universal
1116 ;;; callout convention. For x86, SBCL calls foreign functions in a way
1117 ;;; allowing them to be either stdcall or cdecl; null convention is
1118 ;;; appropriate here, as it is for specifying callbacks that could be
1119 ;;; accepted by foreign code both in cdecl and stdcall form.
1120 (def!type calling-convention () `(or null (member :stdcall :cdecl)))
1122 ;;; Convention could be a values type class, stored at result-type.
1123 ;;; However, it seems appropriate only for epilogue-related
1124 ;;; conventions, those not influencing incoming arg passing.
1126 ;;; As of x86's :stdcall and :cdecl, supported by now, both are
1127 ;;; epilogue-related, but future extensions (like :fastcall and
1128 ;;; miscellaneous non-x86 stuff) might affect incoming argument
1129 ;;; translation as well.
1131 (define-alien-type-class (fun :include mem-block)
1132 (result-type (missing-arg) :type alien-type)
1133 (arg-types (missing-arg) :type list)
1134 (stub nil :type (or null function))
1135 (convention nil :type calling-convention))
1137 ;;; KLUDGE: non-intrusive, backward-compatible way to allow calling
1138 ;;; convention specification for function types is unobvious.
1140 ;;; By now, `RESULT-TYPE' is allowed, but not required, to be a list
1141 ;;; starting with a convention keyword; its second item is a real
1142 ;;; result-type in this case. If convention is ever to become a part
1143 ;;; of result-type, such a syntax can be retained.
1145 (define-alien-type-translator function (result-type &rest arg-types
1146 &environment env)
1147 (multiple-value-bind (bare-result-type calling-convention)
1148 (typecase result-type
1149 ((cons calling-convention *)
1150 (values (second result-type) (first result-type)))
1151 (t result-type))
1152 (make-alien-fun-type
1153 :convention calling-convention
1154 :result-type (let ((*values-type-okay* t))
1155 (parse-alien-type bare-result-type env))
1156 :arg-types (mapcar (lambda (arg-type) (parse-alien-type arg-type env))
1157 arg-types))))
1159 (define-alien-type-method (fun :unparse) (type)
1160 `(function ,(let ((result-type
1161 (%unparse-alien-type (alien-fun-type-result-type type)))
1162 (convention (alien-fun-type-convention type)))
1163 (if convention (list convention result-type)
1164 result-type))
1165 ,@(mapcar #'%unparse-alien-type
1166 (alien-fun-type-arg-types type))))
1168 (define-alien-type-method (fun :type=) (type1 type2)
1169 (and (alien-type-= (alien-fun-type-result-type type1)
1170 (alien-fun-type-result-type type2))
1171 (eq (alien-fun-type-convention type1)
1172 (alien-fun-type-convention type2))
1173 (= (length (alien-fun-type-arg-types type1))
1174 (length (alien-fun-type-arg-types type2)))
1175 (every #'alien-type-=
1176 (alien-fun-type-arg-types type1)
1177 (alien-fun-type-arg-types type2))))
1179 (define-alien-type-class (values)
1180 (values (missing-arg) :type list))
1182 (define-alien-type-translator values (&rest values &environment env)
1183 (unless *values-type-okay*
1184 (error "cannot use values types here"))
1185 (let ((*values-type-okay* nil))
1186 (make-alien-values-type
1187 :values (mapcar (lambda (alien-type) (parse-alien-type alien-type env))
1188 values))))
1190 (define-alien-type-method (values :unparse) (type)
1191 `(values ,@(mapcar #'%unparse-alien-type
1192 (alien-values-type-values type))))
1194 (define-alien-type-method (values :type=) (type1 type2)
1195 (and (= (length (alien-values-type-values type1))
1196 (length (alien-values-type-values type2)))
1197 (every #'alien-type-=
1198 (alien-values-type-values type1)
1199 (alien-values-type-values type2))))
1201 ;;;; a structure definition needed both in the target and in the
1202 ;;;; cross-compilation host
1204 ;;; information about local aliens. The WITH-ALIEN macro builds one of
1205 ;;; these structures and LOCAL-ALIEN and friends communicate
1206 ;;; information about how that local alien is represented.
1207 (def!struct (local-alien-info
1208 (:make-load-form-fun sb!kernel:just-dump-it-normally)
1209 (:constructor make-local-alien-info
1210 (&key type force-to-memory-p
1211 &aux (force-to-memory-p (or force-to-memory-p
1212 (alien-array-type-p type)
1213 (alien-record-type-p type))))))
1214 ;; the type of the local alien
1215 (type (missing-arg) :type alien-type)
1216 ;; Must this local alien be forced into memory? Using the ADDR macro
1217 ;; on a local alien will set this.
1218 (force-to-memory-p nil :type (member t nil)))
1219 (def!method print-object ((info local-alien-info) stream)
1220 (print-unreadable-object (info stream :type t)
1221 (format stream
1222 "~:[~;(forced to stack) ~]~S"
1223 (local-alien-info-force-to-memory-p info)
1224 (unparse-alien-type (local-alien-info-type info)))))
1226 ;;;; the ADDR macro
1228 (sb!xc:defmacro addr (expr &environment env)
1229 #!+sb-doc
1230 "Return an Alien pointer to the data addressed by Expr, which must be a call
1231 to SLOT or DEREF, or a reference to an Alien variable."
1232 (let ((form (%macroexpand expr env)))
1233 (or (typecase form
1234 (cons
1235 (case (car form)
1236 (slot
1237 (cons '%slot-addr (cdr form)))
1238 (deref
1239 (cons '%deref-addr (cdr form)))
1240 (%heap-alien
1241 (cons '%heap-alien-addr (cdr form)))
1242 (local-alien
1243 (let ((info (let ((info-arg (second form)))
1244 (and (consp info-arg)
1245 (eq (car info-arg) 'quote)
1246 (second info-arg)))))
1247 (unless (local-alien-info-p info)
1248 (error "Something is wrong, LOCAL-ALIEN-INFO not found: ~S"
1249 form))
1250 (setf (local-alien-info-force-to-memory-p info) t))
1251 (cons '%local-alien-addr (cdr form)))))
1252 (symbol
1253 (let ((kind (info :variable :kind form)))
1254 (when (eq kind :alien)
1255 `(%heap-alien-addr ',(info :variable :alien-info form))))))
1256 (error "~S is not a valid L-value." form))))
1258 (/show0 "host-alieneval.lisp end of file")