2ef9f244f1d120e11d9b0483ef230cf778f2fae5
[sbcl.git] / src / code / cross-sap.lisp
1 ;;;; support and placeholders for System Area Pointers (SAPs) in the host
2 ;;;; Common Lisp at cross-compile time
3
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.
12
13 (in-package "SB!SYS")
14
15 ;;; SYSTEM-AREA-POINTER is not a primitive type in ANSI Common Lisp,
16 ;;; so we need a compound type to represent it in the host Common Lisp
17 ;;; at cross-compile time:
18 (defstruct (system-area-pointer (:constructor make-sap) (:conc-name "SAP-"))
19   ;; the integer representation of the address
20   (int (error "missing SAP-INT argument") :type sap-int-type :read-only t))
21
22 ;;; cross-compilation-host analogues of target-CMU CL primitive SAP operations
23 (defun int-sap (int)
24   (make-sap :int int))
25 (defun sap+ (sap offset)
26   (declare (type system-area-pointer sap) (type sap-int-type offset))
27   (make-sap :int (+ (sap-int sap) offset)))
28 #.`(progn
29      ,@(mapcar (lambda (info)
30                  (destructuring-bind (sap-fun int-fun) info
31                    `(defun ,sap-fun (x y)
32                       (,int-fun (sap-int x) (sap-int y)))))
33                '((sap< <) (sap<= <=) (sap= =) (sap>= >=) (sap> >) (sap- -))))
34
35 ;;; dummies, defined so that we can declare they never return and thereby
36 ;;; eliminate a thundering herd of optimization notes a la "can't optimize this
37 ;;; expression because we don't know the return type of SAP-REF-8"
38 (defun sap-ref-stub (name)
39   (error "~S doesn't make sense on cross-compilation host." name))
40 #.`(progn
41      ,@(mapcan (lambda (name)
42                  `((declaim (ftype (function (system-area-pointer fixnum) nil)
43                                    ,name))
44                    (defun ,name (sap offset)
45                      (declare (ignore sap offset))
46                      (sap-ref-stub ',name))
47                    ,@(let ((setter-stub (gensym "SAP-SETTER-STUB-")))
48                        `((defun ,setter-stub (foo sap offset)
49                            (declare (ignore foo sap offset))
50                            (sap-ref-stub '(setf ,name)))
51                          (defsetf ,name ,setter-stub)))))
52                '(sap-ref-8
53                  sap-ref-16
54                  sap-ref-32
55                  sap-ref-sap
56                  sap-ref-single
57                  sap-ref-double
58                  signed-sap-ref-8
59                  signed-sap-ref-16
60                  signed-sap-ref-32)))