# source:src/ASM/AssemblyProof.ma@875

Last change on this file since 875 was 875, checked in by sacerdot, 10 years ago

...

File size: 44.2 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.
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:
283  ∀n: nat.
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
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
323      | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE 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  | generalize in match H; generalize in match tl; cases prf;
336    (* cases prf in tl H; : ??? WAS WORKING BEFORE *)
337    #tl
338    normalize in ⊢ (∀_: %. ?)
339    # H
340    whd
341    normalize in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
342    cases (is_a hd (subaddressing_modeel y tl H)) whd // ]
343qed.
344
345definition product_elim ≝
346  λm, n: nat.
347  λv: Vector addressing_mode_tag (S m).
348  λq: Vector addressing_mode_tag (S n).
349  λP: (v × q) → bool.
350    list_addressing_mode_tags_elim ? v (λx. list_addressing_mode_tags_elim ? q (λy. P 〈x, y〉)).
351
352definition union_elim ≝
353  λA, B: Type[0].
354  λelimA: (A → bool) → bool.
355  λelimB: (B → bool) → bool.
356  λelimU: A ⊎ B → bool.
357    elimA (λa. elimB (λb. elimU (inl ? ? a) ∧ elimU (inr ? ? b))).
358
359definition preinstruction_elim: ∀P: preinstruction [[ relative ]] → bool. bool ≝
360  λP.
363    list_addressing_mode_tags_elim ? [[ registr ; direct ; indirect ; data ]] (λaddr. P (SUBB ? ACC_A addr)) ∧
364    list_addressing_mode_tags_elim ? [[ acc_a ; registr ; direct ; indirect ; dptr ]] (λaddr. P (INC ? addr)) ∧
365    list_addressing_mode_tags_elim ? [[ acc_a ; registr ; direct ; indirect ]] (λaddr. P (DEC ? addr)) ∧
368    list_addressing_mode_tags_elim ? [[ registr ; direct ]] (λaddr. bitvector_elim 8 (λr. P (DJNZ ? addr (RELATIVE r)))) ∧
371    P (DA ? ACC_A) ∧
372    bitvector_elim 8 (λr. P (JC ? (RELATIVE r))) ∧
373    bitvector_elim 8 (λr. P (JNC ? (RELATIVE r))) ∧
374    bitvector_elim 8 (λr. P (JZ ? (RELATIVE r))) ∧
375    bitvector_elim 8 (λr. P (JNZ ? (RELATIVE r))) ∧
376    bitvector_elim 8 (λr. (bitvector_elim 8 (λb: BitVector 8. P (JB ? (BIT_ADDR b) (RELATIVE r))))) ∧
377    bitvector_elim 8 (λr. (bitvector_elim 8 (λb: BitVector 8. P (JNB ? (BIT_ADDR b) (RELATIVE r))))) ∧
378    bitvector_elim 8 (λr. (bitvector_elim 8 (λb: BitVector 8. P (JBC ? (BIT_ADDR b) (RELATIVE r))))) ∧
379    list_addressing_mode_tags_elim ? [[ registr; direct ]] (λaddr. bitvector_elim 8 (λr. P (DJNZ ? addr (RELATIVE r)))) ∧
380    P (RL ? ACC_A) ∧
381    P (RLC ? ACC_A) ∧
382    P (RR ? ACC_A) ∧
383    P (RRC ? ACC_A) ∧
384    P (SWAP ? ACC_A) ∧
385    P (RET ?) ∧
386    P (RETI ?) ∧
387    P (NOP ?) ∧
388    bit_elim (λb. P (XCHD ? ACC_A (INDIRECT b))) ∧
392    union_elim ? ? (product_elim ? ? [[ acc_a ]] [[ direct; data ]])
393                   (product_elim ? ? [[ registr; indirect ]] [[ data ]])
394                   (λd. bitvector_elim 8 (λb. P (CJNE ? d (RELATIVE b)))) ∧
396    union_elim ? ? (product_elim ? ? [[acc_a]] [[ data ; registr ; direct ; indirect ]])
397                   (product_elim ? ? [[direct]] [[ acc_a ; data ]])
398                   (λd. P (XRL ? d)) ∧
399    union_elim ? ? (union_elim ? ? (product_elim ? ? [[acc_a]] [[ registr ; direct ; indirect ; data ]])
400                                   (product_elim ? ? [[direct]] [[ acc_a ; data ]]))
402                   (λd. P (ANL ? d)) ∧
403    union_elim ? ? (union_elim ? ? (product_elim ? ? [[acc_a]] [[ registr ; data ; direct ; indirect ]])
404                                   (product_elim ? ? [[direct]] [[ acc_a ; data ]]))
406                   (λd. P (ORL ? d)) ∧
407    union_elim ? ? (product_elim ? ? [[acc_a]] [[ ext_indirect ; ext_indirect_dptr ]])
408                   (product_elim ? ? [[ ext_indirect ; ext_indirect_dptr ]] [[acc_a]])
409                   (λd. P (MOVX ? d)) ∧
410    union_elim ? ? (
411      union_elim ? ? (
412        union_elim ? ? (
413          union_elim ? ? (
414            union_elim ? ?  (product_elim ? ? [[acc_a]] [[ registr ; direct ; indirect ; data ]])
415                            (product_elim ? ? [[ registr ; indirect ]] [[ acc_a ; direct ; data ]]))
416                            (product_elim ? ? [[direct]] [[ acc_a ; registr ; direct ; indirect ; data ]]))
417                            (product_elim ? ? [[dptr]] [[data16]]))
418                            (product_elim ? ? [[carry]] [[bit_addr]]))
419                            (product_elim ? ? [[bit_addr]] [[carry]])
420                            (λd. P (MOV ? d)).
421  %
422qed.
423
424definition instruction_elim: ∀P: instruction → bool. bool ≝
425  λP. (*
426    bitvector_elim 11 (λx. P (ACALL (ADDR11 x))) ∧
427    bitvector_elim 16 (λx. P (LCALL (ADDR16 x))) ∧
428    bitvector_elim 11 (λx. P (AJMP (ADDR11 x))) ∧
429    bitvector_elim 16 (λx. P (LJMP (ADDR16 x))) ∧ *)
430    bitvector_elim 8 (λx. P (SJMP (RELATIVE x))). (*  ∧
431    P (JMP INDIRECT_DPTR) ∧
432    list_addressing_mode_tags_elim ? [[ acc_dptr; acc_pc ]] (λa. P (MOVC ACC_A a)) ∧
433    preinstruction_elim (λp. P (RealInstruction p)). *)
434  %
435qed.
436
437
438axiom instruction_elim_complete:
439 ∀P. instruction_elim P = true → ∀i. P i = true.
440
441definition eq_instruction ≝
442  λi, j: instruction.
443    true.
444
446  (n: nat) (l: Vector addressing_mode_tag (S n))
447  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
448  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
449  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
450  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
451  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
452  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
453  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
454  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
455  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
456  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
457  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
458  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
459  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
460  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
461  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
462  (if is_in ? l direct then ∀x. P (DIRECT x) else True) →
463  (if is_in ? l relative then ∀x. P (RELATIVE x) else True) →
464  (if is_in ? l addr11 then ∀x. P (ADDR11 x) else True) →
465  (if is_in ? l addr16 then ∀x. P (ADDR16 x) else True) →
466  (P: addressing_mode → Prop) (x: l)
467   on l : P x ≝
468  match l return λx.match x with [O ⇒ λl: Vector … O. bool | S x' ⇒ λl: Vector addressing_mode_tag (S x').
469   (l → bool) → bool ] with
470  [ VEmpty      ⇒  true
471  | VCons len hd tl ⇒ λP.
472    let process_hd ≝
473      match hd return λhd. ∀P: hd:::tl → bool. bool with
474      [ direct ⇒ λP.bitvector_elim 8 (λx. P (DIRECT x))
475      | indirect ⇒ λP.bit_elim (λx. P (INDIRECT x))
476      | ext_indirect ⇒ λP.bit_elim (λx. P (EXT_INDIRECT x))
477      | registr ⇒ λP.bitvector_elim 3 (λx. P (REGISTER x))
478      | acc_a ⇒ λP.P ACC_A
479      | acc_b ⇒ λP.P ACC_B
480      | dptr ⇒ λP.P DPTR
481      | data ⇒ λP.bitvector_elim 8 (λx. P (DATA x))
482      | data16 ⇒ λP.bitvector_elim 16 (λx. P (DATA16 x))
483      | acc_dptr ⇒ λP.P ACC_DPTR
484      | acc_pc ⇒ λP.P ACC_PC
485      | ext_indirect_dptr ⇒ λP.P EXT_INDIRECT_DPTR
486      | indirect_dptr ⇒ λP.P INDIRECT_DPTR
487      | carry ⇒ λP.P CARRY
490      | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE x))
493      ]
494    in
495      andb (process_hd P)
496       (match len return λx. x = len → bool with
497         [ O ⇒ λprf. true
498         | S y ⇒ λprf. list_addressing_mode_tags_elim y ? P ] (refl ? len))
499  ].
500  try %
501  [ 2: cases (sym_eq ??? prf); @tl
502  | generalize in match H; generalize in match tl; cases prf;
503    (* cases prf in tl H; : ??? WAS WORKING BEFORE *)
504    #tl
505    normalize in ⊢ (∀_: %. ?)
506    # H
507    whd
508    normalize in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
509    cases (is_a hd (subaddressing_modeel y tl H)) whd // ]
510qed.
511
512(*
513lemma test:
514  let i ≝ SJMP (RELATIVE (bitvector_of_nat 8 255)) in
515      (let assembled ≝ assembly1 i in
516      let code_memory ≝ load_code_memory assembled in
517      let fetched ≝ fetch code_memory ? in
518      let 〈instr_pc, ticks〉 ≝ fetched in
519        eq_instruction (\fst instr_pc)) i = true.
520 [2: @ zero
521 | normalize
522 ]*)
523
524lemma test:
525  ∀i.
526      (let assembled ≝ assembly1 i in
527      let code_memory ≝ load_code_memory assembled in
528      let fetched ≝ fetch code_memory ? in
529      let 〈instr_pc, ticks〉 ≝ fetched in
530        eq_instruction (\fst instr_pc)) i = true.
531 [ #i cases i #arg try #arg2 whd in ⊢ (??%?)
532   [2: whd in ⊢ (??(match ? (? %) ?with [ _ ⇒ ?] ?)?)
533       cases arg #sam cases sam #XX try #PP normalize in PP; try cases PP;
534       whd in ⊢ (??(match ? (? %) ? with [ _ ⇒ ?] ?)?)
535
536   [2: #addr whd in ⊢ (??%?)
537
538   @ (instruction_elim_complete )
539 | @ zero
540 ]
541 normalize
542
543
544(* This establishes the correspondence between pseudo program counters and
545   program counters. It is at the heart of the proof. *)
546(*CSC: code taken from build_maps *)
547definition sigma0: pseudo_assembly_program → option (nat × (nat × (BitVectorTrie Word 16))) ≝
548 λinstr_list.
549  foldl ??
550    (λt. λi.
551       match t with
552       [ None ⇒ None ?
553       | Some ppc_pc_map ⇒
554         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
555         let 〈program_counter, sigma_map〉 ≝ pc_map in
556         let 〈label, i〉 ≝ i in
557          match construct_costs instr_list program_counter (λx. zero ?) (λx. zero ?) (Stub …) i with
558           [ None ⇒ None ?
559           | Some pc_ignore ⇒
560              let 〈pc,ignore〉 ≝ pc_ignore in
561              Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ]
562       ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (\snd instr_list).
563
564definition tech_pc_sigma0: pseudo_assembly_program → option (nat × (BitVectorTrie Word 16)) ≝
565 λinstr_list.
566  match sigma0 instr_list with
567   [ None ⇒ None …
568   | Some result ⇒
569      let 〈ppc,pc_sigma_map〉 ≝ result in
570       Some … pc_sigma_map ].
571
572definition sigma_safe: pseudo_assembly_program → option (Word → Word) ≝
573 λinstr_list.
574  match sigma0 instr_list with
575  [ None ⇒ None ?
576  | Some result ⇒
577    let 〈ppc,pc_sigma_map〉 ≝ result in
578    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
579      if gtb pc (2^16) then
580        None ?
581      else
582        Some ? (λx.lookup ?? x sigma_map (zero …)) ].
583
584axiom policy_ok: ∀p. sigma_safe p ≠ None ….
585
586definition sigma: pseudo_assembly_program → Word → Word ≝
587 λp.
588  match sigma_safe p return λr:option (Word → Word). r ≠ None … → Word → Word with
589   [ None ⇒ λabs. ⊥
590   | Some r ⇒ λ_.r] (policy_ok p).
591 cases abs //
592qed.
593
594lemma length_append:
595 ∀A.∀l1,l2:list A.
596  |l1 @ l2| = |l1| + |l2|.
597 #A #l1 elim l1
598  [ //
599  | #hd #tl #IH #l2 normalize <IH //]
600qed.
601
602let rec does_not_occur (id:Identifier) (l:list labelled_instruction) on l: bool ≝
603 match l with
604  [ nil ⇒ true
605  | cons hd tl ⇒ notb (instruction_matches_identifier id hd) ∧ does_not_occur id tl].
606
607lemma does_not_occur_None:
608 ∀id,i,list_instr.
609  does_not_occur id (list_instr@[〈None …,i〉]) =
610  does_not_occur id list_instr.
611 #id #i #list_instr elim list_instr
612  [ % | #hd #tl #IH whd in ⊢ (??%%) >IH %]
613qed.
614
615let rec occurs_exactly_once (id:Identifier) (l:list labelled_instruction) on l : bool ≝
616 match l with
617  [ nil ⇒ false
618  | cons hd tl ⇒
619     if instruction_matches_identifier id hd then
620      does_not_occur id tl
621     else
622      occurs_exactly_once id tl ].
623
624lemma occurs_exactly_once_None:
625 ∀id,i,list_instr.
626  occurs_exactly_once id (list_instr@[〈None …,i〉]) =
627  occurs_exactly_once id list_instr.
628 #id #i #list_instr elim list_instr
629  [ % | #hd #tl #IH whd in ⊢ (??%%) >IH >does_not_occur_None %]
630qed.
631
632coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0].
633
634lemma index_of_internal_None: ∀i,id,instr_list,n.
635 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
636  index_of_internal ? (instruction_matches_identifier id) instr_list n =
637   index_of_internal ? (instruction_matches_identifier id) (instr_list@[〈None …,i〉]) n.
638 #i #id #instr_list elim instr_list
639  [ #n #abs whd in abs; cases abs
640  | #hd #tl #IH #n whd in ⊢ (% → ??%%); whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ?)
641    cases (instruction_matches_identifier id hd) whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ??%%)
642    [ #H %
643    | #H @IH whd in H; cases (occurs_exactly_once ??) in H ⊢ %
644      [ #_ % | #abs cases abs ]]]
645qed.
646
648 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
651 #i #id #instr_list #H whd in ⊢ (??%%) whd in ⊢ (??(??%?)(??%?))
652 >(index_of_internal_None … H) %
653qed.
654
655axiom tech_pc_sigma0_append:
656 ∀preamble,instr_list,prefix,label,i,pc',code,pc,costs,costs'.
657  Some … 〈pc,costs〉 = tech_pc_sigma0 〈preamble,prefix〉 →
658   construct_costs 〈preamble,instr_list〉 … pc (λx.zero 16) (λx. zero 16) costs i = Some … 〈pc',code〉 →
659    tech_pc_sigma0 〈preamble,prefix@[〈label,i〉]〉 = Some … 〈pc',costs'〉.
660
661axiom tech_pc_sigma0_append_None:
662 ∀preamble,instr_list,prefix,i,pc,costs.
663  Some … 〈pc,costs〉 = tech_pc_sigma0 〈preamble,prefix〉 →
664   construct_costs 〈preamble,instr_list〉 … pc (λx.zero 16) (λx. zero 16) costs i = None …
665    → False.
666
667lemma BitVectorTrie_O:
668 ∀A:Type[0].∀v:BitVectorTrie A 0.(∃w. v ≃ Leaf A w) ∨ v ≃ Stub A 0.
669 #A #v generalize in match (refl … O) cases v in ⊢ (??%? → (?(??(λ_.?%%??)))(?%%??))
670  [ #w #_ %1 %[@w] %
671  | #n #l #r #abs @⊥ //
672  | #n #EQ %2 >EQ %]
673qed.
674
675lemma BitVectorTrie_Sn:
676 ∀A:Type[0].∀n.∀v:BitVectorTrie A (S n).(∃l,r. v ≃ Node A n l r) ∨ v ≃ Stub A (S n).
677 #A #n #v generalize in match (refl … (S n)) cases v in ⊢ (??%? → (?(??(λ_.??(λ_.?%%??))))%)
678  [ #m #abs @⊥ //
679  | #m #l #r #EQ %1 <(injective_S … EQ) %[@l] %[@r] //
680  | #m #EQ %2 // ]
681qed.
682
683lemma lookup_prepare_trie_for_insertion_hit:
684 ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.
685  lookup … b (prepare_trie_for_insertion … b v) a = v.
686 #A #a #v #n #b elim b // #m #hd #tl #IH cases hd normalize //
687qed.
688
689lemma lookup_insert_hit:
690 ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.∀t:BitVectorTrie A n.
691  lookup … b (insert … b v t) a = v.
692 #A #a #v #n #b elim b -b -n //
693 #n #hd #tl #IH #t cases(BitVectorTrie_Sn … t)
694  [ * #l * #r #JMEQ >JMEQ cases hd normalize //
695  | #JMEQ >JMEQ cases hd normalize @lookup_prepare_trie_for_insertion_hit ]
696qed.
697
698lemma BitVector_O: ∀v:BitVector 0. v ≃ VEmpty bool.
699 #v generalize in match (refl … 0) cases v in ⊢ (??%? → ?%%??) //
700 #n #hd #tl #abs @⊥ //
701qed.
702
703lemma BitVector_Sn: ∀n.∀v:BitVector (S n).
704 ∃hd.∃tl.v ≃ VCons bool n hd tl.
705 #n #v generalize in match (refl … (S n)) cases v in ⊢ (??%? → ??(λ_.??(λ_.?%%??)))
706 [ #abs @⊥ //
707 | #m #hd #tl #EQ <(injective_S … EQ) %[@hd] %[@tl] // ]
708qed.
709
710lemma lookup_prepare_trie_for_insertion_miss:
711 ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.
712  (notb (eq_bv ? b c)) → lookup … b (prepare_trie_for_insertion … c v) a = a.
713 #A #a #v #n #c elim c
714  [ #b >(BitVector_O … b) normalize #abs @⊥ //
715  | #m #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ
716    cases hd cases hd' normalize
717    [2,3: #_ cases tl' //
718    |*: change with (bool_to_Prop (notb (eq_bv ???)) → ?) /2/ ]]
719qed.
720
721lemma lookup_insert_miss:
722 ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.∀t:BitVectorTrie A n.
723  (notb (eq_bv ? b c)) → lookup … b (insert … c v t) a = lookup … b t a.
724 #A #a #v #n #c elim c -c -n
725  [ #b #t #DIFF @⊥ whd in DIFF; >(BitVector_O … b) in DIFF //
726  | #n #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ
727    #t cases(BitVectorTrie_Sn … t)
728    [ * #l * #r #JMEQ >JMEQ cases hd cases hd' #H normalize in H;
729     [1,4: change in H with (bool_to_Prop (notb (eq_bv ???))) ] normalize // @IH //
730    | #JMEQ >JMEQ cases hd cases hd' #H normalize in H;
731     [1,4: change in H with (bool_to_Prop (notb (eq_bv ???))) ] normalize
732     [3,4: cases tl' // | *: @lookup_prepare_trie_for_insertion_miss //]]]
733qed.
734
735definition build_maps' ≝
736  λpseudo_program.
737  let 〈preamble,instr_list〉 ≝ pseudo_program in
738  let result ≝
739   foldl_strong
740    (option Identifier × pseudo_instruction)
741    (λpre. Σres:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
742      let pre' ≝ 〈preamble,pre〉 in
743      let 〈labels,pc_costs〉 ≝ res in
744       tech_pc_sigma0 pre' = Some … pc_costs ∧
745       ∀id. occurs_exactly_once id pre →
746        lookup ?? id labels (zero …) = sigma pre' (address_of_word_labels_code_mem pre id))
747    instr_list
748    (λprefix,i,tl,prf,t.
749      let 〈labels, pc_costs〉 ≝ t in
750      let 〈program_counter, costs〉 ≝ pc_costs in
751       let 〈label, i'〉 ≝ i in
752       let labels ≝
753         match label with
754         [ None ⇒ labels
755         | Some label ⇒
756           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
757             insert ? ? label program_counter_bv labels
758         ]
759       in
760         match construct_costs 〈preamble,instr_list〉 program_counter (λx. zero ?) (λx. zero ?) costs i' with
761         [ None ⇒
762            let dummy ≝ 〈labels,pc_costs〉 in
763             dummy
764         | Some construct ⇒ 〈labels, construct〉
765         ]
766    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉
767  in
768   let 〈labels, pc_costs〉 ≝ result in
769   let 〈pc, costs〉 ≝ pc_costs in
770    〈labels, costs〉.
771 [3: whd % // #id normalize in ⊢ (% → ?) #abs @⊥ //
772 | whd cases construct in p3 #PC #CODE #JMEQ %
773    [ @(tech_pc_sigma0_append ??????????? (jmeq_to_eq ??? JMEQ)) | #id #Hid ]
774 | (* dummy case *) @⊥
775   @(tech_pc_sigma0_append_None ?? prefix ???? (jmeq_to_eq ??? p3)) ]
776 [*: generalize in match (sig2 … t) whd in ⊢ (% → ?)
777     >p whd in ⊢ (% → ?) >p1 * #IH0 #IH1 >IH0 // ]
778 whd in ⊢ (??(????%?)?) -labels1;
779 cases label in Hid
780  [ #Hid whd in ⊢ (??(????%?)?) >IH1 -IH1
782       (* MANCA LEMMA: INDIRIZZO TROVATO NEL PROGRAMMA! *)
783     | whd in Hid >occurs_exactly_once_None in Hid // ]
784  | -label #label #Hid whd in ⊢ (??(????%?)?)
785
786  ]
787qed.
788
789(*
790(*
791notation < "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
792 with precedence 10
793for @{ match \$t with [ pair \${ident x} \${ident y} ⇒ \$s ] }.
794*)
795
796lemma build_maps_ok:
797 ∀p:pseudo_assembly_program.
798  let 〈labels,costs〉 ≝ build_maps' p in
799   ∀pc.
800    (nat_of_bitvector … pc) < length … (\snd p) →
801     lookup ?? pc labels (zero …) = sigma p (\snd (fetch_pseudo_instruction (\snd p) pc)).
802 #p cases p #preamble #instr_list
803  elim instr_list
804   [ whd #pc #abs normalize in abs; cases (not_le_Sn_O ?) [#H cases (H abs) ]
805   | #hd #tl #IH
806    whd in ⊢ (match % with [ _ ⇒ ?])
807   ]
808qed.
809*)
810
811(*
812lemma list_elim_rev:
813 ∀A:Type[0].∀P:list A → Prop.
814  P [ ] → (∀n,l. length l = n → P l →
815  P [ ] → (∀l,a. P l → P (l@[a])) →
816   ∀l. P l.
817 #A #P
818qed.*)
819
820lemma rev_preserves_length:
821 ∀A.∀l. length … (rev A l) = length … l.
822  #A #l elim l
823   [ %
824   | #hd #tl #IH normalize >length_append normalize /2/ ]
825qed.
826
827lemma rev_append:
828 ∀A.∀l1,l2.
829  rev A (l1@l2) = rev A l2 @ rev A l1.
830 #A #l1 elim l1 normalize //
831qed.
832
833lemma rev_rev: ∀A.∀l. rev … (rev A l) = l.
834 #A #l elim l
835  [ //
836  | #hd #tl #IH normalize >rev_append normalize // ]
837qed.
838
839lemma split_len_Sn:
840 ∀A:Type[0].∀l:list A.∀len.
841  length … l = S len →
842   Σl'.Σa. l = l'@[a] ∧ length … l' = len.
843 #A #l elim l
844  [ normalize #len #abs destruct
845  | #hd #tl #IH #len
846    generalize in match (rev_rev … tl)
847    cases (rev A tl) in ⊢ (??%? → ?)
848     [ #H <H normalize #EQ % [@[ ]] % [@hd] normalize /2/
849     | #a #l' #H <H normalize #EQ
850      %[@(hd::rev … l')] %[@a] % //
851      >length_append in EQ #EQ normalize in EQ; normalize;
852      generalize in match (injective_S … EQ) #EQ2 /2/ ]]
853qed.
854
855lemma list_elim_rev:
856 ∀A:Type[0].∀P:list A → Type[0].
857  P [ ] → (∀l,a. P l → P (l@[a])) →
858   ∀l. P l.
859 #A #P #H1 #H2 #l
860 generalize in match (refl … (length … l))
861 generalize in ⊢ (???% → ?) #n generalize in match l
862 elim n
863  [ #L cases L [ // | #x #w #abs (normalize in abs) @⊥ // ]
864  | #m #IH #L #EQ
865    cases (split_len_Sn … EQ) #l' * #a * /3/ ]
866qed.
867
868axiom is_prefix: ∀A:Type[0]. list A → list A → Prop.
869axiom prefix_of_append:
870 ∀A:Type[0].∀l,l1,l2:list A.
871  is_prefix … l l1 → is_prefix … l (l1@l2).
872axiom prefix_reflexive: ∀A,l. is_prefix A l l.
873axiom nil_prefix: ∀A,l. is_prefix A [ ] l.
874
875record Propify (A:Type[0]) : Type[0] (*Prop*) ≝ { in_propify: A }.
876
877definition Propify_elim: ∀A. ∀P:Prop. (A → P) → (Propify A → P) ≝
878 λA,P,H,x. match x with [ mk_Propify p ⇒ H p ].
879
880definition app ≝
881 λA:Type[0].λl1:Propify (list A).λl2:list A.
882  match l1 with
883   [ mk_Propify l1 ⇒ mk_Propify … (l1@l2) ].
884
885lemma app_nil: ∀A,l1. app A l1 [ ] = l1.
886 #A * /3/
887qed.
888
889lemma app_assoc: ∀A,l1,l2,l3. app A (app A l1 l2) l3 = app A l1 (l2@l3).
890 #A * #l1 normalize //
891qed.
892
893let rec foldli (A: Type[0]) (B: Propify (list A) → Type[0])
894 (f: ∀prefix. B prefix → ∀x.B (app … prefix [x]))
895 (prefix: Propify (list A)) (b: B prefix) (l: list A) on l :
896 B (app … prefix l) ≝
897  match l with
898  [ nil ⇒ ? (* b *)
899  | cons hd tl ⇒ ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*)
900  ].
901 [ applyS b
902 | <(app_assoc ?? [hd]) @(foldli A B f (app … prefix [hd]) (f prefix b hd) tl) ]
903qed.
904
905(*
906let rec foldli (A: Type[0]) (B: list A → Type[0]) (f: ∀prefix. B prefix → ∀x. B (prefix@[x]))
907 (prefix: list A) (b: B prefix) (l: list A) on l : B (prefix@l) ≝
908  match l with
909  [ nil ⇒ ? (* b *)
910  | cons hd tl ⇒
911     ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*)
912  ].
913 [ applyS b
914 | applyS (foldli A B f (prefix@[hd]) (f prefix b hd) tl) ]
915qed.
916*)
917
918definition foldll:
919 ∀A:Type[0].∀B: Propify (list A) → Type[0].
920  (∀prefix. B prefix → ∀x. B (app … prefix [x])) →
921   B (mk_Propify … []) → ∀l: list A. B (mk_Propify … l)
922 ≝ λA,B,f. foldli A B f (mk_Propify … [ ]).
923
924axiom is_pprefix: ∀A:Type[0]. Propify (list A) → list A → Prop.
925axiom pprefix_of_append:
926 ∀A:Type[0].∀l,l1,l2.
927  is_pprefix A l l1 → is_pprefix A l (l1@l2).
928axiom pprefix_reflexive: ∀A,l. is_pprefix A (mk_Propify … l) l.
929axiom nil_pprefix: ∀A,l. is_pprefix A (mk_Propify … [ ]) l.
930
931
932axiom foldll':
933 ∀A:Type[0].∀l: list A.
934  ∀B: ∀prefix:Propify (list A). is_pprefix ? prefix l → Type[0].
935  (∀prefix,proof. B prefix proof → ∀x,proof'. B (app … prefix [x]) proof') →
936   B (mk_Propify … [ ]) (nil_pprefix …) → B (mk_Propify … l) (pprefix_reflexive … l).
937 #A #l #B
938 generalize in match (foldll A (λprefix. is_pprefix ? prefix l)) #HH
939
940
941  #H #acc
942 @foldll
943  [
944  |
945  ]
946
947 ≝ λA,B,f. foldli A B f (mk_Propify … [ ]).
948
949
950(*
951record subset (A:Type[0]) (P: A → Prop): Type[0] ≝
952 { subset_wit:> A;
953   subset_proof: P subset_wit
954 }.
955*)
956
957definition build_maps' ≝
958  λpseudo_program.
959  let 〈preamble,instr_list〉 ≝ pseudo_program in
960  let result ≝
961   foldll
962    (option Identifier × pseudo_instruction)
963    (λprefix.
964      Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
965       match prefix return λ_.Prop with [mk_Propify prefix ⇒ tech_pc_sigma0 〈preamble,prefix〉 ≠ None ?])
966    (λprefix,t,i.
967      let 〈labels, pc_costs〉 ≝ t in
968      let 〈program_counter, costs〉 ≝ pc_costs in
969       let 〈label, i'〉 ≝ i in
970       let labels ≝
971         match label with
972         [ None ⇒ labels
973         | Some label ⇒
974           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
975             insert ? ? label program_counter_bv labels
976         ]
977       in
978         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
979         [ None ⇒
980            let dummy ≝ 〈labels,pc_costs〉 in
981              dummy
982         | Some construct ⇒ 〈labels, construct〉
983         ]
984    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 instr_list
985  in
986   let 〈labels, pc_costs〉 ≝ result in
987   let 〈pc, costs〉 ≝ pc_costs in
988    〈labels, costs〉.
989 [
990 | @⊥
991 | normalize % //
992 ]
993qed.
994
995definition build_maps' ≝
996  λpseudo_program.
997  let 〈preamble,instr_list〉 ≝ pseudo_program in
998  let result ≝
999   foldl
1000    (Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
1001          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
1002           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)))
1003    (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
1004          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
1005           is_prefix ? instr_list_prefix' instr_list →
1006           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)
1007    (λt: Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
1008          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
1009           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)).
1010     λi: Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
1011          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
1012           is_prefix ? instr_list_prefix' instr_list →
1013           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ? .
1014      let 〈labels, pc_costs〉 ≝ t in
1015      let 〈program_counter, costs〉 ≝ pc_costs in
1016       let 〈label, i'〉 ≝ i in
1017       let labels ≝
1018         match label with
1019         [ None ⇒ labels
1020         | Some label ⇒
1021           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
1022             insert ? ? label program_counter_bv labels
1023         ]
1024       in
1025         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
1026         [ None ⇒
1027            let dummy ≝ 〈labels,pc_costs〉 in
1028              dummy
1029         | Some construct ⇒ 〈labels, construct〉
1030         ]
1031    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 ?(*instr_list*)
1032  in
1033   let 〈labels, pc_costs〉 ≝ result in
1034   let 〈pc, costs〉 ≝ pc_costs in
1035    〈labels, costs〉.
1036 [4: @(list_elim_rev ?
1037       (λinstr_list. list (
1038        (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
1039          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
1040           is_prefix ? instr_list_prefix' instr_list →
1041           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)))
1042       ?? instr_list) (* CSC: BAD ORDER FOR CODE EXTRACTION *)
1043      [ @[ ]
1044      | #l' #a #limage %2
1045        [ %[@a] #PREFIX #PREFIX_OK
1046        | (* CSC: EVEN WORST CODE FOR EXTRACTION: WE SHOULD STRENGTHEN
1047             THE INDUCTION HYPOTHESIS INSTEAD *)
1048          elim limage
1049           [ %1
1050           | #HD #TL #IH @(?::IH) cases HD #ELEM #K1 %[@ELEM] #K2 #K3
1051             @K1 @(prefix_of_append ???? K3)
1052           ]
1053        ]
1054
1055
1056
1057
1058  cases t in c2 ⊢ % #t' * #LIST_PREFIX * #H1t' #H2t' #HJMt'
1059     % [@ (LIST_PREFIX @ [i])] %
1060      [ cases (sig2 … i LIST_PREFIX) #K1 #K2 @K1
1061      | (* DOABLE IN PRINCIPLE *)
1062      ]
1063 | (* assert false case *)
1064 |3: % [@ ([ ])] % [2: % | (* DOABLE *)]
1065 |
1066
1067let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word)
1068                       (encoding: list Byte) on encoding: Prop ≝
1069  match encoding with
1070  [ nil ⇒ final_pc = pc
1071  | cons hd tl ⇒
1072    let 〈new_pc, byte〉 ≝ next code_memory pc in
1073      hd = byte ∧ encoding_check code_memory new_pc final_pc tl
1074  ].
1075
1076definition assembly_specification:
1077  ∀assembly_program: pseudo_assembly_program.
1078  ∀code_mem: BitVectorTrie Byte 16. Prop ≝
1079  λpseudo_assembly_program.
1080  λcode_mem.
1081    ∀pc: Word.
1082      let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in
1083      let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in
1084      let labels ≝ λx. sigma' pseudo_assembly_program (address_of_word_labels_code_mem instr_list x) in
1085      let datalabels ≝ λx. sigma' pseudo_assembly_program (lookup ? ? x (construct_datalabels preamble) (zero ?)) in
1086      let pre_assembled ≝ assembly_1_pseudoinstruction pseudo_assembly_program
1087       (sigma' pseudo_assembly_program pc) labels datalabels pre_instr in
1088      match pre_assembled with
1089       [ None ⇒ True
1090       | Some pc_code ⇒
1091          let 〈new_pc,code〉 ≝ pc_code in
1092           encoding_check code_mem pc (sigma' pseudo_assembly_program pre_new_pc) code ].
1093
1094axiom assembly_meets_specification:
1095  ∀pseudo_assembly_program.
1096    match assembly pseudo_assembly_program with
1097    [ None ⇒ True
1098    | Some code_mem_cost ⇒
1099      let 〈code_mem, cost〉 ≝ code_mem_cost in
1101    ].
1102(*
1103  # PROGRAM
1104  [ cases PROGRAM
1105    # PREAMBLE
1106    # INSTR_LIST
1107    elim INSTR_LIST
1108    [ whd
1109      whd in ⊢ (∀_. %)
1110      # PC
1111      whd
1112    | # INSTR
1113      # INSTR_LIST_TL
1114      # H
1115      whd
1116      whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
1117    ]
1118  | cases not_implemented
1119  ] *)
1120
1121definition status_of_pseudo_status: PseudoStatus → option Status ≝
1122 λps.
1123  let pap ≝ code_memory … ps in
1124   match assembly pap with
1125    [ None ⇒ None …
1126    | Some p ⇒
1127       let cm ≝ load_code_memory (\fst p) in
1128       let pc ≝ sigma' pap (program_counter ? ps) in
1129        Some …
1130         (mk_PreStatus (BitVectorTrie Byte 16)
1131           cm
1132           (low_internal_ram … ps)
1133           (high_internal_ram … ps)
1134           (external_ram … ps)
1135           pc
1136           (special_function_registers_8051 … ps)
1137           (special_function_registers_8052 … ps)
1138           (p1_latch … ps)
1139           (p3_latch … ps)
1140           (clock … ps)) ].
1141
1142definition write_at_stack_pointer':
1143 ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝
1144  λM: Type[0].
1145  λs: PreStatus M.
1146  λv: Byte.
1147    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
1148    let bit_zero ≝ get_index_v… nu O ? in
1149    let bit_1 ≝ get_index_v… nu 1 ? in
1150    let bit_2 ≝ get_index_v… nu 2 ? in
1151    let bit_3 ≝ get_index_v… nu 3 ? in
1152      if bit_zero then
1153        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
1154                              v (low_internal_ram ? s) in
1155          set_low_internal_ram ? s memory
1156      else
1157        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
1158                              v (high_internal_ram ? s) in
1159          set_high_internal_ram ? s memory.
1160  [ cases l0 %
1161  |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ]
1162qed.
1163
1164definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus.
1165 Σps':PseudoStatus.(code_memory … ps = code_memory … ps')
1166
1167  λticks_of.
1168  λs.
1169  let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in
1170  let ticks ≝ ticks_of (program_counter ? s) in
1171  let s ≝ set_clock ? s (clock ? s + ticks) in
1172  let s ≝ set_program_counter ? s pc in
1173    match instr with
1174    [ Instruction instr ⇒
1175       execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s
1176    | Comment cmt ⇒ s
1177    | Cost cst ⇒ s
1178    | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp)
1179    | Call call ⇒
1180      let a ≝ address_of_word_labels s call in
1181      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
1182      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
1183      let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in
1184      let s ≝ write_at_stack_pointer' ? s pc_bl in
1185      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
1186      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
1187      let s ≝ write_at_stack_pointer' ? s pc_bu in
1188        set_program_counter ? s a
1189    | Mov dptr ident ⇒
1190       set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr
1191    ].
1192 [
1193 |2,3,4: %
1194 | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) %
1195 |
1196 | %
1197 ]
1198 cases not_implemented
1199qed.
1200
1201(*
1202lemma execute_code_memory_unchanged:
1203 ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps).
1204 #ticks #ps whd in ⊢ (??? (??%))
1205 cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps))
1206  (program_counter pseudo_assembly_program ps)) #instr #pc
1207 whd in ⊢ (??? (??%)) cases instr
1208  [ #pre cases pre
1209     [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1210       cases (split ????) #z1 #z2 %
1211     | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1212       cases (split ????) #z1 #z2 %
1213     | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1214       cases (split ????) #z1 #z2 %
1215     | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x
1216       [ #x1 whd in ⊢ (??? (??%))
1217     | *: cases not_implemented
1218     ]
1219  | #comment %
1220  | #cost %
1221  | #label %
1222  | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%))
1223    cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2
1224    whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2
1225    whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%))
1226    (* CSC: ??? *)
1227  | #dptr #label (* CSC: ??? *)
1228  ]
1229  cases not_implemented
1230qed.
1231*)
1232
1233lemma status_of_pseudo_status_failure_depends_only_on_code_memory:
1234 ∀ps,ps': PseudoStatus.
1235  code_memory … ps = code_memory … ps' →
1236   match status_of_pseudo_status ps with
1237    [ None ⇒ status_of_pseudo_status ps' = None …
1238    | Some _ ⇒ ∃w. status_of_pseudo_status ps' = Some … w
1239    ].
1240 #ps #ps' #H whd in ⊢ (mat
1241 ch % with [ _ ⇒ ? | _ ⇒ ? ])
1242 generalize in match (refl … (assembly (code_memory … ps)))
1243 cases (assembly ?) in ⊢ (???% → %)
1244  [ #K whd whd in ⊢ (??%?) <H >K %
1245  | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ]
1246qed.*)
1247
1248let rec encoding_check' (code_memory: BitVectorTrie Byte 16) (pc: Word) (encoding: list Byte) on encoding: Prop ≝
1249  match encoding with
1250  [ nil ⇒ True
1251  | cons hd tl ⇒
1252    let 〈new_pc, byte〉 ≝ next code_memory pc in
1253      hd = byte ∧ encoding_check' code_memory new_pc tl
1254  ].
1255
1256(* prove later *)
1257axiom test:
1258  ∀pc: Word.
1259  ∀code_memory: BitVectorTrie Byte 16.
1260  ∀i: instruction.
1261    let assembled ≝ assembly1 i in
1262      encoding_check' code_memory pc assembled →
1263        let 〈instr_pc, ignore〉 ≝ fetch code_memory pc in
1264        let 〈instr, pc〉 ≝ instr_pc in
1265          instr = i.
1266
1267lemma main_thm:
1268 ∀ticks_of.
1269 ∀ps: PseudoStatus.
1270  match status_of_pseudo_status ps with [ None ⇒ True | Some s ⇒
1271  let ps' ≝ execute_1_pseudo_instruction ticks_of ps in
1272  match status_of_pseudo_status ps' with [ None ⇒ True | Some s'' ⇒
1273  let s' ≝ execute_1 s in
1274   s = s'']].
1275 #ticks_of #ps
1276 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
1277 cases (assembly (code_memory pseudo_assembly_program ps)) [%] * #cm #costs whd
1278 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
1279 generalize in match (sig2 … (execute_1_pseudo_instruction' ticks_of ps))
1280
1281 cases (status_of_pseudo_status (execute_1_pseudo_instruction ticks_of ps)) [%] #s'' whd
Note: See TracBrowser for help on using the repository browser.