(*First, construct the two effect semantics*)
Require Export Axioms.
Require Export Maps.
Section TRANSLATION.
Variable prog: Csharpminor.program.
Variable tprog: Cminor.program.
Hypothesis TRANSL: transl_program prog = OK tprog.
Let ge : Csharpminor.genv := Genv.globalenv prog.
Let tge: genv := Genv.globalenv tprog.
Let core_data := CSharpMin_core.
Record match_env (f: meminj) (cenv: compilenv)
(e: Csharpminor.env) (sp: Values.block)
(lo hi: Values.block) : Prop :=
mk_match_env {
(** C#minor local variables match sub-blocks of the Cminor stack data block. *)
me_vars:
forall id, match_var f sp (e!id) (cenv!id);
(** [lo, hi] is a proper interval. *)
me_low_high:
Ple lo hi;
(** Every block appearing in the C#minor environment [e] must be in the range [lo, hi]. *)
me_bounded:
forall id b sz, PTree.get id e = Some(b, sz) -> Ple lo b /\ Plt b hi;
(** All blocks mapped to sub-blocks of the Cminor stack data must be images of variables from the C#minor environment [e] *)
me_inv:
forall b delta,
f b = Some(sp, delta) ->
exists id, exists sz, PTree.get id e = Some(b, sz)
(*LENB: MOVED TO structured_match_callstack below me_incr: forall b tb delta, f b = Some(tb, delta) -> Plt b lo -> Plt tb sp*)
}.
Ltac geninv x :=
let H := fresh in (generalize x; intro H; inv H).

(* Specifications: *)
Lemma match_env_invariant:
forall f1 cenv e sp lo hi f2,
match_env f1 cenv e sp lo hi ->
inject_incr f1 f2 ->
(forall b delta, f2 b = Some(sp, delta) -> f1 b = Some(sp, delta)) ->
(forall b, Plt b lo -> f2 b = f1 b) ->
match_env f2 cenv e sp lo hi.
Lemma match_env_restrict_invariant:
forall f1 cenv e sp lo hi f2 (WD1: SM_wd f1) (WD2: SM_wd f2)
(ME: match_env (restrict (as_inj f1) (vis f1)) cenv e sp lo hi)
(INT: intern_incr f1 f2)
(HA: forall b delta, as_inj f2 b = Some(sp, delta) ->
as_inj f1 b = Some(sp, delta))
(HB: forall b, Plt b lo -> (as_inj f2)  b = (as_inj f1) b),
match_env (restrict (as_inj f2) (vis f2)) cenv e sp lo hi.