aboutsummaryrefslogtreecommitdiffstats
path: root/scheduling/BTL_Schedulerproof.v
blob: aaa2bf80cbc8b067aff824408b81acfe5825774a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
Require Import AST Linking Values Maps Globalenvs Smallstep Registers.
Require Import Coqlib Maps Events Errors Op OptionMonad.
Require Import RTL BTL BTL_SEtheory.
Require Import BTLmatchRTL BTL_Scheduler.

Definition match_prog (p tp: program) :=
  match_program (fun _ f tf => transf_fundef f = OK tf) eq p tp.

Lemma transf_program_match:
  forall prog tprog, transf_program prog = OK tprog -> match_prog prog tprog.
Proof.
  intros. eapply match_transform_partial_program_contextual; eauto.
Qed.

Section PRESERVATION.

Variable prog: program.
Variable tprog: program.

Hypothesis TRANSL: match_prog prog tprog.

Let pge := Genv.globalenv prog.
Let tpge := Genv.globalenv tprog.

Lemma symbols_preserved s: Genv.find_symbol tpge s = Genv.find_symbol pge s.
Proof.
  rewrite <- (Genv.find_symbol_match TRANSL). reflexivity.
Qed.

Lemma senv_preserved:
  Senv.equiv pge tpge.
Proof.
  eapply (Genv.senv_match TRANSL).
Qed.

(* TODO gourdinl move this to BTL_Scheduler.v? *)
Inductive match_fundef: fundef -> fundef -> Prop :=
  | match_Internal f f': match_function f f' -> match_fundef (Internal f) (Internal f')
  | match_External ef: match_fundef (External ef) (External ef).

Inductive match_stackframes: stackframe -> stackframe -> Prop :=
  | match_stackframe_intro 
      res f sp pc rs f'
      (TRANSF: match_function f f')
      : match_stackframes (BTL.Stackframe res f sp pc rs) (BTL.Stackframe res f' sp pc rs).

Inductive match_states: state -> state -> Prop :=
  | match_states_intro 
      st f sp pc rs rs' m st' f'
      (EQREGS: forall r, rs # r = rs' # r)
      (STACKS: list_forall2 match_stackframes st st')
      (TRANSF: match_function f f')
      : match_states (State st f sp pc rs m) (State st' f' sp pc rs' m)
  | match_states_call
      st st' f f' args m
      (STACKS: list_forall2 match_stackframes st st')
      (TRANSF: match_fundef f f')
      : match_states (Callstate st f args m) (Callstate st' f' args m)
  | match_states_return
      st st' v m
      (STACKS: list_forall2 match_stackframes st st')
      : match_states (Returnstate st v m) (Returnstate st' v m)
   .

Lemma transf_function_correct f f':
  transf_function f = OK f' -> match_function f f'.
Proof.
  unfold transf_function. intros H. monadInv H.
  econstructor; eauto. eapply check_symbolic_simu_correct; eauto.
Qed.

Lemma transf_fundef_correct f f':
  transf_fundef f = OK f' -> match_fundef f f'.
Proof.
Admitted.

Lemma function_ptr_preserved:
  forall v f,
  Genv.find_funct_ptr pge v = Some f ->
  exists tf,
  Genv.find_funct_ptr tpge v = Some tf /\ transf_fundef f = OK tf.
Proof.
  intros.
  exploit (Genv.find_funct_ptr_transf_partial TRANSL); eauto.
Qed.

Lemma function_sig_translated f tf: transf_fundef f = OK tf -> funsig tf = funsig f.
Proof.
  intros H; apply transf_fundef_correct in H; destruct H; simpl; eauto.
  symmetry; erewrite preserv_fnsig; eauto.
Qed.

Theorem transf_initial_states:
  forall s1, initial_state prog s1 ->
  exists s2, initial_state tprog s2 /\ match_states s1 s2.
Proof.
  intros. inv H.
  exploit function_ptr_preserved; eauto. intros (tf & FIND &  TRANSF).
  eexists. split.
  - econstructor; eauto.
    + intros; apply (Genv.init_mem_match TRANSL); eauto.
    + replace (prog_main tprog) with (prog_main prog). rewrite symbols_preserved. eauto.
      symmetry. eapply match_program_main. eauto.
    + erewrite function_sig_translated; eauto.
  - constructor; eauto.
    + constructor.
    + apply transf_fundef_correct; auto.
Qed.

Lemma transf_final_states s1 s2 r:
  match_states s1 s2 -> final_state s1 r -> final_state s2 r.
Proof.
  intros. inv H0. inv H. inv STACKS. constructor.
Qed.

(* TODO gourdinl
   generalize with a None version of the lemma *)
Lemma iblock_istep_simulation stk1 stk2 f f' sp pc fin rs1 m rs1' m' rs2 ib1 t s
  (ISTEP: iblock_istep pge sp rs1 m ib1 rs1' m' (Some fin))
  (FSTEP : final_step tr_inputs pge stk1 f sp rs1' m' fin t s)
  (EQREGS: forall r : positive, rs1 # r = rs2 # r)
  (STACKS : list_forall2 match_stackframes stk1 stk2)
  (TRANSF : match_function f f')
  :exists ib2 rs2' s',
       (fn_code f') ! pc = Some ib2
    /\ iblock_istep tpge sp rs2 m ib2.(entry) rs2' m' (Some fin)
    /\ final_step tr_inputs tpge stk2 f' sp rs2' m' fin t s'
    /\ match_states s s'.
Proof.
  induction ib1; inv ISTEP; eauto.
  - (* BF *)
    admit.
  - (* Bseq continue *)
    eauto.
Admitted.

Lemma iblock_step_simulation stk1 stk2 f f' sp rs rs' m ibf t s1 pc
  (STEP: iblock_step tr_inputs pge stk1 f sp rs m ibf.(entry) t s1)
  (PC: (fn_code f) ! pc = Some ibf)
  (EQREGS: forall r : positive, rs # r = rs' # r)
  (STACKS: list_forall2 match_stackframes stk1 stk2)
  (TRANSF: match_function f f')
  :exists ibf' s2,
    (fn_code f') ! pc = Some ibf' /\
    iblock_step tr_inputs tpge stk2 f' sp rs' m ibf'.(entry) t s2 /\
    match_states s1 s2.
Proof.
  destruct STEP as (rs1 & m1 & fin & ISTEP & FSTEP).
  exploit iblock_istep_simulation; eauto.
  intros (ibf2 & rs2' & s' & PC' & ISTEP' & FSTEP' & MS').
  repeat eexists; eauto.
Qed.

Local Hint Constructors step: core.

Theorem step_simulation s1 s1' t s2
  (STEP: step tr_inputs pge s1 t s1')
  (MS: match_states s1 s2)
  :exists s2',
    step tr_inputs tpge s2 t s2'
  /\ match_states s1' s2'.
Proof.
  destruct STEP as [stack ibf f sp n rs m t s PC STEP | | | ]; inv MS.
  - (* iblock *)
    simplify_someHyps. intros PC.
    exploit iblock_step_simulation; eauto.
    intros (ibf' & s2 & PC2 & STEP2 & MS2). 
    eexists; split; eauto.
  - (* function internal *)
    inversion TRANSF as [xf tf MF |]; subst.
    eexists; split.
    + econstructor. erewrite <- preserv_fnstacksize; eauto.
    + erewrite preserv_fnparams; eauto.
      erewrite preserv_entrypoint; eauto.
      econstructor; eauto. 
  - (* function external *)
    inv TRANSF. eexists; split; econstructor; eauto.
    eapply external_call_symbols_preserved; eauto. apply senv_preserved.
  - (* return *)
    inv STACKS. destruct b1 as [res' f' sp' pc' rs'].
    eexists; split.
    + econstructor.
    + inv H1. econstructor; eauto.
Qed.

Theorem transf_program_correct:
  forward_simulation (fsem prog) (fsem tprog).
Proof.
  eapply forward_simulation_step with match_states; simpl; eauto. (* lock-step with respect to match_states *)
  - eapply senv_preserved.
  - eapply transf_initial_states.
  - eapply transf_final_states.
  - intros; eapply step_simulation; eauto.
Qed.

End PRESERVATION.