source: src/ASM/Assembly.ma @ 1061

Last change on this file since 1061 was 1054, checked in by boender, 9 years ago
  • proven policy safe
File size: 54.5 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/BitVectorTrie.ma".
3include "ASM/Arithmetic.ma".
4include "ASM/Fetch.ma".
5include "ASM/Status.ma".
6
7definition assembly_preinstruction ≝
8  λA: Type[0].
9  λaddr_of: A → Byte. (* relative *)
10  λpre: preinstruction A.
11  match pre with
12  [ ADD addr1 addr2 ⇒
13     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
14      [ REGISTER r ⇒ λ_.[ ([[false;false;true;false;true]]) @@ r ]
15      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;false;false;true;false;true]]); b1 ]
16      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;false;false;true;true;i1]]) ]
17      | DATA b1 ⇒ λ_. [ ([[false;false;true;false;false;true;false;false]]) ; b1 ]
18      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
19  | ADDC addr1 addr2 ⇒
20     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
21      [ REGISTER r ⇒ λ_.[ ([[false;false;true;true;true]]) @@ r ]
22      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;true;false;true;false;true]]); b1 ]
23      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;true;false;true;true;i1]]) ]
24      | DATA b1 ⇒ λ_. [ ([[false;false;true;true;false;true;false;false]]) ; b1 ]
25      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
26  | ANL addrs ⇒
27     match addrs with
28      [ inl addrs ⇒ match addrs with
29         [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
30           match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
31            [ REGISTER r ⇒ λ_.[ ([[false;true;false;true;true]]) @@ r ]
32            | DIRECT b1 ⇒ λ_.[ ([[false;true;false;true;false;true;false;true]]); b1 ]
33            | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;true;false;true;true;i1]]) ]
34            | DATA b1 ⇒ λ_. [ ([[false;true;false;true;false;true;false;false]]) ; b1 ]
35            | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
36         | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
37            let b1 ≝
38             match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
39              [ DIRECT b1 ⇒ λ_.b1
40              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
41            match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
42             [ ACC_A ⇒ λ_.[ ([[false;true;false;true;false;false;true;false]]) ; b1 ]
43             | DATA b2 ⇒ λ_. [ ([[false;true;false;true;false;false;true;true]]) ; b1 ; b2 ]
44             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
45         ]
46      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
47         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
48          [ BIT_ADDR b1 ⇒ λ_.[ ([[true;false;false;false;false;false;true;false]]) ; b1 ]
49          | N_BIT_ADDR b1 ⇒ λ_. [ ([[true;false;true;true;false;false;false;false]]) ; b1 ]
50          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
51  | CLR addr ⇒
52     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
53      [ ACC_A ⇒ λ_.
54         [ ([[true; true; true; false; false; true; false; false]]) ]
55      | CARRY ⇒ λ_.
56         [ ([[true; true; false; false; false; false; true; true]]) ]
57      | BIT_ADDR b1 ⇒ λ_.
58         [ ([[true; true; false; false; false; false; true; false]]) ; b1 ]
59      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
60  | CPL addr ⇒
61     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
62      [ ACC_A ⇒ λ_.
63         [ ([[true; true; true; true; false; true; false; false]]) ]
64      | CARRY ⇒ λ_.
65         [ ([[true; false; true; true; false; false; true; true]]) ]
66      | BIT_ADDR b1 ⇒ λ_.
67         [ ([[true; false; true; true; false; false; true; false]]) ; b1 ]
68      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
69  | DA addr ⇒
70     [ ([[true; true; false; true; false; true; false; false]]) ]
71  | DEC addr ⇒
72     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with
73      [ ACC_A ⇒ λ_.
74         [ ([[false; false; false; true; false; true; false; false]]) ]
75      | REGISTER r ⇒ λ_.
76         [ ([[false; false; false; true; true]]) @@ r ]
77      | DIRECT b1 ⇒ λ_.
78         [ ([[false; false; false; true; false; true; false; true]]); b1 ]
79      | INDIRECT i1 ⇒ λ_.
80         [ ([[false; false; false; true; false; true; true; i1]]) ]
81      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
82      | DJNZ addr1 addr2 ⇒
83         let b2 ≝ addr_of addr2 in
84         match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with
85          [ REGISTER r ⇒ λ_.
86             [ ([[true; true; false; true; true]]) @@ r ; b2 ]
87          | DIRECT b1 ⇒ λ_.
88             [ ([[true; true; false; true; false; true; false; true]]); b1; b2 ]
89          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
90      | JC addr ⇒
91        let b1 ≝ addr_of addr in
92          [ ([[false; true; false; false; false; false; false; false]]); b1 ]
93      | JNC addr ⇒
94         let b1 ≝ addr_of addr in
95           [ ([[false; true; false; true; false; false; false; false]]); b1 ]
96      | JZ addr ⇒
97         let b1 ≝ addr_of addr in
98           [ ([[false; true; true; false; false; false; false; false]]); b1 ]
99      | JNZ addr ⇒
100         let b1 ≝ addr_of addr in
101           [ ([[false; true; true; true; false; false; false; false]]); b1 ]
102      | JB addr1 addr2 ⇒
103         let b2 ≝ addr_of addr2 in
104         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
105          [ BIT_ADDR b1 ⇒ λ_.
106             [ ([[false; false; true; false; false; false; false; false]]); b1; b2 ]
107          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
108      | JNB addr1 addr2 ⇒
109         let b2 ≝ addr_of addr2 in
110         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
111          [ BIT_ADDR b1 ⇒ λ_.
112             [ ([[false; false; true; true; false; false; false; false]]); b1; b2 ]
113          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
114      | JBC addr1 addr2 ⇒
115         let b2 ≝ addr_of addr2 in
116         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
117          [ BIT_ADDR b1 ⇒ λ_.
118             [ ([[false; false; false; true; false; false; false; false]]); b1; b2 ]
119          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
120      | CJNE addrs addr3 ⇒
121         let b3 ≝ addr_of addr3 in
122         match addrs with
123          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
124             match addr2 return λx. bool_to_Prop (is_in ? [[direct;data]] x) → ? with
125              [ DIRECT b1 ⇒ λ_.
126                 [ ([[true; false; true; true; false; true; false; true]]); b1; b3 ]
127              | DATA b1 ⇒ λ_.
128                 [ ([[true; false; true; true; false; true; false; false]]); b1; b3 ]
129              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
130          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
131             let b2 ≝
132              match addr2 return λx. bool_to_Prop (is_in ? [[data]] x) → ? with
133               [ DATA b2 ⇒ λ_. b2
134               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) in
135             match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with
136              [ REGISTER r ⇒ λ_.
137                 [ ([[true; false; true; true; true]]) @@ r; b2; b3 ]
138              | INDIRECT i1 ⇒ λ_.
139                 [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ]
140              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
141         ]
142  | DIV addr1 addr2 ⇒
143     [ ([[true;false;false;false;false;true;false;false]]) ]
144  | INC addr ⇒
145     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with
146      [ ACC_A ⇒ λ_.
147         [ ([[false;false;false;false;false;true;false;false]]) ]         
148      | REGISTER r ⇒ λ_.
149         [ ([[false;false;false;false;true]]) @@ r ]
150      | DIRECT b1 ⇒ λ_.
151         [ ([[false; false; false; false; false; true; false; true]]); b1 ]
152      | INDIRECT i1 ⇒ λ_.
153        [ ([[false; false; false; false; false; true; true; i1]]) ]
154      | DPTR ⇒ λ_.
155        [ ([[true;false;true;false;false;false;true;true]]) ]
156      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
157  | MOV addrs ⇒
158     match addrs with
159      [ inl addrs ⇒
160         match addrs with
161          [ inl addrs ⇒
162             match addrs with
163              [ inl addrs ⇒
164                 match addrs with
165                  [ inl addrs ⇒
166                     match addrs with
167                      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
168                         match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
169                          [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ]
170                          | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ]
171                          | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ]
172                          | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ]
173                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
174                      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
175                         match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with
176                          [ REGISTER r ⇒ λ_.
177                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
178                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ]
179                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ]
180                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ]
181                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
182                          | INDIRECT i1 ⇒ λ_.
183                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
184                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ]
185                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ]
186                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ]
187                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
188                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
189                  | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
190                     let b1 ≝
191                      match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
192                       [ DIRECT b1 ⇒ λ_. b1
193                       | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
194                     match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with
195                      [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1]
196                      | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ]
197                      | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ]
198                      | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ]
199                      | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ]
200                      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
201              | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
202                 match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with
203                  [ DATA16 w ⇒ λ_.
204                     let b1_b2 ≝ split ? 8 8 w in
205                     let b1 ≝ \fst b1_b2 in
206                     let b2 ≝ \snd b1_b2 in
207                      [ ([[true;false;false;true;false;false;false;false]]); b1; b2]
208                  | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
209          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
210             match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
211              [ BIT_ADDR b1 ⇒ λ_.
212                 [ ([[true;false;true;false;false;false;true;false]]); b1 ]
213              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
214      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
215         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
216          [ BIT_ADDR b1 ⇒ λ_.
217             [ ([[true;false;false;true;false;false;true;false]]); b1 ]
218          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
219  | MOVX addrs ⇒
220     match addrs with
221      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
222         match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
223          [ EXT_INDIRECT i1 ⇒ λ_.
224             [ ([[true;true;true;false;false;false;true;i1]]) ]
225          | EXT_INDIRECT_DPTR ⇒ λ_.
226             [ ([[true;true;true;false;false;false;false;false]]) ]
227          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
228      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
229         match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
230          [ EXT_INDIRECT i1 ⇒ λ_.
231             [ ([[true;true;true;true;false;false;true;i1]]) ]
232          | EXT_INDIRECT_DPTR ⇒ λ_.
233             [ ([[true;true;true;true;false;false;false;false]]) ]
234          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
235  | MUL addr1 addr2 ⇒
236     [ ([[true;false;true;false;false;true;false;false]]) ]
237  | NOP ⇒
238     [ ([[false;false;false;false;false;false;false;false]]) ]
239  | ORL addrs ⇒
240     match addrs with
241      [ inl addrs ⇒
242         match addrs with
243          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
244             match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with
245             [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ]
246             | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ]
247             | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ]
248             | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ]
249             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
250          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
251            let b1 ≝
252              match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
253               [ DIRECT b1 ⇒ λ_. b1
254               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
255             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
256              [ ACC_A ⇒ λ_.
257                 [ ([[false;true;false;false;false;false;true;false]]); b1 ]
258              | DATA b2 ⇒ λ_.
259                 [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ]
260              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
261      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in     
262         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
263          [ BIT_ADDR b1 ⇒ λ_.
264             [ ([[false;true;true;true;false;false;true;false]]); b1 ]
265          | N_BIT_ADDR b1 ⇒ λ_.
266             [ ([[true;false;true;false;false;false;false;false]]); b1 ]
267          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
268  | POP addr ⇒
269     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
270      [ DIRECT b1 ⇒ λ_.
271         [ ([[true;true;false;true;false;false;false;false]]) ; b1 ]
272      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
273  | PUSH addr ⇒
274     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
275      [ DIRECT b1 ⇒ λ_.
276         [ ([[true;true;false;false;false;false;false;false]]) ; b1 ]
277      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
278  | RET ⇒
279     [ ([[false;false;true;false;false;false;true;false]]) ]
280  | RETI ⇒
281     [ ([[false;false;true;true;false;false;true;false]]) ]
282  | RL addr ⇒
283     [ ([[false;false;true;false;false;false;true;true]]) ]
284  | RLC addr ⇒
285     [ ([[false;false;true;true;false;false;true;true]]) ]
286  | RR addr ⇒
287     [ ([[false;false;false;false;false;false;true;true]]) ]
288  | RRC addr ⇒
289     [ ([[false;false;false;true;false;false;true;true]]) ]
290  | SETB addr ⇒     
291     match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with
292      [ CARRY ⇒ λ_.
293         [ ([[true;true;false;true;false;false;true;true]]) ]
294      | BIT_ADDR b1 ⇒ λ_.
295         [ ([[true;true;false;true;false;false;true;false]]); b1 ]
296      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
297  | SUBB addr1 addr2 ⇒
298     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
299      [ REGISTER r ⇒ λ_.
300         [ ([[true;false;false;true;true]]) @@ r ]
301      | DIRECT b1 ⇒ λ_.
302         [ ([[true;false;false;true;false;true;false;true]]); b1]
303      | INDIRECT i1 ⇒ λ_.
304         [ ([[true;false;false;true;false;true;true;i1]]) ]
305      | DATA b1 ⇒ λ_.
306         [ ([[true;false;false;true;false;true;false;false]]); b1]
307      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
308  | SWAP addr ⇒
309     [ ([[true;true;false;false;false;true;false;false]]) ]
310  | XCH addr1 addr2 ⇒
311     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with
312      [ REGISTER r ⇒ λ_.
313         [ ([[true;true;false;false;true]]) @@ r ]
314      | DIRECT b1 ⇒ λ_.
315         [ ([[true;true;false;false;false;true;false;true]]); b1]
316      | INDIRECT i1 ⇒ λ_.
317         [ ([[true;true;false;false;false;true;true;i1]]) ]
318      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
319  | XCHD addr1 addr2 ⇒
320     match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with
321      [ INDIRECT i1 ⇒ λ_.
322         [ ([[true;true;false;true;false;true;true;i1]]) ]
323      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
324  | XRL addrs ⇒
325     match addrs with
326      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
327         match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with
328          [ REGISTER r ⇒ λ_.
329             [ ([[false;true;true;false;true]]) @@ r ]
330          | DIRECT b1 ⇒ λ_.
331             [ ([[false;true;true;false;false;true;false;true]]); b1]
332          | INDIRECT i1 ⇒ λ_.
333             [ ([[false;true;true;false;false;true;true;i1]]) ]
334          | DATA b1 ⇒ λ_.
335             [ ([[false;true;true;false;false;true;false;false]]); b1]
336          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
337      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
338         let b1 ≝
339          match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
340           [ DIRECT b1 ⇒ λ_. b1
341           | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
342         match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
343          [ ACC_A ⇒ λ_.
344             [ ([[false;true;true;false;false;false;true;false]]); b1 ]         
345          | DATA b2 ⇒ λ_.
346             [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ]
347          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
348       ].
349
350definition assembly1 ≝
351 λi: instruction.
352 match i with
353  [ ACALL addr ⇒
354     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
355      [ ADDR11 w ⇒ λ_.
356         let v1_v2 ≝ split ? 3 8 w in
357         let v1 ≝ \fst v1_v2 in
358         let v2 ≝ \snd v1_v2 in
359          [ (v1 @@ [[true; false; false; false; true]]) ; v2 ]
360      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
361  | AJMP addr ⇒
362     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
363      [ ADDR11 w ⇒ λ_.
364         let v1_v2 ≝ split ? 3 8 w in
365         let v1 ≝ \fst v1_v2 in
366         let v2 ≝ \snd v1_v2 in
367          [ (v1 @@ [[false; false; false; false; true]]) ; v2 ]
368      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
369  | JMP adptr ⇒
370     [ ([[false;true;true;true;false;false;true;true]]) ]
371  | LCALL addr ⇒
372     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
373      [ ADDR16 w ⇒ λ_.
374         let b1_b2 ≝ split ? 8 8 w in
375         let b1 ≝ \fst b1_b2 in
376         let b2 ≝ \snd b1_b2 in
377          [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ]         
378      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
379  | LJMP addr ⇒
380     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
381      [ ADDR16 w ⇒ λ_.
382         let b1_b2 ≝ split ? 8 8 w in
383         let b1 ≝ \fst b1_b2 in
384         let b2 ≝ \snd b1_b2 in
385          [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ]         
386      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
387  | MOVC addr1 addr2 ⇒
388     match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with
389      [ ACC_DPTR ⇒ λ_.
390         [ ([[true;false;false;true;false;false;true;true]]) ]
391      | ACC_PC ⇒ λ_.
392         [ ([[true;false;false;false;false;false;true;true]]) ]
393      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
394  | SJMP addr ⇒
395     match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with
396      [ RELATIVE b1 ⇒ λ_.
397         [ ([[true;false;false;false;false;false;false;false]]); b1 ]
398      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
399  | RealInstruction instr ⇒
400    assembly_preinstruction [[ relative ]]
401      (λx.
402        match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with
403        [ RELATIVE r ⇒ λ_. r
404        | _ ⇒ λabsd. ⊥
405        ] (subaddressing_modein … x)) instr
406  ].
407  cases absd
408qed.
409
410definition is_relative_jump ≝
411  λi: preinstruction Identifier.
412  match i with
413  [ JC _ ⇒ True
414  | JNC _ ⇒ True
415  | JB _ _ ⇒ True
416  | JNB _ _ ⇒ True
417  | JBC _ _ ⇒ True
418  | JZ _ ⇒ True
419  | JNZ _ ⇒ True
420  | CJNE _ _ ⇒ True
421  | DJNZ _ _ ⇒ True
422  | _ ⇒ False
423  ].
424
425definition pseudo_instruction_is_relative_jump: pseudo_instruction → Prop ≝
426 λi.
427  match i with
428   [ Instruction j ⇒ is_relative_jump j
429   | _ ⇒ False ].
430
431inductive jump_length: Type[0] ≝
432  | short_jump: jump_length
433  | medium_jump: jump_length
434  | long_jump: jump_length.
435
436definition find_right_call: Word → Word → (nat × nat) × (Word × (option jump_length)) ≝
437 (* medium call: 11 bits, only in "current segment" *)
438 (* can this be done more efficiently with bit vector arithmetic? *)
439 λpc.λaddress.
440  let 〈a1, a2〉 ≝ split ? 5 11 address in
441  let 〈p1, p2〉 ≝ split ? 5 11 pc in
442   if eq_bv ? a1 p1 then (* we're in the same segment *)
443    〈3, 2, address, Some ? medium_jump〉
444   else
445    〈3, 3, address, Some ? long_jump〉.
446   
447lemma frc_ok:
448  ∀pc.∀j_addr.
449  let 〈i1,i2,addr,jl〉 ≝ find_right_call pc j_addr in
450  addr = j_addr ∧
451  match jl with
452  [ None ⇒ False (* doesn't happen *)
453  | Some j ⇒ match j with
454    [ short_jump ⇒ False (* doesn't happen either *)
455    | medium_jump ⇒ \fst (split ? 5 11 j_addr) = \fst (split ? 5 11 pc)
456    | long_jump ⇒ True
457    ]
458  ].
459 #pc #j_addr whd in match (find_right_call pc j_addr) cases (split ????) cases (split ????)
460 #p1 #p2 #a1 #a2 normalize nodelta
461 lapply (refl ? (eq_bv 5 a1 p1)) cases (eq_bv 5 a1 p1) in ⊢ (???% → %) #H normalize
462 [ %1 [ @refl | @(eq_bv_eq … H) ]
463 | %1 [ @refl | // ] ]
464qed.
465       
466definition distance ≝
467 λx.λy.if gtb x y then x - y else y - x.
468 
469definition find_right_jump: Word → Word → (nat × nat) × (Word × (option jump_length)) ≝
470 (* short jump: 8 bits signed *)
471 (* medium jump: 11 bits, forward only *)
472 λpc.λaddress.
473  let pn ≝ nat_of_bitvector ? pc in
474  let pa ≝ nat_of_bitvector ? address in
475   if ltb (distance pn pa) 127 then
476    〈2, 3, address, Some ? short_jump〉
477   else
478    find_right_call pc address.
479
480lemma frj_ok:
481  ∀pc.∀j_addr.
482  let 〈i1,i2,addr,jl〉 ≝ find_right_jump pc j_addr in
483  addr = j_addr ∧
484  match jl with
485  [ None ⇒ False (* doesn't happen *)
486  | Some j ⇒ match j with
487    [ short_jump ⇒ distance (nat_of_bitvector ? pc) (nat_of_bitvector ? j_addr) < 127
488    | medium_jump ⇒ \fst (split ? 5 11 j_addr) = \fst (split ? 5 11 pc)
489    | long_jump ⇒ True
490    ]
491  ].
492 #pc #j_addr whd in match (find_right_jump pc j_addr)
493 lapply (refl ? (ltb (distance (nat_of_bitvector 16 pc) (nat_of_bitvector 16 j_addr)) 127))
494 cases (ltb (distance (nat_of_bitvector 16 pc) (nat_of_bitvector 16 j_addr)) 127) in ⊢ (???% → %) #H
495 normalize nodelta
496 [ %1 [ @refl | whd @(leb_true_to_le … H) ]
497 | lapply (frc_ok pc j_addr) cases (find_right_call ??) normalize nodelta #x #y cases x
498 #i1 #i2 cases y #addr #jl normalize nodelta cases jl normalize
499   [ //
500   | #jl cases jl normalize
501     [1: #f @⊥ @(proj2 ? ? f) |2,3: // ]
502   ]
503  ]
504qed.
505   
506definition find_right_relative_jump: Word → (BitVectorTrie (Word × Identifier) 16) →
507 Identifier → (nat × nat) × (Word × (option jump_length)) ≝
508 λpc.λlabels.λjmp.
509 match lookup_opt ? ? jmp labels with
510 [ None ⇒ 〈3, 3, pc, Some ? long_jump〉
511 | Some x ⇒ let 〈ignore, a〉 ≝ x in find_right_jump pc a ].
512 
513lemma frrj_ok:
514  ∀pc.∀labels.∀j_id.
515  let 〈i1,i2,addr,jl〉 ≝ find_right_relative_jump pc labels j_id in
516  match lookup_opt ? ? j_id labels with
517  [ None ⇒ True (* long jump *)
518  | Some x ⇒ let 〈ignore,j_addr〉 ≝ x in addr = j_addr ∧ match jl with
519    [ None ⇒ False (* doesn't happen *)
520    | Some j ⇒ match j with
521      [ short_jump ⇒ distance (nat_of_bitvector ? pc) (nat_of_bitvector ? j_addr) < 127
522      | medium_jump ⇒ \fst (split ? 5 11 j_addr) = \fst (split ? 5 11 pc)
523      | long_jump ⇒ True
524      ]
525    ]
526  ].
527 #pc #labels #j_id whd in match (find_right_relative_jump pc labels j_id)
528 cases (lookup_opt ? ? j_id labels) normalize nodelta
529 [ //
530 | #x cases x #y #j_addr -x; normalize nodelta lapply (frj_ok pc j_addr)
531   cases (find_right_jump ??) #x cases x #i1 #i2 -x #x cases x #i3 #z -x; cases z
532   normalize nodelta
533   [ //
534   | #jl cases jl normalize nodelta
535     [1,3: // |2: #H @H ]
536   ]
537 ]
538qed.
539     
540definition jep_relative: Word → (BitVectorTrie (Word × Identifier) 16) → preinstruction Identifier → ? ≝
541 λpc.λlabels.λi.
542  match i with
543  [ JC jmp ⇒ find_right_relative_jump pc labels jmp
544  | JNC jmp ⇒ find_right_relative_jump pc labels jmp
545  | JB baddr jmp ⇒ find_right_relative_jump pc labels jmp
546  | JZ jmp ⇒ find_right_relative_jump pc labels jmp
547  | JNZ jmp ⇒ find_right_relative_jump pc labels jmp
548  | JBC baddr jmp ⇒ find_right_relative_jump pc labels jmp
549  | JNB baddr jmp ⇒ find_right_relative_jump pc labels jmp
550  | CJNE addr jmp ⇒ find_right_relative_jump pc labels jmp
551  | DJNZ addr jmp ⇒ find_right_relative_jump pc labels jmp
552  | _ ⇒ let l ≝ length ? (assembly_preinstruction ? (λx.zero ?) i) in
553         〈l, l, pc, None …〉 ].
554
555definition jgeq_opt ≝
556 λa:jump_length.λb:option (Word × jump_length).
557 match a with
558 [ short_jump ⇒ match b with
559   [ None ⇒ (* XXX *) False
560   | Some x ⇒ let 〈ignore,j〉 ≝ x in j = short_jump
561   ]
562 | medium_jump ⇒ match b with
563  [ None ⇒  (* XXX *) False
564  | Some x ⇒ let 〈ignore,j〉 ≝ x in j = short_jump ∨ j = medium_jump ]
565 | long_jump ⇒ True
566 ].
567
568definition is_long_jump ≝
569 λj.match j with
570 [ long_jump ⇒ true
571 | _         ⇒ false
572 ].
573
574definition policy_safe ≝
575 λp:BitVectorTrie (Word × Word × jump_length) 16.
576  forall ? ? p (λ_.λx.let 〈pc,addr,j〉 ≝ x in
577   match j with
578   [ short_jump ⇒ distance (nat_of_bitvector ? pc) (nat_of_bitvector ? addr) < 127
579   | medium_jump ⇒ \fst (split ? 5 11 addr) = \fst (split ? 5 11 pc)
580   | long_jump ⇒ True
581   ]
582  ).
583
584definition jump_expansion_policy ≝
585  Σpolicy:BitVectorTrie (Word × Word × jump_length) 16.policy_safe policy.
586 
587definition inject_jump_expansion_policy:
588 ∀x:BitVectorTrie (Word × Word × jump_length) 16.
589  policy_safe x → jump_expansion_policy ≝ inject ….
590 
591coercion inject_jump_expansion_policy:
592 ∀x:BitVectorTrie (Word × Word × jump_length) 16.
593  policy_safe x → jump_expansion_policy ≝ inject_jump_expansion_policy
594 on x:(BitVectorTrie (Word × Word × jump_length) 16) to jump_expansion_policy.
595
596definition jump_expansion_policy_internal: pseudo_assembly_program →
597 (BitVectorTrie (Word × Identifier) 16) → jump_expansion_policy →
598 Σres:(BitVectorTrie (Word × Identifier) 16) × (BitVectorTrie (Word × Word × jump_length) 16).
599  let 〈x,p〉 ≝ res in policy_safe p ≝
600 λprogram.λold_labels.λold_policy.
601   let res ≝
602   foldl_strong (option Identifier × pseudo_instruction)
603    (λprefix.Σresult:(Word × Word) × ((BitVectorTrie (Word × Identifier) 16) ×
604      (BitVectorTrie (Word × Word × jump_length) 16)).
605      let 〈x,y,z,p〉 ≝ result in policy_safe p
606    )
607    (\snd program)
608    (λprefix.λi.λtl.λprf.λacc.   
609     let 〈label, instr〉 ≝ i in
610     let 〈pc,orig_pc,labels,policy〉 ≝ acc in
611     let new_labels ≝ match label return λ_.(BitVectorTrie (Word × Identifier) 16) with
612      [ None ⇒ labels
613      | Some l ⇒ insert ? ? orig_pc 〈pc,l〉 labels
614      ] in
615     let add_instr ≝ match instr with
616      [ Call c ⇒
617        match lookup_opt ? ? c new_labels with
618         [ None   ⇒ 〈3, 3, pc, Some ? long_jump〉
619         | Some x ⇒ let 〈ignore,c_addr〉 ≝ x in find_right_call pc c_addr ]
620      | Jmp j ⇒
621        match lookup_opt ? ? j new_labels with
622         [ None   ⇒ 〈3, 3, pc, Some ? long_jump〉
623         | Some x ⇒ let 〈ignore,j_addr〉 ≝ x in find_right_jump pc j_addr ]
624      | Instruction i ⇒ jep_relative pc new_labels i
625      | Mov _ _ ⇒ 〈3, 3, pc, None …〉
626      | Cost _ ⇒ 〈0, 0, pc, None …〉
627      | Comment _ ⇒ 〈0, 0, pc, None …〉 ] in
628     let 〈pc_delta, orig_pc_delta, addr, jmp_len〉 ≝ add_instr in
629     let 〈new_pc,ignore〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in
630     let 〈new_orig_pc,ignore〉 ≝ add_16_with_carry orig_pc (bitvector_of_nat ? orig_pc_delta) false in
631     match jmp_len with
632      [ None   ⇒ 〈new_pc, orig_pc, new_labels, policy〉
633      | Some j ⇒ 〈new_pc, new_orig_pc, new_labels, insert ? ? orig_pc 〈pc, addr, j〉 policy〉
634      ]
635    ) 〈zero ?, zero ?, old_labels, eject … old_policy〉 in
636   let 〈npc, norig_pc, nlabels, npolicy〉 ≝ res in
637   〈nlabels, npolicy〉.
638   [ cases res in p -res; #res >p1 >p2 normalize nodelta #Ha #Hb normalize in Hb;
639     >Hb in Ha; normalize nodelta #H @H
640   | generalize in match (sig2 … acc) >p1 >p2 >p3 #H
641     @(forall_insert … H)  normalize nodelta normalize nodelta in p4; cases instr in p4; >p5 >p6 normalize nodelta
642     [5: #str >p9 #Heq cases (lookup_opt ? ? str ?) in Heq; normalize nodelta
643         [ #Heq2 lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq2))))
644           #Heq3 destruct (Heq3) //
645         | #x cases x -x #i0 #c_addr normalize nodelta lapply (frc_ok pc c_addr)
646           cases (find_right_call pc c_addr) #x #y cases x #i0 #i1 -x; cases y #ad #jmp -y;
647           normalize nodelta #Heq #Heq2
648           >(proj1 ? ? Heq) in Heq2; #Heq2
649           <(proj1 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq2))))
650           >(proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq2)))) in Heq;
651           cases j normalize nodelta #Heq [ @⊥ ] @(proj2 ? ? Heq)
652         ]
653    |2,3,6: [3: #x] #z >p9 #Heq lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq))))
654       #ctd destruct (ctd)
655    |1,4:
656      #pi
657       [1: cases label normalize nodelta [ #Hjep | #id #Hjep ] whd in Hjep: (??%??); cases pi in Hjep;
658       |2: cases (lookup_opt ? ? pi ?) normalize nodelta >p9
659           [ #Heq lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq))))
660             #ctd destruct (ctd) //
661           | #x cases x -x #i0 #j_addr normalize nodelta lapply (frj_ok pc j_addr)
662             cases (find_right_jump pc j_addr) #x #y cases x #i0 #i1 -x; cases y #ad #jump -y;
663             normalize nodelta #Heq #Heq2
664             >(proj1 ? ? Heq) in Heq2; #Heq2
665             <(proj1 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq2))))
666             >(proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq2)))) in Heq;
667             cases j normalize nodelta #Heq @(proj2 ? ? Heq)
668           ]
669       ]
670       [1,2,3,6,7,33,34,38,39,40,43,44,70,71: #acc #x normalize nodelta #Heq
671         <(proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq)))) in p9;
672         #ctd destruct (ctd)
673       |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,41,42,45,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69:
674         #acc normalize nodelta #Heq
675         <(proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq)))) in p9;
676         #ctd destruct (ctd)
677       |35,36,37,72,73,74: normalize nodelta #Heq
678         <(proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq)))) in p9;
679         #ctd destruct (ctd)
680       |9,10,14,15: #j_id normalize nodelta >p9 lapply (frrj_ok pc labels j_id)
681           whd in match (find_right_relative_jump pc labels j_id)
682          normalize nodelta cases (lookup_opt ? ? j_id labels) normalize nodelta
683          [1,3,5,7: #_ #Heq lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq))))
684             #Heq2 destruct (Heq2) //
685          |2,4,6,8: #x cases x #i0 #j_addr #Hok #Heq >Heq in Hok;
686            normalize nodelta #Hok >(proj1 … Hok) @(proj2 … Hok)
687          ]
688       |46,47,51,52: #j_id normalize nodelta >p9 lapply (frrj_ok pc (insert … orig_pc 〈pc,id〉 labels) j_id)
689          whd in match (find_right_relative_jump pc (insert … orig_pc 〈pc,id〉 labels) j_id)
690          normalize nodelta cases (lookup_opt ? ? j_id (insert … orig_pc 〈pc,id〉 labels)) normalize nodelta
691          [1,3,5,7: #_ #Heq lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq))))
692             #Heq2 destruct (Heq2) //
693          |2,4,6,8: #x cases x #i0 #j_addr #Hok #Heq >Heq in Hok;
694            normalize nodelta #Hok >(proj1 … Hok) @(proj2 … Hok)
695          ]
696       |11,12,13,16,17: #x #j_id normalize nodelta >p9 lapply (frrj_ok pc labels j_id)
697          whd in match (find_right_relative_jump pc labels j_id)
698          normalize nodelta cases (lookup_opt ? ? j_id labels) normalize nodelta
699          [1,3,5,7,9: #_ #Heq lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq))))
700             #Heq2 destruct (Heq2) //
701          |2,4,6,8,10: #x cases x #i0 #j_addr #Hok #Heq >Heq in Hok;
702            normalize nodelta #Hok >(proj1 … Hok) @(proj2 … Hok)
703          ]
704       |48,49,50,53,54: #x #j_id normalize nodelta >p9 lapply (frrj_ok pc (insert … orig_pc 〈pc,id〉 labels) j_id)
705          whd in match (find_right_relative_jump pc (insert … orig_pc 〈pc,id〉 labels) j_id)
706          normalize nodelta cases (lookup_opt ? ? j_id (insert … orig_pc 〈pc,id〉 labels)) normalize nodelta
707          [1,3,5,7,9: #_ #Heq lapply (proj2 ? ? (pair_destruct … (proj2 ? ? (pair_destruct … Heq))))
708             #Heq2 destruct (Heq2) //
709          |2,4,6,8,10: #x cases x #i0 #j_addr #Hok #Heq >Heq in Hok;
710            normalize nodelta #Hok >(proj1 … Hok) @(proj2 … Hok)
711          ]
712       ]
713     ]
714   | generalize in match (sig2 … acc) >p1 >p2 >p3 #H
715     @H
716   | generalize in match (sig2 … old_policy) #H @H
717   ]
718qed.
719   
720definition expand_relative_jump_internal:
721 (Identifier → Word) → jump_length → Identifier → Word → ([[relative]] → preinstruction [[relative]]) →
722 option (list instruction)
723 ≝
724  λlookup_labels,jmp_len.λjmp:Identifier.λpc,i.
725  match jmp_len with
726  [ short_jump ⇒
727     let lookup_address ≝ lookup_labels jmp in
728     let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in
729     let 〈upper, lower〉 ≝ split ? 8 8 result in
730     if eq_bv ? upper (zero 8) then
731      let address ≝ RELATIVE lower in
732       Some ? [ RealInstruction (i address) ]
733     else
734       None ?
735  | medium_jump ⇒ None …
736  | long_jump ⇒
737    Some ?
738    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
739      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
740      LJMP (ADDR16 (lookup_labels jmp))
741    ]
742  ].
743  @ I
744qed.
745
746definition expand_relative_jump: (Identifier → Word) → jump_length → Word → preinstruction Identifier → option (list instruction) ≝
747  λlookup_labels.
748  λjmp_len: jump_length.
749  λpc.
750  λi: preinstruction Identifier.
751  let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in
752  match i with
753  [ JC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JC ?)
754  | JNC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNC ?)
755  | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JB ? baddr)
756  | JZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JZ ?)
757  | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNZ ?)
758  | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JBC ? baddr)
759  | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNB ? baddr)
760  | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (CJNE ? addr)
761  | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (DJNZ ? addr)
762  | ADD arg1 arg2 ⇒ Some ? [ ADD ? arg1 arg2 ]
763  | ADDC arg1 arg2 ⇒ Some ? [ ADDC ? arg1 arg2 ]
764  | SUBB arg1 arg2 ⇒ Some ? [ SUBB ? arg1 arg2 ]
765  | INC arg ⇒ Some ? [ INC ? arg ]
766  | DEC arg ⇒ Some ? [ DEC ? arg ]
767  | MUL arg1 arg2 ⇒ Some ? [ MUL ? arg1 arg2 ]
768  | DIV arg1 arg2 ⇒ Some ? [ DIV ? arg1 arg2 ]
769  | DA arg ⇒ Some ? [ DA ? arg ]
770  | ANL arg ⇒ Some ? [ ANL ? arg ]
771  | ORL arg ⇒ Some ? [ ORL ? arg ]
772  | XRL arg ⇒ Some ? [ XRL ? arg ]
773  | CLR arg ⇒ Some ? [ CLR ? arg ]
774  | CPL arg ⇒ Some ? [ CPL ? arg ]
775  | RL arg ⇒ Some ? [ RL ? arg ]
776  | RR arg ⇒ Some ? [ RR ? arg ]
777  | RLC arg ⇒ Some ? [ RLC ? arg ]
778  | RRC arg ⇒ Some ? [ RRC ? arg ]
779  | SWAP arg ⇒ Some ? [ SWAP ? arg ]
780  | MOV arg ⇒ Some ? [ MOV ? arg ]
781  | MOVX arg ⇒ Some ? [ MOVX ? arg ]
782  | SETB arg ⇒ Some ? [ SETB ? arg ]
783  | PUSH arg ⇒ Some ? [ PUSH ? arg ]
784  | POP arg ⇒ Some ? [ POP ? arg ]
785  | XCH arg1 arg2 ⇒ Some ? [ XCH ? arg1 arg2 ]
786  | XCHD arg1 arg2 ⇒ Some ? [ XCHD ? arg1 arg2 ]
787  | RET ⇒ Some ? [ RET ? ]
788  | RETI ⇒ Some ? [ RETI ? ]
789  | NOP ⇒ Some ? [ RealInstruction (NOP ?) ]
790  ].
791
792definition expand_pseudo_instruction: ? → ? → Word → jump_length → pseudo_instruction → option (list instruction) ≝
793  λlookup_labels.
794  λlookup_datalabels.
795  λpc.
796  λjmp_len.
797  λi.
798  match i with
799  [ Cost cost ⇒ Some ? [ ]
800  | Comment comment ⇒ Some ? [ ]
801  | Call call ⇒
802    match jmp_len with
803    [ short_jump ⇒ None ?
804    | medium_jump ⇒
805      let 〈ignore, address〉 ≝ split ? 5 11 (lookup_labels call) in
806      let 〈fst_5, rest〉 ≝ split ? 5 11 pc in
807      if eq_bv ? ignore fst_5 then
808        let address ≝ ADDR11 address in
809          Some ? ([ ACALL address ])
810      else
811        None ?
812    | long_jump ⇒
813      let address ≝ ADDR16 (lookup_labels call) in
814        Some ? [ LCALL address ]
815    ]
816  | Mov d trgt ⇒
817    let address ≝ DATA16 (lookup_datalabels trgt) in
818      Some ? [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))]
819  | Instruction instr ⇒ expand_relative_jump lookup_labels jmp_len pc instr
820  | Jmp jmp ⇒
821    match jmp_len with
822    [ short_jump ⇒
823      let lookup_address ≝ lookup_labels jmp in
824      let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in
825      let 〈upper, lower〉 ≝ split ? 8 8 result in
826      if eq_bv ? upper (zero 8) then
827        let address ≝ RELATIVE lower in
828          Some ? [ SJMP address ]
829      else
830        None ?
831    | medium_jump ⇒
832      let address ≝ lookup_labels jmp in
833      let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 address in
834      let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in
835      if eq_bv ? fst_5_addr fst_5_pc then
836        let address ≝ ADDR11 rest_addr in
837          Some ? ([ AJMP address ])
838      else
839        None ?
840    | long_jump ⇒
841      let address ≝ ADDR16 (lookup_labels jmp) in
842        Some ? [ LJMP address ]
843    ]
844  ].
845  @ I
846qed.
847
848let rec jump_expansion_internal (n: nat) (program: pseudo_assembly_program)
849 (old_labels: BitVectorTrie (Word × Identifier) 16)
850 (old_policy: Σbla:BitVectorTrie (Word × Word × jump_length) 16.policy_safe bla)
851 on n: BitVectorTrie jump_length 16 ≝
852 match n with
853 [ O ⇒ fold …
854   (λ_.λx.λacc.let 〈pc,i2,jmp_len〉 ≝ x in insert … pc jmp_len acc)
855   old_policy (Stub ? ?)
856 | S n' ⇒
857    (* let 〈x,y〉 ≝ jump_expansion_policy_internal program old_labels old_policy in *)
858    jump_expansion_internal n' program (* x y *)
859    (\fst (jump_expansion_policy_internal program old_labels old_policy))
860    (\snd (jump_expansion_policy_internal program old_labels old_policy))
861 ].
862 generalize in match (sig2 … (jump_expansion_policy_internal program old_labels old_policy))
863 cases (jump_expansion_policy_internal program old_labels old_policy)
864 #a cases a #xx #pp normalize nodelta
865 #H #H2 normalize nodelta @H2
866qed.
867 
868(**************************************** START OF POLICY ABSTRACTION ********************)
869
870definition policy_type ≝ Word → jump_length.
871
872definition jump_expansion': pseudo_assembly_program → policy_type ≝
873 λprogram.λpc.
874  let policy ≝ jump_expansion_internal (length ? (\snd program)) program (Stub ? ?) (Stub ? ?) in
875  lookup ? ? pc policy long_jump.
876 normalize //
877qed.
878 
879definition assembly_1_pseudoinstruction_safe ≝
880  λprogram: pseudo_assembly_program.
881  λjump_expansion: policy_type.
882  λppc: Word.
883  λpc: Word.
884  λlookup_labels.
885  λlookup_datalabels.
886  λi.
887  let expansion ≝ jump_expansion ppc in
888    match expand_pseudo_instruction lookup_labels lookup_datalabels pc expansion i with
889    [ None ⇒ None ?
890    | Some pseudos ⇒
891      let mapped ≝ map ? ? assembly1 pseudos in
892      let flattened ≝ flatten ? mapped in
893      let pc_len ≝ length ? flattened in
894        Some ? (〈pc_len, flattened〉)
895    ].
896 
897definition construct_costs_safe ≝
898  λprogram.
899  λjump_expansion: policy_type.
900  λpseudo_program_counter: nat.
901  λprogram_counter: nat.
902  λcosts: BitVectorTrie Word 16.
903  λi.
904  match i with
905  [ Cost cost ⇒
906    let program_counter_bv ≝ bitvector_of_nat ? program_counter in
907      Some ? 〈program_counter, (insert ? ? program_counter_bv cost costs)〉
908  | _ ⇒
909    let pc_bv ≝ bitvector_of_nat ? program_counter in
910    let ppc_bv ≝ bitvector_of_nat ? pseudo_program_counter in
911    let lookup_labels ≝ λx.pc_bv in
912    let lookup_datalabels ≝ λx.zero ? in
913    let pc_delta_assembled ≝
914      assembly_1_pseudoinstruction_safe program jump_expansion ppc_bv pc_bv
915        lookup_labels lookup_datalabels i
916    in
917    match pc_delta_assembled with
918    [ None ⇒ None ?
919    | Some pc_delta_assembled ⇒
920      let 〈pc_delta, assembled〉 ≝ pc_delta_assembled in
921        Some ? 〈pc_delta + program_counter, costs〉       
922    ]
923  ].
924
925(* This establishes the correspondence between pseudo program counters and
926   program counters. It is at the heart of the proof. *)
927(*CSC: code taken from build_maps *)
928definition sigma00: pseudo_assembly_program → policy_type → list ? → option (nat × (nat × (BitVectorTrie Word 16))) ≝
929 λinstr_list.
930 λjump_expansion: policy_type.
931 λl:list labelled_instruction.
932  foldl ??
933   (λt,i.
934       match t with
935       [ None ⇒ None ?
936       | Some ppc_pc_map ⇒
937         let 〈ppc,pc_map〉 ≝ ppc_pc_map in
938         let 〈program_counter, sigma_map〉 ≝ pc_map in
939         let 〈label, i〉 ≝ i in
940          match construct_costs_safe instr_list jump_expansion ppc program_counter (Stub …) i with
941           [ None ⇒ None ?
942           | Some pc_ignore ⇒
943              let 〈pc,ignore〉 ≝ pc_ignore in
944              Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ]
945       ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) l.
946
947definition sigma0: pseudo_assembly_program → policy_type → option (nat × (nat × (BitVectorTrie Word 16)))
948 ≝ λprog.λjump_expansion.sigma00 prog jump_expansion (\snd prog).
949
950definition tech_pc_sigma00: pseudo_assembly_program → policy_type → list labelled_instruction → option (nat × nat) ≝
951 λprogram,jump_expansion,instr_list.
952  match sigma00 program jump_expansion instr_list with
953   [ None ⇒ None …
954   | Some result ⇒
955      let 〈ppc,pc_sigma_map〉 ≝ result in
956      let 〈pc,map〉 ≝ pc_sigma_map in
957       Some … 〈ppc,pc〉 ].
958
959definition sigma_safe: pseudo_assembly_program → policy_type → option (Word → Word) ≝
960 λinstr_list,jump_expansion.
961  match sigma0 instr_list jump_expansion with
962  [ None ⇒ None ?
963  | Some result ⇒
964    let 〈ppc,pc_sigma_map〉 ≝ result in
965    let 〈pc, sigma_map〉 ≝ pc_sigma_map in
966      if gtb pc (2^16) then
967        None ?
968      else
969        Some ? (λx.lookup ?? x sigma_map (zero …)) ].
970
971(* stuff about policy *)
972
973definition policy_ok ≝ λjump_expansion,p. sigma_safe p jump_expansion ≠ None ….
974
975definition policy ≝ λp. Σjump_expansion:policy_type. policy_ok jump_expansion p.
976
977lemma eject_policy: ∀p. policy p → policy_type.
978 #p #pol @(eject … pol)
979qed.
980
981coercion eject_policy nocomposites: ∀p.∀pol:policy p. policy_type ≝ eject_policy on _pol:(policy ?) to policy_type.
982
983definition sigma: ∀p:pseudo_assembly_program. policy p → Word → Word ≝
984 λp,policy.
985  match sigma_safe p (eject … policy) return λr:option (Word → Word). r ≠ None … → Word → Word with
986   [ None ⇒ λabs. ⊥
987   | Some r ⇒ λ_.r] (sig2 … policy).
988 cases abs /2/
989qed.
990
991example sigma_0: ∀p,pol. sigma p pol (bitvector_of_nat ? 0) = bitvector_of_nat ? 0.
992 cases daemon.
993qed.
994
995definition expand_pseudo_instruction:
996 ∀program:pseudo_assembly_program.∀pol: policy program.
997  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pc.
998  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
999  lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)) →
1000  let pi ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in
1001  pc = sigma program pol ppc →
1002  Σres:list instruction. Some … res = expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) pi
1003≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pc,prf1,prf2,prf3.
1004   match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) (\fst (fetch_pseudo_instruction (\snd program) ppc)) with
1005    [ None ⇒ let dummy ≝ [ ] in dummy
1006    | Some res ⇒ res ].
1007 [ cases daemon
1008 | >p %]
1009qed.
1010
1011(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1012definition assembly_1_pseudoinstruction:
1013 ∀program:pseudo_assembly_program.∀pol: policy program.
1014  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1015  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1016  lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)) →
1017  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
1018  Σres:(nat × (list Byte)).
1019   let 〈len,code〉 ≝ res in
1020    sigma program pol (\snd (half_add ? ppc (bitvector_of_nat ? 1))) =
1021     bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len)
1022≝ λprogram: pseudo_assembly_program.
1023  λpol: policy program.
1024  λppc: Word.
1025  λlookup_labels.
1026  λlookup_datalabels.
1027  λpi.
1028  λprf1,prf2,prf3.
1029   match assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi with
1030    [ None ⇒ let dummy ≝ 〈0,[ ]〉 in dummy
1031    | Some res ⇒ res ].
1032 [ @⊥ elim pi in p [*]
1033   try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs
1034   generalize in match (jmeq_to_eq ??? abs) -abs; #abs whd in abs:(??%?); try destruct(abs)
1035   whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?);
1036   (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *)
1037   cases daemon
1038 | cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta;
1039   (* THIS SHOULD BE TRUE INSTEAD *)
1040   cases daemon]
1041qed.
1042
1043(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1044definition construct_costs':
1045 ∀program. policy program → nat → nat → ? → ? →
1046  Σres:(nat × (BitVectorTrie Word 16)). True
1047
1048  λprogram.λpol: policy program.λppc,pc,costs,i.
1049   match construct_costs_safe program pol ppc pc costs i with
1050    [ None ⇒ let dummy ≝ 〈0, Stub ??〉 in dummy
1051    | Some res ⇒ res ].
1052 [ cases daemon
1053 | %]
1054qed.
1055
1056definition construct_costs ≝
1057 λprogram,pol,ppc,pc,costs,i. eject … (construct_costs' program pol ppc pc costs i).
1058
1059axiom construct_costs_sigma:
1060 ∀p.∀pol:policy p.∀ppc,pc,costs,i.
1061  bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) →
1062   bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)).
1063
1064axiom tech_pc_sigma00_append_Some:
1065 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
1066  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
1067   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
1068
1069definition build_maps:
1070 ∀pseudo_program.∀pol:policy pseudo_program.
1071  Σres:((BitVectorTrie Word 16) × (BitVectorTrie Word 16)).
1072   let 〈labels,costs〉 ≝ res in
1073    ∀id. occurs_exactly_once id (\snd pseudo_program) →
1074     lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem (\snd pseudo_program) id)
1075
1076  λpseudo_program.λpol:policy pseudo_program.
1077  let result ≝
1078   foldl_strong
1079    (option Identifier × pseudo_instruction)
1080    (λpre. Σres:((BitVectorTrie Word 16) × (nat × (nat × (BitVectorTrie Word 16)))).
1081      let 〈labels,ppc_pc_costs〉 ≝ res in
1082      let 〈ppc',pc_costs〉 ≝ ppc_pc_costs in
1083      let 〈pc',costs〉 ≝ pc_costs in
1084       And ( And (
1085       And (bitvector_of_nat ? pc' = sigma pseudo_program pol (bitvector_of_nat ? ppc')) (*∧*)
1086       (ppc' = length … pre)) (*∧ *)
1087       (tech_pc_sigma00 pseudo_program (eject … pol) pre = Some ? 〈ppc',pc'〉)) (*∧*)
1088       (∀id. occurs_exactly_once id pre →
1089        lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem pre id)))
1090    (\snd pseudo_program)
1091    (λprefix,i,tl,prf,t.
1092      let 〈labels, ppc_pc_costs〉 ≝ t in
1093      let 〈ppc, pc_costs〉 ≝ ppc_pc_costs in
1094      let 〈pc,costs〉 ≝ pc_costs in
1095       let 〈label, i'〉 ≝ i in
1096       let labels ≝
1097         match label with
1098         [ None ⇒ labels
1099         | Some label ⇒
1100           let program_counter_bv ≝ bitvector_of_nat ? pc in
1101             insert ? ? label program_counter_bv labels]
1102       in
1103         let construct ≝ construct_costs pseudo_program pol ppc pc costs i' in
1104          〈labels, 〈S ppc,construct〉〉
1105    ) 〈(Stub ? ?), 〈0, 〈0, Stub ? ?〉〉〉
1106  in
1107   let 〈labels, ppc_pc_costs〉 ≝ result in
1108   let 〈ppc,pc_costs〉 ≝ ppc_pc_costs in
1109   let 〈pc, costs〉 ≝ pc_costs in
1110    〈labels, costs〉.
1111 [2: whd generalize in match (sig2 … t) >p >p1 >p2 >p3 * * * #IHn1 #IH0 #IH1 #IH2
1112   generalize in match (refl … construct); cases construct in ⊢ (???% → %) #PC #CODE #JMEQ % [% [%]]
1113   [ <(construct_costs_sigma … costs i1 IHn1) change with (? = ?(\fst construct)) >JMEQ %
1114   | >length_append <IH0 normalize; -IHn1; (*CSC: otherwise it diverges!*) //
1115   | >(tech_pc_sigma00_append_Some … costs … IH1) change with (Some … 〈S ppc,\fst construct〉 = ?) >JMEQ %
1116   | #id normalize nodelta; -labels1; cases label; normalize nodelta
1117     [ #K <address_of_word_labels_code_mem_None [2:@K] @IH2 -IHn1; (*CSC: otherwise it diverges!*) //
1118     | #l #H generalize in match (occurs_exactly_once_Some ???? H) in ⊢ ?;
1119       generalize in match (refl … (eq_bv ? l id)); cases (eq_bv … l id) in ⊢ (???% → %)
1120        [ #EQ #_ <(eq_bv_eq … EQ) >lookup_insert_hit >address_of_word_labels_code_mem_Some_hit
1121          <IH0 [@IHn1 | <(eq_bv_eq … EQ) in H #H @H]
1122        | #EQ change with (occurs_exactly_once ?? → ?) #K >lookup_insert_miss [2: -IHn1; /2/]
1123          <(address_of_word_labels_code_mem_Some_miss … EQ … H) @IH2 -IHn1; //]]]
1124 |3: whd % [% [%]] // [>sigma_0//] #id normalize in ⊢ (% → ?) #abs @⊥ //
1125 | generalize in match (sig2 … result) in ⊢ ?;
1126   normalize nodelta in p ⊢ %; -result; >p in ⊢ (match % with [_ ⇒ ?] → ?)
1127   normalize nodelta; >p1 normalize nodelta; >p2; normalize nodelta; * #_; #H @H]
1128qed.
1129
1130definition assembly:
1131 ∀p:pseudo_assembly_program. policy p → list Byte × (BitVectorTrie Identifier 16) ≝
1132  λp.let 〈preamble, instr_list〉 ≝ p in
1133   λpol.
1134    let 〈labels,costs〉 ≝ build_maps 〈preamble,instr_list〉 pol in
1135    let datalabels ≝ construct_datalabels preamble in
1136    let lookup_labels ≝ λx. lookup ? ? x labels (zero ?) in
1137    let lookup_datalabels ≝ λx. lookup ? ? x datalabels (zero ?) in
1138    let result ≝
1139     foldl_strong
1140      (option Identifier × pseudo_instruction)
1141      (λpre. Σpc_ppc_code:(Word × (Word × (list Byte))).
1142        let 〈pc,ppc_code〉 ≝ pc_ppc_code in
1143        let 〈ppc,code〉 ≝ ppc_code in
1144         ∀ppc'.
1145          let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' in
1146          let 〈len,assembledi〉 ≝
1147           assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc' lookup_labels lookup_datalabels pi ??? in
1148           True)
1149      instr_list
1150      (λprefix,hd,tl,prf,pc_ppc_code.
1151        let 〈pc, ppc_code〉 ≝ pc_ppc_code in
1152        let 〈ppc, code〉 ≝ ppc_code in
1153        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc lookup_labels lookup_datalabels (\snd hd) ??? in
1154        let 〈new_pc, flags〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in
1155        let new_ppc ≝ \snd (half_add ? ppc (bitvector_of_nat ? 1)) in
1156         〈new_pc, 〈new_ppc, (code @ program)〉〉)
1157      〈(zero ?), 〈(zero ?), [ ]〉〉
1158    in
1159     〈\snd (\snd result), costs〉.
1160 [2,5: %
1161 |*: cases daemon ]
1162qed.
1163
1164definition assembly_unlabelled_program: assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝
1165 λp. Some ? (〈foldr ? ? (λi,l. assembly1 i @ l) [ ] p, Stub …〉).
Note: See TracBrowser for help on using the repository browser.