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 well_labelling: BitVectorTrie costlabel 16 → Prop ≝ |
---|
32 | λcost_labels. |
---|
33 | injective … (λx. lookup_opt … x cost_labels). |
---|
34 | |
---|
35 | definition good_program: ∀code_memory: BitVectorTrie Byte 16. ∀total_program_size: nat. Prop ≝ |
---|
36 | λcode_memory: BitVectorTrie Byte 16. |
---|
37 | λtotal_program_size: nat. |
---|
38 | ∀program_counter: Word. |
---|
39 | ∀good_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter. |
---|
40 | let 〈instruction, program_counter', ticks〉 ≝ fetch code_memory program_counter in |
---|
41 | match instruction with |
---|
42 | [ RealInstruction instr ⇒ |
---|
43 | match instr with |
---|
44 | [ RET ⇒ True |
---|
45 | | JC relative ⇒ True (* XXX: see below *) |
---|
46 | | JNC relative ⇒ True (* XXX: see below *) |
---|
47 | | JB bit_addr relative ⇒ True |
---|
48 | | JNB bit_addr relative ⇒ True |
---|
49 | | JBC bit_addr relative ⇒ True |
---|
50 | | JZ relative ⇒ True |
---|
51 | | JNZ relative ⇒ True |
---|
52 | | CJNE src_trgt relative ⇒ True |
---|
53 | | DJNZ src_trgt relative ⇒ True |
---|
54 | | _ ⇒ |
---|
55 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
56 | nat_of_bitvector … program_counter' < total_program_size |
---|
57 | ] |
---|
58 | | LCALL addr ⇒ |
---|
59 | match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with |
---|
60 | [ ADDR16 addr ⇒ λaddr16: True. |
---|
61 | reachable_program_counter code_memory total_program_size addr ∧ |
---|
62 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
63 | nat_of_bitvector … program_counter' < total_program_size |
---|
64 | | _ ⇒ λother: False. ⊥ |
---|
65 | ] (subaddressing_modein … addr) |
---|
66 | | ACALL addr ⇒ |
---|
67 | match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with |
---|
68 | [ ADDR11 addr ⇒ λaddr11: True. |
---|
69 | let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter' in |
---|
70 | let 〈thr, eig〉 ≝ split … 3 8 addr in |
---|
71 | let 〈fiv, thr'〉 ≝ split … 5 3 pc_bu in |
---|
72 | let new_program_counter ≝ (fiv @@ thr) @@ pc_bl in |
---|
73 | reachable_program_counter code_memory total_program_size new_program_counter ∧ |
---|
74 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
75 | nat_of_bitvector … program_counter' < total_program_size |
---|
76 | | _ ⇒ λother: False. ⊥ |
---|
77 | ] (subaddressing_modein … addr) |
---|
78 | | AJMP addr ⇒ |
---|
79 | match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with |
---|
80 | [ ADDR11 addr ⇒ λaddr11: True. |
---|
81 | let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter' in |
---|
82 | let 〈nu, nl〉 ≝ split … 4 4 pc_bu in |
---|
83 | let bit ≝ get_index' … O ? nl in |
---|
84 | let 〈relevant1, relevant2〉 ≝ split … 3 8 addr in |
---|
85 | let new_addr ≝ (nu @@ (bit ::: relevant1)) @@ relevant2 in |
---|
86 | let 〈carry, new_program_counter〉 ≝ half_add 16 program_counter new_addr in |
---|
87 | reachable_program_counter code_memory total_program_size new_program_counter |
---|
88 | | _ ⇒ λother: False. ⊥ |
---|
89 | ] (subaddressing_modein … addr) |
---|
90 | | LJMP addr ⇒ |
---|
91 | match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with |
---|
92 | [ ADDR16 addr ⇒ λaddr16: True. |
---|
93 | reachable_program_counter code_memory total_program_size addr |
---|
94 | | _ ⇒ λother: False. ⊥ |
---|
95 | ] (subaddressing_modein … addr) |
---|
96 | | SJMP addr ⇒ |
---|
97 | match addr return λx. bool_to_Prop (is_in … [[ relative ]] x) → Prop with |
---|
98 | [ RELATIVE addr ⇒ λrelative: True. |
---|
99 | let 〈carry, new_program_counter〉 ≝ half_add … program_counter' (sign_extension addr) in |
---|
100 | reachable_program_counter code_memory total_program_size new_program_counter |
---|
101 | | _ ⇒ λother: False. ⊥ |
---|
102 | ] (subaddressing_modein … addr) |
---|
103 | | JMP addr ⇒ (* XXX: JMP is used for fptrs and unconstrained *) |
---|
104 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
105 | nat_of_bitvector … program_counter' < total_program_size |
---|
106 | | MOVC src trgt ⇒ |
---|
107 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' ∧ |
---|
108 | nat_of_bitvector … program_counter' < total_program_size |
---|
109 | ]. |
---|
110 | cases other |
---|
111 | qed. |
---|
112 | |
---|
113 | lemma is_a_decidable: |
---|
114 | ∀hd. |
---|
115 | ∀element. |
---|
116 | is_a hd element = true ∨ is_a hd element = false. |
---|
117 | #hd #element // |
---|
118 | qed. |
---|
119 | |
---|
120 | lemma mem_decidable: |
---|
121 | ∀n: nat. |
---|
122 | ∀v: Vector addressing_mode_tag n. |
---|
123 | ∀element: addressing_mode_tag. |
---|
124 | mem … eq_a n v element = true ∨ |
---|
125 | mem … eq_a … v element = false. |
---|
126 | #n #v #element // |
---|
127 | qed. |
---|
128 | |
---|
129 | lemma eq_a_elim: |
---|
130 | ∀tag. |
---|
131 | ∀hd. |
---|
132 | ∀P: bool → Prop. |
---|
133 | (tag = hd → P (true)) → |
---|
134 | (tag ≠ hd → P (false)) → |
---|
135 | P (eq_a tag hd). |
---|
136 | #tag #hd #P |
---|
137 | cases tag |
---|
138 | cases hd |
---|
139 | #true_hyp #false_hyp |
---|
140 | try @false_hyp |
---|
141 | try @true_hyp |
---|
142 | try % |
---|
143 | #absurd destruct(absurd) |
---|
144 | qed. |
---|
145 | |
---|
146 | lemma is_a_true_to_is_in: |
---|
147 | ∀n: nat. |
---|
148 | ∀x: addressing_mode. |
---|
149 | ∀tag: addressing_mode_tag. |
---|
150 | ∀supervector: Vector addressing_mode_tag n. |
---|
151 | mem addressing_mode_tag eq_a n supervector tag → |
---|
152 | is_a tag x = true → |
---|
153 | is_in … supervector x. |
---|
154 | #n #x #tag #supervector |
---|
155 | elim supervector |
---|
156 | [1: |
---|
157 | #absurd cases absurd |
---|
158 | |2: |
---|
159 | #n' #hd #tl #inductive_hypothesis |
---|
160 | whd in match (mem … eq_a (S n') (hd:::tl) tag); |
---|
161 | @eq_a_elim normalize nodelta |
---|
162 | [1: |
---|
163 | #tag_hd_eq #irrelevant |
---|
164 | whd in match (is_in (S n') (hd:::tl) x); |
---|
165 | <tag_hd_eq #is_a_hyp >is_a_hyp normalize nodelta |
---|
166 | @I |
---|
167 | |2: |
---|
168 | #tag_hd_neq |
---|
169 | whd in match (is_in (S n') (hd:::tl) x); |
---|
170 | change with ( |
---|
171 | mem … eq_a n' tl tag) |
---|
172 | in match (fold_right … n' ? false tl); |
---|
173 | #mem_hyp #is_a_hyp |
---|
174 | cases(is_a hd x) |
---|
175 | [1: |
---|
176 | normalize nodelta // |
---|
177 | |2: |
---|
178 | normalize nodelta |
---|
179 | @inductive_hypothesis assumption |
---|
180 | ] |
---|
181 | ] |
---|
182 | ] |
---|
183 | qed. |
---|
184 | |
---|
185 | lemma is_in_subvector_is_in_supervector: |
---|
186 | ∀m, n: nat. |
---|
187 | ∀subvector: Vector addressing_mode_tag m. |
---|
188 | ∀supervector: Vector addressing_mode_tag n. |
---|
189 | ∀element: addressing_mode. |
---|
190 | subvector_with … eq_a subvector supervector → |
---|
191 | is_in m subvector element → is_in n supervector element. |
---|
192 | #m #n #subvector #supervector #element |
---|
193 | elim subvector |
---|
194 | [1: |
---|
195 | #subvector_with_proof #is_in_proof |
---|
196 | cases is_in_proof |
---|
197 | |2: |
---|
198 | #n' #hd' #tl' #inductive_hypothesis #subvector_with_proof |
---|
199 | whd in match (is_in … (hd':::tl') element); |
---|
200 | cases (is_a_decidable hd' element) |
---|
201 | [1: |
---|
202 | #is_a_true >is_a_true |
---|
203 | #irrelevant |
---|
204 | whd in match (subvector_with … eq_a (hd':::tl') supervector) in subvector_with_proof; |
---|
205 | @(is_a_true_to_is_in … is_a_true) |
---|
206 | lapply(subvector_with_proof) |
---|
207 | cases(mem … eq_a n supervector hd') // |
---|
208 | |2: |
---|
209 | #is_a_false >is_a_false normalize nodelta |
---|
210 | #assm |
---|
211 | @inductive_hypothesis |
---|
212 | [1: |
---|
213 | generalize in match subvector_with_proof; |
---|
214 | whd in match (subvector_with … eq_a (hd':::tl') supervector); |
---|
215 | cases(mem_decidable n supervector hd') |
---|
216 | [1: |
---|
217 | #mem_true >mem_true normalize nodelta |
---|
218 | #assm assumption |
---|
219 | |2: |
---|
220 | #mem_false >mem_false #absurd |
---|
221 | cases absurd |
---|
222 | ] |
---|
223 | |2: |
---|
224 | assumption |
---|
225 | ] |
---|
226 | ] |
---|
227 | ] |
---|
228 | qed. |
---|
229 | |
---|
230 | let rec member_addressing_mode_tag |
---|
231 | (n: nat) (v: Vector addressing_mode_tag n) (a: addressing_mode_tag) |
---|
232 | on v: Prop ≝ |
---|
233 | match v with |
---|
234 | [ VEmpty ⇒ False |
---|
235 | | VCons n' hd tl ⇒ |
---|
236 | bool_to_Prop (eq_a hd a) ∨ member_addressing_mode_tag n' tl a |
---|
237 | ]. |
---|
238 | |
---|
239 | let rec subaddressing_mode_elim_type |
---|
240 | (T: Type[2]) (m: nat) (fixed_v: Vector addressing_mode_tag m) |
---|
241 | (Q: addressing_mode → T → Prop) |
---|
242 | (p_addr11: ∀w: Word11. is_in m fixed_v (ADDR11 w) → T) |
---|
243 | (p_addr16: ∀w: Word. is_in m fixed_v (ADDR16 w) → T) |
---|
244 | (p_direct: ∀w: Byte. is_in m fixed_v (DIRECT w) → T) |
---|
245 | (p_indirect: ∀w: Bit. is_in m fixed_v (INDIRECT w) → T) |
---|
246 | (p_ext_indirect: ∀w: Bit. is_in m fixed_v (EXT_INDIRECT w) → T) |
---|
247 | (p_acc_a: is_in m fixed_v ACC_A → T) |
---|
248 | (p_register: ∀w: BitVector 3. is_in m fixed_v (REGISTER w) → T) |
---|
249 | (p_acc_b: is_in m fixed_v ACC_B → T) |
---|
250 | (p_dptr: is_in m fixed_v DPTR → T) |
---|
251 | (p_data: ∀w: Byte. is_in m fixed_v (DATA w) → T) |
---|
252 | (p_data16: ∀w: Word. is_in m fixed_v (DATA16 w) → T) |
---|
253 | (p_acc_dptr: is_in m fixed_v ACC_DPTR → T) |
---|
254 | (p_acc_pc: is_in m fixed_v ACC_PC → T) |
---|
255 | (p_ext_indirect_dptr: is_in m fixed_v EXT_INDIRECT_DPTR → T) |
---|
256 | (p_indirect_dptr: is_in m fixed_v INDIRECT_DPTR → T) |
---|
257 | (p_carry: is_in m fixed_v CARRY → T) |
---|
258 | (p_bit_addr: ∀w: Byte. is_in m fixed_v (BIT_ADDR w) → T) |
---|
259 | (p_n_bit_addr: ∀w: Byte. is_in m fixed_v (N_BIT_ADDR w) → T) |
---|
260 | (p_relative: ∀w: Byte. is_in m fixed_v (RELATIVE w) → T) |
---|
261 | (n: nat) (v: Vector addressing_mode_tag n) (proof: subvector_with … eq_a v fixed_v) |
---|
262 | on v: Prop ≝ |
---|
263 | match v return λo: nat. λv': Vector addressing_mode_tag o. o = n → v ≃ v' → ? with |
---|
264 | [ VEmpty ⇒ λm_refl. λv_refl. |
---|
265 | ∀addr: addressing_mode. ∀p: is_in m fixed_v addr. |
---|
266 | Q addr ( |
---|
267 | match addr return λx: addressing_mode. is_in … fixed_v x → T with |
---|
268 | [ ADDR11 x ⇒ p_addr11 x |
---|
269 | | ADDR16 x ⇒ p_addr16 x |
---|
270 | | DIRECT x ⇒ p_direct x |
---|
271 | | INDIRECT x ⇒ p_indirect x |
---|
272 | | EXT_INDIRECT x ⇒ p_ext_indirect x |
---|
273 | | ACC_A ⇒ p_acc_a |
---|
274 | | REGISTER x ⇒ p_register x |
---|
275 | | ACC_B ⇒ p_acc_b |
---|
276 | | DPTR ⇒ p_dptr |
---|
277 | | DATA x ⇒ p_data x |
---|
278 | | DATA16 x ⇒ p_data16 x |
---|
279 | | ACC_DPTR ⇒ p_acc_dptr |
---|
280 | | ACC_PC ⇒ p_acc_pc |
---|
281 | | EXT_INDIRECT_DPTR ⇒ p_ext_indirect_dptr |
---|
282 | | INDIRECT_DPTR ⇒ p_indirect_dptr |
---|
283 | | CARRY ⇒ p_carry |
---|
284 | | BIT_ADDR x ⇒ p_bit_addr x |
---|
285 | | N_BIT_ADDR x ⇒ p_n_bit_addr x |
---|
286 | | RELATIVE x ⇒ p_relative x |
---|
287 | ] p) |
---|
288 | | VCons n' hd tl ⇒ λm_refl. λv_refl. |
---|
289 | let tail_call ≝ subaddressing_mode_elim_type T m fixed_v Q p_addr11 |
---|
290 | p_addr16 p_direct p_indirect p_ext_indirect p_acc_a |
---|
291 | p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr |
---|
292 | p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry |
---|
293 | p_bit_addr p_n_bit_addr p_relative n' tl ? |
---|
294 | in |
---|
295 | match hd return λa: addressing_mode_tag. a = hd → ? with |
---|
296 | [ addr11 ⇒ λhd_refl. (∀w. Q (ADDR11 w) (p_addr11 w ?)) → tail_call |
---|
297 | | addr16 ⇒ λhd_refl. (∀w. Q (ADDR16 w) (p_addr16 w ?)) → tail_call |
---|
298 | | direct ⇒ λhd_refl. (∀w. Q (DIRECT w) (p_direct w ?)) → tail_call |
---|
299 | | indirect ⇒ λhd_refl. (∀w. Q (INDIRECT w) (p_indirect w ?)) → tail_call |
---|
300 | | ext_indirect ⇒ λhd_refl. (∀w. Q (EXT_INDIRECT w) (p_ext_indirect w ?)) → tail_call |
---|
301 | | acc_a ⇒ λhd_refl. (Q ACC_A (p_acc_a ?)) → tail_call |
---|
302 | | registr ⇒ λhd_refl. (∀w. Q (REGISTER w) (p_register w ?)) → tail_call |
---|
303 | | acc_b ⇒ λhd_refl. (Q ACC_A (p_acc_b ?)) → tail_call |
---|
304 | | dptr ⇒ λhd_refl. (Q DPTR (p_dptr ?)) → tail_call |
---|
305 | | data ⇒ λhd_refl. (∀w. Q (DATA w) (p_data w ?)) → tail_call |
---|
306 | | data16 ⇒ λhd_refl. (∀w. Q (DATA16 w) (p_data16 w ?)) → tail_call |
---|
307 | | acc_dptr ⇒ λhd_refl. (Q ACC_DPTR (p_acc_dptr ?)) → tail_call |
---|
308 | | acc_pc ⇒ λhd_refl. (Q ACC_PC (p_acc_pc ?)) → tail_call |
---|
309 | | ext_indirect_dptr ⇒ λhd_refl. (Q EXT_INDIRECT_DPTR (p_ext_indirect_dptr ?)) → tail_call |
---|
310 | | indirect_dptr ⇒ λhd_refl. (Q INDIRECT_DPTR (p_indirect_dptr ?)) → tail_call |
---|
311 | | carry ⇒ λhd_refl. (Q CARRY (p_carry ?)) → tail_call |
---|
312 | | bit_addr ⇒ λhd_refl. (∀w. Q (BIT_ADDR w) (p_bit_addr w ?)) → tail_call |
---|
313 | | n_bit_addr ⇒ λhd_refl. (∀w. Q (N_BIT_ADDR w) (p_n_bit_addr w ?)) → tail_call |
---|
314 | | relative ⇒ λhd_refl. (∀w. Q (RELATIVE w) (p_relative w ?)) → tail_call |
---|
315 | ] (refl … hd) |
---|
316 | ] (refl … n) (refl_jmeq … v). |
---|
317 | [20: |
---|
318 | generalize in match proof; destruct |
---|
319 | whd in match (subvector_with … eq_a (hd:::tl) fixed_v); |
---|
320 | cases (mem … eq_a m fixed_v hd) normalize nodelta |
---|
321 | [1: |
---|
322 | whd in match (subvector_with … eq_a tl fixed_v); |
---|
323 | #assm assumption |
---|
324 | |2: |
---|
325 | normalize in ⊢ (% → ?); |
---|
326 | #absurd cases absurd |
---|
327 | ] |
---|
328 | ] |
---|
329 | @(is_in_subvector_is_in_supervector … proof) |
---|
330 | destruct @I |
---|
331 | qed. |
---|
332 | |
---|
333 | (* XXX: todo *) |
---|
334 | lemma subaddressing_mode_elim': |
---|
335 | ∀T: Type[2]. |
---|
336 | ∀n: nat. |
---|
337 | ∀o: nat. |
---|
338 | ∀Q: addressing_mode → T → Prop. |
---|
339 | ∀fixed_v: Vector addressing_mode_tag (n + o). |
---|
340 | ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19. |
---|
341 | ∀v1: Vector addressing_mode_tag n. |
---|
342 | ∀v2: Vector addressing_mode_tag o. |
---|
343 | ∀fixed_v_proof: fixed_v = v1 @@ v2. |
---|
344 | ∀subaddressing_mode_proof. |
---|
345 | subaddressing_mode_elim_type T (n + o) fixed_v Q P1 P2 P3 P4 P5 P6 P7 |
---|
346 | P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 (n + o) (v1 @@ v2) subaddressing_mode_proof. |
---|
347 | #T #n #o #Q #fixed_v #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10 |
---|
348 | #P11 #P12 #P13 #P14 #P15 #P16 #P17 #P18 #P19 #v1 #v2 #fixed_v_proof |
---|
349 | cases daemon |
---|
350 | qed. |
---|
351 | |
---|
352 | (* XXX: todo *) |
---|
353 | axiom subaddressing_mode_elim: |
---|
354 | ∀T: Type[2]. |
---|
355 | ∀m: nat. |
---|
356 | ∀n: nat. |
---|
357 | ∀Q: addressing_mode → T → Prop. |
---|
358 | ∀fixed_v: Vector addressing_mode_tag m. |
---|
359 | ∀P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19. |
---|
360 | ∀v: Vector addressing_mode_tag n. |
---|
361 | ∀proof. |
---|
362 | subaddressing_mode_elim_type T m fixed_v Q P1 P2 P3 P4 P5 P6 P7 |
---|
363 | P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 n v proof. |
---|
364 | |
---|
365 | definition current_instruction_is_labelled ≝ |
---|
366 | λcode_memory. |
---|
367 | λcost_labels: BitVectorTrie costlabel 16. |
---|
368 | λs: Status code_memory. |
---|
369 | let pc ≝ program_counter … code_memory s in |
---|
370 | match lookup_opt … pc cost_labels with |
---|
371 | [ None ⇒ False |
---|
372 | | _ ⇒ True |
---|
373 | ]. |
---|
374 | |
---|
375 | definition next_instruction_properly_relates_program_counters ≝ |
---|
376 | λcode_memory. |
---|
377 | λbefore: Status code_memory. |
---|
378 | λafter : Status code_memory. |
---|
379 | let size ≝ current_instruction_cost code_memory before in |
---|
380 | let pc_before ≝ program_counter … code_memory before in |
---|
381 | let pc_after ≝ program_counter … code_memory after in |
---|
382 | let sum ≝ \snd (half_add … pc_before (bitvector_of_nat … size)) in |
---|
383 | sum = pc_after. |
---|
384 | |
---|
385 | definition ASM_abstract_status: ∀code_memory. BitVectorTrie costlabel 16 → abstract_status ≝ |
---|
386 | λcode_memory. |
---|
387 | λcost_labels. |
---|
388 | mk_abstract_status |
---|
389 | (Status code_memory) |
---|
390 | (λs,s'. (execute_1 … s) = s') |
---|
391 | (λs,class. ASM_classify … s = class) |
---|
392 | (current_instruction_is_labelled … cost_labels) |
---|
393 | (next_instruction_properly_relates_program_counters code_memory). |
---|
394 | |
---|
395 | let rec trace_any_label_length |
---|
396 | (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16) |
---|
397 | (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory) |
---|
398 | (final_status: Status code_memory) |
---|
399 | (the_trace: trace_any_label (ASM_abstract_status code_memory cost_labels) trace_ends_flag start_status final_status) |
---|
400 | on the_trace: nat ≝ |
---|
401 | match the_trace with |
---|
402 | [ tal_base_not_return the_status _ _ _ _ ⇒ 0 |
---|
403 | | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒ 0 |
---|
404 | | tal_base_return the_status _ _ _ ⇒ 0 |
---|
405 | | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final _ _ _ call_trace _ final_trace ⇒ |
---|
406 | let tail_length ≝ trace_any_label_length … final_trace in |
---|
407 | let pc_difference ≝ nat_of_bitvector … (program_counter … after_fun_call) - nat_of_bitvector … (program_counter … pre_fun_call) in |
---|
408 | pc_difference + tail_length |
---|
409 | | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒ |
---|
410 | let tail_length ≝ trace_any_label_length … tail_trace in |
---|
411 | let pc_difference ≝ nat_of_bitvector … (program_counter … status_init) - nat_of_bitvector … (program_counter … status_pre) in |
---|
412 | pc_difference + tail_length |
---|
413 | ]. |
---|
414 | |
---|
415 | let rec compute_paid_trace_any_label |
---|
416 | (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16) |
---|
417 | (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory) |
---|
418 | (final_status: Status code_memory) |
---|
419 | (the_trace: trace_any_label (ASM_abstract_status code_memory cost_labels) trace_ends_flag start_status final_status) |
---|
420 | on the_trace: nat ≝ |
---|
421 | match the_trace with |
---|
422 | [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost … the_status |
---|
423 | | tal_base_return the_status _ _ _ ⇒ current_instruction_cost … the_status |
---|
424 | | tal_base_call pre_fun_call start_fun_call final _ _ _ call_trace _ ⇒ current_instruction_cost … pre_fun_call |
---|
425 | | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final |
---|
426 | _ _ _ call_trace _ final_trace ⇒ |
---|
427 | let current_instruction_cost ≝ current_instruction_cost … pre_fun_call in |
---|
428 | let final_trace_cost ≝ compute_paid_trace_any_label … cost_labels end_flag … final_trace in |
---|
429 | current_instruction_cost + final_trace_cost |
---|
430 | | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒ |
---|
431 | let current_instruction_cost ≝ current_instruction_cost … status_pre in |
---|
432 | let tail_trace_cost ≝ |
---|
433 | compute_paid_trace_any_label … cost_labels end_flag |
---|
434 | status_init status_end tail_trace |
---|
435 | in |
---|
436 | current_instruction_cost + tail_trace_cost |
---|
437 | ]. |
---|
438 | |
---|
439 | definition compute_paid_trace_label_label ≝ |
---|
440 | λcode_memory: BitVectorTrie Byte 16. |
---|
441 | λcost_labels: BitVectorTrie costlabel 16. |
---|
442 | λtrace_ends_flag: trace_ends_with_ret. |
---|
443 | λstart_status: Status code_memory. |
---|
444 | λfinal_status: Status code_memory. |
---|
445 | λthe_trace: trace_label_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status. |
---|
446 | match the_trace with |
---|
447 | [ tll_base ends_flag initial final given_trace labelled_proof ⇒ |
---|
448 | compute_paid_trace_any_label … given_trace |
---|
449 | ]. |
---|
450 | |
---|
451 | include alias "arithmetics/nat.ma". |
---|
452 | include alias "basics/logic.ma". |
---|
453 | |
---|
454 | lemma plus_right_monotone: |
---|
455 | ∀m, n, o: nat. |
---|
456 | m = n → m + o = n + o. |
---|
457 | #m #n #o #refl >refl % |
---|
458 | qed. |
---|
459 | |
---|
460 | lemma minus_plus_cancel: |
---|
461 | ∀m, n : nat. |
---|
462 | ∀proof: n ≤ m. |
---|
463 | (m - n) + n = m. |
---|
464 | #m #n #proof /2 by plus_minus/ |
---|
465 | qed. |
---|
466 | |
---|
467 | (* XXX: indexing bug *) |
---|
468 | lemma fetch_twice_fetch_execute_1: |
---|
469 | ∀code_memory: BitVectorTrie Byte 16. |
---|
470 | ∀start_status: Status code_memory. |
---|
471 | ASM_classify code_memory start_status = cl_other → |
---|
472 | \snd (\fst (fetch code_memory (program_counter … start_status))) = |
---|
473 | program_counter … (execute_1 … start_status). |
---|
474 | #code_memory #start_status #classify_assm |
---|
475 | whd in match execute_1; normalize nodelta |
---|
476 | cases (execute_1' code_memory start_status) #the_status |
---|
477 | * #_ #classify_assm' @classify_assm' assumption |
---|
478 | qed-. |
---|
479 | |
---|
480 | lemma reachable_program_counter_to_0_lt_total_program_size: |
---|
481 | ∀code_memory: BitVectorTrie Byte 16. |
---|
482 | ∀program_counter: Word. |
---|
483 | ∀total_program_size: nat. |
---|
484 | reachable_program_counter code_memory total_program_size program_counter → |
---|
485 | 0 < total_program_size. |
---|
486 | #code_memory #program_counter #total_program_size |
---|
487 | whd in match reachable_program_counter; normalize nodelta * * #some_n |
---|
488 | #_ cases (nat_of_bitvector 16 program_counter) |
---|
489 | [1: |
---|
490 | #assm assumption |
---|
491 | |2: |
---|
492 | #new_pc @ltn_to_ltO |
---|
493 | ] |
---|
494 | qed. |
---|
495 | |
---|
496 | lemma trace_compute_paid_trace_cl_other: |
---|
497 | ∀code_memory' : (BitVectorTrie Byte 16). |
---|
498 | ∀program_counter' : Word. |
---|
499 | ∀total_program_size : ℕ. |
---|
500 | ∀cost_labels : (BitVectorTrie costlabel 16). |
---|
501 | ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter'). |
---|
502 | ∀good_program_witness : (good_program code_memory' total_program_size). |
---|
503 | ∀program_size' : ℕ. |
---|
504 | ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter'). |
---|
505 | ∀ticks : ℕ. |
---|
506 | ∀instruction : instruction. |
---|
507 | ∀program_counter'' : Word. |
---|
508 | ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter'). |
---|
509 | ∀start_status : (Status code_memory'). |
---|
510 | ∀final_status : (Status code_memory'). |
---|
511 | ∀trace_ends_flag : trace_ends_with_ret. |
---|
512 | ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status). |
---|
513 | ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status). |
---|
514 | ∀classify_assm: ASM_classify0 instruction = cl_other. |
---|
515 | ∀pi1 : ℕ. |
---|
516 | (if match lookup_opt costlabel 16 program_counter'' cost_labels with |
---|
517 | [None ⇒ true |
---|
518 | |Some _ ⇒ false |
---|
519 | ] |
---|
520 | then |
---|
521 | ∀start_status0:Status code_memory'. |
---|
522 | ∀final_status0:Status code_memory'. |
---|
523 | ∀trace_ends_flag0:trace_ends_with_ret. |
---|
524 | ∀the_trace0:trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag0 start_status0 final_status0. |
---|
525 | program_counter'' = program_counter (BitVectorTrie Byte 16) code_memory' start_status0 → |
---|
526 | (∃n:ℕ |
---|
527 | .trace_any_label_length code_memory' cost_labels |
---|
528 | trace_ends_flag0 start_status0 final_status0 the_trace0 |
---|
529 | +S n |
---|
530 | =total_program_size) |
---|
531 | ∧pi1 |
---|
532 | =compute_paid_trace_any_label code_memory' cost_labels |
---|
533 | trace_ends_flag0 start_status0 final_status0 the_trace0 |
---|
534 | else (pi1=O) ) |
---|
535 | →(∃n:ℕ |
---|
536 | .trace_any_label_length code_memory' cost_labels trace_ends_flag |
---|
537 | start_status final_status the_trace |
---|
538 | +S n |
---|
539 | =total_program_size) |
---|
540 | ∧ticks+pi1 |
---|
541 | =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag |
---|
542 | start_status final_status the_trace. |
---|
543 | #code_memory' #program_counter' #total_program_size #cost_labels |
---|
544 | #reachable_program_counter_witness #good_program_witness |
---|
545 | #program_size' #recursive_case #ticks #instruction #program_counter'' #FETCH |
---|
546 | #start_status #final_status |
---|
547 | #trace_ends_flag #the_trace #program_counter_refl #classify_assm #recursive_block_cost |
---|
548 | #recursive_assm |
---|
549 | @(trace_any_label_inv_ind … the_trace) |
---|
550 | [5: |
---|
551 | #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label |
---|
552 | #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl |
---|
553 | #the_trace_refl |
---|
554 | destruct |
---|
555 | whd in match (trace_any_label_length … (tal_step_default …)); |
---|
556 | whd in match (compute_paid_trace_any_label … (tal_step_default …)); |
---|
557 | whd in costed_assm:(?%); |
---|
558 | generalize in match costed_assm; |
---|
559 | generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels)); |
---|
560 | generalize in match (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels) |
---|
561 | in ⊢ (??%? → ?(match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?); |
---|
562 | #lookup_assm cases lookup_assm |
---|
563 | [1: |
---|
564 | #None_lookup_opt_assm normalize nodelta #ignore |
---|
565 | generalize in match recursive_assm; |
---|
566 | cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' (execute_1 code_memory' status_pre))) |
---|
567 | [1: |
---|
568 | <fetch_twice_fetch_execute_1 |
---|
569 | [1: |
---|
570 | <FETCH % |
---|
571 | |2: |
---|
572 | >classifier_assm % |
---|
573 | ] |
---|
574 | |2: |
---|
575 | #program_counter_assm >program_counter_assm <None_lookup_opt_assm |
---|
576 | normalize nodelta #new_recursive_assm |
---|
577 | cases(new_recursive_assm (execute_1 code_memory' status_pre) status_end |
---|
578 | end_flag trace_any_label ?) [2: % ] |
---|
579 | #exists_assm #recursive_block_cost_assm % |
---|
580 | [1: |
---|
581 | cases exists_assm #exists_n #total_program_size_refl |
---|
582 | <total_program_size_refl |
---|
583 | %{(exists_n - current_instruction_cost … status_pre)} |
---|
584 | (* XXX: Christ knows what's going on with the rewrite tactic here? *) |
---|
585 | cases daemon |
---|
586 | |2: |
---|
587 | >recursive_block_cost_assm |
---|
588 | whd in match (current_instruction_cost … status_pre); |
---|
589 | cut(ticks = \snd (fetch code_memory' |
---|
590 | (program_counter (BitVectorTrie Byte 16) code_memory' status_pre))) |
---|
591 | [1: |
---|
592 | <FETCH % |
---|
593 | |2: |
---|
594 | #ticks_refl_assm >ticks_refl_assm % |
---|
595 | ] |
---|
596 | ] |
---|
597 | ] |
---|
598 | |2: |
---|
599 | #costlabel #Some_lookup_opt_assm <Some_lookup_opt_assm normalize nodelta |
---|
600 | #absurd cases absurd #absurd cases(absurd I) |
---|
601 | ] |
---|
602 | |1: |
---|
603 | #status_start #status_final #execute_assm #classifier_assm #costed_assm |
---|
604 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
605 | destruct |
---|
606 | whd in match (trace_any_label_length … (tal_base_not_return …)); |
---|
607 | whd in match (compute_paid_trace_any_label … (tal_base_not_return …)); |
---|
608 | whd in costed_assm; |
---|
609 | generalize in match costed_assm; |
---|
610 | generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels)); |
---|
611 | generalize in match (lookup_opt … (program_counter … (execute_1 code_memory' status_start)) cost_labels) |
---|
612 | in ⊢ (??%? → (match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?); |
---|
613 | #lookup_assm cases lookup_assm |
---|
614 | [1: |
---|
615 | #None_lookup_opt_assm normalize nodelta >None_lookup_opt_assm |
---|
616 | #absurd cases absurd |
---|
617 | |2: |
---|
618 | #costlabel #Some_lookup_opt_assm normalize nodelta #ignore |
---|
619 | generalize in match recursive_assm; |
---|
620 | cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' (execute_1 … status_start))) |
---|
621 | [1: |
---|
622 | <fetch_twice_fetch_execute_1 |
---|
623 | [1: |
---|
624 | <FETCH % |
---|
625 | |2: |
---|
626 | cases classifier_assm |
---|
627 | [1: |
---|
628 | whd in ⊢ (% → ?); |
---|
629 | whd in ⊢ (??%? → ?); |
---|
630 | whd in match (current_instruction code_memory' status_start); |
---|
631 | <FETCH generalize in match classify_assm; |
---|
632 | cases instruction |
---|
633 | [8: |
---|
634 | #preinstruction normalize nodelta |
---|
635 | whd in match ASM_classify0; normalize nodelta |
---|
636 | #contradiction >contradiction #absurd destruct(absurd) |
---|
637 | ] |
---|
638 | try(#addr1 #addr2 normalize nodelta #ignore #absurd destruct(absurd)) |
---|
639 | try(#addr normalize nodelta #ignore #absurd destruct(absurd)) |
---|
640 | normalize in ignore; destruct(ignore) |
---|
641 | |2: |
---|
642 | #classifier_assm' >classifier_assm' % |
---|
643 | ] |
---|
644 | ] |
---|
645 | |2: |
---|
646 | #program_counter_assm >program_counter_assm <Some_lookup_opt_assm |
---|
647 | normalize nodelta #new_recursive_assm >new_recursive_assm % |
---|
648 | [1: |
---|
649 | %{(pred total_program_size)} whd in ⊢ (??%?); |
---|
650 | @S_pred |
---|
651 | @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness) |
---|
652 | |2: |
---|
653 | cut(ticks = \snd (fetch code_memory' |
---|
654 | (program_counter (BitVectorTrie Byte 16) code_memory' status_start))) |
---|
655 | [1: |
---|
656 | <FETCH % |
---|
657 | |2: |
---|
658 | #ticks_refl_assm >ticks_refl_assm |
---|
659 | <plus_n_O % |
---|
660 | ] |
---|
661 | ] |
---|
662 | ] |
---|
663 | ] |
---|
664 | |2: |
---|
665 | #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm |
---|
666 | #start_status_refl #final_status_refl #the_trace_assm destruct @⊥ |
---|
667 | |3: |
---|
668 | #status_pre_fun_call #status_start_fun_call #status_final #execute_assm |
---|
669 | #classifier_assm #after_return_assm #trace_label_return #costed_assm |
---|
670 | #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
671 | destruct @⊥ |
---|
672 | |4: |
---|
673 | #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call |
---|
674 | #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return |
---|
675 | #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl |
---|
676 | #final_status_refl #the_trace_refl destruct @⊥ |
---|
677 | ] |
---|
678 | change with (ASM_classify0 ? = ?) in classifier_assm; |
---|
679 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
680 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
681 | <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) |
---|
682 | qed. |
---|
683 | |
---|
684 | lemma trace_compute_paid_trace_cl_jump: |
---|
685 | ∀code_memory': BitVectorTrie Byte 16. |
---|
686 | ∀program_counter': Word. |
---|
687 | ∀total_program_size: ℕ. |
---|
688 | ∀cost_labels: BitVectorTrie costlabel 16. |
---|
689 | ∀reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter'. |
---|
690 | ∀good_program_witness: good_program code_memory' total_program_size. |
---|
691 | ∀first_time_around: bool. |
---|
692 | ∀program_size': ℕ. |
---|
693 | ∀recursive_case: total_program_size ≤ S program_size'+nat_of_bitvector 16 program_counter'. |
---|
694 | ∀ticks: ℕ. |
---|
695 | ∀instruction: instruction. |
---|
696 | ∀program_counter'': Word. |
---|
697 | ∀FETCH: 〈instruction,program_counter'',ticks〉 = fetch code_memory' program_counter'. |
---|
698 | ∀start_status: (Status code_memory'). |
---|
699 | ∀final_status: (Status code_memory'). |
---|
700 | ∀trace_ends_flag: trace_ends_with_ret. |
---|
701 | ∀the_trace: (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status). |
---|
702 | ∀program_counter_refl: (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status). |
---|
703 | ∀classify_assm: ASM_classify0 instruction = cl_jump. |
---|
704 | (∃n:ℕ |
---|
705 | .trace_any_label_length code_memory' cost_labels trace_ends_flag |
---|
706 | start_status final_status the_trace |
---|
707 | +S n |
---|
708 | =total_program_size) |
---|
709 | ∧ticks |
---|
710 | =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag |
---|
711 | start_status final_status the_trace. |
---|
712 | #code_memory' #program_counter' #total_program_size #cost_labels |
---|
713 | #reachable_program_counter_witness #good_program_witness #first_time_around |
---|
714 | #program_size' #recursive_case #ticks #instruction #program_counter'' #FETCH |
---|
715 | #start_status #final_status |
---|
716 | #trace_ends_flag #the_trace #program_counter_refl #classify_assm |
---|
717 | @(trace_any_label_inv_ind … the_trace) |
---|
718 | [5: |
---|
719 | #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label |
---|
720 | #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl |
---|
721 | #the_trace_refl destruct @⊥ |
---|
722 | |1: |
---|
723 | #status_start #status_final #execute_assm #classifier_assm #costed_assm |
---|
724 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
725 | destruct |
---|
726 | whd in match (trace_any_label_length … (tal_base_not_return …)); |
---|
727 | whd in match (compute_paid_trace_any_label … (tal_base_not_return …)); % |
---|
728 | [1: |
---|
729 | %{(pred total_program_size)} whd in ⊢ (??%?); |
---|
730 | @S_pred |
---|
731 | @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness) |
---|
732 | |2: |
---|
733 | <FETCH % |
---|
734 | ] |
---|
735 | |2: |
---|
736 | #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm |
---|
737 | #start_status_refl #final_status_refl #the_trace_assm destruct @⊥ |
---|
738 | |3: |
---|
739 | #status_pre_fun_call #status_start_fun_call #status_final #execute_assm |
---|
740 | #classifier_assm #after_return_assm #trace_label_return #costed_assm |
---|
741 | #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
742 | destruct @⊥ |
---|
743 | |4: |
---|
744 | #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call |
---|
745 | #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return |
---|
746 | #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl |
---|
747 | #final_status_refl #the_trace_refl destruct @⊥ |
---|
748 | ] |
---|
749 | change with (ASM_classify0 ? = ?) in classifier_assm; |
---|
750 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
751 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
752 | <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) |
---|
753 | qed. |
---|
754 | |
---|
755 | lemma trace_compute_paid_trace_cl_call: |
---|
756 | ∀code_memory' : (BitVectorTrie Byte 16). |
---|
757 | ∀program_counter' : Word. |
---|
758 | ∀total_program_size : ℕ. |
---|
759 | ∀cost_labels : (BitVectorTrie costlabel 16). |
---|
760 | ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter'). |
---|
761 | ∀good_program_witness : (good_program code_memory' total_program_size). |
---|
762 | ∀program_size' : ℕ. |
---|
763 | ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter'). |
---|
764 | ∀ticks : ℕ. |
---|
765 | ∀instruction : instruction. |
---|
766 | ∀program_counter'' : Word. |
---|
767 | ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter'). |
---|
768 | ∀start_status : (Status code_memory'). |
---|
769 | ∀final_status : (Status code_memory'). |
---|
770 | ∀trace_ends_flag : trace_ends_with_ret. |
---|
771 | ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status). |
---|
772 | ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status). |
---|
773 | ∀classify_assm: ASM_classify0 instruction = cl_call. |
---|
774 | (∀pi1:ℕ |
---|
775 | .if match lookup_opt costlabel 16 program_counter'' cost_labels with |
---|
776 | [None ⇒ true | Some _ ⇒ false] |
---|
777 | then (∀start_status0:Status code_memory' |
---|
778 | .∀final_status0:Status code_memory' |
---|
779 | .∀trace_ends_flag0:trace_ends_with_ret |
---|
780 | .∀the_trace0:trace_any_label |
---|
781 | (ASM_abstract_status code_memory' cost_labels) |
---|
782 | trace_ends_flag0 start_status0 final_status0 |
---|
783 | .program_counter'' |
---|
784 | =program_counter (BitVectorTrie Byte 16) code_memory' start_status0 |
---|
785 | →(∃n:ℕ |
---|
786 | .trace_any_label_length code_memory' cost_labels trace_ends_flag0 |
---|
787 | start_status0 final_status0 the_trace0 |
---|
788 | +S n |
---|
789 | =total_program_size) |
---|
790 | ∧pi1 |
---|
791 | =compute_paid_trace_any_label code_memory' cost_labels |
---|
792 | trace_ends_flag0 start_status0 final_status0 the_trace0) |
---|
793 | else (pi1=O) |
---|
794 | →(∃n:ℕ |
---|
795 | .trace_any_label_length code_memory' cost_labels trace_ends_flag |
---|
796 | start_status final_status the_trace |
---|
797 | +S n |
---|
798 | =total_program_size) |
---|
799 | ∧ticks+pi1 |
---|
800 | =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag |
---|
801 | start_status final_status the_trace). |
---|
802 | #code_memory' #program_counter' #total_program_size #cost_labels |
---|
803 | #reachable_program_counter_witness #good_program_witness #program_size' |
---|
804 | #recursive_case #ticks #instruction #program_counter'' #FETCH |
---|
805 | #start_status #final_status #trace_ends_flag |
---|
806 | #the_trace #program_counter_refl #classify_assm #recursive_block_cost #recursive_assm |
---|
807 | @(trace_any_label_inv_ind … the_trace) |
---|
808 | [5: |
---|
809 | #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label |
---|
810 | #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl |
---|
811 | #the_trace_refl destruct @⊥ |
---|
812 | |1: |
---|
813 | #status_start #status_final #execute_assm #classifier_assm #costed_assm |
---|
814 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
815 | destruct @⊥ |
---|
816 | |2: |
---|
817 | #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm |
---|
818 | #start_status_refl #final_status_refl #the_trace_assm destruct @⊥ |
---|
819 | |3: |
---|
820 | #status_pre_fun_call #status_start_fun_call #status_final #execute_assm |
---|
821 | #classifier_assm #after_return_assm #trace_label_return #costed_assm |
---|
822 | #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
823 | destruct |
---|
824 | whd in match (trace_any_label_length … (tal_base_call …)); |
---|
825 | whd in match (compute_paid_trace_any_label … (tal_base_call …)); |
---|
826 | whd in costed_assm; |
---|
827 | generalize in match costed_assm; |
---|
828 | generalize in match (refl … (lookup_opt … (program_counter … status_final) cost_labels)); |
---|
829 | generalize in match (lookup_opt … (program_counter … status_final) cost_labels) |
---|
830 | in ⊢ (??%? → (match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?); |
---|
831 | #lookup_assm cases lookup_assm |
---|
832 | [1: |
---|
833 | #None_lookup_opt normalize nodelta #absurd cases absurd |
---|
834 | |2: |
---|
835 | #costlabel #Some_lookup_opt normalize nodelta #ignore |
---|
836 | generalize in match recursive_assm; |
---|
837 | cut(program_counter'' = (program_counter (BitVectorTrie Byte 16) code_memory' status_final)) |
---|
838 | [1: |
---|
839 | whd in after_return_assm; <after_return_assm; (* XXX: here *) |
---|
840 | cases daemon |
---|
841 | |2: |
---|
842 | #program_counter_assm >program_counter_assm <Some_lookup_opt |
---|
843 | normalize nodelta #new_recursive_assm >new_recursive_assm % |
---|
844 | [1: |
---|
845 | %{(pred total_program_size)} whd in ⊢ (??%?); |
---|
846 | @S_pred |
---|
847 | @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness) |
---|
848 | |2: |
---|
849 | cut(ticks = \snd (fetch code_memory' (program_counter (BitVectorTrie Byte 16) code_memory' status_pre_fun_call))) |
---|
850 | [1: |
---|
851 | <FETCH % |
---|
852 | |2: |
---|
853 | #ticks_refl_assm >ticks_refl_assm |
---|
854 | <plus_n_O % |
---|
855 | ] |
---|
856 | ] |
---|
857 | ] |
---|
858 | ] |
---|
859 | |4: |
---|
860 | #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call |
---|
861 | #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return |
---|
862 | #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl |
---|
863 | #final_status_refl #the_trace_refl destruct |
---|
864 | whd in match (trace_any_label_length … (tal_step_call …)); |
---|
865 | whd in match (compute_paid_trace_any_label … (tal_step_call …)); |
---|
866 | whd in costed_assm:(?%); |
---|
867 | generalize in match costed_assm; |
---|
868 | generalize in match (refl … (lookup_opt … (program_counter … status_after_fun_call) cost_labels)); |
---|
869 | generalize in match (lookup_opt … (program_counter … status_after_fun_call) cost_labels) |
---|
870 | in ⊢ (??%? → ?(match % with [ _ ⇒ ? | _ ⇒ ? ]) → ?); |
---|
871 | #lookup_assm cases lookup_assm |
---|
872 | [1: |
---|
873 | #None_lookup_opt_assm normalize nodelta #ignore |
---|
874 | generalize in match recursive_assm; |
---|
875 | cut(program_counter'' = program_counter … status_after_fun_call) |
---|
876 | [1: |
---|
877 | cases daemon |
---|
878 | |2: |
---|
879 | #program_counter_refl >program_counter_refl <None_lookup_opt_assm |
---|
880 | normalize nodelta #new_recursive_assm % |
---|
881 | cases (new_recursive_assm … trace_any_label ?) |
---|
882 | [1,3: |
---|
883 | #exists_assm #recursive_block_cost_assm |
---|
884 | [2: |
---|
885 | >recursive_block_cost_assm |
---|
886 | @plus_right_monotone |
---|
887 | whd in ⊢ (???%); <FETCH % |
---|
888 | |1: |
---|
889 | (* |
---|
890 | cases exists_assm #recursive_n #new_exists_assm |
---|
891 | <new_exists_assm |
---|
892 | |
---|
893 | % |
---|
894 | [1: |
---|
895 | @(recursive_n - current_instruction_cost … status_pre_fun_call) |
---|
896 | |2: |
---|
897 | |
---|
898 | cut(current_instruction_cost … status_pre_fun_call = |
---|
899 | (nat_of_bitvector 16 |
---|
900 | (program_counter (BitVectorTrie Byte 16) code_memory' status_after_fun_call) |
---|
901 | -nat_of_bitvector 16 |
---|
902 | (program_counter (BitVectorTrie Byte 16) code_memory' status_pre_fun_call))) |
---|
903 | [1: |
---|
904 | |2: |
---|
905 | #eq_assm <eq_assm -eq_assm -new_exists_assm -recursive_block_cost_assm |
---|
906 | <plus_n_Sm <plus_n_Sm @eq_f |
---|
907 | >associative_plus in ⊢ (??%?); >commutative_plus in ⊢ (??%?); |
---|
908 | >associative_plus @eq_f |
---|
909 | <plus_minus_m_m [1: % ] |
---|
910 | ] |
---|
911 | ] *) |
---|
912 | cases daemon |
---|
913 | ] |
---|
914 | |2,4: |
---|
915 | % |
---|
916 | ] |
---|
917 | ] |
---|
918 | |2: |
---|
919 | #cost_label #Some_lookup_opt_assm normalize nodelta #absurd |
---|
920 | cases absurd #absurd cases (absurd I) |
---|
921 | ] |
---|
922 | ] |
---|
923 | try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;) |
---|
924 | try (change with (ASM_classify0 ? = ?) in classifier_assm;) |
---|
925 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
926 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
927 | <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) cases absurd |
---|
928 | #absurd destruct(absurd) |
---|
929 | qed. |
---|
930 | |
---|
931 | lemma trace_compute_paid_trace_cl_return: |
---|
932 | ∀code_memory' : (BitVectorTrie Byte 16). |
---|
933 | ∀program_counter' : Word. |
---|
934 | ∀total_program_size : ℕ. |
---|
935 | ∀cost_labels : (BitVectorTrie costlabel 16). |
---|
936 | ∀reachable_program_counter_witness : (reachable_program_counter code_memory' total_program_size program_counter'). |
---|
937 | ∀good_program_witness : (good_program code_memory' total_program_size). |
---|
938 | ∀program_size' : ℕ. |
---|
939 | ∀recursive_case : (total_program_size≤S program_size'+nat_of_bitvector 16 program_counter'). |
---|
940 | ∀ticks : ℕ. |
---|
941 | ∀instruction : instruction. |
---|
942 | ∀program_counter'' : Word. |
---|
943 | ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter'). |
---|
944 | ∀start_status : (Status code_memory'). |
---|
945 | ∀final_status : (Status code_memory'). |
---|
946 | ∀trace_ends_flag : trace_ends_with_ret. |
---|
947 | ∀the_trace : (trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status). |
---|
948 | ∀program_counter_refl : (program_counter' = program_counter (BitVectorTrie Byte 16) code_memory' start_status). |
---|
949 | ∀classify_assm: ASM_classify0 instruction = cl_return. |
---|
950 | (∃n:ℕ |
---|
951 | .trace_any_label_length code_memory' cost_labels trace_ends_flag |
---|
952 | start_status final_status the_trace |
---|
953 | +S n |
---|
954 | =total_program_size) |
---|
955 | ∧ticks |
---|
956 | =compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag |
---|
957 | start_status final_status the_trace. |
---|
958 | #code_memory' #program_counter' #total_program_size #cost_labels |
---|
959 | #reachable_program_counter_witness #good_program_witness #program_size' |
---|
960 | #recursive_case #ticks #instruction #program_counter'' #FETCH |
---|
961 | #start_status #final_status #trace_ends_flag |
---|
962 | #the_trace #program_counter_refl #classify_assm |
---|
963 | @(trace_any_label_inv_ind … the_trace) |
---|
964 | [1: |
---|
965 | #start_status' #final_status' #execute_assm #classifier_assm #costed_assm |
---|
966 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
967 | destruct @⊥ |
---|
968 | |2: |
---|
969 | #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_flag_refl |
---|
970 | #start_status_refl #final_status_refl #the_trace_refl destruct |
---|
971 | whd in match (trace_any_label_length … (tal_base_return …)); |
---|
972 | whd in match (compute_paid_trace_any_label … (tal_base_return …)); % |
---|
973 | [1: |
---|
974 | %{(pred total_program_size)} whd in ⊢ (??%?); |
---|
975 | @S_pred |
---|
976 | @(reachable_program_counter_to_0_lt_total_program_size … reachable_program_counter_witness) |
---|
977 | |2: |
---|
978 | <FETCH % |
---|
979 | ] |
---|
980 | |3: |
---|
981 | #status_pre_fun_call #status_start_fun_call #status_final #execute_assm |
---|
982 | #classifier_assm #after_return_assm #trace_label_return #costed_assm |
---|
983 | #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl |
---|
984 | destruct @⊥ |
---|
985 | |4: |
---|
986 | #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call |
---|
987 | #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return |
---|
988 | #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl |
---|
989 | #final_status_refl #the_trace_refl |
---|
990 | destruct @⊥ |
---|
991 | |5: |
---|
992 | #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label |
---|
993 | #classifier_assm #costed_assm #trace_ends_flag_refl #start_status_refl |
---|
994 | #final_status_refl #the_trace_refl destruct @⊥ |
---|
995 | ] |
---|
996 | try (change with (ASM_classify0 ? = ? ∨ ASM_classify0 ? = ?) in classifier_assm;) |
---|
997 | try (change with (ASM_classify0 ? = ?) in classifier_assm;) |
---|
998 | whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm; |
---|
999 | whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm; |
---|
1000 | <FETCH in classifier_assm; >classify_assm |
---|
1001 | #absurd try (destruct(absurd)) |
---|
1002 | cases absurd |
---|
1003 | #absurd destruct(absurd) |
---|
1004 | qed. |
---|
1005 | |
---|
1006 | |
---|
1007 | let rec block_cost' |
---|
1008 | (code_memory': BitVectorTrie Byte 16) (program_counter': Word) |
---|
1009 | (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16) |
---|
1010 | (reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter') |
---|
1011 | (good_program_witness: good_program code_memory' total_program_size) (first_time_around: bool) |
---|
1012 | on program_size: |
---|
1013 | total_program_size ≤ program_size + nat_of_bitvector … program_counter' → |
---|
1014 | Σcost_of_block: nat. |
---|
1015 | if (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) then |
---|
1016 | ∀start_status: Status code_memory'. |
---|
1017 | ∀final_status: Status code_memory'. |
---|
1018 | ∀trace_ends_flag. |
---|
1019 | ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status. |
---|
1020 | program_counter' = program_counter … start_status → |
---|
1021 | (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧ |
---|
1022 | cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace |
---|
1023 | else |
---|
1024 | (cost_of_block = 0) ≝ |
---|
1025 | match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter' → ? with |
---|
1026 | [ O ⇒ λbase_case. ⊥ |
---|
1027 | | S program_size' ⇒ λrecursive_case. |
---|
1028 | let 〈instruction, program_counter'', ticks〉 as FETCH ≝ fetch code_memory' program_counter' in |
---|
1029 | let to_continue ≝ |
---|
1030 | match lookup_opt … program_counter' cost_labels with |
---|
1031 | [ None ⇒ true |
---|
1032 | | Some _ ⇒ first_time_around |
---|
1033 | ] |
---|
1034 | in |
---|
1035 | ((if to_continue then |
---|
1036 | pi1 … (match instruction return λx. x = instruction → ? with |
---|
1037 | [ RealInstruction real_instruction ⇒ λreal_instruction_refl. |
---|
1038 | match real_instruction return λx. x = real_instruction → |
---|
1039 | Σcost_of_block: nat. |
---|
1040 | ∀start_status: Status code_memory'. |
---|
1041 | ∀final_status: Status code_memory'. |
---|
1042 | ∀trace_ends_flag. |
---|
1043 | ∀the_trace: trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status. |
---|
1044 | program_counter' = program_counter … start_status → |
---|
1045 | (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧ |
---|
1046 | cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace with |
---|
1047 | [ RET ⇒ λinstr. ticks |
---|
1048 | | RETI ⇒ λinstr. ticks |
---|
1049 | | JC relative ⇒ λinstr. ticks |
---|
1050 | | JNC relative ⇒ λinstr. ticks |
---|
1051 | | JB bit_addr relative ⇒ λinstr. ticks |
---|
1052 | | JNB bit_addr relative ⇒ λinstr. ticks |
---|
1053 | | JBC bit_addr relative ⇒ λinstr. ticks |
---|
1054 | | JZ relative ⇒ λinstr. ticks |
---|
1055 | | JNZ relative ⇒ λinstr. ticks |
---|
1056 | | CJNE src_trgt relative ⇒ λinstr. ticks |
---|
1057 | | DJNZ src_trgt relative ⇒ λinstr. ticks |
---|
1058 | | _ ⇒ λinstr. |
---|
1059 | |
---|
1060 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
1061 | ] (refl …) |
---|
1062 | | ACALL addr ⇒ λinstr. |
---|
1063 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
1064 | | AJMP addr ⇒ λinstr. ticks |
---|
1065 | | LCALL addr ⇒ λinstr. |
---|
1066 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
1067 | | LJMP addr ⇒ λinstr. ticks |
---|
1068 | | SJMP addr ⇒ λinstr. ticks |
---|
1069 | | JMP addr ⇒ λinstr. (* XXX: actually a call due to use with fptrs *) |
---|
1070 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
1071 | | MOVC src trgt ⇒ λinstr. |
---|
1072 | ticks + block_cost' code_memory' program_counter'' program_size' total_program_size cost_labels ? good_program_witness false ? |
---|
1073 | ] (refl …)) |
---|
1074 | else |
---|
1075 | 0) |
---|
1076 | : Σcost_of_block: nat. |
---|
1077 | match (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) with |
---|
1078 | [ true ⇒ |
---|
1079 | ∀start_status: Status code_memory'. |
---|
1080 | ∀final_status: Status code_memory'. |
---|
1081 | ∀trace_ends_flag. |
---|
1082 | ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status. |
---|
1083 | program_counter' = program_counter … start_status → |
---|
1084 | (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧ |
---|
1085 | cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace |
---|
1086 | | false ⇒ |
---|
1087 | (cost_of_block = 0) |
---|
1088 | ]) |
---|
1089 | ]. |
---|
1090 | [1: |
---|
1091 | cases reachable_program_counter_witness #_ #hyp |
---|
1092 | @(absurd (total_program_size < total_program_size) … (not_le_Sn_n …)) |
---|
1093 | @(le_to_lt_to_lt … base_case hyp) |
---|
1094 | |2: |
---|
1095 | change with (if to_continue then ? else (? = 0)) |
---|
1096 | >p in ⊢ (match % return ? with [ _ ⇒ ? | _ ⇒ ? ]); normalize nodelta |
---|
1097 | @pi2 |
---|
1098 | |3: |
---|
1099 | change with (if to_continue then ? else (0 = 0)) |
---|
1100 | >p normalize nodelta % |
---|
1101 | |7,8: |
---|
1102 | #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl |
---|
1103 | @(trace_compute_paid_trace_cl_return … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl) |
---|
1104 | destruct % |
---|
1105 | |96,102,105: |
---|
1106 | #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl |
---|
1107 | cases(block_cost' ?????????) -block_cost' |
---|
1108 | @(trace_compute_paid_trace_cl_call … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … program_counter_refl) |
---|
1109 | destruct % |
---|
1110 | |4,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,69,72,75,78,81,84,87, |
---|
1111 | 90,93: |
---|
1112 | #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl |
---|
1113 | cases(block_cost' ?????????) -block_cost' |
---|
1114 | @(trace_compute_paid_trace_cl_other … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl) |
---|
1115 | destruct % |
---|
1116 | |60,61,62,63,64,65,66,67,68,69,99,101,100,102: |
---|
1117 | #start_status #final_status #trace_ends_flag #the_trace #program_counter_refl |
---|
1118 | @(trace_compute_paid_trace_cl_jump … reachable_program_counter_witness good_program_witness … recursive_case … FETCH … the_trace program_counter_refl) |
---|
1119 | destruct % |
---|
1120 | |103: |
---|
1121 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1122 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1123 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1124 | @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr |
---|
1125 | * * * * #n' |
---|
1126 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
1127 | % |
---|
1128 | [1: |
---|
1129 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1130 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1131 | >(le_to_leb_true … program_counter_lt') % |
---|
1132 | |2: |
---|
1133 | assumption |
---|
1134 | ] |
---|
1135 | |104: |
---|
1136 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1137 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1138 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1139 | @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr |
---|
1140 | * * * * #n' |
---|
1141 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
1142 | @(transitive_le |
---|
1143 | total_program_size |
---|
1144 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
1145 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
1146 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
1147 | >plus_n_Sm |
---|
1148 | @monotonic_le_plus_r |
---|
1149 | change with ( |
---|
1150 | nat_of_bitvector … program_counter' < |
---|
1151 | nat_of_bitvector … program_counter'') |
---|
1152 | assumption |
---|
1153 | |106: |
---|
1154 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1155 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1156 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1157 | @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr |
---|
1158 | cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta |
---|
1159 | cases (split … 3 8 new_addr) #thr #eig normalize nodelta |
---|
1160 | cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n' |
---|
1161 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
1162 | % |
---|
1163 | [1: |
---|
1164 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1165 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1166 | >(le_to_leb_true … program_counter_lt') % |
---|
1167 | |2: |
---|
1168 | assumption |
---|
1169 | ] |
---|
1170 | |107: |
---|
1171 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1172 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1173 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1174 | @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr |
---|
1175 | cases (split … 8 8 program_counter'') #pc_bu #pc_bl normalize nodelta |
---|
1176 | cases (split … 3 8 new_addr) #thr #eig normalize nodelta |
---|
1177 | cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n' |
---|
1178 | #_ #_ #program_counter_lt' #program_counter_lt_tps' |
---|
1179 | @(transitive_le |
---|
1180 | total_program_size |
---|
1181 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
1182 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
1183 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
1184 | >plus_n_Sm |
---|
1185 | @monotonic_le_plus_r |
---|
1186 | change with ( |
---|
1187 | nat_of_bitvector … program_counter' < |
---|
1188 | nat_of_bitvector … program_counter'') |
---|
1189 | assumption |
---|
1190 | |94,97: |
---|
1191 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1192 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1193 | <FETCH normalize nodelta <instr normalize nodelta * |
---|
1194 | #program_counter_lt' #program_counter_lt_tps' % |
---|
1195 | [1,3: |
---|
1196 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1197 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1198 | >(le_to_leb_true … program_counter_lt') % |
---|
1199 | |2,4: |
---|
1200 | assumption |
---|
1201 | ] |
---|
1202 | |5,10,12,13,16,18,19,22,25,28,31,34,37,40,43,46,49,52,55,58,70,73, |
---|
1203 | 76,79,82,85,88,91: |
---|
1204 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1205 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1206 | <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta * |
---|
1207 | #program_counter_lt' #program_counter_lt_tps' % |
---|
1208 | try assumption |
---|
1209 | %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta |
---|
1210 | <FETCH normalize nodelta whd in match ltb; normalize nodelta |
---|
1211 | >(le_to_leb_true … program_counter_lt') % |
---|
1212 | (* XXX: got to here *) |
---|
1213 | |6,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,71,74,77,80,83,86,89, |
---|
1214 | 92: |
---|
1215 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1216 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1217 | <FETCH normalize nodelta |
---|
1218 | <real_instruction_refl <instr normalize nodelta * |
---|
1219 | #pc_pc_lt_hyp' #pc_tps_lt_hyp' |
---|
1220 | @(transitive_le |
---|
1221 | total_program_size |
---|
1222 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
1223 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
1224 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
1225 | >plus_n_Sm |
---|
1226 | @monotonic_le_plus_r |
---|
1227 | change with ( |
---|
1228 | nat_of_bitvector … program_counter' < |
---|
1229 | nat_of_bitvector … program_counter'') |
---|
1230 | assumption |
---|
1231 | |95,98: |
---|
1232 | cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp |
---|
1233 | lapply(good_program_witness program_counter' reachable_program_counter_witness) |
---|
1234 | <FETCH normalize nodelta <instr normalize nodelta |
---|
1235 | try(<real_instruction_refl <instr normalize nodelta) * |
---|
1236 | #pc_pc_lt_hyp' #pc_tps_lt_hyp' |
---|
1237 | @(transitive_le |
---|
1238 | total_program_size |
---|
1239 | ((S program_size') + nat_of_bitvector … program_counter') |
---|
1240 | (program_size' + nat_of_bitvector … program_counter'') recursive_case) |
---|
1241 | normalize in match (S program_size' + nat_of_bitvector … program_counter'); |
---|
1242 | >plus_n_Sm |
---|
1243 | @monotonic_le_plus_r |
---|
1244 | change with ( |
---|
1245 | nat_of_bitvector … program_counter' < |
---|
1246 | nat_of_bitvector … program_counter'') |
---|
1247 | assumption |
---|
1248 | ] |
---|
1249 | qed. |
---|
1250 | |
---|
1251 | definition block_cost: |
---|
1252 | ∀code_memory': BitVectorTrie Byte 16. |
---|
1253 | ∀program_counter': Word. |
---|
1254 | ∀total_program_size: nat. |
---|
1255 | ∀cost_labels: BitVectorTrie costlabel 16. |
---|
1256 | ∀reachable_program_counter_witness: reachable_program_counter code_memory' total_program_size program_counter'. |
---|
1257 | ∀good_program_witness: good_program code_memory' total_program_size. |
---|
1258 | Σcost_of_block: nat. |
---|
1259 | ∀start_status: Status code_memory'. |
---|
1260 | ∀final_status: Status code_memory'. |
---|
1261 | ∀trace_ends_flag. |
---|
1262 | ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status. |
---|
1263 | program_counter' = program_counter … start_status → |
---|
1264 | (∃n: nat. trace_any_label_length … the_trace + (S n) = total_program_size) ∧ |
---|
1265 | cost_of_block = compute_paid_trace_any_label code_memory' cost_labels trace_ends_flag start_status final_status the_trace ≝ |
---|
1266 | λcode_memory: BitVectorTrie Byte 16. |
---|
1267 | λprogram_counter: Word. |
---|
1268 | λtotal_program_size: nat. |
---|
1269 | λcost_labels: BitVectorTrie costlabel 16. |
---|
1270 | λreachable_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter. |
---|
1271 | λgood_program_witness: good_program code_memory total_program_size. ?. |
---|
1272 | cases(block_cost' code_memory program_counter total_program_size total_program_size cost_labels |
---|
1273 | reachable_program_counter_witness good_program_witness true ?) |
---|
1274 | [1: |
---|
1275 | #cost_of_block #block_cost_hyp |
---|
1276 | %{cost_of_block} |
---|
1277 | cases(lookup_opt … cost_labels) in block_cost_hyp; |
---|
1278 | [2: #cost_label] normalize nodelta |
---|
1279 | #hyp assumption |
---|
1280 | |2: |
---|
1281 | @le_plus_n_r |
---|
1282 | ] |
---|
1283 | qed. |
---|
1284 | |
---|
1285 | lemma fetch_program_counter_n_Sn: |
---|
1286 | ∀instruction: instruction. |
---|
1287 | ∀program_counter, program_counter': Word. |
---|
1288 | ∀ticks, n: nat. |
---|
1289 | ∀code_memory: BitVectorTrie Byte 16. |
---|
1290 | Some … program_counter = fetch_program_counter_n n code_memory (zero 16) → |
---|
1291 | 〈instruction,program_counter',ticks〉 = fetch code_memory program_counter → |
---|
1292 | nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' → |
---|
1293 | Some … program_counter' = fetch_program_counter_n (S n) code_memory (zero …). |
---|
1294 | #instruction #program_counter #program_counter' #ticks #n #code_memory |
---|
1295 | #fetch_program_counter_n_hyp #fetch_hyp #lt_hyp |
---|
1296 | whd in match (fetch_program_counter_n (S n) code_memory (zero …)); |
---|
1297 | <fetch_program_counter_n_hyp normalize nodelta |
---|
1298 | <fetch_hyp normalize nodelta |
---|
1299 | change with ( |
---|
1300 | leb (S (nat_of_bitvector … program_counter)) (nat_of_bitvector … program_counter') |
---|
1301 | ) in match (ltb (nat_of_bitvector … program_counter) (nat_of_bitvector … program_counter')); |
---|
1302 | >(le_to_leb_true … lt_hyp) % |
---|
1303 | qed. |
---|
1304 | |
---|
1305 | (* XXX: to be moved into common/Identifiers.ma *) |
---|
1306 | lemma lookup_present_add_hit: |
---|
1307 | ∀tag, A, map, k, v, k_pres. |
---|
1308 | lookup_present tag A (add … map k v) k k_pres = v. |
---|
1309 | #tag #a #map #k #v #k_pres |
---|
1310 | lapply (lookup_lookup_present … (add … map k v) … k_pres) |
---|
1311 | >lookup_add_hit #Some_assm destruct(Some_assm) |
---|
1312 | <e0 % |
---|
1313 | qed. |
---|
1314 | |
---|
1315 | lemma lookup_present_add_miss: |
---|
1316 | ∀tag, A, map, k, k', v, k_pres', k_pres''. |
---|
1317 | k' ≠ k → |
---|
1318 | lookup_present tag A (add … map k v) k' k_pres' = lookup_present tag A map k' k_pres''. |
---|
1319 | #tag #A #map #k #k' #v #k_pres' #k_pres'' #neq_assm |
---|
1320 | lapply (lookup_lookup_present … (add … map k v) ? k_pres') |
---|
1321 | >lookup_add_miss try assumption |
---|
1322 | #Some_assm |
---|
1323 | lapply (lookup_lookup_present … map k') >Some_assm #Some_assm' |
---|
1324 | lapply (Some_assm' k_pres'') #Some_assm'' destruct assumption |
---|
1325 | qed. |
---|
1326 | |
---|
1327 | (* XXX: to be moved into basics/types.ma *) |
---|
1328 | lemma not_None_to_Some: |
---|
1329 | ∀A: Type[0]. |
---|
1330 | ∀o: option A. |
---|
1331 | o ≠ None A → ∃v: A. o = Some A v. |
---|
1332 | #A #o cases o |
---|
1333 | [1: |
---|
1334 | #absurd cases absurd #absurd' cases (absurd' (refl …)) |
---|
1335 | |2: |
---|
1336 | #v' #ignore /2/ |
---|
1337 | ] |
---|
1338 | qed. |
---|
1339 | |
---|
1340 | lemma present_add_present: |
---|
1341 | ∀tag, a, map, k, k', v. |
---|
1342 | k' ≠ k → |
---|
1343 | present tag a (add tag a map k v) k' → |
---|
1344 | present tag a map k'. |
---|
1345 | #tag #a #map #k #k' #v #neq_hyp #present_hyp |
---|
1346 | whd in match present; normalize nodelta |
---|
1347 | whd in match present in present_hyp; normalize nodelta in present_hyp; |
---|
1348 | cases (not_None_to_Some a … present_hyp) #v' #Some_eq_hyp |
---|
1349 | lapply (lookup_add_cases tag ?????? Some_eq_hyp) * |
---|
1350 | [1: |
---|
1351 | * #k_eq_hyp @⊥ /2/ |
---|
1352 | |2: |
---|
1353 | #Some_eq_hyp' /2/ |
---|
1354 | ] |
---|
1355 | qed. |
---|
1356 | |
---|
1357 | lemma present_add_hit: |
---|
1358 | ∀tag, a, map, k, v. |
---|
1359 | present tag a (add tag a map k v) k. |
---|
1360 | #tag #a #map #k #v |
---|
1361 | whd >lookup_add_hit |
---|
1362 | % #absurd destruct |
---|
1363 | qed. |
---|
1364 | |
---|
1365 | lemma present_add_miss: |
---|
1366 | ∀tag, a, map, k, k', v. |
---|
1367 | k' ≠ k → present tag a map k' → present tag a (add tag a map k v) k'. |
---|
1368 | #tag #a #map #k #k' #v #neq_assm #present_assm |
---|
1369 | whd >lookup_add_miss assumption |
---|
1370 | qed. |
---|
1371 | |
---|
1372 | let rec traverse_code_internal |
---|
1373 | (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16) |
---|
1374 | (program_counter: Word) (fixed_program_size: nat) (program_size: nat) |
---|
1375 | (reachable_program_counter_witness: |
---|
1376 | ∀lbl: costlabel. |
---|
1377 | ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels → |
---|
1378 | reachable_program_counter code_memory fixed_program_size program_counter) |
---|
1379 | (good_program_witness: good_program code_memory fixed_program_size) |
---|
1380 | on program_size: |
---|
1381 | Σcost_map: identifier_map CostTag nat. |
---|
1382 | (∀pc,k. nat_of_bitvector … program_counter < nat_of_bitvector 16 pc → nat_of_bitvector … pc < program_size + nat_of_bitvector … program_counter → lookup_opt … pc cost_labels = Some … k → present … cost_map k) ∧ |
---|
1383 | (∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k → |
---|
1384 | ∃reachable_witness: reachable_program_counter code_memory fixed_program_size pc. |
---|
1385 | pi1 … (block_cost code_memory pc fixed_program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres) ≝ |
---|
1386 | match program_size with |
---|
1387 | [ O ⇒ empty_map … |
---|
1388 | | S program_size' ⇒ pi1 … |
---|
1389 | |
---|
1390 | (let 〈instruction, new_program_counter, ticks〉 ≝ fetch … code_memory program_counter in |
---|
1391 | let cost_mapping ≝ traverse_code_internal code_memory cost_labels new_program_counter fixed_program_size program_size' ? good_program_witness in |
---|
1392 | match lookup_opt … program_counter cost_labels return λx. x = lookup_opt … program_counter cost_labels → Σcost_map: ?. ? with |
---|
1393 | [ None ⇒ λlookup_refl. pi1 … cost_mapping |
---|
1394 | | Some lbl ⇒ λlookup_refl. |
---|
1395 | let cost ≝ block_cost code_memory program_counter fixed_program_size cost_labels ? good_program_witness in |
---|
1396 | add … cost_mapping lbl cost |
---|
1397 | ] (refl … (lookup_opt … program_counter cost_labels))) |
---|
1398 | ]. |
---|
1399 | [2: |
---|
1400 | @pi2 |
---|
1401 | |4: |
---|
1402 | @(reachable_program_counter_witness lbl) |
---|
1403 | assumption |
---|
1404 | |3: |
---|
1405 | assumption |
---|
1406 | |1: |
---|
1407 | % |
---|
1408 | [1: |
---|
1409 | #pc #k #absurd1 #absurd2 |
---|
1410 | @⊥ lapply(lt_to_not_le … absurd1) #absurd |
---|
1411 | cases absurd #absurd @absurd |
---|
1412 | @(transitive_le … absurd2) @le_S @le_n |
---|
1413 | |2: |
---|
1414 | #k #k_pres |
---|
1415 | @⊥ normalize in k_pres; /2/ |
---|
1416 | ] |
---|
1417 | |5: |
---|
1418 | % |
---|
1419 | [2: |
---|
1420 | #k #k_pres |
---|
1421 | @(eq_identifier_elim … k lbl) |
---|
1422 | [1: |
---|
1423 | #eq_assm %{program_counter} #lookup_opt_assm |
---|
1424 | %{(reachable_program_counter_witness …)} try assumption |
---|
1425 | >eq_assm in k_pres ⊢ (???%); #k_pres >lookup_present_add_hit % |
---|
1426 | |2: |
---|
1427 | #neq_assm |
---|
1428 | cases cost_mapping in k_pres; #cost_mapping' #ind_hyp #present_assm |
---|
1429 | cases ind_hyp #_ #relevant cases (relevant k ?) |
---|
1430 | [2: |
---|
1431 | @(present_add_present … present_assm) assumption |
---|
1432 | |1: |
---|
1433 | #program_counter' #ind_hyp' %{program_counter'} |
---|
1434 | #relevant cases (ind_hyp' relevant) #reachable_witness' |
---|
1435 | #ind_hyp'' %{reachable_witness'} >ind_hyp'' |
---|
1436 | @sym_eq @lookup_present_add_miss assumption |
---|
1437 | ] |
---|
1438 | ] |
---|
1439 | |1: |
---|
1440 | #pc #k #H1 #H2 #lookup_opt_assm @(eq_identifier_elim … k lbl) |
---|
1441 | [1: |
---|
1442 | #eq_assm >eq_assm |
---|
1443 | cases cost_mapping #cost_mapping' * #ind_hyp #_ |
---|
1444 | @present_add_hit |
---|
1445 | |2: |
---|
1446 | #neq_assm @present_add_miss try assumption |
---|
1447 | cases cost_mapping #cost_mapping' * #ind_hyp #_ |
---|
1448 | cases daemon (* XXX: !!! *) |
---|
1449 | ] |
---|
1450 | ] |
---|
1451 | |6: |
---|
1452 | ] |
---|
1453 | qed. |
---|
1454 | |
---|
1455 | definition traverse_code: |
---|
1456 | ∀code_memory: BitVectorTrie Byte 16. |
---|
1457 | ∀cost_labels: BitVectorTrie costlabel 16. |
---|
1458 | ∀program_size: nat. |
---|
1459 | ∀reachable_program_counter_witness: |
---|
1460 | ∀lbl: costlabel. |
---|
1461 | ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels → |
---|
1462 | reachable_program_counter code_memory program_size program_counter. |
---|
1463 | ∀good_program_witness: good_program code_memory program_size. |
---|
1464 | Σcost_map: identifier_map CostTag nat. |
---|
1465 | (∀k. ∀k_pres:present … cost_map k. ∃pc. lookup_opt … pc cost_labels = Some … k → |
---|
1466 | ∃reachable_witness: reachable_program_counter code_memory program_size pc. |
---|
1467 | pi1 … (block_cost code_memory pc program_size cost_labels reachable_witness good_program_witness) = lookup_present … k_pres) ≝ |
---|
1468 | λcode_memory: BitVectorTrie Byte 16. |
---|
1469 | λcost_labels: BitVectorTrie costlabel 16. |
---|
1470 | λprogram_size: nat. |
---|
1471 | λreachable_program_counter_witness: |
---|
1472 | ∀lbl: costlabel. |
---|
1473 | ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels → |
---|
1474 | reachable_program_counter code_memory program_size program_counter. |
---|
1475 | λgood_program_witness: good_program code_memory program_size. |
---|
1476 | traverse_code_internal code_memory cost_labels (zero …) program_size program_size reachable_program_counter_witness good_program_witness. |
---|
1477 | |
---|
1478 | definition compute_costs ≝ |
---|
1479 | λprogram: list Byte. |
---|
1480 | λcost_labels: BitVectorTrie costlabel 16. |
---|
1481 | λreachable_program_witness. |
---|
1482 | λgood_program_witness: good_program (load_code_memory program) (|program| + 1). |
---|
1483 | let program_size ≝ |program| + 1 in |
---|
1484 | let code_memory ≝ load_code_memory program in |
---|
1485 | traverse_code code_memory cost_labels program_size reachable_program_witness ?. |
---|
1486 | @good_program_witness |
---|
1487 | qed. |
---|