source: src/ASM/Assembly.ma @ 2067

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

Warning: this commit adds an hypothesis that breaks all of assembly stuff.

File size: 53.3 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 
17(* Functions that define the conditions under which jumps are possible *)
18definition short_jump_cond: Word → Word → (*pseudo_instruction →*)
19  bool × (BitVector 8) ≝
20  λpc_plus_jmp_length.λaddr.(*λinstr.*)
21  let 〈result, flags〉 ≝ sub_16_with_carry addr pc_plus_jmp_length false in
22  let 〈upper, lower〉 ≝ vsplit ? 9 7 result in
23    if get_index' ? 2 0 flags then
24      〈eq_bv 9 upper [[true;true;true;true;true;true;true;true;true]], true:::lower〉
25    else
26      〈eq_bv 9 upper (zero …), false:::lower〉.
27 
28definition medium_jump_cond: Word → Word → (*pseudo_instruction →*)
29  bool × (BitVector 11) ≝
30  λpc_plus_jmp_length.λaddr.(*λinstr.*)
31  let 〈fst_5_addr, rest_addr〉 ≝ vsplit bool 5 11 addr in
32  let 〈fst_5_pc, rest_pc〉 ≝ vsplit bool 5 11 pc_plus_jmp_length in
33  〈eq_bv 5 fst_5_addr fst_5_pc, rest_addr〉.
34
35definition assembly_preinstruction ≝
36  λA: Type[0].
37  λaddr_of: A → Byte. (* relative *)
38  λpre: preinstruction A.
39  match pre with
40  [ ADD addr1 addr2 ⇒
41     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
42      [ REGISTER r ⇒ λ_.[ ([[false;false;true;false;true]]) @@ r ]
43      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;false;false;true;false;true]]); b1 ]
44      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;false;false;true;true;i1]]) ]
45      | DATA b1 ⇒ λ_. [ ([[false;false;true;false;false;true;false;false]]) ; b1 ]
46      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
47  | ADDC addr1 addr2 ⇒
48     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
49      [ REGISTER r ⇒ λ_.[ ([[false;false;true;true;true]]) @@ r ]
50      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;true;false;true;false;true]]); b1 ]
51      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;true;false;true;true;i1]]) ]
52      | DATA b1 ⇒ λ_. [ ([[false;false;true;true;false;true;false;false]]) ; b1 ]
53      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
54  | ANL addrs ⇒
55     match addrs with
56      [ inl addrs ⇒ match addrs with
57         [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
58           match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
59            [ REGISTER r ⇒ λ_.[ ([[false;true;false;true;true]]) @@ r ]
60            | DIRECT b1 ⇒ λ_.[ ([[false;true;false;true;false;true;false;true]]); b1 ]
61            | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;true;false;true;true;i1]]) ]
62            | DATA b1 ⇒ λ_. [ ([[false;true;false;true;false;true;false;false]]) ; b1 ]
63            | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
64         | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
65            let b1 ≝
66             match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
67              [ DIRECT b1 ⇒ λ_.b1
68              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
69            match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
70             [ ACC_A ⇒ λ_.[ ([[false;true;false;true;false;false;true;false]]) ; b1 ]
71             | DATA b2 ⇒ λ_. [ ([[false;true;false;true;false;false;true;true]]) ; b1 ; b2 ]
72             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
73         ]
74      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
75         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
76          [ BIT_ADDR b1 ⇒ λ_.[ ([[true;false;false;false;false;false;true;false]]) ; b1 ]
77          | N_BIT_ADDR b1 ⇒ λ_. [ ([[true;false;true;true;false;false;false;false]]) ; b1 ]
78          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
79  | CLR addr ⇒
80     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
81      [ ACC_A ⇒ λ_.
82         [ ([[true; true; true; false; false; true; false; false]]) ]
83      | CARRY ⇒ λ_.
84         [ ([[true; true; false; false; false; false; true; true]]) ]
85      | BIT_ADDR b1 ⇒ λ_.
86         [ ([[true; true; false; false; false; false; true; false]]) ; b1 ]
87      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
88  | CPL addr ⇒
89     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
90      [ ACC_A ⇒ λ_.
91         [ ([[true; true; true; true; false; true; false; false]]) ]
92      | CARRY ⇒ λ_.
93         [ ([[true; false; true; true; false; false; true; true]]) ]
94      | BIT_ADDR b1 ⇒ λ_.
95         [ ([[true; false; true; true; false; false; true; false]]) ; b1 ]
96      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
97  | DA addr ⇒
98     [ ([[true; true; false; true; false; true; false; false]]) ]
99  | DEC addr ⇒
100     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with
101      [ ACC_A ⇒ λ_.
102         [ ([[false; false; false; true; false; true; false; false]]) ]
103      | REGISTER r ⇒ λ_.
104         [ ([[false; false; false; true; true]]) @@ r ]
105      | DIRECT b1 ⇒ λ_.
106         [ ([[false; false; false; true; false; true; false; true]]); b1 ]
107      | INDIRECT i1 ⇒ λ_.
108         [ ([[false; false; false; true; false; true; true; i1]]) ]
109      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
110      | DJNZ addr1 addr2 ⇒
111         let b2 ≝ addr_of addr2 in
112         match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with
113          [ REGISTER r ⇒ λ_.
114             [ ([[true; true; false; true; true]]) @@ r ; b2 ]
115          | DIRECT b1 ⇒ λ_.
116             [ ([[true; true; false; true; false; true; false; true]]); b1; b2 ]
117          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
118      | JC addr ⇒
119        let b1 ≝ addr_of addr in
120          [ ([[false; true; false; false; false; false; false; false]]); b1 ]
121      | JNC addr ⇒
122         let b1 ≝ addr_of addr in
123           [ ([[false; true; false; true; false; false; false; false]]); b1 ]
124      | JZ addr ⇒
125         let b1 ≝ addr_of addr in
126           [ ([[false; true; true; false; false; false; false; false]]); b1 ]
127      | JNZ addr ⇒
128         let b1 ≝ addr_of addr in
129           [ ([[false; true; true; true; false; false; false; false]]); b1 ]
130      | JB addr1 addr2 ⇒
131         let b2 ≝ addr_of addr2 in
132         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
133          [ BIT_ADDR b1 ⇒ λ_.
134             [ ([[false; false; true; false; false; false; false; false]]); b1; b2 ]
135          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
136      | JNB addr1 addr2 ⇒
137         let b2 ≝ addr_of addr2 in
138         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
139          [ BIT_ADDR b1 ⇒ λ_.
140             [ ([[false; false; true; true; false; false; false; false]]); b1; b2 ]
141          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
142      | JBC addr1 addr2 ⇒
143         let b2 ≝ addr_of addr2 in
144         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
145          [ BIT_ADDR b1 ⇒ λ_.
146             [ ([[false; false; false; true; false; false; false; false]]); b1; b2 ]
147          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
148      | CJNE addrs addr3 ⇒
149         let b3 ≝ addr_of addr3 in
150         match addrs with
151          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
152             match addr2 return λx. bool_to_Prop (is_in ? [[direct;data]] x) → ? with
153              [ DIRECT b1 ⇒ λ_.
154                 [ ([[true; false; true; true; false; true; false; true]]); b1; b3 ]
155              | DATA b1 ⇒ λ_.
156                 [ ([[true; false; true; true; false; true; false; false]]); b1; b3 ]
157              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
158          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
159             let b2 ≝
160              match addr2 return λx. bool_to_Prop (is_in ? [[data]] x) → ? with
161               [ DATA b2 ⇒ λ_. b2
162               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) in
163             match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with
164              [ REGISTER r ⇒ λ_.
165                 [ ([[true; false; true; true; true]]) @@ r; b2; b3 ]
166              | INDIRECT i1 ⇒ λ_.
167                 [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ]
168              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
169         ]
170  | DIV addr1 addr2 ⇒
171     [ ([[true;false;false;false;false;true;false;false]]) ]
172  | INC addr ⇒
173     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with
174      [ ACC_A ⇒ λ_.
175         [ ([[false;false;false;false;false;true;false;false]]) ]         
176      | REGISTER r ⇒ λ_.
177         [ ([[false;false;false;false;true]]) @@ r ]
178      | DIRECT b1 ⇒ λ_.
179         [ ([[false; false; false; false; false; true; false; true]]); b1 ]
180      | INDIRECT i1 ⇒ λ_.
181        [ ([[false; false; false; false; false; true; true; i1]]) ]
182      | DPTR ⇒ λ_.
183        [ ([[true;false;true;false;false;false;true;true]]) ]
184      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
185  | MOV addrs ⇒
186     match addrs with
187      [ inl addrs ⇒
188         match addrs with
189          [ inl addrs ⇒
190             match addrs with
191              [ inl addrs ⇒
192                 match addrs with
193                  [ inl addrs ⇒
194                     match addrs with
195                      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
196                         match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
197                          [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ]
198                          | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ]
199                          | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ]
200                          | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ]
201                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
202                      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
203                         match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with
204                          [ REGISTER r ⇒ λ_.
205                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
206                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ]
207                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ]
208                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ]
209                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
210                          | INDIRECT i1 ⇒ λ_.
211                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
212                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ]
213                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ]
214                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ]
215                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
216                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
217                  | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
218                     let b1 ≝
219                      match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
220                       [ DIRECT b1 ⇒ λ_. b1
221                       | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
222                     match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with
223                      [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1]
224                      | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ]
225                      | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ]
226                      | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ]
227                      | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ]
228                      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
229              | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
230                 match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with
231                  [ DATA16 w ⇒ λ_.
232                     let b1_b2 ≝ vsplit ? 8 8 w in
233                     let b1 ≝ \fst b1_b2 in
234                     let b2 ≝ \snd b1_b2 in
235                      [ ([[true;false;false;true;false;false;false;false]]); b1; b2]
236                  | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
237          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
238             match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
239              [ BIT_ADDR b1 ⇒ λ_.
240                 [ ([[true;false;true;false;false;false;true;false]]); b1 ]
241              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
242      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
243         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
244          [ BIT_ADDR b1 ⇒ λ_.
245             [ ([[true;false;false;true;false;false;true;false]]); b1 ]
246          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
247  | MOVX addrs ⇒
248     match addrs with
249      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
250         match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
251          [ EXT_INDIRECT i1 ⇒ λ_.
252             [ ([[true;true;true;false;false;false;true;i1]]) ]
253          | EXT_INDIRECT_DPTR ⇒ λ_.
254             [ ([[true;true;true;false;false;false;false;false]]) ]
255          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
256      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
257         match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
258          [ EXT_INDIRECT i1 ⇒ λ_.
259             [ ([[true;true;true;true;false;false;true;i1]]) ]
260          | EXT_INDIRECT_DPTR ⇒ λ_.
261             [ ([[true;true;true;true;false;false;false;false]]) ]
262          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
263  | MUL addr1 addr2 ⇒
264     [ ([[true;false;true;false;false;true;false;false]]) ]
265  | NOP ⇒
266     [ ([[false;false;false;false;false;false;false;false]]) ]
267  | ORL addrs ⇒
268     match addrs with
269      [ inl addrs ⇒
270         match addrs with
271          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
272             match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with
273             [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ]
274             | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ]
275             | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ]
276             | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ]
277             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
278          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
279            let b1 ≝
280              match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
281               [ DIRECT b1 ⇒ λ_. b1
282               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
283             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
284              [ ACC_A ⇒ λ_.
285                 [ ([[false;true;false;false;false;false;true;false]]); b1 ]
286              | DATA b2 ⇒ λ_.
287                 [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ]
288              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
289      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in     
290         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
291          [ BIT_ADDR b1 ⇒ λ_.
292             [ ([[false;true;true;true;false;false;true;false]]); b1 ]
293          | N_BIT_ADDR b1 ⇒ λ_.
294             [ ([[true;false;true;false;false;false;false;false]]); b1 ]
295          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
296  | POP addr ⇒
297     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
298      [ DIRECT b1 ⇒ λ_.
299         [ ([[true;true;false;true;false;false;false;false]]) ; b1 ]
300      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
301  | PUSH addr ⇒
302     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
303      [ DIRECT b1 ⇒ λ_.
304         [ ([[true;true;false;false;false;false;false;false]]) ; b1 ]
305      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
306  | RET ⇒
307     [ ([[false;false;true;false;false;false;true;false]]) ]
308  | RETI ⇒
309     [ ([[false;false;true;true;false;false;true;false]]) ]
310  | RL addr ⇒
311     [ ([[false;false;true;false;false;false;true;true]]) ]
312  | RLC addr ⇒
313     [ ([[false;false;true;true;false;false;true;true]]) ]
314  | RR addr ⇒
315     [ ([[false;false;false;false;false;false;true;true]]) ]
316  | RRC addr ⇒
317     [ ([[false;false;false;true;false;false;true;true]]) ]
318  | SETB addr ⇒     
319     match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with
320      [ CARRY ⇒ λ_.
321         [ ([[true;true;false;true;false;false;true;true]]) ]
322      | BIT_ADDR b1 ⇒ λ_.
323         [ ([[true;true;false;true;false;false;true;false]]); b1 ]
324      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
325  | SUBB addr1 addr2 ⇒
326     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
327      [ REGISTER r ⇒ λ_.
328         [ ([[true;false;false;true;true]]) @@ r ]
329      | DIRECT b1 ⇒ λ_.
330         [ ([[true;false;false;true;false;true;false;true]]); b1]
331      | INDIRECT i1 ⇒ λ_.
332         [ ([[true;false;false;true;false;true;true;i1]]) ]
333      | DATA b1 ⇒ λ_.
334         [ ([[true;false;false;true;false;true;false;false]]); b1]
335      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
336  | SWAP addr ⇒
337     [ ([[true;true;false;false;false;true;false;false]]) ]
338  | XCH addr1 addr2 ⇒
339     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with
340      [ REGISTER r ⇒ λ_.
341         [ ([[true;true;false;false;true]]) @@ r ]
342      | DIRECT b1 ⇒ λ_.
343         [ ([[true;true;false;false;false;true;false;true]]); b1]
344      | INDIRECT i1 ⇒ λ_.
345         [ ([[true;true;false;false;false;true;true;i1]]) ]
346      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
347  | XCHD addr1 addr2 ⇒
348     match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with
349      [ INDIRECT i1 ⇒ λ_.
350         [ ([[true;true;false;true;false;true;true;i1]]) ]
351      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
352  | XRL addrs ⇒
353     match addrs with
354      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
355         match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with
356          [ REGISTER r ⇒ λ_.
357             [ ([[false;true;true;false;true]]) @@ r ]
358          | DIRECT b1 ⇒ λ_.
359             [ ([[false;true;true;false;false;true;false;true]]); b1]
360          | INDIRECT i1 ⇒ λ_.
361             [ ([[false;true;true;false;false;true;true;i1]]) ]
362          | DATA b1 ⇒ λ_.
363             [ ([[false;true;true;false;false;true;false;false]]); b1]
364          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
365      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
366         let b1 ≝
367          match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
368           [ DIRECT b1 ⇒ λ_. b1
369           | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
370         match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
371          [ ACC_A ⇒ λ_.
372             [ ([[false;true;true;false;false;false;true;false]]); b1 ]         
373          | DATA b2 ⇒ λ_.
374             [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ]
375          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
376       ].
377
378definition assembly1 ≝
379 λi: instruction.
380 match i with
381  [ ACALL addr ⇒
382     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
383      [ ADDR11 w ⇒ λ_.
384         let v1_v2 ≝ vsplit ? 3 8 w in
385         let v1 ≝ \fst v1_v2 in
386         let v2 ≝ \snd v1_v2 in
387          [ (v1 @@ [[true; false; false; false; true]]) ; v2 ]
388      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
389  | AJMP addr ⇒
390     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
391      [ ADDR11 w ⇒ λ_.
392         let v1_v2 ≝ vsplit ? 3 8 w in
393         let v1 ≝ \fst v1_v2 in
394         let v2 ≝ \snd v1_v2 in
395          [ (v1 @@ [[false; false; false; false; true]]) ; v2 ]
396      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
397  | JMP adptr ⇒
398     [ ([[false;true;true;true;false;false;true;true]]) ]
399  | LCALL addr ⇒
400     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
401      [ ADDR16 w ⇒ λ_.
402         let b1_b2 ≝ vsplit ? 8 8 w in
403         let b1 ≝ \fst b1_b2 in
404         let b2 ≝ \snd b1_b2 in
405          [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ]         
406      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
407  | LJMP addr ⇒
408     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
409      [ ADDR16 w ⇒ λ_.
410         let b1_b2 ≝ vsplit ? 8 8 w in
411         let b1 ≝ \fst b1_b2 in
412         let b2 ≝ \snd b1_b2 in
413          [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ]         
414      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
415  | MOVC addr1 addr2 ⇒
416     match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with
417      [ ACC_DPTR ⇒ λ_.
418         [ ([[true;false;false;true;false;false;true;true]]) ]
419      | ACC_PC ⇒ λ_.
420         [ ([[true;false;false;false;false;false;true;true]]) ]
421      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
422  | SJMP addr ⇒
423     match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with
424      [ RELATIVE b1 ⇒ λ_.
425         [ ([[true;false;false;false;false;false;false;false]]); b1 ]
426      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
427  | RealInstruction instr ⇒
428    assembly_preinstruction [[ relative ]]
429      (λx.
430        match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with
431        [ RELATIVE r ⇒ λ_. r
432        | _ ⇒ λabsd. ⊥
433        ] (subaddressing_modein … x)) instr
434  ].
435  cases absd
436qed.
437
438(* XXX: pc_plus_sjmp_length used to be just sigma of ppc.  This is incorrect
439        as relative lengths are computed from the *end* of the SJMP, not from
440        the beginning.
441*)
442definition expand_relative_jump_internal:
443 ∀lookup_labels:Identifier → Word.∀sigma:Word → Word.
444 Identifier → Word → ([[relative]] → preinstruction [[relative]]) →
445 list instruction
446 ≝
447  λlookup_labels.λsigma.λlbl.λppc,i.
448   let lookup_address ≝ sigma (lookup_labels lbl) in
449   let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
450   let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
451   if sj_possible
452   then
453     let address ≝ RELATIVE disp in
454       [ RealInstruction (i address) ]
455   else
456    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
457      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
458      LJMP (ADDR16 lookup_address)
459    ].
460  %
461qed.
462
463(*definition rel_jump_length_ok ≝
464 λlookup_address:Word.
465 λpc:Word.
466 Σjump_len:jump_length.
467  (* CSC,JPB: Cheating here, use Jaap's better definition select_reljump_length *)
468  ∀(*x,*)y. expand_relative_jump_internal_safe lookup_address jump_len (*x*) pc y ≠ None ?.
469
470lemma eject_rel_jump_length: ∀x,y. rel_jump_length_ok x y → jump_length.
471 #x #y #p @(pi1 … p)
472qed.
473
474coercion eject_rel_jump_length nocomposites:
475 ∀x,y.∀pol:rel_jump_length_ok x y. jump_length ≝
476 eject_rel_jump_length on _pol:(rel_jump_length_ok ??) to jump_length.*)
477
478(*definition expand_relative_jump_internal:
479 ∀lookup_address:Word. ∀pc:Word. ([[relative]] → preinstruction [[relative]]) →
480 list instruction
481≝ λlookup_address,pc,i.
482   match expand_relative_jump_internal_safe lookup_address pc i
483   return λres. res ≠ None ? → ?
484   with
485   [ None ⇒ λabs.⊥
486   | Some res ⇒ λ_.res ] (pi2 … jump_len i).
487 cases abs /2/
488qed.*)
489
490definition expand_relative_jump:
491  ∀lookup_labels.∀sigma.
492  Word → (*jump_length →*)
493  preinstruction Identifier → list instruction ≝
494  λlookup_labels: Identifier → Word.
495  λsigma:Word → Word.
496  λppc: Word.
497  (*λjmp_len: jump_length.*)
498  λi: preinstruction Identifier.
499  (*let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in*)
500  match i with
501  [ JC jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JC ?)
502  | JNC jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JNC ?)
503  | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JB ? baddr)
504  | JZ jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JZ ?)
505  | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JNZ ?)
506  | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JBC ? baddr)
507  | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JNB ? baddr)
508  | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (CJNE ? addr)
509  | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (DJNZ ? addr)
510  | ADD arg1 arg2 ⇒ [ ADD ? arg1 arg2 ]
511  | ADDC arg1 arg2 ⇒ [ ADDC ? arg1 arg2 ]
512  | SUBB arg1 arg2 ⇒ [ SUBB ? arg1 arg2 ]
513  | INC arg ⇒ [ INC ? arg ]
514  | DEC arg ⇒ [ DEC ? arg ]
515  | MUL arg1 arg2 ⇒ [ MUL ? arg1 arg2 ]
516  | DIV arg1 arg2 ⇒ [ DIV ? arg1 arg2 ]
517  | DA arg ⇒ [ DA ? arg ]
518  | ANL arg ⇒ [ ANL ? arg ]
519  | ORL arg ⇒ [ ORL ? arg ]
520  | XRL arg ⇒ [ XRL ? arg ]
521  | CLR arg ⇒ [ CLR ? arg ]
522  | CPL arg ⇒ [ CPL ? arg ]
523  | RL arg ⇒ [ RL ? arg ]
524  | RR arg ⇒ [ RR ? arg ]
525  | RLC arg ⇒ [ RLC ? arg ]
526  | RRC arg ⇒ [ RRC ? arg ]
527  | SWAP arg ⇒ [ SWAP ? arg ]
528  | MOV arg ⇒ [ MOV ? arg ]
529  | MOVX arg ⇒ [ MOVX ? arg ]
530  | SETB arg ⇒ [ SETB ? arg ]
531  | PUSH arg ⇒ [ PUSH ? arg ]
532  | POP arg ⇒ [ POP ? arg ]
533  | XCH arg1 arg2 ⇒ [ XCH ? arg1 arg2 ]
534  | XCHD arg1 arg2 ⇒ [ XCHD ? arg1 arg2 ]
535  | RET ⇒ [ RET ? ]
536  | RETI ⇒ [ RETI ? ]
537  | NOP ⇒ [ RealInstruction (NOP ?) ]
538  ].
539
540definition expand_pseudo_instruction:
541    ∀lookup_labels.
542    ∀sigma: Word → Word.
543    ∀policy: Word → bool.
544      Word → ? → pseudo_instruction → list instruction ≝
545  λlookup_labels: Identifier → Word.
546  λsigma: Word → Word.
547  λpolicy: Word → bool.
548  λppc.
549  λlookup_datalabels:Identifier → Word.
550  λi.
551  match i with
552  [ Cost cost ⇒ [ ]
553  | Comment comment ⇒ [ ]
554  | Call call ⇒
555    let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
556    let lookup_address ≝ sigma (lookup_labels call) in
557    let 〈mj_possible, disp〉 ≝ medium_jump_cond pc_plus_jmp_length lookup_address in
558    let do_a_long ≝ policy ppc in
559    if mj_possible ∧ ¬ do_a_long then
560      let address ≝ ADDR11 disp in
561        [ ACALL address ]
562    else
563      let address ≝ ADDR16 lookup_address in
564        [ LCALL address ]
565  | Mov d trgt ⇒
566    let address ≝ DATA16 (lookup_datalabels trgt) in
567      [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))]
568  | Instruction instr ⇒ expand_relative_jump lookup_labels sigma ppc instr
569  | Jmp jmp ⇒
570    let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
571    let do_a_long ≝ policy ppc in
572    let lookup_address ≝ sigma (lookup_labels jmp) in
573    let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
574    if sj_possible ∧ ¬ do_a_long then
575      let address ≝ RELATIVE disp in
576        [ SJMP address ]
577    else
578      let 〈mj_possible, disp2〉 ≝ medium_jump_cond pc_plus_jmp_length lookup_address in
579      if mj_possible ∧ ¬ do_a_long then
580        let address ≝ ADDR11 disp2 in
581          [ AJMP address ]
582      else   
583        let address ≝ ADDR16 lookup_address in
584        [ LJMP address ]
585  ].
586  %
587qed.
588
589(*
590(*X?
591definition jump_length_ok ≝
592 λlookup_labels:Identifier → Word.
593 λpc:Word.
594 Σjump_len:jump_length.
595  (* CSC,JPB: Cheating here, use Jaap's better definition select_reljump_length *)
596  ∀x,y.expand_pseudo_instruction_safe lookup_labels pc jump_len x y ≠ None ?.
597*)
598
599lemma eject_jump_length: ∀x,y. jump_length_ok x y → jump_length.
600 #x #y #p @(pi1 … p)
601qed.
602
603coercion eject_jump_length nocomposites:
604 ∀x,y.∀pol:jump_length_ok x y. jump_length ≝
605 eject_jump_length on _pol:(jump_length_ok ??) to jump_length.
606
607definition expand_pseudo_instruction:
608 ∀lookup_labels:Identifier → Word. ∀pc:Word. jump_length_ok lookup_labels pc →
609 ? → pseudo_instruction → list instruction ≝
610 λlookup_labels,pc,jump_len,lookup_datalabels,i.
611   match expand_pseudo_instruction_safe lookup_labels pc jump_len lookup_datalabels i
612   return λres. res ≠ None ? → ?
613   with
614   [ None ⇒ λabs.⊥
615   | Some res ⇒ λ_.res ] (pi2 … jump_len lookup_datalabels i).
616 cases abs /2/
617qed.
618*)
619(*X?
620definition policy_type ≝
621 λlookup_labels:Identifier → Word.
622 ∀pc:Word. jump_length_ok lookup_labels pc.
623*)
624
625(*definition policy_type2 ≝
626 λprogram.
627  Σpol:Word → jump_length.
628   let lookup_labels ≝
629    (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) in
630   ∀pc:Word. let jump_len ≝ pol pc in
631    ∀x,y.expand_pseudo_instruction_safe lookup_labels pc jump_len x y ≠ None ?.*)
632 
633definition assembly_1_pseudoinstruction ≝
634  λlookup_labels.
635  λsigma: Word → Word.
636  λpolicy: Word → bool.
637  λppc: Word.
638  λlookup_datalabels.
639  λi.
640  let pseudos ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels i in
641  let mapped ≝ map ? ? assembly1 pseudos in
642  let flattened ≝ flatten ? mapped in
643  let pc_len ≝ length ? flattened in
644   〈pc_len, flattened〉.
645
646definition instruction_size ≝
647  λlookup_labels.
648  λsigma: Word → Word.
649  λpolicy: Word → bool.
650  λppc.
651  λi.
652    \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc (λx.zero …) i).
653
654(* Jaap: never used
655lemma fetch_pseudo_instruction_prefix:
656  ∀prefix.∀x.∀ppc.ppc < (|prefix|) →
657  fetch_pseudo_instruction prefix (bitvector_of_nat ? ppc) =
658  fetch_pseudo_instruction (prefix@x) (bitvector_of_nat ? ppc).
659 #prefix #x #ppc elim prefix
660 [ #Hppc @⊥ @(absurd … Hppc) @le_to_not_lt @le_O_n
661 | #h #t #Hind #Hppc whd in match (fetch_pseudo_instruction ??);
662   whd in match (fetch_pseudo_instruction ((h::t)@x) ?);
663   >nth_append_first
664   [ //
665   | >nat_of_bitvector_bitvector_of_nat
666     [ @Hppc
667     | cases daemon (* XXX invariant *)
668     ]
669   ]
670 ]
671qed.
672*)
673
674(*CSC: move elsewhere *)
675lemma nth_safe_append:
676 ∀A,l,n,n_ok.
677  ∃pre,suff.
678   (pre @ [nth_safe A n l n_ok]) @ suff = l.
679#A #l elim l normalize
680 [ #n #abs cases (absurd ? abs (not_le_Sn_O ?))
681 | #hd #tl #IH #n cases n normalize
682   [ #_ %{[]} /2/
683   | #m #m_ok cases (IH m ?)
684     [ #pre * #suff #EQ %{(hd::pre)} %{suff} <EQ in ⊢ (???%); % | skip ]]
685qed.
686
687lemma fetch_pseudo_instruction_vsplit:
688 ∀instr_list,ppc,ppc_ok.
689  ∃pre,suff,lbl.
690   (pre @ [〈lbl,\fst (fetch_pseudo_instruction instr_list ppc ppc_ok)〉]) @ suff = instr_list.
691#instr_list #ppc #ppc_ok whd in match (fetch_pseudo_instruction ???);
692cases (nth_safe_append … instr_list … ppc_ok) #pre * #suff #EQ %{pre} %{suff}
693lapply EQ -EQ cases (nth_safe labelled_instruction ???) #lbl0 #instr normalize nodelta
694#EQ %{lbl0} @EQ
695qed.
696
697(*lemma sigma00_append:
698 ∀jump_expansion,l1,l2.
699 ∀acc:ℕ×ℕ×(BitVectorTrie Word 16).
700  sigma00 jump_expansion (l1@l2) acc =
701  sigma00 jump_expansion
702    l2 (pi1 ?? (sigma00 jump_expansion l1 acc)).*)
703
704(* lemma sigma00_strict:
705 ∀jump_expansion,l,acc. acc = None ? →
706  sigma00 jump_expansion l acc = None ….
707 #jump_expansion #l elim l
708  [ #acc #H >H %
709  | #hd #tl #IH #acc #H >H change with (sigma00 ? tl ? = ?) @IH % ]
710qed.
711
712lemma policy_ok_prefix_ok:
713 ∀program.∀pol:policy program.∀suffix,prefix.
714  prefix@suffix = \snd program →
715   sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) ≠ None ….
716 * #preamble #instr_list #pol #suffix #prefix #prf whd in prf:(???%);
717 generalize in match (pi2 ?? pol); whd in prf:(???%); <prf in pol; #pol
718 whd in match policy_ok; whd in match sigma_safe; whd in match sigma0;
719 normalize nodelta >sigma00_append
720 cases (sigma00 ?? prefix ?)
721  [2: #x #_ % #abs destruct(abs)
722  | * #abs @⊥ @abs >sigma00_strict % ]
723qed.
724
725lemma policy_ok_prefix_hd_ok:
726 ∀program.∀pol:policy program.∀suffix,hd,prefix,ppc_pc_map.
727  (prefix@[hd])@suffix = \snd program →
728   Some ? ppc_pc_map = sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) →
729    let 〈ppc,pc_map〉 ≝ ppc_pc_map in
730    let 〈program_counter, sigma_map〉 ≝ pc_map in
731    let 〈label, i〉 ≝ hd in
732     construct_costs_safe program pol ppc program_counter (Stub …) i ≠ None ….
733 * #preamble #instr_list #pol #suffix #hd #prefix #ppc_pc_map #EQ1 #EQ2
734 generalize in match (policy_ok_prefix_ok 〈preamble,instr_list〉 pol suffix
735  (prefix@[hd]) EQ1) in ⊢ ?; >sigma00_append <EQ2 whd in ⊢ (?(??%?) → ?);
736 @pair_elim #ppc #pc_map #EQ3 normalize nodelta
737 @pair_elim #pc #map #EQ4 normalize nodelta
738 @pair_elim #l' #i' #EQ5 normalize nodelta
739 cases (construct_costs_safe ??????) normalize
740  [* #abs @⊥ @abs % | #X #_ % #abs destruct(abs)]
741qed. *)
742
743(* JPB,CSC: this definition is now replaced by the expand_pseudo_instruction higher up
744definition expand_pseudo_instruction:
745 ∀program:pseudo_assembly_program.∀pol: policy program.
746  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pc.
747  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
748  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
749  let pi ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in
750  pc = sigma program pol ppc →
751  Σres:list instruction. Some … res = expand_pseudo_instruction_safe pc (lookup_labels pi) lookup_datalabels (pol ppc) pi
752≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pc,prf1,prf2,prf3.
753   match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) (\fst (fetch_pseudo_instruction (\snd program) ppc)) with
754    [ None ⇒ let dummy ≝ [ ] in dummy
755    | Some res ⇒ res ].
756 [ @⊥ whd in p:(??%??);
757   generalize in match (pi2 ?? pol); whd in ⊢ (% → ?);
758   whd in ⊢ (?(??%?) → ?); change with (sigma00 ????) in ⊢ (?(??(match % with [_ ⇒ ? | _ ⇒ ?])?) → ?);
759   generalize in match (refl … (sigma00 program pol (\snd program) (Some ? 〈O,〈O,Stub (BitVector 16) 16〉〉)));
760   cases (sigma00 ????) in ⊢ (??%? → %); normalize nodelta [#_ * #abs @abs %]
761   #res #K
762   cases (fetch_pseudo_instruction_vsplit (\snd program) ppc) #pre * #suff * #lbl #EQ1
763   generalize in match (policy_ok_prefix_hd_ok program pol … EQ1 ?) in ⊢ ?;
764   cases daemon (* CSC: XXXXXXXX Ero qui
765   
766    [3: @policy_ok_prefix_ok ]
767    | sigma00 program pol pre
768
769
770
771   QUA USARE LEMMA policy_ok_prefix_hd_ok combinato a lemma da fare che
772   fetch ppc = hd sse program = pre @ [hd] @ tl e |pre| = ppc
773   per concludere construct_costs_safe ≠ None *)
774 | >p %]
775qed. *)
776
777(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
778(* definition assembly_1_pseudoinstruction':
779 ∀program:pseudo_assembly_program.∀pol: policy program.
780  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
781  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
782  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
783  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
784  Σres:(nat × (list Byte)).
785   res = assembly_1_pseudoinstruction program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi ∧
786   let 〈len,code〉 ≝ res in
787    sigma program pol (add ? ppc (bitvector_of_nat ? 1)) =
788     bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len)
789≝ λprogram: pseudo_assembly_program.
790  λpol: policy program.
791  λppc: Word.
792  λlookup_labels.
793  λlookup_datalabels.
794  λpi.
795  λprf1,prf2,prf3.
796   assembly_1_pseudoinstruction program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi.
797 [ @⊥ elim pi in p; [*]
798   try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs
799   generalize in match (jmeq_to_eq ??? abs); -abs; #abs whd in abs:(??%?); try destruct(abs)
800   whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?);
801   (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *)
802   cases daemon
803 | % [ >p %]
804   cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta;
805   (* THIS SHOULD BE TRUE INSTEAD *)
806   cases daemon]
807qed.
808
809definition assembly_1_pseudoinstruction:
810 ∀program:pseudo_assembly_program.∀pol: policy program.
811  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
812  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
813  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
814  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
815   nat × (list Byte)
816≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pi,prf1,prf2,prf3.
817   assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1
818    prf2 prf3.
819
820lemma assembly_1_pseudoinstruction_ok1:
821 ∀program:pseudo_assembly_program.∀pol: policy program.
822  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
823  ∀prf1:lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)).
824  ∀prf2:lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)).
825  ∀prf3:\fst (fetch_pseudo_instruction (\snd program) ppc) = pi.
826     Some … (assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3)
827   = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi.
828 #program #pol #ppc #lookup_labels #lookup_datalabels #pi #prf1 #prf2 #prf3
829 cases (pi2 … (assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3))
830 #H1 #_ @H1
831qed. *)
832
833(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
834(* definition construct_costs':
835 ∀program. ∀pol:policy program. ∀ppc,pc,costs,i.
836  Σres:(nat × (BitVectorTrie costlabel 16)). Some … res = construct_costs_safe program pol ppc pc costs i
837
838  λprogram.λpol: policy program.λppc,pc,costs,i.
839   match construct_costs_safe program pol ppc pc costs i with
840    [ None ⇒ let dummy ≝ 〈0, Stub costlabel 16〉 in dummy
841    | Some res ⇒ res ].
842 [ cases daemon
843 | >p %]
844qed.
845
846definition construct_costs ≝
847 λprogram,pol,ppc,pc,costs,i. pi1 … (construct_costs' program pol ppc pc costs i). *)
848
849(*
850axiom suffix_of: ∀A:Type[0]. ∀l,prefix:list A. list A.
851axiom suffix_of_ok: ∀A,l,prefix. prefix @ suffix_of A l prefix = l.
852
853axiom foldl_strong_step:
854 ∀A:Type[0].
855  ∀P: list A → Type[0].
856   ∀l: list A.
857    ∀H: ∀prefix,hd,tl. l =  prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]).
858     ∀acc: P [ ].
859      ∀Q: ∀prefix. P prefix → Prop.
860       ∀HQ: ∀prefix,hd,tl.∀prf: l = prefix @ [hd] @ tl.
861        ∀acc: P prefix. Q prefix acc → Q (prefix @ [hd]) (H prefix hd tl prf acc).
862       Q [ ] acc →
863        Q l (foldl_strong A P l H acc).
864(*
865 #A #P #l #H #acc #Q #HQ #Hacc normalize;
866 generalize in match
867  (foldl_strong ?
868   (λpre. Q pre (foldl_strong_internal A P l (suffix_of A l pre) ? [ ] pre acc ?))
869   l ? Hacc)
870 [3: >suffix_of_ok % | 2: #prefix #hd #tl #EQ @(H prefix hd (tl@suffix_of A l pre) EQ) ]
871 [2: #prefix #hd #tl #prf #X whd in ⊢ (??%)
872 #K
873
874 generalize in match
875  (foldl_strong ?
876   (λpre. Q pre (foldl_strong_internal A P l H pre (suffix_of A l pre) acc (suffix_of_ok A l pre))))
877 [2: @H
878*)
879
880axiom foldl_elim:
881 ∀A:Type[0].
882  ∀B: Type[0].
883   ∀H: A → B → A.
884    ∀acc: A.
885     ∀l: list B.
886      ∀Q: A → Prop.
887       (∀acc:A.∀b:B. Q acc → Q (H acc b)) →
888         Q acc →
889          Q (foldl A B H acc l).
890*)
891
892(*
893lemma tech_pc_sigma00_append_Some:
894 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
895  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
896   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
897 #program #pol #prefix #costs #label #i #ppc #pc #H
898  whd in match tech_pc_sigma00 in ⊢ %; normalize nodelta;
899  whd in match sigma00 in ⊢ %; normalize nodelta in ⊢ %;
900  generalize in match (pi2 … pol) whd in ⊢ (% → ?) whd in ⊢ (?(??%?) → ?)
901  whd in match sigma0; normalize nodelta;
902  >foldl_step
903  change with (? → match match sigma00 program pol prefix with [None ⇒ ? | Some res ⇒ ?] with [ None ⇒ ? | Some res ⇒ ? ] = ?)
904  whd in match tech_pc_sigma00 in H; normalize nodelta in H;
905  cases (sigma00 program pol prefix) in H ⊢ %
906   [ whd in ⊢ (??%% → ?) #abs destruct(abs)
907   | * #ppc' * #pc' #sigma_map normalize nodelta; #H generalize in match (option_destruct_Some ??? H)
908     
909     normalize nodelta; -H;
910     
911 
912   generalize in match H; -H;
913  generalize in match (foldl ?????); in H ⊢ (??match match % with [_ ⇒ ? | _ ⇒ ?] with [_ ⇒ ? | _ ⇒ ?]?)
914   [2: whd in ⊢ (??%%)
915XXX
916*)
917
918(* axiom construct_costs_sigma:
919 ∀p.∀pol:policy p.∀ppc,pc,costs,i.
920  bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) →
921   bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)).
922
923axiom tech_pc_sigma00_append_Some:
924 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
925  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
926   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉. *)
927
928axiom eq_identifier_eq:
929  ∀tag: String.
930  ∀l.
931  ∀r.
932    eq_identifier tag l r = true → l = r.
933
934axiom neq_identifier_neq:
935  ∀tag: String.
936  ∀l, r: identifier tag.
937    eq_identifier tag l r = false → (l = r → False).
938
939(* label_map: identifier ↦ pseudo program counter *)
940definition label_map ≝ identifier_map ASMTag ℕ.
941
942(* Labels *)
943definition is_label ≝
944  λx:labelled_instruction.λl:Identifier.
945  let 〈lbl,instr〉 ≝ x in
946  match lbl with
947  [ Some l' ⇒ l' = l
948  | _       ⇒ False
949  ].
950
951lemma label_does_not_occur:
952  ∀i:ℕ.∀p:list labelled_instruction.∀l:Identifier.
953  is_label (nth i ? p 〈None ?, Comment [ ]〉) l → does_not_occur ?? l p = false.
954 #i #p #l generalize in match i; elim p
955 [ #i >nth_nil #H cases H
956 | #h #t #IH #i cases i -i
957   [ cases h #hi #hp cases hi
958     [ normalize #H cases H
959     | #l' #Heq whd in ⊢ (??%?); change with (eq_identifier ? l' l) in match (instruction_matches_identifier ????);
960       whd in Heq; >Heq
961       >eq_identifier_refl / by refl/
962     ]
963   | #i #H whd in match (does_not_occur ????);
964     whd in match (instruction_matches_identifier ????);
965     cases h #hi #hp cases hi normalize nodelta
966     [ @(IH i) @H
967     | #l' @eq_identifier_elim
968       [ normalize / by /
969       | normalize #_ @(IH i) @H
970       ]
971     ]
972   ]
973 ]
974qed.
975
976(* The function that creates the label-to-address map *)
977definition create_label_cost_map0: ∀program:list labelled_instruction.
978  (Σlabels_costs:label_map × (BitVectorTrie costlabel 16). (* Both on ppcs *)
979    let 〈labels,costs〉 ≝ labels_costs in
980    ∀l.occurs_exactly_once ?? l program →
981    bitvector_of_nat ? (lookup_def ?? labels l 0) =
982     address_of_word_labels_code_mem program l
983  ) ≝
984  λprogram.
985  \fst (pi1 ?? (foldl_strong (option Identifier × pseudo_instruction)
986  (λprefix.Σlabels_costs_ppc:label_map × (BitVectorTrie costlabel 16) × ℕ.
987    let 〈labels,costs,ppc〉 ≝ labels_costs_ppc in
988    ppc = |prefix| ∧
989    ∀l.occurs_exactly_once ?? l prefix →
990    bitvector_of_nat ? (lookup_def ?? labels l 0) =
991     address_of_word_labels_code_mem prefix l)
992  program
993  (λprefix.λx.λtl.λprf.λlabels_costs_ppc.
994   let 〈labels,costs,ppc〉 ≝ pi1 ?? labels_costs_ppc in
995   let 〈label,instr〉 ≝ x in
996   let labels ≝
997     match label with
998     [ None   ⇒ labels
999     | Some l ⇒ add … labels l ppc
1000     ] in
1001   let costs ≝
1002     match instr with
1003     [ Cost cost ⇒ insert … (bitvector_of_nat ? ppc) cost costs
1004     | _ ⇒ costs ] in
1005      〈labels,costs,S ppc〉
1006   ) 〈(empty_map …),(Stub ??),0〉)).
1007[ normalize nodelta lapply (pi2 … labels_costs_ppc) >p >p1 normalize nodelta * #IH1 #IH2
1008  -labels_costs_ppc % [>IH1 >length_append <plus_n_Sm <plus_n_O %]
1009 inversion label [#EQ | #l #EQ]
1010 [ #lbl #Hocc <address_of_word_labels_code_mem_None [2: @Hocc] normalize nodelta
1011   >occurs_exactly_once_None in Hocc; @(IH2 lbl)
1012 | #lbl normalize nodelta inversion (eq_identifier ? lbl l)
1013   [ #Heq #Hocc >(eq_identifier_eq … Heq)
1014     >address_of_word_labels_code_mem_Some_hit
1015     [ >IH1 >lookup_def_add_hit %
1016     | <(eq_identifier_eq … Heq) in Hocc; //
1017     ]
1018   | #Hneq #Hocc
1019     <address_of_word_labels_code_mem_Some_miss
1020     [ >lookup_def_add_miss
1021       [ @IH2 >occurs_exactly_once_Some_eq in Hocc; >eq_identifier_sym> Hneq //
1022       | % @neq_identifier_neq @Hneq
1023       ]
1024     | @Hocc
1025     | >eq_identifier_sym @Hneq
1026     ]
1027   ]
1028 ]
1029| @pair_elim * #labels #costs #ppc #EQ destruct normalize nodelta % try %
1030  #l #abs cases (abs)
1031| cases (foldl_strong ? (λ_.Σx.?) ???) * * #labels #costs #ppc normalize nodelta *
1032  #_ #H @H
1033]
1034qed.
1035
1036(* The function that creates the label-to-address map *)
1037definition create_label_cost_map: ∀program:list labelled_instruction.
1038  label_map × (BitVectorTrie costlabel 16) ≝
1039    λprogram.
1040      pi1 … (create_label_cost_map0 program).
1041
1042theorem create_label_cost_map_ok:
1043 ∀pseudo_program: pseudo_assembly_program.
1044   let 〈labels, costs〉 ≝ create_label_cost_map (\snd pseudo_program) in
1045    ∀id. occurs_exactly_once ??  id (\snd pseudo_program) →
1046     bitvector_of_nat ? (lookup_def ?? labels id 0) = address_of_word_labels_code_mem (\snd pseudo_program) id.
1047 #p change with (pi1 … (create_label_cost_map0 ?)) in match (create_label_cost_map ?); @pi2
1048qed.
1049
1050
1051(*CSC: move elsewhere; practically equal to shift_nth_safe but for commutation
1052  of addition. One of the two lemmas should disappear. *)
1053lemma nth_safe_prepend:
1054 ∀A,l1,l2,j.∀H:j<|l2|.∀K:|l1|+j<|(l1@l2)|.
1055  nth_safe A j l2 H =nth_safe A (|l1|+j) (l1@l2) K.
1056 #A #l1 #l2 #j #H >commutative_plus @shift_nth_safe
1057qed.
1058
1059lemma nth_safe_cons:
1060 ∀A,hd,tl,l2,j,j_ok,Sj_ok.
1061  nth_safe A j (tl@l2) j_ok =nth_safe A (1+j) (hd::tl@l2) Sj_ok.
1062//
1063qed.
1064
1065lemma eq_nth_safe_proof_irrelevant:
1066 ∀A,l,i,i_ok,i_ok'.
1067  nth_safe A l i i_ok = nth_safe A l i i_ok'.
1068#A #l #i #i_ok #i_ok' %
1069qed.
1070
1071(*CSC: move elsewhere *)
1072lemma fetch_pseudo_instruction_append:
1073 ∀l1,l2,ppc,ppc_ok,ppc_ok'.
1074  let code_newppc ≝ fetch_pseudo_instruction l2 ppc ppc_ok in
1075  fetch_pseudo_instruction (l1@l2) (add … (bitvector_of_nat … (|l1|)) (ppc)) ppc_ok' =
1076  〈\fst code_newppc, add … (bitvector_of_nat … (|l1|)) (\snd code_newppc)〉.
1077 #l1 #l2 #ppc #ppc_ok whd in match fetch_pseudo_instruction; normalize nodelta
1078 (*CSC: FALSE, NEED INVARIANT? *)
1079 > (?: nat_of_bitvector … (add 16 (bitvector_of_nat 16 (|l1|)) ppc)
1080     = |l1| + nat_of_bitvector … ppc) [2: cases daemon]
1081 #ppc_ok' <nth_safe_prepend try assumption cases (nth_safe labelled_instruction ???)
1082 #lbl #instr normalize nodelta >add_associative %
1083qed.
1084
1085definition assembly:
1086    ∀p: pseudo_assembly_program.
1087    ∀sigma: Word → Word.
1088    ∀policy: Word → bool.
1089      Σres:list Byte × (BitVectorTrie costlabel 16).
1090       let 〈preamble,instr_list〉 ≝ p in
1091       let 〈assembled,costs〉 ≝ res in
1092       let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in
1093       let datalabels ≝ construct_datalabels preamble in
1094       let lookup_labels ≝ λx. sigma (bitvector_of_nat ? (lookup_def … labels_to_ppc x 0)) in
1095       let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
1096       ∀ppc. ∀ppc_ok:nat_of_bitvector … ppc < |instr_list|.
1097         let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc ppc_ok in
1098         let 〈len,assembledi〉 ≝
1099          assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in
1100         ∀j:nat. ∀H: j < |assembledi|. ∀K.
1101          nth_safe ? j assembledi H =
1102           nth_safe ? (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat ? j)))
1103            assembled K
1104
1105  λp.
1106  λsigma.
1107  λpolicy.
1108  deplet 〈preamble, instr_list〉 as p_refl ≝ p in
1109  let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in
1110  let datalabels ≝ construct_datalabels preamble in
1111  let lookup_labels ≝ λx. sigma (bitvector_of_nat ? (lookup_def … labels_to_ppc x 0)) in
1112  let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
1113  let 〈ignore,revcode〉 ≝ pi1 … (
1114     foldl_strong
1115      (option Identifier × pseudo_instruction)
1116      (λpre. Σppc_code:(Word × (list Byte)).
1117        let 〈ppc,code〉 ≝ ppc_code in
1118         ppc = bitvector_of_nat … (|pre|) ∧
1119         ∀ppc'.∀ppc_ok'.
1120          nat_of_bitvector … ppc' < nat_of_bitvector … ppc →
1121           let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' ppc_ok' in
1122           let 〈len,assembledi〉 ≝
1123            assembly_1_pseudoinstruction lookup_labels sigma policy ppc' lookup_datalabels pi in
1124           ∀j:nat. ∀H: j < |assembledi|. ∀K.
1125            nth_safe ? j assembledi H =
1126             nth_safe ? (nat_of_bitvector … (add … (sigma ppc') (bitvector_of_nat ? j))) (reverse … code) K)
1127      instr_list
1128      (λprefix,hd,tl,prf,ppc_code.
1129        let 〈ppc, code〉 ≝ pi1 … ppc_code in
1130        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels (\snd hd) in
1131        let new_ppc ≝ add ? ppc (bitvector_of_nat ? 1) in
1132         〈new_ppc, (reverse … program @ code)〉)
1133      〈(zero ?), [ ]〉)
1134    in
1135     〈reverse … revcode,
1136      fold … (λppc.λcost.λpc_to_costs. insert … (sigma ppc) cost pc_to_costs) ppc_to_costs (Stub ??)〉.
1137  [ cases (foldl_strong ? (λx.Σy.?) ???) in p2; #ignore_revcode #Hfold #EQignore_revcode
1138    >EQignore_revcode in Hfold; * #Hfold1 #Hfold2 whd >p1 whd #ppc #LTppc @Hfold2
1139    >Hfold1 >nat_of_bitvector_bitvector_of_nat_inverse try assumption
1140    (* CSC: FALSE, NEEDS ADDITIONAL ASSUMPTION *) cases daemon
1141  | % // #ppc' #ppc_ok' #abs @⊥ cases (not_le_Sn_O ?) [#H @(H abs) | skip]
1142  | cases ppc_code in p1; -ppc_code #ppc_code #IH #EQppc_code >EQppc_code in IH; -EQppc_code
1143    * #IH1 #IH2 % [ normalize nodelta >IH1 >length_append cases daemon (*CSC: TRUE, LEMMA NEEDED *)]
1144    #ppc' #LTppc' cases hd in prf p2; #label #pi #prf #p2
1145    cases (le_to_or_lt_eq … LTppc')
1146    [2: #S_S_eq normalize nodelta in S_S_eq;
1147        (*CSC: FALSE, NEEDS INVARIANT *)
1148        cut (ppc' = ppc) [cases daemon] -S_S_eq #EQppc' >EQppc' in LTppc'; -ppc'  >prf
1149        >IH1 (* in ⊢ match % with [_ ⇒ ?];*) #LTppc #X lapply LTppc
1150        >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → match % with [_ ⇒ ?]);
1151        >fetch_pseudo_instruction_append
1152        [3: @le_S_S @le_O_n |2: lapply LTppc; >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → ?); #H @H]
1153        #LTppc' @pair_elim #pi' #newppc' #EQpair destruct(EQpair) <IH1 >p2
1154        #j #LTj
1155        (* CSC: FALSE, NEEDS INVARIANT *)
1156        >(?: nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat … j)) =
1157             nat_of_bitvector … (sigma ppc) + j) [2: cases daemon]
1158        >reverse_append >reverse_reverse
1159        (* CSC: TRUE, NEEDS INVARIANT *)
1160        >(? : nat_of_bitvector … (sigma ppc) = |reverse … code|) [2: cases daemon]
1161        @nth_safe_prepend
1162    | #LTppc' #LT_ppc_ppc lapply (IH2 ppc' ?) [ (*CSC: EASY, FINISH*) cases daemon ]
1163      @pair_elim #pi' #newppc' #eq_fetch_pseudoinstruction
1164      @pair_elim #len' #assembledi' #eq_assembly_1_pseudoinstruction #IH
1165      change with (let 〈len,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi' in ∀j:ℕ. ∀H:j<|assembledi|.?)
1166      >eq_assembly_1_pseudoinstruction #j #LTj >reverse_append >reverse_reverse #K
1167      >IH
1168      [2: (*CSC: FALSE, NEEDS INVARIANT? *) cases daemon
1169      | @shift_nth_prefix
1170      |3: >IH1 >nat_of_bitvector_bitvector_of_nat_inverse try assumption
1171          (*CSC: ALSO FALSE, NEEDS INVARIANT? *) cases daemon
1172      ]
1173    ]
1174  ] 
1175qed.
1176
1177definition assembly_unlabelled_program:
1178    assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝
1179  λp.
1180    Some … (〈foldr … (λi,l. assembly1 i @ l) [ ] p, Stub …〉).
Note: See TracBrowser for help on using the repository browser.