source: src/ASM/Policy.ma @ 2032

Last change on this file since 2032 was 2032, checked in by sacerdot, 8 years ago

!! BEWARE: major commit !!

1) [affects everybody]

split for vectors renamed to vsplit to reduce ambiguity since split is
now also a function in the standard library.
Note: I have not been able to propagate the changes everywhere in
the front-end/back-end because some files do not compile

2) [affects everybody]

functions on Vectors copied both in the front and back-ends moved to
Vectors.ma

3) [affects only the back-end]

subaddressing_mode_elim redesigned from scratch and now also applied to
Policy.ma. Moreover, all daemons about that have been closed.
The new one is much simpler to apply since it behaves like a standard
elimination principle: @(subaddressing_mode_elim \ldots x) where x is
the thing to eliminate.

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