Require Import FunInd.
Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
Require Import AST Linking.
Require Import Values Memory Globalenvs Events Smallstep.
Require Import Registers Op RTL.
Require Import ForwardMoves.
Definition match_prog (
p tp:
RTL.program) :=
match_program (
fun ctx f tf =>
tf =
transf_fundef f)
eq p tp.
Lemma transf_program_match:
forall p,
match_prog p (
transf_program p).
Proof.
Section PRESERVATION.
Variables prog tprog:
program.
Hypothesis TRANSL:
match_prog prog tprog.
Let ge :=
Genv.globalenv prog.
Let tge :=
Genv.globalenv tprog.
Lemma functions_translated:
forall v f,
Genv.find_funct ge v =
Some f ->
Genv.find_funct tge v =
Some (
transf_fundef f).
Proof (
Genv.find_funct_transf TRANSL).
Lemma function_ptr_translated:
forall v f,
Genv.find_funct_ptr ge v =
Some f ->
Genv.find_funct_ptr tge v =
Some (
transf_fundef f).
Proof (
Genv.find_funct_ptr_transf TRANSL).
Lemma symbols_preserved:
forall id,
Genv.find_symbol tge id =
Genv.find_symbol ge id.
Proof (
Genv.find_symbol_transf TRANSL).
Lemma senv_preserved:
Senv.equiv ge tge.
Proof (
Genv.senv_transf TRANSL).
Lemma sig_preserved:
forall f,
funsig (
transf_fundef f) =
funsig f.
Proof.
destruct f; trivial.
Qed.
Lemma find_function_translated:
forall ros rs fd,
find_function ge ros rs =
Some fd ->
find_function tge ros rs =
Some (
transf_fundef fd).
Proof.
Lemma transf_function_at:
forall f pc i,
f.(
fn_code)!
pc =
Some i ->
(
transf_function f).(
fn_code)!
pc =
Some(
transf_instr (
forward_map f)
pc i).
Proof.
Lemma apply_instr'_bot :
forall code,
forall pc,
RB.eq (
apply_instr' code pc RB.bot)
RB.bot.
Proof.
reflexivity.
Qed.
Definition get_rb_sem (
rb :
RB.t) (
rs :
regset) :=
match rb with
|
None =>
False
|
Some rel =>
forall x :
reg,
(
rs # (
get_r rel x)) = (
rs #
x)
end.
Lemma get_rb_sem_ge:
forall rb1 rb2 :
RB.t,
(
RB.ge rb1 rb2) ->
forall rs :
regset,
(
get_rb_sem rb2 rs) -> (
get_rb_sem rb1 rs).
Proof.
destruct rb1 as [
r1 | ];
destruct rb2 as [
r2 | ];
unfold get_rb_sem;
simpl;
intros GE rs RB2RS;
try contradiction.
unfold RELATION.ge in GE.
unfold get_r in *.
intro x.
pose proof (
GE x)
as GEx.
pose proof (
RB2RS x)
as RB2RSx.
destruct (
r1 !
x)
as [
r1x | ]
in *;
destruct (
r2 !
x)
as [
r2x | ]
in *;
congruence.
Qed.
Definition fmap_sem (
fmap :
option (
PMap.t RB.t))
(
pc :
node) (
rs :
regset) :=
match fmap with
|
None =>
True
|
Some m =>
get_rb_sem (
PMap.get pc m)
rs
end.
Lemma subst_arg_ok:
forall f,
forall pc,
forall rs,
forall arg,
fmap_sem (
forward_map f)
pc rs ->
rs # (
subst_arg (
forward_map f)
pc arg) =
rs #
arg.
Proof.
intros until arg.
intro SEM.
unfold fmap_sem in SEM.
destruct (
forward_map f)
as [
map |]
in *;
trivial.
simpl.
unfold get_rb_sem in *.
destruct (
map #
pc).
2:
contradiction.
apply SEM.
Qed.
Lemma subst_args_ok:
forall f,
forall pc,
forall rs,
fmap_sem (
forward_map f)
pc rs ->
forall args,
rs ## (
subst_args (
forward_map f)
pc args) =
rs ##
args.
Proof.
induction args;
trivial.
simpl.
f_equal.
apply subst_arg_ok;
assumption.
assumption.
Qed.
Lemma kill_ok:
forall dst,
forall mpc,
forall rs,
forall v,
get_rb_sem (
Some mpc)
rs ->
get_rb_sem (
Some (
kill dst mpc))
rs #
dst <-
v.
Proof.
Lemma kill_weaken:
forall dst,
forall mpc,
forall rs,
get_rb_sem (
Some mpc)
rs ->
get_rb_sem (
Some (
kill dst mpc))
rs.
Proof.
Lemma top_ok :
forall rs,
get_rb_sem (
Some RELATION.top)
rs.
Proof.
Lemma move_ok:
forall mpc :
RELATION.t,
forall src res :
reg,
forall rs :
regset,
get_rb_sem (
Some mpc)
rs ->
get_rb_sem (
Some (
move src res mpc)) (
rs #
res <- (
rs #
src)).
Proof.
Ltac TR_AT :=
match goal with
| [
A: (
fn_code _)!_ =
Some _ |- _ ] =>
generalize (
transf_function_at _ _ _
A);
intros
end.
Definition is_killed_in_map (
map :
PMap.t RB.t)
pc res :=
match PMap.get pc map with
|
None =>
True
|
Some rel =>
exists rel',
RELATION.ge rel (
kill res rel')
end.
Definition is_killed_in_fmap fmap pc res :=
match fmap with
|
None =>
True
|
Some map =>
is_killed_in_map map pc res
end.
Definition killed_twice:
forall rel :
RELATION.t,
forall res,
RELATION.eq (
kill res rel) (
kill res (
kill res rel)).
Proof.
Lemma get_rb_killed:
forall mpc,
forall rs,
forall rel,
forall res,
forall vres,
(
get_rb_sem (
Some mpc)
rs) ->
(
RELATION.ge mpc (
kill res rel)) ->
(
get_rb_sem (
Some mpc)
rs #
res <-
vres).
Proof.
simpl.
intros until vres.
intros SEM GE x.
pose proof (
GE x)
as GEx.
pose proof (
SEM x)
as SEMx.
unfold get_r in *.
destruct (
mpc !
x)
as [
mpcx | ]
in *;
trivial.
unfold kill in GEx.
rewrite PTree.gfilter1 in GEx.
destruct (
Pos.eq_dec res x)
as [ |
res_NE_x].
{
subst res.
rewrite PTree.grs in GEx.
discriminate.
}
rewrite PTree.gro in GEx by congruence.
rewrite Regmap.gso with (
i :=
x)
by congruence.
destruct (
rel !
x)
as [
relx | ];
try discriminate.
destruct (
Pos.eq_dec res relx)
as [
res_EQ_relx |
res_NE_relx]
in *;
try discriminate.
rewrite Regmap.gso by congruence.
congruence.
Qed.
Inductive match_frames:
RTL.stackframe ->
RTL.stackframe ->
Prop :=
|
match_frames_intro:
forall res f sp pc rs,
(
fmap_sem (
forward_map f)
pc rs) ->
(
is_killed_in_fmap (
forward_map f)
pc res) ->
match_frames (
Stackframe res f sp pc rs)
(
Stackframe res (
transf_function f)
sp pc rs).
Inductive match_states:
RTL.state ->
RTL.state ->
Prop :=
|
match_regular_states:
forall stk f sp pc rs m stk'
(
STACKS:
list_forall2 match_frames stk stk'),
(
fmap_sem (
forward_map f)
pc rs) ->
match_states (
State stk f sp pc rs m)
(
State stk' (
transf_function f)
sp pc rs m)
|
match_callstates:
forall stk f args m stk'
(
STACKS:
list_forall2 match_frames stk stk'),
match_states (
Callstate stk f args m)
(
Callstate stk' (
transf_fundef f)
args m)
|
match_returnstates:
forall stk v m stk'
(
STACKS:
list_forall2 match_frames stk stk'),
match_states (
Returnstate stk v m)
(
Returnstate stk' v m).
Lemma op_cases:
forall op,
forall args,
forall dst,
forall s,
forall x,
(
exists src,
op=
Omove /\
args =
src ::
nil /\
(
apply_instr (
Iop op args dst s)
x) =
Some (
move src dst x))
\/
(
apply_instr (
Iop op args dst s)
x) =
Some (
kill dst x).
Proof.
destruct op; try (right; simpl; reflexivity).
destruct args as [| arg0 args0t]; try (right; simpl; reflexivity).
destruct args0t as [| arg1 args1t]; try (right; simpl; reflexivity).
left.
eauto.
Qed.
Lemma step_simulation:
forall S1 t S2,
RTL.step ge S1 t S2 ->
forall S1',
match_states S1 S1' ->
exists S2',
RTL.step tge S1' t S2' /\
match_states S2 S2'.
Proof.
Lemma transf_initial_states:
forall S1,
RTL.initial_state prog S1 ->
exists S2,
RTL.initial_state tprog S2 /\
match_states S1 S2.
Proof.
Lemma transf_final_states:
forall S1 S2 r,
match_states S1 S2 ->
RTL.final_state S1 r ->
RTL.final_state S2 r.
Proof.
intros. inv H0. inv H. inv STACKS. constructor.
Qed.
Theorem transf_program_correct:
forward_simulation (
RTL.semantics prog) (
RTL.semantics tprog).
Proof.
End PRESERVATION.