-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMutrecAproof.v
815 lines (725 loc) · 28 KB
/
MutrecAproof.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
Require Import CoqlibC Maps Postorder.
Require Import AST Linking.
Require Import ValuesC Memory GlobalenvsC Events Smallstep.
Require Import Op Registers ClightC Renumber.
Require Import CtypesC CtypingC.
Require Import sflib.
Require Import IntegersC.
Require Import MutrecHeader.
Require Import MutrecA MutrecAspec.
Require Import Simulation.
Require Import Skeleton Mod ModSem SimMod SimModSemLift SimSymb SimMemLift AsmregsC MatchSimModSem.
Require SoundTop.
Require SimMemInjC SimMemInjInvC.
Require Import Clightdefs.
Require Import CtypesC.
Set Implicit Arguments.
Definition memoized_inv: SimMemInjInv.memblk_invariant :=
SimMemInjInv.memblk_invarant_mk
(fun mem =>
forall
chunk ofs ind
(BOUND: 0 <= ind < 1000)
(INT: chunk = Mint32)
(INDEX: size_chunk Mint32 * ind = ofs),
exists i,
(<<VINT: mem chunk ofs = Some (Vint i)>>) /\
(<<VAL: forall (NZERO: i.(Int.intval) <> 0),
(<<MEMO: i = sum (Int.repr ind)>>)>>))
(fun chunk ofs p =>
exists ind,
(<<CHUNK: chunk = Mint32>>) /\ (<<BOUND: 0 <= ind < 1000>>) /\
(<<INDEX: ofs = size_chunk Mint32 * ind>>) /\ (<<WRITABLE: p = Writable>>)).
Local Instance SimMemMemoizedA: SimMem.class := SimMemInjInvC.SimMemInjInv
SimMemInjInv.top_inv memoized_inv.
Definition symbol_memoized: ident -> Prop := eq _memoized.
Lemma memoized_inv_store_le i ind blk ofs m_tgt
(sm0 sm1: SimMemInjInv.t')
(MWF: SimMem.wf sm0)
(INVAR: sm0.(SimMemInjInv.mem_inv_tgt) blk)
(SUM: i = sum (Int.repr ind))
(OFS: ofs = size_chunk Mint32 * ind)
(STR: Mem.store Mint32 sm0.(SimMemInjInv.minj).(SimMemInj.tgt) blk ofs (Vint i) = Some m_tgt)
(MREL: sm1 = SimMemInjInv.mk
(SimMemInjC.update
(sm0.(SimMemInjInv.minj))
(sm0.(SimMemInjInv.minj).(SimMemInj.src))
m_tgt
(sm0.(SimMemInjInv.minj).(SimMemInj.inj)))
sm0.(SimMemInjInv.mem_inv_src)
sm0.(SimMemInjInv.mem_inv_tgt))
:
(<<MLE: SimMem.le sm0 sm1>>) /\
(<<MWF: SimMem.wf sm1>>).
Proof.
inv MWF. split.
- econs; ss; eauto. econs; ss; eauto.
+ refl.
+ eapply Mem.store_unchanged_on; eauto.
ii. inv WF. exploit INVRANGETGT; eauto. i. des.
exfalso. eauto.
+ eapply SimMemInj.frozen_refl. + eapply SimMemInj.frozen_refl.
+ ii. eapply Mem.perm_store_2; eauto.
- inv WF. econs; ss; eauto.
+ unfold SimMemInjC.update. econs; ss; eauto.
* eapply MemoryC.private_unchanged_inject; eauto.
{ eapply Mem.store_unchanged_on; eauto.
instantiate (1:=~2
loc_out_of_reach (SimMemInj.inj (SimMemInjInv.minj sm0))
(SimMemInj.src (SimMemInjInv.minj sm0))).
ss. ii. eapply H0.
exploit INVRANGETGT; eauto. i. des. eapply H1. }
{ ss. }
* etrans; eauto.
unfold SimMemInj.tgt_private, SimMemInj.valid_blocks in *. ss.
ii. des. split; auto. eapply Mem.store_valid_block_1; eauto.
* rpapply TGTLE. eapply Mem.nextblock_store; eauto.
+ ii. exploit SATTGT; eauto. i. inv H. econs; ss.
* i. exploit PERMISSIONS; eauto. i.
eapply Mem.store_valid_access_1; eauto.
* i. exploit LOADVALS; eauto. i. des. destruct (peq blk blk0).
{ clarify. destruct (zeq ind ind0).
- clarify. exists (sum (Int.repr ind0)).
esplits; eauto. erewrite Mem.load_store_same; eauto. ss.
- exists i. erewrite Mem.load_store_other; eauto.
right. clear - n. ss. omega. }
{ exists i. erewrite Mem.load_store_other; eauto. }
Qed.
Section SIMMODSEM.
Variable skenv_link: SkEnv.t.
Variable sm_link: SimMem.t.
Let md_src: Mod.t := (MutrecAspec.module).
Let md_tgt: Mod.t := (ClightC.module2 prog).
Hypothesis (INCL: SkEnv.includes skenv_link (Mod.sk md_src)).
Hypothesis (WF: SkEnv.wf skenv_link).
Let ge := (SkEnv.project skenv_link (Mod.sk md_src)).
Let tge := Build_genv (SkEnv.revive (SkEnv.project skenv_link (Mod.sk md_tgt)) prog) prog.(prog_comp_env).
Definition msp: ModSemPair.t :=
ModSemPair.mk (md_src skenv_link) (md_tgt skenv_link) (SimMemInjInvC.mk symbol_memoized md_src md_tgt) sm_link.
Inductive match_states_internal: nat -> MutrecAspec.state -> Clight.state -> Prop :=
| match_callstate_nonzero
idx i m_src m_tgt
fptr
(RANGE: 0 <= i.(Int.intval) < MAX)
(FINDF: Genv.find_funct (Smallstep.globalenv (modsem2 skenv_link prog)) fptr = Some (Internal func_f))
(IDX: (idx > 3)%nat)
:
match_states_internal idx (Callstate i m_src) (Clight.Callstate fptr (Tfunction
(Tcons tint Tnil) tint cc_default)
[Vint i] Kstop m_tgt)
| match_returnstate
idx i m_src m_tgt
:
match_states_internal idx (Returnstate i m_src) (Clight.Returnstate (Vint i) Kstop m_tgt)
.
Inductive match_states
(idx: nat) (st_src0: MutrecAspec.state) (st_tgt0: Clight.state) (sm0: SimMem.t): Prop :=
| match_states_intro
(MATCHST: match_states_internal idx st_src0 st_tgt0)
(MCOMPATSRC: (get_mem st_src0) = sm0.(SimMem.src))
(MCOMPATTGT: (ClightC.get_mem st_tgt0) = sm0.(SimMem.tgt))
(MWF: SimMem.wf sm0)
.
Lemma g_blk_exists
:
exists g_blk,
(<<FINDG: Genv.find_symbol
(SkEnv.revive (SkEnv.project skenv_link (CSk.of_program signature_of_function prog)) prog)
g_id = Some g_blk>>)
/\
(<<FINDG: Genv.find_funct_ptr
(SkEnv.revive (SkEnv.project skenv_link (CSk.of_program signature_of_function prog)) prog)
g_blk = None>>)
/\
(<<FINDG: exists skd, Genv.find_funct_ptr skenv_link g_blk = Some skd /\
Some (signature_of_type (Tcons tint Tnil) tint cc_default) = Sk.get_csig skd>>)
.
Proof.
exploit (prog_defmap_norepet prog g_id); eauto.
{ unfold prog_defs_names. ss. repeat (econs; eauto).
- ii; ss; des; ss.
- ii; ss; des; ss. }
{ ss. eauto. }
intro T; des.
exploit SkEnv.project_impl_spec; eauto. intro PROJ.
assert(PREC: SkEnv.genv_precise
(SkEnv.revive (SkEnv.project skenv_link (CSk.of_program signature_of_function prog)) prog)
prog).
{ eapply CSkEnv.project_revive_precise; ss; et. }
inv PREC.
exploit (P2GE g_id); eauto. i; des. des_ifs.
rename b into g_blk.
eexists. splits; et.
{ unfold Genv.find_funct_ptr. des_ifs. }
{ inv INCL.
exploit (CSk.of_program_prog_defmap prog signature_of_function); et. rewrite T. intro S.
remember ((prog_defmap (CSk.of_program signature_of_function prog)) ! g_id) as U in *.
destruct U eqn:V; try (by ss). inv S. inv H1.
exploit DEFS; eauto. i; des.
assert(blk = g_blk).
{ inv PROJ. exploit SYMBKEEP; et.
- instantiate (1:= g_id). unfold defs. des_sumbool. ss. et.
- i. rewrite SYMB0 in *. clear - SYMB H. unfold SkEnv.revive in *. rewrite Genv_map_defs_symb in *. ss.
rewrite SYMB in *. des. clarify.
}
clarify. inv MATCH.
esplits; eauto.
- unfold Genv.find_funct_ptr. rewrite DEF0. et.
- ss. des_ifs. clear - H1. inv H1; ss.
}
Qed.
Lemma match_states_lxsim
idx st_src0 st_tgt0 sm0
(SIMSK: SimSymb.sim_skenv
sm0 (SimMemInjInvC.mk symbol_memoized md_src md_tgt)
(SkEnv.project skenv_link (CSk.of_program signature_of_function prog))
(SkEnv.project skenv_link (CSk.of_program signature_of_function prog)))
(MATCH: match_states idx st_src0 st_tgt0 sm0)
:
<<XSIM: lxsimL (md_src skenv_link) (md_tgt skenv_link)
(fun st => unit -> exists su m_init, SoundTop.sound_state su m_init st)
top3 (fun _ _ => SimMem.le)
(Ord.lift_idx lt_wf idx) st_src0 st_tgt0 sm0>>
.
Proof.
revert_until tge.
pcofix CIH.
i.
pfold.
generalize g_blk_exists; et. i; des.
inv MATCH; subst. ss. inv MATCHST; ss; clarify.
- (* call *)
destruct (classic (i = Int.zero)).
+ (* zero *)
clarify.
econs 2. ii.
econs 2.
{ split.
- econs 2.
+ ss. econs 1.
+ econs 1.
+ ss.
- eapply Ord.lift_idx_spec.
instantiate (1:=3%nat). nia. }
refl.
left. pfold.
econs 1. i; des.
econs 2.
* split; cycle 1.
{ apply Ord.lift_idx_spec.
instantiate (1:=2%nat). nia. }
eapply plus_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
econs; ss; eauto; try (by repeat (econs; ss; eauto)).
unfold _x. unfold _t'1. rr. ii; ss. des; ss; clarify.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- repeat econs; et.
- ss.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- repeat econs; et.
- ss.
- ss.
}
apply star_refl.
* refl.
* right. eapply CIH; eauto. econs; ss; eauto.
replace (Int.repr 0) with (sum Int.zero).
{ econs; eauto. }
{ rewrite sum_recurse. des_ifs. }
+ (* nonzero *)
destruct (Genv.find_symbol
(SkEnv.project skenv_link (CSk.of_program signature_of_function prog))
_memoized) eqn:BLK; cycle 1.
{ exfalso. clear - INCL BLK. inversion INCL; subst.
exploit DEFS; eauto.
- instantiate (2:=_memoized). ss.
- i. des.
exploit SkEnv.project_impl_spec. eapply INCL. i. inv H. ss.
exploit SYMBKEEP. instantiate (1:=_memoized). ss. i.
rr in H. rewrite H in *. clarify. }
inv MWF. ss.
assert (INVAR: SimMemInjInv.mem_inv_tgt sm0 b).
{ inv SIMSK. ss. inv INJECT.
eapply INVCOMPAT; eauto. ss. }
hexploit SATTGT; eauto. intros SAT0.
exploit SAT0; eauto. i. inv H0. ss.
hexploit LOADVALS; eauto. i. des.
destruct (zeq (Int.intval i0) 0).
{
econs 2. ii.
econs 2.
{ split.
- econs 2; ss.
+ econs 2; eauto.
clear - H.
exploit Int.eq_false; eauto. i.
unfold Int.eq in *. ss. des_ifs.
+ econs; eauto.
+ ss.
- eapply Ord.lift_idx_spec. eauto. }
refl.
left. pfold.
econs.
i; des.
econs 2; eauto.
* esplits; cycle 1.
{ eapply Ord.lift_idx_spec. eauto. }
eapply plus_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
econs; ss; eauto; try (by repeat (econs; ss; eauto)).
unfold _x. unfold _t'1. rr. ii; ss. des; ss; clarify.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- repeat econs; et.
- ss. rewrite Int.eq_false; ss.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto; swap 1 2.
- econs.
+ ss. econs. econs; ss.
* econs.
{ eapply eval_Evar_global; ss.
instantiate (1:=b). eauto. }
{ econs 2; ss. }
* econs; ss.
* ss.
+ econs 1; ss. psimpl.
replace (Ptrofs.unsigned (Ptrofs.mul (Ptrofs.repr 4) (Ptrofs.of_ints i)))
with (4 * Int.intval i); cycle 1.
{ unfold Ptrofs.mul. ss.
destruct i. ss. unfold Ptrofs.of_ints. ss.
unfold Int.signed. ss. des_ifs; cycle 1;
unfold Int.half_modulus, Int.modulus, two_power_nat in *; ss;
unfold MAX in *; rewrite <- Zdiv2_div in *; ss.
{ lia. }
repeat rewrite Ptrofs.unsigned_repr. auto.
all : unfold Ptrofs.max_unsigned; rewrite Ptrofs.modulus_power;
unfold Ptrofs.zwordsize, Ptrofs.wordsize, Wordsize_Ptrofs.wordsize; des_ifs; ss; omega. } eauto. }
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
ss. econs; eauto.
- econs; ss.
+ econs; ss.
+ econs; ss.
+ ss.
- ss. instantiate (1:=true).
unfold Cop.bool_val. ss.
unfold Int.eq. unfold Val.of_bool.
destruct (zeq (Int.unsigned i0) (Int.unsigned (Int.repr 0))) eqn:EQ; ss. }
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto; swap 1 2.
- econs.
+ eapply eval_Evar_global; ss. et.
+ econs 2; et.
- unfold Cop.classify_fun. ss.
- repeat econs; ss; et.
}
eapply star_refl.
* refl.
* left. pfold. econs 3; et.
{ econs; eauto. }
{ econs. econs; eauto. }
ii; des.
inv ATSRC. ss; clarify.
unfold Clight.fundef in *.
assert(g_fptr = g_blk).
{ unfold SkEnv.revive in FINDG. rewrite Genv_map_defs_symb in *. clarify. }
clarify.
eexists (Args.mk _ [Vint (Int.sub i (Int.repr 1))] _).
exists sm0.
esplits; ss; eauto.
{ econs; ss; eauto.
instantiate (1:=Vptr g_blk Ptrofs.zero).
inv SIMSK. inv SIMSKENV. inv INJECT. ss.
econs. eapply DOMAIN; eauto.
exploit Genv.genv_symb_range. unfold Genv.find_symbol in *. eauto.
i. ss. ii.
exploit INVCOMPAT; eauto. i. rewrite <- H1 in H0. ss.
rewrite Ptrofs.add_zero_l. ss. }
{ refl. }
{ econs; eauto. }
i. inv AFTERSRC. inv SIMRETV; ss; clarify.
hexploit Mem.valid_access_store.
{ instantiate (4:=sm_ret.(SimMemInjInv.minj).(SimMemInj.tgt)).
inv MWF. inv WF. exploit SATTGT0; eauto.
- inv MLE. erewrite <- MINVEQTGT. eauto.
- i. inv H0. hexploit PERMISSIONS0; eauto. ss.
esplits; eauto. }
intros [m_tgt STR].
exploit SimMemInjInvC.unlift_wf; try apply MLE; eauto.
{ econs; eauto. } intros MLE1.
exploit memoized_inv_store_le; eauto.
i. des.
esplits.
{ econs; eauto. }
{ apply MLE0. }
left. pfold. econs; eauto. i; des. econs 2; eauto.
{
esplits; eauto; cycle 1.
{ instantiate (1:= (Ord.lift_idx lt_wf 14%nat)). eapply Ord.lift_idx_spec; et. }
eapply plus_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto. econs; eauto.
- econs; eauto. ss.
- econs; eauto. ss.
- inv RETV. ss. unfold typify. des_ifs. }
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- econs; eauto. econs; eauto.
+ econs; eauto.
* eapply eval_Evar_global; ss.
instantiate (1:=b). ss.
* ss. econs 2; eauto.
+ econs; eauto. ss.
+ econs; eauto.
- econs; eauto. ss.
- ss.
- ss. psimpl. econs; ss; eauto.
rpapply STR. f_equal.
+ unfold Ptrofs.mul. ss.
destruct i. ss. unfold Ptrofs.of_ints. ss.
unfold Int.signed. ss. des_ifs; cycle 1;
unfold Int.half_modulus, Int.modulus, two_power_nat in *; ss;
unfold MAX in *; rewrite <- Zdiv2_div in *; ss.
{ lia. }
repeat rewrite Ptrofs.unsigned_repr. auto.
all : unfold Ptrofs.max_unsigned; rewrite Ptrofs.modulus_power;
unfold Ptrofs.zwordsize, Ptrofs.wordsize, Wordsize_Ptrofs.wordsize; des_ifs; ss; omega.
+ f_equal.
rewrite Int.repr_unsigned.
rewrite sum_recurse with (i := i). des_ifs.
rewrite Z.eqb_eq in Heq.
exploit Int.eq_spec. instantiate (1:=i). instantiate (1:=Int.zero).
unfold Int.eq. unfold Int.unsigned. rewrite Heq. des_ifs. i. subst i.
rewrite Int.sub_zero_r. rewrite sum_recurse. des_ifs. }
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- econs; eauto. ss.
- econs; eauto.
- econs; eauto. }
eapply star_refl.
}
{ refl. }
right. eapply CIH.
{ eapply SimMemInjInvC.sim_skenv_inj_lepriv; cycle 1; eauto.
etrans; eauto.
{ exploit (SimMemLift.lift_priv sm0); eauto. ss. }
etrans; eauto; cycle 1.
{ hexploit SimMem.pub_priv; try apply MLE0. eauto. }
etrans; eauto.
{ hexploit SimMem.pub_priv; try apply MLE; eauto. }
hexploit SimMemLift.unlift_priv; revgoals.
{ intro T. ss. eauto. }
{ eauto. }
{ eauto. }
{ exploit (SimMemLift.lift_priv sm0); eauto. ss. }
{ econs; eauto. } }
{ econs; ss.
- replace (Int.add (sum (Int.sub i Int.one)) i) with (sum i); cycle 1.
{ rewrite sum_recurse with (i := i). des_ifs.
rewrite Z.eqb_eq in Heq.
exploit Int.eq_spec. instantiate (1:=i). instantiate (1:=Int.zero).
unfold Int.eq. unfold Int.unsigned. rewrite Heq. des_ifs. i. subst i.
rewrite Int.sub_zero_r. rewrite sum_recurse. des_ifs. }
econs 2.
}
}
{ hexploit VAL; eauto. i. des. clarify.
econs 2. ii.
econs 2.
{ split.
- econs 2; ss.
+ econs; eauto.
+ econs; eauto.
+ ss.
- eapply Ord.lift_idx_spec. eauto. }
refl.
left. pfold.
econs.
i; des.
econs 2; eauto.
* esplits; cycle 1.
{ eapply Ord.lift_idx_spec. eauto. }
eapply plus_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
econs; ss; eauto; try (by repeat (econs; ss; eauto)).
unfold _x. unfold _t'1. rr. ii; ss. des; ss; clarify.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- repeat econs; et.
- ss. rewrite Int.eq_false; ss.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto; swap 1 2.
- econs.
+ ss. econs. econs; ss.
* econs.
{ eapply eval_Evar_global; ss.
instantiate (1:=b). ss. }
{ econs 2; ss. }
* econs; ss.
* ss.
+ econs 1; ss. psimpl.
replace (Ptrofs.unsigned (Ptrofs.mul (Ptrofs.repr 4) (Ptrofs.of_ints i)))
with (4 * Int.intval i); cycle 1.
{ unfold Ptrofs.mul. ss.
destruct i. ss. unfold Ptrofs.of_ints. ss.
unfold Int.signed. ss. des_ifs; cycle 1;
unfold Int.half_modulus, Int.modulus, two_power_nat in *; ss;
unfold MAX in *; rewrite <- Zdiv2_div in *; ss.
{ lia. }
repeat rewrite Ptrofs.unsigned_repr. auto.
all : unfold Ptrofs.max_unsigned; rewrite Ptrofs.modulus_power;
unfold Ptrofs.zwordsize, Ptrofs.wordsize, Wordsize_Ptrofs.wordsize; des_ifs; ss; omega. } eauto. }
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
ss. econs; eauto.
- econs; ss.
+ econs; ss.
+ econs; ss.
+ ss.
- ss. instantiate (1:=false).
unfold Cop.bool_val. ss.
unfold Int.eq. unfold Val.of_bool.
destruct (zeq (Int.unsigned (sum (Int.repr (Int.intval i))))
(Int.unsigned (Int.repr 0))) eqn:EQ; ss. }
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
}
eapply star_left with (t1 := E0) (t2 := E0); ss.
{ econs; eauto.
{ eapply modsem2_determinate; eauto. }
econs; eauto.
- econs; ss.
- econs.
- ss. }
apply star_refl.
* refl.
* right. eapply CIH; eauto.
{ econs; eauto.
- ss. replace (Int.repr (Int.intval i)) with i.
+ econs; eauto.
+ symmetry. eapply Int.eqm_repr_eq.
eapply Int.eqm_refl2. ss.
- econs; eauto. }
}
- (* return *)
econs 4; ss; eauto.
+ refl.
+ econs; ss; eauto.
Qed.
Theorem sim_modsem
:
ModSemPair.sim msp
.
Proof.
eapply sim_mod_sem_implies.
eapply ModSemPair.simL_intro with (has_footprint := top3) (mle_excl := fun _ _ => SimMem.le).
{ i. eapply SoundTop.sound_state_local_preservation. }
{ i. eapply Preservation.local_preservation_noguarantee_weak; eauto. eapply SoundTop.sound_state_local_preservation. }
{ ii; ss. r. etrans; eauto. }
{ ii. eauto. }
i. ss. esplits; eauto.
- i. des. inv SAFESRC.
esplits; eauto.
+ refl.
+ econs; eauto.
+ instantiate (1:= (Ord.lift_idx lt_wf 15%nat)).
inv INITTGT. inv TYP. ss.
assert (FD: fd = func_f).
{ destruct args_src, args_tgt; ss. clarify.
inv SIMARGS; ss. clarify. inv VALS. inv H1. inv H3. inv FPTR. ss.
des_ifs.
inv SIMSKENV. ss. inv SIMSKE. ss. inv INJECT. ss.
exploit IMAGE; eauto.
{ exploit Genv.genv_symb_range.
unfold Genv.find_symbol in SYMB. eauto. i. ss. eauto. }
ii. des. subst. clarify.
rewrite Genv.find_funct_ptr_iff in FINDF.
unfold Genv.find_def in FINDF. ss.
do 2 rewrite MapsC.PTree_filter_map_spec, o_bind_ignore in *.
des_ifs.
destruct (Genv.invert_symbol
(SkEnv.project skenv_link (CSk.of_program signature_of_function prog)) b2) eqn:SKENVSYMB; ss.
unfold o_bind in FINDF. ss.
exploit Genv.find_invert_symbol. eauto. i.
rewrite H in *. clarify.
destruct ((prog_defmap prog) ! f_id) eqn:DMAP; ss. clarify. } clarify.
inv SIMARGS; ss. rewrite VS in *. inv VALS.
inv H3. inv H1.
unfold typify_list, zip, typify. ss. des_ifs; ss.
eapply match_states_lxsim; ss.
* inv SIMSKENV; eauto.
* econs; eauto.
{ econs; eauto. omega. }
- (* init progress *)
i.
des. inv SAFESRC.
inv SIMARGS; ss.
esplits; eauto. econs; eauto.
+ instantiate (1:= func_f).
ss.
inv VALS; ss. inv H1. inv H0. inv FPTR0. ss.
des_ifs.
inv SIMSKENV. ss. inv SIMSKE. ss. inv INJECT. ss.
exploit IMAGE; eauto.
{ exploit Genv.genv_symb_range.
unfold Genv.find_symbol in SYMB. eauto. i. ss. eauto. }
ii. des. subst. clarify.
rewrite Genv.find_funct_ptr_iff in *.
unfold Genv.find_def in *; ss.
do 2 rewrite MapsC.PTree_filter_map_spec, o_bind_ignore in *.
des_ifs.
exploit Genv.find_invert_symbol. eauto. i.
rewrite H0 in *. clarify.
+ econs; ss. erewrite <- inject_list_length; eauto.
rewrite VS. auto.
Qed.
End SIMMODSEM.
Theorem sim_mod
:
ModPair.sim (ModPair.mk (MutrecAspec.module) (ClightC.module2 prog) (SimMemInjInvC.mk symbol_memoized (MutrecAspec.module) (ClightC.module2 prog)))
.
Proof.
econs; ss.
- econs; ss.
+ i. inv SS. esplits; ss; eauto.
* econs; ss.
ii. des. econs.
{ ii. ss. des. clarify. econs; ss.
- ii. eapply PERM; eauto. unfold MAX in *. lia.
- eapply Z.divide_factor_l. }
{ ss. i. clarify. erewrite INIT; ss; eauto.
- esplits; eauto. i. rewrite sum_recurse. des_ifs.
- lia.
- unfold MAX. lia.
- eapply Z.divide_factor_l. }
* ii. des; clarify.
+ ii. destruct H. eapply in_prog_defmap in PROG.
ss. unfold update_snd in PROG. ss.
des; clarify; inv DROP; ss.
des; clarify.
- ii. ss.
inv SIMSKENVLINK. inv SIMSKENV.
eapply sim_modsem; eauto.
Qed.