-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmallStepWf.v
1114 lines (1076 loc) · 34.7 KB
/
SmallStepWf.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import List.
Require Import Bool.
Require Import BinNat.
Require Import Omega.
Require Import sflib.
Require Import Common.
Require Import Memory.
Require Import Value.
Require Import Lang.
Require Import State.
Require Import LoadStore.
Require Import Behaviors.
Require Import SmallStep.
Require Import SmallStepAux.
Import SmallStep.
Import Ir.
Import Ir.SmallStep.
Import SmallStepAux.
Import Ir.
Import Ir.SmallStep.
Module Ir.
Module SmallStep.
(****************************************************
Theorems about sstep of instruction.
****************************************************)
Lemma incrpc_wf:
forall md c c'
(HWF:Ir.Config.wf md c)
(HC':c' = incrpc md c),
Ir.Config.wf md c'.
Proof.
(* High-level proof: incrpc changes stack frame only, and
next_trivial_pc satisfies valid_pc. *)
intros.
unfold incrpc in HC'.
destruct (Ir.Config.cur_fdef_pc md c) eqn:HC.
- destruct p as [fdef pc0].
remember (Ir.IRFunction.next_trivial_pc pc0 fdef) as pc_next.
destruct pc_next as [pc_next | ].
unfold Ir.Config.update_pc in HC'.
remember (Ir.Config.s c) as s'.
destruct s' as [ | [cid [pc0' r0']] st] .
+ congruence.
+ (* show that pc0' = pc0 *)
unfold Ir.Config.cur_fdef_pc in HC.
rewrite <- Heqs' in HC.
remember (Ir.Config.get_funid c cid) as ofunid.
destruct ofunid as [funid | ]; try (inversion HC; fail).
remember (Ir.IRModule.getf funid md) as ofdef'.
destruct ofdef' as [fdef' | ]; try (inversion HC; fail).
inversion HC.
rewrite H0, H1 in *.
clear H0 H1 HC.
(* Now prove Ir.Config.wf c' *)
rewrite HC'.
inversion HWF.
split.
* assumption.
* assumption.
* assumption.
* simpl.
intros.
rewrite <- Heqs' in wf_stack.
simpl in wf_stack.
destruct HIN.
-- inversion H. rewrite H1, H2, H3 in *. clear H H1 H2 H3.
apply Ir.IRFunction.next_trivial_pc_valid with (pc1 := pc0).
apply wf_stack with (curcid0 := curcid) (funid := funid0) (curregfile0 := curregfile).
left. reflexivity.
eassumption. assumption.
assert (HINCID:Some funid0 = Ir.Config.get_funid c curcid).
{ eapply Ir.Config.cid_to_f_In_get_funid. eassumption. assumption. }
rewrite <- Heqofunid in HINCID.
inversion HINCID.
rewrite H0 in HF. rewrite <- HF in Heqofdef'.
inversion Heqofdef'. rewrite <- H1. congruence.
-- apply wf_stack with (curcid := curcid) (funid := funid0) (curregfile := curregfile).
right. assumption. assumption. assumption.
* simpl. intros.
eapply wf_ptr. erewrite <- get_val_incrpc with (md := md).
unfold incrpc. unfold Ir.Config.cur_fdef_pc. rewrite <- Heqs'.
rewrite <- Heqofunid. rewrite <- Heqofdef'.
rewrite <- Heqpc_next. unfold Ir.Config.update_pc.
rewrite <- Heqs'. eassumption.
* simpl. intros. eapply wf_ptr_mem.
eassumption. eassumption. eassumption.
+ congruence.
- congruence.
Qed.
Lemma update_rval_wf:
forall md c c' r v
(HWF:Ir.Config.wf md c)
(HC':c' = Ir.Config.update_rval c r v)
(HPTRWF:forall p, v = Ir.ptr p -> Ir.Config.ptr_wf p (Ir.Config.m c)),
Ir.Config.wf md c'.
Proof.
intros.
inversion HWF.
unfold Ir.Config.update_rval in HC'.
rewrite HC'. clear HC'.
destruct (Ir.Config.s c) as [ | [cid0 [pc0 reg0]] s'] eqn:Hs.
{ split; try assumption.
intros. rewrite Hs in HIN. inversion HIN. }
{ split; try (simpl; assumption).
simpl. intros.
destruct HIN.
- inversion H.
destruct curregfile; inversion H3.
rewrite H1, H2 in *. clear H1 H2.
eapply wf_stack with (curcid0 := curcid). simpl. left. reflexivity.
eassumption. assumption.
- eapply wf_stack.
simpl. right. eassumption. eassumption. assumption.
- simpl. intros.
unfold Ir.Config.get_val in HGETVAL.
unfold Ir.Config.get_rval in HGETVAL.
simpl in HGETVAL.
destruct op eqn:Hop.
+ eapply wf_ptr with (op := op). unfold Ir.Config.get_val.
des_ifs.
+ destruct (Nat.eqb r r0) eqn:Hreg.
{ apply HPTRWF with (p := p).
rewrite Nat.eqb_eq in Hreg. subst r0.
rewrite Ir.Regfile.get_update in HGETVAL. congruence. }
{ rewrite Nat.eqb_neq in Hreg.
rewrite Ir.Regfile.get_update2 in HGETVAL; try congruence.
eapply wf_ptr with (op := Ir.opreg r0).
unfold Ir.Config.get_val. unfold Ir.Config.get_rval.
des_ifs. }
}
Qed.
Lemma update_reg_and_incrpc_wf:
forall md c c' v r
(HWF:Ir.Config.wf md c)
(HC':c' = update_reg_and_incrpc md c r v)
(HPTRWF:forall p, v = Ir.ptr p -> Ir.Config.ptr_wf p (Ir.Config.m c)),
Ir.Config.wf md c'.
Proof.
intros.
unfold update_reg_and_incrpc in HC'.
assert (Ir.Config.wf md (Ir.Config.update_rval c r v)).
{ eapply update_rval_wf. eassumption. reflexivity. eassumption. }
rewrite HC'.
eapply incrpc_wf.
eapply H. reflexivity.
Qed.
(* terminator small step preserves wellformedness. *)
Lemma t_step_wf:
forall md c c' e
(HWF:Ir.Config.wf md c)
(HSTEP:t_step md c = sr_success e c'),
Ir.Config.wf md c'.
Proof.
intros.
inv HWF.
unfold t_step in HSTEP.
des_ifs.
{ unfold br in HSTEP.
des_ifs.
split; try (unfold Ir.Config.update_pc; des_ifs; done).
{ unfold Ir.Config.update_pc.
simpl in wf_stack. simpl.
intros.
destruct (Ir.Config.s c) eqn:HS.
{ rewrite HS in HIN. inv HIN. }
destruct p1. destruct p1. simpl in *.
destruct HIN.
{ inv H.
unfold Ir.Config.cur_fdef_pc in Heq0.
rewrite HS in Heq0.
unfold Ir.Config.get_funid in Heq0.
des_ifs.
apply list_find_key_In in HIN2.
rewrite Heq0 in HIN2.
assert (List.length (p::l) < 2).
{ eapply list_find_key_NoDup.
eapply wf_cid_to_f.
rewrite Heq0. reflexivity. }
destruct l.
{ inv HIN2; try inv H0.
simpl in Heq3. rewrite Heq3 in HF. inv HF.
eapply Ir.IRFunction.get_begin_pc_bb_valid.
eassumption.
}
{ simpl in H. omega. }
}
{ eapply wf_stack.
{ right. eassumption. }
{ eassumption. }
{ eassumption. }
}
}
{ simpl. intros.
rewrite Ir.Config.m_update_pc. rewrite Ir.Config.get_val_update_pc in HGETVAL.
eapply wf_ptr. eassumption. }
}
{ unfold br in HSTEP.
des_ifs.
split; try (unfold Ir.Config.update_pc; des_ifs; done).
{ unfold Ir.Config.update_pc.
simpl in wf_stack. simpl.
intros.
destruct (Ir.Config.s c) eqn:HS.
{ rewrite HS in HIN. inv HIN. }
destruct p1. destruct p1. simpl in *.
destruct HIN.
{ inv H.
unfold Ir.Config.cur_fdef_pc in Heq0.
rewrite HS in Heq0.
unfold Ir.Config.get_funid in Heq0.
des_ifs.
apply list_find_key_In in HIN2.
rewrite Heq0 in HIN2.
assert (List.length (p::l) < 2).
{ eapply list_find_key_NoDup.
eapply wf_cid_to_f.
rewrite Heq0. reflexivity. }
destruct l.
{ inv HIN2; try inv H0.
simpl in Heq5. rewrite Heq5 in HF. inv HF.
eapply Ir.IRFunction.get_begin_pc_bb_valid.
eassumption.
}
{ simpl in H. omega. }
}
{ eapply wf_stack.
{ right. eassumption. }
{ eassumption. }
{ eassumption. }
}
}
{ simpl. intros.
rewrite Ir.Config.m_update_pc. rewrite Ir.Config.get_val_update_pc in HGETVAL.
eapply wf_ptr. eassumption. }
}
{ unfold br in HSTEP.
des_ifs.
split; try (unfold Ir.Config.update_pc; des_ifs; done).
{ unfold Ir.Config.update_pc.
simpl in wf_stack. simpl.
intros.
destruct (Ir.Config.s c) eqn:HS.
{ rewrite HS in HIN. inv HIN. }
destruct p1. destruct p1. simpl in *.
destruct HIN.
{ inv H.
unfold Ir.Config.cur_fdef_pc in Heq0.
rewrite HS in Heq0.
unfold Ir.Config.get_funid in Heq0.
des_ifs.
apply list_find_key_In in HIN2.
rewrite Heq0 in HIN2.
assert (List.length (p::l) < 2).
{ eapply list_find_key_NoDup.
eapply wf_cid_to_f.
rewrite Heq0. reflexivity. }
destruct l.
{ inv HIN2; try inv H0.
simpl in Heq5. rewrite Heq5 in HF. inv HF.
eapply Ir.IRFunction.get_begin_pc_bb_valid.
eassumption.
}
{ simpl in H. omega. }
}
{ eapply wf_stack.
{ right. eassumption. }
{ eassumption. }
{ eassumption. }
}
}
{ simpl. intros.
rewrite Ir.Config.m_update_pc. rewrite Ir.Config.get_val_update_pc in HGETVAL.
eapply wf_ptr. eassumption. }
}
Qed.
Ltac thats_it := eapply update_reg_and_incrpc_wf; eauto.
Ltac des_op c op op' HINV :=
destruct (Ir.Config.get_val c op) as [op' | ]; try (inversion HINV; fail).
Ltac des_inv v HINV :=
destruct (v); try (inversion HINV; fail).
Ltac try_wf :=
des_ifs; try (eapply update_reg_and_incrpc_wf; try eassumption;
try reflexivity; try congruence; fail).
Lemma gep_wf:
forall p n t m inb p0
(HGEP:gep p n t m inb = Ir.ptr p0)
(HMWF:Ir.Memory.wf m)
(HPWF:Ir.Config.ptr_wf p m),
Ir.Config.ptr_wf p0 m.
Proof.
intros.
unfold gep in HGEP.
inv HPWF.
des_ifs.
{ exploit H. ss. intros HH. inv HH. inv H2.
rewrite Heq in H3. inv H3.
split.
{ intros. inv H2.
split.
apply twos_compl_add_lt.
eauto.
}
{ intros. ss. }
}
{ exploit H. ss. intros HH. inv HH. inv H2.
split.
{ intros. inv H2. split.
apply twos_compl_add_lt.
eauto. }
{ intros. ss. }
}
{ exploit H0. ss. intros HH.
split.
{ intros. ss. }
{ intros. inv H1.
apply twos_compl_add_lt. }
}
{ exploit H0. ss. intros HH.
split.
{ intros. ss. }
{ intros. inv H1.
apply twos_compl_add_lt. }
}
{ exploit H0. ss. intros HH.
split.
{ intros. ss. }
{ intros. inv H1.
apply twos_compl_add_lt. }
}
Qed.
Lemma getpbits_ptr_in_byte:
forall x p n
(HPBITS:Ir.Byte.getpbits x = Some (p, n)),
Ir.Config.ptr_in_byte p n x.
Proof.
intros.
unfold Ir.Byte.getpbits in HPBITS.
unfold Ir.Config.ptr_in_byte.
des_ifs.
eauto.
Qed.
Lemma load_val_ptr_wf:
forall md st p retty p0
(HLOAD:Ir.load_val (Ir.Config.m st) p retty = Ir.ptr p0)
(HWF:Ir.Config.wf md st)
(HPWF:Ir.Config.ptr_wf p (Ir.Config.m st)),
Ir.Config.ptr_wf p0 (Ir.Config.m st).
Proof.
intros.
unfold Ir.load_val in HLOAD.
des_ifs.
unfold Ir.Byte.getptr in Heq.
unfold Ir.ty_bytesz in Heq.
unfold Ir.ty_bitsz in Heq.
rewrite Ir.PTRSZ_def in Heq.
simpl in Heq.
des_ifs.
unfold Ir.load_bytes in Heq1.
des_ifs.
dup Heq.
apply Ir.get_deref_singleton in Heq4. inv Heq4; try congruence.
inv H. inv H0. inv H. simpl in H1.
assert (exists x, l = x::nil).
{
destruct l; simpl in Heq0; try( inv Heq0; fail).
rewrite Nat.eqb_eq in Heq0.
destruct l. simpl in Heq0. eexists. reflexivity.
simpl in Heq0. omega.
}
inv H. simpl in Heq0.
simpl in Heq3. des_ifs.
inv HWF.
exploit wf_ptr_mem.
{ rewrite H1. reflexivity. }
{ eapply Ir.MemBlock.bytes_In_c. eassumption.
assert (In t0 [t0;x]). left. ss. eapply H. }
{ eapply getpbits_ptr_in_byte. eassumption. }
intros. ss.
{ inv HWF. ss. }
omega.
Qed.
Lemma store_val_ptr_wf:
forall md p0 c p v valty
(HWF:Ir.Config.wf md c)
(HPWF:Ir.Config.ptr_wf p0 (Ir.Config.m c)),
Ir.Config.ptr_wf p0 (Ir.store_val (Ir.Config.m c) p v valty).
Proof.
intros.
inv HPWF.
destruct p0.
{ exploit H. ss. intros HH. inv HH. inv H2.
split.
{ intros. inv H2. split. ss.
unfold Ir.store_val. des_ifs; eauto.
{ unfold Ir.store_bytes.
des_ifs; eauto.
destruct (b =? l) eqn:HEQ.
{ rewrite Nat.eqb_eq in HEQ. subst b.
erewrite Ir.Memory.get_set_id_short; try eassumption.
eexists. ss. inv HWF. ss. }
{ rewrite Ir.Memory.get_set_diff_short. eexists. eassumption.
inv HWF. ss. rewrite Nat.eqb_neq in HEQ. congruence. }
}
{ unfold Ir.store_bytes.
des_ifs; eauto.
destruct (b =? l) eqn:HEQ.
{ rewrite Nat.eqb_eq in HEQ. subst b.
erewrite Ir.Memory.get_set_id_short; try eassumption.
eexists. ss. inv HWF. ss. }
{ rewrite Ir.Memory.get_set_diff_short. eexists. eassumption.
inv HWF. ss. rewrite Nat.eqb_neq in HEQ. congruence. }
}
}
{ intros. congruence. }
}
{ exploit H0. ss. intros HH.
split.
{ intros. congruence. }
{ intros. inv H1. ss. }
}
Qed.
Lemma In_ofint_not_ptr_in_byte:
forall n1 len b p0 ofs
(HIN:In b (Ir.Byte.ofint n1 len)),
~ Ir.Config.ptr_in_byte p0 ofs b.
Proof.
intros.
unfold Ir.Byte.ofint in HIN.
remember (Ir.Bit.add_hzerobits (Ir.Bit.N_to_bits n1) (len - length (Ir.Bit.N_to_bits n1)))
as bits.
assert (List.Forall (fun b => forall p ofs, b <> Ir.Bit.baddr p ofs) bits).
{ rewrite Heqbits.
eapply Ir.Byte.add_hzerobits_notbaddr.
rewrite Forall_forall.
intros.
eapply Ir.Byte.N_to_bits_notbaddr. eassumption.
}
eapply Ir.Byte.from_bits_notbaddr in H.
rewrite Forall_forall in H.
apply H with (p := p0) (ofs := ofs) in HIN.
intros HH.
unfold Ir.Config.ptr_in_byte in HH.
intuition.
Qed.
Lemma ptr_wf_set:
forall p1 c b mb'
(HMWF:Ir.Memory.wf (Ir.Config.m c))
(HPTRWF:Ir.Config.ptr_wf p1 (Ir.Config.m c)),
Ir.Config.ptr_wf p1 (Ir.Memory.set (Ir.Config.m c) b mb').
Proof.
intros.
destruct p1.
{ inv HPTRWF. exploit H. ss. intros HH. inv HH.
inv H2.
split.
{ intros. inv H2. split. ss.
destruct (l =? b) eqn:HEQ.
{ rewrite Nat.eqb_eq in HEQ. subst l.
erewrite Ir.Memory.get_set_id_short. eexists. ss.
ss. eassumption. }
{ erewrite Ir.Memory.get_set_diff_short. eexists. eassumption.
ss. rewrite Nat.eqb_neq in HEQ. congruence.
}
}
{ intros. congruence. }
}
{ inv HPTRWF. exploit H0. ss. intros HH.
split.
{ intros. congruence. }
{ intros. inv H1. eauto. }
}
Qed.
Lemma store_val_ptr_mem_wf:
forall md c opptr mb p v valty bid byt p0 ofs mb0
(HWF:Ir.Config.wf md c)
(HOPVAL:Ir.Config.get_val c opptr = Some v)
(HGET0:Some mb0 = Ir.Memory.get (Ir.Config.m c) bid)
(HGET:Some mb = Ir.Memory.get (Ir.store_val (Ir.Config.m c) p v valty) bid)
(HIN:In byt (Ir.MemBlock.c mb))
(HPTR:Ir.Config.ptr_in_byte p0 ofs byt),
Ir.Config.ptr_wf p0 (Ir.store_val (Ir.Config.m c) p v valty).
Proof.
intros.
unfold Ir.store_val in *.
des_ifs; try (inv HWF; eapply wf_ptr_mem; eassumption).
{ unfold Ir.store_bytes in *.
des_ifs;
try (inv HWF; eapply wf_ptr_mem; eassumption).
eapply Ir.get_deref_singleton in Heq0.
destruct Heq0; try congruence.
destruct H. destruct H. inv H. simpl in H0.
destruct (b =? bid) eqn:HBID.
{ rewrite Nat.eqb_eq in HBID. subst bid.
erewrite Ir.Memory.get_set_id in HGET.
3: rewrite <- HGET0; reflexivity.
3: reflexivity.
inv HGET.
unfold Ir.MemBlock.set_bytes in HIN.
simpl in HIN.
apply List.in_app_or in HIN.
destruct HIN.
{ (* unchanged part *)
inv HWF.
eapply ptr_wf_set. ss. eapply wf_ptr_mem.
rewrite H0. ss.
eapply firstn_In. ss. eassumption. eassumption.
}
apply List.in_app_or in H.
destruct H.
{ (* changed part! but it's integer. *)
eapply In_ofint_not_ptr_in_byte in H.
eapply H in HPTR. inv HPTR.
}
{ (* unchanged part. *)
inv HWF.
eapply ptr_wf_set. ss. eapply wf_ptr_mem.
rewrite H0. ss.
eapply skipn_In. ss. eassumption. eassumption.
}
{ inv HWF. ss. }
}
{ (* b <> bid *)
rewrite Ir.Memory.get_set_diff_short in HGET.
inv HWF.
eapply ptr_wf_set. ss. eapply wf_ptr_mem.
rewrite HGET. ss. eassumption. eassumption.
inv HWF. ss.
rewrite Nat.eqb_neq in HBID. congruence.
}
inv HWF. ss.
rewrite Nat.eqb_eq in Heq. rewrite <- Heq.
apply Ir.ty_bytesz_pos.
}
{ (* stored value is ptr. *)
unfold Ir.store_bytes in *.
des_ifs; try (inv HWF; eauto; fail).
eapply Ir.get_deref_singleton in Heq0.
destruct Heq0; try congruence.
inv H. inv H0. inv H. simpl in H1.
destruct (b =? bid) eqn:HBID.
{ rewrite Nat.eqb_eq in HBID. subst bid.
erewrite Ir.Memory.get_set_id in HGET.
3: eapply H1. 3: reflexivity.
inv HGET.
unfold Ir.MemBlock.set_bytes in *.
simpl in *.
eapply List.in_app_or in HIN.
destruct HIN.
{ (* unchangd part *)
inv HWF.
eapply ptr_wf_set. ss. eapply wf_ptr_mem.
rewrite H1. ss. eapply firstn_In. ss. eassumption. eassumption.
}
unfold Ir.Byte.ofptr in H.
rewrite Ir.PTRSZ_def in H.
simpl in H.
destruct H.
{ (* changed part 1 *)
(* should use ptr_wf *)
rewrite <- H in HPTR.
unfold Ir.Config.ptr_in_byte in HPTR. simpl in HPTR.
inv HWF. dup HOPVAL.
apply wf_ptr in HOPVAL.
assert (HP0P1:p0 = p1).
{ destruct HPTR. congruence.
repeat (destruct H; try congruence). }
subst p0.
eapply ptr_wf_set. ss. eapply wf_ptr. eassumption.
}
destruct H. (* one more time *)
{
(* should use ptr_wf *)
rewrite <- H in HPTR.
unfold Ir.Config.ptr_in_byte in HPTR. simpl in HPTR.
inv HWF. dup HOPVAL.
apply wf_ptr in HOPVAL.
assert (HP0P1:p0 = p1).
{ destruct HPTR. congruence.
repeat (destruct H; try congruence). }
subst p0.
eapply ptr_wf_set. ss. eapply wf_ptr. eassumption.
}
(* unchanged part *)
inv HWF.
eapply ptr_wf_set. ss. eapply wf_ptr_mem. rewrite H1. ss.
eapply skipn_In. ss. eassumption. eassumption.
inv HWF. ss.
}
{ (* b <> bid *)
rewrite Ir.Memory.get_set_diff_short in HGET.
inv HWF.
eapply ptr_wf_set. ss. eapply wf_ptr_mem.
rewrite HGET. ss. eassumption. eassumption.
inv HWF. ss.
rewrite Nat.eqb_neq in HBID. congruence.
}
inv HWF. ss.
rewrite Nat.eqb_eq in Heq. rewrite <- Heq.
apply Ir.ty_bytesz_pos.
}
Qed.
Lemma free_ptr_wf:
forall c b t p
(HMWF:Ir.Memory.wf (Ir.Config.m c))
(HFREE: Ir.Memory.free (Ir.Config.m c) b = Some t)
(HWF:Ir.Config.ptr_wf p (Ir.Config.m c)),
Ir.Config.ptr_wf p t.
Proof.
intros.
inv HWF.
unfold Ir.Memory.free in HFREE.
des_ifs.
destruct p.
{ exploit H. ss. intros HH. inv HH. inv H2.
split.
{ intros. inv H2. split. ss.
destruct (b =? l) eqn:HEQ.
{ rewrite Nat.eqb_eq in HEQ. subst b.
erewrite Ir.Memory.get_set_id_short. eexists. ss.
eapply Ir.Memory.incr_time_wf. eassumption.
ss.
rewrite Ir.Memory.get_incr_time_id. eassumption.
}
{ rewrite Ir.Memory.get_set_diff_short.
rewrite Ir.Memory.get_incr_time_id. eexists. eassumption.
eapply Ir.Memory.incr_time_wf. eassumption. ss.
rewrite Nat.eqb_neq in HEQ. congruence.
}
}
{ intros. congruence. }
}
{ exploit H0. ss. intros HH.
split.
{ intros. congruence. }
{ intros. inv H1. ss. }
}
Qed.
(* Lemma: inst_det_step preserves well-formedness of configuration. *)
Lemma inst_det_step_wf:
forall md c c' i e
(HWF:Ir.Config.wf md c)
(HCUR:Some i = Ir.Config.cur_inst md c)
(HNEXT:Some (sr_success e c') = inst_det_step md c),
Ir.Config.wf md c'.
Proof.
intros.
unfold inst_det_step in HNEXT. (* ibinop. *)
rewrite <- HCUR in HNEXT.
destruct i as [r retty bopc op1 op2 (* ibinop *)
|r op1 retty (* ifreeze *)
|r opcond condty op1 op2 opty (* iselect *)
|r retty ptrty opptr1 opptr2 (* ipsub *)
|r retty opptr opidx inb (* igep *)
|r retty opptr (* iload *)
|valty opval opptr (* istore *)
|(* imalloc *)
|opptr (* ifree *)
|r opval retty (* ibitcast *)
|r opptr retty (* iptrtoint *)
|r opint retty (* iinttoptr *)
|opval (* ievent *)
|r opty op1 op2 (* iicmp_eq *)
|r opty op1 op2 (* iicmp_ule *)
] eqn:HINST; try (inversion HNEXT; fail).
+ destruct bopc; try_wf.
+ (* ifreeze. *) try_wf.
+ (* iselect. *) try_wf.
eapply update_reg_and_incrpc_wf. eassumption. ss. inv HWF.
intros. destruct v; try discriminate. inv H. eapply wf_ptr. eassumption.
eapply update_reg_and_incrpc_wf. eassumption. ss. inv HWF.
intros. destruct v0; try discriminate. inv H. eapply wf_ptr. eassumption.
+ (* ipsub. *) unfold psub in HNEXT. try_wf.
+ (* igep. *) try_wf.
eapply update_reg_and_incrpc_wf; try reflexivity.
eassumption.
intros. eapply gep_wf; try eassumption.
inv HWF. assumption. inv HWF. eapply wf_ptr; eassumption.
+ (* iload. *) try_wf.
eapply update_reg_and_incrpc_wf. eassumption. reflexivity.
intros. dup HWF. inv HWF.
eapply load_val_ptr_wf. eassumption. eassumption.
eapply wf_ptr. eassumption.
+ (* istore. *) try_wf; try (eapply incrpc_wf; try eassumption; try reflexivity; fail).
apply incrpc_wf with (c := Ir.Config.update_m c (Ir.store_val (Ir.Config.m c) p v valty)).
dup HWF. destruct HWF.
split; simpl; try assumption. eapply Ir.store_val_wf. eassumption.
eapply Ir.ty_bytesz_pos. congruence.
* intros. rewrite Ir.Config.get_val_update_m in HGETVAL.
eapply store_val_ptr_wf. eassumption.
eapply wf_ptr in HGETVAL. ss.
* (* wf_ptr_mem *)
intros.
assert (exists mb', Some mb' = Ir.Memory.get (Ir.Config.m c) bid).
{ unfold Ir.store_val in HBLK. unfold Ir.store_bytes in HBLK.
des_ifs; try (eexists; rewrite HBLK; reflexivity).
eapply Ir.Memory.get_set_exists. eassumption.
eapply Ir.Memory.get_set_exists. eassumption.
}
inv H.
eapply store_val_ptr_mem_wf; try eassumption.
* ss.
+ (* ifree *) try_wf; try (eapply incrpc_wf; try eassumption; try reflexivity; fail).
apply incrpc_wf with (c := Ir.Config.update_m c t); try reflexivity.
unfold free in Heq0.
destruct HWF.
des_ifs.
* split.
-- eapply Ir.Memory.free_wf. eassumption.
rewrite Heq0. unfold Ir.Config.update_m. reflexivity.
-- unfold Ir.Config.cid_to_f in *. des_ifs.
-- intros. apply wf_cid_to_f2. unfold Ir.Config.cid_to_f in *. des_ifs.
-- intros. rewrite Ir.Config.s_update_m in HIN. eauto.
-- intros.
rewrite Ir.Config.get_val_update_m in HGETVAL.
rewrite Ir.Config.m_update_m.
apply wf_ptr in HGETVAL.
eapply free_ptr_wf; eassumption.
-- intros. rewrite Ir.Config.m_update_m in *.
dup HBLK. symmetry in HBLK.
eapply Ir.Memory.get_free_some_inv in HBLK; try eauto.
inv HBLK.
erewrite <- Ir.Memory.get_free_c in HBYTE.
3: eauto. 4: eauto. 3: eauto. 2: eauto.
eapply free_ptr_wf. eassumption. eauto.
eapply wf_ptr_mem. rewrite H. eauto. eauto. eauto.
* split.
-- eapply Ir.Memory.free_wf. eassumption.
rewrite Heq0. unfold Ir.Config.update_m. reflexivity.
-- unfold Ir.Config.cid_to_f in *. des_ifs.
-- intros. apply wf_cid_to_f2. unfold Ir.Config.cid_to_f in *. des_ifs.
-- intros.
apply wf_stack with (curcid := curcid) (funid := funid) (curregfile := curregfile).
assumption. unfold Ir.Config.cid_to_f in *.
unfold Ir.Config.update_m in HIN2. destruct c. simpl in *. assumption.
assumption.
-- intros.
rewrite Ir.Config.get_val_update_m in HGETVAL.
rewrite Ir.Config.m_update_m.
apply wf_ptr in HGETVAL.
eapply free_ptr_wf; eassumption.
-- intros. rewrite Ir.Config.m_update_m in *.
dup HBLK. symmetry in HBLK.
eapply Ir.Memory.get_free_some_inv in HBLK; try eauto.
inv HBLK.
eapply free_ptr_wf. eassumption. eassumption.
eapply wf_ptr_mem.
3: eapply HBIT.
rewrite H. reflexivity.
erewrite <- Ir.Memory.get_free_c in HBYTE. 3:eauto. 4:eauto. 3:eauto. 2:eauto.
eauto.
+ (* ibitcast. *) try_wf.
eapply update_reg_and_incrpc_wf. eassumption.
reflexivity.
intros. inv H.
inv HWF. eapply wf_ptr. eassumption.
+ (* iptrtoint. *) try_wf.
+ (* iinttoptr *) try_wf.
eapply update_reg_and_incrpc_wf; try eassumption.
reflexivity.
intros. inv H.
split. intros. congruence.
intros. inv H.
eapply twos_compl_lt.
+ (* ievent *)
rename HNEXT into H2. simpl in H2.
des_op c opval opv H2. des_inv opv H2.
inversion H2. eapply incrpc_wf. eassumption. reflexivity.
+ (* iicmp_eq, det *)
rename HNEXT into HC'. simpl in HC'.
des_op c op1 op1v HC'.
{ des_ifs;
try (eapply update_reg_and_incrpc_wf;
[ eassumption | ss | intros H1 H2; unfold to_num in H2; congruence ]).
}
{ des_ifs;
try (eapply update_reg_and_incrpc_wf;
[ eassumption | ss | intros H1 H2; unfold to_num in H2; congruence ]).
}
+ (* iicmp_ule, det *)
rename HNEXT into HC'. simpl in HC'.
des_op c op1 op1v HC'.
{ des_inv op1v HC';
des_op c op2 op2v HC'; try (inv HC'; try_wf).
try (eapply update_reg_and_incrpc_wf;
[ eassumption | ss | intros H1 H2; unfold to_num in H2; congruence ]).
try (eapply update_reg_and_incrpc_wf;
[ eassumption | ss | intros H1 H2; unfold to_num in H2; congruence ]).
}
{ des_ifs; try_wf. }
Qed.
Lemma new_ptr_wf:
forall md c p m' nsz contents P l
(HWF:Ir.Config.wf md c)
(HSZ:nsz > 0)
(HGET:Ir.Config.ptr_wf p (Ir.Config.m c))
(HDISJ:Ir.Memory.allocatable (Ir.Config.m c)
(map (fun addr : nat => (addr, nsz)) P) = true)
(HMBWF : forall begt : Ir.time,
Ir.MemBlock.wf
{|
Ir.MemBlock.bt := Ir.heap;
Ir.MemBlock.r := (begt, None);
Ir.MemBlock.n := nsz;
Ir.MemBlock.a := Ir.SYSALIGN;
Ir.MemBlock.c := contents;
Ir.MemBlock.P := P |})
(HNEW:(m', l) = Ir.Memory.new (Ir.Config.m c) Ir.heap nsz Ir.SYSALIGN contents P),
Ir.Config.ptr_wf p m'.
Proof.
intros.
inv HGET.
destruct p.
{ exploit H. ss. intros HH. inv HH. inv H2.
split.
{ intros. inv H2. split. ss.
eapply Ir.Memory.get_new in HNEW. rewrite HNEW. eexists. eassumption.
inv HWF. ss. ss. ss.
ss.
exploit H. ss. intros HH. inv HH.
inv HWF. inv wf_m.
eapply forallb_In in wf_newid.
rewrite Nat.ltb_lt in wf_newid. eassumption.
inv H4. symmetry in H5. eapply Ir.Memory.get_In_key in H5. eassumption.
ss.
}
{ intros. congruence. }
}
{ exploit H0. ss. intros HH.
split.
{ intros. congruence. }
{ intros. inv H1. omega. }
}
Qed.
Lemma poison_not_ptr_in_byte:
forall nsz byt ofs p
(HIN:In byt (repeat Ir.Byte.poison nsz)),
~Ir.Config.ptr_in_byte p ofs byt.
Proof.
intros.
intros HH.
unfold Ir.Config.ptr_in_byte in HH.
assert (byt = Ir.Byte.poison).
{ apply repeat_spec in HIN. subst.
unfold Ir.Byte.poison in *.
simpl in *. reflexivity.
}
subst byt.
unfold Ir.Byte.poison. simpl in *.
repeat (destruct HH as [Ha | HH]; try inv Ha).
inv HH.
Qed.
(* Lemma: inst_step preserves well-formedness of configuration. *)
Lemma inst_step_wf:
forall md c c' e
(HWF:Ir.Config.wf md c)
(HSTEP:inst_step md c (sr_success e c')),
Ir.Config.wf md c'.
Proof.
intros.
inversion HSTEP.
- unfold inst_det_step in HNEXT.
destruct (Ir.Config.cur_inst md c) as [i0|] eqn:Hcur.
eapply inst_det_step_wf. eassumption.
rewrite Hcur. reflexivity. unfold inst_det_step.
rewrite Hcur. eassumption.
inversion HNEXT.
- (* freeze *)
thats_it. intros. ss.
- (* imalloc returning null *)
thats_it. unfold Ir.NULL.
intros. inv H0.
split. intros. congruence.
intros. inv H. apply Ir.MEMSZ_pos.
- (* imalloc, succeed *)
eapply update_reg_and_incrpc_wf with (c := Ir.Config.update_m c m').
+ inversion HWF.
split; try (simpl; assumption).
* simpl. eapply Ir.Memory.new_wf.
eapply wf_m.
eassumption.
eassumption.
eassumption.
* intros.
rewrite Ir.Config.get_val_update_m in HGETVAL.
rewrite Ir.Config.m_update_m.
apply wf_ptr in HGETVAL.
eapply new_ptr_wf; eassumption.
* intros. rewrite Ir.Config.m_update_m in *.
destruct (bid =? l) eqn:HEQ.
{ (* the newly allocated block - all poison. *)
rewrite Nat.eqb_eq in HEQ.
subst.
eapply Ir.Memory.get_new_c_poison in HBLK; try eassumption.
rewrite HBLK in HBYTE.
eapply poison_not_ptr_in_byte in HBYTE.
eapply HBYTE in HBIT. inv HBIT.
}
{ (* old blocks *)
assert (bid < l).
{ eapply Ir.Memory.get_In_key in HBLK; try reflexivity.
assert (wf':Ir.Memory.wf m').
{ eapply Ir.Memory.new_wf. eapply wf_m. eassumption.
ss. eassumption. }
inv wf'.
eapply forallb_In with (i := bid) in wf_newid; try assumption.
unfold Ir.Memory.new in HNEW.
inv HNEW.
simpl in *.
rewrite Nat.ltb_lt in wf_newid.
rewrite Nat.eqb_neq in HEQ.
omega.
}
assert (HBLK':Some mb = Ir.Memory.get (Ir.Config.m c) bid).
{ unfold Ir.Memory.new in HNEW.
inv HNEW.
unfold Ir.Memory.get in HBLK.
simpl in HBLK.
rewrite Nat.eqb_sym in HEQ.
rewrite HEQ in HBLK.
unfold Ir.Memory.get.
ss.
}
exploit wf_ptr_mem.
eapply HBLK'. eassumption. eassumption.
intros HH.
eapply new_ptr_wf; eassumption.
}
+ reflexivity.
+ intros. inv H0.
split.
{ intros. inv H. split. apply Ir.MEMSZ_pos.
unfold Ir.Memory.new in HNEW. inv HNEW.
simpl. unfold Ir.Memory.get. simpl. rewrite Nat.eqb_refl.
eexists. ss.
}
{ intros. congruence. }
- (* iicmp_eq, nondet *)
eapply update_reg_and_incrpc_wf.
eassumption.
reflexivity.
intros. congruence.
- (* icmp_ule, nondet *)
eapply update_reg_and_incrpc_wf. eassumption. reflexivity.
intros. congruence.
Qed.
Lemma phi_step_wf:
forall md c c' bef_bbid
(HWF:Ir.Config.wf md c)
(HSTEP:phi_step md bef_bbid c = Some c'),
Ir.Config.wf md c'.
Proof.
intros.
unfold phi_step in HSTEP.
des_ifs.
eapply update_reg_and_incrpc_wf. eassumption.
ss.
intros.
inv HWF.
eapply wf_ptr. eassumption.
Qed.
Lemma phi_bigstep_wf:
forall md c c' bef_bbid
(HWF:Ir.Config.wf md c)
(HSTEP:phi_bigstep md bef_bbid c c'),
Ir.Config.wf md c'.
Proof.
intros.
induction HSTEP.
{ eapply phi_step_wf; eassumption. }
{ apply IHHSTEP in HWF.
eapply phi_step_wf. eassumption. eassumption.
}
Qed.