Debian Codegraph @

src/cmp/cmpbackend-cxx/cmppass2-var.lsp

raw
1 ;;;;
2 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
3 ;;;; Copyright (c) 1990, Giuseppe Attardi
4 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
5 ;;;; Copyright (c) 2021, Daniel Kochmański
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 (in-package #:compiler)
11
12 (defun c2let-replaceable-var-ref-p (var form rest-forms)
13 (when (and (eq (c1form-name form) 'VARIABLE)
14 (null (var-set-nodes var))
15 (local-var-p var))
16 (let ((var1 (c1form-arg 0 form)))
17 (declare (type var var1))
18 ;; FIXME We should be able to replace variable even if they are referenced
19 ;; across functions. We just need to keep track of their uses.
20 (when (and (local-var-p var1)
21 (eq (unboxed-var-p var) (unboxed-var-p var1))
22 (not (var-changed-in-form-list var1 rest-forms)))
23 (cmpdebug "Replacing variable ~a by its value" (var-name var))
24 (nsubst-var var form)
25 t))))
26
27 (defun locative-type-from-var-kind (kind)
28 (cdr (assoc kind
29 '((:object . "_ecl_object_loc")
30 (:fixnum . "_ecl_fixnum_loc")
31 (:char . "_ecl_base_char_loc")
32 (:float . "_ecl_float_loc")
33 (:double . "_ecl_double_loc")
34 (:long-double . "_ecl_long_double_loc")
35 #+complex-float (:csfloat . "_ecl_csfloat_loc")
36 #+complex-float (:cdfloat . "_ecl_cdfloat_loc")
37 #+complex-float (:clfloat . "_ecl_clfloat_loc")
38 #+sse2 (:int-sse-pack . "_ecl_int_sse_pack_loc")
39 #+sse2 (:float-sse-pack . "_ecl_float_sse_pack_loc")
40 #+sse2 (:double-sse-pack . "_ecl_double_sse_pack_loc")
41 ((special global closure lexical) . NIL)))))
42
43 (defun build-debug-lexical-env (var-locations &optional first)
44 #-:msvc ;; FIXME! Problem with initialization of statically defined vectors
45 (let* ((filtered-locations '())
46 (filtered-codes '()))
47 ;; Filter out variables that we know how to store in the debug information
48 ;; table. This excludes among other things closures and special variables.
49 (loop for var in var-locations
50 for name = (let ((*package* (find-package "KEYWORD")))
51 (format nil "\"~S\"" (var-name var)))
52 for code = (locative-type-from-var-kind (var-kind var))
53 for loc = (var-loc var)
54 when (and code (consp loc) (eq (first loc) 'LCL))
55 do (progn
56 (push (cons name code) filtered-codes)
57 (push loc filtered-locations)))
58 ;; Generate two tables, a static one with information about the variables,
59 ;; including name and type, and dynamic one, which is a vector of pointer to
60 ;; the variables.
61 (when filtered-codes
62 (setf *ihs-used-p* t)
63 (wt-nl "static const struct ecl_var_debug_info _ecl_descriptors[]={")
64 (loop for (name . code) in filtered-codes
65 for i from 0
66 do (wt-nl (if (zerop i) "{" ",{") name "," code "}"))
67 (wt "};")
68 (wt-nl "const cl_index _ecl_debug_info_raw[]={")
69 (wt-nl (if first "(cl_index)(ECL_NIL)," "(cl_index)(_ecl_debug_env),")
70 "(cl_index)(_ecl_descriptors)")
71 (loop for var-loc in filtered-locations
72 do (wt ",(cl_index)(&" var-loc ")"))
73 (wt "};")
74 (wt-nl "ecl_def_ct_vector(_ecl_debug_env,ecl_aet_index,_ecl_debug_info_raw,"
75 (+ 2 (length filtered-locations))
76 ",,);")
77 (unless first
78 (wt-nl "ihs.lex_env = _ecl_debug_env;")))
79 filtered-codes))
80
81 (defun pop-debug-lexical-env ()
82 (wt-nl "ihs.lex_env = _ecl_debug_env;"))
83
84 (defun c2let* (c1form vars forms body
85 &aux
86 (*volatile* (c1form-volatile* c1form))
87 (*unwind-exit* *unwind-exit*)
88 (*env* *env*)
89 (*env-lvl* *env-lvl*)
90 (*inline-blocks* 0))
91 ;; Replace read-only variables when it is worth doing it.
92 (loop for var in vars
93 for rest-forms on (append forms (list body))
94 for form = (first rest-forms)
95 unless (c2let-replaceable-var-ref-p var form rest-forms)
96 collect var into used-vars and
97 collect form into used-forms
98 finally (setf vars used-vars forms used-forms))
99
100 ;; Emit C definitions of local variables
101 (loop for var in vars
102 for kind = (local-var-p var)
103 do (when kind
104 (maybe-open-inline-block)
105 (bind (next-lcl (var-name var)) var)
106 (wt-nl *volatile* (host-type->c-name kind) " " var ";")))
107
108 ;; Create closure bindings for closed-over variables
109 (when (some #'var-ref-ccb vars)
110 (maybe-open-inline-block)
111 (let ((env-lvl *env-lvl*))
112 (wt-nl *volatile* "cl_object env" (incf *env-lvl*) " = env" env-lvl ";")))
113
114 ;; Assign values
115 (loop for form in forms
116 for var in vars
117 do (case (var-kind var)
118 ((LEXICAL CLOSURE SPECIAL GLOBAL)
119 (case (c1form-name form)
120 (LOCATION (bind (c1form-arg 0 form) var))
121 (VARIABLE (bind (c1form-arg 0 form) var))
122 (t (bind-init form var))))
123 (t ; local var
124 (let ((*destination* var)) ; nil (ccb)
125 (c2expr* form)))))
126
127 ;; Optionally register the variables with the IHS frame for debugging
128 (if (policy-debug-variable-bindings)
129 (let ((*unwind-exit* *unwind-exit*))
130 (with-lexical-scope ()
131 (ext:if-let ((env (build-debug-lexical-env vars)))
132 (progn
133 (push 'IHS-ENV *unwind-exit*)
134 (c2expr body)
135 (pop-debug-lexical-env))
136 (c2expr body))))
137 (c2expr body))
138 (close-inline-blocks))
139
140 (defun c2multiple-value-bind (c1form vars init-form body)
141 (declare (ignore c1form))
142 (let* ((*unwind-exit* *unwind-exit*)
143 (*env-lvl* *env-lvl*)
144 (*env* *env*)
145 (*lcl* *lcl*)
146 (labels nil)
147 (env-grows nil)
148 (*inline-blocks* 0)
149 min-values max-values)
150 ;; 1) Retrieve the number of output values.
151 (multiple-value-setq (min-values max-values)
152 (c1form-values-number init-form))
153 ;; 2) For all variables which are not special and do not belong to a
154 ;; closure, make a local C variable.
155 (dolist (var vars)
156 (declare (type var var))
157 (ext:if-let ((kind (local-var-p var)))
158 (when (useful-var-p var)
159 (maybe-open-inline-block)
160 (bind (next-lcl) var)
161 (wt-nl (host-type->c-name kind) " " *volatile* var ";")
162 (wt-comment (var-name var)))
163 (unless env-grows
164 (setq env-grows (var-ref-ccb var)))))
165 ;; 3) If there are closure variables, set up an environment.
166 (when (setq env-grows (env-grows env-grows))
167 (let ((env-lvl *env-lvl*))
168 (maybe-open-inline-block)
169 (wt-nl "volatile cl_object env" (incf *env-lvl*)
170 " = env" env-lvl ";")))
171 ;; 4) Assign the values to the variables, compiling the form and binding the
172 ;; variables in the process.
173 (do-m-v-setq vars init-form t)
174 ;; 5) Compile the body. If there are bindings of special variables, these
175 ;; bindings are undone here.
176 (c2expr body)
177 ;; 6) Close the C expression.
178 (close-inline-blocks)))
179
180 (defun c2location (c1form loc)
181 (unwind-exit (precise-loc-lisp-type loc (c1form-primary-type c1form))))
182
183 ;;; When LOC is not NIL, then the variable is a constant.
184 (defun c2variable (c1form var loc)
185 (unwind-exit (precise-loc-lisp-type
186 (if (and loc (not (numberp (vv-location loc))))
187 loc
188 (or (try-const-c-inliner var) var))
189 (c1form-primary-type c1form))))
190
191 (defun c2setq (c1form vref form)
192 (declare (ignore c1form))
193 ;; First comes the assignement
194 (let ((*destination* vref))
195 (c2expr* form))
196 ;; Then the returned value
197 (if (eq (c1form-name form) 'LOCATION)
198 (c2location form (c1form-arg 0 form))
199 (unwind-exit vref)))
200
201 (defun c2progv (c1form symbols values body)
202 (declare (ignore c1form))
203 (let* ((*lcl* *lcl*)
204 (lcl (next-lcl))
205 (sym-loc (make-lcl-var))
206 (val-loc (make-lcl-var)))
207 (with-lexical-scope ()
208 (wt-nl "cl_object " sym-loc ", " val-loc "; cl_index " lcl ";")
209 (let ((*destination* sym-loc)) (c2expr* symbols))
210 (let ((*destination* val-loc)) (c2expr* values))
211 (let ((*unwind-exit* (cons lcl *unwind-exit*)))
212 (wt-nl lcl " = ecl_progv(cl_env_copy, " sym-loc ", " val-loc ");")
213 (c2expr body)))))
214
215 (defun c2psetq (c1form vrefs forms &aux (saves nil))
216 (declare (ignore c1form))
217 ;; similar to inline-args
218 (with-inline-blocks ()
219 (loop for var in vrefs
220 for (form . rest-forms) on forms
221 do (if (or (var-changed-in-form-list var rest-forms)
222 (var-referenced-in-form-list var rest-forms))
223 (if (eq (c1form-name form) 'LOCATION)
224 (push (cons var (c1form-arg 0 form)) saves)
225 (let* ((typ (c1form-primary-type form))
226 (rep (var-host-type var))
227 (tmp (emit-inlined-temp-var form typ rep)))
228 (push (cons var tmp) saves)))
229 (let ((*destination* var))
230 (c2expr* form))))
231 (dolist (save saves)
232 (set-var (cdr save) (car save))))
233 (unwind-exit *vv-nil*))
234
235 ;;; bind must be called for each variable in a lambda or let, once the value
236 ;;; to be bound has been placed in loc.
237 ;;; bind takes care of setting var-loc.
238
239 (defun bind (loc var)
240 ;; loc can be either (LCL n), 'VA-ARGS, (KEYVARS n), (CAR n),
241 ;; a constant, or (VAR var) from a let binding. ; ccb
242 (declare (type var var))
243 (case (var-kind var)
244 (CLOSURE
245 (let ((var-loc (var-loc var)))
246 (unless (typep var-loc 'fixnum)
247 ;; first binding: assign location
248 (setq var-loc (next-env))
249 (setf (var-loc var) var-loc))
250 (when (zerop var-loc) (wt-nl "env" *env-lvl* " = ECL_NIL;"))
251 (wt-nl "CLV" var-loc " = env" *env-lvl*
252 " = CONS(" (coerce-loc :object loc) ",env" *env-lvl* ");")
253 (wt-comment (var-name var))))
254 (LEXICAL
255 (let ((var-loc (var-loc var)))
256 (unless (consp var-loc)
257 ;; first binding: assign location
258 (setq var-loc (next-lex))
259 (setf (var-loc var) var-loc))
260 (wt-nl) (wt-lex var-loc) (wt " = " (coerce-loc :object loc) ";"))
261 (wt-comment (var-name var)))
262 ((SPECIAL GLOBAL)
263 (bds-bind loc var))
264 (t
265 (cond ((not (eq (var-loc var) 'OBJECT))
266 ;; already has location (e.g. optional in lambda list)
267 ;; check they are not the same
268 (unless (equal (var-loc var) loc)
269 (wt-nl var " = " (coerce-loc (var-host-type var) loc) ";")))
270 ((and (consp loc) (eql (car loc) 'LCL))
271 ;; set location for lambda list requireds
272 (setf (var-loc var) loc))
273 (t
274 (baboon :format-control "bind: unexpected var-kind (~s) and loc (~s)."
275 :format-arguments (list (var-kind var) loc)))))))
276
277 ;;; Used by let*, defmacro and lambda's &aux, &optional, &rest, &keyword
278 (defun bind-init (form var)
279 (let ((kind (var-kind var)))
280 (if (member kind '(CLOSURE LEXICAL SPECIAL GLOBAL))
281 ;; Binding these variables is complicated and involves lexical
282 ;; environments, global environments, etc. If we use `(BIND var)
283 ;; as destination, BIND might receive the wrong environment.
284 (with-inline-blocks ()
285 (let ((locs (coerce-args (inline-args (list form)))))
286 (bind (first locs) var)
287 ;; Notice that we do not need to update *UNWIND-EXIT* because BIND
288 ;; does it for us.
289 ))
290 ;; The simple case of a variable which is local to a function.
291 (let ((*destination* `(BIND ,var)))
292 (c2expr* form)))))
293
294 (defun bds-bind (loc var)
295 ;; Optimize the case (let ((*special-var* *special-var*)) ...)
296 (cond ((and (var-p loc)
297 (member (var-kind loc) '(global special))
298 (eq (var-name loc) (var-name var)))
299 (wt-nl "ecl_bds_push(cl_env_copy," (var-loc var) ");"))
300 (t
301 (wt-nl "ecl_bds_bind(cl_env_copy," (var-loc var) ","
302 (coerce-loc :object loc) ");")))
303 (push 'BDS-BIND *unwind-exit*)
304 (wt-comment (var-name var)))
305
306 (defun bind-or-set (loc v use-bind)
307 (declare (si::c-local))
308 (cond ((not use-bind)
309 (set-var loc v))
310 ((or (plusp (var-ref v))
311 (member (var-kind v) '(SPECIAL GLOBAL)))
312 (bind loc v))))
313
314 (defun values-loc-or-value0 (i)
315 (declare (si::c-local))
316 (if (plusp i)
317 (list 'VALUE i)
318 'VALUE0))
319
320 (defun do-m-v-setq (vars form use-bind)
321 ;; This routine moves values from the multiple-value stack into the
322 ;; variables VARS. The amount of values is not known (or at least we only
323 ;; know that there is some number between MIN-VALUES and MAX-VALUES). If
324 ;; the values are to be created with BIND, then USED-BIND=T. The output of
325 ;; this routine is a location containing the first value (typically, the
326 ;; name of the first variable).
327 (declare (si::c-local))
328 (when (= (length vars) 1)
329 (let ((*destination* (first vars)))
330 (c2expr* form)
331 (return-from do-m-v-setq *destination*)))
332
333 ;; Store the values in the values stack + value0. Try guessing how
334 ;; many they are.
335 (multiple-value-bind (min-values max-values)
336 (c1form-values-number form)
337 (declare (ignore max-values))
338
339 ;; We save the values in the value stack + value0
340 (let ((*destination* 'LEAVE))
341 (c2expr* form))
342
343 ;; At least we always have NIL value0
344 (setf min-values (max 1 min-values))
345
346 (let* ((*lcl* *lcl*)
347 (useful-extra-vars (some #'useful-var-p (nthcdr min-values vars)))
348 (nr (make-lcl-var :type :int)))
349 (with-lexical-scope ()
350 (when useful-extra-vars
351 ;; Make a copy of env->nvalues before assigning to any variables
352 (wt-nl "const int " nr " = cl_env_copy->nvalues;"))
353
354 ;; We know that at least MIN-VALUES variables will get a value
355 (dotimes (i min-values)
356 (when vars
357 (let ((v (pop vars))
358 (loc (values-loc-or-value0 i)))
359 (bind-or-set loc v use-bind))))
360
361 ;; Assign to other variables only when the form returns enough values
362 (when useful-extra-vars
363 (let ((tmp (make-lcl-var)))
364 (wt-nl "cl_object " tmp ";")
365 (loop for v in vars
366 for i from min-values
367 for loc = (values-loc-or-value0 i)
368 do (when (useful-var-p v)
369 (wt-nl tmp " = (" nr "<=" i ")? ECL_NIL : " loc ";")
370 (bind-or-set tmp v use-bind)))))))
371 'VALUE0))
372
373 (defun c2multiple-value-setq (c1form vars form)
374 (declare (ignore c1form))
375 (unwind-exit (do-m-v-setq vars form nil)))
376
377 (defun wt-var (var)
378 (declare (type var var))
379 (let ((var-loc (var-loc var)))
380 (case (var-kind var)
381 (CLOSURE (wt-env var-loc))
382 (LEXICAL (wt-lex var-loc))
383 ((SPECIAL GLOBAL)
384 (if (safe-compile)
385 (wt "ecl_cmp_symbol_value(cl_env_copy," var-loc ")")
386 (wt "ECL_SYM_VAL(cl_env_copy," var-loc ")")))
387 (t (wt var-loc)))))
388
389 (defun set-var (loc var &aux (var-loc (var-loc var))) ; ccb
390 (unless (var-p var)
391 (baboon :format-control "set-var: ~s is not a variable."
392 :format-arguments (list var)))
393 (case (var-kind var)
394 (CLOSURE
395 (wt-nl)(wt-env var-loc)(wt " = " (coerce-loc (var-host-type var) loc) ";"))
396 (LEXICAL
397 (wt-nl)(wt-lex var-loc)(wt " = " (coerce-loc (var-host-type var) loc) ";"))
398 ((SPECIAL GLOBAL)
399 (if (safe-compile)
400 (wt-nl "ecl_cmp_setq(cl_env_copy," var-loc ",")
401 (wt-nl "ECL_SETQ(cl_env_copy," var-loc ","))
402 (wt (coerce-loc (var-host-type var) loc) ");"))
403 (t
404 (wt-nl var-loc " = " (coerce-loc (var-host-type var) loc) ";"))))
405
406 (defun wt-lcl (lcl)
407 (unless (numberp lcl)
408 (baboon :format-control "wt-lcl: ~s NaN"
409 :format-arguments (list lcl)))
410 (wt "v" lcl))
411
412 (defun wt-lcl-loc (lcl &optional type name)
413 (declare (ignore type))
414 (unless (numberp lcl)
415 (baboon :format-control "wt-lcl-loc: ~s NaN"
416 :format-arguments (list lcl)))
417 (wt "v" lcl name))
418
419 (defun wt-lex (lex)
420 (if (consp lex)
421 (wt "lex" (car lex) "[" (cdr lex) "]")
422 (wt-lcl lex)))
423
424 (defun wt-temp (temp)
425 (wt "T" temp))
426
427 ;;; reference to variable of inner closure.
428 (defun wt-env (clv) (wt "ECL_CONS_CAR(CLV" clv ")"))