-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModSem.v
289 lines (239 loc) · 8.68 KB
/
ModSem.v
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
Require Import Events.
Require Import Values.
Require Import AST.
Require Import Memory.
Require Import Globalenvs.
Require Import Smallstep.
From Paco Require Import paco.
Require Import sflib.
Require Import Skeleton.
Require Import CoqlibC.
Require Asm.
Set Implicit Arguments.
Module Args.
Inductive t: Type :=
| Cstyle
(fptr: val)
(vs: list val)
(m: mem)
| Asmstyle
(rs: Asm.regset)
(m: mem).
(* intentional: it should work for both cases *)
Definition get_fptr (args: Args.t): val :=
match args with
| Args.Cstyle fptr _ _ => fptr
| Args.Asmstyle rs _ => rs Asm.PC
end
.
(* intentional: it should work for both cases *)
Definition get_m (args: Args.t): mem :=
match args with
| Args.Cstyle _ _ m => m
| Args.Asmstyle _ m => m
end
.
(* for backward compatibility: it should be used only when "args" is known to be C-style. *)
(* it will return dummy value if it is assembly style. *)
Definition fptr (args: Args.t): val :=
match args with
| Args.Cstyle fptr _ _ => fptr
| Args.Asmstyle rs _ => Vundef (* should not happen *)
end
.
(* for backward compatibility: it should be used only when "args" is known to be C-style. *)
(* it will return dummy value if it is assembly style. *)
Definition vs (args: Args.t): list val :=
match args with
| Args.Cstyle _ vs _ => vs
| Args.Asmstyle _ _ => [] (* should not happen *)
end
.
(* for backward compatibility: it should be used only when "args" is known to be C-style. *)
(* it will return dummy value if it is assembly style. *)
Definition m (args: Args.t): mem :=
match args with
| Args.Cstyle _ _ m => m
| Args.Asmstyle _ m => Mem.empty (* should not happen *)
end
.
(* for backward compatibility *)
Definition mk (fptr: val) (vs: list val) (m: mem): t := Args.Cstyle fptr vs m.
Definition is_cstyle (args: t): bool :=
match args with
| Cstyle _ _ _ => true
| _ => false
end
.
Lemma get_m_m: forall args (CSTYLE: is_cstyle args), (get_m args) = (m args). Proof. destruct args; ss. Qed.
End Args.
Module Retv.
Inductive t: Type :=
| Cstyle
(v: val)
(m: mem)
| Asmstyle
(rs: Asm.regset)
(m: mem).
(* intentional: it should work for both cases *)
Definition get_m (retv: Retv.t): mem :=
match retv with
| Retv.Cstyle _ m => m
| Retv.Asmstyle _ m => m
end
.
(* for backward compatibility: it should be used only when "args" is known to be C-style. *)
(* it will return dummy value if it is assembly style. *)
Definition v (retv: Retv.t): val :=
match retv with
| Retv.Cstyle v _ => v
| Retv.Asmstyle _ _ => Vundef (* should not happen *)
end
.
(* for backward compatibility: it should be used only when "args" is known to be C-style. *)
(* it will return dummy value if it is assembly style. *)
Definition m (retv: Retv.t): mem :=
match retv with
| Retv.Cstyle _ m => m
| Retv.Asmstyle _ m => Mem.empty (* should not happen *)
end
.
(* for backward compatibility *)
Definition mk (v: val) (m: mem): t := Retv.Cstyle v m.
Definition is_cstyle (retv: t): bool :=
match retv with
| Cstyle _ _ => true
| _ => false
end
.
Lemma get_m_m: forall retv (CSTYLE: is_cstyle retv), (get_m retv) = (m retv). Proof. destruct retv; ss. Qed.
End Retv.
Hint Unfold Args.is_cstyle Args.mk Args.fptr Args.vs Args.m Retv.is_cstyle Retv.mk Retv.v Retv.m.
Hint Constructors Args.t Retv.t.
Module ModSem.
Record t: Type := mk {
state: Type;
genvtype: Type;
step (se: Senv.t) (ge: genvtype) (st0: state) (tr: trace) (st1: state): Prop;
at_external (st0: state) (args: Args.t): Prop;
initial_frame (args: Args.t) (st0: state): Prop;
final_frame (st0: state) (retv: Retv.t): Prop;
after_external (st0: state) (retv: Retv.t) (st1: state): Prop;
globalenv: genvtype;
skenv: SkEnv.t;
skenv_link: SkEnv.t;
at_external_dtm: forall st args0 args1
(AT0: at_external st args0)
(AT1: at_external st args1),
args0 = args1;
final_frame_dtm: forall st retv0 retv1
(FINAL0: final_frame st retv0)
(FINAL1: final_frame st retv1),
retv0 = retv1;
after_external_dtm: forall st_call retv st0 st1
(AFTER0: after_external st_call retv st0)
(AFTER0: after_external st_call retv st1),
st0 = st1;
is_call (st0: state): Prop := exists args, at_external st0 args;
is_step (st0: state): Prop := exists tr st1, step skenv_link globalenv st0 tr st1;
is_return (st0: state): Prop := exists retv, final_frame st0 retv;
call_step_disjoint: is_call /1\ is_step <1= bot1;
step_return_disjoint: is_step /1\ is_return <1= bot1;
call_return_disjoint: is_call /1\ is_return <1= bot1;
}.
Ltac tac :=
try( let TAC := u; esplits; eauto in
u in *; des_safe;
first[ exfalso; eapply ModSem.call_step_disjoint; TAC; fail
| exfalso; eapply ModSem.step_return_disjoint; TAC; fail
| exfalso; eapply ModSem.call_return_disjoint; TAC; fail]
).
Definition to_semantics (ms: t) :=
(Semantics_gen ms.(step) bot1 bot2 ms.(globalenv) ms.(skenv_link)).
Module Atomic.
Section Atomic.
Local Coercion ModSem.to_semantics: ModSem.t >-> Smallstep.semantics.
Variable ms: t.
Let state := (trace * ms.(state))%type.
Inductive step (se: Senv.t) (ge: ms.(genvtype)): state -> trace -> state -> Prop :=
| step_silent
st0 st1
(STEPSIL: Step ms st0 E0 st1):
step se ge (E0, st0) E0 (E0, st1)
| step_start
st0 st1 ev tr
(STEPEV: Step ms st0 (ev :: tr) st1):
step se ge (E0, st0) [ev] (tr, st1)
| step_continue
ev tr st0
(WBT: output_trace (ev :: tr)):
step se ge (ev :: tr, st0) [ev] (tr, st0).
Definition at_external (st0: state) (args: Args.t): Prop :=
(fst st0) = [] /\ ms.(at_external) (snd st0) args.
Definition initial_frame (args: Args.t) (st0: state): Prop :=
(fst st0) = [] /\ ms.(initial_frame) args (snd st0).
Definition final_frame (st0: state) (retv: Retv.t): Prop :=
(fst st0) = [] /\ ms.(final_frame) (snd st0) retv.
Definition after_external (st0: state) (retv: Retv.t) (st1: state): Prop :=
(fst st0) = [] /\ ms.(after_external) (snd st0) retv (snd st1) /\ (fst st1) = [].
Program Definition trans: t :=
mk step at_external initial_frame final_frame after_external
ms.(globalenv) ms.(skenv) ms.(skenv_link) _ _ _ _ _ _.
Next Obligation. rr in AT0. rr in AT1. des. eapply at_external_dtm; eauto. Qed.
Next Obligation. rr in FINAL0. rr in FINAL1. des. eapply final_frame_dtm; eauto. Qed.
Next Obligation.
rr in AFTER0. rr in AFTER1. des. destruct st0, st1; ss. clarify. f_equal.
eapply after_external_dtm; eauto.
Qed.
Next Obligation.
ii. des. destruct x0, st1; ss. rr in PR. ss. des. clarify.
eapply call_step_disjoint; eauto. esplits; eauto.
{ rr. esplits; eauto. }
{ rr. inv PR0; esplits; eauto. }
Qed.
Next Obligation.
ii. des. destruct x0, st1; ss. rr in PR0. ss. des. clarify.
eapply step_return_disjoint; eauto. esplits; eauto; cycle 1.
{ rr. esplits; eauto. }
{ rr. inv PR; esplits; eauto. }
Qed.
Next Obligation.
ii. des. destruct x0; ss. rr in PR. rr in PR0. ss. des. clarify.
eapply call_return_disjoint; eauto. esplits; eauto; rr; esplits; eauto.
Qed.
Lemma atomic_continue tr0 tr1 st_src0
(WBT: output_trace (tr1 ** tr0)):
star step (skenv_link ms) (globalenv ms) (tr1 ** tr0, st_src0) tr1 (tr0, st_src0).
Proof.
ginduction tr1; ii; ss.
{ econs; eauto. }
des. econs; eauto; cycle 1.
{ instantiate (1:= [_]). ss. }
econs; eauto. ss.
Qed.
Lemma atomic_lift_step st_src0 tr st_src1
(WBT: well_behaved_traces ms)
(STEP: Step ms st_src0 tr st_src1):
Star trans ([], st_src0) tr ([], st_src1).
Proof.
destruct tr; ss.
{ apply star_one. econs; eauto. }
eapply star_trans; swap 2 3.
{ eapply star_one with (t := [e]). econs; eauto. }
{ ss. }
rpapply atomic_continue; ss; unfold Eapp in *; try rewrite app_nil_r in *; eauto. exploit WBT; eauto.
Qed.
Lemma atomic_lift_star st_src0 tr st_src1
(WBT: well_behaved_traces ms)
(STAR: Star ms st_src0 tr st_src1):
Star trans ([], st_src0) tr ([], st_src1).
Proof.
ginduction STAR; ii; ss.
{ econs; eauto. }
eapply star_trans; eauto. clear - H WBT. exploit atomic_lift_step; eauto.
Qed.
End Atomic.
End Atomic.
End ModSem.
Hint Unfold ModSem.is_call ModSem.is_step ModSem.is_return.
Coercion ModSem.to_semantics: ModSem.t >-> semantics.