0.8.18.14:
[sbcl.git] / src / code / target-c-call.lisp
1 ;;;; FIXME: This file and host-c-call.lisp are separate from the
2 ;;;; rest of the alien source code for historical reasons: CMU CL
3 ;;;; made a distinction between the stuff in the C-CALL package and
4 ;;;; stuff in the ALIEN package. There's no obvious boundary
5 ;;;; there, though, and SBCL doesn't try to make this distinction,
6 ;;;; so it might make sense to just merge these files in with the
7 ;;;; rest of the SB-ALIEN code.
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
17
18 (in-package "SB!ALIEN")
19 \f
20 ;;;; extra types
21
22 (define-alien-type char (integer 8))
23 (define-alien-type short (integer 16))
24 (define-alien-type int (integer 32))
25 (define-alien-type long (integer #.sb!vm::n-machine-word-bits))
26
27 (define-alien-type unsigned-char (unsigned 8))
28 (define-alien-type unsigned-short (unsigned 16))
29 (define-alien-type unsigned-int (unsigned 32))
30 (define-alien-type unsigned-long (unsigned #.sb!vm::n-machine-word-bits))
31
32 (define-alien-type float single-float)
33 (define-alien-type double double-float)
34
35 (define-alien-type-translator void ()
36   (parse-alien-type '(values) (sb!kernel:make-null-lexenv)))
37 \f
38 ;;; FIXME: %NATURALIZE-C-STRING (and the UTF8 siblings below) would
39 ;;; appear to be vulnerable to the lisp string moving from underneath
40 ;;; them if the world undergoes a GC, possibly triggered by another
41 ;;; thread.  Ugh.
42 (defun %naturalize-c-string (sap)
43   (declare (type system-area-pointer sap))
44   (locally
45       (declare (optimize (speed 3) (safety 0)))
46     (let ((length (loop for offset of-type fixnum upfrom 0
47                         until (zerop (sap-ref-8 sap offset))
48                         finally (return offset))))
49       (let ((result (make-string length :element-type 'base-char)))
50         (sb!kernel:copy-from-system-area sap 0
51                                          result (* sb!vm:vector-data-offset
52                                                    sb!vm:n-word-bits)
53                                          (* length sb!vm:n-byte-bits))
54         result))))
55
56 (defun %naturalize-utf8-string (sap)
57   (declare (type system-area-pointer sap))
58   (locally
59     (declare (optimize (speed 3) (safety 0)))
60     (let ((length (do* ((offset 0)
61                         (byte (sap-ref-8 sap offset) (sap-ref-8 sap offset))
62                         (index 0 (1+ index)))
63                        ((zerop byte) index)
64                     (declare (type fixnum offset index))
65                     (cond
66                       ;; FIXME: Here, and below, we don't defend
67                       ;; against malformed utf-8 with any degree of
68                       ;; rigour.
69                       ((< byte #x80) (incf offset))
70                       ((< byte #xe0) (incf offset 2))
71                       ((< byte #xf0) (incf offset 3))
72                       (t (incf offset 4))))))
73       (let ((result (make-string length :element-type 'character)))
74         (do* ((offset 0)
75               (byte (sap-ref-8 sap offset) (sap-ref-8 sap offset))
76               (index 0 (1+ index)))
77             ((>= index length) result)
78           (declare (type fixnum offset index))
79           (setf (char result index)
80                 (cond
81                   ((< byte #x80)
82                    (prog1 (code-char byte) (incf offset)))
83                   ((< byte #xe0)
84                    (prog1 (code-char (dpb byte (byte 5 6)
85                                           (sap-ref-8 sap (1+ offset))))
86                      (incf offset 2)))
87                   ((< byte #xf0)
88                    (prog1 (code-char
89                            (dpb byte (byte 4 12)
90                                 (dpb (sap-ref-8 sap (1+ offset)) (byte 6 6)
91                                      (sap-ref-8 sap (+ 2 offset)))))
92                      (incf offset 3)))
93                   (t
94                    (prog1
95                        (code-char
96                         (dpb byte (byte 3 18)
97                              (dpb (sap-ref-8 sap (1+ offset)) (byte 6 12)
98                                   (dpb (sap-ref-8 sap (+ 2 offset)) (byte 6 6)
99                                        (sap-ref-8 sap (+ 3 offset))))))
100                      (incf offset 4))))))))))
101
102 (defun %deport-utf8-string (string)
103   (declare (type simple-string string))
104   (locally
105     (declare (optimize (speed 3) (safety 0)))
106     (let ((length (1+ (do* ((offset 0)
107                             (length (length string))
108                             (index 0 (1+ index)))
109                            ((= index length) offset)
110                         (declare (type fixnum offset))
111                         (let ((bits (char-code (char string index))))
112                           (cond
113                             ((< bits #x80) (incf offset 1))
114                             ((< bits #x800) (incf offset 2))
115                             ((< bits #x10000) (incf offset 3))
116                             (t (incf offset 4))))))))
117       (let ((vector (make-array length :element-type '(unsigned-byte 8)
118                                 :initial-element 0)))
119         (do* ((offset 0)
120               (length (length string))
121               (index 0 (1+ index)))
122              ((= index length) vector)
123           (declare (type fixnum offset))
124           (let ((bits (char-code (char string index))))
125             (cond
126               ((< bits #x80)
127                (setf (aref vector offset) bits)
128                (incf offset))
129               ((< bits #x800)
130                (setf (aref vector offset) (logior #xc0 (ldb (byte 5 6) bits)))
131                (setf (aref vector (1+ offset))
132                      (logior #x80 (ldb (byte 6 0) bits)))
133                (incf offset 2))
134               ((< bits #x10000)
135                (setf (aref vector offset) (logior #xe0 (ldb (byte 4 12) bits)))
136                (setf (aref vector (1+ offset))
137                      (logior #x80 (ldb (byte 6 6) bits)))
138                (setf (aref vector (+ offset 2))
139                      (logior #x80 (ldb (byte 6 0) bits)))
140                (incf offset 3))
141               (t
142                (setf (aref vector offset) (logior #xf0 (ldb (byte 3 18) bits)))
143                (setf (aref vector (1+ offset))
144                      (logior #x80 (ldb (byte 6 12) bits)))
145                (setf (aref vector (+ offset 2))
146                      (logior #x80 (ldb (byte 6 6) bits)))
147                (setf (aref vector (+ offset 3))
148                      (logior #x80 (ldb (byte 6 0) bits)))
149                (incf offset 4)))))))))