1 | include "ASM/ASM.ma". |
---|
2 | include "ASM/Arithmetic.ma". |
---|
3 | include "ASM/Fetch.ma". |
---|
4 | include "ASM/Interpret.ma". |
---|
5 | include "common/StructuredTraces.ma". |
---|
6 | |
---|
7 | let rec fetch_program_counter_n |
---|
8 | (n: nat) (code_memory: BitVectorTrie Byte 16) (program_counter: Word) |
---|
9 | on n: option Word ≝ |
---|
10 | match n with |
---|
11 | [ O ⇒ Some … program_counter |
---|
12 | | S n ⇒ |
---|
13 | match fetch_program_counter_n n code_memory program_counter with |
---|
14 | [ None ⇒ None … |
---|
15 | | Some tail_pc ⇒ |
---|
16 | let 〈instr, program_counter, ticks〉 ≝ fetch code_memory tail_pc in |
---|
17 | if ltb (nat_of_bitvector … tail_pc) (nat_of_bitvector … program_counter) then |
---|
18 | Some … program_counter |
---|
19 | else |
---|
20 | None Word (* XXX: overflow! *) |
---|
21 | ] |
---|
22 | ]. |
---|
23 | |
---|
24 | definition reachable_program_counter: BitVectorTrie Byte 16 → nat → Word → Prop ≝ |
---|
25 | λcode_memory: BitVectorTrie Byte 16. |
---|
26 | λprogram_size: nat. |
---|
27 | λprogram_counter: Word. |
---|
28 | (∃n: nat. Some … program_counter = fetch_program_counter_n n code_memory (zero 16)) ∧ |
---|
29 | nat_of_bitvector 16 program_counter < program_size. |
---|
30 | |
---|
31 | definition good_program: ∀code_memory: BitVectorTrie Byte 16. ∀total_program_size: nat. Prop ≝ |
---|
32 | λcode_memory: BitVectorTrie Byte 16. |
---|
33 | λtotal_program_size: nat. |
---|
34 | ∀program_counter: Word. |
---|
35 | ∀good_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter. |
---|
36 | let 〈instruction, program_counter', ticks〉 ≝ fetch code_memory program_counter in |
---|
37 | match instruction with |
---|
38 | [ RealInstruction instr ⇒ |
---|
39 | match instr with |
---|
40 | [ RET ⇒ True |
---|
41 | | JC relative ⇒ True (* XXX: see below *) |
---|
42 | | JNC relative ⇒ True (* XXX: see below *) |
---|
43 | | JB bit_addr relative ⇒ True |
---|
44 | | JNB bit_addr relative ⇒ True |
---|
45 | | JBC bit_addr relative ⇒ True |
---|
46 | | JZ relative ⇒ True |
---|
47 | | JNZ relative ⇒ True |
---|
48 | | CJNE src_trgt relative ⇒ True |
---|
49 | | DJNZ src_trgt relative ⇒ True |
---|
50 | | _ ⇒ |
---|
51 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
52 | nat_of_bitvector … program_counter' < total_program_size |
---|
53 | ] |
---|
54 | | LCALL addr ⇒ |
---|
55 | match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with |
---|
56 | [ ADDR16 addr ⇒ λaddr16: True. |
---|
57 | reachable_program_counter code_memory total_program_size addr ∧ |
---|
58 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
59 | nat_of_bitvector … program_counter' < total_program_size |
---|
60 | | _ ⇒ λother: False. ⊥ |
---|
61 | ] (subaddressing_modein … addr) |
---|
62 | | ACALL addr ⇒ |
---|
63 | match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with |
---|
64 | [ ADDR11 addr ⇒ λaddr11: True. |
---|
65 | let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter' in |
---|
66 | let 〈thr, eig〉 ≝ split … 3 8 addr in |
---|
67 | let 〈fiv, thr'〉 ≝ split … 5 3 pc_bu in |
---|
68 | let new_program_counter ≝ (fiv @@ thr) @@ pc_bl in |
---|
69 | reachable_program_counter code_memory total_program_size new_program_counter ∧ |
---|
70 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
71 | nat_of_bitvector … program_counter' < total_program_size |
---|
72 | | _ ⇒ λother: False. ⊥ |
---|
73 | ] (subaddressing_modein … addr) |
---|
74 | | AJMP addr ⇒ |
---|
75 | match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with |
---|
76 | [ ADDR11 addr ⇒ λaddr11: True. |
---|
77 | let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter' in |
---|
78 | let 〈nu, nl〉 ≝ split … 4 4 pc_bu in |
---|
79 | let bit ≝ get_index' … O ? nl in |
---|
80 | let 〈relevant1, relevant2〉 ≝ split … 3 8 addr in |
---|
81 | let new_addr ≝ (nu @@ (bit ::: relevant1)) @@ relevant2 in |
---|
82 | let 〈carry, new_program_counter〉 ≝ half_add 16 program_counter new_addr in |
---|
83 | reachable_program_counter code_memory total_program_size new_program_counter |
---|
84 | | _ ⇒ λother: False. ⊥ |
---|
85 | ] (subaddressing_modein … addr) |
---|
86 | | LJMP addr ⇒ |
---|
87 | match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with |
---|
88 | [ ADDR16 addr ⇒ λaddr16: True. |
---|
89 | reachable_program_counter code_memory total_program_size addr |
---|
90 | | _ ⇒ λother: False. ⊥ |
---|
91 | ] (subaddressing_modein … addr) |
---|
92 | | SJMP addr ⇒ |
---|
93 | match addr return λx. bool_to_Prop (is_in … [[ relative ]] x) → Prop with |
---|
94 | [ RELATIVE addr ⇒ λrelative: True. |
---|
95 | let 〈carry, new_program_counter〉 ≝ half_add … program_counter' (sign_extension addr) in |
---|
96 | reachable_program_counter code_memory total_program_size new_program_counter |
---|
97 | | _ ⇒ λother: False. ⊥ |
---|
98 | ] (subaddressing_modein … addr) |
---|
99 | | JMP addr ⇒ (* XXX: JMP is used for fptrs and unconstrained *) |
---|
100 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
101 | nat_of_bitvector … program_counter' < total_program_size |
---|
102 | | MOVC src trgt ⇒ |
---|
103 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
104 | nat_of_bitvector … program_counter' < total_program_size |
---|
105 | ]. |
---|
106 | cases other |
---|
107 | qed. |
---|
108 | |
---|
109 | lemma is_a_decidable: |
---|
110 | ∀hd. |
---|
111 | ∀element. |
---|
112 | is_a hd element = true ∨ is_a hd element = false. |
---|
113 | #hd #element // |
---|
114 | qed. |
---|
115 | |
---|
116 | lemma mem_decidable: |
---|
117 | ∀n: nat. |
---|
118 | ∀v: Vector addressing_mode_tag n. |
---|
119 | ∀element: addressing_mode_tag. |
---|
120 | mem … eq_a n v element = true ∨ |
---|
121 | mem … eq_a … v element = false. |
---|
122 | #n #v #element // |
---|
123 | qed. |
---|
124 | |
---|
125 | lemma eq_a_elim: |
---|
126 | ∀tag. |
---|
127 | ∀hd. |
---|
128 | ∀P: bool → Prop. |
---|
129 | (tag = hd → P (true)) → |
---|
130 | (tag ≠ hd → P (false)) → |
---|
131 | P (eq_a tag hd). |
---|
132 | #tag #hd #P |
---|
133 | cases tag |
---|
134 | cases hd |
---|
135 | #true_hyp #false_hyp |
---|
136 | try @false_hyp |
---|
137 | try @true_hyp |
---|
138 | try % |
---|
139 | #absurd destruct(absurd) |
---|
140 | qed. |
---|
141 | |
---|
142 | lemma is_a_true_to_is_in: |
---|
143 | ∀n: nat. |
---|
144 | ∀x: addressing_mode. |
---|
145 | ∀tag: addressing_mode_tag. |
---|
146 | ∀supervector: Vector addressing_mode_tag n. |
---|
147 | mem addressing_mode_tag eq_a n supervector tag → |
---|
148 | is_a tag x = true → |
---|
149 | is_in … supervector x. |
---|
150 | #n #x #tag #supervector |
---|
151 | elim supervector |
---|
152 | [1: |
---|
153 | #absurd cases absurd |
---|
154 | |2: |
---|
155 | #n' #hd #tl #inductive_hypothesis |
---|
156 | whd in match (mem … eq_a (S n') (hd:::tl) tag); |
---|
157 | @eq_a_elim normalize nodelta |
---|
158 | [1: |
---|
159 | #tag_hd_eq #irrelevant |
---|
160 | whd in match (is_in (S n') (hd:::tl) x); |
---|
161 | <tag_hd_eq #is_a_hyp >is_a_hyp normalize nodelta |
---|
162 | @I |
---|
163 | |2: |
---|
164 | #tag_hd_neq |
---|
165 | whd in match (is_in (S n') (hd:::tl) x); |
---|
166 | change with ( |
---|
167 | mem … eq_a n' tl tag) |
---|
168 | in match (fold_right … n' ? false tl); |
---|
169 | #mem_hyp #is_a_hyp |
---|
170 | cases(is_a hd x) |
---|
171 | [1: |
---|
172 | normalize nodelta // |
---|
173 | |2: |
---|
174 | normalize nodelta |
---|
175 | @inductive_hypothesis assumption |
---|
176 | ] |
---|
177 | ] |
---|
178 | ] |
---|
179 | qed. |
---|
180 | |
---|
181 | lemma is_in_subvector_is_in_supervector: |
---|
182 | ∀m, n: nat. |
---|
183 | ∀subvector: Vector addressing_mode_tag m. |
---|
184 | ∀supervector: Vector addressing_mode_tag n. |
---|
185 | ∀element: addressing_mode. |
---|
186 | subvector_with … eq_a subvector supervector → |
---|
187 | is_in m subvector element → is_in n supervector element. |
---|
188 | #m #n #subvector #supervector #element |
---|
189 | elim subvector |
---|
190 | [1: |
---|
191 | #subvector_with_proof #is_in_proof |
---|
192 | cases is_in_proof |
---|
193 | |2: |
---|
194 | #n' #hd' #tl' #inductive_hypothesis #subvector_with_proof |
---|
195 | whd in match (is_in … (hd':::tl') element); |
---|
196 | cases (is_a_decidable hd' element) |
---|
197 | [1: |
---|
198 | #is_a_true >is_a_true |
---|
199 | #irrelevant |
---|
200 | whd in match (subvector_with … eq_a (hd':::tl') supervector) in subvector_with_proof; |
---|
201 | @(is_a_true_to_is_in … is_a_true) |
---|
202 | lapply(subvector_with_proof) |
---|
203 | cases(mem … eq_a n supervector hd') // |
---|
204 | |2: |
---|
205 | #is_a_false >is_a_false normalize nodelta |
---|
206 | #assm |
---|
207 | @inductive_hypothesis |
---|
208 | [1: |
---|
209 | generalize in match subvector_with_proof; |
---|
210 | whd in match (subvector_with … eq_a (hd':::tl') supervector); |
---|
211 | cases(mem_decidable n supervector hd') |
---|
212 | [1: |
---|
213 | #mem_true >mem_true normalize nodelta |
---|
214 | #assm assumption |
---|
215 | |2: |
---|
216 | #mem_false >mem_false #absurd |
---|
217 | cases absurd |
---|
218 | ] |
---|
219 | |2: |
---|
220 | assumption |
---|
221 | ] |
---|
222 | ] |
---|
223 | ] |
---|
224 | qed. |
---|
225 | |
---|
226 | let rec member_addressing_mode_tag |
---|
227 | (n: nat) (v: Vector addressing_mode_tag n) (a: addressing_mode_tag) |
---|
228 | on v: Prop ≝ |
---|
229 | match v with |
---|
230 | [ VEmpty ⇒ False |
---|
231 | | VCons n' hd tl ⇒ |
---|
232 | bool_to_Prop (eq_a hd a) ∨ member_addressing_mode_tag n' tl a |
---|
233 | ]. |
---|
234 | |
---|
235 | let rec subaddressing_mode_elim_type |
---|
236 | (T: Type[2]) (m: nat) (fixed_v: Vector addressing_mode_tag m) |
---|
237 | (Q: addressing_mode → T → Prop) |
---|
238 | (p_addr11: ∀w: Word11. is_in m fixed_v (ADDR11 w) → T) |
---|
239 | (p_addr16: ∀w: Word. is_in m fixed_v (ADDR16 w) → T) |
---|
240 | (p_direct: ∀w: Byte. is_in m fixed_v (DIRECT w) → T) |
---|
241 | (p_indirect: ∀w: Bit. is_in m fixed_v (INDIRECT w) → T) |
---|
242 | (p_ext_indirect: ∀w: Bit. is_in m fixed_v (EXT_INDIRECT w) → T) |
---|
243 | (p_acc_a: is_in m fixed_v ACC_A → T) |
---|
244 | (p_register: ∀w: BitVector 3. is_in m fixed_v (REGISTER w) → T) |
---|
245 | (p_acc_b: is_in m fixed_v ACC_B → T) |
---|
246 | (p_dptr: is_in m fixed_v DPTR → T) |
---|
247 | (p_data: ∀w: Byte. is_in m fixed_v (DATA w) → T) |
---|
248 | (p_data16: ∀w: Word. is_in m fixed_v (DATA16 w) → T) |
---|
249 | (p_acc_dptr: is_in m fixed_v ACC_DPTR → T) |
---|
250 | (p_acc_pc: is_in m fixed_v ACC_PC → T) |
---|
251 | (p_ext_indirect_dptr: is_in m fixed_v EXT_INDIRECT_DPTR → T) |
---|
252 | (p_indirect_dptr: is_in m fixed_v INDIRECT_DPTR → T) |
---|
253 | (p_carry: is_in m fixed_v CARRY → T) |
---|
254 | (p_bit_addr: ∀w: Byte. is_in m fixed_v (BIT_ADDR w) → T) |
---|
255 | (p_n_bit_addr: ∀w: Byte. is_in m fixed_v (N_BIT_ADDR w) → T) |
---|
256 | (p_relative: ∀w: Byte. is_in m fixed_v (RELATIVE w) → T) |
---|
257 | (n: nat) (v: Vector addressing_mode_tag n) (proof: subvector_with … eq_a v fixed_v) |
---|
258 | on v: Prop ≝ |
---|
259 | match v return λo: nat. λv': Vector addressing_mode_tag o. o = n → v ≃ v' → ? with |
---|
260 | [ VEmpty ⇒ λm_refl. λv_refl. |
---|
261 | ∀addr: addressing_mode. ∀p: is_in m fixed_v addr. |
---|
262 | Q addr ( |
---|
263 | match addr return λx: addressing_mode. is_in … fixed_v x → T with |
---|
264 | [ ADDR11 x ⇒ p_addr11 x |
---|
265 | | ADDR16 x ⇒ p_addr16 x |
---|
266 | | DIRECT x ⇒ p_direct x |
---|
267 | | INDIRECT x ⇒ p_indirect x |
---|
268 | | EXT_INDIRECT x ⇒ p_ext_indirect x |
---|
269 | | ACC_A ⇒ p_acc_a |
---|
270 | | REGISTER x ⇒ p_register x |
---|
271 | | ACC_B ⇒ p_acc_b |
---|
272 | | DPTR ⇒ p_dptr |
---|
273 | | DATA x ⇒ p_data x |
---|
274 | | DATA16 x ⇒ p_data16 x |
---|
275 | | ACC_DPTR ⇒ p_acc_dptr |
---|
276 | | ACC_PC ⇒ p_acc_pc |
---|
277 | | EXT_INDIRECT_DPTR ⇒ p_ext_indirect_dptr |
---|
278 | | INDIRECT_DPTR ⇒ p_indirect_dptr |
---|
279 | | CARRY ⇒ p_carry |
---|
280 | | BIT_ADDR x ⇒ p_bit_addr x |
---|
281 | | N_BIT_ADDR x ⇒ p_n_bit_addr x |
---|
282 | | RELATIVE x ⇒ p_relative x |
---|
283 | ] p) |
---|
284 | | VCons n' hd tl ⇒ λm_refl. λv_refl. |
---|
285 | let tail_call ≝ subaddressing_mode_elim_type T m fixed_v Q p_addr11 |
---|
286 | p_addr16 p_direct p_indirect p_ext_indirect p_acc_a |
---|
287 | p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr |
---|
288 | p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry |
---|
289 | p_bit_addr p_n_bit_addr p_relative n' tl ? |
---|
290 | in |
---|
291 | match hd return λa: addressing_mode_tag. a = hd → ? with |
---|
292 | [ addr11 ⇒ λhd_refl. (∀w. Q (ADDR11 w) (p_addr11 w ?)) → tail_call |
---|
293 | | addr16 ⇒ λhd_refl. (∀w. Q (ADDR16 w) (p_addr16 w ?)) → tail_call |
---|
294 | | direct ⇒ λhd_refl. (∀w. Q (DIRECT w) (p_direct w ?)) → tail_call |
---|
295 | | indirect ⇒ λhd_refl. (∀w. Q (INDIRECT w) (p_indirect w ?)) → tail_call |
---|
296 | | ext_indirect ⇒ λhd_refl. (∀w. Q (EXT_INDIRECT w) (p_ext_indirect w ?)) → tail_call |
---|
297 | | acc_a ⇒ λhd_refl. (Q ACC_A (p_acc_a ?)) → tail_call |
---|
298 | | registr ⇒ λhd_refl. (∀w. Q (REGISTER w) (p_register w ?)) → tail_call |
---|
299 | | acc_b ⇒ λhd_refl. (Q ACC_A (p_acc_b ?)) → tail_call |
---|
300 | | dptr ⇒ λhd_refl. (Q DPTR (p_dptr ?)) → tail_call |
---|
301 | | data ⇒ λhd_refl. (∀w. Q (DATA w) (p_data w ?)) → tail_call |
---|
302 | | data16 ⇒ λhd_refl. (∀w. Q (DATA16 w) (p_data16 w ?)) → tail_call |
---|
303 | | acc_dptr ⇒ λhd_refl. (Q ACC_DPTR (p_acc_dptr ?)) → tail_call |
---|
304 | | acc_pc ⇒ λhd_refl. (Q ACC_PC (p_acc_pc ?)) → tail_call |
---|
305 | | ext_indirect_dptr ⇒ λhd_refl. (Q EXT_INDIRECT_DPTR (p_ext_indirect_dptr ?)) → tail_call |
---|
306 | | indirect_dptr ⇒ λhd_refl. (Q INDIRECT_DPTR (p_indirect_dptr ?)) → tail_call |
---|
307 | | carry ⇒ λhd_refl. (Q CARRY (p_carry ?)) → tail_call |
---|
308 | | bit_addr ⇒ λhd_refl. (∀w. Q (BIT_ADDR w) (p_bit_addr w ?)) → tail_call |
---|
309 | | n_bit_addr ⇒ λhd_refl. (∀w. Q (N_BIT_ADDR w) (p_n_bit_addr w ?)) → tail_call |
---|
310 | | relative ⇒ λhd_refl. (∀w. Q (RELATIVE w) (p_relative w ?)) → tail_call |
---|
311 | ] (refl … hd) |
---|
312 | ] (refl … n) (refl_jmeq … v). |
---|
313 | [20: |
---|
314 | generalize in match proof; destruct |
---|
315 | whd in match (subvector_with … eq_a (hd:::tl) fixed_v); |
---|
316 | cases (mem … eq_a m fixed_v hd) normalize nodelta |
---|
317 | [1: |
---|
318 | whd in match (subvector_with … eq_a tl fixed_v); |
---|
319 | #assm assumption |
---|
320 | |2: |
---|
321 | normalize in ⊢ (% → ?); |
---|
322 | #absurd cases absurd |
---|
323 | ] |
---|
324 | ] |
---|
325 | @(is_in_subvector_is_in_supervector … proof) |
---|
326 | destruct @I |
---|
327 | qed. |
---|
328 | |
---|
329 | lemma subaddressing_mode_elim': |
---|
330 | ∀T: Type[2]. |
---|
331 | ∀n: nat. |
---|
332 | ∀o: nat. |
---|
333 | ∀Q: addressing_mode → T → Prop. |
---|
334 | ∀fixed_v: Vector addressing_mode_tag (n + o). |
---|
335 | ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19. |
---|
336 | ∀v1: Vector addressing_mode_tag n. |
---|
337 | ∀v2: Vector addressing_mode_tag o. |
---|
338 | ∀fixed_v_proof: fixed_v = v1 @@ v2. |
---|
339 | ∀subaddressing_mode_proof. |
---|
340 | subaddressing_mode_elim_type T (n + o) fixed_v Q P1 P2 P3 P4 P5 P6 P7 |
---|
341 | P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 (n + o) (v1 @@ v2) subaddressing_mode_proof. |
---|
342 | #T #n #o #Q #fixed_v #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10 |
---|
343 | #P11 #P12 #P13 #P14 #P15 #P16 #P17 #P18 #P19 #v1 #v2 #fixed_v_proof |
---|
344 | cases daemon |
---|
345 | qed. |
---|
346 | |
---|
347 | axiom subaddressing_mode_elim: |
---|
348 | ∀T: Type[2]. |
---|
349 | ∀m: nat. |
---|
350 | ∀n: nat. |
---|
351 | ∀Q: addressing_mode → T → Prop. |
---|
352 | ∀fixed_v: Vector addressing_mode_tag m. |
---|
353 | ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19. |
---|
354 | ∀v: Vector addressing_mode_tag n. |
---|
355 | ∀proof. |
---|
356 | subaddressing_mode_elim_type T m fixed_v Q P1 P2 P3 P4 P5 P6 P7 |
---|
357 | P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 n v proof. |
---|
358 | |
---|
359 | definition current_instruction0 ≝ |
---|
360 | λcode_memory: BitVectorTrie Byte 16. |
---|
361 | λprogram_counter: Word. |
---|
362 | \fst (\fst (fetch … code_memory program_counter)). |
---|
363 | |
---|
364 | definition current_instruction ≝ |
---|
365 | λs: Status. |
---|
366 | current_instruction0 (code_memory … s) (program_counter … s). |
---|
367 | |
---|
368 | definition ASM_classify0: instruction → status_class ≝ |
---|
369 | λi: instruction. |
---|
370 | match i with |
---|
371 | [ RealInstruction pre ⇒ |
---|
372 | match pre with |
---|
373 | [ RET ⇒ cl_return |
---|
374 | | JZ _ ⇒ cl_jump |
---|
375 | | JNZ _ ⇒ cl_jump |
---|
376 | | JC _ ⇒ cl_jump |
---|
377 | | JNC _ ⇒ cl_jump |
---|
378 | | JB _ _ ⇒ cl_jump |
---|
379 | | JNB _ _ ⇒ cl_jump |
---|
380 | | JBC _ _ ⇒ cl_jump |
---|
381 | | CJNE _ _ ⇒ cl_jump |
---|
382 | | DJNZ _ _ ⇒ cl_jump |
---|
383 | | _ ⇒ cl_other |
---|
384 | ] |
---|
385 | | ACALL _ ⇒ cl_call |
---|
386 | | LCALL _ ⇒ cl_call |
---|
387 | | JMP _ ⇒ cl_call |
---|
388 | | AJMP _ ⇒ cl_jump |
---|
389 | | LJMP _ ⇒ cl_jump |
---|
390 | | SJMP _ ⇒ cl_jump |
---|
391 | | _ ⇒ cl_other |
---|
392 | ]. |
---|
393 | |
---|
394 | definition ASM_classify: Status → status_class ≝ |
---|
395 | λs: Status. |
---|
396 | ASM_classify0 (current_instruction s). |
---|
397 | |
---|
398 | definition current_instruction_is_labelled ≝ |
---|
399 | λcost_labels: BitVectorTrie costlabel 16. |
---|
400 | λs: Status. |
---|
401 | let pc ≝ program_counter … s in |
---|
402 | match lookup_opt … pc cost_labels with |
---|
403 | [ None ⇒ False |
---|
404 | | _ ⇒ True |
---|
405 | ]. |
---|
406 | |
---|
407 | definition next_instruction_properly_relates_program_counters ≝ |
---|
408 | λbefore: Status. |
---|
409 | λafter : Status. |
---|
410 | let size ≝ current_instruction_cost before in |
---|
411 | let pc_before ≝ program_counter … before in |
---|
412 | let pc_after ≝ program_counter … after in |
---|
413 | let sum ≝ \snd (half_add … pc_before (bitvector_of_nat … size)) in |
---|
414 | sum = pc_after. |
---|
415 | |
---|
416 | definition ASM_abstract_status: BitVectorTrie costlabel 16 → abstract_status ≝ |
---|
417 | λcost_labels. |
---|
418 | mk_abstract_status |
---|
419 | Status |
---|
420 | (λs,s'. (execute_1 s) = s') |
---|
421 | (λs,class. ASM_classify s = class) |
---|
422 | (current_instruction_is_labelled cost_labels) |
---|
423 | next_instruction_properly_relates_program_counters. |
---|
424 | |
---|
425 | let rec trace_any_label_length |
---|
426 | (cost_labels: BitVectorTrie costlabel 16) (trace_ends_flag: trace_ends_with_ret) |
---|
427 | (start_status: Status) (final_status: Status) |
---|
428 | (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status) |
---|
429 | on the_trace: nat ≝ |
---|
430 | match the_trace with |
---|
431 | [ tal_base_not_return the_status _ _ _ _ ⇒ 0 |
---|
432 | | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒ 0 |
---|
433 | | tal_base_return the_status _ _ _ ⇒ 0 |
---|
434 | | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final _ _ _ call_trace _ final_trace ⇒ |
---|
435 | let tail_length ≝ trace_any_label_length … final_trace in |
---|
436 | let pc_difference ≝ nat_of_bitvector … (program_counter … after_fun_call) - nat_of_bitvector … (program_counter … pre_fun_call) in |
---|
437 | pc_difference + tail_length |
---|
438 | | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒ |
---|
439 | let tail_length ≝ trace_any_label_length … tail_trace in |
---|
440 | let pc_difference ≝ nat_of_bitvector … (program_counter … status_init) - nat_of_bitvector … (program_counter … status_pre) in |
---|
441 | pc_difference + tail_length |
---|
442 | ]. |
---|
443 | |
---|
444 | let rec compute_paid_trace_any_label |
---|
445 | (cost_labels: BitVectorTrie costlabel 16) |
---|
446 | (trace_ends_flag: trace_ends_with_ret) |
---|
447 | (start_status: Status) (final_status: Status) |
---|
448 | (the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status) |
---|
449 | on the_trace: nat ≝ |
---|
450 | match the_trace with |
---|
451 | [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost the_status |
---|
452 | | tal_base_return the_status _ _ _ ⇒ current_instruction_cost the_status |
---|
453 | | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒ current_instruction_cost pre_fun_call |
---|
454 | | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final |
---|
455 | _ _ _ call_trace _ final_trace ⇒ |
---|
456 | let current_instruction_cost ≝ current_instruction_cost pre_fun_call in |
---|
457 | let final_trace_cost ≝ compute_paid_trace_any_label cost_labels end_flag … final_trace in |
---|
458 | current_instruction_cost + final_trace_cost |
---|
459 | | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒ |
---|
460 | let current_instruction_cost ≝ current_instruction_cost status_pre in |
---|
461 | let tail_trace_cost ≝ |
---|
462 | compute_paid_trace_any_label cost_labels end_flag |
---|
463 | status_init status_end tail_trace |
---|
464 | in |
---|
465 | current_instruction_cost + tail_trace_cost |
---|
466 | ]. |
---|
467 | |
---|
468 | definition compute_paid_trace_label_label ≝ |
---|
469 | λcost_labels: BitVectorTrie costlabel 16. |
---|
470 | λtrace_ends_flag: trace_ends_with_ret. |
---|
471 | λstart_status: Status. |
---|
472 | λfinal_status: Status. |
---|
473 | λthe_trace: trace_label_label (ASM_abstract_status cost_labels) trace_ends_flag |
---|
474 | start_status final_status. |
---|
475 | match the_trace with |
---|
476 | [ tll_base ends_flag initial final given_trace labelled_proof ⇒ |
---|
477 | compute_paid_trace_any_label … given_trace |
---|
478 | ]. |
---|
479 | |
---|
480 | include alias "arithmetics/nat.ma". |
---|
481 | include alias "basics/logic.ma". |
---|
482 | |
---|
483 | lemma plus_right_monotone: |
---|
484 | ∀m, n, o: nat. |
---|
485 | m = n → m + o = n + o. |
---|
486 | #m #n #o #refl >refl % |
---|
487 | qed. |
---|
488 | |
---|
489 | axiom as_execute_preserves_code_memory: |
---|
490 | ∀cost_labels : BitVectorTrie costlabel 16. |
---|
491 | ∀start_status: ASM_abstract_status cost_labels. |
---|
492 | ∀final_status: ASM_abstract_status cost_labels. |
---|
493 | as_execute … start_status final_status → |
---|
494 | code_memory … start_status = code_memory … final_status. |
---|
495 | |
---|
496 | axiom trace_label_return_preserves_code_memory: |
---|
497 | ∀cost_labels : BitVectorTrie costlabel 16. |
---|
498 | ∀start_status: ASM_abstract_status cost_labels. |
---|
499 | ∀final_status: ASM_abstract_status cost_labels. |
---|
500 | ∀the_trace : trace_label_return … start_status final_status. |
---|
501 | code_memory … start_status = code_memory … final_status. |
---|
502 | |
---|
503 | (* XXX: indexing bug re-emerges! *) |
---|
504 | example fetch_half_add: |
---|
505 | ∀cost_labels: BitVectorTrie costlabel 16. |
---|
506 | ∀the_status : ASM_abstract_status cost_labels. |
---|
507 | \snd (\fst (fetch (code_memory … the_status) (program_counter … the_status))) = |
---|
508 | \snd (half_add 16 (program_counter (BitVectorTrie Byte 16) the_status) |
---|
509 | (bitvector_of_nat … (\snd (fetch (code_memory … the_status) (program_counter … the_status))))). |
---|
510 | cases daemon |
---|
511 | qed. |
---|
512 | |
---|
513 | axiom half_add_minus_right: |
---|
514 | ∀size : nat. |
---|
515 | ∀left : BitVector size. |
---|
516 | ∀right: nat. |
---|
517 | nat_of_bitvector … (\snd (half_add … left (bitvector_of_nat … right))) - |
---|
518 | nat_of_bitvector … left = right. |
---|
519 | |
---|
520 | lemma minus_plus_cancel: |
---|
521 | ∀m, n : nat. |
---|
522 | ∀proof: n ≤ m. |
---|
523 | (m - n) + n = m. |
---|
524 | #m #n #proof /2 by plus_minus/ |
---|
525 | qed. |
---|
526 | |
---|
527 | let rec block_cost' |
---|
528 | (code_memory': BitVectorTrie Byte 16) (program_counter': Word) |
---|
529 | (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16) |
---|
530 | (reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter') |
---|
531 | (good_program_witness: good_program code_memory' total_program_size) (first_time_around: bool) |
---|
532 | on program_size: |
---|
533 | total_program_size ≤ program_size + nat_of_bitvector … program_counter' → |
---|
534 | Σcost_of_block: nat. |
---|
535 | ∀start_status: Status. |
---|
536 | ∀final_status: Status. |
---|
537 | ∀trace_ends_flag. |
---|
538 | ∀the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status. |
---|
539 | code_memory' = code_memory … start_status → |
---|
540 | program_counter' = program_counter … start_status → |
---|
541 | (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧ |
---|
542 | cost_of_block = compute_paid_trace_any_label cost_labels trace_ends_flag start_status final_status the_trace ≝ |
---|
543 | match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter' → ? with |
---|
544 | [ O ⇒ λbase_case. ⊥ |
---|
545 | | S program_size' ⇒ λrecursive_case. |
---|
546 | let 〈instruction, program_counter'', ticks〉 as FETCH ≝ fetch code_memory' program_counter' in |
---|
547 | let to_continue ≝ |
---|
548 | match lookup_opt … program_counter'' cost_labels with |
---|
549 | [ None ⇒ true |
---|
550 | | Some _ ⇒ first_time_around |
---|
551 | ] |
---|
552 | in |
---|
553 | if to_continue then |
---|
554 | match instruction return λx. x = instruction → ? with |
---|
555 | [ RealInstruction real_instruction ⇒ λreal_instruction_refl. |
---|
556 | match real_instruction return λx. x = real_instruction → |
---|
557 | Σcost_of_block: nat. |
---|
558 | ∀start_status: Status. |
---|
559 | ∀final_status: Status. |
---|
560 | ∀trace_ends_flag. |
---|
561 | ∀the_trace: trace_any_label (ASM_abstract_status cost_labels) trace_ends_flag start_status final_status. |
---|
562 | code_memory' = code_memory … start_status → |
---|
563 | program_counter' = program_counter … start_status → |
---|
564 | (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧ |
---|
565 | cost_of_block = compute_paid_trace_any_label cost_labels trace_ends_flag start_status final_status the_trace with |
---|
566 | [ RET ⇒ λinstr. ticks |
---|
567 | | JC relative ⇒ λinstr. ticks |
---|
568 | | JNC relative ⇒ λinstr. ticks |
---|
569 | | JB bit_addr relative ⇒ λinstr. ticks |
---|
570 | | JNB bit_addr relative ⇒ λinstr. ticks |
---|
571 | | JBC bit_addr relative ⇒ λinstr. ticks |
---|
572 | | JZ relative ⇒ λinstr. ticks |
---|
573 | | JNZ relative ⇒ λinstr. ticks |
---|
574 | | CJNE src_trgt relative ⇒ λinstr. ticks |
---|
575 | | DJNZ src_trgt relative ⇒ λinstr. ticks |
---|
576 | | _ ⇒ λinstr. |
---|
577 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
578 | ] (refl …) |
---|
579 | | ACALL addr ⇒ λinstr. |
---|
580 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
581 | | AJMP addr ⇒ λinstr. ticks |
---|
582 | | LCALL addr ⇒ λinstr. |
---|
583 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
584 | | LJMP addr ⇒ λinstr. ticks |
---|
585 | | SJMP addr ⇒ λinstr. ticks |
---|
586 | | JMP addr ⇒ λinstr. (* XXX: actually a call due to use with fptrs *) |
---|
587 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
588 | | MOVC src trgt ⇒ λinstr. |
---|
589 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
590 | ] (refl …) |
---|
591 | else |
---|
592 | (0 : (Σn: nat. ?)) |
---|
593 | ]. |
---|
594 | [1: |
---|
595 | cases reachable_program_counter_witness #_ #hyp |
---|
596 | @(absurd (total_program_size < total_program_size) … (not_le_Sn_n …)) |
---|
597 | @(le_to_lt_to_lt … base_case hyp) |
---|
598 | |4: |
---|
599 | #start_status #final_status #trace_ends_flag #the_trace #code_memory_refl #program_counter_refl |
---|
600 | cases(block_cost' ?????????) -block_cost' |
---|
601 | #recursive_block_cost #recursive_assm |
---|
602 | @(trace_any_label_inv_ind … the_trace) |
---|
603 | [1: |
---|
604 | #start_status' #final_status' #execute_assm #classifier_assm #costed_assm |
---|
605 | #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl destruct |
---|
606 | cases classifier_assm |
---|
607 | whd in match (as_classifier ???); |
---|
608 | whd in ⊢ (??%? → ?); |
---|
609 | whd in match current_instruction; normalize nodelta |
---|
610 | whd in match current_instruction0; normalize nodelta |
---|
611 | #absurd @⊥ <FETCH in absurd; normalize nodelta |
---|
612 | #absurd destruct(absurd) |
---|
613 | |2: |
---|
614 | #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm |
---|
615 | #start_status_refl #final_status_refl #the_trace_assm destruct |
---|
616 | whd in classifier_assm; |
---|
617 | whd in classifier_assm:(??%?); |
---|
618 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
619 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
620 | @⊥ <FETCH in classifier_assm; normalize nodelta |
---|
621 | #absurd destruct(absurd) |
---|
622 | |3: |
---|
623 | #status_pre_fun_call #status_start_fun_call #status_final #execute_assm |
---|
624 | #classifier_assm #after_return_assm #trace_label_return #costed_assm |
---|
625 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
626 | destruct |
---|
627 | whd in match (compute_paid_trace_any_label … (tal_base_call …)); |
---|
628 | whd in match (trace_any_label_length … (tal_base_call …)); |
---|
629 | cases(recursive_assm status_pre_fun_call status_final doesnt_end_with_ret |
---|
630 | (tal_base_call (ASM_abstract_status cost_labels) status_pre_fun_call |
---|
631 | (execute_1 status_pre_fun_call) status_final |
---|
632 | (refl Status (execute_1 status_pre_fun_call)) classifier_assm |
---|
633 | after_return_assm trace_label_return costed_assm) ? ?) |
---|
634 | [1: |
---|
635 | * #recursive_n #recursive_trace_total #recursive_block_cost_assm % |
---|
636 | [1: |
---|
637 | %{(pred total_program_size)} |
---|
638 | whd in ⊢ (??%?); |
---|
639 | >S_pred [1: % ] |
---|
640 | <recursive_trace_total |
---|
641 | @lt_O_S |
---|
642 | |2: |
---|
643 | >recursive_block_cost_assm |
---|
644 | whd in ⊢ (??(??%)?); <FETCH |
---|
645 | (* XXX: ??? *) |
---|
646 | ] |
---|
647 | |2: |
---|
648 | % |
---|
649 | |3: |
---|
650 | cut(program_counter'' = \snd (\fst (fetch (code_memory … status_pre_fun_call) (program_counter … status_pre_fun_call)))) |
---|
651 | [1: |
---|
652 | cases FETCH % |
---|
653 | |2: |
---|
654 | #program_counter_hyp'' >program_counter_hyp'' |
---|
655 | whd in after_return_assm; <after_return_assm |
---|
656 | whd in match (current_instruction_cost status_pre_fun_call); |
---|
657 | @fetch_half_add |
---|
658 | ] |
---|
659 | ] |
---|
660 | [1: |
---|
661 | |4: |
---|
662 | #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call |
---|
663 | #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return |
---|
664 | #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl |
---|
665 | #final_status_refl #the_trace_refl |
---|
666 | generalize in match execute_assm; destruct #execute_assm |
---|
667 | whd in match (compute_paid_trace_any_label … (tal_step_call …)); |
---|
668 | whd in match (trace_any_label_length … (tal_step_call …)); |
---|
669 | cases(recursive_assm status_after_fun_call status_final end_flag trace_any_label ? ?) |
---|
670 | [1: |
---|
671 | * #recursive_n #recursive_trace_total #recursive_block_cost_assm % |
---|
672 | [1: |
---|
673 | <recursive_trace_total % |
---|
674 | [1: |
---|
675 | @(recursive_n - (current_instruction_cost status_pre_fun_call)) |
---|
676 | |2: |
---|
677 | whd in after_return_assm; cases after_return_assm |
---|
678 | >half_add_minus_right in ⊢ (??%?); |
---|
679 | >commutative_plus in ⊢ (???%); |
---|
680 | >commutative_plus in ⊢ (??%?); |
---|
681 | <associative_plus in ⊢ (??%?); |
---|
682 | @plus_right_monotone |
---|
683 | cut(current_instruction_cost status_pre_fun_call ≤ recursive_n) |
---|
684 | [1: |
---|
685 | (* XXX: ??? *) |
---|
686 | |2: |
---|
687 | #current_instruction_cost_assm |
---|
688 | <minus_Sn_m in ⊢ (??%?); [2: assumption ] |
---|
689 | >minus_plus_cancel [1: % ] |
---|
690 | @le_S assumption |
---|
691 | ] |
---|
692 | ] |
---|
693 | |2: |
---|
694 | >recursive_block_cost_assm |
---|
695 | @plus_right_monotone |
---|
696 | whd in match (current_instruction_cost status_pre_fun_call); |
---|
697 | <FETCH % |
---|
698 | ] |
---|
699 | |2: |
---|
700 | lapply (trace_label_return_preserves_code_memory … (execute_1 status_pre_fun_call) status_after_fun_call) |
---|
701 | #hyp cases (hyp trace_label_return) |
---|
702 | @as_execute_preserves_code_memory whd % |
---|
703 | |3: |
---|
704 | cut(program_counter'' = \snd (\fst (fetch (code_memory … status_pre_fun_call) (program_counter … status_pre_fun_call)))) |
---|
705 | [1: |
---|
706 | cases FETCH % |
---|
707 | |2: |
---|
708 | #program_counter_hyp'' >program_counter_hyp'' |
---|
709 | whd in after_return_assm; <after_return_assm |
---|
710 | whd in match (current_instruction_cost status_pre_fun_call); |
---|
711 | @fetch_half_add |
---|
712 | ] |
---|
713 | ] |
---|
714 | |5: |
---|
715 | #end_flag #status_pre #status_init #status_end #execute_assm #the_trace' |
---|
716 | #classifier_assm #costed_assm #trace_ends_flag_refl #start_status_refl |
---|
717 | #final_status_refl #trace_refl |
---|
718 | generalize in match the_trace; destruct #the_trace |
---|
719 | whd in classifier_assm; |
---|
720 | whd in classifier_assm:(??%?); |
---|
721 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
722 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
723 | @⊥ <FETCH in classifier_assm; normalize nodelta |
---|
724 | #absurd destruct(absurd) |
---|
725 | ] |
---|
726 | |7,8,9,10,13,16,19,22,25,28,31,34,37,40,41,42,43,44,45,46,47,48,49,52,55,58,61, |
---|
727 | 64,67,70,73,76,79,82,85,88,91,94,97,100,101,104,108,107: |
---|
728 | cases daemon (* XXX: massive terms *) |
---|
729 | |2: |
---|
730 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
731 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
732 | <FETCH normalize nodelta <instr normalize nodelta |
---|
733 | @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr |
---|
734 | cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta |
---|
735 | cases (split … 3 8 new_addr) #thr #eig normalize nodelta |
---|
736 | cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n' |
---|
737 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
738 | @(transitive_le |
---|
739 | total_program_size |
---|
740 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
741 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
742 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
743 | >plus_n_Sm |
---|
744 | @monotonic_le_plus_r |
---|
745 | change with ( |
---|
746 | nat_of_bitvector … program_counter' < |
---|
747 | nat_of_bitvector … program_counter'') |
---|
748 | assumption |
---|
749 | |3: |
---|
750 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
751 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
752 | <FETCH normalize nodelta <instr normalize nodelta |
---|
753 | @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr |
---|
754 | cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta |
---|
755 | cases (split … 3 8 new_addr) #thr #eig normalize nodelta |
---|
756 | cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n' |
---|
757 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
758 | % |
---|
759 | [1: |
---|
760 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
761 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
762 | >(le_to_leb_true … program_counter_lt') % |
---|
763 | |2: |
---|
764 | assumption |
---|
765 | ] |
---|
766 | |5: |
---|
767 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
768 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
769 | <FETCH normalize nodelta <instr normalize nodelta |
---|
770 | @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr |
---|
771 | * * * * #n' |
---|
772 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
773 | @(transitive_le |
---|
774 | total_program_size |
---|
775 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
776 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
777 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
778 | >plus_n_Sm |
---|
779 | @monotonic_le_plus_r |
---|
780 | change with ( |
---|
781 | nat_of_bitvector … program_counter' < |
---|
782 | nat_of_bitvector … program_counter'') |
---|
783 | assumption |
---|
784 | |6: |
---|
785 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
786 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
787 | <FETCH normalize nodelta <instr normalize nodelta |
---|
788 | @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr |
---|
789 | * * * * #n' |
---|
790 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
791 | % |
---|
792 | [1: |
---|
793 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
794 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
795 | >(le_to_leb_true … program_counter_lt') % |
---|
796 | |2: |
---|
797 | assumption |
---|
798 | ] |
---|
799 | |11,14: (* JMP and MOVC *) |
---|
800 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
801 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
802 | <FETCH normalize nodelta <instr normalize nodelta |
---|
803 | try(<real_instruction_refl <instr normalize nodelta) * |
---|
804 | #pc_pc_lt_hyp' #pc_tps_lt_hyp' |
---|
805 | @(transitive_le |
---|
806 | total_program_size |
---|
807 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
808 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
809 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
810 | >plus_n_Sm |
---|
811 | @monotonic_le_plus_r |
---|
812 | change with ( |
---|
813 | nat_of_bitvector … program_counter' < |
---|
814 | nat_of_bitvector … program_counter'') |
---|
815 | assumption |
---|
816 | |12,15: |
---|
817 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
818 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
819 | <FETCH normalize nodelta <instr normalize nodelta * |
---|
820 | #program_counter_lt' #program_counter_lt_tps' % |
---|
821 | [1,3: |
---|
822 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
823 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
824 | >(le_to_leb_true … program_counter_lt') % |
---|
825 | |2,4: |
---|
826 | assumption |
---|
827 | ] |
---|
828 | |17,20,23,26,29,32,35,38,50,53,56,59,62,65,68,71,74,77,80,83,86,89,92,95,98, |
---|
829 | 102,105: |
---|
830 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
831 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
832 | <FETCH normalize nodelta |
---|
833 | <real_instruction_refl <instr normalize nodelta * |
---|
834 | #pc_pc_lt_hyp' #pc_tps_lt_hyp' |
---|
835 | @(transitive_le |
---|
836 | total_program_size |
---|
837 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
838 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
839 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
840 | >plus_n_Sm |
---|
841 | @monotonic_le_plus_r |
---|
842 | change with ( |
---|
843 | nat_of_bitvector … program_counter' < |
---|
844 | nat_of_bitvector … program_counter'') |
---|
845 | assumption |
---|
846 | |18,21,24,27,30,33,36,39,51,54,57,60,63,66,69,72,75,78, |
---|
847 | 81,84,87,90,93,96,99,103,106: |
---|
848 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
849 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
850 | <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta * |
---|
851 | #program_counter_lt' #program_counter_lt_tps' % |
---|
852 | try assumption |
---|
853 | [*: |
---|
854 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
855 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
856 | >(le_to_leb_true … program_counter_lt') % |
---|
857 | ] |
---|
858 | [1: |
---|
859 | #start_status #final_status #trace_ends_flag #the_trace #code_memory_refl #program_counter_refl |
---|
860 | cases(block_cost' ?????? ?? ?) -block_cost' |
---|
861 | #recursive_block_cost #recursive_assm |
---|
862 | @(trace_any_label_inv_ind … the_trace) |
---|
863 | [1: |
---|
864 | #start_status' #final_status' #execute_assm #classifier_assm #costed_assm |
---|
865 | #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl destruct |
---|
866 | cases classifier_assm |
---|
867 | whd in match (as_classifier ? ? ?); |
---|
868 | whd in ⊢ (??%? → ?); |
---|
869 | whd in match current_instruction; normalize nodelta |
---|
870 | whd in match current_instruction0; normalize nodelta |
---|
871 | #absurd @⊥ >p1 in absurd; normalize nodelta |
---|
872 | #absurd destruct(absurd) |
---|
873 | |2: |
---|
874 | #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm |
---|
875 | #start_status_refl #final_status_refl #the_trace_assm destruct |
---|
876 | whd in classifier_assm; |
---|
877 | whd in classifier_assm:(??%?); |
---|
878 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
879 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
880 | @⊥ |
---|
881 | >p1 in classifier_assm; normalize nodelta |
---|
882 | #absurd destruct(absurd) |
---|
883 | |3: |
---|
884 | #status_pre_fun_call #status_start_fun_call #status_final #execute_assm |
---|
885 | #classifier_assm #after_return_assm #trace_label_return #costed_assm |
---|
886 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
887 | destruct |
---|
888 | cases(recursive_assm status_pre_fun_call status_final doesnt_end_with_ret |
---|
889 | (tal_base_call (ASM_abstract_status cost_labels) status_pre_fun_call |
---|
890 | (execute_1 status_pre_fun_call) status_final |
---|
891 | (refl Status (execute_1 status_pre_fun_call)) classifier_assm |
---|
892 | after_return_assm trace_label_return costed_assm) ? ?) |
---|
893 | [2: % |
---|
894 | |3: |
---|
895 | |1: |
---|
896 | ] |
---|
897 | |4: |
---|
898 | #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call |
---|
899 | #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return |
---|
900 | #costed_assm #trace_any_label #trace_ends_refl #start_status_refl #final_status_refl |
---|
901 | #the_trace_refl destruct whd in ⊢ (??(???%)); whd in match (trace_any_label_length … (tal_step_call … trace_any_label)); |
---|
902 | |5: |
---|
903 | #end_flag #status_pre #status_init #status_end #execute_assm #the_trace |
---|
904 | #classifier_assm #costed_assm #ends_flag_refl #start_status_refl #final_status_refl |
---|
905 | #the_trace_refl -the_trace_refl destruct |
---|
906 | whd in classifier_assm; |
---|
907 | whd in classifier_assm:(??%?); |
---|
908 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
909 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
910 | @⊥ >p1 in classifier_assm; normalize nodelta |
---|
911 | #absurd destruct(absurd) |
---|
912 | ] |
---|
913 | [1: |
---|
914 | #start_status #final_status #trace_ends_flag #the_trace |
---|
915 | #code_memory_refl #program_counter_refl |
---|
916 | cases(block_cost' ?????? ?? ?) -block_cost' |
---|
917 | #recursive_block_cost #recursive_assm |
---|
918 | @(trace_any_label_inv_ind … the_trace) |
---|
919 | [1: |
---|
920 | #start_status' #final_status' #execute_assm #not_return_assm |
---|
921 | #costed_assm #trace_ends_flag_assm #start_status_assm #final_status_assm |
---|
922 | #the_trace_assm |
---|
923 | cases daemon (* XXX: bug in notion of traces here *) |
---|
924 | |2: |
---|
925 | |
---|
926 | |3: |
---|
927 | cases daemon (* XXX *) |
---|
928 | |4: |
---|
929 | ] |
---|
930 | |108: |
---|
931 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
932 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
933 | <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta * |
---|
934 | #program_counter_lt' #program_counter_lt_tps' % |
---|
935 | try assumption |
---|
936 | [*: |
---|
937 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
938 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
939 | >(le_to_leb_true … program_counter_lt') % |
---|
940 | ] |
---|
941 | [1: |
---|
942 | cases reachable_program_counter_witness #_ #hyp |
---|
943 | @(absurd (total_program_size < total_program_size) … (not_le_Sn_n …)) |
---|
944 | @(le_to_lt_to_lt … base_case hyp) |
---|
945 | |2: |
---|
946 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
947 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
948 | <FETCH normalize nodelta <instr normalize nodelta |
---|
949 | @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr |
---|
950 | cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta |
---|
951 | cases (split … 3 8 new_addr) #thr #eig normalize nodelta |
---|
952 | cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n' |
---|
953 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
954 | @(transitive_le |
---|
955 | total_program_size |
---|
956 | ((S program_size') + nat_of_bitvector … program_counter) |
---|
957 | (program_size' + nat_of_bitvector … program_counter') recursive_case) |
---|
958 | normalize in match (S program_size' + nat_of_bitvector … program_counter); |
---|
959 | >plus_n_Sm |
---|
960 | @monotonic_le_plus_r |
---|
961 | change with ( |
---|
962 | nat_of_bitvector … program_counter < |
---|
963 | nat_of_bitvector … program_counter') |
---|
964 | assumption |
---|
965 | |3: |
---|
966 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
967 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
968 | <FETCH normalize nodelta <instr normalize nodelta |
---|
969 | @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr |
---|
970 | cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta |
---|
971 | cases (split … 3 8 new_addr) #thr #eig normalize nodelta |
---|
972 | cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n' |
---|
973 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
974 | % |
---|
975 | [1: |
---|
976 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
977 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
978 | >(le_to_leb_true … program_counter_lt') % |
---|
979 | |2: |
---|
980 | assumption |
---|
981 | ] |
---|
982 | |4: |
---|
983 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
984 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
985 | <FETCH normalize nodelta <instr normalize nodelta |
---|
986 | @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr |
---|
987 | * * * * #n' |
---|
988 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
989 | @(transitive_le |
---|
990 | total_program_size |
---|
991 | ((S program_size') + nat_of_bitvector … program_counter) |
---|
992 | (program_size' + nat_of_bitvector … program_counter') recursive_case) |
---|
993 | normalize in match (S program_size' + nat_of_bitvector … program_counter); |
---|
994 | >plus_n_Sm |
---|
995 | @monotonic_le_plus_r |
---|
996 | change with ( |
---|
997 | nat_of_bitvector … program_counter < |
---|
998 | nat_of_bitvector … program_counter') |
---|
999 | assumption |
---|
1000 | |5: |
---|
1001 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1002 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
1003 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1004 | @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr |
---|
1005 | * * * * #n' |
---|
1006 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
1007 | % |
---|
1008 | [1: |
---|
1009 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1010 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1011 | >(le_to_leb_true … program_counter_lt') % |
---|
1012 | |2: |
---|
1013 | assumption |
---|
1014 | ] |
---|
1015 | |6,8: (* JMP and MOVC *) |
---|
1016 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1017 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
1018 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1019 | try(<real_instruction_refl <instr normalize nodelta) * |
---|
1020 | #pc_pc_lt_hyp' #pc_tps_lt_hyp' |
---|
1021 | @(transitive_le |
---|
1022 | total_program_size |
---|
1023 | ((S program_size') + nat_of_bitvector … program_counter) |
---|
1024 | (program_size' + nat_of_bitvector … program_counter') recursive_case) |
---|
1025 | normalize in match (S program_size' + nat_of_bitvector … program_counter); |
---|
1026 | >plus_n_Sm |
---|
1027 | @monotonic_le_plus_r |
---|
1028 | change with ( |
---|
1029 | nat_of_bitvector … program_counter < |
---|
1030 | nat_of_bitvector … program_counter') |
---|
1031 | assumption |
---|
1032 | |7,9: |
---|
1033 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1034 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
1035 | <FETCH normalize nodelta <instr normalize nodelta * |
---|
1036 | #program_counter_lt' #program_counter_lt_tps' % |
---|
1037 | [1,3: |
---|
1038 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1039 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1040 | >(le_to_leb_true … program_counter_lt') % |
---|
1041 | |2,4: |
---|
1042 | assumption |
---|
1043 | ] |
---|
1044 | |11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53, |
---|
1045 | 55,57,59,61,63: |
---|
1046 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1047 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
1048 | <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta * |
---|
1049 | #program_counter_lt' #program_counter_lt_tps' % |
---|
1050 | try assumption |
---|
1051 | [*: |
---|
1052 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1053 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1054 | >(le_to_leb_true … program_counter_lt') % |
---|
1055 | ] |
---|
1056 | |10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52, |
---|
1057 | 54,56,58,60,62: |
---|
1058 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1059 | lapply(good_program_witness program_counter reachable_program_counter_witness) |
---|
1060 | <FETCH normalize nodelta |
---|
1061 | <real_instruction_refl <instr normalize nodelta * |
---|
1062 | #pc_pc_lt_hyp' #pc_tps_lt_hyp' |
---|
1063 | @(transitive_le |
---|
1064 | total_program_size |
---|
1065 | ((S program_size') + nat_of_bitvector … program_counter) |
---|
1066 | (program_size' + nat_of_bitvector … program_counter') recursive_case) |
---|
1067 | normalize in match (S program_size' + nat_of_bitvector … program_counter); |
---|
1068 | >plus_n_Sm |
---|
1069 | @monotonic_le_plus_r |
---|
1070 | change with ( |
---|
1071 | nat_of_bitvector … program_counter < |
---|
1072 | nat_of_bitvector … program_counter') |
---|
1073 | assumption |
---|
1074 | ] |
---|
1075 | qed. |
---|
1076 | |
---|
1077 | definition block_cost ≝ |
---|
1078 | λcode_memory: BitVectorTrie Byte 16. |
---|
1079 | λprogram_counter: Word. |
---|
1080 | λtotal_program_size: nat. |
---|
1081 | λcost_labels: BitVectorTrie costlabel 16. |
---|
1082 | λreachable_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter. |
---|
1083 | λgood_program_witness: good_program code_memory total_program_size. |
---|
1084 | block_cost' code_memory program_counter total_program_size total_program_size cost_labels |
---|
1085 | reachable_program_counter_witness good_program_witness true ?. |
---|
1086 | @le_plus_n_r |
---|
1087 | qed. |
---|
1088 | |
---|
1089 | lemma fetch_program_counter_n_Sn: |
---|
1090 | ∀instruction: instruction. |
---|
1091 | ∀program_counter, program_counter': Word. |
---|
1092 | ∀ticks, n: nat. |
---|
1093 | ∀code_memory: BitVectorTrie Byte 16. |
---|
1094 | Some … program_counter = fetch_program_counter_n n code_memory (zero 16) → |
---|
1095 | 〈instruction,program_counter',ticks〉 = fetch code_memory program_counter → |
---|
1096 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' → |
---|
1097 | Some … program_counter' = fetch_program_counter_n (S n) code_memory (zero …). |
---|
1098 | #instruction #program_counter #program_counter' #ticks #n #code_memory |
---|
1099 | #fetch_program_counter_n_hyp #fetch_hyp #lt_hyp |
---|
1100 | whd in match (fetch_program_counter_n (S n) code_memory (zero …)); |
---|
1101 | <fetch_program_counter_n_hyp normalize nodelta |
---|
1102 | <fetch_hyp normalize nodelta |
---|
1103 | change with ( |
---|
1104 | leb (S (nat_of_bitvector … program_counter)) (nat_of_bitvector … program_counter') |
---|
1105 | ) in match (ltb (nat_of_bitvector … program_counter) (nat_of_bitvector … program_counter')); |
---|
1106 | >(le_to_leb_true … lt_hyp) % |
---|
1107 | qed. |
---|
1108 | |
---|
1109 | let rec traverse_code_internal |
---|
1110 | (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16) |
---|
1111 | (program_counter: Word) (fixed_program_size: nat) (program_size: nat) |
---|
1112 | (reachable_program_counter_witness: |
---|
1113 | ∀lbl: costlabel. |
---|
1114 | ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels → |
---|
1115 | reachable_program_counter code_memory fixed_program_size program_counter) |
---|
1116 | (good_program_witness: good_program code_memory fixed_program_size) |
---|
1117 | on program_size: identifier_map CostTag nat ≝ |
---|
1118 | match program_size with |
---|
1119 | [ O ⇒ empty_map … |
---|
1120 | | S program_size ⇒ |
---|
1121 | let 〈instruction, new_program_counter, ticks〉 as FETCH ≝ fetch … code_memory program_counter in |
---|
1122 | let cost_mapping ≝ traverse_code_internal code_memory cost_labels new_program_counter fixed_program_size program_size ? good_program_witness in |
---|
1123 | match lookup_opt … program_counter cost_labels return λx. x = lookup_opt … program_counter cost_labels → ? with |
---|
1124 | [ None ⇒ λlookup_refl. cost_mapping |
---|
1125 | | Some lbl ⇒ λlookup_refl. |
---|
1126 | let cost ≝ block_cost code_memory program_counter fixed_program_size cost_labels ? good_program_witness in |
---|
1127 | add … cost_mapping lbl cost |
---|
1128 | ] (refl … (lookup_opt … program_counter cost_labels)) |
---|
1129 | ]. |
---|
1130 | [1: |
---|
1131 | @(reachable_program_counter_witness lbl) |
---|
1132 | assumption |
---|
1133 | |2: |
---|
1134 | assumption |
---|
1135 | ] |
---|
1136 | qed. |
---|
1137 | |
---|
1138 | definition traverse_code ≝ |
---|
1139 | λcode_memory: BitVectorTrie Byte 16. |
---|
1140 | λcost_labels: BitVectorTrie costlabel 16. |
---|
1141 | λprogram_size: nat. |
---|
1142 | λreachable_program_counter_witness: |
---|
1143 | ∀lbl: costlabel. |
---|
1144 | ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels → |
---|
1145 | reachable_program_counter code_memory program_size program_counter. |
---|
1146 | λgood_program_witness: good_program code_memory program_size. |
---|
1147 | traverse_code_internal code_memory cost_labels (zero …) program_size program_size reachable_program_counter_witness good_program_witness. |
---|
1148 | |
---|
1149 | definition compute_costs ≝ |
---|
1150 | λprogram: list Byte. |
---|
1151 | λcost_labels: BitVectorTrie costlabel 16. |
---|
1152 | λreachable_program_witness. |
---|
1153 | λgood_program_witness: good_program (load_code_memory program) (|program| + 1). |
---|
1154 | let program_size ≝ |program| + 1 in |
---|
1155 | let code_memory ≝ load_code_memory program in |
---|
1156 | traverse_code code_memory cost_labels program_size reachable_program_witness ?. |
---|
1157 | @good_program_witness |
---|
1158 | qed. |
---|