Section HybridThreadDiagram.
Notation the_simulation := compiler_simulation.
Parameter option_compiler_order: option (Injindex the_simulation) -> option (Injindex the_simulation) -> Prop.
(* automatic rewrite gsoThreadCode *)
Ltac auto_gsoThreadCode:=
match goal with
| [ |- context[getThreadC ?cntJ'] ] =>
match type of cntJ' with
| containsThread (updThread ?cntI _ _) ?J =>
match type of cntI with
| containsThread ?hb ?I =>
match goal with
| [ cntJ: containsThread hb J |- _ ] =>
rewrite gsoThreadCode with
(cnti:= cntI)
(cntj:= cntJ)
(cntj':= cntJ') by eassumption
end
end
end
end.
Ltac simpl_getThreadC':=
first[ rewrite gssThreadCode |
auto_gsoThreadCode
].
(*Produces all the "containThreads" that might be missing "*)
Ltac containsThread_helper:=
match goal with
| [ cntj': containsThread (updThread ?cntj _ _) ?i  |- _ ] =>
match type of cntj with
| containsThread ?hb ?j =>
progress (
first [match goal with
| [ cnti: containsThread hb i |- _ ] => idtac
end |
let CNTI:= fresh "cnti" in
assert (CNTI: containsThread hb i)
by
(eapply cntUpdate'; eapply cntj') ])
| _ => fail
end
end.
Ltac simpl_getThreadC:=
match goal with
| [  |- context [@getThreadC _ _ ?j (@updThread _ _ _ ?hb _ _ _) _ ] ] =>
let cntJ:= fresh "cntj" in
evar (cntj : containsThread hb j);
let cntJ':= eval unfold cntJ in cntJ in
rewrite gsoThreadCode with
(tp := hb)
(cntj:= cntJ')
by eassumption;
instantiate (cntJ:= ltac:(eauto));
clear cntJ
end.
Ltac containsThread_cleanup:=
repeat match goal with
| [H1: containsThread ?TP ?M,
H2: containsThread (@updThread _ _ _ ?TP _ _ _) ?M |- _] =>
replace H2 with H1 in * by apply proof_irr; clear H2
| [H1: containsThread ?TP ?M,
H2: containsThread ?TP ?M |- _] =>
replace H2 with H1 in * by apply proof_irr; clear H2
end.
Admitted.

Lemma concur_stable_thread_step_target:
forall  cd f (c1:C1) m1 c2 m2 f' ts1' m1' ts2' m2' tid,
forall (Htid1: containsThread c1 tid)
(Htid2: containsThread c2 tid)
(LT : (tid < hb')%nat)
(Hcmpt1: HybridMachine.mem_compatible hb1 Sems Semt c1 m1)
(Hcmpt2: HybridMachine.mem_compatible hb2 Sems Semt c2 m2),
concur_match cd f c1 m1 c2 m2 ->
is_ext f (Mem.nextblock m1) f' (Mem.nextblock m2) ->
semantics.mem_step (restrPermMap (FST (Hcmpt1 _ Htid1))) m1' ->
semantics.mem_step (restrPermMap (FST (Hcmpt2 _ Htid2))) m2' ->
match_self (code_inject Asm_self_simulation) f' ts1' m1' ts2' m2' ->
concur_match cd f'
(updThread Htid1  (Krun (TState _ _ ts1')) (getCurPerm m1', (snd (getThreadR Htid1))))  m1'
(updThread Htid2 (Krun (TState _ _ ts2')) (getCurPerm m2', (snd (getThreadR Htid2)))) m2'.