Optimize out-of-line string CONCATENATE, part 2.
[sbcl.git] / src / code / host-c-call.lisp
blobb76bc5792e3308b0be70ac5276e4b9612c95c345
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!ALIEN")
12 (/show0 "host-c-call.lisp 12")
14 (define-alien-type-class (c-string :include pointer :include-args (to))
15 (external-format :default :type keyword)
16 (element-type 'character :type (member character base-char))
17 (not-null nil :type boolean))
19 (define-alien-type-translator c-string
20 (&key (external-format :default)
21 (element-type 'character)
22 (not-null nil))
23 (make-alien-c-string-type
24 :to (parse-alien-type 'char (sb!kernel:make-null-lexenv))
25 :element-type element-type
26 :external-format external-format
27 :not-null not-null))
29 (defun c-string-external-format (type)
30 (let ((external-format (alien-c-string-type-external-format type)))
31 (if (eq external-format :default)
32 #+sb-xc-host (bug "No default c-string-external-format")
33 #-sb-xc-host (default-c-string-external-format)
34 external-format)))
36 (define-alien-type-method (c-string :unparse) (type)
37 (let* ((external-format (alien-c-string-type-external-format type))
38 (element-type (alien-c-string-type-element-type type))
39 (not-null (alien-c-string-type-not-null type))
40 (tail
41 (append (unless (eq :default external-format)
42 (list :external-format external-format))
43 (unless (eq 'character element-type)
44 (list :element-type element-type))
45 (when not-null
46 (list :not-null t)))))
47 (if tail
48 (cons 'c-string tail)
49 'c-string)))
51 (define-alien-type-method (c-string :lisp-rep) (type)
52 (let ((possibilities '(simple-string (alien (* char)) (simple-array (unsigned-byte 8)))))
53 (if (alien-c-string-type-not-null type)
54 `(or ,@possibilities)
55 `(or null ,@possibilities))))
57 (define-alien-type-method (c-string :deport-pin-p) (type)
58 (declare (ignore type))
61 (defun c-string-needs-conversion-p (type)
62 #+sb-xc-host
63 (declare (ignore type))
64 #+sb-xc-host
66 #-sb-xc-host
67 (let ((external-format (sb!impl::get-external-format
68 ;; Can't use C-STRING-EXTERNAL-FORMAT here,
69 ;; since the meaning of :DEFAULT can change
70 ;; when *DEFAULT-C-STRING-EXTERNAL-FORMAT*
71 ;; changes.
72 (alien-c-string-type-external-format type))))
73 (not (and external-format
74 (or (eq (first (sb!impl::ef-names external-format)) :ascii)
75 ;; On non-SB-UNICODE all latin-1 codepoints will fit
76 ;; into a base-char, on SB-UNICODE they won't.
77 #!-sb-unicode
78 (eq (first (sb!impl::ef-names external-format)) :latin-1))))))
80 (declaim (ftype (sfunction (t) nil) null-error))
81 (defun null-error (type)
82 (aver (alien-c-string-type-not-null type))
83 (error 'type-error
84 :expected-type `(alien ,(unparse-alien-type type))
85 :datum nil))
87 (define-alien-type-method (c-string :naturalize-gen) (type alien)
88 `(if (zerop (sap-int ,alien))
89 ,(if (alien-c-string-type-not-null type)
90 `(null-error ',type)
91 nil)
92 ;; Check whether we need to do a full external-format
93 ;; conversion, or whether we can just do a cheap byte-by-byte
94 ;; copy of the c-string data.
96 ;; On SB-UNICODE we can never do the cheap copy, even if the
97 ;; external format and element-type are suitable, since
98 ;; simple-base-strings may not contain ISO-8859-1 characters.
99 ;; If we need to check for non-ascii data in the input, we
100 ;; might as well go through the usual external-format machinery
101 ;; instead of rewriting another version of it.
102 ,(if #!+sb-unicode t
103 #!-sb-unicode (c-string-needs-conversion-p type)
104 `(c-string-to-string ,alien
105 (c-string-external-format ,type)
106 (alien-c-string-type-element-type
107 ,type))
108 `(%naturalize-c-string ,alien))))
110 (define-alien-type-method (c-string :deport-gen) (type value)
111 ;; This SAP taking is safe as DEPORT callers pin the VALUE when
112 ;; necessary.
113 `(etypecase ,value
114 (null
115 ,(if (alien-c-string-type-not-null type)
116 `(null-error ',type)
117 `(int-sap 0)))
118 ((alien (* char)) (alien-sap ,value))
119 (vector (vector-sap ,value))))
121 (define-alien-type-method (c-string :deport-alloc-gen) (type value)
122 `(etypecase ,value
123 (null
124 ,(if (alien-c-string-type-not-null type)
125 `(null-error ',type)
126 nil))
127 ((alien (* char)) ,value)
128 (simple-base-string
129 ,(if (c-string-needs-conversion-p type)
130 ;; If the alien type is not ascii-compatible (+SB-UNICODE)
131 ;; or latin-1-compatible (-SB-UNICODE), we need to do
132 ;; external format conversion.
133 `(string-to-c-string ,value
134 (c-string-external-format ,type))
135 ;; Otherwise we can just pass it uncopied.
136 value))
137 (simple-string
138 (string-to-c-string ,value
139 (c-string-external-format ,type)))))
141 (/show0 "host-c-call.lisp end of file")