Correctness proof for IA32 generation: auxiliary results.
Require Import Coqlib.
Require Import Maps.
Require Import AST.
Require Import Errors.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import Memory.
Require Import Globalenvs.
Require Import Op.
Require Import Locations.
Require Import Mach.
Require Import Machsem.
Require Import Machtyping.
Require Import Asm.
Require Import Asmgen.
Require Import Conventions.
Open Local Scope error_monad_scope.
Correspondence between Mach registers and IA32 registers
Hint Extern 2 (
_ <>
_) =>
congruence:
ppcgen.
Lemma preg_of_injective:
forall r1 r2,
preg_of r1 =
preg_of r2 ->
r1 =
r2.
Proof.
destruct r1; destruct r2; simpl; intros; reflexivity || discriminate.
Qed.
Lemma preg_of_not_ESP:
forall r,
preg_of r <>
ESP.
Proof.
destruct r; simpl; congruence.
Qed.
Lemma preg_of_not_PC:
forall r,
preg_of r <>
PC.
Proof.
destruct r; simpl; congruence.
Qed.
Hint Resolve preg_of_not_ESP preg_of_not_PC:
ppcgen.
Lemma ireg_of_eq:
forall r r',
ireg_of r =
OK r' ->
preg_of r =
IR r'.
Proof.
unfold ireg_of;
intros.
destruct (
preg_of r);
inv H;
auto.
Qed.
Lemma freg_of_eq:
forall r r',
freg_of r =
OK r' ->
preg_of r =
FR r'.
Proof.
unfold freg_of;
intros.
destruct (
preg_of r);
inv H;
auto.
Qed.
Agreement between Mach register sets and IA32 register sets.
Record agree (
ms:
Mach.regset) (
sp:
val) (
rs:
Asm.regset) :
Prop :=
mkagree {
agree_sp:
rs#
ESP =
sp;
agree_sp_def:
sp <>
Vundef;
agree_mregs:
forall r:
mreg,
Val.lessdef (
ms r) (
rs#(
preg_of r))
}.
Lemma preg_val:
forall ms sp rs r,
agree ms sp rs ->
Val.lessdef (
ms r)
rs#(
preg_of r).
Proof.
intros. destruct H. auto.
Qed.
Lemma preg_vals:
forall ms sp rs,
agree ms sp rs ->
forall l,
Val.lessdef_list (
map ms l) (
map rs (
map preg_of l)).
Proof.
induction l;
simpl.
constructor.
constructor.
eapply preg_val;
eauto.
auto.
Qed.
Lemma ireg_val:
forall ms sp rs r r',
agree ms sp rs ->
ireg_of r =
OK r' ->
Val.lessdef (
ms r)
rs#
r'.
Proof.
Lemma freg_val:
forall ms sp rs r r',
agree ms sp rs ->
freg_of r =
OK r' ->
Val.lessdef (
ms r) (
rs#
r').
Proof.
Lemma sp_val:
forall ms sp rs,
agree ms sp rs ->
sp =
rs#
ESP.
Proof.
intros. destruct H; auto.
Qed.
Hint Resolve preg_val ireg_val freg_val sp_val:
ppcgen.
Definition important_preg (
r:
preg) :
bool :=
match r with
|
PC =>
false
|
IR _ =>
true
|
FR _ =>
true
|
ST0 =>
true
|
CR _ =>
false
|
RA =>
false
end.
Lemma preg_of_important:
forall r,
important_preg (
preg_of r) =
true.
Proof.
intros. destruct r; reflexivity.
Qed.
Lemma important_diff:
forall r r',
important_preg r =
true ->
important_preg r' =
false ->
r <>
r'.
Proof.
congruence.
Qed.
Hint Resolve important_diff:
ppcgen.
Lemma agree_exten:
forall ms sp rs rs',
agree ms sp rs ->
(
forall r,
important_preg r =
true ->
rs'#
r =
rs#
r) ->
agree ms sp rs'.
Proof.
intros.
destruct H.
split.
rewrite H0;
auto.
auto.
intros.
rewrite H0;
auto.
apply preg_of_important.
Qed.
Preservation of register agreement under various assignments.
Lemma agree_set_mreg:
forall ms sp rs r v rs',
agree ms sp rs ->
Val.lessdef v (
rs'#(
preg_of r)) ->
(
forall r',
important_preg r' =
true ->
r' <>
preg_of r ->
rs'#
r' =
rs#
r') ->
agree (
Regmap.set r v ms)
sp rs'.
Proof.
Lemma agree_set_other:
forall ms sp rs r v,
agree ms sp rs ->
important_preg r =
false ->
agree ms sp (
rs#
r <-
v).
Proof.
Lemma agree_nextinstr:
forall ms sp rs,
agree ms sp rs ->
agree ms sp (
nextinstr rs).
Proof.
Lemma agree_undef_unimportant_regs:
forall ms sp rl rs,
agree ms sp rs ->
(
forall r,
In r rl ->
important_preg r =
false) ->
agree ms sp (
undef_regs rl rs).
Proof.
induction rl;
simpl;
intros.
auto.
apply IHrl.
apply agree_exten with rs;
auto.
intros.
apply Pregmap.gso.
red;
intros;
subst.
assert (
important_preg a =
false)
by auto.
congruence.
intros.
apply H0;
auto.
Qed.
Lemma agree_nextinstr_nf:
forall ms sp rs,
agree ms sp rs ->
agree ms sp (
nextinstr_nf rs).
Proof.
Definition nontemp_preg (
r:
preg) :
bool :=
match r with
|
PC =>
false
|
IR ECX =>
false
|
IR EDX =>
false
|
IR _ =>
true
|
FR XMM6 =>
false
|
FR XMM7 =>
false
|
FR _ =>
true
|
ST0 =>
false
|
CR _ =>
false
|
RA =>
false
end.
Lemma nontemp_diff:
forall r r',
nontemp_preg r =
true ->
nontemp_preg r' =
false ->
r <>
r'.
Proof.
congruence.
Qed.
Hint Resolve nontemp_diff:
ppcgen.
Lemma agree_exten_temps:
forall ms sp rs rs',
agree ms sp rs ->
(
forall r,
nontemp_preg r =
true ->
rs'#
r =
rs#
r) ->
agree (
undef_temps ms)
sp rs'.
Proof.
Lemma agree_undef_move:
forall ms sp rs rs',
agree ms sp rs ->
(
forall r,
important_preg r =
true ->
r <>
ST0 ->
rs'#
r =
rs#
r) ->
agree (
undef_move ms)
sp rs'.
Proof.
Lemma agree_set_undef_mreg:
forall ms sp rs r v rs',
agree ms sp rs ->
Val.lessdef v (
rs'#(
preg_of r)) ->
(
forall r',
nontemp_preg r' =
true ->
r' <>
preg_of r ->
rs'#
r' =
rs#
r') ->
agree (
Regmap.set r v (
undef_temps ms))
sp rs'.
Proof.
Lemma agree_set_undef_move_mreg:
forall ms sp rs r v rs',
agree ms sp rs ->
Val.lessdef v (
rs'#(
preg_of r)) ->
(
forall r',
important_preg r' =
true /\
r' <>
ST0 ->
r' <>
preg_of r ->
rs'#
r' =
rs#
r') ->
agree (
Regmap.set r v (
undef_move ms))
sp rs'.
Proof.
Useful properties of the PC register.
Lemma nextinstr_inv:
forall r rs,
r <>
PC ->
(
nextinstr rs)#
r =
rs#
r.
Proof.
intros.
unfold nextinstr.
apply Pregmap.gso.
red;
intro;
subst.
auto.
Qed.
Lemma nextinstr_inv2:
forall r rs,
nontemp_preg r =
true ->
(
nextinstr rs)#
r =
rs#
r.
Proof.
intros.
apply nextinstr_inv.
red;
intro;
subst;
discriminate.
Qed.
Lemma nextinstr_set_preg:
forall rs m v,
(
nextinstr (
rs#(
preg_of m) <-
v))#
PC =
Val.add rs#
PC Vone.
Proof.
Lemma nextinstr_nf_inv:
forall r rs,
match r with PC =>
False |
CR _ =>
False |
_ =>
True end ->
(
nextinstr_nf rs)#
r =
rs#
r.
Proof.
intros.
unfold nextinstr_nf.
rewrite nextinstr_inv.
simpl.
repeat rewrite Pregmap.gso;
auto.
red;
intro;
subst;
contradiction.
red;
intro;
subst;
contradiction.
red;
intro;
subst;
contradiction.
red;
intro;
subst;
contradiction.
red;
intro;
subst;
contradiction.
Qed.
Lemma nextinstr_nf_inv1:
forall r rs,
important_preg r =
true -> (
nextinstr_nf rs)#
r =
rs#
r.
Proof.
intros.
apply nextinstr_nf_inv.
unfold important_preg in H.
destruct r;
auto;
congruence.
Qed.
Lemma nextinstr_nf_inv2:
forall r rs,
nontemp_preg r =
true -> (
nextinstr_nf rs)#
r =
rs#
r.
Proof.
intros.
apply nextinstr_nf_inv.
unfold nontemp_preg in H.
destruct r;
auto;
congruence.
Qed.
Lemma nextinstr_nf_set_preg:
forall rs m v,
(
nextinstr_nf (
rs#(
preg_of m) <-
v))#
PC =
Val.add rs#
PC Vone.
Proof.
Connection between Mach and Asm calling conventions for external
functions.
Lemma extcall_arg_match:
forall ms sp rs m m'
l v,
agree ms sp rs ->
Machsem.extcall_arg ms m sp l v ->
Mem.extends m m' ->
exists v',
Asm.extcall_arg rs m'
l v' /\
Val.lessdef v v'.
Proof.
intros.
inv H0.
exists (
rs#(
preg_of r));
split.
constructor.
eauto with ppcgen.
unfold load_stack in H2.
exploit Mem.loadv_extends;
eauto.
intros [
v' [
A B]].
rewrite (
sp_val _ _ _ H)
in A.
exists v';
split;
auto.
destruct ty;
econstructor;
eauto.
Qed.
Lemma extcall_args_match:
forall ms sp rs m m',
agree ms sp rs ->
Mem.extends m m' ->
forall ll vl,
list_forall2 (
Machsem.extcall_arg ms m sp)
ll vl ->
exists vl',
list_forall2 (
Asm.extcall_arg rs m')
ll vl' /\
Val.lessdef_list vl vl'.
Proof.
induction 3.
exists (@
nil val);
split;
constructor.
exploit extcall_arg_match;
eauto.
intros [
v1' [
A B]].
destruct IHlist_forall2 as [
vl' [
C D]].
exists(
v1' ::
vl');
split.
constructor;
auto.
constructor;
auto.
Qed.
Lemma extcall_arguments_match:
forall ms m sp rs sg args m',
agree ms sp rs ->
Machsem.extcall_arguments ms m sp sg args ->
Mem.extends m m' ->
exists args',
Asm.extcall_arguments rs m'
sg args' /\
Val.lessdef_list args args'.
Proof.
unfold Machsem.extcall_arguments,
Asm.extcall_arguments;
intros.
eapply extcall_args_match;
eauto.
Qed.
Translation of arguments to annotations.
Lemma annot_arg_match:
forall ms sp rs m m'
p v,
agree ms sp rs ->
Mem.extends m m' ->
Machsem.annot_arg ms m sp p v ->
exists v',
Asm.annot_arg rs m' (
transl_annot_param p)
v' /\
Val.lessdef v v'.
Proof.
intros.
inv H1;
simpl.
exists (
rs (
preg_of r));
split.
unfold preg_of.
destruct (
mreg_type r);
constructor.
eapply preg_val;
eauto.
exploit Mem.load_extends;
eauto.
intros [
v' [
A B]].
exists v';
split;
auto.
inv H.
econstructor;
eauto.
Qed.
Lemma annot_arguments_match:
forall ms sp rs m m',
agree ms sp rs ->
Mem.extends m m' ->
forall pl vl,
Machsem.annot_arguments ms m sp pl vl ->
exists vl',
Asm.annot_arguments rs m' (
map transl_annot_param pl)
vl'
/\
Val.lessdef_list vl vl'.
Proof.
induction 3;
intros.
exists (@
nil val);
split.
constructor.
constructor.
exploit annot_arg_match;
eauto.
intros [
v1' [
A B]].
destruct IHlist_forall2 as [
vl' [
C D]].
exists (
v1' ::
vl');
split;
constructor;
auto.
Qed.
Execution of straight-line code
Section STRAIGHTLINE.
Variable ge:
genv.
Variable fn:
code.
Straight-line code is composed of processor instructions that execute
in sequence (no branches, no function calls and returns).
The following inductive predicate relates the machine states
before and after executing a straight-line sequence of instructions.
Instructions are taken from the first list instead of being fetched
from memory.
Inductive exec_straight:
code ->
regset ->
mem ->
code ->
regset ->
mem ->
Prop :=
|
exec_straight_one:
forall i1 c rs1 m1 rs2 m2,
exec_instr ge fn i1 rs1 m1 =
Next rs2 m2 ->
rs2#
PC =
Val.add rs1#
PC Vone ->
exec_straight (
i1 ::
c)
rs1 m1 c rs2 m2
|
exec_straight_step:
forall i c rs1 m1 rs2 m2 c'
rs3 m3,
exec_instr ge fn i rs1 m1 =
Next rs2 m2 ->
rs2#
PC =
Val.add rs1#
PC Vone ->
exec_straight c rs2 m2 c'
rs3 m3 ->
exec_straight (
i ::
c)
rs1 m1 c'
rs3 m3.
Lemma exec_straight_trans:
forall c1 rs1 m1 c2 rs2 m2 c3 rs3 m3,
exec_straight c1 rs1 m1 c2 rs2 m2 ->
exec_straight c2 rs2 m2 c3 rs3 m3 ->
exec_straight c1 rs1 m1 c3 rs3 m3.
Proof.
Lemma exec_straight_two:
forall i1 i2 c rs1 m1 rs2 m2 rs3 m3,
exec_instr ge fn i1 rs1 m1 =
Next rs2 m2 ->
exec_instr ge fn i2 rs2 m2 =
Next rs3 m3 ->
rs2#
PC =
Val.add rs1#
PC Vone ->
rs3#
PC =
Val.add rs2#
PC Vone ->
exec_straight (
i1 ::
i2 ::
c)
rs1 m1 c rs3 m3.
Proof.
Lemma exec_straight_three:
forall i1 i2 i3 c rs1 m1 rs2 m2 rs3 m3 rs4 m4,
exec_instr ge fn i1 rs1 m1 =
Next rs2 m2 ->
exec_instr ge fn i2 rs2 m2 =
Next rs3 m3 ->
exec_instr ge fn i3 rs3 m3 =
Next rs4 m4 ->
rs2#
PC =
Val.add rs1#
PC Vone ->
rs3#
PC =
Val.add rs2#
PC Vone ->
rs4#
PC =
Val.add rs3#
PC Vone ->
exec_straight (
i1 ::
i2 ::
i3 ::
c)
rs1 m1 c rs4 m4.
Proof.
Correctness of IA32 constructor functions
Smart constructor for moves.
Lemma mk_mov_correct:
forall rd rs k c rs1 m,
mk_mov rd rs k =
OK c ->
exists rs2,
exec_straight c rs1 m k rs2 m
/\
rs2#
rd =
rs1#
rs
/\
forall r,
important_preg r =
true ->
r <>
ST0 ->
r <>
rd ->
rs2#
r =
rs1#
r.
Proof.
Smart constructor for shifts
Ltac SRes :=
match goal with
| [ |-
nextinstr _ _ =
_ ] =>
rewrite nextinstr_inv; [
auto |
auto with ppcgen]
| [ |-
nextinstr_nf _ _ =
_ ] =>
rewrite nextinstr_nf_inv; [
auto |
auto with ppcgen]
| [ |-
Pregmap.get ?
x (
Pregmap.set ?
x _ _) =
_ ] =>
rewrite Pregmap.gss;
auto
| [ |-
Pregmap.set ?
x _ _ ?
x =
_ ] =>
rewrite Pregmap.gss;
auto
| [ |-
Pregmap.get _ (
Pregmap.set _ _ _) =
_ ] =>
rewrite Pregmap.gso; [
auto |
auto with ppcgen]
| [ |-
Pregmap.set _ _ _ _ =
_ ] =>
rewrite Pregmap.gso; [
auto |
auto with ppcgen]
end.
Ltac SOther :=
match goal with
| [ |-
nextinstr _ _ =
_ ] =>
rewrite nextinstr_inv; [
auto |
auto with ppcgen]
| [ |-
nextinstr_nf _ _ =
_ ] =>
rewrite nextinstr_nf_inv2; [
auto |
auto with ppcgen]
| [ |-
Pregmap.get ?
x (
Pregmap.set ?
x _ _) =
_ ] =>
rewrite Pregmap.gss;
auto
| [ |-
Pregmap.set ?
x _ _ ?
x =
_ ] =>
rewrite Pregmap.gss;
auto
| [ |-
Pregmap.get _ (
Pregmap.set _ _ _) =
_ ] =>
rewrite Pregmap.gso; [
auto |
auto with ppcgen]
| [ |-
Pregmap.set _ _ _ _ =
_ ] =>
rewrite Pregmap.gso; [
auto |
auto with ppcgen]
end.
Lemma mk_shift_correct:
forall sinstr ssem r1 r2 k c rs1 m,
mk_shift sinstr r1 r2 k =
OK c ->
(
forall r c rs m,
exec_instr ge c (
sinstr r)
rs m =
Next (
nextinstr_nf (
rs#
r <- (
ssem rs#
r rs#
ECX)))
m) ->
exists rs2,
exec_straight c rs1 m k rs2 m
/\
rs2#
r1 =
ssem rs1#
r1 rs1#
r2
/\
forall r,
nontemp_preg r =
true ->
r <>
r1 ->
rs2#
r =
rs1#
r.
Proof.
Parallel move 2
Lemma mk_mov2_correct:
forall src1 dst1 src2 dst2 k rs m,
dst1 <>
dst2 ->
exists rs',
exec_straight (
mk_mov2 src1 dst1 src2 dst2 k)
rs m k rs'
m
/\
rs'#
dst1 =
rs#
src1
/\
rs'#
dst2 =
rs#
src2
/\
forall r,
r <>
PC ->
r <>
dst1 ->
r <>
dst2 ->
rs'#
r =
rs#
r.
Proof.
intros.
unfold mk_mov2.
destruct (
ireg_eq src1 dst1).
subst.
econstructor;
split.
apply exec_straight_one.
simpl;
eauto.
auto.
split.
repeat SRes.
split.
repeat SRes.
intros;
repeat SOther.
destruct (
ireg_eq src2 dst2).
subst.
econstructor;
split.
apply exec_straight_one.
simpl;
eauto.
auto.
split.
repeat SRes.
split.
repeat SRes.
intros;
repeat SOther.
destruct (
ireg_eq src2 dst1).
destruct (
ireg_eq src1 dst2).
subst.
econstructor;
split.
apply exec_straight_one.
simpl;
eauto.
auto.
split.
repeat SRes.
split.
repeat SRes.
intros;
repeat SOther.
subst.
econstructor;
split.
eapply exec_straight_two.
simpl;
eauto.
simpl;
eauto.
auto.
auto.
split.
repeat SRes.
split.
repeat SRes.
intros;
repeat SOther.
subst.
econstructor;
split.
eapply exec_straight_two.
simpl;
eauto.
simpl;
eauto.
auto.
auto.
split.
repeat SRes.
split.
repeat SRes.
intros;
repeat SOther.
Qed.
Smart constructor for division
Lemma mk_div_correct:
forall mkinstr dsem msem r1 r2 k c (
rs1:
regset)
m vq vr,
mk_div mkinstr r1 r2 k =
OK c ->
(
forall r c rs m,
exec_instr ge c (
mkinstr r)
rs m =
let vn :=
rs#
EAX in let vd := (
rs#
EDX <-
Vundef)#
r in
match dsem vn vd,
msem vn vd with
|
Some vq,
Some vr =>
Next (
nextinstr_nf (
rs#
EAX <-
vq #
EDX <-
vr))
m
|
_,
_ =>
Stuck
end) ->
dsem rs1#
r1 rs1#
r2 =
Some vq ->
msem rs1#
r1 rs1#
r2 =
Some vr ->
exists rs2,
exec_straight c rs1 m k rs2 m
/\
rs2#
r1 =
vq
/\
forall r,
nontemp_preg r =
true ->
r <>
r1 ->
rs2#
r =
rs1#
r.
Proof.
unfold mk_div;
intros.
destruct (
ireg_eq r1 EAX).
destruct (
ireg_eq r2 EDX);
monadInv H.
econstructor.
split.
eapply exec_straight_two.
simpl;
eauto.
rewrite H0.
change (
nextinstr rs1 #
ECX <- (
rs1 EDX)
EAX)
with (
rs1#
EAX).
change ((
nextinstr rs1 #
ECX <- (
rs1 EDX)) #
EDX <-
Vundef ECX)
with (
rs1#
EDX).
rewrite H1.
rewrite H2.
eauto.
auto.
auto.
split.
SRes.
intros.
repeat SOther.
econstructor.
split.
eapply exec_straight_one.
rewrite H0.
replace (
rs1 #
EDX <-
Vundef r2)
with (
rs1 r2).
rewrite H1;
rewrite H2.
eauto.
symmetry.
SOther.
auto.
split.
SRes.
intros.
repeat SOther.
monadInv H.
set (
rs2 :=
nextinstr (
rs1#
XMM7 <- (
rs1#
EAX))).
exploit (
mk_mov2_correct r1 EAX r2 ECX).
congruence.
instantiate (1 :=
rs2).
intros [
rs3 [
A [
B [
C D]]]].
econstructor;
split.
apply exec_straight_step with rs2 m;
auto.
eapply exec_straight_trans.
eexact A.
eapply exec_straight_three.
rewrite H0.
replace (
rs3 EAX)
with (
rs1 r1).
replace (
rs3 #
EDX <-
Vundef ECX)
with (
rs1 r2).
rewrite H1;
rewrite H2.
eauto.
simpl;
eauto.
simpl;
eauto.
auto.
auto.
auto.
split.
repeat SRes.
intros.
destruct (
preg_eq r EAX).
subst.
repeat SRes.
rewrite D;
auto with ppcgen.
repeat SOther.
rewrite D;
auto with ppcgen.
unfold rs2;
repeat SOther.
Qed.
Smart constructor for modulus
Lemma mk_mod_correct:
forall mkinstr dsem msem r1 r2 k c (
rs1:
regset)
m vq vr,
mk_mod mkinstr r1 r2 k =
OK c ->
(
forall r c rs m,
exec_instr ge c (
mkinstr r)
rs m =
let vn :=
rs#
EAX in let vd := (
rs#
EDX <-
Vundef)#
r in
match dsem vn vd,
msem vn vd with
|
Some vq,
Some vr =>
Next (
nextinstr_nf (
rs#
EAX <-
vq #
EDX <-
vr))
m
|
_,
_ =>
Stuck
end) ->
dsem rs1#
r1 rs1#
r2 =
Some vq ->
msem rs1#
r1 rs1#
r2 =
Some vr ->
exists rs2,
exec_straight c rs1 m k rs2 m
/\
rs2#
r1 =
vr
/\
forall r,
nontemp_preg r =
true ->
r <>
r1 ->
rs2#
r =
rs1#
r.
Proof.
unfold mk_mod;
intros.
destruct (
ireg_eq r1 EAX).
destruct (
ireg_eq r2 EDX);
monadInv H.
econstructor.
split.
eapply exec_straight_three.
simpl;
eauto.
rewrite H0.
change (
nextinstr rs1 #
ECX <- (
rs1 EDX)
EAX)
with (
rs1#
EAX).
change ((
nextinstr rs1 #
ECX <- (
rs1 EDX)) #
EDX <-
Vundef ECX)
with (
rs1#
EDX).
rewrite H1.
rewrite H2.
eauto.
simpl;
eauto.
auto.
auto.
auto.
split.
SRes.
intros.
repeat SOther.
econstructor.
split.
eapply exec_straight_two.
rewrite H0.
replace (
rs1 #
EDX <-
Vundef r2)
with (
rs1 r2).
rewrite H1;
rewrite H2.
eauto.
symmetry.
SOther.
simpl;
eauto.
auto.
auto.
split.
SRes.
intros.
repeat SOther.
monadInv H.
set (
rs2 :=
nextinstr (
rs1#
XMM7 <- (
rs1#
EAX))).
exploit (
mk_mov2_correct r1 EAX r2 ECX).
congruence.
instantiate (1 :=
rs2).
intros [
rs3 [
A [
B [
C D]]]].
econstructor;
split.
apply exec_straight_step with rs2 m;
auto.
eapply exec_straight_trans.
eexact A.
eapply exec_straight_three.
rewrite H0.
replace (
rs3 EAX)
with (
rs1 r1).
replace (
rs3 #
EDX <-
Vundef ECX)
with (
rs1 r2).
rewrite H1;
rewrite H2.
eauto.
simpl;
eauto.
simpl;
eauto.
auto.
auto.
auto.
split.
repeat SRes.
intros.
destruct (
preg_eq r EAX).
subst.
repeat SRes.
rewrite D;
auto with ppcgen.
repeat SOther.
rewrite D;
auto with ppcgen.
unfold rs2;
repeat SOther.
Qed.
Remark divs_mods_exist:
forall v1 v2,
match Val.divs v1 v2,
Val.mods v1 v2 with
|
Some _,
Some _ =>
True
|
None,
None =>
True
|
_,
_ =>
False
end.
Proof.
Remark divu_modu_exist:
forall v1 v2,
match Val.divu v1 v2,
Val.modu v1 v2 with
|
Some _,
Some _ =>
True
|
None,
None =>
True
|
_,
_ =>
False
end.
Proof.
intros.
unfold Val.divu,
Val.modu.
destruct v1;
auto.
destruct v2;
auto.
destruct (
Int.eq i0 Int.zero);
auto.
Qed.
Smart constructor for shrx
Lemma mk_shrximm_correct:
forall r1 n k c (
rs1:
regset)
v m,
mk_shrximm r1 n k =
OK c ->
Val.shrx (
rs1#
r1) (
Vint n) =
Some v ->
exists rs2,
exec_straight c rs1 m k rs2 m
/\
rs2#
r1 =
v
/\
forall r,
nontemp_preg r =
true ->
r <>
r1 ->
rs2#
r =
rs1#
r.
Proof.
Smart constructor for integer conversions
Lemma mk_intconv_correct:
forall mk sem rd rs k c rs1 m,
mk_intconv mk rd rs k =
OK c ->
(
forall c rd rs r m,
exec_instr ge c (
mk rd rs)
r m =
Next (
nextinstr (
r#
rd <- (
sem r#
rs)))
m) ->
exists rs2,
exec_straight c rs1 m k rs2 m
/\
rs2#
rd =
sem rs1#
rs
/\
forall r,
nontemp_preg r =
true ->
r <>
rd ->
rs2#
r =
rs1#
r.
Proof.
unfold mk_intconv;
intros.
destruct (
low_ireg rs);
monadInv H.
econstructor.
split.
apply exec_straight_one.
rewrite H0.
eauto.
auto.
split.
repeat SRes.
intros.
repeat SOther.
econstructor.
split.
eapply exec_straight_two.
simpl.
eauto.
apply H0.
auto.
auto.
split.
repeat SRes.
intros.
repeat SOther.
Qed.
Smart constructor for small stores
Lemma addressing_mentions_correct:
forall a r (
rs1 rs2:
regset),
(
forall (
r':
ireg),
r' <>
r ->
rs1 r' =
rs2 r') ->
addressing_mentions a r =
false ->
eval_addrmode ge a rs1 =
eval_addrmode ge a rs2.
Proof.
intros until rs2;
intro AG.
unfold addressing_mentions,
eval_addrmode.
destruct a.
intros.
destruct (
orb_false_elim _ _ H).
unfold proj_sumbool in *.
decEq.
destruct base;
auto.
apply AG.
destruct (
ireg_eq r i);
congruence.
decEq.
destruct ofs as [[
r'
sc] | ];
auto.
rewrite AG;
auto.
destruct (
ireg_eq r r');
congruence.
Qed.
Lemma mk_smallstore_correct:
forall chunk sto addr r k c rs1 m1 m2,
mk_smallstore sto addr r k =
OK c ->
Mem.storev chunk m1 (
eval_addrmode ge addr rs1) (
rs1 r) =
Some m2 ->
(
forall c r addr rs m,
exec_instr ge c (
sto addr r)
rs m =
exec_store ge chunk m addr rs r) ->
exists rs2,
exec_straight c rs1 m1 k rs2 m2
/\
forall r,
nontemp_preg r =
true ->
rs2#
r =
rs1#
r.
Proof.
Accessing slots in the stack frame
Lemma loadind_correct:
forall (
base:
ireg)
ofs ty dst k (
rs:
regset)
c m v,
loadind base ofs ty dst k =
OK c ->
Mem.loadv (
chunk_of_type ty)
m (
Val.add rs#
base (
Vint ofs)) =
Some v ->
exists rs',
exec_straight c rs m k rs'
m
/\
rs'#(
preg_of dst) =
v
/\
forall r,
important_preg r =
true ->
r <>
preg_of dst ->
rs'#
r =
rs#
r.
Proof.
Lemma storeind_correct:
forall (
base:
ireg)
ofs ty src k (
rs:
regset)
c m m',
storeind src base ofs ty k =
OK c ->
Mem.storev (
chunk_of_type ty)
m (
Val.add rs#
base (
Vint ofs)) (
rs#(
preg_of src)) =
Some m' ->
exists rs',
exec_straight c rs m k rs'
m'
/\
forall r,
important_preg r =
true ->
r <>
ST0 ->
rs'#
r =
rs#
r.
Proof.
Translation of addressing modes
Lemma transl_addressing_mode_correct:
forall addr args am (
rs:
regset)
v,
transl_addressing addr args =
OK am ->
eval_addressing ge (
rs ESP)
addr (
List.map rs (
List.map preg_of args)) =
Some v ->
Val.lessdef v (
eval_addrmode ge am rs).
Proof.
Processor conditions and comparisons
Lemma compare_ints_spec:
forall rs v1 v2 m,
let rs' :=
nextinstr (
compare_ints v1 v2 rs m)
in
rs'#
ZF =
Val.cmpu (
Mem.valid_pointer m)
Ceq v1 v2
/\
rs'#
CF =
Val.cmpu (
Mem.valid_pointer m)
Clt v1 v2
/\
rs'#
SOF =
Val.cmp Clt v1 v2
/\ (
forall r,
nontemp_preg r =
true ->
rs'#
r =
rs#
r).
Proof.
intros. unfold rs'; unfold compare_ints.
split. auto.
split. auto.
split. auto.
intros. repeat SOther.
Qed.
Lemma int_signed_eq:
forall x y,
Int.eq x y =
zeq (
Int.signed x) (
Int.signed y).
Proof.
Lemma int_not_lt:
forall x y,
negb (
Int.lt y x) = (
Int.lt x y ||
Int.eq x y).
Proof.
Lemma int_lt_not:
forall x y,
Int.lt y x =
negb (
Int.lt x y) &&
negb (
Int.eq x y).
Proof.
Lemma int_not_ltu:
forall x y,
negb (
Int.ltu y x) = (
Int.ltu x y ||
Int.eq x y).
Proof.
Lemma int_ltu_not:
forall x y,
Int.ltu y x =
negb (
Int.ltu x y) &&
negb (
Int.eq x y).
Proof.
Lemma testcond_for_signed_comparison_correct:
forall c v1 v2 rs m b,
Val.cmp_bool c v1 v2 =
Some b ->
eval_testcond (
testcond_for_signed_comparison c)
(
nextinstr (
compare_ints v1 v2 rs m)) =
Some b.
Proof.
intros.
generalize (
compare_ints_spec rs v1 v2 m).
set (
rs' :=
nextinstr (
compare_ints v1 v2 rs m)).
intros [
A [
B [
C D]]].
destruct v1;
destruct v2;
simpl in H;
inv H.
unfold eval_testcond.
rewrite A;
rewrite B;
rewrite C.
unfold Val.cmp,
Val.cmpu.
destruct c;
simpl.
destruct (
Int.eq i i0);
auto.
destruct (
Int.eq i i0);
auto.
destruct (
Int.lt i i0);
auto.
rewrite int_not_lt.
destruct (
Int.lt i i0);
simpl;
destruct (
Int.eq i i0);
auto.
rewrite (
int_lt_not i i0).
destruct (
Int.lt i i0);
destruct (
Int.eq i i0);
reflexivity.
destruct (
Int.lt i i0);
reflexivity.
Qed.
Lemma testcond_for_unsigned_comparison_correct:
forall c v1 v2 rs m b,
Val.cmpu_bool (
Mem.valid_pointer m)
c v1 v2 =
Some b ->
eval_testcond (
testcond_for_unsigned_comparison c)
(
nextinstr (
compare_ints v1 v2 rs m)) =
Some b.
Proof.
intros.
generalize (
compare_ints_spec rs v1 v2 m).
set (
rs' :=
nextinstr (
compare_ints v1 v2 rs m)).
intros [
A [
B [
C D]]].
unfold eval_testcond.
rewrite A;
rewrite B;
rewrite C.
unfold Val.cmpu,
Val.cmp.
destruct v1;
destruct v2;
simpl in H;
inv H.
destruct c;
simpl;
auto.
destruct (
Int.eq i i0);
reflexivity.
destruct (
Int.eq i i0);
auto.
destruct (
Int.ltu i i0);
auto.
rewrite int_not_ltu.
destruct (
Int.ltu i i0);
simpl;
destruct (
Int.eq i i0);
auto.
rewrite (
int_ltu_not i i0).
destruct (
Int.ltu i i0);
destruct (
Int.eq i i0);
reflexivity.
destruct (
Int.ltu i i0);
reflexivity.
destruct (
Int.eq i Int.zero)
as []
_eqn;
try discriminate.
destruct c;
simpl in *;
inv H1.
rewrite Heqb1;
reflexivity.
rewrite Heqb1;
reflexivity.
destruct (
Int.eq i0 Int.zero)
as []
_eqn;
try discriminate.
destruct c;
simpl in *;
inv H1.
rewrite Heqb1;
reflexivity.
rewrite Heqb1;
reflexivity.
simpl.
destruct (
Mem.valid_pointer m b0 (
Int.unsigned i) &&
Mem.valid_pointer m b1 (
Int.unsigned i0));
try discriminate.
destruct (
zeq b0 b1).
inversion H1.
destruct c;
simpl;
auto.
destruct (
Int.eq i i0);
reflexivity.
destruct (
Int.eq i i0);
auto.
destruct (
Int.ltu i i0);
auto.
rewrite int_not_ltu.
destruct (
Int.ltu i i0);
simpl;
destruct (
Int.eq i i0);
auto.
rewrite (
int_ltu_not i i0).
destruct (
Int.ltu i i0);
destruct (
Int.eq i i0);
reflexivity.
destruct (
Int.ltu i i0);
reflexivity.
destruct c;
simpl in *;
inv H1;
reflexivity.
Qed.
Lemma compare_floats_spec:
forall rs n1 n2,
let rs' :=
nextinstr (
compare_floats (
Vfloat n1) (
Vfloat n2)
rs)
in
rs'#
ZF =
Val.of_bool (
negb (
Float.cmp Cne n1 n2))
/\
rs'#
CF =
Val.of_bool (
negb (
Float.cmp Cge n1 n2))
/\
rs'#
PF =
Val.of_bool (
negb (
Float.cmp Ceq n1 n2 ||
Float.cmp Clt n1 n2 ||
Float.cmp Cgt n1 n2))
/\ (
forall r,
nontemp_preg r =
true ->
rs'#
r =
rs#
r).
Proof.
intros. unfold rs'; unfold compare_floats.
split. auto.
split. auto.
split. auto.
intros. repeat SOther.
Qed.
Definition eval_extcond (
xc:
extcond) (
rs:
regset) :
option bool :=
match xc with
|
Cond_base c =>
eval_testcond c rs
|
Cond_and c1 c2 =>
match eval_testcond c1 rs,
eval_testcond c2 rs with
|
Some b1,
Some b2 =>
Some (
b1 &&
b2)
|
_,
_ =>
None
end
|
Cond_or c1 c2 =>
match eval_testcond c1 rs,
eval_testcond c2 rs with
|
Some b1,
Some b2 =>
Some (
b1 ||
b2)
|
_,
_ =>
None
end
end.
Definition swap_floats {
A:
Type} (
c:
comparison) (
n1 n2:
A) :
A :=
match c with
|
Clt |
Cle =>
n2
|
Ceq |
Cne |
Cgt |
Cge =>
n1
end.
Lemma testcond_for_float_comparison_correct:
forall c n1 n2 rs,
eval_extcond (
testcond_for_condition (
Ccompf c))
(
nextinstr (
compare_floats (
Vfloat (
swap_floats c n1 n2))
(
Vfloat (
swap_floats c n2 n1))
rs)) =
Some(
Float.cmp c n1 n2).
Proof.
Lemma testcond_for_neg_float_comparison_correct:
forall c n1 n2 rs,
eval_extcond (
testcond_for_condition (
Cnotcompf c))
(
nextinstr (
compare_floats (
Vfloat (
swap_floats c n1 n2))
(
Vfloat (
swap_floats c n2 n1))
rs)) =
Some(
negb(
Float.cmp c n1 n2)).
Proof.
Remark swap_floats_commut:
forall c x y,
swap_floats c (
Vfloat x) (
Vfloat y) =
Vfloat (
swap_floats c x y).
Proof.
intros. destruct c; auto.
Qed.
Remark compare_floats_inv:
forall vx vy rs r,
r <>
CR ZF ->
r <>
CR CF ->
r <>
CR PF ->
r <>
CR SOF ->
compare_floats vx vy rs r =
rs r.
Proof.
intros.
assert (
DFL:
undef_regs (
CR ZF ::
CR CF ::
CR PF ::
CR SOF ::
nil)
rs r =
rs r).
simpl.
repeat SOther.
unfold compare_floats;
destruct vx;
destruct vy;
auto.
repeat SOther.
Qed.
Lemma transl_cond_correct:
forall cond args k c rs m,
transl_cond cond args k =
OK c ->
exists rs',
exec_straight c rs m k rs'
m
/\
match eval_condition cond (
map rs (
map preg_of args))
m with
|
None =>
True
|
Some b =>
eval_extcond (
testcond_for_condition cond)
rs' =
Some b
end
/\
forall r,
nontemp_preg r =
true ->
rs'#
r =
rs r.
Proof.
Remark eval_testcond_nextinstr:
forall c rs,
eval_testcond c (
nextinstr rs) =
eval_testcond c rs.
Proof.
intros.
unfold eval_testcond.
repeat rewrite nextinstr_inv;
auto with ppcgen.
Qed.
Remark eval_testcond_set_ireg:
forall c rs r v,
eval_testcond c (
rs#(
IR r) <-
v) =
eval_testcond c rs.
Proof.
intros.
unfold eval_testcond.
repeat rewrite Pregmap.gso;
auto with ppcgen.
Qed.
Lemma mk_setcc_correct:
forall cond rd k rs1 m,
exists rs2,
exec_straight (
mk_setcc cond rd k)
rs1 m k rs2 m
/\
rs2#
rd =
Val.of_optbool(
eval_extcond cond rs1)
/\
forall r,
nontemp_preg r =
true ->
r <>
rd ->
rs2#
r =
rs1#
r.
Proof.
Translation of arithmetic operations.
Ltac ArgsInv :=
match goal with
| [
H:
Error _ =
OK _ |-
_ ] =>
discriminate
| [
H:
match ?
args with nil =>
_ |
_ ::
_ =>
_ end =
OK _ |-
_ ] =>
destruct args;
ArgsInv
| [
H:
bind _ _ =
OK _ |-
_ ] =>
monadInv H;
ArgsInv
| [
H:
assertion _ =
OK _ |-
_ ] =>
monadInv H;
subst;
ArgsInv
| [
H:
ireg_of _ =
OK _ |-
_ ] =>
simpl in *;
rewrite (
ireg_of_eq _ _ H)
in *;
clear H;
ArgsInv
| [
H:
freg_of _ =
OK _ |-
_ ] =>
simpl in *;
rewrite (
freg_of_eq _ _ H)
in *;
clear H;
ArgsInv
|
_ =>
idtac
end.
Ltac TranslOp :=
econstructor;
split;
[
apply exec_straight_one; [
simpl;
eauto |
auto ]
|
split; [
repeat SRes |
intros;
repeat SOther ]].
Lemma transl_op_correct:
forall op args res k c (
rs:
regset)
m v,
transl_op op args res k =
OK c ->
eval_operation ge (
rs#
ESP)
op (
map rs (
map preg_of args))
m =
Some v ->
exists rs',
exec_straight c rs m k rs'
m
/\
Val.lessdef v rs'#(
preg_of res)
/\
forall r,
match op with Omove =>
important_preg r =
true /\
r <>
ST0 |
_ =>
nontemp_preg r =
true end ->
r <>
preg_of res ->
rs'
r =
rs r.
Proof.
Translation of memory loads.
Lemma transl_load_correct:
forall chunk addr args dest k c (
rs:
regset)
m a v,
transl_load chunk addr args dest k =
OK c ->
eval_addressing ge (
rs#
ESP)
addr (
map rs (
map preg_of args)) =
Some a ->
Mem.loadv chunk m a =
Some v ->
exists rs',
exec_straight c rs m k rs'
m
/\
rs'#(
preg_of dest) =
v
/\
forall r,
nontemp_preg r =
true ->
r <>
preg_of dest ->
rs'#
r =
rs#
r.
Proof.
Lemma transl_store_correct:
forall chunk addr args src k c (
rs:
regset)
m a m',
transl_store chunk addr args src k =
OK c ->
eval_addressing ge (
rs#
ESP)
addr (
map rs (
map preg_of args)) =
Some a ->
Mem.storev chunk m a (
rs (
preg_of src)) =
Some m' ->
exists rs',
exec_straight c rs m k rs'
m'
/\
forall r,
nontemp_preg r =
true ->
rs'#
r =
rs#
r.
Proof.
End STRAIGHTLINE.