-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathSimplLocalsproof.v
More file actions
2335 lines (2145 loc) · 84.5 KB
/
SimplLocalsproof.v
File metadata and controls
2335 lines (2145 loc) · 84.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. *)
(* *)
(* *********************************************************************)
(** Semantic preservation for the SimplLocals pass. *)
Require Import FSets.
Require Import Coqlib Errors Ordered Maps Integers Floats.
Require Import AST Linking.
Require Import Values Memory Globalenvs Events Smallstep.
Require Import Ctypes Cop Clight SimplLocals.
Module VSF := FSetFacts.Facts(VSet).
Module VSP := FSetProperties.Properties(VSet).
Definition match_prog (p tp: program) : Prop :=
match_program (fun ctx f tf => transf_fundef f = OK tf) eq p tp
/\ prog_types tp = prog_types p.
Lemma match_transf_program:
forall p tp, transf_program p = OK tp -> match_prog p tp.
Proof.
unfold transf_program; intros. monadInv H.
split; auto. apply match_transform_partial_program. rewrite EQ. destruct x; auto.
Qed.
Section PRESERVATION.
Variable prog: program.
Variable tprog: program.
Hypothesis TRANSF: match_prog prog tprog.
Let ge := globalenv prog.
Let tge := globalenv tprog.
Lemma comp_env_preserved:
genv_cenv tge = genv_cenv ge.
Proof.
unfold tge, ge. destruct prog, tprog; simpl. destruct TRANSF as [_ EQ]. simpl in EQ. congruence.
Qed.
Lemma symbols_preserved:
forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
Proof (Genv.find_symbol_match (proj1 TRANSF)).
Lemma senv_preserved:
Senv.equiv ge tge.
Proof (Genv.senv_match (proj1 TRANSF)).
Lemma functions_translated:
forall (v: val) (f: fundef),
Genv.find_funct ge v = Some f ->
exists tf, Genv.find_funct tge v = Some tf /\ transf_fundef f = OK tf.
Proof (Genv.find_funct_transf_partial (proj1 TRANSF)).
Lemma function_ptr_translated:
forall (b: block) (f: fundef),
Genv.find_funct_ptr ge b = Some f ->
exists tf, Genv.find_funct_ptr tge b = Some tf /\ transf_fundef f = OK tf.
Proof (Genv.find_funct_ptr_transf_partial (proj1 TRANSF)).
Lemma type_of_fundef_preserved:
forall fd tfd,
transf_fundef fd = OK tfd -> type_of_fundef tfd = type_of_fundef fd.
Proof.
intros. destruct fd; monadInv H; auto.
monadInv EQ. simpl; unfold type_of_function; simpl. auto.
Qed.
(** Matching between environments before and after *)
Inductive match_var (f: meminj) (cenv: compilenv) (e: env) (m: mem) (te: env) (tle: temp_env) (id: ident) : Prop :=
| match_var_lifted: forall b ty chunk v tv
(ENV: e!id = Some(b, ty))
(TENV: te!id = None)
(LIFTED: VSet.mem id cenv = true)
(MAPPED: f b = None)
(MODE: access_mode ty = By_value chunk)
(LOAD: Mem.load chunk m b 0 = Some v)
(TLENV: tle!(id) = Some tv)
(VINJ: Val.inject f v tv),
match_var f cenv e m te tle id
| match_var_not_lifted: forall b ty b'
(ENV: e!id = Some(b, ty))
(TENV: te!id = Some(b', ty))
(LIFTED: VSet.mem id cenv = false)
(MAPPED: f b = Some(b', 0)),
match_var f cenv e m te tle id
| match_var_not_local: forall
(ENV: e!id = None)
(TENV: te!id = None)
(LIFTED: VSet.mem id cenv = false),
match_var f cenv e m te tle id.
Record match_envs (f: meminj) (cenv: compilenv)
(e: env) (le: temp_env) (m: mem) (lo hi: block)
(te: env) (tle: temp_env) (tlo thi: block) : Prop :=
mk_match_envs {
me_vars:
forall id, match_var f cenv e m te tle id;
me_temps:
forall id v,
le!id = Some v ->
(exists tv, tle!id = Some tv /\ Val.inject f v tv)
/\ (VSet.mem id cenv = true -> v = Vundef);
me_inj:
forall id1 b1 ty1 id2 b2 ty2, e!id1 = Some(b1, ty1) -> e!id2 = Some(b2, ty2) -> id1 <> id2 -> b1 <> b2;
me_range:
forall id b ty, e!id = Some(b, ty) -> Ple lo b /\ Plt b hi;
me_trange:
forall id b ty, te!id = Some(b, ty) -> Ple tlo b /\ Plt b thi;
me_mapped:
forall id b' ty,
te!id = Some(b', ty) -> exists b, f b = Some(b', 0) /\ e!id = Some(b, ty);
me_flat:
forall id b' ty b delta,
te!id = Some(b', ty) -> f b = Some(b', delta) -> e!id = Some(b, ty) /\ delta = 0;
me_incr:
Ple lo hi;
me_tincr:
Ple tlo thi
}.
(** Invariance by change of memory and injection *)
Lemma match_envs_invariant:
forall f cenv e le m lo hi te tle tlo thi f' m',
match_envs f cenv e le m lo hi te tle tlo thi ->
(forall b chunk v,
f b = None -> Ple lo b /\ Plt b hi -> Mem.load chunk m b 0 = Some v -> Mem.load chunk m' b 0 = Some v) ->
inject_incr f f' ->
(forall b, Ple lo b /\ Plt b hi -> f' b = f b) ->
(forall b b' delta, f' b = Some(b', delta) -> Ple tlo b' /\ Plt b' thi -> f' b = f b) ->
match_envs f' cenv e le m' lo hi te tle tlo thi.
Proof.
intros until m'; intros ME LD INCR INV1 INV2.
destruct ME; constructor; eauto.
(* vars *)
intros. generalize (me_vars0 id); intros MV; inv MV.
eapply match_var_lifted; eauto.
rewrite <- MAPPED; eauto.
eapply match_var_not_lifted; eauto.
eapply match_var_not_local; eauto.
(* temps *)
intros. exploit me_temps0; eauto. intros [[v' [A B]] C]. split; auto. exists v'; eauto.
(* mapped *)
intros. exploit me_mapped0; eauto. intros [b [A B]]. exists b; split; auto.
(* flat *)
intros. eapply me_flat0; eauto. rewrite <- H0. symmetry. eapply INV2; eauto.
Qed.
(** Invariance by external call *)
Lemma match_envs_extcall:
forall f cenv e le m lo hi te tle tlo thi tm f' m',
match_envs f cenv e le m lo hi te tle tlo thi ->
Mem.unchanged_on (loc_unmapped f) m m' ->
inject_incr f f' ->
inject_separated f f' m tm ->
Ple hi (Mem.nextblock m) -> Ple thi (Mem.nextblock tm) ->
match_envs f' cenv e le m' lo hi te tle tlo thi.
Proof.
intros. eapply match_envs_invariant; eauto.
intros. eapply Mem.load_unchanged_on; eauto.
red in H2. intros. destruct (f b) as [[b' delta]|] eqn:?.
eapply H1; eauto.
destruct (f' b) as [[b' delta]|] eqn:?; auto.
exploit H2; eauto. unfold Mem.valid_block. intros [A B].
extlia.
intros. destruct (f b) as [[b'' delta']|] eqn:?. eauto.
exploit H2; eauto. unfold Mem.valid_block. intros [A B].
extlia.
Qed.
(** Properties of values resulting from a cast *)
Lemma val_casted_load_result:
forall v ty chunk,
val_casted v ty -> access_mode ty = By_value chunk ->
Val.load_result chunk v = v.
Proof.
intros. inversion H; clear H; subst v ty; simpl in H0.
- destruct sz.
destruct si; inversion H0; clear H0; subst chunk; simpl in *; congruence.
destruct si; inversion H0; clear H0; subst chunk; simpl in *; congruence.
clear H1. inv H0. auto.
inversion H0; clear H0; subst chunk. simpl in *.
destruct (Int.eq n Int.zero); subst n; reflexivity.
- inv H0; auto.
- inv H0; auto.
- inv H0; auto.
- inv H0. unfold Mptr, Val.load_result; destruct Archi.ptr64; auto.
- inv H0. unfold Mptr, Val.load_result; rewrite H1; auto.
- inv H0. unfold Val.load_result; rewrite H1; auto.
- inv H0. unfold Mptr, Val.load_result; rewrite H1; auto.
- inv H0. unfold Val.load_result; rewrite H1; auto.
- discriminate.
- discriminate.
- discriminate.
Qed.
Lemma val_casted_inject:
forall f v v' ty,
Val.inject f v v' -> val_casted v ty -> val_casted v' ty.
Proof.
intros. inv H; auto.
inv H0; constructor; auto.
inv H0; constructor.
Qed.
Lemma forall2_val_casted_inject:
forall f vl vl', Val.inject_list f vl vl' ->
forall tyl, list_forall2 val_casted vl tyl -> list_forall2 val_casted vl' tyl.
Proof.
induction 1; intros tyl F; inv F; constructor; eauto. eapply val_casted_inject; eauto.
Qed.
Inductive val_casted_list: list val -> typelist -> Prop :=
| vcl_nil:
val_casted_list nil Tnil
| vcl_cons: forall v1 vl ty1 tyl,
val_casted v1 ty1 -> val_casted_list vl tyl ->
val_casted_list (v1 :: vl) (Tcons ty1 tyl).
Lemma val_casted_list_params:
forall params vl,
val_casted_list vl (type_of_params params) ->
list_forall2 val_casted vl (map snd params).
Proof.
induction params; simpl; intros.
inv H. constructor.
destruct a as [id ty]. inv H. constructor; auto.
Qed.
(** Correctness of [make_cast] *)
Lemma make_cast_correct:
forall e le m a v1 tto v2,
eval_expr tge e le m a v1 ->
sem_cast v1 (typeof a) tto m = Some v2 ->
eval_expr tge e le m (make_cast a tto) v2.
Proof.
intros.
assert (DFL: eval_expr tge e le m (Ecast a tto) v2).
econstructor; eauto.
unfold sem_cast, make_cast in *.
destruct (classify_cast (typeof a) tto); auto.
destruct v1; destruct Archi.ptr64; inv H0; auto.
destruct sz2; auto. destruct v1; inv H0; auto.
destruct v1; inv H0; auto.
destruct v1; inv H0; auto.
destruct v1; inv H0; auto.
destruct v1; try discriminate.
destruct (ident_eq id1 id2); inv H0; auto.
destruct v1; try discriminate.
destruct (ident_eq id1 id2); inv H0; auto.
inv H0; auto.
Qed.
(** Debug annotations. *)
Lemma cast_typeconv:
forall v ty m,
val_casted v ty ->
sem_cast v ty (typeconv ty) m = Some v.
Proof.
induction 1; simpl.
- unfold sem_cast, classify_cast; destruct sz, Archi.ptr64; auto.
- auto.
- auto.
- unfold sem_cast, classify_cast; destruct Archi.ptr64; auto.
- auto.
- unfold sem_cast; simpl; rewrite H; auto.
- unfold sem_cast; simpl; rewrite H; auto.
- unfold sem_cast; simpl; rewrite H; auto.
- unfold sem_cast; simpl; rewrite H; auto.
- unfold sem_cast; simpl. rewrite dec_eq_true; auto.
- unfold sem_cast. simpl. rewrite dec_eq_true; auto.
- auto.
Qed.
Lemma step_Sdebug_temp:
forall f id ty k e le m v,
le!id = Some v ->
val_casted v ty ->
step2 tge (State f (Sdebug_temp id ty) k e le m)
E0 (State f Sskip k e le m).
Proof.
intros. unfold Sdebug_temp. eapply step_builtin with (optid := None).
econstructor. constructor. eauto. simpl. eapply cast_typeconv; eauto. constructor.
simpl. constructor.
Qed.
Lemma step_Sdebug_var:
forall f id ty k e le m b,
e!id = Some(b, ty) ->
step2 tge (State f (Sdebug_var id ty) k e le m)
E0 (State f Sskip k e le m).
Proof.
intros. unfold Sdebug_var. eapply step_builtin with (optid := None).
econstructor. constructor. constructor. eauto.
simpl. reflexivity. constructor.
simpl. constructor.
Qed.
Lemma step_Sset_debug:
forall f id ty a k e le m v v',
eval_expr tge e le m a v ->
sem_cast v (typeof a) ty m = Some v' ->
plus step2 tge (State f (Sset_debug id ty a) k e le m)
E0 (State f Sskip k e (PTree.set id v' le) m).
Proof.
intros; unfold Sset_debug.
assert (forall k, step2 tge (State f (Sset id (make_cast a ty)) k e le m)
E0 (State f Sskip k e (PTree.set id v' le) m)).
{ intros. apply step_set. eapply make_cast_correct; eauto. }
destruct (Compopts.debug tt).
- eapply plus_left. constructor.
eapply star_left. apply H1.
eapply star_left. constructor.
apply star_one. apply step_Sdebug_temp with (v := v').
apply PTree.gss. eapply cast_val_is_casted; eauto.
reflexivity. reflexivity. reflexivity.
- apply plus_one. apply H1.
Qed.
Lemma step_add_debug_vars:
forall f s e le m vars k,
(forall id ty, In (id, ty) vars -> exists b, e!id = Some (b, ty)) ->
star step2 tge (State f (add_debug_vars vars s) k e le m)
E0 (State f s k e le m).
Proof.
unfold add_debug_vars. destruct (Compopts.debug tt).
- induction vars; simpl; intros.
+ apply star_refl.
+ destruct a as [id ty].
exploit H; eauto. intros (b & TE).
simpl. eapply star_left. constructor.
eapply star_left. eapply step_Sdebug_var; eauto.
eapply star_left. constructor.
apply IHvars; eauto.
reflexivity. reflexivity. reflexivity.
- intros. apply star_refl.
Qed.
Remark bind_parameter_temps_inv:
forall id params args le le',
bind_parameter_temps params args le = Some le' ->
~In id (var_names params) ->
le'!id = le!id.
Proof.
induction params; simpl; intros.
destruct args; inv H. auto.
destruct a as [id1 ty1]. destruct args; try discriminate.
transitivity ((PTree.set id1 v le)!id).
eapply IHparams; eauto. apply PTree.gso. intuition.
Qed.
Lemma step_add_debug_params:
forall f s k e le m params vl le1,
list_norepet (var_names params) ->
list_forall2 val_casted vl (map snd params) ->
bind_parameter_temps params vl le1 = Some le ->
star step2 tge (State f (add_debug_params params s) k e le m)
E0 (State f s k e le m).
Proof.
unfold add_debug_params. destruct (Compopts.debug tt).
- induction params as [ | [id ty] params ]; simpl; intros until le1; intros NR CAST BIND; inv CAST; inv NR.
+ apply star_refl.
+ assert (le!id = Some a1). { erewrite bind_parameter_temps_inv by eauto. apply PTree.gss. }
eapply star_left. constructor.
eapply star_left. eapply step_Sdebug_temp; eauto.
eapply star_left. constructor.
eapply IHparams; eauto.
reflexivity. reflexivity. reflexivity.
- intros; apply star_refl.
Qed.
(** Preservation by assignment to lifted variable. *)
Lemma match_envs_assign_lifted:
forall f cenv e le m lo hi te tle tlo thi b ty v m' id tv,
match_envs f cenv e le m lo hi te tle tlo thi ->
e!id = Some(b, ty) ->
val_casted v ty ->
Val.inject f v tv ->
assign_loc ge ty m b Ptrofs.zero Full v m' ->
VSet.mem id cenv = true ->
match_envs f cenv e le m' lo hi te (PTree.set id tv tle) tlo thi.
Proof.
intros. destruct H. generalize (me_vars0 id); intros MV; inv MV; try congruence.
rewrite ENV in H0; inv H0. inv H3; try congruence.
unfold Mem.storev in H0. rewrite Ptrofs.unsigned_zero in H0.
constructor; eauto; intros.
(* vars *)
destruct (peq id0 id). subst id0.
eapply match_var_lifted with (v := v); eauto.
exploit Mem.load_store_same; eauto. erewrite val_casted_load_result; eauto.
apply PTree.gss.
generalize (me_vars0 id0); intros MV; inv MV.
eapply match_var_lifted; eauto.
rewrite <- LOAD0. eapply Mem.load_store_other; eauto.
rewrite PTree.gso; auto.
eapply match_var_not_lifted; eauto.
eapply match_var_not_local; eauto.
(* temps *)
exploit me_temps0; eauto. intros [[tv1 [A B]] C]. split; auto.
rewrite PTree.gsspec. destruct (peq id0 id).
subst id0. exists tv; split; auto. rewrite C; auto.
exists tv1; auto.
Qed.
(** Preservation by assignment to a temporary *)
Lemma match_envs_set_temp:
forall f cenv e le m lo hi te tle tlo thi id v tv x,
match_envs f cenv e le m lo hi te tle tlo thi ->
Val.inject f v tv ->
check_temp cenv id = OK x ->
match_envs f cenv e (PTree.set id v le) m lo hi te (PTree.set id tv tle) tlo thi.
Proof.
intros. unfold check_temp in H1.
destruct (VSet.mem id cenv) eqn:?; monadInv H1.
destruct H. constructor; eauto; intros.
(* vars *)
generalize (me_vars0 id0); intros MV; inv MV.
eapply match_var_lifted; eauto. rewrite PTree.gso. eauto. congruence.
eapply match_var_not_lifted; eauto.
eapply match_var_not_local; eauto.
(* temps *)
rewrite PTree.gsspec in *. destruct (peq id0 id).
inv H. split. exists tv; auto. intros; congruence.
eapply me_temps0; eauto.
Qed.
Lemma match_envs_set_opttemp:
forall f cenv e le m lo hi te tle tlo thi optid v tv x,
match_envs f cenv e le m lo hi te tle tlo thi ->
Val.inject f v tv ->
check_opttemp cenv optid = OK x ->
match_envs f cenv e (set_opttemp optid v le) m lo hi te (set_opttemp optid tv tle) tlo thi.
Proof.
intros. unfold set_opttemp. destruct optid; simpl in H1.
eapply match_envs_set_temp; eauto.
auto.
Qed.
(** Extensionality with respect to temporaries *)
Lemma match_envs_temps_exten:
forall f cenv e le m lo hi te tle tlo thi tle',
match_envs f cenv e le m lo hi te tle tlo thi ->
(forall id, tle'!id = tle!id) ->
match_envs f cenv e le m lo hi te tle' tlo thi.
Proof.
intros. destruct H. constructor; auto; intros.
(* vars *)
generalize (me_vars0 id); intros MV; inv MV.
eapply match_var_lifted; eauto. rewrite H0; auto.
eapply match_var_not_lifted; eauto.
eapply match_var_not_local; eauto.
(* temps *)
rewrite H0. eauto.
Qed.
(** Invariance by assignment to an irrelevant temporary *)
Lemma match_envs_change_temp:
forall f cenv e le m lo hi te tle tlo thi id v,
match_envs f cenv e le m lo hi te tle tlo thi ->
le!id = None -> VSet.mem id cenv = false ->
match_envs f cenv e le m lo hi te (PTree.set id v tle) tlo thi.
Proof.
intros. destruct H. constructor; auto; intros.
(* vars *)
generalize (me_vars0 id0); intros MV; inv MV.
eapply match_var_lifted; eauto. rewrite PTree.gso; auto. congruence.
eapply match_var_not_lifted; eauto.
eapply match_var_not_local; eauto.
(* temps *)
rewrite PTree.gso. eauto. congruence.
Qed.
(** Properties of [cenv_for]. *)
Definition cenv_for_gen (atk: VSet.t) (vars: list (ident * type)) : compilenv :=
List.fold_right (add_local_variable atk) VSet.empty vars.
Remark add_local_variable_charact:
forall id ty atk cenv id1,
VSet.In id1 (add_local_variable atk (id, ty) cenv) <->
VSet.In id1 cenv \/ exists chunk, access_mode ty = By_value chunk /\ id = id1 /\ VSet.mem id atk = false.
Proof.
intros. unfold add_local_variable. split; intros.
destruct (access_mode ty) eqn:?; auto.
destruct (VSet.mem id atk) eqn:?; auto.
rewrite VSF.add_iff in H. destruct H; auto. right; exists m; auto.
destruct H as [A | [chunk [A [B C]]]].
destruct (access_mode ty); auto. destruct (VSet.mem id atk); auto. rewrite VSF.add_iff; auto.
rewrite A. rewrite <- B. rewrite C. apply VSet.add_1; auto.
Qed.
Lemma cenv_for_gen_domain:
forall atk id vars, VSet.In id (cenv_for_gen atk vars) -> In id (var_names vars).
Proof.
induction vars; simpl; intros.
rewrite VSF.empty_iff in H. auto.
destruct a as [id1 ty1]. rewrite add_local_variable_charact in H.
destruct H as [A | [chunk [A [B C]]]]; auto.
Qed.
Lemma cenv_for_gen_by_value:
forall atk id ty vars,
In (id, ty) vars ->
list_norepet (var_names vars) ->
VSet.In id (cenv_for_gen atk vars) ->
exists chunk, access_mode ty = By_value chunk.
Proof.
induction vars; simpl; intros.
contradiction.
destruct a as [id1 ty1]. simpl in H0. inv H0.
rewrite add_local_variable_charact in H1.
destruct H; destruct H1 as [A | [chunk [A [B C]]]].
inv H. elim H4. eapply cenv_for_gen_domain; eauto.
inv H. exists chunk; auto.
eauto.
subst id1. elim H4. change id with (fst (id, ty)). apply in_map; auto.
Qed.
Lemma cenv_for_gen_compat:
forall atk id vars,
VSet.In id (cenv_for_gen atk vars) -> VSet.mem id atk = false.
Proof.
induction vars; simpl; intros.
rewrite VSF.empty_iff in H. contradiction.
destruct a as [id1 ty1]. rewrite add_local_variable_charact in H.
destruct H as [A | [chunk [A [B C]]]].
auto.
congruence.
Qed.
(** Compatibility between a compilation environment and an address-taken set. *)
Definition compat_cenv (atk: VSet.t) (cenv: compilenv) : Prop :=
forall id, VSet.In id atk -> VSet.In id cenv -> False.
Lemma compat_cenv_for:
forall f, compat_cenv (addr_taken_stmt f.(fn_body)) (cenv_for f).
Proof.
intros; red; intros.
assert (VSet.mem id (addr_taken_stmt (fn_body f)) = false).
eapply cenv_for_gen_compat. eexact H0.
rewrite VSF.mem_iff in H. congruence.
Qed.
Lemma compat_cenv_union_l:
forall atk1 atk2 cenv,
compat_cenv (VSet.union atk1 atk2) cenv -> compat_cenv atk1 cenv.
Proof.
intros; red; intros. eapply H; eauto. apply VSet.union_2; auto.
Qed.
Lemma compat_cenv_union_r:
forall atk1 atk2 cenv,
compat_cenv (VSet.union atk1 atk2) cenv -> compat_cenv atk2 cenv.
Proof.
intros; red; intros. eapply H; eauto. apply VSet.union_3; auto.
Qed.
Lemma compat_cenv_empty:
forall cenv, compat_cenv VSet.empty cenv.
Proof.
intros; red; intros. eapply VSet.empty_1; eauto.
Qed.
Hint Resolve compat_cenv_union_l compat_cenv_union_r compat_cenv_empty: compat.
(** Allocation and initialization of parameters *)
Lemma alloc_variables_nextblock:
forall ge e m vars e' m',
alloc_variables ge e m vars e' m' -> Ple (Mem.nextblock m) (Mem.nextblock m').
Proof.
induction 1.
apply Ple_refl.
eapply Ple_trans; eauto. exploit Mem.nextblock_alloc; eauto. intros EQ; rewrite EQ. apply Ple_succ.
Qed.
Lemma alloc_variables_range:
forall ge id b ty e m vars e' m',
alloc_variables ge e m vars e' m' ->
e'!id = Some(b, ty) -> e!id = Some(b, ty) \/ Ple (Mem.nextblock m) b /\ Plt b (Mem.nextblock m').
Proof.
induction 1; intros.
auto.
exploit IHalloc_variables; eauto. rewrite PTree.gsspec. intros [A|A].
destruct (peq id id0). inv A.
right. exploit Mem.alloc_result; eauto. exploit Mem.nextblock_alloc; eauto.
generalize (alloc_variables_nextblock _ _ _ _ _ _ H0). intros A B C.
subst b. split. apply Ple_refl. eapply Pos.lt_le_trans; eauto. rewrite B. apply Plt_succ.
auto.
right. exploit Mem.nextblock_alloc; eauto. intros B. rewrite B in A. extlia.
Qed.
Lemma alloc_variables_injective:
forall ge id1 b1 ty1 id2 b2 ty2 e m vars e' m',
alloc_variables ge e m vars e' m' ->
(e!id1 = Some(b1, ty1) -> e!id2 = Some(b2, ty2) -> id1 <> id2 -> b1 <> b2) ->
(forall id b ty, e!id = Some(b, ty) -> Plt b (Mem.nextblock m)) ->
(e'!id1 = Some(b1, ty1) -> e'!id2 = Some(b2, ty2) -> id1 <> id2 -> b1 <> b2).
Proof.
induction 1; intros.
eauto.
eapply IHalloc_variables; eauto.
repeat rewrite PTree.gsspec; intros.
destruct (peq id1 id); destruct (peq id2 id).
congruence.
inv H6. exploit Mem.alloc_result; eauto. exploit H2; eauto. unfold block; extlia.
inv H7. exploit Mem.alloc_result; eauto. exploit H2; eauto. unfold block; extlia.
eauto.
intros. rewrite PTree.gsspec in H6. destruct (peq id0 id). inv H6.
exploit Mem.alloc_result; eauto. exploit Mem.nextblock_alloc; eauto. unfold block; extlia.
exploit H2; eauto. exploit Mem.nextblock_alloc; eauto. unfold block; extlia.
Qed.
Lemma match_alloc_variables:
forall cenv e m vars e' m',
alloc_variables ge e m vars e' m' ->
forall j tm te,
list_norepet (var_names vars) ->
Mem.inject j m tm ->
exists j', exists te', exists tm',
alloc_variables tge te tm (remove_lifted cenv vars) te' tm'
/\ Mem.inject j' m' tm'
/\ inject_incr j j'
/\ (forall b, Mem.valid_block m b -> j' b = j b)
/\ (forall b b' delta, j' b = Some(b', delta) -> Mem.valid_block tm b' -> j' b = j b)
/\ (forall b b' delta, j' b = Some(b', delta) -> ~Mem.valid_block tm b' ->
exists id, exists ty, e'!id = Some(b, ty) /\ te'!id = Some(b', ty) /\ delta = 0)
/\ (forall id ty, In (id, ty) vars ->
exists b,
e'!id = Some(b, ty)
/\ if VSet.mem id cenv
then te'!id = te!id /\ j' b = None
else exists tb, te'!id = Some(tb, ty) /\ j' b = Some(tb, 0))
/\ (forall id, ~In id (var_names vars) -> e'!id = e!id /\ te'!id = te!id).
Proof.
induction 1; intros.
(* base case *)
exists j; exists te; exists tm. simpl.
split. constructor.
split. auto. split. auto. split. auto. split. auto.
split. intros. elim H2. eapply Mem.mi_mappedblocks; eauto.
split. tauto. auto.
(* inductive case *)
simpl in H1. inv H1. simpl.
destruct (VSet.mem id cenv) eqn:?. simpl.
(* variable is lifted out of memory *)
exploit Mem.alloc_left_unmapped_inject; eauto.
intros [j1 [A [B [C D]]]].
exploit IHalloc_variables; eauto. instantiate (1 := te).
intros [j' [te' [tm' [J [K [L [M [N [Q [O P]]]]]]]]]].
exists j'; exists te'; exists tm'.
split. auto.
split. auto.
split. eapply inject_incr_trans; eauto.
split. intros. transitivity (j1 b). apply M. eapply Mem.valid_block_alloc; eauto.
apply D. apply Mem.valid_not_valid_diff with m; auto. eapply Mem.fresh_block_alloc; eauto.
split. intros. transitivity (j1 b). eapply N; eauto.
destruct (eq_block b b1); auto. subst.
assert (j' b1 = j1 b1). apply M. eapply Mem.valid_new_block; eauto.
congruence.
split. exact Q.
split. intros. destruct (ident_eq id0 id).
(* same var *)
subst id0.
assert (ty0 = ty).
destruct H1. congruence. elim H5. unfold var_names. change id with (fst (id, ty0)). apply in_map; auto.
subst ty0.
exploit P; eauto. intros [X Y]. rewrite Heqb. rewrite X. rewrite Y.
exists b1. split. apply PTree.gss.
split. auto.
rewrite M. auto. eapply Mem.valid_new_block; eauto.
(* other vars *)
eapply O; eauto. destruct H1. congruence. auto.
intros. exploit (P id0). tauto. intros [X Y]. rewrite X; rewrite Y.
split; auto. apply PTree.gso. intuition.
(* variable is not lifted out of memory *)
exploit Mem.alloc_parallel_inject.
eauto. eauto. apply Z.le_refl. apply Z.le_refl.
intros [j1 [tm1 [tb1 [A [B [C [D E]]]]]]].
exploit IHalloc_variables; eauto. instantiate (1 := PTree.set id (tb1, ty) te).
intros [j' [te' [tm' [J [K [L [M [N [Q [O P]]]]]]]]]].
exists j'; exists te'; exists tm'.
split. simpl. econstructor; eauto. rewrite comp_env_preserved; auto.
split. auto.
split. eapply inject_incr_trans; eauto.
split. intros. transitivity (j1 b). apply M. eapply Mem.valid_block_alloc; eauto.
apply E. apply Mem.valid_not_valid_diff with m; auto. eapply Mem.fresh_block_alloc; eauto.
split. intros. transitivity (j1 b). eapply N; eauto. eapply Mem.valid_block_alloc; eauto.
destruct (eq_block b b1); auto. subst.
assert (j' b1 = j1 b1). apply M. eapply Mem.valid_new_block; eauto.
rewrite H4 in H1. rewrite D in H1. inv H1. eelim Mem.fresh_block_alloc; eauto.
split. intros. destruct (eq_block b' tb1).
subst b'. rewrite (N _ _ _ H1) in H1.
destruct (eq_block b b1). subst b. rewrite D in H1; inv H1.
exploit (P id); auto. intros [X Y]. exists id; exists ty.
rewrite X; rewrite Y. repeat rewrite PTree.gss. auto.
rewrite E in H1; auto. elim H3. eapply Mem.mi_mappedblocks; eauto.
eapply Mem.valid_new_block; eauto.
eapply Q; eauto. unfold Mem.valid_block in *.
exploit Mem.nextblock_alloc. eexact A. exploit Mem.alloc_result. eexact A.
unfold block; extlia.
split. intros. destruct (ident_eq id0 id).
(* same var *)
subst id0.
assert (ty0 = ty).
destruct H1. congruence. elim H5. unfold var_names. change id with (fst (id, ty0)). apply in_map; auto.
subst ty0.
exploit P; eauto. intros [X Y]. rewrite Heqb. rewrite X. rewrite Y.
exists b1. split. apply PTree.gss.
exists tb1; split.
apply PTree.gss.
rewrite M. auto. eapply Mem.valid_new_block; eauto.
(* other vars *)
exploit (O id0 ty0). destruct H1. congruence. auto.
rewrite PTree.gso; auto.
intros. exploit (P id0). tauto. intros [X Y]. rewrite X; rewrite Y.
split; apply PTree.gso; intuition.
Qed.
Lemma alloc_variables_load:
forall e m vars e' m',
alloc_variables ge e m vars e' m' ->
forall chunk b ofs v,
Mem.load chunk m b ofs = Some v ->
Mem.load chunk m' b ofs = Some v.
Proof.
induction 1; intros.
auto.
apply IHalloc_variables. eapply Mem.load_alloc_other; eauto.
Qed.
Lemma sizeof_by_value:
forall ty chunk,
access_mode ty = By_value chunk -> size_chunk chunk <= sizeof ge ty.
Proof.
unfold access_mode; intros.
assert (size_chunk chunk = sizeof ge ty).
{
destruct ty; try destruct i; try destruct s; try destruct f; inv H; auto;
unfold Mptr; simpl; destruct Archi.ptr64; auto.
}
lia.
Qed.
Definition env_initial_value (e: env) (m: mem) :=
forall id b ty chunk,
e!id = Some(b, ty) -> access_mode ty = By_value chunk -> Mem.load chunk m b 0 = Some Vundef.
Lemma alloc_variables_initial_value:
forall e m vars e' m',
alloc_variables ge e m vars e' m' ->
env_initial_value e m ->
env_initial_value e' m'.
Proof.
induction 1; intros.
auto.
apply IHalloc_variables. red; intros. rewrite PTree.gsspec in H2.
destruct (peq id0 id). inv H2.
eapply Mem.load_alloc_same'; eauto.
lia. rewrite Z.add_0_l. eapply sizeof_by_value; eauto.
apply Z.divide_0_r.
eapply Mem.load_alloc_other; eauto.
Qed.
Lemma create_undef_temps_charact:
forall id ty vars, In (id, ty) vars -> (create_undef_temps vars)!id = Some Vundef.
Proof.
induction vars; simpl; intros.
contradiction.
destruct H. subst a. apply PTree.gss.
destruct a as [id1 ty1]. rewrite PTree.gsspec. destruct (peq id id1); auto.
Qed.
Lemma create_undef_temps_inv:
forall vars id v, (create_undef_temps vars)!id = Some v -> v = Vundef /\ In id (var_names vars).
Proof.
induction vars; simpl; intros.
rewrite PTree.gempty in H; congruence.
destruct a as [id1 ty1]. rewrite PTree.gsspec in H. destruct (peq id id1).
inv H. auto.
exploit IHvars; eauto. tauto.
Qed.
Lemma create_undef_temps_exten:
forall id l1 l2,
(In id (var_names l1) <-> In id (var_names l2)) ->
(create_undef_temps l1)!id = (create_undef_temps l2)!id.
Proof.
assert (forall id l1 l2,
(In id (var_names l1) -> In id (var_names l2)) ->
(create_undef_temps l1)!id = None \/ (create_undef_temps l1)!id = (create_undef_temps l2)!id).
intros. destruct ((create_undef_temps l1)!id) as [v1|] eqn:?; auto.
exploit create_undef_temps_inv; eauto. intros [A B]. subst v1.
exploit list_in_map_inv. unfold var_names in H. apply H. eexact B.
intros [[id1 ty1] [P Q]]. simpl in P; subst id1.
right; symmetry; eapply create_undef_temps_charact; eauto.
intros.
exploit (H id l1 l2). tauto.
exploit (H id l2 l1). tauto.
intuition congruence.
Qed.
Remark var_names_app:
forall vars1 vars2, var_names (vars1 ++ vars2) = var_names vars1 ++ var_names vars2.
Proof.
intros. apply map_app.
Qed.
Remark filter_app:
forall (A: Type) (f: A -> bool) l1 l2,
List.filter f (l1 ++ l2) = List.filter f l1 ++ List.filter f l2.
Proof.
induction l1; simpl; intros.
auto.
destruct (f a). simpl. decEq; auto. auto.
Qed.
Remark filter_charact:
forall (A: Type) (f: A -> bool) x l,
In x (List.filter f l) <-> In x l /\ f x = true.
Proof.
induction l; simpl. tauto.
destruct (f a) eqn:?.
simpl. rewrite IHl. intuition congruence.
intuition congruence.
Qed.
Remark filter_norepet:
forall (A: Type) (f: A -> bool) l,
list_norepet l -> list_norepet (List.filter f l).
Proof.
induction 1; simpl. constructor.
destruct (f hd); auto. constructor; auto. rewrite filter_charact. tauto.
Qed.
Remark filter_map:
forall (A B: Type) (f: A -> B) (pa: A -> bool) (pb: B -> bool),
(forall a, pb (f a) = pa a) ->
forall l, List.map f (List.filter pa l) = List.filter pb (List.map f l).
Proof.
induction l; simpl.
auto.
rewrite H. destruct (pa a); simpl; congruence.
Qed.
Lemma create_undef_temps_lifted:
forall id f,
~ In id (var_names (fn_params f)) ->
(create_undef_temps (add_lifted (cenv_for f) (fn_vars f) (fn_temps f))) ! id =
(create_undef_temps (add_lifted (cenv_for f) (fn_params f ++ fn_vars f) (fn_temps f))) ! id.
Proof.
intros. apply create_undef_temps_exten.
unfold add_lifted. rewrite filter_app.
unfold var_names in *.
repeat rewrite map_app. repeat rewrite in_app. intuition.
exploit list_in_map_inv; eauto. intros [[id1 ty1] [P Q]]. simpl in P. subst id.
rewrite filter_charact in Q. destruct Q.
elim H. change id1 with (fst (id1, ty1)). apply List.in_map. auto.
Qed.
Lemma vars_and_temps_properties:
forall cenv params vars temps,
list_norepet (var_names params ++ var_names vars) ->
list_disjoint (var_names params) (var_names temps) ->
list_norepet (var_names params)
/\ list_norepet (var_names (remove_lifted cenv (params ++ vars)))
/\ list_disjoint (var_names params) (var_names (add_lifted cenv vars temps)).
Proof.
intros. rewrite list_norepet_app in H. destruct H as [A [B C]].
split. auto.
split. unfold remove_lifted. unfold var_names. erewrite filter_map.
instantiate (1 := fun a => negb (VSet.mem a cenv)). 2: auto.
apply filter_norepet. rewrite map_app. apply list_norepet_append; assumption.
unfold add_lifted. rewrite var_names_app.
unfold var_names at 2. erewrite filter_map.
instantiate (1 := fun a => VSet.mem a cenv). 2: auto.
change (map fst vars) with (var_names vars).
red; intros.
rewrite in_app in H1. destruct H1.
rewrite filter_charact in H1. destruct H1. apply C; auto.
apply H0; auto.
Qed.
Theorem match_envs_alloc_variables:
forall cenv m vars e m' temps j tm,
alloc_variables ge empty_env m vars e m' ->
list_norepet (var_names vars) ->
Mem.inject j m tm ->
(forall id ty, In (id, ty) vars -> VSet.mem id cenv = true ->
exists chunk, access_mode ty = By_value chunk) ->
(forall id, VSet.mem id cenv = true -> In id (var_names vars)) ->
exists j', exists te, exists tm',
alloc_variables tge empty_env tm (remove_lifted cenv vars) te tm'
/\ match_envs j' cenv e (create_undef_temps temps) m' (Mem.nextblock m) (Mem.nextblock m')
te (create_undef_temps (add_lifted cenv vars temps)) (Mem.nextblock tm) (Mem.nextblock tm')
/\ Mem.inject j' m' tm'
/\ inject_incr j j'
/\ (forall b, Mem.valid_block m b -> j' b = j b)
/\ (forall b b' delta, j' b = Some(b', delta) -> Mem.valid_block tm b' -> j' b = j b)
/\ (forall id ty, In (id, ty) vars -> VSet.mem id cenv = false -> exists b, te!id = Some(b, ty)).
Proof.
intros.
exploit (match_alloc_variables cenv); eauto. instantiate (1 := empty_env).
intros [j' [te [tm' [A [B [C [D [E [K [F G]]]]]]]]]].
exists j'; exists te; exists tm'.
split. auto. split; auto.
constructor; intros.
(* vars *)
destruct (In_dec ident_eq id (var_names vars)).
unfold var_names in i. exploit list_in_map_inv; eauto.
intros [[id' ty] [EQ IN]]; simpl in EQ; subst id'.
exploit F; eauto. intros [b [P R]].
destruct (VSet.mem id cenv) eqn:?.
(* local var, lifted *)
destruct R as [U V]. exploit H2; eauto. intros [chunk X].
eapply match_var_lifted with (v := Vundef) (tv := Vundef); eauto.
rewrite U; apply PTree.gempty.
eapply alloc_variables_initial_value; eauto.
red. unfold empty_env; intros. rewrite PTree.gempty in H4; congruence.
apply create_undef_temps_charact with ty.
unfold add_lifted. apply in_or_app. left.
rewrite filter_In. auto.
(* local var, not lifted *)
destruct R as [tb [U V]].
eapply match_var_not_lifted; eauto.
(* non-local var *)
exploit G; eauto. unfold empty_env. rewrite PTree.gempty. intros [U V].
eapply match_var_not_local; eauto.
destruct (VSet.mem id cenv) eqn:?; auto.
elim n; eauto.
(* temps *)
exploit create_undef_temps_inv; eauto. intros [P Q]. subst v.
unfold var_names in Q. exploit list_in_map_inv; eauto.
intros [[id1 ty] [EQ IN]]; simpl in EQ; subst id1.
split; auto. exists Vundef; split; auto.
apply create_undef_temps_charact with ty. unfold add_lifted.
apply in_or_app; auto.
(* injective *)
eapply alloc_variables_injective. eexact H.
rewrite PTree.gempty. congruence.
intros. rewrite PTree.gempty in H7. congruence.
eauto. eauto. auto.
(* range *)
exploit alloc_variables_range. eexact H. eauto.
rewrite PTree.gempty. intuition congruence.
(* trange *)
exploit alloc_variables_range. eexact A. eauto.
rewrite PTree.gempty. intuition congruence.
(* mapped *)
destruct (In_dec ident_eq id (var_names vars)).
unfold var_names in i. exploit list_in_map_inv; eauto.
intros [[id' ty'] [EQ IN]]; simpl in EQ; subst id'.
exploit F; eauto. intros [b [P Q]].
destruct (VSet.mem id cenv).
rewrite PTree.gempty in Q. destruct Q; congruence.
destruct Q as [tb [U V]]. exists b; split; congruence.
exploit G; eauto. rewrite PTree.gempty. intuition congruence.
(* flat *)
exploit alloc_variables_range. eexact A. eauto.
rewrite PTree.gempty. intros [P|P]. congruence.
exploit K; eauto. unfold Mem.valid_block. extlia.
intros [id0 [ty0 [U [V W]]]]. split; auto.
destruct (ident_eq id id0). congruence.
assert (b' <> b').
eapply alloc_variables_injective with (e' := te) (id1 := id) (id2 := id0); eauto.
rewrite PTree.gempty; congruence.
intros until ty1; rewrite PTree.gempty; congruence.
congruence.
(* incr *)
eapply alloc_variables_nextblock; eauto.
eapply alloc_variables_nextblock; eauto.