source: src/ASM/PolicyFront.ma @ 2276

Last change on this file since 2276 was 2273, checked in by sacerdot, 7 years ago
  1. lemmas moved from all files to Test.ma
  2. most of the lemmas in Test.ma repaired. A few are commented out. Several needs to be slightly generalized.
File size: 31.7 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Status.ma".
5include "utilities/extralib.ma".
6include "ASM/Assembly.ma".
7
8(* Internal types *)
9
10(* ppc_pc_map: program length × (pseudo program counter ↦ 〈pc, jump_length〉) *)
11definition ppc_pc_map ≝ ℕ × (BitVectorTrie (ℕ × jump_length) 16).
12
13(* The different properties that we want/need to prove at some point *)
14(* During our iteration, everything not yet seen is None, and vice versa *)
15definition out_of_program_none ≝
16  λprefix:list labelled_instruction.λsigma:ppc_pc_map.
17  ∀i.i < 2^16 → (i > |prefix| ↔ bvt_lookup_opt … (bitvector_of_nat ? i) (\snd sigma) = None ?).
18
19(* If instruction i is a jump, then there will be something in the policy at
20 * position i *)
21definition is_jump' ≝
22  λx:preinstruction Identifier.
23  match x with
24  [ JC _ ⇒ true
25  | JNC _ ⇒ true
26  | JZ _ ⇒ true
27  | JNZ _ ⇒ true
28  | JB _ _ ⇒ true
29  | JNB _ _ ⇒ true
30  | JBC _ _ ⇒ true
31  | CJNE _ _ ⇒ true
32  | DJNZ _ _ ⇒ true
33  | _ ⇒ false
34  ].
35
36definition is_relative_jump ≝
37  λinstr:pseudo_instruction.
38  match instr with
39  [ Instruction i ⇒ is_jump' i
40  | _             ⇒ false
41  ].
42   
43definition is_jump ≝
44  λinstr:pseudo_instruction.
45  match instr with
46  [ Instruction i   ⇒ is_jump' i
47  | Call _ ⇒ true
48  | Jmp _ ⇒ true
49  | _ ⇒ false
50  ].
51
52definition is_call ≝
53  λinstr:pseudo_instruction.
54  match instr with
55  [ Call _ ⇒ true
56  | _ ⇒ false
57  ].
58 
59definition is_jump_to ≝
60  λx:pseudo_instruction.λd:Identifier.
61  match x with
62  [ Instruction i ⇒ match i with
63    [ JC j ⇒ d = j
64    | JNC j ⇒ d = j
65    | JZ j ⇒ d = j
66    | JNZ j ⇒ d = j
67    | JB _ j ⇒ d = j
68    | JNB _ j ⇒ d = j
69    | JBC _ j ⇒ d = j
70    | CJNE _ j ⇒ d = j
71    | DJNZ _ j ⇒ d = j
72    | _ ⇒ False
73    ]
74  | Call c ⇒ d = c
75  | Jmp j ⇒ d = j
76  | _ ⇒ False
77  ].
78 
79definition not_jump_default ≝
80  λprefix:list labelled_instruction.λsigma:ppc_pc_map.
81  ∀i:ℕ.i < |prefix| →
82  ¬is_jump (\snd (nth i ? prefix 〈None ?, Comment []〉)) →
83  \snd (bvt_lookup … (bitvector_of_nat ? i) (\snd sigma) 〈0,short_jump〉) = short_jump.
84 
85(* Between two policies, jumps cannot decrease *)
86definition jmpeqb: jump_length → jump_length → bool ≝
87  λj1.λj2.
88  match j1 with
89  [ short_jump ⇒ match j2 with [ short_jump ⇒ true | _ ⇒ false ]
90  | absolute_jump ⇒ match j2 with [ absolute_jump ⇒ true | _ ⇒ false ]
91  | long_jump ⇒ match j2 with [ long_jump ⇒ true | _ ⇒ false ]
92  ].
93
94lemma jmpeqb_to_eq: ∀j1,j2.jmpeqb j1 j2 → j1 = j2.
95 #j1 #j2 cases j1 cases j2
96 [1,5,9: / by /]
97 #H cases H
98qed.
99
100definition jmple: jump_length → jump_length → Prop ≝
101  λj1.λj2.
102  match j1 with
103  [ short_jump  ⇒
104    match j2 with
105    [ short_jump ⇒ False
106    | _          ⇒ True
107    ]
108  | absolute_jump ⇒
109    match j2 with
110    [ long_jump ⇒ True
111    | _         ⇒ False
112    ]
113  | long_jump   ⇒ False
114  ].
115
116definition jmpleq: jump_length → jump_length → Prop ≝
117  λj1.λj2.jmple j1 j2 ∨ j1 = j2.
118 
119definition jump_increase ≝
120 λprefix:list labelled_instruction.λop:ppc_pc_map.λp:ppc_pc_map.
121 ∀i.i ≤ |prefix| →
122   let 〈opc,oj〉 ≝ bvt_lookup … (bitvector_of_nat ? i) (\snd op) 〈0,short_jump〉 in
123   let 〈pc,j〉 ≝ bvt_lookup … (bitvector_of_nat ? i) (\snd p) 〈0,short_jump〉 in
124     jmpleq oj j.
125     
126(* this is the instruction size as determined by the jump length given *)
127definition expand_relative_jump_internal_unsafe:
128  jump_length → ([[relative]] → preinstruction [[relative]]) → list instruction ≝
129  λjmp_len:jump_length.λi.
130  match jmp_len with
131  [ short_jump ⇒ [ RealInstruction (i (RELATIVE (zero 8))) ]
132  | absolute_jump ⇒ [ ] (* this should not happen *)
133  | long_jump ⇒
134    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
135      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
136      LJMP (ADDR16 (zero 16))
137    ]
138  ].
139 @I
140qed.
141
142definition strip_target:
143  preinstruction Identifier →
144   ([[relative]] → preinstruction [[relative]]) ⊎ instruction ≝
145  λi.
146  match i with
147  [ JC _ ⇒ inl … (JC ?)
148  | JNC _ ⇒ inl … (JNC ?)
149  | JB baddr _ ⇒ inl … (JB ? baddr)
150  | JZ _ ⇒ inl … (JZ ?)
151  | JNZ _ ⇒ inl … (JNZ ?)
152  | JBC baddr _ ⇒ inl … (JBC ? baddr)
153  | JNB baddr _ ⇒ inl … (JNB ? baddr)
154  | CJNE addr _ ⇒ inl … (CJNE ? addr)
155  | DJNZ addr _ ⇒ inl … (DJNZ ? addr)
156  | ADD arg1 arg2 ⇒ inr … (ADD ? arg1 arg2)
157  | ADDC arg1 arg2 ⇒ inr … (ADDC ? arg1 arg2)
158  | SUBB arg1 arg2 ⇒ inr … (SUBB ? arg1 arg2)
159  | INC arg ⇒ inr … (INC ? arg)
160  | DEC arg ⇒ inr … (DEC ? arg)
161  | MUL arg1 arg2 ⇒ inr … (MUL ? arg1 arg2)
162  | DIV arg1 arg2 ⇒ inr … (DIV ? arg1 arg2)
163  | DA arg ⇒ inr … (DA ? arg)
164  | ANL arg ⇒ inr … (ANL ? arg)
165  | ORL arg ⇒ inr … (ORL ? arg)
166  | XRL arg ⇒ inr … (XRL ? arg)
167  | CLR arg ⇒ inr … (CLR ? arg)
168  | CPL arg ⇒ inr … (CPL ? arg)
169  | RL arg ⇒ inr … (RL ? arg)
170  | RR arg ⇒ inr … (RR ? arg)
171  | RLC arg ⇒ inr … (RLC ? arg)
172  | RRC arg ⇒ inr … (RRC ? arg)
173  | SWAP arg ⇒ inr … (SWAP ? arg)
174  | MOV arg ⇒ inr … (MOV ? arg)
175  | MOVX arg ⇒ inr … (MOVX ? arg)
176  | SETB arg ⇒ inr … (SETB ? arg)
177  | PUSH arg ⇒ inr … (PUSH ? arg)
178  | POP arg ⇒ inr … (POP ? arg)
179  | XCH arg1 arg2 ⇒ inr … (XCH ? arg1 arg2)
180  | XCHD arg1 arg2 ⇒ inr … (XCHD ? arg1 arg2)
181  | RET ⇒ inr … (RET ?)
182  | RETI ⇒ inr … (RETI ?)
183  | NOP ⇒ inr … (RealInstruction (NOP ?))
184  ].
185
186definition expand_relative_jump_unsafe:
187  jump_length → preinstruction Identifier → list instruction ≝
188  λjmp_len:jump_length.λi.
189  match strip_target i with
190  [ inl jmp ⇒ expand_relative_jump_internal_unsafe jmp_len jmp
191  | inr instr ⇒ [ instr ] ].
192
193definition expand_pseudo_instruction_unsafe:
194 jump_length → pseudo_instruction → list instruction ≝
195  λjmp_len.
196  λi.
197  match i with
198  [ Cost cost ⇒ [ ]
199  | Comment comment ⇒ [ ]
200  | Call call ⇒
201    match jmp_len with
202    [ short_jump ⇒ [ ] (* this should not happen *)
203    | absolute_jump ⇒ [ ACALL (ADDR11 (zero 11)) ]
204    | long_jump ⇒ [ LCALL (ADDR16 (zero 16)) ]
205    ]
206  | Mov d trgt ⇒
207     [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, DATA16 (zero 16)〉))))]
208  | Instruction instr ⇒ expand_relative_jump_unsafe jmp_len instr
209  | Jmp jmp ⇒
210    match jmp_len with
211    [ short_jump ⇒ [ SJMP (RELATIVE (zero 8)) ]
212    | absolute_jump ⇒ [ AJMP (ADDR11 (zero 11)) ]
213    | long_jump ⇒ [ LJMP (ADDR16 (zero 16)) ]
214    ]
215  ].
216 %
217qed.
218
219definition instruction_size_jmplen:
220 jump_length → pseudo_instruction → ℕ ≝
221  λjmp_len.
222  λi.
223  let mapped ≝ map ? ? assembly1 (expand_pseudo_instruction_unsafe jmp_len i) in
224  let flattened ≝ flatten ? mapped in
225  let pc_len ≝ length ? flattened in
226    pc_len.
227
228definition sigma_compact_unsafe ≝
229 λprefix:list labelled_instruction.λlabels:label_map.λsigma:ppc_pc_map.
230 ∀n.n < |prefix| →
231  match bvt_lookup_opt … (bitvector_of_nat ? n) (\snd sigma) with
232  [ None ⇒ False
233  | Some x ⇒ let 〈pc,j〉 ≝ x in
234    match bvt_lookup_opt … (bitvector_of_nat ? (S n)) (\snd sigma) with
235    [ None ⇒ False
236    | Some x1 ⇒ let 〈pc1,j1〉 ≝ x1 in
237       pc1 = pc + instruction_size_jmplen j (\snd (nth n ? prefix 〈None ?, Comment []〉))
238    ]
239  ].
240
241(* new safety condition: sigma corresponds to program and resulting program is compact *)
242definition sigma_compact ≝
243 λprogram:list labelled_instruction.λlabels:label_map.λsigma:ppc_pc_map.
244 ∀n.n < |program| →
245  match bvt_lookup_opt … (bitvector_of_nat ? n) (\snd sigma) with
246  [ None ⇒ False
247  | Some x ⇒ let 〈pc,j〉 ≝ x in
248    match bvt_lookup_opt … (bitvector_of_nat ? (S n)) (\snd sigma) with
249    [ None ⇒ False
250    | Some x1 ⇒ let 〈pc1,j1〉 ≝ x1 in
251       pc1 = pc + instruction_size (λid.bitvector_of_nat ? (lookup_def ?? labels id 0))
252         (λppc.bitvector_of_nat ? (\fst (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
253         (λppc.jmpeqb long_jump (\snd (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
254         (bitvector_of_nat ? n) (\snd (nth n ? program 〈None ?, Comment []〉))
255    ]
256  ].
257
258(* jumps are of the proper size *)
259definition sigma_safe ≝
260 λprefix:list labelled_instruction.λlabels:label_map.λadded:ℕ.
261 λold_sigma:ppc_pc_map.λsigma:ppc_pc_map.
262 ∀i.i < |prefix| →
263 let 〈pc,j〉 ≝ bvt_lookup … (bitvector_of_nat ? i) (\snd sigma) 〈0,short_jump〉 in
264 let pc_plus_jmp_length ≝ bitvector_of_nat ? (\fst (bvt_lookup … (bitvector_of_nat ? (S i)) (\snd sigma) 〈0,short_jump〉)) in
265 let 〈label,instr〉 ≝ nth i ? prefix 〈None ?, Comment [ ]〉 in
266 ∀dest.is_jump_to instr dest →
267   let paddr ≝ lookup_def … labels dest 0 in
268   let addr ≝ bitvector_of_nat ? (if leb paddr (|prefix|) (* jump to address already known *)
269   then \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd sigma) 〈0,short_jump〉)
270   else \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
271   match j with
272   [ short_jump ⇒ \fst (short_jump_cond pc_plus_jmp_length addr) = true ∧
273       bool_to_Prop (¬is_call instr)
274   | absolute_jump ⇒  \fst (absolute_jump_cond pc_plus_jmp_length addr) = true ∧
275       \fst (short_jump_cond pc_plus_jmp_length addr) = false ∧
276       bool_to_Prop (¬is_relative_jump instr)
277   | long_jump   ⇒ \fst (short_jump_cond pc_plus_jmp_length addr) = false ∧
278       \fst (absolute_jump_cond pc_plus_jmp_length addr) = false
279   ].
280 
281(* Definitions and theorems for the jump_length type (itself defined in Assembly) *)
282definition max_length: jump_length → jump_length → jump_length ≝
283  λj1.λj2.
284  match j1 with
285  [ long_jump   ⇒ long_jump
286  | absolute_jump ⇒
287    match j2 with
288    [ absolute_jump ⇒ absolute_jump
289    | _           ⇒ long_jump
290    ]
291  | short_jump  ⇒
292    match j2 with
293    [ short_jump ⇒ short_jump
294    | _          ⇒ long_jump
295    ]
296  ].
297
298lemma dec_jmple: ∀x,y:jump_length.Sum (jmple x y) (¬(jmple x y)).
299 #x #y cases x cases y /3 by inl, inr, nmk, I/
300qed.
301 
302lemma jmpleq_max_length: ∀ol,nl.
303  jmpleq ol (max_length ol nl).
304 #ol #nl cases ol cases nl
305 /2 by or_introl, or_intror, I/
306qed.
307
308lemma dec_eq_jump_length: ∀a,b:jump_length.Sum (a = b) (a ≠ b).
309  #a #b cases a cases b /2/
310  %2 @nmk #H destruct (H)
311qed.
312 
313(* The function that creates the label-to-address map *)
314definition create_label_map: ∀program:list labelled_instruction.
315  (Σlabels:label_map.
316    ∀l.occurs_exactly_once ?? l program →
317    (*And (bitvector_of_nat ? (lookup_def ?? labels l 0) =
318     address_of_word_labels program l)
319    ( *)lookup_def ?? labels l 0 < |program|(*)*)
320  ) ≝
321 λprogram.
322   \fst (create_label_cost_map program).
323 #l #Hl cases (create_label_cost_map_ok program) #_ #X @(X … Hl)
324qed.
325
326(* General note on jump length selection: the jump displacement is added/replaced
327 * AFTER the fetch (and attendant PC increase), but we calculate before the
328 * fetch, which means that in the case of a short and medium jump we are 2
329 * bytes off and have to compensate.
330 * For the long jump we don't care, because the PC gets replaced integrally anyway. *)
331definition select_reljump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
332  Identifier → jump_length ≝
333  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
334  let pc ≝ \fst inc_sigma in
335  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
336  let paddr ≝ lookup_def … labels lbl 0 in
337  let addr ≝ bitvector_of_nat ? (if leb paddr ppc (* jump to address already known *)
338  then \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉)
339  else \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
340  let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length addr in
341  if sj_possible
342  then short_jump
343  else long_jump.
344
345definition select_call_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
346  Identifier → jump_length ≝
347  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
348  let pc ≝ \fst inc_sigma in
349  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
350  let paddr ≝ lookup_def ? ? labels lbl 0 in
351  let addr ≝ bitvector_of_nat ?
352    (if leb paddr ppc (* jump to address already known *)
353    then \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd inc_sigma) 〈0,short_jump〉)
354    else \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
355  let 〈aj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length addr in   
356  if aj_possible
357  then absolute_jump
358  else long_jump.
359 
360definition select_jump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
361  Identifier → jump_length ≝
362  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
363  let pc ≝ \fst inc_sigma in
364  let pc_plus_jmp_length ≝ bitvector_of_nat ? (pc+2) in
365  let paddr ≝ lookup_def … labels lbl 0 in
366  let addr ≝ bitvector_of_nat ? (if leb paddr ppc (* jump to address already known *)
367  then \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉)
368  else \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)+added) in
369  let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length addr in
370  if sj_possible
371  then short_jump
372  else select_call_length labels old_sigma inc_sigma added ppc lbl.
373
374definition destination_of: preinstruction Identifier → option Identifier ≝
375  λi.
376  match i with
377  [ JC j     ⇒ Some ? j
378  | JNC j    ⇒ Some ? j
379  | JZ j     ⇒ Some ? j
380  | JNZ j    ⇒ Some ? j
381  | JB _ j   ⇒ Some ? j
382  | JBC _ j  ⇒ Some ? j
383  | JNB _ j  ⇒ Some ? j
384  | CJNE _ j ⇒ Some ? j
385  | DJNZ _ j ⇒ Some ? j
386  | _        ⇒ None ?
387  ].
388
389definition jump_expansion_step_instruction: label_map → ppc_pc_map → ppc_pc_map →
390  ℕ → ℕ → preinstruction Identifier → option jump_length ≝
391  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λi.
392  match destination_of i with
393  [ Some j     ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
394  | None       ⇒ None ?
395  ].
396
397(* The first step of the jump expansion: everything to short. *)
398definition jump_expansion_start:
399  ∀program:(Σl:list labelled_instruction.S (|l|) < 2^16 ∧ is_well_labelled_p l).
400  ∀labels:label_map.
401  Σpolicy:option ppc_pc_map.
402    match policy with
403    [ None ⇒ True
404    | Some p ⇒ And (And (And (And (And
405       (not_jump_default (pi1 ?? program) p)
406       (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd p) 〈0,short_jump〉) = 0))
407       (\fst p = \fst (bvt_lookup … (bitvector_of_nat ? (|program|)) (\snd p) 〈0,short_jump〉)))
408       (sigma_compact_unsafe program labels p))
409       (∀i.i ≤ |program| → ∃pc.
410         bvt_lookup_opt … (bitvector_of_nat ? i) (\snd p) = Some ? 〈pc,short_jump〉))
411       (\fst p ≤ 2^16)         
412    ] ≝
413  λprogram.λlabels.
414  let final_policy ≝ foldl_strong (option Identifier × pseudo_instruction)
415  (λprefix.Σpolicy:ppc_pc_map.And (And (And (And
416    (not_jump_default prefix policy)
417    (\fst (bvt_lookup … (bitvector_of_nat ? 0) (\snd policy) 〈0,short_jump〉) = 0))
418    (\fst policy = \fst (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd policy) 〈0,short_jump〉)))
419    (sigma_compact_unsafe prefix labels policy))
420    (∀i.i ≤ |prefix| → ∃pc.
421      bvt_lookup_opt … (bitvector_of_nat ? i) (\snd policy) = Some ? 〈pc,short_jump〉))
422  program
423  (λprefix.λx.λtl.λprf.λp.
424   let 〈pc,sigma〉 ≝ pi1 ?? p in
425   let 〈label,instr〉 ≝ x in
426   let isize ≝ instruction_size_jmplen short_jump instr in
427   〈pc + isize, bvt_insert … (bitvector_of_nat 16 (S (|prefix|))) 〈pc+isize,short_jump〉 sigma〉
428  ) 〈0, bvt_insert ?? (bitvector_of_nat 16 0) 〈0,short_jump〉 (Stub ??)〉 in
429  if gtb (\fst (pi1 ?? final_policy)) 2^16 then
430    None ?
431  else
432    Some ? (pi1 ?? final_policy).
433[ / by I/
434| lapply p -p cases final_policy -final_policy #p #Hp #hg
435  @conj [ @Hp | @not_lt_to_le @ltb_false_to_not_lt @hg ]
436| @conj [ @conj [ @conj [ @conj
437  [ (* not_jump_default *) cases p -p #p cases p -p #pc #sigma #Hp
438    cases x in prf; #lbl #ins #prf #i >append_length <commutative_plus #Hi
439    normalize in Hi; normalize nodelta cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
440    [ >lookup_insert_miss
441      [ (* USE[pass]: not_jump_default *)
442        lapply (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp))) i Hi)
443        >nth_append_first
444        [ #H #H2 @H @H2
445        | @Hi
446        ]
447      | @bitvector_of_nat_abs
448        [ @(transitive_lt ??? Hi) @le_S_to_le]
449        [1,2: @(transitive_lt … (proj1 ?? (pi2 ?? program))) @le_S_S >prf >append_length
450          <plus_n_Sm @le_S_S @le_plus_n_r
451        | @lt_to_not_eq @le_S @Hi
452        ]
453      ]
454    | >Hi >lookup_insert_miss
455      [ #_ (* USE: everything is short *)
456        elim ((proj2 ?? Hp) (|prefix|) (le_n (|prefix|))) #pc #Hl
457        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) @refl
458      | @bitvector_of_nat_abs
459        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length @le_plus_n_r
460        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm @le_S_S
461          @le_plus_n_r
462        | @lt_to_not_eq @le_n
463        ]
464      ]
465    ]
466  | (* 0 ↦ 0 *)
467    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
468    >lookup_insert_miss
469    [ (* USE[pass]: 0 ↦ 0 *)
470      @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hp))))
471    | @bitvector_of_nat_abs
472      [ / by /
473      | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length @le_S_S <plus_n_Sm
474        @le_S_S @le_plus_n_r
475      | @lt_to_not_eq / by /
476      ]
477    ]
478  ]
479  | (* fst p = pc *)
480    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
481    >append_length >(commutative_plus (|prefix|)) >lookup_insert_hit @refl
482  ]
483  | (* policy_compact_unsafe *) #i >append_length <commutative_plus #Hi normalize in Hi;
484    cases p -p #p cases p -p #fpc #sigma #Hp cases x #lbl #instr normalize nodelta
485    cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
486    [ >lookup_opt_insert_miss
487      [ >lookup_opt_insert_miss
488        [ (* USE[pass]: policy_compact_unsafe *)
489          lapply (proj2 ?? (proj1 ?? Hp) i Hi)
490          lapply (refl ? (bvt_lookup_opt … (bitvector_of_nat ? i) sigma))
491          cases (bvt_lookup_opt … (bitvector_of_nat ? i) sigma) in ⊢ (???% → %);
492          [ #_ normalize nodelta / by /
493          | #x cases x -x #pci #ji #EQi
494            lapply (refl ? (bvt_lookup_opt … (bitvector_of_nat ? (S i)) sigma))
495            cases (bvt_lookup_opt … (bitvector_of_nat ? (S i)) sigma) in ⊢ (???% → %);
496            [ #_ normalize nodelta / by /
497            | #x cases x -x #pcSi #jSi #EQSi normalize nodelta >nth_append_first
498              [ / by /
499              | @Hi
500              ]
501            ]
502          ]
503        ]
504      ]
505      [2: lapply (le_S_to_le … Hi) -Hi #Hi]
506      @bitvector_of_nat_abs
507      [1,4: @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <commutative_plus
508        @le_plus_a @Hi
509      |2,5: @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm
510        @le_S_S @le_plus_n_r
511      |3,6: @lt_to_not_eq @le_S_S @Hi
512      ]
513    | >lookup_opt_insert_miss
514      [ >Hi >lookup_opt_insert_hit normalize nodelta
515        (* USE: everything is short, fst p = pc *)
516        elim ((proj2 ?? Hp) (|prefix|) (le_n ?)) #pc #Hl
517        lapply (proj2 ?? (proj1 ?? (proj1 ?? Hp))) >Hl
518        >(lookup_opt_lookup_hit … Hl 〈0,short_jump〉) #EQ normalize nodelta >nth_append_second
519        [ <minus_n_n whd in match (nth ????); >EQ @refl
520        | @le_n
521        ]
522      | @bitvector_of_nat_abs
523        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >Hi >prf @le_S_S >append_length <commutative_plus
524          @le_plus_a @le_n
525        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf @le_S_S >append_length <plus_n_Sm
526          @le_S_S @le_plus_n_r
527        | @lt_to_not_eq @le_S_S >Hi @le_n
528        ]
529      ]
530    ]
531  ]
532  | (* everything is short *) #i >append_length <commutative_plus #Hi normalize in Hi;
533    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
534    cases (le_to_or_lt_eq … Hi) -Hi #Hi
535    [ >lookup_opt_insert_miss
536      [ (* USE[pass]: everything is short *)
537        @((proj2 ?? Hp) i (le_S_S_to_le … Hi))
538      | @bitvector_of_nat_abs
539        [ @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length @le_S_S
540          >commutative_plus @le_plus_a @le_S_S_to_le @Hi
541        | @(transitive_lt … (proj1 ?? (pi2 ?? program))) >prf >append_length <plus_n_Sm
542          @le_S_S @le_S_S @le_plus_n_r
543        | @lt_to_not_eq @Hi
544        ]
545      ]
546    | >Hi >lookup_opt_insert_hit @(ex_intro ?? (pc+instruction_size_jmplen short_jump instr))
547      @refl
548    ]
549  ]
550| @conj [ @conj [ @conj [ @conj
551  [ #i cases i
552    [ #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
553    | -i #i #Hi #Hj @⊥ @(absurd … Hi) @not_le_Sn_O
554    ]
555  ] ]
556  >lookup_insert_hit @refl
557  | #i cases i
558    [ #Hi @⊥ @(absurd … Hi) @le_to_not_lt @le_n
559    | -i #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
560    ]
561  ]
562  | #i cases i
563    [ #Hi >lookup_opt_insert_hit @(ex_intro ?? 0) @refl
564    | -i #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
565    ]
566  ]
567]
568qed.
569
570(* NOTE: we only compare the first elements here because otherwise the
571 * added = 0 → policy_equal property of jump_expansion_step doesn't hold:
572 * if we have not added anything to the pc, we only know the PC hasn't changed,
573 * there might still have been a short/medium jump change *)
574definition sigma_pc_equal ≝
575  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
576  (∀n.n ≤ |program| →
577    \fst (bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉) =
578    \fst (bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉)).
579
580definition sigma_jump_equal ≝
581  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
582  (∀n.n < |program| →
583    \snd (bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉) =
584    \snd (bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉)).
585   
586definition nec_plus_ultra ≝
587  λprogram:list labelled_instruction.λp:ppc_pc_map.
588  ¬(∀i.i < |program| → is_jump (\snd (nth i ? program 〈None ?, Comment []〉)) →
589  \snd (bvt_lookup … (bitvector_of_nat 16 i) (\snd p) 〈0,short_jump〉) = long_jump).
590 
591(*include alias "common/Identifiers.ma".*)
592include alias "ASM/BitVector.ma".
593include alias "basics/lists/list.ma".
594include alias "arithmetics/nat.ma".
595include alias "basics/logic.ma".
596
597lemma jump_length_equal_max: ∀a,b,i.
598  is_jump i → instruction_size_jmplen (max_length a b) i = instruction_size_jmplen a i →
599  (max_length a b) = a.
600 #a #b #i cases i
601 [1: #pi cases pi
602   try (#x #y #H #EQ) try (#x #H #EQ) try (#H #EQ) cases H
603   cases a in EQ; cases b #EQ try %
604   try (normalize in EQ; destruct(EQ) @False)
605   try (lapply EQ @(subaddressing_mode_elim … x) #w #EQ normalize in EQ; destruct(EQ) @False)
606   lapply EQ -EQ cases x * #a1 #a2 @(subaddressing_mode_elim … a2) #w
607   try (#EQ normalize in EQ; destruct(EQ) @False)
608   @(subaddressing_mode_elim … a1) #w
609   #EQ normalize in EQ; destruct(EQ)
610  |2,3,6: #x [3: #y] #H cases H
611  |4,5: #id #_ cases a cases b #H try % normalize in H; destruct(H)
612 ]
613qed.
614
615lemma jump_length_le_max: ∀a,b,i.is_jump i →
616  instruction_size_jmplen a i ≤ instruction_size_jmplen (max_length a b) i.
617 #a #b #i cases i
618 [2,3,6: #x [3: #y] #H cases H
619 |4,5: #id #_ cases a cases b / by le_n/
620 |1: #pi cases pi
621    try (#x #y #H) try (#x #H) try (#H) cases H
622    -H cases a cases b @leb_true_to_le try %
623    try (@(subaddressing_mode_elim … x) #w % @False)
624    cases x * #a1 #a2 @(subaddressing_mode_elim … a2) #w try %
625    @(subaddressing_mode_elim … a1) #w %
626 ]
627qed.
628
629lemma equal_compact_unsafe_compact: ∀program:(Σl.(S (|l|)) < 2^16 ∧ is_well_labelled_p l).
630  ∀old_sigma.∀sigma.
631  sigma_pc_equal program old_sigma sigma →
632  sigma_safe program (create_label_map program) 0 old_sigma sigma →
633  sigma_compact_unsafe program (create_label_map program) sigma →
634  sigma_compact program (create_label_map program) sigma.
635  #program cases program -program #program #Hprogram #old_sigma #sigma #Hequal
636  #Hsafe #Hcp_unsafe #i #Hi
637  lapply (Hcp_unsafe i Hi) lapply (Hsafe i Hi)
638  inversion (lookup_opt … (bitvector_of_nat ? i) (\snd sigma))
639  [ / by /
640  | #x cases x -x #x1 #x2 #EQ
641    cases (lookup_opt … (bitvector_of_nat ? (S i)) (\snd sigma))
642    [ / by /
643    | #y cases y -y #y1 #y2 normalize nodelta #H #H2
644      (*CSC: make a lemma here; to shorten the proof, reimplement the
645        safe case so that it also does a pattern matching on the jump_length
646        type *)
647      cut (instruction_size_jmplen x2
648       (\snd (nth i ? program 〈None ?, Comment []〉)) =
649       instruction_size … (bitvector_of_nat ? i)
650       (\snd (nth i ? program 〈None ?, Comment []〉)))
651      [5: #H3 <H3 @H2
652      |4: whd in match (instruction_size_jmplen ??);
653          whd in match (instruction_size …);
654          whd in match (assembly_1_pseudoinstruction …);
655          whd in match (expand_pseudo_instruction …);
656          normalize nodelta whd in match (append …) in H; lapply H
657          inversion (nth i ? program 〈None ?,Comment []〉)
658          #lbl #instr cases instr
659          [2,3,6: #x [3: #y] normalize nodelta #H #_ %
660          |4,5: #x >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉) #Heq #Hj
661            lapply (Hj x (refl ? x)) -Hj normalize nodelta
662            >add_bitvector_of_nat_plus <(plus_n_Sm i 0) <plus_n_O
663            cases x2 normalize nodelta
664            [1,4: whd in match short_jump_cond; normalize nodelta
665              cut (lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
666              [1,3: cases (create_label_map program) #clm #Hclm
667                @le_S_to_le @(Hclm x ?)
668                [1: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Jmp x) ??)
669                |2: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Call x) ??)]
670                [1,4: >nat_of_bitvector_bitvector_of_nat_inverse
671                  [2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
672                |2,5: whd in match fetch_pseudo_instruction; normalize nodelta
673                  >nth_safe_nth
674                  [1,3: >nat_of_bitvector_bitvector_of_nat_inverse
675                    [1,3: >Heq / by refl/
676                    |2,4: @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi
677                    ]
678                  ]
679                |3,6: / by /
680                ]
681              |2,4: #H >(le_to_leb_true … H) normalize nodelta <plus_n_O
682              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
683              #result #flags normalize nodelta
684              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
685              cases (get_index' bool 2 0 flags) normalize nodelta
686              [3,4: #H cases (proj2 ?? H)
687              |1,2: cases (eq_bv 9 upper ?)
688                [2,4: #H lapply (proj1 ?? H) #H3 destruct (H3)
689                |1,3: #_ normalize nodelta @refl
690                ]
691              ]
692              ]
693            |2,3,5,6: whd in match short_jump_cond; whd in match absolute_jump_cond;
694              cut (lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
695              [1,3,5,7: cases (create_label_map program) #clm #Hclm
696                @le_S_to_le @(Hclm x ?)
697                [1,2: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Jmp x) ??)
698                |3,4: @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (Call x) ??)]
699                [1,4,7,10: >nat_of_bitvector_bitvector_of_nat_inverse
700                  [2,4,6,8: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
701                |2,5,8,11: whd in match fetch_pseudo_instruction; normalize nodelta
702                  >nth_safe_nth
703                  [1,3,5,7: >nat_of_bitvector_bitvector_of_nat_inverse
704                    [1,3,5,7: >Heq / by refl/
705                    |*: @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi
706                    ]
707                  ]
708                |*: / by /
709                ]
710              |*: #H >(le_to_leb_true … H) normalize nodelta <plus_n_O
711              normalize nodelta cases (vsplit bool 5 11 ?) #addr1 #addr2
712              cases (vsplit bool 5 11 ?) #pc1 #pc2 normalize nodelta
713              cases (sub_16_with_carry (bitvector_of_nat ??) (bitvector_of_nat ??) false)
714              #result #flags normalize nodelta
715              cases (vsplit bool 9 7 result) #upper #lower normalize nodelta
716              cases (get_index' bool 2 0 flags) normalize nodelta
717              #H
718              [1,2,5,6: >(proj2 ?? (proj1 ?? H)) >(proj1 ?? (proj1 ?? H)) normalize nodelta @refl
719              |*: >(proj1 ?? H) >(proj2 ?? H) normalize nodelta @refl
720              ]]]
721          |1: normalize nodelta
722              cut (∀A,B,ab.fst A B ab = (let 〈a,b〉 ≝ ab in a))
723              [#A #B * / by refl/] #fst_foo
724              cut (∀x.
725                    instruction_has_label x (\snd  (nth i labelled_instruction program 〈None (identifier ASMTag),Comment []〉)) →
726               lookup_def ?? (create_label_map program) x 0 ≤ (|program|))
727              [#x #Heq cases (create_label_map program) #clm #Hclm
728               @le_S_to_le @(Hclm x ?)
729               @(proj2 ?? Hprogram x (bitvector_of_nat ? i) ? (\snd (nth i ? program 〈None ?, Comment []〉)) ??)
730               [ >nat_of_bitvector_bitvector_of_nat_inverse
731                 [2: @(transitive_lt … (proj1 ?? Hprogram)) @le_S] @Hi
732               | whd in match fetch_pseudo_instruction; normalize nodelta
733                 >nth_safe_nth
734                 [ >nat_of_bitvector_bitvector_of_nat_inverse
735                   [ @pair_elim // ]
736                   @(transitive_lt … (proj1 ?? Hprogram)) @le_S @Hi ]
737                 | assumption ]] #lookup_in_program
738              -H #pi cases pi
739              try (#y #x #Heq #H) try (#x #Heq #H) try (#Heq #H) try % lapply H -H
740              normalize nodelta >(lookup_opt_lookup_hit … EQ 〈0,short_jump〉)
741              #Hj lapply (Hj x (refl ? x)) -Hj
742              whd in match expand_relative_jump; normalize nodelta
743              whd in match expand_relative_jump_internal; normalize nodelta
744              whd in match expand_relative_jump_unsafe; normalize nodelta
745              whd in match expand_relative_jump_internal_unsafe;
746              normalize nodelta >(add_bitvector_of_nat_plus ? i 1)
747              <(plus_n_Sm i 0) <plus_n_O <plus_n_O cases x2 normalize nodelta
748              [1,4,7,10,13,16,19,22,25:
749                >fst_foo @pair_elim #sj_possible #disp #H #H2
750                @(pair_replace ?????????? (eq_to_jmeq … H))
751                [1,3,5,7,9,11,13,15,17: >(le_to_leb_true … (lookup_in_program …))
752                  try % >Heq % ]
753                >(proj1 ?? H2) try (@refl) normalize nodelta
754                [1,2,3,5: @(subaddressing_mode_elim … y) #w %
755                | cases y * #sth #sth2 @(subaddressing_mode_elim … sth)
756                  @(subaddressing_mode_elim … sth2) #x [3,4: #x2] %
757                ]
758              |2,5,8,11,14,17,20,23,26: ** #_ #_ #abs cases abs
759              ]
760              * #H #_ >fst_foo in H; @pair_elim #sj_possible #disp #H
761              @(pair_replace ?????????? (eq_to_jmeq … H))
762                [1,3,5,7,9,11,13,15,17: >(le_to_leb_true … (lookup_in_program …))
763                  try % >Heq % ]
764                #H2 >H2 try (@refl) normalize nodelta
765                [1,2,3,5: @(subaddressing_mode_elim … y) #w %
766                | cases y * #sth #sth2 @(subaddressing_mode_elim … sth2) #w
767                  [1,2: %] whd in match (map ????); whd in match (flatten ??);
768                  whd in match (map ????) in ⊢ (???%); whd in match (flatten ??) in ⊢ (???%);
769                  >length_append >length_append %]]]]]
770qed.
771
772lemma instruction_size_irrelevant: ∀i.
773  ¬is_jump i → ∀j1,j2.instruction_size_jmplen j1 i = instruction_size_jmplen j2 i.
774 #i cases i
775 [2,3,6: #x [3: #y] #Hj #j1 #j2 %
776 |4,5: #x #Hi cases Hi
777 |1: #pi cases pi try (#x #y #Hj #j1 #j2) try (#y #Hj #j1 #j2) try (#Hj #j1 #j2)
778     try % cases Hj ]
779qed.
Note: See TracBrowser for help on using the repository browser.