source: src/ASM/ASMCosts.ma @ 1623

Last change on this file since 1623 was 1623, checked in by mulligan, 8 years ago

strange matita issue

File size: 22.2 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Interpret.ma".
5include "common/StructuredTraces.ma".
6
7definition current_instruction0 ≝
8  λmem,pc. \fst (\fst (fetch … mem pc)).
9
10definition current_instruction ≝
11 λs:Status. current_instruction0 (code_memory … s) (program_counter … s).
12
13definition ASM_classify0: instruction → status_class ≝
14 λi.
15  match i with
16   [ RealInstruction pre ⇒
17     match pre with
18      [ RET ⇒ cl_return
19      | JZ _ ⇒ cl_jump
20      | JNZ _ ⇒ cl_jump
21      | JC _ ⇒ cl_jump
22      | JNC _ ⇒ cl_jump
23      | JB _ _ ⇒ cl_jump
24      | JNB _ _ ⇒ cl_jump
25      | JBC _ _ ⇒ cl_jump
26      | CJNE _ _ ⇒ cl_jump
27      | DJNZ _ _ ⇒ cl_jump
28      | _ ⇒ cl_other
29      ]
30   | ACALL _ ⇒ cl_call
31   | LCALL _ ⇒ cl_call
32   | JMP _ ⇒ cl_call
33   | AJMP _ ⇒ cl_jump
34   | LJMP _ ⇒ cl_jump
35   | SJMP _ ⇒ cl_jump
36   | _ ⇒ cl_other
37   ].
38
39definition ASM_classify: Status → status_class ≝
40 λs.ASM_classify0 (current_instruction s).
41
42definition current_instruction_is_labelled ≝
43  λcost_labels: BitVectorTrie costlabel 16.
44  λs: Status.
45  let pc ≝ program_counter … s in
46    match lookup_opt … pc cost_labels with
47    [ None ⇒ False
48    | _    ⇒ True
49    ].
50
51definition label_of_current_instruction:
52 BitVectorTrie costlabel 16 → Status → list costlabel
53 ≝
54  λcost_labels,s.
55  let pc ≝ program_counter … s in
56    match lookup_opt … pc cost_labels with
57    [ None ⇒ []
58    | Some l ⇒ [l]
59    ].
60
61definition next_instruction_properly_relates_program_counters ≝
62  λbefore: Status.
63  λafter : Status.
64  let size ≝ current_instruction_cost before in
65  let pc_before ≝ program_counter … before in
66  let pc_after ≝ program_counter … after in
67  let sum ≝ \snd (half_add … pc_before (bitvector_of_nat … size)) in
68    sum = pc_after.
69
70definition ASM_abstract_status: BitVectorTrie costlabel 16 → abstract_status ≝
71 λcost_labels.
72  mk_abstract_status
73   Status
74   (λs,s'. (execute_1 s) = s')
75   (λs,class. ASM_classify s = class)
76   (current_instruction_is_labelled cost_labels)
77   next_instruction_properly_relates_program_counters.
78
79(* To be moved in ASM/arithmetic.ma *)
80definition addr16_of_addr11: Word → Word11 → Word ≝
81  λpc: Word.
82  λa: Word11.
83  let 〈pc_upper, ignore〉 ≝ split … 8 8 pc in
84  let 〈n1, n2〉 ≝ split … 4 4 pc_upper in
85  let 〈b123, b〉 ≝ split … 3 8 a in
86  let b1 ≝ get_index_v … b123 0 ? in
87  let b2 ≝ get_index_v … b123 1 ? in
88  let b3 ≝ get_index_v … b123 2 ? in
89  let p5 ≝ get_index_v … n2 0 ? in
90    (n1 @@ [[ p5; b1; b2; b3 ]]) @@ b.
91  //
92qed.
93   
94definition good_program_counter: BitVectorTrie Byte 16 → Word → nat → Prop ≝
95  λcode_memory: BitVectorTrie Byte 16.
96  λprogram_counter: Word.
97  λprogram_size: nat.
98    ∃n: nat.
99      let tail_program_counter ≝ fetch_program_counter_n n code_memory (zero 16) in
100        program_counter = fetch_program_counter_n (S n) code_memory (zero 16) ∧
101          nat_of_bitvector 16 program_counter ≤ program_size ∧
102            nat_of_bitvector 16 tail_program_counter < nat_of_bitvector 16 program_counter.
103
104definition good_program: BitVectorTrie Byte 16 → Word → nat → Prop ≝
105  λcode_memory: BitVectorTrie Byte 16.
106  λprogram_counter: Word.
107  λtotal_program_size: nat.
108  let 〈instruction, program_counter, ticks〉 ≝ fetch code_memory program_counter in
109    match instruction with
110    [ RealInstruction instr ⇒
111      match instr with
112      [ RET                    ⇒ True
113      | JC   relative          ⇒ True (* XXX: see below *)
114      | JNC  relative          ⇒ True (* XXX: see below *)
115      | JB   bit_addr relative ⇒ True
116      | JNB  bit_addr relative ⇒ True
117      | JBC  bit_addr relative ⇒ True
118      | JZ   relative          ⇒ True
119      | JNZ  relative          ⇒ True
120      | CJNE src_trgt relative ⇒ True
121      | DJNZ src_trgt relative ⇒ True
122      | _                      ⇒
123          good_program_counter code_memory program_counter total_program_size
124      ]
125    | LCALL addr         ⇒
126      match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with
127      [ ADDR16 addr ⇒ λaddr16: True.
128          good_program_counter code_memory addr total_program_size ∧
129            good_program_counter code_memory program_counter total_program_size
130      | _ ⇒ λother: False. ⊥
131      ] (subaddressing_modein … addr)
132    | ACALL addr         ⇒
133      match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with
134      [ ADDR11 addr ⇒ λaddr11: True.
135        let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter in
136        let 〈thr, eig〉 ≝ split … 3 8 addr in
137        let 〈fiv, thr'〉 ≝ split … 5 3 pc_bu in
138        let new_program_counter ≝ (fiv @@ thr) @@ pc_bl in
139          good_program_counter code_memory new_program_counter total_program_size ∧
140            good_program_counter code_memory program_counter total_program_size
141      | _ ⇒ λother: False. ⊥
142      ] (subaddressing_modein … addr)
143    | AJMP  addr         ⇒
144      match addr return λx. bool_to_Prop (is_in … [[ addr11 ]] x) → Prop with
145      [ ADDR11 addr ⇒ λaddr11: True.
146        let 〈pc_bu, pc_bl〉 ≝ split … 8 8 program_counter in
147        let 〈nu, nl〉 ≝ split … 4 4 pc_bu in
148        let bit ≝ get_index' … O ? nl in
149        let 〈relevant1, relevant2〉 ≝ split … 3 8 addr in
150        let new_addr ≝ (nu @@ (bit ::: relevant1)) @@ relevant2 in
151        let 〈carry, new_program_counter〉 ≝ half_add 16 program_counter new_addr in
152          (good_program_counter code_memory new_program_counter total_program_size) ∧
153            (good_program_counter code_memory program_counter total_program_size)
154      | _ ⇒ λother: False. ⊥
155      ] (subaddressing_modein … addr)
156    | LJMP  addr         ⇒
157      match addr return λx. bool_to_Prop (is_in … [[ addr16 ]] x) → Prop with
158      [ ADDR16 addr ⇒ λaddr16: True.
159          good_program_counter code_memory addr total_program_size ∧
160            good_program_counter code_memory program_counter total_program_size
161      | _ ⇒ λother: False. ⊥
162      ] (subaddressing_modein … addr)
163    | SJMP  addr     ⇒
164      match addr return λx. bool_to_Prop (is_in … [[ relative ]] x) → Prop with
165      [ RELATIVE addr ⇒ λrelative: True.
166        let 〈carry, new_program_counter〉 ≝ half_add … program_counter (sign_extension addr) in
167          good_program_counter code_memory new_program_counter total_program_size ∧
168            good_program_counter code_memory program_counter total_program_size
169      | _ ⇒ λother: False. ⊥
170      ] (subaddressing_modein … addr)
171    | JMP   addr     ⇒ True (* XXX: should recurse here, but dptr in JMP ... *)
172    | MOVC  src trgt ⇒
173        good_program_counter code_memory program_counter total_program_size
174    ].
175  cases other
176qed.
177
178let rec member_addressing_mode_tag
179  (n: nat) (v: Vector addressing_mode_tag n) (a: addressing_mode_tag)
180    on v: Prop ≝
181  match v with
182  [ VEmpty ⇒ False
183  | VCons n' hd tl ⇒
184      bool_to_Prop (eq_a hd a) ∨ member_addressing_mode_tag n' tl a
185  ].
186 
187let rec subaddressing_mode_elim_type
188  (T: Type[2]) (n: nat) (v: Vector addressing_mode_tag n)
189    (Q: addressing_mode → T → Prop)
190      on v:
191    (∀w: Word11. is_in n v (ADDR11 w)            → T) →
192    (∀w: Word. is_in n v (ADDR16 w)            → T) →
193    (∀w: Byte. is_in n v (DIRECT w)            → T) →
194    (∀w: Bit. is_in n v (INDIRECT w)          → T) →
195    (∀w: Bit. is_in n v (EXT_INDIRECT w)      → T) →
196    (              is_in n v ACC_A             → T) →
197    (∀w: BitVector 3. is_in n v (REGISTER w)           → T) →
198    (              is_in n v ACC_B             → T) →
199    (              is_in n v DPTR              → T) →
200    (∀w: Byte. is_in n v (DATA w)              → T) →
201    (∀w: Word. is_in n v (DATA16 w)            → T) →
202    (              is_in n v ACC_DPTR          → T) →
203    (              is_in n v ACC_PC            → T) →
204    (              is_in n v EXT_INDIRECT_DPTR → T) →
205    (              is_in n v INDIRECT_DPTR     → T) →
206    (              is_in n v CARRY            → T) →
207    (∀w: Byte. is_in n v (BIT_ADDR w)          → T) →
208    (∀w: Byte. is_in n v (N_BIT_ADDR w)        → T) →
209    (∀w: Byte. is_in n v (RELATIVE w)          → T) → Prop ≝
210  match v return λm: nat. λv': Vector addressing_mode_tag m. m = n → v ≃ v' → ? with
211  [ VEmpty         ⇒ λm_refl. λv_refl.
212    λp_addr11. λp_addr16. λp_direct. λp_indirect. λp_ext_indirect. λp_acc_a.
213    λp_register. λp_acc_b. λp_dptr. λp_data. λp_data16. λp_acc_dptr. λp_acc_pc.
214    λp_ext_indirect_dptr. λp_indirect_dptr. λp_carry. λp_bit_addr.
215    λp_n_bit_addr. λp_relative.
216      ∀addr. ∀p: is_in … v addr.
217        Q addr (
218        match addr return λx:addressing_mode. is_in n v x → T with 
219        [ ADDR11 x ⇒ p_addr11 x
220        | ADDR16 x ⇒ p_addr16 x
221        | DIRECT x ⇒ p_direct x
222        | INDIRECT x ⇒ p_indirect x
223        | EXT_INDIRECT x ⇒ p_ext_indirect x
224        | ACC_A ⇒ p_acc_a
225        | REGISTER x ⇒ p_register x
226        | ACC_B ⇒ p_acc_b
227        | DPTR ⇒ p_dptr
228        | DATA x ⇒ p_data x
229        | DATA16 x ⇒ p_data16 x
230        | ACC_DPTR ⇒ p_acc_dptr
231        | ACC_PC ⇒ p_acc_pc
232        | EXT_INDIRECT_DPTR ⇒ p_ext_indirect_dptr
233        | INDIRECT_DPTR ⇒ p_indirect_dptr
234        | CARRY ⇒ p_carry
235        | BIT_ADDR x ⇒ p_bit_addr x
236        | N_BIT_ADDR x ⇒ p_n_bit_addr x
237        | RELATIVE x ⇒ p_relative x
238        ] p)
239  | VCons n' hd tl ⇒ λm_refl. λv_refl.
240    λp_addr11. λp_addr16: (∀w: Word. is_in n v (ADDR16 w)            → T).
241    λp_direct. λp_indirect. λp_ext_indirect. λp_acc_a. λp_register. λp_acc_b.
242    λp_dptr. λp_data. λp_data16. λp_acc_dptr. λp_acc_pc. λp_ext_indirect_dptr.
243    λp_indirect_dptr. λp_carry. λp_bit_addr. λp_n_bit_addr. λp_relative.
244    let tail_call ≝ subaddressing_mode_elim_type T n' tl Q (? p_addr11)
245      (? p_addr16) (? p_direct) (? p_indirect) (? p_ext_indirect) (? p_acc_a)
246        (? p_register) (? p_acc_b) (? p_dptr) (? p_data) (? p_data16) (? p_acc_dptr)
247          (? p_acc_pc) (? p_ext_indirect_dptr) (? p_indirect_dptr) (? p_carry)
248            (? p_bit_addr) (? p_n_bit_addr) (? p_relative)
249(*
250     (subaddressing_mode_elim_type T n' tl Q (λw. λH. p_addr11 w ?) (λw. λH. p_addr16 w ?)
251     (λw. λH. p_direct w ?) (λw. λH. p_indirect w ?) (λw. λH. p_ext_indirect w ?)
252      (λH. p_acc_a ?) (λw. λH. p_register w ?) (λH. p_acc_b ?) (λH. p_dptr ?)
253       (λw. λH. p_data w ?) (λw. λH. p_data16 w ?) (λH. p_acc_dptr ?) (λH. p_acc_pc ?)
254        (λH. p_ext_indirect_dptr ?) (λH. p_indirect_dptr ?) (λH. p_carry ?) (λw. λH. p_bit_addr w ?)
255         (λw. λH. p_n_bit_addr w ?) (λw. λH. p_relative w ?))*)
256    in
257    match hd return λa: addressing_mode_tag. a = hd → ? with
258    [ addr11 ⇒ λhd_refl. (∀w. Q (ADDR11 w) (p_addr11 w ?)) → tail_call
259    | addr16 ⇒ λhd_refl. (∀w. Q (ADDR16 w) (p_addr16 w ?)) → tail_call
260    | direct ⇒ λhd_refl. (∀w. Q (DIRECT w) (p_direct w ?)) → tail_call
261    | indirect ⇒ λhd_refl. (∀w. Q (INDIRECT w) (p_indirect w ?)) → tail_call
262    | ext_indirect ⇒ λhd_refl. (∀w. Q (EXT_INDIRECT w) (p_ext_indirect w ?)) → tail_call
263    | acc_a ⇒ λhd_refl. (Q ACC_A (p_acc_a ?)) → tail_call
264    | registr ⇒ λhd_refl. (∀w. Q (REGISTER w) (p_register w ?)) → tail_call
265    | acc_b ⇒ λhd_refl. (Q ACC_A (p_acc_b ?)) → tail_call
266    | dptr ⇒ λhd_refl. (Q DPTR (p_dptr ?)) → tail_call
267    | data ⇒ λhd_refl. (∀w. Q (DATA w) (p_data w ?)) → tail_call
268    | data16 ⇒ λhd_refl. (∀w. Q (DATA16 w) (p_data16 w ?)) → tail_call
269    | acc_dptr ⇒ λhd_refl. (Q ACC_DPTR (p_acc_dptr ?)) → tail_call
270    | acc_pc ⇒ λhd_refl. (Q ACC_PC (p_acc_pc ?)) → tail_call
271    | ext_indirect_dptr ⇒ λhd_refl. (Q EXT_INDIRECT_DPTR (p_ext_indirect_dptr ?)) → tail_call
272    | indirect_dptr ⇒ λhd_refl. (Q INDIRECT_DPTR (p_indirect_dptr ?)) → tail_call
273    | carry ⇒ λhd_refl. (Q CARRY (p_carry ?)) → tail_call
274    | bit_addr ⇒ λhd_refl. (∀w. Q (BIT_ADDR w) (p_bit_addr w ?)) → tail_call
275    | n_bit_addr ⇒ λhd_refl. (∀w. Q (N_BIT_ADDR w) (p_n_bit_addr w ?)) → tail_call
276    | relative ⇒ λhd_refl. (∀w. Q (RELATIVE w) (p_relative w ?)) → tail_call
277    ] (refl … hd)
278 ] (refl … n) (refl_jmeq … v).
279 try (#w1 try #w2)
280 destruct
281 [ 38:
282   normalize in p_relative;
283   generalize in match p_relative;
284   destruct
285   cases (is_in (S n') (hd:::tl) (RELATIVE B))
286 destruct whd /2 by or_introl, or_intror, I/
287qed.
288
289lemma subaddressing_mode_elim:
290  ∀T:Type[2].
291  ∀P1: Word11 → T.
292  ∀P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19: False → T.
293  ∀addr: addressing_mode.
294  ∀p: is_in 1 [[ addr11 ]] addr.
295  ∀Q: addressing_mode → T → Prop.
296    (∀w. Q (ADDR11 w) (P1 w)) →
297      Q addr (
298        match addr return λx:addressing_mode. (is_in 1 [[addr11]] x → T) with 
299        [ ADDR11 (x:Word11) ⇒ λH:True. P1 x
300        | ADDR16 _ ⇒ λH:False. P2 H
301        | DIRECT _ ⇒ λH:False. P3 H
302        | INDIRECT _ ⇒ λH:False. P4 H
303        | EXT_INDIRECT _ ⇒ λH:False. P5 H
304        | ACC_A ⇒ λH:False. P6 H
305        | REGISTER _ ⇒ λH:False. P7 H
306        | ACC_B ⇒ λH:False. P8 H
307        | DPTR ⇒ λH:False. P9 H
308        | DATA _ ⇒ λH:False. P10 H
309        | DATA16 _ ⇒ λH:False. P11 H
310        | ACC_DPTR ⇒ λH:False. P12 H
311        | ACC_PC ⇒ λH:False. P13 H
312        | EXT_INDIRECT_DPTR ⇒ λH:False. P14 H
313        | INDIRECT_DPTR ⇒ λH:False. P15 H
314        | CARRY ⇒ λH:False. P16 H
315        | BIT_ADDR _ ⇒ λH:False. P17 H
316        | N_BIT_ADDR _ ⇒ λH:False. P18 H   
317        | RELATIVE _ ⇒ λH:False. P19 H
318        ] p).
319  #T #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10 #P11 #P12 #P13
320  #P14 #P15 #P16 #P17 #P18 #P19
321  * try #x1 try #x2 try #x3 try #x4
322  try (@⊥ assumption) normalize @x4
323qed.
324
325include alias "arithmetics/nat.ma".
326
327lemma lt_n_o_to_plus_m_n_lt_plus_m_o:
328  ∀m, n, o: nat.
329    n < o → m + n < m + o.
330  #m #n #o #assm /2 by monotonic_le_plus_r/
331qed.
332
333axiom fetch_program_counter_n_technical:
334  ∀code_memory: BitVectorTrie Byte 16.
335  ∀program_counter, program_counter': Word.
336  ∀instruction: instruction.
337  ∀ticks, n: nat.
338  program_counter' = \snd (\fst (fetch code_memory program_counter)) →
339    program_counter' = fetch_program_counter_n (S n) code_memory (zero …) →
340      program_counter = fetch_program_counter_n n code_memory (zero …).
341   
342let rec block_cost
343  (code_memory: BitVectorTrie Byte 16) (program_counter: Word)
344    (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
345      (good_program_witness: good_program code_memory program_counter total_program_size)
346        on program_size: total_program_size ≤ program_size + nat_of_bitvector … program_counter → nat ≝
347  match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter → nat with
348  [ O ⇒ λbase_case. 0
349  | S program_size' ⇒ λrecursive_case.
350    let 〈instruction, program_counter', ticks〉 as FETCH ≝ fetch code_memory program_counter in
351      match lookup_opt … program_counter' cost_labels return λx: option costlabel. nat with
352      [ None   ⇒
353        match instruction return λx. x = instruction → ? with
354        [ RealInstruction instruction ⇒ λreal_instruction.
355          match instruction return λx. x = instruction → ? with
356          [ RET                    ⇒ λinstr. ticks
357          | JC   relative          ⇒ λinstr. ticks
358          | JNC  relative          ⇒ λinstr. ticks
359          | JB   bit_addr relative ⇒ λinstr. ticks
360          | JNB  bit_addr relative ⇒ λinstr. ticks
361          | JBC  bit_addr relative ⇒ λinstr. ticks
362          | JZ   relative          ⇒ λinstr. ticks
363          | JNZ  relative          ⇒ λinstr. ticks
364          | CJNE src_trgt relative ⇒ λinstr. ticks
365          | DJNZ src_trgt relative ⇒ λinstr. ticks
366          | _                      ⇒ λinstr.
367              ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
368          ] (refl … instruction)
369        | ACALL addr     ⇒ λinstr.
370            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
371        | AJMP  addr     ⇒ λinstr. ticks
372        | LCALL addr     ⇒ λinstr.
373            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
374        | LJMP  addr     ⇒ λinstr. ticks
375        | SJMP  addr     ⇒ λinstr. ticks
376        | JMP   addr     ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
377            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
378        | MOVC  src trgt ⇒ λinstr.
379            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
380        ] (refl … instruction)
381      | Some _ ⇒ ticks
382      ]
383  ].
384  [1:
385    generalize in match good_program_witness;
386    whd in match good_program; normalize nodelta
387    cases FETCH normalize nodelta
388    cases instr normalize nodelta
389    @subaddressing_mode_elim #new_addr
390    cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta
391    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
392    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta
393    #assm cases assm #ignore
394    whd in match good_program_counter; normalize nodelta * #n * *
395    #program_counter_eq' #program_counter_lt_total_program_size
396    #fetch_n_leq_program_counter'
397    @(transitive_le
398      total_program_size
399      ((S program_size') + nat_of_bitvector … program_counter)
400      (program_size' + nat_of_bitvector … program_counter') recursive_case)
401    whd in ⊢ (?%?);
402    change with (
403      program_size' + (nat_of_bitvector … program_counter) <
404        program_size' + (nat_of_bitvector … program_counter'))
405    @lt_n_o_to_plus_m_n_lt_plus_m_o
406    >(fetch_program_counter_n_technical code_memory program_counter
407      program_counter' instruction ticks n)
408    /2 by pair_destruct_2/
409  |3,5,7,9,11:
410    (* XXX etc. need the subaddressing_mode_elim generalizing *)
411  |2:
412    generalize in match good_program_witness;
413    whd in match (good_program code_memory program_counter total_program_size);
414    cases FETCH normalize nodelta
415    cases instr normalize nodelta
416    @subaddressing_mode_elim #new_addr
417    cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta
418    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
419    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta
420    #assm cases assm #ignore #good_program_counter
421    whd in match (good_program code_memory program_counter' total_program_size);
422    cases(fetch code_memory program_counter') #instruction_program_counter'' #ticks''
423    cases(instruction_program_counter'') #instruction'' #program_counter'' normalize nodelta
424   
425  [2:
426    (* generalize in match good_program_witness; *)
427    whd in match good_program; normalize nodelta
428    cases FETCH normalize nodelta
429    cases (fetch code_memory program_counter') #instruction_program_counter' #ticks' normalize nodelta
430    cases instruction_program_counter' #instruction' #program_counter'' normalize nodelta
431    cases acall normalize nodelta
432    cases addr #subaddressing_mode cases subaddressing_mode
433    try (#assm #absurd normalize in absurd; cases absurd)
434    try (#absurd normalize in absurd; cases absurd)
435    normalize nodelta
436    cases instruction'
437    try (#assm normalize nodelta)
438    [7:
439      #irrelevant
440      whd in match good_program_counter; normalize nodelta
441     
442     
443     
444     
445     
446     
447     
448     
449     
450(* XXX: use memoisation here in the future *)
451let rec block_cost
452  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
453    (program_counter: Word) (program_size: nat) (total_program_size: nat)
454      (size_invariant: total_program_size ≤ code_memory_size)
455        (pc_invariant: nat_of_bitvector … program_counter < total_program_size)
456          on program_size: total_program_size - nat_of_bitvector … program_counter ≤ program_size → nat ≝
457  match program_size return λprogram_size: nat. total_program_size - nat_of_bitvector … program_counter ≤ program_size → nat with
458  [ O ⇒ λbase_case. 0 (* XXX: change from ⊥ before *)
459  | S program_size ⇒ λrecursive_case.
460    let 〈instr, newpc, ticks〉 ≝ fetch … code_memory program_counter in
461      match lookup_opt … newpc cost_labels return λx: option costlabel. nat with
462      [ None ⇒
463          let classify ≝ ASM_classify0 instr in
464          match classify return λx. classify = x → ? with
465          [ cl_jump ⇒ λclassify_refl. ticks
466          | cl_call ⇒ λclassify_refl. (* ite here *)
467              ticks + (block_cost code_memory cost_labels newpc program_size total_program_size size_invariant ? final_instr_invariant ?)
468          | cl_return ⇒ λclassify_refl. ticks
469          | cl_other ⇒ λclassify_refl. (* ite here *)
470              ticks + (block_cost code_memory cost_labels newpc program_size total_program_size size_invariant ? final_instr_invariant ?)
471          ] (refl … classify)
472        | Some _ ⇒ ticks
473      ]
474  ].
475
476let rec traverse_code_internal
477  (program: list Byte) (mem: BitVectorTrie Byte 16)
478    (cost_labels: BitVectorTrie costlabel 16) (pc: Word) (program_size: nat)
479      on program: identifier_map CostTag nat ≝
480 let 〈instr,newpc,ticks〉 ≝ fetch … mem pc in
481 match program with
482 [ nil ⇒ empty_map …
483 | cons hd tl ⇒
484   match lookup_opt … pc cost_labels with
485   [ None ⇒ traverse_code_internal tl mem cost_labels newpc program_size
486   | Some lbl ⇒
487     let cost ≝ block_cost mem cost_labels pc program_size in
488     let cost_mapping ≝ traverse_code_internal tl mem cost_labels newpc program_size in
489       add … cost_mapping lbl cost ]].
490
491definition traverse_code ≝
492  λprogram: list Byte.
493  λmem: BitVectorTrie Byte 16.
494  λcost_labels.
495  λprogram_size: nat.
496    traverse_code_internal program mem cost_labels (zero …) program_size.
497
498definition compute_costs ≝
499  λprogram: list Byte.
500  λcost_labels: BitVectorTrie costlabel 16.
501  λhas_main: bool.
502  let program_size ≝ |program| + 1 in
503  let memory ≝ load_code_memory program in
504   traverse_code program memory cost_labels program_size.
Note: See TracBrowser for help on using the repository browser.