Debian Codegraph @

src/cmp/cmpbackend-cxx/cmppass2-loc.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 ;;;;
8 ;;;; See the file 'LICENSE' for the copyright details.
9 ;;;;
10
11 ;;;; Set-loc and Wt-loc.
12
13 (in-package "COMPILER")
14
15
16 ;;;
17 ;;; Mundane locs
18 ;;;
19
20 (defun wt-loc (loc)
21 (cond ((consp loc)
22 (let ((fd (gethash (car loc) *wt-loc-dispatch-table*)))
23 (if fd
24 (apply fd (cdr loc))
25 (unknown-location 'wt-loc loc))))
26 ((symbolp loc)
27 (let ((txt (gethash loc *wt-loc-dispatch-table* :not-found)))
28 (when (eq txt :not-found)
29 (unknown-location 'wt-loc loc))
30 (wt txt)))
31 ((stringp loc)
32 (wt loc))
33 ((var-p loc)
34 (wt-var loc))
35 ((vv-p loc)
36 (wt-vv loc))
37 (t
38 (unknown-location 'wt-loc loc))))
39
40 (defun wt-fixnum (value &optional vv)
41 (declare (ignore vv))
42 (princ value *compiler-output1*)
43 ;; Specify explicit type suffix as a workaround for MSVC. C99
44 ;; standard compliant compilers don't need type suffixes and choose
45 ;; the correct type themselves. Note that we cannot savely use
46 ;; anything smaller than a long long here, because we might perform
47 ;; some other computation on the integer constant which could
48 ;; overflow if we use a smaller integer type (overflows in long long
49 ;; computations are taken care of by the compiler before we get to
50 ;; this point).
51 #+msvc (princ (cond ((typep value (host-type->lisp-type :long-long) *cmp-env*) "LL")
52 ((typep value (host-type->lisp-type :unsigned-long-long) *cmp-env*) "ULL")
53 (t (baboon :format-control
54 "wt-fixnum: The number ~A doesn't fit any integer type."
55 value)))
56 *compiler-output1*))
57
58 (defun wt-number (value &optional vv)
59 (declare (ignore vv))
60 (wt value))
61
62 (defun wt-character (value &optional vv)
63 (declare (ignore vv))
64 ;; We do not use the '...' format because this creates objects of type
65 ;; 'char' which have sign problems
66 (wt (char-code value)))
67
68 (defun wt-value (i)
69 (wt "cl_env_copy->values[" i "]"))
70
71 (defun wt-keyvars (i)
72 (wt "keyvars[" i "]"))
73
74 (defun wt-the (type loc)
75 (declare (ignore type))
76 (wt-loc loc))
77
78
79 ;;;
80 ;;; CALL-LOC
81 ;;;
82
83 (defun wt-call (fun args &optional fname env)
84 (if env
85 (progn
86 (setf *aux-closure* t)
87 (wt "(aux_closure.env="env",cl_env_copy->function=(cl_object)&aux_closure,")
88 (wt-call fun args)
89 (wt ")"))
90 (progn
91 (wt fun "(")
92 (let ((comma ""))
93 (dolist (arg args)
94 (wt comma arg)
95 (setf comma ", ")))
96 (wt ")")))
97 (when fname (wt-comment fname)))
98
99 (defun wt-call-indirect (fun-loc args fname function-p)
100 (let ((narg (length args)))
101 (cond (function-p
102 (wt "(cl_env_copy->function=" fun-loc ")->cfun.entry(" narg))
103 ((and fname (symbolp fname))
104 (wt "(cl_env_copy->function=" fun-loc "->symbol.gfdef)->cfun.entry(" narg))
105 (t
106 (wt "ecl_function_dispatch(cl_env_copy," fun-loc ")(" narg)))
107 (dolist (arg args)
108 (wt ", " arg))
109 (wt ")")
110 (when fname
111 (wt-comment fname))))
112
113 (defun wt-call-global-stack (loc fname)
114 (wt "ecl_apply_from_stack_frame(_ecl_inner_frame," loc ")")
115 (when fname
116 (wt-comment fname)))
117
118 (defun wt-call-local-stack (fun args type)
119 (wt "(cl_env_copy->stack_frame=_ecl_inner_frame,")
120 (wt-call-normal fun
121 (subseq args 0 (min (length args) si::c-arguments-limit))
122 type
123 (length args))
124 (wt ")"))
125
126 (defun wt-call-normal (fun args type &optional (narg (length args)))
127 (declare (ignore type))
128 (unless (fun-cfun fun)
129 (baboon "Function without a C name: ~A" (fun-name fun)))
130 (let* ((minarg (fun-minarg fun))
131 (maxarg (fun-maxarg fun))
132 (fun-c-name (fun-cfun fun))
133 (fun-lisp-name (fun-name fun))
134 (env nil))
135 (case (fun-closure fun)
136 (CLOSURE
137 (when (plusp *max-env*)
138 (setf env (environment-accessor fun))))
139 (LEXICAL
140 (let ((lex-lvl (fun-level fun)))
141 (dotimes (n lex-lvl)
142 (let* ((j (- lex-lvl n 1))
143 (x (lex-env-var-name j)))
144 (push x args))))))
145 (when (not (<= minarg narg maxarg))
146 (cmperr "Wrong number of arguments for function ~S"
147 (or fun-lisp-name 'ANONYMOUS)))
148 (when (fun-needs-narg fun)
149 (push narg args))
150 (wt-call fun-c-name args nil env)))
151
152
153 ;;;
154 ;;; FDEFINITION, MAKE-CLOSURE
155 ;;;
156 (defun wt-fdefinition (fun-name)
157 (let* ((name (si:function-block-name fun-name))
158 (package (symbol-package name))
159 (safe (or (not (safe-compile))
160 (and (or (eq package (find-package "CL"))
161 (eq package (find-package "CLOS"))
162 (eq package (find-package "SI")))
163 (fboundp fun-name)
164 (functionp (fdefinition fun-name))))))
165 (cond
166 ((not safe)
167 (let ((vv (get-object fun-name :test #'equal)))
168 (wt "ecl_fdefinition(" vv ")")))
169 ((eq name fun-name)
170 ;; #'symbol
171 (let ((vv (get-object name)))
172 (wt "(" vv "->symbol.gfdef)")))
173 (t
174 ;; #'(SETF symbol)
175 (let ((setf-loc (assoc name *setf-definitions*)))
176 (unless setf-loc
177 (let* ((setf-vv (data-empty-loc*))
178 (name-vv (get-object name :test #'equal)))
179 (setf setf-loc (list name setf-vv name-vv))
180 (push setf-loc *setf-definitions*)))
181 (wt "ECL_CONS_CAR(" (second setf-loc) ")"))))))
182
183 (defun environment-accessor (fun)
184 (let* ((env-var (env-var-name *env-lvl*))
185 (expected-env-size (fun-env fun)))
186 (if (< expected-env-size *env*)
187 (format nil "ecl_nthcdr(~D,~A)" (- *env* expected-env-size) env-var)
188 env-var)))
189
190 (defun wt-make-closure (fun)
191 (declare (type fun fun))
192 (let* ((closure (fun-closure fun))
193 (narg (fun-fixed-narg fun))
194 (variadic-entrypoint (fun-variadic-entrypoint fun))
195 (cfun (fun-cfun fun))
196 (entrypoint (or variadic-entrypoint cfun))
197 (narg-fixed (fun-fixed-narg-main-entrypoint fun)))
198 (cond ((eq closure 'CLOSURE)
199 (wt "ecl_make_cclosure_va((cl_objectfn)" entrypoint "," (environment-accessor fun) ",Cblock," narg-fixed ")"))
200 ((eq closure 'LEXICAL)
201 (baboon :format-control "wt-make-closure: lexical closure detected."))
202 (narg ; empty environment fixed number of args
203 (wt (if variadic-entrypoint
204 "ecl_make_cfun_va((cl_objectfn)"
205 "ecl_make_cfun((cl_objectfn_fixed)")
206 entrypoint ",ECL_NIL,Cblock," narg-fixed ")"))
207 (t ; empty environment variable number of args
208 (wt "ecl_make_cfun_va((cl_objectfn)" entrypoint ",ECL_NIL,Cblock," narg-fixed ")")))))
209
210 ;;;
211 ;;; COERCE-LOC
212 ;;;
213
214 (defun wt-to-object-conversion (loc-host-type loc)
215 ;; FIXME we can do better for constant locations.
216 (let* ((record (host-type-record loc-host-type))
217 (coercer (and record (host-type-to-lisp record))))
218 (unless coercer
219 (cmperr "Cannot coerce C variable of type ~S to lisp object" loc-host-type))
220 (wt coercer "(" loc ")")))
221
222 (defun wt-from-object-conversion (dest-type loc-type host-type loc)
223 (let* ((record (host-type-record host-type))
224 (coercer (and record (host-type-from-lisp record))))
225 (unless coercer
226 (cmperr "Cannot coerce lisp object to C type ~A" host-type))
227 (wt (if (or (policy-assume-no-errors)
228 (subtypep loc-type dest-type *cmp-env*))
229 (host-type-from-lisp-unsafe record)
230 coercer)
231 "(" loc ")")))
232
233 (defun wt-coerce-loc (dest-host-type loc)
234 (setq dest-host-type (lisp-type->host-type dest-host-type))
235 ;(print dest-host-type)
236 ;(print loc)
237 (let* ((dest-type (host-type->lisp-type dest-host-type))
238 (loc-type (loc-lisp-type loc))
239 (loc-host-type (loc-host-type loc)))
240 (labels ((coercion-error (&optional (write-zero t))
241 (cmpwarn "Unable to coerce lisp object from type (~S,~S)~%~
242 to C/C++ type (~S,~S)"
243 loc-type loc-host-type dest-type dest-host-type)
244 (when write-zero
245 ;; It is possible to reach this point due to a bug
246 ;; but also due to a failure of the dead code
247 ;; elimination. Write a zero to ensure that the
248 ;; output is syntactically valid C code and hope for
249 ;; the latter case.
250 (wt "(ecl_miscompilation_error(),0)")))
251 (ensure-valid-object-type (a-lisp-type)
252 (when (subtypep `(AND ,loc-type ,a-lisp-type) NIL *cmp-env*)
253 (coercion-error nil))))
254 (when (eq dest-host-type loc-host-type)
255 (wt loc)
256 (return-from wt-coerce-loc))
257 (case dest-host-type
258 ((:char :unsigned-char :wchar)
259 (case loc-host-type
260 ((:char :unsigned-char :wchar)
261 (wt "(" (host-type->c-name dest-host-type) ")(" loc ")"))
262 ((:object)
263 (ensure-valid-object-type dest-type)
264 (wt-from-object-conversion dest-type loc-type dest-host-type loc))
265 (otherwise
266 (coercion-error))))
267 ((:float :double :long-double)
268 (cond
269 ((c-number-host-type-p loc-host-type)
270 (wt "(" (host-type->c-name dest-host-type) ")(" loc ")"))
271 ((eq loc-host-type :object)
272 ;; We relax the check a bit, because it is valid in C to coerce
273 ;; between floats of different types.
274 (ensure-valid-object-type 'FLOAT)
275 (wt-from-object-conversion dest-type loc-type dest-host-type loc))
276 (t
277 (coercion-error))))
278 ((:csfloat :cdfloat :clfloat)
279 (cond
280 ((c-number-host-type-p loc-host-type)
281 (wt "(" (host-type->c-name dest-host-type) ")(" loc ")"))
282 ((eq loc-host-type :object)
283 ;; We relax the check a bit, because it is valid in C to coerce
284 ;; between COMPLEX floats of different types.
285 (ensure-valid-object-type 'SI:COMPLEX-FLOAT)
286 (wt-from-object-conversion dest-type loc-type dest-host-type loc))
287 (t
288 (coercion-error))))
289 ((:bool)
290 (cond
291 ((c-number-host-type-p loc-host-type)
292 (wt "1"))
293 ((eq loc-host-type :object)
294 (wt "(" loc ")!=ECL_NIL"))
295 (t
296 (coercion-error))))
297 ((:object)
298 (case loc-host-type
299 ((:int-sse-pack :float-sse-pack :double-sse-pack)
300 (when (>= (cmp-env-optimization 'speed) 1)
301 (cmpwarn-style "Boxing a value of type ~S - performance degraded."
302 loc-host-type))))
303 (wt-to-object-conversion loc-host-type loc))
304 ((:pointer-void)
305 (case loc-host-type
306 ((:object)
307 (wt-from-object-conversion dest-type loc-type dest-host-type loc))
308 ((:cstring)
309 (wt "(char *)(" loc ")"))
310 (otherwise
311 (coercion-error))))
312 ((:cstring)
313 (coercion-error))
314 ((:char*)
315 (case loc-host-type
316 ((:object)
317 (wt "ecl_base_string_pointer_safe(" loc ")"))
318 ((:pointer-void)
319 (wt "(char *)(" loc ")"))
320 (otherwise
321 (coercion-error))))
322 ((:int-sse-pack :float-sse-pack :double-sse-pack)
323 (case loc-host-type
324 ((:object)
325 (wt-from-object-conversion 'ext:sse-pack loc-type dest-host-type loc))
326 ;; Implicitly cast between SSE subtypes
327 ((:int-sse-pack :float-sse-pack :double-sse-pack)
328 (wt (ecase dest-host-type
329 (:int-sse-pack (ecase loc-host-type
330 (:float-sse-pack "_mm_castps_si128")
331 (:double-sse-pack "_mm_castpd_si128")))
332 (:float-sse-pack (ecase loc-host-type
333 (:int-sse-pack "_mm_castsi128_ps")
334 (:double-sse-pack "_mm_castpd_ps")))
335 (:double-sse-pack (ecase loc-host-type
336 (:int-sse-pack "_mm_castsi128_pd")
337 (:float-sse-pack "_mm_castps_pd"))))
338 "(" loc ")"))
339 (otherwise
340 (coercion-error))))
341 (t
342 ;; At this point we only have coercions to integers
343 (cond
344 ((not (c-integer-host-type-p dest-host-type))
345 (coercion-error))
346 ((c-number-host-type-p loc-host-type)
347 (wt "(" (host-type->c-name dest-host-type) ")(" loc ")"))
348 ((eq :object loc-host-type)
349 (ensure-valid-object-type dest-type)
350 (wt-from-object-conversion dest-type loc-type dest-host-type loc))
351 (t
352 (coercion-error))))))))
353
354
355 ;;;
356 ;;; INLINE-LOC
357 ;;;
358
359 (defun wt-c-inline-loc (output-host-type c-expression coerced-arguments side-effects output-vars)
360 (declare (ignore output-host-type side-effects))
361 (with-input-from-string (s c-expression)
362 (when (and output-vars (not (eq output-vars 'VALUES)))
363 (wt-nl))
364 (do ((c (read-char s nil nil)
365 (read-char s nil nil)))
366 ((null c))
367 (case c
368 (#\@
369 (let ((object (read s)))
370 (unless (and (consp object) (eq (car object) 'CL:RETURN))
371 (cmperr "Used @~s in C-INLINE form. Expected syntax is @(RETURN ...)." object))
372 (if (eq output-vars 'VALUES)
373 (cmperr "Used @(RETURN ...) in a C-INLINE form with no output values.")
374 (let ((ndx (or (second object) 0))
375 (l (length output-vars)))
376 (if (< ndx l)
377 (wt (nth ndx output-vars))
378 (cmperr "Used @(RETURN ~D) in a C-INLINE form with ~D output values."
379 ndx l))))))
380 (#\#
381 (let* ((k (read-char s))
382 (next-char (peek-char nil s nil nil))
383 (index (digit-char-p k 36)))
384 (cond ((eq k #\#)
385 (wt #\#))
386 ((or (null index) (and next-char (alphanumericp next-char)))
387 (wt #\# k))
388 ((< index (length coerced-arguments))
389 (wt (nth index coerced-arguments)))
390 (t
391 (cmperr "C-INLINE: Variable code exceeds number of arguments")))))
392 (otherwise
393 (write-char c *compiler-output1*))))))
394
395
396 ;;;
397 ;;; SET-LOC
398 ;;;
399
400 ;;; Setting the location requires some cooperation between the code that returns
401 ;;; the location and the code that assigns it. By default we treat all RVALs as
402 ;;; having a single value (that's how wt-loc dispatches all known locations).
403 ;;;
404 ;;; This "default" changes when the LVAL expects multiple values. In that case
405 ;;; the SET-LOC method checks whether the RVAL may return multiple values:
406 ;;;
407 ;;; - when it does, then we use these values and do nothing
408 ;;; - when unknown, then we update values[0] and leave nvalues as is
409 ;;; - otherwise we update values[0] and reset nvalues = 1
410 ;;;
411 ;;; The "unknown" requires some explanation. The predicate (USES-VALUES loc)
412 ;;; returns true for locations that possibly can return multiple values. The
413 ;;; most representative example are function calls - the number of returned
414 ;;; values may even change at runtime, because the function may be recompiled.
415 ;;;
416 ;;; The contract between the caller and the callee is that the callee will
417 ;;; ensure upon exit, that nvalues contains the correct value, and that the
418 ;;; returned value is the primary value. When the callee returns only a single
419 ;;; value then it does not update VALUES vector to avoid global memory writes.
420 ;;; This is why LVALs that accept multiple values must assign VALUES[0] when the
421 ;;; (USES-VALUES RVAL) returns T. -- jd 2023-12-14
422
423 (defun set-unknown-loc (destination loc)
424 (unknown-location 'set-loc destination))
425
426 (defun set-loc (destination loc &aux fd)
427 (cond ((eq destination loc))
428 ((symbolp destination)
429 (funcall (gethash destination *set-loc-dispatch-table* 'set-unknown-loc)
430 loc))
431 ((var-p destination)
432 (set-var loc destination))
433 ((vv-p destination)
434 (set-vv loc destination))
435 ((atom destination)
436 (unknown-location 'set-loc destination loc))
437 (t
438 (ext:if-let ((fd (gethash (first destination) *set-loc-dispatch-table*)))
439 (apply fd loc (rest destination))
440 (progn
441 (wt-nl)
442 (wt-loc destination)
443 (wt " = " (coerce-loc (loc-host-type destination) loc) ";"))))))
444
445 (defun set-trash-loc (loc &rest args)
446 (declare (ignore args))
447 (when (loc-with-side-effects-p loc)
448 (wt-nl loc ";")
449 t))
450
451 (defun set-value0-loc (loc)
452 (wt-nl "value0 = " (coerce-loc :object loc) ";"))
453
454 (defun set-the-loc (loc type orig-loc)
455 (declare (ignore type))
456 (set-loc orig-loc loc))
457
458 (defun set-leave-loc (loc)
459 (cond ((or (eq loc 'VALUEZ) (uses-values loc))
460 (wt-nl "value0 = " (coerce-loc :object loc) ";"))
461 ((eq loc 'VALUE0)
462 (wt-nl "cl_env_copy->nvalues = 1;"))
463 ((eq loc 'LEAVE))
464 (t
465 (wt-nl "value0 = " (coerce-loc :object loc) ";")
466 (wt-nl "cl_env_copy->nvalues = 1;"))))
467
468 (defun set-valuez-loc (loc)
469 (cond ((eq loc 'VALUEZ))
470 ((uses-values loc)
471 (wt-nl "cl_env_copy->values[0] = " (coerce-loc :object loc) ";"))
472 (t
473 (wt-nl "cl_env_copy->values[0] = " (coerce-loc :object loc) ";")
474 (wt-nl "cl_env_copy->nvalues = 1;"))))
475
476 ;;;
477 ;;; Foreign data
478 ;;;
479
480 (defun wt-ffi-data-ref (data ffi-tag)
481 (wt "ecl_foreign_data_ref_elt(&" data "," ffi-tag ")"))
482
483 (defun wt-ffi-data-set (value data ffi-tag)
484 (wt "ecl_foreign_data_set_elt(&" data "," ffi-tag "," value ");"))