source: src/ASM/AssemblyProof.ma @ 890

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

Better statement, begin of uniform proof.

File size: 55.0 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  | 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.
361    list_addressing_mode_tags_elim ? [[ registr ; direct ; indirect ; data ]] (λaddr. P (ADD ? ACC_A addr)) ∧
362    list_addressing_mode_tags_elim ? [[ registr ; direct ; indirect ; data ]] (λaddr. P (ADDC ? ACC_A addr)) ∧
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)) ∧
366    list_addressing_mode_tags_elim ? [[acc_b]] (λaddr. P (MUL ? ACC_A addr)) ∧
367    list_addressing_mode_tags_elim ? [[acc_b]] (λaddr. P (DIV ? ACC_A addr)) ∧
368    list_addressing_mode_tags_elim ? [[ registr ; direct ]] (λaddr. bitvector_elim 8 (λr. P (DJNZ ? addr (RELATIVE r)))) ∧
369    list_addressing_mode_tags_elim ? [[ acc_a ; carry ; bit_addr ]] (λaddr. P (CLR ? addr)) ∧
370    list_addressing_mode_tags_elim ? [[ acc_a ; carry ; bit_addr ]] (λaddr. P (CPL ? addr)) ∧
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))) ∧
389    list_addressing_mode_tags_elim ? [[ carry; bit_addr ]] (λaddr. P (SETB ? addr)) ∧
390    bitvector_elim 8 (λaddr. P (PUSH ? (DIRECT addr))) ∧
391    bitvector_elim 8 (λaddr. P (POP ? (DIRECT addr))) ∧
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)))) ∧
395    list_addressing_mode_tags_elim ? [[ registr; direct; indirect ]] (λaddr. P (XCH ? ACC_A addr)) ∧
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 ]]))
401                   (product_elim ? ? [[carry]] [[ bit_addr ; n_bit_addr]])
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 ]]))
405                   (product_elim ? ? [[carry]] [[ bit_addr ; n_bit_addr]])
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
441(*definition eq_instruction ≝
442  λi, j: instruction.
443    true.*)
444axiom eq_instruction: instruction → instruction → bool.
445axiom eq_instruction_refl: ∀i. eq_instruction i i = true.
446
447let rec vect_member
448  (A: Type[0]) (n: nat) (eq: A → A → bool)
449  (v: Vector A n) (a: A) on v: bool ≝
450  match v with
451  [ VEmpty          ⇒ false
452  | VCons len hd tl ⇒
453    eq hd a ∨ (vect_member A ? eq tl a)
454  ].
455   
456let rec list_addressing_mode_tags_elim_prop
457  (n: nat)
458  (l: Vector addressing_mode_tag (S n))
459  on l:
460  ∀P: l → Prop.
461  ∀direct_a. ∀indirect_a. ∀ext_indirect_a. ∀register_a. ∀acc_a_a.
462  ∀acc_b_a. ∀dptr_a. ∀data_a. ∀data16_a. ∀acc_dptr_a. ∀acc_pc_a.
463  ∀ext_indirect_dptr_a. ∀indirect_dptr_a. ∀carry_a. ∀bit_addr_a.
464  ∀n_bit_addr_a. ∀relative_a. ∀addr11_a. ∀addr16_a.
465  ∀x: l. P x ≝
466  match l return
467    λy.
468      match y with
469      [ O    ⇒ λm: Vector addressing_mode_tag O. ∀prf: 0 = S n. True
470      | S y' ⇒ λl: Vector addressing_mode_tag (S y'). ∀prf: S y' = S n.∀P:l → Prop.
471               ∀direct_a: if vect_member … eq_a l direct then ∀x. P (DIRECT x) else True.
472               ∀indirect_a: if vect_member … eq_a l indirect then ∀x. P (INDIRECT x) else True.
473               ∀ext_indirect_a: if vect_member … eq_a l ext_indirect then ∀x. P (EXT_INDIRECT x) else True.
474               ∀register_a: if vect_member … eq_a l registr then ∀x. P (REGISTER x) else True.
475               ∀acc_a_a: if vect_member … eq_a l acc_a then P (ACC_A) else True.
476               ∀acc_b_a: if vect_member … eq_a l acc_b then P (ACC_B) else True.
477               ∀dptr_a: if vect_member … eq_a l dptr then P DPTR else True.
478               ∀data_a: if vect_member … eq_a l data then ∀x. P (DATA x) else True.
479               ∀data16_a: if vect_member … eq_a l data16 then ∀x. P (DATA16 x) else True.
480               ∀acc_dptr_a: if vect_member … eq_a l acc_dptr then P ACC_DPTR else True.
481               ∀acc_pc_a: if vect_member … eq_a l acc_pc then P ACC_PC else True.
482               ∀ext_indirect_dptr_a: if vect_member … eq_a l ext_indirect_dptr then P EXT_INDIRECT_DPTR else True.
483               ∀indirect_dptr_a: if vect_member … eq_a l indirect_dptr then P INDIRECT_DPTR else True.
484               ∀carry_a: if vect_member … eq_a l carry then P CARRY else True.
485               ∀bit_addr_a: if vect_member … eq_a l bit_addr then ∀x. P (BIT_ADDR x) else True.
486               ∀n_bit_addr_a: if vect_member … eq_a l n_bit_addr then ∀x. P (N_BIT_ADDR x) else True.
487               ∀relative_a: if vect_member … eq_a l relative then ∀x. P (RELATIVE x) else True.
488               ∀addr11_a: if vect_member … eq_a l addr11 then ∀x. P (ADDR11 x) else True.
489               ∀addr_16_a: if vect_member … eq_a l addr16 then ∀x. P (ADDR16 x) else True.
490               ∀x:l. P x
491      ] with
492  [ VEmpty          ⇒ λAbsurd. ⊥
493  | VCons len hd tl ⇒ λProof. ?
494  ] (refl ? (S n)). cases daemon. qed. (*
495  [ destruct(Absurd)
496  | # A1 # A2 # A3 # A4 # A5 # A6 # A7
497    # A8 # A9 # A10 # A11 # A12 # A13 # A14
498    # A15 # A16 # A17 # A18 # A19 # X
499    cases X
500    # SUB cases daemon ] qed.
501    cases SUB
502    [ # BYTE
503    normalize
504  ].
505 
506 
507(*    let prepare_hd ≝
508      match hd with
509      [ direct ⇒ λdirect_prf. ?
510      | indirect ⇒ λindirect_prf. ?
511      | ext_indirect ⇒ λext_indirect_prf. ?
512      | registr ⇒ λregistr_prf. ?
513      | acc_a ⇒ λacc_a_prf. ?
514      | acc_b ⇒ λacc_b_prf. ?
515      | dptr ⇒ λdptr_prf. ?
516      | data ⇒ λdata_prf. ?
517      | data16 ⇒ λdata16_prf. ?
518      | acc_dptr ⇒ λacc_dptr_prf. ?
519      | acc_pc ⇒ λacc_pc_prf. ?
520      | ext_indirect_dptr ⇒ λext_indirect_prf. ?
521      | indirect_dptr ⇒ λindirect_prf. ?
522      | carry ⇒ λcarry_prf. ?
523      | bit_addr ⇒ λbit_addr_prf. ?
524      | n_bit_addr ⇒ λn_bit_addr_prf. ?
525      | relative ⇒ λrelative_prf. ?
526      | addr11 ⇒ λaddr11_prf. ?
527      | addr16 ⇒ λaddr16_prf. ?
528      ]
529    in ? *)
530  ].
531  [ 1: destruct(absd)
532  | 2: # A1 # A2 # A3 # A4 # A5 # A6
533       # A7 # A8 # A9 # A10 # A11 # A12
534       # A13 # A14 # A15 # A16 # A17 # A18
535       # A19 *
536  ].
537
538
539  match l return λx.match x with [O ⇒ λl: Vector … O. bool | S x' ⇒ λl: Vector addressing_mode_tag (S x').
540   (l → bool) → bool ] with
541  [ VEmpty      ⇒  true 
542  | VCons len hd tl ⇒ λP.
543    let process_hd ≝
544      match hd return λhd. ∀P: hd:::tl → bool. bool with
545      [ direct ⇒ λP.bitvector_elim 8 (λx. P (DIRECT x))
546      | indirect ⇒ λP.bit_elim (λx. P (INDIRECT x))
547      | ext_indirect ⇒ λP.bit_elim (λx. P (EXT_INDIRECT x))
548      | registr ⇒ λP.bitvector_elim 3 (λx. P (REGISTER x))
549      | acc_a ⇒ λP.P ACC_A
550      | acc_b ⇒ λP.P ACC_B
551      | dptr ⇒ λP.P DPTR
552      | data ⇒ λP.bitvector_elim 8 (λx. P (DATA x))
553      | data16 ⇒ λP.bitvector_elim 16 (λx. P (DATA16 x))
554      | acc_dptr ⇒ λP.P ACC_DPTR
555      | acc_pc ⇒ λP.P ACC_PC
556      | ext_indirect_dptr ⇒ λP.P EXT_INDIRECT_DPTR
557      | indirect_dptr ⇒ λP.P INDIRECT_DPTR
558      | carry ⇒ λP.P CARRY
559      | bit_addr ⇒ λP.bitvector_elim 8 (λx. P (BIT_ADDR x))
560      | n_bit_addr ⇒ λP.bitvector_elim 8 (λx. P (N_BIT_ADDR x))
561      | relative ⇒ λP.bitvector_elim 8 (λx. P (RELATIVE x))
562      | addr11 ⇒ λP.bitvector_elim 11 (λx. P (ADDR11 x))
563      | addr16 ⇒ λP.bitvector_elim 16 (λx. P (ADDR16 x))
564      ]
565    in
566      andb (process_hd P)
567       (match len return λx. x = len → bool with
568         [ O ⇒ λprf. true
569         | S y ⇒ λprf. list_addressing_mode_tags_elim y ? P ] (refl ? len))
570  ].
571  try %
572  [ 2: cases (sym_eq ??? prf); @tl
573  | generalize in match H; generalize in match tl; cases prf;
574    (* cases prf in tl H; : ??? WAS WORKING BEFORE *)
575    #tl
576    normalize in ⊢ (∀_: %. ?)
577    # H
578    whd
579    normalize in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
580    cases (is_a hd (subaddressing_modeel y tl H)) whd // ]
581qed.
582*)
583(*
584lemma test:
585  let i ≝ SJMP (RELATIVE (bitvector_of_nat 8 255)) in
586      (let assembled ≝ assembly1 i in
587      let code_memory ≝ load_code_memory assembled in
588      let fetched ≝ fetch code_memory ? in
589      let 〈instr_pc, ticks〉 ≝ fetched in
590        eq_instruction (\fst instr_pc)) i = true.
591 [2: @ zero
592 | normalize
593 ]*)
594
595lemma BitVectorTrie_O:
596 ∀A:Type[0].∀v:BitVectorTrie A 0.(∃w. v ≃ Leaf A w) ∨ v ≃ Stub A 0.
597 #A #v generalize in match (refl … O) cases v in ⊢ (??%? → (?(??(λ_.?%%??)))(?%%??))
598  [ #w #_ %1 %[@w] %
599  | #n #l #r #abs @⊥ //
600  | #n #EQ %2 >EQ %]
601qed.
602
603lemma BitVectorTrie_Sn:
604 ∀A:Type[0].∀n.∀v:BitVectorTrie A (S n).(∃l,r. v ≃ Node A n l r) ∨ v ≃ Stub A (S n).
605 #A #n #v generalize in match (refl … (S n)) cases v in ⊢ (??%? → (?(??(λ_.??(λ_.?%%??))))%)
606  [ #m #abs @⊥ //
607  | #m #l #r #EQ %1 <(injective_S … EQ) %[@l] %[@r] //
608  | #m #EQ %2 // ]
609qed.
610
611lemma lookup_prepare_trie_for_insertion_hit:
612 ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.
613  lookup … b (prepare_trie_for_insertion … b v) a = v.
614 #A #a #v #n #b elim b // #m #hd #tl #IH cases hd normalize //
615qed.
616 
617lemma lookup_insert_hit:
618 ∀A:Type[0].∀a,v:A.∀n.∀b:BitVector n.∀t:BitVectorTrie A n.
619  lookup … b (insert … b v t) a = v.
620 #A #a #v #n #b elim b -b -n //
621 #n #hd #tl #IH #t cases(BitVectorTrie_Sn … t)
622  [ * #l * #r #JMEQ >JMEQ cases hd normalize //
623  | #JMEQ >JMEQ cases hd normalize @lookup_prepare_trie_for_insertion_hit ]
624qed.
625
626lemma BitVector_O: ∀v:BitVector 0. v ≃ VEmpty bool.
627 #v generalize in match (refl … 0) cases v in ⊢ (??%? → ?%%??) //
628 #n #hd #tl #abs @⊥ //
629qed.
630
631lemma BitVector_Sn: ∀n.∀v:BitVector (S n).
632 ∃hd.∃tl.v ≃ VCons bool n hd tl.
633 #n #v generalize in match (refl … (S n)) cases v in ⊢ (??%? → ??(λ_.??(λ_.?%%??)))
634 [ #abs @⊥ //
635 | #m #hd #tl #EQ <(injective_S … EQ) %[@hd] %[@tl] // ]
636qed.
637
638coercion bool_to_Prop: ∀b:bool. Prop ≝ bool_to_Prop on _b:bool to Type[0].
639
640lemma lookup_prepare_trie_for_insertion_miss:
641 ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.
642  (notb (eq_bv ? b c)) → lookup … b (prepare_trie_for_insertion … c v) a = a.
643 #A #a #v #n #c elim c
644  [ #b >(BitVector_O … b) normalize #abs @⊥ //
645  | #m #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ
646    cases hd cases hd' normalize
647    [2,3: #_ cases tl' //
648    |*: change with (bool_to_Prop (notb (eq_bv ???)) → ?) /2/ ]]
649qed.
650 
651lemma lookup_insert_miss:
652 ∀A:Type[0].∀a,v:A.∀n.∀c,b:BitVector n.∀t:BitVectorTrie A n.
653  (notb (eq_bv ? b c)) → lookup … b (insert … c v t) a = lookup … b t a.
654 #A #a #v #n #c elim c -c -n
655  [ #b #t #DIFF @⊥ whd in DIFF; >(BitVector_O … b) in DIFF //
656  | #n #hd #tl #IH #b cases(BitVector_Sn … b) #hd' * #tl' #JMEQ >JMEQ
657    #t cases(BitVectorTrie_Sn … t)
658    [ * #l * #r #JMEQ >JMEQ cases hd cases hd' #H normalize in H;
659     [1,4: change in H with (bool_to_Prop (notb (eq_bv ???))) ] normalize // @IH //
660    | #JMEQ >JMEQ cases hd cases hd' #H normalize in H;
661     [1,4: change in H with (bool_to_Prop (notb (eq_bv ???))) ] normalize
662     [3,4: cases tl' // | *: @lookup_prepare_trie_for_insertion_miss //]]]
663qed.
664
665definition load_code_memory_aux ≝
666 fold_left_i_aux … (
667   λi, mem, v.
668     insert … (bitvector_of_nat … i) v mem) (Stub Byte 16).
669
670axiom split_elim:
671 ∀A,l,m,v.∀P: (Vector A l) × (Vector A m) → Prop.
672  (∀vl,vm. v = vl@@vm → P 〈vl,vm〉) → P (split A l m v).
673
674axiom half_add_SO:
675 ∀pc.
676 \snd (half_add 16 (bitvector_of_nat … pc) (bitvector_of_nat … 1)) = bitvector_of_nat … (S pc).
677
678axiom not_eqvb_S:
679 ∀pc.
680 (¬eq_bv 16 (bitvector_of_nat 16 pc) (bitvector_of_nat 16 (S pc))).
681
682axiom not_eqvb_SS:
683 ∀pc.
684 (¬eq_bv 16 (bitvector_of_nat 16 pc) (bitvector_of_nat 16 (S (S pc)))).
685
686axiom bitvector_elim_complete:
687 ∀n,P. bitvector_elim n P = true → ∀bv. P bv.
688
689lemma bitvector_elim_complete':
690 ∀n,P. bitvector_elim n P = true → ∀bv. P bv = true.
691 #n #P #H generalize in match (bitvector_elim_complete … H) #K #bv
692 generalize in match (K bv) normalize cases (P bv) normalize // #abs @⊥ //
693qed.
694
695lemma andb_elim':
696 ∀b1,b2. (b1 = true) → (b2 = true) → (b1 ∧ b2) = true.
697 #b1 #b2 #H1 #H2 @andb_elim cases b1 in H1; normalize //
698qed.
699
700let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word)
701                       (encoding: list Byte) on encoding: Prop ≝
702  match encoding with
703  [ nil ⇒ final_pc = pc
704  | cons hd tl ⇒
705    let 〈new_pc, byte〉 ≝ next code_memory pc in
706      hd = byte ∧ encoding_check code_memory new_pc final_pc tl
707  ].
708
709axiom eq_bv_refl: ∀n,v. eq_bv n v v = true.
710
711(*
712lemma test:
713  ∀pc,i.
714     (let assembled ≝ assembly1 i in
715      let code_memory ≝ load_code_memory_aux pc assembled in
716      let fetched ≝ fetch code_memory (bitvector_of_nat … pc) in
717      let 〈instr_pc, ticks〉 ≝ fetched in
718        eq_instruction (\fst instr_pc)) i = true.
719*)
720lemma test:
721  ∀pc,i,code_memory.
722      let assembled ≝ assembly1 i in
723      let len ≝ length … assembled in
724      encoding_check code_memory (bitvector_of_nat … pc) (bitvector_of_nat … (pc + len)) assembled →
725      let fetched ≝ fetch code_memory (bitvector_of_nat … pc) in
726      let 〈instr_pc, ticks〉 ≝ fetched in
727      let 〈instr,pc'〉 ≝ instr_pc in
728       (eq_instruction instr i ∧ eq_bv … pc' (bitvector_of_nat … (pc + len))) = true.
729 #pc #i #code_memory cases i [8: *]
730 [16,20,29: * * |18,19: * * [1,2,4,5: *] |28: * * [1,2: * [1,2: * [1,2: * [1,2: *]]]]]
731 [47,48,49: |*: #arg @(list_addressing_mode_tags_elim_prop … arg) whd try % -arg
732  [2,3,5,7,10,12,16,17,18,21,25,26,27,30,31,32,37,38,39,40,41,42,43,44,45,48,51,58,
733   59,60,63,64,65,66,67: #ARG]
734 [1: #arg2 @(list_addressing_mode_tags_elim_prop … arg2) whd try % -arg2
735 
736 [7: #arg2]
737 
738  whd
739 [ @(list_addressing_mode_tags_elim_prop … arg) whd try %
740   @(list_addressing_mode_tags_elim_prop … arg2) whd try %
741   * #H1 #H2 whd in H2; whd change in ⊢ match % with [_ ⇒ ?] with (fetch0 ??)
742   whd in ⊢ match ??% with [_ ⇒ ?] <H1 whd <H2 >eq_instruction_refl @eq_bv_refl
743 |6: @(list_addressing_mode_tags_elim_prop … arg) whd try % #XX
744   * #H1 * #H2 #H3 whd in H3; whd change in ⊢ match % with [_ ⇒ ?] with (fetch0 ??)
745   whd in ⊢ match ??% with [_ ⇒ ?] <H1 whd <H2 <H3 >eq_instruction_refl @eq_bv_refl
746 |7: @(list_addressing_mode_tags_elim_prop … arg) whd try %
747   * #H1 #H2 whd in H2; whd change in ⊢ match % with [_ ⇒ ?] with (fetch0 ??)
748 |3,5: @(list_addressing_mode_tags_elim_prop … arg) whd try % #XX
749   
750   @split_elim
751
752 
753   whd in ⊢ (??%?)
754   [2,4: @(list_addressing_mode_tags_elim_prop … arg) whd try % #XX
755         whd in ⊢ (??(match ? (? ? %) ? with [ _ ⇒ ?] ?)?)
756         @split_elim #b1 #b2 #EQ >EQ -EQ;
757        change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
758        whd in ⊢ (??(match ?%% with [ _ ⇒ ?] ?)?)
759        >lookup_insert_miss try @not_eqvb_SS
760        >lookup_insert_miss //
761        >lookup_insert_hit
762        whd in ⊢ (??%?) whd in ⊢ (??(?%?)?)
763        >half_add_SO >half_add_SO
764        >lookup_insert_miss try @not_eqvb_S
765        >lookup_insert_hit
766        >lookup_insert_hit
767        @eq_instruction_refl
768   |1,3: @(list_addressing_mode_tags_elim_prop … arg) whd try % #XX
769     whd in ⊢ (??(match ? (? ? %) ? with [ _ ⇒ ?] ?)?)
770     @split_elim #b1 #b2 #EQ >EQ -EQ;
771     change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
772     whd in ⊢ (??(match ?%% with [ _ ⇒ ?] ?)?)
773     >lookup_insert_miss //
774     >lookup_insert_hit
775     >half_add_SO
776     @(bitvector_elim_complete' … b1) @andb_elim' @andb_elim' @andb_elim'
777     whd in ⊢ (??%?) normalize in ⊢ (??(?(???(?))%)?) >lookup_insert_hit
778     normalize
779     (* FALSO!!! AJMP vs ACALL *)
780     cases daemon
781   | @(list_addressing_mode_tags_elim_prop … arg) whd try % #XX
782     whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
783     change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
784     whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
785     >lookup_insert_miss //
786     >lookup_insert_hit
787     >half_add_SO
788     whd in ⊢ (??%?)
789     >lookup_insert_hit
790     normalize
791     @eq_instruction_refl
792   | @(list_addressing_mode_tags_elim_prop … arg) whd try %
793     whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
794     change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
795     whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
796     >lookup_insert_hit
797     >half_add_SO
798     normalize
799     @eq_instruction_refl
800   | @(list_addressing_mode_tags_elim_prop … arg) whd try %
801     @(list_addressing_mode_tags_elim_prop … arg2) whd try %
802     whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
803     change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
804     whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
805     >lookup_insert_hit
806     >half_add_SO
807     normalize
808     @eq_instruction_refl
809   | cases arg -i arg;
810(* 
81116,17,18,19,20,28,19
812*)
813     [1,2,3: #arg1 #arg2
814       @(list_addressing_mode_tags_elim_prop … arg1) whd try %
815       @(list_addressing_mode_tags_elim_prop … arg2) whd try % #XX
816       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
817       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
818       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
819       [1,4,5,8,9,12: >lookup_insert_miss //]
820       >lookup_insert_hit >half_add_SO
821       [1,2,3,4,5,6: whd in ⊢ (??%?) >lookup_insert_hit]
822       normalize
823       @eq_instruction_refl
824     |35,36,37:
825       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
826       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
827       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
828       >lookup_insert_hit >half_add_SO
829       normalize
830       @eq_instruction_refl
831     |6,7,8,23,24,25,26,27: #arg1 try #arg2
832       @(list_addressing_mode_tags_elim_prop … arg1) whd try %
833       try (@(list_addressing_mode_tags_elim_prop … arg2) whd try %)
834       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
835       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
836       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
837       >lookup_insert_hit >half_add_SO
838       normalize
839       @eq_instruction_refl
840     |9,10,14,15,31,32: #arg1
841       @(list_addressing_mode_tags_elim_prop … arg1) whd try % #XX
842       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
843       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
844       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
845       >lookup_insert_miss //
846       >lookup_insert_hit
847       >half_add_SO
848       whd in ⊢ (??%?)
849       >lookup_insert_hit
850       normalize
851       @eq_instruction_refl
852     |33,34: #arg1 #arg2
853       @(list_addressing_mode_tags_elim_prop … arg1) whd try %
854       @(list_addressing_mode_tags_elim_prop … arg2) whd try % #XX2
855       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
856       whd in ⊢ (??(match ?(????%?)? with [_ ⇒ ?] ?)?)
857       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
858       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
859       [>lookup_insert_miss //] >lookup_insert_hit >half_add_SO whd in ⊢ (??%?)
860       [>lookup_insert_hit]
861       normalize
862       @eq_instruction_refl
863     |4,5,21,22,30: #arg1
864       @(list_addressing_mode_tags_elim_prop … arg1) whd try % try #XX1
865       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
866       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
867       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
868       [1,6,12,15,17: >lookup_insert_miss //] >lookup_insert_hit >half_add_SO whd in ⊢ (??%?)
869       [1,2,3,4,5: >lookup_insert_hit] normalize @eq_instruction_refl
870     |11,12,13: #arg1 #arg2
871       @(list_addressing_mode_tags_elim_prop … arg1) whd try % #XX1
872       @(list_addressing_mode_tags_elim_prop … arg2) whd try % #XX2
873       whd in ⊢ (??(match ?%? with [ _ ⇒ ?] ?)?)
874       whd in ⊢ (??(match ?(????%?)? with [_ ⇒ ?] ?)?)
875       change in ⊢ (??(match % with [ _ ⇒ ?] ?)?) with (fetch0 ??)
876       whd in ⊢ (??(match ??% with [ _ ⇒ ?] ?)?)
877       >lookup_insert_miss try @not_eqvb_SS
878       >lookup_insert_miss //
879       >lookup_insert_hit
880       >half_add_SO
881       whd in ⊢ (??%?) whd in ⊢ (??(?%?)?)
882       >half_add_SO
883       >lookup_insert_hit
884       >lookup_insert_miss //
885       >lookup_insert_hit
886       normalize
887       @eq_instruction_refl
888 ]
889qed.   
890 
891(* This establishes the correspondence between pseudo program counters and
892   program counters. It is at the heart of the proof. *)
893(*CSC: code taken from build_maps *)
894definition sigma0: pseudo_assembly_program → option (nat × (nat × (BitVectorTrie Word 16))) ≝
895 λinstr_list.
896  foldl ??
897    (λt. λi.
898       match t with
899       [ None ⇒ None ?
900       | Some ppc_pc_map ⇒
901         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
902         let 〈program_counter, sigma_map〉 ≝ pc_map in
903         let 〈label, i〉 ≝ i in
904          match construct_costs instr_list program_counter (λx. zero ?) (λx. zero ?) (Stub …) i with
905           [ None ⇒ None ?
906           | Some pc_ignore ⇒
907              let 〈pc,ignore〉 ≝ pc_ignore in
908              Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ]
909       ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (\snd instr_list).
910       
911definition tech_pc_sigma0: pseudo_assembly_program → option (nat × (BitVectorTrie Word 16)) ≝
912 λinstr_list.
913  match sigma0 instr_list with
914   [ None ⇒ None …
915   | Some result ⇒
916      let 〈ppc,pc_sigma_map〉 ≝ result in
917       Some … pc_sigma_map ].
918
919definition sigma_safe: pseudo_assembly_program → option (Word → Word) ≝       
920 λinstr_list.
921  match sigma0 instr_list with
922  [ None ⇒ None ?
923  | Some result ⇒
924    let 〈ppc,pc_sigma_map〉 ≝ result in
925    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
926      if gtb pc (2^16) then
927        None ?
928      else
929        Some ? (λx.lookup ?? x sigma_map (zero …)) ].
930
931axiom policy_ok: ∀p. sigma_safe p ≠ None ….
932
933definition sigma: pseudo_assembly_program → Word → Word ≝
934 λp.
935  match sigma_safe p return λr:option (Word → Word). r ≠ None … → Word → Word with
936   [ None ⇒ λabs. ⊥
937   | Some r ⇒ λ_.r] (policy_ok p).
938 cases abs //
939qed.
940
941lemma length_append:
942 ∀A.∀l1,l2:list A.
943  |l1 @ l2| = |l1| + |l2|.
944 #A #l1 elim l1
945  [ //
946  | #hd #tl #IH #l2 normalize <IH //]
947qed.
948
949let rec does_not_occur (id:Identifier) (l:list labelled_instruction) on l: bool ≝
950 match l with
951  [ nil ⇒ true
952  | cons hd tl ⇒ notb (instruction_matches_identifier id hd) ∧ does_not_occur id tl].
953
954lemma does_not_occur_None:
955 ∀id,i,list_instr.
956  does_not_occur id (list_instr@[〈None …,i〉]) =
957  does_not_occur id list_instr.
958 #id #i #list_instr elim list_instr
959  [ % | #hd #tl #IH whd in ⊢ (??%%) >IH %]
960qed.
961
962let rec occurs_exactly_once (id:Identifier) (l:list labelled_instruction) on l : bool ≝
963 match l with
964  [ nil ⇒ false
965  | cons hd tl ⇒
966     if instruction_matches_identifier id hd then
967      does_not_occur id tl
968     else
969      occurs_exactly_once id tl ].
970
971lemma occurs_exactly_once_None:
972 ∀id,i,list_instr.
973  occurs_exactly_once id (list_instr@[〈None …,i〉]) =
974  occurs_exactly_once id list_instr.
975 #id #i #list_instr elim list_instr
976  [ % | #hd #tl #IH whd in ⊢ (??%%) >IH >does_not_occur_None %]
977qed.
978
979lemma index_of_internal_None: ∀i,id,instr_list,n.
980 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
981  index_of_internal ? (instruction_matches_identifier id) instr_list n =
982   index_of_internal ? (instruction_matches_identifier id) (instr_list@[〈None …,i〉]) n.
983 #i #id #instr_list elim instr_list
984  [ #n #abs whd in abs; cases abs
985  | #hd #tl #IH #n whd in ⊢ (% → ??%%); whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ?)
986    cases (instruction_matches_identifier id hd) whd in ⊢ (match % with [_ ⇒ ? | _ ⇒ ?] → ??%%)
987    [ #H %
988    | #H @IH whd in H; cases (occurs_exactly_once ??) in H ⊢ %
989      [ #_ % | #abs cases abs ]]]
990qed.
991
992lemma address_of_word_labels_code_mem_None: ∀i,id,instr_list.
993 occurs_exactly_once id (instr_list@[〈None …,i〉]) →
994  address_of_word_labels_code_mem instr_list id =
995  address_of_word_labels_code_mem (instr_list@[〈None …,i〉]) id.
996 #i #id #instr_list #H whd in ⊢ (??%%) whd in ⊢ (??(??%?)(??%?))
997 >(index_of_internal_None … H) %
998qed.
999
1000axiom tech_pc_sigma0_append:
1001 ∀preamble,instr_list,prefix,label,i,pc',code,pc,costs,costs'.
1002  Some … 〈pc,costs〉 = tech_pc_sigma0 〈preamble,prefix〉 →
1003   construct_costs 〈preamble,instr_list〉 … pc (λx.zero 16) (λx. zero 16) costs i = Some … 〈pc',code〉 →
1004    tech_pc_sigma0 〈preamble,prefix@[〈label,i〉]〉 = Some … 〈pc',costs'〉.
1005
1006axiom tech_pc_sigma0_append_None:
1007 ∀preamble,instr_list,prefix,i,pc,costs.
1008  Some … 〈pc,costs〉 = tech_pc_sigma0 〈preamble,prefix〉 →
1009   construct_costs 〈preamble,instr_list〉 … pc (λx.zero 16) (λx. zero 16) costs i = None …
1010    → False.
1011
1012
1013definition build_maps' ≝
1014  λpseudo_program.
1015  let 〈preamble,instr_list〉 ≝ pseudo_program in
1016  let result ≝
1017   foldl_strong
1018    (option Identifier × pseudo_instruction)
1019    (λpre. Σres:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
1020      let pre' ≝ 〈preamble,pre〉 in
1021      let 〈labels,pc_costs〉 ≝ res in
1022       tech_pc_sigma0 pre' = Some … pc_costs ∧
1023       ∀id. occurs_exactly_once id pre →
1024        lookup ?? id labels (zero …) = sigma pre' (address_of_word_labels_code_mem pre id))
1025    instr_list
1026    (λprefix,i,tl,prf,t.
1027      let 〈labels, pc_costs〉 ≝ t in
1028      let 〈program_counter, costs〉 ≝ pc_costs in
1029       let 〈label, i'〉 ≝ i in
1030       let labels ≝
1031         match label with
1032         [ None ⇒ labels
1033         | Some label ⇒
1034           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
1035             insert ? ? label program_counter_bv labels
1036         ]
1037       in
1038         match construct_costs 〈preamble,instr_list〉 program_counter (λx. zero ?) (λx. zero ?) costs i' with
1039         [ None ⇒
1040            let dummy ≝ 〈labels,pc_costs〉 in
1041             dummy
1042         | Some construct ⇒ 〈labels, construct〉
1043         ]
1044    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉
1045  in
1046   let 〈labels, pc_costs〉 ≝ result in
1047   let 〈pc, costs〉 ≝ pc_costs in
1048    〈labels, costs〉.
1049 [3: whd % // #id normalize in ⊢ (% → ?) #abs @⊥ //
1050 | whd cases construct in p3 #PC #CODE #JMEQ %
1051    [ @(tech_pc_sigma0_append ??????????? (jmeq_to_eq ??? JMEQ)) | #id #Hid ]
1052 | (* dummy case *) @⊥
1053   @(tech_pc_sigma0_append_None ?? prefix ???? (jmeq_to_eq ??? p3)) ]
1054 [*: generalize in match (sig2 … t) whd in ⊢ (% → ?)
1055     >p whd in ⊢ (% → ?) >p1 * #IH0 #IH1 >IH0 // ]
1056 whd in ⊢ (??(????%?)?) -labels1;
1057 cases label in Hid
1058  [ #Hid whd in ⊢ (??(????%?)?) >IH1 -IH1
1059     [ >(address_of_word_labels_code_mem_None … Hid)
1060       (* MANCA LEMMA: INDIRIZZO TROVATO NEL PROGRAMMA! *)
1061     | whd in Hid >occurs_exactly_once_None in Hid // ]
1062  | -label #label #Hid whd in ⊢ (??(????%?)?)
1063   
1064  ]
1065qed.
1066
1067(*
1068(*
1069notation < "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
1070 with precedence 10
1071for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }.
1072*)
1073
1074lemma build_maps_ok:
1075 ∀p:pseudo_assembly_program.
1076  let 〈labels,costs〉 ≝ build_maps' p in
1077   ∀pc.
1078    (nat_of_bitvector … pc) < length … (\snd p) →
1079     lookup ?? pc labels (zero …) = sigma p (\snd (fetch_pseudo_instruction (\snd p) pc)).
1080 #p cases p #preamble #instr_list
1081  elim instr_list
1082   [ whd #pc #abs normalize in abs; cases (not_le_Sn_O ?) [#H cases (H abs) ]
1083   | #hd #tl #IH
1084    whd in ⊢ (match % with [ _ ⇒ ?])
1085   ]
1086qed.
1087*)
1088
1089(*
1090lemma list_elim_rev:
1091 ∀A:Type[0].∀P:list A → Prop.
1092  P [ ] → (∀n,l. length l = n → P l → 
1093  P [ ] → (∀l,a. P l → P (l@[a])) →
1094   ∀l. P l.
1095 #A #P
1096qed.*)
1097
1098lemma rev_preserves_length:
1099 ∀A.∀l. length … (rev A l) = length … l.
1100  #A #l elim l
1101   [ %
1102   | #hd #tl #IH normalize >length_append normalize /2/ ]
1103qed.
1104
1105lemma rev_append:
1106 ∀A.∀l1,l2.
1107  rev A (l1@l2) = rev A l2 @ rev A l1.
1108 #A #l1 elim l1 normalize //
1109qed.
1110 
1111lemma rev_rev: ∀A.∀l. rev … (rev A l) = l.
1112 #A #l elim l
1113  [ //
1114  | #hd #tl #IH normalize >rev_append normalize // ]
1115qed.
1116
1117lemma split_len_Sn:
1118 ∀A:Type[0].∀l:list A.∀len.
1119  length … l = S len →
1120   Σl'.Σa. l = l'@[a] ∧ length … l' = len.
1121 #A #l elim l
1122  [ normalize #len #abs destruct
1123  | #hd #tl #IH #len
1124    generalize in match (rev_rev … tl)
1125    cases (rev A tl) in ⊢ (??%? → ?)
1126     [ #H <H normalize #EQ % [@[ ]] % [@hd] normalize /2/ 
1127     | #a #l' #H <H normalize #EQ
1128      %[@(hd::rev … l')] %[@a] % //
1129      >length_append in EQ #EQ normalize in EQ; normalize;
1130      generalize in match (injective_S … EQ) #EQ2 /2/ ]]
1131qed.
1132
1133lemma list_elim_rev:
1134 ∀A:Type[0].∀P:list A → Type[0].
1135  P [ ] → (∀l,a. P l → P (l@[a])) →
1136   ∀l. P l.
1137 #A #P #H1 #H2 #l
1138 generalize in match (refl … (length … l))
1139 generalize in ⊢ (???% → ?) #n generalize in match l
1140 elim n
1141  [ #L cases L [ // | #x #w #abs (normalize in abs) @⊥ // ]
1142  | #m #IH #L #EQ
1143    cases (split_len_Sn … EQ) #l' * #a * /3/ ]
1144qed.
1145
1146axiom is_prefix: ∀A:Type[0]. list A → list A → Prop.
1147axiom prefix_of_append:
1148 ∀A:Type[0].∀l,l1,l2:list A.
1149  is_prefix … l l1 → is_prefix … l (l1@l2).
1150axiom prefix_reflexive: ∀A,l. is_prefix A l l.
1151axiom nil_prefix: ∀A,l. is_prefix A [ ] l.
1152
1153record Propify (A:Type[0]) : Type[0] (*Prop*) ≝ { in_propify: A }.
1154
1155definition Propify_elim: ∀A. ∀P:Prop. (A → P) → (Propify A → P) ≝
1156 λA,P,H,x. match x with [ mk_Propify p ⇒ H p ].
1157
1158definition app ≝
1159 λA:Type[0].λl1:Propify (list A).λl2:list A.
1160  match l1 with
1161   [ mk_Propify l1 ⇒ mk_Propify … (l1@l2) ].
1162
1163lemma app_nil: ∀A,l1. app A l1 [ ] = l1.
1164 #A * /3/
1165qed.
1166
1167lemma app_assoc: ∀A,l1,l2,l3. app A (app A l1 l2) l3 = app A l1 (l2@l3).
1168 #A * #l1 normalize //
1169qed.
1170
1171let rec foldli (A: Type[0]) (B: Propify (list A) → Type[0])
1172 (f: ∀prefix. B prefix → ∀x.B (app … prefix [x]))
1173 (prefix: Propify (list A)) (b: B prefix) (l: list A) on l :
1174 B (app … prefix l) ≝
1175  match l with
1176  [ nil ⇒ ? (* b *)
1177  | cons hd tl ⇒ ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*)
1178  ].
1179 [ applyS b
1180 | <(app_assoc ?? [hd]) @(foldli A B f (app … prefix [hd]) (f prefix b hd) tl) ]
1181qed.
1182
1183(*
1184let rec foldli (A: Type[0]) (B: list A → Type[0]) (f: ∀prefix. B prefix → ∀x. B (prefix@[x]))
1185 (prefix: list A) (b: B prefix) (l: list A) on l : B (prefix@l) ≝
1186  match l with
1187  [ nil ⇒ ? (* b *)
1188  | cons hd tl ⇒
1189     ? (*foldli A B f (prefix@[hd]) (f prefix b hd) tl*)
1190  ].
1191 [ applyS b
1192 | applyS (foldli A B f (prefix@[hd]) (f prefix b hd) tl) ]
1193qed.
1194*)
1195
1196definition foldll:
1197 ∀A:Type[0].∀B: Propify (list A) → Type[0].
1198  (∀prefix. B prefix → ∀x. B (app … prefix [x])) →
1199   B (mk_Propify … []) → ∀l: list A. B (mk_Propify … l)
1200 ≝ λA,B,f. foldli A B f (mk_Propify … [ ]).
1201
1202axiom is_pprefix: ∀A:Type[0]. Propify (list A) → list A → Prop.
1203axiom pprefix_of_append:
1204 ∀A:Type[0].∀l,l1,l2.
1205  is_pprefix A l l1 → is_pprefix A l (l1@l2).
1206axiom pprefix_reflexive: ∀A,l. is_pprefix A (mk_Propify … l) l.
1207axiom nil_pprefix: ∀A,l. is_pprefix A (mk_Propify … [ ]) l.
1208
1209
1210axiom foldll':
1211 ∀A:Type[0].∀l: list A.
1212  ∀B: ∀prefix:Propify (list A). is_pprefix ? prefix l → Type[0].
1213  (∀prefix,proof. B prefix proof → ∀x,proof'. B (app … prefix [x]) proof') →
1214   B (mk_Propify … [ ]) (nil_pprefix …) → B (mk_Propify … l) (pprefix_reflexive … l).
1215 #A #l #B
1216 generalize in match (foldll A (λprefix. is_pprefix ? prefix l)) #HH
1217 
1218 
1219  #H #acc
1220 @foldll
1221  [
1222  |
1223  ]
1224 
1225 ≝ λA,B,f. foldli A B f (mk_Propify … [ ]).
1226
1227
1228(*
1229record subset (A:Type[0]) (P: A → Prop): Type[0] ≝
1230 { subset_wit:> A;
1231   subset_proof: P subset_wit
1232 }.
1233*)
1234
1235definition build_maps' ≝
1236  λpseudo_program.
1237  let 〈preamble,instr_list〉 ≝ pseudo_program in
1238  let result ≝
1239   foldll
1240    (option Identifier × pseudo_instruction)
1241    (λprefix.
1242      Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
1243       match prefix return λ_.Prop with [mk_Propify prefix ⇒ tech_pc_sigma0 〈preamble,prefix〉 ≠ None ?])
1244    (λprefix,t,i.
1245      let 〈labels, pc_costs〉 ≝ t in
1246      let 〈program_counter, costs〉 ≝ pc_costs in
1247       let 〈label, i'〉 ≝ i in
1248       let labels ≝
1249         match label with
1250         [ None ⇒ labels
1251         | Some label ⇒
1252           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
1253             insert ? ? label program_counter_bv labels
1254         ]
1255       in
1256         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
1257         [ None ⇒
1258            let dummy ≝ 〈labels,pc_costs〉 in
1259              dummy
1260         | Some construct ⇒ 〈labels, construct〉
1261         ]
1262    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 instr_list
1263  in
1264   let 〈labels, pc_costs〉 ≝ result in
1265   let 〈pc, costs〉 ≝ pc_costs in
1266    〈labels, costs〉.
1267 [
1268 | @⊥
1269 | normalize % //
1270 ]
1271qed.
1272
1273definition build_maps' ≝
1274  λpseudo_program.
1275  let 〈preamble,instr_list〉 ≝ pseudo_program in
1276  let result ≝
1277   foldl
1278    (Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
1279          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
1280           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)))
1281    (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
1282          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
1283           is_prefix ? instr_list_prefix' instr_list →
1284           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)
1285    (λt: Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
1286          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
1287           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)).
1288     λi: Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
1289          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
1290           is_prefix ? instr_list_prefix' instr_list →
1291           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ? .
1292      let 〈labels, pc_costs〉 ≝ t in
1293      let 〈program_counter, costs〉 ≝ pc_costs in
1294       let 〈label, i'〉 ≝ i in
1295       let labels ≝
1296         match label with
1297         [ None ⇒ labels
1298         | Some label ⇒
1299           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
1300             insert ? ? label program_counter_bv labels
1301         ]
1302       in
1303         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
1304         [ None ⇒
1305            let dummy ≝ 〈labels,pc_costs〉 in
1306              dummy
1307         | Some construct ⇒ 〈labels, construct〉
1308         ]
1309    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 ?(*instr_list*)
1310  in
1311   let 〈labels, pc_costs〉 ≝ result in
1312   let 〈pc, costs〉 ≝ pc_costs in
1313    〈labels, costs〉.
1314 [4: @(list_elim_rev ?
1315       (λinstr_list. list (
1316        (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
1317          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
1318           is_prefix ? instr_list_prefix' instr_list →
1319           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)))
1320       ?? instr_list) (* CSC: BAD ORDER FOR CODE EXTRACTION *)
1321      [ @[ ]
1322      | #l' #a #limage %2
1323        [ %[@a] #PREFIX #PREFIX_OK
1324        | (* CSC: EVEN WORST CODE FOR EXTRACTION: WE SHOULD STRENGTHEN
1325             THE INDUCTION HYPOTHESIS INSTEAD *)
1326          elim limage
1327           [ %1
1328           | #HD #TL #IH @(?::IH) cases HD #ELEM #K1 %[@ELEM] #K2 #K3
1329             @K1 @(prefix_of_append ???? K3)
1330           ] 
1331        ]
1332       
1333       
1334     
1335 
1336  cases t in c2 ⊢ % #t' * #LIST_PREFIX * #H1t' #H2t' #HJMt'
1337     % [@ (LIST_PREFIX @ [i])] %
1338      [ cases (sig2 … i LIST_PREFIX) #K1 #K2 @K1
1339      | (* DOABLE IN PRINCIPLE *)
1340      ]
1341 | (* assert false case *)
1342 |3: % [@ ([ ])] % [2: % | (* DOABLE *)]
1343 |   
1344
1345definition assembly_specification:
1346  ∀assembly_program: pseudo_assembly_program.
1347  ∀code_mem: BitVectorTrie Byte 16. Prop ≝
1348  λpseudo_assembly_program.
1349  λcode_mem.
1350    ∀pc: Word.
1351      let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in
1352      let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in
1353      let labels ≝ λx. sigma' pseudo_assembly_program (address_of_word_labels_code_mem instr_list x) in
1354      let datalabels ≝ λx. sigma' pseudo_assembly_program (lookup ? ? x (construct_datalabels preamble) (zero ?)) in
1355      let pre_assembled ≝ assembly_1_pseudoinstruction pseudo_assembly_program
1356       (sigma' pseudo_assembly_program pc) labels datalabels pre_instr in
1357      match pre_assembled with
1358       [ None ⇒ True
1359       | Some pc_code ⇒
1360          let 〈new_pc,code〉 ≝ pc_code in
1361           encoding_check code_mem pc (sigma' pseudo_assembly_program pre_new_pc) code ].
1362
1363axiom assembly_meets_specification:
1364  ∀pseudo_assembly_program.
1365    match assembly pseudo_assembly_program with
1366    [ None ⇒ True
1367    | Some code_mem_cost ⇒
1368      let 〈code_mem, cost〉 ≝ code_mem_cost in
1369        assembly_specification pseudo_assembly_program (load_code_memory code_mem)
1370    ].
1371(*
1372  # PROGRAM
1373  [ cases PROGRAM
1374    # PREAMBLE
1375    # INSTR_LIST
1376    elim INSTR_LIST
1377    [ whd
1378      whd in ⊢ (∀_. %)
1379      # PC
1380      whd
1381    | # INSTR
1382      # INSTR_LIST_TL
1383      # H
1384      whd
1385      whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
1386    ]
1387  | cases not_implemented
1388  ] *)
1389
1390definition status_of_pseudo_status: PseudoStatus → option Status ≝
1391 λps.
1392  let pap ≝ code_memory … ps in
1393   match assembly pap with
1394    [ None ⇒ None …
1395    | Some p ⇒
1396       let cm ≝ load_code_memory (\fst p) in
1397       let pc ≝ sigma' pap (program_counter ? ps) in
1398        Some …
1399         (mk_PreStatus (BitVectorTrie Byte 16)
1400           cm
1401           (low_internal_ram … ps)
1402           (high_internal_ram … ps)
1403           (external_ram … ps)
1404           pc
1405           (special_function_registers_8051 … ps)
1406           (special_function_registers_8052 … ps)
1407           (p1_latch … ps)
1408           (p3_latch … ps)
1409           (clock … ps)) ].
1410
1411definition write_at_stack_pointer':
1412 ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝
1413  λM: Type[0].
1414  λs: PreStatus M.
1415  λv: Byte.
1416    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
1417    let bit_zero ≝ get_index_v… nu O ? in
1418    let bit_1 ≝ get_index_v… nu 1 ? in
1419    let bit_2 ≝ get_index_v… nu 2 ? in
1420    let bit_3 ≝ get_index_v… nu 3 ? in
1421      if bit_zero then
1422        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
1423                              v (low_internal_ram ? s) in
1424          set_low_internal_ram ? s memory
1425      else
1426        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
1427                              v (high_internal_ram ? s) in
1428          set_high_internal_ram ? s memory.
1429  [ cases l0 %
1430  |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ]
1431qed.
1432
1433definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus.
1434 Σps':PseudoStatus.(code_memory … ps = code_memory … ps')
1435
1436  λticks_of.
1437  λs.
1438  let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in
1439  let ticks ≝ ticks_of (program_counter ? s) in
1440  let s ≝ set_clock ? s (clock ? s + ticks) in
1441  let s ≝ set_program_counter ? s pc in
1442    match instr with
1443    [ Instruction instr ⇒
1444       execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s
1445    | Comment cmt ⇒ s
1446    | Cost cst ⇒ s
1447    | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp)
1448    | Call call ⇒
1449      let a ≝ address_of_word_labels s call in
1450      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
1451      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
1452      let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in
1453      let s ≝ write_at_stack_pointer' ? s pc_bl in
1454      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
1455      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
1456      let s ≝ write_at_stack_pointer' ? s pc_bu in
1457        set_program_counter ? s a
1458    | Mov dptr ident ⇒
1459       set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr
1460    ].
1461 [
1462 |2,3,4: %
1463 | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) %
1464 |
1465 | %
1466 ]
1467 cases not_implemented
1468qed.
1469
1470(*
1471lemma execute_code_memory_unchanged:
1472 ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps).
1473 #ticks #ps whd in ⊢ (??? (??%))
1474 cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps))
1475  (program_counter pseudo_assembly_program ps)) #instr #pc
1476 whd in ⊢ (??? (??%)) cases instr
1477  [ #pre cases pre
1478     [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1479       cases (split ????) #z1 #z2 %
1480     | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1481       cases (split ????) #z1 #z2 %
1482     | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
1483       cases (split ????) #z1 #z2 %
1484     | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x
1485       [ #x1 whd in ⊢ (??? (??%))
1486     | *: cases not_implemented
1487     ]
1488  | #comment %
1489  | #cost %
1490  | #label %
1491  | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%))
1492    cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2
1493    whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2
1494    whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%))
1495    (* CSC: ??? *)
1496  | #dptr #label (* CSC: ??? *)
1497  ]
1498  cases not_implemented
1499qed.
1500*)
1501
1502lemma status_of_pseudo_status_failure_depends_only_on_code_memory:
1503 ∀ps,ps': PseudoStatus.
1504  code_memory … ps = code_memory … ps' →
1505   match status_of_pseudo_status ps with
1506    [ None ⇒ status_of_pseudo_status ps' = None …
1507    | Some _ ⇒ ∃w. status_of_pseudo_status ps' = Some … w
1508    ].
1509 #ps #ps' #H whd in ⊢ (mat
1510 ch % with [ _ ⇒ ? | _ ⇒ ? ])
1511 generalize in match (refl … (assembly (code_memory … ps)))
1512 cases (assembly ?) in ⊢ (???% → %)
1513  [ #K whd whd in ⊢ (??%?) <H >K %
1514  | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ]
1515qed.*)
1516
1517let rec encoding_check' (code_memory: BitVectorTrie Byte 16) (pc: Word) (encoding: list Byte) on encoding: Prop ≝
1518  match encoding with
1519  [ nil ⇒ True
1520  | cons hd tl ⇒
1521    let 〈new_pc, byte〉 ≝ next code_memory pc in
1522      hd = byte ∧ encoding_check' code_memory new_pc tl
1523  ].
1524
1525(* prove later *)
1526axiom test:
1527  ∀pc: Word.
1528  ∀code_memory: BitVectorTrie Byte 16.
1529  ∀i: instruction.
1530    let assembled ≝ assembly1 i in
1531      encoding_check' code_memory pc assembled →
1532        let 〈instr_pc, ignore〉 ≝ fetch code_memory pc in
1533        let 〈instr, pc〉 ≝ instr_pc in
1534          instr = i.
1535 
1536lemma main_thm:
1537 ∀ticks_of.
1538 ∀ps: PseudoStatus.
1539  match status_of_pseudo_status ps with [ None ⇒ True | Some s ⇒
1540  let ps' ≝ execute_1_pseudo_instruction ticks_of ps in
1541  match status_of_pseudo_status ps' with [ None ⇒ True | Some s'' ⇒
1542  let s' ≝ execute_1 s in
1543   s = s'']].
1544 #ticks_of #ps
1545 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
1546 cases (assembly (code_memory pseudo_assembly_program ps)) [%] * #cm #costs whd
1547 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
1548 generalize in match (sig2 … (execute_1_pseudo_instruction' ticks_of ps))
1549 
1550 cases (status_of_pseudo_status (execute_1_pseudo_instruction ticks_of ps)) [%] #s'' whd
Note: See TracBrowser for help on using the repository browser.