[789] | 1 | include "RTLabs/syntax.ma". |
---|
| 2 | include "RTL/RTL.ma". |
---|
| 3 | include "common/AssocList.ma". |
---|
[1071] | 4 | include "common/FrontEndOps.ma". |
---|
[789] | 5 | include "common/Graphs.ma". |
---|
[1280] | 6 | include "joint/TranslateUtils.ma". |
---|
[2286] | 7 | include "utilities/bindLists.ma". |
---|
[1352] | 8 | include alias "ASM/BitVector.ma". |
---|
| 9 | include alias "arithmetics/nat.ma". |
---|
[789] | 10 | |
---|
[2286] | 11 | definition rtl_fresh_reg: |
---|
| 12 | ∀globals.freshT RTL globals register ≝ |
---|
| 13 | λglobals,def. |
---|
| 14 | let 〈r, runiverse〉 ≝ fresh … (joint_if_runiverse … def) in |
---|
| 15 | 〈set_locals ?? (set_runiverse ?? def runiverse)(r::joint_if_locals ?? def), r〉. |
---|
[1047] | 16 | |
---|
[2286] | 17 | definition rtl_fresh_reg_no_local : |
---|
| 18 | ∀globals.freshT RTL globals register ≝ |
---|
| 19 | λglobals,def. |
---|
| 20 | let 〈r, runiverse〉 ≝ fresh … (joint_if_runiverse … def) in |
---|
| 21 | 〈set_runiverse ?? def runiverse, r〉. |
---|
[1047] | 22 | |
---|
[1053] | 23 | definition size_of_sig_type ≝ |
---|
[1052] | 24 | λsig. |
---|
| 25 | match sig with |
---|
[1047] | 26 | [ ASTint isize sign ⇒ |
---|
[2286] | 27 | match isize with [ I8 ⇒ 1 | I16 ⇒ 2 | I32 ⇒ 4 ] |
---|
| 28 | | ASTptr ⇒ 2 (* rgn ⇒ nat_of_bitvector ? ptr_size *) |
---|
[1047] | 29 | ]. |
---|
| 30 | |
---|
[789] | 31 | inductive register_type: Type[0] ≝ |
---|
| 32 | | register_int: register → register_type |
---|
| 33 | | register_ptr: register → register → register_type. |
---|
| 34 | |
---|
[1515] | 35 | definition local_env ≝ identifier_map RegisterTag (list register). |
---|
[789] | 36 | |
---|
[2286] | 37 | definition local_env_typed : |
---|
| 38 | list (register × typ) → local_env → Prop ≝ |
---|
| 39 | λl,env.All ? |
---|
| 40 | (λp.let 〈r, ty〉 ≝ p in ∃regs.lookup … env r = Some ? regs ∧ |
---|
| 41 | |regs| = size_of_sig_type ty) l. |
---|
[1052] | 42 | |
---|
[2286] | 43 | definition find_local_env ≝ λr.λlenv : local_env. |
---|
| 44 | λprf : r ∈ lenv.opt_safe … (lookup … lenv r) ?. |
---|
| 45 | lapply (in_map_domain … lenv r) |
---|
| 46 | >prf * #x #lookup_eq >lookup_eq % #ABS destruct(ABS) |
---|
| 47 | qed. |
---|
[1052] | 48 | |
---|
[2286] | 49 | lemma find_local_env_elim : ∀P : list register → Prop.∀r. ∀lenv: local_env.∀prf. |
---|
| 50 | (∀x.lookup … lenv r = Some ? x → P x) → P (find_local_env r lenv prf). |
---|
| 51 | #P#r#lenv#prf #H |
---|
| 52 | change with (P (opt_safe ???)) |
---|
| 53 | @opt_safe_elim assumption |
---|
| 54 | qed. |
---|
[1052] | 55 | |
---|
[2286] | 56 | definition find_local_env_arg : register → local_env → ? → list psd_argument ≝ |
---|
| 57 | λr,lenv,prf. map … (Reg ?) (find_local_env r lenv prf). |
---|
| 58 | |
---|
| 59 | definition initialize_local_env_internal : |
---|
| 60 | ∀globals. |
---|
| 61 | ((joint_internal_function RTL globals) × local_env) → (register×typ) → |
---|
| 62 | ((joint_internal_function RTL globals) × local_env) ≝ |
---|
| 63 | λglobals,def_env,r_sig. |
---|
| 64 | let 〈def,lenv〉 ≝ def_env in |
---|
[1053] | 65 | let 〈r, sig〉 ≝ r_sig in |
---|
| 66 | let size ≝ size_of_sig_type sig in |
---|
[2286] | 67 | let 〈def,rs〉 ≝ repeat_fresh … (rtl_fresh_reg_no_local globals) size def in |
---|
| 68 | 〈def,add … lenv r rs〉. |
---|
[789] | 69 | |
---|
[2286] | 70 | include alias "common/Identifiers.ma". |
---|
| 71 | let rec map_list_local_env |
---|
| 72 | lenv (regs : list (register×typ)) on regs : |
---|
| 73 | All ? (λpr.bool_to_Prop (\fst pr ∈ lenv)) regs → list register ≝ |
---|
| 74 | match regs return λx.All ?? x → ? with |
---|
| 75 | [ nil ⇒ λ_.[ ] |
---|
| 76 | | cons hd tl ⇒ λprf.find_local_env (\fst hd) lenv ? @ map_list_local_env lenv tl ? |
---|
| 77 | ].cases prf #A #B assumption qed. |
---|
| 78 | |
---|
| 79 | definition initialize_local_env : |
---|
| 80 | ∀globals. |
---|
| 81 | list (register×typ) → |
---|
| 82 | freshT RTL globals local_env ≝ |
---|
| 83 | λglobals,registers,def. |
---|
| 84 | foldl ?? (initialize_local_env_internal globals) 〈def,empty_map …〉 registers. |
---|
| 85 | |
---|
| 86 | lemma initialize_local_env_in : ∀globals,l,def,r. |
---|
| 87 | Exists ? (λx.\fst x = r) l → r ∈ \snd (initialize_local_env globals l def). |
---|
| 88 | #globals #l #U #r @(list_elim_left … l) |
---|
| 89 | [ * |
---|
| 90 | | * #tl #sig #hd #IH #G elim (Exists_append … G) -G |
---|
| 91 | whd in match initialize_local_env; normalize nodelta |
---|
| 92 | >foldl_step change with (initialize_local_env ???) in match (foldl ?????); |
---|
| 93 | [ #H lapply (IH H) |
---|
| 94 | | * [2: *] #EQ destruct(EQ) |
---|
| 95 | ] |
---|
| 96 | cases (initialize_local_env ???) |
---|
| 97 | #U' #env' [#G] whd in match initialize_local_env_internal; normalize nodelta |
---|
| 98 | elim (repeat_fresh ??????) #U'' #rs |
---|
| 99 | [ >mem_set_add @orb_Prop_r assumption |
---|
| 100 | | @mem_set_add_id |
---|
| 101 | ] |
---|
| 102 | qed. |
---|
| 103 | |
---|
| 104 | example proj1_rewrite : ∀A,B,a,b.∀pr : A×B.〈a,b〉 = pr → a = \fst pr. |
---|
| 105 | // qed-. |
---|
| 106 | example proj2_rewrite : ∀A,B,a,b.∀pr : A×B.〈a,b〉 = pr → b = \snd pr. |
---|
| 107 | // qed-. |
---|
| 108 | |
---|
| 109 | definition initialize_locals_params_ret : |
---|
| 110 | ∀globals. |
---|
| 111 | (* locals *) list (register×typ) → |
---|
| 112 | (* params *) list (register×typ) → |
---|
| 113 | (* return *) option (register×typ) → |
---|
| 114 | freshT RTL globals local_env ≝ |
---|
| 115 | λglobals,locals,params,ret,def. |
---|
| 116 | let 〈def',lenv〉 as EQ ≝ |
---|
| 117 | initialize_local_env globals |
---|
| 118 | ((match ret with |
---|
| 119 | [ Some r_sig ⇒ [r_sig] |
---|
| 120 | | None ⇒ [ ] |
---|
| 121 | ]) @ locals @ params) def in |
---|
| 122 | let locals' ≝ map_list_local_env lenv locals ? in |
---|
| 123 | let params' ≝ map_list_local_env lenv params ? in |
---|
| 124 | let ret' ≝ match ret return λx.ret = x → ? with |
---|
| 125 | [ Some r_sig ⇒ λprf.find_local_env (\fst r_sig) lenv ? |
---|
| 126 | | None ⇒ λ_.[ ] |
---|
| 127 | ] (refl …) in |
---|
| 128 | let def'' ≝ |
---|
| 129 | mk_joint_internal_function RTL globals |
---|
| 130 | (joint_if_luniverse … def') (joint_if_runiverse … def') ret' |
---|
| 131 | params' locals' (joint_if_stacksize … def') |
---|
| 132 | (joint_if_code … def') (joint_if_entry … def') (joint_if_exit … def') in |
---|
| 133 | 〈def'', lenv〉. @hide_prf |
---|
| 134 | [ >(proj2_rewrite ????? EQ) |
---|
| 135 | @initialize_local_env_in >prf %1 % |
---|
| 136 | |*: >(proj2_rewrite ????? EQ) |
---|
| 137 | @(All_mp ??? (λpr.initialize_local_env_in ??? (\fst pr))) |
---|
| 138 | [ @(All_mp … (λpr.Exists ? (λx.\fst x = \fst pr) params)) |
---|
| 139 | [ #a #H @Exists_append_r @Exists_append_r @H |
---|
| 140 | | generalize in match params; |
---|
[1050] | 141 | ] |
---|
[2286] | 142 | | @(All_mp … (λpr.Exists ? (λx.\fst x = \fst pr) locals)) |
---|
| 143 | [ #a #H @Exists_append_r @Exists_append_l @H |
---|
| 144 | | generalize in match locals; |
---|
| 145 | ] |
---|
| 146 | ] |
---|
| 147 | #l elim l [1,3: %] #hd #tl #IH % [1,3: %1 %] @(All_mp … IH) #x #G %2{G} |
---|
| 148 | ] |
---|
| 149 | qed. |
---|
[1280] | 150 | |
---|
[1053] | 151 | definition make_addr ≝ |
---|
| 152 | λA. |
---|
| 153 | λlst: list A. |
---|
[2286] | 154 | λprf: 2 = length A lst.〈nth_safe … 0 lst ?, nth_safe … 1 lst ?〉. <prf // |
---|
| 155 | qed. |
---|
[1050] | 156 | |
---|
[789] | 157 | definition find_and_addr ≝ |
---|
[2286] | 158 | λr,lenv,prf. make_addr ? (find_local_env r lenv prf). |
---|
[789] | 159 | |
---|
[2490] | 160 | definition find_and_addr_arg ≝ |
---|
| 161 | λr,lenv,prf. make_addr ? (find_local_env_arg r lenv prf). |
---|
| 162 | |
---|
[2286] | 163 | include alias "common/Identifiers.ma". |
---|
| 164 | let rec rtl_args (args : list register) (env : local_env) on args : |
---|
| 165 | All ? (λr.bool_to_Prop (r∈env)) args → list psd_argument ≝ |
---|
| 166 | match args return λx.All ?? x → ? with |
---|
| 167 | [ nil ⇒ λ_.[ ] |
---|
| 168 | | cons hd tl ⇒ λprf.find_local_env_arg hd env ? @ rtl_args tl env ? |
---|
| 169 | ]. |
---|
| 170 | cases prf #H #G assumption |
---|
| 171 | qed. |
---|
[789] | 172 | |
---|
[2286] | 173 | include alias "basics/lists/list.ma". |
---|
| 174 | let rec vrsplit A (m,n : nat) |
---|
| 175 | on m : Vector A (m*n) → Σs : list (Vector A n).|s| = m ≝ |
---|
| 176 | match m return λx.Vector A (x*n) → Sig (list ?) ? with |
---|
| 177 | [ O ⇒ λv.[ ] |
---|
| 178 | | S k ⇒ λv.let spl ≝ vsplit ? n … v in \fst spl :: vrsplit ? k n (\snd spl) |
---|
| 179 | ]. |
---|
| 180 | [ % |
---|
| 181 | | cases (vrsplit ????) #lst #EQ normalize >EQ % |
---|
| 182 | ] qed. |
---|
[1057] | 183 | |
---|
[1354] | 184 | definition split_into_bytes: |
---|
| 185 | ∀size. ∀int: bvint size. Σbytes: list Byte. |bytes| = size_intsize size ≝ |
---|
[2286] | 186 | λsize.vrsplit ? (size_intsize size) 8. |
---|
[1308] | 187 | |
---|
[2286] | 188 | let rec list_inject_All_aux A P (l : list A) on l : All A P l → list (Σx.P x) ≝ |
---|
| 189 | match l return λx.All A P x → ? with |
---|
| 190 | [ nil ⇒ λ_.[ ] |
---|
| 191 | | cons hd tl ⇒ λprf.«hd, ?» :: list_inject_All_aux A P tl ? |
---|
| 192 | ]. cases prf #H1 #H2 [@H1 | @H2] |
---|
[1308] | 193 | qed. |
---|
| 194 | |
---|
[2286] | 195 | include alias "basics/lists/list.ma". |
---|
| 196 | definition translate_op: |
---|
| 197 | ∀globals. Op2 → |
---|
| 198 | ∀dests : list register. |
---|
| 199 | ∀srcrs1 : list psd_argument. |
---|
| 200 | ∀srcrs2 : list psd_argument. |
---|
| 201 | |dests| = |srcrs1| → |srcrs1| = |srcrs2| → |
---|
| 202 | list (joint_seq RTL globals) |
---|
| 203 | ≝ |
---|
[1325] | 204 | λglobals: list ident. |
---|
| 205 | λop. |
---|
[2286] | 206 | λdestrs. |
---|
| 207 | λsrcrs1. |
---|
| 208 | λsrcrs2. |
---|
| 209 | λprf1,prf2. |
---|
| 210 | (* first, clear carry if op relies on it *) |
---|
| 211 | match op with |
---|
| 212 | [ Addc ⇒ [CLEAR_CARRY ??] |
---|
| 213 | | Sub ⇒ [CLEAR_CARRY ??] |
---|
| 214 | | _ ⇒ [ ] |
---|
| 215 | ] @ map3 ???? (OP2 RTL globals op) destrs srcrs1 srcrs2 prf1 prf2. |
---|
[1325] | 216 | |
---|
[2286] | 217 | definition cast_list : ∀A.A → ℕ → list A → list A ≝ |
---|
| 218 | λA,deflt,new_length,l. |
---|
| 219 | if leb (|l|) new_length then |
---|
| 220 | l @ make_list ? deflt (new_length - |l|) |
---|
| 221 | else |
---|
| 222 | lhd … l new_length. |
---|
[1057] | 223 | |
---|
[2286] | 224 | lemma length_make_list: |
---|
[1060] | 225 | ∀A: Type[0]. |
---|
| 226 | ∀elt: A. |
---|
| 227 | ∀n: nat. |
---|
[2286] | 228 | length ? (make_list A elt n) = n. |
---|
[1060] | 229 | #A #ELT #N |
---|
[2286] | 230 | elim N normalize // qed. |
---|
| 231 | |
---|
| 232 | lemma length_lhd : ∀A,l,n.|lhd A l n| = min (|l|) n. |
---|
| 233 | #A #l elim l -l |
---|
| 234 | [ * // |
---|
| 235 | | #hd #tl #IH * normalize [%] |
---|
| 236 | #n >IH normalize elim (leb ??) % |
---|
| 237 | ] |
---|
[1060] | 238 | qed. |
---|
| 239 | |
---|
[2286] | 240 | lemma length_cast_list : ∀A,dflt,n,l.|cast_list A dflt n l| = n. |
---|
| 241 | #A #dflt #n #l |
---|
| 242 | normalize @leb_elim #H normalize |
---|
| 243 | [ >length_append >length_make_list |
---|
| 244 | @sym_eq @minus_to_plus // |
---|
| 245 | | >length_lhd normalize @leb_elim |
---|
| 246 | [ #abs elim (absurd ? abs H) ] |
---|
| 247 | #_ % |
---|
| 248 | ] |
---|
| 249 | qed. |
---|
[1060] | 250 | |
---|
[2286] | 251 | definition translate_op_asym_unsigned : |
---|
| 252 | ∀globals.Op2 → list register → list psd_argument → list psd_argument → |
---|
| 253 | list (joint_seq RTL globals) ≝ |
---|
| 254 | λglobals,op,destrs,srcrs1,srcrs2. |
---|
| 255 | let l ≝ |destrs| in |
---|
| 256 | let srcrs1' ≝ cast_list ? (zero_byte : psd_argument) l srcrs1 in |
---|
| 257 | let srcrs2' ≝ cast_list ? (zero_byte : psd_argument) l srcrs2 in |
---|
| 258 | translate_op globals op destrs srcrs1' srcrs2' ??. |
---|
| 259 | normalize nodelta |
---|
| 260 | >length_cast_list [2: >length_cast_list ] % |
---|
[1060] | 261 | qed. |
---|
| 262 | |
---|
[2286] | 263 | let rec nat_to_args (size : nat) (k : nat) on size : Σl : list psd_argument.|l| = size ≝ |
---|
| 264 | match size with |
---|
| 265 | [ O ⇒ [ ] |
---|
| 266 | | S size' ⇒ |
---|
| 267 | (byte_of_nat k : psd_argument) :: nat_to_args size' (k ÷ 8) |
---|
| 268 | ]. [ % | cases (nat_to_args ??) #res #EQ normalize >EQ % ] qed. |
---|
[1060] | 269 | |
---|
[2286] | 270 | definition size_of_cst ≝ λtyp.λcst : constant typ.match cst with |
---|
| 271 | [ Ointconst size _ _ ⇒ size_intsize size |
---|
| 272 | | _ ⇒ 2 |
---|
[1060] | 273 | ]. |
---|
| 274 | |
---|
[2286] | 275 | definition cst_well_defd : ∀ty.list ident → constant ty → Prop ≝ λty,globals,cst. |
---|
| 276 | match cst with |
---|
| 277 | [ Oaddrsymbol id _ ⇒ member id (eq_identifier ?) globals |
---|
| 278 | | _ ⇒ True |
---|
[1060] | 279 | ]. |
---|
[2286] | 280 | |
---|
| 281 | definition translate_cst : |
---|
| 282 | ∀ty. |
---|
| 283 | ∀globals: list ident. |
---|
| 284 | ∀cst_sig: Σcst : constant ty.cst_well_defd ty globals cst. |
---|
| 285 | ∀destrs: list register. |
---|
| 286 | |destrs| = size_of_cst ? cst_sig → |
---|
| 287 | list (joint_seq RTL globals) |
---|
| 288 | ≝ |
---|
| 289 | λty,globals,cst_sig,destrs. |
---|
| 290 | match pi1 … cst_sig in constant return λty'.λx : constant ty'. |
---|
| 291 | cst_well_defd ty' ? x → |destrs| = size_of_cst ty' x → ? |
---|
| 292 | with |
---|
| 293 | [ Ointconst size sign const ⇒ λcst_prf,prf. |
---|
| 294 | map2 … (λr.λb : Byte.r ← b) destrs |
---|
| 295 | (split_into_bytes size const) ? |
---|
| 296 | | Oaddrsymbol id offset ⇒ λcst_prf,prf. |
---|
| 297 | let 〈r1, r2〉 ≝ make_addr … destrs ? in |
---|
| 298 | [ADDRESS RTL globals id ? r1 r2] |
---|
| 299 | | Oaddrstack offset ⇒ λcst_prf,prf. |
---|
| 300 | let 〈r1, r2〉 ≝ make_addr … destrs ? in |
---|
| 301 | [(rtl_stack_address r1 r2 : joint_seq RTL globals)] |
---|
| 302 | ] (pi2 … cst_sig). |
---|
[2490] | 303 | [ cases (split_into_bytes ??) #lst |
---|
[2286] | 304 | #EQ >EQ >prf whd in ⊢ (??%?); cases size % |
---|
[2490] | 305 | | @cst_prf |
---|
[2286] | 306 | |*: >prf % |
---|
| 307 | ] |
---|
[1066] | 308 | qed. |
---|
[2286] | 309 | |
---|
| 310 | definition translate_move : |
---|
| 311 | ∀globals. |
---|
| 312 | ∀destrs: list register. |
---|
| 313 | ∀srcrs: list psd_argument. |
---|
| 314 | |destrs| = |srcrs| → list (joint_seq RTL globals) ≝ |
---|
| 315 | λglobals,destrs,srcrs,length_eq. |
---|
| 316 | map2 … (λdst,src.dst ← src) destrs srcrs length_eq. |
---|
[1057] | 317 | |
---|
[2286] | 318 | definition sign_mask : ∀globals.register → psd_argument → |
---|
| 319 | list (joint_seq RTL globals) ≝ |
---|
| 320 | (* this sets destr to 0xFF if s is neg, 0x00 o.w. Done like that: |
---|
| 321 | byte in destr if srcr is: neg | pos |
---|
| 322 | destr ← srcr | 127 11...1 | 01...1 |
---|
| 323 | destr ← destr <rot< 1 1...11 | 1...10 |
---|
| 324 | destr ← INC destr 0....0 | 1....1 |
---|
| 325 | destr ← CPL destr 1....1 | 0....0 |
---|
| 326 | *) |
---|
| 327 | λglobals,destr,srca. |
---|
| 328 | match srca with |
---|
| 329 | [ Reg srcr ⇒ |
---|
| 330 | let byte_127 : Byte ≝ false ::: maximum ? in |
---|
| 331 | [destr ← srcr .Or. byte_127 ; |
---|
| 332 | destr ← .Rl. destr ; |
---|
| 333 | destr ← .Inc. destr ; |
---|
| 334 | destr ← .Cmpl. destr ] |
---|
[2490] | 335 | | Imm b ⇒ |
---|
| 336 | if sign_bit … b then |
---|
| 337 | [ destr ← (maximum … : Byte) ] |
---|
| 338 | else |
---|
| 339 | [ destr ← zero_byte ] |
---|
[1062] | 340 | ]. |
---|
| 341 | |
---|
[2286] | 342 | definition translate_cast_signed : |
---|
| 343 | ∀globals : list ident. |
---|
| 344 | list register → psd_argument → |
---|
| 345 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 346 | λglobals,destrs,srca. |
---|
| 347 | ν tmp in |
---|
| 348 | (sign_mask ? tmp srca @ |
---|
| 349 | translate_move ? destrs (make_list ? (Reg ? tmp) (|destrs|)) ?). |
---|
| 350 | >length_make_list % qed. |
---|
[1356] | 351 | |
---|
[2286] | 352 | definition translate_fill_with_zero : |
---|
| 353 | ∀globals : list ident. |
---|
| 354 | list register → list (joint_seq RTL globals) ≝ |
---|
| 355 | λglobals,destrs. |
---|
| 356 | match nat_to_args (|destrs|) 0 with |
---|
| 357 | [ mk_Sig res prf ⇒ translate_move ? destrs res ?]. |
---|
| 358 | // qed. |
---|
[1356] | 359 | |
---|
[2286] | 360 | let rec last A (l : list A) on l : option A ≝ |
---|
| 361 | match l with |
---|
| 362 | [ nil ⇒ None ? |
---|
| 363 | | cons hd tl ⇒ |
---|
| 364 | match tl with |
---|
| 365 | [ nil ⇒ Some ? hd |
---|
| 366 | | _ ⇒ last A tl |
---|
| 367 | ] |
---|
| 368 | ]. |
---|
[1356] | 369 | |
---|
[2286] | 370 | lemma last_not_empty : ∀A,l. |
---|
| 371 | match l with [ nil ⇒ False | _ ⇒ True ] → |
---|
| 372 | match last A l with |
---|
| 373 | [ None ⇒ False |
---|
| 374 | | _ ⇒ True ]. |
---|
| 375 | #A #l elim l [ * ] |
---|
| 376 | #hd * [ #_ * % ] |
---|
| 377 | #hd' #tl #IH * @(IH I) |
---|
[1356] | 378 | qed. |
---|
[2286] | 379 | |
---|
| 380 | definition translate_op_asym_signed : |
---|
| 381 | ∀globals.Op2 → list register → list psd_argument → list psd_argument → |
---|
| 382 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 383 | λglobals,op,destrs,srcrs1,srcrs2. |
---|
| 384 | νtmp1,tmp2 in |
---|
| 385 | let l ≝ |destrs| in |
---|
| 386 | let f ≝ λsrcrs,tmp. |
---|
| 387 | let srcrs_l ≝ |srcrs| in |
---|
| 388 | if leb srcrs_l l then |
---|
| 389 | match last … srcrs with |
---|
| 390 | [ Some last ⇒ |
---|
| 391 | 〈srcrs @ make_list … (Reg ? tmp) (l - srcrs_l), |
---|
| 392 | sign_mask … tmp last〉 |
---|
| 393 | | None ⇒ |
---|
| 394 | 〈make_list … (zero_byte : psd_argument) l, [ ]〉 |
---|
[1331] | 395 | ] |
---|
[2286] | 396 | else |
---|
| 397 | 〈lhd … srcrs l, [ ]〉 in |
---|
| 398 | let prf : ∀srcrs,tmp.|destrs| = |\fst (f srcrs tmp)| ≝ ? in |
---|
| 399 | let srcrs1init ≝ f srcrs1 tmp1 in |
---|
| 400 | let srcrs2init ≝ f srcrs2 tmp2 in |
---|
| 401 | \snd srcrs1init @ \snd srcrs2init @ |
---|
| 402 | translate_op globals op destrs (\fst srcrs1init) (\fst srcrs2init) ??. |
---|
| 403 | [ @prf | <prf @prf ] |
---|
| 404 | #srcrs #tmp normalize nodelta |
---|
| 405 | @leb_elim #H normalize nodelta |
---|
| 406 | [ lapply (last_not_empty … srcrs) |
---|
| 407 | cases (last ??) |
---|
| 408 | [ cases srcrs |
---|
| 409 | [ #_ normalize >length_make_list % |
---|
| 410 | | #hd #tl #abs elim(abs I) |
---|
| 411 | ] |
---|
| 412 | | #last #_ normalize nodelta |
---|
| 413 | >length_append >length_make_list |
---|
| 414 | @minus_to_plus // |
---|
| 415 | ] |
---|
| 416 | | >length_lhd normalize @leb_elim |
---|
| 417 | [ #G elim (absurd … G H) |
---|
| 418 | | #_ % |
---|
| 419 | ] |
---|
| 420 | ] |
---|
[1331] | 421 | qed. |
---|
[1062] | 422 | |
---|
[2286] | 423 | (* using size of lists as size of ints *) |
---|
| 424 | definition translate_cast : |
---|
| 425 | ∀globals: list ident. |
---|
| 426 | signedness → list register → list register → |
---|
| 427 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 428 | λglobals,src_sign,destrs,srcrs. |
---|
| 429 | match reduce_strong ?? destrs srcrs with |
---|
| 430 | [ mk_Sig t prf ⇒ |
---|
| 431 | let src_common ≝ \fst (\fst t) in |
---|
| 432 | let src_rest ≝ \snd (\fst t) in |
---|
| 433 | let dst_common ≝ \fst (\snd t) in |
---|
| 434 | let dst_rest ≝ \snd (\snd t) in |
---|
| 435 | (* first, move the common part *) |
---|
| 436 | translate_move ? src_common (map … (Reg ?) dst_common) ? @@ |
---|
| 437 | match src_rest return λ_.bind_new ?? with |
---|
| 438 | [ nil ⇒ (* upcast *) |
---|
| 439 | match src_sign return λ_.bind_new ?? with |
---|
| 440 | [ Unsigned ⇒ translate_fill_with_zero ? dst_rest |
---|
| 441 | | Signed ⇒ |
---|
| 442 | match last … srcrs (* = src_common *) with |
---|
| 443 | [ Some src_last ⇒ translate_cast_signed ? dst_rest src_last |
---|
| 444 | | None ⇒ (* srcrs is empty *) translate_fill_with_zero ? dst_rest |
---|
| 445 | ] |
---|
| 446 | ] |
---|
| 447 | | _ ⇒ (* downcast, nothing else to do *) [ ] |
---|
[1064] | 448 | ] |
---|
| 449 | ]. |
---|
[2286] | 450 | >length_map @prf qed. |
---|
| 451 | |
---|
| 452 | definition translate_notint : |
---|
| 453 | ∀globals : list ident. |
---|
| 454 | ∀destrs : list register. |
---|
| 455 | ∀srcrs_arg : list register. |
---|
| 456 | |destrs| = |srcrs_arg| → list (joint_seq RTL globals) ≝ |
---|
| 457 | λglobals, destrs, srcrs, prf. |
---|
| 458 | map2 ??? (OP1 RTL globals Cmpl) destrs srcrs prf. |
---|
[1063] | 459 | |
---|
[2286] | 460 | definition translate_negint : ∀globals.? → ? → ? → bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
[1287] | 461 | λglobals: list ident. |
---|
[1064] | 462 | λdestrs: list register. |
---|
[2286] | 463 | λsrcrs: list register. |
---|
| 464 | λprf: |destrs| = |srcrs|. (* assert in caml code *) |
---|
| 465 | translate_notint … destrs srcrs prf @ |
---|
| 466 | match nat_to_args (|destrs|) 1 with |
---|
| 467 | [ mk_Sig res prf' ⇒ |
---|
| 468 | translate_op ? Add destrs (map … (Reg ?) destrs) res ?? |
---|
| 469 | ]. |
---|
| 470 | >length_map // qed. |
---|
| 471 | |
---|
| 472 | definition translate_notbool: |
---|
| 473 | ∀globals : list ident. |
---|
| 474 | list register → list register → |
---|
| 475 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 476 | λglobals,destrs,srcrs. |
---|
[1064] | 477 | match destrs with |
---|
[2286] | 478 | [ nil ⇒ [ ] |
---|
| 479 | | cons destr destrs' ⇒ |
---|
| 480 | translate_fill_with_zero ? destrs' @@ |
---|
| 481 | match srcrs return λ_.bind_new ?? with |
---|
| 482 | [ nil ⇒ [destr ← zero_byte] |
---|
| 483 | | cons srcr srcrs' ⇒ |
---|
| 484 | (destr ← srcr) ::: |
---|
| 485 | map register (joint_seq RTL globals) (λr. destr ← destr .Or. r) srcrs' @@ |
---|
| 486 | (* now destr is non-null iff srcrs was non-null *) |
---|
| 487 | CLEAR_CARRY ?? ::: |
---|
| 488 | (* many uses of 0, better not use immediates *) |
---|
| 489 | ν tmp in |
---|
| 490 | [tmp ← zero_byte ; |
---|
| 491 | destr ← tmp .Sub. tmp ; |
---|
| 492 | (* now carry bit is set iff destr was non-null *) |
---|
| 493 | destr ← tmp .Addc. tmp] |
---|
| 494 | ] |
---|
| 495 | ]. |
---|
| 496 | |
---|
| 497 | definition translate_op1 : ∀globals.? → ? → ? → ? → ? → ? → ? → |
---|
| 498 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 499 | λglobals. |
---|
| 500 | λty, ty'. |
---|
| 501 | λop1: unary_operation ty ty'. |
---|
| 502 | λdestrs: list register. |
---|
| 503 | λsrcrs: list register. |
---|
| 504 | λprf1: |destrs| = size_of_sig_type ty'. |
---|
| 505 | λprf2: |srcrs| = size_of_sig_type ty. |
---|
| 506 | match op1 |
---|
| 507 | return λty'',ty'''.λx : unary_operation ty'' ty'''.ty'' = ty → ty''' = ty' → |
---|
| 508 | bind_new (localsT RTL) (list (joint_seq RTL globals)) with |
---|
| 509 | [ Ocastint _ src_sign _ _ ⇒ λeq1,eq2. |
---|
| 510 | translate_cast globals src_sign destrs srcrs |
---|
| 511 | | Onegint sz sg ⇒ λeq1,eq2. |
---|
| 512 | translate_negint globals destrs srcrs ? |
---|
| 513 | | Onotbool _ _ _ _ ⇒ λeq1,eq2. |
---|
| 514 | translate_notbool globals destrs srcrs |
---|
| 515 | | Onotint sz sg ⇒ λeq1,eq2. |
---|
| 516 | translate_notint globals destrs srcrs ? |
---|
| 517 | | Optrofint sz sg ⇒ λeq1,eq2. |
---|
| 518 | translate_cast globals Unsigned destrs srcrs |
---|
| 519 | | Ointofptr sz sg ⇒ λeq1,eq2. |
---|
| 520 | translate_cast globals Unsigned destrs srcrs |
---|
| 521 | | Oid t ⇒ λeq1,eq2. |
---|
| 522 | translate_move globals destrs (map … (Reg ?) srcrs) ? |
---|
| 523 | | _ ⇒ λeq1,eq2.? (* float operations implemented in runtime *) |
---|
| 524 | ] (refl …) (refl …). |
---|
[2490] | 525 | destruct >prf1 >prf2 [3: >length_map ] // |
---|
[1064] | 526 | qed. |
---|
| 527 | |
---|
[2286] | 528 | include alias "arithmetics/nat.ma". |
---|
[1064] | 529 | |
---|
[2286] | 530 | definition translate_mul_i : |
---|
| 531 | ∀globals. |
---|
| 532 | register → register → |
---|
| 533 | (* size of destination and sources *) |
---|
| 534 | ∀n : ℕ. |
---|
| 535 | (* the temporary destination, with a dummy register at the end *) |
---|
| 536 | ∀tmp_destrs_dummy : list register. |
---|
| 537 | ∀srcrs1,srcrs2 : list psd_argument. |
---|
| 538 | |tmp_destrs_dummy| = S n → |
---|
| 539 | n = |srcrs1| → |
---|
| 540 | |srcrs1| = |srcrs2| → |
---|
| 541 | (* the position of the least significant byte of the result we compute at |
---|
| 542 | this stage (goes from 0 to n in the main function) *) |
---|
| 543 | ∀k : ℕ. |
---|
| 544 | lt k n → |
---|
| 545 | (* the position of the byte in the first source we will use in this stage. |
---|
| 546 | the position in the other source will be k - i *) |
---|
| 547 | (Σi.i<S k) → |
---|
| 548 | (* the accumulator *) |
---|
| 549 | list (joint_seq RTL globals) → |
---|
| 550 | list (joint_seq RTL globals) ≝ |
---|
| 551 | λglobals,a,b,n,tmp_destrs_dummy,srcrs1,srcrs2, |
---|
| 552 | tmp_destrs_dummy_prf,srcrs1_prf,srcrs2_prf,k,k_prf,i_sig,acc. |
---|
| 553 | (* the following will expand to |
---|
| 554 | a, b ← srcrs1[i] * srcrs2[k-i] |
---|
| 555 | tmp_destrs_dummy[k] ← tmp_destrs_dummy[k] + a |
---|
| 556 | tmp_destrs_dummy[k+1] ← tmp_destrs_dummy[k+1] + b + C |
---|
| 557 | tmp_destrs_dummy[k+2] ← tmp_destrs_dummy[k+2] + 0 + C |
---|
| 558 | ... |
---|
| 559 | tmp_destrs_dummy[n] ← tmp_destrs_dummy[n] + 0 + C |
---|
| 560 | ( all calculations on tmp_destrs_dummy[n] will be eliminated with |
---|
| 561 | liveness analysis) *) |
---|
| 562 | match i_sig with |
---|
| 563 | [ mk_Sig i i_prf ⇒ |
---|
| 564 | (* we pad the result of a byte multiplication with zeros in order |
---|
| 565 | for the bit to be carried. Redundant calculations will be eliminated |
---|
| 566 | by constant propagation. *) |
---|
| 567 | let args : list psd_argument ≝ |
---|
| 568 | [Reg ? a;Reg ? b] @ make_list ? (zero_byte : psd_argument) (n - 1 - k) in |
---|
| 569 | let tmp_destrs_view : list register ≝ |
---|
| 570 | ltl ? tmp_destrs_dummy k in |
---|
| 571 | ❮a, b❯ ← (nth_safe ? i srcrs1 ?) .Mul. (nth_safe ? (k - i) srcrs2 ?) :: |
---|
| 572 | translate_op … Add tmp_destrs_view (map … (Reg ?) tmp_destrs_view) args ?? @ |
---|
| 573 | acc |
---|
| 574 | ]. |
---|
| 575 | [ @lt_plus_to_minus [ @le_S_S_to_le assumption | <srcrs2_prf <srcrs1_prf |
---|
| 576 | whd >(plus_n_O (S k)) @le_plus // ] |
---|
| 577 | | <srcrs1_prf |
---|
| 578 | @(transitive_le … i_prf k_prf) |
---|
| 579 | | >length_map // |
---|
| 580 | | >length_map |
---|
| 581 | >length_ltl |
---|
| 582 | >tmp_destrs_dummy_prf >length_append |
---|
| 583 | >length_make_list |
---|
| 584 | normalize in ⊢ (???(?%?)); |
---|
| 585 | >plus_minus_commutative |
---|
| 586 | [2: @le_plus_to_minus_r <plus_n_Sm <plus_n_O assumption] |
---|
| 587 | cut (S n = 2 + (n - 1)) |
---|
| 588 | [2: #EQ >EQ %] |
---|
| 589 | >plus_minus_commutative |
---|
| 590 | [2: @(transitive_le … k_prf) //] |
---|
| 591 | @sym_eq |
---|
| 592 | @plus_to_minus % |
---|
| 593 | ] qed. |
---|
[1064] | 594 | |
---|
[2286] | 595 | definition translate_mul : ∀globals.?→?→?→?→?→bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 596 | λglobals : list ident. |
---|
| 597 | λdestrs : list register. |
---|
| 598 | λsrcrs1 : list psd_argument. |
---|
| 599 | λsrcrs2 : list psd_argument. |
---|
| 600 | λsrcrs1_prf : |destrs| = |srcrs1|. |
---|
| 601 | λsrcrs2_prf : |srcrs1| = |srcrs2|. |
---|
| 602 | (* needed fresh registers *) |
---|
| 603 | νa in |
---|
| 604 | νb in |
---|
| 605 | (* temporary registers for the result are created, so to avoid overwriting |
---|
| 606 | sources *) |
---|
| 607 | νν |destrs| as tmp_destrs with tmp_destrs_prf in |
---|
| 608 | νdummy in |
---|
| 609 | (* the step calculating all products with least significant byte going in the |
---|
| 610 | k-th position of the result *) |
---|
| 611 | let translate_mul_k : (Σk.k<|destrs|) → list (joint_seq RTL globals) → |
---|
| 612 | list (joint_seq RTL globals) ≝ |
---|
| 613 | λk_sig,acc.match k_sig with |
---|
| 614 | [ mk_Sig k k_prf ⇒ |
---|
| 615 | foldr … (translate_mul_i ? a b (|destrs|) |
---|
| 616 | (tmp_destrs @ [dummy]) srcrs1 srcrs2 |
---|
| 617 | ? srcrs1_prf srcrs2_prf k k_prf) acc (range_strong (S k)) |
---|
| 618 | ] in |
---|
| 619 | (* initializing tmp_destrs to zero |
---|
| 620 | dummy is intentionally uninitialized *) |
---|
| 621 | translate_fill_with_zero … tmp_destrs @ |
---|
| 622 | (* the main body, roughly: |
---|
| 623 | for k in 0 ... n-1 do |
---|
| 624 | for i in 0 ... k do |
---|
| 625 | translate_mul_i … k … i *) |
---|
| 626 | foldr … translate_mul_k [ ] (range_strong (|destrs|)) @ |
---|
| 627 | (* epilogue: saving the result *) |
---|
| 628 | translate_move … destrs (map … (Reg ?) tmp_destrs) ?. |
---|
| 629 | [ >length_map >tmp_destrs_prf // |
---|
| 630 | | >length_append <plus_n_Sm <plus_n_O // |
---|
| 631 | ] |
---|
[1064] | 632 | qed. |
---|
| 633 | |
---|
[2286] | 634 | definition translate_divumodu8 : ∀globals.?→?→?→?→?→?→ |
---|
| 635 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
[1287] | 636 | λglobals: list ident. |
---|
[2286] | 637 | λdiv_not_mod: bool. |
---|
[1064] | 638 | λdestrs: list register. |
---|
[2286] | 639 | λsrcrs1: list psd_argument. |
---|
| 640 | λsrcrs2: list psd_argument. |
---|
| 641 | λsrcrs1_prf : |destrs| = |srcrs1|. |
---|
| 642 | λsrcrs2_prf : |srcrs1| = |srcrs2|. |
---|
| 643 | match destrs return λx.x = destrs → bind_new ?? with |
---|
| 644 | [ nil ⇒ λ_.[ ] |
---|
| 645 | | cons destr destrs' ⇒ λeq_destrs. |
---|
| 646 | match destrs' with |
---|
| 647 | [ nil ⇒ |
---|
| 648 | match srcrs1 return λx.x = srcrs1 → bind_new ?? with |
---|
| 649 | [ nil ⇒ λeq_srcrs1.⊥ |
---|
| 650 | | cons srcr1 srcrs1' ⇒ λeq_srcrs1. |
---|
| 651 | match srcrs2 return λx.x = srcrs2 → bind_new ?? with |
---|
| 652 | [ nil ⇒ λeq_srcrs2.⊥ |
---|
| 653 | | cons srcr2 srcrs2' ⇒ λeq_srcrs2. |
---|
| 654 | νdummy in |
---|
| 655 | let 〈destr1, destr2〉 ≝ |
---|
| 656 | if div_not_mod then 〈destr, dummy〉 else 〈dummy, destr〉 in |
---|
| 657 | [❮destr1, destr2❯ ← srcr1 .DivuModu. srcr2] |
---|
| 658 | ] (refl …) |
---|
| 659 | ] (refl …) |
---|
| 660 | | _ ⇒ ? (* not implemented *) |
---|
[1064] | 661 | ] |
---|
[2286] | 662 | ] (refl …). |
---|
| 663 | [3: elim not_implemented] |
---|
| 664 | destruct normalize in srcrs1_prf; normalize in srcrs2_prf; destruct qed. |
---|
| 665 | |
---|
| 666 | (* Paolo: to be moved elsewhere *) |
---|
| 667 | let rec foldr2 (A : Type[0]) (B : Type[0]) (C : Type[0]) (f : A→B→C→C) (init : C) (l1 : list A) (l2 : list B) |
---|
| 668 | (prf : |l1| = |l2|) on l1 : C ≝ |
---|
| 669 | match l1 return λx.x = l1 → C with |
---|
| 670 | [ nil ⇒ λ_.init |
---|
| 671 | | cons a l1' ⇒ λeq_l1. |
---|
| 672 | match l2 return λy.y = l2 → C with |
---|
| 673 | [ nil ⇒ λeq_l2.⊥ |
---|
| 674 | | cons b l2' ⇒ λeq_l2. |
---|
| 675 | f a b (foldr2 A B C f init l1' l2' ?) |
---|
| 676 | ] (refl …) |
---|
| 677 | ] (refl …). |
---|
| 678 | destruct normalize in prf; [destruct|//] |
---|
[1064] | 679 | qed. |
---|
| 680 | |
---|
[2286] | 681 | definition translate_ne: ∀globals: list ident.?→?→?→?→ |
---|
| 682 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
[1287] | 683 | λglobals: list ident. |
---|
[2286] | 684 | λdestrs: list register. |
---|
| 685 | λsrcrs1: list psd_argument. |
---|
| 686 | λsrcrs2: list psd_argument. |
---|
| 687 | match destrs return λ_.|srcrs1| = |srcrs2| → bind_new ?? with |
---|
| 688 | [ nil ⇒ λ_.[ ] |
---|
| 689 | | cons destr destrs' ⇒ λEQ. |
---|
| 690 | translate_fill_with_zero … destrs' @@ |
---|
| 691 | match srcrs1 return λx.|x| = |srcrs2| → bind_new ?? with |
---|
| 692 | [ nil ⇒ λ_.[destr ← zero_byte] |
---|
| 693 | | cons srcr1 srcrs1' ⇒ |
---|
| 694 | match srcrs2 return λx.S (|srcrs1'|) = |x| → bind_new ?? with |
---|
| 695 | [ nil ⇒ λEQ.⊥ |
---|
| 696 | | cons srcr2 srcrs2' ⇒ λEQ. |
---|
| 697 | νtmpr in |
---|
| 698 | let f : psd_argument → psd_argument → list (joint_seq RTL globals) → list (joint_seq RTL globals) ≝ |
---|
| 699 | λs1,s2,acc. |
---|
| 700 | tmpr ← s1 .Xor. s2 :: |
---|
| 701 | destr ← destr .Or. tmpr :: |
---|
| 702 | acc in |
---|
| 703 | let epilogue : list (joint_seq RTL globals) ≝ |
---|
| 704 | [ CLEAR_CARRY ?? ; |
---|
| 705 | tmpr ← zero_byte .Sub. destr ; |
---|
| 706 | (* now carry bit is 1 iff destrs != 0 *) |
---|
| 707 | destr ← zero_byte .Addc. zero_byte ] in |
---|
| 708 | destr ← srcr1 .Xor. srcr2 :: |
---|
| 709 | foldr2 ??? f epilogue srcrs1' srcrs2' ? |
---|
| 710 | ] |
---|
| 711 | ] EQ |
---|
| 712 | ]. normalize in EQ; destruct(EQ) assumption qed. |
---|
[1064] | 713 | |
---|
[2286] | 714 | (* if destrs is 0 or 1, it inverses it. To be used after operations that |
---|
| 715 | ensure this. *) |
---|
| 716 | definition translate_toggle_bool : ∀globals.?→list (joint_seq RTL globals) ≝ |
---|
[1287] | 717 | λglobals: list ident. |
---|
[1064] | 718 | λdestrs: list register. |
---|
[2286] | 719 | match destrs with |
---|
| 720 | [ nil ⇒ [ ] |
---|
| 721 | | cons destr _ ⇒ [destr ← .Cmpl. destr] |
---|
[1064] | 722 | ]. |
---|
[2286] | 723 | |
---|
| 724 | definition translate_lt_unsigned : |
---|
| 725 | ∀globals. |
---|
| 726 | ∀destrs: list register. |
---|
| 727 | ∀srcrs1: list psd_argument. |
---|
| 728 | ∀srcrs2: list psd_argument. |
---|
| 729 | |srcrs1| = |srcrs2| → |
---|
| 730 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 731 | λglobals,destrs,srcrs1,srcrs2,srcrs_prf. |
---|
| 732 | match destrs with |
---|
| 733 | [ nil ⇒ [ ] |
---|
| 734 | | cons destr destrs' ⇒ |
---|
| 735 | ν tmpr in |
---|
| 736 | (translate_fill_with_zero … destrs' @ |
---|
| 737 | (* I perform a subtraction, but the only interest is in the carry bit *) |
---|
| 738 | translate_op ? Sub (make_list … tmpr (|srcrs1|)) srcrs1 srcrs2 ? srcrs_prf @ |
---|
| 739 | [ destr ← zero_byte .Addc. zero_byte ]) |
---|
| 740 | ]. |
---|
| 741 | >length_make_list % qed. |
---|
[1064] | 742 | |
---|
[2286] | 743 | (* shifts signed integers by adding 128 to the most significant byte |
---|
| 744 | it replaces it with a fresh register which must be provided *) |
---|
| 745 | let rec shift_signed globals |
---|
| 746 | (tmp : register) |
---|
| 747 | (srcrs : list psd_argument) on srcrs : |
---|
| 748 | Σt : (list psd_argument) × (list (joint_seq RTL globals)).|\fst t| = |srcrs| ≝ |
---|
| 749 | let byte_128 : Byte ≝ true ::: bv_zero ? in |
---|
| 750 | match srcrs with |
---|
| 751 | [ nil ⇒ 〈[ ],[ ]〉 |
---|
| 752 | | cons srcr srcrs' ⇒ |
---|
| 753 | match srcrs' with |
---|
| 754 | [ nil ⇒ 〈[ Reg ? tmp ], [ tmp ← srcr .Add. byte_128 ]〉 |
---|
| 755 | | _ ⇒ |
---|
| 756 | let re ≝ shift_signed globals tmp srcrs' in |
---|
| 757 | 〈srcr :: \fst re, \snd re〉 |
---|
[1064] | 758 | ] |
---|
[1066] | 759 | ]. |
---|
[2286] | 760 | [1,2: % |
---|
| 761 | |*: cases re * #a #b >p1 normalize #EQ >EQ % |
---|
| 762 | ] qed. |
---|
| 763 | |
---|
| 764 | definition translate_lt_signed : |
---|
| 765 | ∀globals. |
---|
| 766 | ∀destrs: list register. |
---|
| 767 | ∀srcrs1: list psd_argument. |
---|
| 768 | ∀srcrs2: list psd_argument. |
---|
| 769 | |srcrs1| = |srcrs2| → |
---|
| 770 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 771 | λglobals,destrs,srcrs1,srcrs2,srcrs_prf. |
---|
| 772 | νtmp_last_s1 in |
---|
| 773 | νtmp_last_s2 in |
---|
| 774 | let p1 ≝ shift_signed globals tmp_last_s1 srcrs1 in |
---|
| 775 | let new_srcrs1 ≝ \fst p1 in |
---|
| 776 | let shift_srcrs1 ≝ \snd p1 in |
---|
| 777 | let p2 ≝ shift_signed globals tmp_last_s2 srcrs2 in |
---|
| 778 | let new_srcrs2 ≝ \fst p2 in |
---|
| 779 | let shift_srcrs2 ≝ \snd p2 in |
---|
| 780 | shift_srcrs1 @@ shift_srcrs2 @@ |
---|
| 781 | translate_lt_unsigned globals destrs new_srcrs1 new_srcrs2 ?. |
---|
| 782 | whd in match new_srcrs1; whd in match new_srcrs2; |
---|
| 783 | cases p1 |
---|
| 784 | cases p2 |
---|
| 785 | // |
---|
[1066] | 786 | qed. |
---|
| 787 | |
---|
[2286] | 788 | definition translate_lt : bool→∀globals.?→?→?→?→bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 789 | λis_unsigned,globals,destrs,srcrs1,srcrs2,srcrs_prf. |
---|
| 790 | if is_unsigned then |
---|
| 791 | translate_lt_unsigned globals destrs srcrs1 srcrs2 srcrs_prf |
---|
| 792 | else |
---|
| 793 | translate_lt_signed globals destrs srcrs1 srcrs2 srcrs_prf. |
---|
| 794 | |
---|
| 795 | definition translate_cmp ≝ |
---|
| 796 | λis_unsigned,globals,cmp,destrs,srcrs1,srcrs2,srcrs_prf. |
---|
| 797 | match cmp with |
---|
| 798 | [ Ceq ⇒ |
---|
| 799 | translate_ne globals destrs srcrs1 srcrs2 srcrs_prf @@ |
---|
| 800 | translate_toggle_bool globals destrs |
---|
| 801 | | Cne ⇒ |
---|
| 802 | translate_ne globals destrs srcrs1 srcrs2 srcrs_prf |
---|
| 803 | | Clt ⇒ |
---|
| 804 | translate_lt is_unsigned globals destrs srcrs1 srcrs2 srcrs_prf |
---|
| 805 | | Cgt ⇒ |
---|
| 806 | translate_lt is_unsigned globals destrs srcrs2 srcrs1 ? |
---|
| 807 | | Cle ⇒ |
---|
| 808 | translate_lt is_unsigned globals destrs srcrs2 srcrs1 ? @@ |
---|
| 809 | translate_toggle_bool globals destrs |
---|
| 810 | | Cge ⇒ |
---|
| 811 | translate_lt is_unsigned globals destrs srcrs1 srcrs2 srcrs_prf @@ |
---|
| 812 | translate_toggle_bool globals destrs |
---|
| 813 | ]. |
---|
| 814 | // qed. |
---|
| 815 | |
---|
| 816 | definition translate_op2 : |
---|
| 817 | ∀globals.∀ty1,ty2,ty3.∀op : binary_operation ty1 ty2 ty3. |
---|
| 818 | ∀destrs : list register. |
---|
| 819 | ∀srcrs1,srcrs2 : list psd_argument. |
---|
| 820 | |destrs| = size_of_sig_type ty3 → |
---|
| 821 | |srcrs1| = size_of_sig_type ty1 → |
---|
| 822 | |srcrs2| = size_of_sig_type ty2 → |
---|
| 823 | bind_new (localsT RTL) (list (joint_seq RTL globals)) ≝ |
---|
| 824 | λglobals,ty1,ty2,ty3,op2,destrs,srcrs1,srcrs2. |
---|
| 825 | match op2 return λty1,ty2,ty3.λx : binary_operation ty1 ty2 ty3. |
---|
| 826 | ? = size_of_sig_type ty3 → ? = size_of_sig_type ty1 → ? = size_of_sig_type ty2 → |
---|
| 827 | bind_new ?? with |
---|
| 828 | [ Oadd sz sg ⇒ λprf1,prf2,prf3. |
---|
| 829 | translate_op globals Add destrs srcrs1 srcrs2 ?? |
---|
| 830 | | Oaddp sz ⇒ λprf1,prf2,prf3. |
---|
| 831 | translate_op_asym_signed globals Add destrs srcrs1 srcrs2 |
---|
| 832 | | Osub sz sg ⇒ λprf1,prf2,prf2. |
---|
| 833 | translate_op globals Sub destrs srcrs1 srcrs2 ?? |
---|
| 834 | | Osubpi sz ⇒ λprf1,prf2,prf3. |
---|
| 835 | translate_op_asym_signed globals Add destrs srcrs1 srcrs2 |
---|
| 836 | | Osubpp sz ⇒ λprf1,prf2,prf3. |
---|
| 837 | translate_op_asym_unsigned globals Sub destrs srcrs1 srcrs2 |
---|
| 838 | | Omul sz sg ⇒ λprf1,prf2,prf3. |
---|
| 839 | translate_mul globals destrs srcrs1 srcrs2 ?? |
---|
| 840 | | Odivu sz ⇒ λprf1,prf2,prf3. |
---|
| 841 | translate_divumodu8 globals true destrs srcrs1 srcrs2 ?? |
---|
| 842 | | Omodu sz ⇒ λprf1,prf2,prf3. |
---|
| 843 | translate_divumodu8 globals false destrs srcrs1 srcrs2 ?? |
---|
| 844 | | Oand sz sg ⇒ λprf1,prf2,prf3. |
---|
| 845 | translate_op globals And destrs srcrs1 srcrs2 ?? |
---|
| 846 | | Oor sz sg ⇒ λprf1,prf2,prf3. |
---|
| 847 | translate_op globals Or destrs srcrs1 srcrs2 ?? |
---|
| 848 | | Oxor sz sg ⇒ λprf1,prf2,prf3. |
---|
| 849 | translate_op globals Xor destrs srcrs1 srcrs2 ?? |
---|
| 850 | | Ocmp sz sg1 sg2 c ⇒ λprf1,prf2,prf3. |
---|
| 851 | translate_cmp false globals c destrs srcrs1 srcrs2 ? |
---|
| 852 | | Ocmpu sz sg c ⇒ λprf1,prf2,prf3. |
---|
| 853 | translate_cmp true globals c destrs srcrs1 srcrs2 ? |
---|
| 854 | | Ocmpp sg c ⇒ λprf1,prf2,prf3. |
---|
| 855 | let is_Ocmpp ≝ 0 in |
---|
| 856 | translate_cmp true globals c destrs srcrs1 srcrs2 ? |
---|
| 857 | | _ ⇒ ⊥ (* assert false, implemented in run time or float op *) |
---|
| 858 | ]. try @not_implemented // |
---|
| 859 | qed. |
---|
| 860 | |
---|
| 861 | definition translate_cond: ∀globals: list ident. list register → label → |
---|
| 862 | bind_seq_block RTL globals (joint_step RTL globals) ≝ |
---|
[1288] | 863 | λglobals: list ident. |
---|
[1066] | 864 | λsrcrs: list register. |
---|
| 865 | λlbl_true: label. |
---|
[2286] | 866 | match srcrs return λ_.bind_seq_block RTL ? (joint_step ??) with |
---|
| 867 | [ nil ⇒ bret … 〈[ ], NOOP ??〉 |
---|
| 868 | | cons srcr srcrs' ⇒ |
---|
| 869 | ν tmpr in |
---|
| 870 | let f : register → joint_seq RTL globals ≝ |
---|
| 871 | λr. tmpr ← tmpr .Or. r in |
---|
| 872 | bret … 〈MOVE RTL globals 〈tmpr,srcr〉 :: |
---|
| 873 | map ?? f srcrs', (COND … tmpr lbl_true : joint_step ??) 〉 |
---|
| 874 | ]. |
---|
[1066] | 875 | |
---|
[2286] | 876 | (* Paolo: to be controlled (original seemed overly complex) *) |
---|
| 877 | definition translate_load : ∀globals.?→?→?→bind_new (localsT RTL) ? ≝ |
---|
[1292] | 878 | λglobals: list ident. |
---|
[2286] | 879 | λaddr : list psd_argument. |
---|
[1343] | 880 | λaddr_prf: 2 = |addr|. |
---|
[1066] | 881 | λdestrs: list register. |
---|
[2286] | 882 | ν tmp_addr_l in |
---|
| 883 | ν tmp_addr_h in |
---|
| 884 | let f ≝ λdestr : register.λacc : list (joint_seq RTL globals). |
---|
| 885 | LOAD RTL globals destr (Reg ? tmp_addr_l) (Reg ? tmp_addr_h) :: |
---|
| 886 | translate_op ? Add |
---|
| 887 | [tmp_addr_l ; tmp_addr_h] |
---|
| 888 | [tmp_addr_l ; tmp_addr_h] |
---|
| 889 | [zero_byte ; (int_size : Byte)] ? ? @ acc in |
---|
| 890 | translate_move … [tmp_addr_l ; tmp_addr_h] addr ? @@ |
---|
| 891 | foldr … f [ ] destrs. |
---|
| 892 | [1: <addr_prf |
---|
| 893 | ] // qed. |
---|
| 894 | |
---|
| 895 | definition translate_store : ∀globals.?→?→?→bind_new (localsT RTL) ? ≝ |
---|
[1292] | 896 | λglobals: list ident. |
---|
[2286] | 897 | λaddr : list psd_argument. |
---|
[1343] | 898 | λaddr_prf: 2 = |addr|. |
---|
[2286] | 899 | λsrcrs: list psd_argument. |
---|
| 900 | ν tmp_addr_l in |
---|
| 901 | ν tmp_addr_h in |
---|
| 902 | let f ≝ λsrcr : psd_argument.λacc : list (joint_seq RTL globals). |
---|
| 903 | STORE RTL globals (Reg ? tmp_addr_l) (Reg ? tmp_addr_h) srcr :: |
---|
| 904 | translate_op … Add |
---|
| 905 | [tmp_addr_l ; tmp_addr_h] |
---|
| 906 | [tmp_addr_l ; tmp_addr_h] |
---|
| 907 | [zero_byte ; (int_size : Byte)] ? ? @ acc in |
---|
| 908 | translate_move … [tmp_addr_l ; tmp_addr_h] addr ? @@ |
---|
| 909 | foldr … f [ ] srcrs. |
---|
| 910 | [1: <addr_prf] // qed. |
---|
| 911 | |
---|
| 912 | lemma lenv_typed_reg_typed_ok1 : |
---|
| 913 | ∀locals,env,r,ty. |
---|
| 914 | local_env_typed locals env → |
---|
| 915 | Exists ? (λx:register×typ.〈r,ty〉=x) locals → |
---|
| 916 | ∀prf. |
---|
| 917 | |find_local_env r env prf| = size_of_sig_type ty. |
---|
| 918 | #locals #env #r #ty #env_typed #r_ok |
---|
| 919 | elim (Exists_All … r_ok env_typed) |
---|
| 920 | * #r' #ty' * #EQ destruct(EQ) * #rs * #EQ1 #EQ2 #prf |
---|
| 921 | whd in match find_local_env; normalize nodelta |
---|
| 922 | @opt_safe_elim #rs' >EQ1 #EQ' destruct assumption |
---|
[1066] | 923 | qed. |
---|
| 924 | |
---|
[2286] | 925 | lemma lenv_typed_arg_typed_ok1 : |
---|
| 926 | ∀locals,env,r,ty. |
---|
| 927 | local_env_typed locals env → |
---|
| 928 | Exists ? (λx:register×typ.〈r,ty〉=x) locals → |
---|
| 929 | ∀prf. |
---|
| 930 | |find_local_env_arg r env prf| = size_of_sig_type ty. |
---|
| 931 | #locals #env #r #ty #env_typed #r_ok #prf |
---|
| 932 | whd in match find_local_env_arg; normalize nodelta |
---|
| 933 | >length_map @lenv_typed_reg_typed_ok1 assumption |
---|
| 934 | qed. |
---|
[1293] | 935 | |
---|
[2286] | 936 | lemma lenv_typed_reg_typed_ok2 : |
---|
| 937 | ∀locals,env,r,ty. |
---|
| 938 | local_env_typed locals env → |
---|
| 939 | Exists ? (λx:register×typ.〈r,ty〉=x) locals → |
---|
| 940 | r ∈ env. |
---|
| 941 | #locals #env #r #ty #env_typed #r_ok |
---|
| 942 | elim (Exists_All … r_ok env_typed) |
---|
| 943 | * #r' #ty' * #EQ destruct(EQ) * #rs * #EQ1 #EQ2 |
---|
| 944 | whd in ⊢ (?%); |
---|
| 945 | >EQ1 % |
---|
| 946 | qed. |
---|
| 947 | |
---|
| 948 | lemma cst_size_ok : ∀ty,cst.size_of_sig_type ty=size_of_cst ty cst. |
---|
| 949 | #ty * -ty [*] // |
---|
| 950 | qed. |
---|
| 951 | |
---|
| 952 | definition translate_statement : ∀globals, locals.∀env. |
---|
| 953 | local_env_typed locals env → |
---|
| 954 | ∀stmt : statement.statement_typed locals stmt → |
---|
| 955 | 𝚺b : |
---|
| 956 | bind_seq_block RTL globals (joint_step RTL globals) + |
---|
| 957 | bind_seq_block RTL globals (joint_fin_step RTL). |
---|
| 958 | if is_inl … b then label else unit ≝ |
---|
| 959 | λglobals,locals,lenv,lenv_typed,stmt. |
---|
| 960 | match stmt return λstmt.statement_typed locals stmt → 𝚺b:bind_seq_block ???+bind_seq_block ???.if is_inl … b then label else unit with |
---|
| 961 | [ St_skip lbl' ⇒ λstmt_typed. |
---|
| 962 | ❬NOOP ??, lbl'❭ |
---|
| 963 | | St_cost cost_lbl lbl' ⇒ λstmt_typed. |
---|
| 964 | ❬COST_LABEL … cost_lbl, lbl'❭ |
---|
| 965 | | St_const ty destr cst lbl' ⇒ λstmt_typed. |
---|
| 966 | ❬translate_cst ty globals cst (find_local_env destr lenv ?) ?, lbl'❭ |
---|
| 967 | | St_op1 ty ty' op1 destr srcr lbl' ⇒ λstmt_typed. |
---|
| 968 | ❬translate_op1 globals ty' ty op1 |
---|
| 969 | (find_local_env destr lenv ?) |
---|
| 970 | (find_local_env srcr lenv ?) ??, lbl'❭ |
---|
| 971 | | St_op2 ty1 ty2 ty3 op2 destr srcr1 srcr2 lbl' ⇒ λstmt_typed. |
---|
| 972 | ❬translate_op2 globals … op2 |
---|
| 973 | (find_local_env destr lenv ?) |
---|
| 974 | (find_local_env_arg srcr1 lenv ?) |
---|
| 975 | (find_local_env_arg srcr2 lenv ?) ???, lbl'❭ |
---|
[1307] | 976 | (* XXX: should we be ignoring this? *) |
---|
[2286] | 977 | | St_load ignore addr destr lbl' ⇒ λstmt_typed. |
---|
| 978 | ❬translate_load globals |
---|
| 979 | (find_local_env_arg addr lenv ?) ? (find_local_env destr lenv ?), lbl'❭ |
---|
[1307] | 980 | (* XXX: should we be ignoring this? *) |
---|
[2286] | 981 | | St_store ignore addr srcr lbl' ⇒ λstmt_typed. |
---|
| 982 | ❬translate_store globals (find_local_env_arg addr lenv ?) ? |
---|
| 983 | (find_local_env_arg srcr lenv ?), lbl'❭ |
---|
| 984 | | St_call_id f args retr lbl' ⇒ λstmt_typed. |
---|
[2490] | 985 | ❬(CALL RTL ? (inl … f) (rtl_args args lenv ?) |
---|
| 986 | (match retr with |
---|
| 987 | [ Some retr ⇒ find_local_env retr lenv ? |
---|
| 988 | | None ⇒ [ ] |
---|
| 989 | ]) : bind_seq_block ???), lbl'❭ |
---|
[2286] | 990 | | St_call_ptr f args retr lbl' ⇒ λstmt_typed. |
---|
[2490] | 991 | let fs ≝ find_and_addr_arg f lenv ?? in |
---|
| 992 | ❬(CALL RTL ? (inr ?? fs) (rtl_args args lenv ?) |
---|
[2286] | 993 | (match retr with |
---|
| 994 | [ Some retr ⇒ |
---|
| 995 | find_local_env retr lenv ? |
---|
| 996 | | None ⇒ [ ] |
---|
| 997 | ]) : joint_step ??), lbl'❭ |
---|
| 998 | | St_cond r lbl_true lbl_false ⇒ λstmt_typed. |
---|
| 999 | ❬translate_cond globals (find_local_env r lenv ?) lbl_true, lbl_false❭ |
---|
| 1000 | | St_return ⇒ λ_. ❬inr … (RETURN ?),it❭ |
---|
| 1001 | ]. |
---|
| 1002 | [ >(lenv_typed_reg_typed_ok1 … lenv_typed stmt_typed) |
---|
| 1003 | @cst_size_ok |
---|
| 1004 | | @(lenv_typed_reg_typed_ok2 … lenv_typed stmt_typed) |
---|
| 1005 | | cases daemon (* needs more hypotheses *) |
---|
| 1006 | |4,5,6,7: cases stmt_typed #dstr_typed #srcr_typed |
---|
| 1007 | [1,2: @(lenv_typed_reg_typed_ok1 … lenv_typed) assumption |
---|
| 1008 | | @(lenv_typed_reg_typed_ok2 … lenv_typed srcr_typed) |
---|
| 1009 | | @(lenv_typed_reg_typed_ok2 … lenv_typed dstr_typed) |
---|
[1066] | 1010 | ] |
---|
[2286] | 1011 | |8,9,10,11,12,13: |
---|
| 1012 | cases stmt_typed * #srcrs1_prf #srcrs2_prf #dstr_prf |
---|
| 1013 | [1,2: @(lenv_typed_arg_typed_ok1 … lenv_typed) assumption |
---|
| 1014 | | @(lenv_typed_reg_typed_ok1 … lenv_typed) assumption |
---|
| 1015 | | @(lenv_typed_reg_typed_ok2 … lenv_typed srcrs2_prf) |
---|
| 1016 | | @(lenv_typed_reg_typed_ok2 … lenv_typed srcrs1_prf) |
---|
| 1017 | | @(lenv_typed_reg_typed_ok2 … lenv_typed dstr_prf) |
---|
[1066] | 1018 | ] |
---|
[2490] | 1019 | |*: cases daemon (* TODO *) |
---|
[1067] | 1020 | ] |
---|
| 1021 | qed. |
---|
[1064] | 1022 | |
---|
[1073] | 1023 | (* XXX: we use a "hack" here to circumvent the proof obligations required to |
---|
| 1024 | create res, an empty internal_function record. we insert into our graph |
---|
| 1025 | the start and end nodes to ensure that they are in, and place dummy |
---|
| 1026 | skip instructions at these nodes. *) |
---|
[2490] | 1027 | definition translate_internal : |
---|
| 1028 | ∀globals.internal_function → (* insert here more properties *) |
---|
| 1029 | joint_closed_internal_function RTL globals ≝ |
---|
[1293] | 1030 | λglobals: list ident. |
---|
| 1031 | λdef. |
---|
[2286] | 1032 | let runiverse' ≝ f_reggen def in |
---|
[1068] | 1033 | let luniverse' ≝ f_labgen def in |
---|
| 1034 | let stack_size' ≝ f_stacksize def in |
---|
| 1035 | let entry' ≝ f_entry def in |
---|
| 1036 | let exit' ≝ f_exit def in |
---|
[2286] | 1037 | let init ≝ init_graph_if RTL globals luniverse' runiverse' [ ] [ ] |
---|
| 1038 | [ ] stack_size' (pi1 … entry') (pi1 … exit') in |
---|
| 1039 | let 〈init',lenv〉 as pr_eq ≝ initialize_locals_params_ret globals |
---|
| 1040 | (f_locals def) (f_params def) (f_result def) init in |
---|
| 1041 | let vars ≝ (f_locals def) @ (f_params def) @ |
---|
| 1042 | match f_result def with [ Some x ⇒ [x] | _ ⇒ [ ] ] in |
---|
| 1043 | let f_trans ≝ λlbl,stmt,def. |
---|
| 1044 | let pr ≝ translate_statement … vars lenv ? stmt ? in |
---|
| 1045 | b_adds_graph … (rtl_fresh_reg …) (dpi1 … pr) lbl (dpi2 … pr) def in |
---|
| 1046 | foldi … f_trans (f_graph def) init'. (* TODO *) cases daemon |
---|
[1073] | 1047 | qed. |
---|
[1053] | 1048 | |
---|
[1239] | 1049 | (*CSC: here we are not doing any alignment on variables, like it is done in OCaml |
---|
| 1050 | because of CompCert heritage *) |
---|
[1293] | 1051 | definition rtlabs_to_rtl: RTLabs_program → rtl_program ≝ |
---|
[2490] | 1052 | λp. transform_program … p (λvarnames. transf_fundef … (translate_internal varnames)). |
---|