source: src/RTLabs/RTLabsToRTL.ma @ 1354

Last change on this file since 1354 was 1354, checked in by sacerdot, 8 years ago

One axiom closed.

File size: 45.4 KB
Line 
1include "RTLabs/syntax.ma".
2include "RTL/RTL.ma".
3include "common/AssocList.ma".
4include "common/FrontEndOps.ma".
5include "common/Graphs.ma".
6include "joint/TranslateUtils.ma".
7include alias "ASM/BitVector.ma".
8include alias "arithmetics/nat.ma".
9
10let rec register_freshes (runiverse: universe RegisterTag) (n: nat) on n ≝
11  match n with
12  [ O ⇒ 〈[],runiverse〉
13  | S n' ⇒
14     let 〈r,runiverse〉 ≝ fresh … runiverse in
15     let 〈res,runiverse〉 ≝ register_freshes runiverse n' in
16      〈r::res,runiverse〉 ].
17
18definition complete_regs ≝
19  λglobals.
20  λdef.
21  λsrcrs1.
22  λsrcrs2.
23  if leb (length … srcrs2) (length … srcrs1) then
24   let 〈def, added_regs〉 ≝ fresh_regs rtl_params0 globals def (minus (length ? srcrs1) (length ? srcrs2)) in
25    〈〈srcrs1, srcrs2 @ added_regs〉, added_regs〉
26  else
27   let 〈def, added_regs〉 ≝ fresh_regs rtl_params0 globals def (minus (length ? srcrs2) (length ? srcrs1)) in
28    〈〈srcrs1 @ added_regs, srcrs2〉, added_regs〉.
29
30lemma complete_regs_length:
31  ∀globals,def,left,right.
32   |\fst (\fst (complete_regs globals def left right))| = |\snd (\fst (complete_regs globals def left right))|.
33 #globals #def #left #right
34 whd in match complete_regs normalize nodelta
35 @leb_elim normalize nodelta #H
36 [ generalize in match (fresh_regs_length rtl_params0 globals def (minus (length … left) (length … right)))
37 | generalize in match (fresh_regs_length rtl_params0 globals def (minus (length … right) (length … left)))]
38 cases (fresh_regs ????) #def' #fresh normalize >append_length
39 generalize in match H -H;
40 generalize in match (length … left) generalize in match (length … right) generalize in match (length … fresh)
41 [ /2/ | #x #y #z #H generalize in match (not_le_to_lt … H) -H #H #E >E >commutative_plus
42         <plus_minus_m_m /2/ ]
43qed.
44
45definition size_of_sig_type ≝
46  λsig.
47  match sig with
48  [ ASTint isize sign ⇒
49    let isize' ≝ match isize with [ I8 ⇒ 8 | I16 ⇒ 16 | I32 ⇒ 32 ] in
50      isize' ÷ (nat_of_bitvector ? int_size)
51  | ASTfloat _ ⇒ ? (* dpm: not implemented *)
52  | ASTptr rgn ⇒ nat_of_bitvector ? ptr_size
53  ].
54  cases not_implemented;
55qed.
56
57inductive register_type: Type[0] ≝
58  | register_int: register → register_type
59  | register_ptr: register → register → register_type.
60
61definition local_env ≝ BitVectorTrie (list register) 16.
62
63definition mem_local_env : register → local_env → bool ≝
64  λr. member … (word_of_identifier … r).
65
66definition add_local_env : register → list register → local_env → local_env ≝
67  λr. insert … (word_of_identifier … r).
68
69definition find_local_env : register → local_env → list register ≝
70  λr: register.λbvt. lookup … (word_of_identifier … r) bvt [].
71
72definition initialize_local_env_internal ≝
73  λlenv_runiverse.
74  λr_sig.
75  let 〈lenv,runiverse〉 ≝ lenv_runiverse in
76  let 〈r, sig〉 ≝ r_sig in
77  let size ≝ size_of_sig_type sig in
78  let 〈rs,runiverse〉 ≝ register_freshes runiverse size in
79    〈add_local_env r rs lenv,runiverse〉.
80
81definition initialize_local_env ≝
82  λruniverse.
83  λregisters.
84  λresult.
85  let registers ≝ registers @
86    match result with
87    [ None ⇒ [ ]
88    | Some rt ⇒ [ rt ]
89    ]
90  in
91    foldl ? ? initialize_local_env_internal 〈Stub …,runiverse〉 registers.
92
93definition map_list_local_env_internal ≝
94  λlenv,res,r. res @ (find_local_env r lenv).
95   
96definition map_list_local_env ≝
97  λlenv,regs. foldl … (map_list_local_env_internal lenv) [ ] regs.
98
99definition make_addr ≝
100  λA.
101  λlst: list A.
102  λprf: 2 = length A lst.
103  match lst return λx. 2 = |x| → A × A with
104  [ nil ⇒ λlst_nil_prf. ?
105  | cons hd tl ⇒ λprf.
106    match tl return λx. 1 = |x| → A × A with
107    [ nil ⇒ λtl_nil_prf. ?
108    | cons hd' tl' ⇒ λtl_cons_prf. 〈hd, hd'〉
109    ] ?
110  ] prf.
111  [1: normalize in lst_nil_prf;
112      destruct(lst_nil_prf)
113  |2: normalize in prf;
114      @injective_S
115      assumption
116  |3: normalize in tl_nil_prf;
117      destruct(tl_nil_prf)
118  ]
119qed.
120
121definition find_and_addr ≝
122  λr,lenv. make_addr ? (find_local_env r lenv).
123
124definition rtl_args ≝
125  λregs_list,lenv. flatten … (map … (λr. find_local_env r lenv) regs_list).
126
127definition translate_cst_int_internal ≝
128  λglobals,dest_lbl,r,i. sequential rtl_params_ globals (INT … r i) dest_lbl.
129
130definition split_into_bytes:
131  ∀size. ∀int: bvint size. Σbytes: list Byte. |bytes| = size_intsize size ≝
132λsize.
133 match size return λsize.∀int: bvint size. Σbytes. |bytes| = size_intsize size with
134  [ I8 ⇒ λint. ? | I16 ⇒ λint. ? | I32 ⇒ λint. ? ].
135[ %[@[int]] //
136| %[@(let 〈h,l〉 ≝ split ? 8 … int in [l;h])] cases (split ????) //
137| %[@(let 〈h1,l〉 ≝ split ? 8 … int in
138      let 〈h2,l〉 ≝ split ? 8 … l in
139      let 〈h3,l〉 ≝ split ? 8 … l in
140       [l;h3;h2;h1])]
141  cases (split ????) #h1 #l normalize
142  cases (split ????) #h2 #l normalize
143  cases (split ????) // ]
144qed.
145
146lemma eqb_implies_eq:
147  ∀m, n: nat.
148    eqb m n = true → m = n.
149  #M
150  elim M
151  [1: #N normalize
152      cases N
153      [1: normalize //
154      |2: #M' normalize #HYP destruct(HYP)
155      ]
156  |2: #M' #IND_HYP #N
157      normalize
158      cases N
159      [1: normalize #HYP destruct(HYP)
160      |2: #M'' normalize #HYP
161          @eq_f @(IND_HYP M'')
162          assumption
163      ]
164   ]
165qed.
166
167definition translate_op: ∀globals. ? → list register → list register → list register →
168  label → label → rtl_internal_function globals → rtl_internal_function globals ≝
169  λglobals: list ident.
170  λop.
171  λdestrs: list register.
172  λsrcrs1: list register.
173  λsrcrs2: list register.
174  λstart_lbl: label.
175  λdest_lbl: label.
176  λdef: rtl_internal_function globals.
177  match reduce_strong register register srcrs1 srcrs2 with
178  [ dp reduced first_reduced_proof ⇒
179    let srcrsl_common ≝ \fst (\fst reduced) in
180    let srcrsr_common ≝ \fst (\snd reduced) in
181    let srcrsl_rest ≝ \snd (\fst reduced) in
182    let srcrsr_rest ≝ \snd (\snd reduced) in
183    let srcrs_rest ≝ srcrsl_rest @ srcrsr_rest in
184    match reduce_strong register register destrs srcrsl_common with
185    [ dp reduced second_reduced_proof ⇒
186      let destrs_common ≝ \fst (\fst reduced) in
187      let destrs_rest ≝ \snd (\fst reduced) in
188      match reduce_strong register register destrs_rest srcrs_rest with
189      [ dp reduced third_reduced_proof ⇒
190        let destrs_cted ≝ \fst (\fst reduced) in
191        let destrs_rest ≝ \snd (\fst reduced) in
192        let srcrs_cted ≝ \fst (\snd reduced) in
193        let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
194        let insts_init ≝ [
195          sequential … (CLEAR_CARRY …);
196          sequential … (INT rtl_params_ globals tmpr (zero …))
197        ] in
198        let f_add ≝ λdestr. λsrcr1. λsrcr2. sequential … (OP2 rtl_params_ globals op destr srcr1 srcr2) in
199        let insts_add ≝ map3 ? ? ? ? f_add destrs_common srcrsl_common srcrsr_common ? ? in
200        let f_add_cted ≝ λdestr. λsrcr. sequential … (OP2 rtl_params_ globals op destr srcr tmpr) in
201        let insts_add_cted ≝ map2 … f_add_cted destrs_cted srcrs_cted ? in
202        let f_rest ≝ λdestr. sequential … (OP2 rtl_params_ globals op destr tmpr tmpr) in
203        let insts_rest ≝ map … f_rest destrs_rest in
204          adds_graph rtl_params1 globals (insts_init @ insts_add @ insts_add_cted @ insts_rest) start_lbl dest_lbl def
205      ]
206    ]
207  ].
208  [1: @third_reduced_proof
209  |3: @first_reduced_proof
210  |*: cases daemon (* XXX: some of these look like they may be false *)
211  ]
212qed.
213
214let rec translate_cst
215  (globals: list ident) (cst: constant) (destrs: list register)
216    (start_lbl: label) (dest_lbl: label) (def: rtl_internal_function globals)
217      on cst: rtl_internal_function globals ≝
218  match cst with
219  [ Ointconst size const ⇒
220    match destrs with
221    [ nil ⇒ add_graph … start_lbl (GOTO … dest_lbl) def
222    | _   ⇒
223      let size' ≝ size_intsize size in
224        match eqb size' (|destrs|) return λx. (eqb size' (|destrs|)) = x → rtl_internal_function globals with
225        [ true  ⇒ λgood_case.
226          match split_into_bytes size const with
227          [ dp bytes bytes_length_proof ⇒
228            let mapped ≝ map2 … (λd. λb. (sequential … (INT rtl_params_ globals d b))) destrs bytes ? in
229              adds_graph rtl_params1 globals mapped start_lbl dest_lbl def
230          ]
231        | false ⇒ λbad_case. ?
232        ] (refl … (eqb size' (|destrs|)))
233    ]
234  | Ofloatconst float ⇒ ⊥
235  | Oaddrsymbol id offset ⇒
236    let 〈r1, r2〉 ≝ make_addr … destrs ? in
237    let def ≝ add_graph rtl_params1 globals start_lbl (sequential … (ADDRESS rtl_params_ globals id ? r1 r2) dest_lbl) def in
238    let def ≝ translate_op globals Addc [r1] [r1] [r2] start_lbl dest_lbl def in
239      def
240  | Oaddrstack offset ⇒
241    let 〈r1, r2〉 ≝ make_addr … destrs ? in
242    let def ≝ add_graph rtl_params1 globals start_lbl (sequential … (extension rtl_params_ globals (rtl_st_ext_stack_address r1 r2)) dest_lbl) def in
243    let def ≝ translate_op globals Addc [r1] [r1] [r2] start_lbl dest_lbl def in
244      def
245  ].
246  [1: >bytes_length_proof
247      cut(size' = |destrs|)
248      [1: @eqb_implies_eq
249          assumption
250      |2: #EQ_HYP
251          <EQ_HYP %
252      ]
253  |2: cases daemon (* XXX: bad case where destrs is of the wrong length *)
254  |3: cases not_implemented (* XXX: float, error_float in o'caml *)
255  |*: cases daemon (* XXX: various proofs to be filled in *)
256  ].
257qed.
258 
259definition translate_move_internal ≝
260  λglobals.
261  λdestr: register.
262  λsrcr: register.
263    sequential rtl_params_ globals (MOVE … 〈destr,srcr〉).
264
265definition translate_move ≝
266  λglobals.
267  λdestrs: list register.
268  λsrcrs: list register.
269  λstart_lbl: label.
270    match reduce_strong register register destrs srcrs with
271    [ dp crl_crr len_proof ⇒
272      let commonl ≝ \fst (\fst crl_crr) in
273      let commonr ≝ \fst (\snd crl_crr) in
274      let restl ≝ \snd (\fst crl_crr) in
275      let restr ≝ \snd (\snd crl_crr) in
276      let f_common ≝ translate_move_internal globals in
277      let translate1 ≝ adds_graph rtl_params1 … (map2 … f_common commonl commonr ?) in
278      let translate2 ≝ translate_cst … (Ointconst ? (repr I8 0)) restl in (* should this be 8? *)
279        add_translates … [ translate1 ; translate2 ] start_lbl
280    ].
281    @len_proof
282qed.
283
284let rec make
285  (A: Type[0]) (elt: A) (n: nat) on n ≝
286  match n with
287  [ O ⇒ [ ]
288  | S n' ⇒ elt :: make A elt n'
289  ].
290 
291lemma make_length:
292  ∀A: Type[0].
293  ∀elt: A.
294  ∀n: nat.
295    n = length ? (make A elt n).
296  #A #ELT #N
297  elim N
298  [ normalize %
299  | #N #IH
300    normalize <IH %
301  ]
302qed.
303
304definition translate_cast_unsigned ≝
305  λglobals.
306  λdestrs.
307  λstart_lbl.
308  λdest_lbl.
309  λdef: joint_internal_function … (rtl_params globals).
310  let 〈def, tmp_zero〉 ≝ fresh_reg … def in
311  let zeros ≝ make … tmp_zero (length … destrs) in
312    add_translates … [
313      adds_graph rtl_params1 … [
314        sequential rtl_params_ … (INT rtl_params_ ? tmp_zero (bitvector_of_nat ? 0))
315        ];
316      translate_move globals destrs zeros
317    ] start_lbl dest_lbl def.
318
319definition translate_cast_signed:
320    ∀globals: list ident. list register → ? → label → label → rtl_internal_function globals → rtl_internal_function globals ≝
321  λglobals: list ident.
322  λdestrs.
323  λsrcr.
324  λstart_lbl.
325  λdest_lbl.
326  λdef.
327  let 〈def, tmp_128〉 ≝ fresh_reg … def in
328  let 〈def, tmp_255〉 ≝ fresh_reg … def in
329  let 〈def, tmpr〉 ≝ fresh_reg … def in
330  let 〈def, dummy〉 ≝ fresh_reg … def in
331  let insts ≝ [
332    sequential  … (INT rtl_params_ globals tmp_128 (bitvector_of_nat ? 128));
333    sequential … (OP2 rtl_params_ globals And tmpr tmp_128 srcr);
334    sequential … (OPACCS rtl_params_ globals DivuModu tmpr dummy tmpr tmp_128);
335    sequential … (INT rtl_params_ globals tmp_255 (bitvector_of_nat ? 255));
336    sequential … (OPACCS rtl_params_ globals Mul tmpr dummy tmpr tmp_255)
337  ]
338  in
339  let srcrs ≝ make … tmpr (length … destrs) in
340    add_translates rtl_params1 globals [
341      adds_graph rtl_params1 globals insts;
342      translate_move globals destrs srcrs
343    ] start_lbl dest_lbl def.
344
345definition translate_cast ≝
346  λglobals: list ident.
347  λsrc_size: nat.
348  λsrc_sign: signedness.
349  λdest_size: nat.
350  λdestrs: list register.
351  λsrcrs: list register.
352  match |srcrs| return λx. |srcrs| = x → ? with
353  [ O ⇒ λzero_prf. adds_graph rtl_params1 globals [ ]
354  | S n' ⇒ λsucc_prf.
355    if ltb dest_size src_size then
356      translate_move globals destrs srcrs
357    else
358      match reduce_strong register register destrs srcrs with
359      [ dp crl len_proof ⇒
360        let commonl ≝ \fst (\fst crl) in
361        let commonr ≝ \fst (\snd crl) in
362        let restl ≝ \snd (\fst crl) in
363        let restr ≝ \snd (\snd crl) in
364        let insts_common ≝ translate_move globals commonl commonr in
365        let sign_reg ≝ last_safe ? srcrs ? in
366        let insts_sign ≝
367          match src_sign with
368          [ Unsigned ⇒ translate_cast_unsigned globals restl
369          | Signed ⇒ translate_cast_signed globals restl sign_reg
370          ]
371        in
372          add_translates rtl_params1 globals [ insts_common; insts_sign ]
373      ]
374  ] (refl ? (|srcrs|)).
375  >succ_prf //
376qed.
377
378definition translate_negint ≝
379  λglobals: list ident.
380  λdestrs: list register.
381  λsrcrs: list register.
382  λstart_lbl: label.
383  λdest_lbl: label.
384  λdef: rtl_internal_function globals.
385  λprf: |destrs| = |srcrs|. (* assert in caml code *)
386  let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
387  let f_cmpl ≝ λdestr. λsrcr. sequential rtl_params_ globals (OP1 rtl_params1 globals Cmpl destr srcr) in
388  let insts_cmpl ≝ map2 … f_cmpl destrs srcrs prf in
389  let insts_init ≝ [
390    sequential … (SET_CARRY …);
391    sequential … (INT rtl_params_ globals tmpr (zero ?))
392  ] in
393  let f_add ≝ λdestr. sequential … (OP2 rtl_params_ globals Addc destr destr tmpr) in
394  let insts_add ≝ map … f_add destrs in
395    adds_graph rtl_params1 globals (insts_cmpl @ insts_init @ insts_add) start_lbl dest_lbl def.
396
397definition translate_notbool: ∀globals. list register → list register → label → label → rtl_internal_function globals → rtl_internal_function globals ≝
398  λglobals: list ident.
399  λdestrs: list register.
400  λsrcrs: list register.
401  λstart_lbl: label.
402  λdest_lbl: label.
403  λdef: rtl_internal_function globals.
404  match destrs with
405  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … start_lbl) def
406  | cons destr destrs ⇒
407    let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
408    let 〈def, tmp_srcrs〉 ≝ fresh_regs rtl_params0 globals def (length ? srcrs) in
409    let save_srcrs ≝ translate_move globals tmp_srcrs srcrs in
410    let init_destr ≝ sequential … (INT rtl_params_ globals destr (bitvector_of_nat ? 1)) in
411    let f ≝ λtmp_srcr. [
412      sequential … (CLEAR_CARRY rtl_params_ globals);
413      sequential … (INT rtl_params_ globals tmpr (zero ?));
414      sequential … (OP2 rtl_params_ globals Sub tmpr tmpr tmp_srcr);
415      sequential … (INT rtl_params_ globals tmpr (zero ?));
416      sequential … (OP2 rtl_params_ globals Addc tmpr tmpr tmpr);
417      sequential … (OP2 rtl_params_ globals Xor destr destr tmpr)
418    ] in
419    let insts ≝ init_destr :: (flatten … (map … f tmp_srcrs)) in
420    let epilogue ≝ translate_cst globals (Ointconst I8 (zero …)) destrs in
421      add_translates rtl_params1 globals [
422        save_srcrs; adds_graph rtl_params1 globals insts; epilogue
423      ] start_lbl dest_lbl def
424  ].
425
426(* TODO: examine this properly.  This is a change from the O'caml code due
427   to us dropping the explicit use of a cast destination size field.  We
428   instead infer the size of the cast's destination from the context.  Is
429   this correct?
430*)
431definition translate_op1 ≝
432  λglobals: list ident.
433  λop1: unary_operation.
434  λdestrs: list register.
435  λsrcrs: list register.
436  λprf: |destrs| = |srcrs|.
437  λstart_lbl: label.
438  λdest_lbl: label.
439  λdef: rtl_internal_function globals.
440  match op1 with
441  [ Ocastint src_sign src_size ⇒
442    let dest_size ≝ |destrs| * 8 in
443    let src_size ≝ bitsize_of_intsize src_size in
444      translate_cast globals src_size src_sign dest_size destrs srcrs start_lbl dest_lbl def
445  | Onegint ⇒
446      translate_negint globals destrs srcrs start_lbl dest_lbl def prf
447  | Onotbool ⇒
448      translate_notbool globals destrs srcrs start_lbl dest_lbl def
449  | Onotint ⇒
450    let f ≝ λdestr. λsrcr. sequential rtl_params_ globals (OP1 … Cmpl destr srcr) in
451    let l ≝ map2 … f destrs srcrs prf in
452      adds_graph rtl_params1 globals l start_lbl dest_lbl def
453  | Optrofint r ⇒
454      translate_move globals destrs srcrs start_lbl dest_lbl def
455  | Ointofptr r ⇒
456      translate_move globals destrs srcrs start_lbl dest_lbl def
457  | Oid ⇒
458      translate_move globals destrs srcrs start_lbl dest_lbl def
459  | _ ⇒ ? (* float operations implemented in runtime *)
460  ].
461  cases not_implemented
462qed.
463
464let rec translate_mul1
465  (globals: list ident) (dummy: register) (tmpr: register)
466    (destrs: list register) (srcrs1: list register) (srcr2: register)
467      (start_lbl: label)
468        on srcrs1 ≝
469  match destrs with
470  [ nil ⇒ adds_graph rtl_params1 globals [ GOTO … ] start_lbl
471  | cons destr tl ⇒
472    match tl with
473    [ nil ⇒
474      match srcrs1 with
475      [ nil ⇒
476        adds_graph rtl_params1 globals [
477          sequential … (INT rtl_params_ globals tmpr (zero …));
478          sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
479        ] start_lbl
480      | cons srcr1 tl' ⇒
481        adds_graph rtl_params1 globals [
482          sequential … (OPACCS rtl_params_ globals Mul tmpr dummy srcr2 srcr1);
483          sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
484        ] start_lbl
485      ]
486    | cons destr2 destrs ⇒
487      match srcrs1 with
488      [ nil ⇒
489        add_translates rtl_params1 globals [
490          adds_graph rtl_params1 globals [
491            sequential … (INT rtl_params_ globals tmpr (zero …));
492            sequential … (OP2 rtl_params_ globals Addc destr destr tmpr);
493            sequential … (OP2 rtl_params_ globals Addc destr2 tmpr tmpr)
494          ];
495          translate_cst globals (Ointconst I8 (zero …)) destrs
496        ] start_lbl
497      | cons srcr1 srcrs1 ⇒
498        match destrs with
499        [ nil ⇒
500          add_translates rtl_params1 globals [
501            adds_graph rtl_params1 globals [
502              sequential … (INT rtl_params_ globals tmpr (zero …));
503              sequential … (OP2 rtl_params_ globals Addc destr destr tmpr);
504              sequential … (OP2 rtl_params_ globals Addc destr2 tmpr tmpr)
505            ];
506            translate_cst globals (Ointconst I8 (zero ?)) destrs
507          ] start_lbl
508        | cons destr2 destrs ⇒
509          add_translates rtl_params1 globals [
510            adds_graph rtl_params1 globals [
511              sequential … (OPACCS rtl_params_ globals Mul tmpr destr2 srcr2 srcr1);
512              sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
513            ];
514            translate_mul1 globals dummy tmpr (destr2 :: destrs) srcrs1 srcr2
515          ] start_lbl
516        ]
517      ]
518    ]
519  ].
520
521definition translate_muli ≝
522  λglobals: list ident.
523  λdummy: register.
524  λtmpr: register.
525  λdestrs: list register.
526  λtmp_destrs: list register.
527  λsrcrs1: list register.
528  λdummy_lbl: label.
529  λi: nat.
530  λi_prf: i ≤ |tmp_destrs|.
531  λtranslates: list ?.
532  λsrcr2i: register.
533  let 〈tmp_destrs1, tmp_destrs2〉 ≝ split … tmp_destrs i i_prf in
534  let tmp_destrs2' ≝
535    match tmp_destrs2 with
536    [ nil ⇒ [ ]
537    | cons tmp_destr2 tmp_destrs2 ⇒ [
538        adds_graph rtl_params1 globals [
539          sequential rtl_params_ globals (CLEAR_CARRY …);
540          sequential … (INT rtl_params_ globals tmp_destr2 (zero …))
541        ];
542        translate_mul1 globals dummy tmpr tmp_destrs2 srcrs1 srcr2i;
543        translate_cst globals (Ointconst I8 (zero …)) tmp_destrs1;
544        adds_graph rtl_params1 globals [
545          sequential rtl_params_ globals (CLEAR_CARRY …)
546        ];
547        translate_op globals Addc destrs destrs tmp_destrs
548      ]
549    ]
550  in
551    translates @ tmp_destrs2'.
552 
553axiom foldi_strong:
554  ∀a: Type[0].
555  ∀b: Type[0].
556  ∀the_list: list b.
557  ∀f: (∀i: nat. ∀proof: i ≤ |the_list|. a → b → a).
558  ∀seed: a.
559    a.
560 
561definition translate_mul ≝
562  λglobals: list ident.
563  λdestrs: list register.
564  λsrcrs1: list register.
565  λsrcrs2: list register.
566  λregs_proof: |destrs| = |srcrs2|.
567  λstart_lbl: label.
568  λdest_lbl: label.
569  λdef: rtl_internal_function globals.
570  let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
571  let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
572    match fresh_regs_strong rtl_params0 globals def (|destrs|) with
573    [ dp def_tmp_destrs tmp_destrs_prf ⇒
574      let def ≝ \fst def_tmp_destrs in
575      let tmp_destrs ≝ \snd def_tmp_destrs in
576      let 〈def, fresh_srcrs1〉 ≝ fresh_regs rtl_params0 globals def (|srcrs1|) in
577      let 〈def, fresh_srcrs2〉 ≝ fresh_regs rtl_params0 globals def (|srcrs2|) in
578      let insts_init ≝ [
579        translate_move globals fresh_srcrs1 srcrs1;
580        translate_move globals fresh_srcrs2 srcrs2;
581        translate_cst globals (Ointconst I8 (zero …)) destrs
582      ]
583      in
584        let f ≝ translate_muli globals dummy tmpr destrs tmp_destrs fresh_srcrs1 start_lbl in
585        let f' ≝ λi. λi_proof: i ≤ |srcrs2|. f i ? in
586        let insts_mul ≝ foldi_strong … srcrs2 f' [ ] in
587          add_translates rtl_params1 globals (insts_init @ insts_mul) start_lbl dest_lbl def
588    ].
589  >tmp_destrs_prf
590  >regs_proof
591  assumption
592qed.
593
594definition translate_divumodu8 ≝
595  λglobals: list ident.
596  λorder: bool.
597  λdestrs: list register.
598  λsrcr1: register.
599  λsrcr2: register.
600  λstart_lbl: label.
601  λdest_lbl: label.
602  λdef: rtl_internal_function globals.
603  match destrs with
604  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
605  | cons destr destrs ⇒
606    let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
607    let 〈destr1, destr2〉 ≝ match order with [ true ⇒ 〈destr, dummy〉 | _ ⇒ 〈dummy, destr〉 ] in
608    let inst_div ≝ adds_graph rtl_params1 globals [
609      sequential rtl_params_ globals (OPACCS … DivuModu destr1 destr2 srcr1 srcr2)
610    ]
611    in
612    let insts_rest ≝ translate_cst globals (Ointconst I8 (zero ?)) destrs in
613      add_translates rtl_params1 globals [ inst_div; insts_rest ] start_lbl dest_lbl def
614  ].
615
616definition translate_ne: ∀globals: list ident. ? → ? → ? → ? → ? → ? → rtl_internal_function globals ≝
617  λglobals: list ident.
618  λdestrs: list register.
619  λsrcrs1: list register.
620  λsrcrs2: list register.
621  λstart_lbl: label.
622  λdest_lbl: label.
623  λdef: rtl_internal_function globals.
624  match destrs with
625  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
626  | cons destr destrs ⇒
627    let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
628    let 〈def, tmp_zero〉 ≝ fresh_reg rtl_params0 globals def in
629    let 〈def, tmp_srcrs1〉 ≝ fresh_regs rtl_params0 globals def (|srcrs1|) in
630    let save_srcrs1 ≝ translate_move globals tmp_srcrs1 srcrs1 in
631    let 〈def, tmp_srcrs2〉 ≝ fresh_regs rtl_params0 globals def (|srcrs2|) in
632    let save_srcrs2 ≝ translate_move globals tmp_srcrs2 srcrs2 in
633    match reduce_strong register register tmp_srcrs1 tmp_srcrs2 with
634    [ dp crl their_proof ⇒
635      let commonl ≝ \fst (\fst crl) in
636      let commonr ≝ \fst (\snd crl) in
637      let restl ≝ \snd (\snd crl) in
638      let restr ≝ \snd (\snd crl) in
639      let rest ≝ restl @ restr in
640      let inits ≝ [
641        sequential … (INT rtl_params_ globals destr (zero …));
642        sequential … (INT rtl_params_ globals tmp_zero (zero …))
643      ]
644      in
645      let f_common ≝ λtmp_srcr1. λtmp_srcr2. [
646        sequential … (CLEAR_CARRY …);
647        sequential … (OP2 rtl_params_ globals Sub tmpr tmp_srcr1 tmp_srcr2);
648        sequential … (OP2 rtl_params_ globals Or destr destr tmpr)
649      ]
650      in
651      let insts_common ≝ flatten … (map2 … f_common commonl commonr ?) in
652      let f_rest ≝ λtmp_srcr. [
653        sequential … (CLEAR_CARRY …);
654        sequential … (OP2 rtl_params_ globals Sub tmpr tmp_zero tmp_srcr);
655        sequential … (OP2 rtl_params_ globals Or destr destr tmpr)
656      ]
657      in
658      let insts_rest ≝ flatten … (map … f_rest rest) in
659      let insts ≝ inits @ insts_common @ insts_rest in
660      let epilogue ≝ translate_cst globals (Ointconst I8 (zero …)) destrs in
661        add_translates rtl_params1 globals [
662          save_srcrs1; save_srcrs2; adds_graph rtl_params1 globals insts; epilogue
663        ] start_lbl dest_lbl def
664    ]
665  ].
666  @their_proof
667qed.
668
669definition translate_eq_reg ≝
670  λglobals: list ident.
671  λtmp_zero: register.
672  λtmp_one: register.
673  λtmpr1: register.
674  λtmpr2: register.
675  λdestr: register.
676  λdummy_lbl: label.
677  λsrcr12: register × register.
678  let 〈srcr1, srcr2〉 ≝ srcr12 in
679  [ sequential … (CLEAR_CARRY …);
680    sequential rtl_params_ globals (OP2 rtl_params_ globals Sub tmpr1 srcr1 srcr2);
681    sequential … (OP2 rtl_params_ globals Addc tmpr1 tmp_zero tmp_zero);
682    sequential … (OP2 rtl_params_ globals Sub tmpr2 srcr2 srcr1);
683    sequential … (OP2 rtl_params_ globals Addc tmpr2 tmp_zero tmp_zero);
684    sequential … (OP2 rtl_params_ globals Or tmpr1 tmpr1 tmpr2);
685    sequential … (OP2 rtl_params_ globals Xor tmpr1 tmpr1 tmp_one);
686    sequential … (OP2 rtl_params_ globals And destr destr tmpr1) ].
687
688definition translate_eq_list ≝
689  λglobals: list ident.
690  λtmp_zero: register.
691  λtmp_one: register.
692  λtmpr1: register.
693  λtmpr2: register.
694  λdestr: register.
695  λleq: list (register × register).
696  λdummy_lbl: label.
697  let f ≝ translate_eq_reg globals tmp_zero tmp_one tmpr1 tmpr2 destr dummy_lbl in
698    (sequential … (INT rtl_params_ globals destr (bitvector_of_nat ? 1))) ::
699      flatten … (map … f leq).
700
701definition translate_atom ≝
702  λglobals: list ident.
703  λtmp_zero: register.
704  λtmp_one: register.
705  λtmpr1: register.
706  λtmpr2: register.
707  λtmpr3: register.
708  λdestr: register.
709  λdummy_lbl: label.
710  λleq: list (register × register).
711  λsrcr1: register.
712  λsrcr2: register.
713    translate_eq_list globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 leq dummy_lbl @
714    [ sequential … (CLEAR_CARRY …);
715      sequential … (OP2 rtl_params_ globals Sub tmpr1 srcr1 srcr2);
716      sequential … (OP2 rtl_params_ globals Addc tmpr1 tmp_zero tmp_zero);
717      sequential … (OP2 rtl_params_ globals And tmpr3 tmpr3 tmpr1);
718      sequential … (OP2 rtl_params_ globals Or destr destr tmpr3) ].
719
720definition translate_lt_main ≝
721  λglobals: list ident.
722  λtmp_zero: register.
723  λtmp_one: register.
724  λtmpr1: register.
725  λtmpr2: register.
726  λtmpr3: register.
727  λdestr: register.
728  λdummy_lbl: label.
729  λsrcrs1: list register.
730  λsrcrs2: list register.
731  λproof: |srcrs1| = |srcrs2|.
732  let f ≝ λinsts_leq. λsrcr1. λsrcr2.
733    let 〈insts, leq〉 ≝ insts_leq in
734    let added_insts ≝ translate_atom globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 destr dummy_lbl leq srcr1 srcr2 in
735      〈insts @ added_insts, leq @ [〈srcr1, srcr2〉]〉
736  in
737    \fst (fold_left2 … f 〈[ ], [ ]〉 srcrs1 srcrs2 proof).
738
739definition fresh_regs_strong:
740  ∀globals. rtl_internal_function globals → ∀n: nat. Σfresh: (rtl_internal_function globals) × (list register). |\snd fresh| = n ≝
741  λglobals: list ident.
742  λdef.
743  λn.
744    fresh_regs rtl_params0 globals def n.
745  @fresh_regs_length
746qed.
747
748definition complete_regs_strong:
749  ∀globals: list ident. rtl_internal_function globals → list register → list register → Σcomplete: (list register) × (list register) × (list register). |\fst (\fst complete)| = |\snd (\fst complete)| ≝
750  λglobals: list ident.
751  λdef.
752  λleft.
753  λright.
754    complete_regs globals def left right.
755  @complete_regs_length
756qed.
757
758definition translate_lt ≝
759  λglobals: list ident.
760  λdestrs: list register.
761  λprf_destrs: 0 < |destrs|.
762  λsrcrs1: list register.
763  λsrcrs2: list register.
764  λstart_lbl: label.
765  λdest_lbl: label.
766  λdef: rtl_internal_function globals.
767  match destrs with
768  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
769  | _ ⇒
770    match fresh_regs_strong globals def (|destrs|) with
771    [ dp def_tmp_destrs tmp_destrs_proof ⇒
772      let def ≝ \fst def_tmp_destrs in
773      let tmp_destrs ≝ \snd def_tmp_destrs in
774      let tmp_destr ≝ hd_safe ? tmp_destrs ? in
775      let 〈def, tmp_zero〉 ≝ fresh_reg rtl_params1 globals def in
776      let 〈def, tmp_one〉 ≝ fresh_reg rtl_params1 globals def in
777      let 〈def, tmpr1〉 ≝ fresh_reg rtl_params1 globals def in
778      let 〈def, tmpr2〉 ≝ fresh_reg rtl_params1 globals def in
779      let 〈def, tmpr3〉 ≝ fresh_reg rtl_params1 globals def in
780      match complete_regs_strong globals def srcrs1 srcrs2 with
781      [ dp srcrs12_added srcrs12_proof ⇒
782        let srcrs1 ≝ \fst (\fst srcrs12_added) in
783        let srcrs2 ≝ \snd (\fst srcrs12_added) in
784        let added ≝ \snd srcrs12_added in
785        let srcrs1' ≝ rev … srcrs1 in
786        let srcrs2' ≝ rev … srcrs2 in
787        let insts_init ≝ [
788          translate_cst globals (Ointconst I8 (zero ?)) tmp_destrs;
789          translate_cst globals (Ointconst I8 (zero ?)) added;
790          adds_graph rtl_params1 globals [
791            sequential rtl_params_ globals (INT rtl_params_ globals tmp_zero (zero …));
792            sequential rtl_params_ globals (INT rtl_params_ globals tmp_one (bitvector_of_nat … 1))
793          ]
794        ]
795        in
796        let insts_main ≝
797          translate_lt_main globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 tmp_destr start_lbl srcrs1' srcrs2' ? in
798          let insts_main ≝ [ adds_graph rtl_params1 globals insts_main ] in
799          let insts_exit ≝ [ translate_move globals destrs tmp_destrs ] in
800            add_translates rtl_params1 globals (insts_init @ insts_main @ insts_exit) start_lbl dest_lbl def
801      ]
802    ]
803  ].
804  [2: >tmp_destrs_proof @prf_destrs
805  |1: normalize nodelta
806      generalize in match srcrs12_proof
807      #HYP >rev_length >rev_length @HYP
808  ]
809qed.
810
811definition add_128_to_last ≝
812  λglobals: list ident.
813  λtmp_128: register.
814  λrs.
815  λprf: 0 < |rs|.
816  λstart_lbl: label.
817  match rs with
818  [ nil ⇒ adds_graph rtl_params1 globals [ ] start_lbl
819  | _ ⇒
820    let r ≝ last_safe … rs prf in
821      adds_graph rtl_params1 globals [
822        sequential rtl_params_ globals (OP2 rtl_params_ globals Add r r tmp_128)
823      ] start_lbl
824  ].
825
826definition translate_lts ≝
827  λglobals: list ident.
828  λdestrs: list register.
829  λdestrs_prf: 0 < |destrs|.
830  λsrcrs1: list register.
831  λsrcrs2: list register.
832  λsrcrs1_lt_prf: 0 < |srcrs1|.
833  λsrcrs2_lt_prf: 0 < |srcrs2|.
834  λstart_lbl: label.
835  λdest_lbl: label.
836  λdef: rtl_internal_function globals.
837  match fresh_regs_strong globals def (|srcrs1|) with
838  [ dp def_tmp_srcrs1 srcrs1_prf ⇒
839    let def ≝ \fst def_tmp_srcrs1 in
840    let tmp_srcrs1 ≝ \snd def_tmp_srcrs1 in
841    match fresh_regs_strong globals def (|srcrs2|) with
842    [ dp def_tmp_srcrs2 srcrs2_prf ⇒
843      let def ≝ \fst def_tmp_srcrs2 in
844      let tmp_srcrs2 ≝ \snd def_tmp_srcrs2 in
845      let 〈def, tmp_128〉 ≝ fresh_reg rtl_params0 globals def in
846        add_translates rtl_params1 globals [
847          translate_move globals tmp_srcrs1 srcrs1;
848          translate_move globals tmp_srcrs2 srcrs2;
849          adds_graph rtl_params1 globals [
850            sequential rtl_params_ globals (INT rtl_params_ globals tmp_128 (bitvector_of_nat ? 128))
851          ];
852          add_128_to_last globals tmp_128 tmp_srcrs1 ?;
853          add_128_to_last globals tmp_128 tmp_srcrs2 ?;
854          translate_lt globals destrs destrs_prf tmp_srcrs1 tmp_srcrs2
855        ] start_lbl dest_lbl def
856    ]
857  ].
858  [1: >srcrs1_prf @srcrs1_lt_prf
859  |2: >srcrs2_prf @srcrs2_lt_prf
860  ]
861qed.
862
863definition translate_op2 ≝
864  λglobals: list ident.
865  λop2.
866  λdestrs: list register.
867  λdestrs_prf: 0 < |destrs|.
868  λsrcrs1: list register.
869  λsrcrs2: list register.
870  λsrcrs2_destrs_prf: |srcrs2| = |destrs|.
871  λsrcrs1_destrs_prf: |srcrs1| = |destrs|.
872  λstart_lbl: label.
873  λdest_lbl: label.
874  λdef: rtl_internal_function globals.
875  match op2 with
876  [ Oadd ⇒
877    translate_op globals Addc destrs srcrs1 srcrs2 start_lbl dest_lbl def
878  | Oaddp ⇒
879    translate_op globals Addc destrs srcrs1 srcrs2 start_lbl dest_lbl def
880  | Osub ⇒
881    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
882  | Osubpi ⇒
883    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
884  | Osubpp sz ⇒
885    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
886  | Omul ⇒
887    translate_mul globals destrs srcrs1 srcrs2 ? start_lbl dest_lbl def
888  | Odivu ⇒
889    match srcrs1 return λx. 0 < |x| → rtl_internal_function globals with
890    [ cons hd tl ⇒ λcons_prf.
891      match tl with
892      [ nil ⇒ translate_divumodu8 globals true destrs hd (hd_safe register srcrs2 ?) start_lbl dest_lbl def
893      | _ ⇒ ? (* not implemented *)
894      ]
895    | nil ⇒ λnil_absrd. ?
896    ] ?
897  | Omodu ⇒
898    match srcrs1 return λx. 0 < |x| → rtl_internal_function globals with
899    [ cons hd tl ⇒ λcons_prf.
900      match tl with
901      [ nil ⇒ translate_divumodu8 globals false destrs hd (hd_safe register srcrs2 ?) start_lbl dest_lbl def
902      | _ ⇒ ? (* not implemented *)
903      ]
904    | nil ⇒ λnil_absrd. ?
905    ] ?
906  | Oand ⇒
907    translate_op globals And destrs srcrs1 srcrs2 start_lbl dest_lbl def
908  | Oor ⇒
909    translate_op globals Or destrs srcrs1 srcrs2 start_lbl dest_lbl def
910  | Oxor ⇒
911    translate_op globals Xor destrs srcrs1 srcrs2 start_lbl dest_lbl def
912  | Ocmp c ⇒
913    match c with
914    [ Ceq ⇒
915      add_translates rtl_params1 globals [
916        translate_ne globals destrs srcrs1 srcrs2;
917        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
918      ] start_lbl dest_lbl def
919    | Cne ⇒ translate_ne globals destrs srcrs1 srcrs2 start_lbl dest_lbl def
920    | Clt ⇒ translate_lts globals destrs destrs_prf srcrs1 srcrs2 ? ? start_lbl dest_lbl def
921    | Cgt ⇒ translate_lts globals destrs destrs_prf srcrs2 srcrs1 ? ? start_lbl dest_lbl def
922    | Cle ⇒
923      add_translates rtl_params1 globals [
924        translate_lts globals destrs destrs_prf srcrs2 srcrs1 ? ?;
925        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
926      ] start_lbl dest_lbl def
927    | Cge ⇒
928      add_translates rtl_params1 globals [
929        translate_lts globals destrs destrs_prf srcrs1 srcrs2 ? ?;
930        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
931      ] start_lbl dest_lbl def
932    ]
933  | Ocmpu c ⇒
934    match c with
935    [ Ceq ⇒
936      add_translates rtl_params1 globals [
937        translate_ne globals destrs srcrs1 srcrs2;
938        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
939      ] start_lbl dest_lbl def
940    | Cne ⇒ translate_ne globals destrs srcrs1 srcrs2 start_lbl dest_lbl def
941    | Clt ⇒ translate_lt globals destrs destrs_prf srcrs1 srcrs2 start_lbl dest_lbl def
942    | Cgt ⇒ translate_lt globals destrs destrs_prf srcrs2 srcrs1 start_lbl dest_lbl def
943    | Cle ⇒
944      add_translates rtl_params1 globals [
945        translate_lt globals destrs destrs_prf srcrs2 srcrs1;
946        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
947      ] start_lbl dest_lbl def
948    | Cge ⇒
949      add_translates rtl_params1 globals [
950        translate_lt globals destrs destrs_prf srcrs1 srcrs2;
951        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
952      ] start_lbl dest_lbl def
953    ]
954  | Ocmpp c ⇒
955    match c with
956    [ Ceq ⇒
957      add_translates rtl_params1 globals [
958        translate_ne globals destrs srcrs1 srcrs2;
959        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
960      ] start_lbl dest_lbl def
961    | Cne ⇒ translate_ne globals destrs srcrs1 srcrs2 start_lbl dest_lbl def
962    | Clt ⇒ translate_lt globals destrs destrs_prf srcrs1 srcrs2 start_lbl dest_lbl def
963    | Cgt ⇒ translate_lt globals destrs destrs_prf srcrs2 srcrs1 start_lbl dest_lbl def
964    | Cle ⇒
965      add_translates rtl_params1 globals [
966        translate_lt globals destrs destrs_prf srcrs2 srcrs1;
967        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
968      ] start_lbl dest_lbl def
969    | Cge ⇒
970      add_translates rtl_params1 globals [
971        translate_lt globals destrs destrs_prf srcrs1 srcrs2;
972        translate_op1 globals Onotbool destrs destrs (refl ? (|destrs|))
973      ] start_lbl dest_lbl def
974    ]
975  | _ ⇒ ? (* assert false, implemented in run time or float op *)
976  ].
977  [1:
978    @sym_eq
979    assumption
980  |3,8,19,22,24,25:
981    >srcrs1_destrs_prf
982    assumption
983  |4,9:
984    normalize in nil_absrd;
985    cases(not_le_Sn_O 0)
986    #HYP cases(HYP nil_absrd)
987  |5,10,20,21,23,26:
988    >srcrs2_destrs_prf
989    assumption
990  |*:
991    cases not_implemented (* XXX: yes, really *)
992  ]
993qed.
994
995definition translate_cond: ∀globals: list ident. list register → label → label → label → rtl_internal_function globals → rtl_internal_function globals ≝
996  λglobals: list ident.
997  λsrcrs: list register.
998  λstart_lbl: label.
999  λlbl_true: label.
1000  λlbl_false: label.
1001  λdef: rtl_internal_function globals.
1002  let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
1003  let 〈tmp_lbl, def〉 ≝ fresh_label rtl_params1 globals def in
1004  let init ≝ sequential rtl_params_ globals (INT rtl_params_ globals tmpr (zero …)) in
1005  let f ≝ λsrcr. sequential rtl_params_ globals (OP2 rtl_params_ globals Or tmpr tmpr srcr) in
1006  let def ≝ adds_graph rtl_params1 globals (init :: (map ? ? f srcrs)) start_lbl tmp_lbl def in
1007    add_graph rtl_params1 globals tmp_lbl (sequential rtl_params_ globals (COND rtl_params_ globals tmpr lbl_true) lbl_false) def.
1008
1009definition translate_load ≝
1010  λglobals: list ident.
1011  λaddr.
1012  λaddr_prf: 2 = |addr|.
1013  λdestrs: list register.
1014  λstart_lbl: label.
1015  λdest_lbl: label.
1016  λdef: rtl_internal_function globals.
1017  match fresh_regs_strong rtl_params0 globals def (|addr|) with
1018  [ dp def_save_addr save_addr_prf ⇒
1019    let def ≝ \fst def_save_addr in
1020    let save_addr ≝ \snd def_save_addr in
1021    match fresh_regs_strong rtl_params0 globals def (|addr|) with
1022    [ dp def_tmp_addr tmp_addr_prf ⇒
1023      let def ≝ \fst def_tmp_addr in
1024      let tmp_addr ≝ \snd def_tmp_addr in
1025      let 〈tmp_addr1, tmp_addr2〉 ≝ make_addr … tmp_addr ? in
1026      let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
1027      let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
1028      let insts_save_addr ≝ translate_move globals save_addr addr in
1029      let f ≝ λtranslates_off. λr.
1030        let 〈translates, off〉 ≝ translates_off in
1031        let translates ≝ translates @ [
1032          adds_graph rtl_params1 globals [
1033            sequential rtl_params_ globals (INT rtl_params_ globals tmpr off)
1034          ];
1035          translate_op2 globals Oaddp tmp_addr ? save_addr [dummy; tmpr] ? ?;
1036          adds_graph rtl_params1 globals [
1037            sequential rtl_params_ globals (LOAD rtl_params_ globals r tmp_addr1 tmp_addr2)
1038          ]
1039        ]
1040        in
1041        let 〈carry, result〉 ≝ half_add … off int_size in
1042          〈translates, result〉
1043      in
1044      let 〈translates, ignore〉 ≝ foldl … f 〈[ ], (zero ?)〉 destrs in
1045        add_translates rtl_params1 globals (insts_save_addr :: translates) start_lbl dest_lbl def
1046    ]
1047  ].
1048  [1: >tmp_addr_prf >save_addr_prf %
1049  |2: >save_addr_prf >tmp_addr_prf
1050      @addr_prf
1051  |3: >tmp_addr_prf normalize
1052      <addr_prf //
1053  |4: >tmp_addr_prf assumption
1054  ]
1055qed.
1056
1057definition translate_store ≝
1058  λglobals: list ident.
1059  λaddr.
1060  λaddr_prf: 2 = |addr|.
1061  λsrcrs: list register.
1062  λstart_lbl: label.
1063  λdest_lbl: label.
1064  λdef: rtl_internal_function globals.
1065  match fresh_regs_strong rtl_params0 globals def (|addr|) with
1066  [ dp def_tmp_addr tmp_addr_prf ⇒
1067    let def ≝ \fst def_tmp_addr in
1068    let tmp_addr ≝ \snd def_tmp_addr in
1069    let 〈tmp_addr1, tmp_addr2〉 ≝ make_addr … tmp_addr ? in
1070    let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
1071    let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
1072    let f ≝ λtranslate_off. λsrcr.
1073      let 〈translates, off〉 ≝ translate_off in
1074      let translates ≝ translates @ [
1075        adds_graph rtl_params1 globals [
1076          sequential rtl_params_ globals (INT rtl_params_ globals tmpr off)
1077        ];
1078        translate_op2 globals Oaddp tmp_addr … addr [dummy; tmpr] ? ?;
1079        adds_graph rtl_params1 globals [
1080          sequential rtl_params_ globals (STORE rtl_params_ globals tmp_addr1 tmp_addr2 srcr)
1081        ]
1082      ]
1083      in
1084        let 〈carry, result〉 ≝ half_add … off int_size in
1085          〈translates, result〉
1086    in
1087    let 〈translates, ignore〉 ≝ foldl … f 〈[ ], zero …〉 srcrs in
1088      add_translates rtl_params1 globals translates start_lbl dest_lbl def
1089  ].
1090  [1: >tmp_addr_prf %
1091  |2: >tmp_addr_prf normalize
1092      <addr_prf //
1093  |3: >tmp_addr_prf <addr_prf //
1094  |4: >tmp_addr_prf @addr_prf
1095  ]
1096qed.
1097
1098axiom fake_label: label.
1099
1100(* XXX: following conversation with CSC about the mix up in extension statements
1101        and extension instructions in RTL, use fake_label in calls to
1102        tailcall_* instructions. *)
1103definition translate_stmt ≝
1104  λglobals: list ident.
1105  λlenv: local_env.
1106  λlbl: label.
1107  λstmt.
1108  λdef: rtl_internal_function globals.
1109  match stmt with
1110  [ St_skip lbl' ⇒ add_graph … lbl (GOTO … lbl') def
1111  | St_cost cost_lbl lbl' ⇒ add_graph rtl_params1 globals lbl (sequential rtl_params_ globals (COST_LABEL rtl_params_ globals cost_lbl) lbl') def
1112  | St_const destr cst lbl' ⇒
1113    translate_cst globals cst (find_local_env destr lenv) lbl lbl' def
1114  | St_op1 op1 destr srcr lbl' ⇒
1115    translate_op1 globals op1 (find_local_env destr lenv) (find_local_env srcr lenv) … lbl lbl' def
1116  | St_op2 op2 destr srcr1 srcr2 lbl' ⇒
1117    translate_op2 globals op2 (find_local_env destr lenv) ? (find_local_env srcr1 lenv) (find_local_env srcr2 lenv) ? ? lbl lbl' def
1118    (* XXX: should we be ignoring this? *)
1119  | St_load ignore addr destr lbl' ⇒
1120    translate_load globals (find_local_env addr lenv) ? (find_local_env destr lenv) lbl lbl' def
1121    (* XXX: should we be ignoring this? *)
1122  | St_store ignore addr srcr lbl' ⇒
1123    translate_store globals (find_local_env addr lenv) ? (find_local_env srcr lenv) lbl lbl' def
1124  | St_call_id f args retr lbl' ⇒
1125    match retr with
1126    [ Some retr ⇒
1127      add_graph rtl_params1 globals lbl (sequential … (CALL_ID rtl_params1 globals f (rtl_args args lenv) (find_local_env retr lenv)) lbl') def
1128    | None ⇒ add_graph rtl_params1 globals lbl (sequential … (CALL_ID rtl_params1 globals f (rtl_args args lenv) [ ]) lbl') def
1129    ]
1130  | St_call_ptr f args retr lbl' ⇒
1131    match retr with
1132    [ None ⇒
1133      let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
1134        add_graph rtl_params1 globals lbl (sequential … (extension … (rtl_st_ext_call_ptr f1 f2 (rtl_args args lenv) [ ])) lbl') def
1135    | Some retr ⇒
1136      let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
1137        add_graph rtl_params1 globals lbl (sequential … (extension … (rtl_st_ext_call_ptr f1 f2 (rtl_args args lenv) (find_local_env retr lenv))) lbl') def
1138    ]
1139  | St_tailcall_id f args ⇒
1140    add_graph rtl_params1 globals lbl (sequential rtl_params_ globals (extension rtl_params_ globals (rtl_st_ext_tailcall_id f (rtl_args args lenv))) fake_label) def
1141  | St_tailcall_ptr f args ⇒
1142    let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
1143      add_graph rtl_params1 globals lbl (sequential rtl_params_ globals (extension rtl_params1 globals (rtl_st_ext_tailcall_ptr f1 f2 (rtl_args args lenv))) fake_label) def
1144  | St_cond r lbl_true lbl_false ⇒
1145    translate_cond globals (find_local_env r lenv) lbl lbl_true lbl_false def
1146  | St_jumptable r l ⇒ ? (* assert false: not implemented yet *)
1147  | St_return ⇒ add_graph rtl_params1 globals lbl (RETURN …) def
1148  ].
1149  [10: cases not_implemented (* jtable case *)
1150  |*: cases daemon (* TODO: proofs regarding lengths of lists, examine! *)
1151  ]
1152qed.
1153
1154(* XXX: we use a "hack" here to circumvent the proof obligations required to
1155   create res, an empty internal_function record.  we insert into our graph
1156   the start and end nodes to ensure that they are in, and place dummy
1157   skip instructions at these nodes. *)
1158definition translate_internal ≝
1159  λglobals: list ident.
1160  λdef.
1161  let runiverse ≝ f_reggen def in
1162  let 〈lenv,runiverse〉 ≝ initialize_local_env runiverse
1163    (f_params def @ f_locals def) (f_result def) in
1164  let params ≝ map_list_local_env lenv (map … \fst (f_params def)) in
1165  let locals ≝ map_list_local_env lenv (map … \fst (f_locals def)) in
1166  let result ≝
1167    match (f_result def) with
1168    [ None ⇒ [ ]
1169    | Some r_typ ⇒
1170      let 〈r, typ〉 ≝ r_typ in
1171        find_local_env r lenv
1172    ]
1173  in
1174  let luniverse' ≝ f_labgen def in
1175  let runiverse' ≝ runiverse in
1176  let result' ≝ result in
1177  let params' ≝ params in
1178  let locals' ≝ locals in
1179  let stack_size' ≝ f_stacksize def in
1180  let entry' ≝ f_entry def in
1181  let exit' ≝ f_exit def in
1182  let graph' ≝ add LabelTag ? (empty_map LabelTag ?) entry' (GOTO rtl_params_ globals entry') in
1183  let graph' ≝ add LabelTag ? graph' exit' (GOTO rtl_params_ globals exit') in
1184  let res ≝
1185    mk_joint_internal_function … (rtl_params globals) luniverse' runiverse' result' params'
1186     locals' stack_size' graph' ? ? in
1187    foldi … (translate_stmt globals … lenv) (f_graph def) res.
1188[ % [@entry' | @graph_add_lookup @graph_add]
1189| % [@exit'  | @graph_add]]
1190qed.
1191
1192(*CSC: here we are not doing any alignment on variables, like it is done in OCaml
1193  because of CompCert heritage *)
1194definition rtlabs_to_rtl: RTLabs_program → rtl_program ≝
1195 λp. transform_program … p (transf_fundef … (translate_internal (prog_var_names …))).
Note: See TracBrowser for help on using the repository browser.