source: src/ASM/Assembly.ma @ 1905

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