1 | include "ASM/Util.ma". |
---|
2 | include "ERTL/ERTL.ma". |
---|
3 | include "utilities/adt/set_adt.ma". |
---|
4 | include "utilities/fixpoints.ma". |
---|
5 | |
---|
6 | definition rl_included ≝ |
---|
7 | λleft,right. |
---|
8 | set_subset … (eq_identifier RegisterTag) (\fst left) (\fst right) ∧ |
---|
9 | set_subset … eq_Register (\snd left) (\snd right). |
---|
10 | |
---|
11 | definition register_lattice : property_lattice ≝ |
---|
12 | mk_property_lattice |
---|
13 | ((set register) × (set Register)) |
---|
14 | 〈set_empty …, set_empty …〉 |
---|
15 | (λleft. |
---|
16 | λright. |
---|
17 | set_equal … (eq_identifier ?) (\fst left) (\fst right) ∧ |
---|
18 | set_equal … eq_Register (\snd left) (\snd right)) |
---|
19 | rl_included |
---|
20 | (λ_.false). |
---|
21 | |
---|
22 | definition rl_bottom ≝ l_bottom register_lattice. |
---|
23 | definition rl_psingleton: register → register_lattice ≝ |
---|
24 | λr. |
---|
25 | 〈set_singleton … r, set_empty …〉. |
---|
26 | definition rl_hsingleton: Register → register_lattice ≝ |
---|
27 | λr. |
---|
28 | 〈set_empty …, set_singleton … r〉. |
---|
29 | |
---|
30 | definition pairwise : ∀A,B : Type[0]. ∀f : A → A → A. ∀g: B → B → B. |
---|
31 | A × B → A × B → A × B≝ |
---|
32 | λA,B,f,g,c1,c2.〈f (\fst c1) (\fst c2) , g (\snd c1) (\snd c2)〉. |
---|
33 | |
---|
34 | definition rl_join: register_lattice → register_lattice → register_lattice ≝ |
---|
35 | pairwise ?? (set_union …) (set_union …). |
---|
36 | |
---|
37 | definition rl_diff: register_lattice → register_lattice → register_lattice ≝ |
---|
38 | pairwise ?? (set_diff …) (set_diff …). |
---|
39 | |
---|
40 | definition defined ≝ |
---|
41 | λglobals: list ident. |
---|
42 | λs: joint_statement ERTL globals. |
---|
43 | match s with |
---|
44 | [ sequential seq l ⇒ |
---|
45 | match seq with |
---|
46 | [ step_seq s ⇒ |
---|
47 | match s with |
---|
48 | [ OP2 op2 r1 r2 _ ⇒ |
---|
49 | match op2 with |
---|
50 | [ Add ⇒ rl_join (rl_hsingleton RegisterCarry) (rl_psingleton r1) |
---|
51 | | Addc ⇒ rl_join (rl_hsingleton RegisterCarry) (rl_psingleton r1) |
---|
52 | | Sub ⇒ rl_join (rl_hsingleton RegisterCarry) (rl_psingleton r1) |
---|
53 | | _ ⇒ rl_psingleton r1 |
---|
54 | ] |
---|
55 | | CLEAR_CARRY ⇒ rl_hsingleton RegisterCarry |
---|
56 | | SET_CARRY ⇒ rl_hsingleton RegisterCarry |
---|
57 | | OPACCS opaccs dr1 dr2 sr1 sr2 ⇒ |
---|
58 | rl_join (rl_join (rl_psingleton dr1) |
---|
59 | (rl_psingleton dr2)) |
---|
60 | (rl_hsingleton RegisterCarry) |
---|
61 | | OP1 op1 r1 r2 ⇒ rl_psingleton r1 |
---|
62 | | POP r ⇒ rl_psingleton r |
---|
63 | | ADDRESS _ _ _ r1 r2 ⇒ rl_join (rl_psingleton r1) (rl_psingleton r2) |
---|
64 | | LOAD r _ _ ⇒ rl_psingleton r |
---|
65 | | COMMENT c ⇒ rl_bottom |
---|
66 | | STORE acc_a dpl dph ⇒ rl_bottom |
---|
67 | | PUSH r ⇒ rl_bottom |
---|
68 | | MOVE pair_reg ⇒ |
---|
69 | (* first register relevant only *) |
---|
70 | match \fst pair_reg with |
---|
71 | [ PSD p ⇒ rl_psingleton p |
---|
72 | | HDW h ⇒ rl_hsingleton h |
---|
73 | ] |
---|
74 | | extension_seq ext ⇒ |
---|
75 | match ext with |
---|
76 | [ ertl_frame_size r ⇒ rl_psingleton r |
---|
77 | ] |
---|
78 | (* Potentially destroys all caller-save hardware registers. *) |
---|
79 | ] |
---|
80 | | CALL _ _ _ ⇒ 〈set_empty …, set_from_list … RegisterCallerSaved〉 |
---|
81 | | COND r lbl_true ⇒ rl_bottom |
---|
82 | | COST_LABEL clabel ⇒ rl_bottom |
---|
83 | ] |
---|
84 | | final _ ⇒ rl_bottom |
---|
85 | | FCOND abs _ _ _ ⇒ Ⓧabs |
---|
86 | ]. |
---|
87 | |
---|
88 | definition ret_regs ≝ set_from_list … RegisterRets. |
---|
89 | |
---|
90 | definition rl_arg : psd_argument → register_lattice ≝ |
---|
91 | λarg.match arg with |
---|
92 | [ Imm _ ⇒ rl_bottom |
---|
93 | | Reg r ⇒ rl_psingleton r |
---|
94 | ]. |
---|
95 | |
---|
96 | definition used ≝ |
---|
97 | λglobals: list ident. |
---|
98 | λs: joint_statement ERTL globals. |
---|
99 | match s with |
---|
100 | [ sequential seq l ⇒ |
---|
101 | match seq with |
---|
102 | [ step_seq s ⇒ |
---|
103 | match s with |
---|
104 | [ OP2 op2 acc_a r1 r2 ⇒ |
---|
105 | rl_join (rl_join (rl_arg r1) (rl_arg r2)) |
---|
106 | (match op2 with |
---|
107 | [ Addc ⇒ rl_hsingleton RegisterCarry |
---|
108 | | Sub ⇒ rl_hsingleton RegisterCarry |
---|
109 | | _ ⇒ rl_bottom |
---|
110 | ]) |
---|
111 | (* acc_a and acc_b *) |
---|
112 | | OPACCS opaccs dr1 dr2 sr1 sr2 ⇒ |
---|
113 | rl_join (rl_arg sr1) (rl_arg sr2) |
---|
114 | | OP1 op1 r1 r2 ⇒ rl_psingleton r2 |
---|
115 | | LOAD acc_a dpl dph ⇒ rl_join (rl_arg dpl) (rl_arg dph) |
---|
116 | | STORE acc_a dpl dph ⇒ |
---|
117 | rl_join (rl_join (rl_arg acc_a) (rl_arg dpl)) (rl_arg dph) |
---|
118 | | PUSH r ⇒ rl_arg r |
---|
119 | | MOVE pair_reg ⇒ |
---|
120 | let r2 ≝ \snd pair_reg in |
---|
121 | match r2 with |
---|
122 | [ Reg p ⇒ |
---|
123 | match p with |
---|
124 | [ PSD r ⇒ rl_psingleton r |
---|
125 | | HDW r ⇒ rl_hsingleton r |
---|
126 | ] |
---|
127 | | Imm _ ⇒ rl_bottom |
---|
128 | ] |
---|
129 | | extension_seq ext ⇒ |
---|
130 | match ext with |
---|
131 | [ ertl_frame_size r ⇒ rl_bottom ] |
---|
132 | (* Reads the hardware registers that are used to pass parameters. *) |
---|
133 | | _ ⇒ rl_bottom |
---|
134 | ] |
---|
135 | | COST_LABEL clabel ⇒ rl_bottom |
---|
136 | | CALL f nparams _ ⇒ |
---|
137 | rl_join (match f with |
---|
138 | [ inl _ ⇒ rl_bottom |
---|
139 | | inr pr ⇒ rl_join (rl_arg (\fst pr)) (rl_arg (\snd pr)) |
---|
140 | ]) 〈set_empty …, set_from_list … (prefix ? nparams RegisterParams)〉 |
---|
141 | | COND r lbl_true ⇒ rl_psingleton r |
---|
142 | ] |
---|
143 | | final fin ⇒ |
---|
144 | match fin with |
---|
145 | [ RETURN ⇒ 〈set_empty …, set_union … (set_from_list … RegisterCalleeSaved) ret_regs〉 |
---|
146 | | GOTO l ⇒ rl_bottom |
---|
147 | | TAILCALL abs _ _ ⇒ match abs in False with [ ] |
---|
148 | ] |
---|
149 | | FCOND abs _ _ _ ⇒ Ⓧabs |
---|
150 | ]. |
---|
151 | |
---|
152 | definition eliminable_step ≝ |
---|
153 | λglobals: list ident. |
---|
154 | λl: register_lattice. |
---|
155 | λs: joint_step ERTL globals. |
---|
156 | let pliveafter ≝ \fst l in |
---|
157 | let hliveafter ≝ \snd l in |
---|
158 | match s with |
---|
159 | [ step_seq s ⇒ |
---|
160 | match s with |
---|
161 | [ OP2 op2 r1 r2 r3 ⇒ |
---|
162 | ¬(match op2 with |
---|
163 | [ Add ⇒ set_member … eq_Register RegisterCarry hliveafter |
---|
164 | | Addc ⇒ set_member … eq_Register RegisterCarry hliveafter |
---|
165 | | Sub ⇒ set_member … eq_Register RegisterCarry hliveafter |
---|
166 | | _ ⇒ false |
---|
167 | ] ∨ set_member … (eq_identifier …) r1 pliveafter) |
---|
168 | | OPACCS opaccs dr1 dr2 sr1 sr2 ⇒ |
---|
169 | ¬(set_member … (eq_identifier …) dr1 pliveafter ∨ |
---|
170 | set_member … (eq_identifier …) dr2 pliveafter ∨ |
---|
171 | set_member … eq_Register RegisterCarry hliveafter) |
---|
172 | | OP1 op1 r1 r2 ⇒ |
---|
173 | ¬set_member … (eq_identifier …) r1 pliveafter |
---|
174 | | ADDRESS _ _ _ r1 r2 ⇒ |
---|
175 | ¬(set_member … (eq_identifier …) r1 pliveafter ∨ |
---|
176 | set_member … (eq_identifier …) r2 pliveafter) |
---|
177 | | LOAD acc_a dpl dph ⇒ |
---|
178 | ¬set_member ? (eq_identifier …) acc_a pliveafter |
---|
179 | | MOVE pair_reg ⇒ |
---|
180 | ¬match \fst pair_reg with |
---|
181 | [ PSD p1 ⇒ |
---|
182 | set_member … (eq_identifier …) p1 pliveafter |
---|
183 | | HDW h1 ⇒ |
---|
184 | set_member … eq_Register h1 hliveafter |
---|
185 | ] |
---|
186 | | extension_seq ext ⇒ |
---|
187 | match ext with |
---|
188 | [ ertl_frame_size r ⇒ |
---|
189 | ¬set_member ? (eq_identifier RegisterTag) r pliveafter |
---|
190 | ] |
---|
191 | | _ ⇒ false |
---|
192 | ] |
---|
193 | | _ ⇒ false |
---|
194 | ]. |
---|
195 | |
---|
196 | definition eliminable ≝ |
---|
197 | λglobals: list ident. |
---|
198 | λl: register_lattice. |
---|
199 | λs: joint_statement ERTL globals. |
---|
200 | let pliveafter ≝ \fst l in |
---|
201 | let hliveafter ≝ \snd l in |
---|
202 | match s with |
---|
203 | [ sequential seq _ ⇒ eliminable_step … l seq |
---|
204 | | _ ⇒ false |
---|
205 | ]. |
---|
206 | |
---|
207 | definition statement_semantics: ∀globals: list ident. |
---|
208 | joint_statement ERTL globals → register_lattice → register_lattice ≝ |
---|
209 | λglobals. |
---|
210 | λstmt. |
---|
211 | λliveafter. |
---|
212 | if eliminable globals liveafter stmt then |
---|
213 | liveafter |
---|
214 | else |
---|
215 | rl_join (rl_diff liveafter (defined globals stmt)) (used globals stmt). |
---|
216 | |
---|
217 | definition livebefore: |
---|
218 | ∀globals: list ident. |
---|
219 | joint_internal_function ERTL globals → |
---|
220 | valuation register_lattice → valuation register_lattice |
---|
221 | ≝ |
---|
222 | λglobals: list ident. |
---|
223 | λint_fun: joint_internal_function ERTL globals. |
---|
224 | λliveafter: valuation register_lattice. |
---|
225 | λlabel. |
---|
226 | match lookup ?? (joint_if_code … int_fun) label with |
---|
227 | [ None ⇒ rl_bottom |
---|
228 | | Some stmt ⇒ statement_semantics globals stmt (liveafter label) |
---|
229 | ]. |
---|
230 | |
---|
231 | definition liveafter ≝ |
---|
232 | λglobals: list ident. |
---|
233 | λint_fun: joint_internal_function ERTL globals. |
---|
234 | λlabel. |
---|
235 | λliveafter: valuation register_lattice. |
---|
236 | match lookup ?? (joint_if_code … int_fun) label with |
---|
237 | [ None ⇒ rl_bottom |
---|
238 | | Some stmt ⇒ |
---|
239 | \fold[rl_join,rl_bottom]_{successor ∈ stmt_labels … stmt} |
---|
240 | (livebefore globals int_fun liveafter successor) |
---|
241 | ]. |
---|
242 | |
---|
243 | definition analyse_liveness ≝ |
---|
244 | λthe_fixpoint: fixpoint_computer. λglobals,int_fun. |
---|
245 | the_fixpoint ? (liveafter globals int_fun). |
---|
246 | |
---|
247 | definition vertex ≝ register + Register. |
---|
248 | |
---|
249 | definition p_in_lattice : register → register_lattice → bool ≝ |
---|
250 | λvertex.λprop.set_member ? (eq_identifier RegisterTag) vertex (\fst prop). |
---|
251 | definition h_in_lattice : Register → register_lattice → bool ≝ |
---|
252 | λvertex.λprop.set_member ? eq_Register vertex (\snd prop). |
---|
253 | |
---|
254 | definition in_lattice : vertex → register_lattice → bool ≝ |
---|
255 | λvertex. |
---|
256 | match vertex with |
---|
257 | [ inl v ⇒ p_in_lattice v |
---|
258 | | inr v ⇒ h_in_lattice v |
---|
259 | ]. |
---|