| 1 |
From Coq Require Import Permutation. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
From Coq Require Import NArith. |
| 5 |
From Coq Require Import ZArith. |
| 6 |
From Coq Require Import Morphisms. |
| 7 |
Require Import Crypto.Language.PreExtra. |
| 8 |
Require Import Crypto.Language.API. |
| 9 |
Require Import Crypto.Language.APINotations. |
| 10 |
Require Import Crypto.AbstractInterpretation.ZRange. |
| 11 |
Require Import Crypto.Util.ErrorT. |
| 12 |
Require Import Crypto.Assembly.Syntax. |
| 13 |
Require Import Crypto.Assembly.Symbolic. |
| 14 |
Require Import Crypto.Assembly.Equivalence. |
| 15 |
Require Import Crypto.CastLemmas. |
| 16 |
Require Import Crypto.Util.Option. |
| 17 |
Require Import Crypto.Util.Prod. |
| 18 |
Require Import Crypto.Util.Sigma. |
| 19 |
Require Import Crypto.Util.Notations. |
| 20 |
Require Import Crypto.Util.Sum. |
| 21 |
Require Import Crypto.Util.Bool.Reflect. |
| 22 |
Require Import Crypto.Util.Bool. |
| 23 |
Require Import Crypto.Util.NatUtil. |
| 24 |
Require Import Crypto.Util.ListUtil. |
| 25 |
Require Import Crypto.Util.ListUtil.FoldMap. |
| 26 |
Require Import Crypto.Util.ListUtil.Forall. |
| 27 |
Require Import Crypto.Util.ListUtil.Permutation. |
| 28 |
Require Import Crypto.Util.ListUtil.IndexOf. |
| 29 |
Require Import Crypto.Util.ListUtil.Filter. |
| 30 |
Require Import Crypto.Util.ListUtil.Split. |
| 31 |
Require Import Crypto.Util.OptionList. |
| 32 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 33 |
Require Import Crypto.Util.ZUtil.AddGetCarry. |
| 34 |
Require Import Crypto.Util.ZUtil.MulSplit. |
| 35 |
Require Import Crypto.Util.ZUtil.TruncatingShiftl. |
| 36 |
Require Import Crypto.Util.ZUtil.Land. |
| 37 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 38 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 39 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 40 |
Require Import Crypto.Util.ZUtil.Ones. |
| 41 |
Require Import Crypto.Util.ZUtil.LandLorShiftBounds. |
| 42 |
Require Import Crypto.Util.ZUtil.LandLorBounds. |
| 43 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 44 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 45 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 46 |
Require Import Crypto.Util.Tactics.RevertUntil. |
| 47 |
Require Import Crypto.Util.Tactics.HasBody. |
| 48 |
Require Import Crypto.Util.Tactics.Head. |
| 49 |
Require Import Crypto.Util.Tactics.PrintContext. |
| 50 |
Require Import Crypto.Util.Tactics.PrintGoal. |
| 51 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 52 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 53 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 54 |
Require Import Crypto.Util.Tactics.SetEvars. |
| 55 |
Require Import Crypto.Util.Tactics.SubstEvars. |
| 56 |
Require Import Crypto.Util.Tactics.ClearHead. |
| 57 |
Require Import Crypto.Assembly.EquivalenceProofs. |
| 58 |
Require Import Crypto.Assembly.WithBedrock.Semantics. |
| 59 |
Require Import Crypto.Assembly.WithBedrock.SymbolicProofs. |
| 60 |
Import Assembly.Symbolic. |
| 61 |
Import API.Compilers APINotations.Compilers AbstractInterpretation.ZRange.Compilers. |
| 62 |
Import ListNotations. |
| 63 |
Local Open Scope list_scope. |
| 64 |
|
| 65 |
(* TODO: move to global settings *) |
| 66 |
Local Set Keyed Unification. |
| 67 |
|
| 68 |
Import Map.Interface Map.Separation. (* for coercions *) |
| 69 |
Require Import bedrock2.Array. |
| 70 |
Require Import bedrock2.ZnWords. |
| 71 |
Require Import Rupicola.Lib.Tactics. (* for sepsimpl *) |
| 72 |
Import LittleEndianList. |
| 73 |
Import coqutil.Word.Interface. |
| 74 |
Definition cell64 wa (v : Z) : Semantics.mem_state -> Prop := |
| 75 |
Lift1Prop.ex1 (fun bs => sep (emp ( |
| 76 |
length bs = 8%nat /\ v = le_combine bs)) |
| 77 |
(eq (OfListWord.map.of_list_word_at wa bs))). |
| 78 |
|
| 79 |
Definition R_scalar_or_array {dereference_scalar:bool} |
| 80 |
(val : Z + list Z) (asm_val : Naive.word 64) |
| 81 |
:= match val with |
| 82 |
| inr array_vals => array cell64 (word.of_Z 8) asm_val array_vals |
| 83 |
| inl scalar_val => if dereference_scalar |
| 84 |
then cell64 asm_val scalar_val |
| 85 |
else emp (word.unsigned asm_val = scalar_val) |
| 86 |
end. |
| 87 |
Definition R_list_scalar_or_array_nolen {dereference_scalar:bool} |
| 88 |
(Z_vals : list (Z + list Z)) (asm_vals : list (Naive.word 64)) |
| 89 |
:= List.fold_right |
| 90 |
sep |
| 91 |
(emp True) |
| 92 |
(List.map |
| 93 |
(fun '(val, asm_val) => R_scalar_or_array (dereference_scalar:=dereference_scalar) val asm_val) |
| 94 |
(List.combine Z_vals asm_vals)). |
| 95 |
Definition R_list_scalar_or_array {dereference_scalar:bool} |
| 96 |
(Z_vals : list (Z + list Z)) (asm_vals : list (Naive.word 64)) |
| 97 |
:= sep (emp (List.length Z_vals = List.length asm_vals)) |
| 98 |
(R_list_scalar_or_array_nolen (dereference_scalar:=dereference_scalar) Z_vals asm_vals). |
| 99 |
|
| 100 |
Definition get_asm_reg (m : Semantics.reg_state) (reg_available : list REG) : list Z |
| 101 |
:= List.map (Semantics.get_reg m) reg_available. |
| 102 |
|
| 103 |
Definition R_runtime_input_mem |
| 104 |
{output_scalars_are_pointers:bool} |
| 105 |
(frame : Semantics.mem_state -> Prop) |
| 106 |
(output_types : type_spec) (runtime_inputs : list (Z + list Z)) |
| 107 |
(stack_size : nat) (stack_base : Naive.word 64) |
| 108 |
(asm_arguments_out asm_arguments_in : list (Naive.word 64)) |
| 109 |
(runtime_reg : list Z) |
| 110 |
(m : Semantics.mem_state) |
| 111 |
: Prop |
| 112 |
:= exists (stack_placeholder_values : list Z) (output_placeholder_values : list (Z + list Z)), |
| 113 |
Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) stack_placeholder_values |
| 114 |
/\ stack_size = List.length stack_placeholder_values |
| 115 |
/\ Forall2 val_or_list_val_matches_spec output_placeholder_values output_types |
| 116 |
/\ Forall (fun v => match v with |
| 117 |
| inl v => (0 <= v < 2^64)%Z |
| 118 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 119 |
end) output_placeholder_values |
| 120 |
/\ (* it must be the case that all the scalars in output_placeholder_values match what's in registers / the calling convention *) |
| 121 |
Forall2 |
| 122 |
(fun v1 v2 => match v1 with |
| 123 |
| inl v => if output_scalars_are_pointers |
| 124 |
then True |
| 125 |
else v = v2 |
| 126 |
| inr _ => True |
| 127 |
end) |
| 128 |
output_placeholder_values |
| 129 |
(firstn (length output_types) runtime_reg) |
| 130 |
/\ ((frame * |
| 131 |
R_list_scalar_or_array (dereference_scalar:=output_scalars_are_pointers) output_placeholder_values asm_arguments_out * |
| 132 |
R_list_scalar_or_array (dereference_scalar:=false) runtime_inputs asm_arguments_in * |
| 133 |
array cell64 (word.of_Z 8) stack_base stack_placeholder_values)%sep) |
| 134 |
m. |
| 135 |
|
| 136 |
Definition R_runtime_input |
| 137 |
{output_scalars_are_pointers:bool} |
| 138 |
(frame : Semantics.mem_state -> Prop) |
| 139 |
(output_types : type_spec) (runtime_inputs : list (Z + list Z)) |
| 140 |
(stack_size : nat) (stack_base : Naive.word 64) |
| 141 |
(asm_pointer_arguments_out asm_pointer_arguments_in : list (Naive.word 64)) |
| 142 |
(reg_available : list REG) (runtime_reg : list Z) |
| 143 |
(callee_saved_registers : list REG) (runtime_callee_saved_registers : list Z) |
| 144 |
(m : machine_state) |
| 145 |
: Prop |
| 146 |
:= exists (asm_arguments_out asm_arguments_in : list (Naive.word 64)), |
| 147 |
Forall (fun v => (0 <= v < 2^64)%Z) (Tuple.to_list _ m.(machine_reg_state)) |
| 148 |
/\ (Nat.min (List.length output_types + List.length runtime_inputs) (List.length reg_available) <= List.length runtime_reg)%nat |
| 149 |
/\ get_asm_reg m reg_available = runtime_reg |
| 150 |
/\ get_asm_reg m callee_saved_registers = runtime_callee_saved_registers |
| 151 |
/\ List.length asm_arguments_out = List.length output_types |
| 152 |
/\ List.map word.unsigned asm_arguments_out = List.firstn (List.length output_types) runtime_reg |
| 153 |
/\ List.map word.unsigned asm_arguments_in = List.firstn (List.length runtime_inputs) (List.skipn (List.length output_types) runtime_reg) |
| 154 |
/\ List.map fst (List.filter (fun '(_, v) => output_scalars_are_pointers || Option.is_Some v)%bool (List.combine asm_arguments_out output_types)) = asm_pointer_arguments_out |
| 155 |
/\ List.map fst (List.filter (fun '(_, v) => match v with inl _ => false | inr _ => true end)%bool (List.combine asm_arguments_in runtime_inputs)) = asm_pointer_arguments_in |
| 156 |
/\ (Semantics.get_reg m rsp - 8 * Z.of_nat stack_size)%Z = word.unsigned stack_base |
| 157 |
/\ (* it must be the case that all the scalars in the real input values match what's in registers / the calling convention *) |
| 158 |
Forall2 |
| 159 |
(fun v1 v2 => match v1 with |
| 160 |
| inl v => v = v2 |
| 161 |
| inr _ => True |
| 162 |
end) |
| 163 |
runtime_inputs |
| 164 |
(firstn (length runtime_inputs) (skipn (length output_types) runtime_reg)) |
| 165 |
/\ R_runtime_input_mem (output_scalars_are_pointers:=output_scalars_are_pointers) frame output_types runtime_inputs stack_size stack_base asm_arguments_out asm_arguments_in runtime_reg m. |
| 166 |
|
| 167 |
(* TODO : should we preserve inputs? *) |
| 168 |
Definition R_runtime_output_mem |
| 169 |
{output_scalars_are_pointers:bool} |
| 170 |
(frame : Semantics.mem_state -> Prop) |
| 171 |
(runtime_outputs : list (Z + list Z)) (input_types : type_spec) |
| 172 |
(stack_size : nat) (stack_base : Naive.word 64) |
| 173 |
(asm_arguments_out asm_arguments_in : list (Naive.word 64)) |
| 174 |
(m : Semantics.mem_state) |
| 175 |
: Prop |
| 176 |
:= exists (stack_placeholder_values : list Z) (input_placeholder_values : list (Z + list Z)), |
| 177 |
Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) stack_placeholder_values |
| 178 |
/\ stack_size = List.length stack_placeholder_values |
| 179 |
/\ Forall2 val_or_list_val_matches_spec input_placeholder_values input_types |
| 180 |
/\ Forall (fun v => match v with |
| 181 |
| inl v => (0 <= v < 2^64)%Z |
| 182 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 183 |
end) input_placeholder_values |
| 184 |
/\ ((frame * |
| 185 |
R_list_scalar_or_array (dereference_scalar:=output_scalars_are_pointers) runtime_outputs asm_arguments_out * |
| 186 |
R_list_scalar_or_array (dereference_scalar:=false) input_placeholder_values asm_arguments_in * |
| 187 |
array cell64 (word.of_Z 8) stack_base stack_placeholder_values)%sep) |
| 188 |
m. |
| 189 |
|
| 190 |
Definition R_runtime_output |
| 191 |
{output_scalars_are_pointers:bool} |
| 192 |
(frame : Semantics.mem_state -> Prop) |
| 193 |
(runtime_outputs : list (Z + list Z)) (input_types : type_spec) |
| 194 |
(stack_size : nat) (stack_base : Naive.word 64) |
| 195 |
(asm_pointer_arguments_out asm_pointer_arguments_in : list (Naive.word 64)) |
| 196 |
(callee_saved_registers : list REG) (runtime_callee_saved_registers : list Z) |
| 197 |
(m : machine_state) |
| 198 |
: Prop |
| 199 |
:= exists (asm_arguments_out asm_arguments_in : list (Naive.word 64)), |
| 200 |
Forall (fun v => (0 <= v < 2^64)%Z) (Tuple.to_list _ m.(machine_reg_state)) |
| 201 |
/\ get_asm_reg m callee_saved_registers = runtime_callee_saved_registers |
| 202 |
/\ List.map fst (List.filter (fun '(_, v) => output_scalars_are_pointers || match v with inl _ => false | inr _ => true end)%bool (List.combine asm_arguments_out runtime_outputs)) = asm_pointer_arguments_out |
| 203 |
/\ List.map fst (List.filter (fun '(_, v) => Option.is_Some v)%bool (List.combine asm_arguments_in input_types)) = asm_pointer_arguments_in |
| 204 |
/\ R_runtime_output_mem (output_scalars_are_pointers:=output_scalars_are_pointers) frame runtime_outputs input_types stack_size stack_base asm_arguments_out asm_arguments_in m. |
| 205 |
|
| 206 |
Definition word_args_to_Z_args |
| 207 |
: list (Naive.word 64 + list (Naive.word 64)) -> list (Z + list Z) |
| 208 |
:= List.map (fun v => match v with |
| 209 |
| inl v => inl (word.unsigned v) |
| 210 |
| inr vs => inr (List.map word.unsigned vs) |
| 211 |
end). |
| 212 |
|
| 213 |
Lemma word_args_to_Z_args_bounded args |
| 214 |
: Forall (fun v => match v with |
| 215 |
| inl v => (0 <= v < 2^64)%Z |
| 216 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 217 |
end) (word_args_to_Z_args args). |
| 218 |
Proof. |
| 219 |
cbv [word_args_to_Z_args]. |
| 220 |
repeat first [ progress intros |
| 221 |
| rewrite Forall_map_iff, Forall_forall |
| 222 |
| progress break_innermost_match |
| 223 |
| apply Word.Properties.word.unsigned_range ]. |
| 224 |
Qed. |
| 225 |
|
| 226 |
Lemma word_args_to_Z_args_length args |
| 227 |
: length (word_args_to_Z_args args) = length args. |
| 228 |
Proof. apply map_length. Qed. |
| 229 |
|
| 230 |
Definition init_symbolic_state_G (m : machine_state) |
| 231 |
: (symbol -> option Z) * dag -> (symbol -> option Z) * symbolic_state |
| 232 |
:= fun st |
| 233 |
=> let _ := init_symbolic_state_descr in |
| 234 |
let vals := Tuple.to_list _ (m.(machine_reg_state)) in |
| 235 |
let '(initial_reg_idxs, (G, d)) := dag_gensym_n_G vals st in |
| 236 |
(G, |
| 237 |
{| dag_state := d |
| 238 |
; symbolic_reg_state := Tuple.from_list_default None _ (List.map Some initial_reg_idxs) |
| 239 |
; symbolic_flag_state := Tuple.repeat None 6 |
| 240 |
; symbolic_mem_state := [] |
| 241 |
|}). |
| 242 |
|
| 243 |
Lemma init_symbolic_state_eq_G G d m |
| 244 |
: init_symbolic_state d = snd (init_symbolic_state_G m (G, d)). |
| 245 |
Proof. |
| 246 |
cbv [init_symbolic_state init_symbolic_state_G]. |
| 247 |
epose proof (dag_gensym_n_eq_G G d (Tuple.to_list _ m.(machine_reg_state))) as H. |
| 248 |
rewrite Tuple.length_to_list in H; rewrite H; clear H. |
| 249 |
eta_expand; cbn [fst snd]. |
| 250 |
reflexivity. |
| 251 |
Qed. |
| 252 |
|
| 253 |
Lemma init_symbolic_state_G_ok m G d G' ss |
| 254 |
(frame : Semantics.mem_state -> Prop) |
| 255 |
(Hd : gensym_dag_ok G d) |
| 256 |
(H : init_symbolic_state_G m (G, d) = (G', ss)) |
| 257 |
(d' := ss.(dag_state)) |
| 258 |
(Hframe : frame m) |
| 259 |
(Hbounds : Forall (fun v => (0 <= v < 2^64)%Z) (Tuple.to_list _ m.(machine_reg_state))) |
| 260 |
: R frame G' ss m |
| 261 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg ss.(symbolic_reg_state) (reg_index reg)) = true) |
| 262 |
/\ gensym_dag_ok G' d' |
| 263 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 264 |
Proof. |
| 265 |
cbv [init_symbolic_state_G] in *. |
| 266 |
eta_expand; cbn [fst snd] in *; inversion_prod; subst. |
| 267 |
epose proof (dag_gensym_n_G_ok) as H. (* COQBUG(https://github.com/coq/coq/issues/15927) *) |
| 268 |
specialize (H _ _ _ _ _ _ ltac:(eassumption) ltac:(repeat rewrite <- surjective_pairing; reflexivity) ltac:(eassumption)). |
| 269 |
destruct_head'_and; cbv [R]. |
| 270 |
repeat match goal with |- _ /\ _ => split end; try eassumption; try reflexivity. |
| 271 |
2: { cbv [Tuple.repeat R_flags Tuple.append Tuple.fieldwise Tuple.fieldwise' R_flag]; cbn [fst snd]; |
| 272 |
repeat apply conj; congruence. } |
| 273 |
2: { intros; cbn [List.map]; cbv [Symbolic.get_reg symbolic_reg_state]. |
| 274 |
unshelve erewrite <- Tuple.nth_default_to_list, Tuple.from_list_default_eq, Tuple.to_list_from_list. |
| 275 |
{ rewrite map_length. |
| 276 |
erewrite length_Forall2, Tuple.length_to_list |
| 277 |
by (eapply dag_gensym_n_G_ok; [ | eta_expand; reflexivity | ]; assumption). |
| 278 |
reflexivity. } |
| 279 |
erewrite map_nth_default with (x:=0%N); [ reflexivity | ]. |
| 280 |
erewrite length_Forall2, Tuple.length_to_list |
| 281 |
by (eapply dag_gensym_n_G_ok; [ | eta_expand; reflexivity | ]; assumption). |
| 282 |
clear; cbv [reg_index]; break_innermost_match; lia. } |
| 283 |
set (v := dag_gensym_n_G _ _) in *; clearbody v; destruct_head'_prod; cbn [fst snd] in *. |
| 284 |
eassert (H' : Tuple.to_list _ m.(machine_reg_state) = _). |
| 285 |
{ repeat match goal with H : _ |- _ => clear H end. |
| 286 |
cbv [Tuple.to_list Tuple.to_list']. |
| 287 |
set_evars; eta_expand; subst_evars; reflexivity. } |
| 288 |
generalize dependent (Tuple.to_list _ m.(machine_reg_state)); intros; subst. |
| 289 |
repeat match goal with H : context[?x :: _] |- _ => assert_fails is_var x; set x in * end. |
| 290 |
repeat match goal with H : Forall2 _ ?v (_ :: _) |- _ => is_var v; inversion H; clear H; subst end. |
| 291 |
repeat match goal with H : Forall2 _ ?v nil |- _ => is_var v; inversion H; clear H; subst end. |
| 292 |
repeat match goal with H : Forall _ (_ :: _) |- _ => inversion H; clear H; subst end. |
| 293 |
cbv [List.map Tuple.from_list_default Tuple.from_list_default']. |
| 294 |
cbv [R_regs R_reg Tuple.fieldwise Tuple.fieldwise' eval_idx_Z] in *; cbn [fst snd]. |
| 295 |
repeat apply conj; intros; inversion_option; subst; try assumption. |
| 296 |
all: change 64%Z with (Z.of_N 64). |
| 297 |
all: rewrite Z.land_ones_eq_of_bounded; [ reflexivity | ]. |
| 298 |
all: assumption. |
| 299 |
Qed. |
| 300 |
|
| 301 |
Lemma init_symbolic_state_ok m G d |
| 302 |
(frame : Semantics.mem_state -> Prop) |
| 303 |
(Hd : gensym_dag_ok G d) |
| 304 |
(ss := init_symbolic_state d) |
| 305 |
(d' := ss.(dag_state)) |
| 306 |
(Hbounds : Forall (fun v => (0 <= v < 2^64)%Z) (Tuple.to_list _ m.(machine_reg_state))) |
| 307 |
(Hframe : frame m) |
| 308 |
: exists G', |
| 309 |
R frame G' ss m |
| 310 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg ss.(symbolic_reg_state) (reg_index reg)) = true) |
| 311 |
/\ gensym_dag_ok G' d' |
| 312 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 313 |
Proof. |
| 314 |
subst ss d'; erewrite init_symbolic_state_eq_G. |
| 315 |
eexists; eapply init_symbolic_state_G_ok; try eassumption. |
| 316 |
eta_expand; reflexivity. |
| 317 |
Qed. |
| 318 |
|
| 319 |
Lemma get_reg_of_R_regs G d r mr reg |
| 320 |
(Hwide : let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) |
| 321 |
(Hreg : R_regs G d r mr) |
| 322 |
: forall idx', Symbolic.get_reg r (reg_index reg) = Some idx' -> eval_idx_Z G d idx' (Semantics.get_reg mr reg). |
| 323 |
Proof. |
| 324 |
assert (reg_offset reg = 0%N) by now destruct reg. |
| 325 |
assert (N.to_nat (reg_index reg) < List.length (Tuple.to_list _ r)) |
| 326 |
by now rewrite Tuple.length_to_list; destruct reg; cbv [reg_index]; lia. |
| 327 |
cbv [Symbolic.get_reg Semantics.get_reg R_regs] in *. |
| 328 |
rewrite Tuple.fieldwise_to_list_iff in Hreg. |
| 329 |
erewrite @Forall2_forall_iff in Hreg by now rewrite !Tuple.length_to_list. |
| 330 |
specialize (Hreg (N.to_nat (reg_index reg)) ltac:(assumption)); rewrite [email protected]_default_to_list in *. |
| 331 |
cbv [index_and_shift_and_bitcount_of_reg] in *. |
| 332 |
generalize dependent (reg_size reg); intros; subst. |
| 333 |
generalize dependent (reg_offset reg); intros; subst. |
| 334 |
change (Z.of_N 0) with 0%Z. |
| 335 |
change (Z.of_N 64) with 64%Z. |
| 336 |
rewrite Z.shiftr_0_r. |
| 337 |
do 2 match goal with |
| 338 |
| [ H : context[Tuple.nth_default ?d ?i ?l], H' : context[Tuple.nth_default ?d' ?i' ?l] |- _ ] |
| 339 |
=> is_evar d; unify d d'; unify i i' |
| 340 |
| [ H : context[Tuple.nth_default ?d ?i ?l] |- context[Tuple.nth_default ?d' ?i' ?l] ] |
| 341 |
=> is_evar d; unify d d'; unify i i' |
| 342 |
end. |
| 343 |
cbv [R_reg] in Hreg. |
| 344 |
destruct Hreg as [Hreg Hreg']. |
| 345 |
rewrite <- Hreg'. |
| 346 |
now apply Hreg. |
| 347 |
Qed. |
| 348 |
|
| 349 |
Lemma Forall2_get_reg_of_R_regs G d r mr regs |
| 350 |
(Hwide : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) regs) |
| 351 |
(Hreg : R_regs G d r mr) |
| 352 |
: Forall2 (fun idx v => forall idx', idx = Some idx' -> eval_idx_Z G d idx' v) |
| 353 |
(List.map (Symbolic.get_reg r) (List.map reg_index regs)) |
| 354 |
(List.map (Semantics.get_reg mr) regs). |
| 355 |
Proof. |
| 356 |
induction regs as [|reg regs IH]; cbn [List.map] in *; |
| 357 |
inversion Hwide; clear Hwide; subst; |
| 358 |
constructor; auto; clear dependent regs; []. |
| 359 |
now apply get_reg_of_R_regs. |
| 360 |
Qed. |
| 361 |
|
| 362 |
Lemma get_reg_of_R frame G s m reg |
| 363 |
(Hwide : let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) |
| 364 |
(Hreg : R frame G s m) |
| 365 |
: forall idx', Symbolic.get_reg s (reg_index reg) = Some idx' -> eval_idx_Z G s idx' (Semantics.get_reg m reg). |
| 366 |
Proof. |
| 367 |
apply get_reg_of_R_regs; cbv [R] in *; destruct s; tauto. |
| 368 |
Qed. |
| 369 |
|
| 370 |
Lemma Forall2_get_reg_of_R frame G s m regs |
| 371 |
(Hwide : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) regs) |
| 372 |
(Hreg : R frame G s m) |
| 373 |
: Forall2 (fun idx v => forall idx', idx = Some idx' -> eval_idx_Z G s idx' v) |
| 374 |
(List.map (Symbolic.get_reg s) (List.map reg_index regs)) |
| 375 |
(List.map (Semantics.get_reg m) regs). |
| 376 |
Proof. |
| 377 |
apply Forall2_get_reg_of_R_regs; cbv [R] in *; destruct s; tauto. |
| 378 |
Qed. |
| 379 |
|
| 380 |
Lemma get_reg_bounded mr reg |
| 381 |
: (0 <= Semantics.get_reg mr reg < 2 ^ 64)%Z. |
| 382 |
Proof. |
| 383 |
assert ((reg_size reg <= 64)%N) |
| 384 |
by now clear; destruct reg; cbv [reg_index reg_size]; lia. |
| 385 |
cbv [Semantics.get_reg index_and_shift_and_bitcount_of_reg] in *. |
| 386 |
rewrite Z.land_nonneg, Z.shiftr_nonneg. |
| 387 |
split; [ right; apply Z.ones_nonneg; lia | ]. |
| 388 |
eapply Z.le_lt_trans; |
| 389 |
[ rewrite Z.land_comm; apply Z.land_le; intros; apply Z.ones_nonneg |
| 390 |
| rewrite Z.ones_equiv, Z.lt_pred_le; Z.peel_le ]; |
| 391 |
lia. |
| 392 |
Qed. |
| 393 |
|
| 394 |
Lemma get_reg_bounded_Forall mr regs |
| 395 |
: Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) |
| 396 |
(List.map (Semantics.get_reg mr) regs). |
| 397 |
Proof. |
| 398 |
rewrite Forall_map, Forall_forall; intros; apply get_reg_bounded. |
| 399 |
Qed. |
| 400 |
|
| 401 |
Lemma load_eval_R_mem_eval G d idx idx' mem_st m' frame |
| 402 |
(Hmem : R_mem frame G d mem_st m') |
| 403 |
(Hload : load idx mem_st = Some idx') |
| 404 |
: exists val, |
| 405 |
eval_idx_Z G d idx' val. |
| 406 |
Proof. |
| 407 |
cbv [load option_map] in *. |
| 408 |
break_innermost_match_hyps; inversion_option; subst; destruct_head'_prod. |
| 409 |
revert dependent m'; induction mem_st as [|m mem_st IH]; cbn [R_mem find] in *; intros. |
| 410 |
all: repeat first [ progress inversion_option |
| 411 |
| progress inversion_pair |
| 412 |
| progress cbn [fst snd] in * |
| 413 |
| progress reflect_hyps |
| 414 |
| progress subst |
| 415 |
| progress specialize_by_assumption |
| 416 |
| progress break_innermost_match_hyps ]. |
| 417 |
all: repeat first [ progress cbv [sep R_cell64 Lift1Prop.ex1 emp eval_idx_Z] in * |
| 418 |
| progress destruct_head'_ex |
| 419 |
| progress destruct_head'_and |
| 420 |
| progress subst |
| 421 |
| now eauto ]. |
| 422 |
Qed. |
| 423 |
|
| 424 |
Lemma load_eval_R_mem_eval_Forall2 G d idxs l n z mem_st m' frame l1 |
| 425 |
(Hmem : R_mem frame G d mem_st m') |
| 426 |
(Hl : Forall2 (eval_idx_Z G d) l |
| 427 |
(List.map (fun i => Z.land (z + 8 * Z.of_nat i) (Z.ones 64)) |
| 428 |
(seq 0 n))) |
| 429 |
(Hmem_all : Option.List.lift (List.map (fun a => load a mem_st) idxs) = Some l1) |
| 430 |
: exists vals, |
| 431 |
Forall2 (eval_idx_Z G d) l1 vals. |
| 432 |
Proof. |
| 433 |
revert dependent l1. |
| 434 |
induction idxs as [|idx idxs IH], |
| 435 |
l1 as [|x xs]; |
| 436 |
try specialize (IH xs); intros. |
| 437 |
all: repeat first [ progress cbn [List.map fold_right] in * |
| 438 |
| progress cbv [Crypto.Util.Option.bind Option.List.lift] in * |
| 439 |
| progress inversion_option |
| 440 |
| progress inversion_list |
| 441 |
| progress destruct_head'_ex |
| 442 |
| progress destruct_head'_and |
| 443 |
| progress specialize_by reflexivity |
| 444 |
| progress subst |
| 445 |
| progress break_match_hyps |
| 446 |
| match goal with |
| 447 |
| [ H : load _ _ = Some _ |- _ ] => eapply load_eval_R_mem_eval in H; [ | eassumption ] |
| 448 |
end |
| 449 |
| now eexists nil; eauto |
| 450 |
| now eexists (_ :: _); eauto ]. |
| 451 |
Qed. |
| 452 |
|
| 453 |
Definition R_regs_preserved_v rn (m : Semantics.reg_state) |
| 454 |
:= Z.land (Tuple.nth_default 0%Z rn m) (Z.ones 64). |
| 455 |
|
| 456 |
Definition R_regs_preserved G d G' d' (m : Semantics.reg_state) rs rs' |
| 457 |
:= forall rn idx, Symbolic.get_reg rs' rn = Some idx -> exists idx', Symbolic.get_reg rs rn = Some idx' /\ let v := R_regs_preserved_v (N.to_nat rn) m in eval_idx_Z G d idx' v -> eval_idx_Z G' d' idx v. |
| 458 |
|
| 459 |
Global Instance R_regs_preserved_Reflexive G d m : Reflexive (R_regs_preserved G d G d m) | 100. |
| 460 |
Proof. intro; cbv [R_regs_preserved]; eauto. Qed. |
| 461 |
|
| 462 |
Lemma R_regs_subsumed_get_reg_same_eval G d G' d' rs rs' rm |
| 463 |
(H : R_regs G d rs rm) |
| 464 |
(H_impl : R_regs_preserved G d G' d' rm rs rs') |
| 465 |
: R_regs G' d' rs' rm. |
| 466 |
Proof. |
| 467 |
cbv [R_regs Symbolic.get_reg R_regs_preserved R_regs_preserved_v] in *. |
| 468 |
rewrite @Tuple.fieldwise_to_list_iff, @Forall2_forall_iff_nth_error in *. |
| 469 |
intro i; specialize (H i); specialize (H_impl (N.of_nat i)). |
| 470 |
rewrite <- [email protected]_default_to_list in *. |
| 471 |
cbv [nth_default option_eq] in *. |
| 472 |
repeat first [ progress destruct_head'_and |
| 473 |
| progress destruct_head'_ex |
| 474 |
| progress inversion_option |
| 475 |
| rewrite @Tuple.length_to_list in * |
| 476 |
| progress cbv [R_reg eval_idx_Z] in * |
| 477 |
| progress break_innermost_match |
| 478 |
| progress break_innermost_match_hyps |
| 479 |
| rewrite !Nat2N.id in * |
| 480 |
| now auto |
| 481 |
| progress intros |
| 482 |
| progress subst |
| 483 |
| match goal with |
| 484 |
| [ H : ?x = Some ?a, H' : ?x = Some ?b |- _ ] => rewrite H in H' |
| 485 |
| [ H : nth_error _ _ = None |- _ ] => apply nth_error_error_length in H |
| 486 |
| [ H : ?i >= ?n, H' : context[nth_error (Tuple.to_list ?n _) ?i] |- _ ] |
| 487 |
=> rewrite nth_error_length_error in H' by now rewrite Tuple.length_to_list; lia |
| 488 |
| [ H : forall x, Some _ = Some x -> _ |- _ ] |
| 489 |
=> specialize (H _ eq_refl) |
| 490 |
| [ H : ?x = ?y, H' : context[?y] |- _ ] => is_var x; rewrite <- H in H' |
| 491 |
end |
| 492 |
| apply conj; auto; [] ]. |
| 493 |
Qed. |
| 494 |
|
| 495 |
Lemma R_regs_preserved_set_reg G d G' d' rs rs' ri rm v |
| 496 |
(H : R_regs_preserved G d G' d' rm rs rs') |
| 497 |
(H_same : (ri < N.of_nat (List.length widest_registers))%N -> exists idx, Symbolic.get_reg rs ri = Some idx /\ let v' := R_regs_preserved_v (N.to_nat ri) rm in eval_idx_Z G d idx v' -> eval_idx_Z G' d' v v') |
| 498 |
: R_regs_preserved G d G' d' rm rs (Symbolic.set_reg rs' ri v). |
| 499 |
Proof. |
| 500 |
cbv [R_regs_preserved] in *. |
| 501 |
intros rn idx; specialize (H rn). |
| 502 |
rewrite get_reg_set_reg_full; intro. |
| 503 |
vm_compute (length widest_registers) in *. |
| 504 |
repeat first [ progress break_innermost_match_hyps |
| 505 |
| progress inversion_option |
| 506 |
| progress subst |
| 507 |
| progress destruct_head'_and |
| 508 |
| progress destruct_head'_or |
| 509 |
| progress destruct_head'_ex |
| 510 |
| progress specialize_by_assumption |
| 511 |
| progress specialize_by lia |
| 512 |
| rewrite @Bool.andb_true_iff in * |
| 513 |
| rewrite @Bool.andb_false_iff in * |
| 514 |
| solve [ eauto ] |
| 515 |
| progress reflect_hyps |
| 516 |
| match goal with |
| 517 |
| [ H : forall v, ?k = Some v -> _, H' : ?k = Some _ |- _ ] |
| 518 |
=> specialize (H _ H') |
| 519 |
end ]. |
| 520 |
Qed. |
| 521 |
|
| 522 |
Lemma R_regs_preserved_fold_left_set_reg_index {T1 T2} G d G' d' rs rs' rm (r_idxs : list (_ * (_ * T1 + _ * T2))) |
| 523 |
(H : R_regs_preserved G d G' d' rm rs rs') |
| 524 |
(H_same : Forall (fun '(r, v) => let v := match v with inl (v, _) => v | inr (v, _) => v end in exists idx, Symbolic.get_reg rs (reg_index r) = Some idx /\ let v' := R_regs_preserved_v (N.to_nat (reg_index r)) rm in eval_idx_Z G d idx v' -> eval_idx_Z G' d' v v') r_idxs) |
| 525 |
: R_regs_preserved |
| 526 |
G d G' d' rm |
| 527 |
rs |
| 528 |
(fold_left (fun rst '(r, idx') |
| 529 |
=> Symbolic.set_reg rst (reg_index r) |
| 530 |
match idx' with |
| 531 |
| inl (idx', _) => idx' |
| 532 |
| inr (base_idx', idxs') => base_idx' |
| 533 |
end) |
| 534 |
r_idxs rs'). |
| 535 |
Proof. |
| 536 |
revert dependent rs'; induction r_idxs as [|[r v] r_idxs IH]; cbn [fold_left]; |
| 537 |
inversion_one_head Forall; auto; intros; []. |
| 538 |
apply IH; clear IH; auto; []. |
| 539 |
apply R_regs_preserved_set_reg; auto. |
| 540 |
Qed. |
| 541 |
|
| 542 |
Lemma Semantics_get_reg_eq_nth_default_of_R_regs G d ss ms r |
| 543 |
(Hsz : reg_size r = 64%N) |
| 544 |
(HR : R_regs G d ss ms) |
| 545 |
: Semantics.get_reg ms r = Tuple.nth_default 0%Z (N.to_nat (reg_index r)) (ms : Semantics.reg_state). |
| 546 |
Proof. |
| 547 |
assert (Hro : reg_offset r = 0%N) by now revert Hsz; clear; cbv; destruct r; lia. |
| 548 |
cbv [R_regs R_reg] in HR. |
| 549 |
rewrite Tuple.fieldwise_to_list_iff, Forall2_forall_iff_nth_error in HR. |
| 550 |
specialize (HR (N.to_nat (reg_index r))). |
| 551 |
cbv [Semantics.get_reg index_and_shift_and_bitcount_of_reg]. |
| 552 |
rewrite Hro, Hsz; change (Z.of_N 0) with 0%Z; change (Z.of_N 64) with 64%Z. |
| 553 |
rewrite Z.shiftr_0_r, <- Tuple.nth_default_to_list; cbv [nth_default option_eq] in *. |
| 554 |
break_innermost_match_hyps; break_innermost_match; inversion_option; destruct_head'_and; try congruence; try tauto. |
| 555 |
Qed. |
| 556 |
|
| 557 |
Lemma Semantics_get_reg_eq_nth_default_of_R frame G ss ms r |
| 558 |
(Hsz : reg_size r = 64%N) |
| 559 |
(HR : R frame G ss ms) |
| 560 |
: Semantics.get_reg ms r = Tuple.nth_default 0%Z (N.to_nat (reg_index r)) (ms : Semantics.reg_state). |
| 561 |
Proof. |
| 562 |
destruct ss, ms; eapply Semantics_get_reg_eq_nth_default_of_R_regs; try eassumption; apply HR. |
| 563 |
Qed. |
| 564 |
|
| 565 |
Lemma Forall2_R_regs_preserved_same_helper G d reg_available idxs m rs |
| 566 |
(Hreg_wide_enough |
| 567 |
: Forall |
| 568 |
(fun reg : REG => |
| 569 |
let |
| 570 |
'(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in |
| 571 |
sz = 64%N) reg_available) |
| 572 |
(H : Forall2 (fun (idx' : idx * option idx + idx * list idx) v |
| 573 |
=> match idx' with |
| 574 |
| inl (idx', _) => eval_idx_Z G d idx' (Z.land v (Z.ones 64)) |
| 575 |
| inr (base', _) => eval_idx_Z G d base' (Z.land v (Z.ones 64)) |
| 576 |
end) |
| 577 |
idxs (firstn (List.length idxs) (get_asm_reg m reg_available))) |
| 578 |
(HR : R_regs G d rs m) |
| 579 |
(Hex : forall n r, (n < List.length idxs)%nat -> nth_error reg_available n = Some r -> exists idx, Symbolic.get_reg rs (reg_index r) = Some idx) |
| 580 |
: Forall (fun '(r, v) |
| 581 |
=> let v := match v with inl (v, _) => v | inr (v, _) => v end in |
| 582 |
exists idx, Symbolic.get_reg rs (reg_index r) = Some idx |
| 583 |
/\ let v' := R_regs_preserved_v (N.to_nat (reg_index r)) m in eval_idx_Z G d idx v' -> eval_idx_Z G d v v') |
| 584 |
(List.combine reg_available idxs). |
| 585 |
Proof. |
| 586 |
cbv [get_asm_reg] in *. |
| 587 |
rewrite firstn_map, Forall2_map_r_iff in H. |
| 588 |
apply Forall2_flip in H. |
| 589 |
rewrite Forall2_forall_iff_nth_error in H. |
| 590 |
rewrite @Forall_forall in *. |
| 591 |
intros [r ?] H'; apply In_nth_error_value in H'. |
| 592 |
specialize (Hreg_wide_enough r). |
| 593 |
specialize (fun v pf => Hreg_wide_enough (nth_error_In _ v pf)). |
| 594 |
destruct H' as [n H']. |
| 595 |
specialize (H n). |
| 596 |
rewrite ListUtil.nth_error_firstn in H. |
| 597 |
rewrite nth_error_combine in H'. |
| 598 |
cbv [option_eq] in H. |
| 599 |
cbv [R_regs_preserved_v]. |
| 600 |
repeat first [ progress break_innermost_match_hyps |
| 601 |
| progress inversion_option |
| 602 |
| progress subst |
| 603 |
| progress inversion_pair |
| 604 |
| progress cbv [index_and_shift_and_bitcount_of_reg] in * |
| 605 |
| solve [ eauto ] |
| 606 |
| match goal with |
| 607 |
| [ H : (forall reg, Option.is_Some (Symbolic.get_reg ?s (reg_index reg)) = true) |
| 608 |
|- context[Symbolic.get_reg ?s (reg_index ?ri)] ] |
| 609 |
=> generalize (H ri); cbv [Option.is_Some]; break_innermost_match; |
| 610 |
try congruence |
| 611 |
| [ H : forall v, nth_error ?ls v = Some _ -> _ |- _ ] |
| 612 |
=> specialize (H _ ltac:(eassumption)) |
| 613 |
end ]. |
| 614 |
all: specialize (Hex _ _ ltac:(eassumption) ltac:(eassumption)). |
| 615 |
all: destruct_head'_ex. |
| 616 |
all: erewrite <- Semantics_get_reg_eq_nth_default_of_R_regs by eassumption. |
| 617 |
all: eauto. |
| 618 |
Qed. |
| 619 |
|
| 620 |
Lemma R_list_scalar_or_array_cons_iff dereference_scalar i1 i2 w1 w2 |
| 621 |
: Lift1Prop.iff1 |
| 622 |
(@R_list_scalar_or_array dereference_scalar (i1 :: i2) (w1 :: w2)) |
| 623 |
(@R_scalar_or_array dereference_scalar i1 w1 * @R_list_scalar_or_array dereference_scalar i2 w2)%sep. |
| 624 |
Proof. |
| 625 |
cbv [R_list_scalar_or_array R_list_scalar_or_array_nolen]; cbn [List.length List.combine List.map fold_right]. |
| 626 |
SeparationLogic.cancel; cbn [seps]; split; cbv [emp]; intros; sepsimpl; inversion_nat_eq; subst; congruence. |
| 627 |
Qed. |
| 628 |
|
| 629 |
Lemma R_list_scalar_or_array_nolen_app_iff dereference_scalar i1 i2 w1 w2 |
| 630 |
(H : List.length i1 = List.length w1) |
| 631 |
: Lift1Prop.iff1 |
| 632 |
(@R_list_scalar_or_array_nolen dereference_scalar (i1 ++ i2) (w1 ++ w2)) |
| 633 |
(@R_list_scalar_or_array_nolen dereference_scalar i1 w1 * @R_list_scalar_or_array_nolen dereference_scalar i2 w2)%sep. |
| 634 |
Proof. |
| 635 |
cbv [R_list_scalar_or_array_nolen]. |
| 636 |
rewrite !combine_app_samelength, map_app, fold_right_app by assumption. |
| 637 |
generalize (List.combine i1 w1). |
| 638 |
intro l; induction l as [|?? IH]; cbn [List.map fold_right]; [ | rewrite IH ]; |
| 639 |
SeparationLogic.cancel; cbn [seps]. |
| 640 |
Qed. |
| 641 |
|
| 642 |
Lemma R_list_scalar_or_array_app_iff dereference_scalar i1 i2 w1 w2 |
| 643 |
(H : List.length i1 = List.length w1) |
| 644 |
: Lift1Prop.iff1 |
| 645 |
(@R_list_scalar_or_array dereference_scalar (i1 ++ i2) (w1 ++ w2)) |
| 646 |
(@R_list_scalar_or_array dereference_scalar i1 w1 * @R_list_scalar_or_array dereference_scalar i2 w2)%sep. |
| 647 |
Proof. |
| 648 |
cbv [R_list_scalar_or_array]. |
| 649 |
rewrite R_list_scalar_or_array_nolen_app_iff by assumption. |
| 650 |
rewrite !app_length. |
| 651 |
SeparationLogic.cancel; cbn [seps]. |
| 652 |
rewrite SeparationLogic.sep_emp_emp. |
| 653 |
split; cbv [emp]; intros; sepsimpl; subst; try congruence; lia. |
| 654 |
Qed. |
| 655 |
|
| 656 |
Lemma R_mem_frame_cps_id {P : Prop} (HP : P) frame G d s |
| 657 |
: Lift1Prop.iff1 (R_mem frame G d s) (frame * (R_mem (emp P) G d s))%sep. |
| 658 |
Proof. |
| 659 |
induction s as [|[? ?] s IH]; cbn [R_mem]; [ | rewrite IH ]; |
| 660 |
SeparationLogic.cancel. |
| 661 |
cbn [seps]; apply SeparationLogic.Proper_emp_iff; tauto. |
| 662 |
Qed. |
| 663 |
|
| 664 |
Lemma R_mem_nil_iff G d P |
| 665 |
: Lift1Prop.iff1 (R_mem (emp P) G d nil) (emp P). |
| 666 |
Proof. reflexivity. Qed. |
| 667 |
|
| 668 |
Lemma R_mem_cons_iff G d v s P |
| 669 |
: Lift1Prop.iff1 (R_mem (emp P) G d (v :: s)) |
| 670 |
(R_cell64 G d (fst v) (snd v) * R_mem (emp P) G d s)%sep. |
| 671 |
Proof. destruct v; reflexivity. Qed. |
| 672 |
|
| 673 |
Lemma R_mem_app_iff G d s1 s2 P |
| 674 |
: Lift1Prop.iff1 (R_mem (emp P) G d (s1 ++ s2)) |
| 675 |
(R_mem (emp P) G d s1 * R_mem (emp P) G d s2)%sep. |
| 676 |
Proof. |
| 677 |
induction s1 as [|? ? IH]; cbn [List.app]. |
| 678 |
all: rewrite ?R_mem_nil_iff, ?R_mem_cons_iff, ?IH, ?(R_mem_frame_cps_id I (emp P)). |
| 679 |
{ intro; rewrite ?SeparationLogic.sep_emp_l; tauto. } |
| 680 |
{ SeparationLogic.ecancel. } |
| 681 |
Qed. |
| 682 |
|
| 683 |
Lemma R_mem_rev_iff G d s P |
| 684 |
: Lift1Prop.iff1 (R_mem (emp P) G d (List.rev s)) |
| 685 |
(R_mem (emp P) G d s). |
| 686 |
Proof. |
| 687 |
induction s as [|? ? IH]; cbn [List.rev]. |
| 688 |
all: rewrite ?R_mem_app_iff, ?R_mem_cons_iff, ?R_mem_nil_iff, ?IH, ?(R_mem_frame_cps_id I (emp P)). |
| 689 |
{ SeparationLogic.cancel. } |
| 690 |
{ assert (H : @Lift1Prop.iff1 Semantics.mem_state (emp P) (emp (P /\ P))) |
| 691 |
by (apply SeparationLogic.Proper_emp_iff; tauto). |
| 692 |
rewrite H at 3. |
| 693 |
SeparationLogic.cancel. |
| 694 |
cbn [seps]; intro. |
| 695 |
rewrite ?SeparationLogic.sep_emp_l; cbv [emp]; tauto. } |
| 696 |
Qed. |
| 697 |
|
| 698 |
Lemma R_cell64_ex_cell64_iff G d ia iv |
| 699 |
: Lift1Prop.iff1 (R_cell64 G d ia iv) |
| 700 |
(Lift1Prop.ex1 |
| 701 |
(fun a |
| 702 |
=> Lift1Prop.ex1 |
| 703 |
(fun v |
| 704 |
=> emp (eval_idx_Z G d ia (word.unsigned a) /\ eval_idx_Z G d iv v /\ 0 <= v < 2^64)%Z * cell64 a v)%sep)). |
| 705 |
Proof. |
| 706 |
apply Lift1Prop.Proper_ex1_iff1; intro. |
| 707 |
split; intros. |
| 708 |
all: repeat |
| 709 |
repeat |
| 710 |
first [ progress subst |
| 711 |
| reflexivity |
| 712 |
| eassumption |
| 713 |
| progress sepsimpl |
| 714 |
| progress cbv [cell64] in * |
| 715 |
| match goal with |
| 716 |
| [ H : le_combine _ = le_combine _ |- _ ] => apply le_combine_inj in H; [ | congruence ] |
| 717 |
| [ |- Lift1Prop.ex1 _ _ ] => eexists |
| 718 |
end ]. |
| 719 |
all: sepsimpl; subst; [ > apply le_combine_bound | eapply Z.lt_le_trans; [ apply le_combine_bound | ] ]. |
| 720 |
rewrite (ltac:(eassumption) : List.length _ = _); reflexivity. |
| 721 |
Qed. |
| 722 |
|
| 723 |
Lemma bounded_of_cell64 wa v m |
| 724 |
(H : cell64 wa v m) |
| 725 |
: (0 <= v < 2^64)%Z. |
| 726 |
Proof. |
| 727 |
cbv [cell64] in H. |
| 728 |
sepsimpl; subst; [ apply le_combine_bound | eapply Z.lt_le_trans; [ apply le_combine_bound | ] ]. |
| 729 |
rewrite H; reflexivity. |
| 730 |
Qed. |
| 731 |
|
| 732 |
Lemma bounded_of_array_cell64 v |
| 733 |
: forall wa base m |
| 734 |
(H : array cell64 base wa v m), |
| 735 |
Forall (fun v => 0 <= v < 2^64)%Z v. |
| 736 |
Proof. |
| 737 |
induction v as [|v vs IH]; cbn [array]; constructor; cbv [sep] in *; sepsimpl. |
| 738 |
all: try now eapply bounded_of_cell64; eassumption. |
| 739 |
eauto. |
| 740 |
Qed. |
| 741 |
|
| 742 |
(* TODO: move? *) |
| 743 |
Lemma sep_emp_holds_r (p : Semantics.mem_state -> Prop) (P : Prop) |
| 744 |
(H : forall m, p m -> P) |
| 745 |
: Lift1Prop.iff1 (sep p (emp P)) p. |
| 746 |
Proof. |
| 747 |
split; intro; sepsimpl; eauto. |
| 748 |
Qed. |
| 749 |
Lemma sep_emp_holds_l (p : Semantics.mem_state -> Prop) (P : Prop) |
| 750 |
(H : forall m, p m -> P) |
| 751 |
: Lift1Prop.iff1 (sep (emp P) p) p. |
| 752 |
Proof. |
| 753 |
split; intro; sepsimpl; eauto. |
| 754 |
Qed. |
| 755 |
|
| 756 |
Lemma R_mem_combine_ex_array_iff frame G d n addrs val_idxs base_value base_word_value init |
| 757 |
(Haddrs : Forall2 (eval_idx_Z G d) addrs (List.map (fun i => Z.land (base_value + 8 * Z.of_nat i) (Z.ones 64)) (seq init n))) |
| 758 |
(*(Hn : List.length val_idxs = n)*) |
| 759 |
(Hbase : base_value = word.unsigned base_word_value) |
| 760 |
: Lift1Prop.iff1 |
| 761 |
(R_mem frame G d (List.combine addrs val_idxs)) |
| 762 |
(Lift1Prop.ex1 (fun vals |
| 763 |
=> emp (Forall2 (eval_idx_Z G d) (firstn n val_idxs) vals /\ Forall (fun v => 0 <= v < 2^64)%Z vals) * frame * array cell64 (word.of_Z 8) (word.add base_word_value (word.of_Z (8 * Z.of_nat init))) vals))%sep. |
| 764 |
Proof. |
| 765 |
subst. |
| 766 |
revert val_idxs n init Haddrs. |
| 767 |
induction addrs as [|addr addrs IH], val_idxs as [|val_idx val_idxs], n as [|n]; |
| 768 |
intro init; cbn [List.combine List.length seq List.map R_mem array]; intros; |
| 769 |
try (specialize (IH val_idxs n (S init)); rewrite IH; clear IH); |
| 770 |
rewrite ?firstn_O, ?firstn_nil, ?firstn_cons; |
| 771 |
try now inversion_one_head Forall2. |
| 772 |
all: lazymatch goal with |
| 773 |
| [ |- Lift1Prop.iff1 ?frame (Lift1Prop.ex1 _) ] |
| 774 |
=> (tryif is_var frame |
| 775 |
then let x := fresh in intro x; split; revert x; refine (_ : Lift1Prop.impl1 _ _) |
| 776 |
else idtac) |
| 777 |
end. |
| 778 |
all: try (apply Lift1Prop.impl1_ex1_l; intro). |
| 779 |
all: try solve [ apply Lift1Prop.impl1_ex1_r with (x:=nil); cbn [array]; |
| 780 |
unshelve erewrite (_ : (Forall2 _ _ _ /\ _) <-> True); [ intuition | ]; |
| 781 |
SeparationLogic.cancel; cbn [seps]; reflexivity ]. |
| 782 |
all: rewrite ?SeparationLogic.sep_assoc, ?SeparationLogic.impl1_l_sep_emp; intros. |
| 783 |
all: rewrite ?Forall2_nil_l_iff, ?Forall2_nil_r_iff in *; destruct_head'_and; subst; cbn [array]. |
| 784 |
all: try solve [ hnf; intros; sepsimpl; trivial ]. |
| 785 |
{ rewrite !@Forall2_cons_cons_iff in *; destruct_head'_and. |
| 786 |
rewrite R_cell64_ex_cell64_iff. |
| 787 |
rewrite SeparationLogic.sep_ex1_r. |
| 788 |
intro x; split; revert x; refine (_ : Lift1Prop.impl1 _ _). |
| 789 |
all: repeat first [ progress intros |
| 790 |
| progress destruct_head'_and |
| 791 |
| progress destruct_head'_ex |
| 792 |
| progress subst |
| 793 |
| rewrite Lift1Prop.impl1_ex1_l |
| 794 |
| rewrite !SeparationLogic.sep_assoc, SeparationLogic.impl1_l_sep_emp |
| 795 |
| rewrite SeparationLogic.sep_ex1_l |
| 796 |
| rewrite !SeparationLogic.sep_emp_emp |
| 797 |
| rewrite <- !SeparationLogic.sep_assoc |
| 798 |
| rewrite !SeparationLogic.sep_comm_emp_r |
| 799 |
| rewrite Forall2_cons_l_ex_iff in * |
| 800 |
| rewrite Forall_cons_iff in * ]. |
| 801 |
all: [ > eapply (Lift1Prop.impl1_ex1_r _ _ (_ :: _)) |
| 802 |
| eapply (Lift1Prop.impl1_ex1_r _ _ _) ]; |
| 803 |
cbn [array]. |
| 804 |
all: repeat first [ progress intros |
| 805 |
| progress destruct_head'_and |
| 806 |
| rewrite Forall2_cons_cons_iff |
| 807 |
| set_evars; rewrite Forall_cons_iff; subst_evars |
| 808 |
| rewrite Lift1Prop.impl1_ex1_l |
| 809 |
| rewrite !SeparationLogic.sep_assoc, SeparationLogic.impl1_l_sep_emp |
| 810 |
| rewrite SeparationLogic.sep_ex1_l |
| 811 |
| rewrite !SeparationLogic.sep_emp_emp |
| 812 |
| rewrite <- !SeparationLogic.sep_assoc |
| 813 |
| rewrite !SeparationLogic.sep_comm_emp_r |
| 814 |
| lazymatch goal with |
| 815 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun h : ?T => _)) ] |
| 816 |
=> lazymatch T with word.rep => idtac | Z => idtac end; |
| 817 |
eapply Lift1Prop.impl1_ex1_r |
| 818 |
end ]. |
| 819 |
all: rewrite !SeparationLogic.sep_assoc; apply SeparationLogic.impl1_r_sep_emp. |
| 820 |
all: repeat first [ solve [ eauto 10 ] |
| 821 |
| match goal with |
| 822 |
| [ |- _ /\ _ ] => split |
| 823 |
| [ H : eval_idx_Z ?G ?d ?i ?v |- eval_idx_Z ?G ?d ?i (word.unsigned ?v') ] |
| 824 |
=> let _ := open_constr:(eq_refl : v' = word.of_Z v) in |
| 825 |
replace (word.unsigned v') with v; [ exact H | rewrite ?Z.land_ones by lia; ZnWords ] |
| 826 |
end ]. |
| 827 |
all: match goal with |- Lift1Prop.impl1 ?A ?B => cut (Lift1Prop.iff1 A B); [ intros ->; reflexivity | ] end. |
| 828 |
all: repeat first [ rewrite Z.land_ones in * by lia |
| 829 |
| match goal with |
| 830 |
| [ H : eval_idx_Z ?G ?d ?x ?y, H' : eval_idx_Z ?G ?d ?x ?y' |- _ ] |
| 831 |
=> eapply eval_eval in H; [ | exact H' ] |
| 832 |
| [ |- ?R (sep (cell64 ?x ?y) _) (sep (cell64 ?x' ?y') _) ] |
| 833 |
=> progress (replace x with x' by ZnWords; replace y with y' by ZnWords) |
| 834 |
| [ |- ?R (array _ _ ?x _) (array _ _ ?x' _) ] |
| 835 |
=> progress replace x with x' by ZnWords |
| 836 |
end |
| 837 |
| progress (SeparationLogic.cancel; cbn [seps]) ]. } |
| 838 |
Qed. |
| 839 |
|
| 840 |
Lemma R_mem_combine_array_impl1 frame G d n addrs val_idxs base_value base_word_value init |
| 841 |
(Haddrs : Forall2 (eval_idx_Z G d) addrs (List.map (fun i => Z.land (base_value + 8 * Z.of_nat i) (Z.ones 64)) (seq init n))) |
| 842 |
(*(Hn : List.length val_idxs = n)*) |
| 843 |
(Hbase : base_value = word.unsigned base_word_value) |
| 844 |
: Lift1Prop.impl1 |
| 845 |
(R_mem frame G d (List.combine addrs val_idxs)) |
| 846 |
(Lift1Prop.ex1 (fun vals |
| 847 |
=> emp (Forall2 (eval_idx_Z G d) (firstn n val_idxs) vals /\ Forall (fun v => 0 <= v < 2^64)%Z vals) * frame * array cell64 (word.of_Z 8) (word.add base_word_value (word.of_Z (8 * Z.of_nat init))) vals))%sep. |
| 848 |
Proof. rewrite R_mem_combine_ex_array_iff by eassumption; reflexivity. Qed. |
| 849 |
|
| 850 |
Lemma R_mem_combine_array_iff_helper frame G d addrs val_idxs base_value base_word_value vals init |
| 851 |
(Haddrs : Forall2 (eval_idx_Z G d) addrs (List.map (fun i => Z.land (base_value + 8 * Z.of_nat i) (Z.ones 64)) (seq init (List.length vals)))) |
| 852 |
(Hvals : Forall2 (eval_idx_Z G d) val_idxs vals) |
| 853 |
(Hbase : base_value = word.unsigned base_word_value) |
| 854 |
: Lift1Prop.iff1 (R_mem frame G d (List.combine addrs val_idxs)) |
| 855 |
(frame * (emp (Forall (fun v => 0 <= v < 2^64)%Z vals) * array cell64 (word.of_Z 8) (word.add base_word_value (word.of_Z (8 * Z.of_nat init))) vals))%sep. |
| 856 |
Proof. |
| 857 |
rewrite (@sep_emp_holds_l _ _ (@bounded_of_array_cell64 _ _ _)). |
| 858 |
rewrite R_mem_combine_ex_array_iff by eassumption. |
| 859 |
subst. |
| 860 |
cbv [Lift1Prop.iff1 Lift1Prop.ex1]; split; intros; [ | exists vals ]; sepsimpl; eauto. |
| 861 |
all: rewrite ?@firstn_all in * by eauto using eq_length_Forall2, eq_sym. |
| 862 |
all: repeat first [ match goal with |
| 863 |
| [ H1 : Forall2 (eval_idx_Z _ _) ?l1 ?l2, H2 : Forall2 (eval_idx_Z _ _) ?l1 ?l2' |- _ ] |
| 864 |
=> eapply eval_eval_idx_Z_Forall2 in H2; [ | exact H1 ] |
| 865 |
end |
| 866 |
| progress subst |
| 867 |
| solve [ eauto ] ]. |
| 868 |
cbv [sep] in *; sepsimpl. |
| 869 |
eapply bounded_of_array_cell64; eassumption. |
| 870 |
Qed. |
| 871 |
|
| 872 |
Lemma R_mem_combine_array_iff frame G d n addrs val_idxs base_value base_word_value vals |
| 873 |
(Haddrs : Forall2 (eval_idx_Z G d) addrs (List.map (fun i => Z.land (base_value + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 n))) |
| 874 |
(Hvals : Forall2 (eval_idx_Z G d) val_idxs vals) |
| 875 |
(Hn : n = List.length vals) |
| 876 |
(Hbase : base_value = word.unsigned base_word_value) |
| 877 |
: Lift1Prop.iff1 (R_mem frame G d (List.combine addrs val_idxs)) |
| 878 |
(frame * (emp (Forall (fun v => 0 <= v < 2^64)%Z vals) * array cell64 (word.of_Z 8) base_word_value vals))%sep. |
| 879 |
Proof. |
| 880 |
subst n. |
| 881 |
rewrite R_mem_combine_array_iff_helper by eassumption. |
| 882 |
SeparationLogic.cancel; cbn [seps]. |
| 883 |
match goal with |
| 884 |
| [ |- Lift1Prop.iff1 (array ?cell ?sz ?init ?val) (array ?cell ?sz ?init' ?val) ] |
| 885 |
=> cut (init = init'); |
| 886 |
[ intros ->; reflexivity | ] |
| 887 |
end. |
| 888 |
ZnWords. |
| 889 |
Qed. |
| 890 |
|
| 891 |
Local Ltac cleanup_min := |
| 892 |
repeat match goal with |
| 893 |
| [ H : context[Nat.min ?x ?y] |- _ ] |
| 894 |
=> lazymatch x with context[Nat.min] => fail | _ => idtac end; |
| 895 |
lazymatch y with context[Nat.min] => fail | _ => idtac end; |
| 896 |
(assert ((x <= y)%nat) + assert ((y <= x)%nat)); |
| 897 |
[ repeat match goal with H : context[Nat.min] |- _ => clear dependent H end; |
| 898 |
lia |
| 899 |
| ]; |
| 900 |
first [ rewrite (Nat.min_l x y) in * by assumption |
| 901 |
| rewrite (Nat.min_r x y) in * by assumption ] |
| 902 |
end. |
| 903 |
|
| 904 |
Lemma bounded_of_R_scalar_or_array {dereference_scalar:bool} v |
| 905 |
: forall ws m |
| 906 |
(H : R_scalar_or_array (dereference_scalar:=dereference_scalar) v ws m), |
| 907 |
match v with |
| 908 |
| inl v => 0 <= v < 2^64 |
| 909 |
| inr vs => Forall (fun v => 0 <= v < 2^64) vs |
| 910 |
end%Z. |
| 911 |
Proof. |
| 912 |
cbv [R_scalar_or_array]; intros *; break_innermost_match; cbv [emp]; intros; destruct_head'_and; subst. |
| 913 |
all: try solve [ eapply bounded_of_cell64; eassumption |
| 914 |
| apply Properties.word.unsigned_range |
| 915 |
| eapply bounded_of_array_cell64; eassumption ]. |
| 916 |
Qed. |
| 917 |
|
| 918 |
Lemma bounded_of_R_list_scalar_nolen_or_array {dereference_scalar:bool} v |
| 919 |
: forall ws m |
| 920 |
(Hlen : List.length ws = List.length v) |
| 921 |
(H : R_list_scalar_or_array_nolen (dereference_scalar:=dereference_scalar) v ws m), |
| 922 |
Forall (fun v => match v with |
| 923 |
| inl v => 0 <= v < 2^64 |
| 924 |
| inr vs => Forall (fun v => 0 <= v < 2^64) vs |
| 925 |
end%Z) v. |
| 926 |
Proof. |
| 927 |
cbv [R_list_scalar_or_array_nolen]. |
| 928 |
induction v as [|v vs IH], ws as [|? ws]; |
| 929 |
try specialize (IH ws); |
| 930 |
cbn [fold_right List.combine List.map List.length] in *; constructor; cbv [sep] in *; sepsimpl; inversion_nat_eq. |
| 931 |
all: try solve [ eapply bounded_of_R_scalar_or_array; eassumption |
| 932 |
| eapply IH; eassumption ]. |
| 933 |
Qed. |
| 934 |
|
| 935 |
Lemma bounded_of_R_list_scalar_or_array {dereference_scalar:bool} v |
| 936 |
: forall ws m |
| 937 |
(H : R_list_scalar_or_array (dereference_scalar:=dereference_scalar) v ws m), |
| 938 |
Forall (fun v => match v with |
| 939 |
| inl v => 0 <= v < 2^64 |
| 940 |
| inr vs => Forall (fun v => 0 <= v < 2^64) vs |
| 941 |
end%Z) v. |
| 942 |
Proof. |
| 943 |
cbv [R_list_scalar_or_array]. |
| 944 |
intros; sepsimpl. |
| 945 |
eapply bounded_of_R_list_scalar_nolen_or_array; try eassumption; congruence. |
| 946 |
Qed. |
| 947 |
|
| 948 |
Lemma R_mem_flat_map_ex_R_list_scalar_or_array_iff_emp {dereference_scalar:bool} G d |
| 949 |
: forall (idxs : list (idx * option idx + idx * list idx)) base_vals addr_idxs base_vals_words |
| 950 |
(Hidxs : Forall2 (fun idx' v |
| 951 |
=> let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 952 |
match idx' with |
| 953 |
| inl (idx', addr') |
| 954 |
=> eval_idx_Z G d idx' (Z.land v (Z.ones 64)) |
| 955 |
/\ option_eq (eval_idx_Z G d) addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 956 |
| inr (base', addrs') |
| 957 |
=> eval_idx_Z G d base' (Z.land v (Z.ones 64)) |
| 958 |
/\ Forall2 (eval_idx_Z G d) addrs' (addrs_vals_of v addrs') |
| 959 |
end) |
| 960 |
idxs base_vals) |
| 961 |
(Hidxs_array : Forall2 (fun idx addr_idx |
| 962 |
=> match idx, addr_idx with |
| 963 |
| inl _, inl _ => True |
| 964 |
| inr (base, idxs), inr addr_idxs |
| 965 |
=> List.length idxs = List.length addr_idxs |
| 966 |
| inl _, inr _ | inr _, inl _ => False |
| 967 |
end) |
| 968 |
idxs addr_idxs) |
| 969 |
(Hidxs_scalar : Forall2 (fun idx addr_idx |
| 970 |
=> match idx, addr_idx with |
| 971 |
| inl (_, addr), inl addr_idx |
| 972 |
=> Option.is_Some addr = dereference_scalar |
| 973 |
| inr _, inr _ => True |
| 974 |
| inl _, inr _ | inr _, inl _ => False |
| 975 |
end) |
| 976 |
idxs addr_idxs) |
| 977 |
(Haddr_vals : Forall2 (fun idx v |
| 978 |
=> match idx with |
| 979 |
| inl base => if dereference_scalar then True else eval_idx_Z G d base v |
| 980 |
| _ => True |
| 981 |
end) |
| 982 |
addr_idxs base_vals) |
| 983 |
(Hbase_vals_words : List.map word.unsigned base_vals_words = base_vals), |
| 984 |
Lift1Prop.iff1 |
| 985 |
(R_mem (emp True) G d |
| 986 |
(List.flat_map |
| 987 |
(fun '(idx', idx) |
| 988 |
=> match idx', idx with |
| 989 |
| inl (_, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 990 |
| inr (base', addrs'), inr items |
| 991 |
=> List.combine addrs' items |
| 992 |
| _, _ => [] |
| 993 |
end) |
| 994 |
(List.combine idxs addr_idxs))) |
| 995 |
(Lift1Prop.ex1 |
| 996 |
(fun addr_vals |
| 997 |
=> emp (Forall2 (eval_idx_or_list_idx G d) addr_idxs addr_vals |
| 998 |
/\ Forall (fun v => match v with |
| 999 |
| inl v => 0 <= v < 2^64 |
| 1000 |
| inr vs => Forall (fun v => 0 <= v < 2^64) vs |
| 1001 |
end%Z) addr_vals) |
| 1002 |
* R_list_scalar_or_array (dereference_scalar:=dereference_scalar) addr_vals base_vals_words))%sep. |
| 1003 |
Proof. |
| 1004 |
pose dereference_scalar as dereference_scalar'. |
| 1005 |
induction idxs as [|idx idxs IH], |
| 1006 |
base_vals as [|base_val base_vals], |
| 1007 |
addr_idxs as [|addr_idx addr_idxs], |
| 1008 |
base_vals_words as [|base_vals_word base_vals_words]; |
| 1009 |
try specialize (IH base_vals addr_idxs base_vals_words); |
| 1010 |
do 2 (intro H; inversion H; clear H); subst. |
| 1011 |
all: cbv [R_list_scalar_or_array R_list_scalar_or_array_nolen] in *; cbn [List.map flat_map R_mem fold_right List.combine List.length]; intros; inversion_nat_eq; inversion_list. |
| 1012 |
all: SeparationLogic.cancel; cbn [seps]. |
| 1013 |
all: try (rewrite R_mem_app_iff, IH by assumption; clear IH). |
| 1014 |
{ intro x; split; revert x; refine (_ : Lift1Prop.impl1 _ _). |
| 1015 |
all: [ > apply Lift1Prop.impl1_ex1_r with (x:=nil) | apply Lift1Prop.impl1_ex1_l; intro ]. |
| 1016 |
all: cbn [List.length]. |
| 1017 |
all: rewrite Forall2_nil_l_iff. |
| 1018 |
all: rewrite ?Forall_nil_iff. |
| 1019 |
all: rewrite <- ?SeparationLogic.sep_assoc, ?SeparationLogic.sep_emp_emp. |
| 1020 |
all: try (apply SeparationLogic.impl1_r_sep_emp; split; trivial). |
| 1021 |
all: try apply SeparationLogic.impl1_l_sep_emp; intros; destruct_head'_and; subst. |
| 1022 |
all: cbn [List.combine List.map fold_right]. |
| 1023 |
all: eauto; try reflexivity. } |
| 1024 |
all: [ > ]. |
| 1025 |
all: intro x; split; revert x; refine (_ : Lift1Prop.impl1 _ _). |
| 1026 |
all: repeat |
| 1027 |
repeat |
| 1028 |
first [ progress intros |
| 1029 |
| exfalso; assumption |
| 1030 |
| progress inversion_option |
| 1031 |
| progress destruct_head'_ex |
| 1032 |
| progress destruct_head'_and |
| 1033 |
| progress subst |
| 1034 |
| progress cbn [List.combine List.map fold_right R_scalar_or_array R_mem List.length List.app Option.is_Some] in * |
| 1035 |
| progress cbv [option_eq] in * |
| 1036 |
| rewrite SeparationLogic.sep_emp_emp |
| 1037 |
| rewrite SeparationLogic.sep_ex1_l |
| 1038 |
| rewrite SeparationLogic.sep_ex1_r |
| 1039 |
| rewrite R_mem_app_iff |
| 1040 |
(*| let P := lazymatch goal with |- Lift1Prop.impl1 ?P _ => P end in |
| 1041 |
lazymatch P with |
| 1042 |
| context[sep (Lift1Prop.ex1 ?q) ?p] |
| 1043 |
=> rewrite (SeparationLogic.sep_ex1_l p q) |
| 1044 |
| context[sep ?p (Lift1Prop.ex1 ?q)] |
| 1045 |
=> rewrite (SeparationLogic.sep_ex1_r p q) |
| 1046 |
end*) |
| 1047 |
| rewrite SeparationLogic.impl1_l_sep_emp |
| 1048 |
| rewrite Lift1Prop.impl1_ex1_l |
| 1049 |
| rewrite !SeparationLogic.sep_assoc, SeparationLogic.impl1_l_sep_emp |
| 1050 |
| rewrite <- !SeparationLogic.sep_assoc |
| 1051 |
| rewrite !SeparationLogic.sep_comm_emp_r |
| 1052 |
| rewrite Forall2_cons_cons_iff in * |
| 1053 |
| set_evars; rewrite Forall_cons_iff in *; subst_evars |
| 1054 |
| rewrite Forall2_cons_l_ex_iff in * |- |
| 1055 |
| rewrite Forall2_cons_r_ex_iff in * |- |
| 1056 |
| rewrite R_mem_combine_array_impl1 by (eassumption + reflexivity) |
| 1057 |
| rewrite R_cell64_ex_cell64_iff |
| 1058 |
| progress specialize_by_assumption |
| 1059 |
| progress specialize_by reflexivity |
| 1060 |
| match goal with |
| 1061 |
| [ H : Lift1Prop.iff1 _ _ |- _ ] => rewrite H; clear H |
| 1062 |
| [ H : Forall2 _ ?addr_idxs ?v |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun h => sep _ (sep (emp (Forall2 _ ?addr_idxs h /\ _)) _))) ] |
| 1063 |
=> apply (Lift1Prop.impl1_ex1_r _ _ v) |
| 1064 |
| [ |- Lift1Prop.impl1 ?A (Lift1Prop.ex1 ?B0) ] |
| 1065 |
=> lazymatch B0 with |
| 1066 |
| fun h => sep (emp (Forall2 ?R (?addr_idx :: ?addr_idxs) h /\ _)) (@?B h) |
| 1067 |
=> lazymatch goal with |
| 1068 |
| [ H : Forall2 _ addr_idxs ?rest |- _ ] |
| 1069 |
=> cut (Lift1Prop.impl1 A (Lift1Prop.ex1 (fun h0 => Lift1Prop.ex1 (fun h1 => Lift1Prop.ex1 (fun h2 => B0 (match addr_idx with inl _ => inl (if dereference_scalar' then h0 else h1) | inr _ => inr h2 end :: rest)))))); |
| 1070 |
[ (intros ->); |
| 1071 |
let h0 := fresh in |
| 1072 |
let h1 := fresh in |
| 1073 |
let h2 := fresh in |
| 1074 |
rewrite Lift1Prop.impl1_ex1_l; intro h0; |
| 1075 |
rewrite Lift1Prop.impl1_ex1_l; intro h1; |
| 1076 |
rewrite Lift1Prop.impl1_ex1_l; intro h2; |
| 1077 |
apply (Lift1Prop.impl1_ex1_r _ _ (match addr_idx with inl _ => inl (if dereference_scalar' then h0 else h1) | inr _ => inr h2 end :: rest)); |
| 1078 |
reflexivity |
| 1079 |
| break_innermost_match; break_innermost_match_hyps; cbn [R_mem] ] |
| 1080 |
end |
| 1081 |
end |
| 1082 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun _ => ?A)) ] |
| 1083 |
=> unshelve eapply Lift1Prop.impl1_ex1_r; try solve [ constructor ]; [] |
| 1084 |
| [ H : eval_idx_or_list_idx _ _ (inr _) _ |- _ ] |
| 1085 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 1086 |
| [ H : eval_idx_or_list_idx _ _ (inl _) _ |- _ ] |
| 1087 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 1088 |
| [ H : eval_idx_or_list_idx _ _ _ (inr _) |- _ ] |
| 1089 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 1090 |
| [ H : eval_idx_or_list_idx _ _ _ (inl _) |- _ ] |
| 1091 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 1092 |
end |
| 1093 |
| progress break_innermost_match |
| 1094 |
| match goal with |
| 1095 |
| [ |- Lift1Prop.impl1 ?A ?B ] |
| 1096 |
=> lazymatch B with |
| 1097 |
| context[R_mem _ _ _ (List.combine _ _)] |
| 1098 |
=> rewrite R_mem_combine_array_iff |
| 1099 |
by solve [ eassumption | reflexivity | saturate_lengths; congruence ] |
| 1100 |
end |
| 1101 |
| [ |- Lift1Prop.impl1 _ ?B ] |
| 1102 |
=> lazymatch B with |
| 1103 |
| context[sep (emp _) _] |
| 1104 |
=> rewrite ?SeparationLogic.sep_assoc; |
| 1105 |
apply SeparationLogic.impl1_r_sep_emp; split; [ solve [ eauto ] | ] |
| 1106 |
end |
| 1107 |
end |
| 1108 |
| reflexivity |
| 1109 |
| break_innermost_match_hyps_step ]. |
| 1110 |
all: repeat first [ rewrite Z.land_ones in * by lia |
| 1111 |
| progress subst |
| 1112 |
| match goal with |
| 1113 |
| [ H1 : eval_idx_Z ?G ?d ?i _, H2 : eval_idx_Z ?G ?d ?i _ |- _ ] |
| 1114 |
=> eapply eval_eval in H2; [ | exact H1 ] |
| 1115 |
| [ H : context[(?v mod ?m)%Z] |- _ ] |
| 1116 |
=> lazymatch v with |
| 1117 |
| word.unsigned ?x |
| 1118 |
=> replace ((v mod m)%Z) with v in * by ZnWords |
| 1119 |
end |
| 1120 |
| [ H : word.unsigned _ = word.unsigned _ |- _ ] |
| 1121 |
=> apply Properties.word.unsigned_inj in H |
| 1122 |
| [ H : context[firstn ?n ?l] |- _ ] |
| 1123 |
=> rewrite firstn_all in H by congruence |
| 1124 |
| [ |- context[?v] ] |
| 1125 |
=> lazymatch v with |
| 1126 |
| word.add ?x (word.of_Z 0) |
| 1127 |
=> replace v with x by ZnWords |
| 1128 |
end |
| 1129 |
end |
| 1130 |
| progress autorewrite with zsimplify_const ]. |
| 1131 |
all: repeat match goal with |
| 1132 |
| [ H : context[word.unsigned ?x] |- _ ] |
| 1133 |
=> unique pose proof (Properties.word.unsigned_range x) |
| 1134 |
| [ |- context[word.unsigned ?x] ] |
| 1135 |
=> unique pose proof (Properties.word.unsigned_range x) |
| 1136 |
end. |
| 1137 |
all: repeat first [ match goal with |
| 1138 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun _ => ?A)) ] |
| 1139 |
=> unshelve eapply Lift1Prop.impl1_ex1_r; try solve [ constructor ]; [] |
| 1140 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 _) ] |
| 1141 |
=> eapply Lift1Prop.impl1_ex1_r |
| 1142 |
| [ |- Lift1Prop.impl1 _ ?B ] |
| 1143 |
=> lazymatch B with |
| 1144 |
| context[sep (emp _) _] |
| 1145 |
=> rewrite ?SeparationLogic.sep_assoc; |
| 1146 |
apply SeparationLogic.impl1_r_sep_emp; split; [ solve [ eauto 10 ] | ] |
| 1147 |
end |
| 1148 |
end |
| 1149 |
| reflexivity |
| 1150 |
| rewrite SeparationLogic.sep_ex1_l |
| 1151 |
| rewrite <- !SeparationLogic.sep_assoc |
| 1152 |
| rewrite SeparationLogic.sep_emp_emp |
| 1153 |
| rewrite Forall2_cons_cons_iff |
| 1154 |
| set_evars; rewrite Forall_cons_iff; subst_evars |
| 1155 |
| progress cbn [eval_idx_or_list_idx List.length] in * |
| 1156 |
| apply SeparationLogic.impl1_r_sep_emp; split; [ | reflexivity ] ]. |
| 1157 |
Qed. |
| 1158 |
|
| 1159 |
Lemma R_mem_flat_map_ex_R_list_scalar_or_array_impl_emp {dereference_scalar:bool} G d |
| 1160 |
: forall (idxs : list (idx * option idx + idx * list idx)) base_vals addr_idxs base_vals_words |
| 1161 |
(Hidxs : Forall2 (fun idx' v |
| 1162 |
=> let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 1163 |
match idx' with |
| 1164 |
| inl (idx', addr') |
| 1165 |
=> eval_idx_Z G d idx' (Z.land v (Z.ones 64)) |
| 1166 |
/\ option_eq (eval_idx_Z G d) addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 1167 |
| inr (base', addrs') |
| 1168 |
=> eval_idx_Z G d base' (Z.land v (Z.ones 64)) |
| 1169 |
/\ Forall2 (eval_idx_Z G d) addrs' (addrs_vals_of v addrs') |
| 1170 |
end) |
| 1171 |
idxs base_vals) |
| 1172 |
(Hidxs_array : Forall2 (fun idx addr_idx |
| 1173 |
=> match idx, addr_idx with |
| 1174 |
| inl _, inl _ => True |
| 1175 |
| inr (base, idxs), inr addr_idxs |
| 1176 |
=> List.length idxs = List.length addr_idxs |
| 1177 |
| inl _, inr _ | inr _, inl _ => False |
| 1178 |
end) |
| 1179 |
idxs addr_idxs) |
| 1180 |
(Hidxs_scalar : Forall2 (fun idx addr_idx |
| 1181 |
=> match idx, addr_idx with |
| 1182 |
| inl (_, addr), inl addr_idx |
| 1183 |
=> Option.is_Some addr = dereference_scalar |
| 1184 |
| inr _, inr _ => True |
| 1185 |
| inl _, inr _ | inr _, inl _ => False |
| 1186 |
end) |
| 1187 |
idxs addr_idxs) |
| 1188 |
(Haddr_vals : Forall2 (fun idx v |
| 1189 |
=> match idx with |
| 1190 |
| inl base => if dereference_scalar then True else eval_idx_Z G d base v |
| 1191 |
| _ => True |
| 1192 |
end) |
| 1193 |
addr_idxs base_vals) |
| 1194 |
(Hbase_vals_words : List.map word.unsigned base_vals_words = base_vals), |
| 1195 |
Lift1Prop.impl1 |
| 1196 |
(R_mem (emp True) G d |
| 1197 |
(List.flat_map |
| 1198 |
(fun '(idx', idx) |
| 1199 |
=> match idx', idx with |
| 1200 |
| inl (_, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 1201 |
| inr (base', addrs'), inr items |
| 1202 |
=> List.combine addrs' items |
| 1203 |
| _, _ => [] |
| 1204 |
end) |
| 1205 |
(List.combine idxs addr_idxs))) |
| 1206 |
(Lift1Prop.ex1 |
| 1207 |
(fun addr_vals |
| 1208 |
=> emp (Forall2 (eval_idx_or_list_idx G d) addr_idxs addr_vals |
| 1209 |
/\ Forall (fun v => match v with |
| 1210 |
| inl v => 0 <= v < 2^64 |
| 1211 |
| inr vs => Forall (fun v => 0 <= v < 2^64) vs |
| 1212 |
end%Z) addr_vals) |
| 1213 |
* R_list_scalar_or_array (dereference_scalar:=dereference_scalar) addr_vals base_vals_words))%sep. |
| 1214 |
Proof. |
| 1215 |
intros; rewrite R_mem_flat_map_ex_R_list_scalar_or_array_iff_emp by eassumption. |
| 1216 |
reflexivity. |
| 1217 |
Qed. |
| 1218 |
|
| 1219 |
Lemma R_mem_flat_map_R_list_scalar_or_array_iff_emp {dereference_scalar:bool} G d |
| 1220 |
: forall (idxs : list (idx * option idx + idx * list idx)) base_vals addr_idxs addr_vals base_vals_words |
| 1221 |
(Haddrs : Forall2 (eval_idx_or_list_idx G d) addr_idxs addr_vals) |
| 1222 |
(Hidxs : Forall2 (fun idx' v |
| 1223 |
=> let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 1224 |
match idx' with |
| 1225 |
| inl (idx', addr') |
| 1226 |
=> eval_idx_Z G d idx' (Z.land v (Z.ones 64)) |
| 1227 |
/\ option_eq (eval_idx_Z G d) addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 1228 |
| inr (base', addrs') |
| 1229 |
=> eval_idx_Z G d base' (Z.land v (Z.ones 64)) |
| 1230 |
/\ Forall2 (eval_idx_Z G d) addrs' (addrs_vals_of v addrs') |
| 1231 |
end) |
| 1232 |
idxs base_vals) |
| 1233 |
(Hidxs_array : Forall2 (fun idx addr_idx |
| 1234 |
=> match idx, addr_idx with |
| 1235 |
| inl _, inl _ => True |
| 1236 |
| inr (base, idxs), inr addr_idxs |
| 1237 |
=> List.length idxs = List.length addr_idxs |
| 1238 |
| inl _, inr _ | inr _, inl _ => False |
| 1239 |
end) |
| 1240 |
idxs addr_idxs) |
| 1241 |
(Hidxs_scalar : Forall2 (fun idx addr_idx |
| 1242 |
=> match idx, addr_idx with |
| 1243 |
| inl (_, addr), inl addr_idx |
| 1244 |
=> Option.is_Some addr = dereference_scalar |
| 1245 |
| inr _, inr _ => True |
| 1246 |
| inl _, inr _ | inr _, inl _ => False |
| 1247 |
end) |
| 1248 |
idxs addr_idxs) |
| 1249 |
(Haddr_vals : Forall2 (fun idx v |
| 1250 |
=> match idx with |
| 1251 |
| inl base => if dereference_scalar then True else eval_idx_Z G d base v |
| 1252 |
| _ => True |
| 1253 |
end) |
| 1254 |
addr_idxs base_vals) |
| 1255 |
(Hbase_vals_words : List.map word.unsigned base_vals_words = base_vals), |
| 1256 |
Lift1Prop.iff1 |
| 1257 |
(R_mem (emp True) G d |
| 1258 |
(List.flat_map |
| 1259 |
(fun '(idx', idx) |
| 1260 |
=> match idx', idx with |
| 1261 |
| inl (_, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 1262 |
| inr (base', addrs'), inr items |
| 1263 |
=> List.combine addrs' items |
| 1264 |
| _, _ => [] |
| 1265 |
end) |
| 1266 |
(List.combine idxs addr_idxs))) |
| 1267 |
(R_list_scalar_or_array (dereference_scalar:=dereference_scalar) addr_vals base_vals_words)%sep. |
| 1268 |
Proof. |
| 1269 |
intros; rewrite R_mem_flat_map_ex_R_list_scalar_or_array_iff_emp by eassumption. |
| 1270 |
cbv [Lift1Prop.iff1 Lift1Prop.ex1]; split; intros; [ | eexists ]; sepsimpl; eauto. |
| 1271 |
eapply eval_eval_idx_or_list_idx_Forall2 in Haddrs; [ | clear Haddrs; eassumption ]. |
| 1272 |
{ subst; assumption. } |
| 1273 |
{ eapply bounded_of_R_list_scalar_or_array; eassumption. } |
| 1274 |
Qed. |
| 1275 |
|
| 1276 |
|
| 1277 |
Lemma R_mem_flat_map_R_list_scalar_or_array_iff {dereference_scalar:bool} frame G d |
| 1278 |
(idxs : list (idx * option idx + idx * list idx)) base_vals addr_idxs addr_vals base_vals_words |
| 1279 |
(Haddrs : Forall2 (eval_idx_or_list_idx G d) addr_idxs addr_vals) |
| 1280 |
(Hidxs : Forall2 (fun idx' v |
| 1281 |
=> let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 1282 |
match idx' with |
| 1283 |
| inl (idx', addr') |
| 1284 |
=> eval_idx_Z G d idx' (Z.land v (Z.ones 64)) |
| 1285 |
/\ option_eq (eval_idx_Z G d) addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 1286 |
| inr (base', addrs') |
| 1287 |
=> eval_idx_Z G d base' (Z.land v (Z.ones 64)) |
| 1288 |
/\ Forall2 (eval_idx_Z G d) addrs' (addrs_vals_of v addrs') |
| 1289 |
end) |
| 1290 |
idxs base_vals) |
| 1291 |
(Hidxs_array : Forall2 (fun idx addr_idx |
| 1292 |
=> match idx, addr_idx with |
| 1293 |
| inl _, inl _ => True |
| 1294 |
| inr (base, idxs), inr addr_idxs |
| 1295 |
=> List.length idxs = List.length addr_idxs |
| 1296 |
| inl _, inr _ | inr _, inl _ => False |
| 1297 |
end) |
| 1298 |
idxs addr_idxs) |
| 1299 |
(Hidxs_scalar : Forall2 (fun idx addr_idx |
| 1300 |
=> match idx, addr_idx with |
| 1301 |
| inl (_, addr), inl addr_idx |
| 1302 |
=> Option.is_Some addr = dereference_scalar |
| 1303 |
| inr _, inr _ => True |
| 1304 |
| inl _, inr _ | inr _, inl _ => False |
| 1305 |
end) |
| 1306 |
idxs addr_idxs) |
| 1307 |
(Haddr_vals : Forall2 (fun idx v |
| 1308 |
=> match idx with |
| 1309 |
| inl base => if dereference_scalar then True else eval_idx_Z G d base v |
| 1310 |
| _ => True |
| 1311 |
end) |
| 1312 |
addr_idxs base_vals) |
| 1313 |
(Hbase_vals_words : List.map word.unsigned base_vals_words = base_vals) |
| 1314 |
: Lift1Prop.iff1 |
| 1315 |
(R_mem frame G d |
| 1316 |
(List.flat_map |
| 1317 |
(fun '(idx', idx) |
| 1318 |
=> match idx', idx with |
| 1319 |
| inl (_, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 1320 |
| inr (base', addrs'), inr items |
| 1321 |
=> List.combine addrs' items |
| 1322 |
| _, _ => [] |
| 1323 |
end) |
| 1324 |
(List.combine idxs addr_idxs))) |
| 1325 |
(frame * R_list_scalar_or_array (dereference_scalar:=dereference_scalar) addr_vals base_vals_words)%sep. |
| 1326 |
Proof. |
| 1327 |
intros. |
| 1328 |
rewrite ?(R_mem_frame_cps_id I frame). |
| 1329 |
erewrite R_mem_flat_map_R_list_scalar_or_array_iff_emp by eassumption. |
| 1330 |
reflexivity. |
| 1331 |
Qed. |
| 1332 |
|
| 1333 |
(* TODO: move? *) |
| 1334 |
Local Lemma eq_list_of_filter_nil A (l : list A) l1 l2 |
| 1335 |
(H : (List.length l1 = List.length l2) /\ (List.length l1 <= List.length l)%nat) |
| 1336 |
(H'' : filter (fun '(_, (init, final)) => negb (init =? final)%N) |
| 1337 |
(List.combine l (List.combine l2 l1)) = []) |
| 1338 |
: l1 = l2. |
| 1339 |
Proof. |
| 1340 |
apply List.nth_error_ext; intro i. |
| 1341 |
rewrite eq_filter_nil_Forall_iff, Forall_forall_iff_nth_error_match in H''. |
| 1342 |
specialize (H'' i). |
| 1343 |
rewrite !nth_error_combine in H''. |
| 1344 |
break_innermost_match_hyps; reflect_hyps; subst; try congruence. |
| 1345 |
all: repeat first [ exfalso; lia |
| 1346 |
| reflexivity |
| 1347 |
| match goal with |
| 1348 |
| [ H : nth_error _ _ = None |- _ ] |
| 1349 |
=> apply nth_error_error_length in H |
| 1350 |
| [ H : nth_error _ _ = Some _ |- _ ] |
| 1351 |
=> apply nth_error_value_length in H |
| 1352 |
end |
| 1353 |
| rewrite nth_error_length_error by lia ]. |
| 1354 |
Qed. |
| 1355 |
|
| 1356 |
Lemma bounded_of_R_regs G d s m |
| 1357 |
(H : R_regs G d s m) |
| 1358 |
: Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) (Tuple.to_list _ m). |
| 1359 |
Proof. |
| 1360 |
cbv [R_regs R_reg] in H. |
| 1361 |
rewrite Tuple.fieldwise_to_list_iff in H. |
| 1362 |
rewrite Forall_forall_iff_nth_error_match. |
| 1363 |
rewrite Forall2_forall_iff_nth_error in H. |
| 1364 |
intro i; specialize (H i). |
| 1365 |
cbv [option_eq] in *. |
| 1366 |
break_innermost_match; break_innermost_match_hyps; inversion_option; destruct_head'_and; trivial. |
| 1367 |
rewrite Z.land_ones in * by lia. |
| 1368 |
Z.to_euclidean_division_equations; nia. |
| 1369 |
Qed. |
| 1370 |
|
| 1371 |
Lemma reg_bounded_of_R frame G s m |
| 1372 |
(H : R frame G s m) |
| 1373 |
: Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) (Tuple.to_list _ m.(machine_reg_state)). |
| 1374 |
Proof. |
| 1375 |
destruct s; cbv [R] in H. |
| 1376 |
eapply bounded_of_R_regs, H. |
| 1377 |
Qed. |
| 1378 |
|
| 1379 |
Lemma eq_lift_to_Forall2_rets_inner mem_st idxs rets |
| 1380 |
(Hrets : Some rets = Option.List.lift (List.map (fun a => load a mem_st) idxs)) |
| 1381 |
: Forall2 (fun rv idx => load idx mem_st = Some rv) rets idxs. |
| 1382 |
Proof. |
| 1383 |
revert idxs rets Hrets; cbv [Option.List.lift option_map Crypto.Util.Option.bind]. |
| 1384 |
induction idxs as [|idx idxs IH], rets as [|ret rets]; |
| 1385 |
try specialize (IH rets); cbn [List.map fold_right]; intros. |
| 1386 |
all: repeat first [ progress inversion_option |
| 1387 |
| progress inversion_list |
| 1388 |
| progress subst |
| 1389 |
| progress break_match_hyps |
| 1390 |
| solve [ eauto ] |
| 1391 |
| constructor ]. |
| 1392 |
Qed. |
| 1393 |
|
| 1394 |
Lemma eq_lift_to_Forall2_rets mem_st idxs rets |
| 1395 |
(Hrets : Some rets |
| 1396 |
= Option.List.lift |
| 1397 |
(List.map (fun idxs |
| 1398 |
=> match idxs : idx + list idx with |
| 1399 |
| inr idxs => option_map inr (Option.List.lift (List.map (fun a => load a mem_st) idxs)) |
| 1400 |
| inl idx => Some (inl idx) |
| 1401 |
end) |
| 1402 |
idxs)) |
| 1403 |
: Forall2 (fun rv idxs |
| 1404 |
=> match idxs, rv with |
| 1405 |
| inl idx, inl v => idx = v |
| 1406 |
| inr idxs, inr rets => Forall2 (fun rv idx => load idx mem_st = Some rv) rets idxs |
| 1407 |
| inl _, inr _ | inr _, inl _ => False |
| 1408 |
end) |
| 1409 |
rets idxs. |
| 1410 |
Proof. |
| 1411 |
revert idxs rets Hrets; cbv [Option.List.lift option_map Crypto.Util.Option.bind]. |
| 1412 |
induction idxs as [|idx idxs IH], rets as [|ret rets]; |
| 1413 |
try specialize (IH rets); cbn [List.map fold_right]; intros. |
| 1414 |
all: repeat first [ progress inversion_option |
| 1415 |
| progress inversion_list |
| 1416 |
| progress subst |
| 1417 |
| break_innermost_match_hyps_step |
| 1418 |
| break_innermost_match_step |
| 1419 |
| progress break_match_hyps |
| 1420 |
| progress specialize_by reflexivity |
| 1421 |
| eapply eq_lift_to_Forall2_rets_inner; (idtac + symmetry); eassumption |
| 1422 |
| solve [ eauto ] |
| 1423 |
| constructor ]. |
| 1424 |
Qed. |
| 1425 |
|
| 1426 |
Lemma build_inputs_ok_R {descr:description} G ss types inputs args d' frame ms |
| 1427 |
(d := ss.(dag_state)) |
| 1428 |
(H : build_inputs types d = (inputs, d')) |
| 1429 |
(HR : R frame G ss ms) |
| 1430 |
(Hargs : Forall2 val_or_list_val_matches_spec args types) |
| 1431 |
(Hbounds : Forall (fun v => match v with |
| 1432 |
| inl v => (0 <= v < 2^64)%Z |
| 1433 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 1434 |
end) args) |
| 1435 |
: exists G', |
| 1436 |
Forall2 (eval_idx_or_list_idx G' d') inputs args |
| 1437 |
/\ gensym_dag_ok G' d' |
| 1438 |
/\ (forall e n, eval G d e n -> eval G' d' e n) |
| 1439 |
/\ R frame G' (update_dag_with ss (fun _ => d')) ms. |
| 1440 |
Proof. |
| 1441 |
eapply build_inputs_ok in H; [ | try eassumption .. ]; [ | destruct ss; apply HR .. ]; []. |
| 1442 |
destruct H as [G' H]; exists G'. |
| 1443 |
intuition idtac. |
| 1444 |
eapply R_subsumed; eassumption. |
| 1445 |
Qed. |
| 1446 |
|
| 1447 |
Lemma build_merge_stack_placeholders_ok_R {opts : symbolic_options_computed_opt} {descr:description} G s s' frame frame' ms |
| 1448 |
(rsp_val : Z) (stack_vals : list Z) base_stack base_stack_word_val |
| 1449 |
(H : build_merge_stack_placeholders (List.length stack_vals) s = Success (base_stack, s')) |
| 1450 |
(d := s.(dag_state)) |
| 1451 |
(d' := s'.(dag_state)) |
| 1452 |
(r := s.(symbolic_reg_state)) |
| 1453 |
(r' := s'.(symbolic_reg_state)) |
| 1454 |
(f := s.(symbolic_flag_state)) |
| 1455 |
(f' := s'.(symbolic_flag_state)) |
| 1456 |
(m := s.(symbolic_mem_state)) |
| 1457 |
(m' := s'.(symbolic_mem_state)) |
| 1458 |
(Hstack_vals_bounded : Forall (fun v : Z => (0 <= v < 2 ^ 64)%Z) stack_vals) |
| 1459 |
(stack_addr_vals := List.map (fun i => Z.land (rsp_val - 8 * Z.of_nat (List.length stack_vals) + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length stack_vals))) |
| 1460 |
(HR : R frame' G s ms) |
| 1461 |
(Hrsp : (rsp_val - 8 * Z.of_nat (List.length stack_vals))%Z = word.unsigned base_stack_word_val) |
| 1462 |
(Hframe : Lift1Prop.iff1 frame' (frame * array cell64 (word.of_Z 8) base_stack_word_val stack_vals)%sep) |
| 1463 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 1464 |
(Hrsp_val : Z.land rsp_val (Z.ones 64) = Z.land (Semantics.get_reg ms rsp) (Z.ones 64)) |
| 1465 |
: exists G', |
| 1466 |
((exists rsp_idx, |
| 1467 |
set_reg r (reg_index rsp) rsp_idx = r' |
| 1468 |
/\ eval_idx_Z G' d' rsp_idx (Z.land rsp_val (Z.ones 64))) |
| 1469 |
/\ f = f' |
| 1470 |
/\ (exists stack_addr_idxs stack_idxs, |
| 1471 |
Forall2 (eval_idx_Z G' d') stack_addr_idxs stack_addr_vals |
| 1472 |
/\ Forall2 (eval_idx_Z G' d') stack_idxs stack_vals |
| 1473 |
/\ (* TODO: Is this too specific a spec? *) List.rev (List.combine stack_addr_idxs stack_idxs) ++ m = m')) |
| 1474 |
/\ eval_idx_Z G' d' base_stack (Z.land (rsp_val - 8 * Z.of_nat (List.length stack_vals)) (Z.ones 64)) |
| 1475 |
/\ gensym_dag_ok G' d' |
| 1476 |
/\ (forall e n, eval G d e n -> eval G' d' e n) |
| 1477 |
/\ R frame G' s' ms |
| 1478 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true). |
| 1479 |
|
| 1480 |
Proof. |
| 1481 |
eapply build_merge_stack_placeholders_ok in H; [ | try eassumption .. ]; [ | destruct s; apply HR .. ]; []. |
| 1482 |
destruct H as [G' H]; exists G'. |
| 1483 |
intuition try eassumption. |
| 1484 |
all: destruct_head'_ex; destruct_head'_and. |
| 1485 |
all: subst m m' f f' r r' d d'. |
| 1486 |
all: destruct s, s'; cbv [R] in *; cbn [Symbolic.dag_state Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state Symbolic.symbolic_reg_state] in *. |
| 1487 |
all: subst. |
| 1488 |
all: destruct_head'_and. |
| 1489 |
1: intuition eauto using R_flags_subsumed. |
| 1490 |
{ eapply R_regs_subsumed_get_reg_same_eval; |
| 1491 |
[ eapply R_regs_subsumed; now eauto | ]. |
| 1492 |
apply R_regs_preserved_set_reg; [ reflexivity | ]. |
| 1493 |
specialize (Hreg_good rsp). |
| 1494 |
cbv [Option.is_Some] in *; break_innermost_match_hyps; try congruence. |
| 1495 |
eexists; split; [ reflexivity | ]. |
| 1496 |
cbv [R_regs_preserved_v] in *. |
| 1497 |
erewrite <- Semantics_get_reg_eq_nth_default_of_R_regs by first [ reflexivity | eassumption ]. |
| 1498 |
congruence. } |
| 1499 |
{ destruct ms; cbn [Semantics.machine_mem_state] in *. |
| 1500 |
clear Hrsp_val. |
| 1501 |
lazymatch goal with |
| 1502 |
| [ H : R_mem ?frame _ _ ?ss ?m |- R_mem ?frame' ?G' ?d' ?ss' ?m ] |
| 1503 |
=> eapply R_mem_subsumed in H; |
| 1504 |
[ revert m H; |
| 1505 |
refine (_ : Lift1Prop.impl1 (R_mem frame G' d' ss) (R_mem frame' G' d' ss')) |
| 1506 |
| now eauto ] |
| 1507 |
end. |
| 1508 |
rewrite (R_mem_frame_cps_id I frame), (R_mem_frame_cps_id I frame'), R_mem_app_iff, Hframe. |
| 1509 |
rewrite R_mem_rev_iff, R_mem_combine_ex_array_iff by eassumption. |
| 1510 |
repeat rewrite ?SeparationLogic.sep_ex1_r, ?SeparationLogic.sep_ex1_l. |
| 1511 |
unshelve eapply Lift1Prop.impl1_ex1_r; try exact stack_vals. |
| 1512 |
match goal with |
| 1513 |
| [ |- Lift1Prop.impl1 ?P ?Q ] |
| 1514 |
=> let v1 := lazymatch P with |
| 1515 |
| context[array _ _ ?base] => base |
| 1516 |
end in |
| 1517 |
let v2 := lazymatch Q with |
| 1518 |
| context[array _ _ ?base] => base |
| 1519 |
end in |
| 1520 |
replace v2 with v1 by ZnWords |
| 1521 |
end. |
| 1522 |
match goal with |
| 1523 |
[ |- Lift1Prop.impl1 ?P ?Q ] |
| 1524 |
=> cut (Lift1Prop.iff1 P Q); [ intros ->; reflexivity | ] |
| 1525 |
end. |
| 1526 |
SeparationLogic.cancel; cbn [seps]. |
| 1527 |
apply SeparationLogic.Proper_emp_iff; split; trivial; intros. |
| 1528 |
intuition idtac. |
| 1529 |
saturate_lengths. |
| 1530 |
rewrite ListUtil.firstn_all by congruence. |
| 1531 |
assumption. } |
| 1532 |
{ rewrite get_reg_set_reg_full; break_innermost_match; eauto. } |
| 1533 |
Qed. |
| 1534 |
|
| 1535 |
Local Ltac find_list_in ls in_ls := |
| 1536 |
lazymatch in_ls with |
| 1537 |
| ls => idtac |
| 1538 |
| firstn _ ?ls' => find_list_in ls ls' |
| 1539 |
| List.combine ?lsa ?lsb => first [ find_list_in ls lsa | find_list_in ls lsb ] |
| 1540 |
| List.map _ ?ls' => find_list_in ls ls' |
| 1541 |
end. |
| 1542 |
|
| 1543 |
Local Ltac find_sublist_in ls in_ls := |
| 1544 |
first [ find_list_in ls in_ls |
| 1545 |
| lazymatch ls with |
| 1546 |
| firstn _ ?ls => find_sublist_in ls in_ls |
| 1547 |
| List.combine ?lsa ?lsb => first [ find_sublist_in lsa in_ls | find_sublist_in lsb in_ls ] |
| 1548 |
| List.map _ ?ls => find_sublist_in ls in_ls |
| 1549 |
end ]. |
| 1550 |
|
| 1551 |
Local Ltac revert_Forall_step ls := |
| 1552 |
match goal with |
| 1553 |
| [ H : Forall2 _ ?lsa ?lsb |- _ ] |
| 1554 |
=> first [ find_list_in ls lsa | find_list_in ls lsb ]; |
| 1555 |
revert H |
| 1556 |
| [ H : Forall _ ?ls' |- _ ] |
| 1557 |
=> find_list_in ls ls'; |
| 1558 |
revert H |
| 1559 |
| [ H : context[nth_error ?ls' _] |- _ ] |
| 1560 |
=> find_list_in ls ls'; |
| 1561 |
revert H |
| 1562 |
end. |
| 1563 |
|
| 1564 |
Local Ltac revert_Foralls := |
| 1565 |
repeat match goal with |
| 1566 |
| [ |- context[Forall2 _ ?ls _] ] |
| 1567 |
=> revert_Forall_step ls |
| 1568 |
| [ |- context[Forall2 _ _ ?ls] ] |
| 1569 |
=> revert_Forall_step ls |
| 1570 |
| [ |- context[Forall _ ?ls] ] |
| 1571 |
=> revert_Forall_step ls |
| 1572 |
end. |
| 1573 |
|
| 1574 |
Local Ltac intros_then_revert tac := |
| 1575 |
let H := fresh in |
| 1576 |
pose proof I as H; |
| 1577 |
intros; |
| 1578 |
tac (); |
| 1579 |
revert_until H; |
| 1580 |
clear H. |
| 1581 |
|
| 1582 |
Local Ltac reverted_Foralls_to_nth_error := |
| 1583 |
rewrite ?@Forall2_forall_iff_nth_error, ?@Forall_forall_iff_nth_error_match; |
| 1584 |
cbv [option_eq]; |
| 1585 |
intros_then_revert |
| 1586 |
ltac:(fun _ |
| 1587 |
=> repeat match goal with |
| 1588 |
| [ H : context[nth_error ?ls _] |- context[nth_error ?ls ?i] ] |
| 1589 |
=> specialize (H i) |
| 1590 |
| [ H : context[nth_error ?ls _], H' : context[nth_error ?ls ?i] |- _ ] |
| 1591 |
=> specialize (H i) |
| 1592 |
| [ H : context[nth_error ?ls _] |- context[nth_error ?ls' ?i] ] |
| 1593 |
=> first [ find_list_in ls ls' | find_list_in ls' ls ]; |
| 1594 |
specialize (H i) |
| 1595 |
| [ H : context[nth_error ?ls _], H' : context[nth_error ?ls' ?i] |- _ ] |
| 1596 |
=> find_list_in ls ls'; specialize (H i) |
| 1597 |
end). |
| 1598 |
|
| 1599 |
Local Ltac revert_Foralls_to_nth_error := revert_Foralls; reverted_Foralls_to_nth_error. |
| 1600 |
|
| 1601 |
Ltac adjust_Foralls_firstn_skipn := |
| 1602 |
let rep a a' := |
| 1603 |
tryif constr_eq a a' |
| 1604 |
then idtac |
| 1605 |
else (let H' := fresh in |
| 1606 |
assert (H' : a' = a) by congruence; |
| 1607 |
rewrite H' in * ) in |
| 1608 |
let check_H H := |
| 1609 |
lazymatch type of H with |
| 1610 |
| Forall _ _ => idtac |
| 1611 |
| Forall2 _ _ _ => idtac |
| 1612 |
end in |
| 1613 |
repeat match goal with |
| 1614 |
| [ H : context[firstn ?n ?ls] |- context[firstn ?n' ?ls] ] |
| 1615 |
=> check_H H; progress rep n n' |
| 1616 |
| [ H : context[skipn ?n ?ls] |- context[skipn ?n' ?ls] ] |
| 1617 |
=> check_H H; progress rep n n' |
| 1618 |
end. |
| 1619 |
|
| 1620 |
Local Ltac Foralls_to_nth_error_rewrites := |
| 1621 |
repeat rewrite ?@nth_error_combine, ?@nth_error_firstn, ?@nth_error_skipn, ?@nth_error_map, ?@nth_error_seq; cbv [option_map]. |
| 1622 |
|
| 1623 |
Local Ltac Foralls_to_nth_error_destructs := |
| 1624 |
let cleanup _ := |
| 1625 |
try match goal with |
| 1626 |
| [ |- context[False -> _] ] => intros; exfalso; assumption |
| 1627 |
| [ |- context[Some _ = None -> _] ] => intros; now inversion_option |
| 1628 |
| [ |- context[None = Some _ -> _] ] => intros; now inversion_option |
| 1629 |
| [ |- context[_ -> True] ] => intros; exact I |
| 1630 |
| [ |- context[_ -> ?x = ?x] ] => intros; reflexivity |
| 1631 |
end in |
| 1632 |
let match_free v := |
| 1633 |
lazymatch v with |
| 1634 |
| context[match _ with _ => _ end] => fail |
| 1635 |
| _ => idtac |
| 1636 |
end in |
| 1637 |
let do_destruct v := |
| 1638 |
first [ is_var v; destruct v |
| 1639 |
| lazymatch type of v with |
| 1640 |
| sumbool _ _ => destruct v |
| 1641 |
| _ => let H := fresh in |
| 1642 |
destruct v eqn:H; revert H |
| 1643 |
end ] in |
| 1644 |
let find_v P := |
| 1645 |
match P with |
| 1646 |
| context[False] |
| 1647 |
=> lazymatch P with |
| 1648 |
| match ?v with _ => _ end => v |
| 1649 |
end |
| 1650 |
| match ?v with _ => _ end = ?RHS |
| 1651 |
=> let LHS := lazymatch P with ?LHS = _ => LHS end in |
| 1652 |
lazymatch RHS with |
| 1653 |
| None |
| 1654 |
=> lazymatch LHS with |
| 1655 |
| context[None] => v |
| 1656 |
end |
| 1657 |
| Some _ |
| 1658 |
=> lazymatch LHS with |
| 1659 |
| context[Some _] => v |
| 1660 |
end |
| 1661 |
end |
| 1662 |
end in |
| 1663 |
repeat (first [ match goal with |
| 1664 |
| [ |- context[?P -> _] ] |
| 1665 |
=> let v := find_v P in |
| 1666 |
match_free v; do_destruct v |
| 1667 |
| [ |- context[?P -> _] ] |
| 1668 |
=> let v := find_v P in |
| 1669 |
do_destruct v |
| 1670 |
end |
| 1671 |
| break_innermost_match_step |
| 1672 |
| progress Foralls_to_nth_error_rewrites ]; |
| 1673 |
cleanup ()). |
| 1674 |
|
| 1675 |
Local Ltac Foralls_to_nth_error_cleanup := |
| 1676 |
intros_then_revert |
| 1677 |
ltac:(fun _ |
| 1678 |
=> repeat first [ assumption |
| 1679 |
| exact I |
| 1680 |
| exfalso; assumption |
| 1681 |
| progress subst |
| 1682 |
| progress destruct_head'_and |
| 1683 |
| progress inversion_option |
| 1684 |
| progress inversion_pair |
| 1685 |
| progress inversion_sum |
| 1686 |
| discriminate ]). |
| 1687 |
|
| 1688 |
Local Ltac Foralls_to_nth_error := |
| 1689 |
revert_Foralls_to_nth_error; |
| 1690 |
intros *; |
| 1691 |
Foralls_to_nth_error_rewrites; |
| 1692 |
Foralls_to_nth_error_destructs; |
| 1693 |
Foralls_to_nth_error_cleanup. |
| 1694 |
|
| 1695 |
Local Ltac handle_simple_R_mem := |
| 1696 |
repeat first [ progress cbn [R_mem] |
| 1697 |
| match goal with |
| 1698 |
| [ |- context[R_mem ?frame _ _ _] ] |
| 1699 |
=> lazymatch frame with |
| 1700 |
| emp _ => fail |
| 1701 |
| _ => rewrite !(R_mem_frame_cps_id I frame) |
| 1702 |
end |
| 1703 |
| [ |- context[R_mem (emp _) _ _ (_ ++ _)] ] |
| 1704 |
=> rewrite R_mem_app_iff |
| 1705 |
| [ |- context[R_mem (emp _) _ _ (List.rev _)] ] |
| 1706 |
=> rewrite R_mem_rev_iff |
| 1707 |
| [ |- context[R_mem (emp _) _ _ (List.combine _ _)] ] |
| 1708 |
=> rewrite R_mem_combine_ex_array_iff; |
| 1709 |
[ |
| 1710 |
| lazymatch goal with |
| 1711 |
| [ |- Forall2 _ _ _ ] => eapply Forall2_weaken; [ | eassumption ]; eauto using lift_eval_idx_Z_impl |
| 1712 |
| [ |- (Tuple.nth_default 0 (reg_index _) _ - 8 * Z.of_nat (Datatypes.length _))%Z = word.unsigned _ ] |
| 1713 |
=> erewrite <- Semantics_get_reg_eq_nth_default_of_R_regs by (eassumption + reflexivity); (*try*) eassumption |
| 1714 |
end .. ] |
| 1715 |
| [ |- Lift1Prop.impl1 ?P _ ] |
| 1716 |
=> match P with |
| 1717 |
| context[sep ?p (Lift1Prop.ex1 ?q)] |
| 1718 |
=> rewrite (SeparationLogic.sep_ex1_r p q) |
| 1719 |
| context[sep (Lift1Prop.ex1 ?q) ?p] |
| 1720 |
=> rewrite (SeparationLogic.sep_ex1_l p q) |
| 1721 |
| Lift1Prop.ex1 _ |
| 1722 |
=> rewrite Lift1Prop.impl1_ex1_l; intro |
| 1723 |
end |
| 1724 |
| [ H : Forall2 _ _ (_ ++ _) |- _ ] => rewrite Forall2_app_r_iff in H |
| 1725 |
| [ |- context[List.flat_map _ (_ ++ _)] ] |
| 1726 |
=> rewrite flat_map_app |
| 1727 |
| [ |- context[List.combine ?ls (?l1 ++ ?l2)] ] |
| 1728 |
=> is_var ls; is_var l1; |
| 1729 |
rewrite <- (firstn_skipn (List.length l1) ls), combine_app_samelength |
| 1730 |
by (saturate_lengths; lia) |
| 1731 |
| [ H : Forall2 _ (firstn ?n ?l1) _ |- context[List.combine ?l1 ?l2] ] |
| 1732 |
=> is_var l1; |
| 1733 |
rewrite <- (firstn_skipn n l1), <- (firstn_skipn n l2), combine_app_samelength by (saturate_lengths; congruence) |
| 1734 |
| [ H : Forall2 _ ?ls _ |- context[firstn ?n' ?ls] ] |
| 1735 |
=> let H' := fresh in |
| 1736 |
pose proof H as H'; |
| 1737 |
apply (Forall2_firstn (n:=n')) in H; |
| 1738 |
apply (Forall2_skipn (n:=n')) in H' |
| 1739 |
end |
| 1740 |
| progress destruct_head'_and |
| 1741 |
| rewrite !SeparationLogic.sep_emp_emp |
| 1742 |
| rewrite <- ?SeparationLogic.sep_assoc; |
| 1743 |
progress repeat first [ rewrite !SeparationLogic.sep_emp_emp | rewrite SeparationLogic.sep_comm_emp_r ] |
| 1744 |
| rewrite ?SeparationLogic.sep_assoc, SeparationLogic.impl1_l_sep_emp; intro |
| 1745 |
| progress autorewrite with zsimplify_const ]. |
| 1746 |
|
| 1747 |
Local Ltac handle_eval_eval := |
| 1748 |
repeat match goal with |
| 1749 |
| [ H : eval_idx_Z _ _ ?i _, H' : eval_idx_Z _ _ ?i _ |- _ ] |
| 1750 |
=> eapply eval_eval_idx_Z in H'; [ | eapply lift_eval_idx_Z_impl; [ | exact H ]; now eauto ] |
| 1751 |
end. |
| 1752 |
|
| 1753 |
Lemma build_merge_base_addresses_ok_R |
| 1754 |
{opts : symbolic_options_computed_opt} {descr:description} {dereference_scalar:bool} |
| 1755 |
(idxs : list (idx + list idx)) |
| 1756 |
(reg_available : list REG) |
| 1757 |
(runtime_reg : list Z) |
| 1758 |
G s frame frame' ms |
| 1759 |
(d := s.(dag_state)) |
| 1760 |
(Hvals : Forall2 (fun it rv |
| 1761 |
=> match it with |
| 1762 |
| inl idx => if dereference_scalar |
| 1763 |
then True |
| 1764 |
else eval_idx_Z G d idx rv |
| 1765 |
| inr _ => True |
| 1766 |
end) |
| 1767 |
idxs (List.firstn (List.length idxs) runtime_reg)) |
| 1768 |
(Hreg : Nat.min (List.length idxs) (List.length reg_available) <= List.length runtime_reg) |
| 1769 |
(Henough_reg : List.length idxs <= List.length reg_available) |
| 1770 |
s' |
| 1771 |
(d' := s'.(dag_state)) |
| 1772 |
(r := s.(symbolic_reg_state)) |
| 1773 |
(r' := s'.(symbolic_reg_state)) |
| 1774 |
(f := s.(symbolic_flag_state)) |
| 1775 |
(f' := s'.(symbolic_flag_state)) |
| 1776 |
(m := s.(symbolic_mem_state)) |
| 1777 |
(m' := s'.(symbolic_mem_state)) |
| 1778 |
outputaddrs |
| 1779 |
(H : build_merge_base_addresses (dereference_scalar:=dereference_scalar) idxs reg_available s = Success (outputaddrs, s')) |
| 1780 |
(Hreg_available_wide : Forall (fun reg => let '(rn, lo, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) reg_available) |
| 1781 |
(HR : R frame' G s ms) |
| 1782 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 1783 |
(Hruntime_reg : get_asm_reg ms reg_available = runtime_reg) |
| 1784 |
addr_vals addr_ptr_vals |
| 1785 |
(Haddr_ptr_vals : List.map word.unsigned addr_ptr_vals = List.firstn (List.length idxs) runtime_reg) |
| 1786 |
(Hframe : Lift1Prop.iff1 frame' (frame * R_list_scalar_or_array (dereference_scalar:=dereference_scalar) addr_vals addr_ptr_vals)%sep) |
| 1787 |
(Heval_addr_vals : Forall2 (eval_idx_or_list_idx G d) idxs addr_vals) |
| 1788 |
(Hruntime_reg_bounded : Forall (fun v => (0 <= v < 2^64)%Z) runtime_reg) |
| 1789 |
: exists G', |
| 1790 |
((exists (outputaddrs' : list (idx * option idx + idx * list idx)), |
| 1791 |
let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 1792 |
fold_left (fun rst '(r, idx') |
| 1793 |
=> set_reg rst (reg_index r) |
| 1794 |
match idx' with |
| 1795 |
| inl (idx', _) => idx' |
| 1796 |
| inr (base_idx', idxs') => base_idx' |
| 1797 |
end) |
| 1798 |
(List.combine reg_available outputaddrs') |
| 1799 |
r |
| 1800 |
= r' |
| 1801 |
/\ ((* TODO: Is this too specific a spec? *) |
| 1802 |
List.rev (List.flat_map |
| 1803 |
(fun '(idx', idx) |
| 1804 |
=> match idx', idx with |
| 1805 |
| inl (reg_val, Some addr), inl val => if dereference_scalar then [(addr, val)] else [] |
| 1806 |
|
| 1807 |
| inl _, _ | _, inl _ => [] |
| 1808 |
| inr (base', addrs'), inr items |
| 1809 |
=> List.combine addrs' items |
| 1810 |
end) |
| 1811 |
(List.combine outputaddrs' idxs)) |
| 1812 |
++ m) |
| 1813 |
= m' |
| 1814 |
/\ (* outputaddrs' base / array *) |
| 1815 |
Forall2 |
| 1816 |
(fun idx' v => |
| 1817 |
match idx' with |
| 1818 |
| inl (idx', addr') (* scalars *) |
| 1819 |
=> eval_idx_Z G' d' idx' (Z.land v (Z.ones 64)) |
| 1820 |
/\ option_eq (eval_idx_Z G' d') addr' (if dereference_scalar then Some (Z.land v (Z.ones 64)) else None) |
| 1821 |
| inr (base', addrs') |
| 1822 |
(* arrays: (exists base', |
| 1823 |
(* set_reg r rn base' = r' |
| 1824 |
/\ *) eval_idx_Z G' d' base' (Z.land base_reg_val (Z.ones 64))) *) |
| 1825 |
=> eval_idx_Z G' d' base' (Z.land v (Z.ones 64)) |
| 1826 |
/\ (* arrays : Forall2 (eval_idx_Z G' d') addrs addrs_vals *) |
| 1827 |
Forall2 (eval_idx_Z G' d') addrs' (addrs_vals_of v addrs') |
| 1828 |
end) |
| 1829 |
outputaddrs' |
| 1830 |
(List.firstn (List.length outputaddrs') runtime_reg) |
| 1831 |
/\ (* outputaddrs base: |
| 1832 |
arrays: eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) *) |
| 1833 |
Forall2 |
| 1834 |
(fun idx base_reg_val => |
| 1835 |
match idx with |
| 1836 |
| inr base => eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) |
| 1837 |
| inl (inr base) => eval_idx_Z G' d' base (Z.land base_reg_val (Z.ones 64)) |
| 1838 |
| inl (inl r) => True |
| 1839 |
end) |
| 1840 |
outputaddrs |
| 1841 |
(List.firstn (List.length outputaddrs) runtime_reg) |
| 1842 |
/\ (* outputaddrs reg: *) |
| 1843 |
Forall2 |
| 1844 |
(fun idx r => |
| 1845 |
match idx with |
| 1846 |
| inl (inl r') => r = r' |
| 1847 |
| inl (inr _) | inr _ => True |
| 1848 |
end) |
| 1849 |
outputaddrs |
| 1850 |
(List.firstn (List.length outputaddrs) reg_available) |
| 1851 |
/\ Forall2 (fun addr idx => |
| 1852 |
match addr, idx with |
| 1853 |
| inl (idx, _), inl val_idx |
| 1854 |
=> if dereference_scalar |
| 1855 |
then True |
| 1856 |
else forall v, (0 <= v < 2^64)%Z -> eval_idx_Z G' d' idx v <-> eval_idx_Z G' d' val_idx v |
| 1857 |
| inr (_, ls), inr ls' => List.length ls = List.length ls' |
| 1858 |
| inl _, inr _ | inr _, inl _ => False |
| 1859 |
end) |
| 1860 |
outputaddrs' idxs |
| 1861 |
/\ Forall2 (fun addr idx => |
| 1862 |
match addr, idx with |
| 1863 |
| inl (inl _), inl (_, None) => dereference_scalar = false |
| 1864 |
| inl (inr addr), inl (_, Some addr') => dereference_scalar = true /\ addr = addr' |
| 1865 |
| inr _, inr _ => True |
| 1866 |
| inl (inl _), inl (_, Some _) | inl (inr _), inl (_, None) => False |
| 1867 |
| inl _, inr _ | inr _, inl _ => False |
| 1868 |
end) |
| 1869 |
outputaddrs outputaddrs') |
| 1870 |
/\ f = f') |
| 1871 |
/\ gensym_dag_ok G' d' |
| 1872 |
/\ (forall e n, eval G d e n -> eval G' d' e n) |
| 1873 |
/\ R frame G' s' ms |
| 1874 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true). |
| 1875 |
Proof. |
| 1876 |
eapply build_merge_base_addresses_ok in H; [ | try eassumption .. ]; [ | destruct s; apply HR .. ]; []. |
| 1877 |
destruct H as [G' H]; exists G'. |
| 1878 |
intuition idtac. |
| 1879 |
all: destruct_head'_ex; destruct_head'_and. |
| 1880 |
all: subst m m' f f' r r' d d'. |
| 1881 |
all: destruct s, s'; cbv [R] in *; cbn [Symbolic.dag_state Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state Symbolic.symbolic_reg_state] in *. |
| 1882 |
all: destruct_head'_and. |
| 1883 |
1: intuition idtac. |
| 1884 |
all: try solve [ subst; eapply R_flags_subsumed; eauto ]. |
| 1885 |
{ subst. |
| 1886 |
eapply R_regs_subsumed_get_reg_same_eval; |
| 1887 |
[ eapply R_regs_subsumed; now eauto | ]. |
| 1888 |
apply R_regs_preserved_fold_left_set_reg_index; [ reflexivity | ]. |
| 1889 |
eapply Forall2_R_regs_preserved_same_helper; try assumption. |
| 1890 |
all: eauto using R_regs_subsumed. |
| 1891 |
all: destruct_head'_and. |
| 1892 |
{ Foralls_to_nth_error; intuition idtac. } |
| 1893 |
{ intros. |
| 1894 |
match goal with |
| 1895 |
| [ H : (forall reg, Option.is_Some (Symbolic.get_reg ?s (reg_index reg)) = true) |
| 1896 |
|- context[Symbolic.get_reg ?s (reg_index ?ri)] ] |
| 1897 |
=> generalize (H ri); cbv [Option.is_Some]; break_innermost_match; |
| 1898 |
try congruence |
| 1899 |
end; eauto. } } |
| 1900 |
{ destruct ms; cbn [Semantics.machine_mem_state] in *. |
| 1901 |
subst. |
| 1902 |
lazymatch goal with |
| 1903 |
| [ H : R_mem ?frame _ _ ?ss ?m |- R_mem ?frame' ?G' ?d' ?ss' ?m ] |
| 1904 |
=> eapply R_mem_subsumed in H; |
| 1905 |
[ revert H; generalize m; |
| 1906 |
refine (_ : Lift1Prop.impl1 (R_mem frame G' d' ss) (R_mem frame' G' d' ss')) |
| 1907 |
| now eauto ] |
| 1908 |
end. |
| 1909 |
rewrite (R_mem_frame_cps_id I frame), (R_mem_frame_cps_id I frame'), R_mem_app_iff, Hframe. |
| 1910 |
rewrite R_mem_rev_iff, (R_mem_flat_map_ex_R_list_scalar_or_array_iff_emp (dereference_scalar:=dereference_scalar)). |
| 1911 |
all: [ > |
| 1912 |
| try first [ eassumption |
| 1913 |
| etransitivity; [ eassumption | saturate_lengths; congruence ] ] .. ]. |
| 1914 |
{ repeat rewrite ?SeparationLogic.sep_ex1_r, ?SeparationLogic.sep_ex1_l. |
| 1915 |
unshelve eapply Lift1Prop.impl1_ex1_r; try exact addr_vals. |
| 1916 |
handle_simple_R_mem. |
| 1917 |
rewrite !SeparationLogic.sep_assoc. |
| 1918 |
rewrite sep_emp_holds_l. |
| 1919 |
{ reflexivity. } |
| 1920 |
{ cbv [sep]; intros; repeat (destruct_head'_ex; destruct_head'_and). |
| 1921 |
intuition eauto using bounded_of_R_list_scalar_or_array. |
| 1922 |
eapply Forall2_weaken; [ | eassumption ]. |
| 1923 |
eauto using lift_eval_idx_or_list_idx_impl. } } |
| 1924 |
{ Foralls_to_nth_error. |
| 1925 |
all: discriminate. } |
| 1926 |
{ saturate_lengths. |
| 1927 |
adjust_Foralls_firstn_skipn. |
| 1928 |
repeat (revert Hreg_available_wide; Foralls_to_nth_error; intros). |
| 1929 |
all: cbv [eval_idx_or_list_idx] in *. |
| 1930 |
all: break_innermost_match_hyps; try now exfalso. |
| 1931 |
all: intros; handle_eval_eval; subst. |
| 1932 |
all: eauto using lift_eval_idx_Z_impl. } |
| 1933 |
{ cbn [Semantics.machine_reg_state] in *. |
| 1934 |
saturate_lengths. |
| 1935 |
progress adjust_Foralls_firstn_skipn. |
| 1936 |
Foralls_to_nth_error. |
| 1937 |
all: intuition try congruence. |
| 1938 |
split_iff. |
| 1939 |
repeat match goal with |
| 1940 |
| [ |- context[Semantics.get_reg ?m ?r] ] |
| 1941 |
=> unique pose proof (@get_reg_bounded m r) |
| 1942 |
end. |
| 1943 |
eauto using lift_eval_idx_Z_impl. } } |
| 1944 |
{ subst. |
| 1945 |
let H := fresh in |
| 1946 |
match goal with |
| 1947 |
| [ |- Option.is_Some (get_reg (fold_left ?f ?ls ?s) ?r) = true ] |
| 1948 |
=> unshelve (epose proof (reg_all_set_fold_left s [r] f ls _ _) as H; |
| 1949 |
inversion H; subst; assumption) |
| 1950 |
end. |
| 1951 |
all: cbn [List.map]; intros; invlist Forall; constructor; try solve [ constructor ]; eauto with nocore. |
| 1952 |
all: destruct_head'_prod. |
| 1953 |
all: rewrite get_reg_set_reg_full. |
| 1954 |
all: break_innermost_match; try reflexivity. |
| 1955 |
all: eauto with nocore. } |
| 1956 |
Qed. |
| 1957 |
|
| 1958 |
Lemma mapM_GetReg_ok_R_full {opts : symbolic_options_computed_opt} {descr:description} G regs idxs reg_vals s s' frame ms |
| 1959 |
(H : mapM GetReg regs s = Success (idxs, s')) |
| 1960 |
(d := s.(dag_state)) |
| 1961 |
(d' := s'.(dag_state)) |
| 1962 |
(r := s.(symbolic_reg_state)) |
| 1963 |
(r' := s'.(symbolic_reg_state)) |
| 1964 |
(f := s.(symbolic_flag_state)) |
| 1965 |
(f' := s'.(symbolic_flag_state)) |
| 1966 |
(m := s.(symbolic_mem_state)) |
| 1967 |
(m' := s'.(symbolic_mem_state)) |
| 1968 |
(Hwide : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) regs) |
| 1969 |
(Hregval : Forall2 (fun idx v => forall idx', idx = Some idx' -> eval_idx_Z G s idx' v) (List.map (get_reg r) (List.map reg_index regs)) reg_vals) |
| 1970 |
(Hbounded : Forall (fun v => (0 <= v < 2^64)%Z) reg_vals) |
| 1971 |
(Hregval_len : List.length regs = List.length reg_vals) |
| 1972 |
(HR : R frame G s ms) |
| 1973 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 1974 |
: ((exists reg_idxs, |
| 1975 |
List.map (get_reg r) (List.map reg_index regs) = List.map Some reg_idxs |
| 1976 |
/\ Forall2 (eval_idx_Z G s') reg_idxs reg_vals) |
| 1977 |
/\ Forall2 (eval_idx_Z G s') idxs reg_vals |
| 1978 |
/\ r = r' |
| 1979 |
/\ f = f' |
| 1980 |
/\ m = m') |
| 1981 |
/\ gensym_dag_ok G d' |
| 1982 |
/\ (forall e n, eval G d e n -> eval G d' e n) |
| 1983 |
/\ R frame G s' ms |
| 1984 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true). |
| 1985 |
Proof. |
| 1986 |
eapply mapM_GetReg_ok_bounded in H; [ | try eassumption .. ]; [ | destruct s; apply HR .. ]; []. |
| 1987 |
all: intuition idtac. |
| 1988 |
all: destruct s, s'; cbv [R Symbolic.symbolic_reg_state Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state] in *; intuition subst. |
| 1989 |
all: eauto using R_regs_subsumed, R_flags_subsumed, R_mem_subsumed. |
| 1990 |
Qed. |
| 1991 |
|
| 1992 |
Lemma mapM_GetReg_ok_R {opts : symbolic_options_computed_opt} {descr:description} G regs idxs s s' frame (ms : machine_state) |
| 1993 |
(H : mapM GetReg regs s = Success (idxs, s')) |
| 1994 |
(d := s.(dag_state)) |
| 1995 |
(d' := s'.(dag_state)) |
| 1996 |
(r := s.(symbolic_reg_state)) |
| 1997 |
(r' := s'.(symbolic_reg_state)) |
| 1998 |
(f := s.(symbolic_flag_state)) |
| 1999 |
(f' := s'.(symbolic_flag_state)) |
| 2000 |
(m := s.(symbolic_mem_state)) |
| 2001 |
(m' := s'.(symbolic_mem_state)) |
| 2002 |
(reg_vals := List.map (Semantics.get_reg ms) regs) |
| 2003 |
(Hwide : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) regs) |
| 2004 |
(HR : R frame G s ms) |
| 2005 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 2006 |
: ((exists reg_idxs, |
| 2007 |
List.map (get_reg r) (List.map reg_index regs) = List.map Some reg_idxs |
| 2008 |
/\ Forall2 (eval_idx_Z G s') reg_idxs reg_vals) |
| 2009 |
/\ Forall2 (eval_idx_Z G s') idxs reg_vals |
| 2010 |
/\ r = r' |
| 2011 |
/\ f = f' |
| 2012 |
/\ m = m') |
| 2013 |
/\ gensym_dag_ok G d' |
| 2014 |
/\ (forall e n, eval G d e n -> eval G d' e n) |
| 2015 |
/\ R frame G s' ms |
| 2016 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true). |
| 2017 |
Proof. |
| 2018 |
subst reg_vals. |
| 2019 |
eapply mapM_GetReg_ok_R_full in H; [ eassumption | try eassumption .. ]. |
| 2020 |
all: rewrite ?map_length. |
| 2021 |
all: intuition idtac. |
| 2022 |
all: try apply get_reg_bounded_Forall. |
| 2023 |
all: cbv [R] in HR. |
| 2024 |
all: try solve [ destruct s; apply HR ]. |
| 2025 |
all: try solve [ cbv [R] in HR; destruct s; eapply Forall2_get_reg_of_R_regs; try assumption; try apply HR ]. |
| 2026 |
Qed. |
| 2027 |
|
| 2028 |
Lemma SymexLines_ok_R {opts : symbolic_options_computed_opt} |
| 2029 |
frame G s m asm _tt s' |
| 2030 |
(d := s.(dag_state)) |
| 2031 |
(d' := s'.(dag_state)) |
| 2032 |
(r := s.(symbolic_reg_state)) |
| 2033 |
(r' := s'.(symbolic_reg_state)) |
| 2034 |
(H : Symbolic.SymexLines asm s = Success (_tt, s')) |
| 2035 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 2036 |
(HR : R frame G s m) |
| 2037 |
: exists m', |
| 2038 |
Semantics.DenoteLines m asm = Some m' |
| 2039 |
/\ gensym_dag_ok G d |
| 2040 |
/\ (forall e n, eval G d e n -> eval G d' e n) |
| 2041 |
/\ R frame G s' m' |
| 2042 |
/\ same_mem_addressed s s' |
| 2043 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true). |
| 2044 |
Proof. |
| 2045 |
destruct _tt. |
| 2046 |
pose proof H as H'; eapply SymexLines_mem_same in H'. |
| 2047 |
pose proof H as H''; eapply SymexLines_reg_same in H''. |
| 2048 |
cbv [same_reg_some] in *. |
| 2049 |
eapply SymexLines_R in H; [ | eassumption .. ]. |
| 2050 |
destruct H as [m' H]; exists m'. |
| 2051 |
intuition eauto with nocore. |
| 2052 |
all: cbv [R] in *. |
| 2053 |
all: try solve [ destruct s; apply HR ]. |
| 2054 |
Qed. |
| 2055 |
|
| 2056 |
Local Instance R_mem_Proper_Permutation_iff1 |
| 2057 |
: Proper (eq ==> eq ==> eq ==> @Permutation _ ==> Lift1Prop.iff1) R_mem | 10. |
| 2058 |
Proof. |
| 2059 |
cbv [Proper respectful]; intros; subst. |
| 2060 |
let H := match goal with H : Permutation _ _ |- _ => H end in |
| 2061 |
induction H; cbn [R_mem]; break_innermost_match. |
| 2062 |
all: repeat first [ reflexivity |
| 2063 |
| assumption |
| 2064 |
| progress (SeparationLogic.cancel; cbn [seps]) |
| 2065 |
| etransitivity; eassumption ]. |
| 2066 |
Qed. |
| 2067 |
|
| 2068 |
Local Instance R_mem_Proper_Permutation_iff |
| 2069 |
: Proper (eq ==> eq ==> eq ==> @Permutation _ ==> eq ==> iff) R_mem | 10. |
| 2070 |
Proof. |
| 2071 |
repeat intro; subst; eapply R_mem_Proper_Permutation_iff1; (reflexivity + eassumption). |
| 2072 |
Qed. |
| 2073 |
|
| 2074 |
Local Instance R_mem_Proper_Permutation_impl |
| 2075 |
: Proper (eq ==> eq ==> eq ==> @Permutation _ ==> eq ==> Basics.impl) R_mem | 10. |
| 2076 |
Proof. |
| 2077 |
repeat intro; subst; eapply R_mem_Proper_Permutation_iff; (idtac + symmetry); (eassumption + reflexivity). |
| 2078 |
Qed. |
| 2079 |
|
| 2080 |
Local Instance R_mem_Proper_Permutation_flip_impl |
| 2081 |
: Proper (eq ==> eq ==> eq ==> @Permutation _ ==> eq ==> Basics.flip Basics.impl) R_mem | 10. |
| 2082 |
Proof. |
| 2083 |
repeat intro; subst; eapply R_mem_Proper_Permutation_iff; (idtac + symmetry); (eassumption + reflexivity). |
| 2084 |
Qed. |
| 2085 |
|
| 2086 |
Local Instance R_mem_Proper_Permutation_impl1 |
| 2087 |
: Proper (eq ==> eq ==> eq ==> @Permutation _ ==> Lift1Prop.impl1) R_mem | 10. |
| 2088 |
Proof. |
| 2089 |
repeat intro; eapply R_mem_Proper_Permutation_impl; (reflexivity + eassumption). |
| 2090 |
Qed. |
| 2091 |
|
| 2092 |
Local Instance R_mem_Proper_Permutation_flip_impl1 |
| 2093 |
: Proper (eq ==> eq ==> eq ==> @Permutation _ ==> Basics.flip Lift1Prop.impl1) R_mem | 10. |
| 2094 |
Proof. |
| 2095 |
repeat intro; eapply R_mem_Proper_Permutation_flip_impl; (reflexivity + eassumption). |
| 2096 |
Qed. |
| 2097 |
|
| 2098 |
Lemma get_asm_reg_bounded s rs : Forall (fun v => (0 <= v < 2 ^ 64)%Z) (get_asm_reg s rs). |
| 2099 |
Proof. apply get_reg_bounded_Forall. Qed. |
| 2100 |
|
| 2101 |
Lemma LoadArray_ok_R {opts : symbolic_options_computed_opt} {descr:description} frame G s ms s' base base_val base_word_val len idxs |
| 2102 |
(H : LoadArray base len s = Success (idxs, s')) |
| 2103 |
(d := s.(dag_state)) |
| 2104 |
(d' := s'.(dag_state)) |
| 2105 |
(r := s.(symbolic_reg_state)) |
| 2106 |
(r' := s'.(symbolic_reg_state)) |
| 2107 |
(f := s.(symbolic_flag_state)) |
| 2108 |
(f' := s'.(symbolic_flag_state)) |
| 2109 |
(m := s.(symbolic_mem_state)) |
| 2110 |
(m' := s'.(symbolic_mem_state)) |
| 2111 |
(HR : R frame G s ms) |
| 2112 |
(Hbase : eval_idx_Z G d base (Z.land base_val (Z.ones 64))) |
| 2113 |
(Hbase_word_val : base_val = word.unsigned base_word_val) |
| 2114 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 2115 |
: ((exists (addrs : list idx), |
| 2116 |
Permutation m (List.combine addrs idxs ++ m') |
| 2117 |
/\ List.length idxs = len |
| 2118 |
/\ Forall2 (eval_idx_Z G d') addrs (List.map (fun i => Z.land (base_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 len)) |
| 2119 |
/\ Forall (fun addr => Forall (fun x => (fst x =? addr)%N = false) m') addrs) |
| 2120 |
/\ r = r' |
| 2121 |
/\ f = f') |
| 2122 |
/\ gensym_dag_ok G d' |
| 2123 |
/\ (forall e n, eval G d e n -> eval G d' e n) |
| 2124 |
/\ ((exists vals, |
| 2125 |
Forall2 (eval_idx_Z G d') idxs vals |
| 2126 |
/\ Forall (fun v => 0 <= v < 2^64)%Z vals |
| 2127 |
/\ R (frame * array cell64 (word.of_Z 8) base_word_val vals)%sep G s' ms) |
| 2128 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true)). |
| 2129 |
Proof. |
| 2130 |
replace (Z.land base_val (Z.ones 64)) with base_val in Hbase |
| 2131 |
by now (subst; rewrite Z.land_ones by lia; rewrite Z.mod_small by apply Properties.word.unsigned_range). |
| 2132 |
eapply LoadArray_ok in H; [ | try eassumption .. ]; [ | destruct s; apply HR .. ]. |
| 2133 |
repeat (destruct_head'_and; destruct_head'_ex). |
| 2134 |
subst r r'. |
| 2135 |
repeat (split; eauto 10 using ex_intro, conj with nocore; []). |
| 2136 |
split; try solve [ destruct s, s'; cbn [Symbolic.symbolic_reg_state] in *; subst; eauto ]; []. |
| 2137 |
move ms at bottom. |
| 2138 |
subst d d' f f' m m'. |
| 2139 |
destruct s, s'; cbv [R] in *; cbn [Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state Symbolic.symbolic_reg_state] in *; subst. |
| 2140 |
destruct_head'_and. |
| 2141 |
all: [ > ]. |
| 2142 |
match goal with |
| 2143 |
| [ H : Permutation ?s _, H' : R_mem _ _ _ ?s _ |- _ ] => is_var s; rewrite H in H' |
| 2144 |
end. |
| 2145 |
match goal with |
| 2146 |
| [ H : R_mem _ _ _ _ _ |- _ ] |
| 2147 |
=> eapply R_mem_subsumed in H; [ | eassumption ]; |
| 2148 |
let T := open_constr:(_) in |
| 2149 |
evar (R' : T); |
| 2150 |
let P := lazymatch type of H with ?P _ => P end in |
| 2151 |
let H' := fresh in |
| 2152 |
assert (H' : Lift1Prop.iff1 P R'); |
| 2153 |
[ |
| 2154 |
| hnf in H'; rewrite H' in H; subst R'; clear H' ] |
| 2155 |
end. |
| 2156 |
{ erewrite (R_mem_frame_cps_id I frame), R_mem_app_iff, R_mem_combine_ex_array_iff by (eassumption + reflexivity). |
| 2157 |
repeat rewrite ?SeparationLogic.sep_ex1_r, ?SeparationLogic.sep_ex1_l. |
| 2158 |
subst R'; reflexivity. } |
| 2159 |
cbv [Lift1Prop.ex1] in *; destruct_head'_ex. |
| 2160 |
match goal with |
| 2161 |
| [ H : sep _ _ _ |- _ ] |
| 2162 |
=> let T := open_constr:(_) in |
| 2163 |
evar (R' : T); |
| 2164 |
let P := lazymatch type of H with ?P _ => P end in |
| 2165 |
let H' := fresh in |
| 2166 |
assert (H' : Lift1Prop.impl1 P R'); |
| 2167 |
[ |
| 2168 |
| hnf in H'; apply H' in H; subst R'; clear H' ] |
| 2169 |
end. |
| 2170 |
{ repeat rewrite <- ?SeparationLogic.sep_assoc, ?SeparationLogic.sep_emp_emp, ?SeparationLogic.sep_comm_emp_r. |
| 2171 |
rewrite ?SeparationLogic.sep_assoc. |
| 2172 |
rewrite <- ?SeparationLogic.sep_comm_emp_r. |
| 2173 |
rewrite <- ?SeparationLogic.sep_assoc. |
| 2174 |
erewrite <- (R_mem_frame_cps_id I). |
| 2175 |
subst R'; reflexivity. } |
| 2176 |
destruct_head' (@sep). |
| 2177 |
cbv [emp] in *. |
| 2178 |
destruct_head'_ex; destruct_head'_and. |
| 2179 |
subst. |
| 2180 |
rewrite @Properties.map.split_empty_r in * by typeclasses eauto. |
| 2181 |
subst. |
| 2182 |
rewrite @firstn_all in * by reflexivity. |
| 2183 |
eexists; repeat (apply conj; eauto using R_flags_subsumed, R_regs_subsumed; []). |
| 2184 |
match goal with |
| 2185 |
| [ H : context[array _ _ ?n] |- context[array _ _ ?n'] ] |
| 2186 |
=> replace n with n' in H by ZnWords |
| 2187 |
end. |
| 2188 |
assumption. |
| 2189 |
Qed. |
| 2190 |
|
| 2191 |
Lemma R_mem_flat_map_no_opt_ex_R_list_scalar_or_array_iff_emp {dereference_scalar:bool} G d |
| 2192 |
: forall (addr_idxs : list (idx + list idx)) base_vals base_vals_words val_idxs |
| 2193 |
(Haddr_idxs : Forall2 (fun idx' v |
| 2194 |
=> let addrs_vals_of := fun base_reg_val addrs' => List.map (fun i => Z.land (base_reg_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 (List.length addrs')) in |
| 2195 |
match idx' with |
| 2196 |
| inl idx' |
| 2197 |
=> eval_idx_Z G d idx' v |
| 2198 |
| inr addrs' |
| 2199 |
=> Forall2 (eval_idx_Z G d) addrs' (addrs_vals_of v addrs') |
| 2200 |
end) |
| 2201 |
addr_idxs base_vals) |
| 2202 |
(Hidxs_array : Forall2 (fun idx val_idx |
| 2203 |
=> match idx, val_idx with |
| 2204 |
| inl idx, inl val_idx |
| 2205 |
=> if dereference_scalar |
| 2206 |
then True |
| 2207 |
else forall v, (0 <= v < 2^64)%Z -> eval_idx_Z G d idx v <-> eval_idx_Z G d val_idx v |
| 2208 |
| inr idxs, inr val_idxs |
| 2209 |
=> List.length idxs = List.length val_idxs |
| 2210 |
| inl _, inr _ | inr _, inl _ => False |
| 2211 |
end) |
| 2212 |
addr_idxs val_idxs) |
| 2213 |
(*(Haddr_vals : Forall2 (fun idx v |
| 2214 |
=> match idx with |
| 2215 |
| inl base => TODO2 (dereference_scalar, base, v) /\ if dereference_scalar then eval_idx_Z G d base v else True |
| 2216 |
| _ => True |
| 2217 |
end) |
| 2218 |
val_idxs base_vals)*) |
| 2219 |
(Hbase_vals_words : List.map word.unsigned base_vals_words = base_vals), |
| 2220 |
Lift1Prop.iff1 |
| 2221 |
(R_mem (emp True) G d |
| 2222 |
(List.flat_map |
| 2223 |
(fun '(idx', idx) |
| 2224 |
=> match idx', idx with |
| 2225 |
| inl addr_or_val, inl val => if dereference_scalar then [(addr_or_val, val)] else [] |
| 2226 |
| inr addrs', inr items |
| 2227 |
=> List.combine addrs' items |
| 2228 |
| _, _ => [] |
| 2229 |
end) |
| 2230 |
(List.combine addr_idxs val_idxs))) |
| 2231 |
(Lift1Prop.ex1 |
| 2232 |
(fun vals |
| 2233 |
=> emp (Forall2 (eval_idx_or_list_idx G d) val_idxs vals |
| 2234 |
/\ Forall (fun v => match v with |
| 2235 |
| inl v => 0 <= v < 2^64 |
| 2236 |
| inr vs => Forall (fun v => 0 <= v < 2^64) vs |
| 2237 |
end%Z) vals) |
| 2238 |
* R_list_scalar_or_array (dereference_scalar:=dereference_scalar) vals base_vals_words))%sep. |
| 2239 |
Proof. |
| 2240 |
pose dereference_scalar as dereference_scalar'. |
| 2241 |
induction addr_idxs as [|addr_idx addr_idxs IH], |
| 2242 |
base_vals as [|base_val base_vals], |
| 2243 |
base_vals_words as [|base_vals_word base_vals_words], |
| 2244 |
val_idxs as [|val_idx val_idxs]; |
| 2245 |
try specialize (IH base_vals base_vals_words val_idxs); |
| 2246 |
do 2 (intro H; inversion H; clear H); subst. |
| 2247 |
all: cbv [R_list_scalar_or_array R_list_scalar_or_array_nolen] in *; cbn [List.map flat_map R_mem fold_right List.combine List.length] in *; intros; inversion_nat_eq; inversion_list. |
| 2248 |
all: SeparationLogic.cancel; cbn [seps]. |
| 2249 |
all: try (rewrite R_mem_app_iff, IH by assumption; clear IH). |
| 2250 |
{ intro x; split; revert x; refine (_ : Lift1Prop.impl1 _ _). |
| 2251 |
all: [ > apply Lift1Prop.impl1_ex1_r with (x:=nil) | apply Lift1Prop.impl1_ex1_l; intro ]. |
| 2252 |
all: cbn [List.length]. |
| 2253 |
all: rewrite Forall2_nil_l_iff. |
| 2254 |
all: rewrite ?Forall_nil_iff. |
| 2255 |
all: rewrite <- ?SeparationLogic.sep_assoc, ?SeparationLogic.sep_emp_emp. |
| 2256 |
all: try (apply SeparationLogic.impl1_r_sep_emp; split; trivial). |
| 2257 |
all: try apply SeparationLogic.impl1_l_sep_emp; intros; destruct_head'_and; subst. |
| 2258 |
all: cbn [List.combine List.map fold_right]. |
| 2259 |
all: eauto; try reflexivity. } |
| 2260 |
all: [ > ]. |
| 2261 |
all: intro x; split; revert x; refine (_ : Lift1Prop.impl1 _ _). |
| 2262 |
all: repeat |
| 2263 |
repeat |
| 2264 |
first [ progress intros |
| 2265 |
| exfalso; assumption |
| 2266 |
| progress inversion_option |
| 2267 |
| progress destruct_head'_ex |
| 2268 |
| progress destruct_head'_and |
| 2269 |
| progress subst |
| 2270 |
| progress cbn [List.combine List.map fold_right R_scalar_or_array R_mem List.length List.app Option.is_Some] in * |
| 2271 |
| progress cbv [option_eq] in * |
| 2272 |
| rewrite SeparationLogic.sep_emp_emp |
| 2273 |
| rewrite SeparationLogic.sep_ex1_l |
| 2274 |
| rewrite SeparationLogic.sep_ex1_r |
| 2275 |
| rewrite R_mem_app_iff |
| 2276 |
(*| let P := lazymatch goal with |- Lift1Prop.impl1 ?P _ => P end in |
| 2277 |
lazymatch P with |
| 2278 |
| context[sep (Lift1Prop.ex1 ?q) ?p] |
| 2279 |
=> rewrite (SeparationLogic.sep_ex1_l p q) |
| 2280 |
| context[sep ?p (Lift1Prop.ex1 ?q)] |
| 2281 |
=> rewrite (SeparationLogic.sep_ex1_r p q) |
| 2282 |
end*) |
| 2283 |
| rewrite SeparationLogic.impl1_l_sep_emp |
| 2284 |
| rewrite Lift1Prop.impl1_ex1_l |
| 2285 |
| rewrite !SeparationLogic.sep_assoc, SeparationLogic.impl1_l_sep_emp |
| 2286 |
| rewrite <- !SeparationLogic.sep_assoc |
| 2287 |
| rewrite !SeparationLogic.sep_comm_emp_r |
| 2288 |
| rewrite Forall2_cons_cons_iff in * |
| 2289 |
| set_evars; rewrite Forall_cons_iff in *; subst_evars |
| 2290 |
| rewrite Forall2_cons_l_ex_iff in * |- |
| 2291 |
| rewrite Forall2_cons_r_ex_iff in * |- |
| 2292 |
| rewrite R_mem_combine_array_impl1 by (eassumption + reflexivity) |
| 2293 |
| rewrite R_cell64_ex_cell64_iff |
| 2294 |
| progress specialize_by_assumption |
| 2295 |
| progress specialize_by reflexivity |
| 2296 |
| match goal with |
| 2297 |
| [ H : Lift1Prop.iff1 _ _ |- _ ] => rewrite H; clear H |
| 2298 |
| [ H : Forall2 _ ?addr_idxs ?v |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun h => sep _ (sep (emp (Forall2 _ ?addr_idxs h /\ _)) _))) ] |
| 2299 |
=> apply (Lift1Prop.impl1_ex1_r _ _ v) |
| 2300 |
| [ |- Lift1Prop.impl1 ?A (Lift1Prop.ex1 ?B0) ] |
| 2301 |
=> lazymatch B0 with |
| 2302 |
| fun h => sep (emp (Forall2 ?R (?addr_idx :: ?addr_idxs) h /\ _)) (@?B h) |
| 2303 |
=> lazymatch goal with |
| 2304 |
| [ H : Forall2 _ addr_idxs ?rest |- _ ] |
| 2305 |
=> cut (Lift1Prop.impl1 A (Lift1Prop.ex1 (fun h0 => Lift1Prop.ex1 (fun h1 => Lift1Prop.ex1 (fun h2 => B0 (match addr_idx with inl _ => inl (if dereference_scalar' then h0 else h1) | inr _ => inr h2 end :: rest)))))); |
| 2306 |
[ (intros ->); |
| 2307 |
let h0 := fresh in |
| 2308 |
let h1 := fresh in |
| 2309 |
let h2 := fresh in |
| 2310 |
rewrite Lift1Prop.impl1_ex1_l; intro h0; |
| 2311 |
rewrite Lift1Prop.impl1_ex1_l; intro h1; |
| 2312 |
rewrite Lift1Prop.impl1_ex1_l; intro h2; |
| 2313 |
apply (Lift1Prop.impl1_ex1_r _ _ (match addr_idx with inl _ => inl (if dereference_scalar' then h0 else h1) | inr _ => inr h2 end :: rest)); |
| 2314 |
reflexivity |
| 2315 |
| break_innermost_match; break_innermost_match_hyps; cbn [R_mem] ] |
| 2316 |
end |
| 2317 |
end |
| 2318 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun _ => ?A)) ] |
| 2319 |
=> unshelve eapply Lift1Prop.impl1_ex1_r; try solve [ constructor ]; [] |
| 2320 |
| [ H : eval_idx_or_list_idx _ _ (inr _) _ |- _ ] |
| 2321 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 2322 |
| [ H : eval_idx_or_list_idx _ _ (inl _) _ |- _ ] |
| 2323 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 2324 |
| [ H : eval_idx_or_list_idx _ _ _ (inr _) |- _ ] |
| 2325 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 2326 |
| [ H : eval_idx_or_list_idx _ _ _ (inl _) |- _ ] |
| 2327 |
=> cbv [eval_idx_or_list_idx] in H; break_innermost_match_hyps |
| 2328 |
end |
| 2329 |
| progress break_innermost_match |
| 2330 |
| match goal with |
| 2331 |
| [ |- Lift1Prop.impl1 ?A ?B ] |
| 2332 |
=> lazymatch B with |
| 2333 |
| context[R_mem _ _ _ (List.combine _ _)] |
| 2334 |
=> rewrite R_mem_combine_array_iff |
| 2335 |
by solve [ eassumption | reflexivity | saturate_lengths; congruence ] |
| 2336 |
end |
| 2337 |
| [ |- Lift1Prop.impl1 _ ?B ] |
| 2338 |
=> lazymatch B with |
| 2339 |
| context[sep (emp _) _] |
| 2340 |
=> rewrite ?SeparationLogic.sep_assoc; |
| 2341 |
apply SeparationLogic.impl1_r_sep_emp; split; [ solve [ eauto ] | ] |
| 2342 |
end |
| 2343 |
end |
| 2344 |
| reflexivity |
| 2345 |
| break_innermost_match_hyps_step ]. |
| 2346 |
all: repeat first [ rewrite Z.land_ones in * by lia |
| 2347 |
| progress subst |
| 2348 |
| match goal with |
| 2349 |
| [ H1 : eval_idx_Z ?G ?d ?i _, H2 : eval_idx_Z ?G ?d ?i _ |- _ ] |
| 2350 |
=> eapply eval_eval in H2; [ | exact H1 ] |
| 2351 |
| [ H : context[(?v mod ?m)%Z] |- _ ] |
| 2352 |
=> lazymatch v with |
| 2353 |
| word.unsigned ?x |
| 2354 |
=> replace ((v mod m)%Z) with v in * by ZnWords |
| 2355 |
end |
| 2356 |
| [ H : word.unsigned _ = word.unsigned _ |- _ ] |
| 2357 |
=> apply Properties.word.unsigned_inj in H |
| 2358 |
| [ H : context[firstn ?n ?l] |- _ ] |
| 2359 |
=> rewrite firstn_all in H by congruence |
| 2360 |
| [ |- context[?v] ] |
| 2361 |
=> lazymatch v with |
| 2362 |
| word.add ?x (word.of_Z 0) |
| 2363 |
=> replace v with x by ZnWords |
| 2364 |
end |
| 2365 |
end |
| 2366 |
| progress autorewrite with zsimplify_const ]. |
| 2367 |
all: repeat match goal with |
| 2368 |
| [ H : context[word.unsigned ?x] |- _ ] |
| 2369 |
=> unique pose proof (Properties.word.unsigned_range x) |
| 2370 |
| [ |- context[word.unsigned ?x] ] |
| 2371 |
=> unique pose proof (Properties.word.unsigned_range x) |
| 2372 |
end. |
| 2373 |
all: repeat first [ progress split_iff |
| 2374 |
| match goal with |
| 2375 |
| [ H' : eval_idx_Z _ _ ?i ?v, H'' : (0 <= ?v < _)%Z, H : forall v', (0 <= v' < _)%Z -> eval_idx_Z _ _ ?i v' -> eval_idx_Z _ _ _ _ |- _ ] |
| 2376 |
=> specialize (H _ H'' H') |
| 2377 |
end ]. |
| 2378 |
all: repeat first [ match goal with |
| 2379 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 (fun _ => ?A)) ] |
| 2380 |
=> unshelve eapply Lift1Prop.impl1_ex1_r; try solve [ constructor ]; [] |
| 2381 |
| [ |- Lift1Prop.impl1 _ (Lift1Prop.ex1 _) ] |
| 2382 |
=> eapply Lift1Prop.impl1_ex1_r |
| 2383 |
| [ |- Lift1Prop.impl1 _ ?B ] |
| 2384 |
=> lazymatch B with |
| 2385 |
| context[sep (emp _) _] |
| 2386 |
=> rewrite ?SeparationLogic.sep_assoc; |
| 2387 |
apply SeparationLogic.impl1_r_sep_emp; split; [ solve [ eauto 10 ] | ] |
| 2388 |
end |
| 2389 |
end |
| 2390 |
| reflexivity |
| 2391 |
| rewrite SeparationLogic.sep_ex1_l |
| 2392 |
| rewrite <- !SeparationLogic.sep_assoc |
| 2393 |
| rewrite SeparationLogic.sep_emp_emp |
| 2394 |
| rewrite Forall2_cons_cons_iff |
| 2395 |
| set_evars; rewrite Forall_cons_iff; subst_evars |
| 2396 |
| progress cbn [eval_idx_or_list_idx List.length] in * |
| 2397 |
| apply SeparationLogic.impl1_r_sep_emp; split; [ | reflexivity ] ]. |
| 2398 |
Qed. |
| 2399 |
|
| 2400 |
Lemma LoadOutputs_ok_R {opts : symbolic_options_computed_opt} {descr:description} {dereference_scalar:bool} frame G s ms s' outputaddrs output_types output_vals idxs |
| 2401 |
(H : LoadOutputs (dereference_scalar:=dereference_scalar) outputaddrs output_types s = Success (Success idxs, s')) |
| 2402 |
(d := s.(dag_state)) |
| 2403 |
(d' := s'.(dag_state)) |
| 2404 |
(r := s.(symbolic_reg_state)) |
| 2405 |
(r' := s'.(symbolic_reg_state)) |
| 2406 |
(f := s.(symbolic_flag_state)) |
| 2407 |
(f' := s'.(symbolic_flag_state)) |
| 2408 |
(m := s.(symbolic_mem_state)) |
| 2409 |
(m' := s'.(symbolic_mem_state)) |
| 2410 |
(HR : R frame G s ms) |
| 2411 |
(Houtputaddrs : Forall2 (fun idx val |
| 2412 |
=> match idx with |
| 2413 |
| inl (inr base) | inr base => eval_idx_Z G d base (Z.land val (Z.ones 64)) |
| 2414 |
| inl (inl _) |
| 2415 |
=> True |
| 2416 |
end) outputaddrs output_vals) |
| 2417 |
(Houtputaddrs_reg : Forall (fun idx |
| 2418 |
=> match idx with |
| 2419 |
| inl (inl r) |
| 2420 |
=> (let '(rn, lo, sz) := index_and_shift_and_bitcount_of_reg r in sz = 64%N) |
| 2421 |
/\ dereference_scalar = false |
| 2422 |
| inl (inr _) |
| 2423 |
=> dereference_scalar = true |
| 2424 |
| _ => True |
| 2425 |
end) outputaddrs) |
| 2426 |
(Hreg_good : forall reg, Option.is_Some (Symbolic.get_reg r (reg_index reg)) = true) |
| 2427 |
: (exists output_vals_words (output_vals' : list Z) (outputaddrs' : list (idx + list idx)) vals, |
| 2428 |
output_vals' = List.map word.unsigned output_vals_words |
| 2429 |
/\ Forall (fun v => (0 <= v < 2^64)%Z) output_vals' |
| 2430 |
/\ List.length output_vals = List.length output_vals' |
| 2431 |
/\ (Forall2 (fun idxs '((base, len), (base_val, base_val')) |
| 2432 |
=> match idxs, base, len with |
| 2433 |
| inr idxs, inr base, Some len |
| 2434 |
=> Forall2 (eval_idx_Z G d') idxs (List.map (fun i => Z.land (base_val + 8 * Z.of_nat i) (Z.ones 64)) (seq 0 len)) |
| 2435 |
/\ Z.land base_val (Z.ones 64) = base_val' |
| 2436 |
| inl idx, inl (inl r), None |
| 2437 |
=> (exists idx', |
| 2438 |
get_reg s' (reg_index r) = Some idx' |
| 2439 |
/\ eval_idx_Z G d' idx' base_val') |
| 2440 |
/\ eval_idx_Z G d' idx base_val' |
| 2441 |
/\ dereference_scalar = false |
| 2442 |
| inl idx, inl (inr addr), None |
| 2443 |
=> idx = addr |
| 2444 |
/\ eval_idx_Z G d' addr base_val' |
| 2445 |
/\ dereference_scalar = true |
| 2446 |
| _, _, _ => False |
| 2447 |
end) |
| 2448 |
outputaddrs' |
| 2449 |
(List.combine (List.combine outputaddrs output_types) (List.combine output_vals output_vals')) |
| 2450 |
/\ Permutation m (List.flat_map |
| 2451 |
(fun '(addrs, idxs) |
| 2452 |
=> match addrs, idxs with |
| 2453 |
| inr addrs, inr idxs |
| 2454 |
=> List.combine addrs idxs |
| 2455 |
| inl addr, inl idx |
| 2456 |
=> if dereference_scalar |
| 2457 |
then [(addr, idx)] |
| 2458 |
else [] |
| 2459 |
| inl _, inr _ | inr _, inl _ => [] |
| 2460 |
end) |
| 2461 |
(List.combine outputaddrs' idxs) |
| 2462 |
++ m') |
| 2463 |
/\ Forall2 |
| 2464 |
(fun addrs idxs |
| 2465 |
=> match addrs, idxs with |
| 2466 |
| inl idx, inl val_idx |
| 2467 |
=> if dereference_scalar |
| 2468 |
then True |
| 2469 |
else forall v, (0 <= v < 2^64)%Z -> eval_idx_Z G d' idx v <-> eval_idx_Z G d' val_idx v |
| 2470 |
| inr addrs, inr idxs |
| 2471 |
=> List.length idxs = List.length addrs |
| 2472 |
| inl _, inr _ | inr _, inl _ => False |
| 2473 |
end) |
| 2474 |
outputaddrs' |
| 2475 |
idxs |
| 2476 |
/\ Forall (fun addrs => match addrs with |
| 2477 |
| inl idx => True |
| 2478 |
| inr addrs |
| 2479 |
=> Forall (fun addr => Forall (fun x => (fst x =? addr)%N = false) m') addrs |
| 2480 |
end) |
| 2481 |
outputaddrs' |
| 2482 |
/\ List.length output_types = List.length outputaddrs) |
| 2483 |
/\ (Forall2 (eval_idx_or_list_idx G d') idxs vals |
| 2484 |
/\ Forall (fun v => match v with |
| 2485 |
| inl v => (0 <= v < 2^64)%Z |
| 2486 |
| inr vs => Forall (fun v => (0 <= v < 2^64)%Z) vs |
| 2487 |
end) vals |
| 2488 |
/\ R (frame * R_list_scalar_or_array (dereference_scalar:=dereference_scalar) vals output_vals_words)%sep G s' ms)) |
| 2489 |
/\ r = r' |
| 2490 |
/\ f = f' |
| 2491 |
/\ gensym_dag_ok G d' |
| 2492 |
/\ (forall e n, eval G d e n -> eval G d' e n) |
| 2493 |
/\ (forall reg, Option.is_Some (Symbolic.get_reg r' (reg_index reg)) = true). |
| 2494 |
Proof. |
| 2495 |
pose dereference_scalar as dereference_scalar'. |
| 2496 |
pose proof (fun (*i*) rv (*(H : nth_error outputaddrs i = Some (inl (inl rv)))*) H1 => @Forall2_get_reg_of_R_regs G d r ms [rv] (Forall_cons _ H1 (Forall_nil _)) ltac:(destruct s; apply HR)). |
| 2497 |
pose proof (fun i rv (H : nth_error outputaddrs i = Some (inl (inl rv))) H1 => @Forall2_get_reg_of_R_regs G d r ms [rv] (Forall_cons _ H1 (Forall_nil _)) ltac:(destruct s; apply HR)) as Houtputaddrs_reg_nth_error. |
| 2498 |
eapply LoadOutputs_ok in H; [ | try eassumption .. ]; [ | try (destruct s; apply HR) .. ]. |
| 2499 |
all: [ > |
| 2500 |
| lazymatch goal with |
| 2501 |
| [ |- Forall _ _ ] |
| 2502 |
=> Foralls_to_nth_error; |
| 2503 |
repeat first [ break_innermost_match_hyps_step |
| 2504 |
| progress destruct_head'_and |
| 2505 |
| progress intros |
| 2506 |
| progress inversion_pair |
| 2507 |
| progress subst |
| 2508 |
| progress specialize_by reflexivity |
| 2509 |
| progress cbn [List.map] in * |
| 2510 |
| progress invlist Forall2 |
| 2511 |
| match goal with |
| 2512 |
| [ H : forall x, Some _ = Some _ -> _ |- _ ] => specialize (H _ eq_refl) |
| 2513 |
| [ H : context[eval_idx_Z _ _ _ (Semantics.get_reg ?m ?r)] |- _ ] |
| 2514 |
=> replace (Semantics.get_reg m r) with (Z.land (Semantics.get_reg m r) (Z.ones 64)) in H |
| 2515 |
by now rewrite Z.land_ones by lia; rewrite Z.mod_small by now apply get_reg_bounded |
| 2516 |
end |
| 2517 |
| solve [ eauto ] |
| 2518 |
| solve [ repeat (intuition (inversion_option; subst; eauto) || esplit) ] ] |
| 2519 |
| [ |- Forall2 _ _ _ ] |
| 2520 |
=> eapply Forall2_weaken; [ | eassumption ]; cbv beta; intros *; break_innermost_match; eauto |
| 2521 |
end .. ]. |
| 2522 |
all: [ > ]. |
| 2523 |
subst r r' d d' f f' m m'. |
| 2524 |
cbv [R] in *. |
| 2525 |
destruct_head' symbolic_state; cbn [Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state Symbolic.symbolic_reg_state] in *. |
| 2526 |
repeat (destruct_head'_and; destruct_head'_ex; subst). |
| 2527 |
repeat (apply conj; eauto; []). |
| 2528 |
match goal with |
| 2529 |
| [ H : Permutation ?s _, H' : R_mem _ _ _ ?s _ |- _ ] => is_var s; rewrite H in H'; setoid_rewrite H; clear s H |
| 2530 |
end. |
| 2531 |
setoid_rewrite (R_mem_frame_cps_id I (sep frame _) G _ _ ms). |
| 2532 |
let H := match goal with H : R_mem _ _ _ _ _ |- _ => H end in |
| 2533 |
let P := lazymatch type of H with ?P _ => P end in |
| 2534 |
let Q := open_constr:(_) in |
| 2535 |
assert (H' : Lift1Prop.impl1 P Q); [ | apply H' in H; clear H' ]; |
| 2536 |
move H at bottom. |
| 2537 |
{ rewrite (R_mem_frame_cps_id I frame), R_mem_app_iff, <- SeparationLogic.sep_assoc. |
| 2538 |
reflexivity. } |
| 2539 |
cbv [sep R_list_scalar_or_array] in *. |
| 2540 |
repeat (destruct_head'_and; destruct_head'_ex). |
| 2541 |
match goal with |
| 2542 |
| [ |- exists (a : ?A) b c (d : ?D), @?P a b c d ] |
| 2543 |
=> let Q := open_constr:(_ /\ exists (a : A) (d : D), _) in |
| 2544 |
cut Q |
| 2545 |
end. |
| 2546 |
{ intros [H'0 [output_vals_words [vals H'1] ] ]. |
| 2547 |
eexists output_vals_words, (List.map word.unsigned output_vals_words), _, vals. |
| 2548 |
repeat match goal with |- _ /\ _ => split | |- ex _ => esplit end. |
| 2549 |
all: lazymatch goal with |
| 2550 |
| [ |- List.length _ = List.length (List.map _ _) ] |
| 2551 |
=> rewrite map_length |
| 2552 |
| [ |- Forall (fun v => (0 <= v < _)%Z) (List.map word.unsigned _) ] |
| 2553 |
=> rewrite Forall_map_iff, Forall_forall; intros; ZnWords |
| 2554 |
| [ |- Permutation _ _ ] => reflexivity |
| 2555 |
| [ |- ?x = ?x ] => reflexivity |
| 2556 |
| [ |- ?x = ?y ] |
| 2557 |
=> first [ is_evar x; tryif has_evar y then fail 1 else reflexivity |
| 2558 |
| idtac ] |
| 2559 |
| _ => idtac |
| 2560 |
end. |
| 2561 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2562 |
repeat first [ match type of H'1 with |
| 2563 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2564 |
end |
| 2565 |
| match type of H'0 with |
| 2566 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2567 |
end ]. |
| 2568 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2569 |
repeat first [ match type of H'1 with |
| 2570 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2571 |
end |
| 2572 |
| match type of H'0 with |
| 2573 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2574 |
end ]. |
| 2575 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2576 |
repeat first [ match type of H'1 with |
| 2577 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2578 |
end |
| 2579 |
| match type of H'0 with |
| 2580 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2581 |
end ]. |
| 2582 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2583 |
repeat first [ match type of H'1 with |
| 2584 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2585 |
end |
| 2586 |
| match type of H'0 with |
| 2587 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2588 |
end ]. |
| 2589 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2590 |
repeat first [ match type of H'1 with |
| 2591 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2592 |
end |
| 2593 |
| match type of H'0 with |
| 2594 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2595 |
end ]. |
| 2596 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2597 |
repeat first [ match type of H'1 with |
| 2598 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2599 |
end |
| 2600 |
| match type of H'0 with |
| 2601 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2602 |
end ]. |
| 2603 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2604 |
repeat first [ match type of H'1 with |
| 2605 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2606 |
end |
| 2607 |
| match type of H'0 with |
| 2608 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2609 |
end ]. |
| 2610 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2611 |
repeat first [ match type of H'1 with |
| 2612 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2613 |
end |
| 2614 |
| match type of H'0 with |
| 2615 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2616 |
end ]. |
| 2617 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2618 |
repeat first [ match type of H'1 with |
| 2619 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2620 |
end |
| 2621 |
| match type of H'0 with |
| 2622 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2623 |
end ]. |
| 2624 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2625 |
repeat first [ match type of H'1 with |
| 2626 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2627 |
end |
| 2628 |
| match type of H'0 with |
| 2629 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2630 |
end ]. |
| 2631 |
all: match goal with |
| 2632 |
| [ H : R_mem _ _ _ _ _ |- R_mem _ _ _ _ _ ] => eapply R_mem_subsumed; [ exact H | auto ] |
| 2633 |
| [ H : ?frame ?x |- ?frame ?ev ] => is_var frame; is_evar ev; is_var x; exact H |
| 2634 |
| [ H : R_mem _ _ _ (flat_map _ (List.combine _ _)) ?m |- R_list_scalar_or_array_nolen _ _ ?m' ] |
| 2635 |
=> unify m m' |
| 2636 |
| _ => cbv [emp] |
| 2637 |
end. |
| 2638 |
all: repeat match goal with |
| 2639 |
| [ H : ?T |- map.split _ _ _ ] => is_evar T; clear H |
| 2640 |
end. |
| 2641 |
all: lazymatch goal with |
| 2642 |
| [ |- map.split _ _ _ ] => try eassumption |
| 2643 |
| [ |- ?x = map.empty /\ _ ] => is_evar x; split; [ reflexivity | ] |
| 2644 |
| _ => idtac |
| 2645 |
end. |
| 2646 |
all: lazymatch goal with |
| 2647 |
| [ |- map.split _ map.empty _ ] => rewrite Properties.map.split_empty_l; try reflexivity |
| 2648 |
| _ => idtac |
| 2649 |
end. |
| 2650 |
all: [ > lazymatch goal with |- List.length _ = List.length _ => idtac end | lazymatch goal with |- R_list_scalar_or_array_nolen _ _ _ => idtac end ]. |
| 2651 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2652 |
repeat first [ match type of H'1 with |
| 2653 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2654 |
end |
| 2655 |
| match type of H'0 with |
| 2656 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2657 |
end ]. |
| 2658 |
all: [ > first [ refine (proj1 H'0) | refine (proj1 H'1) ] | .. ]; shelve_unifiable; |
| 2659 |
repeat first [ match type of H'1 with |
| 2660 |
| _ /\ _ => destruct H'1 as [? H'1] |
| 2661 |
end |
| 2662 |
| match type of H'0 with |
| 2663 |
| _ /\ _ => destruct H'0 as [? H'0] |
| 2664 |
end ]. |
| 2665 |
Unshelve. |
| 2666 |
all: exact True. } |
| 2667 |
split. |
| 2668 |
{ intuition eauto using R_regs_subsumed, R_flags_subsumed. |
| 2669 |
all: [ > ]. |
| 2670 |
Foralls_to_nth_error. |
| 2671 |
all: intros; repeat (destruct_head'_ex; destruct_head'_and). |
| 2672 |
all: split; intros. |
| 2673 |
all: handle_eval_eval; subst; eauto. } |
| 2674 |
clear Houtputaddrs_reg_nth_error. |
| 2675 |
repeat match goal with |
| 2676 |
| [ m : @map.rep _ _ Semantics.mem_state |- _ ] => clear dependent m |
| 2677 |
| [ m : Symbolic.mem_state |- _ ] => clear dependent m |
| 2678 |
end. |
| 2679 |
exactly_once multimatch goal with |
| 2680 |
| [ m : @map.rep _ _ Semantics.mem_state |- _ ] => revert dependent m |
| 2681 |
end. |
| 2682 |
intro m; revert m. |
| 2683 |
lazymatch goal with |
| 2684 |
| [ |- context[R_mem _ _ _ (flat_map _ (List.combine ?addrs _))] ] |
| 2685 |
=> is_var addrs; revert dependent addrs |
| 2686 |
end. |
| 2687 |
intro outputaddrs'. |
| 2688 |
revert dependent output_types; intro. |
| 2689 |
revert dependent outputaddrs; intro. |
| 2690 |
revert dependent idxs; intro. |
| 2691 |
revert dependent output_vals; intro. |
| 2692 |
revert outputaddrs' outputaddrs output_types output_vals idxs. |
| 2693 |
cbv [R_list_scalar_or_array_nolen]. |
| 2694 |
induction outputaddrs' as [|outputaddr' outputaddrs' IH], |
| 2695 |
outputaddrs as [|outputaddr outputaddrs], |
| 2696 |
output_types as [|output_type output_types], |
| 2697 |
output_vals as [|output_val output_vals], |
| 2698 |
idxs as [|idx idxs]; |
| 2699 |
try specialize (IH outputaddrs output_types output_vals idxs); |
| 2700 |
cbn [List.combine List.flat_map List.length List.map R_mem fold_right]; |
| 2701 |
intros; inversion_nat_eq; invlist Forall2; invlist Forall. |
| 2702 |
{ exists nil, nil; eauto 10. } |
| 2703 |
all: [ > ]. |
| 2704 |
specialize_by_assumption. |
| 2705 |
let H := match goal with H : R_mem _ _ _ _ _ |- _ => H end in |
| 2706 |
rewrite (R_mem_app_iff _ _ _ _ _ _) in H; cbv [sep] in H. |
| 2707 |
repeat (destruct_head'_ex; destruct_head'_and). |
| 2708 |
lazymatch type of IH with |
| 2709 |
| forall m, R_mem _ _ _ (flat_map _ (List.combine _ _)) m -> _ => idtac |
| 2710 |
end. |
| 2711 |
specialize (IH _ ltac:(eassumption)). |
| 2712 |
lazymatch type of IH with |
| 2713 |
| ex _ => idtac |
| 2714 |
end. |
| 2715 |
destruct IH as [output_vals_words [vals IH] ]. |
| 2716 |
pose outputaddr as outputaddrv. |
| 2717 |
all: repeat first [ progress destruct_head'_and |
| 2718 |
| progress destruct_head'_ex |
| 2719 |
| exfalso; discriminate |
| 2720 |
| exfalso; assumption |
| 2721 |
| progress subst |
| 2722 |
| progress cbn [R_mem] in * |
| 2723 |
| progress cbv [emp sep Lift1Prop.ex1] in * |
| 2724 |
| progress inversion_prod |
| 2725 |
| progress handle_eval_eval |
| 2726 |
| match goal with |
| 2727 |
| [ H : R_cell64 _ _ _ _ _ |- _ ] |
| 2728 |
=> rewrite (R_cell64_ex_cell64_iff _ _ _ _ _) in H |
| 2729 |
| [ H : map.split _ map.empty _ |- _ ] |
| 2730 |
=> rewrite Properties.map.split_empty_l in H |
| 2731 |
| [ H : map.split _ _ map.empty |- _ ] |
| 2732 |
=> rewrite Properties.map.split_empty_r in H |
| 2733 |
| [ H : R_mem _ _ _ (List.combine _ _) _ |- _ ] |
| 2734 |
=> (idtac + (eapply R_mem_subsumed in H; [ | eassumption ])); |
| 2735 |
let H' := fresh in |
| 2736 |
pose R_mem_combine_ex_array_iff as H'; |
| 2737 |
cbv [Lift1Prop.iff1] in H'; |
| 2738 |
rewrite H' in H; |
| 2739 |
clear H'; |
| 2740 |
[ |
| 2741 |
| clear H; try reflexivity .. ]; |
| 2742 |
[ |
| 2743 |
| rewrite ?@Forall2_map_r_iff in *; |
| 2744 |
(eapply Forall2_weaken; [ | eassumption ]); cbv beta; intros *; |
| 2745 |
match goal with |
| 2746 |
| [ |- ?f ?x -> ?f ?y ] => cut (x = y); [ intros ->; exact id | ] |
| 2747 |
end; |
| 2748 |
rewrite !Z.land_ones by (clear; lia); push_Zmod; |
| 2749 |
rewrite (Z.mod_small (word.unsigned _)) by apply Properties.word.unsigned_range; |
| 2750 |
rewrite Properties.word.unsigned_of_Z_nowrap; [ reflexivity | apply Z.mod_pos_bound; lia ] .. ]; |
| 2751 |
[] |
| 2752 |
| [ H : context[word.add ?x (word.of_Z (8 * Z.of_nat 0))] |- _ ] |
| 2753 |
=> replace (word.add x (word.of_Z (8 * Z.of_nat 0))) with x in * |- by ZnWords |
| 2754 |
end |
| 2755 |
| rewrite <- !Z.land_ones in * |- by (clear; lia) |
| 2756 |
| break_innermost_match_hyps_step ]. |
| 2757 |
all: let v := lazymatch (eval cbv delta [outputaddrv] in outputaddrv) with |
| 2758 |
| inl _ => open_constr:(inl _) |
| 2759 |
| inr _ => open_constr:(inr _) |
| 2760 |
end in |
| 2761 |
eexists (_ :: output_vals_words), (v :: vals); |
| 2762 |
subst outputaddrv. |
| 2763 |
all: cbn [List.length List.combine fold_right List.map List.flat_map eval_idx_or_list_idx R_scalar_or_array R_mem] in *. |
| 2764 |
all: rewrite !Forall2_cons_cons_iff. |
| 2765 |
all: rewrite Forall_cons_iff. |
| 2766 |
all: cbv [eval_idx_or_list_idx] in *. |
| 2767 |
all: repeat first [ match goal with |
| 2768 |
| [ |- ?G ] |
| 2769 |
=> tryif has_evar G then fail else solve [ cbv [eval_idx_Z] in *; eauto ] |
| 2770 |
end |
| 2771 |
| (idtac + rewrite -> !and_assoc + rewrite <- !and_assoc); |
| 2772 |
progress repeat match goal with |
| 2773 |
| [ |- ?A /\ ?B ] => tryif (has_evar A; has_evar B) then fail else split |
| 2774 |
| [ |- context[?A /\ ?B] ] |
| 2775 |
=> has_evar A; tryif has_evar B then fail else rewrite (and_comm A B) |
| 2776 |
end |
| 2777 |
| match goal with |
| 2778 |
| [ |- context[?x = ?y] ] => first [ is_evar x | is_evar y ]; progress unify x y |
| 2779 |
| [ H : ?v = Some ?x |- context[exists y, ?v = Some y /\ @?P y ] ] |
| 2780 |
=> rewrite <- (ex_intro _ x : Basics.impl _ (exists y, v = Some y /\ P y)) |
| 2781 |
| [ H : array cell64 _ ?base ?len ?ma |- context[exists mp mq, map.split ?m mp mq /\ array cell64 _ ?base' ?len' mp /\ _] ] |
| 2782 |
=> unify base base'; unify len len'; |
| 2783 |
rewrite <- (ex_intro (fun mp => exists mq, map.split m mp mq /\ array cell64 _ base' len' mp /\ _) ma : Basics.impl _ _) |
| 2784 |
| [ H : map.split ?m ?mp ?mqv |- context[exists mq, map.split ?m ?mp mq /\ _ /\ _] ] |
| 2785 |
=> rewrite <- (ex_intro (fun mq => map.split m mp mq /\ _ /\ _) mqv : Basics.impl _ _) |
| 2786 |
| [ |- context[?x = word.unsigned ?ev] ] |
| 2787 |
=> lazymatch x with |
| 2788 |
| Z.land ?x (Z.ones ?n) => idtac |
| 2789 |
end; |
| 2790 |
is_evar ev; |
| 2791 |
let __unif := open_constr:(eq_refl : ev = word.of_Z x) in |
| 2792 |
rewrite Properties.word.unsigned_of_Z_nowrap |
| 2793 |
by (rewrite Z.land_ones by (clear; lia); apply Z.mod_pos_bound; clear; lia) |
| 2794 |
| [ H : eval_idx_Z _ _ ?i ?x |- context[eval_idx_Z _ _ ?i (word.unsigned ?ev)] ] |
| 2795 |
=> is_evar ev; |
| 2796 |
let __unif := open_constr:(eq_refl : ev = word.of_Z x) in |
| 2797 |
rewrite Properties.word.unsigned_of_Z_nowrap by lia |
| 2798 |
| [ H : eval_idx_Z _ _ ?i ?x |- context[eval_idx_Z _ _ ?i ?y] ] |
| 2799 |
=> has_evar y; progress unify x y |
| 2800 |
end ]. |
| 2801 |
all: lazymatch goal with |- ?G => tryif has_evar G then fail 0 G "has evar" else idtac end. |
| 2802 |
all: repeat first [ match goal with |
| 2803 |
| [ |- exists mp mq, map.split ?m mp mq /\ emp _ mp /\ _ ] |
| 2804 |
=> exists map.empty, m |
| 2805 |
| [ |- context[map.split _ map.empty _] ] |
| 2806 |
=> rewrite Properties.map.split_empty_l |
| 2807 |
| [ |- context[map.split _ _ map.empty] ] |
| 2808 |
=> rewrite Properties.map.split_empty_r |
| 2809 |
| [ H : Forall2 _ (firstn ?n ?ls) ?y |- Forall2 _ ?ls ?y ] |
| 2810 |
=> saturate_lengths; |
| 2811 |
rewrite firstn_all in H by congruence |
| 2812 |
end |
| 2813 |
| solve [ cbv [emp]; auto ] |
| 2814 |
| rewrite !Z.land_ones by (clear; lia) |
| 2815 |
| rewrite Properties.word.unsigned_of_Z_nowrap by (apply Z.mod_pos_bound; clear; lia) ]. |
| 2816 |
Qed. |
| 2817 |
(* turn off once proof is finished *) |
| 2818 |
Local Ltac debug_run tac := tac (). |
| 2819 |
|
| 2820 |
Theorem symex_asm_func_M_correct |
| 2821 |
{opts : symbolic_options_computed_opt} |
| 2822 |
{output_scalars_are_pointers:bool} |
| 2823 |
d frame asm_args_out asm_args_in (G : symbol -> option Z) (s := init_symbolic_state d) |
| 2824 |
(s' : symbolic_state) (m : machine_state) (output_types : type_spec) (stack_size : nat) (stack_base : Naive.word 64) |
| 2825 |
(inputs : list (idx + list idx)) (callee_saved_registers : list REG) (reg_available : list REG) (asm : Lines) |
| 2826 |
(rets : list (idx + list idx)) |
| 2827 |
(H : symex_asm_func_M (dereference_output_scalars:=output_scalars_are_pointers) callee_saved_registers output_types stack_size inputs reg_available asm s = Success (Success rets, s')) |
| 2828 |
(word_runtime_inputs : list (Naive.word 64 + list (Naive.word 64))) |
| 2829 |
(runtime_inputs := word_args_to_Z_args word_runtime_inputs) |
| 2830 |
(runtime_reg : list Z) |
| 2831 |
(*(Hasm_reg : get_asm_reg m reg_available = runtime_reg)*) |
| 2832 |
(runtime_callee_saved_registers : list Z) |
| 2833 |
(*(Hasm_callee_saved_registers : get_asm_reg m callee_saved_registers = runtime_callee_saved_registers)*) |
| 2834 |
(HR : R_runtime_input (output_scalars_are_pointers:=output_scalars_are_pointers) frame output_types runtime_inputs stack_size stack_base asm_args_out asm_args_in reg_available runtime_reg callee_saved_registers runtime_callee_saved_registers m) |
| 2835 |
(Hd_ok : gensym_dag_ok G d) |
| 2836 |
(d' := s'.(dag_state)) |
| 2837 |
(H_enough_reg : (List.length output_types + List.length runtime_inputs <= List.length reg_available)%nat) |
| 2838 |
(Hcallee_saved_reg_wide_enough : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) callee_saved_registers) |
| 2839 |
(Hreg_wide_enough : Forall (fun reg => let '(_, _, sz) := index_and_shift_and_bitcount_of_reg reg in sz = 64%N) reg_available) |
| 2840 |
(Hinputs : List.Forall2 (eval_idx_or_list_idx G d) inputs runtime_inputs) |
| 2841 |
: exists m' G' |
| 2842 |
(runtime_rets : list (Z + list Z)), |
| 2843 |
(DenoteLines m asm = Some m' |
| 2844 |
/\ R_runtime_output (output_scalars_are_pointers:=output_scalars_are_pointers) frame runtime_rets (type_spec_of_runtime runtime_inputs) stack_size stack_base asm_args_out asm_args_in callee_saved_registers runtime_callee_saved_registers m' |
| 2845 |
/\ List.Forall2 (eval_idx_or_list_idx G' d') rets runtime_rets) |
| 2846 |
/\ gensym_dag_ok G' d' |
| 2847 |
/\ (forall e n, eval G d e n -> eval G' d' e n). |
| 2848 |
Proof. |
| 2849 |
pose proof (word_args_to_Z_args_bounded word_runtime_inputs). |
| 2850 |
pose proof get_asm_reg_bounded. |
| 2851 |
cbv [symex_asm_func_M Symbolic.bind ErrorT.bind lift_dag] in H. |
| 2852 |
break_match_hyps; destruct_head'_prod; destruct_head'_unit; cbn [fst snd] in *; try discriminate; []. |
| 2853 |
repeat first [ progress subst |
| 2854 |
| match goal with |
| 2855 |
| [ H : Success _ = Success _ |- _ ] => inversion H; clear H |
| 2856 |
| [ x := ?y |- _ ] => subst x |
| 2857 |
end ]. |
| 2858 |
cbv [R_runtime_output]. |
| 2859 |
cbv [R_runtime_input R_runtime_input_mem] in HR; repeat (destruct_head'_ex; destruct_head'_and). |
| 2860 |
let HR := lazymatch goal with HR : sep _ _ (machine_mem_state m) |- _ => HR end in |
| 2861 |
destruct (init_symbolic_state_ok m G _ _ ltac:(eassumption) ltac:(eassumption) HR) as [G1 ?]; destruct_head'_and. |
| 2862 |
move Heqp at bottom. |
| 2863 |
repeat first [ progress destruct_head'_ex |
| 2864 |
| progress destruct_head'_and |
| 2865 |
| progress cbn [update_dag_with Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state Symbolic.symbolic_reg_state] in * |
| 2866 |
| solve [ auto ] |
| 2867 |
| match goal with |
| 2868 |
| [ H : filter _ (List.combine _ (List.combine ?l1 ?l2)) = [] |- _ ] |
| 2869 |
=> is_var l1; is_var l2; apply eq_list_of_filter_nil in H; |
| 2870 |
[ try (subst l1 || subst l2) |
| 2871 |
| cbv [idx] in *; saturate_lengths; |
| 2872 |
generalize dependent (List.length l1); |
| 2873 |
generalize dependent (List.length l2); |
| 2874 |
intros; subst; rewrite ?map_length; split; try reflexivity; try congruence; lia ] |
| 2875 |
| [ H : Forall2 (eval_idx_Z _ _) ?l ?v, H' : Forall2 (eval_idx_Z _ _) ?l ?v' |- _ ] |
| 2876 |
=> unique assert (v = v') |
| 2877 |
by (eapply eval_eval_idx_Z_Forall2; eapply Forall2_weaken; [ | eassumption | | eassumption ]; |
| 2878 |
intros *; apply lift_eval_idx_Z_impl; |
| 2879 |
now typeclasses eauto with core) |
| 2880 |
| [ H : R _ _ _ ?m |- _ ] |
| 2881 |
=> is_var m; unique pose proof (@reg_bounded_of_R _ _ _ _ H) |
| 2882 |
end |
| 2883 |
| match reverse goal with |
| 2884 |
| [ H : build_inputs _ _ = _ |- exists m' : machine_state, _ ] |
| 2885 |
=> debug_run ltac:(fun _ => idtac "build_inputs start"); |
| 2886 |
move H at bottom; eapply build_inputs_ok_R in H; |
| 2887 |
[ | eassumption .. ]; |
| 2888 |
debug_run ltac:(fun _ => idtac "build_inputs end") |
| 2889 |
| [ H : build_merge_base_addresses _ ?reg_available _ = _ |- exists m' : machine_state, _ ] |
| 2890 |
=> debug_run ltac:(fun _ => idtac "build_merge_base_addresses start"); |
| 2891 |
move H at bottom; |
| 2892 |
let runtime_regv := lazymatch reg_available with |
| 2893 |
| firstn ?n _ => constr:(firstn n runtime_reg) |
| 2894 |
| skipn ?n _ => constr:(skipn n runtime_reg) |
| 2895 |
| _ => runtime_reg |
| 2896 |
end in |
| 2897 |
eapply @build_merge_base_addresses_ok_R |
| 2898 |
with (runtime_reg := runtime_regv (*get_asm_reg m reg_available*)) |
| 2899 |
in H; |
| 2900 |
[ |
| 2901 |
| try solve [ eassumption | subst; eauto using Forall_skipn, Forall_firstn with nocore ] .. ]; |
| 2902 |
[ |
| 2903 |
| lazymatch goal with |
| 2904 |
| [ |- (_ <= _)%nat ] => saturate_lengths; try lia |
| 2905 |
| [ |- ?G ] |
| 2906 |
=> first [ has_evar G |
| 2907 |
| try solve |
| 2908 |
[ repeat |
| 2909 |
first [ reflexivity |
| 2910 |
| progress subst |
| 2911 |
| progress cbv [get_asm_reg] |
| 2912 |
| rewrite skipn_map |
| 2913 |
| rewrite firstn_map ] ] ] |
| 2914 |
end .. ]; |
| 2915 |
[ |
| 2916 |
| try solve [ eapply Forall2_weaken; [ | eassumption ]; now eauto using lift_eval_idx_or_list_idx_impl ] .. ]; |
| 2917 |
[ |
| 2918 |
| lazymatch goal with |
| 2919 |
| [ |- Lift1Prop.iff1 ?P ?Q ] |
| 2920 |
=> match P with |
| 2921 |
| context[R_list_scalar_or_array ?p ?v] |
| 2922 |
=> match Q with |
| 2923 |
| context[R_list_scalar_or_array ?p' ?v'] |
| 2924 |
=> tryif (is_evar p'; is_evar v') |
| 2925 |
then fail |
| 2926 |
else (unify p p'; unify v v') |
| 2927 |
end |
| 2928 |
end; |
| 2929 |
SeparationLogic.cancel |
| 2930 |
| [ |- Forall2 _ _ _ ] |
| 2931 |
=> saturate_lengths; |
| 2932 |
adjust_Foralls_firstn_skipn; |
| 2933 |
try solve [ Foralls_to_nth_error; eauto using lift_eval_idx_Z_impl ] |
| 2934 |
| _ => idtac |
| 2935 |
end .. ]; |
| 2936 |
[ |
| 2937 |
| lazymatch goal with |
| 2938 |
| [ |- List.map word.unsigned _ = _ ] |
| 2939 |
=> saturate_lengths; |
| 2940 |
adjust_Foralls_firstn_skipn; |
| 2941 |
try rewrite ListUtil.List.firstn_firstn, Nat.min_idempotent; |
| 2942 |
try eassumption |
| 2943 |
| _ => idtac |
| 2944 |
end .. ]; |
| 2945 |
[]; |
| 2946 |
debug_run ltac:(fun _ => idtac "build_merge_base_addresses end") |
| 2947 |
| [ H : build_merge_stack_placeholders _ _ = _ |- exists m' : machine_state, _ ] |
| 2948 |
=> debug_run ltac:(fun _ => idtac "build_merge_stack_placeholders start"); |
| 2949 |
subst stack_size; |
| 2950 |
eapply build_merge_stack_placeholders_ok_R in H; |
| 2951 |
[ | try eassumption; auto .. ]; |
| 2952 |
[ |
| 2953 |
| lazymatch goal with |
| 2954 |
| [ |- Lift1Prop.iff1 _ _ ] => set_evars; SeparationLogic.cancel |
| 2955 |
| _ => idtac |
| 2956 |
end .. ]; |
| 2957 |
[]; |
| 2958 |
debug_run ltac:(fun _ => idtac "build_merge_stack_placeholders end") |
| 2959 |
| [ H : mapM _ callee_saved_registers _ = _ |- @ex ?T _ ] |
| 2960 |
=> lazymatch T with |
| 2961 |
| (*runtime_rets*) list (Z + list Z) => idtac |
| 2962 |
| (*m'*) machine_state => idtac |
| 2963 |
end; |
| 2964 |
debug_run ltac:(fun _ => idtac "get callee_saved_registers start"); |
| 2965 |
eapply mapM_GetReg_ok_R in H; |
| 2966 |
[ | eassumption .. ]; |
| 2967 |
debug_run ltac:(fun _ => idtac "get callee_saved_registers end") |
| 2968 |
| [ H : SymexLines _ _ = Success _ |- exists m' : machine_state, _ ] |
| 2969 |
=> debug_run ltac:(fun _ => idtac "SymexLines start"); |
| 2970 |
let H' := fresh in |
| 2971 |
eapply SymexLines_ok_R in H; |
| 2972 |
[ destruct H as [m' H]; |
| 2973 |
exists m'; |
| 2974 |
unshelve eexists; |
| 2975 |
lazymatch goal with |
| 2976 |
| [ |- symbol -> option Z ] |
| 2977 |
=> repeat match goal with |
| 2978 |
| [ H : ?T |- _ ] |
| 2979 |
=> lazymatch T with |
| 2980 |
| symbol -> option Z => fail |
| 2981 |
| _ => clear H |
| 2982 |
end |
| 2983 |
end; |
| 2984 |
shelve |
| 2985 |
| _ => idtac |
| 2986 |
end |
| 2987 |
| eassumption .. ]; |
| 2988 |
debug_run ltac:(fun _ => idtac "SymexLines end") |
| 2989 |
| [ H : LoadArray _ _ _ = Success _ |- _ ] |
| 2990 |
=> debug_run ltac:(fun _ => idtac "LoadArray start"); |
| 2991 |
eapply LoadArray_ok_R in H; |
| 2992 |
[ | eauto using lift_eval_idx_Z_impl .. ]; |
| 2993 |
[]; |
| 2994 |
debug_run ltac:(fun _ => idtac "LoadArray end") |
| 2995 |
| [ H : LoadOutputs _ _ _ = Success _ |- _ ] |
| 2996 |
=> debug_run ltac:(fun _ => idtac "LoadOutputs start"); |
| 2997 |
eapply LoadOutputs_ok_R in H; |
| 2998 |
[ | try eassumption .. ]; |
| 2999 |
[ |
| 3000 |
| try solve [ eapply Forall2_weaken; [ | eassumption ]; cbv beta; intros *; |
| 3001 |
break_innermost_match; cbv [eval_idx_Z] in *; eauto 10 |
| 3002 |
| lazymatch goal with |
| 3003 |
| [ |- Forall _ ?ls ] |
| 3004 |
=> rewrite Forall_forall_iff_nth_error in Hreg_wide_enough; |
| 3005 |
cbv [get_asm_reg] in *; |
| 3006 |
cbv [val_or_list_val_matches_spec] in *; |
| 3007 |
subst; |
| 3008 |
Foralls_to_nth_error; |
| 3009 |
cbv [index_and_shift_and_bitcount_of_reg] in *; inversion_pair; subst; |
| 3010 |
eauto |
| 3011 |
end ] .. ]; |
| 3012 |
[]; |
| 3013 |
debug_run ltac:(fun _ => idtac "LoadOutputs end") |
| 3014 |
end ]. |
| 3015 |
all: [ > ]. |
| 3016 |
lazymatch reverse goal with |
| 3017 |
| [ H : ?x = Success ?y |- _ ] |
| 3018 |
=> let T := type of H in |
| 3019 |
fail 0 "A non-processed step remains:" T |
| 3020 |
| _ => idtac |
| 3021 |
end. |
| 3022 |
all: destruct_head' symbolic_state; cbn [update_dag_with Symbolic.dag_state Symbolic.symbolic_flag_state Symbolic.symbolic_mem_state Symbolic.symbolic_reg_state] in *; subst. |
| 3023 |
all: cbv [R update_dag_with R_runtime_output_mem] in *; destruct_head'_and. |
| 3024 |
all: destruct_head' machine_state; cbn [Semantics.machine_reg_state Semantics.machine_flag_state Semantics.machine_mem_state] in *. |
| 3025 |
all: cbn [R_mem] in *. |
| 3026 |
eexists. |
| 3027 |
all: repeat first [ assumption |
| 3028 |
| match goal with |
| 3029 |
| [ |- _ /\ _ ] => split |
| 3030 |
| [ |- ex _ ] => eexists |
| 3031 |
end ]. |
| 3032 |
all: lazymatch goal with |
| 3033 |
| [ |- gensym_dag_ok _ _ ] => eassumption |
| 3034 |
| [ |- forall x y, eval _ _ _ _ -> eval _ _ _ _ ] => solve [ eauto 100 ] |
| 3035 |
| [ |- Forall2 (eval_idx_or_list_idx _ _) _ _ ] |
| 3036 |
=> eapply Forall2_weaken; [ | eassumption ]; |
| 3037 |
eapply lift_eval_idx_or_list_idx_impl; eauto 100 |
| 3038 |
| _ => idtac |
| 3039 |
end. |
| 3040 |
all: lazymatch goal with |
| 3041 |
| [ |- sep _ _ ?m ] |
| 3042 |
=> let H := lazymatch goal with H : sep _ _ m |- _ => H end in |
| 3043 |
set_evars; |
| 3044 |
revert H; generalize m; |
| 3045 |
refine (_ : Lift1Prop.impl1 _ _); |
| 3046 |
subst_evars; |
| 3047 |
lazymatch goal with |
| 3048 |
| [ |- Lift1Prop.impl1 ?P ?Q ] |
| 3049 |
=> cut (Lift1Prop.iff1 P Q); [ intros ->; reflexivity | ] |
| 3050 |
end; |
| 3051 |
repeat first [ progress (SeparationLogic.cancel; cbn [seps]) |
| 3052 |
| match goal with |
| 3053 |
| [ |- Lift1Prop.iff1 ?P ?Q ] |
| 3054 |
=> match P with |
| 3055 |
| context[array ?c ?s ?base ?v] |
| 3056 |
=> match Q with |
| 3057 |
| context[array c s base ?v'] |
| 3058 |
=> assert_fails (constr_eq v v'); |
| 3059 |
cut (v' = v); [ intros -> | ] |
| 3060 |
end |
| 3061 |
| context[R_list_scalar_or_array ?x ?v] |
| 3062 |
=> match Q with |
| 3063 |
| context[R_list_scalar_or_array x ?v'] |
| 3064 |
=> assert_fails (constr_eq v v'); |
| 3065 |
cut (v' = v); [ intros -> | ] |
| 3066 |
end |
| 3067 |
end |
| 3068 |
| [ |- Lift1Prop.iff1 (R_list_scalar_or_array ?x ?v) (R_list_scalar_or_array ?x' ?v') ] |
| 3069 |
=> unify x x'; |
| 3070 |
cut (v' = v); [ intros -> | ] |
| 3071 |
end ] |
| 3072 |
| _ => idtac |
| 3073 |
end. |
| 3074 |
all: let G := match goal with |- ?G => G end in |
| 3075 |
tryif has_evar G |
| 3076 |
then idtac |
| 3077 |
else (try assumption; |
| 3078 |
lazymatch goal with |
| 3079 |
| [ |- List.length _ = List.length _ ] |
| 3080 |
=> saturate_lengths; congruence |
| 3081 |
| [ |- ?x = ?x ] => reflexivity |
| 3082 |
| _ => idtac |
| 3083 |
end). |
| 3084 |
all: lazymatch goal with |
| 3085 |
| [ |- Forall2 val_or_list_val_matches_spec _ _ ] |
| 3086 |
=> cbv [eval_idx_or_list_idx val_or_list_val_matches_spec type_spec_of_runtime] in *; |
| 3087 |
rewrite ?@Forall2_map_l_iff, ?@Forall2_map_r_iff in *; |
| 3088 |
saturate_lengths; |
| 3089 |
Foralls_to_nth_error; |
| 3090 |
intros; saturate_lengths; |
| 3091 |
try congruence; |
| 3092 |
try match goal with |
| 3093 |
| [ H : (?x < ?y)%nat, H' : (?x >= ?y')%nat |- _ ] |
| 3094 |
=> exfalso; cut (y = y'); [ clear -H H'; lia | congruence ] |
| 3095 |
end |
| 3096 |
| _ => idtac |
| 3097 |
end. |
| 3098 |
all: [ > | ]. |
| 3099 |
all: lazymatch goal with |
| 3100 |
| [ |- List.map fst (filter _ (List.combine _ _)) = List.map fst (filter _ (List.combine _ _)) ] |
| 3101 |
=> idtac |
| 3102 |
end. |
| 3103 |
all: rewrite <- Forall2_eq, !Forall2_map_l_iff, !Forall2_map_r_iff. |
| 3104 |
all: apply List.Forall2_filter_same. |
| 3105 |
all: pose proof Hreg_wide_enough as Hreg_wide_enough'. |
| 3106 |
all: rewrite Forall_forall_iff_nth_error in Hreg_wide_enough; |
| 3107 |
cbv [get_asm_reg val_or_list_val_matches_spec] in *; |
| 3108 |
rewrite <- !@Forall2_eq, ?@Forall2_map_l_iff, ?@Forall2_map_r_iff in *; |
| 3109 |
saturate_lengths; |
| 3110 |
Foralls_to_nth_error. |
| 3111 |
all: cbn [fst]. |
| 3112 |
all: repeat (rewrite ?Bool.orb_true_l, ?Bool.orb_true_r, ?Bool.orb_false_l, ?Bool.orb_false_r in *; subst). |
| 3113 |
all: try discriminate. |
| 3114 |
all: try reflexivity. |
| 3115 |
all: try specialize (Hreg_wide_enough _ eq_refl). |
| 3116 |
all: try specialize (Hreg_wide_enough _ _ ltac:(eassumption)). |
| 3117 |
all: intros; saturate_lengths. |
| 3118 |
all: try specialize (Hreg_wide_enough _ _ ltac:(eassumption)). |
| 3119 |
all: try match goal with |
| 3120 |
| [ H : (?x < ?y)%nat, H' : (?x >= ?y')%nat |- _ ] |
| 3121 |
=> exfalso; cut (y = y'); [ clear -H H'; lia | congruence ] |
| 3122 |
end. |
| 3123 |
all: repeat match goal with |
| 3124 |
| [ H : ?x = Some _, H' : ?x = None |- _ ] |
| 3125 |
=> rewrite H in H'; inversion_option |
| 3126 |
| [ H : ?x = Some _, H' : ?x = Some _ |- _ ] |
| 3127 |
=> rewrite H in H'; inversion_option |
| 3128 |
end. |
| 3129 |
all: repeat match goal with |
| 3130 |
| [ H : nth_error ?ls (?a + ?b) = _, H' : nth_error ?ls (?a' + ?b) = _ |- _ ] |
| 3131 |
=> replace a' with a in * by congruence; |
| 3132 |
rewrite H in H' |
| 3133 |
end. |
| 3134 |
all: inversion_option; subst. |
| 3135 |
all: cbv [Option.is_Some] in *; break_innermost_match_hyps; inversion_option; inversion_sum. |
| 3136 |
all: subst. |
| 3137 |
all: try discriminate. |
| 3138 |
all: lazymatch goal with |
| 3139 |
| [ H : nth_error ?ls ?i = Some (Some _), H' : nth_error ?ls' ?i = Some (inl _) |- False ] |
| 3140 |
=> revert H H' |
| 3141 |
| [ H : nth_error ?ls ?i = Some None, H' : nth_error ?ls' ?i = Some (inr _) |- False ] |
| 3142 |
=> revert H H' |
| 3143 |
| [ H : nth_error _ _ = Some ?r, H' : nth_error _ _ = Some ?r0 |- ?r = ?r0 ] |
| 3144 |
=> apply Properties.word.unsigned_inj; revert H H' |
| 3145 |
end. |
| 3146 |
all: repeat match goal with |
| 3147 |
| [ H : Forall2 _ ?x _ |- context[nth_error ?x _] ] => revert H |
| 3148 |
| [ H : Forall2 _ _ (List.combine ?lsa ?lsb) |- context[nth_error ?ls] ] |
| 3149 |
=> first [ find_list_in ls lsa | find_list_in ls lsb ]; revert H |
| 3150 |
end. |
| 3151 |
all: revert dependent rets; intro. |
| 3152 |
all: reverted_Foralls_to_nth_error; |
| 3153 |
intros *; Foralls_to_nth_error_rewrites; Foralls_to_nth_error_destructs; |
| 3154 |
Foralls_to_nth_error_cleanup. |
| 3155 |
all: intros; saturate_lengths. |
| 3156 |
all: try match goal with |
| 3157 |
| [ H : (?x < ?y)%nat, H' : (?x >= ?y')%nat |- _ ] |
| 3158 |
=> exfalso; cut (y = y'); [ clear -H H'; lia | congruence ] |
| 3159 |
end. |
| 3160 |
all: try congruence. |
| 3161 |
all: repeat match goal with |
| 3162 |
| [ H : context[nth_error (List.combine _ _)] |- _ ] |
| 3163 |
=> lazymatch type of H with |
| 3164 |
| forall i : nat, @?P i |
| 3165 |
=> let T := open_constr:(forall i : nat, _) in |
| 3166 |
cut T; |
| 3167 |
[ clear H |
| 3168 |
| let i := fresh "i" in |
| 3169 |
intro i; specialize (H i); |
| 3170 |
set_evars; revert H; Foralls_to_nth_error_rewrites; |
| 3171 |
intro H; subst_evars; exact H ] |
| 3172 |
end |
| 3173 |
end. |
| 3174 |
all: Foralls_to_nth_error. |
| 3175 |
all: intros; saturate_lengths. |
| 3176 |
all: try match goal with |
| 3177 |
| [ H : (?x < ?y)%nat, H' : (?x >= ?y')%nat |- _ ] |
| 3178 |
=> exfalso; cut (y = y'); [ clear -H H'; lia | congruence ] |
| 3179 |
end. |
| 3180 |
all: rewrite ?(fun x y => Z.land_ones (Semantics.get_reg x y)) in * by (clear; lia). |
| 3181 |
all: rewrite ?(fun x y => Z.mod_small (Semantics.get_reg x y)) in * by now apply get_reg_bounded. |
| 3182 |
all: split_iff. |
| 3183 |
all: repeat match goal with |
| 3184 |
| [ H : forall v, (0 <= v < _)%Z -> eval_idx_Z _ _ ?a v -> _, H' : eval_idx_Z _ _ ?a ?v' |- _ ] |
| 3185 |
=> specialize (fun H1 H2 => H v' (conj H1 H2)) |
| 3186 |
end. |
| 3187 |
all: specialize_by_assumption. |
| 3188 |
all: specialize_by (cbv [eval_idx_Z] in *; eauto). |
| 3189 |
all: specialize_by apply get_reg_bounded. |
| 3190 |
all: specialize_by_assumption. |
| 3191 |
all: handle_eval_eval. |
| 3192 |
all: subst. |
| 3193 |
all: try congruence. |
| 3194 |
all: destruct_head'_ex. |
| 3195 |
all: destruct_head'_and. |
| 3196 |
all: handle_eval_eval. |
| 3197 |
all: subst. |
| 3198 |
all: try match goal with |
| 3199 |
| [ H : Symbolic.get_reg _ _ = Some _ |- _ ] |
| 3200 |
=> eapply get_reg_of_R_regs in H; [ | eassumption .. ] |
| 3201 |
end. |
| 3202 |
all: lazymatch goal with |
| 3203 |
| [ H : nth_error ?ls _ = Some (inl (inl ?r)), H' : Symbolic.get_reg _ (reg_index ?r) = _ |- _ ] |
| 3204 |
=> repeat match goal with |
| 3205 |
| [ H : Forall2 _ ls _ |- _ ] |
| 3206 |
=> revert H |
| 3207 |
end |
| 3208 |
| _ => idtac |
| 3209 |
end. |
| 3210 |
all: Foralls_to_nth_error. |
| 3211 |
all: handle_eval_eval. |
| 3212 |
all: repeat match goal with |
| 3213 |
| [ H : eval_idx_Z ?G ?d ?e ?v, H' : forall a b, eval ?G ?d a b -> eval ?G' ?d' a b |- _ ] |
| 3214 |
=> apply H' in H; change (eval_idx_Z G' d' e v) in H |
| 3215 |
end. |
| 3216 |
all: handle_eval_eval. |
| 3217 |
all: subst. |
| 3218 |
all: try congruence. |
| 3219 |
all: try specialize (Hreg_wide_enough _ eq_refl). |
| 3220 |
all: lazymatch goal with |
| 3221 |
| [ H : eval_idx_Z _ _ ?i ?v, H' : nth_error _ _ = Some (inl ?i), H'' : nth_error _ _ = Some (inl (inr ?i)) |- ?v = _ ] |
| 3222 |
=> revert H' H'' |
| 3223 |
| [ H : eval_idx_Z _ _ ?i ?v, H' : nth_error ?ls _ = Some (inl ?i) |- ?v = _ ] |
| 3224 |
=> revert H' |
| 3225 |
| [ H : nth_error _ _ = Some ?r |- word.unsigned ?r = _ ] |
| 3226 |
=> revert H |
| 3227 |
end. |
| 3228 |
all: repeat match goal with |
| 3229 |
| [ H : Forall2 _ ?lsa ?lsb |- context[nth_error ?ls] ] |
| 3230 |
=> first [ find_list_in ls lsa | find_list_in ls lsb ]; |
| 3231 |
revert H |
| 3232 |
end. |
| 3233 |
all: Foralls_to_nth_error. |
| 3234 |
all: intros. |
| 3235 |
all: cbv [eval_idx_or_list_idx] in *. |
| 3236 |
all: rewrite ?(fun x y => Z.land_ones (Semantics.get_reg x y)) in * by (clear; lia). |
| 3237 |
all: rewrite ?(fun x y => Z.mod_small (Semantics.get_reg x y)) in * by now apply get_reg_bounded. |
| 3238 |
all: split_iff. |
| 3239 |
all: try specialize (Hreg_wide_enough _ eq_refl). |
| 3240 |
all: repeat first [ match goal with |
| 3241 |
| [ H : forall v, (0 <= v < _)%Z -> eval_idx_Z _ _ ?a v -> _, H' : eval_idx_Z _ _ ?a ?v' |- _ ] |
| 3242 |
=> specialize (fun H1 H2 => H v' (conj H1 H2)) |
| 3243 |
| [ H : ?x = Some _, H' : ?x = None |- _ ] |
| 3244 |
=> rewrite H in H'; inversion_option |
| 3245 |
| [ H : ?x = Some _, H' : ?x = Some _ |- _ ] |
| 3246 |
=> rewrite H in H'; inversion_option |
| 3247 |
| [ H : Symbolic.get_reg _ _ = Some _ |- _ ] |
| 3248 |
=> eapply get_reg_of_R_regs in H; [ | eassumption .. ] |
| 3249 |
end |
| 3250 |
| progress subst |
| 3251 |
| progress inversion_sum |
| 3252 |
| congruence |
| 3253 |
| progress specialize_by_assumption |
| 3254 |
| progress specialize_by (cbv [eval_idx_Z] in *; eauto) |
| 3255 |
| progress specialize_by apply get_reg_bounded |
| 3256 |
| progress handle_eval_eval ]. |
| 3257 |
all: repeat match goal with |
| 3258 |
| [ H : eval_idx_Z ?G ?d ?e ?v, H' : forall a b, eval ?G ?d a b -> eval ?G' ?d' a b |- _ ] |
| 3259 |
=> apply H' in H; change (eval_idx_Z G' d' e v) in H |
| 3260 |
end. |
| 3261 |
all: handle_eval_eval. |
| 3262 |
all: try congruence. |
| 3263 |
Time Qed. (* Finished transaction in 14.751 secs (14.751u,0.s) *) |
| 3264 |
|
| 3265 |
Theorem symex_asm_func_correct |
| 3266 |
{opts : symbolic_options_computed_opt} |
| 3267 |
{output_scalars_are_pointers:bool} |
| 3268 |
frame asm_args_out asm_args_in (G : symbol -> option Z) (d : dag) (output_types : type_spec) (stack_size : nat) (stack_base : Naive.word 64) |
| 3269 |
(inputs : list (idx + list idx)) (callee_saved_registers : list REG) (reg_available : list REG) (asm : Lines) |
| 3270 |
(rets : list (idx + list idx)) |
| 3271 |
(s' : symbolic_state) |
| 3272 |
(H : symex_asm_func (dereference_output_scalars:=output_scalars_are_pointers) d callee_saved_registers output_types stack_size inputs reg_available asm = Success (rets, s')) |
| 3273 |
(d' := s'.(dag_state)) |
| 3274 |
(m : machine_state) |
| 3275 |
(word_runtime_inputs : list (Naive.word 64 + list (Naive.word 64))) |
| 3276 |
(runtime_inputs := word_args_to_Z_args word_runtime_inputs) |
| 3277 |
(runtime_reg : list Z) |
| 3278 |
(runtime_callee_saved_registers : list Z) |
| 3279 |
(Hasm_reg : get_asm_reg m reg_available = runtime_reg) |
| 3280 |
(HR : R_runtime_input (output_scalars_are_pointers:=output_scalars_are_pointers) frame output_types runtime_inputs stack_size stack_base asm_args_out asm_args_in reg_available runtime_reg callee_saved_registers runtime_callee_saved_registers m) |
| 3281 |
(HG_ok : gensym_dag_ok G d) |
| 3282 |
(Hinputs : List.Forall2 (eval_idx_or_list_idx G d) inputs runtime_inputs) |
| 3283 |
: (exists m' G' |
| 3284 |
(runtime_rets : list (Z + list Z)), |
| 3285 |
DenoteLines m asm = Some m' |
| 3286 |
/\ R_runtime_output (output_scalars_are_pointers:=output_scalars_are_pointers) frame runtime_rets (type_spec_of_runtime runtime_inputs) stack_size stack_base asm_args_out asm_args_in callee_saved_registers runtime_callee_saved_registers m' |
| 3287 |
/\ (forall e n, eval G d e n -> eval G' d' e n) |
| 3288 |
/\ gensym_dag_ok G' d' |
| 3289 |
/\ List.Forall2 (eval_idx_or_list_idx G' d') rets runtime_rets). |
| 3290 |
Proof. |
| 3291 |
cbv [symex_asm_func] in H; break_innermost_match_hyps; inversion_ErrorT; inversion_prod; subst; reflect_hyps. |
| 3292 |
cbv [R]; break_innermost_match. |
| 3293 |
let H := multimatch goal with H : _ = Success _ |- _ => H end in |
| 3294 |
eapply symex_asm_func_M_correct in H; try eassumption; try apply surjective_pairing; try reflexivity. |
| 3295 |
{ destruct_head'_ex; destruct_head'_and. |
| 3296 |
do 3 eexists; repeat match goal with |- _ /\ _ => apply conj end; try eassumption. } |
| 3297 |
{ subst runtime_inputs; apply eq_length_Forall2 in Hinputs; lia. } |
| 3298 |
Qed. |
| 3299 |
|
| 3300 |
Theorem check_equivalence_correct |
| 3301 |
{opts : symbolic_options_computed_opt} |
| 3302 |
(output_scalars_are_pointers:=false) |
| 3303 |
{assembly_calling_registers' : assembly_calling_registers_opt} |
| 3304 |
{assembly_stack_size' : assembly_stack_size_opt} |
| 3305 |
{assembly_output_first : assembly_output_first_opt} |
| 3306 |
{assembly_argument_registers_left_to_right : assembly_argument_registers_left_to_right_opt} |
| 3307 |
{assembly_callee_saved_registers' : assembly_callee_saved_registers_opt} |
| 3308 |
{t} |
| 3309 |
(frame : Semantics.mem_state -> Prop) |
| 3310 |
(asm : list (String.string (* fname *) * Lines)) |
| 3311 |
(expr : API.Expr t) |
| 3312 |
(arg_bounds : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 3313 |
(out_bounds : ZRange.type.base.option.interp (type.final_codomain t)) |
| 3314 |
(Hwf : API.Wf expr) |
| 3315 |
(H : check_equivalence asm expr arg_bounds out_bounds = Success tt) |
| 3316 |
(PHOAS_args : type.for_each_lhs_of_arrow API.interp_type t) |
| 3317 |
(word_args : list (Naive.word 64 + list (Naive.word 64))) |
| 3318 |
(args := word_args_to_Z_args word_args) |
| 3319 |
(Hargs : build_input_runtime t args = Some (PHOAS_args, [])) |
| 3320 |
(HPHOAS_args : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) arg_bounds PHOAS_args = true) |
| 3321 |
(output_types : type_spec := match simplify_base_type (type.final_codomain t) out_bounds with Success output_types => output_types | Error _ => nil end) |
| 3322 |
(st : machine_state) |
| 3323 |
(asm_args_out asm_args_in : list (Naive.word 64)) |
| 3324 |
(runtime_regs := get_asm_reg st assembly_calling_registers) |
| 3325 |
(runtime_callee_saved_registers := get_asm_reg st assembly_callee_saved_registers) |
| 3326 |
: Forall |
| 3327 |
(fun '(_fname, asm) |
| 3328 |
=> forall |
| 3329 |
(stack_size := N.to_nat (assembly_stack_size match strip_ret asm with Success asm => asm | Error _ => asm end)) |
| 3330 |
(stack_base := word.of_Z (Semantics.get_reg st rsp - 8 * Z.of_nat stack_size)) |
| 3331 |
(HR : R_runtime_input (output_scalars_are_pointers:=output_scalars_are_pointers) frame output_types args stack_size stack_base asm_args_out asm_args_in assembly_calling_registers runtime_regs assembly_callee_saved_registers runtime_callee_saved_registers st), |
| 3332 |
exists asm' st' (retvals : list (Z + list Z)), |
| 3333 |
strip_ret asm = Success asm' |
| 3334 |
/\ DenoteLines st asm' = Some st' |
| 3335 |
/\ simplify_base_runtime (type.app_curried (API.Interp expr) PHOAS_args) = Some retvals |
| 3336 |
/\ R_runtime_output (output_scalars_are_pointers:=output_scalars_are_pointers) frame retvals (type_spec_of_runtime args) stack_size stack_base asm_args_out asm_args_in assembly_callee_saved_registers runtime_callee_saved_registers st') |
| 3337 |
asm. |
| 3338 |
Proof. |
| 3339 |
cbv beta delta [check_equivalence map_symex_asm ErrorT.bind] in H. |
| 3340 |
repeat |
| 3341 |
first [ rewrite List.ErrorT.List.bind_list_cps_id, <- !List.ErrorT.List.eq_bind_list_lift in H; |
| 3342 |
cbv beta delta [ErrorT.bind] in H |
| 3343 |
| match type of H with |
| 3344 |
| (let n := ?v in _) = _ |
| 3345 |
=> set v as n in H; |
| 3346 |
lazymatch type of H with |
| 3347 |
| (let n := ?v in ?rest) = ?rhs |
| 3348 |
=> change (match v with n => rest end = rhs) in H |
| 3349 |
end |
| 3350 |
| match ?v with Success n => @?S n | Error e => @?E e end = ?rhs |
| 3351 |
=> let n := fresh n in |
| 3352 |
let e := fresh e in |
| 3353 |
destruct v as [n|e] eqn:?; [ change (S n = rhs) in H | change (E e = rhs) in H ]; |
| 3354 |
cbv beta in H |
| 3355 |
| match ?v with pair a b => @?P a b end = ?rhs |
| 3356 |
=> let a := fresh a in |
| 3357 |
let b := fresh b in |
| 3358 |
destruct v as [a b] eqn:?; change (P a b = rhs) in H; |
| 3359 |
cbv beta in H |
| 3360 |
| match ?v with true => ?T | false => ?F end = ?rhs |
| 3361 |
=> let a := fresh a in |
| 3362 |
let b := fresh b in |
| 3363 |
destruct v eqn:?; [ change (T = rhs) in H | change (F = rhs) in H ]; |
| 3364 |
cbv beta in H |
| 3365 |
end ]; try discriminate; []. |
| 3366 |
progress cbv beta delta [map_error ErrorT.map2 id] in *. |
| 3367 |
repeat first [ match goal with |
| 3368 |
| [ H : context G[let x := ?y in @?P x] |- _ ] => |
| 3369 |
tryif is_var y then |
| 3370 |
let G' := context G[P y] in |
| 3371 |
progress change G' in H |
| 3372 |
else |
| 3373 |
let h := fresh x in |
| 3374 |
set (h := y) in *; |
| 3375 |
let G' := context G[P h] in |
| 3376 |
progress change G' in H |
| 3377 |
| [ H : context[let x := ?y in _] |- _ ] => tryif is_var y then fail else let h := fresh x in set (h := y) in * |
| 3378 |
| [ H : context[List.ErrorT.List.bind_list] |- _ ] |
| 3379 |
=> rewrite List.ErrorT.List.bind_list_cps_id, <- !List.ErrorT.List.eq_bind_list_lift in H; |
| 3380 |
cbv beta delta [ErrorT.bind] in H |
| 3381 |
| [ H := Some _ |- _ ] => subst H |
| 3382 |
| [ H := None |- _ ] => subst H |
| 3383 |
| [ H := List.map _ _ |- _ ] => subst H |
| 3384 |
| [ H : context[match List.ErrorT.List.lift ?x with _ => _ end] |- _ ] |
| 3385 |
=> destruct (List.ErrorT.List.lift x) eqn:? |
| 3386 |
end |
| 3387 |
| progress cbv beta in * |
| 3388 |
| progress inversion_ErrorT |
| 3389 |
| progress subst |
| 3390 |
| progress break_innermost_match_hyps |
| 3391 |
| progress rewrite @List.ErrorT.List.lift_Success_Forall2_iff in * |
| 3392 |
| progress rewrite ?@Forall2_map_l_iff, ?@Forall2_map_r_iff in * ]. |
| 3393 |
all: Foralls_to_nth_error. |
| 3394 |
all: intros; inversion_ErrorT; inversion_pair; subst. |
| 3395 |
all: progress reflect_hyps. |
| 3396 |
all: subst. |
| 3397 |
all: pose proof empty_gensym_dag_ok. |
| 3398 |
all: let H := fresh in pose proof Hargs as H; eapply build_input_runtime_ok_nil in H; [ | eassumption .. ]. |
| 3399 |
all: pose proof (word_args_to_Z_args_bounded word_args). |
| 3400 |
all: repeat |
| 3401 |
first [ assumption |
| 3402 |
| match goal with |
| 3403 |
| [ H : build_inputs _ _ = _ |- _ ] => move H at bottom; eapply build_inputs_ok in H; [ | eassumption .. ] |
| 3404 |
| [ H : symex_PHOAS ?expr ?inputs ?d = Success _, H' : build_input_runtime _ ?ri = Some _ |- _ ] |
| 3405 |
=> move H at bottom; eapply symex_PHOAS_correct with (runtime_inputs:=ri) in H; |
| 3406 |
[ | try eassumption .. ]; |
| 3407 |
[ | first [ assumption | eapply Forall2_weaken; [ apply lift_eval_idx_or_list_idx_impl | eassumption ] ] .. ] |
| 3408 |
| [ H : symex_asm_func _ _ _ _ _ _ _ = Success _ |- _ ] |
| 3409 |
=> move H at bottom; eapply symex_asm_func_correct in H; |
| 3410 |
[ | (eassumption + apply surjective_pairing + reflexivity + trivial) .. ]; |
| 3411 |
[ | clear H; first [ assumption | eapply Forall2_weaken; [ apply lift_eval_idx_or_list_idx_impl | eassumption ] ] ] |
| 3412 |
end |
| 3413 |
| progress destruct_head'_ex |
| 3414 |
| progress destruct_head'_and |
| 3415 |
| progress inversion_prod |
| 3416 |
| progress inversion_ErrorT |
| 3417 |
| progress subst |
| 3418 |
| match goal with |
| 3419 |
| [ H : ?x = Some ?a, H' : ?x = Some ?b |- _ ] |
| 3420 |
=> rewrite H in H'; inversion_option |
| 3421 |
| [ H : ?x = Some _, H' : ?x = None |- _ ] => exfalso; clear -H H'; rewrite H in H'; inversion_option |
| 3422 |
| [ H : forall args, Forall2 ?P args ?v -> Forall2 _ _ _, H' : Forall2 ?P _ ?v |- _ ] |
| 3423 |
=> specialize (H _ H') |
| 3424 |
| [ Himpl : forall e n, eval ?G1 ?d1 e n -> eval ?G2 ?d2 e n, |
| 3425 |
H1 : Forall2 (eval_idx_or_list_idx ?G1 ?d1) ?PHOAS_output ?r1, |
| 3426 |
H2 : Forall2 (eval_idx_or_list_idx ?G2 ?d2) ?PHOAS_output ?r2 |
| 3427 |
|- _ ] |
| 3428 |
=> assert_fails constr_eq r1 r2; |
| 3429 |
assert (r1 = r2) by (eapply eval_eval_idx_or_list_idx_Forall2_gen; eassumption); |
| 3430 |
subst |
| 3431 |
| [ H := _ |- _ ] => subst H |
| 3432 |
end ]. |
| 3433 |
all: do 3 eexists; repeat first [ eassumption | apply conj ]; trivial. |
| 3434 |
Qed. |
| 3435 |
|
| 3436 |
Theorem generate_assembly_of_hinted_expr_correct |
| 3437 |
{opts : symbolic_options_computed_opt} |
| 3438 |
(output_scalars_are_pointers:=false) |
| 3439 |
{assembly_calling_registers' : assembly_calling_registers_opt} |
| 3440 |
{assembly_stack_size' : assembly_stack_size_opt} |
| 3441 |
{assembly_output_first : assembly_output_first_opt} |
| 3442 |
{assembly_argument_registers_left_to_right : assembly_argument_registers_left_to_right_opt} |
| 3443 |
{assembly_callee_saved_registers' : assembly_callee_saved_registers_opt} |
| 3444 |
{t} |
| 3445 |
(asm : list (String.string (* fname *) * Lines)) |
| 3446 |
(expr : API.Expr t) |
| 3447 |
(f : type.interp _ t) |
| 3448 |
(arg_bounds : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 3449 |
(out_bounds : ZRange.type.base.option.interp (type.final_codomain t)) |
| 3450 |
(asm_out : list (String.string (* fname *) * Lines)) |
| 3451 |
(* Phrased this way to line up with the bounds pipeline exactly *) |
| 3452 |
(Hbounded |
| 3453 |
: (forall arg1 arg2 |
| 3454 |
(Harg12 : type.and_for_each_lhs_of_arrow (@type.eqv) arg1 arg2) |
| 3455 |
(Harg1 : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) arg_bounds arg1 = true), |
| 3456 |
ZRange.type.base.option.is_bounded_by out_bounds (type.app_curried (API.Interp expr) arg1) = true |
| 3457 |
/\ type.app_curried (API.Interp expr) arg1 = type.app_curried f arg2) |
| 3458 |
/\ API.Wf expr) |
| 3459 |
(H : generate_assembly_of_hinted_expr asm expr arg_bounds out_bounds = Success asm_out) |
| 3460 |
: asm = asm_out |
| 3461 |
/\ forall (st : machine_state) |
| 3462 |
(frame : Semantics.mem_state -> Prop) |
| 3463 |
(PHOAS_args : type.for_each_lhs_of_arrow API.interp_type t) |
| 3464 |
(word_args : list (Naive.word 64 + list (Naive.word 64))) |
| 3465 |
(args := word_args_to_Z_args word_args) |
| 3466 |
(Hargs : build_input_runtime t args = Some (PHOAS_args, [])) |
| 3467 |
(HPHOAS_args : type.andb_bool_for_each_lhs_of_arrow (@ZRange.type.option.is_bounded_by) arg_bounds PHOAS_args = true) |
| 3468 |
(output_types : type_spec := match simplify_base_type (type.final_codomain t) out_bounds with Success output_types => output_types | Error _ => nil end) |
| 3469 |
(asm_args_out asm_args_in : list (Naive.word 64)) |
| 3470 |
(runtime_regs := get_asm_reg st assembly_calling_registers) |
| 3471 |
(runtime_callee_saved_registers := get_asm_reg st assembly_callee_saved_registers), |
| 3472 |
Forall |
| 3473 |
(fun '(_fname, asm) |
| 3474 |
=> forall (stack_size := N.to_nat (assembly_stack_size match strip_ret asm with Success asm => asm | Error _ => asm end)) |
| 3475 |
(stack_base := word.of_Z (Semantics.get_reg st rsp - 8 * Z.of_nat stack_size)) |
| 3476 |
(HR : R_runtime_input (output_scalars_are_pointers:=output_scalars_are_pointers) frame output_types args stack_size stack_base asm_args_out asm_args_in assembly_calling_registers runtime_regs assembly_callee_saved_registers runtime_callee_saved_registers st), |
| 3477 |
(* Should match check_equivalence_correct exactly *) |
| 3478 |
exists asm' st' (retvals : list (Z + list Z)), |
| 3479 |
strip_ret asm = Success asm' |
| 3480 |
/\ DenoteLines st asm' = Some st' |
| 3481 |
/\ simplify_base_runtime (type.app_curried (API.Interp expr) PHOAS_args) = Some retvals |
| 3482 |
/\ R_runtime_output (output_scalars_are_pointers:=output_scalars_are_pointers) frame retvals (type_spec_of_runtime args) stack_size stack_base asm_args_out asm_args_in assembly_callee_saved_registers runtime_callee_saved_registers st') |
| 3483 |
asm. |
| 3484 |
Proof. |
| 3485 |
cbv [generate_assembly_of_hinted_expr] in H. |
| 3486 |
break_innermost_match_hyps; inversion H; subst; destruct_head'_and; split; [ reflexivity | intros ]. |
| 3487 |
eapply check_equivalence_correct; eassumption. |
| 3488 |
Qed. |
| 3489 |
|
| 3490 |
(* Some theorems about the result of calling generate_assembly_of_hinted_expr_correct on various Pipeline functions *) |