| 1 |
From Coq Require Import Utf8. |
| 2 |
Require Import Rupicola.Lib.Api. |
| 3 |
Require Import Rupicola.Lib.Loops. |
| 4 |
|
| 5 |
From Coq Require Init.Byte String. |
| 6 |
From Coq Require Import Init.Byte(byte(..)). |
| 7 |
Import String. |
| 8 |
Require Import coqutil.Datatypes.List. Import Lists.List List.ListNotations. |
| 9 |
From Coq Require Import BinInt. Import Zdiv. Local Open Scope Z_scope. |
| 10 |
Require Import coqutil.Byte coqutil.Word.LittleEndianList. |
| 11 |
|
| 12 |
(* TODO: move into core Bedrock *) |
| 13 |
Require Import Crypto.Bedrock.Field.Common.Util. |
| 14 |
|
| 15 |
Import Syntax.Coercions ProgramLogic.Coercions. |
| 16 |
Import Datatypes. |
| 17 |
|
| 18 |
|
| 19 |
Open Scope Z_scope. |
| 20 |
|
| 21 |
|
| 22 |
(*TODO: move to Rupicola.Lib *) |
| 23 |
|
| 24 |
|
| 25 |
(*TODO: this statement is specialized to its use below. |
| 26 |
Should it be generalized? |
| 27 |
*) |
| 28 |
Lemma skipn_nth_0 {A} (i : nat) (l : list A) d |
| 29 |
: i < length l -> skipn i l = (nth 0 (skipn i l) d):: skipn (S i) l. |
| 30 |
Proof using. |
| 31 |
change (S i) with (1+i)%nat. |
| 32 |
rewrite <- skipn_skipn. |
| 33 |
intros. |
| 34 |
assert (0 < length (skipn i l)) by (rewrite length_skipn;lia). |
| 35 |
revert H0; generalize (skipn i l). |
| 36 |
intros. |
| 37 |
destruct l0; simpl in *; [lia|]. |
| 38 |
reflexivity. |
| 39 |
Qed. |
| 40 |
|
| 41 |
|
| 42 |
Lemma length_fold_left_invariant {A B} (l : list A) (acc : list B) f |
| 43 |
: (forall a acc, length (f acc a) = length acc) -> |
| 44 |
length (fold_left f l acc) = length acc. |
| 45 |
Proof. |
| 46 |
revert acc; induction l; simpl; intros; auto. |
| 47 |
rewrite IHl; congruence. |
| 48 |
Qed. |
| 49 |
|
| 50 |
|
| 51 |
Lemma upd_firstn_skipn A (l old : list A) (start : nat) d |
| 52 |
: start < length l -> |
| 53 |
start < length old -> |
| 54 |
upd (firstn start l ++ skipn start old) start (nth start l d) |
| 55 |
= (firstn (S start) l ++ skipn (S start) old). |
| 56 |
Proof. |
| 57 |
intros. |
| 58 |
unfold upd, upds. |
| 59 |
repeat rewrite !firstn_app, !skipn_app, |
| 60 |
!app_length, |
| 61 |
!firstn_length, |
| 62 |
!skipn_length, |
| 63 |
!firstn_firstn, |
| 64 |
!length_cons, |
| 65 |
!length_nil, |
| 66 |
!Nat.min_id. |
| 67 |
rewrite !min_l by lia. |
| 68 |
rewrite app_assoc. |
| 69 |
f_equal. |
| 70 |
{ |
| 71 |
erewrite <- firstn_nth by lia. |
| 72 |
f_equal. |
| 73 |
{ |
| 74 |
replace (start - start)%nat with 0%nat by lia. |
| 75 |
simpl. |
| 76 |
rewrite app_nil_r. |
| 77 |
reflexivity. |
| 78 |
} |
| 79 |
{ |
| 80 |
rewrite firstn_all2; eauto. |
| 81 |
simpl. |
| 82 |
lia. |
| 83 |
} |
| 84 |
} |
| 85 |
{ |
| 86 |
rewrite skipn_all2. |
| 87 |
{ |
| 88 |
rewrite skipn_skipn. |
| 89 |
replace (1 + start - start + start)%nat with (S start) by lia. |
| 90 |
reflexivity. |
| 91 |
} |
| 92 |
{ |
| 93 |
rewrite firstn_length. |
| 94 |
rewrite min_l; lia. |
| 95 |
} |
| 96 |
} |
| 97 |
Qed. |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
Lemma list_as_nd_ranged_for_all_helper {A} (l old : list A) d |
| 102 |
(measure len start : nat) |
| 103 |
: measure = (len - start)%nat -> |
| 104 |
length l = len -> |
| 105 |
length old = len -> |
| 106 |
fold_left (λ (old0 : list A) (i : Z), upd old0 (Z.to_nat i) (nth (Z.to_nat i) l d)) (z_range start (Z.of_nat len)) ((firstn start l) ++ (skipn start old)) |
| 107 |
= l. |
| 108 |
Proof. |
| 109 |
revert start len l old. |
| 110 |
induction measure; simpl; intros. |
| 111 |
{ |
| 112 |
assert (start >=len) by lia. |
| 113 |
rewrite z_range_nil by lia. |
| 114 |
rewrite firstn_all2 by lia. |
| 115 |
rewrite skipn_all2 by lia. |
| 116 |
rewrite app_nil_r. |
| 117 |
reflexivity. |
| 118 |
} |
| 119 |
{ |
| 120 |
rewrite z_range_cons by lia. |
| 121 |
simpl. |
| 122 |
rewrite Nat2Z.id. |
| 123 |
rewrite upd_firstn_skipn by lia. |
| 124 |
replace (Z.of_nat start + 1) with (Z.of_nat (S start)) by lia. |
| 125 |
rewrite IHmeasure; eauto. |
| 126 |
lia. |
| 127 |
} |
| 128 |
Qed. |
| 129 |
|
| 130 |
Lemma list_as_nd_ranged_for_all {A} (l old : list A) d len |
| 131 |
: length l = len -> |
| 132 |
length old = len -> |
| 133 |
nd_ranged_for_all 0 len (fun old i => upd old (Z.to_nat i) (nth (Z.to_nat i) l d)) old = l. |
| 134 |
Proof. |
| 135 |
rewrite <- fold_left_as_nd_ranged_for_all. |
| 136 |
intros. |
| 137 |
change old with ((firstn 0 l) ++ (skipn 0 old)). |
| 138 |
replace 0 with (Z.of_nat 0%nat) by lia. |
| 139 |
eapply list_as_nd_ranged_for_all_helper; eauto. |
| 140 |
Qed. |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
Section with_parameters. |
| 145 |
Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}. |
| 146 |
Context {locals: map.map String.string word}. |
| 147 |
Context {ext_spec: bedrock2.Semantics.ExtSpec}. |
| 148 |
Context {word_ok : word.ok word} {mem_ok : map.ok mem}. |
| 149 |
Context {locals_ok : map.ok locals}. |
| 150 |
Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}. |
| 151 |
|
| 152 |
|
| 153 |
(*TODO: should be in core bedrock*) |
| 154 |
Lemma expr_locals_put m l x v exp (P : word -> Prop) |
| 155 |
: map.get l x = None -> |
| 156 |
WeakestPrecondition.expr m l exp P -> |
| 157 |
WeakestPrecondition.expr m (map.put l x v) exp P. |
| 158 |
Proof using locals_ok word_ok. |
| 159 |
intros. |
| 160 |
eapply Util.expr_only_differ_undef; eauto. |
| 161 |
eapply Util.only_differ_sym; eauto. |
| 162 |
eapply Util.only_differ_put; eauto. |
| 163 |
unfold map.undef_on, PropSet.singleton_set. |
| 164 |
unfold map.agree_on. |
| 165 |
intros. |
| 166 |
cbv in H1; subst. |
| 167 |
rewrite map.get_empty; auto. |
| 168 |
Qed. |
| 169 |
|
| 170 |
Lemma dexpr_locals_put m l x v exp (w : word) |
| 171 |
: map.get l x = None -> |
| 172 |
DEXPR m l exp w -> |
| 173 |
DEXPR m (map.put l x v) exp w. |
| 174 |
Proof using locals_ok word_ok. |
| 175 |
intros. |
| 176 |
eapply expr_locals_put; eauto. |
| 177 |
Qed. |
| 178 |
|
| 179 |
Class FitsInLocal T : Type := |
| 180 |
{ |
| 181 |
predT : word -> T -> mem -> Prop; |
| 182 |
szT : access_size; |
| 183 |
word_of_T : T -> word; |
| 184 |
}. |
| 185 |
(*TODO: properties*) |
| 186 |
Class FitsInLocal_ok T `(FitsInLocal T) : Prop := |
| 187 |
{ |
| 188 |
truncate_of_T : forall t, truncate_word szT (word_of_T t) = word_of_T t; |
| 189 |
predT_to_truncated_word |
| 190 |
: forall ptr t, |
| 191 |
Lift1Prop.iff1 (predT ptr t) (truncated_word szT ptr (word_of_T t)); |
| 192 |
}. |
| 193 |
|
| 194 |
Section WithAccessSize. |
| 195 |
Context (T : Type) {T_Fits: FitsInLocal T} |
| 196 |
{T_Fits_ok : @FitsInLocal_ok T T_Fits} |
| 197 |
`{HasDefault T}. |
| 198 |
Existing Instance T_Fits. |
| 199 |
|
| 200 |
Notation predT := (@predT T T_Fits). |
| 201 |
Notation szT := (@szT T T_Fits). |
| 202 |
Notation word_of_T := (@word_of_T T T_Fits). |
| 203 |
|
| 204 |
|
| 205 |
Declare Scope word_scope. |
| 206 |
Delimit Scope word_scope with word. |
| 207 |
Local Infix "+" := word.add : word_scope. |
| 208 |
Local Infix "*" := word.mul : word_scope. |
| 209 |
|
| 210 |
|
| 211 |
Definition sz_word : word := (word.of_Z (Z.of_nat (Memory.bytes_per (width:=width) szT))). |
| 212 |
|
| 213 |
Local Notation "xs $@ a" := |
| 214 |
(array predT sz_word a%word xs%list) (at level 10, format "xs $@ a"). |
| 215 |
|
| 216 |
(* Array broadcasting/pointwise expressions. |
| 217 |
Requires some care to make sure that the array being written to can also be read. |
| 218 |
allows more things to be written in a expr style rather than a statement style, |
| 219 |
avoiding dealing directly with a pile of loop invariant inference. |
| 220 |
*) |
| 221 |
|
| 222 |
(* TODO: could add knowledge about lstl, but it's not yet necessary *) |
| 223 |
(* TODO: m not mentioned? *) |
| 224 |
(* expr expect idx_var to hold the index |
| 225 |
*) |
| 226 |
(* |
| 227 |
TODO: generalize to things that don't fit in a word by allocating scratch space |
| 228 |
*) |
| 229 |
Definition broadcast_expr |
| 230 |
l idx_var |
| 231 |
old_data ptr R |
| 232 |
(expr : expr.expr) (lst : list T) := |
| 233 |
forall (m : mem) (lstl : list T), |
| 234 |
length lstl < length old_data -> |
| 235 |
((lstl ++ skipn (length lstl) old_data)$@ptr * R)%sep m -> |
| 236 |
DEXPR m (map.put l idx_var (word.of_Z (length lstl))) |
| 237 |
expr (word_of_T (nth (length lstl) lst default)). |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
Lemma ranged_for'_length_helper (from' : nat) scratch lst |
| 242 |
: length (snd (ranged_for' 0 from' |
| 243 |
(λ (acc : list T) (tok : ExitToken.t) (idx : Z) (_ : 0 - 1 < idx < from'), |
| 244 |
(tok, upd acc (Z.to_nat idx) (nth (Z.to_nat idx) lst default))) scratch : |
| 245 |
ExitToken.t * list T)) = length scratch. |
| 246 |
Proof. |
| 247 |
revert scratch lst. |
| 248 |
induction from'. |
| 249 |
{ |
| 250 |
simpl. |
| 251 |
lia. |
| 252 |
} |
| 253 |
{ |
| 254 |
intros. |
| 255 |
replace (Z.of_nat (S from')) with ((Z.of_nat from') + 1) by lia. |
| 256 |
erewrite ranged_for'_unfold_r; try lia. |
| 257 |
simpl. |
| 258 |
set (fst _). |
| 259 |
destruct y; simpl; eauto. |
| 260 |
rewrite upd_length. |
| 261 |
eauto. |
| 262 |
} |
| 263 |
Qed. |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
Lemma ranged_for'_invariant {A} n m (f : _ -> _ -> _ -> _) acc (P : A -> Prop) |
| 268 |
: (forall acc' tok idx, fst (f acc' tok idx) = tok) -> |
| 269 |
P acc -> |
| 270 |
(forall acc' tok idx, n - 1 < idx < m -> P acc' -> P (snd (f acc' tok idx))) -> |
| 271 |
P (snd (nd_ranged_for' n m f acc)). |
| 272 |
Proof. |
| 273 |
intro Htok. |
| 274 |
unfold nd_ranged_for'. |
| 275 |
unfold nd_ranged_for_break. |
| 276 |
intros H0 Hn. |
| 277 |
revert acc H0. |
| 278 |
pose proof (z_range_sound n m) as H'; revert H'. |
| 279 |
generalize ((z_range n m)). |
| 280 |
|
| 281 |
induction l; simpl; intros; eauto. |
| 282 |
specialize (IHl ltac:(eauto)). |
| 283 |
specialize (H' a ltac:(tauto)). |
| 284 |
rewrite (surjective_pairing (f acc ExitToken.new a)). |
| 285 |
rewrite Htok. |
| 286 |
eapply IHl. |
| 287 |
eapply Hn; eauto. |
| 288 |
Qed. |
| 289 |
|
| 290 |
|
| 291 |
Lemma skipn_ranged_for'_upd (from' : nat) scratch lst |
| 292 |
: skipn from' |
| 293 |
(snd |
| 294 |
(ranged_for' 0 from' |
| 295 |
(λ (acc0 : list T) (tok : ExitToken.t) (idx : Z) (_ : 0 - 1 < idx < from'), |
| 296 |
(tok, upd acc0 (Z.to_nat idx) (nth (Z.to_nat idx) lst default))) scratch)) |
| 297 |
= skipn from' scratch. |
| 298 |
Proof. |
| 299 |
rewrite <- nd_as_ranged_for'. |
| 300 |
eapply ranged_for'_invariant; eauto. |
| 301 |
intros. |
| 302 |
simpl in *. |
| 303 |
eapply nth_error_ext. |
| 304 |
intro i. |
| 305 |
rewrite ListUtil.nth_error_skipn. |
| 306 |
rewrite nth_error_upd_diff. |
| 307 |
{ |
| 308 |
rewrite <- !ListUtil.nth_error_skipn. |
| 309 |
congruence. |
| 310 |
} |
| 311 |
lia. |
| 312 |
Qed. |
| 313 |
|
| 314 |
Lemma map_predT_to_truncated_word ptr t |
| 315 |
: Lift1Prop.iff1 (t$@ptr) (array (truncated_word szT) sz_word ptr (map word_of_T t)). |
| 316 |
Proof. |
| 317 |
revert ptr; induction t; |
| 318 |
simpl. |
| 319 |
1: cbv; eauto. |
| 320 |
intros ptr m. |
| 321 |
split; intro H'. |
| 322 |
2:{ |
| 323 |
seprewrite IHt. |
| 324 |
seprewrite predT_to_truncated_word. |
| 325 |
auto. |
| 326 |
} |
| 327 |
{ |
| 328 |
symmetry in IHt. |
| 329 |
seprewrite IHt. |
| 330 |
pose proof predT_to_truncated_word as Hpred; |
| 331 |
symmetry in Hpred; |
| 332 |
seprewrite Hpred. |
| 333 |
auto. |
| 334 |
} |
| 335 |
Qed. |
| 336 |
|
| 337 |
|
| 338 |
Lemma map_upd A B (f : A -> B) i (a : A) l |
| 339 |
: map f (upd l i a) = upd (map f l) i (f a). |
| 340 |
Proof. |
| 341 |
eapply nth_error_ext; |
| 342 |
intros i0. |
| 343 |
destruct (Nat.compare_spec i0 (length l)); subst. |
| 344 |
1,3: rewrite !ListUtil.nth_error_length_error; eauto; |
| 345 |
repeat rewrite ?List.map_length, ?List.upd_length; lia. |
| 346 |
assert A as default. |
| 347 |
{ |
| 348 |
destruct l; simpl in*; try lia; auto. |
| 349 |
} |
| 350 |
rewrite !nth_error_nth' with (d:= f default) |
| 351 |
by (repeat rewrite ?List.map_length, ?List.upd_length; lia). |
| 352 |
f_equal. |
| 353 |
destruct (Nat.eq_dec i i0); subst. |
| 354 |
{ |
| 355 |
repeat rewrite ?nth_upd_same, ?map_nth |
| 356 |
by (repeat rewrite ?List.map_length, ?List.upd_length; lia). |
| 357 |
auto. |
| 358 |
} |
| 359 |
{ |
| 360 |
repeat rewrite ?nth_upd_diff, ?map_nth |
| 361 |
by (repeat rewrite ?List.map_length, ?List.upd_length; lia). |
| 362 |
auto. |
| 363 |
} |
| 364 |
Qed. |
| 365 |
|
| 366 |
Lemma compile_broadcast_expr' {t m l e} (len : nat) (lst scratch : list T) : |
| 367 |
len = length scratch -> |
| 368 |
len = length lst -> |
| 369 |
len < 2^width -> |
| 370 |
let v := lst in |
| 371 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl |
| 372 |
(a_ptr : word) a_var lst_expr idx_var to_var R, |
| 373 |
|
| 374 |
DEXPR m l (expr.var a_var) a_ptr -> |
| 375 |
|
| 376 |
(scratch$@a_ptr * R)%sep m -> |
| 377 |
|
| 378 |
~idx_var = to_var -> |
| 379 |
map.get l idx_var = None -> |
| 380 |
map.get l to_var = None -> |
| 381 |
|
| 382 |
broadcast_expr l idx_var scratch a_ptr R lst_expr v -> |
| 383 |
(let v := v in |
| 384 |
(*TODO: should this allow writing to the trace?*) |
| 385 |
forall idx to m, |
| 386 |
(v$@a_ptr* R)%sep m -> |
| 387 |
<{ Trace := t; Memory := m; Locals := map.put (map.put l idx_var idx) to_var to; Functions := e }> |
| 388 |
k_impl |
| 389 |
<{ pred (k v eq_refl) }>) -> |
| 390 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 391 |
cmd_loop_fresh false idx_var (expr.literal 0) to_var len |
| 392 |
(cmd.store szT (expr.op bopname.add a_var |
| 393 |
(expr.op bopname.mul idx_var sz_word)) |
| 394 |
lst_expr) |
| 395 |
k_impl |
| 396 |
<{ pred (nlet_eq [a_var] v k) }>. |
| 397 |
Proof. |
| 398 |
intros len_scratch len_lst. |
| 399 |
rewrite <- (list_as_nd_ranged_for_all lst scratch default len ltac:(auto) ltac:(auto)). |
| 400 |
rewrite nd_as_ranged_for_all. |
| 401 |
rewrite ranged_for_all_as_ranged_for. |
| 402 |
repeat compile_step. |
| 403 |
|
| 404 |
assert (~ a_var = to_var). |
| 405 |
{ |
| 406 |
intro; subst. |
| 407 |
revert H1. |
| 408 |
unfold DEXPR. |
| 409 |
simpl. |
| 410 |
unfold WeakestPrecondition.get. |
| 411 |
rewrite H5. |
| 412 |
intro H'; destruct H' as [? [? ?]]; congruence. |
| 413 |
} |
| 414 |
assert (~ a_var = idx_var). |
| 415 |
{ |
| 416 |
intro; subst. |
| 417 |
revert H1. |
| 418 |
unfold DEXPR. |
| 419 |
simpl. |
| 420 |
unfold WeakestPrecondition.get. |
| 421 |
rewrite H4. |
| 422 |
intro H'; destruct H' as [? [? ?]]; congruence. |
| 423 |
} |
| 424 |
eapply (compile_ranged_for_fresh _ _ _ _ _ _ _ _ _ _ _ _ idx_var to_var). |
| 425 |
repeat compile_step. |
| 426 |
repeat compile_step. |
| 427 |
(*Issue: in loop invariant lp from -> lp from'*) |
| 428 |
let x := open_constr:(fun from' lst tr mem locals => |
| 429 |
(lst$@a_ptr ⋆ R) mem |
| 430 |
/\ tr = t |
| 431 |
/\ locals = map.put (map.put l idx_var (word.of_Z from')) |
| 432 |
to_var (word.of_Z (Z.of_nat (length scratch)))) in |
| 433 |
instantiate(1:= x). |
| 434 |
{ |
| 435 |
cbn beta. |
| 436 |
repeat compile_step. |
| 437 |
rewrite map.get_put_diff; repeat compile_step. |
| 438 |
rewrite map.get_put_same; repeat compile_step. |
| 439 |
rewrite map.get_put_same; repeat compile_step. |
| 440 |
} |
| 441 |
{ |
| 442 |
cbn beta. |
| 443 |
repeat compile_step. |
| 444 |
rewrite map.put_comm; try compile_step. |
| 445 |
f_equal. |
| 446 |
rewrite map.put_put_same. |
| 447 |
reflexivity. |
| 448 |
} |
| 449 |
{ |
| 450 |
cbn beta. |
| 451 |
repeat compile_step. |
| 452 |
} |
| 453 |
{ |
| 454 |
repeat compile_step; try lia. |
| 455 |
} |
| 456 |
{ |
| 457 |
cbn beta. |
| 458 |
repeat compile_step. |
| 459 |
repeat straightline'. |
| 460 |
exists (word.add a_ptr (word.mul (word.of_Z from') sz_word)); repeat compile_step. |
| 461 |
{ |
| 462 |
eapply Util.dexpr_put_diff; repeat compile_step. |
| 463 |
eapply Util.dexpr_put_diff; repeat compile_step. |
| 464 |
} |
| 465 |
{ |
| 466 |
eapply Util.dexpr_put_diff; repeat compile_step. |
| 467 |
eapply Util.dexpr_put_same; repeat compile_step. |
| 468 |
} |
| 469 |
exists (word_of_T (nth (Z.to_nat from') lst default)); repeat compile_step. |
| 470 |
{ |
| 471 |
unfold broadcast_expr in *. |
| 472 |
subst v. |
| 473 |
revert H6. |
| 474 |
rewrite <- ranged_for_all_as_ranged_for. |
| 475 |
rewrite <- nd_as_ranged_for_all. |
| 476 |
rewrite (list_as_nd_ranged_for_all lst scratch default (length scratch) |
| 477 |
ltac:(auto) ltac:(auto)). |
| 478 |
intro H'. |
| 479 |
eapply dexpr_locals_put. |
| 480 |
{ |
| 481 |
rewrite map.get_put_diff; auto. |
| 482 |
} |
| 483 |
(*TODO: need to know about the ranged_for'*) |
| 484 |
specialize (H' mem0 (firstn (Z.to_nat from') acc)). |
| 485 |
rewrite firstn_length in H'. |
| 486 |
assert (from' <= length acc). |
| 487 |
{ |
| 488 |
unfold acc. |
| 489 |
unfold a. |
| 490 |
replace (from') with (Z.of_nat (Z.to_nat from')) by lia. |
| 491 |
rewrite ranged_for'_length_helper. |
| 492 |
lia. |
| 493 |
} |
| 494 |
rewrite Nat.min_l in H' by lia. |
| 495 |
rewrite Z2Nat.id in H'. |
| 496 |
eapply H'. |
| 497 |
all:try lia. |
| 498 |
replace (firstn (Z.to_nat from') acc ++ skipn (Z.to_nat from') scratch) with acc; eauto. |
| 499 |
rewrite <- firstn_skipn with (n:=Z.to_nat from') (l:=acc) at 1. |
| 500 |
f_equal. |
| 501 |
unfold acc, a. |
| 502 |
replace (from') with (Z.of_nat (Z.to_nat from')) by lia. |
| 503 |
rewrite Nat2Z.id. |
| 504 |
apply skipn_ranged_for'_upd. |
| 505 |
} |
| 506 |
{ |
| 507 |
|
| 508 |
seprewrite_in map_predT_to_truncated_word H12. |
| 509 |
eapply array_store_of_sep in H12. |
| 510 |
destruct H12 as [? [? ?]]. |
| 511 |
|
| 512 |
eexists; split. |
| 513 |
{ apply H11. } |
| 514 |
{ |
| 515 |
unfold lp. |
| 516 |
simpl. |
| 517 |
split; eauto. |
| 518 |
} |
| 519 |
{ |
| 520 |
instantiate (1:=Z.to_nat from'). |
| 521 |
f_equal. |
| 522 |
rewrite Z2Nat.id by lia. |
| 523 |
rewrite Z.mul_comm. |
| 524 |
rewrite word.ring_morph_mul. |
| 525 |
rewrite word.of_Z_unsigned. |
| 526 |
reflexivity. |
| 527 |
} |
| 528 |
{ |
| 529 |
rewrite List.map_length. |
| 530 |
unfold acc, a. |
| 531 |
replace (from') with (Z.of_nat (Z.to_nat from')) by lia. |
| 532 |
rewrite ranged_for'_length_helper. |
| 533 |
lia. |
| 534 |
} |
| 535 |
{ |
| 536 |
intros. |
| 537 |
seprewrite map_predT_to_truncated_word. |
| 538 |
rewrite <- map_upd in H11. |
| 539 |
eauto. |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
{ |
| 544 |
simpl. |
| 545 |
intros. |
| 546 |
destruct H10. |
| 547 |
eapply H7 in H10. |
| 548 |
change (-1) with (0-1). |
| 549 |
unfold v in H10. |
| 550 |
destruct H11; |
| 551 |
subst. |
| 552 |
exact H10. |
| 553 |
} |
| 554 |
Qed. |
| 555 |
|
| 556 |
Lemma compile_broadcast_expr {t m l e} (len : nat) (lst scratch : list T) : |
| 557 |
let v := lst in |
| 558 |
forall P (pred: P v -> predicate) (k: nlet_eq_k P v) k_impl |
| 559 |
(a_ptr : word) a_var lst_expr idx_var to_var R, |
| 560 |
|
| 561 |
DEXPR m l (expr.var a_var) a_ptr -> |
| 562 |
|
| 563 |
(scratch$@a_ptr * R)%sep m -> |
| 564 |
|
| 565 |
len = length scratch -> |
| 566 |
len = length lst -> |
| 567 |
len < 2^width -> |
| 568 |
|
| 569 |
~idx_var = to_var -> |
| 570 |
map.get l idx_var = None -> |
| 571 |
map.get l to_var = None -> |
| 572 |
|
| 573 |
broadcast_expr l idx_var scratch a_ptr R lst_expr v -> |
| 574 |
(let v := v in |
| 575 |
(*TODO: should we allow affecting the trace? *) |
| 576 |
forall idx to m, |
| 577 |
(v$@a_ptr* R)%sep m -> |
| 578 |
<{ Trace := t; Memory := m; Locals := map.put (map.put l idx_var idx) to_var to; Functions := e }> |
| 579 |
k_impl |
| 580 |
<{ pred (k v eq_refl) }>) -> |
| 581 |
<{ Trace := t; Memory := m; Locals := l; Functions := e }> |
| 582 |
cmd_loop_fresh false idx_var (expr.literal 0) to_var len |
| 583 |
(cmd.store szT (expr.op bopname.add a_var |
| 584 |
(expr.op bopname.mul idx_var sz_word)) |
| 585 |
lst_expr) |
| 586 |
k_impl |
| 587 |
<{ pred (nlet_eq [a_var] v k) }>. |
| 588 |
Proof using T_Fits_ok ext_spec_ok locals_ok mem_ok word_ok. |
| 589 |
eauto using compile_broadcast_expr'. |
| 590 |
Qed. |
| 591 |
|
| 592 |
Section Binops. |
| 593 |
|
| 594 |
Context (expr_op : expr -> expr -> expr) |
| 595 |
(word_op : word -> word -> word) |
| 596 |
(op : T -> T -> T). |
| 597 |
Context (word_morph : forall a b, word_of_T (op a b) = word_op (word_of_T a) (word_of_T b)). |
| 598 |
|
| 599 |
Context (expr_compile_word_op |
| 600 |
: forall {m l} (w1 w2 : word.rep) (e1 e2 : expr), |
| 601 |
DEXPR m l e1 w1 -> |
| 602 |
DEXPR m l e2 w2 -> |
| 603 |
DEXPR m l (expr_op e1 e2) (word_op w1 w2)). |
| 604 |
|
| 605 |
(*TODO: generalize to a DEXPR goal instead of bop?*) |
| 606 |
Lemma broadcast_binop l idx_var scratch a_ptr R l1_expr l2_expr (l1 l2 : list T) |
| 607 |
: (*TODO: not necessary, just makes proof using expr_compile_word_op word_morph easier*) |
| 608 |
op default default = default -> |
| 609 |
|
| 610 |
length l1 = length l2 -> |
| 611 |
broadcast_expr l idx_var scratch a_ptr R l1_expr l1 -> |
| 612 |
broadcast_expr l idx_var scratch a_ptr R l2_expr l2 -> |
| 613 |
broadcast_expr l idx_var scratch a_ptr R |
| 614 |
(expr_op l1_expr l2_expr) |
| 615 |
(List.map (fun '(w1, w2) => op w1 w2) (combine l1 l2)). |
| 616 |
Proof. |
| 617 |
unfold broadcast_expr; intuition idtac. |
| 618 |
set (op_uncurry:=fun '(w1,w2) => op w1 w2). |
| 619 |
replace default with (op_uncurry (default,default)). |
| 620 |
rewrite map_nth. |
| 621 |
rewrite combine_nth by auto. |
| 622 |
subst op_uncurry; cbn beta match. |
| 623 |
rewrite word_morph. |
| 624 |
eapply expr_compile_word_op; eauto. |
| 625 |
Qed. |
| 626 |
|
| 627 |
End Binops. |
| 628 |
|
| 629 |
(*TODO: this is awkward*) |
| 630 |
Local Definition predT_to_tw := @predT_to_truncated_word T T_Fits T_Fits_ok. |
| 631 |
|
| 632 |
Local Hint Resolve predT_to_tw : ecancel_impl. |
| 633 |
|
| 634 |
Lemma broadcast_id l idx_var scratch a_ptr R a_var |
| 635 |
: map.get l a_var = Some a_ptr -> |
| 636 |
~a_var = idx_var -> |
| 637 |
broadcast_expr l idx_var scratch a_ptr R |
| 638 |
(expr.load szT |
| 639 |
(expr.op bopname.add a_var |
| 640 |
(expr.op bopname.mul idx_var sz_word))) |
| 641 |
scratch. |
| 642 |
Proof using BW T_Fits_ok locals_ok mem_ok word_ok. |
| 643 |
unfold broadcast_expr; intuition idtac. |
| 644 |
repeat straightline. |
| 645 |
exists a_ptr; intuition idtac. |
| 646 |
{ |
| 647 |
rewrite map.get_put_diff by assumption. |
| 648 |
assumption. |
| 649 |
} |
| 650 |
exists (word.of_Z (Z.of_nat (length lstl))). |
| 651 |
intuition idtac. |
| 652 |
{ |
| 653 |
rewrite map.get_put_same; eauto. |
| 654 |
} |
| 655 |
simpl. |
| 656 |
unfold WeakestPrecondition.literal. |
| 657 |
cbv [dlet]. |
| 658 |
exists (word_of_T (nth (length lstl) scratch default)). |
| 659 |
split; auto. |
| 660 |
erewrite load_of_sep. |
| 661 |
{ |
| 662 |
erewrite truncate_of_T. |
| 663 |
reflexivity. |
| 664 |
} |
| 665 |
seprewrite_in (array_append (T:=T) predT sz_word) H3. |
| 666 |
replace (nth (length lstl) scratch) |
| 667 |
with (nth ((length lstl)+0) scratch) by (f_equal;lia). |
| 668 |
rewrite <- nth_skipn. |
| 669 |
assert (0 < length (skipn (length lstl) scratch)). |
| 670 |
{ |
| 671 |
rewrite length_skipn. |
| 672 |
lia. |
| 673 |
} |
| 674 |
erewrite skipn_nth_0 in H3 by lia. |
| 675 |
cbn [nth array] in *. |
| 676 |
lazymatch goal with |
| 677 |
| [H : context [predT ?ptr1] |- context [ truncated_word _ ?ptr2]] => |
| 678 |
replace ptr2 with ptr1 |
| 679 |
end. |
| 680 |
seprewrite_in predT_to_truncated_word H3. |
| 681 |
ecancel_assumption. |
| 682 |
f_equal. |
| 683 |
rewrite Z.mul_comm. |
| 684 |
rewrite word.ring_morph_mul. |
| 685 |
reflexivity. |
| 686 |
Qed. |
| 687 |
|
| 688 |
|
| 689 |
Lemma split_hd_tl {A} (a:A) (l:list A) |
| 690 |
: 0 < length l -> |
| 691 |
l = hd a l :: tl l. |
| 692 |
Proof. |
| 693 |
destruct l; simpl in *; [lia | auto]. |
| 694 |
Qed. |
| 695 |
|
| 696 |
Lemma broadcast_var l idx_var scratch a_ptr b_ptr a_var a_data R' R |
| 697 |
: Lift1Prop.iff1 R' (a_data$@a_ptr * R)%sep -> |
| 698 |
map.get l a_var = Some a_ptr -> |
| 699 |
~a_var = idx_var -> |
| 700 |
length scratch <= length a_data -> |
| 701 |
broadcast_expr l idx_var scratch b_ptr R' |
| 702 |
(expr.load szT |
| 703 |
(expr.op bopname.add a_var |
| 704 |
(expr.op bopname.mul idx_var sz_word))) |
| 705 |
a_data. |
| 706 |
Proof using T_Fits_ok locals_ok mem_ok word_ok BW. |
| 707 |
unfold broadcast_expr; intuition idtac. |
| 708 |
repeat straightline. |
| 709 |
exists a_ptr; intuition idtac. |
| 710 |
{ |
| 711 |
rewrite map.get_put_diff by assumption. |
| 712 |
assumption. |
| 713 |
} |
| 714 |
exists (word.of_Z (Z.of_nat (length lstl))). |
| 715 |
intuition idtac. |
| 716 |
{ |
| 717 |
rewrite map.get_put_same; eauto. |
| 718 |
} |
| 719 |
simpl. |
| 720 |
unfold WeakestPrecondition.literal. |
| 721 |
cbv [dlet]. |
| 722 |
exists (word_of_T (nth (length lstl) a_data default)). |
| 723 |
split; auto. |
| 724 |
erewrite load_of_sep. |
| 725 |
{ |
| 726 |
erewrite truncate_of_T. |
| 727 |
reflexivity. |
| 728 |
} |
| 729 |
seprewrite_in H0 H5. |
| 730 |
clear R' H0. |
| 731 |
assert (((lstl ++ skipn (length lstl) scratch)$@b_ptr ⋆ (a_data$@a_ptr) * R)%sep m) |
| 732 |
as H6 by ecancel_assumption. |
| 733 |
clear H5; rename H6 into H5. |
| 734 |
seprewrite_in (array_append (T:=T) predT sz_word) H5. |
| 735 |
replace (nth (length lstl) scratch) |
| 736 |
with (nth ((length lstl)+0) scratch) by (f_equal;lia). |
| 737 |
seprewrite_in map_predT_to_truncated_word H5. |
| 738 |
seprewrite_in map_predT_to_truncated_word H5. |
| 739 |
seprewrite_in map_predT_to_truncated_word H5. |
| 740 |
rewrite <- (firstn_skipn (length lstl) a_data) in H5. |
| 741 |
rewrite map_app in H5. |
| 742 |
seprewrite_in (array_append (T:=word)) H5. |
| 743 |
rewrite map_length in H5. |
| 744 |
rewrite firstn_length in H5. |
| 745 |
replace ((Init.Nat.min (length lstl) (length a_data))) with (length lstl) in H5 by lia. |
| 746 |
rewrite (split_hd_tl default (skipn (length lstl) a_data)) in H5 by (rewrite skipn_length; lia). |
| 747 |
simpl in H5. |
| 748 |
rewrite Z.mul_comm in H5. |
| 749 |
rewrite word.ring_morph_mul in H5. |
| 750 |
rewrite <- hd_skipn_nth_default in H5. |
| 751 |
rewrite nth_default_eq in H5. |
| 752 |
ecancel_assumption. |
| 753 |
Qed. |
| 754 |
|
| 755 |
End WithAccessSize. |
| 756 |
|
| 757 |
Instance byte_ac : FitsInLocal byte := |
| 758 |
{| |
| 759 |
predT := ptsto; |
| 760 |
szT := access_size.one; |
| 761 |
word_of_T b := word_of_byte b; |
| 762 |
|}. |
| 763 |
|
| 764 |
Lemma byte_and_xff b : byte.and b xff = b. |
| 765 |
Proof using Type. |
| 766 |
destruct b; reflexivity. |
| 767 |
Qed. |
| 768 |
|
| 769 |
(*TODO: where to put?*) |
| 770 |
Axiom (width_at_least_byte : 8 <= width). |
| 771 |
|
| 772 |
Lemma byte_in_word_bounds t |
| 773 |
: 0 <= byte.unsigned t < 2 ^ width. |
| 774 |
Proof using Type. |
| 775 |
assert (256 <= 2^width). |
| 776 |
{ |
| 777 |
assert (256 <= 2^8) by (compute; congruence). |
| 778 |
pose proof (Z.pow_le_mono_r 2 8 width). |
| 779 |
pose proof width_at_least_byte. |
| 780 |
lia. |
| 781 |
} |
| 782 |
destruct t; cbv [byte.unsigned Byte.to_N Z.of_N]; lia. |
| 783 |
Qed. |
| 784 |
|
| 785 |
|
| 786 |
Instance byte_ac_ok : FitsInLocal_ok byte byte_ac. |
| 787 |
Proof using BW mem_ok word_ok. |
| 788 |
constructor; unfold word_of_T, szT, predT, byte_ac. |
| 789 |
{ |
| 790 |
intros; unfold truncate_word. |
| 791 |
cbn. |
| 792 |
unfold truncate_Z. |
| 793 |
simpl. |
| 794 |
rewrite word.morph_and. |
| 795 |
rewrite word.unsigned_of_Z_nowrap; [| apply byte_in_word_bounds]. |
| 796 |
change (word.of_Z (Z.ones 8)) with (word_of_byte xff : word). |
| 797 |
rewrite <- byte_morph_and. |
| 798 |
rewrite byte_and_xff. |
| 799 |
reflexivity. |
| 800 |
} |
| 801 |
intros ptr t m. |
| 802 |
cbv [truncated_word truncated_scalar]. |
| 803 |
simpl le_split. |
| 804 |
rewrite word.unsigned_of_Z_nowrap, word.byte_of_Z_unsigned by apply byte_in_word_bounds. |
| 805 |
rewrite OfListWord.map.of_list_word_singleton. |
| 806 |
split; cbv [sepclause_of_map ptsto] in *; auto. |
| 807 |
Qed. |
| 808 |
|
| 809 |
|
| 810 |
Instance word_ac : FitsInLocal word := |
| 811 |
{| |
| 812 |
predT := scalar; |
| 813 |
szT := access_size.word; |
| 814 |
word_of_T b := b; |
| 815 |
|}. |
| 816 |
|
| 817 |
(*TODO: where to get this fact from?*) |
| 818 |
Axiom width_mul_8 : exists x, width = x * 8. |
| 819 |
|
| 820 |
Instance word_ac_ok : FitsInLocal_ok word word_ac. |
| 821 |
Proof. |
| 822 |
constructor; unfold word_of_T, szT, predT, word_ac. |
| 823 |
{ |
| 824 |
intros. |
| 825 |
intros; unfold truncate_word. |
| 826 |
cbn. |
| 827 |
unfold truncate_Z. |
| 828 |
simpl. |
| 829 |
rewrite word.morph_and. |
| 830 |
rewrite !word.of_Z_unsigned. |
| 831 |
rewrite Z2Nat.id. |
| 832 |
unfold Memory.bytes_per_word. |
| 833 |
replace ((width + 7) / 8 * 8) with width. |
| 834 |
{ |
| 835 |
rewrite <- (word.of_Z_unsigned t). |
| 836 |
rewrite <- word.morph_and. |
| 837 |
rewrite word.of_Z_land_ones. |
| 838 |
auto. |
| 839 |
} |
| 840 |
{ |
| 841 |
pose proof width_mul_8 as Hw; destruct Hw as [x Hw]; subst. |
| 842 |
lia. |
| 843 |
} |
| 844 |
{ |
| 845 |
unfold Memory.bytes_per_word. |
| 846 |
pose proof (word.width_pos). |
| 847 |
pose proof width_mul_8 as Hw; destruct Hw as [x Hw]; subst. |
| 848 |
lia. |
| 849 |
} |
| 850 |
} |
| 851 |
{ |
| 852 |
intros ptr t m; |
| 853 |
split; |
| 854 |
unfold scalar in *; |
| 855 |
auto. |
| 856 |
} |
| 857 |
Qed. |
| 858 |
|
| 859 |
|
| 860 |
(*TODO: define in general*) |
| 861 |
Lemma broadcast_byte_const l (idx_var : string) scratch a_ptr R (const_list : list byte) |
| 862 |
: length scratch <= length const_list -> |
| 863 |
length scratch <= 2^width -> |
| 864 |
broadcast_expr byte l idx_var scratch a_ptr R |
| 865 |
(expr.inlinetable |
| 866 |
access_size.one |
| 867 |
const_list |
| 868 |
idx_var) |
| 869 |
const_list. |
| 870 |
Proof. |
| 871 |
unfold broadcast_expr. |
| 872 |
intros. |
| 873 |
repeat straightline. |
| 874 |
exists (word.of_Z (length lstl)); split; auto. |
| 875 |
{ |
| 876 |
rewrite map.get_put_same; eauto. |
| 877 |
} |
| 878 |
unfold WeakestPrecondition.load. |
| 879 |
exists (word_of_byte (nth (length lstl) const_list x00)). |
| 880 |
split; auto. |
| 881 |
eapply load_one_of_sep. |
| 882 |
instantiate (1:= fun _ => True). |
| 883 |
|
| 884 |
exists (map.put map.empty (word.of_Z (Z.of_nat (length lstl))) (nth (length lstl) const_list x00)). |
| 885 |
exists (map.remove (OfListWord.map.of_list_word const_list) (word.of_Z (Z.of_nat (length lstl)))). |
| 886 |
intuition idtac. |
| 887 |
{ |
| 888 |
eapply map.split_comm. |
| 889 |
eapply map.split_remove_put. |
| 890 |
rewrite map.split_empty_r; auto. |
| 891 |
rewrite OfListWord.map.get_of_list_word. |
| 892 |
rewrite word.unsigned_of_Z. |
| 893 |
rewrite word.wrap_small. |
| 894 |
rewrite Nat2Z.id. |
| 895 |
eapply nth_error_nth'. |
| 896 |
lia. |
| 897 |
split; try lia. |
| 898 |
} |
| 899 |
{ |
| 900 |
reflexivity. |
| 901 |
} |
| 902 |
Qed. |
| 903 |
|
| 904 |
|
| 905 |
Lemma broadcast_byte_and l idx_var scratch a_ptr R l1_expr l2_expr (l1 l2 : list byte) |
| 906 |
: length l1 = length l2 -> |
| 907 |
broadcast_expr byte l idx_var scratch a_ptr R l1_expr l1 -> |
| 908 |
broadcast_expr byte l idx_var scratch a_ptr R l2_expr l2 -> |
| 909 |
broadcast_expr byte l idx_var scratch a_ptr R |
| 910 |
(expr.op bopname.and l1_expr l2_expr) |
| 911 |
(List.map (fun '(w1, w2) => byte.and w1 w2) (combine l1 l2)). |
| 912 |
Proof using BW word_ok. |
| 913 |
eapply broadcast_binop. |
| 914 |
- exact byte_morph_and. |
| 915 |
- intros; eapply expr_compile_word_and; eauto. |
| 916 |
- reflexivity. |
| 917 |
Qed. |
| 918 |
|
| 919 |
|
| 920 |
Lemma broadcast_word_add l idx_var scratch a_ptr R l1_expr l2_expr (l1 l2 : list word) |
| 921 |
: length l1 = length l2 -> |
| 922 |
broadcast_expr word l idx_var scratch a_ptr R l1_expr l1 -> |
| 923 |
broadcast_expr word l idx_var scratch a_ptr R l2_expr l2 -> |
| 924 |
broadcast_expr word l idx_var scratch a_ptr R |
| 925 |
(expr.op bopname.add l1_expr l2_expr) |
| 926 |
(List.map (fun '(w1, w2) => word.add w1 w2) (combine l1 l2)). |
| 927 |
Proof using word_ok. |
| 928 |
eapply broadcast_binop. |
| 929 |
- intros; reflexivity. |
| 930 |
- intros; eapply expr_compile_word_add; eauto. |
| 931 |
- unfold default, HasDefault_word. |
| 932 |
rewrite <- word.ring_morph_add. |
| 933 |
reflexivity. |
| 934 |
Qed. |
| 935 |
|
| 936 |
End with_parameters. |
| 937 |
|
| 938 |
Ltac compile_broadcast_expr := |
| 939 |
lazymatch goal with |
| 940 |
| [ |- WeakestPrecondition.cmd _ _ _ _ ?locals (_ (nlet_eq [?var] ?v _)) ] => |
| 941 |
let idx_var_str := gensym locals constr:((var++"_idx")%string) in |
| 942 |
let to_var_str := gensym locals constr:((var++"_to")%string) in |
| 943 |
simple eapply compile_broadcast_expr |
| 944 |
with (idx_var:=idx_var_str) (to_var:=to_var_str); |
| 945 |
[ typeclasses eauto|..] |
| 946 |
end. |