Require Import Coqlib.
Require Import Maps.
Require Import AST.
Require Import Integers.
Require Import Values.
Require Import Memory.
Require Import Globalenvs.
Require Import Events.
Require Import Smallstep.
Require Import Op.
Require Import Locations.
Require Import Conventions.
Require Stacklayout.
Require Import Mach.
Require Import Linking.
Require Import Machblock.
Require Import Machblockgen.
Require Import ForwardSimulationBlock.
Require Import Lia.
Ltac subst_is_trans_code H :=
rewrite is_trans_code_inv in H;
rewrite <-
H in * |- *;
rewrite <-
is_trans_code_inv in H.
Definition inv_trans_rao (
rao:
function ->
code ->
ptrofs ->
Prop) (
f:
Mach.function) (
c:
Mach.code) :=
rao (
transf_function f) (
trans_code c).
Definition match_prog (
p:
Mach.program) (
tp:
Machblock.program) :=
match_program (
fun _
f tf =>
tf =
transf_fundef f)
eq p tp.
Lemma transf_program_match:
forall p tp,
transf_program p =
tp ->
match_prog p tp.
Proof.
Definition trans_stackframe (
msf:
Mach.stackframe) :
stackframe :=
match msf with
|
Mach.Stackframe f sp retaddr c =>
Stackframe f sp retaddr (
trans_code c)
end.
Fixpoint trans_stack (
mst:
list Mach.stackframe) :
list stackframe :=
match mst with
|
nil =>
nil
|
msf ::
mst0 => (
trans_stackframe msf) :: (
trans_stack mst0)
end.
Definition trans_state (
ms:
Mach.state):
state :=
match ms with
|
Mach.State s f sp c rs m =>
State (
trans_stack s)
f sp (
trans_code c)
rs m
|
Mach.Callstate s f rs m =>
Callstate (
trans_stack s)
f rs m
|
Mach.Returnstate s rs m =>
Returnstate (
trans_stack s)
rs m
end.
Section PRESERVATION.
Local Open Scope nat_scope.
Variable prog:
Mach.program.
Variable tprog:
Machblock.program.
Hypothesis TRANSF:
match_prog prog tprog.
Let ge :=
Genv.globalenv prog.
Let tge :=
Genv.globalenv tprog.
Variable rao:
function ->
code ->
ptrofs ->
Prop.
Definition match_states:
Mach.state ->
state ->
Prop
:=
ForwardSimulationBlock.match_states (
Mach.semantics (
inv_trans_rao rao)
prog) (
Machblock.semantics rao tprog)
trans_state.
Lemma match_states_trans_state s1:
match_states s1 (
trans_state s1).
Proof.
Local Hint Resolve match_states_trans_state:
core.
Lemma symbols_preserved:
forall s,
Genv.find_symbol tge s =
Genv.find_symbol ge s.
Proof.
Lemma senv_preserved:
Senv.equiv ge tge.
Proof.
Lemma init_mem_preserved:
forall m,
Genv.init_mem prog =
Some m ->
Genv.init_mem tprog =
Some m.
Proof.
Lemma prog_main_preserved:
prog_main tprog =
prog_main prog.
Proof.
Lemma functions_translated:
forall b f,
Genv.find_funct_ptr ge b =
Some f ->
exists tf,
Genv.find_funct_ptr tge b =
Some tf /\
transf_fundef f =
tf.
Proof.
Lemma find_function_ptr_same:
forall s rs,
Mach.find_function_ptr ge s rs =
find_function_ptr tge s rs.
Proof.
Lemma find_funct_ptr_same:
forall f f0,
Genv.find_funct_ptr ge f =
Some (
Internal f0) ->
Genv.find_funct_ptr tge f =
Some (
Internal (
transf_function f0)).
Proof.
Lemma find_funct_ptr_same_external:
forall f f0,
Genv.find_funct_ptr ge f =
Some (
External f0) ->
Genv.find_funct_ptr tge f =
Some (
External f0).
Proof.
Lemma parent_sp_preserved:
forall s,
Mach.parent_sp s =
parent_sp (
trans_stack s).
Proof.
Lemma parent_ra_preserved:
forall s,
Mach.parent_ra s =
parent_ra (
trans_stack s).
Proof.
Lemma external_call_preserved:
forall ef args m t res m',
external_call ef ge args m t res m' ->
external_call ef tge args m t res m'.
Proof.
Lemma Mach_find_label_split l i c c':
Mach.find_label l (
i ::
c) =
Some c' ->
(
i=
Mlabel l /\
c' =
c) \/ (
i <>
Mlabel l /\
Mach.find_label l c =
Some c').
Proof.
intros H.
destruct i;
try (
constructor 2;
split;
auto;
discriminate ).
destruct (
peq l0 l)
as [
P|
P].
-
constructor.
subst l0;
split;
auto.
revert H.
unfold Mach.find_label.
cbn.
rewrite peq_true.
intros H;
injection H;
auto.
-
constructor 2.
split.
+
intro F.
injection F.
intros.
contradict P;
auto.
+
revert H.
unfold Mach.find_label.
cbn.
rewrite peq_false;
auto.
Qed.
Lemma find_label_is_end_block_not_label i l c bl:
is_end_block (
trans_inst i)
bl ->
is_trans_code c bl ->
i <>
Mlabel l ->
find_label l (
add_to_new_bblock (
trans_inst i) ::
bl) =
find_label l bl.
Proof.
intros H H0 H1.
unfold find_label.
remember (
is_label l _)
as b.
replace b with false;
auto.
subst;
unfold is_label.
destruct i;
cbn in * |- *;
try (
destruct (
in_dec l nil);
intuition).
inversion H.
destruct (
in_dec l (
l0::
nil))
as [
H6|
H6];
auto.
cbn in H6;
intuition try congruence.
Qed.
Lemma find_label_at_begin l bh bl:
In l (
header bh)
->
find_label l (
bh ::
bl) =
Some (
bh::
bl).
Proof.
Lemma find_label_add_label_diff l bh bl:
~(
In l (
header bh)) ->
find_label l (
bh::
bl) =
find_label l bl.
Proof.
Definition concat (
h:
list label) (
c:
code):
code :=
match c with
|
nil => {|
header :=
h;
body :=
nil;
exit :=
None |}::
nil
|
b::
c' => {|
header :=
h ++ (
header b);
body :=
body b;
exit :=
exit b |}::
c'
end.
Lemma find_label_transcode_preserved:
forall l c c',
Mach.find_label l c =
Some c' ->
exists h,
In l h /\
find_label l (
trans_code c) =
Some (
concat h (
trans_code c')).
Proof.
Lemma find_label_preserved:
forall l f c,
Mach.find_label l (
Mach.fn_code f) =
Some c ->
exists h,
In l h /\
find_label l (
fn_code (
transf_function f)) =
Some (
concat h (
trans_code c)).
Proof.
Lemma mem_free_preserved:
forall m stk f,
Mem.free m stk 0 (
Mach.fn_stacksize f) =
Mem.free m stk 0 (
fn_stacksize (
transf_function f)).
Proof.
intros. auto.
Qed.
Local Hint Resolve symbols_preserved senv_preserved init_mem_preserved prog_main_preserved functions_translated
parent_sp_preserved:
core.
Definition dist_end_block_code (
c:
Mach.code) :=
match trans_code c with
|
nil => 0
|
bh::_ => (
size bh-1)%
nat
end.
Definition dist_end_block (
s:
Mach.state):
nat :=
match s with
|
Mach.State _ _ _
c _ _ =>
dist_end_block_code c
| _ => 0
end.
Local Hint Resolve exec_nil_body exec_cons_body:
core.
Local Hint Resolve exec_MBgetstack exec_MBsetstack exec_MBgetparam exec_MBop exec_MBload exec_MBstore:
core.
Lemma size_add_label l bh:
size (
add_label l bh) =
size bh + 1.
Proof.
Lemma size_add_basic bi bh:
header bh =
nil ->
size (
add_basic bi bh) =
size bh + 1.
Proof.
Lemma size_add_to_newblock i:
size (
add_to_new_bblock i) = 1.
Proof.
destruct i; auto.
Qed.
Lemma dist_end_block_code_simu_mid_block i c:
dist_end_block_code (
i::
c) <> 0 ->
(
dist_end_block_code (
i::
c) =
Datatypes.S (
dist_end_block_code c)).
Proof.
Local Hint Resolve dist_end_block_code_simu_mid_block:
core.
Lemma size_nonzero c b bl:
is_trans_code c (
b ::
bl) ->
size b <> 0.
Proof.
Inductive is_header:
list label ->
Mach.code ->
Mach.code ->
Prop :=
|
header_empty :
is_header nil nil nil
|
header_not_label i c: (
forall l,
i <>
Mlabel l) ->
is_header nil (
i::
c) (
i::
c)
|
header_is_label l h c c0:
is_header h c c0 ->
is_header (
l::
h) ((
Mlabel l)::
c)
c0
.
Inductive is_body:
list basic_inst ->
Mach.code ->
Mach.code ->
Prop :=
|
body_empty :
is_body nil nil nil
|
body_not_bi i c: (
forall bi, (
trans_inst i) <> (
MB_basic bi)) ->
is_body nil (
i::
c) (
i::
c)
|
body_is_bi i lbi c0 c1 bi: (
trans_inst i) =
MB_basic bi ->
is_body lbi c0 c1 ->
is_body (
bi::
lbi) (
i::
c0)
c1
.
Inductive is_exit:
option control_flow_inst ->
Mach.code ->
Mach.code ->
Prop :=
|
exit_empty:
is_exit None nil nil
|
exit_not_cfi i c: (
forall cfi, (
trans_inst i) <>
MB_cfi cfi) ->
is_exit None (
i::
c) (
i::
c)
|
exit_is_cfi i c cfi: (
trans_inst i) =
MB_cfi cfi ->
is_exit (
Some cfi) (
i::
c)
c
.
Lemma Mlabel_is_not_basic i:
forall bi,
trans_inst i =
MB_basic bi ->
forall l,
i <>
Mlabel l.
Proof.
intros.
unfold trans_inst in H.
destruct i;
congruence.
Qed.
Lemma Mlabel_is_not_cfi i:
forall cfi,
trans_inst i =
MB_cfi cfi ->
forall l,
i <>
Mlabel l.
Proof.
intros.
unfold trans_inst in H.
destruct i;
congruence.
Qed.
Lemma MBbasic_is_not_cfi i:
forall cfi,
trans_inst i =
MB_cfi cfi ->
forall bi,
trans_inst i <>
MB_basic bi.
Proof.
Local Hint Resolve Mlabel_is_not_cfi:
core.
Local Hint Resolve MBbasic_is_not_cfi:
core.
Lemma add_to_new_block_is_label i:
header (
add_to_new_bblock (
trans_inst i)) <>
nil ->
exists l,
i =
Mlabel l.
Proof.
Local Hint Resolve Mlabel_is_not_basic:
core.
Lemma trans_code_decompose c:
forall b bl,
is_trans_code c (
b::
bl) ->
exists c0 c1 c2,
is_header (
header b)
c c0 /\
is_body (
body b)
c0 c1 /\
is_exit (
exit b)
c1 c2 /\
is_trans_code c2 bl.
Proof.
induction c as [|
i c].
{
intros b bl H;
inversion H. }
intros b bl H;
remember (
trans_inst i)
as ti.
destruct ti as [
lbl|
bi|
cfi];
inversion H as [|
d0 d1 d2 H0 H1|
 |];
subst;
try (
rewrite <-
Heqti in * |- *);
cbn in * |- *;
try congruence.
+
inversion H1;
subst.
inversion H0;
subst.
assert (
X:
i=
Mlabel lbl). {
destruct i;
cbn in Heqti;
congruence. }
subst.
repeat econstructor;
eauto.
+
exploit IHc;
eauto.
intros (
c0 &
c1 &
c2 &
H1 &
H2 &
H3 &
H4).
repeat econstructor;
eauto.
+
inversion H1;
subst.
lapply (
Mlabel_is_not_basic i bi);
auto.
intro H2.
-
inversion H0;
subst.
assert (
X:(
trans_inst i) =
MB_basic bi ). {
repeat econstructor;
congruence. }
repeat econstructor;
congruence.
-
exists (
i::
c),
c,
c.
repeat econstructor;
eauto;
inversion H0;
subst;
repeat econstructor;
cbn;
try congruence.
*
exploit (
add_to_new_block_is_label i0);
eauto.
intros (
l &
H8);
subst;
cbn;
congruence.
*
exploit H3;
eauto.
*
exploit (
add_to_new_block_is_label i0);
eauto.
intros (
l &
H8);
subst;
cbn;
congruence.
+
inversion H1;
subst.
exploit IHc;
eauto.
intros (
c0 &
c1 &
c2 &
H3 &
H4 &
H5 &
H6).
exists (
i::
c0),
c1,
c2.
repeat econstructor;
eauto.
rewrite H2 in H3.
inversion H3;
econstructor;
eauto.
+
inversion H1;
subst;
repeat econstructor;
eauto.
Qed.
Lemma step_simu_header st f sp rs m s c h c' t:
is_header h c c' ->
starN (
Mach.step (
inv_trans_rao rao)) (
Genv.globalenv prog) (
length h) (
Mach.State st f sp c rs m)
t s ->
s =
Mach.State st f sp c' rs m /\
t =
E0.
Proof.
induction 1; cbn; intros hs; try (inversion hs; tauto).
inversion hs as [|n1 s1 t1 t2 s2 t3 s3 H1]. inversion H1. subst. auto.
Qed.
Lemma step_simu_basic_step (
i:
Mach.instruction) (
bi:
basic_inst) (
c:
Mach.code)
s f sp rs m (
t:
trace) (
s':
Mach.state):
trans_inst i =
MB_basic bi ->
Mach.step (
inv_trans_rao rao)
ge (
Mach.State s f sp (
i::
c)
rs m)
t s' ->
exists rs' m',
s'=
Mach.State s f sp c rs' m' /\
t=
E0 /\
basic_step tge (
trans_stack s)
f sp rs m bi rs' m'.
Proof.
Lemma star_step_simu_body_step s f sp c bdy c':
is_body bdy c c' ->
forall rs m t s',
starN (
Mach.step (
inv_trans_rao rao))
ge (
length bdy) (
Mach.State s f sp c rs m)
t s' ->
exists rs' m',
s'=
Mach.State s f sp c' rs' m' /\
t=
E0 /\
body_step tge (
trans_stack s)
f sp bdy rs m rs' m'.
Proof.
induction 1;
cbn.
+
intros.
inversion H.
exists rs.
exists m.
auto.
+
intros.
inversion H0.
exists rs.
exists m.
auto.
+
intros.
inversion H1;
subst.
exploit (
step_simu_basic_step );
eauto.
destruct 1
as [
rs1 [
m1 Hs]].
destruct Hs as [
Hs1 [
Hs2 Hs3]].
destruct (
IHis_body rs1 m1 t2 s')
as [
rs2 Hb].
rewrite <-
Hs1;
eauto.
destruct Hb as [
m2 [
Hb1 [
Hb2 Hb3]]].
exists rs2,
m2.
rewrite Hs2,
Hb2;
eauto.
Qed.
Local Hint Resolve exec_MBcall exec_MBtailcall exec_MBbuiltin exec_MBgoto exec_MBcond_true exec_MBcond_false exec_MBjumptable exec_MBreturn exec_Some_exit exec_None_exit:
core.
Local Hint Resolve eval_builtin_args_preserved external_call_symbols_preserved find_funct_ptr_same:
core.
Lemma match_states_concat_trans_code st f sp c rs m h:
match_states (
Mach.State st f sp c rs m) (
State (
trans_stack st)
f sp (
concat h (
trans_code c))
rs m).
Proof.
intros;
constructor 1;
cbn.
+
intros (
t0 &
s1' &
H0)
t s'.
remember (
trans_code _)
as bl.
destruct bl as [|
bh bl].
{
rewrite <-
is_trans_code_inv in Heqbl;
inversion Heqbl;
inversion H0;
congruence. }
clear H0.
cbn;
constructor 1;
intros X;
inversion X as [
d1 d2 d3 d4 d5 d6 d7 rs' m' d10 d11 X1 X2| | | ];
subst;
cbn in * |- *;
eapply exec_bblock;
eauto;
cbn;
inversion X2 as [
cfi d1 d2 d3 H1|];
subst;
eauto;
inversion H1;
subst;
eauto.
+
intros H r;
constructor 1;
intro X;
inversion X.
Qed.
Lemma step_simu_cfi_step (
i:
Mach.instruction) (
cfi:
control_flow_inst) (
c:
Mach.code) (
blc:
code)
stk f sp rs m (
t:
trace) (
s':
Mach.state)
b:
trans_inst i =
MB_cfi cfi ->
is_trans_code c blc ->
Mach.step (
inv_trans_rao rao)
ge (
Mach.State stk f sp (
i::
c)
rs m)
t s' ->
exists s2,
cfi_step rao tge cfi (
State (
trans_stack stk)
f sp (
b::
blc)
rs m)
t s2 /\
match_states s' s2.
Proof.
Lemma step_simu_exit_step stk f sp rs m t s1 e c c' b blc:
is_exit e c c' ->
is_trans_code c' blc ->
starN (
Mach.step (
inv_trans_rao rao)) (
Genv.globalenv prog) (
length_opt e) (
Mach.State stk f sp c rs m)
t s1 ->
exists s2,
exit_step rao tge e (
State (
trans_stack stk)
f sp (
b::
blc)
rs m)
t s2 /\
match_states s1 s2.
Proof.
Lemma simu_end_block:
forall s1 t s1',
starN (
Mach.step (
inv_trans_rao rao))
ge (
Datatypes.S (
dist_end_block s1))
s1 t s1' ->
exists s2',
step rao tge (
trans_state s1)
t s2' /\
match_states s1' s2'.
Proof.
Lemma cfi_dist_end_block i c:
(
exists cfi,
trans_inst i =
MB_cfi cfi) ->
dist_end_block_code (
i ::
c) = 0.
Proof.
unfold dist_end_block_code.
intro H.
destruct H as [
cfi H].
destruct i;
cbn in H;
try(
congruence); (
remember (
trans_code _)
as bl;
rewrite <-
is_trans_code_inv in Heqbl;
inversion Heqbl;
subst;
cbn in * |- *;
try (
congruence)).
Qed.
Theorem transf_program_correct:
forward_simulation (
Mach.semantics (
inv_trans_rao rao)
prog) (
Machblock.semantics rao tprog).
Proof.
End PRESERVATION.
Auxiliary lemmas used to prove existence of a Mach return adress from a Machblock return address.
Lemma is_trans_code_monotonic i c b l:
is_trans_code c (
b::
l) ->
exists l' b',
is_trans_code (
i::
c) (
l' ++ (
b'::
l)).
Proof.
Lemma trans_code_monotonic i c b l:
(
b::
l) =
trans_code c ->
exists l' b',
trans_code (
i::
c) = (
l' ++ (
b'::
l)).
Proof.
Lemma Mach_Machblock_tail sg ros c c1 c2:
c1=(
Mcall sg ros ::
c) ->
is_tail c1 c2 ->
exists b,
is_tail (
b ::
trans_code c) (
trans_code c2).
Proof.
intros H;
induction 1.
-
intros;
subst.
remember (
trans_code (
Mcall _ _::
c))
as tc2.
rewrite <-
is_trans_code_inv in Heqtc2.
inversion Heqtc2;
cbn in * |- *;
subst;
try congruence.
subst_is_trans_code H1.
eapply ex_intro;
eauto with coqlib.
-
intros;
exploit IHis_tail;
eauto.
clear IHis_tail.
intros (
b &
Hb).
inversion Hb;
clear Hb.
*
exploit (
trans_code_monotonic i c2);
eauto.
intros (
l' &
b' &
Hl');
rewrite Hl'.
exists b';
cbn;
eauto with coqlib.
*
exploit (
trans_code_monotonic i c2);
eauto.
intros (
l' &
b' &
Hl');
rewrite Hl'.
cbn;
eapply ex_intro.
eapply is_tail_trans;
eauto with coqlib.
Qed.
Section Mach_Return_Address.
Variable return_address_offset:
function ->
code ->
ptrofs ->
Prop.
Hypothesis ra_exists:
forall (
b:
bblock) (
f:
function) (
c :
list bblock),
is_tail (
b ::
c) (
fn_code f) ->
exists ra :
ptrofs,
return_address_offset f c ra.
Definition Mach_return_address_offset (
f:
Mach.function) (
c:
Mach.code) (
ofs:
ptrofs) :
Prop :=
return_address_offset (
transf_function f) (
trans_code c)
ofs.
Lemma Mach_return_address_exists:
forall f sg ros c,
is_tail (
Mcall sg ros ::
c)
f.(
Mach.fn_code) ->
exists ra,
Mach_return_address_offset f c ra.
Proof.
End Mach_Return_Address.