| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import bedrock2.Syntax. |
| 5 |
Require Import coqutil.Tactics.Tactics. |
| 6 |
Require Import coqutil.Word.Interface coqutil.Word.Properties. |
| 7 |
Require Import Crypto.AbstractInterpretation.AbstractInterpretation. |
| 8 |
Require Import Crypto.COperationSpecifications. (* for list_Z_bounded_by *) |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 10 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 11 |
Require Import Crypto.Bedrock.Field.Translation.Proofs.LoadStoreList. |
| 12 |
Require Import Crypto.Util.ZRange. |
| 13 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 14 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 15 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 18 |
Local Open Scope Z_scope. |
| 19 |
|
| 20 |
Import ListNotations. |
| 21 |
Import AbstractInterpretation.Compilers. |
| 22 |
Import Types.Notations. |
| 23 |
|
| 24 |
Section __. |
| 25 |
Context |
| 26 |
{width BW word mem locals ext_spec varname_gen error} |
| 27 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 28 |
Existing Instances rep.Z rep.listZ_mem. |
| 29 |
|
| 30 |
Let all_access_sizes := |
| 31 |
[access_size.one; access_size.two; access_size.four; access_size.word]. |
| 32 |
|
| 33 |
Fixpoint loosest_range (ranges : list (option zrange)) : option zrange := |
| 34 |
match ranges with |
| 35 |
| [] => None |
| 36 |
| x :: ranges' => |
| 37 |
let y := loosest_range ranges' in |
| 38 |
match x, y with |
| 39 |
| Some rx, Some ry => |
| 40 |
if is_tighter_than_bool rx ry |
| 41 |
then Some ry else Some rx |
| 42 |
| Some rx, None => Some rx |
| 43 |
| _, _ => None |
| 44 |
end |
| 45 |
end. |
| 46 |
|
| 47 |
Definition access_size_to_zrange (size : access_size) : zrange := |
| 48 |
let bytes := Z.of_nat (Memory.bytes_per |
| 49 |
(width:=width) size) in |
| 50 |
{| lower:=0; upper:= (2^(bytes*8) - 1) |}. |
| 51 |
|
| 52 |
Definition zrange_to_access_size' |
| 53 |
(r : zrange) |
| 54 |
(access_sizes : list access_size) : option access_size := |
| 55 |
fold_right |
| 56 |
(fun nsize current => |
| 57 |
match current with |
| 58 |
| Some csize => |
| 59 |
let nr := access_size_to_zrange nsize in |
| 60 |
let cr := access_size_to_zrange csize in |
| 61 |
if ((is_tighter_than_bool nr cr) |
| 62 |
&& (is_tighter_than_bool r nr))%bool |
| 63 |
then Some nsize |
| 64 |
else Some csize |
| 65 |
| None => |
| 66 |
let nr := access_size_to_zrange nsize in |
| 67 |
if is_tighter_than_bool r nr |
| 68 |
then Some nsize else None |
| 69 |
end |
| 70 |
) None access_sizes. |
| 71 |
Definition zrange_to_access_size r := |
| 72 |
zrange_to_access_size' r all_access_sizes. |
| 73 |
|
| 74 |
Definition make_access_size (ranges : list (option zrange)) |
| 75 |
: option access_size := |
| 76 |
match loosest_range ranges with |
| 77 |
| Some r => |
| 78 |
let wordr := access_size_to_zrange access_size.word in |
| 79 |
if is_tighter_than_bool r wordr |
| 80 |
then |
| 81 |
match zrange_to_access_size r with |
| 82 |
| Some s => |
| 83 |
let sr := access_size_to_zrange s in |
| 84 |
if zrange_beq sr wordr |
| 85 |
then Some access_size.word (* prioritize word over equivalent sizes (e.g. over access_size.four on 32-bit) *) |
| 86 |
else Some s |
| 87 |
| None => None |
| 88 |
end |
| 89 |
else None |
| 90 |
| None => None |
| 91 |
end. |
| 92 |
|
| 93 |
Fixpoint make_base_access_sizes {t} : |
| 94 |
ZRange.type.base.option.interp t -> |
| 95 |
option (base_access_sizes t) := |
| 96 |
match t as t0 return |
| 97 |
ZRange.type.base.option.interp t0 -> |
| 98 |
option (base_access_sizes t0) with |
| 99 |
| base.type.prod a b => |
| 100 |
fun r => |
| 101 |
match make_base_access_sizes (fst r), |
| 102 |
make_base_access_sizes (snd r) with |
| 103 |
| Some s1, Some s2 => Some (s1, s2) |
| 104 |
| _, _ => None |
| 105 |
end |
| 106 |
| base_listZ => |
| 107 |
fun r => |
| 108 |
match r with |
| 109 |
| Some ranges => make_access_size ranges |
| 110 |
| None => None |
| 111 |
end |
| 112 |
| _ => fun _ => Some tt |
| 113 |
end. |
| 114 |
|
| 115 |
Fixpoint make_access_sizes_args {t} : |
| 116 |
type.for_each_lhs_of_arrow |
| 117 |
ZRange.type.option.interp t -> |
| 118 |
option (type.for_each_lhs_of_arrow |
| 119 |
access_sizes t) := |
| 120 |
match t with |
| 121 |
| type.base _ => fun _ => Some tt |
| 122 |
| type.arrow (type.base s) d => |
| 123 |
fun r => |
| 124 |
match make_base_access_sizes (fst r), |
| 125 |
make_access_sizes_args (snd r) with |
| 126 |
| Some ssizes, Some dsizes => Some (ssizes, dsizes) |
| 127 |
| _, _ => None |
| 128 |
end |
| 129 |
| _ => fun _ => None |
| 130 |
end. |
| 131 |
|
| 132 |
Fixpoint access_sizes_repeat_base (s : access_size) t |
| 133 |
: base_access_sizes t := |
| 134 |
match t as t0 return base_access_sizes t0 with |
| 135 |
| base.type.prod a b => |
| 136 |
(access_sizes_repeat_base s a, access_sizes_repeat_base s b) |
| 137 |
| base_listZ => s |
| 138 |
| _ => tt |
| 139 |
end. |
| 140 |
Fixpoint access_sizes_repeat_args (sz : access_size) t |
| 141 |
: type.for_each_lhs_of_arrow access_sizes t := |
| 142 |
match t as t0 return type.for_each_lhs_of_arrow access_sizes t0 with |
| 143 |
| type.base b => tt |
| 144 |
| type.arrow (type.base s) d => |
| 145 |
(access_sizes_repeat_base sz s, access_sizes_repeat_args sz d) |
| 146 |
| type.arrow s d => (tt, access_sizes_repeat_args sz d) |
| 147 |
end. |
| 148 |
|
| 149 |
Section proofs. |
| 150 |
Context {ok : Types.ok}. |
| 151 |
|
| 152 |
Lemma access_size_to_zrange_lower s : |
| 153 |
lower (access_size_to_zrange s) = 0. |
| 154 |
Proof. reflexivity. Qed. |
| 155 |
|
| 156 |
Lemma is_tighter_than_bool_reverse_same_lower r1 r2 : |
| 157 |
lower r1 = lower r2 -> |
| 158 |
(r1 <=? r2)%zrange = false -> |
| 159 |
(r2 <=? r1)%zrange = true. |
| 160 |
Proof. |
| 161 |
cbv [is_tighter_than_bool]. |
| 162 |
rewrite Bool.andb_false_iff, Bool.andb_true_iff. |
| 163 |
intros; DestructHead.destruct_head'_or; split; |
| 164 |
Z.ltb_to_lt; try lia. |
| 165 |
Qed. |
| 166 |
|
| 167 |
Lemma is_tighter_than_bool_reverse_access_size s1 s2 : |
| 168 |
(access_size_to_zrange s1 |
| 169 |
<=? access_size_to_zrange s2)%zrange = false -> |
| 170 |
(access_size_to_zrange s2 |
| 171 |
<=? access_size_to_zrange s1)%zrange = true. |
| 172 |
Proof. |
| 173 |
apply is_tighter_than_bool_reverse_same_lower. |
| 174 |
rewrite !access_size_to_zrange_lower; auto. |
| 175 |
Qed. |
| 176 |
|
| 177 |
Lemma zrange_to_access_size'_correct r : |
| 178 |
forall sizes, |
| 179 |
(zrange_to_access_size' r sizes = None |
| 180 |
/\ (forall sz', |
| 181 |
In sz' sizes -> |
| 182 |
~ is_true (r <=? access_size_to_zrange sz')%zrange)) |
| 183 |
\/ (exists sz, |
| 184 |
zrange_to_access_size' r sizes = Some sz |
| 185 |
/\ In sz sizes |
| 186 |
/\ (forall sz', |
| 187 |
In sz' sizes -> |
| 188 |
is_true (r <=? access_size_to_zrange sz')%zrange -> |
| 189 |
is_true (access_size_to_zrange sz <=? access_size_to_zrange sz')%zrange)). |
| 190 |
Proof. |
| 191 |
cbv [zrange_to_access_size']. |
| 192 |
induction sizes; |
| 193 |
repeat match goal with |
| 194 |
| _ => progress intros |
| 195 |
| _ => progress cbn [fold_right] in * |
| 196 |
| H : _ /\ _ |- _ => destruct H |
| 197 |
| H : In _ [] |- _ => solve [inversion H] |
| 198 |
| H : In _ (_ :: _) |- _ => |
| 199 |
inversion H; clear H; subst |
| 200 |
| H : Forall _ (_ :: _) |- _ => |
| 201 |
inversion H; clear H; subst |
| 202 |
| H : Some _ = Some _ |- _ => |
| 203 |
inversion H; clear H; subst |
| 204 |
| H : (_ && _)%bool = true |- _ => |
| 205 |
apply Bool.andb_true_iff in H; destruct H |
| 206 |
| H : (_ && _)%bool = false |- _ => |
| 207 |
apply Bool.andb_false_iff in H; destruct H |
| 208 |
| _ => progress break_match |
| 209 |
| |- (Some _ = None /\ _) \/ _ => right |
| 210 |
| |- (None = None /\ _) \/ _ => left |
| 211 |
| |- exists x, Some ?y = Some x /\ _ => |
| 212 |
exists y; split; [ reflexivity | ] |
| 213 |
| |- _ /\ _ => split |
| 214 |
| H : (Some _ = None /\ _) \/ exists _, _ |- _ => |
| 215 |
destruct H as [ [? ?] |H]; [ congruence | destruct H ] |
| 216 |
| H : _ \/ exists _, None = Some _ /\ _ |- _ => |
| 217 |
destruct H as [H|H]; |
| 218 |
[ | destruct H as [? [? ?] ]; congruence ] |
| 219 |
| H : forall _, In _ _ -> ~ _ |- _ => |
| 220 |
specialize (H _ ltac:(eassumption)); contradiction |
| 221 |
| _ => reflexivity |
| 222 |
| _ => congruence |
| 223 |
| _ => solve [auto using |
| 224 |
in_eq, in_cons, |
| 225 |
is_tighter_than_bool_reverse_access_size] |
| 226 |
| _ => etransitivity; [ eassumption | ]; solve [auto] |
| 227 |
end. |
| 228 |
Qed. |
| 229 |
|
| 230 |
Lemma zrange_to_access_size_tighter_than_word r : |
| 231 |
is_true (r <=? access_size_to_zrange access_size.word)%zrange -> |
| 232 |
forall sz, |
| 233 |
zrange_to_access_size r = Some sz -> |
| 234 |
is_true (access_size_to_zrange sz |
| 235 |
<=? access_size_to_zrange access_size.word)%zrange. |
| 236 |
Proof. |
| 237 |
cbv [zrange_to_access_size]. intros. |
| 238 |
pose proof (zrange_to_access_size'_correct r all_access_sizes). |
| 239 |
assert (In access_size.word all_access_sizes) by (cbn; tauto). |
| 240 |
repeat match goal with |
| 241 |
| H: _ /\ _ |- _ => destruct H |
| 242 |
| H: _ \/ _ |- _ => destruct H |
| 243 |
| H: exists _, _ |- _ => destruct H |
| 244 |
| H1: ?x = Some ?y1, H2 : ?x = Some ?y2 |- _ => |
| 245 |
assert (y1 = y2) by congruence; subst; clear H1 H2 |
| 246 |
| _ => congruence |
| 247 |
| _ => solve [auto] |
| 248 |
end. |
| 249 |
Qed. |
| 250 |
|
| 251 |
Lemma make_access_size_tighter_than_word ranges size : |
| 252 |
make_access_size ranges = Some size -> |
| 253 |
is_true (access_size_to_zrange size |
| 254 |
<=? access_size_to_zrange access_size.word)%zrange. |
| 255 |
Proof. |
| 256 |
cbv [make_access_size]. |
| 257 |
break_match; try congruence; [ | ]; intros; |
| 258 |
match goal with |
| 259 |
| H : Some _ = Some _ |- _ => |
| 260 |
inversion H; clear H; subst |
| 261 |
end; |
| 262 |
try reflexivity; |
| 263 |
eauto using zrange_to_access_size_tighter_than_word. |
| 264 |
Qed. |
| 265 |
|
| 266 |
Lemma bits_per_word_eq_width : |
| 267 |
(Z.of_nat (Memory.bytes_per |
| 268 |
(width:=width) access_size.word) * 8 |
| 269 |
= width). |
| 270 |
Proof. |
| 271 |
destruct Bitwidth.width_cases; subst; cbv; trivial. |
| 272 |
Qed. |
| 273 |
|
| 274 |
Lemma bits_per_word_le_width : |
| 275 |
(Z.of_nat (Memory.bytes_per |
| 276 |
(width:=width) access_size.word) * 8 |
| 277 |
<= width). |
| 278 |
Proof. |
| 279 |
rewrite bits_per_word_eq_width; reflexivity. |
| 280 |
Qed. |
| 281 |
|
| 282 |
Lemma bytes_per_word_eq : |
| 283 |
Z.to_nat (Memory.bytes_per_word width) |
| 284 |
= Memory.bytes_per (width:=width) access_size.word. |
| 285 |
Proof. reflexivity. Qed. |
| 286 |
|
| 287 |
Lemma make_access_size_good ranges size : |
| 288 |
make_access_size ranges = Some size -> |
| 289 |
(Z.of_nat (Memory.bytes_per |
| 290 |
(width:=width) size) * 8 |
| 291 |
<= width). |
| 292 |
Proof. |
| 293 |
intros. |
| 294 |
pose proof word.width_pos. |
| 295 |
pose proof make_access_size_tighter_than_word ranges size |
| 296 |
ltac:(assumption). |
| 297 |
eapply Z.le_trans; [ | apply bits_per_word_le_width ]. |
| 298 |
cbv [is_true is_tighter_than_bool] in *. |
| 299 |
cbn [lower upper access_size_to_zrange] in *. |
| 300 |
match goal with |
| 301 |
| H : (_ && _)%bool = true |- _ => |
| 302 |
apply Bool.andb_true_iff in H; destruct H |
| 303 |
end; Z.ltb_to_lt. |
| 304 |
apply (Z.pow_le_mono_r_iff 2); lia. |
| 305 |
Qed. |
| 306 |
|
| 307 |
Lemma make_base_access_sizes_good t bounds sizes : |
| 308 |
make_base_access_sizes bounds = Some sizes -> |
| 309 |
base_access_sizes_good (t:=t) sizes. |
| 310 |
Proof. |
| 311 |
induction t; |
| 312 |
repeat match goal with |
| 313 |
| _ => progress intros |
| 314 |
| _ => progress |
| 315 |
cbn [fst snd make_base_access_sizes |
| 316 |
base_access_sizes_good] in * |
| 317 |
| _ => progress break_match_hyps |
| 318 |
| H : Some _ = Some _ |- _ => |
| 319 |
inversion H; clear H; subst |
| 320 |
| H : None = Some _ |- _ => |
| 321 |
solve [inversion H] |
| 322 |
| _ => progress ssplit |
| 323 |
| H : _ |- _ => eapply H; solve [eauto] |
| 324 |
| |- True => tauto |
| 325 |
| _ => solve [eauto using make_access_size_good] |
| 326 |
end. |
| 327 |
Qed. |
| 328 |
|
| 329 |
Lemma make_access_sizes_args_good t bounds sizes : |
| 330 |
make_access_sizes_args bounds = Some sizes -> |
| 331 |
access_sizes_good_args (t:=t) sizes. |
| 332 |
Proof. |
| 333 |
induction t; |
| 334 |
repeat match goal with |
| 335 |
| _ => progress intros |
| 336 |
| _ => progress |
| 337 |
cbn [fst snd make_access_sizes_args |
| 338 |
access_sizes_good_args] in * |
| 339 |
| H : Some _ = Some _ |- _ => |
| 340 |
inversion H; clear H; subst |
| 341 |
| H : None = Some _ |- _ => congruence |
| 342 |
| _ => progress break_match_hyps |
| 343 |
| |- _ /\ _ => split |
| 344 |
| |- True => tauto |
| 345 |
| _ => eapply make_base_access_sizes_good; |
| 346 |
eassumption |
| 347 |
| H : _ |- _ => eapply H; solve [eauto] |
| 348 |
end. |
| 349 |
Qed. |
| 350 |
|
| 351 |
(* useful for proving byte access sizes are legal *) |
| 352 |
Lemma width_ge_8 : 8 <= width. |
| 353 |
Proof. |
| 354 |
pose proof word.width_pos. |
| 355 |
pose proof width_0mod_8. |
| 356 |
let H := fresh in |
| 357 |
let x := fresh in |
| 358 |
pose proof width_0mod_8 as H; apply Z.mod_divide in H; |
| 359 |
[ | lia ]; destruct H as [x ?]; |
| 360 |
destruct (Z.eq_dec x 0); subst; lia. |
| 361 |
Qed. |
| 362 |
End proofs. |
| 363 |
End __. |