forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_lemmas.v
2110 lines (1936 loc) · 77.1 KB
/
mem_lemmas.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import compcert.lib.Coqlib.
Require Import compcert.lib.Maps.
Require Import compcert.lib.Integers.
Require Import compcert.lib.Axioms.
Require Import compcert.common.AST.
Require Import compcert.common.Values.
Require Import compcert.common.Memory.
Require Import compcert.common.Events.
Require Import compcert.common.Globalenvs.
Require Import msl.Extensionality.
Require Import sepcomp.Address.
Notation val_inject:= Val.inject.
Lemma valid_block_dec: forall m b, {Mem.valid_block m b} + {~Mem.valid_block m b}.
Proof. intros.
unfold Mem.valid_block.
remember (plt b (Mem.nextblock m)).
destruct s. left; assumption.
right. intros N. xomega.
Qed.
Lemma Forall2_length {A B} {f:A -> B -> Prop} {l1 l2} (F:Forall2 f l1 l2): length l1 = length l2.
Proof. induction F; trivial. simpl; rewrite IHF. trivial. Qed.
Lemma Forall2_Zlength {A B} {f:A -> B -> Prop} {l1 l2} (F:Forall2 f l1 l2): Zlength l1 = Zlength l2.
Proof. do 2 rewrite Zlength_correct. rewrite (Forall2_length F). trivial. Qed.
Lemma pos_succ_plus_assoc: forall n m,
(Pos.succ n + m = n + Pos.succ m)%positive.
Proof. intros.
do 2 rewrite Pplus_one_succ_r;
rewrite (Pos.add_comm m);
rewrite Pos.add_assoc; trivial.
Qed.
Lemma mem_unchanged_on_sub: forall (P Q: block -> BinInt.Z -> Prop) m m',
Mem.unchanged_on Q m m' ->
(forall b ofs, P b ofs -> Q b ofs) ->
Mem.unchanged_on P m m'.
Proof.
intros until m'; intros [L H1 H2] H3.
split; intros.
auto.
solve[apply (H1 b ofs k p (H3 b ofs H)); auto].
solve[apply (H2 b ofs); auto].
Qed.
Lemma mem_unchanged_on_sub_strong: forall (P Q: block -> BinInt.Z -> Prop) m m',
Mem.unchanged_on Q m m' ->
(forall b ofs, Mem.valid_block m b -> P b ofs -> Q b ofs) ->
Mem.unchanged_on P m m'.
Proof.
intros until m'; intros [L H1 H2] H3.
split; intros. auto.
solve[apply (H1 b ofs k p (H3 b ofs H0 H) H0)].
apply (H2 b ofs); auto. apply H3; auto.
solve[eapply Mem.perm_valid_block; eauto].
Qed.
Lemma inject_separated_same_meminj: forall j m m',
Events.inject_separated j j m m'.
Proof. intros j m m' b; intros. congruence. Qed.
Lemma compose_meminj_idR: forall j, j = compose_meminj j inject_id.
Proof. intros. unfold compose_meminj, inject_id.
apply extensionality. intro b.
remember (j b).
destruct o; trivial. destruct p. rewrite Zplus_0_r. trivial.
Qed.
Lemma compose_meminj_idL: forall j, j = compose_meminj inject_id j.
Proof. intros. unfold compose_meminj, inject_id.
apply extensionality. intro b.
remember (j b).
destruct o; trivial. destruct p. rewrite Zplus_0_l. trivial.
Qed.
Theorem drop_extends:
forall m1 m2 lo hi b p m1',
Mem.extends m1 m2 ->
Mem.drop_perm m1 b lo hi p = Some m1' ->
exists m2',
Mem.drop_perm m2 b lo hi p = Some m2'
/\ Mem.extends m1' m2'.
Proof.
intros. inv H.
destruct (Mem.drop_mapped_inj _ _ _ b b 0 _ _ _ _ mext_inj H0) as [m2' [D Inj]].
intros b'; intros. inv H1. inv H2. left. assumption.
reflexivity.
repeat rewrite Zplus_0_r in D. exists m2'. split; trivial.
split; trivial.
rewrite (Mem.nextblock_drop _ _ _ _ _ _ H0).
rewrite (Mem.nextblock_drop _ _ _ _ _ _ D). assumption.
(*perm_inv*)
intros. specialize (Mem.perm_drop_4 _ _ _ _ _ _ D _ _ _ _ H); intros.
destruct (mext_perm_inv _ _ _ _ H1).
+ left.
destruct (eq_block b0 b); subst.
- destruct (zle lo ofs).
* destruct (zlt ofs hi).
{ eapply Mem.perm_implies.
eapply Mem.perm_drop_1. eassumption. omega.
eapply (Mem.perm_drop_2 _ _ _ _ _ _ D). 2: eassumption. omega. }
{ eapply Mem.perm_drop_3; try eassumption. right. omega. }
* eapply Mem.perm_drop_3; try eassumption. right. omega.
- eapply Mem.perm_drop_3; try eassumption. left; trivial.
+ right. intros N. elim H2; clear H2.
eapply Mem.perm_drop_4; eassumption.
Qed.
Lemma mem_inj_id_trans: forall m1 m2 (Inj12: Mem.mem_inj inject_id m1 m2) m3
(Inj23: Mem.mem_inj inject_id m2 m3),Mem.mem_inj inject_id m1 m3.
Proof. intros.
destruct Inj12. rename mi_perm into perm12. rename mi_align into align12.
rename mi_memval into memval12.
destruct Inj23. rename mi_perm into perm23. rename mi_align into align23.
rename mi_memval into memval23.
split; intros.
(*mi_perm*)
apply (perm12 _ _ _ _ _ _ H) in H0.
assert (inject_id b2 = Some (b2, delta)).
unfold inject_id in *. inv H. trivial.
apply (perm23 _ _ _ _ _ _ H1) in H0. inv H. inv H1. rewrite Zplus_0_r in H0.
assumption.
(*mi_align*)
apply (align12 _ _ _ _ _ _ H) in H0.
assert (inject_id b2 = Some (b2, delta)).
unfold inject_id in *. inv H. trivial. assumption.
(*mi_memval*)
assert (MV1:= memval12 _ _ _ _ H H0).
assert (inject_id b2 = Some (b2, delta)).
unfold inject_id in *. inv H. trivial.
assert (R2: Mem.perm m2 b2 ofs Cur Readable).
apply (perm12 _ _ _ _ _ _ H) in H0. inv H. rewrite Zplus_0_r in H0.
assumption.
assert (MV2:= memval23 _ _ _ _ H1 R2).
inv H. inv H1. rewrite Zplus_0_r in *.
remember (ZMap.get ofs (PMap.get b2 (Mem.mem_contents m2))) as v.
destruct v. inv MV1. apply MV2.
inv MV1. apply MV2.
inv MV2. constructor.
inv MV1; try solve[constructor]. inv MV2; constructor.
specialize (val_inject_compose _ _ _ _ _ H2 H3).
rewrite <- compose_meminj_idL; trivial.
Qed.
Lemma extends_trans: forall m1 m2
(Ext12: Mem.extends m1 m2) m3 (Ext23: Mem.extends m2 m3), Mem.extends m1 m3.
Proof. intros. inv Ext12. inv Ext23.
split. rewrite mext_next. assumption. eapply mem_inj_id_trans; eauto.
(*perm_inv*)
intros. destruct (mext_perm_inv0 _ _ _ _ H).
+ apply (mext_perm_inv _ _ _ _ H0).
+ right. intros N. elim H0; clear H0.
specialize (Mem.mi_perm _ _ _ mext_inj b b 0 ofs Max Nonempty); intros.
rewrite Zplus_0_r in H0; apply H0; trivial.
Qed.
Lemma memval_inject_id_refl: forall v, memval_inject inject_id v v.
Proof.
destruct v. constructor. constructor. econstructor.
destruct v; try econstructor. reflexivity. rewrite Int.add_zero. trivial.
Qed.
Lemma extends_refl: forall m, Mem.extends m m.
Proof. intros.
split. trivial.
split; intros.
inv H. rewrite Zplus_0_r. assumption.
inv H. apply Z.divide_0_r. (*rewrite Zplus_0_r. assumption.*)
inv H. rewrite Zplus_0_r. apply memval_inject_id_refl.
(*perm_inv*)
intros. left; trivial.
Qed.
Lemma perm_decE:
forall m b ofs k p PF,
(Mem.perm_dec m b ofs k p = left PF <-> Mem.perm m b ofs k p).
Proof.
intros until p.
split; auto.
intros H1.
destruct (Mem.perm_dec m b ofs k p).
solve[f_equal; apply proof_irr].
solve[elimtype False; auto].
Qed.
Lemma flatinj_E: forall b b1 b2 delta (H:Mem.flat_inj b b1 = Some (b2, delta)),
b2=b1 /\ delta=0 /\ Plt b2 b.
Proof.
unfold Mem.flat_inj. intros.
destruct (plt b1 b); inv H. repeat split; trivial.
Qed.
Lemma flatinj_I: forall bb b, Plt b bb -> Mem.flat_inj bb b = Some (b, 0).
Proof.
intros. unfold Mem.flat_inj.
destruct (plt b bb); trivial. exfalso. xomega.
Qed.
Lemma flatinj_mono: forall b b1 b2 b' delta
(F: Mem.flat_inj b1 b = Some (b', delta)),
Plt b1 b2 -> Mem.flat_inj b2 b = Some (b', delta).
Proof. intros.
apply flatinj_E in F. destruct F as [? [? ?]]; subst.
apply flatinj_I. xomega.
Qed.
(* A minimal preservation property we sometimes require. *)
(*
Definition readonly {F V} (ge: Genv.t F V) m1 b m2 :=
forall gv, Genv.find_var_info ge b = Some gv ->
gvar_readonly gv && negb (gvar_volatile gv) = true ->
forall ch ofs, Mem.load ch m2 b ofs = Mem.load ch m1 b ofs.
*)
Definition readonlyLD m1 b m2 :=
forall chunk ofs
(NWR: forall ofs', ofs <= ofs' < ofs + size_chunk chunk ->
~(Mem.perm m1 b ofs' Cur Writable)),
Mem.load chunk m2 b ofs = Mem.load chunk m1 b ofs /\
(forall ofs', ofs <= ofs' < ofs + size_chunk chunk ->
(forall k p, Mem.perm m1 b ofs' k p <-> Mem.perm m2 b ofs' k p)).
Definition readonly m1 b m2 :=
forall n ofs
(NWR: forall i, 0 <= i < n ->
~(Mem.perm m1 b (ofs + i) Cur Writable)),
Mem.loadbytes m2 b ofs n = Mem.loadbytes m1 b ofs n /\
(forall i, 0 <= i < n ->
(forall k p, Mem.perm m1 b (ofs+i) k p <-> Mem.perm m2 b (ofs+i) k p)).
Definition max_readonlyLD m1 b m2 :=
forall chunk ofs
(NWR: forall ofs', ofs <= ofs' < ofs + size_chunk chunk ->
~(Mem.perm m1 b ofs' Max Writable)),
Mem.load chunk m2 b ofs = Mem.load chunk m1 b ofs /\
(forall ofs', ofs <= ofs' < ofs + size_chunk chunk ->
(forall k p, Mem.perm m1 b ofs' k p <-> Mem.perm m2 b ofs' k p)).
Definition max_readonly m1 b m2 :=
forall n ofs
(NWR: forall i, 0 <= i < n ->
~(Mem.perm m1 b (ofs + i) Max Writable)),
Mem.loadbytes m2 b ofs n = Mem.loadbytes m1 b ofs n /\
(forall i, 0 <= i < n ->
(forall k p, Mem.perm m1 b (ofs+i) k p <-> Mem.perm m2 b (ofs+i) k p)).
Lemma readonlyLD_max_readonlyLD m1 b m2: readonlyLD m1 b m2 -> max_readonlyLD m1 b m2.
Proof. red; intros. destruct (H chunk ofs).
intros; intros N. apply Mem.perm_max in N. apply (NWR _ H0 N).
split; trivial.
Qed.
Lemma readonly_max_readonly m1 b m2: readonly m1 b m2 -> max_readonly m1 b m2.
Proof. red; intros. destruct (H n ofs).
intros; intros N. apply Mem.perm_max in N. apply (NWR _ H0 N).
split; trivial.
Qed.
Lemma readonly_readonlyLD m1 b m2: readonly m1 b m2 -> readonlyLD m1 b m2.
Proof.
red; intros. destruct (H (size_chunk chunk) ofs); clear H.
intros. apply NWR. omega.
split; intros.
remember (Mem.load chunk m2 b ofs) as d; symmetry in Heqd; symmetry.
destruct d.
{ destruct (Mem.load_loadbytes _ _ _ _ _ Heqd) as [bytes [LDB V]]; subst v.
apply Mem.load_valid_access in Heqd; destruct Heqd.
rewrite H0 in LDB. apply Mem.loadbytes_load; trivial.
}
{ remember (Mem.load chunk m1 b ofs) as q; symmetry in Heqq; symmetry.
destruct q; trivial.
destruct (Mem.load_loadbytes _ _ _ _ _ Heqq) as [bytes [LDB V]]; subst v.
apply Mem.load_valid_access in Heqq; destruct Heqq.
rewrite <- Heqd.
rewrite <- H0 in LDB. apply Mem.loadbytes_load; trivial. }
specialize (H1 (ofs'-ofs)). rewrite Zplus_minus in H1. apply H1. omega.
Qed.
Lemma readonly_refl m b: readonly m b m.
Proof. red; intuition. Qed.
Lemma readonlyLD_refl m b: readonlyLD m b m.
Proof. red; intuition. Qed.
Lemma readonlyLD_trans m1 m2 m3 b: readonlyLD m1 b m2 -> readonlyLD m2 b m3 -> readonlyLD m1 b m3.
Proof. red; intros. destruct (H _ _ NWR); clear H.
destruct (H0 chunk ofs); clear H0.
intros. intros N. apply (NWR _ H). apply (H2 _ H). assumption.
split. rewrite <- H1. assumption.
intros. destruct (H2 _ H0 k p); clear H2. destruct (H3 _ H0 k p); clear H3.
split; eauto.
Qed.
Lemma readonly_trans m1 m2 m3 b: readonly m1 b m2 -> readonly m2 b m3 -> readonly m1 b m3.
Proof. red; intros. destruct (H _ _ NWR); clear H.
destruct (H0 n ofs); clear H0.
intros. intros N. apply (NWR _ H). apply (H2 _ H). assumption.
split. rewrite <- H1. assumption.
intros. destruct (H2 _ H0 k p); clear H2. destruct (H3 _ H0 k p); clear H3.
split; eauto.
Qed.
Definition mem_forward (m1 m2:mem) :=
forall b, Mem.valid_block m1 b ->
(Mem.valid_block m2 b
/\ (forall ofs p, Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p)
(*/\ readonly m1 b m2*)).
Lemma mem_forward_refl: forall m, mem_forward m m.
Proof. intros m b H. split; trivial.
(* split; eauto. apply readonly_refl.*)
Qed.
Lemma mem_forward_trans: forall m1 m2 m3,
mem_forward m1 m2 -> mem_forward m2 m3 -> mem_forward m1 m3.
Proof. intros. intros b Hb.
destruct (H _ Hb) as [? ?].
destruct (H0 _ H1) as [? ?].
split; eauto.
Qed.
Lemma forward_unchanged_trans: forall P m1 m2 m3,
Mem.unchanged_on P m1 m2 -> Mem.unchanged_on P m2 m3 ->
mem_forward m1 m2 -> mem_forward m2 m3 ->
mem_forward m1 m3 /\ Mem.unchanged_on P m1 m3.
Proof. intros.
split. eapply mem_forward_trans; eassumption.
split; intros.
eapply Ple_trans; eapply Mem.unchanged_on_nextblock; eassumption.
destruct H.
destruct (unchanged_on_perm _ _ k p H3 H4).
destruct H0. destruct (H1 _ H4).
destruct (unchanged_on_perm0 _ _ k p H3 H0).
split; intros; auto.
destruct H.
rewrite <- (unchanged_on_contents _ _ H3 H4).
destruct H0.
apply (unchanged_on_contents0 _ _ H3).
apply unchanged_on_perm; try assumption.
apply Mem.perm_valid_block in H4. assumption.
Qed.
Lemma matchOptE: forall {A} (a:option A) (P: A -> Prop),
match a with Some b => P b | None => False end ->
exists b, a = Some b /\ P b.
Proof. intros. destruct a; try contradiction. exists a; auto. Qed.
Lemma compose_meminjD_None: forall j jj b,
(compose_meminj j jj) b = None ->
j b = None \/
(exists b', exists ofs, j b = Some(b',ofs) /\ jj b' = None).
Proof.
unfold compose_meminj; intros.
destruct (j b).
destruct p. right.
remember (jj b0) as zz; destruct zz. destruct p. inv H.
exists b0. exists z. rewrite <- Heqzz. auto.
left; trivial.
Qed.
Lemma compose_meminjD_Some: forall j jj b b2 ofs2,
(compose_meminj j jj) b = Some(b2,ofs2) ->
exists b1, exists ofs1, exists ofs,
j b = Some(b1,ofs1) /\ jj b1 = Some(b2,ofs) /\ ofs2=ofs1+ofs.
Proof. unfold compose_meminj; intros.
remember (j b) as z; destruct z; apply eq_sym in Heqz.
destruct p. exists b0. exists z.
remember (jj b0) as zz.
destruct zz; apply eq_sym in Heqzz.
destruct p. inv H. exists z0. auto.
inv H.
inv H.
Qed.
Lemma compose_meminj_inject_incr: forall j12 j12' j23 j23'
(InjIncr12: inject_incr j12 j12') (InjIncr23: inject_incr j23 j23'),
inject_incr (compose_meminj j12 j23) (compose_meminj j12' j23').
Proof.
intros. intros b; intros.
apply compose_meminjD_Some in H.
destruct H as [b1 [ofs1 [ofs [Hb [Hb1 Hdelta]]]]].
unfold compose_meminj.
rewrite (InjIncr12 _ _ _ Hb).
rewrite (InjIncr23 _ _ _ Hb1). subst. trivial.
Qed.
Lemma compose_meminj_inject_separated: forall j12 j12' j23 j23' m1 m2 m3
(InjSep12 : inject_separated j12 j12' m1 m2)
(InjSep23 : inject_separated j23 j23' m2 m3)
(InjIncr12: inject_incr j12 j12') (InjIncr23: inject_incr j23 j23')
(BV12: forall b1 b2 ofs, j12 b1 = Some (b2,ofs) -> Mem.valid_block m1 b1 /\ Mem.valid_block m2 b2)
(BV23: forall b1 b2 ofs, j23 b1 = Some (b2,ofs) -> Mem.valid_block m2 b1 /\ Mem.valid_block m3 b2),
inject_separated (compose_meminj j12 j23) (compose_meminj j12' j23') m1 m3.
Proof. intros.
unfold compose_meminj; intros b; intros.
remember (j12 b) as j12b.
destruct j12b; inv H; apply eq_sym in Heqj12b. destruct p.
rewrite (InjIncr12 _ _ _ Heqj12b) in H0.
remember (j23 b0) as j23b0.
destruct j23b0; inv H2; apply eq_sym in Heqj23b0. destruct p. inv H1.
remember (j23' b0) as j23'b0.
destruct j23'b0; inv H0; apply eq_sym in Heqj23'b0. destruct p. inv H1.
destruct (InjSep23 _ _ _ Heqj23b0 Heqj23'b0).
split; trivial. exfalso. apply H. eapply BV12. apply Heqj12b.
remember (j12' b) as j12'b.
destruct j12'b; inv H0; apply eq_sym in Heqj12'b. destruct p.
destruct (InjSep12 _ _ _ Heqj12b Heqj12'b). split; trivial.
remember (j23' b0) as j23'b0.
destruct j23'b0; inv H1; apply eq_sym in Heqj23'b0. destruct p. inv H3.
remember (j23 b0) as j23b0.
destruct j23b0; apply eq_sym in Heqj23b0. destruct p.
rewrite (InjIncr23 _ _ _ Heqj23b0) in Heqj23'b0. inv Heqj23'b0.
exfalso. apply H0. eapply BV23. apply Heqj23b0.
destruct (InjSep23 _ _ _ Heqj23b0 Heqj23'b0). assumption.
Qed.
Lemma compose_meminj_inject_separated': forall j12 j12' j23 j23' m1 m2 m3
(InjSep12 : inject_separated j12 j12' m1 m2)
(InjSep23 : inject_separated j23 j23' m2 m3)
(InjIncr12: inject_incr j12 j12') (InjIncr23: inject_incr j23 j23')
(MInj12: Mem.inject j12 m1 m2)
(MInj23: Mem.inject j23 m2 m3),
inject_separated (compose_meminj j12 j23) (compose_meminj j12' j23') m1 m3.
Proof. intros.
eapply compose_meminj_inject_separated; try eassumption.
intros; split. apply (Mem.valid_block_inject_1 _ _ _ _ _ _ H MInj12).
apply (Mem.valid_block_inject_2 _ _ _ _ _ _ H MInj12).
intros; split. apply (Mem.valid_block_inject_1 _ _ _ _ _ _ H MInj23).
apply (Mem.valid_block_inject_2 _ _ _ _ _ _ H MInj23).
Qed.
Lemma forall_lessdef_refl: forall vals, Forall2 Val.lessdef vals vals.
Proof. induction vals; econstructor; trivial. Qed.
Lemma lessdef_hastype: forall v v' (V:Val.lessdef v v') T,
Val.has_type v' T -> Val.has_type v T.
Proof. intros. inv V; simpl; trivial. Qed.
Lemma forall_lessdef_hastype: forall vals vals'
(V:Forall2 Val.lessdef vals vals') Ts
(HTs: Forall2 Val.has_type vals' Ts),
Forall2 Val.has_type vals Ts.
Proof.
intros vals vals' V.
induction V; simpl in *; intros.
trivial.
inv HTs. constructor. eapply lessdef_hastype; eassumption. apply (IHV _ H4).
Qed.
Lemma valinject_hastype: forall j v v'
(V: (val_inject j) v v') T,
Val.has_type v' T -> Val.has_type v T.
Proof. intros. inv V; simpl; trivial. Qed.
Lemma forall_valinject_hastype: forall j vals vals'
(V: Forall2 (val_inject j) vals vals')
Ts (HTs: Forall2 Val.has_type vals' Ts),
Forall2 Val.has_type vals Ts.
Proof.
intros j vals vals' V.
induction V; simpl in *; intros.
trivial.
inv HTs. constructor. eapply valinject_hastype; eassumption. apply (IHV _ H4).
Qed.
Definition val_inject_opt (j: meminj) (v1 v2: option val) :=
match v1, v2 with Some v1', Some v2' => val_inject j v1' v2'
| None, None => True
| _, _ => False
end.
Lemma val_inject_split:
forall v1 v3 j12 j23 (V: val_inject (compose_meminj j12 j23) v1 v3),
exists v2, val_inject j12 v1 v2 /\ val_inject j23 v2 v3.
Proof. intros.
inv V.
exists (Vint i). split; constructor.
exists (Vlong i); split; constructor.
exists (Vfloat f). split; constructor.
exists (Vsingle f). split; constructor.
apply compose_meminjD_Some in H. rename b2 into b3.
destruct H as [b2 [ofs2 [ofs3 [J12 [J23 DD]]]]]; subst.
eexists. split. econstructor. apply J12. reflexivity.
econstructor. apply J23. rewrite Int.add_assoc.
assert (H: Int.repr (ofs2 + ofs3) = Int.add (Int.repr ofs2) (Int.repr ofs3)).
clear - ofs2 ofs3. rewrite Int.add_unsigned.
apply Int.eqm_samerepr. apply Int.eqm_add; apply Int.eqm_unsigned_repr.
rewrite H. trivial.
exists Vundef. split; constructor.
Qed.
Lemma forall_lessdef_trans:
forall vals1 vals2 (V12: Forall2 Val.lessdef vals1 vals2)
vals3 (V23: Forall2 Val.lessdef vals2 vals3),
Forall2 Val.lessdef vals1 vals3.
Proof. intros vals1 vals2 V12.
induction V12; intros; inv V23; econstructor.
eapply Val.lessdef_trans; eauto.
eapply IHV12; trivial.
Qed.
Lemma extends_loc_out_of_bounds:
forall m1 m2 (Ext: Mem.extends m1 m2) b ofs,
loc_out_of_bounds m2 b ofs -> loc_out_of_bounds m1 b ofs.
Proof. intros.
inv Ext. inv mext_inj.
intros N. eapply H. apply (mi_perm _ b 0) in N.
rewrite Zplus_0_r in N. assumption. reflexivity.
Qed.
Lemma extends_loc_out_of_reach:
forall m1 m2 (Ext: Mem.extends m1 m2) b ofs j
(Hj: loc_out_of_reach j m2 b ofs), loc_out_of_reach j m1 b ofs.
Proof.
intros. unfold loc_out_of_reach in *. intros.
intros N. eapply (Hj _ _ H). clear Hj H. inv Ext. inv mext_inj.
specialize (mi_perm b0 _ 0 (ofs - delta) Max Nonempty (eq_refl _)).
rewrite Zplus_0_r in mi_perm. apply (mi_perm N).
Qed.
Lemma valinject_lessdef:
forall v1 v2 v3 j (V12:val_inject j v1 v2) (V23 : Val.lessdef v2 v3),
val_inject j v1 v3.
Proof.
intros.
inv V12; inv V23; try constructor.
econstructor. eassumption. trivial.
Qed.
Lemma forall_valinject_lessdef:
forall vals1 vals2 j (VInj12 : Forall2 (val_inject j) vals1 vals2) vals3
(LD23 : Forall2 Val.lessdef vals2 vals3), Forall2 (val_inject j) vals1 vals3.
Proof. intros vals1 vals2 j VInj12.
induction VInj12; intros; inv LD23. constructor.
econstructor. eapply valinject_lessdef; eassumption.
eapply (IHVInj12 _ H4).
Qed.
Lemma val_lessdef_inject_compose:
forall v1 v2 (LD12 : Val.lessdef v1 v2) j v3
(InjV23 : val_inject j v2 v3), val_inject j v1 v3.
Proof. intros.
apply val_inject_id in LD12.
apply (val_inject_compose _ _ _ _ _ LD12) in InjV23.
rewrite <- compose_meminj_idL in InjV23. assumption.
Qed.
Lemma forall_val_lessdef_inject_compose:
forall v1 v2 (LD12 : Forall2 Val.lessdef v1 v2) j v3
(InjV23 : Forall2 (val_inject j) v2 v3), Forall2 (val_inject j) v1 v3.
Proof. intros v1 v2 H.
induction H; intros; inv InjV23; econstructor.
eapply val_lessdef_inject_compose; eassumption.
apply (IHForall2 _ _ H5).
Qed.
Lemma forall_val_inject_compose:
forall vals1 vals2 j1 (ValsInj12 : Forall2 (val_inject j1) vals1 vals2)
vals3 j2 (ValsInj23 : Forall2 (val_inject j2) vals2 vals3),
Forall2 (val_inject (compose_meminj j1 j2)) vals1 vals3.
Proof.
intros vals1 vals2 j1 ValsInj12.
induction ValsInj12; intros; inv ValsInj23; econstructor.
eapply val_inject_compose; eassumption.
apply (IHValsInj12 _ _ H4).
Qed.
Lemma val_inject_flat:
forall m1 m2 j (Inj: Mem.inject j m1 m2) v1 v2 (V: val_inject j v1 v2),
val_inject (Mem.flat_inj (Mem.nextblock m1)) v1 v1.
Proof. intros.
inv V; try constructor.
apply Val.inject_ptr with (delta:=0).
unfold Mem.flat_inj. inv Inj.
destruct (plt b1 (Mem.nextblock m1)).
trivial.
assert (j b1 = None).
apply mi_freeblocks. assumption. rewrite H in H0. inv H0.
rewrite Int.add_zero. trivial.
Qed.
Lemma forall_val_inject_flat: forall m1 m2 j (Inj: Mem.inject j m1 m2) vals1 vals2
(V: Forall2 (val_inject j) vals1 vals2),
Forall2 (val_inject (Mem.flat_inj (Mem.nextblock m1))) vals1 vals1.
Proof. intros.
induction V; intros; try econstructor.
eapply val_inject_flat; eassumption.
apply IHV.
Qed.
Lemma po_trans: forall a b c, Mem.perm_order'' a b -> Mem.perm_order'' b c ->
Mem.perm_order'' a c.
Proof. intros.
destruct a; destruct b; destruct c; simpl in *; try contradiction; try trivial.
eapply perm_order_trans; eassumption.
Qed.
Lemma extends_perm: forall m1 m2 (Ext: Mem.extends m1 m2) b ofs k p,
Mem.perm m1 b ofs k p -> Mem.perm m2 b ofs k p.
Proof. intros. destruct Ext. destruct mext_inj.
specialize (mi_perm b b 0 ofs k p (eq_refl _) H).
rewrite Zplus_0_r in mi_perm. assumption.
Qed.
Lemma extends_permorder: forall m1 m2 (Ext: Mem.extends m1 m2) (b:block) ofs k,
Mem.perm_order'' (PMap.get b (Mem.mem_access m2) ofs k)
(PMap.get b (Mem.mem_access m1) ofs k).
Proof.
intros. destruct Ext. destruct mext_inj as [prm _ _].
specialize (prm b b 0 ofs k). unfold Mem.perm in prm.
remember ((PMap.get b (Mem.mem_access m2) ofs k)) as z.
destruct z; apply eq_sym in Heqz; simpl in *.
remember ((PMap.get b (Mem.mem_access m1) ofs k)) as zz.
destruct zz; trivial; apply eq_sym in Heqzz; simpl in *.
rewrite Zplus_0_r in prm. rewrite Heqz in prm.
specialize (prm p0 (eq_refl _)). apply prm. apply perm_refl.
remember ((PMap.get b (Mem.mem_access m1) ofs k)) as zz.
destruct zz; trivial; apply eq_sym in Heqzz; simpl in *.
rewrite Zplus_0_r in prm. rewrite Heqz in prm.
specialize (prm p (eq_refl _)). exfalso. apply prm. apply perm_refl.
Qed.
Lemma fwd_maxperm: forall m1 m2 (FWD: mem_forward m1 m2) b
(V:Mem.valid_block m1 b) ofs p,
Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p.
Proof. intros. apply FWD; assumption. Qed.
Lemma fwd_maxpermorder: forall m1 m2 (FWD: mem_forward m1 m2) (b:block)
(V:Mem.valid_block m1 b) ofs,
Mem.perm_order'' (PMap.get b (Mem.mem_access m1) ofs Max)
(PMap.get b (Mem.mem_access m2) ofs Max).
Proof.
intros. destruct (FWD b) as [? ?]; try assumption.
remember ((PMap.get b (Mem.mem_access m2) ofs Max)) as z.
destruct z; apply eq_sym in Heqz; simpl in *.
remember ((PMap.get b (Mem.mem_access m1) ofs Max)) as zz.
destruct zz; apply eq_sym in Heqzz; simpl in *.
specialize (H0 ofs p). unfold Mem.perm in H0. unfold Mem.perm_order' in H0.
rewrite Heqz in H0. rewrite Heqzz in H0. apply H0. apply perm_refl.
specialize (H0 ofs p). unfold Mem.perm in H0. unfold Mem.perm_order' in H0.
rewrite Heqz in H0. rewrite Heqzz in H0. apply H0. apply perm_refl.
remember ((PMap.get b (Mem.mem_access m1) ofs Max)) as zz.
destruct zz; apply eq_sym in Heqzz; simpl in *; trivial.
Qed.
Lemma po_oo: forall p q, Mem.perm_order' p q = Mem.perm_order'' p (Some q).
Proof. intros. destruct p; trivial. Qed.
Lemma inject_permorder:
forall j m1 m2 (Inj : Mem.inject j m1 m2) (b b':block) ofs'
(J: j b = Some (b', ofs')) ofs k,
Mem.perm_order'' (PMap.get b' (Mem.mem_access m2) (ofs + ofs') k)
(PMap.get b (Mem.mem_access m1) ofs k).
Proof.
intros. destruct Inj. destruct mi_inj as [prm _ _ ].
specialize (prm b b' ofs' ofs k). unfold Mem.perm in prm.
remember ((PMap.get b' (Mem.mem_access m2) (ofs + ofs') k)) as z.
destruct z; apply eq_sym in Heqz; simpl in *.
remember ((PMap.get b (Mem.mem_access m1) ofs k)) as zz.
destruct zz; trivial; apply eq_sym in Heqzz; simpl in *.
eapply prm. apply J. apply perm_refl.
remember ((PMap.get b (Mem.mem_access m1) ofs k)) as zz.
destruct zz; trivial; apply eq_sym in Heqzz; simpl in *.
eapply prm. apply J. apply perm_refl.
Qed.
Lemma PermExtNotnonempty:
forall m1 m2
(Inj: Mem.extends m1 m2) b ofs p
(H: ~ Mem.perm m2 b ofs p Nonempty),
~Mem.perm m1 b ofs p Nonempty.
Proof. intros. destruct Inj. destruct mext_inj.
intros N. apply H. apply (mi_perm _ _ _ _ _ _ (eq_refl _)) in N.
rewrite Zplus_0_r in N. apply N.
Qed.
Lemma PermInjNotnonempty:
forall j m1 m2
(Inj: Mem.inject j m1 m2) b b2 delta (J:j b = Some(b2,delta)) ofs p
(H: ~Mem.perm m2 b2 (ofs+delta) p Nonempty),
~Mem.perm m1 b ofs p Nonempty.
Proof. intros. destruct Inj. destruct mi_inj.
intros N. apply H. apply (mi_perm _ _ _ _ _ _ J) in N. apply N.
Qed.
Lemma inject_LOOR_LOOB:
forall m1 m2 j (Minj12 : Mem.inject j m1 m2) m3 m3',
Mem.unchanged_on (loc_out_of_reach j m1) m3 m3' ->
Mem.unchanged_on (loc_out_of_bounds m2) m3 m3'.
Proof. intros.
split; intros; eapply H; trivial.
intros b2; intros.
unfold loc_out_of_bounds in H0. intros N. apply H0.
inv Minj12. inv mi_inj. apply (mi_perm _ _ _ _ _ _ H2) in N.
rewrite <- Zplus_comm in N. rewrite Zplus_minus in N. apply N.
intros b2; intros. unfold loc_out_of_bounds in H0. intros N. apply H0.
inv Minj12. inv mi_inj. apply (mi_perm _ _ _ _ _ _ H2) in N.
rewrite <- Zplus_comm in N. rewrite Zplus_minus in N. apply N.
Qed.
Lemma free_neutral:
forall (thr : block) (m : mem) (lo hi : Z) (b : block) (m' : Mem.mem')
(FREE: Mem.free m b lo hi = Some m'),
Mem.inject_neutral thr m -> Mem.inject_neutral thr m'.
Proof. intros. inv H.
split; intros.
apply flatinj_E in H. destruct H as [? [? ?]]; subst.
rewrite Zplus_0_r. assumption.
apply flatinj_E in H. destruct H as [? [? ?]]; subst.
apply Z.divide_0_r.
apply flatinj_E in H. destruct H as [? [? ?]]; subst.
rewrite Zplus_0_r.
assert (X: Mem.flat_inj thr b1 = Some (b1,0)).
{ apply flatinj_I. assumption. }
assert (Y:= Mem.perm_free_3 _ _ _ _ _ FREE _ _ _ _ H0).
specialize (mi_memval _ _ _ _ X Y). rewrite Zplus_0_r in *.
rewrite (Mem.free_result _ _ _ _ _ FREE) in *. simpl in *.
apply mi_memval.
Qed.
Lemma getN_aux: forall n p c B1 v B2, Mem.getN n p c = B1 ++ v::B2 ->
v = ZMap.get (p + Z.of_nat (length B1)) c.
Proof. intros n.
induction n; simpl; intros.
destruct B1; simpl in *. inv H. inv H.
destruct B1; simpl in *.
inv H. rewrite Zplus_0_r. trivial.
inv H. specialize (IHn _ _ _ _ _ H2). subst.
rewrite Zpos_P_of_succ_nat.
remember (Z.of_nat (length B1)) as m. clear Heqm H2. rewrite <- Z.add_1_l.
rewrite Zplus_assoc. trivial.
Qed.
Lemma getN_range: forall n ofs M bytes1 v bytes2,
Mem.getN n ofs M = bytes1 ++ v::bytes2 ->
(length bytes1 < n)%nat.
Proof. intros n.
induction n; simpl; intros.
destruct bytes1; inv H.
destruct bytes1; simpl in *; inv H.
omega.
specialize (IHn _ _ _ _ _ H2). omega.
Qed.
Lemma loadbytes_D: forall m b ofs n bytes
(LD: Mem.loadbytes m b ofs n = Some bytes),
Mem.range_perm m b ofs (ofs + n) Cur Readable /\
bytes = Mem.getN (nat_of_Z n) ofs (PMap.get b (Mem.mem_contents m)).
Proof. intros.
Transparent Mem.loadbytes.
unfold Mem.loadbytes in LD.
Opaque Mem.loadbytes.
remember (Mem.range_perm_dec m b ofs (ofs + n) Cur Readable) as d.
destruct d; inv LD. auto.
Qed.
(*A new lemma, used in CompCert2.5 where CompComp-4-CompCert2.1 used decode_val_pointer_inv*)
Lemma load_ptr_is_fragment ch m b ofs b0 i
(LD: Mem.load ch m b ofs = Some (Vptr b0 i)):
exists q n, ZMap.get ofs (Mem.mem_contents m) !! b = Fragment (Vptr b0 i) q n.
Proof.
apply Mem.load_result in LD.
apply eq_sym in LD.
unfold decode_val in LD.
remember (proj_bytes
(Mem.getN (size_chunk_nat ch) ofs (Mem.mem_contents m) !! b)) as v.
destruct v.
+ destruct ch; inv LD.
+ destruct ch; try solve [inv LD].
- unfold Val.load_result in LD. unfold proj_bytes in Heqv. simpl in *.
remember (ZMap.get ofs (Mem.mem_contents m) !! b) as w.
destruct w; try discriminate. clear Heqv.
destruct (Val.eq v v); try discriminate.
destruct q; try discriminate. simpl in *.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *. subst v0. clear e e0 e2.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate. simpl in LD.
destruct v; try discriminate. inv LD. eexists; eexists; reflexivity.
- unfold Val.load_result in LD. unfold proj_bytes in Heqv. simpl in *.
remember (ZMap.get ofs (Mem.mem_contents m) !! b) as w.
destruct w; try discriminate. clear Heqv.
destruct (Val.eq v v); try discriminate.
destruct q; try discriminate. simpl in *.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *. subst v0. clear e e0 e2.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q32 Q32); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate. simpl in LD.
destruct v; try discriminate. inv LD. eexists; eexists; reflexivity.
- unfold Val.load_result in LD. unfold proj_bytes in Heqv. simpl in *.
remember (ZMap.get ofs (Mem.mem_contents m) !! b) as w.
destruct w; try discriminate. clear Heqv.
destruct (Val.eq v v); try discriminate.
destruct q; try discriminate. simpl in *.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e e0 e2.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 + 1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 + 1+1+1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 +1 +1 +1 + 1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate.
destruct n; try discriminate. simpl in LD.
destruct (ZMap.get (ofs + 1 + 1 +1 +1 +1 + 1+1) (Mem.mem_contents m) !! b); try discriminate.
destruct (Val.eq v v0); try discriminate.
destruct q; try discriminate.
destruct (quantity_eq Q64 Q64); try discriminate. simpl in *. subst v0. clear e0.
destruct n; try discriminate. simpl in LD. subst v. eexists; eexists; reflexivity.
Qed.
(******** Compatibility of memory operation with [mem_forward] ********)
Lemma load_storebytes_nil m b ofs m': Mem.storebytes m b ofs nil = Some m' ->
forall ch bb z, Mem.load ch m' bb z = Mem.load ch m bb z.
Proof. intros.
remember (Mem.load ch m' bb z) as u'; symmetry in Hequ'; destruct u'.
symmetry.
eapply Mem.load_unchanged_on; try eassumption.
instantiate (1:= fun b ofs => True).
split; intros.
rewrite (Mem.nextblock_storebytes _ _ _ _ _ H); apply Ple_refl.
split; intros.
eapply Mem.perm_storebytes_2; eassumption.
eapply Mem.perm_storebytes_1; eassumption.
rewrite (Mem.storebytes_mem_contents _ _ _ _ _ H).
destruct (eq_block b0 b); subst. rewrite PMap.gss; trivial.
rewrite PMap.gso; trivial.
intros; simpl; trivial.
remember (Mem.load ch m bb z) as u; symmetry in Hequ; destruct u; trivial.
rewrite <- Hequ'; clear Hequ'.
eapply Mem.load_unchanged_on; try eassumption.
instantiate (1:= fun b ofs => True).
split; intros.
rewrite (Mem.nextblock_storebytes _ _ _ _ _ H); apply Ple_refl.
split; intros.
eapply Mem.perm_storebytes_1; eassumption.
eapply Mem.perm_storebytes_2; eassumption.
rewrite (Mem.storebytes_mem_contents _ _ _ _ _ H).
destruct (eq_block b0 b); subst. rewrite PMap.gss; trivial.
rewrite PMap.gso; trivial.
intros; simpl; trivial.
Qed.
Lemma loadbytes_storebytes_nil m b ofs m': Mem.storebytes m b ofs nil = Some m' ->
forall n bb z, Mem.loadbytes m' bb z n = Mem.loadbytes m bb z n.
Proof. intros.
remember (Mem.loadbytes m' bb z n) as u'; symmetry in Hequ'; destruct u'.
symmetry.
eapply Mem.loadbytes_unchanged_on; try eassumption.
instantiate (1:= fun b ofs => True).
split; intros.
rewrite (Mem.nextblock_storebytes _ _ _ _ _ H); apply Ple_refl.
split; intros.
eapply Mem.perm_storebytes_2; eassumption.
eapply Mem.perm_storebytes_1; eassumption.
rewrite (Mem.storebytes_mem_contents _ _ _ _ _ H).
destruct (eq_block b0 b); subst. rewrite PMap.gss; trivial.
rewrite PMap.gso; trivial.
intros; simpl; trivial.
remember (Mem.loadbytes m bb z n) as u; symmetry in Hequ; destruct u; trivial.
rewrite <- Hequ'; clear Hequ'.
eapply Mem.loadbytes_unchanged_on; try eassumption.
instantiate (1:= fun b ofs => True).
split; intros.
rewrite (Mem.nextblock_storebytes _ _ _ _ _ H); apply Ple_refl.
split; intros.
eapply Mem.perm_storebytes_1; eassumption.
eapply Mem.perm_storebytes_2; eassumption.
rewrite (Mem.storebytes_mem_contents _ _ _ _ _ H).
destruct (eq_block b0 b); subst. rewrite PMap.gss; trivial.
rewrite PMap.gso; trivial.
intros; simpl; trivial.
Qed.
Lemma storebytes_forward: forall m b ofs bytes m'
(M: Mem.storebytes m b ofs bytes = Some m'),
mem_forward m m'.
Proof. intros.
split; intros.
eapply Mem.storebytes_valid_block_1; eassumption.
eapply Mem.perm_storebytes_2; eassumption.
Qed.
Lemma storebytes_readonlyLD: forall m b ofs bytes m'
(M: Mem.storebytes m b ofs bytes = Some m'),
forall b, Mem.valid_block m b -> readonlyLD m b m'.
Proof.
red; intros.
split.
destruct bytes.
eapply load_storebytes_nil; eassumption.
eapply Mem.load_storebytes_other; try eassumption.
destruct (eq_block b0 b); subst. 2: left; trivial. right.
apply Mem.storebytes_range_perm in M.
destruct (zle (ofs0 + size_chunk chunk) ofs). left; trivial. right.
destruct (zle (ofs + Z.of_nat (length (m0 :: bytes))) ofs0); trivial.
exfalso.
destruct (zle ofs0 ofs).
apply (NWR ofs). omega.
(*eapply Mem.perm_max.*) apply M. simpl. specialize (Zle_0_nat (length bytes)); intros; xomega.
elim (NWR ofs0). specialize (size_chunk_pos chunk); intros; omega.
(*eapply Mem.perm_max.*) apply M. omega.
split; intros. eapply Mem.perm_storebytes_1; eassumption. eapply Mem.perm_storebytes_2; eassumption.
Qed.