| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import NArith. |
| 3 |
From Coq Require Import String. |
| 4 |
From Coq Require Import List. |
| 5 |
From Coq Require Import Derive. |
| 6 |
Require Import Crypto.Util.Prod. |
| 7 |
Require Import Crypto.Util.Option. |
| 8 |
Require Import Crypto.Util.Bool.Reflect. |
| 9 |
Require Import Crypto.Util.Listable. |
| 10 |
Require Import Crypto.Util.ListUtil. |
| 11 |
Require Import Crypto.Assembly.Syntax. |
| 12 |
Require Crypto.Util.Tuple. |
| 13 |
Require Crypto.Util.OptionList. |
| 14 |
Import ListNotations. |
| 15 |
|
| 16 |
Local Open Scope list_scope. |
| 17 |
|
| 18 |
Local Set Implicit Arguments. |
| 19 |
Local Set Primitive Projections. |
| 20 |
|
| 21 |
Local Coercion N.of_nat : nat >-> N. |
| 22 |
|
| 23 |
Lemma reg_of_index_and_shift_and_bitcount_opt_of_index_and_shift_and_bitcount_of_reg : forall r : REG, reg_of_index_and_shift_and_bitcount_opt (index_and_shift_and_bitcount_of_reg r) = Some r. |
| 24 |
Proof. destruct r; vm_compute; try reflexivity. Defined. |
| 25 |
|
| 26 |
Lemma reg_of_index_and_shift_and_bitcount_of_index_and_shift_and_bitcount_of_reg : forall r : REG, reg_of_index_and_shift_and_bitcount (index_and_shift_and_bitcount_of_reg r) = r. |
| 27 |
Proof. destruct r; vm_compute; reflexivity. Defined. |
| 28 |
|
| 29 |
Lemma reg_index_widest_register_of : forall r : REG, reg_index (widest_register_of r) = reg_index r. |
| 30 |
Proof. destruct r; reflexivity. Defined. |
| 31 |
|
| 32 |
Lemma reg_index_widest_register_of_index_opt : forall index : N, (N.to_nat index <? List.length widest_registers) = true -> option_map reg_index (widest_register_of_index_opt index) = Some index. |
| 33 |
Proof. |
| 34 |
intros index; cbv [widest_register_of_index_opt]. |
| 35 |
rewrite <- (N2Nat.id index), Nat2N.id; generalize (N.to_nat index); clear index; intro index. |
| 36 |
vm_compute List.map. |
| 37 |
vm_compute List.length. |
| 38 |
cbv [Nat.ltb]; cbn [Nat.leb]. |
| 39 |
repeat lazymatch goal with |
| 40 |
| [ |- false = true -> _ ] => discriminate |
| 41 |
| [ |- (?index <=? _) = true -> _ ] => |
| 42 |
is_var index; destruct index; [ reflexivity | cbn [Nat.leb] ] |
| 43 |
end. |
| 44 |
Qed. |
| 45 |
|
| 46 |
Lemma widest_register_of_index_opt_Some_length_iff : forall index : N, (exists r, widest_register_of_index_opt index = Some r) <-> (N.to_nat index <? List.length widest_registers) = true. |
| 47 |
Proof. |
| 48 |
intros index; cbv [widest_register_of_index_opt]. |
| 49 |
rewrite <- (N2Nat.id index), Nat2N.id; generalize (N.to_nat index); clear index; intro index. |
| 50 |
vm_compute List.map. |
| 51 |
vm_compute List.length. |
| 52 |
cbv [Nat.ltb]; cbn [Nat.leb]. |
| 53 |
repeat lazymatch goal with |
| 54 |
| [ |- (exists r, nth_error nil ?index = Some _) <-> _ ] => is_var index; destruct index; cbn [nth_error] |
| 55 |
| [ |- _ <-> false = true ] => split; [ | discriminate ] |
| 56 |
| [ |- _ <-> true = true ] => repeat esplit |
| 57 |
| [ |- _ <-> (?index <=? _) = true ] => |
| 58 |
is_var index; destruct index; cbn [Nat.leb nth_error] |
| 59 |
end. |
| 60 |
all: intros [? H]; discriminate. |
| 61 |
Qed. |
| 62 |
|
| 63 |
Lemma reg_index_widest_register_of_index : forall index : N, (N.to_nat index <? List.length widest_registers) = true -> reg_index (widest_register_of_index index) = index. |
| 64 |
Proof. |
| 65 |
intros index H; cbv [widest_register_of_index]. |
| 66 |
apply reg_index_widest_register_of_index_opt in H. |
| 67 |
destruct widest_register_of_index_opt; cbv [option_map] in *; inversion H; subst. |
| 68 |
reflexivity. |
| 69 |
Qed. |
| 70 |
|
| 71 |
Lemma reg_index_overlapping_registers : forall r r' n, nth_error (overlapping_registers r) n = Some r' -> reg_index r' = reg_index r. |
| 72 |
Proof. |
| 73 |
intros r r' n; destruct r. |
| 74 |
all: vm_compute overlapping_registers. |
| 75 |
all: repeat lazymatch goal with |
| 76 |
| [ |- nth_error (_ :: _) ?v = Some _ -> _ ] => is_var v; destruct v; cbn [nth_error] |
| 77 |
| [ |- nth_error [] ?v = Some _ -> _ ] => is_var v; destruct v; cbn [nth_error] |
| 78 |
| [ |- Some _ = Some _ -> _ ] => let H := fresh in intro H; inversion H |
| 79 |
| [ |- None = Some _ -> _ ] => let H := fresh in intro H; inversion H |
| 80 |
end. |
| 81 |
all: subst; reflexivity. |
| 82 |
Qed. |
| 83 |
|
| 84 |
Lemma reg_of_index_and_shift_and_bitcount_of_reg r |
| 85 |
: reg_of_index_and_shift_and_bitcount (index_and_shift_and_bitcount_of_reg r) = r. |
| 86 |
Proof. destruct r; vm_compute; reflexivity. Qed. |
| 87 |
|
| 88 |
Lemma widest_register_of_index_opt_correct |
| 89 |
: forall n r, widest_register_of_index_opt n = Some r -> |
| 90 |
reg_index r = n |
| 91 |
/\ forall r', reg_index r' = n -> r = r' \/ (reg_size r' < reg_size r)%N. |
| 92 |
Proof. |
| 93 |
intros n r H. |
| 94 |
epose proof (proj1 (widest_register_of_index_opt_Some_length_iff _) (ex_intro _ _ H)) as H'. |
| 95 |
pose proof H' as H''. |
| 96 |
apply reg_index_widest_register_of_index_opt in H''. |
| 97 |
rewrite H in H''; cbn in H''; inversion H''; subst. |
| 98 |
split; [ reflexivity | ]. |
| 99 |
destruct r, r'. |
| 100 |
all: vm_compute; try (constructor; reflexivity); try discriminate. |
| 101 |
Qed. |
| 102 |
|
| 103 |
Lemma widest_register_of_index_correct |
| 104 |
: forall n, |
| 105 |
(~exists r, reg_index r = n) |
| 106 |
\/ (let r := widest_register_of_index n in reg_index r = n |
| 107 |
/\ forall r', reg_index r' = n -> r = r' \/ (reg_size r' < reg_size r)%N). |
| 108 |
Proof. |
| 109 |
intro n; pose proof (widest_register_of_index_opt_correct n) as H. |
| 110 |
cbv [widest_register_of_index]. |
| 111 |
destruct (widest_register_of_index_opt n) as [r |] eqn:H'; [ right; apply H; reflexivity | left ]. |
| 112 |
intros [ [] H'' ]; subst; cbv in H'. |
| 113 |
all: inversion H'. |
| 114 |
Qed. |
| 115 |
|
| 116 |
Lemma reg_of_index_and_shift_and_bitcount_opt_correct v r |
| 117 |
: reg_of_index_and_shift_and_bitcount_opt v = Some r <-> index_and_shift_and_bitcount_of_reg r = v. |
| 118 |
Proof. |
| 119 |
split; [ | intro; subst; destruct r; vm_compute; reflexivity ]. |
| 120 |
cbv [index_and_shift_and_bitcount_of_reg]; destruct v as [ [index shift] bitcount ]. |
| 121 |
cbv [reg_of_index_and_shift_and_bitcount_opt]. |
| 122 |
pose proof (reg_index_widest_register_of_index index) as H''. |
| 123 |
cbv [widest_register_of_index] in H''. |
| 124 |
rewrite <- widest_register_of_index_opt_Some_length_iff in H''. |
| 125 |
destruct widest_register_of_index_opt eqn:H; [ | intro H'; cbv in H'; now inversion H' ]. |
| 126 |
cbv [Option.bind Option.sequence_return] in *. |
| 127 |
specialize (H'' (ex_intro _ _ eq_refl)). |
| 128 |
subst. |
| 129 |
rewrite find_some_iff. |
| 130 |
repeat first |
| 131 |
[ progress intros |
| 132 |
| progress destruct_head'_ex |
| 133 |
| progress destruct_head'_and |
| 134 |
| progress reflect_hyps |
| 135 |
| progress subst |
| 136 |
| match goal with |
| 137 |
| [ H : nth_error (overlapping_registers _) _ = Some _ |- _ ] => |
| 138 |
apply reg_index_overlapping_registers in H; try rewrite H |
| 139 |
end |
| 140 |
| reflexivity ]. |
| 141 |
Qed. |
| 142 |
|
| 143 |
Lemma reg_of_index_and_shift_and_bitcount_eq v r |
| 144 |
: reg_of_index_and_shift_and_bitcount v = r |
| 145 |
-> (index_and_shift_and_bitcount_of_reg r = v |
| 146 |
\/ ((~exists r, index_and_shift_and_bitcount_of_reg r = v) |
| 147 |
/\ r = widest_register_of_index (fst (fst v)))). |
| 148 |
Proof. |
| 149 |
cbv [reg_of_index_and_shift_and_bitcount]. |
| 150 |
destruct v as [ [index offset] size ]. |
| 151 |
destruct reg_of_index_and_shift_and_bitcount_opt eqn:H; |
| 152 |
[ left | right; split; [ intros [r' H'] | ] ]; subst; try reflexivity. |
| 153 |
{ rewrite reg_of_index_and_shift_and_bitcount_opt_correct in H; assumption. } |
| 154 |
{ rewrite <- reg_of_index_and_shift_and_bitcount_opt_correct in H'; congruence. } |
| 155 |
Qed. |