(* *********************************************************************)
(*                                                                     *)
(*              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 clean-up of labels *)

Require Import FSets.
Require Import Coqlib Ordered Integers.
Require Import AST Linking.
Require Import Values Memory Events Globalenvs Smallstep.
Require Import Op Locations Linear.
Require Import CleanupLabels.

Module LabelsetFacts := FSetFacts.Facts(Labelset).

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

Section CLEANUP.

Variables prog tprog: program.
Hypothesis TRANSL: match_prog prog tprog.
Let ge := Genv.globalenv prog.
Let tge := Genv.globalenv tprog.

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 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 sig_function_translated:
  forall f,
  funsig (transf_fundef f) = funsig f.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. destruct f; reflexivity.
Qed.

Lemma find_function_translated:
  forall ros ls f,
  find_function ge ros ls = Some f ->
  find_function tge ros ls = Some (transf_fundef f).
Proof.
(* Goal: forall (ros : sum mreg ident) (ls : locset) (f : fundef) (_ : @eq (option fundef) (find_function ge ros ls) (@Some fundef f)), @eq (option fundef) (find_function tge ros ls) (@Some fundef (transf_fundef f)) *)
  unfold find_function; intros; destruct ros; simpl.
(* Goal: @eq (option fundef) (@Genv.find_funct fundef unit tge (ls (R m))) (@Some fundef (transf_fundef f)) *)
(* Goal: @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 (transf_fundef f)) *)
  apply functions_translated; auto.
(* Goal: @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 (transf_fundef f)) *)
  rewrite symbols_preserved. destruct (Genv.find_symbol ge i).
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit tge b) (@Some fundef (transf_fundef f)) *)
(* Goal: @eq (option fundef) (@None fundef) (@Some fundef (transf_fundef f)) *)
  apply function_ptr_translated; auto.
(* Goal: @eq (option code) (@None code) (@Some code (remove_unused_labels bto c')) *)
(* Goal: @eq (option code) (find_label lbl (remove_unused_labels bto (@cons instruction a c))) (@Some code (remove_unused_labels bto c')) *)
  congruence.
Qed.

(** Correctness of [labels_branched_to]. *)

Definition instr_branches_to (i: instruction) (lbl: label) : Prop :=
  match i with
  | Lgoto lbl' => lbl = lbl'
  | Lcond cond args lbl' => lbl = lbl'
  | Ljumptable arg tbl => In lbl tbl
  | _ => False
  end.

Remark add_label_branched_to_incr:
  forall ls i, Labelset.Subset ls (add_label_branched_to ls i).
Proof.
(* Goal: forall (ls : Labelset.t) (i : instruction), Labelset.Subset ls (add_label_branched_to ls i) *)
  intros; red; intros; destruct i; simpl; auto.
(* Goal: Labelset.In lbl (Labelset.add a (@fold_right Labelset.t Labelset.elt Labelset.add ls l)) *)
  apply Labelset.add_2; auto.
(* Goal: Labelset.In lbl (Labelset.add a (@fold_right Labelset.t Labelset.elt Labelset.add ls l)) *)
  apply Labelset.add_2; auto.
(* Goal: Labelset.In lbl (Labelset.add a (@fold_right Labelset.t Labelset.elt Labelset.add ls l)) *)
  revert H; induction l; simpl. auto. intros; apply Labelset.add_2; auto.
Qed.

Remark add_label_branched_to_contains:
  forall ls i lbl,
  instr_branches_to i lbl ->
  Labelset.In lbl (add_label_branched_to ls i).
Proof.
(* Goal: forall (ls : Labelset.t) (i : instruction) (lbl : label) (_ : instr_branches_to i lbl), Labelset.In lbl (add_label_branched_to ls i) *)
  destruct i; simpl; intros; try contradiction.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  apply Labelset.add_1; auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  apply Labelset.add_1; auto.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  revert H. induction l; simpl; intros.
(* Goal: Labelset.In lbl bto *)
(* Goal: Labelset.In lbl (@fold_left Labelset.t instruction add_label_branched_to c' (add_label_branched_to bto a)) *)
(* Goal: Labelset.In lbl (labels_branched_to c) *)
  contradiction.
(* Goal: Labelset.In lbl (Labelset.add a (@fold_right Labelset.t Labelset.elt Labelset.add ls l)) *)
  destruct H. apply Labelset.add_1; auto. apply Labelset.add_2; auto.
Qed.

Lemma labels_branched_to_correct:
  forall c i lbl,
  In i c -> instr_branches_to i lbl -> Labelset.In lbl (labels_branched_to c).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros.
  assert (forall c' bto,
             Labelset.Subset bto (fold_left add_label_branched_to c' bto)).
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction c'; intros; simpl; red; intros.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  apply IHc'. apply add_label_branched_to_incr; auto.

  assert (forall c' bto,
             In i c' -> Labelset.In lbl (fold_left add_label_branched_to c' bto)).
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction c'; simpl; intros.
(* Goal: Labelset.In lbl bto *)
(* Goal: Labelset.In lbl (@fold_left Labelset.t instruction add_label_branched_to c' (add_label_branched_to bto a)) *)
(* Goal: Labelset.In lbl (labels_branched_to c) *)
  contradiction.
(* Goal: Labelset.In lbl (@fold_left Labelset.t instruction add_label_branched_to c' (add_label_branched_to bto a)) *)
(* Goal: Labelset.In lbl (labels_branched_to c) *)
  destruct H2.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  subst a. apply H1. apply add_label_branched_to_contains; auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  apply IHc'; auto.

(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  unfold labels_branched_to. auto.
Qed.

(** Commutation with [find_label]. *)

Lemma remove_unused_labels_cons:
  forall bto i c,
  remove_unused_labels bto (i :: c) =
  match i with
  | Llabel lbl =>
      if Labelset.mem lbl bto then i :: remove_unused_labels bto c else remove_unused_labels bto c
  | _ =>
      i :: remove_unused_labels bto c
  end.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  unfold remove_unused_labels; intros. rewrite list_fold_right_eq. auto.
Qed.


Lemma find_label_commut:
  forall lbl bto,
  Labelset.In lbl bto ->
  forall c c',
  find_label lbl c = Some c' ->
  find_label lbl (remove_unused_labels bto c) = Some (remove_unused_labels bto c').
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction c; simpl; intros.
(* Goal: @eq (option code) (@None code) (@Some code (remove_unused_labels bto c')) *)
(* Goal: @eq (option code) (find_label lbl (remove_unused_labels bto (@cons instruction a c))) (@Some code (remove_unused_labels bto c')) *)
  congruence.
(* Goal: @eq (option code) (find_label lbl (remove_unused_labels bto (@cons instruction a c))) (@Some code (remove_unused_labels bto c')) *)
  rewrite remove_unused_labels_cons.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  unfold is_label in H0. destruct a; simpl; auto.
(* Goal: @eq (option code) (find_label lbl (if Labelset.mem l bto then @cons instruction (Llabel l) (remove_unused_labels bto c) else remove_unused_labels bto c)) (@Some code (remove_unused_labels bto c')) *)
  destruct (peq lbl l). subst l. inv H0.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  rewrite Labelset.mem_1; auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  simpl. rewrite peq_true. auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  destruct (Labelset.mem l bto); auto. simpl. rewrite peq_false; auto.
Qed.

Corollary find_label_translated:
  forall f i c' lbl c,
  incl (i :: c') (fn_code f) ->
  find_label lbl (fn_code f) = Some c ->
  instr_branches_to i lbl ->
  find_label lbl (fn_code (transf_function f)) =
     Some (remove_unused_labels (labels_branched_to (fn_code f)) c).
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  intros. unfold transf_function; unfold cleanup_labels; simpl.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  apply find_label_commut. eapply labels_branched_to_correct; eauto.
(* Goal: @In instruction i (fn_code f) *)
(* Goal: @eq (option code) (find_label lbl (fn_code f)) (@Some code c) *)
  apply H; auto with coqlib.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  auto.
Qed.

Lemma find_label_incl:
  forall lbl c c', find_label lbl c = Some c' -> incl c' c.
Proof.
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  induction c; simpl; intros.
(* Goal: @incl instruction c' (@nil instruction) *)
(* Goal: @incl instruction c' (@cons instruction a c) *)
  discriminate.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  destruct (is_label lbl a). inv H; auto with coqlib. auto with coqlib.
Qed.

(** Correctness of clean-up *)

Inductive match_stackframes: stackframe -> stackframe -> Prop :=
  | match_stackframe_intro:
      forall f sp ls c,
      incl c f.(fn_code) ->
      match_stackframes
        (Stackframe f sp ls c)
        (Stackframe (transf_function f) sp ls
          (remove_unused_labels (labels_branched_to f.(fn_code)) c)).

Inductive match_states: state -> state -> Prop :=
  | match_states_intro:
      forall s f sp c ls m ts
        (STACKS: list_forall2 match_stackframes s ts)
        (INCL: incl c f.(fn_code)),
      match_states (State s f sp c ls m)
                   (State ts (transf_function f) sp (remove_unused_labels (labels_branched_to f.(fn_code)) c) ls m)
  | match_states_call:
      forall s f ls m ts,
      list_forall2 match_stackframes s ts ->
      match_states (Callstate s f ls m)
                   (Callstate ts (transf_fundef f) ls m)
  | match_states_return:
      forall s ls m ts,
      list_forall2 match_stackframes s ts ->
      match_states (Returnstate s ls m)
                   (Returnstate ts ls m).

Definition measure (st: state) : nat :=
  match st with
  | State s f sp c ls m => List.length c
  | _ => O
  end.

Lemma match_parent_locset:
  forall s ts,
  list_forall2 match_stackframes s ts ->
  parent_locset ts = parent_locset s.
Proof.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  induction 1; simpl. auto. inv H; auto.
Qed.

Theorem transf_step_correct:
  forall s1 t s2, step ge s1 t s2 ->
  forall s1' (MS: match_states s1 s1'),
  (exists s2', step tge s1' t s2' /\ match_states s2 s2')
  \/ (measure s2 < measure s1 /\ t = E0 /\ match_states s2 s1')%nat.
Proof.
(* Goal: @eq (option code) (find_label lbl (remove_unused_labels bto (@cons instruction a c))) (@Some code (remove_unused_labels bto c')) *)
  induction 1; intros; inv MS; try rewrite remove_unused_labels_cons.
(* Lgetstack *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Lsetstack *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Lop *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto. instantiate (1 := v). rewrite <- H.
  apply eval_operation_preserved. exact symbols_preserved.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Lload *)
  assert (eval_addressing tge sp addr (LTL.reglist rs args) = Some a).
    rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Lstore *)
  assert (eval_addressing tge sp addr (LTL.reglist rs args) = Some a).
    rewrite <- H. apply eval_addressing_preserved. exact symbols_preserved.
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Lcall *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor. eapply find_function_translated; eauto.
  symmetry; apply sig_function_translated.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto. constructor; auto. constructor; eauto with coqlib.
(* Ltailcall *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor. erewrite match_parent_locset; eauto. eapply find_function_translated; eauto.
  symmetry; apply sig_function_translated.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  simpl. eauto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Lbuiltin *)
  left; econstructor; split.
  econstructor.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  eapply eval_builtin_args_preserved with (ge1 := ge); eauto. exact symbols_preserved.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Llabel *)
(* Goal: forall (st1 st2 : state) (r : Int.int) (_ : match_states st1 st2) (_ : final_state st1 r), final_state st2 r *)
  case_eq (Labelset.mem lbl (labels_branched_to (fn_code f))); intros.
  (* not eliminated *)
  left; econstructor; split.
  constructor.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
  (* eliminated *)
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  right. split. simpl. omega. split. auto. econstructor; eauto with coqlib.
(* Lgoto *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor. eapply find_label_translated; eauto. red; auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto. eapply find_label_incl; eauto.
(* Lcond taken *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor. auto. eauto. eapply find_label_translated; eauto. red; auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto. eapply find_label_incl; eauto.
(* Lcond not taken *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  eapply exec_Lcond_false; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* Ljumptable *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor. eauto. eauto. eapply find_label_translated; eauto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  red. eapply list_nth_z_in; eauto. eauto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto. eapply find_label_incl; eauto.
(* Lreturn *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  erewrite <- match_parent_locset; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* internal function *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; simpl; eauto.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* external function *)
  left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto. eapply external_call_symbols_preserved; eauto. apply senv_preserved.
(* Goal: @incl instruction c' (@cons instruction a c) *)
  econstructor; eauto with coqlib.
(* return *)
  inv H3. inv H1. left; econstructor; split.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  econstructor; eauto.
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. inv H.
  econstructor; split.
  eapply initial_state_intro with (f := transf_fundef f).
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  eapply (Genv.init_mem_transf TRANSL); eauto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  rewrite (match_program_main TRANSL), symbols_preserved; eauto.
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit tge b) (@Some fundef (transf_fundef f)) *)
(* Goal: @eq (option fundef) (@None fundef) (@Some fundef (transf_fundef f)) *)
  apply function_ptr_translated; auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  rewrite sig_function_translated. auto.
(* Goal: @eq locset (Locmap.init Vundef) (Locmap.init Vundef) *)
(* Goal: @eq locset match b1 with | Stackframe f sp ls c => ls end match a1 with | Stackframe f sp ls c => ls end *)
  constructor; auto. constructor.
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 (Linear.semantics prog) (Linear.semantics tprog).
Proof.
  eapply forward_simulation_opt.
  apply senv_preserved.
  eexact transf_initial_states.
  eexact transf_final_states.
  eexact transf_step_correct.
Qed.

End CLEANUP.

