source: src/ASM/ASMCosts.ma @ 1650

Last change on this file since 1650 was 1650, checked in by mulligan, 7 years ago

changes over the last couple of days: stuck due to matita producing terms so large that the program cannot print them out!

File size: 27.5 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Interpret.ma".
5include "common/StructuredTraces.ma".
6
7let 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   
24definition 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   
31definition 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
107qed.
108       
109lemma is_a_decidable:
110  ∀hd.
111  ∀element.
112    is_a hd element = true ∨ is_a hd element = false.
113  #hd #element //
114qed.
115
116lemma 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 //
123qed.
124
125lemma 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)
140qed.
141 
142lemma 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  ]
179qed.
180
181lemma 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  ]
224qed.
225
226let 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 
235let 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
327qed.
328
329lemma 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
345qed.
346
347axiom 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
359include alias "arithmetics/nat.ma".
360
361let rec block_cost'
362  (code_memory: BitVectorTrie Byte 16) (program_counter: Word)
363    (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
364      (reachable_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter)
365        (good_program_witness: good_program code_memory total_program_size) (first_time_around: bool)
366          on program_size: total_program_size ≤ program_size + nat_of_bitvector … program_counter → nat ≝
367  match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter → nat with
368  [ O ⇒ λbase_case. ⊥
369  | S program_size' ⇒ λrecursive_case.
370    let 〈instruction, program_counter', ticks〉 as FETCH ≝ fetch code_memory program_counter in
371    let to_continue ≝
372      match lookup_opt … program_counter' cost_labels with
373      [ None ⇒ true
374      | Some _ ⇒ first_time_around
375      ]
376    in
377      if to_continue then
378        match instruction return λx. x = instruction → ? with
379        [ RealInstruction real_instruction ⇒ λreal_instruction_refl.
380          match real_instruction return λx. x = real_instruction → ? with
381          [ RET                    ⇒ λinstr. ticks
382          | JC   relative          ⇒ λinstr. ticks
383          | JNC  relative          ⇒ λinstr. ticks
384          | JB   bit_addr relative ⇒ λinstr. ticks
385          | JNB  bit_addr relative ⇒ λinstr. ticks
386          | JBC  bit_addr relative ⇒ λinstr. ticks
387          | JZ   relative          ⇒ λinstr. ticks
388          | JNZ  relative          ⇒ λinstr. ticks
389          | CJNE src_trgt relative ⇒ λinstr. ticks
390          | DJNZ src_trgt relative ⇒ λinstr. ticks
391          | _                      ⇒ λinstr.
392              ticks + block_cost' code_memory program_counter' program_size' total_program_size cost_labels ? good_program_witness false ?
393          ] (refl … real_instruction)
394        | ACALL addr     ⇒ λinstr.
395            ticks + block_cost' code_memory program_counter' program_size' total_program_size cost_labels ? good_program_witness false ?
396        | AJMP  addr     ⇒ λinstr. ticks
397        | LCALL addr     ⇒ λinstr.
398            ticks + block_cost' code_memory program_counter' program_size' total_program_size cost_labels ? good_program_witness false ?
399        | LJMP  addr     ⇒ λinstr. ticks
400        | SJMP  addr     ⇒ λinstr. ticks
401        | JMP   addr     ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
402            ticks + block_cost' code_memory program_counter' program_size' total_program_size cost_labels ? good_program_witness false ?
403        | MOVC  src trgt ⇒ λinstr.
404            ticks + block_cost' code_memory program_counter' program_size' total_program_size cost_labels ? good_program_witness false ?
405        ] (refl … instruction)
406      else
407        0
408  ].
409  [1:
410    cases reachable_program_counter_witness #_ #hyp
411    @(absurd (total_program_size < total_program_size) … (not_le_Sn_n …))
412    @(le_to_lt_to_lt … base_case hyp)
413  |2:
414    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
415    lapply(good_program_witness program_counter reachable_program_counter_witness)
416      <FETCH normalize nodelta <instr normalize nodelta
417    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
418    cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta
419    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
420    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
421    #_ #_ #program_counter_lt' #program_counter_lt_tps'     
422    @(transitive_le
423      total_program_size
424      ((S program_size') + nat_of_bitvector … program_counter)
425      (program_size' + nat_of_bitvector … program_counter') recursive_case)
426    normalize in match (S program_size' + nat_of_bitvector … program_counter);
427    >plus_n_Sm
428    @monotonic_le_plus_r
429    change with (
430      nat_of_bitvector … program_counter <
431        nat_of_bitvector … program_counter')
432    assumption
433  |3:
434    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
435    lapply(good_program_witness program_counter reachable_program_counter_witness)
436      <FETCH normalize nodelta <instr normalize nodelta
437    @(subaddressing_mode_elim … [[addr11]] … [[addr11]]) [1: // ] #new_addr
438    cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta
439    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
440    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta * * * * #n'
441    #_ #_ #program_counter_lt' #program_counter_lt_tps'
442    %
443    [1:
444      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
445      <FETCH normalize nodelta whd in match ltb; normalize nodelta
446      >(le_to_leb_true … program_counter_lt') %
447    |2:
448      assumption
449    ]
450  |4:
451    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
452    lapply(good_program_witness program_counter reachable_program_counter_witness)
453      <FETCH normalize nodelta <instr normalize nodelta
454    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
455    * * * * #n'
456    #_ #_ #program_counter_lt' #program_counter_lt_tps'     
457    @(transitive_le
458      total_program_size
459      ((S program_size') + nat_of_bitvector … program_counter)
460      (program_size' + nat_of_bitvector … program_counter') recursive_case)
461    normalize in match (S program_size' + nat_of_bitvector … program_counter);
462    >plus_n_Sm
463    @monotonic_le_plus_r
464    change with (
465      nat_of_bitvector … program_counter <
466        nat_of_bitvector … program_counter')
467    assumption
468  |5:
469    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
470    lapply(good_program_witness program_counter reachable_program_counter_witness)
471      <FETCH normalize nodelta <instr normalize nodelta
472    @(subaddressing_mode_elim … [[addr16]] … [[addr16]]) [1: // ] #new_addr
473    * * * * #n'
474    #_ #_ #program_counter_lt' #program_counter_lt_tps'
475    %
476    [1:
477      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
478      <FETCH normalize nodelta whd in match ltb; normalize nodelta
479      >(le_to_leb_true … program_counter_lt') %
480    |2:
481      assumption
482    ]
483  |6,8: (* JMP and MOVC *)
484    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
485    lapply(good_program_witness program_counter reachable_program_counter_witness)
486    <FETCH normalize nodelta <instr normalize nodelta
487    try(<real_instruction_refl <instr normalize nodelta) *
488    #pc_pc_lt_hyp' #pc_tps_lt_hyp'
489    @(transitive_le
490      total_program_size
491      ((S program_size') + nat_of_bitvector … program_counter)
492      (program_size' + nat_of_bitvector … program_counter') recursive_case)
493    normalize in match (S program_size' + nat_of_bitvector … program_counter);
494    >plus_n_Sm
495    @monotonic_le_plus_r
496    change with (
497      nat_of_bitvector … program_counter <
498        nat_of_bitvector … program_counter')
499    assumption
500  |7,9:
501    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
502    lapply(good_program_witness program_counter reachable_program_counter_witness)
503    <FETCH normalize nodelta <instr normalize nodelta *
504    #program_counter_lt' #program_counter_lt_tps' %
505    [1,3:
506      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
507      <FETCH normalize nodelta whd in match ltb; normalize nodelta
508      >(le_to_leb_true … program_counter_lt') %
509    |2,4:
510      assumption
511    ]
512  |11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,
513    55,57,59,61,63:
514    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
515    lapply(good_program_witness program_counter reachable_program_counter_witness)
516    <FETCH normalize nodelta <real_instruction_refl <instr normalize nodelta *
517    #program_counter_lt' #program_counter_lt_tps' %
518    try assumption
519    [*:
520      %{(S n)} whd in ⊢ (???%); <fetch_n_hyp normalize nodelta
521      <FETCH normalize nodelta whd in match ltb; normalize nodelta
522      >(le_to_leb_true … program_counter_lt') %
523    ]
524  |10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,
525    54,56,58,60,62:
526    cases reachable_program_counter_witness * #n #fetch_n_hyp #lt_hyp
527    lapply(good_program_witness program_counter reachable_program_counter_witness)
528    <FETCH normalize nodelta
529    <real_instruction_refl <instr normalize nodelta *
530    #pc_pc_lt_hyp' #pc_tps_lt_hyp'
531    @(transitive_le
532      total_program_size
533      ((S program_size') + nat_of_bitvector … program_counter)
534      (program_size' + nat_of_bitvector … program_counter') recursive_case)
535    normalize in match (S program_size' + nat_of_bitvector … program_counter);
536    >plus_n_Sm
537    @monotonic_le_plus_r
538    change with (
539      nat_of_bitvector … program_counter <
540        nat_of_bitvector … program_counter')
541    assumption
542  ]
543qed.
544
545definition block_cost ≝
546  λcode_memory: BitVectorTrie Byte 16.
547  λprogram_counter: Word.
548  λtotal_program_size: nat.
549  λcost_labels: BitVectorTrie costlabel 16.
550  λreachable_program_counter_witness: reachable_program_counter code_memory total_program_size program_counter.
551  λgood_program_witness: good_program code_memory total_program_size.
552    block_cost' code_memory program_counter total_program_size total_program_size cost_labels
553      reachable_program_counter_witness good_program_witness true ?.
554  @le_plus_n_r
555qed.
556
557lemma fetch_program_counter_n_Sn:
558  ∀instruction: instruction.
559  ∀program_counter, program_counter': Word.
560  ∀ticks, n: nat.
561  ∀code_memory: BitVectorTrie Byte 16.
562    Some … program_counter = fetch_program_counter_n n code_memory (zero 16) →
563      〈instruction,program_counter',ticks〉 = fetch code_memory program_counter →
564        nat_of_bitvector … program_counter < nat_of_bitvector … program_counter' →
565          Some … program_counter' = fetch_program_counter_n (S n) code_memory (zero …).
566  #instruction #program_counter #program_counter' #ticks #n #code_memory
567  #fetch_program_counter_n_hyp #fetch_hyp #lt_hyp
568  whd in match (fetch_program_counter_n (S n) code_memory (zero …));
569  <fetch_program_counter_n_hyp normalize nodelta
570  <fetch_hyp normalize nodelta
571  change with (
572    leb (S (nat_of_bitvector … program_counter)) (nat_of_bitvector … program_counter')
573  ) in match (ltb (nat_of_bitvector … program_counter) (nat_of_bitvector … program_counter'));
574  >(le_to_leb_true … lt_hyp) %
575qed.
576
577let rec traverse_code_internal
578  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
579    (program_counter: Word) (fixed_program_size: nat) (program_size: nat)
580      (reachable_program_counter_witness:
581          ∀lbl: costlabel.
582          ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels →
583            reachable_program_counter code_memory fixed_program_size program_counter)
584        (good_program_witness: good_program code_memory fixed_program_size)
585        on program_size: identifier_map CostTag nat ≝
586  match program_size with
587  [ O ⇒ empty_map …
588  | S program_size ⇒
589    let 〈instruction, new_program_counter, ticks〉 as FETCH ≝ fetch … code_memory program_counter in
590    let cost_mapping ≝ traverse_code_internal code_memory cost_labels new_program_counter fixed_program_size program_size ? good_program_witness in
591    match lookup_opt … program_counter cost_labels return λx. x = lookup_opt … program_counter cost_labels → ? with
592    [ None ⇒ λlookup_refl. cost_mapping
593    | Some lbl ⇒ λlookup_refl.
594      let cost ≝ block_cost code_memory program_counter fixed_program_size cost_labels ? good_program_witness in
595        add … cost_mapping lbl cost
596    ] (refl … (lookup_opt … program_counter cost_labels))
597  ].
598  [1:
599    @(reachable_program_counter_witness lbl)
600    assumption
601  |2:
602    assumption
603  ]
604qed.
605
606definition traverse_code ≝
607  λcode_memory: BitVectorTrie Byte 16.
608  λcost_labels: BitVectorTrie costlabel 16.
609  λprogram_size: nat.
610  λreachable_program_counter_witness:
611          ∀lbl: costlabel.
612          ∀program_counter: Word. Some … lbl = lookup_opt … program_counter cost_labels →
613            reachable_program_counter code_memory program_size program_counter.
614  λgood_program_witness: good_program code_memory program_size.
615    traverse_code_internal code_memory cost_labels (zero …) program_size program_size reachable_program_counter_witness good_program_witness.
616 
617definition compute_costs ≝
618  λprogram: list Byte.
619  λcost_labels: BitVectorTrie costlabel 16.
620  λreachable_program_witness.
621  λgood_program_witness: good_program (load_code_memory program) (|program| + 1).
622    let program_size ≝ |program| + 1 in
623    let code_memory ≝ load_code_memory program in
624      traverse_code code_memory cost_labels program_size reachable_program_witness ?.
625  @good_program_witness
626qed.
Note: See TracBrowser for help on using the repository browser.