1 | (* Pasted from Pottier's PP compiler *) |
---|
2 | |
---|
3 | open ERTL |
---|
4 | |
---|
5 | (* In the following, a ``variable'' means a pseudo-register or an |
---|
6 | allocatable hardware register. *) |
---|
7 | |
---|
8 | (* These functions allow turning an [ERTL] control flow graph into an |
---|
9 | explicit graph, that is, making successor edges explicit. This is |
---|
10 | useful in itself and facilitates the computation of predecessor |
---|
11 | edges. *) |
---|
12 | |
---|
13 | let statement_successors (stmt : statement) = |
---|
14 | match stmt with |
---|
15 | | St_return _ -> |
---|
16 | Label.Set.empty |
---|
17 | | St_skip l |
---|
18 | | St_comment (_, l) |
---|
19 | | St_cost (_, l) |
---|
20 | | St_set_hdw (_, _, l) |
---|
21 | | St_get_hdw (_, _, l) |
---|
22 | | St_hdw_to_hdw (_, _, l) |
---|
23 | | St_newframe l |
---|
24 | | St_delframe l |
---|
25 | | St_framesize (_, l) |
---|
26 | | St_push (_, l) |
---|
27 | | St_pop (_, l) |
---|
28 | | St_addrH (_, _, l) |
---|
29 | | St_addrL (_, _, l) |
---|
30 | | St_int (_, _, l) |
---|
31 | | St_move (_, _, l) |
---|
32 | | St_opaccs (_, _, _, _, l) |
---|
33 | | St_op1 (_, _, _, l) |
---|
34 | | St_op2 (_, _, _, _, l) |
---|
35 | | St_clear_carry l |
---|
36 | | St_load (_, _, _, l) |
---|
37 | | St_store (_, _, _, l) |
---|
38 | | St_call_id (_, _, l) -> |
---|
39 | Label.Set.singleton l |
---|
40 | | St_condacc (_, l1, l2) -> |
---|
41 | Label.Set.add l1 (Label.Set.singleton l2) |
---|
42 | |
---|
43 | (* The analysis uses the lattice of sets of variables. The lattice's |
---|
44 | join operation is pointwise set union, which reflects the fact that |
---|
45 | a variable is deemed live at a program point if and only if it is |
---|
46 | live at any of the successors of that program point. *) |
---|
47 | |
---|
48 | module L = struct |
---|
49 | |
---|
50 | (* A set of variable is represented as a pair of a set of |
---|
51 | pseudo-registers and a set of hardware registers. *) |
---|
52 | |
---|
53 | type t = |
---|
54 | Register.Set.t * I8051.RegisterSet.t |
---|
55 | |
---|
56 | type property = |
---|
57 | t |
---|
58 | |
---|
59 | let bottom = |
---|
60 | Register.Set.empty, I8051.RegisterSet.empty |
---|
61 | |
---|
62 | let psingleton r = |
---|
63 | Register.Set.singleton r, I8051.RegisterSet.empty |
---|
64 | |
---|
65 | let hsingleton hwr = |
---|
66 | Register.Set.empty, I8051.RegisterSet.singleton hwr |
---|
67 | |
---|
68 | let join (rset1, hwrset1) (rset2, hwrset2) = |
---|
69 | (Register.Set.union rset1 rset2, I8051.RegisterSet.union hwrset1 hwrset2) |
---|
70 | |
---|
71 | let diff (rset1, hwrset1) (rset2, hwrset2) = |
---|
72 | (Register.Set.diff rset1 rset2, I8051.RegisterSet.diff hwrset1 hwrset2) |
---|
73 | |
---|
74 | let equal (rset1, hwrset1) (rset2, hwrset2) = |
---|
75 | Register.Set.equal rset1 rset2 && I8051.RegisterSet.equal hwrset1 hwrset2 |
---|
76 | |
---|
77 | let is_maximal _ = |
---|
78 | false |
---|
79 | |
---|
80 | end |
---|
81 | |
---|
82 | module Label_ImperativeMap = struct |
---|
83 | |
---|
84 | type key = |
---|
85 | Label.Map.key |
---|
86 | |
---|
87 | type 'data t = |
---|
88 | 'data Label.Map.t ref |
---|
89 | |
---|
90 | let create () = |
---|
91 | ref Label.Map.empty |
---|
92 | |
---|
93 | let clear t = |
---|
94 | t := Label.Map.empty |
---|
95 | |
---|
96 | let add k d t = |
---|
97 | t := Label.Map.add k d !t |
---|
98 | |
---|
99 | let find k t = |
---|
100 | Label.Map.find k !t |
---|
101 | |
---|
102 | let iter f t = |
---|
103 | Label.Map.iter f !t |
---|
104 | |
---|
105 | end |
---|
106 | |
---|
107 | module F = Fix.Make (Label_ImperativeMap) (L) |
---|
108 | |
---|
109 | (* These are the sets of variables defined at (written by) a statement. *) |
---|
110 | |
---|
111 | let defined (stmt : statement) : L.t = |
---|
112 | match stmt with |
---|
113 | | St_skip _ |
---|
114 | | St_comment _ |
---|
115 | | St_cost _ |
---|
116 | | St_push _ |
---|
117 | | St_clear_carry _ |
---|
118 | | St_store _ |
---|
119 | | St_condacc _ |
---|
120 | | St_return _ -> |
---|
121 | L.bottom |
---|
122 | | St_op2 (I8051.Add, r, _, _, _) |
---|
123 | | St_op2 (I8051.Addc, r, _, _, _) |
---|
124 | | St_op2 (I8051.Sub, r, _, _, _) -> |
---|
125 | L.join (L.hsingleton I8051.carry) (L.psingleton r) |
---|
126 | | St_op1 (I8051.Inc, r, _, _) -> |
---|
127 | L.join (L.hsingleton I8051.carry) (L.psingleton r) |
---|
128 | | St_get_hdw (r, _, _) |
---|
129 | | St_framesize (r, _) |
---|
130 | | St_pop (r, _) |
---|
131 | | St_int (r, _, _) |
---|
132 | | St_addrH (r, _, _) |
---|
133 | | St_addrL (r, _, _) |
---|
134 | | St_move (r, _, _) |
---|
135 | | St_opaccs (_, r, _, _, _) |
---|
136 | | St_op1 (_, r, _, _) |
---|
137 | | St_op2 (_, r, _, _, _) |
---|
138 | | St_load (r, _, _, _) -> |
---|
139 | L.psingleton r |
---|
140 | | St_set_hdw (r, _, _) |
---|
141 | | St_hdw_to_hdw (r, _, _) -> |
---|
142 | L.hsingleton r |
---|
143 | | St_call_id _ -> |
---|
144 | (* Potentially destroys all caller-save hardware registers. *) |
---|
145 | Register.Set.empty, I8051.caller_saved |
---|
146 | | St_newframe _ |
---|
147 | | St_delframe _ -> |
---|
148 | L.join (L.hsingleton I8051.spl) (L.hsingleton I8051.sph) |
---|
149 | |
---|
150 | let set_of_list rl = |
---|
151 | List.fold_right I8051.RegisterSet.add rl I8051.RegisterSet.empty |
---|
152 | |
---|
153 | (* This is the set of variables used at (read by) a statement. *) |
---|
154 | |
---|
155 | let dptr = |
---|
156 | I8051.RegisterSet.add I8051.dph (I8051.RegisterSet.singleton I8051.dpl) |
---|
157 | |
---|
158 | let used (stmt : statement) : L.t = |
---|
159 | match stmt with |
---|
160 | | St_skip _ |
---|
161 | | St_comment _ |
---|
162 | | St_cost _ |
---|
163 | | St_framesize _ |
---|
164 | | St_pop _ |
---|
165 | | St_addrH _ |
---|
166 | | St_addrL _ |
---|
167 | | St_int _ |
---|
168 | | St_clear_carry _ -> |
---|
169 | L.bottom |
---|
170 | | St_call_id (_, nparams, _) -> |
---|
171 | (* Reads the hardware registers that are used to pass parameters. *) |
---|
172 | Register.Set.empty, |
---|
173 | set_of_list (MiscPottier.prefix nparams I8051.parameters) |
---|
174 | | St_get_hdw (_, r, _) |
---|
175 | | St_hdw_to_hdw (_, r, _) -> |
---|
176 | L.hsingleton r |
---|
177 | | St_set_hdw (_, r, _) |
---|
178 | | St_push (r, _) |
---|
179 | | St_move (_, r, _) |
---|
180 | | St_op1 (_, _, r, _) |
---|
181 | | St_condacc (r, _, _) -> |
---|
182 | L.psingleton r |
---|
183 | | St_op2 (I8051.Addc, _, r1, r2, _) -> |
---|
184 | L.join (L.join (L.psingleton r1) (L.psingleton r2)) |
---|
185 | (L.hsingleton I8051.carry) |
---|
186 | | St_opaccs (_, _, r1, r2, _) |
---|
187 | | St_op2 (_, _, r1, r2, _) |
---|
188 | | St_load (_, r1, r2, _) -> |
---|
189 | L.join (L.psingleton r1) (L.psingleton r2) |
---|
190 | | St_store (r1, r2, r3, _) -> |
---|
191 | L.join (L.join (L.psingleton r1) (L.psingleton r2)) (L.psingleton r3) |
---|
192 | | St_newframe _ |
---|
193 | | St_delframe _ -> |
---|
194 | L.join (L.hsingleton I8051.spl) (L.hsingleton I8051.sph) |
---|
195 | | St_return _ -> |
---|
196 | Register.Set.empty, I8051.RegisterSet.union I8051.callee_saved dptr |
---|
197 | |
---|
198 | (* A statement is considered pure if it has no side effect, that is, if |
---|
199 | its only effect is to write a value to its destination variable. |
---|
200 | |
---|
201 | A pure statement whose destination is dead after the statement will |
---|
202 | be eliminated during the translation of [ERTL] to [LTL]. This is done by |
---|
203 | replacing the statement with an [St_skip] statement. |
---|
204 | |
---|
205 | [eliminable liveafter stmt] returns [Some l], where [l] is [stmt]'s single |
---|
206 | successor, if statement [stmt] is eliminable. Otherwise, it returns |
---|
207 | [None]. The parameter [liveafter] is the set of variables that are live |
---|
208 | after the statement. *) |
---|
209 | |
---|
210 | let eliminable ((pliveafter, hliveafter) : L.t) (stmt : statement) = |
---|
211 | match stmt with |
---|
212 | | St_skip _ |
---|
213 | | St_comment _ |
---|
214 | | St_cost _ |
---|
215 | | St_newframe _ |
---|
216 | | St_delframe _ |
---|
217 | | St_pop _ |
---|
218 | | St_push _ |
---|
219 | | St_clear_carry _ |
---|
220 | | St_store _ |
---|
221 | | St_call_id _ |
---|
222 | | St_condacc _ |
---|
223 | | St_return _ -> |
---|
224 | None |
---|
225 | | St_get_hdw (r, _, l) |
---|
226 | | St_framesize (r, l) |
---|
227 | | St_int (r, _, l) |
---|
228 | | St_addrH (r, _, l) |
---|
229 | | St_addrL (r, _, l) |
---|
230 | | St_move (r, _, l) |
---|
231 | | St_opaccs (_, r, _, _, l) |
---|
232 | | St_op1 (_, r, _, l) |
---|
233 | | St_op2 (_, r, _, _, l) |
---|
234 | | St_load (r, _, _, l) -> |
---|
235 | if (Register.Set.mem r pliveafter) || |
---|
236 | (I8051.RegisterSet.mem I8051.carry hliveafter) then None else Some l |
---|
237 | | St_set_hdw (r, _, l) |
---|
238 | | St_hdw_to_hdw (r, _, l) -> |
---|
239 | if I8051.RegisterSet.mem r hliveafter then None else Some l |
---|
240 | |
---|
241 | (* This is the abstract semantics of instructions. It defines the |
---|
242 | variables that are live before the instruction in terms of |
---|
243 | those that are live after the instruction. *) |
---|
244 | |
---|
245 | (* The standard definition is: a variable is considered live |
---|
246 | before the instruction if either (1) it is used by the instruction, |
---|
247 | or (2) it is live after the instruction and not defined by the |
---|
248 | instruction. |
---|
249 | |
---|
250 | As an exception to this rule, if the instruction is eliminable, |
---|
251 | then a variable is considered live before the instruction |
---|
252 | if and only if it is live after the instruction. This anticipates |
---|
253 | on the instruction's elimination. |
---|
254 | |
---|
255 | This exception means that the source variables of a pure |
---|
256 | instruction need not be considered live if the instruction's result |
---|
257 | is unused. This allows a sequence of pure instructions whose end |
---|
258 | result is dead to be considered entirely dead. |
---|
259 | |
---|
260 | It is a simple, but not entirely trivial, exercise to check that |
---|
261 | this transfer function is monotone. *) |
---|
262 | |
---|
263 | let statement_semantics (stmt : statement) (liveafter : L.t) : L.t = |
---|
264 | match eliminable liveafter stmt with |
---|
265 | | None -> |
---|
266 | L.join (L.diff liveafter (defined stmt)) (used stmt) |
---|
267 | | Some _ -> |
---|
268 | liveafter |
---|
269 | |
---|
270 | (* A valuation is a function that maps a program point (a control flow |
---|
271 | graph label) to the set of variables that are live after that |
---|
272 | point. *) |
---|
273 | |
---|
274 | type valuation = |
---|
275 | Label.t -> L.t |
---|
276 | |
---|
277 | (* This is how we turn an [ERTL] procedure into a liveness analysis |
---|
278 | problem and solve it. *) |
---|
279 | |
---|
280 | let analyze (int_fun : internal_function) : valuation = |
---|
281 | |
---|
282 | (* Formulate the problem. Construct a system (recursive) equations |
---|
283 | that describe the live variables before and after each |
---|
284 | instruction. *) |
---|
285 | |
---|
286 | (* The following two functions, [livebefore] and [liveafter], |
---|
287 | define these equations. Both use an oracle, a valuation -- |
---|
288 | also called [liveafter] -- which is supposed to tell us |
---|
289 | which variables are live after each label. *) |
---|
290 | |
---|
291 | (* The live variables before an instruction are computed, using the |
---|
292 | instruction's semantics, in terms of the live variables after the |
---|
293 | instruction -- which are given by the oracle. *) |
---|
294 | |
---|
295 | let livebefore label (liveafter : valuation) : L.t = |
---|
296 | let stmt : statement = Label.Map.find label int_fun.f_graph in |
---|
297 | statement_semantics stmt (liveafter label) |
---|
298 | in |
---|
299 | |
---|
300 | (* The live variables after an instruction are the union of the live |
---|
301 | variables before each of the instruction's successors. *) |
---|
302 | |
---|
303 | let liveafter label (liveafter : valuation) : L.t = |
---|
304 | let stmt : statement = Label.Map.find label int_fun.f_graph in |
---|
305 | Label.Set.fold (fun successor accu -> |
---|
306 | L.join (livebefore successor liveafter) accu |
---|
307 | ) (statement_successors stmt) L.bottom |
---|
308 | in |
---|
309 | |
---|
310 | (* Compute the least fixed point of these recursive equations. *) |
---|
311 | |
---|
312 | F.lfp liveafter |
---|