(* *********************************************************************)
(*                                                                     *)
(*              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.     *)
(*                                                                     *)
(* *********************************************************************)

(** Typing rules and a type inference algorithm for RTL. *)

Require Import Coqlib.
Require Import Errors.
Require Import Unityping.
Require Import Maps.
Require Import AST.
Require Import Op.
Require Import Registers.
Require Import Globalenvs.
Require Import Values.
Require Import Integers.
Require Import Memory.
Require Import Events.
Require Import RTL.
Require Import Conventions.

(** * The type system *)

(** Like Cminor and all intermediate languages, RTL can be equipped with
  a simple type system that statically guarantees that operations
  and addressing modes are applied to the right number of arguments
  and that the arguments are of the correct types.   The type algebra
  is very simple, consisting of the four types [Tint] (for integers
  and pointers), [Tfloat] (for double-precision floats), [Tlong]
  (for 64-bit integers) and [Tsingle] (for single-precision floats).

  Additionally, we impose that each pseudo-register has the same type
  throughout the function.  This requirement helps with register allocation,
  enabling each pseudo-register to be mapped to a single hardware register
  or stack location of the correct type.

  Finally, we also check that the successors of instructions
  are valid, i.e. refer to non-empty nodes in the CFG.

  The typing judgement for instructions is of the form [wt_instr f env
  instr], where [f] is the current function (used to type-check
  [Ireturn] instructions) and [env] is a typing environment
  associating types to pseudo-registers.  Since pseudo-registers have
  unique types throughout the function, the typing environment does
  not change during type-checking of individual instructions.  One
  point to note is that we have one polymorphic operator, [Omove],
  which can work over both integers and floats.
*)

Definition regenv := reg -> typ.

Section WT_INSTR.

Variable funct: function.
Variable env: regenv.

Definition valid_successor (s: node) : Prop :=
  exists i, funct.(fn_code)!s = Some i.

Definition type_of_builtin_arg (a: builtin_arg reg) : typ :=
  match a with
  | BA r => env r
  | BA_int _ => Tint
  | BA_long _ => Tlong
  | BA_float _ => Tfloat
  | BA_single _ => Tsingle
  | BA_loadstack chunk ofs => type_of_chunk chunk
  | BA_addrstack ofs => Tptr
  | BA_loadglobal chunk id ofs => type_of_chunk chunk
  | BA_addrglobal id ofs => Tptr
  | BA_splitlong hi lo => Tlong
  | BA_addptr a1 a2 => Tptr
  end.

Definition type_of_builtin_res (r: builtin_res reg) : typ :=
  match r with
  | BR r => env r
  | _    => Tint
  end.

Inductive wt_instr : instruction -> Prop :=
  | wt_Inop:
      forall s,
      valid_successor s ->
      wt_instr (Inop s)
  | wt_Iopmove:
      forall r1 r s,
      env r = env r1 ->
      valid_successor s ->
      wt_instr (Iop Omove (r1 :: nil) r s)
  | wt_Iop:
      forall op args res s,
      op <> Omove ->
      map env args = fst (type_of_operation op) ->
      env res = snd (type_of_operation op) ->
      valid_successor s ->
      wt_instr (Iop op args res s)
  | wt_Iload:
      forall chunk addr args dst s,
      map env args = type_of_addressing addr ->
      env dst = type_of_chunk chunk ->
      valid_successor s ->
      wt_instr (Iload chunk addr args dst s)
  | wt_Istore:
      forall chunk addr args src s,
      map env args = type_of_addressing addr ->
      env src = type_of_chunk chunk ->
      valid_successor s ->
      wt_instr (Istore chunk addr args src s)
  | wt_Icall:
      forall sig ros args res s,
      match ros with inl r => env r = Tptr | inr s => True end ->
      map env args = sig.(sig_args) ->
      env res = proj_sig_res sig ->
      valid_successor s ->
      wt_instr (Icall sig ros args res s)
  | wt_Itailcall:
      forall sig ros args,
      match ros with inl r => env r = Tptr | inr s => True end ->
      map env args = sig.(sig_args) ->
      sig.(sig_res) = funct.(fn_sig).(sig_res) ->
      tailcall_possible sig ->
      wt_instr (Itailcall sig ros args)
  | wt_Ibuiltin:
      forall ef args res s,
      match ef with
      | EF_annot _ _ _ | EF_debug _ _ _ => True
      | _ => map type_of_builtin_arg args = (ef_sig ef).(sig_args)
      end ->
      type_of_builtin_res res = proj_sig_res (ef_sig ef) ->
      valid_successor s ->
      wt_instr (Ibuiltin ef args res s)
  | wt_Icond:
      forall cond args s1 s2,
      map env args = type_of_condition cond ->
      valid_successor s1 ->
      valid_successor s2 ->
      wt_instr (Icond cond args s1 s2)
  | wt_Ijumptable:
      forall arg tbl,
      env arg = Tint ->
      (forall s, In s tbl -> valid_successor s) ->
      list_length_z tbl * 4 <= Int.max_unsigned ->
      wt_instr (Ijumptable arg tbl)
  | wt_Ireturn_none:
      funct.(fn_sig).(sig_res) = None ->
      wt_instr (Ireturn None)
  | wt_Ireturn_some:
      forall arg ty,
      funct.(fn_sig).(sig_res) = Some ty ->
      env arg = ty ->
      wt_instr (Ireturn (Some arg)).

End WT_INSTR.

(** A function [f] is well-typed w.r.t. a typing environment [env],
   written [wt_function env f], if all instructions are well-typed,
   parameters agree in types with the function signature, and
   parameters are pairwise distinct. *)

Record wt_function (f: function) (env: regenv): Prop :=
(* Goal: P (fn_code f) (type_code e0) *)
(* Goal: @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) (type_code e0) (@OK S.typenv e')) (S.satisf te e')) *)
  mk_wt_function {
    wt_params:
      map env f.(fn_params) = f.(fn_sig).(sig_args);
    wt_norepet:
      list_norepet f.(fn_params);
    wt_instrs:
      forall pc instr,
      f.(fn_code)!pc = Some instr -> wt_instr f env instr;
    wt_entrypoint:
      valid_successor f f.(fn_entrypoint)
}.

Inductive wt_fundef: fundef -> Prop :=
  | wt_fundef_external: forall ef,
      wt_fundef (External ef)
  | wt_function_internal: forall f env,
      wt_function f env ->
      wt_fundef (Internal f).

Definition wt_program (p: program): Prop :=
  forall i f, In (i, Gfun f) (prog_defs p) -> wt_fundef f.

(** * Type inference *)

(** Type inference reuses the generic solver for unification constraints
  defined in module [Unityping]. *)

Module RTLtypes <: TYPE_ALGEBRA.

Definition t := typ.
Definition eq := typ_eq.
Definition default := Tint.

End RTLtypes.

Module S := UniSolver(RTLtypes).

Section INFERENCE.

Local Open Scope error_monad_scope.

Variable f: function.

(** Checking the validity of successor nodes. *)

Definition check_successor (s: node): res unit :=
  match f.(fn_code)!s with
  | None => Error (MSG "bad successor " :: POS s :: nil)
  | Some i => OK tt
  end.

Fixpoint check_successors (sl: list node): res unit :=
  match sl with
  | nil => OK tt
  | s1 :: sl' => do x <- check_successor s1; check_successors sl'
  end.

(** Check structural constraints and process / record all type constraints. *)

Definition type_ros (e: S.typenv) (ros: reg + ident) : res S.typenv :=
  match ros with
  | inl r => S.set e r Tptr
  | inr s => OK e
  end.

Definition is_move (op: operation) : bool :=
  match op with Omove => true | _ => false end.

Definition type_expect (e: S.typenv) (t1 t2: typ) : res S.typenv :=
  if typ_eq t1 t2 then OK e else Error(msg "unexpected type").

Definition type_builtin_arg (e: S.typenv) (a: builtin_arg reg) (ty: typ) : res S.typenv :=
  match a with
  | BA r => S.set e r ty
  | BA_int _ => type_expect e ty Tint
  | BA_long _ => type_expect e ty Tlong
  | BA_float _ => type_expect e ty Tfloat
  | BA_single _ => type_expect e ty Tsingle
  | BA_loadstack chunk ofs => type_expect e ty (type_of_chunk chunk)
  | BA_addrstack ofs => type_expect e ty Tptr
  | BA_loadglobal chunk id ofs => type_expect e ty (type_of_chunk chunk)
  | BA_addrglobal id ofs => type_expect e ty Tptr
  | BA_splitlong hi lo => type_expect e ty Tlong
  | BA_addptr a1 a2 => type_expect e ty Tptr
  end.

Fixpoint type_builtin_args (e: S.typenv) (al: list (builtin_arg reg)) (tyl: list typ) : res S.typenv :=
  match al, tyl with
  | nil, nil => OK e
  | a1 :: al, ty1 :: tyl =>
      do e1 <- type_builtin_arg e a1 ty1; type_builtin_args e1 al tyl
  | _, _ =>
      Error (msg "builtin arity mismatch")
  end.

Definition type_builtin_res (e: S.typenv) (a: builtin_res reg) (ty: typ) : res S.typenv :=
  match a with
  | BR r => S.set e r ty
  | _    => type_expect e ty Tint
  end.

Definition type_instr (e: S.typenv) (i: instruction) : res S.typenv :=
  match i with
  | Inop s =>
      do x <- check_successor s; OK e
  | Iop op args res s =>
      do x <- check_successor s;
      if is_move op then
        match args with
        | arg :: nil => do (changed, e') <- S.move e res arg; OK e'
        | _ => Error (msg "ill-formed move")
        end
      else
       (let (targs, tres) := type_of_operation op in
        do e1 <- S.set_list e args targs; S.set e1 res tres)
  | Iload chunk addr args dst s =>
      do x <- check_successor s;
      do e1 <- S.set_list e args (type_of_addressing addr);
      S.set e1 dst (type_of_chunk chunk)
  | Istore chunk addr args src s =>
      do x <- check_successor s;
      do e1 <- S.set_list e args (type_of_addressing addr);
      S.set e1 src (type_of_chunk chunk)
  | Icall sig ros args res s =>
      do x <- check_successor s;
      do e1 <- type_ros e ros;
      do e2 <- S.set_list e1 args sig.(sig_args);
      S.set e2 res (proj_sig_res sig)
  | Itailcall sig ros args =>
      do e1 <- type_ros e ros;
      do e2 <- S.set_list e1 args sig.(sig_args);
      if opt_typ_eq sig.(sig_res) f.(fn_sig).(sig_res) then
        if tailcall_is_possible sig
        then OK e2
        else Error(msg "tailcall not possible")
      else Error(msg "bad return type in tailcall")
  | Ibuiltin ef args res s =>
      let sig := ef_sig ef in
      do x <- check_successor s;
      do e1 <-
        match ef with
        | EF_annot _ _ _ | EF_debug _ _ _ => OK e
        | _ => type_builtin_args e args sig.(sig_args)
        end;
      type_builtin_res e1 res (proj_sig_res sig)
 | Icond cond args s1 s2 =>
      do x1 <- check_successor s1;
      do x2 <- check_successor s2;
      S.set_list e args (type_of_condition cond)
 | Ijumptable arg tbl =>
      do x <- check_successors tbl;
      do e1 <- S.set e arg Tint;
      if zle (list_length_z tbl * 4) Int.max_unsigned
      then OK e1
      else Error(msg "jumptable too big")
  | Ireturn optres =>
      match optres, f.(fn_sig).(sig_res) with
      | None, None => OK e
      | Some r, Some t => S.set e r t
      | _, _ => Error(msg "bad return")
      end
  end.

Definition type_code (e: S.typenv): res S.typenv :=
  PTree.fold (fun re pc i =>
    match re with
    | Error _ => re
    | OK e =>
        match type_instr e i with
        | Error msg => Error(MSG "At PC " :: POS pc :: MSG ": " :: msg)
        | OK e' => OK e'
        end
    end)
  f.(fn_code) (OK e).

(** Solve remaining constraints *)

Definition check_params_norepet (params: list reg): res unit :=
  if list_norepet_dec Reg.eq params
  then OK tt
  else Error(msg "duplicate parameters").

Definition type_function : res regenv :=
  do e1 <- type_code S.initial;
  do e2 <- S.set_list e1 f.(fn_params) f.(fn_sig).(sig_args);
  do te <- S.solve e2;
  do x1 <- check_params_norepet f.(fn_params);
  do x2 <- check_successor f.(fn_entrypoint);
  OK te.

(** ** Soundness proof *)

Remark type_ros_incr:
  forall e ros e' te, type_ros e ros = OK e' -> S.satisf te e' -> S.satisf te e.
Proof.
(* Goal: forall (e : S.typenv) (ros : sum reg ident) (e' : S.typenv) (te : S.typassign) (_ : @eq (res S.typenv) (type_ros e ros) (@OK S.typenv e')) (_ : S.satisf te e'), match ros with | inl r => @eq RTLtypes.t (te r) Tptr | inr s => True end *)
  unfold type_ros; intros. destruct ros. eauto with ty. inv H; auto with ty.
Qed.

Hint Resolve type_ros_incr: ty.

Lemma type_ros_sound:
  forall e ros e' te, type_ros e ros = OK e' -> S.satisf te e' ->
  match ros with inl r => te r = Tptr | inr s => True end.
Proof.
(* Goal: forall (e : S.typenv) (ros : sum reg ident) (e' : S.typenv) (te : S.typassign) (_ : @eq (res S.typenv) (type_ros e ros) (@OK S.typenv e')) (_ : S.satisf te e'), match ros with | inl r => @eq RTLtypes.t (te r) Tptr | inr s => True end *)
  unfold type_ros; intros. destruct ros.
(* Goal: @eq typ (te x) ty *)
(* Goal: @eq typ Tint ty *)
(* Goal: @eq typ Tint ty *)
  eapply S.set_sound; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  auto.
Qed.

Lemma check_successor_sound:
  forall s x, check_successor s = OK x -> valid_successor f s.
Proof.
(* Goal: forall (s : node) (x : unit) (_ : @eq (res unit) (check_successor s) (@OK unit x)), valid_successor f s *)
  unfold check_successor, valid_successor; intros.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct (fn_code f)!s; inv H. exists i; auto.
Qed.

Hint Resolve check_successor_sound: ty.

Lemma check_successors_sound:
  forall sl x, check_successors sl = OK x -> forall s, In s sl -> valid_successor f s.
Proof.
(* Goal: forall (sl : list node) (x : unit) (_ : @eq (res unit) (check_successors sl) (@OK unit x)) (s : node) (_ : @In node s sl), valid_successor f s *)
  induction sl; simpl; intros.
(* Goal: valid_successor f s *)
(* Goal: valid_successor f s *)
  contradiction.
(* Goal: valid_successor f (fn_entrypoint f) *)
  monadInv H. destruct H0. subst a; eauto with ty. eauto.
Qed.

Remark type_expect_incr:
  forall e ty1 ty2 e' te, type_expect e ty1 ty2 = OK e' -> S.satisf te e' -> S.satisf te e.
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  unfold type_expect; intros. destruct (typ_eq ty1 ty2); inv H. auto.
Qed.

Hint Resolve type_expect_incr: ty.

Lemma type_expect_sound:
  forall e ty1 ty2 e', type_expect e ty1 ty2 = OK e' -> ty1 = ty2.
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  unfold type_expect; intros. destruct (typ_eq ty1 ty2); inv H. auto.
Qed.

Lemma type_builtin_arg_incr:
  forall e a ty e' te, type_builtin_arg e a ty = OK e' -> S.satisf te e' -> S.satisf te e.
Proof.
(* Goal: valid_successor f (fn_entrypoint f) *)
  unfold type_builtin_arg; intros; destruct a; eauto with ty.
Qed.

Lemma type_builtin_args_incr:
  forall a ty e e' te, type_builtin_args e a ty = OK e' -> S.satisf te e' -> S.satisf te e.
Proof.
(* Goal: forall (a : list (builtin_arg reg)) (ty : list typ) (e e' : S.typenv) (te : S.typassign) (_ : @eq (res S.typenv) (type_builtin_args e a ty) (@OK S.typenv e')) (_ : S.satisf te e'), S.satisf te e *)
  induction a; destruct ty; simpl; intros; try discriminate.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv H; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  monadInv H. eapply type_builtin_arg_incr; eauto.
Qed.

Lemma type_builtin_res_incr:
  forall e a ty e' te, type_builtin_res e a ty = OK e' -> S.satisf te e' -> S.satisf te e.
Proof.
(* Goal: valid_successor f (fn_entrypoint f) *)
  unfold type_builtin_res; intros; destruct a; inv H; eauto with ty.
Qed.

Hint Resolve type_builtin_args_incr type_builtin_res_incr: ty.

Lemma type_builtin_arg_sound:
  forall e a ty e' te,
  type_builtin_arg e a ty = OK e' -> S.satisf te e' -> type_of_builtin_arg te a = ty.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. destruct a; simpl in *; try (symmetry; eapply type_expect_sound; eassumption).
(* Goal: @eq typ (te x) ty *)
(* Goal: @eq typ Tint ty *)
(* Goal: @eq typ Tint ty *)
  eapply S.set_sound; eauto.
Qed.

Lemma type_builtin_args_sound:
  forall al tyl e e' te,
  type_builtin_args e al tyl = OK e' -> S.satisf te e' -> List.map (type_of_builtin_arg te) al = tyl.
Proof.
(* Goal: forall (al : list (builtin_arg reg)) (tyl : list typ) (e e' : S.typenv) (te : S.typassign) (_ : @eq (res S.typenv) (type_builtin_args e al tyl) (@OK S.typenv e')) (_ : S.satisf te e'), @eq (list typ) (@map (builtin_arg reg) typ (type_of_builtin_arg te) al) tyl *)
  induction al as [|a al]; destruct tyl as [|ty tyl]; simpl; intros; try discriminate.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
- auto.
(* Goal: wt_function f env *)
- monadInv H. f_equal.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply type_builtin_arg_sound; eauto with ty.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eauto.
Qed.

Lemma type_builtin_res_sound:
  forall e a ty e' te,
  type_builtin_res e a ty = OK e' -> S.satisf te e' -> type_of_builtin_res te a = ty.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. destruct a; simpl in *.
(* Goal: @eq typ (te x) ty *)
(* Goal: @eq typ Tint ty *)
(* Goal: @eq typ Tint ty *)
  eapply S.set_sound; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  symmetry; eapply type_expect_sound; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  symmetry; eapply type_expect_sound; eauto.
Qed.

Lemma type_instr_incr:
  forall e i e' te,
  type_instr e i = OK e' -> S.satisf te e' -> S.satisf te e.
Proof.
(* Goal: valid_successor f (fn_entrypoint f) *)
  intros; destruct i; try (monadInv H); eauto with ty.
- (* op *)
(* Goal: S.satisf te e *)
  destruct (is_move o) eqn:ISMOVE.
(* Goal: valid_successor f (fn_entrypoint f) *)
  destruct l; try discriminate. destruct l; monadInv EQ0. eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  destruct (type_of_operation o) as [targs tres] eqn:TYOP. monadInv EQ0. eauto with ty.
- (* tailcall *)
(* Goal: S.satisf te e *)
  destruct (opt_typ_eq (sig_res s) (sig_res (fn_sig f))); try discriminate.
(* Goal: S.satisf te e *)
  destruct (tailcall_is_possible s) eqn:TCIP; inv EQ2.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* builtin *)
(* Goal: valid_successor f (fn_entrypoint f) *)
  destruct e0; try monadInv EQ1; eauto with ty.
- (* jumptable *)
(* Goal: S.satisf te e *)
  destruct (zle (list_length_z l * 4) Int.max_unsigned); inv EQ2.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* return *)
(* Goal: S.satisf te e *)
  simpl in H. destruct o as [r|] eqn: RET; destruct (sig_res (fn_sig f)) as [t|] eqn: RES; try discriminate.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
(* Goal: S.satisf te e *)
  inv H; auto with ty.
Qed.

Lemma type_instr_sound:
  forall e i e' te,
  type_instr e i = OK e' -> S.satisf te e' -> wt_instr f te i.
Proof.
  intros; destruct i; try (monadInv H); simpl.
- (* nop *)
(* Goal: valid_successor f (fn_entrypoint f) *)
  constructor; eauto with ty.
- (* op *)
(* Goal: S.satisf te e *)
  destruct (is_move o) eqn:ISMOVE.
  (* move *)
  + assert (o = Omove) by (unfold is_move in ISMOVE; destruct o; congruence).
    subst o.
(* Goal: S.satisf te e *)
(* Goal: S.satisf te e *)
    destruct l; try discriminate. destruct l; monadInv EQ0.
(* Goal: valid_successor f (fn_entrypoint f) *)
    constructor. eapply S.move_sound; eauto. eauto with ty.
(* Goal: S.satisf te e *)
  + destruct (type_of_operation o) as [targs tres] eqn:TYOP. monadInv EQ0.
    apply wt_Iop.
    unfold is_move in ISMOVE; destruct o; congruence.
(* Goal: valid_successor f (fn_entrypoint f) *)
    rewrite TYOP. eapply S.set_list_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
    rewrite TYOP. eapply S.set_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
    eauto with ty.
- (* load *)
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_list_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* store *)
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_list_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* call *)
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply type_ros_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_list_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* tailcall *)
(* Goal: S.satisf te e *)
  destruct (opt_typ_eq (sig_res s) (sig_res (fn_sig f))); try discriminate.
(* Goal: S.satisf te e *)
  destruct (tailcall_is_possible s) eqn:TCIP; inv EQ2.
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply type_ros_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_list_sound; eauto with ty.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply tailcall_is_possible_correct; auto.
- (* builtin *)
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: valid_successor f (fn_entrypoint f) *)
  destruct e0; auto; eapply type_builtin_args_sound; eauto with ty.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply type_builtin_res_sound; eauto.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* cond *)
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eapply S.set_list_sound; eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
- (* jumptable *)
(* Goal: S.satisf te e *)
  destruct (zle (list_length_z l * 4) Int.max_unsigned); inv EQ2.
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
(* Goal: @eq typ (te x) ty *)
(* Goal: @eq typ Tint ty *)
(* Goal: @eq typ Tint ty *)
  eapply S.set_sound; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply check_successors_sound; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  auto.
- (* return *)
(* Goal: S.satisf te e *)
  simpl in H. destruct o as [r|] eqn: RET; destruct (sig_res (fn_sig f)) as [t|] eqn: RES; try discriminate.
(* Goal: valid_successor f (fn_entrypoint f) *)
  econstructor. eauto. eapply S.set_sound; eauto with ty.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv H. constructor. auto.
Qed.

Lemma type_code_sound:
  forall pc i e e' te,
  type_code e = OK e' ->
  f.(fn_code)!pc = Some i -> S.satisf te e' -> wt_instr f te i.
Proof.
(* Goal: forall (pc : positive) (i : instruction) (e e' : S.typenv) (te : S.typassign) (_ : @eq (res S.typenv) (type_code e) (@OK S.typenv e')) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)) (_ : S.satisf te e'), wt_instr f te i *)
  intros pc i e0 e1 te TCODE.
  set (P := fun c opte =>
         match opte with
         | Error _ => True
         | OK e' => c!pc = Some i -> S.satisf te e' -> wt_instr f te i
         end).
(* Goal: forall (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)) (_ : S.satisf te e1), wt_instr f te i *)
  change (P f.(fn_code) (OK e1)).
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  rewrite <- TCODE. unfold type_code. apply PTree_Properties.fold_rec; unfold P; intros.
  - (* extensionality *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    destruct a; auto; intros. rewrite <- H in H1. eapply H0; eauto.
  - (* base case *)
(* Goal: wt_instr f te i *)
    rewrite PTree.gempty in H; discriminate.
  - (* inductive case *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    destruct a as [e|?]; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    destruct (type_instr e v) as [e'|?] eqn:TYINSTR; auto.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
    intros. rewrite PTree.gsspec in H2. destruct (peq pc k).
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    inv H2. eapply type_instr_sound; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    eapply H1; eauto. eapply type_instr_incr; eauto.
Qed.

Theorem type_function_correct:
  forall env, type_function = OK env -> wt_function f env.
Proof.
(* Goal: wt_function f env *)
  unfold type_function; intros. monadInv H.
(* Goal: wt_function f env *)
  assert (SAT0: S.satisf env x0) by (eapply S.solve_sound; eauto).
(* Goal: wt_function f env *)
  assert (SAT1: S.satisf env x) by (eauto with ty).
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  constructor.
- (* type of parameters *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply S.set_list_sound; eauto.
- (* parameters are unique *)
(* Goal: @list_norepet reg (fn_params f) *)
  unfold check_params_norepet in EQ2.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct (list_norepet_dec Reg.eq (fn_params f)); inv EQ2; auto.
- (* instructions are well typed *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  intros. eapply type_code_sound; eauto.
- (* entry point is valid *)
(* Goal: valid_successor f (fn_entrypoint f) *)
  eauto with ty.
Qed.

(** ** Completeness proof *)

Lemma type_ros_complete:
  forall te ros e,
  S.satisf te e ->
  match ros with inl r => te r = Tptr | inr s => True end ->
  exists e', type_ros e ros = OK e' /\ S.satisf te e'.
Proof.
(* Goal: forall (te : S.typassign) (ros : sum positive ident) (e : S.typenv) (_ : S.satisf te e) (_ : match ros with | inl r => @eq RTLtypes.t (te r) Tptr | inr s => True end), @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) (type_ros e ros) (@OK S.typenv e')) (S.satisf te e')) *)
  intros; destruct ros; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply S.set_complete; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e; auto.
Qed.

Lemma check_successor_complete:
  forall s, valid_successor f s -> check_successor s = OK tt.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  unfold valid_successor, check_successor; intros.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct H as [i EQ]; rewrite EQ; auto.
Qed.

Lemma type_expect_complete:
  forall e ty, type_expect e ty ty = OK e.
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  unfold type_expect; intros. rewrite dec_eq_true; auto.
Qed.

Lemma type_builtin_arg_complete:
  forall te a e,
  S.satisf te e ->
  exists e', type_builtin_arg e a (type_of_builtin_arg te a) = OK e' /\ S.satisf te e'.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. destruct a; simpl; try (exists e; split; [apply type_expect_complete|assumption]).
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply S.set_complete; auto.
Qed.

Lemma type_builtin_args_complete:
  forall te al e,
  S.satisf te e ->
  exists e', type_builtin_args e al (List.map (type_of_builtin_arg te) al) = OK e' /\ S.satisf te e'.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  induction al; simpl; intros.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
- exists e; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
- destruct (type_builtin_arg_complete te a e) as (e1 & A & B); auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct (IHal e1) as (e2 & C & D); auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e2; split; auto. rewrite A. auto.
Qed.

Lemma type_builtin_res_complete:
  forall te a e,
  S.satisf te e ->
  exists e', type_builtin_res e a (type_of_builtin_res te a) = OK e' /\ S.satisf te e'.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. destruct a; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply S.set_complete; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e; auto.
Qed.

Lemma type_instr_complete:
  forall te e i,
  S.satisf te e ->
  wt_instr f te i ->
  exists e', type_instr e i = OK e' /\ S.satisf te e'.
Proof.
  induction 2; simpl.
- (* nop *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; split. rewrite check_successor_complete; simpl; eauto. auto.
- (* move *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.move_complete; eauto. intros (changed & e' & A & B).
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e'; split. rewrite check_successor_complete by auto; simpl. rewrite A; auto. auto.
- (* other op *)
  destruct (type_of_operation op) as [targ tres]. simpl in *.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_list_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_complete. eexact B. eauto. intros [e2 [C D]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e2; split; auto.
  rewrite check_successor_complete by auto; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  replace (is_move op) with false. rewrite A; simpl; rewrite C; auto.
  destruct op; reflexivity || congruence.
- (* load *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_list_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_complete. eexact B. eauto. intros [e2 [C D]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e2; split; auto.
  rewrite check_successor_complete by auto; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite A; simpl; rewrite C; auto.
- (* store *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_list_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_complete. eexact B. eauto. intros [e2 [C D]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e2; split; auto.
  rewrite check_successor_complete by auto; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite A; simpl; rewrite C; auto.
- (* call *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit type_ros_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_list_complete. eauto. eauto. intros [e2 [C D]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_complete. eexact D. eauto. intros [e3 [E F]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e3; split; auto.
  rewrite check_successor_complete by auto; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite A; simpl; rewrite C; simpl; rewrite E; auto.
- (* tailcall *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit type_ros_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_list_complete. eauto. eauto. intros [e2 [C D]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e2; split; auto.
  rewrite A; simpl; rewrite C; simpl.
  rewrite H2; rewrite dec_eq_true.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  replace (tailcall_is_possible sig) with true; auto.
  symmetry. unfold tailcall_is_possible. apply forallb_forall.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  intros. apply H3 in H4. destruct x; intuition auto.
- (* builtin *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit type_builtin_args_complete; eauto. instantiate (1 := args). intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit type_builtin_res_complete; eauto. instantiate (1 := res). intros [e2 [C D]].
  exploit type_builtin_res_complete. eexact H. instantiate (1 := res). intros [e3 [E F]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite check_successor_complete by auto. simpl.
  exists (match ef with EF_annot _ _ _ | EF_debug _ _ _ => e3 | _ => e2 end); split.
  rewrite H1 in C, E.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct ef; try (rewrite <- H0; rewrite A); simpl; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct ef; auto.
- (* cond *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_list_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e1; split; auto.
  rewrite check_successor_complete by auto; simpl.
  rewrite check_successor_complete by auto; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  auto.
- (* jumptbl *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exploit S.set_complete. eauto. eauto. intros [e1 [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  exists e1; split; auto.
(* Goal: Val.has_type res (proj_sig_res (funsig (@External function ef))) *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  replace (check_successors tbl) with (OK tt). simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite A; simpl. apply zle_true; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  revert H1. generalize tbl. induction tbl0; simpl; intros. auto.
  rewrite check_successor_complete by auto; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply IHtbl0; intros; auto.
- (* return none *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite H0. exists e; auto.
- (* return some *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite H0. apply S.set_complete; auto.
Qed.

Lemma type_code_complete:
  forall te e,
  (forall pc instr, f.(fn_code)!pc = Some instr -> wt_instr f te instr) ->
  S.satisf te e ->
  exists e', type_code e = OK e' /\ S.satisf te e'.
Proof.
(* Goal: forall (te : regenv) (e : S.typenv) (_ : forall (pc : positive) (instr : instruction) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction instr)), wt_instr f te instr) (_ : S.satisf te e), @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) (type_code e) (@OK S.typenv e')) (S.satisf te e')) *)
  intros te e0 WTC SAT0.
  set (P := fun c res =>
        (forall pc i, c!pc = Some i -> wt_instr f te i) ->
        exists e', res = OK e' /\ S.satisf te e').
(* Goal: @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) (type_code e0) (@OK S.typenv e')) (S.satisf te e')) *)
  assert (P f.(fn_code) (type_code e0)).
(* Goal: P (fn_code f) (type_code e0) *)
(* Goal: @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) (type_code e0) (@OK S.typenv e')) (S.satisf te e')) *)
  {
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
    unfold type_code. apply PTree_Properties.fold_rec; unfold P; intros.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    - apply H0. intros. apply H1 with pc. rewrite <- H; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    - exists e0; auto.
    - destruct H1 as [e [A B]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
      intros. apply H2 with pc. rewrite PTree.gso; auto. congruence.
(* Goal: @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) match a with | OK e => match type_instr e v with | OK e'0 => @OK S.typenv e'0 | Error msg => @Error S.typenv (@cons errcode (MSG (String (Ascii.Ascii true false false false false false true false) (String (Ascii.Ascii false false true false true true true false) (String (Ascii.Ascii false false false false false true false false) (String (Ascii.Ascii false false false false true false true false) (String (Ascii.Ascii true true false false false false true false) (String (Ascii.Ascii false false false false false true false false) EmptyString))))))) (@cons errcode (POS k) (@cons errcode (MSG (String (Ascii.Ascii false true false true true true false false) (String (Ascii.Ascii false false false false false true false false) EmptyString))) msg))) end | Error e => a end (@OK S.typenv e')) (S.satisf te e')) *)
      subst a.
(* Goal: @ex S.typenv (fun e' : S.typenv => and (@eq (res S.typenv) match type_instr e v with | OK e'0 => @OK S.typenv e'0 | Error msg => @Error S.typenv (@cons errcode (MSG (String (Ascii.Ascii true false false false false false true false) (String (Ascii.Ascii false false true false true true true false) (String (Ascii.Ascii false false false false false true false false) (String (Ascii.Ascii false false false false true false true false) (String (Ascii.Ascii true true false false false false true false) (String (Ascii.Ascii false false false false false true false false) EmptyString))))))) (@cons errcode (POS k) (@cons errcode (MSG (String (Ascii.Ascii false true false true true true false false) (String (Ascii.Ascii false false false false false true false false) EmptyString))) msg))) end (@OK S.typenv e')) (S.satisf te e')) *)
      destruct (type_instr_complete te e v) as [e' [C D]].
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
      auto. apply H2 with k. apply PTree.gss.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
      exists e'; split; auto. rewrite C; auto.
  }
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply H; auto.
Qed.

Theorem type_function_complete:
  forall te, wt_function f te -> exists te, type_function = OK te.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. destruct H.
(* Goal: @ex regenv (fun te : regenv => @eq (res regenv) type_function (@OK regenv te)) *)
  destruct (type_code_complete te S.initial) as (e1 & A & B).
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  auto. apply S.satisf_initial.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct (S.set_list_complete te f.(fn_params) f.(fn_sig).(sig_args) e1) as (e2 & C & D); auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  destruct (S.solve_complete te e2) as (te' & E); auto.
(* Goal: @ex regenv (fun te : regenv => @eq (res regenv) type_function (@OK regenv te)) *)
  exists te'; unfold type_function.
(* Goal: Val.has_type res (proj_sig_res (funsig (@External function ef))) *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite A; simpl. rewrite C; simpl. rewrite E; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  unfold check_params_norepet. rewrite pred_dec_true; auto. simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite check_successor_complete by auto. auto.
Qed.

End INFERENCE.

(** * Type preservation during evaluation *)

(** The type system for RTL is not sound in that it does not guarantee
  progress: well-typed instructions such as [Icall] can fail because
  of run-time type tests (such as the equality between callee and caller's
  signatures).  However, the type system guarantees a type preservation
  property: if the execution does not fail because of a failed run-time
  test, the result values and register states match the static
  typing assumptions.  This preservation property will be useful
  later for the proof of semantic equivalence between [Linear] and [Mach].
  Even though we do not need it for [RTL], we show preservation for [RTL]
  here, as a warm-up exercise and because some of the lemmas will be
  useful later. *)

Definition wt_regset (env: regenv) (rs: regset) : Prop :=
  forall r, Val.has_type (rs#r) (env r).

Lemma wt_regset_assign:
  forall env rs v r,
  wt_regset env rs ->
  Val.has_type v (env r) ->
  wt_regset env (rs#r <- v).
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros; red; intros.
(* Goal: Val.has_type (Regmap.get val r0 (Regmap.set val r v rs)) (env r0) *)
  rewrite Regmap.gsspec.
(* Goal: Val.has_type (if peq r0 r then v else Regmap.get val r0 rs) (env r0) *)
  case (peq r0 r); intro.
(* Goal: Val.has_type v (env r0) *)
(* Goal: Val.has_type (Regmap.get val r0 rs) (env r0) *)
  subst r0. assumption.
(* Goal: Val.has_type (Regmap.get val a rs) (env a) *)
(* Goal: Val.has_type_list (@map positive val (fun r : positive => Regmap.get val r rs) rl) (@map reg typ env rl) *)
  apply H.
Qed.

Lemma wt_regset_list:
  forall env rs,
  wt_regset env rs ->
  forall rl, Val.has_type_list (rs##rl) (List.map env rl).
Proof.
(* Goal: Val.has_type res (proj_sig_res (funsig (@External function ef))) *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  induction rl; simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  auto.
(* Goal: Val.has_type (Regmap.get val a rs) (env a) *)
(* Goal: Val.has_type_list (@map positive val (fun r : positive => Regmap.get val r rs) rl) (@map reg typ env rl) *)
  split. apply H. apply IHrl.
Qed.

Lemma wt_regset_setres:
  forall env rs v res,
  wt_regset env rs ->
  Val.has_type v (type_of_builtin_res env res) ->
  wt_regset env (regmap_setres res v rs).
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  intros. destruct res; simpl in *; auto. apply wt_regset_assign; auto.
Qed.

Lemma wt_init_regs:
  forall env rl args,
  Val.has_type_list args (List.map env rl) ->
  wt_regset env (init_regs args rl).
Proof.
(* Goal: forall (env : forall _ : reg, typ) (rl : list reg) (args : list val) (_ : Val.has_type_list args (@map reg typ env rl)), wt_regset env (init_regs args rl) *)
  induction rl; destruct args; simpl; intuition.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  red; intros. rewrite Regmap.gi. simpl; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply wt_regset_assign; auto.
Qed.

Lemma wt_exec_Iop:
  forall (ge: genv) env f sp op args res s rs m v,
  wt_instr f env (Iop op args res s) ->
  eval_operation ge sp op rs##args m = Some v ->
  wt_regset env rs ->
  wt_regset env (rs#res <- v).
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. inv H.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  simpl in H0. inv H0. apply wt_regset_assign; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite H4; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply wt_regset_assign; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite H8. eapply type_of_operation_sound; eauto.
Qed.

Lemma wt_exec_Iload:
  forall env f chunk addr args dst s m a v rs,
  wt_instr f env (Iload chunk addr args dst s) ->
  Mem.loadv chunk m a = Some v ->
  wt_regset env rs ->
  wt_regset env (rs#dst <- v).
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. destruct a; simpl in H0; try discriminate. inv H.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply wt_regset_assign; eauto. rewrite H8; eapply Mem.load_type; eauto.
Qed.

Lemma wt_exec_Ibuiltin:
  forall env f ef (ge: genv) args res s vargs m t vres m' rs,
  wt_instr f env (Ibuiltin ef args res s) ->
  external_call ef ge vargs m t vres m' ->
  wt_regset env rs ->
  wt_regset env (regmap_setres res vres rs).
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. inv H.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply wt_regset_setres; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  rewrite H7. eapply external_call_well_typed; eauto.
Qed.

Lemma wt_instr_at:
  forall f env pc i,
  wt_function f env -> f.(fn_code)!pc = Some i -> wt_instr f env i.
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  intros. inv H. eauto.
Qed.

Inductive wt_stackframes: list stackframe -> signature -> Prop :=
  | wt_stackframes_nil: forall sg,
      sg.(sig_res) = Some Tint ->
      wt_stackframes nil sg
  | wt_stackframes_cons:
      forall s res f sp pc rs env sg,
      wt_function f env ->
      wt_regset env rs ->
      env res = proj_sig_res sg ->
      wt_stackframes s (fn_sig f) ->
      wt_stackframes (Stackframe res f sp pc rs :: s) sg.

Inductive wt_state: state -> Prop :=
  | wt_state_intro:
      forall s f sp pc rs m env
        (WT_STK: wt_stackframes s (fn_sig f))
        (WT_FN: wt_function f env)
        (WT_RS: wt_regset env rs),
      wt_state (State s f sp pc rs m)
  | wt_state_call:
      forall s f args m,
      wt_stackframes s (funsig f) ->
      wt_fundef f ->
      Val.has_type_list args (sig_args (funsig f)) ->
      wt_state (Callstate s f args m)
  | wt_state_return:
      forall s v m sg,
      wt_stackframes s sg ->
      Val.has_type v (proj_sig_res sg) ->
      wt_state (Returnstate s v m).

Remark wt_stackframes_change_sig:
  forall s sg1 sg2,
  sg1.(sig_res) = sg2.(sig_res) -> wt_stackframes s sg1 -> wt_stackframes s sg2.
Proof.
(* Goal: forall (s : list stackframe) (f : function) (sp : val) (pc : node) (rs : regset) (m : Mem.mem) (i : instruction) (_ : wt_state (State s f sp pc rs m)) (_ : @eq (option instruction) (@PTree.get instruction pc (fn_code f)) (@Some instruction i)), @ex regenv (fun env : regenv => and (wt_instr f env i) (wt_regset env rs)) *)
  intros. inv H0.
- constructor; congruence.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
- econstructor; eauto. rewrite H3. unfold proj_sig_res. rewrite H. auto.
Qed.

Section SUBJECT_REDUCTION.

Variable p: program.

Hypothesis wt_p: wt_program p.

Let ge := Genv.globalenv p.

Lemma subject_reduction:
  forall st1 t st2, step ge st1 t st2 ->
  forall (WT: wt_state st1), wt_state st2.
Proof.
  induction 1; intros; inv WT;
  try (generalize (wt_instrs _ _ WT_FN pc _ H); intros WTI).
  (* Inop *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
  (* Iop *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto. eapply wt_exec_Iop; eauto.
  (* Iload *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto. eapply wt_exec_Iload; eauto.
  (* Istore *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
  (* Icall *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  assert (wt_fundef fd).
(* Goal: wt_regset env (Regmap.set val res v rs) *)
(* Goal: wt_regset env (Regmap.set val res v rs) *)
    destruct ros; simpl in H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    pattern fd. apply Genv.find_funct_prop with fundef unit p (rs#r).
(* Goal: forall (id : ident) (f : fundef) (_ : @In (prod ident (globdef fundef unit)) (@pair ident (globdef fundef unit) id (@Gfun fundef unit f)) (@prog_defs fundef unit p)), wt_fundef f *)
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit (@Genv.globalenv fundef unit p) b) (@Some fundef f) *)
(* Goal: Val.has_type_list (@nil val) (sig_args (funsig f)) *)
    exact wt_p. exact H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    caseEq (Genv.find_symbol ge i); intros; rewrite H1 in H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    pattern fd. apply Genv.find_funct_ptr_prop with fundef unit p b.
(* Goal: forall (id : ident) (f : fundef) (_ : @In (prod ident (globdef fundef unit)) (@pair ident (globdef fundef unit) id (@Gfun fundef unit f)) (@prog_defs fundef unit p)), wt_fundef f *)
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit (@Genv.globalenv fundef unit p) b) (@Some fundef f) *)
(* Goal: Val.has_type_list (@nil val) (sig_args (funsig f)) *)
    exact wt_p. exact H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    discriminate.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto. inv WTI; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv WTI. rewrite <- H8. apply wt_regset_list. auto.
  (* Itailcall *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  assert (wt_fundef fd).
(* Goal: wt_regset env (Regmap.set val res v rs) *)
(* Goal: wt_regset env (Regmap.set val res v rs) *)
    destruct ros; simpl in H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    pattern fd. apply Genv.find_funct_prop with fundef unit p (rs#r).
(* Goal: forall (id : ident) (f : fundef) (_ : @In (prod ident (globdef fundef unit)) (@pair ident (globdef fundef unit) id (@Gfun fundef unit f)) (@prog_defs fundef unit p)), wt_fundef f *)
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit (@Genv.globalenv fundef unit p) b) (@Some fundef f) *)
(* Goal: Val.has_type_list (@nil val) (sig_args (funsig f)) *)
    exact wt_p. exact H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    caseEq (Genv.find_symbol ge i); intros; rewrite H1 in H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    pattern fd. apply Genv.find_funct_ptr_prop with fundef unit p b.
(* Goal: forall (id : ident) (f : fundef) (_ : @In (prod ident (globdef fundef unit)) (@pair ident (globdef fundef unit) id (@Gfun fundef unit f)) (@prog_defs fundef unit p)), wt_fundef f *)
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit (@Genv.globalenv fundef unit p) b) (@Some fundef f) *)
(* Goal: Val.has_type_list (@nil val) (sig_args (funsig f)) *)
    exact wt_p. exact H0.
(* Goal: wt_fundef fd *)
(* Goal: wt_state (Callstate s fd (@map positive val (fun r : positive => Regmap.get val r rs) args) m') *)
(* Goal: wt_state (State s f sp pc' (@regmap_setres val res vres rs) m') *)
(* Goal: wt_state (State s f sp (if b then ifso else ifnot) rs m) *)
(* Goal: wt_state (State s f sp pc' rs m) *)
(* Goal: wt_state (Returnstate s (@regmap_optget val or Vundef rs) m') *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
    discriminate.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv WTI. apply wt_stackframes_change_sig with (fn_sig f); auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv WTI. rewrite <- H7. apply wt_regset_list. auto.
  (* Ibuiltin *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto. eapply wt_exec_Ibuiltin; eauto.
  (* Icond *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
  (* Ijumptable *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
  (* Ireturn *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv WTI; simpl. auto. unfold proj_sig_res; rewrite H2. auto.
  (* internal function *)
(* Goal: wt_state (State s f (Vptr stk Ptrofs.zero) (fn_entrypoint f) (init_regs args (fn_params f)) m') *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  simpl in *. inv H5.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv H1. apply wt_init_regs; auto. rewrite wt_params0. auto.
  (* external function *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  econstructor; eauto. simpl.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  eapply external_call_well_typed; eauto.
  (* return *)
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv H1. econstructor; eauto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  apply wt_regset_assign; auto. rewrite H10; auto.
Qed.

Lemma wt_initial_state:
  forall S, initial_state p S -> wt_state S.
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  intros. inv H. constructor. constructor. rewrite H3; auto.
(* Goal: (fun f : fundef => wt_fundef f) f *)
(* Goal: Val.has_type_list (@nil val) (sig_args (funsig f)) *)
  pattern f. apply Genv.find_funct_ptr_prop with fundef unit p b.
(* Goal: forall (id : ident) (f : fundef) (_ : @In (prod ident (globdef fundef unit)) (@pair ident (globdef fundef unit) id (@Gfun fundef unit f)) (@prog_defs fundef unit p)), wt_fundef f *)
(* Goal: @eq (option fundef) (@Genv.find_funct_ptr fundef unit (@Genv.globalenv fundef unit p) b) (@Some fundef f) *)
(* Goal: Val.has_type_list (@nil val) (sig_args (funsig f)) *)
  exact wt_p. exact H2.
(* Goal: Val.has_type_list (@nil val) (sig_args signature_main) *)
  rewrite H3. constructor.
Qed.

Lemma wt_instr_inv:
  forall s f sp pc rs m i,
  wt_state (State s f sp pc rs m) ->
  f.(fn_code)!pc = Some i ->
  exists env, wt_instr f env i /\ wt_regset env rs.
Proof.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  intros. inv H. exists env; split; auto.
(* Goal: Val.has_type_list args (sig_args (fn_sig f)) *)
(* Goal: wt_state (Returnstate s res m') *)
(* Goal: wt_state (State s f sp pc (Regmap.set val res vres rs) m) *)
  inv WT_FN. eauto.
Qed.

End SUBJECT_REDUCTION.


