source: src/ASM/ASMCosts.ma @ 1622

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

to avoid conflicts, bug in typechecker?

File size: 26.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    match eq_a hd a with
185    [ true  ⇒ True
186    | false ⇒ member_addressing_mode_tag n' tl a
187    ]
188  ].
189 
190let rec subaddressing_mode_elim_type
191  (T: Type[2]) (n: nat) (v: Vector addressing_mode_tag n)
192    (Q: addressing_mode → T → Prop)
193      on v:
194    (∀n. ∀v. Word11      → member_addressing_mode_tag n v addr11            → T) →
195    (∀n. ∀v. Word        → member_addressing_mode_tag n v addr16            → T) →
196    (∀n. ∀v. Byte        → member_addressing_mode_tag n v direct            → T) →
197    (∀n. ∀v. Bit         → member_addressing_mode_tag n v indirect          → T) →
198    (∀n. ∀v. Bit         → member_addressing_mode_tag n v ext_indirect      → T) →
199    (∀n. ∀v.               member_addressing_mode_tag n v acc_a             → T) →
200    (∀n. ∀v. BitVector 3 → member_addressing_mode_tag n v registr           → T) →
201    (∀n. ∀v.               member_addressing_mode_tag n v acc_b             → T) →
202    (∀n. ∀v.               member_addressing_mode_tag n v dptr              → T) →
203    (∀n. ∀v. Byte        → member_addressing_mode_tag n v data              → T) →
204    (∀n. ∀v. Word        → member_addressing_mode_tag n v data16            → T) →
205    (∀n. ∀v.               member_addressing_mode_tag n v acc_dptr          → T) →
206    (∀n. ∀v.               member_addressing_mode_tag n v acc_pc            → T) →
207    (∀n. ∀v.               member_addressing_mode_tag n v ext_indirect_dptr → T) →
208    (∀n. ∀v.               member_addressing_mode_tag n v indirect_dptr     → T) →
209    (∀n. ∀v.               member_addressing_mode_tag n v carry             → T) →
210    (∀n. ∀v. Byte        → member_addressing_mode_tag n v bit_addr          → T) →
211    (∀n. ∀v. Byte        → member_addressing_mode_tag n v n_bit_addr        → T) →
212    (∀n. ∀v. Byte        → member_addressing_mode_tag n v relative          → T) → Prop ≝
213  match v return λm: nat. λv': Vector addressing_mode_tag m. v ≃ v' → ? with
214  [ VEmpty         ⇒ λv_refl.
215    λp_addr11. λp_addr16. λp_direct. λp_indirect. λp_ext_indirect. λp_acc_a.
216    λp_register. λp_acc_b. λp_dptr. λp_data. λp_data16. λp_acc_dptr. λp_acc_pc.
217    λp_ext_indirect_dptr. λp_indirect_dptr. λp_carry. λp_bit_addr.
218    λp_n_bit_addr. λp_relative. False
219      (* ∀addr:v. Q addr (match addr with *)
220  | VCons n' hd tl ⇒ λv_refl.
221    λp_addr11. λp_addr16: ∀n. ∀v. Word   → member_addressing_mode_tag n v addr16  → T.
222    λp_direct. λp_indirect. λp_ext_indirect. λp_acc_a. λp_register. λp_acc_b.
223    λp_dptr. λp_data. λp_data16. λp_acc_dptr. λp_acc_pc. λp_ext_indirect_dptr.
224    λp_indirect_dptr. λp_carry. λp_bit_addr. λp_n_bit_addr. λp_relative.
225    match hd return λa: addressing_mode_tag. a = hd → Prop with
226    [ addr11 ⇒ λhd_refl. (∀w. Q (ADDR11 w) (p_addr11 n v w ?)) →
227        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
228          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
229            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
230              p_relative)
231    | addr16 ⇒ λhd_refl. (∀w. Q (ADDR16 w) (p_addr16 n v w ?)) →
232        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
233          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
234            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
235              p_relative)
236    | direct ⇒ λhd_refl. (∀w. Q (DIRECT w) (p_direct n v w ?)) →
237        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
238          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
239            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
240              p_relative)
241    | indirect ⇒ λhd_refl. (∀w. Q (INDIRECT w) (p_indirect n v w ?)) →
242        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
243          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
244            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
245              p_relative)
246    | ext_indirect ⇒ λhd_refl. (∀w. Q (EXT_INDIRECT w) (p_ext_indirect n v w ?)) →
247        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
248          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
249            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
250              p_relative)
251    | acc_a ⇒ λhd_refl. (Q ACC_A (p_acc_a n v ?)) →
252        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
253          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
254            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
255              p_relative)
256    | registr ⇒ λhd_refl. (∀w. Q (REGISTER w) (p_register n v w ?)) →
257        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
258          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
259            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
260              p_relative)
261    | acc_b ⇒ λhd_refl. (Q ACC_A (p_acc_b n v ?)) →
262        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
263          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
264            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
265              p_relative)
266    | dptr ⇒ λhd_refl. (Q DPTR (p_dptr n v ?)) →
267        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
268          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
269            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
270              p_relative)
271    | data ⇒ λhd_refl. (∀w. Q (DATA w) (p_data n v w ?)) →
272        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
273          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
274            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
275              p_relative)
276    | data16 ⇒ λhd_refl. (∀w. Q (DATA16 w) (p_data16 n v w ?)) →
277        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
278          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
279            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
280              p_relative)
281    | acc_dptr ⇒ λhd_refl. (Q ACC_DPTR (p_acc_dptr n v ?)) →
282        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
283          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
284            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
285              p_relative)
286    | acc_pc ⇒ λhd_refl. (Q ACC_PC (p_acc_pc n v ?)) →
287        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
288          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
289            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
290              p_relative)
291    | ext_indirect_dptr ⇒ λhd_refl. (Q EXT_INDIRECT_DPTR (p_ext_indirect_dptr n v ?)) →
292        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
293          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
294            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
295              p_relative)
296    | indirect_dptr ⇒ λhd_refl. (Q INDIRECT_DPTR (p_indirect_dptr n v ?)) →
297        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
298          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
299            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
300              p_relative)
301    | carry ⇒ λhd_refl. (Q CARRY (p_carry n v ?)) →
302        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
303          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
304            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
305              p_relative)
306    | bit_addr ⇒ λhd_refl. (∀w. Q (BIT_ADDR w) (p_bit_addr n v w ?)) →
307        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
308          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
309            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
310              p_relative)
311    | n_bit_addr ⇒ λhd_refl. (∀w. Q (N_BIT_ADDR w) (p_n_bit_addr n v w ?)) →
312        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
313          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
314            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
315              p_relative)
316    | relative ⇒ λhd_refl. (∀w. Q (RELATIVE w) (p_relative n v w ?)) →
317        (subaddressing_mode_elim_type T n' tl Q p_addr11 p_addr16 p_direct p_indirect
318          p_ext_indirect p_acc_a p_register p_acc_b p_dptr p_data p_data16 p_acc_dptr
319            p_acc_pc p_ext_indirect_dptr p_indirect_dptr p_carry p_bit_addr p_n_bit_addr
320              p_relative)
321    ] (refl … hd)
322  ] (refl_jmeq … v).
323
324lemma subaddressing_mode_elim:
325  ∀T:Type[2].
326  ∀P1: Word11 → T.
327  ∀P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19: False → T.
328  ∀addr: addressing_mode.
329  ∀p: is_in 1 [[ addr11 ]] addr.
330  ∀Q: addressing_mode → T → Prop.
331    (∀w. Q (ADDR11 w) (P1 w)) →
332      Q addr (
333        match addr return λx:addressing_mode. (is_in 1 [[addr11]] x → T) with 
334        [ ADDR11 (x:Word11) ⇒ λH:True. P1 x
335        | ADDR16 _ ⇒ λH:False. P2 H
336        | DIRECT _ ⇒ λH:False. P3 H
337        | INDIRECT _ ⇒ λH:False. P4 H
338        | EXT_INDIRECT _ ⇒ λH:False. P5 H
339        | ACC_A ⇒ λH:False. P6 H
340        | REGISTER _ ⇒ λH:False. P7 H
341        | ACC_B ⇒ λH:False. P8 H
342        | DPTR ⇒ λH:False. P9 H
343        | DATA _ ⇒ λH:False. P10 H
344        | DATA16 _ ⇒ λH:False. P11 H
345        | ACC_DPTR ⇒ λH:False. P12 H
346        | ACC_PC ⇒ λH:False. P13 H
347        | EXT_INDIRECT_DPTR ⇒ λH:False. P14 H
348        | INDIRECT_DPTR ⇒ λH:False. P15 H
349        | CARRY ⇒ λH:False. P16 H
350        | BIT_ADDR _ ⇒ λH:False. P17 H
351        | N_BIT_ADDR _ ⇒ λH:False. P18 H   
352        | RELATIVE _ ⇒ λH:False. P19 H
353        ] p).
354  #T #P1 #P2 #P3 #P4 #P5 #P6 #P7 #P8 #P9 #P10 #P11 #P12 #P13
355  #P14 #P15 #P16 #P17 #P18 #P19
356  * try #x1 try #x2 try #x3 try #x4
357  try (@⊥ assumption) normalize @x4
358qed.
359
360include alias "arithmetics/nat.ma".
361
362lemma lt_n_o_to_plus_m_n_lt_plus_m_o:
363  ∀m, n, o: nat.
364    n < o → m + n < m + o.
365  #m #n #o #assm /2 by monotonic_le_plus_r/
366qed.
367
368axiom fetch_program_counter_n_technical:
369  ∀code_memory: BitVectorTrie Byte 16.
370  ∀program_counter, program_counter': Word.
371  ∀instruction: instruction.
372  ∀ticks, n: nat.
373  program_counter' = \snd (\fst (fetch code_memory program_counter)) →
374    program_counter' = fetch_program_counter_n (S n) code_memory (zero …) →
375      program_counter = fetch_program_counter_n n code_memory (zero …).
376   
377let rec block_cost
378  (code_memory: BitVectorTrie Byte 16) (program_counter: Word)
379    (program_size: nat) (total_program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
380      (good_program_witness: good_program code_memory program_counter total_program_size)
381        on program_size: total_program_size ≤ program_size + nat_of_bitvector … program_counter → nat ≝
382  match program_size return λprogram_size: nat. total_program_size ≤ program_size + nat_of_bitvector … program_counter → nat with
383  [ O ⇒ λbase_case. 0
384  | S program_size' ⇒ λrecursive_case.
385    let 〈instruction, program_counter', ticks〉 as FETCH ≝ fetch code_memory program_counter in
386      match lookup_opt … program_counter' cost_labels return λx: option costlabel. nat with
387      [ None   ⇒
388        match instruction return λx. x = instruction → ? with
389        [ RealInstruction instruction ⇒ λreal_instruction.
390          match instruction return λx. x = instruction → ? with
391          [ RET                    ⇒ λinstr. ticks
392          | JC   relative          ⇒ λinstr. ticks
393          | JNC  relative          ⇒ λinstr. ticks
394          | JB   bit_addr relative ⇒ λinstr. ticks
395          | JNB  bit_addr relative ⇒ λinstr. ticks
396          | JBC  bit_addr relative ⇒ λinstr. ticks
397          | JZ   relative          ⇒ λinstr. ticks
398          | JNZ  relative          ⇒ λinstr. ticks
399          | CJNE src_trgt relative ⇒ λinstr. ticks
400          | DJNZ src_trgt relative ⇒ λinstr. ticks
401          | _                      ⇒ λinstr.
402              ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
403          ] (refl … instruction)
404        | ACALL addr     ⇒ λinstr.
405            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
406        | AJMP  addr     ⇒ λinstr. ticks
407        | LCALL addr     ⇒ λinstr.
408            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
409        | LJMP  addr     ⇒ λinstr. ticks
410        | SJMP  addr     ⇒ λinstr. ticks
411        | JMP   addr     ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
412            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
413        | MOVC  src trgt ⇒ λinstr.
414            ticks + block_cost code_memory program_counter' program_size' total_program_size cost_labels ? ?
415        ] (refl … instruction)
416      | Some _ ⇒ ticks
417      ]
418  ].
419  [1:
420    generalize in match good_program_witness;
421    whd in match good_program; normalize nodelta
422    cases FETCH normalize nodelta
423    cases instr normalize nodelta
424    @subaddressing_mode_elim #new_addr
425    cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta
426    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
427    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta
428    #assm cases assm #ignore
429    whd in match good_program_counter; normalize nodelta * #n * *
430    #program_counter_eq' #program_counter_lt_total_program_size
431    #fetch_n_leq_program_counter'
432    @(transitive_le
433      total_program_size
434      ((S program_size') + nat_of_bitvector … program_counter)
435      (program_size' + nat_of_bitvector … program_counter') recursive_case)
436    whd in ⊢ (?%?);
437    change with (
438      program_size' + (nat_of_bitvector … program_counter) <
439        program_size' + (nat_of_bitvector … program_counter'))
440    @lt_n_o_to_plus_m_n_lt_plus_m_o
441    >(fetch_program_counter_n_technical code_memory program_counter
442      program_counter' instruction ticks n)
443    /2 by pair_destruct_2/
444  |3,5,7,9,11:
445    (* XXX etc. need the subaddressing_mode_elim generalizing *)
446  |2:
447    generalize in match good_program_witness;
448    whd in match (good_program code_memory program_counter total_program_size);
449    cases FETCH normalize nodelta
450    cases instr normalize nodelta
451    @subaddressing_mode_elim #new_addr
452    cases (split … 8 8 program_counter') #pc_bu #pc_bl normalize nodelta
453    cases (split … 3 8 new_addr) #thr #eig normalize nodelta
454    cases (split … 5 3 pc_bu) #fiv #thr' normalize nodelta
455    #assm cases assm #ignore #good_program_counter
456    whd in match (good_program code_memory program_counter' total_program_size);
457    cases(fetch code_memory program_counter') #instruction_program_counter'' #ticks''
458    cases(instruction_program_counter'') #instruction'' #program_counter'' normalize nodelta
459   
460  [2:
461    (* generalize in match good_program_witness; *)
462    whd in match good_program; normalize nodelta
463    cases FETCH normalize nodelta
464    cases (fetch code_memory program_counter') #instruction_program_counter' #ticks' normalize nodelta
465    cases instruction_program_counter' #instruction' #program_counter'' normalize nodelta
466    cases acall normalize nodelta
467    cases addr #subaddressing_mode cases subaddressing_mode
468    try (#assm #absurd normalize in absurd; cases absurd)
469    try (#absurd normalize in absurd; cases absurd)
470    normalize nodelta
471    cases instruction'
472    try (#assm normalize nodelta)
473    [7:
474      #irrelevant
475      whd in match good_program_counter; normalize nodelta
476     
477     
478     
479     
480     
481     
482     
483     
484     
485(* XXX: use memoisation here in the future *)
486let rec block_cost
487  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
488    (program_counter: Word) (program_size: nat) (total_program_size: nat)
489      (size_invariant: total_program_size ≤ code_memory_size)
490        (pc_invariant: nat_of_bitvector … program_counter < total_program_size)
491          on program_size: total_program_size - nat_of_bitvector … program_counter ≤ program_size → nat ≝
492  match program_size return λprogram_size: nat. total_program_size - nat_of_bitvector … program_counter ≤ program_size → nat with
493  [ O ⇒ λbase_case. 0 (* XXX: change from ⊥ before *)
494  | S program_size ⇒ λrecursive_case.
495    let 〈instr, newpc, ticks〉 ≝ fetch … code_memory program_counter in
496      match lookup_opt … newpc cost_labels return λx: option costlabel. nat with
497      [ None ⇒
498          let classify ≝ ASM_classify0 instr in
499          match classify return λx. classify = x → ? with
500          [ cl_jump ⇒ λclassify_refl. ticks
501          | cl_call ⇒ λclassify_refl. (* ite here *)
502              ticks + (block_cost code_memory cost_labels newpc program_size total_program_size size_invariant ? final_instr_invariant ?)
503          | cl_return ⇒ λclassify_refl. ticks
504          | cl_other ⇒ λclassify_refl. (* ite here *)
505              ticks + (block_cost code_memory cost_labels newpc program_size total_program_size size_invariant ? final_instr_invariant ?)
506          ] (refl … classify)
507        | Some _ ⇒ ticks
508      ]
509  ].
510
511let rec traverse_code_internal
512  (program: list Byte) (mem: BitVectorTrie Byte 16)
513    (cost_labels: BitVectorTrie costlabel 16) (pc: Word) (program_size: nat)
514      on program: identifier_map CostTag nat ≝
515 let 〈instr,newpc,ticks〉 ≝ fetch … mem pc in
516 match program with
517 [ nil ⇒ empty_map …
518 | cons hd tl ⇒
519   match lookup_opt … pc cost_labels with
520   [ None ⇒ traverse_code_internal tl mem cost_labels newpc program_size
521   | Some lbl ⇒
522     let cost ≝ block_cost mem cost_labels pc program_size in
523     let cost_mapping ≝ traverse_code_internal tl mem cost_labels newpc program_size in
524       add … cost_mapping lbl cost ]].
525
526definition traverse_code ≝
527  λprogram: list Byte.
528  λmem: BitVectorTrie Byte 16.
529  λcost_labels.
530  λprogram_size: nat.
531    traverse_code_internal program mem cost_labels (zero …) program_size.
532
533definition compute_costs ≝
534  λprogram: list Byte.
535  λcost_labels: BitVectorTrie costlabel 16.
536  λhas_main: bool.
537  let program_size ≝ |program| + 1 in
538  let memory ≝ load_code_memory program in
539   traverse_code program memory cost_labels program_size.
Note: See TracBrowser for help on using the repository browser.