-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniCET_Index.v
More file actions
4072 lines (3790 loc) · 163 KB
/
MiniCET_Index.v
File metadata and controls
4072 lines (3790 loc) · 163 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From Stdlib Require Import Strings.String.
From SECF Require Import Utils.
From SECF Require Import MiniCET.
From Stdlib Require Import Bool.Bool.
From Stdlib Require Import Arith.Arith.
From Stdlib Require Import Arith.EqNat.
From Stdlib Require Import Arith.PeanoNat. Import Nat.
From Stdlib Require Import Lia.
From Stdlib Require Import List. Import ListNotations.
Require Import ExtLib.Data.Monads.OptionMonad.
From SECF Require Import Maps.
From SECF Require Import MapsFunctor.
From SECF Require Import sflib.
Set Default Goal Selector "!".
Module MCC := MiniCETCommon(TotalMap).
Import MCC.
Import FunctionalExtensionality.
Notation t_update_eq := TotalMap.t_update_eq.
Notation t_update_neq := TotalMap.t_update_neq.
Reserved Notation
"p '|-' '<((' c '))>' '-->^' os '<((' ct '))>'"
(at level 40, c constr, ct constr).
Inductive seq_eval_small_step_inst (p:prog) :
@state cfg -> @state cfg -> obs -> Prop :=
| SSMI_Skip : forall pc rs m stk,
p[[pc]] = Some <{{ skip }}> ->
p |- <(( S_Running (pc, rs, m, stk) ))> -->^[] <(( S_Running (pc+1, rs, m, stk) ))>
| SSMI_Asgn : forall pc r m sk e x,
p[[pc]] = Some <{{ x := e }}> ->
p |- <(( S_Running (pc, r, m, sk) ))> -->^[] <(( S_Running (pc+1, (x !-> (eval r e); r), m, sk) ))>
| SSMI_Div : forall pc r m sk e1 e2 x v1 v2,
p[[pc]] = Some <{{ x <- div e1, e2 }}> ->
to_nat (eval r e1) = Some v1 ->
to_nat (eval r e2) = Some v2 ->
let res := match v2 with
| 0 => UV
| _ => N (div v1 v2)
end
in
p |- <(( S_Running (pc, r, m, sk) ))> -->^[ODiv v1 v2] <(( S_Running (pc+1, (x !-> res; r), m, sk) ))>
| SSMI_Branch : forall pc pc' r m sk e n l,
p[[pc]] = Some <{{ branch e to l }}> ->
to_nat (eval r e) = Some n ->
pc' = (if (not_zero n) then (l,0) else pc+1) ->
p |- <(( S_Running (pc, r, m, sk) ))> -->^[OBranch (not_zero n)] <(( S_Running (pc', r, m, sk) ))>
| SSMI_Jump : forall l pc r m sk,
p[[pc]] = Some <{{ jump l }}> ->
p |- <(( S_Running (pc, r, m, sk) ))> -->^[] <(( S_Running ((l,0), r, m, sk) ))>
| SSMI_Load : forall pc r m sk x e n v',
p[[pc]] = Some <{{ x <- load[e] }}> ->
to_nat (eval r e) = Some n ->
nth_error m n = Some v' ->
p |- <(( S_Running (pc, r, m, sk) ))> -->^[OLoad n] <(( S_Running (pc+1, (x !-> v'; r), m, sk) ))>
| SSMI_Store : forall pc r m sk e e' n,
p[[pc]] = Some <{{ store[e] <- e' }}> ->
to_nat (eval r e) = Some n ->
p |- <(( S_Running (pc, r, m, sk) ))> -->^[OStore n] <(( S_Running (pc+1, r, upd n m (eval r e'), sk) ))>
| SSMI_Call : forall pc r m sk e l,
p[[pc]] = Some <{{ call e }}> ->
to_fp (eval r e) = Some l ->
p |- <(( S_Running (pc, r, m, sk) ))> -->^[OCall l] <(( S_Running (l, r, m, ((pc+1)::sk)) ))>
| SSMI_Ret : forall pc r m sk pc',
p[[pc]] = Some <{{ ret }}> ->
p |- <(( S_Running (pc, r, m, pc'::sk) ))> -->^[] <(( S_Running (pc', r, m, sk) ))>
| SSMI_Peek : forall pc r m sk x, (* YH: Maybe we can assume source program does not contain peek instruction. *)
p[[pc]] = Some <{{x <- peek}}> ->
let val := match sk with
| [] => UV
| pc' :: sk' => FP pc'
end
in
p |- <(( S_Running (pc, r, m, sk) ))> -->^[] <(( S_Running (pc+1, (x !-> val; r), m, sk) ))>
| SSMI_Term : forall pc r m,
p[[pc]] = Some <{{ ret }}> ->
p |- <(( S_Running (pc, r, m, []) ))> -->^[] <(( S_Term ))>
where "p |- <(( c ))> -->^ os <(( ct ))>" :=
(seq_eval_small_step_inst p c ct os).
Reserved Notation
"p '|-' '<((' c '))>' '-->*^' os '<((' ct '))>'"
(at level 40, c constr, ct constr).
Inductive multi_seq_inst (p : prog) (c : @state cfg) : @state cfg -> obs -> Prop :=
| multi_seq_inst_refl : p |- <(( c ))> -->*^[] <(( c ))>
| multi_seq_inst_trans (c' c'' : @state cfg) (os1 os2 : obs) :
p |- <(( c ))> -->^os1 <(( c' ))> ->
p |- <(( c' ))> -->*^os2 <(( c'' ))> ->
p |- <(( c ))> -->*^(os1 ++ os2) <(( c'' ))>
where "p |- <(( c ))> -->*^ os <(( ct ))>" :=
(multi_seq_inst p c ct os).
Definition wf_ret (p: prog) (pc: cptr) : Prop :=
let '(l, o) := pc in
p[[(l, o)]] <> None /\ exists e, p[[(l, o - 1)]] = Some <{{ call e }}> /\ o > 0.
Definition wf_retb (p: prog) (pc: cptr) : bool :=
let '(l, o) := pc in
match p[[(l, o)]] with
| Some _ => match o with
| 0 => false
| S o' => match p[[(l, o')]] with
| Some (ICall e) => true
| _ => false
end
end
| _ => false
end.
Lemma wf_ret_wf_retb p pc:
wf_ret p pc <-> wf_retb p pc = true.
Proof.
destruct pc as [l o]. ss. split; i.
- des_ifs; des; ss; try lia; clarify.
all : rewrite sub_0_r in *; clarify.
- des_ifs; des; ss; try lia; clarify.
rewrite sub_0_r. esplits; eauto; ii; clarify. lia.
Qed.
Definition wf_stk (p: prog) (stk: list cptr) : Prop :=
Forall (fun pc => wf_ret p pc) stk.
Definition call_return_targets (p: prog) : list cptr :=
let ip := add_index p in
List.concat
(List.map (fun '(l, (bl, _)) =>
List.flat_map (fun '(o, i) => match i with
| ICall _ => [(l, add o 1)]
| _ => []
end) (add_index bl)) ip).
Reserved Notation
"p '|-' '<((' sc '))>' '-->_' ds '^^' os '<((' sct '))>'"
(at level 40, sc constr, sct constr).
Inductive spec_eval_small_step (p:prog):
@state spec_cfg -> @state spec_cfg -> dirs -> obs -> Prop :=
| SpecSMI_Skip : forall pc r m sk ms,
p[[pc]] = Some <{{ skip }}> ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[]^^[] <(( S_Running ((pc+1, r, m, sk), false, ms) ))>
| SpecSMI_Asgn : forall pc r m sk ms e x,
p[[pc]] = Some <{{ x := e }}> ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[]^^[] <(( S_Running ((pc+1, (x !-> (eval r e); r), m, sk), false, ms) ))>
| SpecSMI_Div : forall pc r m sk ms e1 e2 x v1 v2,
p[[pc]] = Some <{{ x <- div e1, e2 }}> ->
to_nat (eval r e1) = Some v1 ->
to_nat (eval r e2) = Some v2 ->
let res := match v2 with
| 0 => UV
| _ => N (div v1 v2)
end
in
p |- <(( S_Running (pc, r, m, sk, false, ms) ))> -->_[]^^[ODiv v1 v2] <(( S_Running (pc+1, (x !-> res; r), m, sk, false, ms) ))>
| SpecSMI_Branch : forall pc pc' r m sk ms ms' b (b': bool) e n l,
p[[pc]] = Some <{{ branch e to l }}> ->
to_nat (eval r e) = Some n ->
b = (not_zero n) ->
pc' = (if b' then (l, 0) else (pc+1)) ->
ms' = ms || negb (Bool.eqb b b') ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[DBranch b']^^[OBranch b] <(( S_Running ((pc', r, m, sk), false, ms') ))>
| SpecSMI_Jump : forall l pc r m sk ms,
p[[pc]] = Some <{{ jump l }}> ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[]^^[] <(( S_Running (((l,0), r, m, sk), false, ms) ))>
| SpecSMI_Load : forall pc r m sk x e n v' ms,
p[[pc]] = Some <{{ x <- load[e] }}> ->
to_nat (eval r e) = Some n ->
nth_error m n = Some v' ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[]^^[OLoad n] <(( S_Running ((pc+1, (x !-> v'; r), m, sk), false, ms) ))>
| SpecSMI_Store : forall pc r m sk e e' n ms,
p[[pc]] = Some <{{ store[e] <- e' }}> ->
to_nat (eval r e) = Some n ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[]^^[OStore n] <(( S_Running ((pc+1, r, upd n m (eval r e'), sk), false, ms) ))>
| SpecSMI_Call : forall pc pc' r m sk e l ms ms',
p[[pc]] = Some <{{ call e }}> ->
to_fp (eval r e) = Some l ->
ms' = ms || negb ((fst pc' =? fst l)%nat && (snd pc' =? snd l)%nat) ->
p |- <(( S_Running ((pc, r, m, sk), false, ms) ))> -->_[DCall pc']^^[OCall l] <(( S_Running ((pc', r, m, (pc + 1)::sk), true, ms') ))>
| SpecSMI_CTarget : forall pc r m sk ct ms,
p[[pc]] = Some <{{ ctarget }}> ->
p |- <(( S_Running ((pc, r, m, sk), ct, ms) ))> -->_[]^^[] <(( S_Running ((pc+1, r, m, sk), false, ms) ))>
| SpecSMI_Ret : forall pc r m sk pc' pc'' ms ms',
p[[pc]] = Some <{{ ret }}> ->
wf_ret p pc'' ->
ms' = ms || negb ((fst pc' =? fst pc'')%nat && (snd pc' =? snd pc'')%nat) ->
p |- <(( S_Running ((pc, r, m, pc'::sk), false, ms) ))> -->_[DRet pc'']^^[] <(( S_Running ((pc'', r, m, sk), false, ms') ))>
| SpecSMI_Peek : forall pc r m sk ms x,
p[[pc]] = Some <{{x <- peek}}> ->
let val := match sk with
| [] => UV
| pc' :: sk' => FP pc'
end
in
p |- <(( S_Running (pc, r, m, sk, false, ms) ))> -->_[]^^[] <(( S_Running (pc+1, (x !-> val; r), m, sk, false, ms) ))>
| SpecSMI_Term : forall pc r m ms,
p[[pc]] = Some <{{ ret }}> ->
p |- <(( S_Running ((pc, r, m, []), false, ms) ))> -->_[]^^[] <(( S_Term ))>
| SpecSMI_Fault : forall pc r m sk ms,
p[[pc]] <> Some <{{ ctarget }}> ->
p |- <(( S_Running ((pc, r, m, sk), true, ms) ))> -->_[]^^[] <(( S_Fault ))>
where "p |- <(( sc ))> -->_ ds ^^ os <(( sct ))>" :=
(spec_eval_small_step p sc sct ds os).
Reserved Notation
"p '|-' '<((' sc '))>' '-->*_' ds '^^' os '^^' n '<((' sct '))>'"
(at level 40, sc constr, sct constr).
Inductive multi_spec_inst (p:prog) :
@state spec_cfg -> @state spec_cfg -> dirs -> obs -> nat -> Prop :=
|multi_spec_inst_refl sc : p |- <(( sc ))> -->*_[]^^[]^^0 <(( sc ))>
|multi_spec_inst_trans sc1 sc2 sc3 ds1 ds2 os1 os2 n :
p |- <(( sc1 ))> -->_ds1^^os1 <(( sc2 ))> ->
p |- <(( sc2 ))> -->*_ds2^^os2^^n <(( sc3 ))> ->
p |- <(( sc1 ))> -->*_(ds1++ds2)^^(os1++os2)^^(S n) <(( sc3 ))>
where "p |- <(( sc ))> -->*_ ds ^^ os ^^ n <(( sct ))>" :=
(multi_spec_inst p sc sct ds os n).
Reserved Notation
"p '|-' '<((' ic '))>' '-->i_' ds '^^' os '<((' ict '))>'"
(at level 40, ic constr, ict constr).
Inductive ideal_eval_small_step_inst (p:prog) :
@state ideal_cfg -> @state ideal_cfg -> dirs -> obs -> Prop :=
| ISMI_Skip : forall pc r m sk ms,
p[[pc]] = Some <{{ skip }}> ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[]^^[] <(( S_Running ((pc+1, r, m, sk), ms) ))>
| ISMI_Asgn : forall pc r m sk ms e x,
p[[pc]] = Some <{{ x := e }}> ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[]^^[] <(( S_Running ((pc+1, (x !-> (eval r e); r), m, sk), ms) ))>
| ISMI_Div : forall pc r m sk (ms: bool) e1 e2 x v1 v2,
p[[pc]] = Some <{{ x <- div e1, e2 }}> ->
(if ms then Some 0 else to_nat (eval r e1)) = Some v1 ->
(if ms then Some 0 else to_nat (eval r e2)) = Some v2 ->
let res := match v2 with
| 0 => UV
| _ => N (div v1 v2)
end
in
p |- <(( S_Running (pc, r, m, sk, ms) ))> -->i_[]^^[ODiv v1 v2] <(( S_Running (pc+1, (x !-> res; r), m, sk, ms) ))>
| ISMI_Branch : forall pc pc' r m sk (ms ms' b b' : bool) e n' l,
p[[pc]] = Some <{{ branch e to l }}> ->
(if ms then Some 0 else to_nat (eval r e)) = Some n' ->
b = (not_zero n') ->
pc' = (if b' then (l,0) else pc+1) ->
ms' = (ms || (negb (Bool.eqb b b'))) ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[DBranch b']^^[OBranch b] <(( S_Running ((pc', r, m, sk), ms') ))>
| ISMI_Jump : forall l pc r m sk ms,
p[[pc]] = Some <{{ jump l }}> ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[]^^[] <(( S_Running (((l,0), r, m, sk), ms) ))>
| ISMI_Load : forall pc r m sk x e n me v' (ms : bool),
p[[pc]] = Some <{{ x <- load[e] }}> ->
me = (if ms then (ANum 0) else e) ->
to_nat (eval r me) = Some n ->
nth_error m n = Some v' ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[]^^[OLoad n] <(( S_Running ((pc+1, (x !-> v'; r), m, sk), ms) ))>
| ISMI_Store : forall pc r m sk e e' me n (ms : bool),
p[[pc]] = Some <{{ store[e] <- e' }}> ->
me = (if ms then (ANum 0) else e) ->
to_nat (eval r me) = Some n ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[]^^[OStore n] <(( S_Running ((pc+1, r, upd n m (eval r e'), sk), ms) ))>
| ISMI_Call : forall pc pc' r m sk e l (ms ms' : bool) blk,
p[[pc]] = Some <{{ call e }}> ->
(if ms then Some (0,0) else to_fp (eval r e)) = Some l ->
ms' = ms || negb ((fst pc' =? fst l)%nat && (snd pc' =? snd l)%nat) ->
nth_error p (fst pc') = Some blk ->
snd blk = true ->
snd pc' = 0 ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[DCall pc']^^[OCall l] <(( S_Running ((pc', r, m, (pc+1)::sk), ms') ))>
| ISMI_Call_F : forall pc pc' r m sk e l (ms : bool),
p[[pc]] = Some <{{ call e }}> ->
(if ms then Some (0,0) else to_fp (eval r e)) = Some l ->
(forall blk, nth_error p (fst pc') = Some blk -> snd blk = false \/ snd pc' <> 0) ->
p |- <(( S_Running ((pc, r, m, sk), ms) ))> -->i_[DCall pc']^^[OCall l] <(( S_Fault ))>
| ISMI_Ret : forall pc r m sk pc' pc'' ms ms',
p[[pc]] = Some <{{ ret }}> ->
wf_ret p pc'' ->
ms' = ms || negb ((fst pc' =? fst pc'')%nat && (snd pc' =? snd pc'')%nat) ->
p |- <(( S_Running ((pc, r, m, pc'::sk), ms) ))> -->i_[DRet pc'']^^[] <(( S_Running ((pc'', r, m, sk), ms') ))>
| ISMI_Peek : forall pc r m sk ms x, (* YH: Do we need this for source program? *)
p[[pc]] = Some <{{x <- peek}}> ->
let val := match sk with
| [] => UV
| pc' :: sk' => FP pc'
end
in
p |- <(( S_Running (pc, r, m, sk, ms) ))> -->i_[]^^[] <(( S_Running (pc+1, (x !-> val; r), m, sk, ms) ))>
| ISMI_Term : forall pc r m ms,
p[[pc]] = Some <{{ ret }}> ->
p |- <(( S_Running ((pc, r, m, []), ms) ))> -->i_[]^^[] <(( S_Term ))>
where "p |- <(( ic ))> -->i_ ds ^^ os <(( ict ))>" :=
(ideal_eval_small_step_inst p ic ict ds os).
Reserved Notation
"p '|-' '<((' ic '))>' '-->i*_' ds '^^' os '<((' ict '))>'"
(at level 40, ic constr, ict constr).
Inductive multi_ideal_inst (p:prog) :
@state ideal_cfg -> @state ideal_cfg -> dirs -> obs -> Prop :=
| multi_ideal_inst_refl ic : p |- <(( ic ))> -->i*_[]^^[] <(( ic ))>
| multi_ideal_inst_trans ic1 ic2 ic3 ds1 ds2 os1 os2 :
p |- <(( ic1 ))> -->i_ds1^^os1 <(( ic2 ))> ->
p |- <(( ic2 ))> -->i*_ds2^^os2 <(( ic3 ))> ->
p |- <(( ic1 ))> -->i*_(ds1++ds2)^^(os1++os2) <(( ic3 ))>
where "p |- <(( ic ))> -->i*_ ds ^^ os <(( ict ))>" :=
(multi_ideal_inst p ic ict ds os).
Definition msf_lookup_sc (sc: spec_cfg) : val :=
let '(c, ct, ms) := sc in
let '(pc, r, m, stk) := c in
r ! msf.
Definition msf_lookup_ic (ic: ideal_cfg) : val :=
let '(c, ms) := ic in
let '(pc, r, m, stk) := c in
r ! msf.
Definition callee_lookup_sc (sc: spec_cfg) : val :=
let '(c, ct, ms) := sc in
let '(pc, r, m, stk) := c in
r ! callee.
Definition callee_lookup_ic (ic: ideal_cfg) : val :=
let '(c, ms) := ic in
let '(pc, r, m, stk) := c in
r ! callee.
Definition ms_true_sc (sc: spec_cfg) : bool :=
let '(c, ct, ms) := sc in ms.
Definition ms_true_ic (ic: ideal_cfg) : bool :=
let '(c, ms) := ic in ms.
(* Definition is_br_or_call (i : inst) := *)
(* match i with *)
(* | <{{branch _ to _}}> | <{{call _}}> => true *)
(* | _ => false *)
(* end. *)
(* Definition pc_sync (p: prog) (pc: cptr) : option cptr := *)
(* match nth_error p (fst pc) with *)
(* | Some blk => match nth_error (fst blk) (snd pc) with *)
(* | Some i => *)
(* let acc1 := if (Bool.eqb (snd blk) true) then 2 else 0 in *)
(* let insts_before_pc := (firstn (snd pc) (fst blk)) in *)
(* let acc2 := fold_left (fun acc (i: inst) => if (is_br_or_call i) *)
(* then (add acc 1) *)
(* else acc) *)
(* insts_before_pc acc1 *)
(* in Some ((fst pc), add (snd pc) acc2) *)
(* | _ => None *)
(* end *)
(* | _ => None *)
(* end. *)
Definition offset_uslh (bl: list inst * bool) : list nat :=
_offset_uslh (fst bl) (if snd bl then 2 else 0).
(* pc - the first element of uslh_inst result *)
Definition pc_sync (p: prog) (pc: cptr) : option cptr :=
let ip := map offset_uslh p in
let '(l, o) := pc in
match nth_error ip l with
| Some ib => match nth_error ib o with
| Some io => Some (l, io)
| _ => None
end
| _ => None
end.
Definition r_sync (r: reg) (ms: bool) : reg :=
msf !-> N (if ms then 1 else 0); r.
Lemma eval_unused_update : forall (x : string) (r: reg) (e: exp) (v: val),
e_unused x e -> eval (x !-> v; r) e = eval r e.
Proof.
intros until v. induction e; intros; simpl in *; try reflexivity.
- eapply t_update_neq; auto.
- destruct H. specialize (IHe1 H). specialize (IHe2 H0).
rewrite IHe1, IHe2. auto.
- destruct H, H0. specialize (IHe1 H). specialize (IHe2 H0).
specialize (IHe3 H1). rewrite IHe1, IHe2, IHe3.
destruct (to_nat (eval r e1)) eqn:Heval1; auto.
Qed.
Fixpoint map_opt {S T} (f: S -> option T) l : option (list T):=
match l with
| [] => Some []
| a :: l' => match f a with
| Some a' =>
match map_opt f l' with
| Some l'' => Some (a' :: l'')
| _ => None
end
| _ => None
end
end.
Definition ret_sync (p: prog) (pc: cptr) : option cptr :=
match pc with
| (l, S o) => match p[[(l, o)]] with
| Some (ICall _) => match (pc_sync p (l, o)) with
| Some (l', o') => Some (l', o'+2)
| None => None
end
| _ => None
end
| _ => None
end.
Definition val_match (p: prog) (v1 v2: val) : Prop :=
match v1, v2 with
| N n1, N n2 => n1 = n2
| FP (l1, o1), FP (l2, o2) => if (o1 =? 0)%nat
then l1 = l2 /\ (o2 = 0)
else ret_sync p (l1, o1) = Some (l2, o2)
| UV, UV => True
| _, _ => False
end.
Definition Rsync (p: prog) (sr tr: reg) (ms: bool) : Prop :=
(forall x, x <> msf /\ x <> callee -> val_match p (sr ! x) (tr ! x)) /\
(tr ! msf = N (if ms then 1 else 0)).
Definition Msync (p: prog) (sm tm: mem) : Prop :=
forall i, match nth_error sm i, nth_error tm i with
| Some sv, Some tv => val_match p sv tv
| None, None => True
| _, _ => False
end.
Definition Rsync1 (p: prog) (sr tr: reg) : Prop :=
(forall x, x <> msf /\ x <> callee -> val_match p (sr ! x) (tr ! x)).
Definition ms_msf_match (tr: reg) (ms: bool) : Prop :=
(tr ! msf = N (if ms then 1 else 0)).
Variant match_cfgs (p: prog) : ideal_cfg -> spec_cfg -> Prop :=
| match_cfgs_intro pc r m stk ms pc' r' m' stk'
(PC: pc_sync p pc = Some pc')
(REG: Rsync p r r' ms)
(MSC: Msync p m m')
(STK: map_opt (ret_sync p) stk = Some stk') :
match_cfgs p ((pc, r, m, stk), ms) ((pc', r', m', stk'), false, ms)
.
(* Definition steps_to_sync_point' (p: prog) (ic: ideal_cfg) (ds: dirs) : option nat := *)
(* let '(c, ms) := ic in *)
(* let '(pc, r, m, sk) := c in *)
(* match p[[pc]] with *)
(* | Some i => match i with *)
(* | IBranch e l => match ds with *)
(* | DBranch b :: ds' => Some (if b then 3 else 2) *)
(* | _ => None *)
(* end *)
(* | ICall e => match ds with *)
(* | DCall pc' :: ds' => Some 4 *)
(* | _ => None *)
(* end *)
(* | _ => Some 1 *)
(* end *)
(* | _ => None *)
(* end. *)
Definition get_reg_sc (sc: spec_cfg) : reg :=
let '(c, ct, ms) := sc in
let '(pc, r, m, sk) := c in
r.
Definition get_reg_ic (ic: ideal_cfg) : reg :=
let '(c, ms) := ic in
let '(pc, r, m, sk) := c in
r.
Definition get_pc_sc (sc: spec_cfg) : cptr :=
let '(c, ct, ms) := sc in
let '(pc, r, m, sk) := c in
pc.
Definition get_pc_ic (ic: ideal_cfg) : cptr :=
let '(c, ms) := ic in
let '(pc, r, m, sk) := c in
pc.
Definition wf_dir' (p: prog) (d: direction) : Prop :=
match d with
| DCall pc' => is_some p[[pc']] = true
(* | DRet pc' => wf_ret p pc' *)
| _ => True
end.
Definition wf_ds' (p: prog) (ds: dirs) : Prop :=
Forall (wf_dir' p) ds.
Definition match_ob (p: prog) (o1 o2: observation) : Prop :=
match o1, o2 with
| ODiv _ _, ODiv _ _ | OBranch _, OBranch _ | OLoad _, OLoad _ | OStore _, OStore _ => o1 = o2
| OCall (l1, 0), OCall (l2, 0) => l1 = l2
| OCall (l1, S o1), OCall (l2, S o2) => ret_sync p (l1, S o1) = Some (l2, S o2)
(* if (wf_retb (uslh_prog p) (l2, S o2)) *)
(* then ret_sync p (l1, S o1) = Some (l2, S o2) *)
(* else o1 = o2 *)
| _, _ => False
end.
Definition match_obs (p: prog) (os1 os2: obs) : Prop :=
Forall2 (match_ob p) os1 os2.
Definition match_dir (p: prog) (d1 d2: direction) : Prop :=
match d1, d2 with
| DBranch b1, DBranch b2 => b1 = b2
| DCall pc1, DCall pc2 => pc1 = pc2
| DRet pc1, DRet pc2 => ret_sync p pc1 = Some pc2
| _, _ => False
end.
Definition match_ds (p: prog) (ds1 ds2: dirs) : Prop :=
Forall2 (match_dir p) ds1 ds2.
Definition nonempty_block (blk : (list inst * bool)) : Prop :=
0 < Datatypes.length (fst blk).
Definition is_terminator (i: inst) : Prop :=
match i with
| <{{ ret }}> | <{{ jump _}}> => True
| _ => False
end.
Definition last_inst_terminator (blk: (list inst * bool)) : Prop :=
match (rev (fst blk)) with
| [] => False
| h::t => is_terminator h
end.
Definition wf_lbl (p: prog) (is_proc: bool) (l: nat) : Prop :=
match nth_error p l with
| Some (_, b) => is_proc = b
| None => False
end.
(* JB: can we maybe deduplicate these w.r.t. MiniCET, where they are defined for bool? YH: Yes. We can. *)
Fixpoint wf_expr (p: prog) (e: exp) : Prop :=
match e with
| ANum _ | AId _ => True
| ABin _ e1 e2 => wf_expr p e1 /\ wf_expr p e2
| <{b ? e1 : e2}> => wf_expr p b /\ wf_expr p e1 /\ wf_expr p e2
| <{&l}> => snd l = 0 /\ wf_lbl p true (fst l)
end.
Lemma wf_expr_wf_exp p e:
wf_expr p e <-> wf_exp p e = true.
Proof.
induction e; ss; rewrite! andb_true_iff; firstorder.
1, 3: now apply Nat.eqb_eq.
all: unfold wf_lbl, wf_label in *; destruct (nth_error p (fst l)).
2: contradiction. 3: discriminate.
all: destruct p0; ss; clarify. now destruct b.
Qed.
Definition wf_instr (p: prog) (i: inst) : Prop :=
match i with
| <{{skip}}> | <{{ctarget}}> | <{{ret}}> | <{{_<-peek}}> => True
| <{{_:=e}}> | ILoad _ e | <{{call e}}> => wf_expr p e
| <{{store[e]<-e'}}> => wf_expr p e /\ wf_expr p e'
| <{{_<-div e1, e2}}> => wf_expr p e1 /\ wf_expr p e2
| <{{branch e to l}}> => wf_expr p e /\ wf_lbl p false l
| <{{jump l}}> => wf_lbl p false l
end.
Definition wf_block (p: prog) (blk : (list inst * bool)) : Prop :=
nonempty_block blk /\ last_inst_terminator blk /\ Forall (wf_instr p) (fst blk).
Definition nonempty_program (p: prog) : Prop :=
0 < Datatypes.length p.
Definition wf_prog (p: prog) : Prop :=
nonempty_program p /\ Forall (wf_block p) p.
Lemma wf_ds_app p ds1 ds2
(WF: wf_ds' p (ds1 ++ ds2)) :
wf_ds' p ds1 /\ wf_ds' p ds2.
Proof. eapply Forall_app. eauto. Qed.
Lemma multi_spec_splitting p sc ds os n sct m
(LEN: n >= m)
(STEPS: p |- <(( sc ))> -->*_ ds ^^ os ^^ n <(( sct ))>) :
exists scm ds1 ds2 os1 os2,
p |- <(( sc ))> -->*_ ds1 ^^ os1 ^^ m <(( scm ))>
/\ p |- <(( scm ))> -->*_ ds2 ^^ os2 ^^ (n - m) <(( sct ))>
/\ ds = ds1 ++ ds2 /\ os = os1 ++ os2.
Proof.
ginduction m; ii.
- esplits; try econs. rewrite sub_0_r. auto.
- destruct n as [|n]; try lia. inv STEPS.
exploit IHm; try eapply H5; eauto; [lia|]. i. des.
replace (S n - S m) with (n - m) by lia.
esplits; eauto.
{ econs; eauto. }
{ subst. rewrite app_assoc. auto. }
{ subst. rewrite app_assoc. auto. }
Qed.
Lemma rev_fetch : forall (p: prog) (pc: cptr) (blk: list inst * bool) (i: inst),
nth_error p (fst pc) = Some blk ->
nth_error (fst blk) (snd pc) = Some i ->
p[[pc]] = Some i.
Proof.
intros. destruct pc as (l & o) eqn:Hpc.
destruct (nth_error p (fst pc)) eqn:Hblk.
- unfold fetch. ss. des_ifs.
- rewrite Hpc in *. ss. des_ifs.
Qed.
Lemma blk_not_empty_list : forall (blk: list inst * bool),
nonempty_block blk -> (fst blk) <> [].
Proof.
intros. unfold nonempty_block in H. unfold not; intros. rewrite H0 in H.
simpl in H. apply nlt_0_r in H. destruct H.
Qed.
Lemma prog_not_empty_list (p: prog) : nonempty_program p -> p <> [].
Proof.
intros. unfold nonempty_program in H. unfold not; intros. rewrite H0 in H.
simpl in H. apply nlt_0_r in H. destruct H.
Qed.
Lemma cons_app : forall {A} (l: list A) (a: A),
a :: l = [a] ++ l.
Proof.
intros. destruct l; [rewrite app_nil_r|]; reflexivity.
Qed.
Lemma rev_cons : forall {A} (l: list A) (a: A),
rev (a :: l) = rev l ++ [a].
Proof.
intros. simpl. reflexivity.
Qed.
Lemma utils_rev_append_and_rev : forall {X : Type} (l1 l2 : list X),
Utils.rev_append l1 l2 = rev l1 ++ l2.
Proof.
intros X. induction l1 as [|h1 t1 IHl1].
- reflexivity.
- simpl. rewrite <- IHl1. apply functional_extensionality in IHl1.
rewrite IHl1. intros l2. rewrite <- app_assoc. simpl. reflexivity.
Qed.
Lemma revs : forall {A}, @Utils.rev A = @rev A.
Proof.
intros. apply functional_extensionality. intros.
rename x into l. induction l as [|h t IHl]; auto.
unfold Utils.rev in *. simpl. rewrite <- IHl.
rewrite utils_rev_append_and_rev. rewrite IHl. reflexivity.
Qed.
Lemma block_always_terminator b o i
(WFB: last_inst_terminator b)
(INST: nth_error (fst b) o = Some i)
(NT: ~ is_terminator i) :
exists i', nth_error (fst b) (add o 1) = Some i'.
Proof.
red in WFB. des_ifs.
destruct (le_dec o (Datatypes.length (fst b) - 1)).
{ assert (i <> i0).
{ unfold not; intros. unfold is_terminator in *.
destruct i eqn:Hi; destruct i0 eqn:Hi0; clarify. }
destruct (eq_dec o (Datatypes.length (fst b) - 1)).
{ assert (rev (i0 :: l) = rev l ++ [i0]). { simpl. auto. }
assert (rev (rev (fst b)) = rev (i0 :: l)). { rewrite Heq. simpl. auto. }
rewrite rev_involutive in H1. simpl in H1.
assert (nth_error (fst b) o = Some i0).
{ rewrite H1, e. simpl. rewrite H1. simpl. rewrite nth_error_app.
assert ((Datatypes.length (rev l ++ [i0]) - 1 <? Datatypes.length (rev l))%nat = false).
{ induction l as [|h t]; clarify. simpl in *.
assert (add 1 (Datatypes.length (rev t ++ [h])) = Datatypes.length ((rev t ++ [h]) ++ [i0])).
{ repeat rewrite length_app. assert (Datatypes.length [i0] = 1). { auto. } rewrite H2. rewrite add_comm. auto. }
rewrite <- H2. simpl. rewrite sub_0_r. apply ltb_irrefl.
}
rewrite H2.
assert (forall (n: nat), ((add n 1) - 1) - n = 0). { lia. }
specialize (H3 (Datatypes.length (rev l))).
rewrite length_app. assert (Datatypes.length [i0] = 1). { simpl. auto. }
rewrite H4.
assert (((add 1 (Datatypes.length (rev l))) - 1) = Datatypes.length (rev l)). { simpl. rewrite sub_0_r. auto. }
rewrite add_comm. rewrite H5. simpl.
assert ( ((Datatypes.length (rev l)) - (Datatypes.length (rev l))) = 0 ). { lia. }
rewrite H6. simpl. auto.
}
rewrite INST in H2. injection H2; intros. clarify.
}
assert (rev (i0 :: l) = rev l ++ [i0]) by ss.
assert (rev (rev (fst b)) = rev (i0 :: l)) by (rewrite Heq; ss).
rewrite rev_involutive in H1. simpl in H1. rewrite H1 in INST, l0, n. rewrite H1.
assert (o <= Datatypes.length (rev l ++ [i0]) - 1
-> o <> Datatypes.length (rev l ++ [i0]) - 1
-> o < Datatypes.length (rev l ++ [i0]) - 1 ).
{ lia. }
specialize (H2 l0 n); intros.
assert ((add o 1) <= (Datatypes.length (rev l ++ [i0]) - 1)). { lia. }
assert ((add o 1) < (Datatypes.length (rev l ++ [i0]))). { lia. }
rewrite <- nth_error_Some in H4.
destruct (nth_error (rev l ++ [i0]) (add o 1)); clarify. exists i1. auto.
}
exfalso. clear - n INST. eapply not_le in n.
assert (nth_error (fst b) o <> None).
{ ii. clarify. }
rewrite nth_error_Some in H. lia.
Qed.
Lemma block_always_terminator_prog_aux p pc i
(WF: Forall last_inst_terminator p)
(INST: p[[pc]] = Some i)
(NT: ~ is_terminator i) :
exists i', p[[pc+1]] = Some i'.
Proof.
destruct pc as [l o]. ss. des_ifs_safe.
exploit block_always_terminator; eauto.
rewrite Forall_forall in WF. eapply WF.
eapply nth_error_In; eauto.
Qed.
Lemma block_always_terminator_prog p pc i
(WF: wf_prog p)
(INST: p[[pc]] = Some i)
(NT: ~ is_terminator i) :
exists i', p[[pc+1]] = Some i'.
Proof.
inv WF. des. eapply block_always_terminator_prog_aux; eauto.
eapply Forall_and_inv in H0. des. eapply Forall_and_inv in H1. des.
eauto.
Qed.
Import MonadNotation.
Open Scope monad_scope.
Definition simple_inst (i: inst) : Prop :=
match i with
| ISkip | IJump _ | ILoad _ _ | IStore _ _ | IAsgn _ _ | IDiv _ _ _ | IPeek _ => True
| _ => False
end.
(* lock-step instructions *)
Variant match_simple_inst_uslh : inst -> inst -> Prop :=
| uslh_skip :
match_simple_inst_uslh ISkip ISkip
| uslh_jump l:
match_simple_inst_uslh (IJump l) (IJump l)
| uslh_load x e e'
(IDX: e' = <{{ (msf = 1) ? 0 : e }}>) :
match_simple_inst_uslh (ILoad x e) (ILoad x e')
| uslh_store e e' e1
(IDX: e' = <{{ (msf = 1) ? 0 : e }}>) :
match_simple_inst_uslh (IStore e e1) (IStore e' e1)
| uslh_asgn x e:
match_simple_inst_uslh (IAsgn x e) (IAsgn x e)
| uslh_div x e1 e2 e1' e2'
(IDe1: e1' = <{{ (msf = 1) ? 0 : e1}}>)
(IDe2: e2' = <{{ (msf = 1) ? 0 : e2}}>):
match_simple_inst_uslh (IDiv x e1 e2) (IDiv x e1' e2')
| uslh_peek x:
match_simple_inst_uslh (IPeek x) (IPeek x)
.
Definition _branch_in_block (blk: list inst) : nat :=
fold_left (fun acc i => add acc (match i with
| IBranch _ _ => 1
| _ => 0
end)) blk 0.
Definition branch_in_block (bb: list inst * bool) : nat :=
_branch_in_block (fst bb).
Definition branch_in_prog (p: prog) : nat :=
fold_left (fun acc b => add acc (branch_in_block b)) p 0.
Definition branch_in_prog_before (p: prog) (l: nat) : nat :=
branch_in_prog (firstn l p).
Definition _offset_branch_before (blk: list inst) (ofs: nat) : nat :=
_branch_in_block (firstn ofs blk).
Definition offset_branch_before (blk: list inst * bool) (ofs: nat) : nat :=
_offset_branch_before (fst blk) ofs.
Definition match_branch_target (p: prog) (pc: nat * nat) : option nat :=
let '(l, o) := pc in
match nth_error p l with
| Some blk => Some (Datatypes.length p + branch_in_prog_before p l + offset_branch_before blk o)
| _ => None
end.
Variant match_inst_uslh (p: prog) (pc: cptr) : inst -> inst -> Prop :=
| uslh_simple i i'
(SIMPL: simple_inst i)
(MATCH: match_simple_inst_uslh i i') :
match_inst_uslh p pc i i'
| uslh_branch e e' l l' tpc
(COND: e' = <{{ (msf = 1) ? 0 : e }}>)
(LB: match_branch_target p pc = Some l')
(IN: nth_error (uslh_prog p) l' =
Some ([<{{ msf := (~ e') ? 1 : msf }}>; <{{ jump l }}>], false))
(SYNC: pc_sync p pc = Some tpc)
(NXT: (uslh_prog p)[[tpc + 1]] = Some <{{ msf := e' ? 1 : msf }}>) :
match_inst_uslh p pc (IBranch e l) (IBranch e' l')
| uslh_call e e' e'' tpc
(CALL: e' = <{{ (msf = 1) ? & (0,0) : e }}>)
(SYNC: pc_sync p pc = Some tpc)
(RET: e'' = <{ callee = &(fst tpc, snd tpc + 2) }>)
(IN: (uslh_prog p)[[ tpc + 1 ]] = Some (ICall e'))
(IN': (uslh_prog p)[[ (fst tpc, snd tpc + 2) ]] = Some <{{ msf := e'' ? msf : 1 }}>) :
match_inst_uslh p pc (ICall e) (IAsgn callee e')
| uslh_ret tpc
(SYNC: pc_sync p pc = Some tpc)
(NXT: (uslh_prog p)[[tpc + 1]] = Some IRet) :
match_inst_uslh p pc IRet (IPeek callee)
.
Lemma uslh_inst_simple i l o c iss np
(SIMP: simple_inst i)
(USLH: uslh_inst i l o c = (iss, np)) :
exists i', iss = [i'] /\ match_simple_inst_uslh i i' /\ np = [].
Proof.
ii. unfold uslh_inst in USLH. ss.
des_ifs; ss; unfold MiniCET.uslh_ret in *; clarify; esplits; econs; eauto.
Qed.
Lemma mapM_nth_error {A B} (f: A -> M B) l c l' np o blk
(MM: mapM f l c = (l', np))
(SRC: nth_error l o = Some blk) :
exists blk' c' np', nth_error l' o = Some blk' /\ f blk c' = (blk', np').
Proof.
ginduction l; ss; ii.
{ rewrite nth_error_nil in SRC. clarify. }
rewrite unfold_mapM in MM.
destruct o as [|o].
{ ss; clarify. unfold uslh_bind in MM.
destruct (f blk c) eqn:F.
destruct (mapM f l (c + Datatypes.length p)) eqn:MF.
ss. clarify. esplits; eauto. }
ss. unfold uslh_bind in MM.
destruct (f a c) eqn:F.
destruct (mapM f l (c + Datatypes.length p)) eqn:MF. ss. clarify.
exploit IHl; eauto.
Qed.
Definition len_before {A B} (f: A -> M B) (l: list A) (o c: nat) : nat :=
let '(_, np) := mapM f (firstn o l) c in
Datatypes.length np.
Lemma mapM_nth_error_strong {A B} (f: A -> M B) l c l' np o blk
(MM: mapM f l c = (l', np))
(SRC: nth_error l o = Some blk) :
exists blk' c' np',
nth_error l' o = Some blk'
/\ f blk c' = (blk', np')
/\ c' = c + len_before f l o c.
Proof.
ginduction l; ss; ii.
{ rewrite nth_error_nil in SRC. clarify. }
rewrite unfold_mapM in MM.
destruct o as [|o].
{ ss; clarify. unfold uslh_bind in MM.
destruct (f blk c) eqn:F.
destruct (mapM f l (c + Datatypes.length p)) eqn:MF.
ss. clarify. esplits; eauto.
unfold len_before, mapM. des_ifs. ss.
unfold MiniCET.uslh_ret in Heq. clarify. ss. lia. }
ss. unfold uslh_bind in MM.
destruct (f a c) eqn:F.
destruct (mapM f l (c + Datatypes.length p)) eqn:MF. ss. clarify.
exploit IHl; eauto. i. des.
esplits; eauto.
unfold len_before. ss. des_ifs.
rewrite unfold_mapM in Heq. eapply bind_inv in Heq. des. subst.
eapply bind_inv in Heq0. des. subst.
unfold len_before. rewrite Heq in F. clarify. rewrite Heq0.
ss. unfold MiniCET.uslh_ret in Heq1. clarify.
do 2 rewrite length_app. ss. lia.
Qed.
(* Definition blk_offset (blk: list inst * bool) (o: nat) := *)
(* fold_left (fun (acc : nat) (i0 : inst) => if is_br_or_call i0 then add acc 1 else acc) (firstn o (fst blk)) *)
(* (if Bool.eqb (snd blk) true then 2 else 0). *)
Definition prefix_offset {A} (ll: list (list A)) (i: nat) (base: nat) :=
fold_left (fun acc l => acc + (Datatypes.length l)) (firstn i ll) base.
Definition fold_left_add_init {A} (f: A -> nat) (l: list A) (n k: nat) :
fold_left (fun acc x => acc + f x) l (n + k) = (fold_left (fun acc x => acc + f x) l n) + k.
Proof.
ginduction l; ss; ii.
replace (n + k + f a) with ((n + f a) + k) by lia. eauto.
Qed.
Definition fold_left_init_0 {A} (f: A -> nat) (l: list A) (n: nat) :
fold_left (fun acc x => acc + f x) l n = (fold_left (fun acc x => acc + f x) l 0) + n.
Proof.
replace n with (0 + n) by lia.
rewrite fold_left_add_init. lia.
Qed.
Lemma concat_nth_error {A} (ll: list (list A)) l i j ii
(LL: nth_error ll i = Some l)
(L: nth_error l j = Some ii) :
nth_error (List.concat ll) ((prefix_offset ll i 0) + j)%nat = Some ii.
Proof.
ginduction ll; ss; ii.
{ rewrite nth_error_nil in LL. clarify. }
destruct i; ss.
{ clarify. rewrite nth_error_app1; auto.
rewrite <- nth_error_Some. ii; clarify. }
exploit IHll; eauto. i.
replace (prefix_offset (a :: ll) (S i) 0) with ((Datatypes.length a) + (prefix_offset ll i 0)).
2:{ unfold prefix_offset. ss. rewrite add_comm. rewrite <- fold_left_add_init.
rewrite add_0_l. auto. }
rewrite nth_error_app2.
2:{ lia. }
replace (Datatypes.length a + prefix_offset ll i 0 + j - Datatypes.length a) with
(prefix_offset ll i 0 + j) by lia.
auto.
Qed.
Lemma uslh_inst_inst_sz i l n c i' pm
(USLH: uslh_inst i l n c = (i', pm)) :
Datatypes.length i' = uslh_inst_sz i.
Proof.
destruct i; ss; unfold MiniCET.uslh_ret in *; clarify.
eapply bind_inv in USLH. des. ss. clarify.
Qed.
Lemma offset_eq_aux
l blk n c blk' pn o o'
(TRANSL: mapM (fun '(o, i) => uslh_inst i l o)
(combine (_offset_uslh blk n) blk) c = (blk', pn))
(OFS: nth_error (_offset_uslh blk n) o = Some o')
(BDD: o < Datatypes.length blk) :
prefix_offset blk' o n = o'.
Proof.
ginduction blk; ii.
{ ss. lia. }
exploit mapM_perserve_len; eauto. intros LEN.
ss. rewrite unfold_mapM in TRANSL. eapply bind_inv in TRANSL. des.
eapply bind_inv in TRANSL0. des. clarify.
ss. unfold MiniCET.uslh_ret in *. clarify.
destruct o.
{ ss. clarify. }
ss. hexploit IHblk; eauto.
{ lia. }
i. subst. unfold prefix_offset. rewrite firstn_cons.
simpl. erewrite uslh_inst_inst_sz; eauto.
Qed.
(* Lemma offset_eq_aux blk c' l0 p1 n o *)
(* (BLK: mapM uslh_inst blk c' = (l0, p1)) *)
(* (BDD: o <= Datatypes.length blk) : *)
(* prefix_offset l0 o n = *)
(* o + fold_left (fun (acc : nat) (i0 : inst) => if is_br_or_call i0 then add acc 1 else acc) (firstn o blk) n. *)
(* Proof. *)
(* ginduction o; ii. *)
(* { ss. } *)
(* unfold prefix_offset. *)
(* exploit mapM_perserve_len; eauto. intros LEN. *)
(* destruct blk. *)
(* { ss. des_ifs. lia. } *)
(* destruct l0. *)
(* { ss. } *)
(* do 2 rewrite firstn_cons. *)
(* rewrite unfold_mapM in BLK. *)
(* exploit bind_inv; eauto. i. des. subst. ss. *)
(* unfold uslh_bind in x1. *)
(* destruct (mapM uslh_inst blk (c' + Datatypes.length pm)) eqn:X. ss. clarify. *)
(* exploit IHo. *)
(* { eauto. } *)
(* { lia. } *)
(* i. rewrite <- x1. *)
(* unfold prefix_offset. *)
(* replace (n + Datatypes.length l) with *)
(* (add (if is_br_or_call i then add n 1 else n) 1); auto. *)
(* 2:{ destruct i; ss; unfold MiniCET.uslh_ret in *; ss; clarify; ss. *)
(* - unfold uslh_bind in x0. ss. clarify. ss. lia. *)
(* - lia. } *)