(** ** Semantic preservation for the translation of statements *)
(** The simulation diagram for the translation of statements and functions is a "star" diagram of the form: << I                         I S1 ------- R1             S1 ------- R1 |          |              |          | t |        + | t      or  t |        * | t    and |S2| < |S1| v          v              v          | S2 ------- R2             S2 ------- R2 I                         I >> where [I] is the [match_states] predicate defined below.  It includes: - Agreement between the Cminor statement under consideration and the current program point in the RTL control-flow graph, as captured by the [tr_stmt] predicate. - Agreement between the Cminor continuation and the RTL control-flow graph and call stack, as captured by the [tr_cont] predicate below. - Agreement between Cminor environments and RTL register environments, as captured by [match_envs].  *)
Inductive tr_fun (tf: function) (map: mapping) (f: CminorSel.function)
(ngoto: labelmap) (nret: node) (rret: option reg) : Prop :=
| tr_fun_intro: forall nentry r,
rret = ret_reg f.(CminorSel.fn_sig) r ->
tr_stmt tf.(fn_code) map f.(fn_body) nentry nret nil ngoto nret rret ->
tf.(fn_stacksize) = f.(fn_stackspace) ->
tr_fun tf map f ngoto nret rret.
(*LENB: new: meminj parameter j*)
Inductive tr_cont (j:meminj): RTL.code -> mapping ->
CminorSel.cont -> node -> list node -> labelmap -> node -> option reg ->
list RTL.stackframe -> Prop :=
| tr_Kseq: forall c map s k nd nexits ngoto nret rret cs n,
tr_stmt c map s nd n nexits ngoto nret rret ->
tr_cont j c map k n nexits ngoto nret rret cs ->
tr_cont j c map (Kseq s k) nd nexits ngoto nret rret cs
| tr_Kblock: forall c map k nd nexits ngoto nret rret cs,
tr_cont j c map k nd nexits ngoto nret rret cs ->
tr_cont j c map (Kblock k) nd (nd :: nexits) ngoto nret rret cs
| tr_Kstop: forall c map ngoto nret rret cs,
c!nret = Some(Ireturn rret) ->
match_stacks j Kstop cs ->
tr_cont j c map Kstop nret nil ngoto nret rret cs
| tr_Kcall: forall c map optid f sp e k ngoto nret rret cs,
c!nret = Some(Ireturn rret) ->
match_stacks j (Kcall optid f sp e k) cs ->
tr_cont j c map (Kcall optid f sp e k) nret nil ngoto nret rret cs
with match_stacks (j:meminj) : CminorSel.cont -> list RTL.stackframe -> Prop :=
| match_stacks_stop:
match_stacks j Kstop nil
| match_stacks_call: forall optid f sp e k r tf n rs cs map nexits ngoto nret rret sp',
map_wf map ->
tr_fun tf map f ngoto nret rret ->
match_env j map e nil rs ->
reg_map_ok map r optid ->
tr_cont j tf.(fn_code) map k n nexits ngoto nret rret cs ->
(*NEW:*) sp_preserved j sp sp' -> match_stacks j (Kcall optid f sp e k) (Stackframe r tf sp' n rs :: cs).  (*Derivation of induction scheme as explained her: http://coq.inria.fr/cocorico/Mutual%20Induction*)
Scheme tr_cont_ind_2 := Induction for tr_cont Sort Prop
with match_stacks_ind_2 := Induction for match_stacks Sort Prop.
Combined Scheme tr_cont_match_stacks_mutual_ind
from tr_cont_ind_2, match_stacks_ind_2.

Lemma tr_cont_match_stacks_inject_incr:
forall j j' (INC: inject_incr j j'),
(forall c map k ncont nexits ngoto nret rret cs,
tr_cont j c map k ncont nexits ngoto nret rret cs ->
tr_cont j' c map k ncont nexits ngoto nret rret cs) /\
(forall k cs, match_stacks j k cs -> match_stacks j' k cs).