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

Last change on this file since 872 was 872, checked in by mulligan, 9 years ago

changes from today, need investigation of reduction machine

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