Debian Codegraph @

src/cmp/cmpbackend-cxx/cmppass2-exit.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 ;;;; CMPEXIT Exit manager.
13 ;;;;
14 ;;;; The exit manager has two main operators that unwind the dynamic context:
15 ;;;;
16 ;;;; (UNWIND-EXIT value) carries VALUE to *DESTINATION* and unwinds to *EXIT*
17 ;;;; (UNWIND-JUMP label) unwinds to LABEL
18 ;;;; (UNWIND-COND label) unwinds to LABEL (conditionally)
19 ;;;; (UNWIND-FLEE label) escapes to LABEL (runtime unwind)
20 ;;;;
21
22 (in-package "COMPILER")
23
24 (defun unwind-exit (loc)
25 (flet ((unwind-cond-p ()
26 (and (consp *destination*)
27 (member (si:cons-car *destination*) '(JUMP-FALSE JUMP-TRUE))))
28 (unwind-jump-p ()
29 (labelp *exit*))
30 (unwind-exit-p ()
31 (eq *exit* 'LEAVE)))
32 (cond ((unwind-cond-p) (unwind-cjump loc))
33 ((unwind-jump-p) (unwind-label loc))
34 ((unwind-exit-p) (unwind-leave loc))
35 (t (baboon-exit-invalid *exit*)))))
36
37 (defun unwind-jump (exit)
38 (%unwind (label-denv exit) *unwind-exit*)
39 (%goto exit))
40
41 (defun unwind-cont (exit)
42 (%unwind (label-denv exit) *unwind-exit*)
43 (%goto exit))
44
45 (defun unwind-flee (exit kind)
46 (%escape exit kind))
47
48 (defun unwind-cond (exit kind &rest args)
49 (%branch exit *unwind-exit* kind args))
50
51 ;;;
52
53 (defun baboon-exit-not-found (exit)
54 (baboon :format-control "The value of exit~%~A~%is not found in *UNWIND-EXIT*~%~A"
55 :format-arguments (list exit *unwind-exit*)))
56
57 (defun baboon-exit-invalid (exit)
58 (baboon :format-control "The value of exit~%~A~%is not valid."
59 :format-arguments (list exit)))
60
61 (defun baboon-unwind-invalid (unwind-to unwind-from)
62 (baboon :format-control "The unwind value~%~A~%is not a tail of the unwind value~%~A"
63 :format-arguments (list unwind-to unwind-from)))
64
65 (defun baboon-unwind-exit (exit)
66 (baboon :format-control "The value of exit~%~A~%found in *UNWIND-EXIT*~%~A~%is not valid."
67 :format-arguments (list exit *unwind-exit*)))
68
69 (defun destination-value-matters-p (dest)
70 (declare (si::c-local))
71 (if (atom dest)
72 (not (eq dest 'TRASH))
73 (not (member (car dest) '(JUMP-FALSE JUMP-TRUE)))))
74
75 ;;; UNWIND-EXIT TAGS PURPOSE
76 ;;;
77 ;;; FRAME -> ecl_frs_push()
78 ;;; (STACK frame) -> ecl_stack_frame_open(env, frame, initial_size)
79 ;;; IHS -> ihs push
80 ;;; IHS-ENV -> ihs push
81 ;;; BDS-BIND -> binding of 1 special variable
82 ;;; (LCL n) -> binding stack pointer to Nth local variable
83 ;;; LEAVE -> outermost location
84 ;;; #<label id used-p> -> label (basic block leader)
85
86 (defun compute-unwind (unwind-to unwind-from)
87 (declare (si::c-local))
88 (unless (tailp unwind-to unwind-from)
89 (baboon-unwind-invalid unwind-to unwind-from))
90 (loop with bds-lcl = nil
91 with bds-bind = 0
92 with stack-frame = nil
93 with ihs-p = nil
94 with frs-bind = 0
95 with jump-p = nil
96 with exit-p = nil
97 for unwind-exit on unwind-from
98 for ue = (car unwind-exit)
99 until (eq unwind-exit unwind-to)
100 do (cond
101 ((consp ue)
102 (case (first ue)
103 (STACK (setq stack-frame (second ue)))
104 (LCL (setq bds-lcl ue bds-bind 0))
105 (otherwise (baboon-unwind-exit ue))))
106 ((labelp ue)
107 (setf jump-p t))
108 ((eq ue 'BDS-BIND)
109 (incf bds-bind))
110 ((eq ue 'FRAME)
111 (incf frs-bind))
112 ((eq ue 'IHS)
113 (setf ihs-p ue))
114 ((eq ue 'IHS-ENV)
115 (setf ihs-p (or ihs-p ue)))
116 ((eq ue 'LEAVE)
117 (setf exit-p t))
118 (t (baboon-unwind-exit ue)))
119 finally (return (values frs-bind bds-lcl bds-bind stack-frame ihs-p jump-p exit-p))))
120
121 (defun unwind-leave (loc)
122 (declare (si::c-local))
123 ;; *destination* must be either LEAVE or TRASH.
124 (unless (member loc '(VALUEZ LEAVE))
125 (set-loc 'LEAVE loc)
126 (setf loc 'LEAVE))
127 (%unwind nil *unwind-exit*)
128 (%exit loc))
129
130 (defun unwind-label (loc)
131 (declare (si::c-local))
132 (let* ((exit *exit*)
133 (dest *destination*)
134 (from *unwind-exit*)
135 (exit-denv (member exit from :test #'eq)))
136 (unless exit-denv
137 (baboon-exit-not-found exit))
138 (if (and (destination-value-matters-p dest)
139 (loc-refers-to-special-p dest))
140 ;; Save the value if destination may possibly refer to a special
141 ;; binding. Otherwise we set the destination /before/ the unwind.
142 (let* ((*temp* *temp*)
143 (temp (make-temp-var)))
144 (set-loc temp loc)
145 (%unwind exit-denv from)
146 (set-loc dest temp))
147 (progn
148 (set-loc dest loc)
149 (%unwind exit-denv from)))
150 (%jump exit from)))
151
152 ;;; Conditional JUMP based on the value of *DESTINATION*. This allows FMLA to
153 ;;; jump over *EXIT* to skip the dead part of the computation. -- jd 2023-11-16
154 (defun unwind-cjump (loc)
155 (declare (si::c-local))
156 (multiple-value-bind (constantp value) (loc-immediate-value-p loc)
157 (destructuring-bind (target label) *destination*
158 (ecase target
159 (JUMP-TRUE
160 (cond ((not constantp)
161 (unwind-cond label :jump-t loc))
162 ((not (null value))
163 (unwind-jump label)))
164 (unless (and constantp (not (null value)))
165 (let ((*destination* 'TRASH))
166 (if (labelp *exit*)
167 (unwind-label *vv-nil*)
168 (unwind-leave *vv-nil*)))))
169 (JUMP-FALSE
170 (cond ((not constantp)
171 (unwind-cond label :jump-f loc))
172 ((null value)
173 (unwind-jump label)))
174 (unless (and constantp (null value))
175 (let ((*destination* 'TRASH))
176 (if (labelp *exit*)
177 (unwind-label *vv-t*)
178 (unwind-leave *vv-t*)))))))))
179
180
181 ;;; Helper functions
182 ;;;
183 ;;; These functions will be moved to codegen.
184
185 ;;; INV this function arguments are procured by COMPUTE-UNWIND.
186 (defun perform-unwind (frs-bind bds-lcl bds-bind stack-frame ihs-p)
187 (declare (si::c-local)
188 (fixnum frs-bind bds-bind))
189 (when (plusp frs-bind)
190 (wt-nl "ecl_frs_pop_n(cl_env_copy, " frs-bind ");"))
191 (when stack-frame
192 (wt-nl "ecl_stack_frame_close(" stack-frame ");"))
193 (when bds-lcl
194 (wt-nl "ecl_bds_unwind(cl_env_copy," bds-lcl ");"))
195 (if (< bds-bind 4)
196 (dotimes (n bds-bind)
197 (declare (ignorable n))
198 (wt-nl "ecl_bds_unwind1(cl_env_copy);"))
199 (wt-nl "ecl_bds_unwind_n(cl_env_copy," bds-bind ");"))
200 (case ihs-p
201 (IHS (wt-nl "ecl_ihs_pop(cl_env_copy);"))
202 (IHS-ENV (wt-nl "ihs.lex_env = _ecl_debug_env;"))))
203
204 (defun %unwind (into from)
205 (declare (si::c-local))
206 (multiple-value-bind (frs-bind bds-lcl bds-bind stack-frame ihs-p)
207 (compute-unwind into from)
208 (perform-unwind frs-bind bds-lcl bds-bind stack-frame ihs-p)))
209
210 (defun %branch (exit from kind args)
211 (ecase kind
212 (:jump-t
213 (destructuring-bind (loc) args
214 (case (loc-host-type loc)
215 (:bool (wt-nl "if (" loc ") "))
216 (:object (wt-nl "if (" loc "!=ECL_NIL) "))
217 (otherwise (wt-nl "if ((" (coerce-loc :object loc) ")!=ECL_NIL) ")))))
218 (:jump-f
219 (destructuring-bind (loc) args
220 (case (loc-host-type loc)
221 (:bool (wt-nl "if (!(" loc "))"))
222 (:object (wt-nl "if (Null(" loc "))"))
223 (otherwise (wt-nl "if (Null(" (coerce-loc :object loc) "))")))))
224 (:jump-eq
225 (destructuring-bind (x y) args
226 (wt-nl "if (" (coerce-loc :object x) "==" (coerce-loc :object y) ") "))))
227 (wt-open-brace)
228 (multiple-value-bind (frs-bind bds-lcl bds-bind stack-frame ihs-p)
229 (compute-unwind (label-denv exit) from)
230 (perform-unwind frs-bind bds-lcl bds-bind stack-frame ihs-p)
231 (wt-nl-go exit))
232 (wt-nl-close-brace))
233
234 (defun %escape (exit kind)
235 ;; All these boil down to calling ecl_unwind which unwinds stacks dynamically.
236 ;; If we want to implement call/cc, then this is the place where we dispatch.
237 #+ (or) (wt-nl "ecl_unwind(cl_env_copy," frs-id ");")
238 (ecase kind
239 (:go
240 ;; The second argument is passed as a value (index for jump).
241 (wt-nl "cl_go(" (tag-var exit) ",ecl_make_fixnum(" (tag-index exit) "));"))
242 (:throw
243 ;; Unlike GO and RETURN-FROM, the destination is not known at compile time.
244 ;; TODO in some cases it is possible to prove the destination CATCH form.
245 (wt-nl "cl_throw(" exit ");"))
246 (:return-from
247 ;; The second argument is used only to signal the error.
248 (wt-nl "cl_return_from(" (blk-var exit) "," (get-object (blk-name exit)) ");"))))
249
250 ;;; JUMP is similar to %GOTO, but it allows a fallthough.
251 (defun %jump (label from)
252 (unless (eq label (find-if #'labelp from))
253 (wt-nl-go label)))
254
255 (defun %goto (label)
256 (wt-nl-go label))
257
258 (defun %exit (loc)
259 (wt-nl "return " loc ";"))