source: src/ASM/Assembly.ma @ 2032

Last change on this file since 2032 was 2032, checked in by sacerdot, 7 years ago

!! BEWARE: major commit !!

1) [affects everybody]

split for vectors renamed to vsplit to reduce ambiguity since split is
now also a function in the standard library.
Note: I have not been able to propagate the changes everywhere in
the front-end/back-end because some files do not compile

2) [affects everybody]

functions on Vectors copied both in the front and back-ends moved to
Vectors.ma

3) [affects only the back-end]

subaddressing_mode_elim redesigned from scratch and now also applied to
Policy.ma. Moreover, all daemons about that have been closed.
The new one is much simpler to apply since it behaves like a standard
elimination principle: @(subaddressing_mode_elim \ldots x) where x is
the thing to eliminate.

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