1 | |
---|
2 | include "LTL/LTL.ma". |
---|
3 | include "ERTL/Interference.ma". |
---|
4 | include "ASM/Arithmetic.ma". |
---|
5 | include "joint/TranslateUtils.ma". |
---|
6 | |
---|
7 | (* Note: translation is complicated by having to preserve the carry bit and |
---|
8 | wanting to do it with as less boilerplate as possible. It could be somewhat |
---|
9 | simplified if constant and copy propagation was to be done after this pass: |
---|
10 | those optimisations would take care of the boilerplate for us.*) |
---|
11 | |
---|
12 | coercion Reg_to_dec : ∀r:Register.decision ≝ decision_colour on _r : Register to decision. |
---|
13 | |
---|
14 | inductive arg_decision : Type[0] ≝ |
---|
15 | | arg_decision_colour : Register → arg_decision |
---|
16 | | arg_decision_spill : ℕ → arg_decision |
---|
17 | | arg_decision_imm : beval → arg_decision. |
---|
18 | |
---|
19 | coercion Reg_to_arg_dec : ∀r:Register.arg_decision ≝ arg_decision_colour on _r : Register to arg_decision. |
---|
20 | |
---|
21 | (* Paolo: I'm changing the following: before, spilled registers were |
---|
22 | assigned stack addresses going from SP + #frame_size - #stack_params |
---|
23 | excluded down to SP included. I am turning it upside down, so that |
---|
24 | the offset does not need the stack size to be computed *) |
---|
25 | |
---|
26 | definition preserve_carry_bit : |
---|
27 | ∀globals.bool → list (joint_seq LTL globals) → list (joint_seq LTL globals) ≝ |
---|
28 | λglobals,do_it,steps. |
---|
29 | if do_it then SAVE_CARRY :: steps @ [RESTORE_CARRY] else steps. |
---|
30 | |
---|
31 | (* for notation *) |
---|
32 | definition A ≝ it. |
---|
33 | |
---|
34 | coercion beval_of_byte : ∀b : Byte.beval ≝ BVByte on _b : Byte to beval. |
---|
35 | |
---|
36 | (* spill should be byte-based from the start *) |
---|
37 | definition set_dp_by_offset : |
---|
38 | ∀globals.nat → list (joint_seq LTL globals) ≝ |
---|
39 | λglobals,off. |
---|
40 | [ A ← byte_of_nat off |
---|
41 | ; A ← A .Add. RegisterSPL |
---|
42 | ; RegisterDPL ← A |
---|
43 | ; A ← zero_byte |
---|
44 | ; A ← A .Addc. RegisterSPH |
---|
45 | ; RegisterDPH ← A |
---|
46 | ]. |
---|
47 | |
---|
48 | definition get_stack: |
---|
49 | ∀globals.Register → nat → list (joint_seq LTL globals) ≝ |
---|
50 | λglobals,r,off. |
---|
51 | set_dp_by_offset ? off @ |
---|
52 | [ LOAD … A it it ] @ |
---|
53 | if eq_Register r RegisterA then [ ] else [ r ← A ]. |
---|
54 | |
---|
55 | definition set_stack_not_a : |
---|
56 | ∀globals.nat → Register → list (joint_seq LTL globals) ≝ |
---|
57 | λglobals,off,r. |
---|
58 | set_dp_by_offset ? off @ |
---|
59 | [ A ← r |
---|
60 | ; STORE … it it A ]. |
---|
61 | |
---|
62 | definition set_stack_a : |
---|
63 | ∀globals.nat → list (joint_seq LTL globals) ≝ |
---|
64 | λglobals,off. |
---|
65 | [ RegisterST1 ← A ] @ |
---|
66 | set_stack_not_a ? off RegisterST1. |
---|
67 | |
---|
68 | definition set_stack : |
---|
69 | ∀globals.nat → Register → list (joint_seq LTL globals) ≝ |
---|
70 | λglobals,off,r. |
---|
71 | if eq_Register r RegisterA then |
---|
72 | set_stack_a ? off |
---|
73 | else |
---|
74 | set_stack_not_a ? off r. |
---|
75 | |
---|
76 | definition set_stack_int : |
---|
77 | ∀globals.nat → beval → list (joint_seq LTL globals) ≝ |
---|
78 | λglobals,off,int. |
---|
79 | set_dp_by_offset ? off @ |
---|
80 | [ A ← int |
---|
81 | ; STORE … it it A ]. |
---|
82 | |
---|
83 | definition move : |
---|
84 | ∀globals.bool → decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
85 | λglobals,carry_lives_after,dst,src. |
---|
86 | match dst with |
---|
87 | [ decision_colour dstr ⇒ |
---|
88 | match src with |
---|
89 | [ arg_decision_colour srcr ⇒ |
---|
90 | if eq_Register dstr srcr then [ ] else |
---|
91 | if eq_Register dstr RegisterA then [ A ← srcr ] else |
---|
92 | if eq_Register srcr RegisterA then [ dstr ← A ] else |
---|
93 | [ A ← srcr ; dstr ← A] |
---|
94 | | arg_decision_spill srco ⇒ |
---|
95 | preserve_carry_bit ? carry_lives_after |
---|
96 | (get_stack ? dstr srco) |
---|
97 | | arg_decision_imm int ⇒ |
---|
98 | [ A ← int ] @ |
---|
99 | if eq_Register dstr RegisterA then [ ] else |
---|
100 | [ dstr ← A ] |
---|
101 | ] |
---|
102 | | decision_spill dsto ⇒ |
---|
103 | match src with |
---|
104 | [ arg_decision_colour srcr ⇒ |
---|
105 | preserve_carry_bit ? carry_lives_after |
---|
106 | (set_stack ? dsto srcr) |
---|
107 | | arg_decision_spill srco ⇒ |
---|
108 | if eqb srco dsto then [ ] else |
---|
109 | preserve_carry_bit ? carry_lives_after |
---|
110 | (get_stack ? RegisterA srco @ |
---|
111 | set_stack ? dsto RegisterA) |
---|
112 | | arg_decision_imm int ⇒ |
---|
113 | preserve_carry_bit ? carry_lives_after |
---|
114 | (set_stack_int ? dsto int) |
---|
115 | ] |
---|
116 | ]. |
---|
117 | |
---|
118 | definition arg_is_spilled : arg_decision → bool ≝ |
---|
119 | λx.match x with [ arg_decision_spill _ ⇒ true | _ ⇒ false ]. |
---|
120 | definition is_spilled : decision → bool ≝ |
---|
121 | λx.match x with [ decision_spill _ ⇒ true | _ ⇒ false ]. |
---|
122 | |
---|
123 | definition newframe : |
---|
124 | ∀globals.ℕ → list (joint_seq LTL globals) ≝ |
---|
125 | λglobals,stack_sz. |
---|
126 | [ CLEAR_CARRY … |
---|
127 | ; A ← RegisterSPL |
---|
128 | ; A ← A .Sub. byte_of_nat stack_sz |
---|
129 | ; RegisterSPL ← A |
---|
130 | ; A ← RegisterSPH |
---|
131 | ; A ← A .Sub. zero_byte |
---|
132 | ; RegisterSPL ← A |
---|
133 | ]. |
---|
134 | |
---|
135 | definition delframe : |
---|
136 | ∀globals.ℕ → list (joint_seq LTL globals) ≝ |
---|
137 | λglobals,stack_sz. |
---|
138 | [ A ← RegisterSPL |
---|
139 | ; A ← A .Add. byte_of_nat stack_sz |
---|
140 | ; RegisterSPL ← A |
---|
141 | ; A ← RegisterSPH |
---|
142 | ; A ← A .Addc. zero_byte |
---|
143 | ; RegisterSPL ← A |
---|
144 | ]. |
---|
145 | |
---|
146 | definition commutative : Op2 → bool ≝ |
---|
147 | λop.match op with |
---|
148 | [ Add ⇒ true |
---|
149 | | Addc ⇒ true |
---|
150 | | Or ⇒ true |
---|
151 | | Xor ⇒ true |
---|
152 | | And ⇒ true |
---|
153 | | _ ⇒ false |
---|
154 | ]. |
---|
155 | |
---|
156 | definition uses_carry : Op2 → bool ≝ |
---|
157 | λop.match op with |
---|
158 | [ Addc ⇒ true |
---|
159 | | Sub ⇒ true |
---|
160 | | _ ⇒ false |
---|
161 | ]. |
---|
162 | |
---|
163 | definition sets_carry : Op2 → bool ≝ |
---|
164 | λop.match op with |
---|
165 | [ Add ⇒ true |
---|
166 | | Addc ⇒ true |
---|
167 | | Sub ⇒ true |
---|
168 | | _ ⇒ false |
---|
169 | ]. |
---|
170 | |
---|
171 | definition translate_op2 : |
---|
172 | ∀globals.bool→ Op2 → decision → arg_decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
173 | λglobals,carry_lives_after,op,dst,arg1,arg2. |
---|
174 | (* this won't preserve the carry bit if op does not set it: left to next function *) |
---|
175 | (* if op uses carry bit (⇒ it sets it too) it must be preserved before the op *) |
---|
176 | (preserve_carry_bit ? |
---|
177 | (uses_carry op ∧ (arg_is_spilled arg1 ∨ arg_is_spilled arg2)) |
---|
178 | (move ? false RegisterB arg2 @ |
---|
179 | move ? false RegisterA arg1) @ |
---|
180 | [ A ← A .op. RegisterB ] @ |
---|
181 | (* it op sets the carry bit and it is needed afterwards it must be preserved here *) |
---|
182 | move ? (sets_carry op ∧ carry_lives_after) dst RegisterA). |
---|
183 | |
---|
184 | definition translate_op2_smart : |
---|
185 | ∀globals.bool → Op2 → decision → arg_decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
186 | λglobals,carry_lives_after,op,dst,arg1,arg2. |
---|
187 | (* if op does not set carry bit (⇒ it does not use it either) then it must be |
---|
188 | preserved *) |
---|
189 | preserve_carry_bit ? |
---|
190 | (¬sets_carry op ∧ carry_lives_after ∧ |
---|
191 | (arg_is_spilled arg1 ∨ arg_is_spilled arg2 ∨ is_spilled dst)) |
---|
192 | (match arg2 with |
---|
193 | [ arg_decision_colour arg2r ⇒ |
---|
194 | move ? (uses_carry op) RegisterA arg1 @ |
---|
195 | [ A ← A .op. arg2r ] @ |
---|
196 | move ? (sets_carry op ∧ carry_lives_after) dst RegisterA |
---|
197 | | arg_decision_imm arg2i ⇒ |
---|
198 | move ? (uses_carry op) RegisterA arg1 @ |
---|
199 | [ A ← A .op. arg2i ] @ |
---|
200 | move ? (sets_carry op ∧ carry_lives_after) dst RegisterA |
---|
201 | | _ ⇒ |
---|
202 | if commutative op then |
---|
203 | match arg1 with |
---|
204 | [ arg_decision_colour arg1r ⇒ |
---|
205 | move ? (uses_carry op) RegisterA arg2 @ |
---|
206 | [ A ← A .op. arg1r ] @ |
---|
207 | move ? (sets_carry op ∧ carry_lives_after) dst RegisterA |
---|
208 | | arg_decision_imm arg1i ⇒ |
---|
209 | move ? (uses_carry op) RegisterA arg2 @ |
---|
210 | [ A ← A .op. arg1i ] @ |
---|
211 | move ? (sets_carry op ∧ carry_lives_after) dst RegisterA |
---|
212 | | _ ⇒ |
---|
213 | translate_op2 ? carry_lives_after op dst arg1 arg2 |
---|
214 | ] |
---|
215 | else |
---|
216 | translate_op2 ? carry_lives_after op dst arg1 arg2 |
---|
217 | ]). |
---|
218 | |
---|
219 | definition dec_to_arg_dec : decision → arg_decision ≝ |
---|
220 | λd.match d with |
---|
221 | [ decision_colour r ⇒ arg_decision_colour r |
---|
222 | | decision_spill n ⇒ arg_decision_spill n |
---|
223 | ]. |
---|
224 | |
---|
225 | coercion dec_arg_dec : ∀d:decision.arg_decision ≝ dec_to_arg_dec on _d : decision to arg_decision. |
---|
226 | |
---|
227 | definition translate_op1 : |
---|
228 | ∀globals.bool → Op1 → decision → decision → list (joint_seq LTL globals) ≝ |
---|
229 | λglobals,carry_lives_after,op,dst,arg. |
---|
230 | let preserve_carry ≝ carry_lives_after ∧ (is_spilled dst ∨ is_spilled arg) in |
---|
231 | preserve_carry_bit ? preserve_carry |
---|
232 | (move ? false RegisterA arg @ |
---|
233 | OP1 … op it it :: |
---|
234 | move ? false dst RegisterA). |
---|
235 | |
---|
236 | definition translate_opaccs : |
---|
237 | ∀globals.bool → OpAccs → decision → decision → arg_decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
238 | λglobals,carry_lives_after,op,dst1,dst2,arg1,arg2. |
---|
239 | (* OPACCS always has dead carry bit and sets it to zero *) |
---|
240 | move ? false RegisterB arg2 @ |
---|
241 | move ? false RegisterA arg1 @ |
---|
242 | OPACCS … op it it it it :: |
---|
243 | move ? false dst1 RegisterA @ |
---|
244 | move ? false dst2 RegisterB @ |
---|
245 | if carry_lives_after ∧ (is_spilled dst1 ∨ is_spilled dst2) then |
---|
246 | [CLEAR_CARRY ??] |
---|
247 | else [ ]. |
---|
248 | |
---|
249 | (* does not preserve carry bit *) |
---|
250 | definition move_to_dp : |
---|
251 | ∀globals.arg_decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
252 | λglobals,arg1,arg2. |
---|
253 | if ¬arg_is_spilled arg1 then |
---|
254 | move ? false RegisterDPH arg2 @ |
---|
255 | (* does not change dph because arg1 is not spilled *) |
---|
256 | move ? false RegisterDPL arg1 |
---|
257 | else if ¬arg_is_spilled arg2 then |
---|
258 | move ? false RegisterDPL arg1 @ |
---|
259 | (* does not change dpl because arg2 is not spilled *) |
---|
260 | move ? false RegisterDPH arg2 |
---|
261 | else |
---|
262 | (* using B as temporary, as moving spilled registers tampers with DPTR *) |
---|
263 | move ? false RegisterB arg1 @ |
---|
264 | move ? false RegisterDPH arg2 @ |
---|
265 | move ? false RegisterDPL RegisterB. |
---|
266 | |
---|
267 | definition translate_store : |
---|
268 | ∀globals.bool → arg_decision → arg_decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
269 | λglobals,carry_lives_after,addr1,addr2,src. |
---|
270 | (* requires src != RegisterA and RegisterB *) |
---|
271 | preserve_carry_bit ? (carry_lives_after ∧ |
---|
272 | (arg_is_spilled addr1 ∨ arg_is_spilled addr1 ∨ arg_is_spilled src)) |
---|
273 | (let move_to_dptr ≝ move_to_dp ? addr1 addr2 in |
---|
274 | (if arg_is_spilled src then |
---|
275 | move ? false RegisterST0 src @ |
---|
276 | move_to_dptr @ |
---|
277 | [ A ← RegisterST0] |
---|
278 | else move_to_dptr) @ |
---|
279 | [STORE … it it A]). |
---|
280 | |
---|
281 | definition translate_load : |
---|
282 | ∀globals.bool → decision → arg_decision → arg_decision → list (joint_seq LTL globals) ≝ |
---|
283 | λglobals,carry_lives_after,dst,addr1,addr2. |
---|
284 | preserve_carry_bit ? (carry_lives_after ∧ |
---|
285 | (is_spilled dst ∨ arg_is_spilled addr1 ∨ arg_is_spilled addr1)) |
---|
286 | (move_to_dp ? addr1 addr2 @ |
---|
287 | [ LOAD … A it it ] @ |
---|
288 | move ? false dst RegisterA). |
---|
289 | |
---|
290 | definition translate_address : |
---|
291 | ∀globals.bool → ∀i.member i (eq_identifier ?) globals → decision → decision → |
---|
292 | list (joint_seq LTL globals) ≝ |
---|
293 | λglobals,carry_lives_after,id,prf,addr1,addr2. |
---|
294 | preserve_carry_bit ? (carry_lives_after ∧ (is_spilled addr1 ∨ is_spilled addr2)) |
---|
295 | (ADDRESS LTL ? id prf it it :: |
---|
296 | move ? false addr1 RegisterDPL @ |
---|
297 | move ? false addr2 RegisterDPH). |
---|
298 | |
---|
299 | definition translate_step: |
---|
300 | ∀globals.∀after : valuation register_lattice. |
---|
301 | coloured_graph after → |
---|
302 | ℕ → label → joint_step ERTL globals → seq_block LTL globals (joint_step LTL globals) ≝ |
---|
303 | λglobals,after,grph,stack_sz,lbl,s. |
---|
304 | let lookup ≝ λr.colouring … grph (inl … r) in |
---|
305 | let lookup_arg ≝ λa.match a with |
---|
306 | [ Reg r ⇒ lookup r |
---|
307 | | Imm b ⇒ arg_decision_imm b |
---|
308 | ] in |
---|
309 | let carry_lives_after ≝ hlives RegisterCarry (after lbl) in |
---|
310 | let move ≝ move globals carry_lives_after in |
---|
311 | match s with |
---|
312 | [ step_seq s' ⇒ |
---|
313 | match s' return λ_.seq_block LTL globals (joint_step LTL globals) with |
---|
314 | [ COMMENT c ⇒ COMMENT … c |
---|
315 | | COST_LABEL cost_lbl ⇒ COST_LABEL … cost_lbl |
---|
316 | | POP r ⇒ |
---|
317 | POP … A :: |
---|
318 | move (lookup r) RegisterA |
---|
319 | | PUSH a ⇒ |
---|
320 | move RegisterA (lookup_arg a) @ |
---|
321 | [ PUSH … A ] |
---|
322 | | STORE addr1 addr2 srcr ⇒ |
---|
323 | translate_store ? carry_lives_after |
---|
324 | (lookup_arg addr1) |
---|
325 | (lookup_arg addr2) |
---|
326 | (lookup_arg srcr) |
---|
327 | | LOAD dstr addr1 addr2 ⇒ |
---|
328 | translate_load ? carry_lives_after |
---|
329 | (lookup dstr) |
---|
330 | (lookup_arg addr1) |
---|
331 | (lookup_arg addr2) |
---|
332 | | CLEAR_CARRY ⇒ CLEAR_CARRY … |
---|
333 | | SET_CARRY ⇒ CLEAR_CARRY … |
---|
334 | | OP2 op dst arg1 arg2 ⇒ |
---|
335 | translate_op2_smart ? carry_lives_after op |
---|
336 | (lookup dst) |
---|
337 | (lookup_arg arg1) |
---|
338 | (lookup_arg arg2) |
---|
339 | | OP1 op dst arg ⇒ |
---|
340 | translate_op1 ? carry_lives_after op |
---|
341 | (lookup dst) |
---|
342 | (lookup arg) |
---|
343 | | MOVE pair_regs ⇒ |
---|
344 | let lookup_move_dst ≝ λx.match x return λ_.decision with |
---|
345 | [ PSD r ⇒ lookup r |
---|
346 | | HDW r ⇒ r |
---|
347 | ] in |
---|
348 | let dst ≝ lookup_move_dst (\fst pair_regs) in |
---|
349 | let src ≝ |
---|
350 | match \snd pair_regs return λ_.arg_decision with |
---|
351 | [ Reg r ⇒ lookup_move_dst r |
---|
352 | | Imm b ⇒ arg_decision_imm b |
---|
353 | ] in |
---|
354 | move dst src |
---|
355 | | ADDRESS lbl prf dpl dph ⇒ |
---|
356 | translate_address ? carry_lives_after |
---|
357 | lbl prf (lookup dpl) (lookup dph) |
---|
358 | | OPACCS op dst1 dst2 arg1 arg2 ⇒ |
---|
359 | translate_opaccs ? carry_lives_after op |
---|
360 | (lookup dst1) (lookup dst2) |
---|
361 | (lookup_arg arg1) (lookup_arg arg2) |
---|
362 | | extension_seq ext ⇒ |
---|
363 | match ext with |
---|
364 | [ ertl_new_frame ⇒ newframe ? stack_sz |
---|
365 | | ertl_del_frame ⇒ delframe ? stack_sz |
---|
366 | | ertl_frame_size r ⇒ |
---|
367 | move (lookup r) (arg_decision_imm (byte_of_nat stack_sz)) |
---|
368 | ] |
---|
369 | | CALL_ID f n_args _ ⇒ CALL_ID LTL ? f n_args it |
---|
370 | | extension_call abs ⇒ match abs in void with [ ] |
---|
371 | ] |
---|
372 | | COND r ltrue ⇒ |
---|
373 | 〈move RegisterA (lookup r),COND … it ltrue〉 |
---|
374 | ]. |
---|
375 | |
---|
376 | definition translate_fin_step: |
---|
377 | ∀globals.label → joint_fin_step ERTL → seq_block LTL globals (joint_fin_step LTL) ≝ |
---|
378 | λglobals,lbl,s. |
---|
379 | match s return λ_.seq_block LTL globals (joint_fin_step LTL) with |
---|
380 | [ RETURN ⇒ RETURN ? |
---|
381 | | GOTO l ⇒ GOTO ? l |
---|
382 | | tailcall abs ⇒ match abs in void with [ ] |
---|
383 | ]. |
---|
384 | |
---|
385 | definition translate_internal: ∀globals: list ident. |
---|
386 | joint_internal_function ERTL globals → |
---|
387 | joint_internal_function LTL globals ≝ |
---|
388 | λglobals: list ident. |
---|
389 | λint_fun: joint_internal_function ERTL globals. |
---|
390 | (* initialize graph *) |
---|
391 | let entry ≝ pi1 … (joint_if_entry … int_fun) in |
---|
392 | let exit ≝ pi1 … (joint_if_exit … int_fun) in |
---|
393 | (* colour registers *) |
---|
394 | let after ≝ analyse_liveness globals int_fun in |
---|
395 | let coloured_graph ≝ build after in |
---|
396 | (* compute new stack size *) |
---|
397 | let stack_sz ≝ spilled_no … coloured_graph + joint_if_stacksize … int_fun in |
---|
398 | (* initialize internal function *) |
---|
399 | let init ≝ init_graph_if LTL globals |
---|
400 | (joint_if_luniverse … int_fun) |
---|
401 | (joint_if_runiverse … int_fun) |
---|
402 | it it [ ] stack_sz entry exit in |
---|
403 | graph_translate … |
---|
404 | init |
---|
405 | (translate_step … coloured_graph stack_sz) |
---|
406 | (translate_fin_step …) |
---|
407 | int_fun. |
---|
408 | |
---|
409 | definition ertl_to_ltl: ertl_program → ltl_program ≝ |
---|
410 | λp.transform_program … p (λvarnames. transf_fundef … (translate_internal varnames)). |
---|