Section CompileOneThread.
Context (hb: nat).
(*Instantiate definitions in Concur with the current hybridbound*)
Notation concur_match:= (concur_match hb).
Notation match_thread:= (match_thread hb).
Notation match_thread_source:= (match_thread_source hb).
Notation match_thread_target:= (match_thread_target hb).
Notation memcompat2:= (memcompat2 hb).
Notation memcompat1:= (memcompat1 hb).
Notation contains12:= (contains12 hb).
Notation mtch_target:= (mtch_target hb).
Notation mtch_compiled:= (mtch_compiled hb).
Notation mtch_source:= (mtch_source hb).
Notation thread_perms st i cnt:= (fst (@getThreadR _ _ st i cnt)).
Notation lock_perms st i cnt:= (snd (@getThreadR  _ _ st i cnt)).
Definition cast_t {Sem}:
@OrdinalPool.t _ Sem -> @ThreadPool.t _ Sem (@OrdinalThreadPool dryResources _):=
fun X => X.
(* Where to move this:*)
(* ConcurMatch used to be here. *)
(* The following tactics are also in permissions.v but for some reason that one doesn't work... *)
Ltac unfold_getCurPerm:=
repeat rewrite getCurPerm_correct in *;
unfold permission_at in *.
Ltac unfold_getMaxPerm:=
repeat rewrite getMaxPerm_correct in *;
unfold permission_at in *.
Ltac unfold_getPerm:=
try unfold_getMaxPerm; try unfold_getMaxPerm.
(** *Tactics These tactics are here becasue they must be outside a section. they also must be after concur_match definition. *)
(*Do I have to reppeat the LTAC from the section? *)
Inductive opt_rel' {A} (ord: A -> A -> Prop): option A -> option A -> Prop:=
| Some_ord:
forall x y, ord x y -> opt_rel' ord (Some x) (Some y).
(*This lemma is used when the compiled thread steps*)
Ltac exploit_match tac:=
unfold match_thread_target,match_thread_source in *;
repeat match goal with
| [ H: ThreadPool.getThreadC ?i = _ ?c |- _] => simpl in H
end;
match goal with
| [ H: getThreadC ?i = _ ?c,
H0: context[match_thread] |- _ ] =>
match type of H0 with
| forall (_: ?Hlt1Type) (_: ?Hlt2Type), _ =>
assert (Hlt1:Hlt1Type); [
first [eassumption | tac | idtac]|
assert (Hlt2:Hlt2Type); [
first [eassumption | tac | idtac]|
specialize (H0 Hlt1 Hlt2);
rewrite H in H0; inversion H0; subst; simpl in *; clear H0
]]
end
| [ H: getThreadC ?i = _ ?c,
H0: context[match_thread_compiled] |- _ ] =>
match type of H0 with
| forall (_: ?Hlt1Type) (_: ?Hlt2Type), _ =>
assert (Hlt1:Hlt1Type); [
first [eassumption | tac | idtac]|
assert (Hlt2:Hlt2Type); [
first [eassumption | tac | idtac]|
specialize (H0 Hlt1 Hlt2);
rewrite H in H0; inversion H0; subst; simpl in *; clear H0
]]
end
end;
fold match_thread_target in *;
fold match_thread_source in *.
(* Build the concur_match *)
Ltac destroy_ev_step_sum:=
match goal with
| [ H: ev_step_sum _ _ _ _ _ _ _ |- _ ] => inversion H; clear H
end.

Lemma thread_step_plus_from_corestep:
forall (m : option mem) (tge : ClightSemanticsForMachines.G * Asm.genv)
i
(U : list nat) (st1 : t) (m1 : mem) (Htid : containsThread st1 i)
(st2 : t) (mu : meminj) (m2 : mem) (cd0 : compiler_index)
(CMatch : concur_match (Some cd0) mu st1 m1 st2 m2) (code2 : Asm.state)
(s4' : Smallstep.state (Asm.part_semantics Asm_g))
(m4' : mem) (cnt2 : containsThread st2 i),
getThreadC cnt2 = Krun (TST code2) ->
HybridMachineSig.schedPeek U = Some i ->
corestep_plus (Asm_core.Asm_core_sem Asm_g) code2
(restrPermMap
(proj1 ((memcompat2 CMatch) i (contains12 CMatch Htid))))
s4' m4' ->
machine_semantics_lemmas.thread_step_plus
(HybConcSem (Some (S hb)) m) tge U st2
m2 (updThread cnt2 (Krun (TState Clight.state Asm.state s4'))
(getCurPerm m4', snd (getThreadR cnt2))) m4'.