Debian Codegraph @

src/cmp/cmpbackend-cxx/cmpc-mach.lsp

raw
1
2 ;;;;
3 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
4 ;;;; Copyright (c) 1990, Giuseppe Attardi
5 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
6 ;;;; Copyright (c) 2023, Daniel Kochmański
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 (in-package #:compiler)
11
12 ;;; Abstract target machine details
13
14 (defstruct machine
15 (c-types '())
16 host-type-hash
17 sorted-types
18 inline-information)
19
20 (defstruct (host-type (:constructor %make-host-type))
21 (index 0) ; Precedence order in the type list
22 (name t)
23 (lisp-type t)
24 (bits nil)
25 (numberp nil)
26 (integerp nil)
27 (c-name nil)
28 (to-lisp nil)
29 (from-lisp nil)
30 (from-lisp-unsafe nil))
31
32 (defun lisp-type-p (type)
33 (subtypep type 'T *cmp-env*))
34
35 (defun host-type-record-unsafe (host-type)
36 (gethash host-type (machine-host-type-hash *machine*)))
37
38 (defun host-type-record (host-type)
39 (ext:if-let ((record (gethash host-type (machine-host-type-hash *machine*))))
40 record
41 (cmperr "Not a valid C type name ~A" host-type)))
42
43 (defun host-type->lisp-type (name)
44 (let ((output (host-type-record-unsafe name)))
45 (cond (output
46 (host-type-lisp-type output))
47 ((lisp-type-p name) name)
48 (t (error "Unknown representation type ~S" name)))))
49
50 (defun lisp-type->host-type (type)
51 (cond
52 ;; We expect type = NIL when we have no information. Should be fixed. FIXME!
53 ((null type)
54 :object)
55 ((let ((r (host-type-record-unsafe type)))
56 (and r (host-type-name r))))
57 (t
58 ;; Find the most specific type that fits
59 (dolist (record (machine-sorted-types *machine*) :object)
60 (when (subtypep type (host-type-lisp-type record) *cmp-env*)
61 (return-from lisp-type->host-type (host-type-name record)))))))
62
63 (defun c-number-host-type-p (host-type)
64 (let ((r (host-type-record-unsafe host-type)))
65 (and r (host-type-numberp r))))
66
67 (defun c-integer-host-type-p (host-type)
68 (let ((r (host-type-record-unsafe host-type)))
69 (and r (host-type-integerp r))))
70
71 (defun c-integer-host-type-bits (host-type)
72 (let ((r (host-type-record-unsafe host-type)))
73 (and r (host-type-bits r))))
74
75 (defun c-number-type-p (type)
76 (c-number-host-type-p (lisp-type->host-type type)))
77
78 (defun c-integer-type-p (type)
79 (c-integer-host-type-p (lisp-type->host-type type)))
80
81 (defun c-integer-type-bits (type)
82 (c-number-host-type-bits (lisp-type->host-type type)))
83
84 (defun host-type->c-name (type)
85 (host-type-c-name (host-type-record type)))
86
87 ;; These types can be used by ECL to unbox data They are sorted from
88 ;; the most specific, to the least specific one. All functions must
89 ;; be declared in external.h (not internal.h) header file.
90 (defconstant +host-types+
91 ;; host type lisp type c type convert C->Lisp convert Lisp->C unbox Lisp->C (unsafe)
92 '((:byte . #1=((signed-byte 8) "int8_t" "ecl_make_int8_t" "ecl_to_int8_t" "ecl_fixnum"))
93 (:unsigned-byte . #2=((unsigned-byte 8) "uint8_t" "ecl_make_uint8_t" "ecl_to_uint8_t" "ecl_fixnum"))
94 (:fixnum integer "cl_fixnum" "ecl_make_fixnum" "ecl_to_fixnum" "ecl_fixnum")
95 (:int integer "int" "ecl_make_int" "ecl_to_int" "ecl_to_int")
96 (:unsigned-int integer "unsigned int" "ecl_make_uint" "ecl_to_uint" "ecl_to_uint")
97 (:long integer "long" "ecl_make_long" "ecl_to_long" "ecl_to_long")
98 (:unsigned-long integer "unsigned long" "ecl_make_ulong" "ecl_to_ulong" "ecl_to_ulong")
99 (:cl-index integer "cl_index" "ecl_make_unsigned_integer" "ecl_to_cl_index" "ecl_fixnum")
100 (:long-long integer "ecl_long_long_t" "ecl_make_long_long" "ecl_to_long_long" "ecl_to_long_long")
101 (:unsigned-long-long integer "ecl_ulong_long_t" "ecl_make_ulong_long" "ecl_to_ulong_long" "ecl_to_ulong_long")
102 (:float single-float "float" "ecl_make_single_float" "ecl_to_float" "ecl_single_float")
103 (:double double-float "double" "ecl_make_double_float" "ecl_to_double" "ecl_double_float")
104 (:long-double long-float "long double" "ecl_make_long_float" "ecl_to_long_double" "ecl_long_float")
105 (:csfloat si::complex-single-float "_Complex float" "ecl_make_csfloat" "ecl_to_csfloat" "ecl_csfloat")
106 (:cdfloat si::complex-double-float "_Complex double" "ecl_make_cdfloat" "ecl_to_cdfloat" "ecl_cdfloat")
107 (:clfloat si::complex-long-float "_Complex long double" "ecl_make_clfloat" "ecl_to_clfloat" "ecl_clfloat")
108 (:unsigned-char base-char "unsigned char" "ECL_CODE_CHAR" "ecl_base_char_code" "ECL_CHAR_CODE")
109 (:char base-char "char" "ECL_CODE_CHAR" "ecl_base_char_code" "ECL_CHAR_CODE")
110 (:wchar character "ecl_character" "ECL_CODE_CHAR" "ecl_char_code" "ECL_CHAR_CODE")
111 (:float-sse-pack ext::float-sse-pack "__m128" "ecl_make_float_sse_pack" "ecl_unbox_float_sse_pack" "ecl_unbox_float_sse_pack_unsafe")
112 (:double-sse-pack ext::double-sse-pack "__m128d" "ecl_make_double_sse_pack" "ecl_unbox_double_sse_pack" "ecl_unbox_double_sse_pack_unsafe")
113 ;; intentional v
114 (:int-sse-pack ext::sse-pack "__m128i" "ecl_make_int_sse_pack" "ecl_unbox_int_sse_pack" "ecl_unbox_int_sse_pack_unsafe")
115 (:object t "cl_object" "" "" "")
116 (:bool t "bool" "ecl_make_bool" "ecl_to_bool" "ecl_to_bool")
117 ;; These types are never selected to unbox data.
118 ;; They are here, because we need to know how to print them.
119 (:void t "void" nil nil nil)
120 (:pointer-void si::foreign-data "void*" "ecl_make_pointer" "ecl_to_pointer" "ecl_to_pointer")
121 (:cstring string "char*" "ecl_cstring_to_base_string_or_nil" nil nil)
122 (:char* string "char*" nil nil nil)
123 (:int8-t . #1#)
124 (:uint8-t . #2#)
125 (:int16-t integer "ecl_int16_t" "ecl_make_int16_t" "ecl_to_int16_t" "ecl_to_int16_t")
126 (:uint16-t integer "ecl_uint16_t" "ecl_make_uint16_t" "ecl_to_uint16_t" "ecl_to_unt16_t")
127 (:int32-t integer "ecl_int32_t" "ecl_make_int32_t" "ecl_to_int32_t" "ecl_to_int32_t")
128 (:uint32-t integer "ecl_uint32_t" "ecl_make_uint32_t" "ecl_to_uint32_t" "ecl_to_uint32_t")
129 (:int64-t integer "ecl_int64_t" "ecl_make_int64_t" "ecl_to_int64_t" "ecl_to_int64_t")
130 (:uint64-t integer "ecl_uint64_t" "ecl_make_uint64_t" "ecl_to_uint64_t" "ecl_to_uint64_t")
131 (:short integer "short" "ecl_make_short" "ecl_to_short" "ecl_fixnum")
132 (:unsigned-short integer "unsigned short" "ecl_make_ushort" "ecl_to_ushort" "ecl_fixnum")))
133
134
135 ;; FIXME number of bits is used for bit fiddling optimizations. That
136 ;; information should be defined separately. -- jd 2019-11-27
137 (defconstant +this-machine-c-types+
138 ;; type integer bits (negative means "signed")
139 '((:byte . -8)
140 (:unsigned-byte . 8)
141 (:unsigned-short . #.(- (logcount ffi:c-ushort-max)))
142 (:short . #.(- (logcount ffi:c-ushort-max)))
143 (:unsigned-int . #.(logcount ffi:c-uint-max))
144 (:int . #.(- (logcount ffi:c-uint-max)))
145 (:unsigned-long . #.(logcount ffi:c-ulong-max))
146 (:long . #.(- (logcount ffi:c-ulong-max)))
147 #+long-long (:unsigned-long-long . #.(logcount ffi:c-ulong-long-max))
148 #+long-long (:long-long . #.(- (logcount ffi:c-ulong-long-max)))
149 (:cl-index . #.(logcount most-positive-fixnum))
150 (:fixnum . #.(- -1 (logcount most-positive-fixnum)))
151 (:uint8-t . 8)
152 (:int8-t . -8)
153 #+:uint16-t (:uint16-t . 16)
154 #+:uint16-t (:int16-t . -16)
155 #+:uint32-t (:uint32-t . 32)
156 #+:uint32-t (:int32-t . -32)
157 #+:uint64-t (:uint64-t . 64)
158 #+:uint64-t (:int64-t . -64)
159 #+:sse2 (:float-sse-pack . nil)
160 #+:sse2 (:double-sse-pack . nil)
161 #+:sse2 (:int-sse-pack . nil)
162 #+complex-float (:csfloat . nil)
163 #+complex-float (:cdfloat . nil)
164 #+complex-float (:clfloat . nil)))
165
166 (defconstant +all-machines-c-types+
167 '((:object)
168 (:float)
169 (:double)
170 (:long-double)
171 (:char)
172 (:unsigned-char)
173 (:wchar)
174 (:char*)
175 (:cstring)
176 (:bool)
177 (:void)
178 (:pointer-void)))
179
180 (defun make-host-type (all-c-types name lisp-type c-name &optional to-lisp from-lisp from-lisp-unsafe)
181 (let* ((record (assoc name all-c-types))
182 (bits (cdr record)))
183 (when record
184 ;; For integer bits we get extra information from ALL-C-TYPES
185 (when bits
186 (if (plusp bits)
187 (setf lisp-type `(unsigned-byte ,bits))
188 (setf bits (- bits)
189 lisp-type `(signed-byte ,bits))))
190 (%make-host-type
191 :name name
192 :lisp-type lisp-type
193 :bits bits
194 :numberp (subtypep lisp-type 'number *cmp-env*)
195 :integerp (subtypep lisp-type 'integer *cmp-env*)
196 :c-name c-name
197 :to-lisp to-lisp
198 :from-lisp from-lisp
199 :from-lisp-unsafe from-lisp-unsafe))))
200
201 (defun default-machine ()
202 (let* ((all-c-types (append +this-machine-c-types+ +all-machines-c-types+))
203 (table (make-hash-table :size 128 :test 'eq))
204 (sorted-host-types
205 ;; Create the host-type objects
206 (loop for i from 0
207 for record in +host-types+
208 for host-type = (apply #'make-host-type all-c-types record)
209 when host-type
210 do (setf (host-type-index host-type) i)
211 and collect (setf (gethash (host-type-name host-type) table) host-type))))
212 ;; hack: sse-pack -> int, but int -> int-sse-pack
213 (let ((r (gethash :int-sse-pack table)))
214 (when r
215 (setf (host-type-index r) 'ext:int-sse-pack)))
216 ;; On a second pass, we replace types with more general ones
217 (loop with fixnum-host-type = (gethash ':fixnum table)
218 with fixnum-lisp-type = (host-type-lisp-type fixnum-host-type)
219 for (name . rest) in +host-types+
220 for r = (gethash name table)
221 when (and r (subtypep (host-type-lisp-type r) fixnum-lisp-type *cmp-env*))
222 do (setf (host-type-from-lisp-unsafe r) "ecl_fixnum"))
223 ;; Create machine object
224 (make-machine :c-types all-c-types
225 :host-type-hash table
226 :sorted-types sorted-host-types)))
227
228 (defun machine-c-type-p (name)
229 (gethash name (machine-host-type-hash *machine*)))
230
231 (defvar *default-machine* (setf *machine* (default-machine)))