(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Correctness proof for the [Debugvar] pass. *)

Require Import Axioms Coqlib Maps Iteration Errors.
Require Import Integers Floats AST Linking.
Require Import Values Memory Events Globalenvs Smallstep.
Require Import Machregs Locations Conventions Op Linear.
Require Import Debugvar.

(** * Relational characterization of the transformation *)

Definition match_prog (p tp: program) :=
  match_program (fun _ 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.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. eapply match_transform_partial_program; eauto.
Qed.

Inductive match_code: code -> code -> Prop :=
  | match_code_nil:
      match_code nil nil
  | match_code_cons: forall i before after c c',
      match_code c c' ->
      match_code (i :: c) (i :: add_delta_ranges before after c').

Remark diff_same:
  forall s, diff s s = nil.
Proof.
(* Goal: forall s : avail, @eq avail (diff s s) (@nil (prod ident debuginfo)) *)
  induction s as [ | [v i] s]; simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  rewrite Pos.compare_refl. rewrite dec_eq_true. auto.
Qed.

Remark delta_state_same:
  forall s, delta_state s s = (nil, nil).
Proof.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  destruct s; simpl. rewrite ! diff_same; auto. auto.
Qed.

Lemma transf_code_match:
  forall lm c before, match_code c (transf_code lm before c).
Proof.
(* Goal: forall (lm : labelmap) (c : code) (before : option avail), match_code c (transf_code lm before c) *)
  intros lm. fix REC 1. destruct c; intros before; simpl.
- constructor.
- assert (DEFAULT: forall before after,
            match_code (i :: c)
                       (i :: add_delta_ranges before after (transf_code lm after c))).
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  { intros. constructor. apply REC. }
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  destruct i; auto. destruct c; auto. destruct i; auto.
(* Goal: match_code (@cons instruction (Lgoto l) (@cons instruction (Llabel l0) c)) (@cons instruction (Lgoto l) (@cons instruction (Llabel l0) (add_delta_ranges before (get_label l0 lm) (transf_code lm (get_label l0 lm) c)))) *)
  set (after := get_label l0 lm).
(* Goal: match_code (@cons instruction (Lgoto l) (@cons instruction (Llabel l0) c)) (@cons instruction (Lgoto l) (@cons instruction (Llabel l0) (add_delta_ranges before after (transf_code lm after c)))) *)
  set (c1 := Llabel l0 :: add_delta_ranges before after (transf_code lm after c)).
(* Goal: match_code (@cons instruction (Lgoto l) (@cons instruction (Llabel l0) c)) (@cons instruction (Lgoto l) c1) *)
  replace c1 with (add_delta_ranges before before c1).
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor. constructor. apply REC.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  unfold add_delta_ranges. rewrite delta_state_same. auto.
Qed.

Inductive match_function: function -> function -> Prop :=
  | match_function_intro: forall f c,
      match_code f.(fn_code) c ->
      match_function f (mkfunction f.(fn_sig) f.(fn_stacksize) c).

Lemma transf_function_match:
  forall f tf, transf_function f = OK tf -> match_function f tf.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold transf_function; intros.
(* Goal: match_function f tf *)
  destruct (ana_function f) as [lm|]; inv H.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor. apply transf_code_match.
Qed.

Remark find_label_add_delta_ranges:
  forall lbl c before after, find_label lbl (add_delta_ranges before after c) = find_label lbl c.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. unfold add_delta_ranges.
(* Goal: @eq (option code) (find_label lbl (let (killed, born) := delta_state before after in @fold_right code (prod ident debuginfo) add_end_range (@fold_right code (prod ident debuginfo) add_start_range c born) killed)) (find_label lbl c) *)
  destruct (delta_state before after) as [killed born].
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  induction killed as [ | [v i] l]; simpl; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  induction born as [ | [v i] l]; simpl; auto.
Qed.

Lemma find_label_match_rec:
  forall lbl c' c tc,
  match_code c tc ->
  find_label lbl c = Some c' ->
  exists before after tc', find_label lbl tc = Some (add_delta_ranges before after tc') /\ match_code c' tc'.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; simpl; intros.
- discriminate.
- destruct (is_label lbl i).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  inv H0. econstructor; econstructor; econstructor; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  rewrite find_label_add_delta_ranges. auto.
Qed.

Lemma find_label_match:
  forall f tf lbl c,
  match_function f tf ->
  find_label lbl f.(fn_code) = Some c ->
  exists before after tc, find_label lbl tf.(fn_code) = Some (add_delta_ranges before after tc) /\ match_code c tc.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H. eapply find_label_match_rec; eauto.
Qed.

(** * Properties of availability sets *)

(** These properties are not used in the semantic preservation proof,
    but establish some confidence in the availability analysis. *)

Definition avail_above (v: ident) (s: avail) : Prop :=
  forall v' i', In (v', i') s -> Plt v v'.

Inductive wf_avail: avail -> Prop :=
  | wf_avail_nil:
      wf_avail nil
  | wf_avail_cons: forall v i s,
     avail_above v s ->
     wf_avail s ->
     wf_avail ((v, i) :: s).

Lemma set_state_1:
  forall v i s, In (v, i) (set_state v i s).
Proof.
(* Goal: forall (v : ident) (i : debuginfo) (s : avail), @In (prod ident debuginfo) (@pair ident debuginfo v i) (set_state v i s) *)
  induction s as [ | [v' i'] s]; simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- destruct (Pos.compare v v'); simpl; auto.
Qed.

Lemma set_state_2:
  forall v i v' i' s,
  v' <> v -> In (v', i') s -> In (v', i') (set_state v i s).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction s as [ | [v1 i1] s]; simpl; intros.
- contradiction.
- destruct (Pos.compare_spec v v1); simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ subst v1. destruct H0. congruence. auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H0; auto.
Qed.

Lemma set_state_3:
  forall v i v' i' s,
  wf_avail s ->
  In (v', i') (set_state v i s) ->
  (v' = v /\ i' = i) \/ (v' <> v /\ In (v', i') s).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; simpl; intros.
- intuition congruence.
- destruct (Pos.compare_spec v v0); simpl in H1.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ subst v0. destruct H1. inv H1; auto. right; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  apply not_eq_sym. apply Plt_ne. eapply H; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H1. inv H1; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  destruct H1. inv H1. right; split; auto. apply not_eq_sym. apply Plt_ne. auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  right; split; auto. apply not_eq_sym. apply Plt_ne. apply Plt_trans with v0; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H1. inv H1. right; split; auto. apply Plt_ne. auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  destruct IHwf_avail as [A | [A B]]; auto.
Qed.

Lemma wf_set_state:
  forall v i s, wf_avail s -> wf_avail (set_state v i s).
Proof.
(* Goal: forall (s ts : list stackframe) (_ : @list_forall2 stackframe stackframe match_stackframes s ts), @eq locset (parent_locset ts) (parent_locset s) *)
  induction 1; simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- constructor. red; simpl; tauto. constructor.
- destruct (Pos.compare_spec v v0).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ subst v0. constructor; auto.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ constructor.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; simpl; intros. destruct H2.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  inv H2. auto. apply Plt_trans with v0; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ constructor.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. exploit set_state_3. eexact H0. eauto. intros [[A B] | [A B]]; subst; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  auto.
Qed.

Lemma remove_state_1:
  forall v i s, wf_avail s -> ~ In (v, i) (remove_state v s).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; simpl; red; intros.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- auto.
- destruct (Pos.compare_spec v v0); simpl in *.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ subst v0. elim (Plt_strict v); eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H1. inv H1.  elim (Plt_strict v); eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  elim (Plt_strict v). apply Plt_trans with v0; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H1. inv H1. elim (Plt_strict v); eauto.  tauto.
Qed.

Lemma remove_state_2:
  forall v v' i' s, v' <> v -> In (v', i') s -> In (v', i') (remove_state v s).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction s as [ | [v1 i1] s]; simpl; intros.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- auto.
- destruct (Pos.compare_spec v v1); simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ subst v1. destruct H0. congruence. auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H0; auto.
Qed.

Lemma remove_state_3:
  forall v v' i' s, wf_avail s -> In (v', i') (remove_state v s) -> v' <> v /\ In (v', i') s.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction 1; simpl; intros.
- contradiction.
- destruct (Pos.compare_spec v v0); simpl in H1.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ subst v0. split; auto. apply not_eq_sym; apply Plt_ne; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H1. inv H1. split; auto. apply not_eq_sym; apply Plt_ne; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  split; auto. apply not_eq_sym; apply Plt_ne. apply Plt_trans with v0; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ destruct H1. inv H1. split; auto. apply Plt_ne; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  destruct IHwf_avail as [A B] ; auto.
Qed.

Lemma wf_remove_state:
  forall v s, wf_avail s -> wf_avail (remove_state v s).
Proof.
(* Goal: forall (s ts : list stackframe) (_ : @list_forall2 stackframe stackframe match_stackframes s ts), @eq locset (parent_locset ts) (parent_locset s) *)
  induction 1; simpl.
- constructor.
- destruct (Pos.compare_spec v v0).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ constructor; auto.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
+ constructor; auto. red; intros.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit remove_state_3. eexact H0. eauto. intros [A B]. eauto.
Qed.

Lemma wf_filter:
  forall pred s, wf_avail s -> wf_avail (List.filter pred s).
Proof.
(* Goal: forall (s ts : list stackframe) (_ : @list_forall2 stackframe stackframe match_stackframes s ts), @eq locset (parent_locset ts) (parent_locset s) *)
  induction 1; simpl.
- constructor.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- destruct (pred (v, i)) eqn:P; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. apply filter_In in H1. destruct H1. eauto.
Qed.

Lemma join_1:
  forall v i s1, wf_avail s1 -> forall s2, wf_avail s2 ->
  In (v, i) s1 -> In (v, i) s2 -> In (v, i) (join s1 s2).
Proof.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  induction 1; simpl; try tauto; induction 1; simpl; intros I1 I2; auto.
(* Goal: @In (prod ident debuginfo) (@pair ident debuginfo v i) match Pos.compare v0 v1 with | Eq => if eq_debuginfo i0 i1 then @cons (prod ident debuginfo) (@pair ident debuginfo v0 i0) (join s s0) else join s s0 | Lt => join s (@cons (prod ident debuginfo) (@pair ident debuginfo v1 i1) s0) | Gt => (fix join2 (s2 : avail) : avail := match s2 with | nil => @nil (prod ident debuginfo) | cons (pair v2 i2 as vi2) s2' => match Pos.compare v0 v2 with | Eq => if eq_debuginfo i0 i2 then @cons (prod ident debuginfo) (@pair ident debuginfo v0 i0) (join s s2') else join s s2' | Lt => join s s2 | Gt => join2 s2' end end) s0 end *)
  destruct I1, I2.
(* Goal: @In (prod ident debuginfo) (@pair ident debuginfo v i) match Pos.compare v v with | Eq => if eq_debuginfo i i then @cons (prod ident debuginfo) (@pair ident debuginfo v i) (join s s0) else join s s0 | Lt => join s (@cons (prod ident debuginfo) (@pair ident debuginfo v i) s0) | Gt => (fix join2 (s2 : avail) : avail := match s2 with | nil => @nil (prod ident debuginfo) | cons (pair v2 i2 as vi2) s2' => match Pos.compare v v2 with | Eq => if eq_debuginfo i i2 then @cons (prod ident debuginfo) (@pair ident debuginfo v i) (join s s2') else join s s2' | Lt => join s s2 | Gt => join2 s2' end end) s0 end *)
- inv H3; inv H4. rewrite Pos.compare_refl. rewrite dec_eq_true; auto with coqlib.
- inv H3.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  assert (L: Plt v1 v) by eauto. apply Pos.compare_gt_iff in L. rewrite L. auto.
- inv H4.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  assert (L: Plt v0 v) by eauto. apply Pos.compare_lt_iff in L. rewrite L. apply IHwf_avail. constructor; auto. auto. auto with coqlib.
- destruct (Pos.compare v0 v1).
+ destruct (eq_debuginfo i0 i1); auto with coqlib.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ apply IHwf_avail; auto with coqlib. constructor; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
+ eauto.
Qed.

Lemma join_2:
  forall v i s1, wf_avail s1 -> forall s2, wf_avail s2 ->
  In (v, i) (join s1 s2) -> In (v, i) s1 /\ In (v, i) s2.
Proof.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  induction 1; simpl; try tauto; induction 1; simpl; intros I; try tauto.
(* Goal: and (or (@eq (prod ident debuginfo) (@pair ident debuginfo v0 i0) (@pair ident debuginfo v i)) (@In (prod ident debuginfo) (@pair ident debuginfo v i) s)) (or (@eq (prod ident debuginfo) (@pair ident debuginfo v1 i1) (@pair ident debuginfo v i)) (@In (prod ident debuginfo) (@pair ident debuginfo v i) s0)) *)
  destruct (Pos.compare_spec v0 v1).
- subst v1. destruct (eq_debuginfo i0 i1).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  + subst i1. destruct I. auto. exploit IHwf_avail; eauto. tauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  + exploit IHwf_avail; eauto. tauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- exploit (IHwf_avail ((v1, i1) :: s0)); eauto. constructor; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  simpl. tauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- exploit IHwf_avail0; eauto. tauto.
Qed.

Lemma wf_join:
  forall s1, wf_avail s1 -> forall s2, wf_avail s2 -> wf_avail (join s1 s2).
Proof.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  induction 1; simpl; induction 1; simpl; try constructor.
(* Goal: wf_avail match Pos.compare v v0 with | Eq => if eq_debuginfo i i0 then @cons (prod ident debuginfo) (@pair ident debuginfo v i) (join s s0) else join s s0 | Lt => join s (@cons (prod ident debuginfo) (@pair ident debuginfo v0 i0) s0) | Gt => (fix join2 (s2 : avail) : avail := match s2 with | nil => @nil (prod ident debuginfo) | cons (pair v2 i2 as vi2) s2' => match Pos.compare v v2 with | Eq => if eq_debuginfo i i2 then @cons (prod ident debuginfo) (@pair ident debuginfo v i) (join s s2') else join s s2' | Lt => join s s2 | Gt => join2 s2' end end) s0 end *)
  destruct (Pos.compare_spec v v0).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- subst v0. destruct (eq_debuginfo i i0); auto. constructor; auto.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  red; intros. apply join_2 in H3; auto. destruct H3. eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- apply IHwf_avail. constructor; auto.
- apply IHwf_avail0.
Qed.

(** * Semantic preservation *)

Section PRESERVATION.

Variable prog: program.
Variable tprog: program.

Hypothesis TRANSF: match_prog prog tprog.

Let ge := Genv.globalenv prog.
Let tge := Genv.globalenv tprog.

Lemma symbols_preserved:
  forall (s: ident), Genv.find_symbol tge s = Genv.find_symbol ge s.
Proof (Genv.find_symbol_match TRANSF).

Lemma senv_preserved:
  Senv.equiv ge tge.
Proof (Genv.senv_match TRANSF).

Lemma functions_translated:
  forall (v: val) (f: fundef),
  Genv.find_funct ge v = Some f ->
  exists tf,
  Genv.find_funct tge v = Some tf /\ transf_fundef f = OK tf.
Proof (Genv.find_funct_transf_partial TRANSF).

Lemma function_ptr_translated:
  forall (b: block) (f: 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 (Genv.find_funct_ptr_transf_partial TRANSF).

Lemma sig_preserved:
  forall f tf,
  transf_fundef f = OK tf ->
  funsig tf = funsig f.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold transf_fundef, transf_partial_fundef; intros.
(* Goal: @eq signature (funsig tf) (funsig f) *)
  destruct f. monadInv H.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit transf_function_match; eauto. intros M; inv M; auto.
(* Goal: final_state st2 r *)
  inv H. reflexivity.
Qed.

Lemma find_function_translated:
  forall ros ls f,
  find_function ge ros ls = Some f ->
  exists tf,
  find_function tge ros ls = Some tf /\ transf_fundef f = OK tf.
Proof.
(* Goal: forall _ : and (@In (prod ident debuginfo) (@pair ident debuginfo v i) s) (@In (prod ident debuginfo) (@pair ident debuginfo v i) (@cons (prod ident debuginfo) (@pair ident debuginfo v1 i1) s0)), and (or (@eq (prod ident debuginfo) (@pair ident debuginfo v0 i0) (@pair ident debuginfo v i)) (@In (prod ident debuginfo) (@pair ident debuginfo v i) s)) (or (@eq (prod ident debuginfo) (@pair ident debuginfo v1 i1) (@pair ident debuginfo v i)) (@In (prod ident debuginfo) (@pair ident debuginfo v i) s0)) *)
  unfold find_function; intros; destruct ros; simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  apply functions_translated; auto.
(* Goal: @ex fundef (fun tf : fundef => and (@eq (option fundef) match @Genv.find_symbol fundef unit tge i with | Some b => @Genv.find_funct_ptr fundef unit tge b | None => @None fundef end (@Some fundef tf)) (@eq (res fundef) (transf_fundef f) (@OK fundef tf))) *)
  rewrite symbols_preserved. destruct (Genv.find_symbol ge i).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  apply function_ptr_translated; auto.
(* Goal: @ex fundef (fun tf : fundef => and (@eq (option fundef) (@None fundef) (@Some fundef tf)) (@eq (res fundef) (transf_fundef f) (@OK fundef tf))) *)
  congruence.
Qed.

(** Evaluation of the debug annotations introduced by the transformation. *)

Lemma can_eval_safe_arg:
  forall (rs: locset) sp m (a: builtin_arg loc),
  safe_builtin_arg a -> exists v, eval_builtin_arg tge rs sp m a v.
Proof.
  induction a; simpl; intros; try contradiction;
  try (econstructor; now eauto with barg).
(* Goal: @ex val (fun v : val => @eval_builtin_arg loc (@Genv.to_senv fundef unit tge) rs sp m (@BA_splitlong loc a1 a2) v) *)
  destruct H as [S1 S2].
(* Goal: @ex val (fun v : val => @eval_builtin_arg loc (@Genv.to_senv fundef unit tge) rs sp m (@BA_splitlong loc a1 a2) v) *)
  destruct (IHa1 S1) as [v1 E1]. destruct (IHa2 S2) as [v2 E2].
(* Goal: @ex val (fun v : val => @eval_builtin_arg loc (@Genv.to_senv fundef unit tge) rs sp m (@BA_splitlong loc a1 a2) v) *)
  exists (Val.longofwords v1 v2); auto with barg.
Qed.

Lemma eval_add_delta_ranges:
  forall s f sp c rs m before after,
  star step tge (State s f sp (add_delta_ranges before after c) rs m)
             E0 (State s f sp c rs m).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. unfold add_delta_ranges.
(* Goal: @eq (option code) (find_label lbl (let (killed, born) := delta_state before after in @fold_right code (prod ident debuginfo) add_end_range (@fold_right code (prod ident debuginfo) add_start_range c born) killed)) (find_label lbl c) *)
  destruct (delta_state before after) as [killed born].
(* Goal: forall _ : and (@In (prod ident debuginfo) (@pair ident debuginfo v i) s) (@In (prod ident debuginfo) (@pair ident debuginfo v i) (@cons (prod ident debuginfo) (@pair ident debuginfo v1 i1) s0)), and (or (@eq (prod ident debuginfo) (@pair ident debuginfo v0 i0) (@pair ident debuginfo v i)) (@In (prod ident debuginfo) (@pair ident debuginfo v i) s)) (or (@eq (prod ident debuginfo) (@pair ident debuginfo v1 i1) (@pair ident debuginfo v i)) (@In (prod ident debuginfo) (@pair ident debuginfo v i) s0)) *)
  induction killed as [ | [v i] l]; simpl.
- induction born as [ | [v i] l]; simpl.
+ apply star_refl.
+ destruct i as [a SAFE]; simpl.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit can_eval_safe_arg; eauto. intros [v1 E1].
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply star_step; eauto.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor. eexact E1. constructor.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  simpl; constructor.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  simpl; auto.
  traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
- eapply star_step; eauto.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  simpl; constructor.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  simpl; auto.
  traceEq.
Qed.

(** Matching between program states. *)

Inductive match_stackframes: Linear.stackframe -> Linear.stackframe -> Prop :=
  | match_stackframe_intro:
      forall f sp rs c tf tc before after,
      match_function f tf ->
      match_code c tc ->
      match_stackframes
        (Stackframe f sp rs c)
        (Stackframe tf sp rs (add_delta_ranges before after tc)).

Inductive match_states: Linear.state ->  Linear.state -> Prop :=
  | match_states_instr:
      forall s f sp c rs m tf ts tc
        (STACKS: list_forall2 match_stackframes s ts)
        (TRF: match_function f tf)
        (TRC: match_code c tc),
      match_states (State s f sp c rs m)
                   (State ts tf sp tc rs m)
  | match_states_call:
      forall s f rs m tf ts,
      list_forall2 match_stackframes s ts ->
      transf_fundef f = OK tf ->
      match_states (Callstate s f rs m)
                   (Callstate ts tf rs m)
  | match_states_return:
      forall s rs m ts,
      list_forall2 match_stackframes s ts ->
      match_states (Returnstate s rs m)
                   (Returnstate ts rs m).

Lemma parent_locset_match:
  forall s ts,
  list_forall2 match_stackframes s ts ->
  parent_locset ts = parent_locset s.
Proof.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  induction 1; simpl. auto. inv H; auto.
Qed.

(** The simulation diagram. *)

Theorem transf_step_correct:
  forall s1 t s2, step ge s1 t s2 ->
  forall ts1 (MS: match_states s1 ts1),
  exists ts2, plus step tge ts1 t ts2 /\ match_states s2 ts2.
Proof.
  induction 1; intros ts1 MS; inv MS; try (inv TRC).
- (* getstack *)
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. constructor; auto. apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* setstack *)
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. constructor; auto. apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* op *)
  econstructor; split.
  eapply plus_left.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor; eauto.
  instantiate (1 := v). rewrite <- H; apply eval_operation_preserved; exact symbols_preserved.
  apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* load *)
  econstructor; split.
  eapply plus_left.
  eapply exec_Lload with (a := a).
  rewrite <- H; apply eval_addressing_preserved; exact symbols_preserved.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eauto. eauto.
  apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* store *)
  econstructor; split.
  eapply plus_left.
  eapply exec_Lstore with (a := a).
  rewrite <- H; apply eval_addressing_preserved; exact symbols_preserved.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eauto. eauto.
  apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* call *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit find_function_translated; eauto. intros (tf' & A & B).
  econstructor; split.
  apply plus_one.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor. eexact A. symmetry; apply sig_preserved; auto. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto. constructor; auto. constructor; auto.
- (* tailcall *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit find_function_translated; eauto. intros (tf' & A & B).
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit parent_locset_match; eauto. intros PLS.
  econstructor; split.
  apply plus_one.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor. eauto. rewrite PLS. eexact A.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  symmetry; apply sig_preserved; auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  inv TRF; eauto. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  rewrite PLS. constructor; auto.
- (* builtin *)
  econstructor; split.
  eapply plus_left.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply external_call_symbols_preserved; eauto. apply senv_preserved.
  apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* label *)
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. constructor; auto. apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* goto *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit find_label_match; eauto. intros (before' & after' & tc' & A & B).
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. constructor; eauto. apply eval_add_delta_ranges; eauto. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* cond taken *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit find_label_match; eauto. intros (before' & after' & tc' & A & B).
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. eapply exec_Lcond_true; eauto. apply eval_add_delta_ranges; eauto. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* cond not taken *)
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. eapply exec_Lcond_false; auto. apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* jumptable *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit find_label_match; eauto. intros (before' & after' & tc' & A & B).
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. econstructor; eauto.
(* Goal: @eq signature (funsig (@External function e)) (funsig (@External function e)) *)
  apply eval_add_delta_ranges. reflexivity. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* return *)
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  apply plus_one.  constructor. inv TRF; eauto. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  rewrite (parent_locset_match _ _ STACKS). constructor; auto.
- (* internal function *)
  monadInv H7. rename x into tf.
  assert (MF: match_function f tf) by (apply transf_function_match; auto).
  inversion MF; subst.
  econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  apply plus_one. constructor. simpl; eauto. reflexivity.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* external function *)
  monadInv H8. econstructor; split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  apply plus_one. econstructor; eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
- (* return *)
(* Goal: and (not (@eq ident v' v)) (or (@eq (prod ident debuginfo) (@pair ident debuginfo v0 i) (@pair ident debuginfo v' i')) (@In (prod ident debuginfo) (@pair ident debuginfo v' i') s)) *)
(* Goal: and (not (@eq ident v' v)) (or (@eq (prod ident debuginfo) (@pair ident debuginfo v0 i) (@pair ident debuginfo v' i')) (@In (prod ident debuginfo) (@pair ident debuginfo v' i') s)) *)
  inv H3. inv H1.
  econstructor; split.
(* Goal: @list_forall2 stackframe stackframe match_stackframes (@nil stackframe) (@nil stackframe) *)
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  eapply plus_left. econstructor. apply eval_add_delta_ranges. traceEq.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor; auto.
Qed.

Lemma transf_initial_states:
  forall st1, initial_state prog st1 ->
  exists st2, initial_state tprog st2 /\ match_states st1 st2.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inversion H.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  exploit function_ptr_translated; eauto. intros [tf [A B]].
(* Goal: @ex state (fun st2 : state => and (initial_state tprog st2) (match_states (Callstate (@nil stackframe) f (Locmap.init Vundef) m0) st2)) *)
  exists (Callstate nil tf (Locmap.init Vundef) m0); split.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  econstructor; eauto. eapply (Genv.init_mem_transf_partial TRANSF); eauto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  rewrite (match_program_main TRANSF), symbols_preserved. auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  rewrite <- H3. apply sig_preserved. auto.
(* Goal: @eq (res fundef) (transf_fundef f) (@OK fundef tf) *)
  constructor. constructor. auto.
Qed.

Lemma transf_final_states:
  forall st1 st2 r,
  match_states st1 st2 -> final_state st1 r -> final_state st2 r.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. inv H0. inv H. inv H5. econstructor; eauto.
Qed.

Theorem transf_program_correct:
  forward_simulation (semantics prog) (semantics tprog).
Proof.
  eapply forward_simulation_plus.
  apply senv_preserved.
  eexact transf_initial_states.
  eexact transf_final_states.
  eexact transf_step_correct.
Qed.

End PRESERVATION.
