-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathCminorgenproof.v
More file actions
2261 lines (2073 loc) · 81.5 KB
/
Cminorgenproof.v
File metadata and controls
2261 lines (2073 loc) · 81.5 KB
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
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* *********************************************************************)
(** Correctness proof for Cminor generation. *)
Require Import Coq.Program.Equality FSets Permutation.
Require Import FSets FSetAVL Orders Mergesort.
Require Import Coqlib Maps Ordered Errors Integers Floats.
Require Intv.
Require Import AST Linking.
Require Import Values Memory Events Globalenvs Smallstep.
Require Import Csharpminor Switch Cminor Cminorgen.
Local Open Scope error_monad_scope.
Definition match_prog (p: Csharpminor.program) (tp: Cminor.program) :=
match_program (fun cu f tf => transl_fundef f = OK tf) eq p tp.
Lemma transf_program_match:
forall p tp, transl_program p = OK tp -> match_prog p tp.
Proof.
intros. apply match_transform_partial_program; auto.
Qed.
Section TRANSLATION.
Variable prog: Csharpminor.program.
Variable tprog: program.
Hypothesis TRANSL: match_prog prog tprog.
Let ge : Csharpminor.genv := Genv.globalenv prog.
Let tge: genv := Genv.globalenv tprog.
Lemma symbols_preserved:
forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
Proof (Genv.find_symbol_transf_partial TRANSL).
Lemma senv_preserved:
Senv.equiv ge tge.
Proof (Genv.senv_transf_partial TRANSL).
Lemma function_ptr_translated:
forall (b: block) (f: Csharpminor.fundef),
Genv.find_funct_ptr ge b = Some f ->
exists tf,
Genv.find_funct_ptr tge b = Some tf /\ transl_fundef f = OK tf.
Proof (Genv.find_funct_ptr_transf_partial TRANSL).
Lemma functions_translated:
forall (v: val) (f: Csharpminor.fundef),
Genv.find_funct ge v = Some f ->
exists tf,
Genv.find_funct tge v = Some tf /\ transl_fundef f = OK tf.
Proof (Genv.find_funct_transf_partial TRANSL).
Lemma sig_preserved_body:
forall f tf cenv size,
transl_funbody cenv size f = OK tf ->
tf.(fn_sig) = Csharpminor.fn_sig f.
Proof.
intros. unfold transl_funbody in H. monadInv H; reflexivity.
Qed.
Lemma sig_preserved:
forall f tf,
transl_fundef f = OK tf ->
Cminor.funsig tf = Csharpminor.funsig f.
Proof.
intros until tf; destruct f; simpl.
unfold transl_function. destruct (build_compilenv f).
case (zle z Ptrofs.max_unsigned); simpl bind; try congruence.
intros. monadInv H. simpl. eapply sig_preserved_body; eauto.
intro. inv H. reflexivity.
Qed.
(** * Derived properties of memory operations *)
Lemma load_freelist:
forall fbl chunk m b ofs m',
(forall b' lo hi, In (b', lo, hi) fbl -> b' <> b) ->
Mem.free_list m fbl = Some m' ->
Mem.load chunk m' b ofs = Mem.load chunk m b ofs.
Proof.
induction fbl; intros.
simpl in H0. congruence.
destruct a as [[b' lo] hi].
generalize H0. simpl. case_eq (Mem.free m b' lo hi); try congruence.
intros m1 FR1 FRL.
transitivity (Mem.load chunk m1 b ofs).
eapply IHfbl; eauto. intros. eapply H. eauto with coqlib.
eapply Mem.load_free; eauto. left. apply not_eq_sym. eapply H. auto with coqlib.
Qed.
Lemma perm_freelist:
forall fbl m m' b ofs k p,
Mem.free_list m fbl = Some m' ->
Mem.perm m' b ofs k p ->
Mem.perm m b ofs k p.
Proof.
induction fbl; simpl; intros until p.
congruence.
destruct a as [[b' lo] hi]. case_eq (Mem.free m b' lo hi); try congruence.
intros. eauto with mem.
Qed.
Lemma nextblock_freelist:
forall fbl m m',
Mem.free_list m fbl = Some m' ->
Mem.nextblock m' = Mem.nextblock m.
Proof.
induction fbl; intros until m'; simpl.
congruence.
destruct a as [[b lo] hi].
case_eq (Mem.free m b lo hi); intros; try congruence.
transitivity (Mem.nextblock m0). eauto. eapply Mem.nextblock_free; eauto.
Qed.
Lemma free_list_freeable:
forall l m m',
Mem.free_list m l = Some m' ->
forall b lo hi,
In (b, lo, hi) l -> Mem.range_perm m b lo hi Cur Freeable.
Proof.
induction l; simpl; intros.
contradiction.
revert H. destruct a as [[b' lo'] hi'].
caseEq (Mem.free m b' lo' hi'); try congruence.
intros m1 FREE1 FREE2.
destruct H0. inv H.
eauto with mem.
red; intros. eapply Mem.perm_free_3; eauto. exploit IHl; eauto.
Qed.
Lemma nextblock_storev:
forall chunk m addr v m',
Mem.storev chunk m addr v = Some m' -> Mem.nextblock m' = Mem.nextblock m.
Proof.
unfold Mem.storev; intros. destruct addr; try discriminate.
eapply Mem.nextblock_store; eauto.
Qed.
(** * Correspondence between C#minor's and Cminor's environments and memory states *)
(** In C#minor, every variable is stored in a separate memory block.
In the corresponding Cminor code, these variables become sub-blocks
of the stack data block. We capture these changes in memory via a
memory injection [f]:
[f b = Some(b', ofs)] means that C#minor block [b] corresponds
to a sub-block of Cminor block [b] at offset [ofs].
A memory injection [f] defines a relation [Val.inject f] between
values and a relation [Mem.inject f] between memory states. These
relations will be used intensively in our proof of simulation
between C#minor and Cminor executions. *)
(** ** Matching between Cshaprminor's temporaries and Cminor's variables *)
Definition match_temps (f: meminj) (le: Csharpminor.temp_env) (te: env) : Prop :=
forall id v, le!id = Some v -> exists v', te!(id) = Some v' /\ Val.inject f v v'.
Lemma match_temps_invariant:
forall f f' le te,
match_temps f le te ->
inject_incr f f' ->
match_temps f' le te.
Proof.
intros; red; intros. destruct (H _ _ H1) as [v' [A B]]. exists v'; eauto.
Qed.
Lemma match_temps_assign:
forall f le te id v tv,
match_temps f le te ->
Val.inject f v tv ->
match_temps f (PTree.set id v le) (PTree.set id tv te).
Proof.
intros; red; intros. rewrite PTree.gsspec in *. destruct (peq id0 id).
inv H1. exists tv; auto.
eauto.
Qed.
(** ** Matching between C#minor's variable environment and Cminor's stack pointer *)
Inductive match_var (f: meminj) (sp: block): option (block * Z) -> option Z -> Prop :=
| match_var_local: forall b sz ofs,
Val.inject f (Vptr b Ptrofs.zero) (Vptr sp (Ptrofs.repr ofs)) ->
match_var f sp (Some(b, sz)) (Some ofs)
| match_var_global:
match_var f sp None None.
(** Matching between a C#minor environment [e] and a Cminor
stack pointer [sp]. The [lo] and [hi] parameters delimit the range
of addresses for the blocks referenced from [te]. *)
Record match_env (f: meminj) (cenv: compilenv)
(e: Csharpminor.env) (sp: block)
(lo hi: block) : Prop :=
mk_match_env {
(** C#minor local variables match sub-blocks of the Cminor stack data block. *)
me_vars:
forall id, match_var f sp (e!id) (cenv!id);
(** [lo, hi] is a proper interval. *)
me_low_high:
Ple lo hi;
(** Every block appearing in the C#minor environment [e] must be
in the range [lo, hi]. *)
me_bounded:
forall id b sz, PTree.get id e = Some(b, sz) -> Ple lo b /\ Plt b hi;
(** All blocks mapped to sub-blocks of the Cminor stack data must be
images of variables from the C#minor environment [e] *)
me_inv:
forall b delta,
f b = Some(sp, delta) ->
exists id, exists sz, PTree.get id e = Some(b, sz);
(** All C#minor blocks below [lo] (i.e. allocated before the blocks
referenced from [e]) must map to blocks that are below [sp]
(i.e. allocated before the stack data for the current Cminor function). *)
me_incr:
forall b tb delta,
f b = Some(tb, delta) -> Plt b lo -> Plt tb sp
}.
Ltac geninv x :=
let H := fresh in (generalize x; intro H; inv H).
Lemma match_env_invariant:
forall f1 cenv e sp lo hi f2,
match_env f1 cenv e sp lo hi ->
inject_incr f1 f2 ->
(forall b delta, f2 b = Some(sp, delta) -> f1 b = Some(sp, delta)) ->
(forall b, Plt b lo -> f2 b = f1 b) ->
match_env f2 cenv e sp lo hi.
Proof.
intros. destruct H. constructor; auto.
(* vars *)
intros. geninv (me_vars0 id); econstructor; eauto.
(* bounded *)
intros. eauto.
(* below *)
intros. rewrite H2 in H; eauto.
Qed.
(** [match_env] and external calls *)
Remark inject_incr_separated_same:
forall f1 f2 m1 m1',
inject_incr f1 f2 -> inject_separated f1 f2 m1 m1' ->
forall b, Mem.valid_block m1 b -> f2 b = f1 b.
Proof.
intros. case_eq (f1 b).
intros [b' delta] EQ. apply H; auto.
intros EQ. case_eq (f2 b).
intros [b'1 delta1] EQ1. exploit H0; eauto. intros [C D]. contradiction.
auto.
Qed.
Remark inject_incr_separated_same':
forall f1 f2 m1 m1',
inject_incr f1 f2 -> inject_separated f1 f2 m1 m1' ->
forall b b' delta,
f2 b = Some(b', delta) -> Mem.valid_block m1' b' -> f1 b = Some(b', delta).
Proof.
intros. case_eq (f1 b).
intros [b'1 delta1] EQ. exploit H; eauto. congruence.
intros. exploit H0; eauto. intros [C D]. contradiction.
Qed.
Lemma match_env_external_call:
forall f1 cenv e sp lo hi f2 m1 m1',
match_env f1 cenv e sp lo hi ->
inject_incr f1 f2 ->
inject_separated f1 f2 m1 m1' ->
Ple hi (Mem.nextblock m1) -> Plt sp (Mem.nextblock m1') ->
match_env f2 cenv e sp lo hi.
Proof.
intros. apply match_env_invariant with f1; auto.
intros. eapply inject_incr_separated_same'; eauto.
intros. eapply inject_incr_separated_same; eauto. red. destruct H. xomega.
Qed.
(** [match_env] and allocations *)
Lemma match_env_alloc:
forall f1 id cenv e sp lo m1 sz m2 b ofs f2,
match_env f1 (PTree.remove id cenv) e sp lo (Mem.nextblock m1) ->
Mem.alloc m1 0 sz = (m2, b) ->
cenv!id = Some ofs ->
inject_incr f1 f2 ->
f2 b = Some(sp, ofs) ->
(forall b', b' <> b -> f2 b' = f1 b') ->
e!id = None ->
match_env f2 cenv (PTree.set id (b, sz) e) sp lo (Mem.nextblock m2).
Proof.
intros until f2; intros ME ALLOC CENV INCR SAME OTHER ENV.
exploit Mem.nextblock_alloc; eauto. intros NEXTBLOCK.
exploit Mem.alloc_result; eauto. intros RES.
inv ME; constructor.
(* vars *)
intros. rewrite PTree.gsspec. destruct (peq id0 id).
(* the new var *)
subst id0. rewrite CENV. constructor. econstructor. eauto.
rewrite Ptrofs.add_commut; rewrite Ptrofs.add_zero; auto.
(* old vars *)
generalize (me_vars0 id0). rewrite PTree.gro; auto. intros M; inv M.
constructor; eauto.
constructor.
(* low-high *)
rewrite NEXTBLOCK; xomega.
(* bounded *)
intros. rewrite PTree.gsspec in H. destruct (peq id0 id).
inv H. rewrite NEXTBLOCK; xomega.
exploit me_bounded0; eauto. rewrite NEXTBLOCK; xomega.
(* inv *)
intros. destruct (eq_block b (Mem.nextblock m1)).
subst b. rewrite SAME in H; inv H. exists id; exists sz. apply PTree.gss.
rewrite OTHER in H; auto. exploit me_inv0; eauto.
intros [id1 [sz1 EQ]]. exists id1; exists sz1. rewrite PTree.gso; auto. congruence.
(* incr *)
intros. rewrite OTHER in H. eauto. unfold block in *; xomega.
Qed.
(** The sizes of blocks appearing in [e] are respected. *)
Definition match_bounds (e: Csharpminor.env) (m: mem) : Prop :=
forall id b sz ofs p,
PTree.get id e = Some(b, sz) -> Mem.perm m b ofs Max p -> 0 <= ofs < sz.
Lemma match_bounds_invariant:
forall e m1 m2,
match_bounds e m1 ->
(forall id b sz ofs p,
PTree.get id e = Some(b, sz) -> Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p) ->
match_bounds e m2.
Proof.
intros; red; intros. eapply H; eauto.
Qed.
(** ** Permissions on the Cminor stack block *)
(** The parts of the Cminor stack data block that are not images of
C#minor local variable blocks remain freeable at all times. *)
Inductive is_reachable_from_env (f: meminj) (e: Csharpminor.env) (sp: block) (ofs: Z) : Prop :=
| is_reachable_intro: forall id b sz delta,
e!id = Some(b, sz) ->
f b = Some(sp, delta) ->
delta <= ofs < delta + sz ->
is_reachable_from_env f e sp ofs.
Definition padding_freeable (f: meminj) (e: Csharpminor.env) (tm: mem) (sp: block) (sz: Z) : Prop :=
forall ofs,
0 <= ofs < sz -> Mem.perm tm sp ofs Cur Freeable \/ is_reachable_from_env f e sp ofs.
Lemma padding_freeable_invariant:
forall f1 e tm1 sp sz cenv lo hi f2 tm2,
padding_freeable f1 e tm1 sp sz ->
match_env f1 cenv e sp lo hi ->
(forall ofs, Mem.perm tm1 sp ofs Cur Freeable -> Mem.perm tm2 sp ofs Cur Freeable) ->
(forall b, Plt b hi -> f2 b = f1 b) ->
padding_freeable f2 e tm2 sp sz.
Proof.
intros; red; intros.
exploit H; eauto. intros [A | A].
left; auto.
right. inv A. exploit me_bounded; eauto. intros [D E].
econstructor; eauto. rewrite H2; auto.
Qed.
(** Decidability of the [is_reachable_from_env] predicate. *)
Lemma is_reachable_from_env_dec:
forall f e sp ofs, is_reachable_from_env f e sp ofs \/ ~is_reachable_from_env f e sp ofs.
Proof.
intros.
set (pred := fun id_b_sz : ident * (block * Z) =>
match id_b_sz with
| (id, (b, sz)) =>
match f b with
| None => false
| Some(sp', delta) =>
if eq_block sp sp'
then zle delta ofs && zlt ofs (delta + sz)
else false
end
end).
destruct (List.existsb pred (PTree.elements e)) eqn:?.
(* yes *)
rewrite List.existsb_exists in Heqb.
destruct Heqb as [[id [b sz]] [A B]].
simpl in B. destruct (f b) as [[sp' delta] |] eqn:?; try discriminate.
destruct (eq_block sp sp'); try discriminate.
destruct (andb_prop _ _ B).
left. apply is_reachable_intro with id b sz delta.
apply PTree.elements_complete; auto.
congruence.
split; eapply proj_sumbool_true; eauto.
(* no *)
right; red; intro NE; inv NE.
assert (existsb pred (PTree.elements e) = true).
rewrite List.existsb_exists. exists (id, (b, sz)); split.
apply PTree.elements_correct; auto.
simpl. rewrite H0. rewrite dec_eq_true.
unfold proj_sumbool. destruct H1. rewrite zle_true; auto. rewrite zlt_true; auto.
congruence.
Qed.
(** * Correspondence between global environments *)
(** Global environments match if the memory injection [f] leaves unchanged
the references to global symbols and functions. *)
Inductive match_globalenvs (f: meminj) (bound: block): Prop :=
| mk_match_globalenvs
(DOMAIN: forall b, Plt b bound -> f b = Some(b, 0))
(IMAGE: forall b1 b2 delta, f b1 = Some(b2, delta) -> Plt b2 bound -> b1 = b2)
(SYMBOLS: forall id b, Genv.find_symbol ge id = Some b -> Plt b bound)
(FUNCTIONS: forall b fd, Genv.find_funct_ptr ge b = Some fd -> Plt b bound)
(VARINFOS: forall b gv, Genv.find_var_info ge b = Some gv -> Plt b bound).
Remark inj_preserves_globals:
forall f hi,
match_globalenvs f hi ->
meminj_preserves_globals ge f.
Proof.
intros. inv H.
split. intros. apply DOMAIN. eapply SYMBOLS. eauto.
split. intros. apply DOMAIN. eapply VARINFOS. eauto.
intros. symmetry. eapply IMAGE; eauto.
Qed.
(** * Invariant on abstract call stacks *)
(** Call stacks represent abstractly the execution state of the current
C#minor and Cminor functions, as well as the states of the
calling functions. A call stack is a list of frames, each frame
collecting information on the current execution state of a C#minor
function and its Cminor translation. *)
Inductive frame : Type :=
Frame(cenv: compilenv)
(tf: Cminor.function)
(e: Csharpminor.env)
(le: Csharpminor.temp_env)
(te: Cminor.env)
(sp: block)
(lo hi: block).
Definition callstack : Type := list frame.
(** Matching of call stacks imply:
- matching of environments for each of the frames
- matching of the global environments
- separation conditions over the memory blocks allocated for C#minor local variables;
- separation conditions over the memory blocks allocated for Cminor stack data;
- freeable permissions on the parts of the Cminor stack data blocks
that are not images of C#minor local variable blocks.
*)
Inductive match_callstack (f: meminj) (m: mem) (tm: mem):
callstack -> block -> block -> Prop :=
| mcs_nil:
forall hi bound tbound,
match_globalenvs f hi ->
Ple hi bound -> Ple hi tbound ->
match_callstack f m tm nil bound tbound
| mcs_cons:
forall cenv tf e le te sp lo hi cs bound tbound
(BOUND: Ple hi bound)
(TBOUND: Plt sp tbound)
(MTMP: match_temps f le te)
(MENV: match_env f cenv e sp lo hi)
(BOUND: match_bounds e m)
(PERM: padding_freeable f e tm sp tf.(fn_stackspace))
(MCS: match_callstack f m tm cs lo sp),
match_callstack f m tm (Frame cenv tf e le te sp lo hi :: cs) bound tbound.
(** [match_callstack] implies [match_globalenvs]. *)
Lemma match_callstack_match_globalenvs:
forall f m tm cs bound tbound,
match_callstack f m tm cs bound tbound ->
exists hi, match_globalenvs f hi.
Proof.
induction 1; eauto.
Qed.
(** Invariance properties for [match_callstack]. *)
Lemma match_callstack_invariant:
forall f1 m1 tm1 f2 m2 tm2 cs bound tbound,
match_callstack f1 m1 tm1 cs bound tbound ->
inject_incr f1 f2 ->
(forall b ofs p, Plt b bound -> Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p) ->
(forall sp ofs, Plt sp tbound -> Mem.perm tm1 sp ofs Cur Freeable -> Mem.perm tm2 sp ofs Cur Freeable) ->
(forall b, Plt b bound -> f2 b = f1 b) ->
(forall b b' delta, f2 b = Some(b', delta) -> Plt b' tbound -> f1 b = Some(b', delta)) ->
match_callstack f2 m2 tm2 cs bound tbound.
Proof.
induction 1; intros.
(* base case *)
econstructor; eauto.
inv H. constructor; intros; eauto.
eapply IMAGE; eauto. eapply H6; eauto. xomega.
(* inductive case *)
assert (Ple lo hi) by (eapply me_low_high; eauto).
econstructor; eauto.
eapply match_temps_invariant; eauto.
eapply match_env_invariant; eauto.
intros. apply H3. xomega.
eapply match_bounds_invariant; eauto.
intros. eapply H1; eauto.
exploit me_bounded; eauto. xomega.
eapply padding_freeable_invariant; eauto.
intros. apply H3. xomega.
eapply IHmatch_callstack; eauto.
intros. eapply H1; eauto. xomega.
intros. eapply H2; eauto. xomega.
intros. eapply H3; eauto. xomega.
intros. eapply H4; eauto. xomega.
Qed.
Lemma match_callstack_incr_bound:
forall f m tm cs bound tbound bound' tbound',
match_callstack f m tm cs bound tbound ->
Ple bound bound' -> Ple tbound tbound' ->
match_callstack f m tm cs bound' tbound'.
Proof.
intros. inv H.
econstructor; eauto. xomega. xomega.
constructor; auto. xomega. xomega.
Qed.
(** Assigning a temporary variable. *)
Lemma match_callstack_set_temp:
forall f cenv e le te sp lo hi cs bound tbound m tm tf id v tv,
Val.inject f v tv ->
match_callstack f m tm (Frame cenv tf e le te sp lo hi :: cs) bound tbound ->
match_callstack f m tm (Frame cenv tf e (PTree.set id v le) (PTree.set id tv te) sp lo hi :: cs) bound tbound.
Proof.
intros. inv H0. constructor; auto.
eapply match_temps_assign; eauto.
Qed.
(** Preservation of [match_callstack] by freeing all blocks allocated
for local variables at function entry (on the C#minor side)
and simultaneously freeing the Cminor stack data block. *)
Lemma in_blocks_of_env:
forall e id b sz,
e!id = Some(b, sz) -> In (b, 0, sz) (blocks_of_env e).
Proof.
unfold blocks_of_env; intros.
change (b, 0, sz) with (block_of_binding (id, (b, sz))).
apply List.in_map. apply PTree.elements_correct. auto.
Qed.
Lemma in_blocks_of_env_inv:
forall b lo hi e,
In (b, lo, hi) (blocks_of_env e) ->
exists id, e!id = Some(b, hi) /\ lo = 0.
Proof.
unfold blocks_of_env; intros.
exploit list_in_map_inv; eauto. intros [[id [b' sz]] [A B]].
unfold block_of_binding in A. inv A.
exists id; intuition. apply PTree.elements_complete. auto.
Qed.
Lemma match_callstack_freelist:
forall f cenv tf e le te sp lo hi cs m m' tm,
Mem.inject f m tm ->
Mem.free_list m (blocks_of_env e) = Some m' ->
match_callstack f m tm (Frame cenv tf e le te sp lo hi :: cs) (Mem.nextblock m) (Mem.nextblock tm) ->
exists tm',
Mem.free tm sp 0 tf.(fn_stackspace) = Some tm'
/\ match_callstack f m' tm' cs (Mem.nextblock m') (Mem.nextblock tm')
/\ Mem.inject f m' tm'.
Proof.
intros until tm; intros INJ FREELIST MCS. inv MCS. inv MENV.
assert ({tm' | Mem.free tm sp 0 (fn_stackspace tf) = Some tm'}).
apply Mem.range_perm_free.
red; intros.
exploit PERM; eauto. intros [A | A].
auto.
inv A. assert (Mem.range_perm m b 0 sz Cur Freeable).
eapply free_list_freeable; eauto. eapply in_blocks_of_env; eauto.
replace ofs with ((ofs - delta) + delta) by omega.
eapply Mem.perm_inject; eauto. apply H3. omega.
destruct X as [tm' FREE].
exploit nextblock_freelist; eauto. intro NEXT.
exploit Mem.nextblock_free; eauto. intro NEXT'.
exists tm'. split. auto. split.
rewrite NEXT; rewrite NEXT'.
apply match_callstack_incr_bound with lo sp; try omega.
apply match_callstack_invariant with f m tm; auto.
intros. eapply perm_freelist; eauto.
intros. eapply Mem.perm_free_1; eauto. left; unfold block; xomega. xomega. xomega.
eapply Mem.free_inject; eauto.
intros. exploit me_inv0; eauto. intros [id [sz A]].
exists 0; exists sz; split.
eapply in_blocks_of_env; eauto.
eapply BOUND0; eauto. eapply Mem.perm_max. eauto.
Qed.
(** Preservation of [match_callstack] by external calls. *)
Lemma match_callstack_external_call:
forall f1 f2 m1 m2 m1' m2',
Mem.unchanged_on (loc_unmapped f1) m1 m2 ->
Mem.unchanged_on (loc_out_of_reach f1 m1) m1' m2' ->
inject_incr f1 f2 ->
inject_separated f1 f2 m1 m1' ->
(forall b ofs p, Mem.valid_block m1 b -> Mem.perm m2 b ofs Max p -> Mem.perm m1 b ofs Max p) ->
forall cs bound tbound,
match_callstack f1 m1 m1' cs bound tbound ->
Ple bound (Mem.nextblock m1) -> Ple tbound (Mem.nextblock m1') ->
match_callstack f2 m2 m2' cs bound tbound.
Proof.
intros until m2'.
intros UNMAPPED OUTOFREACH INCR SEPARATED MAXPERMS.
induction 1; intros.
(* base case *)
apply mcs_nil with hi; auto.
inv H. constructor; auto.
intros. case_eq (f1 b1).
intros [b2' delta'] EQ. rewrite (INCR _ _ _ EQ) in H. inv H. eauto.
intro EQ. exploit SEPARATED; eauto. intros [A B]. elim B. red. xomega.
(* inductive case *)
constructor. auto. auto.
eapply match_temps_invariant; eauto.
eapply match_env_invariant; eauto.
red in SEPARATED. intros. destruct (f1 b) as [[b' delta']|] eqn:?.
exploit INCR; eauto. congruence.
exploit SEPARATED; eauto. intros [A B]. elim B. red. xomega.
intros. assert (Ple lo hi) by (eapply me_low_high; eauto).
destruct (f1 b) as [[b' delta']|] eqn:?.
apply INCR; auto.
destruct (f2 b) as [[b' delta']|] eqn:?; auto.
exploit SEPARATED; eauto. intros [A B]. elim A. red. xomega.
eapply match_bounds_invariant; eauto.
intros. eapply MAXPERMS; eauto. red. exploit me_bounded; eauto. xomega.
(* padding-freeable *)
red; intros.
destruct (is_reachable_from_env_dec f1 e sp ofs).
inv H3. right. apply is_reachable_intro with id b sz delta; auto.
exploit PERM; eauto. intros [A|A]; try contradiction.
left. eapply Mem.perm_unchanged_on; eauto.
red; intros; red; intros. elim H3.
exploit me_inv; eauto. intros [id [lv B]].
exploit BOUND0; eauto. intros C.
apply is_reachable_intro with id b0 lv delta; auto; omega.
eauto with mem.
(* induction *)
eapply IHmatch_callstack; eauto. inv MENV; xomega. xomega.
Qed.
(** [match_callstack] and allocations *)
Lemma match_callstack_alloc_right:
forall f m tm cs tf tm' sp le te cenv,
match_callstack f m tm cs (Mem.nextblock m) (Mem.nextblock tm) ->
Mem.alloc tm 0 tf.(fn_stackspace) = (tm', sp) ->
Mem.inject f m tm ->
match_temps f le te ->
(forall id, cenv!id = None) ->
match_callstack f m tm'
(Frame cenv tf empty_env le te sp (Mem.nextblock m) (Mem.nextblock m) :: cs)
(Mem.nextblock m) (Mem.nextblock tm').
Proof.
intros.
exploit Mem.nextblock_alloc; eauto. intros NEXTBLOCK.
exploit Mem.alloc_result; eauto. intros RES.
constructor.
xomega.
unfold block in *; xomega.
auto.
constructor; intros.
rewrite H3. rewrite PTree.gempty. constructor.
xomega.
rewrite PTree.gempty in H4; discriminate.
eelim Mem.fresh_block_alloc; eauto. eapply Mem.valid_block_inject_2; eauto.
rewrite RES. change (Mem.valid_block tm tb). eapply Mem.valid_block_inject_2; eauto.
red; intros. rewrite PTree.gempty in H4. discriminate.
red; intros. left. eapply Mem.perm_alloc_2; eauto.
eapply match_callstack_invariant with (tm1 := tm); eauto.
rewrite RES; auto.
intros. eapply Mem.perm_alloc_1; eauto.
Qed.
Lemma match_callstack_alloc_left:
forall f1 m1 tm id cenv tf e le te sp lo cs sz m2 b f2 ofs,
match_callstack f1 m1 tm
(Frame (PTree.remove id cenv) tf e le te sp lo (Mem.nextblock m1) :: cs)
(Mem.nextblock m1) (Mem.nextblock tm) ->
Mem.alloc m1 0 sz = (m2, b) ->
cenv!id = Some ofs ->
inject_incr f1 f2 ->
f2 b = Some(sp, ofs) ->
(forall b', b' <> b -> f2 b' = f1 b') ->
e!id = None ->
match_callstack f2 m2 tm
(Frame cenv tf (PTree.set id (b, sz) e) le te sp lo (Mem.nextblock m2) :: cs)
(Mem.nextblock m2) (Mem.nextblock tm).
Proof.
intros. inv H.
exploit Mem.nextblock_alloc; eauto. intros NEXTBLOCK.
exploit Mem.alloc_result; eauto. intros RES.
assert (LO: Ple lo (Mem.nextblock m1)) by (eapply me_low_high; eauto).
constructor.
xomega.
auto.
eapply match_temps_invariant; eauto.
eapply match_env_alloc; eauto.
red; intros. rewrite PTree.gsspec in H. destruct (peq id0 id).
inversion H. subst b0 sz0 id0. eapply Mem.perm_alloc_3; eauto.
eapply BOUND0; eauto. eapply Mem.perm_alloc_4; eauto.
exploit me_bounded; eauto. unfold block in *; xomega.
red; intros. exploit PERM; eauto. intros [A|A]. auto. right.
inv A. apply is_reachable_intro with id0 b0 sz0 delta; auto.
rewrite PTree.gso. auto. congruence.
eapply match_callstack_invariant with (m1 := m1); eauto.
intros. eapply Mem.perm_alloc_4; eauto.
unfold block in *; xomega.
intros. apply H4. unfold block in *; xomega.
intros. destruct (eq_block b0 b).
subst b0. rewrite H3 in H. inv H. xomegaContradiction.
rewrite H4 in H; auto.
Qed.
(** * Correctness of stack allocation of local variables *)
(** This section shows the correctness of the translation of Csharpminor
local variables as sub-blocks of the Cminor stack data. This is the most difficult part of the proof. *)
Definition cenv_remove (cenv: compilenv) (vars: list (ident * Z)) : compilenv :=
fold_right (fun id_lv ce => PTree.remove (fst id_lv) ce) cenv vars.
Remark cenv_remove_gso:
forall id vars cenv,
~In id (map fst vars) ->
PTree.get id (cenv_remove cenv vars) = PTree.get id cenv.
Proof.
induction vars; simpl; intros.
auto.
rewrite PTree.gro. apply IHvars. intuition. intuition.
Qed.
Remark cenv_remove_gss:
forall id vars cenv,
In id (map fst vars) ->
PTree.get id (cenv_remove cenv vars) = None.
Proof.
induction vars; simpl; intros.
contradiction.
rewrite PTree.grspec. destruct (PTree.elt_eq id (fst a)). auto.
destruct H. intuition. eauto.
Qed.
Definition cenv_compat (cenv: compilenv) (vars: list (ident * Z)) (tsz: Z) : Prop :=
forall id sz,
In (id, sz) vars ->
exists ofs,
PTree.get id cenv = Some ofs
/\ Mem.inj_offset_aligned ofs sz
/\ 0 <= ofs
/\ ofs + Z.max 0 sz <= tsz.
Definition cenv_separated (cenv: compilenv) (vars: list (ident * Z)) : Prop :=
forall id1 sz1 ofs1 id2 sz2 ofs2,
In (id1, sz1) vars -> In (id2, sz2) vars ->
PTree.get id1 cenv = Some ofs1 -> PTree.get id2 cenv = Some ofs2 ->
id1 <> id2 ->
ofs1 + sz1 <= ofs2 \/ ofs2 + sz2 <= ofs1.
Definition cenv_mem_separated (cenv: compilenv) (vars: list (ident * Z)) (f: meminj) (sp: block) (m: mem) : Prop :=
forall id sz ofs b delta ofs' k p,
In (id, sz) vars -> PTree.get id cenv = Some ofs ->
f b = Some (sp, delta) ->
Mem.perm m b ofs' k p ->
ofs <= ofs' + delta < sz + ofs -> False.
Lemma match_callstack_alloc_variables_rec:
forall tm sp tf cenv le te lo cs,
Mem.valid_block tm sp ->
fn_stackspace tf <= Ptrofs.max_unsigned ->
(forall ofs k p, Mem.perm tm sp ofs k p -> 0 <= ofs < fn_stackspace tf) ->
(forall ofs k p, 0 <= ofs < fn_stackspace tf -> Mem.perm tm sp ofs k p) ->
forall e1 m1 vars e2 m2,
alloc_variables e1 m1 vars e2 m2 ->
forall f1,
list_norepet (map fst vars) ->
cenv_compat cenv vars (fn_stackspace tf) ->
cenv_separated cenv vars ->
cenv_mem_separated cenv vars f1 sp m1 ->
(forall id sz, In (id, sz) vars -> e1!id = None) ->
match_callstack f1 m1 tm
(Frame (cenv_remove cenv vars) tf e1 le te sp lo (Mem.nextblock m1) :: cs)
(Mem.nextblock m1) (Mem.nextblock tm) ->
Mem.inject f1 m1 tm ->
exists f2,
match_callstack f2 m2 tm
(Frame cenv tf e2 le te sp lo (Mem.nextblock m2) :: cs)
(Mem.nextblock m2) (Mem.nextblock tm)
/\ Mem.inject f2 m2 tm.
Proof.
intros until cs; intros VALID REPRES STKSIZE STKPERMS.
induction 1; intros f1 NOREPET COMPAT SEP1 SEP2 UNBOUND MCS MINJ.
(* base case *)
simpl in MCS. exists f1; auto.
(* inductive case *)
simpl in NOREPET. inv NOREPET.
(* exploit Mem.alloc_result; eauto. intros RES.
exploit Mem.nextblock_alloc; eauto. intros NB.*)
exploit (COMPAT id sz). auto with coqlib. intros [ofs [CENV [ALIGNED [LOB HIB]]]].
exploit Mem.alloc_left_mapped_inject.
eexact MINJ.
eexact H.
eexact VALID.
instantiate (1 := ofs). zify. omega.
intros. exploit STKSIZE; eauto. omega.
intros. apply STKPERMS. zify. omega.
replace (sz - 0) with sz by omega. auto.
intros. eapply SEP2. eauto with coqlib. eexact CENV. eauto. eauto. omega.
intros [f2 [A [B [C D]]]].
exploit (IHalloc_variables f2); eauto.
red; intros. eapply COMPAT. auto with coqlib.
red; intros. eapply SEP1; eauto with coqlib.
red; intros. exploit Mem.perm_alloc_inv; eauto. destruct (eq_block b b1); intros P.
subst b. rewrite C in H5; inv H5.
exploit SEP1. eapply in_eq. eapply in_cons; eauto. eauto. eauto.
red; intros; subst id0. elim H3. change id with (fst (id, sz0)). apply in_map; auto.
omega.
eapply SEP2. apply in_cons; eauto. eauto.
rewrite D in H5; eauto. eauto. auto.
intros. rewrite PTree.gso. eapply UNBOUND; eauto with coqlib.
red; intros; subst id0. elim H3. change id with (fst (id, sz0)). apply in_map; auto.
eapply match_callstack_alloc_left; eauto.
rewrite cenv_remove_gso; auto.
apply UNBOUND with sz; auto with coqlib.
Qed.
Lemma match_callstack_alloc_variables:
forall tm1 sp tm2 m1 vars e m2 cenv f1 cs fn le te,
Mem.alloc tm1 0 (fn_stackspace fn) = (tm2, sp) ->
fn_stackspace fn <= Ptrofs.max_unsigned ->
alloc_variables empty_env m1 vars e m2 ->
list_norepet (map fst vars) ->
cenv_compat cenv vars (fn_stackspace fn) ->
cenv_separated cenv vars ->
(forall id ofs, cenv!id = Some ofs -> In id (map fst vars)) ->
Mem.inject f1 m1 tm1 ->
match_callstack f1 m1 tm1 cs (Mem.nextblock m1) (Mem.nextblock tm1) ->
match_temps f1 le te ->
exists f2,
match_callstack f2 m2 tm2 (Frame cenv fn e le te sp (Mem.nextblock m1) (Mem.nextblock m2) :: cs)
(Mem.nextblock m2) (Mem.nextblock tm2)
/\ Mem.inject f2 m2 tm2.
Proof.
intros.
eapply match_callstack_alloc_variables_rec; eauto.
eapply Mem.valid_new_block; eauto.
intros. eapply Mem.perm_alloc_3; eauto.
intros. apply Mem.perm_implies with Freeable; auto with mem. eapply Mem.perm_alloc_2; eauto.
instantiate (1 := f1). red; intros. eelim Mem.fresh_block_alloc; eauto.
eapply Mem.valid_block_inject_2; eauto.
intros. apply PTree.gempty.
eapply match_callstack_alloc_right; eauto.
intros. destruct (In_dec peq id (map fst vars)).
apply cenv_remove_gss; auto.
rewrite cenv_remove_gso; auto.
destruct (cenv!id) as [ofs|] eqn:?; auto. elim n; eauto.
eapply Mem.alloc_right_inject; eauto.
Qed.
(** Properties of the compilation environment produced by [build_compilenv] *)
Remark block_alignment_pos:
forall sz, block_alignment sz > 0.
Proof.
unfold block_alignment; intros.
destruct (zlt sz 2). omega.
destruct (zlt sz 4). omega.
destruct (zlt sz 8); omega.
Qed.
Remark assign_variable_incr:
forall id sz cenv stksz cenv' stksz',
assign_variable (cenv, stksz) (id, sz) = (cenv', stksz') -> stksz <= stksz'.
Proof.
simpl; intros. inv H.
generalize (align_le stksz (block_alignment sz) (block_alignment_pos sz)).
assert (0 <= Z.max 0 sz). apply Zmax_bound_l. omega.
omega.
Qed.
Remark assign_variables_incr:
forall vars cenv sz cenv' sz',
assign_variables (cenv, sz) vars = (cenv', sz') -> sz <= sz'.
Proof.
induction vars; intros until sz'.
simpl; intros. inv H. omega.
Opaque assign_variable.
destruct a as [id s]. simpl. intros.
destruct (assign_variable (cenv, sz) (id, s)) as [cenv1 sz1] eqn:?.
apply Z.le_trans with sz1. eapply assign_variable_incr; eauto. eauto.
Transparent assign_variable.
Qed.
Remark inj_offset_aligned_block:
forall stacksize sz,
Mem.inj_offset_aligned (align stacksize (block_alignment sz)) sz.
Proof.
intros; red; intros.
apply Zdivides_trans with (block_alignment sz).
unfold align_chunk. unfold block_alignment.
generalize Z.divide_1_l; intro.
generalize Z.divide_refl; intro.
assert (2 | 4). exists 2; auto.
assert (2 | 8). exists 4; auto.
assert (4 | 8). exists 2; auto.
destruct (zlt sz 2).
destruct chunk; simpl in *; auto; omegaContradiction.
destruct (zlt sz 4).
destruct chunk; simpl in *; auto; omegaContradiction.
destruct (zlt sz 8).
destruct chunk; simpl in *; auto; omegaContradiction.
destruct chunk; simpl; auto.
apply align_divides. apply block_alignment_pos.
Qed.
Remark inj_offset_aligned_block':
forall stacksize sz,
Mem.inj_offset_aligned (align stacksize (block_alignment sz)) (Z.max 0 sz).
Proof.
intros.
replace (block_alignment sz) with (block_alignment (Z.max 0 sz)).
apply inj_offset_aligned_block.
rewrite Zmax_spec. destruct (zlt sz 0); auto.
transitivity 1. reflexivity. unfold block_alignment. rewrite zlt_true. auto. omega.
Qed.
Lemma assign_variable_sound:
forall cenv1 sz1 id sz cenv2 sz2 vars,
assign_variable (cenv1, sz1) (id, sz) = (cenv2, sz2) ->
~In id (map fst vars) ->
0 <= sz1 ->
cenv_compat cenv1 vars sz1 ->
cenv_separated cenv1 vars ->
cenv_compat cenv2 (vars ++ (id, sz) :: nil) sz2
/\ cenv_separated cenv2 (vars ++ (id, sz) :: nil).
Proof.
unfold assign_variable; intros until vars; intros ASV NOREPET POS COMPAT SEP.
inv ASV.
assert (LE: sz1 <= align sz1 (block_alignment sz)). apply align_le. apply block_alignment_pos.
assert (EITHER: forall id' sz',
In (id', sz') (vars ++ (id, sz) :: nil) ->
In (id', sz') vars /\ id' <> id \/ (id', sz') = (id, sz)).
intros. rewrite in_app in H. destruct H.
left; split; auto. red; intros; subst id'. elim NOREPET.
change id with (fst (id, sz')). apply in_map; auto.
simpl in H. destruct H. auto. contradiction.
split; red; intros.
apply EITHER in H. destruct H as [[P Q] | P].
exploit COMPAT; eauto. intros [ofs [A [B [C D]]]].
exists ofs.
split. rewrite PTree.gso; auto.
split. auto. split. auto. zify; omega.
inv P. exists (align sz1 (block_alignment sz)).
split. apply PTree.gss.
split. apply inj_offset_aligned_block.
split. omega.
omega.
apply EITHER in H; apply EITHER in H0.
destruct H as [[P Q] | P]; destruct H0 as [[R S] | R].
rewrite PTree.gso in *; auto. eapply SEP; eauto.
inv R. rewrite PTree.gso in H1; auto. rewrite PTree.gss in H2; inv H2.
exploit COMPAT; eauto. intros [ofs [A [B [C D]]]].
assert (ofs = ofs1) by congruence. subst ofs.
left. zify; omega.
inv P. rewrite PTree.gso in H2; auto. rewrite PTree.gss in H1; inv H1.
exploit COMPAT; eauto. intros [ofs [A [B [C D]]]].
assert (ofs = ofs2) by congruence. subst ofs.
right. zify; omega.
congruence.
Qed.
Lemma assign_variables_sound:
forall vars' cenv1 sz1 cenv2 sz2 vars,