Require Import Coqlib Maps Errors Integers Floats Lattice Kildall.
Require Import AST Linking.
Require Import Memory Registers Op RTL Maps.
Require Import Globalenvs Values.
Require Import Linking Values Memory Globalenvs Events Smallstep.
Require Import Registers Op RTL.
Require Import CSE3 CSE3analysis CSE3analysisproof.
Require Import RTLtyping.
Definition match_prog (
p tp:
RTL.program) :=
match_program (
fun ctx f tf =>
transf_fundef f =
OK tf)
eq p tp.
Lemma transf_program_match:
forall p tp,
transf_program p =
OK tp ->
match_prog p tp.
Proof.
Section PRESERVATION.
Variables prog tprog:
program.
Hypothesis TRANSF:
match_prog prog tprog.
Let ge :=
Genv.globalenv prog.
Let tge :=
Genv.globalenv tprog.
Section SOUNDNESS.
Variable sp :
val.
Variable ctx :
eq_context.
Definition sem_rel_b (
rel :
RB.t) (
rs :
regset) (
m :
mem) :=
match rel with
|
None =>
False
|
Some rel =>
sem_rel (
ctx:=
ctx) (
genv:=
ge) (
sp:=
sp)
rel rs m
end.
Lemma forward_move_b_sound :
forall rel rs m x,
(
sem_rel_b rel rs m) ->
rs # (
forward_move_b (
ctx :=
ctx)
rel x) =
rs #
x.
Proof.
destruct rel as [
rel | ];
simpl;
intros.
2:
contradiction.
eapply forward_move_sound;
eauto.
Qed.
Lemma forward_move_l_b_sound :
forall rel rs m x,
(
sem_rel_b rel rs m) ->
rs ## (
forward_move_l_b (
ctx :=
ctx)
rel x) =
rs ##
x.
Proof.
Definition fmap_sem (
fmap :
PMap.t RB.t) (
pc :
node) (
rs :
regset) (
m :
mem) :=
sem_rel_b (
PMap.get pc fmap)
rs m.
Lemma subst_arg_ok:
forall invariants,
forall pc,
forall rs,
forall m,
forall arg,
forall (
SEM :
fmap_sem invariants pc rs m),
rs # (
subst_arg (
ctx:=
ctx)
invariants pc arg) =
rs #
arg.
Proof.
Lemma subst_args_ok:
forall invariants,
forall pc,
forall rs,
forall m,
forall args,
forall (
SEM :
fmap_sem invariants pc rs m),
rs ## (
subst_args (
ctx:=
ctx)
invariants pc args) =
rs ##
args.
Proof.
End SOUNDNESS.
Lemma functions_translated:
forall (
v:
val) (
f:
RTL.fundef),
Genv.find_funct ge v =
Some f ->
exists tf,
Genv.find_funct tge v =
Some tf /\
transf_fundef f =
OK tf.
Proof.
Lemma function_ptr_translated:
forall (
b:
block) (
f:
RTL.fundef),
Genv.find_funct_ptr ge b =
Some f ->
exists tf,
Genv.find_funct_ptr tge b =
Some tf /\
transf_fundef f =
OK tf.
Proof.
Lemma symbols_preserved:
forall id,
Genv.find_symbol tge id =
Genv.find_symbol ge id.
Proof.
Lemma senv_preserved:
Senv.equiv ge tge.
Proof.
Lemma sig_preserved:
forall f tf,
transf_fundef f =
OK tf ->
funsig tf =
funsig f.
Proof.
destruct f;
simpl;
intros.
-
monadInv H.
monadInv EQ.
destruct preanalysis as [
invariants hints].
destruct check_inductiveness.
2:
discriminate.
inv EQ1.
reflexivity.
-
monadInv H.
reflexivity.
Qed.
Lemma stacksize_preserved:
forall f tf,
transf_function f =
OK tf ->
fn_stacksize tf =
fn_stacksize f.
Proof.
Lemma params_preserved:
forall f tf,
transf_function f =
OK tf ->
fn_params tf =
fn_params f.
Proof.
Lemma entrypoint_preserved:
forall f tf,
transf_function f =
OK tf ->
fn_entrypoint tf =
fn_entrypoint f.
Proof.
Lemma sig_preserved2:
forall f tf,
transf_function f =
OK tf ->
fn_sig tf =
fn_sig f.
Proof.
Lemma transf_function_is_typable:
forall f tf,
transf_function f =
OK tf ->
exists tenv,
type_function f =
OK tenv.
Proof.
unfold transf_function;
destruct f;
simpl;
intros.
monadInv H.
exists x.
assumption.
Qed.
Lemma transf_function_invariants_inductive:
forall f tf tenv,
transf_function f =
OK tf ->
type_function f =
OK tenv ->
check_inductiveness (
ctx:=(
context_from_hints (
snd (
preanalysis tenv f))))
f tenv (
fst (
preanalysis tenv f)) =
true.
Proof.
Lemma find_function_translated:
forall ros rs fd,
find_function ge ros rs =
Some fd ->
exists tfd,
find_function tge ros rs =
Some tfd /\
transf_fundef fd =
OK tfd.
Proof.
Inductive match_stackframes:
list stackframe ->
list stackframe ->
signature ->
Prop :=
|
match_stackframes_nil:
forall sg,
sg.(
sig_res) =
Tint ->
match_stackframes nil nil sg
|
match_stackframes_cons:
forall res f sp pc rs s tf ts sg tenv
(
STACKS:
match_stackframes s ts (
fn_sig tf))
(
FUN:
transf_function f =
OK tf)
(
WTF:
type_function f =
OK tenv)
(
WTRS:
wt_regset tenv rs)
(
WTRES:
tenv res =
proj_sig_res sg)
(
REL:
forall m vres,
sem_rel_b sp (
context_from_hints (
snd (
preanalysis tenv f)))
((
fst (
preanalysis tenv f))#
pc) (
rs#
res <-
vres)
m),
match_stackframes
(
Stackframe res f sp pc rs ::
s)
(
Stackframe res tf sp pc rs ::
ts)
sg.
Inductive match_states:
state ->
state ->
Prop :=
|
match_states_intro:
forall s f sp pc rs m ts tf tenv
(
STACKS:
match_stackframes s ts (
fn_sig tf))
(
FUN:
transf_function f =
OK tf)
(
WTF:
type_function f =
OK tenv)
(
WTRS:
wt_regset tenv rs)
(
REL:
sem_rel_b sp (
context_from_hints (
snd (
preanalysis tenv f))) ((
fst (
preanalysis tenv f))#
pc)
rs m),
match_states (
State s f sp pc rs m)
(
State ts tf sp pc rs m)
|
match_states_call:
forall s f args m ts tf
(
STACKS:
match_stackframes s ts (
funsig tf))
(
FUN:
transf_fundef f =
OK tf)
(
WTARGS:
Val.has_type_list args (
sig_args (
funsig tf))),
match_states (
Callstate s f args m)
(
Callstate ts tf args m)
|
match_states_return:
forall s res m ts sg
(
STACKS:
match_stackframes s ts sg)
(
WTRES:
Val.has_type res (
proj_sig_res sg)),
match_states (
Returnstate s res m)
(
Returnstate ts res m).
Lemma match_stackframes_change_sig:
forall s ts sg sg',
match_stackframes s ts sg ->
sg'.(
sig_res) =
sg.(
sig_res) ->
match_stackframes s ts sg'.
Proof.
intros.
inv H.
constructor.
congruence.
econstructor;
eauto.
unfold proj_sig_res in *.
rewrite H0;
auto.
Qed.
Lemma transf_function_at:
forall f tf pc tenv instr
(
TF :
transf_function f =
OK tf)
(
TYPE :
type_function f =
OK tenv)
(
PC : (
fn_code f) !
pc =
Some instr),
(
fn_code tf) !
pc =
Some (
transf_instr
(
ctx := (
context_from_hints (
snd (
preanalysis tenv f))))
(
fst (
preanalysis tenv f))
pc instr).
Proof.
Ltac TR_AT :=
erewrite transf_function_at by eauto.
Hint Resolve wt_instrs type_function_correct :
wt.
Lemma wt_undef :
forall tenv rs dst,
wt_regset tenv rs ->
wt_regset tenv rs #
dst <-
Vundef.
Proof.
Lemma rel_ge:
forall inv inv'
(
GE :
RELATION.ge inv'
inv)
ctx sp rs m
(
REL:
sem_rel (
genv:=
ge) (
sp:=
sp) (
ctx:=
ctx)
inv rs m),
sem_rel (
genv:=
ge) (
sp:=
sp) (
ctx:=
ctx)
inv'
rs m.
Proof.
Hint Resolve rel_ge :
cse3.
Lemma relb_ge:
forall inv inv'
(
GE :
RB.ge inv'
inv)
ctx sp rs m
(
REL:
sem_rel_b sp ctx inv rs m),
sem_rel_b sp ctx inv'
rs m.
Proof.
intros.
destruct inv;
cbn in *.
2:
contradiction.
destruct inv';
cbn in *.
2:
assumption.
eapply rel_ge;
eassumption.
Qed.
Hint Resolve relb_ge :
cse3.
Lemma sem_rhs_sop :
forall sp op rs args m v,
eval_operation ge sp op rs ##
args m =
Some v ->
sem_rhs (
genv:=
ge) (
sp:=
sp) (
SOp op)
args rs m v.
Proof.
intros. simpl.
rewrite H.
reflexivity.
Qed.
Hint Resolve sem_rhs_sop :
cse3.
Lemma sem_rhs_sload :
forall sp chunk addr rs args m a v,
eval_addressing ge sp addr rs ##
args =
Some a ->
Mem.loadv chunk m a =
Some v ->
sem_rhs (
genv:=
ge) (
sp:=
sp) (
SLoad chunk addr)
args rs m v.
Proof.
intros. simpl.
rewrite H. rewrite H0.
reflexivity.
Qed.
Hint Resolve sem_rhs_sload :
cse3.
Lemma sem_rhs_sload_notrap1 :
forall sp chunk addr rs args m,
eval_addressing ge sp addr rs ##
args =
None ->
sem_rhs (
genv:=
ge) (
sp:=
sp) (
SLoad chunk addr)
args rs m Vundef.
Proof.
intros. simpl.
rewrite H.
reflexivity.
Qed.
Hint Resolve sem_rhs_sload_notrap1 :
cse3.
Lemma sem_rhs_sload_notrap2 :
forall sp chunk addr rs args m a,
eval_addressing ge sp addr rs ##
args =
Some a ->
Mem.loadv chunk m a =
None ->
sem_rhs (
genv:=
ge) (
sp:=
sp) (
SLoad chunk addr)
args rs m Vundef.
Proof.
intros. simpl.
rewrite H. rewrite H0.
reflexivity.
Qed.
Hint Resolve sem_rhs_sload_notrap2 :
cse3.
Lemma sem_rel_top:
forall ctx sp rs m,
sem_rel (
genv:=
ge) (
sp:=
sp) (
ctx:=
ctx)
RELATION.top rs m.
Proof.
Hint Resolve sem_rel_top :
cse3.
Lemma sem_rel_b_top:
forall ctx sp rs m,
sem_rel_b sp ctx (
Some RELATION.top)
rs m.
Proof.
Hint Resolve sem_rel_b_top :
cse3.
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 ->
final_state S1 r ->
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.