Debian Codegraph @

src/cmp/cmplocs.lsp

raw
1
2 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
3 ;;;; Copyright (c) 1990, Giuseppe Attardi
4 ;;;; Copyright (c) 2003, Juan Jose Garcia-Ripoll
5 ;;;; Copyright (c) 2023, Daniel Kochmański
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8
9 (in-package #:compiler)
10
11 ;;; ----------------------------------------------------------------------------
12 ;;; LOCATIONS and representation types
13 ;;;
14 ;;; Locations are lisp expressions which represent actual target (i.e C) data.
15 ;;; To each location we can associate a representation type, which is the type
16 ;;; of the target data (i.e uint32_t).
17
18 ;;; The following routines help in determining these types, and also in moving
19 ;;; data from one location to another.
20
21 ;;; VV represents an object referenced in the compiled program. In the first
22 ;;; pass VV instances are added to the vector *REFERENCED-OBJECT*, and in the
23 ;;; second pass the backend decides if the referenced object can be inlined, or
24 ;;; if it needs to be put in the data segment and initialized at load-time.
25
26 (defstruct (vv (:constructor %make-vv))
27 (location nil)
28 (used-p nil)
29 (always nil) ; when true then vv is never optimized
30 (permanent-p t)
31 (value nil)
32 (type nil)
33 (host-type :object))
34
35 (defun make-vv (&rest args &key location used-p always permanent-p value type host-type)
36 (declare (ignore location used-p always permanent-p host-type))
37 (unless type
38 ;; When the value is the "empty location" then it was created to be filled
39 ;; later and the real type of the object is not known. See DATA-EMPTY-LOC.
40 (setf type (if (eq value *empty-loc*) t (type-of value))))
41 (apply #'%make-vv :type type args))
42
43 ;;; TODO investigate where raw numbers are stemming from and change them to VV
44 ;;; TODO tighter checks for compound locations (cons)
45 ;;; TODO baboon on locations that are unknown (for now we signal a warning)
46 ;;;
47 ;;; -- jd 2023-12-07
48
49 (defun loc-lisp-type (loc)
50 (typecase loc
51 (var (var-type loc))
52 (vv (vv-type loc))
53 (number (type-of loc))
54 (atom
55 (case loc
56 (FRAME++ 'FIXNUM)
57 ((TRASH LEAVE VALUE0 VA-ARG VALUEZ CL-VA-ARG) T)
58 (otherwise
59 (cmpwarn "LOC-LISP-TYPE: unknown location ~s." loc)
60 T)))
61 (otherwise
62 (case (first loc)
63 (FFI:C-INLINE (let ((type (first (second loc))))
64 (cond ((and (consp type) (eq (first type) 'VALUES)) T)
65 ((lisp-type-p type) type)
66 (t (host-type->lisp-type type)))))
67 (BIND (var-type (second loc)))
68 (LCL (or (third loc) T))
69 (THE (second loc))
70 (CALL-NORMAL (fourth loc))
71 (otherwise T)))))
72
73 (defun loc-host-type (loc)
74 (typecase loc
75 (var (var-host-type loc))
76 (vv (vv-host-type loc))
77 (number (lisp-type->host-type (type-of loc)))
78 (atom
79 (case loc
80 (TRASH :void)
81 ((FRAME++ LEAVE VALUE0 VA-ARG VALUEZ CL-VA-ARG) :object)
82 (otherwise
83 (cmpwarn "LOC-LISP-TYPE: unknown location ~s." loc)
84 :object)))
85 (otherwise
86 (case (first loc)
87 (FFI:C-INLINE (let ((type (first (second loc))))
88 (cond ((and (consp type) (eq (first type) 'VALUES)) :object)
89 ((lisp-type-p type) (lisp-type->host-type type))
90 (t type))))
91 (BIND (var-host-type (second loc)))
92 (LCL (lisp-type->host-type (or (third loc) T)))
93 ((JUMP-TRUE JUMP-FALSE) :bool)
94 (THE (loc-host-type (third loc)))
95 (otherwise :object)))))
96
97 (defun loc-movable-p (loc)
98 (if (atom loc)
99 t
100 (case (first loc)
101 ((CALL CALL-LOCAL) NIL)
102 ((ffi:c-inline) (not (fifth loc))) ; side effects?
103 (otherwise t))))
104
105 (defun loc-with-side-effects-p (loc &aux name)
106 (when (atom loc)
107 (return-from loc-with-side-effects-p
108 (and (var-p loc)
109 (global-var-p loc)
110 (policy-global-var-checking))))
111 (case (first loc)
112 ((CALL CALL-NORMAL CALL-INDIRECT CALL-GLOBAL-STACK CALL-LOCAL-STACK) T)
113 (CL:THE (loc-with-side-effects-p (third loc)))
114 (CL:FDEFINITION (policy-global-function-checking))
115 ;; Uses VALUES or has side effects.
116 (FFI:C-INLINE (or (eq (sixth loc) 'CL:VALUES) (fifth loc)))
117 (otherwise NIL)))
118
119 (defun loc-refers-to-special-p (loc)
120 (flet ((special-var-p (loc)
121 (and (var-p loc)
122 (member (var-kind loc) '(SPECIAL GLOBAL)))))
123 (if (atom loc)
124 (special-var-p loc)
125 (case (first loc)
126 (CL:THE (loc-refers-to-special-p (third loc)))
127 (BIND (special-var-p (second loc)))
128 ;; We do not know, so guess yes.
129 (FFI:C-INLINE T)
130 (otherwise NIL)))))
131
132 ;;; Valid locations are:
133 ;;; VALUE0
134 ;;; VALUEZ
135 ;;; var-object
136 ;;; a string designating a C expression
137 ;;; ( VALUE i ) VALUES(i)
138 ;;; ( VV vv-index )
139 ;;; ( VV-temp vv-index )
140 ;;; ( LCL lcl [host-type]) local variable, type unboxed
141 ;;; ( TEMP temp ) local variable, type object
142 ;;; ( FRAME ndx ) variable in local frame stack
143 ;;; ( CALL-NORMAL fun locs 1st-type ) similar as CALL, but number of arguments is fixed
144 ;;; ( CALL-INDIRECT fun narg args) similar as CALL, but unknown function
145 ;;; ( CALL-LOCAL-STACK fun) similar as CALL-NORMAL, but args are on the stack
146 ;;; ( CALL-GLOBAL-STACK fun) similar as CALL-INDIRECT, but args are on the stack
147 ;;; ( FFI:C-INLINE output-type fun/string locs side-effects output-var )
148 ;;; ( COERCE-LOC host-type location)
149 ;;; ( FDEFINITION vv-index )
150 ;;; ( MAKE-CCLOSURE cfun )
151 ;;; ( STACK-POINTER index ) retrieve a value from the stack
152 ;;; ( SYS:STRUCTURE-REF loc slot-name-vv slot-index )
153 ;;; ( THE type location )
154 ;;; ( KEYVARS n )
155 ;;; VA-ARG
156 ;;; CL-VA-ARG
157
158 (defun precise-loc-lisp-type (loc new-type)
159 (let ((loc-type (loc-lisp-type loc)))
160 (if (subtypep loc-type new-type *cmp-env*)
161 loc
162 `(the ,(type-and loc-type new-type) ,loc))))
163
164 (defun loc-in-c1form-movable-p (loc)
165 "A location that is in a C1FORM and can be moved"
166 (cond ((numberp loc)
167 t)
168 ((stringp loc)
169 t)
170 ((vv-p loc)
171 t)
172 ((member loc '(VALUE0 VALUEZ VA-ARG CL-VA-ARG))
173 nil)
174 ((atom loc)
175 (baboon :format-control "Unknown location ~A found in C1FORM"
176 :format-arguments (list loc)))
177 ((eq (first loc) 'THE)
178 (loc-in-c1form-movable-p (third loc)))
179 ((member (setf loc (car loc))
180 '(VV VV-TEMP KEYVARS))
181 t)
182 (t
183 (baboon :format-control "Unknown location ~A found in C1FORM"
184 :format-arguments (list loc)))))
185
186 (defun uses-values (loc)
187 (and (consp loc)
188 (or (member (car loc) '(CALL-NORMAL CALL-INDIRECT CALL-GLOBAL-STACK CALL-LOCAL-STACK) :test #'eq)
189 (and (eq (car loc) 'ffi:C-INLINE)
190 (eq (sixth loc) 'cl:VALUES)))))
191
192 (defun loc-immediate-value-p (loc)
193 (cond ((numberp loc)
194 (values t loc))
195 ((vv-p loc)
196 (let ((value (vv-value loc)))
197 (cond
198 ((eq value *empty-loc*)
199 (values nil nil))
200 ((eq value *inline-loc*)
201 (loc-immediate-value-p (vv-location loc)))
202 (t
203 (values t value)))))
204 ((atom loc)
205 (values nil nil))
206 ((eq (first loc) 'THE)
207 (loc-immediate-value-p (third loc)))
208 (t
209 (values nil nil))))
210
211 (defun loc-immediate-value (loc)
212 (nth-value 1 (loc-immediate-value-p loc)))
213
214 (defun unknown-location (where loc)
215 (baboon :format-control "Unknown location found in ~A~%~S"
216 :format-arguments (list where loc)))