source: src/ASM/AssemblyProof.ma @ 2136

Last change on this file since 2136 was 2136, checked in by sacerdot, 8 years ago

...

File size: 42.4 KB
Line 
1include "ASM/Assembly.ma".
2include "ASM/Interpret.ma".
3include "ASM/StatusProofs.ma".
4include alias "arithmetics/nat.ma".
5
6let rec encoding_check
7  (code_memory: BitVectorTrie Byte 16) (pc: Word) (final_pc: Word)
8    (encoding: list Byte)
9      on encoding: Prop ≝
10  match encoding with
11  [ nil ⇒ final_pc = pc
12  | cons hd tl ⇒
13    let 〈new_pc, byte〉 ≝ next code_memory pc in
14      hd = byte ∧ encoding_check code_memory new_pc final_pc tl
15  ].
16
17lemma encoding_check_append:
18  ∀code_memory: BitVectorTrie Byte 16.
19  ∀final_pc: Word.
20  ∀l1: list Byte.
21  ∀pc: Word.
22  ∀l2: list Byte.
23    encoding_check code_memory pc final_pc (l1@l2) →
24      let pc_plus_len ≝ add … pc (bitvector_of_nat … (length … l1)) in
25        encoding_check code_memory pc pc_plus_len l1 ∧
26          encoding_check code_memory pc_plus_len final_pc l2.
27  #code_memory #final_pc #l1 elim l1
28  [1:
29    #pc #l2
30    whd in ⊢ (????% → ?); #H
31    <add_zero
32    whd whd in ⊢ (?%?); /2/
33  |2:
34    #hd #tl #IH #pc #l2 * #H1 #H2
35(*    >add_SO in H2; #H2 *)
36    cases (IH … H2) #E1 #E2 %
37    [1:
38      % try @H1
39      <(add_bitvector_of_nat_Sm 16 (|tl|)) in E1;
40      <add_associative #assm assumption
41    |2:
42      <add_associative in E2;
43      <(add_bitvector_of_nat_Sm 16 (|tl|)) #assm
44      assumption
45    ]
46  ]
47qed.
48
49definition ticks_of_instruction ≝
50  λi.
51    let trivial_code_memory ≝ assembly1 i in
52    let trivial_status ≝ load_code_memory trivial_code_memory in
53      \snd (fetch trivial_status (zero ?)).
54
55lemma fetch_assembly:
56  ∀pc: Word.
57  ∀i: instruction.
58  ∀code_memory: BitVectorTrie Byte 16.
59  ∀assembled: list Byte.
60    assembled = assembly1 i →
61      let len ≝ length … assembled in
62      let pc_plus_len ≝ add … pc (bitvector_of_nat … len) in
63        encoding_check code_memory pc pc_plus_len assembled →
64          let 〈instr, pc', ticks〉 ≝ fetch code_memory pc in
65           (eq_instruction instr i ∧ eqb ticks (ticks_of_instruction instr) ∧ eq_bv … pc' pc_plus_len) = true.
66  cases daemon
67(* XXX: commented out as takes ages to typecheck
68  #pc #i #code_memory #assembled cases i [8: *]
69 [16,20,29: * * |18,19: * * [1,2,4,5: *] |28: * * [1,2: * [1,2: * [1,2: * [1,2: *]]]]]
70 [47,48,49:
71 |*: #arg @(subaddressing_mode_elim … arg)
72  [2,3,5,7,10,12,16,17,18,21,26,27,28,31,32,33,37,38,39,40,41,42,43,44,45,48,51,58,
73   59,60,63,64,65,66,67: #ARG]]
74 [4,5,6,7,8,9,10,11,12,13,22,23,24,27,28,39,40,41,42,43,44,45,46,47,48,49,50,51,52,
75  56,57,69,70,72: #arg2 @(subaddressing_mode_elim … arg2)
76  [1,2,4,7,9,11,12,14,15,17,18,19,20,22,23,24,25,26,27,28,29,30,31,32,33,36,37,38,
77   39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,
78   68,69,70,71: #ARG2]]
79 [1,2,19,20: #arg3 @(subaddressing_mode_elim … arg3) #ARG3]
80 normalize in ⊢ (???% → ?);
81 [92,94,42,93,95: @vsplit_elim #vl #vm #E >E -E; [2,4: @(bitvector_3_elim_prop … vl)]
82  normalize in ⊢ (???% → ?);]
83 #H >H * #H1 try (whd in ⊢ (% → ?); * #H2)
84 try (whd in ⊢ (% → ?); * #H3) whd in ⊢ (% → ?); #H4
85 whd in match fetch; normalize nodelta <H1 whd in ⊢ (match % with [ _ ⇒ ? ]);
86 [17,18,19,20,21,22,23,24,25,26,32,34,35,36,37,39: <H3]
87 try <H2 whd >eq_instruction_refl >H4 @eq_bv_refl *)
88qed.
89
90let rec fetch_many
91  (code_memory: BitVectorTrie Byte 16) (final_pc: Word) (pc: Word)
92    (expected: list instruction)
93      on expected: Prop ≝
94  match expected with
95  [ nil ⇒ eq_bv … pc final_pc = true
96  | cons i tl ⇒
97    let pc' ≝ add 16 pc (bitvector_of_nat 16 (|assembly1 … i|)) in
98      (〈i, pc', ticks_of_instruction i〉 = fetch code_memory pc ∧
99        fetch_many code_memory final_pc pc' tl)
100  ].
101         
102lemma fetch_assembly_pseudo':
103  ∀lookup_labels.
104  ∀sigma: Word → Word.
105  ∀policy: Word → bool.
106  ∀ppc.
107  ∀lookup_datalabels.
108  ∀pi.
109  ∀code_memory.
110  ∀len.
111  ∀assembled.
112  ∀instructions.
113    let pc ≝ sigma ppc in
114      instructions = expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels pi →
115        〈len,assembled〉 = assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi →
116          let pc_plus_len ≝ add … pc (bitvector_of_nat … len) in
117            encoding_check code_memory pc pc_plus_len assembled →
118              fetch_many code_memory pc_plus_len pc instructions.
119  #lookup_labels #sigma #policy #ppc #lookup_datalabels #pi #code_memory #len #assembled #instructions
120  normalize nodelta #instructions_refl whd in ⊢ (???% → ?); <instructions_refl whd in ⊢ (???% → ?); #assembled_refl
121  cases (pair_destruct ?????? assembled_refl) -assembled_refl #len_refl #assembled_refl
122  >len_refl >assembled_refl -len_refl
123  generalize in match (add 16 (sigma ppc)
124    (bitvector_of_nat 16
125     (|flatten (Vector bool 8)
126       (map instruction (list (Vector bool 8)) assembly1 instructions)|)));
127  #final_pc
128  generalize in match (sigma ppc); elim instructions
129  [1:
130    #pc whd in ⊢ (% → %); #H >H @eq_bv_refl
131  |2:
132    #i #tl #IH #pc #H whd
133    cases (encoding_check_append ????? H) -H #H1 #H2
134    lapply (fetch_assembly pc i code_memory (assembly1 i) (refl …)) whd in ⊢ (% → ?);   
135    cases (fetch ??) * #instr #pc' #ticks
136    #H3 lapply (H3 H1) -H3 normalize nodelta #H3
137    lapply (conjunction_true ?? H3) * #H4 #H5
138    lapply (conjunction_true … H4) * #B1 #B2
139    >(eq_instruction_to_eq … B1) >(eq_bv_eq … H5) %
140    >(eqb_true_to_refl … B2) >(eq_instruction_to_eq … B1) try % @IH @H2
141  ]
142qed.
143
144lemma fetch_assembly_pseudo:
145  ∀program: pseudo_assembly_program.
146  ∀sigma: Word → Word.
147  ∀policy: Word → bool.
148  let 〈labels, costs〉 ≝ create_label_cost_map (\snd program) in
149  let lookup_labels ≝ λx. bitvector_of_nat 16 (lookup_def … labels x 0) in
150  ∀ppc.∀ppc_ok.
151  ∀code_memory.
152  let lookup_datalabels ≝ λx:Identifier. lookup_def … (construct_datalabels (\fst  program)) x (zero 16) in
153  let pi ≝  \fst  (fetch_pseudo_instruction (\snd program) ppc ppc_ok) in
154  let pc ≝ sigma ppc in
155  let instructions ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels pi in
156  let 〈len,assembled〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in
157  let pc_plus_len ≝ add … pc (bitvector_of_nat … len) in
158    encoding_check code_memory pc pc_plus_len assembled →
159      fetch_many code_memory pc_plus_len pc instructions.
160  #program #sigma #policy
161  @pair_elim #labels #costs #create_label_cost_map_refl
162  letin lookup_labels ≝ (λx.?) #ppc #ppc_ok #code_memory
163  letin lookup_datalabels ≝ (λx.?)
164  letin pi ≝ (fst ???)
165  letin pc ≝ (sigma ?)
166  letin instructions ≝ (expand_pseudo_instruction ??????)
167  @pair_elim #len #assembled #assembled_refl normalize nodelta
168  #H
169  generalize in match
170   (fetch_assembly_pseudo' lookup_labels sigma policy ppc lookup_datalabels pi code_memory len assembled instructions) in ⊢ ?;
171  #X destruct normalize nodelta @X try % <assembled_refl try % assumption
172qed.
173
174definition is_present_in_machine_code_image_p: ∀pseudo_instruction. Prop ≝
175  λpseudo_instruction.
176    match pseudo_instruction with
177    [ Comment c ⇒ False
178    | Cost c ⇒ False
179    | _ ⇒ True
180    ].
181
182lemma fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels:
183 ∀lookup_labels,sigma,policy,ppc,pi.
184  ∀lookup_datalabels1,lookup_datalabels2.
185   \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels1 pi) =
186   \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels2 pi).
187#lookup_labels #sigma #policy #ppc #pi #lookup_datalabels1 #lookup_datalabels2
188cases pi //
189qed.
190
191lemma fst_snd_assembly_1_pseudoinstruction:
192 ∀lookup_labels,sigma,policy,ppc,pi,lookup_datalabels,len,assembled.
193   assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi
194   = 〈len,assembled〉 →
195    len = |assembled|.
196#lookup #sigma #policy #ppc #pi #lookup_datalabels #len #assembled
197inversion (assembly_1_pseudoinstruction ??????) #len' #assembled'
198whd in ⊢ (??%? → ?); #EQ1 #EQ2 destruct %
199qed.
200
201(* XXX: easy but tedious *)
202lemma assembly1_lt_128:
203  ∀i: instruction.
204    |(assembly1 i)| < 128.
205  cases daemon
206(* XXX: commented out as takes ages to type check
207  #i cases i
208  try (#assm1 #assm2) try #assm1
209  [8:
210    cases assm1
211    try (#assm1 #assm2) try #assm1
212    whd in match assembly1; normalize nodelta
213    whd in match assembly_preinstruction; normalize nodelta
214    try @(subaddressing_mode_elim … assm2)
215    try @(subaddressing_mode_elim … assm1) try #w try #w' normalize nodelta
216    [32:
217      cases assm1 -assm1 #assm1 normalize nodelta
218      cases assm1 #addr1 #addr2 normalize nodelta
219      [1:
220        @(subaddressing_mode_elim … addr2)
221      |2:
222        @(subaddressing_mode_elim … addr1)
223      ]
224      #w
225    |35,36,37:
226      cases assm1 -assm1 #assm1 normalize nodelta
227      [1,3:
228        cases assm1 -assm1 #assm1 normalize nodelta
229      ]
230      cases assm1 #addr1 #addr2 normalize nodelta
231      @(subaddressing_mode_elim … addr2) try #w
232    |49:
233      cases assm1 -assm1 #assm1 normalize nodelta
234      [1:
235        cases assm1 -assm1 #assm1 normalize nodelta
236        [1:
237          cases assm1 -assm1 #assm1 normalize nodelta
238          [1:
239            cases assm1 -assm1 #assm1 normalize nodelta
240            [1:
241              cases assm1 -assm1 #assm1 normalize nodelta
242            ]
243          ]
244        ]
245      ]
246      cases assm1 #addr1 #addr2 normalize nodelta
247      [1,3,4,5:
248        @(subaddressing_mode_elim … addr2) try #w
249      |*:
250        @(subaddressing_mode_elim … addr1) try #w
251        normalize nodelta
252        [1,2:
253          @(subaddressing_mode_elim … addr2) try #w
254        ]
255      ]
256    |50:
257      cases assm1 -assm1 #assm1 normalize nodelta
258      cases assm1 #addr1 #addr2 normalize nodelta
259      [1:
260        @(subaddressing_mode_elim … addr2) try #w
261      |2:
262        @(subaddressing_mode_elim … addr1) try #w
263      ]
264    ]
265    normalize repeat @le_S_S @le_O_n
266  ]
267  whd in match assembly1; normalize nodelta
268  [6:
269    normalize repeat @le_S_S @le_O_n
270  |7:
271    @(subaddressing_mode_elim … assm2) normalize repeat @le_S_S @le_O_n
272  |*:
273    @(subaddressing_mode_elim … assm1) #w normalize nodelta repeat @le_S_S @le_O_n
274  ]
275  *)
276qed.
277
278(*CSC: move elsewhere*)
279lemma flatten_singleton:
280 ∀A,a. flatten A [a] = a.
281#A #a normalize //
282qed.
283
284(*CSC: move elsewhere*)
285lemma length_flatten_cons:
286 ∀A,hd,tl.
287  |flatten A (hd::tl)| = |hd| + |flatten A tl|.
288 #A #hd #tl normalize //
289qed.
290
291lemma tech_transitive_lt_3:
292 ∀n1,n2,n3,b.
293  n1 < b → n2 < b → n3 < b →
294   n1 + n2 + n3 < 3 * b.
295 #n1 #n2 #n3 #b #H1 #H2 #H3
296 @(transitive_lt … (b + n2 + n3)) [ @monotonic_lt_plus_l @monotonic_lt_plus_l // ]
297 @(transitive_lt … (b + b + n3)) [ @monotonic_lt_plus_l @monotonic_lt_plus_r // ]
298 @(lt_to_le_to_lt … (b + b + b)) [ @monotonic_lt_plus_r // ] //
299qed.
300
301lemma assembly1_pseudoinstruction_lt_2_to_16:
302  ∀lookup_labels,sigma,policy,ppc,lookup_datalabels,pi.
303  |\snd (assembly_1_pseudoinstruction
304    lookup_labels sigma policy ppc lookup_datalabels pi)|
305   < 2^16.
306 #lookup_labels #sigma #policy #ppc #lookup_datalabels *
307[ cut (128 < 2^16) [@leb_true_to_le %] #LT
308  * whd in match (assembly_1_pseudoinstruction ??????);
309  whd in match (expand_pseudo_instruction ??????);
310  whd in match assembly_1_pseudoinstruction; normalize nodelta
311  try (#arg1 #arg2 #arg3) try (#arg1 #arg2) try #arg1
312  whd in match (expand_pseudo_instruction ??????);
313  try
314   (change with (|flatten ? [assembly1 ?]| < ?)
315    >flatten_singleton
316    @(transitive_lt … (assembly1_lt_128 ?))
317    @LT)
318  @pair_elim #x #y #_ cases x normalize nodelta
319  try
320   (change with (|flatten ? [assembly1 ?]| < ?)
321    >flatten_singleton
322    @(transitive_lt … (assembly1_lt_128 ?))
323    @LT)
324  change with (|flatten ? [assembly1 ?; assembly1 ?; assembly1 ?]| < ?)
325  >length_flatten_cons >length_flatten_cons >length_flatten_cons <plus_n_O
326  <associative_plus @(transitive_lt … (tech_transitive_lt_3 … (2^7) ???))
327  try @assembly1_lt_128 @leb_true_to_le %
328|2,3: #msg normalize in ⊢ (?%?); //
329| #label whd in match (assembly_1_pseudoinstruction ??????);
330  whd in match (expand_pseudo_instruction ??????);
331  @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_
332  [2: @pair_elim #x #y #_ cases (?∧?)]
333  normalize in ⊢ (?%?); //
334| #label whd in match (assembly_1_pseudoinstruction ??????);
335  whd in match (expand_pseudo_instruction ??????);
336  @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_
337  normalize in ⊢ (?%?); //
338| #dptr #id normalize in ⊢ (?%?); //
339]
340qed.
341
342(*CSC: move elsewhere*)         
343axiom lt_to_lt_nat_of_bitvector_add:
344 ∀n,v,m1,m2.
345  m2 < 2^n → nat_of_bitvector n v + m2 < 2^n → m1 < m2 →
346   nat_of_bitvector n (add n v (bitvector_of_nat n m1)) <
347   nat_of_bitvector n (add n v (bitvector_of_nat n m2)).
348
349(* This is a trivial consequence of fetch_assembly_pseudo + load_code_memory_ok. *)     
350lemma assembly_ok:
351  ∀program.
352  ∀length_proof: |\snd program| ≤ 2^16.
353  ∀sigma: Word → Word.
354  ∀policy: Word → bool.
355  ∀sigma_policy_witness: sigma_policy_specification program sigma policy.
356  ∀assembled.
357  ∀costs': BitVectorTrie costlabel 16.
358  let 〈preamble, instr_list〉 ≝ program in
359  let 〈labels, costs〉 ≝ create_label_cost_map instr_list in
360  let datalabels ≝ construct_datalabels preamble in
361  let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero …) in
362    〈assembled,costs'〉 = assembly program sigma policy →
363      (* costs = costs' ∧ CSC: costs != costs', costs' = sigma costs! *)
364        let code_memory ≝ load_code_memory assembled in
365        let lookup_labels ≝ λx. bitvector_of_nat 16 (lookup_def … labels x 0) in
366          ∀ppc.∀ppc_ok.
367            let 〈pi, newppc〉 ≝ fetch_pseudo_instruction (\snd program) ppc ppc_ok in     
368            let 〈len,assembled〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in
369            let pc ≝ sigma ppc in
370            let pc_plus_len ≝ add … pc (bitvector_of_nat … len) in
371              encoding_check code_memory pc pc_plus_len assembled ∧
372                  sigma newppc = add … pc (bitvector_of_nat … len).
373  #program #length_proof #sigma #policy #sigma_policy_witness #assembled #costs'
374  cases (assembly program sigma policy) * #assembled' #costs''
375  @pair_elim #preamble #instr_list #EQprogram whd in ⊢ (% → %);
376  cut (|instr_list| ≤ 2^16) [ >EQprogram in length_proof; // ] #instr_list_ok
377  #H lapply (H sigma_policy_witness instr_list_ok) -H whd in ⊢ (% → ?);
378  @pair_elim #labels #costs #create_label_cost_refl whd in ⊢ (% → %);
379  * #assembly_ok1 #assembly_ok2 #Pair_eq destruct(Pair_eq) whd
380  #ppc #ppc_ok @pair_elim #pi #newppc #eq_fetch_pseudo_instruction
381  @pair_elim #len #assembled #eq_assembly_1_pseudoinstruction whd
382  lapply (assembly_ok2 ppc ?) try // -assembly_ok2
383  >eq_fetch_pseudo_instruction
384  change with ((let 〈len0,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi in ∀j.∀H:j<|assembledi|.?) → ?)
385  >eq_assembly_1_pseudoinstruction
386  whd in ⊢ (% → ?); #assembly_ok
387  %
388  [2: >(pair_destruct_2 ????? (sym_eq … eq_fetch_pseudo_instruction))
389      >snd_fetch_pseudo_instruction
390      cases sigma_policy_witness #_ >EQprogram #H cases (H ? ppc_ok) -H
391      #spw1 #_ >spw1 -spw1 [2: @(transitive_le … ppc_ok) // ] @eq_f @eq_f
392      >eq_fetch_pseudo_instruction whd in match instruction_size;
393      normalize nodelta >create_label_cost_refl
394      >fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels
395      [>eq_assembly_1_pseudoinstruction % | skip]
396  | lapply (load_code_memory_ok assembled' ?) [ assumption ]
397    #load_code_memory_ok
398    lapply (fst_snd_assembly_1_pseudoinstruction … eq_assembly_1_pseudoinstruction) #EQlen
399    (* Nice statement here *)
400    cut (∀j. ∀H: j < |assembled|.
401          nth_safe Byte j assembled H =
402          \snd (next (load_code_memory assembled')
403          (bitvector_of_nat 16
404           (nat_of_bitvector …
405            (add … (sigma ppc) (bitvector_of_nat … j))))))
406    [1: #j #H <load_code_memory_ok
407        [ @assembly_ok
408        | cut (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat … (|assembled|))) ≤ |assembled'|)
409          [ cases sigma_policy_witness #_ #sigma_ok cases daemon
410            (*CSC: True, propagate already proved invariant*)
411          | #LE @(lt_to_le_to_lt … LE)
412            @lt_to_lt_nat_of_bitvector_add
413            [ @(eq_ind ?? (λp.λ_. |\snd p| < 2^16) ?? eq_assembly_1_pseudoinstruction)
414              / by /
415            | @(transitive_le ???? assembly_ok1) change with (? < ?) cases daemon
416              (*CSC: Same as before*)
417            | assumption
418            ]   
419          ]
420        ]
421    |2:
422      -assembly_ok -load_code_memory_ok generalize in match (sigma ppc); >EQlen -len
423      elim assembled
424      [1:
425        #pc #_ whd <add_zero %
426      | #hd #tl #IH #pc #H %
427         [ lapply (H 0 ?) [ normalize @le_S_S @le_O_n ] whd in ⊢ (??%? → ?); -H #H
428           >H -H whd in ⊢ (??%?); <add_zero //
429         | >(?: add … pc (bitvector_of_nat … (S (|tl|))) = add … (add … pc (bitvector_of_nat … 1)) (bitvector_of_nat … (|tl|)))
430           [2: <add_bitvector_of_nat_Sm @add_associative ]
431           @IH -IH #j #LTj lapply (H (S j) ?) [ @le_S_S @LTj ]
432           <(nth_safe_prepend … [hd] … LTj) #IH >IH <add_bitvector_of_nat_Sm
433           >add_associative % ]]
434  ]]
435qed.
436
437(* XXX: should we add that costs = costs'? *)
438lemma fetch_assembly_pseudo2:
439  ∀program.
440  ∀length_proof: |\snd program| ≤ 2^16.
441  ∀sigma.
442  ∀policy.
443  ∀sigma_policy_specification_witness: sigma_policy_specification program sigma policy.
444  ∀ppc.∀ppc_ok.
445  let 〈labels, costs〉 ≝ create_label_cost_map (\snd program) in
446  let 〈assembled, costs'〉 ≝ pi1 … (assembly program sigma policy) in
447  let code_memory ≝ load_code_memory assembled in
448  let data_labels ≝ construct_datalabels (\fst program) in
449  let lookup_labels ≝ λx. bitvector_of_nat 16 (lookup_def … labels x 0) in
450  let lookup_datalabels ≝ λx. lookup_def ? ? data_labels x (zero ?) in
451  let 〈pi,newppc〉 ≝ fetch_pseudo_instruction (\snd program) ppc ppc_ok in
452  let instructions ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels pi in
453    fetch_many code_memory (sigma newppc) (sigma ppc) instructions.
454  * #preamble #instr_list #length_proof #sigma #policy #sigma_policy_specification_witness #ppc #ppc_ok
455  @pair_elim #labels #costs #create_label_map_refl
456  @pair_elim #assembled #costs' #assembled_refl
457  letin code_memory ≝ (load_code_memory ?)
458  letin data_labels ≝ (construct_datalabels ?)
459  letin lookup_labels ≝ (λx. ?)
460  letin lookup_datalabels ≝ (λx. ?)
461  @pair_elim #pi #newppc #fetch_pseudo_refl
462  lapply (assembly_ok 〈preamble, instr_list〉 ? sigma policy sigma_policy_specification_witness assembled costs')
463  normalize nodelta try assumption
464  >create_label_map_refl in ⊢ (match % with [_ ⇒ ?] → ?); #H
465  lapply (H (sym_eq … assembled_refl)) -H
466  lapply (refl … (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi))
467  cases (assembly_1_pseudoinstruction ??????) in ⊢ (???% → ?);
468  #len #assembledi #EQ4 #H
469  lapply (H ppc) >fetch_pseudo_refl #H
470  lapply (fetch_assembly_pseudo 〈preamble,instr_list〉 sigma policy)
471  >create_label_map_refl #X lapply (X ppc ppc_ok (load_code_memory assembled)) -X
472  >EQ4 #H1 cases (H ppc_ok)
473  #H2 #H3 >H3 normalize nodelta in H1; normalize nodelta
474  >fetch_pseudo_refl in H1; #assm @assm assumption
475qed.
476
477(* XXX: changed this type.  Bool specifies whether byte is first or second
478        component of an address, and the Word is the pseudoaddress that it
479        corresponds to.  Second component is the same principle for the accumulator
480        A.
481*)
482definition internal_pseudo_address_map ≝ list ((BitVector 8) × (bool × Word)) × (option (bool × Word)).
483
484include alias "ASM/BitVectorTrie.ma".
485 
486include "common/AssocList.ma".
487
488axiom low_internal_ram_of_pseudo_low_internal_ram:
489 ∀M:internal_pseudo_address_map.∀ram:BitVectorTrie Byte 7.BitVectorTrie Byte 7.
490
491axiom high_internal_ram_of_pseudo_high_internal_ram:
492 ∀M:internal_pseudo_address_map.∀ram:BitVectorTrie Byte 7.BitVectorTrie Byte 7.
493
494axiom low_internal_ram_of_pseudo_internal_ram_hit:
495 ∀M:internal_pseudo_address_map.∀cm.∀s:PseudoStatus cm.∀sigma:Word → Word × bool.∀high: bool. ∀points_to: Word. ∀addr:BitVector 7.
496  assoc_list_lookup ?? (false:::addr) (eq_bv 8) (\fst M) = Some … (〈high, points_to〉)  →
497   let ram ≝ low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … s) in
498   let pbl ≝ lookup ? 7 addr (low_internal_ram … s) (zero 8) in
499   let bl ≝ lookup ? 7 addr ram (zero 8) in
500   let 〈lower, higher〉 ≝ vsplit ? 8 8 points_to in
501   let 〈plower, phigher〉 ≝ vsplit ? 8 8 (\fst (sigma points_to)) in
502     if high then
503       (pbl = higher) ∧ (bl = phigher)
504     else
505       (pbl = lower) ∧ (bl = plower).
506
507(* changed from add to sub *)
508axiom low_internal_ram_of_pseudo_internal_ram_miss:
509 ∀T.∀M:internal_pseudo_address_map.∀cm.∀s:PreStatus T cm.∀addr:BitVector 7.
510  let ram ≝ low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … s) in
511    assoc_list_exists ?? (false:::addr) (eq_bv 8) (\fst M) = false →
512      lookup ? 7 addr ram (zero ?) = lookup ? 7 addr (low_internal_ram … s) (zero ?).
513
514definition addressing_mode_ok ≝
515 λT.λM:internal_pseudo_address_map.λcm.λs:PreStatus T cm.
516  λaddr:addressing_mode.
517   match addr with
518    [ DIRECT d ⇒
519       ¬(assoc_list_exists ?? d (eq_bv 8) (\fst M)) ∧
520       ¬(assoc_list_exists ?? (\fst (sub_8_with_carry d (bitvector_of_nat 8 1) false)) (eq_bv 8) (\fst M))
521    | INDIRECT i ⇒
522       let d ≝ get_register … s [[false;false;i]] in
523       ¬(assoc_list_exists ?? d (eq_bv 8) (\fst M)) ∧
524       ¬(assoc_list_exists ?? (\fst (sub_8_with_carry d (bitvector_of_nat 8 1) false)) (eq_bv 8) (\fst M))
525    | EXT_INDIRECT _ ⇒ true
526    | REGISTER _ ⇒ true
527    | ACC_A ⇒ match \snd M with [ None ⇒ true | _ ⇒ false ]
528    | ACC_B ⇒ true
529    | DPTR ⇒ true
530    | DATA _ ⇒ true
531    | DATA16 _ ⇒ true
532    | ACC_DPTR ⇒ true
533    | ACC_PC ⇒ true
534    | EXT_INDIRECT_DPTR ⇒ true
535    | INDIRECT_DPTR ⇒ true
536    | CARRY ⇒ true
537    | BIT_ADDR _ ⇒ ¬true (* TO BE COMPLETED *)
538    | N_BIT_ADDR _ ⇒ ¬true (* TO BE COMPLETED *)
539    | RELATIVE _ ⇒ true
540    | ADDR11 _ ⇒ true
541    | ADDR16 _ ⇒ true ].
542   
543definition next_internal_pseudo_address_map0 ≝
544  λT.
545  λcm:T.
546  λaddr_of: Identifier → PreStatus T cm → Word.
547  λfetched.
548  λM: internal_pseudo_address_map.
549  λs: PreStatus T cm.
550   match fetched with
551    [ Comment _ ⇒ Some ? M
552    | Cost _ ⇒ Some … M
553    | Jmp _ ⇒ Some … M
554    | Call a ⇒
555      let a' ≝ addr_of a s in
556      let 〈callM, accM〉 ≝ M in
557         Some … 〈〈(add 8 (get_8051_sfr ?? s SFR_SP) (bitvector_of_nat 8 1)), 〈false, a'〉〉::
558                 〈(add 8 (get_8051_sfr ?? s SFR_SP) (bitvector_of_nat 8 2)), 〈true, a'〉〉::callM, accM〉
559    | Mov _ _ ⇒ Some … M
560    | Instruction instr ⇒
561       match instr with
562        [ ADD addr1 addr2 ⇒
563           if addressing_mode_ok T M … s addr1 ∧ addressing_mode_ok T M … s addr2 then
564            Some ? M
565           else
566            None ?
567        | ADDC addr1 addr2 ⇒
568           if addressing_mode_ok T M … s addr1 ∧ addressing_mode_ok T M … s addr2 then
569            Some ? M
570           else
571            None ?
572        | SUBB addr1 addr2 ⇒
573           if addressing_mode_ok T M … s addr1 ∧ addressing_mode_ok T M … s addr2 then
574            Some ? M
575           else
576            None ?       
577        | _ ⇒ (* TO BE COMPLETED *) Some ? M ]].
578
579definition next_internal_pseudo_address_map ≝
580 λM:internal_pseudo_address_map.
581 λcm.
582 λaddr_of.
583 λs:PseudoStatus cm.
584 λppc_ok.
585    next_internal_pseudo_address_map0 ? cm addr_of
586     (\fst (fetch_pseudo_instruction (\snd cm) (program_counter … s) ppc_ok)) M s.
587
588definition code_memory_of_pseudo_assembly_program:
589    ∀pap:pseudo_assembly_program.
590      (Word → Word) → (Word → bool) → BitVectorTrie Byte 16 ≝
591  λpap.
592  λsigma.
593  λpolicy.
594    let p ≝ pi1 … (assembly pap sigma policy) in
595      load_code_memory (\fst p).
596
597definition sfr_8051_of_pseudo_sfr_8051 ≝
598  λM: internal_pseudo_address_map.
599  λsfrs: Vector Byte 19.
600  λsigma: Word → Word.
601    match \snd M with
602    [ None ⇒ sfrs
603    | Some s ⇒
604      let 〈high, address〉 ≝ s in
605      let index ≝ sfr_8051_index SFR_ACC_A in
606      let 〈upper, lower〉 ≝ vsplit ? 8 8 (sigma address) in
607        if high then
608          set_index Byte 19 sfrs index upper ?
609        else
610          set_index Byte 19 sfrs index lower ?
611    ].
612  //
613qed.
614
615definition status_of_pseudo_status:
616    internal_pseudo_address_map → ∀pap. ∀ps: PseudoStatus pap.
617      ∀sigma: Word → Word. ∀policy: Word → bool.
618        Status (code_memory_of_pseudo_assembly_program pap sigma policy) ≝
619  λM.
620  λpap.
621  λps.
622  λsigma.
623  λpolicy.
624  let cm ≝ code_memory_of_pseudo_assembly_program … sigma policy in
625  let pc ≝ sigma (program_counter … ps) in
626  let lir ≝ low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … ps) in
627  let hir ≝ high_internal_ram_of_pseudo_high_internal_ram M (high_internal_ram … ps) in
628     mk_PreStatus (BitVectorTrie Byte 16)
629      cm
630      lir
631      hir
632      (external_ram … ps)
633      pc
634      (special_function_registers_8051 … ps)
635      (special_function_registers_8052 … ps)
636      (p1_latch … ps)
637      (p3_latch … ps)
638      (clock … ps).
639
640(*
641definition write_at_stack_pointer':
642 ∀M. ∀ps: PreStatus M. Byte → Σps':PreStatus M.(code_memory … ps = code_memory … ps') ≝
643  λM: Type[0].
644  λs: PreStatus M.
645  λv: Byte.
646    let 〈 nu, nl 〉 ≝ vsplit … 4 4 (get_8051_sfr ? s SFR_SP) in
647    let bit_zero ≝ get_index_v… nu O ? in
648    let bit_1 ≝ get_index_v… nu 1 ? in
649    let bit_2 ≝ get_index_v… nu 2 ? in
650    let bit_3 ≝ get_index_v… nu 3 ? in
651      if bit_zero then
652        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
653                              v (low_internal_ram ? s) in
654          set_low_internal_ram ? s memory
655      else
656        let memory ≝ insert … ([[ bit_1 ; bit_2 ; bit_3 ]] @@ nl)
657                              v (high_internal_ram ? s) in
658          set_high_internal_ram ? s memory.
659  [ cases l0 %
660  |2,3,4,5: normalize repeat (@ le_S_S) @ le_O_n ]
661qed.
662
663definition execute_1_pseudo_instruction': (Word → nat) → ∀ps:PseudoStatus.
664 Σps':PseudoStatus.(code_memory … ps = code_memory … ps')
665
666  λticks_of.
667  λs.
668  let 〈instr, pc〉 ≝ fetch_pseudo_instruction (\snd (code_memory ? s)) (program_counter ? s) in
669  let ticks ≝ ticks_of (program_counter ? s) in
670  let s ≝ set_clock ? s (clock ? s + ticks) in
671  let s ≝ set_program_counter ? s pc in
672    match instr with
673    [ Instruction instr ⇒
674       execute_1_preinstruction … (λx, y. address_of_word_labels y x) instr s
675    | Comment cmt ⇒ s
676    | Cost cst ⇒ s
677    | Jmp jmp ⇒ set_program_counter ? s (address_of_word_labels s jmp)
678    | Call call ⇒
679      let a ≝ address_of_word_labels s call in
680      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
681      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
682      let 〈pc_bu, pc_bl〉 ≝ vsplit ? 8 8 (program_counter ? s) in
683      let s ≝ write_at_stack_pointer' ? s pc_bl in
684      let 〈carry, new_sp〉 ≝ half_add ? (get_8051_sfr ? s SFR_SP) (bitvector_of_nat 8 1) in
685      let s ≝ set_8051_sfr ? s SFR_SP new_sp in
686      let s ≝ write_at_stack_pointer' ? s pc_bu in
687        set_program_counter ? s a
688    | Mov dptr ident ⇒
689       set_arg_16 ? s (get_arg_16 ? s (DATA16 (address_of_word_labels s ident))) dptr
690    ].
691 [
692 |2,3,4: %
693 | <(sig2 … l7) whd in ⊢ (??? (??%)) <(sig2 … l5) %
694 |
695 | %
696 ]
697 cases not_implemented
698qed.
699*)
700
701(*
702lemma execute_code_memory_unchanged:
703 ∀ticks_of,ps. code_memory ? ps = code_memory ? (execute_1_pseudo_instruction ticks_of ps).
704 #ticks #ps whd in ⊢ (??? (??%))
705 cases (fetch_pseudo_instruction (\snd (code_memory pseudo_assembly_program ps))
706  (program_counter pseudo_assembly_program ps)) #instr #pc
707 whd in ⊢ (??? (??%)) cases instr
708  [ #pre cases pre
709     [ #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
710       cases (vsplit ????) #z1 #z2 %
711     | #a1 #a2 whd in ⊢ (??? (??%)) cases (add_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
712       cases (vsplit ????) #z1 #z2 %
713     | #a1 #a2 whd in ⊢ (??? (??%)) cases (sub_8_with_carry ???) #y1 #y2 whd in ⊢ (??? (??%))
714       cases (vsplit ????) #z1 #z2 %
715     | #a1 whd in ⊢ (??? (??%)) cases a1 #x #H whd in ⊢ (??? (??%)) cases x
716       [ #x1 whd in ⊢ (??? (??%))
717     | *: cases not_implemented
718     ]
719  | #comment %
720  | #cost %
721  | #label %
722  | #label whd in ⊢ (??? (??%)) cases (half_add ???) #x1 #x2 whd in ⊢ (??? (??%))
723    cases (vsplit ????) #y1 #y2 whd in ⊢ (??? (??%)) cases (half_add ???) #z1 #z2
724    whd in ⊢ (??? (??%)) whd in ⊢ (??? (??%)) cases (vsplit ????) #w1 #w2
725    whd in ⊢ (??? (??%)) cases (get_index_v bool ????) whd in ⊢ (??? (??%))
726    (* CSC: ??? *)
727  | #dptr #label (* CSC: ??? *)
728  ]
729  cases not_implemented
730qed.
731*)
732
733(* DEAD CODE?
734lemma status_of_pseudo_status_failure_depends_only_on_code_memory:
735 ∀M:internal_pseudo_address_map.
736 ∀ps,ps': PseudoStatus.
737 ∀pol.
738  ∀prf:code_memory … ps = code_memory … ps'.
739   let pol' ≝ ? in
740   match status_of_pseudo_status M ps pol with
741    [ None ⇒ status_of_pseudo_status M ps' pol' = None …
742    | Some _ ⇒ ∃w. status_of_pseudo_status M ps' pol' = Some … w
743    ].
744 [2: <prf @pol]
745 #M #ps #ps' #pol #H normalize nodelta; whd in ⊢ (match % with [ _ ⇒ ? | _ ⇒ ? ])
746 generalize in match (refl … (assembly (code_memory … ps) pol))
747 cases (assembly ??) in ⊢ (???% → %)
748  [ #K whd whd in ⊢ (??%?) <H >K %
749  | #x #K whd whd in ⊢ (?? (λ_.??%?)) <H >K % [2: % ] ]
750qed.
751*)
752
753definition ticks_of0:
754    ∀p:pseudo_assembly_program.
755      (Word → Word) → (Word → bool) → Word → pseudo_instruction → nat × nat ≝
756  λprogram: pseudo_assembly_program.
757  λsigma.
758  λpolicy.
759  λppc: Word.
760  λfetched.
761    match fetched with
762    [ Instruction instr ⇒
763      match instr with
764      [ JC lbl ⇒ ? (*
765        match pol lookup_labels ppc with
766        [ short_jump ⇒ 〈2, 2〉
767        | absolute_jump ⇒ ?
768        | long_jump ⇒ 〈4, 4〉
769        ] *)
770      | JNC lbl ⇒ ? (*
771        match pol lookup_labels ppc with
772        [ short_jump ⇒ 〈2, 2〉
773        | absolute_jump ⇒ ?
774        | long_jump ⇒ 〈4, 4〉
775        ] *)
776      | JB bit lbl ⇒ ? (*
777        match pol lookup_labels ppc with
778        [ short_jump ⇒ 〈2, 2〉
779        | absolute_jump ⇒ ?
780        | long_jump ⇒ 〈4, 4〉
781        ] *)
782      | JNB bit lbl ⇒ ? (*
783        match pol lookup_labels ppc with
784        [ short_jump ⇒ 〈2, 2〉
785        | absolute_jump ⇒ ?
786        | long_jump ⇒ 〈4, 4〉
787        ] *)
788      | JBC bit lbl ⇒ ? (*
789        match pol lookup_labels ppc with
790        [ short_jump ⇒ 〈2, 2〉
791        | absolute_jump ⇒ ?
792        | long_jump ⇒ 〈4, 4〉
793        ] *)
794      | JZ lbl ⇒ ? (*
795        match pol lookup_labels ppc with
796        [ short_jump ⇒ 〈2, 2〉
797        | absolute_jump ⇒ ?
798        | long_jump ⇒ 〈4, 4〉
799        ] *)
800      | JNZ lbl ⇒ ? (*
801        match pol lookup_labels  ppc with
802        [ short_jump ⇒ 〈2, 2〉
803        | absolute_jump ⇒ ?
804        | long_jump ⇒ 〈4, 4〉
805        ] *)
806      | CJNE arg lbl ⇒ ? (*
807        match pol lookup_labels ppc with
808        [ short_jump ⇒ 〈2, 2〉
809        | absolute_jump ⇒ ?
810        | long_jump ⇒ 〈4, 4〉
811        ] *)
812      | DJNZ arg lbl ⇒ ? (*
813        match pol lookup_labels ppc with
814        [ short_jump ⇒ 〈2, 2〉
815        | absolute_jump ⇒ ?
816        | long_jump ⇒ 〈4, 4〉
817        ] *)
818      | ADD arg1 arg2 ⇒
819        let ticks ≝ ticks_of_instruction (ADD ? arg1 arg2) in
820         〈ticks, ticks〉
821      | ADDC arg1 arg2 ⇒
822        let ticks ≝ ticks_of_instruction (ADDC ? arg1 arg2) in
823         〈ticks, ticks〉
824      | SUBB arg1 arg2 ⇒
825        let ticks ≝ ticks_of_instruction (SUBB ? arg1 arg2) in
826         〈ticks, ticks〉
827      | INC arg ⇒
828        let ticks ≝ ticks_of_instruction (INC ? arg) in
829         〈ticks, ticks〉
830      | DEC arg ⇒
831        let ticks ≝ ticks_of_instruction (DEC ? arg) in
832         〈ticks, ticks〉
833      | MUL arg1 arg2 ⇒
834        let ticks ≝ ticks_of_instruction (MUL ? arg1 arg2) in
835         〈ticks, ticks〉
836      | DIV arg1 arg2 ⇒
837        let ticks ≝ ticks_of_instruction (DIV ? arg1 arg2) in
838         〈ticks, ticks〉
839      | DA arg ⇒
840        let ticks ≝ ticks_of_instruction (DA ? arg) in
841         〈ticks, ticks〉
842      | ANL arg ⇒
843        let ticks ≝ ticks_of_instruction (ANL ? arg) in
844         〈ticks, ticks〉
845      | ORL arg ⇒
846        let ticks ≝ ticks_of_instruction (ORL ? arg) in
847         〈ticks, ticks〉
848      | XRL arg ⇒
849        let ticks ≝ ticks_of_instruction (XRL ? arg) in
850         〈ticks, ticks〉
851      | CLR arg ⇒
852        let ticks ≝ ticks_of_instruction (CLR ? arg) in
853         〈ticks, ticks〉
854      | CPL arg ⇒
855        let ticks ≝ ticks_of_instruction (CPL ? arg) in
856         〈ticks, ticks〉
857      | RL arg ⇒
858        let ticks ≝ ticks_of_instruction (RL ? arg) in
859         〈ticks, ticks〉
860      | RLC arg ⇒
861        let ticks ≝ ticks_of_instruction (RLC ? arg) in
862         〈ticks, ticks〉
863      | RR arg ⇒
864        let ticks ≝ ticks_of_instruction (RR ? arg) in
865         〈ticks, ticks〉
866      | RRC arg ⇒
867        let ticks ≝ ticks_of_instruction (RRC ? arg) in
868         〈ticks, ticks〉
869      | SWAP arg ⇒
870        let ticks ≝ ticks_of_instruction (SWAP ? arg) in
871         〈ticks, ticks〉
872      | MOV arg ⇒
873        let ticks ≝ ticks_of_instruction (MOV ? arg) in
874         〈ticks, ticks〉
875      | MOVX arg ⇒
876        let ticks ≝ ticks_of_instruction (MOVX ? arg) in
877         〈ticks, ticks〉
878      | SETB arg ⇒
879        let ticks ≝ ticks_of_instruction (SETB ? arg) in
880         〈ticks, ticks〉
881      | PUSH arg ⇒
882        let ticks ≝ ticks_of_instruction (PUSH ? arg) in
883         〈ticks, ticks〉
884      | POP arg ⇒
885        let ticks ≝ ticks_of_instruction (POP ? arg) in
886         〈ticks, ticks〉
887      | XCH arg1 arg2 ⇒
888        let ticks ≝ ticks_of_instruction (XCH ? arg1 arg2) in
889         〈ticks, ticks〉
890      | XCHD arg1 arg2 ⇒
891        let ticks ≝ ticks_of_instruction (XCHD ? arg1 arg2) in
892         〈ticks, ticks〉
893      | RET ⇒
894        let ticks ≝ ticks_of_instruction (RET ?) in
895         〈ticks, ticks〉
896      | RETI ⇒
897        let ticks ≝ ticks_of_instruction (RETI ?) in
898         〈ticks, ticks〉
899      | NOP ⇒
900        let ticks ≝ ticks_of_instruction (NOP ?) in
901         〈ticks, ticks〉
902      ]
903    | Comment comment ⇒ 〈0, 0〉
904    | Cost cost ⇒ 〈0, 0〉
905    | Jmp jmp ⇒ 〈2, 2〉
906    | Call call ⇒ 〈2, 2〉
907    | Mov dptr tgt ⇒ 〈2, 2〉
908    ].
909    cases daemon
910qed.
911
912definition ticks_of:
913    ∀p:pseudo_assembly_program.
914      (Word → Word) → (Word → bool) → ∀ppc:Word.
915       nat_of_bitvector … ppc < |\snd p| → nat × nat ≝
916  λprogram: pseudo_assembly_program.
917  λsigma.
918  λpolicy.
919  λppc: Word. λppc_ok.
920    let pseudo ≝ \snd program in
921    let 〈fetched, new_ppc〉 ≝ fetch_pseudo_instruction pseudo ppc ppc_ok in
922     ticks_of0 program sigma policy ppc fetched.
923
924(*
925lemma blah:
926  ∀m: internal_pseudo_address_map.
927  ∀s: PseudoStatus.
928  ∀arg: Byte.
929  ∀b: bool.
930    addressing_mode_ok m s (DIRECT arg) = true →
931      get_arg_8 ? (set_low_internal_ram ? s (low_internal_ram_of_pseudo_low_internal_ram m (low_internal_ram ? s))) b (DIRECT arg) =
932      get_arg_8 ? s b (DIRECT arg).
933  [2, 3: normalize % ]
934  #m #s #arg #b #hyp
935  whd in ⊢ (??%%)
936  @vsplit_elim''
937  #nu' #nl' #arg_nu_nl_eq
938  normalize nodelta
939  generalize in match (refl ? (get_index_v bool 4 nu' ? ?))
940  cases (get_index_v bool 4 nu' ? ?) in ⊢ (??%? → %)
941  #get_index_v_eq
942  normalize nodelta
943  [2:
944    normalize nodelta
945    @vsplit_elim''
946    #bit_one' #three_bits' #bit_one_three_bit_eq
947    generalize in match (low_internal_ram_of_pseudo_internal_ram_miss m s (three_bits'@@nl'))
948    normalize nodelta
949    generalize in match (refl ? (sub_7_with_carry ? ? ?))
950    cases (sub_7_with_carry ? ? ?) in ⊢ (??%? → %)
951    #Saddr #carr' #Saddr_carr_eq
952    normalize nodelta
953    #carr_hyp'
954    @carr_hyp'
955    [1:
956    |2: whd in hyp:(??%?); generalize in match hyp; -hyp;
957        generalize in match (refl ? (¬(member (BitVector 8) ? arg m)))
958        cases (¬(member (BitVector 8) ? arg m)) in ⊢ (??%? → %)
959        #member_eq
960        normalize nodelta
961        [2: #destr destruct(destr)
962        |1: -carr_hyp';
963            >arg_nu_nl_eq
964            <(vsplit_vector_singleton ? ? nu' ? ? ? bit_one_three_bit_eq)
965            [1: >get_index_v_eq in ⊢ (??%? → ?)
966            |2: @le_S @le_S @le_S @le_n
967            ]
968            cases (member (BitVector 8) ? (\fst ?) ?)
969            [1: #destr normalize in destr; destruct(destr)
970            |2:
971            ]
972        ]
973    |3: >get_index_v_eq in ⊢ (??%?)
974        change in ⊢ (??(???%?)?) with ((? ::: three_bits') @@ nl')
975        >(vsplit_vector_singleton … bit_one_three_bit_eq)
976        <arg_nu_nl_eq
977        whd in hyp:(??%?)
978        cases (member (BitVector 8) (eq_bv 8) arg m) in hyp
979        normalize nodelta [*: #ignore @sym_eq ]
980    ]
981  |
982  ].
983*)
984(*
985map_address0 ... (DIRECT arg) = Some .. →
986  get_arg_8 (map_address0 ... (internal_ram ...) (DIRECT arg) =
987  get_arg_8 (internal_ram ...) (DIRECT arg)
988
989((if addressing_mode_ok M ps ACC_A∧addressing_mode_ok M ps (DIRECT ARG2) 
990                     then Some internal_pseudo_address_map M 
991                     else None internal_pseudo_address_map )
992                    =Some internal_pseudo_address_map M')
993
994axiom low_internal_ram_write_at_stack_pointer:
995 ∀T1,T2,M,cm1,s1,cm2,s2,cm3,s3.∀sigma: Word → Word.∀policy: Word → bool.
996 ∀pbu,pbl,bu,bl,sp1,sp2:BitVector 8.
997  get_8051_sfr T2 cm2 s2 SFR_SP = get_8051_sfr ? cm3 s3 SFR_SP →
998  low_internal_ram ? cm2 s2 = low_internal_ram T2 cm3 s3 →
999  sp1 = add ? (get_8051_sfr … cm1 s1 SFR_SP) (bitvector_of_nat 8 1) →
1000  sp2 = add ? sp1 (bitvector_of_nat 8 1) →
1001  bu@@bl = sigma (pbu@@pbl) →
1002   low_internal_ram T1 cm1
1003     (write_at_stack_pointer …
1004       (set_8051_sfr …
1005         (write_at_stack_pointer …
1006           (set_8051_sfr …
1007             (set_low_internal_ram … s1
1008               (low_internal_ram_of_pseudo_low_internal_ram M (low_internal_ram … s2)))
1009             SFR_SP sp1)
1010          bl)
1011        SFR_SP sp2)
1012      bu)
1013   = low_internal_ram_of_pseudo_low_internal_ram (sp1::M)
1014      (low_internal_ram …
1015       (write_at_stack_pointer …
1016         (set_8051_sfr …
1017           (write_at_stack_pointer … (set_8051_sfr … s3 SFR_SP sp1) pbl)
1018          SFR_SP sp2)
1019        pbu)).
1020
1021lemma high_internal_ram_write_at_stack_pointer:
1022 ∀T1,T2,M,cm1,s1,cm2,s2,cm3,s3.∀sigma:Word → Word.∀policy: Word → bool.
1023 ∀pbu,pbl,bu,bl,sp1,sp2:BitVector 8.
1024  get_8051_sfr T2 cm2 s2 SFR_SP = get_8051_sfr ? cm3 s3 SFR_SP →
1025  high_internal_ram ?? s2 = high_internal_ram T2 cm3 s3 →
1026  sp1 = add ? (get_8051_sfr ? cm1 s1 SFR_SP) (bitvector_of_nat 8 1) →
1027  sp2 = add ? sp1 (bitvector_of_nat 8 1) →
1028  bu@@bl = sigma (pbu@@pbl) →
1029   high_internal_ram T1 cm1
1030     (write_at_stack_pointer …
1031       (set_8051_sfr …
1032         (write_at_stack_pointer …
1033           (set_8051_sfr …
1034             (set_high_internal_ram … s1
1035               (high_internal_ram_of_pseudo_high_internal_ram M (high_internal_ram … s2)))
1036             SFR_SP sp1)
1037          bl)
1038        SFR_SP sp2)
1039      bu)
1040   = high_internal_ram_of_pseudo_high_internal_ram (sp1::M)
1041      (high_internal_ram …
1042       (write_at_stack_pointer …
1043         (set_8051_sfr …
1044           (write_at_stack_pointer … (set_8051_sfr … s3 SFR_SP sp1) pbl)
1045          SFR_SP sp2)
1046        pbu)).
1047  #T1 #T2 #M #cm1 #s1 #cm2 #s2 #cm3 #s3 #sigma #policy #pbu #pbl #bu #bl #sp1 #sp2
1048  #get_8051_sfr_refl #high_internal_ram_refl #sp1_refl #sp2_refl #sigma_refl
1049  cases daemon (* XXX: !!! *)
1050qed.
1051*)
1052
1053(*CSC: ???*)
1054(* XXX: we need a precondition here stating that the PPC is within the
1055        bounds of the instruction list in order for Jaap's specification to
1056        apply.
1057*)
1058lemma snd_assembly_1_pseudoinstruction_ok:
1059  ∀program: pseudo_assembly_program.
1060  ∀program_length_proof: |\snd program| ≤ 2^16.
1061  ∀sigma: Word → Word.
1062  ∀policy: Word → bool.
1063  ∀sigma_policy_specification_witness: sigma_policy_specification program sigma policy.
1064  ∀ppc: Word.
1065  ∀ppc_in_bounds: nat_of_bitvector 16 ppc < |\snd program|.
1066  ∀pi.
1067  ∀lookup_labels.
1068  ∀lookup_datalabels.
1069    let 〈labels, costs〉 ≝ create_label_cost_map (\snd program) in
1070    lookup_labels = (λx. bitvector_of_nat 16 (lookup_def … labels x 0)) →
1071    lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1072    \fst (fetch_pseudo_instruction (\snd program) ppc ppc_in_bounds) = pi →
1073    let len ≝ \fst (assembly_1_pseudoinstruction lookup_labels sigma policy (*(sigma ppc)*) ppc lookup_datalabels pi) in
1074      sigma (add … ppc (bitvector_of_nat ? 1)) = add … (sigma ppc) (bitvector_of_nat ? len).
1075  #program #program_length_proof #sigma #policy #sigma_policy_specification_witness #ppc #ppc_in_bounds #pi
1076  #lookup_labels #lookup_datalabels
1077  @pair_elim #labels #costs #create_label_cost_map_refl
1078  #lookup_labels_refl #lookup_datalabels_refl #fetch_pseudo_refl
1079  normalize nodelta
1080  generalize in match fetch_pseudo_refl; -fetch_pseudo_refl
1081  #fetch_pseudo_refl
1082  letin assembled ≝ (\fst (pi1 … (assembly program sigma policy)))
1083  letin costs ≝ (\snd (pi1 … (assembly program sigma policy)))
1084  lapply (assembly_ok program program_length_proof sigma policy sigma_policy_specification_witness assembled costs)
1085  @pair_elim #preamble #instr_list #program_refl
1086  lapply create_label_cost_map_refl; -create_label_cost_map_refl
1087  >program_refl in ⊢ (% → ?); #create_label_cost_map_refl
1088  >create_label_cost_map_refl
1089  <eq_pair_fst_snd #H lapply (H (refl …)) -H #H
1090  lapply (H ppc ppc_in_bounds) -H
1091  @pair_elim #pi' #newppc #fetch_pseudo_refl'
1092  @pair_elim #len #assembled #assembly1_refl #H
1093  cases H
1094  #encoding_check_assm #sigma_newppc_refl
1095  >fetch_pseudo_refl' in fetch_pseudo_refl; #pi_refl'
1096  >pi_refl' in assembly1_refl; #assembly1_refl
1097  >lookup_labels_refl >lookup_datalabels_refl
1098  >program_refl normalize nodelta
1099  >assembly1_refl
1100  <sigma_newppc_refl
1101  generalize in match fetch_pseudo_refl';
1102  whd in match (fetch_pseudo_instruction ???);
1103  @pair_elim #lbl #instr #nth_refl normalize nodelta
1104  #G cases (pair_destruct_right ?????? G) %
1105qed.
1106
1107(* To be moved in ProofStatus *)
1108lemma program_counter_set_program_counter:
1109  ∀T.
1110  ∀cm.
1111  ∀s.
1112  ∀x.
1113    program_counter T cm (set_program_counter T cm s x) = x.
1114  //
1115qed.
Note: See TracBrowser for help on using the repository browser.