source: src/ASM/Assembly.ma @ 1556

Last change on this file since 1556 was 1556, checked in by mulligan, 8 years ago

submitting to avoid conflicts

File size: 79.7 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   ⇒ (* hmm, 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 jmpleq_max_length: ∀ol,nl.
739  jmpleq ol (max_length ol nl).
740 #ol #nl cases ol cases nl
741 /2 by or_introl, or_intror, I/
742qed.
743 
744definition is_jump' ≝
745  λx:preinstruction Identifier.
746  match x with
747  [ JC _ ⇒ True
748  | JNC _ ⇒ True
749  | JZ _ ⇒ True
750  | JNZ _ ⇒ True
751  | JB _ _ ⇒ True
752  | JNB _ _ ⇒ True
753  | JBC _ _ ⇒ True
754  | CJNE _ _ ⇒ True
755  | DJNZ _ _ ⇒ True
756  | _ ⇒ False
757  ].
758 
759definition is_jump ≝
760  λx:labelled_instruction.
761  let 〈label,instr〉 ≝ x in
762  match instr with
763  [ Instruction i   ⇒ is_jump' i
764  | Call _ ⇒ True
765  | Jmp _ ⇒ True
766  | _ ⇒ False
767  ].
768
769definition jump_in_policy ≝
770  λprefix:list labelled_instruction.λpolicy:jump_expansion_policy.
771  ∀i:ℕ.i < |prefix| →
772  (is_jump (nth i ? prefix 〈None ?, Comment []〉) ↔
773   ∃p,j.lookup_opt … (bitvector_of_nat 16 i) policy = Some ? 〈p,j〉).
774 
775axiom bitvector_of_nat_abs:
776  ∀x,y:ℕ.x ≠ y → ¬eq_bv 16 (bitvector_of_nat 16 x) (bitvector_of_nat 16 y).
777
778lemma le_S_to_le: ∀n,m:ℕ.S n ≤ m → n ≤ m.
779 /2/ qed.
780
781lemma jump_not_in_policy: ∀prefix:list labelled_instruction.
782 ∀policy:(Σp:jump_expansion_policy.
783 (∀i.i ≥ |prefix| → lookup_opt … (bitvector_of_nat ? i) p = None ?) ∧
784 jump_in_policy prefix p).
785  ∀i:ℕ.i < |prefix| →
786  ¬is_jump (nth i ? prefix 〈None ?, Comment []〉) ↔
787  lookup_opt … (bitvector_of_nat 16 i) policy = None ?.
788 #prefix #policy #i #Hi @conj
789 [ #Hnotjmp lapply (refl ? (lookup_opt … (bitvector_of_nat 16 i) policy))
790   cases (lookup_opt … (bitvector_of_nat 16 i) policy) in ⊢ (???% → ?);
791   [ #H @H
792   | #x cases x #y #z #H @⊥ @(absurd ? ? Hnotjmp) @(proj2 ?? (proj2 ?? (sig2 ?? policy) i Hi))
793     @(ex_intro … y (ex_intro … z H))
794   ]
795 | #Hnone @nmk #Hj lapply (proj1 ?? (proj2 ?? (sig2 ?? policy) i Hi) Hj)
796   #H elim H -H; #x #H elim H -H; #y #H >H in Hnone; #abs destruct (abs)
797 ] 
798qed.
799 
800definition jump_expansion_start: ∀program:list labelled_instruction.
801  Σpolicy:jump_expansion_policy.(∀i.i ≥ |program| → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧ jump_in_policy program policy ≝
802  λprogram.
803  foldl_strong (option Identifier × pseudo_instruction)
804  (λprefix.Σpolicy:jump_expansion_policy.(∀i.i ≥ |prefix| → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧ jump_in_policy prefix policy)
805  program
806  (λprefix.λx.λtl.λprf.λpolicy.
807   let 〈label,instr〉 ≝ x in
808   match instr with
809   [ Instruction i ⇒ match i with
810     [ JC _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
811     | JNC _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
812     | JZ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
813     | JNZ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
814     | JB _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
815     | JNB _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
816     | JBC _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
817     | CJNE _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
818     | DJNZ _ _ ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
819     | _ ⇒ (eject … policy)
820     ]
821   | Call c ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
822   | Jmp j  ⇒ bvt_insert … (bitvector_of_nat 16 (|prefix|)) 〈0,short_jump〉 policy
823   | _      ⇒ (eject … policy)
824   ]
825  ) (Stub ? ?).
826@conj
827(* non-jumps, lookup_opt = None *)
828[1,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,69,71,73,75,77,79,81,83:
829  #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi)
830  -Hi; #Hi @((proj1 ?? (sig2 ?? policy)) i)
831  [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,63,65:
832    @le_S_to_le @le_S_to_le @Hi
833  |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66:
834    <Hi @le_n_Sn
835  ]
836(* non-jumps, lookup_opt = Some *)
837|2,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,70,72,74,76,78,80,82,84:
838  #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi)
839  -Hi; #Hi
840  [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,63,65:
841    >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi))
842    @((proj2 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
843  |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66:
844    @conj >(injective_S … Hi)
845    [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,63,65:
846      >(nth_append_second ? ? prefix ? ? (le_n (|prefix|)))
847      <minus_n_n #H @⊥ @H
848    |2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66:
849      #H elim H; -H; #t1 #H elim H; -H #t2 #H
850      lapply (proj1 ?? (sig2 ?? policy) (|prefix|) (le_n (|prefix|)))
851      #H2 >H2 in H; #H destruct (H)
852    ]
853  ]
854(* jumps, lookup_opt = None *)
855|3,5,51,53,55,57,59,61,63,65,67: #i >append_length <commutative_plus #Hi normalize in Hi;
856  >lookup_opt_insert_miss
857  [1,3,5,7,9,11,13,15,17,19,21,23: @((proj1 ?? (sig2 ?? policy)) i) @(le_S_to_le … Hi)
858  |2,4,6,8,10,12,14,16,18,20,22,24: >eq_bv_sym @bitvector_of_nat_abs @lt_to_not_eq @Hi
859  ]
860(* non-jumps, lookup_opt = Some *)
861|4,6,52,54,56,58,60,62,64,66,68: #i >append_length <commutative_plus #Hi normalize in Hi;
862  cases (le_to_or_lt_eq … Hi) -Hi; #Hi
863  [1,3,5,7,9,11,13,15,17,19,21,23: >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi))
864    >lookup_opt_insert_miss
865    [1,3,5,7,9,11,13,15,17,19,21,23: @((proj2 ?? (sig2 ?? policy)) i) @(le_S_S_to_le … Hi)
866    |2,4,6,8,10,12,14,16,18,20,22,24: @bitvector_of_nat_abs @(lt_to_not_eq … (le_S_S_to_le … Hi))
867    ]
868  |2,4,6,8,10,12,14,16,18,20,22,24: @conj >(injective_S … Hi) #H
869    [2,4,6,8,10,12,14,16,18,20,22,24: >(nth_append_second ? ? prefix ? ? (le_n (|prefix|)))
870       <minus_n_n // ]
871    @(ex_intro ? ? 0 (ex_intro ? ? short_jump (lookup_opt_insert_hit ? ? 16 ? policy)))
872  ]
873(* cases for the empty program *)
874|85: #i #Hi //
875|86: whd #i #Hi @⊥ @(absurd (i < 0)) [ @Hi | @not_le_Sn_O ]
876]
877qed.
878
879definition policy_increase: list labelled_instruction → jump_expansion_policy →
880  jump_expansion_policy → Prop ≝
881 λprogram.λop.λp.
882  (* (∀i:ℕ.i < |program| →
883    lookup_opt … (bitvector_of_nat ? i) op = lookup_opt … (bitvector_of_nat ? i) p) ∨ *)
884  (∀i:ℕ.i < |program| →
885    jmpleq
886      (\snd (bvt_lookup … (bitvector_of_nat ? i) op 〈0,short_jump〉))
887      (\snd (bvt_lookup … (bitvector_of_nat ? i) p 〈0,short_jump〉))).
888   
889definition jump_expansion_step: ∀program:list labelled_instruction.
890  ∀old_policy:(Σpolicy.
891    (∀i.i ≥ |program| → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
892    jump_in_policy program policy).
893  (Σpolicy.
894    (∀i.i ≥ |program| → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
895    jump_in_policy program policy ∧
896    policy_increase program old_policy policy)
897    ≝
898  λprogram.λold_policy.
899  let labels ≝ create_label_map program old_policy in
900  let 〈final_pc, final_policy〉 ≝
901    foldl_strong (option Identifier × pseudo_instruction)
902    (λprefix.ℕ × Σpolicy.
903      (∀i.i ≥ |prefix| → lookup_opt … (bitvector_of_nat 16 i) policy = None ?) ∧
904      jump_in_policy prefix policy ∧
905      policy_increase prefix old_policy policy
906    )
907    program
908    (λprefix.λx.λtl.λprf.λacc.
909      let 〈pc, policy〉 ≝ acc in
910      let 〈label,instr〉 ≝ x in
911      let old_jump_length ≝ lookup_opt ? ? (bitvector_of_nat 16 (|prefix|)) old_policy in
912      let add_instr ≝
913        match instr with
914        [ Instruction i ⇒ jump_expansion_step_instruction labels pc i
915        | Call c        ⇒ Some ? (select_call_length labels pc c)
916        | Jmp j         ⇒ Some ? (select_jump_length labels pc j)
917        | _             ⇒ None ?
918        ] in
919      let 〈new_pc, new_policy〉 ≝
920        let 〈ignore,old_length〉 ≝ lookup … (bitvector_of_nat 16 (|prefix|)) old_policy 〈0, short_jump〉 in
921        match add_instr with
922        [ None    ⇒ (* i.e. it's not a jump *)
923          〈add_instruction_size pc long_jump instr, policy〉
924        | Some ai ⇒
925          let new_length ≝ max_length old_length ai in
926          〈add_instruction_size pc new_length instr, insert … (bitvector_of_nat 16 (|prefix|)) 〈pc, new_length〉 policy〉
927        ] in
928      〈new_pc, new_policy〉
929    ) 〈0, Stub ? ?〉 in
930    final_policy.
931[ @conj [ @conj #i >append_length <commutative_plus #Hi normalize in Hi;
932[ cases (lookup ??? old_policy ?) #h #n cases add_instr
933  [ @(proj1 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_to_le … Hi))
934  | #z normalize nodelta >lookup_opt_insert_miss
935    [ @(proj1 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_to_le … Hi))
936    | >eq_bv_sym @bitvector_of_nat_abs @lt_to_not_eq @Hi
937    ]
938  ]
939| cases (le_to_or_lt_eq … Hi) -Hi;
940  [ #Hi; >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) @conj
941    [ #Hj lapply (proj2 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi)) #Hacc
942      cases add_instr cases (lookup ??? old_policy ?) normalize nodelta #x #y
943      [ @(proj1 ?? Hacc Hj)
944      | #z elim (proj1 ?? Hacc Hj) #h #n elim n -n #n #Hn
945        % [ @h | % [ @n | <Hn @lookup_opt_insert_miss @bitvector_of_nat_abs
946            @(lt_to_not_eq i (|prefix|)) @(le_S_S_to_le … Hi) ] ]
947      ]
948    | lapply (proj2 ?? (proj1 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi)) #Hacc
949      #H elim H -H; #h #H elim H -H; #n cases add_instr cases (lookup ??? old_policy ?)
950      normalize nodelta #x #y [2: #z]
951      #Hl @(proj2 ?? Hacc) @(ex_intro ?? h (ex_intro ?? n ?))
952      [ <Hl @sym_eq @lookup_opt_insert_miss @bitvector_of_nat_abs @lt_to_not_eq @(le_S_S_to_le … Hi)
953      | @Hl
954      ]
955    ]
956  | #Hi >(injective_S … Hi) >(nth_append_second ? ? prefix ? ? (le_n (|prefix|)))
957     <minus_n_n whd in match (nth ????); whd in match (add_instr); cases instr
958     [1: #pi | 2,3: #x | 4,5: #id | 6: #x #y] @conj normalize nodelta
959     [3,5,11: #H @⊥ @H (* instr is not a jump *)
960     |4,6,12: #H elim H -H; #h #H elim H -H #n cases (lookup ??? old_policy ?)
961       #x #y normalize nodelta >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|)))
962       #H destruct (H)
963     |7,9: (* instr is a jump *) #_ cases (lookup ??? old_policy ?) #h #n
964       whd in match (snd ???); @(ex_intro ?? pc)
965       [ @(ex_intro ?? (max_length n (select_jump_length (create_label_map program old_policy) pc id)))
966       | @(ex_intro ?? (max_length n (select_call_length (create_label_map program old_policy) pc id)))
967       ] @lookup_opt_insert_hit
968     |8,10: #_ //
969     |1,2: cases pi
970       [35,36,37: #H @⊥ @H
971       |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #H @⊥ @H
972       |1,2,3,6,7,33,34: #x #y #H @⊥ @H
973       |9,10,14,15: #id #_ cases (lookup ??? old_policy ?) #h #n
974         whd in match (snd ???);
975         @(ex_intro ?? pc (ex_intro ?? (max_length n (select_reljump_length (create_label_map program old_policy) pc id)) ?))
976         @lookup_opt_insert_hit
977       |11,12,13,16,17: #x #id #_ cases (lookup ??? old_policy ?) #h #n
978         whd in match (snd ???);
979         @(ex_intro ?? pc (ex_intro ?? (max_length n (select_reljump_length (create_label_map program old_policy) pc id)) ?))
980         @lookup_opt_insert_hit
981       |72,73,74: #H elim H -H; #h #H elim H -H #n cases (lookup ??? old_policy ?)
982        #x #y normalize nodelta
983        >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|))) #H destruct (H)
984       |41,42,45,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69: #x
985        #H elim H -H; #h #H elim H -H #n cases (lookup ??? old_policy ?)
986        #x #y normalize nodelta
987        >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|))) #H destruct (H)
988       |38,39,40,43,44,70,71: #x #y #H elim H -H; #h #H elim H -H #n
989        cases (lookup ??? old_policy ?) #x #y normalize nodelta
990        >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|))) #H destruct (H)
991       |46,47,51,52: #id #_ //
992       |48,49,50,53,54: #x #id #_ //
993       ]
994     ]
995   ]
996  ]
997| lapply (refl ? add_instr) cases add_instr in ⊢ (???% → %);
998  [ #Ha #i >append_length >commutative_plus #Hi normalize in Hi;
999    cases (le_to_or_lt_eq … Hi) -Hi; #Hi
1000    [ cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)
1001      #x #y @((proj2 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
1002    | normalize nodelta >(injective_S … Hi)
1003      >lookup_opt_lookup_miss
1004      [ >lookup_opt_lookup_miss
1005        [ //
1006        | cases (lookup ?? (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)
1007          #x #y normalize nodelta
1008          >(proj1 ?? (proj1 ?? (sig2 ?? policy)) (|prefix|) (le_n (|prefix|))) //
1009        ]
1010      | >(proj1 ?? (jump_not_in_policy program old_policy (|prefix|) ?))
1011        [ //
1012        | >prf >p1 >nth_append_second [ <minus_n_n
1013        generalize in match Ha; normalize nodelta cases instr normalize nodelta
1014        [1: #pi cases pi
1015         [1,2,3,6,7,33,34: #x #y #H normalize /2 by nmk/
1016         |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #H normalize /2 by nmk/
1017         |35,36,37: #H normalize /2 by nmk/
1018         |9,10,14,15: #id whd in match (jump_expansion_step_instruction ???);
1019           #H destruct (H)
1020         |11,12,13,16,17: #x #id whd in match (jump_expansion_step_instruction ???);
1021           #H destruct (H)
1022         ]
1023        |2,3: #x #H normalize /2 by nmk/
1024        |6: #x #y #H normalize /2 by nmk/
1025        |4,5: #id #H destruct (H)
1026        ] | @le_n ]
1027        | >prf >append_length normalize <plus_n_Sm @le_plus_n_r
1028        ]
1029      ]
1030    ]
1031  | #x #Ha #i >append_length >commutative_plus #Hi normalize in Hi;
1032    cases (le_to_or_lt_eq … Hi) -Hi; #Hi
1033    [ cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)
1034      #y #z normalize nodelta normalize nodelta >lookup_insert_miss
1035      [ @((proj2 ?? (sig2 ?? policy)) i (le_S_S_to_le … Hi))
1036      | @bitvector_of_nat_abs @lt_to_not_eq @(le_S_S_to_le … Hi)
1037      ]
1038    | >(injective_S … Hi) elim (proj1 ?? (proj2 ?? (sig2 ?? old_policy) (|prefix|) ?) ?)
1039      [ #a #H elim H -H; #b #H >H >(lookup_opt_lookup_hit … 〈a,b〉 H)
1040        normalize nodelta >lookup_insert_hit @jmpleq_max_length
1041      | >prf >p1 >nth_append_second
1042        [ <minus_n_n generalize in match Ha; normalize nodelta cases instr normalize nodelta
1043          [1: #pi cases pi
1044           [1,2,3,6,7,33,34: #x #y #H normalize in H; destruct (H)
1045           |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)
1046           |35,36,37: #H normalize in H; destruct (H)
1047           |9,10,14,15: #id #H //
1048           |11,12,13,16,17: #x #id #H //
1049           ]
1050          |2,3: #x #H normalize in H; destruct (H)
1051          |6: #x #y #H normalize in H; destruct (H)
1052          |4,5: #id #H //
1053          ]
1054        | @le_n ]
1055      | >prf >append_length normalize <plus_n_Sm @le_plus_n_r
1056      ]
1057    ]
1058  ] ]
1059| @conj [ @conj
1060  [ #i #Hi //
1061  | #i #Hi @conj [ >nth_nil #H @⊥ @H | #H elim H #x #H1 elim H1 #y #H2
1062                   normalize in H2; destruct (H2) ]
1063  ]                 
1064  | #i #Hi @⊥ @(absurd (i<0)) [ @Hi | @(not_le_Sn_O) ]
1065]
1066qed.
1067 
1068let rec jump_expansion_internal (program: list labelled_instruction)
1069  (n: ℕ) on n: (Σpolicy:jump_expansion_policy.
1070    And
1071    (∀i:ℕ.i ≥ |program| → lookup_opt ? 16 (bitvector_of_nat ? i) policy = None ?)
1072    (jump_in_policy program policy)) ≝
1073  match n with
1074  [ O   ⇒ jump_expansion_start program
1075  | S m ⇒ jump_expansion_step program (jump_expansion_internal program m)
1076  ].
1077[ @(sig2 … (jump_expansion_start program))
1078| @(proj1 … (sig2 … (jump_expansion_step program (jump_expansion_internal program m))))
1079]
1080qed.
1081
1082definition policy_equal ≝
1083  λprogram:list labelled_instruction.λp1,p2:jump_expansion_policy.
1084  ∀n:ℕ.n < |program| →
1085    (\snd (bvt_lookup … (bitvector_of_nat 16 n) p1 〈0,short_jump〉)) =
1086    (\snd (bvt_lookup … (bitvector_of_nat 16 n) p2 〈0,short_jump〉)).
1087
1088lemma pe_refl:
1089  ∀program.reflexive ? (policy_equal program).
1090 #program whd #x whd #n #Hn @refl
1091qed.
1092
1093lemma pe_sym:
1094  ∀program.symmetric ? (policy_equal program).
1095 #program whd #x #y #Hxy whd #n #Hn
1096 >(Hxy n Hn) @refl
1097qed.
1098
1099lemma pe_trans:
1100  ∀program.transitive ? (policy_equal program).
1101 #program whd #x #y #z #Hxy #Hyz whd #n #Hn
1102 >(Hxy n Hn) @(Hyz n Hn)
1103qed.
1104
1105lemma le_plus:
1106  ∀n,m:ℕ.n ≤ m → ∃k:ℕ.m = n + k.
1107 #n #m elim m -m;
1108 [ #Hn % [ @O | <(le_n_O_to_eq n Hn) // ]
1109 | #m #Hind #Hn cases (le_to_or_lt_eq … Hn) -Hn; #Hn
1110   [ elim (Hind (le_S_S_to_le … Hn)) #k #Hk % [ @(S k) | >Hk // ]
1111   | % [ @O | <Hn // ]
1112   ]
1113 ]
1114qed.
1115
1116theorem plus_Sn_m1: ∀n,m:nat. S m + n = m + S n.
1117#n (elim n) normalize /2 by S_pred/ qed.
1118
1119lemma pe_step: ∀program:list labelled_instruction.
1120 ∀p1,p2:Σpolicy.
1121 (∀i:ℕ.i ≥ |program| → lookup_opt … (bitvector_of_nat ? i) policy = None ?)
1122 ∧jump_in_policy program policy.
1123  policy_equal program p1 p2 →
1124  policy_equal program (jump_expansion_step program p1) (jump_expansion_step program p2).
1125 #program #p1 #p2 #Heq whd #n #Hn lapply (Heq n Hn) #H
1126 lapply (refl ? (lookup_opt … (bitvector_of_nat ? n) p1))
1127 cases (lookup_opt … (bitvector_of_nat ? n) p1) in ⊢ (???% → ?);
1128 [ #Hl lapply ((proj2 ?? (jump_not_in_policy program p1 n Hn)) Hl)
1129   #Hnotjmp >lookup_opt_lookup_miss
1130   [ >lookup_opt_lookup_miss
1131     [ @refl
1132     | @(proj1 ?? (jump_not_in_policy program (eject … (jump_expansion_step program p2)) n Hn))
1133       [ @(proj1 ?? (sig2 … (jump_expansion_step program p2)))
1134       | @Hnotjmp
1135       ]
1136     ]
1137   | @(proj1 ?? (jump_not_in_policy program (eject … (jump_expansion_step program p1)) n Hn))
1138     [ @(proj1 ?? (sig2 ?? (jump_expansion_step program p1)))
1139     | @Hnotjmp
1140     ]
1141   ]
1142 | #x #Hl cases daemon
1143 ]
1144qed.
1145   
1146lemma equal_remains_equal: ∀program:list labelled_instruction.∀n:ℕ.
1147  policy_equal program (jump_expansion_internal program n) (jump_expansion_internal program (S n)) →
1148  ∀k.k ≥ n → policy_equal program (jump_expansion_internal program n) (jump_expansion_internal program k).
1149 #program #n #Heq #k #Hk elim (le_plus … Hk); #z #H >H -H -Hk -k;
1150 lapply Heq -Heq; lapply n -n; elim z -z;
1151 [ #n #Heq <plus_n_O @pe_refl 
1152 | #z #Hind #n #Heq <plus_Sn_m1 whd in match (plus (S n) z); @(pe_trans … (jump_expansion_internal program (S n)))
1153   [ @Heq
1154   | @pe_step @Hind @Heq
1155   ]
1156 ]
1157qed.
1158
1159lemma dec_bounded_forall:
1160  ∀P:ℕ → Prop.(∀n.(P n) + (¬P n)) → ∀k.(∀n.n < k → P n) + ¬(∀n.n < k → P n).
1161 #P #HP_dec #k elim k -k
1162 [ %1 #n #Hn @⊥ @(absurd (n < 0) Hn) @not_le_Sn_O
1163 | #k #Hind cases Hind
1164   [ #Hk cases (HP_dec k)
1165     [ #HPk %1 #n #Hn cases (le_to_or_lt_eq … Hn)
1166       [ #H @(Hk … (le_S_S_to_le … H))
1167       | #H >(injective_S … H) @HPk
1168       ]
1169     | #HPk %2 @nmk #Habs @(absurd (P k)) [ @(Habs … (le_n (S k))) | @HPk ]
1170     ]
1171   | #Hk %2 @nmk #Habs @(absurd (∀n.n<k→P n)) [ #n' #Hn' @(Habs … (le_S … Hn')) | @Hk ]
1172   ]
1173 ]
1174qed.
1175
1176lemma dec_bounded_exists:
1177  ∀P:ℕ→Prop.(∀n.(P n) + (¬P n)) → ∀k.(∃n.n < k ∧ P n) + ¬(∃n.n < k ∧ P n).
1178 #P #HP_dec #k elim k -k
1179 [ %2 @nmk #Habs elim Habs #n #Hn @(absurd (n < 0) (proj1 … Hn)) @not_le_Sn_O
1180 | #k #Hind cases Hind
1181   [ #Hk %1 elim Hk #n #Hn @(ex_intro … n) @conj [ @le_S @(proj1 … Hn) | @(proj2 … Hn) ]
1182   | #Hk cases (HP_dec k)
1183     [ #HPk %1 @(ex_intro … k) @conj [ @le_n | @HPk ]
1184     | #HPk %2 @nmk #Habs elim Habs #n #Hn cases (le_to_or_lt_eq … (proj1 … Hn))
1185       [ #H @(absurd (∃n.n < k ∧ P n)) [ @(ex_intro … n) @conj
1186         [ @(le_S_S_to_le … H) | @(proj2 … Hn) ] | @Hk ]
1187       | #H @(absurd (P k)) [ <(injective_S … H) @(proj2 … Hn) | @HPk ]
1188       ] 
1189     ]
1190   ]
1191 ]
1192qed.
1193
1194lemma not_exists_forall:
1195  ∀A:Type[0].∀P:A → Prop.¬(∃x.P x) → ∀x.¬P x.
1196 #A #P #Hex #x @nmk #Habs @(absurd ? ? Hex) @(ex_intro … x) @Habs
1197qed.
1198
1199lemma de_morgan1:
1200 ∀A,B:Prop.¬(A ∧ ¬B) → A → ¬¬B.
1201 #A #B #Hnot #HA @nmk #H @(absurd (A∧¬B)) [ % [ @HA | @H ] | @Hnot ]
1202qed.
1203
1204lemma thingie:
1205  ∀A:Prop.(A + ¬A) → (¬¬A) → A.
1206 #A #Adec #nnA cases Adec
1207 [ //
1208 | #H @⊥ @(absurd (¬A) H nnA)
1209 ]
1210qed.
1211 
1212lemma dec_eq_jump_length: ∀a,b:jump_length.(a = b) + (a ≠ b).
1213  #a #b cases a cases b /2/
1214  %2 @nmk #H destruct (H)
1215qed.
1216
1217lemma incr_or_equal: ∀program:list labelled_instruction.
1218  ∀policy:(Σp:jump_expansion_policy.
1219    (∀i.i ≥ |program| → lookup_opt … (bitvector_of_nat ? i) p = None ?) ∧
1220    jump_in_policy program p).
1221  policy_equal program policy (jump_expansion_step program policy) ∨
1222  ∃n:ℕ.jmple
1223    (\snd (bvt_lookup … (bitvector_of_nat ? n) policy 〈0,short_jump〉))
1224    (\snd (bvt_lookup … (bitvector_of_nat ? n) (jump_expansion_step program policy) 〈0,short_jump〉)).
1225 #program #policy cases (dec_bounded_exists
1226   (λk.
1227     \snd (bvt_lookup ?? (bitvector_of_nat ? k) policy 〈0,short_jump〉) ≠
1228     \snd (bvt_lookup ?? (bitvector_of_nat ? k) (jump_expansion_step program policy) 〈0,short_jump〉))
1229   ? (|program|))
1230   [ #H %2 elim H -H; #i #Hi
1231     cases (proj2 ?? (sig2 ?? (jump_expansion_step program policy)) i (proj1 ?? Hi))
1232     [ #H @(ex_intro … i H)
1233     | #H @⊥ @(absurd ? H (proj2 ?? Hi))
1234     ]
1235   | #H %1 whd #i #Hi @(thingie ? (dec_eq_jump_length ??)) elim H -H #H; @nmk #H2 @H
1236     @(ex_intro … i) @conj [ @Hi | @H2 ]
1237   | #n cases (dec_eq_jump_length (\snd (lookup ?? (bitvector_of_nat ? n) policy 〈0,short_jump〉))
1238     (\snd (lookup ?? (bitvector_of_nat ? n) (jump_expansion_step program policy) 〈0,short_jump〉)))
1239     [ #H %2 @nmk #H1 elim H1 #H2 @H2 @H
1240     | #H %1 @H
1241     ]
1242   ]
1243qed. 
1244
1245(**************************************** START OF POLICY ABSTRACTION ********************)
1246
1247definition policy_type ≝ Word → jump_length.
1248
1249definition jump_expansion': pseudo_assembly_program → policy_type ≝
1250 λprogram.λpc.
1251  let policy ≝ jump_expansion_internal (\snd program) (|\snd program|) in
1252  let 〈n,j〉 ≝ lookup ? ? pc policy 〈0, long_jump〉 in
1253    j.
1254 
1255definition assembly_1_pseudoinstruction_safe ≝
1256  λprogram: pseudo_assembly_program.
1257  λjump_expansion: policy_type.
1258  λppc: Word.
1259  λpc: Word.
1260  λlookup_labels.
1261  λlookup_datalabels.
1262  λi.
1263  let expansion ≝ jump_expansion ppc in
1264    match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc expansion i with
1265    [ None ⇒ None ?
1266    | Some pseudos ⇒
1267      let mapped ≝ map ? ? assembly1 pseudos in
1268      let flattened ≝ flatten ? mapped in
1269      let pc_len ≝ length ? flattened in
1270        Some ? (〈pc_len, flattened〉)
1271    ].
1272 
1273definition construct_costs_safe ≝
1274  λprogram.
1275  λjump_expansion: policy_type.
1276  λpseudo_program_counter: nat.
1277  λprogram_counter: nat.
1278  λcosts: BitVectorTrie costlabel 16.
1279  λi.
1280  match i with
1281  [ Cost cost ⇒
1282    let program_counter_bv ≝ bitvector_of_nat ? program_counter in
1283      Some ? 〈program_counter, (insert … program_counter_bv cost costs)〉
1284  | _ ⇒
1285    let pc_bv ≝ bitvector_of_nat ? program_counter in
1286    let ppc_bv ≝ bitvector_of_nat ? pseudo_program_counter in
1287    let lookup_labels ≝ λx.pc_bv in
1288    let lookup_datalabels ≝ λx.zero ? in
1289    let pc_delta_assembled ≝
1290      assembly_1_pseudoinstruction_safe program jump_expansion ppc_bv pc_bv
1291        lookup_labels lookup_datalabels i
1292    in
1293    match pc_delta_assembled with
1294    [ None ⇒ None ?
1295    | Some pc_delta_assembled ⇒
1296      let 〈pc_delta, assembled〉 ≝ pc_delta_assembled in
1297        Some ? 〈pc_delta + program_counter, costs〉       
1298    ]
1299  ].
1300
1301(* This establishes the correspondence between pseudo program counters and
1302   program counters. It is at the heart of the proof. *)
1303(*CSC: code taken from build_maps *)
1304definition sigma00: pseudo_assembly_program → policy_type → list ? → ? → option (nat × (nat × (BitVectorTrie Word 16))) ≝
1305 λinstr_list.
1306 λjump_expansion: policy_type.
1307 λl:list labelled_instruction.
1308 λacc.
1309  foldl …
1310   (λt,i.
1311       match t with
1312       [ None ⇒ None …
1313       | Some ppc_pc_map ⇒
1314         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
1315         let 〈program_counter, sigma_map〉 ≝ pc_map in
1316         let 〈label, i〉 ≝ i in
1317          match construct_costs_safe instr_list jump_expansion ppc program_counter (Stub …) i with
1318           [ None ⇒ None ?
1319           | Some pc_ignore ⇒
1320              let 〈pc,ignore〉 ≝ pc_ignore in
1321                Some … 〈S ppc, 〈pc, insert ?? (bitvector_of_nat 16 ppc) (bitvector_of_nat 16 pc) sigma_map〉〉 ]
1322       ]) acc l.
1323
1324definition sigma0: pseudo_assembly_program → policy_type → option (nat × (nat × (BitVectorTrie Word 16))) ≝
1325  λprog.
1326  λjump_expansion.
1327    sigma00 prog jump_expansion (\snd prog) (Some ? 〈0, 〈0, Stub …〉〉).
1328
1329definition tech_pc_sigma00: pseudo_assembly_program → policy_type → list labelled_instruction → option (nat × nat) ≝
1330 λprogram,jump_expansion,instr_list.
1331  match sigma00 program jump_expansion instr_list (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (* acc copied from sigma0 *) with
1332   [ None ⇒ None …
1333   | Some result ⇒
1334      let 〈ppc,pc_sigma_map〉 ≝ result in
1335      let 〈pc,map〉 ≝ pc_sigma_map in
1336       Some … 〈ppc,pc〉 ].
1337
1338definition sigma_safe: pseudo_assembly_program → policy_type → option (Word → Word) ≝
1339 λinstr_list,jump_expansion.
1340  match sigma0 instr_list jump_expansion with
1341  [ None ⇒ None ?
1342  | Some result ⇒
1343    let 〈ppc,pc_sigma_map〉 ≝ result in
1344    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
1345      if gtb pc (2^16) then
1346        None ?
1347      else
1348        Some ? (λx. lookup … x sigma_map (zero …)) ].
1349
1350(* stuff about policy *)
1351
1352definition policy_ok ≝ λjump_expansion,p. sigma_safe p jump_expansion ≠ None ….
1353
1354definition policy ≝ λp. Σjump_expansion:policy_type. policy_ok jump_expansion p.
1355
1356lemma eject_policy: ∀p. policy p → policy_type.
1357 #p #pol @(eject … pol)
1358qed.
1359
1360coercion eject_policy nocomposites: ∀p.∀pol:policy p. policy_type ≝ eject_policy on _pol:(policy ?) to policy_type.
1361
1362definition sigma: ∀p:pseudo_assembly_program. policy p → Word → Word ≝
1363 λp,policy.
1364  match sigma_safe p (eject … policy) return λr:option (Word → Word). r ≠ None … → Word → Word with
1365   [ None ⇒ λabs. ⊥
1366   | Some r ⇒ λ_.r] (sig2 … policy).
1367 cases abs /2/
1368qed.
1369
1370example sigma_0: ∀p,pol. sigma p pol (bitvector_of_nat ? 0) = bitvector_of_nat ? 0.
1371 cases daemon.
1372qed.
1373
1374axiom fetch_pseudo_instruction_split:
1375 ∀instr_list,ppc.
1376  ∃pre,suff,lbl.
1377   (pre @ [〈lbl,\fst (fetch_pseudo_instruction instr_list ppc)〉]) @ suff = instr_list.
1378
1379lemma sigma00_append:
1380 ∀instr_list,jump_expansion,l1,l2,acc.
1381  sigma00 instr_list jump_expansion (l1@l2) acc =
1382   sigma00 instr_list jump_expansion
1383    l2 (sigma00 instr_list jump_expansion l1 acc).
1384 whd in match sigma00; normalize nodelta;
1385 #instr_list #jump_expansion #l1 #l2 #acc @foldl_append
1386qed.
1387
1388lemma sigma00_strict:
1389 ∀instr_list,jump_expansion,l,acc. acc = None ? →
1390  sigma00 instr_list jump_expansion l acc = None ….
1391 #instr_list #jump_expansion #l elim l
1392  [ #acc #H >H %
1393  | #hd #tl #IH #acc #H >H change with (sigma00 ?? tl ? = ?) @IH % ]
1394qed.
1395
1396lemma policy_ok_prefix_ok:
1397 ∀program.∀pol:policy program.∀suffix,prefix.
1398  prefix@suffix = \snd program →
1399   sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) ≠ None ….
1400 * #preamble #instr_list #pol #suffix #prefix #prf whd in prf:(???%);
1401 generalize in match (sig2 ?? pol); whd in prf:(???%); <prf in pol; #pol
1402 whd in match policy_ok; whd in match sigma_safe; whd in match sigma0;
1403 normalize nodelta >sigma00_append
1404 cases (sigma00 ?? prefix ?)
1405  [2: #x #_ % #abs destruct(abs)
1406  | * #abs @⊥ @abs >sigma00_strict % ]
1407qed.
1408
1409lemma policy_ok_prefix_hd_ok:
1410 ∀program.∀pol:policy program.∀suffix,hd,prefix,ppc_pc_map.
1411  (prefix@[hd])@suffix = \snd program →
1412   Some ? ppc_pc_map = sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) →
1413    let 〈ppc,pc_map〉 ≝ ppc_pc_map in
1414    let 〈program_counter, sigma_map〉 ≝ pc_map in
1415    let 〈label, i〉 ≝ hd in
1416     construct_costs_safe program pol ppc program_counter (Stub …) i ≠ None ….
1417 * #preamble #instr_list #pol #suffix #hd #prefix #ppc_pc_map #EQ1 #EQ2
1418 generalize in match (policy_ok_prefix_ok 〈preamble,instr_list〉 pol suffix
1419  (prefix@[hd]) EQ1) in ⊢ ?; >sigma00_append <EQ2 whd in ⊢ (?(??%?) → ?);
1420 @pair_elim' #ppc #pc_map #EQ3 normalize nodelta
1421 @pair_elim' #pc #map #EQ4 normalize nodelta
1422 @pair_elim' #l' #i' #EQ5 normalize nodelta
1423 cases (construct_costs_safe ??????) normalize
1424  [* #abs @⊥ @abs % | #X #_ % #abs destruct(abs)]
1425qed.
1426
1427definition expand_pseudo_instruction:
1428 ∀program:pseudo_assembly_program.∀pol: policy program.
1429  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pc.
1430  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1431  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1432  let pi ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in
1433  pc = sigma program pol ppc →
1434  Σres:list instruction. Some … res = expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) pi
1435≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pc,prf1,prf2,prf3.
1436   match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) (\fst (fetch_pseudo_instruction (\snd program) ppc)) with
1437    [ None ⇒ let dummy ≝ [ ] in dummy
1438    | Some res ⇒ res ].
1439 [ @⊥ whd in p:(??%??);
1440   generalize in match (sig2 ?? pol); whd in ⊢ (% → ?);
1441   whd in ⊢ (?(??%?) → ?); change with (sigma00 ????) in ⊢ (?(??(match % with [_ ⇒ ? | _ ⇒ ?])?) → ?);
1442   generalize in match (refl … (sigma00 program pol (\snd program) (Some ? 〈O,〈O,Stub (BitVector 16) 16〉〉)));
1443   cases (sigma00 ????) in ⊢ (??%? → %); normalize nodelta [#_ * #abs @abs %]
1444   #res #K
1445   cases (fetch_pseudo_instruction_split (\snd program) ppc) #pre * #suff * #lbl #EQ1
1446   generalize in match (policy_ok_prefix_hd_ok program pol … EQ1 ?) in ⊢ ?;
1447   cases daemon (* CSC: XXXXXXXX Ero qui
1448   
1449    [3: @policy_ok_prefix_ok ]
1450    | sigma00 program pol pre
1451
1452
1453
1454   QUA USARE LEMMA policy_ok_prefix_hd_ok combinato a lemma da fare che
1455   fetch ppc = hd sse program = pre @ [hd] @ tl e |pre| = ppc
1456   per concludere construct_costs_safe ≠ None *)
1457 | >p %]
1458qed.
1459
1460(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1461definition assembly_1_pseudoinstruction':
1462 ∀program:pseudo_assembly_program.∀pol: policy program.
1463  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1464  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1465  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1466  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
1467  Σres:(nat × (list Byte)).
1468   Some … res = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi ∧
1469   let 〈len,code〉 ≝ res in
1470    sigma program pol (\snd (half_add ? ppc (bitvector_of_nat ? 1))) =
1471     bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len)
1472≝ λprogram: pseudo_assembly_program.
1473  λpol: policy program.
1474  λppc: Word.
1475  λlookup_labels.
1476  λlookup_datalabels.
1477  λpi.
1478  λprf1,prf2,prf3.
1479   match assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi with
1480    [ None ⇒ let dummy ≝ 〈0,[ ]〉 in dummy
1481    | Some res ⇒ res ].
1482 [ @⊥ elim pi in p; [*]
1483   try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs
1484   generalize in match (jmeq_to_eq ??? abs); -abs; #abs whd in abs:(??%?); try destruct(abs)
1485   whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?);
1486   (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *)
1487   cases daemon
1488 | % [ >p %]
1489   cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta;
1490   (* THIS SHOULD BE TRUE INSTEAD *)
1491   cases daemon]
1492qed.
1493
1494definition assembly_1_pseudoinstruction:
1495 ∀program:pseudo_assembly_program.∀pol: policy program.
1496  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1497  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1498  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1499  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
1500   nat × (list Byte)
1501≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pi,prf1,prf2,prf3.
1502   assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1
1503    prf2 prf3.
1504
1505lemma assembly_1_pseudoinstruction_ok1:
1506 ∀program:pseudo_assembly_program.∀pol: policy program.
1507  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1508  ∀prf1:lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)).
1509  ∀prf2:lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)).
1510  ∀prf3:\fst (fetch_pseudo_instruction (\snd program) ppc) = pi.
1511     Some … (assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3)
1512   = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi.
1513 #program #pol #ppc #lookup_labels #lookup_datalabels #pi #prf1 #prf2 #prf3
1514 cases (sig2 … (assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3))
1515 #H1 #_ @H1
1516qed.
1517
1518(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1519definition construct_costs':
1520 ∀program. ∀pol:policy program. ∀ppc,pc,costs,i.
1521  Σres:(nat × (BitVectorTrie costlabel 16)). Some … res = construct_costs_safe program pol ppc pc costs i
1522
1523  λprogram.λpol: policy program.λppc,pc,costs,i.
1524   match construct_costs_safe program pol ppc pc costs i with
1525    [ None ⇒ let dummy ≝ 〈0, Stub costlabel 16〉 in dummy
1526    | Some res ⇒ res ].
1527 [ cases daemon
1528 | >p %]
1529qed.
1530
1531definition construct_costs ≝
1532 λprogram,pol,ppc,pc,costs,i. eject … (construct_costs' program pol ppc pc costs i).
1533
1534(*
1535axiom suffix_of: ∀A:Type[0]. ∀l,prefix:list A. list A.
1536axiom suffix_of_ok: ∀A,l,prefix. prefix @ suffix_of A l prefix = l.
1537
1538axiom foldl_strong_step:
1539 ∀A:Type[0].
1540  ∀P: list A → Type[0].
1541   ∀l: list A.
1542    ∀H: ∀prefix,hd,tl. l =  prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]).
1543     ∀acc: P [ ].
1544      ∀Q: ∀prefix. P prefix → Prop.
1545       ∀HQ: ∀prefix,hd,tl.∀prf: l = prefix @ [hd] @ tl.
1546        ∀acc: P prefix. Q prefix acc → Q (prefix @ [hd]) (H prefix hd tl prf acc).
1547       Q [ ] acc →
1548        Q l (foldl_strong A P l H acc).
1549(*
1550 #A #P #l #H #acc #Q #HQ #Hacc normalize;
1551 generalize in match
1552  (foldl_strong ?
1553   (λpre. Q pre (foldl_strong_internal A P l (suffix_of A l pre) ? [ ] pre acc ?))
1554   l ? Hacc)
1555 [3: >suffix_of_ok % | 2: #prefix #hd #tl #EQ @(H prefix hd (tl@suffix_of A l pre) EQ) ]
1556 [2: #prefix #hd #tl #prf #X whd in ⊢ (??%)
1557 #K
1558
1559 generalize in match
1560  (foldl_strong ?
1561   (λpre. Q pre (foldl_strong_internal A P l H pre (suffix_of A l pre) acc (suffix_of_ok A l pre))))
1562 [2: @H
1563*)
1564
1565axiom foldl_elim:
1566 ∀A:Type[0].
1567  ∀B: Type[0].
1568   ∀H: A → B → A.
1569    ∀acc: A.
1570     ∀l: list B.
1571      ∀Q: A → Prop.
1572       (∀acc:A.∀b:B. Q acc → Q (H acc b)) →
1573         Q acc →
1574          Q (foldl A B H acc l).
1575*)
1576
1577lemma option_destruct_Some: ∀A,a,b. Some A a = Some A b → a=b.
1578 #A #a #b #EQ destruct //
1579qed.
1580
1581(*
1582lemma tech_pc_sigma00_append_Some:
1583 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
1584  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
1585   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
1586 #program #pol #prefix #costs #label #i #ppc #pc #H
1587  whd in match tech_pc_sigma00 in ⊢ %; normalize nodelta;
1588  whd in match sigma00 in ⊢ %; normalize nodelta in ⊢ %;
1589  generalize in match (sig2 … pol) whd in ⊢ (% → ?) whd in ⊢ (?(??%?) → ?)
1590  whd in match sigma0; normalize nodelta;
1591  >foldl_step
1592  change with (? → match match sigma00 program pol prefix with [None ⇒ ? | Some res ⇒ ?] with [ None ⇒ ? | Some res ⇒ ? ] = ?)
1593  whd in match tech_pc_sigma00 in H; normalize nodelta in H;
1594  cases (sigma00 program pol prefix) in H ⊢ %
1595   [ whd in ⊢ (??%% → ?) #abs destruct(abs)
1596   | * #ppc' * #pc' #sigma_map normalize nodelta; #H generalize in match (option_destruct_Some ??? H)
1597     
1598     normalize nodelta; -H;
1599     
1600 
1601   generalize in match H; -H;
1602  generalize in match (foldl ?????); in H ⊢ (??match match % with [_ ⇒ ? | _ ⇒ ?] with [_ ⇒ ? | _ ⇒ ?]?)
1603   [2: whd in ⊢ (??%%)
1604XXX
1605*)
1606
1607axiom construct_costs_sigma:
1608 ∀p.∀pol:policy p.∀ppc,pc,costs,i.
1609  bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) →
1610   bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)).
1611
1612axiom tech_pc_sigma00_append_Some:
1613 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
1614  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
1615   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
1616
1617lemma eq_identifier_eq:
1618  ∀tag: String.
1619  ∀l.
1620  ∀r.
1621    eq_identifier tag l r = true → l = r.
1622  #tag #l #r cases l cases r #posl #posr
1623  cases daemon
1624qed.
1625
1626definition build_maps:
1627 ∀pseudo_program.∀pol:policy pseudo_program.
1628  Σres:((identifier_map ASMTag Word) × (BitVectorTrie costlabel 16)).
1629   let 〈labels, costs〉 ≝ res in
1630    ∀id. occurs_exactly_once id (\snd pseudo_program) →
1631     lookup_def … labels id (zero ?) = sigma pseudo_program pol (address_of_word_labels_code_mem (\snd pseudo_program) id) ≝
1632  λpseudo_program.
1633  λpol:policy pseudo_program.
1634    let result ≝
1635      foldl_strong
1636        (option Identifier × pseudo_instruction)
1637          (λpre. Σres:((identifier_map ASMTag Word) × (nat × (nat × (BitVectorTrie costlabel 16)))).
1638            let 〈labels,ppc_pc_costs〉 ≝ res in
1639            let 〈ppc',pc_costs〉 ≝ ppc_pc_costs in
1640            let 〈pc',costs〉 ≝ pc_costs in
1641              And ( And (
1642              And (bitvector_of_nat ? pc' = sigma pseudo_program pol (bitvector_of_nat ? ppc')) (*∧*)
1643                (ppc' = length … pre)) (*∧ *)
1644                (tech_pc_sigma00 pseudo_program (eject … pol) pre = Some ? 〈ppc',pc'〉)) (*∧*)
1645                (∀id. occurs_exactly_once id pre →
1646                  lookup_def … labels id (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem pre id)))
1647                (\snd pseudo_program)
1648        (λprefix,i,tl,prf,t.
1649          let 〈labels, ppc_pc_costs〉 ≝ t in
1650          let 〈ppc, pc_costs〉 ≝ ppc_pc_costs in
1651          let 〈pc,costs〉 ≝ pc_costs in
1652          let 〈label, i'〉 ≝ i in
1653          let labels ≝
1654            match label with
1655            [ None ⇒ labels
1656            | Some label ⇒
1657                let program_counter_bv ≝ bitvector_of_nat ? pc in
1658                  add ? ? labels label program_counter_bv
1659            ]
1660          in
1661            let construct ≝ construct_costs pseudo_program pol ppc pc costs i' in
1662              〈labels, 〈S ppc,construct〉〉) 〈empty_map …, 〈0, 〈0, Stub ? ?〉〉〉
1663    in
1664      let 〈labels, ppc_pc_costs〉 ≝ result in
1665      let 〈ppc,pc_costs〉 ≝ ppc_pc_costs in
1666      let 〈pc, costs〉 ≝ pc_costs in
1667        〈labels, costs〉.
1668 [2: whd generalize in match (sig2 … t); >p >p1 >p2 >p3 * * * #IHn1 #IH0 #IH1 #IH2
1669   generalize in match (refl … construct); cases construct in ⊢ (???% → %); #PC #CODE #JMEQ % [% [%]]
1670   [ <(construct_costs_sigma … costs i1 IHn1) change with (? = ?(\fst construct)) >JMEQ %
1671   | >append_length <IH0 normalize; -IHn1; (*CSC: otherwise it diverges!*) //
1672   | >(tech_pc_sigma00_append_Some … costs … IH1) change with (Some … 〈S ppc,\fst construct〉 = ?) >JMEQ %
1673   | #id normalize nodelta; -labels1; cases label; normalize nodelta
1674     [ #K <address_of_word_labels_code_mem_None [2:@K] @IH2 -IHn1; (*CSC: otherwise it diverges!*) //
1675     | #l #H generalize in match (occurs_exactly_once_Some ???? H) in ⊢ ?;
1676       generalize in match (refl … (eq_identifier ? l id)); cases (eq_identifier … l id) in ⊢ (???% → %);
1677        [ #EQ #_ <(eq_identifier_eq … EQ) check lookup_add_hit. >lookup_insert_hit >address_of_word_labels_code_mem_Some_hit
1678          <IH0 [@IHn1 | <(eq_bv_eq … EQ) in H #H @H]
1679        | #EQ change with (occurs_exactly_once ?? → ?) #K >lookup_insert_miss [2: -IHn1; (*Andrea:XXXX used to work /2/*)>eq_bv_sym >EQ // ]
1680          <(address_of_word_labels_code_mem_Some_miss … EQ … H) @IH2 -IHn1; //]]]
1681 |3: whd % [% [%]] // [>sigma_0//] #id normalize in ⊢ (% → ?); #abs @⊥ //
1682 | generalize in match (sig2 … result) in ⊢ ?;
1683   normalize nodelta in p ⊢ %; -result; >p in ⊢ (match % with [_ ⇒ ?] → ?);
1684   normalize nodelta; >p1 normalize nodelta; >p2; normalize nodelta; * #_; #H @H]
1685qed.
1686
1687definition build_maps:
1688 ∀pseudo_program.∀pol:policy pseudo_program.
1689  Σres:((BitVectorTrie Identifier 16) × (BitVectorTrie costlabel 16)).
1690   let 〈labels,costs〉 ≝ res in
1691    ∀id. occurs_exactly_once id (\snd pseudo_program) →
1692     lookup ? ? id labels (zero …) = ? (* sigma pseudo_program pol (address_of_word_labels_code_mem (\snd pseudo_program) id) *)
1693≝ ?.
1694  λpseudo_program.λpol:policy pseudo_program.
1695  let result ≝
1696   foldl_strong
1697    (option Identifier × pseudo_instruction)
1698    (λpre. Σres:((BitVectorTrie Word 16) × (nat × (nat × (BitVectorTrie costlabel 16)))).
1699      let 〈labels,ppc_pc_costs〉 ≝ res in
1700      let 〈ppc',pc_costs〉 ≝ ppc_pc_costs in
1701      let 〈pc',costs〉 ≝ pc_costs in
1702       And ( And (
1703       And (bitvector_of_nat ? pc' = sigma pseudo_program pol (bitvector_of_nat ? ppc')) (*∧*)
1704       (ppc' = length … pre)) (*∧ *)
1705       (tech_pc_sigma00 pseudo_program (eject … pol) pre = Some ? 〈ppc',pc'〉)) (*∧*)
1706       (∀id. occurs_exactly_once id pre →
1707        lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem pre id)))
1708    (\snd pseudo_program)
1709    (λprefix,i,tl,prf,t.
1710      let 〈labels, ppc_pc_costs〉 ≝ t in
1711      let 〈ppc, pc_costs〉 ≝ ppc_pc_costs in
1712      let 〈pc,costs〉 ≝ pc_costs in
1713       let 〈label, i'〉 ≝ i in
1714       let labels ≝
1715         match label with
1716         [ None ⇒ labels
1717         | Some label ⇒
1718           let program_counter_bv ≝ bitvector_of_nat ? pc in
1719             insert ? ? label program_counter_bv labels]
1720       in
1721         let construct ≝ construct_costs pseudo_program pol ppc pc costs i' in
1722          〈labels, 〈S ppc,construct〉〉
1723    ) 〈(Stub ? ?), 〈0, 〈0, Stub ? ?〉〉〉
1724  in
1725   let 〈labels, ppc_pc_costs〉 ≝ result in
1726   let 〈ppc,pc_costs〉 ≝ ppc_pc_costs in
1727   let 〈pc, costs〉 ≝ pc_costs in
1728    〈labels, costs〉.
1729 [2: whd generalize in match (sig2 … t) >p >p1 >p2 >p3 * * * #IHn1 #IH0 #IH1 #IH2
1730   generalize in match (refl … construct); cases construct in ⊢ (???% → %) #PC #CODE #JMEQ % [% [%]]
1731   [ <(construct_costs_sigma … costs i1 IHn1) change with (? = ?(\fst construct)) >JMEQ %
1732   | >append_length <IH0 normalize; -IHn1; (*CSC: otherwise it diverges!*) //
1733   | >(tech_pc_sigma00_append_Some … costs … IH1) change with (Some … 〈S ppc,\fst construct〉 = ?) >JMEQ %
1734   | #id normalize nodelta; -labels1; cases label; normalize nodelta
1735     [ #K <address_of_word_labels_code_mem_None [2:@K] @IH2 -IHn1; (*CSC: otherwise it diverges!*) //
1736     | #l #H generalize in match (occurs_exactly_once_Some ???? H) in ⊢ ?;
1737       generalize in match (refl … (eq_bv ? l id)); cases (eq_bv … l id) in ⊢ (???% → %)
1738        [ #EQ #_ <(eq_bv_eq … EQ) >lookup_insert_hit >address_of_word_labels_code_mem_Some_hit
1739          <IH0 [@IHn1 | <(eq_bv_eq … EQ) in H #H @H]
1740        | #EQ change with (occurs_exactly_once ?? → ?) #K >lookup_insert_miss [2: -IHn1; (*Andrea:XXXX used to work /2/*)>eq_bv_sym >EQ // ]
1741          <(address_of_word_labels_code_mem_Some_miss … EQ … H) @IH2 -IHn1; //]]]
1742 |3: whd % [% [%]] // [>sigma_0//] #id normalize in ⊢ (% → ?) #abs @⊥ //
1743 | generalize in match (sig2 … result) in ⊢ ?;
1744   normalize nodelta in p ⊢ %; -result; >p in ⊢ (match % with [_ ⇒ ?] → ?)
1745   normalize nodelta; >p1 normalize nodelta; >p2; normalize nodelta; * #_; #H @H]
1746qed.
1747
1748definition assembly:
1749 ∀p:pseudo_assembly_program. policy p → list Byte × (BitVectorTrie Identifier 16) ≝
1750  λp.let 〈preamble, instr_list〉 ≝ p in
1751   λpol.
1752    let 〈labels,costs〉 ≝ build_maps 〈preamble,instr_list〉 pol in
1753    let datalabels ≝ construct_datalabels preamble in
1754    let lookup_labels ≝ λx. lookup ? ? x labels (zero ?) in
1755    let lookup_datalabels ≝ λx. lookup ? ? x datalabels (zero ?) in
1756    let result ≝
1757     foldl_strong
1758      (option Identifier × pseudo_instruction)
1759      (λpre. Σpc_ppc_code:(Word × (Word × (list Byte))).
1760        let 〈pc,ppc_code〉 ≝ pc_ppc_code in
1761        let 〈ppc,code〉 ≝ ppc_code in
1762         ∀ppc'.
1763          let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' in
1764          let 〈len,assembledi〉 ≝
1765           assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc' lookup_labels lookup_datalabels pi ??? in
1766           True)
1767      instr_list
1768      (λprefix,hd,tl,prf,pc_ppc_code.
1769        let 〈pc, ppc_code〉 ≝ pc_ppc_code in
1770        let 〈ppc, code〉 ≝ ppc_code in
1771        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc lookup_labels lookup_datalabels (\snd hd) ??? in
1772        let 〈new_pc, flags〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in
1773        let new_ppc ≝ \snd (half_add ? ppc (bitvector_of_nat ? 1)) in
1774         〈new_pc, 〈new_ppc, (code @ program)〉〉)
1775      〈(zero ?), 〈(zero ?), [ ]〉〉
1776    in
1777     〈\snd (\snd result), costs〉.
1778 [2,5: %
1779 |*: cases daemon ]
1780qed.
1781
1782definition assembly_unlabelled_program: assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝
1783 λp. Some ? (〈foldr ? ? (λi,l. assembly1 i @ l) [ ] p, Stub …〉).
Note: See TracBrowser for help on using the repository browser.