source: src/ASM/ASMCosts.ma @ 2714

Last change on this file since 2714 was 2710, checked in by sacerdot, 7 years ago

ASMCosts.ma repaired

File size: 37.0 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Interpret.ma". (* includes ASM/AbstractStatus.ma *)
5
6(*include "common/StructuredTraces.ma". (* already included by ASM/AbstractStatus.ma *) *)
7
8(* could this definition be moved to ASM/AbstractStatus.ma ???
9   no: requires ASM/Interpret/execute_1 *)
10
11definition ASM_abstract_status: ∀code_memory. BitVectorTrie costlabel 16 → abstract_status ≝
12  λcode_memory.
13  λcost_labels.
14    mk_abstract_status
15      (Status code_memory)
16      (λs,s'. (execute_1 … s) = s')
17      word_deqset
18      (program_counter …)
19      (λs. Some ? (ASM_classify … s))
20      (λpc.lookup_opt … pc cost_labels)
21      (next_instruction_properly_relates_program_counters code_memory)
22      ? (* (λs.False) *)
23      ?
24      ?.
25  cases daemon (* XXX: is_final predicate, (tail)call_ident function *)
26qed.
27
28lemma Some_inv : ∀A.∀a,b. Some A a = Some A b → a = b.
29  #A#a #b #eq destruct //
30  qed.
31
32definition trace_any_label_length ≝
33  λcode_memory: BitVectorTrie Byte 16.
34  λcost_labels: BitVectorTrie costlabel 16.
35  λtrace_ends_flag: trace_ends_with_ret.
36  λstart_status: Status code_memory.
37  λfinal_status: Status code_memory.
38  λthe_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
39    as_trace_any_label_length' … the_trace.
40
41let rec all_program_counter_list
42  (n: nat)
43    on n ≝
44  match n with
45  [ O ⇒ [ ]
46  | S n' ⇒ (bitvector_of_nat 16 n')::(all_program_counter_list n')
47  ].
48
49lemma all_program_counter_list_bound_eq:
50  ∀bound: nat.
51    |all_program_counter_list bound| = bound.
52  #bound elim bound try %
53  #bound' #inductive_hypothesis normalize
54  >inductive_hypothesis %
55qed.
56
57lemma mem_all_program_counter_list_lt_bound:
58  ∀bound: nat.
59  ∀bound_proof: bound ≤ 2^16.
60  ∀e: Word.
61    memb word_deqset e (all_program_counter_list bound) = true →
62      nat_of_bitvector … e < bound.
63  #bound elim bound
64  [1:
65    #bound_proof #e
66    normalize #absurd destruct(absurd)
67  |2:
68    #bound' #inductive_hypothesis
69    #bound_proof #e
70    change with (if eq_bv ??? then ? else ? = ? → ?)
71    @eq_bv_elim
72    [1:
73      #eq_assm
74      normalize nodelta destruct #_
75      >nat_of_bitvector_bitvector_of_nat_inverse try assumption
76      normalize %
77    |2:
78      #neq_assm normalize nodelta
79      #memb_assm %2 @inductive_hypothesis
80      try assumption
81      @(transitive_le bound' (S bound'))
82      try assumption %2 %
83    ]
84  ]
85qed.
86
87lemma lt_bound_mem_all_program_counter_list:
88  ∀bound: nat.
89  ∀bound_proof: bound ≤ 2^16.
90  ∀e: Word.
91    nat_of_bitvector … e < bound →
92      memb word_deqset e (all_program_counter_list bound) = true.
93  #bound elim bound
94  [1:
95    #bound_proof #e normalize
96    #absurd cases (lt_to_not_zero … absurd)
97  |2:
98    #bound' #inductive_hypothesis
99    #bound_proof #e
100    change with (? → if eq_bv ??? then ? else ? = ?)
101    #assm
102    cases (le_to_or_lt_eq … (nat_of_bitvector 16 e) bound' ?)
103    [1:
104      #e_lt_assm
105      @eq_bv_elim
106      [1:
107        #eq_assm normalize nodelta %
108      |2:
109        #neq_assm normalize nodelta
110        @inductive_hypothesis try assumption
111        @(transitive_le bound' (S bound')) try assumption
112        %2 %
113      ]
114    |2:
115      #relevant >eq_eq_bv normalize nodelta try %
116      destruct >bitvector_of_nat_inverse_nat_of_bitvector %
117    |3:
118      normalize in assm;
119      @le_S_S_to_le assumption
120    ]
121  ]
122qed.
123
124include alias "arithmetics/nat.ma".
125
126lemma execute_1_and_program_counter_after_other_in_lockstep00:
127  ∀code_memory: BitVectorTrie Byte 16.
128  ∀start_status: Status code_memory.
129      let 〈instruction, pc, ticks〉 ≝ fetch code_memory (program_counter … start_status) in
130    ASM_classify0 instruction = cl_other →
131        program_counter ? ? (execute_1 … start_status) =
132          program_counter_after_other pc instruction.
133  #code_memory #start_status
134  whd in match execute_1; normalize nodelta 
135  cases (execute_1' code_memory start_status) #the_status; * #_
136  cases (fetch ? ?); * #instruction #pc #ticks
137  #classify_assm #classify_assm0 @classify_assm assumption
138qed.
139
140lemma execute_1_and_program_counter_after_other_in_lockstep0:
141  ∀code_memory: BitVectorTrie Byte 16.
142  ∀start_status: Status code_memory.
143    ASM_classify … start_status = cl_other →
144      let 〈instruction, pc, ticks〉 ≝ fetch code_memory (program_counter … start_status) in
145        program_counter ? ? (execute_1 … start_status) =
146          program_counter_after_other pc instruction.
147  #code_memory #start_status #classify_assm
148  whd in match execute_1; normalize nodelta
149  cases (execute_1' code_memory start_status) #the_status
150  * #_ whd in classify_assm:(??%?); whd in match (current_instruction ??) in classify_assm;
151  cases (fetch ? ?) in classify_assm; * #instruction #pc #ticks
152  #classify_assm #classify_assm' @classify_assm' assumption
153qed.
154
155lemma execute_1_and_program_counter_after_other_in_lockstep:
156  ∀code_memory: BitVectorTrie Byte 16.
157  ∀cost_labels: BitVectorTrie costlabel 16.
158  ∀start_status: Status code_memory.
159    as_classifier (ASM_abstract_status code_memory cost_labels) start_status cl_other →
160      let 〈instruction, pc, ticks〉 ≝ fetch code_memory (program_counter … start_status) in
161        program_counter ? ? (execute_1 … start_status) =
162          program_counter_after_other pc instruction.
163  #code_memory #cost_labels #start_status #classify_assm
164  change with (Some ? (ASM_classify0 ?) = ?) in classify_assm;
165  destruct (classify_assm)
166  @execute_1_and_program_counter_after_other_in_lockstep0 assumption.
167qed. 
168
169lemma sublist_tal_pc_list_all_program_counter_list:
170  ∀code_memory: BitVectorTrie Byte 16.
171  ∀cost_labels: BitVectorTrie costlabel 16.
172  ∀start, final: Status code_memory.
173  ∀trace_ends_flag.
174  ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start final.
175    sublist ? (tal_pc_list … the_trace) (all_program_counter_list (2^16)).
176  #code_memory #cost_labels #start #final #trace_ends_flag #the_trace
177  whd in match (sublist ???); #pc #memb_assm
178  @lt_bound_mem_all_program_counter_list try %
179  @nat_of_bitvector_lt_bound
180qed.
181
182corollary tal_pc_list_length_leq_total_program_size:
183  ∀code_memory: BitVectorTrie Byte 16.
184  ∀cost_labels: BitVectorTrie costlabel 16.
185  ∀start, final: Status code_memory.
186  ∀trace_ends_flag.
187  ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start final.
188  ∀unrepeating_witness: tal_unrepeating … the_trace.
189    |tal_pc_list … the_trace| ≤ 2^16.
190  #code_memory #cost_labels #start #final #trace_ends_flag #the_trace #unrepeating_witness
191  <(all_program_counter_list_bound_eq (2^16))
192  @sublist_length
193  [1:
194    @tal_unrepeating_uniqueb
195    assumption
196  |2:
197    @sublist_tal_pc_list_all_program_counter_list
198    assumption
199  ]
200qed.
201
202let rec compute_paid_trace_any_label
203  (code_memory: BitVectorTrie Byte 16) (cost_labels: BitVectorTrie costlabel 16)
204    (trace_ends_flag: trace_ends_with_ret) (start_status: Status code_memory)
205      (final_status: Status code_memory)
206        (the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status)
207       on the_trace: nat ≝
208  match the_trace with
209  [ tal_base_not_return the_status _ _ _ _ ⇒ current_instruction_cost … the_status
210  | tal_base_return the_status _ _ _ ⇒ current_instruction_cost … the_status
211  | tal_base_call pre_fun_call start_fun_call final _ _ _ _ _ ⇒ current_instruction_cost … pre_fun_call
212  | tal_base_tailcall pre_fun_call start_fun_call final _ _ _ ⇒ current_instruction_cost … pre_fun_call
213  | tal_step_call end_flag pre_fun_call start_fun_call after_fun_call final
214     _ _ _ call_trace _ final_trace ⇒
215      let current_instruction_cost ≝ current_instruction_cost … pre_fun_call in
216      let final_trace_cost ≝ compute_paid_trace_any_label … cost_labels end_flag … final_trace in
217        current_instruction_cost + final_trace_cost
218  | tal_step_default end_flag status_pre status_init status_end _ tail_trace _ _ ⇒
219      let current_instruction_cost ≝ current_instruction_cost … status_pre in
220      let tail_trace_cost ≝
221       compute_paid_trace_any_label … cost_labels end_flag
222        status_init status_end tail_trace
223      in
224        current_instruction_cost + tail_trace_cost
225  ].
226
227definition compute_paid_trace_label_label ≝
228  λcode_memory: BitVectorTrie Byte 16.
229  λcost_labels: BitVectorTrie costlabel 16.
230  λtrace_ends_flag: trace_ends_with_ret.
231  λstart_status: Status code_memory.
232  λfinal_status: Status code_memory.
233  λthe_trace: (trace_label_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status).
234  match the_trace with
235  [ tll_base ends_flag initial final given_trace labelled_proof ⇒
236      compute_paid_trace_any_label … given_trace
237  ].
238
239lemma trace_compute_paid_trace_cl_other:
240  ∀code_memory' : (BitVectorTrie Byte 16).
241  ∀program_counter' : Word.
242  ∀cost_labels : BitVectorTrie costlabel 16.
243  ∀program_size' : ℕ.
244  ∀ticks : ℕ.
245  ∀instruction : instruction.
246  ∀program_counter'' : Word.
247  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
248  let program_counter''' ≝ program_counter_after_other program_counter'' instruction in
249  ∀start_status : (Status code_memory').
250  ∀final_status : (Status code_memory').
251  ∀trace_ends_flag : trace_ends_with_ret.
252  ∀the_trace : (trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status).
253  ∀program_counter_refl : (program_counter' = program_counter … start_status).
254  ∀size_invariant : trace_any_label_length … the_trace ≤S program_size'.
255  ∀classify_assm: ASM_classify0 instruction = cl_other.
256  ∀pi1 : ℕ.
257   (if match lookup_opt costlabel 16 program_counter''' cost_labels with 
258         [None ⇒ true
259         |Some _ ⇒ false
260         ] 
261    then
262      ∀start_status0:Status code_memory'.
263      ∀final_status0:Status code_memory'.
264      ∀trace_ends_flag0:trace_ends_with_ret.
265      ∀the_trace0:trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag0 start_status0 final_status0.
266        trace_any_label_length … the_trace0 ≤ program_size' →
267        program_counter''' = program_counter … start_status0 →
268                  pi1
269                    =compute_paid_trace_any_label … the_trace0
270    else (pi1=O) )
271   → ticks+pi1
272     =compute_paid_trace_any_label … the_trace.
273  #code_memory' #program_counter' #cost_labels #program_size' #ticks #instruction
274  #program_counter'' #FETCH #start_status #final_status #trace_ends_flag #the_trace
275  #program_counter_refl #size_invariant #classify_assm #recursive_block_cost
276  #recursive_assm
277  @(trace_any_label_inv_ind … the_trace)
278    [2:
279      #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
280      #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
281    |3:
282      #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
283      #classifier_assm #after_return_assm #trace_label_return #costed_assm
284      #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
285      destruct @⊥
286    |4:
287      #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
288      #classifier_assm #trace_label_return
289      #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
290      destruct @⊥
291    |5:
292      #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
293      #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
294      #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
295      #final_status_refl #the_trace_refl destruct @⊥
296    |6:
297      #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
298      #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
299      #the_trace_refl
300      destruct
301      whd in match (trace_any_label_length … (tal_step_default …));
302      whd in match (compute_paid_trace_any_label … (tal_step_default …));
303      whd in costed_assm:(?%);
304      generalize in match costed_assm;
305      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels));
306      whd in match (as_label ??);
307      generalize in match (lookup_opt … (program_counter … (execute_1 … status_pre)) cost_labels)
308        in ⊢ (??%? → % → ?);
309      #lookup_assm cases lookup_assm
310      [1:
311        #None_lookup_opt_assm normalize nodelta #_
312        generalize in match recursive_assm;
313        (*lapply (execute_1_and_program_counter_after_other_in_lockstep0 code_memory' ? (Some_inv … classifier_assm))*)
314        lapply (execute_1_and_program_counter_after_other_in_lockstep … classifier_assm)
315        <FETCH normalize nodelta #rewrite_assm >rewrite_assm in None_lookup_opt_assm;
316        #None_lookup_opt_assm <None_lookup_opt_assm
317        normalize nodelta #new_recursive_assm
318        cases(new_recursive_assm (execute_1 code_memory' status_pre) status_end
319          end_flag trace_any_label ? ?) try (>rewrite_assm %)
320        whd in match (current_instruction_cost … status_pre);
321        cut(ticks = \snd (fetch code_memory'
322           (program_counter … status_pre)))
323        [1,3:
324          <FETCH %
325        |2:
326          #ticks_refl_assm
327          >ticks_refl_assm %
328        |4:
329          #ticks_refl_assm
330          change with (S ? ≤ S ?) in size_invariant;
331          lapply (le_S_S_to_le … size_invariant) #assm
332          assumption
333        ]
334      |2:
335        #costlabel #Some_lookup_opt_assm <Some_lookup_opt_assm
336        #absurd
337        cases (not_Some_neq_None_to_False ?? absurd)
338      ]
339    |1:
340      #status_start #status_final #execute_assm #classifier_assm #costed_assm
341      #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
342      destruct
343      whd in match (trace_any_label_length … (tal_base_not_return …));
344      whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
345      whd in costed_assm;
346      generalize in match costed_assm;
347      generalize in match (refl … (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels));
348      whd in match (as_label ??);
349      generalize in match (lookup_opt … (program_counter … (execute_1 … status_start)) cost_labels)
350        in ⊢ (??%? → % → ?);
351      #lookup_assm cases lookup_assm
352      [1:
353        #None_lookup_opt_assm
354        #absurd @⊥ cases absurd -absurd #absurd @absurd %
355      |2:
356        #costlabel #Some_lookup_opt_assm normalize nodelta #ignore
357        generalize in match recursive_assm;
358        cases classifier_assm -classifier_assm #classifier_assm
359        [2:
360          lapply (execute_1_and_program_counter_after_other_in_lockstep … classifier_assm)
361          <FETCH normalize nodelta #rewrite_assm >rewrite_assm in Some_lookup_opt_assm;
362          #Some_lookup_opt_assm <Some_lookup_opt_assm
363          normalize nodelta #new_recursive_assm >new_recursive_assm
364          cut(ticks = \snd (fetch code_memory'
365             (program_counter … status_start)))
366          [1:
367            <FETCH %
368          |2:
369            #ticks_refl_assm >ticks_refl_assm
370            <plus_n_O %
371          ]
372        |1:
373                (* JHM: wicked failure because Some prevent previous unfolding of ASM_classify here *)
374                @⊥
375                (* *** (* previously: unnecessary case analysis on instruction *)
376          change with ((Some ? (ASM_classify0 ?) = ?) → ?);
377          whd in match (current_instruction code_memory' status_start);
378          <FETCH generalize in match classify_assm;
379          cases instruction
380          [8:
381            #preinstruction normalize nodelta
382            whd in match ASM_classify0; normalize nodelta
383            #contradiction >contradiction #absurd destruct(absurd)
384          ]
385          try(#addr1 #addr2 normalize nodelta #ignore #absurd destruct(absurd))
386          try(#addr normalize nodelta #ignore #absurd destruct(absurd))
387          normalize in ignore; destruct(ignore)
388                *** *)
389        ]
390      ]
391    ]
392  change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;
393  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
394  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
395  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
396qed.
397
398lemma trace_compute_paid_trace_cl_jump:
399  ∀code_memory': BitVectorTrie Byte 16.
400  ∀program_counter': Word.
401  ∀cost_labels: BitVectorTrie costlabel 16.
402  ∀first_time_around: bool.
403  ∀program_size': ℕ.
404  ∀ticks: ℕ.
405  ∀instruction: instruction.
406  ∀program_counter'': Word.
407  ∀FETCH: 〈instruction,program_counter'',ticks〉 = fetch code_memory' program_counter'.
408  ∀start_status: (Status code_memory').
409  ∀final_status: (Status code_memory').
410  ∀trace_ends_flag: trace_ends_with_ret.
411  ∀the_trace: (trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status).
412  ∀program_counter_refl: (program_counter' = program_counter … start_status).
413  ∀classify_assm: ASM_classify0 instruction = cl_jump.
414    ticks
415     =compute_paid_trace_any_label … the_trace.
416  #code_memory' #program_counter' #cost_labels #first_time_around
417  #program_size' #ticks #instruction #program_counter'' #FETCH
418  #start_status #final_status
419  #trace_ends_flag #the_trace #program_counter_refl #classify_assm
420  @(trace_any_label_inv_ind … the_trace)
421  [6:
422    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
423    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
424    #the_trace_refl destruct @⊥
425  |1:
426    #status_start #status_final #execute_assm #classifier_assm #costed_assm
427    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
428    destruct
429    whd in match (trace_any_label_length … (tal_base_not_return …));
430    whd in match (compute_paid_trace_any_label … (tal_base_not_return …));
431    <FETCH %
432  |2:
433    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
434    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
435  |3:
436    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
437    #classifier_assm #after_return_assm #trace_label_return #costed_assm
438    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
439    destruct @⊥
440  |4:
441    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
442    #classifier_assm #trace_label_return
443    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
444    destruct @⊥
445  |5:
446    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
447    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
448    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
449    #final_status_refl #the_trace_refl destruct @⊥
450  ]
451  change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;
452  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
453  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
454  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd)
455qed.
456
457lemma trace_compute_paid_trace_cl_call:
458  ∀code_memory' : (BitVectorTrie Byte 16).
459  ∀program_counter' : Word.
460  ∀cost_labels : BitVectorTrie costlabel 16.
461  ∀program_size' : ℕ.
462  ∀ticks : ℕ.
463  ∀instruction : instruction.
464  ∀program_counter'' : Word.
465  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
466  ∀start_status : (Status code_memory').
467  ∀final_status : (Status code_memory').
468  ∀trace_ends_flag : trace_ends_with_ret.
469  ∀the_trace : trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
470  ∀program_counter_refl : (program_counter' = program_counter … start_status).
471  ∀size_invariant : trace_any_label_length … the_trace ≤S program_size'.
472  ∀classify_assm: ASM_classify0 instruction = cl_call.
473  (∀pi1:ℕ
474  .if match lookup_opt costlabel 16 program_counter'' cost_labels with 
475      [None ⇒ true | Some _ ⇒ false] 
476   then (∀start_status0:Status code_memory'
477             .∀final_status0:Status code_memory'
478              .∀trace_ends_flag0:trace_ends_with_ret
479               .∀the_trace0:trace_any_label
480                                        (ASM_abstract_status … cost_labels)
481                                        trace_ends_flag0 start_status0 final_status0.
482                trace_any_label_length … the_trace0
483                   ≤ program_size' →
484                program_counter''
485                 =program_counter … start_status0
486                 → pi1
487                   =compute_paid_trace_any_label … the_trace0) 
488   else (pi1=O) 
489   → ticks+pi1
490     =compute_paid_trace_any_label … the_trace).
491  #code_memory' #program_counter' #cost_labels #program_size'
492  #ticks #instruction #program_counter'' #FETCH
493  #start_status #final_status #trace_ends_flag
494  #the_trace #program_counter_refl #size_invariant #classify_assm
495  #recursive_block_cost #recursive_assm
496  @(trace_any_label_inv_ind … the_trace)
497  [6:
498    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
499    #classifier_assm #costed_assm #trace_ends_refl #start_status_refl #final_status_refl
500    #the_trace_refl destruct @⊥
501  |1:
502    #status_start #status_final #execute_assm #classifier_assm #costed_assm
503    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
504    destruct @⊥
505  |2:
506    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_assm
507    #start_status_refl #final_status_refl #the_trace_assm destruct @⊥
508  |3:
509    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
510    #classifier_assm #after_return_assm #trace_label_return #costed_assm
511    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
512    destruct
513    whd in match (trace_any_label_length … (tal_base_call …));
514    whd in match (compute_paid_trace_any_label … (tal_base_call …));
515    whd in costed_assm;
516    generalize in match costed_assm;
517    generalize in match (refl … (lookup_opt … (program_counter … status_final) cost_labels));
518    whd in match (as_label ??);
519    generalize in match (lookup_opt … (program_counter … status_final) cost_labels)
520      in ⊢ (??%? → % → ?);
521    #lookup_assm cases lookup_assm
522    [1:
523      #None_lookup_opt normalize nodelta #absurd cases absurd
524      -absurd #absurd @⊥ @absurd %
525    |2:
526      #costlabel #Some_lookup_opt normalize nodelta #ignore
527      generalize in match recursive_assm;
528      cut(program_counter'' = (program_counter … status_final))
529      [1:
530        generalize in match after_return_assm;
531        whd in ⊢ (% → ?); <FETCH normalize nodelta #relevant <relevant %
532      |2:
533        #program_counter_assm >program_counter_assm <Some_lookup_opt
534        normalize nodelta #new_recursive_assm >new_recursive_assm
535        cut(ticks = \snd (fetch code_memory' (program_counter … status_pre_fun_call)))
536        [1:
537          <FETCH %
538        |2:
539          #ticks_refl_assm >ticks_refl_assm
540          <plus_n_O %
541        ]
542      ]
543    ]
544  |4:
545    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
546    #classifier_assm #trace_label_return
547    #ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
548    destruct @⊥
549  |5:
550    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
551    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
552    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
553    #final_status_refl #the_trace_refl
554    generalize in match execute_assm; destruct #execute_assm
555    whd in match (trace_any_label_length … (tal_step_call …));
556    whd in match (compute_paid_trace_any_label … (tal_step_call …));
557    whd in costed_assm:(?%);
558    generalize in match costed_assm;
559    generalize in match (refl … (lookup_opt … (program_counter … status_after_fun_call) cost_labels));
560    whd in match (as_label ??);
561    generalize in match (lookup_opt … (program_counter … status_after_fun_call) cost_labels)
562      in ⊢ (??%? → % → ?);
563    #lookup_assm cases lookup_assm
564    [1:
565      #None_lookup_opt_assm normalize nodelta #ignore
566      generalize in match recursive_assm;
567      cut(program_counter'' = program_counter … status_after_fun_call)
568      [1:
569        generalize in match after_return_assm;
570        whd in ⊢ (% → ?); <FETCH normalize nodelta #relevant >relevant %
571      |2:
572        #program_counter_refl >program_counter_refl <None_lookup_opt_assm
573        normalize nodelta #new_recursive_assm
574        cases (new_recursive_assm … trace_any_label ? ?)
575        [1:
576          @plus_right_monotone whd in ⊢ (???%); <FETCH %
577        |2:
578          @le_S_S_to_le @size_invariant
579        |3:
580          %
581        ]
582      ]
583    |2:
584      #cost_label #Some_lookup_opt_assm #absurd
585      cases (not_Some_neq_None_to_False ?? absurd)
586    ]
587  ]
588  try (change with (Some ? (ASM_classify0 ?) = ? ∨ Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
589  try (change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
590  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
591  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
592  <FETCH in classifier_assm; >classify_assm #absurd destruct(absurd) cases absurd
593  #absurd destruct(absurd)
594qed.
595
596lemma trace_compute_paid_trace_cl_return:
597  ∀code_memory' : (BitVectorTrie Byte 16).
598  ∀program_counter' : Word.
599  ∀cost_labels : BitVectorTrie costlabel 16.
600  ∀program_size' : ℕ.
601  ∀ticks : ℕ.
602  ∀instruction : instruction.
603  ∀program_counter'' : Word.
604  ∀FETCH : (〈instruction,program_counter'',ticks〉=fetch code_memory' program_counter').
605  ∀start_status : (Status code_memory').
606  ∀final_status : (Status code_memory').
607  ∀trace_ends_flag : trace_ends_with_ret.
608  ∀the_trace : trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
609  ∀program_counter_refl : (program_counter' = program_counter … start_status).
610  ∀classify_assm: ASM_classify0 instruction = cl_return.
611    ticks
612     =compute_paid_trace_any_label … the_trace.
613  #code_memory' #program_counter' #cost_labels #program_size'
614  #ticks #instruction #program_counter'' #FETCH
615  #start_status #final_status #trace_ends_flag
616  #the_trace #program_counter_refl #classify_assm
617  @(trace_any_label_inv_ind … the_trace)
618  [1:
619    #start_status' #final_status' #execute_assm #classifier_assm #costed_assm
620    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
621    destruct @⊥
622  |2:
623    #start_status' #final_status' #execute_assm #classifier_assm #trace_ends_flag_refl
624    #start_status_refl #final_status_refl #the_trace_refl destruct
625    whd in match (trace_any_label_length … (tal_base_return …));
626    whd in match (compute_paid_trace_any_label … (tal_base_return …));
627    <FETCH %
628  |3:
629    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
630    #classifier_assm #after_return_assm #trace_label_return #costed_assm
631    #trace_ends_flag_refl #start_status_refl #final_status_refl #the_trace_refl
632    destruct @⊥
633  |4:
634    #status_pre_fun_call #status_start_fun_call #status_final #execute_assm
635    #classifier_assm
636    destruct @⊥
637    change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;
638    cases (current_instruction ??) in classifier_assm;
639    [7: #preinstruction cases preinstruction ]
640    try(#addr1 #addr2 #absurd destruct(absurd))
641    try(#addr #absurd destruct(absurd))
642    #absurd destruct(absurd)
643  |5:
644    #end_flag #status_pre_fun_call #status_start_fun_call #status_after_fun_call
645    #status_final #execute_assm #classifier_assm #after_return_assm #trace_label_return
646    #costed_assm #trace_any_label #trace_ends_flag_refl #start_status_refl
647    #final_status_refl #the_trace_refl
648    destruct @⊥
649  |6:
650    #end_flag #status_pre #status_init #status_end #execute_assm #trace_any_label
651    #classifier_assm #costed_assm #trace_ends_flag_refl #start_status_refl
652    #final_status_refl #the_trace_refl destruct @⊥
653  ]
654  try (change with (Some ? (ASM_classify0 ?) = ? ∨ Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
655  try (change with (Some ? (ASM_classify0 ?) = ?) in classifier_assm;)
656  whd in match current_instruction in classifier_assm; normalize nodelta in classifier_assm;
657  whd in match current_instruction0 in classifier_assm; normalize nodelta in classifier_assm;
658  <FETCH in classifier_assm; >classify_assm
659  #absurd try (destruct(absurd))
660  cases absurd
661  #absurd destruct(absurd)
662qed.
663
664lemma trace_any_label_length_leq_0_to_False:
665  ∀code_memory: BitVectorTrie Byte 16.
666  ∀cost_labels: BitVectorTrie costlabel 16.
667  ∀trace_ends_flag: trace_ends_with_ret.
668  ∀start_status: Status code_memory.
669  ∀final_status: Status code_memory.
670  ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
671   trace_any_label_length … the_trace ≤ 0 → False.
672  #code_memory #cost_labels #trace_ends_flag #start_status #final_status
673  #the_trace
674  cases the_trace /2/
675qed.
676     
677let rec block_cost'
678  (code_memory': BitVectorTrie Byte 16) (program_counter': Word)
679    (program_size: nat) (cost_labels: BitVectorTrie costlabel 16)
680      (first_time_around: bool)
681        on program_size:
682          Σcost_of_block: nat.
683          if (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) then
684            ∀start_status: Status code_memory'.
685            ∀final_status: Status code_memory'.
686            ∀trace_ends_flag.
687            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
688              trace_any_label_length … the_trace ≤ program_size →
689              program_counter' = program_counter … start_status →
690                cost_of_block = compute_paid_trace_any_label … the_trace
691          else
692            (cost_of_block = 0) ≝
693  match program_size return λx. x = program_size → ? with
694  [ O ⇒ λbase_case. 0 (* XXX: dummy to be inserted here *)
695  | S program_size' ⇒ λstep_case.
696    let 〈instruction, program_counter'', ticks〉 as FETCH ≝ fetch code_memory' program_counter' in
697    let to_continue ≝
698      match lookup_opt … program_counter' cost_labels with
699      [ None ⇒ true
700      | Some _ ⇒ first_time_around
701      ]
702    in
703      ((if to_continue then
704       pi1 … (match instruction return λx. x = instruction → ? with
705        [ RealInstruction real_instruction ⇒ λreal_instruction_refl.
706          match real_instruction return λx. x = real_instruction →
707          Σcost_of_block: nat.
708            ∀start_status: Status code_memory'.
709            ∀final_status: Status code_memory'.
710            ∀trace_ends_flag.
711            ∀the_trace: trace_any_label (ASM_abstract_status code_memory' cost_labels) trace_ends_flag start_status final_status.
712              trace_any_label_length … the_trace ≤ S program_size' →
713              program_counter' = program_counter … start_status →
714                cost_of_block = compute_paid_trace_any_label … the_trace with
715          [ RET                    ⇒ λinstr. ticks
716          | RETI                   ⇒ λinstr. ticks
717          | JC   relative          ⇒ λinstr. ticks
718          | JNC  relative          ⇒ λinstr. ticks
719          | JB   bit_addr relative ⇒ λinstr. ticks
720          | JNB  bit_addr relative ⇒ λinstr. ticks
721          | JBC  bit_addr relative ⇒ λinstr. ticks
722          | JZ   relative          ⇒ λinstr. ticks
723          | JNZ  relative          ⇒ λinstr. ticks
724          | CJNE src_trgt relative ⇒ λinstr. ticks
725          | DJNZ src_trgt relative ⇒ λinstr. ticks
726          | JMP   addr             ⇒ λinstr. (* XXX: actually a call due to use with fptrs *)
727              ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
728          | _                      ⇒ λinstr.
729              ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
730          ] (refl …)
731        | ACALL addr     ⇒ λinstr.
732            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
733        | AJMP  addr     ⇒ λinstr.
734          let jump_target ≝ compute_target_of_unconditional_jump program_counter'' instruction in
735            ticks + block_cost' code_memory' jump_target program_size' cost_labels false
736        | LCALL addr     ⇒ λinstr.
737            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
738        | LJMP  addr     ⇒ λinstr.
739          let jump_target ≝ compute_target_of_unconditional_jump program_counter'' instruction in
740            ticks + block_cost' code_memory' jump_target program_size' cost_labels false
741        | SJMP  addr     ⇒ λinstr.
742          let jump_target ≝ compute_target_of_unconditional_jump program_counter'' instruction in
743            ticks + block_cost' code_memory' jump_target program_size' cost_labels false
744        | MOVC  src trgt ⇒ λinstr.
745            ticks + block_cost' code_memory' program_counter'' program_size' cost_labels false
746        ] (refl …))
747      else
748        0)
749      : Σcost_of_block: nat.
750          match (match lookup_opt … program_counter' cost_labels with [ None ⇒ true | _ ⇒ first_time_around ]) with
751          [ true ⇒
752            ∀start_status: Status code_memory'.
753            ∀final_status: Status code_memory'.
754            ∀trace_ends_flag.
755            ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
756              trace_any_label_length … the_trace ≤ S program_size' →
757              program_counter' = program_counter … start_status →
758                cost_of_block = compute_paid_trace_any_label … the_trace
759          | false ⇒
760            (cost_of_block = 0)
761          ])
762  ] (refl …).
763  [2:
764    change with (if to_continue then ? else (? = 0))
765    >p in ⊢ (match % return ? with [ _ ⇒ ? | _ ⇒ ? ]); normalize nodelta
766    @pi2
767  |1:
768    destruct
769    cases (lookup_opt ????) normalize nodelta
770    [1:
771      #start_status #final_status #trace_ends_flag #the_trace
772      #absurd
773      cases (trace_any_label_length_leq_0_to_False … absurd)
774    |2:
775      #cost cases first_time_around normalize nodelta try %
776      #start_status #final_status #trace_ends_flag #the_trace #absurd
777      cases (trace_any_label_length_leq_0_to_False … absurd)
778    ]
779  |3:
780    change with (if to_continue then ? else (0 = 0))
781    >p normalize nodelta %
782  |6,7:
783    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
784    @(trace_compute_paid_trace_cl_return … program_size' … FETCH … the_trace program_counter_refl)
785    destruct %
786  |4,46,47:
787    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
788    cases(block_cost' ?????) -block_cost'
789    @(trace_compute_paid_trace_cl_call … program_size' … FETCH … the_trace program_counter_refl size_invariant)
790    destruct %
791  |43,44,45: (* XXX: unconditional jumps *)
792    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
793    cases (block_cost' ?????) -block_cost'
794    lapply (trace_compute_paid_trace_cl_other … program_size' … FETCH … the_trace program_counter_refl size_invariant)
795    whd in match (program_counter_after_other ??); normalize nodelta destruct
796    whd in match (is_unconditional_jump ?); normalize nodelta #assm @assm %
797  |25,26,27,28,29,30,31,32,33:
798    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
799    @(trace_compute_paid_trace_cl_jump … first_time_around program_size' … FETCH … the_trace program_counter_refl)
800    destruct %
801  |4,33:
802  |*:
803    #start_status #final_status #trace_ends_flag #the_trace #size_invariant #program_counter_refl
804    cases(block_cost' ?????) -block_cost'
805    lapply (trace_compute_paid_trace_cl_other … program_size' … FETCH … the_trace program_counter_refl size_invariant)     
806    destruct #assm @assm %
807  ]
808qed.
809
810definition block_cost:
811    ∀code_memory': BitVectorTrie Byte 16.
812    ∀program_counter': Word.
813    ∀cost_labels: BitVectorTrie costlabel 16.
814      Σcost_of_block: nat.
815        ∀start_status: Status code_memory'.
816        ∀final_status: Status code_memory'.
817        ∀trace_ends_flag.
818        ∀the_trace: trace_any_label (ASM_abstract_status … cost_labels) trace_ends_flag start_status final_status.
819        ∀unrepeating_witness: tal_unrepeating … the_trace.
820          program_counter' = program_counter … start_status →
821            cost_of_block = compute_paid_trace_any_label … the_trace ≝
822  λcode_memory: BitVectorTrie Byte 16.
823  λprogram_counter: Word.
824  λcost_labels: BitVectorTrie costlabel 16. ?.
825  cases(block_cost' code_memory program_counter (2^16) cost_labels true)
826  #cost_of_block #block_cost_hyp
827  %{cost_of_block}
828  cases(lookup_opt … cost_labels) in block_cost_hyp;
829  [2: #cost_label] normalize nodelta
830  #hyp #start_status #final_status #trace_ends_flag #the_trace #unrepeating_witness
831  @hyp @tal_pc_list_length_leq_total_program_size try assumption
832qed.
Note: See TracBrowser for help on using the repository browser.