source: src/RTLabs/RTLabsToRTL.ma @ 1782

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

Files ported to new version of the standard library.

File size: 47.5 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 ≝ identifier_map RegisterTag (list register).
62
63definition mem_local_env : register → local_env → bool ≝
64  λr,e. member … e r.
65
66definition add_local_env : register → list register → local_env → local_env ≝
67  λr,v,e. add … e r v.
68
69definition find_local_env : register → local_env → list register ≝
70  λr: register.λenv. lookup_def … env r [].
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 〈empty_map …,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  [ mk_Sig 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    [ mk_Sig 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      [ mk_Sig 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          [ mk_Sig 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    [ mk_Sig 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      [ mk_Sig 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  λty, ty'.
434  λop1: unary_operation ty ty'.
435  λdestrs: list register.
436  λsrcrs: list register.
437  λprf: |destrs| = |srcrs|.
438  λstart_lbl: label.
439  λdest_lbl: label.
440  λdef: rtl_internal_function globals.
441  match op1 with
442  [ Ocastint src_size src_sign _ _ ⇒
443    let dest_size ≝ |destrs| * 8 in
444    let src_size ≝ bitsize_of_intsize src_size in
445      translate_cast globals src_size src_sign dest_size destrs srcrs start_lbl dest_lbl def
446  | Onegint _ _ ⇒
447      translate_negint globals destrs srcrs start_lbl dest_lbl def prf
448  | Onotbool _ _ _ _ ⇒
449      translate_notbool globals destrs srcrs start_lbl dest_lbl def
450  | Onotint _ _ ⇒
451    let f ≝ λdestr. λsrcr. sequential rtl_params_ globals (OP1 … Cmpl destr srcr) in
452    let l ≝ map2 … f destrs srcrs prf in
453      adds_graph rtl_params1 globals l start_lbl dest_lbl def
454  | Optrofint _ _ r ⇒
455      translate_move globals destrs srcrs start_lbl dest_lbl def
456  | Ointofptr _ _ r ⇒
457      translate_move globals destrs srcrs start_lbl dest_lbl def
458  | Oid _ ⇒
459      translate_move globals destrs srcrs start_lbl dest_lbl def
460  | _ ⇒ ? (* float operations implemented in runtime *)
461  ].
462  cases not_implemented
463qed.
464
465let rec translate_mul1
466  (globals: list ident) (dummy: register) (tmpr: register)
467    (destrs: list register) (srcrs1: list register) (srcr2: register)
468      (start_lbl: label)
469        on srcrs1 ≝
470  match destrs with
471  [ nil ⇒ adds_graph rtl_params1 globals [ GOTO … ] start_lbl
472  | cons destr tl ⇒
473    match tl with
474    [ nil ⇒
475      match srcrs1 with
476      [ nil ⇒
477        adds_graph rtl_params1 globals [
478          sequential … (INT rtl_params_ globals tmpr (zero …));
479          sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
480        ] start_lbl
481      | cons srcr1 tl' ⇒
482        adds_graph rtl_params1 globals [
483          sequential … (OPACCS rtl_params_ globals Mul tmpr dummy srcr2 srcr1);
484          sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
485        ] start_lbl
486      ]
487    | cons destr2 destrs ⇒
488      match srcrs1 with
489      [ nil ⇒
490        add_translates rtl_params1 globals [
491          adds_graph rtl_params1 globals [
492            sequential … (INT rtl_params_ globals tmpr (zero …));
493            sequential … (OP2 rtl_params_ globals Addc destr destr tmpr);
494            sequential … (OP2 rtl_params_ globals Addc destr2 tmpr tmpr)
495          ];
496          translate_cst globals (Ointconst I8 (zero …)) destrs
497        ] start_lbl
498      | cons srcr1 srcrs1 ⇒
499        match destrs with
500        [ nil ⇒
501          add_translates rtl_params1 globals [
502            adds_graph rtl_params1 globals [
503              sequential … (INT rtl_params_ globals tmpr (zero …));
504              sequential … (OP2 rtl_params_ globals Addc destr destr tmpr);
505              sequential … (OP2 rtl_params_ globals Addc destr2 tmpr tmpr)
506            ];
507            translate_cst globals (Ointconst I8 (zero ?)) destrs
508          ] start_lbl
509        | cons destr2 destrs ⇒
510          add_translates rtl_params1 globals [
511            adds_graph rtl_params1 globals [
512              sequential … (OPACCS rtl_params_ globals Mul tmpr destr2 srcr2 srcr1);
513              sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
514            ];
515            translate_mul1 globals dummy tmpr (destr2 :: destrs) srcrs1 srcr2
516          ] start_lbl
517        ]
518      ]
519    ]
520  ].
521
522definition translate_muli ≝
523  λglobals: list ident.
524  λdummy: register.
525  λtmpr: register.
526  λdestrs: list register.
527  λtmp_destrs: list register.
528  λsrcrs1: list register.
529  λdummy_lbl: label.
530  λi: nat.
531  λi_prf: i ≤ |tmp_destrs|.
532  λtranslates: list ?.
533  λsrcr2i: register.
534  let 〈tmp_destrs1, tmp_destrs2〉 ≝ split … tmp_destrs i i_prf in
535  let tmp_destrs2' ≝
536    match tmp_destrs2 with
537    [ nil ⇒ [ ]
538    | cons tmp_destr2 tmp_destrs2 ⇒ [
539        adds_graph rtl_params1 globals [
540          sequential rtl_params_ globals (CLEAR_CARRY …);
541          sequential … (INT rtl_params_ globals tmp_destr2 (zero …))
542        ];
543        translate_mul1 globals dummy tmpr tmp_destrs2 srcrs1 srcr2i;
544        translate_cst globals (Ointconst I8 (zero …)) tmp_destrs1;
545        adds_graph rtl_params1 globals [
546          sequential rtl_params_ globals (CLEAR_CARRY …)
547        ];
548        translate_op globals Addc destrs destrs tmp_destrs
549      ]
550    ]
551  in
552    translates @ tmp_destrs2'.
553   
554let rec remove_n_first_internal
555  (b: Type[0]) (n: nat) (the_list: list b) (i: nat)
556    on the_list ≝
557  match the_list with
558  [ nil        ⇒ [ ]
559  | cons hd tl ⇒
560    match eqb i n with
561    [ true  ⇒ the_list
562    | false ⇒ remove_n_first_internal b n tl (S i)
563    ]
564  ].
565   
566definition remove_n_first ≝
567  λb: Type[0].
568  λn: nat.
569  λthe_list: list b.
570    remove_n_first_internal b n the_list 0.
571
572axiom plus_m_n_eq_o_to_lt_m_o:
573  ∀m, n, o: nat.
574    m + n = o → m ≤ o.
575
576include alias "arithmetics/nat.ma".
577
578axiom minus_m_n_Sp_to_minus_m_Sn_p:
579  ∀m, n, p: nat.
580    minus m n = S p → minus m (S n) = p.
581   
582let rec foldi_strong_internal
583  (a: Type[0]) (b: Type[0]) (reference: nat) (the_list: list b)
584    (f: ∀j: nat. ∀proof: lt j (|the_list|). a → b → a) (seed: a)
585      (counter: nat) (counter_proof: minus reference counter = |the_list|)
586        on the_list: a ≝
587  match the_list return λx. the_list = x → (minus reference counter = |x|) → a with
588  [ nil        ⇒ λidentity. λbase_case. seed
589  | cons hd tl ⇒ λidentity. λstep_case.
590    let f' ≝ λj: nat. λproof: j < |tl|. f j ? in
591      foldi_strong_internal a b reference tl f' (f counter ? seed hd) (S counter) ?
592  ] (refl … the_list) counter_proof.
593  [1: cases daemon (* XXX: to do *)
594  |2: generalize in match counter_proof;
595      >identity
596      #HYP
597      normalize in HYP:(???%);
598      generalize in match (minus_m_n_Sp_to_minus_m_Sn_p reference counter (|tl|) HYP);
599      #ASSM assumption
600  |3: >identity
601      normalize
602      normalize in proof;
603      generalize in match(le_S … proof);
604      #HYP assumption
605  ]
606qed.
607 
608definition foldi_strong ≝
609  λa: Type[0].
610  λb: Type[0].
611  λthe_list: list b.
612  λf: (∀i: nat. ∀proof: i < |the_list|. a → b → a).
613  λseed: a.
614    foldi_strong_internal a b (|the_list|) the_list f seed 0 ?.
615  //
616qed.
617 
618definition translate_mul ≝
619  λglobals: list ident.
620  λdestrs: list register.
621  λsrcrs1: list register.
622  λsrcrs2: list register.
623  λregs_proof: |destrs| = |srcrs2|.
624  λstart_lbl: label.
625  λdest_lbl: label.
626  λdef: rtl_internal_function globals.
627  let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
628  let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
629    match fresh_regs_strong rtl_params0 globals def (|destrs|) with
630    [ mk_Sig def_tmp_destrs tmp_destrs_prf ⇒
631      let def ≝ \fst def_tmp_destrs in
632      let tmp_destrs ≝ \snd def_tmp_destrs in
633      let 〈def, fresh_srcrs1〉 ≝ fresh_regs rtl_params0 globals def (|srcrs1|) in
634      let 〈def, fresh_srcrs2〉 ≝ fresh_regs rtl_params0 globals def (|srcrs2|) in
635      let insts_init ≝ [
636        translate_move globals fresh_srcrs1 srcrs1;
637        translate_move globals fresh_srcrs2 srcrs2;
638        translate_cst globals (Ointconst I8 (zero …)) destrs
639      ]
640      in
641        let f ≝ translate_muli globals dummy tmpr destrs tmp_destrs fresh_srcrs1 start_lbl in
642        let f' ≝ λi. λi_proof: i < |srcrs2|. f i ? in
643        let insts_mul ≝ foldi_strong … srcrs2 f' [ ] in
644          add_translates rtl_params1 globals (insts_init @ insts_mul) start_lbl dest_lbl def
645    ].
646  >tmp_destrs_prf
647  >regs_proof
648  /2/
649qed.
650
651definition translate_divumodu8 ≝
652  λglobals: list ident.
653  λorder: bool.
654  λdestrs: list register.
655  λsrcr1: register.
656  λsrcr2: register.
657  λstart_lbl: label.
658  λdest_lbl: label.
659  λdef: rtl_internal_function globals.
660  match destrs with
661  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
662  | cons destr destrs ⇒
663    let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
664    let 〈destr1, destr2〉 ≝ match order with [ true ⇒ 〈destr, dummy〉 | _ ⇒ 〈dummy, destr〉 ] in
665    let inst_div ≝ adds_graph rtl_params1 globals [
666      sequential rtl_params_ globals (OPACCS … DivuModu destr1 destr2 srcr1 srcr2)
667    ]
668    in
669    let insts_rest ≝ translate_cst globals (Ointconst I8 (zero ?)) destrs in
670      add_translates rtl_params1 globals [ inst_div; insts_rest ] start_lbl dest_lbl def
671  ].
672
673definition translate_ne: ∀globals: list ident. ? → ? → ? → ? → ? → ? → rtl_internal_function globals ≝
674  λglobals: list ident.
675  λdestrs: list register.
676  λsrcrs1: list register.
677  λsrcrs2: list register.
678  λstart_lbl: label.
679  λdest_lbl: label.
680  λdef: rtl_internal_function globals.
681  match destrs with
682  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
683  | cons destr destrs ⇒
684    let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
685    let 〈def, tmp_zero〉 ≝ fresh_reg rtl_params0 globals def in
686    let 〈def, tmp_srcrs1〉 ≝ fresh_regs rtl_params0 globals def (|srcrs1|) in
687    let save_srcrs1 ≝ translate_move globals tmp_srcrs1 srcrs1 in
688    let 〈def, tmp_srcrs2〉 ≝ fresh_regs rtl_params0 globals def (|srcrs2|) in
689    let save_srcrs2 ≝ translate_move globals tmp_srcrs2 srcrs2 in
690    match reduce_strong register register tmp_srcrs1 tmp_srcrs2 with
691    [ mk_Sig crl their_proof ⇒
692      let commonl ≝ \fst (\fst crl) in
693      let commonr ≝ \fst (\snd crl) in
694      let restl ≝ \snd (\snd crl) in
695      let restr ≝ \snd (\snd crl) in
696      let rest ≝ restl @ restr in
697      let inits ≝ [
698        sequential … (INT rtl_params_ globals destr (zero …));
699        sequential … (INT rtl_params_ globals tmp_zero (zero …))
700      ]
701      in
702      let f_common ≝ λtmp_srcr1. λtmp_srcr2. [
703        sequential … (CLEAR_CARRY …);
704        sequential … (OP2 rtl_params_ globals Sub tmpr tmp_srcr1 tmp_srcr2);
705        sequential … (OP2 rtl_params_ globals Or destr destr tmpr)
706      ]
707      in
708      let insts_common ≝ flatten … (map2 … f_common commonl commonr ?) in
709      let f_rest ≝ λtmp_srcr. [
710        sequential … (CLEAR_CARRY …);
711        sequential … (OP2 rtl_params_ globals Sub tmpr tmp_zero tmp_srcr);
712        sequential … (OP2 rtl_params_ globals Or destr destr tmpr)
713      ]
714      in
715      let insts_rest ≝ flatten … (map … f_rest rest) in
716      let insts ≝ inits @ insts_common @ insts_rest in
717      let epilogue ≝ translate_cst globals (Ointconst I8 (zero …)) destrs in
718        add_translates rtl_params1 globals [
719          save_srcrs1; save_srcrs2; adds_graph rtl_params1 globals insts; epilogue
720        ] start_lbl dest_lbl def
721    ]
722  ].
723  @their_proof
724qed.
725
726definition translate_eq_reg ≝
727  λglobals: list ident.
728  λtmp_zero: register.
729  λtmp_one: register.
730  λtmpr1: register.
731  λtmpr2: register.
732  λdestr: register.
733  λdummy_lbl: label.
734  λsrcr12: register × register.
735  let 〈srcr1, srcr2〉 ≝ srcr12 in
736  [ sequential … (CLEAR_CARRY …);
737    sequential rtl_params_ globals (OP2 rtl_params_ globals Sub tmpr1 srcr1 srcr2);
738    sequential … (OP2 rtl_params_ globals Addc tmpr1 tmp_zero tmp_zero);
739    sequential … (OP2 rtl_params_ globals Sub tmpr2 srcr2 srcr1);
740    sequential … (OP2 rtl_params_ globals Addc tmpr2 tmp_zero tmp_zero);
741    sequential … (OP2 rtl_params_ globals Or tmpr1 tmpr1 tmpr2);
742    sequential … (OP2 rtl_params_ globals Xor tmpr1 tmpr1 tmp_one);
743    sequential … (OP2 rtl_params_ globals And destr destr tmpr1) ].
744
745definition translate_eq_list ≝
746  λglobals: list ident.
747  λtmp_zero: register.
748  λtmp_one: register.
749  λtmpr1: register.
750  λtmpr2: register.
751  λdestr: register.
752  λleq: list (register × register).
753  λdummy_lbl: label.
754  let f ≝ translate_eq_reg globals tmp_zero tmp_one tmpr1 tmpr2 destr dummy_lbl in
755    (sequential … (INT rtl_params_ globals destr (bitvector_of_nat ? 1))) ::
756      flatten … (map … f leq).
757
758definition translate_atom ≝
759  λglobals: list ident.
760  λtmp_zero: register.
761  λtmp_one: register.
762  λtmpr1: register.
763  λtmpr2: register.
764  λtmpr3: register.
765  λdestr: register.
766  λdummy_lbl: label.
767  λleq: list (register × register).
768  λsrcr1: register.
769  λsrcr2: register.
770    translate_eq_list globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 leq dummy_lbl @
771    [ sequential … (CLEAR_CARRY …);
772      sequential … (OP2 rtl_params_ globals Sub tmpr1 srcr1 srcr2);
773      sequential … (OP2 rtl_params_ globals Addc tmpr1 tmp_zero tmp_zero);
774      sequential … (OP2 rtl_params_ globals And tmpr3 tmpr3 tmpr1);
775      sequential … (OP2 rtl_params_ globals Or destr destr tmpr3) ].
776
777definition translate_lt_main ≝
778  λglobals: list ident.
779  λtmp_zero: register.
780  λtmp_one: register.
781  λtmpr1: register.
782  λtmpr2: register.
783  λtmpr3: register.
784  λdestr: register.
785  λdummy_lbl: label.
786  λsrcrs1: list register.
787  λsrcrs2: list register.
788  λproof: |srcrs1| = |srcrs2|.
789  let f ≝ λinsts_leq. λsrcr1. λsrcr2.
790    let 〈insts, leq〉 ≝ insts_leq in
791    let added_insts ≝ translate_atom globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 destr dummy_lbl leq srcr1 srcr2 in
792      〈insts @ added_insts, leq @ [〈srcr1, srcr2〉]〉
793  in
794    \fst (fold_left2 … f 〈[ ], [ ]〉 srcrs1 srcrs2 proof).
795
796definition fresh_regs_strong:
797  ∀globals. rtl_internal_function globals → ∀n: nat. Σfresh: (rtl_internal_function globals) × (list register). |\snd fresh| = n ≝
798  λglobals: list ident.
799  λdef.
800  λn.
801    fresh_regs rtl_params0 globals def n.
802  @fresh_regs_length
803qed.
804
805definition complete_regs_strong:
806  ∀globals: list ident. rtl_internal_function globals → list register → list register → Σcomplete: (list register) × (list register) × (list register). |\fst (\fst complete)| = |\snd (\fst complete)| ≝
807  λglobals: list ident.
808  λdef.
809  λleft.
810  λright.
811    complete_regs globals def left right.
812  @complete_regs_length
813qed.
814
815definition translate_lt ≝
816  λglobals: list ident.
817  λdestrs: list register.
818  λprf_destrs: 0 < |destrs|.
819  λsrcrs1: list register.
820  λsrcrs2: list register.
821  λstart_lbl: label.
822  λdest_lbl: label.
823  λdef: rtl_internal_function globals.
824  match destrs with
825  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
826  | _ ⇒
827    match fresh_regs_strong globals def (|destrs|) with
828    [ mk_Sig def_tmp_destrs tmp_destrs_proof ⇒
829      let def ≝ \fst def_tmp_destrs in
830      let tmp_destrs ≝ \snd def_tmp_destrs in
831      let tmp_destr ≝ hd_safe ? tmp_destrs ? in
832      let 〈def, tmp_zero〉 ≝ fresh_reg rtl_params1 globals def in
833      let 〈def, tmp_one〉 ≝ fresh_reg rtl_params1 globals def in
834      let 〈def, tmpr1〉 ≝ fresh_reg rtl_params1 globals def in
835      let 〈def, tmpr2〉 ≝ fresh_reg rtl_params1 globals def in
836      let 〈def, tmpr3〉 ≝ fresh_reg rtl_params1 globals def in
837      match complete_regs_strong globals def srcrs1 srcrs2 with
838      [ mk_Sig srcrs12_added srcrs12_proof ⇒
839        let srcrs1 ≝ \fst (\fst srcrs12_added) in
840        let srcrs2 ≝ \snd (\fst srcrs12_added) in
841        let added ≝ \snd srcrs12_added in
842        let srcrs1' ≝ rev … srcrs1 in
843        let srcrs2' ≝ rev … srcrs2 in
844        let insts_init ≝ [
845          translate_cst globals (Ointconst I8 (zero ?)) tmp_destrs;
846          translate_cst globals (Ointconst I8 (zero ?)) added;
847          adds_graph rtl_params1 globals [
848            sequential rtl_params_ globals (INT rtl_params_ globals tmp_zero (zero …));
849            sequential rtl_params_ globals (INT rtl_params_ globals tmp_one (bitvector_of_nat … 1))
850          ]
851        ]
852        in
853        let insts_main ≝
854          translate_lt_main globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 tmp_destr start_lbl srcrs1' srcrs2' ? in
855          let insts_main ≝ [ adds_graph rtl_params1 globals insts_main ] in
856          let insts_exit ≝ [ translate_move globals destrs tmp_destrs ] in
857            add_translates rtl_params1 globals (insts_init @ insts_main @ insts_exit) start_lbl dest_lbl def
858      ]
859    ]
860  ].
861  [2: >tmp_destrs_proof @prf_destrs
862  |1: normalize nodelta
863      generalize in match srcrs12_proof;
864      #HYP >rev_length >rev_length @HYP
865  ]
866qed.
867
868definition add_128_to_last ≝
869  λglobals: list ident.
870  λtmp_128: register.
871  λrs.
872  λprf: 0 < |rs|.
873  λstart_lbl: label.
874  match rs with
875  [ nil ⇒ adds_graph rtl_params1 globals [ ] start_lbl
876  | _ ⇒
877    let r ≝ last_safe … rs prf in
878      adds_graph rtl_params1 globals [
879        sequential rtl_params_ globals (OP2 rtl_params_ globals Add r r tmp_128)
880      ] start_lbl
881  ].
882
883definition translate_lts ≝
884  λglobals: list ident.
885  λdestrs: list register.
886  λdestrs_prf: 0 < |destrs|.
887  λsrcrs1: list register.
888  λsrcrs2: list register.
889  λsrcrs1_lt_prf: 0 < |srcrs1|.
890  λsrcrs2_lt_prf: 0 < |srcrs2|.
891  λstart_lbl: label.
892  λdest_lbl: label.
893  λdef: rtl_internal_function globals.
894  match fresh_regs_strong globals def (|srcrs1|) with
895  [ mk_Sig def_tmp_srcrs1 srcrs1_prf ⇒
896    let def ≝ \fst def_tmp_srcrs1 in
897    let tmp_srcrs1 ≝ \snd def_tmp_srcrs1 in
898    match fresh_regs_strong globals def (|srcrs2|) with
899    [ mk_Sig def_tmp_srcrs2 srcrs2_prf ⇒
900      let def ≝ \fst def_tmp_srcrs2 in
901      let tmp_srcrs2 ≝ \snd def_tmp_srcrs2 in
902      let 〈def, tmp_128〉 ≝ fresh_reg rtl_params0 globals def in
903        add_translates rtl_params1 globals [
904          translate_move globals tmp_srcrs1 srcrs1;
905          translate_move globals tmp_srcrs2 srcrs2;
906          adds_graph rtl_params1 globals [
907            sequential rtl_params_ globals (INT rtl_params_ globals tmp_128 (bitvector_of_nat ? 128))
908          ];
909          add_128_to_last globals tmp_128 tmp_srcrs1 ?;
910          add_128_to_last globals tmp_128 tmp_srcrs2 ?;
911          translate_lt globals destrs destrs_prf tmp_srcrs1 tmp_srcrs2
912        ] start_lbl dest_lbl def
913    ]
914  ].
915  [1: >srcrs1_prf @srcrs1_lt_prf
916  |2: >srcrs2_prf @srcrs2_lt_prf
917  ]
918qed.
919
920definition translate_op2 ≝
921  λglobals: list ident.
922  λop2.
923  λdestrs: list register.
924  λdestrs_prf: 0 < |destrs|.
925  λsrcrs1: list register.
926  λsrcrs2: list register.
927  λsrcrs2_destrs_prf: |srcrs2| = |destrs|.
928  λsrcrs1_destrs_prf: |srcrs1| = |destrs|.
929  λstart_lbl: label.
930  λdest_lbl: label.
931  λdef: rtl_internal_function globals.
932  match op2 with
933  [ Oadd ⇒
934    translate_op globals Addc destrs srcrs1 srcrs2 start_lbl dest_lbl def
935  | Oaddp ⇒
936    translate_op globals Addc destrs srcrs1 srcrs2 start_lbl dest_lbl def
937  | Osub ⇒
938    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
939  | Osubpi ⇒
940    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
941  | Osubpp sz ⇒
942    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
943  | Omul ⇒
944    translate_mul globals destrs srcrs1 srcrs2 ? start_lbl dest_lbl def
945  | Odivu ⇒
946    match srcrs1 return λx. 0 < |x| → rtl_internal_function globals with
947    [ cons hd tl ⇒ λcons_prf.
948      match tl with
949      [ nil ⇒ translate_divumodu8 globals true destrs hd (hd_safe register srcrs2 ?) start_lbl dest_lbl def
950      | _ ⇒ ? (* not implemented *)
951      ]
952    | nil ⇒ λnil_absrd. ?
953    ] ?
954  | Omodu ⇒
955    match srcrs1 return λx. 0 < |x| → rtl_internal_function globals with
956    [ cons hd tl ⇒ λcons_prf.
957      match tl with
958      [ nil ⇒ translate_divumodu8 globals false destrs hd (hd_safe register srcrs2 ?) start_lbl dest_lbl def
959      | _ ⇒ ? (* not implemented *)
960      ]
961    | nil ⇒ λnil_absrd. ?
962    ] ?
963  | Oand ⇒
964    translate_op globals And destrs srcrs1 srcrs2 start_lbl dest_lbl def
965  | Oor ⇒
966    translate_op globals Or destrs srcrs1 srcrs2 start_lbl dest_lbl def
967  | Oxor ⇒
968    translate_op globals Xor destrs srcrs1 srcrs2 start_lbl dest_lbl def
969  | Ocmp c ⇒
970    match c with
971    [ Ceq ⇒
972      add_translates rtl_params1 globals [
973        translate_ne globals destrs srcrs1 srcrs2;
974        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
975      ] start_lbl dest_lbl def
976    | Cne ⇒ translate_ne globals destrs srcrs1 srcrs2 start_lbl dest_lbl def
977    | Clt ⇒ translate_lts globals destrs destrs_prf srcrs1 srcrs2 ? ? start_lbl dest_lbl def
978    | Cgt ⇒ translate_lts globals destrs destrs_prf srcrs2 srcrs1 ? ? start_lbl dest_lbl def
979    | Cle ⇒
980      add_translates rtl_params1 globals [
981        translate_lts globals destrs destrs_prf srcrs2 srcrs1 ? ?;
982        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
983      ] start_lbl dest_lbl def
984    | Cge ⇒
985      add_translates rtl_params1 globals [
986        translate_lts globals destrs destrs_prf srcrs1 srcrs2 ? ?;
987        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
988      ] start_lbl dest_lbl def
989    ]
990  | Ocmpu c ⇒
991    match c with
992    [ Ceq ⇒
993      add_translates rtl_params1 globals [
994        translate_ne globals destrs srcrs1 srcrs2;
995        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
996      ] start_lbl dest_lbl def
997    | Cne ⇒ translate_ne globals destrs srcrs1 srcrs2 start_lbl dest_lbl def
998    | Clt ⇒ translate_lt globals destrs destrs_prf srcrs1 srcrs2 start_lbl dest_lbl def
999    | Cgt ⇒ translate_lt globals destrs destrs_prf srcrs2 srcrs1 start_lbl dest_lbl def
1000    | Cle ⇒
1001      add_translates rtl_params1 globals [
1002        translate_lt globals destrs destrs_prf srcrs2 srcrs1;
1003        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
1004      ] start_lbl dest_lbl def
1005    | Cge ⇒
1006      add_translates rtl_params1 globals [
1007        translate_lt globals destrs destrs_prf srcrs1 srcrs2;
1008        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
1009      ] start_lbl dest_lbl def
1010    ]
1011  | Ocmpp c ⇒
1012    match c with
1013    [ Ceq ⇒
1014      add_translates rtl_params1 globals [
1015        translate_ne globals destrs srcrs1 srcrs2;
1016        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
1017      ] start_lbl dest_lbl def
1018    | Cne ⇒ translate_ne globals destrs srcrs1 srcrs2 start_lbl dest_lbl def
1019    | Clt ⇒ translate_lt globals destrs destrs_prf srcrs1 srcrs2 start_lbl dest_lbl def
1020    | Cgt ⇒ translate_lt globals destrs destrs_prf srcrs2 srcrs1 start_lbl dest_lbl def
1021    | Cle ⇒
1022      add_translates rtl_params1 globals [
1023        translate_lt globals destrs destrs_prf srcrs2 srcrs1;
1024        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
1025      ] start_lbl dest_lbl def
1026    | Cge ⇒
1027      add_translates rtl_params1 globals [
1028        translate_lt globals destrs destrs_prf srcrs1 srcrs2;
1029        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
1030      ] start_lbl dest_lbl def
1031    ]
1032  | _ ⇒ ? (* assert false, implemented in run time or float op *)
1033  ].
1034  [1:
1035    @sym_eq
1036    assumption
1037  |3,8,19,22,24,25:
1038    >srcrs1_destrs_prf
1039    assumption
1040  |4,9:
1041    normalize in nil_absrd;
1042    cases(not_le_Sn_O 0)
1043    #HYP cases(HYP nil_absrd)
1044  |5,10,20,21,23,26:
1045    >srcrs2_destrs_prf
1046    assumption
1047  |*:
1048    cases not_implemented (* XXX: yes, really *)
1049  ]
1050qed.
1051
1052definition translate_cond: ∀globals: list ident. list register → label → label → label → rtl_internal_function globals → rtl_internal_function globals ≝
1053  λglobals: list ident.
1054  λsrcrs: list register.
1055  λstart_lbl: label.
1056  λlbl_true: label.
1057  λlbl_false: label.
1058  λdef: rtl_internal_function globals.
1059  let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
1060  let 〈tmp_lbl, def〉 ≝ fresh_label rtl_params1 globals def in
1061  let init ≝ sequential rtl_params_ globals (INT rtl_params_ globals tmpr (zero …)) in
1062  let f ≝ λsrcr. sequential rtl_params_ globals (OP2 rtl_params_ globals Or tmpr tmpr srcr) in
1063  let def ≝ adds_graph rtl_params1 globals (init :: (map ? ? f srcrs)) start_lbl tmp_lbl def in
1064    add_graph rtl_params1 globals tmp_lbl (sequential rtl_params_ globals (COND rtl_params_ globals tmpr lbl_true) lbl_false) def.
1065
1066definition translate_load ≝
1067  λglobals: list ident.
1068  λaddr.
1069  λaddr_prf: 2 = |addr|.
1070  λdestrs: list register.
1071  λstart_lbl: label.
1072  λdest_lbl: label.
1073  λdef: rtl_internal_function globals.
1074  match fresh_regs_strong rtl_params0 globals def (|addr|) with
1075  [ mk_Sig def_save_addr save_addr_prf ⇒
1076    let def ≝ \fst def_save_addr in
1077    let save_addr ≝ \snd def_save_addr in
1078    match fresh_regs_strong rtl_params0 globals def (|addr|) with
1079    [ mk_Sig def_tmp_addr tmp_addr_prf ⇒
1080      let def ≝ \fst def_tmp_addr in
1081      let tmp_addr ≝ \snd def_tmp_addr in
1082      let 〈tmp_addr1, tmp_addr2〉 ≝ make_addr … tmp_addr ? in
1083      let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
1084      let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
1085      let insts_save_addr ≝ translate_move globals save_addr addr in
1086      let f ≝ λtranslates_off. λr.
1087        let 〈translates, off〉 ≝ translates_off in
1088        let translates ≝ translates @ [
1089          adds_graph rtl_params1 globals [
1090            sequential rtl_params_ globals (INT rtl_params_ globals tmpr off)
1091          ];
1092          translate_op2 globals Oaddp tmp_addr ? save_addr [dummy; tmpr] ? ?;
1093          adds_graph rtl_params1 globals [
1094            sequential rtl_params_ globals (LOAD rtl_params_ globals r tmp_addr1 tmp_addr2)
1095          ]
1096        ]
1097        in
1098        let 〈carry, result〉 ≝ half_add … off int_size in
1099          〈translates, result〉
1100      in
1101      let 〈translates, ignore〉 ≝ foldl … f 〈[ ], (zero ?)〉 destrs in
1102        add_translates rtl_params1 globals (insts_save_addr :: translates) start_lbl dest_lbl def
1103    ]
1104  ].
1105  [1: >tmp_addr_prf >save_addr_prf %
1106  |2: >save_addr_prf >tmp_addr_prf
1107      @addr_prf
1108  |3: >tmp_addr_prf normalize
1109      <addr_prf //
1110  |4: >tmp_addr_prf assumption
1111  ]
1112qed.
1113
1114definition translate_store ≝
1115  λglobals: list ident.
1116  λaddr.
1117  λaddr_prf: 2 = |addr|.
1118  λsrcrs: list register.
1119  λstart_lbl: label.
1120  λdest_lbl: label.
1121  λdef: rtl_internal_function globals.
1122  match fresh_regs_strong rtl_params0 globals def (|addr|) with
1123  [ mk_Sig def_tmp_addr tmp_addr_prf ⇒
1124    let def ≝ \fst def_tmp_addr in
1125    let tmp_addr ≝ \snd def_tmp_addr in
1126    let 〈tmp_addr1, tmp_addr2〉 ≝ make_addr … tmp_addr ? in
1127    let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
1128    let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
1129    let f ≝ λtranslate_off. λsrcr.
1130      let 〈translates, off〉 ≝ translate_off in
1131      let translates ≝ translates @ [
1132        adds_graph rtl_params1 globals [
1133          sequential rtl_params_ globals (INT rtl_params_ globals tmpr off)
1134        ];
1135        translate_op2 globals Oaddp tmp_addr … addr [dummy; tmpr] ? ?;
1136        adds_graph rtl_params1 globals [
1137          sequential rtl_params_ globals (STORE rtl_params_ globals tmp_addr1 tmp_addr2 srcr)
1138        ]
1139      ]
1140      in
1141        let 〈carry, result〉 ≝ half_add … off int_size in
1142          〈translates, result〉
1143    in
1144    let 〈translates, ignore〉 ≝ foldl … f 〈[ ], zero …〉 srcrs in
1145      add_translates rtl_params1 globals translates start_lbl dest_lbl def
1146  ].
1147  [1: >tmp_addr_prf %
1148  |2: >tmp_addr_prf normalize
1149      <addr_prf //
1150  |3: >tmp_addr_prf <addr_prf //
1151  |4: >tmp_addr_prf @addr_prf
1152  ]
1153qed.
1154
1155axiom fake_label: label.
1156
1157(* XXX: following conversation with CSC about the mix up in extension statements
1158        and extension instructions in RTL, use fake_label in calls to
1159        tailcall_* instructions. *)
1160definition translate_stmt ≝
1161  λglobals: list ident.
1162  λlenv: local_env.
1163  λlbl: label.
1164  λstmt.
1165  λdef: rtl_internal_function globals.
1166  match stmt with
1167  [ St_skip lbl' ⇒ add_graph … lbl (GOTO … lbl') def
1168  | St_cost cost_lbl lbl' ⇒ add_graph rtl_params1 globals lbl (sequential rtl_params_ globals (COST_LABEL rtl_params_ globals cost_lbl) lbl') def
1169  | St_const destr cst lbl' ⇒
1170    translate_cst globals cst (find_local_env destr lenv) lbl lbl' def
1171  | St_op1 ty ty' op1 destr srcr lbl' ⇒
1172    translate_op1 globals ty ty' op1 (find_local_env destr lenv) (find_local_env srcr lenv) … lbl lbl' def
1173  | St_op2 op2 destr srcr1 srcr2 lbl' ⇒
1174    translate_op2 globals op2 (find_local_env destr lenv) ? (find_local_env srcr1 lenv) (find_local_env srcr2 lenv) ? ? lbl lbl' def
1175    (* XXX: should we be ignoring this? *)
1176  | St_load ignore addr destr lbl' ⇒
1177    translate_load globals (find_local_env addr lenv) ? (find_local_env destr lenv) lbl lbl' def
1178    (* XXX: should we be ignoring this? *)
1179  | St_store ignore addr srcr lbl' ⇒
1180    translate_store globals (find_local_env addr lenv) ? (find_local_env srcr lenv) lbl lbl' def
1181  | St_call_id f args retr lbl' ⇒
1182    match retr with
1183    [ Some retr ⇒
1184      add_graph rtl_params1 globals lbl (sequential … (CALL_ID rtl_params1 globals f (rtl_args args lenv) (find_local_env retr lenv)) lbl') def
1185    | None ⇒ add_graph rtl_params1 globals lbl (sequential … (CALL_ID rtl_params1 globals f (rtl_args args lenv) [ ]) lbl') def
1186    ]
1187  | St_call_ptr f args retr lbl' ⇒
1188    match retr with
1189    [ None ⇒
1190      let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
1191        add_graph rtl_params1 globals lbl (sequential … (extension … (rtl_st_ext_call_ptr f1 f2 (rtl_args args lenv) [ ])) lbl') def
1192    | Some retr ⇒
1193      let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
1194        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
1195    ]
1196  | St_tailcall_id f args ⇒
1197    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
1198  | St_tailcall_ptr f args ⇒
1199    let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
1200      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
1201  | St_cond r lbl_true lbl_false ⇒
1202    translate_cond globals (find_local_env r lenv) lbl lbl_true lbl_false def
1203  | St_jumptable r l ⇒ ? (* assert false: not implemented yet *)
1204  | St_return ⇒ add_graph rtl_params1 globals lbl (RETURN …) def
1205  ].
1206  [10: cases not_implemented (* jtable case *)
1207  |*: cases daemon (* TODO: proofs regarding lengths of lists, examine! *)
1208  ]
1209qed.
1210
1211(* XXX: we use a "hack" here to circumvent the proof obligations required to
1212   create res, an empty internal_function record.  we insert into our graph
1213   the start and end nodes to ensure that they are in, and place dummy
1214   skip instructions at these nodes. *)
1215definition translate_internal ≝
1216  λglobals: list ident.
1217  λdef.
1218  let runiverse ≝ f_reggen def in
1219  let 〈lenv,runiverse〉 ≝ initialize_local_env runiverse
1220    (f_params def @ f_locals def) (f_result def) in
1221  let params ≝ map_list_local_env lenv (map … \fst (f_params def)) in
1222  let locals ≝ map_list_local_env lenv (map … \fst (f_locals def)) in
1223  let result ≝
1224    match (f_result def) with
1225    [ None ⇒ [ ]
1226    | Some r_typ ⇒
1227      let 〈r, typ〉 ≝ r_typ in
1228        find_local_env r lenv
1229    ]
1230  in
1231  let luniverse' ≝ f_labgen def in
1232  let runiverse' ≝ runiverse in
1233  let result' ≝ result in
1234  let params' ≝ params in
1235  let locals' ≝ locals in
1236  let stack_size' ≝ f_stacksize def in
1237  let entry' ≝ f_entry def in
1238  let exit' ≝ f_exit def in
1239  let graph' ≝ add LabelTag ? (empty_map LabelTag ?) entry' (GOTO rtl_params_ globals entry') in
1240  let graph' ≝ add LabelTag ? graph' exit' (GOTO rtl_params_ globals exit') in
1241  let res ≝
1242    mk_joint_internal_function … (rtl_params globals) luniverse' runiverse' result' params'
1243     locals' stack_size' graph' ? ? in
1244    foldi … (translate_stmt globals … lenv) (f_graph def) res.
1245[ % [@entry' | @graph_add_lookup @graph_add]
1246| % [@exit'  | @graph_add]]
1247qed.
1248
1249(*CSC: here we are not doing any alignment on variables, like it is done in OCaml
1250  because of CompCert heritage *)
1251definition rtlabs_to_rtl: RTLabs_program → rtl_program ≝
1252 λp. transform_program … p (transf_fundef … (translate_internal (prog_var_names …))).
Note: See TracBrowser for help on using the repository browser.