-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerification.v
350 lines (336 loc) · 11.8 KB
/
Verification.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
Require Import Common.
Require Import Language.
Require Import Assembly.
Require Import Simulation.
Require Import Compiler.
Require Import Lia.
Require Import List.
Import ListNotations.
From Hammer Require Import Hammer.
From Hammer Require Import Tactics.
Import Nat.
Module Verification.
Lemma lm1 : forall a p, Compiler.compile'' (a :: p) = [] -> False.
Proof.
intros.
destruct p;
destruct a; simpl;
discriminate.
Qed.
Lemma trv : forall n, n - 0 = n.
Proof. lia. Qed.
Lemma comp_instr_lm : forall p q pc i,
Compiler.compile'' p = q ->
Common.lookup p pc i ->
Common.lookup q (Compiler.compile_index p pc)
(Compiler.comp_first i).
Proof.
induction p; destruct q; destruct i; intros; try inversion H; inversion H0;
(assert (Compiler.compile'' (a :: p) = [] -> False) by apply lm1);
try (exfalso; apply H6; assumption); ssimpl;
try (repeat apply Common.lu2; rewrite trv; now apply IHp).
Qed.
Lemma comp_instr : forall prog pc i,
Common.lookup prog pc i ->
Common.lookup (Compiler.compile'' prog)
(Compiler.compile_index prog pc)
(Compiler.comp_first i).
Proof.
assert (forall p q pc i,
Compiler.compile'' p = q ->
Common.lookup p pc i ->
Common.lookup q (Compiler.compile_index p pc)
(Compiler.comp_first i)).
apply comp_instr_lm.
auto.
Qed.
Lemma link_stable :
forall p ind i,
(forall n, i <> Assembly.Jump n /\ i <> Assembly.UJUMP /\ i <> Assembly.URET) ->
Common.lookup p ind i ->
Common.lookup (Compiler.link p) ind i.
Proof.
intros.
rewrite Compiler.link_eq.
induction H0.
- ssimpl.
+ exfalso. apply H1. exact 0. reflexivity.
+ exfalso. apply H0. exact 0. reflexivity.
- assert (Common.lookup (Compiler.link_aux xs) i y).
apply IHlookup.
sfirstorder.
apply Compiler.lookup_link_stable; try apply H; try exact 0.
assumption.
Qed.
Lemma lm2 : forall p, Compiler.compile_index p 0 = 0.
Proof.
destruct p;
now reflexivity.
Qed.
Lemma lm3 : forall p i ins,
(ins <> Language.Jump /\ ins <> Language.Ret) ->
Common.lookup p i ins ->
Compiler.compile_index p i + 1 =
Compiler.compile_index p (i + 1).
Proof.
intros; destruct ins; ssimpl;
induction H0; ssimpl; f_equal;
assert (forall p, Compiler.compile_index p 0 = 0) by apply lm2; ssimpl;
rewrite trv; rewrite trv; repeat f_equal; try assumption.
Qed.
Theorem jump_ret_lm1 :
forall p p' q_inter, Language.semantics p p' ->
Language.read_instr p Language.Jump \/
Language.read_instr p Language.Ret ->
Common.lookup (Language.mem p) (Language.ptr p) 0 ->
q_inter =
{| Assembly.prog :=
Assembly.prog (Compiler.compile' p);
Assembly.mem := Assembly.mem (Compiler.compile' p);
Assembly.pc := Assembly.pc (Compiler.compile' p) + 1;
Assembly.ac := Assembly.ac (Compiler.compile' p);|} ->
(Assembly.read_instr q_inter
(Assembly.Jump (Compiler.compile_index (Language.prog p')
(Language.pc p')))).
Proof.
Admitted.
Lemma lm4 : forall p i ins,
(ins = Language.Jump \/ ins = Language.Ret) ->
Common.lookup p i ins ->
Compiler.compile_index p i + 2 =
Compiler.compile_index p (i + 1).
Admitted.
Theorem jump_ret_lm2 :
forall p, Language.read_instr p Language.Jump \/
Language.read_instr p Language.Ret ->
Compiler.compile_index (Language.prog p) (Language.pc p + 1) =
Compiler.compile_index (Language.prog p) (Language.pc p) + 2.
Admitted.
Theorem th : Simulation.plus_forward_sim Compiler.compile
Language.semantics Assembly.semantics.
Proof.
unfold Simulation.plus_forward_sim.
intros.
inversion H.
assert (forall q2, Assembly.semantics q q2 -> Assembly.prog q2 = Assembly.prog q).
sauto.
destruct H0 eqn:T; exists (Assembly.mkState
(Assembly.prog q)
(Assembly.mem (Compiler.compile' p'))
(Assembly.pc (Compiler.compile' p'))
(Assembly.ac (Compiler.compile' p'))); split; qsimpl;
try (apply Compiler.comp;
unfold Compiler.compile'; rewrite e; reflexivity); clear H2.
- apply Common.t_base.
apply Assembly.add with (imm := 1).
+ unfold Language.read_instr, Assembly.read_instr in *.
qsimpl.
assert (Assembly.Add 1 = Compiler.comp_first Language.PtrInc).
now reflexivity.
apply link_stable. auto with *.
rewrite H.
apply comp_instr.
assumption.
+ simpl.
unfold Language.read_instr in r.
inversion r.
* destruct p'; ssimpl.
destruct p; ssimpl.
assert (Compiler.compile_index xs 0 = 0).
apply lm2.
rewrite H.
reflexivity.
* rewrite H;
rewrite <- e;
rewrite <- e0;
rewrite H0;
destruct p; ssimpl; f_equal; rewrite trv; rewrite trv; f_equal;
(apply lm3 with (ins := Language.PtrInc)); try (split; discriminate);
assumption.
+ simpl; reflexivity.
+ simpl; assumption.
+ simpl; inversion e1; reflexivity.
- apply Common.t_base.
apply Assembly.sub with (imm := 1).
+ unfold Language.read_instr, Assembly.read_instr in *.
qsimpl.
assert (Assembly.Sub 1 = Compiler.comp_first Language.PtrDec).
now reflexivity.
apply link_stable. auto with *.
rewrite H.
apply comp_instr.
assumption.
+ simpl.
unfold Language.read_instr in r.
inversion r.
* destruct p'; ssimpl.
destruct p; ssimpl.
assert (Compiler.compile_index xs 0 = 0).
apply lm2.
rewrite H.
reflexivity.
* rewrite H;
rewrite <- e;
rewrite <- e0;
rewrite H0;
destruct p; ssimpl; f_equal; rewrite trv; rewrite trv; f_equal;
(apply lm3 with (ins := Language.PtrDec)); try (split; discriminate);
assumption.
+ simpl; reflexivity.
+ simpl; assumption.
+ simpl; inversion e1; reflexivity.
- apply Common.t_base.
apply Assembly.add_ptr with (imm := 1).
+ unfold Language.read_instr, Assembly.read_instr in *.
assert (Assembly.AddPtr 1 = Compiler.comp_first Language.Inc).
now reflexivity.
apply link_stable. auto with *.
rewrite H.
apply comp_instr.
assumption.
+ simpl.
unfold Language.read_instr in r.
inversion r.
* destruct p'; ssimpl.
destruct p; ssimpl.
assert (Compiler.compile_index xs 0 = 0).
apply lm2.
rewrite H.
reflexivity.
* rewrite H.
rewrite <- e.
rewrite <- e0.
rewrite H1; destruct p; ssimpl; f_equal; rewrite trv; rewrite trv; f_equal;
(apply lm3 with (ins := Language.Inc)); try (split; discriminate);
assumption.
+ now reflexivity.
+ simpl.
destruct p, p'; ssimpl.
+ ssimpl.
+ rewrite <- e1 in *. ssimpl.
- apply Common.t_base.
apply Assembly.sub_ptr with (imm := 1).
+ unfold Language.read_instr, Assembly.read_instr in *.
assert (Assembly.SubPtr 1 = Compiler.comp_first Language.Dec).
now reflexivity.
apply link_stable. auto with *.
rewrite H.
apply comp_instr.
assumption.
+ simpl.
unfold Language.read_instr in r.
inversion r.
* destruct p'; ssimpl.
destruct p; ssimpl.
assert (Compiler.compile_index xs 0 = 0).
apply lm2.
rewrite H.
reflexivity.
* rewrite H.
rewrite <- e.
rewrite <- e0.
rewrite H1; destruct p; ssimpl; f_equal; rewrite trv; rewrite trv; f_equal;
(apply lm3 with (ins := Language.Dec)); try (split; discriminate);
assumption.
+ now reflexivity.
+ simpl.
destruct p, p'; ssimpl.
+ ssimpl.
+ rewrite <- e1 in *. ssimpl.
- destruct p'; ssimpl.
- remember (
{| Assembly.prog := Compiler.link (Compiler.compile'' (Language.prog p));
Assembly.mem := Language.mem p';
Assembly.pc := Compiler.compile_index (Language.prog p')
(Language.pc p');
Assembly.ac := Language.ptr p' |}) as q'.
assert (Assembly.read_instr (Compiler.compile' p) Assembly.Skip) as H1.
unfold Language.read_instr in r.
unfold Assembly.read_instr.
unfold Compiler.compile'. ssimpl.
apply link_stable. auto with *.
assert (Assembly.Skip = Compiler.comp_first Language.Jump).
auto.
rewrite H.
apply comp_instr; assumption.
(* skip not taken: q_inter points to Assembly.Jump n *)
pose (q_inter :=
{| Assembly.prog := Assembly.prog (Compiler.compile' p);
Assembly.mem := Assembly.mem (Compiler.compile' p);
Assembly.pc := Assembly.pc (Compiler.compile' p) + 1;
Assembly.ac := Assembly.ac (Compiler.compile' p);|}).
assert (Assembly.semantics (Compiler.compile' p) q_inter).
apply Assembly.skipz; simpl; try assumption; try reflexivity.
assert (Assembly.read_instr q_inter
(Assembly.Jump (Compiler.compile_index (Language.prog p')
(Language.pc p')))).
apply jump_ret_lm1 with (p := p); auto with *; sfirstorder.
remember (Compiler.compile_index (Language.prog p')
(Language.pc p')) as n.
apply Common.t_trans with (y := q_inter).
sfirstorder.
apply Common.t_base.
apply Assembly.jump with (addr := n); sauto.
- destruct p'; sauto.
(* skip taken: q_inter isn't relevant:*)
- apply Common.t_base.
apply Assembly.skipnz; auto.
assert (Assembly.Skip = Compiler.comp_first Language.Jump).
auto.
rewrite H.
unfold Compiler.compile'.
apply link_stable. sfirstorder.
apply comp_instr; try auto; ssimpl.
ssimpl. ssimpl.
rewrite <- e2.
rewrite <- e0.
apply jump_ret_lm2. auto.
- destruct p'; sauto.
- remember (
{| Assembly.prog := Compiler.link (Compiler.compile'' (Language.prog p));
Assembly.mem := Language.mem p';
Assembly.pc := Compiler.compile_index (Language.prog p')
(Language.pc p');
Assembly.ac := Language.ptr p' |}) as q'.
assert (Assembly.read_instr (Compiler.compile' p) Assembly.Skip) as H1.
unfold Language.read_instr in r.
unfold Assembly.read_instr.
unfold Compiler.compile'. ssimpl.
apply link_stable. auto with *.
assert (Assembly.Skip = Compiler.comp_first Language.Ret).
auto.
rewrite H.
apply comp_instr; assumption.
(* skip not taken: q_inter points to Assembly.Jump n *)
pose (q_inter :=
{| Assembly.prog := Assembly.prog (Compiler.compile' p);
Assembly.mem := Assembly.mem (Compiler.compile' p);
Assembly.pc := Assembly.pc (Compiler.compile' p) + 1;
Assembly.ac := Assembly.ac (Compiler.compile' p);|}).
assert (Assembly.semantics (Compiler.compile' p) q_inter).
apply Assembly.skipz; simpl; try assumption; try reflexivity.
assert (Assembly.read_instr q_inter
(Assembly.Jump (Compiler.compile_index (Language.prog p')
(Language.pc p')))).
apply jump_ret_lm1 with (p := p); auto with *; sfirstorder.
remember (Compiler.compile_index (Language.prog p')
(Language.pc p')) as n.
apply Common.t_trans with (y := q_inter).
sfirstorder.
apply Common.t_base.
apply Assembly.jump with (addr := n); sauto.
- destruct p'. sauto.
- apply Common.t_base.
apply Assembly.skipnz; auto.
assert (Assembly.Skip = Compiler.comp_first Language.Ret).
auto.
rewrite H.
unfold Compiler.compile'.
apply link_stable. sfirstorder.
apply comp_instr; try auto; ssimpl.
ssimpl. ssimpl.
rewrite <- e2.
rewrite <- e0.
apply jump_ret_lm2. auto.
Qed.
End Verification.