source: src/ASM/AssemblyProof.ma @ 871

Last change on this file since 871 was 871, checked in by sacerdot, 9 years ago

...

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