source: src/ASM/Policy.ma @ 1973

Last change on this file since 1973 was 1973, checked in by boender, 6 years ago
  • removed superfluous match
  • displaced 'cases daemon'
File size: 66.6 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
8include alias "basics/lists/list.ma".
9include alias "arithmetics/nat.ma".
10include alias "basics/logic.ma".
11
12(* Internal types *)
13
14(* ppc_pc_map: program length × (pseudo program counter ↦ 〈pc, jump_length〉) *)
15definition ppc_pc_map ≝ ℕ × (BitVectorTrie (ℕ × jump_length) 16).
16
17(* The different properties that we want/need to prove at some point *)
18(* Anything that's not in the program doesn't end up in the policy *)
19definition out_of_program_none: list labelled_instruction → ppc_pc_map → Prop ≝
20  λprefix.λsigma.
21  ∀i:ℕ.i ≥ |prefix| → i < 2^16 → bvt_lookup_opt … (bitvector_of_nat 16 i) (\snd sigma) = None ?.
22
23(* If instruction i is a jump, then there will be something in the policy at
24 * position i *)
25definition is_jump' ≝
26  λx:preinstruction Identifier.
27  match x with
28  [ JC _ ⇒ True
29  | JNC _ ⇒ True
30  | JZ _ ⇒ True
31  | JNZ _ ⇒ True
32  | JB _ _ ⇒ True
33  | JNB _ _ ⇒ True
34  | JBC _ _ ⇒ True
35  | CJNE _ _ ⇒ True
36  | DJNZ _ _ ⇒ True
37  | _ ⇒ False
38  ].
39 
40definition is_jump ≝
41  λinstr:pseudo_instruction.
42  match instr with
43  [ Instruction i   ⇒ is_jump' i
44  | Call _ ⇒ True
45  | Jmp _ ⇒ True
46  | _ ⇒ False
47  ].
48
49definition is_jump_to ≝
50  λx:pseudo_instruction.λd:Identifier.
51  match x with
52  [ Instruction i ⇒ match i with
53    [ JC j ⇒ d = j
54    | JNC j ⇒ d = j
55    | JZ j ⇒ d = j
56    | JNZ j ⇒ d = j
57    | JB _ j ⇒ d = j
58    | JNB _ j ⇒ d = j
59    | CJNE _ j ⇒ d = j
60    | DJNZ _ j ⇒ d = j
61    | _ ⇒ False
62    ]
63  | Call c ⇒ d = c
64  | Jmp j ⇒ d = j
65  | _ ⇒ False
66  ].
67 
68definition jump_not_in_policy: list labelled_instruction → ppc_pc_map → Prop ≝
69  λprefix.λsigma.
70  ∀i:ℕ.i < |prefix| →
71  ¬is_jump (\snd (nth i ? prefix 〈None ?, Comment []〉)) →
72  \snd (bvt_lookup … (bitvector_of_nat 16 i) (\snd sigma) 〈0,short_jump〉) = short_jump.
73
74(* if the instruction 〈p,a〉 is a jump to label l, then label l is at address a *)
75(* definition labels_okay: label_map → ppc_pc_map → Prop ≝
76  λlabels.λsigma.
77  bvt_forall ?? (\snd sigma) (λn.λx.
78   let 〈pc,addr_nat〉 ≝ x in
79   ∃id:Identifier.lookup_def … labels id 0 = addr_nat
80  ). *)
81 
82(* Between two policies, jumps cannot decrease *)
83definition jmpeqb: jump_length → jump_length → bool ≝
84  λj1.λj2.
85  match j1 with
86  [ short_jump ⇒ match j2 with [ short_jump ⇒ true | _ ⇒ false ]
87  | medium_jump ⇒ match j2 with [ medium_jump ⇒ true | _ ⇒ false ]
88  | long_jump ⇒ match j2 with [ long_jump ⇒ true | _ ⇒ false ]
89  ].
90
91lemma jmpeqb_to_eq: ∀j1,j2.jmpeqb j1 j2 → j1 = j2.
92 #j1 #j2 cases j1 cases j2
93 [1,5,9: / by /]
94 #H cases H
95qed.
96
97definition jmple: jump_length → jump_length → Prop ≝
98  λj1.λj2.
99  match j1 with
100  [ short_jump  ⇒
101    match j2 with
102    [ short_jump ⇒ False
103    | _          ⇒ True
104    ]
105  | medium_jump ⇒
106    match j2 with
107    [ long_jump ⇒ True
108    | _         ⇒ False
109    ]
110  | long_jump   ⇒ False
111  ].
112
113definition jmpleq: jump_length → jump_length → Prop ≝
114  λj1.λj2.jmple j1 j2 ∨ j1 = j2.
115 
116definition policy_increase: list labelled_instruction → ppc_pc_map →
117  ppc_pc_map → Prop ≝
118 λprogram.λop.λp.
119 ∀i.i < |program| →
120   let 〈opc,oj〉 ≝ bvt_lookup … (bitvector_of_nat 16 i) (\snd op) 〈0,short_jump〉 in
121   let 〈pc,j〉 ≝ bvt_lookup … (bitvector_of_nat 16 i) (\snd p) 〈0,short_jump〉 in
122     (*opc ≤ pc ∧*) jmpleq oj j.
123
124(* Policy safety *)
125(*definition policy_safe: list labelled_instruction → label_map → ppc_pc_map → Prop ≝
126 λprogram.λlabels.λsigma.
127 ∀i.i < |program| →
128 let 〈pc,j〉 ≝ bvt_lookup … (bitvector_of_nat 16 i) (\snd sigma) 〈0,false〉 in
129 let 〈label,instr〉 ≝ nth i ? program 〈None ?, Comment [ ]〉 in
130 ∀dest.is_jump_to instr dest →
131   let paddr ≝ lookup_def … labels dest 0 in
132   let addr ≝ \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd sigma) 〈0,false〉) in
133   match j with
134   [ None ⇒ True
135   | Some j ⇒ match j with
136     [ short_jump  ⇒
137        if leb pc addr
138        then le (addr - pc) 126
139        else le (pc - addr) 129
140     | medium_jump ⇒   
141        let a ≝ bitvector_of_nat 16 addr in
142        let p ≝ bitvector_of_nat 16 pc in
143        let 〈fst_5_addr, rest_addr〉 ≝ split bool 5 11 a in
144        let 〈fst_5_pc, rest_pc〉 ≝ split bool 5 11 p in
145        eq_bv 5 fst_5_addr fst_5_pc = true
146     | long_jump   ⇒ True
147     ]
148   ].*)
149
150(* this is the instruction size as determined by the distance from origin to destination *)
151(*definition instruction_size_sigma: label_map → ppc_pc_map → Word → pseudo_instruction → ℕ ≝
152 λlabels.λsigma.λpc.λi.
153 \fst (assembly_1_pseudoinstruction
154   (λid.bitvector_of_nat 16 (lookup_def … labels id 0))
155   (λi.bitvector_of_nat 16 (\fst (bvt_lookup ?? i (\snd sigma) 〈0,false〉))) pc
156   (λx.zero 16) i).*)
157 
158(* this is the instruction size as determined by the jump length given *)
159definition expand_relative_jump_internal_unsafe:
160  jump_length → ([[relative]] → preinstruction [[relative]]) → list instruction ≝
161  λjmp_len:jump_length.λi.
162  match jmp_len with
163  [ short_jump ⇒ [ RealInstruction (i (RELATIVE (zero 8))) ]
164  | medium_jump ⇒ [ ] (* this should not happen *)
165  | long_jump ⇒
166    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
167      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
168      LJMP (ADDR16 (zero 16))
169    ]
170  ].
171 @I
172qed.
173
174definition expand_relative_jump_unsafe:
175  jump_length → preinstruction Identifier → list instruction ≝
176  λjmp_len:jump_length.λi.
177  match i with
178  [ JC jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JC ?)
179  | JNC jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JNC ?)
180  | JB baddr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JB ? baddr)
181  | JZ jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JZ ?)
182  | JNZ jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JNZ ?)
183  | JBC baddr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JBC ? baddr)
184  | JNB baddr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (JNB ? baddr)
185  | CJNE addr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (CJNE ? addr)
186  | DJNZ addr jmp ⇒ expand_relative_jump_internal_unsafe jmp_len (DJNZ ? addr)
187  | ADD arg1 arg2 ⇒ [ ADD ? arg1 arg2 ]
188  | ADDC arg1 arg2 ⇒ [ ADDC ? arg1 arg2 ]
189  | SUBB arg1 arg2 ⇒ [ SUBB ? arg1 arg2 ]
190  | INC arg ⇒ [ INC ? arg ]
191  | DEC arg ⇒ [ DEC ? arg ]
192  | MUL arg1 arg2 ⇒ [ MUL ? arg1 arg2 ]
193  | DIV arg1 arg2 ⇒ [ DIV ? arg1 arg2 ]
194  | DA arg ⇒ [ DA ? arg ]
195  | ANL arg ⇒ [ ANL ? arg ]
196  | ORL arg ⇒ [ ORL ? arg ]
197  | XRL arg ⇒ [ XRL ? arg ]
198  | CLR arg ⇒ [ CLR ? arg ]
199  | CPL arg ⇒ [ CPL ? arg ]
200  | RL arg ⇒ [ RL ? arg ]
201  | RR arg ⇒ [ RR ? arg ]
202  | RLC arg ⇒ [ RLC ? arg ]
203  | RRC arg ⇒ [ RRC ? arg ]
204  | SWAP arg ⇒ [ SWAP ? arg ]
205  | MOV arg ⇒ [ MOV ? arg ]
206  | MOVX arg ⇒ [ MOVX ? arg ]
207  | SETB arg ⇒ [ SETB ? arg ]
208  | PUSH arg ⇒ [ PUSH ? arg ]
209  | POP arg ⇒ [ POP ? arg ]
210  | XCH arg1 arg2 ⇒ [ XCH ? arg1 arg2 ]
211  | XCHD arg1 arg2 ⇒ [ XCHD ? arg1 arg2 ]
212  | RET ⇒ [ RET ? ]
213  | RETI ⇒ [ RETI ? ]
214  | NOP ⇒ [ RealInstruction (NOP ?) ]
215  ].
216
217definition instruction_size_jmplen:
218 jump_length → pseudo_instruction → ℕ ≝
219  λjmp_len.
220  λi.
221  let pseudos ≝ match i with
222  [ Cost cost ⇒ [ ]
223  | Comment comment ⇒ [ ]
224  | Call call ⇒
225    match jmp_len with
226    [ short_jump ⇒ [ ] (* this should not happen *)
227    | medium_jump ⇒ [ ACALL (ADDR11 (zero 11)) ]
228    | long_jump ⇒ [ LCALL (ADDR16 (zero 16)) ]
229    ]
230  | Mov d trgt ⇒
231     [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, DATA16 (zero 16)〉))))]
232  | Instruction instr ⇒ expand_relative_jump_unsafe jmp_len instr
233  | Jmp jmp ⇒
234    match jmp_len with
235    [ short_jump ⇒ [ SJMP (RELATIVE (zero 8)) ]
236    | medium_jump ⇒ [ AJMP (ADDR11 (zero 11)) ]
237    | long_jump ⇒ [ LJMP (ADDR16 (zero 16)) ]
238    ]
239  ] in
240  let mapped ≝ map ? ? assembly1 pseudos in
241  let flattened ≝ flatten ? mapped in
242  let pc_len ≝ length ? flattened in
243    pc_len.
244 @I.
245qed.
246
247(* new safety condition: policy corresponds to program and resulting program is compact *)
248definition policy_compact: list labelled_instruction → label_map → ppc_pc_map → Prop ≝
249 λprogram.λlabels.λsigma.
250 ∀n:ℕ.S n < |program| →
251  match bvt_lookup_opt … (bitvector_of_nat ? n) (\snd sigma) with
252  [ None ⇒ False
253  | Some x ⇒ let 〈pc,j〉 ≝ x in
254    match bvt_lookup_opt … (bitvector_of_nat ? (S n)) (\snd sigma) with
255    [ None ⇒ False
256    | Some x1 ⇒ let 〈pc1,j1〉 ≝ x1 in
257       pc1 = instruction_size (λid.bitvector_of_nat ? (lookup_def ?? labels id 0))
258         (λppc.bitvector_of_nat ? (\fst (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
259         (λppc.jmpeqb long_jump (\snd (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
260         (bitvector_of_nat ? pc) (\snd (nth n ? program 〈None ?, Comment []〉))
261    ]
262  ].
263 
264(* Definitions and theorems for the jump_length type (itself defined in Assembly) *)
265definition max_length: jump_length → jump_length → jump_length ≝
266  λj1.λj2.
267  match j1 with
268  [ long_jump   ⇒ long_jump
269  | medium_jump ⇒
270    match j2 with
271    [ medium_jump ⇒ medium_jump
272    | _           ⇒ long_jump
273    ]
274  | short_jump  ⇒
275    match j2 with
276    [ short_jump ⇒ short_jump
277    | _          ⇒ long_jump
278    ]
279  ].
280
281lemma dec_jmple: ∀x,y:jump_length.Sum (jmple x y) (¬(jmple x y)).
282 #x #y cases x cases y /3 by inl, inr, nmk, I/
283qed.
284 
285lemma jmpleq_max_length: ∀ol,nl.
286  jmpleq ol (max_length ol nl).
287 #ol #nl cases ol cases nl
288 /2 by or_introl, or_intror, I/
289qed.
290
291lemma dec_eq_jump_length: ∀a,b:jump_length.Sum (a = b) (a ≠ b).
292  #a #b cases a cases b /2/
293  %2 @nmk #H destruct (H)
294qed.
295 
296(* definition policy_isize_sum ≝
297  λprefix:list labelled_instruction.λlabels:label_map.λsigma:ppc_pc_map.
298  (\fst sigma) = foldl_strong (option Identifier × pseudo_instruction)
299  (λacc.ℕ)
300  prefix
301  (λhd.λx.λtl.λp.λacc.
302    acc + (instruction_size (λid.bitvector_of_nat ? (lookup_def ?? labels id 0))
303    (λppc.bitvector_of_nat ? (\fst (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
304    (λppc.jmpeqb long_jump (\snd (bvt_lookup ?? ppc (\snd sigma) 〈0,short_jump〉)))
305    (bitvector_of_nat 16 (\fst sigma)) (\snd x)))
306  0. *)
307 
308(* The function that creates the label-to-address map *)
309definition create_label_map: ∀program:list labelled_instruction.
310  (Σlabels:label_map.
311    ∀l.occurs_exactly_once ?? l program →
312    bitvector_of_nat ? (lookup_def ?? labels l 0) =
313     address_of_word_labels_code_mem program l
314  ) ≝
315 λprogram.
316   \fst (create_label_cost_map program).
317 #l #Hl lapply (pi2 ?? (create_label_cost_map0 program)) @pair_elim
318 #labels #costs #EQ normalize nodelta #H whd in match create_label_cost_map;
319 normalize nodelta >EQ @(H l Hl)
320qed.
321
322definition select_reljump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ →  ℕ →
323  Identifier → jump_length ≝
324  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
325  let paddr ≝ lookup_def … labels lbl 0 in
326  if leb ppc paddr (* forward jump *)
327  then
328    let addr ≝ \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)
329                    + added in
330    if leb (addr - \fst inc_sigma) 126
331    then short_jump
332    else long_jump
333  else
334    let addr ≝ \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉) in
335    if leb (\fst inc_sigma - addr) 129
336    then short_jump
337    else long_jump.
338
339definition select_call_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
340  Identifier → jump_length ≝
341  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
342  let paddr ≝ lookup_def ? ? labels lbl 0 in
343  let addr ≝
344    if leb ppc paddr (* forward jump *)
345    then \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd old_sigma) 〈0,short_jump〉)
346            + added
347    else \fst (bvt_lookup … (bitvector_of_nat ? paddr) (\snd inc_sigma) 〈0,short_jump〉) in
348  let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 (bitvector_of_nat ? addr) in
349  let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 (bitvector_of_nat ? (\fst inc_sigma)) in
350  if eq_bv ? fst_5_addr fst_5_pc
351  then medium_jump
352  else long_jump.
353 
354definition select_jump_length: label_map → ppc_pc_map → ppc_pc_map → ℕ → ℕ →
355  Identifier → jump_length ≝
356  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λlbl.
357  let paddr ≝ lookup_def … labels lbl 0 in
358  if leb ppc paddr (* forward jump *)
359  then
360    let addr ≝ \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd old_sigma) 〈0,short_jump〉)
361              + added in
362    if leb (addr - \fst inc_sigma) 126
363    then short_jump
364    else select_call_length labels old_sigma inc_sigma added ppc lbl
365  else
366    let addr ≝ \fst (bvt_lookup … (bitvector_of_nat 16 paddr) (\snd inc_sigma) 〈0,short_jump〉) in
367    if leb (\fst inc_sigma - addr) 129
368    then short_jump
369    else select_call_length labels old_sigma inc_sigma added ppc lbl.
370 
371definition jump_expansion_step_instruction: label_map → ppc_pc_map → ppc_pc_map →
372  ℕ → ℕ → preinstruction Identifier → option jump_length ≝
373  λlabels.λold_sigma.λinc_sigma.λadded.λppc.λi.
374  match i with
375  [ JC j     ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
376  | JNC j    ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
377  | JZ j     ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
378  | JNZ j    ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
379  | JB _ j   ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
380  | JBC _ j  ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
381  | JNB _ j  ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
382  | CJNE _ j ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
383  | DJNZ _ j ⇒ Some ? (select_reljump_length labels old_sigma inc_sigma added ppc j)
384  | _        ⇒ None ?
385  ].
386
387lemma dec_is_jump: ∀x.Sum (is_jump x) (¬is_jump x).
388#i cases i
389[#id cases id
390 [1,2,3,6,7,33,34:
391  #x #y %2 whd in match (is_jump ?); /2 by nmk/
392 |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32:
393  #x %2 whd in match (is_jump ?); /2 by nmk/
394 |35,36,37: %2 whd in match (is_jump ?); /2 by nmk/
395 |9,10,14,15: #x %1 / by I/
396 |11,12,13,16,17: #x #y %1 / by I/
397 ]
398|2,3: #x %2 /2 by nmk/
399|4,5: #x %1 / by I/
400|6: #x #y %2 /2 by nmk/
401]
402qed.
403
404lemma geb_to_leb: ∀a,b:ℕ.geb a b = leb b a.
405  #a #b / by refl/
406qed.
407
408(* The first step of the jump expansion: everything to short. *)
409definition jump_expansion_start:
410  ∀program:(Σl:list labelled_instruction.|l| < 2^16).
411  ∀labels:label_map.
412  Σpolicy:option ppc_pc_map.
413    match policy with
414    [ None ⇒ True
415    | Some p ⇒
416       And (And (And (And (out_of_program_none (pi1 ?? program) p)
417       (jump_not_in_policy (pi1 ?? program) p))
418       (policy_compact program labels p))
419       (∀i.i < |program| →
420         \snd (bvt_lookup … (bitvector_of_nat ? i) (\snd p) 〈0,short_jump〉) = short_jump))
421       (\fst p < 2^16)
422    ] ≝
423  λprogram.λlabels.
424  let final_policy ≝ foldl_strong (option Identifier × pseudo_instruction)
425  (λprefix.Σpolicy:ppc_pc_map.
426    And (And (And (out_of_program_none prefix policy)
427    (jump_not_in_policy prefix policy))
428    (policy_compact prefix labels policy))
429    (∀i.i < |prefix| →
430      \snd (bvt_lookup … (bitvector_of_nat ? i) (\snd policy) 〈0,short_jump〉) = short_jump))
431  program
432  (λprefix.λx.λtl.λprf.λp.
433   let 〈pc,sigma〉 ≝ p in
434   let 〈label,instr〉 ≝ x in
435   let isize ≝ instruction_size_jmplen short_jump instr in
436   〈pc + isize, bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈pc,short_jump〉 sigma〉
437  ) 〈0, Stub ? ?〉 in
438  if geb (\fst final_policy) 2^16 then
439    None ?
440  else
441    Some ? (pi1 ?? final_policy).
442[ / by I/
443| lapply p -p generalize in match (foldl_strong ?????); * #p #Hp #hg
444  @conj [ @Hp | @not_le_to_lt @leb_false_to_not_le <geb_to_leb @hg ]
445| @conj [ @conj [ @conj
446  [ (* out_of_program_none *)
447    #i >append_length <commutative_plus #Hi normalize in Hi; #Hi2
448    cases (le_to_or_lt_eq … Hi) -Hi #Hi
449    cases p -p #p cases p -p #pc #p #Hp cases x -x #l #pi
450    [ >lookup_opt_insert_miss
451      [ @(proj1 ?? (proj1 ?? (proj1 ?? Hp)) i ? Hi2) @le_S_to_le @le_S_to_le @Hi
452      | @bitvector_of_nat_abs
453        [ @Hi2
454        | @(transitive_lt … Hi2) @le_S_to_le @Hi
455        | @sym_neq @lt_to_not_eq @le_S_to_le @Hi
456        ]
457      ]
458    | >lookup_opt_insert_miss
459      [ <Hi @(proj1 ?? (proj1 ?? (proj1 ?? Hp)) (S (|prefix|)) (le_S ?? (le_n (|prefix|))) ?)
460        >Hi @Hi2
461      | @bitvector_of_nat_abs
462        [ @Hi2
463        | @(transitive_lt … Hi2) <Hi @le_n
464        | @sym_neq @lt_to_not_eq <Hi @le_n
465        ]
466      ]
467    ]
468  | (* jump_not_in_policy *) #i >append_length <commutative_plus
469    #Hi normalize in Hi; cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
470    [ cases p -p #p cases p -p #pc #sigma #Hp cases x #l #ins >lookup_insert_miss
471      [ >(nth_append_first ? i prefix ?? Hi) @((proj2 ?? (proj1 ?? (proj1 ?? Hp))) i Hi)
472      | @bitvector_of_nat_abs
473        [ @(transitive_lt … (pi2 ?? program)) >prf >append_length >commutative_plus
474          @le_plus_a @Hi
475        | @(transitive_lt … (pi2 ?? program)) >prf >append_length <plus_n_Sm @le_S_S
476          @le_plus_n_r
477        | @lt_to_not_eq @Hi
478        ]
479      ]
480    | >Hi >nth_append_second [2: @le_n] <minus_n_n whd in match (nth ????);
481      cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #ins
482      >lookup_insert_hit #_ / by /
483    ]
484  ]
485  | (* policy_compact *) cases daemon
486  ]       
487  | (* lookup = short_jump *) #i >append_length <commutative_plus #Hi normalize in Hi;
488    cases p -p #p cases p -p #pc #sigma #Hp cases x #lbl #instr normalize nodelta
489    cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
490    [ >lookup_insert_miss
491      [ @((proj2 ?? Hp) i Hi)
492      | @bitvector_of_nat_abs
493        [ @(transitive_lt … (pi2 ?? program)) >prf >append_length >commutative_plus
494          @le_plus_a @Hi
495        | @(transitive_lt … (pi2 ?? program)) >prf >append_length <plus_n_Sm @le_S_S
496          @le_plus_n_r
497        | @lt_to_not_eq @Hi
498        ]
499      ]
500    | >Hi >lookup_insert_hit @refl
501    ]
502  ]
503| @conj [ @conj [ @conj
504  [ #i #_ #Hi2 / by refl/
505  ]]]
506  #i #H @⊥ @(absurd … H) @not_le_Sn_O
507]
508qed.
509
510definition policy_equal ≝
511  λprogram:list labelled_instruction.λp1,p2:ppc_pc_map.
512  (* \fst p1 = \fst p2 ∧ *)
513  (∀n:ℕ.n < |program| →
514    let pc1 ≝ bvt_lookup … (bitvector_of_nat 16 n) (\snd p1) 〈0,short_jump〉 in
515    let pc2 ≝ bvt_lookup … (bitvector_of_nat 16 n) (\snd p2) 〈0,short_jump〉 in
516    \snd pc1 = \snd pc2).
517   
518definition nec_plus_ultra ≝
519  λprogram:list labelled_instruction.λp:ppc_pc_map.
520  ¬(∀i.i < |program| → is_jump (\snd (nth i ? program 〈None ?, Comment []〉)) →
521  \snd (bvt_lookup … (bitvector_of_nat 16 i) (\snd p) 〈0,short_jump〉) = long_jump).
522 
523(*include alias "common/Identifiers.ma".*)
524include alias "ASM/BitVector.ma".
525include alias "basics/lists/list.ma".
526include alias "arithmetics/nat.ma".
527include alias "basics/logic.ma".
528
529lemma blerpque: ∀a,b,i.
530  is_jump i → instruction_size_jmplen (max_length a b) i = instruction_size_jmplen a i →
531  (max_length a b) = a.
532 #a #b #i cases i
533 [1: #pi cases pi
534   [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
535     [1,2,3,6,7,24,25: #x #y
536     |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x]
537     #H cases H
538   |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y]
539     #_ cases a cases b
540     [1,5,7,8,9: #_ / by refl/
541     |10,14,16,17,18: #_ / by refl/
542     |19,23,25,26,27: #_ / by refl/
543     |28,32,34,35,36: #_ / by refl/
544     |37,41,43,44,45: #_ / by refl/
545     |46,50,52,53,54: #_ / by refl/
546     |55,59,61,62,63: #_ / by refl/
547     |64,68,70,71,72: #_ / by refl/
548     |73,77,79,80,81: #_ / by refl/
549     |2,3,4,6: cases x #a cases a
550       [1,2,3,4,8,9,16,17,18,19: #b #Hb cases Hb
551       |20,21,22,23,27,28,35,36,37,38: #b #Hb cases Hb
552       |39,40,41,42,46,47,54,55,56,57: #b #Hb cases Hb
553       |58,59,60,61,65,66,73,74,75,76: #b #Hb cases Hb
554       |5,6,7,10,11,12,13,14: #Hb cases Hb
555       |24,25,26,29,30,31,32,33: #Hb cases Hb
556       |43,44,45,48,49,50,51,52: #Hb cases Hb
557       |62,63,64,67,68,69,70,71: #Hb cases Hb
558       |15,34,53,72: #b #Hb #H normalize in H; destruct (H)
559       ]
560     |11,12,13,15: cases x #a cases a
561       [1,2,3,4,8,9,16,17,18,19: #b #Hb cases Hb
562       |20,21,22,23,27,28,35,36,37,38: #b #Hb cases Hb
563       |39,40,41,42,46,47,54,55,56,57: #b #Hb cases Hb
564       |58,59,60,61,65,66,73,74,75,76: #b #Hb cases Hb
565       |5,6,7,10,11,12,13,14: #Hb cases Hb
566       |24,25,26,29,30,31,32,33: #Hb cases Hb
567       |43,44,45,48,49,50,51,52: #Hb cases Hb
568       |62,63,64,67,68,69,70,71: #Hb cases Hb
569       |15,34,53,72: #b #Hb #H normalize in H; destruct (H)
570       ]
571     |20,21,22,24: cases x #a cases a
572       [1,2,3,4,8,9,16,17,18,19: #b #Hb cases Hb
573       |20,21,22,23,27,28,35,36,37,38: #b #Hb cases Hb
574       |39,40,41,42,46,47,54,55,56,57: #b #Hb cases Hb
575       |58,59,60,61,65,66,73,74,75,76: #b #Hb cases Hb
576       |5,6,7,10,11,12,13,14: #Hb cases Hb
577       |24,25,26,29,30,31,32,33: #Hb cases Hb
578       |43,44,45,48,49,50,51,52: #Hb cases Hb
579       |62,63,64,67,68,69,70,71: #Hb cases Hb
580       |15,34,53,72: #b #Hb #H normalize in H; destruct (H)
581       ]
582     |29,30,31,33: cases x #a cases a #a1 #a2
583       [1,3,5,7: cases a2 #b cases b
584         [2,3,4,9,15,16,17,18,19: #b #Hb cases Hb
585         |21,22,23,28,34,35,36,37,38: #b #Hb cases Hb
586         |40,41,42,47,53,54,55,56,57: #b #Hb cases Hb
587         |59,60,61,66,72,73,74,75,76: #b #Hb cases Hb
588         |5,6,7,10,11,12,13,14: #Hb cases Hb
589         |24,25,26,29,30,31,32,33: #Hb cases Hb
590         |43,44,45,48,49,50,51,52: #Hb cases Hb
591         |62,63,64,67,68,69,70,71: #Hb cases Hb
592         |1,8: #b #Hb #H normalize in H; destruct (H)
593         |20,27: #b #Hb #H normalize in H; destruct (H)
594         |39,46: #b #Hb #H normalize in H; destruct (H)
595         |58,65: #b #Hb #H normalize in H; destruct (H)
596         ]
597       |2,4,6,8: cases a1 #b cases b
598         [1,3,8,9,15,16,17,18,19: #b #Hb cases Hb
599         |20,22,27,28,34,35,36,37,38: #b #Hb cases Hb
600         |39,41,46,47,53,54,55,56,57: #b #Hb cases Hb
601         |58,60,65,66,72,73,74,75,76: #b #Hb cases Hb
602         |5,6,7,10,11,12,13,14: #Hb cases Hb
603         |24,25,26,29,30,31,32,33: #Hb cases Hb
604         |43,44,45,48,49,50,51,52: #Hb cases Hb
605         |62,63,64,67,68,69,70,71: #Hb cases Hb
606         |2,4: #b #Hb #H normalize in H; destruct (H)
607         |21,23: #b #Hb #H normalize in H; destruct (H)
608         |40,42: #b #Hb #H normalize in H; destruct (H)
609         |59,61: #b #Hb #H normalize in H; destruct (H)
610         ]
611       ]
612     |38,39,40,42: cases x #a cases a
613       [2,3,8,9,15,16,17,18,19: #b #Hb cases Hb
614       |21,22,27,28,34,35,36,37,38: #b #Hb cases Hb
615       |40,41,46,47,53,54,55,56,57: #b #Hb cases Hb
616       |59,60,65,66,72,73,74,75,76: #b #Hb cases Hb
617       |5,6,7,10,11,12,13,14: #Hb cases Hb
618       |24,25,26,29,30,31,32,33: #Hb cases Hb
619       |43,44,45,48,49,50,51,52: #Hb cases Hb
620       |62,63,64,67,68,69,70,71: #Hb cases Hb
621       |1,4: #b #Hb #H normalize in H; destruct (H)
622       |20,23: #b #Hb #H normalize in H; destruct (H)
623       |39,42: #b #Hb #H normalize in H; destruct (H)
624       |58,61: #b #Hb #H normalize in H; destruct (H)
625       ]
626     |47,48,49,51: cases x #a #H normalize in H; destruct (H)
627     |56,57,58,60: cases x #a #H normalize in H; destruct (H)
628     |65,66,67,69: cases x #a #H normalize in H; destruct (H)
629     |74,75,76,78: cases x #a #H normalize in H; destruct (H)
630     ]
631   ]
632  |2,3,6: #x [3: #y] #H cases H
633  |4,5: #id #_ cases a cases b
634    [2,3,4,6,11,12,13,15: normalize #H destruct (H)
635    |1,5,7,8,9,10,14,16,17,18: #H / by refl/
636    ]
637  ]
638qed.
639
640lemma etblorp: ∀a,b,i.is_jump i →
641  instruction_size_jmplen a i ≤ instruction_size_jmplen (max_length a b) i.
642 #a #b #i cases i
643 [2,3,6: #x [3: #y] #H cases H
644 |4,5: #id #_ cases a cases b / by le_n/
645 |1: #pi cases pi
646   [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
647     [1,2,3,6,7,24,25: #x #y
648     |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x]
649     #H cases H
650   |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y]
651     #_ cases a cases b
652     [2,3: cases x #ad cases ad
653       [15,34: #b #Hb / by le_n/
654       |1,2,3,4,8,9,16,17,18,19,20,21,22,23,27,28,35,36,37,38: #b] #Hb cases Hb
655     |1,4,5,6,7,8,9: / by le_n/
656     |11,12: cases x #ad cases ad
657       [15,34: #b #Hb / by le_n/
658       |1,2,3,4,8,9,16,17,18,19,20,21,22,23,27,28,35,36,37,38: #b] #Hb cases Hb
659     |10,13,14,15,16,17,18: / by le_n/
660     |20,21: cases x #ad cases ad
661       [15,34: #b #Hb / by le_n/
662       |1,2,3,4,8,9,16,17,18,19,20,21,22,23,27,28,35,36,37,38: #b] #Hb cases Hb
663     |19,22,23,24,25,26,27: / by le_n/
664     |29,30: cases x #ad cases ad #a1 #a2
665       [1,3: cases a2 #ad2 cases ad2
666         [1,8,20,27: #b #Hb / by le_n/
667         |2,3,4,9,15,16,17,18,19,21,22,23,28,34,35,36,37,38: #b] #Hb cases Hb
668       |2,4: cases a1 #ad1 cases ad1
669         [2,4,21,23: #b #Hb / by le_n/
670         |1,3,8,9,15,16,17,18,19,20,22,27,28,34,35,36,37,38: #b] #Hb cases Hb
671       ]
672     |28,31,32,33,34,35,36: / by le_n/
673     |38,39: cases x #ad cases ad
674       [1,4,20,23: #b #Hb / by le_n/
675       |2,3,8,9,15,16,17,18,19,21,22,27,28,34,35,36,37,38: #b] #Hb cases Hb
676     |37,40,41,42,43,44,45: / by le_n/
677     |46,47,48,49,50,51,52,53,54: / by le_n/
678     |55,56,57,58,59,60,61,62,63: / by le_n/
679     |64,65,66,67,68,69,70,71,72: / by le_n/
680     |73,74,75,76,77,78,79,80,81: / by le_n/
681     ]
682   ]
683 ]
684qed.
685
686lemma minus_zero_to_le: ∀n,m:ℕ.n - m = 0 → n ≤ m.
687 #n
688 elim n
689 [ #m #_ @le_O_n
690 | #n' #Hind #m cases m
691   [ #H -n whd in match (minus ??) in H; >H @le_n
692   | #m' -m #H whd in match (minus ??) in H; @le_S_S @Hind @H
693   ]
694 ]
695qed.
696
697lemma plus_zero_zero: ∀n,m:ℕ.n + m = 0 → m = 0.
698 #n #m #Hn @sym_eq @le_n_O_to_eq <Hn >commutative_plus @le_plus_n_r
699qed.
700
701(* One step in the search for a jump expansion fixpoint. *)
702definition jump_expansion_step: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
703  ∀labels:(Σlm:label_map.∀l.
704    occurs_exactly_once ?? l program →
705    bitvector_of_nat ? (lookup_def … lm l 0) =
706    address_of_word_labels_code_mem program l).
707  ∀old_policy:(Σpolicy:ppc_pc_map.
708    And (And (out_of_program_none program policy)
709    (jump_not_in_policy program policy))
710    (\fst policy < 2^16)).
711  (Σx:bool × (option ppc_pc_map).
712    let 〈no_ch,y〉 ≝ x in
713    match y with
714    [ None ⇒ nec_plus_ultra program old_policy
715    | Some p ⇒ And (And (And (And (And (out_of_program_none program p)
716       (jump_not_in_policy program p))
717       (policy_increase program old_policy p))
718       (policy_compact program labels p))
719       (no_ch = true → policy_equal program old_policy p))
720       (\fst p < 2^16)
721    ])
722    ≝
723  λprogram.λlabels.λold_sigma.
724  let 〈final_added, final_policy〉 ≝
725    foldl_strong (option Identifier × pseudo_instruction)
726    (λprefix.Σx:ℕ × ppc_pc_map.
727      let 〈added,policy〉 ≝ x in
728      And (And (And (And (out_of_program_none prefix policy)
729      (jump_not_in_policy prefix policy))
730      (policy_increase prefix old_sigma policy))
731      (policy_compact prefix labels policy))
732      (added = 0 → policy_equal prefix old_sigma policy))
733    program
734    (λprefix.λx.λtl.λprf.λacc.
735      let 〈inc_added, inc_pc_sigma〉 ≝ (pi1 ?? acc) in
736      let 〈label,instr〉 ≝ x in
737      (* Now, we must add the current ppc and its pc translation.
738       * Three possibilities:
739       *   - Instruction is not a jump; i.e. constant size whatever the sigma we use;
740       *   - Instruction is a backward jump; we can use the sigma we're constructing,
741       *     since it will already know the translation of its destination;
742       *   - Instruction is a forward jump; we must use the old sigma (the new sigma
743       *     does not know the translation yet), but compensate for the jumps we
744       *     have lengthened.
745       *)
746      let add_instr ≝ match instr with
747      [ Jmp  j        ⇒ Some ? (select_jump_length labels old_sigma inc_pc_sigma inc_added (|prefix|) j)
748      | Call c        ⇒ Some ? (select_call_length labels old_sigma inc_pc_sigma inc_added (|prefix|) c)
749      | Instruction i ⇒ jump_expansion_step_instruction labels old_sigma inc_pc_sigma inc_added (|prefix|) i
750      | _             ⇒ None ?
751      ] in
752      let 〈inc_pc, inc_sigma〉 ≝ inc_pc_sigma in
753      let 〈old_pc,old_length〉 ≝ bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉 in
754      let old_size ≝ instruction_size_jmplen old_length instr in
755      let 〈new_length, isize〉 ≝ match add_instr with
756      [ None    ⇒ 〈short_jump, instruction_size_jmplen short_jump instr〉
757      | Some pl ⇒ 〈max_length old_length pl, instruction_size_jmplen (max_length old_length pl) instr〉
758      ] in
759      let new_added ≝ match add_instr with
760      [ None   ⇒ inc_added
761      | Some x ⇒ plus inc_added (minus isize old_size)
762      ] in
763      〈new_added, 〈plus inc_pc isize, bvt_insert … (bitvector_of_nat ? (|prefix|)) 〈inc_pc, new_length〉 inc_sigma〉〉
764    ) 〈0, 〈0, Stub ??〉〉 in
765    if geb (\fst final_policy) 2^16 then
766      〈eqb final_added 0, None ?〉
767    else
768      〈eqb final_added 0, Some ? final_policy〉.
769[ normalize nodelta cases daemon (* XXX nec_plus_ultra *)
770| normalize nodelta lapply p generalize in match (foldl_strong ?????); * #x #H #H2
771  >H2 in H; normalize nodelta -H2 -x #H @conj
772  [ @conj
773    [ @(proj1 ?? H)
774    | #H2 @(proj2 ?? H) @eqb_true_to_eq @H2
775    ]
776  | @not_le_to_lt @leb_false_to_not_le <geb_to_leb @p1
777  ]
778| lapply (pi2 ?? acc) >p cases inc_pc_sigma #inc_pc #inc_sigma
779  lapply (refl ? (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉))
780  cases (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉) in ⊢ (???% → %);
781  #old_pc #old_length #Holdeq #Hpolicy @pair_elim #added #policy normalize nodelta
782  @pair_elim #new_length #isize normalize nodelta #Heq1 #Heq2
783  @conj [ @conj [ @conj [ @conj
784  [ (* out_of_program_none *) #i >append_length <commutative_plus #Hi normalize in Hi; #Hi2
785    cases instr in Heq2; normalize nodelta
786    #x [6: #y] #H <(proj2 ?? (pair_destruct ?????? H)) >lookup_opt_insert_miss
787    [1,3,5,7,9,11: @(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hpolicy))) i ? Hi2)
788      @le_S_to_le @Hi
789    |2,4,6,8,10,12: @bitvector_of_nat_abs
790      [1,4,7,10,13,16: @Hi2
791      |2,5,8,11,14,17: @(transitive_lt … Hi2) @Hi
792      |3,6,9,12,15,18: @sym_neq @lt_to_not_eq @Hi
793      ]
794    ]
795  | (* jump_not_in_policy *) #i >append_length <commutative_plus #Hi normalize in Hi;
796    cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
797    [ <(proj2 ?? (pair_destruct ?????? Heq2)) >lookup_insert_miss
798      [ >(nth_append_first ? i prefix ?? Hi)
799        @(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hpolicy))) i Hi)
800      | @bitvector_of_nat_abs
801        [ @(transitive_lt … (pi2 ?? program)) >prf >append_length >commutative_plus
802          @le_plus_a @Hi
803        | @(transitive_lt … (pi2 ?? program)) >prf >append_length <plus_n_Sm @le_S_S
804          @le_plus_n_r
805        | @lt_to_not_eq @Hi
806        ]
807      ]
808    | >Hi <(proj2 ?? (pair_destruct ?????? Heq2)) >lookup_insert_hit
809      >nth_append_second
810      [ <minus_n_n whd in match (nth ????); cases instr in Heq1;
811        [4,5: #x #_ #H cases H #H2 @⊥ @H2 / by I/
812        |2,3,6: #x [3: #y] #Heq1 <(proj1 ?? (pair_destruct ?????? Heq1)) #_ / by /
813        |1: #pi cases pi
814          [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
815            [1,2,3,6,7,24,25: #x #y
816            |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x] #Heq1
817              <(proj1 ?? (pair_destruct ?????? Heq1)) #_ / by /
818          |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y]
819            #_ #H @⊥ cases H #H2 @H2 / by I/
820          ]
821        ]
822      | @le_n
823      ]
824    ]
825  ]
826  | (* policy_increase *) #i >append_length >commutative_plus #Hi normalize in Hi;
827    cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi; #Hi
828    [ lapply (proj2 ?? (proj1 ?? (proj1 ?? Hpolicy)) i Hi)
829      <(proj2 ?? (pair_destruct ?????? Heq2))     
830      @pair_elim #opc #oj #EQ1 >lookup_insert_miss
831      [ @pair_elim #pc #j #EQ2 / by /
832      | @bitvector_of_nat_abs
833        [ @(transitive_lt … (pi2 ?? program)) >prf >append_length >commutative_plus @le_plus_a
834          @Hi
835        | @(transitive_lt … (pi2 ?? program)) >prf >append_length <plus_n_Sm @le_S_S @le_plus_n_r
836        | @lt_to_not_eq @Hi
837        ]
838      ]
839    | >Hi <(proj2 ?? (pair_destruct ?????? Heq2)) >lookup_insert_hit
840      cases (dec_is_jump instr)
841      [ cases instr in Heq1; normalize nodelta
842        [ #pi cases pi
843          [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
844            [1,2,3,6,7,24,25: #x #y
845            |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x] #_ #Hj cases Hj
846          |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y]
847            whd in match jump_expansion_step_instruction; normalize nodelta #Heq1
848            <(proj1 ?? (pair_destruct ?????? Heq1)) #_ >Holdeq normalize nodelta
849            @jmpleq_max_length
850          ]
851        |2,3,6: #x [3: #y] #_ #Hj cases Hj
852        |4,5: #x #Heq1 #_ <(proj1 ?? (pair_destruct ?????? Heq1)) >Holdeq normalize nodelta
853          @jmpleq_max_length
854        ]
855      | lapply Heq1 -Heq1; lapply (refl ? instr); cases instr in ⊢ (???% → %); normalize nodelta
856        [ #pi cases pi
857          [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
858            [1,2,3,6,7,24,25: #x #y
859            |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x]
860            whd in match jump_expansion_step_instruction; normalize nodelta #Heqi #Heq1
861            #Hj <(proj1 ?? (pair_destruct ?????? Heq1))
862            lapply (proj2 ?? (proj1 ?? (pi2 ?? old_sigma)) (|prefix|) ??)
863            [1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82:
864              >prf >nth_append_second
865              [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55:
866                <minus_n_n whd in match (nth ????); >p1 >Heqi @Hj
867              |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56:
868                @le_n
869              ]
870            |2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83:
871              >prf >append_length <plus_n_Sm @le_S_S @le_plus_n_r
872            |3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84:
873              cases (lookup ?? (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉)
874              #a #b #H >H normalize nodelta %2 @refl
875            ]
876          |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y]
877            #_ #_ #abs cases abs #ABS @⊥ @ABS / by I/
878          ]
879        |2,3,6: #x [3: #y] #Heqi #Heq1 #Hj <(proj1 ?? (pair_destruct ?????? Heq1))
880          lapply (proj2 ?? (proj1 ?? (pi2 ?? old_sigma)) (|prefix|) ??)
881          [1,4,7: >prf >nth_append_second
882            [1,3,5: <minus_n_n whd in match (nth ????); >p1 >Heqi @Hj
883            |2,4,6: @le_n
884            ]
885          |2,5,8: >prf >append_length <plus_n_Sm @le_S_S @le_plus_n_r
886          |3,6,9: cases (lookup ?? (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉)
887            #a #b #H >H normalize nodelta %2 @refl
888          ]
889        |4,5: #x #_ #_ #abs cases abs #ABS @⊥ @ABS / by I/
890        ]
891      ]
892    ]
893  ]
894  | (* policy_compact *) (*XXX*) cases daemon
895  ]
896  | (* added = 0 → policy_equal *) lapply (proj2 ?? Hpolicy)
897    lapply Heq2 -Heq2 lapply Heq1 -Heq1 lapply (refl ? instr)
898    cases instr in ⊢ (???% → % → % → %); normalize nodelta
899    [ #pi cases pi normalize nodelta
900      [1,2,3,4,5,6,7,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37:
901        [1,2,3,6,7,24,25: #x #y
902        |4,5,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23: #x]
903        #Hins #Heq1 #Heq2 #Hold <(proj1 ?? (pair_destruct ?????? Heq2)) #Hadded
904        #i >append_length >commutative_plus #Hi normalize in Hi;
905        cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
906        [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55:
907          <(proj2 ?? (pair_destruct ?????? Heq2)) >lookup_insert_miss
908          [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55:
909            @(Hold Hadded i Hi)
910          |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56:
911            @bitvector_of_nat_abs
912            [1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82:
913              @(transitive_lt … (pi2 ?? program)) >prf >append_length >commutative_plus
914              @le_plus_a @Hi
915            |2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83:
916              @(transitive_lt … (pi2 ?? program)) >prf >append_length <plus_n_Sm @le_S_S
917              @le_plus_n_r
918            |3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84:
919              @lt_to_not_eq @Hi
920            ]
921          ]
922        |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56:
923           <(proj2 ?? (pair_destruct ?????? Heq2)) >Hi >lookup_insert_hit
924           lapply (proj2 ?? (proj1 ?? (pi2 ?? old_sigma)) (|prefix|) ??)
925           [1,4,7,10,13,16,19,22,25,28,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82:
926             >prf >nth_append_second
927             [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55:
928               <minus_n_n whd in match (nth ????); >p1 >Hins @nmk #H @H
929             |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56:
930               @le_n
931             ]
932           |2,5,8,11,14,17,20,23,26,29,32,35,38,41,44,47,50,53,56,59,62,65,68,71,74,77,80,83:
933             >prf >append_length <plus_n_Sm @le_S_S @le_plus_n_r
934           |3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84:
935             cases (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉)
936             #a #b #H >H <(proj1 ?? (pair_destruct ?????? Heq1)) normalize nodelta @refl
937           ]
938         ]
939       |9,10,11,12,13,14,15,16,17: #x [3,4,5,8,9: #y] #Hins #Heq1 #Heq2 #Hold
940         <(proj1 ?? (pair_destruct ?????? Heq2)) <(proj2 ?? (pair_destruct ?????? Heq1))
941         #H #i >append_length >commutative_plus #Hi normalize in Hi;
942         cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
943         [1,3,5,7,9,11,13,15,17: <(proj2 ?? (pair_destruct ?????? Heq2))
944           >lookup_insert_miss
945           [1,3,5,7,9,11,13,15,17: @(Hold ? i Hi)
946             [1,2,3,4,5,6,7,8,9: @sym_eq @le_n_O_to_eq <H @le_plus_n_r]
947           ]
948           @bitvector_of_nat_abs
949           [1,4,7,10,13,16,19,22,25: @(transitive_lt … (pi2 ?? program)) >prf
950             >append_length >commutative_plus @le_plus_a @Hi
951           |2,5,8,11,14,17,20,23,26: @(transitive_lt … (pi2 ?? program)) >prf
952             >append_length <plus_n_Sm @le_S_S
953           |3,6,9,12,15,18,21,24,27: @lt_to_not_eq @Hi
954           ] @le_plus_n_r
955         |2,4,6,8,10,12,14,16,18: <(proj2 ?? (pair_destruct ?????? Heq2)) >Hi
956           >lookup_insert_hit <(proj1 ?? (pair_destruct ?????? Heq1))
957           >Holdeq normalize nodelta @sym_eq @blerpque
958           [3,6,9,12,15,18,21,24,27:
959             elim (le_to_or_lt_eq … (minus_zero_to_le … (plus_zero_zero … H)))
960             [1,3,5,7,9,11,13,15,17: #H @⊥ @(absurd ? H) @le_to_not_lt @etblorp
961             |2,4,6,8,10,12,14,16,18: #H @H
962             ]
963             / by I/
964           |2,5,8,11,14,17,20,23,26: / by I/
965           ]
966         ]
967       ]
968     |2,3,6: #x [3: #y] #Hins #Heq1 #Heq2 #Hold <(proj1 ?? (pair_destruct ?????? Heq2))
969       #Hadded #i >append_length >commutative_plus #Hi normalize in Hi;
970       cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
971       [1,3,5: <(proj2 ?? (pair_destruct ?????? Heq2)) >lookup_insert_miss
972         [1,3,5: @(Hold Hadded i Hi)
973         |2,4,6: @bitvector_of_nat_abs
974           [1,4,7: @(transitive_lt … (pi2 ?? program)) >prf >append_length >commutative_plus
975             @le_plus_a @Hi
976           |2,5,8: @(transitive_lt … (pi2 ?? program)) >prf >append_length <plus_n_Sm @le_S_S
977             @le_plus_n_r
978           |3,6,9: @lt_to_not_eq @Hi
979           ]
980         ]
981       |2,4,6: <(proj2 ?? (pair_destruct ?????? Heq2)) >Hi >lookup_insert_hit
982         lapply (proj2 ?? (proj1 ?? (pi2 ?? old_sigma)) (|prefix|) ??)
983         [1,4,7: >prf >nth_append_second
984           [1,3,5: <minus_n_n whd in match (nth ????); >p1 >Hins @nmk #H @H
985           |2,4,6: @le_n
986           ]
987         |2,5,8: >prf >append_length <plus_n_Sm @le_S_S @le_plus_n_r
988         |3,6,9: cases (bvt_lookup … (bitvector_of_nat ? (|prefix|)) (\snd old_sigma) 〈0,short_jump〉)
989           #a #b #H >H <(proj1 ?? (pair_destruct ?????? Heq1)) normalize nodelta @refl
990         ]
991       ]
992     |4,5: #x #Hins #Heq1 #Heq2 #Hold
993       <(proj1 ?? (pair_destruct ?????? Heq2)) <(proj2 ?? (pair_destruct ?????? Heq1))
994       #H #i >append_length >commutative_plus #Hi normalize in Hi;
995       cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
996       [1,3: <(proj2 ?? (pair_destruct ?????? Heq2)) >lookup_insert_miss
997         [1,3: @(Hold ? i Hi)
998           [1,2: @sym_eq @le_n_O_to_eq <H @le_plus_n_r]
999         ]
1000         @bitvector_of_nat_abs
1001         [1,4: @(transitive_lt … (pi2 ?? program)) >prf
1002           >append_length >commutative_plus @le_plus_a @Hi
1003         |2,5: @(transitive_lt … (pi2 ?? program)) >prf
1004           >append_length <plus_n_Sm @le_S_S
1005         |3,6: @lt_to_not_eq @Hi
1006         ] @le_plus_n_r
1007         |2,4: <(proj2 ?? (pair_destruct ?????? Heq2)) >Hi >lookup_insert_hit
1008           <(proj1 ?? (pair_destruct ?????? Heq1))>Holdeq normalize nodelta
1009           @sym_eq @blerpque
1010           [3,6: elim (le_to_or_lt_eq … (minus_zero_to_le … (plus_zero_zero … H)))
1011             [1,3: #H @⊥ @(absurd ? H) @le_to_not_lt @etblorp
1012             |2,4: #H @H
1013             ]
1014             / by I/
1015           |2,5: / by I/
1016           ]
1017         ]
1018       ]
1019     ]
1020| normalize nodelta @conj [ @conj [ @conj [ @conj
1021  [ #i #Hi / by refl/
1022  | / by refl/
1023  ]]]]
1024  [3: #_]
1025  #i #Hi @⊥ @(absurd ? Hi) @not_le_Sn_O
1026]
1027qed.
1028   
1029let rec jump_expansion_internal (program: Σl:list labelled_instruction.lt (length ? l) 2^16) (n: ℕ)
1030  on n:(Σx:bool × (option ppc_pc_map).
1031    let 〈c,pol〉 ≝ x in
1032    match pol with
1033    [ None ⇒ True
1034    | Some x ⇒
1035      And (And (And
1036        (out_of_program_none program x)
1037        (jump_not_in_policy program x))
1038        (policy_compact program (create_label_map program) x))
1039        (\fst x < 2^16)
1040    ]) ≝
1041  let labels ≝ create_label_map program in
1042  match n with
1043  [ O   ⇒ 〈true,pi1 ?? (jump_expansion_start program labels)〉
1044  | S m ⇒ let 〈ch,z〉 as p1 ≝ (pi1 ?? (jump_expansion_internal program m)) in
1045          match z return λx. z=x → Σa:bool × (option ppc_pc_map).? with
1046          [ None    ⇒ λp2.〈false,None ?〉
1047          | Some op ⇒ λp2.if ch
1048            then pi1 ?? (jump_expansion_step program labels «op,?»)
1049            else (jump_expansion_internal program m)
1050          ] (refl … z)
1051  ].
1052[ normalize nodelta cases (jump_expansion_start program (create_label_map program))
1053  #p cases p
1054  [ / by I/
1055  | #pm normalize nodelta #H @conj [ @(proj1 ?? (proj1 ?? H)) | @(proj2 ?? H) ]
1056  ]
1057| lapply (pi2 ?? (jump_expansion_internal program m)) <p1 >p2 normalize nodelta / by /
1058| lapply (pi2 ?? (jump_expansion_internal program m)) <p1 >p2 normalize nodelta
1059  #H @conj [ @(proj1 ?? (proj1 ?? H)) | @(proj2 ?? H) ]
1060| normalize nodelta cases (jump_expansion_step program labels «op,?»)
1061  #p cases p -p #p #r cases r normalize nodelta
1062  [ #H / by I/
1063  | #j #H @conj
1064    [ @conj
1065      [ @(proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? H))))
1066      | @(proj2 ?? (proj1 ?? (proj1 ?? H)))
1067      ]
1068    | @(proj2 ?? H)
1069    ]
1070  ]
1071]
1072qed.
1073
1074lemma pe_int_refl: ∀program.reflexive ? (policy_equal program).
1075#program whd #x whd #n #Hn
1076cases (bvt_lookup … (bitvector_of_nat 16 n) (\snd x) 〈0,short_jump〉)
1077#y #z normalize nodelta @refl
1078qed.
1079
1080lemma pe_int_sym: ∀program.symmetric ? (policy_equal program).
1081#program whd #x #y #Hxy whd #n #Hn
1082lapply (Hxy n Hn) cases (bvt_lookup … (bitvector_of_nat ? n) (\snd x) 〈0,short_jump〉)
1083#x1 #x2
1084cases (bvt_lookup … (bitvector_of_nat ? n) (\snd y) 〈0,short_jump〉)
1085#y1 #y2 normalize nodelta //
1086qed.
1087 
1088lemma pe_int_trans: ∀program.transitive ? (policy_equal program).
1089#program whd #x #y #z whd in match (policy_equal ???); whd in match (policy_equal ?y ?);
1090whd in match (policy_equal ? x z); #Hxy #Hyz #n #Hn lapply (Hxy n Hn) -Hxy
1091lapply (Hyz n Hn) -Hyz cases (bvt_lookup … (bitvector_of_nat ? n) (\snd x) 〈0,short_jump〉)
1092#x1 #x2
1093cases (bvt_lookup … (bitvector_of_nat ? n) (\snd y) 〈0,short_jump〉) #y1 #y2
1094cases (bvt_lookup … (bitvector_of_nat ? n) (\snd z) 〈0,short_jump〉) #z1 #z2
1095normalize nodelta //
1096qed.
1097
1098definition policy_equal_opt ≝
1099  λprogram:list labelled_instruction.λp1,p2:option ppc_pc_map.
1100  match p1 with
1101  [ Some x1 ⇒ match p2 with
1102              [ Some x2 ⇒ policy_equal program x1 x2
1103              | _       ⇒ False
1104              ]
1105  | None    ⇒ p2 = None ?
1106  ].
1107
1108lemma pe_refl: ∀program.reflexive ? (policy_equal_opt program).
1109#program whd #x whd cases x
1110[ //
1111| #y @pe_int_refl
1112]
1113qed.
1114
1115lemma pe_sym: ∀program.symmetric ? (policy_equal_opt program).
1116#program whd #x #y #Hxy whd cases y in Hxy;
1117[ cases x
1118  [ //
1119  | #x' #H @⊥ @(absurd ? H) /2 by nmk/
1120  ]
1121| #y' cases x
1122  [ #H @⊥ @(absurd ? H) whd in match (policy_equal_opt ???); @nmk #H destruct (H)
1123  | #x' #H @pe_int_sym @H
1124  ]
1125]
1126qed.
1127
1128lemma pe_trans: ∀program.transitive ? (policy_equal_opt program).
1129#program whd #x #y #z cases x
1130[ #Hxy #Hyz >Hxy in Hyz; //
1131| #x' cases y
1132  [ #H @⊥ @(absurd ? H) /2 by nmk/
1133  | #y' cases z
1134    [ #_ #H @⊥ @(absurd ? H) /2 by nmk/
1135    | #z' @pe_int_trans
1136    ]
1137  ]
1138]
1139qed.
1140
1141definition step_none: ∀program.∀n.
1142  (\snd (pi1 ?? (jump_expansion_internal program n))) = None ? →
1143  ∀k.(\snd (pi1 ?? (jump_expansion_internal program (n+k)))) = None ?.
1144#program #n lapply (refl ? (jump_expansion_internal program n))
1145 cases (jump_expansion_internal program n) in ⊢ (???% → %);
1146 #x1 cases x1 #p1 #j1 -x1; #H1 #Heqj #Hj #k elim k
1147[ <plus_n_O >Heqj @Hj
1148| #k' -k <plus_n_Sm whd in match (jump_expansion_internal program (S (n+k')));
1149  lapply (refl ? (jump_expansion_internal program (n+k')))
1150  cases (jump_expansion_internal program (n+k')) in ⊢ (???% → % → %);
1151  #x2 cases x2 -x2 #c2 #p2 normalize nodelta #H #Heqj2
1152  cases p2 in H Heqj2;
1153  [ #H #Heqj2 #_ whd in match (jump_expansion_internal ??);
1154    >Heqj2 normalize nodelta @refl
1155  | #x #H #Heqj2 #abs destruct (abs)
1156  ]
1157]
1158qed.
1159
1160lemma pe_step: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
1161  ∀n.policy_equal_opt program (\snd (pi1 ?? (jump_expansion_internal program n)))
1162   (\snd (pi1 ?? (jump_expansion_internal program (S n)))) →
1163  policy_equal_opt program (\snd (pi1 ?? (jump_expansion_internal program (S n))))
1164    (\snd (pi1 ?? (jump_expansion_internal program (S (S n))))).
1165#program #n #Heq
1166cases daemon (* XXX *)
1167qed.
1168
1169(* this is in the stdlib, but commented out, why? *)
1170theorem plus_Sn_m1: ∀n,m:nat. S m + n = m + S n.
1171  #n (elim n) normalize /2 by S_pred/ qed.
1172 
1173lemma equal_remains_equal: ∀program:(Σl:list labelled_instruction.|l| < 2^16).∀n:ℕ.
1174  policy_equal_opt program (\snd (pi1 … (jump_expansion_internal program n)))
1175   (\snd (pi1 … (jump_expansion_internal program (S n)))) →
1176  ∀k.k ≥ n → policy_equal_opt program (\snd (pi1 … (jump_expansion_internal program n)))
1177   (\snd (pi1 … (jump_expansion_internal program k))).
1178#program #n #Heq #k #Hk elim (le_plus_k … Hk); #z #H >H -H -Hk -k;
1179lapply Heq -Heq; lapply n -n; elim z -z;
1180[ #n #Heq <plus_n_O @pe_refl
1181| #z #Hind #n #Heq <plus_Sn_m1 whd in match (plus (S n) z);
1182  @(pe_trans … (\snd (pi1 … (jump_expansion_internal program (S n)))))
1183  [ @Heq
1184  | @Hind @pe_step @Heq
1185  ]
1186]
1187qed.
1188
1189(* this number monotonically increases over iterations, maximum 2*|program| *)
1190let rec measure_int (program: list labelled_instruction) (policy: ppc_pc_map) (acc: ℕ)
1191 on program: ℕ ≝
1192 match program with
1193 [ nil      ⇒ acc
1194 | cons h t ⇒ match (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)) with
1195   [ long_jump   ⇒ measure_int t policy (acc + 2)
1196   | medium_jump ⇒ measure_int t policy (acc + 1)
1197   | _           ⇒ measure_int t policy acc
1198   ]
1199 ].
1200
1201lemma measure_plus: ∀program.∀policy.∀x,d:ℕ.
1202 measure_int program policy (x+d) = measure_int program policy x + d.
1203#program #policy #x #d generalize in match x; -x elim d
1204[ //
1205| -d; #d #Hind elim program
1206  [ / by refl/
1207  | #h #t #Hd #x whd in match (measure_int ???); whd in match (measure_int ?? x);
1208    cases (\snd (bvt_lookup … (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉))
1209    [ normalize nodelta @Hd
1210    |2,3: normalize nodelta >associative_plus >(commutative_plus (S d) ?) <associative_plus
1211      @Hd
1212    ]
1213  ]
1214]
1215qed.
1216
1217lemma measure_le: ∀program.∀policy.
1218  measure_int program policy 0 ≤ 2*|program|.
1219#program #policy elim program
1220[ normalize @le_n
1221| #h #t #Hind whd in match (measure_int ???);
1222  cases (\snd (lookup ?? (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉))
1223  [ normalize nodelta @(transitive_le ??? Hind) /2 by monotonic_le_times_r/
1224  |2,3: normalize nodelta >measure_plus <times_n_Sm >(commutative_plus 2 ?)
1225    @le_plus [1,3: @Hind |2,4: / by le_n/ ]
1226  ]
1227]
1228qed.
1229
1230(* uses the second part of policy_increase *)
1231lemma measure_incr_or_equal: ∀program:Σl:list labelled_instruction.|l|<2^16.
1232  ∀policy:Σp:ppc_pc_map.
1233    out_of_program_none program p ∧
1234    jump_not_in_policy program p ∧ \fst p < 2^16.
1235  ∀l.|l| ≤ |program| → ∀acc:ℕ.
1236  match \snd (jump_expansion_step program (create_label_map program) policy) with
1237  [ None   ⇒ True
1238  | Some p ⇒ measure_int l policy acc ≤ measure_int l p acc
1239  ].
1240#program #policy #l elim l -l;
1241[ #Hp #acc cases (jump_expansion_step ???) #pi1 cases pi1 #p #q -pi1; cases q [ // | #x #_ @le_n ]
1242| #h #t #Hind #Hp #acc
1243  lapply (refl ? (jump_expansion_step program (create_label_map program) policy))
1244  cases (jump_expansion_step ???) in ⊢ (???% → %); #pi1 cases pi1 -pi1 #c #r cases r
1245  [ / by I/
1246  | #x normalize nodelta #Hx #Hjeq lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hx))) (|t|) Hp)
1247    whd in match (measure_int ???); whd in match (measure_int ? x ?);
1248    cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)
1249    #x1 #x2 cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (\snd x) 〈0,short_jump〉)
1250    #y1 #y2 normalize nodelta #Hblerp cases Hblerp cases x2 cases y2
1251    [1,4,5,7,8,9: #H cases H
1252    |2,3,6: #_ normalize nodelta
1253      [1,2: @(transitive_le ? (measure_int t x acc))
1254      |3: @(transitive_le ? (measure_int t x (acc+1)))
1255      ]
1256      [2,4,5,6: >measure_plus [1,2: @le_plus_n_r] >measure_plus @le_plus / by le_n/]
1257      >Hjeq in Hind; #Hind @Hind @(transitive_le … Hp) @le_n_Sn
1258    |11,12,13,15,16,17: #H destruct (H)
1259    |10,14,18: normalize nodelta #_ >Hjeq in Hind; #Hind @Hind @(transitive_le … Hp) @le_n_Sn
1260    ]
1261  ]
1262]
1263qed.
1264
1265(* these lemmas seem superfluous, but not sure how *)
1266lemma bla: ∀a,b:ℕ.a + a = b + b → a = b.
1267 #a elim a
1268 [ normalize #b //
1269 | -a #a #Hind #b cases b [ /2 by le_n_O_to_eq/ | -b #b normalize
1270   <plus_n_Sm <plus_n_Sm #H
1271   >(Hind b (injective_S ?? (injective_S ?? H))) // ]
1272 ]
1273qed.
1274
1275lemma sth_not_s: ∀x.x ≠ S x.
1276 #x cases x
1277 [ // | #y // ]
1278qed.
1279 
1280lemma measure_full: ∀program.∀policy.
1281  measure_int program policy 0 = 2*|program| → ∀i.i<|program| →
1282  is_jump (\snd (nth i ? program 〈None ?,Comment []〉)) →
1283  (\snd (bvt_lookup ?? (bitvector_of_nat ? i) (\snd policy) 〈0,short_jump〉)) = long_jump.
1284#program #policy elim program in ⊢ (% → ∀i.% → ? → %);
1285[ #Hm #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
1286| #h #t #Hind #Hm #i #Hi #Hj
1287  cases (le_to_or_lt_eq … Hi) -Hi
1288  [ #Hi @Hind
1289    [ whd in match (measure_int ???) in Hm;
1290      cases (\snd (bvt_lookup … (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)) in Hm;
1291      normalize nodelta
1292      [ #H @⊥ @(absurd ? (measure_le t policy)) >H @lt_to_not_le /2 by lt_plus, le_n/
1293      | >measure_plus >commutative_plus #H @⊥ @(absurd ? (measure_le t policy))
1294        <(plus_to_minus … (sym_eq … H)) @lt_to_not_le normalize /2 by le_n/
1295      | >measure_plus <times_n_Sm >commutative_plus /2 by injective_plus_r/
1296      ]
1297    | @(le_S_S_to_le … Hi)
1298    | @Hj
1299    ]
1300  | #Hi >(injective_S … Hi) whd in match (measure_int ???) in Hm;
1301    cases (\snd (bvt_lookup … (bitvector_of_nat ? (|t|)) (\snd policy) 〈0,short_jump〉)) in Hm;
1302    normalize nodelta
1303    [ #Hm @⊥ @(absurd ? (measure_le t policy)) >Hm @lt_to_not_le /2 by lt_plus, le_n/
1304    | >measure_plus >commutative_plus #H @⊥ @(absurd ? (measure_le t policy))
1305      <(plus_to_minus … (sym_eq … H)) @lt_to_not_le normalize /2 by le_n/
1306    | >measure_plus <times_n_Sm >commutative_plus /2 by injective_plus_r/
1307    ]
1308  ]
1309]
1310qed.
1311
1312(* uses second part of policy_increase *)
1313lemma measure_special: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
1314  ∀policy:Σp:ppc_pc_map.
1315    out_of_program_none program p ∧ jump_not_in_policy program p ∧ \fst p < 2^16.
1316  match (\snd (pi1 ?? (jump_expansion_step program (create_label_map program) policy))) with
1317  [ None ⇒ True
1318  | Some p ⇒ measure_int program policy 0 = measure_int program p 0 → policy_equal program policy p ].
1319#program #policy lapply (refl ? (pi1 ?? (jump_expansion_step program (create_label_map program) policy)))
1320cases (jump_expansion_step program (create_label_map program) policy) in ⊢ (???% → %);
1321#p cases p -p #ch #pol normalize nodelta cases pol
1322[ / by I/
1323| #p normalize nodelta #Hpol #eqpol lapply (le_n (|program|))
1324  @(list_ind ?  (λx.|x| ≤ |pi1 ?? program| →
1325      measure_int x policy 0 = measure_int x p 0 →
1326      policy_equal x policy p) ?? (pi1 ?? program))
1327 [ #_ #_ #i #Hi @⊥ @(absurd ? Hi) @not_le_Sn_O
1328 | #h #t #Hind #Hp #Hm #i #Hi cases (le_to_or_lt_eq … Hi) -Hi;
1329   [ #Hi @Hind
1330     [ @(transitive_le … Hp) / by /
1331     | whd in match (measure_int ???) in Hm; whd in match (measure_int ? p ?) in Hm;
1332       lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hpol))) (|t|) Hp) #Hinc
1333       cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉) in Hm Hinc; #x1 #x2
1334       cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉); #y1 #y2
1335       #Hm #Hinc lapply Hm -Hm; lapply Hinc -Hinc; normalize nodelta
1336       cases x2 cases y2 normalize nodelta
1337       [1: / by /
1338       |2,3: >measure_plus #_ #H @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt
1339         lapply (measure_incr_or_equal program policy t ? 0)
1340         [1,3: @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
1341       |4,7,8: #H elim H #H2 [1,3,5: cases H2 |2,4,6: destruct (H2) ]
1342       |5: >measure_plus >measure_plus >commutative_plus >(commutative_plus ? 1)
1343         #_ #H @(injective_plus_r … H)
1344       |6: >measure_plus >measure_plus
1345         change with (1+1) in match (2); >assoc_plus1 >(commutative_plus 1 (measure_int ???))
1346         #_ #H @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt @monotonic_le_plus_l
1347         lapply (measure_incr_or_equal program policy t ? 0)
1348         [ @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
1349       |9: >measure_plus >measure_plus >commutative_plus >(commutative_plus ? 2)
1350         #_ #H @(injective_plus_r … H)
1351       ]
1352     | @(le_S_S_to_le … Hi)
1353     ]
1354   | #Hi >(injective_S … Hi) whd in match (measure_int ???) in Hm;
1355     whd in match (measure_int ? p ?) in Hm;
1356     lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? Hpol))) (|t|) Hp)
1357     cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉) in Hm;
1358     #x1 #x2
1359     cases (bvt_lookup ?? (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉);
1360     #y1 #y2
1361     normalize nodelta cases x2 cases y2 normalize nodelta
1362     [1,5,9: #_ #_ @refl
1363     |4,7,8: #_ #H elim H #H2 [1,3,5: cases H2 |2,4,6: destruct (H2) ]
1364     |2,3: >measure_plus #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt
1365       lapply (measure_incr_or_equal program policy t ? 0)
1366       [1,3: @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
1367     |6: >measure_plus >measure_plus
1368        change with (1+1) in match (2); >assoc_plus1 >(commutative_plus 1 (measure_int ???))
1369        #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt @monotonic_le_plus_l
1370        lapply (measure_incr_or_equal program policy t ? 0)
1371        [ @(transitive_le … Hp) @le_n_Sn ] >eqpol / by /
1372     ]
1373   ]
1374 ]
1375qed.
1376
1377lemma le_to_eq_plus: ∀n,z.
1378  n ≤ z → ∃k.z = n + k.
1379 #n #z elim z
1380 [ #H cases (le_to_or_lt_eq … H)
1381   [ #H2 @⊥ @(absurd … H2) @not_le_Sn_O
1382   | #H2 @(ex_intro … 0) >H2 //
1383   ]
1384 | #z' #Hind #H cases (le_to_or_lt_eq … H)
1385   [ #H' elim (Hind (le_S_S_to_le … H')) #k' #H2 @(ex_intro … (S k'))
1386     >H2 >plus_n_Sm //
1387   | #H' @(ex_intro … 0) >H' //
1388   ]
1389 ]
1390qed.
1391
1392lemma measure_zero: ∀l.∀program:Σl:list labelled_instruction.|l| < 2^16.
1393  match jump_expansion_start program (create_label_map program) with
1394  [ None ⇒ True
1395  | Some p ⇒ |l| ≤ |program| → measure_int l p 0 = 0
1396  ].
1397 #l #program lapply (refl ? (jump_expansion_start program (create_label_map program)))
1398 cases (jump_expansion_start program (create_label_map program)) in ⊢ (???% → %); #p #Hp #EQ
1399 cases p in Hp EQ;
1400 [ / by I/
1401 | #pl normalize nodelta #Hpl #EQ elim l
1402   [ / by refl/
1403   | #h #t #Hind #Hp whd in match (measure_int ???);
1404     >(proj2 ?? (proj1 ?? Hpl) (|t|))
1405     [ normalize nodelta @Hind ]
1406     @(transitive_le … Hp) [ @le_n_Sn | @ le_n ]
1407   ]
1408 ]   
1409qed.
1410
1411(* the actual computation of the fixpoint *)
1412definition je_fixpoint: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
1413  Σp:option ppc_pc_map.
1414    And (match p with
1415      [ None ⇒ True
1416      | Some pol ⇒ And (out_of_program_none program pol)
1417      (policy_compact program (create_label_map program) pol)
1418      ])
1419    (∃n.∀k.n < k →
1420      policy_equal_opt program (\snd (pi1 ?? (jump_expansion_internal program k))) p).
1421#program @(\snd (pi1 ?? (jump_expansion_internal program (2*|program|)))) @conj
1422[ lapply (pi2 ?? (jump_expansion_internal program (2*|program|)))
1423    cases (jump_expansion_internal program (2*|program|)) #p cases p -p
1424    #c #pol #Hp cases pol
1425    [ normalize nodelta //
1426    | #x normalize nodelta #H @conj [ @(proj1 ?? (proj1 ?? (proj1 ?? H)))
1427    | cases daemon ] ]
1428| cases (dec_bounded_exists (λk.policy_equal_opt (pi1 ?? program)
1429   (\snd (pi1 ?? (jump_expansion_internal program k)))
1430   (\snd (pi1 ?? (jump_expansion_internal program (S k))))) ? (2*|program|))
1431[ #Hex elim Hex -Hex #x #Hx @(ex_intro … x) #k #Hk
1432  @pe_trans
1433  [ @(\snd (pi1 ?? (jump_expansion_internal program x)))
1434  | @pe_sym @equal_remains_equal
1435    [ @(proj2 ?? Hx)
1436    | @le_S_S_to_le @le_S @Hk
1437    ]
1438  | @equal_remains_equal
1439    [ @(proj2 ?? Hx)
1440    | @le_S_S_to_le @le_S @(proj1 ?? Hx)
1441    ]   
1442  ]
1443| #Hnex lapply (not_exists_forall … Hnex) -Hnex; #Hfa
1444  @(ex_intro … (2*|program|)) #k #Hk @pe_sym @equal_remains_equal
1445  [ lapply (refl ? (jump_expansion_internal program (2*|program|)))
1446    cases (jump_expansion_internal program (2*|program|)) in ⊢ (???% → %);
1447    #x cases x -x #Fch #Fpol normalize nodelta #HFpol cases Fpol in HFpol; normalize nodelta
1448    [ (* if we're at None in 2*|program|, we're at None in S 2*|program| too *)
1449      #HFpol #EQ whd in match (jump_expansion_internal ??); >EQ
1450      normalize nodelta / by /
1451    | #Fp #HFp #EQ whd in match (jump_expansion_internal ??);
1452      >EQ normalize nodelta
1453      lapply (refl ? (jump_expansion_step program (create_label_map program) «Fp,?»))
1454      [ @conj [ @(proj1 ?? (proj1 ?? HFp)) | @(proj2 ?? HFp) ]
1455      | lapply (measure_full program Fp ?)
1456        [ @le_to_le_to_eq
1457          [ @measure_le
1458          | cut (∀x:ℕ.x ≤ 2*|program| →
1459             ∃p.(\snd (pi1 ?? (jump_expansion_internal program x)) = Some ? p ∧       
1460                x ≤ measure_int program p 0))
1461            [ #x elim x
1462              [ #Hx lapply (refl ? (jump_expansion_start program (create_label_map program)))
1463                cases (jump_expansion_start program (create_label_map program)) in ⊢ (???% → %);
1464                #z cases z -z normalize nodelta
1465                [ #Waar #Heqn @⊥ elim (le_to_eq_plus ?? Hx) #k #Hk
1466                  @(absurd … (step_none program 0 ? k))
1467                  [ whd in match (jump_expansion_internal ??); >Heqn @refl
1468                  | <Hk >EQ @nmk #H destruct (H)
1469                  ]
1470                | #pol #Hpol #Heqpol @(ex_intro ?? pol) @conj
1471                  [ whd in match (jump_expansion_internal ??); >Heqpol @refl
1472                  | @le_O_n
1473                  ]
1474                ]
1475              | -x #x #Hind #Hx
1476                lapply (refl ? (jump_expansion_internal program (S x)))
1477                cases (jump_expansion_internal program (S x)) in ⊢ (???% → %);
1478                #z cases z -z #Sxch #Sxpol cases Sxpol -Sxpol normalize nodelta
1479                [ #H #HeqSxpol @⊥ elim (le_to_eq_plus ?? Hx) #k #Hk
1480                  @(absurd … (step_none program (S x) ? k))
1481                  [ >HeqSxpol / by /
1482                  | <Hk >EQ @nmk #H destruct (H)
1483                  ]
1484                | #Sxpol #HSxpol #HeqSxpol @(ex_intro ?? Sxpol) @conj
1485                  [ @refl
1486                  | elim (Hind (transitive_le … (le_n_Sn x) Hx))
1487                    #xpol #Hxpol @(le_to_lt_to_lt … (proj2 ?? Hxpol))
1488                    lapply (measure_incr_or_equal program xpol program (le_n (|program|)) 0)
1489                    [ cases (jump_expansion_internal program x) in Hxpol;
1490                      #z cases z -z #xch #xpol normalize nodelta #H #H2 >(proj1 ?? H2) in H;
1491                      normalize nodelta #H @conj [ @(proj1 ?? (proj1 ?? H)) | @(proj2 ?? H) ]
1492                    | lapply (Hfa x Hx) lapply HeqSxpol -HeqSxpol
1493                      whd in match (jump_expansion_internal program (S x));
1494                      lapply (refl ? (jump_expansion_internal program x))
1495                      lapply Hxpol -Hxpol cases (jump_expansion_internal program x) in ⊢ (% → ???% → %);
1496                      #z cases z -z #xch #b normalize nodelta #H #Heq >(proj1 ?? Heq) in H;
1497                      #H #Heq cases xch in Heq; #Heq normalize nodelta
1498                      [ lapply (refl ? (jump_expansion_step program (create_label_map (pi1 ?? program)) «xpol,?»))
1499                        [ @conj [ @(proj1 ?? (proj1 ?? H)) | @(proj2 ?? H) ]
1500                        | cases (jump_expansion_step ???) in ⊢ (???% → %); #z cases z -z #a #c
1501                          normalize nodelta cases c normalize nodelta
1502                          [ #H1 #Heq #H2 destruct (H2)
1503                          | #d #H1 #Heq #H2 destruct (H2) #Hfull #H2 elim (le_to_or_lt_eq … H2)
1504                            [ / by /
1505                            | #H3 lapply (measure_special program «xpol,?»)
1506                              [ @conj [ @(proj1 ?? (proj1 ?? H)) | @(proj2 ?? H) ]
1507                              | >Heq normalize nodelta #H4 @⊥ @(absurd … (H4 H3)) @Hfull
1508                              ]
1509                            ]
1510                          ]
1511                        ]
1512                      | lapply (refl ? (jump_expansion_step program (create_label_map (pi1 ?? program)) «xpol,?»))
1513                        [ @conj [ @(proj1 ?? (proj1 ?? H)) | @(proj2 ?? H) ]
1514                        | cases (jump_expansion_step ???) in ⊢ (???% → %); #z cases z -z #a #c
1515                          normalize nodelta cases c normalize nodelta
1516                          [ #H1 #Heq #H2 #H3 #_ @⊥ @(absurd ?? H3) @pe_refl
1517                          | #d #H1 #Heq #H2 #H3 @⊥ @(absurd ?? H3) @pe_refl
1518                          ]
1519                        ]
1520                      ]
1521                    ]
1522                  ]
1523                ]
1524              ]
1525            | #H elim (H (2*|program|) (le_n ?)) #plp >EQ #Hplp
1526              >(Some_eq ??? (proj1 ?? Hplp)) @(proj2 ?? Hplp)
1527            ]
1528          ]
1529        | #Hfull cases (jump_expansion_step program (create_label_map program) «Fp,?») in ⊢ (???% → %);
1530          #x cases x -x #Gch #Gpol cases Gpol normalize nodelta
1531          [ #H #EQ2 @⊥ @(absurd ?? H) @Hfull
1532          | #Gp #HGp #EQ2 cases Fch
1533            [ normalize nodelta #i #Hi
1534              cases (dec_is_jump (\snd (nth i ? program 〈None ?, Comment []〉))) #Hj
1535              [ lapply (proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? HGp))) i Hi)
1536                lapply (Hfull i Hi Hj) cases (bvt_lookup … (bitvector_of_nat ? i) (\snd Fp) 〈0,short_jump〉)
1537                #fp #fj #Hfj >Hfj normalize nodelta
1538                cases (bvt_lookup … (bitvector_of_nat ? i) (\snd Gp) 〈0,short_jump〉) #gp #gj
1539                cases gj normalize nodelta
1540                [1,2: #H cases H #H2 cases H2 destruct (H2)
1541                |3: #_ @refl
1542                ]
1543              | >(proj2 ?? (proj1 ?? (proj1 ?? (proj1 ?? (proj1 ?? HGp)))) i Hi Hj)
1544                >(proj2 ?? (proj1 ?? (proj1 ?? HFp)) i Hi Hj) @refl
1545              ]
1546            | normalize nodelta /2 by pe_int_refl/
1547            ]
1548          ]
1549        ]
1550      ]
1551    ]
1552  | @le_S_S_to_le @le_S @Hk
1553  ]
1554| #n cases (jump_expansion_internal program n) cases (jump_expansion_internal program (S n))
1555  #x cases x -x #nch #npol normalize nodelta #Hnpol
1556  #x cases x -x #Sch #Spol normalize nodelta #HSpol
1557  cases npol in Hnpol; cases Spol in HSpol;
1558  [ #Hnpol #HSpol %1 //
1559  |2,3: #x #Hnpol #HSpol %2 @nmk whd in match (policy_equal ???); //
1560    #H destruct (H)
1561  |4: #np #Hnp #Sp #HSp whd in match (policy_equal ???); @dec_bounded_forall #m
1562      cases (bvt_lookup ?? (bitvector_of_nat 16 m) ? 〈0,short_jump〉)
1563      #x1 #x2
1564      cases (bvt_lookup ?? (bitvector_of_nat ? m) ? 〈0,short_jump〉)
1565      #y1 #y2 normalize nodelta
1566      @dec_eq_jump_length 
1567  ]
1568]
1569qed.
1570
1571include alias "arithmetics/nat.ma".
1572include alias "basics/logic.ma".
1573
1574(* The glue between Policy and Assembly. *)
1575definition jump_expansion':
1576∀program:preamble × (Σl:list labelled_instruction.|l| < 2^16).
1577 option (Σsigma:Word → Word × bool.
1578   ∀ppc: Word.
1579   let pc ≝ \fst (sigma ppc) in
1580   let labels ≝ \fst (create_label_cost_map (\snd program)) in
1581   let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def ?? labels x 0) in
1582   let instruction ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in
1583   let next_pc ≝ \fst (sigma (add ? ppc (bitvector_of_nat ? 1))) in
1584     And (nat_of_bitvector … ppc ≤ |\snd program| →
1585       next_pc = add ? pc (bitvector_of_nat …
1586         (instruction_size lookup_labels (λx.\fst (sigma x)) (λx.\snd (sigma x)) ppc instruction)))
1587      (Or (nat_of_bitvector … ppc < |\snd program| →
1588        nat_of_bitvector … pc < nat_of_bitvector … next_pc)
1589       (nat_of_bitvector … ppc = |\snd program| → next_pc = (zero …)))) ≝
1590 λprogram.
1591  let policy ≝ pi1 … (je_fixpoint (\snd program)) in
1592  match policy with
1593  [ None ⇒ None ?
1594  | Some x ⇒ Some ?
1595      «λppc.let 〈pc,jl〉 ≝ bvt_lookup ?? ppc (\snd x) 〈0,short_jump〉 in
1596        〈bitvector_of_nat 16 pc,jmpeqb jl long_jump〉,?»
1597  ].
1598 #ppc normalize nodelta cases daemon
1599qed.
Note: See TracBrowser for help on using the repository browser.