End EQ_SIMULATION_PLUS.
End Sim_EQ_SIMU_DIAGRAMS.
Section Sim_EXT_SIMU_DIAGRAMS.
Context {F1 V1 C1 F2 V2 C2:Type}
{Sem1 : CoreSemantics (Genv.t F1 V1) C1 mem}
{Sem2 : CoreSemantics (Genv.t F2 V2) C2 mem}
{ge1: Genv.t F1 V1}
{ge2: Genv.t F2 V2}
{entry_points : list (val * val * signature)}.
Let core_data := C1.
Variable match_states: core_data -> C1 -> mem -> C2 -> mem -> Prop.
Hypothesis genvs_dom_eq: genvs_domain_eq ge1 ge2.
Hypothesis Hyp_valid:
forall cd c1 m1 c2 m2,
match_states cd c1 m1 c2 m2 -> forall b,
(Mem.valid_block m1 b <-> Mem.valid_block m2 b).
Hypothesis match_initial_cores: forall v1 v2 sig,
In (v1,v2,sig) entry_points ->
forall vals vals' m1 m2,
Forall2 Val.lessdef vals vals' ->
Mem.extends m1 m2 ->
exists c1, exists c2,
initial_core Sem1 ge1 v1 vals = Some c1 /\
initial_core Sem2 ge2 v2 vals' = Some c2 /\
match_states c1 c1 m1 c2 m2.
Hypothesis ext_halted:
forall cd st1 m1 st2 m2 v1,
match_states cd st1 m1 st2 m2 ->
halted Sem1 st1 = Some v1 ->
exists v2, Val.lessdef v1 v2 /\
halted Sem2 st2 = Some v2 /\
Mem.extends m1 m2.
Hypothesis ext_at_external:
forall d st1 m1 st2 m2 e vals1 sig,
(d = st1 /\ match_states d st1 m1 st2 m2) ->
at_external Sem1 st1 = Some (e, sig, vals1) ->
exists vals2,
Mem.extends m1 m2 /\
Forall2 Val.lessdef vals1 vals2 /\
at_external Sem2 st2 = Some (e,sig,vals2).
Hypothesis ext_after_external:
forall d st1 st2 m1 m2 e vals1 vals2 ret1 ret2 m1' m2' sig,
(d=st1 /\ match_states d st1 m1 st2 m2) ->
at_external Sem1 st1 = Some (e,sig,vals1) ->
at_external Sem2 st2 = Some (e,sig,vals2) ->
Forall2 Val.lessdef vals1 vals2 ->
mem_forward m1 m1' ->
mem_forward m2 m2' ->
Mem.unchanged_on (loc_out_of_bounds m1) m2 m2' ->
(*ie spill-locations didn't change*)
Val.lessdef ret1 ret2 ->
Mem.extends m1' m2' ->
exists st1', exists st2', exists d',
after_external Sem1 (Some ret1) st1 = Some st1' /\
after_external Sem2 (Some ret2) st2 = Some st2' /\
d' = st1' /\ match_states d' st1' m1' st2' m2'.
Section EXT_SIMULATION_STAR_WF.
Variable order: C1 -> C1 -> Prop.
Hypothesis order_wf: well_founded order.
Hypothesis ext_simulation:
forall c1 m1 c1' m1',  corestep Sem1 ge1 c1 m1 c1' m1' ->
forall c2 m2, match_states c1 c1 m1 c2 m2 ->
exists c2', exists m2',
match_states c1' c1' m1' c2' m2' /\
(corestep_plus Sem2 ge2  c2 m2 c2' m2' \/
(corestep_star Sem2 ge2 c2 m2 c2' m2' /\ order c1' c1)).

(* Specifications: *)
Lemma ext_simulation_star_wf:
Forward_simulation_ext.Forward_simulation_extends Sem1 Sem2 ge1 ge2 entry_points.