source: src/ASM/Assembly.ma @ 1592

Last change on this file since 1592 was 1592, checked in by boender, 8 years ago
  • updated definitions to work with programs of maximum 216 instructions
File size: 92.9 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Status.ma".
5include alias "basics/logic.ma".
6include alias "arithmetics/nat.ma".
7
8definition assembly_preinstruction ≝
9  λA: Type[0].
10  λaddr_of: A → Byte. (* relative *)
11  λpre: preinstruction A.
12  match pre with
13  [ ADD addr1 addr2 ⇒
14     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
15      [ REGISTER r ⇒ λ_.[ ([[false;false;true;false;true]]) @@ r ]
16      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;false;false;true;false;true]]); b1 ]
17      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;false;false;true;true;i1]]) ]
18      | DATA b1 ⇒ λ_. [ ([[false;false;true;false;false;true;false;false]]) ; b1 ]
19      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
20  | ADDC addr1 addr2 ⇒
21     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
22      [ REGISTER r ⇒ λ_.[ ([[false;false;true;true;true]]) @@ r ]
23      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;true;false;true;false;true]]); b1 ]
24      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;true;false;true;true;i1]]) ]
25      | DATA b1 ⇒ λ_. [ ([[false;false;true;true;false;true;false;false]]) ; b1 ]
26      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
27  | ANL addrs ⇒
28     match addrs with
29      [ inl addrs ⇒ match addrs with
30         [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
31           match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
32            [ REGISTER r ⇒ λ_.[ ([[false;true;false;true;true]]) @@ r ]
33            | DIRECT b1 ⇒ λ_.[ ([[false;true;false;true;false;true;false;true]]); b1 ]
34            | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;true;false;true;true;i1]]) ]
35            | DATA b1 ⇒ λ_. [ ([[false;true;false;true;false;true;false;false]]) ; b1 ]
36            | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
37         | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
38            let b1 ≝
39             match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
40              [ DIRECT b1 ⇒ λ_.b1
41              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
42            match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
43             [ ACC_A ⇒ λ_.[ ([[false;true;false;true;false;false;true;false]]) ; b1 ]
44             | DATA b2 ⇒ λ_. [ ([[false;true;false;true;false;false;true;true]]) ; b1 ; b2 ]
45             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
46         ]
47      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
48         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
49          [ BIT_ADDR b1 ⇒ λ_.[ ([[true;false;false;false;false;false;true;false]]) ; b1 ]
50          | N_BIT_ADDR b1 ⇒ λ_. [ ([[true;false;true;true;false;false;false;false]]) ; b1 ]
51          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
52  | CLR addr ⇒
53     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
54      [ ACC_A ⇒ λ_.
55         [ ([[true; true; true; false; false; true; false; false]]) ]
56      | CARRY ⇒ λ_.
57         [ ([[true; true; false; false; false; false; true; true]]) ]
58      | BIT_ADDR b1 ⇒ λ_.
59         [ ([[true; true; false; false; false; false; true; false]]) ; b1 ]
60      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
61  | CPL addr ⇒
62     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
63      [ ACC_A ⇒ λ_.
64         [ ([[true; true; true; true; false; true; false; false]]) ]
65      | CARRY ⇒ λ_.
66         [ ([[true; false; true; true; false; false; true; true]]) ]
67      | BIT_ADDR b1 ⇒ λ_.
68         [ ([[true; false; true; true; false; false; true; false]]) ; b1 ]
69      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
70  | DA addr ⇒
71     [ ([[true; true; false; true; false; true; false; false]]) ]
72  | DEC addr ⇒
73     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with
74      [ ACC_A ⇒ λ_.
75         [ ([[false; false; false; true; false; true; false; false]]) ]
76      | REGISTER r ⇒ λ_.
77         [ ([[false; false; false; true; true]]) @@ r ]
78      | DIRECT b1 ⇒ λ_.
79         [ ([[false; false; false; true; false; true; false; true]]); b1 ]
80      | INDIRECT i1 ⇒ λ_.
81         [ ([[false; false; false; true; false; true; true; i1]]) ]
82      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
83      | DJNZ addr1 addr2 ⇒
84         let b2 ≝ addr_of addr2 in
85         match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with
86          [ REGISTER r ⇒ λ_.
87             [ ([[true; true; false; true; true]]) @@ r ; b2 ]
88          | DIRECT b1 ⇒ λ_.
89             [ ([[true; true; false; true; false; true; false; true]]); b1; b2 ]
90          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
91      | JC addr ⇒
92        let b1 ≝ addr_of addr in
93          [ ([[false; true; false; false; false; false; false; false]]); b1 ]
94      | JNC addr ⇒
95         let b1 ≝ addr_of addr in
96           [ ([[false; true; false; true; false; false; false; false]]); b1 ]
97      | JZ addr ⇒
98         let b1 ≝ addr_of addr in
99           [ ([[false; true; true; false; false; false; false; false]]); b1 ]
100      | JNZ addr ⇒
101         let b1 ≝ addr_of addr in
102           [ ([[false; true; true; true; false; false; false; false]]); b1 ]
103      | JB addr1 addr2 ⇒
104         let b2 ≝ addr_of addr2 in
105         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
106          [ BIT_ADDR b1 ⇒ λ_.
107             [ ([[false; false; true; false; false; false; false; false]]); b1; b2 ]
108          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
109      | JNB addr1 addr2 ⇒
110         let b2 ≝ addr_of addr2 in
111         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
112          [ BIT_ADDR b1 ⇒ λ_.
113             [ ([[false; false; true; true; false; false; false; false]]); b1; b2 ]
114          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
115      | JBC addr1 addr2 ⇒
116         let b2 ≝ addr_of addr2 in
117         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
118          [ BIT_ADDR b1 ⇒ λ_.
119             [ ([[false; false; false; true; false; false; false; false]]); b1; b2 ]
120          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
121      | CJNE addrs addr3 ⇒
122         let b3 ≝ addr_of addr3 in
123         match addrs with
124          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
125             match addr2 return λx. bool_to_Prop (is_in ? [[direct;data]] x) → ? with
126              [ DIRECT b1 ⇒ λ_.
127                 [ ([[true; false; true; true; false; true; false; true]]); b1; b3 ]
128              | DATA b1 ⇒ λ_.
129                 [ ([[true; false; true; true; false; true; false; false]]); b1; b3 ]
130              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
131          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
132             let b2 ≝
133              match addr2 return λx. bool_to_Prop (is_in ? [[data]] x) → ? with
134               [ DATA b2 ⇒ λ_. b2
135               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) in
136             match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with
137              [ REGISTER r ⇒ λ_.
138                 [ ([[true; false; true; true; true]]) @@ r; b2; b3 ]
139              | INDIRECT i1 ⇒ λ_.
140                 [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ]
141              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
142         ]
143  | DIV addr1 addr2 ⇒
144     [ ([[true;false;false;false;false;true;false;false]]) ]
145  | INC addr ⇒
146     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with
147      [ ACC_A ⇒ λ_.
148         [ ([[false;false;false;false;false;true;false;false]]) ]         
149      | REGISTER r ⇒ λ_.
150         [ ([[false;false;false;false;true]]) @@ r ]
151      | DIRECT b1 ⇒ λ_.
152         [ ([[false; false; false; false; false; true; false; true]]); b1 ]
153      | INDIRECT i1 ⇒ λ_.
154        [ ([[false; false; false; false; false; true; true; i1]]) ]
155      | DPTR ⇒ λ_.
156        [ ([[true;false;true;false;false;false;true;true]]) ]
157      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
158  | MOV addrs ⇒
159     match addrs with
160      [ inl addrs ⇒
161         match addrs with
162          [ inl addrs ⇒
163             match addrs with
164              [ inl addrs ⇒
165                 match addrs with
166                  [ inl addrs ⇒
167                     match addrs with
168                      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
169                         match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
170                          [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ]
171                          | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ]
172                          | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ]
173                          | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ]
174                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
175                      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
176                         match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with
177                          [ REGISTER r ⇒ λ_.
178                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
179                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ]
180                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ]
181                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ]
182                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
183                          | INDIRECT i1 ⇒ λ_.
184                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
185                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ]
186                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ]
187                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ]
188                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
189                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
190                  | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
191                     let b1 ≝
192                      match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
193                       [ DIRECT b1 ⇒ λ_. b1
194                       | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
195                     match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with
196                      [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1]
197                      | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ]
198                      | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ]
199                      | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ]
200                      | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ]
201                      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
202              | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
203                 match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with
204                  [ DATA16 w ⇒ λ_.
205                     let b1_b2 ≝ split ? 8 8 w in
206                     let b1 ≝ \fst b1_b2 in
207                     let b2 ≝ \snd b1_b2 in
208                      [ ([[true;false;false;true;false;false;false;false]]); b1; b2]
209                  | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
210          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
211             match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
212              [ BIT_ADDR b1 ⇒ λ_.
213                 [ ([[true;false;true;false;false;false;true;false]]); b1 ]
214              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
215      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
216         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
217          [ BIT_ADDR b1 ⇒ λ_.
218             [ ([[true;false;false;true;false;false;true;false]]); b1 ]
219          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
220  | MOVX addrs ⇒
221     match addrs with
222      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
223         match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
224          [ EXT_INDIRECT i1 ⇒ λ_.
225             [ ([[true;true;true;false;false;false;true;i1]]) ]
226          | EXT_INDIRECT_DPTR ⇒ λ_.
227             [ ([[true;true;true;false;false;false;false;false]]) ]
228          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
229      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
230         match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
231          [ EXT_INDIRECT i1 ⇒ λ_.
232             [ ([[true;true;true;true;false;false;true;i1]]) ]
233          | EXT_INDIRECT_DPTR ⇒ λ_.
234             [ ([[true;true;true;true;false;false;false;false]]) ]
235          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
236  | MUL addr1 addr2 ⇒
237     [ ([[true;false;true;false;false;true;false;false]]) ]
238  | NOP ⇒
239     [ ([[false;false;false;false;false;false;false;false]]) ]
240  | ORL addrs ⇒
241     match addrs with
242      [ inl addrs ⇒
243         match addrs with
244          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
245             match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with
246             [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ]
247             | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ]
248             | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ]
249             | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ]
250             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
251          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
252            let b1 ≝
253              match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
254               [ DIRECT b1 ⇒ λ_. b1
255               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
256             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
257              [ ACC_A ⇒ λ_.
258                 [ ([[false;true;false;false;false;false;true;false]]); b1 ]
259              | DATA b2 ⇒ λ_.
260                 [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ]
261              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
262      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in     
263         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
264          [ BIT_ADDR b1 ⇒ λ_.
265             [ ([[false;true;true;true;false;false;true;false]]); b1 ]
266          | N_BIT_ADDR b1 ⇒ λ_.
267             [ ([[true;false;true;false;false;false;false;false]]); b1 ]
268          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
269  | POP addr ⇒
270     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
271      [ DIRECT b1 ⇒ λ_.
272         [ ([[true;true;false;true;false;false;false;false]]) ; b1 ]
273      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
274  | PUSH addr ⇒
275     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
276      [ DIRECT b1 ⇒ λ_.
277         [ ([[true;true;false;false;false;false;false;false]]) ; b1 ]
278      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
279  | RET ⇒
280     [ ([[false;false;true;false;false;false;true;false]]) ]
281  | RETI ⇒
282     [ ([[false;false;true;true;false;false;true;false]]) ]
283  | RL addr ⇒
284     [ ([[false;false;true;false;false;false;true;true]]) ]
285  | RLC addr ⇒
286     [ ([[false;false;true;true;false;false;true;true]]) ]
287  | RR addr ⇒
288     [ ([[false;false;false;false;false;false;true;true]]) ]
289  | RRC addr ⇒
290     [ ([[false;false;false;true;false;false;true;true]]) ]
291  | SETB addr ⇒     
292     match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with
293      [ CARRY ⇒ λ_.
294         [ ([[true;true;false;true;false;false;true;true]]) ]
295      | BIT_ADDR b1 ⇒ λ_.
296         [ ([[true;true;false;true;false;false;true;false]]); b1 ]
297      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
298  | SUBB addr1 addr2 ⇒
299     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
300      [ REGISTER r ⇒ λ_.
301         [ ([[true;false;false;true;true]]) @@ r ]
302      | DIRECT b1 ⇒ λ_.
303         [ ([[true;false;false;true;false;true;false;true]]); b1]
304      | INDIRECT i1 ⇒ λ_.
305         [ ([[true;false;false;true;false;true;true;i1]]) ]
306      | DATA b1 ⇒ λ_.
307         [ ([[true;false;false;true;false;true;false;false]]); b1]
308      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
309  | SWAP addr ⇒
310     [ ([[true;true;false;false;false;true;false;false]]) ]
311  | XCH addr1 addr2 ⇒
312     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with
313      [ REGISTER r ⇒ λ_.
314         [ ([[true;true;false;false;true]]) @@ r ]
315      | DIRECT b1 ⇒ λ_.
316         [ ([[true;true;false;false;false;true;false;true]]); b1]
317      | INDIRECT i1 ⇒ λ_.
318         [ ([[true;true;false;false;false;true;true;i1]]) ]
319      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
320  | XCHD addr1 addr2 ⇒
321     match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with
322      [ INDIRECT i1 ⇒ λ_.
323         [ ([[true;true;false;true;false;true;true;i1]]) ]
324      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
325  | XRL addrs ⇒
326     match addrs with
327      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
328         match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with
329          [ REGISTER r ⇒ λ_.
330             [ ([[false;true;true;false;true]]) @@ r ]
331          | DIRECT b1 ⇒ λ_.
332             [ ([[false;true;true;false;false;true;false;true]]); b1]
333          | INDIRECT i1 ⇒ λ_.
334             [ ([[false;true;true;false;false;true;true;i1]]) ]
335          | DATA b1 ⇒ λ_.
336             [ ([[false;true;true;false;false;true;false;false]]); b1]
337          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
338      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
339         let b1 ≝
340          match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
341           [ DIRECT b1 ⇒ λ_. b1
342           | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
343         match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
344          [ ACC_A ⇒ λ_.
345             [ ([[false;true;true;false;false;false;true;false]]); b1 ]         
346          | DATA b2 ⇒ λ_.
347             [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ]
348          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
349       ].
350
351definition assembly1 ≝
352 λi: instruction.
353 match i with
354  [ ACALL addr ⇒
355     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
356      [ ADDR11 w ⇒ λ_.
357         let v1_v2 ≝ split ? 3 8 w in
358         let v1 ≝ \fst v1_v2 in
359         let v2 ≝ \snd v1_v2 in
360          [ (v1 @@ [[true; false; false; false; true]]) ; v2 ]
361      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
362  | AJMP addr ⇒
363     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
364      [ ADDR11 w ⇒ λ_.
365         let v1_v2 ≝ split ? 3 8 w in
366         let v1 ≝ \fst v1_v2 in
367         let v2 ≝ \snd v1_v2 in
368          [ (v1 @@ [[false; false; false; false; true]]) ; v2 ]
369      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
370  | JMP adptr ⇒
371     [ ([[false;true;true;true;false;false;true;true]]) ]
372  | LCALL addr ⇒
373     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
374      [ ADDR16 w ⇒ λ_.
375         let b1_b2 ≝ split ? 8 8 w in
376         let b1 ≝ \fst b1_b2 in
377         let b2 ≝ \snd b1_b2 in
378          [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ]         
379      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
380  | LJMP addr ⇒
381     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
382      [ ADDR16 w ⇒ λ_.
383         let b1_b2 ≝ split ? 8 8 w in
384         let b1 ≝ \fst b1_b2 in
385         let b2 ≝ \snd b1_b2 in
386          [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ]         
387      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
388  | MOVC addr1 addr2 ⇒
389     match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with
390      [ ACC_DPTR ⇒ λ_.
391         [ ([[true;false;false;true;false;false;true;true]]) ]
392      | ACC_PC ⇒ λ_.
393         [ ([[true;false;false;false;false;false;true;true]]) ]
394      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
395  | SJMP addr ⇒
396     match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with
397      [ RELATIVE b1 ⇒ λ_.
398         [ ([[true;false;false;false;false;false;false;false]]); b1 ]
399      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
400  | RealInstruction instr ⇒
401    assembly_preinstruction [[ relative ]]
402      (λx.
403        match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with
404        [ RELATIVE r ⇒ λ_. r
405        | _ ⇒ λabsd. ⊥
406        ] (subaddressing_modein … x)) instr
407  ].
408  cases absd
409qed.
410
411inductive jump_length: Type[0] ≝
412  | short_jump: jump_length
413  | medium_jump: jump_length
414  | long_jump: jump_length.
415
416(* jump_expansion_policy: instruction number ↦ 〈pc, jump_length〉 *)
417definition jump_expansion_policy ≝ BitVectorTrie (ℕ × jump_length) 16.
418
419definition expand_relative_jump_internal:
420 (Identifier → Word) → jump_length → Identifier → Word → ([[relative]] → preinstruction [[relative]]) →
421 option (list instruction)
422 ≝
423  λlookup_labels,jmp_len.λjmp:Identifier.λpc,i.
424  match jmp_len with
425  [ short_jump ⇒
426     let lookup_address ≝ lookup_labels jmp in
427     let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in
428     let 〈upper, lower〉 ≝ split ? 8 8 result in
429     if eq_bv ? upper (zero 8) then
430      let address ≝ RELATIVE lower in
431       Some ? [ RealInstruction (i address) ]
432     else
433       None ?
434  | medium_jump ⇒ None …
435  | long_jump ⇒
436    Some ?
437    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
438      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
439      LJMP (ADDR16 (lookup_labels jmp))
440    ]
441  ].
442  @ I
443qed.
444
445definition expand_relative_jump: (Identifier → Word) → jump_length → Word → preinstruction Identifier → option (list instruction) ≝
446  λlookup_labels.
447  λjmp_len: jump_length.
448  λpc.
449  λi: preinstruction Identifier.
450  let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in
451  match i with
452  [ JC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JC ?)
453  | JNC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNC ?)
454  | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JB ? baddr)
455  | JZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JZ ?)
456  | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNZ ?)
457  | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JBC ? baddr)
458  | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNB ? baddr)
459  | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (CJNE ? addr)
460  | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (DJNZ ? addr)
461  | ADD arg1 arg2 ⇒ Some ? [ ADD ? arg1 arg2 ]
462  | ADDC arg1 arg2 ⇒ Some ? [ ADDC ? arg1 arg2 ]
463  | SUBB arg1 arg2 ⇒ Some ? [ SUBB ? arg1 arg2 ]
464  | INC arg ⇒ Some ? [ INC ? arg ]
465  | DEC arg ⇒ Some ? [ DEC ? arg ]
466  | MUL arg1 arg2 ⇒ Some ? [ MUL ? arg1 arg2 ]
467  | DIV arg1 arg2 ⇒ Some ? [ DIV ? arg1 arg2 ]
468  | DA arg ⇒ Some ? [ DA ? arg ]
469  | ANL arg ⇒ Some ? [ ANL ? arg ]
470  | ORL arg ⇒ Some ? [ ORL ? arg ]
471  | XRL arg ⇒ Some ? [ XRL ? arg ]
472  | CLR arg ⇒ Some ? [ CLR ? arg ]
473  | CPL arg ⇒ Some ? [ CPL ? arg ]
474  | RL arg ⇒ Some ? [ RL ? arg ]
475  | RR arg ⇒ Some ? [ RR ? arg ]
476  | RLC arg ⇒ Some ? [ RLC ? arg ]
477  | RRC arg ⇒ Some ? [ RRC ? arg ]
478  | SWAP arg ⇒ Some ? [ SWAP ? arg ]
479  | MOV arg ⇒ Some ? [ MOV ? arg ]
480  | MOVX arg ⇒ Some ? [ MOVX ? arg ]
481  | SETB arg ⇒ Some ? [ SETB ? arg ]
482  | PUSH arg ⇒ Some ? [ PUSH ? arg ]
483  | POP arg ⇒ Some ? [ POP ? arg ]
484  | XCH arg1 arg2 ⇒ Some ? [ XCH ? arg1 arg2 ]
485  | XCHD arg1 arg2 ⇒ Some ? [ XCHD ? arg1 arg2 ]
486  | RET ⇒ Some ? [ RET ? ]
487  | RETI ⇒ Some ? [ RETI ? ]
488  | NOP ⇒ Some ? [ RealInstruction (NOP ?) ]
489  ].
490
491definition expand_pseudo_instruction_safe: ? → ? → Word → jump_length → pseudo_instruction → option (list instruction) ≝
492  λlookup_labels.
493  λlookup_datalabels.
494  λpc.
495  λjmp_len.
496  λi.
497  match i with
498  [ Cost cost ⇒ Some ? [ ]
499  | Comment comment ⇒ Some ? [ ]
500  | Call call ⇒
501    match jmp_len with
502    [ short_jump ⇒ None ?
503    | medium_jump ⇒
504      let 〈ignore, address〉 ≝ split ? 5 11 (lookup_labels call) in
505      let 〈fst_5, rest〉 ≝ split ? 5 11 pc in
506      if eq_bv ? ignore fst_5 then
507        let address ≝ ADDR11 address in
508          Some ? ([ ACALL address ])
509      else
510        None ?
511    | long_jump ⇒
512      let address ≝ ADDR16 (lookup_labels call) in
513        Some ? [ LCALL address ]
514    ]
515  | Mov d trgt ⇒
516    let address ≝ DATA16 (lookup_datalabels trgt) in
517      Some ? [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))]
518  | Instruction instr ⇒ expand_relative_jump lookup_labels jmp_len pc instr
519  | Jmp jmp ⇒
520    match jmp_len with
521    [ short_jump ⇒
522      let lookup_address ≝ lookup_labels jmp in
523      let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in
524      let 〈upper, lower〉 ≝ split ? 8 8 result in
525      if eq_bv ? upper (zero 8) then
526        let address ≝ RELATIVE lower in
527          Some ? [ SJMP address ]
528      else
529        None ?
530    | medium_jump ⇒
531      let address ≝ lookup_labels jmp in
532      let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 address in
533      let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in
534      if eq_bv ? fst_5_addr fst_5_pc then
535        let address ≝ ADDR11 rest_addr in
536          Some ? ([ AJMP address ])
537      else
538        None ?
539    | long_jump ⇒
540      let address ≝ ADDR16 (lookup_labels jmp) in
541        Some ? [ LJMP address ]
542    ]
543  ].
544  @ I
545qed.
546
547(* label_map: identifier ↦ 〈instruction number, address〉 *)
548definition label_map ≝ identifier_map ASMTag (nat × nat).
549
550definition add_instruction_size: ℕ → jump_length → pseudo_instruction → ℕ ≝
551  λpc.λjmp_len.λinstr.
552  let bv_pc ≝ bitvector_of_nat 16 pc in
553  let ilist ≝ expand_pseudo_instruction_safe (λx.bv_pc) (λx.bv_pc) bv_pc jmp_len instr in
554  let bv: list (BitVector 8) ≝ match ilist with
555    [ None   ⇒ (* this shouldn't happen *) [ ]
556    | Some l ⇒ flatten … (map … assembly1 l)
557    ] in
558  pc + (|bv|).
559 
560definition is_label ≝
561  λx:labelled_instruction.λl:Identifier.
562  let 〈lbl,instr〉 ≝ x in
563  match lbl with
564  [ Some l' ⇒ l' = l
565  | _       ⇒ False
566  ].
567 
568lemma label_does_not_occur:
569  ∀i,p,l.
570  is_label (nth i ? p 〈None ?, Comment [ ]〉) l → does_not_occur l p = false.
571 #i #p #l generalize in match i; elim p
572 [ #i >nth_nil #H @⊥ @H
573 | #h #t #IH #i cases i -i
574   [ cases h #hi #hp cases hi
575     [ normalize #H @⊥ @H
576     | #l' #Heq whd in ⊢ (??%?); change with (eq_identifier ? l' l) in match (instruction_matches_identifier ??);
577       whd in Heq; >Heq
578       >eq_identifier_refl //
579     ]
580   | #i #H whd in match (does_not_occur ??);
581     whd in match (instruction_matches_identifier ??);
582     cases h #hi #hp cases hi normalize nodelta
583     [ @(IH i) @H
584     | #l' @eq_identifier_elim
585       [ normalize //
586       | normalize #_ @(IH i) @H
587       ]
588     ]
589   ]
590 ]
591qed. 
592
593lemma coerc_pair_sigma:
594 ∀A,B,P. ∀p:A × B. P (\snd p) → A × (Σx:B.P x).
595#A #B #P * #a #b #p % [@a | /2/]
596qed.
597coercion coerc_pair_sigma:∀A,B,P. ∀p:A × B. P (\snd p) → A × (Σx:B.P x)
598≝ coerc_pair_sigma on p: (? × ?) to (? × (Sig ??)).
599
600definition create_label_map: ∀program:list labelled_instruction.
601  ∀policy:jump_expansion_policy.
602  (Σlabels:label_map.
603    ∀i:ℕ.lt i (|program|) →
604    ∀l.occurs_exactly_once l program →
605    is_label (nth i ? program 〈None ?, Comment [ ]〉) l →
606    ∃a.lookup … labels l = Some ? 〈i,a〉
607  ) ≝
608  λprogram.λpolicy.
609  let 〈final_pc, final_labels〉 ≝
610    foldl_strong (option Identifier × pseudo_instruction)
611    (λprefix.ℕ × (Σlabels.
612      ∀i:ℕ.lt i (|prefix|) →
613      ∀l.occurs_exactly_once l prefix →
614      is_label (nth i ? prefix 〈None ?, Comment [ ]〉) l →
615      ∃a.lookup … labels l = Some ? 〈i,a〉)
616    )
617    program
618    (λprefix.λx.λtl.λprf.λacc.
619     let 〈pc,labels〉 ≝ acc in
620     let 〈label,instr〉 ≝ x in
621          let new_labels ≝
622          match label with
623          [ None   ⇒ labels
624          | Some l ⇒ add … labels l 〈|prefix|, pc〉
625          ] in
626          let jmp_len ≝ \snd (bvt_lookup ?? (bitvector_of_nat 16 (|prefix|)) policy 〈pc, long_jump〉) in
627          〈add_instruction_size pc jmp_len instr, new_labels〉
628    ) 〈0, empty_map …〉 in
629    final_labels.
630[ #i >append_length >commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi) -Hi;
631  [ #Hi #l normalize nodelta; cases label normalize nodelta
632    [ >occurs_exactly_once_None #Hocc >(nth_append_first ? ? prefix ? ? (le_S_S_to_le ? ? Hi)) #Hl
633      lapply (sig2 … labels) #Hacc elim (Hacc i (le_S_S_to_le … Hi) l Hocc Hl) #addr #Haddr 
634      % [ @addr | @Haddr ]
635    | #l' #Hocc #Hl lapply (occurs_exactly_once_Some_stronger … Hocc) -Hocc;
636      @eq_identifier_elim #Heq #Hocc
637      [ normalize in Hocc;
638        >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) in Hl; #Hl 
639        @⊥ @(absurd … Hocc)
640      | normalize nodelta lapply (sig2 … labels) #Hacc elim (Hacc i (le_S_S_to_le … Hi) l Hocc ?)
641        [ #addr #Haddr % [ @addr | <Haddr @lookup_add_miss /2/ ]
642        | >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) in Hl; //
643        ]
644      ]
645      >(label_does_not_occur i … Hl) normalize nodelta @nmk //
646    ]
647  | #Hi #l #Hocc >(injective_S … Hi) >nth_append_second
648    [ <minus_n_n #Hl normalize in Hl; normalize nodelta cases label in Hl;
649      [ normalize nodelta #H @⊥ @H
650      | #l' normalize nodelta #Heq % [ @pc | <Heq normalize nodelta @lookup_add_hit ]
651      ]
652    | @le_n
653    ]
654  ]
655| #i #Hi #l #Hl @⊥ @Hl
656]
657qed.
658
659definition select_reljump_length: label_map → ℕ → Identifier → jump_length ≝
660  λlabels.λpc.λlbl.
661  let 〈n, addr〉 ≝ lookup_def … labels lbl 〈0, pc〉 in
662  if leb pc addr (* forward jump *)
663  then if leb (addr - pc) 126
664       then short_jump
665       else long_jump
666  else if leb (pc - addr) 129
667       then short_jump
668       else long_jump.
669
670definition select_call_length: label_map → ℕ → Identifier → jump_length ≝
671  λlabels.λpc_nat.λlbl.
672  let pc ≝ bitvector_of_nat 16 pc_nat in
673  let addr ≝ bitvector_of_nat 16 (\snd (lookup_def ? ? labels lbl 〈0, pc_nat〉)) in
674  let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 addr in
675  let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in
676  if eq_bv ? fst_5_addr fst_5_pc
677  then medium_jump
678  else long_jump.
679 
680definition select_jump_length: label_map → ℕ → Identifier → jump_length ≝
681  λlabels.λpc.λlbl.
682  let 〈n, addr〉 ≝ lookup_def … labels lbl 〈0, pc〉 in
683  if leb pc addr
684  then if leb (addr - pc) 126
685       then short_jump
686       else select_call_length labels pc lbl
687  else if leb (pc - addr) 129
688       then short_jump
689       else select_call_length labels pc lbl.
690 
691definition jump_expansion_step_instruction: label_map → ℕ →
692  preinstruction Identifier → option jump_length ≝
693  λlabels.λpc.λi.
694  match i with
695  [ JC j     ⇒ Some ? (select_reljump_length labels pc j)
696  | JNC j    ⇒ Some ? (select_reljump_length labels pc j)
697  | JZ j     ⇒ Some ? (select_reljump_length labels pc j)
698  | JNZ j    ⇒ Some ? (select_reljump_length labels pc j)
699  | JB _ j   ⇒ Some ? (select_reljump_length labels pc j)
700  | JBC _ j  ⇒ Some ? (select_reljump_length labels pc j)
701  | JNB _ j  ⇒ Some ? (select_reljump_length labels pc j)
702  | CJNE _ j ⇒ Some ? (select_reljump_length labels pc j)
703  | DJNZ _ j ⇒ Some ? (select_reljump_length labels pc j)
704  | _        ⇒ None ?
705  ].
706
707definition max_length: jump_length → jump_length → jump_length ≝
708  λj1.λj2.
709  match j1 with
710  [ long_jump   ⇒ long_jump
711  | medium_jump ⇒
712    match j2 with
713    [ long_jump ⇒ long_jump
714    | _         ⇒ medium_jump
715    ]
716  | short_jump  ⇒ j2
717  ].
718
719definition jmple: jump_length → jump_length → Prop ≝
720  λj1.λj2.
721  match j1 with
722  [ short_jump  ⇒
723    match j2 with
724    [ short_jump ⇒ False
725    | _          ⇒ True
726    ]
727  | medium_jump ⇒
728    match j2 with
729    [ long_jump ⇒ True
730    | _         ⇒ False
731    ]
732  | long_jump   ⇒ False
733  ].
734
735definition jmpleq: jump_length → jump_length → Prop ≝
736  λj1.λj2.jmple j1 j2 ∨ j1 = j2.
737 
738lemma dec_jmple: ∀x,y:jump_length.jmple x y + ¬(jmple x y).
739 #x #y cases x cases y /3 by inl, inr, nmk, I/
740qed.
741 
742lemma jmpleq_max_length: ∀ol,nl.
743  jmpleq ol (max_length ol nl).
744 #ol #nl cases ol cases nl
745 /2 by or_introl, or_intror, I/
746qed.
747 
748definition is_jump' ≝
749  λx:preinstruction Identifier.
750  match x with
751  [ JC _ ⇒ True
752  | JNC _ ⇒ True
753  | JZ _ ⇒ True
754  | JNZ _ ⇒ True
755  | JB _ _ ⇒ True
756  | JNB _ _ ⇒ True
757  | JBC _ _ ⇒ True
758  | CJNE _ _ ⇒ True
759  | DJNZ _ _ ⇒ True
760  | _ ⇒ False
761  ].
762 
763definition is_jump ≝
764  λx:labelled_instruction.
765  let 〈label,instr〉 ≝ x in
766  match instr with
767  [ Instruction i   ⇒ is_jump' i
768  | Call _ ⇒ True
769  | Jmp _ ⇒ True
770  | _ ⇒ False
771  ].
772
773definition jump_in_policy ≝
774  λprefix:list labelled_instruction.λpolicy:jump_expansion_policy.
775  ∀i:ℕ.i < |prefix| →
776  (is_jump (nth i ? prefix 〈None ?, Comment []〉) ↔
777   ∃p,j.lookup_opt … (bitvector_of_nat 16 i) policy = Some ? 〈p,j〉).
778 
779lemma le_S_to_le: ∀n,m:ℕ.S n ≤ m → n ≤ m.
780 /2/ qed.
781 
782lemma bitvector_of_nat_ok:
783  ∀n,x,y:ℕ.x < 2^n → y < 2^n → eq_bv n (bitvector_of_nat n x) (bitvector_of_nat n y) → x = y.
784 #n elim n -n
785 [ #x #y #Hx #Hy #Heq <(le_n_O_to_eq ? (le_S_S_to_le ?? Hx)) <(le_n_O_to_eq ? (le_S_S_to_le ?? Hy)) @refl
786 | #n #Hind #x #y #Hx #Hy #Heq cases daemon (* XXX *)
787 ]
788qed.
789
790lemma bitvector_of_nat_abs:
791  ∀n,x,y:ℕ.x < 2^n → y < 2^n → x ≠y → ¬eq_bv n (bitvector_of_nat n x) (bitvector_of_nat n y).
792 #n #x #y #Hx #Hy #Heq @notb_elim
793 lapply (refl ? (eq_bv ? (bitvector_of_nat n x) (bitvector_of_nat n y)))
794 cases (eq_bv ? (bitvector_of_nat n x) (bitvector_of_nat n y)) in ⊢ (???% → %);
795 [ #H @⊥ @(absurd ?? Heq) @(bitvector_of_nat_ok n x y Hx Hy) >H //
796 | #H //
797 ]
798qed.
799
800
801lemma jump_not_in_policy: ∀prefix:list labelled_instruction.
802 ∀policy:(Σp:jump_expansion_policy.
803 (∀i.i ≥ |prefix| → i < 2^16 → lookup_opt … (bitvector_of_nat ? i) p = None ?) ∧
804 jump_in_policy prefix p).
805  ∀i:ℕ.i < |prefix| →
806  ¬is_jump (nth i ? prefix 〈None ?, Comment []〉) ↔
807  lookup_opt … (bitvector_of_nat 16 i) policy = None ?.
808 #prefix #policy #i #Hi @conj
809 [ #Hnotjmp lapply (refl ? (lookup_opt … (bitvector_of_nat 16 i) policy))
810   cases (lookup_opt … (bitvector_of_nat 16 i) policy) in ⊢ (???% → ?);
811   [ #H @H
812   | #x cases x #y #z #H @⊥ @(absurd ? ? Hnotjmp) @(proj2 ?? (proj2 ?? (sig2 ?? policy) i Hi))
813     @(ex_intro … y (ex_intro … z H))
814   ]
815 | #Hnone @nmk #Hj lapply (proj1 ?? (proj2 ?? (sig2 ?? policy) i Hi) Hj)
816   #H elim H -H; #x #H elim H -H; #y #H >H in Hnone; #abs destruct (abs)
817 ] 
818qed.
819 
820definition jump_expansion_start:
821  ∀program:(Σl:list labelled_instruction.|l| < 2^16).
822  Σpolicy:jump_expansion_policy.
823    (∀i.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
824    jump_in_policy program policy ∧
825    ∀i.i < |program| → is_jump (nth i ? program 〈None ?, Comment []〉) →
826     lookup_opt … (bitvector_of_nat 16 i) policy = Some ? 〈0,short_jump〉 ≝
827  λprogram.
828  foldl_strong (option Identifier × pseudo_instruction)
829  (λprefix.Σpolicy:jump_expansion_policy.
830    (∀i.i ≥ |prefix| → i < 2^16 → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
831    jump_in_policy prefix policy ∧
832    ∀i.i < |prefix| → is_jump (nth i ? prefix 〈None ?, Comment []〉) →
833      lookup_opt … (bitvector_of_nat 16 i) policy = Some ? 〈0,short_jump〉)
834  program
835  (λprefix.λx.λtl.λprf.λpolicy.
836   let 〈label,instr〉 ≝ x in
837   match instr with
838   [ Instruction i ⇒ match i with
839     [ JC _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
840     | JNC _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
841     | JZ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
842     | JNZ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
843     | JB _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
844     | JNB _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
845     | JBC _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
846     | CJNE _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
847     | DJNZ _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
848     | _ ⇒ (eject … policy)
849     ]
850   | Call c ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
851   | Jmp j  ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
852   | _      ⇒ (eject … policy)
853   ]
854  ) (Stub ? ?).
855[1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,35,36,37,38,39,40,41,42:
856 @conj
857 [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,57,59,61:
858  @conj
859  #i >append_length <commutative_plus #Hi normalize in Hi;
860  [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,57,59,61:
861   #Hi2 cases (le_to_or_lt_eq … Hi) -Hi; #Hi @(proj1 ?? (proj1 ?? (sig2 ?? policy)) i)
862   [1,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77,81,85,89,93,97,101,105,109,113,117,121:
863     @le_S_to_le @le_S_to_le @Hi
864   |2,6,10,14,18,22,26,30,34,38,42,46,50,54,58,62,66,70,74,78,82,86,90,94,98,102,106,110,114,118,122:
865     @Hi2
866   |3,7,11,15,19,23,27,31,35,39,43,47,51,55,59,63,67,71,75,79,83,87,91,95,99,103,107,111,115,119,123:
867     <Hi @le_n_Sn
868   |4,8,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,112,116,120,124:
869     @Hi2
870   ]
871  ]
872  cases (le_to_or_lt_eq … Hi) -Hi; #Hi
873  [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,57,59,61:
874    >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi))
875    @(proj2 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
876  ]
877  @conj >(injective_S … Hi)
878   [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,57,59,61:
879    >(nth_append_second ? ? prefix ? ? (le_n (|prefix|))) <minus_n_n #H @⊥ @H
880   ]
881   #H elim H; -H; #t1 #H elim H; -H #t2 #H
882   >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)) ?) in H;
883   [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,57,59,61:
884     #H destruct (H)
885   ]
886   @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm @le_S_S
887   @le_plus_n_r
888 ]
889 #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi)
890 -Hi; #Hi
891 [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,57,59,61:
892  #Hj @(proj2 ?? (sig2 ?? policy) i (le_S_S_to_le … Hi))
893  >(nth_append_first ?? prefix ?? (le_S_S_to_le ?? Hi)) in Hj; //
894 ]
895 >(injective_S … Hi) >(nth_append_second ?? prefix ?? (le_n (|prefix|))) <minus_n_n
896 #H @⊥ @H
897|2,3,26,27,28,29,30,31,32,33,34: @conj
898 [1,3,5,7,9,11,13,15,17,19,21: @conj
899  [1,3,5,7,9,11,13,15,17,19,21:
900    #i >append_length <commutative_plus #Hi #Hi2 normalize in Hi; >lookup_opt_insert_miss
901   [1,3,5,7,9,11,13,15,17,19,21:
902     @(proj1 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_to_le … Hi) Hi2)
903   ]
904   >eq_bv_sym @bitvector_of_nat_abs
905   [1,4,7,10,13,16,19,22,25,28,31:
906     @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm @le_S_S
907     @le_plus_n_r
908   |2,5,8,11,14,17,20,23,26,29,32: @Hi2
909   |3,6,9,12,15,18,21,24,27,30,33: @lt_to_not_eq @Hi
910   ]
911  ]
912  #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi)
913  -Hi #Hi
914  [1,3,5,7,9,11,13,15,17,19,21:
915   >(nth_append_first ?? prefix ?? (le_S_S_to_le … Hi)) >lookup_opt_insert_miss
916   [1,3,5,7,9,11,13,15,17,19,21:
917    @(proj2 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
918   ]
919   @bitvector_of_nat_abs
920   [3,6,9,12,15,18,21,24,27,30,33: @(lt_to_not_eq … (le_S_S_to_le … Hi))
921   |1,4,7,10,13,16,19,22,25,28,31: @(transitive_lt ??? (le_S_S_to_le ?? Hi))
922   ]
923   @(transitive_lt … (sig2 ?? program))
924   >prf >append_length normalize <plus_n_Sm @le_S_S @le_plus_n_r
925  ]
926  @conj >(injective_S … Hi) #H
927  [2,4,6,8,10,12,14,16,18,20,22:
928   >(nth_append_second ?? prefix ?? (le_n (|prefix|))) <minus_n_n //
929  ]
930  @(ex_intro ?? 0 (ex_intro ?? short_jump (lookup_opt_insert_hit ?? 16 ? policy)))
931 ]
932 #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi)
933  -Hi #Hi
934 [1,3,5,7,9,11,13,15,17,19,21:
935  >(nth_append_first ?? prefix ?? (le_S_S_to_le … Hi)) #Hj >lookup_opt_insert_miss
936  [1,3,5,7,9,11,13,15,17,19,21:
937   @(proj2 ?? (sig2 ?? policy) i (le_S_S_to_le … Hi) Hj)
938  ]
939  @bitvector_of_nat_abs
940  [3,6,9,12,15,18,21,24,27,30,33: @(lt_to_not_eq … (le_S_S_to_le … Hi))
941  |1,4,7,10,13,16,19,22,25,28,31: @(transitive_lt ??? (le_S_S_to_le ?? Hi))
942  ]
943  @(transitive_lt … (sig2 ?? program))
944  >prf >append_length normalize <plus_n_Sm @le_S_S @le_plus_n_r
945 ]
946 #_ >(injective_S … Hi) @lookup_opt_insert_hit
947|@conj
948 [@conj
949  [ #i #Hi //
950  | whd #i #Hi @⊥ @(absurd (i < 0) Hi (not_le_Sn_O ?))
951  ]
952 | #i #Hi >nth_nil #Hj @⊥ @Hj
953]
954qed.
955
956definition policy_increase: list labelled_instruction → jump_expansion_policy →
957  jump_expansion_policy → Prop ≝
958 λprogram.λop.λp.
959  (∀i:ℕ.i < |program| →
960    jmpleq
961      (\snd (bvt_lookup … (bitvector_of_nat ? i) op 〈0,short_jump〉))
962      (\snd (bvt_lookup … (bitvector_of_nat ? i) p 〈0,short_jump〉))).
963   
964definition jump_expansion_step: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
965  ∀old_policy:(Σpolicy.
966    (∀i.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
967    jump_in_policy program policy).
968  (Σpolicy.
969    (∀i.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
970    jump_in_policy program policy ∧
971    policy_increase program old_policy policy)
972    ≝
973  λprogram.λold_policy.
974  let labels ≝ create_label_map program old_policy in
975  let 〈final_pc, final_policy〉 ≝
976    foldl_strong (option Identifier × pseudo_instruction)
977    (λprefix.ℕ × Σpolicy.
978      (∀i.i ≥ |prefix| → i < 2^16 → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
979      jump_in_policy prefix policy ∧
980      policy_increase prefix old_policy policy
981    )
982    program
983    (λprefix.λx.λtl.λprf.λacc.
984      let 〈pc, policy〉 ≝ acc in
985      let 〈label,instr〉 ≝ x in
986      let old_jump_length ≝ lookup_opt ? ? (bitvector_of_nat 16 (|prefix|)) old_policy in
987      let add_instr ≝
988        match instr with
989        [ Instruction i ⇒ jump_expansion_step_instruction labels pc i
990        | Call c        ⇒ Some ? (select_call_length labels pc c)
991        | Jmp j         ⇒ Some ? (select_jump_length labels pc j)
992        | _             ⇒ None ?
993        ] in
994      let 〈new_pc, new_policy〉 ≝
995        let 〈ignore,old_length〉 ≝ lookup … (bitvector_of_nat 16 (|prefix|)) old_policy 〈0, short_jump〉 in
996        match add_instr with
997        [ None    ⇒ (* i.e. it's not a jump *)
998          〈add_instruction_size pc long_jump instr, policy〉
999        | Some ai ⇒
1000          let new_length ≝ max_length old_length ai in
1001          〈add_instruction_size pc new_length instr, insert … (bitvector_of_nat 16 (|prefix|)) 〈pc, new_length〉 policy〉
1002        ] in
1003      〈new_pc, new_policy〉
1004    ) 〈0, Stub ? ?〉 in
1005    final_policy.
1006[ @conj [ @conj #i >append_length <commutative_plus #Hi normalize in Hi;
1007[ #Hi2 cases (lookup ??? old_policy ?) #h #n cases add_instr
1008  [ @(proj1 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_to_le … Hi) Hi2)
1009  | #z normalize nodelta >lookup_opt_insert_miss
1010    [ @(proj1 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_to_le … Hi) Hi2)
1011    | >eq_bv_sym @bitvector_of_nat_abs
1012      [ @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1013        @le_S_S @le_plus_n_r
1014      | @Hi2
1015      | @lt_to_not_eq @Hi
1016      ]
1017    ]
1018  ]
1019| cases (le_to_or_lt_eq … Hi) -Hi;
1020  [ #Hi; >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) @conj
1021    [ #Hj lapply (proj2 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi)) #Hacc
1022      cases add_instr cases (lookup ??? old_policy ?) normalize nodelta #x #y
1023      [ @(proj1 ?? Hacc Hj)
1024      | #z elim (proj1 ?? Hacc Hj) #h #n elim n -n #n #Hn
1025        % [ @h | % [ @n | <Hn @lookup_opt_insert_miss @bitvector_of_nat_abs
1026        [3: @(lt_to_not_eq i (|prefix|)) @(le_S_S_to_le … Hi)
1027        |1: @(transitive_lt ??? (le_S_S_to_le ?? Hi))
1028        ]
1029        @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1030        @le_S_S @le_plus_n_r
1031        ] ]
1032      ]
1033    | lapply (proj2 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi)) #Hacc
1034      #H elim H -H; #h #H elim H -H; #n cases add_instr cases (lookup ??? old_policy ?)
1035      normalize nodelta #x #y [2: #z]
1036      #Hl @(proj2 ?? Hacc) @(ex_intro ?? h (ex_intro ?? n ?))
1037      [ <Hl @sym_eq @lookup_opt_insert_miss @bitvector_of_nat_abs
1038        [3: @lt_to_not_eq @(le_S_S_to_le … Hi)
1039        |1: @(transitive_lt ??? (le_S_S_to_le ?? Hi))
1040        ]
1041        @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1042        @le_S_S @le_plus_n_r
1043      | @Hl
1044      ]
1045    ]
1046  | #Hi >(injective_S … Hi) >(nth_append_second ? ? prefix ? ? (le_n (|prefix|)))
1047     <minus_n_n whd in match (nth ????); whd in match (add_instr); cases instr
1048     [1: #pi | 2,3: #x | 4,5: #id | 6: #x #y] @conj normalize nodelta
1049     [3,5,11: #H @⊥ @H (* instr is not a jump *)
1050     |4,6,12: #H elim H -H; #h #H elim H -H #n cases (lookup ??? old_policy ?)
1051       #x #y normalize nodelta >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)) ?)
1052       [1,3,5: #H destruct (H)]
1053       @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1054       @le_S_S @le_plus_n_r
1055     |7,9: (* instr is a jump *) #_ cases (lookup ??? old_policy ?) #h #n
1056       whd in match (snd ???); @(ex_intro ?? pc)
1057       [ @(ex_intro ?? (max_length n (select_jump_length (create_label_map program old_policy) pc id)))
1058       | @(ex_intro ?? (max_length n (select_call_length (create_label_map program old_policy) pc id)))
1059       ] @lookup_opt_insert_hit
1060     |8,10: #_ //
1061     |1,2: cases pi
1062       [35,36,37: #H @⊥ @H
1063       |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #H @⊥ @H
1064       |1,2,3,6,7,33,34: #x #y #H @⊥ @H
1065       |9,10,14,15: #id #_ cases (lookup ??? old_policy ?) #h #n
1066         whd in match (snd ???);
1067         @(ex_intro ?? pc (ex_intro ?? (max_length n (select_reljump_length (create_label_map program old_policy) pc id)) ?))
1068         @lookup_opt_insert_hit
1069       |11,12,13,16,17: #x #id #_ cases (lookup ??? old_policy ?) #h #n
1070         whd in match (snd ???);
1071         @(ex_intro ?? pc (ex_intro ?? (max_length n (select_reljump_length (create_label_map program old_policy) pc id)) ?))
1072         @lookup_opt_insert_hit
1073       |72,73,74: #H elim H -H; #h #H elim H -H #n cases (lookup ??? old_policy ?)
1074        #x #y normalize nodelta
1075        >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)) ?)
1076        [1,3,5: #H destruct (H)]
1077        @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1078        @le_S_S @le_plus_n_r     
1079       |41,42,45,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69: #x
1080        #H elim H -H; #h #H elim H -H #n cases (lookup ??? old_policy ?)
1081        #x #y normalize nodelta
1082        >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)) ?)
1083        [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35: #H destruct (H)]
1084        @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1085        @le_S_S @le_plus_n_r
1086       |38,39,40,43,44,70,71: #x #y #H elim H -H; #h #H elim H -H #n
1087        cases (lookup ??? old_policy ?) #x #y normalize nodelta
1088        >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)) ?)
1089        [1,3,5,7,9,11,13: #H destruct (H)]
1090        @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1091        @le_S_S @le_plus_n_r             
1092       |46,47,51,52: #id #_ //
1093       |48,49,50,53,54: #x #id #_ //
1094       ]
1095     ]
1096   ]
1097  ]
1098| lapply (refl ? add_instr) cases add_instr in ⊢ (???% → %);
1099  [ #Ha #i >append_length >commutative_plus #Hi normalize in Hi;
1100    cases (le_to_or_lt_eq … Hi) -Hi; #Hi
1101    [ cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)
1102      #x #y @((proj2 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
1103    | normalize nodelta >(injective_S … Hi)
1104      >lookup_opt_lookup_miss
1105      [ >lookup_opt_lookup_miss
1106        [ //
1107        | cases (lookup ?? (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)
1108          #x #y normalize nodelta
1109          >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)) ?)
1110          [ //
1111          | @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1112            @le_S_S @le_plus_n_r
1113          ]
1114        ]
1115      | >(proj1 ?? (jump_not_in_policy (eject … program) old_policy (|prefix|) ?))
1116        [ //
1117        | >prf >p1 >nth_append_second [ <minus_n_n
1118        generalize in match Ha; normalize nodelta cases instr normalize nodelta
1119        [1: #pi cases pi
1120         [1,2,3,6,7,33,34: #x #y #H normalize /2 by nmk/
1121         |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #H normalize /2 by nmk/
1122         |35,36,37: #H normalize /2 by nmk/
1123         |9,10,14,15: #id whd in match (jump_expansion_step_instruction ???);
1124           #H destruct (H)
1125         |11,12,13,16,17: #x #id whd in match (jump_expansion_step_instruction ???);
1126           #H destruct (H)
1127         ]
1128        |2,3: #x #H normalize /2 by nmk/
1129        |6: #x #y #H normalize /2 by nmk/
1130        |4,5: #id #H destruct (H)
1131        ] | @le_n ]
1132        | >prf >append_length normalize <plus_n_Sm @le_plus_n_r
1133        ]
1134      ]
1135    ]
1136  | #x #Ha #i >append_length >commutative_plus #Hi normalize in Hi;
1137    cases (le_to_or_lt_eq … Hi) -Hi; #Hi
1138    [ cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)
1139      #y #z normalize nodelta normalize nodelta >lookup_insert_miss
1140      [ @((proj2 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
1141      | @bitvector_of_nat_abs
1142        [3: @lt_to_not_eq @(le_S_S_to_le … Hi)
1143        |1: @(transitive_lt ??? (le_S_S_to_le … Hi))
1144        ]
1145        @(transitive_lt … (sig2 ?? program)) >prf >append_length normalize <plus_n_Sm
1146        @le_S_S @le_plus_n_r
1147      ]
1148    | >(injective_S … Hi) elim (proj1 ?? (proj2 ?? (sig2 ?? old_policy) (|prefix|) ?) ?)
1149      [ #a #H elim H -H; #b #H >H >(lookup_opt_lookup_hit … 〈a,b〉 H)
1150        normalize nodelta >lookup_insert_hit @jmpleq_max_length
1151      | >prf >p1 >nth_append_second
1152        [ <minus_n_n generalize in match Ha; normalize nodelta cases instr normalize nodelta
1153          [1: #pi cases pi
1154           [1,2,3,6,7,33,34: #x #y #H normalize in H; destruct (H)
1155           |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #H normalize in H; destruct (H)
1156           |35,36,37: #H normalize in H; destruct (H)
1157           |9,10,14,15: #id #H //
1158           |11,12,13,16,17: #x #id #H //
1159           ]
1160          |2,3: #x #H normalize in H; destruct (H)
1161          |6: #x #y #H normalize in H; destruct (H)
1162          |4,5: #id #H //
1163          ]
1164        | @le_n ]
1165      | >prf >append_length normalize <plus_n_Sm @le_plus_n_r
1166      ]
1167    ]
1168  ] ]
1169| @conj [ @conj
1170  [ #i #Hi //
1171  | #i #Hi @conj [ >nth_nil #H @⊥ @H | #H elim H #x #H1 elim H1 #y #H2
1172                   normalize in H2; destruct (H2) ]
1173  ]                 
1174  | #i #Hi @⊥ @(absurd (i<0)) [ @Hi | @(not_le_Sn_O) ]
1175]
1176qed.
1177 
1178let rec jump_expansion_internal (program: Σl:list labelled_instruction.|l| < 2^16)
1179  (n: ℕ) on n: (Σpolicy:jump_expansion_policy.
1180    And
1181    (∀i:ℕ.i ≥ |program| → i < 2^16 → lookup_opt ? 16 (bitvector_of_nat ? i) policy = None ?)
1182    (jump_in_policy program policy)) ≝
1183  match n with
1184  [ O   ⇒ jump_expansion_start program
1185  | S m ⇒ jump_expansion_step program (jump_expansion_internal program m)
1186  ].
1187[ @(proj1 ?? (sig2 ?? (jump_expansion_start program)))
1188| @(proj1 ?? (sig2 ?? (jump_expansion_step program (jump_expansion_internal program m))))
1189]
1190qed.
1191
1192definition policy_equal ≝
1193  λprogram:list labelled_instruction.λp1,p2:jump_expansion_policy.
1194  ∀n:ℕ.n < |program| →
1195    (\snd (bvt_lookup … (bitvector_of_nat 16 n) p1 〈0,short_jump〉)) =
1196    (\snd (bvt_lookup … (bitvector_of_nat 16 n) p2 〈0,short_jump〉)).
1197
1198lemma pe_refl:
1199  ∀program.reflexive ? (policy_equal program).
1200 #program whd #x whd #n #Hn @refl
1201qed.
1202
1203lemma pe_sym:
1204  ∀program.symmetric ? (policy_equal program).
1205 #program whd #x #y #Hxy whd #n #Hn
1206 >(Hxy n Hn) @refl
1207qed.
1208
1209lemma pe_trans:
1210  ∀program.transitive ? (policy_equal program).
1211 #program whd #x #y #z #Hxy #Hyz whd #n #Hn
1212 >(Hxy n Hn) @(Hyz n Hn)
1213qed.
1214
1215lemma le_plus:
1216  ∀n,m:ℕ.n ≤ m → ∃k:ℕ.m = n + k.
1217 #n #m elim m -m;
1218 [ #Hn % [ @O | <(le_n_O_to_eq n Hn) // ]
1219 | #m #Hind #Hn cases (le_to_or_lt_eq … Hn) -Hn; #Hn
1220   [ elim (Hind (le_S_S_to_le … Hn)) #k #Hk % [ @(S k) | >Hk // ]
1221   | % [ @O | <Hn // ]
1222   ]
1223 ]
1224qed.
1225
1226theorem plus_Sn_m1: ∀n,m:nat. S m + n = m + S n.
1227#n (elim n) normalize /2 by S_pred/ qed.
1228
1229lemma pe_step: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
1230 ∀p1,p2:Σpolicy.
1231 (∀i:ℕ.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat ? i) policy = None ?)
1232 ∧jump_in_policy program policy.
1233  policy_equal program p1 p2 →
1234  policy_equal program (jump_expansion_step program p1) (jump_expansion_step program p2).
1235 #program #p1 #p2 #Heq whd #n #Hn lapply (Heq n Hn) #H
1236 lapply (refl ? (lookup_opt … (bitvector_of_nat ? n) p1))
1237 cases (lookup_opt … (bitvector_of_nat ? n) p1) in ⊢ (???% → ?);
1238 [ #Hl lapply ((proj2 ?? (jump_not_in_policy program p1 n Hn)) Hl)
1239   #Hnotjmp >lookup_opt_lookup_miss
1240   [ >lookup_opt_lookup_miss
1241     [ @refl
1242     | @(proj1 ?? (jump_not_in_policy program (eject … (jump_expansion_step program p2)) n Hn))
1243       [ @(proj1 ?? (sig2 … (jump_expansion_step program p2)))
1244       | @Hnotjmp
1245       ]
1246     ]
1247   | @(proj1 ?? (jump_not_in_policy program (eject … (jump_expansion_step program p1)) n Hn))
1248     [ @(proj1 ?? (sig2 ?? (jump_expansion_step program p1)))
1249     | @Hnotjmp
1250     ]
1251   ]
1252 | #x #Hl cases daemon
1253 ]
1254qed.
1255   
1256lemma equal_remains_equal: ∀program:(Σl:list labelled_instruction.|l| < 2^16).∀n:ℕ.
1257  policy_equal program (jump_expansion_internal program n) (jump_expansion_internal program (S n)) →
1258  ∀k.k ≥ n → policy_equal program (jump_expansion_internal program n) (jump_expansion_internal program k).
1259 #program #n #Heq #k #Hk elim (le_plus … Hk); #z #H >H -H -Hk -k;
1260 lapply Heq -Heq; lapply n -n; elim z -z;
1261 [ #n #Heq <plus_n_O @pe_refl 
1262 | #z #Hind #n #Heq <plus_Sn_m1 whd in match (plus (S n) z); @(pe_trans … (jump_expansion_internal program (S n)))
1263   [ @Heq
1264   | @pe_step @Hind @Heq
1265   ]
1266 ]
1267qed.
1268
1269lemma dec_bounded_forall:
1270  ∀P:ℕ → Prop.(∀n.(P n) + (¬P n)) → ∀k.(∀n.n < k → P n) + ¬(∀n.n < k → P n).
1271 #P #HP_dec #k elim k -k
1272 [ %1 #n #Hn @⊥ @(absurd (n < 0) Hn) @not_le_Sn_O
1273 | #k #Hind cases Hind
1274   [ #Hk cases (HP_dec k)
1275     [ #HPk %1 #n #Hn cases (le_to_or_lt_eq … Hn)
1276       [ #H @(Hk … (le_S_S_to_le … H))
1277       | #H >(injective_S … H) @HPk
1278       ]
1279     | #HPk %2 @nmk #Habs @(absurd (P k)) [ @(Habs … (le_n (S k))) | @HPk ]
1280     ]
1281   | #Hk %2 @nmk #Habs @(absurd (∀n.n<k→P n)) [ #n' #Hn' @(Habs … (le_S … Hn')) | @Hk ]
1282   ]
1283 ]
1284qed.
1285
1286lemma dec_bounded_exists:
1287  ∀P:ℕ→Prop.(∀n.(P n) + (¬P n)) → ∀k.(∃n.n < k ∧ P n) + ¬(∃n.n < k ∧ P n).
1288 #P #HP_dec #k elim k -k
1289 [ %2 @nmk #Habs elim Habs #n #Hn @(absurd (n < 0) (proj1 … Hn)) @not_le_Sn_O
1290 | #k #Hind cases Hind
1291   [ #Hk %1 elim Hk #n #Hn @(ex_intro … n) @conj [ @le_S @(proj1 … Hn) | @(proj2 … Hn) ]
1292   | #Hk cases (HP_dec k)
1293     [ #HPk %1 @(ex_intro … k) @conj [ @le_n | @HPk ]
1294     | #HPk %2 @nmk #Habs elim Habs #n #Hn cases (le_to_or_lt_eq … (proj1 … Hn))
1295       [ #H @(absurd (∃n.n < k ∧ P n)) [ @(ex_intro … n) @conj
1296         [ @(le_S_S_to_le … H) | @(proj2 … Hn) ] | @Hk ]
1297       | #H @(absurd (P k)) [ <(injective_S … H) @(proj2 … Hn) | @HPk ]
1298       ] 
1299     ]
1300   ]
1301 ]
1302qed.
1303
1304lemma not_exists_forall:
1305  ∀k:ℕ.∀P:ℕ → Prop.¬(∃x.x < k ∧ P x) → ∀x.x < k → ¬P x.
1306 #k #P #Hex #x #Hx @nmk #Habs @(absurd ? ? Hex) @(ex_intro … x)
1307 @conj [ @Hx | @Habs ]
1308qed.
1309
1310lemma not_forall_exists:
1311  ∀k:ℕ.∀P:ℕ → Prop.(∀n.(P n) + (¬P n)) → ¬(∀x.x < k → P x) → ∃x.x < k ∧ ¬P x.
1312 #k #P #Hdec elim k
1313 [ #Hfa @⊥ @(absurd ?? Hfa) #z #Hz @⊥ @(absurd ? Hz) @not_le_Sn_O
1314 | -k #k #Hind #Hfa cases (Hdec k)
1315   [ #HP elim (Hind ?)
1316     [ -Hind; #x #Hx @(ex_intro ?? x) @conj [ @le_S @(proj1 ?? Hx) | @(proj2 ?? Hx) ]
1317     | @nmk #H @(absurd ?? Hfa) #x #Hx cases (le_to_or_lt_eq ?? Hx)
1318       [ #H2 @H @(le_S_S_to_le … H2)
1319       | #H2 >(injective_S … H2) @HP
1320       ]
1321     ]
1322   | #HP @(ex_intro … k) @conj [ @le_n | @HP ]
1323   ]
1324 ]
1325qed.
1326
1327lemma thingie:
1328  ∀A:Prop.(A + ¬A) → (¬¬A) → A.
1329 #A #Adec #nnA cases Adec
1330 [ //
1331 | #H @⊥ @(absurd (¬A) H nnA)
1332 ]
1333qed.
1334 
1335lemma dec_eq_jump_length: ∀a,b:jump_length.(a = b) + (a ≠ b).
1336  #a #b cases a cases b /2/
1337  %2 @nmk #H destruct (H)
1338qed.
1339
1340lemma policy_not_equal_incr: ∀program:(Σl:list labelled_instruction.|l|<2^16).
1341 ∀policy:(Σp:jump_expansion_policy.
1342    (∀i.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat ? i) p = None ?) ∧
1343    jump_in_policy program p).
1344  ¬policy_equal program policy (jump_expansion_step program policy) →
1345  ∃n:ℕ.n < (|program|) ∧ jmple
1346    (\snd (bvt_lookup … (bitvector_of_nat ? n) policy 〈0,short_jump〉))
1347    (\snd (bvt_lookup … (bitvector_of_nat ? n) (jump_expansion_step program policy) 〈0,short_jump〉)).
1348 #program #policy #Hp
1349 cases (dec_bounded_exists (λn.jmple
1350   (\snd (bvt_lookup ?? (bitvector_of_nat ? n) policy 〈0,short_jump〉))
1351   (\snd (bvt_lookup ?? (bitvector_of_nat ? n) (jump_expansion_step program policy) 〈0,short_jump〉))) ? (|program|))
1352 [ #H elim H; -H #i #Hi @(ex_intro ?? i) @Hi
1353 | #abs @⊥ @(absurd ?? Hp) #n #Hn cases (proj2 ?? (sig2 ?? (jump_expansion_step program policy)) n Hn)
1354   [ #Hj @⊥ @(absurd ?? abs) @(ex_intro ?? n) @conj [ @Hn | @Hj ]
1355   | #H @H
1356   ]
1357 | #n @dec_jmple
1358 ]
1359qed.
1360
1361lemma stupid:
1362  ∀program,n.
1363  eject … (jump_expansion_step program (jump_expansion_internal program n)) =
1364  eject … (jump_expansion_internal program (S n)).
1365 //
1366qed.
1367
1368let rec measure_int (program: list labelled_instruction) (policy: jump_expansion_policy) (acc: ℕ)
1369 on program: ℕ ≝
1370 match program with
1371 [ nil      ⇒ acc
1372 | cons h t ⇒ match (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉)) with
1373   [ long_jump   ⇒ measure_int t policy (acc + 2)
1374   | medium_jump ⇒ measure_int t policy (acc + 1)
1375   | _           ⇒ measure_int t policy acc
1376   ]
1377 ].
1378
1379definition measure ≝
1380  λprogram.λpolicy.measure_int program policy 0.
1381 
1382lemma measure_plus: ∀program.∀policy.∀x,d:ℕ.
1383  measure_int program policy (x+d) = measure_int program policy x + d.
1384 #program #policy #x #d generalize in match x; -x elim d
1385 [ //
1386 | -d; #d #Hind elim program
1387   [ //
1388   | #h #t #Hd #x whd in match (measure_int ???); whd in match (measure_int ?? x);
1389     cases (\snd (lookup … (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉))
1390     [ normalize nodelta @Hd
1391     |2,3: normalize nodelta >associative_plus >(commutative_plus (S d) ?) <associative_plus
1392       @Hd
1393     ]
1394   ]
1395 ]
1396qed.
1397   
1398lemma measure_incr_or_equal: ∀program:Σl:list labelled_instruction.|l|<2^16.
1399  ∀policy:Σp:jump_expansion_policy.
1400    (∀i.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat ? i) p = None ?) ∧
1401    jump_in_policy program p.∀l.|l| ≤ |program| → ∀acc:ℕ.
1402  measure_int l policy acc ≤ measure_int l (jump_expansion_step program policy) acc.
1403#program #policy #l elim l -l;
1404  [ #Hp #acc normalize @le_n
1405  | #h #t #Hind #Hp #acc
1406    cases (proj2 ?? (sig2 ?? (jump_expansion_step program policy)) (|t|) ?)
1407    [ whd in match (measure_int ???); whd in match (measure_int ?(jump_expansion_step ??)?);
1408      cases (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉))
1409      cases (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (jump_expansion_step program policy) 〈0,short_jump〉))
1410      [1,4,5,7,8,9: #H @⊥ @H
1411      |2,3,6: #_ normalize nodelta
1412        [1,2: @(transitive_le ? (measure_int t (jump_expansion_step program policy) acc))
1413        |3: @(transitive_le ? (measure_int t (jump_expansion_step program policy) (acc+1)))
1414        ]
1415        [1,3,5: @Hind @(transitive_le … Hp) @le_n_Sn
1416        |2,4,6: >measure_plus [1,2: @le_plus_n_r] >measure_plus @le_plus [ @le_n | //]
1417        ]
1418      ]
1419    | #Heq whd in match (measure_int ???); whd in match (measure_int ?(jump_expansion_step ??)?);
1420      >Heq cases (\snd (lookup … (bitvector_of_nat ? (|t|)) ? 〈0,short_jump〉))
1421      [ normalize nodelta @Hind @(transitive_le … Hp) @le_n_Sn
1422      | normalize nodelta @Hind @(transitive_le … Hp) @le_n_Sn
1423      | normalize nodelta @Hind @(transitive_le … Hp) @le_n_Sn
1424      ]
1425    | @Hp
1426    ]
1427  ]
1428qed.
1429
1430lemma measure_le: ∀program.∀policy.
1431  measure_int program policy 0 ≤ 2*|program|.
1432 #program #policy elim program
1433 [ normalize @le_n
1434 | #h #t #Hind whd in match (measure_int ???);
1435   cases (\snd (lookup ?? (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉))
1436   [ normalize nodelta @(transitive_le ??? Hind) /2 by monotonic_le_times_r/
1437   |2,3: normalize nodelta >measure_plus <times_n_Sm >(commutative_plus 2 ?)
1438     @le_plus [1,3: @Hind |2,4: // ]
1439   ]
1440 ]
1441qed.
1442
1443lemma bla: ∀a,b:ℕ.a + a = b + b → a = b.
1444 #a elim a
1445 [ normalize #b //
1446 | -a #a #Hind #b cases b [ /2 by le_n_O_to_eq/ | -b #b normalize
1447   <plus_n_Sm <plus_n_Sm #H
1448   >(Hind b (injective_S ?? (injective_S ?? H))) // ]
1449 ]
1450qed.
1451
1452lemma sth_not_s: ∀x.x ≠ S x.
1453 #x cases x
1454 [ // | #y // ]
1455qed.
1456
1457lemma measure_full: ∀program.∀policy.
1458  measure_int program policy 0 = 2*|program| → ∀i.i<|program| →
1459  (\snd (bvt_lookup ?? (bitvector_of_nat ? i) policy 〈0,short_jump〉)) = long_jump.
1460 #program #policy elim program
1461 [ #Hm #i #Hi @⊥ @(absurd … Hi) @not_le_Sn_O
1462 | #h #t #Hind #Hm #i #Hi cut (measure_int t policy 0 = 2*|t|)
1463   [ whd in match (measure_int ???) in Hm;
1464     cases (\snd (lookup … (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉)) in Hm;
1465     normalize nodelta
1466     [ #H @⊥ @(absurd ? (measure_le t policy)) >H @lt_to_not_le /2 by lt_plus, le_n/
1467     | >measure_plus >commutative_plus #H @⊥ @(absurd ? (measure_le t policy))
1468       <(plus_to_minus … (sym_eq … H)) @lt_to_not_le normalize
1469       >(commutative_plus (|t|) 0) <plus_O_n <minus_n_O
1470       >plus_n_Sm @le_n
1471     | >measure_plus <times_n_Sm >commutative_plus #H lapply (injective_plus_r … H) //
1472     ]
1473   | #Hmt cases (le_to_or_lt_eq … Hi) -Hi;
1474   [ #Hi @(Hind Hmt i (le_S_S_to_le … Hi))
1475   | #Hi >(injective_S … Hi) whd in match (measure_int ???) in Hm;
1476     cases (\snd (lookup … (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉)) in Hm;
1477     normalize nodelta
1478     [ >Hmt normalize <plus_n_O >(commutative_plus (|t|) (S (|t|)))
1479       >plus_n_Sm #H @⊥ @(absurd ? (bla ?? H)) @sth_not_s
1480     | >measure_plus >Hmt normalize <plus_n_O >commutative_plus normalize
1481       #H @⊥ @(absurd ? (injective_plus_r … (injective_S ?? H))) @sth_not_s
1482     | #_ //
1483     ]
1484   ]]
1485 ]
1486qed.
1487
1488lemma eq_plus_S_to_lt:
1489  ∀n,m,p:ℕ.n = m + (S p) → m < n.
1490 //
1491qed.
1492
1493(* how to do this with a sigma-type and elimination? *)
1494lemma measure_special: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
1495  ∀policy:Σp:jump_expansion_policy.
1496    (∀i.i ≥ |program| → i < 2^16 → lookup_opt … (bitvector_of_nat ? i) p = None ?) ∧
1497    jump_in_policy program p.
1498  measure_int program policy 0 = measure_int program (jump_expansion_step program policy) 0 →
1499  policy_equal program policy (jump_expansion_step program policy).
1500#program #policy lapply (le_n (|program|)) @(list_ind ?
1501  (λx.|x| ≤ |program| → measure_int x (eject … policy) 0 = measure_int x (eject … (jump_expansion_step program policy)) 0 →
1502      policy_equal x (eject … policy) (eject … (jump_expansion_step program policy)))
1503   ?? program)
1504 [ #Hp #Hm #i #Hi @⊥ @(absurd ? Hi) @not_le_Sn_O
1505 | #h #t #Hind #Hp #Hm #i #Hi cases (le_to_or_lt_eq … Hi) -Hi;
1506   [ #Hi @Hind
1507     [ @(transitive_le … Hp) //
1508     | whd in match (measure_int ???) in Hm; whd in match (measure_int ?(jump_expansion_step ??)?) in Hm;
1509       lapply (proj2 ?? (sig2 ?? (jump_expansion_step program policy)) (|t|) ?)
1510       [ @(lt_to_le_to_lt … (|h::t|)) [ // | @Hp ]
1511       | cases (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉)) in Hm;
1512         cases (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (jump_expansion_step program policy) 〈0,short_jump〉));
1513         normalize nodelta
1514         [1: #H #_ @H
1515         |2,3: >measure_plus #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt
1516           @measure_incr_or_equal @(transitive_le … Hp) @le_n_Sn
1517         |4,7,8: #_ #H elim H #H2 [1,3,5: @⊥ @H2 |2,4,6: destruct (H2) ]
1518         |5: >measure_plus >measure_plus >commutative_plus >(commutative_plus ? 1)
1519           #H #_ @(injective_plus_r … H)
1520         |6: >measure_plus >measure_plus
1521            change with (1+1) in match (2); >assoc_plus1 >(commutative_plus 1 (measure_int ???))
1522            #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt @monotonic_le_plus_l
1523            @measure_incr_or_equal @(transitive_le … Hp) @le_n_Sn
1524         |9: >measure_plus >measure_plus >commutative_plus >(commutative_plus ? 2)
1525           #H #_ @(injective_plus_r … H)
1526         ]
1527       ]
1528     | @(le_S_S_to_le … Hi)
1529     ]
1530   | #Hi >(injective_S … Hi) whd in match (measure_int ???) in Hm; 
1531     whd in match (measure_int ?(jump_expansion_step ??)?) in Hm;
1532     lapply (proj2 ?? (sig2 ?? (jump_expansion_step program policy)) (|t|) ?)
1533     [ @(lt_to_le_to_lt … (|h::t|)) [ // | @Hp ]
1534     | cases (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) policy 〈0,short_jump〉)) in Hm;
1535       cases (\snd (bvt_lookup ?? (bitvector_of_nat ? (|t|)) (jump_expansion_step program policy) 〈0,short_jump〉));
1536       normalize nodelta
1537       [1,5,9: #_ #_ //
1538       |4,7,8: #_ #H elim H #H2 [1,3,5: @⊥ @H2 |2,4,6: destruct (H2) ]
1539       |2,3: >measure_plus #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt
1540         @measure_incr_or_equal @(transitive_le … Hp) @le_n_Sn
1541       |6: >measure_plus >measure_plus
1542          change with (1+1) in match (2); >assoc_plus1 >(commutative_plus 1 (measure_int ???))
1543          #H #_ @⊥ @(absurd ? (eq_plus_S_to_lt … H)) @le_to_not_lt @monotonic_le_plus_l
1544          @measure_incr_or_equal @(transitive_le … Hp) @le_n_Sn
1545       ]
1546     ]
1547   ]
1548 ] 
1549qed.
1550
1551lemma dec_is_jump: ∀x.(is_jump x) + (¬is_jump x).
1552#x cases x #l #i cases i
1553[#id cases id
1554 [1,2,3,6,7,33,34:
1555  #x #y %2 whd in match (is_jump ?); /2 by nmk/
1556 |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32:
1557  #x %2 whd in match (is_jump ?); /2 by nmk/
1558 |35,36,37: %2 whd in match (is_jump ?); /2 by nmk/
1559 |9,10,14,15: #x %1 //
1560 |11,12,13,16,17: #x #y %1 //
1561 ]
1562|2,3: #x %2 /2 by nmk/
1563|4,5: #x %1 //
1564|6: #x #y %2 /2 by nmk/
1565]
1566qed.
1567 
1568lemma measure_zero: ∀l.∀program:Σl:list labelled_instruction.|l| < 2^16.
1569  |l| ≤ |program| → measure_int l (jump_expansion_internal program 0) 0 = 0.
1570 #l #program elim l
1571 [ //
1572 | #h #t #Hind #Hp whd in match (measure_int ???);
1573   cases (dec_is_jump (nth (|t|) ? program 〈None ?, Comment []〉)) #Hj
1574   [ >(lookup_opt_lookup_hit … (proj2 ?? (sig2 ?? (jump_expansion_start program)) (|t|) ? Hj) 〈0,short_jump〉)
1575     [ normalize nodelta @Hind @le_S_to_le ]
1576     @Hp
1577   | >(lookup_opt_lookup_miss … (proj1 ?? (jump_not_in_policy program (jump_expansion_internal program 0) (|t|) ?) Hj) 〈0,short_jump〉)
1578     [ normalize nodelta @Hind @le_S_to_le ]
1579     @Hp
1580   ]
1581 ]
1582qed.
1583 
1584definition je_fixpoint: ∀program:(Σl:list labelled_instruction.|l| < 2^16).
1585  Σp:jump_expansion_policy.∃n.∀k.n < k → policy_equal program (jump_expansion_internal program k) p.
1586 #program @(dp … (jump_expansion_internal program (2*|program|)))
1587 @(ex_intro … (2*|program|)) #k #Hk
1588 cases (dec_bounded_exists (λk.policy_equal program (jump_expansion_internal program k)
1589   (jump_expansion_internal program (S k))) ? (2*|program|))
1590 [ #H elim H -H #x #Hx @pe_trans
1591   [ @(jump_expansion_internal program x)
1592   | @pe_sym @equal_remains_equal
1593     [ @(proj2 ?? Hx)
1594     | @(transitive_le ? (2*|program|))
1595       [ @le_S_S_to_le @le_S @(proj1 ?? Hx)
1596       | @le_S_S_to_le @le_S @Hk
1597       ]
1598     ]
1599   | @equal_remains_equal
1600     [ @(proj2 ?? Hx)
1601     | @le_S_S_to_le @le_S @(proj1 ?? Hx)
1602     ]
1603   ]
1604 | #Hnex lapply (not_exists_forall … Hnex) -Hnex; #Hfa @pe_sym @equal_remains_equal
1605   [ lapply (measure_full program (jump_expansion_internal program (2*|program|)))
1606     #Hfull #i #Hi
1607     lapply (proj2 ?? (sig2 ?? (jump_expansion_step program (jump_expansion_internal program (2*|program|)))) i Hi)
1608     >(Hfull ? i Hi)
1609     [ cases (\snd (bvt_lookup ?? (bitvector_of_nat 16 i) (jump_expansion_step program (jump_expansion_internal program (2*|program|))) 〈0,short_jump〉))
1610       [1,2: #H elim H #H2 [1,3: @⊥ @H2 |2,4: destruct (H2) ]
1611       | #_ //
1612       ]
1613     | -i @le_to_le_to_eq
1614       [ @measure_le
1615       | lapply (le_n (2*|program|)) elim (2*|program|) in ⊢ (?%? → %);
1616         [ #_ >measure_zero @le_n
1617         | #x #Hind #Hx
1618           cut (measure_int program (jump_expansion_internal program x) 0 <
1619                measure_int program (jump_expansion_internal program (S x)) 0)
1620           [ elim (le_to_or_lt_eq …
1621               (measure_incr_or_equal program (jump_expansion_internal program x) program (le_n (|program|)) 0))
1622             [ //
1623             | #H @⊥ @(absurd ?? (Hfa x Hx)) @measure_special @H
1624             ]
1625           | #H lapply (Hind (le_S_to_le … Hx)) #H2 @(le_to_lt_to_lt … H) @H2
1626           ]
1627         ]
1628       ]
1629     ]
1630   | @le_S_to_le @Hk
1631   ]
1632 | #n @dec_bounded_forall #m @dec_eq_jump_length
1633 ]
1634qed.
1635
1636let rec transform_policy (n: nat) policy (acc: BitVectorTrie jump_length 16) on n:
1637  BitVectorTrie jump_length 16 ≝
1638  match n with
1639  [ O    ⇒ acc
1640  | S n' ⇒
1641    match lookup_opt … (bitvector_of_nat 16 n') policy with
1642    [ None   ⇒ transform_policy n' policy acc
1643    | Some x ⇒ let 〈pc,length〉 ≝ x in
1644      transform_policy n' policy (insert … pc length acc)
1645    ]
1646  ].
1647
1648(**************************************** START OF POLICY ABSTRACTION ********************)
1649
1650definition policy_type ≝ Word → jump_length.
1651
1652definition jump_expansion':
1653 ∀program:preamble × (Σl:list labelled_instruction.|l| < 2^16).
1654 policy_type ≝
1655 λprogram.λpc.
1656  let policy ≝
1657    transform_policy (|\snd program|) (eject … (je_fixpoint (\snd program))) (Stub ??) in
1658  lookup ? ? pc policy long_jump.
1659/2 by Stub, dp/
1660qed.
1661
1662definition assembly_1_pseudoinstruction_safe ≝
1663  λprogram: pseudo_assembly_program.
1664  λjump_expansion: policy_type.
1665  λppc: Word.
1666  λpc: Word.
1667  λlookup_labels.
1668  λlookup_datalabels.
1669  λi.
1670  let expansion ≝ jump_expansion ppc in
1671    match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc expansion i with
1672    [ None ⇒ None ?
1673    | Some pseudos ⇒
1674      let mapped ≝ map ? ? assembly1 pseudos in
1675      let flattened ≝ flatten ? mapped in
1676      let pc_len ≝ length ? flattened in
1677        Some ? (〈pc_len, flattened〉)
1678    ].
1679 
1680definition construct_costs_safe ≝
1681  λprogram.
1682  λjump_expansion: policy_type.
1683  λpseudo_program_counter: nat.
1684  λprogram_counter: nat.
1685  λcosts: BitVectorTrie costlabel 16.
1686  λi.
1687  match i with
1688  [ Cost cost ⇒
1689    let program_counter_bv ≝ bitvector_of_nat ? program_counter in
1690      Some ? 〈program_counter, (insert … program_counter_bv cost costs)〉
1691  | _ ⇒
1692    let pc_bv ≝ bitvector_of_nat ? program_counter in
1693    let ppc_bv ≝ bitvector_of_nat ? pseudo_program_counter in
1694    let lookup_labels ≝ λx.pc_bv in
1695    let lookup_datalabels ≝ λx.zero ? in
1696    let pc_delta_assembled ≝
1697      assembly_1_pseudoinstruction_safe program jump_expansion ppc_bv pc_bv
1698        lookup_labels lookup_datalabels i
1699    in
1700    match pc_delta_assembled with
1701    [ None ⇒ None ?
1702    | Some pc_delta_assembled ⇒
1703      let 〈pc_delta, assembled〉 ≝ pc_delta_assembled in
1704        Some ? 〈pc_delta + program_counter, costs〉       
1705    ]
1706  ].
1707
1708(* This establishes the correspondence between pseudo program counters and
1709   program counters. It is at the heart of the proof. *)
1710(*CSC: code taken from build_maps *)
1711definition sigma00: pseudo_assembly_program → policy_type → list ? → ? → option (nat × (nat × (BitVectorTrie Word 16))) ≝
1712 λinstr_list.
1713 λjump_expansion: policy_type.
1714 λl:list labelled_instruction.
1715 λacc.
1716  foldl …
1717   (λt,i.
1718       match t with
1719       [ None ⇒ None …
1720       | Some ppc_pc_map ⇒
1721         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
1722         let 〈program_counter, sigma_map〉 ≝ pc_map in
1723         let 〈label, i〉 ≝ i in
1724          match construct_costs_safe instr_list jump_expansion ppc program_counter (Stub …) i with
1725           [ None ⇒ None ?
1726           | Some pc_ignore ⇒
1727              let 〈pc,ignore〉 ≝ pc_ignore in
1728                Some … 〈S ppc, 〈pc, insert ?? (bitvector_of_nat 16 ppc) (bitvector_of_nat 16 pc) sigma_map〉〉 ]
1729       ]) acc l.
1730
1731definition sigma0: pseudo_assembly_program → policy_type → option (nat × (nat × (BitVectorTrie Word 16))) ≝
1732  λprog.
1733  λjump_expansion.
1734    sigma00 prog jump_expansion (\snd prog) (Some ? 〈0, 〈0, Stub …〉〉).
1735
1736definition tech_pc_sigma00: pseudo_assembly_program → policy_type → list labelled_instruction → option (nat × nat) ≝
1737 λprogram,jump_expansion,instr_list.
1738  match sigma00 program jump_expansion instr_list (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (* acc copied from sigma0 *) with
1739   [ None ⇒ None …
1740   | Some result ⇒
1741      let 〈ppc,pc_sigma_map〉 ≝ result in
1742      let 〈pc,map〉 ≝ pc_sigma_map in
1743       Some … 〈ppc,pc〉 ].
1744
1745definition sigma_safe: pseudo_assembly_program → policy_type → option (Word → Word) ≝
1746 λinstr_list,jump_expansion.
1747  match sigma0 instr_list jump_expansion with
1748  [ None ⇒ None ?
1749  | Some result ⇒
1750    let 〈ppc,pc_sigma_map〉 ≝ result in
1751    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
1752      if gtb pc (2^16) then
1753        None ?
1754      else
1755        Some ? (λx. lookup … x sigma_map (zero …)) ].
1756
1757(* stuff about policy *)
1758
1759definition policy_ok ≝ λjump_expansion,p. sigma_safe p jump_expansion ≠ None ….
1760
1761definition policy ≝ λp. Σjump_expansion:policy_type. policy_ok jump_expansion p.
1762
1763lemma eject_policy: ∀p. policy p → policy_type.
1764 #p #pol @(eject … pol)
1765qed.
1766
1767coercion eject_policy nocomposites: ∀p.∀pol:policy p. policy_type ≝ eject_policy on _pol:(policy ?) to policy_type.
1768
1769definition sigma: ∀p:pseudo_assembly_program. policy p → Word → Word ≝
1770 λp,policy.
1771  match sigma_safe p (eject … policy) return λr:option (Word → Word). r ≠ None … → Word → Word with
1772   [ None ⇒ λabs. ⊥
1773   | Some r ⇒ λ_.r] (sig2 … policy).
1774 cases abs /2/
1775qed.
1776
1777example sigma_0: ∀p,pol. sigma p pol (bitvector_of_nat ? 0) = bitvector_of_nat ? 0.
1778 cases daemon.
1779qed.
1780
1781axiom fetch_pseudo_instruction_split:
1782 ∀instr_list,ppc.
1783  ∃pre,suff,lbl.
1784   (pre @ [〈lbl,\fst (fetch_pseudo_instruction instr_list ppc)〉]) @ suff = instr_list.
1785
1786lemma sigma00_append:
1787 ∀instr_list,jump_expansion,l1,l2,acc.
1788  sigma00 instr_list jump_expansion (l1@l2) acc =
1789   sigma00 instr_list jump_expansion
1790    l2 (sigma00 instr_list jump_expansion l1 acc).
1791 whd in match sigma00; normalize nodelta;
1792 #instr_list #jump_expansion #l1 #l2 #acc @foldl_append
1793qed.
1794
1795lemma sigma00_strict:
1796 ∀instr_list,jump_expansion,l,acc. acc = None ? →
1797  sigma00 instr_list jump_expansion l acc = None ….
1798 #instr_list #jump_expansion #l elim l
1799  [ #acc #H >H %
1800  | #hd #tl #IH #acc #H >H change with (sigma00 ?? tl ? = ?) @IH % ]
1801qed.
1802
1803lemma policy_ok_prefix_ok:
1804 ∀program.∀pol:policy program.∀suffix,prefix.
1805  prefix@suffix = \snd program →
1806   sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) ≠ None ….
1807 * #preamble #instr_list #pol #suffix #prefix #prf whd in prf:(???%);
1808 generalize in match (sig2 ?? pol); whd in prf:(???%); <prf in pol; #pol
1809 whd in match policy_ok; whd in match sigma_safe; whd in match sigma0;
1810 normalize nodelta >sigma00_append
1811 cases (sigma00 ?? prefix ?)
1812  [2: #x #_ % #abs destruct(abs)
1813  | * #abs @⊥ @abs >sigma00_strict % ]
1814qed.
1815
1816lemma policy_ok_prefix_hd_ok:
1817 ∀program.∀pol:policy program.∀suffix,hd,prefix,ppc_pc_map.
1818  (prefix@[hd])@suffix = \snd program →
1819   Some ? ppc_pc_map = sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) →
1820    let 〈ppc,pc_map〉 ≝ ppc_pc_map in
1821    let 〈program_counter, sigma_map〉 ≝ pc_map in
1822    let 〈label, i〉 ≝ hd in
1823     construct_costs_safe program pol ppc program_counter (Stub …) i ≠ None ….
1824 * #preamble #instr_list #pol #suffix #hd #prefix #ppc_pc_map #EQ1 #EQ2
1825 generalize in match (policy_ok_prefix_ok 〈preamble,instr_list〉 pol suffix
1826  (prefix@[hd]) EQ1) in ⊢ ?; >sigma00_append <EQ2 whd in ⊢ (?(??%?) → ?);
1827 @pair_elim' #ppc #pc_map #EQ3 normalize nodelta
1828 @pair_elim' #pc #map #EQ4 normalize nodelta
1829 @pair_elim' #l' #i' #EQ5 normalize nodelta
1830 cases (construct_costs_safe ??????) normalize
1831  [* #abs @⊥ @abs % | #X #_ % #abs destruct(abs)]
1832qed.
1833
1834definition expand_pseudo_instruction:
1835 ∀program:pseudo_assembly_program.∀pol: policy program.
1836  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pc.
1837  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1838  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1839  let pi ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in
1840  pc = sigma program pol ppc →
1841  Σres:list instruction. Some … res = expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) pi
1842≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pc,prf1,prf2,prf3.
1843   match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) (\fst (fetch_pseudo_instruction (\snd program) ppc)) with
1844    [ None ⇒ let dummy ≝ [ ] in dummy
1845    | Some res ⇒ res ].
1846 [ @⊥ whd in p:(??%??);
1847   generalize in match (sig2 ?? pol); whd in ⊢ (% → ?);
1848   whd in ⊢ (?(??%?) → ?); change with (sigma00 ????) in ⊢ (?(??(match % with [_ ⇒ ? | _ ⇒ ?])?) → ?);
1849   generalize in match (refl … (sigma00 program pol (\snd program) (Some ? 〈O,〈O,Stub (BitVector 16) 16〉〉)));
1850   cases (sigma00 ????) in ⊢ (??%? → %); normalize nodelta [#_ * #abs @abs %]
1851   #res #K
1852   cases (fetch_pseudo_instruction_split (\snd program) ppc) #pre * #suff * #lbl #EQ1
1853   generalize in match (policy_ok_prefix_hd_ok program pol … EQ1 ?) in ⊢ ?;
1854   cases daemon (* CSC: XXXXXXXX Ero qui
1855   
1856    [3: @policy_ok_prefix_ok ]
1857    | sigma00 program pol pre
1858
1859
1860
1861   QUA USARE LEMMA policy_ok_prefix_hd_ok combinato a lemma da fare che
1862   fetch ppc = hd sse program = pre @ [hd] @ tl e |pre| = ppc
1863   per concludere construct_costs_safe ≠ None *)
1864 | >p %]
1865qed.
1866
1867(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1868definition assembly_1_pseudoinstruction':
1869 ∀program:pseudo_assembly_program.∀pol: policy program.
1870  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1871  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1872  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1873  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
1874  Σres:(nat × (list Byte)).
1875   Some … res = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi ∧
1876   let 〈len,code〉 ≝ res in
1877    sigma program pol (\snd (half_add ? ppc (bitvector_of_nat ? 1))) =
1878     bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len)
1879≝ λprogram: pseudo_assembly_program.
1880  λpol: policy program.
1881  λppc: Word.
1882  λlookup_labels.
1883  λlookup_datalabels.
1884  λpi.
1885  λprf1,prf2,prf3.
1886   match assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi with
1887    [ None ⇒ let dummy ≝ 〈0,[ ]〉 in dummy
1888    | Some res ⇒ res ].
1889 [ @⊥ elim pi in p; [*]
1890   try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs
1891   generalize in match (jmeq_to_eq ??? abs); -abs; #abs whd in abs:(??%?); try destruct(abs)
1892   whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?);
1893   (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *)
1894   cases daemon
1895 | % [ >p %]
1896   cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta;
1897   (* THIS SHOULD BE TRUE INSTEAD *)
1898   cases daemon]
1899qed.
1900
1901definition assembly_1_pseudoinstruction:
1902 ∀program:pseudo_assembly_program.∀pol: policy program.
1903  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1904  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1905  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1906  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
1907   nat × (list Byte)
1908≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pi,prf1,prf2,prf3.
1909   assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1
1910    prf2 prf3.
1911
1912lemma assembly_1_pseudoinstruction_ok1:
1913 ∀program:pseudo_assembly_program.∀pol: policy program.
1914  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1915  ∀prf1:lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)).
1916  ∀prf2:lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)).
1917  ∀prf3:\fst (fetch_pseudo_instruction (\snd program) ppc) = pi.
1918     Some … (assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3)
1919   = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi.
1920 #program #pol #ppc #lookup_labels #lookup_datalabels #pi #prf1 #prf2 #prf3
1921 cases (sig2 … (assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3))
1922 #H1 #_ @H1
1923qed.
1924
1925(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1926definition construct_costs':
1927 ∀program. ∀pol:policy program. ∀ppc,pc,costs,i.
1928  Σres:(nat × (BitVectorTrie costlabel 16)). Some … res = construct_costs_safe program pol ppc pc costs i
1929
1930  λprogram.λpol: policy program.λppc,pc,costs,i.
1931   match construct_costs_safe program pol ppc pc costs i with
1932    [ None ⇒ let dummy ≝ 〈0, Stub costlabel 16〉 in dummy
1933    | Some res ⇒ res ].
1934 [ cases daemon
1935 | >p %]
1936qed.
1937
1938definition construct_costs ≝
1939 λprogram,pol,ppc,pc,costs,i. eject … (construct_costs' program pol ppc pc costs i).
1940
1941(*
1942axiom suffix_of: ∀A:Type[0]. ∀l,prefix:list A. list A.
1943axiom suffix_of_ok: ∀A,l,prefix. prefix @ suffix_of A l prefix = l.
1944
1945axiom foldl_strong_step:
1946 ∀A:Type[0].
1947  ∀P: list A → Type[0].
1948   ∀l: list A.
1949    ∀H: ∀prefix,hd,tl. l =  prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]).
1950     ∀acc: P [ ].
1951      ∀Q: ∀prefix. P prefix → Prop.
1952       ∀HQ: ∀prefix,hd,tl.∀prf: l = prefix @ [hd] @ tl.
1953        ∀acc: P prefix. Q prefix acc → Q (prefix @ [hd]) (H prefix hd tl prf acc).
1954       Q [ ] acc →
1955        Q l (foldl_strong A P l H acc).
1956(*
1957 #A #P #l #H #acc #Q #HQ #Hacc normalize;
1958 generalize in match
1959  (foldl_strong ?
1960   (λpre. Q pre (foldl_strong_internal A P l (suffix_of A l pre) ? [ ] pre acc ?))
1961   l ? Hacc)
1962 [3: >suffix_of_ok % | 2: #prefix #hd #tl #EQ @(H prefix hd (tl@suffix_of A l pre) EQ) ]
1963 [2: #prefix #hd #tl #prf #X whd in ⊢ (??%)
1964 #K
1965
1966 generalize in match
1967  (foldl_strong ?
1968   (λpre. Q pre (foldl_strong_internal A P l H pre (suffix_of A l pre) acc (suffix_of_ok A l pre))))
1969 [2: @H
1970*)
1971
1972axiom foldl_elim:
1973 ∀A:Type[0].
1974  ∀B: Type[0].
1975   ∀H: A → B → A.
1976    ∀acc: A.
1977     ∀l: list B.
1978      ∀Q: A → Prop.
1979       (∀acc:A.∀b:B. Q acc → Q (H acc b)) →
1980         Q acc →
1981          Q (foldl A B H acc l).
1982*)
1983
1984lemma option_destruct_Some: ∀A,a,b. Some A a = Some A b → a=b.
1985 #A #a #b #EQ destruct //
1986qed.
1987
1988(*
1989lemma tech_pc_sigma00_append_Some:
1990 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
1991  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
1992   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
1993 #program #pol #prefix #costs #label #i #ppc #pc #H
1994  whd in match tech_pc_sigma00 in ⊢ %; normalize nodelta;
1995  whd in match sigma00 in ⊢ %; normalize nodelta in ⊢ %;
1996  generalize in match (sig2 … pol) whd in ⊢ (% → ?) whd in ⊢ (?(??%?) → ?)
1997  whd in match sigma0; normalize nodelta;
1998  >foldl_step
1999  change with (? → match match sigma00 program pol prefix with [None ⇒ ? | Some res ⇒ ?] with [ None ⇒ ? | Some res ⇒ ? ] = ?)
2000  whd in match tech_pc_sigma00 in H; normalize nodelta in H;
2001  cases (sigma00 program pol prefix) in H ⊢ %
2002   [ whd in ⊢ (??%% → ?) #abs destruct(abs)
2003   | * #ppc' * #pc' #sigma_map normalize nodelta; #H generalize in match (option_destruct_Some ??? H)
2004     
2005     normalize nodelta; -H;
2006     
2007 
2008   generalize in match H; -H;
2009  generalize in match (foldl ?????); in H ⊢ (??match match % with [_ ⇒ ? | _ ⇒ ?] with [_ ⇒ ? | _ ⇒ ?]?)
2010   [2: whd in ⊢ (??%%)
2011XXX
2012*)
2013
2014axiom construct_costs_sigma:
2015 ∀p.∀pol:policy p.∀ppc,pc,costs,i.
2016  bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) →
2017   bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)).
2018
2019axiom tech_pc_sigma00_append_Some:
2020 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
2021  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
2022   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
2023
2024axiom eq_identifier_eq:
2025  ∀tag: String.
2026  ∀l.
2027  ∀r.
2028    eq_identifier tag l r = true → l = r.
2029
2030axiom neq_identifier_neq:
2031  ∀tag: String.
2032  ∀l, r: identifier tag.
2033    eq_identifier tag l r = false → (l = r → False).
2034
2035definition build_maps:
2036 ∀pseudo_program.∀pol:policy pseudo_program.
2037  Σres:((identifier_map ASMTag Word) × (BitVectorTrie costlabel 16)).
2038   let 〈labels, costs〉 ≝ res in
2039    ∀id. occurs_exactly_once id (\snd pseudo_program) →
2040     lookup_def … labels id (zero ?) = sigma pseudo_program pol (address_of_word_labels_code_mem (\snd pseudo_program) id) ≝
2041  λpseudo_program.
2042  λpol:policy pseudo_program.
2043    let result ≝
2044      foldl_strong
2045        (option Identifier × pseudo_instruction)
2046          (λpre. Σres:((identifier_map ASMTag Word) × (nat × (nat × (BitVectorTrie costlabel 16)))).
2047            let 〈labels,ppc_pc_costs〉 ≝ res in
2048            let 〈ppc',pc_costs〉 ≝ ppc_pc_costs in
2049            let 〈pc',costs〉 ≝ pc_costs in
2050              And ( And (
2051              And (bitvector_of_nat ? pc' = sigma pseudo_program pol (bitvector_of_nat ? ppc')) (*∧*)
2052                (ppc' = length … pre)) (*∧ *)
2053                (tech_pc_sigma00 pseudo_program (eject … pol) pre = Some ? 〈ppc',pc'〉)) (*∧*)
2054                (∀id. occurs_exactly_once id pre →
2055                  lookup_def … labels id (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem pre id)))
2056                (\snd pseudo_program)
2057        (λprefix,i,tl,prf,t.
2058          let 〈labels, ppc_pc_costs〉 ≝ t in
2059          let 〈ppc, pc_costs〉 ≝ ppc_pc_costs in
2060          let 〈pc,costs〉 ≝ pc_costs in
2061          let 〈label, i'〉 ≝ i in
2062          let labels ≝
2063            match label with
2064            [ None ⇒ labels
2065            | Some label ⇒
2066                let program_counter_bv ≝ bitvector_of_nat ? pc in
2067                  add ? ? labels label program_counter_bv
2068            ]
2069          in
2070            let construct ≝ construct_costs pseudo_program pol ppc pc costs i' in
2071              〈labels, 〈S ppc,construct〉〉) 〈empty_map …, 〈0, 〈0, Stub ? ?〉〉〉
2072    in
2073      let 〈labels, ppc_pc_costs〉 ≝ result in
2074      let 〈ppc,pc_costs〉 ≝ ppc_pc_costs in
2075      let 〈pc, costs〉 ≝ pc_costs in
2076        〈labels, costs〉.
2077 [2: whd generalize in match (sig2 … t); >p >p1 >p2 >p3 * * * #IHn1 #IH0 #IH1 #IH2
2078   generalize in match (refl … construct); cases construct in ⊢ (???% → %); #PC #CODE #JMEQ % [% [%]]
2079   [ <(construct_costs_sigma … costs i1 IHn1) change with (? = ?(\fst construct)) >JMEQ %
2080   | >append_length <IH0 normalize; -IHn1; (*CSC: otherwise it diverges!*) //
2081   | >(tech_pc_sigma00_append_Some … costs … IH1) change with (Some … 〈S ppc,\fst construct〉 = ?) >JMEQ %
2082   | #id normalize nodelta; -labels1; cases label; normalize nodelta
2083     [ #K <address_of_word_labels_code_mem_None [2:@K] @IH2 -IHn1; (*CSC: otherwise it diverges!*) //
2084     | #l #H generalize in match (occurs_exactly_once_Some ???? H) in ⊢ ?;
2085       generalize in match (refl … (eq_identifier ? l id)); cases (eq_identifier … l id) in ⊢ (???% → %);
2086        [ #EQ #_ <(eq_identifier_eq … EQ) >lookup_def_add_hit >address_of_word_labels_code_mem_Some_hit
2087          <IH0 [@IHn1 | <(eq_identifier_eq … EQ) in H; #H @H]
2088        | #EQ change with (occurs_exactly_once ?? → ?) #K >lookup_def_add_miss [2: -IHn1;
2089          (*Andrea:XXXX used to work /2/*)@nmk #IDL lapply (sym_eq ? ? ? IDL)
2090          lapply (neq_identifier_neq ? ? ? EQ) #ASSM assumption ]
2091          <(address_of_word_labels_code_mem_Some_miss … EQ … H) @IH2 assumption ]]]
2092 |3: whd % [% [%]] [>sigma_0 % | % | % | #id normalize in ⊢ (% → ?); #abs @⊥ // ]
2093 | generalize in match (sig2 … result) in ⊢ ?;
2094   normalize nodelta in p ⊢ %; -result; >p in ⊢ (match % with [_ ⇒ ?] → ?);
2095   normalize nodelta; >p1 normalize nodelta; >p2; normalize nodelta; * #_; #H @H]
2096qed.
2097
2098definition assembly:
2099 ∀p:pseudo_assembly_program. policy p → list Byte × (BitVectorTrie costlabel 16) ≝
2100  λp.let 〈preamble, instr_list〉 ≝ p in
2101   λpol.
2102    let 〈labels,costs〉 ≝ build_maps 〈preamble,instr_list〉 pol in
2103    let datalabels ≝ construct_datalabels preamble in
2104    let lookup_labels ≝ λx. lookup_def … labels x (zero ?) in
2105    let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
2106    let result ≝
2107     foldl_strong
2108      (option Identifier × pseudo_instruction)
2109      (λpre. Σpc_ppc_code:(Word × (Word × (list Byte))).
2110        let 〈pc,ppc_code〉 ≝ pc_ppc_code in
2111        let 〈ppc,code〉 ≝ ppc_code in
2112         ∀ppc'.
2113          let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' in
2114          let 〈len,assembledi〉 ≝
2115           assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc' lookup_labels lookup_datalabels pi ??? in
2116           True)
2117      instr_list
2118      (λprefix,hd,tl,prf,pc_ppc_code.
2119        let 〈pc, ppc_code〉 ≝ pc_ppc_code in
2120        let 〈ppc, code〉 ≝ ppc_code in
2121        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc lookup_labels lookup_datalabels (\snd hd) ??? in
2122        let 〈new_pc, flags〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in
2123        let new_ppc ≝ \snd (half_add ? ppc (bitvector_of_nat ? 1)) in
2124         〈new_pc, 〈new_ppc, (code @ program)〉〉)
2125      〈(zero ?), 〈(zero ?), [ ]〉〉
2126    in
2127     〈\snd (\snd result), costs〉.
2128 [2,5: %
2129 |*: cases daemon ]
2130qed.
2131
2132definition assembly_unlabelled_program: assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝
2133 λp. Some ? (〈foldr ? ? (λi,l. assembly1 i @ l) [ ] p, Stub …〉).
Note: See TracBrowser for help on using the repository browser.