source: src/ASM/Assembly.ma @ 2048

Last change on this file since 2048 was 2048, checked in by boender, 8 years ago
  • factorised jump decisions
File size: 61.7 KB
Line 
1include "ASM/ASM.ma".
2include "ASM/Arithmetic.ma".
3include "ASM/Fetch.ma".
4include "ASM/Status.ma".
5include alias "basics/logic.ma".
6include alias "arithmetics/nat.ma".
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 ? 8 8 result in
23    if get_index' ? 2 0 flags then
24      〈eq_bv 8 upper [[true;true;true;true;true;true;true;true]], lower〉
25    else
26      〈eq_bv 8 upper (zero 8), zero 8〉.
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(*
675(* This establishes the correspondence between pseudo program counters and
676   program counters. It is at the heart of the proof. *)
677(*CSC: code taken from build_maps *)
678definition sigma00:
679 ∀jump_expansion:policy_type2.∀l:list labelled_instruction.? →
680 (Σppc_pc_map:ℕ×(ℕ×(BitVectorTrie Word 16)).
681  let 〈ppc,pc_map〉 ≝ ppc_pc_map in
682  let 〈program_counter, sigma_map〉 ≝ pc_map in
683  ppc = |l| ∧
684  (ppc = |l| →
685   (bvt_lookup ?? (bitvector_of_nat ? ppc) sigma_map (zero ?) = (bitvector_of_nat ? program_counter)) ∧
686   (∀x.x < |l| →
687    ∀pi.\fst (fetch_pseudo_instruction l (bitvector_of_nat ? x)) = pi →
688   let pc_x ≝ bvt_lookup ?? (bitvector_of_nat 16 x) sigma_map (zero ?) in
689   bvt_lookup ?? (bitvector_of_nat 16 (S x)) sigma_map (zero ?) =
690   bitvector_of_nat 16 ((nat_of_bitvector ? pc_x) +
691   (\fst (assembly_1_pseudoinstruction lookup_labels(*X?(λx.pc_x)*) (jump_expansion (*?(λx.pc_x)*)) pc_x
692     (λx.zero ?) pi)))))
693 ) ≝
694 (*?*)λlookup_labels.
695 λjump_expansion(*X?: policy_type2*).
696 λl:list labelled_instruction.
697 λacc.
698  foldl_strong ?
699   (λprefix.(Σppc_pc_map:ℕ×(ℕ×(BitVectorTrie Word 16)).
700     let 〈ppc,pc_map〉 ≝ ppc_pc_map in
701     let 〈program_counter, sigma_map〉 ≝ pc_map in
702     (ppc = |prefix|) ∧
703     (ppc = |prefix| →
704      (bvt_lookup ?? (bitvector_of_nat ? ppc) sigma_map (zero ?) = (bitvector_of_nat ? program_counter)) ∧
705      (∀x.x < |prefix| →
706       ∀pi.\fst (fetch_pseudo_instruction l (bitvector_of_nat ? x)) = pi →
707       let pc_x ≝  bvt_lookup ?? (bitvector_of_nat 16 x) sigma_map (zero ?) in
708       bvt_lookup ?? (bitvector_of_nat 16 (S x)) sigma_map (zero ?) =
709       bitvector_of_nat 16 ((nat_of_bitvector ? pc_x) +
710       (\fst (assembly_1_pseudoinstruction (*X?(λx.pc_x)*)lookup_labels (jump_expansion (*X?(λx.pc_x)*)) pc_x
711        (λx.zero ?) pi))))))
712    )
713   l
714   (λhd.λi.λtl.λp.λppc_pc_map.
715     let 〈ppc,pc_map〉 ≝ ppc_pc_map in
716     let 〈program_counter, sigma_map〉 ≝ pc_map in
717     let 〈label, i〉 ≝ i in
718      let 〈pc,ignore〉 ≝ construct_costs lookup_labels program_counter (jump_expansion (*X?(λx.bitvector_of_nat ? program_counter)*)) ppc (Stub …) i in
719         〈S ppc, 〈pc, insert ?? (bitvector_of_nat 16 (S ppc)) (bitvector_of_nat 16 pc) sigma_map〉〉
720   ) acc.
721cases i in p; #label #ins #p @pair_elim #new_ppc #x normalize nodelta cases x -x #old_pc #old_map
722@pair_elim #new_pc #ignore #Hc #Heq normalize nodelta @conj
723[ lapply (pi2 ?? ppc_pc_map) >p1 >p2 normalize nodelta #Hind
724  <(pair_eq1 ?????? Heq) >(proj1 ?? Hind) >append_length <commutative_plus normalize @refl
725| #Hnew <(pair_eq2 ?????? (pair_eq2 ?????? Heq)) <(pair_eq1 ?????? Heq) @conj
726  [ >lookup_insert_hit >(pair_eq1 ?????? (pair_eq2 ?????? Heq)) @refl
727  | #x <(pair_eq1 ?????? Heq) >append_length <commutative_plus #Hx normalize in Hx;
728    #pi #Hpi <(pair_eq2 ?????? (pair_eq2 ?????? Heq)) <(pair_eq1 ?????? Heq) in Hnew;
729    >append_length <commutative_plus #Hnew normalize in Hnew; >(injective_S … Hnew)
730    elim (le_to_or_lt_eq … Hx) -Hx #Hx
731    [ lapply (pi2 ?? ppc_pc_map) >p1 >p2 normalize nodelta #Hind
732      lapply (proj2 ?? ((proj2 ?? Hind) (proj1 ?? Hind)) x (le_S_S_to_le … Hx) pi Hpi)
733      -Hind #Hind >lookup_insert_miss
734      [2: @bitvector_of_nat_abs
735        [3: @lt_to_not_eq @Hx
736        |1: @(transitive_le … Hx)
737        ]
738        cases daemon (* XXX invariant *)
739      ]
740      >lookup_insert_miss
741      [2: @bitvector_of_nat_abs
742        [3: @lt_to_not_eq @(transitive_le … (le_S_S_to_le … Hx)) @le_S @le_n
743        |1: @(transitive_le … (le_S_S_to_le … Hx))
744        ]
745        cases daemon (* XXX invariant *)
746      ]
747      @Hind
748    | lapply (pi2 ?? ppc_pc_map) >p1 >p2 normalize nodelta
749      #Hind lapply (proj1 ?? ((proj2 ?? Hind) (proj1 ?? Hind))) -Hind
750      >(injective_S … Hnew) #Hind <(injective_S … Hx) >lookup_insert_hit >lookup_insert_miss
751      [2: @bitvector_of_nat_abs
752        [3: @lt_to_not_eq @le_n
753        |1: @(transitive_le ??? (le_n (S x)))
754        ]
755        cases daemon (* XXX invariant *)
756      ]
757      >p in Hpi; whd in match (fetch_pseudo_instruction ??); >nth_append_second
758      >nat_of_bitvector_bitvector_of_nat >(injective_S … Hx)
759      [3: @le_n]
760      [2,3: cases daemon (* XXX invariant *)]
761      <minus_n_n cases (half_add ???) #x #y normalize nodelta -x -y #Heq <Heq
762      whd in match (construct_costs ?????) in Hc; whd in match (assembly_1_pseudoinstruction ?????);
763      cases ins in p Hc; normalize nodelta
764      [1,2,4,5: #x #p >Hind #H <(pair_eq1 ?????? H) >commutative_plus >nat_of_bitvector_bitvector_of_nat
765        [1,3,5,7: @refl
766        |2,4,6,8: cases daemon (* XXX invariant *)
767        ]
768      |3: #c #p >Hind #H <(pair_eq1 ?????? H) >nat_of_bitvector_bitvector_of_nat
769        [2: cases daemon (* XXX invariant *) ]
770        whd in match (expand_pseudo_instruction ?????); normalize <plus_n_O @refl
771      |6: #x #y #p >Hind #H <(pair_eq1 ?????? H) >commutative_plus >nat_of_bitvector_bitvector_of_nat
772        [ @refl
773        | cases daemon (* XXX invariant *)
774        ]
775      ]
776    ]
777  ]
778]
779qed.
780
781definition sigma0: pseudo_assembly_program → policy_type2 → (nat × (nat × (BitVectorTrie Word 16))) ≝
782  λprog.
783  λjump_expansion.
784    sigma00 jump_expansion (\snd prog)
785    〈0, 〈0, (insert … (bitvector_of_nat ? 0) (bitvector_of_nat ? 0) (Stub …))〉〉.
786 normalize nodelta @conj
787 [ / by refl/
788 | #H @conj
789   [ >lookup_insert_hit @refl
790   | #x #Hx @⊥ @(absurd … Hx) @le_to_not_lt @le_O_n
791   ]
792 ]
793qed.
794
795definition tech_pc_sigma00: pseudo_assembly_program → policy_type2 →
796 list labelled_instruction → (nat × nat) ≝
797 λprogram,jump_expansion,instr_list.
798   let 〈ppc,pc_sigma_map〉 ≝ sigma00 jump_expansion instr_list
799   〈0, 〈0, (insert … (bitvector_of_nat ? 0) (bitvector_of_nat ? 0) (Stub ? ?))〉〉 in
800   (* acc copied from sigma0 *)
801   let 〈pc,map〉 ≝ pc_sigma_map in
802     〈ppc,pc〉.
803 normalize nodelta @conj
804 [ / by refl/
805 | #H @conj
806   [ >lookup_insert_hit @refl
807   | #x #Hx @⊥ @(absurd … Hx) @le_to_not_lt @le_O_n
808   ]
809 ]
810qed.
811
812definition sigma_safe: pseudo_assembly_program → policy_type2 →
813 option (Word → Word) ≝
814 λinstr_list,jump_expansion.
815  let 〈ppc,pc_sigma_map〉 ≝ sigma0 instr_list jump_expansion in
816  let 〈pc, sigma_map〉 ≝ pc_sigma_map in
817    if gtb pc (2^16) then
818      None ?
819    else
820      Some ? (λx. lookup … x sigma_map (zero …)). *)
821
822(* stuff about policy *)
823
824(*definition policy_ok ≝ λjump_expansion,p. sigma_safe p jump_expansion ≠ None ….*)
825
826(*definition policy ≝ λp. Σjump_expansion:policy_type2. policy_ok jump_expansion p.*)
827
828(*lemma eject_policy: ∀p. policy p → policy_type2.
829 #p #pol @(pi1 … pol)
830qed.
831
832coercion eject_policy nocomposites: ∀p.∀pol:policy p. policy_type2 ≝ eject_policy on _pol:(policy ?) to policy_type2.
833
834definition sigma: ∀p:pseudo_assembly_program. policy p → Word → Word ≝
835 λp,policy.
836  match sigma_safe p (pi1 … policy) return λr:option (Word → Word). r ≠ None … → Word → Word with
837   [ None ⇒ λabs. ⊥
838   | Some r ⇒ λ_.r] (pi2 … policy).
839 cases abs /2 by /
840qed.*)
841
842(*CSC: Main axiom here, needs to be proved soon! *)
843(*lemma snd_assembly_1_pseudoinstruction_ok:
844 ∀program:pseudo_assembly_program.∀pol: policy program.
845 ∀ppc:Word.∀pi,lookup_labels,lookup_datalabels.
846  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
847  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
848  (nat_of_bitvector 16 ppc) < |\snd program| →
849  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
850   let len ≝ \fst (assembly_1_pseudoinstruction lookup_labels (pol lookup_labels) (sigma program pol ppc) lookup_datalabels  pi) in
851    sigma program pol (add ? ppc (bitvector_of_nat ? 1)) =
852     bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len).
853 #program #pol #ppc #pi #lookup_labels #lookup_datalabels #Hll #Hldl #Hppc
854 lapply (refl … (sigma0 program pol)) whd in match (sigma0 ??) in ⊢ (??%? → ?);
855 cases (sigma00 ???) #x #Hpmap #EQ
856 whd in match (sigma ???);
857 whd in match (sigma program pol (add ???));
858 whd in match sigma_safe; normalize nodelta
859 (*Problem1: backtracking cases (sigma0 program pol)*)
860 generalize in match (pi2 ???); whd in match policy_ok; normalize nodelta
861 whd in match sigma_safe; normalize nodelta <EQ cases x in Hpmap EQ; -x #final_ppc #x
862 cases x -x #final_pc #smap normalize nodelta #Hpmap #EQ #Heq #Hfetch cases (gtb final_pc (2^16)) in Heq;
863 normalize nodelta
864 [ #abs @⊥ @(absurd ?? abs) @refl
865 | #_ lapply (proj1 ?? ((proj2 ?? Hpmap) (proj1 ?? Hpmap))) #Hpmap1
866   lapply ((proj2 ?? ((proj2 ?? Hpmap) (proj1 ?? Hpmap))) (nat_of_bitvector 16 ppc) Hppc) #Hpmap2 -Hpmap
867   <(bitvector_of_nat_nat_of_bitvector 16 ppc) >add_SO
868   
869   >(Hpmap2 ? (refl …)) @eq_f @eq_f2 [%]
870   >bitvector_of_nat_nat_of_bitvector
871   >Hfetch lapply Hfetch lapply pi
872
873   
874   whd in match assembly_1_pseudoinstruction; normalize nodelta
875 
876qed.*)
877
878
879(*example sigma_0: ∀p,pol. sigma p pol (bitvector_of_nat ? 0) = bitvector_of_nat ? 0.
880 cases daemon.
881qed.*)
882
883(*CSC: FALSE!!!*)
884axiom fetch_pseudo_instruction_vsplit:
885 ∀instr_list,ppc.
886  ∃pre,suff,lbl.
887   (pre @ [〈lbl,\fst (fetch_pseudo_instruction instr_list ppc)〉]) @ suff = instr_list.
888
889(*lemma sigma00_append:
890 ∀jump_expansion,l1,l2.
891 ∀acc:ℕ×ℕ×(BitVectorTrie Word 16).
892  sigma00 jump_expansion (l1@l2) acc =
893  sigma00 jump_expansion
894    l2 (pi1 ?? (sigma00 jump_expansion l1 acc)).*)
895
896(* lemma sigma00_strict:
897 ∀jump_expansion,l,acc. acc = None ? →
898  sigma00 jump_expansion l acc = None ….
899 #jump_expansion #l elim l
900  [ #acc #H >H %
901  | #hd #tl #IH #acc #H >H change with (sigma00 ? tl ? = ?) @IH % ]
902qed.
903
904lemma policy_ok_prefix_ok:
905 ∀program.∀pol:policy program.∀suffix,prefix.
906  prefix@suffix = \snd program →
907   sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) ≠ None ….
908 * #preamble #instr_list #pol #suffix #prefix #prf whd in prf:(???%);
909 generalize in match (pi2 ?? pol); whd in prf:(???%); <prf in pol; #pol
910 whd in match policy_ok; whd in match sigma_safe; whd in match sigma0;
911 normalize nodelta >sigma00_append
912 cases (sigma00 ?? prefix ?)
913  [2: #x #_ % #abs destruct(abs)
914  | * #abs @⊥ @abs >sigma00_strict % ]
915qed.
916
917lemma policy_ok_prefix_hd_ok:
918 ∀program.∀pol:policy program.∀suffix,hd,prefix,ppc_pc_map.
919  (prefix@[hd])@suffix = \snd program →
920   Some ? ppc_pc_map = sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) →
921    let 〈ppc,pc_map〉 ≝ ppc_pc_map in
922    let 〈program_counter, sigma_map〉 ≝ pc_map in
923    let 〈label, i〉 ≝ hd in
924     construct_costs_safe program pol ppc program_counter (Stub …) i ≠ None ….
925 * #preamble #instr_list #pol #suffix #hd #prefix #ppc_pc_map #EQ1 #EQ2
926 generalize in match (policy_ok_prefix_ok 〈preamble,instr_list〉 pol suffix
927  (prefix@[hd]) EQ1) in ⊢ ?; >sigma00_append <EQ2 whd in ⊢ (?(??%?) → ?);
928 @pair_elim #ppc #pc_map #EQ3 normalize nodelta
929 @pair_elim #pc #map #EQ4 normalize nodelta
930 @pair_elim #l' #i' #EQ5 normalize nodelta
931 cases (construct_costs_safe ??????) normalize
932  [* #abs @⊥ @abs % | #X #_ % #abs destruct(abs)]
933qed. *)
934
935(* JPB,CSC: this definition is now replaced by the expand_pseudo_instruction higher up
936definition expand_pseudo_instruction:
937 ∀program:pseudo_assembly_program.∀pol: policy program.
938  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pc.
939  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
940  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
941  let pi ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in
942  pc = sigma program pol ppc →
943  Σres:list instruction. Some … res = expand_pseudo_instruction_safe pc (lookup_labels pi) lookup_datalabels (pol ppc) pi
944≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pc,prf1,prf2,prf3.
945   match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) (\fst (fetch_pseudo_instruction (\snd program) ppc)) with
946    [ None ⇒ let dummy ≝ [ ] in dummy
947    | Some res ⇒ res ].
948 [ @⊥ whd in p:(??%??);
949   generalize in match (pi2 ?? pol); whd in ⊢ (% → ?);
950   whd in ⊢ (?(??%?) → ?); change with (sigma00 ????) in ⊢ (?(??(match % with [_ ⇒ ? | _ ⇒ ?])?) → ?);
951   generalize in match (refl … (sigma00 program pol (\snd program) (Some ? 〈O,〈O,Stub (BitVector 16) 16〉〉)));
952   cases (sigma00 ????) in ⊢ (??%? → %); normalize nodelta [#_ * #abs @abs %]
953   #res #K
954   cases (fetch_pseudo_instruction_vsplit (\snd program) ppc) #pre * #suff * #lbl #EQ1
955   generalize in match (policy_ok_prefix_hd_ok program pol … EQ1 ?) in ⊢ ?;
956   cases daemon (* CSC: XXXXXXXX Ero qui
957   
958    [3: @policy_ok_prefix_ok ]
959    | sigma00 program pol pre
960
961
962
963   QUA USARE LEMMA policy_ok_prefix_hd_ok combinato a lemma da fare che
964   fetch ppc = hd sse program = pre @ [hd] @ tl e |pre| = ppc
965   per concludere construct_costs_safe ≠ None *)
966 | >p %]
967qed. *)
968
969(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
970(* definition assembly_1_pseudoinstruction':
971 ∀program:pseudo_assembly_program.∀pol: policy program.
972  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
973  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
974  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
975  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
976  Σres:(nat × (list Byte)).
977   res = assembly_1_pseudoinstruction program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi ∧
978   let 〈len,code〉 ≝ res in
979    sigma program pol (add ? ppc (bitvector_of_nat ? 1)) =
980     bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len)
981≝ λprogram: pseudo_assembly_program.
982  λpol: policy program.
983  λppc: Word.
984  λlookup_labels.
985  λlookup_datalabels.
986  λpi.
987  λprf1,prf2,prf3.
988   assembly_1_pseudoinstruction program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi.
989 [ @⊥ elim pi in p; [*]
990   try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs
991   generalize in match (jmeq_to_eq ??? abs); -abs; #abs whd in abs:(??%?); try destruct(abs)
992   whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?);
993   (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *)
994   cases daemon
995 | % [ >p %]
996   cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta;
997   (* THIS SHOULD BE TRUE INSTEAD *)
998   cases daemon]
999qed.
1000
1001definition assembly_1_pseudoinstruction:
1002 ∀program:pseudo_assembly_program.∀pol: policy program.
1003  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1004  lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) →
1005  lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)) →
1006  \fst (fetch_pseudo_instruction (\snd program) ppc) = pi →
1007   nat × (list Byte)
1008≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pi,prf1,prf2,prf3.
1009   assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1
1010    prf2 prf3.
1011
1012lemma assembly_1_pseudoinstruction_ok1:
1013 ∀program:pseudo_assembly_program.∀pol: policy program.
1014  ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi.
1015  ∀prf1:lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)).
1016  ∀prf2:lookup_datalabels = (λx. lookup_def … (construct_datalabels (\fst program)) x (zero ?)).
1017  ∀prf3:\fst (fetch_pseudo_instruction (\snd program) ppc) = pi.
1018     Some … (assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3)
1019   = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi.
1020 #program #pol #ppc #lookup_labels #lookup_datalabels #pi #prf1 #prf2 #prf3
1021 cases (pi2 … (assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3))
1022 #H1 #_ @H1
1023qed. *)
1024
1025(* MAIN AXIOM HERE, HIDDEN USING cases daemon *)
1026(* definition construct_costs':
1027 ∀program. ∀pol:policy program. ∀ppc,pc,costs,i.
1028  Σres:(nat × (BitVectorTrie costlabel 16)). Some … res = construct_costs_safe program pol ppc pc costs i
1029
1030  λprogram.λpol: policy program.λppc,pc,costs,i.
1031   match construct_costs_safe program pol ppc pc costs i with
1032    [ None ⇒ let dummy ≝ 〈0, Stub costlabel 16〉 in dummy
1033    | Some res ⇒ res ].
1034 [ cases daemon
1035 | >p %]
1036qed.
1037
1038definition construct_costs ≝
1039 λprogram,pol,ppc,pc,costs,i. pi1 … (construct_costs' program pol ppc pc costs i). *)
1040
1041(*
1042axiom suffix_of: ∀A:Type[0]. ∀l,prefix:list A. list A.
1043axiom suffix_of_ok: ∀A,l,prefix. prefix @ suffix_of A l prefix = l.
1044
1045axiom foldl_strong_step:
1046 ∀A:Type[0].
1047  ∀P: list A → Type[0].
1048   ∀l: list A.
1049    ∀H: ∀prefix,hd,tl. l =  prefix @ [hd] @ tl → P prefix → P (prefix @ [hd]).
1050     ∀acc: P [ ].
1051      ∀Q: ∀prefix. P prefix → Prop.
1052       ∀HQ: ∀prefix,hd,tl.∀prf: l = prefix @ [hd] @ tl.
1053        ∀acc: P prefix. Q prefix acc → Q (prefix @ [hd]) (H prefix hd tl prf acc).
1054       Q [ ] acc →
1055        Q l (foldl_strong A P l H acc).
1056(*
1057 #A #P #l #H #acc #Q #HQ #Hacc normalize;
1058 generalize in match
1059  (foldl_strong ?
1060   (λpre. Q pre (foldl_strong_internal A P l (suffix_of A l pre) ? [ ] pre acc ?))
1061   l ? Hacc)
1062 [3: >suffix_of_ok % | 2: #prefix #hd #tl #EQ @(H prefix hd (tl@suffix_of A l pre) EQ) ]
1063 [2: #prefix #hd #tl #prf #X whd in ⊢ (??%)
1064 #K
1065
1066 generalize in match
1067  (foldl_strong ?
1068   (λpre. Q pre (foldl_strong_internal A P l H pre (suffix_of A l pre) acc (suffix_of_ok A l pre))))
1069 [2: @H
1070*)
1071
1072axiom foldl_elim:
1073 ∀A:Type[0].
1074  ∀B: Type[0].
1075   ∀H: A → B → A.
1076    ∀acc: A.
1077     ∀l: list B.
1078      ∀Q: A → Prop.
1079       (∀acc:A.∀b:B. Q acc → Q (H acc b)) →
1080         Q acc →
1081          Q (foldl A B H acc l).
1082*)
1083
1084(*
1085lemma tech_pc_sigma00_append_Some:
1086 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
1087  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
1088   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉.
1089 #program #pol #prefix #costs #label #i #ppc #pc #H
1090  whd in match tech_pc_sigma00 in ⊢ %; normalize nodelta;
1091  whd in match sigma00 in ⊢ %; normalize nodelta in ⊢ %;
1092  generalize in match (pi2 … pol) whd in ⊢ (% → ?) whd in ⊢ (?(??%?) → ?)
1093  whd in match sigma0; normalize nodelta;
1094  >foldl_step
1095  change with (? → match match sigma00 program pol prefix with [None ⇒ ? | Some res ⇒ ?] with [ None ⇒ ? | Some res ⇒ ? ] = ?)
1096  whd in match tech_pc_sigma00 in H; normalize nodelta in H;
1097  cases (sigma00 program pol prefix) in H ⊢ %
1098   [ whd in ⊢ (??%% → ?) #abs destruct(abs)
1099   | * #ppc' * #pc' #sigma_map normalize nodelta; #H generalize in match (option_destruct_Some ??? H)
1100     
1101     normalize nodelta; -H;
1102     
1103 
1104   generalize in match H; -H;
1105  generalize in match (foldl ?????); in H ⊢ (??match match % with [_ ⇒ ? | _ ⇒ ?] with [_ ⇒ ? | _ ⇒ ?]?)
1106   [2: whd in ⊢ (??%%)
1107XXX
1108*)
1109
1110(* axiom construct_costs_sigma:
1111 ∀p.∀pol:policy p.∀ppc,pc,costs,i.
1112  bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) →
1113   bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)).
1114
1115axiom tech_pc_sigma00_append_Some:
1116 ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc.
1117  tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 →
1118   tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉. *)
1119
1120axiom eq_identifier_eq:
1121  ∀tag: String.
1122  ∀l.
1123  ∀r.
1124    eq_identifier tag l r = true → l = r.
1125
1126axiom neq_identifier_neq:
1127  ∀tag: String.
1128  ∀l, r: identifier tag.
1129    eq_identifier tag l r = false → (l = r → False).
1130
1131(* label_map: identifier ↦ pseudo program counter *)
1132definition label_map ≝ identifier_map ASMTag ℕ.
1133
1134(* Labels *)
1135definition is_label ≝
1136  λx:labelled_instruction.λl:Identifier.
1137  let 〈lbl,instr〉 ≝ x in
1138  match lbl with
1139  [ Some l' ⇒ l' = l
1140  | _       ⇒ False
1141  ].
1142
1143lemma label_does_not_occur:
1144  ∀i:ℕ.∀p:list labelled_instruction.∀l:Identifier.
1145  is_label (nth i ? p 〈None ?, Comment [ ]〉) l → does_not_occur ?? l p = false.
1146 #i #p #l generalize in match i; elim p
1147 [ #i >nth_nil #H cases H
1148 | #h #t #IH #i cases i -i
1149   [ cases h #hi #hp cases hi
1150     [ normalize #H cases H
1151     | #l' #Heq whd in ⊢ (??%?); change with (eq_identifier ? l' l) in match (instruction_matches_identifier ????);
1152       whd in Heq; >Heq
1153       >eq_identifier_refl / by refl/
1154     ]
1155   | #i #H whd in match (does_not_occur ????);
1156     whd in match (instruction_matches_identifier ????);
1157     cases h #hi #hp cases hi normalize nodelta
1158     [ @(IH i) @H
1159     | #l' @eq_identifier_elim
1160       [ normalize / by /
1161       | normalize #_ @(IH i) @H
1162       ]
1163     ]
1164   ]
1165 ]
1166qed.
1167
1168(* The function that creates the label-to-address map *)
1169definition create_label_cost_map0: ∀program:list labelled_instruction.
1170  (Σlabels_costs:label_map × (BitVectorTrie costlabel 16). (* Both on ppcs *)
1171    let 〈labels,costs〉 ≝ labels_costs in
1172    ∀l.occurs_exactly_once ?? l program →
1173    bitvector_of_nat ? (lookup_def ?? labels l 0) =
1174     address_of_word_labels_code_mem program l
1175  ) ≝
1176  λprogram.
1177  \fst (pi1 ?? (foldl_strong (option Identifier × pseudo_instruction)
1178  (λprefix.Σlabels_costs_ppc:label_map × (BitVectorTrie costlabel 16) × ℕ.
1179    let 〈labels,costs,ppc〉 ≝ labels_costs_ppc in
1180    ppc = |prefix| ∧
1181    ∀l.occurs_exactly_once ?? l prefix →
1182    bitvector_of_nat ? (lookup_def ?? labels l 0) =
1183     address_of_word_labels_code_mem prefix l)
1184  program
1185  (λprefix.λx.λtl.λprf.λlabels_costs_ppc.
1186   let 〈labels,costs,ppc〉 ≝ pi1 ?? labels_costs_ppc in
1187   let 〈label,instr〉 ≝ x in
1188   let labels ≝
1189     match label with
1190     [ None   ⇒ labels
1191     | Some l ⇒ add … labels l ppc
1192     ] in
1193   let costs ≝
1194     match instr with
1195     [ Cost cost ⇒ insert … (bitvector_of_nat ? ppc) cost costs
1196     | _ ⇒ costs ] in
1197      〈labels,costs,S ppc〉
1198   ) 〈(empty_map …),(Stub ??),0〉)).
1199[ normalize nodelta lapply (pi2 … labels_costs_ppc) >p >p1 normalize nodelta * #IH1 #IH2
1200  -labels_costs_ppc % [>IH1 >length_append <plus_n_Sm <plus_n_O %]
1201 inversion label [#EQ | #l #EQ]
1202 [ #lbl #Hocc <address_of_word_labels_code_mem_None [2: @Hocc] normalize nodelta
1203   >occurs_exactly_once_None in Hocc; @(IH2 lbl)
1204 | #lbl normalize nodelta inversion (eq_identifier ? lbl l)
1205   [ #Heq #Hocc >(eq_identifier_eq … Heq)
1206     >address_of_word_labels_code_mem_Some_hit
1207     [ >IH1 >lookup_def_add_hit %
1208     | <(eq_identifier_eq … Heq) in Hocc; //
1209     ]
1210   | #Hneq #Hocc
1211     <address_of_word_labels_code_mem_Some_miss
1212     [ >lookup_def_add_miss
1213       [ @IH2 >occurs_exactly_once_Some_eq in Hocc; >eq_identifier_sym> Hneq //
1214       | % @neq_identifier_neq @Hneq
1215       ]
1216     | @Hocc
1217     | >eq_identifier_sym @Hneq
1218     ]
1219   ]
1220 ]
1221| @pair_elim * #labels #costs #ppc #EQ destruct normalize nodelta % try %
1222  #l #abs cases (abs)
1223| cases (foldl_strong ? (λ_.Σx.?) ???) * * #labels #costs #ppc normalize nodelta *
1224  #_ #H @H
1225]
1226qed.
1227
1228(* The function that creates the label-to-address map *)
1229definition create_label_cost_map: ∀program:list labelled_instruction.
1230  label_map × (BitVectorTrie costlabel 16) ≝
1231    λprogram.
1232      pi1 … (create_label_cost_map0 program).
1233
1234theorem create_label_cost_map_ok:
1235 ∀pseudo_program: pseudo_assembly_program.
1236   let 〈labels, costs〉 ≝ create_label_cost_map (\snd pseudo_program) in
1237    ∀id. occurs_exactly_once ??  id (\snd pseudo_program) →
1238     bitvector_of_nat ? (lookup_def ?? labels id 0) = address_of_word_labels_code_mem (\snd pseudo_program) id.
1239 #p change with (pi1 … (create_label_cost_map0 ?)) in match (create_label_cost_map ?); @pi2
1240qed.
1241
1242(*CSC: move elsewhere; also proved in CostProofs as shift_nth_safe *)
1243lemma nth_safe_prepend:
1244 ∀A,l1,l2,j.∀H:j<|l2|.∀K:|l1|+j<|(l1@l2)|.
1245  nth_safe A j l2 H =nth_safe A (|l1|+j) (l1@l2) K.
1246 #A #l1 elim l1 normalize //
1247qed.
1248
1249(*CSC: move elsewhere; also proved in CostProofs as shift_nth_prefix *)
1250lemma shift_nth_prefix:
1251 ∀T,l1,i,l2,K1,K2.
1252  nth_safe T i l1 K1 = nth_safe T i (l1@l2) K2.
1253  #T #l1 elim l1 normalize
1254  [
1255    #i #l1 #K1 cases(lt_to_not_zero … K1)
1256  |
1257    #hd #tl #IH #i #l2
1258    cases i
1259    [
1260      //
1261    |
1262      #i' #K1 #K2 whd in ⊢ (??%%);
1263      @IH
1264    ]
1265  ]
1266qed.
1267
1268lemma nth_cons:
1269 ∀A,hd,tl,l2,j,d.
1270  nth j A (tl@l2) d =nth (1+j) A (hd::tl@l2) d.
1271//
1272qed.
1273
1274(*CSC: move elsewhere *)
1275lemma fetch_pseudo_instruction_append:
1276 ∀l1,l2,ppc.
1277  let code_newppc ≝ fetch_pseudo_instruction l2 ppc in
1278  fetch_pseudo_instruction (l1@l2) (add … (bitvector_of_nat … (|l1|)) (ppc)) =
1279  〈\fst code_newppc, add … (bitvector_of_nat … (|l1|)) (\snd code_newppc)〉.
1280 #l1 elim l1
1281 [ #l2 #ppc >add_commutative <add_zero >add_commutative <add_zero //
1282 | #hd #tl #IH #l2 #ppc whd whd in match fetch_pseudo_instruction in ⊢ (??%?); normalize nodelta
1283   (*CSC: FALSE, NEED INVARIANT? *)
1284   > (?: nat_of_bitvector … (add 16 (bitvector_of_nat 16 (|hd::tl|)) ppc)
1285       = 1 + nat_of_bitvector … (add … (bitvector_of_nat … (|tl|)) ppc)) [2: cases daemon]
1286   <nth_cons lapply (IH l2 ppc) -IH normalize nodelta cases (fetch_pseudo_instruction l2 ppc)
1287   #i #newppc whd in match fetch_pseudo_instruction; normalize nodelta
1288   cases (nth ? labelled_instruction ??) #i' #newppc' normalize nodelta #EQ
1289   destruct -EQ change with (add ??? = ?) in e0;
1290   (*CSC: TRUE, NEEDS TRIVIAL ARITHMETICS *) cases daemon
1291 ]
1292qed.
1293
1294definition assembly:
1295    ∀p: pseudo_assembly_program.
1296    ∀sigma: Word → Word.
1297    ∀policy: Word → bool.
1298      Σres:list Byte × (BitVectorTrie costlabel 16).
1299       let 〈preamble,instr_list〉 ≝ p in
1300       let 〈assembled,costs〉 ≝ res in
1301       let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in
1302       let datalabels ≝ construct_datalabels preamble in
1303       let lookup_labels ≝ λx. sigma (bitvector_of_nat ? (lookup_def … labels_to_ppc x 0)) in
1304       let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
1305       ∀ppc.
1306        nat_of_bitvector … ppc < |instr_list| →
1307         let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc in
1308         let 〈len,assembledi〉 ≝
1309          assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in
1310         ∀j:nat. ∀H: j < |assembledi|. ∀K.
1311          nth_safe ? j assembledi H =
1312           nth_safe ? (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat ? j)))
1313            assembled K
1314
1315  λp.
1316  λsigma.
1317  λpolicy.
1318  deplet 〈preamble, instr_list〉 as p_refl ≝ p in
1319  let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in
1320  let datalabels ≝ construct_datalabels preamble in
1321  let lookup_labels ≝ λx. sigma (bitvector_of_nat ? (lookup_def … labels_to_ppc x 0)) in
1322  let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
1323  let 〈ignore,revcode〉 ≝ pi1 … (
1324     foldl_strong
1325      (option Identifier × pseudo_instruction)
1326      (λpre. Σppc_code:(Word × (list Byte)).
1327        let 〈ppc,code〉 ≝ ppc_code in
1328         ppc = bitvector_of_nat … (|pre|) ∧
1329         ∀ppc'.
1330          nat_of_bitvector … ppc' < nat_of_bitvector … ppc →
1331           let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' in
1332           let 〈len,assembledi〉 ≝
1333            assembly_1_pseudoinstruction lookup_labels sigma policy ppc' lookup_datalabels pi in
1334           ∀j:nat. ∀H: j < |assembledi|. ∀K.
1335            nth_safe ? j assembledi H =
1336             nth_safe ? (nat_of_bitvector … (add … (sigma ppc') (bitvector_of_nat ? j))) (reverse … code) K)
1337      instr_list
1338      (λprefix,hd,tl,prf,ppc_code.
1339        let 〈ppc, code〉 ≝ pi1 … ppc_code in
1340        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels (\snd hd) in
1341        let new_ppc ≝ add ? ppc (bitvector_of_nat ? 1) in
1342         〈new_ppc, (reverse … program @ code)〉)
1343      〈(zero ?), [ ]〉)
1344    in
1345     〈reverse … revcode,
1346      fold … (λppc.λcost.λpc_to_costs. insert … (sigma ppc) cost pc_to_costs) ppc_to_costs (Stub ??)〉.
1347  [ cases (foldl_strong ? (λx.Σy.?) ???) in p2; #ignore_revcode #Hfold #EQignore_revcode
1348    >EQignore_revcode in Hfold; * #_ #Hfold whd >p1 whd #ppc #LTppc @Hfold
1349    (* CSC: ??? *) cases daemon
1350  | % // #ppc' #abs @⊥ cases (not_le_Sn_O ?) [#H @(H abs) | skip]
1351  | cases ppc_code in p1; -ppc_code #ppc_code #IH #EQppc_code >EQppc_code in IH; -EQppc_code
1352    * #IH1 #IH2 % [ normalize nodelta >IH1 >length_append cases daemon (*CSC: TRUE, LEMMA NEEDED *)]
1353    whd #ppc' #LTppc' cases hd in prf p2; #label #pi #prf #p2
1354    cases (le_to_or_lt_eq … LTppc')
1355    [2: #S_S_eq normalize nodelta in S_S_eq;
1356        (*CSC: FALSE, NEEDS INVARIANT *)
1357        cut (ppc' = ppc) [cases daemon] -S_S_eq #EQppc' >EQppc' in LTppc'; -ppc' #LTppc
1358        >prf >IH1 in ⊢ match % with [_ ⇒ ?]; >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ match % with [_ ⇒ ?];
1359        @pair_elim #pi' #newppc' >fetch_pseudo_instruction_append #EQpair destruct(EQpair)
1360        >p2
1361        #j #LTj
1362        (* CSC: FALSE, NEEDS INVARIANT *)
1363        >(?: nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat … j)) =
1364             nat_of_bitvector … (sigma ppc) + j) [2: cases daemon]
1365        >reverse_append >reverse_reverse
1366        (* CSC: TRUE, NEEDS INVARIANT *)
1367        >(? : nat_of_bitvector … (sigma ppc) = |reverse … code|) [2: cases daemon]
1368        @nth_safe_prepend
1369    | #LTppc' lapply (IH2 ppc' ?) [ (*CSC: EASY, FINISH*) cases daemon ]
1370      @pair_elim #pi' #newppc' #eq_fetch_pseudoinstruction
1371      @pair_elim #len' #assembledi' #eq_assembly_1_pseudoinstruction #IH
1372      change with (let 〈len,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi' in ∀j:ℕ. ∀H:j<|assembledi|.?)
1373      >eq_assembly_1_pseudoinstruction #j #LTj >reverse_append >reverse_reverse #K
1374      >IH
1375      [2: (*CSC: FALSE, NEEDS INVARIANT? *) cases daemon
1376      | @shift_nth_prefix
1377      ]
1378    ]
1379  ] 
1380qed.
1381
1382definition assembly_unlabelled_program:
1383    assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝
1384  λp.
1385    Some … (〈foldr … (λi,l. assembly1 i @ l) [ ] p, Stub …〉).
Note: See TracBrowser for help on using the repository browser.