1 | include "ASM/Assembly.ma". |
---|
2 | include "ASM/Interpret.ma". |
---|
3 | |
---|
4 | (* RUSSEL **) |
---|
5 | |
---|
6 | include "basics/jmeq.ma". |
---|
7 | |
---|
8 | notation > "hvbox(a break ≃ b)" |
---|
9 | non associative with precedence 45 |
---|
10 | for @{ 'jmeq ? $a ? $b }. |
---|
11 | |
---|
12 | notation < "hvbox(term 46 a break maction (≃) (≃\sub(t,u)) term 46 b)" |
---|
13 | non associative with precedence 45 |
---|
14 | for @{ 'jmeq $t $a $u $b }. |
---|
15 | |
---|
16 | interpretation "john major's equality" 'jmeq t x u y = (jmeq t x u y). |
---|
17 | |
---|
18 | lemma eq_to_jmeq: |
---|
19 | ∀A: Type[0]. |
---|
20 | ∀x, y: A. |
---|
21 | x = y → x ≃ y. |
---|
22 | // |
---|
23 | qed. |
---|
24 | |
---|
25 | definition inject : ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ λA,P,a,p. dp … a p. |
---|
26 | definition eject : ∀A.∀P: A → Prop.(Σx:A.P x) → A ≝ λA,P,c.match c with [ dp w p ⇒ w]. |
---|
27 | |
---|
28 | coercion inject nocomposites: ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ inject on a:? to Σx:?.?. |
---|
29 | coercion eject nocomposites: ∀A.∀P:A → Prop.∀c:Σx:A.P x.A ≝ eject on _c:Σx:?.? to ?. |
---|
30 | |
---|
31 | axiom VOID: Type[0]. |
---|
32 | axiom assert_false: VOID. |
---|
33 | definition bigbang: ∀A:Type[0].False → VOID → A. |
---|
34 | #A #abs cases abs |
---|
35 | qed. |
---|
36 | |
---|
37 | coercion bigbang nocomposites: ∀A:Type[0].False → ∀v:VOID.A ≝ bigbang on _v:VOID to ?. |
---|
38 | |
---|
39 | lemma sig2: ∀A.∀P:A → Prop. ∀p:Σx:A.P x. P (eject … p). |
---|
40 | #A #P #p cases p #w #q @q |
---|
41 | qed. |
---|
42 | |
---|
43 | lemma jmeq_to_eq: ∀A:Type[0]. ∀x,y:A. x≃y → x=y. |
---|
44 | #A #x #y #JMEQ @(jmeq_elim ? x … JMEQ) % |
---|
45 | qed. |
---|
46 | |
---|
47 | coercion jmeq_to_eq: ∀A:Type[0]. ∀x,y:A. ∀p:x≃y.x=y ≝ jmeq_to_eq on _p:?≃? to ?=?. |
---|
48 | |
---|
49 | (* END RUSSELL **) |
---|
50 | |
---|
51 | let rec foldl_strong_internal |
---|
52 | (A: Type[0]) (P: list A → Type[0]) (l: list A) |
---|
53 | (H: ∀prefix. ∀hd. ∀tl. l = prefix @ [hd] @ tl → P prefix → P (prefix @ [hd])) |
---|
54 | (prefix: list A) (suffix: list A) (acc: P prefix) on suffix: |
---|
55 | l = prefix @ suffix → P(prefix @ suffix) ≝ |
---|
56 | match suffix return λl'. l = prefix @ l' → P (prefix @ l') with |
---|
57 | [ nil ⇒ λprf. ? |
---|
58 | | cons hd tl ⇒ λprf. ? |
---|
59 | ]. |
---|
60 | [ > (append_nil ?) |
---|
61 | @ acc |
---|
62 | | applyS (foldl_strong_internal A P l H (prefix @ [hd]) tl ? ?) |
---|
63 | [ @ (H prefix hd tl prf acc) |
---|
64 | | applyS prf |
---|
65 | ] |
---|
66 | ] |
---|
67 | qed. |
---|
68 | |
---|
69 | definition foldl_strong ≝ |
---|
70 | λA: Type[0]. |
---|
71 | λP: list A → Type[0]. |
---|
72 | λl: list A. |
---|
73 | λH: ∀prefix. ∀hd. ∀tl. l = prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]). |
---|
74 | λacc: P [ ]. |
---|
75 | foldl_strong_internal A P l H [ ] l acc (refl …). |
---|
76 | |
---|
77 | definition bit_elim: ∀P: bool → bool. bool ≝ |
---|
78 | λP. |
---|
79 | P true ∧ P false. |
---|
80 | |
---|
81 | let rec bitvector_elim_internal |
---|
82 | (n: nat) (P: BitVector n → bool) (m: nat) on m: m ≤ n → BitVector (n - m) → bool ≝ |
---|
83 | match m return λm. m ≤ n → BitVector (n - m) → bool with |
---|
84 | [ O ⇒ λprf1. λprefix. P ? |
---|
85 | | S n' ⇒ λprf2. λprefix. bit_elim (λbit. bitvector_elim_internal n P n' ? ?) |
---|
86 | ]. |
---|
87 | [ applyS prefix |
---|
88 | | letin res ≝ (bit ::: prefix) |
---|
89 | < (minus_S_S ? ?) |
---|
90 | > (minus_Sn_m ? ?) |
---|
91 | [ @ res |
---|
92 | | @ prf2 |
---|
93 | ] |
---|
94 | | /2/ |
---|
95 | ]. |
---|
96 | qed. |
---|
97 | |
---|
98 | definition bitvector_elim ≝ |
---|
99 | λn: nat. |
---|
100 | λP: BitVector n → bool. |
---|
101 | bitvector_elim_internal n P n ? ?. |
---|
102 | [ @ (le_n ?) |
---|
103 | | < (minus_n_n ?) |
---|
104 | @ [[ ]] |
---|
105 | ] |
---|
106 | qed. |
---|
107 | |
---|
108 | axiom vector_associativity_of_append: |
---|
109 | ∀A: Type[0]. |
---|
110 | ∀n, m, o: nat. |
---|
111 | ∀v: Vector A n. |
---|
112 | ∀q: Vector A m. |
---|
113 | ∀r: Vector A o. |
---|
114 | ((v @@ q) @@ r) |
---|
115 | ≃ |
---|
116 | (v @@ (q @@ r)). |
---|
117 | |
---|
118 | axiom vector_cons_append: |
---|
119 | ∀A: Type[0]. |
---|
120 | ∀n: nat. |
---|
121 | ∀a: A. |
---|
122 | ∀v: Vector A n. |
---|
123 | a ::: v = [[ a ]] @@ v. |
---|
124 | |
---|
125 | lemma super_rewrite2: |
---|
126 | ∀A:Type[0].∀n,m.∀v1: Vector A n.∀v2: Vector A m. |
---|
127 | ∀P: ∀m. Vector A m → Prop. |
---|
128 | n=m → v1 ≃ v2 → P n v1 → P m v2. |
---|
129 | #A #n #m #v1 #v2 #P #EQ <EQ in v2; #V #JMEQ >JMEQ // |
---|
130 | qed. |
---|
131 | |
---|
132 | lemma mem_middle_vector: |
---|
133 | ∀A: Type[0]. |
---|
134 | ∀m, o: nat. |
---|
135 | ∀eq: A → A → bool. |
---|
136 | ∀reflex: ∀a. eq a a = true. |
---|
137 | ∀p: Vector A m. |
---|
138 | ∀a: A. |
---|
139 | ∀r: Vector A o. |
---|
140 | mem A eq ? (p@@(a:::r)) a = true. |
---|
141 | # A # M # O # EQ # REFLEX # P # A |
---|
142 | elim P |
---|
143 | [ normalize |
---|
144 | > (REFLEX A) |
---|
145 | normalize |
---|
146 | # H |
---|
147 | % |
---|
148 | | # NN # AA # PP # IH |
---|
149 | normalize |
---|
150 | cases (EQ A AA) // |
---|
151 | @ IH |
---|
152 | ] |
---|
153 | qed. |
---|
154 | |
---|
155 | lemma mem_monotonic_wrt_append: |
---|
156 | ∀A: Type[0]. |
---|
157 | ∀m, o: nat. |
---|
158 | ∀eq: A → A → bool. |
---|
159 | ∀reflex: ∀a. eq a a = true. |
---|
160 | ∀p: Vector A m. |
---|
161 | ∀a: A. |
---|
162 | ∀r: Vector A o. |
---|
163 | mem A eq ? r a = true → mem A eq ? (p @@ r) a = true. |
---|
164 | # A # M # O # EQ # REFLEX # P # A |
---|
165 | elim P |
---|
166 | [ #R #H @H |
---|
167 | | #NN #AA # PP # IH #R #H |
---|
168 | normalize |
---|
169 | cases (EQ A AA) |
---|
170 | [ normalize % |
---|
171 | | @ IH @ H |
---|
172 | ] |
---|
173 | ] |
---|
174 | qed. |
---|
175 | |
---|
176 | lemma subvector_hd_tl: |
---|
177 | ∀A: Type[0]. |
---|
178 | ∀o, n: nat. |
---|
179 | ∀eq: A → A → bool. |
---|
180 | ∀refl: ∀a. eq a a = true. |
---|
181 | ∀h: Vector A o. |
---|
182 | ∀v: Vector A n. |
---|
183 | ∀m: nat. |
---|
184 | ∀q: Vector A m. |
---|
185 | bool_to_Prop (subvector_with A ? ? eq v (h @@ q @@ v)). |
---|
186 | # A # O # N # EQ # REFLEX # H # V |
---|
187 | elim V |
---|
188 | [ normalize |
---|
189 | # M # V % |
---|
190 | | # NN # AA # VV # IH # MM # QQ |
---|
191 | change with (bool_to_Prop (andb ??)) |
---|
192 | cut ((mem A EQ (O + (MM + S NN)) (H@@QQ@@AA:::VV) AA) = true) |
---|
193 | [ |
---|
194 | | # HH > HH |
---|
195 | > (vector_cons_append ? ? AA VV) |
---|
196 | change with (bool_to_Prop (subvector_with ??????)) |
---|
197 | @(super_rewrite2 A ((MM + 1)+ NN) (MM+S NN) ?? |
---|
198 | (λSS.λVS.bool_to_Prop (subvector_with ?? (O+SS) ?? (H@@VS))) |
---|
199 | ? |
---|
200 | (vector_associativity_of_append A ? ? ? QQ [[AA]] VV)) |
---|
201 | [ >associative_plus // |
---|
202 | | @IH ] |
---|
203 | ] |
---|
204 | @(mem_monotonic_wrt_append) |
---|
205 | [ @ REFLEX |
---|
206 | | @(mem_monotonic_wrt_append) |
---|
207 | [ @ REFLEX |
---|
208 | | normalize |
---|
209 | > REFLEX |
---|
210 | normalize |
---|
211 | % |
---|
212 | ] |
---|
213 | ] |
---|
214 | qed. |
---|
215 | (* |
---|
216 | lemma subvector_hd_tl: |
---|
217 | ∀A: Type[0]. |
---|
218 | ∀n: nat. |
---|
219 | ∀h: A. |
---|
220 | ∀eq: A → A → bool. |
---|
221 | ∀v: Vector A n. |
---|
222 | ∀m: nat. |
---|
223 | ∀q: Vector A m. |
---|
224 | bool_to_Prop (subvector_with A ? ? eq v (h ::: q @@ v)). |
---|
225 | # A |
---|
226 | # N |
---|
227 | # H |
---|
228 | # EQ |
---|
229 | # V |
---|
230 | elim V |
---|
231 | [ normalize |
---|
232 | # M |
---|
233 | # Q |
---|
234 | % |
---|
235 | | # NN |
---|
236 | # AA |
---|
237 | # VV |
---|
238 | # IH |
---|
239 | # MM |
---|
240 | # QQ |
---|
241 | whd |
---|
242 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ]) |
---|
243 | change in ⊢ (match (match ? with [_ ⇒ % | _ ⇒ ?]) with [_ ⇒ ? | _ ⇒ ?]) |
---|
244 | with (subvector_with ??????) |
---|
245 | change in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?]) with (andb ??) |
---|
246 | change with (bool_to_Prop ?); |
---|
247 | cut ((mem A EQ (S MM+S NN) (H:::QQ@@AA:::VV) AA) = true) |
---|
248 | [ |
---|
249 | | # H |
---|
250 | > H |
---|
251 | applyS (IH ? (QQ@@[[AA]])) |
---|
252 | ] |
---|
253 | generalize in match (IH ? (QQ@@[[AA]])) |
---|
254 | whd in ⊢ (% → ?) |
---|
255 | ] |
---|
256 | *) |
---|
257 | |
---|
258 | (* |
---|
259 | let rec list_addressing_mode_tags_elim |
---|
260 | (n: nat) (l: Vector addressing_mode_tag (S n)) on l: (l → bool) → bool ≝ |
---|
261 | match l return λx.match x with [O ⇒ λl: Vector … O. bool | S x' ⇒ λl: Vector addressing_mode_tag (S x'). |
---|
262 | (l → bool) → bool ] with |
---|
263 | [ VEmpty ⇒ true |
---|
264 | | VCons len hd tl ⇒ λP. |
---|
265 | let process_hd ≝ |
---|
266 | match hd return λhd. ∀P: hd:::tl → bool. bool with |
---|
267 | [ direct ⇒ λP.bitvector_elim 8 (λx. P (DIRECT x)) |
---|
268 | | indirect ⇒ λP.bit_elim (λx. P (INDIRECT x)) |
---|
269 | | ext_indirect ⇒ λP.bit_elim (λx. P (EXT_INDIRECT x)) |
---|
270 | | registr ⇒ λP.bitvector_elim 3 (λx. P (REGISTER x)) |
---|
271 | | acc_a ⇒ λP.P ACC_A |
---|
272 | | acc_b ⇒ λP.P ACC_B |
---|
273 | | dptr ⇒ λP.P DPTR |
---|
274 | | data ⇒ λP.bitvector_elim 8 (λx. P (DATA x)) |
---|
275 | | data16 ⇒ λP.bitvector_elim 16 (λx. P (DATA16 x)) |
---|
276 | | acc_dptr ⇒ λP.P ACC_DPTR |
---|
277 | | acc_pc ⇒ λP.P ACC_PC |
---|
278 | | ext_indirect_dptr ⇒ λP.P EXT_INDIRECT_DPTR |
---|
279 | | indirect_dptr ⇒ λP.P INDIRECT_DPTR |
---|
280 | | carry ⇒ λP.P CARRY |
---|
281 | | bit_addr ⇒ λP.bitvector_elim 8 (λx. P (BIT_ADDR x)) |
---|
282 | | n_bit_addr ⇒ λP.bitvector_elim 8 (λx. P (N_BIT_ADDR x)) |
---|
283 | | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE x)) |
---|
284 | | addr11 ⇒ λP.bitvector_elim 11 (λx. P (ADDR11 x)) |
---|
285 | | addr16 ⇒ λP.bitvector_elim 16 (λx. P (ADDR16 x)) |
---|
286 | ] |
---|
287 | in |
---|
288 | andb (process_hd P) |
---|
289 | (match len return λlen. Vector addressing_mode_tag len → bool with |
---|
290 | [ O ⇒ λ_.true |
---|
291 | | S y ⇒ λtl.list_addressing_mode_tags_elim y tl (λaddr.P addr) ] tl) |
---|
292 | ]. |
---|
293 | [1: @ (execute_1_technical ? ? tl) |
---|
294 | [ // |
---|
295 | | |
---|
296 | ] |
---|
297 | ]. |
---|
298 | |
---|
299 | definition preinstruction_elim: ∀P: preinstruction [[ relative ]] → bool. bool ≝ |
---|
300 | λP. |
---|
301 | P (ADD … ACC_A |
---|
302 | P (DA … ACC_A).lemma jmeq_to_eq: ∀A:Type[0]. ∀x,y:A. x≃y → x=y. |
---|
303 | #A #x #y #JMEQ @(jmeq_elim ? x … JMEQ) % |
---|
304 | qed. |
---|
305 | % |
---|
306 | qed. |
---|
307 | |
---|
308 | |
---|
309 | definition instruction_elim: ∀P: instruction → bool. bool. |
---|
310 | |
---|
311 | |
---|
312 | lemma instruction_elim_correct: |
---|
313 | ∀i: instruction. |
---|
314 | ∀P: instruction → bool. |
---|
315 | instruction_elim P = true → ∀j. P j = true. |
---|
316 | |
---|
317 | lemma test: |
---|
318 | ∀i: instruction. |
---|
319 | ∃pc. |
---|
320 | let assembled ≝ assembly1 i in |
---|
321 | let code_memory ≝ load_code_memory assembled in |
---|
322 | let fetched ≝ fetch code_memory pc in |
---|
323 | let 〈instr_pc, ticks〉 ≝ fetched in |
---|
324 | \fst instr_pc = i. |
---|
325 | # INSTR |
---|
326 | @ (ex_intro ?) |
---|
327 | [ @ (zero 16) |
---|
328 | | @ (instruction_elim INSTR) |
---|
329 | ]. |
---|
330 | *) |
---|
331 | |
---|
332 | (* This establishes the correspondence between pseudo program counters and |
---|
333 | program counters. It is at the heart of the proof. *) |
---|
334 | (*CSC: code taken from build_maps *) |
---|
335 | definition sigma0: pseudo_assembly_program → option (nat × (nat × (BitVectorTrie Word 16))) ≝ |
---|
336 | λinstr_list. |
---|
337 | foldl ?? |
---|
338 | (λt. λi. |
---|
339 | match t with |
---|
340 | [ None ⇒ None ? |
---|
341 | | Some ppc_pc_map ⇒ |
---|
342 | let 〈ppc,pc_map〉 ≝ ppc_pc_map in |
---|
343 | let 〈program_counter, sigma_map〉 ≝ pc_map in |
---|
344 | let 〈label, i〉 ≝ i in |
---|
345 | match construct_costs instr_list program_counter (λx. zero ?) (λx. zero ?) (Stub …) i with |
---|
346 | [ None ⇒ None ? |
---|
347 | | Some pc_ignore ⇒ |
---|
348 | let 〈pc,ignore〉 ≝ pc_ignore in |
---|
349 | Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ] |
---|
350 | ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (\snd instr_list). |
---|
351 | |
---|
352 | definition tech_pc_sigma0: pseudo_assembly_program → option nat ≝ |
---|
353 | λinstr_list. |
---|
354 | match sigma0 instr_list with |
---|
355 | [ None ⇒ None … |
---|
356 | | Some result ⇒ |
---|
357 | let 〈ppc,pc_sigma_map〉 ≝ result in |
---|
358 | let 〈pc, sigma_map〉 ≝ pc_sigma_map in |
---|
359 | Some … pc ]. |
---|
360 | |
---|
361 | definition sigma_safe: pseudo_assembly_program → option (Word → Word) ≝ |
---|
362 | λinstr_list. |
---|
363 | match sigma0 instr_list with |
---|
364 | [ None ⇒ None ? |
---|
365 | | Some result ⇒ |
---|
366 | let 〈ppc,pc_sigma_map〉 ≝ result in |
---|
367 | let 〈pc, sigma_map〉 ≝ pc_sigma_map in |
---|
368 | if gtb pc (2^16) then |
---|
369 | None ? |
---|
370 | else |
---|
371 | Some ? (λx.lookup ?? x sigma_map (zero …)) ]. |
---|
372 | |
---|
373 | axiom policy_ok: ∀p. sigma_safe p ≠ None …. |
---|
374 | |
---|
375 | definition sigma: pseudo_assembly_program → Word → Word ≝ |
---|
376 | λp. |
---|
377 | match sigma_safe p return λr:option (Word → Word). r ≠ None … → Word → Word with |
---|
378 | [ None ⇒ λabs. ⊥ |
---|
379 | | Some r ⇒ λ_.r] (policy_ok p). |
---|
380 | cases abs // |
---|
381 | qed. |
---|
382 | |
---|
383 | lemma length_append: |
---|
384 | ∀A.∀l1,l2:list A. |
---|
385 | |l1 @ l2| = |l1| + |l2|. |
---|
386 | #A #l1 elim l1 |
---|
387 | [ // |
---|
388 | | #hd #tl #IH #l2 normalize <IH //] |
---|
389 | qed. |
---|
390 | |
---|
391 | let rec does_not_occur (id:Identifier) (l:list labelled_instruction) on l: bool ≝ |
---|
392 | match l with |
---|
393 | [ nil ⇒ true |
---|
394 | | cons hd tl ⇒ notb (address_of_word_labels_internal id hd) ∧ does_not_occur id tl]. |
---|
395 | |
---|
396 | lemma does_not_occur_None: |
---|
397 | ∀id,i,list_instr. |
---|
398 | does_not_occur id (list_instr@[〈None …,i〉]) = |
---|
399 | does_not_occur id list_instr. |
---|
400 | #id #i #list_instr elim list_instr |
---|
401 | [ % | #hd #tl #IH whd in ⊢ (??%%) >IH %] |
---|
402 | qed. |
---|
403 | |
---|
404 | let rec occurs_exactly_once (id:Identifier) (l:list labelled_instruction) on l : bool ≝ |
---|
405 | match l with |
---|
406 | [ nil ⇒ false |
---|
407 | | cons hd tl ⇒ |
---|
408 | if address_of_word_labels_internal id hd then |
---|
409 | does_not_occur id tl |
---|
410 | else |
---|
411 | occurs_exactly_once id tl ]. |
---|
412 | |
---|
413 | lemma occurs_exactly_once_None: |
---|
414 | ∀id,i,list_instr. |
---|
415 | occurs_exactly_once id (list_instr@[〈None …,i〉]) = |
---|
416 | occurs_exactly_once id list_instr. |
---|
417 | #id #i #list_instr elim list_instr |
---|
418 | [ % | #hd #tl #IH whd in ⊢ (??%%) >IH >does_not_occur_None %] |
---|
419 | qed. |
---|
420 | |
---|
421 | coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0]. |
---|
422 | |
---|
423 | lemma index_of_internal_None: ∀i,id,instr_list,n. |
---|
424 | occurs_exactly_once id (instr_list@[〈None …,i〉]) → |
---|
425 | index_of_internal ? (address_of_word_labels_internal id) instr_list n = |
---|
426 | index_of_internal ? (address_of_word_labels_internal id) (instr_list@[〈None …,i〉]) n. |
---|
427 | #i #id #instr_list elim instr_list |
---|
428 | [ #n #abs whd in abs; cases abs |
---|
429 | | #hd #tl #IH #n whd in ⊢ (% → ??%%); whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ?) |
---|
430 | cases (address_of_word_labels_internal id hd) whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ??%%) |
---|
431 | [ #H % |
---|
432 | | #H @IH whd in H; cases (occurs_exactly_once ??) in H ⊢ % |
---|
433 | [ #_ % | #abs cases abs ]]] |
---|
434 | qed. |
---|
435 | |
---|
436 | lemma address_of_word_labels_code_mem_None: ∀i,id,instr_list. |
---|
437 | occurs_exactly_once id (instr_list@[〈None …,i〉]) → |
---|
438 | address_of_word_labels_code_mem instr_list id = |
---|
439 | address_of_word_labels_code_mem (instr_list@[〈None …,i〉]) id. |
---|
440 | #i #id #instr_list #H whd in ⊢ (??%%) whd in ⊢ (??(??%?)(??%?)) |
---|
441 | >(index_of_internal_None … H) % |
---|
442 | qed. |
---|
443 | |
---|
444 | definition build_maps' ≝ |
---|
445 | λpseudo_program. |
---|
446 | let 〈preamble,instr_list〉 ≝ pseudo_program in |
---|
447 | let result ≝ |
---|
448 | foldl_strong |
---|
449 | (option Identifier × pseudo_instruction) |
---|
450 | (λpre. Σres:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))). |
---|
451 | let pre' ≝ 〈preamble,pre〉 in |
---|
452 | let 〈labels,pc_costs〉 ≝ res in |
---|
453 | let 〈ignore,costs〉 ≝ pc_costs in |
---|
454 | ∀id. occurs_exactly_once id pre → |
---|
455 | lookup ?? id labels (zero …) = sigma pre' (address_of_word_labels_code_mem pre id)) |
---|
456 | instr_list |
---|
457 | (λprefix,i,tl,prf,t. |
---|
458 | let 〈labels, pc_costs〉 ≝ t in |
---|
459 | let 〈program_counter, costs〉 ≝ pc_costs in |
---|
460 | let 〈label, i'〉 ≝ i in |
---|
461 | let labels ≝ |
---|
462 | match label with |
---|
463 | [ None ⇒ labels |
---|
464 | | Some label ⇒ |
---|
465 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
466 | insert ? ? label program_counter_bv labels |
---|
467 | ] |
---|
468 | in |
---|
469 | match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with |
---|
470 | [ None ⇒ |
---|
471 | let dummy ≝ 〈labels,pc_costs〉 in |
---|
472 | dummy |
---|
473 | | Some construct ⇒ 〈labels, construct〉 |
---|
474 | ] |
---|
475 | ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 |
---|
476 | in |
---|
477 | let 〈labels, pc_costs〉 ≝ result in |
---|
478 | let 〈pc, costs〉 ≝ pc_costs in |
---|
479 | 〈labels, costs〉. |
---|
480 | [ whd cases construct in p3 #PC #CODE #JMEQ whd #id #Hid |
---|
481 | generalize in match (sig2 … t) whd in ⊢ (% → ?) |
---|
482 | >p whd in ⊢ (% → ?) >p1 whd in ⊢ (% → ?) #IH1 |
---|
483 | whd in ⊢ (??(????%?)?) -labels1; |
---|
484 | cases label in Hid |
---|
485 | [ #Hid whd in ⊢ (??(????%?)?) >IH1 -IH1 |
---|
486 | [ >(address_of_word_labels_code_mem_None … Hid) |
---|
487 | (* MANCA LEMMA: INDIRIZZO TROVATO NEL PROGRAMMA! *) |
---|
488 | | whd in Hid >occurs_exactly_once_None in Hid // ] |
---|
489 | | -label #label #Hid whd in ⊢ (??(????%?)?) |
---|
490 | |
---|
491 | ] |
---|
492 | | (* dummy case *) |
---|
493 | | whd #pc normalize in ⊢ (% → ?) #abs @⊥ // ] |
---|
494 | qed. |
---|
495 | |
---|
496 | (* |
---|
497 | (* |
---|
498 | notation < "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)" |
---|
499 | with precedence 10 |
---|
500 | for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }. |
---|
501 | *) |
---|
502 | |
---|
503 | lemma build_maps_ok: |
---|
504 | ∀p:pseudo_assembly_program. |
---|
505 | let 〈labels,costs〉 ≝ build_maps' p in |
---|
506 | ∀pc. |
---|
507 | (nat_of_bitvector … pc) < length … (\snd p) → |
---|
508 | lookup ?? pc labels (zero …) = sigma p (\snd (fetch_pseudo_instruction (\snd p) pc)). |
---|
509 | #p cases p #preamble #instr_list |
---|
510 | elim instr_list |
---|
511 | [ whd #pc #abs normalize in abs; cases (not_le_Sn_O ?) [#H cases (H abs) ] |
---|
512 | | #hd #tl #IH |
---|
513 | whd in ⊢ (match % with [ _ ⇒ ?]) |
---|
514 | ] |
---|
515 | qed. |
---|
516 | *) |
---|
517 | |
---|
518 | (* |
---|
519 | lemma list_elim_rev: |
---|
520 | ∀A:Type[0].∀P:list A → Prop. |
---|
521 | P [ ] → (∀n,l. length l = n → P l → |
---|
522 | P [ ] → (∀l,a. P l → P (l@[a])) → |
---|
523 | ∀l. P l. |
---|
524 | #A #P |
---|
525 | qed.*) |
---|
526 | |
---|
527 | lemma rev_preserves_length: |
---|
528 | ∀A.∀l. length … (rev A l) = length … l. |
---|
529 | #A #l elim l |
---|
530 | [ % |
---|
531 | | #hd #tl #IH normalize >length_append normalize /2/ ] |
---|
532 | qed. |
---|
533 | |
---|
534 | lemma rev_append: |
---|
535 | ∀A.∀l1,l2. |
---|
536 | rev A (l1@l2) = rev A l2 @ rev A l1. |
---|
537 | #A #l1 elim l1 normalize // |
---|
538 | qed. |
---|
539 | |
---|
540 | lemma rev_rev: ∀A.∀l. rev … (rev A l) = l. |
---|
541 | #A #l elim l |
---|
542 | [ // |
---|
543 | | #hd #tl #IH normalize >rev_append normalize // ] |
---|
544 | qed. |
---|
545 | |
---|
546 | lemma split_len_Sn: |
---|
547 | ∀A:Type[0].∀l:list A.∀len. |
---|
548 | length … l = S len → |
---|
549 | Σl'.Σa. l = l'@[a] ∧ length … l' = len. |
---|
550 | #A #l elim l |
---|
551 | [ normalize #len #abs destruct |
---|
552 | | #hd #tl #IH #len |
---|
553 | generalize in match (rev_rev … tl) |
---|
554 | cases (rev A tl) in ⊢ (??%? → ?) |
---|
555 | [ #H <H normalize #EQ % [@[ ]] % [@hd] normalize /2/ |
---|
556 | | #a #l' #H <H normalize #EQ |
---|
557 | %[@(hd::rev … l')] %[@a] % // |
---|
558 | >length_append in EQ #EQ normalize in EQ; normalize; |
---|
559 | generalize in match (injective_S … EQ) #EQ2 /2/ ]] |
---|
560 | qed. |
---|
561 | |
---|
562 | lemma list_elim_rev: |
---|
563 | ∀A:Type[0].∀P:list A → Type[0]. |
---|
564 | P [ ] → (∀l,a. P l → P (l@[a])) → |
---|
565 | ∀l. P l. |
---|
566 | #A #P #H1 #H2 #l |
---|
567 | generalize in match (refl … (length … l)) |
---|
568 | generalize in ⊢ (???% → ?) #n generalize in match l |
---|
569 | elim n |
---|
570 | [ #L cases L [ // | #x #w #abs (normalize in abs) @⊥ // ] |
---|
571 | | #m #IH #L #EQ |
---|
572 | cases (split_len_Sn … EQ) #l' * #a * /3/ ] |
---|
573 | qed. |
---|
574 | |
---|
575 | axiom is_prefix: ∀A:Type[0]. list A → list A → Prop. |
---|
576 | axiom prefix_of_append: |
---|
577 | ∀A:Type[0].∀l,l1,l2:list A. |
---|
578 | is_prefix … l l1 → is_prefix … l (l1@l2). |
---|
579 | axiom prefix_reflexive: ∀A,l. is_prefix A l l. |
---|
580 | axiom nil_prefix: ∀A,l. is_prefix A [ ] l. |
---|
581 | |
---|
582 | record Propify (A:Type[0]) : Type[0] (*Prop*) ≝ { in_propify: A }. |
---|
583 | |
---|
584 | definition Propify_elim: ∀A. ∀P:Prop. (A → P) → (Propify A → P) ≝ |
---|
585 | λA,P,H,x. match x with [ mk_Propify p ⇒ H p ]. |
---|
586 | |
---|
587 | definition app ≝ |
---|
588 | λA:Type[0].λl1:Propify (list A).λl2:list A. |
---|
589 | match l1 with |
---|
590 | [ mk_Propify l1 ⇒ mk_Propify … (l1@l2) ]. |
---|
591 | |
---|
592 | lemma app_nil: ∀A,l1. app A l1 [ ] = l1. |
---|
593 | #A * /3/ |
---|
594 | qed. |
---|
595 | |
---|
596 | lemma app_assoc: ∀A,l1,l2,l3. app A (app A l1 l2) l3 = app A l1 (l2@l3). |
---|
597 | #A * #l1 normalize // |
---|
598 | qed. |
---|
599 | |
---|
600 | let rec foldli (A: Type[0]) (B: Propify (list A) → Type[0]) |
---|
601 | (f: ∀prefix. B prefix → ∀x.B (app … prefix [x])) |
---|
602 | (prefix: Propify (list A)) (b: B prefix) (l: list A) on l : |
---|
603 | B (app … prefix l) ≝ |
---|
604 | match l with |
---|
605 | [ nil ⇒ ? (* b *) |
---|
606 | | cons hd tl ⇒ ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*) |
---|
607 | ]. |
---|
608 | [ applyS b |
---|
609 | | <(app_assoc ?? [hd]) @(foldli A B f (app … prefix [hd]) (f prefix b hd) tl) ] |
---|
610 | qed. |
---|
611 | |
---|
612 | (* |
---|
613 | let rec foldli (A: Type[0]) (B: list A → Type[0]) (f: ∀prefix. B prefix → ∀x. B (prefix@[x])) |
---|
614 | (prefix: list A) (b: B prefix) (l: list A) on l : B (prefix@l) ≝ |
---|
615 | match l with |
---|
616 | [ nil ⇒ ? (* b *) |
---|
617 | | cons hd tl ⇒ |
---|
618 | ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*) |
---|
619 | ]. |
---|
620 | [ applyS b |
---|
621 | | applyS (foldli A B f (prefix@[hd]) (f prefix b hd) tl) ] |
---|
622 | qed. |
---|
623 | *) |
---|
624 | |
---|
625 | definition foldll: |
---|
626 | ∀A:Type[0].∀B: Propify (list A) → Type[0]. |
---|
627 | (∀prefix. B prefix → ∀x. B (app … prefix [x])) → |
---|
628 | B (mk_Propify … []) → ∀l: list A. B (mk_Propify … l) |
---|
629 | ≝ λA,B,f. foldli A B f (mk_Propify … [ ]). |
---|
630 | |
---|
631 | axiom is_pprefix: ∀A:Type[0]. Propify (list A) → list A → Prop. |
---|
632 | axiom pprefix_of_append: |
---|
633 | ∀A:Type[0].∀l,l1,l2. |
---|
634 | is_pprefix A l l1 → is_pprefix A l (l1@l2). |
---|
635 | axiom pprefix_reflexive: ∀A,l. is_pprefix A (mk_Propify … l) l. |
---|
636 | axiom nil_pprefix: ∀A,l. is_pprefix A (mk_Propify … [ ]) l. |
---|
637 | |
---|
638 | |
---|
639 | axiom foldll': |
---|
640 | ∀A:Type[0].∀l: list A. |
---|
641 | ∀B: ∀prefix:Propify (list A). is_pprefix ? prefix l → Type[0]. |
---|
642 | (∀prefix,proof. B prefix proof → ∀x,proof'. B (app … prefix [x]) proof') → |
---|
643 | B (mk_Propify … [ ]) (nil_pprefix …) → B (mk_Propify … l) (pprefix_reflexive … l). |
---|
644 | #A #l #B |
---|
645 | generalize in match (foldll A (λprefix. is_pprefix ? prefix l)) #HH |
---|
646 | |
---|
647 | |
---|
648 | #H #acc |
---|
649 | @foldll |
---|
650 | [ |
---|
651 | | |
---|
652 | ] |
---|
653 | |
---|
654 | ≝ λA,B,f. foldli A B f (mk_Propify … [ ]). |
---|
655 | |
---|
656 | |
---|
657 | (* |
---|
658 | record subset (A:Type[0]) (P: A → Prop): Type[0] ≝ |
---|
659 | { subset_wit:> A; |
---|
660 | subset_proof: P subset_wit |
---|
661 | }. |
---|
662 | *) |
---|
663 | |
---|
664 | definition build_maps' ≝ |
---|
665 | λpseudo_program. |
---|
666 | let 〈preamble,instr_list〉 ≝ pseudo_program in |
---|
667 | let result ≝ |
---|
668 | foldll |
---|
669 | (option Identifier × pseudo_instruction) |
---|
670 | (λprefix. |
---|
671 | Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))). |
---|
672 | match prefix return λ_.Prop with [mk_Propify prefix ⇒ tech_pc_sigma0 〈preamble,prefix〉 ≠ None ?]) |
---|
673 | (λprefix,t,i. |
---|
674 | let 〈labels, pc_costs〉 ≝ t in |
---|
675 | let 〈program_counter, costs〉 ≝ pc_costs in |
---|
676 | let 〈label, i'〉 ≝ i in |
---|
677 | let labels ≝ |
---|
678 | match label with |
---|
679 | [ None ⇒ labels |
---|
680 | | Some label ⇒ |
---|
681 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
682 | insert ? ? label program_counter_bv labels |
---|
683 | ] |
---|
684 | in |
---|
685 | match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with |
---|
686 | [ None ⇒ |
---|
687 | let dummy ≝ 〈labels,pc_costs〉 in |
---|
688 | dummy |
---|
689 | | Some construct ⇒ 〈labels, construct〉 |
---|
690 | ] |
---|
691 | ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 instr_list |
---|
692 | in |
---|
693 | let 〈labels, pc_costs〉 ≝ result in |
---|
694 | let 〈pc, costs〉 ≝ pc_costs in |
---|
695 | 〈labels, costs〉. |
---|
696 | [ |
---|
697 | | @⊥ |
---|
698 | | normalize % // |
---|
699 | ] |
---|
700 | qed. |
---|
701 | |
---|
702 | definition build_maps' ≝ |
---|
703 | λpseudo_program. |
---|
704 | let 〈preamble,instr_list〉 ≝ pseudo_program in |
---|
705 | let result ≝ |
---|
706 | foldl |
---|
707 | (Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))). |
---|
708 | ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧ |
---|
709 | tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t))) |
---|
710 | (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix. |
---|
711 | let instr_list_prefix' ≝ instr_list_prefix @ [i] in |
---|
712 | is_prefix ? instr_list_prefix' instr_list → |
---|
713 | tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?) |
---|
714 | (λt: Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))). |
---|
715 | ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧ |
---|
716 | tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)). |
---|
717 | λi: Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix. |
---|
718 | let instr_list_prefix' ≝ instr_list_prefix @ [i] in |
---|
719 | is_prefix ? instr_list_prefix' instr_list → |
---|
720 | tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ? . |
---|
721 | let 〈labels, pc_costs〉 ≝ t in |
---|
722 | let 〈program_counter, costs〉 ≝ pc_costs in |
---|
723 | let 〈label, i'〉 ≝ i in |
---|
724 | let labels ≝ |
---|
725 | match label with |
---|
726 | [ None ⇒ labels |
---|
727 | | Some label ⇒ |
---|
728 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
729 | insert ? ? label program_counter_bv labels |
---|
730 | ] |
---|
731 | in |
---|
732 | match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with |
---|
733 | [ None ⇒ |
---|
734 | let dummy ≝ 〈labels,pc_costs〉 in |
---|
735 | dummy |
---|
736 | | Some construct ⇒ 〈labels, construct〉 |
---|
737 | ] |
---|
738 | ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 ?(*instr_list*) |
---|
739 | in |
---|
740 | let 〈labels, pc_costs〉 ≝ result in |
---|
741 | let 〈pc, costs〉 ≝ pc_costs in |
---|
742 | 〈labels, costs〉. |
---|
743 | [4: @(list_elim_rev ? |
---|
744 | (λinstr_list. list ( |
---|
745 | (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix. |
---|
746 | let instr_list_prefix' ≝ instr_list_prefix @ [i] in |
---|
747 | is_prefix ? instr_list_prefix' instr_list → |
---|
748 | tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?))) |
---|
749 | ?? instr_list) (* CSC: BAD ORDER FOR CODE EXTRACTION *) |
---|
750 | [ @[ ] |
---|
751 | | #l' #a #limage %2 |
---|
752 | [ %[@a] #PREFIX #PREFIX_OK |
---|
753 | | (* CSC: EVEN WORST CODE FOR EXTRACTION: WE SHOULD STRENGTHEN |
---|
754 | THE INDUCTION HYPOTHESIS INSTEAD *) |
---|
755 | elim limage |
---|
756 | [ %1 |
---|
757 | | #HD #TL #IH @(?::IH) cases HD #ELEM #K1 %[@ELEM] #K2 #K3 |
---|
758 | @K1 @(prefix_of_append ???? K3) |
---|
759 | ] |
---|
760 | ] |
---|
761 | |
---|
762 | |
---|
763 | |
---|
764 | |
---|
765 | cases t in c2 ⊢ % #t' * #LIST_PREFIX * #H1t' #H2t' #HJMt' |
---|
766 | % [@ (LIST_PREFIX @ [i])] % |
---|
767 | [ cases (sig2 … i LIST_PREFIX) #K1 #K2 @K1 |
---|
768 | | (* DOABLE IN PRINCIPLE *) |
---|
769 | ] |
---|
770 | | (* assert false case *) |
---|
771 | |3: % [@ ([ ])] % [2: % | (* DOABLE *)] |
---|
772 | | |
---|
773 | |
---|
774 | let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word) |
---|
775 | (encoding: list Byte) on encoding: Prop ≝ |
---|
776 | match encoding with |
---|
777 | [ nil ⇒ final_pc = pc |
---|
778 | | cons hd tl ⇒ |
---|
779 | let 〈new_pc, byte〉 ≝ next code_memory pc in |
---|
780 | hd = byte ∧ encoding_check code_memory new_pc final_pc tl |
---|
781 | ]. |
---|
782 | |
---|
783 | definition assembly_specification: |
---|
784 | ∀assembly_program: pseudo_assembly_program. |
---|
785 | ∀code_mem: BitVectorTrie Byte 16. Prop ≝ |
---|
786 | λpseudo_assembly_program. |
---|
787 | λcode_mem. |
---|
788 | ∀pc: Word. |
---|
789 | let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in |
---|
790 | let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in |
---|
791 | let labels ≝ λx. sigma' pseudo_assembly_program (address_of_word_labels_code_mem instr_list x) in |
---|
792 | let datalabels ≝ λx. sigma' pseudo_assembly_program (lookup ? ? x (construct_datalabels preamble) (zero ?)) in |
---|
793 | let pre_assembled ≝ assembly_1_pseudoinstruction pseudo_assembly_program |
---|
794 | (sigma' pseudo_assembly_program pc) labels datalabels pre_instr in |
---|
795 | match pre_assembled with |
---|
796 | [ None ⇒ True |
---|
797 | | Some pc_code ⇒ |
---|
798 | let 〈new_pc,code〉 ≝ pc_code in |
---|
799 | encoding_check code_mem pc (sigma' pseudo_assembly_program pre_new_pc) code ]. |
---|
800 | |
---|
801 | axiom assembly_meets_specification: |
---|
802 | ∀pseudo_assembly_program. |
---|
803 | match assembly pseudo_assembly_program with |
---|
804 | [ None ⇒ True |
---|
805 | | Some code_mem_cost ⇒ |
---|
806 | let 〈code_mem, cost〉 ≝ code_mem_cost in |
---|
807 | assembly_specification pseudo_assembly_program (load_code_memory code_mem) |
---|
808 | ]. |
---|
809 | (* |
---|
810 | # PROGRAM |
---|
811 | [ cases PROGRAM |
---|
812 | # PREAMBLE |
---|
813 | # INSTR_LIST |
---|
814 | elim INSTR_LIST |
---|
815 | [ whd |
---|
816 | whd in ⊢ (∀_. %) |
---|
817 | # PC |
---|
818 | whd |
---|
819 | | # INSTR |
---|
820 | # INSTR_LIST_TL |
---|
821 | # H |
---|
822 | whd |
---|
823 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?]) |
---|
824 | ] |
---|
825 | | cases not_implemented |
---|
826 | ] *) |
---|
827 | |
---|
828 | definition status_of_pseudo_status: PseudoStatus → option Status ≝ |
---|
829 | λps. |
---|
830 | let pap ≝ code_memory … ps in |
---|
831 | match assembly pap with |
---|
832 | [ None ⇒ None … |
---|
833 | | Some p ⇒ |
---|
834 | let cm ≝ load_code_memory (\fst p) in |
---|
835 | let pc ≝ sigma' pap (program_counter ? ps) in |
---|
836 | Some … |
---|
837 | (mk_PreStatus (BitVectorTrie Byte 16) |
---|
838 | cm |
---|
839 | (low_internal_ram … ps) |
---|
840 | (high_internal_ram … ps) |
---|
841 | (external_ram … ps) |
---|
842 | pc |
---|
843 | (special_function_registers_8051 … ps) |
---|
844 | (special_function_registers_8052 … ps) |
---|
845 | (p1_latch … ps) |
---|
846 | (p3_latch … ps) |
---|
847 | (clock … ps)) ]. |
---|
848 | |
---|
849 | definition write_at_stack_pointer': |
---|
850 | ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝ |
---|
851 | λM: Type[0]. |
---|
852 | λs: PreStatus M. |
---|
853 | λv: Byte. |
---|
854 | let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in |
---|
855 | let bit_zero ≝ get_index_v… nu O ? in |
---|
856 | let bit_1 ≝ get_index_v… nu 1 ? in |
---|
857 | let bit_2 ≝ get_index_v… nu 2 ? in |
---|
858 | let bit_3 ≝ get_index_v… nu 3 ? in |
---|
859 | if bit_zero then |
---|
860 | let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl) |
---|
861 | v (low_internal_ram ? s) in |
---|
862 | set_low_internal_ram ? s memory |
---|
863 | else |
---|
864 | let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl) |
---|
865 | v (high_internal_ram ? s) in |
---|
866 | set_high_internal_ram ? s memory. |
---|
867 | [ cases l0 % |
---|
868 | |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ] |
---|
869 | qed. |
---|
870 | |
---|
871 | definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus. |
---|
872 | Σps':PseudoStatus.(code_memory … ps = code_memory … ps') |
---|
873 | ≝ |
---|
874 | λticks_of. |
---|
875 | λs. |
---|
876 | let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in |
---|
877 | let ticks ≝ ticks_of (program_counter ? s) in |
---|
878 | let s ≝ set_clock ? s (clock ? s + ticks) in |
---|
879 | let s ≝ set_program_counter ? s pc in |
---|
880 | match instr with |
---|
881 | [ Instruction instr ⇒ |
---|
882 | execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s |
---|
883 | | Comment cmt ⇒ s |
---|
884 | | Cost cst ⇒ s |
---|
885 | | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp) |
---|
886 | | Call call ⇒ |
---|
887 | let a ≝ address_of_word_labels s call in |
---|
888 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in |
---|
889 | let s ≝ set_8051_sfr ? s SFR_SP new_sp in |
---|
890 | let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in |
---|
891 | let s ≝ write_at_stack_pointer' ? s pc_bl in |
---|
892 | let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in |
---|
893 | let s ≝ set_8051_sfr ? s SFR_SP new_sp in |
---|
894 | let s ≝ write_at_stack_pointer' ? s pc_bu in |
---|
895 | set_program_counter ? s a |
---|
896 | | Mov dptr ident ⇒ |
---|
897 | set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr |
---|
898 | ]. |
---|
899 | [ |
---|
900 | |2,3,4: % |
---|
901 | | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) % |
---|
902 | | |
---|
903 | | % |
---|
904 | ] |
---|
905 | cases not_implemented |
---|
906 | qed. |
---|
907 | |
---|
908 | (* |
---|
909 | lemma execute_code_memory_unchanged: |
---|
910 | ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps). |
---|
911 | #ticks #ps whd in ⊢ (??? (??%)) |
---|
912 | cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps)) |
---|
913 | (program_counter pseudo_assembly_program ps)) #instr #pc |
---|
914 | whd in ⊢ (??? (??%)) cases instr |
---|
915 | [ #pre cases pre |
---|
916 | [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%)) |
---|
917 | cases (split ????) #z1 #z2 % |
---|
918 | | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%)) |
---|
919 | cases (split ????) #z1 #z2 % |
---|
920 | | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%)) |
---|
921 | cases (split ????) #z1 #z2 % |
---|
922 | | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x |
---|
923 | [ #x1 whd in ⊢ (??? (??%)) |
---|
924 | | *: cases not_implemented |
---|
925 | ] |
---|
926 | | #comment % |
---|
927 | | #cost % |
---|
928 | | #label % |
---|
929 | | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%)) |
---|
930 | cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2 |
---|
931 | whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2 |
---|
932 | whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%)) |
---|
933 | (* CSC: ??? *) |
---|
934 | | #dptr #label (* CSC: ??? *) |
---|
935 | ] |
---|
936 | cases not_implemented |
---|
937 | qed. |
---|
938 | *) |
---|
939 | |
---|
940 | lemma status_of_pseudo_status_failure_depends_only_on_code_memory: |
---|
941 | ∀ps,ps': PseudoStatus. |
---|
942 | code_memory … ps = code_memory … ps' → |
---|
943 | match status_of_pseudo_status ps with |
---|
944 | [ None ⇒ status_of_pseudo_status ps' = None … |
---|
945 | | Some _ ⇒ ∃w. status_of_pseudo_status ps' = Some … w |
---|
946 | ]. |
---|
947 | #ps #ps' #H whd in ⊢ (mat |
---|
948 | ch % with [ _ ⇒ ? | _ ⇒ ? ]) |
---|
949 | generalize in match (refl … (assembly (code_memory … ps))) |
---|
950 | cases (assembly ?) in ⊢ (???% → %) |
---|
951 | [ #K whd whd in ⊢ (??%?) <H >K % |
---|
952 | | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ] |
---|
953 | qed.*) |
---|
954 | |
---|
955 | let rec encoding_check' (code_memory: BitVectorTrie Byte 16) (pc: Word) (encoding: list Byte) on encoding: Prop ≝ |
---|
956 | match encoding with |
---|
957 | [ nil ⇒ True |
---|
958 | | cons hd tl ⇒ |
---|
959 | let 〈new_pc, byte〉 ≝ next code_memory pc in |
---|
960 | hd = byte ∧ encoding_check' code_memory new_pc tl |
---|
961 | ]. |
---|
962 | |
---|
963 | (* prove later *) |
---|
964 | axiom test: |
---|
965 | ∀pc: Word. |
---|
966 | ∀code_memory: BitVectorTrie Byte 16. |
---|
967 | ∀i: instruction. |
---|
968 | let assembled ≝ assembly1 i in |
---|
969 | encoding_check' code_memory pc assembled → |
---|
970 | let 〈instr_pc, ignore〉 ≝ fetch code_memory pc in |
---|
971 | let 〈instr, pc〉 ≝ instr_pc in |
---|
972 | instr = i. |
---|
973 | |
---|
974 | lemma main_thm: |
---|
975 | ∀ticks_of. |
---|
976 | ∀ps: PseudoStatus. |
---|
977 | match status_of_pseudo_status ps with [ None ⇒ True | Some s ⇒ |
---|
978 | let ps' ≝ execute_1_pseudo_instruction ticks_of ps in |
---|
979 | match status_of_pseudo_status ps' with [ None ⇒ True | Some s'' ⇒ |
---|
980 | let s' ≝ execute_1 s in |
---|
981 | s = s'']]. |
---|
982 | #ticks_of #ps |
---|
983 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ]) |
---|
984 | cases (assembly (code_memory pseudo_assembly_program ps)) [%] * #cm #costs whd |
---|
985 | whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ]) |
---|
986 | generalize in match (sig2 … (execute_1_pseudo_instruction' ticks_of ps)) |
---|
987 | |
---|
988 | cases (status_of_pseudo_status (execute_1_pseudo_instruction ticks_of ps)) [%] #s'' whd |
---|