forked from AbsInt/CompCert
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Asmgenproof1.v
1391 lines (1319 loc) · 58.9 KB
/
Asmgenproof1.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
(* *********************************************************************)
(* *)
(* The Compcert verified compiler *)
(* *)
(* Xavier Leroy, INRIA Paris-Rocquencourt *)
(* Prashanth Mundkur, SRI International *)
(* *)
(* Copyright Institut National de Recherche en Informatique et en *)
(* Automatique. All rights reserved. This file is distributed *)
(* under the terms of the INRIA Non-Commercial License Agreement. *)
(* *)
(* The contributions by Prashanth Mundkur are reused and adapted *)
(* under the terms of a Contributor License Agreement between *)
(* SRI International and INRIA. *)
(* *)
(* *********************************************************************)
Require Import Coqlib Errors Maps.
Require Import AST Zbits Integers Floats Values Memory Globalenvs.
Require Import Op Locations Mach Conventions.
Require Import Asm Asmgen Asmgenproof0.
(** Decomposition of integer constants. *)
Lemma make_immed32_sound:
forall n,
match make_immed32 n with
| Imm32_single imm => n = imm
| Imm32_pair hi lo => n = Int.add (Int.shl hi (Int.repr 12)) lo
end.
Proof.
intros; unfold make_immed32. set (lo := Int.sign_ext 12 n).
predSpec Int.eq Int.eq_spec n lo.
- auto.
- set (m := Int.sub n lo).
assert (A: eqmod (two_p 12) (Int.unsigned lo) (Int.unsigned n)) by (apply Int.eqmod_sign_ext'; compute; auto).
assert (B: eqmod (two_p 12) (Int.unsigned n - Int.unsigned lo) 0).
{ replace 0 with (Int.unsigned n - Int.unsigned n) by lia.
auto using eqmod_sub, eqmod_refl. }
assert (C: eqmod (two_p 12) (Int.unsigned m) 0).
{ apply eqmod_trans with (Int.unsigned n - Int.unsigned lo); auto.
apply eqmod_divides with Int.modulus. apply Int.eqm_sym; apply Int.eqm_unsigned_repr.
exists (two_p (32-12)); auto. }
assert (D: Int.modu m (Int.repr 4096) = Int.zero).
{ apply eqmod_mod_eq in C. unfold Int.modu.
change (Int.unsigned (Int.repr 4096)) with (two_p 12). rewrite C.
reflexivity.
apply two_p_gt_ZERO; lia. }
rewrite <- (Int.divu_pow2 m (Int.repr 4096) (Int.repr 12)) by auto.
rewrite Int.shl_mul_two_p.
change (two_p (Int.unsigned (Int.repr 12))) with 4096.
replace (Int.mul (Int.divu m (Int.repr 4096)) (Int.repr 4096)) with m.
unfold m. rewrite Int.sub_add_opp. rewrite Int.add_assoc. rewrite <- (Int.add_commut lo).
rewrite Int.add_neg_zero. rewrite Int.add_zero. auto.
rewrite (Int.modu_divu_Euclid m (Int.repr 4096)) at 1 by (vm_compute; congruence).
rewrite D. apply Int.add_zero.
Qed.
Lemma make_immed64_sound:
forall n,
match make_immed64 n with
| Imm64_single imm => n = imm
| Imm64_pair hi lo => n = Int64.add (Int64.sign_ext 32 (Int64.shl hi (Int64.repr 12))) lo
| Imm64_large imm => n = imm
end.
Proof.
intros; unfold make_immed64. set (lo := Int64.sign_ext 12 n).
predSpec Int64.eq Int64.eq_spec n lo.
- auto.
- set (m := Int64.sub n lo).
set (p := Int64.zero_ext 20 (Int64.shru m (Int64.repr 12))).
predSpec Int64.eq Int64.eq_spec n (Int64.add (Int64.sign_ext 32 (Int64.shl p (Int64.repr 12))) lo).
auto.
auto.
Qed.
(** Properties of registers *)
Lemma ireg_of_not_X31:
forall m r, ireg_of m = OK r -> IR r <> IR X31.
Proof.
intros. erewrite <- ireg_of_eq; eauto with asmgen.
Qed.
Lemma ireg_of_not_X31':
forall m r, ireg_of m = OK r -> r <> X31.
Proof.
intros. apply ireg_of_not_X31 in H. congruence.
Qed.
Global Hint Resolve ireg_of_not_X31 ireg_of_not_X31': asmgen.
(** Useful simplification tactic *)
Ltac Simplif :=
((rewrite nextinstr_inv by eauto with asmgen)
|| (rewrite nextinstr_inv1 by eauto with asmgen)
|| (rewrite Pregmap.gss)
|| (rewrite nextinstr_pc)
|| (rewrite Pregmap.gso by eauto with asmgen)); auto with asmgen.
Ltac Simpl := repeat Simplif.
(** * Correctness of RISC-V constructor functions *)
Section CONSTRUCTORS.
Variable ge: genv.
Variable fn: function.
(** 32-bit integer constants and arithmetic *)
Lemma load_hilo32_correct:
forall rd hi lo k rs m,
exists rs',
exec_straight ge fn (load_hilo32 rd hi lo k) rs m k rs' m
/\ rs'#rd = Vint (Int.add (Int.shl hi (Int.repr 12)) lo)
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
unfold load_hilo32; intros.
predSpec Int.eq Int.eq_spec lo Int.zero.
- subst lo. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split. rewrite Int.add_zero. Simpl.
intros; Simpl.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split. Simpl.
intros; Simpl.
Qed.
Lemma loadimm32_correct:
forall rd n k rs m,
exists rs',
exec_straight ge fn (loadimm32 rd n k) rs m k rs' m
/\ rs'#rd = Vint n
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
unfold loadimm32; intros. generalize (make_immed32_sound n); intros E.
destruct (make_immed32 n).
- subst imm. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split. rewrite Int.add_zero_l; Simpl.
intros; Simpl.
- rewrite E. apply load_hilo32_correct.
Qed.
Lemma opimm32_correct:
forall (op: ireg -> ireg0 -> ireg0 -> instruction)
(opi: ireg -> ireg0 -> int -> instruction)
(sem: val -> val -> val) m,
(forall d s1 s2 rs,
exec_instr ge fn (op d s1 s2) rs m = Next (nextinstr (rs#d <- (sem rs##s1 rs##s2))) m) ->
(forall d s n rs,
exec_instr ge fn (opi d s n) rs m = Next (nextinstr (rs#d <- (sem rs##s (Vint n)))) m) ->
forall rd r1 n k rs,
r1 <> X31 ->
exists rs',
exec_straight ge fn (opimm32 op opi rd r1 n k) rs m k rs' m
/\ rs'#rd = sem rs##r1 (Vint n)
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. unfold opimm32. generalize (make_immed32_sound n); intros E.
destruct (make_immed32 n).
- subst imm. econstructor; split.
apply exec_straight_one. rewrite H0. simpl; eauto. auto.
split. Simpl. intros; Simpl.
- destruct (load_hilo32_correct X31 hi lo (op rd r1 X31 :: k) rs m)
as (rs' & A & B & C).
econstructor; split.
eapply exec_straight_trans. eexact A. apply exec_straight_one.
rewrite H; eauto. auto.
split. Simpl. simpl. rewrite B, C, E. auto. congruence. congruence.
intros; Simpl.
Qed.
(** 64-bit integer constants and arithmetic *)
Lemma load_hilo64_correct:
forall rd hi lo k rs m,
exists rs',
exec_straight ge fn (load_hilo64 rd hi lo k) rs m k rs' m
/\ rs'#rd = Vlong (Int64.add (Int64.sign_ext 32 (Int64.shl hi (Int64.repr 12))) lo)
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
unfold load_hilo64; intros.
predSpec Int64.eq Int64.eq_spec lo Int64.zero.
- subst lo. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split. rewrite Int64.add_zero. Simpl.
intros; Simpl.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split. Simpl.
intros; Simpl.
Qed.
Lemma loadimm64_correct:
forall rd n k rs m,
exists rs',
exec_straight ge fn (loadimm64 rd n k) rs m k rs' m
/\ rs'#rd = Vlong n
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
unfold loadimm64; intros. generalize (make_immed64_sound n); intros E.
destruct (make_immed64 n).
- subst imm. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split. rewrite Int64.add_zero_l; Simpl.
intros; Simpl.
- exploit load_hilo64_correct; eauto. intros (rs' & A & B & C).
rewrite E. exists rs'; eauto.
- subst imm. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split. Simpl.
intros; Simpl.
Qed.
Lemma opimm64_correct:
forall (op: ireg -> ireg0 -> ireg0 -> instruction)
(opi: ireg -> ireg0 -> int64 -> instruction)
(sem: val -> val -> val) m,
(forall d s1 s2 rs,
exec_instr ge fn (op d s1 s2) rs m = Next (nextinstr (rs#d <- (sem rs###s1 rs###s2))) m) ->
(forall d s n rs,
exec_instr ge fn (opi d s n) rs m = Next (nextinstr (rs#d <- (sem rs###s (Vlong n)))) m) ->
forall rd r1 n k rs,
r1 <> X31 ->
exists rs',
exec_straight ge fn (opimm64 op opi rd r1 n k) rs m k rs' m
/\ rs'#rd = sem rs##r1 (Vlong n)
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. unfold opimm64. generalize (make_immed64_sound n); intros E.
destruct (make_immed64 n).
- subst imm. econstructor; split.
apply exec_straight_one. rewrite H0. simpl; eauto. auto.
split. Simpl. intros; Simpl.
- destruct (load_hilo64_correct X31 hi lo (op rd r1 X31 :: k) rs m)
as (rs' & A & B & C).
econstructor; split.
eapply exec_straight_trans. eexact A. apply exec_straight_one.
rewrite H; eauto. auto.
split. Simpl. simpl. rewrite B, C, E. auto. congruence. congruence.
intros; Simpl.
- subst imm. econstructor; split.
eapply exec_straight_two. simpl; eauto. rewrite H. simpl; eauto. auto. auto.
split. Simpl. intros; Simpl.
Qed.
(** Add offset to pointer *)
Lemma addptrofs_correct:
forall rd r1 n k rs m,
r1 <> X31 ->
exists rs',
exec_straight ge fn (addptrofs rd r1 n k) rs m k rs' m
/\ Val.lessdef (Val.offset_ptr rs#r1 n) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
unfold addptrofs; intros.
destruct (Ptrofs.eq_dec n Ptrofs.zero).
- subst n. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split. Simpl. destruct (rs r1); simpl; auto. rewrite Ptrofs.add_zero; auto.
intros; Simpl.
- destruct Archi.ptr64 eqn:SF.
+ unfold addimm64.
exploit (opimm64_correct Paddl Paddil Val.addl); eauto. intros (rs' & A & B & C).
exists rs'; split. eexact A. split; auto.
rewrite B. simpl. destruct (rs r1); simpl; auto. rewrite SF.
rewrite Ptrofs.of_int64_to_int64 by auto. auto.
+ unfold addimm32.
exploit (opimm32_correct Paddw Paddiw Val.add); eauto. intros (rs' & A & B & C).
exists rs'; split. eexact A. split; auto.
rewrite B. simpl. destruct (rs r1); simpl; auto. rewrite SF.
rewrite Ptrofs.of_int_to_int by auto. auto.
Qed.
Lemma addptrofs_correct_2:
forall rd r1 n k (rs: regset) m b ofs,
r1 <> X31 -> rs#r1 = Vptr b ofs ->
exists rs',
exec_straight ge fn (addptrofs rd r1 n k) rs m k rs' m
/\ rs'#rd = Vptr b (Ptrofs.add ofs n)
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. exploit (addptrofs_correct rd r1 n); eauto. intros (rs' & A & B & C).
exists rs'; intuition eauto.
rewrite H0 in B. inv B. auto.
Qed.
(** Translation of conditional branches *)
Lemma transl_cbranch_int32s_correct:
forall cmp r1 r2 lbl (rs: regset) m b,
Val.cmp_bool cmp rs##r1 rs##r2 = Some b ->
exec_instr ge fn (transl_cbranch_int32s cmp r1 r2 lbl) rs m =
eval_branch fn lbl rs m (Some b).
Proof.
intros. destruct cmp; simpl; rewrite ? H.
- destruct rs##r1; simpl in H; try discriminate. destruct rs##r2; inv H.
simpl; auto.
- destruct rs##r1; simpl in H; try discriminate. destruct rs##r2; inv H.
simpl; auto.
- auto.
- rewrite <- Val.swap_cmp_bool. simpl. rewrite H; auto.
- rewrite <- Val.swap_cmp_bool. simpl. rewrite H; auto.
- auto.
Qed.
Lemma transl_cbranch_int32u_correct:
forall cmp r1 r2 lbl (rs: regset) m b,
Val.cmpu_bool (Mem.valid_pointer m) cmp rs##r1 rs##r2 = Some b ->
exec_instr ge fn (transl_cbranch_int32u cmp r1 r2 lbl) rs m =
eval_branch fn lbl rs m (Some b).
Proof.
intros. destruct cmp; simpl; rewrite ? H; auto.
- rewrite <- Val.swap_cmpu_bool. simpl. rewrite H; auto.
- rewrite <- Val.swap_cmpu_bool. simpl. rewrite H; auto.
Qed.
Lemma transl_cbranch_int64s_correct:
forall cmp r1 r2 lbl (rs: regset) m b,
Val.cmpl_bool cmp rs###r1 rs###r2 = Some b ->
exec_instr ge fn (transl_cbranch_int64s cmp r1 r2 lbl) rs m =
eval_branch fn lbl rs m (Some b).
Proof.
intros. destruct cmp; simpl; rewrite ? H.
- destruct rs###r1; simpl in H; try discriminate. destruct rs###r2; inv H.
simpl; auto.
- destruct rs###r1; simpl in H; try discriminate. destruct rs###r2; inv H.
simpl; auto.
- auto.
- rewrite <- Val.swap_cmpl_bool. simpl. rewrite H; auto.
- rewrite <- Val.swap_cmpl_bool. simpl. rewrite H; auto.
- auto.
Qed.
Lemma transl_cbranch_int64u_correct:
forall cmp r1 r2 lbl (rs: regset) m b,
Val.cmplu_bool (Mem.valid_pointer m) cmp rs###r1 rs###r2 = Some b ->
exec_instr ge fn (transl_cbranch_int64u cmp r1 r2 lbl) rs m =
eval_branch fn lbl rs m (Some b).
Proof.
intros. destruct cmp; simpl; rewrite ? H; auto.
- rewrite <- Val.swap_cmplu_bool. simpl. rewrite H; auto.
- rewrite <- Val.swap_cmplu_bool. simpl. rewrite H; auto.
Qed.
Lemma transl_cond_float_correct:
forall (rs: regset) m cmp rd r1 r2 insn normal v,
transl_cond_float cmp rd r1 r2 = (insn, normal) ->
v = (if normal then Val.cmpf cmp rs#r1 rs#r2 else Val.notbool (Val.cmpf cmp rs#r1 rs#r2)) ->
exec_instr ge fn insn rs m = Next (nextinstr (rs#rd <- v)) m.
Proof.
intros. destruct cmp; simpl in H; inv H; auto.
- rewrite Val.negate_cmpf_eq. auto.
- simpl. f_equal. f_equal. f_equal. destruct (rs r2), (rs r1); auto. unfold Val.cmpf, Val.cmpf_bool.
rewrite <- Float.cmp_swap. auto.
- simpl. f_equal. f_equal. f_equal. destruct (rs r2), (rs r1); auto. unfold Val.cmpf, Val.cmpf_bool.
rewrite <- Float.cmp_swap. auto.
Qed.
Lemma transl_cond_single_correct:
forall (rs: regset) m cmp rd r1 r2 insn normal v,
transl_cond_single cmp rd r1 r2 = (insn, normal) ->
v = (if normal then Val.cmpfs cmp rs#r1 rs#r2 else Val.notbool (Val.cmpfs cmp rs#r1 rs#r2)) ->
exec_instr ge fn insn rs m = Next (nextinstr (rs#rd <- v)) m.
Proof.
intros. destruct cmp; simpl in H; inv H; auto.
- simpl. f_equal. f_equal. f_equal. destruct (rs r2), (rs r1); auto. unfold Val.cmpfs, Val.cmpfs_bool.
rewrite Float32.cmp_ne_eq. destruct (Float32.cmp Ceq f0 f); auto.
- simpl. f_equal. f_equal. f_equal. destruct (rs r2), (rs r1); auto. unfold Val.cmpfs, Val.cmpfs_bool.
rewrite <- Float32.cmp_swap. auto.
- simpl. f_equal. f_equal. f_equal. destruct (rs r2), (rs r1); auto. unfold Val.cmpfs, Val.cmpfs_bool.
rewrite <- Float32.cmp_swap. auto.
Qed.
Remark branch_on_X31:
forall normal lbl (rs: regset) m b,
rs#X31 = Val.of_bool (eqb normal b) ->
exec_instr ge fn (if normal then Pbnew X31 X0 lbl else Pbeqw X31 X0 lbl) rs m =
eval_branch fn lbl rs m (Some b).
Proof.
intros. destruct normal; simpl; rewrite H; simpl; destruct b; reflexivity.
Qed.
Ltac ArgsInv :=
repeat (match goal with
| [ H: Error _ = OK _ |- _ ] => discriminate
| [ H: match ?args with nil => _ | _ :: _ => _ end = OK _ |- _ ] => destruct args
| [ H: bind _ _ = OK _ |- _ ] => monadInv H
| [ H: match _ with left _ => _ | right _ => assertion_failed end = OK _ |- _ ] => monadInv H; ArgsInv
| [ H: match _ with true => _ | false => assertion_failed end = OK _ |- _ ] => monadInv H; ArgsInv
end);
subst;
repeat (match goal with
| [ H: ireg_of _ = OK _ |- _ ] => simpl in *; rewrite (ireg_of_eq _ _ H) in *
| [ H: freg_of _ = OK _ |- _ ] => simpl in *; rewrite (freg_of_eq _ _ H) in *
end).
Lemma transl_cbranch_correct_1:
forall cond args lbl k c m ms b sp rs m',
transl_cbranch cond args lbl k = OK c ->
eval_condition cond (List.map ms args) m = Some b ->
agree ms sp rs ->
Mem.extends m m' ->
exists rs', exists insn,
exec_straight_opt ge fn c rs m' (insn :: k) rs' m'
/\ exec_instr ge fn insn rs' m' = eval_branch fn lbl rs' m' (Some b)
/\ forall r, r <> PC -> r <> X31 -> rs'#r = rs#r.
Proof.
intros until m'; intros TRANSL EVAL AG MEXT.
set (vl' := map rs (map preg_of args)).
assert (EVAL': eval_condition cond vl' m' = Some b).
{ apply eval_condition_lessdef with (map ms args) m; auto. eapply preg_vals; eauto. }
clear EVAL MEXT AG.
destruct cond; simpl in TRANSL; ArgsInv.
- exists rs, (transl_cbranch_int32s c0 x x0 lbl).
intuition auto. constructor. apply transl_cbranch_int32s_correct; auto.
- exists rs, (transl_cbranch_int32u c0 x x0 lbl).
intuition auto. constructor. apply transl_cbranch_int32u_correct; auto.
- predSpec Int.eq Int.eq_spec n Int.zero.
+ subst n. exists rs, (transl_cbranch_int32s c0 x X0 lbl).
intuition auto. constructor. apply transl_cbranch_int32s_correct; auto.
+ exploit (loadimm32_correct X31 n); eauto. intros (rs' & A & B & C).
exists rs', (transl_cbranch_int32s c0 x X31 lbl).
split. constructor; eexact A. split; auto.
apply transl_cbranch_int32s_correct; auto.
simpl; rewrite B, C; eauto with asmgen.
- predSpec Int.eq Int.eq_spec n Int.zero.
+ subst n. exists rs, (transl_cbranch_int32u c0 x X0 lbl).
intuition auto. constructor. apply transl_cbranch_int32u_correct; auto.
+ exploit (loadimm32_correct X31 n); eauto. intros (rs' & A & B & C).
exists rs', (transl_cbranch_int32u c0 x X31 lbl).
split. constructor; eexact A. split; auto.
apply transl_cbranch_int32u_correct; auto.
simpl; rewrite B, C; eauto with asmgen.
- exists rs, (transl_cbranch_int64s c0 x x0 lbl).
intuition auto. constructor. apply transl_cbranch_int64s_correct; auto.
- exists rs, (transl_cbranch_int64u c0 x x0 lbl).
intuition auto. constructor. apply transl_cbranch_int64u_correct; auto.
- predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ subst n. exists rs, (transl_cbranch_int64s c0 x X0 lbl).
intuition auto. constructor. apply transl_cbranch_int64s_correct; auto.
+ exploit (loadimm64_correct X31 n); eauto. intros (rs' & A & B & C).
exists rs', (transl_cbranch_int64s c0 x X31 lbl).
split. constructor; eexact A. split; auto.
apply transl_cbranch_int64s_correct; auto.
simpl; rewrite B, C; eauto with asmgen.
- predSpec Int64.eq Int64.eq_spec n Int64.zero.
+ subst n. exists rs, (transl_cbranch_int64u c0 x X0 lbl).
intuition auto. constructor. apply transl_cbranch_int64u_correct; auto.
+ exploit (loadimm64_correct X31 n); eauto. intros (rs' & A & B & C).
exists rs', (transl_cbranch_int64u c0 x X31 lbl).
split. constructor; eexact A. split; auto.
apply transl_cbranch_int64u_correct; auto.
simpl; rewrite B, C; eauto with asmgen.
- destruct (transl_cond_float c0 X31 x x0) as [insn normal] eqn:TC; inv EQ2.
set (v := if normal then Val.cmpf c0 rs#x rs#x0 else Val.notbool (Val.cmpf c0 rs#x rs#x0)).
assert (V: v = Val.of_bool (eqb normal b)).
{ unfold v, Val.cmpf. rewrite EVAL'. destruct normal, b; reflexivity. }
econstructor; econstructor.
split. constructor. apply exec_straight_one. eapply transl_cond_float_correct with (v := v); eauto. auto.
split. rewrite V; destruct normal, b; reflexivity.
intros; Simpl.
- destruct (transl_cond_float c0 X31 x x0) as [insn normal] eqn:TC; inv EQ2.
assert (EVAL'': Val.cmpf_bool c0 (rs x) (rs x0) = Some (negb b)).
{ destruct (Val.cmpf_bool c0 (rs x) (rs x0)) as [[]|]; inv EVAL'; auto. }
set (v := if normal then Val.cmpf c0 rs#x rs#x0 else Val.notbool (Val.cmpf c0 rs#x rs#x0)).
assert (V: v = Val.of_bool (xorb normal b)).
{ unfold v, Val.cmpf. rewrite EVAL''. destruct normal, b; reflexivity. }
econstructor; econstructor.
split. constructor. apply exec_straight_one. eapply transl_cond_float_correct with (v := v); eauto. auto.
split. rewrite V; destruct normal, b; reflexivity.
intros; Simpl.
- destruct (transl_cond_single c0 X31 x x0) as [insn normal] eqn:TC; inv EQ2.
set (v := if normal then Val.cmpfs c0 rs#x rs#x0 else Val.notbool (Val.cmpfs c0 rs#x rs#x0)).
assert (V: v = Val.of_bool (eqb normal b)).
{ unfold v, Val.cmpfs. rewrite EVAL'. destruct normal, b; reflexivity. }
econstructor; econstructor.
split. constructor. apply exec_straight_one. eapply transl_cond_single_correct with (v := v); eauto. auto.
split. rewrite V; destruct normal, b; reflexivity.
intros; Simpl.
- destruct (transl_cond_single c0 X31 x x0) as [insn normal] eqn:TC; inv EQ2.
assert (EVAL'': Val.cmpfs_bool c0 (rs x) (rs x0) = Some (negb b)).
{ destruct (Val.cmpfs_bool c0 (rs x) (rs x0)) as [[]|]; inv EVAL'; auto. }
set (v := if normal then Val.cmpfs c0 rs#x rs#x0 else Val.notbool (Val.cmpfs c0 rs#x rs#x0)).
assert (V: v = Val.of_bool (xorb normal b)).
{ unfold v, Val.cmpfs. rewrite EVAL''. destruct normal, b; reflexivity. }
econstructor; econstructor.
split. constructor. apply exec_straight_one. eapply transl_cond_single_correct with (v := v); eauto. auto.
split. rewrite V; destruct normal, b; reflexivity.
intros; Simpl.
Qed.
Lemma transl_cbranch_correct_true:
forall cond args lbl k c m ms sp rs m',
transl_cbranch cond args lbl k = OK c ->
eval_condition cond (List.map ms args) m = Some true ->
agree ms sp rs ->
Mem.extends m m' ->
exists rs', exists insn,
exec_straight_opt ge fn c rs m' (insn :: k) rs' m'
/\ exec_instr ge fn insn rs' m' = goto_label fn lbl rs' m'
/\ forall r, r <> PC -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. eapply transl_cbranch_correct_1 with (b := true); eauto.
Qed.
Lemma transl_cbranch_correct_false:
forall cond args lbl k c m ms sp rs m',
transl_cbranch cond args lbl k = OK c ->
eval_condition cond (List.map ms args) m = Some false ->
agree ms sp rs ->
Mem.extends m m' ->
exists rs',
exec_straight ge fn c rs m' k rs' m'
/\ forall r, r <> PC -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. exploit transl_cbranch_correct_1; eauto. simpl.
intros (rs' & insn & A & B & C).
exists (nextinstr rs').
split. eapply exec_straight_opt_right; eauto. apply exec_straight_one; auto.
intros; Simpl.
Qed.
(** Translation of condition operators *)
Lemma transl_cond_int32s_correct:
forall cmp rd r1 r2 k rs m,
exists rs',
exec_straight ge fn (transl_cond_int32s cmp rd r1 r2 k) rs m k rs' m
/\ Val.lessdef (Val.cmp cmp rs##r1 rs##r2) rs'#rd
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
intros. destruct cmp; simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. destruct (rs##r1); auto. destruct (rs##r2); auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. destruct (rs##r1); auto. destruct (rs##r2); auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmp. rewrite <- Val.swap_cmp_bool.
simpl. rewrite (Val.negate_cmp_bool Clt).
destruct (Val.cmp_bool Clt rs##r2 rs##r1) as [[]|]; auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. unfold Val.cmp. rewrite <- Val.swap_cmp_bool. auto.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmp. rewrite (Val.negate_cmp_bool Clt).
destruct (Val.cmp_bool Clt rs##r1 rs##r2) as [[]|]; auto.
Qed.
Lemma transl_cond_int32u_correct:
forall cmp rd r1 r2 k rs m,
exists rs',
exec_straight ge fn (transl_cond_int32u cmp rd r1 r2 k) rs m k rs' m
/\ rs'#rd = Val.cmpu (Mem.valid_pointer m) cmp rs##r1 rs##r2
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
intros. destruct cmp; simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmpu. rewrite <- Val.swap_cmpu_bool.
simpl. rewrite (Val.negate_cmpu_bool (Mem.valid_pointer m) Cle).
destruct (Val.cmpu_bool (Mem.valid_pointer m) Cle rs##r1 rs##r2) as [[]|]; auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. unfold Val.cmpu. rewrite <- Val.swap_cmpu_bool. auto.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmpu. rewrite (Val.negate_cmpu_bool (Mem.valid_pointer m) Clt).
destruct (Val.cmpu_bool (Mem.valid_pointer m) Clt rs##r1 rs##r2) as [[]|]; auto.
Qed.
Lemma transl_cond_int64s_correct:
forall cmp rd r1 r2 k rs m,
exists rs',
exec_straight ge fn (transl_cond_int64s cmp rd r1 r2 k) rs m k rs' m
/\ Val.lessdef (Val.maketotal (Val.cmpl cmp rs###r1 rs###r2)) rs'#rd
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
intros. destruct cmp; simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. destruct (rs###r1); auto. destruct (rs###r2); auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. destruct (rs###r1); auto. destruct (rs###r2); auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmpl. rewrite <- Val.swap_cmpl_bool.
simpl. rewrite (Val.negate_cmpl_bool Clt).
destruct (Val.cmpl_bool Clt rs###r2 rs###r1) as [[]|]; auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. unfold Val.cmpl. rewrite <- Val.swap_cmpl_bool. auto.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmpl. rewrite (Val.negate_cmpl_bool Clt).
destruct (Val.cmpl_bool Clt rs###r1 rs###r2) as [[]|]; auto.
Qed.
Lemma transl_cond_int64u_correct:
forall cmp rd r1 r2 k rs m,
exists rs',
exec_straight ge fn (transl_cond_int64u cmp rd r1 r2 k) rs m k rs' m
/\ rs'#rd = Val.maketotal (Val.cmplu (Mem.valid_pointer m) cmp rs###r1 rs###r2)
/\ forall r, r <> PC -> r <> rd -> rs'#r = rs#r.
Proof.
intros. destruct cmp; simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmplu. rewrite <- Val.swap_cmplu_bool.
simpl. rewrite (Val.negate_cmplu_bool (Mem.valid_pointer m) Cle).
destruct (Val.cmplu_bool (Mem.valid_pointer m) Cle rs###r1 rs###r2) as [[]|]; auto.
- econstructor; split. apply exec_straight_one; [simpl; eauto|auto].
split; intros; Simpl. unfold Val.cmplu. rewrite <- Val.swap_cmplu_bool. auto.
- econstructor; split.
eapply exec_straight_two. simpl; eauto. simpl; eauto. auto. auto.
split; intros; Simpl. unfold Val.cmplu. rewrite (Val.negate_cmplu_bool (Mem.valid_pointer m) Clt).
destruct (Val.cmplu_bool (Mem.valid_pointer m) Clt rs###r1 rs###r2) as [[]|]; auto.
Qed.
Lemma transl_condimm_int32s_correct:
forall cmp rd r1 n k rs m,
r1 <> X31 ->
exists rs',
exec_straight ge fn (transl_condimm_int32s cmp rd r1 n k) rs m k rs' m
/\ Val.lessdef (Val.cmp cmp rs#r1 (Vint n)) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. unfold transl_condimm_int32s.
predSpec Int.eq Int.eq_spec n Int.zero.
- subst n. exploit transl_cond_int32s_correct. intros (rs' & A & B & C).
exists rs'; eauto.
- assert (DFL:
exists rs',
exec_straight ge fn (loadimm32 X31 n (transl_cond_int32s cmp rd r1 X31 k)) rs m k rs' m
/\ Val.lessdef (Val.cmp cmp rs#r1 (Vint n)) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r).
{ exploit loadimm32_correct; eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int32s_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2. rewrite B1, C1 in B2 by auto with asmgen. auto.
intros; transitivity (rs1 r); auto. }
destruct cmp.
+ unfold xorimm32.
exploit (opimm32_correct Pxorw Pxoriw Val.xor); eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int32s_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2; rewrite B1 in B2; simpl in B2. destruct (rs#r1); auto.
unfold Val.cmp in B2; simpl in B2; rewrite Int.xor_is_zero in B2. exact B2.
intros; transitivity (rs1 r); auto.
+ unfold xorimm32.
exploit (opimm32_correct Pxorw Pxoriw Val.xor); eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int32s_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2; rewrite B1 in B2; simpl in B2. destruct (rs#r1); auto.
unfold Val.cmp in B2; simpl in B2; rewrite Int.xor_is_zero in B2. exact B2.
intros; transitivity (rs1 r); auto.
+ exploit (opimm32_correct Psltw Psltiw (Val.cmp Clt)); eauto. intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto. rewrite B1; auto.
+ predSpec Int.eq Int.eq_spec n (Int.repr Int.max_signed).
* subst n. exploit loadimm32_correct; eauto. intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto.
unfold Val.cmp; destruct (rs#r1); simpl; auto. rewrite B1.
unfold Int.lt. rewrite zlt_false. auto.
change (Int.signed (Int.repr Int.max_signed)) with Int.max_signed.
generalize (Int.signed_range i); lia.
* exploit (opimm32_correct Psltw Psltiw (Val.cmp Clt)); eauto. intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto.
rewrite B1. unfold Val.cmp; simpl; destruct (rs#r1); simpl; auto.
unfold Int.lt. replace (Int.signed (Int.add n Int.one)) with (Int.signed n + 1).
destruct (zlt (Int.signed n) (Int.signed i)).
rewrite zlt_false by lia. auto.
rewrite zlt_true by lia. auto.
rewrite Int.add_signed. symmetry; apply Int.signed_repr.
assert (Int.signed n <> Int.max_signed).
{ red; intros E. elim H1. rewrite <- (Int.repr_signed n). rewrite E. auto. }
generalize (Int.signed_range n); lia.
+ apply DFL.
+ apply DFL.
Qed.
Lemma transl_condimm_int32u_correct:
forall cmp rd r1 n k rs m,
r1 <> X31 ->
exists rs',
exec_straight ge fn (transl_condimm_int32u cmp rd r1 n k) rs m k rs' m
/\ Val.lessdef (Val.cmpu (Mem.valid_pointer m) cmp rs#r1 (Vint n)) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. unfold transl_condimm_int32u.
predSpec Int.eq Int.eq_spec n Int.zero.
- subst n. exploit transl_cond_int32u_correct. intros (rs' & A & B & C).
exists rs'; split. eexact A. split; auto. rewrite B; auto.
- assert (DFL:
exists rs',
exec_straight ge fn (loadimm32 X31 n (transl_cond_int32u cmp rd r1 X31 k)) rs m k rs' m
/\ Val.lessdef (Val.cmpu (Mem.valid_pointer m) cmp rs#r1 (Vint n)) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r).
{ exploit loadimm32_correct; eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int32u_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2. rewrite B1, C1 in B2 by auto with asmgen. rewrite B2; auto.
intros; transitivity (rs1 r); auto. }
destruct cmp.
+ apply DFL.
+ apply DFL.
+ exploit (opimm32_correct Psltuw Psltiuw (Val.cmpu (Mem.valid_pointer m) Clt) m); eauto.
intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto. rewrite B1; auto.
+ apply DFL.
+ apply DFL.
+ apply DFL.
Qed.
Lemma transl_condimm_int64s_correct:
forall cmp rd r1 n k rs m,
r1 <> X31 ->
exists rs',
exec_straight ge fn (transl_condimm_int64s cmp rd r1 n k) rs m k rs' m
/\ Val.lessdef (Val.maketotal (Val.cmpl cmp rs#r1 (Vlong n))) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. unfold transl_condimm_int64s.
predSpec Int64.eq Int64.eq_spec n Int64.zero.
- subst n. exploit transl_cond_int64s_correct. intros (rs' & A & B & C).
exists rs'; eauto.
- assert (DFL:
exists rs',
exec_straight ge fn (loadimm64 X31 n (transl_cond_int64s cmp rd r1 X31 k)) rs m k rs' m
/\ Val.lessdef (Val.maketotal (Val.cmpl cmp rs#r1 (Vlong n))) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r).
{ exploit loadimm64_correct; eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int64s_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2. rewrite B1, C1 in B2 by auto with asmgen. auto.
intros; transitivity (rs1 r); auto. }
destruct cmp.
+ unfold xorimm64.
exploit (opimm64_correct Pxorl Pxoril Val.xorl); eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int64s_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2; rewrite B1 in B2; simpl in B2. destruct (rs#r1); auto.
unfold Val.cmpl in B2; simpl in B2; rewrite Int64.xor_is_zero in B2. exact B2.
intros; transitivity (rs1 r); auto.
+ unfold xorimm64.
exploit (opimm64_correct Pxorl Pxoril Val.xorl); eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int64s_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2; rewrite B1 in B2; simpl in B2. destruct (rs#r1); auto.
unfold Val.cmpl in B2; simpl in B2; rewrite Int64.xor_is_zero in B2. exact B2.
intros; transitivity (rs1 r); auto.
+ exploit (opimm64_correct Psltl Psltil (fun v1 v2 => Val.maketotal (Val.cmpl Clt v1 v2))); eauto. intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto. rewrite B1; auto.
+ predSpec Int64.eq Int64.eq_spec n (Int64.repr Int64.max_signed).
* subst n. exploit loadimm32_correct; eauto. intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto.
unfold Val.cmpl; destruct (rs#r1); simpl; auto. rewrite B1.
unfold Int64.lt. rewrite zlt_false. auto.
change (Int64.signed (Int64.repr Int64.max_signed)) with Int64.max_signed.
generalize (Int64.signed_range i); lia.
* exploit (opimm64_correct Psltl Psltil (fun v1 v2 => Val.maketotal (Val.cmpl Clt v1 v2))); eauto. intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto.
rewrite B1. unfold Val.cmpl; simpl; destruct (rs#r1); simpl; auto.
unfold Int64.lt. replace (Int64.signed (Int64.add n Int64.one)) with (Int64.signed n + 1).
destruct (zlt (Int64.signed n) (Int64.signed i)).
rewrite zlt_false by lia. auto.
rewrite zlt_true by lia. auto.
rewrite Int64.add_signed. symmetry; apply Int64.signed_repr.
assert (Int64.signed n <> Int64.max_signed).
{ red; intros E. elim H1. rewrite <- (Int64.repr_signed n). rewrite E. auto. }
generalize (Int64.signed_range n); lia.
+ apply DFL.
+ apply DFL.
Qed.
Lemma transl_condimm_int64u_correct:
forall cmp rd r1 n k rs m,
r1 <> X31 ->
exists rs',
exec_straight ge fn (transl_condimm_int64u cmp rd r1 n k) rs m k rs' m
/\ Val.lessdef (Val.maketotal (Val.cmplu (Mem.valid_pointer m) cmp rs#r1 (Vlong n))) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
intros. unfold transl_condimm_int64u.
predSpec Int64.eq Int64.eq_spec n Int64.zero.
- subst n. exploit transl_cond_int64u_correct. intros (rs' & A & B & C).
exists rs'; split. eexact A. split; auto. rewrite B; auto.
- assert (DFL:
exists rs',
exec_straight ge fn (loadimm64 X31 n (transl_cond_int64u cmp rd r1 X31 k)) rs m k rs' m
/\ Val.lessdef (Val.maketotal (Val.cmplu (Mem.valid_pointer m) cmp rs#r1 (Vlong n))) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r).
{ exploit loadimm64_correct; eauto. intros (rs1 & A1 & B1 & C1).
exploit transl_cond_int64u_correct; eauto. intros (rs2 & A2 & B2 & C2).
exists rs2; split.
eapply exec_straight_trans. eexact A1. eexact A2.
split. simpl in B2. rewrite B1, C1 in B2 by auto with asmgen. rewrite B2; auto.
intros; transitivity (rs1 r); auto. }
destruct cmp.
+ apply DFL.
+ apply DFL.
+ exploit (opimm64_correct Psltul Psltiul (fun v1 v2 => Val.maketotal (Val.cmplu (Mem.valid_pointer m) Clt v1 v2)) m); eauto.
intros (rs1 & A1 & B1 & C1).
exists rs1; split. eexact A1. split; auto. rewrite B1; auto.
+ apply DFL.
+ apply DFL.
+ apply DFL.
Qed.
Lemma transl_cond_op_correct:
forall cond rd args k c rs m,
transl_cond_op cond rd args k = OK c ->
exists rs',
exec_straight ge fn c rs m k rs' m
/\ Val.lessdef (Val.of_optbool (eval_condition cond (map rs (map preg_of args)) m)) rs'#rd
/\ forall r, r <> PC -> r <> rd -> r <> X31 -> rs'#r = rs#r.
Proof.
assert (MKTOT: forall ob, Val.of_optbool ob = Val.maketotal (option_map Val.of_bool ob)).
{ destruct ob as [[]|]; reflexivity. }
intros until m; intros TR.
destruct cond; simpl in TR; ArgsInv.
+ (* cmp *)
exploit transl_cond_int32s_correct; eauto. intros (rs' & A & B & C). exists rs'; eauto.
+ (* cmpu *)
exploit transl_cond_int32u_correct; eauto. intros (rs' & A & B & C).
exists rs'; repeat split; eauto. rewrite B; auto.
+ (* cmpimm *)
apply transl_condimm_int32s_correct; eauto with asmgen.
+ (* cmpuimm *)
apply transl_condimm_int32u_correct; eauto with asmgen.
+ (* cmpl *)
exploit transl_cond_int64s_correct; eauto. intros (rs' & A & B & C).
exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
+ (* cmplu *)
exploit transl_cond_int64u_correct; eauto. intros (rs' & A & B & C).
exists rs'; repeat split; eauto. rewrite B, MKTOT; eauto.
+ (* cmplimm *)
exploit transl_condimm_int64s_correct; eauto. instantiate (1 := x); eauto with asmgen.
intros (rs' & A & B & C).
exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
+ (* cmpluimm *)
exploit transl_condimm_int64u_correct; eauto. instantiate (1 := x); eauto with asmgen.
intros (rs' & A & B & C).
exists rs'; repeat split; eauto. rewrite MKTOT; eauto.
+ (* cmpf *)
destruct (transl_cond_float c0 rd x x0) as [insn normal] eqn:TR.
fold (Val.cmpf c0 (rs x) (rs x0)).
set (v := Val.cmpf c0 (rs x) (rs x0)).
destruct normal; inv EQ2.
* econstructor; split.
apply exec_straight_one. eapply transl_cond_float_correct with (v := v); eauto. auto.
split; intros; Simpl.
* econstructor; split.
eapply exec_straight_two.
eapply transl_cond_float_correct with (v := Val.notbool v); eauto.
simpl; reflexivity.
auto. auto.
split; intros; Simpl. unfold v, Val.cmpf. destruct (Val.cmpf_bool c0 (rs x) (rs x0)) as [[]|]; auto.
+ (* notcmpf *)
destruct (transl_cond_float c0 rd x x0) as [insn normal] eqn:TR.
rewrite Val.notbool_negb_3. fold (Val.cmpf c0 (rs x) (rs x0)).
set (v := Val.cmpf c0 (rs x) (rs x0)).
destruct normal; inv EQ2.
* econstructor; split.
eapply exec_straight_two.
eapply transl_cond_float_correct with (v := v); eauto.
simpl; reflexivity.
auto. auto.
split; intros; Simpl. unfold v, Val.cmpf. destruct (Val.cmpf_bool c0 (rs x) (rs x0)) as [[]|]; auto.
* econstructor; split.
apply exec_straight_one. eapply transl_cond_float_correct with (v := Val.notbool v); eauto. auto.
split; intros; Simpl.
+ (* cmpfs *)
destruct (transl_cond_single c0 rd x x0) as [insn normal] eqn:TR.
fold (Val.cmpfs c0 (rs x) (rs x0)).
set (v := Val.cmpfs c0 (rs x) (rs x0)).
destruct normal; inv EQ2.
* econstructor; split.
apply exec_straight_one. eapply transl_cond_single_correct with (v := v); eauto. auto.
split; intros; Simpl.
* econstructor; split.
eapply exec_straight_two.
eapply transl_cond_single_correct with (v := Val.notbool v); eauto.
simpl; reflexivity.
auto. auto.
split; intros; Simpl. unfold v, Val.cmpfs. destruct (Val.cmpfs_bool c0 (rs x) (rs x0)) as [[]|]; auto.
+ (* notcmpfs *)
destruct (transl_cond_single c0 rd x x0) as [insn normal] eqn:TR.
rewrite Val.notbool_negb_3. fold (Val.cmpfs c0 (rs x) (rs x0)).
set (v := Val.cmpfs c0 (rs x) (rs x0)).
destruct normal; inv EQ2.
* econstructor; split.
eapply exec_straight_two.
eapply transl_cond_single_correct with (v := v); eauto.
simpl; reflexivity.
auto. auto.
split; intros; Simpl. unfold v, Val.cmpfs. destruct (Val.cmpfs_bool c0 (rs x) (rs x0)) as [[]|]; auto.
* econstructor; split.
apply exec_straight_one. eapply transl_cond_single_correct with (v := Val.notbool v); eauto. auto.
split; intros; Simpl.
Qed.
(** Some arithmetic properties. *)
Remark cast32unsigned_from_cast32signed:
forall i, Int64.repr (Int.unsigned i) = Int64.zero_ext 32 (Int64.repr (Int.signed i)).
Proof.
intros. apply Int64.same_bits_eq; intros.
rewrite Int64.bits_zero_ext, !Int64.testbit_repr by tauto.
rewrite Int.bits_signed by tauto. fold (Int.testbit i i0).
change Int.zwordsize with 32.
destruct (zlt i0 32). auto. apply Int.bits_above. auto.
Qed.
(* Translation of arithmetic operations *)
Ltac SimplEval H :=
match type of H with
| Some _ = None _ => discriminate
| Some _ = Some _ => inv H
| ?a = Some ?b => let A := fresh in assert (A: Val.maketotal a = b) by (rewrite H; reflexivity)
end.
Ltac TranslOpSimpl :=
econstructor; split;
[ apply exec_straight_one; [simpl; eauto | reflexivity]
| split; [ apply Val.lessdef_same; Simpl; fail | intros; Simpl; fail ] ].
Lemma transl_op_correct:
forall op args res k (rs: regset) m v c,
transl_op op args res k = OK c ->
eval_operation ge (rs#SP) op (map rs (map preg_of args)) m = Some v ->
exists rs',
exec_straight ge fn c rs m k rs' m
/\ Val.lessdef v rs'#(preg_of res)
/\ forall r, data_preg r = true -> r <> preg_of res -> preg_notin r (destroyed_by_op op) -> rs' r = rs r.
Proof.
assert (SAME: forall v1 v2, v1 = v2 -> Val.lessdef v2 v1). { intros; subst; auto. }
Opaque Int.eq.
intros until c; intros TR EV.
unfold transl_op in TR; destruct op; ArgsInv; simpl in EV; SimplEval EV; try TranslOpSimpl.
- (* move *)
destruct (preg_of res), (preg_of m0); inv TR; TranslOpSimpl.
- (* intconst *)
exploit loadimm32_correct; eauto. intros (rs' & A & B & C).
exists rs'; split; eauto. rewrite B; auto with asmgen.
- (* longconst *)
exploit loadimm64_correct; eauto. intros (rs' & A & B & C).
exists rs'; split; eauto. rewrite B; auto with asmgen.
- (* floatconst *)
destruct (Float.eq_dec n Float.zero).
+ subst n. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split; intros; Simpl.
+ econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split; intros; Simpl.
- (* singleconst *)
destruct (Float32.eq_dec n Float32.zero).
+ subst n. econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split; intros; Simpl.
+ econstructor; split.
apply exec_straight_one. simpl; eauto. auto.
split; intros; Simpl.
- (* addrsymbol *)
destruct (Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)).
+ set (rs1 := nextinstr (rs#x <- (Genv.symbol_address ge id Ptrofs.zero))).
exploit (addptrofs_correct x x ofs k rs1 m); eauto with asmgen.
intros (rs2 & A & B & C).
exists rs2; split.
apply exec_straight_step with rs1 m; auto.
split. replace ofs with (Ptrofs.add Ptrofs.zero ofs) by (apply Ptrofs.add_zero_l).
rewrite Genv.shift_symbol_address.
replace (rs1 x) with (Genv.symbol_address ge id Ptrofs.zero) in B by (unfold rs1; Simpl).