source: src/ASM/Assembly.ma @ 1691

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