Require veric.Clight_core. Import Clight_core.
(*Require Import VST.concurrency.compiler_correct.*)
(* Require Import VST.concurrency.Clight_self_simulation. Require Import VST.concurrency.Asm_self_simulation. *)
(*This should be removed, change for some _core.v *)
Set Bullet Behavior "Strict Subproofs".
(** *These are all the possible external calls:*)
Section HybridProofs.
Variable p: program.
Variable tp: ia32.Asm.program.
Variable compiled: (simpl_transf_clight_program p = Errors.OK tp).
Definition Clight_semantics2 (p: program) : semantics:=
Clight.semantics2 p.
Definition Asm_semantics (tp: Asm.program) : semantics:=
Asm.semantics tp.
Definition Sems:= ClightSEM_rec.
Definition Semt:= X86SEM_rec.
Section OneThreadCompiled.
Variable (hb': nat).
Notation hb1:= (Some hb').
Notation hb2:= (Some (S hb')).
Variable U: seq.seq nat.
Notation C1:= (t_ (HybridMachine.ThreadPool hb1 Sems Semt)).
Notation C2:= (t_ (HybridMachine.ThreadPool hb2 Sems Semt)).
Notation G1:= (semG (HybridMachine.Sem hb1 Sems Semt)) .
Notation G2:= (semG (HybridMachine.Sem hb2 Sems Semt)).
(*The two following globals must be taken from the program...*)
(*and they are probaly the smae global??? *)
Definition genvS:= Clight.globalenv p.
Definition genvT:= (Genv.globalenv tp).
Definition genv:= (genvS, genvT).
Section OneThreadCompiledProofs.
(* we extract match_source from the self_simulation of Clight*)
Parameter Clight_core_inject: meminj -> CC_core -> CC_core -> Prop.
Parameter Clight_self_simulation:
self_simulation (Clight.semantics2 p) _ CC_core_to_CC_state.
Definition code_inject_source:=
match_self (code_inject Clight_self_simulation).
Parameter Asm_core_inject: meminj -> Asm.regset -> Asm.regset -> Prop.
Parameter Asm_self_simulation:
self_simulation (Asm_semantics tp) _ (Asm.State).
Definition code_inject_target:=
match_self (code_inject Asm_self_simulation).
Instance mem_Proper_match_perm_image:
Proper (eq ==> mem_equiv ==> mem_equiv ==> iff) perm_image.
Instance mem_Proper_match_perm_preimage:
Proper (eq ==> mem_equiv ==> mem_equiv ==> iff) perm_preimage.
Instance mem_Proper_match_mem:
Proper (eq ==> mem_equiv ==> mem_equiv ==> iff) match_mem.
Instance mem_Proper_code_inject_target:
Proper (eq ==> eq ==> mem_equiv ==> eq ==> mem_equiv ==> iff) code_inject_target.
Instance mem_Proper_code_inject_source:
Proper (eq ==> eq ==> mem_equiv ==> eq ==> mem_equiv ==> iff) code_inject_source.
(* Definition code_inject_source:= self_simulation.code_inject _ _ (Clight_self_simulation p). *)
(* we extract match_target from the self_simulation of Asm*)
(*Definition code_inject_target:= self_simulation.code_inject _  _ (Asm_self_simulation tp).*)
Definition make_state_Clight: CC_core -> mem -> Clight.state:=
CC_core_to_CC_state.
Definition get_state_Clight c:=
fst (CC_state_to_CC_core c).
Definition make_state_Asm
(reg: X86Machines.ErasedMachine.ThreadPool.code)
(m: mem): ia32.Asm.state:=
ia32.Asm.State reg m.
Definition get_state_Asm (st:Asm.state): X86Machines.ErasedMachine.ThreadPool.code:=
match st with
Asm.State reg _ => reg
end.
Definition compiler_simulation: fsim_properties_inj ( Clight.semantics2 p) (ia32.Asm.semantics tp)
( Clight.get_mem) (ia32.Asm.get_mem):=
simpl_clight_semantic_preservation p tp compiled.
Definition compiler_index := Injindex compiler_simulation.
Definition compiler_order := Injorder compiler_simulation.
Definition compiler_match := Injmatch_states compiler_simulation.
Definition match_compiled_states :=
fun i f c1 m1 c2 m2 =>
exists c1' m1' c2' m2' f1 f12 f2,
code_inject_source f1 c1 m1 c1' m1' /\
compiler_match i f12 (make_state_Clight c1' m1') (make_state_Asm c2' m2') /\
code_inject_target f2 c2 m2 c2' m2' /\
inject_incr (compose_meminj f1 (compose_meminj f12 f2)) f.
Notation source_match f st1 m1 st2 m2 :=
(code_inject_source f st1 m1 st2 m2).
Notation target_match f st1 m1 st2 m2 :=
(code_inject_target f st1 m1 st2 m2).
Section OneThreadCompiledMatch.
Definition HM1:=HybridMachine hb1 Sems Semt.
Definition HM2:=HybridMachine hb2 Sems Semt.
Notation Sem1:=(ConcurMachineSemantics _ _ _ HM1 U None).
Notation Sem2:=(ConcurMachineSemantics _ _ _ HM2 U None) .
Inductive condition: Set :=
running | blocked | resuming | initializing.
Definition get_condition {cT} (c:@ctl cT):=
match c with
| Krun _ => running
| Kblocked _ => blocked
| Kresume _ _ => resuming
| Kinit _ _ =>  initializing
end.
Definition get_state_inside {cT} (c:@ctl cT):=
match c with
| Krun c => Some c
| Kblocked c => Some c
| Kresume c _ => Some c
| Kinit _ _ =>  None
end.
(*This will probably need reworking*)

(* Specifications: *)
Lemma same_length_contains:
forall {ms1: C1} {ms2: C2} {i},
num_threads ms1 = num_threads ms2 ->
containsThread ms1 i ->
containsThread ms2 i.