summaryrefslogtreecommitdiff
path: root/common/Globalenvs.v
blob: 65ae06c1a57f56adf2522d45858c687e2f514243 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
(* *********************************************************************)
(*                                                                     *)
(*              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 GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 2 of the License, or  *)
(*  (at your option) any later version.  This file is also distributed *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Global environments are a component of the dynamic semantics of 
  all languages involved in the compiler.  A global environment
  maps symbol names (names of functions and of global variables)
  to the corresponding memory addresses.  It also maps memory addresses
  of functions to the corresponding function descriptions.  

  Global environments, along with the initial memory state at the beginning
  of program execution, are built from the program of interest, as follows:
- A distinct memory address is assigned to each function of the program.
  These function addresses use negative numbers to distinguish them from
  addresses of memory blocks.  The associations of function name to function
  address and function address to function description are recorded in
  the global environment.
- For each global variable, a memory block is allocated and associated to
  the name of the variable.

  These operations reflect (at a high level of abstraction) what takes
  place during program linking and program loading in a real operating
  system. *)

Require Import Coqlib.
Require Import Errors.
Require Import Maps.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import Memory.

Notation "s #1" := (fst s) (at level 9, format "s '#1'") : pair_scope.
Notation "s #2" := (snd s) (at level 9, format "s '#2'") : pair_scope.

Local Open Scope pair_scope.
Local Open Scope error_monad_scope.

Set Implicit Arguments.

Module Genv.

(** * Global environments *)

Section GENV.

Variable F: Type.  (**r The type of function descriptions *)
Variable V: Type.  (**r The type of information attached to variables *)

(** The type of global environments. *)

Record t: Type := mkgenv {
  genv_symb: PTree.t block;             (**r mapping symbol -> block *)
  genv_funs: ZMap.t (option F);         (**r mapping function pointer -> definition *)
  genv_vars: ZMap.t (option V);         (**r mapping variable pointer -> info *)
  genv_nextfun: block;                  (**r next function pointer *)
  genv_nextvar: block;                  (**r next variable pointer *)
  genv_nextfun_neg: genv_nextfun < 0;
  genv_nextvar_pos: genv_nextvar > 0;
  genv_symb_range: forall id b, PTree.get id genv_symb = Some b -> b <> 0 /\ genv_nextfun < b /\ b < genv_nextvar;
  genv_funs_range: forall b f, ZMap.get b genv_funs = Some f -> genv_nextfun < b < 0;
  genv_vars_range: forall b v, ZMap.get b genv_vars = Some v -> 0 < b < genv_nextvar;
  genv_vars_inj: forall id1 id2 b, 
    PTree.get id1 genv_symb = Some b -> PTree.get id2 genv_symb = Some b -> id1 = id2
}.

(** ** Lookup functions *)

(** [find_symbol ge id] returns the block associated with the given name, if any *)

Definition find_symbol (ge: t) (id: ident) : option block :=
  PTree.get id ge.(genv_symb).

(** [find_funct_ptr ge b] returns the function description associated with
    the given address. *)

Definition find_funct_ptr (ge: t) (b: block) : option F :=
  ZMap.get b ge.(genv_funs).

(** [find_funct] is similar to [find_funct_ptr], but the function address
    is given as a value, which must be a pointer with offset 0. *)

Definition find_funct (ge: t) (v: val) : option F :=
  match v with
  | Vptr b ofs => if Int.eq_dec ofs Int.zero then find_funct_ptr ge b else None
  | _ => None
  end.

(** [find_var_info ge b] returns the information attached to the variable
   at address [b]. *)

Definition find_var_info (ge: t) (b: block) : option V :=
  ZMap.get b ge.(genv_vars).

(** ** Constructing the global environment *)

Program Definition add_function (ge: t) (idf: ident * F) : t :=
  @mkgenv
    (PTree.set idf#1 ge.(genv_nextfun) ge.(genv_symb))
    (ZMap.set ge.(genv_nextfun) (Some idf#2) ge.(genv_funs))
    ge.(genv_vars)
    (ge.(genv_nextfun) - 1)
    ge.(genv_nextvar)
    _ _ _ _ _ _.
Next Obligation.
  destruct ge; simpl; omega.
Qed.
Next Obligation.
  destruct ge; auto.
Qed.
Next Obligation.
  destruct ge; simpl in *.
  rewrite PTree.gsspec in H. destruct (peq id i). inv H. unfold block; omega.
  exploit genv_symb_range0; eauto. unfold block; omega.
Qed.
Next Obligation.
  destruct ge; simpl in *. rewrite ZMap.gsspec in H. 
  destruct (ZIndexed.eq b genv_nextfun0). subst; omega. 
  exploit genv_funs_range0; eauto. omega.
Qed.
Next Obligation.
  destruct ge; eauto. 
Qed.
Next Obligation.
  destruct ge; simpl in *. 
  rewrite PTree.gsspec in H. rewrite PTree.gsspec in H0. 
  destruct (peq id1 i); destruct (peq id2 i).
  congruence.
  exploit genv_symb_range0; eauto. intros [A B]. inv H. omegaContradiction.
  exploit genv_symb_range0; eauto. intros [A B]. inv H0. omegaContradiction.
  eauto.
Qed.

Program Definition add_variable (ge: t) (idv: ident * list init_data * V) : t :=
  @mkgenv
    (PTree.set idv#1#1 ge.(genv_nextvar) ge.(genv_symb))
    ge.(genv_funs)
    (ZMap.set ge.(genv_nextvar) (Some idv#2) ge.(genv_vars))
    ge.(genv_nextfun)
    (ge.(genv_nextvar) + 1)
    _ _ _ _ _ _.
Next Obligation.
  destruct ge; auto.
Qed.
Next Obligation.
  destruct ge; simpl; omega.
Qed.
Next Obligation.
  destruct ge; simpl in *.
  rewrite PTree.gsspec in H. destruct (peq id i). inv H. unfold block; omega.
  exploit genv_symb_range0; eauto. unfold block; omega.
Qed.
Next Obligation.
  destruct ge; eauto. 
Qed.
Next Obligation.
  destruct ge; simpl in *. rewrite ZMap.gsspec in H. 
  destruct (ZIndexed.eq b genv_nextvar0). subst; omega. 
  exploit genv_vars_range0; eauto. omega.
Qed.
Next Obligation.
  destruct ge; simpl in *. 
  rewrite PTree.gsspec in H. rewrite PTree.gsspec in H0. 
  destruct (peq id1 i); destruct (peq id2 i).
  congruence.
  exploit genv_symb_range0; eauto. intros [A B]. inv H. omegaContradiction.
  exploit genv_symb_range0; eauto. intros [A B]. inv H0. omegaContradiction.
  eauto.
Qed.

Program Definition empty_genv : t :=
  @mkgenv (PTree.empty block) (ZMap.init None) (ZMap.init None) (-1) 1 _ _ _ _ _ _.
Next Obligation.
  omega.
Qed.
Next Obligation.
  omega.
Qed.
Next Obligation.
  rewrite PTree.gempty in H. discriminate.
Qed.
Next Obligation.
  rewrite ZMap.gi in H. discriminate.
Qed.
Next Obligation.
  rewrite ZMap.gi in H. discriminate.
Qed.
Next Obligation.
  rewrite PTree.gempty in H. discriminate.
Qed.

Definition add_functions (ge: t) (fl: list (ident * F)) : t :=
  List.fold_left add_function fl ge.

Definition add_variables (ge: t) (vl: list (ident * list init_data * V)) : t :=
  List.fold_left add_variable vl ge.

Definition globalenv (p: program F V) :=
  add_variables (add_functions empty_genv p.(prog_funct)) p.(prog_vars).

(** ** Properties of the operations over global environments *)

Theorem find_funct_inv:
  forall ge v f,
  find_funct ge v = Some f -> exists b, v = Vptr b Int.zero.
Proof.
  intros until f; unfold find_funct. 
  destruct v; try congruence. 
  destruct (Int.eq_dec i Int.zero); try congruence.
  intros. exists b; congruence.
Qed.

Theorem find_funct_find_funct_ptr:
  forall ge b,
  find_funct ge (Vptr b Int.zero) = find_funct_ptr ge b.
Proof.
  intros; simpl. apply dec_eq_true.
Qed.

Theorem find_symbol_exists:
  forall p id init v,
  In (id, init, v) (prog_vars p) ->
  exists b, find_symbol (globalenv p) id = Some b.
Proof.
  intros until v.
  assert (forall vl ge,
          (exists b, find_symbol ge id = Some b) ->
          exists b, find_symbol (add_variables ge vl) id = Some b).
  unfold find_symbol; induction vl; simpl; intros. auto. apply IHvl.
  simpl. rewrite PTree.gsspec. fold ident. destruct (peq id a#1#1).
  exists (genv_nextvar ge); auto. auto.

  assert (forall vl ge, In (id, init, v) vl ->
          exists b, find_symbol (add_variables ge vl) id = Some b).
  unfold find_symbol; induction vl; simpl; intros. contradiction.
  destruct H0. apply H. subst; unfold find_symbol; simpl.
  rewrite PTree.gss. exists (genv_nextvar ge); auto.
  eauto.

  intros. unfold globalenv; eauto. 
Qed.

Remark add_functions_same_symb:
  forall id fl ge, 
  ~In id (map (@fst ident F) fl) ->
  find_symbol (add_functions ge fl) id = find_symbol ge id.
Proof.
  induction fl; simpl; intros. auto. 
  rewrite IHfl. unfold find_symbol; simpl. apply PTree.gso. intuition. intuition.
Qed.

Remark add_functions_same_address:
  forall b fl ge,
  b > ge.(genv_nextfun) ->
  find_funct_ptr (add_functions ge fl) b = find_funct_ptr ge b.
Proof.
  induction fl; simpl; intros. auto. 
  rewrite IHfl. unfold find_funct_ptr; simpl. apply ZMap.gso. 
  red; intros; subst b; omegaContradiction.
  simpl. omega. 
Qed.

Remark add_variables_same_symb:
  forall id vl ge, 
  ~In id (map (fun idv => idv#1#1) vl) ->
  find_symbol (add_variables ge vl) id = find_symbol ge id.
Proof.
  induction vl; simpl; intros. auto. 
  rewrite IHvl. unfold find_symbol; simpl. apply PTree.gso. intuition. intuition.
Qed.

Remark add_variables_same_address:
  forall b vl ge,
  b < ge.(genv_nextvar) ->
  find_var_info (add_variables ge vl) b = find_var_info ge b.
Proof.
  induction vl; simpl; intros. auto. 
  rewrite IHvl. unfold find_var_info; simpl. apply ZMap.gso. 
  red; intros; subst b; omegaContradiction.
  simpl. omega. 
Qed.

Remark add_variables_same_funs:
  forall b vl ge, find_funct_ptr (add_variables ge vl) b = find_funct_ptr ge b.
Proof.
  induction vl; simpl; intros. auto. rewrite IHvl. auto.
Qed.

Remark add_functions_nextvar:
  forall fl ge, genv_nextvar (add_functions ge fl) = genv_nextvar ge.
Proof.
  induction fl; simpl; intros. auto. rewrite IHfl. auto. 
Qed.

Remark add_variables_nextvar:
  forall vl ge, genv_nextvar (add_variables ge vl) = genv_nextvar ge + Z_of_nat(List.length vl).
Proof.
  induction vl; intros.
  simpl. unfold block; omega.
  simpl length; rewrite inj_S; simpl. rewrite IHvl. simpl. unfold block; omega.
Qed.

Theorem find_funct_ptr_exists:
  forall p id f,
  list_norepet (prog_funct_names p) ->
  list_disjoint (prog_funct_names p) (prog_var_names p) ->
  In (id, f) (prog_funct p) ->
  exists b, find_symbol (globalenv p) id = Some b
         /\ find_funct_ptr (globalenv p) b = Some f.
Proof.
  intros until f.

  assert (forall fl ge, In (id, f) fl -> list_norepet (map (@fst ident F) fl) ->
          exists b, find_symbol (add_functions ge fl) id = Some b
                 /\ find_funct_ptr (add_functions ge fl) b = Some f).
  induction fl; simpl; intros. contradiction. inv H0. 
  destruct H. subst a. exists (genv_nextfun ge); split.
  rewrite add_functions_same_symb; auto. unfold find_symbol; simpl. apply PTree.gss.
  rewrite add_functions_same_address. unfold find_funct_ptr; simpl. apply ZMap.gss. 
  simpl; omega.
  apply IHfl; auto. 

  intros. exploit (H p.(prog_funct) empty_genv); eauto. intros [b [A B]].
  unfold globalenv; exists b; split.
  rewrite add_variables_same_symb. auto. eapply list_disjoint_notin; eauto. 
  unfold prog_funct_names. change id with (fst (id, f)). apply in_map; auto. 
  rewrite add_variables_same_funs. auto.
Qed.

Theorem find_funct_ptr_prop:
  forall (P: F -> Prop) p b f,
  (forall id f, In (id, f) (prog_funct p) -> P f) ->
  find_funct_ptr (globalenv p) b = Some f ->
  P f.
Proof.
  intros until f. intros PROP.
  assert (forall fl ge,
          List.incl fl (prog_funct p) ->
          match find_funct_ptr ge b with None => True | Some f => P f end ->
          match find_funct_ptr (add_functions ge fl) b with None => True | Some f => P f end).
  induction fl; simpl; intros. auto.
  apply IHfl. eauto with coqlib. unfold find_funct_ptr; simpl.
  destruct a as [id' f']; simpl. 
  rewrite ZMap.gsspec. destruct (ZIndexed.eq b (genv_nextfun ge)). 
  apply PROP with id'. apply H. auto with coqlib. 
  assumption.

  unfold globalenv. rewrite add_variables_same_funs. intro. 
  exploit (H p.(prog_funct) empty_genv). auto with coqlib. 
  unfold find_funct_ptr; simpl. rewrite ZMap.gi. auto.
  rewrite H0. auto.
Qed.

Theorem find_funct_prop:
  forall (P: F -> Prop) p v f,
  (forall id f, In (id, f) (prog_funct p) -> P f) ->
  find_funct (globalenv p) v = Some f ->
  P f.
Proof.
  intros. exploit find_funct_inv; eauto. intros [b EQ]. subst v. 
  rewrite find_funct_find_funct_ptr in H0. 
  eapply find_funct_ptr_prop; eauto.
Qed.

Theorem find_funct_ptr_inversion:
  forall p b f,
  find_funct_ptr (globalenv p) b = Some f ->
  exists id, In (id, f) (prog_funct p).
Proof.
  intros. pattern f. apply find_funct_ptr_prop with p b; auto.
  intros. exists id; auto.
Qed.

Theorem find_funct_inversion:
  forall p v f,
  find_funct (globalenv p) v = Some f ->
  exists id, In (id, f) (prog_funct p).
Proof.
  intros. pattern f. apply find_funct_prop with p v; auto.
  intros. exists id; auto.
Qed.

Theorem find_funct_ptr_negative:
  forall p b f,
  find_funct_ptr (globalenv p) b = Some f -> b < 0.
Proof.
  unfold find_funct_ptr. intros. destruct (globalenv p). simpl in H. 
  exploit genv_funs_range0; eauto. omega. 
Qed.

Theorem find_var_info_positive:
  forall p b v,
  find_var_info (globalenv p) b = Some v -> b > 0.
Proof.
  unfold find_var_info. intros. destruct (globalenv p). simpl in H. 
  exploit genv_vars_range0; eauto. omega. 
Qed.

Remark add_variables_symb_neg:
  forall id b vl ge,
  find_symbol (add_variables ge vl) id = Some b -> b < 0 ->
  find_symbol ge id = Some b.
Proof.
  induction vl; simpl; intros. auto.
  exploit IHvl; eauto. unfold find_symbol; simpl. rewrite PTree.gsspec. 
  fold ident. destruct (peq id (a#1#1)); auto. intros. inv H1. 
  generalize (genv_nextvar_pos ge). intros. omegaContradiction.
Qed.

Theorem find_funct_ptr_symbol_inversion:
  forall p id b f,
  find_symbol (globalenv p) id = Some b ->
  find_funct_ptr (globalenv p) b = Some f ->
  In (id, f) p.(prog_funct).
Proof.
  intros until f.

  assert (forall fl ge,
          find_symbol (add_functions ge fl) id = Some b ->
          find_funct_ptr (add_functions ge fl) b = Some f ->
          In (id, f) fl \/ (find_symbol ge id = Some b /\ find_funct_ptr ge b = Some f)).
  induction fl; simpl; intros.
  auto.
  exploit IHfl; eauto. intros [A | [A B]]. auto.
  destruct a as [id' f'].
  unfold find_symbol in A; simpl in A.
  unfold find_funct_ptr in B; simpl in B.
  rewrite PTree.gsspec in A. destruct (peq id id'). inv A. 
  rewrite ZMap.gss in B. inv B. auto.
  rewrite ZMap.gso in B. right; auto. 
  exploit genv_symb_range; eauto. unfold block, ZIndexed.t; omega.

  intros. assert (b < 0) by (eapply find_funct_ptr_negative; eauto). 
  unfold globalenv in *. rewrite add_variables_same_funs in H1.
  exploit (H (prog_funct p) empty_genv). 
  eapply add_variables_symb_neg; eauto. auto. 
  intuition. unfold find_symbol in H3; simpl in H3. rewrite PTree.gempty in H3. discriminate.
Qed.

Theorem find_symbol_not_nullptr:
  forall p id b,
  find_symbol (globalenv p) id = Some b -> b <> Mem.nullptr.
Proof.
  intros until b. unfold find_symbol. destruct (globalenv p); simpl. 
  intros. exploit genv_symb_range0; eauto. intuition.
Qed.

Theorem global_addresses_distinct:
  forall ge id1 id2 b1 b2,
  id1 <> id2 ->
  find_symbol ge id1 = Some b1 ->
  find_symbol ge id2 = Some b2 ->
  b1 <> b2.
Proof.
  intros. red; intros; subst. elim H. destruct ge. eauto. 
Qed.

(** * Construction of the initial memory state *)

Section INITMEM.

Variable ge: t.

Definition init_data_size (i: init_data) : Z :=
  match i with
  | Init_int8 _ => 1
  | Init_int16 _ => 2
  | Init_int32 _ => 4
  | Init_float32 _ => 4
  | Init_float64 _ => 8
  | Init_addrof _ _ => 4
  | Init_space n => Zmax n 0
  end.

Lemma init_data_size_pos:
  forall i, init_data_size i >= 0.
Proof.
  destruct i; simpl; try omega. generalize (Zle_max_r z 0). omega.
Qed.

Definition store_init_data (m: mem) (b: block) (p: Z) (id: init_data) : option mem :=
  match id with
  | Init_int8 n => Mem.store Mint8unsigned m b p (Vint n)
  | Init_int16 n => Mem.store Mint16unsigned m b p (Vint n)
  | Init_int32 n => Mem.store Mint32 m b p (Vint n)
  | Init_float32 n => Mem.store Mfloat32 m b p (Vfloat n)
  | Init_float64 n => Mem.store Mfloat64 m b p (Vfloat n)
  | Init_addrof symb ofs =>
      match find_symbol ge symb with
      | None => None
      | Some b' => Mem.store Mint32 m b p (Vptr b' ofs)
      end
  | Init_space n => Some m
  end.

Fixpoint store_init_data_list (m: mem) (b: block) (p: Z) (idl: list init_data)
                              {struct idl}: option mem :=
  match idl with
  | nil => Some m
  | id :: idl' =>
      match store_init_data m b p id with
      | None => None
      | Some m' => store_init_data_list m' b (p + init_data_size id) idl'
      end
  end.

Fixpoint init_data_list_size (il: list init_data) {struct il} : Z :=
  match il with
  | nil => 0
  | i :: il' => init_data_size i + init_data_list_size il'
  end.

Definition alloc_variable (m: mem) (idv: ident * list init_data * V) : option mem :=
  let (m', b) := Mem.alloc m 0 (init_data_list_size idv#1#2) in
  store_init_data_list m' b 0 idv#1#2.

Fixpoint alloc_variables (m: mem) (vl: list (ident * list init_data * V))
                         {struct vl} : option mem :=
  match vl with
  | nil => Some m
  | v :: vl' =>
      match alloc_variable m v with
      | None => None
      | Some m' => alloc_variables m' vl'
      end
  end.

Remark store_init_data_list_nextblock:
  forall idl b m p m',
  store_init_data_list m b p idl = Some m' ->
  Mem.nextblock m' = Mem.nextblock m.
Proof.
  induction idl; simpl; intros until m'.
  intros. congruence.
  caseEq (store_init_data m b p a); try congruence. intros. 
  transitivity (Mem.nextblock m0). eauto. 
  destruct a; simpl in H; try (eapply Mem.nextblock_store; eauto; fail).
  congruence. 
  destruct (find_symbol ge i); try congruence. eapply Mem.nextblock_store; eauto. 
Qed.

Remark alloc_variables_nextblock:
  forall vl m m',
  alloc_variables m vl = Some m' ->
  Mem.nextblock m' = Mem.nextblock m + Z_of_nat(List.length vl).
Proof.
  induction vl.
  simpl; intros. inv H; unfold block; omega.
  simpl length; rewrite inj_S; simpl. intros m m'. 
  unfold alloc_variable. 
  caseEq (Mem.alloc m 0 (init_data_list_size (a#1)#2)). intros m1 b ALLOC.
  caseEq (store_init_data_list m1 b 0 a#1#2); try congruence. intros m2 STORE REC.
  rewrite (IHvl _ _ REC). 
  rewrite (store_init_data_list_nextblock _ _ _ _ STORE).
  rewrite (Mem.nextblock_alloc _ _ _ _ _ ALLOC).
  unfold block in *; omega.
Qed.

Remark store_init_data_list_perm:
  forall prm b' q idl b m p m',
  store_init_data_list m b p idl = Some m' ->
  Mem.perm m b' q prm -> Mem.perm m' b' q prm.
Proof.
  induction idl; simpl; intros until m'.
  intros. congruence.
  caseEq (store_init_data m b p a); try congruence. intros. 
  eapply IHidl; eauto. 
  destruct a; simpl in H; eauto with mem.
  congruence.
  destruct (find_symbol ge i); try congruence. eauto with mem.
Qed.

Remark alloc_variables_perm:
  forall prm b' q vl m m',
  alloc_variables m vl = Some m' ->
  Mem.perm m b' q prm -> Mem.perm m' b' q prm.
Proof.
  induction vl.
  simpl; intros. congruence.
  intros until m'. simpl. unfold alloc_variable. 
  caseEq (Mem.alloc m 0 (init_data_list_size (a#1)#2)). intros m1 b ALLOC.
  caseEq (store_init_data_list m1 b 0 a#1#2); try congruence. intros m2 STORE REC PERM.
  eapply IHvl; eauto. 
  eapply store_init_data_list_perm; eauto. 
  eauto with mem.
Qed.

Remark store_init_data_list_outside:
  forall b il m p m',
  store_init_data_list m b p il = Some m' ->
  forall chunk b' q,
  b' <> b \/ q + size_chunk chunk <= p ->
  Mem.load chunk m' b' q = Mem.load chunk m b' q.
Proof.
  induction il; simpl.
  intros; congruence.
  intros until m'. caseEq (store_init_data m b p a); try congruence. 
  intros m1 A B chunk b' q C. transitivity (Mem.load chunk m1 b' q).
  eapply IHil; eauto. generalize (init_data_size_pos a). intuition omega.
  destruct a; simpl in A;
  try (eapply Mem.load_store_other; eauto; intuition; fail).
  congruence.
  destruct (find_symbol ge i); try congruence. 
  eapply Mem.load_store_other; eauto; intuition.
Qed.

(*
Remark alloc_variables_nextblock:
  forall vl g m m',
  alloc_variables m vl = Some m' ->
  Mem.nextblock m = genv_nextvar g ->
  Mem.nextblock m' = genv_nextvar (add_variables g vl).
Proof.
  induction vl; simpl; intros until m'.
  intros. congruence.
  unfold alloc_variable. 
  caseEq (Mem.alloc m 0 (init_data_list_size (a#1)#2)). intros m1 b ALLOC.
  caseEq (store_init_data_list m1 b 0 a#1#2); try congruence. intros m2 STORE REC EQ.
  eapply IHvl; eauto. 
  rewrite (store_init_data_list_nextblock _ _ _ _ STORE).
  rewrite (Mem.nextblock_alloc _ _ _ _ _ ALLOC).
  simpl. unfold block in *; omega.
Qed.
*)
Fixpoint load_store_init_data (m: mem) (b: block) (p: Z) (il: list init_data) {struct il} : Prop :=
  match il with
  | nil => True
  | Init_int8 n :: il' =>
      Mem.load Mint8unsigned m b p = Some(Vint(Int.zero_ext 8 n))
      /\ load_store_init_data m b (p + 1) il'
  | Init_int16 n :: il' =>
      Mem.load Mint16unsigned m b p = Some(Vint(Int.zero_ext 16 n))
      /\ load_store_init_data m b (p + 2) il'
  | Init_int32 n :: il' =>
      Mem.load Mint32 m b p = Some(Vint n)
      /\ load_store_init_data m b (p + 4) il'
  | Init_float32 n :: il' =>
      Mem.load Mfloat32 m b p = Some(Vfloat(Float.singleoffloat n))
      /\ load_store_init_data m b (p + 4) il'
  | Init_float64 n :: il' =>
      Mem.load Mfloat64 m b p = Some(Vfloat n)
      /\ load_store_init_data m b (p + 8) il'
  | Init_addrof symb ofs :: il' =>
      (exists b', find_symbol ge symb = Some b' /\ Mem.load Mint32 m b p = Some(Vptr b' ofs))
      /\ load_store_init_data m b (p + 4) il'
  | Init_space n :: il' =>
      load_store_init_data m b (p + Zmax n 0) il'
  end.

Lemma store_init_data_list_charact:
  forall b il m p m',
  store_init_data_list m b p il = Some m' ->
  load_store_init_data m' b p il.
Proof.
  assert (A: forall chunk v m b p m1 il m',
    Mem.store chunk m b p v = Some m1 ->
    store_init_data_list m1 b (p + size_chunk chunk) il = Some m' ->
    Val.has_type v (type_of_chunk chunk) ->
    Mem.load chunk m' b p = Some(Val.load_result chunk v)).
  intros. transitivity (Mem.load chunk m1 b p).
  eapply store_init_data_list_outside; eauto. right. omega. 
  eapply Mem.load_store_same; eauto. 

  induction il; simpl.
  auto.
  intros until m'. caseEq (store_init_data m b p a); try congruence. 
  intros m1 B C.
  exploit IHil; eauto. intro D. 
  destruct a; simpl in B; intuition.
  eapply (A Mint8unsigned (Vint i)); eauto. simpl; auto.
  eapply (A Mint16unsigned (Vint i)); eauto. simpl; auto.
  eapply (A Mint32 (Vint i)); eauto. simpl; auto.
  eapply (A Mfloat32 (Vfloat f)); eauto. simpl; auto.
  eapply (A Mfloat64 (Vfloat f)); eauto. simpl; auto.
  destruct (find_symbol ge i); try congruence. exists b0; split; auto. 
  eapply (A Mint32 (Vptr b0 i0)); eauto. simpl; auto. 
Qed.

Remark load_alloc_variables:
  forall chunk b p vl m m',
  alloc_variables m vl = Some m' ->
  Mem.valid_block m b ->
  Mem.load chunk m' b p = Mem.load chunk m b p.
Proof.
  induction vl; simpl; intros until m'.
  congruence.
  unfold alloc_variable. 
  caseEq (Mem.alloc m 0 (init_data_list_size a#1#2)); intros m1 b1 ALLOC.
  caseEq (store_init_data_list m1 b1 0 a#1#2); try congruence. intros m2 STO REC VAL.
  transitivity (Mem.load chunk m2 b p). 
  apply IHvl; auto. red. rewrite (store_init_data_list_nextblock _ _ _ _ STO).
  change (Mem.valid_block m1 b). eauto with mem. 
  transitivity (Mem.load chunk m1 b p).
  eapply store_init_data_list_outside; eauto. left. 
  apply Mem.valid_not_valid_diff with m; eauto with mem.
  eapply Mem.load_alloc_unchanged; eauto.
Qed. 

Remark load_store_init_data_invariant:
  forall m m' b,
  (forall chunk ofs, Mem.load chunk m' b ofs = Mem.load chunk m b ofs) ->
  forall il p,
  load_store_init_data m b p il -> load_store_init_data m' b p il.
Proof.
  induction il; intro p; simpl.
  auto.
  repeat rewrite H. destruct a; intuition. 
Qed.

Lemma alloc_variables_charact:
  forall id init v vl g m m',
  genv_nextvar g = Mem.nextblock m ->
  alloc_variables m vl = Some m' ->
  list_norepet (map (fun v => v#1#1) vl) ->
  In (id, init, v) vl ->
  exists b, find_symbol (add_variables g vl) id = Some b 
         /\ find_var_info (add_variables g vl) b = Some v
         /\ Mem.range_perm m' b 0 (init_data_list_size init) Writable
         /\ load_store_init_data m' b 0 init.
Proof.
  induction vl; simpl.
  contradiction.
  intros until m'; intro NEXT.
  unfold alloc_variable. destruct a as [[id' init'] v']. simpl.
  caseEq (Mem.alloc m 0 (init_data_list_size init')); try congruence.
  intros m1 b ALLOC. 
  caseEq (store_init_data_list m1 b 0 init'); try congruence.
  intros m2 STORE REC NOREPET IN. inv NOREPET.
  exploit Mem.alloc_result; eauto. intro BEQ. 
  destruct IN. inv H.
  exists (Mem.nextblock m); split. 
  rewrite add_variables_same_symb; auto. unfold find_symbol; simpl. 
  rewrite PTree.gss. congruence. 
  split. rewrite add_variables_same_address. unfold find_var_info; simpl.
  rewrite NEXT. apply ZMap.gss.
  simpl. rewrite <- NEXT; omega.
  split. red; intros. eapply alloc_variables_perm; eauto. 
  eapply store_init_data_list_perm; eauto.
  apply Mem.perm_implies with Freeable; eauto with mem.
  apply load_store_init_data_invariant with m2.
  intros. eapply load_alloc_variables; eauto. 
  red. rewrite (store_init_data_list_nextblock _ _ _ _ STORE).
  change (Mem.valid_block m1 (Mem.nextblock m)). eauto with mem.
  eapply store_init_data_list_charact; eauto. 

  apply IHvl with m2; auto.
  simpl. rewrite (store_init_data_list_nextblock _ _ _ _ STORE).
  rewrite (Mem.nextblock_alloc _ _ _ _ _ ALLOC). unfold block in *; omega.
Qed.

End INITMEM.

Definition init_mem (p: program F V) :=
  alloc_variables (globalenv p) Mem.empty p.(prog_vars).

Theorem find_symbol_not_fresh:
  forall p id b m,
  init_mem p = Some m ->
  find_symbol (globalenv p) id = Some b -> Mem.valid_block m b.
Proof.
  unfold init_mem; intros.
  exploit alloc_variables_nextblock; eauto. rewrite Mem.nextblock_empty. intro.
  exploit genv_symb_range; eauto. intros.
  generalize (add_variables_nextvar (prog_vars p) (add_functions empty_genv (prog_funct p))).
  rewrite add_functions_nextvar. simpl genv_nextvar. intro.
  red. rewrite H1. rewrite <- H3. intuition.
Qed.

Theorem find_var_exists:
  forall p id init v m,
  list_norepet (prog_var_names p) ->
  In (id, init, v) (prog_vars p) ->
  init_mem p = Some m ->
  exists b, find_symbol (globalenv p) id = Some b
         /\ find_var_info (globalenv p) b = Some v
         /\ Mem.range_perm m b 0 (init_data_list_size init) Writable
         /\ load_store_init_data (globalenv p) m b 0 init.
Proof.
  intros. exploit alloc_variables_charact; eauto. 
  instantiate (1 := Mem.empty). rewrite add_functions_nextvar. rewrite Mem.nextblock_empty; auto.
  assumption.
Qed.

(** ** Compatibility with memory injections *)

Section INITMEM_INJ.

Variable ge: t.
Variable thr: block.
Hypothesis symb_inject: forall id b, find_symbol ge id = Some b -> b < thr.

Lemma store_init_data_neutral:
  forall m b p id m',
  Mem.inject_neutral thr m ->
  b < thr ->
  store_init_data ge m b p id = Some m' ->
  Mem.inject_neutral thr m'.
Proof.
  intros.
  destruct id; simpl in H1; try (eapply Mem.store_inject_neutral; eauto; fail).
  inv H1; auto.
  revert H1. caseEq (find_symbol ge i); try congruence. intros b' FS ST. 
  eapply Mem.store_inject_neutral; eauto. 
  econstructor. unfold Mem.flat_inj. apply zlt_true; eauto. 
  rewrite Int.add_zero. auto. 
Qed.

Lemma store_init_data_list_neutral:
  forall b idl m p m',
  Mem.inject_neutral thr m ->
  b < thr ->
  store_init_data_list ge m b p idl = Some m' ->
  Mem.inject_neutral thr m'.
Proof.
  induction idl; simpl.
  intros; congruence.
  intros until m'; intros INJ FB.
  caseEq (store_init_data ge m b p a); try congruence. intros. 
  eapply IHidl. eapply store_init_data_neutral; eauto. auto. eauto. 
Qed.

Lemma alloc_variable_neutral:
  forall id m m',
  alloc_variable ge m id = Some m' ->
  Mem.inject_neutral thr m ->
  Mem.nextblock m < thr ->
  Mem.inject_neutral thr m'.
Proof.
  intros until m'. unfold alloc_variable. 
  caseEq (Mem.alloc m 0 (init_data_list_size (id#1)#2)); intros m1 b; intros.
  eapply store_init_data_list_neutral with (b := b).
  eapply Mem.alloc_inject_neutral; eauto.
  rewrite (Mem.alloc_result _ _ _ _ _ H). auto.
  eauto.
Qed.

Lemma alloc_variables_neutral:
  forall idl m m',
  alloc_variables ge m idl = Some m' ->
  Mem.inject_neutral thr m ->
  Mem.nextblock m' <= thr ->
  Mem.inject_neutral thr m'.
Proof.
  induction idl; simpl.
  intros. congruence.
  intros until m'. caseEq (alloc_variable ge m a); try congruence. intros.
  assert (Mem.nextblock m' = Mem.nextblock m + Z_of_nat(length (a :: idl))).
  eapply alloc_variables_nextblock with ge. simpl. rewrite H. auto. 
  simpl length in H3. rewrite inj_S in H3. 
  exploit alloc_variable_neutral; eauto. unfold block in *; omega.
Qed.

End INITMEM_INJ.

Theorem initmem_inject:
  forall p m,
  init_mem p = Some m ->
  Mem.inject (Mem.flat_inj (Mem.nextblock m)) m m.
Proof.
  unfold init_mem; intros.
  apply Mem.neutral_inject. 
  eapply alloc_variables_neutral; eauto. 
  intros. exploit find_symbol_not_fresh; eauto.
  apply Mem.empty_inject_neutral.
  omega.
Qed.

End GENV.

(** * Commutation with program transformations *)

(** ** Commutation with matching between programs. *)

Section MATCH_PROGRAMS.

Variables A B V W: Type.
Variable match_fun: A -> B -> Prop.
Variable match_var: V -> W -> Prop.

Record match_genvs (ge1: t A V) (ge2: t B W): Prop := {
  mge_nextfun: genv_nextfun ge1 = genv_nextfun ge2;
  mge_nextvar: genv_nextvar ge1 = genv_nextvar ge2;
  mge_symb:    genv_symb ge1 = genv_symb ge2;
  mge_funs:
    forall b f, ZMap.get b (genv_funs ge1) = Some f ->
    exists tf, ZMap.get b (genv_funs ge2) = Some tf /\ match_fun f tf;
  mge_rev_funs:
    forall b tf, ZMap.get b (genv_funs ge2) = Some tf ->
    exists f, ZMap.get b (genv_funs ge1) = Some f /\ match_fun f tf;
  mge_vars:
    forall b v, ZMap.get b (genv_vars ge1) = Some v ->
    exists tv, ZMap.get b (genv_vars ge2) = Some tv /\ match_var v tv;
  mge_rev_vars:
    forall b tv, ZMap.get b (genv_vars ge2) = Some tv ->
    exists v, ZMap.get b (genv_vars ge1) = Some v /\ match_var v tv
}.

Lemma add_function_match:
  forall ge1 ge2 id f1 f2,
  match_genvs ge1 ge2 ->
  match_fun f1 f2 ->
  match_genvs (add_function ge1 (id, f1)) (add_function ge2 (id, f2)).
Proof.
  intros. destruct H. constructor; simpl. 
  congruence. congruence. congruence.
  rewrite mge_nextfun0. intros. rewrite ZMap.gsspec in H. rewrite ZMap.gsspec. 
  destruct (ZIndexed.eq b (genv_nextfun ge2)). 
  exists f2; split; congruence.
  eauto.
  rewrite mge_nextfun0. intros. rewrite ZMap.gsspec in H. rewrite ZMap.gsspec. 
  destruct (ZIndexed.eq b (genv_nextfun ge2)). 
  exists f1; split; congruence.
  eauto.
  auto.
  auto.
Qed.

Lemma add_functions_match:
  forall fl1 fl2, list_forall2 (match_funct_entry match_fun) fl1 fl2 ->
  forall ge1 ge2, match_genvs ge1 ge2 ->
  match_genvs (add_functions ge1 fl1) (add_functions ge2 fl2).
Proof.
  induction 1; intros; simpl. 
  auto.
  destruct a1 as [id1 f1]; destruct b1 as [id2 f2].
  destruct H. subst. apply IHlist_forall2. apply add_function_match; auto.
Qed.

Lemma add_variable_match:
  forall ge1 ge2 id idl v1 v2,
  match_genvs ge1 ge2 ->
  match_var v1 v2 ->
  match_genvs (add_variable ge1 (id, idl, v1)) (add_variable ge2 (id, idl, v2)).
Proof.
  intros. destruct H. constructor; simpl. 
  congruence. congruence. congruence.
  auto.
  auto.
  rewrite mge_nextvar0. intros. rewrite ZMap.gsspec in H. rewrite ZMap.gsspec. 
  destruct (ZIndexed.eq b (genv_nextvar ge2)). 
  exists v2; split; congruence.
  eauto.
  rewrite mge_nextvar0. intros. rewrite ZMap.gsspec in H. rewrite ZMap.gsspec. 
  destruct (ZIndexed.eq b (genv_nextvar ge2)). 
  exists v1; split; congruence.
  eauto.
Qed.

Lemma add_variables_match:
  forall vl1 vl2, list_forall2 (match_var_entry match_var) vl1 vl2 ->
  forall ge1 ge2, match_genvs ge1 ge2 ->
  match_genvs (add_variables ge1 vl1) (add_variables ge2 vl2).
Proof.
  induction 1; intros; simpl. 
  auto.
  destruct a1 as [[id1 init1] f1]; destruct b1 as [[id2 init2] f2].
  destruct H. destruct H2. subst. apply IHlist_forall2. apply add_variable_match; auto.
Qed.

Variable p: program A V.
Variable p': program B W.
Hypothesis progmatch: match_program match_fun match_var p p'.

Lemma globalenvs_match:
  match_genvs (globalenv p) (globalenv p').
Proof.
  unfold globalenv. destruct progmatch. destruct H0. 
  apply add_variables_match; auto. apply add_functions_match; auto. 
  constructor; simpl; auto; intros; rewrite ZMap.gi in H2; congruence.
Qed.

Theorem find_funct_ptr_match:
  forall (b : block) (f : A),
  find_funct_ptr (globalenv p) b = Some f ->
  exists tf : B,
  find_funct_ptr (globalenv p') b = Some tf /\ match_fun f tf.
Proof (mge_funs globalenvs_match).

Theorem find_funct_ptr_rev_match:
  forall (b : block) (tf : B),
  find_funct_ptr (globalenv p') b = Some tf ->
  exists f : A,
  find_funct_ptr (globalenv p) b = Some f /\ match_fun f tf.
Proof (mge_rev_funs globalenvs_match).

Theorem find_funct_match:
  forall (v : val) (f : A),
  find_funct (globalenv p) v = Some f ->
  exists tf : B, find_funct (globalenv p') v = Some tf /\ match_fun f tf.
Proof.
  intros. exploit find_funct_inv; eauto. intros [b EQ]. subst v. 
  rewrite find_funct_find_funct_ptr in H. 
  rewrite find_funct_find_funct_ptr.
  apply find_funct_ptr_match. auto.
Qed.

Theorem find_funct_rev_match:
  forall (v : val) (tf : B),
  find_funct (globalenv p') v = Some tf ->
  exists f : A, find_funct (globalenv p) v = Some f /\ match_fun f tf.
Proof.
  intros. exploit find_funct_inv; eauto. intros [b EQ]. subst v. 
  rewrite find_funct_find_funct_ptr in H. 
  rewrite find_funct_find_funct_ptr.
  apply find_funct_ptr_rev_match. auto.
Qed.

Theorem find_var_info_match:
  forall (b : block) (v : V),
  find_var_info (globalenv p) b = Some v ->
  exists tv,
  find_var_info (globalenv p') b = Some tv /\ match_var v tv.
Proof (mge_vars globalenvs_match).

Theorem find_var_info_rev_match:
  forall (b : block) (tv : W),
  find_var_info (globalenv p') b = Some tv ->
  exists v,
  find_var_info (globalenv p) b = Some v /\ match_var v tv.
Proof (mge_rev_vars globalenvs_match).

Theorem find_symbol_match:
  forall (s : ident),
  find_symbol (globalenv p') s = find_symbol (globalenv p) s.
Proof.
  intros. destruct globalenvs_match. unfold find_symbol. congruence. 
Qed.

Lemma store_init_data_list_match:
  forall idl m b ofs,
  store_init_data_list (globalenv p') m b ofs idl =
  store_init_data_list (globalenv p) m b ofs idl.
Proof.
  induction idl; simpl; intros. 
  auto.
  assert (store_init_data (globalenv p') m b ofs a =
          store_init_data (globalenv p) m b ofs a).
  destruct a; simpl; auto. rewrite find_symbol_match. auto. 
  rewrite H. destruct (store_init_data (globalenv p) m b ofs a); auto. 
Qed.

Lemma alloc_variables_match:
  forall vl1 vl2, list_forall2 (match_var_entry match_var) vl1 vl2 ->
  forall m,
  alloc_variables (globalenv p') m vl2 = alloc_variables (globalenv p) m vl1.
Proof.
  induction 1; intros; simpl.
  auto.
  destruct a1 as [[id1 init1] v1]; destruct b1 as [[id2 init2] v2].
  destruct H. destruct H1. subst.
  unfold alloc_variable; simpl. 
  destruct (Mem.alloc m 0 (init_data_list_size init2)). 
  rewrite store_init_data_list_match. 
  destruct (store_init_data_list (globalenv p) m0 b 0 init2); auto.
Qed.

Theorem init_mem_match:
  forall m, init_mem p = Some m -> init_mem p' = Some m.
Proof.
  intros. rewrite <- H. unfold init_mem. destruct progmatch. destruct H1. 
  apply alloc_variables_match; auto. 
Qed.

End MATCH_PROGRAMS.

Section TRANSF_PROGRAM_PARTIAL2.

Variable A B V W: Type.
Variable transf_fun: A -> res B.
Variable transf_var: V -> res W.
Variable p: program A V.
Variable p': program B W.
Hypothesis transf_OK:
  transform_partial_program2 transf_fun transf_var p = OK p'.

Remark prog_match:
  match_program
    (fun fd tfd => transf_fun fd = OK tfd)
    (fun info tinfo => transf_var info = OK tinfo)
    p p'.
Proof.
  apply transform_partial_program2_match; auto.
Qed.

Theorem find_funct_ptr_transf_partial2:
  forall (b: block) (f: A),
  find_funct_ptr (globalenv p) b = Some f ->
  exists f',
  find_funct_ptr (globalenv p') b = Some f' /\ transf_fun f = OK f'.
Proof.
  intros. 
  exploit find_funct_ptr_match. eexact prog_match. eauto. 
  intros [tf [X Y]]. exists tf; auto.
Qed.

Theorem find_funct_ptr_rev_transf_partial2:
  forall (b: block) (tf: B),
  find_funct_ptr (globalenv p') b = Some tf ->
  exists f, find_funct_ptr (globalenv p) b = Some f /\ transf_fun f = OK tf.
Proof.
  intros. 
  exploit find_funct_ptr_rev_match. eexact prog_match. eauto. auto. 
Qed.

Theorem find_funct_transf_partial2:
  forall (v: val) (f: A),
  find_funct (globalenv p) v = Some f ->
  exists f',
  find_funct (globalenv p') v = Some f' /\ transf_fun f = OK f'.
Proof.
  intros. 
  exploit find_funct_match. eexact prog_match. eauto. 
  intros [tf [X Y]]. exists tf; auto.
Qed.

Theorem find_funct_rev_transf_partial2:
  forall (v: val) (tf: B),
  find_funct (globalenv p') v = Some tf ->
  exists f, find_funct (globalenv p) v = Some f /\ transf_fun f = OK tf.
Proof.
  intros. 
  exploit find_funct_rev_match. eexact prog_match. eauto. auto. 
Qed.

Theorem find_var_info_transf_partial2:
  forall (b: block) (v: V),
  find_var_info (globalenv p) b = Some v ->
  exists v',
  find_var_info (globalenv p') b = Some v' /\ transf_var v = OK v'.
Proof.
  intros. 
  exploit find_var_info_match. eexact prog_match. eauto. 
  intros [tv [X Y]]. exists tv; auto.
Qed.

Theorem find_var_info_rev_transf_partial2:
  forall (b: block) (v': W),
  find_var_info (globalenv p') b = Some v' ->
  exists v,
  find_var_info (globalenv p) b = Some v /\ transf_var v = OK v'.
Proof.
  intros. 
  exploit find_var_info_rev_match. eexact prog_match. eauto. 
  intros [v [X Y]]. exists v; auto.
Qed.

Theorem find_symbol_transf_partial2:
  forall (s: ident),
  find_symbol (globalenv p') s = find_symbol (globalenv p) s.
Proof.
  intros. eapply find_symbol_match. eexact prog_match.
Qed.

Theorem init_mem_transf_partial2:
  forall m, init_mem p = Some m -> init_mem p' = Some m.
Proof.
  intros. eapply init_mem_match. eexact prog_match. auto.
Qed.

End TRANSF_PROGRAM_PARTIAL2.

Section TRANSF_PROGRAM_PARTIAL.

Variable A B V: Type.
Variable transf: A -> res B.
Variable p: program A V.
Variable p': program B V.
Hypothesis transf_OK: transform_partial_program transf p = OK p'.

Remark transf2_OK:
  transform_partial_program2 transf (fun x => OK x) p = OK p'.
Proof.
  rewrite <- transf_OK. unfold transform_partial_program2, transform_partial_program.
  destruct (map_partial prefix_funct_name transf (prog_funct p)); auto.
  rewrite map_partial_identity; auto. 
Qed.

Theorem find_funct_ptr_transf_partial:
  forall (b: block) (f: A),
  find_funct_ptr (globalenv p) b = Some f ->
  exists f',
  find_funct_ptr (globalenv p') b = Some f' /\ transf f = OK f'.
Proof.
  exact (@find_funct_ptr_transf_partial2 _ _ _ _ _ _ _ _ transf2_OK).
Qed.

Theorem find_funct_ptr_rev_transf_partial:
  forall (b: block) (tf: B),
  find_funct_ptr (globalenv p') b = Some tf ->
  exists f, find_funct_ptr (globalenv p) b = Some f /\ transf f = OK tf.
Proof.
  exact (@find_funct_ptr_rev_transf_partial2 _ _ _ _ _ _ _ _ transf2_OK).
Qed.

Theorem find_funct_transf_partial:
  forall (v: val) (f: A),
  find_funct (globalenv p) v = Some f ->
  exists f',
  find_funct (globalenv p') v = Some f' /\ transf f = OK f'.
Proof.
  exact (@find_funct_transf_partial2 _ _ _ _ _ _ _ _ transf2_OK).
Qed.

Theorem find_funct_rev_transf_partial:
  forall (v: val) (tf: B),
  find_funct (globalenv p') v = Some tf ->
  exists f, find_funct (globalenv p) v = Some f /\ transf f = OK tf.
Proof.
  exact (@find_funct_rev_transf_partial2 _ _ _ _ _ _ _ _ transf2_OK).
Qed.

Theorem find_symbol_transf_partial:
  forall (s: ident),
  find_symbol (globalenv p') s = find_symbol (globalenv p) s.
Proof.
  exact (@find_symbol_transf_partial2 _ _ _ _ _ _ _ _ transf2_OK).
Qed.

Theorem init_mem_transf_partial:
  forall m, init_mem p = Some m -> init_mem p' = Some m.
Proof.
  exact (@init_mem_transf_partial2 _ _ _ _ _ _ _ _ transf2_OK).
Qed.

End TRANSF_PROGRAM_PARTIAL.

Section TRANSF_PROGRAM.

Variable A B V: Type.
Variable transf: A -> B.
Variable p: program A V.
Let tp := transform_program transf p.

Remark transf_OK:
  transform_partial_program (fun x => OK (transf x)) p = OK tp.
Proof.
  unfold tp, transform_program, transform_partial_program.
  rewrite map_partial_total. reflexivity.
Qed.

Theorem find_funct_ptr_transf:
  forall (b: block) (f: A),
  find_funct_ptr (globalenv p) b = Some f ->
  find_funct_ptr (globalenv tp) b = Some (transf f).
Proof.
  intros. 
  destruct (@find_funct_ptr_transf_partial _ _ _ _ _ _ transf_OK _ _ H)
  as [f' [X Y]]. congruence.
Qed.

Theorem find_funct_ptr_rev_transf:
  forall (b: block) (tf: B),
  find_funct_ptr (globalenv tp) b = Some tf ->
  exists f, find_funct_ptr (globalenv p) b = Some f /\ transf f = tf.
Proof.
  intros. exploit find_funct_ptr_rev_transf_partial. eexact transf_OK. eauto.
  intros [f [X Y]]. exists f; split. auto. congruence.
Qed.

Theorem find_funct_transf:
  forall (v: val) (f: A),
  find_funct (globalenv p) v = Some f ->
  find_funct (globalenv tp) v = Some (transf f).
Proof.
  intros. 
  destruct (@find_funct_transf_partial _ _ _ _ _ _ transf_OK _ _ H)
  as [f' [X Y]]. congruence.
Qed.

Theorem find_funct_rev_transf:
  forall (v: val) (tf: B),
  find_funct (globalenv tp) v = Some tf ->
  exists f, find_funct (globalenv p) v = Some f /\ transf f = tf.
Proof.
  intros. exploit find_funct_rev_transf_partial. eexact transf_OK. eauto.
  intros [f [X Y]]. exists f; split. auto. congruence.
Qed.

Theorem find_symbol_transf:
  forall (s: ident),
  find_symbol (globalenv tp) s = find_symbol (globalenv p) s.
Proof.
  exact (@find_symbol_transf_partial _ _ _ _ _ _ transf_OK).
Qed.

Theorem init_mem_transf:
  forall m, init_mem p = Some m -> init_mem tp = Some m.
Proof.
  exact (@init_mem_transf_partial _ _ _ _ _ _ transf_OK).
Qed.

End TRANSF_PROGRAM.

End Genv.