source: src/RTLabs/RTLabsToRTL.ma @ 1601

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

Files ported to new version of the standard library.

File size: 47.5 KB
RevLine 
[789]1include "RTLabs/syntax.ma".
2include "RTL/RTL.ma".
3include "common/AssocList.ma".
[1071]4include "common/FrontEndOps.ma".
[789]5include "common/Graphs.ma".
[1280]6include "joint/TranslateUtils.ma".
[1352]7include alias "ASM/BitVector.ma".
8include alias "arithmetics/nat.ma".
[789]9
[1047]10let rec register_freshes (runiverse: universe RegisterTag) (n: nat) on n ≝
11  match n with
[1281]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〉 ].
[1047]17
18definition complete_regs ≝
[1280]19  λglobals.
[1047]20  λdef.
21  λsrcrs1.
22  λsrcrs2.
[1280]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〉.
[1047]29
[1280]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
[1521]34 whd in match complete_regs; normalize nodelta
[1280]35 @leb_elim normalize nodelta #H
[1521]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)));]
[1280]38 cases (fresh_regs ????) #def' #fresh normalize >append_length
[1521]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
[1280]42         <plus_minus_m_m /2/ ]
43qed.
[1064]44
[1053]45definition size_of_sig_type ≝
[1052]46  λsig.
47  match sig with
[1047]48  [ ASTint isize sign ⇒
[1053]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
[1047]53  ].
[1053]54  cases not_implemented;
[1052]55qed.
[1047]56
[789]57inductive register_type: Type[0] ≝
58  | register_int: register → register_type
59  | register_ptr: register → register → register_type.
60
[1515]61definition local_env ≝ identifier_map RegisterTag (list register).
[789]62
[1280]63definition mem_local_env : register → local_env → bool ≝
[1515]64  λr,e. member … e r.
[1052]65
[1280]66definition add_local_env : register → list register → local_env → local_env ≝
[1515]67  λr,v,e. add … e r v.
[1052]68
[1280]69definition find_local_env : register → local_env → list register ≝
[1515]70  λr: register.λenv. lookup_def … env r [].
[1052]71
[789]72definition initialize_local_env_internal ≝
[1281]73  λlenv_runiverse.
[1053]74  λr_sig.
[1281]75  let 〈lenv,runiverse〉 ≝ lenv_runiverse in
[1053]76  let 〈r, sig〉 ≝ r_sig in
77  let size ≝ size_of_sig_type sig in
[1281]78  let 〈rs,runiverse〉 ≝ register_freshes runiverse size in
79    〈add_local_env r rs lenv,runiverse〉.
[789]80
81definition initialize_local_env ≝
82  λruniverse.
83  λregisters.
[1053]84  λresult.
[1050]85  let registers ≝ registers @
86    match result with
87    [ None ⇒ [ ]
88    | Some rt ⇒ [ rt ]
89    ]
90  in
[1515]91    foldl … initialize_local_env_internal 〈empty_map …,runiverse〉 registers.
[1280]92
[1050]93definition map_list_local_env_internal ≝
[1280]94  λlenv,res,r. res @ (find_local_env r lenv).
[1050]95   
96definition map_list_local_env ≝
[1280]97  λlenv,regs. foldl … (map_list_local_env_internal lenv) [ ] regs.
[1050]98
[1053]99definition make_addr ≝
100  λA.
101  λlst: list A.
[1343]102  λprf: 2 = length A lst.
103  match lst return λx. 2 = |x| → A × A with
[1053]104  [ nil ⇒ λlst_nil_prf. ?
105  | cons hd tl ⇒ λprf.
[1343]106    match tl return λx. 1 = |x| → A × A with
[1053]107    [ nil ⇒ λtl_nil_prf. ?
108    | cons hd' tl' ⇒ λtl_cons_prf. 〈hd, hd'〉
109    ] ?
110  ] prf.
111  [1: normalize in lst_nil_prf;
[1343]112      destruct(lst_nil_prf)
[1053]113  |2: normalize in prf;
[1343]114      @injective_S
[1066]115      assumption
[1053]116  |3: normalize in tl_nil_prf;
[1343]117      destruct(tl_nil_prf)
118  ]
[1053]119qed.
[1050]120
[789]121definition find_and_addr ≝
[1280]122  λr,lenv. make_addr ? (find_local_env r lenv).
[789]123
124definition rtl_args ≝
[1280]125  λregs_list,lenv. flatten … (map … (λr. find_local_env r lenv) regs_list).
[789]126
[1057]127definition translate_cst_int_internal ≝
[1282]128  λglobals,dest_lbl,r,i. sequential rtl_params_ globals (INT … r i) dest_lbl.
[1057]129
[1354]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.
[1308]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
[1325]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
[1601]178  [ mk_Sig reduced first_reduced_proof ⇒
[1325]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
[1601]185    [ mk_Sig reduced second_reduced_proof ⇒
[1325]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
[1601]189      [ mk_Sig reduced third_reduced_proof ⇒
[1325]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
[1307]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
[1308]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
[1601]227          [ mk_Sig bytes bytes_length_proof ⇒
[1308]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|)))
[1307]233    ]
234  | Ofloatconst float ⇒ ⊥
[1308]235  | Oaddrsymbol id offset ⇒
236    let 〈r1, r2〉 ≝ make_addr … destrs ? in
[1325]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
[1308]240  | Oaddrstack offset ⇒
241    let 〈r1, r2〉 ≝ make_addr … destrs ? in
[1343]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
[1325]243    let def ≝ translate_op globals Addc [r1] [r1] [r2] start_lbl dest_lbl def in
244      def
[1307]245  ].
[1308]246  [1: >bytes_length_proof
247      cut(size' = |destrs|)
248      [1: @eqb_implies_eq
249          assumption
250      |2: #EQ_HYP
251          <EQ_HYP %
252      ]
[1325]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 *)
[1307]256  ].
[1315]257qed.
[1307]258 
[1059]259definition translate_move_internal ≝
[1280]260  λglobals.
[1059]261  λdestr: register.
262  λsrcr: register.
[1283]263    sequential rtl_params_ globals (MOVE … 〈destr,srcr〉).
[1057]264
[1059]265definition translate_move ≝
[1280]266  λglobals.
[1059]267  λdestrs: list register.
268  λsrcrs: list register.
269  λstart_lbl: label.
[1071]270    match reduce_strong register register destrs srcrs with
[1601]271    [ mk_Sig crl_crr len_proof ⇒
[1060]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
[1283]276      let f_common ≝ translate_move_internal globals in
[1280]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
[1060]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 ≝
[1280]305  λglobals.
[1060]306  λdestrs.
307  λstart_lbl.
308  λdest_lbl.
[1280]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 … [
[1283]314        sequential rtl_params_ … (INT rtl_params_ ? tmp_zero (bitvector_of_nat ? 0))
[1060]315        ];
[1280]316      translate_move globals destrs zeros
[1060]317    ] start_lbl dest_lbl def.
318
[1285]319definition translate_cast_signed:
320    ∀globals: list ident. list register → ? → label → label → rtl_internal_function globals → rtl_internal_function globals ≝
321  λglobals: list ident.
[1060]322  λdestrs.
323  λsrcr.
324  λstart_lbl.
325  λdest_lbl.
326  λdef.
[1280]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
[1060]331  let insts ≝ [
[1285]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
[1280]339  let srcrs ≝ make … tmpr (length … destrs) in
[1285]340    add_translates rtl_params1 globals [
341      adds_graph rtl_params1 globals insts;
342      translate_move globals destrs srcrs
[1060]343    ] start_lbl dest_lbl def.
344
345definition translate_cast ≝
[1285]346  λglobals: list ident.
347  λsrc_size: nat.
348  λsrc_sign: signedness.
349  λdest_size: nat.
350  λdestrs: list register.
351  λsrcrs: list register.
[1066]352  match |srcrs| return λx. |srcrs| = x → ? with
[1285]353  [ O ⇒ λzero_prf. adds_graph rtl_params1 globals [ ]
[1060]354  | S n' ⇒ λsucc_prf.
355    if ltb dest_size src_size then
[1285]356      translate_move globals destrs srcrs
[1060]357    else
[1071]358      match reduce_strong register register destrs srcrs with
[1601]359      [ mk_Sig crl len_proof ⇒
[1060]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
[1285]364        let insts_common ≝ translate_move globals commonl commonr in
[1060]365        let sign_reg ≝ last_safe ? srcrs ? in
366        let insts_sign ≝
367          match src_sign with
[1285]368          [ Unsigned ⇒ translate_cast_unsigned globals restl
369          | Signed ⇒ translate_cast_signed globals restl sign_reg
[1060]370          ]
371        in
[1285]372          add_translates rtl_params1 globals [ insts_common; insts_sign ]
[1060]373      ]
374  ] (refl ? (|srcrs|)).
375  >succ_prf //
376qed.
377
378definition translate_negint ≝
[1285]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
[1060]389  let insts_init ≝ [
[1285]390    sequential … (SET_CARRY …);
391    sequential … (INT rtl_params_ globals tmpr (zero ?))
[1060]392  ] in
[1285]393  let f_add ≝ λdestr. sequential … (OP2 rtl_params_ globals Addc destr destr tmpr) in
[1060]394  let insts_add ≝ map … f_add destrs in
[1285]395    adds_graph rtl_params1 globals (insts_cmpl @ insts_init @ insts_add) start_lbl dest_lbl def.
[1060]396
[1285]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.
[1060]404  match destrs with
[1285]405  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … start_lbl) def
[1060]406  | cons destr destrs ⇒
[1285]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
[1060]411    let f ≝ λtmp_srcr. [
[1285]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)
[1060]418    ] in
[1285]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
[1060]423      ] start_lbl dest_lbl def
424  ].
425
[1066]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*)
[1064]431definition translate_op1 ≝
[1286]432  λglobals: list ident.
[1529]433  λty, ty'.
434  λop1: unary_operation ty ty'.
[1064]435  λdestrs: list register.
436  λsrcrs: list register.
[1062]437  λprf: |destrs| = |srcrs|.
[1064]438  λstart_lbl: label.
439  λdest_lbl: label.
[1286]440  λdef: rtl_internal_function globals.
[1060]441  match op1 with
[1529]442  [ Ocastint src_size src_sign _ _ ⇒
[1066]443    let dest_size ≝ |destrs| * 8 in
444    let src_size ≝ bitsize_of_intsize src_size in
[1286]445      translate_cast globals src_size src_sign dest_size destrs srcrs start_lbl dest_lbl def
[1529]446  | Onegint _ _ ⇒
[1286]447      translate_negint globals destrs srcrs start_lbl dest_lbl def prf
[1529]448  | Onotbool _ _ _ _ ⇒
[1286]449      translate_notbool globals destrs srcrs start_lbl dest_lbl def
[1529]450  | Onotint _ _ ⇒
[1286]451    let f ≝ λdestr. λsrcr. sequential rtl_params_ globals (OP1 … Cmpl destr srcr) in
[1060]452    let l ≝ map2 … f destrs srcrs prf in
[1286]453      adds_graph rtl_params1 globals l start_lbl dest_lbl def
[1529]454  | Optrofint _ _ r ⇒
[1286]455      translate_move globals destrs srcrs start_lbl dest_lbl def
[1529]456  | Ointofptr _ _ r ⇒
[1286]457      translate_move globals destrs srcrs start_lbl dest_lbl def
[1529]458  | Oid _ ⇒
[1286]459      translate_move globals destrs srcrs start_lbl dest_lbl def
[1066]460  | _ ⇒ ? (* float operations implemented in runtime *)
[1060]461  ].
[1066]462  cases not_implemented
463qed.
[1057]464
[1062]465let rec translate_mul1
[1286]466  (globals: list ident) (dummy: register) (tmpr: register)
467    (destrs: list register) (srcrs1: list register) (srcr2: register)
468      (start_lbl: label)
469        on srcrs1 ≝
[1062]470  match destrs with
[1286]471  [ nil ⇒ adds_graph rtl_params1 globals [ GOTO … ] start_lbl
[1062]472  | cons destr tl ⇒
473    match tl with
474    [ nil ⇒
475      match srcrs1 with
476      [ nil ⇒
[1286]477        adds_graph rtl_params1 globals [
478          sequential … (INT rtl_params_ globals tmpr (zero …));
479          sequential … (OP2 rtl_params_ globals Addc destr destr tmpr)
[1062]480        ] start_lbl
481      | cons srcr1 tl' ⇒
[1286]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)
[1062]485        ] start_lbl
486      ]
487    | cons destr2 destrs ⇒
488      match srcrs1 with
489      [ nil ⇒
[1286]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)
[1062]495          ];
[1286]496          translate_cst globals (Ointconst I8 (zero …)) destrs
[1062]497        ] start_lbl
498      | cons srcr1 srcrs1 ⇒
499        match destrs with
500        [ nil ⇒
[1286]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)
[1062]506            ];
[1286]507            translate_cst globals (Ointconst I8 (zero ?)) destrs
[1062]508          ] start_lbl
509        | cons destr2 destrs ⇒
[1286]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)
[1062]514            ];
[1286]515            translate_mul1 globals dummy tmpr (destr2 :: destrs) srcrs1 srcr2
[1062]516          ] start_lbl
517        ]
518      ]
519    ]
520  ].
521
522definition translate_muli ≝
[1286]523  λglobals: list ident.
[1062]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.
[1325]531  λi_prf: i ≤ |tmp_destrs|.
[1063]532  λtranslates: list ?.
533  λsrcr2i: register.
[1286]534  let 〈tmp_destrs1, tmp_destrs2〉 ≝ split … tmp_destrs i i_prf in
[1063]535  let tmp_destrs2' ≝
536    match tmp_destrs2 with
537    [ nil ⇒ [ ]
538    | cons tmp_destr2 tmp_destrs2 ⇒ [
[1286]539        adds_graph rtl_params1 globals [
540          sequential rtl_params_ globals (CLEAR_CARRY …);
541          sequential … (INT rtl_params_ globals tmp_destr2 (zero …))
[1063]542        ];
[1286]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 …)
[1063]547        ];
[1286]548        translate_op globals Addc destrs destrs tmp_destrs
[1063]549      ]
550    ]
551  in
552    translates @ tmp_destrs2'.
[1356]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.
[1358]593  [1: cases daemon (* XXX: to do *)
[1356]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.
[1358]607 
608definition foldi_strong ≝
[1356]609  λa: Type[0].
610  λb: Type[0].
611  λthe_list: list b.
[1358]612  λf: (∀i: nat. ∀proof: i < |the_list|. a → b → a).
[1356]613  λseed: a.
[1358]614    foldi_strong_internal a b (|the_list|) the_list f seed 0 ?.
[1356]615  //
616qed.
[1343]617 
[1063]618definition translate_mul ≝
[1287]619  λglobals: list ident.
[1063]620  λdestrs: list register.
621  λsrcrs1: list register.
622  λsrcrs2: list register.
[1343]623  λregs_proof: |destrs| = |srcrs2|.
[1063]624  λstart_lbl: label.
625  λdest_lbl: label.
[1287]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
[1331]629    match fresh_regs_strong rtl_params0 globals def (|destrs|) with
[1601]630    [ mk_Sig def_tmp_destrs tmp_destrs_prf ⇒
[1331]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
[1358]642        let f' ≝ λi. λi_proof: i < |srcrs2|. f i ? in
[1343]643        let insts_mul ≝ foldi_strong … srcrs2 f' [ ] in
[1331]644          add_translates rtl_params1 globals (insts_init @ insts_mul) start_lbl dest_lbl def
645    ].
646  >tmp_destrs_prf
[1343]647  >regs_proof
[1358]648  /2/
[1331]649qed.
[1062]650
[1064]651definition translate_divumodu8 ≝
[1287]652  λglobals: list ident.
[1064]653  λorder: bool.
654  λdestrs: list register.
655  λsrcr1: register.
656  λsrcr2: register.
657  λstart_lbl: label.
658  λdest_lbl: label.
[1287]659  λdef: rtl_internal_function globals.
[1064]660  match destrs with
[1287]661  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
[1064]662  | cons destr destrs ⇒
[1287]663    let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
[1064]664    let 〈destr1, destr2〉 ≝ match order with [ true ⇒ 〈destr, dummy〉 | _ ⇒ 〈dummy, destr〉 ] in
[1287]665    let inst_div ≝ adds_graph rtl_params1 globals [
666      sequential rtl_params_ globals (OPACCS … DivuModu destr1 destr2 srcr1 srcr2)
[1064]667    ]
668    in
[1287]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
[1064]671  ].
[1063]672
[1287]673definition translate_ne: ∀globals: list ident. ? → ? → ? → ? → ? → ? → rtl_internal_function globals ≝
674  λglobals: list ident.
[1064]675  λdestrs: list register.
676  λsrcrs1: list register.
677  λsrcrs2: list register.
678  λstart_lbl: label.
679  λdest_lbl: label.
[1287]680  λdef: rtl_internal_function globals.
[1064]681  match destrs with
[1287]682  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
[1064]683  | cons destr destrs ⇒
[1287]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
[1071]690    match reduce_strong register register tmp_srcrs1 tmp_srcrs2 with
[1601]691    [ mk_Sig crl their_proof ⇒
[1064]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
[1293]696      let rest ≝ restl @ restr in
[1064]697      let inits ≝ [
[1287]698        sequential … (INT rtl_params_ globals destr (zero …));
699        sequential … (INT rtl_params_ globals tmp_zero (zero …))
[1064]700      ]
701      in
702      let f_common ≝ λtmp_srcr1. λtmp_srcr2. [
[1287]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)
[1064]706      ]
707      in
[1287]708      let insts_common ≝ flatten … (map2 … f_common commonl commonr ?) in
[1064]709      let f_rest ≝ λtmp_srcr. [
[1287]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)
[1064]713      ]
714      in
[1287]715      let insts_rest ≝ flatten … (map … f_rest rest) in
[1064]716      let insts ≝ inits @ insts_common @ insts_rest in
[1287]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
[1064]720        ] start_lbl dest_lbl def
721    ]
722  ].
[1293]723  @their_proof
[1064]724qed.
725
726definition translate_eq_reg ≝
[1287]727  λglobals: list ident.
[1064]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
[1287]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) ].
[1064]744
745definition translate_eq_list ≝
[1287]746  λglobals: list ident.
[1064]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.
[1287]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).
[1064]757
758definition translate_atom ≝
[1287]759  λglobals: list ident.
[1064]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.
[1287]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) ].
[1064]776
777definition translate_lt_main ≝
[1287]778  λglobals: list ident.
[1064]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
[1287]791    let added_insts ≝ translate_atom globals tmp_zero tmp_one tmpr1 tmpr2 tmpr3 destr dummy_lbl leq srcr1 srcr2 in
[1064]792      〈insts @ added_insts, leq @ [〈srcr1, srcr2〉]〉
793  in
[1287]794    \fst (fold_left2 … f 〈[ ], [ ]〉 srcrs1 srcrs2 proof).
[1064]795
796definition fresh_regs_strong:
[1287]797  ∀globals. rtl_internal_function globals → ∀n: nat. Σfresh: (rtl_internal_function globals) × (list register). |\snd fresh| = n ≝
798  λglobals: list ident.
[1064]799  λdef.
800  λn.
[1287]801    fresh_regs rtl_params0 globals def n.
[1064]802  @fresh_regs_length
803qed.
804
805definition complete_regs_strong:
[1287]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.
[1064]808  λdef.
809  λleft.
810  λright.
[1287]811    complete_regs globals def left right.
[1064]812  @complete_regs_length
813qed.
814
815definition translate_lt ≝
[1287]816  λglobals: list ident.
[1064]817  λdestrs: list register.
[1306]818  λprf_destrs: 0 < |destrs|.
[1064]819  λsrcrs1: list register.
820  λsrcrs2: list register.
821  λstart_lbl: label.
822  λdest_lbl: label.
[1287]823  λdef: rtl_internal_function globals.
[1064]824  match destrs with
[1287]825  [ nil ⇒ add_graph rtl_params1 globals start_lbl (GOTO … dest_lbl) def
[1064]826  | _ ⇒
[1287]827    match fresh_regs_strong globals def (|destrs|) with
[1601]828    [ mk_Sig def_tmp_destrs tmp_destrs_proof ⇒
[1064]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
[1287]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
[1601]838      [ mk_Sig srcrs12_added srcrs12_proof ⇒
[1064]839        let srcrs1 ≝ \fst (\fst srcrs12_added) in
840        let srcrs2 ≝ \snd (\fst srcrs12_added) in
841        let added ≝ \snd srcrs12_added in
[1287]842        let srcrs1' ≝ rev … srcrs1 in
843        let srcrs2' ≝ rev … srcrs2 in
[1064]844        let insts_init ≝ [
[1287]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))
[1064]850          ]
851        ]
852        in
853        let insts_main ≝
[1287]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
[1064]858      ]
859    ]
860  ].
861  [2: >tmp_destrs_proof @prf_destrs
862  |1: normalize nodelta
[1529]863      generalize in match srcrs12_proof;
[1064]864      #HYP >rev_length >rev_length @HYP
865  ]
866qed.
867
868definition add_128_to_last ≝
[1287]869  λglobals: list ident.
[1064]870  λtmp_128: register.
871  λrs.
872  λprf: 0 < |rs|.
873  λstart_lbl: label.
874  match rs with
[1287]875  [ nil ⇒ adds_graph rtl_params1 globals [ ] start_lbl
[1064]876  | _ ⇒
[1287]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)
[1064]880      ] start_lbl
881  ].
882
883definition translate_lts ≝
[1287]884  λglobals: list ident.
[1064]885  λdestrs: list register.
[1306]886  λdestrs_prf: 0 < |destrs|.
[1064]887  λsrcrs1: list register.
888  λsrcrs2: list register.
889  λsrcrs1_lt_prf: 0 < |srcrs1|.
[1066]890  λsrcrs2_lt_prf: 0 < |srcrs2|.
[1064]891  λstart_lbl: label.
892  λdest_lbl: label.
[1287]893  λdef: rtl_internal_function globals.
894  match fresh_regs_strong globals def (|srcrs1|) with
[1601]895  [ mk_Sig def_tmp_srcrs1 srcrs1_prf ⇒
[1064]896    let def ≝ \fst def_tmp_srcrs1 in
897    let tmp_srcrs1 ≝ \snd def_tmp_srcrs1 in
[1287]898    match fresh_regs_strong globals def (|srcrs2|) with
[1601]899    [ mk_Sig def_tmp_srcrs2 srcrs2_prf ⇒
[1064]900      let def ≝ \fst def_tmp_srcrs2 in
901      let tmp_srcrs2 ≝ \snd def_tmp_srcrs2 in
[1287]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))
[1064]908          ];
[1287]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
[1064]912        ] start_lbl dest_lbl def
913    ]
914  ].
[1066]915  [1: >srcrs1_prf @srcrs1_lt_prf
916  |2: >srcrs2_prf @srcrs2_lt_prf
[1064]917  ]
918qed.
919
920definition translate_op2 ≝
[1288]921  λglobals: list ident.
[1064]922  λop2.
923  λdestrs: list register.
[1306]924  λdestrs_prf: 0 < |destrs|.
[1064]925  λsrcrs1: list register.
926  λsrcrs2: list register.
[1343]927  λsrcrs2_destrs_prf: |srcrs2| = |destrs|.
928  λsrcrs1_destrs_prf: |srcrs1| = |destrs|.
[1064]929  λstart_lbl: label.
930  λdest_lbl: label.
[1288]931  λdef: rtl_internal_function globals.
[1064]932  match op2 with
[1066]933  [ Oadd ⇒
[1288]934    translate_op globals Addc destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]935  | Oaddp ⇒
[1288]936    translate_op globals Addc destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]937  | Osub ⇒
[1288]938    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]939  | Osubpi ⇒
[1288]940    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]941  | Osubpp sz ⇒
[1288]942    translate_op globals Sub destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]943  | Omul ⇒
[1331]944    translate_mul globals destrs srcrs1 srcrs2 ? start_lbl dest_lbl def
[1066]945  | Odivu ⇒
[1288]946    match srcrs1 return λx. 0 < |x| → rtl_internal_function globals with
[1066]947    [ cons hd tl ⇒ λcons_prf.
948      match tl with
[1288]949      [ nil ⇒ translate_divumodu8 globals true destrs hd (hd_safe register srcrs2 ?) start_lbl dest_lbl def
[1066]950      | _ ⇒ ? (* not implemented *)
951      ]
952    | nil ⇒ λnil_absrd. ?
[1343]953    ] ?
[1066]954  | Omodu ⇒
[1288]955    match srcrs1 return λx. 0 < |x| → rtl_internal_function globals with
[1066]956    [ cons hd tl ⇒ λcons_prf.
957      match tl with
[1288]958      [ nil ⇒ translate_divumodu8 globals false destrs hd (hd_safe register srcrs2 ?) start_lbl dest_lbl def
[1066]959      | _ ⇒ ? (* not implemented *)
960      ]
961    | nil ⇒ λnil_absrd. ?
[1343]962    ] ?
[1066]963  | Oand ⇒
[1288]964    translate_op globals And destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]965  | Oor ⇒
[1288]966    translate_op globals Or destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]967  | Oxor ⇒
[1288]968    translate_op globals Xor destrs srcrs1 srcrs2 start_lbl dest_lbl def
[1066]969  | Ocmp c ⇒
[1064]970    match c with
[1066]971    [ Ceq ⇒
[1288]972      add_translates rtl_params1 globals [
973        translate_ne globals destrs srcrs1 srcrs2;
[1529]974        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1064]975      ] start_lbl dest_lbl def
[1288]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
[1066]979    | Cle ⇒
[1288]980      add_translates rtl_params1 globals [
981        translate_lts globals destrs destrs_prf srcrs2 srcrs1 ? ?;
[1529]982        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1064]983      ] start_lbl dest_lbl def
[1066]984    | Cge ⇒
[1288]985      add_translates rtl_params1 globals [
986        translate_lts globals destrs destrs_prf srcrs1 srcrs2 ? ?;
[1529]987        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1064]988      ] start_lbl dest_lbl def
989    ]
[1066]990  | Ocmpu c ⇒
991    match c with
992    [ Ceq ⇒
[1288]993      add_translates rtl_params1 globals [
994        translate_ne globals destrs srcrs1 srcrs2;
[1529]995        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1066]996      ] start_lbl dest_lbl def
[1288]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
[1066]1000    | Cle ⇒
[1288]1001      add_translates rtl_params1 globals [
1002        translate_lt globals destrs destrs_prf srcrs2 srcrs1;
[1529]1003        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1066]1004      ] start_lbl dest_lbl def
1005    | Cge ⇒
[1288]1006      add_translates rtl_params1 globals [
1007        translate_lt globals destrs destrs_prf srcrs1 srcrs2;
[1529]1008        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1066]1009      ] start_lbl dest_lbl def
1010    ]
1011  | Ocmpp c ⇒
1012    match c with
1013    [ Ceq ⇒
[1288]1014      add_translates rtl_params1 globals [
1015        translate_ne globals destrs srcrs1 srcrs2;
[1529]1016        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1066]1017      ] start_lbl dest_lbl def
[1288]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
[1066]1021    | Cle ⇒
[1288]1022      add_translates rtl_params1 globals [
1023        translate_lt globals destrs destrs_prf srcrs2 srcrs1;
[1529]1024        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1066]1025      ] start_lbl dest_lbl def
1026    | Cge ⇒
[1288]1027      add_translates rtl_params1 globals [
1028        translate_lt globals destrs destrs_prf srcrs1 srcrs2;
[1529]1029        translate_op1 globals ?? (Onotbool (ASTint I8 Unsigned) I8 Unsigned I) destrs destrs (refl ? (|destrs|))
[1066]1030      ] start_lbl dest_lbl def
1031    ]
1032  | _ ⇒ ? (* assert false, implemented in run time or float op *)
1033  ].
[1343]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 *)
[1066]1049  ]
1050qed.
1051
[1288]1052definition translate_cond: ∀globals: list ident. list register → label → label → label → rtl_internal_function globals → rtl_internal_function globals ≝
1053  λglobals: list ident.
[1066]1054  λsrcrs: list register.
1055  λstart_lbl: label.
1056  λlbl_true: label.
1057  λlbl_false: label.
[1288]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
[1290]1064    add_graph rtl_params1 globals tmp_lbl (sequential rtl_params_ globals (COND rtl_params_ globals tmpr lbl_true) lbl_false) def.
[1066]1065
1066definition translate_load ≝
[1292]1067  λglobals: list ident.
[1066]1068  λaddr.
[1343]1069  λaddr_prf: 2 = |addr|.
[1066]1070  λdestrs: list register.
1071  λstart_lbl: label.
1072  λdest_lbl: label.
[1292]1073  λdef: rtl_internal_function globals.
1074  match fresh_regs_strong rtl_params0 globals def (|addr|) with
[1601]1075  [ mk_Sig def_save_addr save_addr_prf ⇒
[1066]1076    let def ≝ \fst def_save_addr in
1077    let save_addr ≝ \snd def_save_addr in
[1292]1078    match fresh_regs_strong rtl_params0 globals def (|addr|) with
[1601]1079    [ mk_Sig def_tmp_addr tmp_addr_prf ⇒
[1066]1080      let def ≝ \fst def_tmp_addr in
1081      let tmp_addr ≝ \snd def_tmp_addr in
[1292]1082      let 〈tmp_addr1, tmp_addr2〉 ≝ make_addr … tmp_addr ? in
1083      let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
[1343]1084      let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
[1292]1085      let insts_save_addr ≝ translate_move globals save_addr addr in
[1066]1086      let f ≝ λtranslates_off. λr.
1087        let 〈translates, off〉 ≝ translates_off in
1088        let translates ≝ translates @ [
[1292]1089          adds_graph rtl_params1 globals [
1090            sequential rtl_params_ globals (INT rtl_params_ globals tmpr off)
[1066]1091          ];
[1343]1092          translate_op2 globals Oaddp tmp_addr ? save_addr [dummy; tmpr] ? ?;
[1292]1093          adds_graph rtl_params1 globals [
1094            sequential rtl_params_ globals (LOAD rtl_params_ globals r tmp_addr1 tmp_addr2)
[1066]1095          ]
1096        ]
1097        in
[1292]1098        let 〈carry, result〉 ≝ half_add … off int_size in
[1066]1099          〈translates, result〉
1100      in
[1292]1101      let 〈translates, ignore〉 ≝ foldl … f 〈[ ], (zero ?)〉 destrs in
1102        add_translates rtl_params1 globals (insts_save_addr :: translates) start_lbl dest_lbl def
[1066]1103    ]
1104  ].
[1343]1105  [1: >tmp_addr_prf >save_addr_prf %
1106  |2: >save_addr_prf >tmp_addr_prf
1107      @addr_prf
[1066]1108  |3: >tmp_addr_prf normalize
[1343]1109      <addr_prf //
1110  |4: >tmp_addr_prf assumption
[1066]1111  ]
1112qed.
1113
1114definition translate_store ≝
[1292]1115  λglobals: list ident.
[1066]1116  λaddr.
[1343]1117  λaddr_prf: 2 = |addr|.
[1066]1118  λsrcrs: list register.
1119  λstart_lbl: label.
1120  λdest_lbl: label.
[1292]1121  λdef: rtl_internal_function globals.
1122  match fresh_regs_strong rtl_params0 globals def (|addr|) with
[1601]1123  [ mk_Sig def_tmp_addr tmp_addr_prf ⇒
[1066]1124    let def ≝ \fst def_tmp_addr in
1125    let tmp_addr ≝ \snd def_tmp_addr in
[1292]1126    let 〈tmp_addr1, tmp_addr2〉 ≝ make_addr … tmp_addr ? in
1127    let 〈def, tmpr〉 ≝ fresh_reg rtl_params0 globals def in
[1343]1128    let 〈def, dummy〉 ≝ fresh_reg rtl_params0 globals def in
[1066]1129    let f ≝ λtranslate_off. λsrcr.
1130      let 〈translates, off〉 ≝ translate_off in
1131      let translates ≝ translates @ [
[1292]1132        adds_graph rtl_params1 globals [
1133          sequential rtl_params_ globals (INT rtl_params_ globals tmpr off)
[1066]1134        ];
[1343]1135        translate_op2 globals Oaddp tmp_addr … addr [dummy; tmpr] ? ?;
[1292]1136        adds_graph rtl_params1 globals [
1137          sequential rtl_params_ globals (STORE rtl_params_ globals tmp_addr1 tmp_addr2 srcr)
[1066]1138        ]
1139      ]
1140      in
[1292]1141        let 〈carry, result〉 ≝ half_add … off int_size in
[1066]1142          〈translates, result〉
1143    in
[1306]1144    let 〈translates, ignore〉 ≝ foldl … f 〈[ ], zero …〉 srcrs in
[1292]1145      add_translates rtl_params1 globals translates start_lbl dest_lbl def
[1066]1146  ].
[1343]1147  [1: >tmp_addr_prf %
[1066]1148  |2: >tmp_addr_prf normalize
[1343]1149      <addr_prf //
1150  |3: >tmp_addr_prf <addr_prf //
1151  |4: >tmp_addr_prf @addr_prf
[1066]1152  ]
1153qed.
1154
[1293]1155axiom fake_label: label.
1156
1157(* XXX: following conversation with CSC about the mix up in extension statements
[1296]1158        and extension instructions in RTL, use fake_label in calls to
1159        tailcall_* instructions. *)
[1066]1160definition translate_stmt ≝
[1292]1161  λglobals: list ident.
[1281]1162  λlenv: local_env.
[1066]1163  λlbl: label.
1164  λstmt.
[1292]1165  λdef: rtl_internal_function globals.
[1066]1166  match stmt with
[1282]1167  [ St_skip lbl' ⇒ add_graph … lbl (GOTO … lbl') def
[1293]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
[1066]1169  | St_const destr cst lbl' ⇒
[1293]1170    translate_cst globals cst (find_local_env destr lenv) lbl lbl' def
[1529]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
[1066]1173  | St_op2 op2 destr srcr1 srcr2 lbl' ⇒
[1306]1174    translate_op2 globals op2 (find_local_env destr lenv) ? (find_local_env srcr1 lenv) (find_local_env srcr2 lenv) ? ? lbl lbl' def
[1307]1175    (* XXX: should we be ignoring this? *)
[1066]1176  | St_load ignore addr destr lbl' ⇒
[1306]1177    translate_load globals (find_local_env addr lenv) ? (find_local_env destr lenv) lbl lbl' def
[1307]1178    (* XXX: should we be ignoring this? *)
[1066]1179  | St_store ignore addr srcr lbl' ⇒
[1306]1180    translate_store globals (find_local_env addr lenv) ? (find_local_env srcr lenv) lbl lbl' def
[1066]1181  | St_call_id f args retr lbl' ⇒
1182    match retr with
1183    [ Some retr ⇒
[1293]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
[1066]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
[1293]1191        add_graph rtl_params1 globals lbl (sequential … (extension … (rtl_st_ext_call_ptr f1 f2 (rtl_args args lenv) [ ])) lbl') def
[1066]1192    | Some retr ⇒
1193      let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
[1293]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
[1066]1195    ]
1196  | St_tailcall_id f args ⇒
[1293]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
[1066]1198  | St_tailcall_ptr f args ⇒
1199    let 〈f1, f2〉 ≝ find_and_addr f lenv ? in
[1293]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
[1066]1201  | St_cond r lbl_true lbl_false ⇒
[1292]1202    translate_cond globals (find_local_env r lenv) lbl lbl_true lbl_false def
[1066]1203  | St_jumptable r l ⇒ ? (* assert false: not implemented yet *)
[1292]1204  | St_return ⇒ add_graph rtl_params1 globals lbl (RETURN …) def
[1064]1205  ].
[1067]1206  [10: cases not_implemented (* jtable case *)
1207  |*: cases daemon (* TODO: proofs regarding lengths of lists, examine! *)
1208  ]
1209qed.
[1064]1210
[1073]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. *)
[1067]1215definition translate_internal ≝
[1293]1216  λglobals: list ident.
1217  λdef.
[1067]1218  let runiverse ≝ f_reggen def in
[1281]1219  let 〈lenv,runiverse〉 ≝ initialize_local_env runiverse
[1068]1220    (f_params def @ f_locals def) (f_result def) in
[1292]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
[1149]1223  let result ≝
[1068]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
[1053]1229    ]
[1068]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
[1293]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
[1068]1241  let res ≝
[1281]1242    mk_joint_internal_function … (rtl_params globals) luniverse' runiverse' result' params'
1243     locals' stack_size' graph' ? ? in
[1293]1244    foldi … (translate_stmt globals … lenv) (f_graph def) res.
[1281]1245[ % [@entry' | @graph_add_lookup @graph_add]
1246| % [@exit'  | @graph_add]]
[1073]1247qed.
[1053]1248
[1239]1249(*CSC: here we are not doing any alignment on variables, like it is done in OCaml
1250  because of CompCert heritage *)
[1293]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.