source: src/ASM/AssemblyProof.ma @ 848

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

...

File size: 17.7 KB
Line 
1include "ASM/Assembly.ma".
2include "ASM/Interpret.ma".
3
4(* RUSSEL **)
5
6include "basics/jmeq.ma".
7
8definition inject : ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ λA,P,a,p. dp … a p.
9definition eject : ∀A.∀P: A → Prop.(Σx:A.P x) → A ≝ λA,P,c.match c with [ dp w p ⇒ w].
10
11coercion inject nocomposites: ∀A.∀P:A → Prop.∀a.∀p:P a.Σx:A.P x ≝ inject on a:? to Σx:?.?.
12coercion eject nocomposites: ∀A.∀P:A → Prop.∀c:Σx:A.P x.A ≝ eject on _c:Σx:?.? to ?.
13
14axiom VOID: Type[0].
15axiom assert_false: VOID.
16definition bigbang: ∀A:Type[0].False → VOID → A.
17 #A #abs cases abs
18qed.
19
20coercion bigbang nocomposites: ∀A:Type[0].False → ∀v:VOID.A ≝ bigbang on _v:VOID to ?.
21
22lemma sig2: ∀A.∀P:A → Prop. ∀p:Σx:A.P x. P (eject … p).
23 #A #P #p cases p #w #q @q
24qed.
25
26(* END RUSSELL **)
27
28(* This establishes the correspondence between pseudo program counters and
29   program counters. It is at the heart of the proof. *)
30(*CSC: code taken from build_maps *)
31definition sigma0: pseudo_assembly_program → option (nat × (nat × (BitVectorTrie Word 16))) ≝
32 λinstr_list.
33  foldl ??
34    (λt. λi.
35       match t with
36       [ None ⇒ None ?
37       | Some ppc_pc_map ⇒
38         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
39         let 〈program_counter, sigma_map〉 ≝ pc_map in
40         let 〈label, i〉 ≝ i in
41          match construct_costs instr_list program_counter (λx. zero ?) (λx. zero ?) (Stub …) i with
42           [ None ⇒ None ?
43           | Some pc_ignore ⇒
44              let 〈pc,ignore〉 ≝ pc_ignore in
45              Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ]
46       ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (\snd instr_list).
47       
48definition tech_pc_sigma0: pseudo_assembly_program → option nat ≝
49 λinstr_list.
50  match sigma0 instr_list with
51   [ None ⇒ None …
52   | Some result ⇒
53      let 〈ppc,pc_sigma_map〉 ≝ result in
54      let 〈pc, sigma_map〉 ≝ pc_sigma_map in
55       Some … pc ].
56
57definition sigma_safe: pseudo_assembly_program → option (Word → Word) ≝       
58 λinstr_list.
59  match sigma0 instr_list with
60  [ None ⇒ None ?
61  | Some result ⇒
62    let 〈ppc,pc_sigma_map〉 ≝ result in
63    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
64      if gtb pc (2^16) then
65        None ?
66      else
67        Some ? (λx.lookup ?? x sigma_map (zero …)) ].
68
69axiom policy_ok: ∀p. sigma_safe p ≠ None ….
70
71definition sigma: pseudo_assembly_program → Word → Word ≝
72 λp.
73  match sigma_safe p return λr:option (Word → Word). r ≠ None … → Word → Word with
74   [ None ⇒ λabs. ⊥
75   | Some r ⇒ λ_.r] (policy_ok p).
76 cases abs //
77qed.
78
79(*
80definition build_maps' ≝
81  λpseudo_program.
82  let 〈preamble,instr_list〉 ≝ pseudo_program in
83  let result ≝
84   foldl
85    ((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16)))
86    (option Identifier × pseudo_instruction)
87    (λt,i.
88      let 〈labels, pc_costs〉 ≝ t in
89      let 〈program_counter, costs〉 ≝ pc_costs in
90       let 〈label, i'〉 ≝ i in
91       let labels ≝
92         match label with
93         [ None ⇒ labels
94         | Some label ⇒
95           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
96             insert ? ? label program_counter_bv labels
97         ]
98       in
99         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
100         [ None ⇒
101            let dummy ≝ 〈labels,pc_costs〉 in
102             dummy
103         | Some construct ⇒ 〈labels, construct〉
104         ]
105    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 instr_list
106  in
107   let 〈labels, pc_costs〉 ≝ result in
108   let 〈pc, costs〉 ≝ pc_costs in
109    〈labels, costs〉.
110
111(*
112notation < "hvbox('let' 〈ident x,ident y〉 ≝ t 'in' s)"
113 with precedence 10
114for @{ match $t with [ pair ${ident x} ${ident y} ⇒ $s ] }.
115*)
116
117lemma build_maps_ok:
118 ∀p:pseudo_assembly_program.
119  let 〈labels,costs〉 ≝ build_maps' p in
120   ∀pc.
121    (nat_of_bitvector … pc) < length … (\snd p) →
122     lookup ?? pc labels (zero …) = sigma p (\snd (fetch_pseudo_instruction (\snd p) pc)).
123 #p cases p #preamble #instr_list
124  elim instr_list
125   [ whd #pc #abs normalize in abs; cases (not_le_Sn_O ?) [#H cases (H abs) ]
126   | #hd #tl #IH
127    whd in ⊢ (match % with [ _ ⇒ ?])
128   ]
129qed.
130*)
131
132(*
133lemma list_elim_rev:
134 ∀A:Type[0].∀P:list A → Prop.
135  P [ ] → (∀n,l. length l = n → P l → 
136  P [ ] → (∀l,a. P l → P (l@[a])) →
137   ∀l. P l.
138 #A #P
139qed.*)
140
141lemma length_append:
142 ∀A.∀l1,l2:list A.
143  |l1 @ l2| = |l1| + |l2|.
144 #A #l1 elim l1
145  [ //
146  | #hd #tl #IH #l2 normalize <IH //]
147qed.
148
149lemma rev_preserves_length:
150 ∀A.∀l. length … (rev A l) = length … l.
151  #A #l elim l
152   [ %
153   | #hd #tl #IH normalize >length_append normalize /2/ ]
154qed.
155
156lemma rev_append:
157 ∀A.∀l1,l2.
158  rev A (l1@l2) = rev A l2 @ rev A l1.
159 #A #l1 elim l1 normalize //
160qed.
161 
162lemma rev_rev: ∀A.∀l. rev … (rev A l) = l.
163 #A #l elim l
164  [ //
165  | #hd #tl #IH normalize >rev_append normalize // ]
166qed.
167
168lemma split_len_Sn:
169 ∀A:Type[0].∀l:list A.∀len.
170  length … l = S len →
171   Σl'.Σa. l = l'@[a] ∧ length … l' = len.
172 #A #l elim l
173  [ normalize #len #abs destruct
174  | #hd #tl #IH #len
175    generalize in match (rev_rev … tl)
176    cases (rev A tl) in ⊢ (??%? → ?)
177     [ #H <H normalize #EQ % [@[ ]] % [@hd] normalize /2/ 
178     | #a #l' #H <H normalize #EQ
179      %[@(hd::rev … l')] %[@a] % //
180      >length_append in EQ #EQ normalize in EQ; normalize;
181      generalize in match (injective_S … EQ) #EQ2 /2/ ]]
182qed.
183
184lemma list_elim_rev:
185 ∀A:Type[0].∀P:list A → Type[0].
186  P [ ] → (∀l,a. P l → P (l@[a])) →
187   ∀l. P l.
188 #A #P #H1 #H2 #l
189 generalize in match (refl … (length … l))
190 generalize in ⊢ (???% → ?) #n generalize in match l
191 elim n
192  [ #L cases L [ // | #x #w #abs (normalize in abs) @⊥ // ]
193  | #m #IH #L #EQ
194    cases (split_len_Sn … EQ) #l' * #a * /3/ ]
195qed.
196
197axiom is_prefix: ∀A:Type[0]. list A → list A → Prop.
198axiom prefix_of_append:
199 ∀A:Type[0].∀l,l1,l2:list A.
200  is_prefix … l l1 → is_prefix … l (l1@l2).
201
202definition build_maps' ≝
203  λpseudo_program.
204  let 〈preamble,instr_list〉 ≝ pseudo_program in
205  let result ≝
206   foldl
207    (Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
208          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
209           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)))
210    (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
211          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
212           is_prefix ? instr_list_prefix' instr_list →
213           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)
214    (λt: Σt:((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16))).
215          ∃instr_list_prefix. is_prefix ? instr_list_prefix instr_list ∧
216           tech_pc_sigma0 〈preamble,instr_list_prefix〉 = Some ? (\fst (\snd t)).
217     λi: Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
218          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
219           is_prefix ? instr_list_prefix' instr_list →
220           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ? .
221      let 〈labels, pc_costs〉 ≝ t in
222      let 〈program_counter, costs〉 ≝ pc_costs in
223       let 〈label, i'〉 ≝ i in
224       let labels ≝
225         match label with
226         [ None ⇒ labels
227         | Some label ⇒
228           let program_counter_bv ≝ bitvector_of_nat ? program_counter in
229             insert ? ? label program_counter_bv labels
230         ]
231       in
232         match construct_costs pseudo_program program_counter (λx. zero ?) (λx. zero ?) costs i' with
233         [ None ⇒
234            let dummy ≝ 〈labels,pc_costs〉 in
235              dummy
236         | Some construct ⇒ 〈labels, construct〉
237         ]
238    ) 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉 ?(*instr_list*)
239  in
240   let 〈labels, pc_costs〉 ≝ result in
241   let 〈pc, costs〉 ≝ pc_costs in
242    〈labels, costs〉.
243 [4: @(list_elim_rev ?
244       (λinstr_list. list (
245        (Σi:option Identifier × pseudo_instruction. ∀instr_list_prefix.
246          let instr_list_prefix' ≝ instr_list_prefix @ [i] in
247           is_prefix ? instr_list_prefix' instr_list →
248           tech_pc_sigma0 〈preamble,instr_list_prefix'〉 ≠ None ?)))
249       ?? instr_list) (* CSC: BAD ORDER FOR CODE EXTRACTION *)
250      [ @[ ]
251      | #l' #a #limage %2
252        [ %[@a] #PREFIX #PREFIX_OK
253        | (* CSC: EVEN WORST CODE FOR EXTRACTION: WE SHOULD STRENGTHEN
254             THE INDUCTION HYPOTHESIS INSTEAD *)
255          elim limage
256           [ %1
257           | #HD #TL #IH @(?::IH) cases HD #ELEM #K1 %[@ELEM] #K2 #K3
258             @K1 @(prefix_of_append ???? K3)
259           ] 
260        ]
261       
262       
263     
264 
265  cases t in c2 ⊢ % #t' * #LIST_PREFIX * #H1t' #H2t' #HJMt'
266     % [@ (LIST_PREFIX @ [i])] %
267      [ cases (sig2 … i LIST_PREFIX) #K1 #K2 @K1
268      | (* DOABLE IN PRINCIPLE *)
269      ]
270 | (* assert false case *)
271 |3: % [@ ([ ])] % [2: % | (* DOABLE *)]
272 |   
273
274let rec encoding_check (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word)
275                       (encoding: list Byte) on encoding: Prop ≝
276  match encoding with
277  [ nil ⇒ final_pc = pc
278  | cons hd tl ⇒
279    let 〈new_pc, byte〉 ≝ next code_memory pc in
280      hd = byte ∧ encoding_check code_memory new_pc final_pc tl
281  ].
282
283definition assembly_specification:
284  ∀assembly_program: pseudo_assembly_program.
285  ∀code_mem: BitVectorTrie Byte 16. Prop ≝
286  λpseudo_assembly_program.
287  λcode_mem.
288    ∀pc: Word.
289      let 〈preamble, instr_list〉 ≝ pseudo_assembly_program in
290      let 〈pre_instr, pre_new_pc〉 ≝ fetch_pseudo_instruction instr_list pc in
291      let labels ≝ λx. sigma' pseudo_assembly_program (address_of_word_labels_code_mem instr_list x) in
292      let datalabels ≝ λx. sigma' pseudo_assembly_program (lookup ? ? x (construct_datalabels preamble) (zero ?)) in
293      let pre_assembled ≝ assembly_1_pseudoinstruction pseudo_assembly_program
294       (sigma' pseudo_assembly_program pc) labels datalabels pre_instr in
295      match pre_assembled with
296       [ None ⇒ True
297       | Some pc_code ⇒
298          let 〈new_pc,code〉 ≝ pc_code in
299           encoding_check code_mem pc (sigma' pseudo_assembly_program pre_new_pc) code ].
300
301axiom assembly_meets_specification:
302  ∀pseudo_assembly_program.
303    match assembly pseudo_assembly_program with
304    [ None ⇒ True
305    | Some code_mem_cost ⇒
306      let 〈code_mem, cost〉 ≝ code_mem_cost in
307        assembly_specification pseudo_assembly_program (load_code_memory code_mem)
308    ].
309(*
310  # PROGRAM
311  [ cases PROGRAM
312    # PREAMBLE
313    # INSTR_LIST
314    elim INSTR_LIST
315    [ whd
316      whd in ⊢ (∀_. %)
317      # PC
318      whd
319    | # INSTR
320      # INSTR_LIST_TL
321      # H
322      whd
323      whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ?])
324    ]
325  | cases not_implemented
326  ] *)
327
328definition status_of_pseudo_status: PseudoStatus → option Status ≝
329 λps.
330  let pap ≝ code_memory … ps in
331   match assembly pap with
332    [ None ⇒ None …
333    | Some p ⇒
334       let cm ≝ load_code_memory (\fst p) in
335       let pc ≝ sigma' pap (program_counter ? ps) in
336        Some …
337         (mk_PreStatus (BitVectorTrie Byte 16)
338           cm
339           (low_internal_ram … ps)
340           (high_internal_ram … ps)
341           (external_ram … ps)
342           pc
343           (special_function_registers_8051 … ps)
344           (special_function_registers_8052 … ps)
345           (p1_latch … ps)
346           (p3_latch … ps)
347           (clock … ps)) ].
348
349definition write_at_stack_pointer':
350 ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝
351  λM: Type[0].
352  λs: PreStatus M.
353  λv: Byte.
354    let 〈 nu, nl 〉 ≝ split … 4 4 (get_8051_sfr ? s SFR_SP) in
355    let bit_zero ≝ get_index_v… nu O ? in
356    let bit_1 ≝ get_index_v… nu 1 ? in
357    let bit_2 ≝ get_index_v… nu 2 ? in
358    let bit_3 ≝ get_index_v… nu 3 ? in
359      if bit_zero then
360        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
361                              v (low_internal_ram ? s) in
362          set_low_internal_ram ? s memory
363      else
364        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
365                              v (high_internal_ram ? s) in
366          set_high_internal_ram ? s memory.
367  [ cases l0 %
368  |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ]
369qed.
370
371definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus.
372 Σps':PseudoStatus.(code_memory … ps = code_memory … ps')
373
374  λticks_of.
375  λs.
376  let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in
377  let ticks ≝ ticks_of (program_counter ? s) in
378  let s ≝ set_clock ? s (clock ? s + ticks) in
379  let s ≝ set_program_counter ? s pc in
380    match instr with
381    [ Instruction instr ⇒
382       execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s
383    | Comment cmt ⇒ s
384    | Cost cst ⇒ s
385    | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp)
386    | Call call ⇒
387      let a ≝ address_of_word_labels s call in
388      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
389      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
390      let 〈pc_bu, pc_bl〉 ≝ split ? 8 8 (program_counter ? s) in
391      let s ≝ write_at_stack_pointer' ? s pc_bl in
392      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
393      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
394      let s ≝ write_at_stack_pointer' ? s pc_bu in
395        set_program_counter ? s a
396    | Mov dptr ident ⇒
397       set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr
398    ].
399 [
400 |2,3,4: %
401 | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) %
402 |
403 | %
404 ]
405 cases not_implemented
406qed.
407
408(*
409lemma execute_code_memory_unchanged:
410 ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps).
411 #ticks #ps whd in ⊢ (??? (??%))
412 cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps))
413  (program_counter pseudo_assembly_program ps)) #instr #pc
414 whd in ⊢ (??? (??%)) cases instr
415  [ #pre cases pre
416     [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
417       cases (split ????) #z1 #z2 %
418     | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
419       cases (split ????) #z1 #z2 %
420     | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
421       cases (split ????) #z1 #z2 %
422     | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x
423       [ #x1 whd in ⊢ (??? (??%))
424     | *: cases not_implemented
425     ]
426  | #comment %
427  | #cost %
428  | #label %
429  | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%))
430    cases (split ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2
431    whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (split ????) #w1 #w2
432    whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%))
433    (* CSC: ??? *)
434  | #dptr #label (* CSC: ??? *)
435  ]
436  cases not_implemented
437qed.
438*)
439
440lemma status_of_pseudo_status_failure_depends_only_on_code_memory:
441 ∀ps,ps': PseudoStatus.
442  code_memory … ps = code_memory … ps' →
443   match status_of_pseudo_status ps with
444    [ None ⇒ status_of_pseudo_status ps' = None …
445    | Some _ ⇒ ∃w. status_of_pseudo_status ps' = Some … w
446    ].
447 #ps #ps' #H whd in ⊢ (mat
448 ch % with [ _ ⇒ ? | _ ⇒ ? ])
449 generalize in match (refl … (assembly (code_memory … ps)))
450 cases (assembly ?) in ⊢ (???% → %)
451  [ #K whd whd in ⊢ (??%?) <H >K %
452  | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ]
453qed.*)
454
455let rec encoding_check' (code_memory: BitVectorTrie Byte 16) (pc: Word) (encoding: list Byte) on encoding: Prop ≝
456  match encoding with
457  [ nil ⇒ True
458  | cons hd tl ⇒
459    let 〈new_pc, byte〉 ≝ next code_memory pc in
460      hd = byte ∧ encoding_check' code_memory new_pc tl
461  ].
462
463lemma test:
464  ∀i: instruction.
465  ∃pc.
466  let assembled ≝ assembly1 i in
467  let code_memory ≝ load_code_memory assembled in
468  let fetched ≝ fetch code_memory pc in
469  let 〈instr_pc, ticks〉 ≝ fetched in
470    \fst instr_pc = i.
471  # INSTR
472  %
473  [ @ (zero 16)
474  | cases INSTR
475  ].
476
477lemma test:
478  ∀pc: Word.
479  ∀code_memory: BitVectorTrie Byte 16.
480  ∀i: instruction.
481    let assembled ≝ assembly1 i in
482      encoding_check' code_memory pc assembled →
483        let 〈instr_pc, ignore〉 ≝ fetch code_memory pc in
484        let 〈instr, pc〉 ≝ instr_pc in
485          instr = i.
486  # PC # CODE_MEMORY # INSTRUCTION
487  whd
488  whd in ⊢ (? → match % with [ _ ⇒ ? ])
489  cases (next CODE_MEMORY PC)
490  whd
491  # PC1 # V1
492
493
494  cases (fetch CODE_MEMORY PC)
495  # INSTR_PC
496  # TICKS
497  cases (INSTRUCTION)
498  [ # ADDR11
499    # ENCODING_CHECK
500    whd in ENCODING_CHECK;
501    normalize
502    cases (INSTR_PC)
503    # INSTR
504    # PC
505    normalize
506   
507   
508  # ENCODING
509  normalize
510  [ cases INSTR_PC
511    # NEW_INSTRUCTION
512    # PC
513    normalize
514    normalize in ENCODING;
515  | # ASSEMBLED
516    whd
517 
518lemma main_thm:
519 ∀ticks_of.
520 ∀ps: PseudoStatus.
521  match status_of_pseudo_status ps with [ None ⇒ True | Some s ⇒
522  let ps' ≝ execute_1_pseudo_instruction ticks_of ps in
523  match status_of_pseudo_status ps' with [ None ⇒ True | Some s'' ⇒
524  let s' ≝ execute_1 s in
525   s = s'']].
526 #ticks_of #ps
527 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
528 cases (assembly (code_memory pseudo_assembly_program ps)) [%] * #cm #costs whd
529 whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
530 generalize in match (sig2 … (execute_1_pseudo_instruction' ticks_of ps))
531 
532 cases (status_of_pseudo_status (execute_1_pseudo_instruction ticks_of ps)) [%] #s'' whd
Note: See TracBrowser for help on using the repository browser.