Debian Codegraph @

src/cmp/cmpbackend-cxx/cmpc-fun-inl.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
6 ;;;; Copyright (c) 1990, Giuseppe Attardi
7 ;;;; Copyright (c) 2023, Daniel Kochmański
8 ;;;;
9 ;;;; See the file 'LICENSE' for the copyright details.
10 ;;;;
11
12 ;;;;
13 ;;;; Open coding functions as C expressions.
14 ;;;;
15
16 (in-package "COMPILER")
17
18 (defstruct (inline-info)
19 name ;;; Function name
20 arg-host-types ;;; List of representation types for the arguments
21 return-host-type ;;; Representation type for the output
22 arg-types ;;; List of lisp types for the arguments
23 return-type ;;; Lisp type for the output
24 exact-return-type ;;; Only use this expansion when the output is
25 ;;; declared to have a subtype of RETURN-TYPE
26 multiple-values ;;; Works with all destinations, including VALUEZ / LEAVE
27 expansion ;;; C template containing the expansion
28 one-liner ;;; Whether the expansion spans more than one line
29 )
30
31 (defmacro define-c-inliner (fname lambda-list &body body)
32 `(setf (gethash ',fname *cinline-dispatch-table*)
33 #'(lambda ,lambda-list (block nil ,@body))))
34
35 (defun apply-inliner (fname return-type inlined-args)
36 (ext:if-let ((fd (gethash fname *cinline-dispatch-table*)))
37 (apply fd return-type inlined-args)
38 (default-c-inliner fname return-type inlined-args)))
39
40 (defun default-c-inliner (fname return-type inlined-args)
41 (let* ((arg-types (mapcar #'loc-lisp-type inlined-args))
42 (ii (inline-function fname arg-types return-type)))
43 (and ii (apply-inline-info ii inlined-args))))
44
45 ;;;
46 ;;; inline-function:
47 ;;; locs are typed locs as produced by inline-args
48 ;;; returns NIL if inline expansion of the function is not possible
49 ;;;
50 (defun inline-function (fname arg-types return-type &optional (return-host-type 'any))
51 ;; Those functions that use INLINE-FUNCTION must rebind the variable
52 ;; *INLINE-BLOCKS*.
53 (and (inline-possible fname)
54 (not (gethash fname *c2-dispatch-table*))
55 (let* (;; (dest-host-type (loc-host-type *destination*))
56 (ii (get-inline-info fname arg-types return-type return-host-type)))
57 ii)))
58
59 (defun apply-inline-info (ii inlined-locs)
60 (let* ((arg-types (inline-info-arg-types ii))
61 (out-host-type (inline-info-return-host-type ii))
62 ;; (out-type (inline-info-return-type ii))
63 (side-effects-p (function-may-have-side-effects (inline-info-name ii)))
64 (fun (inline-info-expansion ii))
65 (one-liner (inline-info-one-liner ii)))
66 (produce-inline-loc inlined-locs arg-types (list out-host-type)
67 fun side-effects-p one-liner)))
68
69 (defun choose-inline-info (ia ib return-type return-host-type)
70 (declare (ignore return-type))
71 (cond
72 ;; Only accept inliners that have the right rep type
73 ((not (or (eq return-host-type 'any)
74 (eq return-host-type :void)
75 (let ((info-type (inline-info-return-host-type ib)))
76 (or (eq return-host-type info-type)
77 ;; :bool can be coerced to any other location type
78 (eq info-type :bool)))))
79 ia)
80 ((null ia)
81 ib)
82 ;; Keep the first one, which is typically the least safe but fastest.
83 ((equal (inline-info-arg-types ia) (inline-info-arg-types ib))
84 ia)
85 ;; More specific?
86 ((every #'(lambda (t1 t2) (type>= t1 t2 *cmp-env*))
87 (inline-info-arg-types ia)
88 (inline-info-arg-types ib))
89 ib)
90 ;; Keep the first one, which is typically the least safe but fastest.
91 (t
92 ia)))
93
94 (defun get-inline-info (fname types return-type return-host-type)
95 (declare (si::c-local))
96 (let ((output nil))
97 (unless (safe-compile)
98 (dolist (x (inline-information fname ':INLINE-UNSAFE))
99 (ext:when-let ((other (inline-type-matches x types return-type)))
100 (setf output (choose-inline-info output other return-type return-host-type)))))
101 (dolist (x (inline-information fname ':INLINE-ALWAYS))
102 (ext:when-let ((other (inline-type-matches x types return-type)))
103 (setf output (choose-inline-info output other return-type return-host-type))))
104 output))
105
106 (defun to-fixnum-float-type (type)
107 (dolist (i '(CL:FIXNUM CL:DOUBLE-FLOAT CL:SINGLE-FLOAT CL:LONG-FLOAT) nil)
108 (when (type>= i type *cmp-env*)
109 (return i))))
110
111 (defun maximum-float-type (t1 t2)
112 (macrolet ((try-type (type)
113 `(and (or (eq t1 ,type) (eq t2 ,type))
114 ,type)))
115 (or (and (null t1) t2)
116 (try-type 'CL:LONG-FLOAT)
117 (try-type 'CL:DOUBLE-FLOAT)
118 (try-type 'CL:SINGLE-FLOAT)
119 'CL:FIXNUM)))
120
121 (defun inline-type-matches (inline-info arg-types return-type)
122 (when (and (not (inline-info-multiple-values inline-info))
123 (member *destination* '(VALUEZ LEAVE)))
124 (return-from inline-type-matches nil))
125 (let* ((rts nil)
126 (number-max nil))
127 ;;
128 ;; Check that the argument types match those of the inline expression
129 ;;
130 (do* ((arg-types arg-types (cdr arg-types))
131 (types (inline-info-arg-types inline-info) (cdr types)))
132 ((or (endp arg-types) (endp types))
133 (when (or arg-types types)
134 (return-from inline-type-matches nil)))
135 (let* ((arg-type (first arg-types))
136 (type (first types)))
137 (cond ((eq type 'FIXNUM-FLOAT)
138 (let ((new-type (to-fixnum-float-type arg-type)))
139 (unless new-type
140 (return-from inline-type-matches nil))
141 (push new-type rts)
142 (setq number-max (maximum-float-type number-max new-type))))
143 #+sse2
144 ;; Allow implicit casts between SSE subtypes to kick in
145 ((and (type>= 'ext:sse-pack type *cmp-env*)
146 (type>= 'ext:sse-pack arg-type *cmp-env*))
147 (push type rts))
148 ((type>= type arg-type *cmp-env*)
149 (push type rts))
150 (t (return-from inline-type-matches nil)))))
151 ;;
152 ;; Now there is an optional check of the return type. This check is
153 ;; only used when enforced by the inliner.
154 ;;
155 (when (or (eq (inline-info-return-host-type inline-info) :bool)
156 (null (inline-info-exact-return-type inline-info))
157 (and (policy-assume-right-type)
158 (let ((inline-return-type (inline-info-return-type inline-info)))
159 (if number-max
160 ;; for arithmetic operators we take the maximal
161 ;; type as possible result type. Note that FIXNUM
162 ;; is not an option, because the product, addition
163 ;; or difference of fixnums may be a larger
164 ;; integer.
165 (and (setf number-max (if (eq number-max 'fixnum)
166 'integer
167 number-max))
168 (type>= inline-return-type number-max *cmp-env*)
169 (type>= number-max return-type *cmp-env*))
170 ;; no contravariance
171 (type>= inline-return-type return-type *cmp-env*)))))
172 (let ((inline-info (copy-structure inline-info)))
173 (setf (inline-info-arg-types inline-info)
174 (nreverse rts))
175 inline-info))))
176
177 (defun produce-inline-loc (inlined-arguments arg-types output-host-type
178 c-expression side-effects one-liner)
179 (let* (args-to-be-saved
180 coerced-arguments)
181 ;; If the expression begins with @[0-9a-z]*, this means we are
182 ;; saving some variables.
183 (when (and (> (length c-expression) 1)
184 (eq (char c-expression 0) #\@))
185 (do ((ndx 1 (1+ ndx)))
186 ((>= ndx (length c-expression)))
187 (let ((c (char c-expression ndx)))
188 (when (eq c #\;)
189 (setf c-expression (subseq c-expression (1+ ndx)))
190 (return))
191 (unless (alphanumericp c)
192 (setf args-to-be-saved nil)
193 (return))
194 (push (- (char-code c) (char-code #\0))
195 args-to-be-saved))))
196
197 (setf coerced-arguments (coerce-locs inlined-arguments arg-types args-to-be-saved))
198 ;;(setf output-host-type (lisp-type->host-type output-host-type))
199
200 ;; If the form does not output any data, and there are no side
201 ;; effects, try to omit it.
202 (when (null output-host-type)
203 (if side-effects
204 (progn
205 (wt-nl)
206 (wt-c-inline-loc output-host-type c-expression coerced-arguments t nil)
207 (when one-liner (wt ";")))
208 (cmpnote "Ignoring form ~S" c-expression))
209 (wt-nl "value0 = ECL_NIL;")
210 (wt-nl "cl_env_copy->nvalues = 0;")
211 (return-from produce-inline-loc 'LEAVE))
212
213 ;; If the form is a one-liner, we can simply propagate this expression until the
214 ;; place where the value is used.
215 (when one-liner
216 (return-from produce-inline-loc
217 `(ffi:c-inline ,output-host-type ,c-expression ,coerced-arguments ,side-effects
218 ,(if (equalp output-host-type '((VALUES &REST T)))
219 'VALUES NIL))))
220
221 ;; If the output is a in the VALUES vector, just write down the form and
222 ;; output the location of the data.
223 (when (equalp output-host-type '((VALUES &REST T)))
224 (wt-c-inline-loc output-host-type c-expression coerced-arguments side-effects
225 'VALUES)
226 (return-from produce-inline-loc 'VALUEZ))
227
228 ;; Otherwise we have to set up variables for holding the output.
229 (flet ((make-output-var (type)
230 (let ((var (make-lcl-var :host-type type)))
231 (wt-nl (host-type->c-name type) " " var ";")
232 var)))
233 (open-inline-block)
234 (let ((output-vars (mapcar #'make-output-var output-host-type)))
235 (wt-c-inline-loc output-host-type c-expression coerced-arguments side-effects output-vars)
236 (cond ((= (length output-vars) 1)
237 (first output-vars))
238 (t
239 (loop for v in output-vars
240 for i from 0
241 do (set-loc `(VALUE ,i) v))
242 (wt "cl_env_copy->nvalues = " (length output-vars) ";")
243 'VALUEZ))))))
244
245 ;;; Whoever calls this function must wrap the body in WITH-INLINE-BLOCKS.
246 (defun negate-argument (argument dest-loc)
247 (let* ((inlined-arg (emit-inline-form argument nil))
248 (host-type (loc-host-type inlined-arg)))
249 (apply #'produce-inline-loc
250 (list inlined-arg)
251 (if (eq (loc-host-type dest-loc) :bool)
252 (case host-type
253 (:bool '((:bool) (:bool) "(#0)==ECL_NIL" nil t))
254 (:object '((:object) (:bool) "(#0)!=ECL_NIL" nil t))
255 (otherwise (return-from negate-argument nil)))
256 (case host-type
257 (:bool '((:bool) (:object) "(#0)?ECL_NIL:ECL_T" nil t))
258 (:object '((:object) (:object) "Null(#0)?ECL_T:ECL_NIL" nil t))
259 (otherwise (return-from negate-argument *vv-nil*)))))))